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);
534 ec_dbg ("sending test paramters to the server..");
535 if (ecm->cfg.verbose)
536 hs_test_cfg_dump (&ecm->cfg, 1);
538 rv = svm_fifo_enqueue (s->tx_fifo, sizeof (ecm->cfg), (u8 *) &ecm->cfg);
539 ASSERT (rv == sizeof (ecm->cfg));
540 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
545 ec_ctrl_session_connected_callback (session_t *s)
547 ec_main_t *ecm = &ec_main;
549 s->opaque = HS_CTRL_HANDLE;
550 ecm->ctrl_session_handle = session_handle (s);
552 /* send test parameters to the server */
553 ec_ctrl_send (HS_TEST_CMD_SYNC);
558 quic_ec_session_connected_callback (u32 app_index, u32 api_context,
559 session_t *s, session_error_t err)
561 ec_main_t *ecm = &ec_main;
566 if (PREDICT_FALSE (api_context == HS_CTRL_HANDLE))
567 return ec_ctrl_session_connected_callback (s);
569 if (PREDICT_FALSE (ecm->run_test != EC_STARTING))
574 ec_err ("connection %d failed!", api_context);
575 ecm->run_test = EC_EXITING;
576 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
580 if (s->listener_handle == SESSION_INVALID_HANDLE)
581 return quic_ec_qsession_connected_callback (app_index, api_context, s,
583 ec_dbg ("STREAM Connection callback %d", api_context);
585 thread_index = s->thread_index;
586 ASSERT (thread_index == vlib_get_thread_index ()
587 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
589 wrk = ec_worker_get (thread_index);
594 es = ec_session_alloc (wrk);
596 es->bytes_to_send = ecm->bytes_to_send;
597 es->bytes_to_receive = ecm->echo_bytes ? ecm->bytes_to_send : 0ULL;
598 es->data.rx_fifo = s->rx_fifo;
599 es->data.rx_fifo->shr->client_session_index = es->data.session_index;
600 es->data.tx_fifo = s->tx_fifo;
601 es->data.tx_fifo->shr->client_session_index = es->data.session_index;
602 es->data.vpp_evt_q = wrk->vpp_event_queue;
603 es->vpp_session_handle = session_handle (s);
604 es->vpp_session_index = s->session_index;
605 s->opaque = es->data.session_index;
609 transport_connection_t *tc;
610 tc = session_get_transport (s);
611 clib_memcpy_fast (&es->data.transport, tc, sizeof (es->data.transport));
612 es->data.is_dgram = 1;
615 vec_add1 (wrk->conn_indices, es->data.session_index);
616 clib_atomic_fetch_add (&ecm->ready_connections, 1);
617 if (ecm->ready_connections == ecm->expected_connections)
619 ecm->run_test = EC_RUNNING;
620 /* Signal the CLI process that the action is starting... */
621 signal_evt_to_cli (EC_CLI_CONNECTS_DONE);
628 ec_session_connected_callback (u32 app_index, u32 api_context, session_t *s,
631 ec_main_t *ecm = &ec_main;
636 if (PREDICT_FALSE (ecm->run_test != EC_STARTING))
641 ec_err ("connection %d failed! %U", api_context, format_session_error,
643 ecm->run_test = EC_EXITING;
644 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
648 thread_index = s->thread_index;
649 ASSERT (thread_index == vlib_get_thread_index ()
650 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
652 if (PREDICT_FALSE (api_context == HS_CTRL_HANDLE))
653 return ec_ctrl_session_connected_callback (s);
655 wrk = ec_worker_get (thread_index);
660 es = ec_session_alloc (wrk);
662 es->bytes_to_send = ecm->bytes_to_send;
663 es->bytes_to_receive = ecm->echo_bytes ? ecm->bytes_to_send : 0ULL;
664 es->data.rx_fifo = s->rx_fifo;
665 es->data.rx_fifo->shr->client_session_index = es->data.session_index;
666 es->data.tx_fifo = s->tx_fifo;
667 es->data.tx_fifo->shr->client_session_index = es->data.session_index;
668 es->data.vpp_evt_q = wrk->vpp_event_queue;
669 es->vpp_session_handle = session_handle (s);
670 es->vpp_session_index = s->session_index;
671 s->opaque = es->data.session_index;
675 transport_connection_t *tc;
676 tc = session_get_transport (s);
677 clib_memcpy_fast (&es->data.transport, tc, sizeof (es->data.transport));
678 es->data.is_dgram = 1;
681 vec_add1 (wrk->conn_indices, es->data.session_index);
682 clib_atomic_fetch_add (&ecm->ready_connections, 1);
683 if (ecm->ready_connections == ecm->expected_connections)
685 ecm->run_test = EC_RUNNING;
686 /* Signal the CLI process that the action is starting... */
687 signal_evt_to_cli (EC_CLI_CONNECTS_DONE);
694 ec_session_reset_callback (session_t *s)
696 ec_main_t *ecm = &ec_main;
697 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
699 if (s->session_state == SESSION_STATE_READY)
700 ec_err ("Reset active connection %U", format_session, s, 2);
702 a->handle = session_handle (s);
703 a->app_index = ecm->app_index;
704 vnet_disconnect_session (a);
709 ec_session_accept_callback (session_t *s)
715 ec_session_disconnect_callback (session_t *s)
717 ec_main_t *ecm = &ec_main;
718 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
720 if (session_handle (s) == ecm->ctrl_session_handle)
722 ec_dbg ("ctrl session disconnect");
723 ecm->ctrl_session_handle = SESSION_INVALID_HANDLE;
726 a->handle = session_handle (s);
727 a->app_index = ecm->app_index;
728 vnet_disconnect_session (a);
733 ec_session_disconnect (session_t *s)
735 ec_main_t *ecm = &ec_main;
736 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
737 a->handle = session_handle (s);
738 a->app_index = ecm->app_index;
739 vnet_disconnect_session (a);
743 ec_ctrl_session_rx_callback (session_t *s)
745 ec_main_t *ecm = &ec_main;
747 hs_test_cfg_t cfg = { 0 };
749 rx_bytes = svm_fifo_dequeue (s->rx_fifo, sizeof (cfg), (u8 *) &cfg);
750 if (rx_bytes != sizeof (cfg))
752 ec_err ("invalid cfg length %d (expected %d)", rx_bytes, sizeof (cfg));
753 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
757 ec_dbg ("control message received:");
758 if (ecm->cfg.verbose)
759 hs_test_cfg_dump (&cfg, 1);
763 case HS_TEST_CMD_SYNC:
764 switch (ecm->run_test)
767 if (!hs_test_cfg_verify (&cfg, &ecm->cfg))
769 ec_err ("invalid config received from server!");
770 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
773 signal_evt_to_cli (EC_CLI_CFG_SYNC);
777 ec_dbg ("test running..");
782 signal_evt_to_cli (EC_CLI_CFG_SYNC);
786 ec_err ("unexpected test state! %d", ecm->run_test);
790 case HS_TEST_CMD_START:
791 signal_evt_to_cli (EC_CLI_START);
793 case HS_TEST_CMD_STOP:
794 signal_evt_to_cli (EC_CLI_STOP);
797 ec_err ("unexpected cmd! %d", cfg.cmd);
805 ec_session_rx_callback (session_t *s)
807 ec_main_t *ecm = &ec_main;
811 if (PREDICT_FALSE (s->opaque == HS_CTRL_HANDLE))
812 return ec_ctrl_session_rx_callback (s);
814 if (PREDICT_FALSE (ecm->run_test != EC_RUNNING))
816 ec_session_disconnect (s);
820 wrk = ec_worker_get (s->thread_index);
821 es = ec_session_get (wrk, s->opaque);
823 receive_data_chunk (wrk, es);
825 if (svm_fifo_max_dequeue_cons (s->rx_fifo))
826 session_enqueue_notify (s);
832 ec_add_segment_callback (u32 app_index, u64 segment_handle)
834 /* New segments may be added */
839 ec_del_segment_callback (u32 app_index, u64 segment_handle)
844 static session_cb_vft_t ec_cb_vft = {
845 .session_reset_callback = ec_session_reset_callback,
846 .session_connected_callback = ec_session_connected_callback,
847 .session_accept_callback = ec_session_accept_callback,
848 .session_disconnect_callback = ec_session_disconnect_callback,
849 .builtin_app_rx_callback = ec_session_rx_callback,
850 .add_segment_callback = ec_add_segment_callback,
851 .del_segment_callback = ec_del_segment_callback,
854 static clib_error_t *
857 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
858 ec_main_t *ecm = &ec_main;
859 vnet_app_attach_args_t _a, *a = &_a;
864 clib_memset (a, 0, sizeof (*a));
865 clib_memset (options, 0, sizeof (options));
867 a->api_client_index = ~0;
868 a->name = format (0, "echo_client");
869 if (ecm->transport_proto == TRANSPORT_PROTO_QUIC)
870 ec_cb_vft.session_connected_callback = quic_ec_session_connected_callback;
871 a->session_cb_vft = &ec_cb_vft;
873 prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
875 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
876 options[APP_OPTIONS_SEGMENT_SIZE] = ecm->private_segment_size;
877 options[APP_OPTIONS_ADD_SEGMENT_SIZE] = ecm->private_segment_size;
878 options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
879 options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
880 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
881 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
882 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
883 options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
884 options[APP_OPTIONS_PCT_FIRST_ALLOC] = 100;
885 options[APP_OPTIONS_FLAGS] |= ecm->attach_flags;
888 options[APP_OPTIONS_NAMESPACE_SECRET] = ecm->appns_secret;
889 a->namespace_id = ecm->appns_id;
891 a->options = options;
893 if ((rv = vnet_application_attach (a)))
894 return clib_error_return (0, "attach returned %d", rv);
896 ecm->app_index = a->app_index;
899 clib_memset (ck_pair, 0, sizeof (*ck_pair));
900 ck_pair->cert = (u8 *) test_srv_crt_rsa;
901 ck_pair->key = (u8 *) test_srv_key_rsa;
902 ck_pair->cert_len = test_srv_crt_rsa_len;
903 ck_pair->key_len = test_srv_key_rsa_len;
904 vnet_app_add_cert_key_pair (ck_pair);
905 ecm->ckpair_index = ck_pair->index;
907 ecm->test_client_attached = 1;
915 ec_main_t *ecm = &ec_main;
916 vnet_app_detach_args_t _da, *da = &_da;
919 if (!ecm->test_client_attached)
922 da->app_index = ecm->app_index;
923 da->api_client_index = ~0;
924 rv = vnet_application_detach (da);
925 ecm->test_client_attached = 0;
927 vnet_app_del_cert_key_pair (ecm->ckpair_index);
933 ec_transport_needs_crypto (transport_proto_t proto)
935 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
936 proto == TRANSPORT_PROTO_QUIC;
940 ec_connect_rpc (void *args)
942 ec_main_t *ecm = &ec_main;
943 vnet_connect_args_t _a = {}, *a = &_a;
944 int rv, needs_crypto;
947 n_clients = ecm->n_clients;
948 needs_crypto = ec_transport_needs_crypto (ecm->transport_proto);
949 clib_memcpy (&a->sep_ext, &ecm->connect_sep, sizeof (ecm->connect_sep));
950 a->sep_ext.transport_flags |= TRANSPORT_CFG_F_CONNECTED;
951 a->app_index = ecm->app_index;
953 ci = ecm->connect_conn_index;
955 while (ci < n_clients)
957 /* Crude pacing for call setups */
958 if (ci - ecm->ready_connections > 128)
960 ecm->connect_conn_index = ci;
967 session_endpoint_alloc_ext_cfg (&a->sep_ext,
968 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
969 a->sep_ext.ext_cfg->crypto.ckpair_index = ecm->ckpair_index;
972 rv = vnet_connect (a);
975 clib_mem_free (a->sep_ext.ext_cfg);
979 ec_err ("connect returned: %U", format_session_error, rv);
980 ecm->run_test = EC_EXITING;
981 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
988 if (ci < ecm->expected_connections && ecm->run_test != EC_EXITING)
989 ec_program_connects ();
995 ec_program_connects (void)
997 session_send_rpc_evt_to_thread_force (transport_cl_thread (), ec_connect_rpc,
1001 static clib_error_t *
1002 ec_ctrl_connect_rpc ()
1005 ec_main_t *ecm = &ec_main;
1006 vnet_connect_args_t _a = {}, *a = &_a;
1008 a->api_context = HS_CTRL_HANDLE;
1009 ecm->cfg.cmd = HS_TEST_CMD_SYNC;
1010 clib_memcpy (&a->sep_ext, &ecm->connect_sep, sizeof (ecm->connect_sep));
1011 a->sep_ext.transport_proto = TRANSPORT_PROTO_TCP;
1012 a->app_index = ecm->app_index;
1014 rv = vnet_connect (a);
1017 ec_err ("ctrl connect returned: %U", format_session_error, rv);
1018 ecm->run_test = EC_EXITING;
1019 signal_evt_to_cli (EC_CLI_CONNECTS_FAILED);
1025 ec_ctrl_connect (void)
1027 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
1028 ec_ctrl_connect_rpc, 0);
1032 ec_ctrl_session_disconnect ()
1034 ec_main_t *ecm = &ec_main;
1035 vnet_disconnect_args_t _a, *a = &_a;
1036 session_error_t err;
1038 a->handle = ecm->ctrl_session_handle;
1039 a->app_index = ecm->app_index;
1040 err = vnet_disconnect_session (a);
1042 ec_err ("vnet_disconnect_session: %U", format_session_error, err);
1046 ec_ctrl_test_sync ()
1048 ec_main_t *ecm = &ec_main;
1049 ecm->cfg.test = HS_TEST_TYPE_ECHO;
1050 return ec_ctrl_send (HS_TEST_CMD_SYNC);
1054 ec_ctrl_test_start ()
1056 return ec_ctrl_send (HS_TEST_CMD_START);
1060 ec_ctrl_test_stop ()
1062 return ec_ctrl_send (HS_TEST_CMD_STOP);
1065 #define ec_wait_for_signal(_sig) \
1066 vlib_process_wait_for_event_or_clock (vm, ecm->syn_timeout); \
1067 event_type = vlib_process_get_events (vm, &event_data); \
1068 switch (event_type) \
1071 ec_cli ("Timeout while waiting for " #_sig); \
1073 clib_error_return (0, "failed: timeout while waiting for " #_sig); \
1078 ec_cli ("unexpected event while waiting for " #_sig ": %d", \
1081 clib_error_return (0, "failed: unexpected event: %d", event_type); \
1085 static clib_error_t *
1086 ec_command_fn (vlib_main_t *vm, unformat_input_t *input,
1087 vlib_cli_command_t *cmd)
1089 unformat_input_t _line_input, *line_input = &_line_input;
1090 char *default_uri = "tcp://6.0.1.1/1234", *transfer_type;
1091 ec_main_t *ecm = &ec_main;
1092 uword *event_data = 0, event_type;
1093 clib_error_t *error = 0;
1094 int rv, had_config = 1;
1095 u64 tmp, total_bytes;
1098 if (ecm->test_client_attached)
1099 return clib_error_return (0, "failed: already running!");
1103 error = clib_error_return (0, "failed init");
1107 if (!unformat_user (input, unformat_line_input, line_input))
1113 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1115 if (unformat (line_input, "uri %s", &ecm->connect_uri))
1117 else if (unformat (line_input, "nclients %d", &ecm->n_clients))
1119 else if (unformat (line_input, "quic-streams %d", &ecm->quic_streams))
1121 else if (unformat (line_input, "mbytes %lld", &tmp))
1122 ecm->bytes_to_send = tmp << 20;
1123 else if (unformat (line_input, "gbytes %lld", &tmp))
1124 ecm->bytes_to_send = tmp << 30;
1125 else if (unformat (line_input, "bytes %U", unformat_memory_size,
1126 &ecm->bytes_to_send))
1128 else if (unformat (line_input, "test-timeout %f", &ecm->test_timeout))
1130 else if (unformat (line_input, "syn-timeout %f", &ecm->syn_timeout))
1132 else if (unformat (line_input, "echo-bytes"))
1133 ecm->echo_bytes = 1;
1134 else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
1137 else if (unformat (line_input, "private-segment-count %d",
1138 &ecm->private_segment_count))
1140 else if (unformat (line_input, "private-segment-size %U",
1141 unformat_memory_size, &ecm->private_segment_size))
1143 else if (unformat (line_input, "preallocate-fifos"))
1144 ecm->prealloc_fifos = 1;
1145 else if (unformat (line_input, "preallocate-sessions"))
1146 ecm->prealloc_sessions = 1;
1147 else if (unformat (line_input, "client-batch %d",
1148 &ecm->connections_per_batch))
1150 else if (unformat (line_input, "appns %_%v%_", &ecm->appns_id))
1152 else if (unformat (line_input, "all-scope"))
1153 ecm->attach_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE |
1154 APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
1155 else if (unformat (line_input, "local-scope"))
1156 ecm->attach_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1157 else if (unformat (line_input, "global-scope"))
1158 ecm->attach_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1159 else if (unformat (line_input, "secret %lu", &ecm->appns_secret))
1161 else if (unformat (line_input, "verbose"))
1162 ecm->cfg.verbose = 1;
1163 else if (unformat (line_input, "test-bytes"))
1164 ecm->cfg.test_bytes = 1;
1165 else if (unformat (line_input, "tls-engine %d", &ecm->tls_engine))
1169 error = clib_error_return (0, "failed: unknown input `%U'",
1170 format_unformat_error, line_input);
1177 ecm->cfg.num_test_sessions = ecm->expected_connections =
1178 ecm->n_clients * ecm->quic_streams;
1180 if (!ecm->connect_uri)
1182 ec_cli ("No uri provided. Using default: %s", default_uri);
1183 ecm->connect_uri = format (0, "%s%c", default_uri, 0);
1186 if ((rv = parse_uri ((char *) ecm->connect_uri, &ecm->connect_sep)))
1188 error = clib_error_return (0, "Uri parse error: %d", rv);
1191 ecm->transport_proto = ecm->connect_sep.transport_proto;
1192 ecm->is_dgram = (ecm->transport_proto == TRANSPORT_PROTO_UDP);
1194 if (ecm->prealloc_sessions)
1195 ec_prealloc_sessions (ecm);
1197 if ((error = ec_attach ()))
1199 clib_error_report (error);
1203 if (ecm->echo_bytes)
1204 ecm->cfg.test = HS_TEST_TYPE_BI;
1206 ecm->cfg.test = HS_TEST_TYPE_UNI;
1209 ec_wait_for_signal (EC_CLI_CFG_SYNC);
1211 if (ec_ctrl_test_start () < 0)
1213 ec_cli ("failed to send start command");
1216 ec_wait_for_signal (EC_CLI_START);
1219 * Start. Fire off connect requests
1222 /* update data port */
1223 ecm->connect_sep.port = hs_make_data_port (ecm->connect_sep.port);
1225 ecm->syn_start_time = vlib_time_now (vm);
1226 ec_program_connects ();
1229 * Park until the sessions come up, or syn_timeout seconds pass
1232 vlib_process_wait_for_event_or_clock (vm, ecm->syn_timeout);
1233 event_type = vlib_process_get_events (vm, &event_data);
1237 ec_cli ("Timeout with only %d sessions active...",
1238 ecm->ready_connections);
1239 error = clib_error_return (0, "failed: syn timeout with %d sessions",
1240 ecm->ready_connections);
1243 case EC_CLI_CONNECTS_DONE:
1244 delta = vlib_time_now (vm) - ecm->syn_start_time;
1246 ec_cli ("%d three-way handshakes in %.2f seconds %.2f/s",
1247 ecm->n_clients, delta, ((f64) ecm->n_clients) / delta);
1250 case EC_CLI_CONNECTS_FAILED:
1251 error = clib_error_return (0, "failed: connect returned");
1255 ec_cli ("unexpected event(2): %d", event_type);
1257 clib_error_return (0, "failed: unexpected event(2): %d", event_type);
1262 * Wait for the sessions to finish or test_timeout seconds pass
1264 ecm->test_start_time = vlib_time_now (ecm->vlib_main);
1265 ec_cli ("Test started at %.6f", ecm->test_start_time);
1266 vlib_process_wait_for_event_or_clock (vm, ecm->test_timeout);
1267 event_type = vlib_process_get_events (vm, &event_data);
1271 ec_cli ("Timeout at %.6f with %d sessions still active...",
1272 vlib_time_now (ecm->vlib_main), ecm->ready_connections);
1273 error = clib_error_return (0, "failed: timeout with %d sessions",
1274 ecm->ready_connections);
1277 case EC_CLI_TEST_DONE:
1278 ecm->test_end_time = vlib_time_now (vm);
1279 ec_cli ("Test finished at %.6f", ecm->test_end_time);
1283 ec_cli ("unexpected event(3): %d", event_type);
1285 clib_error_return (0, "failed: unexpected event(3): %d", event_type);
1290 * Done. Compute stats
1292 delta = ecm->test_end_time - ecm->test_start_time;
1295 ec_cli ("zero delta-t?");
1296 error = clib_error_return (0, "failed: zero delta-t");
1300 total_bytes = (ecm->echo_bytes ? ecm->rx_total : ecm->tx_total);
1301 transfer_type = ecm->echo_bytes ? "full-duplex" : "half-duplex";
1302 ec_cli ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds", total_bytes,
1303 total_bytes / (1ULL << 20), total_bytes / (1ULL << 30), delta);
1304 ec_cli ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
1306 ec_cli ("%.4f gbit/second %s", (((f64) total_bytes * 8.0) / delta / 1e9),
1309 if (ecm->cfg.test_bytes && ecm->test_failed)
1310 error = clib_error_return (0, "failed: test bytes");
1313 ecm->run_test = EC_EXITING;
1315 /* send stop test command to the server */
1316 if (ec_ctrl_test_stop () < 0)
1318 ec_cli ("failed to send stop command");
1321 ec_wait_for_signal (EC_CLI_STOP);
1323 /* post test sync */
1324 if (ec_ctrl_test_sync () < 0)
1326 ec_cli ("failed to send post sync command");
1329 ec_wait_for_signal (EC_CLI_CFG_SYNC);
1331 /* disconnect control session */
1332 ec_ctrl_session_disconnect ();
1336 ecm->run_test = EC_EXITING;
1337 vlib_process_wait_for_event_or_clock (vm, 10e-3);
1339 /* Detach the application, so we can use different fifo sizes next time */
1342 error = clib_error_return (0, "failed: app detach");
1343 ec_cli ("WARNING: app detach failed...");
1348 unformat_free (line_input);
1351 ec_cli ("test failed");
1356 VLIB_CLI_COMMAND (ec_command, static) = {
1357 .path = "test echo clients",
1359 "test echo clients [nclients %d][[m|g]bytes <bytes>]"
1360 "[test-timeout <time>][syn-timeout <time>][echo-bytes][fifo-size <size>]"
1361 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
1362 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
1363 "[uri <tcp://ip/port>][test-bytes][verbose]",
1364 .function = ec_command_fn,
1369 ec_main_init (vlib_main_t *vm)
1371 ec_main_t *ecm = &ec_main;
1372 ecm->app_is_init = 0;
1376 VLIB_INIT_FUNCTION (ec_main_init);
1379 * fd.io coding-style-patch-verification: ON
1382 * eval: (c-set-style "gnu")