merging tridge's code...
[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_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
33 {
34         if (ctx!=NULL) {
35                 /* ctx->state changed */
36                 switch(ctx->state) {
37                 case IBWS_INIT: /* ctx start - after ibw_init */
38                         break;
39                 case IBWS_READY: /* after ibw_bind & ibw_listen */
40                         break;
41                 case IBWS_CONNECT_REQUEST: /* after [IBWS_READY + incoming request] */
42                                 /* => [(ibw_accept)IBWS_READY | (ibw_disconnect)STOPPED | ERROR] */
43                         if (ibw_accept(ctx, conn, NULL)) {
44                                 DEBUG(0, ("connstate_handler/ibw_accept failed\n"));
45                                 return -1;
46                         } /* else continue in IBWC_CONNECTED */
47                         break;
48                 case IBWS_STOPPED: /* normal stop <= ibw_disconnect+(IBWS_READY | IBWS_CONNECT_REQUEST) */
49                         /* TODO: have a CTDB upcall for which CTDB should wait in a (final) loop */
50                         break;
51                 case IBWS_ERROR: /* abnormal state; ibw_stop must be called after this */
52                         break;
53                 default:
54                         assert(0);
55                         break;
56                 }
57         }
58
59         if (conn!=NULL) {
60                 /* conn->state changed */
61                 switch(conn->state) {
62                 case IBWC_INIT: /* conn start - internal state */
63                         break;
64                 case IBWC_CONNECTED: { /* after ibw_accept or ibw_connect */
65                         struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node);
66                         if (node!=NULL) /* after ibw_connect */
67                                 node->ctdb->upcalls->node_connected(node);
68                         else { /* after ibw_accept */
69                                 /* NOP in CTDB case */
70                         }
71                 } break;
72                 case IBWC_DISCONNECTED: /* after ibw_disconnect */
73                         /* TODO: have a CTDB upcall */
74                         break;
75                 case IBWC_ERROR: {
76                         struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node);
77                         if (node!=NULL)
78                                 node->ctdb->upcalls->node_connected(node);
79                 } break;
80                 default:
81                         assert(0);
82                         break;
83                 }
84         }
85
86         return 0;
87 }
88
89 int ctdb_ibw_receive_handler(struct ibw_conn *conn, void *buf, int n)
90 {
91         struct ctdb_context *ctdb = talloc_get_type(conn->ctx->ctx_userdata, struct ctdb_context);
92
93         assert(ctdb!=NULL);
94         assert(conn->state==IBWC_CONNECTED);
95
96         /* TODO: shall I short-circuit this in ibwrapper? */
97         /* maybe when everything go fine... */
98
99         /* TODO2: !!! here I can provide conn->conn_userdata (with no perf. penalty) -
100          * as struct ctdb_node in case the connection
101          * has been built up by ibw_connect !!! */
102         ctdb->upcalls->recv_pkt(ctdb, (uint8_t *)buf, (uint32_t)n);
103
104         return 0;
105 }