fd-io-styleify pass

Change-Id: I72d0dff064162d11321257c858f68da1ba79ea48
Signed-off-by: Dave Barach <dave@barachs.net>
This commit is contained in:
Dave Barach
2016-07-08 09:38:52 -04:00
committed by Damjan Marion
parent 9b8ffd99fb
commit 371e4e19d1
17 changed files with 4012 additions and 3426 deletions

View File

@ -26,15 +26,17 @@
#include <vlibmemory/unix_shared_memory_queue.h>
#include <vlib/unix/unix.h>
typedef enum {
REGISTRATION_TYPE_FREE=0,
typedef enum
{
REGISTRATION_TYPE_FREE = 0,
REGISTRATION_TYPE_SHMEM,
REGISTRATION_TYPE_SOCKET_LISTEN,
REGISTRATION_TYPE_SOCKET_SERVER,
REGISTRATION_TYPE_SOCKET_CLIENT,
} vl_registration_type_t;
typedef struct vl_api_registration_ {
typedef struct vl_api_registration_
{
vl_registration_type_t registration_type;
/* Index in VLIB's brain (not shared memory). */
@ -52,9 +54,9 @@ typedef struct vl_api_registration_ {
/* socket server and client */
u32 unix_file_index;
i8 * unprocessed_input;
i8 *unprocessed_input;
u32 unprocessed_msg_length;
u8 * output_vector;
u8 *output_vector;
/* socket client only */
u32 server_handle;
@ -64,7 +66,8 @@ typedef struct vl_api_registration_ {
/* Trace configuration for a single message */
typedef struct {
typedef struct
{
int size;
int trace_enable;
int replay_enable;
@ -73,7 +76,8 @@ typedef struct {
/*
* API recording
*/
typedef struct {
typedef struct
{
u8 endian;
u8 enabled;
u8 wrapped;
@ -83,13 +87,14 @@ typedef struct {
u8 **traces;
} vl_api_trace_t;
typedef CLIB_PACKED (struct {
u8 endian;
u8 wrapped;
typedef CLIB_PACKED (struct
{
u8 endian; u8 wrapped;
u32 nitems;
}) vl_api_trace_file_header_t;
}) vl_api_trace_file_header_t;
typedef enum {
typedef enum
{
VL_API_TRACE_TX,
VL_API_TRACE_RX,
} vl_api_trace_which_t;
@ -97,18 +102,20 @@ typedef enum {
#define VL_API_LITTLE_ENDIAN 0x00
#define VL_API_BIG_ENDIAN 0x01
typedef struct {
u8 * name;
typedef struct
{
u8 *name;
u16 first_msg_id;
u16 last_msg_id;
} vl_api_msg_range_t;
typedef struct {
void (**msg_handlers)(void *);
int (**pd_msg_handlers)(void *, int);
void (**msg_cleanup_handlers)(void *);
void (**msg_endian_handlers)(void *);
void (**msg_print_handlers)(void *, void *);
typedef struct
{
void (**msg_handlers) (void *);
int (**pd_msg_handlers) (void *, int);
void (**msg_cleanup_handlers) (void *);
void (**msg_endian_handlers) (void *);
void (**msg_print_handlers) (void *, void *);
char **msg_names;
u8 *message_bounce;
u8 *is_mp_safe;
@ -129,7 +136,7 @@ typedef struct {
u16 first_available_msg_id;
/* message range by name hash */
uword * msg_range_by_name;
uword *msg_range_by_name;
/* vector of message ranges */
vl_api_msg_range_t *msg_ranges;
@ -161,7 +168,8 @@ typedef struct {
api_main_t api_main;
typedef struct {
typedef struct
{
int id;
char *name;
void *handler;
@ -176,55 +184,63 @@ typedef struct {
} vl_msg_api_msg_config_t;
/* api_shared.c prototypes */
int vl_msg_api_rx_trace_enabled(api_main_t *am);
int vl_msg_api_tx_trace_enabled(api_main_t *am);
void vl_msg_api_trace(api_main_t *am, vl_api_trace_t *tp, void *msg);
int vl_msg_api_trace_onoff(api_main_t *am, vl_api_trace_which_t which,
int vl_msg_api_rx_trace_enabled (api_main_t * am);
int vl_msg_api_tx_trace_enabled (api_main_t * am);
void vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg);
int vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
int onoff);
int vl_msg_api_trace_free(api_main_t *am, vl_api_trace_which_t which);
int vl_msg_api_trace_save(api_main_t *am,
vl_api_trace_which_t which, FILE *fp);
int vl_msg_api_trace_configure(api_main_t *am, vl_api_trace_which_t which,
int vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which);
int vl_msg_api_trace_save (api_main_t * am,
vl_api_trace_which_t which, FILE * fp);
int vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
u32 nitems);
void vl_msg_api_handler_with_vm_node (api_main_t *am,
void *the_msg, vlib_main_t *vm,
vlib_node_runtime_t *node);
void vl_msg_api_handler_with_vm_node (api_main_t * am,
void *the_msg, vlib_main_t * vm,
vlib_node_runtime_t * node);
void vl_msg_api_handler (void *the_msg);
void vl_msg_api_handler_no_free (void *the_msg);
void vl_msg_api_handler_no_trace_no_free (void *the_msg);
void vl_msg_api_trace_only (void *the_msg);
void vl_msg_api_cleanup_handler (void *the_msg);
void vl_msg_api_replay_handler(void *the_msg);
void vl_msg_api_socket_handler(void *the_msg);
void vl_msg_api_set_handlers(int msg_id, char *msg_name,
void vl_msg_api_replay_handler (void *the_msg);
void vl_msg_api_socket_handler (void *the_msg);
void vl_msg_api_set_handlers (int msg_id, char *msg_name,
void *handler,
void *cleanup,
void *endian,
void *print,
int msg_size, int traced);
void *print, int msg_size, int traced);
void vl_msg_api_config (vl_msg_api_msg_config_t *);
void vl_msg_api_set_cleanup_handler(int msg_id, void *fp);
void vl_msg_api_queue_handler(unix_shared_memory_queue_t *q);
vl_api_trace_t *vl_msg_api_trace_get(api_main_t *am,
void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
void vl_msg_api_queue_handler (unix_shared_memory_queue_t * q);
vl_api_trace_t *vl_msg_api_trace_get (api_main_t * am,
vl_api_trace_which_t which);
void vl_msg_api_free (void *);
void vl_noop_handler (void *mp);
clib_error_t *vl_api_init (vlib_main_t *vm);
void vl_msg_api_increment_missing_client_counter(void);
clib_error_t *vl_api_init (vlib_main_t * vm);
void vl_msg_api_increment_missing_client_counter (void);
void vl_msg_api_post_mortem_dump (void);
void vl_msg_api_register_pd_handler (void *handler, u16 msg_id_host_byte_order);
void vl_msg_api_register_pd_handler (void *handler,
u16 msg_id_host_byte_order);
int vl_msg_api_pd_handler (void *mp, int rv);
void vl_msg_api_set_first_available_msg_id (u16 first_avail);
u16 vl_msg_api_get_msg_ids (char * name, int n);
u16 vl_msg_api_get_msg_ids (char *name, int n);
/* node_serialize.c prototypes */
u8 * vlib_node_serialize (vlib_node_main_t *nm, u8 * vector,
u8 *vlib_node_serialize (vlib_node_main_t * nm, u8 * vector,
u32 max_threads, int include_nexts,
int include_stats);
vlib_node_t ** vlib_node_unserialize (u8 * vector);
vlib_node_t **vlib_node_unserialize (u8 * vector);
#define VLIB_API_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,api_init)
#endif /* included_api_h */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -38,7 +38,8 @@
*
* No locks, no hits, no errors...
*/
typedef struct ring_alloc_ {
typedef struct ring_alloc_
{
unix_shared_memory_queue_t *rp;
u16 size;
u16 nitems;
@ -61,7 +62,8 @@ _(1024+8, 1024) \
_(2048+8, 128) \
_(4096+8, 8)
typedef struct vl_shmem_hdr_ {
typedef struct vl_shmem_hdr_
{
int version;
/* getpid () for the VLIB client process */
@ -87,7 +89,8 @@ typedef struct vl_shmem_hdr_ {
} vl_shmem_hdr_t;
/* Note that the size of the structure is 16 bytes, with 4 bytes of padding after data[0]. */
typedef struct msgbuf_ {
typedef struct msgbuf_
{
unix_shared_memory_queue_t *q;
u32 data_len;
u8 data[0];
@ -98,33 +101,37 @@ typedef struct msgbuf_ {
#define VL_API_EPOCH_MASK 0xFF
#define VL_API_EPOCH_SHIFT 8
static inline u32 vl_msg_api_handle_get_epoch (u32 index)
static inline u32
vl_msg_api_handle_get_epoch (u32 index)
{
return (index & VL_API_EPOCH_MASK);
}
static inline u32 vl_msg_api_handle_get_index (u32 index)
static inline u32
vl_msg_api_handle_get_index (u32 index)
{
return (index >> VL_API_EPOCH_SHIFT);
}
static inline u32 vl_msg_api_handle_from_index_and_epoch (u32 index, u32 epoch)
static inline u32
vl_msg_api_handle_from_index_and_epoch (u32 index, u32 epoch)
{
u32 handle;
ASSERT (index < 0x00FFFFFF);
handle = (index<<VL_API_EPOCH_SHIFT) | (epoch & VL_API_EPOCH_MASK);
handle = (index << VL_API_EPOCH_SHIFT) | (epoch & VL_API_EPOCH_MASK);
return handle;
}
void *vl_msg_api_alloc(int nbytes);
void *vl_msg_api_alloc (int nbytes);
void *vl_msg_api_alloc_as_if_client (int nbytes);
void vl_msg_api_free(void *a);
void vl_msg_api_free (void *a);
int vl_map_shmem (char *region_name, int is_vlib);
void vl_register_mapped_shmem_region(svm_region_t *rp);
void vl_register_mapped_shmem_region (svm_region_t * rp);
void vl_unmap_shmem (void);
void vl_msg_api_send_shmem (unix_shared_memory_queue_t *q, u8 *elem);
void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t *q, u8 *elem);
void vl_msg_api_send (vl_api_registration_t *rp, u8 *elem);
void vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem);
void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem);
void vl_msg_api_send (vl_api_registration_t * rp, u8 * elem);
int vl_client_connect (char *name, int ctx_quota, int input_queue_size);
void vl_client_disconnect (void);
unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index);
@ -135,14 +142,23 @@ void vl_set_memory_region_name (char *name);
void vl_set_memory_root_path (char *root_path);
void vl_set_memory_uid (int uid);
void vl_set_memory_gid (int gid);
void vl_enable_disable_memory_api (vlib_main_t *vm, int yesno);
void vl_enable_disable_memory_api (vlib_main_t * vm, int yesno);
void vl_client_disconnect_from_vlib (void);
int vl_client_connect_to_vlib(char *svm_name, char *client_name,
int vl_client_connect_to_vlib (char *svm_name, char *client_name,
int rx_queue_size);
int vl_client_connect_to_vlib_no_rx_pthread (char *svm_name, char *client_name,
int vl_client_connect_to_vlib_no_rx_pthread (char *svm_name,
char *client_name,
int rx_queue_size);
u16 vl_client_get_first_plugin_msg_id (char * plugin_name);
u16 vl_client_get_first_plugin_msg_id (char *plugin_name);
void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
#endif /* included_vlibmemory_api_h */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -61,7 +61,8 @@
#include <vlibmemory/vl_memory_api_h.h>
#undef vl_printfun
typedef struct {
typedef struct
{
u8 rx_thread_jmpbuf_valid;
u8 connected_to_vlib;
jmp_buf rx_thread_jmpbuf;
@ -73,7 +74,8 @@ typedef struct {
memory_client_main_t memory_client_main;
static void *rx_thread_fn(void *arg)
static void *
rx_thread_fn (void *arg)
{
unix_shared_memory_queue_t *q;
memory_client_main_t *mm = &memory_client_main;
@ -82,37 +84,42 @@ static void *rx_thread_fn(void *arg)
q = am->vl_input_queue;
/* So we can make the rx thread terminate cleanly */
if (setjmp(mm->rx_thread_jmpbuf) == 0) {
if (setjmp (mm->rx_thread_jmpbuf) == 0)
{
mm->rx_thread_jmpbuf_valid = 1;
while (1) {
while (1)
{
vl_msg_api_queue_handler (q);
}
}
pthread_exit(0);
pthread_exit (0);
}
static void vl_api_rx_thread_exit_t_handler (
vl_api_rx_thread_exit_t *mp)
static void
vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
{
memory_client_main_t *mm = &memory_client_main;
vl_msg_api_free (mp);
longjmp (mm->rx_thread_jmpbuf, 1);
}
static void noop_handler (void *notused)
static void
noop_handler (void *notused)
{
}
#define foreach_api_msg \
_(RX_THREAD_EXIT, rx_thread_exit)
static int connect_to_vlib_internal (char *svm_name, char *client_name,
static int
connect_to_vlib_internal (char *svm_name, char *client_name,
int rx_queue_size, int want_pthread)
{
int rv=0;
int rv = 0;
memory_client_main_t *mm = &memory_client_main;
if ((rv = vl_client_api_map(svm_name))) {
if ((rv = vl_client_api_map (svm_name)))
{
clib_warning ("vl_client_api map rv %d", rv);
return rv;
}
@ -127,80 +134,88 @@ static int connect_to_vlib_internal (char *svm_name, char *client_name,
foreach_api_msg;
#undef _
if (vl_client_connect(client_name, 0 /* punt quota */,
rx_queue_size /* input queue */) < 0) {
vl_client_api_unmap();
if (vl_client_connect (client_name, 0 /* punt quota */ ,
rx_queue_size /* input queue */ ) < 0)
{
vl_client_api_unmap ();
return -1;
}
/* Start the rx queue thread */
if (want_pthread) {
rv = pthread_create(&mm->rx_thread_handle,
NULL /*attr*/, rx_thread_fn, 0);
if (want_pthread)
{
rv = pthread_create (&mm->rx_thread_handle,
NULL /*attr */ , rx_thread_fn, 0);
if (rv)
clib_warning("pthread_create returned %d", rv);
clib_warning ("pthread_create returned %d", rv);
}
mm->connected_to_vlib = 1;
return 0;
}
int vl_client_connect_to_vlib(char *svm_name, char *client_name,
int
vl_client_connect_to_vlib (char *svm_name, char *client_name,
int rx_queue_size)
{
return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
1 /* want pthread */);
1 /* want pthread */ );
}
int vl_client_connect_to_vlib_no_rx_pthread (char *svm_name, char *client_name,
int
vl_client_connect_to_vlib_no_rx_pthread (char *svm_name, char *client_name,
int rx_queue_size)
{
return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
0 /* want pthread */);
0 /* want pthread */ );
}
void vl_client_disconnect_from_vlib (void)
void
vl_client_disconnect_from_vlib (void)
{
memory_client_main_t *mm = &memory_client_main;
api_main_t *am = &api_main;
uword junk;
if (mm->rx_thread_jmpbuf_valid) {
if (mm->rx_thread_jmpbuf_valid)
{
vl_api_rx_thread_exit_t *ep;
ep = vl_msg_api_alloc (sizeof (*ep));
ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
vl_msg_api_send_shmem (am->vl_input_queue, (u8 *)&ep);
ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
pthread_join (mm->rx_thread_handle, (void **) &junk);
}
if (mm->connected_to_vlib) {
vl_client_disconnect();
vl_client_api_unmap();
if (mm->connected_to_vlib)
{
vl_client_disconnect ();
vl_client_api_unmap ();
}
memset (mm, 0, sizeof (*mm));
}
static void vl_api_get_first_msg_id_reply_t_handler
(vl_api_get_first_msg_id_reply_t * mp)
(vl_api_get_first_msg_id_reply_t * mp)
{
memory_client_main_t *mm = &memory_client_main;
i32 retval = ntohl(mp->retval);
i32 retval = ntohl (mp->retval);
mm->first_msg_id_reply = (retval >= 0) ? ntohs(mp->first_msg_id) : ~0;
mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
mm->first_msg_id_reply_ready = 1;
}
u16 vl_client_get_first_plugin_msg_id (char * plugin_name)
u16
vl_client_get_first_plugin_msg_id (char *plugin_name)
{
vl_api_get_first_msg_id_t * mp;
api_main_t * am = &api_main;
memory_client_main_t * mm = &memory_client_main;
vl_api_get_first_msg_id_t *mp;
api_main_t *am = &api_main;
memory_client_main_t *mm = &memory_client_main;
f64 timeout;
void * old_handler;
void *old_handler;
clib_time_t clib_time;
u16 rv = ~0;
if (strlen(plugin_name) + 1 > sizeof (mp->name))
if (strlen (plugin_name) + 1 > sizeof (mp->name))
return (rv);
memset (&clib_time, 0, sizeof (clib_time));
@ -214,20 +229,23 @@ u16 vl_client_get_first_plugin_msg_id (char * plugin_name)
/* Ask the data-plane for the message-ID base of the indicated plugin */
mm->first_msg_id_reply_ready = 0;
mp = vl_msg_api_alloc (sizeof(*mp));
mp = vl_msg_api_alloc (sizeof (*mp));
memset (mp, 0, sizeof (*mp));
mp->_vl_msg_id = ntohs(VL_API_GET_FIRST_MSG_ID);
mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
mp->client_index = am->my_client_index;
strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *)&mp);
vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
/* Synchronously wait for the answer */
do {
do
{
timeout = clib_time_now (&clib_time) + 1.0;
while (clib_time_now (&clib_time) < timeout) {
if (mm->first_msg_id_reply_ready == 1) {
while (clib_time_now (&clib_time) < timeout)
{
if (mm->first_msg_id_reply_ready == 1)
{
rv = mm->first_msg_id_reply;
goto result;
}
@ -236,18 +254,30 @@ u16 vl_client_get_first_plugin_msg_id (char * plugin_name)
am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
return rv;
} while(0);
}
while (0);
result:
/* Restore the old handler */
am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
if (rv == (u16) ~0)
if (rv == (u16) ~ 0)
clib_warning ("plugin '%s' not registered", plugin_name);
return rv;
}
void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n)
{ clib_warning ("STUB called..."); }
void
vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n)
{
clib_warning ("STUB called...");
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -23,7 +23,8 @@
#include <pthread.h>
#include <vppinfra/mem.h>
typedef struct _unix_shared_memory_queue {
typedef struct _unix_shared_memory_queue
{
pthread_mutex_t mutex; /* 8 bytes */
pthread_cond_t condvar; /* 8 bytes */
int head;
@ -36,25 +37,33 @@ typedef struct _unix_shared_memory_queue {
char data[0];
} unix_shared_memory_queue_t;
unix_shared_memory_queue_t *
unix_shared_memory_queue_init(int nels,
unix_shared_memory_queue_t *unix_shared_memory_queue_init (int nels,
int elsize,
int consumer_pid,
int signal_when_queue_non_empty);
void unix_shared_memory_queue_free(unix_shared_memory_queue_t *q);
int unix_shared_memory_queue_add (unix_shared_memory_queue_t *q,
u8 *elem, int nowait);
int unix_shared_memory_queue_sub(unix_shared_memory_queue_t *q,
u8 *elem, int nowait);
void unix_shared_memory_queue_lock (unix_shared_memory_queue_t *q);
void unix_shared_memory_queue_unlock (unix_shared_memory_queue_t *q);
int unix_shared_memory_queue_is_full (unix_shared_memory_queue_t *q);
int unix_shared_memory_queue_add_nolock (unix_shared_memory_queue_t *q,
u8 *elem);
int
signal_when_queue_non_empty);
void unix_shared_memory_queue_free (unix_shared_memory_queue_t * q);
int unix_shared_memory_queue_add (unix_shared_memory_queue_t * q,
u8 * elem, int nowait);
int unix_shared_memory_queue_sub (unix_shared_memory_queue_t * q,
u8 * elem, int nowait);
void unix_shared_memory_queue_lock (unix_shared_memory_queue_t * q);
void unix_shared_memory_queue_unlock (unix_shared_memory_queue_t * q);
int unix_shared_memory_queue_is_full (unix_shared_memory_queue_t * q);
int unix_shared_memory_queue_add_nolock (unix_shared_memory_queue_t * q,
u8 * elem);
int unix_shared_memory_queue_sub_raw (unix_shared_memory_queue_t *q,
u8 *elem);
int unix_shared_memory_queue_add_raw (unix_shared_memory_queue_t *q,
u8 *elem);
int unix_shared_memory_queue_sub_raw (unix_shared_memory_queue_t * q,
u8 * elem);
int unix_shared_memory_queue_add_raw (unix_shared_memory_queue_t * q,
u8 * elem);
#endif /* included_unix_shared_memory_queue_h */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -23,3 +23,11 @@
*/
#include <vlibmemory/memclnt.api.h>
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -24,10 +24,19 @@
#include <vppinfra/byte_order.h>
#define vl_msg_id(n,h) n,
typedef enum {
VL_ILLEGAL_MESSAGE_ID=0,
typedef enum
{
VL_ILLEGAL_MESSAGE_ID = 0,
#include <vlibmemory/vl_memory_api_h.h>
} vl_msg_id_t;
#undef vl_msg_id
#endif /* __VL_MSG_ENUM_H__ */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -22,7 +22,8 @@
#include <vlibapi/api.h>
typedef struct {
typedef struct
{
/* Server port number */
int portno;
@ -47,33 +48,40 @@ typedef struct {
socket_main_t socket_main;
void socksvr_add_pending_output (struct unix_file * uf,
struct vl_api_registration_ * cf,
void socksvr_add_pending_output (struct unix_file *uf,
struct vl_api_registration_ *cf,
u8 * buffer, uword buffer_bytes);
#define SOCKSVR_DEFAULT_PORT 32741 /* whatever */
void vl_free_socket_registration_index (u32 pool_index);
void vl_socket_process_msg (struct unix_file *uf,
struct vl_api_registration_ *rp, i8 *input_v);
clib_error_t * vl_socket_read_ready (struct unix_file * uf);
void vl_socket_add_pending_output (struct unix_file * uf,
struct vl_api_registration_ * rp,
u8 * buffer,
uword buffer_bytes);
clib_error_t * vl_socket_write_ready (struct unix_file * uf);
void vl_socket_api_send (vl_api_registration_t *rp, u8 *elem) ;
void vl_socket_api_send_with_data (vl_api_registration_t *rp,
u8 *elem, u8 *data_vector);
void vl_socket_api_send_with_length (vl_api_registration_t *rp,
u8 *elem, u32 msg_length);
void vl_socket_api_send_with_length_no_free (vl_api_registration_t *rp,
u8 *elem, u32 msg_length);
struct vl_api_registration_ *rp, i8 * input_v);
clib_error_t *vl_socket_read_ready (struct unix_file *uf);
void vl_socket_add_pending_output (struct unix_file *uf,
struct vl_api_registration_ *rp,
u8 * buffer, uword buffer_bytes);
clib_error_t *vl_socket_write_ready (struct unix_file *uf);
void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem);
void vl_socket_api_send_with_data (vl_api_registration_t * rp,
u8 * elem, u8 * data_vector);
void vl_socket_api_send_with_length (vl_api_registration_t * rp,
u8 * elem, u32 msg_length);
void vl_socket_api_send_with_length_no_free (vl_api_registration_t * rp,
u8 * elem, u32 msg_length);
u32 sockclnt_open_index (char *client_name, char *hostname, int port);
void sockclnt_close_index (u32 index);
void vl_client_msg_api_send (vl_api_registration_t *cm, u8 *elem);
void vl_client_msg_api_send (vl_api_registration_t * cm, u8 * elem);
vl_api_registration_t *sockclnt_get_registration (u32 index);
void socksvr_set_port (u16 port);
void socksvr_set_bind_address (u32 bind_address);
#endif /* included_vlibsocket_api_h */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -36,39 +36,38 @@ typedef unsigned long uword;
#define VL_API_PACKED(x) x __attribute__ ((packed))
typedef VL_API_PACKED(struct _vl_api_sockclnt_create {
u16 _vl_msg_id;
u8 name[64];
typedef VL_API_PACKED (struct _vl_api_sockclnt_create
{
u16 _vl_msg_id; u8 name[64];
u32 context;
}) vl_api_sockclnt_create_t;
}) vl_api_sockclnt_create_t;
typedef VL_API_PACKED(struct _vl_api_sockclnt_create_reply {
typedef VL_API_PACKED (struct _vl_api_sockclnt_create_reply
{
u16 _vl_msg_id;
i32 response;
u64 handle;
u32 index;
u32 context;
}) vl_api_sockclnt_create_reply_t;
i32 response; u64 handle; u32 index; u32 context;
}) vl_api_sockclnt_create_reply_t;
typedef VL_API_PACKED(struct _vl_api_sockclnt_delete {
u16 _vl_msg_id;
u32 index;
typedef VL_API_PACKED (struct _vl_api_sockclnt_delete
{
u16 _vl_msg_id; u32 index;
u64 handle;
}) vl_api_sockclnt_delete_t;
}) vl_api_sockclnt_delete_t;
typedef VL_API_PACKED(struct _vl_api_sockclnt_delete_reply {
u16 _vl_msg_id;
i32 response;
u64 handle;
}) vl_api_sockclnt_delete_reply_t;
typedef VL_API_PACKED (struct _vl_api_sockclnt_delete_reply
{
u16 _vl_msg_id; i32 response; u64 handle;
}) vl_api_sockclnt_delete_reply_t;
void error(char *msg)
void
error (char *msg)
{
perror(msg);
exit(0);
perror (msg);
exit (0);
}
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
@ -81,64 +80,76 @@ int main(int argc, char *argv[])
char *rdptr;
int total_bytes;
for (i = 0; i < 1; i++) {
for (i = 0; i < 1; i++)
{
portno = SOCKCLNT_SERVER_PORT;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("localhost");
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
error ("ERROR opening socket");
server = gethostbyname ("localhost");
if (server == NULL)
{
fprintf (stderr, "ERROR, no such host\n");
exit (0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
bzero ((char *) &serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(const void *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
bcopy ((char *) server->h_addr,
(char *) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons (portno);
if (connect (sockfd, (const void *) &serv_addr, sizeof (serv_addr)) < 0)
error ("ERROR connecting");
memset(buffer, 0, sizeof(buffer));
memset (buffer, 0, sizeof (buffer));
mp = (vl_api_sockclnt_create_t *)buffer;
mp->_vl_msg_id = ntohs(8); /* VL_API_SOCKCLNT_CREATE */
strncpy ((char *) mp->name, "socket-test", sizeof(mp->name)-1);
mp->name[sizeof(mp->name)-1]=0;
mp = (vl_api_sockclnt_create_t *) buffer;
mp->_vl_msg_id = ntohs (8); /* VL_API_SOCKCLNT_CREATE */
strncpy ((char *) mp->name, "socket-test", sizeof (mp->name) - 1);
mp->name[sizeof (mp->name) - 1] = 0;
mp->context = 0xfeedface;
/* length of the message, including the length itself */
nbytes = sizeof (*mp) + sizeof (nbytes);
nbytes = ntohl(nbytes);
n = write(sockfd, &nbytes, sizeof(nbytes));
nbytes = ntohl (nbytes);
n = write (sockfd, &nbytes, sizeof (nbytes));
if (n < 0)
error("ERROR writing len to socket");
n = write(sockfd, mp, sizeof (*mp));
error ("ERROR writing len to socket");
n = write (sockfd, mp, sizeof (*mp));
if (n < 0)
error("ERROR writing msg to socket");
error ("ERROR writing msg to socket");
memset(buffer, 0, sizeof (buffer));
memset (buffer, 0, sizeof (buffer));
total_bytes = 0;
rdptr = buffer;
do {
n = read(sockfd,rdptr,sizeof(buffer) - (rdptr - buffer));
do
{
n = read (sockfd, rdptr, sizeof (buffer) - (rdptr - buffer));
if (n < 0)
error("ERROR reading from socket");
error ("ERROR reading from socket");
printf ("read %d bytes\n", n);
total_bytes += n;
rdptr += n;
} while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t) + 4);
}
while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t) + 4);
rp = (vl_api_sockclnt_create_reply_t *)(buffer + 4);
rp = (vl_api_sockclnt_create_reply_t *) (buffer + 4);
/* VL_API_SOCKCLNT_CREATE_REPLY */
if (ntohs(rp->_vl_msg_id) != 9) {
printf ("WARNING: msg id %d\n", ntohs(rp->_vl_msg_id));
if (ntohs (rp->_vl_msg_id) != 9)
{
printf ("WARNING: msg id %d\n", ntohs (rp->_vl_msg_id));
}
printf ("response %d, handle 0x%llx, index %d, context 0x%x\n",
ntohl(rp->response), rp->handle, rp->index, rp->context);
close(sockfd);
ntohl (rp->response), rp->handle, rp->index, rp->context);
close (sockfd);
}
return 0;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -48,8 +48,8 @@
#include <vlibsocket/vl_socket_api_h.h>
#undef vl_endianfun
static void vl_api_sockclnt_create_reply_t_handler (
vl_api_sockclnt_create_reply_t *mp)
static void
vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
{
vl_api_registration_t *rp = socket_main.current_rp;
@ -57,8 +57,8 @@ static void vl_api_sockclnt_create_reply_t_handler (
rp->server_index = mp->index;
}
static void vl_api_sockclnt_delete_reply_t_handler (
vl_api_sockclnt_delete_reply_t *mp)
static void
vl_api_sockclnt_delete_reply_t_handler (vl_api_sockclnt_delete_reply_t * mp)
{
unix_main_t *um = &unix_main;
unix_file_t *uf = socket_main.current_uf;
@ -68,11 +68,12 @@ static void vl_api_sockclnt_delete_reply_t_handler (
vl_free_socket_registration_index (rp->vl_api_registration_pool_index);
}
u32 sockclnt_open_index (char *client_name, char *hostname, int port)
u32
sockclnt_open_index (char *client_name, char *hostname, int port)
{
vl_api_registration_t * rp;
unix_main_t * um = &unix_main;
unix_file_t template = {0};
vl_api_registration_t *rp;
unix_main_t *um = &unix_main;
unix_file_t template = { 0 };
int sockfd;
int one = 1;
int rv;
@ -81,41 +82,43 @@ u32 sockclnt_open_index (char *client_name, char *hostname, int port)
vl_api_sockclnt_create_t *mp;
char my_hostname[64];
server = gethostbyname(hostname);
if (server == NULL) {
clib_warning("Couldn't translate server name %s", hostname);
server = gethostbyname (hostname);
if (server == NULL)
{
clib_warning ("Couldn't translate server name %s", hostname);
return ~0;
}
/* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
if (sockfd < 0)
{
clib_unix_warning ("socket");
return ~0;
}
bzero((char *) &serv_addr, sizeof(serv_addr));
bzero ((char *) &serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(port);
bcopy ((char *) server->h_addr,
(char *) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons (port);
if (connect(sockfd,(const void *)&serv_addr,sizeof(serv_addr)) < 0) {
clib_unix_warning("Connect failure to (%s, %d)",
hostname, port);
if (connect (sockfd, (const void *) &serv_addr, sizeof (serv_addr)) < 0)
{
clib_unix_warning ("Connect failure to (%s, %d)", hostname, port);
return ~0;
}
rv = ioctl (sockfd, FIONBIO, &one);
if (rv < 0) {
if (rv < 0)
{
clib_unix_warning ("FIONBIO");
return ~0;
}
pool_get (socket_main.registration_pool, rp);
memset(rp, 0, sizeof(*rp));
memset (rp, 0, sizeof (*rp));
rp->registration_type = REGISTRATION_TYPE_SOCKET_CLIENT;
rp->vl_api_registration_pool_index = rp - socket_main.registration_pool;
@ -125,42 +128,46 @@ u32 sockclnt_open_index (char *client_name, char *hostname, int port)
template.private_data = rp - socket_main.registration_pool;
rp->unix_file_index = unix_file_add (um, &template);
rp->name = format(0, "%s:%d", hostname, port);
rp->name = format (0, "%s:%d", hostname, port);
mp = vl_msg_api_alloc (sizeof (*mp));
mp->_vl_msg_id = ntohs(VL_API_SOCKCLNT_CREATE);
mp->_vl_msg_id = ntohs (VL_API_SOCKCLNT_CREATE);
mp->context = rp - socket_main.registration_pool;
if (gethostname(my_hostname, sizeof (my_hostname)) < 0) {
clib_unix_warning("gethostname");
strncpy (my_hostname, "unknown!", sizeof(my_hostname)-1);
if (gethostname (my_hostname, sizeof (my_hostname)) < 0)
{
clib_unix_warning ("gethostname");
strncpy (my_hostname, "unknown!", sizeof (my_hostname) - 1);
}
strncpy ((char *)mp->name, my_hostname, sizeof (mp->name)-1);
strncpy ((char *) mp->name, my_hostname, sizeof (mp->name) - 1);
vl_msg_api_send (rp, (u8 *)mp);
vl_msg_api_send (rp, (u8 *) mp);
return rp - socket_main.registration_pool;
}
void sockclnt_close_index (u32 index)
void
sockclnt_close_index (u32 index)
{
vl_api_sockclnt_delete_t *mp;
vl_api_registration_t *rp;
/* Don't crash / assert if fed garbage */
if (pool_is_free_index (socket_main.registration_pool, index)) {
if (pool_is_free_index (socket_main.registration_pool, index))
{
clib_warning ("registration_pool index %d already free", index);
return;
}
rp = pool_elt_at_index(socket_main.registration_pool, index);
rp = pool_elt_at_index (socket_main.registration_pool, index);
mp = vl_msg_api_alloc (sizeof (*mp));
mp->_vl_msg_id = ntohs(VL_API_SOCKCLNT_DELETE);
mp->_vl_msg_id = ntohs (VL_API_SOCKCLNT_DELETE);
mp->handle = rp->server_handle;
mp->index = rp->server_index;
vl_msg_api_send (rp, (u8 *)mp);
vl_msg_api_send (rp, (u8 *) mp);
}
vl_api_registration_t *sockclnt_get_registration (u32 index)
vl_api_registration_t *
sockclnt_get_registration (u32 index)
{
return pool_elt_at_index (socket_main.registration_pool, index);
}
@ -175,7 +182,7 @@ _(SOCKCLNT_DELETE_REPLY, sockclnt_delete_reply)
static clib_error_t *
sockclnt_vlib_api_init (vlib_main_t *vm)
sockclnt_vlib_api_init (vlib_main_t * vm)
{
#define _(N,n) \
vl_msg_api_set_handlers(VL_API_##N, #n, \
@ -189,4 +196,12 @@ sockclnt_vlib_api_init (vlib_main_t *vm)
return 0;
}
VLIB_API_INIT_FUNCTION(sockclnt_vlib_api_init);
VLIB_API_INIT_FUNCTION (sockclnt_vlib_api_init);
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

File diff suppressed because it is too large Load Diff

View File

@ -23,3 +23,11 @@
*/
#include <vlibmemory/vl_memory_api_h.h>
#include <vlibsocket/sockclnt.api.h>
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -24,10 +24,19 @@
#include <vppinfra/byte_order.h>
#define vl_msg_id(n,h) n,
typedef enum {
VL_ILLEGAL_MESSAGE_ID=0,
typedef enum
{
VL_ILLEGAL_MESSAGE_ID = 0,
#include <vlibsocket/vl_socket_api_h.h>
} vl_msg_id_t;
#undef vl_msg_id
#endif /* __VL_MSG_ENUM_H__ */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/