Difference between revisions of "CTDB Project ibwrapper"

Line 20: Line 20:
 
   */
 
   */
 
   
 
   
/*
 
  * Basically, traditional socket is chosen for exchanging
 
  * infiniband/verbs-specific info when connecting a client.
 
  *
 
  * The socket-like functions call the real socket functions, with some
 
  * ib wrapping and error and state checking. Must be used "normally" ...
 
  *
 
  * However, ibw_write and ibw_read use real infiniband/verbs calls only.
 
  */
 
 
   
 
   
 +
/* Server communication state */
 +
typedef enum {
 +
IBWS_INIT = 0, /* ctx start - after ibw_init */
 +
IBWS_ADDR_RESOLVED, /* after bind - should proceed to IBWS_READY processing the next fd_cm event - internally */
 +
IBWS_READY, /* after bind 2. */
 +
IBWS_CONNECT_REQUEST, /* after [IBWS_ROUTE_RESOLVED + listen]
 +
=> [IBWS_READY | STOPPED | ERROR] */
 +
IBWS_STOPPED, /* normal stop */
 +
IBWS_ERROR /* abnormal state */
 +
} ibw_state_ctx;
 +
 +
/* Connection state */
 
  typedef struct _ibw_ctx {
 
  typedef struct _ibw_ctx {
        int fd; /* read fd of verbs events */
+
int fd_events; /* read fd of verbs events */
 
  /* ibw_process_event must be _always_ invoked */
 
  /* ibw_process_event must be _always_ invoked */
 
  /* when this fd is set after a select/poll */
 
  /* when this fd is set after a select/poll */
        void *internal;
+
 +
int fd_cm; /* read fd about a cm state change */
 +
/* call ibw_process_statechange after it's set */
 +
 +
void *ctx_userdata; /* see ibw_init */
 +
 +
ibw_state_ctx state;
 +
void *internal;
 
  } ibw_ctx;
 
  } ibw_ctx;
 +
 +
typedef enum {
 +
IBWC_INIT = 0, /* conn start - internal state */
 +
IBWC_CONNECTED, /* after ibw_accept or ibw_connect */
 +
IBWC_DISCONNECTED, /* after ibw_disconnect */
 +
IBWC_ERROR
 +
} ibw_state_conn;
 
   
 
   
 
  typedef struct _ibw_conn {
 
  typedef struct _ibw_conn {
        ibw_ctx *ctx;
+
ibw_ctx *ctx;
  void *userdata; /* see also ibw_connect and ibw_accept */
+
ibw_state_conn state;
        void *internal;
+
  void *conn_userdata; /* see ibw_connect and ibw_accept */
 +
void *internal;
 
  } ibw_conn;
 
  } ibw_conn;
 
   
 
   
Line 54: Line 72:
 
  const char *value;
 
  const char *value;
 
  } ibw_initattr;
 
  } ibw_initattr;
 +
 +
/*
 +
  * Callback function definition which should inform you about
 +
  * connection state change
 +
  * invoked from within ibw_process_statechange
 +
  * Both <conn> and <ctx> can be NULL if their state didn't change.
 +
  * Return nonzero on error.
 +
  */
 +
typedef int (*ibw_connstate_fn_t)(ibw_ctx *ctx, ibw_conn *conn, void *ctx_userdata);
 
   
 
   
 
  /*
 
  /*
 
   * Callback function definition which should process incoming packets
 
   * Callback function definition which should process incoming packets
   * It's called from within ibw_process_event.
+
   * invoked from within ibw_process_event.
 +
  * Return nonzero on error.
 
   */
 
   */
  typedef int (*ibw_receive_fn_t)(ibw_conn *conn, void *buf, int nsize);
+
  typedef int (*ibw_receive_fn_t)(ibw_conn *conn, void *buf, int n);
 
   
 
   
 
  /*
 
  /*
Line 74: Line 102:
 
   * talloc_free must be called for the result
 
   * talloc_free must be called for the result
 
   */
 
   */
  ibw_ctx *ibw_init(ibw_initattr *attr, int nattr, ibw_receive_fn_t ibw_receive);
+
  ibw_ctx *ibw_init(ibw_initattr *attr, int nattr,
 +
ibw_connstate_fn_t ibw_connstate,
 +
void *ctx_userdata,
 +
ibw_receive_fn_t ibw_receive);
 
   
 
   
 
   
 
   
 
  /*************** connection initiation - like stream sockets *****/
 
  /*************** connection initiation - like stream sockets *****/
/* TODO: enum nodes + verify this connection method */
 
 
   
 
   
 
  /*
 
  /*
   * Call as the normal one (see man page)
+
   * works like socket bind
   * returns a sockfd as the normal one
+
   * needs a normal internet address here
  */
+
   *
int ibw_socket(ibw_ctx *ctx, int domain, int type, int protocol);
+
   * return 0 on success
 
/*
 
   * Needs a normal internet address here
 
   * return is a real socket fd
 
 
   */
 
   */
 
  int ibw_bind(ibw_ctx *ctx, struct sockaddr_in *my_addr);
 
  int ibw_bind(ibw_ctx *ctx, struct sockaddr_in *my_addr);
 
   
 
   
 
  /*
 
  /*
   * sockfd here is a real sockfd
+
   * works like socket listen
  * see also the man page
+
   * !!!: it's NON-blocking; use ctx->fd_cm + ibw_process_statechange +
   * !!!: it's also blocking
+
  * ibw_connstate_fn_t to wait for a conn state change (=IBWS_READY)
 +
  *
 +
  * returns ctx->cm_fd
 
   */
 
   */
 
  int ibw_listen(ibw_ctx *ctx, int sockfd, int backlog);
 
  int ibw_listen(ibw_ctx *ctx, int sockfd, int backlog);
 
   
 
   
 
  /*
 
  /*
   * sockfd here is a real sockfd
+
   * works like socket accept
   * see also the man page
+
   * initializes a connection
   * !!!:
+
   * Normally should be called from ibw_connstate_fn_t callback
   * additionally, the server exchanges ib-specific
+
   * when state=IBWS_CONNECT_REQUEST
  * 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)
 
   * userdata: will be put into ibw_conn (see also ibw_callback_fn_t)
 
   */
 
   */
  ibw_conn *ibw_accept(ibw_ctx *ctx, int sockfd, struct sockaddr_in *cli_addr, void *userdata);
+
  ibw_conn *ibw_accept(ibw_ctx *ctx, void *conn_userdata);
 
   
 
   
 
  /*
 
  /*
 
   * Needs a normal internet address here
 
   * Needs a normal internet address here
 +
  * can be called within IBWS_READY (or IBWS_CONNECT_REQUEST)
 
   *
 
   *
 
   * 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)
 
   * userdata: will be put into ibw_conn (see also ibw_callback_fn_t)
 
   */
 
   */
  ibw_conn *ibw_connect(ibw_ctx *ctx, int sockfd, struct sockaddr_in *serv_addr, void *userdata);
+
  ibw_conn *ibw_connect(ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_userdata);
 
   
 
   
 +
/*
 +
  * Sends a disconnect request
 +
  * You should process ctx->cm_fd ater calling this function
 +
  * and then process it with ibw_process_event normally
 +
  * (until you get conn->state = IBWC_DISCONNECTED)
 +
  *
 +
  * You have to talloc_free <conn> after this (even in the callback).
 +
  */
 +
void ibw_disconnect(ibw_conn *conn);
 
   
 
   
 
  /************ Infiniband specific event loop wrapping ******************/
 
  /************ Infiniband specific event loop wrapping ******************/
 
   
 
   
 
  /*  
 
  /*  
   * !!! Must be called in all cases after selecting/polling for ctx->fd is set.
+
   * !!! Must be called in all cases after selecting/polling for ctx->fd_events is set.
 
   */
 
   */
 
  int ibw_process_event(ibw_ctx *ctx);
 
  int ibw_process_event(ibw_ctx *ctx);
 
   
 
   
 +
/*
 +
  * !!! Must be called in all cases after selecting/polling for ctx->fd_cm is set.
 +
  */
 +
int ibw_process_statechange(ibw_ctx *ctx);
 
   
 
   
 
  /*
 
  /*
Line 137: Line 175:
 
   * 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
 
   */
 
   */
  int ibw_send(ibw_conn *connctx, void *buf, int n);
+
  int ibw_send(ibw_conn *conn, void *buf, int n);

Revision as of 17:38, 27 November 2006

File ibwrapper.h:

/*
 * Infiniband Verbs API socket-like wrapper
 * Copyright (C) Peter Somogyi 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


/* Server communication state */
typedef enum {
	IBWS_INIT = 0, /* ctx start - after ibw_init */
	IBWS_ADDR_RESOLVED, /* after bind - should proceed to IBWS_READY processing the next fd_cm event - internally */
	IBWS_READY, /* after bind 2. */
	IBWS_CONNECT_REQUEST, /* after [IBWS_ROUTE_RESOLVED + listen]
		=> [IBWS_READY | STOPPED | ERROR] */
	IBWS_STOPPED, /* normal stop */
	IBWS_ERROR /* abnormal state */
} ibw_state_ctx;

/* Connection state */
typedef struct _ibw_ctx {
	int fd_events; /* read fd of verbs events */
		/* ibw_process_event must be _always_ invoked */
		/* when this fd is set after a select/poll */

	int fd_cm; /* read fd about a cm state change */
		/* call ibw_process_statechange after it's set */

	void *ctx_userdata; /* see ibw_init */

	ibw_state_ctx state;
	void *internal;
} ibw_ctx;

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

typedef struct _ibw_conn {
	ibw_ctx *ctx;
	ibw_state_conn state;
	void *conn_userdata; /* see ibw_connect and ibw_accept */
	void *internal;
} ibw_conn;

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


typedef struct _ibw_initattr {
	const char *name;
	const char *value;
} ibw_initattr;

/*
 * Callback function definition which should inform you about
 * connection state change
 * invoked from within ibw_process_statechange
 * Both <conn> and <ctx> can be NULL if their state didn't change.
 * Return nonzero on error.
 */
typedef int (*ibw_connstate_fn_t)(ibw_ctx *ctx, ibw_conn *conn, void *ctx_userdata);

/*
 * Callback function definition which should process incoming packets
 * invoked from within ibw_process_event.
 * Return nonzero on error.
 */
typedef int (*ibw_receive_fn_t)(ibw_conn *conn, void *buf, int n);

/*
 * settings: array of (name, value) pairs
 * where name is one of:
 *      dev_name [default is the first one]
 *      rx_depth [default is 500]
 *      mtu     [default is 1024]
 *      ib_port [default is 1]
 *
 * Must be called for each NODE _ONCE_
 *
 * returns non-NULL on success
 * talloc_free must be called for the result
 */
ibw_ctx *ibw_init(ibw_initattr *attr, int nattr,
	ibw_connstate_fn_t ibw_connstate,
	void *ctx_userdata,
	ibw_receive_fn_t ibw_receive);


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

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

/*
 * works like socket listen
 * !!!: it's NON-blocking; use ctx->fd_cm + ibw_process_statechange +
 * ibw_connstate_fn_t to wait for a conn state change (=IBWS_READY)
 *
 * returns ctx->cm_fd
 */
int ibw_listen(ibw_ctx *ctx, int sockfd, int backlog);

/*
 * works like socket accept
 * initializes a connection
 * Normally should be called from ibw_connstate_fn_t callback
 * when state=IBWS_CONNECT_REQUEST
 *
 * returns non-NULL on success
 *
 * userdata: will be put into ibw_conn (see also ibw_callback_fn_t)
 */
ibw_conn *ibw_accept(ibw_ctx *ctx, void *conn_userdata);

/*
 * Needs a normal internet address here
 * can be called within IBWS_READY (or IBWS_CONNECT_REQUEST)
 *
 * returns non-NULL on success
 *
 * userdata: will be put into ibw_conn (see also ibw_callback_fn_t)
 */
ibw_conn *ibw_connect(ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_userdata);

/*
 * Sends a disconnect request
 * You should process ctx->cm_fd ater calling this function
 * and then process it with ibw_process_event normally
 * (until you get conn->state = IBWC_DISCONNECTED)
 *
 * You have to talloc_free <conn> after this (even in the callback).
 */
void ibw_disconnect(ibw_conn *conn);

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

/* 
 * !!! Must be called in all cases after selecting/polling for ctx->fd_events is set.
 */
int ibw_process_event(ibw_ctx *ctx);

/* 
 * !!! Must be called in all cases after selecting/polling for ctx->fd_cm is set.
 */
int ibw_process_statechange(ibw_ctx *ctx);

/*
 * Send the message in one
 * Can be invoked any times (should fit into buffers) and at any time
 */
int ibw_send(ibw_conn *conn, void *buf, int n);