blob: 54769817c0f4382d66354f6a0af6fba413095929 [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>
33#endif
34
Dave Wallace35830af2017-10-09 01:43:42 -040035#ifdef VCL_TEST
36#if VPPCOM_SESSION_ATTR_UNIT_TEST
37#define BUFLEN sizeof (uint64_t) * 16
38uint64_t buffer[16];
39uint32_t buflen = BUFLEN;
40uint32_t *flags = (uint32_t *) buffer;
41#endif
42#endif
43
Dave Wallace543852a2017-08-03 02:11:34 -040044typedef struct
45{
46 uint8_t is_alloc;
47 int fd;
48 uint8_t *buf;
49 uint32_t buf_size;
50 sock_test_cfg_t cfg;
51 sock_test_stats_t stats;
52#ifdef VCL_TEST
53 vppcom_endpt_t endpt;
54 uint8_t ip[16];
55#endif
56} sock_server_conn_t;
57
58#define SOCK_SERVER_MAX_TEST_CONN 10
Dave Wallacef7f809c2017-10-03 01:48:42 -040059#define SOCK_SERVER_MAX_EPOLL_EVENTS 10
Dave Wallace543852a2017-08-03 02:11:34 -040060typedef struct
61{
62 int listen_fd;
Dave Wallacef7f809c2017-10-03 01:48:42 -040063#if SOCK_SERVER_USE_EPOLL
64 int epfd;
65 struct epoll_event listen_ev;
66 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
67#endif
Dave Wallace543852a2017-08-03 02:11:34 -040068 size_t num_conn;
69 size_t conn_pool_size;
70 sock_server_conn_t *conn_pool;
71 int nfds;
72 fd_set rd_fdset;
73 fd_set wr_fdset;
74 struct timeval timeout;
75} sock_server_main_t;
76
77sock_server_main_t sock_server_main;
78
Dave Wallacef7f809c2017-10-03 01:48:42 -040079#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -040080static inline int
81get_nfds (void)
82{
83 sock_server_main_t *ssm = &sock_server_main;
84 int i, nfds;
85
86 for (nfds = i = 0; i < FD_SETSIZE; i++)
87 {
88 if (FD_ISSET (i, &ssm->rd_fdset) || FD_ISSET (i, &ssm->wr_fdset))
89 nfds = i + 1;
90 }
91 return nfds;
92}
93
94static inline void
95conn_fdset_set (sock_server_conn_t * conn, fd_set * fdset)
96{
97 sock_server_main_t *ssm = &sock_server_main;
98
99 FD_SET (conn->fd, fdset);
100 ssm->nfds = get_nfds ();
101}
102
103static inline void
104conn_fdset_clr (sock_server_conn_t * conn, fd_set * fdset)
105{
106 sock_server_main_t *ssm = &sock_server_main;
107
108 FD_CLR (conn->fd, fdset);
109 ssm->nfds = get_nfds ();
110}
Dave Wallacef7f809c2017-10-03 01:48:42 -0400111#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400112
113static inline void
114conn_pool_expand (size_t expand_size)
115{
116 sock_server_main_t *ssm = &sock_server_main;
117 sock_server_conn_t *conn_pool;
118 size_t new_size = ssm->conn_pool_size + expand_size;
119 int i;
120
121 conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
122 if (conn_pool)
123 {
124 for (i = ssm->conn_pool_size; i < new_size; i++)
125 {
126 sock_server_conn_t *conn = &conn_pool[i];
127 memset (conn, 0, sizeof (*conn));
128 sock_test_cfg_init (&conn->cfg);
129 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
130 &conn->buf, &conn->buf_size);
131 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
132 }
133
134 ssm->conn_pool = conn_pool;
135 ssm->conn_pool_size = new_size;
136 }
137 else
138 {
139 int errno_val = errno;
140 perror ("ERROR in conn_pool_expand()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500141 fprintf (stderr, "SERVER: ERROR: Memory allocation "
142 "failed (errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400143 }
144}
145
146static inline sock_server_conn_t *
147conn_pool_alloc (void)
148{
149 sock_server_main_t *ssm = &sock_server_main;
150 int i;
151
152 for (i = 0; i < ssm->conn_pool_size; i++)
153 {
154 if (!ssm->conn_pool[i].is_alloc)
155 {
156#ifdef VCL_TEST
157 ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip;
158#endif
159 ssm->conn_pool[i].is_alloc = 1;
160 return (&ssm->conn_pool[i]);
161 }
162 }
163
164 return 0;
165}
166
167static inline void
168conn_pool_free (sock_server_conn_t * conn)
169{
Dave Wallacef7f809c2017-10-03 01:48:42 -0400170#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400171 sock_server_main_t *ssm = &sock_server_main;
172
173 conn_fdset_clr (conn, &ssm->rd_fdset);
174 conn_fdset_clr (conn, &ssm->wr_fdset);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400175#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400176 conn->fd = 0;
177 conn->is_alloc = 0;
178}
179
180static inline void
181sync_config_and_reply (sock_server_conn_t * conn, sock_test_cfg_t * rx_cfg)
182{
183 conn->cfg = *rx_cfg;
184 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
185 &conn->buf, &conn->buf_size);
186 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
187
188 if (conn->cfg.verbose)
189 {
190 printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd);
191 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
192 }
193 (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
194 sizeof (conn->cfg), NULL, conn->cfg.verbose);
195}
196
197static void
198stream_test_server_start_stop (sock_server_conn_t * conn,
199 sock_test_cfg_t * rx_cfg)
200{
201 sock_server_main_t *ssm = &sock_server_main;
202 int client_fd = conn->fd;
203 sock_test_t test = rx_cfg->test;
204
205 if (rx_cfg->ctrl_handle == conn->fd)
206 {
207 int i;
208 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
209
210 for (i = 0; i < ssm->conn_pool_size; i++)
211 {
212 sock_server_conn_t *tc = &ssm->conn_pool[i];
213
214 if (tc->cfg.ctrl_handle == conn->fd)
215 {
216 sock_test_stats_accumulate (&conn->stats, &tc->stats);
217
218 if (conn->cfg.verbose)
219 {
220 static char buf[64];
221
222 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
223 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
224 test == SOCK_TEST_TYPE_BI
225 /* show tx */ ,
226 conn->cfg.verbose);
227 }
228 }
229 }
230
231 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
232 (test == SOCK_TEST_TYPE_BI) /* show_tx */ ,
233 conn->cfg.verbose);
234 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
235 if (conn->cfg.verbose)
236 {
237 printf (" sock server main\n"
238 SOCK_TEST_SEPARATOR_STRING
239 " buf: %p\n"
240 " buf size: %u (0x%08x)\n"
241 SOCK_TEST_SEPARATOR_STRING,
242 conn->buf, conn->buf_size, conn->buf_size);
243 }
244
245 sync_config_and_reply (conn, rx_cfg);
246 printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
247 SOCK_TEST_BANNER_STRING "\n", conn->fd,
248 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
249 }
250 else
251 {
252 printf ("\n" SOCK_TEST_BANNER_STRING
253 "SERVER (fd %d): %s-directional Stream Test!\n"
254 " Sending client the test cfg to start streaming data...\n",
255 client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
256
257 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
258 rx_cfg->ctrl_handle;
259
260 sync_config_and_reply (conn, rx_cfg);
261
262 /* read the 1st chunk, record start time */
263 memset (&conn->stats, 0, sizeof (conn->stats));
264 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
265 }
266}
267
268
269static inline void
270stream_test_server (sock_server_conn_t * conn, int rx_bytes)
271{
272 int client_fd = conn->fd;
273 sock_test_t test = conn->cfg.test;
274
275 if (test == SOCK_TEST_TYPE_BI)
276 (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
277 conn->cfg.verbose);
278
279 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
280 {
281 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
282 }
283}
284
285static inline void
286new_client (void)
287{
288 sock_server_main_t *ssm = &sock_server_main;
289 int client_fd;
290 sock_server_conn_t *conn;
291
292 if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
293 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
294
295 conn = conn_pool_alloc ();
296 if (!conn)
297 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500298 fprintf (stderr, "\nSERVER: ERROR: No free connections!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400299 return;
300 }
301
302#ifdef VCL_TEST
Dave Wallace048b1d62018-01-03 22:24:41 -0500303 client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, 0);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400304 if (client_fd < 0)
305 errno = -client_fd;
Dave Wallace59179392017-11-07 02:20:07 -0500306#elif HAVE_ACCEPT4
307 client_fd = accept4 (ssm->listen_fd, (struct sockaddr *) NULL, NULL, NULL);
Dave Wallace543852a2017-08-03 02:11:34 -0400308#else
309 client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
310#endif
311 if (client_fd < 0)
312 {
313 int errno_val;
314 errno_val = errno;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400315 perror ("ERROR in new_client()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500316 fprintf (stderr, "SERVER: ERROR: accept failed "
317 "(errno = %d)!\n", errno_val);
Dave Wallaceee45d412017-11-24 21:44:06 -0500318 return;
Dave Wallace543852a2017-08-03 02:11:34 -0400319 }
320
321 printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n",
322 client_fd, client_fd);
323
324 conn->fd = client_fd;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400325
326#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400327 conn_fdset_set (conn, &ssm->rd_fdset);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400328 ssm->nfds++;
329#else
330 {
331 struct epoll_event ev;
332 int rv;
333
334 ev.events = EPOLLIN;
335 ev.data.u64 = conn - ssm->conn_pool;
336#ifdef VCL_TEST
337 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
338 if (rv)
339 errno = -rv;
340#else
341 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
342#endif
343 if (rv < 0)
344 {
345 int errno_val;
346 errno_val = errno;
347 perror ("ERROR in new_client()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500348 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed (errno = %d)!\n",
Dave Wallacef7f809c2017-10-03 01:48:42 -0400349 errno_val);
350 }
351 else
352 ssm->nfds++;
353 }
354#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400355}
356
357int
358main (int argc, char **argv)
359{
360 sock_server_main_t *ssm = &sock_server_main;
361 int client_fd, rv, main_rv = 0;
362 int tx_bytes, rx_bytes, nbytes;
363 sock_server_conn_t *conn;
364 sock_test_cfg_t *rx_cfg;
365 uint32_t xtra = 0;
366 uint64_t xtra_bytes = 0;
367 struct sockaddr_in servaddr;
368 int errno_val;
369 int v, i;
370 uint16_t port = SOCK_TEST_SERVER_PORT;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400371#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400372 fd_set _rfdset, *rfdset = &_rfdset;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400373#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400374#ifdef VCL_TEST
375 vppcom_endpt_t endpt;
376#else
Dave Wallacef7f809c2017-10-03 01:48:42 -0400377#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400378 fd_set _wfdset, *wfdset = &_wfdset;
379#endif
Dave Wallacef7f809c2017-10-03 01:48:42 -0400380#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400381
382 if ((argc == 2) && (sscanf (argv[1], "%d", &v) == 1))
383 port = (uint16_t) v;
384
385 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
386
387#ifdef VCL_TEST
388 rv = vppcom_app_create ("vcl_test_server");
389 if (rv)
390 {
391 errno = -rv;
392 ssm->listen_fd = -1;
393 }
394 else
395 {
396 ssm->listen_fd =
Dave Wallacec04cbf12018-02-07 18:14:02 -0500397 vppcom_session_create (VPPCOM_PROTO_TCP, 0 /* is_nonblocking */ );
Dave Wallace543852a2017-08-03 02:11:34 -0400398 }
399#else
400 ssm->listen_fd = socket (AF_INET, SOCK_STREAM, 0);
401#endif
402 if (ssm->listen_fd < 0)
403 {
404 errno_val = errno;
405 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500406 fprintf (stderr, "SERVER: ERROR: socket() failed "
407 "(errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400408 return ssm->listen_fd;
409 }
410
411 memset (&servaddr, 0, sizeof (servaddr));
412
413 servaddr.sin_family = AF_INET;
Dave Wallace33e002b2017-09-06 01:20:02 -0400414 servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
Dave Wallace543852a2017-08-03 02:11:34 -0400415 servaddr.sin_port = htons (port);
416
417#ifdef VCL_TEST
Dave Wallace543852a2017-08-03 02:11:34 -0400418 endpt.is_ip4 = (servaddr.sin_family == AF_INET);
419 endpt.ip = (uint8_t *) & servaddr.sin_addr;
420 endpt.port = (uint16_t) servaddr.sin_port;
421
422 rv = vppcom_session_bind (ssm->listen_fd, &endpt);
423 if (rv)
424 {
425 errno = -rv;
426 rv = -1;
427 }
428#else
429 rv =
430 bind (ssm->listen_fd, (struct sockaddr *) &servaddr, sizeof (servaddr));
431#endif
432 if (rv < 0)
433 {
434 errno_val = errno;
435 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500436 fprintf (stderr, "SERVER: ERROR: bind failed (errno = %d)!\n",
437 errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400438 return rv;
439 }
440
441#ifdef VCL_TEST
442 rv = vppcom_session_listen (ssm->listen_fd, 10);
443 if (rv)
444 {
445 errno = -rv;
446 rv = -1;
447 }
448#else
449 rv = listen (ssm->listen_fd, 10);
450#endif
451 if (rv < 0)
452 {
453 errno_val = errno;
454 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500455 fprintf (stderr, "SERVER: ERROR: listen failed "
456 "(errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400457 return rv;
458 }
459
Dave Wallacef7f809c2017-10-03 01:48:42 -0400460 printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
461
462#if ! SOCK_SERVER_USE_EPOLL
463
Dave Wallace543852a2017-08-03 02:11:34 -0400464 FD_ZERO (&ssm->wr_fdset);
465 FD_ZERO (&ssm->rd_fdset);
466
467 FD_SET (ssm->listen_fd, &ssm->rd_fdset);
468 ssm->nfds = ssm->listen_fd + 1;
469
Dave Wallacef7f809c2017-10-03 01:48:42 -0400470#else
471#ifdef VCL_TEST
472 ssm->epfd = vppcom_epoll_create ();
473 if (ssm->epfd < 0)
474 errno = -ssm->epfd;
475#else
476 ssm->epfd = epoll_create (1);
477#endif
478 if (ssm->epfd < 0)
479 {
480 errno_val = errno;
481 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500482 fprintf (stderr, "SERVER: ERROR: epoll_create failed (errno = %d)!\n",
Dave Wallacef7f809c2017-10-03 01:48:42 -0400483 errno_val);
484 return ssm->epfd;
485 }
486
487 ssm->listen_ev.events = EPOLLIN;
488 ssm->listen_ev.data.u32 = ~0;
489#ifdef VCL_TEST
490 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd,
491 &ssm->listen_ev);
492 if (rv < 0)
493 errno = -rv;
494#else
495 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
496#endif
497 if (rv < 0)
498 {
499 errno_val = errno;
500 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500501 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed "
502 "(errno = %d)!\n", errno_val);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400503 return rv;
504 }
505#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400506
507 while (1)
508 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400509#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400510 _rfdset = ssm->rd_fdset;
511
512#ifdef VCL_TEST
513 rv = vppcom_select (ssm->nfds, (uint64_t *) rfdset, NULL, NULL, 0);
514#else
515 {
516 struct timeval timeout;
517 timeout = ssm->timeout;
518 _wfdset = ssm->wr_fdset;
519 rv = select (ssm->nfds, rfdset, wfdset, NULL, &timeout);
520 }
521#endif
522 if (rv < 0)
523 {
524 perror ("select()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500525 fprintf (stderr, "\nSERVER: ERROR: select() failed -- aborting!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400526 main_rv = -1;
527 goto done;
528 }
529 else if (rv == 0)
530 continue;
531
532 if (FD_ISSET (ssm->listen_fd, rfdset))
533 new_client ();
534
535 for (i = 0; i < ssm->conn_pool_size; i++)
536 {
537 if (!ssm->conn_pool[i].is_alloc)
538 continue;
539
540 conn = &ssm->conn_pool[i];
Dave Wallacef7f809c2017-10-03 01:48:42 -0400541#else
542 int num_ev;
543#ifdef VCL_TEST
544 num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events,
545 SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0);
Dave Wallace0fd6ad62017-10-20 13:23:40 -0400546 if (num_ev < 0)
547 errno = -num_ev;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400548#else
549 num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
550 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
551#endif
552 if (num_ev < 0)
553 {
554 perror ("epoll_wait()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500555 fprintf (stderr, "\nSERVER: ERROR: epoll_wait() "
556 "failed -- aborting!\n");
Dave Wallacef7f809c2017-10-03 01:48:42 -0400557 main_rv = -1;
558 goto done;
559 }
560 if (num_ev == 0)
561 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500562 fprintf (stderr, "\nSERVER: epoll_wait() timeout!\n");
Dave Wallacef7f809c2017-10-03 01:48:42 -0400563 continue;
564 }
565 for (i = 0; i < num_ev; i++)
566 {
567 if (ssm->wait_events[i].data.u32 == ~0)
568 {
569 new_client ();
570 continue;
571 }
572 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
573#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400574 client_fd = conn->fd;
575
Dave Wallacef7f809c2017-10-03 01:48:42 -0400576#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400577 if (FD_ISSET (client_fd, rfdset))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400578#else
579 if (EPOLLIN & ssm->wait_events[i].events)
580#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400581 {
582 rx_bytes = sock_test_read (client_fd, conn->buf,
583 conn->buf_size, &conn->stats);
584 if (rx_bytes > 0)
585 {
586 rx_cfg = (sock_test_cfg_t *) conn->buf;
587 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
588 {
589 if (rx_cfg->verbose)
590 {
591 printf ("SERVER (fd %d): Received a cfg message!\n",
592 client_fd);
593 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
594 }
595
596 if (rx_bytes != sizeof (*rx_cfg))
597 {
598 printf ("SERVER (fd %d): Invalid cfg message "
599 "size (%d)!\n Should be %lu bytes.\n",
600 client_fd, rx_bytes, sizeof (*rx_cfg));
601 conn->cfg.rxbuf_size = 0;
602 conn->cfg.num_writes = 0;
603 if (conn->cfg.verbose)
604 {
605 printf ("SERVER (fd %d): Replying to "
606 "cfg message!\n", client_fd);
607 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
608 }
609 sock_test_write (client_fd, (uint8_t *) & conn->cfg,
610 sizeof (conn->cfg), NULL,
611 conn->cfg.verbose);
612 continue;
613 }
614
615 switch (rx_cfg->test)
616 {
617 case SOCK_TEST_TYPE_NONE:
618 case SOCK_TEST_TYPE_ECHO:
619 sync_config_and_reply (conn, rx_cfg);
620 break;
621
622 case SOCK_TEST_TYPE_BI:
623 case SOCK_TEST_TYPE_UNI:
624 stream_test_server_start_stop (conn, rx_cfg);
625 break;
626
627 case SOCK_TEST_TYPE_EXIT:
628 printf ("SERVER: Have a great day, "
629 "connection %d!\n", client_fd);
630#ifdef VCL_TEST
631 vppcom_session_close (client_fd);
632#else
633 close (client_fd);
634#endif
635 conn_pool_free (conn);
Dave Wallaceee45d412017-11-24 21:44:06 -0500636 printf ("SERVER: Closed client fd %d\n", client_fd);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400637#if ! SOCK_SERVER_USE_EPOLL
Dave Wallace543852a2017-08-03 02:11:34 -0400638 if (ssm->nfds == (ssm->listen_fd + 1))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400639#else
640 ssm->nfds--;
641 if (!ssm->nfds)
642#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400643 {
644 printf ("SERVER: All client connections "
645 "closed.\n\nSERVER: "
646 "May the force be with you!\n\n");
647 goto done;
648 }
649 break;
650
651 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500652 fprintf (stderr,
653 "SERVER: ERROR: Unknown test type!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400654 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
655 break;
656 }
657 continue;
658 }
659
660 else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) ||
661 (conn->cfg.test == SOCK_TEST_TYPE_BI))
662 {
663 stream_test_server (conn, rx_bytes);
664 continue;
665 }
666
Chris Luke879ace32017-09-26 13:15:16 -0400667 else if (isascii (conn->buf[0]))
Chris Lukeab7b8d92017-09-07 07:40:13 -0400668 {
669 // If it looks vaguely like a string, make sure it's terminated
670 ((char *) conn->buf)[rx_bytes <
671 conn->buf_size ? rx_bytes :
672 conn->buf_size - 1] = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400673 printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
Chris Lukeab7b8d92017-09-07 07:40:13 -0400674 conn->fd, rx_bytes, conn->buf);
675 }
Dave Wallace543852a2017-08-03 02:11:34 -0400676 }
677 else // rx_bytes < 0
678 {
679 if (errno == ECONNRESET)
680 {
681 printf ("\nSERVER: Connection reset by remote peer.\n"
682 " Y'all have a great day now!\n\n");
683 break;
684 }
685 else
686 continue;
687 }
688
Chris Luke879ace32017-09-26 13:15:16 -0400689 if (isascii (conn->buf[0]))
Dave Wallace543852a2017-08-03 02:11:34 -0400690 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500691 /* If it looks vaguely like a string,
692 * make sure it's terminated
693 */
Chris Luke879ace32017-09-26 13:15:16 -0400694 ((char *) conn->buf)[rx_bytes <
695 conn->buf_size ? rx_bytes :
696 conn->buf_size - 1] = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400697 if (xtra)
Dave Wallace048b1d62018-01-03 22:24:41 -0500698 fprintf (stderr, "SERVER: ERROR: "
699 "FIFO not drained in previous test!\n"
Dave Wallace543852a2017-08-03 02:11:34 -0400700 " extra chunks %u (0x%x)\n"
701 " extra bytes %lu (0x%lx)\n",
702 xtra, xtra, xtra_bytes, xtra_bytes);
703
704 xtra = 0;
705 xtra_bytes = 0;
706
707 if (conn->cfg.verbose)
708 printf ("SERVER (fd %d): Echoing back\n", client_fd);
709
710 nbytes = strlen ((const char *) conn->buf) + 1;
711
712 tx_bytes = sock_test_write (client_fd, conn->buf,
713 nbytes, &conn->stats,
714 conn->cfg.verbose);
715 if (tx_bytes >= 0)
716 printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
717 conn->fd, tx_bytes, conn->buf);
718 }
719
720 else // Extraneous read data from non-echo tests???
721 {
722 xtra++;
723 xtra_bytes += rx_bytes;
724 }
725 }
726 }
727 }
728
729done:
730#ifdef VCL_TEST
731 vppcom_session_close (ssm->listen_fd);
732 vppcom_app_destroy ();
733#else
734 close (ssm->listen_fd);
735#endif
736 if (ssm->conn_pool)
737 free (ssm->conn_pool);
738
739 return main_rv;
740}
741
742/*
743 * fd.io coding-style-patch-verification: ON
744 *
745 * Local Variables:
746 * eval: (c-set-style "gnu")
747 * End:
748 */