Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * echo_client.c - vpp built-in echo client code |
| 3 | * |
| 4 | * Copyright (c) 2017 by Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vlibapi/api.h> |
| 20 | #include <vlibmemory/api.h> |
| 21 | #include <vnet/session-apps/echo_client.h> |
| 22 | |
| 23 | echo_client_main_t echo_client_main; |
| 24 | |
| 25 | #define ECHO_CLIENT_DBG (0) |
| 26 | |
| 27 | static void |
| 28 | signal_evt_to_cli_i (int *code) |
| 29 | { |
| 30 | echo_client_main_t *ecm = &echo_client_main; |
| 31 | ASSERT (vlib_get_thread_index () == 0); |
| 32 | vlib_process_signal_event (ecm->vlib_main, ecm->cli_node_index, *code, 0); |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | signal_evt_to_cli (int code) |
| 37 | { |
| 38 | if (vlib_get_thread_index () != 0) |
| 39 | vl_api_rpc_call_main_thread (signal_evt_to_cli_i, (u8 *) & code, |
| 40 | sizeof (code)); |
| 41 | else |
| 42 | signal_evt_to_cli_i (&code); |
| 43 | } |
| 44 | |
| 45 | static void |
| 46 | send_data_chunk (echo_client_main_t * ecm, session_t * s) |
| 47 | { |
| 48 | u8 *test_data = ecm->connect_test_data; |
| 49 | int test_buf_offset; |
| 50 | u32 bytes_this_chunk; |
| 51 | session_fifo_event_t evt; |
| 52 | svm_fifo_t *txf; |
| 53 | int rv; |
| 54 | |
| 55 | ASSERT (vec_len (test_data) > 0); |
| 56 | |
| 57 | test_buf_offset = s->bytes_sent % vec_len (test_data); |
| 58 | bytes_this_chunk = vec_len (test_data) - test_buf_offset; |
| 59 | |
| 60 | bytes_this_chunk = bytes_this_chunk < s->bytes_to_send |
| 61 | ? bytes_this_chunk : s->bytes_to_send; |
| 62 | |
| 63 | txf = s->server_tx_fifo; |
| 64 | rv = svm_fifo_enqueue_nowait (txf, bytes_this_chunk, |
| 65 | test_data + test_buf_offset); |
| 66 | |
| 67 | /* If we managed to enqueue data... */ |
| 68 | if (rv > 0) |
| 69 | { |
| 70 | /* Account for it... */ |
| 71 | s->bytes_to_send -= rv; |
| 72 | s->bytes_sent += rv; |
| 73 | |
| 74 | if (ECHO_CLIENT_DBG) |
| 75 | { |
| 76 | /* *INDENT-OFF* */ |
| 77 | ELOG_TYPE_DECLARE (e) = |
| 78 | { |
| 79 | .format = "tx-enq: xfer %d bytes, sent %u remain %u", |
| 80 | .format_args = "i4i4i4", |
| 81 | }; |
| 82 | /* *INDENT-ON* */ |
| 83 | struct |
| 84 | { |
| 85 | u32 data[3]; |
| 86 | } *ed; |
| 87 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 88 | ed->data[0] = rv; |
| 89 | ed->data[1] = s->bytes_sent; |
| 90 | ed->data[2] = s->bytes_to_send; |
| 91 | } |
| 92 | |
| 93 | /* Poke the session layer */ |
| 94 | if (svm_fifo_set_event (txf)) |
| 95 | { |
| 96 | /* Fabricate TX event, send to vpp */ |
| 97 | evt.fifo = txf; |
| 98 | evt.event_type = FIFO_EVENT_APP_TX; |
| 99 | |
| 100 | if (svm_queue_add |
| 101 | (ecm->vpp_event_queue[txf->master_thread_index], (u8 *) & evt, |
| 102 | 0 /* do wait for mutex */ )) |
| 103 | clib_warning ("could not enqueue event"); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | receive_data_chunk (echo_client_main_t * ecm, session_t * s) |
| 110 | { |
| 111 | svm_fifo_t *rx_fifo = s->server_rx_fifo; |
| 112 | u32 my_thread_index = vlib_get_thread_index (); |
| 113 | int n_read, i; |
| 114 | |
| 115 | if (ecm->test_bytes) |
| 116 | { |
| 117 | n_read = svm_fifo_dequeue_nowait (rx_fifo, |
| 118 | vec_len (ecm->rx_buf |
| 119 | [my_thread_index]), |
| 120 | ecm->rx_buf[my_thread_index]); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | n_read = svm_fifo_max_dequeue (rx_fifo); |
| 125 | svm_fifo_dequeue_drop (rx_fifo, n_read); |
| 126 | } |
| 127 | |
| 128 | if (n_read > 0) |
| 129 | { |
| 130 | if (ECHO_CLIENT_DBG) |
| 131 | { |
| 132 | /* *INDENT-OFF* */ |
| 133 | ELOG_TYPE_DECLARE (e) = |
| 134 | { |
| 135 | .format = "rx-deq: %d bytes", |
| 136 | .format_args = "i4", |
| 137 | }; |
| 138 | /* *INDENT-ON* */ |
| 139 | struct |
| 140 | { |
| 141 | u32 data[1]; |
| 142 | } *ed; |
| 143 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 144 | ed->data[0] = n_read; |
| 145 | } |
| 146 | |
| 147 | if (ecm->test_bytes) |
| 148 | { |
| 149 | for (i = 0; i < n_read; i++) |
| 150 | { |
| 151 | if (ecm->rx_buf[my_thread_index][i] |
| 152 | != ((s->bytes_received + i) & 0xff)) |
| 153 | { |
| 154 | clib_warning ("read %d error at byte %lld, 0x%x not 0x%x", |
| 155 | n_read, s->bytes_received + i, |
| 156 | ecm->rx_buf[my_thread_index][i], |
| 157 | ((s->bytes_received + i) & 0xff)); |
| 158 | ecm->test_failed = 1; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | s->bytes_to_receive -= n_read; |
| 163 | s->bytes_received += n_read; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static uword |
| 168 | echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 169 | vlib_frame_t * frame) |
| 170 | { |
| 171 | echo_client_main_t *ecm = &echo_client_main; |
| 172 | int my_thread_index = vlib_get_thread_index (); |
| 173 | session_t *sp; |
| 174 | int i; |
| 175 | int delete_session; |
| 176 | u32 *connection_indices; |
| 177 | u32 *connections_this_batch; |
| 178 | u32 nconnections_this_batch; |
| 179 | |
| 180 | connection_indices = ecm->connection_index_by_thread[my_thread_index]; |
| 181 | connections_this_batch = |
| 182 | ecm->connections_this_batch_by_thread[my_thread_index]; |
| 183 | |
| 184 | if ((ecm->run_test == 0) || |
| 185 | ((vec_len (connection_indices) == 0) |
| 186 | && vec_len (connections_this_batch) == 0)) |
| 187 | return 0; |
| 188 | |
| 189 | /* Grab another pile of connections */ |
| 190 | if (PREDICT_FALSE (vec_len (connections_this_batch) == 0)) |
| 191 | { |
| 192 | nconnections_this_batch = |
| 193 | clib_min (ecm->connections_per_batch, vec_len (connection_indices)); |
| 194 | |
| 195 | ASSERT (nconnections_this_batch > 0); |
| 196 | vec_validate (connections_this_batch, nconnections_this_batch - 1); |
| 197 | clib_memcpy (connections_this_batch, |
| 198 | connection_indices + vec_len (connection_indices) |
| 199 | - nconnections_this_batch, |
| 200 | nconnections_this_batch * sizeof (u32)); |
| 201 | _vec_len (connection_indices) -= nconnections_this_batch; |
| 202 | } |
| 203 | |
| 204 | if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch |
| 205 | && ecm->prev_conns == vec_len (connections_this_batch))) |
| 206 | { |
| 207 | ecm->repeats++; |
| 208 | ecm->prev_conns = vec_len (connections_this_batch); |
| 209 | if (ecm->repeats == 500000) |
| 210 | { |
| 211 | clib_warning ("stuck clients"); |
| 212 | } |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | ecm->prev_conns = vec_len (connections_this_batch); |
| 217 | ecm->repeats = 0; |
| 218 | } |
| 219 | |
| 220 | for (i = 0; i < vec_len (connections_this_batch); i++) |
| 221 | { |
| 222 | delete_session = 1; |
| 223 | |
| 224 | sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]); |
| 225 | |
| 226 | if (sp->bytes_to_send > 0) |
| 227 | { |
| 228 | send_data_chunk (ecm, sp); |
| 229 | delete_session = 0; |
| 230 | } |
| 231 | if (sp->bytes_to_receive > 0) |
| 232 | { |
| 233 | receive_data_chunk (ecm, sp); |
| 234 | delete_session = 0; |
| 235 | } |
| 236 | if (PREDICT_FALSE (delete_session == 1)) |
| 237 | { |
| 238 | u32 index, thread_index; |
| 239 | stream_session_t *s; |
| 240 | |
| 241 | __sync_fetch_and_add (&ecm->tx_total, sp->bytes_sent); |
| 242 | __sync_fetch_and_add (&ecm->rx_total, sp->bytes_received); |
| 243 | |
| 244 | session_parse_handle (sp->vpp_session_handle, |
| 245 | &index, &thread_index); |
| 246 | s = session_get_if_valid (index, thread_index); |
| 247 | |
| 248 | if (s) |
| 249 | { |
| 250 | vnet_disconnect_args_t _a, *a = &_a; |
| 251 | a->handle = session_handle (s); |
| 252 | a->app_index = ecm->app_index; |
| 253 | vnet_disconnect_session (a); |
| 254 | |
| 255 | vec_delete (connections_this_batch, 1, i); |
| 256 | i--; |
| 257 | __sync_fetch_and_add (&ecm->ready_connections, -1); |
| 258 | } |
| 259 | else |
| 260 | clib_warning ("session AWOL?"); |
| 261 | |
| 262 | /* Kick the debug CLI process */ |
| 263 | if (ecm->ready_connections == 0) |
| 264 | { |
| 265 | signal_evt_to_cli (2); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | ecm->connection_index_by_thread[my_thread_index] = connection_indices; |
| 271 | ecm->connections_this_batch_by_thread[my_thread_index] = |
| 272 | connections_this_batch; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /* *INDENT-OFF* */ |
| 277 | VLIB_REGISTER_NODE (echo_clients_node) = |
| 278 | { |
| 279 | .function = echo_client_node_fn, |
| 280 | .name = "echo-clients", |
| 281 | .type = VLIB_NODE_TYPE_INPUT, |
| 282 | .state = VLIB_NODE_STATE_DISABLED, |
| 283 | }; |
| 284 | /* *INDENT-ON* */ |
| 285 | |
| 286 | static int |
| 287 | create_api_loopback (echo_client_main_t * ecm) |
| 288 | { |
| 289 | api_main_t *am = &api_main; |
| 290 | vl_shmem_hdr_t *shmem_hdr; |
| 291 | |
| 292 | shmem_hdr = am->shmem_hdr; |
| 293 | ecm->vl_input_queue = shmem_hdr->vl_input_queue; |
| 294 | ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client", |
| 295 | ecm->vl_input_queue); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | echo_clients_init (vlib_main_t * vm) |
| 301 | { |
| 302 | echo_client_main_t *ecm = &echo_client_main; |
| 303 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 304 | u32 num_threads; |
| 305 | int i; |
| 306 | |
| 307 | if (create_api_loopback (ecm)) |
| 308 | return -1; |
| 309 | |
| 310 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 311 | |
| 312 | /* Init test data. Bigecmuffer */ |
| 313 | vec_validate (ecm->connect_test_data, 1024 * 1024 - 1); |
| 314 | for (i = 0; i < vec_len (ecm->connect_test_data); i++) |
| 315 | ecm->connect_test_data[i] = i & 0xff; |
| 316 | |
| 317 | vec_validate (ecm->rx_buf, num_threads - 1); |
| 318 | for (i = 0; i < num_threads; i++) |
| 319 | vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1); |
| 320 | |
| 321 | ecm->is_init = 1; |
| 322 | |
| 323 | vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains); |
| 324 | vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains); |
| 325 | vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int |
| 331 | echo_clients_session_connected_callback (u32 app_index, u32 api_context, |
| 332 | stream_session_t * s, u8 is_fail) |
| 333 | { |
| 334 | echo_client_main_t *ecm = &echo_client_main; |
| 335 | session_t *session; |
| 336 | u32 session_index; |
| 337 | u8 thread_index = vlib_get_thread_index (); |
| 338 | |
| 339 | if (is_fail) |
| 340 | { |
| 341 | clib_warning ("connection %d failed!", api_context); |
| 342 | signal_evt_to_cli (-1); |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | ASSERT (s->thread_index == thread_index); |
| 347 | |
| 348 | if (!ecm->vpp_event_queue[thread_index]) |
| 349 | ecm->vpp_event_queue[thread_index] = |
| 350 | session_manager_get_vpp_event_queue (thread_index); |
| 351 | |
| 352 | /* |
| 353 | * Setup session |
| 354 | */ |
| 355 | clib_spinlock_lock_if_init (&ecm->sessions_lock); |
| 356 | pool_get (ecm->sessions, session); |
| 357 | clib_spinlock_unlock_if_init (&ecm->sessions_lock); |
| 358 | |
| 359 | memset (session, 0, sizeof (*session)); |
| 360 | session_index = session - ecm->sessions; |
| 361 | session->bytes_to_send = ecm->bytes_to_send; |
| 362 | session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send; |
| 363 | session->server_rx_fifo = s->server_rx_fifo; |
| 364 | session->server_rx_fifo->client_session_index = session_index; |
| 365 | session->server_tx_fifo = s->server_tx_fifo; |
| 366 | session->server_tx_fifo->client_session_index = session_index; |
| 367 | session->vpp_session_handle = session_handle (s); |
| 368 | |
| 369 | vec_add1 (ecm->connection_index_by_thread[thread_index], session_index); |
| 370 | __sync_fetch_and_add (&ecm->ready_connections, 1); |
| 371 | if (ecm->ready_connections == ecm->expected_connections) |
| 372 | { |
| 373 | ecm->run_test = 1; |
| 374 | /* Signal the CLI process that the action is starting... */ |
| 375 | signal_evt_to_cli (1); |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void |
| 382 | echo_clients_session_reset_callback (stream_session_t * s) |
| 383 | { |
| 384 | if (s->session_state == SESSION_STATE_READY) |
| 385 | clib_warning ("Reset active connection %U", format_stream_session, s, 2); |
| 386 | stream_session_cleanup (s); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | static int |
| 391 | echo_clients_session_create_callback (stream_session_t * s) |
| 392 | { |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static void |
| 397 | echo_clients_session_disconnect_callback (stream_session_t * s) |
| 398 | { |
| 399 | echo_client_main_t *ecm = &echo_client_main; |
| 400 | vnet_disconnect_args_t _a, *a = &_a; |
| 401 | a->handle = session_handle (s); |
| 402 | a->app_index = ecm->app_index; |
| 403 | vnet_disconnect_session (a); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | static int |
| 408 | echo_clients_rx_callback (stream_session_t * s) |
| 409 | { |
| 410 | clib_warning ("BUG"); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* *INDENT-OFF* */ |
| 415 | static session_cb_vft_t echo_clients = { |
| 416 | .session_reset_callback = echo_clients_session_reset_callback, |
| 417 | .session_connected_callback = echo_clients_session_connected_callback, |
| 418 | .session_accept_callback = echo_clients_session_create_callback, |
| 419 | .session_disconnect_callback = echo_clients_session_disconnect_callback, |
| 420 | .builtin_server_rx_callback = echo_clients_rx_callback |
| 421 | }; |
| 422 | /* *INDENT-ON* */ |
| 423 | |
| 424 | static clib_error_t * |
| 425 | echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret) |
| 426 | { |
| 427 | u32 prealloc_fifos, segment_size = 2 << 20; |
| 428 | echo_client_main_t *ecm = &echo_client_main; |
| 429 | vnet_app_attach_args_t _a, *a = &_a; |
| 430 | u64 options[16]; |
| 431 | clib_error_t *error = 0; |
| 432 | |
| 433 | memset (a, 0, sizeof (*a)); |
| 434 | memset (options, 0, sizeof (options)); |
| 435 | |
| 436 | a->api_client_index = ecm->my_client_index; |
| 437 | a->session_cb_vft = &echo_clients; |
| 438 | |
| 439 | prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1; |
| 440 | |
| 441 | if (ecm->private_segment_size) |
| 442 | segment_size = ecm->private_segment_size; |
| 443 | |
| 444 | options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678; |
| 445 | options[APP_OPTIONS_SEGMENT_SIZE] = segment_size; |
| 446 | options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size; |
| 447 | options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size; |
| 448 | options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count; |
| 449 | options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos; |
| 450 | |
| 451 | options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; |
| 452 | if (appns_id) |
| 453 | { |
| 454 | options[APP_OPTIONS_FLAGS] |= appns_flags; |
| 455 | options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret; |
| 456 | } |
| 457 | a->options = options; |
| 458 | a->namespace_id = appns_id; |
| 459 | |
| 460 | if ((error = vnet_application_attach (a))) |
| 461 | return error; |
| 462 | |
| 463 | ecm->app_index = a->app_index; |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int |
| 468 | echo_clients_detach () |
| 469 | { |
| 470 | echo_client_main_t *ecm = &echo_client_main; |
| 471 | vnet_app_detach_args_t _da, *da = &_da; |
| 472 | int rv; |
| 473 | |
| 474 | da->app_index = ecm->app_index; |
| 475 | rv = vnet_application_detach (da); |
| 476 | ecm->test_client_attached = 0; |
| 477 | ecm->app_index = ~0; |
| 478 | return rv; |
| 479 | } |
| 480 | |
| 481 | static void * |
| 482 | echo_client_thread_fn (void *arg) |
| 483 | { |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | /** Start a transmit thread */ |
| 488 | int |
| 489 | echo_clients_start_tx_pthread (echo_client_main_t * ecm) |
| 490 | { |
| 491 | if (ecm->client_thread_handle == 0) |
| 492 | { |
| 493 | int rv = pthread_create (&ecm->client_thread_handle, |
| 494 | NULL /*attr */ , |
| 495 | echo_client_thread_fn, 0); |
| 496 | if (rv) |
| 497 | { |
| 498 | ecm->client_thread_handle = 0; |
| 499 | return -1; |
| 500 | } |
| 501 | } |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | clib_error_t * |
| 506 | echo_clients_connect (vlib_main_t * vm, u32 n_clients) |
| 507 | { |
| 508 | echo_client_main_t *ecm = &echo_client_main; |
| 509 | vnet_connect_args_t _a, *a = &_a; |
| 510 | clib_error_t *error = 0; |
| 511 | int i; |
| 512 | for (i = 0; i < n_clients; i++) |
| 513 | { |
| 514 | memset (a, 0, sizeof (*a)); |
| 515 | |
| 516 | a->uri = (char *) ecm->connect_uri; |
| 517 | a->api_context = i; |
| 518 | a->app_index = ecm->app_index; |
| 519 | a->mp = 0; |
| 520 | |
| 521 | if ((error = vnet_connect_uri (a))) |
| 522 | return error; |
| 523 | |
| 524 | /* Crude pacing for call setups */ |
| 525 | if ((i % 4) == 0) |
| 526 | vlib_process_suspend (vm, 10e-6); |
| 527 | ASSERT (i + 1 >= ecm->ready_connections); |
| 528 | while (i + 1 - ecm->ready_connections > 1000) |
| 529 | { |
| 530 | vlib_process_suspend (vm, 100e-6); |
| 531 | } |
| 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | #define ec_cli_output(_fmt, _args...) \ |
| 537 | if (!ecm->no_output) \ |
| 538 | vlib_cli_output(vm, _fmt, ##_args) |
| 539 | |
| 540 | static clib_error_t * |
| 541 | echo_clients_command_fn (vlib_main_t * vm, |
| 542 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 543 | { |
| 544 | echo_client_main_t *ecm = &echo_client_main; |
| 545 | vlib_thread_main_t *thread_main = vlib_get_thread_main (); |
| 546 | uword *event_data = 0, event_type; |
| 547 | u8 *default_uri = (u8 *) "tcp://6.0.1.1/1234", *appns_id = 0; |
| 548 | u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0; |
| 549 | f64 test_timeout = 20.0, syn_timeout = 20.0, delta; |
| 550 | f64 time_before_connects; |
| 551 | u32 n_clients = 1; |
| 552 | int preallocate_sessions = 0; |
| 553 | char *transfer_type; |
| 554 | clib_error_t *error = 0; |
| 555 | int i; |
| 556 | |
| 557 | ecm->bytes_to_send = 8192; |
| 558 | ecm->no_return = 0; |
| 559 | ecm->fifo_size = 64 << 10; |
| 560 | ecm->connections_per_batch = 1000; |
| 561 | ecm->private_segment_count = 0; |
| 562 | ecm->private_segment_size = 0; |
| 563 | ecm->no_output = 0; |
| 564 | ecm->test_bytes = 0; |
| 565 | ecm->test_failed = 0; |
| 566 | ecm->vlib_main = vm; |
| 567 | if (thread_main->n_vlib_mains > 1) |
| 568 | clib_spinlock_init (&ecm->sessions_lock); |
| 569 | vec_free (ecm->connect_uri); |
| 570 | |
| 571 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 572 | { |
| 573 | if (unformat (input, "uri %s", &ecm->connect_uri)) |
| 574 | ; |
| 575 | else if (unformat (input, "nclients %d", &n_clients)) |
| 576 | ; |
| 577 | else if (unformat (input, "mbytes %lld", &tmp)) |
| 578 | ecm->bytes_to_send = tmp << 20; |
| 579 | else if (unformat (input, "gbytes %lld", &tmp)) |
| 580 | ecm->bytes_to_send = tmp << 30; |
| 581 | else if (unformat (input, "bytes %lld", &ecm->bytes_to_send)) |
| 582 | ; |
| 583 | else if (unformat (input, "test-timeout %f", &test_timeout)) |
| 584 | ; |
| 585 | else if (unformat (input, "syn-timeout %f", &syn_timeout)) |
| 586 | ; |
| 587 | else if (unformat (input, "no-return")) |
| 588 | ecm->no_return = 1; |
| 589 | else if (unformat (input, "fifo-size %d", &ecm->fifo_size)) |
| 590 | ecm->fifo_size <<= 10; |
| 591 | else if (unformat (input, "private-segment-count %d", |
| 592 | &ecm->private_segment_count)) |
| 593 | ; |
| 594 | else if (unformat (input, "private-segment-size %U", |
| 595 | unformat_memory_size, &tmp)) |
| 596 | { |
| 597 | if (tmp >= 0x100000000ULL) |
| 598 | return clib_error_return |
| 599 | (0, "private segment size %lld (%llu) too large", tmp, tmp); |
| 600 | ecm->private_segment_size = tmp; |
| 601 | } |
| 602 | else if (unformat (input, "preallocate-fifos")) |
| 603 | ecm->prealloc_fifos = 1; |
| 604 | else if (unformat (input, "preallocate-sessions")) |
| 605 | preallocate_sessions = 1; |
| 606 | else |
| 607 | if (unformat (input, "client-batch %d", &ecm->connections_per_batch)) |
| 608 | ; |
| 609 | else if (unformat (input, "appns %_%v%_", &appns_id)) |
| 610 | ; |
| 611 | else if (unformat (input, "all-scope")) |
| 612 | appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE |
| 613 | | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE); |
| 614 | else if (unformat (input, "local-scope")) |
| 615 | appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 616 | else if (unformat (input, "global-scope")) |
| 617 | appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 618 | else if (unformat (input, "secret %lu", &appns_secret)) |
| 619 | ; |
| 620 | else if (unformat (input, "no-output")) |
| 621 | ecm->no_output = 1; |
| 622 | else if (unformat (input, "test-bytes")) |
| 623 | ecm->test_bytes = 1; |
| 624 | else |
| 625 | return clib_error_return (0, "unknown input `%U'", |
| 626 | format_unformat_error, input); |
| 627 | } |
| 628 | |
| 629 | /* Store cli process node index for signalling */ |
| 630 | ecm->cli_node_index = |
| 631 | vlib_get_current_process (vm)->node_runtime.node_index; |
| 632 | |
| 633 | if (ecm->is_init == 0) |
| 634 | { |
| 635 | if (echo_clients_init (vm)) |
| 636 | return clib_error_return (0, "failed init"); |
| 637 | } |
| 638 | |
| 639 | |
| 640 | ecm->ready_connections = 0; |
| 641 | ecm->expected_connections = n_clients; |
| 642 | ecm->rx_total = 0; |
| 643 | ecm->tx_total = 0; |
| 644 | |
| 645 | if (!ecm->connect_uri) |
| 646 | { |
| 647 | clib_warning ("No uri provided. Using default: %v", default_uri); |
| 648 | ecm->connect_uri = default_uri; |
| 649 | } |
| 650 | |
| 651 | #if ECHO_CLIENT_PTHREAD |
| 652 | echo_clients_start_tx_pthread (); |
| 653 | #endif |
| 654 | |
| 655 | vlib_worker_thread_barrier_sync (vm); |
| 656 | vnet_session_enable_disable (vm, 1 /* turn on session and transports */ ); |
| 657 | vlib_worker_thread_barrier_release (vm); |
| 658 | |
| 659 | if (ecm->test_client_attached == 0) |
| 660 | { |
| 661 | if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret))) |
| 662 | { |
| 663 | vec_free (appns_id); |
| 664 | clib_error_report (error); |
| 665 | return error; |
| 666 | } |
| 667 | vec_free (appns_id); |
| 668 | } |
| 669 | ecm->test_client_attached = 1; |
| 670 | |
| 671 | /* Turn on the builtin client input nodes */ |
| 672 | for (i = 0; i < thread_main->n_vlib_mains; i++) |
| 673 | vlib_node_set_state (vlib_mains[i], echo_clients_node.index, |
| 674 | VLIB_NODE_STATE_POLLING); |
| 675 | |
| 676 | if (preallocate_sessions) |
| 677 | { |
| 678 | session_t *sp __attribute__ ((unused)); |
| 679 | for (i = 0; i < n_clients; i++) |
| 680 | pool_get (ecm->sessions, sp); |
| 681 | for (i = 0; i < n_clients; i++) |
| 682 | pool_put_index (ecm->sessions, i); |
| 683 | } |
| 684 | |
| 685 | /* Fire off connect requests */ |
| 686 | time_before_connects = vlib_time_now (vm); |
| 687 | if ((error = echo_clients_connect (vm, n_clients))) |
| 688 | return error; |
| 689 | |
| 690 | /* Park until the sessions come up, or ten seconds elapse... */ |
| 691 | vlib_process_wait_for_event_or_clock (vm, syn_timeout); |
| 692 | event_type = vlib_process_get_events (vm, &event_data); |
| 693 | switch (event_type) |
| 694 | { |
| 695 | case ~0: |
| 696 | ec_cli_output ("Timeout with only %d sessions active...", |
| 697 | ecm->ready_connections); |
| 698 | error = clib_error_return (0, "failed: syn timeout with %d sessions", |
| 699 | ecm->ready_connections); |
| 700 | goto cleanup; |
| 701 | |
| 702 | case 1: |
| 703 | delta = vlib_time_now (vm) - time_before_connects; |
| 704 | if (delta != 0.0) |
| 705 | ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s", |
| 706 | n_clients, delta, ((f64) n_clients) / delta); |
| 707 | |
| 708 | ecm->test_start_time = vlib_time_now (ecm->vlib_main); |
| 709 | ec_cli_output ("Test started at %.6f", ecm->test_start_time); |
| 710 | break; |
| 711 | |
| 712 | default: |
| 713 | ec_cli_output ("unexpected event(1): %d", event_type); |
| 714 | error = clib_error_return (0, "failed: unexpected event(1): %d", |
| 715 | event_type); |
| 716 | goto cleanup; |
| 717 | } |
| 718 | |
| 719 | /* Now wait for the sessions to finish... */ |
| 720 | vlib_process_wait_for_event_or_clock (vm, test_timeout); |
| 721 | event_type = vlib_process_get_events (vm, &event_data); |
| 722 | switch (event_type) |
| 723 | { |
| 724 | case ~0: |
| 725 | ec_cli_output ("Timeout with %d sessions still active...", |
| 726 | ecm->ready_connections); |
| 727 | error = clib_error_return (0, "failed: timeout with %d sessions", |
| 728 | ecm->ready_connections); |
| 729 | goto cleanup; |
| 730 | |
| 731 | case 2: |
| 732 | ecm->test_end_time = vlib_time_now (vm); |
| 733 | ec_cli_output ("Test finished at %.6f", ecm->test_end_time); |
| 734 | break; |
| 735 | |
| 736 | default: |
| 737 | ec_cli_output ("unexpected event(2): %d", event_type); |
| 738 | error = clib_error_return (0, "failed: unexpected event(2): %d", |
| 739 | event_type); |
| 740 | goto cleanup; |
| 741 | } |
| 742 | |
| 743 | delta = ecm->test_end_time - ecm->test_start_time; |
| 744 | if (delta != 0.0) |
| 745 | { |
| 746 | total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total); |
| 747 | transfer_type = ecm->no_return ? "half-duplex" : "full-duplex"; |
| 748 | ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds", |
| 749 | total_bytes, total_bytes / (1ULL << 20), |
| 750 | total_bytes / (1ULL << 30), delta); |
| 751 | ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta), |
| 752 | transfer_type); |
| 753 | ec_cli_output ("%.4f gbit/second %s", |
| 754 | (((f64) total_bytes * 8.0) / delta / 1e9), |
| 755 | transfer_type); |
| 756 | } |
| 757 | else |
| 758 | { |
| 759 | ec_cli_output ("zero delta-t?"); |
| 760 | error = clib_error_return (0, "failed: zero delta-t"); |
| 761 | goto cleanup; |
| 762 | } |
| 763 | |
| 764 | if (ecm->test_bytes && ecm->test_failed) |
| 765 | error = clib_error_return (0, "failed: test bytes"); |
| 766 | |
| 767 | cleanup: |
| 768 | ecm->run_test = 0; |
| 769 | for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++) |
| 770 | { |
| 771 | vec_reset_length (ecm->connection_index_by_thread[i]); |
| 772 | vec_reset_length (ecm->connections_this_batch_by_thread[i]); |
| 773 | } |
| 774 | |
| 775 | pool_free (ecm->sessions); |
| 776 | |
| 777 | /* Detach the application, so we can use different fifo sizes next time */ |
| 778 | if (ecm->test_client_attached) |
| 779 | { |
| 780 | if (echo_clients_detach ()) |
| 781 | { |
| 782 | error = clib_error_return (0, "failed: app detach"); |
| 783 | ec_cli_output ("WARNING: app detach failed..."); |
| 784 | } |
| 785 | } |
| 786 | if (error) |
| 787 | ec_cli_output ("test failed"); |
| 788 | return error; |
| 789 | } |
| 790 | |
| 791 | /* *INDENT-OFF* */ |
| 792 | VLIB_CLI_COMMAND (echo_clients_command, static) = |
| 793 | { |
| 794 | .path = "test echo clients", |
| 795 | .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]" |
| 796 | "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]" |
| 797 | "[private-segment-count <count>][private-segment-size <bytes>[m|g]]" |
| 798 | "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]" |
| 799 | "[uri <tcp://ip/port>][test-bytes][no-output]", |
| 800 | .function = echo_clients_command_fn, |
| 801 | .is_mp_safe = 1, |
| 802 | }; |
| 803 | /* *INDENT-ON* */ |
| 804 | |
| 805 | clib_error_t * |
| 806 | echo_clients_main_init (vlib_main_t * vm) |
| 807 | { |
| 808 | echo_client_main_t *ecm = &echo_client_main; |
| 809 | ecm->is_init = 0; |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | VLIB_INIT_FUNCTION (echo_clients_main_init); |
| 814 | |
| 815 | /* |
| 816 | * fd.io coding-style-patch-verification: ON |
| 817 | * |
| 818 | * Local Variables: |
| 819 | * eval: (c-set-style "gnu") |
| 820 | * End: |
| 821 | */ |