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;
59 test_buf_offset = s->bytes_sent % vec_len (test_data);
60 bytes_this_chunk = vec_len (test_data) - test_buf_offset;
62 bytes_this_chunk = bytes_this_chunk < s->bytes_to_send
63 ? bytes_this_chunk : s->bytes_to_send;
65 rv = svm_fifo_enqueue_nowait (s->server_tx_fifo, 0 /*pid */ ,
67 test_data + test_buf_offset);
69 /* If we managed to enqueue data... */
72 if (TCP_BUILTIN_CLIENT_DBG)
75 ELOG_TYPE_DECLARE (e) =
77 .format = "tx-enq: %d bytes",
85 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
89 /* Account for it... */
90 s->bytes_to_send -= rv;
93 /* Poke the TCP state machine */
94 if (svm_fifo_set_event (s->server_tx_fifo))
96 /* Fabricate TX event, send to vpp */
97 evt.fifo = s->server_tx_fifo;
98 evt.event_type = FIFO_EVENT_SERVER_TX;
99 evt.event_id = serial_number++;
101 unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt,
102 0 /* do wait for mutex */ );
108 receive_test_chunk (tclient_main_t * tm, session_t * s)
110 svm_fifo_t *rx_fifo = s->server_rx_fifo;
111 int n_read, test_bytes = 0;
113 /* Allow enqueuing of new event */
114 // svm_fifo_unset_event (rx_fifo);
116 n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (tm->rx_buf),
120 if (TCP_BUILTIN_CLIENT_DBG)
123 ELOG_TYPE_DECLARE (e) =
125 .format = "rx-deq: %d bytes",
133 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
134 ed->data[0] = n_read;
140 for (i = 0; i < n_read; i++)
142 if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff))
144 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
145 n_read, s->bytes_received + i, tm->rx_buf[i],
146 ((s->bytes_received + i) & 0xff));
150 s->bytes_to_receive -= n_read;
151 s->bytes_received += n_read;
155 #if TCP_BUILTIN_CLIENT_VPP_THREAD
160 tclient_thread_fn (void *arg)
162 tclient_main_t *tm = &tclient_main;
163 vl_api_disconnect_session_t *dmp;
165 struct timespec ts, tsrem;
168 u32 *session_indices = 0;
170 /* stats thread wants no signals. */
174 pthread_sigmask (SIG_SETMASK, &s, 0);
177 clib_per_cpu_mheaps[vlib_get_thread_index ()] = clib_per_cpu_mheaps[0];
181 /* Wait until we're told to get busy */
182 while (tm->run_test == 0
183 || (tm->ready_connections != tm->expected_connections))
186 ts.tv_nsec = 100000000;
187 while (nanosleep (&ts, &tsrem) < 0)
192 clib_warning ("Run %d iterations", tm->n_iterations);
194 for (i = 0; i < tm->n_iterations; i++)
203 pool_foreach (sp, tm->sessions, ({
204 if (sp->bytes_to_send > 0)
206 send_test_chunk (tm, sp);
210 pool_foreach (sp, tm->sessions, ({
211 if (sp->bytes_to_receive > 0)
213 receive_test_chunk (tm, sp);
220 while (try_tx || try_rx);
222 clib_warning ("Done %d iterations", tm->n_iterations);
224 /* Disconnect sessions... */
225 vec_reset_length (session_indices);
228 pool_foreach (sp, tm->sessions, ({
229 vec_add1 (session_indices, sp - tm->sessions);
233 for (i = 0; i < vec_len (session_indices); i++)
235 sp = pool_elt_at_index (tm->sessions, session_indices[i]);
236 dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp));
237 memset (dmp, 0, sizeof (*dmp));
238 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
239 dmp->client_index = tm->my_client_index;
240 dmp->session_index = sp->vpp_session_index;
241 dmp->session_thread_index = sp->vpp_session_thread;
242 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & dmp);
243 pool_put (tm->sessions, sp);
247 #if TCP_BUILTIN_CLIENT_PTHREAD
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 tclient_main_t *tm = &tclient_main;
258 tm->my_client_index = mp->index;
262 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
264 tclient_main_t *tm = &tclient_main;
268 i32 retval = /* clib_net_to_host_u32 ( */ mp->retval /*) */ ;
272 clib_warning ("connection failed: retval %d", retval);
276 tm->our_event_queue = (unix_shared_memory_queue_t *)
277 mp->vpp_event_queue_address;
279 tm->vpp_event_queue = (unix_shared_memory_queue_t *)
280 mp->vpp_event_queue_address;
285 pool_get (tm->sessions, session);
286 memset (session, 0, sizeof (*session));
287 session_index = session - tm->sessions;
288 session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send;
290 session->server_rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
291 session->server_rx_fifo->client_session_index = session_index;
292 session->server_tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
293 session->server_tx_fifo->client_session_index = session_index;
295 session->vpp_session_index = mp->session_index;
296 session->vpp_session_thread = mp->session_thread_index;
298 /* Add it to the session lookup table */
299 key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
300 hash_set (tm->session_index_by_vpp_handles, key, session_index);
302 tm->ready_connections++;
306 create_api_loopback (tclient_main_t * tm)
308 vl_api_memclnt_create_t _m, *mp = &_m;
309 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
310 api_main_t *am = &api_main;
311 vl_shmem_hdr_t *shmem_hdr;
314 * Create a "loopback" API client connection
315 * Don't do things like this unless you know what you're doing...
318 shmem_hdr = am->shmem_hdr;
319 tm->vl_input_queue = shmem_hdr->vl_input_queue;
320 memset (mp, 0, sizeof (*mp));
321 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
322 mp->context = 0xFEEDFACE;
323 mp->input_queue = (u64) tm->vl_input_queue;
324 strncpy ((char *) mp->name, "tcp_tester", sizeof (mp->name) - 1);
326 vl_api_memclnt_create_t_handler (mp);
329 #define foreach_tclient_static_api_msg \
330 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
331 _(CONNECT_URI_REPLY, connect_uri_reply)
333 static clib_error_t *
334 tclient_api_hookup (vlib_main_t * vm)
336 tclient_main_t *tm = &tclient_main;
337 vl_msg_api_msg_config_t _c, *c = &_c;
341 vec_validate (tm->connect_test_data, 64 * 1024 - 1);
342 for (i = 0; i < vec_len (tm->connect_test_data); i++)
343 tm->connect_test_data[i] = i & 0xff;
345 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
346 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
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;
368 VLIB_API_INIT_FUNCTION (tclient_api_hookup);
370 static clib_error_t *
371 test_tcp_clients_command_fn (vlib_main_t * vm,
372 unformat_input_t * input,
373 vlib_cli_command_t * cmd)
375 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
377 tclient_main_t *tm = &tclient_main;
381 tm->bytes_to_send = 8192;
382 tm->n_iterations = 1;
383 vec_free (tm->connect_uri);
385 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
387 if (unformat (input, "nclients %d", &n_clients))
389 else if (unformat (input, "iterations %d", &tm->n_iterations))
391 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
393 else if (unformat (input, "uri %s", &tm->connect_uri))
396 return clib_error_return (0, "unknown input `%U'",
397 format_unformat_error, input);
400 tm->ready_connections = 0;
401 tm->expected_connections = n_clients;
404 uri = tm->connect_uri;
406 create_api_loopback (tm);
408 #if TCP_BUILTIN_CLIENT_PTHREAD
409 /* Start a transmit thread */
410 if (tm->client_thread_handle == 0)
412 int rv = pthread_create (&tm->client_thread_handle,
414 tclient_thread_fn, 0);
417 tm->client_thread_handle = 0;
418 return clib_error_return (0, "pthread_create returned %d", rv);
423 /* Fire off connect requests, in something approaching a normal manner */
424 for (i = 0; i < n_clients; i++)
426 vl_api_connect_uri_t *cmp;
427 cmp = vl_msg_api_alloc_as_if_client (sizeof (*cmp));
428 memset (cmp, 0, sizeof (*cmp));
430 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
431 cmp->client_index = tm->my_client_index;
432 cmp->context = ntohl (0xfeedface);
433 memcpy (cmp->uri, uri, strlen ((char *) uri) + 1);
434 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & cmp);
442 #if TCP_BUILTIN_CLIENT_VPP_THREAD
444 VLIB_REGISTER_THREAD (builtin_client_reg, static) = {
445 .name = "tcp-builtin-client",
446 .function = tclient_thread_fn,
449 .no_data_structure_clone = 1,
455 VLIB_CLI_COMMAND (test_clients_command, static) =
457 .path = "test tcp clients",
458 .short_help = "test tcp clients",
459 .function = test_tcp_clients_command_fn,
464 * fd.io coding-style-patch-verification: ON
467 * eval: (c-set-style "gnu")