From f205d2e6729e48b56ae659e426f71385870e7afb Mon Sep 17 00:00:00 2001 From: Timo Dritschler Date: Fri, 25 Apr 2014 12:19:50 +0200 Subject: Added first draft of 'KIRO Server' class Updated Makefile --- Makefile | 6 +- kiro-server.c | 241 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ kiro-server.h | 83 ++++++++++++++++++++ 3 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 kiro-server.c create mode 100644 kiro-server.h diff --git a/Makefile b/Makefile index 26e4da3..104ee44 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LDFLAGS= -lrdmacm -libverbs -lpthread $(shell pkg-config --libs gobject-2.0) all: base -base: kiro-trb.o kiro-client.o +base: kiro-trb.o kiro-client.o kiro-server.o kiro-cbr.o: kiro-trb.c kiro-trb.h $(CC) $(CFLAGS) $(LDFLAGS) -c kiro-trb.c -o kiro-trb.o @@ -13,6 +13,10 @@ kiro-cbr.o: kiro-trb.c kiro-trb.h kiro-client.o: kiro-client.c kiro-client.h $(CC) $(CFLAGS) $(LDFLAGS) -c kiro-client.c -o kiro-client.o +kiro-server.o: kiro-server.c kiro-server.h + $(CC) $(CFLAGS) $(LDFLAGS) -c kiro-server.c -o kiro-server.o + + test-trb: test diff --git a/kiro-server.c b/kiro-server.c new file mode 100644 index 0000000..2636817 --- /dev/null +++ b/kiro-server.c @@ -0,0 +1,241 @@ +/* Copyright (C) 2014 Timo Dritschler + (Karlsruhe Institute of Technology) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2.1 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License along + with this library; if not, write to the Free Software Foundation, Inc., 51 + Franklin St, Fifth Floor, Boston, MA 02110, USA +*/ + +/** + * SECTION: kiro-server + * @Short_description: KIRO RDMA Server / Consumer + * @Title: KiroServer + * + * KiroServer implements the server / passive / provider side of the the RDMA + * Communication Channel. It uses a KIRO-TRB to manage its data. + */ + +#include +#include +#include +#include +#include +#include +#include "kiro-server.h" +#include "kiro-rdma.h" + + +/* + * Definition of 'private' structures and members and macro to access them + */ + +#define KIRO_SERVER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KIRO_TYPE_SERVER, KiroServerPrivate)) + +struct _KiroServerPrivate { + + /* Properties */ + // PLACEHOLDER // + + /* 'Real' private structures */ + /* (Not accessible by properties) */ + struct rdma_event_channel *ec; // Main Event Channel + struct rdma_cm_id *base; // Base-Listening-Connection + struct kiro_connection *client; // Connection to the client + + +}; + + +G_DEFINE_TYPE_WITH_PRIVATE (KiroServer, kiro_server, G_TYPE_OBJECT); + + +static void kiro_server_init (KiroServer *self) +{ + KiroServerPrivate *priv = KIRO_SERVER_GET_PRIVATE(self); + memset(priv, 0, sizeof(&priv)); +} + +static void +kiro_server_finalize (GObject *object) +{ + //PASS +} + +static void +kiro_server_class_init (KiroServerClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + gobject_class->finalize = kiro_server_finalize; +} + + + +int kiro_server_start (KiroServer *self, char *address, char *port) +{ + KiroServerPrivate *priv = KIRO_SERVER_GET_PRIVATE(self); + + if(priv->base) + { + printf("Server already started.\n"); + return -1; + } + + struct rdma_addrinfo hints, *res_addrinfo; + memset(&hints, 0, sizeof(hints)); + hints.ai_port_space = RDMA_PS_IB; + hints.ai_flags = RAI_PASSIVE; + if(rdma_getaddrinfo(address, port, &hints, &res_addrinfo)) + { + printf("Failed to bind to address %s:%s\n",address, port); + return -1; + } + + struct ibv_qp_init_attr qp_attr; + memset(&qp_attr, 0, sizeof(qp_attr)); + qp_attr.cap.max_send_wr = 10; + qp_attr.cap.max_recv_wr = 10; + qp_attr.cap.max_send_sge = 1; + qp_attr.cap.max_recv_sge = 1; + qp_attr.qp_context = priv->base; + qp_attr.sq_sig_all = 1; + + if(rdma_create_ep(&(priv->base), res_addrinfo, NULL, &qp_attr)) + { + printf("Endpoint creation failed.\n"); + return -1; + } + + if(rdma_listen(priv->base, NULL)) + { + printf("Failed to put server into listening state.\n"); + rdma_destroy_ep(priv->base); + return -1; + } + + priv->client = (struct kiro_connection *)calloc(1, sizeof(struct kiro_connection)); + if(!priv->client) + { + printf("Failed to create container for client connection.\n"); + rdma_destroy_ep(priv->base); + return -1; + } + priv->client->identifier = 0; //First Client + + if(rdma_get_request(priv->base, &(priv->client->id))) + { + printf("Failure waiting for clienet connection.\n"); + rdma_destroy_ep(priv->base); + return -1; + } + + struct kiro_connection_context *ctx = (struct kiro_connection_context *)calloc(1,sizeof(struct kiro_connection_context)); + if(!ctx) + { + printf("Failed to create connection context.\n"); + rdma_destroy_ep(priv->base); + rdma_destroy_ep(priv->client->id); + free(priv->client); + return -1; + } + + ctx->cf_mr_send = (struct kiro_rdma_mem *)calloc(1, sizeof(struct kiro_rdma_mem)); + ctx->cf_mr_recv = (struct kiro_rdma_mem *)calloc(1, sizeof(struct kiro_rdma_mem)); + if(!ctx->cf_mr_recv || !ctx->cf_mr_send) + { + printf("Failed to allocate Control Flow Memory Container.\n"); + kiro_destroy_connection_context(ctx); + rdma_destroy_ep(priv->base); + rdma_destroy_ep(priv->client->id); + free(priv->client); + return -1; + } + + ctx->cf_mr_recv = kiro_create_rdma_memory(priv->client->id->pd, sizeof(struct kiro_ctrl_msg), IBV_ACCESS_LOCAL_WRITE); + ctx->cf_mr_send = kiro_create_rdma_memory(priv->client->id->pd, sizeof(struct kiro_ctrl_msg), IBV_ACCESS_LOCAL_WRITE); + if(!ctx->cf_mr_recv || !ctx->cf_mr_send) + { + printf("Failed to register control message memory.\n"); + kiro_destroy_connection_context(ctx); + rdma_destroy_ep(priv->base); + rdma_destroy_ep(priv->client->id); + free(priv->client); + return -1; + } + ctx->cf_mr_recv->size = ctx->cf_mr_send->size = sizeof(struct kiro_ctrl_msg); + priv->client->id->context = ctx; + + if(!rdma_post_recv(priv->client->id, priv->client, ctx->cf_mr_recv->mem, ctx->cf_mr_recv->size, ctx->cf_mr_recv->mr)) + { + printf("Posting preemtive receive for connection failed.\n"); + kiro_destroy_connection_context(ctx); + rdma_destroy_ep(priv->base); + rdma_destroy_ep(priv->client->id); + free(priv->client); + return -1; + } + + if(rdma_accept(priv->client->id, NULL)) + { + printf("Failed to establish connection to the server.\n"); + kiro_destroy_connection_context(ctx); + rdma_destroy_ep(priv->base); + rdma_destroy_ep(priv->client->id); + free(priv->client); + return -1; + } + + priv->ec = rdma_create_event_channel(); + int oldflags = fcntl (priv->ec->fd, F_GETFL, 0); + /* Only change the FD Mode if we were able to get its flags */ + if (oldflags >= 0) { + oldflags |= O_NONBLOCK; + /* Store modified flag word in the descriptor. */ + fcntl (priv->ec->fd, F_SETFL, oldflags); + } + if(rdma_migrate_id(priv->base, priv->ec)) + { + printf("Was unable to migrate connection to new Event Channel.\n"); + rdma_disconnect(priv->client->id); + kiro_destroy_connection_context(ctx); + rdma_destroy_ep(priv->base); + rdma_destroy_ep(priv->client->id); + free(priv->client); + return -1; + } + + //ToDo: + //Create TRB, request RDMA from Server, call kiro_server_sync, ???, Profit! + + + printf("Client Connected.\n"); + return 0; + +} + + + +int kiro_server_sync (KiroServer *self) +{ + //PASS + return 0; +} + + + + + + + + + + diff --git a/kiro-server.h b/kiro-server.h new file mode 100644 index 0000000..6e7973c --- /dev/null +++ b/kiro-server.h @@ -0,0 +1,83 @@ +/* Copyright (C) 2014 Timo Dritschler + (Karlsruhe Institute of Technology) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2.1 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License along + with this library; if not, write to the Free Software Foundation, Inc., 51 + Franklin St, Fifth Floor, Boston, MA 02110, USA +*/ + +/** + * SECTION: kiro-server + * @Short_description: KIRO RDMA Server / Consumer + * @Title: KiroServer + * + * KiroServer implements the server / passive / provider side of the the RDMA + * Communication Channel. It uses a KIRO-TRB to manage its data. + */ + +#ifndef __KIRO_SERVER_H +#define __KIRO_SERVER_H + +#include +#include + +G_BEGIN_DECLS + +#define KIRO_TYPE_SERVER (kiro_server_get_type()) +#define KIRO_SERVER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), KIRO_TYPE_SERVER, KiroServer)) +#define KIRO_IS_SERVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), KIRO_TYPE_SERVER)) +#define KIRO_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KIRO_TYPE_SERVER, KiroServerClass)) +#define KIRO_IS_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KIRO_TYPE_SERVER)) +#define KIRO_SERVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), KIRO_TYPE_SERVER, KiroServerClass)) + + +typedef struct _KiroServer KiroServer; +typedef struct _KiroServerClass KiroServerClass; +typedef struct _KiroServerPrivate KiroServerPrivate; + + +struct _KiroServer { + + GObject parent; + + /*< private >*/ + KiroServerPrivate *priv; +}; + + +/** + * IbvConnectorInterface: + * + * Base interface for IbvConnectors. + */ + +struct _KiroServerClass { + + GObjectClass parent_class; + +}; + + + +/* GObject and GType functions */ +GType kiro_server_get_type (void); + +GObject kiro_server_new (void); + +/* server functions */ + +int kiro_server_start (KiroServer*, char*, char*); + +G_END_DECLS + +#endif //__KIRO_SERVER_H \ No newline at end of file -- cgit v1.2.3