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 | } |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 317 | wrk->n_sessions = 0; |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 320 | static void * |
| 321 | vtc_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 Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 326 | uint32_t n_active_sessions, n_bytes; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 327 | fd_set _wfdset, *wfdset = &_wfdset; |
| 328 | fd_set _rfdset, *rfdset = &_rfdset; |
| 329 | sock_test_socket_t *tsock; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 330 | 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 Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 378 | n_bytes = tsock->cfg.txbuf_size; |
| 379 | if (tsock->cfg.test == SOCK_TEST_TYPE_ECHO) |
| 380 | n_bytes = strlen (ctrl->txbuf) + 1; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 381 | rv = vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf, |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 382 | n_bytes, &tsock->stats, |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 383 | 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 | } |
| 401 | exit: |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 402 | vtinf ("Worker %d done ...", wrk->wrk_index); |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 403 | if (tsock->cfg.test != SOCK_TEST_TYPE_ECHO) |
| 404 | vtc_accumulate_stats (wrk, ctrl); |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 405 | sleep (1); |
| 406 | vtc_worker_sessions_exit (wrk); |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 407 | if (wrk->wrk_index) |
| 408 | vt_atomic_add (&vcm->active_workers, -1); |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static void |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 413 | vtc_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 | |
| 453 | static void |
| 454 | vtc_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 Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 470 | /* 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static void |
| 478 | vtc_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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 531 | 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 | |
| 539 | static void |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 540 | dump_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 | |
| 565 | static void |
| 566 | cfg_txbuf_size_set (void) |
| 567 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 568 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 569 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 570 | 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 | |
| 587 | static void |
| 588 | cfg_num_writes_set (void) |
| 589 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 590 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 591 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 592 | 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 | |
| 607 | static void |
| 608 | cfg_num_test_sockets_set (void) |
| 609 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 610 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 611 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 612 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 619 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 620 | } |
| 621 | else |
| 622 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 623 | vtwrn ("invalid num test sockets: %u, (%d max)", |
| 624 | num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | static void |
| 629 | cfg_rxbuf_size_set (void) |
| 630 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 631 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 632 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 633 | 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 | |
| 649 | static void |
| 650 | cfg_verbose_toggle (void) |
| 651 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 652 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 653 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 654 | |
| 655 | ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1; |
| 656 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 657 | |
| 658 | } |
| 659 | |
| 660 | static sock_test_t |
| 661 | parse_input () |
| 662 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 663 | vcl_test_client_main_t *vcm = &vcl_client_main; |
| 664 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 665 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 677 | vcm->dump_cfg = 1; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 678 | |
| 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 | |
| 713 | void |
| 714 | print_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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 735 | static void |
| 736 | 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] | 737 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 738 | sock_test_socket_t *ctrl = &vcm->ctrl_socket; |
| 739 | int c, v; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 740 | |
| 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 746 | vcm->dump_cfg = 1; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 747 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 753 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 754 | print_usage_and_exit (); |
| 755 | } |
| 756 | if (!ctrl->cfg.num_test_sockets || |
| 757 | (ctrl->cfg.num_test_sockets > FD_SETSIZE)) |
| 758 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 759 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 762 | print_usage_and_exit (); |
| 763 | } |
| 764 | break; |
| 765 | |
| 766 | case 'w': |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 767 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 774 | break; |
| 775 | |
| 776 | case 'X': |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 777 | vcm->post_test = SOCK_TEST_TYPE_EXIT; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 778 | break; |
| 779 | |
| 780 | case 'E': |
| 781 | if (strlen (optarg) > ctrl->txbuf_size) |
| 782 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 783 | vtwrn ("Option -%c value larger than txbuf size (%d)!", |
| 784 | optopt, ctrl->txbuf_size); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 785 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 795 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 796 | print_usage_and_exit (); |
| 797 | } |
| 798 | if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS) |
| 799 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 800 | vtwrn ("value greater than max number test sockets (%d)!", |
| 801 | SOCK_TEST_CFG_MAX_TEST_SCKTS); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 802 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 810 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 811 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 820 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 821 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 832 | vtwrn ("rxbuf size (%lu) less than minumum (%u)", |
| 833 | ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 834 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 843 | vtwrn ("Invalid value for option -%c!", c); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 844 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 857 | vtwrn ("txbuf size (%lu) less than minumum (%u)!", |
| 858 | ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 859 | 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 892 | vtwrn ("Option -%c requires an argument.", optopt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 893 | break; |
| 894 | |
| 895 | default: |
| 896 | if (isprint (optopt)) |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 897 | vtwrn ("Unknown option `-%c'.", optopt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 898 | else |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 899 | vtwrn ("Unknown option character `\\x%x'.", optopt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 900 | } |
| 901 | /* fall thru */ |
| 902 | case 'h': |
| 903 | default: |
| 904 | print_usage_and_exit (); |
| 905 | } |
| 906 | |
| 907 | if (argc < (optind + 2)) |
| 908 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 909 | vtwrn ("Insufficient number of arguments!"); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 910 | print_usage_and_exit (); |
| 911 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 912 | vcm->proto = ctrl->cfg.transport_udp ? VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 913 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 914 | memset (&vcm->server_addr, 0, sizeof (vcm->server_addr)); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 915 | if (ctrl->cfg.address_ip6) |
| 916 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 917 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 925 | } |
| 926 | else |
| 927 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 928 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 932 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 933 | 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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 936 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 937 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 938 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 939 | static void |
| 940 | vtc_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 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 | if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL) |
| 946 | { |
| 947 | if (strlen (ctrl->txbuf) == 1) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 948 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 949 | printf ("\nNothing to send! Please try again...\n"); |
| 950 | return; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 951 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 952 | ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline. |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 953 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 954 | /* Parse input for keywords */ |
| 955 | ctrl->cfg.test = parse_input (); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 956 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 957 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 958 | |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 959 | static void |
| 960 | vtc_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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 978 | int |
| 979 | main (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 Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1011 | |
| 1012 | while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT) |
| 1013 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1014 | if (vcm->dump_cfg) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1015 | { |
| 1016 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1017 | vcm->dump_cfg = 0; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | switch (ctrl->cfg.test) |
| 1021 | { |
| 1022 | case SOCK_TEST_TYPE_ECHO: |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1023 | vtc_echo_client (vcm); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1024 | break; |
| 1025 | |
| 1026 | case SOCK_TEST_TYPE_UNI: |
| 1027 | case SOCK_TEST_TYPE_BI: |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1028 | vtc_stream_client (vcm); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1029 | break; |
| 1030 | |
| 1031 | case SOCK_TEST_TYPE_EXIT: |
| 1032 | continue; |
| 1033 | |
| 1034 | case SOCK_TEST_TYPE_NONE: |
| 1035 | default: |
| 1036 | break; |
| 1037 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1038 | switch (vcm->post_test) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1039 | { |
| 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 Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1067 | vtc_read_user_input (ctrl); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1068 | } |
| 1069 | |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 1070 | vtc_ctrl_session_exit (); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1071 | vppcom_session_close (ctrl->fd); |
| 1072 | vppcom_app_destroy (); |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 1073 | free (vcm->workers); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1074 | return 0; |
| 1075 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1076 | |
| 1077 | /* |
| 1078 | * fd.io coding-style-patch-verification: ON |
| 1079 | * |
| 1080 | * Local Variables: |
| 1081 | * eval: (c-set-style "gnu") |
| 1082 | * End: |
| 1083 | */ |