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