2 * Copyright (c) 2017-2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <sys/types.h>
19 #include <sys/socket.h>
26 #include <vcl/vcl_test.h>
27 #include <sys/epoll.h>
28 #include <vppinfra/mem.h>
38 sock_test_stats_t stats;
41 } vcl_test_server_conn_t;
50 } vcl_test_server_cfg_t;
52 #define SOCK_SERVER_MAX_TEST_CONN 16
53 #define SOCK_SERVER_MAX_EPOLL_EVENTS 16
60 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
61 size_t conn_pool_size;
62 vcl_test_server_conn_t *conn_pool;
64 pthread_t thread_handle;
65 } vcl_test_server_worker_t;
69 vcl_test_server_cfg_t cfg;
70 vcl_test_server_worker_t *workers;
72 struct sockaddr_storage servaddr;
73 volatile int worker_fails;
74 volatile int active_workers;
75 } vcl_test_server_main_t;
77 static __thread int __wrk_index = 0;
79 static vcl_test_server_main_t sock_server_main;
81 #define vtfail(_fn, _rv) \
84 perror ("ERROR when calling " _fn); \
85 fprintf (stderr, "\nSERVER ERROR: " _fn " failed (errno = %d)!\n", -_rv);\
89 #define vterr(_fn, _rv) \
92 fprintf (stderr, "\nSERVER ERROR: " _fn " failed (errno = %d)!\n", -_rv);\
95 #define vtwrn(_fmt, _args...) \
96 fprintf (stderr, "\nSERVER ERROR: " _fmt "\n", ##_args) \
98 #define vtinf(_fmt, _args...) \
99 fprintf (stdout, "\nvts<w%u>: " _fmt "\n", __wrk_index, ##_args) \
102 conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
104 vcl_test_server_conn_t *conn_pool;
105 size_t new_size = wrk->conn_pool_size + expand_size;
108 conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
111 for (i = wrk->conn_pool_size; i < new_size; i++)
113 vcl_test_server_conn_t *conn = &conn_pool[i];
114 memset (conn, 0, sizeof (*conn));
115 sock_test_cfg_init (&conn->cfg);
116 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
117 &conn->buf, &conn->buf_size);
118 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
121 wrk->conn_pool = conn_pool;
122 wrk->conn_pool_size = new_size;
126 vterr ("conn_pool_expand()", -errno);
130 static inline vcl_test_server_conn_t *
131 conn_pool_alloc (vcl_test_server_worker_t * wrk)
135 for (i = 0; i < wrk->conn_pool_size; i++)
137 if (!wrk->conn_pool[i].is_alloc)
139 wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip;
140 wrk->conn_pool[i].is_alloc = 1;
141 return (&wrk->conn_pool[i]);
149 conn_pool_free (vcl_test_server_conn_t * conn)
156 sync_config_and_reply (vcl_test_server_conn_t * conn,
157 sock_test_cfg_t * rx_cfg)
160 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
161 &conn->buf, &conn->buf_size);
162 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
164 if (conn->cfg.verbose)
166 vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
167 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
169 (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
170 sizeof (conn->cfg), NULL, conn->cfg.verbose);
174 stream_test_server_start_stop (vcl_test_server_worker_t * wrk,
175 vcl_test_server_conn_t * conn,
176 sock_test_cfg_t * rx_cfg)
178 int client_fd = conn->fd;
179 sock_test_t test = rx_cfg->test;
181 if (rx_cfg->ctrl_handle == conn->fd)
184 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
186 for (i = 0; i < wrk->conn_pool_size; i++)
188 vcl_test_server_conn_t *tc = &wrk->conn_pool[i];
190 if (tc->cfg.ctrl_handle == conn->fd)
192 sock_test_stats_accumulate (&conn->stats, &tc->stats);
194 if (conn->cfg.verbose)
198 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
199 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
200 test == SOCK_TEST_TYPE_BI
207 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
208 (test == SOCK_TEST_TYPE_BI) /* show_tx */ ,
210 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
211 if (conn->cfg.verbose)
213 printf (" sock server main\n"
214 SOCK_TEST_SEPARATOR_STRING
216 " buf size: %u (0x%08x)\n"
217 SOCK_TEST_SEPARATOR_STRING,
218 conn->buf, conn->buf_size, conn->buf_size);
221 sync_config_and_reply (conn, rx_cfg);
222 printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
223 SOCK_TEST_BANNER_STRING "\n", conn->fd,
224 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
228 printf ("\n" SOCK_TEST_BANNER_STRING
229 "SERVER (fd %d): %s-directional Stream Test!\n"
230 " Sending client the test cfg to start streaming data...\n",
231 client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
233 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
236 sync_config_and_reply (conn, rx_cfg);
238 /* read the 1st chunk, record start time */
239 memset (&conn->stats, 0, sizeof (conn->stats));
240 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
246 stream_test_server (vcl_test_server_conn_t * conn, int rx_bytes)
248 int client_fd = conn->fd;
249 sock_test_t test = conn->cfg.test;
251 if (test == SOCK_TEST_TYPE_BI)
252 (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
255 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
257 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
262 vcl_test_server_echo (vcl_test_server_conn_t * conn, int rx_bytes)
264 int tx_bytes, nbytes, pos;
266 /* If it looks vaguely like a string,
267 * make sure it's terminated
269 pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
270 ((char *) conn->buf)[pos] = 0;
271 vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->buf);
273 if (conn->cfg.verbose)
274 vtinf ("(fd %d): Echoing back", conn->fd);
276 nbytes = strlen ((const char *) conn->buf) + 1;
277 tx_bytes = vcl_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
280 vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf);
284 vcl_test_server_new_client (vcl_test_server_worker_t * wrk)
287 vcl_test_server_conn_t *conn;
289 conn = conn_pool_alloc (wrk);
292 vtwrn ("No free connections!");
296 client_fd = vppcom_session_accept (wrk->listen_fd, &conn->endpt, 0);
299 vterr ("vppcom_session_accept()", client_fd);
303 vtinf ("Got a connection -- fd = %d (0x%08x)!", client_fd, client_fd);
305 conn->fd = client_fd;
308 struct epoll_event ev;
312 ev.data.u64 = conn - wrk->conn_pool;
313 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev);
316 vterr ("vppcom_epoll_ctl()", rv);
324 print_usage_and_exit (void)
327 "sock_test_server [OPTIONS] <port>\n"
329 " -h Print this message and exit.\n"
331 " -w <num> Number of workers\n"
332 " -u Use UDP transport layer\n");
337 vcl_test_init_endpoint_addr (vcl_test_server_main_t * ssm)
339 struct sockaddr_storage *servaddr = &ssm->servaddr;
340 memset (servaddr, 0, sizeof (*servaddr));
342 if (ssm->cfg.address_ip6)
344 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
345 server_addr->sin6_family = AF_INET6;
346 server_addr->sin6_addr = in6addr_any;
347 server_addr->sin6_port = htons (ssm->cfg.port);
351 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
352 server_addr->sin_family = AF_INET;
353 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
354 server_addr->sin_port = htons (ssm->cfg.port);
357 if (ssm->cfg.address_ip6)
359 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
360 ssm->cfg.endpt.is_ip4 = 0;
361 ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr;
362 ssm->cfg.endpt.port = (uint16_t) server_addr->sin6_port;
366 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
367 ssm->cfg.endpt.is_ip4 = 1;
368 ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr;
369 ssm->cfg.endpt.port = (uint16_t) server_addr->sin_port;
374 vcl_test_server_process_opts (vcl_test_server_main_t * ssm, int argc,
379 ssm->cfg.proto = VPPCOM_PROTO_TCP;
382 while ((c = getopt (argc, argv, "6Dw:")) != -1)
386 ssm->cfg.address_ip6 = 1;
390 ssm->cfg.proto = VPPCOM_PROTO_UDP;
396 ssm->cfg.workers = v;
398 vtwrn ("Invalid number of workers %d", v);
405 if (isprint (optopt))
406 fprintf (stderr, "SERVER: ERROR: Unknown "
407 "option `-%c'.\n", optopt);
409 fprintf (stderr, "SERVER: ERROR: Unknown "
410 "option character `\\x%x'.\n", optopt);
415 print_usage_and_exit ();
418 if (argc < (optind + 1))
420 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
421 print_usage_and_exit ();
424 if (sscanf (argv[optind], "%d", &v) == 1)
425 ssm->cfg.port = (uint16_t) v;
428 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
429 print_usage_and_exit ();
432 vcl_test_init_endpoint_addr (ssm);
436 vcl_test_server_handle_cfg (vcl_test_server_worker_t * wrk,
437 sock_test_cfg_t * rx_cfg,
438 vcl_test_server_conn_t * conn, int rx_bytes)
442 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
443 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
446 if (rx_bytes != sizeof (*rx_cfg))
448 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
449 rx_bytes, sizeof (*rx_cfg));
450 conn->cfg.rxbuf_size = 0;
451 conn->cfg.num_writes = 0;
452 if (conn->cfg.verbose)
454 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
455 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
457 vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
458 sizeof (conn->cfg), NULL, conn->cfg.verbose);
462 switch (rx_cfg->test)
464 case SOCK_TEST_TYPE_NONE:
465 case SOCK_TEST_TYPE_ECHO:
466 sync_config_and_reply (conn, rx_cfg);
469 case SOCK_TEST_TYPE_BI:
470 case SOCK_TEST_TYPE_UNI:
471 stream_test_server_start_stop (wrk, conn, rx_cfg);
474 case SOCK_TEST_TYPE_EXIT:
475 vtinf ("Have a great day conn %d!", conn->fd);
476 vppcom_session_close (conn->fd);
477 conn_pool_free (conn);
478 vtinf ("Closed client fd %d", conn->fd);
483 vtwrn ("Unknown test type %d", rx_cfg->test);
484 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
492 vcl_test_server_worker_init (vcl_test_server_worker_t * wrk)
494 vcl_test_server_main_t *ssm = &sock_server_main;
495 struct epoll_event listen_ev;
498 __wrk_index = wrk->wrk_index;
500 vtinf ("Initializing worker ...");
502 conn_pool_expand (wrk, SOCK_SERVER_MAX_TEST_CONN + 1);
504 vppcom_worker_register ();
506 wrk->listen_fd = vppcom_session_create (ssm->cfg.proto,
507 0 /* is_nonblocking */ );
508 if (wrk->listen_fd < 0)
509 vtfail ("vppcom_session_create()", wrk->listen_fd);
511 rv = vppcom_session_bind (wrk->listen_fd, &ssm->cfg.endpt);
513 vtfail ("vppcom_session_bind()", rv);
515 if (!(ssm->cfg.proto == VPPCOM_PROTO_UDP))
517 rv = vppcom_session_listen (wrk->listen_fd, 10);
519 vtfail ("vppcom_session_listen()", rv);
522 wrk->epfd = vppcom_epoll_create ();
524 vtfail ("vppcom_epoll_create()", wrk->epfd);
526 listen_ev.events = EPOLLIN;
527 listen_ev.data.u32 = ~0;
528 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listen_fd,
531 vtfail ("vppcom_epoll_ctl", rv);
533 vtinf ("Waiting for a client to connect on port %d ...", ssm->cfg.port);
537 vcl_test_server_worker_loop (void *arg)
539 vcl_test_server_main_t *ssm = &sock_server_main;
540 vcl_test_server_worker_t *wrk = arg;
541 vcl_test_server_conn_t *conn;
542 int i, rx_bytes, num_ev;
543 sock_test_cfg_t *rx_cfg;
546 vcl_test_server_worker_init (wrk);
550 num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events,
551 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000.0);
554 vterr ("vppcom_epoll_wait()", num_ev);
557 else if (num_ev == 0)
559 vtinf ("vppcom_epoll_wait() timeout!");
562 for (i = 0; i < num_ev; i++)
564 conn = &wrk->conn_pool[wrk->wait_events[i].data.u32];
565 if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
567 vppcom_session_close (conn->fd);
570 if (wrk->wait_events[i].data.u32 == ~0)
572 vcl_test_server_new_client (wrk);
576 if (EPOLLIN & wrk->wait_events[i].events)
578 rx_bytes = vcl_test_read (conn->fd, conn->buf,
579 conn->buf_size, &conn->stats);
583 if (errno == ECONNRESET)
585 vtinf ("Connection reset by remote peer.\n");
592 rx_cfg = (sock_test_cfg_t *) conn->buf;
593 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
595 vcl_test_server_handle_cfg (wrk, rx_cfg, conn, rx_bytes);
598 vtinf ("All client connections closed\n");
599 vtinf ("May the force be with you!\n");
604 else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI)
605 || (conn->cfg.test == SOCK_TEST_TYPE_BI))
607 stream_test_server (conn, rx_bytes);
610 else if (isascii (conn->buf[0]))
612 vcl_test_server_echo (conn, rx_bytes);
616 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
621 vtwrn ("Unhandled event");
628 ssm->worker_fails -= 1;
631 vppcom_session_close (wrk->listen_fd);
633 free (wrk->conn_pool);
634 ssm->active_workers -= 1;
639 main (int argc, char **argv)
641 vcl_test_server_main_t *ssm = &sock_server_main;
644 clib_mem_init_thread_safe (0, 64 << 20);
645 ssm->cfg.port = SOCK_TEST_SERVER_PORT;
646 ssm->cfg.workers = 1;
647 vcl_test_server_process_opts (ssm, argc, argv);
649 rv = vppcom_app_create ("vcl_test_server");
651 vtfail ("vppcom_app_create()", rv);
653 ssm->workers = calloc (ssm->cfg.workers, sizeof (*ssm->workers));
654 vcl_test_server_worker_init (&ssm->workers[0]);
655 for (i = 1; i < ssm->cfg.workers; i++)
657 ssm->workers[i].wrk_index = i;
658 rv = pthread_create (&ssm->workers[i].thread_handle, NULL,
659 vcl_test_server_worker_loop,
660 (void *) &ssm->workers[i]);
662 vcl_test_server_worker_loop (&ssm->workers[0]);
664 while (ssm->active_workers)
667 vppcom_app_destroy ();
670 return ssm->worker_fails;
674 * fd.io coding-style-patch-verification: ON
677 * eval: (c-set-style "gnu")