2 * builtin_client.c - vpp built-in tcp client/connect 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 <vnet/plugin/plugin.h>
20 #include <vnet/tcp/builtin_client.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
26 tclient_main_t tclient_main;
28 #define TCP_BUILTIN_CLIENT_DBG (0)
31 signal_evt_to_cli_i (int *code)
33 tclient_main_t *tm = &tclient_main;
34 ASSERT (vlib_get_thread_index () == 0);
35 vlib_process_signal_event (tm->vlib_main, tm->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_test_chunk (tclient_main_t * tm, session_t * s)
51 u8 *test_data = tm->connect_test_data;
54 session_fifo_event_t evt;
58 ASSERT (vec_len (test_data) > 0);
60 test_buf_offset = s->bytes_sent % vec_len (test_data);
61 bytes_this_chunk = vec_len (test_data) - test_buf_offset;
63 bytes_this_chunk = bytes_this_chunk < s->bytes_to_send
64 ? bytes_this_chunk : s->bytes_to_send;
66 txf = s->server_tx_fifo;
67 rv = svm_fifo_enqueue_nowait (txf, bytes_this_chunk,
68 test_data + test_buf_offset);
70 /* If we managed to enqueue data... */
73 /* Account for it... */
74 s->bytes_to_send -= rv;
77 if (TCP_BUILTIN_CLIENT_DBG)
80 ELOG_TYPE_DECLARE (e) =
82 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
83 .format_args = "i4i4i4",
90 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
92 ed->data[1] = s->bytes_sent;
93 ed->data[2] = s->bytes_to_send;
96 /* Poke the session layer */
97 if (svm_fifo_set_event (txf))
99 /* Fabricate TX event, send to vpp */
101 evt.event_type = FIFO_EVENT_APP_TX;
104 (tm->vpp_event_queue[txf->master_thread_index], (u8 *) & evt,
105 0 /* do wait for mutex */ ))
106 clib_warning ("could not enqueue event");
112 receive_test_chunk (tclient_main_t * tm, session_t * s)
114 svm_fifo_t *rx_fifo = s->server_rx_fifo;
115 u32 my_thread_index = vlib_get_thread_index ();
118 /* Allow enqueuing of new event */
119 // svm_fifo_unset_event (rx_fifo);
123 n_read = svm_fifo_dequeue_nowait (rx_fifo,
124 vec_len (tm->rx_buf[my_thread_index]),
125 tm->rx_buf[my_thread_index]);
129 n_read = svm_fifo_max_dequeue (rx_fifo);
130 svm_fifo_dequeue_drop (rx_fifo, n_read);
135 if (TCP_BUILTIN_CLIENT_DBG)
138 ELOG_TYPE_DECLARE (e) =
140 .format = "rx-deq: %d bytes",
148 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
149 ed->data[0] = n_read;
154 for (i = 0; i < n_read; i++)
156 if (tm->rx_buf[my_thread_index][i]
157 != ((s->bytes_received + i) & 0xff))
159 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
160 n_read, s->bytes_received + i,
161 tm->rx_buf[my_thread_index][i],
162 ((s->bytes_received + i) & 0xff));
167 s->bytes_to_receive -= n_read;
168 s->bytes_received += n_read;
173 builtin_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
174 vlib_frame_t * frame)
176 tclient_main_t *tm = &tclient_main;
177 int my_thread_index = vlib_get_thread_index ();
181 u32 *connection_indices;
182 u32 *connections_this_batch;
183 u32 nconnections_this_batch;
185 connection_indices = tm->connection_index_by_thread[my_thread_index];
186 connections_this_batch =
187 tm->connections_this_batch_by_thread[my_thread_index];
189 if ((tm->run_test == 0) ||
190 ((vec_len (connection_indices) == 0)
191 && vec_len (connections_this_batch) == 0))
194 /* Grab another pile of connections */
195 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
197 nconnections_this_batch =
198 clib_min (tm->connections_per_batch, vec_len (connection_indices));
200 ASSERT (nconnections_this_batch > 0);
201 vec_validate (connections_this_batch, nconnections_this_batch - 1);
202 clib_memcpy (connections_this_batch,
203 connection_indices + vec_len (connection_indices)
204 - nconnections_this_batch,
205 nconnections_this_batch * sizeof (u32));
206 _vec_len (connection_indices) -= nconnections_this_batch;
209 if (PREDICT_FALSE (tm->prev_conns != tm->connections_per_batch
210 && tm->prev_conns == vec_len (connections_this_batch)))
213 tm->prev_conns = vec_len (connections_this_batch);
214 if (tm->repeats == 500000)
216 clib_warning ("stuck clients");
221 tm->prev_conns = vec_len (connections_this_batch);
225 for (i = 0; i < vec_len (connections_this_batch); i++)
229 sp = pool_elt_at_index (tm->sessions, connections_this_batch[i]);
231 if (sp->bytes_to_send > 0)
233 send_test_chunk (tm, sp);
236 if (sp->bytes_to_receive > 0)
238 receive_test_chunk (tm, sp);
241 if (PREDICT_FALSE (delete_session == 1))
243 u32 index, thread_index;
246 __sync_fetch_and_add (&tm->tx_total, sp->bytes_sent);
247 __sync_fetch_and_add (&tm->rx_total, sp->bytes_received);
249 session_parse_handle (sp->vpp_session_handle,
250 &index, &thread_index);
251 s = session_get_if_valid (index, thread_index);
255 vnet_disconnect_args_t _a, *a = &_a;
256 a->handle = session_handle (s);
257 a->app_index = tm->app_index;
258 vnet_disconnect_session (a);
260 vec_delete (connections_this_batch, 1, i);
262 __sync_fetch_and_add (&tm->ready_connections, -1);
265 clib_warning ("session AWOL?");
267 /* Kick the debug CLI process */
268 if (tm->ready_connections == 0)
270 signal_evt_to_cli (2);
275 tm->connection_index_by_thread[my_thread_index] = connection_indices;
276 tm->connections_this_batch_by_thread[my_thread_index] =
277 connections_this_batch;
282 VLIB_REGISTER_NODE (builtin_client_node) =
284 .function = builtin_client_node_fn,
285 .name = "builtin-tcp-client",
286 .type = VLIB_NODE_TYPE_INPUT,
287 .state = VLIB_NODE_STATE_DISABLED,
292 create_api_loopback (tclient_main_t * tm)
294 api_main_t *am = &api_main;
295 vl_shmem_hdr_t *shmem_hdr;
297 shmem_hdr = am->shmem_hdr;
298 tm->vl_input_queue = shmem_hdr->vl_input_queue;
299 tm->my_client_index = vl_api_memclnt_create_internal ("tcp_test_client",
305 tcp_test_clients_init (vlib_main_t * vm)
307 tclient_main_t *tm = &tclient_main;
308 vlib_thread_main_t *vtm = vlib_get_thread_main ();
312 if (create_api_loopback (tm))
315 num_threads = 1 /* main thread */ + vtm->n_threads;
317 /* Init test data. Big buffer */
318 vec_validate (tm->connect_test_data, 1024 * 1024 - 1);
319 for (i = 0; i < vec_len (tm->connect_test_data); i++)
320 tm->connect_test_data[i] = i & 0xff;
322 vec_validate (tm->rx_buf, num_threads - 1);
323 for (i = 0; i < num_threads; i++)
324 vec_validate (tm->rx_buf[i], vec_len (tm->connect_test_data) - 1);
328 vec_validate (tm->connection_index_by_thread, vtm->n_vlib_mains);
329 vec_validate (tm->connections_this_batch_by_thread, vtm->n_vlib_mains);
330 vec_validate (tm->vpp_event_queue, vtm->n_vlib_mains);
336 builtin_session_connected_callback (u32 app_index, u32 api_context,
337 stream_session_t * s, u8 is_fail)
339 tclient_main_t *tm = &tclient_main;
342 u8 thread_index = vlib_get_thread_index ();
346 clib_warning ("connection %d failed!", api_context);
347 signal_evt_to_cli (-1);
351 ASSERT (s->thread_index == thread_index);
353 if (!tm->vpp_event_queue[thread_index])
354 tm->vpp_event_queue[thread_index] =
355 session_manager_get_vpp_event_queue (thread_index);
360 clib_spinlock_lock_if_init (&tm->sessions_lock);
361 pool_get (tm->sessions, session);
362 clib_spinlock_unlock_if_init (&tm->sessions_lock);
364 memset (session, 0, sizeof (*session));
365 session_index = session - tm->sessions;
366 session->bytes_to_send = tm->bytes_to_send;
367 session->bytes_to_receive = tm->no_return ? 0ULL : tm->bytes_to_send;
368 session->server_rx_fifo = s->server_rx_fifo;
369 session->server_rx_fifo->client_session_index = session_index;
370 session->server_tx_fifo = s->server_tx_fifo;
371 session->server_tx_fifo->client_session_index = session_index;
372 session->vpp_session_handle = session_handle (s);
374 vec_add1 (tm->connection_index_by_thread[thread_index], session_index);
375 __sync_fetch_and_add (&tm->ready_connections, 1);
376 if (tm->ready_connections == tm->expected_connections)
379 /* Signal the CLI process that the action is starting... */
380 signal_evt_to_cli (1);
387 builtin_session_reset_callback (stream_session_t * s)
389 if (s->session_state == SESSION_STATE_READY)
390 clib_warning ("Reset active connection %U", format_stream_session, s, 2);
391 stream_session_cleanup (s);
396 builtin_session_create_callback (stream_session_t * s)
402 builtin_session_disconnect_callback (stream_session_t * s)
404 tclient_main_t *tm = &tclient_main;
405 vnet_disconnect_args_t _a, *a = &_a;
406 a->handle = session_handle (s);
407 a->app_index = tm->app_index;
408 vnet_disconnect_session (a);
413 builtin_server_rx_callback (stream_session_t * s)
419 static session_cb_vft_t builtin_clients = {
420 .session_reset_callback = builtin_session_reset_callback,
421 .session_connected_callback = builtin_session_connected_callback,
422 .session_accept_callback = builtin_session_create_callback,
423 .session_disconnect_callback = builtin_session_disconnect_callback,
424 .builtin_server_rx_callback = builtin_server_rx_callback
428 static clib_error_t *
429 attach_builtin_test_clients_app (u8 * appns_id, u64 appns_flags,
432 u32 prealloc_fifos, segment_size = 2 << 20;
433 tclient_main_t *tm = &tclient_main;
434 vnet_app_attach_args_t _a, *a = &_a;
436 clib_error_t *error = 0;
438 memset (a, 0, sizeof (*a));
439 memset (options, 0, sizeof (options));
441 a->api_client_index = tm->my_client_index;
442 a->session_cb_vft = &builtin_clients;
444 prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1;
446 if (tm->private_segment_size)
447 segment_size = tm->private_segment_size;
449 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
450 options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
451 options[APP_OPTIONS_RX_FIFO_SIZE] = tm->fifo_size;
452 options[APP_OPTIONS_TX_FIFO_SIZE] = tm->fifo_size;
453 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = tm->private_segment_count;
454 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
456 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
459 options[APP_OPTIONS_FLAGS] |= appns_flags;
460 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
462 a->options = options;
463 a->namespace_id = appns_id;
465 if ((error = vnet_application_attach (a)))
468 tm->app_index = a->app_index;
473 tclient_thread_fn (void *arg)
478 /** Start a transmit thread */
480 start_tx_pthread (tclient_main_t * tm)
482 if (tm->client_thread_handle == 0)
484 int rv = pthread_create (&tm->client_thread_handle,
486 tclient_thread_fn, 0);
489 tm->client_thread_handle = 0;
497 clients_connect (vlib_main_t * vm, u8 * uri, u32 n_clients)
499 tclient_main_t *tm = &tclient_main;
500 vnet_connect_args_t _a, *a = &_a;
501 clib_error_t *error = 0;
503 for (i = 0; i < n_clients; i++)
505 memset (a, 0, sizeof (*a));
507 a->uri = (char *) uri;
509 a->app_index = tm->app_index;
512 if ((error = vnet_connect_uri (a)))
516 /* Crude pacing for call setups */
518 vlib_process_suspend (vm, 10e-6);
519 ASSERT (i + 1 >= tm->ready_connections);
520 while (i + 1 - tm->ready_connections > 1000)
522 vlib_process_suspend (vm, 100e-6);
528 #define CLI_OUTPUT(_fmt, _args...) \
529 if (!tm->no_output) \
530 vlib_cli_output(vm, _fmt, ##_args)
532 static clib_error_t *
533 test_tcp_clients_command_fn (vlib_main_t * vm,
534 unformat_input_t * input,
535 vlib_cli_command_t * cmd)
537 tclient_main_t *tm = &tclient_main;
538 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
539 uword *event_data = 0, event_type;
540 u8 *default_connect_uri = (u8 *) "tcp://6.0.1.1/1234", *uri, *appns_id = 0;
541 u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
542 f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
543 f64 time_before_connects;
545 int preallocate_sessions = 0;
547 clib_error_t *error = 0;
550 tm->bytes_to_send = 8192;
552 tm->fifo_size = 64 << 10;
553 tm->connections_per_batch = 1000;
554 tm->private_segment_count = 0;
555 tm->private_segment_size = 0;
560 if (thread_main->n_vlib_mains > 1)
561 clib_spinlock_init (&tm->sessions_lock);
562 vec_free (tm->connect_uri);
564 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
566 if (unformat (input, "nclients %d", &n_clients))
568 else if (unformat (input, "mbytes %lld", &tmp))
569 tm->bytes_to_send = tmp << 20;
570 else if (unformat (input, "gbytes %lld", &tmp))
571 tm->bytes_to_send = tmp << 30;
572 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
574 else if (unformat (input, "uri %s", &tm->connect_uri))
576 else if (unformat (input, "test-timeout %f", &test_timeout))
578 else if (unformat (input, "syn-timeout %f", &syn_timeout))
580 else if (unformat (input, "no-return"))
582 else if (unformat (input, "fifo-size %d", &tm->fifo_size))
583 tm->fifo_size <<= 10;
584 else if (unformat (input, "private-segment-count %d",
585 &tm->private_segment_count))
587 else if (unformat (input, "private-segment-size %U",
588 unformat_memory_size, &tmp))
590 if (tmp >= 0x100000000ULL)
591 return clib_error_return
592 (0, "private segment size %lld (%llu) too large", tmp, tmp);
593 tm->private_segment_size = tmp;
595 else if (unformat (input, "preallocate-fifos"))
596 tm->prealloc_fifos = 1;
597 else if (unformat (input, "preallocate-sessions"))
598 preallocate_sessions = 1;
600 if (unformat (input, "client-batch %d", &tm->connections_per_batch))
602 else if (unformat (input, "appns %_%v%_", &appns_id))
604 else if (unformat (input, "all-scope"))
605 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
606 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
607 else if (unformat (input, "local-scope"))
608 appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
609 else if (unformat (input, "global-scope"))
610 appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
611 else if (unformat (input, "secret %lu", &appns_secret))
613 else if (unformat (input, "no-output"))
615 else if (unformat (input, "test-bytes"))
618 return clib_error_return (0, "unknown input `%U'",
619 format_unformat_error, input);
622 /* Store cli process node index for signalling */
623 tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
625 if (tm->is_init == 0)
627 if (tcp_test_clients_init (vm))
628 return clib_error_return (0, "failed init");
632 tm->ready_connections = 0;
633 tm->expected_connections = n_clients;
637 uri = default_connect_uri;
639 uri = tm->connect_uri;
641 #if TCP_BUILTIN_CLIENT_PTHREAD
645 vlib_worker_thread_barrier_sync (vm);
646 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
647 vlib_worker_thread_barrier_release (vm);
649 if (tm->test_client_attached == 0)
651 if ((error = attach_builtin_test_clients_app (appns_id, appns_flags,
655 clib_error_report (error);
660 tm->test_client_attached = 1;
662 /* Turn on the builtin client input nodes */
663 for (i = 0; i < thread_main->n_vlib_mains; i++)
664 vlib_node_set_state (vlib_mains[i], builtin_client_node.index,
665 VLIB_NODE_STATE_POLLING);
667 if (preallocate_sessions)
669 session_t *sp __attribute__ ((unused));
670 for (i = 0; i < n_clients; i++)
671 pool_get (tm->sessions, sp);
672 for (i = 0; i < n_clients; i++)
673 pool_put_index (tm->sessions, i);
676 /* Fire off connect requests */
677 time_before_connects = vlib_time_now (vm);
678 if ((error = clients_connect (vm, uri, n_clients)))
681 /* Park until the sessions come up, or ten seconds elapse... */
682 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
683 event_type = vlib_process_get_events (vm, &event_data);
687 CLI_OUTPUT ("Timeout with only %d sessions active...",
688 tm->ready_connections);
689 error = clib_error_return (0, "failed: syn timeout with %d sessions",
690 tm->ready_connections);
694 delta = vlib_time_now (vm) - time_before_connects;
696 CLI_OUTPUT ("%d three-way handshakes in %.2f seconds %.2f/s",
697 n_clients, delta, ((f64) n_clients) / delta);
699 tm->test_start_time = vlib_time_now (tm->vlib_main);
700 CLI_OUTPUT ("Test started at %.6f", tm->test_start_time);
704 CLI_OUTPUT ("unexpected event(1): %d", event_type);
705 error = clib_error_return (0, "failed: unexpected event(1): %d",
710 /* Now wait for the sessions to finish... */
711 vlib_process_wait_for_event_or_clock (vm, test_timeout);
712 event_type = vlib_process_get_events (vm, &event_data);
716 CLI_OUTPUT ("Timeout with %d sessions still active...",
717 tm->ready_connections);
718 error = clib_error_return (0, "failed: timeout with %d sessions",
719 tm->ready_connections);
723 tm->test_end_time = vlib_time_now (vm);
724 CLI_OUTPUT ("Test finished at %.6f", tm->test_end_time);
728 CLI_OUTPUT ("unexpected event(2): %d", event_type);
729 error = clib_error_return (0, "failed: unexpected event(2): %d",
734 delta = tm->test_end_time - tm->test_start_time;
738 total_bytes = (tm->no_return ? tm->tx_total : tm->rx_total);
739 transfer_type = tm->no_return ? "half-duplex" : "full-duplex";
740 CLI_OUTPUT ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
741 total_bytes, total_bytes / (1ULL << 20),
742 total_bytes / (1ULL << 30), delta);
743 CLI_OUTPUT ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
745 CLI_OUTPUT ("%.4f gbit/second %s",
746 (((f64) total_bytes * 8.0) / delta / 1e9), transfer_type);
750 CLI_OUTPUT ("zero delta-t?");
751 error = clib_error_return (0, "failed: zero delta-t");
755 if (tm->test_bytes && tm->test_failed)
756 error = clib_error_return (0, "failed: test bytes");
760 for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
762 vec_reset_length (tm->connection_index_by_thread[i]);
763 vec_reset_length (tm->connections_this_batch_by_thread[i]);
766 pool_free (tm->sessions);
768 /* Detach the application, so we can use different fifo sizes next time */
769 if (tm->test_client_attached)
771 vnet_app_detach_args_t _da, *da = &_da;
774 da->app_index = tm->app_index;
775 rv = vnet_application_detach (da);
778 error = clib_error_return (0, "failed: app detach");
779 CLI_OUTPUT ("WARNING: app detach failed...");
781 tm->test_client_attached = 0;
785 CLI_OUTPUT ("test failed");
790 VLIB_CLI_COMMAND (test_clients_command, static) =
792 .path = "test tcp clients",
793 .short_help = "test tcp clients [nclients %d] [[m|g]bytes <bytes>] "
794 "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
795 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
796 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
797 "[uri <tcp://ip/port>][test-bytes][no-output]",
798 .function = test_tcp_clients_command_fn,
804 tcp_test_clients_main_init (vlib_main_t * vm)
806 tclient_main_t *tm = &tclient_main;
811 VLIB_INIT_FUNCTION (tcp_test_clients_main_init);
814 * fd.io coding-style-patch-verification: ON
817 * eval: (c-set-style "gnu")