blob: 1280429f0f6bce5932b2ebca7cf4537dcd80f032 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
2 * Copyright (c) 2017 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <unistd.h>
17#include <errno.h>
18#include <sys/types.h>
19#include <sys/socket.h>
20#include <stdio.h>
21#include <string.h>
22#include <time.h>
23#include <ctype.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040024#include <vcl/sock_test.h>
Dave Wallace35830af2017-10-09 01:43:42 -040025#include <sys/stat.h>
26#include <fcntl.h>
Dave Wallace543852a2017-08-03 02:11:34 -040027
Dave Wallacef7f809c2017-10-03 01:48:42 -040028#define SOCK_SERVER_USE_EPOLL 1
Dave Wallace35830af2017-10-09 01:43:42 -040029#define VPPCOM_SESSION_ATTR_UNIT_TEST 0
Dave Wallacef7f809c2017-10-03 01:48:42 -040030
31#if SOCK_SERVER_USE_EPOLL
32#include <sys/epoll.h>
Dave Wallace3ee1fe12018-02-23 01:09:11 -050033#if !defined(VCL_TEST)
34#include <sys/un.h>
35#endif
Dave Wallacef7f809c2017-10-03 01:48:42 -040036#endif
37
Dave Wallace35830af2017-10-09 01:43:42 -040038#ifdef VCL_TEST
39#if VPPCOM_SESSION_ATTR_UNIT_TEST
40#define BUFLEN sizeof (uint64_t) * 16
41uint64_t buffer[16];
42uint32_t buflen = BUFLEN;
43uint32_t *flags = (uint32_t *) buffer;
44#endif
45#endif
46
Dave Wallace543852a2017-08-03 02:11:34 -040047typedef struct
48{
49 uint8_t is_alloc;
50 int fd;
51 uint8_t *buf;
52 uint32_t buf_size;
53 sock_test_cfg_t cfg;
54 sock_test_stats_t stats;
55#ifdef VCL_TEST
56 vppcom_endpt_t endpt;
57 uint8_t ip[16];
58#endif
59} sock_server_conn_t;
60
Dave Wallacede910062018-03-20 09:22:13 -040061typedef struct
62{
63 uint32_t port;
64 uint32_t address_ip6;
65 uint32_t transport_udp;
66} sock_server_cfg_t;
67
Dave Wallace543852a2017-08-03 02:11:34 -040068#define SOCK_SERVER_MAX_TEST_CONN 10
Dave Wallacef7f809c2017-10-03 01:48:42 -040069#define SOCK_SERVER_MAX_EPOLL_EVENTS 10
Dave Wallace543852a2017-08-03 02:11:34 -040070typedef struct
71{
72 int listen_fd;
Dave Wallacede910062018-03-20 09:22:13 -040073 sock_server_cfg_t cfg;
Dave Wallacef7f809c2017-10-03 01:48:42 -040074#if SOCK_SERVER_USE_EPOLL
75 int epfd;
76 struct epoll_event listen_ev;
77 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
Dave Wallace3ee1fe12018-02-23 01:09:11 -050078#if !defined (VCL_TEST)
79 int af_unix_listen_fd;
80 int af_unix_fd;
81 struct epoll_event af_unix_listen_ev;
82 struct sockaddr_un serveraddr;
83 uint32_t af_unix_xacts;
84#endif
Dave Wallacef7f809c2017-10-03 01:48:42 -040085#endif
Dave Wallace543852a2017-08-03 02:11:34 -040086 size_t num_conn;
87 size_t conn_pool_size;
88 sock_server_conn_t *conn_pool;
89 int nfds;
90 fd_set rd_fdset;
91 fd_set wr_fdset;
92 struct timeval timeout;
93} sock_server_main_t;
94
95sock_server_main_t sock_server_main;
96
Dave Wallacef7f809c2017-10-03 01:48:42 -040097#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -040098static inline int
99get_nfds (void)
100{
101 sock_server_main_t *ssm = &sock_server_main;
102 int i, nfds;
103
104 for (nfds = i = 0; i < FD_SETSIZE; i++)
105 {
106 if (FD_ISSET (i, &ssm->rd_fdset) || FD_ISSET (i, &ssm->wr_fdset))
107 nfds = i + 1;
108 }
109 return nfds;
110}
111
112static inline void
113conn_fdset_set (sock_server_conn_t * conn, fd_set * fdset)
114{
115 sock_server_main_t *ssm = &sock_server_main;
116
117 FD_SET (conn->fd, fdset);
118 ssm->nfds = get_nfds ();
119}
120
121static inline void
122conn_fdset_clr (sock_server_conn_t * conn, fd_set * fdset)
123{
124 sock_server_main_t *ssm = &sock_server_main;
125
126 FD_CLR (conn->fd, fdset);
127 ssm->nfds = get_nfds ();
128}
Dave Wallacef7f809c2017-10-03 01:48:42 -0400129#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400130
131static inline void
132conn_pool_expand (size_t expand_size)
133{
134 sock_server_main_t *ssm = &sock_server_main;
135 sock_server_conn_t *conn_pool;
136 size_t new_size = ssm->conn_pool_size + expand_size;
137 int i;
138
139 conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
140 if (conn_pool)
141 {
142 for (i = ssm->conn_pool_size; i < new_size; i++)
143 {
144 sock_server_conn_t *conn = &conn_pool[i];
145 memset (conn, 0, sizeof (*conn));
146 sock_test_cfg_init (&conn->cfg);
147 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
148 &conn->buf, &conn->buf_size);
149 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
150 }
151
152 ssm->conn_pool = conn_pool;
153 ssm->conn_pool_size = new_size;
154 }
155 else
156 {
157 int errno_val = errno;
158 perror ("ERROR in conn_pool_expand()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500159 fprintf (stderr, "SERVER: ERROR: Memory allocation "
160 "failed (errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400161 }
162}
163
164static inline sock_server_conn_t *
165conn_pool_alloc (void)
166{
167 sock_server_main_t *ssm = &sock_server_main;
168 int i;
169
170 for (i = 0; i < ssm->conn_pool_size; i++)
171 {
172 if (!ssm->conn_pool[i].is_alloc)
173 {
174#ifdef VCL_TEST
175 ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip;
176#endif
177 ssm->conn_pool[i].is_alloc = 1;
178 return (&ssm->conn_pool[i]);
179 }
180 }
181
182 return 0;
183}
184
185static inline void
186conn_pool_free (sock_server_conn_t * conn)
187{
Dave Wallacef7f809c2017-10-03 01:48:42 -0400188#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400189 sock_server_main_t *ssm = &sock_server_main;
190
191 conn_fdset_clr (conn, &ssm->rd_fdset);
192 conn_fdset_clr (conn, &ssm->wr_fdset);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400193#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400194 conn->fd = 0;
195 conn->is_alloc = 0;
196}
197
198static inline void
199sync_config_and_reply (sock_server_conn_t * conn, sock_test_cfg_t * rx_cfg)
200{
201 conn->cfg = *rx_cfg;
202 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
203 &conn->buf, &conn->buf_size);
204 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
205
206 if (conn->cfg.verbose)
207 {
208 printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd);
209 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
210 }
211 (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
212 sizeof (conn->cfg), NULL, conn->cfg.verbose);
213}
214
215static void
216stream_test_server_start_stop (sock_server_conn_t * conn,
217 sock_test_cfg_t * rx_cfg)
218{
219 sock_server_main_t *ssm = &sock_server_main;
220 int client_fd = conn->fd;
221 sock_test_t test = rx_cfg->test;
222
223 if (rx_cfg->ctrl_handle == conn->fd)
224 {
225 int i;
226 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
227
228 for (i = 0; i < ssm->conn_pool_size; i++)
229 {
230 sock_server_conn_t *tc = &ssm->conn_pool[i];
231
232 if (tc->cfg.ctrl_handle == conn->fd)
233 {
234 sock_test_stats_accumulate (&conn->stats, &tc->stats);
235
236 if (conn->cfg.verbose)
237 {
238 static char buf[64];
239
240 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
241 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
242 test == SOCK_TEST_TYPE_BI
243 /* show tx */ ,
244 conn->cfg.verbose);
245 }
246 }
247 }
248
249 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
250 (test == SOCK_TEST_TYPE_BI) /* show_tx */ ,
251 conn->cfg.verbose);
252 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
253 if (conn->cfg.verbose)
254 {
255 printf (" sock server main\n"
256 SOCK_TEST_SEPARATOR_STRING
257 " buf: %p\n"
258 " buf size: %u (0x%08x)\n"
259 SOCK_TEST_SEPARATOR_STRING,
260 conn->buf, conn->buf_size, conn->buf_size);
261 }
262
263 sync_config_and_reply (conn, rx_cfg);
264 printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
265 SOCK_TEST_BANNER_STRING "\n", conn->fd,
266 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
267 }
268 else
269 {
270 printf ("\n" SOCK_TEST_BANNER_STRING
271 "SERVER (fd %d): %s-directional Stream Test!\n"
272 " Sending client the test cfg to start streaming data...\n",
273 client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
274
275 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
276 rx_cfg->ctrl_handle;
277
278 sync_config_and_reply (conn, rx_cfg);
279
280 /* read the 1st chunk, record start time */
281 memset (&conn->stats, 0, sizeof (conn->stats));
282 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
283 }
284}
285
286
287static inline void
288stream_test_server (sock_server_conn_t * conn, int rx_bytes)
289{
290 int client_fd = conn->fd;
291 sock_test_t test = conn->cfg.test;
292
293 if (test == SOCK_TEST_TYPE_BI)
294 (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
295 conn->cfg.verbose);
296
297 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
298 {
299 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
300 }
301}
302
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500303#if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
304static inline void
305af_unix_echo (void)
306{
307 sock_server_main_t *ssm = &sock_server_main;
308 int af_unix_client_fd;
309 int rv;
310 int errno_val;
311 uint8_t buffer[256];
312 size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1;
313
314#if HAVE_ACCEPT4
315 af_unix_client_fd = accept4 (ssm->af_unix_listen_fd,
316 (struct sockaddr *) NULL, NULL, NULL);
317#else
318 af_unix_client_fd = accept (ssm->af_unix_listen_fd,
319 (struct sockaddr *) NULL, NULL);
320#endif
321 if (af_unix_client_fd < 0)
322 {
323 errno_val = errno;
324 perror ("ERROR in af_unix_accept()");
325 fprintf (stderr, "SERVER: ERROR: accept failed "
326 "(errno = %d)!\n", errno_val);
327 return;
328 }
329
330 printf ("SERVER: Got an AF_UNIX connection -- fd = %d (0x%08x)!\n",
331 af_unix_client_fd, af_unix_client_fd);
332
333 memset (buffer, 0, sizeof (buffer));
334
335 rv = read (af_unix_client_fd, buffer, nbytes);
336 if (rv < 0)
337 {
338 errno_val = errno;
339 perror ("ERROR in af_unix_echo(): read() failed");
340 fprintf (stderr, "SERVER: ERROR: read(af_unix_client_fd %d (0x%x), "
341 "\"%s\", nbytes %lu) failed (errno = %d)!\n",
342 af_unix_client_fd, af_unix_client_fd, buffer, nbytes,
343 errno_val);
344 goto done;
345 }
346
347 printf ("SERVER (AF_UNIX): RX (%d bytes) - '%s'\n", rv, buffer);
348
349 if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes))
350 {
351 rv = write (af_unix_client_fd, buffer, nbytes);
352 if (rv < 0)
353 {
354 errno_val = errno;
355 perror ("ERROR in af_unix_echo(): write() failed");
356 fprintf (stderr,
357 "SERVER: ERROR: write(af_unix_client_fd %d (0x%x), "
358 "\"%s\", nbytes %ld) failed (errno = %d)!\n",
359 af_unix_client_fd, af_unix_client_fd, buffer, nbytes,
360 errno_val);
361 goto done;
362 }
363 printf ("SERVER (AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
364 ssm->af_unix_xacts++;
365 }
366done:
367 close (af_unix_client_fd);
368}
369
370#endif
371
Dave Wallace543852a2017-08-03 02:11:34 -0400372static inline void
373new_client (void)
374{
375 sock_server_main_t *ssm = &sock_server_main;
376 int client_fd;
377 sock_server_conn_t *conn;
378
379 if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
380 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
381
382 conn = conn_pool_alloc ();
383 if (!conn)
384 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500385 fprintf (stderr, "\nSERVER: ERROR: No free connections!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400386 return;
387 }
388
389#ifdef VCL_TEST
Dave Wallace048b1d62018-01-03 22:24:41 -0500390 client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, 0);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400391 if (client_fd < 0)
392 errno = -client_fd;
Dave Wallace59179392017-11-07 02:20:07 -0500393#elif HAVE_ACCEPT4
394 client_fd = accept4 (ssm->listen_fd, (struct sockaddr *) NULL, NULL, NULL);
Dave Wallace543852a2017-08-03 02:11:34 -0400395#else
396 client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
397#endif
398 if (client_fd < 0)
399 {
400 int errno_val;
401 errno_val = errno;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400402 perror ("ERROR in new_client()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500403 fprintf (stderr, "SERVER: ERROR: accept failed "
404 "(errno = %d)!\n", errno_val);
Dave Wallaceee45d412017-11-24 21:44:06 -0500405 return;
Dave Wallace543852a2017-08-03 02:11:34 -0400406 }
407
408 printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n",
409 client_fd, client_fd);
410
411 conn->fd = client_fd;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400412
413#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400414 conn_fdset_set (conn, &ssm->rd_fdset);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400415 ssm->nfds++;
416#else
417 {
418 struct epoll_event ev;
419 int rv;
420
421 ev.events = EPOLLIN;
422 ev.data.u64 = conn - ssm->conn_pool;
423#ifdef VCL_TEST
424 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
425 if (rv)
426 errno = -rv;
427#else
428 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
429#endif
430 if (rv < 0)
431 {
432 int errno_val;
433 errno_val = errno;
434 perror ("ERROR in new_client()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500435 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed (errno = %d)!\n",
Dave Wallacef7f809c2017-10-03 01:48:42 -0400436 errno_val);
437 }
438 else
439 ssm->nfds++;
440 }
441#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400442}
443
Dave Wallacede910062018-03-20 09:22:13 -0400444void
445print_usage_and_exit (void)
446{
447 fprintf (stderr,
448 "sock_test_server [OPTIONS] <port>\n"
449 " OPTIONS\n"
450 " -h Print this message and exit.\n"
451 " -6 Use IPv6\n"
452 " -u Use UDP transport layer\n");
453 exit (1);
454}
455
Dave Wallace543852a2017-08-03 02:11:34 -0400456int
457main (int argc, char **argv)
458{
459 sock_server_main_t *ssm = &sock_server_main;
460 int client_fd, rv, main_rv = 0;
461 int tx_bytes, rx_bytes, nbytes;
462 sock_server_conn_t *conn;
463 sock_test_cfg_t *rx_cfg;
464 uint32_t xtra = 0;
465 uint64_t xtra_bytes = 0;
Dave Wallacede910062018-03-20 09:22:13 -0400466 struct sockaddr_storage servaddr;
Dave Wallace543852a2017-08-03 02:11:34 -0400467 int errno_val;
Dave Wallacede910062018-03-20 09:22:13 -0400468 int c, v, i;
Dave Wallace543852a2017-08-03 02:11:34 -0400469 uint16_t port = SOCK_TEST_SERVER_PORT;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400470#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400471 fd_set _rfdset, *rfdset = &_rfdset;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400472#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400473#ifdef VCL_TEST
474 vppcom_endpt_t endpt;
475#else
Dave Wallacede910062018-03-20 09:22:13 -0400476 uint32_t servaddr_size;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400477#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400478 fd_set _wfdset, *wfdset = &_wfdset;
479#endif
Dave Wallacef7f809c2017-10-03 01:48:42 -0400480#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400481
Dave Wallacede910062018-03-20 09:22:13 -0400482 opterr = 0;
483 while ((c = getopt (argc, argv, "6D")) != -1)
484 switch (c)
485 {
486 case '6':
487 ssm->cfg.address_ip6 = 1;
488 break;
489
490 case 'D':
491 ssm->cfg.transport_udp = 1;
492 break;
493
494 case '?':
495 switch (optopt)
496 {
497 default:
498 if (isprint (optopt))
499 fprintf (stderr, "SERVER: ERROR: Unknown "
500 "option `-%c'.\n", optopt);
501 else
502 fprintf (stderr, "SERVER: ERROR: Unknown "
503 "option character `\\x%x'.\n", optopt);
504 }
505 /* fall thru */
506 case 'h':
507 default:
508 print_usage_and_exit ();
509 }
510
511 if (argc < (optind + 1))
512 {
513 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
514 print_usage_and_exit ();
515 }
516
517 if (sscanf (argv[optind], "%d", &v) == 1)
Dave Wallace543852a2017-08-03 02:11:34 -0400518 port = (uint16_t) v;
Dave Wallacede910062018-03-20 09:22:13 -0400519 else
520 {
521 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
522 print_usage_and_exit ();
523 }
Dave Wallace543852a2017-08-03 02:11:34 -0400524
525 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
526
527#ifdef VCL_TEST
528 rv = vppcom_app_create ("vcl_test_server");
529 if (rv)
530 {
531 errno = -rv;
532 ssm->listen_fd = -1;
533 }
534 else
535 {
Dave Wallacede910062018-03-20 09:22:13 -0400536 ssm->listen_fd = vppcom_session_create (ssm->cfg.transport_udp ?
537 VPPCOM_PROTO_UDP :
538 VPPCOM_PROTO_TCP,
539 0 /* is_nonblocking */ );
Dave Wallace543852a2017-08-03 02:11:34 -0400540 }
541#else
Dave Wallacede910062018-03-20 09:22:13 -0400542 ssm->listen_fd = socket (ssm->cfg.address_ip6 ? AF_INET6 : AF_INET,
543 ssm->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM,
544 0);
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500545#if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
546 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
547 ssm->af_unix_listen_fd = socket (AF_UNIX, SOCK_STREAM, 0);
548 if (ssm->af_unix_listen_fd < 0)
549 {
550 errno_val = errno;
551 perror ("ERROR in main(): socket(AF_UNIX) failed");
552 fprintf (stderr,
553 "SERVER: ERROR: socket(AF_UNIX, SOCK_STREAM, 0) failed "
554 "(errno = %d)!\n", errno_val);
555 return ssm->af_unix_listen_fd;
556 }
557
558 memset (&ssm->serveraddr, 0, sizeof (ssm->serveraddr));
559 ssm->serveraddr.sun_family = AF_UNIX;
560 strcpy (ssm->serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME);
561
562 rv = bind (ssm->af_unix_listen_fd, (struct sockaddr *) &ssm->serveraddr,
563 SUN_LEN (&ssm->serveraddr));
564 if (rv < 0)
565 {
566 errno_val = errno;
567 perror ("ERROR in main(): bind(SOCK_TEST_AF_UNIX_FILENAME) failed");
568 fprintf (stderr, "SERVER: ERROR: bind() fd %d, \"%s\": "
569 "failed (errno = %d)!\n", ssm->af_unix_listen_fd,
570 SOCK_TEST_AF_UNIX_FILENAME, errno_val);
571 close (ssm->af_unix_listen_fd);
572 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
573 return rv;
574 }
575
576 rv = listen (ssm->af_unix_listen_fd, 10);
577 if (rv < 0)
578 {
579 errno_val = errno;
580 perror ("ERROR in main(): listen(AF_UNIX) failed");
581 fprintf (stderr, "SERVER: ERROR: listen() fd %d, \"%s\": "
582 "failed (errno = %d)!\n", ssm->af_unix_listen_fd,
583 SOCK_TEST_AF_UNIX_FILENAME, errno_val);
584 close (ssm->af_unix_listen_fd);
585 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
586 return rv;
587 }
588#endif /* SOCK_SERVER_USE_EPOLL */
Dave Wallace543852a2017-08-03 02:11:34 -0400589#endif
590 if (ssm->listen_fd < 0)
591 {
592 errno_val = errno;
593 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500594 fprintf (stderr, "SERVER: ERROR: socket() failed "
595 "(errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400596 return ssm->listen_fd;
597 }
598
599 memset (&servaddr, 0, sizeof (servaddr));
600
Dave Wallacede910062018-03-20 09:22:13 -0400601 if (ssm->cfg.address_ip6)
602 {
603 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
604#ifndef VCL_TEST
605 servaddr_size = sizeof (*server_addr);
606#endif
607 server_addr->sin6_family = AF_INET6;
608 server_addr->sin6_addr = in6addr_any;
609 server_addr->sin6_port = htons (port);
610 }
611 else
612 {
613 struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
614#ifndef VCL_TEST
615 servaddr_size = sizeof (*server_addr);
616#endif
617 server_addr->sin_family = AF_INET;
618 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
619 server_addr->sin_port = htons (port);
620 }
Dave Wallace543852a2017-08-03 02:11:34 -0400621
622#ifdef VCL_TEST
Dave Wallacede910062018-03-20 09:22:13 -0400623 if (ssm->cfg.address_ip6)
624 {
625 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
626 endpt.is_ip4 = 0;
627 endpt.ip = (uint8_t *) & server_addr->sin6_addr;
628 endpt.port = (uint16_t) server_addr->sin6_port;
629 }
630 else
631 {
632 struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
633 endpt.is_ip4 = 1;
634 endpt.ip = (uint8_t *) & server_addr->sin_addr;
635 endpt.port = (uint16_t) server_addr->sin_port;
636 }
Dave Wallace543852a2017-08-03 02:11:34 -0400637
638 rv = vppcom_session_bind (ssm->listen_fd, &endpt);
639 if (rv)
640 {
641 errno = -rv;
642 rv = -1;
643 }
644#else
Dave Wallacede910062018-03-20 09:22:13 -0400645 rv = bind (ssm->listen_fd, (struct sockaddr *) &servaddr, servaddr_size);
Dave Wallace543852a2017-08-03 02:11:34 -0400646#endif
647 if (rv < 0)
648 {
649 errno_val = errno;
650 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500651 fprintf (stderr, "SERVER: ERROR: bind failed (errno = %d)!\n",
652 errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400653 return rv;
654 }
655
656#ifdef VCL_TEST
657 rv = vppcom_session_listen (ssm->listen_fd, 10);
658 if (rv)
659 {
660 errno = -rv;
661 rv = -1;
662 }
663#else
664 rv = listen (ssm->listen_fd, 10);
665#endif
666 if (rv < 0)
667 {
668 errno_val = errno;
669 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500670 fprintf (stderr, "SERVER: ERROR: listen failed "
671 "(errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400672 return rv;
673 }
674
Dave Wallacef7f809c2017-10-03 01:48:42 -0400675#if ! SOCK_SERVER_USE_EPOLL
676
Dave Wallace543852a2017-08-03 02:11:34 -0400677 FD_ZERO (&ssm->wr_fdset);
678 FD_ZERO (&ssm->rd_fdset);
679
680 FD_SET (ssm->listen_fd, &ssm->rd_fdset);
681 ssm->nfds = ssm->listen_fd + 1;
682
Dave Wallacef7f809c2017-10-03 01:48:42 -0400683#else
684#ifdef VCL_TEST
685 ssm->epfd = vppcom_epoll_create ();
686 if (ssm->epfd < 0)
687 errno = -ssm->epfd;
688#else
689 ssm->epfd = epoll_create (1);
690#endif
691 if (ssm->epfd < 0)
692 {
693 errno_val = errno;
694 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500695 fprintf (stderr, "SERVER: ERROR: epoll_create failed (errno = %d)!\n",
Dave Wallacef7f809c2017-10-03 01:48:42 -0400696 errno_val);
697 return ssm->epfd;
698 }
699
700 ssm->listen_ev.events = EPOLLIN;
701 ssm->listen_ev.data.u32 = ~0;
702#ifdef VCL_TEST
703 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd,
704 &ssm->listen_ev);
705 if (rv < 0)
706 errno = -rv;
707#else
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500708 ssm->af_unix_listen_ev.events = EPOLLIN;
709 ssm->af_unix_listen_ev.data.u32 = SOCK_TEST_AF_UNIX_ACCEPT_DATA;
710 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->af_unix_listen_fd,
711 &ssm->af_unix_listen_ev);
712 if (rv < 0)
713 {
714 errno_val = errno;
715 perror ("ERROR in main(): mixed epoll_ctl(EPOLL_CTL_ADD)");
716 fprintf (stderr, "SERVER: ERROR: mixed epoll_ctl(epfd %d (0x%x), "
717 "EPOLL_CTL_ADD, af_unix_listen_fd %d (0x%x), EPOLLIN) failed "
718 "(errno = %d)!\n", ssm->epfd, ssm->epfd,
719 ssm->af_unix_listen_fd, ssm->af_unix_listen_fd, errno_val);
720 close (ssm->af_unix_listen_fd);
721 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
722 return rv;
723 }
724
Dave Wallacef7f809c2017-10-03 01:48:42 -0400725 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
726#endif
727 if (rv < 0)
728 {
729 errno_val = errno;
730 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500731 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed "
732 "(errno = %d)!\n", errno_val);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400733 return rv;
734 }
735#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400736
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500737 printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
738
Dave Wallace543852a2017-08-03 02:11:34 -0400739 while (1)
740 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400741#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400742 _rfdset = ssm->rd_fdset;
743
744#ifdef VCL_TEST
745 rv = vppcom_select (ssm->nfds, (uint64_t *) rfdset, NULL, NULL, 0);
746#else
747 {
748 struct timeval timeout;
749 timeout = ssm->timeout;
750 _wfdset = ssm->wr_fdset;
751 rv = select (ssm->nfds, rfdset, wfdset, NULL, &timeout);
752 }
753#endif
754 if (rv < 0)
755 {
756 perror ("select()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500757 fprintf (stderr, "\nSERVER: ERROR: select() failed -- aborting!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400758 main_rv = -1;
759 goto done;
760 }
761 else if (rv == 0)
762 continue;
763
764 if (FD_ISSET (ssm->listen_fd, rfdset))
765 new_client ();
766
767 for (i = 0; i < ssm->conn_pool_size; i++)
768 {
769 if (!ssm->conn_pool[i].is_alloc)
770 continue;
771
772 conn = &ssm->conn_pool[i];
Dave Wallacef7f809c2017-10-03 01:48:42 -0400773#else
774 int num_ev;
775#ifdef VCL_TEST
776 num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events,
777 SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0);
Dave Wallace0fd6ad62017-10-20 13:23:40 -0400778 if (num_ev < 0)
779 errno = -num_ev;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400780#else
781 num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
782 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
783#endif
784 if (num_ev < 0)
785 {
786 perror ("epoll_wait()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500787 fprintf (stderr, "\nSERVER: ERROR: epoll_wait() "
788 "failed -- aborting!\n");
Dave Wallacef7f809c2017-10-03 01:48:42 -0400789 main_rv = -1;
790 goto done;
791 }
792 if (num_ev == 0)
793 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500794 fprintf (stderr, "\nSERVER: epoll_wait() timeout!\n");
Dave Wallacef7f809c2017-10-03 01:48:42 -0400795 continue;
796 }
797 for (i = 0; i < num_ev; i++)
798 {
Dave Wallace63f18592018-03-13 19:37:54 -0400799 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
800 if (ssm->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
801 {
802#ifdef VCL_TEST
803 vppcom_session_close (conn->fd);
804#else
805 close (conn->fd);
806#endif
807 continue;
808 }
Dave Wallacef7f809c2017-10-03 01:48:42 -0400809 if (ssm->wait_events[i].data.u32 == ~0)
810 {
811 new_client ();
812 continue;
813 }
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500814#if !defined (VCL_TEST)
815 else if (ssm->wait_events[i].data.u32 ==
816 SOCK_TEST_AF_UNIX_ACCEPT_DATA)
817 {
818 af_unix_echo ();
819 continue;
820 }
821#endif
Dave Wallacef7f809c2017-10-03 01:48:42 -0400822#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400823 client_fd = conn->fd;
824
Dave Wallacef7f809c2017-10-03 01:48:42 -0400825#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400826 if (FD_ISSET (client_fd, rfdset))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400827#else
828 if (EPOLLIN & ssm->wait_events[i].events)
829#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400830 {
831 rx_bytes = sock_test_read (client_fd, conn->buf,
832 conn->buf_size, &conn->stats);
833 if (rx_bytes > 0)
834 {
835 rx_cfg = (sock_test_cfg_t *) conn->buf;
836 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
837 {
838 if (rx_cfg->verbose)
839 {
840 printf ("SERVER (fd %d): Received a cfg message!\n",
841 client_fd);
842 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
843 }
844
845 if (rx_bytes != sizeof (*rx_cfg))
846 {
847 printf ("SERVER (fd %d): Invalid cfg message "
848 "size (%d)!\n Should be %lu bytes.\n",
849 client_fd, rx_bytes, sizeof (*rx_cfg));
850 conn->cfg.rxbuf_size = 0;
851 conn->cfg.num_writes = 0;
852 if (conn->cfg.verbose)
853 {
854 printf ("SERVER (fd %d): Replying to "
855 "cfg message!\n", client_fd);
856 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
857 }
858 sock_test_write (client_fd, (uint8_t *) & conn->cfg,
859 sizeof (conn->cfg), NULL,
860 conn->cfg.verbose);
861 continue;
862 }
863
864 switch (rx_cfg->test)
865 {
866 case SOCK_TEST_TYPE_NONE:
867 case SOCK_TEST_TYPE_ECHO:
868 sync_config_and_reply (conn, rx_cfg);
869 break;
870
871 case SOCK_TEST_TYPE_BI:
872 case SOCK_TEST_TYPE_UNI:
873 stream_test_server_start_stop (conn, rx_cfg);
874 break;
875
876 case SOCK_TEST_TYPE_EXIT:
877 printf ("SERVER: Have a great day, "
878 "connection %d!\n", client_fd);
879#ifdef VCL_TEST
880 vppcom_session_close (client_fd);
881#else
882 close (client_fd);
883#endif
884 conn_pool_free (conn);
Dave Wallaceee45d412017-11-24 21:44:06 -0500885 printf ("SERVER: Closed client fd %d\n", client_fd);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400886#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400887 if (ssm->nfds == (ssm->listen_fd + 1))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400888#else
889 ssm->nfds--;
890 if (!ssm->nfds)
891#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400892 {
893 printf ("SERVER: All client connections "
894 "closed.\n\nSERVER: "
895 "May the force be with you!\n\n");
896 goto done;
897 }
898 break;
899
900 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500901 fprintf (stderr,
902 "SERVER: ERROR: Unknown test type!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400903 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
904 break;
905 }
906 continue;
907 }
908
909 else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) ||
910 (conn->cfg.test == SOCK_TEST_TYPE_BI))
911 {
912 stream_test_server (conn, rx_bytes);
913 continue;
914 }
915
Chris Luke879ace32017-09-26 13:15:16 -0400916 else if (isascii (conn->buf[0]))
Chris Lukeab7b8d92017-09-07 07:40:13 -0400917 {
918 // If it looks vaguely like a string, make sure it's terminated
919 ((char *) conn->buf)[rx_bytes <
920 conn->buf_size ? rx_bytes :
921 conn->buf_size - 1] = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400922 printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
Chris Lukeab7b8d92017-09-07 07:40:13 -0400923 conn->fd, rx_bytes, conn->buf);
924 }
Dave Wallace543852a2017-08-03 02:11:34 -0400925 }
926 else // rx_bytes < 0
927 {
928 if (errno == ECONNRESET)
929 {
930 printf ("\nSERVER: Connection reset by remote peer.\n"
931 " Y'all have a great day now!\n\n");
932 break;
933 }
934 else
935 continue;
936 }
937
Chris Luke879ace32017-09-26 13:15:16 -0400938 if (isascii (conn->buf[0]))
Dave Wallace543852a2017-08-03 02:11:34 -0400939 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500940 /* If it looks vaguely like a string,
941 * make sure it's terminated
942 */
Chris Luke879ace32017-09-26 13:15:16 -0400943 ((char *) conn->buf)[rx_bytes <
944 conn->buf_size ? rx_bytes :
945 conn->buf_size - 1] = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400946 if (xtra)
Dave Wallace048b1d62018-01-03 22:24:41 -0500947 fprintf (stderr, "SERVER: ERROR: "
948 "FIFO not drained in previous test!\n"
Dave Wallace543852a2017-08-03 02:11:34 -0400949 " extra chunks %u (0x%x)\n"
950 " extra bytes %lu (0x%lx)\n",
951 xtra, xtra, xtra_bytes, xtra_bytes);
952
953 xtra = 0;
954 xtra_bytes = 0;
955
956 if (conn->cfg.verbose)
957 printf ("SERVER (fd %d): Echoing back\n", client_fd);
958
959 nbytes = strlen ((const char *) conn->buf) + 1;
960
961 tx_bytes = sock_test_write (client_fd, conn->buf,
962 nbytes, &conn->stats,
963 conn->cfg.verbose);
964 if (tx_bytes >= 0)
965 printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
966 conn->fd, tx_bytes, conn->buf);
967 }
968
969 else // Extraneous read data from non-echo tests???
970 {
971 xtra++;
972 xtra_bytes += rx_bytes;
973 }
974 }
975 }
976 }
977
978done:
979#ifdef VCL_TEST
980 vppcom_session_close (ssm->listen_fd);
981 vppcom_app_destroy ();
982#else
983 close (ssm->listen_fd);
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500984
985#if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
986 close (ssm->af_unix_listen_fd);
987 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
988#endif /* SOCK_SERVER_USE_EPOLL */
989
Dave Wallace543852a2017-08-03 02:11:34 -0400990#endif
991 if (ssm->conn_pool)
992 free (ssm->conn_pool);
993
994 return main_rv;
995}
996
997/*
998 * fd.io coding-style-patch-verification: ON
999 *
1000 * Local Variables:
1001 * eval: (c-set-style "gnu")
1002 * End:
1003 */