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>
36 sock_test_stats_t stats;
45 uint32_t transport_udp;
48 #define SOCK_SERVER_MAX_TEST_CONN 10
49 #define SOCK_SERVER_MAX_EPOLL_EVENTS 10
53 sock_server_cfg_t cfg;
55 struct epoll_event listen_ev;
56 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
58 size_t conn_pool_size;
59 sock_server_conn_t *conn_pool;
63 struct timeval timeout;
66 sock_server_main_t sock_server_main;
69 conn_pool_expand (size_t expand_size)
71 sock_server_main_t *ssm = &sock_server_main;
72 sock_server_conn_t *conn_pool;
73 size_t new_size = ssm->conn_pool_size + expand_size;
76 conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
79 for (i = ssm->conn_pool_size; i < new_size; i++)
81 sock_server_conn_t *conn = &conn_pool[i];
82 memset (conn, 0, sizeof (*conn));
83 sock_test_cfg_init (&conn->cfg);
84 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
85 &conn->buf, &conn->buf_size);
86 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
89 ssm->conn_pool = conn_pool;
90 ssm->conn_pool_size = new_size;
94 int errno_val = errno;
95 perror ("ERROR in conn_pool_expand()");
96 fprintf (stderr, "SERVER: ERROR: Memory allocation "
97 "failed (errno = %d)!\n", errno_val);
101 static inline sock_server_conn_t *
102 conn_pool_alloc (void)
104 sock_server_main_t *ssm = &sock_server_main;
107 for (i = 0; i < ssm->conn_pool_size; i++)
109 if (!ssm->conn_pool[i].is_alloc)
111 ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip;
112 ssm->conn_pool[i].is_alloc = 1;
113 return (&ssm->conn_pool[i]);
121 conn_pool_free (sock_server_conn_t * conn)
128 sync_config_and_reply (sock_server_conn_t * conn, sock_test_cfg_t * rx_cfg)
131 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
132 &conn->buf, &conn->buf_size);
133 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
135 if (conn->cfg.verbose)
137 printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd);
138 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
140 (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
141 sizeof (conn->cfg), NULL, conn->cfg.verbose);
145 stream_test_server_start_stop (sock_server_conn_t * conn,
146 sock_test_cfg_t * rx_cfg)
148 sock_server_main_t *ssm = &sock_server_main;
149 int client_fd = conn->fd;
150 sock_test_t test = rx_cfg->test;
152 if (rx_cfg->ctrl_handle == conn->fd)
155 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
157 for (i = 0; i < ssm->conn_pool_size; i++)
159 sock_server_conn_t *tc = &ssm->conn_pool[i];
161 if (tc->cfg.ctrl_handle == conn->fd)
163 sock_test_stats_accumulate (&conn->stats, &tc->stats);
165 if (conn->cfg.verbose)
169 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
170 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
171 test == SOCK_TEST_TYPE_BI
178 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
179 (test == SOCK_TEST_TYPE_BI) /* show_tx */ ,
181 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
182 if (conn->cfg.verbose)
184 printf (" sock server main\n"
185 SOCK_TEST_SEPARATOR_STRING
187 " buf size: %u (0x%08x)\n"
188 SOCK_TEST_SEPARATOR_STRING,
189 conn->buf, conn->buf_size, conn->buf_size);
192 sync_config_and_reply (conn, rx_cfg);
193 printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
194 SOCK_TEST_BANNER_STRING "\n", conn->fd,
195 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
199 printf ("\n" SOCK_TEST_BANNER_STRING
200 "SERVER (fd %d): %s-directional Stream Test!\n"
201 " Sending client the test cfg to start streaming data...\n",
202 client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
204 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
207 sync_config_and_reply (conn, rx_cfg);
209 /* read the 1st chunk, record start time */
210 memset (&conn->stats, 0, sizeof (conn->stats));
211 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
217 stream_test_server (sock_server_conn_t * conn, int rx_bytes)
219 int client_fd = conn->fd;
220 sock_test_t test = conn->cfg.test;
222 if (test == SOCK_TEST_TYPE_BI)
223 (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
226 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
228 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
235 sock_server_main_t *ssm = &sock_server_main;
237 sock_server_conn_t *conn;
239 if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
240 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
242 conn = conn_pool_alloc ();
245 fprintf (stderr, "\nSERVER: ERROR: No free connections!\n");
249 client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, 0);
253 errno_val = errno = -client_fd;
254 perror ("ERROR in new_client()");
255 fprintf (stderr, "SERVER: ERROR: accept failed "
256 "(errno = %d)!\n", errno_val);
260 printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n",
261 client_fd, client_fd);
263 conn->fd = client_fd;
266 struct epoll_event ev;
270 ev.data.u64 = conn - ssm->conn_pool;
271 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
275 errno_val = errno = -rv;
276 perror ("ERROR in new_client()");
277 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed (errno = %d)!\n",
286 print_usage_and_exit (void)
289 "sock_test_server [OPTIONS] <port>\n"
291 " -h Print this message and exit.\n"
293 " -u Use UDP transport layer\n");
298 main (int argc, char **argv)
300 sock_server_main_t *ssm = &sock_server_main;
301 int client_fd, rv, main_rv = 0;
302 int tx_bytes, rx_bytes, nbytes;
303 sock_server_conn_t *conn;
304 sock_test_cfg_t *rx_cfg;
306 uint64_t xtra_bytes = 0;
307 struct sockaddr_storage servaddr;
310 uint16_t port = SOCK_TEST_SERVER_PORT;
311 vppcom_endpt_t endpt;
314 while ((c = getopt (argc, argv, "6D")) != -1)
318 ssm->cfg.address_ip6 = 1;
322 ssm->cfg.transport_udp = 1;
329 if (isprint (optopt))
330 fprintf (stderr, "SERVER: ERROR: Unknown "
331 "option `-%c'.\n", optopt);
333 fprintf (stderr, "SERVER: ERROR: Unknown "
334 "option character `\\x%x'.\n", optopt);
339 print_usage_and_exit ();
342 if (argc < (optind + 1))
344 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
345 print_usage_and_exit ();
348 if (sscanf (argv[optind], "%d", &v) == 1)
352 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
353 print_usage_and_exit ();
356 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
358 rv = vppcom_app_create ("vcl_test_server");
361 errno_val = errno = -rv;
362 perror ("ERROR in main()");
363 fprintf (stderr, "SERVER: ERROR: vppcom_app_create() failed "
364 "(errno = %d)!\n", errno_val);
369 ssm->listen_fd = vppcom_session_create (ssm->cfg.transport_udp ?
372 0 /* is_nonblocking */ );
374 if (ssm->listen_fd < 0)
376 errno_val = errno = -ssm->listen_fd;
377 perror ("ERROR in main()");
378 fprintf (stderr, "SERVER: ERROR: vppcom_session_create() failed "
379 "(errno = %d)!\n", errno_val);
383 memset (&servaddr, 0, sizeof (servaddr));
385 if (ssm->cfg.address_ip6)
387 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
388 server_addr->sin6_family = AF_INET6;
389 server_addr->sin6_addr = in6addr_any;
390 server_addr->sin6_port = htons (port);
394 struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
395 server_addr->sin_family = AF_INET;
396 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
397 server_addr->sin_port = htons (port);
400 if (ssm->cfg.address_ip6)
402 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
404 endpt.ip = (uint8_t *) & server_addr->sin6_addr;
405 endpt.port = (uint16_t) server_addr->sin6_port;
409 struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
411 endpt.ip = (uint8_t *) & server_addr->sin_addr;
412 endpt.port = (uint16_t) server_addr->sin_port;
415 rv = vppcom_session_bind (ssm->listen_fd, &endpt);
418 errno_val = errno = -rv;
419 perror ("ERROR in main()");
420 fprintf (stderr, "SERVER: ERROR: bind failed (errno = %d)!\n",
425 rv = vppcom_session_listen (ssm->listen_fd, 10);
428 errno_val = errno = -rv;
429 perror ("ERROR in main()");
430 fprintf (stderr, "SERVER: ERROR: listen failed "
431 "(errno = %d)!\n", errno_val);
435 ssm->epfd = vppcom_epoll_create ();
438 errno_val = errno = -ssm->epfd;
439 perror ("ERROR in main()");
440 fprintf (stderr, "SERVER: ERROR: epoll_create failed (errno = %d)!\n",
445 ssm->listen_ev.events = EPOLLIN;
446 ssm->listen_ev.data.u32 = ~0;
447 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd,
451 errno_val = errno = -rv;
452 perror ("ERROR in main()");
453 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed "
454 "(errno = %d)!\n", errno_val);
457 printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
462 num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events,
463 SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0);
467 perror ("epoll_wait()");
468 fprintf (stderr, "\nSERVER: ERROR: epoll_wait() "
469 "failed -- aborting!\n");
473 else if (num_ev == 0)
475 fprintf (stderr, "\nSERVER: epoll_wait() timeout!\n");
478 for (i = 0; i < num_ev; i++)
480 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
481 if (ssm->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
483 vppcom_session_close (conn->fd);
486 if (ssm->wait_events[i].data.u32 == ~0)
491 client_fd = conn->fd;
493 if (EPOLLIN & ssm->wait_events[i].events)
495 rx_bytes = vcl_test_read (client_fd, conn->buf,
496 conn->buf_size, &conn->stats);
499 rx_cfg = (sock_test_cfg_t *) conn->buf;
500 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
504 printf ("SERVER (fd %d): Received a cfg message!\n",
506 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
509 if (rx_bytes != sizeof (*rx_cfg))
511 printf ("SERVER (fd %d): Invalid cfg message "
512 "size (%d)!\n Should be %lu bytes.\n",
513 client_fd, rx_bytes, sizeof (*rx_cfg));
514 conn->cfg.rxbuf_size = 0;
515 conn->cfg.num_writes = 0;
516 if (conn->cfg.verbose)
518 printf ("SERVER (fd %d): Replying to "
519 "cfg message!\n", client_fd);
520 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
522 vcl_test_write (client_fd, (uint8_t *) & conn->cfg,
523 sizeof (conn->cfg), NULL,
528 switch (rx_cfg->test)
530 case SOCK_TEST_TYPE_NONE:
531 case SOCK_TEST_TYPE_ECHO:
532 sync_config_and_reply (conn, rx_cfg);
535 case SOCK_TEST_TYPE_BI:
536 case SOCK_TEST_TYPE_UNI:
537 stream_test_server_start_stop (conn, rx_cfg);
540 case SOCK_TEST_TYPE_EXIT:
541 printf ("SERVER: Have a great day, "
542 "connection %d!\n", client_fd);
543 vppcom_session_close (client_fd);
544 conn_pool_free (conn);
545 printf ("SERVER: Closed client fd %d\n", client_fd);
549 printf ("SERVER: All client connections "
550 "closed.\n\nSERVER: "
551 "May the force be with you!\n\n");
558 "SERVER: ERROR: Unknown test type!\n");
559 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
565 else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) ||
566 (conn->cfg.test == SOCK_TEST_TYPE_BI))
568 stream_test_server (conn, rx_bytes);
572 else if (isascii (conn->buf[0]))
574 /* If it looks vaguely like a string,
575 * make sure it's terminated.
577 ((char *) conn->buf)[rx_bytes <
578 conn->buf_size ? rx_bytes :
579 conn->buf_size - 1] = 0;
580 printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
581 conn->fd, rx_bytes, conn->buf);
586 if (errno == ECONNRESET)
588 printf ("\nSERVER: Connection reset by remote peer.\n"
589 " Y'all have a great day now!\n\n");
596 if (isascii (conn->buf[0]))
598 /* If it looks vaguely like a string,
599 * make sure it's terminated
601 ((char *) conn->buf)[rx_bytes <
602 conn->buf_size ? rx_bytes :
603 conn->buf_size - 1] = 0;
605 fprintf (stderr, "SERVER: ERROR: "
606 "FIFO not drained in previous test!\n"
607 " extra chunks %u (0x%x)\n"
608 " extra bytes %lu (0x%lx)\n",
609 xtra, xtra, xtra_bytes, xtra_bytes);
614 if (conn->cfg.verbose)
615 printf ("SERVER (fd %d): Echoing back\n", client_fd);
617 nbytes = strlen ((const char *) conn->buf) + 1;
619 tx_bytes = vcl_test_write (client_fd, conn->buf,
620 nbytes, &conn->stats,
623 printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
624 conn->fd, tx_bytes, conn->buf);
627 else // Extraneous read data from non-echo tests???
630 xtra_bytes += rx_bytes;
637 vppcom_session_close (ssm->listen_fd);
638 vppcom_app_destroy ();
641 free (ssm->conn_pool);
647 * fd.io coding-style-patch-verification: ON
650 * eval: (c-set-style "gnu")