2 * echo_client.c - vpp built-in echo client code
4 * Copyright (c) 2017-2019 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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
21 #include <hs_apps/echo_client.h>
23 echo_client_main_t echo_client_main;
25 #define ECHO_CLIENT_DBG (0)
26 #define DBG(_fmt, _args...) \
27 if (ECHO_CLIENT_DBG) \
28 clib_warning (_fmt, ##_args)
31 signal_evt_to_cli_i (int *code)
33 echo_client_main_t *ecm = &echo_client_main;
34 ASSERT (vlib_get_thread_index () == 0);
35 vlib_process_signal_event (ecm->vlib_main, ecm->cli_node_index, *code, 0);
39 signal_evt_to_cli (int code)
41 if (vlib_get_thread_index () != 0)
42 vl_api_rpc_call_main_thread (signal_evt_to_cli_i, (u8 *) & code,
45 signal_evt_to_cli_i (&code);
49 send_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
51 u8 *test_data = ecm->connect_test_data;
52 int test_buf_len, test_buf_offset, rv;
55 test_buf_len = vec_len (test_data);
56 ASSERT (test_buf_len > 0);
57 test_buf_offset = s->bytes_sent % test_buf_len;
58 bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
65 svm_fifo_t *f = s->data.tx_fifo;
66 rv = clib_min (svm_fifo_max_enqueue_prod (f), bytes_this_chunk);
67 svm_fifo_enqueue_nocopy (f, rv);
68 session_send_io_evt_to_thread_custom (&f->master_session_index,
73 rv = app_send_stream (&s->data, test_data + test_buf_offset,
78 svm_fifo_t *f = s->data.tx_fifo;
79 u32 max_enqueue = svm_fifo_max_enqueue_prod (f);
81 if (max_enqueue < sizeof (session_dgram_hdr_t))
84 max_enqueue -= sizeof (session_dgram_hdr_t);
88 session_dgram_hdr_t hdr;
89 app_session_transport_t *at = &s->data.transport;
91 rv = clib_min (max_enqueue, bytes_this_chunk);
95 clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip,
96 sizeof (ip46_address_t));
97 hdr.is_ip4 = at->is_ip4;
98 hdr.rmt_port = at->rmt_port;
99 clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip,
100 sizeof (ip46_address_t));
101 hdr.lcl_port = at->lcl_port;
102 svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
103 svm_fifo_enqueue_nocopy (f, rv);
104 session_send_io_evt_to_thread_custom (&f->master_session_index,
110 bytes_this_chunk = clib_min (bytes_this_chunk, max_enqueue);
111 rv = app_send_dgram (&s->data, test_data + test_buf_offset,
112 bytes_this_chunk, 0);
116 /* If we managed to enqueue data... */
119 /* Account for it... */
120 s->bytes_to_send -= rv;
126 ELOG_TYPE_DECLARE (e) =
128 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
129 .format_args = "i4i4i4",
136 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
138 ed->data[1] = s->bytes_sent;
139 ed->data[2] = s->bytes_to_send;
145 receive_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
147 svm_fifo_t *rx_fifo = s->data.rx_fifo;
148 u32 thread_index = vlib_get_thread_index ();
154 n_read = app_recv_stream (&s->data, ecm->rx_buf[thread_index],
155 vec_len (ecm->rx_buf[thread_index]));
157 n_read = app_recv_dgram (&s->data, ecm->rx_buf[thread_index],
158 vec_len (ecm->rx_buf[thread_index]));
162 n_read = svm_fifo_max_dequeue_cons (rx_fifo);
163 svm_fifo_dequeue_drop (rx_fifo, n_read);
171 ELOG_TYPE_DECLARE (e) =
173 .format = "rx-deq: %d bytes",
181 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
182 ed->data[0] = n_read;
187 for (i = 0; i < n_read; i++)
189 if (ecm->rx_buf[thread_index][i]
190 != ((s->bytes_received + i) & 0xff))
192 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
193 n_read, s->bytes_received + i,
194 ecm->rx_buf[thread_index][i],
195 ((s->bytes_received + i) & 0xff));
196 ecm->test_failed = 1;
200 ASSERT (n_read <= s->bytes_to_receive);
201 s->bytes_to_receive -= n_read;
202 s->bytes_received += n_read;
207 echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
208 vlib_frame_t * frame)
210 echo_client_main_t *ecm = &echo_client_main;
211 int my_thread_index = vlib_get_thread_index ();
212 eclient_session_t *sp;
215 u32 *connection_indices;
216 u32 *connections_this_batch;
217 u32 nconnections_this_batch;
219 connection_indices = ecm->connection_index_by_thread[my_thread_index];
220 connections_this_batch =
221 ecm->connections_this_batch_by_thread[my_thread_index];
223 if ((ecm->run_test != ECHO_CLIENTS_RUNNING) ||
224 ((vec_len (connection_indices) == 0)
225 && vec_len (connections_this_batch) == 0))
228 /* Grab another pile of connections */
229 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
231 nconnections_this_batch =
232 clib_min (ecm->connections_per_batch, vec_len (connection_indices));
234 ASSERT (nconnections_this_batch > 0);
235 vec_validate (connections_this_batch, nconnections_this_batch - 1);
236 clib_memcpy_fast (connections_this_batch,
237 connection_indices + vec_len (connection_indices)
238 - nconnections_this_batch,
239 nconnections_this_batch * sizeof (u32));
240 _vec_len (connection_indices) -= nconnections_this_batch;
243 if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch
244 && ecm->prev_conns == vec_len (connections_this_batch)))
247 ecm->prev_conns = vec_len (connections_this_batch);
248 if (ecm->repeats == 500000)
250 clib_warning ("stuck clients");
255 ecm->prev_conns = vec_len (connections_this_batch);
259 for (i = 0; i < vec_len (connections_this_batch); i++)
263 sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
265 if (sp->bytes_to_send > 0)
267 send_data_chunk (ecm, sp);
270 if (sp->bytes_to_receive > 0)
274 if (PREDICT_FALSE (delete_session == 1))
278 clib_atomic_fetch_add (&ecm->tx_total, sp->bytes_sent);
279 clib_atomic_fetch_add (&ecm->rx_total, sp->bytes_received);
280 s = session_get_from_handle_if_valid (sp->vpp_session_handle);
284 vnet_disconnect_args_t _a, *a = &_a;
285 a->handle = session_handle (s);
286 a->app_index = ecm->app_index;
287 vnet_disconnect_session (a);
289 vec_delete (connections_this_batch, 1, i);
291 clib_atomic_fetch_add (&ecm->ready_connections, -1);
295 clib_warning ("session AWOL?");
296 vec_delete (connections_this_batch, 1, i);
299 /* Kick the debug CLI process */
300 if (ecm->ready_connections == 0)
302 signal_evt_to_cli (2);
307 ecm->connection_index_by_thread[my_thread_index] = connection_indices;
308 ecm->connections_this_batch_by_thread[my_thread_index] =
309 connections_this_batch;
314 VLIB_REGISTER_NODE (echo_clients_node) =
316 .function = echo_client_node_fn,
317 .name = "echo-clients",
318 .type = VLIB_NODE_TYPE_INPUT,
319 .state = VLIB_NODE_STATE_DISABLED,
324 create_api_loopback (echo_client_main_t * ecm)
326 api_main_t *am = vlibapi_get_main ();
327 vl_shmem_hdr_t *shmem_hdr;
329 shmem_hdr = am->shmem_hdr;
330 ecm->vl_input_queue = shmem_hdr->vl_input_queue;
331 ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client",
332 ecm->vl_input_queue);
337 echo_clients_init (vlib_main_t * vm)
339 echo_client_main_t *ecm = &echo_client_main;
340 vlib_thread_main_t *vtm = vlib_get_thread_main ();
344 if (create_api_loopback (ecm))
347 num_threads = 1 /* main thread */ + vtm->n_threads;
349 /* Init test data. Big buffer */
350 vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
351 for (i = 0; i < vec_len (ecm->connect_test_data); i++)
352 ecm->connect_test_data[i] = i & 0xff;
354 vec_validate (ecm->rx_buf, num_threads - 1);
355 for (i = 0; i < num_threads; i++)
356 vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
360 vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains);
361 vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains);
362 vec_validate (ecm->quic_session_index_by_thread, vtm->n_vlib_mains);
363 vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains);
369 quic_echo_clients_qsession_connected_callback (u32 app_index, u32 api_context,
373 echo_client_main_t *ecm = &echo_client_main;
374 vnet_connect_args_t *a = 0;
376 u8 thread_index = vlib_get_thread_index ();
377 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
379 session_handle_t handle;
381 DBG ("QUIC Connection handle %d", session_handle (s));
384 a->uri = (char *) ecm->connect_uri;
385 if (parse_uri (a->uri, &sep))
387 sep.parent_handle = handle = session_handle (s);
389 for (stream_n = 0; stream_n < ecm->quic_streams; stream_n++)
391 clib_memset (a, 0, sizeof (*a));
392 a->app_index = ecm->app_index;
393 a->api_context = -1 - api_context;
394 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
396 DBG ("QUIC opening stream %d", stream_n);
397 if ((rv = vnet_connect (a)))
399 clib_error ("Stream session %d opening failed: %d", stream_n, rv);
402 DBG ("QUIC stream %d connected", stream_n);
405 * 's' is no longer valid, its underlying pool could have been moved in
408 vec_add1 (ecm->quic_session_index_by_thread[thread_index], handle);
414 quic_echo_clients_session_connected_callback (u32 app_index, u32 api_context,
418 echo_client_main_t *ecm = &echo_client_main;
419 eclient_session_t *session;
423 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_STARTING))
428 clib_warning ("connection %d failed!", api_context);
429 ecm->run_test = ECHO_CLIENTS_EXITING;
430 signal_evt_to_cli (-1);
434 if (s->listener_handle == SESSION_INVALID_HANDLE)
435 return quic_echo_clients_qsession_connected_callback (app_index,
438 DBG ("STREAM Connection callback %d", api_context);
440 thread_index = s->thread_index;
441 ASSERT (thread_index == vlib_get_thread_index ()
442 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
444 if (!ecm->vpp_event_queue[thread_index])
445 ecm->vpp_event_queue[thread_index] =
446 session_main_get_vpp_event_queue (thread_index);
451 clib_spinlock_lock_if_init (&ecm->sessions_lock);
452 pool_get (ecm->sessions, session);
453 clib_spinlock_unlock_if_init (&ecm->sessions_lock);
455 clib_memset (session, 0, sizeof (*session));
456 session_index = session - ecm->sessions;
457 session->bytes_to_send = ecm->bytes_to_send;
458 session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
459 session->data.rx_fifo = s->rx_fifo;
460 session->data.rx_fifo->client_session_index = session_index;
461 session->data.tx_fifo = s->tx_fifo;
462 session->data.tx_fifo->client_session_index = session_index;
463 session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
464 session->vpp_session_handle = session_handle (s);
468 transport_connection_t *tc;
469 tc = session_get_transport (s);
470 clib_memcpy_fast (&session->data.transport, tc,
471 sizeof (session->data.transport));
472 session->data.is_dgram = 1;
475 vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
476 clib_atomic_fetch_add (&ecm->ready_connections, 1);
477 if (ecm->ready_connections == ecm->expected_connections)
479 ecm->run_test = ECHO_CLIENTS_RUNNING;
480 /* Signal the CLI process that the action is starting... */
481 signal_evt_to_cli (1);
488 echo_clients_session_connected_callback (u32 app_index, u32 api_context,
489 session_t * s, session_error_t err)
491 echo_client_main_t *ecm = &echo_client_main;
492 eclient_session_t *session;
496 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_STARTING))
501 clib_warning ("connection %d failed!", api_context);
502 ecm->run_test = ECHO_CLIENTS_EXITING;
503 signal_evt_to_cli (-1);
507 thread_index = s->thread_index;
508 ASSERT (thread_index == vlib_get_thread_index ()
509 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
511 if (!ecm->vpp_event_queue[thread_index])
512 ecm->vpp_event_queue[thread_index] =
513 session_main_get_vpp_event_queue (thread_index);
518 clib_spinlock_lock_if_init (&ecm->sessions_lock);
519 pool_get (ecm->sessions, session);
520 clib_spinlock_unlock_if_init (&ecm->sessions_lock);
522 clib_memset (session, 0, sizeof (*session));
523 session_index = session - ecm->sessions;
524 session->bytes_to_send = ecm->bytes_to_send;
525 session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
526 session->data.rx_fifo = s->rx_fifo;
527 session->data.rx_fifo->client_session_index = session_index;
528 session->data.tx_fifo = s->tx_fifo;
529 session->data.tx_fifo->client_session_index = session_index;
530 session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
531 session->vpp_session_handle = session_handle (s);
535 transport_connection_t *tc;
536 tc = session_get_transport (s);
537 clib_memcpy_fast (&session->data.transport, tc,
538 sizeof (session->data.transport));
539 session->data.is_dgram = 1;
542 vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
543 clib_atomic_fetch_add (&ecm->ready_connections, 1);
544 if (ecm->ready_connections == ecm->expected_connections)
546 ecm->run_test = ECHO_CLIENTS_RUNNING;
547 /* Signal the CLI process that the action is starting... */
548 signal_evt_to_cli (1);
555 echo_clients_session_reset_callback (session_t * s)
557 echo_client_main_t *ecm = &echo_client_main;
558 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
560 if (s->session_state == SESSION_STATE_READY)
561 clib_warning ("Reset active connection %U", format_session, s, 2);
563 a->handle = session_handle (s);
564 a->app_index = ecm->app_index;
565 vnet_disconnect_session (a);
570 echo_clients_session_create_callback (session_t * s)
576 echo_clients_session_disconnect_callback (session_t * s)
578 echo_client_main_t *ecm = &echo_client_main;
579 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
580 a->handle = session_handle (s);
581 a->app_index = ecm->app_index;
582 vnet_disconnect_session (a);
587 echo_clients_session_disconnect (session_t * s)
589 echo_client_main_t *ecm = &echo_client_main;
590 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
591 a->handle = session_handle (s);
592 a->app_index = ecm->app_index;
593 vnet_disconnect_session (a);
597 echo_clients_rx_callback (session_t * s)
599 echo_client_main_t *ecm = &echo_client_main;
600 eclient_session_t *sp;
602 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_RUNNING))
604 echo_clients_session_disconnect (s);
608 sp = pool_elt_at_index (ecm->sessions, s->rx_fifo->client_session_index);
609 receive_data_chunk (ecm, sp);
611 if (svm_fifo_max_dequeue_cons (s->rx_fifo))
613 if (svm_fifo_set_event (s->rx_fifo))
614 session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
620 echo_client_add_segment_callback (u32 client_index, u64 segment_handle)
622 /* New heaps may be added */
627 static session_cb_vft_t echo_clients = {
628 .session_reset_callback = echo_clients_session_reset_callback,
629 .session_connected_callback = echo_clients_session_connected_callback,
630 .session_accept_callback = echo_clients_session_create_callback,
631 .session_disconnect_callback = echo_clients_session_disconnect_callback,
632 .builtin_app_rx_callback = echo_clients_rx_callback,
633 .add_segment_callback = echo_client_add_segment_callback
637 static clib_error_t *
638 echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
640 vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
641 vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
642 u32 prealloc_fifos, segment_size = 256 << 20;
643 echo_client_main_t *ecm = &echo_client_main;
644 vnet_app_attach_args_t _a, *a = &_a;
648 clib_memset (a, 0, sizeof (*a));
649 clib_memset (options, 0, sizeof (options));
651 a->api_client_index = ecm->my_client_index;
652 if (ecm->transport_proto == TRANSPORT_PROTO_QUIC)
653 echo_clients.session_connected_callback =
654 quic_echo_clients_session_connected_callback;
655 a->session_cb_vft = &echo_clients;
657 prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
659 if (ecm->private_segment_size)
660 segment_size = ecm->private_segment_size;
662 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
663 options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
664 options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
665 options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
666 options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
667 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
668 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
669 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
670 options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
671 options[APP_OPTIONS_PCT_FIRST_ALLOC] = 100;
674 options[APP_OPTIONS_FLAGS] |= appns_flags;
675 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
677 a->options = options;
678 a->namespace_id = appns_id;
680 if ((rv = vnet_application_attach (a)))
681 return clib_error_return (0, "attach returned %d", rv);
683 ecm->app_index = a->app_index;
685 clib_memset (a_cert, 0, sizeof (*a_cert));
686 a_cert->app_index = a->app_index;
687 vec_validate (a_cert->cert, test_srv_crt_rsa_len);
688 clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
689 vnet_app_add_tls_cert (a_cert);
691 clib_memset (a_key, 0, sizeof (*a_key));
692 a_key->app_index = a->app_index;
693 vec_validate (a_key->key, test_srv_key_rsa_len);
694 clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
695 vnet_app_add_tls_key (a_key);
700 echo_clients_detach ()
702 echo_client_main_t *ecm = &echo_client_main;
703 vnet_app_detach_args_t _da, *da = &_da;
706 da->app_index = ecm->app_index;
707 da->api_client_index = ~0;
708 rv = vnet_application_detach (da);
709 ecm->test_client_attached = 0;
715 echo_client_thread_fn (void *arg)
720 /** Start a transmit thread */
722 echo_clients_start_tx_pthread (echo_client_main_t * ecm)
724 if (ecm->client_thread_handle == 0)
726 int rv = pthread_create (&ecm->client_thread_handle,
728 echo_client_thread_fn, 0);
731 ecm->client_thread_handle = 0;
739 echo_clients_connect (vlib_main_t * vm, u32 n_clients)
741 echo_client_main_t *ecm = &echo_client_main;
742 vnet_connect_args_t _a, *a = &_a;
745 clib_memset (a, 0, sizeof (*a));
747 for (i = 0; i < n_clients; i++)
749 a->uri = (char *) ecm->connect_uri;
751 a->app_index = ecm->app_index;
753 vlib_worker_thread_barrier_sync (vm);
754 if ((rv = vnet_connect_uri (a)))
756 vlib_worker_thread_barrier_release (vm);
757 return clib_error_return (0, "connect returned: %d", rv);
759 vlib_worker_thread_barrier_release (vm);
761 /* Crude pacing for call setups */
763 vlib_process_suspend (vm, 100e-6);
764 ASSERT (i + 1 >= ecm->ready_connections);
765 while (i + 1 - ecm->ready_connections > 128)
766 vlib_process_suspend (vm, 1e-3);
771 #define ec_cli_output(_fmt, _args...) \
772 if (!ecm->no_output) \
773 vlib_cli_output(vm, _fmt, ##_args)
775 static clib_error_t *
776 echo_clients_command_fn (vlib_main_t * vm,
777 unformat_input_t * input, vlib_cli_command_t * cmd)
779 echo_client_main_t *ecm = &echo_client_main;
780 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
781 u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
782 f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
783 char *default_uri = "tcp://6.0.1.1/1234";
784 uword *event_data = 0, event_type;
785 f64 time_before_connects;
787 int preallocate_sessions = 0;
789 clib_error_t *error = 0;
792 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
795 ecm->quic_streams = 1;
796 ecm->bytes_to_send = 8192;
798 ecm->fifo_size = 64 << 10;
799 ecm->connections_per_batch = 1000;
800 ecm->private_segment_count = 0;
801 ecm->private_segment_size = 0;
804 ecm->test_failed = 0;
806 ecm->tls_engine = CRYPTO_ENGINE_OPENSSL;
808 ecm->run_test = ECHO_CLIENTS_STARTING;
810 if (thread_main->n_vlib_mains > 1)
811 clib_spinlock_init (&ecm->sessions_lock);
812 vec_free (ecm->connect_uri);
814 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
816 if (unformat (input, "uri %s", &ecm->connect_uri))
818 else if (unformat (input, "nclients %d", &n_clients))
820 else if (unformat (input, "quic-streams %d", &ecm->quic_streams))
822 else if (unformat (input, "mbytes %lld", &tmp))
823 ecm->bytes_to_send = tmp << 20;
824 else if (unformat (input, "gbytes %lld", &tmp))
825 ecm->bytes_to_send = tmp << 30;
826 else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
828 else if (unformat (input, "test-timeout %f", &test_timeout))
830 else if (unformat (input, "syn-timeout %f", &syn_timeout))
832 else if (unformat (input, "no-return"))
834 else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
835 ecm->fifo_size <<= 10;
836 else if (unformat (input, "private-segment-count %d",
837 &ecm->private_segment_count))
839 else if (unformat (input, "private-segment-size %U",
840 unformat_memory_size, &tmp))
842 if (tmp >= 0x100000000ULL)
843 return clib_error_return
844 (0, "private segment size %lld (%llu) too large", tmp, tmp);
845 ecm->private_segment_size = tmp;
847 else if (unformat (input, "preallocate-fifos"))
848 ecm->prealloc_fifos = 1;
849 else if (unformat (input, "preallocate-sessions"))
850 preallocate_sessions = 1;
852 if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
854 else if (unformat (input, "appns %_%v%_", &appns_id))
856 else if (unformat (input, "all-scope"))
857 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
858 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
859 else if (unformat (input, "local-scope"))
860 appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
861 else if (unformat (input, "global-scope"))
862 appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
863 else if (unformat (input, "secret %lu", &appns_secret))
865 else if (unformat (input, "no-output"))
867 else if (unformat (input, "test-bytes"))
869 else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
872 return clib_error_return (0, "failed: unknown input `%U'",
873 format_unformat_error, input);
876 /* Store cli process node index for signalling */
877 ecm->cli_node_index =
878 vlib_get_current_process (vm)->node_runtime.node_index;
880 if (ecm->is_init == 0)
882 if (echo_clients_init (vm))
883 return clib_error_return (0, "failed init");
887 ecm->ready_connections = 0;
888 ecm->expected_connections = n_clients * ecm->quic_streams;
892 if (!ecm->connect_uri)
894 clib_warning ("No uri provided. Using default: %s", default_uri);
895 ecm->connect_uri = format (0, "%s%c", default_uri, 0);
898 if ((rv = parse_uri ((char *) ecm->connect_uri, &sep)))
899 return clib_error_return (0, "Uri parse error: %d", rv);
900 ecm->transport_proto = sep.transport_proto;
901 ecm->is_dgram = (sep.transport_proto == TRANSPORT_PROTO_UDP);
903 #if ECHO_CLIENT_PTHREAD
904 echo_clients_start_tx_pthread ();
907 vlib_worker_thread_barrier_sync (vm);
908 vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
909 vlib_worker_thread_barrier_release (vm);
911 if (ecm->test_client_attached == 0)
913 if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
916 clib_error_report (error);
921 ecm->test_client_attached = 1;
923 /* Turn on the builtin client input nodes */
924 for (i = 0; i < thread_main->n_vlib_mains; i++)
925 vlib_node_set_state (vlib_mains[i], echo_clients_node.index,
926 VLIB_NODE_STATE_POLLING);
928 if (preallocate_sessions)
929 pool_init_fixed (ecm->sessions, 1.1 * n_clients);
931 /* Fire off connect requests */
932 time_before_connects = vlib_time_now (vm);
933 if ((error = echo_clients_connect (vm, n_clients)))
938 /* Park until the sessions come up, or ten seconds elapse... */
939 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
940 event_type = vlib_process_get_events (vm, &event_data);
944 ec_cli_output ("Timeout with only %d sessions active...",
945 ecm->ready_connections);
946 error = clib_error_return (0, "failed: syn timeout with %d sessions",
947 ecm->ready_connections);
951 delta = vlib_time_now (vm) - time_before_connects;
953 ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
954 n_clients, delta, ((f64) n_clients) / delta);
956 ecm->test_start_time = vlib_time_now (ecm->vlib_main);
957 ec_cli_output ("Test started at %.6f", ecm->test_start_time);
961 ec_cli_output ("unexpected event(1): %d", event_type);
962 error = clib_error_return (0, "failed: unexpected event(1): %d",
967 /* Now wait for the sessions to finish... */
968 vlib_process_wait_for_event_or_clock (vm, test_timeout);
969 event_type = vlib_process_get_events (vm, &event_data);
973 ec_cli_output ("Timeout with %d sessions still active...",
974 ecm->ready_connections);
975 error = clib_error_return (0, "failed: timeout with %d sessions",
976 ecm->ready_connections);
980 ecm->test_end_time = vlib_time_now (vm);
981 ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
985 ec_cli_output ("unexpected event(2): %d", event_type);
986 error = clib_error_return (0, "failed: unexpected event(2): %d",
991 delta = ecm->test_end_time - ecm->test_start_time;
994 total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
995 transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
996 ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
997 total_bytes, total_bytes / (1ULL << 20),
998 total_bytes / (1ULL << 30), delta);
999 ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
1001 ec_cli_output ("%.4f gbit/second %s",
1002 (((f64) total_bytes * 8.0) / delta / 1e9),
1007 ec_cli_output ("zero delta-t?");
1008 error = clib_error_return (0, "failed: zero delta-t");
1012 if (ecm->test_bytes && ecm->test_failed)
1013 error = clib_error_return (0, "failed: test bytes");
1016 ecm->run_test = ECHO_CLIENTS_EXITING;
1017 vlib_process_wait_for_event_or_clock (vm, 10e-3);
1018 for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
1020 vec_reset_length (ecm->connection_index_by_thread[i]);
1021 vec_reset_length (ecm->connections_this_batch_by_thread[i]);
1022 vec_reset_length (ecm->quic_session_index_by_thread[i]);
1025 pool_free (ecm->sessions);
1027 /* Detach the application, so we can use different fifo sizes next time */
1028 if (ecm->test_client_attached)
1030 if (echo_clients_detach ())
1032 error = clib_error_return (0, "failed: app detach");
1033 ec_cli_output ("WARNING: app detach failed...");
1037 ec_cli_output ("test failed");
1038 vec_free (ecm->connect_uri);
1043 VLIB_CLI_COMMAND (echo_clients_command, static) =
1045 .path = "test echo clients",
1046 .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
1047 "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
1048 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
1049 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
1050 "[uri <tcp://ip/port>][test-bytes][no-output]",
1051 .function = echo_clients_command_fn,
1057 echo_clients_main_init (vlib_main_t * vm)
1059 echo_client_main_t *ecm = &echo_client_main;
1064 VLIB_INIT_FUNCTION (echo_clients_main_init);
1067 * fd.io coding-style-patch-verification: ON
1070 * eval: (c-set-style "gnu")