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 <hs_apps/echo_client.h>
20 static ec_main_t ec_main;
22 #define ec_err(_fmt, _args...) clib_warning (_fmt, ##_args);
24 #define ec_dbg(_fmt, _args...) \
27 if (ec_main.cfg.verbose) \
28 ec_err (_fmt, ##_args); \
32 #define ec_cli(_fmt, _args...) vlib_cli_output (vm, _fmt, ##_args)
35 signal_evt_to_cli_i (void *codep)
37 ec_main_t *ecm = &ec_main;
40 ASSERT (vlib_get_thread_index () == 0);
41 code = pointer_to_uword (codep);
42 vlib_process_signal_event (ecm->vlib_main, ecm->cli_node_index, code, 0);
46 signal_evt_to_cli (int code)
48 if (vlib_get_thread_index () != 0)
49 session_send_rpc_evt_to_thread_force (
50 0, signal_evt_to_cli_i, uword_to_pointer ((uword) code, void *));
52 signal_evt_to_cli_i (uword_to_pointer ((uword) code, void *));
55 static inline ec_worker_t *
56 ec_worker_get (u32 thread_index)
58 return vec_elt_at_index (ec_main.wrk, thread_index);
61 static inline ec_session_t *
62 ec_session_alloc (ec_worker_t *wrk)
66 pool_get_zero (wrk->sessions, ecs);
67 ecs->data.session_index = ecs - wrk->sessions;
68 ecs->thread_index = wrk->thread_index;
73 static inline ec_session_t *
74 ec_session_get (ec_worker_t *wrk, u32 ec_index)
76 return pool_elt_at_index (wrk->sessions, ec_index);
80 send_data_chunk (ec_main_t *ecm, ec_session_t *es)
82 u8 *test_data = ecm->connect_test_data;
83 int test_buf_len, test_buf_offset, rv;
86 test_buf_len = vec_len (test_data);
87 ASSERT (test_buf_len > 0);
88 test_buf_offset = es->bytes_sent % test_buf_len;
90 clib_min (test_buf_len - test_buf_offset, es->bytes_to_send);
96 svm_fifo_t *f = es->data.tx_fifo;
97 rv = clib_min (svm_fifo_max_enqueue_prod (f), bytes_this_chunk);
98 svm_fifo_enqueue_nocopy (f, rv);
99 session_send_io_evt_to_thread_custom (
100 &es->vpp_session_index, es->thread_index, SESSION_IO_EVT_TX);
103 rv = app_send_stream (&es->data, test_data + test_buf_offset,
104 bytes_this_chunk, 0);
108 svm_fifo_t *f = es->data.tx_fifo;
109 u32 max_enqueue = svm_fifo_max_enqueue_prod (f);
111 if (max_enqueue < sizeof (session_dgram_hdr_t))
114 max_enqueue -= sizeof (session_dgram_hdr_t);
118 session_dgram_hdr_t hdr;
119 app_session_transport_t *at = &es->data.transport;
121 rv = clib_min (max_enqueue, bytes_this_chunk);
123 hdr.data_length = rv;
125 clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip,
126 sizeof (ip46_address_t));
127 hdr.is_ip4 = at->is_ip4;
128 hdr.rmt_port = at->rmt_port;
129 clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip,
130 sizeof (ip46_address_t));
131 hdr.lcl_port = at->lcl_port;
132 svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
133 svm_fifo_enqueue_nocopy (f, rv);
134 session_send_io_evt_to_thread_custom (
135 &es->vpp_session_index, es->thread_index, SESSION_IO_EVT_TX);
139 bytes_this_chunk = clib_min (bytes_this_chunk, max_enqueue);
140 bytes_this_chunk = clib_min (bytes_this_chunk, 1460);
141 rv = app_send_dgram (&es->data, test_data + test_buf_offset,
142 bytes_this_chunk, 0);
146 /* If we managed to enqueue data... */
149 /* Account for it... */
150 es->bytes_to_send -= rv;
151 es->bytes_sent += rv;
153 if (ecm->cfg.verbose)
155 ELOG_TYPE_DECLARE (e) =
157 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
158 .format_args = "i4i4i4",
164 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
166 ed->data[1] = es->bytes_sent;
167 ed->data[2] = es->bytes_to_send;
173 receive_data_chunk (ec_worker_t *wrk, ec_session_t *es)
175 ec_main_t *ecm = &ec_main;
176 svm_fifo_t *rx_fifo = es->data.rx_fifo;
179 if (ecm->cfg.test_bytes)
183 app_recv_stream (&es->data, wrk->rx_buf, vec_len (wrk->rx_buf));
186 app_recv_dgram (&es->data, wrk->rx_buf, vec_len (wrk->rx_buf));
190 n_read = svm_fifo_max_dequeue_cons (rx_fifo);
191 svm_fifo_dequeue_drop (rx_fifo, n_read);
196 if (ecm->cfg.verbose)
198 ELOG_TYPE_DECLARE (e) =
200 .format = "rx-deq: %d bytes",
207 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
208 ed->data[0] = n_read;
211 if (ecm->cfg.test_bytes)
213 for (i = 0; i < n_read; i++)
215 if (wrk->rx_buf[i] != ((es->bytes_received + i) & 0xff))
217 ec_err ("read %d error at byte %lld, 0x%x not 0x%x", n_read,
218 es->bytes_received + i, wrk->rx_buf[i],
219 ((es->bytes_received + i) & 0xff));
220 ecm->test_failed = 1;
224 ASSERT (n_read <= es->bytes_to_receive);
225 es->bytes_to_receive -= n_read;
226 es->bytes_received += n_read;
231 ec_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
233 u32 *conn_indices, *conns_this_batch, nconns_this_batch;
234 int thread_index = vm->thread_index, i, delete_session;
235 ec_main_t *ecm = &ec_main;
240 if (ecm->run_test != EC_RUNNING)
243 wrk = ec_worker_get (thread_index);
244 conn_indices = wrk->conn_indices;
245 conns_this_batch = wrk->conns_this_batch;
247 if (((vec_len (conn_indices) == 0) && vec_len (conns_this_batch) == 0))
250 /* Grab another pile of connections */
251 if (PREDICT_FALSE (vec_len (conns_this_batch) == 0))
254 clib_min (ecm->connections_per_batch, vec_len (conn_indices));
256 ASSERT (nconns_this_batch > 0);
257 vec_validate (conns_this_batch, nconns_this_batch - 1);
258 clib_memcpy_fast (conns_this_batch,
259 conn_indices + vec_len (conn_indices) -
261 nconns_this_batch * sizeof (u32));
262 vec_dec_len (conn_indices, nconns_this_batch);
268 if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch &&
269 ecm->prev_conns == vec_len (conns_this_batch)))
272 ecm->prev_conns = vec_len (conns_this_batch);
273 if (ecm->repeats == 500000)
275 ec_err ("stuck clients");
280 ecm->prev_conns = vec_len (conns_this_batch);
285 * Handle connections in this batch
287 for (i = 0; i < vec_len (conns_this_batch); i++)
289 es = ec_session_get (wrk, conns_this_batch[i]);
293 if (es->bytes_to_send > 0)
295 send_data_chunk (ecm, es);
299 if (es->bytes_to_receive > 0)
304 if (PREDICT_FALSE (delete_session == 1))
306 clib_atomic_fetch_add (&ecm->tx_total, es->bytes_sent);
307 clib_atomic_fetch_add (&ecm->rx_total, es->bytes_received);
308 s = session_get_from_handle_if_valid (es->vpp_session_handle);
312 vnet_disconnect_args_t _a, *a = &_a;
313 a->handle = session_handle (s);
314 a->app_index = ecm->app_index;
315 vnet_disconnect_session (a);
317 vec_delete (conns_this_batch, 1, i);
319 clib_atomic_fetch_add (&ecm->ready_connections, -1);
323 ec_err ("session AWOL?");
324 vec_delete (conns_this_batch, 1, i);
327 /* Kick the debug CLI process */
328 if (ecm->ready_connections == 0)
330 signal_evt_to_cli (EC_CLI_TEST_DONE);
335 wrk->conn_indices = conn_indices;
336 wrk->conns_this_batch = conns_this_batch;
340 VLIB_REGISTER_NODE (echo_clients_node) = {
341 .function = ec_node_fn,
342 .name = "echo-clients",
343 .type = VLIB_NODE_TYPE_INPUT,
344 .state = VLIB_NODE_STATE_DISABLED,
348 ec_reset_runtime_config (ec_main_t *ecm)
350 hs_test_cfg_init (&ecm->cfg);
352 ecm->quic_streams = 1;
353 ecm->bytes_to_send = 8192;
355 ecm->fifo_size = 64 << 10;
356 ecm->connections_per_batch = 1000;
357 ecm->private_segment_count = 0;
358 ecm->private_segment_size = 256 << 20;
359 ecm->test_failed = 0;
360 ecm->tls_engine = CRYPTO_ENGINE_OPENSSL;
362 ecm->run_test = EC_STARTING;
363 ecm->ready_connections = 0;
364 ecm->connect_conn_index = 0;
367 ecm->barrier_acq_needed = 0;
368 ecm->prealloc_sessions = 0;
369 ecm->prealloc_fifos = 0;
371 ecm->appns_secret = 0;
372 ecm->attach_flags = 0;
373 ecm->syn_timeout = 20.0;
374 ecm->test_timeout = 20.0;
375 vec_free (ecm->connect_uri);
379 ec_init (vlib_main_t *vm)
381 ec_main_t *ecm = &ec_main;
386 ec_reset_runtime_config (ecm);
388 /* Store cli process node index for signaling */
389 ecm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
392 if (vlib_num_workers ())
394 /* The request came over the binary api and the inband cli handler
395 * is not mp_safe. Drop the barrier to make sure the workers are not
398 if (vlib_thread_is_main_w_barrier ())
400 ecm->barrier_acq_needed = 1;
401 vlib_worker_thread_barrier_release (vm);
404 * There's a good chance that both the client and the server echo
405 * apps will be enabled so make sure the session queue node polls on
406 * the main thread as connections will probably be established on it.
408 vlib_node_set_state (vm, session_queue_node.index,
409 VLIB_NODE_STATE_POLLING);
412 /* App init done only once */
413 if (ecm->app_is_init)
417 /* Init test data. Big buffer */
418 vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
419 for (i = 0; i < vec_len (ecm->connect_test_data); i++)
420 ecm->connect_test_data[i] = i & 0xff;
422 num_threads = 1 /* main thread */ + vlib_num_workers ();
423 vec_validate (ecm->wrk, num_threads - 1);
424 vec_foreach (wrk, ecm->wrk)
426 vec_validate (wrk->rx_buf, vec_len (ecm->connect_test_data) - 1);
427 wrk->thread_index = wrk - ecm->wrk;
428 wrk->vpp_event_queue =
429 session_main_get_vpp_event_queue (wrk->thread_index);
432 ecm->app_is_init = 1;
434 vlib_worker_thread_barrier_sync (vm);
435 vnet_session_enable_disable (vm, 1 /* turn on session and transports */);
437 /* Turn on the builtin client input nodes */
439 vlib_node_set_state (this_vlib_main, echo_clients_node.index,
440 VLIB_NODE_STATE_POLLING);
442 vlib_worker_thread_barrier_release (vm);
448 ec_prealloc_sessions (ec_main_t *ecm)
450 u32 sessions_per_wrk, n_wrks;
453 n_wrks = vlib_num_workers () ? vlib_num_workers () : 1;
455 sessions_per_wrk = ecm->n_clients / n_wrks;
456 vec_foreach (wrk, ecm->wrk)
457 pool_init_fixed (wrk->sessions, 1.1 * sessions_per_wrk);
461 ec_worker_cleanup (ec_worker_t *wrk)
463 pool_free (wrk->sessions);
464 vec_free (wrk->conn_indices);
465 vec_free (wrk->conns_this_batch);
469 ec_cleanup (ec_main_t *ecm)
473 vec_foreach (wrk, ecm->wrk)
474 ec_worker_cleanup (wrk);
476 vec_free (ecm->connect_uri);
477 vec_free (ecm->appns_id);
479 if (ecm->barrier_acq_needed)
480 vlib_worker_thread_barrier_sync (ecm->vlib_main);
484 quic_ec_qsession_connected_callback (u32 app_index, u32 api_context,
485 session_t *s, session_error_t err)
487 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
488 ec_main_t *ecm = &ec_main;
489 vnet_connect_args_t _a, *a = &_a;
493 ec_dbg ("QUIC Connection handle %d", session_handle (s));
495 a->uri = (char *) ecm->connect_uri;
496 if (parse_uri (a->uri, &sep))
498 sep.parent_handle = session_handle (s);
500 for (stream_n = 0; stream_n < ecm->quic_streams; stream_n++)
502 clib_memset (a, 0, sizeof (*a));
503 a->app_index = ecm->app_index;
504 a->api_context = -2 - api_context;
505 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
507 ec_dbg ("QUIC opening stream %d", stream_n);
508 if ((rv = vnet_connect (a)))
510 clib_error ("Stream session %d opening failed: %d", stream_n, rv);
513 ec_dbg ("QUIC stream %d connected", stream_n);
519 ec_ctrl_send (hs_test_cmd_t cmd)
521 ec_main_t *ecm = &ec_main;
526 if (ecm->ctrl_session_handle == SESSION_INVALID_HANDLE)
528 ec_dbg ("ctrl session went away");
532 s = session_get_from_handle_if_valid (ecm->ctrl_session_handle);
535 ec_err ("ctrl session not found");
539 ec_dbg ("sending test paramters to the server..");
540 if (ecm->cfg.verbose)
541 hs_test_cfg_dump (&ecm->cfg, 1);
543 rv = svm_fifo_enqueue (s->tx_fifo, sizeof (ecm->cfg), (u8 *) &ecm->cfg);
544 ASSERT (rv == sizeof (ecm->cfg));
545 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
550 ec_ctrl_session_connected_callback (session_t *s)
552 ec_main_t *ecm = &ec_main;
554 s->opaque = HS_CTRL_HANDLE;
555 ecm->ctrl_session_handle = session_handle (s);
557 /* send test parameters to the server */
558 ec_ctrl_send (HS_TEST_CMD_SYNC);
563 quic_ec_session_connected_callback (u32 app_index, u32 api_context,
564 session_t *s, session_error_t err)
566 ec_main_t *ecm = &ec_main;
571 if (PREDICT_FALSE (api_context == HS_CTRL_HANDLE))
572 return ec_ctrl_session_connected_callback (s);
574 if (PREDICT_FALSE (ecm->run_test != EC_STARTING))
579 ec_err ("connection %d failed!", api_context);
580 ecm->run_test = EC_EXITING;
581 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
585 if (s->listener_handle == SESSION_INVALID_HANDLE)
586 return quic_ec_qsession_connected_callback (app_index, api_context, s,
588 ec_dbg ("STREAM Connection callback %d", api_context);
590 thread_index = s->thread_index;
591 ASSERT (thread_index == vlib_get_thread_index ()
592 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
594 wrk = ec_worker_get (thread_index);
599 es = ec_session_alloc (wrk);
601 es->bytes_to_send = ecm->bytes_to_send;
602 es->bytes_to_receive = ecm->echo_bytes ? ecm->bytes_to_send : 0ULL;
603 es->data.rx_fifo = s->rx_fifo;
604 es->data.rx_fifo->shr->client_session_index = es->data.session_index;
605 es->data.tx_fifo = s->tx_fifo;
606 es->data.tx_fifo->shr->client_session_index = es->data.session_index;
607 es->data.vpp_evt_q = wrk->vpp_event_queue;
608 es->vpp_session_handle = session_handle (s);
609 es->vpp_session_index = s->session_index;
610 s->opaque = es->data.session_index;
614 transport_connection_t *tc;
615 tc = session_get_transport (s);
616 clib_memcpy_fast (&es->data.transport, tc, sizeof (es->data.transport));
617 es->data.is_dgram = 1;
620 vec_add1 (wrk->conn_indices, es->data.session_index);
621 clib_atomic_fetch_add (&ecm->ready_connections, 1);
622 if (ecm->ready_connections == ecm->expected_connections)
624 ecm->run_test = EC_RUNNING;
625 /* Signal the CLI process that the action is starting... */
626 signal_evt_to_cli (EC_CLI_CONNECTS_DONE);
633 ec_session_connected_callback (u32 app_index, u32 api_context, session_t *s,
636 ec_main_t *ecm = &ec_main;
641 if (PREDICT_FALSE (ecm->run_test != EC_STARTING))
646 ec_err ("connection %d failed! %U", api_context, format_session_error,
648 ecm->run_test = EC_EXITING;
649 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
653 thread_index = s->thread_index;
654 ASSERT (thread_index == vlib_get_thread_index ()
655 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
657 if (PREDICT_FALSE (api_context == HS_CTRL_HANDLE))
658 return ec_ctrl_session_connected_callback (s);
660 wrk = ec_worker_get (thread_index);
665 es = ec_session_alloc (wrk);
667 es->bytes_to_send = ecm->bytes_to_send;
668 es->bytes_to_receive = ecm->echo_bytes ? ecm->bytes_to_send : 0ULL;
669 es->data.rx_fifo = s->rx_fifo;
670 es->data.rx_fifo->shr->client_session_index = es->data.session_index;
671 es->data.tx_fifo = s->tx_fifo;
672 es->data.tx_fifo->shr->client_session_index = es->data.session_index;
673 es->data.vpp_evt_q = wrk->vpp_event_queue;
674 es->vpp_session_handle = session_handle (s);
675 es->vpp_session_index = s->session_index;
676 s->opaque = es->data.session_index;
680 transport_connection_t *tc;
681 tc = session_get_transport (s);
682 clib_memcpy_fast (&es->data.transport, tc, sizeof (es->data.transport));
683 es->data.is_dgram = 1;
686 vec_add1 (wrk->conn_indices, es->data.session_index);
687 clib_atomic_fetch_add (&ecm->ready_connections, 1);
688 if (ecm->ready_connections == ecm->expected_connections)
690 ecm->run_test = EC_RUNNING;
691 /* Signal the CLI process that the action is starting... */
692 signal_evt_to_cli (EC_CLI_CONNECTS_DONE);
699 ec_session_reset_callback (session_t *s)
701 ec_main_t *ecm = &ec_main;
702 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
704 if (s->session_state == SESSION_STATE_READY)
705 ec_err ("Reset active connection %U", format_session, s, 2);
707 a->handle = session_handle (s);
708 a->app_index = ecm->app_index;
709 vnet_disconnect_session (a);
714 ec_session_accept_callback (session_t *s)
720 ec_session_disconnect_callback (session_t *s)
722 ec_main_t *ecm = &ec_main;
723 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
725 if (session_handle (s) == ecm->ctrl_session_handle)
727 ec_dbg ("ctrl session disconnect");
728 ecm->ctrl_session_handle = SESSION_INVALID_HANDLE;
731 a->handle = session_handle (s);
732 a->app_index = ecm->app_index;
733 vnet_disconnect_session (a);
738 ec_session_disconnect (session_t *s)
740 ec_main_t *ecm = &ec_main;
741 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
742 a->handle = session_handle (s);
743 a->app_index = ecm->app_index;
744 vnet_disconnect_session (a);
748 ec_ctrl_session_rx_callback (session_t *s)
750 ec_main_t *ecm = &ec_main;
752 hs_test_cfg_t cfg = { 0 };
754 rx_bytes = svm_fifo_dequeue (s->rx_fifo, sizeof (cfg), (u8 *) &cfg);
755 if (rx_bytes != sizeof (cfg))
757 ec_err ("invalid cfg length %d (expected %d)", rx_bytes, sizeof (cfg));
758 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
762 ec_dbg ("control message received:");
763 if (ecm->cfg.verbose)
764 hs_test_cfg_dump (&cfg, 1);
768 case HS_TEST_CMD_SYNC:
769 switch (ecm->run_test)
772 if (!hs_test_cfg_verify (&cfg, &ecm->cfg))
774 ec_err ("invalid config received from server!");
775 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
778 signal_evt_to_cli (EC_CLI_CFG_SYNC);
782 ec_dbg ("test running..");
787 signal_evt_to_cli (EC_CLI_CFG_SYNC);
791 ec_err ("unexpected test state! %d", ecm->run_test);
795 case HS_TEST_CMD_START:
796 signal_evt_to_cli (EC_CLI_START);
798 case HS_TEST_CMD_STOP:
799 signal_evt_to_cli (EC_CLI_STOP);
802 ec_err ("unexpected cmd! %d", cfg.cmd);
810 ec_session_rx_callback (session_t *s)
812 ec_main_t *ecm = &ec_main;
816 if (PREDICT_FALSE (s->opaque == HS_CTRL_HANDLE))
817 return ec_ctrl_session_rx_callback (s);
819 if (PREDICT_FALSE (ecm->run_test != EC_RUNNING))
821 ec_session_disconnect (s);
825 wrk = ec_worker_get (s->thread_index);
826 es = ec_session_get (wrk, s->opaque);
828 receive_data_chunk (wrk, es);
830 if (svm_fifo_max_dequeue_cons (s->rx_fifo))
831 session_enqueue_notify (s);
837 ec_add_segment_callback (u32 app_index, u64 segment_handle)
839 /* New segments may be added */
844 ec_del_segment_callback (u32 app_index, u64 segment_handle)
849 static session_cb_vft_t ec_cb_vft = {
850 .session_reset_callback = ec_session_reset_callback,
851 .session_connected_callback = ec_session_connected_callback,
852 .session_accept_callback = ec_session_accept_callback,
853 .session_disconnect_callback = ec_session_disconnect_callback,
854 .builtin_app_rx_callback = ec_session_rx_callback,
855 .add_segment_callback = ec_add_segment_callback,
856 .del_segment_callback = ec_del_segment_callback,
859 static clib_error_t *
862 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
863 ec_main_t *ecm = &ec_main;
864 vnet_app_attach_args_t _a, *a = &_a;
869 clib_memset (a, 0, sizeof (*a));
870 clib_memset (options, 0, sizeof (options));
872 a->api_client_index = ~0;
873 a->name = format (0, "echo_client");
874 if (ecm->transport_proto == TRANSPORT_PROTO_QUIC)
875 ec_cb_vft.session_connected_callback = quic_ec_session_connected_callback;
876 a->session_cb_vft = &ec_cb_vft;
878 prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
880 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
881 options[APP_OPTIONS_SEGMENT_SIZE] = ecm->private_segment_size;
882 options[APP_OPTIONS_ADD_SEGMENT_SIZE] = ecm->private_segment_size;
883 options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
884 options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
885 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
886 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
887 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
888 options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
889 options[APP_OPTIONS_PCT_FIRST_ALLOC] = 100;
890 options[APP_OPTIONS_FLAGS] |= ecm->attach_flags;
893 options[APP_OPTIONS_NAMESPACE_SECRET] = ecm->appns_secret;
894 a->namespace_id = ecm->appns_id;
896 a->options = options;
898 if ((rv = vnet_application_attach (a)))
899 return clib_error_return (0, "attach returned %d", rv);
901 ecm->app_index = a->app_index;
904 clib_memset (ck_pair, 0, sizeof (*ck_pair));
905 ck_pair->cert = (u8 *) test_srv_crt_rsa;
906 ck_pair->key = (u8 *) test_srv_key_rsa;
907 ck_pair->cert_len = test_srv_crt_rsa_len;
908 ck_pair->key_len = test_srv_key_rsa_len;
909 vnet_app_add_cert_key_pair (ck_pair);
910 ecm->ckpair_index = ck_pair->index;
912 ecm->test_client_attached = 1;
920 ec_main_t *ecm = &ec_main;
921 vnet_app_detach_args_t _da, *da = &_da;
924 if (!ecm->test_client_attached)
927 da->app_index = ecm->app_index;
928 da->api_client_index = ~0;
929 rv = vnet_application_detach (da);
930 ecm->test_client_attached = 0;
932 vnet_app_del_cert_key_pair (ecm->ckpair_index);
938 ec_transport_needs_crypto (transport_proto_t proto)
940 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
941 proto == TRANSPORT_PROTO_QUIC;
945 ec_connect_rpc (void *args)
947 ec_main_t *ecm = &ec_main;
948 vnet_connect_args_t _a = {}, *a = &_a;
949 int rv, needs_crypto;
952 n_clients = ecm->n_clients;
953 needs_crypto = ec_transport_needs_crypto (ecm->transport_proto);
954 clib_memcpy (&a->sep_ext, &ecm->connect_sep, sizeof (ecm->connect_sep));
955 a->sep_ext.transport_flags |= TRANSPORT_CFG_F_CONNECTED;
956 a->app_index = ecm->app_index;
958 ci = ecm->connect_conn_index;
960 while (ci < n_clients)
962 /* Crude pacing for call setups */
963 if (ci - ecm->ready_connections > 128)
965 ecm->connect_conn_index = ci;
972 session_endpoint_alloc_ext_cfg (&a->sep_ext,
973 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
974 a->sep_ext.ext_cfg->crypto.ckpair_index = ecm->ckpair_index;
977 rv = vnet_connect (a);
980 clib_mem_free (a->sep_ext.ext_cfg);
984 ec_err ("connect returned: %U", format_session_error, rv);
985 ecm->run_test = EC_EXITING;
986 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
993 if (ci < ecm->expected_connections && ecm->run_test != EC_EXITING)
994 ec_program_connects ();
1000 ec_program_connects (void)
1002 session_send_rpc_evt_to_thread_force (transport_cl_thread (), ec_connect_rpc,
1006 static clib_error_t *
1007 ec_ctrl_connect_rpc ()
1010 ec_main_t *ecm = &ec_main;
1011 vnet_connect_args_t _a = {}, *a = &_a;
1013 a->api_context = HS_CTRL_HANDLE;
1014 ecm->cfg.cmd = HS_TEST_CMD_SYNC;
1015 clib_memcpy (&a->sep_ext, &ecm->connect_sep, sizeof (ecm->connect_sep));
1016 a->sep_ext.transport_proto = TRANSPORT_PROTO_TCP;
1017 a->app_index = ecm->app_index;
1019 rv = vnet_connect (a);
1022 ec_err ("ctrl connect returned: %U", format_session_error, rv);
1023 ecm->run_test = EC_EXITING;
1024 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
1030 ec_ctrl_connect (void)
1032 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
1033 ec_ctrl_connect_rpc, 0);
1037 ec_ctrl_session_disconnect ()
1039 ec_main_t *ecm = &ec_main;
1040 vnet_disconnect_args_t _a, *a = &_a;
1041 session_error_t err;
1043 a->handle = ecm->ctrl_session_handle;
1044 a->app_index = ecm->app_index;
1045 err = vnet_disconnect_session (a);
1047 ec_err ("vnet_disconnect_session: %U", format_session_error, err);
1051 ec_ctrl_test_sync ()
1053 ec_main_t *ecm = &ec_main;
1054 ecm->cfg.test = HS_TEST_TYPE_ECHO;
1055 return ec_ctrl_send (HS_TEST_CMD_SYNC);
1059 ec_ctrl_test_start ()
1061 return ec_ctrl_send (HS_TEST_CMD_START);
1065 ec_ctrl_test_stop ()
1067 return ec_ctrl_send (HS_TEST_CMD_STOP);
1070 #define ec_wait_for_signal(_sig) \
1071 vlib_process_wait_for_event_or_clock (vm, ecm->syn_timeout); \
1072 event_type = vlib_process_get_events (vm, &event_data); \
1073 switch (event_type) \
1076 ec_cli ("Timeout while waiting for " #_sig); \
1078 clib_error_return (0, "failed: timeout while waiting for " #_sig); \
1083 ec_cli ("unexpected event while waiting for " #_sig ": %d", \
1086 clib_error_return (0, "failed: unexpected event: %d", event_type); \
1090 static clib_error_t *
1091 ec_command_fn (vlib_main_t *vm, unformat_input_t *input,
1092 vlib_cli_command_t *cmd)
1094 unformat_input_t _line_input, *line_input = &_line_input;
1095 char *default_uri = "tcp://6.0.1.1/1234", *transfer_type;
1096 ec_main_t *ecm = &ec_main;
1097 uword *event_data = 0, event_type;
1098 clib_error_t *error = 0;
1099 int rv, had_config = 1;
1100 u64 tmp, total_bytes;
1103 if (ecm->test_client_attached)
1104 return clib_error_return (0, "failed: already running!");
1108 error = clib_error_return (0, "failed init");
1112 if (!unformat_user (input, unformat_line_input, line_input))
1118 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1120 if (unformat (line_input, "uri %s", &ecm->connect_uri))
1122 else if (unformat (line_input, "nclients %d", &ecm->n_clients))
1124 else if (unformat (line_input, "quic-streams %d", &ecm->quic_streams))
1126 else if (unformat (line_input, "mbytes %lld", &tmp))
1127 ecm->bytes_to_send = tmp << 20;
1128 else if (unformat (line_input, "gbytes %lld", &tmp))
1129 ecm->bytes_to_send = tmp << 30;
1130 else if (unformat (line_input, "bytes %U", unformat_memory_size,
1131 &ecm->bytes_to_send))
1133 else if (unformat (line_input, "test-timeout %f", &ecm->test_timeout))
1135 else if (unformat (line_input, "syn-timeout %f", &ecm->syn_timeout))
1137 else if (unformat (line_input, "echo-bytes"))
1138 ecm->echo_bytes = 1;
1139 else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
1142 else if (unformat (line_input, "private-segment-count %d",
1143 &ecm->private_segment_count))
1145 else if (unformat (line_input, "private-segment-size %U",
1146 unformat_memory_size, &ecm->private_segment_size))
1148 else if (unformat (line_input, "preallocate-fifos"))
1149 ecm->prealloc_fifos = 1;
1150 else if (unformat (line_input, "preallocate-sessions"))
1151 ecm->prealloc_sessions = 1;
1152 else if (unformat (line_input, "client-batch %d",
1153 &ecm->connections_per_batch))
1155 else if (unformat (line_input, "appns %_%v%_", &ecm->appns_id))
1157 else if (unformat (line_input, "all-scope"))
1158 ecm->attach_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE |
1159 APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
1160 else if (unformat (line_input, "local-scope"))
1161 ecm->attach_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1162 else if (unformat (line_input, "global-scope"))
1163 ecm->attach_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1164 else if (unformat (line_input, "secret %lu", &ecm->appns_secret))
1166 else if (unformat (line_input, "verbose"))
1167 ecm->cfg.verbose = 1;
1168 else if (unformat (line_input, "test-bytes"))
1169 ecm->cfg.test_bytes = 1;
1170 else if (unformat (line_input, "tls-engine %d", &ecm->tls_engine))
1174 error = clib_error_return (0, "failed: unknown input `%U'",
1175 format_unformat_error, line_input);
1182 ecm->cfg.num_test_sessions = ecm->expected_connections =
1183 ecm->n_clients * ecm->quic_streams;
1185 if (!ecm->connect_uri)
1187 ec_cli ("No uri provided. Using default: %s", default_uri);
1188 ecm->connect_uri = format (0, "%s%c", default_uri, 0);
1191 if ((rv = parse_uri ((char *) ecm->connect_uri, &ecm->connect_sep)))
1193 error = clib_error_return (0, "Uri parse error: %d", rv);
1196 ecm->transport_proto = ecm->connect_sep.transport_proto;
1197 ecm->is_dgram = (ecm->transport_proto == TRANSPORT_PROTO_UDP);
1199 if (ecm->prealloc_sessions)
1200 ec_prealloc_sessions (ecm);
1202 if ((error = ec_attach ()))
1204 clib_error_report (error);
1208 if (ecm->echo_bytes)
1209 ecm->cfg.test = HS_TEST_TYPE_BI;
1211 ecm->cfg.test = HS_TEST_TYPE_UNI;
1214 ec_wait_for_signal (EC_CLI_CFG_SYNC);
1216 if (ec_ctrl_test_start () < 0)
1218 ec_cli ("failed to send start command");
1221 ec_wait_for_signal (EC_CLI_START);
1224 * Start. Fire off connect requests
1227 /* update data port */
1228 ecm->connect_sep.port = hs_make_data_port (ecm->connect_sep.port);
1230 ecm->syn_start_time = vlib_time_now (vm);
1231 ec_program_connects ();
1234 * Park until the sessions come up, or syn_timeout seconds pass
1237 vlib_process_wait_for_event_or_clock (vm, ecm->syn_timeout);
1238 event_type = vlib_process_get_events (vm, &event_data);
1242 ec_cli ("Timeout with only %d sessions active...",
1243 ecm->ready_connections);
1244 error = clib_error_return (0, "failed: syn timeout with %d sessions",
1245 ecm->ready_connections);
1248 case EC_CLI_CONNECTS_DONE:
1249 delta = vlib_time_now (vm) - ecm->syn_start_time;
1251 ec_cli ("%d three-way handshakes in %.2f seconds %.2f/s",
1252 ecm->n_clients, delta, ((f64) ecm->n_clients) / delta);
1255 case EC_CLI_CONNECTS_FAILED:
1256 error = clib_error_return (0, "failed: connect returned");
1260 ec_cli ("unexpected event(2): %d", event_type);
1262 clib_error_return (0, "failed: unexpected event(2): %d", event_type);
1267 * Wait for the sessions to finish or test_timeout seconds pass
1269 ecm->test_start_time = vlib_time_now (ecm->vlib_main);
1270 ec_cli ("Test started at %.6f", ecm->test_start_time);
1271 vlib_process_wait_for_event_or_clock (vm, ecm->test_timeout);
1272 event_type = vlib_process_get_events (vm, &event_data);
1276 ec_cli ("Timeout at %.6f with %d sessions still active...",
1277 vlib_time_now (ecm->vlib_main), ecm->ready_connections);
1278 error = clib_error_return (0, "failed: timeout with %d sessions",
1279 ecm->ready_connections);
1282 case EC_CLI_TEST_DONE:
1283 ecm->test_end_time = vlib_time_now (vm);
1284 ec_cli ("Test finished at %.6f", ecm->test_end_time);
1288 ec_cli ("unexpected event(3): %d", event_type);
1290 clib_error_return (0, "failed: unexpected event(3): %d", event_type);
1295 * Done. Compute stats
1297 delta = ecm->test_end_time - ecm->test_start_time;
1300 ec_cli ("zero delta-t?");
1301 error = clib_error_return (0, "failed: zero delta-t");
1305 total_bytes = (ecm->echo_bytes ? ecm->rx_total : ecm->tx_total);
1306 transfer_type = ecm->echo_bytes ? "full-duplex" : "half-duplex";
1307 ec_cli ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds", total_bytes,
1308 total_bytes / (1ULL << 20), total_bytes / (1ULL << 30), delta);
1309 ec_cli ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
1311 ec_cli ("%.4f gbit/second %s", (((f64) total_bytes * 8.0) / delta / 1e9),
1314 if (ecm->cfg.test_bytes && ecm->test_failed)
1315 error = clib_error_return (0, "failed: test bytes");
1318 ecm->run_test = EC_EXITING;
1320 /* send stop test command to the server */
1321 if (ec_ctrl_test_stop () < 0)
1323 ec_cli ("failed to send stop command");
1326 ec_wait_for_signal (EC_CLI_STOP);
1328 /* post test sync */
1329 if (ec_ctrl_test_sync () < 0)
1331 ec_cli ("failed to send post sync command");
1334 ec_wait_for_signal (EC_CLI_CFG_SYNC);
1336 /* disconnect control session */
1337 ec_ctrl_session_disconnect ();
1341 ecm->run_test = EC_EXITING;
1342 vlib_process_wait_for_event_or_clock (vm, 10e-3);
1344 /* Detach the application, so we can use different fifo sizes next time */
1347 error = clib_error_return (0, "failed: app detach");
1348 ec_cli ("WARNING: app detach failed...");
1353 unformat_free (line_input);
1356 ec_cli ("test failed");
1361 VLIB_CLI_COMMAND (ec_command, static) = {
1362 .path = "test echo clients",
1364 "test echo clients [nclients %d][[m|g]bytes <bytes>]"
1365 "[test-timeout <time>][syn-timeout <time>][echo-bytes][fifo-size <size>]"
1366 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
1367 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
1368 "[uri <tcp://ip/port>][test-bytes][verbose]",
1369 .function = ec_command_fn,
1374 ec_main_init (vlib_main_t *vm)
1376 ec_main_t *ecm = &ec_main;
1377 ecm->app_is_init = 0;
1381 VLIB_INIT_FUNCTION (ec_main_init);
1384 * fd.io coding-style-patch-verification: ON
1387 * eval: (c-set-style "gnu")