don't start the transport connecting to the other nodes until after the startup event...
[vlendec/samba-autobuild/.git] / ctdb / 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 static int ctdb_ibw_listen(struct ctdb_context *ctdb, int backlog)
33 {
34         struct ibw_ctx *ictx = talloc_get_type(ctdb->private_data, struct ibw_ctx);
35         struct sockaddr_in my_addr;
36
37         assert(ictx!=NULL);
38         memset(&my_addr, 0, sizeof(struct sockaddr_in));
39         my_addr.sin_port = htons(ctdb->address.port);
40         my_addr.sin_family = PF_INET;
41         if (ctdb_ibw_get_address(ctdb, ctdb->address.address, &my_addr.sin_addr))
42                 return -1;
43
44         if (ibw_bind(ictx, &my_addr)) {
45                 DEBUG(0, ("ctdb_ibw_listen: ibw_bind failed\n"));
46                 return -1;
47         }
48
49         if (ibw_listen(ictx, backlog)) {
50                 DEBUG(0, ("ctdb_ibw_listen: ibw_listen failed\n"));
51                 return -1;
52         }
53
54         return 0;
55 }
56
57 /*
58  * initialise ibw portion of a ctdb node 
59  */
60 static int ctdb_ibw_add_node(struct ctdb_node *node)
61 {
62         struct ibw_ctx *ictx = talloc_get_type(node->ctdb->private_data, struct ibw_ctx);
63         struct ctdb_ibw_node *cn = talloc_zero(node, struct ctdb_ibw_node);
64
65         assert(cn!=NULL);
66         cn->conn = ibw_conn_new(ictx, node);
67         node->private_data = (void *)cn;
68
69         return (cn->conn!=NULL ? 0 : -1);
70 }
71
72 /*
73  * initialise infiniband
74  */
75 static int ctdb_ibw_initialise(struct ctdb_context *ctdb)
76 {
77         int i, ret;
78
79         ret = ctdb_ibw_init(ctdb);
80         if (ret != 0) {
81                 return ret;
82         }
83
84         for (i=0; i<ctdb->num_nodes; i++) {
85                 if (ctdb_ibw_add_node(ctdb->nodes[i]) != 0) {
86                         DEBUG(0, ("methods->add_node failed at %d\n", i));
87                         return -1;
88                 }
89         }
90
91         return 0;
92 }
93
94
95 /*
96  * Start infiniband
97  */
98 static int ctdb_ibw_start(struct ctdb_context *ctdb)
99 {
100         int i, ret;
101
102         /* listen on our own address */
103         if (ctdb_ibw_listen(ctdb, 10)) /* TODO: backlog as param */
104                 return -1;
105
106         /* everything async here */
107         for (i=0;i<ctdb->num_nodes;i++) {
108                 struct ctdb_node *node = ctdb->nodes[i];
109                 if (!(ctdb->flags & CTDB_FLAG_SELF_CONNECT) &&
110                         ctdb_same_address(&ctdb->address, &node->address))
111                         continue;
112                 ctdb_ibw_node_connect(node);
113         }
114
115         return 0;
116 }
117
118 static int ctdb_ibw_send_pkt(struct ibw_conn *conn, uint8_t *data, uint32_t length)
119 {
120         void    *buf, *key;
121
122         if (ibw_alloc_send_buf(conn, &buf, &key, length)) {
123                 DEBUG(0, ("queue_pkt/ibw_alloc_send_buf failed\n"));
124                 return -1;
125         }
126
127         memcpy(buf, data, length);
128         return ibw_send(conn, buf, key, length);
129 }
130
131 int ctdb_flush_cn_queue(struct ctdb_ibw_node *cn)
132 {
133         struct ctdb_ibw_msg *p;
134         int     rc = 0;
135
136         while(cn->queue) {
137                 p = cn->queue;
138                 rc = ctdb_ibw_send_pkt(cn->conn, p->data, p->length);
139                 if (rc)
140                         return -1; /* will be retried later when conn is up */
141
142                 DLIST_REMOVE(cn->queue, p);
143                 cn->qcnt--;
144                 talloc_free(p); /* it will talloc_free p->data as well */
145         }
146         assert(cn->qcnt==0);
147         /* cn->queue_last = NULL is not needed - see DLIST_ADD_AFTER */
148
149         return rc;
150 }
151
152 static int ctdb_ibw_queue_pkt(struct ctdb_node *node, uint8_t *data, uint32_t length)
153 {
154         struct ctdb_ibw_node *cn = talloc_get_type(node->private_data, struct ctdb_ibw_node);
155         int     rc;
156
157         assert(length>=sizeof(uint32_t));
158         assert(cn!=NULL);
159
160         if (cn->conn==NULL) {
161                 DEBUG(0, ("ctdb_ibw_queue_pkt: conn is NULL\n"));
162                 return -1;
163         }
164
165         if (cn->conn->state==IBWC_CONNECTED) {
166                 rc = ctdb_ibw_send_pkt(cn->conn, data, length);
167         } else {
168                 struct ctdb_ibw_msg *p = talloc_zero(cn, struct ctdb_ibw_msg);
169                 p->data = talloc_memdup(p, data, length);
170                 p->length = length;
171
172                 DLIST_ADD_AFTER(cn->queue, p, cn->queue_last);
173                 cn->queue_last = p;
174                 cn->qcnt++;
175
176                 rc = 0;
177         }
178
179         return rc;
180 }
181
182 /*
183  * transport packet allocator - allows transport to control memory for packets
184  */
185 static void *ctdb_ibw_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
186 {
187         /* TODO: use ibw_alloc_send_buf instead... */
188         return talloc_size(mem_ctx, size);
189 }
190
191 #ifdef __NOTDEF__
192
193 static int ctdb_ibw_stop(struct ctdb_context *cctx)
194 {
195         struct ibw_ctx *ictx = talloc_get_type(cctx->private_data, struct ibw_ctx);
196
197         assert(ictx!=NULL);
198         return ibw_stop(ictx);
199 }
200
201 #endif /* __NOTDEF__ */
202
203 static const struct ctdb_methods ctdb_ibw_methods = {
204         .initialise= ctdb_ibw_initialise,
205         .start     = ctdb_ibw_start,
206         .queue_pkt = ctdb_ibw_queue_pkt,
207         .add_node = ctdb_ibw_add_node,
208         .allocate_pkt = ctdb_ibw_allocate_pkt,
209
210 //      .stop = ctdb_ibw_stop
211 };
212
213 /*
214  * initialise ibw portion of ctdb 
215  */
216 int ctdb_ibw_init(struct ctdb_context *ctdb)
217 {
218         struct ibw_ctx *ictx;
219
220         DEBUG(10, ("ctdb_ibw_init invoked...\n"));
221         ictx = ibw_init(
222                 NULL, //struct ibw_initattr *attr, /* TODO */
223                 0, //int nattr, /* TODO */
224                 ctdb,
225                 ctdb_ibw_connstate_handler,
226                 ctdb_ibw_receive_handler,
227                 ctdb->ev);
228
229         if (ictx==NULL) {
230                 DEBUG(0, ("ctdb_ibw_init: ibw_init failed\n"));
231                 return -1;
232         }
233
234         ctdb->methods = &ctdb_ibw_methods;
235         ctdb->private_data = ictx;
236         
237         DEBUG(10, ("ctdb_ibw_init succeeded.\n"));
238         return 0;
239 }