2 * echo_client.c - vpp built-in echo client code
4 * Copyright (c) 2017 by Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
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 <vnet/session-apps/echo_client.h>
23 echo_client_main_t echo_client_main;
25 #define ECHO_CLIENT_DBG (0)
28 signal_evt_to_cli_i (int *code)
30 echo_client_main_t *ecm = &echo_client_main;
31 ASSERT (vlib_get_thread_index () == 0);
32 vlib_process_signal_event (ecm->vlib_main, ecm->cli_node_index, *code, 0);
36 signal_evt_to_cli (int code)
38 if (vlib_get_thread_index () != 0)
39 vl_api_rpc_call_main_thread (signal_evt_to_cli_i, (u8 *) & code,
42 signal_evt_to_cli_i (&code);
46 send_data_chunk (echo_client_main_t * ecm, session_t * s)
48 u8 *test_data = ecm->connect_test_data;
51 session_fifo_event_t evt;
55 ASSERT (vec_len (test_data) > 0);
57 test_buf_offset = s->bytes_sent % vec_len (test_data);
58 bytes_this_chunk = vec_len (test_data) - test_buf_offset;
60 bytes_this_chunk = bytes_this_chunk < s->bytes_to_send
61 ? bytes_this_chunk : s->bytes_to_send;
63 txf = s->server_tx_fifo;
64 rv = svm_fifo_enqueue_nowait (txf, bytes_this_chunk,
65 test_data + test_buf_offset);
67 /* If we managed to enqueue data... */
70 /* Account for it... */
71 s->bytes_to_send -= rv;
77 ELOG_TYPE_DECLARE (e) =
79 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
80 .format_args = "i4i4i4",
87 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
89 ed->data[1] = s->bytes_sent;
90 ed->data[2] = s->bytes_to_send;
93 /* Poke the session layer */
94 if (svm_fifo_set_event (txf))
96 /* Fabricate TX event, send to vpp */
98 evt.event_type = FIFO_EVENT_APP_TX;
101 (ecm->vpp_event_queue[txf->master_thread_index], (u8 *) & evt,
102 0 /* do wait for mutex */ ))
103 clib_warning ("could not enqueue event");
109 receive_data_chunk (echo_client_main_t * ecm, session_t * s)
111 svm_fifo_t *rx_fifo = s->server_rx_fifo;
112 u32 my_thread_index = vlib_get_thread_index ();
117 n_read = svm_fifo_dequeue_nowait (rx_fifo,
120 ecm->rx_buf[my_thread_index]);
124 n_read = svm_fifo_max_dequeue (rx_fifo);
125 svm_fifo_dequeue_drop (rx_fifo, n_read);
133 ELOG_TYPE_DECLARE (e) =
135 .format = "rx-deq: %d bytes",
143 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
144 ed->data[0] = n_read;
149 for (i = 0; i < n_read; i++)
151 if (ecm->rx_buf[my_thread_index][i]
152 != ((s->bytes_received + i) & 0xff))
154 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
155 n_read, s->bytes_received + i,
156 ecm->rx_buf[my_thread_index][i],
157 ((s->bytes_received + i) & 0xff));
158 ecm->test_failed = 1;
162 s->bytes_to_receive -= n_read;
163 s->bytes_received += n_read;
168 echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
169 vlib_frame_t * frame)
171 echo_client_main_t *ecm = &echo_client_main;
172 int my_thread_index = vlib_get_thread_index ();
176 u32 *connection_indices;
177 u32 *connections_this_batch;
178 u32 nconnections_this_batch;
180 connection_indices = ecm->connection_index_by_thread[my_thread_index];
181 connections_this_batch =
182 ecm->connections_this_batch_by_thread[my_thread_index];
184 if ((ecm->run_test == 0) ||
185 ((vec_len (connection_indices) == 0)
186 && vec_len (connections_this_batch) == 0))
189 /* Grab another pile of connections */
190 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
192 nconnections_this_batch =
193 clib_min (ecm->connections_per_batch, vec_len (connection_indices));
195 ASSERT (nconnections_this_batch > 0);
196 vec_validate (connections_this_batch, nconnections_this_batch - 1);
197 clib_memcpy (connections_this_batch,
198 connection_indices + vec_len (connection_indices)
199 - nconnections_this_batch,
200 nconnections_this_batch * sizeof (u32));
201 _vec_len (connection_indices) -= nconnections_this_batch;
204 if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch
205 && ecm->prev_conns == vec_len (connections_this_batch)))
208 ecm->prev_conns = vec_len (connections_this_batch);
209 if (ecm->repeats == 500000)
211 clib_warning ("stuck clients");
216 ecm->prev_conns = vec_len (connections_this_batch);
220 for (i = 0; i < vec_len (connections_this_batch); i++)
224 sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
226 if (sp->bytes_to_send > 0)
228 send_data_chunk (ecm, sp);
231 if (sp->bytes_to_receive > 0)
233 receive_data_chunk (ecm, sp);
236 if (PREDICT_FALSE (delete_session == 1))
238 u32 index, thread_index;
241 __sync_fetch_and_add (&ecm->tx_total, sp->bytes_sent);
242 __sync_fetch_and_add (&ecm->rx_total, sp->bytes_received);
244 session_parse_handle (sp->vpp_session_handle,
245 &index, &thread_index);
246 s = session_get_if_valid (index, thread_index);
250 vnet_disconnect_args_t _a, *a = &_a;
251 a->handle = session_handle (s);
252 a->app_index = ecm->app_index;
253 vnet_disconnect_session (a);
255 vec_delete (connections_this_batch, 1, i);
257 __sync_fetch_and_add (&ecm->ready_connections, -1);
261 clib_warning ("session AWOL?");
262 vec_delete (connections_this_batch, 1, i);
265 /* Kick the debug CLI process */
266 if (ecm->ready_connections == 0)
268 signal_evt_to_cli (2);
273 ecm->connection_index_by_thread[my_thread_index] = connection_indices;
274 ecm->connections_this_batch_by_thread[my_thread_index] =
275 connections_this_batch;
280 VLIB_REGISTER_NODE (echo_clients_node) =
282 .function = echo_client_node_fn,
283 .name = "echo-clients",
284 .type = VLIB_NODE_TYPE_INPUT,
285 .state = VLIB_NODE_STATE_DISABLED,
290 create_api_loopback (echo_client_main_t * ecm)
292 api_main_t *am = &api_main;
293 vl_shmem_hdr_t *shmem_hdr;
295 shmem_hdr = am->shmem_hdr;
296 ecm->vl_input_queue = shmem_hdr->vl_input_queue;
297 ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client",
298 ecm->vl_input_queue);
303 echo_clients_init (vlib_main_t * vm)
305 echo_client_main_t *ecm = &echo_client_main;
306 vlib_thread_main_t *vtm = vlib_get_thread_main ();
310 if (create_api_loopback (ecm))
313 num_threads = 1 /* main thread */ + vtm->n_threads;
315 /* Init test data. Bigecmuffer */
316 vec_validate (ecm->connect_test_data, 1024 * 1024 - 1);
317 for (i = 0; i < vec_len (ecm->connect_test_data); i++)
318 ecm->connect_test_data[i] = i & 0xff;
320 vec_validate (ecm->rx_buf, num_threads - 1);
321 for (i = 0; i < num_threads; i++)
322 vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
326 vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains);
327 vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains);
328 vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains);
334 echo_clients_session_connected_callback (u32 app_index, u32 api_context,
335 stream_session_t * s, u8 is_fail)
337 echo_client_main_t *ecm = &echo_client_main;
340 u8 thread_index = vlib_get_thread_index ();
344 clib_warning ("connection %d failed!", api_context);
345 signal_evt_to_cli (-1);
349 ASSERT (s->thread_index == thread_index);
351 if (!ecm->vpp_event_queue[thread_index])
352 ecm->vpp_event_queue[thread_index] =
353 session_manager_get_vpp_event_queue (thread_index);
358 clib_spinlock_lock_if_init (&ecm->sessions_lock);
359 pool_get (ecm->sessions, session);
360 clib_spinlock_unlock_if_init (&ecm->sessions_lock);
362 memset (session, 0, sizeof (*session));
363 session_index = session - ecm->sessions;
364 session->bytes_to_send = ecm->bytes_to_send;
365 session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
366 session->server_rx_fifo = s->server_rx_fifo;
367 session->server_rx_fifo->client_session_index = session_index;
368 session->server_tx_fifo = s->server_tx_fifo;
369 session->server_tx_fifo->client_session_index = session_index;
370 session->vpp_session_handle = session_handle (s);
372 vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
373 __sync_fetch_and_add (&ecm->ready_connections, 1);
374 if (ecm->ready_connections == ecm->expected_connections)
377 /* Signal the CLI process that the action is starting... */
378 signal_evt_to_cli (1);
385 echo_clients_session_reset_callback (stream_session_t * s)
387 if (s->session_state == SESSION_STATE_READY)
388 clib_warning ("Reset active connection %U", format_stream_session, s, 2);
389 stream_session_cleanup (s);
394 echo_clients_session_create_callback (stream_session_t * s)
400 echo_clients_session_disconnect_callback (stream_session_t * s)
402 echo_client_main_t *ecm = &echo_client_main;
403 vnet_disconnect_args_t _a, *a = &_a;
404 a->handle = session_handle (s);
405 a->app_index = ecm->app_index;
406 vnet_disconnect_session (a);
411 echo_clients_rx_callback (stream_session_t * s)
417 static session_cb_vft_t echo_clients = {
418 .session_reset_callback = echo_clients_session_reset_callback,
419 .session_connected_callback = echo_clients_session_connected_callback,
420 .session_accept_callback = echo_clients_session_create_callback,
421 .session_disconnect_callback = echo_clients_session_disconnect_callback,
422 .builtin_server_rx_callback = echo_clients_rx_callback
426 static clib_error_t *
427 echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
429 u32 prealloc_fifos, segment_size = 256 << 20;
430 echo_client_main_t *ecm = &echo_client_main;
431 vnet_app_attach_args_t _a, *a = &_a;
433 clib_error_t *error = 0;
435 memset (a, 0, sizeof (*a));
436 memset (options, 0, sizeof (options));
438 a->api_client_index = ecm->my_client_index;
439 a->session_cb_vft = &echo_clients;
441 prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
443 if (ecm->private_segment_size)
444 segment_size = ecm->private_segment_size;
446 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
447 options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
448 options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
449 options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
450 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
451 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
453 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
456 options[APP_OPTIONS_FLAGS] |= appns_flags;
457 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
459 a->options = options;
460 a->namespace_id = appns_id;
462 if ((error = vnet_application_attach (a)))
465 ecm->app_index = a->app_index;
470 echo_clients_detach ()
472 echo_client_main_t *ecm = &echo_client_main;
473 vnet_app_detach_args_t _da, *da = &_da;
476 da->app_index = ecm->app_index;
477 rv = vnet_application_detach (da);
478 ecm->test_client_attached = 0;
484 echo_client_thread_fn (void *arg)
489 /** Start a transmit thread */
491 echo_clients_start_tx_pthread (echo_client_main_t * ecm)
493 if (ecm->client_thread_handle == 0)
495 int rv = pthread_create (&ecm->client_thread_handle,
497 echo_client_thread_fn, 0);
500 ecm->client_thread_handle = 0;
508 echo_clients_connect (vlib_main_t * vm, u32 n_clients)
510 echo_client_main_t *ecm = &echo_client_main;
511 vnet_connect_args_t _a, *a = &_a;
512 clib_error_t *error = 0;
514 for (i = 0; i < n_clients; i++)
516 memset (a, 0, sizeof (*a));
518 a->uri = (char *) ecm->connect_uri;
520 a->app_index = ecm->app_index;
523 if ((error = vnet_connect_uri (a)))
526 /* Crude pacing for call setups */
528 vlib_process_suspend (vm, 10e-6);
529 ASSERT (i + 1 >= ecm->ready_connections);
530 while (i + 1 - ecm->ready_connections > 1000)
532 vlib_process_suspend (vm, 100e-6);
538 #define ec_cli_output(_fmt, _args...) \
539 if (!ecm->no_output) \
540 vlib_cli_output(vm, _fmt, ##_args)
542 static clib_error_t *
543 echo_clients_command_fn (vlib_main_t * vm,
544 unformat_input_t * input, vlib_cli_command_t * cmd)
546 echo_client_main_t *ecm = &echo_client_main;
547 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
548 u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
549 f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
550 char *default_uri = "tcp://6.0.1.1/1234";
551 uword *event_data = 0, event_type;
552 f64 time_before_connects;
554 int preallocate_sessions = 0;
556 clib_error_t *error = 0;
560 ecm->bytes_to_send = 8192;
562 ecm->fifo_size = 64 << 10;
563 ecm->connections_per_batch = 1000;
564 ecm->private_segment_count = 0;
565 ecm->private_segment_size = 0;
568 ecm->test_failed = 0;
570 if (thread_main->n_vlib_mains > 1)
571 clib_spinlock_init (&ecm->sessions_lock);
572 vec_free (ecm->connect_uri);
574 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
576 if (unformat (input, "uri %s", &ecm->connect_uri))
578 else if (unformat (input, "nclients %d", &n_clients))
580 else if (unformat (input, "mbytes %lld", &tmp))
581 ecm->bytes_to_send = tmp << 20;
582 else if (unformat (input, "gbytes %lld", &tmp))
583 ecm->bytes_to_send = tmp << 30;
584 else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
586 else if (unformat (input, "test-timeout %f", &test_timeout))
588 else if (unformat (input, "syn-timeout %f", &syn_timeout))
590 else if (unformat (input, "no-return"))
592 else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
593 ecm->fifo_size <<= 10;
594 else if (unformat (input, "private-segment-count %d",
595 &ecm->private_segment_count))
597 else if (unformat (input, "private-segment-size %U",
598 unformat_memory_size, &tmp))
600 if (tmp >= 0x100000000ULL)
601 return clib_error_return
602 (0, "private segment size %lld (%llu) too large", tmp, tmp);
603 ecm->private_segment_size = tmp;
605 else if (unformat (input, "preallocate-fifos"))
606 ecm->prealloc_fifos = 1;
607 else if (unformat (input, "preallocate-sessions"))
608 preallocate_sessions = 1;
610 if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
612 else if (unformat (input, "appns %_%v%_", &appns_id))
614 else if (unformat (input, "all-scope"))
615 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
616 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
617 else if (unformat (input, "local-scope"))
618 appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
619 else if (unformat (input, "global-scope"))
620 appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
621 else if (unformat (input, "secret %lu", &appns_secret))
623 else if (unformat (input, "no-output"))
625 else if (unformat (input, "test-bytes"))
628 return clib_error_return (0, "unknown input `%U'",
629 format_unformat_error, input);
632 /* Store cli process node index for signalling */
633 ecm->cli_node_index =
634 vlib_get_current_process (vm)->node_runtime.node_index;
636 if (ecm->is_init == 0)
638 if (echo_clients_init (vm))
639 return clib_error_return (0, "failed init");
643 ecm->ready_connections = 0;
644 ecm->expected_connections = n_clients;
648 if (!ecm->connect_uri)
650 clib_warning ("No uri provided. Using default: %s", default_uri);
651 ecm->connect_uri = format (0, "%s%c", default_uri, 0);
654 #if ECHO_CLIENT_PTHREAD
655 echo_clients_start_tx_pthread ();
658 vlib_worker_thread_barrier_sync (vm);
659 vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
660 vlib_worker_thread_barrier_release (vm);
662 if (ecm->test_client_attached == 0)
664 if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
667 clib_error_report (error);
672 ecm->test_client_attached = 1;
674 /* Turn on the builtin client input nodes */
675 for (i = 0; i < thread_main->n_vlib_mains; i++)
676 vlib_node_set_state (vlib_mains[i], echo_clients_node.index,
677 VLIB_NODE_STATE_POLLING);
679 if (preallocate_sessions)
681 session_t *sp __attribute__ ((unused));
682 for (i = 0; i < n_clients; i++)
683 pool_get (ecm->sessions, sp);
684 for (i = 0; i < n_clients; i++)
685 pool_put_index (ecm->sessions, i);
688 /* Fire off connect requests */
689 time_before_connects = vlib_time_now (vm);
690 if ((error = echo_clients_connect (vm, n_clients)))
693 /* Park until the sessions come up, or ten seconds elapse... */
694 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
695 event_type = vlib_process_get_events (vm, &event_data);
699 ec_cli_output ("Timeout with only %d sessions active...",
700 ecm->ready_connections);
701 error = clib_error_return (0, "failed: syn timeout with %d sessions",
702 ecm->ready_connections);
706 delta = vlib_time_now (vm) - time_before_connects;
708 ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
709 n_clients, delta, ((f64) n_clients) / delta);
711 ecm->test_start_time = vlib_time_now (ecm->vlib_main);
712 ec_cli_output ("Test started at %.6f", ecm->test_start_time);
716 ec_cli_output ("unexpected event(1): %d", event_type);
717 error = clib_error_return (0, "failed: unexpected event(1): %d",
722 /* Now wait for the sessions to finish... */
723 vlib_process_wait_for_event_or_clock (vm, test_timeout);
724 event_type = vlib_process_get_events (vm, &event_data);
728 ec_cli_output ("Timeout with %d sessions still active...",
729 ecm->ready_connections);
730 error = clib_error_return (0, "failed: timeout with %d sessions",
731 ecm->ready_connections);
735 ecm->test_end_time = vlib_time_now (vm);
736 ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
740 ec_cli_output ("unexpected event(2): %d", event_type);
741 error = clib_error_return (0, "failed: unexpected event(2): %d",
746 delta = ecm->test_end_time - ecm->test_start_time;
749 total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
750 transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
751 ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
752 total_bytes, total_bytes / (1ULL << 20),
753 total_bytes / (1ULL << 30), delta);
754 ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
756 ec_cli_output ("%.4f gbit/second %s",
757 (((f64) total_bytes * 8.0) / delta / 1e9),
762 ec_cli_output ("zero delta-t?");
763 error = clib_error_return (0, "failed: zero delta-t");
767 if (ecm->test_bytes && ecm->test_failed)
768 error = clib_error_return (0, "failed: test bytes");
772 for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
774 vec_reset_length (ecm->connection_index_by_thread[i]);
775 vec_reset_length (ecm->connections_this_batch_by_thread[i]);
778 pool_free (ecm->sessions);
780 /* Detach the application, so we can use different fifo sizes next time */
781 if (ecm->test_client_attached)
783 if (echo_clients_detach ())
785 error = clib_error_return (0, "failed: app detach");
786 ec_cli_output ("WARNING: app detach failed...");
790 ec_cli_output ("test failed");
791 vec_free (ecm->connect_uri);
796 VLIB_CLI_COMMAND (echo_clients_command, static) =
798 .path = "test echo clients",
799 .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
800 "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
801 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
802 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
803 "[uri <tcp://ip/port>][test-bytes][no-output]",
804 .function = echo_clients_command_fn,
810 echo_clients_main_init (vlib_main_t * vm)
812 echo_client_main_t *ecm = &echo_client_main;
817 VLIB_INIT_FUNCTION (echo_clients_main_init);
820 * fd.io coding-style-patch-verification: ON
823 * eval: (c-set-style "gnu")