Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <unistd.h> |
| 17 | #include <errno.h> |
| 18 | #include <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> |
Dave Wallace | 5c7cf1c | 2017-10-24 04:12:18 -0400 | [diff] [blame] | 25 | #include <vcl/sock_test.h> |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 26 | |
| 27 | typedef struct |
| 28 | { |
| 29 | #ifdef VCL_TEST |
| 30 | vppcom_endpt_t server_endpt; |
| 31 | #endif |
| 32 | struct sockaddr_in server_addr; |
| 33 | sock_test_socket_t ctrl_socket; |
| 34 | sock_test_socket_t *test_socket; |
| 35 | uint32_t num_test_sockets; |
| 36 | uint8_t dump_cfg; |
| 37 | } sock_client_main_t; |
| 38 | |
| 39 | sock_client_main_t sock_client_main; |
| 40 | |
| 41 | |
| 42 | static int |
| 43 | sock_test_cfg_sync (sock_test_socket_t * socket) |
| 44 | { |
| 45 | sock_client_main_t *scm = &sock_client_main; |
| 46 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 47 | sock_test_cfg_t *rl_cfg = (sock_test_cfg_t *) socket->rxbuf; |
| 48 | int rx_bytes, tx_bytes; |
| 49 | |
| 50 | if (socket->cfg.verbose) |
| 51 | sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ ); |
| 52 | |
| 53 | tx_bytes = sock_test_write (socket->fd, (uint8_t *) & ctrl->cfg, |
| 54 | sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose); |
| 55 | if (tx_bytes < 0) |
| 56 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 57 | fprintf (stderr, "CLIENT: ERROR: write test cfg failed (%d)!\n", |
| 58 | tx_bytes); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 59 | return tx_bytes; |
| 60 | } |
| 61 | |
| 62 | rx_bytes = sock_test_read (socket->fd, (uint8_t *) socket->rxbuf, |
| 63 | sizeof (sock_test_cfg_t), NULL); |
| 64 | if (rx_bytes < 0) |
| 65 | return rx_bytes; |
| 66 | |
| 67 | if (rl_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC) |
| 68 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 69 | fprintf (stderr, "CLIENT: ERROR: Bad server reply cfg -- aborting!\n"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 70 | return -1; |
| 71 | } |
| 72 | if (socket->cfg.verbose) |
| 73 | { |
| 74 | printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd); |
| 75 | sock_test_cfg_dump (rl_cfg, 1 /* is_client */ ); |
| 76 | } |
| 77 | if ((rx_bytes != sizeof (sock_test_cfg_t)) |
| 78 | || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg)) |
| 79 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 80 | fprintf (stderr, "CLIENT: ERROR: Invalid config received " |
| 81 | "from server -- aborting!\n"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 82 | sock_test_cfg_dump (rl_cfg, 1 /* is_client */ ); |
| 83 | return -1; |
| 84 | } |
| 85 | ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ? |
| 86 | rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | echo_test_client () |
| 93 | { |
| 94 | sock_client_main_t *scm = &sock_client_main; |
| 95 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 96 | sock_test_socket_t *tsock; |
| 97 | int rx_bytes, tx_bytes, nbytes; |
| 98 | uint32_t i, n; |
| 99 | int rv; |
| 100 | int nfds = 0; |
| 101 | fd_set wr_fdset, rd_fdset; |
| 102 | fd_set _wfdset, *wfdset = &_wfdset; |
| 103 | fd_set _rfdset, *rfdset = &_rfdset; |
| 104 | |
| 105 | FD_ZERO (&wr_fdset); |
| 106 | FD_ZERO (&rd_fdset); |
| 107 | memset (&ctrl->stats, 0, sizeof (ctrl->stats)); |
| 108 | ctrl->cfg.total_bytes = nbytes = strlen (ctrl->txbuf) + 1; |
| 109 | for (n = 0; n != ctrl->cfg.num_test_sockets; n++) |
| 110 | { |
| 111 | tsock = &scm->test_socket[n]; |
| 112 | tsock->cfg = ctrl->cfg; |
| 113 | sock_test_socket_buf_alloc (tsock); |
| 114 | sock_test_cfg_sync (tsock); |
| 115 | |
| 116 | memcpy (tsock->txbuf, ctrl->txbuf, nbytes); |
| 117 | memset (&tsock->stats, 0, sizeof (tsock->stats)); |
| 118 | |
| 119 | FD_SET (tsock->fd, &wr_fdset); |
| 120 | FD_SET (tsock->fd, &rd_fdset); |
| 121 | nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds; |
| 122 | } |
| 123 | |
| 124 | nfds++; |
| 125 | clock_gettime (CLOCK_REALTIME, &ctrl->stats.start); |
| 126 | while (n) |
| 127 | { |
| 128 | _wfdset = wr_fdset; |
| 129 | _rfdset = rd_fdset; |
| 130 | |
| 131 | #ifdef VCL_TEST |
| 132 | rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset, |
| 133 | NULL, 0); |
| 134 | #else |
| 135 | { |
| 136 | struct timeval timeout; |
| 137 | timeout.tv_sec = 0; |
| 138 | timeout.tv_usec = 0; |
| 139 | rv = select (nfds, rfdset, wfdset, NULL, &timeout); |
| 140 | } |
| 141 | #endif |
| 142 | if (rv < 0) |
| 143 | { |
| 144 | perror ("select()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 145 | fprintf (stderr, "\nCLIENT: ERROR: select() failed -- " |
| 146 | "aborting test!\n"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | else if (rv == 0) |
| 150 | continue; |
| 151 | |
| 152 | for (i = 0; i < ctrl->cfg.num_test_sockets; i++) |
| 153 | { |
| 154 | tsock = &scm->test_socket[i]; |
| 155 | if (!((tsock->stats.stop.tv_sec == 0) && |
| 156 | (tsock->stats.stop.tv_nsec == 0))) |
| 157 | continue; |
| 158 | |
| 159 | if (FD_ISSET (tsock->fd, wfdset) && |
| 160 | (tsock->stats.tx_bytes < ctrl->cfg.total_bytes)) |
| 161 | |
| 162 | { |
| 163 | tx_bytes = |
| 164 | sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf, nbytes, |
| 165 | &tsock->stats, ctrl->cfg.verbose); |
| 166 | if (tx_bytes < 0) |
| 167 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 168 | fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) " |
| 169 | "failed -- aborting test!\n", tsock->fd); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
| 173 | printf ("CLIENT (fd %d): TX (%d bytes) - '%s'\n", |
| 174 | tsock->fd, tx_bytes, tsock->txbuf); |
| 175 | } |
| 176 | |
| 177 | if ((FD_ISSET (tsock->fd, rfdset)) && |
| 178 | (tsock->stats.rx_bytes < ctrl->cfg.total_bytes)) |
| 179 | { |
| 180 | rx_bytes = |
| 181 | sock_test_read (tsock->fd, (uint8_t *) tsock->rxbuf, |
| 182 | nbytes, &tsock->stats); |
| 183 | if (rx_bytes > 0) |
| 184 | { |
| 185 | printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n", |
| 186 | tsock->fd, rx_bytes, tsock->rxbuf); |
| 187 | |
| 188 | if (tsock->stats.rx_bytes != tsock->stats.tx_bytes) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 189 | printf ("CLIENT: WARNING: bytes read (%lu) " |
| 190 | "!= bytes written (%lu)!\n", |
| 191 | tsock->stats.rx_bytes, tsock->stats.tx_bytes); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes) |
| 196 | { |
| 197 | clock_gettime (CLOCK_REALTIME, &tsock->stats.stop); |
| 198 | n--; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop); |
| 203 | |
| 204 | for (i = 0; i < ctrl->cfg.num_test_sockets; i++) |
| 205 | { |
| 206 | tsock = &scm->test_socket[i]; |
| 207 | tsock->stats.start = ctrl->stats.start; |
| 208 | |
| 209 | if (ctrl->cfg.verbose) |
| 210 | { |
| 211 | static char buf[64]; |
| 212 | |
| 213 | sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd); |
| 214 | sock_test_stats_dump (buf, &tsock->stats, |
| 215 | 1 /* show_rx */ , 1 /* show tx */ , |
| 216 | ctrl->cfg.verbose); |
| 217 | } |
| 218 | |
| 219 | sock_test_stats_accumulate (&ctrl->stats, &tsock->stats); |
| 220 | } |
| 221 | |
| 222 | if (ctrl->cfg.verbose) |
| 223 | { |
| 224 | sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats, |
| 225 | 1 /* show_rx */ , 1 /* show tx */ , |
| 226 | ctrl->cfg.verbose); |
| 227 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 228 | |
| 229 | if (ctrl->cfg.verbose > 1) |
| 230 | { |
| 231 | printf (" ctrl socket info\n" |
| 232 | SOCK_TEST_SEPARATOR_STRING |
| 233 | " fd: %d (0x%08x)\n" |
| 234 | " rxbuf: %p\n" |
| 235 | " rxbuf size: %u (0x%08x)\n" |
| 236 | " txbuf: %p\n" |
| 237 | " txbuf size: %u (0x%08x)\n" |
| 238 | SOCK_TEST_SEPARATOR_STRING, |
| 239 | ctrl->fd, (uint32_t) ctrl->fd, |
| 240 | ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size, |
| 241 | ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | stream_test_client (sock_test_t test) |
| 248 | { |
| 249 | sock_client_main_t *scm = &sock_client_main; |
| 250 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 251 | sock_test_socket_t *tsock; |
| 252 | int tx_bytes; |
| 253 | uint32_t i, n; |
| 254 | int rv; |
| 255 | int nfds = 0; |
| 256 | fd_set wr_fdset, rd_fdset; |
| 257 | fd_set _wfdset, *wfdset = &_wfdset; |
| 258 | fd_set _rfdset, *rfdset = (test == SOCK_TEST_TYPE_BI) ? &_rfdset : 0; |
| 259 | |
| 260 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 261 | ctrl->cfg.ctrl_handle = ~0; |
| 262 | |
| 263 | printf ("\n" SOCK_TEST_BANNER_STRING |
| 264 | "CLIENT (fd %d): %s-directional Stream Test!\n\n" |
| 265 | "CLIENT (fd %d): Sending config to server on ctrl socket...\n", |
| 266 | ctrl->fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni", ctrl->fd); |
| 267 | |
| 268 | if (sock_test_cfg_sync (ctrl)) |
| 269 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 270 | fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | |
| 274 | FD_ZERO (&wr_fdset); |
| 275 | FD_ZERO (&rd_fdset); |
| 276 | memset (&ctrl->stats, 0, sizeof (ctrl->stats)); |
| 277 | for (n = 0; n != ctrl->cfg.num_test_sockets; n++) |
| 278 | { |
| 279 | tsock = &scm->test_socket[n]; |
| 280 | tsock->cfg = ctrl->cfg; |
| 281 | sock_test_socket_buf_alloc (tsock); |
| 282 | printf ("CLIENT (fd %d): Sending config to server on " |
| 283 | "test socket %d...\n", tsock->fd, n); |
| 284 | sock_test_cfg_sync (tsock); |
| 285 | |
| 286 | /* Fill payload with incrementing uint32's */ |
| 287 | for (i = 0; i < tsock->txbuf_size; i++) |
| 288 | tsock->txbuf[i] = i & 0xff; |
| 289 | |
| 290 | memset (&tsock->stats, 0, sizeof (tsock->stats)); |
| 291 | FD_SET (tsock->fd, &wr_fdset); |
| 292 | FD_SET (tsock->fd, &rd_fdset); |
| 293 | nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds; |
| 294 | } |
| 295 | |
| 296 | nfds++; |
| 297 | clock_gettime (CLOCK_REALTIME, &ctrl->stats.start); |
| 298 | while (n) |
| 299 | { |
| 300 | _wfdset = wr_fdset; |
| 301 | _rfdset = rd_fdset; |
| 302 | |
| 303 | #ifdef VCL_TEST |
| 304 | rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset, |
| 305 | NULL, 0); |
| 306 | #else |
| 307 | { |
| 308 | struct timeval timeout; |
| 309 | timeout.tv_sec = 0; |
| 310 | timeout.tv_usec = 0; |
| 311 | rv = select (nfds, rfdset, wfdset, NULL, &timeout); |
| 312 | } |
| 313 | #endif |
| 314 | if (rv < 0) |
| 315 | { |
| 316 | perror ("select()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 317 | fprintf (stderr, "\nCLIENT: ERROR: select() failed -- " |
| 318 | "aborting test!\n"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | else if (rv == 0) |
| 322 | continue; |
| 323 | |
| 324 | for (i = 0; i < ctrl->cfg.num_test_sockets; i++) |
| 325 | { |
| 326 | tsock = &scm->test_socket[i]; |
| 327 | if (!((tsock->stats.stop.tv_sec == 0) && |
| 328 | (tsock->stats.stop.tv_nsec == 0))) |
| 329 | continue; |
| 330 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 331 | if ((test == SOCK_TEST_TYPE_BI) && |
| 332 | FD_ISSET (tsock->fd, rfdset) && |
| 333 | (tsock->stats.rx_bytes < ctrl->cfg.total_bytes)) |
| 334 | { |
| 335 | (void) sock_test_read (tsock->fd, |
| 336 | (uint8_t *) tsock->rxbuf, |
| 337 | tsock->rxbuf_size, &tsock->stats); |
| 338 | } |
| 339 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 340 | if (FD_ISSET (tsock->fd, wfdset) && |
| 341 | (tsock->stats.tx_bytes < ctrl->cfg.total_bytes)) |
| 342 | { |
| 343 | tx_bytes = |
| 344 | sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf, |
| 345 | ctrl->cfg.txbuf_size, &tsock->stats, |
| 346 | ctrl->cfg.verbose); |
| 347 | if (tx_bytes < 0) |
| 348 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 349 | fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) " |
| 350 | "failed -- aborting test!\n", tsock->fd); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | } |
| 354 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 355 | if (((test == SOCK_TEST_TYPE_UNI) && |
| 356 | (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) || |
| 357 | ((test == SOCK_TEST_TYPE_BI) && |
| 358 | (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes))) |
| 359 | { |
| 360 | clock_gettime (CLOCK_REALTIME, &tsock->stats.stop); |
| 361 | n--; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop); |
| 366 | |
| 367 | printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n", |
| 368 | ctrl->fd); |
| 369 | |
| 370 | if (sock_test_cfg_sync (ctrl)) |
| 371 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 372 | fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
| 376 | for (i = 0; i < ctrl->cfg.num_test_sockets; i++) |
| 377 | { |
| 378 | tsock = &scm->test_socket[i]; |
| 379 | |
| 380 | if (ctrl->cfg.verbose) |
| 381 | { |
| 382 | static char buf[64]; |
| 383 | |
| 384 | sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd); |
| 385 | sock_test_stats_dump (buf, &tsock->stats, |
| 386 | test == SOCK_TEST_TYPE_BI /* show_rx */ , |
| 387 | 1 /* show tx */ , ctrl->cfg.verbose); |
| 388 | } |
| 389 | |
| 390 | sock_test_stats_accumulate (&ctrl->stats, &tsock->stats); |
| 391 | } |
| 392 | |
| 393 | sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats, |
| 394 | test == SOCK_TEST_TYPE_BI /* show_rx */ , |
| 395 | 1 /* show tx */ , ctrl->cfg.verbose); |
| 396 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 397 | |
| 398 | if (ctrl->cfg.verbose) |
| 399 | { |
| 400 | printf (" ctrl socket info\n" |
| 401 | SOCK_TEST_SEPARATOR_STRING |
| 402 | " fd: %d (0x%08x)\n" |
| 403 | " rxbuf: %p\n" |
| 404 | " rxbuf size: %u (0x%08x)\n" |
| 405 | " txbuf: %p\n" |
| 406 | " txbuf size: %u (0x%08x)\n" |
| 407 | SOCK_TEST_SEPARATOR_STRING, |
| 408 | ctrl->fd, (uint32_t) ctrl->fd, |
| 409 | ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size, |
| 410 | ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size); |
| 411 | } |
| 412 | |
| 413 | ctrl->cfg.test = SOCK_TEST_TYPE_ECHO; |
| 414 | if (sock_test_cfg_sync (ctrl)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 415 | fprintf (stderr, "CLIENT: ERROR: post-test cfg sync failed!"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 416 | |
| 417 | printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n" |
| 418 | SOCK_TEST_BANNER_STRING "\n", ctrl->fd, |
| 419 | test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni"); |
| 420 | } |
| 421 | |
| 422 | static void |
| 423 | exit_client (void) |
| 424 | { |
| 425 | sock_client_main_t *scm = &sock_client_main; |
| 426 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 427 | sock_test_socket_t *tsock; |
| 428 | int i; |
| 429 | |
| 430 | for (i = 0; i < ctrl->cfg.num_test_sockets; i++) |
| 431 | { |
| 432 | tsock = &scm->test_socket[i]; |
| 433 | tsock->cfg.test = SOCK_TEST_TYPE_EXIT; |
| 434 | |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 435 | /* coverity[COPY_PASTE_ERROR] */ |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 436 | if (ctrl->cfg.verbose) |
| 437 | { |
| 438 | printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", |
| 439 | tsock->fd); |
| 440 | sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ ); |
| 441 | } |
| 442 | (void) sock_test_write (tsock->fd, (uint8_t *) & tsock->cfg, |
| 443 | sizeof (tsock->cfg), &tsock->stats, |
| 444 | ctrl->cfg.verbose); |
| 445 | } |
| 446 | |
| 447 | ctrl->cfg.test = SOCK_TEST_TYPE_EXIT; |
| 448 | if (ctrl->cfg.verbose) |
| 449 | { |
| 450 | printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd); |
| 451 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 452 | } |
| 453 | (void) sock_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg, |
| 454 | sizeof (ctrl->cfg), &ctrl->stats, |
| 455 | ctrl->cfg.verbose); |
| 456 | printf ("\nCLIENT: So long and thanks for all the fish!\n\n"); |
| 457 | sleep (1); |
| 458 | } |
| 459 | |
| 460 | static int |
| 461 | sock_test_connect_test_sockets (uint32_t num_test_sockets) |
| 462 | { |
| 463 | sock_client_main_t *scm = &sock_client_main; |
| 464 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 465 | sock_test_socket_t *tsock; |
| 466 | int i, rv, errno_val; |
| 467 | |
| 468 | if (num_test_sockets < 1) |
| 469 | { |
| 470 | errno = EINVAL; |
| 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | if (num_test_sockets < scm->num_test_sockets) |
| 475 | { |
| 476 | for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--) |
| 477 | { |
| 478 | tsock = &scm->test_socket[i]; |
| 479 | #ifdef VCL_TEST |
| 480 | vppcom_session_close (tsock->fd); |
| 481 | #else |
| 482 | close (tsock->fd); |
| 483 | #endif |
| 484 | free (tsock->txbuf); |
| 485 | free (tsock->rxbuf); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | else if (num_test_sockets > scm->num_test_sockets) |
| 490 | { |
| 491 | tsock = realloc (scm->test_socket, |
| 492 | sizeof (sock_test_socket_t) * num_test_sockets); |
| 493 | if (!tsock) |
| 494 | { |
| 495 | errno_val = errno; |
| 496 | perror ("ERROR in sock_test_connect_test_sockets()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 497 | fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n", |
| 498 | errno_val); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 499 | return -1; |
| 500 | } |
| 501 | |
Dave Wallace | d48e976 | 2017-08-22 23:29:33 -0400 | [diff] [blame] | 502 | if (!scm->test_socket) |
| 503 | memset (tsock, 0, sizeof (*tsock)); |
| 504 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 505 | scm->test_socket = tsock; |
| 506 | for (i = scm->num_test_sockets; i < num_test_sockets; i++) |
| 507 | { |
| 508 | tsock = &scm->test_socket[i]; |
| 509 | #ifdef VCL_TEST |
| 510 | tsock->fd = |
| 511 | vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP, |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 512 | 1 /* is_nonblocking */ ); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 513 | if (tsock->fd < 0) |
| 514 | { |
| 515 | errno = -tsock->fd; |
| 516 | tsock->fd = -1; |
| 517 | } |
| 518 | #else |
| 519 | tsock->fd = socket (AF_INET, SOCK_STREAM, 0); |
| 520 | #endif |
| 521 | if (tsock->fd < 0) |
| 522 | { |
| 523 | errno_val = errno; |
| 524 | perror ("ERROR in sock_test_connect_test_sockets()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 525 | fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 526 | errno_val); |
| 527 | return tsock->fd; |
| 528 | } |
| 529 | |
| 530 | #ifdef VCL_TEST |
| 531 | rv = vppcom_session_connect (tsock->fd, &scm->server_endpt); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 532 | if (rv) |
| 533 | { |
| 534 | errno = -rv; |
| 535 | rv = -1; |
| 536 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 537 | #else |
| 538 | rv = |
| 539 | connect (tsock->fd, (struct sockaddr *) &scm->server_addr, |
| 540 | sizeof (scm->server_addr)); |
| 541 | #endif |
| 542 | if (rv < 0) |
| 543 | { |
| 544 | errno_val = errno; |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 545 | perror ("ERROR in sock_test_connect_test_sockets()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 546 | fprintf (stderr, "CLIENT: ERROR: connect failed " |
| 547 | "(errno = %d)!\n", errno_val); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 548 | return -1; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 549 | } |
| 550 | tsock->cfg = ctrl->cfg; |
| 551 | sock_test_socket_buf_alloc (tsock); |
| 552 | sock_test_cfg_sync (tsock); |
| 553 | |
| 554 | printf ("CLIENT (fd %d): Test socket %d connected.\n", |
| 555 | tsock->fd, i); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | scm->num_test_sockets = num_test_sockets; |
| 560 | printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1); |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static void |
| 565 | dump_help (void) |
| 566 | { |
| 567 | #define INDENT "\n " |
| 568 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 569 | printf ("CLIENT: Test configuration commands:" |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 570 | INDENT SOCK_TEST_TOKEN_HELP |
| 571 | "\t\t\tDisplay help." |
| 572 | INDENT SOCK_TEST_TOKEN_EXIT |
| 573 | "\t\t\tExit test client & server." |
| 574 | INDENT SOCK_TEST_TOKEN_SHOW_CFG |
| 575 | "\t\t\tShow the current test cfg." |
| 576 | INDENT SOCK_TEST_TOKEN_RUN_UNI |
| 577 | "\t\t\tRun the Uni-directional test." |
| 578 | INDENT SOCK_TEST_TOKEN_RUN_BI |
| 579 | "\t\t\tRun the Bi-directional test." |
| 580 | INDENT SOCK_TEST_TOKEN_VERBOSE |
| 581 | "\t\t\tToggle verbose setting." |
| 582 | INDENT SOCK_TEST_TOKEN_RXBUF_SIZE |
| 583 | "<rxbuf size>\tRx buffer size (bytes)." |
| 584 | INDENT SOCK_TEST_TOKEN_TXBUF_SIZE |
| 585 | "<txbuf size>\tTx buffer size (bytes)." |
| 586 | INDENT SOCK_TEST_TOKEN_NUM_WRITES |
| 587 | "<# of writes>\tNumber of txbuf writes to server." "\n"); |
| 588 | } |
| 589 | |
| 590 | static void |
| 591 | cfg_txbuf_size_set (void) |
| 592 | { |
| 593 | sock_client_main_t *scm = &sock_client_main; |
| 594 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 595 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE); |
| 596 | uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10); |
| 597 | |
| 598 | if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 599 | { |
| 600 | ctrl->cfg.txbuf_size = txbuf_size; |
| 601 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 602 | sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , |
| 603 | (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size); |
| 604 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 605 | } |
| 606 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 607 | fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < " |
| 608 | "minimum buf size (%u)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 609 | txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 610 | } |
| 611 | |
| 612 | static void |
| 613 | cfg_num_writes_set (void) |
| 614 | { |
| 615 | sock_client_main_t *scm = &sock_client_main; |
| 616 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 617 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES); |
| 618 | uint32_t num_writes = strtoul ((const char *) p, NULL, 10); |
| 619 | |
| 620 | if (num_writes > 0) |
| 621 | { |
| 622 | ctrl->cfg.num_writes = num_writes; |
| 623 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 624 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 625 | } |
| 626 | else |
| 627 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 628 | fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | static void |
| 633 | cfg_num_test_sockets_set (void) |
| 634 | { |
| 635 | sock_client_main_t *scm = &sock_client_main; |
| 636 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 637 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS); |
| 638 | uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10); |
| 639 | |
| 640 | if ((num_test_sockets > 0) && |
| 641 | (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS)) |
| 642 | { |
| 643 | ctrl->cfg.num_test_sockets = num_test_sockets; |
| 644 | sock_test_connect_test_sockets (num_test_sockets); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 645 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 646 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 647 | } |
| 648 | else |
| 649 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 650 | fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: " |
| 651 | "%u, (%d max)\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 652 | num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | static void |
| 657 | cfg_rxbuf_size_set (void) |
| 658 | { |
| 659 | sock_client_main_t *scm = &sock_client_main; |
| 660 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 661 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE); |
| 662 | uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10); |
| 663 | |
| 664 | if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 665 | { |
| 666 | ctrl->cfg.rxbuf_size = rxbuf_size; |
| 667 | sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , |
| 668 | (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size); |
| 669 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 670 | } |
| 671 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 672 | fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < " |
| 673 | "minimum buf size (%u)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 674 | rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 675 | } |
| 676 | |
| 677 | static void |
| 678 | cfg_verbose_toggle (void) |
| 679 | { |
| 680 | sock_client_main_t *scm = &sock_client_main; |
| 681 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 682 | |
| 683 | ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1; |
| 684 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 685 | |
| 686 | } |
| 687 | |
| 688 | static sock_test_t |
| 689 | parse_input () |
| 690 | { |
| 691 | sock_client_main_t *scm = &sock_client_main; |
| 692 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 693 | sock_test_t rv = SOCK_TEST_TYPE_NONE; |
| 694 | |
| 695 | if (!strcmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf)) |
| 696 | rv = SOCK_TEST_TYPE_EXIT; |
| 697 | |
| 698 | else if (!strcmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf)) |
| 699 | dump_help (); |
| 700 | |
| 701 | else if (!strcmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf)) |
| 702 | scm->dump_cfg = 1; |
| 703 | |
| 704 | else if (!strcmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf)) |
| 705 | cfg_verbose_toggle (); |
| 706 | |
| 707 | else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf, |
| 708 | strlen (SOCK_TEST_TOKEN_TXBUF_SIZE))) |
| 709 | cfg_txbuf_size_set (); |
| 710 | |
| 711 | else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf, |
| 712 | strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS))) |
| 713 | cfg_num_test_sockets_set (); |
| 714 | |
| 715 | else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf, |
| 716 | strlen (SOCK_TEST_TOKEN_NUM_WRITES))) |
| 717 | cfg_num_writes_set (); |
| 718 | |
| 719 | else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf, |
| 720 | strlen (SOCK_TEST_TOKEN_RXBUF_SIZE))) |
| 721 | cfg_rxbuf_size_set (); |
| 722 | |
| 723 | else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf, |
| 724 | strlen (SOCK_TEST_TOKEN_RUN_UNI))) |
| 725 | rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI; |
| 726 | |
| 727 | else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf, |
| 728 | strlen (SOCK_TEST_TOKEN_RUN_BI))) |
| 729 | rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI; |
| 730 | |
| 731 | else |
| 732 | rv = SOCK_TEST_TYPE_ECHO; |
| 733 | |
| 734 | return rv; |
| 735 | } |
| 736 | |
| 737 | void |
| 738 | print_usage_and_exit (void) |
| 739 | { |
| 740 | fprintf (stderr, |
| 741 | "sock_test_client [OPTIONS] <ipaddr> <port>\n" |
| 742 | " OPTIONS\n" |
| 743 | " -h Print this message and exit.\n" |
| 744 | " -c Print test config before test.\n" |
| 745 | " -w <dir> Write test results to <dir>.\n" |
| 746 | " -X Exit after running test.\n" |
| 747 | " -E Run Echo test.\n" |
| 748 | " -N <num-writes> Test Cfg: number of writes.\n" |
| 749 | " -R <rxbuf-size> Test Cfg: rx buffer size.\n" |
| 750 | " -T <txbuf-size> Test Cfg: tx buffer size.\n" |
| 751 | " -U Run Uni-directional test.\n" |
| 752 | " -B Run Bi-directional test.\n" |
| 753 | " -V Verbose mode.\n"); |
| 754 | exit (1); |
| 755 | } |
| 756 | |
| 757 | int |
| 758 | main (int argc, char **argv) |
| 759 | { |
| 760 | sock_client_main_t *scm = &sock_client_main; |
| 761 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 762 | int c, rv, errno_val; |
| 763 | sock_test_t post_test = SOCK_TEST_TYPE_NONE; |
| 764 | |
| 765 | sock_test_cfg_init (&ctrl->cfg); |
| 766 | sock_test_socket_buf_alloc (ctrl); |
| 767 | |
| 768 | opterr = 0; |
| 769 | while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV")) != -1) |
| 770 | switch (c) |
| 771 | { |
| 772 | case 'c': |
| 773 | scm->dump_cfg = 1; |
| 774 | break; |
| 775 | |
| 776 | case 's': |
| 777 | if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1) |
| 778 | if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1) |
| 779 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 780 | fprintf (stderr, "CLIENT: ERROR: Invalid value for " |
| 781 | "option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 782 | print_usage_and_exit (); |
| 783 | } |
| 784 | if (!ctrl->cfg.num_test_sockets || |
| 785 | (ctrl->cfg.num_test_sockets > FD_SETSIZE)) |
| 786 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 787 | fprintf (stderr, "CLIENT: ERROR: Invalid number of " |
| 788 | "sockets (%d) specified for option -%c!\n" |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 789 | " Valid range is 1 - %d\n", |
| 790 | ctrl->cfg.num_test_sockets, c, FD_SETSIZE); |
| 791 | print_usage_and_exit (); |
| 792 | } |
| 793 | break; |
| 794 | |
| 795 | case 'w': |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 796 | fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 797 | break; |
| 798 | |
| 799 | case 'X': |
| 800 | post_test = SOCK_TEST_TYPE_EXIT; |
| 801 | break; |
| 802 | |
| 803 | case 'E': |
| 804 | if (strlen (optarg) > ctrl->txbuf_size) |
| 805 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 806 | fprintf (stderr, "CLIENT: ERROR: Option -%c value " |
| 807 | "larger than txbuf size (%d)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 808 | optopt, ctrl->txbuf_size); |
| 809 | print_usage_and_exit (); |
| 810 | } |
| 811 | strcpy (ctrl->txbuf, optarg); |
| 812 | ctrl->cfg.test = SOCK_TEST_TYPE_ECHO; |
| 813 | break; |
| 814 | |
| 815 | case 'I': |
| 816 | if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1) |
| 817 | if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1) |
| 818 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 819 | fprintf (stderr, "CLIENT: ERROR: Invalid value for " |
| 820 | "option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 821 | print_usage_and_exit (); |
| 822 | } |
| 823 | if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS) |
| 824 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 825 | fprintf (stderr, "CLIENT: ERROR: value greater than max " |
| 826 | "number test sockets (%d)!", |
| 827 | SOCK_TEST_CFG_MAX_TEST_SCKTS); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 828 | print_usage_and_exit (); |
| 829 | } |
| 830 | break; |
| 831 | |
| 832 | case 'N': |
| 833 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1) |
| 834 | if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1) |
| 835 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 836 | fprintf (stderr, "CLIENT: ERROR: Invalid value for " |
| 837 | "option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 838 | print_usage_and_exit (); |
| 839 | } |
| 840 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 841 | break; |
| 842 | |
| 843 | case 'R': |
| 844 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1) |
| 845 | if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1) |
| 846 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 847 | fprintf (stderr, "CLIENT: ERROR: Invalid value for " |
| 848 | "option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 849 | print_usage_and_exit (); |
| 850 | } |
| 851 | if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 852 | { |
| 853 | ctrl->rxbuf_size = ctrl->cfg.rxbuf_size; |
| 854 | sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , |
| 855 | (uint8_t **) & ctrl->rxbuf, |
| 856 | &ctrl->rxbuf_size); |
| 857 | } |
| 858 | else |
| 859 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 860 | fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) " |
| 861 | "less than minumum (%u)\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 862 | ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 863 | print_usage_and_exit (); |
| 864 | } |
| 865 | |
| 866 | break; |
| 867 | |
| 868 | case 'T': |
| 869 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1) |
| 870 | if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1) |
| 871 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 872 | fprintf (stderr, "CLIENT: ERROR: Invalid value " |
| 873 | "for option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 874 | print_usage_and_exit (); |
| 875 | } |
| 876 | if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 877 | { |
| 878 | ctrl->txbuf_size = ctrl->cfg.txbuf_size; |
| 879 | sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , |
| 880 | (uint8_t **) & ctrl->txbuf, |
| 881 | &ctrl->txbuf_size); |
| 882 | ctrl->cfg.total_bytes = |
| 883 | ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 884 | } |
| 885 | else |
| 886 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 887 | fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) " |
| 888 | "less than minumum (%u)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 889 | ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 890 | print_usage_and_exit (); |
| 891 | } |
| 892 | break; |
| 893 | |
| 894 | case 'U': |
| 895 | ctrl->cfg.test = SOCK_TEST_TYPE_UNI; |
| 896 | break; |
| 897 | |
| 898 | case 'B': |
| 899 | ctrl->cfg.test = SOCK_TEST_TYPE_BI; |
| 900 | break; |
| 901 | |
| 902 | case 'V': |
| 903 | ctrl->cfg.verbose = 1; |
| 904 | break; |
| 905 | |
| 906 | case '?': |
| 907 | switch (optopt) |
| 908 | { |
| 909 | case 'E': |
| 910 | case 'I': |
| 911 | case 'N': |
| 912 | case 'R': |
| 913 | case 'T': |
| 914 | case 'w': |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 915 | fprintf (stderr, "CLIENT: ERROR: Option -%c " |
| 916 | "requires an argument.\n", optopt); |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 917 | break; |
| 918 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 919 | default: |
| 920 | if (isprint (optopt)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 921 | fprintf (stderr, "CLIENT: ERROR: Unknown " |
| 922 | "option `-%c'.\n", optopt); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 923 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 924 | fprintf (stderr, "CLIENT: ERROR: Unknown " |
| 925 | "option character `\\x%x'.\n", optopt); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 926 | } |
| 927 | /* fall thru */ |
| 928 | case 'h': |
| 929 | default: |
| 930 | print_usage_and_exit (); |
| 931 | } |
| 932 | |
| 933 | if (argc < (optind + 2)) |
| 934 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 935 | fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 936 | print_usage_and_exit (); |
| 937 | } |
| 938 | |
| 939 | #ifdef VCL_TEST |
| 940 | ctrl->fd = vppcom_app_create ("vcl_test_client"); |
| 941 | if (ctrl->fd < 0) |
| 942 | { |
| 943 | errno = -ctrl->fd; |
| 944 | ctrl->fd = -1; |
| 945 | } |
| 946 | else |
| 947 | { |
| 948 | ctrl->fd = vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP, |
| 949 | 0 /* is_nonblocking */ ); |
| 950 | if (ctrl->fd < 0) |
| 951 | { |
| 952 | errno = -ctrl->fd; |
| 953 | ctrl->fd = -1; |
| 954 | } |
| 955 | } |
| 956 | #else |
| 957 | ctrl->fd = socket (AF_INET, SOCK_STREAM, 0); |
| 958 | #endif |
| 959 | |
| 960 | if (ctrl->fd < 0) |
| 961 | { |
| 962 | errno_val = errno; |
| 963 | perror ("ERROR in main()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 964 | fprintf (stderr, "CLIENT: ERROR: socket " |
| 965 | "failed (errno = %d)!\n", errno_val); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 966 | return ctrl->fd; |
| 967 | } |
| 968 | |
| 969 | memset (&scm->server_addr, 0, sizeof (scm->server_addr)); |
| 970 | |
| 971 | scm->server_addr.sin_family = AF_INET; |
| 972 | inet_pton (AF_INET, argv[optind++], &(scm->server_addr.sin_addr)); |
| 973 | scm->server_addr.sin_port = htons (atoi (argv[optind])); |
| 974 | |
| 975 | #ifdef VCL_TEST |
| 976 | scm->server_endpt.vrf = VPPCOM_VRF_DEFAULT; |
| 977 | scm->server_endpt.is_ip4 = (scm->server_addr.sin_family == AF_INET); |
| 978 | scm->server_endpt.ip = (uint8_t *) & scm->server_addr.sin_addr; |
| 979 | scm->server_endpt.port = (uint16_t) scm->server_addr.sin_port; |
| 980 | #endif |
| 981 | |
| 982 | do |
| 983 | { |
| 984 | printf ("\nCLIENT: Connecting to server...\n"); |
| 985 | |
| 986 | #ifdef VCL_TEST |
| 987 | rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 988 | if (rv) |
| 989 | { |
| 990 | errno = -rv; |
| 991 | rv = -1; |
| 992 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 993 | #else |
| 994 | rv = |
| 995 | connect (ctrl->fd, (struct sockaddr *) &scm->server_addr, |
| 996 | sizeof (scm->server_addr)); |
| 997 | #endif |
| 998 | if (rv < 0) |
| 999 | { |
| 1000 | errno_val = errno; |
| 1001 | perror ("ERROR in main()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1002 | fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1003 | errno_val); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1004 | return -1; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | sock_test_cfg_sync (ctrl); |
| 1008 | printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd); |
| 1009 | } |
| 1010 | while (rv < 0); |
| 1011 | |
| 1012 | sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets); |
| 1013 | |
| 1014 | while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT) |
| 1015 | { |
| 1016 | if (scm->dump_cfg) |
| 1017 | { |
| 1018 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 1019 | scm->dump_cfg = 0; |
| 1020 | } |
| 1021 | |
| 1022 | switch (ctrl->cfg.test) |
| 1023 | { |
| 1024 | case SOCK_TEST_TYPE_ECHO: |
| 1025 | echo_test_client (); |
| 1026 | break; |
| 1027 | |
| 1028 | case SOCK_TEST_TYPE_UNI: |
| 1029 | case SOCK_TEST_TYPE_BI: |
| 1030 | stream_test_client (ctrl->cfg.test); |
| 1031 | break; |
| 1032 | |
| 1033 | case SOCK_TEST_TYPE_EXIT: |
| 1034 | continue; |
| 1035 | |
| 1036 | case SOCK_TEST_TYPE_NONE: |
| 1037 | default: |
| 1038 | break; |
| 1039 | } |
| 1040 | switch (post_test) |
| 1041 | { |
| 1042 | case SOCK_TEST_TYPE_EXIT: |
| 1043 | switch (ctrl->cfg.test) |
| 1044 | { |
| 1045 | case SOCK_TEST_TYPE_EXIT: |
| 1046 | case SOCK_TEST_TYPE_UNI: |
| 1047 | case SOCK_TEST_TYPE_BI: |
| 1048 | case SOCK_TEST_TYPE_ECHO: |
| 1049 | ctrl->cfg.test = SOCK_TEST_TYPE_EXIT; |
| 1050 | continue; |
| 1051 | |
| 1052 | case SOCK_TEST_TYPE_NONE: |
| 1053 | default: |
| 1054 | break; |
| 1055 | } |
| 1056 | break; |
| 1057 | |
| 1058 | case SOCK_TEST_TYPE_NONE: |
| 1059 | case SOCK_TEST_TYPE_ECHO: |
| 1060 | case SOCK_TEST_TYPE_UNI: |
| 1061 | case SOCK_TEST_TYPE_BI: |
| 1062 | default: |
| 1063 | break; |
| 1064 | } |
| 1065 | |
| 1066 | memset (ctrl->txbuf, 0, ctrl->txbuf_size); |
| 1067 | memset (ctrl->rxbuf, 0, ctrl->rxbuf_size); |
| 1068 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1069 | printf ("\nCLIENT: Type some characters and hit <return>\n" |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1070 | "('" SOCK_TEST_TOKEN_HELP "' for help): "); |
| 1071 | |
| 1072 | if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL) |
| 1073 | { |
| 1074 | if (strlen (ctrl->txbuf) == 1) |
| 1075 | { |
| 1076 | printf ("\nCLIENT: Nothing to send! Please try again...\n"); |
| 1077 | continue; |
| 1078 | } |
| 1079 | ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline. |
| 1080 | |
| 1081 | /* Parse input for keywords */ |
| 1082 | ctrl->cfg.test = parse_input (); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | exit_client (); |
| 1087 | #ifdef VCL_TEST |
| 1088 | vppcom_session_close (ctrl->fd); |
| 1089 | vppcom_app_destroy (); |
| 1090 | #else |
| 1091 | close (ctrl->fd); |
| 1092 | #endif |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * fd.io coding-style-patch-verification: ON |
| 1098 | * |
| 1099 | * Local Variables: |
| 1100 | * eval: (c-set-style "gnu") |
| 1101 | * End: |
| 1102 | */ |