Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1 | /* |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 Cisco and/or its affiliates. |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 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 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 16 | #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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 26 | #include <pthread.h> |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 27 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 28 | typedef struct |
| 29 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 30 | 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 | |
| 40 | typedef struct |
| 41 | { |
| 42 | vcl_test_client_worker_t *workers; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 43 | vppcom_endpt_t server_endpt; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 44 | uint32_t cfg_seq_num; |
| 45 | sock_test_socket_t ctrl_socket; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 46 | sock_test_socket_t *sessions; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 47 | uint8_t dump_cfg; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 48 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 54 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 55 | static __thread int __wrk_index = 0; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 56 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 57 | vcl_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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 61 | |
| 62 | static int |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 63 | vtc_cfg_sync (sock_test_socket_t * sock) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 64 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 65 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 66 | sock_test_cfg_t *rx_cfg = (sock_test_cfg_t *) sock->rxbuf; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 67 | int rx_bytes, tx_bytes; |
| 68 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 69 | vt_atomic_add (&sock->cfg.seq_num, 1); |
| 70 | if (sock->cfg.verbose) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 71 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 72 | vtinf ("(fd %d): Sending config to server.", sock->fd); |
| 73 | sock_test_cfg_dump (&sock->cfg, 1 /* is_client */ ); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 74 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 75 | tx_bytes = vcl_test_write (sock->fd, (uint8_t *) & sock->cfg, |
| 76 | sizeof (sock->cfg), NULL, sock->cfg.verbose); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 77 | if (tx_bytes < 0) |
| 78 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 79 | vtwrn ("(fd %d): write test cfg failed (%d)!", sock->fd, tx_bytes); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 80 | return tx_bytes; |
| 81 | } |
| 82 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 83 | rx_bytes = vcl_test_read (sock->fd, (uint8_t *) sock->rxbuf, |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 84 | sizeof (sock_test_cfg_t), NULL); |
| 85 | if (rx_bytes < 0) |
| 86 | return rx_bytes; |
| 87 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 88 | if (rx_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 89 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 90 | vtwrn ("(fd %d): Bad server reply cfg -- aborting!", sock->fd); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 91 | return -1; |
| 92 | } |
| 93 | if ((rx_bytes != sizeof (sock_test_cfg_t)) |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 94 | || !sock_test_cfg_verify (rx_cfg, &sock->cfg)) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 95 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 96 | vtwrn ("(fd %d): Invalid config received from server!", sock->fd); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 97 | if (rx_bytes != sizeof (sock_test_cfg_t)) |
| 98 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 99 | vtinf ("\tRx bytes %d != cfg size %lu", rx_bytes, |
| 100 | sizeof (sock_test_cfg_t)); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 104 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 107 | } |
| 108 | return -1; |
| 109 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 110 | if (sock->cfg.verbose) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 111 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 112 | vtinf ("(fd %d): Got config back from server.", sock->fd); |
| 113 | sock_test_cfg_dump (rx_cfg, 1 /* is_client */ ); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 114 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 119 | static int |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 120 | vtc_connect_test_sessions (vcl_test_client_worker_t * wrk) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 121 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 122 | vcl_test_client_main_t *vcm = &vcl_client_main; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 123 | sock_test_socket_t *tsock; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 124 | uint32_t n_test_sessions; |
| 125 | int i, rv; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 126 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 127 | n_test_sessions = wrk->cfg.num_test_sockets; |
| 128 | if (n_test_sessions < 1) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 129 | { |
| 130 | errno = EINVAL; |
| 131 | return -1; |
| 132 | } |
| 133 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 134 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 144 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 145 | vterr ("failed to alloc sessions", -errno); |
| 146 | return errno; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 149 | for (i = 0; i < n_test_sessions; i++) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 150 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 151 | tsock = &wrk->sessions[i]; |
| 152 | tsock->fd = vppcom_session_create (vcm->proto, 1 /* is_nonblocking */ ); |
| 153 | if (tsock->fd < 0) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 154 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 155 | 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 | |
| 169 | done: |
| 170 | vtinf ("All test sessions (%d) connected!", n_test_sessions); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int |
| 175 | vtc_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 | |
| 215 | static int |
| 216 | vtc_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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 234 | return -1; |
| 235 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 236 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 245 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame^] | 261 | static int stats_lock = 0; |
| 262 | |
| 263 | static void |
| 264 | vtc_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 | |
| 296 | static void |
| 297 | vtc_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 | } |
| 317 | } |
| 318 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 319 | static void * |
| 320 | vtc_worker_loop (void *arg) |
| 321 | { |
| 322 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 323 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
| 324 | vcl_test_client_worker_t *wrk = arg; |
| 325 | fd_set _wfdset, *wfdset = &_wfdset; |
| 326 | fd_set _rfdset, *rfdset = &_rfdset; |
| 327 | sock_test_socket_t *tsock; |
| 328 | uint32_t n_active_sessions; |
| 329 | int i, rv, check_rx = 0; |
| 330 | |
| 331 | rv = vtc_worker_init (wrk); |
| 332 | if (rv) |
| 333 | { |
| 334 | vterr ("vtc_worker_init()", rv); |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | vtinf ("Starting test ..."); |
| 339 | |
| 340 | if (wrk->wrk_index == 0) |
| 341 | clock_gettime (CLOCK_REALTIME, &ctrl->stats.start); |
| 342 | |
| 343 | check_rx = wrk->cfg.test != SOCK_TEST_TYPE_UNI; |
| 344 | n_active_sessions = wrk->cfg.num_test_sockets; |
| 345 | while (n_active_sessions) |
| 346 | { |
| 347 | _wfdset = wrk->wr_fdset; |
| 348 | _rfdset = wrk->rd_fdset; |
| 349 | |
| 350 | rv = vppcom_select (wrk->max_fd_index, (uint64_t *) rfdset, |
| 351 | (uint64_t *) wfdset, NULL, 0); |
| 352 | if (rv < 0) |
| 353 | { |
| 354 | vterr ("vppcom_select()", rv); |
| 355 | goto exit; |
| 356 | } |
| 357 | else if (rv == 0) |
| 358 | continue; |
| 359 | |
| 360 | for (i = 0; i < wrk->cfg.num_test_sockets; i++) |
| 361 | { |
| 362 | tsock = &wrk->sessions[i]; |
| 363 | if (!((tsock->stats.stop.tv_sec == 0) && |
| 364 | (tsock->stats.stop.tv_nsec == 0))) |
| 365 | continue; |
| 366 | |
| 367 | if (FD_ISSET (vppcom_session_index (tsock->fd), rfdset) |
| 368 | && tsock->stats.rx_bytes < tsock->cfg.total_bytes) |
| 369 | { |
| 370 | (void) vcl_test_read (tsock->fd, (uint8_t *) tsock->rxbuf, |
| 371 | tsock->rxbuf_size, &tsock->stats); |
| 372 | } |
| 373 | |
| 374 | if (FD_ISSET (vppcom_session_index (tsock->fd), wfdset) |
| 375 | && tsock->stats.tx_bytes < tsock->cfg.total_bytes) |
| 376 | { |
| 377 | rv = vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf, |
| 378 | tsock->cfg.txbuf_size, &tsock->stats, |
| 379 | tsock->cfg.verbose); |
| 380 | if (rv < 0) |
| 381 | { |
| 382 | vtwrn ("vppcom_test_write (%d) failed -- aborting test", |
| 383 | tsock->fd); |
| 384 | goto exit; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if ((!check_rx && tsock->stats.tx_bytes >= tsock->cfg.total_bytes) |
| 389 | || (check_rx |
| 390 | && tsock->stats.rx_bytes >= tsock->cfg.total_bytes)) |
| 391 | { |
| 392 | clock_gettime (CLOCK_REALTIME, &tsock->stats.stop); |
| 393 | n_active_sessions--; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | exit: |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame^] | 398 | vtinf ("Worker %d done ...", wrk->wrk_index); |
| 399 | vtc_accumulate_stats (wrk, ctrl); |
| 400 | sleep (1); |
| 401 | vtc_worker_sessions_exit (wrk); |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 402 | if (wrk->wrk_index) |
| 403 | vt_atomic_add (&vcm->active_workers, -1); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static void |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 408 | vtc_print_stats (sock_test_socket_t * ctrl) |
| 409 | { |
| 410 | int is_echo = ctrl->cfg.test == SOCK_TEST_TYPE_ECHO; |
| 411 | int show_rx = 0; |
| 412 | char buf[64]; |
| 413 | |
| 414 | if (ctrl->cfg.test == SOCK_TEST_TYPE_BI |
| 415 | || ctrl->cfg.test == SOCK_TEST_TYPE_ECHO) |
| 416 | show_rx = 1; |
| 417 | |
| 418 | sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats, |
| 419 | show_rx, 1 /* show tx */ , |
| 420 | ctrl->cfg.verbose); |
| 421 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 422 | |
| 423 | if (ctrl->cfg.verbose) |
| 424 | { |
| 425 | vtinf (" ctrl socket info\n" |
| 426 | SOCK_TEST_SEPARATOR_STRING |
| 427 | " fd: %d (0x%08x)\n" |
| 428 | " rxbuf: %p\n" |
| 429 | " rxbuf size: %u (0x%08x)\n" |
| 430 | " txbuf: %p\n" |
| 431 | " txbuf size: %u (0x%08x)\n" |
| 432 | SOCK_TEST_SEPARATOR_STRING, |
| 433 | ctrl->fd, (uint32_t) ctrl->fd, |
| 434 | ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size, |
| 435 | ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size); |
| 436 | } |
| 437 | |
| 438 | if (is_echo) |
| 439 | sprintf (buf, "Echo"); |
| 440 | else |
| 441 | sprintf (buf, "%s-directional Stream", |
| 442 | ctrl->cfg.test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni"); |
| 443 | |
| 444 | vtinf ("(fd %d): %s Test Complete!\n" |
| 445 | SOCK_TEST_BANNER_STRING, ctrl->fd, buf); |
| 446 | } |
| 447 | |
| 448 | static void |
| 449 | vtc_echo_client (vcl_test_client_main_t * vcm) |
| 450 | { |
| 451 | vcl_test_client_worker_t *wrk; |
| 452 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
| 453 | sock_test_cfg_t *cfg = &ctrl->cfg; |
| 454 | |
| 455 | cfg->total_bytes = strlen (ctrl->txbuf) + 1; |
| 456 | memset (&ctrl->stats, 0, sizeof (ctrl->stats)); |
| 457 | |
| 458 | /* Echo works with only one worker */ |
| 459 | wrk = vcm->workers; |
| 460 | wrk->wrk_index = 0; |
| 461 | wrk->cfg = *cfg; |
| 462 | |
| 463 | vtc_worker_loop (wrk); |
| 464 | |
| 465 | clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop); |
| 466 | vtc_accumulate_stats (wrk, ctrl); |
| 467 | vtc_print_stats (ctrl); |
| 468 | } |
| 469 | |
| 470 | static void |
| 471 | vtc_stream_client (vcl_test_client_main_t * vcm) |
| 472 | { |
| 473 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
| 474 | sock_test_cfg_t *cfg = &ctrl->cfg; |
| 475 | vcl_test_client_worker_t *wrk; |
| 476 | sock_test_socket_t *tsock; |
| 477 | int tx_bytes, rv; |
| 478 | uint32_t i, n, sidx, n_conn, n_conn_per_wrk; |
| 479 | |
| 480 | vtinf (SOCK_TEST_BANNER_STRING "%s-directional Stream Test!\n", |
| 481 | ctrl->cfg.test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni"); |
| 482 | |
| 483 | cfg->total_bytes = cfg->num_writes * cfg->txbuf_size; |
| 484 | cfg->ctrl_handle = ~0; |
| 485 | if (vtc_cfg_sync (ctrl)) |
| 486 | { |
| 487 | vtwrn ("test cfg sync failed -- aborting!"); |
| 488 | return; |
| 489 | } |
| 490 | cfg->ctrl_handle = ((sock_test_cfg_t *) ctrl->rxbuf)->ctrl_handle; |
| 491 | memset (&ctrl->stats, 0, sizeof (ctrl->stats)); |
| 492 | |
| 493 | n_conn = cfg->num_test_sockets; |
| 494 | n_conn_per_wrk = n_conn / vcm->n_workers; |
| 495 | for (i = 0; i < vcm->n_workers; i++) |
| 496 | { |
| 497 | wrk = &vcm->workers[i]; |
| 498 | wrk->wrk_index = i; |
| 499 | wrk->cfg = ctrl->cfg; |
| 500 | wrk->cfg.num_test_sockets = vtc_min (n_conn_per_wrk, n_conn); |
| 501 | n_conn -= wrk->cfg.num_test_sockets; |
| 502 | } |
| 503 | |
| 504 | for (i = 1; i < vcm->n_workers; i++) |
| 505 | { |
| 506 | wrk = &vcm->workers[i]; |
| 507 | pthread_create (&wrk->thread_handle, NULL, vtc_worker_loop, |
| 508 | (void *) wrk); |
| 509 | } |
| 510 | vtc_worker_loop (&vcm->workers[0]); |
| 511 | |
| 512 | while (vcm->active_workers > 0) |
| 513 | ; |
| 514 | |
| 515 | clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop); |
| 516 | |
| 517 | vtinf ("(fd %d): Sending config on ctrl socket for stats...\n", ctrl->fd); |
| 518 | if (vtc_cfg_sync (ctrl)) |
| 519 | { |
| 520 | vtwrn ("test cfg sync failed -- aborting!"); |
| 521 | return; |
| 522 | } |
| 523 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 524 | vtc_print_stats (ctrl); |
| 525 | |
| 526 | ctrl->cfg.test = SOCK_TEST_TYPE_ECHO; |
| 527 | ctrl->cfg.total_bytes = 0; |
| 528 | if (vtc_cfg_sync (ctrl)) |
| 529 | vtwrn ("post-test cfg sync failed!"); |
| 530 | } |
| 531 | |
| 532 | static void |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 533 | dump_help (void) |
| 534 | { |
| 535 | #define INDENT "\n " |
| 536 | |
| 537 | printf ("CLIENT: Test configuration commands:" |
| 538 | INDENT SOCK_TEST_TOKEN_HELP |
| 539 | "\t\t\tDisplay help." |
| 540 | INDENT SOCK_TEST_TOKEN_EXIT |
| 541 | "\t\t\tExit test client & server." |
| 542 | INDENT SOCK_TEST_TOKEN_SHOW_CFG |
| 543 | "\t\t\tShow the current test cfg." |
| 544 | INDENT SOCK_TEST_TOKEN_RUN_UNI |
| 545 | "\t\t\tRun the Uni-directional test." |
| 546 | INDENT SOCK_TEST_TOKEN_RUN_BI |
| 547 | "\t\t\tRun the Bi-directional test." |
| 548 | INDENT SOCK_TEST_TOKEN_VERBOSE |
| 549 | "\t\t\tToggle verbose setting." |
| 550 | INDENT SOCK_TEST_TOKEN_RXBUF_SIZE |
| 551 | "<rxbuf size>\tRx buffer size (bytes)." |
| 552 | INDENT SOCK_TEST_TOKEN_TXBUF_SIZE |
| 553 | "<txbuf size>\tTx buffer size (bytes)." |
| 554 | INDENT SOCK_TEST_TOKEN_NUM_WRITES |
| 555 | "<# of writes>\tNumber of txbuf writes to server." "\n"); |
| 556 | } |
| 557 | |
| 558 | static void |
| 559 | cfg_txbuf_size_set (void) |
| 560 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 561 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 562 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 563 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE); |
| 564 | uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10); |
| 565 | |
| 566 | if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 567 | { |
| 568 | ctrl->cfg.txbuf_size = txbuf_size; |
| 569 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 570 | sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , |
| 571 | (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size); |
| 572 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 573 | } |
| 574 | else |
| 575 | fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < " |
| 576 | "minimum buf size (%u)!\n", |
| 577 | txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | cfg_num_writes_set (void) |
| 582 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 583 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 584 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 585 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES); |
| 586 | uint32_t num_writes = strtoul ((const char *) p, NULL, 10); |
| 587 | |
| 588 | if (num_writes > 0) |
| 589 | { |
| 590 | ctrl->cfg.num_writes = num_writes; |
| 591 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 592 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | static void |
| 601 | cfg_num_test_sockets_set (void) |
| 602 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 603 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 604 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 605 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS); |
| 606 | uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10); |
| 607 | |
| 608 | if ((num_test_sockets > 0) && |
| 609 | (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS)) |
| 610 | { |
| 611 | ctrl->cfg.num_test_sockets = num_test_sockets; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 612 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 613 | } |
| 614 | else |
| 615 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 616 | vtwrn ("invalid num test sockets: %u, (%d max)", |
| 617 | num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | static void |
| 622 | cfg_rxbuf_size_set (void) |
| 623 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 624 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 625 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 626 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE); |
| 627 | uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10); |
| 628 | |
| 629 | if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 630 | { |
| 631 | ctrl->cfg.rxbuf_size = rxbuf_size; |
| 632 | sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , |
| 633 | (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size); |
| 634 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 635 | } |
| 636 | else |
| 637 | fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < " |
| 638 | "minimum buf size (%u)!\n", |
| 639 | rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 640 | } |
| 641 | |
| 642 | static void |
| 643 | cfg_verbose_toggle (void) |
| 644 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 645 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 646 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 647 | |
| 648 | ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1; |
| 649 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 650 | |
| 651 | } |
| 652 | |
| 653 | static sock_test_t |
| 654 | parse_input () |
| 655 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 656 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 657 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 658 | sock_test_t rv = SOCK_TEST_TYPE_NONE; |
| 659 | |
| 660 | if (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf, |
| 661 | strlen (SOCK_TEST_TOKEN_EXIT))) |
| 662 | rv = SOCK_TEST_TYPE_EXIT; |
| 663 | |
| 664 | else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf, |
| 665 | strlen (SOCK_TEST_TOKEN_HELP))) |
| 666 | dump_help (); |
| 667 | |
| 668 | else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf, |
| 669 | strlen (SOCK_TEST_TOKEN_SHOW_CFG))) |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 670 | vcm->dump_cfg = 1; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 671 | |
| 672 | else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf, |
| 673 | strlen (SOCK_TEST_TOKEN_VERBOSE))) |
| 674 | cfg_verbose_toggle (); |
| 675 | |
| 676 | else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf, |
| 677 | strlen (SOCK_TEST_TOKEN_TXBUF_SIZE))) |
| 678 | cfg_txbuf_size_set (); |
| 679 | |
| 680 | else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf, |
| 681 | strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS))) |
| 682 | cfg_num_test_sockets_set (); |
| 683 | |
| 684 | else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf, |
| 685 | strlen (SOCK_TEST_TOKEN_NUM_WRITES))) |
| 686 | cfg_num_writes_set (); |
| 687 | |
| 688 | else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf, |
| 689 | strlen (SOCK_TEST_TOKEN_RXBUF_SIZE))) |
| 690 | cfg_rxbuf_size_set (); |
| 691 | |
| 692 | else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf, |
| 693 | strlen (SOCK_TEST_TOKEN_RUN_UNI))) |
| 694 | rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI; |
| 695 | |
| 696 | else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf, |
| 697 | strlen (SOCK_TEST_TOKEN_RUN_BI))) |
| 698 | rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI; |
| 699 | |
| 700 | else |
| 701 | rv = SOCK_TEST_TYPE_ECHO; |
| 702 | |
| 703 | return rv; |
| 704 | } |
| 705 | |
| 706 | void |
| 707 | print_usage_and_exit (void) |
| 708 | { |
| 709 | fprintf (stderr, |
| 710 | "sock_test_client [OPTIONS] <ipaddr> <port>\n" |
| 711 | " OPTIONS\n" |
| 712 | " -h Print this message and exit.\n" |
| 713 | " -6 Use IPv6\n" |
| 714 | " -u Use UDP transport layer\n" |
| 715 | " -c Print test config before test.\n" |
| 716 | " -w <dir> Write test results to <dir>.\n" |
| 717 | " -X Exit after running test.\n" |
| 718 | " -E Run Echo test.\n" |
| 719 | " -N <num-writes> Test Cfg: number of writes.\n" |
| 720 | " -R <rxbuf-size> Test Cfg: rx buffer size.\n" |
| 721 | " -T <txbuf-size> Test Cfg: tx buffer size.\n" |
| 722 | " -U Run Uni-directional test.\n" |
| 723 | " -B Run Bi-directional test.\n" |
| 724 | " -V Verbose mode.\n"); |
| 725 | exit (1); |
| 726 | } |
| 727 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 728 | static void |
| 729 | vtc_process_opts (vcl_test_client_main_t * vcm, int argc, char **argv) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 730 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 731 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
| 732 | int c, v; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 733 | |
| 734 | opterr = 0; |
| 735 | while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1) |
| 736 | switch (c) |
| 737 | { |
| 738 | case 'c': |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 739 | vcm->dump_cfg = 1; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 740 | break; |
| 741 | |
| 742 | case 's': |
| 743 | if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1) |
| 744 | if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1) |
| 745 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 746 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 747 | print_usage_and_exit (); |
| 748 | } |
| 749 | if (!ctrl->cfg.num_test_sockets || |
| 750 | (ctrl->cfg.num_test_sockets > FD_SETSIZE)) |
| 751 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 752 | vtwrn ("Invalid number of sockets (%d) specified for option -%c!" |
| 753 | "\n Valid range is 1 - %d", |
| 754 | ctrl->cfg.num_test_sockets, c, FD_SETSIZE); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 755 | print_usage_and_exit (); |
| 756 | } |
| 757 | break; |
| 758 | |
| 759 | case 'w': |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 760 | if (sscanf (optarg, "%d", &v) != 1) |
| 761 | { |
| 762 | vtwrn ("Invalid value for option -%c!", c); |
| 763 | print_usage_and_exit (); |
| 764 | } |
| 765 | if (v > 1) |
| 766 | vcm->n_workers = v; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 767 | break; |
| 768 | |
| 769 | case 'X': |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 770 | vcm->post_test = SOCK_TEST_TYPE_EXIT; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 771 | break; |
| 772 | |
| 773 | case 'E': |
| 774 | if (strlen (optarg) > ctrl->txbuf_size) |
| 775 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 776 | vtwrn ("Option -%c value larger than txbuf size (%d)!", |
| 777 | optopt, ctrl->txbuf_size); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 778 | print_usage_and_exit (); |
| 779 | } |
| 780 | strcpy (ctrl->txbuf, optarg); |
| 781 | ctrl->cfg.test = SOCK_TEST_TYPE_ECHO; |
| 782 | break; |
| 783 | |
| 784 | case 'I': |
| 785 | if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1) |
| 786 | if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1) |
| 787 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 788 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 789 | print_usage_and_exit (); |
| 790 | } |
| 791 | if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS) |
| 792 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 793 | vtwrn ("value greater than max number test sockets (%d)!", |
| 794 | SOCK_TEST_CFG_MAX_TEST_SCKTS); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 795 | print_usage_and_exit (); |
| 796 | } |
| 797 | break; |
| 798 | |
| 799 | case 'N': |
| 800 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1) |
| 801 | if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1) |
| 802 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 803 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 804 | print_usage_and_exit (); |
| 805 | } |
| 806 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 807 | break; |
| 808 | |
| 809 | case 'R': |
| 810 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1) |
| 811 | if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1) |
| 812 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 813 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 814 | print_usage_and_exit (); |
| 815 | } |
| 816 | if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 817 | { |
| 818 | ctrl->rxbuf_size = ctrl->cfg.rxbuf_size; |
| 819 | sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , |
| 820 | (uint8_t **) & ctrl->rxbuf, |
| 821 | &ctrl->rxbuf_size); |
| 822 | } |
| 823 | else |
| 824 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 825 | vtwrn ("rxbuf size (%lu) less than minumum (%u)", |
| 826 | ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 827 | print_usage_and_exit (); |
| 828 | } |
| 829 | |
| 830 | break; |
| 831 | |
| 832 | case 'T': |
| 833 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1) |
| 834 | if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1) |
| 835 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 836 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 837 | print_usage_and_exit (); |
| 838 | } |
| 839 | if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 840 | { |
| 841 | ctrl->txbuf_size = ctrl->cfg.txbuf_size; |
| 842 | sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , |
| 843 | (uint8_t **) & ctrl->txbuf, |
| 844 | &ctrl->txbuf_size); |
| 845 | ctrl->cfg.total_bytes = |
| 846 | ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 847 | } |
| 848 | else |
| 849 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 850 | vtwrn ("txbuf size (%lu) less than minumum (%u)!", |
| 851 | ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 852 | print_usage_and_exit (); |
| 853 | } |
| 854 | break; |
| 855 | |
| 856 | case 'U': |
| 857 | ctrl->cfg.test = SOCK_TEST_TYPE_UNI; |
| 858 | break; |
| 859 | |
| 860 | case 'B': |
| 861 | ctrl->cfg.test = SOCK_TEST_TYPE_BI; |
| 862 | break; |
| 863 | |
| 864 | case 'V': |
| 865 | ctrl->cfg.verbose = 1; |
| 866 | break; |
| 867 | |
| 868 | case '6': |
| 869 | ctrl->cfg.address_ip6 = 1; |
| 870 | break; |
| 871 | |
| 872 | case 'D': |
| 873 | ctrl->cfg.transport_udp = 1; |
| 874 | break; |
| 875 | |
| 876 | case '?': |
| 877 | switch (optopt) |
| 878 | { |
| 879 | case 'E': |
| 880 | case 'I': |
| 881 | case 'N': |
| 882 | case 'R': |
| 883 | case 'T': |
| 884 | case 'w': |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 885 | vtwrn ("Option -%c requires an argument.", optopt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 886 | break; |
| 887 | |
| 888 | default: |
| 889 | if (isprint (optopt)) |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 890 | vtwrn ("Unknown option `-%c'.", optopt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 891 | else |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 892 | vtwrn ("Unknown option character `\\x%x'.", optopt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 893 | } |
| 894 | /* fall thru */ |
| 895 | case 'h': |
| 896 | default: |
| 897 | print_usage_and_exit (); |
| 898 | } |
| 899 | |
| 900 | if (argc < (optind + 2)) |
| 901 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 902 | vtwrn ("Insufficient number of arguments!"); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 903 | print_usage_and_exit (); |
| 904 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 905 | vcm->proto = ctrl->cfg.transport_udp ? VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 906 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 907 | memset (&vcm->server_addr, 0, sizeof (vcm->server_addr)); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 908 | if (ctrl->cfg.address_ip6) |
| 909 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 910 | struct sockaddr_in6 *sddr6 = (struct sockaddr_in6 *) &vcm->server_addr; |
| 911 | sddr6->sin6_family = AF_INET6; |
| 912 | inet_pton (AF_INET6, argv[optind++], &(sddr6->sin6_addr)); |
| 913 | sddr6->sin6_port = htons (atoi (argv[optind])); |
| 914 | |
| 915 | vcm->server_endpt.is_ip4 = 0; |
| 916 | vcm->server_endpt.ip = (uint8_t *) & sddr6->sin6_addr; |
| 917 | vcm->server_endpt.port = (uint16_t) sddr6->sin6_port; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 918 | } |
| 919 | else |
| 920 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 921 | struct sockaddr_in *saddr4 = (struct sockaddr_in *) &vcm->server_addr; |
| 922 | saddr4->sin_family = AF_INET; |
| 923 | inet_pton (AF_INET, argv[optind++], &(saddr4->sin_addr)); |
| 924 | saddr4->sin_port = htons (atoi (argv[optind])); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 925 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 926 | vcm->server_endpt.is_ip4 = 1; |
| 927 | vcm->server_endpt.ip = (uint8_t *) & saddr4->sin_addr; |
| 928 | vcm->server_endpt.port = (uint16_t) saddr4->sin_port; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 929 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 930 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 931 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 932 | static void |
| 933 | vtc_read_user_input (sock_test_socket_t * ctrl) |
| 934 | { |
| 935 | printf ("\nType some characters and hit <return>\n" |
| 936 | "('" SOCK_TEST_TOKEN_HELP "' for help): "); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 937 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 938 | if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL) |
| 939 | { |
| 940 | if (strlen (ctrl->txbuf) == 1) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 941 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 942 | printf ("\nNothing to send! Please try again...\n"); |
| 943 | return; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 944 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 945 | ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline. |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 946 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 947 | /* Parse input for keywords */ |
| 948 | ctrl->cfg.test = parse_input (); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 949 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 950 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 951 | |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame^] | 952 | static void |
| 953 | vtc_ctrl_session_exit (void) |
| 954 | { |
| 955 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 956 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
| 957 | int verbose = ctrl->cfg.verbose; |
| 958 | |
| 959 | ctrl->cfg.test = SOCK_TEST_TYPE_EXIT; |
| 960 | if (verbose) |
| 961 | { |
| 962 | vtinf ("(fd %d): Sending exit cfg to server...", ctrl->fd); |
| 963 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 964 | } |
| 965 | (void) vcl_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg, |
| 966 | sizeof (ctrl->cfg), &ctrl->stats, verbose); |
| 967 | vtinf ("So long and thanks for all the fish!\n\n"); |
| 968 | sleep (1); |
| 969 | } |
| 970 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 971 | int |
| 972 | main (int argc, char **argv) |
| 973 | { |
| 974 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 975 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
| 976 | int rv, errno_val; |
| 977 | |
| 978 | vcm->n_workers = 1; |
| 979 | sock_test_cfg_init (&ctrl->cfg); |
| 980 | sock_test_socket_buf_alloc (ctrl); |
| 981 | vtc_process_opts (vcm, argc, argv); |
| 982 | |
| 983 | vcm->workers = calloc (vcm->n_workers, sizeof (vcl_test_client_worker_t)); |
| 984 | rv = vppcom_app_create ("vcl_test_client"); |
| 985 | if (rv < 0) |
| 986 | vtfail ("vppcom_app_create()", rv); |
| 987 | |
| 988 | ctrl->fd = vppcom_session_create (vcm->proto, 0 /* is_nonblocking */ ); |
| 989 | if (ctrl->fd < 0) |
| 990 | vtfail ("vppcom_session_create()", ctrl->fd); |
| 991 | |
| 992 | vtinf ("Connecting to server..."); |
| 993 | rv = vppcom_session_connect (ctrl->fd, &vcm->server_endpt); |
| 994 | if (rv) |
| 995 | vtfail ("vppcom_session_connect()", rv); |
| 996 | vtinf ("(fd %d): Control socket connected.", ctrl->fd); |
| 997 | |
| 998 | rv = vtc_cfg_sync (ctrl); |
| 999 | if (rv) |
| 1000 | vtfail ("vtc_cfg_sync()", rv); |
| 1001 | |
| 1002 | ctrl->cfg.ctrl_handle = ((sock_test_cfg_t *) ctrl->rxbuf)->ctrl_handle; |
| 1003 | memset (&ctrl->stats, 0, sizeof (ctrl->stats)); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1004 | |
| 1005 | while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT) |
| 1006 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1007 | if (vcm->dump_cfg) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1008 | { |
| 1009 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1010 | vcm->dump_cfg = 0; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | switch (ctrl->cfg.test) |
| 1014 | { |
| 1015 | case SOCK_TEST_TYPE_ECHO: |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1016 | vtc_echo_client (vcm); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1017 | break; |
| 1018 | |
| 1019 | case SOCK_TEST_TYPE_UNI: |
| 1020 | case SOCK_TEST_TYPE_BI: |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1021 | vtc_stream_client (vcm); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1022 | break; |
| 1023 | |
| 1024 | case SOCK_TEST_TYPE_EXIT: |
| 1025 | continue; |
| 1026 | |
| 1027 | case SOCK_TEST_TYPE_NONE: |
| 1028 | default: |
| 1029 | break; |
| 1030 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1031 | switch (vcm->post_test) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1032 | { |
| 1033 | case SOCK_TEST_TYPE_EXIT: |
| 1034 | switch (ctrl->cfg.test) |
| 1035 | { |
| 1036 | case SOCK_TEST_TYPE_EXIT: |
| 1037 | case SOCK_TEST_TYPE_UNI: |
| 1038 | case SOCK_TEST_TYPE_BI: |
| 1039 | case SOCK_TEST_TYPE_ECHO: |
| 1040 | ctrl->cfg.test = SOCK_TEST_TYPE_EXIT; |
| 1041 | continue; |
| 1042 | |
| 1043 | case SOCK_TEST_TYPE_NONE: |
| 1044 | default: |
| 1045 | break; |
| 1046 | } |
| 1047 | break; |
| 1048 | |
| 1049 | case SOCK_TEST_TYPE_NONE: |
| 1050 | case SOCK_TEST_TYPE_ECHO: |
| 1051 | case SOCK_TEST_TYPE_UNI: |
| 1052 | case SOCK_TEST_TYPE_BI: |
| 1053 | default: |
| 1054 | break; |
| 1055 | } |
| 1056 | |
| 1057 | memset (ctrl->txbuf, 0, ctrl->txbuf_size); |
| 1058 | memset (ctrl->rxbuf, 0, ctrl->rxbuf_size); |
| 1059 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1060 | vtc_read_user_input (ctrl); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1061 | } |
| 1062 | |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame^] | 1063 | vtc_ctrl_session_exit (); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1064 | vppcom_session_close (ctrl->fd); |
| 1065 | vppcom_app_destroy (); |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1066 | free (vcm->workers); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1067 | return 0; |
| 1068 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1069 | |
| 1070 | /* |
| 1071 | * fd.io coding-style-patch-verification: ON |
| 1072 | * |
| 1073 | * Local Variables: |
| 1074 | * eval: (c-set-style "gnu") |
| 1075 | * End: |
| 1076 | */ |