merging tridge's code...
[sahlberg/ctdb.git] / ib / ibw_ctdb_init.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Join infiniband wrapper and ctdb.
4  *
5  * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006
6  *
7  * Major code contributions by Peter Somogyi <psomogyi@gamax.hu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include <system/network.h>
27 #include <assert.h>
28 #include "ctdb_private.h"
29 #include "ibwrapper.h"
30 #include "ibw_ctdb.h"
31
32 /* not nice; temporary workaround for the current implementation... */
33 static void *last_key = NULL;
34
35 static int ctdb_ibw_listen(struct ctdb_context *ctdb, int backlog)
36 {
37         struct ibw_ctx *ictx = talloc_get_type(ctdb->private, struct ibw_ctx);
38         struct sockaddr_in my_addr;
39
40         assert(ictx!=NULL);
41         memset(&my_addr, 0, sizeof(struct sockaddr_in));
42         my_addr.sin_port = htons(ctdb->address.port);
43         my_addr.sin_family = PF_INET;
44         inet_pton(AF_INET, ctdb->address.address, &my_addr.sin_addr);
45
46         if (ibw_bind(ictx, &my_addr)) {
47                 DEBUG(0, ("ctdb_ibw_listen: ibw_bind failed\n"));
48                 return -1;
49         }
50
51         if (ibw_listen(ictx, backlog)) {
52                 DEBUG(0, ("ctdb_ibw_listen: ibw_listen failed\n"));
53                 return -1;
54         }
55
56         return 0;
57 }
58
59 int ctdb_ibw_node_connect(struct ibw_ctx *ictx, struct ctdb_node *node)
60 {
61         struct sockaddr_in sock_out;
62
63         inet_pton(AF_INET, node->address.address, &sock_out.sin_addr);
64         sock_out.sin_port = htons(node->address.port);
65         sock_out.sin_family = PF_INET;
66
67         if (ibw_connect(ictx, &sock_out, node)) {
68                 DEBUG(0, ("ctdb_ibw_node_connect: ibw_connect failed\n"));
69                 return -1;
70         }
71
72         /* continues at ibw_ctdb.c/IBWC_CONNECTED in good case */
73         return 0;
74 }
75
76 /*
77  * Start infiniband
78  */
79 static int ctdb_ibw_start(struct ctdb_context *ctdb)
80 {
81         struct ibw_ctx *ictx = talloc_get_type(ctdb->private, struct ibw_ctx);
82         int i;
83
84         /* listen on our own address */
85         if (ctdb_ibw_listen(ctdb, 10)) /* TODO: backlog as param */
86                 return -1;
87
88         /* everything async here */
89         for (i=0;i<ctdb->num_nodes;i++) {
90                 struct ctdb_node *node = ctdb->nodes[i];
91                 if (!(ctdb->flags & CTDB_FLAG_SELF_CONNECT) &&
92                         ctdb_same_address(&ctdb->address, &node->address))
93                         continue;
94                 ctdb_ibw_node_connect(ictx, node);
95         }
96
97         return 0;
98 }
99
100
101 /*
102  * initialise ibw portion of a ctdb node 
103  */
104 static int ctdb_ibw_add_node(struct ctdb_node *node)
105 {
106         /* TODO: clarify whether is this necessary for us ?
107            - why not enough doing such thing internally at connect time ? */
108         return 0;
109 }
110
111 static int ctdb_ibw_queue_pkt(struct ctdb_node *node, uint8_t *data, uint32_t length)
112 {
113         struct ibw_conn *conn = talloc_get_type(node->private, struct ibw_conn);
114         int     rc;
115
116         rc = ibw_send(conn, data, last_key, length);
117         last_key = NULL;
118
119         return rc;
120 }
121
122 #ifdef __NOTDEF__
123 /*
124  * transport packet allocator - allows transport to control memory for packets
125  */
126 static void *ctdb_ibw_allocate_pkt(struct ctdb_node *node, size_t size)
127 {
128         struct ibw_conn *conn = NULL;
129         void *buf = NULL;
130
131         if (ibw_alloc_send_buf(conn, &buf, &last_key, size))
132                 return NULL;
133
134         return buf;
135 }
136
137 static void ctdb_ibw_dealloc_pkt(struct ctdb_node *node, void *data)
138 {
139         if (last_key) {
140                 struct ibw_conn *conn = talloc_get_type(node->private, struct ibw_conn);
141         
142                 assert(conn!=NULL);
143                 ibw_cancel_send_buf(conn, data, last_key);
144         } /* else ibw_send is already using it and will free it after completion */
145 }
146
147 static int ctdb_ibw_stop(struct ctdb_context *cctx)
148 {
149         struct ibw_ctx *ictx = talloc_get_type(cctx->private, struct ibw_ctx);
150
151         assert(ictx!=NULL);
152         return ibw_stop(ictx);
153 }
154
155 #endif /* __NOTDEF__ */
156
157 static const struct ctdb_methods ctdb_ibw_methods = {
158         .start     = ctdb_ibw_start,
159         .add_node  = ctdb_ibw_add_node,
160         .queue_pkt = ctdb_ibw_queue_pkt,
161 //      .allocate_pkt = ctdb_ibw_allocate_pkt,
162
163 //      .dealloc_pkt = ctdb_ibw_dealloc_pkt,
164 //      .stop = ctdb_ibw_stop
165 };
166
167 /*
168  * initialise ibw portion of ctdb 
169  */
170 int ctdb_ibw_init(struct ctdb_context *ctdb)
171 {
172         struct ibw_ctx *ictx;
173
174         ictx = ibw_init(
175                 NULL, //struct ibw_initattr *attr, /* TODO */
176                 0, //int nattr, /* TODO */
177                 ctdb,
178                 ctdb_ibw_connstate_handler,
179                 ctdb_ibw_receive_handler,
180                 ctdb->ev);
181
182         if (ictx==NULL) {
183                 DEBUG(0, ("ctdb_ibw_init: ibw_init failed\n"));
184                 return -1;
185         }
186
187         ctdb->methods = &ctdb_ibw_methods;
188         ctdb->private = ictx;
189         return 0;
190 }