blob: c92f0cb754d61ea154b87aecca94078988c3dbd4 [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 <stdlib.h>
19#include <ctype.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <stdio.h>
23#include <time.h>
24#include <arpa/inet.h>
25#include <vcl/vcl_test.h>
Florin Coras6d4bb422018-09-04 22:07:27 -070026#include <pthread.h>
Dave Wallace543852a2017-08-03 02:11:34 -040027
Dave Wallacee4d5a652018-06-24 21:21:21 -040028typedef struct
29{
Florin Coras6d4bb422018-09-04 22:07:27 -070030 sock_test_socket_t *sessions;
31 uint32_t n_sessions;
32 uint32_t wrk_index;
33 fd_set wr_fdset;
34 fd_set rd_fdset;
35 int max_fd_index;
36 pthread_t thread_handle;
37 sock_test_cfg_t cfg;
38} vcl_test_client_worker_t;
39
40typedef struct
41{
42 vcl_test_client_worker_t *workers;
Dave Wallacee4d5a652018-06-24 21:21:21 -040043 vppcom_endpt_t server_endpt;
Dave Wallacee4d5a652018-06-24 21:21:21 -040044 uint32_t cfg_seq_num;
45 sock_test_socket_t ctrl_socket;
Florin Coras6d4bb422018-09-04 22:07:27 -070046 sock_test_socket_t *sessions;
Dave Wallacee4d5a652018-06-24 21:21:21 -040047 uint8_t dump_cfg;
Florin Coras6d4bb422018-09-04 22:07:27 -070048 sock_test_t post_test;
49 uint32_t proto;
50 uint32_t n_workers;
51 volatile int active_workers;
52 struct sockaddr_storage server_addr;
53} vcl_test_client_main_t;
Dave Wallacee4d5a652018-06-24 21:21:21 -040054
Florin Coras6d4bb422018-09-04 22:07:27 -070055static __thread int __wrk_index = 0;
Dave Wallacee4d5a652018-06-24 21:21:21 -040056
Florin Coras6d4bb422018-09-04 22:07:27 -070057vcl_test_client_main_t vcl_client_main;
58
59#define vtc_min(a, b) (a < b ? a : b)
60#define vtc_max(a, b) (a > b ? a : b)
Dave Wallacee4d5a652018-06-24 21:21:21 -040061
62static int
Florin Coras6d4bb422018-09-04 22:07:27 -070063vtc_cfg_sync (sock_test_socket_t * sock)
Dave Wallacee4d5a652018-06-24 21:21:21 -040064{
Florin Coras6d4bb422018-09-04 22:07:27 -070065 vcl_test_client_main_t *vcm = &vcl_client_main;
66 sock_test_cfg_t *rx_cfg = (sock_test_cfg_t *) sock->rxbuf;
Dave Wallacee4d5a652018-06-24 21:21:21 -040067 int rx_bytes, tx_bytes;
68
Florin Coras6d4bb422018-09-04 22:07:27 -070069 vt_atomic_add (&sock->cfg.seq_num, 1);
70 if (sock->cfg.verbose)
Dave Wallacee4d5a652018-06-24 21:21:21 -040071 {
Florin Coras6d4bb422018-09-04 22:07:27 -070072 vtinf ("(fd %d): Sending config to server.", sock->fd);
73 sock_test_cfg_dump (&sock->cfg, 1 /* is_client */ );
Dave Wallacee4d5a652018-06-24 21:21:21 -040074 }
Florin Coras6d4bb422018-09-04 22:07:27 -070075 tx_bytes = vcl_test_write (sock->fd, (uint8_t *) & sock->cfg,
76 sizeof (sock->cfg), NULL, sock->cfg.verbose);
Dave Wallacee4d5a652018-06-24 21:21:21 -040077 if (tx_bytes < 0)
78 {
Florin Coras6d4bb422018-09-04 22:07:27 -070079 vtwrn ("(fd %d): write test cfg failed (%d)!", sock->fd, tx_bytes);
Dave Wallacee4d5a652018-06-24 21:21:21 -040080 return tx_bytes;
81 }
82
Florin Coras6d4bb422018-09-04 22:07:27 -070083 rx_bytes = vcl_test_read (sock->fd, (uint8_t *) sock->rxbuf,
Dave Wallacee4d5a652018-06-24 21:21:21 -040084 sizeof (sock_test_cfg_t), NULL);
85 if (rx_bytes < 0)
86 return rx_bytes;
87
Florin Coras6d4bb422018-09-04 22:07:27 -070088 if (rx_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC)
Dave Wallacee4d5a652018-06-24 21:21:21 -040089 {
Florin Coras6d4bb422018-09-04 22:07:27 -070090 vtwrn ("(fd %d): Bad server reply cfg -- aborting!", sock->fd);
Dave Wallacee4d5a652018-06-24 21:21:21 -040091 return -1;
92 }
93 if ((rx_bytes != sizeof (sock_test_cfg_t))
Florin Coras6d4bb422018-09-04 22:07:27 -070094 || !sock_test_cfg_verify (rx_cfg, &sock->cfg))
Dave Wallacee4d5a652018-06-24 21:21:21 -040095 {
Florin Coras6d4bb422018-09-04 22:07:27 -070096 vtwrn ("(fd %d): Invalid config received from server!", sock->fd);
Dave Wallacee4d5a652018-06-24 21:21:21 -040097 if (rx_bytes != sizeof (sock_test_cfg_t))
98 {
Florin Coras6d4bb422018-09-04 22:07:27 -070099 vtinf ("\tRx bytes %d != cfg size %lu", rx_bytes,
100 sizeof (sock_test_cfg_t));
Dave Wallacee4d5a652018-06-24 21:21:21 -0400101 }
102 else
103 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700104 sock_test_cfg_dump (rx_cfg, 1 /* is_client */ );
105 vtinf ("(fd %d): Valid config sent to server.", sock->fd);
106 sock_test_cfg_dump (&sock->cfg, 1 /* is_client */ );
Dave Wallacee4d5a652018-06-24 21:21:21 -0400107 }
108 return -1;
109 }
Florin Coras6d4bb422018-09-04 22:07:27 -0700110 if (sock->cfg.verbose)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400111 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700112 vtinf ("(fd %d): Got config back from server.", sock->fd);
113 sock_test_cfg_dump (rx_cfg, 1 /* is_client */ );
Dave Wallacee4d5a652018-06-24 21:21:21 -0400114 }
Dave Wallacee4d5a652018-06-24 21:21:21 -0400115
116 return 0;
117}
118
Dave Wallacee4d5a652018-06-24 21:21:21 -0400119static int
Florin Coras6d4bb422018-09-04 22:07:27 -0700120vtc_connect_test_sessions (vcl_test_client_worker_t * wrk)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400121{
Florin Coras6d4bb422018-09-04 22:07:27 -0700122 vcl_test_client_main_t *vcm = &vcl_client_main;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400123 sock_test_socket_t *tsock;
Florin Coras6d4bb422018-09-04 22:07:27 -0700124 uint32_t n_test_sessions;
125 int i, rv;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400126
Florin Coras6d4bb422018-09-04 22:07:27 -0700127 n_test_sessions = wrk->cfg.num_test_sockets;
128 if (n_test_sessions < 1)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400129 {
130 errno = EINVAL;
131 return -1;
132 }
133
Florin Coras6d4bb422018-09-04 22:07:27 -0700134 if (wrk->n_sessions >= n_test_sessions)
135 goto done;
136
137 if (wrk->n_sessions)
138 wrk->sessions = realloc (wrk->sessions,
139 n_test_sessions * sizeof (sock_test_socket_t));
140 else
141 wrk->sessions = calloc (n_test_sessions, sizeof (sock_test_socket_t));
142
143 if (!wrk->sessions)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400144 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700145 vterr ("failed to alloc sessions", -errno);
146 return errno;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400147 }
148
Florin Coras6d4bb422018-09-04 22:07:27 -0700149 for (i = 0; i < n_test_sessions; i++)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400150 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700151 tsock = &wrk->sessions[i];
152 tsock->fd = vppcom_session_create (vcm->proto, 1 /* is_nonblocking */ );
153 if (tsock->fd < 0)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400154 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700155 vterr ("vppcom_session_create()", tsock->fd);
156 return tsock->fd;
157 }
158
159 rv = vppcom_session_connect (tsock->fd, &vcm->server_endpt);
160 if (rv < 0)
161 {
162 vterr ("vppcom_session_connect()", rv);
163 return rv;
164 }
165 vtinf ("(fd %d): Test socket %d connected.", tsock->fd, i);
166 }
167 wrk->n_sessions = n_test_sessions;
168
169done:
170 vtinf ("All test sessions (%d) connected!", n_test_sessions);
171 return 0;
172}
173
174static int
175vtc_worker_test_setup (vcl_test_client_worker_t * wrk)
176{
177 vcl_test_client_main_t *vcm = &vcl_client_main;
178 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
179 sock_test_cfg_t *cfg = &wrk->cfg;
180 sock_test_socket_t *tsock;
181 uint32_t sidx;
182 int i, j;
183
184 FD_ZERO (&wrk->wr_fdset);
185 FD_ZERO (&wrk->rd_fdset);
186
187 for (i = 0; i < cfg->num_test_sockets; i++)
188 {
189 tsock = &wrk->sessions[i];
190 tsock->cfg = wrk->cfg;
191 sock_test_socket_buf_alloc (tsock);
192
193 switch (cfg->test)
194 {
195 case SOCK_TEST_TYPE_ECHO:
196 memcpy (tsock->txbuf, ctrl->txbuf, cfg->total_bytes);
197 break;
198 case SOCK_TEST_TYPE_UNI:
199 case SOCK_TEST_TYPE_BI:
200 for (j = 0; j < tsock->txbuf_size; j++)
201 tsock->txbuf[j] = j & 0xff;
202 break;
203 }
204
205 FD_SET (vppcom_session_index (tsock->fd), &wrk->wr_fdset);
206 FD_SET (vppcom_session_index (tsock->fd), &wrk->rd_fdset);
207 sidx = vppcom_session_index (tsock->fd);
208 wrk->max_fd_index = vtc_max (sidx, wrk->max_fd_index);
209 }
210 wrk->max_fd_index += 1;
211
212 return 0;
213}
214
215static int
216vtc_worker_init (vcl_test_client_worker_t * wrk)
217{
218 vcl_test_client_main_t *vcm = &vcl_client_main;
219 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
220 sock_test_cfg_t *cfg = &wrk->cfg;
221 sock_test_socket_t *tsock;
222 uint32_t i, n;
223 int rv, nbytes;
224
225 __wrk_index = wrk->wrk_index;
226
227 vtinf ("Initializing worker ...");
228
229 if (wrk->wrk_index)
230 {
231 if (vppcom_worker_register ())
232 {
233 vtwrn ("failed to register worker");
Dave Wallacee4d5a652018-06-24 21:21:21 -0400234 return -1;
235 }
Florin Coras6d4bb422018-09-04 22:07:27 -0700236 vt_atomic_add (&vcm->active_workers, 1);
237 }
238 rv = vtc_connect_test_sessions (wrk);
239 if (rv)
240 {
241 vterr ("vtc_connect_test_sockets ()", rv);
242 return rv;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400243 }
244
Florin Coras6d4bb422018-09-04 22:07:27 -0700245 if (vtc_worker_test_setup (wrk))
246 return -1;
247
248 vtinf ("Sending config to server on all sessions ...");
249
250 for (n = 0; n < cfg->num_test_sockets; n++)
251 {
252 tsock = &wrk->sessions[n];
253 if (vtc_cfg_sync (tsock))
254 return -1;
255 memset (&tsock->stats, 0, sizeof (tsock->stats));
256 }
257
Dave Wallacee4d5a652018-06-24 21:21:21 -0400258 return 0;
259}
260
Florin Coras21795132018-09-09 09:40:51 -0700261static int stats_lock = 0;
262
263static void
264vtc_accumulate_stats (vcl_test_client_worker_t * wrk,
265 sock_test_socket_t * ctrl)
266{
267 sock_test_socket_t *tsock;
268 static char buf[64];
269 int i, show_rx = 0;
270
271 while (__sync_lock_test_and_set (&stats_lock, 1))
272 ;
273
274 if (ctrl->cfg.test == SOCK_TEST_TYPE_BI
275 || ctrl->cfg.test == SOCK_TEST_TYPE_ECHO)
276 show_rx = 1;
277
278 for (i = 0; i < wrk->cfg.num_test_sockets; i++)
279 {
280 tsock = &wrk->sessions[i];
281 tsock->stats.start = ctrl->stats.start;
282
283 if (ctrl->cfg.verbose > 1)
284 {
285 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
286 sock_test_stats_dump (buf, &tsock->stats, show_rx, 1 /* show tx */ ,
287 ctrl->cfg.verbose);
288 }
289
290 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
291 }
292
293 __sync_lock_release (&stats_lock);
294}
295
296static void
297vtc_worker_sessions_exit (vcl_test_client_worker_t * wrk)
298{
299 vcl_test_client_main_t *vcm = &vcl_client_main;
300 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
301 sock_test_socket_t *tsock;
302 int i, verbose = ctrl->cfg.verbose;
303
304 for (i = 0; i < wrk->cfg.num_test_sockets; i++)
305 {
306 tsock = &wrk->sessions[i];
307 tsock->cfg.test = SOCK_TEST_TYPE_EXIT;
308
309 if (verbose)
310 {
311 vtinf ("(fd %d): Sending exit cfg to server...", tsock->fd);
312 sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
313 }
314 (void) vcl_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
315 sizeof (tsock->cfg), &tsock->stats, verbose);
316 }
Florin Coras2cba8532018-09-11 16:33:36 -0700317 wrk->n_sessions = 0;
Florin Coras21795132018-09-09 09:40:51 -0700318}
319
Florin Coras6d4bb422018-09-04 22:07:27 -0700320static void *
321vtc_worker_loop (void *arg)
322{
323 vcl_test_client_main_t *vcm = &vcl_client_main;
324 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
325 vcl_test_client_worker_t *wrk = arg;
Florin Coras2cba8532018-09-11 16:33:36 -0700326 uint32_t n_active_sessions, n_bytes;
Florin Coras6d4bb422018-09-04 22:07:27 -0700327 fd_set _wfdset, *wfdset = &_wfdset;
328 fd_set _rfdset, *rfdset = &_rfdset;
329 sock_test_socket_t *tsock;
Florin Coras6d4bb422018-09-04 22:07:27 -0700330 int i, rv, check_rx = 0;
331
332 rv = vtc_worker_init (wrk);
333 if (rv)
334 {
335 vterr ("vtc_worker_init()", rv);
336 return 0;
337 }
338
339 vtinf ("Starting test ...");
340
341 if (wrk->wrk_index == 0)
342 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
343
344 check_rx = wrk->cfg.test != SOCK_TEST_TYPE_UNI;
345 n_active_sessions = wrk->cfg.num_test_sockets;
346 while (n_active_sessions)
347 {
348 _wfdset = wrk->wr_fdset;
349 _rfdset = wrk->rd_fdset;
350
351 rv = vppcom_select (wrk->max_fd_index, (uint64_t *) rfdset,
352 (uint64_t *) wfdset, NULL, 0);
353 if (rv < 0)
354 {
355 vterr ("vppcom_select()", rv);
356 goto exit;
357 }
358 else if (rv == 0)
359 continue;
360
361 for (i = 0; i < wrk->cfg.num_test_sockets; i++)
362 {
363 tsock = &wrk->sessions[i];
364 if (!((tsock->stats.stop.tv_sec == 0) &&
365 (tsock->stats.stop.tv_nsec == 0)))
366 continue;
367
368 if (FD_ISSET (vppcom_session_index (tsock->fd), rfdset)
369 && tsock->stats.rx_bytes < tsock->cfg.total_bytes)
370 {
371 (void) vcl_test_read (tsock->fd, (uint8_t *) tsock->rxbuf,
372 tsock->rxbuf_size, &tsock->stats);
373 }
374
375 if (FD_ISSET (vppcom_session_index (tsock->fd), wfdset)
376 && tsock->stats.tx_bytes < tsock->cfg.total_bytes)
377 {
Florin Coras2cba8532018-09-11 16:33:36 -0700378 n_bytes = tsock->cfg.txbuf_size;
379 if (tsock->cfg.test == SOCK_TEST_TYPE_ECHO)
380 n_bytes = strlen (ctrl->txbuf) + 1;
Florin Coras6d4bb422018-09-04 22:07:27 -0700381 rv = vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
Florin Coras2cba8532018-09-11 16:33:36 -0700382 n_bytes, &tsock->stats,
Florin Coras6d4bb422018-09-04 22:07:27 -0700383 tsock->cfg.verbose);
384 if (rv < 0)
385 {
386 vtwrn ("vppcom_test_write (%d) failed -- aborting test",
387 tsock->fd);
388 goto exit;
389 }
390 }
391
392 if ((!check_rx && tsock->stats.tx_bytes >= tsock->cfg.total_bytes)
393 || (check_rx
394 && tsock->stats.rx_bytes >= tsock->cfg.total_bytes))
395 {
396 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
397 n_active_sessions--;
398 }
399 }
400 }
401exit:
Florin Coras21795132018-09-09 09:40:51 -0700402 vtinf ("Worker %d done ...", wrk->wrk_index);
Florin Coras2cba8532018-09-11 16:33:36 -0700403 if (tsock->cfg.test != SOCK_TEST_TYPE_ECHO)
404 vtc_accumulate_stats (wrk, ctrl);
Florin Coras21795132018-09-09 09:40:51 -0700405 sleep (1);
406 vtc_worker_sessions_exit (wrk);
Florin Coras6d4bb422018-09-04 22:07:27 -0700407 if (wrk->wrk_index)
408 vt_atomic_add (&vcm->active_workers, -1);
409 return 0;
410}
411
412static void
Florin Coras6d4bb422018-09-04 22:07:27 -0700413vtc_print_stats (sock_test_socket_t * ctrl)
414{
415 int is_echo = ctrl->cfg.test == SOCK_TEST_TYPE_ECHO;
416 int show_rx = 0;
417 char buf[64];
418
419 if (ctrl->cfg.test == SOCK_TEST_TYPE_BI
420 || ctrl->cfg.test == SOCK_TEST_TYPE_ECHO)
421 show_rx = 1;
422
423 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
424 show_rx, 1 /* show tx */ ,
425 ctrl->cfg.verbose);
426 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
427
428 if (ctrl->cfg.verbose)
429 {
430 vtinf (" ctrl socket info\n"
431 SOCK_TEST_SEPARATOR_STRING
432 " fd: %d (0x%08x)\n"
433 " rxbuf: %p\n"
434 " rxbuf size: %u (0x%08x)\n"
435 " txbuf: %p\n"
436 " txbuf size: %u (0x%08x)\n"
437 SOCK_TEST_SEPARATOR_STRING,
438 ctrl->fd, (uint32_t) ctrl->fd,
439 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
440 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
441 }
442
443 if (is_echo)
444 sprintf (buf, "Echo");
445 else
446 sprintf (buf, "%s-directional Stream",
447 ctrl->cfg.test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
448
449 vtinf ("(fd %d): %s Test Complete!\n"
450 SOCK_TEST_BANNER_STRING, ctrl->fd, buf);
451}
452
453static void
454vtc_echo_client (vcl_test_client_main_t * vcm)
455{
456 vcl_test_client_worker_t *wrk;
457 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
458 sock_test_cfg_t *cfg = &ctrl->cfg;
459
460 cfg->total_bytes = strlen (ctrl->txbuf) + 1;
461 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
462
463 /* Echo works with only one worker */
464 wrk = vcm->workers;
465 wrk->wrk_index = 0;
466 wrk->cfg = *cfg;
467
468 vtc_worker_loop (wrk);
469
Florin Coras2cba8532018-09-11 16:33:36 -0700470 /* Not relevant for echo test
471 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
472 vtc_accumulate_stats (wrk, ctrl);
473 vtc_print_stats (ctrl);
474 */
Florin Coras6d4bb422018-09-04 22:07:27 -0700475}
476
477static void
478vtc_stream_client (vcl_test_client_main_t * vcm)
479{
480 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
481 sock_test_cfg_t *cfg = &ctrl->cfg;
482 vcl_test_client_worker_t *wrk;
483 sock_test_socket_t *tsock;
484 int tx_bytes, rv;
485 uint32_t i, n, sidx, n_conn, n_conn_per_wrk;
486
487 vtinf (SOCK_TEST_BANNER_STRING "%s-directional Stream Test!\n",
488 ctrl->cfg.test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
489
490 cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
491 cfg->ctrl_handle = ~0;
492 if (vtc_cfg_sync (ctrl))
493 {
494 vtwrn ("test cfg sync failed -- aborting!");
495 return;
496 }
497 cfg->ctrl_handle = ((sock_test_cfg_t *) ctrl->rxbuf)->ctrl_handle;
498 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
499
500 n_conn = cfg->num_test_sockets;
501 n_conn_per_wrk = n_conn / vcm->n_workers;
502 for (i = 0; i < vcm->n_workers; i++)
503 {
504 wrk = &vcm->workers[i];
505 wrk->wrk_index = i;
506 wrk->cfg = ctrl->cfg;
507 wrk->cfg.num_test_sockets = vtc_min (n_conn_per_wrk, n_conn);
508 n_conn -= wrk->cfg.num_test_sockets;
509 }
510
511 for (i = 1; i < vcm->n_workers; i++)
512 {
513 wrk = &vcm->workers[i];
514 pthread_create (&wrk->thread_handle, NULL, vtc_worker_loop,
515 (void *) wrk);
516 }
517 vtc_worker_loop (&vcm->workers[0]);
518
519 while (vcm->active_workers > 0)
520 ;
521
522 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
523
524 vtinf ("(fd %d): Sending config on ctrl socket for stats...\n", ctrl->fd);
525 if (vtc_cfg_sync (ctrl))
526 {
527 vtwrn ("test cfg sync failed -- aborting!");
528 return;
529 }
530
Florin Coras6d4bb422018-09-04 22:07:27 -0700531 vtc_print_stats (ctrl);
532
533 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
534 ctrl->cfg.total_bytes = 0;
535 if (vtc_cfg_sync (ctrl))
536 vtwrn ("post-test cfg sync failed!");
537}
538
539static void
Dave Wallacee4d5a652018-06-24 21:21:21 -0400540dump_help (void)
541{
542#define INDENT "\n "
543
544 printf ("CLIENT: Test configuration commands:"
545 INDENT SOCK_TEST_TOKEN_HELP
546 "\t\t\tDisplay help."
547 INDENT SOCK_TEST_TOKEN_EXIT
548 "\t\t\tExit test client & server."
549 INDENT SOCK_TEST_TOKEN_SHOW_CFG
550 "\t\t\tShow the current test cfg."
551 INDENT SOCK_TEST_TOKEN_RUN_UNI
552 "\t\t\tRun the Uni-directional test."
553 INDENT SOCK_TEST_TOKEN_RUN_BI
554 "\t\t\tRun the Bi-directional test."
555 INDENT SOCK_TEST_TOKEN_VERBOSE
556 "\t\t\tToggle verbose setting."
557 INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
558 "<rxbuf size>\tRx buffer size (bytes)."
559 INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
560 "<txbuf size>\tTx buffer size (bytes)."
561 INDENT SOCK_TEST_TOKEN_NUM_WRITES
562 "<# of writes>\tNumber of txbuf writes to server." "\n");
563}
564
565static void
566cfg_txbuf_size_set (void)
567{
Florin Coras6d4bb422018-09-04 22:07:27 -0700568 vcl_test_client_main_t *vcm = &vcl_client_main;
569 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400570 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
571 uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
572
573 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
574 {
575 ctrl->cfg.txbuf_size = txbuf_size;
576 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
577 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
578 (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
579 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
580 }
581 else
582 fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < "
583 "minimum buf size (%u)!\n",
584 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
585}
586
587static void
588cfg_num_writes_set (void)
589{
Florin Coras6d4bb422018-09-04 22:07:27 -0700590 vcl_test_client_main_t *vcm = &vcl_client_main;
591 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400592 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
593 uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
594
595 if (num_writes > 0)
596 {
597 ctrl->cfg.num_writes = num_writes;
598 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
599 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
600 }
601 else
602 {
603 fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes);
604 }
605}
606
607static void
608cfg_num_test_sockets_set (void)
609{
Florin Coras6d4bb422018-09-04 22:07:27 -0700610 vcl_test_client_main_t *vcm = &vcl_client_main;
611 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400612 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
613 uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
614
615 if ((num_test_sockets > 0) &&
616 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
617 {
618 ctrl->cfg.num_test_sockets = num_test_sockets;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400619 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
620 }
621 else
622 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700623 vtwrn ("invalid num test sockets: %u, (%d max)",
624 num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400625 }
626}
627
628static void
629cfg_rxbuf_size_set (void)
630{
Florin Coras6d4bb422018-09-04 22:07:27 -0700631 vcl_test_client_main_t *vcm = &vcl_client_main;
632 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400633 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
634 uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);
635
636 if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
637 {
638 ctrl->cfg.rxbuf_size = rxbuf_size;
639 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
640 (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
641 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
642 }
643 else
644 fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < "
645 "minimum buf size (%u)!\n",
646 rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
647}
648
649static void
650cfg_verbose_toggle (void)
651{
Florin Coras6d4bb422018-09-04 22:07:27 -0700652 vcl_test_client_main_t *vcm = &vcl_client_main;
653 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400654
655 ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
656 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
657
658}
659
660static sock_test_t
661parse_input ()
662{
Florin Coras6d4bb422018-09-04 22:07:27 -0700663 vcl_test_client_main_t *vcm = &vcl_client_main;
664 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400665 sock_test_t rv = SOCK_TEST_TYPE_NONE;
666
667 if (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf,
668 strlen (SOCK_TEST_TOKEN_EXIT)))
669 rv = SOCK_TEST_TYPE_EXIT;
670
671 else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf,
672 strlen (SOCK_TEST_TOKEN_HELP)))
673 dump_help ();
674
675 else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf,
676 strlen (SOCK_TEST_TOKEN_SHOW_CFG)))
Florin Coras6d4bb422018-09-04 22:07:27 -0700677 vcm->dump_cfg = 1;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400678
679 else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf,
680 strlen (SOCK_TEST_TOKEN_VERBOSE)))
681 cfg_verbose_toggle ();
682
683 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
684 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
685 cfg_txbuf_size_set ();
686
687 else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
688 strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
689 cfg_num_test_sockets_set ();
690
691 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
692 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
693 cfg_num_writes_set ();
694
695 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
696 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
697 cfg_rxbuf_size_set ();
698
699 else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
700 strlen (SOCK_TEST_TOKEN_RUN_UNI)))
701 rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
702
703 else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
704 strlen (SOCK_TEST_TOKEN_RUN_BI)))
705 rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
706
707 else
708 rv = SOCK_TEST_TYPE_ECHO;
709
710 return rv;
711}
712
713void
714print_usage_and_exit (void)
715{
716 fprintf (stderr,
717 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
718 " OPTIONS\n"
719 " -h Print this message and exit.\n"
720 " -6 Use IPv6\n"
721 " -u Use UDP transport layer\n"
722 " -c Print test config before test.\n"
723 " -w <dir> Write test results to <dir>.\n"
724 " -X Exit after running test.\n"
725 " -E Run Echo test.\n"
726 " -N <num-writes> Test Cfg: number of writes.\n"
727 " -R <rxbuf-size> Test Cfg: rx buffer size.\n"
728 " -T <txbuf-size> Test Cfg: tx buffer size.\n"
729 " -U Run Uni-directional test.\n"
730 " -B Run Bi-directional test.\n"
731 " -V Verbose mode.\n");
732 exit (1);
733}
734
Florin Coras6d4bb422018-09-04 22:07:27 -0700735static void
736vtc_process_opts (vcl_test_client_main_t * vcm, int argc, char **argv)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400737{
Florin Coras6d4bb422018-09-04 22:07:27 -0700738 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
739 int c, v;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400740
741 opterr = 0;
742 while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1)
743 switch (c)
744 {
745 case 'c':
Florin Coras6d4bb422018-09-04 22:07:27 -0700746 vcm->dump_cfg = 1;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400747 break;
748
749 case 's':
750 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
751 if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
752 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700753 vtwrn ("Invalid value for option -%c!", c);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400754 print_usage_and_exit ();
755 }
756 if (!ctrl->cfg.num_test_sockets ||
757 (ctrl->cfg.num_test_sockets > FD_SETSIZE))
758 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700759 vtwrn ("Invalid number of sockets (%d) specified for option -%c!"
760 "\n Valid range is 1 - %d",
761 ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400762 print_usage_and_exit ();
763 }
764 break;
765
766 case 'w':
Florin Coras6d4bb422018-09-04 22:07:27 -0700767 if (sscanf (optarg, "%d", &v) != 1)
768 {
769 vtwrn ("Invalid value for option -%c!", c);
770 print_usage_and_exit ();
771 }
772 if (v > 1)
773 vcm->n_workers = v;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400774 break;
775
776 case 'X':
Florin Coras6d4bb422018-09-04 22:07:27 -0700777 vcm->post_test = SOCK_TEST_TYPE_EXIT;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400778 break;
779
780 case 'E':
781 if (strlen (optarg) > ctrl->txbuf_size)
782 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700783 vtwrn ("Option -%c value larger than txbuf size (%d)!",
784 optopt, ctrl->txbuf_size);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400785 print_usage_and_exit ();
786 }
787 strcpy (ctrl->txbuf, optarg);
788 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
789 break;
790
791 case 'I':
792 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
793 if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
794 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700795 vtwrn ("Invalid value for option -%c!", c);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400796 print_usage_and_exit ();
797 }
798 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
799 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700800 vtwrn ("value greater than max number test sockets (%d)!",
801 SOCK_TEST_CFG_MAX_TEST_SCKTS);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400802 print_usage_and_exit ();
803 }
804 break;
805
806 case 'N':
807 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
808 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
809 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700810 vtwrn ("Invalid value for option -%c!", c);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400811 print_usage_and_exit ();
812 }
813 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
814 break;
815
816 case 'R':
817 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
818 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
819 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700820 vtwrn ("Invalid value for option -%c!", c);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400821 print_usage_and_exit ();
822 }
823 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
824 {
825 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
826 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
827 (uint8_t **) & ctrl->rxbuf,
828 &ctrl->rxbuf_size);
829 }
830 else
831 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700832 vtwrn ("rxbuf size (%lu) less than minumum (%u)",
833 ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400834 print_usage_and_exit ();
835 }
836
837 break;
838
839 case 'T':
840 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
841 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
842 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700843 vtwrn ("Invalid value for option -%c!", c);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400844 print_usage_and_exit ();
845 }
846 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
847 {
848 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
849 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
850 (uint8_t **) & ctrl->txbuf,
851 &ctrl->txbuf_size);
852 ctrl->cfg.total_bytes =
853 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
854 }
855 else
856 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700857 vtwrn ("txbuf size (%lu) less than minumum (%u)!",
858 ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400859 print_usage_and_exit ();
860 }
861 break;
862
863 case 'U':
864 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
865 break;
866
867 case 'B':
868 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
869 break;
870
871 case 'V':
872 ctrl->cfg.verbose = 1;
873 break;
874
875 case '6':
876 ctrl->cfg.address_ip6 = 1;
877 break;
878
879 case 'D':
880 ctrl->cfg.transport_udp = 1;
881 break;
882
883 case '?':
884 switch (optopt)
885 {
886 case 'E':
887 case 'I':
888 case 'N':
889 case 'R':
890 case 'T':
891 case 'w':
Florin Coras6d4bb422018-09-04 22:07:27 -0700892 vtwrn ("Option -%c requires an argument.", optopt);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400893 break;
894
895 default:
896 if (isprint (optopt))
Florin Coras6d4bb422018-09-04 22:07:27 -0700897 vtwrn ("Unknown option `-%c'.", optopt);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400898 else
Florin Coras6d4bb422018-09-04 22:07:27 -0700899 vtwrn ("Unknown option character `\\x%x'.", optopt);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400900 }
901 /* fall thru */
902 case 'h':
903 default:
904 print_usage_and_exit ();
905 }
906
907 if (argc < (optind + 2))
908 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700909 vtwrn ("Insufficient number of arguments!");
Dave Wallacee4d5a652018-06-24 21:21:21 -0400910 print_usage_and_exit ();
911 }
Florin Coras6d4bb422018-09-04 22:07:27 -0700912 vcm->proto = ctrl->cfg.transport_udp ? VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400913
Florin Coras6d4bb422018-09-04 22:07:27 -0700914 memset (&vcm->server_addr, 0, sizeof (vcm->server_addr));
Dave Wallacee4d5a652018-06-24 21:21:21 -0400915 if (ctrl->cfg.address_ip6)
916 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700917 struct sockaddr_in6 *sddr6 = (struct sockaddr_in6 *) &vcm->server_addr;
918 sddr6->sin6_family = AF_INET6;
919 inet_pton (AF_INET6, argv[optind++], &(sddr6->sin6_addr));
920 sddr6->sin6_port = htons (atoi (argv[optind]));
921
922 vcm->server_endpt.is_ip4 = 0;
923 vcm->server_endpt.ip = (uint8_t *) & sddr6->sin6_addr;
924 vcm->server_endpt.port = (uint16_t) sddr6->sin6_port;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400925 }
926 else
927 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700928 struct sockaddr_in *saddr4 = (struct sockaddr_in *) &vcm->server_addr;
929 saddr4->sin_family = AF_INET;
930 inet_pton (AF_INET, argv[optind++], &(saddr4->sin_addr));
931 saddr4->sin_port = htons (atoi (argv[optind]));
Dave Wallacee4d5a652018-06-24 21:21:21 -0400932
Florin Coras6d4bb422018-09-04 22:07:27 -0700933 vcm->server_endpt.is_ip4 = 1;
934 vcm->server_endpt.ip = (uint8_t *) & saddr4->sin_addr;
935 vcm->server_endpt.port = (uint16_t) saddr4->sin_port;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400936 }
Florin Coras6d4bb422018-09-04 22:07:27 -0700937}
Dave Wallacee4d5a652018-06-24 21:21:21 -0400938
Florin Coras6d4bb422018-09-04 22:07:27 -0700939static void
940vtc_read_user_input (sock_test_socket_t * ctrl)
941{
942 printf ("\nType some characters and hit <return>\n"
943 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
Dave Wallacee4d5a652018-06-24 21:21:21 -0400944
Florin Coras6d4bb422018-09-04 22:07:27 -0700945 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
946 {
947 if (strlen (ctrl->txbuf) == 1)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400948 {
Florin Coras6d4bb422018-09-04 22:07:27 -0700949 printf ("\nNothing to send! Please try again...\n");
950 return;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400951 }
Florin Coras6d4bb422018-09-04 22:07:27 -0700952 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
Dave Wallacee4d5a652018-06-24 21:21:21 -0400953
Florin Coras6d4bb422018-09-04 22:07:27 -0700954 /* Parse input for keywords */
955 ctrl->cfg.test = parse_input ();
Dave Wallacee4d5a652018-06-24 21:21:21 -0400956 }
Florin Coras6d4bb422018-09-04 22:07:27 -0700957}
Dave Wallacee4d5a652018-06-24 21:21:21 -0400958
Florin Coras21795132018-09-09 09:40:51 -0700959static void
960vtc_ctrl_session_exit (void)
961{
962 vcl_test_client_main_t *vcm = &vcl_client_main;
963 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
964 int verbose = ctrl->cfg.verbose;
965
966 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
967 if (verbose)
968 {
969 vtinf ("(fd %d): Sending exit cfg to server...", ctrl->fd);
970 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
971 }
972 (void) vcl_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
973 sizeof (ctrl->cfg), &ctrl->stats, verbose);
974 vtinf ("So long and thanks for all the fish!\n\n");
975 sleep (1);
976}
977
Florin Coras6d4bb422018-09-04 22:07:27 -0700978int
979main (int argc, char **argv)
980{
981 vcl_test_client_main_t *vcm = &vcl_client_main;
982 sock_test_socket_t *ctrl = &vcm->ctrl_socket;
983 int rv, errno_val;
984
985 vcm->n_workers = 1;
986 sock_test_cfg_init (&ctrl->cfg);
987 sock_test_socket_buf_alloc (ctrl);
988 vtc_process_opts (vcm, argc, argv);
989
990 vcm->workers = calloc (vcm->n_workers, sizeof (vcl_test_client_worker_t));
991 rv = vppcom_app_create ("vcl_test_client");
992 if (rv < 0)
993 vtfail ("vppcom_app_create()", rv);
994
995 ctrl->fd = vppcom_session_create (vcm->proto, 0 /* is_nonblocking */ );
996 if (ctrl->fd < 0)
997 vtfail ("vppcom_session_create()", ctrl->fd);
998
999 vtinf ("Connecting to server...");
1000 rv = vppcom_session_connect (ctrl->fd, &vcm->server_endpt);
1001 if (rv)
1002 vtfail ("vppcom_session_connect()", rv);
1003 vtinf ("(fd %d): Control socket connected.", ctrl->fd);
1004
1005 rv = vtc_cfg_sync (ctrl);
1006 if (rv)
1007 vtfail ("vtc_cfg_sync()", rv);
1008
1009 ctrl->cfg.ctrl_handle = ((sock_test_cfg_t *) ctrl->rxbuf)->ctrl_handle;
1010 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
Dave Wallacee4d5a652018-06-24 21:21:21 -04001011
1012 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
1013 {
Florin Coras6d4bb422018-09-04 22:07:27 -07001014 if (vcm->dump_cfg)
Dave Wallacee4d5a652018-06-24 21:21:21 -04001015 {
1016 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
Florin Coras6d4bb422018-09-04 22:07:27 -07001017 vcm->dump_cfg = 0;
Dave Wallacee4d5a652018-06-24 21:21:21 -04001018 }
1019
1020 switch (ctrl->cfg.test)
1021 {
1022 case SOCK_TEST_TYPE_ECHO:
Florin Coras6d4bb422018-09-04 22:07:27 -07001023 vtc_echo_client (vcm);
Dave Wallacee4d5a652018-06-24 21:21:21 -04001024 break;
1025
1026 case SOCK_TEST_TYPE_UNI:
1027 case SOCK_TEST_TYPE_BI:
Florin Coras6d4bb422018-09-04 22:07:27 -07001028 vtc_stream_client (vcm);
Dave Wallacee4d5a652018-06-24 21:21:21 -04001029 break;
1030
1031 case SOCK_TEST_TYPE_EXIT:
1032 continue;
1033
1034 case SOCK_TEST_TYPE_NONE:
1035 default:
1036 break;
1037 }
Florin Coras6d4bb422018-09-04 22:07:27 -07001038 switch (vcm->post_test)
Dave Wallacee4d5a652018-06-24 21:21:21 -04001039 {
1040 case SOCK_TEST_TYPE_EXIT:
1041 switch (ctrl->cfg.test)
1042 {
1043 case SOCK_TEST_TYPE_EXIT:
1044 case SOCK_TEST_TYPE_UNI:
1045 case SOCK_TEST_TYPE_BI:
1046 case SOCK_TEST_TYPE_ECHO:
1047 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1048 continue;
1049
1050 case SOCK_TEST_TYPE_NONE:
1051 default:
1052 break;
1053 }
1054 break;
1055
1056 case SOCK_TEST_TYPE_NONE:
1057 case SOCK_TEST_TYPE_ECHO:
1058 case SOCK_TEST_TYPE_UNI:
1059 case SOCK_TEST_TYPE_BI:
1060 default:
1061 break;
1062 }
1063
1064 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1065 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1066
Florin Coras6d4bb422018-09-04 22:07:27 -07001067 vtc_read_user_input (ctrl);
Dave Wallacee4d5a652018-06-24 21:21:21 -04001068 }
1069
Florin Coras21795132018-09-09 09:40:51 -07001070 vtc_ctrl_session_exit ();
Dave Wallacee4d5a652018-06-24 21:21:21 -04001071 vppcom_session_close (ctrl->fd);
1072 vppcom_app_destroy ();
Florin Coras6d4bb422018-09-04 22:07:27 -07001073 free (vcm->workers);
Dave Wallacee4d5a652018-06-24 21:21:21 -04001074 return 0;
1075}
Dave Wallace543852a2017-08-03 02:11:34 -04001076
1077/*
1078 * fd.io coding-style-patch-verification: ON
1079 *
1080 * Local Variables:
1081 * eval: (c-set-style "gnu")
1082 * End:
1083 */