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 if (TCP_BUILTIN_CLIENT_DBG)
77 ELOG_TYPE_DECLARE (e) =
79 .format = "tx-enq: %d bytes",
87 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
91 /* Account for it... */
92 s->bytes_to_send -= rv;
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);
118 n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (tm->rx_buf),
122 if (TCP_BUILTIN_CLIENT_DBG)
125 ELOG_TYPE_DECLARE (e) =
127 .format = "rx-deq: %d bytes",
135 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
136 ed->data[0] = n_read;
142 for (i = 0; i < n_read; i++)
144 if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff))
146 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
147 n_read, s->bytes_received + i, tm->rx_buf[i],
148 ((s->bytes_received + i) & 0xff));
152 s->bytes_to_receive -= n_read;
153 s->bytes_received += n_read;
157 #if TCP_BUILTIN_CLIENT_VPP_THREAD
162 tclient_thread_fn (void *arg)
164 tclient_main_t *tm = &tclient_main;
165 vl_api_disconnect_session_t *dmp;
167 struct timespec ts, tsrem;
170 u32 *session_indices = 0;
172 /* stats thread wants no signals. */
176 pthread_sigmask (SIG_SETMASK, &s, 0);
179 clib_per_cpu_mheaps[vlib_get_thread_index ()] = clib_per_cpu_mheaps[0];
183 /* Wait until we're told to get busy */
184 while (tm->run_test == 0
185 || (tm->ready_connections != tm->expected_connections))
188 ts.tv_nsec = 100000000;
189 while (nanosleep (&ts, &tsrem) < 0)
194 clib_warning ("Run %d iterations", tm->n_iterations);
196 for (i = 0; i < tm->n_iterations; i++)
205 pool_foreach (sp, tm->sessions, ({
206 if (sp->bytes_to_send > 0)
208 send_test_chunk (tm, sp);
212 pool_foreach (sp, tm->sessions, ({
213 if (sp->bytes_to_receive > 0)
215 receive_test_chunk (tm, sp);
222 while (try_tx || try_rx);
224 clib_warning ("Done %d iterations", tm->n_iterations);
226 /* Disconnect sessions... */
227 vec_reset_length (session_indices);
230 pool_foreach (sp, tm->sessions, ({
231 vec_add1 (session_indices, sp - tm->sessions);
235 for (i = 0; i < vec_len (session_indices); i++)
237 sp = pool_elt_at_index (tm->sessions, session_indices[i]);
238 dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp));
239 memset (dmp, 0, sizeof (*dmp));
240 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
241 dmp->client_index = tm->my_client_index;
242 dmp->handle = sp->vpp_session_handle;
243 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & dmp);
244 pool_put (tm->sessions, sp);
248 #if TCP_BUILTIN_CLIENT_PTHREAD
253 /* So we don't get "no handler for... " msgs */
255 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
257 vlib_main_t *vm = vlib_get_main ();
258 tclient_main_t *tm = &tclient_main;
259 tm->my_client_index = mp->index;
260 vlib_process_signal_event (vm, tm->node_index, 1 /* evt */ , 0 /* data */ );
264 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
266 tclient_main_t *tm = &tclient_main;
269 i32 retval = /* clib_net_to_host_u32 ( */ mp->retval /*) */ ;
273 clib_warning ("connection failed: retval %d", retval);
277 tm->our_event_queue = (unix_shared_memory_queue_t *)
278 mp->vpp_event_queue_address;
280 tm->vpp_event_queue = (unix_shared_memory_queue_t *)
281 mp->vpp_event_queue_address;
286 pool_get (tm->sessions, session);
287 memset (session, 0, sizeof (*session));
288 session_index = session - tm->sessions;
289 session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send;
291 session->server_rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
292 session->server_rx_fifo->client_session_index = session_index;
293 session->server_tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
294 session->server_tx_fifo->client_session_index = session_index;
295 session->vpp_session_handle = mp->handle;
297 /* Add it to the session lookup table */
298 hash_set (tm->session_index_by_vpp_handles, mp->handle, session_index);
300 tm->ready_connections++;
304 create_api_loopback (tclient_main_t * tm)
306 vlib_main_t *vm = vlib_get_main ();
307 vl_api_memclnt_create_t _m, *mp = &_m;
308 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
309 api_main_t *am = &api_main;
310 vl_shmem_hdr_t *shmem_hdr;
311 uword *event_data = 0, event_type;
315 * Create a "loopback" API client connection
316 * Don't do things like this unless you know what you're doing...
319 shmem_hdr = am->shmem_hdr;
320 tm->vl_input_queue = shmem_hdr->vl_input_queue;
321 memset (mp, 0, sizeof (*mp));
322 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
323 mp->context = 0xFEEDFACE;
324 mp->input_queue = (u64) tm->vl_input_queue;
325 strncpy ((char *) mp->name, "tcp_tester", sizeof (mp->name) - 1);
327 vl_api_memclnt_create_t_handler (mp);
330 tm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
331 vlib_process_wait_for_event_or_clock (vm, 1.0);
332 event_type = vlib_process_get_events (vm, &event_data);
342 clib_warning ("unknown event_type %d", event_type);
349 #define foreach_tclient_static_api_msg \
350 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
351 _(CONNECT_URI_REPLY, connect_uri_reply)
353 static clib_error_t *
354 tclient_api_hookup (vlib_main_t * vm)
356 vl_msg_api_msg_config_t _c, *c = &_c;
358 /* Hook up client-side static APIs to our handlers */
359 #define _(N,n) do { \
360 c->id = VL_API_##N; \
362 c->handler = vl_api_##n##_t_handler; \
363 c->cleanup = vl_noop_handler; \
364 c->endian = vl_api_##n##_t_endian; \
365 c->print = vl_api_##n##_t_print; \
366 c->size = sizeof(vl_api_##n##_t); \
367 c->traced = 1; /* trace, so these msgs print */ \
368 c->replay = 0; /* don't replay client create/delete msgs */ \
369 c->message_bounce = 0; /* don't bounce this message */ \
370 vl_msg_api_config(c);} while (0);
372 foreach_tclient_static_api_msg;
379 tcp_test_clients_init (vlib_main_t * vm)
381 tclient_main_t *tm = &tclient_main;
384 tclient_api_hookup (vm);
385 if (create_api_loopback (tm))
389 vec_validate (tm->connect_test_data, 64 * 1024 - 1);
390 for (i = 0; i < vec_len (tm->connect_test_data); i++)
391 tm->connect_test_data[i] = i & 0xff;
393 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
394 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
402 builtin_session_reset_callback (stream_session_t * s)
408 builtin_session_connected_callback (u32 app_index, u32 api_context,
409 stream_session_t * s, u8 code)
415 builtin_session_create_callback (stream_session_t * s)
421 builtin_session_disconnect_callback (stream_session_t * s)
427 builtin_server_rx_callback (stream_session_t * s)
433 static session_cb_vft_t builtin_clients = {
434 .session_reset_callback = builtin_session_reset_callback,
435 .session_connected_callback = builtin_session_connected_callback,
436 .session_accept_callback = builtin_session_create_callback,
437 .session_disconnect_callback = builtin_session_disconnect_callback,
438 .builtin_server_rx_callback = builtin_server_rx_callback
443 attach_builtin_test_clients ()
445 vnet_app_attach_args_t _a, *a = &_a;
446 u8 segment_name[128];
447 u32 segment_name_length;
450 segment_name_length = ARRAY_LEN (segment_name);
452 memset (a, 0, sizeof (*a));
453 memset (options, 0, sizeof (options));
455 a->api_client_index = ~0;
456 a->segment_name = segment_name;
457 a->segment_name_length = segment_name_length;
458 a->session_cb_vft = &builtin_clients;
460 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
461 options[SESSION_OPTIONS_SEGMENT_SIZE] = (2 << 30); /*$$$$ config / arg */
462 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
464 a->options = options;
466 return vnet_application_attach (a);
469 static clib_error_t *
470 test_tcp_clients_command_fn (vlib_main_t * vm,
471 unformat_input_t * input,
472 vlib_cli_command_t * cmd)
474 tclient_main_t *tm = &tclient_main;
475 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
480 tm->bytes_to_send = 8192;
481 tm->n_iterations = 1;
482 vec_free (tm->connect_uri);
484 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
486 if (unformat (input, "nclients %d", &n_clients))
488 else if (unformat (input, "iterations %d", &tm->n_iterations))
490 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
492 else if (unformat (input, "uri %s", &tm->connect_uri))
495 return clib_error_return (0, "unknown input `%U'",
496 format_unformat_error, input);
499 if (tm->is_init == 0)
501 if (tcp_test_clients_init (vm))
502 return clib_error_return (0, "failed init");
505 tm->ready_connections = 0;
506 tm->expected_connections = n_clients;
510 uri = tm->connect_uri;
512 #if TCP_BUILTIN_CLIENT_PTHREAD
513 /* Start a transmit thread */
514 if (tm->client_thread_handle == 0)
516 int rv = pthread_create (&tm->client_thread_handle,
518 tclient_thread_fn, 0);
521 tm->client_thread_handle = 0;
522 return clib_error_return (0, "pthread_create returned %d", rv);
526 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
527 attach_builtin_test_clients ();
529 /* Fire off connect requests, in something approaching a normal manner */
530 for (i = 0; i < n_clients; i++)
532 vl_api_connect_uri_t *cmp;
533 cmp = vl_msg_api_alloc_as_if_client (sizeof (*cmp));
534 memset (cmp, 0, sizeof (*cmp));
536 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
537 cmp->client_index = tm->my_client_index;
538 cmp->context = ntohl (0xfeedface);
539 memcpy (cmp->uri, uri, strlen ((char *) uri) + 1);
540 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & cmp);
548 #if TCP_BUILTIN_CLIENT_VPP_THREAD
550 VLIB_REGISTER_THREAD (builtin_client_reg, static) = {
551 .name = "tcp-builtin-client",
552 .function = tclient_thread_fn,
555 .no_data_structure_clone = 1,
561 VLIB_CLI_COMMAND (test_clients_command, static) =
563 .path = "test tcp clients",
564 .short_help = "test tcp clients [nclients %d] [iterations %d] [bytes %d] [uri tcp://1.2.3.4/1234]",
565 .function = test_tcp_clients_command_fn,
570 tcp_test_clients_main_init (vlib_main_t * vm)
572 tclient_main_t *tm = &tclient_main;
577 VLIB_INIT_FUNCTION (tcp_test_clients_main_init);
580 * fd.io coding-style-patch-verification: ON
583 * eval: (c-set-style "gnu")