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