CTDB Project ibwrapper: Difference between revisions

From SambaWiki
No edit summary
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 2: Line 2:


/*
/*
* Unix SMB/CIFS implementation.
* Infiniband Verbs API socket-like wrapper
* Wrap Infiniband calls.
* Copyright (C) Peter Somogyi 2006
*
*
* Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
*
* Major code contributions by Peter Somogyi <psomogyi@gamax.hu>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* This program is free software; you can redistribute it and/or modify
* You should have received a copy of the GNU Lesser General Public
* it under the terms of the GNU General Public License as published by
* License along with this library; if not, write to the Free Software
* the Free Software Foundation; either version 2 of the License, or
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* (at your option) any later version.
*/
/*
* Basically, traditional socket is chosen for exchanging
* infiniband/verbs-specific info when connecting a client.
*
*
* This program is distributed in the hope that it will be useful,
* The socket-like functions call the real socket functions, with some
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* ib wrapping and error and state checking. Must be used "normally" ...
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* However, ibw_write and ibw_read use real infiniband/verbs calls only.
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
*/
/* Server communication state */
typedef struct _ibw_ctx {
enum ibw_state_ctx {
int fd; /* read fd of verbs events */
IBWS_INIT = 0, /* ctx start - after ibw_init */
/* ibw_process_event must be _always_ invoked */
IBWS_READY, /* after ibw_bind & ibw_listen */
/* when this fd is set after a select/poll */
IBWS_CONNECT_REQUEST, /* after [IBWS_READY + incoming request] */
void *internal;
/* => [(ibw_accept)IBWS_READY | (ibw_disconnect)STOPPED | ERROR] */
} ibw_ctx;
IBWS_STOPPED, /* normal stop <= ibw_disconnect+(IBWS_READY | IBWS_CONNECT_REQUEST) */
IBWS_ERROR /* abnormal state; ibw_stop must be called after this */
};
/* Connection state */
typedef struct _ibw_conn {
ibw_ctx *ctx;
struct ibw_ctx {
void *userdata; /* see also ibw_connect and ibw_accept */
void *ctx_userdata; /* see ibw_init */
void *internal;
} ibw_conn;
enum ibw_state_ctx state;
/*
void *internal;
* Retrieves the last error
* result: always non-zero, mustn't be freed (static)
*/
const char *ibw_getLastError();
struct ibw_conn *conn_list; /* 1st elem of double linked list */
};
enum ibw_state_conn {
typedef struct _ibw_initattr {
IBWC_INIT = 0, /* conn start - internal state */
IBWC_CONNECTED, /* after ibw_accept or ibw_connect */
IBWC_DISCONNECTED, /* after ibw_disconnect */
IBWC_ERROR
};
struct ibw_conn {
struct ibw_ctx *ctx;
enum ibw_state_conn state;
void *conn_userdata; /* see ibw_connect and ibw_accept */
void *internal;
struct ibw_conn *prev, *next;
};
/*
* (name, value) pair for array param of ibw_init
*/
struct ibw_initattr {
const char *name;
const char *name;
const char *value;
const char *value;
};
} ibw_initattr;
/*
* Callback function definition which should inform you about
* connection state change
* This callback is invoked whenever server or client connection changes.
* Both <conn> and <ctx> can be NULL if their state didn't change.
* Return nonzero on error.
*/
typedef int (*ibw_connstate_fn_t)(struct ibw_ctx *ctx, struct ibw_conn *conn);
/*
/*
* Callback function definition which should process incoming packets
* Callback function definition which should process incoming packets
* This callback is invoked whenever any message arrives.
* It's called from within ibw_process_event.
* Return nonzero on error.
*
* Important: you mustn't store buf pointer for later use.
* Process its contents before returning.
*/
*/
typedef int (*ibw_receive_fn_t)(ibw_conn *conn, void *buf, int nsize);
typedef int (*ibw_receive_fn_t)(struct ibw_conn *conn, void *buf, int n);
/*
/*
* settings: array of (name, value) pairs
* settings: array of (name, value) pairs
* where name is one of:
* where name is one of:
* dev_name [default is the first one]
* max_send_wr [default is 256]
* rx_depth [default is 500]
* max_recv_wr [default is 1024]
* <...>
* mtu [default is 1024]
* ib_port [default is 1]
*
*
* Must be called for each NODE _ONCE_
* Must be called _ONCE_ for each node.
*
* max_msg_size is the maximum size of a message
* (max_send_wr + max_recv_wr) * max_msg_size bytes allocated per connection
*
*
* returns non-NULL on success
* returns non-NULL on success
*
* talloc_free must be called for the result
* talloc_free must be called for the result in IBWS_STOPPED;
* it will close resources by destructor
* connections(ibw_conn *) must have been closed prior talloc_free
*/
*/
ibw_ctx *ibw_init(ibw_initattr *attr, int nattr, ibw_receive_fn_t ibw_receive);
struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
void *ctx_userdata,
ibw_connstate_fn_t ibw_connstate,
ibw_receive_fn_t ibw_receive,
struct event_context *ectx);
/*
* Must be called in states of (IBWS_ERROR, IBWS_READY, IBWS_CONNECT_REQUEST)
*
* It will send out disconnect requests and free up ibw_conn structures.
* The ctx->state will transit to IBWS_STOPPED after every conn are disconnected.
* During that time, you mustn't send/recv/disconnect any more.
* Only after ctx->state=IBWS_STOPPED you can talloc_free the ctx.
*/
int ibw_stop(struct ibw_ctx *ctx);
/*************** connection initiation - like stream sockets *****/
/*************** connection initiation - like stream sockets *****/
/* TODO: enum nodes + verify this connection method */
/*
/*
* works like socket bind
* Call as the normal one (see man page)
* returns a sockfd as the normal one
* needs a normal internet address here
*
* return 0 on success
*/
*/
int ibw_socket(ibw_ctx *ctx, int domain, int type, int protocol);
int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr);
/*
/*
* works like socket listen
* Needs a normal internet address here
* non-blocking
* return is a real socket fd
* enables accepting incoming connections (after IBWS_READY)
* (it doesn't touch ctx->state by itself)
*
* returns 0 on success
*/
*/
int ibw_bind(ibw_ctx *ctx, struct sockaddr_in *my_addr);
int ibw_listen(struct ibw_ctx *ctx, int backlog);
/*
/*
* works like socket accept
* sockfd here is a real sockfd
* initializes a connection to a client
* see also the man page
* must be called when state=IBWS_CONNECT_REQUEST
* !!!: it's also blocking
*
* returns 0 on success
*
* You have +1 waiting here: you will get ibw_conn (having the
* same <conn_userdata> member) structure in ibw_connstate_fn_t.
*
* Important: you won't get remote IP address (only internal conn info)
*/
*/
int ibw_listen(ibw_ctx *ctx, int sockfd, int backlog);
int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata);
/*
/*
* Needs a normal internet address here
* sockfd here is a real sockfd
* can be called within IBWS_READY|IBWS_CONNECT_REQUEST
* see also the man page
* !!!:
* additionally, the server exchanges ib-specific
* properties (lid, qpn, psn) here with the client
* + initializes a connection
*
*
* returns non-NULL on success
* returns non-NULL on success
* talloc_free must be called for the result (which calls close)
*
*
* userdata: will be put into ibw_conn (see also ibw_callback_fn_t)
* You have +1 waiting here: you will get ibw_conn (having the
* same <conn_userdata> member) structure in ibw_connstate_fn_t.
*/
*/
ibw_conn *ibw_accept(ibw_ctx *ctx, int sockfd, struct sockaddr_in *cli_addr, void *userdata);
int ibw_connect(struct ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_userdata);
/*
/*
* Sends out a disconnect request.
* Needs a normal internet address here
* You should process fds after calling this function
* and then process it with ibw_process_event normally
* until you get conn->state = IBWC_DISCONNECTED
*
*
* You mustn't talloc_free <conn> yet right after this,
* returns non-NULL on success
* first wait for IBWC_DISCONNECTED.
* talloc_free must be called for the result (which calls close)
*
* userdata: will be put into ibw_conn (see also ibw_callback_fn_t)
*/
*/
int ibw_disconnect(struct ibw_conn *conn);
ibw_conn *ibw_connect(ibw_ctx *ctx, int sockfd, struct sockaddr_in *serv_addr, void *userdata);
/************ Infiniband specific event loop wrapping ******************/
/************ Infiniband specific event loop wrapping ******************/
/*
/*
* You have to use this buf to fill in before send.
* !!! Must be called in all cases after selecting/polling for ctx->fd is set.
* It's just to avoid memcpy.in ibw_send.
* Use the same (buf, key) pair with ibw_send.
* Don't use more space than maxsize (see ibw_init).
*
* Returns 0 on success.
*/
*/
int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, int n);
int ibw_process_event(ibw_ctx *ctx);
/*
/*
* Send the message in one
* Send the message in one
* Can be invoked any times (should fit into buffers) and at any time
* Can be invoked any times (should fit into buffers) and at any time
* (in conn->state=IBWC_CONNECTED)
* n must be less or equal than max_msg_size (see ibw_init)
*
* You mustn't use (buf, key) any more for sending.
*/
int ibw_send(struct ibw_conn *conn, void *buf, void *key, int n);
/*
* Retrieves the last error
* result: always non-zero, mustn't be freed (static)
*/
*/
const char *ibw_getLastError(void);
int ibw_send(ibw_conn *connctx, void *buf, int n);

Latest revision as of 19:57, 18 December 2006

File ibwrapper.h:

/*
 * Unix SMB/CIFS implementation.
 * Wrap Infiniband calls.
 *
 * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006
 *
 * Major code contributions by Peter Somogyi <psomogyi@gamax.hu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* Server communication state */
enum ibw_state_ctx {
	IBWS_INIT = 0, /* ctx start - after ibw_init */
	IBWS_READY, /* after ibw_bind & ibw_listen */
	IBWS_CONNECT_REQUEST, /* after [IBWS_READY + incoming request] */
		/* => [(ibw_accept)IBWS_READY | (ibw_disconnect)STOPPED | ERROR] */
	IBWS_STOPPED, /* normal stop <= ibw_disconnect+(IBWS_READY | IBWS_CONNECT_REQUEST) */
	IBWS_ERROR /* abnormal state; ibw_stop must be called after this */
};

/* Connection state */
struct ibw_ctx {
	void *ctx_userdata; /* see ibw_init */

	enum ibw_state_ctx state;
	void *internal;

	struct ibw_conn *conn_list; /* 1st elem of double linked list */
};

enum ibw_state_conn {
	IBWC_INIT = 0, /* conn start - internal state */
	IBWC_CONNECTED, /* after ibw_accept or ibw_connect */
	IBWC_DISCONNECTED, /* after ibw_disconnect */
	IBWC_ERROR
};

struct ibw_conn {
	struct ibw_ctx *ctx;
	enum ibw_state_conn state;

	void *conn_userdata; /* see ibw_connect and ibw_accept */
	void *internal;

	struct ibw_conn *prev, *next;
};

/*
 * (name, value) pair for array param of ibw_init
 */
struct ibw_initattr {
	const char *name;
	const char *value;
};

/*
 * Callback function definition which should inform you about
 * connection state change
 * This callback is invoked whenever server or client connection changes.
 * Both <conn> and <ctx> can be NULL if their state didn't change.
 * Return nonzero on error.
 */
typedef int (*ibw_connstate_fn_t)(struct ibw_ctx *ctx, struct ibw_conn *conn);

/*
 * Callback function definition which should process incoming packets
 * This callback is invoked whenever any message arrives.
 * Return nonzero on error.
 *
 * Important: you mustn't store buf pointer for later use.
 * Process its contents before returning.
 */
typedef int (*ibw_receive_fn_t)(struct ibw_conn *conn, void *buf, int n);

/*
 * settings: array of (name, value) pairs
 * where name is one of:
 *      max_send_wr [default is 256]
 *      max_recv_wr [default is 1024]
 * <...>
 *
 * Must be called _ONCE_ for each node.
 *
 * max_msg_size is the maximum size of a message
 * (max_send_wr + max_recv_wr) * max_msg_size bytes allocated per connection
 *
 * returns non-NULL on success
 *
 * talloc_free must be called for the result in IBWS_STOPPED;
 *    it will close resources by destructor
 *    connections(ibw_conn *) must have been closed prior talloc_free
 */
struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
	void *ctx_userdata,
	ibw_connstate_fn_t ibw_connstate,
	ibw_receive_fn_t ibw_receive,
	struct event_context *ectx);

/*
 * Must be called in states of (IBWS_ERROR, IBWS_READY, IBWS_CONNECT_REQUEST)
 *
 * It will send out disconnect requests and free up ibw_conn structures.
 * The ctx->state will transit to IBWS_STOPPED after every conn are disconnected.
 * During that time, you mustn't send/recv/disconnect any more.
 * Only after ctx->state=IBWS_STOPPED you can talloc_free the ctx.
 */
int ibw_stop(struct ibw_ctx *ctx);

/*************** connection initiation - like stream sockets *****/

/*
 * works like socket bind
 * needs a normal internet address here
 *
 * return 0 on success
 */
int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr);

/*
 * works like socket listen
 * non-blocking
 * enables accepting incoming connections (after IBWS_READY)
 * (it doesn't touch ctx->state by itself)
 *
 * returns 0 on success
 */
int ibw_listen(struct ibw_ctx *ctx, int backlog);

/*
 * works like socket accept
 * initializes a connection to a client
 * must be called when state=IBWS_CONNECT_REQUEST
 *
 * returns 0 on success
 *
 * You have +1 waiting here: you will get ibw_conn (having the
 * same <conn_userdata> member) structure in ibw_connstate_fn_t.
 *
 * Important: you won't get remote IP address (only internal conn info)
 */
int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata);

/*
 * Needs a normal internet address here
 * can be called within IBWS_READY|IBWS_CONNECT_REQUEST
 *
 * returns non-NULL on success
 *
 * You have +1 waiting here: you will get ibw_conn (having the
 * same <conn_userdata> member) structure in ibw_connstate_fn_t.
 */
int ibw_connect(struct ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_userdata);

/*
 * Sends out a disconnect request.
 * You should process fds after calling this function
 * and then process it with ibw_process_event normally
 * until you get conn->state = IBWC_DISCONNECTED
 *
 * You mustn't talloc_free <conn> yet right after this,
 * first wait for IBWC_DISCONNECTED.
 */
int ibw_disconnect(struct ibw_conn *conn);

/************ Infiniband specific event loop wrapping ******************/

/*
 * You have to use this buf to fill in before send.
 * It's just to avoid memcpy.in ibw_send.
 * Use the same (buf, key) pair with ibw_send.
 * Don't use more space than maxsize (see ibw_init).
 *
 * Returns 0 on success.
 */
int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, int n);

/*
 * Send the message in one
 * Can be invoked any times (should fit into buffers) and at any time
 * (in conn->state=IBWC_CONNECTED)
 * n must be less or equal than max_msg_size (see ibw_init)
 *
 * You mustn't use (buf, key) any more for sending.
 */
int ibw_send(struct ibw_conn *conn, void *buf, void *key, int n);

/*
 * Retrieves the last error
 * result: always non-zero, mustn't be freed (static)
 */
const char *ibw_getLastError(void);