2 * Copyright (c) 2017-2019 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>
24 #include <hs_apps/vcl/sock_test.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
41 vcl_test_stats_t stats;
48 uint32_t transport_udp;
54 sock_server_cfg_t cfg;
56 struct epoll_event listen_ev;
57 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
58 int af_unix_listen_fd;
60 struct epoll_event af_unix_listen_ev;
61 struct sockaddr_un serveraddr;
62 uint32_t af_unix_xacts;
64 size_t conn_pool_size;
65 sock_server_conn_t *conn_pool;
69 struct timeval timeout;
72 sock_server_main_t sock_server_main;
75 conn_pool_expand (size_t expand_size)
77 sock_server_main_t *ssm = &sock_server_main;
78 sock_server_conn_t *conn_pool;
79 size_t new_size = ssm->conn_pool_size + expand_size;
82 conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
84 stfail ("conn_pool_expand()");
86 for (i = ssm->conn_pool_size; i < new_size; i++)
88 sock_server_conn_t *conn = &conn_pool[i];
89 memset (conn, 0, sizeof (*conn));
90 hs_test_cfg_init (&conn->cfg);
91 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , &conn->buf,
93 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
96 ssm->conn_pool = conn_pool;
97 ssm->conn_pool_size = new_size;
100 static inline sock_server_conn_t *
101 conn_pool_alloc (void)
103 sock_server_main_t *ssm = &sock_server_main;
106 for (i = 0; i < ssm->conn_pool_size; i++)
108 if (!ssm->conn_pool[i].is_alloc)
110 ssm->conn_pool[i].is_alloc = 1;
111 return (&ssm->conn_pool[i]);
119 conn_pool_free (sock_server_conn_t * conn)
126 sync_config_and_reply (sock_server_conn_t *conn, hs_test_cfg_t *rx_cfg)
129 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
130 &conn->buf, &conn->buf_size);
131 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
133 if (conn->cfg.verbose)
135 stinf ("(fd %d): Replying to cfg message!\n", conn->fd);
136 hs_test_cfg_dump (&conn->cfg, 0 /* is_client */);
138 (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
139 sizeof (conn->cfg), NULL, conn->cfg.verbose);
143 stream_test_server_start_stop (sock_server_conn_t *conn, hs_test_cfg_t *rx_cfg)
145 sock_server_main_t *ssm = &sock_server_main;
146 int client_fd = conn->fd;
147 hs_test_t test = rx_cfg->test;
149 if (rx_cfg->ctrl_handle == conn->fd)
152 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
154 for (i = 0; i < ssm->conn_pool_size; i++)
156 sock_server_conn_t *tc = &ssm->conn_pool[i];
158 if (tc->cfg.ctrl_handle == conn->fd)
160 vcl_test_stats_accumulate (&conn->stats, &tc->stats);
162 if (conn->cfg.verbose)
166 snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS",
168 vcl_test_stats_dump (buf, &tc->stats, 1 /* show_rx */,
169 test == HS_TEST_TYPE_BI
176 vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */,
177 (test == HS_TEST_TYPE_BI) /* show_tx */,
179 hs_test_cfg_dump (&conn->cfg, 0 /* is_client */);
180 if (conn->cfg.verbose)
182 stinf (" sock server main\n" HS_TEST_SEPARATOR_STRING
184 " buf size: %u (0x%08x)\n" HS_TEST_SEPARATOR_STRING,
185 conn->buf, conn->buf_size, conn->buf_size);
188 sync_config_and_reply (conn, rx_cfg);
189 stinf ("SERVER (fd %d): %s-directional Stream Test "
190 "Complete!\n" SOCK_TEST_BANNER_STRING "\n",
191 conn->fd, test == HS_TEST_TYPE_BI ? "Bi" : "Uni");
195 stinf (SOCK_TEST_BANNER_STRING
196 "SERVER (fd %d): %s-directional Stream Test!\n"
197 " Sending client the test cfg to start streaming data...\n",
198 client_fd, test == HS_TEST_TYPE_BI ? "Bi" : "Uni");
200 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
203 sync_config_and_reply (conn, rx_cfg);
205 /* read the 1st chunk, record start time */
206 memset (&conn->stats, 0, sizeof (conn->stats));
207 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
213 stream_test_server (sock_server_conn_t * conn, int rx_bytes)
215 int client_fd = conn->fd;
216 hs_test_t test = conn->cfg.test;
218 if (test == HS_TEST_TYPE_BI)
219 (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
222 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
224 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
231 sock_server_main_t *ssm = &sock_server_main;
232 int af_unix_client_fd, rv;
234 size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1;
236 af_unix_client_fd = accept (ssm->af_unix_listen_fd,
237 (struct sockaddr *) NULL, NULL);
238 if (af_unix_client_fd < 0)
239 stfail ("af_unix_echo accept()");
241 stinf ("Got an AF_UNIX connection -- fd = %d (0x%08x)!",
242 af_unix_client_fd, af_unix_client_fd);
244 memset (buffer, 0, sizeof (buffer));
246 rv = read (af_unix_client_fd, buffer, nbytes);
248 stfail ("af_unix_echo read()");
250 /* Make the buffer is NULL-terminated. */
251 buffer[sizeof (buffer) - 1] = 0;
252 stinf ("(AF_UNIX): RX (%d bytes) - '%s'", rv, buffer);
254 if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes))
256 rv = write (af_unix_client_fd, buffer, nbytes);
258 stfail ("af_unix_echo write()");
259 stinf ("(AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
260 ssm->af_unix_xacts++;
262 close (af_unix_client_fd);
268 sock_server_main_t *ssm = &sock_server_main;
270 sock_server_conn_t *conn;
272 if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
273 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
275 conn = conn_pool_alloc ();
277 stfail ("No free connections!");
279 client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
281 stfail ("new_client accept()");
283 stinf ("Got a connection -- fd = %d (0x%08x)!\n", client_fd, client_fd);
284 if (fcntl (client_fd, F_SETFL, O_NONBLOCK) < 0)
287 conn->fd = client_fd;
289 struct epoll_event ev;
292 ev.events = EPOLLET | EPOLLIN;
293 ev.data.u64 = conn - ssm->conn_pool;
294 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
297 stfail ("new_client epoll_ctl()");
303 socket_server_echo_af_unix_init (sock_server_main_t * ssm)
307 if (ssm->af_unix_listen_fd > 0)
310 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
311 ssm->af_unix_listen_fd = socket (AF_UNIX, SOCK_STREAM, 0);
312 if (ssm->af_unix_listen_fd < 0)
313 stfail ("echo_af_unix_init socket()");
315 memset (&ssm->serveraddr, 0, sizeof (ssm->serveraddr));
316 ssm->serveraddr.sun_family = AF_UNIX;
317 strncpy (ssm->serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME,
318 sizeof (ssm->serveraddr.sun_path));
320 rv = bind (ssm->af_unix_listen_fd, (struct sockaddr *) &ssm->serveraddr,
321 SUN_LEN (&ssm->serveraddr));
323 stfail ("echo_af_unix_init bind()");
325 rv = listen (ssm->af_unix_listen_fd, 10);
327 stfail ("echo_af_unix_init listen()");
329 ssm->af_unix_listen_ev.events = EPOLLET | EPOLLIN;
330 ssm->af_unix_listen_ev.data.u32 = SOCK_TEST_AF_UNIX_ACCEPT_DATA;
331 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->af_unix_listen_fd,
332 &ssm->af_unix_listen_ev);
334 stfail ("echo_af_unix_init epoll_ctl()");
340 print_usage_and_exit (void)
343 "sock_test_server [OPTIONS] <port>\n"
345 " -h Print this message and exit.\n"
347 " -u Use UDP transport layer\n");
353 sts_server_echo (sock_server_conn_t * conn, int rx_bytes)
355 int tx_bytes, nbytes, pos;
357 /* If it looks vaguely like a string make sure it's terminated */
358 pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
359 ((char *) conn->buf)[pos] = 0;
361 if (conn->cfg.verbose)
362 stinf ("(fd %d): Echoing back\n", conn->fd);
364 nbytes = strlen ((const char *) conn->buf) + 1;
366 tx_bytes = sock_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
369 stinf ("(fd %d): TX (%d bytes) - '%s'\n", conn->fd, tx_bytes, conn->buf);
373 sts_handle_cfg (hs_test_cfg_t *rx_cfg, sock_server_conn_t *conn, int rx_bytes)
375 sock_server_main_t *ssm = &sock_server_main;
379 stinf ("(fd %d): Received a cfg message!\n", conn->fd);
380 hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
383 if (rx_bytes != sizeof (*rx_cfg))
385 stinf ("(fd %d): Invalid cfg message size (%d) expected %lu!", conn->fd,
386 rx_bytes, sizeof (*rx_cfg));
387 conn->cfg.rxbuf_size = 0;
388 conn->cfg.num_writes = 0;
389 if (conn->cfg.verbose)
391 stinf ("(fd %d): Replying to cfg message!\n", conn->fd);
392 hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
394 sock_test_write (conn->fd, (uint8_t *) & conn->cfg, sizeof (conn->cfg),
395 NULL, conn->cfg.verbose);
399 switch (rx_cfg->test)
401 case HS_TEST_TYPE_NONE:
402 sync_config_and_reply (conn, rx_cfg);
405 case HS_TEST_TYPE_ECHO:
406 if (socket_server_echo_af_unix_init (ssm))
409 sync_config_and_reply (conn, rx_cfg);
412 case HS_TEST_TYPE_BI:
413 case HS_TEST_TYPE_UNI:
414 stream_test_server_start_stop (conn, rx_cfg);
417 case HS_TEST_TYPE_EXIT:
418 stinf ("Have a great day connection %d!", conn->fd);
420 conn_pool_free (conn);
421 stinf ("Closed client fd %d", conn->fd);
426 stinf ("ERROR: Unknown test type!\n");
427 hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
436 sts_conn_expect_config (sock_server_conn_t * conn)
438 if (conn->cfg.test == HS_TEST_TYPE_ECHO)
441 return (conn->stats.rx_bytes < 128
442 || conn->stats.rx_bytes > conn->cfg.total_bytes);
446 main (int argc, char **argv)
448 int client_fd, rv, main_rv = 0, rx_bytes, c, v, i;
449 sock_server_main_t *ssm = &sock_server_main;
450 sock_server_conn_t *conn;
451 hs_test_cfg_t *rx_cfg;
452 struct sockaddr_storage servaddr;
453 uint16_t port = VCL_TEST_SERVER_PORT;
454 uint32_t servaddr_size;
457 while ((c = getopt (argc, argv, "6D")) != -1)
461 ssm->cfg.address_ip6 = 1;
465 ssm->cfg.transport_udp = 1;
472 if (isprint (optopt))
473 stinf ("ERROR: Unknown option `-%c'", optopt);
475 stinf ("ERROR: Unknown option character `\\x%x'.\n", optopt);
480 print_usage_and_exit ();
483 if (argc < (optind + 1))
485 stinf ("ERROR: Insufficient number of arguments!\n");
486 print_usage_and_exit ();
489 if (sscanf (argv[optind], "%d", &v) == 1)
493 stinf ("ERROR: Invalid port (%s)!\n", argv[optind]);
494 print_usage_and_exit ();
497 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
499 ssm->listen_fd = socket (ssm->cfg.address_ip6 ? AF_INET6 : AF_INET,
500 ssm->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM,
503 if (ssm->listen_fd < 0)
504 stfail ("main listen()");
506 memset (&servaddr, 0, sizeof (servaddr));
508 if (ssm->cfg.address_ip6)
510 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
511 servaddr_size = sizeof (*server_addr);
512 server_addr->sin6_family = AF_INET6;
513 server_addr->sin6_addr = in6addr_any;
514 server_addr->sin6_port = htons (port);
518 struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
519 servaddr_size = sizeof (*server_addr);
520 server_addr->sin_family = AF_INET;
521 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
522 server_addr->sin_port = htons (port);
525 rv = bind (ssm->listen_fd, (struct sockaddr *) &servaddr, servaddr_size);
527 stfail ("main bind()");
529 rv = fcntl (ssm->listen_fd, F_SETFL, O_NONBLOCK);
531 stfail ("main fcntl()");
533 rv = listen (ssm->listen_fd, 10);
535 stfail ("main listen()");
537 ssm->epfd = epoll_create (1);
539 stfail ("main epoll_create()");
541 ssm->listen_ev.events = EPOLLET | EPOLLIN;
542 ssm->listen_ev.data.u32 = ~0;
544 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
546 stfail ("main epoll_ctl()");
548 stinf ("Waiting for a client to connect on port %d...\n", port);
553 num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
554 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
556 stfail ("main epoll_wait()");
560 stinf ("epoll_wait() timeout!\n");
563 for (i = 0; i < num_ev; i++)
565 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
566 if (ssm->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
571 if (ssm->wait_events[i].data.u32 == ~0)
576 else if (ssm->wait_events[i].data.u32 ==
577 SOCK_TEST_AF_UNIX_ACCEPT_DATA)
582 client_fd = conn->fd;
584 if (EPOLLIN & ssm->wait_events[i].events)
587 rx_bytes = sock_test_read (client_fd, conn->buf,
588 conn->buf_size, &conn->stats);
592 if (errno == ECONNRESET)
594 stinf ("Connection reset by peer\n");
602 if (sts_conn_expect_config (conn))
604 rx_cfg = (hs_test_cfg_t *) conn->buf;
605 if (rx_cfg->magic == HS_TEST_CFG_CTRL_MAGIC)
607 sts_handle_cfg (rx_cfg, conn, rx_bytes);
610 stinf ("All client connections closed.\n\nSERVER: "
611 "May the force be with you!\n\n");
618 if ((conn->cfg.test == HS_TEST_TYPE_UNI) ||
619 (conn->cfg.test == HS_TEST_TYPE_BI))
621 stream_test_server (conn, rx_bytes);
622 if (ioctl (conn->fd, FIONREAD))
626 else if (isascii (conn->buf[0]))
628 sts_server_echo (conn, rx_bytes);
632 stwrn ("FIFO not drained! extra bytes %d", rx_bytes);
639 close (ssm->listen_fd);
640 close (ssm->af_unix_listen_fd);
641 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
644 free (ssm->conn_pool);
650 * fd.io coding-style-patch-verification: ON
653 * eval: (c-set-style "gnu")