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