c3d1dc368c7c9f80f46ac7f9e628b58b07a98c88
[ira/wip.git] / source / cluster / ctdb / ib / ibw_ctdb.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 "lib/events/events.h"
25 #include <system/network.h>
26 #include <assert.h>
27 #include "ctdb_private.h"
28 #include "ibwrapper.h"
29 #include "ibw_ctdb.h"
30
31 int ctdb_ibw_node_connect(struct ctdb_node *node)
32 {
33         struct ctdb_ibw_node *cn = talloc_get_type(node->private_data, struct ctdb_ibw_node);
34         int     rc;
35
36         assert(cn!=NULL);
37         assert(cn->conn!=NULL);
38         struct sockaddr_in sock_out;
39
40         memset(&sock_out, 0, sizeof(struct sockaddr_in));
41         inet_pton(AF_INET, node->address.address, &sock_out.sin_addr);
42         sock_out.sin_port = htons(node->address.port);
43         sock_out.sin_family = PF_INET;
44
45         rc = ibw_connect(cn->conn, &sock_out, node);
46         if (rc) {
47                 DEBUG(0, ("ctdb_ibw_node_connect/ibw_connect failed - retrying...\n"));
48                 /* try again once a second */
49                 event_add_timed(node->ctdb->ev, node, timeval_current_ofs(1, 0), 
50                         ctdb_ibw_node_connect_event, node);
51         }
52
53         /* continues at ibw_ctdb.c/IBWC_CONNECTED in good case */
54         return 0;
55 }
56
57 void ctdb_ibw_node_connect_event(struct event_context *ev, struct timed_event *te, 
58         struct timeval t, void *private_data)
59 {
60         struct ctdb_node *node = talloc_get_type(private_data, struct ctdb_node);
61
62         ctdb_ibw_node_connect(node);
63 }
64
65 int ctdb_ibw_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
66 {
67         if (ctx!=NULL) {
68                 /* ctx->state changed */
69                 switch(ctx->state) {
70                 case IBWS_INIT: /* ctx start - after ibw_init */
71                         break;
72                 case IBWS_READY: /* after ibw_bind & ibw_listen */
73                         break;
74                 case IBWS_CONNECT_REQUEST: /* after [IBWS_READY + incoming request] */
75                                 /* => [(ibw_accept)IBWS_READY | (ibw_disconnect)STOPPED | ERROR] */
76                         if (ibw_accept(ctx, conn, NULL)) {
77                                 DEBUG(0, ("connstate_handler/ibw_accept failed\n"));
78                                 return -1;
79                         } /* else continue in IBWC_CONNECTED */
80                         break;
81                 case IBWS_STOPPED: /* normal stop <= ibw_disconnect+(IBWS_READY | IBWS_CONNECT_REQUEST) */
82                         /* TODO: have a CTDB upcall for which CTDB should wait in a (final) loop */
83                         break;
84                 case IBWS_ERROR: /* abnormal state; ibw_stop must be called after this */
85                         break;
86                 default:
87                         assert(0);
88                         break;
89                 }
90         }
91
92         if (conn!=NULL) {
93                 /* conn->state changed */
94                 switch(conn->state) {
95                 case IBWC_INIT: /* conn start - internal state */
96                         break;
97                 case IBWC_CONNECTED: { /* after ibw_accept or ibw_connect */
98                         struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node);
99                         if (node!=NULL) { /* after ibw_connect */
100                                 struct ctdb_ibw_node *cn = talloc_get_type(node->private_data, struct ctdb_ibw_node);
101
102                                 node->ctdb->upcalls->node_connected(node);
103                                 ctdb_flush_cn_queue(cn);
104                         } else { /* after ibw_accept */
105                                 /* NOP in CTDB case */
106                         }
107                 } break;
108                 case IBWC_DISCONNECTED: { /* after ibw_disconnect */
109                         struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node);
110                         if (node!=NULL)
111                                 node->ctdb->upcalls->node_dead(node);
112                         talloc_free(conn);
113                         /* normal + intended disconnect => not reconnecting in this layer */
114                 } break;
115                 case IBWC_ERROR: {
116                         struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node);
117                         if (node!=NULL) {
118                                 struct ctdb_ibw_node *cn = talloc_get_type(node->private_data, struct ctdb_ibw_node);
119                                 struct ibw_ctx *ictx = cn->conn->ctx;
120
121                                 DEBUG(10, ("IBWC_ERROR, reconnecting...\n"));
122                                 talloc_free(cn->conn); /* internal queue content is destroyed */
123                                 cn->conn = (void *)ibw_conn_new(ictx, node);
124                                 event_add_timed(node->ctdb->ev, node, timeval_current_ofs(1, 0),
125                                         ctdb_ibw_node_connect_event, node);
126                         }
127                 } break;
128                 default:
129                         assert(0);
130                         break;
131                 }
132         }
133
134         return 0;
135 }
136
137 int ctdb_ibw_receive_handler(struct ibw_conn *conn, void *buf, int n)
138 {
139         struct ctdb_context *ctdb = talloc_get_type(conn->ctx->ctx_userdata, struct ctdb_context);
140         void    *buf2; /* future TODO: a solution for removal of this */
141
142         assert(ctdb!=NULL);
143         assert(buf!=NULL);
144         assert(conn!=NULL);
145         assert(conn->state==IBWC_CONNECTED);
146
147         /* so far "buf" is an ib-registered memory area
148          * and being reused for next receive
149          * noticed that HL requires talloc-ed memory to be stolen */
150         buf2 = talloc_zero_size(conn, n);
151         memcpy(buf2, buf, n);
152
153         ctdb->upcalls->recv_pkt(ctdb, (uint8_t *)buf2, (uint32_t)n);
154
155         return 0;
156 }