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;
82 conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
84 vcl_test_server_conn_t *conn_pool;
85 size_t new_size = wrk->conn_pool_size + expand_size;
88 conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
91 for (i = wrk->conn_pool_size; i < new_size; i++)
93 vcl_test_server_conn_t *conn = &conn_pool[i];
94 memset (conn, 0, sizeof (*conn));
95 sock_test_cfg_init (&conn->cfg);
96 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
97 &conn->buf, &conn->buf_size);
98 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
101 wrk->conn_pool = conn_pool;
102 wrk->conn_pool_size = new_size;
106 vterr ("conn_pool_expand()", -errno);
110 static inline vcl_test_server_conn_t *
111 conn_pool_alloc (vcl_test_server_worker_t * wrk)
115 for (i = 0; i < wrk->conn_pool_size; i++)
117 if (!wrk->conn_pool[i].is_alloc)
119 wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip;
120 wrk->conn_pool[i].is_alloc = 1;
121 return (&wrk->conn_pool[i]);
129 conn_pool_free (vcl_test_server_conn_t * conn)
136 sync_config_and_reply (vcl_test_server_conn_t * conn,
137 sock_test_cfg_t * rx_cfg)
140 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
141 &conn->buf, &conn->buf_size);
142 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
144 if (conn->cfg.verbose)
146 vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
147 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
149 (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
150 sizeof (conn->cfg), NULL, conn->cfg.verbose);
154 vts_server_start_stop (vcl_test_server_worker_t * wrk,
155 vcl_test_server_conn_t * conn,
156 sock_test_cfg_t * rx_cfg)
158 u8 is_bi = rx_cfg->test == SOCK_TEST_TYPE_BI;
159 int client_fd = conn->fd, i;
160 vcl_test_server_conn_t *tc;
163 if (rx_cfg->ctrl_handle == conn->fd)
165 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
167 for (i = 0; i < wrk->conn_pool_size; i++)
169 tc = &wrk->conn_pool[i];
170 if (tc->cfg.ctrl_handle != conn->fd)
173 sock_test_stats_accumulate (&conn->stats, &tc->stats);
175 if (conn->cfg.verbose)
177 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
178 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
179 is_bi /* show tx */ , conn->cfg.verbose);
183 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
184 is_bi /* show_tx */ , conn->cfg.verbose);
185 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
186 if (conn->cfg.verbose)
188 vtinf (" sock server main\n"
189 SOCK_TEST_SEPARATOR_STRING
191 " buf size: %u (0x%08x)\n"
192 SOCK_TEST_SEPARATOR_STRING,
193 conn->buf, conn->buf_size, conn->buf_size);
196 sync_config_and_reply (conn, rx_cfg);
197 vtinf ("(fd %d): %s-directional Stream Test Complete!\n"
198 SOCK_TEST_BANNER_STRING "\n", conn->fd, is_bi ? "Bi" : "Uni");
202 vtinf (SOCK_TEST_BANNER_STRING "(fd %d): %s-directional Stream Test!\n"
203 " Sending client the test cfg to start streaming data...\n",
204 client_fd, is_bi ? "Bi" : "Uni");
206 if (rx_cfg->ctrl_handle == ~0)
207 rx_cfg->ctrl_handle = conn->fd;
209 sync_config_and_reply (conn, rx_cfg);
211 /* read the 1st chunk, record start time */
212 memset (&conn->stats, 0, sizeof (conn->stats));
213 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
218 vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes)
220 int client_fd = conn->fd;
221 sock_test_t test = conn->cfg.test;
223 if (test == SOCK_TEST_TYPE_BI)
224 (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
227 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
228 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
232 vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes)
234 int tx_bytes, nbytes, pos;
236 /* If it looks vaguely like a string,
237 * make sure it's terminated
239 pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
240 ((char *) conn->buf)[pos] = 0;
241 vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->buf);
243 if (conn->cfg.verbose)
244 vtinf ("(fd %d): Echoing back", conn->fd);
246 nbytes = strlen ((const char *) conn->buf) + 1;
247 tx_bytes = vcl_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
250 vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf);
254 vts_new_client (vcl_test_server_worker_t * wrk)
257 vcl_test_server_conn_t *conn;
259 conn = conn_pool_alloc (wrk);
262 vtwrn ("No free connections!");
266 client_fd = vppcom_session_accept (wrk->listen_fd, &conn->endpt, 0);
269 vterr ("vppcom_session_accept()", client_fd);
273 vtinf ("Got a connection -- fd = %d (0x%08x)!", client_fd, client_fd);
275 conn->fd = client_fd;
278 struct epoll_event ev;
282 ev.data.u64 = conn - wrk->conn_pool;
283 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev);
286 vterr ("vppcom_epoll_ctl()", rv);
294 print_usage_and_exit (void)
297 "sock_test_server [OPTIONS] <port>\n"
299 " -h Print this message and exit.\n"
301 " -w <num> Number of workers\n"
302 " -u Use UDP transport layer\n");
307 vcl_test_init_endpoint_addr (vcl_test_server_main_t * ssm)
309 struct sockaddr_storage *servaddr = &ssm->servaddr;
310 memset (servaddr, 0, sizeof (*servaddr));
312 if (ssm->cfg.address_ip6)
314 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
315 server_addr->sin6_family = AF_INET6;
316 server_addr->sin6_addr = in6addr_any;
317 server_addr->sin6_port = htons (ssm->cfg.port);
321 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
322 server_addr->sin_family = AF_INET;
323 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
324 server_addr->sin_port = htons (ssm->cfg.port);
327 if (ssm->cfg.address_ip6)
329 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
330 ssm->cfg.endpt.is_ip4 = 0;
331 ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr;
332 ssm->cfg.endpt.port = (uint16_t) server_addr->sin6_port;
336 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
337 ssm->cfg.endpt.is_ip4 = 1;
338 ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr;
339 ssm->cfg.endpt.port = (uint16_t) server_addr->sin_port;
344 vcl_test_server_process_opts (vcl_test_server_main_t * ssm, int argc,
349 ssm->cfg.proto = VPPCOM_PROTO_TCP;
352 while ((c = getopt (argc, argv, "6Dw:")) != -1)
356 ssm->cfg.address_ip6 = 1;
360 ssm->cfg.proto = VPPCOM_PROTO_UDP;
366 ssm->cfg.workers = v;
368 vtwrn ("Invalid number of workers %d", v);
375 if (isprint (optopt))
376 vtwrn ("Unknown option `-%c'.", optopt);
378 vtwrn ("Unknown option character `\\x%x'.", optopt);
383 print_usage_and_exit ();
386 if (argc < (optind + 1))
388 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
389 print_usage_and_exit ();
392 if (sscanf (argv[optind], "%d", &v) == 1)
393 ssm->cfg.port = (uint16_t) v;
396 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
397 print_usage_and_exit ();
400 vcl_test_init_endpoint_addr (ssm);
404 vts_handle_cfg (vcl_test_server_worker_t * wrk, sock_test_cfg_t * rx_cfg,
405 vcl_test_server_conn_t * conn, int rx_bytes)
409 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
410 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
413 if (rx_bytes != sizeof (*rx_cfg))
415 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
416 rx_bytes, sizeof (*rx_cfg));
417 conn->cfg.rxbuf_size = 0;
418 conn->cfg.num_writes = 0;
419 if (conn->cfg.verbose)
421 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
422 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
424 vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
425 sizeof (conn->cfg), NULL, conn->cfg.verbose);
429 switch (rx_cfg->test)
431 case SOCK_TEST_TYPE_NONE:
432 case SOCK_TEST_TYPE_ECHO:
433 sync_config_and_reply (conn, rx_cfg);
436 case SOCK_TEST_TYPE_BI:
437 case SOCK_TEST_TYPE_UNI:
438 vts_server_start_stop (wrk, conn, rx_cfg);
441 case SOCK_TEST_TYPE_EXIT:
442 vtinf ("Have a great day conn %d (closing)!", conn->fd);
443 vppcom_session_close (conn->fd);
444 conn_pool_free (conn);
449 vtwrn ("Unknown test type %d", rx_cfg->test);
450 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
458 vts_worker_init (vcl_test_server_worker_t * wrk)
460 vcl_test_server_main_t *ssm = &sock_server_main;
461 struct epoll_event listen_ev;
464 __wrk_index = wrk->wrk_index;
466 vtinf ("Initializing worker ...");
468 conn_pool_expand (wrk, SOCK_SERVER_MAX_TEST_CONN + 1);
470 vppcom_worker_register ();
472 wrk->listen_fd = vppcom_session_create (ssm->cfg.proto,
473 0 /* is_nonblocking */ );
474 if (wrk->listen_fd < 0)
475 vtfail ("vppcom_session_create()", wrk->listen_fd);
477 rv = vppcom_session_bind (wrk->listen_fd, &ssm->cfg.endpt);
479 vtfail ("vppcom_session_bind()", rv);
481 if (!(ssm->cfg.proto == VPPCOM_PROTO_UDP))
483 rv = vppcom_session_listen (wrk->listen_fd, 10);
485 vtfail ("vppcom_session_listen()", rv);
488 wrk->epfd = vppcom_epoll_create ();
490 vtfail ("vppcom_epoll_create()", wrk->epfd);
492 listen_ev.events = EPOLLIN;
493 listen_ev.data.u32 = ~0;
494 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listen_fd,
497 vtfail ("vppcom_epoll_ctl", rv);
499 vtinf ("Waiting for a client to connect on port %d ...", ssm->cfg.port);
503 vts_worker_loop (void *arg)
505 vcl_test_server_main_t *ssm = &sock_server_main;
506 vcl_test_server_worker_t *wrk = arg;
507 vcl_test_server_conn_t *conn;
508 int i, rx_bytes, num_ev;
509 sock_test_cfg_t *rx_cfg;
512 vts_worker_init (wrk);
516 num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events,
517 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000.0);
520 vterr ("vppcom_epoll_wait()", num_ev);
523 else if (num_ev == 0)
525 vtinf ("vppcom_epoll_wait() timeout!");
528 for (i = 0; i < num_ev; i++)
530 conn = &wrk->conn_pool[wrk->wait_events[i].data.u32];
531 if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
533 vppcom_session_close (conn->fd);
536 if (wrk->wait_events[i].data.u32 == ~0)
538 vts_new_client (wrk);
542 if (EPOLLIN & wrk->wait_events[i].events)
544 rx_bytes = vcl_test_read (conn->fd, conn->buf,
545 conn->buf_size, &conn->stats);
549 if (errno == ECONNRESET)
551 vtinf ("Connection reset by remote peer.\n");
558 rx_cfg = (sock_test_cfg_t *) conn->buf;
559 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
561 vts_handle_cfg (wrk, rx_cfg, conn, rx_bytes);
564 vtinf ("All client connections closed\n");
565 vtinf ("May the force be with you!\n");
570 else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI)
571 || (conn->cfg.test == SOCK_TEST_TYPE_BI))
573 vts_server_rx (conn, rx_bytes);
576 else if (isascii (conn->buf[0]))
578 vts_server_echo (conn, rx_bytes);
582 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
587 vtwrn ("Unhandled event");
594 ssm->worker_fails -= 1;
597 vppcom_session_close (wrk->listen_fd);
599 free (wrk->conn_pool);
600 ssm->active_workers -= 1;
605 main (int argc, char **argv)
607 vcl_test_server_main_t *vsm = &sock_server_main;
610 clib_mem_init_thread_safe (0, 64 << 20);
611 vsm->cfg.port = SOCK_TEST_SERVER_PORT;
612 vsm->cfg.workers = 1;
613 vsm->active_workers = 1;
614 vcl_test_server_process_opts (vsm, argc, argv);
616 rv = vppcom_app_create ("vcl_test_server");
618 vtfail ("vppcom_app_create()", rv);
620 vsm->workers = calloc (vsm->cfg.workers, sizeof (*vsm->workers));
621 vts_worker_init (&vsm->workers[0]);
622 for (i = 1; i < vsm->cfg.workers; i++)
624 vsm->workers[i].wrk_index = i;
625 rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
626 vts_worker_loop, (void *) &vsm->workers[i]);
628 vts_worker_loop (&vsm->workers[0]);
630 while (vsm->active_workers > 0)
633 vppcom_app_destroy ();
636 return vsm->worker_fails;
640 * fd.io coding-style-patch-verification: ON
643 * eval: (c-set-style "gnu")