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