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 (
69 &f->shr->master_session_index, s->thread_index, SESSION_IO_EVT_TX);
72 rv = app_send_stream (&s->data, test_data + test_buf_offset,
77 svm_fifo_t *f = s->data.tx_fifo;
78 u32 max_enqueue = svm_fifo_max_enqueue_prod (f);
80 if (max_enqueue < sizeof (session_dgram_hdr_t))
83 max_enqueue -= sizeof (session_dgram_hdr_t);
87 session_dgram_hdr_t hdr;
88 app_session_transport_t *at = &s->data.transport;
90 rv = clib_min (max_enqueue, bytes_this_chunk);
94 clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip,
95 sizeof (ip46_address_t));
96 hdr.is_ip4 = at->is_ip4;
97 hdr.rmt_port = at->rmt_port;
98 clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip,
99 sizeof (ip46_address_t));
100 hdr.lcl_port = at->lcl_port;
101 svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
102 svm_fifo_enqueue_nocopy (f, rv);
103 session_send_io_evt_to_thread_custom (
104 &f->shr->master_session_index, s->thread_index, SESSION_IO_EVT_TX);
108 bytes_this_chunk = clib_min (bytes_this_chunk, max_enqueue);
109 rv = app_send_dgram (&s->data, test_data + test_buf_offset,
110 bytes_this_chunk, 0);
114 /* If we managed to enqueue data... */
117 /* Account for it... */
118 s->bytes_to_send -= rv;
124 ELOG_TYPE_DECLARE (e) =
126 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
127 .format_args = "i4i4i4",
134 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
136 ed->data[1] = s->bytes_sent;
137 ed->data[2] = s->bytes_to_send;
143 receive_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
145 svm_fifo_t *rx_fifo = s->data.rx_fifo;
146 u32 thread_index = vlib_get_thread_index ();
152 n_read = app_recv_stream (&s->data, ecm->rx_buf[thread_index],
153 vec_len (ecm->rx_buf[thread_index]));
155 n_read = app_recv_dgram (&s->data, ecm->rx_buf[thread_index],
156 vec_len (ecm->rx_buf[thread_index]));
160 n_read = svm_fifo_max_dequeue_cons (rx_fifo);
161 svm_fifo_dequeue_drop (rx_fifo, n_read);
169 ELOG_TYPE_DECLARE (e) =
171 .format = "rx-deq: %d bytes",
179 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
180 ed->data[0] = n_read;
185 for (i = 0; i < n_read; i++)
187 if (ecm->rx_buf[thread_index][i]
188 != ((s->bytes_received + i) & 0xff))
190 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
191 n_read, s->bytes_received + i,
192 ecm->rx_buf[thread_index][i],
193 ((s->bytes_received + i) & 0xff));
194 ecm->test_failed = 1;
198 ASSERT (n_read <= s->bytes_to_receive);
199 s->bytes_to_receive -= n_read;
200 s->bytes_received += n_read;
205 echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
206 vlib_frame_t * frame)
208 echo_client_main_t *ecm = &echo_client_main;
209 int my_thread_index = vlib_get_thread_index ();
210 eclient_session_t *sp;
213 u32 *connection_indices;
214 u32 *connections_this_batch;
215 u32 nconnections_this_batch;
217 connection_indices = ecm->connection_index_by_thread[my_thread_index];
218 connections_this_batch =
219 ecm->connections_this_batch_by_thread[my_thread_index];
221 if ((ecm->run_test != ECHO_CLIENTS_RUNNING) ||
222 ((vec_len (connection_indices) == 0)
223 && vec_len (connections_this_batch) == 0))
226 /* Grab another pile of connections */
227 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
229 nconnections_this_batch =
230 clib_min (ecm->connections_per_batch, vec_len (connection_indices));
232 ASSERT (nconnections_this_batch > 0);
233 vec_validate (connections_this_batch, nconnections_this_batch - 1);
234 clib_memcpy_fast (connections_this_batch,
235 connection_indices + vec_len (connection_indices)
236 - nconnections_this_batch,
237 nconnections_this_batch * sizeof (u32));
238 _vec_len (connection_indices) -= nconnections_this_batch;
241 if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch
242 && ecm->prev_conns == vec_len (connections_this_batch)))
245 ecm->prev_conns = vec_len (connections_this_batch);
246 if (ecm->repeats == 500000)
248 clib_warning ("stuck clients");
253 ecm->prev_conns = vec_len (connections_this_batch);
257 for (i = 0; i < vec_len (connections_this_batch); i++)
261 sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
263 if (sp->bytes_to_send > 0)
265 send_data_chunk (ecm, sp);
268 if (sp->bytes_to_receive > 0)
272 if (PREDICT_FALSE (delete_session == 1))
276 clib_atomic_fetch_add (&ecm->tx_total, sp->bytes_sent);
277 clib_atomic_fetch_add (&ecm->rx_total, sp->bytes_received);
278 s = session_get_from_handle_if_valid (sp->vpp_session_handle);
282 vnet_disconnect_args_t _a, *a = &_a;
283 a->handle = session_handle (s);
284 a->app_index = ecm->app_index;
285 vnet_disconnect_session (a);
287 vec_delete (connections_this_batch, 1, i);
289 clib_atomic_fetch_add (&ecm->ready_connections, -1);
293 clib_warning ("session AWOL?");
294 vec_delete (connections_this_batch, 1, i);
297 /* Kick the debug CLI process */
298 if (ecm->ready_connections == 0)
300 signal_evt_to_cli (2);
305 ecm->connection_index_by_thread[my_thread_index] = connection_indices;
306 ecm->connections_this_batch_by_thread[my_thread_index] =
307 connections_this_batch;
311 VLIB_REGISTER_NODE (echo_clients_node) =
313 .function = echo_client_node_fn,
314 .name = "echo-clients",
315 .type = VLIB_NODE_TYPE_INPUT,
316 .state = VLIB_NODE_STATE_DISABLED,
320 ec_reset_runtime_config (echo_client_main_t *ecm)
323 ecm->quic_streams = 1;
324 ecm->bytes_to_send = 8192;
326 ecm->fifo_size = 64 << 10;
327 ecm->connections_per_batch = 1000;
328 ecm->private_segment_count = 0;
329 ecm->private_segment_size = 256 << 20;
332 ecm->test_failed = 0;
333 ecm->tls_engine = CRYPTO_ENGINE_OPENSSL;
335 ecm->run_test = ECHO_CLIENTS_STARTING;
336 ecm->ready_connections = 0;
337 ecm->connect_conn_index = 0;
340 ecm->barrier_acq_needed = 0;
341 ecm->prealloc_sessions = 0;
342 ecm->prealloc_fifos = 0;
344 ecm->appns_secret = 0;
345 ecm->attach_flags = 0;
346 ecm->syn_timeout = 20.0;
347 ecm->test_timeout = 20.0;
348 vec_free (ecm->connect_uri);
352 echo_clients_init (vlib_main_t * vm)
354 echo_client_main_t *ecm = &echo_client_main;
355 vlib_thread_main_t *vtm = vlib_get_thread_main ();
359 ec_reset_runtime_config (ecm);
361 /* Store cli process node index for signaling */
362 ecm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
365 if (vlib_num_workers ())
367 /* The request came over the binary api and the inband cli handler
368 * is not mp_safe. Drop the barrier to make sure the workers are not
371 if (vlib_thread_is_main_w_barrier ())
373 ecm->barrier_acq_needed = 1;
374 vlib_worker_thread_barrier_release (vm);
377 * There's a good chance that both the client and the server echo
378 * apps will be enabled so make sure the session queue node polls on
379 * the main thread as connections will probably be established on it.
381 vlib_node_set_state (vm, session_queue_node.index,
382 VLIB_NODE_STATE_POLLING);
384 clib_spinlock_init (&ecm->sessions_lock);
387 /* App init done only once */
388 if (ecm->app_is_init)
391 num_threads = 1 /* main thread */ + vtm->n_threads;
393 /* Init test data. Big buffer */
394 vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
395 for (i = 0; i < vec_len (ecm->connect_test_data); i++)
396 ecm->connect_test_data[i] = i & 0xff;
398 vec_validate (ecm->rx_buf, num_threads - 1);
399 for (i = 0; i < num_threads; i++)
400 vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
402 ecm->app_is_init = 1;
404 vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains);
405 vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains);
406 vec_validate (ecm->quic_session_index_by_thread, vtm->n_vlib_mains);
407 vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains);
409 vlib_worker_thread_barrier_sync (vm);
410 vnet_session_enable_disable (vm, 1 /* turn on session and transports */);
411 vlib_worker_thread_barrier_release (vm);
413 /* Turn on the builtin client input nodes */
414 for (i = 0; i < vtm->n_vlib_mains; i++)
415 vlib_node_set_state (vlib_get_main_by_index (i), echo_clients_node.index,
416 VLIB_NODE_STATE_POLLING);
422 echo_clients_cleanup (echo_client_main_t *ecm)
426 for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
428 vec_reset_length (ecm->connection_index_by_thread[i]);
429 vec_reset_length (ecm->connections_this_batch_by_thread[i]);
430 vec_reset_length (ecm->quic_session_index_by_thread[i]);
433 pool_free (ecm->sessions);
434 vec_free (ecm->connect_uri);
435 vec_free (ecm->appns_id);
436 clib_spinlock_free (&ecm->sessions_lock);
438 if (ecm->barrier_acq_needed)
439 vlib_worker_thread_barrier_sync (ecm->vlib_main);
443 quic_echo_clients_qsession_connected_callback (u32 app_index, u32 api_context,
447 echo_client_main_t *ecm = &echo_client_main;
448 vnet_connect_args_t *a = 0;
450 u8 thread_index = vlib_get_thread_index ();
451 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
453 session_handle_t handle;
455 DBG ("QUIC Connection handle %d", session_handle (s));
458 a->uri = (char *) ecm->connect_uri;
459 if (parse_uri (a->uri, &sep))
461 sep.parent_handle = handle = session_handle (s);
463 for (stream_n = 0; stream_n < ecm->quic_streams; stream_n++)
465 clib_memset (a, 0, sizeof (*a));
466 a->app_index = ecm->app_index;
467 a->api_context = -1 - api_context;
468 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
470 DBG ("QUIC opening stream %d", stream_n);
471 if ((rv = vnet_connect (a)))
473 clib_error ("Stream session %d opening failed: %d", stream_n, rv);
476 DBG ("QUIC stream %d connected", stream_n);
479 * 's' is no longer valid, its underlying pool could have been moved in
482 vec_add1 (ecm->quic_session_index_by_thread[thread_index], handle);
488 quic_echo_clients_session_connected_callback (u32 app_index, u32 api_context,
492 echo_client_main_t *ecm = &echo_client_main;
493 eclient_session_t *session;
497 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_STARTING))
502 clib_warning ("connection %d failed!", api_context);
503 ecm->run_test = ECHO_CLIENTS_EXITING;
504 signal_evt_to_cli (-1);
508 if (s->listener_handle == SESSION_INVALID_HANDLE)
509 return quic_echo_clients_qsession_connected_callback (app_index,
512 DBG ("STREAM Connection callback %d", api_context);
514 thread_index = s->thread_index;
515 ASSERT (thread_index == vlib_get_thread_index ()
516 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
518 if (!ecm->vpp_event_queue[thread_index])
519 ecm->vpp_event_queue[thread_index] =
520 session_main_get_vpp_event_queue (thread_index);
525 clib_spinlock_lock_if_init (&ecm->sessions_lock);
526 pool_get (ecm->sessions, session);
527 clib_spinlock_unlock_if_init (&ecm->sessions_lock);
529 clib_memset (session, 0, sizeof (*session));
530 session_index = session - ecm->sessions;
531 session->bytes_to_send = ecm->bytes_to_send;
532 session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
533 session->data.rx_fifo = s->rx_fifo;
534 session->data.rx_fifo->shr->client_session_index = session_index;
535 session->data.tx_fifo = s->tx_fifo;
536 session->data.tx_fifo->shr->client_session_index = session_index;
537 session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
538 session->vpp_session_handle = session_handle (s);
542 transport_connection_t *tc;
543 tc = session_get_transport (s);
544 clib_memcpy_fast (&session->data.transport, tc,
545 sizeof (session->data.transport));
546 session->data.is_dgram = 1;
549 vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
550 clib_atomic_fetch_add (&ecm->ready_connections, 1);
551 if (ecm->ready_connections == ecm->expected_connections)
553 ecm->run_test = ECHO_CLIENTS_RUNNING;
554 /* Signal the CLI process that the action is starting... */
555 signal_evt_to_cli (1);
562 echo_clients_session_connected_callback (u32 app_index, u32 api_context,
563 session_t * s, session_error_t err)
565 echo_client_main_t *ecm = &echo_client_main;
566 eclient_session_t *session;
570 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_STARTING))
575 clib_warning ("connection %d failed!", api_context);
576 ecm->run_test = ECHO_CLIENTS_EXITING;
577 signal_evt_to_cli (-1);
581 thread_index = s->thread_index;
582 ASSERT (thread_index == vlib_get_thread_index ()
583 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
585 if (!ecm->vpp_event_queue[thread_index])
586 ecm->vpp_event_queue[thread_index] =
587 session_main_get_vpp_event_queue (thread_index);
592 clib_spinlock_lock_if_init (&ecm->sessions_lock);
593 pool_get (ecm->sessions, session);
594 clib_spinlock_unlock_if_init (&ecm->sessions_lock);
596 clib_memset (session, 0, sizeof (*session));
597 session_index = session - ecm->sessions;
598 session->bytes_to_send = ecm->bytes_to_send;
599 session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
600 session->data.rx_fifo = s->rx_fifo;
601 session->data.rx_fifo->shr->client_session_index = session_index;
602 session->data.tx_fifo = s->tx_fifo;
603 session->data.tx_fifo->shr->client_session_index = session_index;
604 session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
605 session->vpp_session_handle = session_handle (s);
609 transport_connection_t *tc;
610 tc = session_get_transport (s);
611 clib_memcpy_fast (&session->data.transport, tc,
612 sizeof (session->data.transport));
613 session->data.is_dgram = 1;
616 vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
617 clib_atomic_fetch_add (&ecm->ready_connections, 1);
618 if (ecm->ready_connections == ecm->expected_connections)
620 ecm->run_test = ECHO_CLIENTS_RUNNING;
621 /* Signal the CLI process that the action is starting... */
622 signal_evt_to_cli (1);
629 echo_clients_session_reset_callback (session_t * s)
631 echo_client_main_t *ecm = &echo_client_main;
632 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
634 if (s->session_state == SESSION_STATE_READY)
635 clib_warning ("Reset active connection %U", format_session, s, 2);
637 a->handle = session_handle (s);
638 a->app_index = ecm->app_index;
639 vnet_disconnect_session (a);
644 echo_clients_session_create_callback (session_t * s)
650 echo_clients_session_disconnect_callback (session_t * s)
652 echo_client_main_t *ecm = &echo_client_main;
653 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
654 a->handle = session_handle (s);
655 a->app_index = ecm->app_index;
656 vnet_disconnect_session (a);
661 echo_clients_session_disconnect (session_t * s)
663 echo_client_main_t *ecm = &echo_client_main;
664 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
665 a->handle = session_handle (s);
666 a->app_index = ecm->app_index;
667 vnet_disconnect_session (a);
671 echo_clients_rx_callback (session_t * s)
673 echo_client_main_t *ecm = &echo_client_main;
674 eclient_session_t *sp;
676 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_RUNNING))
678 echo_clients_session_disconnect (s);
683 pool_elt_at_index (ecm->sessions, s->rx_fifo->shr->client_session_index);
684 receive_data_chunk (ecm, sp);
686 if (svm_fifo_max_dequeue_cons (s->rx_fifo))
688 if (svm_fifo_set_event (s->rx_fifo))
689 session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
695 echo_client_add_segment_callback (u32 client_index, u64 segment_handle)
697 /* New heaps may be added */
701 static session_cb_vft_t echo_clients = {
702 .session_reset_callback = echo_clients_session_reset_callback,
703 .session_connected_callback = echo_clients_session_connected_callback,
704 .session_accept_callback = echo_clients_session_create_callback,
705 .session_disconnect_callback = echo_clients_session_disconnect_callback,
706 .builtin_app_rx_callback = echo_clients_rx_callback,
707 .add_segment_callback = echo_client_add_segment_callback
710 static clib_error_t *
711 echo_clients_attach ()
713 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
714 echo_client_main_t *ecm = &echo_client_main;
715 vnet_app_attach_args_t _a, *a = &_a;
720 clib_memset (a, 0, sizeof (*a));
721 clib_memset (options, 0, sizeof (options));
723 a->api_client_index = ~0;
724 a->name = format (0, "echo_client");
725 if (ecm->transport_proto == TRANSPORT_PROTO_QUIC)
726 echo_clients.session_connected_callback =
727 quic_echo_clients_session_connected_callback;
728 a->session_cb_vft = &echo_clients;
730 prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
732 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
733 options[APP_OPTIONS_SEGMENT_SIZE] = ecm->private_segment_size;
734 options[APP_OPTIONS_ADD_SEGMENT_SIZE] = ecm->private_segment_size;
735 options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
736 options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
737 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
738 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
739 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
740 options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
741 options[APP_OPTIONS_PCT_FIRST_ALLOC] = 100;
742 options[APP_OPTIONS_FLAGS] |= ecm->attach_flags;
745 options[APP_OPTIONS_NAMESPACE_SECRET] = ecm->appns_secret;
746 a->namespace_id = ecm->appns_id;
748 a->options = options;
750 if ((rv = vnet_application_attach (a)))
751 return clib_error_return (0, "attach returned %d", rv);
753 ecm->app_index = a->app_index;
756 clib_memset (ck_pair, 0, sizeof (*ck_pair));
757 ck_pair->cert = (u8 *) test_srv_crt_rsa;
758 ck_pair->key = (u8 *) test_srv_key_rsa;
759 ck_pair->cert_len = test_srv_crt_rsa_len;
760 ck_pair->key_len = test_srv_key_rsa_len;
761 vnet_app_add_cert_key_pair (ck_pair);
762 ecm->ckpair_index = ck_pair->index;
764 ecm->test_client_attached = 1;
770 echo_clients_detach ()
772 echo_client_main_t *ecm = &echo_client_main;
773 vnet_app_detach_args_t _da, *da = &_da;
776 if (!ecm->test_client_attached)
779 da->app_index = ecm->app_index;
780 da->api_client_index = ~0;
781 rv = vnet_application_detach (da);
782 ecm->test_client_attached = 0;
784 vnet_app_del_cert_key_pair (ecm->ckpair_index);
790 echo_client_thread_fn (void *arg)
795 /** Start a transmit thread */
797 echo_clients_start_tx_pthread (echo_client_main_t * ecm)
799 if (ecm->client_thread_handle == 0)
801 int rv = pthread_create (&ecm->client_thread_handle,
803 echo_client_thread_fn, 0);
806 ecm->client_thread_handle = 0;
814 ec_transport_needs_crypto (transport_proto_t proto)
816 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
817 proto == TRANSPORT_PROTO_QUIC;
821 echo_clients_connect_rpc (void *args)
823 echo_client_main_t *ecm = &echo_client_main;
824 vnet_connect_args_t _a = {}, *a = &_a;
825 vlib_main_t *vm = vlib_get_main ();
826 int rv, needs_crypto;
829 n_clients = ecm->n_clients;
830 needs_crypto = ec_transport_needs_crypto (ecm->transport_proto);
831 clib_memcpy (&a->sep_ext, &ecm->connect_sep, sizeof (ecm->connect_sep));
832 a->app_index = ecm->app_index;
834 ci = ecm->connect_conn_index;
836 vlib_worker_thread_barrier_sync (vm);
838 while (ci < n_clients)
840 /* Crude pacing for call setups */
841 if (ci - ecm->ready_connections > 128)
843 ecm->connect_conn_index = ci;
850 session_endpoint_alloc_ext_cfg (&a->sep_ext,
851 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
852 a->sep_ext.ext_cfg->crypto.ckpair_index = ecm->ckpair_index;
855 rv = vnet_connect (a);
858 clib_mem_free (a->sep_ext.ext_cfg);
862 clib_warning ("connect returned: %U", format_session_error, rv);
863 signal_evt_to_cli (2);
870 vlib_worker_thread_barrier_release (vm);
872 if (ci < ecm->expected_connections)
873 echo_clients_program_connects ();
879 echo_clients_program_connects (void)
881 session_send_rpc_evt_to_thread_force (0, echo_clients_connect_rpc, 0);
884 #define ec_cli(_fmt, _args...) \
885 if (!ecm->no_output) \
886 vlib_cli_output (vm, _fmt, ##_args)
888 static clib_error_t *
889 echo_clients_command_fn (vlib_main_t *vm, unformat_input_t *input,
890 vlib_cli_command_t *cmd)
892 unformat_input_t _line_input, *line_input = &_line_input;
893 char *default_uri = "tcp://6.0.1.1/1234", *transfer_type;
894 echo_client_main_t *ecm = &echo_client_main;
895 uword *event_data = 0, event_type;
896 clib_error_t *error = 0;
897 int rv, had_config = 1;
898 u64 tmp, total_bytes;
901 if (ecm->test_client_attached)
902 return clib_error_return (0, "failed: already running!");
904 if (echo_clients_init (vm))
906 error = clib_error_return (0, "failed init");
910 if (!unformat_user (input, unformat_line_input, line_input))
916 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
918 if (unformat (line_input, "uri %s", &ecm->connect_uri))
920 else if (unformat (line_input, "nclients %d", &ecm->n_clients))
922 else if (unformat (line_input, "quic-streams %d", &ecm->quic_streams))
924 else if (unformat (line_input, "mbytes %lld", &tmp))
925 ecm->bytes_to_send = tmp << 20;
926 else if (unformat (line_input, "gbytes %lld", &tmp))
927 ecm->bytes_to_send = tmp << 30;
928 else if (unformat (line_input, "bytes %U", unformat_memory_size,
929 &ecm->bytes_to_send))
931 else if (unformat (line_input, "test-timeout %f", &ecm->test_timeout))
933 else if (unformat (line_input, "syn-timeout %f", &ecm->syn_timeout))
935 else if (unformat (line_input, "no-return"))
937 else if (unformat (line_input, "fifo-size %d", &ecm->fifo_size))
938 ecm->fifo_size <<= 10;
939 else if (unformat (line_input, "private-segment-count %d",
940 &ecm->private_segment_count))
942 else if (unformat (line_input, "private-segment-size %U",
943 unformat_memory_size, &ecm->private_segment_size))
945 else if (unformat (line_input, "preallocate-fifos"))
946 ecm->prealloc_fifos = 1;
947 else if (unformat (line_input, "preallocate-sessions"))
948 ecm->prealloc_sessions = 1;
949 else if (unformat (line_input, "client-batch %d",
950 &ecm->connections_per_batch))
952 else if (unformat (line_input, "appns %_%v%_", &ecm->appns_id))
954 else if (unformat (line_input, "all-scope"))
955 ecm->attach_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE |
956 APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
957 else if (unformat (line_input, "local-scope"))
958 ecm->attach_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
959 else if (unformat (line_input, "global-scope"))
960 ecm->attach_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
961 else if (unformat (line_input, "secret %lu", &ecm->appns_secret))
963 else if (unformat (line_input, "no-output"))
965 else if (unformat (line_input, "test-bytes"))
967 else if (unformat (line_input, "tls-engine %d", &ecm->tls_engine))
971 error = clib_error_return (0, "failed: unknown input `%U'",
972 format_unformat_error, line_input);
979 ecm->expected_connections = ecm->n_clients * ecm->quic_streams;
981 if (!ecm->connect_uri)
983 clib_warning ("No uri provided. Using default: %s", default_uri);
984 ecm->connect_uri = format (0, "%s%c", default_uri, 0);
987 if ((rv = parse_uri ((char *) ecm->connect_uri, &ecm->connect_sep)))
989 error = clib_error_return (0, "Uri parse error: %d", rv);
992 ecm->transport_proto = ecm->connect_sep.transport_proto;
993 ecm->is_dgram = (ecm->transport_proto == TRANSPORT_PROTO_UDP);
995 if (ecm->prealloc_sessions)
996 pool_init_fixed (ecm->sessions, 1.1 * ecm->n_clients);
998 if ((error = echo_clients_attach ()))
1000 clib_error_report (error);
1005 * Start. Fire off connect requests
1008 ecm->syn_start_time = vlib_time_now (vm);
1009 echo_clients_program_connects ();
1012 * Park until the sessions come up, or syn_timeout seconds pass
1015 vlib_process_wait_for_event_or_clock (vm, ecm->syn_timeout);
1016 event_type = vlib_process_get_events (vm, &event_data);
1020 ec_cli ("Timeout with only %d sessions active...",
1021 ecm->ready_connections);
1022 error = clib_error_return (0, "failed: syn timeout with %d sessions",
1023 ecm->ready_connections);
1027 delta = vlib_time_now (vm) - ecm->syn_start_time;
1029 ec_cli ("%d three-way handshakes in %.2f seconds %.2f/s",
1030 ecm->n_clients, delta, ((f64) ecm->n_clients) / delta);
1034 error = clib_error_return (0, "failed: connect returned");
1037 ec_cli ("unexpected event(1): %d", event_type);
1038 error = clib_error_return (0, "failed: unexpected event(1): %d",
1044 * Wait for the sessions to finish or test_timeout seconds pass
1046 ecm->test_start_time = vlib_time_now (ecm->vlib_main);
1047 ec_cli ("Test started at %.6f", ecm->test_start_time);
1048 vlib_process_wait_for_event_or_clock (vm, ecm->test_timeout);
1049 event_type = vlib_process_get_events (vm, &event_data);
1053 ec_cli ("Timeout with %d sessions still active...",
1054 ecm->ready_connections);
1055 error = clib_error_return (0, "failed: timeout with %d sessions",
1056 ecm->ready_connections);
1060 ecm->test_end_time = vlib_time_now (vm);
1061 ec_cli ("Test finished at %.6f", ecm->test_end_time);
1065 ec_cli ("unexpected event(2): %d", event_type);
1066 error = clib_error_return (0, "failed: unexpected event(2): %d",
1072 * Done. Compute stats
1074 delta = ecm->test_end_time - ecm->test_start_time;
1077 ec_cli ("zero delta-t?");
1078 error = clib_error_return (0, "failed: zero delta-t");
1082 total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
1083 transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
1084 ec_cli ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds", total_bytes,
1085 total_bytes / (1ULL << 20), total_bytes / (1ULL << 30), delta);
1086 ec_cli ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
1088 ec_cli ("%.4f gbit/second %s", (((f64) total_bytes * 8.0) / delta / 1e9),
1091 if (ecm->test_bytes && ecm->test_failed)
1092 error = clib_error_return (0, "failed: test bytes");
1099 ecm->run_test = ECHO_CLIENTS_EXITING;
1100 vlib_process_wait_for_event_or_clock (vm, 10e-3);
1102 /* Detach the application, so we can use different fifo sizes next time */
1103 if (echo_clients_detach ())
1105 error = clib_error_return (0, "failed: app detach");
1106 ec_cli ("WARNING: app detach failed...");
1109 echo_clients_cleanup (ecm);
1111 unformat_free (line_input);
1114 ec_cli ("test failed");
1119 VLIB_CLI_COMMAND (echo_clients_command, static) =
1121 .path = "test echo clients",
1122 .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
1123 "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
1124 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
1125 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
1126 "[uri <tcp://ip/port>][test-bytes][no-output]",
1127 .function = echo_clients_command_fn,
1132 echo_clients_main_init (vlib_main_t * vm)
1134 echo_client_main_t *ecm = &echo_client_main;
1135 ecm->app_is_init = 0;
1139 VLIB_INIT_FUNCTION (echo_clients_main_init);
1142 * fd.io coding-style-patch-verification: ON
1145 * eval: (c-set-style "gnu")