18 #include <sys/types.h> 19 #include <sys/socket.h> 27 #include <sys/ioctl.h> 28 #include <sys/epoll.h> 31 #define SOCK_SERVER_MAX_TEST_CONN 10 32 #define SOCK_SERVER_MAX_EPOLL_EVENTS 10 56 struct epoll_event listen_ev;
60 struct epoll_event af_unix_listen_ev;
61 struct sockaddr_un serveraddr;
69 struct timeval timeout;
84 stfail (
"conn_pool_expand()");
89 memset (conn, 0,
sizeof (*conn));
135 stinf (
"(fd %d): Replying to cfg message!\n", conn->
fd);
147 int client_fd = conn->
fd;
153 clock_gettime (CLOCK_REALTIME, &conn->
stats.
stop);
167 snprintf (buf,
sizeof (buf),
"SERVER (fd %d) RESULTS",
183 stinf (
" sock server main\n" 186 " buf size: %u (0x%08x)\n" 192 stinf (
"SERVER (fd %d): %s-directional Stream Test Complete!\n" 199 "SERVER (fd %d): %s-directional Stream Test!\n" 200 " Sending client the test cfg to start streaming data...\n",
209 memset (&conn->
stats, 0, sizeof (conn->
stats));
210 clock_gettime (CLOCK_REALTIME, &conn->
stats.
start);
218 int client_fd = conn->
fd;
227 clock_gettime (CLOCK_REALTIME, &conn->
stats.
stop);
235 int af_unix_client_fd, rv;
240 (
struct sockaddr *) NULL, NULL);
241 if (af_unix_client_fd < 0)
242 stfail (
"af_unix_echo accept()");
244 stinf (
"Got an AF_UNIX connection -- fd = %d (0x%08x)!",
245 af_unix_client_fd, af_unix_client_fd);
247 memset (buffer, 0,
sizeof (buffer));
249 rv = read (af_unix_client_fd, buffer, nbytes);
251 stfail (
"af_unix_echo read()");
254 buffer[
sizeof (
buffer) - 1] = 0;
255 stinf (
"(AF_UNIX): RX (%d bytes) - '%s'", rv, buffer);
259 rv = write (af_unix_client_fd, buffer, nbytes);
261 stfail (
"af_unix_echo write()");
262 stinf (
"(AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
265 close (af_unix_client_fd);
280 stfail (
"No free connections!");
282 client_fd = accept (ssm->
listen_fd, (
struct sockaddr *) NULL, NULL);
284 stfail (
"new_client accept()");
286 stinf (
"Got a connection -- fd = %d (0x%08x)!\n", client_fd, client_fd);
287 if (fcntl (client_fd, F_SETFL, O_NONBLOCK) < 0)
290 conn->
fd = client_fd;
292 struct epoll_event ev;
297 rv = epoll_ctl (ssm->
epfd, EPOLL_CTL_ADD, client_fd, &ev);
300 stfail (
"new_client epoll_ctl()");
316 stfail (
"echo_af_unix_init socket()");
320 strncpy (ssm->
serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME,
326 stfail (
"echo_af_unix_init bind()");
330 stfail (
"echo_af_unix_init listen()");
337 stfail (
"echo_af_unix_init epoll_ctl()");
346 "sock_test_server [OPTIONS] <port>\n" 348 " -h Print this message and exit.\n" 350 " -u Use UDP transport layer\n");
358 int tx_bytes, nbytes, pos;
362 ((
char *) conn->
buf)[pos] = 0;
365 stinf (
"(fd %d): Echoing back\n", conn->
fd);
367 nbytes = strlen ((
const char *) conn->
buf) + 1;
372 stinf (
"(fd %d): TX (%d bytes) - '%s'\n", conn->
fd, tx_bytes, conn->
buf);
383 stinf (
"(fd %d): Received a cfg message!\n", conn->
fd);
387 if (rx_bytes !=
sizeof (*rx_cfg))
389 stinf (
"(fd %d): Invalid cfg message size (%d) expected %lu!", conn->
fd,
390 rx_bytes, sizeof (*rx_cfg));
395 stinf (
"(fd %d): Replying to cfg message!\n", conn->
fd);
403 switch (rx_cfg->
test)
422 stinf (
"Have a great day connection %d!", conn->
fd);
425 stinf (
"Closed client fd %d", conn->
fd);
430 stinf (
"ERROR: Unknown test type!\n");
452 int client_fd, rv, main_rv = 0, rx_bytes,
c, v,
i;
456 struct sockaddr_storage servaddr;
458 uint32_t servaddr_size;
461 while ((c = getopt (argc, argv,
"6D")) != -1)
476 if (isprint (optopt))
477 stinf (
"ERROR: Unknown option `-%c'", optopt);
479 stinf (
"ERROR: Unknown option character `\\x%x'.\n", optopt);
487 if (argc < (optind + 1))
489 stinf (
"ERROR: Insufficient number of arguments!\n");
493 if (sscanf (argv[optind],
"%d", &v) == 1)
497 stinf (
"ERROR: Invalid port (%s)!\n", argv[optind]);
510 memset (&servaddr, 0,
sizeof (servaddr));
514 struct sockaddr_in6 *server_addr = (
struct sockaddr_in6 *) &servaddr;
515 servaddr_size =
sizeof (*server_addr);
516 server_addr->sin6_family = AF_INET6;
517 server_addr->sin6_addr = in6addr_any;
518 server_addr->sin6_port = htons (port);
522 struct sockaddr_in *server_addr = (
struct sockaddr_in *) &servaddr;
523 servaddr_size =
sizeof (*server_addr);
524 server_addr->sin_family = AF_INET;
525 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
526 server_addr->sin_port = htons (port);
529 rv = bind (ssm->
listen_fd, (
struct sockaddr *) &servaddr, servaddr_size);
533 rv = fcntl (ssm->
listen_fd, F_SETFL, O_NONBLOCK);
541 ssm->
epfd = epoll_create (1);
543 stfail (
"main epoll_create()");
550 stfail (
"main epoll_ctl()");
552 stinf (
"Waiting for a client to connect on port %d...\n", port);
560 stfail (
"main epoll_wait()");
564 stinf (
"epoll_wait() timeout!\n");
567 for (i = 0; i < num_ev; i++)
570 if (ssm->
wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
586 client_fd = conn->
fd;
596 if (errno == ECONNRESET)
598 stinf (
"Connection reset by peer\n");
614 stinf (
"All client connections closed.\n\nSERVER: " 615 "May the force be with you!\n\n");
626 if (ioctl (conn->
fd, FIONREAD))
630 else if (isascii (conn->
buf[0]))
636 stwrn (
"FIFO not drained! extra bytes %d", rx_bytes);
static void new_client(void)
static int sock_test_read(int fd, uint8_t *buf, uint32_t nbytes, vcl_test_stats_t *stats)
static void conn_pool_expand(size_t expand_size)
static int sts_conn_expect_config(sock_server_conn_t *conn)
#define SOCK_TEST_AF_UNIX_ACCEPT_DATA
Optimized string handling code, including c11-compliant "safe C library" variants.
static void sync_config_and_reply(sock_server_conn_t *conn, vcl_test_cfg_t *rx_cfg)
static void conn_pool_free(sock_server_conn_t *conn)
struct sockaddr_un serveraddr
#define VCL_TEST_SERVER_PORT
#define SOCK_TEST_BANNER_STRING
#define SOCK_TEST_AF_UNIX_FILENAME
static void vcl_test_stats_dump(char *header, vcl_test_stats_t *stats, uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
static int sock_test_write(int fd, uint8_t *buf, uint32_t nbytes, vcl_test_stats_t *stats, uint32_t verbose)
#define SOCK_SERVER_MAX_TEST_CONN
#define VCL_TEST_SEPARATOR_STRING
static void vcl_test_cfg_dump(vcl_test_cfg_t *cfg, uint8_t is_client)
struct epoll_event listen_ev
static void stream_test_server_start_stop(sock_server_conn_t *conn, vcl_test_cfg_t *rx_cfg)
#define VCL_TEST_CFG_CTRL_MAGIC
struct epoll_event af_unix_listen_ev
sock_server_main_t sock_server_main
static int sts_handle_cfg(vcl_test_cfg_t *rx_cfg, sock_server_conn_t *conn, int rx_bytes)
sll srl srl sll sra u16x4 i
static void vcl_test_cfg_init(vcl_test_cfg_t *cfg)
#define stwrn(_fmt, _args...)
static void vcl_test_buf_alloc(vcl_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf, uint32_t *bufsize)
#define SOCK_TEST_MIXED_EPOLL_DATA
static sock_server_conn_t * conn_pool_alloc(void)
void print_usage_and_exit(void)
static void af_unix_echo(void)
void * realloc(void *p, size_t size)
#define stinf(_fmt, _args...)
int main(int argc, char **argv)
#define SOCK_SERVER_MAX_EPOLL_EVENTS
static void vcl_test_stats_accumulate(vcl_test_stats_t *accum, vcl_test_stats_t *incr)
static void sts_server_echo(sock_server_conn_t *conn, int rx_bytes)
struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS]
sock_server_conn_t * conn_pool
static int socket_server_echo_af_unix_init(sock_server_main_t *ssm)
static void stream_test_server(sock_server_conn_t *conn, int rx_bytes)