merging tridge's code...
[vlendec/samba-autobuild/.git] / ctdb / ib / ibwrapper.h
1 /*
2  * Unix SMB/CIFS implementation.
3  * Wrap Infiniband calls.
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 /* Server communication state */
25 enum ibw_state_ctx {
26         IBWS_INIT = 0, /* ctx start - after ibw_init */
27         IBWS_READY, /* after ibw_bind & ibw_listen */
28         IBWS_CONNECT_REQUEST, /* after [IBWS_READY + incoming request] */
29                 /* => [(ibw_accept)IBWS_READY | (ibw_disconnect)STOPPED | ERROR] */
30         IBWS_STOPPED, /* normal stop <= ibw_disconnect+(IBWS_READY | IBWS_CONNECT_REQUEST) */
31         IBWS_ERROR /* abnormal state; ibw_stop must be called after this */
32 };
33
34 /* Connection state */
35 struct ibw_ctx {
36         void *ctx_userdata; /* see ibw_init */
37
38         enum ibw_state_ctx state;
39         void *internal;
40
41         struct ibw_conn *conn_list; /* 1st elem of double linked list */
42 };
43
44 enum ibw_state_conn {
45         IBWC_INIT = 0, /* conn start - internal state */
46         IBWC_CONNECTED, /* after ibw_accept or ibw_connect */
47         IBWC_DISCONNECTED, /* after ibw_disconnect */
48         IBWC_ERROR
49 };
50
51 struct ibw_conn {
52         struct ibw_ctx *ctx;
53         enum ibw_state_conn state;
54
55         void *conn_userdata; /* see ibw_connect and ibw_accept */
56         void *internal;
57
58         struct ibw_conn *prev, *next;
59 };
60
61 /*
62  * (name, value) pair for array param of ibw_init
63  */
64 struct ibw_initattr {
65         const char *name;
66         const char *value;
67 };
68
69 /*
70  * Callback function definition which should inform you about
71  * connection state change
72  * This callback is invoked whenever server or client connection changes.
73  * Both <conn> and <ctx> can be NULL if their state didn't change.
74  * Return nonzero on error.
75  */
76 typedef int (*ibw_connstate_fn_t)(struct ibw_ctx *ctx, struct ibw_conn *conn);
77
78 /*
79  * Callback function definition which should process incoming packets
80  * This callback is invoked whenever any message arrives.
81  * Return nonzero on error.
82  *
83  * Important: you mustn't store buf pointer for later use.
84  * Process its contents before returning.
85  */
86 typedef int (*ibw_receive_fn_t)(struct ibw_conn *conn, void *buf, int n);
87
88 /*
89  * settings: array of (name, value) pairs
90  * where name is one of:
91  *      max_send_wr [default is 256]
92  *      max_recv_wr [default is 1024]
93  * <...>
94  *
95  * Must be called _ONCE_ for each node.
96  *
97  * max_msg_size is the maximum size of a message
98  * (max_send_wr + max_recv_wr) * max_msg_size bytes allocated per connection
99  *
100  * returns non-NULL on success
101  *
102  * talloc_free must be called for the result in IBWS_STOPPED;
103  *    it will close resources by destructor
104  *    connections(ibw_conn *) must have been closed prior talloc_free
105  */
106 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
107         void *ctx_userdata,
108         ibw_connstate_fn_t ibw_connstate,
109         ibw_receive_fn_t ibw_receive,
110         struct event_context *ectx);
111
112 /*
113  * Must be called in states of (IBWS_ERROR, IBWS_READY, IBWS_CONNECT_REQUEST)
114  *
115  * It will send out disconnect requests and free up ibw_conn structures.
116  * The ctx->state will transit to IBWS_STOPPED after every conn are disconnected.
117  * During that time, you mustn't send/recv/disconnect any more.
118  * Only after ctx->state=IBWS_STOPPED you can talloc_free the ctx.
119  */
120 int ibw_stop(struct ibw_ctx *ctx);
121
122 /*************** connection initiation - like stream sockets *****/
123
124 /*
125  * works like socket bind
126  * needs a normal internet address here
127  *
128  * return 0 on success
129  */
130 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr);
131
132 /*
133  * works like socket listen
134  * non-blocking
135  * enables accepting incoming connections (after IBWS_READY)
136  * (it doesn't touch ctx->state by itself)
137  *
138  * returns 0 on success
139  */
140 int ibw_listen(struct ibw_ctx *ctx, int backlog);
141
142 /*
143  * works like socket accept
144  * initializes a connection to a client
145  * must be called when state=IBWS_CONNECT_REQUEST
146  *
147  * returns 0 on success
148  *
149  * You have +1 waiting here: you will get ibw_conn (having the
150  * same <conn_userdata> member) structure in ibw_connstate_fn_t.
151  *
152  * Important: you won't get remote IP address (only internal conn info)
153  */
154 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata);
155
156 /*
157  * Needs a normal internet address here
158  * can be called within IBWS_READY|IBWS_CONNECT_REQUEST
159  *
160  * returns non-NULL on success
161  *
162  * You have +1 waiting here: you will get ibw_conn (having the
163  * same <conn_userdata> member) structure in ibw_connstate_fn_t.
164  */
165 int ibw_connect(struct ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_userdata);
166
167 /*
168  * Sends out a disconnect request.
169  * You should process fds after calling this function
170  * and then process it with ibw_process_event normally
171  * until you get conn->state = IBWC_DISCONNECTED
172  *
173  * You mustn't talloc_free <conn> yet right after this,
174  * first wait for IBWC_DISCONNECTED.
175  */
176 int ibw_disconnect(struct ibw_conn *conn);
177
178 /************ Infiniband specific event loop wrapping ******************/
179
180 /*
181  * You have to use this buf to fill in before send.
182  * It's just to avoid memcpy.in ibw_send.
183  * Use the same (buf, key) pair with ibw_send.
184  * Don't use more space than maxsize (see ibw_init).
185  *
186  * Returns 0 on success.
187  */
188 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len);
189
190 /*
191  * Send the message in one
192  * Can be invoked any times (should fit into buffers) and at any time
193  * (in conn->state=IBWC_CONNECTED)
194  * n must be less or equal than max_msg_size (see ibw_init)
195  *
196  * You mustn't use (buf, key) any more for sending.
197  */
198 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len);
199
200 /*
201  * Call this after ibw_alloc_send_buf
202  * when you won't call ibw_send for (buf, key)
203  * You mustn't use (buf, key) any more.
204  */
205 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key);
206
207 /*
208  * Retrieves the last error
209  * result: always non-zero, mustn't be freed (static)
210  */
211 const char *ibw_getLastError(void);