2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vppinfra/clib.h>
19 #include <vppinfra/format.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/time.h>
22 #include <vppinfra/macros.h>
23 #include <vnet/vnet.h>
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vpp-api/vpe_msg_enum.h>
29 #include <svm_fifo_segment.h>
31 #define vl_typedefs /* define message structures */
32 #include <vpp-api/vpe_all_api_h.h>
35 /* declare message handlers for each api */
37 #define vl_endianfun /* define message structures */
38 #include <vpp-api/vpe_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
44 #include <vpp-api/vpe_all_api_h.h>
57 unix_shared_memory_queue_t *vl_input_queue;
59 /* API client handle */
65 /* The URI we're playing with */
69 svm_fifo_segment_private_t *seg;
74 /* For deadman timers */
75 clib_time_t clib_time;
77 /* State of the connection, shared between msg RX thread and main thread */
78 volatile connection_state_t state;
80 /* VNET_API_ERROR_FOO -> "Foo" hash table */
81 uword *error_string_by_error_number;
90 uritest_main_t uritest_main;
93 format_api_error (u8 * s, va_list * args)
95 uritest_main_t *utm = va_arg (*args, uritest_main_t *);
96 i32 error = va_arg (*args, u32);
99 p = hash_get (utm->error_string_by_error_number, -error);
102 s = format (s, "%s", p[0]);
104 s = format (s, "%d", error);
109 wait_for_state_change (uritest_main_t * utm, connection_state_t state)
111 f64 timeout = clib_time_now (&utm->clib_time) + 1.0;
113 while (clib_time_now (&utm->clib_time) < timeout)
115 if (utm->state == state)
122 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
124 uritest_main_t *utm = &uritest_main;
125 svm_fifo_segment_create_args_t _a, *a = &_a;
128 ASSERT (utm->i_am_master);
130 if (mp->segment_name_length == 0)
132 clib_warning ("segment_name_length zero");
136 a->segment_name = (char *) mp->segment_name;
137 a->segment_size = mp->segment_size;
139 /* Create the segment */
140 rv = svm_fifo_segment_create (a);
143 clib_warning ("sm_fifo_segment_create ('%s') failed", mp->segment_name);
147 vec_validate (utm->seg, 0);
149 memcpy (utm->seg, a->rv, sizeof (*utm->seg));
152 * By construction the master's idea of the rx fifo ends up in
153 * fsh->fifos[0], and the master's idea of the tx fifo ends up in
156 utm->rx_fifo = svm_fifo_segment_alloc_fifo (utm->seg, 10240);
157 ASSERT (utm->rx_fifo);
159 utm->tx_fifo = svm_fifo_segment_alloc_fifo (utm->seg, 10240);
160 ASSERT (utm->tx_fifo);
162 utm->state = STATE_READY;
166 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
168 uritest_main_t *utm = &uritest_main;
169 svm_fifo_segment_create_args_t _a, *a = &_a;
170 ssvm_shared_header_t *sh;
171 svm_fifo_segment_header_t *fsh;
174 ASSERT (utm->i_am_master == 0);
176 if (mp->segment_name_length == 0)
178 clib_warning ("segment_name_length zero");
182 memset (a, 0, sizeof (*a));
184 a->segment_name = (char *) mp->segment_name;
186 rv = svm_fifo_segment_attach (a);
189 clib_warning ("sm_fifo_segment_create ('%s') failed", mp->segment_name);
193 vec_validate (utm->seg, 0);
195 memcpy (utm->seg, a->rv, sizeof (*utm->seg));
196 sh = utm->seg->ssvm.sh;
197 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
199 while (vec_len (fsh->fifos) < 2)
202 utm->rx_fifo = (svm_fifo_t *) fsh->fifos[1];
203 ASSERT (utm->rx_fifo);
204 utm->tx_fifo = (svm_fifo_t *) fsh->fifos[0];
205 ASSERT (utm->tx_fifo);
207 /* security: could unlink /dev/shm/<mp->segment_name> here, maybe */
209 utm->state = STATE_READY;
213 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
215 uritest_main_t *utm = &uritest_main;
218 clib_warning ("returned %d", ntohl (mp->retval));
220 utm->state = STATE_START;
223 #define foreach_uri_msg \
224 _(BIND_URI_REPLY, bind_uri_reply) \
225 _(CONNECT_URI_REPLY, connect_uri_reply) \
226 _(UNBIND_URI_REPLY, unbind_uri_reply)
229 uri_api_hookup (uritest_main_t * utm)
232 vl_msg_api_set_handlers(VL_API_##N, #n, \
233 vl_api_##n##_t_handler, \
235 vl_api_##n##_t_endian, \
236 vl_api_##n##_t_print, \
237 sizeof(vl_api_##n##_t), 1);
245 connect_to_vpp (char *name)
247 uritest_main_t *utm = &uritest_main;
248 api_main_t *am = &api_main;
250 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
253 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
254 utm->my_client_index = am->my_client_index;
260 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
262 clib_warning ("BUG");
266 init_error_string_table (uritest_main_t * utm)
268 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
270 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
271 foreach_vnet_api_error;
274 hash_set (utm->error_string_by_error_number, 99, "Misc");
278 uritest_master (uritest_main_t * utm)
280 vl_api_bind_uri_t *bmp;
281 vl_api_unbind_uri_t *ump;
286 int mypid = getpid ();
288 for (i = 0; i < 2048; i++)
289 vec_add1 (test_data, 'a' + (i % 32));
291 bmp = vl_msg_api_alloc (sizeof (*bmp));
292 memset (bmp, 0, sizeof (*bmp));
294 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
295 bmp->client_index = utm->my_client_index;
296 bmp->context = ntohl (0xfeedface);
297 bmp->segment_size = 256 << 10;
298 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
299 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
301 if (wait_for_state_change (utm, STATE_READY))
303 clib_warning ("timeout waiting for STATE_READY");
307 for (i = 0; i < NITER; i++)
308 svm_fifo_enqueue (utm->tx_fifo, mypid, vec_len (test_data), test_data);
310 vec_validate (reply, 0);
312 reply_len = svm_fifo_dequeue (utm->rx_fifo, mypid, vec_len (reply), reply);
315 clib_warning ("reply length %d", reply_len);
318 fformat (stdout, "Test OK...");
320 ump = vl_msg_api_alloc (sizeof (*ump));
321 memset (ump, 0, sizeof (*ump));
323 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
324 ump->client_index = utm->my_client_index;
325 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
326 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
328 if (wait_for_state_change (utm, STATE_START))
330 clib_warning ("timeout waiting for STATE_READY");
334 fformat (stdout, "Master done...\n");
338 uritest_slave (uritest_main_t * utm)
340 vl_api_connect_uri_t *cmp;
344 u32 bytes_received = 0;
346 int mypid = getpid ();
348 f64 before, after, delta, bytes_per_second;
350 vec_validate (test_data, 4095);
352 cmp = vl_msg_api_alloc (sizeof (*cmp));
353 memset (cmp, 0, sizeof (*cmp));
355 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
356 cmp->client_index = utm->my_client_index;
357 cmp->context = ntohl (0xfeedface);
358 memcpy (cmp->uri, utm->uri, vec_len (utm->uri));
359 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
361 if (wait_for_state_change (utm, STATE_READY))
363 clib_warning ("timeout waiting for STATE_READY");
368 before = clib_time_now (&utm->clib_time);
369 for (i = 0; i < NITER; i++)
371 actual_bytes = svm_fifo_dequeue (utm->rx_fifo, mypid,
372 vec_len (test_data), test_data);
374 while (j < actual_bytes)
376 if (test_data[j] != ('a' + (bytes_received % 32)))
381 if (bytes_received == NITER * 2048)
385 vec_add1 (reply, ok);
387 svm_fifo_enqueue (utm->tx_fifo, mypid, vec_len (reply), reply);
388 after = clib_time_now (&utm->clib_time);
389 delta = after - before;
390 bytes_per_second = 0.0;
393 bytes_per_second = (f64) bytes_received / delta;
396 "Slave done, %d bytes in %.2f seconds, %.2f bytes/sec...\n",
397 bytes_received, delta, bytes_per_second);
401 main (int argc, char **argv)
403 uritest_main_t *utm = &uritest_main;
404 unformat_input_t _argv, *a = &_argv;
407 char *bind_name = "fifo:uritest";
411 clib_mem_init (0, 128 << 20);
413 heap = clib_mem_get_per_cpu_heap ();
414 h = mheap_header (heap);
416 /* make the main heap thread-safe */
417 h->flags |= MHEAP_FLAG_THREAD_SAFE;
419 clib_time_init (&utm->clib_time);
420 init_error_string_table (utm);
421 svm_fifo_segment_init (0x200000000ULL, 20);
422 unformat_init_command_line (a, argv);
424 utm->uri = format (0, "%s%c", bind_name, 0);
426 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
428 if (unformat (a, "master"))
430 else if (unformat (a, "slave"))
432 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
434 vl_set_memory_root_path ((char *) chroot_prefix);
438 fformat (stderr, "%s: usage [master|slave]\n");
443 uri_api_hookup (utm);
445 if (connect_to_vpp (i_am_master ? "uritest_master" : "uritest_slave") < 0)
448 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
452 utm->i_am_master = i_am_master;
455 uritest_master (utm);
459 vl_client_disconnect_from_vlib ();
463 #undef vl_api_version
464 #define vl_api_version(n,v) static u32 vpe_api_version = v;
465 #include <vpp-api/vpe.api.h>
466 #undef vl_api_version
469 vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
472 * Send the main API signature in slot 0. This bit of code must
473 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
475 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
479 * fd.io coding-style-patch-verification: ON
482 * eval: (c-set-style "gnu")