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