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