blob: a184d995372595176b2ebdd7c8605406e936bab9 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallacee4d5a652018-06-24 21:21:21 -04002 * Copyright (c) 2017-2018 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * 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
Dave Wallacee4d5a652018-06-24 21:21:21 -040016#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 <sys/stat.h>
25#include <fcntl.h>
26#include <vcl/vcl_test.h>
27#include <sys/epoll.h>
Dave Barach6a5adc32018-07-04 10:56:23 -040028#include <vppinfra/mem.h>
Florin Coras293aa052018-08-30 18:49:13 -070029#include <pthread.h>
Dave Wallace543852a2017-08-03 02:11:34 -040030
Dave Wallacee4d5a652018-06-24 21:21:21 -040031typedef struct
32{
33 uint8_t is_alloc;
34 int fd;
35 uint8_t *buf;
36 uint32_t buf_size;
37 sock_test_cfg_t cfg;
38 sock_test_stats_t stats;
39 vppcom_endpt_t endpt;
40 uint8_t ip[16];
Florin Coras2cba8532018-09-11 16:33:36 -070041 vppcom_data_segments_t ds;
Florin Coras293aa052018-08-30 18:49:13 -070042} vcl_test_server_conn_t;
Dave Wallacee4d5a652018-06-24 21:21:21 -040043
44typedef struct
45{
Florin Coras293aa052018-08-30 18:49:13 -070046 uint16_t port;
Dave Wallacee4d5a652018-06-24 21:21:21 -040047 uint32_t address_ip6;
Florin Coras293aa052018-08-30 18:49:13 -070048 u8 proto;
49 u8 workers;
50 vppcom_endpt_t endpt;
51} vcl_test_server_cfg_t;
Dave Wallacee4d5a652018-06-24 21:21:21 -040052
Florin Coras293aa052018-08-30 18:49:13 -070053#define SOCK_SERVER_MAX_TEST_CONN 16
54#define SOCK_SERVER_MAX_EPOLL_EVENTS 16
55
Dave Wallacee4d5a652018-06-24 21:21:21 -040056typedef struct
57{
Florin Coras293aa052018-08-30 18:49:13 -070058 uint32_t wrk_index;
Dave Wallacee4d5a652018-06-24 21:21:21 -040059 int listen_fd;
Dave Wallacee4d5a652018-06-24 21:21:21 -040060 int epfd;
Dave Wallacee4d5a652018-06-24 21:21:21 -040061 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
Dave Wallacee4d5a652018-06-24 21:21:21 -040062 size_t conn_pool_size;
Florin Coras293aa052018-08-30 18:49:13 -070063 vcl_test_server_conn_t *conn_pool;
Dave Wallacee4d5a652018-06-24 21:21:21 -040064 int nfds;
Florin Coras293aa052018-08-30 18:49:13 -070065 pthread_t thread_handle;
66} vcl_test_server_worker_t;
Dave Wallacee4d5a652018-06-24 21:21:21 -040067
Florin Coras293aa052018-08-30 18:49:13 -070068typedef struct
69{
70 vcl_test_server_cfg_t cfg;
71 vcl_test_server_worker_t *workers;
72
73 struct sockaddr_storage servaddr;
74 volatile int worker_fails;
75 volatile int active_workers;
Florin Coras2cba8532018-09-11 16:33:36 -070076 u8 use_ds;
Florin Coras293aa052018-08-30 18:49:13 -070077} vcl_test_server_main_t;
78
79static __thread int __wrk_index = 0;
80
81static vcl_test_server_main_t sock_server_main;
82
Dave Wallacee4d5a652018-06-24 21:21:21 -040083static inline void
Florin Coras293aa052018-08-30 18:49:13 -070084conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
Dave Wallacee4d5a652018-06-24 21:21:21 -040085{
Florin Coras293aa052018-08-30 18:49:13 -070086 vcl_test_server_conn_t *conn_pool;
87 size_t new_size = wrk->conn_pool_size + expand_size;
Dave Wallacee4d5a652018-06-24 21:21:21 -040088 int i;
89
Florin Coras293aa052018-08-30 18:49:13 -070090 conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
Dave Wallacee4d5a652018-06-24 21:21:21 -040091 if (conn_pool)
92 {
Florin Coras293aa052018-08-30 18:49:13 -070093 for (i = wrk->conn_pool_size; i < new_size; i++)
Dave Wallacee4d5a652018-06-24 21:21:21 -040094 {
Florin Coras293aa052018-08-30 18:49:13 -070095 vcl_test_server_conn_t *conn = &conn_pool[i];
Dave Wallacee4d5a652018-06-24 21:21:21 -040096 memset (conn, 0, sizeof (*conn));
97 sock_test_cfg_init (&conn->cfg);
98 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
99 &conn->buf, &conn->buf_size);
100 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
101 }
102
Florin Coras293aa052018-08-30 18:49:13 -0700103 wrk->conn_pool = conn_pool;
104 wrk->conn_pool_size = new_size;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400105 }
106 else
107 {
Florin Coras293aa052018-08-30 18:49:13 -0700108 vterr ("conn_pool_expand()", -errno);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400109 }
110}
111
Florin Coras293aa052018-08-30 18:49:13 -0700112static inline vcl_test_server_conn_t *
113conn_pool_alloc (vcl_test_server_worker_t * wrk)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400114{
Dave Wallacee4d5a652018-06-24 21:21:21 -0400115 int i;
116
Florin Coras293aa052018-08-30 18:49:13 -0700117 for (i = 0; i < wrk->conn_pool_size; i++)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400118 {
Florin Coras293aa052018-08-30 18:49:13 -0700119 if (!wrk->conn_pool[i].is_alloc)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400120 {
Florin Coras293aa052018-08-30 18:49:13 -0700121 wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip;
122 wrk->conn_pool[i].is_alloc = 1;
123 return (&wrk->conn_pool[i]);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400124 }
125 }
126
127 return 0;
128}
129
130static inline void
Florin Coras293aa052018-08-30 18:49:13 -0700131conn_pool_free (vcl_test_server_conn_t * conn)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400132{
133 conn->fd = 0;
134 conn->is_alloc = 0;
135}
136
137static inline void
Florin Coras293aa052018-08-30 18:49:13 -0700138sync_config_and_reply (vcl_test_server_conn_t * conn,
139 sock_test_cfg_t * rx_cfg)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400140{
141 conn->cfg = *rx_cfg;
142 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
143 &conn->buf, &conn->buf_size);
144 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
145
146 if (conn->cfg.verbose)
147 {
Florin Coras293aa052018-08-30 18:49:13 -0700148 vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400149 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
150 }
151 (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
152 sizeof (conn->cfg), NULL, conn->cfg.verbose);
153}
154
155static void
Florin Coras6d4bb422018-09-04 22:07:27 -0700156vts_server_start_stop (vcl_test_server_worker_t * wrk,
157 vcl_test_server_conn_t * conn,
158 sock_test_cfg_t * rx_cfg)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400159{
Florin Coras6d4bb422018-09-04 22:07:27 -0700160 u8 is_bi = rx_cfg->test == SOCK_TEST_TYPE_BI;
161 int client_fd = conn->fd, i;
162 vcl_test_server_conn_t *tc;
163 char buf[64];
Dave Wallacee4d5a652018-06-24 21:21:21 -0400164
165 if (rx_cfg->ctrl_handle == conn->fd)
166 {
Dave Wallacee4d5a652018-06-24 21:21:21 -0400167 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
168
Florin Coras293aa052018-08-30 18:49:13 -0700169 for (i = 0; i < wrk->conn_pool_size; i++)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400170 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700171 tc = &wrk->conn_pool[i];
172 if (tc->cfg.ctrl_handle != conn->fd)
173 continue;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400174
Florin Coras6d4bb422018-09-04 22:07:27 -0700175 sock_test_stats_accumulate (&conn->stats, &tc->stats);
176
177 if (conn->cfg.verbose)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400178 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700179 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
180 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
181 is_bi /* show tx */ , conn->cfg.verbose);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400182 }
183 }
184
185 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
Florin Coras6d4bb422018-09-04 22:07:27 -0700186 is_bi /* show_tx */ , conn->cfg.verbose);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400187 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
188 if (conn->cfg.verbose)
189 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700190 vtinf (" sock server main\n"
191 SOCK_TEST_SEPARATOR_STRING
192 " buf: %p\n"
193 " buf size: %u (0x%08x)\n"
194 SOCK_TEST_SEPARATOR_STRING,
195 conn->buf, conn->buf_size, conn->buf_size);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400196 }
197
198 sync_config_and_reply (conn, rx_cfg);
Florin Coras6d4bb422018-09-04 22:07:27 -0700199 vtinf ("(fd %d): %s-directional Stream Test Complete!\n"
200 SOCK_TEST_BANNER_STRING "\n", conn->fd, is_bi ? "Bi" : "Uni");
Florin Coras2cba8532018-09-11 16:33:36 -0700201 memset (&conn->stats, 0, sizeof (conn->stats));
Dave Wallacee4d5a652018-06-24 21:21:21 -0400202 }
203 else
204 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700205 vtinf (SOCK_TEST_BANNER_STRING "(fd %d): %s-directional Stream Test!\n"
206 " Sending client the test cfg to start streaming data...\n",
207 client_fd, is_bi ? "Bi" : "Uni");
Dave Wallacee4d5a652018-06-24 21:21:21 -0400208
Florin Coras6d4bb422018-09-04 22:07:27 -0700209 if (rx_cfg->ctrl_handle == ~0)
210 rx_cfg->ctrl_handle = conn->fd;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400211
212 sync_config_and_reply (conn, rx_cfg);
213
214 /* read the 1st chunk, record start time */
215 memset (&conn->stats, 0, sizeof (conn->stats));
216 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
217 }
218}
219
Dave Wallacee4d5a652018-06-24 21:21:21 -0400220static inline void
Florin Coras6d4bb422018-09-04 22:07:27 -0700221vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400222{
Florin Coras2cba8532018-09-11 16:33:36 -0700223 vcl_test_server_main_t *vts = &sock_server_main;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400224 int client_fd = conn->fd;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400225
Florin Coras2cba8532018-09-11 16:33:36 -0700226 if (conn->cfg.test == SOCK_TEST_TYPE_BI)
227 {
228 if (vts->use_ds)
229 {
230 (void) vcl_test_write (client_fd, conn->ds[0].data, conn->ds[0].len,
231 &conn->stats, conn->cfg.verbose);
232 if (conn->ds[1].len)
233 (void) vcl_test_write (client_fd, conn->ds[1].data,
234 conn->ds[1].len, &conn->stats,
235 conn->cfg.verbose);
236 }
237 else
238 (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
239 conn->cfg.verbose);
240 }
241
242 if (vts->use_ds)
243 vppcom_session_free_segments (conn->fd, conn->ds);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400244
245 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
Florin Coras6d4bb422018-09-04 22:07:27 -0700246 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400247}
248
Florin Coras293aa052018-08-30 18:49:13 -0700249static void
Florin Coras6d4bb422018-09-04 22:07:27 -0700250vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400251{
Florin Coras2cba8532018-09-11 16:33:36 -0700252 vcl_test_server_main_t *vts = &sock_server_main;
Florin Coras293aa052018-08-30 18:49:13 -0700253 int tx_bytes, nbytes, pos;
254
Florin Coras2cba8532018-09-11 16:33:36 -0700255 if (vts->use_ds)
256 vppcom_data_segment_copy (conn->buf, conn->ds, rx_bytes);
257
258 /* If it looks vaguely like a string, make sure it's terminated */
Florin Coras293aa052018-08-30 18:49:13 -0700259 pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
260 ((char *) conn->buf)[pos] = 0;
261 vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->buf);
262
263 if (conn->cfg.verbose)
264 vtinf ("(fd %d): Echoing back", conn->fd);
265
266 nbytes = strlen ((const char *) conn->buf) + 1;
267 tx_bytes = vcl_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
268 conn->cfg.verbose);
269 if (tx_bytes >= 0)
270 vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf);
271}
272
273static inline void
Florin Coras6d4bb422018-09-04 22:07:27 -0700274vts_new_client (vcl_test_server_worker_t * wrk)
Florin Coras293aa052018-08-30 18:49:13 -0700275{
Dave Wallacee4d5a652018-06-24 21:21:21 -0400276 int client_fd;
Florin Coras293aa052018-08-30 18:49:13 -0700277 vcl_test_server_conn_t *conn;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400278
Florin Coras293aa052018-08-30 18:49:13 -0700279 conn = conn_pool_alloc (wrk);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400280 if (!conn)
281 {
Florin Coras293aa052018-08-30 18:49:13 -0700282 vtwrn ("No free connections!");
Dave Wallacee4d5a652018-06-24 21:21:21 -0400283 return;
284 }
285
Florin Coras293aa052018-08-30 18:49:13 -0700286 client_fd = vppcom_session_accept (wrk->listen_fd, &conn->endpt, 0);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400287 if (client_fd < 0)
288 {
Florin Coras293aa052018-08-30 18:49:13 -0700289 vterr ("vppcom_session_accept()", client_fd);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400290 return;
291 }
292
Florin Coras293aa052018-08-30 18:49:13 -0700293 vtinf ("Got a connection -- fd = %d (0x%08x)!", client_fd, client_fd);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400294
295 conn->fd = client_fd;
296
297 {
298 struct epoll_event ev;
299 int rv;
300
301 ev.events = EPOLLIN;
Florin Coras293aa052018-08-30 18:49:13 -0700302 ev.data.u64 = conn - wrk->conn_pool;
303 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400304 if (rv < 0)
305 {
Florin Coras293aa052018-08-30 18:49:13 -0700306 vterr ("vppcom_epoll_ctl()", rv);
307 return;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400308 }
Florin Coras293aa052018-08-30 18:49:13 -0700309 wrk->nfds++;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400310 }
311}
312
313void
314print_usage_and_exit (void)
315{
316 fprintf (stderr,
317 "sock_test_server [OPTIONS] <port>\n"
318 " OPTIONS\n"
319 " -h Print this message and exit.\n"
320 " -6 Use IPv6\n"
Florin Coras293aa052018-08-30 18:49:13 -0700321 " -w <num> Number of workers\n"
Dave Wallacee4d5a652018-06-24 21:21:21 -0400322 " -u Use UDP transport layer\n");
323 exit (1);
324}
325
Florin Coras293aa052018-08-30 18:49:13 -0700326static void
327vcl_test_init_endpoint_addr (vcl_test_server_main_t * ssm)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400328{
Florin Coras293aa052018-08-30 18:49:13 -0700329 struct sockaddr_storage *servaddr = &ssm->servaddr;
330 memset (servaddr, 0, sizeof (*servaddr));
Dave Wallacee4d5a652018-06-24 21:21:21 -0400331
Florin Coras293aa052018-08-30 18:49:13 -0700332 if (ssm->cfg.address_ip6)
333 {
334 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
335 server_addr->sin6_family = AF_INET6;
336 server_addr->sin6_addr = in6addr_any;
337 server_addr->sin6_port = htons (ssm->cfg.port);
338 }
339 else
340 {
341 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
342 server_addr->sin_family = AF_INET;
343 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
344 server_addr->sin_port = htons (ssm->cfg.port);
345 }
346
347 if (ssm->cfg.address_ip6)
348 {
349 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
350 ssm->cfg.endpt.is_ip4 = 0;
351 ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr;
352 ssm->cfg.endpt.port = (uint16_t) server_addr->sin6_port;
353 }
354 else
355 {
356 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
357 ssm->cfg.endpt.is_ip4 = 1;
358 ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr;
359 ssm->cfg.endpt.port = (uint16_t) server_addr->sin_port;
360 }
361}
362
Florin Coras6d4bb422018-09-04 22:07:27 -0700363static void
Florin Coras293aa052018-08-30 18:49:13 -0700364vcl_test_server_process_opts (vcl_test_server_main_t * ssm, int argc,
365 char **argv)
366{
367 int v, c;
368
369 ssm->cfg.proto = VPPCOM_PROTO_TCP;
Dave Barach6a5adc32018-07-04 10:56:23 -0400370
Dave Wallacee4d5a652018-06-24 21:21:21 -0400371 opterr = 0;
Florin Coras2cba8532018-09-11 16:33:36 -0700372 while ((c = getopt (argc, argv, "6Dsw:")) != -1)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400373 switch (c)
374 {
375 case '6':
376 ssm->cfg.address_ip6 = 1;
377 break;
378
379 case 'D':
Florin Coras293aa052018-08-30 18:49:13 -0700380 ssm->cfg.proto = VPPCOM_PROTO_UDP;
381 break;
382
383 case 'w':
384 v = atoi (optarg);
385 if (v > 1)
386 ssm->cfg.workers = v;
387 else
388 vtwrn ("Invalid number of workers %d", v);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400389 break;
Florin Coras2cba8532018-09-11 16:33:36 -0700390 case 's':
391 ssm->use_ds = 1;
392 break;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400393 case '?':
394 switch (optopt)
395 {
396 default:
397 if (isprint (optopt))
Florin Coras6d4bb422018-09-04 22:07:27 -0700398 vtwrn ("Unknown option `-%c'.", optopt);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400399 else
Florin Coras6d4bb422018-09-04 22:07:27 -0700400 vtwrn ("Unknown option character `\\x%x'.", optopt);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400401 }
402 /* fall thru */
403 case 'h':
404 default:
405 print_usage_and_exit ();
406 }
407
408 if (argc < (optind + 1))
409 {
410 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
411 print_usage_and_exit ();
412 }
413
414 if (sscanf (argv[optind], "%d", &v) == 1)
Florin Coras293aa052018-08-30 18:49:13 -0700415 ssm->cfg.port = (uint16_t) v;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400416 else
417 {
418 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
419 print_usage_and_exit ();
420 }
421
Florin Coras293aa052018-08-30 18:49:13 -0700422 vcl_test_init_endpoint_addr (ssm);
423}
Dave Wallacee4d5a652018-06-24 21:21:21 -0400424
Florin Coras293aa052018-08-30 18:49:13 -0700425int
Florin Coras6d4bb422018-09-04 22:07:27 -0700426vts_handle_cfg (vcl_test_server_worker_t * wrk, sock_test_cfg_t * rx_cfg,
427 vcl_test_server_conn_t * conn, int rx_bytes)
Florin Coras293aa052018-08-30 18:49:13 -0700428{
429 if (rx_cfg->verbose)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400430 {
Florin Coras293aa052018-08-30 18:49:13 -0700431 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
432 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
Dave Wallacee4d5a652018-06-24 21:21:21 -0400433 }
434
Florin Coras293aa052018-08-30 18:49:13 -0700435 if (rx_bytes != sizeof (*rx_cfg))
Dave Wallacee4d5a652018-06-24 21:21:21 -0400436 {
Florin Coras293aa052018-08-30 18:49:13 -0700437 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
438 rx_bytes, sizeof (*rx_cfg));
439 conn->cfg.rxbuf_size = 0;
440 conn->cfg.num_writes = 0;
441 if (conn->cfg.verbose)
Florin Coras460dce62018-07-27 05:45:06 -0700442 {
Florin Coras293aa052018-08-30 18:49:13 -0700443 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
444 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
Florin Coras460dce62018-07-27 05:45:06 -0700445 }
Florin Coras293aa052018-08-30 18:49:13 -0700446 vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
447 sizeof (conn->cfg), NULL, conn->cfg.verbose);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400448 return -1;
449 }
450
Florin Coras293aa052018-08-30 18:49:13 -0700451 switch (rx_cfg->test)
452 {
453 case SOCK_TEST_TYPE_NONE:
454 case SOCK_TEST_TYPE_ECHO:
455 sync_config_and_reply (conn, rx_cfg);
456 break;
457
458 case SOCK_TEST_TYPE_BI:
459 case SOCK_TEST_TYPE_UNI:
Florin Coras6d4bb422018-09-04 22:07:27 -0700460 vts_server_start_stop (wrk, conn, rx_cfg);
Florin Coras293aa052018-08-30 18:49:13 -0700461 break;
462
463 case SOCK_TEST_TYPE_EXIT:
Florin Corasab2f6db2018-08-31 14:31:41 -0700464 vtinf ("Have a great day conn %d (closing)!", conn->fd);
Florin Coras293aa052018-08-30 18:49:13 -0700465 vppcom_session_close (conn->fd);
466 conn_pool_free (conn);
Florin Coras293aa052018-08-30 18:49:13 -0700467 wrk->nfds--;
468 break;
469
470 default:
471 vtwrn ("Unknown test type %d", rx_cfg->test);
472 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
473 break;
474 }
475
476 return 0;
477}
478
479static void
Florin Coras6d4bb422018-09-04 22:07:27 -0700480vts_worker_init (vcl_test_server_worker_t * wrk)
Florin Coras293aa052018-08-30 18:49:13 -0700481{
482 vcl_test_server_main_t *ssm = &sock_server_main;
483 struct epoll_event listen_ev;
484 int rv;
485
486 __wrk_index = wrk->wrk_index;
487
488 vtinf ("Initializing worker ...");
489
490 conn_pool_expand (wrk, SOCK_SERVER_MAX_TEST_CONN + 1);
491 if (wrk->wrk_index)
Florin Corasde9f08b2018-09-07 14:32:58 -0700492 if (vppcom_worker_register ())
493 vtfail ("vppcom_worker_register()", 1);
Florin Coras293aa052018-08-30 18:49:13 -0700494
495 wrk->listen_fd = vppcom_session_create (ssm->cfg.proto,
496 0 /* is_nonblocking */ );
497 if (wrk->listen_fd < 0)
498 vtfail ("vppcom_session_create()", wrk->listen_fd);
499
500 rv = vppcom_session_bind (wrk->listen_fd, &ssm->cfg.endpt);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400501 if (rv < 0)
Florin Coras293aa052018-08-30 18:49:13 -0700502 vtfail ("vppcom_session_bind()", rv);
503
504 if (!(ssm->cfg.proto == VPPCOM_PROTO_UDP))
Dave Wallacee4d5a652018-06-24 21:21:21 -0400505 {
Florin Coras293aa052018-08-30 18:49:13 -0700506 rv = vppcom_session_listen (wrk->listen_fd, 10);
507 if (rv < 0)
508 vtfail ("vppcom_session_listen()", rv);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400509 }
Florin Coras293aa052018-08-30 18:49:13 -0700510
511 wrk->epfd = vppcom_epoll_create ();
512 if (wrk->epfd < 0)
513 vtfail ("vppcom_epoll_create()", wrk->epfd);
514
515 listen_ev.events = EPOLLIN;
516 listen_ev.data.u32 = ~0;
517 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listen_fd,
518 &listen_ev);
519 if (rv < 0)
520 vtfail ("vppcom_epoll_ctl", rv);
521
Florin Coras21795132018-09-09 09:40:51 -0700522 ssm->active_workers += 1;
Florin Coras293aa052018-08-30 18:49:13 -0700523 vtinf ("Waiting for a client to connect on port %d ...", ssm->cfg.port);
524}
525
Florin Coras2cba8532018-09-11 16:33:36 -0700526static int
527vts_conn_expect_config (vcl_test_server_conn_t * conn)
528{
529 if (conn->cfg.test == SOCK_TEST_TYPE_ECHO)
530 return 1;
531
532 return (conn->stats.rx_bytes < 128
533 || conn->stats.rx_bytes > conn->cfg.total_bytes);
534}
535
536static sock_test_cfg_t *
537vts_conn_read_config (vcl_test_server_conn_t * conn)
538{
539 vcl_test_server_main_t *vts = &sock_server_main;
540
541 if (vts->use_ds)
542 {
543 /* We could avoid the copy if the first segment is big enough but this
544 * just simplifies things */
545 vppcom_data_segment_copy (conn->buf, conn->ds,
546 sizeof (sock_test_cfg_t));
547 vppcom_session_free_segments (conn->fd, conn->ds);
548 }
549 return (sock_test_cfg_t *) conn->buf;
550}
551
552static inline int
553vts_conn_read (vcl_test_server_conn_t * conn)
554{
555 vcl_test_server_main_t *vts = &sock_server_main;
556 if (vts->use_ds)
557 return vcl_test_read_ds (conn->fd, conn->ds, &conn->stats);
558 else
559 return vcl_test_read (conn->fd, conn->buf, conn->buf_size, &conn->stats);
560}
561
562static inline int
563vts_conn_has_ascii (vcl_test_server_conn_t * conn)
564{
565 vcl_test_server_main_t *vts = &sock_server_main;
566
567 if (vts->use_ds)
568 return isascii (conn->ds[0].data[0]);
569 else
570 return isascii (conn->buf[0]);
571}
572
Florin Coras293aa052018-08-30 18:49:13 -0700573static void *
Florin Coras6d4bb422018-09-04 22:07:27 -0700574vts_worker_loop (void *arg)
Florin Coras293aa052018-08-30 18:49:13 -0700575{
576 vcl_test_server_main_t *ssm = &sock_server_main;
577 vcl_test_server_worker_t *wrk = arg;
578 vcl_test_server_conn_t *conn;
579 int i, rx_bytes, num_ev;
580 sock_test_cfg_t *rx_cfg;
581
582 if (wrk->wrk_index)
Florin Coras6d4bb422018-09-04 22:07:27 -0700583 vts_worker_init (wrk);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400584
585 while (1)
586 {
Florin Coras293aa052018-08-30 18:49:13 -0700587 num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events,
Florin Coras54693d22018-07-17 10:46:29 -0700588 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000.0);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400589 if (num_ev < 0)
590 {
Florin Coras293aa052018-08-30 18:49:13 -0700591 vterr ("vppcom_epoll_wait()", num_ev);
592 goto fail;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400593 }
594 else if (num_ev == 0)
595 {
Florin Coras293aa052018-08-30 18:49:13 -0700596 vtinf ("vppcom_epoll_wait() timeout!");
Dave Wallacee4d5a652018-06-24 21:21:21 -0400597 continue;
598 }
599 for (i = 0; i < num_ev; i++)
600 {
Florin Coras293aa052018-08-30 18:49:13 -0700601 conn = &wrk->conn_pool[wrk->wait_events[i].data.u32];
602 if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
Dave Wallacee4d5a652018-06-24 21:21:21 -0400603 {
604 vppcom_session_close (conn->fd);
Florin Coras21795132018-09-09 09:40:51 -0700605 wrk->nfds -= 1;
606 if (!wrk->nfds)
607 {
608 vtinf ("All client connections closed\n");
609 goto done;
610 }
Dave Wallacee4d5a652018-06-24 21:21:21 -0400611 continue;
612 }
Florin Coras293aa052018-08-30 18:49:13 -0700613 if (wrk->wait_events[i].data.u32 == ~0)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400614 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700615 vts_new_client (wrk);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400616 continue;
617 }
Dave Wallacee4d5a652018-06-24 21:21:21 -0400618
Florin Coras293aa052018-08-30 18:49:13 -0700619 if (EPOLLIN & wrk->wait_events[i].events)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400620 {
Florin Coras21795132018-09-09 09:40:51 -0700621 read_again:
Florin Coras2cba8532018-09-11 16:33:36 -0700622 rx_bytes = vts_conn_read (conn);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400623
Florin Coras293aa052018-08-30 18:49:13 -0700624 if (rx_bytes <= 0)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400625 {
626 if (errno == ECONNRESET)
627 {
Florin Coras293aa052018-08-30 18:49:13 -0700628 vtinf ("Connection reset by remote peer.\n");
629 goto fail;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400630 }
631 else
632 continue;
633 }
634
Florin Coras2cba8532018-09-11 16:33:36 -0700635 if (vts_conn_expect_config (conn))
Dave Wallacee4d5a652018-06-24 21:21:21 -0400636 {
Florin Coras2cba8532018-09-11 16:33:36 -0700637 rx_cfg = vts_conn_read_config (conn);
638 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
Florin Coras293aa052018-08-30 18:49:13 -0700639 {
Florin Coras2cba8532018-09-11 16:33:36 -0700640 vts_handle_cfg (wrk, rx_cfg, conn, rx_bytes);
641 if (!wrk->nfds)
642 {
643 vtinf ("All client connections closed\n");
644 goto done;
645 }
646 continue;
Florin Coras293aa052018-08-30 18:49:13 -0700647 }
Dave Wallacee4d5a652018-06-24 21:21:21 -0400648 }
Florin Coras41c9e042018-09-11 00:10:41 -0700649 if ((conn->cfg.test == SOCK_TEST_TYPE_UNI)
650 || (conn->cfg.test == SOCK_TEST_TYPE_BI))
Dave Wallacee4d5a652018-06-24 21:21:21 -0400651 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700652 vts_server_rx (conn, rx_bytes);
Florin Coras21795132018-09-09 09:40:51 -0700653 if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0,
654 0) > 0)
655 goto read_again;
Florin Coras293aa052018-08-30 18:49:13 -0700656 continue;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400657 }
Florin Coras2cba8532018-09-11 16:33:36 -0700658 if (vts_conn_has_ascii (conn))
Florin Coras293aa052018-08-30 18:49:13 -0700659 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700660 vts_server_echo (conn, rx_bytes);
Florin Coras293aa052018-08-30 18:49:13 -0700661 }
662 else
663 {
664 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
665 }
666 }
667 else
668 {
669 vtwrn ("Unhandled event");
670 goto fail;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400671 }
672 }
673 }
674
Florin Coras293aa052018-08-30 18:49:13 -0700675fail:
676 ssm->worker_fails -= 1;
677
Dave Wallacee4d5a652018-06-24 21:21:21 -0400678done:
Florin Coras293aa052018-08-30 18:49:13 -0700679 vppcom_session_close (wrk->listen_fd);
680 if (wrk->conn_pool)
681 free (wrk->conn_pool);
682 ssm->active_workers -= 1;
683 return 0;
684}
685
686int
687main (int argc, char **argv)
688{
Florin Coras6d4bb422018-09-04 22:07:27 -0700689 vcl_test_server_main_t *vsm = &sock_server_main;
Florin Coras293aa052018-08-30 18:49:13 -0700690 int rv, i;
691
692 clib_mem_init_thread_safe (0, 64 << 20);
Florin Coras6d4bb422018-09-04 22:07:27 -0700693 vsm->cfg.port = SOCK_TEST_SERVER_PORT;
694 vsm->cfg.workers = 1;
Florin Coras2cba8532018-09-11 16:33:36 -0700695 vsm->active_workers = 0;
Florin Coras6d4bb422018-09-04 22:07:27 -0700696 vcl_test_server_process_opts (vsm, argc, argv);
Florin Coras293aa052018-08-30 18:49:13 -0700697
698 rv = vppcom_app_create ("vcl_test_server");
699 if (rv)
700 vtfail ("vppcom_app_create()", rv);
701
Florin Coras6d4bb422018-09-04 22:07:27 -0700702 vsm->workers = calloc (vsm->cfg.workers, sizeof (*vsm->workers));
703 vts_worker_init (&vsm->workers[0]);
704 for (i = 1; i < vsm->cfg.workers; i++)
Florin Coras293aa052018-08-30 18:49:13 -0700705 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700706 vsm->workers[i].wrk_index = i;
707 rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
708 vts_worker_loop, (void *) &vsm->workers[i]);
Florin Coras293aa052018-08-30 18:49:13 -0700709 }
Florin Coras6d4bb422018-09-04 22:07:27 -0700710 vts_worker_loop (&vsm->workers[0]);
Florin Coras293aa052018-08-30 18:49:13 -0700711
Florin Coras6d4bb422018-09-04 22:07:27 -0700712 while (vsm->active_workers > 0)
Florin Coras293aa052018-08-30 18:49:13 -0700713 ;
714
Dave Wallacee4d5a652018-06-24 21:21:21 -0400715 vppcom_app_destroy ();
Florin Coras6d4bb422018-09-04 22:07:27 -0700716 free (vsm->workers);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400717
Florin Coras6d4bb422018-09-04 22:07:27 -0700718 return vsm->worker_fails;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400719}
Dave Wallace543852a2017-08-03 02:11:34 -0400720
721/*
722 * fd.io coding-style-patch-verification: ON
723 *
724 * Local Variables:
725 * eval: (c-set-style "gnu")
726 * End:
727 */