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>
47 send_test_chunk (tclient_main_t * tm, session_t * s)
49 u8 *test_data = tm->connect_test_data;
50 int test_buf_offset = 0;
52 session_fifo_event_t evt;
53 static int serial_number = 0;
56 while (s->bytes_to_send > 0)
58 bytes_this_chunk = vec_len (test_data) < s->bytes_to_send
59 ? vec_len (test_data) : s->bytes_to_send;
61 rv = svm_fifo_enqueue_nowait (s->server_tx_fifo, 0 /*pid */ ,
63 test_data + test_buf_offset);
67 s->bytes_to_send -= rv;
68 test_buf_offset += rv;
70 if (svm_fifo_set_event (s->server_tx_fifo))
72 /* Fabricate TX event, send to vpp */
73 evt.fifo = s->server_tx_fifo;
74 evt.event_type = FIFO_EVENT_SERVER_TX;
75 evt.event_id = serial_number++;
77 unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt,
78 0 /* do wait for mutex */ );
85 receive_test_chunk (tclient_main_t * tm, session_t * s)
87 svm_fifo_t *rx_fifo = s->server_rx_fifo;
90 bytes = svm_fifo_max_dequeue (rx_fifo);
91 /* Allow enqueuing of new event */
92 svm_fifo_unset_event (rx_fifo);
97 n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (tm->rx_buf),
102 for (i = 0; i < n_read; i++)
104 if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff))
106 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
107 n_read, s->bytes_received + i,
109 ((s->bytes_received + i) & 0xff));
112 s->bytes_to_receive -= n_read;
113 s->bytes_received += n_read;
117 while (n_read < 0 || bytes > 0);
121 tclient_thread_fn (void *arg)
123 tclient_main_t *tm = &tclient_main;
124 vl_api_disconnect_session_t *dmp;
126 struct timespec ts, tsrem;
129 u32 *session_indices = 0;
131 /* stats thread wants no signals. */
135 pthread_sigmask (SIG_SETMASK, &s, 0);
140 /* Wait until we're told to get busy */
141 while (tm->run_test == 0
142 || (tm->ready_connections != tm->expected_connections))
145 ts.tv_nsec = 100000000;
146 while (nanosleep (&ts, &tsrem) < 0)
151 clib_warning ("Run %d iterations", tm->n_iterations);
153 for (i = 0; i < tm->n_iterations; i++)
162 pool_foreach (sp, tm->sessions, ({
163 if (sp->bytes_to_send > 0)
165 send_test_chunk (tm, sp);
169 pool_foreach (sp, tm->sessions, ({
170 if (sp->bytes_to_receive > 0)
172 receive_test_chunk (tm, sp);
179 while (try_tx || try_rx);
181 clib_warning ("Done %d iterations", tm->n_iterations);
183 /* Disconnect sessions... */
184 vec_reset_length (session_indices);
185 pool_foreach (sp, tm->sessions, (
187 vec_add1 (session_indices,
192 for (i = 0; i < vec_len (session_indices); i++)
194 sp = pool_elt_at_index (tm->sessions, session_indices[i]);
195 dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp));
196 memset (dmp, 0, sizeof (*dmp));
197 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
198 dmp->client_index = tm->my_client_index;
199 dmp->session_index = sp->vpp_session_index;
200 dmp->session_thread_index = sp->vpp_session_thread;
201 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & dmp);
202 pool_put (tm->sessions, sp);
209 /* So we don't get "no handler for... " msgs */
211 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
213 tclient_main_t *tm = &tclient_main;
215 tm->my_client_index = mp->index;
219 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
221 tclient_main_t *tm = &tclient_main;
225 i32 retval = /* clib_net_to_host_u32 ( */ mp->retval /*) */ ;
229 clib_warning ("connection failed: retval %d", retval);
233 tm->our_event_queue = (unix_shared_memory_queue_t *)
234 mp->vpp_event_queue_address;
236 tm->vpp_event_queue = (unix_shared_memory_queue_t *)
237 mp->vpp_event_queue_address;
242 pool_get (tm->sessions, session);
243 memset (session, 0, sizeof (*session));
244 session_index = session - tm->sessions;
245 session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send;
247 session->server_rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
248 session->server_rx_fifo->client_session_index = session_index;
249 session->server_tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
250 session->server_tx_fifo->client_session_index = session_index;
252 session->vpp_session_index = mp->session_index;
253 session->vpp_session_thread = mp->session_thread_index;
255 /* Add it to the session lookup table */
256 key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
257 hash_set (tm->session_index_by_vpp_handles, key, session_index);
259 tm->ready_connections++;
263 create_api_loopback (tclient_main_t * tm)
265 vl_api_memclnt_create_t _m, *mp = &_m;
266 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
267 api_main_t *am = &api_main;
268 vl_shmem_hdr_t *shmem_hdr;
271 * Create a "loopback" API client connection
272 * Don't do things like this unless you know what you're doing...
275 shmem_hdr = am->shmem_hdr;
276 tm->vl_input_queue = shmem_hdr->vl_input_queue;
277 memset (mp, 0, sizeof (*mp));
278 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
279 mp->context = 0xFEEDFACE;
280 mp->input_queue = (u64) tm->vl_input_queue;
281 strncpy ((char *) mp->name, "tcp_tester", sizeof (mp->name) - 1);
283 vl_api_memclnt_create_t_handler (mp);
286 #define foreach_tclient_static_api_msg \
287 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
288 _(CONNECT_URI_REPLY, connect_uri_reply)
290 static clib_error_t *
291 tclient_api_hookup (vlib_main_t * vm)
293 tclient_main_t *tm = &tclient_main;
294 vl_msg_api_msg_config_t _c, *c = &_c;
298 vec_validate (tm->connect_test_data, 64 * 1024 - 1);
299 for (i = 0; i < vec_len (tm->connect_test_data); i++)
300 tm->connect_test_data[i] = i & 0xff;
302 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
303 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
305 /* Hook up client-side static APIs to our handlers */
306 #define _(N,n) do { \
307 c->id = VL_API_##N; \
309 c->handler = vl_api_##n##_t_handler; \
310 c->cleanup = vl_noop_handler; \
311 c->endian = vl_api_##n##_t_endian; \
312 c->print = vl_api_##n##_t_print; \
313 c->size = sizeof(vl_api_##n##_t); \
314 c->traced = 1; /* trace, so these msgs print */ \
315 c->replay = 0; /* don't replay client create/delete msgs */ \
316 c->message_bounce = 0; /* don't bounce this message */ \
317 vl_msg_api_config(c);} while (0);
319 foreach_tclient_static_api_msg;
325 VLIB_API_INIT_FUNCTION (tclient_api_hookup);
327 static clib_error_t *
328 test_tcp_clients_command_fn (vlib_main_t * vm,
329 unformat_input_t * input,
330 vlib_cli_command_t * cmd)
332 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
334 tclient_main_t *tm = &tclient_main;
338 tm->bytes_to_send = 8192;
339 tm->n_iterations = 1;
340 vec_free (tm->connect_uri);
342 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
344 if (unformat (input, "nclients %d", &n_clients))
346 else if (unformat (input, "iterations %d", &tm->n_iterations))
348 else if (unformat (input, "bytes %d", &tm->bytes_to_send))
350 else if (unformat (input, "uri %s", &tm->connect_uri))
353 return clib_error_return (0, "unknown input `%U'",
354 format_unformat_error, input);
357 tm->ready_connections = 0;
358 tm->expected_connections = n_clients;
361 uri = tm->connect_uri;
363 create_api_loopback (tm);
365 /* Start a transmit thread */
366 if (tm->client_thread_handle == 0)
368 int rv = pthread_create (&tm->client_thread_handle,
369 NULL /*attr */ , tclient_thread_fn, 0);
372 tm->client_thread_handle = 0;
373 return clib_error_return (0, "pthread_create returned %d", rv);
377 /* Fire off connect requests, in something approaching a normal manner */
378 for (i = 0; i < n_clients; i++)
380 vl_api_connect_uri_t *cmp;
381 cmp = vl_msg_api_alloc_as_if_client (sizeof (*cmp));
382 memset (cmp, 0, sizeof (*cmp));
384 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
385 cmp->client_index = tm->my_client_index;
386 cmp->context = ntohl (0xfeedface);
387 memcpy (cmp->uri, uri, strlen ((char *) uri) + 1);
388 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & cmp);
397 VLIB_CLI_COMMAND (test_clients_command, static) =
399 .path = "test tcp clients",
400 .short_help = "test tcp clients",
401 .function = test_tcp_clients_command_fn,
406 * fd.io coding-style-patch-verification: ON
409 * eval: (c-set-style "gnu")