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 (1)
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 TCP state machine */
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 unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt,
104 0 /* do wait for mutex */ );
110 receive_test_chunk (tclient_main_t * tm, session_t * s)
112 svm_fifo_t *rx_fifo = s->server_rx_fifo;
113 int n_read, test_bytes = 0;
115 /* Allow enqueuing of new event */
116 // svm_fifo_unset_event (rx_fifo);
120 n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (tm->rx_buf),
125 n_read = svm_fifo_max_dequeue (rx_fifo);
126 svm_fifo_dequeue_drop (rx_fifo, n_read);
131 if (TCP_BUILTIN_CLIENT_DBG)
134 ELOG_TYPE_DECLARE (e) =
136 .format = "rx-deq: %d bytes",
144 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
145 ed->data[0] = n_read;
151 for (i = 0; i < n_read; i++)
153 if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff))
155 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
156 n_read, s->bytes_received + i, tm->rx_buf[i],
157 ((s->bytes_received + i) & 0xff));
161 s->bytes_to_receive -= n_read;
162 s->bytes_received += n_read;
167 builtin_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
168 vlib_frame_t * frame)
170 tclient_main_t *tm = &tclient_main;
171 int my_thread_index = vlib_get_thread_index ();
172 vl_api_disconnect_session_t *dmp;
176 u32 *connection_indices;
178 u32 delta, prev_bytes_received_this_session;
180 connection_indices = tm->connection_index_by_thread[my_thread_index];
182 if (tm->run_test == 0 || vec_len (connection_indices) == 0)
185 for (i = 0; i < vec_len (connection_indices); i++)
189 sp = pool_elt_at_index (tm->sessions, connection_indices[i]);
191 if (tx_quota < 60 && sp->bytes_to_send > 0)
193 send_test_chunk (tm, sp);
197 if (sp->bytes_to_receive > 0)
199 prev_bytes_received_this_session = sp->bytes_received;
200 receive_test_chunk (tm, sp);
201 delta = sp->bytes_received - prev_bytes_received_this_session;
206 if (PREDICT_FALSE (delete_session == 1))
208 __sync_fetch_and_add (&tm->rx_total, sp->bytes_received);
209 dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp));
210 memset (dmp, 0, sizeof (*dmp));
211 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
212 dmp->client_index = tm->my_client_index;
213 dmp->handle = sp->vpp_session_handle;
214 // vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & dmp);
215 if (!unix_shared_memory_queue_add (tm->vl_input_queue, (u8 *) & dmp,
218 vec_delete (connection_indices, 1, i);
219 tm->connection_index_by_thread[my_thread_index] =
221 __sync_fetch_and_add (&tm->ready_connections, -1);
225 vl_msg_api_free (dmp);
228 /* Kick the debug CLI process */
229 if (tm->ready_connections == 0)
231 tm->test_end_time = vlib_time_now (vm);
232 vlib_process_signal_event (vm, tm->cli_node_index,
241 VLIB_REGISTER_NODE (builtin_client_node) =
243 .function = builtin_client_node_fn,
244 .name = "builtin-tcp-client",
245 .type = VLIB_NODE_TYPE_INPUT,
246 .state = VLIB_NODE_STATE_DISABLED,
251 /* So we don't get "no handler for... " msgs */
253 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
255 vlib_main_t *vm = vlib_get_main ();
256 tclient_main_t *tm = &tclient_main;
257 tm->my_client_index = mp->index;
258 vlib_process_signal_event (vm, tm->node_index, 1 /* evt */ ,
263 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
265 tclient_main_t *tm = &tclient_main;
268 i32 retval = /* clib_net_to_host_u32 ( */ mp->retval /*) */ ;
273 clib_warning ("connection failed: retval %d", retval);
277 tm->our_event_queue =
278 uword_to_pointer (mp->vpp_event_queue_address,
279 unix_shared_memory_queue_t *);
280 tm->vpp_event_queue =
281 uword_to_pointer (mp->vpp_event_queue_address,
282 unix_shared_memory_queue_t *);
287 pool_get (tm->sessions, session);
288 memset (session, 0, sizeof (*session));
289 session_index = session - tm->sessions;
290 session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send;
292 session->server_rx_fifo =
293 uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
294 session->server_rx_fifo->client_session_index = session_index;
295 session->server_tx_fifo =
296 uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
297 session->server_tx_fifo->client_session_index = session_index;
298 session->vpp_session_handle = mp->handle;
300 /* Add it to the session lookup table */
301 hash_set (tm->session_index_by_vpp_handles, mp->handle, session_index);
303 if (tm->ready_connections == tm->expected_connections - 1)
305 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
309 for (i = 0; i < pool_elts (tm->sessions); i++)
311 vec_add1 (tm->connection_index_by_thread[thread_index], i);
313 if (thread_index == thread_main->n_vlib_mains)
317 __sync_fetch_and_add (&tm->ready_connections, 1);
318 if (tm->ready_connections == tm->expected_connections)
321 tm->test_start_time = vlib_time_now (tm->vlib_main);
322 /* Signal the CLI process that the action is starting... */
323 vlib_process_signal_event (tm->vlib_main, tm->cli_node_index,
329 create_api_loopback (tclient_main_t * tm)
331 vlib_main_t *vm = vlib_get_main ();
332 vl_api_memclnt_create_t _m, *mp = &_m;
333 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
334 api_main_t *am = &api_main;
335 vl_shmem_hdr_t *shmem_hdr;
336 uword *event_data = 0, event_type;
340 * Create a "loopback" API client connection
341 * Don't do things like this unless you know what you're doing...
344 shmem_hdr = am->shmem_hdr;
345 tm->vl_input_queue = shmem_hdr->vl_input_queue;
346 memset (mp, 0, sizeof (*mp));
347 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
348 mp->context = 0xFEEDFACE;
349 mp->input_queue = pointer_to_uword (tm->vl_input_queue);
350 strncpy ((char *) mp->name, "tcp_tester", sizeof (mp->name) - 1);
352 vl_api_memclnt_create_t_handler (mp);
355 tm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
356 vlib_process_wait_for_event_or_clock (vm, 1.0);
357 event_type = vlib_process_get_events (vm, &event_data);
367 clib_warning ("unknown event_type %d", event_type);
374 #define foreach_tclient_static_api_msg \
375 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
376 _(CONNECT_URI_REPLY, connect_uri_reply)
378 static clib_error_t *
379 tclient_api_hookup (vlib_main_t * vm)
381 vl_msg_api_msg_config_t _c, *c = &_c;
383 /* Hook up client-side static APIs to our handlers */
384 #define _(N,n) do { \
385 c->id = VL_API_##N; \
387 c->handler = vl_api_##n##_t_handler; \
388 c->cleanup = vl_noop_handler; \
389 c->endian = vl_api_##n##_t_endian; \
390 c->print = vl_api_##n##_t_print; \
391 c->size = sizeof(vl_api_##n##_t); \
392 c->traced = 1; /* trace, so these msgs print */ \
393 c->replay = 0; /* don't replay client create/delete msgs */ \
394 c->message_bounce = 0; /* don't bounce this message */ \
395 vl_msg_api_config(c);} while (0);
397 foreach_tclient_static_api_msg;
404 tcp_test_clients_init (vlib_main_t * vm)
406 tclient_main_t *tm = &tclient_main;
407 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
410 tclient_api_hookup (vm);
411 if (create_api_loopback (tm))
415 vec_validate (tm->connect_test_data, 64 * 1024 - 1);
416 for (i = 0; i < vec_len (tm->connect_test_data); i++)
417 tm->connect_test_data[i] = i & 0xff;
419 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
420 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
425 vec_validate (tm->connection_index_by_thread, thread_main->n_vlib_mains);
430 builtin_session_connected_callback (u32 app_index, u32 api_context,
431 stream_session_t * s, u8 is_fail)
433 vl_api_connect_uri_reply_t _m, *mp = &_m;
434 unix_shared_memory_queue_t *q;
436 unix_shared_memory_queue_t *vpp_queue;
438 app = application_get (app_index);
439 q = vl_api_client_index_to_input_queue (app->api_client_index);
444 memset (mp, 0, sizeof (*mp));
445 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY);
446 mp->context = api_context;
449 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
450 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
451 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
452 mp->handle = stream_session_handle (s);
453 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
455 s->session_state = SESSION_STATE_READY;
459 mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT_FAIL);
462 vl_api_connect_uri_reply_t_handler (mp);
468 builtin_session_reset_callback (stream_session_t * s)
474 builtin_session_create_callback (stream_session_t * s)
480 builtin_session_disconnect_callback (stream_session_t * s)
486 builtin_server_rx_callback (stream_session_t * s)
492 static session_cb_vft_t builtin_clients =
494 .session_reset_callback = builtin_session_reset_callback,
495 .session_connected_callback = builtin_session_connected_callback,
496 .session_accept_callback = builtin_session_create_callback,
497 .session_disconnect_callback = builtin_session_disconnect_callback,
498 .builtin_server_rx_callback = builtin_server_rx_callback
503 attach_builtin_test_clients ()
505 tclient_main_t *tm = &tclient_main;
506 vnet_app_attach_args_t _a, *a = &_a;
507 u8 segment_name[128];
508 u32 segment_name_length;
511 segment_name_length = ARRAY_LEN (segment_name);
513 memset (a, 0, sizeof (*a));
514 memset (options, 0, sizeof (options));
516 a->api_client_index = tm->my_client_index;
517 a->segment_name = segment_name;
518 a->segment_name_length = segment_name_length;
519 a->session_cb_vft = &builtin_clients;
521 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
522 options[SESSION_OPTIONS_SEGMENT_SIZE] = (2 << 30); /*$$$$ config / arg */
523 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
525 a->options = options;
527 return vnet_application_attach (a);
530 static clib_error_t *
531 test_tcp_clients_command_fn (vlib_main_t * vm,
532 unformat_input_t * input,
533 vlib_cli_command_t * cmd)
535 tclient_main_t *tm = &tclient_main;
536 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
537 uword *event_data = 0;
539 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
544 f64 cli_timeout = 20.0;
547 tm->bytes_to_send = 8192;
548 vec_free (tm->connect_uri);
550 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
552 if (unformat (input, "nclients %d", &n_clients))
554 else if (unformat (input, "mbytes %lld", &tmp))
555 tm->bytes_to_send = tmp << 20;
556 else if (unformat (input, "gbytes %lld", &tmp))
557 tm->bytes_to_send = tmp << 30;
558 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
560 else if (unformat (input, "uri %s", &tm->connect_uri))
562 else if (unformat (input, "cli-timeout %f", &cli_timeout))
565 return clib_error_return (0, "unknown input `%U'",
566 format_unformat_error, input);
569 if (tm->is_init == 0)
571 if (tcp_test_clients_init (vm))
572 return clib_error_return (0, "failed init");
575 tm->ready_connections = 0;
576 tm->expected_connections = n_clients;
581 uri = tm->connect_uri;
583 #if TCP_BUILTIN_CLIENT_PTHREAD
584 /* Start a transmit thread */
585 if (tm->client_thread_handle == 0)
587 int rv = pthread_create (&tm->client_thread_handle,
589 tclient_thread_fn, 0);
592 tm->client_thread_handle = 0;
593 return clib_error_return (0, "pthread_create returned %d", rv);
597 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
598 if (tm->test_client_attached == 0)
599 attach_builtin_test_clients ();
600 tm->test_client_attached = 1;
602 /* Turn on the builtin client input nodes */
603 for (i = 0; i < thread_main->n_vlib_mains; i++)
604 vlib_node_set_state (vlib_mains[i], builtin_client_node.index,
605 VLIB_NODE_STATE_POLLING);
607 tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
609 /* Fire off connect requests */
610 for (i = 0; i < n_clients; i++)
612 vl_api_connect_uri_t _cmp, *cmp = &_cmp;
613 void vl_api_connect_uri_t_handler (vl_api_connect_uri_t * cmp);
615 memset (cmp, 0, sizeof (*cmp));
617 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
618 cmp->client_index = tm->my_client_index;
619 cmp->context = ntohl (0xfeedface);
620 memcpy (cmp->uri, uri, strlen ((char *) uri) + 1);
622 vl_api_connect_uri_t_handler (cmp);
623 /* Crude pacing for call setups, 100k/sec */
624 vlib_process_suspend (vm, 10e-6);
627 /* Park until the sessions come up, or ten seconds elapse... */
628 vlib_process_wait_for_event_or_clock (vm, 10.0 /* timeout, seconds */ );
629 event_type = vlib_process_get_events (vm, &event_data);
634 vlib_cli_output (vm, "Timeout with only %d sessions active...",
635 tm->ready_connections);
639 vlib_cli_output (vm, "Test started at %.6f", tm->test_start_time);
643 vlib_cli_output (vm, "unexpected event(1): %d", event_type);
647 /* Now wait for the sessions to finish... */
648 vlib_process_wait_for_event_or_clock (vm, cli_timeout);
649 event_type = vlib_process_get_events (vm, &event_data);
654 vlib_cli_output (vm, "Timeout with %d sessions still active...",
655 tm->ready_connections);
659 vlib_cli_output (vm, "Test finished at %.6f", tm->test_end_time);
663 vlib_cli_output (vm, "unexpected event(2): %d", event_type);
667 delta = tm->test_end_time - tm->test_start_time;
672 "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
673 tm->rx_total, tm->rx_total / (1ULL << 20),
674 tm->rx_total / (1ULL << 30), delta);
675 vlib_cli_output (vm, "%.2f bytes/second full-duplex",
676 ((f64) tm->rx_total) / (delta));
677 vlib_cli_output (vm, "%.4f gbit/second full-duplex",
678 (((f64) tm->rx_total * 8.0) / delta / 1e9));
681 vlib_cli_output (vm, "zero delta-t?");
684 pool_free (tm->sessions);
685 for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
686 vec_reset_length (tm->connection_index_by_thread[i]);
692 VLIB_CLI_COMMAND (test_clients_command, static) =
694 .path = "test tcp clients",
695 .short_help = "test tcp clients [nclients %d]"
696 "[iterations %d] [bytes %d] [uri tcp://6.0.1.1/1234]",
697 .function = test_tcp_clients_command_fn,
702 tcp_test_clients_main_init (vlib_main_t * vm)
704 tclient_main_t *tm = &tclient_main;
709 VLIB_INIT_FUNCTION (tcp_test_clients_main_init);
712 * fd.io coding-style-patch-verification: ON
715 * eval: (c-set-style "gnu")