18 #include <sys/types.h> 19 #include <sys/socket.h> 40 #define SOCK_SERVER_MAX_TEST_CONN 10 50 struct timeval timeout;
61 for (nfds = i = 0; i < FD_SETSIZE; i++)
74 FD_SET (conn->
fd, fdset);
83 FD_CLR (conn->
fd, fdset);
101 memset (conn, 0,
sizeof (*conn));
113 int errno_val = errno;
114 perror (
"ERROR in conn_pool_expand()");
115 fprintf (stderr,
"ERROR: Memory allocation failed (errno = %d)!\n",
162 printf (
"\nSERVER (fd %d): Replying to cfg message!\n", conn->
fd);
174 int client_fd = conn->
fd;
180 clock_gettime (CLOCK_REALTIME, &conn->
stats.
stop);
194 sprintf (buf,
"SERVER (fd %d) RESULTS", tc->
fd);
209 printf (
" sock server main\n" 212 " buf size: %u (0x%08x)\n" 218 printf (
"\nSERVER (fd %d): %s-directional Stream Test Complete!\n" 225 "SERVER (fd %d): %s-directional Stream Test!\n" 226 " Sending client the test cfg to start streaming data...\n",
235 memset (&conn->
stats, 0, sizeof (conn->
stats));
236 clock_gettime (CLOCK_REALTIME, &conn->
stats.
start);
244 int client_fd = conn->
fd;
253 clock_gettime (CLOCK_REALTIME, &conn->
stats.
stop);
270 fprintf (stderr,
"\nERROR: No free connections!\n");
284 perror (
"ERROR in main()");
285 fprintf (stderr,
"ERROR: accept failed (errno = %d)!\n", errno_val);
288 printf (
"SERVER: Got a connection -- fd = %d (0x%08x)!\n",
289 client_fd, client_fd);
291 conn->
fd = client_fd;
299 int client_fd, rv, main_rv = 0;
300 int tx_bytes, rx_bytes, nbytes;
304 uint64_t xtra_bytes = 0;
305 struct sockaddr_in servaddr;
309 fd_set _rfdset, *rfdset = &_rfdset;
313 fd_set _wfdset, *wfdset = &_wfdset;
316 if ((argc == 2) && (sscanf (argv[1],
"%d", &v) == 1))
335 ssm->
listen_fd = socket (AF_INET, SOCK_STREAM, 0);
340 perror (
"ERROR in main()");
341 fprintf (stderr,
"ERROR: socket() failed (errno = %d)!\n", errno_val);
345 memset (&servaddr, 0,
sizeof (servaddr));
347 servaddr.sin_family = AF_INET;
348 servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
349 servaddr.sin_port = htons (port);
353 endpt.
is_ip4 = (servaddr.sin_family == AF_INET);
354 endpt.
ip = (uint8_t *) & servaddr.sin_addr;
355 endpt.
port = (uint16_t) servaddr.sin_port;
365 bind (ssm->
listen_fd, (
struct sockaddr *) &servaddr, sizeof (servaddr));
370 perror (
"ERROR in main()");
371 fprintf (stderr,
"ERROR: bind failed (errno = %d)!\n", errno_val);
388 perror (
"ERROR in main()");
389 fprintf (stderr,
"ERROR: listen failed (errno = %d)!\n", errno_val);
399 printf (
"\nSERVER: Waiting for a client to connect on port %d...\n", port);
409 struct timeval timeout;
412 rv = select (ssm->
nfds, rfdset, wfdset,
NULL, &timeout);
418 fprintf (stderr,
"\nERROR: select() failed -- aborting!\n");
434 client_fd = conn->
fd;
436 if (FD_ISSET (client_fd, rfdset))
447 printf (
"SERVER (fd %d): Received a cfg message!\n",
452 if (rx_bytes !=
sizeof (*rx_cfg))
454 printf (
"SERVER (fd %d): Invalid cfg message " 455 "size (%d)!\n Should be %lu bytes.\n",
456 client_fd, rx_bytes,
sizeof (*rx_cfg));
461 printf (
"SERVER (fd %d): Replying to " 462 "cfg message!\n", client_fd);
471 switch (rx_cfg->
test)
484 printf (
"SERVER: Have a great day, " 485 "connection %d!\n", client_fd);
495 printf (
"SERVER: All client connections " 496 "closed.\n\nSERVER: " 497 "May the force be with you!\n\n");
503 fprintf (stderr,
"ERROR: Unknown test type!\n");
517 else if (isascii (conn->
buf[0]))
520 ((
char *) conn->
buf)[rx_bytes <
523 printf (
"\nSERVER (fd %d): RX (%d bytes) - '%s'\n",
524 conn->
fd, rx_bytes, conn->
buf);
529 if (errno == ECONNRESET)
531 printf (
"\nSERVER: Connection reset by remote peer.\n" 532 " Y'all have a great day now!\n\n");
539 if (isascii (conn->
buf[0]))
542 ((
char *) conn->
buf)[rx_bytes <
547 "ERROR: FIFO not drained in previous test!\n" 548 " extra chunks %u (0x%x)\n" 549 " extra bytes %lu (0x%lx)\n",
550 xtra, xtra, xtra_bytes, xtra_bytes);
556 printf (
"SERVER (fd %d): Echoing back\n", client_fd);
558 nbytes = strlen ((
const char *) conn->
buf) + 1;
561 nbytes, &conn->
stats,
564 printf (
"SERVER (fd %d): TX (%d bytes) - '%s'\n",
565 conn->
fd, tx_bytes, conn->
buf);
571 xtra_bytes += rx_bytes;
static void new_client(void)
static void conn_pool_expand(size_t expand_size)
sll srl srl sll sra u16x4 i
int vppcom_app_create(char *app_name)
int vppcom_session_create(u32 vrf, u8 proto, u8 is_nonblocking)
int vppcom_session_bind(uint32_t session_index, vppcom_endpt_t *ep)
static void conn_pool_free(sock_server_conn_t *conn)
static int sock_test_write(int fd, uint8_t *buf, uint32_t nbytes, sock_test_stats_t *stats, uint32_t verbose)
#define SOCK_TEST_CFG_CTRL_MAGIC
static void sock_test_buf_alloc(sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf, uint32_t *bufsize)
static void sock_test_cfg_init(sock_test_cfg_t *cfg)
#define SOCK_TEST_BANNER_STRING
int vppcom_session_accept(uint32_t listen_session_index, vppcom_endpt_t *ep, double wait_for_time)
void vppcom_app_destroy(void)
static void sock_test_cfg_dump(sock_test_cfg_t *cfg, uint8_t is_client)
int vppcom_session_listen(uint32_t listen_session_index, uint32_t q_len)
static void sync_config_and_reply(sock_server_conn_t *conn, sock_test_cfg_t *rx_cfg)
#define SOCK_SERVER_MAX_TEST_CONN
sock_server_main_t sock_server_main
int vppcom_select(unsigned long n_bits, unsigned long *read_map, unsigned long *write_map, unsigned long *except_map, double time_to_wait)
static void sock_test_stats_dump(char *header, sock_test_stats_t *stats, uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
static void conn_fdset_clr(sock_server_conn_t *conn, fd_set *fdset)
static void conn_fdset_set(sock_server_conn_t *conn, fd_set *fdset)
static int sock_test_read(int fd, uint8_t *buf, uint32_t nbytes, sock_test_stats_t *stats)
static sock_server_conn_t * conn_pool_alloc(void)
#define SOCK_TEST_SEPARATOR_STRING
int main(int argc, char **argv)
static int get_nfds(void)
static void sock_test_stats_accumulate(sock_test_stats_t *accum, sock_test_stats_t *incr)
static void stream_test_server_start_stop(sock_server_conn_t *conn, sock_test_cfg_t *rx_cfg)
#define SOCK_TEST_SERVER_PORT
int vppcom_session_close(uint32_t session_index)
#define VPPCOM_VRF_DEFAULT
sock_server_conn_t * conn_pool
static void stream_test_server(sock_server_conn_t *conn, int rx_bytes)