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 ();
173 vl_api_disconnect_session_t *dmp;
177 u32 *connection_indices;
179 u32 delta, prev_bytes_received_this_session;
181 connection_indices = tm->connection_index_by_thread[my_thread_index];
183 if (tm->run_test == 0 || vec_len (connection_indices) == 0)
186 for (i = 0; i < vec_len (connection_indices); i++)
190 sp = pool_elt_at_index (tm->sessions, connection_indices[i]);
192 if ((tm->no_return || tx_quota < 60) && sp->bytes_to_send > 0)
194 send_test_chunk (tm, sp);
198 if (!tm->no_return && sp->bytes_to_receive > 0)
200 prev_bytes_received_this_session = sp->bytes_received;
201 receive_test_chunk (tm, sp);
202 delta = sp->bytes_received - prev_bytes_received_this_session;
207 if (PREDICT_FALSE (delete_session == 1))
209 __sync_fetch_and_add (&tm->tx_total, tm->bytes_to_send);
210 __sync_fetch_and_add (&tm->rx_total, sp->bytes_received);
212 dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp));
213 memset (dmp, 0, sizeof (*dmp));
214 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
215 dmp->client_index = tm->my_client_index;
216 dmp->handle = sp->vpp_session_handle;
217 if (!unix_shared_memory_queue_add (tm->vl_input_queue, (u8 *) & dmp,
220 vec_delete (connection_indices, 1, i);
221 tm->connection_index_by_thread[my_thread_index] =
223 __sync_fetch_and_add (&tm->ready_connections, -1);
227 vl_msg_api_free (dmp);
230 /* Kick the debug CLI process */
231 if (tm->ready_connections == 0)
233 tm->test_end_time = vlib_time_now (vm);
234 vlib_process_signal_event (vm, tm->cli_node_index,
243 VLIB_REGISTER_NODE (builtin_client_node) =
245 .function = builtin_client_node_fn,
246 .name = "builtin-tcp-client",
247 .type = VLIB_NODE_TYPE_INPUT,
248 .state = VLIB_NODE_STATE_DISABLED,
252 /* So we don't get "no handler for... " msgs */
254 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
256 vlib_main_t *vm = vlib_get_main ();
257 tclient_main_t *tm = &tclient_main;
258 tm->my_client_index = mp->index;
259 vlib_process_signal_event (vm, tm->cli_node_index, 1 /* evt */ ,
264 create_api_loopback (tclient_main_t * tm)
266 vlib_main_t *vm = vlib_get_main ();
267 vl_api_memclnt_create_t _m, *mp = &_m;
268 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
269 api_main_t *am = &api_main;
270 vl_shmem_hdr_t *shmem_hdr;
271 uword *event_data = 0, event_type;
275 * Create a "loopback" API client connection
276 * Don't do things like this unless you know what you're doing...
279 shmem_hdr = am->shmem_hdr;
280 tm->vl_input_queue = shmem_hdr->vl_input_queue;
281 memset (mp, 0, sizeof (*mp));
282 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
283 mp->context = 0xFEEDFACE;
284 mp->input_queue = pointer_to_uword (tm->vl_input_queue);
285 strncpy ((char *) mp->name, "tcp_clients_tester", sizeof (mp->name) - 1);
287 vl_api_memclnt_create_t_handler (mp);
290 vlib_process_wait_for_event_or_clock (vm, 1.0);
291 event_type = vlib_process_get_events (vm, &event_data);
301 clib_warning ("unknown event_type %d", event_type);
308 #define foreach_tclient_static_api_msg \
309 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
311 static clib_error_t *
312 tclient_api_hookup (vlib_main_t * vm)
314 vl_msg_api_msg_config_t _c, *c = &_c;
316 /* Hook up client-side static APIs to our handlers */
317 #define _(N,n) do { \
318 c->id = VL_API_##N; \
320 c->handler = vl_api_##n##_t_handler; \
321 c->cleanup = vl_noop_handler; \
322 c->endian = vl_api_##n##_t_endian; \
323 c->print = vl_api_##n##_t_print; \
324 c->size = sizeof(vl_api_##n##_t); \
325 c->traced = 1; /* trace, so these msgs print */ \
326 c->replay = 0; /* don't replay client create/delete msgs */ \
327 c->message_bounce = 0; /* don't bounce this message */ \
328 vl_msg_api_config(c);} while (0);
330 foreach_tclient_static_api_msg;
337 tcp_test_clients_init (vlib_main_t * vm)
339 tclient_main_t *tm = &tclient_main;
340 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
343 tclient_api_hookup (vm);
344 if (create_api_loopback (tm))
347 /* Init test data. Big buffer */
348 vec_validate (tm->connect_test_data, 1024 * 1024 - 1);
349 for (i = 0; i < vec_len (tm->connect_test_data); i++)
350 tm->connect_test_data[i] = i & 0xff;
352 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
353 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
358 vec_validate (tm->connection_index_by_thread, thread_main->n_vlib_mains);
363 builtin_session_connected_callback (u32 app_index, u32 api_context,
364 stream_session_t * s, u8 is_fail)
366 tclient_main_t *tm = &tclient_main;
373 clib_warning ("connection %d failed!", api_context);
374 vlib_process_signal_event (tm->vlib_main, tm->cli_node_index, -1,
379 /* Mark vpp session as connected */
380 s->session_state = SESSION_STATE_READY;
382 tm->our_event_queue = session_manager_get_vpp_event_queue (s->thread_index);
383 tm->vpp_event_queue = session_manager_get_vpp_event_queue (s->thread_index);
388 pool_get (tm->sessions, session);
389 memset (session, 0, sizeof (*session));
390 session_index = session - tm->sessions;
391 session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send;
392 session->server_rx_fifo = s->server_rx_fifo;
393 session->server_rx_fifo->client_session_index = session_index;
394 session->server_tx_fifo = s->server_tx_fifo;
395 session->server_tx_fifo->client_session_index = session_index;
396 session->vpp_session_handle = stream_session_handle (s);
398 /* Add it to the session lookup table */
399 hash_set (tm->session_index_by_vpp_handles, session->vpp_session_handle,
402 if (tm->ready_connections == tm->expected_connections - 1)
404 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
408 for (i = 0; i < pool_elts (tm->sessions); i++)
410 vec_add1 (tm->connection_index_by_thread[thread_index], i);
412 if (thread_index == thread_main->n_vlib_mains)
416 __sync_fetch_and_add (&tm->ready_connections, 1);
417 if (tm->ready_connections == tm->expected_connections)
420 tm->test_start_time = vlib_time_now (tm->vlib_main);
421 /* Signal the CLI process that the action is starting... */
422 vlib_process_signal_event (tm->vlib_main, tm->cli_node_index, 1,
430 builtin_session_reset_callback (stream_session_t * s)
436 builtin_session_create_callback (stream_session_t * s)
442 builtin_session_disconnect_callback (stream_session_t * s)
448 builtin_server_rx_callback (stream_session_t * s)
454 static session_cb_vft_t builtin_clients = {
455 .session_reset_callback = builtin_session_reset_callback,
456 .session_connected_callback = builtin_session_connected_callback,
457 .session_accept_callback = builtin_session_create_callback,
458 .session_disconnect_callback = builtin_session_disconnect_callback,
459 .builtin_server_rx_callback = builtin_server_rx_callback
464 attach_builtin_test_clients_app (void)
466 tclient_main_t *tm = &tclient_main;
467 vnet_app_attach_args_t _a, *a = &_a;
468 u8 segment_name[128];
469 u32 segment_name_length, prealloc_fifos;
472 segment_name_length = ARRAY_LEN (segment_name);
474 memset (a, 0, sizeof (*a));
475 memset (options, 0, sizeof (options));
477 a->api_client_index = tm->my_client_index;
478 a->segment_name = segment_name;
479 a->segment_name_length = segment_name_length;
480 a->session_cb_vft = &builtin_clients;
482 prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1;
484 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
485 options[SESSION_OPTIONS_SEGMENT_SIZE] = (2ULL << 32);
486 options[SESSION_OPTIONS_RX_FIFO_SIZE] = tm->fifo_size;
487 options[SESSION_OPTIONS_TX_FIFO_SIZE] = tm->fifo_size / 2;
488 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
490 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
492 a->options = options;
494 if (vnet_application_attach (a))
497 tm->app_index = a->app_index;
502 tclient_thread_fn (void *arg)
507 /** Start a transmit thread */
509 start_tx_pthread (tclient_main_t * tm)
511 if (tm->client_thread_handle == 0)
513 int rv = pthread_create (&tm->client_thread_handle,
515 tclient_thread_fn, 0);
518 tm->client_thread_handle = 0;
526 clients_connect (vlib_main_t * vm, u8 * uri, u32 n_clients)
528 tclient_main_t *tm = &tclient_main;
529 vnet_connect_args_t _a, *a = &_a;
531 for (i = 0; i < n_clients; i++)
533 memset (a, 0, sizeof (*a));
535 a->uri = (char *) uri;
537 a->app_index = tm->app_index;
539 vnet_connect_uri (a);
541 /* Crude pacing for call setups, 100k/sec */
542 vlib_process_suspend (vm, 10e-6);
546 static clib_error_t *
547 test_tcp_clients_command_fn (vlib_main_t * vm,
548 unformat_input_t * input,
549 vlib_cli_command_t * cmd)
551 tclient_main_t *tm = &tclient_main;
552 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
553 uword *event_data = 0, event_type;
554 u8 *default_connect_uri = (u8 *) "tcp://6.0.1.1/1234", *uri;
555 u64 tmp, total_bytes;
556 f64 cli_timeout = 20.0, delta;
561 tm->bytes_to_send = 8192;
563 tm->fifo_size = 64 << 10;
565 vec_free (tm->connect_uri);
567 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
569 if (unformat (input, "nclients %d", &n_clients))
571 else if (unformat (input, "mbytes %lld", &tmp))
572 tm->bytes_to_send = tmp << 20;
573 else if (unformat (input, "gbytes %lld", &tmp))
574 tm->bytes_to_send = tmp << 30;
575 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
577 else if (unformat (input, "uri %s", &tm->connect_uri))
579 else if (unformat (input, "cli-timeout %f", &cli_timeout))
581 else if (unformat (input, "no-return"))
583 else if (unformat (input, "fifo-size %d", &tm->fifo_size))
584 tm->fifo_size <<= 10;
586 return clib_error_return (0, "unknown input `%U'",
587 format_unformat_error, input);
590 /* Store cli process node index for signalling */
591 tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
593 if (tm->is_init == 0)
595 if (tcp_test_clients_init (vm))
596 return clib_error_return (0, "failed init");
599 tm->ready_connections = 0;
600 tm->expected_connections = n_clients;
604 uri = default_connect_uri;
606 uri = tm->connect_uri;
608 #if TCP_BUILTIN_CLIENT_PTHREAD
612 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
614 if (tm->test_client_attached == 0)
616 if (attach_builtin_test_clients_app ())
618 return clib_error_return (0, "app attach failed");
621 tm->test_client_attached = 1;
623 /* Turn on the builtin client input nodes */
624 for (i = 0; i < thread_main->n_vlib_mains; i++)
625 vlib_node_set_state (vlib_mains[i], builtin_client_node.index,
626 VLIB_NODE_STATE_POLLING);
628 /* Fire off connect requests */
629 clients_connect (vm, uri, n_clients);
631 /* Park until the sessions come up, or ten seconds elapse... */
632 vlib_process_wait_for_event_or_clock (vm, 10.0 /* timeout, seconds */ );
633 event_type = vlib_process_get_events (vm, &event_data);
638 vlib_cli_output (vm, "Timeout with only %d sessions active...",
639 tm->ready_connections);
643 vlib_cli_output (vm, "Test started at %.6f", tm->test_start_time);
647 vlib_cli_output (vm, "unexpected event(1): %d", event_type);
651 /* Now wait for the sessions to finish... */
652 vlib_process_wait_for_event_or_clock (vm, cli_timeout);
653 event_type = vlib_process_get_events (vm, &event_data);
658 vlib_cli_output (vm, "Timeout with %d sessions still active...",
659 tm->ready_connections);
663 vlib_cli_output (vm, "Test finished at %.6f", tm->test_end_time);
667 vlib_cli_output (vm, "unexpected event(2): %d", event_type);
671 delta = tm->test_end_time - tm->test_start_time;
675 total_bytes = (tm->no_return ? tm->tx_total : tm->rx_total);
676 transfer_type = tm->no_return ? "half-duplex" : "full-duplex";
678 "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
679 total_bytes, total_bytes / (1ULL << 20),
680 total_bytes / (1ULL << 30), delta);
681 vlib_cli_output (vm, "%.2f bytes/second %s",
682 ((f64) total_bytes) / (delta), transfer_type);
683 vlib_cli_output (vm, "%.4f gbit/second %s",
684 (((f64) total_bytes * 8.0) / delta / 1e9),
688 vlib_cli_output (vm, "zero delta-t?");
691 pool_free (tm->sessions);
692 for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
693 vec_reset_length (tm->connection_index_by_thread[i]);
699 VLIB_CLI_COMMAND (test_clients_command, static) =
701 .path = "test tcp clients",
702 .short_help = "test tcp clients [nclients %d]"
703 "[iterations %d] [bytes %d] [uri tcp://6.0.1.1/1234]",
704 .function = test_tcp_clients_command_fn,
709 tcp_test_clients_main_init (vlib_main_t * vm)
711 tclient_main_t *tm = &tclient_main;
716 VLIB_INIT_FUNCTION (tcp_test_clients_main_init);
719 * fd.io coding-style-patch-verification: ON
722 * eval: (c-set-style "gnu")