blob: 283895102a263ba1eebe04595f5bda9e1efb9b85 [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 */
Florin Corase86a8ed2018-01-05 03:20:25 -080068 svm_queue_t *vl_input_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050069
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 */
Florin Corase86a8ed2018-01-05 03:20:25 -080094 svm_queue_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050095
96 /* $$$ single thread only for the moment */
Florin Corase86a8ed2018-01-05 03:20:25 -080097 svm_queue_t *vpp_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050098
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 Corasff6e7692017-12-11 04:59:01 -0800179 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
180 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
181 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
182 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
183 bmp->options[APP_OPTIONS_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;
Florin Coras84a30ef2018-01-24 10:49:23 -0800205 svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700206 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 =
Florin Corase86a8ed2018-01-05 03:20:25 -0800236 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700237}
238
239static void
240vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
241 mp)
242{
243 if (mp->retval)
244 clib_warning ("detach returned with err: %d", mp->retval);
245}
246
Dave Barach68b0fb02017-02-28 15:15:56 -0500247u8 *
248format_api_error (u8 * s, va_list * args)
249{
250 uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *);
251 i32 error = va_arg (*args, u32);
252 uword *p;
253
254 p = hash_get (utm->error_string_by_error_number, -error);
255
256 if (p)
257 s = format (s, "%s", p[0]);
258 else
259 s = format (s, "%d", error);
260 return s;
261}
262
263int
264wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state)
265{
Florin Corase04c2992017-03-01 08:17:34 -0800266#if CLIB_DEBUG > 0
267#define TIMEOUT 600.0
268#else
269#define TIMEOUT 600.0
270#endif
271
272 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500273
274 while (clib_time_now (&utm->clib_time) < timeout)
275 {
276 if (utm->state == state)
277 return 0;
278 }
279 return -1;
280}
281
Florin Corase04c2992017-03-01 08:17:34 -0800282u64 server_bytes_received, server_bytes_sent;
283
284static void *
285cut_through_thread_fn (void *arg)
286{
287 session_t *s;
288 svm_fifo_t *rx_fifo;
289 svm_fifo_t *tx_fifo;
290 u8 *my_copy_buffer = 0;
291 uri_udp_test_main_t *utm = &uri_udp_test_main;
292 i32 actual_transfer;
293 int rv;
294 u32 buffer_offset;
295
296 while (utm->cut_through_session_index == ~0)
297 ;
298
299 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
300
301 rx_fifo = s->server_rx_fifo;
302 tx_fifo = s->server_tx_fifo;
303
304 vec_validate (my_copy_buffer, 64 * 1024 - 1);
305
306 while (true)
307 {
308 /* We read from the tx fifo and write to the rx fifo */
309 do
310 {
Florin Corasa5464812017-04-19 13:00:05 -0700311 actual_transfer = svm_fifo_dequeue_nowait (tx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800312 vec_len (my_copy_buffer),
313 my_copy_buffer);
314 }
315 while (actual_transfer <= 0);
316
317 server_bytes_received += actual_transfer;
318
319 buffer_offset = 0;
320 while (actual_transfer > 0)
321 {
Florin Corasa5464812017-04-19 13:00:05 -0700322 rv = svm_fifo_enqueue_nowait (rx_fifo, actual_transfer,
Florin Corase04c2992017-03-01 08:17:34 -0800323 my_copy_buffer + buffer_offset);
324 if (rv > 0)
325 {
326 actual_transfer -= rv;
327 buffer_offset += rv;
328 server_bytes_sent += rv;
329 }
330
331 }
332 if (PREDICT_FALSE (utm->time_to_stop))
333 break;
334 }
335
336 pthread_exit (0);
337}
338
339static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700340udp_client_connect (uri_udp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800341{
342 vl_api_connect_uri_t *cmp;
Florin Corase04c2992017-03-01 08:17:34 -0800343 cmp = vl_msg_api_alloc (sizeof (*cmp));
344 memset (cmp, 0, sizeof (*cmp));
345
346 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
347 cmp->client_index = utm->my_client_index;
348 cmp->context = ntohl (0xfeedface);
349 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
350 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700351}
Florin Corase04c2992017-03-01 08:17:34 -0800352
Florin Coras6cf30ad2017-04-04 23:08:23 -0700353static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700354client_send_cut_through (uri_udp_test_main_t * utm, session_t * session)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700355{
356 int i;
357 u8 *test_data = 0;
358 u64 bytes_received = 0, bytes_sent = 0;
359 i32 bytes_to_read;
360 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700361 f64 before, after, delta, bytes_per_second;
362 svm_fifo_t *rx_fifo, *tx_fifo;
363 int buffer_offset, bytes_to_send = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800364
Florin Coras6cf30ad2017-04-04 23:08:23 -0700365 /*
366 * Prepare test data
367 */
368 vec_validate (test_data, 64 * 1024 - 1);
369 for (i = 0; i < vec_len (test_data); i++)
370 test_data[i] = i & 0xff;
371
Florin Corase04c2992017-03-01 08:17:34 -0800372 rx_fifo = session->server_rx_fifo;
373 tx_fifo = session->server_tx_fifo;
374
375 before = clib_time_now (&utm->clib_time);
376
377 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
378
379 for (i = 0; i < NITER; i++)
380 {
381 bytes_to_send = vec_len (test_data);
382 buffer_offset = 0;
383 while (bytes_to_send > 0)
384 {
Florin Corasa5464812017-04-19 13:00:05 -0700385 rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
Florin Corase04c2992017-03-01 08:17:34 -0800386 test_data + buffer_offset);
387
388 if (rv > 0)
389 {
390 bytes_to_send -= rv;
391 buffer_offset += rv;
392 bytes_sent += rv;
393 }
394 }
395
396 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
Florin Corase04c2992017-03-01 08:17:34 -0800397 bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ?
398 bytes_to_read : vec_len (utm->rx_buf);
399
400 buffer_offset = 0;
401 while (bytes_to_read > 0)
402 {
Florin Corasa5464812017-04-19 13:00:05 -0700403 rv = svm_fifo_dequeue_nowait (rx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800404 bytes_to_read,
405 utm->rx_buf + buffer_offset);
406 if (rv > 0)
407 {
408 bytes_to_read -= rv;
409 buffer_offset += rv;
410 bytes_received += rv;
411 }
412 }
413 }
414 while (bytes_received < bytes_sent)
415 {
Florin Corasa5464812017-04-19 13:00:05 -0700416 rv =
417 svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800418 if (rv > 0)
419 {
420#if CLIB_DEBUG > 0
421 int j;
422 for (j = 0; j < rv; j++)
423 {
424 if (utm->rx_buf[j] != ((bytes_received + j) & 0xff))
425 {
426 clib_warning ("error at byte %lld, 0x%x not 0x%x",
427 bytes_received + j,
428 utm->rx_buf[j],
429 ((bytes_received + j) & 0xff));
430 }
431 }
432#endif
433 bytes_received += (u64) rv;
434 }
435 }
436
437 after = clib_time_now (&utm->clib_time);
438 delta = after - before;
439 bytes_per_second = 0.0;
440
441 if (delta > 0.0)
442 bytes_per_second = (f64) bytes_received / delta;
443
444 fformat (stdout,
445 "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n",
446 bytes_received, delta, bytes_per_second);
447 fformat (stdout,
448 "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n",
449 bytes_sent, delta, bytes_per_second);
450 fformat (stdout,
451 "client -> server -> client round trip: %.2f Gbit/sec \n\n",
452 (bytes_per_second * 8.0) / 1e9);
453}
454
Dave Barach68b0fb02017-02-28 15:15:56 -0500455static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700456send_test_chunk (uri_udp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
457 u32 bytes)
458{
459 u8 *test_data = utm->connect_test_data;
460 u64 bytes_sent = 0;
461 int test_buf_offset = 0;
462 u32 bytes_to_snd;
463 u32 queue_max_chunk = 128 << 10, actual_write;
464 session_fifo_event_t evt;
465 int rv;
466
467 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
468 if (bytes_to_snd > vec_len (test_data))
469 bytes_to_snd = vec_len (test_data);
470
471 while (bytes_to_snd > 0 && !utm->time_to_stop)
472 {
473 actual_write = (bytes_to_snd > queue_max_chunk) ?
474 queue_max_chunk : bytes_to_snd;
475 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
476 test_data + test_buf_offset);
477
478 if (rv > 0)
479 {
480 bytes_to_snd -= rv;
481 test_buf_offset += rv;
482 bytes_sent += rv;
483
484 if (svm_fifo_set_event (tx_fifo))
485 {
486 /* Fabricate TX event, send to vpp */
487 evt.fifo = tx_fifo;
488 evt.event_type = FIFO_EVENT_APP_TX;
489
Florin Corase86a8ed2018-01-05 03:20:25 -0800490 svm_queue_add (utm->vpp_event_queue,
491 (u8 *) & evt, 0 /* do wait for mutex */ );
Florin Coras3cbc04b2017-10-02 00:18:51 -0700492 }
493 }
494 }
495}
496
497static void
498recv_test_chunk (uri_udp_test_main_t * utm, session_t * session)
499{
500 svm_fifo_t *rx_fifo;
501 int buffer_offset, bytes_to_read = 0, rv;
502
503 rx_fifo = session->server_rx_fifo;
504 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
505 bytes_to_read =
506 vec_len (utm->rx_buf) > bytes_to_read ?
507 bytes_to_read : vec_len (utm->rx_buf);
508
509 buffer_offset = 0;
510 while (bytes_to_read > 0)
511 {
512 rv = svm_fifo_dequeue_nowait (rx_fifo, bytes_to_read,
513 utm->rx_buf + buffer_offset);
514 if (rv > 0)
515 {
516 bytes_to_read -= rv;
517 buffer_offset += rv;
518 }
519 }
520}
521
522void
523client_send_data (uri_udp_test_main_t * utm)
524{
525 u8 *test_data;
526 int mypid = getpid ();
527 session_t *session;
528 svm_fifo_t *tx_fifo;
529 u32 n_iterations;
530 int i;
531
532 vec_validate (utm->connect_test_data, 64 * 1024 - 1);
533 for (i = 0; i < vec_len (utm->connect_test_data); i++)
534 utm->connect_test_data[i] = i & 0xff;
535
536 test_data = utm->connect_test_data;
537 session = pool_elt_at_index (utm->sessions, utm->connected_session);
538 tx_fifo = session->server_tx_fifo;
539
540 ASSERT (vec_len (test_data) > 0);
541
542 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
543 n_iterations = NITER;
544
545 for (i = 0; i < n_iterations; i++)
546 {
547 send_test_chunk (utm, tx_fifo, mypid, 0);
548 recv_test_chunk (utm, session);
549 if (utm->time_to_stop)
550 break;
551 }
552
553 f64 timeout = clib_time_now (&utm->clib_time) + 5;
554 while (clib_time_now (&utm->clib_time) < timeout)
555 {
556 recv_test_chunk (utm, session);
557 }
558
559}
560
561static void
562client_test (uri_udp_test_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700563{
564 session_t *session;
565
Florin Corasa5464812017-04-19 13:00:05 -0700566 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700567 udp_client_connect (utm);
568
569 if (wait_for_state_change (utm, STATE_READY))
570 {
571 clib_warning ("timeout waiting for STATE_READY");
572 return;
573 }
574
Florin Coras3cbc04b2017-10-02 00:18:51 -0700575 if (utm->cut_through_session_index != ~0)
576 {
577 session = pool_elt_at_index (utm->sessions,
578 utm->cut_through_session_index);
579 client_send_cut_through (utm, session);
580 }
581 else
582 {
583 session = pool_elt_at_index (utm->sessions, utm->connected_session);
584 client_send_data (utm);
585 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700586
Florin Coras6cf30ad2017-04-04 23:08:23 -0700587 application_detach (utm);
588}
589
590static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500591vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
592{
593 uri_udp_test_main_t *utm = &uri_udp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500594
Florin Coras6cf30ad2017-04-04 23:08:23 -0700595 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500596 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700597 clib_warning ("bind failed: %d", mp->retval);
598 utm->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500599 return;
600 }
601
Florin Coras3cbc04b2017-10-02 00:18:51 -0700602 utm->state = STATE_BOUND;
Dave Barach68b0fb02017-02-28 15:15:56 -0500603}
604
605static void
Florin Corase04c2992017-03-01 08:17:34 -0800606vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
607{
608 svm_fifo_segment_create_args_t _a, *a = &_a;
609 int rv;
610
Florin Coras3cbc04b2017-10-02 00:18:51 -0700611 memset (a, 0, sizeof (*a));
Florin Corase04c2992017-03-01 08:17:34 -0800612 a->segment_name = (char *) mp->segment_name;
613 a->segment_size = mp->segment_size;
614 /* Attach to the segment vpp created */
615 rv = svm_fifo_segment_attach (a);
616 if (rv)
617 {
618 clib_warning ("svm_fifo_segment_attach ('%s') failed",
619 mp->segment_name);
620 return;
621 }
622 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
623 mp->segment_size);
624}
625
Florin Coras6cf30ad2017-04-04 23:08:23 -0700626/**
627 * Acting as server for redirected connect requests
628 */
Florin Corase04c2992017-03-01 08:17:34 -0800629static void
630vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
631{
632 u32 segment_index;
633 uri_udp_test_main_t *utm = &uri_udp_test_main;
634 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
635 svm_fifo_segment_create_args_t _a, *a = &_a;
636 svm_fifo_segment_private_t *seg;
Florin Corase86a8ed2018-01-05 03:20:25 -0800637 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -0400638 vl_api_connect_session_reply_t *rmp;
Dave Barach10d8cc62017-05-30 09:30:07 -0400639 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800640 int rv = 0;
641
642 /* Create the segment */
643 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
644 utm->unique_segment_index++, 0);
645 a->segment_size = utm->configured_segment_size;
646
647 rv = svm_fifo_segment_create (a);
648 if (rv)
649 {
650 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
651 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
652 goto send_reply;
653 }
654
655 vec_add2 (utm->seg, seg, 1);
656
657 segment_index = vec_len (sm->segments) - 1;
Florin Corase04c2992017-03-01 08:17:34 -0800658 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
659
660 pool_get (utm->sessions, session);
661
Dave Barach10d8cc62017-05-30 09:30:07 -0400662 session->server_rx_fifo = svm_fifo_segment_alloc_fifo
663 (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
Florin Corase04c2992017-03-01 08:17:34 -0800664 ASSERT (session->server_rx_fifo);
665
Dave Barach10d8cc62017-05-30 09:30:07 -0400666 session->server_tx_fifo = svm_fifo_segment_alloc_fifo
667 (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
Florin Corase04c2992017-03-01 08:17:34 -0800668 ASSERT (session->server_tx_fifo);
669
Florin Corasa5464812017-04-19 13:00:05 -0700670 session->server_rx_fifo->master_session_index = session - utm->sessions;
671 session->server_tx_fifo->master_session_index = session - utm->sessions;
Florin Corase04c2992017-03-01 08:17:34 -0800672 utm->cut_through_session_index = session - utm->sessions;
673
674 rv = pthread_create (&utm->cut_through_thread_handle,
675 NULL /*attr */ , cut_through_thread_fn, 0);
676 if (rv)
677 {
678 clib_warning ("pthread_create returned %d", rv);
679 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
680 }
681
682send_reply:
683 rmp = vl_msg_api_alloc (sizeof (*rmp));
684 memset (rmp, 0, sizeof (*rmp));
685
Dave Wallace33e002b2017-09-06 01:20:02 -0400686 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800687 rmp->context = mp->context;
688 rmp->retval = ntohl (rv);
689 rmp->segment_name_length = vec_len (a->segment_name);
Dave Barach10d8cc62017-05-30 09:30:07 -0400690 if (session)
691 {
692 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
693 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
694 }
695
Florin Corase04c2992017-03-01 08:17:34 -0800696 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
697
698 vec_free (a->segment_name);
699
Florin Corase86a8ed2018-01-05 03:20:25 -0800700 client_q = uword_to_pointer (mp->client_queue_address, svm_queue_t *);
Florin Corase04c2992017-03-01 08:17:34 -0800701 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
702}
703
704static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500705vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
706{
707 uri_udp_test_main_t *utm = &uri_udp_test_main;
708
709 if (mp->retval != 0)
710 clib_warning ("returned %d", ntohl (mp->retval));
711
712 utm->state = STATE_START;
713}
714
715static void
716vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
717{
718 uri_udp_test_main_t *utm = &uri_udp_test_main;
719 vl_api_accept_session_reply_t *rmp;
720 svm_fifo_t *rx_fifo, *tx_fifo;
721 session_t *session;
722 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500723
724 if (start_time == 0.0)
725 start_time = clib_time_now (&utm->clib_time);
726
Damjan Marion7bee80c2017-04-26 15:32:12 +0200727 utm->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -0800728 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500729
730 pool_get (utm->sessions, session);
731
Damjan Marion7bee80c2017-04-26 15:32:12 +0200732 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500733 rx_fifo->client_session_index = session - utm->sessions;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200734 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500735 tx_fifo->client_session_index = session - utm->sessions;
736
737 session->server_rx_fifo = rx_fifo;
738 session->server_tx_fifo = tx_fifo;
739
Florin Coras6cf30ad2017-04-04 23:08:23 -0700740 hash_set (utm->session_index_by_vpp_handles, mp->handle,
741 session - utm->sessions);
Dave Barach68b0fb02017-02-28 15:15:56 -0500742
Dave Barach68b0fb02017-02-28 15:15:56 -0500743 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
744 {
745 f64 now = clib_time_now (&utm->clib_time);
746 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
747 pool_elts (utm->sessions), now - start_time,
748 (f64) pool_elts (utm->sessions) / (now - start_time));
749 }
750
751 rmp = vl_msg_api_alloc (sizeof (*rmp));
752 memset (rmp, 0, sizeof (*rmp));
753 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700754 rmp->handle = mp->handle;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700755 rmp->context = mp->context;
Dave Barach68b0fb02017-02-28 15:15:56 -0500756 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700757
758 CLIB_MEMORY_BARRIER ();
759 utm->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500760}
761
762static void
763vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
764{
765 uri_udp_test_main_t *utm = &uri_udp_test_main;
766 session_t *session;
767 vl_api_disconnect_session_reply_t *rmp;
768 uword *p;
769 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500770
Florin Coras6cf30ad2017-04-04 23:08:23 -0700771 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500772
773 if (p)
774 {
775 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700776 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500777 pool_put (utm->sessions, session);
778 }
779 else
780 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700781 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500782 rv = -11;
783 }
784
785 rmp = vl_msg_api_alloc (sizeof (*rmp));
786 memset (rmp, 0, sizeof (*rmp));
787 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
788 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700789 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500790 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
791}
792
Florin Corase04c2992017-03-01 08:17:34 -0800793static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400794vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800795{
Florin Corase04c2992017-03-01 08:17:34 -0800796 uri_udp_test_main_t *utm = &uri_udp_test_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700797 session_t *session;
Florin Corase04c2992017-03-01 08:17:34 -0800798
799 ASSERT (utm->i_am_master == 0);
800
Florin Coras3cbc04b2017-10-02 00:18:51 -0700801 if (mp->retval)
802 {
803 clib_warning ("failed connect");
804 return;
805 }
806
Florin Coras6cf30ad2017-04-04 23:08:23 -0700807 /* We've been redirected */
808 if (mp->segment_name_length > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800809 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700810 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
811 svm_fifo_segment_create_args_t _a, *a = &_a;
812 u32 segment_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700813 svm_fifo_segment_private_t *seg;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700814 int rv;
815
816 memset (a, 0, sizeof (*a));
817 a->segment_name = (char *) mp->segment_name;
818
819 sleep (1);
820
821 rv = svm_fifo_segment_attach (a);
822 if (rv)
823 {
824 clib_warning ("sm_fifo_segment_create ('%v') failed",
825 mp->segment_name);
826 return;
827 }
828
Dave Barach2c25a622017-06-26 11:35:07 -0400829 segment_index = a->new_segment_indices[0];
Florin Coras6cf30ad2017-04-04 23:08:23 -0700830 vec_add2 (utm->seg, seg, 1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700831 memcpy (seg, sm->segments + segment_index, sizeof (*seg));
Dave Barach10d8cc62017-05-30 09:30:07 -0400832 sleep (1);
Florin Corase04c2992017-03-01 08:17:34 -0800833 }
834
Florin Coras3cbc04b2017-10-02 00:18:51 -0700835 pool_get (utm->sessions, session);
836 session->server_rx_fifo = uword_to_pointer (mp->server_rx_fifo,
837 svm_fifo_t *);
838 ASSERT (session->server_rx_fifo);
839 session->server_tx_fifo = uword_to_pointer (mp->server_tx_fifo,
840 svm_fifo_t *);
841 ASSERT (session->server_tx_fifo);
Florin Corase04c2992017-03-01 08:17:34 -0800842
Florin Coras3cbc04b2017-10-02 00:18:51 -0700843 if (mp->segment_name_length > 0)
844 utm->cut_through_session_index = session - utm->sessions;
845 else
846 {
847 utm->connected_session = session - utm->sessions;
848 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
Florin Corase86a8ed2018-01-05 03:20:25 -0800849 svm_queue_t *);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700850 }
Florin Corase04c2992017-03-01 08:17:34 -0800851 utm->state = STATE_READY;
852}
853
Florin Corasb384b542018-01-15 01:08:33 -0800854#define foreach_tcp_echo_msg \
Florin Coras6cf30ad2017-04-04 23:08:23 -0700855_(BIND_URI_REPLY, bind_uri_reply) \
856_(CONNECT_URI, connect_uri) \
Dave Wallace33e002b2017-09-06 01:20:02 -0400857_(CONNECT_SESSION_REPLY, connect_session_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -0700858_(UNBIND_URI_REPLY, unbind_uri_reply) \
859_(ACCEPT_SESSION, accept_session) \
860_(DISCONNECT_SESSION, disconnect_session) \
861_(MAP_ANOTHER_SEGMENT, map_another_segment) \
862_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
863_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -0500864
865void
Florin Corasb384b542018-01-15 01:08:33 -0800866tcp_echo_api_hookup (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500867{
868#define _(N,n) \
869 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Corase04c2992017-03-01 08:17:34 -0800870 vl_api_##n##_t_handler, \
Dave Barach68b0fb02017-02-28 15:15:56 -0500871 vl_noop_handler, \
872 vl_api_##n##_t_endian, \
873 vl_api_##n##_t_print, \
874 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -0800875 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500876#undef _
877
878}
879
Dave Barach68b0fb02017-02-28 15:15:56 -0500880int
881connect_to_vpp (char *name)
882{
883 uri_udp_test_main_t *utm = &uri_udp_test_main;
884 api_main_t *am = &api_main;
885
886 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
887 return -1;
888
889 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
890 utm->my_client_index = am->my_client_index;
891
892 return 0;
893}
894
895void
896vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
897{
898 clib_warning ("BUG");
899}
900
901static void
902init_error_string_table (uri_udp_test_main_t * utm)
903{
904 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
905
906#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
907 foreach_vnet_api_error;
908#undef _
909
910 hash_set (utm->error_string_by_error_number, 99, "Misc");
911}
912
913void
Florin Corase04c2992017-03-01 08:17:34 -0800914server_handle_fifo_event_rx (uri_udp_test_main_t * utm,
Dave Barach68b0fb02017-02-28 15:15:56 -0500915 session_fifo_event_t * e)
916{
917 svm_fifo_t *rx_fifo, *tx_fifo;
918 int nbytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500919 session_fifo_event_t evt;
Florin Corase86a8ed2018-01-05 03:20:25 -0800920 svm_queue_t *q;
Dave Barach68b0fb02017-02-28 15:15:56 -0500921 int rv;
922
923 rx_fifo = e->fifo;
924 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700925 svm_fifo_unset_event (rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500926
927 do
928 {
Florin Corasa5464812017-04-19 13:00:05 -0700929 nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
930 utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500931 }
932 while (nbytes <= 0);
933 do
934 {
Florin Corasa5464812017-04-19 13:00:05 -0700935 rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500936 }
937 while (rv == -2);
938
Florin Coras6792ec02017-03-13 03:49:51 -0700939 if (svm_fifo_set_event (tx_fifo))
940 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700941 /* Fabricate TX event, send to vpp */
942 evt.fifo = tx_fifo;
943 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -0700944 q = utm->vpp_event_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -0800945 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
Florin Coras6792ec02017-03-13 03:49:51 -0700946 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500947}
948
949void
Florin Corase04c2992017-03-01 08:17:34 -0800950server_handle_event_queue (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500951{
Florin Coras6792ec02017-03-13 03:49:51 -0700952 session_fifo_event_t _e, *e = &_e;
Dave Barach68b0fb02017-02-28 15:15:56 -0500953
Florin Coras3cbc04b2017-10-02 00:18:51 -0700954 while (utm->state != STATE_READY)
955 sleep (5);
956
Dave Barach68b0fb02017-02-28 15:15:56 -0500957 while (1)
958 {
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100959 svm_queue_sub (utm->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500960 switch (e->event_type)
961 {
Florin Corasa5464812017-04-19 13:00:05 -0700962 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800963 server_handle_fifo_event_rx (utm, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500964 break;
965
Florin Corasa5464812017-04-19 13:00:05 -0700966 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500967 return;
968
969 default:
970 clib_warning ("unknown event type %d", e->event_type);
971 break;
972 }
973 if (PREDICT_FALSE (utm->time_to_stop == 1))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700974 return;
Dave Barach68b0fb02017-02-28 15:15:56 -0500975 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
976 {
977 utm->time_to_print_stats = 0;
978 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
979 }
980 }
981}
982
Florin Coras6cf30ad2017-04-04 23:08:23 -0700983static void
984server_unbind (uri_udp_test_main_t * utm)
985{
986 vl_api_unbind_uri_t *ump;
987
988 ump = vl_msg_api_alloc (sizeof (*ump));
989 memset (ump, 0, sizeof (*ump));
990
991 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
992 ump->client_index = utm->my_client_index;
993 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
994 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
995}
996
997static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700998server_bind (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500999{
1000 vl_api_bind_uri_t *bmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001001
1002 bmp = vl_msg_api_alloc (sizeof (*bmp));
1003 memset (bmp, 0, sizeof (*bmp));
1004
1005 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1006 bmp->client_index = utm->my_client_index;
1007 bmp->context = ntohl (0xfeedface);
Dave Barach68b0fb02017-02-28 15:15:56 -05001008 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1009 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001010}
1011
1012void
1013udp_server_test (uri_udp_test_main_t * utm)
1014{
1015
Florin Corasa5464812017-04-19 13:00:05 -07001016 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001017
1018 /* Bind to uri */
Florin Coras3cbc04b2017-10-02 00:18:51 -07001019 server_bind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001020
Florin Coras3cbc04b2017-10-02 00:18:51 -07001021 if (wait_for_state_change (utm, STATE_BOUND))
Dave Barach68b0fb02017-02-28 15:15:56 -05001022 {
Florin Coras3cbc04b2017-10-02 00:18:51 -07001023 clib_warning ("timeout waiting for STATE_BOUND");
Dave Barach68b0fb02017-02-28 15:15:56 -05001024 return;
1025 }
1026
Florin Corase04c2992017-03-01 08:17:34 -08001027 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001028
Florin Coras6cf30ad2017-04-04 23:08:23 -07001029 /* Cleanup */
1030 server_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001031
1032 if (wait_for_state_change (utm, STATE_START))
1033 {
1034 clib_warning ("timeout waiting for STATE_START");
1035 return;
1036 }
1037
Florin Coras6cf30ad2017-04-04 23:08:23 -07001038 application_detach (utm);
1039
Dave Barach68b0fb02017-02-28 15:15:56 -05001040 fformat (stdout, "Test complete...\n");
1041}
1042
1043int
1044main (int argc, char **argv)
1045{
1046 uri_udp_test_main_t *utm = &uri_udp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -08001047 u8 *bind_name = (u8 *) "udp://0.0.0.0/1234";
Florin Corasb384b542018-01-15 01:08:33 -08001048 unformat_input_t _argv, *a = &_argv;
Florin Corase04c2992017-03-01 08:17:34 -08001049 int i_am_master = 1;
Florin Corasb384b542018-01-15 01:08:33 -08001050 session_t *session;
1051 u8 *chroot_prefix;
1052 char *app_name;
1053 mheap_t *h;
1054 u8 *heap;
1055 u32 tmp;
1056 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001057
1058 clib_mem_init (0, 256 << 20);
1059
1060 heap = clib_mem_get_per_cpu_heap ();
1061 h = mheap_header (heap);
1062
1063 /* make the main heap thread-safe */
1064 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1065
1066 vec_validate (utm->rx_buf, 8192);
1067
1068 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Corase04c2992017-03-01 08:17:34 -08001069 utm->my_pid = getpid ();
1070 utm->configured_segment_size = 1 << 20;
1071
Dave Barach68b0fb02017-02-28 15:15:56 -05001072 clib_time_init (&utm->clib_time);
1073 init_error_string_table (utm);
1074 svm_fifo_segment_init (0x200000000ULL, 20);
1075 unformat_init_command_line (a, argv);
1076
1077 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1078 {
1079 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1080 {
1081 vl_set_memory_root_path ((char *) chroot_prefix);
1082 }
1083 else if (unformat (a, "uri %s", &bind_name))
1084 ;
Florin Corase04c2992017-03-01 08:17:34 -08001085 else if (unformat (a, "segment-size %dM", &tmp))
1086 utm->configured_segment_size = tmp << 20;
1087 else if (unformat (a, "segment-size %dG", &tmp))
1088 utm->configured_segment_size = tmp << 30;
1089 else if (unformat (a, "master"))
1090 i_am_master = 1;
1091 else if (unformat (a, "slave"))
1092 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001093 else
1094 {
1095 fformat (stderr, "%s: usage [master|slave]\n");
1096 exit (1);
1097 }
1098 }
1099
Florin Corase04c2992017-03-01 08:17:34 -08001100 utm->cut_through_session_index = ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001101 utm->uri = format (0, "%s%c", bind_name, 0);
Florin Corase04c2992017-03-01 08:17:34 -08001102 utm->i_am_master = i_am_master;
1103 utm->segment_main = &svm_fifo_segment_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001104 utm->connect_uri = format (0, "udp://6.0.1.2/1234%c", 0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001105
1106 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001107 tcp_echo_api_hookup (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001108
Florin Corasb384b542018-01-15 01:08:33 -08001109 app_name = i_am_master ? "udp_echo_master" : "udp_echo_slave";
1110 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001111 {
1112 svm_region_exit ();
1113 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1114 exit (1);
1115 }
1116
Florin Corase04c2992017-03-01 08:17:34 -08001117 if (i_am_master == 0)
1118 {
Florin Coras3cbc04b2017-10-02 00:18:51 -07001119 client_test (utm);
Florin Corase04c2992017-03-01 08:17:34 -08001120 exit (0);
1121 }
1122
Dave Barach68b0fb02017-02-28 15:15:56 -05001123 /* $$$$ hack preallocation */
1124 for (i = 0; i < 200000; i++)
1125 {
1126 pool_get (utm->sessions, session);
1127 memset (session, 0, sizeof (*session));
1128 }
1129 for (i = 0; i < 200000; i++)
1130 pool_put_index (utm->sessions, i);
1131
Florin Coras6cf30ad2017-04-04 23:08:23 -07001132 udp_server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001133
1134 vl_client_disconnect_from_vlib ();
1135 exit (0);
1136}
1137
1138#undef vl_api_version
1139#define vl_api_version(n,v) static u32 vpe_api_version = v;
Florin Corase04c2992017-03-01 08:17:34 -08001140#include <vpp/api/vpe.api.h>
Dave Barach68b0fb02017-02-28 15:15:56 -05001141#undef vl_api_version
1142
1143void
1144vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1145{
1146 /*
1147 * Send the main API signature in slot 0. This bit of code must
1148 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1149 */
1150 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1151}
1152
Florin Corase04c2992017-03-01 08:17:34 -08001153u32
1154vl (void *p)
1155{
1156 return vec_len (p);
1157}
1158
Dave Barach68b0fb02017-02-28 15:15:56 -05001159/*
1160 * fd.io coding-style-patch-verification: ON
1161 *
1162 * Local Variables:
1163 * eval: (c-set-style "gnu")
1164 * End:
1165 */