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