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