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 vppcom_data_segments_t ds;
42 } vcl_test_server_conn_t;
51 } vcl_test_server_cfg_t;
53 #define SOCK_SERVER_MAX_TEST_CONN 16
54 #define SOCK_SERVER_MAX_EPOLL_EVENTS 16
61 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
62 size_t conn_pool_size;
63 vcl_test_server_conn_t *conn_pool;
65 pthread_t thread_handle;
66 } vcl_test_server_worker_t;
70 vcl_test_server_cfg_t cfg;
71 vcl_test_server_worker_t *workers;
73 struct sockaddr_storage servaddr;
74 volatile int worker_fails;
75 volatile int active_workers;
77 } vcl_test_server_main_t;
79 static __thread int __wrk_index = 0;
81 static vcl_test_server_main_t sock_server_main;
84 conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
86 vcl_test_server_conn_t *conn_pool;
87 size_t new_size = wrk->conn_pool_size + expand_size;
90 conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
93 for (i = wrk->conn_pool_size; i < new_size; i++)
95 vcl_test_server_conn_t *conn = &conn_pool[i];
96 memset (conn, 0, sizeof (*conn));
97 sock_test_cfg_init (&conn->cfg);
98 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
99 &conn->buf, &conn->buf_size);
100 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
103 wrk->conn_pool = conn_pool;
104 wrk->conn_pool_size = new_size;
108 vterr ("conn_pool_expand()", -errno);
112 static inline vcl_test_server_conn_t *
113 conn_pool_alloc (vcl_test_server_worker_t * wrk)
118 for (i = 0; i < wrk->conn_pool_size; i++)
120 if (!wrk->conn_pool[i].is_alloc)
122 wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip;
123 wrk->conn_pool[i].is_alloc = 1;
124 return (&wrk->conn_pool[i]);
130 conn_pool_expand (wrk, 2 * wrk->conn_pool_size);
134 vtwrn ("Failed to allocate connection even after expand");
139 conn_pool_free (vcl_test_server_conn_t * conn)
146 sync_config_and_reply (vcl_test_server_conn_t * conn,
147 sock_test_cfg_t * rx_cfg)
150 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
151 &conn->buf, &conn->buf_size);
152 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
154 if (conn->cfg.verbose)
156 vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
157 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
159 (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
160 sizeof (conn->cfg), NULL, conn->cfg.verbose);
164 vts_server_start_stop (vcl_test_server_worker_t * wrk,
165 vcl_test_server_conn_t * conn,
166 sock_test_cfg_t * rx_cfg)
168 u8 is_bi = rx_cfg->test == SOCK_TEST_TYPE_BI;
169 int client_fd = conn->fd, i;
170 vcl_test_server_conn_t *tc;
173 if (rx_cfg->ctrl_handle == conn->fd)
175 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
177 for (i = 0; i < wrk->conn_pool_size; i++)
179 tc = &wrk->conn_pool[i];
180 if (tc->cfg.ctrl_handle != conn->fd)
183 sock_test_stats_accumulate (&conn->stats, &tc->stats);
185 if (conn->cfg.verbose)
187 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
188 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
189 is_bi /* show tx */ , conn->cfg.verbose);
193 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
194 is_bi /* show_tx */ , conn->cfg.verbose);
195 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
196 if (conn->cfg.verbose)
198 vtinf (" sock server main\n"
199 SOCK_TEST_SEPARATOR_STRING
201 " buf size: %u (0x%08x)\n"
202 SOCK_TEST_SEPARATOR_STRING,
203 conn->buf, conn->buf_size, conn->buf_size);
206 sync_config_and_reply (conn, rx_cfg);
207 vtinf ("(fd %d): %s-directional Stream Test Complete!\n"
208 SOCK_TEST_BANNER_STRING "\n", conn->fd, is_bi ? "Bi" : "Uni");
209 memset (&conn->stats, 0, sizeof (conn->stats));
213 vtinf (SOCK_TEST_BANNER_STRING "(fd %d): %s-directional Stream Test!\n"
214 " Sending client the test cfg to start streaming data...\n",
215 client_fd, is_bi ? "Bi" : "Uni");
217 if (rx_cfg->ctrl_handle == ~0)
218 rx_cfg->ctrl_handle = conn->fd;
220 sync_config_and_reply (conn, rx_cfg);
222 /* read the 1st chunk, record start time */
223 memset (&conn->stats, 0, sizeof (conn->stats));
224 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
229 vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes)
231 vcl_test_server_main_t *vts = &sock_server_main;
232 int client_fd = conn->fd;
234 if (conn->cfg.test == SOCK_TEST_TYPE_BI)
238 (void) vcl_test_write (client_fd, conn->ds[0].data, conn->ds[0].len,
239 &conn->stats, conn->cfg.verbose);
241 (void) vcl_test_write (client_fd, conn->ds[1].data,
242 conn->ds[1].len, &conn->stats,
246 (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
251 vppcom_session_free_segments (conn->fd, conn->ds);
253 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
254 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
258 vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes)
260 vcl_test_server_main_t *vts = &sock_server_main;
261 int tx_bytes, nbytes, pos;
264 vppcom_data_segment_copy (conn->buf, conn->ds, rx_bytes);
266 /* If it looks vaguely like a string, make sure it's terminated */
267 pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
268 ((char *) conn->buf)[pos] = 0;
269 vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->buf);
271 if (conn->cfg.verbose)
272 vtinf ("(fd %d): Echoing back", conn->fd);
274 nbytes = strlen ((const char *) conn->buf) + 1;
275 tx_bytes = vcl_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
278 vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf);
282 vts_new_client (vcl_test_server_worker_t * wrk)
285 vcl_test_server_conn_t *conn;
287 conn = conn_pool_alloc (wrk);
290 vtwrn ("No free connections!");
294 client_fd = vppcom_session_accept (wrk->listen_fd, &conn->endpt, 0);
297 vterr ("vppcom_session_accept()", client_fd);
301 vtinf ("Got a connection -- fd = %d (0x%08x)!", client_fd, client_fd);
303 conn->fd = client_fd;
306 struct epoll_event ev;
310 ev.data.u64 = conn - wrk->conn_pool;
311 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev);
314 vterr ("vppcom_epoll_ctl()", rv);
322 print_usage_and_exit (void)
325 "sock_test_server [OPTIONS] <port>\n"
327 " -h Print this message and exit.\n"
329 " -w <num> Number of workers\n"
330 " -u Use UDP transport layer\n");
335 vcl_test_init_endpoint_addr (vcl_test_server_main_t * ssm)
337 struct sockaddr_storage *servaddr = &ssm->servaddr;
338 memset (servaddr, 0, sizeof (*servaddr));
340 if (ssm->cfg.address_ip6)
342 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
343 server_addr->sin6_family = AF_INET6;
344 server_addr->sin6_addr = in6addr_any;
345 server_addr->sin6_port = htons (ssm->cfg.port);
349 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
350 server_addr->sin_family = AF_INET;
351 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
352 server_addr->sin_port = htons (ssm->cfg.port);
355 if (ssm->cfg.address_ip6)
357 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
358 ssm->cfg.endpt.is_ip4 = 0;
359 ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr;
360 ssm->cfg.endpt.port = (uint16_t) server_addr->sin6_port;
364 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
365 ssm->cfg.endpt.is_ip4 = 1;
366 ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr;
367 ssm->cfg.endpt.port = (uint16_t) server_addr->sin_port;
372 vcl_test_server_process_opts (vcl_test_server_main_t * ssm, int argc,
377 ssm->cfg.proto = VPPCOM_PROTO_TCP;
380 while ((c = getopt (argc, argv, "6Dsw:")) != -1)
384 ssm->cfg.address_ip6 = 1;
388 ssm->cfg.proto = VPPCOM_PROTO_UDP;
394 ssm->cfg.workers = v;
396 vtwrn ("Invalid number of workers %d", v);
405 if (isprint (optopt))
406 vtwrn ("Unknown option `-%c'.", optopt);
408 vtwrn ("Unknown option character `\\x%x'.", optopt);
413 print_usage_and_exit ();
416 if (argc < (optind + 1))
418 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
419 print_usage_and_exit ();
422 if (sscanf (argv[optind], "%d", &v) == 1)
423 ssm->cfg.port = (uint16_t) v;
426 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
427 print_usage_and_exit ();
430 vcl_test_init_endpoint_addr (ssm);
434 vts_handle_cfg (vcl_test_server_worker_t * wrk, sock_test_cfg_t * rx_cfg,
435 vcl_test_server_conn_t * conn, int rx_bytes)
439 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
440 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
443 if (rx_bytes != sizeof (*rx_cfg))
445 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
446 rx_bytes, sizeof (*rx_cfg));
447 conn->cfg.rxbuf_size = 0;
448 conn->cfg.num_writes = 0;
449 if (conn->cfg.verbose)
451 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
452 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
454 vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
455 sizeof (conn->cfg), NULL, conn->cfg.verbose);
459 switch (rx_cfg->test)
461 case SOCK_TEST_TYPE_NONE:
462 case SOCK_TEST_TYPE_ECHO:
463 sync_config_and_reply (conn, rx_cfg);
466 case SOCK_TEST_TYPE_BI:
467 case SOCK_TEST_TYPE_UNI:
468 vts_server_start_stop (wrk, conn, rx_cfg);
471 case SOCK_TEST_TYPE_EXIT:
472 vtinf ("Have a great day conn %d (closing)!", conn->fd);
473 vppcom_session_close (conn->fd);
474 conn_pool_free (conn);
479 vtwrn ("Unknown test type %d", rx_cfg->test);
480 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
488 vts_worker_init (vcl_test_server_worker_t * wrk)
490 vcl_test_server_main_t *ssm = &sock_server_main;
491 struct epoll_event listen_ev;
494 __wrk_index = wrk->wrk_index;
496 vtinf ("Initializing worker ...");
498 conn_pool_expand (wrk, SOCK_SERVER_MAX_TEST_CONN + 1);
500 if (vppcom_worker_register ())
501 vtfail ("vppcom_worker_register()", 1);
503 wrk->listen_fd = vppcom_session_create (ssm->cfg.proto,
504 0 /* is_nonblocking */ );
505 if (wrk->listen_fd < 0)
506 vtfail ("vppcom_session_create()", wrk->listen_fd);
508 rv = vppcom_session_bind (wrk->listen_fd, &ssm->cfg.endpt);
510 vtfail ("vppcom_session_bind()", rv);
512 if (!(ssm->cfg.proto == VPPCOM_PROTO_UDP))
514 rv = vppcom_session_listen (wrk->listen_fd, 10);
516 vtfail ("vppcom_session_listen()", rv);
519 wrk->epfd = vppcom_epoll_create ();
521 vtfail ("vppcom_epoll_create()", wrk->epfd);
523 listen_ev.events = EPOLLIN;
524 listen_ev.data.u32 = ~0;
525 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listen_fd,
528 vtfail ("vppcom_epoll_ctl", rv);
530 ssm->active_workers += 1;
531 vtinf ("Waiting for a client to connect on port %d ...", ssm->cfg.port);
535 vts_conn_expect_config (vcl_test_server_conn_t * conn)
537 if (conn->cfg.test == SOCK_TEST_TYPE_ECHO)
540 return (conn->stats.rx_bytes < 128
541 || conn->stats.rx_bytes >= conn->cfg.total_bytes);
544 static sock_test_cfg_t *
545 vts_conn_read_config (vcl_test_server_conn_t * conn)
547 vcl_test_server_main_t *vts = &sock_server_main;
551 /* We could avoid the copy if the first segment is big enough but this
552 * just simplifies things */
553 vppcom_data_segment_copy (conn->buf, conn->ds,
554 sizeof (sock_test_cfg_t));
555 vppcom_session_free_segments (conn->fd, conn->ds);
557 return (sock_test_cfg_t *) conn->buf;
561 vts_conn_read (vcl_test_server_conn_t * conn)
563 vcl_test_server_main_t *vts = &sock_server_main;
565 return vcl_test_read_ds (conn->fd, conn->ds, &conn->stats);
567 return vcl_test_read (conn->fd, conn->buf, conn->buf_size, &conn->stats);
571 vts_conn_has_ascii (vcl_test_server_conn_t * conn)
573 vcl_test_server_main_t *vts = &sock_server_main;
576 return isascii (conn->ds[0].data[0]);
578 return isascii (conn->buf[0]);
582 vts_worker_loop (void *arg)
584 vcl_test_server_main_t *ssm = &sock_server_main;
585 vcl_test_server_worker_t *wrk = arg;
586 vcl_test_server_conn_t *conn;
587 int i, rx_bytes, num_ev;
588 sock_test_cfg_t *rx_cfg;
591 vts_worker_init (wrk);
595 num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events,
596 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000.0);
599 vterr ("vppcom_epoll_wait()", num_ev);
602 else if (num_ev == 0)
604 vtinf ("vppcom_epoll_wait() timeout!");
607 for (i = 0; i < num_ev; i++)
609 conn = &wrk->conn_pool[wrk->wait_events[i].data.u32];
610 if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
612 vppcom_session_close (conn->fd);
616 vtinf ("All client connections closed\n");
621 if (wrk->wait_events[i].data.u32 == ~0)
623 vts_new_client (wrk);
627 if (EPOLLIN & wrk->wait_events[i].events)
630 rx_bytes = vts_conn_read (conn);
634 if (errno == ECONNRESET)
636 vtinf ("Connection reset by remote peer.\n");
643 if (vts_conn_expect_config (conn))
645 rx_cfg = vts_conn_read_config (conn);
646 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
648 vts_handle_cfg (wrk, rx_cfg, conn, rx_bytes);
651 vtinf ("All client connections closed\n");
657 if ((conn->cfg.test == SOCK_TEST_TYPE_UNI)
658 || (conn->cfg.test == SOCK_TEST_TYPE_BI))
660 vts_server_rx (conn, rx_bytes);
661 if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0,
666 if (vts_conn_has_ascii (conn))
668 vts_server_echo (conn, rx_bytes);
672 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
677 vtwrn ("Unhandled event");
684 ssm->worker_fails -= 1;
687 vppcom_session_close (wrk->listen_fd);
689 free (wrk->conn_pool);
690 ssm->active_workers -= 1;
695 main (int argc, char **argv)
697 vcl_test_server_main_t *vsm = &sock_server_main;
700 clib_mem_init_thread_safe (0, 64 << 20);
701 vsm->cfg.port = SOCK_TEST_SERVER_PORT;
702 vsm->cfg.workers = 1;
703 vsm->active_workers = 0;
704 vcl_test_server_process_opts (vsm, argc, argv);
706 rv = vppcom_app_create ("vcl_test_server");
708 vtfail ("vppcom_app_create()", rv);
710 vsm->workers = calloc (vsm->cfg.workers, sizeof (*vsm->workers));
711 vts_worker_init (&vsm->workers[0]);
712 for (i = 1; i < vsm->cfg.workers; i++)
714 vsm->workers[i].wrk_index = i;
715 rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
716 vts_worker_loop, (void *) &vsm->workers[i]);
718 vts_worker_loop (&vsm->workers[0]);
720 while (vsm->active_workers > 0)
723 vppcom_app_destroy ();
726 return vsm->worker_fails;
730 * fd.io coding-style-patch-verification: ON
733 * eval: (c-set-style "gnu")