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)
47 #define TCP_BUILTIN_CLIENT_VPP_THREAD (0)
48 #define TCP_BUILTIN_CLIENT_PTHREAD (!TCP_BUILTIN_CLIENT_VPP_THREAD)
51 send_test_chunk (tclient_main_t * tm, session_t * s)
53 u8 *test_data = tm->connect_test_data;
56 session_fifo_event_t evt;
57 static int serial_number = 0;
60 ASSERT (vec_len (test_data) > 0);
62 test_buf_offset = s->bytes_sent % vec_len (test_data);
63 bytes_this_chunk = vec_len (test_data) - test_buf_offset;
65 bytes_this_chunk = bytes_this_chunk < s->bytes_to_send
66 ? bytes_this_chunk : s->bytes_to_send;
68 rv = svm_fifo_enqueue_nowait (s->server_tx_fifo, bytes_this_chunk,
69 test_data + test_buf_offset);
71 /* If we managed to enqueue data... */
74 /* Account for it... */
75 s->bytes_to_send -= rv;
78 if (TCP_BUILTIN_CLIENT_DBG)
81 ELOG_TYPE_DECLARE (e) =
83 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
84 .format_args = "i4i4i4",
91 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
93 ed->data[1] = s->bytes_sent;
94 ed->data[2] = s->bytes_to_send;
97 /* Poke the TCP state machine */
98 if (svm_fifo_set_event (s->server_tx_fifo))
100 /* Fabricate TX event, send to vpp */
101 evt.fifo = s->server_tx_fifo;
102 evt.event_type = FIFO_EVENT_APP_TX;
103 evt.event_id = serial_number++;
105 unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt,
106 0 /* do wait for mutex */ );
112 receive_test_chunk (tclient_main_t * tm, session_t * s)
114 svm_fifo_t *rx_fifo = s->server_rx_fifo;
115 int n_read, test_bytes = 0;
117 /* Allow enqueuing of new event */
118 // svm_fifo_unset_event (rx_fifo);
120 n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (tm->rx_buf),
124 if (TCP_BUILTIN_CLIENT_DBG)
127 ELOG_TYPE_DECLARE (e) =
129 .format = "rx-deq: %d bytes",
137 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
138 ed->data[0] = n_read;
144 for (i = 0; i < n_read; i++)
146 if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff))
148 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
149 n_read, s->bytes_received + i, tm->rx_buf[i],
150 ((s->bytes_received + i) & 0xff));
154 s->bytes_to_receive -= n_read;
155 s->bytes_received += n_read;
159 #if TCP_BUILTIN_CLIENT_VPP_THREAD
160 #define THREAD_PROTOTYPE static void
162 #define THREAD_PROTOTYPE static void *
166 tclient_thread_fn (void *arg)
168 tclient_main_t *tm = &tclient_main;
169 vl_api_disconnect_session_t *dmp;
171 struct timespec ts, tsrem;
174 u32 *session_indices = 0;
179 clib_time_init (&ttime);
181 /* stats thread wants no signals. */
185 pthread_sigmask (SIG_SETMASK, &s, 0);
188 clib_per_cpu_mheaps[vlib_get_thread_index ()] = clib_per_cpu_mheaps[0];
190 vec_validate (session_indices, 0);
191 vec_reset_length (session_indices);
195 /* Wait until we're told to get busy */
196 while (tm->run_test == 0
197 || (tm->ready_connections != tm->expected_connections))
200 ts.tv_nsec = 100000000;
201 while (nanosleep (&ts, &tsrem) < 0)
207 clib_warning ("Start test...");
209 before = clib_time_now (&ttime);
218 pool_foreach (sp, tm->sessions,
220 if (sp->bytes_to_send > 0)
222 send_test_chunk (tm, sp);
226 pool_foreach (sp, tm->sessions,
228 if (sp->bytes_to_receive > 0)
230 receive_test_chunk (tm, sp);
235 /* Session is complete */
236 vec_add1 (session_indices, sp - tm->sessions);
239 /* Terminate any completed sessions */
240 if (PREDICT_FALSE (_vec_len(session_indices) != 0))
242 for (i = 0; i < _vec_len (session_indices); i++)
244 sp = pool_elt_at_index (tm->sessions, session_indices[i]);
245 rx_total += sp->bytes_received;
246 dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp));
247 memset (dmp, 0, sizeof (*dmp));
248 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
249 dmp->client_index = tm->my_client_index;
250 dmp->handle = sp->vpp_session_handle;
251 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & dmp);
252 pool_put (tm->sessions, sp);
254 _vec_len(session_indices) = 0;
258 while (try_tx || try_rx);
261 after = clib_time_now (&ttime);
263 clib_warning ("Test complete %lld bytes in %.2f secs",
264 rx_total, (after - before));
265 if ((after - before) != 0.0)
267 clib_warning ("%.2f bytes/second full-duplex",
268 ((f64) rx_total) / (after - before));
269 clib_warning ("%.4f gbit/second full-duplex",
270 (((f64) rx_total * 8.0) / (after - before)) / 1e9);
273 if (pool_elts (tm->sessions))
274 clib_warning ("BUG: %d active sessions remain...",
275 pool_elts (tm->sessions));
279 #if TCP_BUILTIN_CLIENT_PTHREAD
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->node_index, 1 /* evt */ ,
296 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
298 tclient_main_t *tm = &tclient_main;
301 i32 retval = /* clib_net_to_host_u32 ( */ mp->retval /*) */ ;
305 clib_warning ("connection failed: retval %d", retval);
309 tm->our_event_queue =
310 uword_to_pointer (mp->vpp_event_queue_address,
311 unix_shared_memory_queue_t *);
312 tm->vpp_event_queue =
313 uword_to_pointer (mp->vpp_event_queue_address,
314 unix_shared_memory_queue_t *);
319 pool_get (tm->sessions, session);
320 memset (session, 0, sizeof (*session));
321 session_index = session - tm->sessions;
322 session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send;
324 session->server_rx_fifo =
325 uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
326 session->server_rx_fifo->client_session_index = session_index;
327 session->server_tx_fifo =
328 uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
329 session->server_tx_fifo->client_session_index = session_index;
330 session->vpp_session_handle = mp->handle;
332 /* Add it to the session lookup table */
333 hash_set (tm->session_index_by_vpp_handles, mp->handle, session_index);
335 tm->ready_connections++;
339 create_api_loopback (tclient_main_t * tm)
341 vlib_main_t *vm = vlib_get_main ();
342 vl_api_memclnt_create_t _m, *mp = &_m;
343 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
344 api_main_t *am = &api_main;
345 vl_shmem_hdr_t *shmem_hdr;
346 uword *event_data = 0, event_type;
350 * Create a "loopback" API client connection
351 * Don't do things like this unless you know what you're doing...
354 shmem_hdr = am->shmem_hdr;
355 tm->vl_input_queue = shmem_hdr->vl_input_queue;
356 memset (mp, 0, sizeof (*mp));
357 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
358 mp->context = 0xFEEDFACE;
359 mp->input_queue = pointer_to_uword (tm->vl_input_queue);
360 strncpy ((char *) mp->name, "tcp_tester", sizeof (mp->name) - 1);
362 vl_api_memclnt_create_t_handler (mp);
365 tm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
366 vlib_process_wait_for_event_or_clock (vm, 1.0);
367 event_type = vlib_process_get_events (vm, &event_data);
377 clib_warning ("unknown event_type %d", event_type);
384 #define foreach_tclient_static_api_msg \
385 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
386 _(CONNECT_URI_REPLY, connect_uri_reply)
388 static clib_error_t *
389 tclient_api_hookup (vlib_main_t * vm)
391 vl_msg_api_msg_config_t _c, *c = &_c;
393 /* Hook up client-side static APIs to our handlers */
394 #define _(N,n) do { \
395 c->id = VL_API_##N; \
397 c->handler = vl_api_##n##_t_handler; \
398 c->cleanup = vl_noop_handler; \
399 c->endian = vl_api_##n##_t_endian; \
400 c->print = vl_api_##n##_t_print; \
401 c->size = sizeof(vl_api_##n##_t); \
402 c->traced = 1; /* trace, so these msgs print */ \
403 c->replay = 0; /* don't replay client create/delete msgs */ \
404 c->message_bounce = 0; /* don't bounce this message */ \
405 vl_msg_api_config(c);} while (0);
407 foreach_tclient_static_api_msg;
414 tcp_test_clients_init (vlib_main_t * vm)
416 tclient_main_t *tm = &tclient_main;
419 tclient_api_hookup (vm);
420 if (create_api_loopback (tm))
424 vec_validate (tm->connect_test_data, 64 * 1024 - 1);
425 for (i = 0; i < vec_len (tm->connect_test_data); i++)
426 tm->connect_test_data[i] = i & 0xff;
428 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
429 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
437 builtin_session_reset_callback (stream_session_t * s)
443 builtin_session_create_callback (stream_session_t * s)
449 builtin_session_disconnect_callback (stream_session_t * s)
455 builtin_server_rx_callback (stream_session_t * s)
461 static session_cb_vft_t builtin_clients =
463 .session_reset_callback = builtin_session_reset_callback,
464 .session_connected_callback = send_session_connected_callback,
465 .session_accept_callback = builtin_session_create_callback,
466 .session_disconnect_callback = builtin_session_disconnect_callback,
467 .builtin_server_rx_callback = builtin_server_rx_callback
472 attach_builtin_test_clients ()
474 tclient_main_t *tm = &tclient_main;
475 vnet_app_attach_args_t _a, *a = &_a;
476 u8 segment_name[128];
477 u32 segment_name_length;
480 segment_name_length = ARRAY_LEN (segment_name);
482 memset (a, 0, sizeof (*a));
483 memset (options, 0, sizeof (options));
485 a->api_client_index = tm->my_client_index;
486 a->segment_name = segment_name;
487 a->segment_name_length = segment_name_length;
488 a->session_cb_vft = &builtin_clients;
490 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
491 options[SESSION_OPTIONS_SEGMENT_SIZE] = (2 << 30); /*$$$$ config / arg */
492 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
494 a->options = options;
496 return vnet_application_attach (a);
499 static clib_error_t *
500 test_tcp_clients_command_fn (vlib_main_t * vm,
501 unformat_input_t * input,
502 vlib_cli_command_t * cmd)
504 tclient_main_t *tm = &tclient_main;
505 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
511 tm->bytes_to_send = 8192;
512 vec_free (tm->connect_uri);
514 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
516 if (unformat (input, "nclients %d", &n_clients))
518 else if (unformat (input, "mbytes %lld", &tmp))
519 tm->bytes_to_send = tmp << 20;
520 else if (unformat (input, "gbytes %lld", &tmp))
521 tm->bytes_to_send = tmp << 30;
522 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
524 else if (unformat (input, "uri %s", &tm->connect_uri))
527 return clib_error_return (0, "unknown input `%U'",
528 format_unformat_error, input);
531 if (tm->is_init == 0)
533 if (tcp_test_clients_init (vm))
534 return clib_error_return (0, "failed init");
537 tm->ready_connections = 0;
538 tm->expected_connections = n_clients;
542 uri = tm->connect_uri;
544 #if TCP_BUILTIN_CLIENT_PTHREAD
545 /* Start a transmit thread */
546 if (tm->client_thread_handle == 0)
548 int rv = pthread_create (&tm->client_thread_handle,
550 tclient_thread_fn, 0);
553 tm->client_thread_handle = 0;
554 return clib_error_return (0, "pthread_create returned %d", rv);
558 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
559 attach_builtin_test_clients ();
561 /* Fire off connect requests, in something approaching a normal manner */
562 for (i = 0; i < n_clients; i++)
564 vl_api_connect_uri_t *cmp;
565 cmp = vl_msg_api_alloc_as_if_client (sizeof (*cmp));
566 memset (cmp, 0, sizeof (*cmp));
568 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
569 cmp->client_index = tm->my_client_index;
570 cmp->context = ntohl (0xfeedface);
571 memcpy (cmp->uri, uri, strlen ((char *) uri) + 1);
572 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & cmp);
581 #if TCP_BUILTIN_CLIENT_VPP_THREAD
582 VLIB_REGISTER_THREAD (builtin_client_reg, static) =
584 .name = "tcp-builtin-client",
585 .function = tclient_thread_fn,
588 .no_data_structure_clone = 1,
594 VLIB_CLI_COMMAND (test_clients_command, static) =
596 .path = "test tcp clients",
597 .short_help = "test tcp clients [nclients %d]"
598 "[iterations %d] [bytes %d] [uri tcp://6.0.1.1/1234]",
599 .function = test_tcp_clients_command_fn,
604 tcp_test_clients_main_init (vlib_main_t * vm)
606 tclient_main_t *tm = &tclient_main;
611 VLIB_INIT_FUNCTION (tcp_test_clients_main_init);
614 * fd.io coding-style-patch-verification: ON
617 * eval: (c-set-style "gnu")