diff options
author | Dave Barach <dave@barachs.net> | 2016-07-08 09:38:52 -0400 |
---|---|---|
committer | Damjan Marion <dmarion.lists@gmail.com> | 2016-07-08 15:11:40 +0000 |
commit | 371e4e19d1db9ed0113d0db7b2f3651e4fce855f (patch) | |
tree | 4a38ebcf6322a1fa71a16dfe19eaab01ed83184e /vlib-api/vlibsocket | |
parent | 9b8ffd99fb35d37250cfb7094a0a31515f1bb577 (diff) |
fd-io-styleify pass
Change-Id: I72d0dff064162d11321257c858f68da1ba79ea48
Signed-off-by: Dave Barach <dave@barachs.net>
Diffstat (limited to 'vlib-api/vlibsocket')
-rw-r--r-- | vlib-api/vlibsocket/api.h | 90 | ||||
-rw-r--r-- | vlib-api/vlibsocket/sock_test.c | 215 | ||||
-rw-r--r-- | vlib-api/vlibsocket/sockclnt_vlib.c | 215 | ||||
-rw-r--r-- | vlib-api/vlibsocket/socksvr_vlib.c | 900 | ||||
-rw-r--r-- | vlib-api/vlibsocket/vl_socket_api_h.h | 12 | ||||
-rw-r--r-- | vlib-api/vlibsocket/vl_socket_msg_enum.h | 15 |
6 files changed, 777 insertions, 670 deletions
diff --git a/vlib-api/vlibsocket/api.h b/vlib-api/vlibsocket/api.h index 3477f9560c9..3a86bd0c533 100644 --- a/vlib-api/vlibsocket/api.h +++ b/vlib-api/vlibsocket/api.h @@ -1,7 +1,7 @@ /* *------------------------------------------------------------------ * api.h - * + * * Copyright (c) 2009 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,58 +22,66 @@ #include <vlibapi/api.h> -typedef struct { - /* Server port number */ - int portno; - - /* By default, localhost... */ - u32 bind_address; +typedef struct +{ + /* Server port number */ + int portno; + + /* By default, localhost... */ + u32 bind_address; - /* - * (listen, server, client) registrations. Shared memory - * registrations are in shared memory - */ - vl_api_registration_t *registration_pool; - /* - * Chain-drag variables, so message API handlers - * (generally) don't know whether they're talking to a socket - * or to a shared-memory connection. - */ - vl_api_registration_t *current_rp; - unix_file_t *current_uf; - /* One input buffer, shared across all sockets */ - i8 *input_buffer; + /* + * (listen, server, client) registrations. Shared memory + * registrations are in shared memory + */ + vl_api_registration_t *registration_pool; + /* + * Chain-drag variables, so message API handlers + * (generally) don't know whether they're talking to a socket + * or to a shared-memory connection. + */ + vl_api_registration_t *current_rp; + unix_file_t *current_uf; + /* One input buffer, shared across all sockets */ + i8 *input_buffer; } socket_main_t; socket_main_t socket_main; -void socksvr_add_pending_output (struct unix_file * uf, - struct vl_api_registration_ * cf, - u8 * buffer, uword buffer_bytes); +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 */ +#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); +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); 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: + */ diff --git a/vlib-api/vlibsocket/sock_test.c b/vlib-api/vlibsocket/sock_test.c index cfae32f68fa..3bded08fd99 100644 --- a/vlib-api/vlibsocket/sock_test.c +++ b/vlib-api/vlibsocket/sock_test.c @@ -20,9 +20,9 @@ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> -#include <netdb.h> +#include <netdb.h> -#define SOCKCLNT_SERVER_PORT 32741 /* whatever */ +#define SOCKCLNT_SERVER_PORT 32741 /* whatever */ typedef signed char i8; typedef signed short i16; @@ -36,109 +36,120 @@ 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]; - u32 context; -}) vl_api_sockclnt_create_t; - -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; - -typedef VL_API_PACKED(struct _vl_api_sockclnt_delete { - u16 _vl_msg_id; - u32 index; - u64 handle; -}) 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; - -void error(char *msg) +typedef VL_API_PACKED (struct _vl_api_sockclnt_create + { + u16 _vl_msg_id; u8 name[64]; + u32 context; + }) vl_api_sockclnt_create_t; + +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; + +typedef VL_API_PACKED (struct _vl_api_sockclnt_delete + { + u16 _vl_msg_id; u32 index; + u64 handle; + }) 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; + +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; - struct hostent *server; - char buffer[256]; - int i; - u32 nbytes; - vl_api_sockclnt_create_t *mp; - vl_api_sockclnt_create_reply_t *rp; - char *rdptr; - int total_bytes; - - for (i = 0; i < 1; i++) { - portno = SOCKCLNT_SERVER_PORT; - 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); - } - 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"); - - 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->context = 0xfeedface; - /* length of the message, including the length itself */ - nbytes = sizeof (*mp) + 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)); - if (n < 0) - error("ERROR writing msg to socket"); - - memset(buffer, 0, sizeof (buffer)); - - total_bytes = 0; - rdptr = buffer; - do { - n = read(sockfd,rdptr,sizeof(buffer) - (rdptr - buffer)); - if (n < 0) - 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); - - 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)); - } - - printf ("response %d, handle 0x%llx, index %d, context 0x%x\n", - ntohl(rp->response), rp->handle, rp->index, rp->context); - close(sockfd); + int sockfd, portno, n; + struct sockaddr_in serv_addr; + struct hostent *server; + char buffer[256]; + int i; + u32 nbytes; + vl_api_sockclnt_create_t *mp; + vl_api_sockclnt_create_reply_t *rp; + char *rdptr; + int total_bytes; + + for (i = 0; i < 1; i++) + { + portno = SOCKCLNT_SERVER_PORT; + 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); + } + 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"); + + 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->context = 0xfeedface; + /* length of the message, including the length itself */ + nbytes = sizeof (*mp) + 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)); + if (n < 0) + error ("ERROR writing msg to socket"); + + memset (buffer, 0, sizeof (buffer)); + + total_bytes = 0; + rdptr = buffer; + do + { + n = read (sockfd, rdptr, sizeof (buffer) - (rdptr - buffer)); + if (n < 0) + 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); + + 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)); + } + + printf ("response %d, handle 0x%llx, index %d, context 0x%x\n", + ntohl (rp->response), rp->handle, rp->index, rp->context); + close (sockfd); } - return 0; + return 0; } + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib-api/vlibsocket/sockclnt_vlib.c b/vlib-api/vlibsocket/sockclnt_vlib.c index 8ea4f3083a3..d173c860ea2 100644 --- a/vlib-api/vlibsocket/sockclnt_vlib.c +++ b/vlib-api/vlibsocket/sockclnt_vlib.c @@ -1,4 +1,4 @@ -/* +/* *------------------------------------------------------------------ * sockclnt_vlib.c * @@ -17,7 +17,7 @@ *------------------------------------------------------------------ */ -#include <sys/types.h> +#include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <netinet/in.h> @@ -33,7 +33,7 @@ #include <vlibsocket/vl_socket_msg_enum.h> -#define vl_typedefs /* define message structures */ +#define vl_typedefs /* define message structures */ #include <vlibsocket/vl_socket_api_h.h> #undef vl_typedefs @@ -48,134 +48,141 @@ #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; + vl_api_registration_t *rp = socket_main.current_rp; - rp->server_handle = mp->handle; - rp->server_index = mp->index; -} + rp->server_handle = mp->handle; + 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; - vl_api_registration_t *rp = socket_main.current_rp; + unix_main_t *um = &unix_main; + unix_file_t *uf = socket_main.current_uf; + vl_api_registration_t *rp = socket_main.current_rp; - unix_file_del (um, uf); - vl_free_socket_registration_index (rp->vl_api_registration_pool_index); -} + unix_file_del (um, uf); + 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}; - int sockfd; - int one = 1; - int rv; - struct sockaddr_in serv_addr; - struct hostent *server; - 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); - return ~0; + vl_api_registration_t *rp; + unix_main_t *um = &unix_main; + unix_file_t template = { 0 }; + int sockfd; + int one = 1; + int rv; + struct sockaddr_in serv_addr; + struct hostent *server; + 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); + return ~0; } - /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */ - sockfd = socket(AF_INET, SOCK_STREAM, 0); + /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */ + sockfd = socket (AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) { - clib_unix_warning ("socket"); - return ~0; + if (sockfd < 0) + { + clib_unix_warning ("socket"); + return ~0; } - 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); - - if (connect(sockfd,(const void *)&serv_addr,sizeof(serv_addr)) < 0) { - clib_unix_warning("Connect failure to (%s, %d)", - hostname, port); - return ~0; + 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); + + 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) { - clib_unix_warning ("FIONBIO"); - return ~0; + rv = ioctl (sockfd, FIONBIO, &one); + if (rv < 0) + { + clib_unix_warning ("FIONBIO"); + return ~0; } - pool_get (socket_main.registration_pool, rp); - memset(rp, 0, sizeof(*rp)); - rp->registration_type = REGISTRATION_TYPE_SOCKET_CLIENT; - rp->vl_api_registration_pool_index = rp - socket_main.registration_pool; - - template.read_function = vl_socket_read_ready; - template.write_function = vl_socket_write_ready; - template.file_descriptor = sockfd; - 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); - - mp = vl_msg_api_alloc (sizeof (*mp)); - 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); + pool_get (socket_main.registration_pool, rp); + memset (rp, 0, sizeof (*rp)); + rp->registration_type = REGISTRATION_TYPE_SOCKET_CLIENT; + rp->vl_api_registration_pool_index = rp - socket_main.registration_pool; + + template.read_function = vl_socket_read_ready; + template.write_function = vl_socket_write_ready; + template.file_descriptor = sockfd; + 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); + + mp = vl_msg_api_alloc (sizeof (*mp)); + 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); } - 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); - return rp - socket_main.registration_pool; + 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)) { - clib_warning ("registration_pool index %d already free", index); - return; + 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)) + { + 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->handle = rp->server_handle; - mp->index = rp->server_index; - vl_msg_api_send (rp, (u8 *)mp); + mp = vl_msg_api_alloc (sizeof (*mp)); + 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_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); + return pool_elt_at_index (socket_main.registration_pool, index); } /* * Both rx and tx msgs MUST be initialized, or we'll have - * precisely no idea how many bytes to write into the API trace... + * precisely no idea how many bytes to write into the API trace... */ #define foreach_sockclnt_api_msg \ _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \ -_(SOCKCLNT_DELETE_REPLY, sockclnt_delete_reply) +_(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, \ @@ -183,10 +190,18 @@ sockclnt_vlib_api_init (vlib_main_t *vm) vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ - sizeof(vl_api_##n##_t), 1); - foreach_sockclnt_api_msg; + sizeof(vl_api_##n##_t), 1); + foreach_sockclnt_api_msg; #undef _ - return 0; -} + 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: + */ diff --git a/vlib-api/vlibsocket/socksvr_vlib.c b/vlib-api/vlibsocket/socksvr_vlib.c index d65646acf39..e70124d3a57 100644 --- a/vlib-api/vlibsocket/socksvr_vlib.c +++ b/vlib-api/vlibsocket/socksvr_vlib.c @@ -1,4 +1,4 @@ -/* +/* *------------------------------------------------------------------ * socksvr_vlib.c * @@ -17,7 +17,7 @@ *------------------------------------------------------------------ */ -#include <sys/types.h> +#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/ioctl.h> @@ -29,42 +29,44 @@ #include <vlibsocket/api.h> #include <vlibmemory/api.h> -#include <vlibsocket/vl_socket_msg_enum.h> /* enumerate all vlib messages */ +#include <vlibsocket/vl_socket_msg_enum.h> /* enumerate all vlib messages */ -#define vl_typedefs /* define message structures */ -#include <vlibsocket/vl_socket_api_h.h> +#define vl_typedefs /* define message structures */ +#include <vlibsocket/vl_socket_api_h.h> #undef vl_typedefs /* instantiate all the print functions we know about */ #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) #define vl_printfun -#include <vlibsocket/vl_socket_api_h.h> +#include <vlibsocket/vl_socket_api_h.h> #undef vl_printfun /* instantiate all the endian swap functions we know about */ #define vl_endianfun -#include <vlibsocket/vl_socket_api_h.h> +#include <vlibsocket/vl_socket_api_h.h> #undef vl_endianfun socket_main_t socket_main; -void dump_socket_clients (vlib_main_t *vm, api_main_t *am) +void +dump_socket_clients (vlib_main_t * vm, api_main_t * am) { - vl_api_registration_t *reg; - socket_main_t *sm = &socket_main; - unix_main_t *um = &unix_main; - unix_file_t *f; - - /* - * Must have at least one active client, not counting the - * REGISTRATION_TYPE_SOCKET_LISTEN bind/accept socket - */ - if (pool_elts (sm->registration_pool) < 2) - return; - - vlib_cli_output (vm, "TCP socket clients"); - vlib_cli_output (vm, "%16s %8s", "Name", "Fildesc"); - pool_foreach (reg, sm->registration_pool, + vl_api_registration_t *reg; + socket_main_t *sm = &socket_main; + unix_main_t *um = &unix_main; + unix_file_t *f; + + /* + * Must have at least one active client, not counting the + * REGISTRATION_TYPE_SOCKET_LISTEN bind/accept socket + */ + if (pool_elts (sm->registration_pool) < 2) + return; + + vlib_cli_output (vm, "TCP socket clients"); + vlib_cli_output (vm, "%16s %8s", "Name", "Fildesc"); + /* *INDENT-OFF* */ + pool_foreach (reg, sm->registration_pool, ({ if (reg->registration_type == REGISTRATION_TYPE_SOCKET_SERVER) { f = pool_elt_at_index (um->file_pool, reg->unix_file_index); @@ -72,450 +74,483 @@ void dump_socket_clients (vlib_main_t *vm, api_main_t *am) reg->name, f->file_descriptor); } })); +/* *INDENT-ON* */ } -void -vl_socket_api_send (vl_api_registration_t *rp, u8 *elem) +void +vl_socket_api_send (vl_api_registration_t * rp, u8 * elem) { - u32 nbytes = 4; /* for the length... */ - u16 msg_id = ntohs(*(u16 *)elem); - u32 msg_length; - u32 tmp; - api_main_t *am = &api_main; - - ASSERT(rp->registration_type > REGISTRATION_TYPE_SHMEM); - - if (msg_id >= vec_len(am->api_trace_cfg)) { - clib_warning("id out of range: %d", msg_id); - vl_msg_api_free((void *)elem); - return; + u32 nbytes = 4; /* for the length... */ + u16 msg_id = ntohs (*(u16 *) elem); + u32 msg_length; + u32 tmp; + api_main_t *am = &api_main; + + ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM); + + if (msg_id >= vec_len (am->api_trace_cfg)) + { + clib_warning ("id out of range: %d", msg_id); + vl_msg_api_free ((void *) elem); + return; } - msg_length = am->api_trace_cfg[msg_id].size; - nbytes += msg_length; - tmp = clib_host_to_net_u32 (nbytes); - - vl_socket_add_pending_output (rp->unix_file_index - + unix_main.file_pool, - rp->vl_api_registration_pool_index - + socket_main.registration_pool, - (u8 *) &tmp, - sizeof(tmp)); - vl_socket_add_pending_output (rp->unix_file_index - + unix_main.file_pool, - rp->vl_api_registration_pool_index - + socket_main.registration_pool, - elem, msg_length); - vl_msg_api_free((void *)elem); + msg_length = am->api_trace_cfg[msg_id].size; + nbytes += msg_length; + tmp = clib_host_to_net_u32 (nbytes); + + vl_socket_add_pending_output (rp->unix_file_index + + unix_main.file_pool, + rp->vl_api_registration_pool_index + + socket_main.registration_pool, + (u8 *) & tmp, sizeof (tmp)); + vl_socket_add_pending_output (rp->unix_file_index + + unix_main.file_pool, + rp->vl_api_registration_pool_index + + socket_main.registration_pool, + elem, msg_length); + vl_msg_api_free ((void *) elem); } -void vl_socket_api_send_with_data (vl_api_registration_t *rp, - u8 *elem, u8 *data_vector) +void +vl_socket_api_send_with_data (vl_api_registration_t * rp, + u8 * elem, u8 * data_vector) { - u32 nbytes = 4; /* for the length... */ - u16 msg_id = ntohs(*(u16 *)elem); - u32 msg_length; - u32 tmp; - api_main_t *am = &api_main; - - ASSERT(rp->registration_type > REGISTRATION_TYPE_SHMEM); - - if (msg_id >= vec_len(am->api_trace_cfg)) { - clib_warning("id out of range: %d", msg_id); - vec_free(data_vector); - vl_msg_api_free((void *)elem); - return; + u32 nbytes = 4; /* for the length... */ + u16 msg_id = ntohs (*(u16 *) elem); + u32 msg_length; + u32 tmp; + api_main_t *am = &api_main; + + ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM); + + if (msg_id >= vec_len (am->api_trace_cfg)) + { + clib_warning ("id out of range: %d", msg_id); + vec_free (data_vector); + vl_msg_api_free ((void *) elem); + return; } - msg_length = am->api_trace_cfg[msg_id].size; - nbytes += msg_length; - nbytes += vec_len(data_vector); - - /* Length in network byte order */ - tmp = clib_host_to_net_u32(nbytes); - - vl_socket_add_pending_output (rp->unix_file_index - + unix_main.file_pool, - rp->vl_api_registration_pool_index - + socket_main.registration_pool, - (u8 *) &tmp, - sizeof(tmp)); - vl_socket_add_pending_output (rp->unix_file_index - + unix_main.file_pool, - rp->vl_api_registration_pool_index - + socket_main.registration_pool, - elem, msg_length); - vl_socket_add_pending_output (rp->unix_file_index - + unix_main.file_pool, - rp->vl_api_registration_pool_index - + socket_main.registration_pool, - data_vector, vec_len(data_vector)); - vl_msg_api_free((void *)elem); + msg_length = am->api_trace_cfg[msg_id].size; + nbytes += msg_length; + nbytes += vec_len (data_vector); + + /* Length in network byte order */ + tmp = clib_host_to_net_u32 (nbytes); + + vl_socket_add_pending_output (rp->unix_file_index + + unix_main.file_pool, + rp->vl_api_registration_pool_index + + socket_main.registration_pool, + (u8 *) & tmp, sizeof (tmp)); + vl_socket_add_pending_output (rp->unix_file_index + + unix_main.file_pool, + rp->vl_api_registration_pool_index + + socket_main.registration_pool, + elem, msg_length); + vl_socket_add_pending_output (rp->unix_file_index + + unix_main.file_pool, + rp->vl_api_registration_pool_index + + socket_main.registration_pool, + data_vector, vec_len (data_vector)); + vl_msg_api_free ((void *) elem); } -static inline void -vl_socket_api_send_with_length_internal (vl_api_registration_t *rp, - u8 *elem, u32 msg_length, int free) +static inline void +vl_socket_api_send_with_length_internal (vl_api_registration_t * rp, + u8 * elem, u32 msg_length, int free) { - u32 nbytes = 4; /* for the length... */ - u16 msg_id = ntohs(*(u16 *)elem); - u32 tmp; - api_main_t *am = &api_main; - - ASSERT(rp->registration_type > REGISTRATION_TYPE_SHMEM); - - if (msg_id >= vec_len(am->api_trace_cfg)) { - clib_warning("id out of range: %d", msg_id); - if (free) - vl_msg_api_free((void *)elem); - return; + u32 nbytes = 4; /* for the length... */ + u16 msg_id = ntohs (*(u16 *) elem); + u32 tmp; + api_main_t *am = &api_main; + + ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM); + + if (msg_id >= vec_len (am->api_trace_cfg)) + { + clib_warning ("id out of range: %d", msg_id); + if (free) + vl_msg_api_free ((void *) elem); + return; } - nbytes += msg_length; - - /* Length in network byte order */ - tmp = clib_host_to_net_u32(nbytes); - - vl_socket_add_pending_output (rp->unix_file_index - + unix_main.file_pool, - rp->vl_api_registration_pool_index - + socket_main.registration_pool, - (u8 *) &tmp, - sizeof(tmp)); - vl_socket_add_pending_output (rp->unix_file_index - + unix_main.file_pool, - rp->vl_api_registration_pool_index - + socket_main.registration_pool, - elem, msg_length); - if (free) - vl_msg_api_free((void *)elem); + nbytes += msg_length; + + /* Length in network byte order */ + tmp = clib_host_to_net_u32 (nbytes); + + vl_socket_add_pending_output (rp->unix_file_index + + unix_main.file_pool, + rp->vl_api_registration_pool_index + + socket_main.registration_pool, + (u8 *) & tmp, sizeof (tmp)); + vl_socket_add_pending_output (rp->unix_file_index + + unix_main.file_pool, + rp->vl_api_registration_pool_index + + socket_main.registration_pool, + elem, msg_length); + if (free) + vl_msg_api_free ((void *) elem); } -void vl_socket_api_send_with_length (vl_api_registration_t *rp, - u8 *elem, u32 msg_length) +void +vl_socket_api_send_with_length (vl_api_registration_t * rp, + u8 * elem, u32 msg_length) { - vl_socket_api_send_with_length_internal(rp,elem,msg_length, 1 /* free */); + vl_socket_api_send_with_length_internal (rp, elem, msg_length, + 1 /* free */ ); } -void vl_socket_api_send_with_length_no_free (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) { - vl_socket_api_send_with_length_internal(rp,elem,msg_length, 0 /* free */); + vl_socket_api_send_with_length_internal (rp, elem, msg_length, + 0 /* free */ ); } -void vl_free_socket_registration_index (u32 pool_index) +void +vl_free_socket_registration_index (u32 pool_index) { - vl_api_registration_t *rp; - if (pool_is_free_index (socket_main.registration_pool, pool_index)) { - clib_warning ("main pool index %d already free", pool_index); - return; + vl_api_registration_t *rp; + if (pool_is_free_index (socket_main.registration_pool, pool_index)) + { + clib_warning ("main pool index %d already free", pool_index); + return; } - rp = pool_elt_at_index (socket_main.registration_pool, pool_index); - - ASSERT(rp->registration_type != REGISTRATION_TYPE_FREE); - vec_free (rp->name); - vec_free (rp->unprocessed_input); - vec_free (rp->output_vector); - rp->registration_type = REGISTRATION_TYPE_FREE; - pool_put (socket_main.registration_pool, rp); + rp = pool_elt_at_index (socket_main.registration_pool, pool_index); + + ASSERT (rp->registration_type != REGISTRATION_TYPE_FREE); + vec_free (rp->name); + vec_free (rp->unprocessed_input); + vec_free (rp->output_vector); + rp->registration_type = REGISTRATION_TYPE_FREE; + pool_put (socket_main.registration_pool, rp); } static inline void -socket_process_msg (unix_file_t *uf, vl_api_registration_t *rp, i8 *input_v) +socket_process_msg (unix_file_t * uf, vl_api_registration_t * rp, + i8 * input_v) { - u8 *the_msg = (u8 *)(input_v+sizeof(u32)); - socket_main.current_uf = uf; - socket_main.current_rp = rp; - vl_msg_api_socket_handler (the_msg); - socket_main.current_uf = 0; - socket_main.current_rp = 0; + u8 *the_msg = (u8 *) (input_v + sizeof (u32)); + socket_main.current_uf = uf; + socket_main.current_rp = rp; + vl_msg_api_socket_handler (the_msg); + socket_main.current_uf = 0; + socket_main.current_rp = 0; } -clib_error_t * vl_socket_read_ready (unix_file_t * uf) +clib_error_t * +vl_socket_read_ready (unix_file_t * uf) { - unix_main_t * um = &unix_main; - vl_api_registration_t * rp; - int n; - i8 *msg_buffer=0; - u32 msg_len; - u32 save_input_buffer_length = vec_len(socket_main.input_buffer); - - rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); - - n = read (uf->file_descriptor, socket_main.input_buffer, - vec_len(socket_main.input_buffer)); - - if (n <= 0 && errno != EAGAIN) { - unix_file_del (um, uf); - - if (!pool_is_free (socket_main.registration_pool, rp)) { - u32 index = rp - socket_main.registration_pool; - vl_free_socket_registration_index (index); - } else { - clib_warning("client index %d already free?", - rp->vl_api_registration_pool_index); - } - return 0; - } - - _vec_len(socket_main.input_buffer) = n; - - /* - * Look for bugs here. This code is tricky because - * data read from a stream socket does honor message - * boundaries. In the case of a long message (>4K bytes) - * we have to do (at least) 2 reads, etc. - */ - do { - if (vec_len(rp->unprocessed_input)) { - vec_append(rp->unprocessed_input, socket_main.input_buffer); - msg_buffer = rp->unprocessed_input; - msg_len = rp->unprocessed_msg_length; - } else { - msg_buffer = socket_main.input_buffer; - msg_len = 0; - } + unix_main_t *um = &unix_main; + vl_api_registration_t *rp; + int n; + i8 *msg_buffer = 0; + u32 msg_len; + u32 save_input_buffer_length = vec_len (socket_main.input_buffer); - if (msg_len == 0) { - /* Length may be split across two reads */ - if (vec_len(msg_buffer) < sizeof (u32)) - goto save_and_split; + rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); - /* total length, including msg_len itself, in network byte order */ - msg_len = clib_net_to_host_u32 (*((u32 *)msg_buffer)); - } + n = read (uf->file_descriptor, socket_main.input_buffer, + vec_len (socket_main.input_buffer)); - /* Happens if the client sent msg_len == 0 */ - if (msg_len == 0) { - clib_warning ("msg_len == 0"); - goto turf_it; - } + if (n <= 0 && errno != EAGAIN) + { + unix_file_del (um, uf); + + if (!pool_is_free (socket_main.registration_pool, rp)) + { + u32 index = rp - socket_main.registration_pool; + vl_free_socket_registration_index (index); + } + else + { + clib_warning ("client index %d already free?", + rp->vl_api_registration_pool_index); + } + return 0; + } - /* We don't have the entire message yet. */ - if (msg_len > vec_len(msg_buffer)) { - save_and_split: - /* - * if we were using the shared input buffer, - * save the fragment. - */ - if (msg_buffer == socket_main.input_buffer) { - ASSERT(vec_len(rp->unprocessed_input) == 0); - vec_validate (rp->unprocessed_input, vec_len (msg_buffer)-1); - clib_memcpy (rp->unprocessed_input, msg_buffer, vec_len(msg_buffer)); - _vec_len (rp->unprocessed_input) = vec_len (msg_buffer); - } - _vec_len(socket_main.input_buffer) = save_input_buffer_length; - rp->unprocessed_msg_length = msg_len; - return 0; - } + _vec_len (socket_main.input_buffer) = n; - socket_process_msg (uf, rp, msg_buffer); - if (n > msg_len) - vec_delete(msg_buffer, msg_len, 0); - else - _vec_len(msg_buffer) = 0; - n -= msg_len; - msg_len = 0; - rp->unprocessed_msg_length = 0; - } while (n > 0); + /* + * Look for bugs here. This code is tricky because + * data read from a stream socket does honor message + * boundaries. In the case of a long message (>4K bytes) + * we have to do (at least) 2 reads, etc. + */ + do + { + if (vec_len (rp->unprocessed_input)) + { + vec_append (rp->unprocessed_input, socket_main.input_buffer); + msg_buffer = rp->unprocessed_input; + msg_len = rp->unprocessed_msg_length; + } + else + { + msg_buffer = socket_main.input_buffer; + msg_len = 0; + } + + if (msg_len == 0) + { + /* Length may be split across two reads */ + if (vec_len (msg_buffer) < sizeof (u32)) + goto save_and_split; + + /* total length, including msg_len itself, in network byte order */ + msg_len = clib_net_to_host_u32 (*((u32 *) msg_buffer)); + } + + /* Happens if the client sent msg_len == 0 */ + if (msg_len == 0) + { + clib_warning ("msg_len == 0"); + goto turf_it; + } + + /* We don't have the entire message yet. */ + if (msg_len > vec_len (msg_buffer)) + { + save_and_split: + /* + * if we were using the shared input buffer, + * save the fragment. + */ + if (msg_buffer == socket_main.input_buffer) + { + ASSERT (vec_len (rp->unprocessed_input) == 0); + vec_validate (rp->unprocessed_input, vec_len (msg_buffer) - 1); + clib_memcpy (rp->unprocessed_input, msg_buffer, + vec_len (msg_buffer)); + _vec_len (rp->unprocessed_input) = vec_len (msg_buffer); + } + _vec_len (socket_main.input_buffer) = save_input_buffer_length; + rp->unprocessed_msg_length = msg_len; + return 0; + } + + socket_process_msg (uf, rp, msg_buffer); + if (n > msg_len) + vec_delete (msg_buffer, msg_len, 0); + else + _vec_len (msg_buffer) = 0; + n -= msg_len; + msg_len = 0; + rp->unprocessed_msg_length = 0; + } + while (n > 0); turf_it: - _vec_len(socket_main.input_buffer) = save_input_buffer_length; + _vec_len (socket_main.input_buffer) = save_input_buffer_length; - return 0; + return 0; } void vl_socket_add_pending_output (unix_file_t * uf, - vl_api_registration_t * rp, - u8 * buffer, - uword buffer_bytes) + vl_api_registration_t * rp, + u8 * buffer, uword buffer_bytes) { - unix_main_t * um = &unix_main; - - vec_add (rp->output_vector, buffer, buffer_bytes); - if (vec_len (rp->output_vector) > 0) { - int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); - uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE; - if (! skip_update) - um->file_update (uf, UNIX_FILE_UPDATE_MODIFY); + unix_main_t *um = &unix_main; + + vec_add (rp->output_vector, buffer, buffer_bytes); + if (vec_len (rp->output_vector) > 0) + { + int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); + uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE; + if (!skip_update) + um->file_update (uf, UNIX_FILE_UPDATE_MODIFY); } } static void socket_del_pending_output (unix_file_t * uf, - vl_api_registration_t * rp, - uword n_bytes) + vl_api_registration_t * rp, uword n_bytes) { - unix_main_t * um = &unix_main; + unix_main_t *um = &unix_main; - vec_delete (rp->output_vector, n_bytes, 0); - if (vec_len (rp->output_vector) <= 0) + vec_delete (rp->output_vector, n_bytes, 0); + if (vec_len (rp->output_vector) <= 0) { - int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); - uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; - if (! skip_update) - um->file_update (uf, UNIX_FILE_UPDATE_MODIFY); + int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); + uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; + if (!skip_update) + um->file_update (uf, UNIX_FILE_UPDATE_MODIFY); } } -clib_error_t * +clib_error_t * vl_socket_write_ready (unix_file_t * uf) { - unix_main_t * um = &unix_main; - vl_api_registration_t * rp; - int n; + unix_main_t *um = &unix_main; + vl_api_registration_t *rp; + int n; - rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); - - /* Flush output vector. */ - n = write (uf->file_descriptor, - rp->output_vector, vec_len (rp->output_vector)); + rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); - if (n < 0) { + /* Flush output vector. */ + n = write (uf->file_descriptor, + rp->output_vector, vec_len (rp->output_vector)); + + if (n < 0) + { #if DEBUG > 2 - clib_warning ("write error, close the file...\n"); + clib_warning ("write error, close the file...\n"); #endif - unix_file_del (um, uf); + unix_file_del (um, uf); - vl_free_socket_registration_index (rp - socket_main.registration_pool); - return 0; + vl_free_socket_registration_index (rp - socket_main.registration_pool); + return 0; } - else if (n > 0) - socket_del_pending_output (uf, rp, n); + else if (n > 0) + socket_del_pending_output (uf, rp, n); - return 0; + return 0; } -clib_error_t * vl_socket_error_ready (unix_file_t * uf) +clib_error_t * +vl_socket_error_ready (unix_file_t * uf) { - vl_api_registration_t * rp; - unix_main_t * um = &unix_main; + vl_api_registration_t *rp; + unix_main_t *um = &unix_main; - rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); - unix_file_del (um, uf); - vl_free_socket_registration_index (rp - socket_main.registration_pool); + rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data); + unix_file_del (um, uf); + vl_free_socket_registration_index (rp - socket_main.registration_pool); - return 0; + return 0; } -void socksvr_file_add (unix_main_t *um, int fd) +void +socksvr_file_add (unix_main_t * um, int fd) { - vl_api_registration_t *rp; - unix_file_t template = {0}; - - pool_get (socket_main.registration_pool, rp); - memset(rp, 0, sizeof(*rp)); - - template.read_function = vl_socket_read_ready; - template.write_function = vl_socket_write_ready; - template.error_function = vl_socket_error_ready; - template.file_descriptor = fd; - template.private_data = rp - socket_main.registration_pool; - - rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER; - rp->vl_api_registration_pool_index = rp - socket_main.registration_pool; - rp->unix_file_index = unix_file_add (um, &template); + vl_api_registration_t *rp; + unix_file_t template = { 0 }; + + pool_get (socket_main.registration_pool, rp); + memset (rp, 0, sizeof (*rp)); + + template.read_function = vl_socket_read_ready; + template.write_function = vl_socket_write_ready; + template.error_function = vl_socket_error_ready; + template.file_descriptor = fd; + template.private_data = rp - socket_main.registration_pool; + + rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER; + rp->vl_api_registration_pool_index = rp - socket_main.registration_pool; + rp->unix_file_index = unix_file_add (um, &template); } -static clib_error_t * socksvr_accept_ready (unix_file_t * uf) +static clib_error_t * +socksvr_accept_ready (unix_file_t * uf) { - unix_main_t * um = &unix_main; - struct sockaddr_in client_addr; - int client_fd; - int client_len; - - client_len = sizeof(client_addr); - - /* - * Supposedly acquires the non-blocking attrib from the - * server socket. - */ - client_fd = accept (uf->file_descriptor, - (struct sockaddr *)&client_addr, - (socklen_t *)&client_len); - - if (client_fd < 0) - return clib_error_return_unix (0, "socksvr_accept_ready: accept"); - - socksvr_file_add (um, client_fd); - return 0; + unix_main_t *um = &unix_main; + struct sockaddr_in client_addr; + int client_fd; + int client_len; + + client_len = sizeof (client_addr); + + /* + * Supposedly acquires the non-blocking attrib from the + * server socket. + */ + client_fd = accept (uf->file_descriptor, + (struct sockaddr *) &client_addr, + (socklen_t *) & client_len); + + if (client_fd < 0) + return clib_error_return_unix (0, "socksvr_accept_ready: accept"); + + socksvr_file_add (um, client_fd); + return 0; } -static clib_error_t * socksvr_bogus_write (unix_file_t * uf) +static clib_error_t * +socksvr_bogus_write (unix_file_t * uf) { - clib_warning ("why am I here?"); - return 0; + clib_warning ("why am I here?"); + return 0; } /* * vl_api_sockclnt_create_t_handler */ -void vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t *mp) +void +vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp) { - vl_api_registration_t *regp; - vl_api_sockclnt_create_reply_t *rp; - int rv = 1; + vl_api_registration_t *regp; + vl_api_sockclnt_create_reply_t *rp; + int rv = 1; - regp = socket_main.current_rp; + regp = socket_main.current_rp; - ASSERT(regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER); + ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER); - regp->name = format(0, "%s%c", mp->name, 0); + regp->name = format (0, "%s%c", mp->name, 0); - rp = vl_msg_api_alloc(sizeof(*rp)); - rp->_vl_msg_id = htons(VL_API_SOCKCLNT_CREATE_REPLY); - rp->handle = (uword)regp; - rp->index = (uword)regp->vl_api_registration_pool_index; - rp->context = mp->context; - rp->response = htonl(rv); + rp = vl_msg_api_alloc (sizeof (*rp)); + rp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE_REPLY); + rp->handle = (uword) regp; + rp->index = (uword) regp->vl_api_registration_pool_index; + rp->context = mp->context; + rp->response = htonl (rv); - vl_msg_api_send (regp, (u8 *)rp); + vl_msg_api_send (regp, (u8 *) rp); } /* * vl_api_sockclnt_delete_t_handler */ -void vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t *mp) +void +vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp) { - vl_api_registration_t *regp; - vl_api_sockclnt_delete_reply_t *rp; + vl_api_registration_t *regp; + vl_api_sockclnt_delete_reply_t *rp; + + if (!pool_is_free_index (socket_main.registration_pool, mp->index)) + { + regp = pool_elt_at_index (socket_main.registration_pool, mp->index); - if (!pool_is_free_index(socket_main.registration_pool, mp->index)) { - regp = pool_elt_at_index(socket_main.registration_pool, mp->index); + rp = vl_msg_api_alloc (sizeof (*rp)); + rp->_vl_msg_id = htons (VL_API_SOCKCLNT_DELETE_REPLY); + rp->handle = mp->handle; + rp->response = htonl (1); - rp = vl_msg_api_alloc(sizeof(*rp)); - rp->_vl_msg_id = htons(VL_API_SOCKCLNT_DELETE_REPLY); - rp->handle = mp->handle; - rp->response = htonl(1); - - vl_msg_api_send (regp, (u8 *)rp); + vl_msg_api_send (regp, (u8 *) rp); - unix_file_del (&unix_main, - unix_main.file_pool + regp->unix_file_index); + unix_file_del (&unix_main, unix_main.file_pool + regp->unix_file_index); - vl_free_socket_registration_index (mp->index); - } else { - clib_warning("unknown client ID %d", mp->index); + vl_free_socket_registration_index (mp->index); + } + else + { + clib_warning ("unknown client ID %d", mp->index); } } #define foreach_vlib_api_msg \ _(SOCKCLNT_CREATE, sockclnt_create) \ -_(SOCKCLNT_DELETE, sockclnt_delete) +_(SOCKCLNT_DELETE, sockclnt_delete) -static clib_error_t *socksvr_api_init (vlib_main_t * vm) +static clib_error_t * +socksvr_api_init (vlib_main_t * vm) { - unix_main_t * um = &unix_main; - unix_file_t template = {0}; - int sockfd; - int one = 1; - int rv; - struct sockaddr_in serv_addr; - vl_api_registration_t *rp; - u16 portno; - u32 bind_address; + unix_main_t *um = &unix_main; + unix_file_t template = { 0 }; + int sockfd; + int one = 1; + int rv; + struct sockaddr_in serv_addr; + vl_api_registration_t *rp; + u16 portno; + u32 bind_address; #define _(N,n) \ vl_msg_api_set_handlers(VL_API_##N, #n, \ @@ -523,85 +558,93 @@ static clib_error_t *socksvr_api_init (vlib_main_t * vm) vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ - sizeof(vl_api_##n##_t), 1); - foreach_vlib_api_msg; + sizeof(vl_api_##n##_t), 1); + foreach_vlib_api_msg; #undef _ - vec_resize(socket_main.input_buffer, 4096); + vec_resize (socket_main.input_buffer, 4096); - /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */ - sockfd = socket(AF_INET, SOCK_STREAM, 0); + /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */ + sockfd = socket (AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) { - return clib_error_return_unix (0, "socket"); + if (sockfd < 0) + { + return clib_error_return_unix (0, "socket"); } - rv = ioctl (sockfd, FIONBIO, &one); - if (rv < 0) { - return clib_error_return_unix (0, "FIONBIO"); + rv = ioctl (sockfd, FIONBIO, &one); + if (rv < 0) + { + return clib_error_return_unix (0, "FIONBIO"); } - rv = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); - if (rv < 0) { - return clib_error_return_unix (0, "SO_REUSEADDR"); + rv = setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one)); + if (rv < 0) + { + return clib_error_return_unix (0, "SO_REUSEADDR"); } - bzero((char *) &serv_addr, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; + bzero ((char *) &serv_addr, sizeof (serv_addr)); + serv_addr.sin_family = AF_INET; - if (socket_main.bind_address) - bind_address = socket_main.bind_address; - else - bind_address = INADDR_LOOPBACK; + if (socket_main.bind_address) + bind_address = socket_main.bind_address; + else + bind_address = INADDR_LOOPBACK; - if (socket_main.portno) - portno = socket_main.portno; - else - portno = SOCKSVR_DEFAULT_PORT; + if (socket_main.portno) + portno = socket_main.portno; + else + portno = SOCKSVR_DEFAULT_PORT; - serv_addr.sin_port = clib_host_to_net_u16 (portno); - serv_addr.sin_addr.s_addr = clib_host_to_net_u32(bind_address); + serv_addr.sin_port = clib_host_to_net_u16 (portno); + serv_addr.sin_addr.s_addr = clib_host_to_net_u32 (bind_address); - if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { - return clib_error_return_unix (0, "bind"); + if (bind (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) + { + return clib_error_return_unix (0, "bind"); } - - rv = listen(sockfd,5); - if (rv < 0) { - return clib_error_return_unix (0, "listen"); + + rv = listen (sockfd, 5); + if (rv < 0) + { + return clib_error_return_unix (0, "listen"); } - pool_get (socket_main.registration_pool, rp); - memset(rp, 0, sizeof(*rp)); - - rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN; - - template.read_function = socksvr_accept_ready; - template.write_function = socksvr_bogus_write; - template.file_descriptor = sockfd; - template.private_data = rp - socket_main.registration_pool; - - rp->unix_file_index = unix_file_add (um, &template); - return 0; + pool_get (socket_main.registration_pool, rp); + memset (rp, 0, sizeof (*rp)); + + rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN; + + template.read_function = socksvr_accept_ready; + template.write_function = socksvr_bogus_write; + template.file_descriptor = sockfd; + template.private_data = rp - socket_main.registration_pool; + + rp->unix_file_index = unix_file_add (um, &template); + return 0; } static clib_error_t * socket_exit (vlib_main_t * vm) { - unix_main_t * um = &unix_main; - vl_api_registration_t *rp; + unix_main_t *um = &unix_main; + vl_api_registration_t *rp; - /* Defensive driving in case something wipes out early */ - if (socket_main.registration_pool) { - u32 index; + /* Defensive driving in case something wipes out early */ + if (socket_main.registration_pool) + { + u32 index; + /* *INDENT-OFF* */ pool_foreach (rp, socket_main.registration_pool, ({ unix_file_del (um, um->file_pool + rp->unix_file_index); index = rp->vl_api_registration_pool_index; vl_free_socket_registration_index (index); })); +/* *INDENT-ON* */ } - return 0; + return 0; } VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit); @@ -609,38 +652,51 @@ VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit); static clib_error_t * socksvr_config (vlib_main_t * vm, unformat_input_t * input) { - int portno; + int portno; - while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { - if (unformat (input, "port %d", &portno)) { - socket_main.portno = portno; - } else { - return clib_error_return (0, "unknown input '%U'", - format_unformat_error, input); - } + if (unformat (input, "port %d", &portno)) + { + socket_main.portno = portno; + } + else + { + return clib_error_return (0, "unknown input '%U'", + format_unformat_error, input); + } } - return socksvr_api_init (vm); + return socksvr_api_init (vm); } VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr"); /* argument in host byte order */ -void socksvr_set_port (u16 port) +void +socksvr_set_port (u16 port) { - socket_main.portno = port; + socket_main.portno = port; } /* argument in host byte order */ -void socksvr_set_bind_address (u32 bind_address) +void +socksvr_set_bind_address (u32 bind_address) { - socket_main.bind_address = bind_address; + socket_main.bind_address = bind_address; } clib_error_t * vlibsocket_init (vlib_main_t * vm) { - return 0; + return 0; } VLIB_INIT_FUNCTION (vlibsocket_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib-api/vlibsocket/vl_socket_api_h.h b/vlib-api/vlibsocket/vl_socket_api_h.h index 7877ba66e1c..7fc53cefc03 100644 --- a/vlib-api/vlibsocket/vl_socket_api_h.h +++ b/vlib-api/vlibsocket/vl_socket_api_h.h @@ -1,4 +1,4 @@ -/* +/* *------------------------------------------------------------------ * vl_socket_api_h.h - all API headers, in a specific order. * @@ -19,7 +19,15 @@ /* * Add to the bottom of the #include list, or elves will steal your - * keyboard in the middle of the night! + * keyboard in the middle of the night! */ #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: + */ diff --git a/vlib-api/vlibsocket/vl_socket_msg_enum.h b/vlib-api/vlibsocket/vl_socket_msg_enum.h index 13786a08e04..cdc61a5bcf5 100644 --- a/vlib-api/vlibsocket/vl_socket_msg_enum.h +++ b/vlib-api/vlibsocket/vl_socket_msg_enum.h @@ -1,4 +1,4 @@ -/* +/* *------------------------------------------------------------------ * vl_msg_enum.h - Our view of how to number API messages * Clients have their own view, which has to agree with ours. @@ -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: + */ |