blob: e5b5a56e8d665276787943aa53bb6c4f6bf3dd83 [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>
24#include <uri/sock_test.h>
25
Dave Wallacef7f809c2017-10-03 01:48:42 -040026#define SOCK_SERVER_USE_EPOLL 1
27
28#if SOCK_SERVER_USE_EPOLL
29#include <sys/epoll.h>
30#endif
31
Dave Wallace543852a2017-08-03 02:11:34 -040032typedef struct
33{
34 uint8_t is_alloc;
35 int fd;
36 uint8_t *buf;
37 uint32_t buf_size;
38 sock_test_cfg_t cfg;
39 sock_test_stats_t stats;
40#ifdef VCL_TEST
41 vppcom_endpt_t endpt;
42 uint8_t ip[16];
43#endif
44} sock_server_conn_t;
45
46#define SOCK_SERVER_MAX_TEST_CONN 10
Dave Wallacef7f809c2017-10-03 01:48:42 -040047#define SOCK_SERVER_MAX_EPOLL_EVENTS 10
Dave Wallace543852a2017-08-03 02:11:34 -040048typedef struct
49{
50 int listen_fd;
Dave Wallacef7f809c2017-10-03 01:48:42 -040051#if SOCK_SERVER_USE_EPOLL
52 int epfd;
53 struct epoll_event listen_ev;
54 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
55#endif
Dave Wallace543852a2017-08-03 02:11:34 -040056 size_t num_conn;
57 size_t conn_pool_size;
58 sock_server_conn_t *conn_pool;
59 int nfds;
60 fd_set rd_fdset;
61 fd_set wr_fdset;
62 struct timeval timeout;
63} sock_server_main_t;
64
65sock_server_main_t sock_server_main;
66
Dave Wallacef7f809c2017-10-03 01:48:42 -040067#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -040068static inline int
69get_nfds (void)
70{
71 sock_server_main_t *ssm = &sock_server_main;
72 int i, nfds;
73
74 for (nfds = i = 0; i < FD_SETSIZE; i++)
75 {
76 if (FD_ISSET (i, &ssm->rd_fdset) || FD_ISSET (i, &ssm->wr_fdset))
77 nfds = i + 1;
78 }
79 return nfds;
80}
81
82static inline void
83conn_fdset_set (sock_server_conn_t * conn, fd_set * fdset)
84{
85 sock_server_main_t *ssm = &sock_server_main;
86
87 FD_SET (conn->fd, fdset);
88 ssm->nfds = get_nfds ();
89}
90
91static inline void
92conn_fdset_clr (sock_server_conn_t * conn, fd_set * fdset)
93{
94 sock_server_main_t *ssm = &sock_server_main;
95
96 FD_CLR (conn->fd, fdset);
97 ssm->nfds = get_nfds ();
98}
Dave Wallacef7f809c2017-10-03 01:48:42 -040099#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400100
101static inline void
102conn_pool_expand (size_t expand_size)
103{
104 sock_server_main_t *ssm = &sock_server_main;
105 sock_server_conn_t *conn_pool;
106 size_t new_size = ssm->conn_pool_size + expand_size;
107 int i;
108
109 conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
110 if (conn_pool)
111 {
112 for (i = ssm->conn_pool_size; i < new_size; i++)
113 {
114 sock_server_conn_t *conn = &conn_pool[i];
115 memset (conn, 0, sizeof (*conn));
116 sock_test_cfg_init (&conn->cfg);
117 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
118 &conn->buf, &conn->buf_size);
119 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
120 }
121
122 ssm->conn_pool = conn_pool;
123 ssm->conn_pool_size = new_size;
124 }
125 else
126 {
127 int errno_val = errno;
128 perror ("ERROR in conn_pool_expand()");
129 fprintf (stderr, "ERROR: Memory allocation failed (errno = %d)!\n",
130 errno_val);
131 }
132}
133
134static inline sock_server_conn_t *
135conn_pool_alloc (void)
136{
137 sock_server_main_t *ssm = &sock_server_main;
138 int i;
139
140 for (i = 0; i < ssm->conn_pool_size; i++)
141 {
142 if (!ssm->conn_pool[i].is_alloc)
143 {
144#ifdef VCL_TEST
145 ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip;
146#endif
147 ssm->conn_pool[i].is_alloc = 1;
148 return (&ssm->conn_pool[i]);
149 }
150 }
151
152 return 0;
153}
154
155static inline void
156conn_pool_free (sock_server_conn_t * conn)
157{
Dave Wallacef7f809c2017-10-03 01:48:42 -0400158#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400159 sock_server_main_t *ssm = &sock_server_main;
160
161 conn_fdset_clr (conn, &ssm->rd_fdset);
162 conn_fdset_clr (conn, &ssm->wr_fdset);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400163#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400164 conn->fd = 0;
165 conn->is_alloc = 0;
166}
167
168static inline void
169sync_config_and_reply (sock_server_conn_t * conn, sock_test_cfg_t * rx_cfg)
170{
171 conn->cfg = *rx_cfg;
172 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
173 &conn->buf, &conn->buf_size);
174 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
175
176 if (conn->cfg.verbose)
177 {
178 printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd);
179 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
180 }
181 (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
182 sizeof (conn->cfg), NULL, conn->cfg.verbose);
183}
184
185static void
186stream_test_server_start_stop (sock_server_conn_t * conn,
187 sock_test_cfg_t * rx_cfg)
188{
189 sock_server_main_t *ssm = &sock_server_main;
190 int client_fd = conn->fd;
191 sock_test_t test = rx_cfg->test;
192
193 if (rx_cfg->ctrl_handle == conn->fd)
194 {
195 int i;
196 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
197
198 for (i = 0; i < ssm->conn_pool_size; i++)
199 {
200 sock_server_conn_t *tc = &ssm->conn_pool[i];
201
202 if (tc->cfg.ctrl_handle == conn->fd)
203 {
204 sock_test_stats_accumulate (&conn->stats, &tc->stats);
205
206 if (conn->cfg.verbose)
207 {
208 static char buf[64];
209
210 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
211 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
212 test == SOCK_TEST_TYPE_BI
213 /* show tx */ ,
214 conn->cfg.verbose);
215 }
216 }
217 }
218
219 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
220 (test == SOCK_TEST_TYPE_BI) /* show_tx */ ,
221 conn->cfg.verbose);
222 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
223 if (conn->cfg.verbose)
224 {
225 printf (" sock server main\n"
226 SOCK_TEST_SEPARATOR_STRING
227 " buf: %p\n"
228 " buf size: %u (0x%08x)\n"
229 SOCK_TEST_SEPARATOR_STRING,
230 conn->buf, conn->buf_size, conn->buf_size);
231 }
232
233 sync_config_and_reply (conn, rx_cfg);
234 printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
235 SOCK_TEST_BANNER_STRING "\n", conn->fd,
236 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
237 }
238 else
239 {
240 printf ("\n" SOCK_TEST_BANNER_STRING
241 "SERVER (fd %d): %s-directional Stream Test!\n"
242 " Sending client the test cfg to start streaming data...\n",
243 client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
244
245 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
246 rx_cfg->ctrl_handle;
247
248 sync_config_and_reply (conn, rx_cfg);
249
250 /* read the 1st chunk, record start time */
251 memset (&conn->stats, 0, sizeof (conn->stats));
252 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
253 }
254}
255
256
257static inline void
258stream_test_server (sock_server_conn_t * conn, int rx_bytes)
259{
260 int client_fd = conn->fd;
261 sock_test_t test = conn->cfg.test;
262
263 if (test == SOCK_TEST_TYPE_BI)
264 (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
265 conn->cfg.verbose);
266
267 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
268 {
269 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
270 }
271}
272
273static inline void
274new_client (void)
275{
276 sock_server_main_t *ssm = &sock_server_main;
277 int client_fd;
278 sock_server_conn_t *conn;
279
280 if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
281 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
282
283 conn = conn_pool_alloc ();
284 if (!conn)
285 {
286 fprintf (stderr, "\nERROR: No free connections!\n");
287 return;
288 }
289
290#ifdef VCL_TEST
291 client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt,
292 -1.0 /* wait forever */ );
Dave Wallacef7f809c2017-10-03 01:48:42 -0400293 if (client_fd < 0)
294 errno = -client_fd;
Dave Wallace543852a2017-08-03 02:11:34 -0400295#else
296 client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
297#endif
298 if (client_fd < 0)
299 {
300 int errno_val;
301 errno_val = errno;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400302 perror ("ERROR in new_client()");
Dave Wallace543852a2017-08-03 02:11:34 -0400303 fprintf (stderr, "ERROR: accept failed (errno = %d)!\n", errno_val);
304 }
305
306 printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n",
307 client_fd, client_fd);
308
309 conn->fd = client_fd;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400310
311#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400312 conn_fdset_set (conn, &ssm->rd_fdset);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400313 ssm->nfds++;
314#else
315 {
316 struct epoll_event ev;
317 int rv;
318
319 ev.events = EPOLLIN;
320 ev.data.u64 = conn - ssm->conn_pool;
321#ifdef VCL_TEST
322 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
323 if (rv)
324 errno = -rv;
325#else
326 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
327#endif
328 if (rv < 0)
329 {
330 int errno_val;
331 errno_val = errno;
332 perror ("ERROR in new_client()");
333 fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n",
334 errno_val);
335 }
336 else
337 ssm->nfds++;
338 }
339#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400340}
341
342int
343main (int argc, char **argv)
344{
345 sock_server_main_t *ssm = &sock_server_main;
346 int client_fd, rv, main_rv = 0;
347 int tx_bytes, rx_bytes, nbytes;
348 sock_server_conn_t *conn;
349 sock_test_cfg_t *rx_cfg;
350 uint32_t xtra = 0;
351 uint64_t xtra_bytes = 0;
352 struct sockaddr_in servaddr;
353 int errno_val;
354 int v, i;
355 uint16_t port = SOCK_TEST_SERVER_PORT;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400356#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400357 fd_set _rfdset, *rfdset = &_rfdset;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400358#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400359#ifdef VCL_TEST
360 vppcom_endpt_t endpt;
361#else
Dave Wallacef7f809c2017-10-03 01:48:42 -0400362#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400363 fd_set _wfdset, *wfdset = &_wfdset;
364#endif
Dave Wallacef7f809c2017-10-03 01:48:42 -0400365#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400366
367 if ((argc == 2) && (sscanf (argv[1], "%d", &v) == 1))
368 port = (uint16_t) v;
369
370 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
371
372#ifdef VCL_TEST
373 rv = vppcom_app_create ("vcl_test_server");
374 if (rv)
375 {
376 errno = -rv;
377 ssm->listen_fd = -1;
378 }
379 else
380 {
381 ssm->listen_fd =
382 vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP,
383 0 /* is_nonblocking */ );
384 }
385#else
386 ssm->listen_fd = socket (AF_INET, SOCK_STREAM, 0);
387#endif
388 if (ssm->listen_fd < 0)
389 {
390 errno_val = errno;
391 perror ("ERROR in main()");
392 fprintf (stderr, "ERROR: socket() failed (errno = %d)!\n", errno_val);
393 return ssm->listen_fd;
394 }
395
396 memset (&servaddr, 0, sizeof (servaddr));
397
398 servaddr.sin_family = AF_INET;
Dave Wallace33e002b2017-09-06 01:20:02 -0400399 servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
Dave Wallace543852a2017-08-03 02:11:34 -0400400 servaddr.sin_port = htons (port);
401
402#ifdef VCL_TEST
403 endpt.vrf = VPPCOM_VRF_DEFAULT;
404 endpt.is_ip4 = (servaddr.sin_family == AF_INET);
405 endpt.ip = (uint8_t *) & servaddr.sin_addr;
406 endpt.port = (uint16_t) servaddr.sin_port;
407
408 rv = vppcom_session_bind (ssm->listen_fd, &endpt);
409 if (rv)
410 {
411 errno = -rv;
412 rv = -1;
413 }
414#else
415 rv =
416 bind (ssm->listen_fd, (struct sockaddr *) &servaddr, sizeof (servaddr));
417#endif
418 if (rv < 0)
419 {
420 errno_val = errno;
421 perror ("ERROR in main()");
422 fprintf (stderr, "ERROR: bind failed (errno = %d)!\n", errno_val);
423 return rv;
424 }
425
426#ifdef VCL_TEST
427 rv = vppcom_session_listen (ssm->listen_fd, 10);
428 if (rv)
429 {
430 errno = -rv;
431 rv = -1;
432 }
433#else
434 rv = listen (ssm->listen_fd, 10);
435#endif
436 if (rv < 0)
437 {
438 errno_val = errno;
439 perror ("ERROR in main()");
440 fprintf (stderr, "ERROR: listen failed (errno = %d)!\n", errno_val);
441 return rv;
442 }
443
Dave Wallacef7f809c2017-10-03 01:48:42 -0400444 printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
445
446#if ! SOCK_SERVER_USE_EPOLL
447
Dave Wallace543852a2017-08-03 02:11:34 -0400448 FD_ZERO (&ssm->wr_fdset);
449 FD_ZERO (&ssm->rd_fdset);
450
451 FD_SET (ssm->listen_fd, &ssm->rd_fdset);
452 ssm->nfds = ssm->listen_fd + 1;
453
Dave Wallacef7f809c2017-10-03 01:48:42 -0400454#else
455#ifdef VCL_TEST
456 ssm->epfd = vppcom_epoll_create ();
457 if (ssm->epfd < 0)
458 errno = -ssm->epfd;
459#else
460 ssm->epfd = epoll_create (1);
461#endif
462 if (ssm->epfd < 0)
463 {
464 errno_val = errno;
465 perror ("ERROR in main()");
466 fprintf (stderr, "ERROR: epoll_create failed (errno = %d)!\n",
467 errno_val);
468 return ssm->epfd;
469 }
470
471 ssm->listen_ev.events = EPOLLIN;
472 ssm->listen_ev.data.u32 = ~0;
473#ifdef VCL_TEST
474 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd,
475 &ssm->listen_ev);
476 if (rv < 0)
477 errno = -rv;
478#else
479 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
480#endif
481 if (rv < 0)
482 {
483 errno_val = errno;
484 perror ("ERROR in main()");
485 fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n", errno_val);
486 return rv;
487 }
488#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400489
490 while (1)
491 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400492#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400493 _rfdset = ssm->rd_fdset;
494
495#ifdef VCL_TEST
496 rv = vppcom_select (ssm->nfds, (uint64_t *) rfdset, NULL, NULL, 0);
497#else
498 {
499 struct timeval timeout;
500 timeout = ssm->timeout;
501 _wfdset = ssm->wr_fdset;
502 rv = select (ssm->nfds, rfdset, wfdset, NULL, &timeout);
503 }
504#endif
505 if (rv < 0)
506 {
507 perror ("select()");
508 fprintf (stderr, "\nERROR: select() failed -- aborting!\n");
509 main_rv = -1;
510 goto done;
511 }
512 else if (rv == 0)
513 continue;
514
515 if (FD_ISSET (ssm->listen_fd, rfdset))
516 new_client ();
517
518 for (i = 0; i < ssm->conn_pool_size; i++)
519 {
520 if (!ssm->conn_pool[i].is_alloc)
521 continue;
522
523 conn = &ssm->conn_pool[i];
Dave Wallacef7f809c2017-10-03 01:48:42 -0400524#else
525 int num_ev;
526#ifdef VCL_TEST
527 num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events,
528 SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0);
529 if (rv < 0)
530 errno = -rv;
531#else
532 num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
533 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
534#endif
535 if (num_ev < 0)
536 {
537 perror ("epoll_wait()");
538 fprintf (stderr, "\nERROR: epoll_wait() failed -- aborting!\n");
539 main_rv = -1;
540 goto done;
541 }
542 if (num_ev == 0)
543 {
544 fprintf (stderr, "\nepoll_wait() timeout!\n");
545 continue;
546 }
547 for (i = 0; i < num_ev; i++)
548 {
549 if (ssm->wait_events[i].data.u32 == ~0)
550 {
551 new_client ();
552 continue;
553 }
554 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
555#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400556 client_fd = conn->fd;
557
Dave Wallacef7f809c2017-10-03 01:48:42 -0400558#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400559 if (FD_ISSET (client_fd, rfdset))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400560#else
561 if (EPOLLIN & ssm->wait_events[i].events)
562#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400563 {
564 rx_bytes = sock_test_read (client_fd, conn->buf,
565 conn->buf_size, &conn->stats);
566 if (rx_bytes > 0)
567 {
568 rx_cfg = (sock_test_cfg_t *) conn->buf;
569 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
570 {
571 if (rx_cfg->verbose)
572 {
573 printf ("SERVER (fd %d): Received a cfg message!\n",
574 client_fd);
575 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
576 }
577
578 if (rx_bytes != sizeof (*rx_cfg))
579 {
580 printf ("SERVER (fd %d): Invalid cfg message "
581 "size (%d)!\n Should be %lu bytes.\n",
582 client_fd, rx_bytes, sizeof (*rx_cfg));
583 conn->cfg.rxbuf_size = 0;
584 conn->cfg.num_writes = 0;
585 if (conn->cfg.verbose)
586 {
587 printf ("SERVER (fd %d): Replying to "
588 "cfg message!\n", client_fd);
589 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
590 }
591 sock_test_write (client_fd, (uint8_t *) & conn->cfg,
592 sizeof (conn->cfg), NULL,
593 conn->cfg.verbose);
594 continue;
595 }
596
597 switch (rx_cfg->test)
598 {
599 case SOCK_TEST_TYPE_NONE:
600 case SOCK_TEST_TYPE_ECHO:
601 sync_config_and_reply (conn, rx_cfg);
602 break;
603
604 case SOCK_TEST_TYPE_BI:
605 case SOCK_TEST_TYPE_UNI:
606 stream_test_server_start_stop (conn, rx_cfg);
607 break;
608
609 case SOCK_TEST_TYPE_EXIT:
610 printf ("SERVER: Have a great day, "
611 "connection %d!\n", client_fd);
612#ifdef VCL_TEST
613 vppcom_session_close (client_fd);
614#else
615 close (client_fd);
616#endif
617 conn_pool_free (conn);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400618#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400619 if (ssm->nfds == (ssm->listen_fd + 1))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400620#else
621 ssm->nfds--;
622 if (!ssm->nfds)
623#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400624 {
625 printf ("SERVER: All client connections "
626 "closed.\n\nSERVER: "
627 "May the force be with you!\n\n");
628 goto done;
629 }
630 break;
631
632 default:
633 fprintf (stderr, "ERROR: Unknown test type!\n");
634 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
635 break;
636 }
637 continue;
638 }
639
640 else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) ||
641 (conn->cfg.test == SOCK_TEST_TYPE_BI))
642 {
643 stream_test_server (conn, rx_bytes);
644 continue;
645 }
646
Chris Luke879ace32017-09-26 13:15:16 -0400647 else if (isascii (conn->buf[0]))
Chris Lukeab7b8d92017-09-07 07:40:13 -0400648 {
649 // If it looks vaguely like a string, make sure it's terminated
650 ((char *) conn->buf)[rx_bytes <
651 conn->buf_size ? rx_bytes :
652 conn->buf_size - 1] = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400653 printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
Chris Lukeab7b8d92017-09-07 07:40:13 -0400654 conn->fd, rx_bytes, conn->buf);
655 }
Dave Wallace543852a2017-08-03 02:11:34 -0400656 }
657 else // rx_bytes < 0
658 {
659 if (errno == ECONNRESET)
660 {
661 printf ("\nSERVER: Connection reset by remote peer.\n"
662 " Y'all have a great day now!\n\n");
663 break;
664 }
665 else
666 continue;
667 }
668
Chris Luke879ace32017-09-26 13:15:16 -0400669 if (isascii (conn->buf[0]))
Dave Wallace543852a2017-08-03 02:11:34 -0400670 {
Chris Luke879ace32017-09-26 13:15:16 -0400671 // If it looks vaguely like a string, make sure it's terminated
672 ((char *) conn->buf)[rx_bytes <
673 conn->buf_size ? rx_bytes :
674 conn->buf_size - 1] = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400675 if (xtra)
676 fprintf (stderr,
677 "ERROR: FIFO not drained in previous test!\n"
678 " extra chunks %u (0x%x)\n"
679 " extra bytes %lu (0x%lx)\n",
680 xtra, xtra, xtra_bytes, xtra_bytes);
681
682 xtra = 0;
683 xtra_bytes = 0;
684
685 if (conn->cfg.verbose)
686 printf ("SERVER (fd %d): Echoing back\n", client_fd);
687
688 nbytes = strlen ((const char *) conn->buf) + 1;
689
690 tx_bytes = sock_test_write (client_fd, conn->buf,
691 nbytes, &conn->stats,
692 conn->cfg.verbose);
693 if (tx_bytes >= 0)
694 printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
695 conn->fd, tx_bytes, conn->buf);
696 }
697
698 else // Extraneous read data from non-echo tests???
699 {
700 xtra++;
701 xtra_bytes += rx_bytes;
702 }
703 }
704 }
705 }
706
707done:
708#ifdef VCL_TEST
709 vppcom_session_close (ssm->listen_fd);
710 vppcom_app_destroy ();
711#else
712 close (ssm->listen_fd);
713#endif
714 if (ssm->conn_pool)
715 free (ssm->conn_pool);
716
717 return main_rv;
718}
719
720/*
721 * fd.io coding-style-patch-verification: ON
722 *
723 * Local Variables:
724 * eval: (c-set-style "gnu")
725 * End:
726 */