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 = |
Dave Wallace | c04cbf1 | 2018-02-07 18:14:02 -0500 | [diff] [blame^] | 511 | vppcom_session_create (VPPCOM_PROTO_TCP, 1 /* is_nonblocking */ ); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 512 | if (tsock->fd < 0) |
| 513 | { |
| 514 | errno = -tsock->fd; |
| 515 | tsock->fd = -1; |
| 516 | } |
| 517 | #else |
| 518 | tsock->fd = socket (AF_INET, SOCK_STREAM, 0); |
| 519 | #endif |
| 520 | if (tsock->fd < 0) |
| 521 | { |
| 522 | errno_val = errno; |
| 523 | perror ("ERROR in sock_test_connect_test_sockets()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 524 | fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 525 | errno_val); |
| 526 | return tsock->fd; |
| 527 | } |
| 528 | |
| 529 | #ifdef VCL_TEST |
| 530 | rv = vppcom_session_connect (tsock->fd, &scm->server_endpt); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 531 | if (rv) |
| 532 | { |
| 533 | errno = -rv; |
| 534 | rv = -1; |
| 535 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 536 | #else |
| 537 | rv = |
| 538 | connect (tsock->fd, (struct sockaddr *) &scm->server_addr, |
| 539 | sizeof (scm->server_addr)); |
| 540 | #endif |
| 541 | if (rv < 0) |
| 542 | { |
| 543 | errno_val = errno; |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 544 | perror ("ERROR in sock_test_connect_test_sockets()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 545 | fprintf (stderr, "CLIENT: ERROR: connect failed " |
| 546 | "(errno = %d)!\n", errno_val); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 547 | return -1; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 548 | } |
| 549 | tsock->cfg = ctrl->cfg; |
| 550 | sock_test_socket_buf_alloc (tsock); |
| 551 | sock_test_cfg_sync (tsock); |
| 552 | |
| 553 | printf ("CLIENT (fd %d): Test socket %d connected.\n", |
| 554 | tsock->fd, i); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | scm->num_test_sockets = num_test_sockets; |
| 559 | printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static void |
| 564 | dump_help (void) |
| 565 | { |
| 566 | #define INDENT "\n " |
| 567 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 568 | printf ("CLIENT: Test configuration commands:" |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 569 | INDENT SOCK_TEST_TOKEN_HELP |
| 570 | "\t\t\tDisplay help." |
| 571 | INDENT SOCK_TEST_TOKEN_EXIT |
| 572 | "\t\t\tExit test client & server." |
| 573 | INDENT SOCK_TEST_TOKEN_SHOW_CFG |
| 574 | "\t\t\tShow the current test cfg." |
| 575 | INDENT SOCK_TEST_TOKEN_RUN_UNI |
| 576 | "\t\t\tRun the Uni-directional test." |
| 577 | INDENT SOCK_TEST_TOKEN_RUN_BI |
| 578 | "\t\t\tRun the Bi-directional test." |
| 579 | INDENT SOCK_TEST_TOKEN_VERBOSE |
| 580 | "\t\t\tToggle verbose setting." |
| 581 | INDENT SOCK_TEST_TOKEN_RXBUF_SIZE |
| 582 | "<rxbuf size>\tRx buffer size (bytes)." |
| 583 | INDENT SOCK_TEST_TOKEN_TXBUF_SIZE |
| 584 | "<txbuf size>\tTx buffer size (bytes)." |
| 585 | INDENT SOCK_TEST_TOKEN_NUM_WRITES |
| 586 | "<# of writes>\tNumber of txbuf writes to server." "\n"); |
| 587 | } |
| 588 | |
| 589 | static void |
| 590 | cfg_txbuf_size_set (void) |
| 591 | { |
| 592 | sock_client_main_t *scm = &sock_client_main; |
| 593 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 594 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE); |
| 595 | uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10); |
| 596 | |
| 597 | if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 598 | { |
| 599 | ctrl->cfg.txbuf_size = txbuf_size; |
| 600 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 601 | sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , |
| 602 | (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size); |
| 603 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 604 | } |
| 605 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 606 | fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < " |
| 607 | "minimum buf size (%u)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 608 | txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 609 | } |
| 610 | |
| 611 | static void |
| 612 | cfg_num_writes_set (void) |
| 613 | { |
| 614 | sock_client_main_t *scm = &sock_client_main; |
| 615 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 616 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES); |
| 617 | uint32_t num_writes = strtoul ((const char *) p, NULL, 10); |
| 618 | |
| 619 | if (num_writes > 0) |
| 620 | { |
| 621 | ctrl->cfg.num_writes = num_writes; |
| 622 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 623 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 624 | } |
| 625 | else |
| 626 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 627 | fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | static void |
| 632 | cfg_num_test_sockets_set (void) |
| 633 | { |
| 634 | sock_client_main_t *scm = &sock_client_main; |
| 635 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 636 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS); |
| 637 | uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10); |
| 638 | |
| 639 | if ((num_test_sockets > 0) && |
| 640 | (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS)) |
| 641 | { |
| 642 | ctrl->cfg.num_test_sockets = num_test_sockets; |
| 643 | sock_test_connect_test_sockets (num_test_sockets); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 644 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 645 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 646 | } |
| 647 | else |
| 648 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 649 | fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: " |
| 650 | "%u, (%d max)\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 651 | num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static void |
| 656 | cfg_rxbuf_size_set (void) |
| 657 | { |
| 658 | sock_client_main_t *scm = &sock_client_main; |
| 659 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 660 | char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE); |
| 661 | uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10); |
| 662 | |
| 663 | if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 664 | { |
| 665 | ctrl->cfg.rxbuf_size = rxbuf_size; |
| 666 | sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , |
| 667 | (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size); |
| 668 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 669 | } |
| 670 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 671 | fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < " |
| 672 | "minimum buf size (%u)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 673 | rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 674 | } |
| 675 | |
| 676 | static void |
| 677 | cfg_verbose_toggle (void) |
| 678 | { |
| 679 | sock_client_main_t *scm = &sock_client_main; |
| 680 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 681 | |
| 682 | ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1; |
| 683 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 684 | |
| 685 | } |
| 686 | |
| 687 | static sock_test_t |
| 688 | parse_input () |
| 689 | { |
| 690 | sock_client_main_t *scm = &sock_client_main; |
| 691 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 692 | sock_test_t rv = SOCK_TEST_TYPE_NONE; |
| 693 | |
| 694 | if (!strcmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf)) |
| 695 | rv = SOCK_TEST_TYPE_EXIT; |
| 696 | |
| 697 | else if (!strcmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf)) |
| 698 | dump_help (); |
| 699 | |
| 700 | else if (!strcmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf)) |
| 701 | scm->dump_cfg = 1; |
| 702 | |
| 703 | else if (!strcmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf)) |
| 704 | cfg_verbose_toggle (); |
| 705 | |
| 706 | else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf, |
| 707 | strlen (SOCK_TEST_TOKEN_TXBUF_SIZE))) |
| 708 | cfg_txbuf_size_set (); |
| 709 | |
| 710 | else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf, |
| 711 | strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS))) |
| 712 | cfg_num_test_sockets_set (); |
| 713 | |
| 714 | else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf, |
| 715 | strlen (SOCK_TEST_TOKEN_NUM_WRITES))) |
| 716 | cfg_num_writes_set (); |
| 717 | |
| 718 | else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf, |
| 719 | strlen (SOCK_TEST_TOKEN_RXBUF_SIZE))) |
| 720 | cfg_rxbuf_size_set (); |
| 721 | |
| 722 | else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf, |
| 723 | strlen (SOCK_TEST_TOKEN_RUN_UNI))) |
| 724 | rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI; |
| 725 | |
| 726 | else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf, |
| 727 | strlen (SOCK_TEST_TOKEN_RUN_BI))) |
| 728 | rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI; |
| 729 | |
| 730 | else |
| 731 | rv = SOCK_TEST_TYPE_ECHO; |
| 732 | |
| 733 | return rv; |
| 734 | } |
| 735 | |
| 736 | void |
| 737 | print_usage_and_exit (void) |
| 738 | { |
| 739 | fprintf (stderr, |
| 740 | "sock_test_client [OPTIONS] <ipaddr> <port>\n" |
| 741 | " OPTIONS\n" |
| 742 | " -h Print this message and exit.\n" |
| 743 | " -c Print test config before test.\n" |
| 744 | " -w <dir> Write test results to <dir>.\n" |
| 745 | " -X Exit after running test.\n" |
| 746 | " -E Run Echo test.\n" |
| 747 | " -N <num-writes> Test Cfg: number of writes.\n" |
| 748 | " -R <rxbuf-size> Test Cfg: rx buffer size.\n" |
| 749 | " -T <txbuf-size> Test Cfg: tx buffer size.\n" |
| 750 | " -U Run Uni-directional test.\n" |
| 751 | " -B Run Bi-directional test.\n" |
| 752 | " -V Verbose mode.\n"); |
| 753 | exit (1); |
| 754 | } |
| 755 | |
| 756 | int |
| 757 | main (int argc, char **argv) |
| 758 | { |
| 759 | sock_client_main_t *scm = &sock_client_main; |
| 760 | sock_test_socket_t *ctrl = &scm->ctrl_socket; |
| 761 | int c, rv, errno_val; |
| 762 | sock_test_t post_test = SOCK_TEST_TYPE_NONE; |
| 763 | |
| 764 | sock_test_cfg_init (&ctrl->cfg); |
| 765 | sock_test_socket_buf_alloc (ctrl); |
| 766 | |
| 767 | opterr = 0; |
| 768 | while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV")) != -1) |
| 769 | switch (c) |
| 770 | { |
| 771 | case 'c': |
| 772 | scm->dump_cfg = 1; |
| 773 | break; |
| 774 | |
| 775 | case 's': |
| 776 | if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1) |
| 777 | if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1) |
| 778 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 779 | fprintf (stderr, "CLIENT: ERROR: Invalid value for " |
| 780 | "option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 781 | print_usage_and_exit (); |
| 782 | } |
| 783 | if (!ctrl->cfg.num_test_sockets || |
| 784 | (ctrl->cfg.num_test_sockets > FD_SETSIZE)) |
| 785 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 786 | fprintf (stderr, "CLIENT: ERROR: Invalid number of " |
| 787 | "sockets (%d) specified for option -%c!\n" |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 788 | " Valid range is 1 - %d\n", |
| 789 | ctrl->cfg.num_test_sockets, c, FD_SETSIZE); |
| 790 | print_usage_and_exit (); |
| 791 | } |
| 792 | break; |
| 793 | |
| 794 | case 'w': |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 795 | fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 796 | break; |
| 797 | |
| 798 | case 'X': |
| 799 | post_test = SOCK_TEST_TYPE_EXIT; |
| 800 | break; |
| 801 | |
| 802 | case 'E': |
| 803 | if (strlen (optarg) > ctrl->txbuf_size) |
| 804 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 805 | fprintf (stderr, "CLIENT: ERROR: Option -%c value " |
| 806 | "larger than txbuf size (%d)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 807 | optopt, ctrl->txbuf_size); |
| 808 | print_usage_and_exit (); |
| 809 | } |
| 810 | strcpy (ctrl->txbuf, optarg); |
| 811 | ctrl->cfg.test = SOCK_TEST_TYPE_ECHO; |
| 812 | break; |
| 813 | |
| 814 | case 'I': |
| 815 | if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1) |
| 816 | if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1) |
| 817 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 818 | fprintf (stderr, "CLIENT: ERROR: Invalid value for " |
| 819 | "option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 820 | print_usage_and_exit (); |
| 821 | } |
| 822 | if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS) |
| 823 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 824 | fprintf (stderr, "CLIENT: ERROR: value greater than max " |
| 825 | "number test sockets (%d)!", |
| 826 | SOCK_TEST_CFG_MAX_TEST_SCKTS); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 827 | print_usage_and_exit (); |
| 828 | } |
| 829 | break; |
| 830 | |
| 831 | case 'N': |
| 832 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1) |
| 833 | if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1) |
| 834 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 835 | fprintf (stderr, "CLIENT: ERROR: Invalid value for " |
| 836 | "option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 837 | print_usage_and_exit (); |
| 838 | } |
| 839 | ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 840 | break; |
| 841 | |
| 842 | case 'R': |
| 843 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1) |
| 844 | if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1) |
| 845 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 846 | fprintf (stderr, "CLIENT: ERROR: Invalid value for " |
| 847 | "option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 848 | print_usage_and_exit (); |
| 849 | } |
| 850 | if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 851 | { |
| 852 | ctrl->rxbuf_size = ctrl->cfg.rxbuf_size; |
| 853 | sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , |
| 854 | (uint8_t **) & ctrl->rxbuf, |
| 855 | &ctrl->rxbuf_size); |
| 856 | } |
| 857 | else |
| 858 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 859 | fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) " |
| 860 | "less than minumum (%u)\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 861 | ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 862 | print_usage_and_exit (); |
| 863 | } |
| 864 | |
| 865 | break; |
| 866 | |
| 867 | case 'T': |
| 868 | if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1) |
| 869 | if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1) |
| 870 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 871 | fprintf (stderr, "CLIENT: ERROR: Invalid value " |
| 872 | "for option -%c!\n", c); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 873 | print_usage_and_exit (); |
| 874 | } |
| 875 | if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) |
| 876 | { |
| 877 | ctrl->txbuf_size = ctrl->cfg.txbuf_size; |
| 878 | sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , |
| 879 | (uint8_t **) & ctrl->txbuf, |
| 880 | &ctrl->txbuf_size); |
| 881 | ctrl->cfg.total_bytes = |
| 882 | ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; |
| 883 | } |
| 884 | else |
| 885 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 886 | fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) " |
| 887 | "less than minumum (%u)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 888 | ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); |
| 889 | print_usage_and_exit (); |
| 890 | } |
| 891 | break; |
| 892 | |
| 893 | case 'U': |
| 894 | ctrl->cfg.test = SOCK_TEST_TYPE_UNI; |
| 895 | break; |
| 896 | |
| 897 | case 'B': |
| 898 | ctrl->cfg.test = SOCK_TEST_TYPE_BI; |
| 899 | break; |
| 900 | |
| 901 | case 'V': |
| 902 | ctrl->cfg.verbose = 1; |
| 903 | break; |
| 904 | |
| 905 | case '?': |
| 906 | switch (optopt) |
| 907 | { |
| 908 | case 'E': |
| 909 | case 'I': |
| 910 | case 'N': |
| 911 | case 'R': |
| 912 | case 'T': |
| 913 | case 'w': |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 914 | fprintf (stderr, "CLIENT: ERROR: Option -%c " |
| 915 | "requires an argument.\n", optopt); |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 916 | break; |
| 917 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 918 | default: |
| 919 | if (isprint (optopt)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 920 | fprintf (stderr, "CLIENT: ERROR: Unknown " |
| 921 | "option `-%c'.\n", optopt); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 922 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 923 | fprintf (stderr, "CLIENT: ERROR: Unknown " |
| 924 | "option character `\\x%x'.\n", optopt); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 925 | } |
| 926 | /* fall thru */ |
| 927 | case 'h': |
| 928 | default: |
| 929 | print_usage_and_exit (); |
| 930 | } |
| 931 | |
| 932 | if (argc < (optind + 2)) |
| 933 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 934 | fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 935 | print_usage_and_exit (); |
| 936 | } |
| 937 | |
| 938 | #ifdef VCL_TEST |
| 939 | ctrl->fd = vppcom_app_create ("vcl_test_client"); |
| 940 | if (ctrl->fd < 0) |
| 941 | { |
| 942 | errno = -ctrl->fd; |
| 943 | ctrl->fd = -1; |
| 944 | } |
| 945 | else |
| 946 | { |
Dave Wallace | c04cbf1 | 2018-02-07 18:14:02 -0500 | [diff] [blame^] | 947 | ctrl->fd = vppcom_session_create (VPPCOM_PROTO_TCP, |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 948 | 0 /* is_nonblocking */ ); |
| 949 | if (ctrl->fd < 0) |
| 950 | { |
| 951 | errno = -ctrl->fd; |
| 952 | ctrl->fd = -1; |
| 953 | } |
| 954 | } |
| 955 | #else |
| 956 | ctrl->fd = socket (AF_INET, SOCK_STREAM, 0); |
| 957 | #endif |
| 958 | |
| 959 | if (ctrl->fd < 0) |
| 960 | { |
| 961 | errno_val = errno; |
| 962 | perror ("ERROR in main()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 963 | fprintf (stderr, "CLIENT: ERROR: socket " |
| 964 | "failed (errno = %d)!\n", errno_val); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 965 | return ctrl->fd; |
| 966 | } |
| 967 | |
| 968 | memset (&scm->server_addr, 0, sizeof (scm->server_addr)); |
| 969 | |
| 970 | scm->server_addr.sin_family = AF_INET; |
| 971 | inet_pton (AF_INET, argv[optind++], &(scm->server_addr.sin_addr)); |
| 972 | scm->server_addr.sin_port = htons (atoi (argv[optind])); |
| 973 | |
| 974 | #ifdef VCL_TEST |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 975 | scm->server_endpt.is_ip4 = (scm->server_addr.sin_family == AF_INET); |
| 976 | scm->server_endpt.ip = (uint8_t *) & scm->server_addr.sin_addr; |
| 977 | scm->server_endpt.port = (uint16_t) scm->server_addr.sin_port; |
| 978 | #endif |
| 979 | |
| 980 | do |
| 981 | { |
| 982 | printf ("\nCLIENT: Connecting to server...\n"); |
| 983 | |
| 984 | #ifdef VCL_TEST |
| 985 | rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 986 | if (rv) |
| 987 | { |
| 988 | errno = -rv; |
| 989 | rv = -1; |
| 990 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 991 | #else |
| 992 | rv = |
| 993 | connect (ctrl->fd, (struct sockaddr *) &scm->server_addr, |
| 994 | sizeof (scm->server_addr)); |
| 995 | #endif |
| 996 | if (rv < 0) |
| 997 | { |
| 998 | errno_val = errno; |
| 999 | perror ("ERROR in main()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1000 | fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1001 | errno_val); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1002 | return -1; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | sock_test_cfg_sync (ctrl); |
| 1006 | printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd); |
| 1007 | } |
| 1008 | while (rv < 0); |
| 1009 | |
| 1010 | sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets); |
| 1011 | |
| 1012 | while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT) |
| 1013 | { |
| 1014 | if (scm->dump_cfg) |
| 1015 | { |
| 1016 | sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); |
| 1017 | scm->dump_cfg = 0; |
| 1018 | } |
| 1019 | |
| 1020 | switch (ctrl->cfg.test) |
| 1021 | { |
| 1022 | case SOCK_TEST_TYPE_ECHO: |
| 1023 | echo_test_client (); |
| 1024 | break; |
| 1025 | |
| 1026 | case SOCK_TEST_TYPE_UNI: |
| 1027 | case SOCK_TEST_TYPE_BI: |
| 1028 | stream_test_client (ctrl->cfg.test); |
| 1029 | break; |
| 1030 | |
| 1031 | case SOCK_TEST_TYPE_EXIT: |
| 1032 | continue; |
| 1033 | |
| 1034 | case SOCK_TEST_TYPE_NONE: |
| 1035 | default: |
| 1036 | break; |
| 1037 | } |
| 1038 | switch (post_test) |
| 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 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1067 | printf ("\nCLIENT: Type some characters and hit <return>\n" |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1068 | "('" SOCK_TEST_TOKEN_HELP "' for help): "); |
| 1069 | |
| 1070 | if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL) |
| 1071 | { |
| 1072 | if (strlen (ctrl->txbuf) == 1) |
| 1073 | { |
| 1074 | printf ("\nCLIENT: Nothing to send! Please try again...\n"); |
| 1075 | continue; |
| 1076 | } |
| 1077 | ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline. |
| 1078 | |
| 1079 | /* Parse input for keywords */ |
| 1080 | ctrl->cfg.test = parse_input (); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | exit_client (); |
| 1085 | #ifdef VCL_TEST |
| 1086 | vppcom_session_close (ctrl->fd); |
| 1087 | vppcom_app_destroy (); |
| 1088 | #else |
| 1089 | close (ctrl->fd); |
| 1090 | #endif |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * fd.io coding-style-patch-verification: ON |
| 1096 | * |
| 1097 | * Local Variables: |
| 1098 | * eval: (c-set-style "gnu") |
| 1099 | * End: |
| 1100 | */ |