blob: 27e70cf944cf51f511c2e417528fd88dfd37c441 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <stdio.h>
17#include <setjmp.h>
18#include <signal.h>
19#include <vppinfra/clib.h>
20#include <vppinfra/format.h>
21#include <vppinfra/error.h>
22#include <vppinfra/time.h>
23#include <vppinfra/macros.h>
24#include <vnet/vnet.h>
25#include <vlib/vlib.h>
26#include <vlib/unix/unix.h>
27#include <vlibapi/api.h>
28#include <vlibmemory/api.h>
Florin Corase04c2992017-03-01 08:17:34 -080029#include <vpp/api/vpe_msg_enum.h>
30#include <svm/svm_fifo_segment.h>
31#include <pthread.h>
32#include <vnet/session/application_interface.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050033
34#define vl_typedefs /* define message structures */
Florin Corase04c2992017-03-01 08:17:34 -080035#include <vpp/api/vpe_all_api_h.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050036#undef vl_typedefs
37
38/* declare message handlers for each api */
39
40#define vl_endianfun /* define message structures */
Florin Corase04c2992017-03-01 08:17:34 -080041#include <vpp/api/vpe_all_api_h.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050042#undef vl_endianfun
43
44/* instantiate all the print functions we know about */
45#define vl_print(handle, ...)
46#define vl_printfun
Florin Corase04c2992017-03-01 08:17:34 -080047#include <vpp/api/vpe_all_api_h.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050048#undef vl_printfun
49
Dave Barach68b0fb02017-02-28 15:15:56 -050050typedef enum
51{
52 STATE_START,
Florin Coras3cbc04b2017-10-02 00:18:51 -070053 STATE_BOUND,
Dave Barach68b0fb02017-02-28 15:15:56 -050054 STATE_READY,
Florin Coras6cf30ad2017-04-04 23:08:23 -070055 STATE_FAILED,
Dave Barach68b0fb02017-02-28 15:15:56 -050056 STATE_DISCONNECTING,
57} connection_state_t;
58
59typedef struct
60{
61 svm_fifo_t *server_rx_fifo;
62 svm_fifo_t *server_tx_fifo;
63} session_t;
64
65typedef struct
66{
67 /* vpe input queue */
68 unix_shared_memory_queue_t *vl_input_queue;
69
70 /* API client handle */
71 u32 my_client_index;
72
73 /* The URI we're playing with */
74 u8 *uri;
75
76 /* Session pool */
77 session_t *sessions;
78
79 /* Hash table for disconnect processing */
80 uword *session_index_by_vpp_handles;
81
82 /* fifo segment */
83 svm_fifo_segment_private_t *seg;
84
85 /* intermediate rx buffer */
86 u8 *rx_buf;
87
Florin Corase04c2992017-03-01 08:17:34 -080088 /* URI for connect */
89 u8 *connect_uri;
90
91 int i_am_master;
92
Dave Barach68b0fb02017-02-28 15:15:56 -050093 /* Our event queue */
94 unix_shared_memory_queue_t *our_event_queue;
95
96 /* $$$ single thread only for the moment */
97 unix_shared_memory_queue_t *vpp_event_queue;
98
Florin Corase04c2992017-03-01 08:17:34 -080099 /* $$$$ hack: cut-through session index */
100 volatile u32 cut_through_session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700101 volatile u32 connected_session;
Florin Corase04c2992017-03-01 08:17:34 -0800102
103 /* unique segment name counter */
104 u32 unique_segment_index;
105
106 pid_t my_pid;
107
108 /* pthread handle */
109 pthread_t cut_through_thread_handle;
110
Dave Barach68b0fb02017-02-28 15:15:56 -0500111 /* For deadman timers */
112 clib_time_t clib_time;
113
114 /* State of the connection, shared between msg RX thread and main thread */
115 volatile connection_state_t state;
116
117 volatile int time_to_stop;
118 volatile int time_to_print_stats;
119
Florin Corase04c2992017-03-01 08:17:34 -0800120 u32 configured_segment_size;
121
Dave Barach68b0fb02017-02-28 15:15:56 -0500122 /* VNET_API_ERROR_FOO -> "Foo" hash table */
123 uword *error_string_by_error_number;
Florin Corase04c2992017-03-01 08:17:34 -0800124
125 /* convenience */
126 svm_fifo_segment_main_t *segment_main;
127
Florin Coras3cbc04b2017-10-02 00:18:51 -0700128 u8 *connect_test_data;
Dave Barach68b0fb02017-02-28 15:15:56 -0500129} uri_udp_test_main_t;
130
131#if CLIB_DEBUG > 0
Florin Corase04c2992017-03-01 08:17:34 -0800132#define NITER 10000
Dave Barach68b0fb02017-02-28 15:15:56 -0500133#else
Florin Corase04c2992017-03-01 08:17:34 -0800134#define NITER 4000000
Dave Barach68b0fb02017-02-28 15:15:56 -0500135#endif
136
137uri_udp_test_main_t uri_udp_test_main;
138
139static void
140stop_signal (int signum)
141{
142 uri_udp_test_main_t *um = &uri_udp_test_main;
143
144 um->time_to_stop = 1;
145}
146
147static void
148stats_signal (int signum)
149{
150 uri_udp_test_main_t *um = &uri_udp_test_main;
151
152 um->time_to_print_stats = 1;
153}
154
155static clib_error_t *
156setup_signal_handlers (void)
157{
158 signal (SIGINT, stats_signal);
159 signal (SIGQUIT, stop_signal);
160 signal (SIGTERM, stop_signal);
161
162 return 0;
163}
164
Florin Coras6cf30ad2017-04-04 23:08:23 -0700165void
Florin Corasa5464812017-04-19 13:00:05 -0700166application_send_attach (uri_udp_test_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700167{
168 vl_api_application_attach_t *bmp;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700169 u32 fifo_size = 1 << 20;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700170 bmp = vl_msg_api_alloc (sizeof (*bmp));
171 memset (bmp, 0, sizeof (*bmp));
172
173 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
174 bmp->client_index = utm->my_client_index;
175 bmp->context = ntohl (0xfeedface);
Florin Corasa5464812017-04-19 13:00:05 -0700176 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700177 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700178 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700179 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
180 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
181 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
182 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700183 bmp->options[APP_EVT_QUEUE_SIZE] = 16768;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700184 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
185}
186
187void
188application_detach (uri_udp_test_main_t * utm)
189{
190 vl_api_application_detach_t *bmp;
191 bmp = vl_msg_api_alloc (sizeof (*bmp));
192 memset (bmp, 0, sizeof (*bmp));
193
194 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
195 bmp->client_index = utm->my_client_index;
196 bmp->context = ntohl (0xfeedface);
197 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
198}
199
200static void
201vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
202 mp)
203{
204 uri_udp_test_main_t *utm = &uri_udp_test_main;
205 svm_fifo_segment_create_args_t _a, *a = &_a;
206 int rv;
207
208 if (mp->retval)
209 {
210 clib_warning ("attach failed: %d", mp->retval);
211 utm->state = STATE_FAILED;
212 return;
213 }
214
215 if (mp->segment_name_length == 0)
216 {
217 clib_warning ("segment_name_length zero");
218 return;
219 }
220
221 a->segment_name = (char *) mp->segment_name;
222 a->segment_size = mp->segment_size;
223
224 ASSERT (mp->app_event_queue_address);
225
226 /* Attach to the segment vpp created */
227 rv = svm_fifo_segment_attach (a);
228 if (rv)
229 {
230 clib_warning ("svm_fifo_segment_attach ('%s') failed",
231 mp->segment_name);
232 return;
233 }
234
235 utm->our_event_queue =
Damjan Marion7bee80c2017-04-26 15:32:12 +0200236 uword_to_pointer (mp->app_event_queue_address,
237 unix_shared_memory_queue_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700238}
239
240static void
241vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
242 mp)
243{
244 if (mp->retval)
245 clib_warning ("detach returned with err: %d", mp->retval);
246}
247
Dave Barach68b0fb02017-02-28 15:15:56 -0500248u8 *
249format_api_error (u8 * s, va_list * args)
250{
251 uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *);
252 i32 error = va_arg (*args, u32);
253 uword *p;
254
255 p = hash_get (utm->error_string_by_error_number, -error);
256
257 if (p)
258 s = format (s, "%s", p[0]);
259 else
260 s = format (s, "%d", error);
261 return s;
262}
263
264int
265wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state)
266{
Florin Corase04c2992017-03-01 08:17:34 -0800267#if CLIB_DEBUG > 0
268#define TIMEOUT 600.0
269#else
270#define TIMEOUT 600.0
271#endif
272
273 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500274
275 while (clib_time_now (&utm->clib_time) < timeout)
276 {
277 if (utm->state == state)
278 return 0;
279 }
280 return -1;
281}
282
Florin Corase04c2992017-03-01 08:17:34 -0800283u64 server_bytes_received, server_bytes_sent;
284
285static void *
286cut_through_thread_fn (void *arg)
287{
288 session_t *s;
289 svm_fifo_t *rx_fifo;
290 svm_fifo_t *tx_fifo;
291 u8 *my_copy_buffer = 0;
292 uri_udp_test_main_t *utm = &uri_udp_test_main;
293 i32 actual_transfer;
294 int rv;
295 u32 buffer_offset;
296
297 while (utm->cut_through_session_index == ~0)
298 ;
299
300 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
301
302 rx_fifo = s->server_rx_fifo;
303 tx_fifo = s->server_tx_fifo;
304
305 vec_validate (my_copy_buffer, 64 * 1024 - 1);
306
307 while (true)
308 {
309 /* We read from the tx fifo and write to the rx fifo */
310 do
311 {
Florin Corasa5464812017-04-19 13:00:05 -0700312 actual_transfer = svm_fifo_dequeue_nowait (tx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800313 vec_len (my_copy_buffer),
314 my_copy_buffer);
315 }
316 while (actual_transfer <= 0);
317
318 server_bytes_received += actual_transfer;
319
320 buffer_offset = 0;
321 while (actual_transfer > 0)
322 {
Florin Corasa5464812017-04-19 13:00:05 -0700323 rv = svm_fifo_enqueue_nowait (rx_fifo, actual_transfer,
Florin Corase04c2992017-03-01 08:17:34 -0800324 my_copy_buffer + buffer_offset);
325 if (rv > 0)
326 {
327 actual_transfer -= rv;
328 buffer_offset += rv;
329 server_bytes_sent += rv;
330 }
331
332 }
333 if (PREDICT_FALSE (utm->time_to_stop))
334 break;
335 }
336
337 pthread_exit (0);
338}
339
340static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700341udp_client_connect (uri_udp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800342{
343 vl_api_connect_uri_t *cmp;
Florin Corase04c2992017-03-01 08:17:34 -0800344 cmp = vl_msg_api_alloc (sizeof (*cmp));
345 memset (cmp, 0, sizeof (*cmp));
346
347 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
348 cmp->client_index = utm->my_client_index;
349 cmp->context = ntohl (0xfeedface);
350 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
351 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700352}
Florin Corase04c2992017-03-01 08:17:34 -0800353
Florin Coras6cf30ad2017-04-04 23:08:23 -0700354static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700355client_send_cut_through (uri_udp_test_main_t * utm, session_t * session)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700356{
357 int i;
358 u8 *test_data = 0;
359 u64 bytes_received = 0, bytes_sent = 0;
360 i32 bytes_to_read;
361 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700362 f64 before, after, delta, bytes_per_second;
363 svm_fifo_t *rx_fifo, *tx_fifo;
364 int buffer_offset, bytes_to_send = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800365
Florin Coras6cf30ad2017-04-04 23:08:23 -0700366 /*
367 * Prepare test data
368 */
369 vec_validate (test_data, 64 * 1024 - 1);
370 for (i = 0; i < vec_len (test_data); i++)
371 test_data[i] = i & 0xff;
372
Florin Corase04c2992017-03-01 08:17:34 -0800373 rx_fifo = session->server_rx_fifo;
374 tx_fifo = session->server_tx_fifo;
375
376 before = clib_time_now (&utm->clib_time);
377
378 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
379
380 for (i = 0; i < NITER; i++)
381 {
382 bytes_to_send = vec_len (test_data);
383 buffer_offset = 0;
384 while (bytes_to_send > 0)
385 {
Florin Corasa5464812017-04-19 13:00:05 -0700386 rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
Florin Corase04c2992017-03-01 08:17:34 -0800387 test_data + buffer_offset);
388
389 if (rv > 0)
390 {
391 bytes_to_send -= rv;
392 buffer_offset += rv;
393 bytes_sent += rv;
394 }
395 }
396
397 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
Florin Corase04c2992017-03-01 08:17:34 -0800398 bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ?
399 bytes_to_read : vec_len (utm->rx_buf);
400
401 buffer_offset = 0;
402 while (bytes_to_read > 0)
403 {
Florin Corasa5464812017-04-19 13:00:05 -0700404 rv = svm_fifo_dequeue_nowait (rx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800405 bytes_to_read,
406 utm->rx_buf + buffer_offset);
407 if (rv > 0)
408 {
409 bytes_to_read -= rv;
410 buffer_offset += rv;
411 bytes_received += rv;
412 }
413 }
414 }
415 while (bytes_received < bytes_sent)
416 {
Florin Corasa5464812017-04-19 13:00:05 -0700417 rv =
418 svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800419 if (rv > 0)
420 {
421#if CLIB_DEBUG > 0
422 int j;
423 for (j = 0; j < rv; j++)
424 {
425 if (utm->rx_buf[j] != ((bytes_received + j) & 0xff))
426 {
427 clib_warning ("error at byte %lld, 0x%x not 0x%x",
428 bytes_received + j,
429 utm->rx_buf[j],
430 ((bytes_received + j) & 0xff));
431 }
432 }
433#endif
434 bytes_received += (u64) rv;
435 }
436 }
437
438 after = clib_time_now (&utm->clib_time);
439 delta = after - before;
440 bytes_per_second = 0.0;
441
442 if (delta > 0.0)
443 bytes_per_second = (f64) bytes_received / delta;
444
445 fformat (stdout,
446 "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n",
447 bytes_received, delta, bytes_per_second);
448 fformat (stdout,
449 "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n",
450 bytes_sent, delta, bytes_per_second);
451 fformat (stdout,
452 "client -> server -> client round trip: %.2f Gbit/sec \n\n",
453 (bytes_per_second * 8.0) / 1e9);
454}
455
Dave Barach68b0fb02017-02-28 15:15:56 -0500456static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700457send_test_chunk (uri_udp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
458 u32 bytes)
459{
460 u8 *test_data = utm->connect_test_data;
461 u64 bytes_sent = 0;
462 int test_buf_offset = 0;
463 u32 bytes_to_snd;
464 u32 queue_max_chunk = 128 << 10, actual_write;
465 session_fifo_event_t evt;
466 int rv;
467
468 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
469 if (bytes_to_snd > vec_len (test_data))
470 bytes_to_snd = vec_len (test_data);
471
472 while (bytes_to_snd > 0 && !utm->time_to_stop)
473 {
474 actual_write = (bytes_to_snd > queue_max_chunk) ?
475 queue_max_chunk : bytes_to_snd;
476 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
477 test_data + test_buf_offset);
478
479 if (rv > 0)
480 {
481 bytes_to_snd -= rv;
482 test_buf_offset += rv;
483 bytes_sent += rv;
484
485 if (svm_fifo_set_event (tx_fifo))
486 {
487 /* Fabricate TX event, send to vpp */
488 evt.fifo = tx_fifo;
489 evt.event_type = FIFO_EVENT_APP_TX;
490
491 unix_shared_memory_queue_add (utm->vpp_event_queue,
492 (u8 *) & evt,
493 0 /* do wait for mutex */ );
494 }
495 }
496 }
497}
498
499static void
500recv_test_chunk (uri_udp_test_main_t * utm, session_t * session)
501{
502 svm_fifo_t *rx_fifo;
503 int buffer_offset, bytes_to_read = 0, rv;
504
505 rx_fifo = session->server_rx_fifo;
506 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
507 bytes_to_read =
508 vec_len (utm->rx_buf) > bytes_to_read ?
509 bytes_to_read : vec_len (utm->rx_buf);
510
511 buffer_offset = 0;
512 while (bytes_to_read > 0)
513 {
514 rv = svm_fifo_dequeue_nowait (rx_fifo, bytes_to_read,
515 utm->rx_buf + buffer_offset);
516 if (rv > 0)
517 {
518 bytes_to_read -= rv;
519 buffer_offset += rv;
520 }
521 }
522}
523
524void
525client_send_data (uri_udp_test_main_t * utm)
526{
527 u8 *test_data;
528 int mypid = getpid ();
529 session_t *session;
530 svm_fifo_t *tx_fifo;
531 u32 n_iterations;
532 int i;
533
534 vec_validate (utm->connect_test_data, 64 * 1024 - 1);
535 for (i = 0; i < vec_len (utm->connect_test_data); i++)
536 utm->connect_test_data[i] = i & 0xff;
537
538 test_data = utm->connect_test_data;
539 session = pool_elt_at_index (utm->sessions, utm->connected_session);
540 tx_fifo = session->server_tx_fifo;
541
542 ASSERT (vec_len (test_data) > 0);
543
544 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
545 n_iterations = NITER;
546
547 for (i = 0; i < n_iterations; i++)
548 {
549 send_test_chunk (utm, tx_fifo, mypid, 0);
550 recv_test_chunk (utm, session);
551 if (utm->time_to_stop)
552 break;
553 }
554
555 f64 timeout = clib_time_now (&utm->clib_time) + 5;
556 while (clib_time_now (&utm->clib_time) < timeout)
557 {
558 recv_test_chunk (utm, session);
559 }
560
561}
562
563static void
564client_test (uri_udp_test_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700565{
566 session_t *session;
567
Florin Corasa5464812017-04-19 13:00:05 -0700568 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700569 udp_client_connect (utm);
570
571 if (wait_for_state_change (utm, STATE_READY))
572 {
573 clib_warning ("timeout waiting for STATE_READY");
574 return;
575 }
576
Florin Coras3cbc04b2017-10-02 00:18:51 -0700577 if (utm->cut_through_session_index != ~0)
578 {
579 session = pool_elt_at_index (utm->sessions,
580 utm->cut_through_session_index);
581 client_send_cut_through (utm, session);
582 }
583 else
584 {
585 session = pool_elt_at_index (utm->sessions, utm->connected_session);
586 client_send_data (utm);
587 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700588
Florin Coras6cf30ad2017-04-04 23:08:23 -0700589 application_detach (utm);
590}
591
592static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500593vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
594{
595 uri_udp_test_main_t *utm = &uri_udp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500596
Florin Coras6cf30ad2017-04-04 23:08:23 -0700597 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500598 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700599 clib_warning ("bind failed: %d", mp->retval);
600 utm->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500601 return;
602 }
603
Florin Coras3cbc04b2017-10-02 00:18:51 -0700604 utm->state = STATE_BOUND;
Dave Barach68b0fb02017-02-28 15:15:56 -0500605}
606
607static void
Florin Corase04c2992017-03-01 08:17:34 -0800608vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
609{
610 svm_fifo_segment_create_args_t _a, *a = &_a;
611 int rv;
612
Florin Coras3cbc04b2017-10-02 00:18:51 -0700613 memset (a, 0, sizeof (*a));
Florin Corase04c2992017-03-01 08:17:34 -0800614 a->segment_name = (char *) mp->segment_name;
615 a->segment_size = mp->segment_size;
616 /* Attach to the segment vpp created */
617 rv = svm_fifo_segment_attach (a);
618 if (rv)
619 {
620 clib_warning ("svm_fifo_segment_attach ('%s') failed",
621 mp->segment_name);
622 return;
623 }
624 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
625 mp->segment_size);
626}
627
Florin Coras6cf30ad2017-04-04 23:08:23 -0700628/**
629 * Acting as server for redirected connect requests
630 */
Florin Corase04c2992017-03-01 08:17:34 -0800631static void
632vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
633{
634 u32 segment_index;
635 uri_udp_test_main_t *utm = &uri_udp_test_main;
636 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
637 svm_fifo_segment_create_args_t _a, *a = &_a;
638 svm_fifo_segment_private_t *seg;
639 unix_shared_memory_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -0400640 vl_api_connect_session_reply_t *rmp;
Dave Barach10d8cc62017-05-30 09:30:07 -0400641 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800642 int rv = 0;
643
644 /* Create the segment */
645 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
646 utm->unique_segment_index++, 0);
647 a->segment_size = utm->configured_segment_size;
648
649 rv = svm_fifo_segment_create (a);
650 if (rv)
651 {
652 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
653 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
654 goto send_reply;
655 }
656
657 vec_add2 (utm->seg, seg, 1);
658
659 segment_index = vec_len (sm->segments) - 1;
Florin Corase04c2992017-03-01 08:17:34 -0800660 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
661
662 pool_get (utm->sessions, session);
663
Dave Barach10d8cc62017-05-30 09:30:07 -0400664 session->server_rx_fifo = svm_fifo_segment_alloc_fifo
665 (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
Florin Corase04c2992017-03-01 08:17:34 -0800666 ASSERT (session->server_rx_fifo);
667
Dave Barach10d8cc62017-05-30 09:30:07 -0400668 session->server_tx_fifo = svm_fifo_segment_alloc_fifo
669 (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
Florin Corase04c2992017-03-01 08:17:34 -0800670 ASSERT (session->server_tx_fifo);
671
Florin Corasa5464812017-04-19 13:00:05 -0700672 session->server_rx_fifo->master_session_index = session - utm->sessions;
673 session->server_tx_fifo->master_session_index = session - utm->sessions;
Florin Corase04c2992017-03-01 08:17:34 -0800674 utm->cut_through_session_index = session - utm->sessions;
675
676 rv = pthread_create (&utm->cut_through_thread_handle,
677 NULL /*attr */ , cut_through_thread_fn, 0);
678 if (rv)
679 {
680 clib_warning ("pthread_create returned %d", rv);
681 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
682 }
683
684send_reply:
685 rmp = vl_msg_api_alloc (sizeof (*rmp));
686 memset (rmp, 0, sizeof (*rmp));
687
Dave Wallace33e002b2017-09-06 01:20:02 -0400688 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800689 rmp->context = mp->context;
690 rmp->retval = ntohl (rv);
691 rmp->segment_name_length = vec_len (a->segment_name);
Dave Barach10d8cc62017-05-30 09:30:07 -0400692 if (session)
693 {
694 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
695 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
696 }
697
Florin Corase04c2992017-03-01 08:17:34 -0800698 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
699
700 vec_free (a->segment_name);
701
Damjan Marion7bee80c2017-04-26 15:32:12 +0200702 client_q =
703 uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
Florin Corase04c2992017-03-01 08:17:34 -0800704 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
705}
706
707static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500708vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
709{
710 uri_udp_test_main_t *utm = &uri_udp_test_main;
711
712 if (mp->retval != 0)
713 clib_warning ("returned %d", ntohl (mp->retval));
714
715 utm->state = STATE_START;
716}
717
718static void
719vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
720{
721 uri_udp_test_main_t *utm = &uri_udp_test_main;
722 vl_api_accept_session_reply_t *rmp;
723 svm_fifo_t *rx_fifo, *tx_fifo;
724 session_t *session;
725 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500726
727 if (start_time == 0.0)
728 start_time = clib_time_now (&utm->clib_time);
729
Damjan Marion7bee80c2017-04-26 15:32:12 +0200730 utm->vpp_event_queue =
731 uword_to_pointer (mp->vpp_event_queue_address,
732 unix_shared_memory_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500733
734 pool_get (utm->sessions, session);
735
Damjan Marion7bee80c2017-04-26 15:32:12 +0200736 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500737 rx_fifo->client_session_index = session - utm->sessions;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200738 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500739 tx_fifo->client_session_index = session - utm->sessions;
740
741 session->server_rx_fifo = rx_fifo;
742 session->server_tx_fifo = tx_fifo;
743
Florin Coras6cf30ad2017-04-04 23:08:23 -0700744 hash_set (utm->session_index_by_vpp_handles, mp->handle,
745 session - utm->sessions);
Dave Barach68b0fb02017-02-28 15:15:56 -0500746
Dave Barach68b0fb02017-02-28 15:15:56 -0500747 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
748 {
749 f64 now = clib_time_now (&utm->clib_time);
750 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
751 pool_elts (utm->sessions), now - start_time,
752 (f64) pool_elts (utm->sessions) / (now - start_time));
753 }
754
755 rmp = vl_msg_api_alloc (sizeof (*rmp));
756 memset (rmp, 0, sizeof (*rmp));
757 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700758 rmp->handle = mp->handle;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700759 rmp->context = mp->context;
Dave Barach68b0fb02017-02-28 15:15:56 -0500760 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700761
762 CLIB_MEMORY_BARRIER ();
763 utm->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500764}
765
766static void
767vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
768{
769 uri_udp_test_main_t *utm = &uri_udp_test_main;
770 session_t *session;
771 vl_api_disconnect_session_reply_t *rmp;
772 uword *p;
773 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500774
Florin Coras6cf30ad2017-04-04 23:08:23 -0700775 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500776
777 if (p)
778 {
779 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700780 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500781 pool_put (utm->sessions, session);
782 }
783 else
784 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700785 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500786 rv = -11;
787 }
788
789 rmp = vl_msg_api_alloc (sizeof (*rmp));
790 memset (rmp, 0, sizeof (*rmp));
791 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
792 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700793 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500794 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
795}
796
Florin Corase04c2992017-03-01 08:17:34 -0800797static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400798vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800799{
Florin Corase04c2992017-03-01 08:17:34 -0800800 uri_udp_test_main_t *utm = &uri_udp_test_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700801 session_t *session;
Florin Corase04c2992017-03-01 08:17:34 -0800802
803 ASSERT (utm->i_am_master == 0);
804
Florin Coras3cbc04b2017-10-02 00:18:51 -0700805 if (mp->retval)
806 {
807 clib_warning ("failed connect");
808 return;
809 }
810
Florin Coras6cf30ad2017-04-04 23:08:23 -0700811 /* We've been redirected */
812 if (mp->segment_name_length > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800813 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700814 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
815 svm_fifo_segment_create_args_t _a, *a = &_a;
816 u32 segment_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700817 svm_fifo_segment_private_t *seg;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700818 int rv;
819
820 memset (a, 0, sizeof (*a));
821 a->segment_name = (char *) mp->segment_name;
822
823 sleep (1);
824
825 rv = svm_fifo_segment_attach (a);
826 if (rv)
827 {
828 clib_warning ("sm_fifo_segment_create ('%v') failed",
829 mp->segment_name);
830 return;
831 }
832
Dave Barach2c25a622017-06-26 11:35:07 -0400833 segment_index = a->new_segment_indices[0];
Florin Coras6cf30ad2017-04-04 23:08:23 -0700834 vec_add2 (utm->seg, seg, 1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700835 memcpy (seg, sm->segments + segment_index, sizeof (*seg));
Dave Barach10d8cc62017-05-30 09:30:07 -0400836 sleep (1);
Florin Corase04c2992017-03-01 08:17:34 -0800837 }
838
Florin Coras3cbc04b2017-10-02 00:18:51 -0700839 pool_get (utm->sessions, session);
840 session->server_rx_fifo = uword_to_pointer (mp->server_rx_fifo,
841 svm_fifo_t *);
842 ASSERT (session->server_rx_fifo);
843 session->server_tx_fifo = uword_to_pointer (mp->server_tx_fifo,
844 svm_fifo_t *);
845 ASSERT (session->server_tx_fifo);
Florin Corase04c2992017-03-01 08:17:34 -0800846
Florin Coras3cbc04b2017-10-02 00:18:51 -0700847 if (mp->segment_name_length > 0)
848 utm->cut_through_session_index = session - utm->sessions;
849 else
850 {
851 utm->connected_session = session - utm->sessions;
852 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
853 unix_shared_memory_queue_t *);
854 }
Florin Corase04c2992017-03-01 08:17:34 -0800855 utm->state = STATE_READY;
856}
857
Florin Coras6cf30ad2017-04-04 23:08:23 -0700858#define foreach_uri_msg \
859_(BIND_URI_REPLY, bind_uri_reply) \
860_(CONNECT_URI, connect_uri) \
Dave Wallace33e002b2017-09-06 01:20:02 -0400861_(CONNECT_SESSION_REPLY, connect_session_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -0700862_(UNBIND_URI_REPLY, unbind_uri_reply) \
863_(ACCEPT_SESSION, accept_session) \
864_(DISCONNECT_SESSION, disconnect_session) \
865_(MAP_ANOTHER_SEGMENT, map_another_segment) \
866_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
867_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -0500868
869void
870uri_api_hookup (uri_udp_test_main_t * utm)
871{
872#define _(N,n) \
873 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Corase04c2992017-03-01 08:17:34 -0800874 vl_api_##n##_t_handler, \
Dave Barach68b0fb02017-02-28 15:15:56 -0500875 vl_noop_handler, \
876 vl_api_##n##_t_endian, \
877 vl_api_##n##_t_print, \
878 sizeof(vl_api_##n##_t), 1);
879 foreach_uri_msg;
880#undef _
881
882}
883
Dave Barach68b0fb02017-02-28 15:15:56 -0500884int
885connect_to_vpp (char *name)
886{
887 uri_udp_test_main_t *utm = &uri_udp_test_main;
888 api_main_t *am = &api_main;
889
890 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
891 return -1;
892
893 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
894 utm->my_client_index = am->my_client_index;
895
896 return 0;
897}
898
899void
900vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
901{
902 clib_warning ("BUG");
903}
904
905static void
906init_error_string_table (uri_udp_test_main_t * utm)
907{
908 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
909
910#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
911 foreach_vnet_api_error;
912#undef _
913
914 hash_set (utm->error_string_by_error_number, 99, "Misc");
915}
916
917void
Florin Corase04c2992017-03-01 08:17:34 -0800918server_handle_fifo_event_rx (uri_udp_test_main_t * utm,
Dave Barach68b0fb02017-02-28 15:15:56 -0500919 session_fifo_event_t * e)
920{
921 svm_fifo_t *rx_fifo, *tx_fifo;
922 int nbytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500923 session_fifo_event_t evt;
924 unix_shared_memory_queue_t *q;
925 int rv;
926
927 rx_fifo = e->fifo;
928 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700929 svm_fifo_unset_event (rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500930
931 do
932 {
Florin Corasa5464812017-04-19 13:00:05 -0700933 nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
934 utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500935 }
936 while (nbytes <= 0);
937 do
938 {
Florin Corasa5464812017-04-19 13:00:05 -0700939 rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500940 }
941 while (rv == -2);
942
Florin Coras6792ec02017-03-13 03:49:51 -0700943 if (svm_fifo_set_event (tx_fifo))
944 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700945 /* Fabricate TX event, send to vpp */
946 evt.fifo = tx_fifo;
947 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -0700948 q = utm->vpp_event_queue;
949 unix_shared_memory_queue_add (q, (u8 *) & evt,
950 0 /* do wait for mutex */ );
951 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500952}
953
954void
Florin Corase04c2992017-03-01 08:17:34 -0800955server_handle_event_queue (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500956{
Florin Coras6792ec02017-03-13 03:49:51 -0700957 session_fifo_event_t _e, *e = &_e;
Dave Barach68b0fb02017-02-28 15:15:56 -0500958
Florin Coras3cbc04b2017-10-02 00:18:51 -0700959 while (utm->state != STATE_READY)
960 sleep (5);
961
Dave Barach68b0fb02017-02-28 15:15:56 -0500962 while (1)
963 {
964 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
965 0 /* nowait */ );
966 switch (e->event_type)
967 {
Florin Corasa5464812017-04-19 13:00:05 -0700968 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800969 server_handle_fifo_event_rx (utm, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500970 break;
971
Florin Corasa5464812017-04-19 13:00:05 -0700972 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500973 return;
974
975 default:
976 clib_warning ("unknown event type %d", e->event_type);
977 break;
978 }
979 if (PREDICT_FALSE (utm->time_to_stop == 1))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700980 return;
Dave Barach68b0fb02017-02-28 15:15:56 -0500981 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
982 {
983 utm->time_to_print_stats = 0;
984 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
985 }
986 }
987}
988
Florin Coras6cf30ad2017-04-04 23:08:23 -0700989static void
990server_unbind (uri_udp_test_main_t * utm)
991{
992 vl_api_unbind_uri_t *ump;
993
994 ump = vl_msg_api_alloc (sizeof (*ump));
995 memset (ump, 0, sizeof (*ump));
996
997 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
998 ump->client_index = utm->my_client_index;
999 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1000 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1001}
1002
1003static void
Florin Coras3cbc04b2017-10-02 00:18:51 -07001004server_bind (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001005{
1006 vl_api_bind_uri_t *bmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001007
1008 bmp = vl_msg_api_alloc (sizeof (*bmp));
1009 memset (bmp, 0, sizeof (*bmp));
1010
1011 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1012 bmp->client_index = utm->my_client_index;
1013 bmp->context = ntohl (0xfeedface);
Dave Barach68b0fb02017-02-28 15:15:56 -05001014 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1015 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001016}
1017
1018void
1019udp_server_test (uri_udp_test_main_t * utm)
1020{
1021
Florin Corasa5464812017-04-19 13:00:05 -07001022 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001023
1024 /* Bind to uri */
Florin Coras3cbc04b2017-10-02 00:18:51 -07001025 server_bind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001026
Florin Coras3cbc04b2017-10-02 00:18:51 -07001027 if (wait_for_state_change (utm, STATE_BOUND))
Dave Barach68b0fb02017-02-28 15:15:56 -05001028 {
Florin Coras3cbc04b2017-10-02 00:18:51 -07001029 clib_warning ("timeout waiting for STATE_BOUND");
Dave Barach68b0fb02017-02-28 15:15:56 -05001030 return;
1031 }
1032
Florin Corase04c2992017-03-01 08:17:34 -08001033 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001034
Florin Coras6cf30ad2017-04-04 23:08:23 -07001035 /* Cleanup */
1036 server_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001037
1038 if (wait_for_state_change (utm, STATE_START))
1039 {
1040 clib_warning ("timeout waiting for STATE_START");
1041 return;
1042 }
1043
Florin Coras6cf30ad2017-04-04 23:08:23 -07001044 application_detach (utm);
1045
Dave Barach68b0fb02017-02-28 15:15:56 -05001046 fformat (stdout, "Test complete...\n");
1047}
1048
1049int
1050main (int argc, char **argv)
1051{
1052 uri_udp_test_main_t *utm = &uri_udp_test_main;
1053 unformat_input_t _argv, *a = &_argv;
1054 u8 *chroot_prefix;
1055 u8 *heap;
Florin Corase04c2992017-03-01 08:17:34 -08001056 u8 *bind_name = (u8 *) "udp://0.0.0.0/1234";
1057 u32 tmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001058 mheap_t *h;
1059 session_t *session;
1060 int i;
Florin Corase04c2992017-03-01 08:17:34 -08001061 int i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001062
1063 clib_mem_init (0, 256 << 20);
1064
1065 heap = clib_mem_get_per_cpu_heap ();
1066 h = mheap_header (heap);
1067
1068 /* make the main heap thread-safe */
1069 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1070
1071 vec_validate (utm->rx_buf, 8192);
1072
1073 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1074
Florin Corase04c2992017-03-01 08:17:34 -08001075 utm->my_pid = getpid ();
1076 utm->configured_segment_size = 1 << 20;
1077
Dave Barach68b0fb02017-02-28 15:15:56 -05001078 clib_time_init (&utm->clib_time);
1079 init_error_string_table (utm);
1080 svm_fifo_segment_init (0x200000000ULL, 20);
1081 unformat_init_command_line (a, argv);
1082
1083 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1084 {
1085 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1086 {
1087 vl_set_memory_root_path ((char *) chroot_prefix);
1088 }
1089 else if (unformat (a, "uri %s", &bind_name))
1090 ;
Florin Corase04c2992017-03-01 08:17:34 -08001091 else if (unformat (a, "segment-size %dM", &tmp))
1092 utm->configured_segment_size = tmp << 20;
1093 else if (unformat (a, "segment-size %dG", &tmp))
1094 utm->configured_segment_size = tmp << 30;
1095 else if (unformat (a, "master"))
1096 i_am_master = 1;
1097 else if (unformat (a, "slave"))
1098 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001099 else
1100 {
1101 fformat (stderr, "%s: usage [master|slave]\n");
1102 exit (1);
1103 }
1104 }
1105
Florin Corase04c2992017-03-01 08:17:34 -08001106 utm->cut_through_session_index = ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001107 utm->uri = format (0, "%s%c", bind_name, 0);
Florin Corase04c2992017-03-01 08:17:34 -08001108 utm->i_am_master = i_am_master;
1109 utm->segment_main = &svm_fifo_segment_main;
1110
Florin Coras3cbc04b2017-10-02 00:18:51 -07001111 utm->connect_uri = format (0, "udp://6.0.1.2/1234%c", 0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001112
1113 setup_signal_handlers ();
1114
1115 uri_api_hookup (utm);
1116
Florin Corase04c2992017-03-01 08:17:34 -08001117 if (connect_to_vpp (i_am_master ? "uri_udp_master" : "uri_udp_slave") < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001118 {
1119 svm_region_exit ();
1120 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1121 exit (1);
1122 }
1123
Florin Corase04c2992017-03-01 08:17:34 -08001124 if (i_am_master == 0)
1125 {
Florin Coras3cbc04b2017-10-02 00:18:51 -07001126 client_test (utm);
Florin Corase04c2992017-03-01 08:17:34 -08001127 exit (0);
1128 }
1129
Dave Barach68b0fb02017-02-28 15:15:56 -05001130 /* $$$$ hack preallocation */
1131 for (i = 0; i < 200000; i++)
1132 {
1133 pool_get (utm->sessions, session);
1134 memset (session, 0, sizeof (*session));
1135 }
1136 for (i = 0; i < 200000; i++)
1137 pool_put_index (utm->sessions, i);
1138
Florin Coras6cf30ad2017-04-04 23:08:23 -07001139 udp_server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001140
1141 vl_client_disconnect_from_vlib ();
1142 exit (0);
1143}
1144
1145#undef vl_api_version
1146#define vl_api_version(n,v) static u32 vpe_api_version = v;
Florin Corase04c2992017-03-01 08:17:34 -08001147#include <vpp/api/vpe.api.h>
Dave Barach68b0fb02017-02-28 15:15:56 -05001148#undef vl_api_version
1149
1150void
1151vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1152{
1153 /*
1154 * Send the main API signature in slot 0. This bit of code must
1155 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1156 */
1157 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1158}
1159
Florin Corase04c2992017-03-01 08:17:34 -08001160u32
1161vl (void *p)
1162{
1163 return vec_len (p);
1164}
1165
Dave Barach68b0fb02017-02-28 15:15:56 -05001166/*
1167 * fd.io coding-style-patch-verification: ON
1168 *
1169 * Local Variables:
1170 * eval: (c-set-style "gnu")
1171 * End:
1172 */