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 <vlibsocket/api.h>
25 #include <vpp/app/version.h>
27 /* define message IDs */
28 #include <vpp/api/vpe_msg_enum.h>
30 /* define message structures */
32 #include <vpp/api/vpe_all_api_h.h>
35 /* define generated endian-swappers */
37 #include <vpp/api/vpe_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #include <vpp/api/vpe_all_api_h.h>
46 #define TCP_BUILTIN_CLIENT_DBG (0)
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;
55 static int serial_number = 0;
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 rv = svm_fifo_enqueue_nowait (s->server_tx_fifo, bytes_this_chunk,
67 test_data + test_buf_offset);
69 /* If we managed to enqueue data... */
72 /* Account for it... */
73 s->bytes_to_send -= rv;
76 if (TCP_BUILTIN_CLIENT_DBG)
79 ELOG_TYPE_DECLARE (e) =
81 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
82 .format_args = "i4i4i4",
89 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
91 ed->data[1] = s->bytes_sent;
92 ed->data[2] = s->bytes_to_send;
95 /* Poke the session layer */
96 if (svm_fifo_set_event (s->server_tx_fifo))
98 /* Fabricate TX event, send to vpp */
99 evt.fifo = s->server_tx_fifo;
100 evt.event_type = FIFO_EVENT_APP_TX;
101 evt.event_id = serial_number++;
103 if (unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt,
104 0 /* do wait for mutex */ ))
105 clib_warning ("could not enqueue event");
111 receive_test_chunk (tclient_main_t * tm, session_t * s)
113 svm_fifo_t *rx_fifo = s->server_rx_fifo;
114 int n_read, test_bytes = 0;
116 /* Allow enqueuing of new event */
117 // svm_fifo_unset_event (rx_fifo);
121 n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (tm->rx_buf),
126 n_read = svm_fifo_max_dequeue (rx_fifo);
127 svm_fifo_dequeue_drop (rx_fifo, n_read);
132 if (TCP_BUILTIN_CLIENT_DBG)
135 ELOG_TYPE_DECLARE (e) =
137 .format = "rx-deq: %d bytes",
145 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
146 ed->data[0] = n_read;
152 for (i = 0; i < n_read; i++)
154 if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff))
156 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
157 n_read, s->bytes_received + i, tm->rx_buf[i],
158 ((s->bytes_received + i) & 0xff));
162 s->bytes_to_receive -= n_read;
163 s->bytes_received += n_read;
168 builtin_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
169 vlib_frame_t * frame)
171 tclient_main_t *tm = &tclient_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 = tm->connection_index_by_thread[my_thread_index];
181 connections_this_batch =
182 tm->connections_this_batch_by_thread[my_thread_index];
184 if ((tm->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 (tm->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 (tm->prev_conns != tm->connections_per_batch
205 && tm->prev_conns == vec_len (connections_this_batch)))
208 tm->prev_conns = vec_len (connections_this_batch);
209 if (tm->repeats == 500000)
211 clib_warning ("stuck clients");
216 tm->prev_conns = vec_len (connections_this_batch);
220 for (i = 0; i < vec_len (connections_this_batch); i++)
224 sp = pool_elt_at_index (tm->sessions, connections_this_batch[i]);
226 if (sp->bytes_to_send > 0)
228 send_test_chunk (tm, sp);
231 if (sp->bytes_to_receive > 0)
233 receive_test_chunk (tm, sp);
236 if (PREDICT_FALSE (delete_session == 1))
238 u32 index, thread_index;
241 __sync_fetch_and_add (&tm->tx_total, sp->bytes_sent);
242 __sync_fetch_and_add (&tm->rx_total, sp->bytes_received);
244 stream_session_parse_handle (sp->vpp_session_handle,
245 &index, &thread_index);
246 s = stream_session_get_if_valid (index, thread_index);
250 stream_session_disconnect (s);
251 vec_delete (connections_this_batch, 1, i);
253 __sync_fetch_and_add (&tm->ready_connections, -1);
256 clib_warning ("session AWOL?");
258 /* Kick the debug CLI process */
259 if (tm->ready_connections == 0)
261 tm->test_end_time = vlib_time_now (vm);
262 vlib_process_signal_event (vm, tm->cli_node_index,
268 tm->connection_index_by_thread[my_thread_index] = connection_indices;
269 tm->connections_this_batch_by_thread[my_thread_index] =
270 connections_this_batch;
275 VLIB_REGISTER_NODE (builtin_client_node) =
277 .function = builtin_client_node_fn,
278 .name = "builtin-tcp-client",
279 .type = VLIB_NODE_TYPE_INPUT,
280 .state = VLIB_NODE_STATE_DISABLED,
284 /* So we don't get "no handler for... " msgs */
286 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
288 vlib_main_t *vm = vlib_get_main ();
289 tclient_main_t *tm = &tclient_main;
290 tm->my_client_index = mp->index;
291 vlib_process_signal_event (vm, tm->cli_node_index, 1 /* evt */ ,
296 create_api_loopback (tclient_main_t * tm)
298 vlib_main_t *vm = vlib_get_main ();
299 vl_api_memclnt_create_t _m, *mp = &_m;
300 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
301 api_main_t *am = &api_main;
302 vl_shmem_hdr_t *shmem_hdr;
303 uword *event_data = 0, event_type;
307 * Create a "loopback" API client connection
308 * Don't do things like this unless you know what you're doing...
311 shmem_hdr = am->shmem_hdr;
312 tm->vl_input_queue = shmem_hdr->vl_input_queue;
313 memset (mp, 0, sizeof (*mp));
314 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
315 mp->context = 0xFEEDFACE;
316 mp->input_queue = pointer_to_uword (tm->vl_input_queue);
317 strncpy ((char *) mp->name, "tcp_clients_tester", sizeof (mp->name) - 1);
319 vl_api_memclnt_create_t_handler (mp);
322 vlib_process_wait_for_event_or_clock (vm, 1.0);
323 event_type = vlib_process_get_events (vm, &event_data);
333 clib_warning ("unknown event_type %d", event_type);
340 #define foreach_tclient_static_api_msg \
341 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
343 static clib_error_t *
344 tclient_api_hookup (vlib_main_t * vm)
346 vl_msg_api_msg_config_t _c, *c = &_c;
348 /* Hook up client-side static APIs to our handlers */
349 #define _(N,n) do { \
350 c->id = VL_API_##N; \
352 c->handler = vl_api_##n##_t_handler; \
353 c->cleanup = vl_noop_handler; \
354 c->endian = vl_api_##n##_t_endian; \
355 c->print = vl_api_##n##_t_print; \
356 c->size = sizeof(vl_api_##n##_t); \
357 c->traced = 1; /* trace, so these msgs print */ \
358 c->replay = 0; /* don't replay client create/delete msgs */ \
359 c->message_bounce = 0; /* don't bounce this message */ \
360 vl_msg_api_config(c);} while (0);
362 foreach_tclient_static_api_msg;
369 tcp_test_clients_init (vlib_main_t * vm)
371 tclient_main_t *tm = &tclient_main;
372 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
375 tclient_api_hookup (vm);
376 if (create_api_loopback (tm))
379 /* Init test data. Big buffer */
380 vec_validate (tm->connect_test_data, 1024 * 1024 - 1);
381 for (i = 0; i < vec_len (tm->connect_test_data); i++)
382 tm->connect_test_data[i] = i & 0xff;
384 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
385 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
390 vec_validate (tm->connection_index_by_thread, thread_main->n_vlib_mains);
391 vec_validate (tm->connections_this_batch_by_thread,
392 thread_main->n_vlib_mains);
397 builtin_session_connected_callback (u32 app_index, u32 api_context,
398 stream_session_t * s, u8 is_fail)
400 tclient_main_t *tm = &tclient_main;
407 clib_warning ("connection %d failed!", api_context);
408 vlib_process_signal_event (tm->vlib_main, tm->cli_node_index, -1,
413 tm->our_event_queue = session_manager_get_vpp_event_queue (s->thread_index);
414 tm->vpp_event_queue = session_manager_get_vpp_event_queue (s->thread_index);
419 pool_get (tm->sessions, session);
420 memset (session, 0, sizeof (*session));
421 session_index = session - tm->sessions;
422 session->bytes_to_send = tm->bytes_to_send;
423 session->bytes_to_receive = tm->no_return ? 0ULL : tm->bytes_to_send;
424 session->server_rx_fifo = s->server_rx_fifo;
425 session->server_rx_fifo->client_session_index = session_index;
426 session->server_tx_fifo = s->server_tx_fifo;
427 session->server_tx_fifo->client_session_index = session_index;
428 session->vpp_session_handle = stream_session_handle (s);
430 /* Add it to the session lookup table */
431 hash_set (tm->session_index_by_vpp_handles, session->vpp_session_handle,
434 if (tm->ready_connections == tm->expected_connections - 1)
436 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
440 for (i = 0; i < pool_elts (tm->sessions); i++)
442 vec_add1 (tm->connection_index_by_thread[thread_index], i);
444 if (thread_index == thread_main->n_vlib_mains)
448 __sync_fetch_and_add (&tm->ready_connections, 1);
449 if (tm->ready_connections == tm->expected_connections)
452 tm->test_start_time = vlib_time_now (tm->vlib_main);
453 /* Signal the CLI process that the action is starting... */
454 vlib_process_signal_event (tm->vlib_main, tm->cli_node_index, 1,
462 builtin_session_reset_callback (stream_session_t * s)
464 if (s->session_state == SESSION_STATE_READY)
465 clib_warning ("Reset active connection %U", format_stream_session, s, 2);
466 stream_session_cleanup (s);
471 builtin_session_create_callback (stream_session_t * s)
477 builtin_session_disconnect_callback (stream_session_t * s)
479 tclient_main_t *tm = &tclient_main;
480 vnet_disconnect_args_t _a, *a = &_a;
481 a->handle = stream_session_handle (s);
482 a->app_index = tm->app_index;
483 vnet_disconnect_session (a);
488 builtin_server_rx_callback (stream_session_t * s)
494 static session_cb_vft_t builtin_clients = {
495 .session_reset_callback = builtin_session_reset_callback,
496 .session_connected_callback = builtin_session_connected_callback,
497 .session_accept_callback = builtin_session_create_callback,
498 .session_disconnect_callback = builtin_session_disconnect_callback,
499 .builtin_server_rx_callback = builtin_server_rx_callback
504 attach_builtin_test_clients_app (void)
506 tclient_main_t *tm = &tclient_main;
507 vnet_app_attach_args_t _a, *a = &_a;
508 u8 segment_name[128];
509 u32 segment_name_length, prealloc_fifos;
512 segment_name_length = ARRAY_LEN (segment_name);
514 memset (a, 0, sizeof (*a));
515 memset (options, 0, sizeof (options));
517 a->api_client_index = tm->my_client_index;
518 a->segment_name = segment_name;
519 a->segment_name_length = segment_name_length;
520 a->session_cb_vft = &builtin_clients;
522 prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1;
524 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
525 options[SESSION_OPTIONS_SEGMENT_SIZE] = (2ULL << 32);
526 options[SESSION_OPTIONS_RX_FIFO_SIZE] = tm->fifo_size;
527 options[SESSION_OPTIONS_TX_FIFO_SIZE] = tm->fifo_size;
528 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = tm->private_segment_count;
529 options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = tm->private_segment_size;
530 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
532 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
534 a->options = options;
536 if (vnet_application_attach (a))
539 tm->app_index = a->app_index;
544 tclient_thread_fn (void *arg)
549 /** Start a transmit thread */
551 start_tx_pthread (tclient_main_t * tm)
553 if (tm->client_thread_handle == 0)
555 int rv = pthread_create (&tm->client_thread_handle,
557 tclient_thread_fn, 0);
560 tm->client_thread_handle = 0;
568 clients_connect (vlib_main_t * vm, u8 * uri, u32 n_clients)
570 tclient_main_t *tm = &tclient_main;
571 vnet_connect_args_t _a, *a = &_a;
573 for (i = 0; i < n_clients; i++)
575 memset (a, 0, sizeof (*a));
577 a->uri = (char *) uri;
579 a->app_index = tm->app_index;
581 vnet_connect_uri (a);
583 /* Crude pacing for call setups, 100k/sec */
584 vlib_process_suspend (vm, 10e-6);
588 static clib_error_t *
589 test_tcp_clients_command_fn (vlib_main_t * vm,
590 unformat_input_t * input,
591 vlib_cli_command_t * cmd)
593 tclient_main_t *tm = &tclient_main;
594 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
595 uword *event_data = 0, event_type;
596 u8 *default_connect_uri = (u8 *) "tcp://6.0.1.1/1234", *uri;
597 u64 tmp, total_bytes;
598 f64 cli_timeout = 20.0, delta;
603 tm->bytes_to_send = 8192;
605 tm->fifo_size = 64 << 10;
606 tm->connections_per_batch = 1000;
607 tm->private_segment_count = 0;
608 tm->private_segment_size = 0;
610 vec_free (tm->connect_uri);
612 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
614 if (unformat (input, "nclients %d", &n_clients))
616 else if (unformat (input, "mbytes %lld", &tmp))
617 tm->bytes_to_send = tmp << 20;
618 else if (unformat (input, "gbytes %lld", &tmp))
619 tm->bytes_to_send = tmp << 30;
620 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
622 else if (unformat (input, "uri %s", &tm->connect_uri))
624 else if (unformat (input, "cli-timeout %f", &cli_timeout))
626 else if (unformat (input, "no-return"))
628 else if (unformat (input, "fifo-size %d", &tm->fifo_size))
629 tm->fifo_size <<= 10;
630 else if (unformat (input, "private-segment-count %d",
631 &tm->private_segment_count))
633 else if (unformat (input, "private-segment-size %dm", &tmp))
634 tm->private_segment_size = tmp << 20;
635 else if (unformat (input, "private-segment-size %dg", &tmp))
636 tm->private_segment_size = tmp << 30;
637 else if (unformat (input, "private-segment-size %d", &tmp))
638 tm->private_segment_size = tmp;
639 else if (unformat (input, "preallocate-fifos"))
640 tm->prealloc_fifos = 1;
642 if (unformat (input, "client-batch %d", &tm->connections_per_batch))
645 return clib_error_return (0, "unknown input `%U'",
646 format_unformat_error, input);
649 /* Store cli process node index for signalling */
650 tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
652 if (tm->is_init == 0)
654 if (tcp_test_clients_init (vm))
655 return clib_error_return (0, "failed init");
658 tm->ready_connections = 0;
659 tm->expected_connections = n_clients;
663 uri = default_connect_uri;
665 uri = tm->connect_uri;
667 #if TCP_BUILTIN_CLIENT_PTHREAD
671 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
673 if (tm->test_client_attached == 0)
675 if (attach_builtin_test_clients_app ())
677 return clib_error_return (0, "app attach failed");
680 tm->test_client_attached = 1;
682 /* Turn on the builtin client input nodes */
683 for (i = 0; i < thread_main->n_vlib_mains; i++)
684 vlib_node_set_state (vlib_mains[i], builtin_client_node.index,
685 VLIB_NODE_STATE_POLLING);
687 /* Fire off connect requests */
688 clients_connect (vm, uri, n_clients);
690 /* Park until the sessions come up, or ten seconds elapse... */
691 vlib_process_wait_for_event_or_clock (vm, 10.0 /* timeout, seconds */ );
692 event_type = vlib_process_get_events (vm, &event_data);
697 vlib_cli_output (vm, "Timeout with only %d sessions active...",
698 tm->ready_connections);
702 vlib_cli_output (vm, "Test started at %.6f", tm->test_start_time);
706 vlib_cli_output (vm, "unexpected event(1): %d", event_type);
710 /* Now wait for the sessions to finish... */
711 vlib_process_wait_for_event_or_clock (vm, cli_timeout);
712 event_type = vlib_process_get_events (vm, &event_data);
717 vlib_cli_output (vm, "Timeout with %d sessions still active...",
718 tm->ready_connections);
722 vlib_cli_output (vm, "Test finished at %.6f", tm->test_end_time);
726 vlib_cli_output (vm, "unexpected event(2): %d", event_type);
730 delta = tm->test_end_time - tm->test_start_time;
734 total_bytes = (tm->no_return ? tm->tx_total : tm->rx_total);
735 transfer_type = tm->no_return ? "half-duplex" : "full-duplex";
737 "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
738 total_bytes, total_bytes / (1ULL << 20),
739 total_bytes / (1ULL << 30), delta);
740 vlib_cli_output (vm, "%.2f bytes/second %s",
741 ((f64) total_bytes) / (delta), transfer_type);
742 vlib_cli_output (vm, "%.4f gbit/second %s",
743 (((f64) total_bytes * 8.0) / delta / 1e9),
747 vlib_cli_output (vm, "zero delta-t?");
751 for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
753 vec_reset_length (tm->connection_index_by_thread[i]);
754 vec_reset_length (tm->connections_this_batch_by_thread[i]);
756 pool_free (tm->sessions);
762 VLIB_CLI_COMMAND (test_clients_command, static) =
764 .path = "test tcp clients",
765 .short_help = "test tcp clients [nclients %d]"
766 "[iterations %d] [bytes %d] [uri tcp://6.0.1.1/1234]",
767 .function = test_tcp_clients_command_fn,
772 tcp_test_clients_main_init (vlib_main_t * vm)
774 tclient_main_t *tm = &tclient_main;
779 VLIB_INIT_FUNCTION (tcp_test_clients_main_init);
782 * fd.io coding-style-patch-verification: ON
785 * eval: (c-set-style "gnu")