blob: 45ad35a4a5372f910c74d7528d4cc75a545f90f1 [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
50/* Satisfy external references when not linking with -lvlib */
51vlib_main_t vlib_global_main;
52vlib_main_t **vlib_mains;
53
54typedef enum
55{
56 STATE_START,
57 STATE_READY,
Florin Coras6cf30ad2017-04-04 23:08:23 -070058 STATE_FAILED,
Dave Barach68b0fb02017-02-28 15:15:56 -050059 STATE_DISCONNECTING,
60} connection_state_t;
61
62typedef struct
63{
64 svm_fifo_t *server_rx_fifo;
65 svm_fifo_t *server_tx_fifo;
66} session_t;
67
68typedef struct
69{
70 /* vpe input queue */
71 unix_shared_memory_queue_t *vl_input_queue;
72
73 /* API client handle */
74 u32 my_client_index;
75
76 /* The URI we're playing with */
77 u8 *uri;
78
79 /* Session pool */
80 session_t *sessions;
81
82 /* Hash table for disconnect processing */
83 uword *session_index_by_vpp_handles;
84
85 /* fifo segment */
86 svm_fifo_segment_private_t *seg;
87
88 /* intermediate rx buffer */
89 u8 *rx_buf;
90
Florin Corase04c2992017-03-01 08:17:34 -080091 /* URI for connect */
92 u8 *connect_uri;
93
94 int i_am_master;
95
Dave Barach68b0fb02017-02-28 15:15:56 -050096 /* Our event queue */
97 unix_shared_memory_queue_t *our_event_queue;
98
99 /* $$$ single thread only for the moment */
100 unix_shared_memory_queue_t *vpp_event_queue;
101
Florin Corase04c2992017-03-01 08:17:34 -0800102 /* $$$$ hack: cut-through session index */
103 volatile u32 cut_through_session_index;
104
105 /* unique segment name counter */
106 u32 unique_segment_index;
107
108 pid_t my_pid;
109
110 /* pthread handle */
111 pthread_t cut_through_thread_handle;
112
Dave Barach68b0fb02017-02-28 15:15:56 -0500113 /* For deadman timers */
114 clib_time_t clib_time;
115
116 /* State of the connection, shared between msg RX thread and main thread */
117 volatile connection_state_t state;
118
119 volatile int time_to_stop;
120 volatile int time_to_print_stats;
121
Florin Corase04c2992017-03-01 08:17:34 -0800122 u32 configured_segment_size;
123
Dave Barach68b0fb02017-02-28 15:15:56 -0500124 /* VNET_API_ERROR_FOO -> "Foo" hash table */
125 uword *error_string_by_error_number;
Florin Corase04c2992017-03-01 08:17:34 -0800126
127 /* convenience */
128 svm_fifo_segment_main_t *segment_main;
129
Dave Barach68b0fb02017-02-28 15:15:56 -0500130} uri_udp_test_main_t;
131
132#if CLIB_DEBUG > 0
Florin Corase04c2992017-03-01 08:17:34 -0800133#define NITER 10000
Dave Barach68b0fb02017-02-28 15:15:56 -0500134#else
Florin Corase04c2992017-03-01 08:17:34 -0800135#define NITER 4000000
Dave Barach68b0fb02017-02-28 15:15:56 -0500136#endif
137
138uri_udp_test_main_t uri_udp_test_main;
139
140static void
141stop_signal (int signum)
142{
143 uri_udp_test_main_t *um = &uri_udp_test_main;
144
145 um->time_to_stop = 1;
146}
147
148static void
149stats_signal (int signum)
150{
151 uri_udp_test_main_t *um = &uri_udp_test_main;
152
153 um->time_to_print_stats = 1;
154}
155
156static clib_error_t *
157setup_signal_handlers (void)
158{
159 signal (SIGINT, stats_signal);
160 signal (SIGQUIT, stop_signal);
161 signal (SIGTERM, stop_signal);
162
163 return 0;
164}
165
Florin Coras6cf30ad2017-04-04 23:08:23 -0700166void
Florin Corasa5464812017-04-19 13:00:05 -0700167application_send_attach (uri_udp_test_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700168{
169 vl_api_application_attach_t *bmp;
170 u32 fifo_size = 3 << 20;
171 bmp = vl_msg_api_alloc (sizeof (*bmp));
172 memset (bmp, 0, sizeof (*bmp));
173
174 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
175 bmp->client_index = utm->my_client_index;
176 bmp->context = ntohl (0xfeedface);
Florin Corasa5464812017-04-19 13:00:05 -0700177 bmp->options[APP_OPTIONS_FLAGS] =
178 APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT;
Dave Barach10d8cc62017-05-30 09:30:07 -0400179 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700180 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
181 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
182 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
183 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
184 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
355client_send (uri_udp_test_main_t * utm, session_t * session)
356{
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);
398
399 bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ?
400 bytes_to_read : vec_len (utm->rx_buf);
401
402 buffer_offset = 0;
403 while (bytes_to_read > 0)
404 {
Florin Corasa5464812017-04-19 13:00:05 -0700405 rv = svm_fifo_dequeue_nowait (rx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800406 bytes_to_read,
407 utm->rx_buf + buffer_offset);
408 if (rv > 0)
409 {
410 bytes_to_read -= rv;
411 buffer_offset += rv;
412 bytes_received += rv;
413 }
414 }
415 }
416 while (bytes_received < bytes_sent)
417 {
Florin Corasa5464812017-04-19 13:00:05 -0700418 rv =
419 svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800420 if (rv > 0)
421 {
422#if CLIB_DEBUG > 0
423 int j;
424 for (j = 0; j < rv; j++)
425 {
426 if (utm->rx_buf[j] != ((bytes_received + j) & 0xff))
427 {
428 clib_warning ("error at byte %lld, 0x%x not 0x%x",
429 bytes_received + j,
430 utm->rx_buf[j],
431 ((bytes_received + j) & 0xff));
432 }
433 }
434#endif
435 bytes_received += (u64) rv;
436 }
437 }
438
439 after = clib_time_now (&utm->clib_time);
440 delta = after - before;
441 bytes_per_second = 0.0;
442
443 if (delta > 0.0)
444 bytes_per_second = (f64) bytes_received / delta;
445
446 fformat (stdout,
447 "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n",
448 bytes_received, delta, bytes_per_second);
449 fformat (stdout,
450 "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n",
451 bytes_sent, delta, bytes_per_second);
452 fformat (stdout,
453 "client -> server -> client round trip: %.2f Gbit/sec \n\n",
454 (bytes_per_second * 8.0) / 1e9);
455}
456
Dave Barach68b0fb02017-02-28 15:15:56 -0500457static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700458uri_udp_client_test (uri_udp_test_main_t * utm)
459{
460 session_t *session;
461
Florin Corasa5464812017-04-19 13:00:05 -0700462 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700463 udp_client_connect (utm);
464
465 if (wait_for_state_change (utm, STATE_READY))
466 {
467 clib_warning ("timeout waiting for STATE_READY");
468 return;
469 }
470
471 /* Only works with cut through sessions */
472 session = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
473
474 client_send (utm, session);
475 application_detach (utm);
476}
477
478static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500479vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
480{
481 uri_udp_test_main_t *utm = &uri_udp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500482
Florin Coras6cf30ad2017-04-04 23:08:23 -0700483 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500484 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700485 clib_warning ("bind failed: %d", mp->retval);
486 utm->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500487 return;
488 }
489
Dave Barach68b0fb02017-02-28 15:15:56 -0500490 utm->state = STATE_READY;
491}
492
493static void
Florin Corase04c2992017-03-01 08:17:34 -0800494vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
495{
496 svm_fifo_segment_create_args_t _a, *a = &_a;
497 int rv;
498
499 a->segment_name = (char *) mp->segment_name;
500 a->segment_size = mp->segment_size;
501 /* Attach to the segment vpp created */
502 rv = svm_fifo_segment_attach (a);
503 if (rv)
504 {
505 clib_warning ("svm_fifo_segment_attach ('%s') failed",
506 mp->segment_name);
507 return;
508 }
509 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
510 mp->segment_size);
511}
512
Florin Coras6cf30ad2017-04-04 23:08:23 -0700513/**
514 * Acting as server for redirected connect requests
515 */
Florin Corase04c2992017-03-01 08:17:34 -0800516static void
517vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
518{
519 u32 segment_index;
520 uri_udp_test_main_t *utm = &uri_udp_test_main;
521 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
522 svm_fifo_segment_create_args_t _a, *a = &_a;
523 svm_fifo_segment_private_t *seg;
524 unix_shared_memory_queue_t *client_q;
525 vl_api_connect_uri_reply_t *rmp;
Dave Barach10d8cc62017-05-30 09:30:07 -0400526 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800527 int rv = 0;
528
529 /* Create the segment */
530 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
531 utm->unique_segment_index++, 0);
532 a->segment_size = utm->configured_segment_size;
533
534 rv = svm_fifo_segment_create (a);
535 if (rv)
536 {
537 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
538 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
539 goto send_reply;
540 }
541
542 vec_add2 (utm->seg, seg, 1);
543
544 segment_index = vec_len (sm->segments) - 1;
Florin Corase04c2992017-03-01 08:17:34 -0800545 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
546
547 pool_get (utm->sessions, session);
548
Dave Barach10d8cc62017-05-30 09:30:07 -0400549 session->server_rx_fifo = svm_fifo_segment_alloc_fifo
550 (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
Florin Corase04c2992017-03-01 08:17:34 -0800551 ASSERT (session->server_rx_fifo);
552
Dave Barach10d8cc62017-05-30 09:30:07 -0400553 session->server_tx_fifo = svm_fifo_segment_alloc_fifo
554 (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
Florin Corase04c2992017-03-01 08:17:34 -0800555 ASSERT (session->server_tx_fifo);
556
Florin Corasa5464812017-04-19 13:00:05 -0700557 session->server_rx_fifo->master_session_index = session - utm->sessions;
558 session->server_tx_fifo->master_session_index = session - utm->sessions;
Florin Corase04c2992017-03-01 08:17:34 -0800559 utm->cut_through_session_index = session - utm->sessions;
560
561 rv = pthread_create (&utm->cut_through_thread_handle,
562 NULL /*attr */ , cut_through_thread_fn, 0);
563 if (rv)
564 {
565 clib_warning ("pthread_create returned %d", rv);
566 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
567 }
568
569send_reply:
570 rmp = vl_msg_api_alloc (sizeof (*rmp));
571 memset (rmp, 0, sizeof (*rmp));
572
573 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI_REPLY);
574 rmp->context = mp->context;
575 rmp->retval = ntohl (rv);
576 rmp->segment_name_length = vec_len (a->segment_name);
Dave Barach10d8cc62017-05-30 09:30:07 -0400577 if (session)
578 {
579 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
580 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
581 }
582
Florin Corase04c2992017-03-01 08:17:34 -0800583 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
584
585 vec_free (a->segment_name);
586
Damjan Marion7bee80c2017-04-26 15:32:12 +0200587 client_q =
588 uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
Florin Corase04c2992017-03-01 08:17:34 -0800589 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
590}
591
592static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500593vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
594{
595 uri_udp_test_main_t *utm = &uri_udp_test_main;
596
597 if (mp->retval != 0)
598 clib_warning ("returned %d", ntohl (mp->retval));
599
600 utm->state = STATE_START;
601}
602
603static void
604vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
605{
606 uri_udp_test_main_t *utm = &uri_udp_test_main;
607 vl_api_accept_session_reply_t *rmp;
608 svm_fifo_t *rx_fifo, *tx_fifo;
609 session_t *session;
610 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500611
612 if (start_time == 0.0)
613 start_time = clib_time_now (&utm->clib_time);
614
Damjan Marion7bee80c2017-04-26 15:32:12 +0200615 utm->vpp_event_queue =
616 uword_to_pointer (mp->vpp_event_queue_address,
617 unix_shared_memory_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500618
619 pool_get (utm->sessions, session);
620
Damjan Marion7bee80c2017-04-26 15:32:12 +0200621 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500622 rx_fifo->client_session_index = session - utm->sessions;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200623 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500624 tx_fifo->client_session_index = session - utm->sessions;
625
626 session->server_rx_fifo = rx_fifo;
627 session->server_tx_fifo = tx_fifo;
628
Florin Coras6cf30ad2017-04-04 23:08:23 -0700629 hash_set (utm->session_index_by_vpp_handles, mp->handle,
630 session - utm->sessions);
Dave Barach68b0fb02017-02-28 15:15:56 -0500631
632 utm->state = STATE_READY;
633
634 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
635 {
636 f64 now = clib_time_now (&utm->clib_time);
637 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
638 pool_elts (utm->sessions), now - start_time,
639 (f64) pool_elts (utm->sessions) / (now - start_time));
640 }
641
642 rmp = vl_msg_api_alloc (sizeof (*rmp));
643 memset (rmp, 0, sizeof (*rmp));
644 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700645 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500646 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
647}
648
649static void
650vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
651{
652 uri_udp_test_main_t *utm = &uri_udp_test_main;
653 session_t *session;
654 vl_api_disconnect_session_reply_t *rmp;
655 uword *p;
656 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500657
Florin Coras6cf30ad2017-04-04 23:08:23 -0700658 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500659
660 if (p)
661 {
662 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700663 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500664 pool_put (utm->sessions, session);
665 }
666 else
667 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700668 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500669 rv = -11;
670 }
671
672 rmp = vl_msg_api_alloc (sizeof (*rmp));
673 memset (rmp, 0, sizeof (*rmp));
674 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
675 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700676 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500677 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
678}
679
Florin Corase04c2992017-03-01 08:17:34 -0800680static void
681vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
682{
Florin Corase04c2992017-03-01 08:17:34 -0800683 uri_udp_test_main_t *utm = &uri_udp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800684
685 ASSERT (utm->i_am_master == 0);
686
Florin Coras6cf30ad2017-04-04 23:08:23 -0700687 /* We've been redirected */
688 if (mp->segment_name_length > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800689 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700690 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
691 svm_fifo_segment_create_args_t _a, *a = &_a;
692 u32 segment_index;
693 session_t *session;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700694 svm_fifo_segment_private_t *seg;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700695 int rv;
696
697 memset (a, 0, sizeof (*a));
698 a->segment_name = (char *) mp->segment_name;
699
700 sleep (1);
701
702 rv = svm_fifo_segment_attach (a);
703 if (rv)
704 {
705 clib_warning ("sm_fifo_segment_create ('%v') failed",
706 mp->segment_name);
707 return;
708 }
709
Dave Barach10d8cc62017-05-30 09:30:07 -0400710 segment_index = a->new_segment_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700711 vec_add2 (utm->seg, seg, 1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700712 memcpy (seg, sm->segments + segment_index, sizeof (*seg));
Dave Barach10d8cc62017-05-30 09:30:07 -0400713 sleep (1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700714
715 pool_get (utm->sessions, session);
716 utm->cut_through_session_index = session - utm->sessions;
717
Dave Barach10d8cc62017-05-30 09:30:07 -0400718 session->server_rx_fifo = uword_to_pointer (mp->server_rx_fifo,
719 svm_fifo_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700720 ASSERT (session->server_rx_fifo);
Dave Barach10d8cc62017-05-30 09:30:07 -0400721 session->server_tx_fifo = uword_to_pointer (mp->server_tx_fifo,
722 svm_fifo_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700723 ASSERT (session->server_tx_fifo);
Florin Corase04c2992017-03-01 08:17:34 -0800724 }
725
Florin Corase04c2992017-03-01 08:17:34 -0800726 /* security: could unlink /dev/shm/<mp->segment_name> here, maybe */
727
728 utm->state = STATE_READY;
729}
730
Florin Coras6cf30ad2017-04-04 23:08:23 -0700731#define foreach_uri_msg \
732_(BIND_URI_REPLY, bind_uri_reply) \
733_(CONNECT_URI, connect_uri) \
734_(CONNECT_URI_REPLY, connect_uri_reply) \
735_(UNBIND_URI_REPLY, unbind_uri_reply) \
736_(ACCEPT_SESSION, accept_session) \
737_(DISCONNECT_SESSION, disconnect_session) \
738_(MAP_ANOTHER_SEGMENT, map_another_segment) \
739_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
740_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -0500741
742void
743uri_api_hookup (uri_udp_test_main_t * utm)
744{
745#define _(N,n) \
746 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Corase04c2992017-03-01 08:17:34 -0800747 vl_api_##n##_t_handler, \
Dave Barach68b0fb02017-02-28 15:15:56 -0500748 vl_noop_handler, \
749 vl_api_##n##_t_endian, \
750 vl_api_##n##_t_print, \
751 sizeof(vl_api_##n##_t), 1);
752 foreach_uri_msg;
753#undef _
754
755}
756
Dave Barach68b0fb02017-02-28 15:15:56 -0500757int
758connect_to_vpp (char *name)
759{
760 uri_udp_test_main_t *utm = &uri_udp_test_main;
761 api_main_t *am = &api_main;
762
763 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
764 return -1;
765
766 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
767 utm->my_client_index = am->my_client_index;
768
769 return 0;
770}
771
772void
773vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
774{
775 clib_warning ("BUG");
776}
777
778static void
779init_error_string_table (uri_udp_test_main_t * utm)
780{
781 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
782
783#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
784 foreach_vnet_api_error;
785#undef _
786
787 hash_set (utm->error_string_by_error_number, 99, "Misc");
788}
789
790void
Florin Corase04c2992017-03-01 08:17:34 -0800791server_handle_fifo_event_rx (uri_udp_test_main_t * utm,
Dave Barach68b0fb02017-02-28 15:15:56 -0500792 session_fifo_event_t * e)
793{
794 svm_fifo_t *rx_fifo, *tx_fifo;
795 int nbytes;
796
797 session_fifo_event_t evt;
798 unix_shared_memory_queue_t *q;
799 int rv;
800
801 rx_fifo = e->fifo;
802 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
803
804 do
805 {
Florin Corasa5464812017-04-19 13:00:05 -0700806 nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
807 utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500808 }
809 while (nbytes <= 0);
810 do
811 {
Florin Corasa5464812017-04-19 13:00:05 -0700812 rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500813 }
814 while (rv == -2);
815
816 /* Fabricate TX event, send to vpp */
817 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700818 evt.event_type = FIFO_EVENT_APP_TX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500819 evt.event_id = e->event_id;
Florin Coras6792ec02017-03-13 03:49:51 -0700820
821 if (svm_fifo_set_event (tx_fifo))
822 {
823 q = utm->vpp_event_queue;
824 unix_shared_memory_queue_add (q, (u8 *) & evt,
825 0 /* do wait for mutex */ );
826 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500827}
828
829void
Florin Corase04c2992017-03-01 08:17:34 -0800830server_handle_event_queue (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500831{
Florin Coras6792ec02017-03-13 03:49:51 -0700832 session_fifo_event_t _e, *e = &_e;
Dave Barach68b0fb02017-02-28 15:15:56 -0500833
834 while (1)
835 {
836 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
837 0 /* nowait */ );
838 switch (e->event_type)
839 {
Florin Corasa5464812017-04-19 13:00:05 -0700840 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800841 server_handle_fifo_event_rx (utm, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500842 break;
843
Florin Corasa5464812017-04-19 13:00:05 -0700844 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500845 return;
846
847 default:
848 clib_warning ("unknown event type %d", e->event_type);
849 break;
850 }
851 if (PREDICT_FALSE (utm->time_to_stop == 1))
852 break;
853 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
854 {
855 utm->time_to_print_stats = 0;
856 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
857 }
858 }
859}
860
Florin Coras6cf30ad2017-04-04 23:08:23 -0700861static void
862server_unbind (uri_udp_test_main_t * utm)
863{
864 vl_api_unbind_uri_t *ump;
865
866 ump = vl_msg_api_alloc (sizeof (*ump));
867 memset (ump, 0, sizeof (*ump));
868
869 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
870 ump->client_index = utm->my_client_index;
871 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
872 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
873}
874
875static void
876server_listen (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500877{
878 vl_api_bind_uri_t *bmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500879
880 bmp = vl_msg_api_alloc (sizeof (*bmp));
881 memset (bmp, 0, sizeof (*bmp));
882
883 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
884 bmp->client_index = utm->my_client_index;
885 bmp->context = ntohl (0xfeedface);
Dave Barach68b0fb02017-02-28 15:15:56 -0500886 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
887 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700888}
889
890void
891udp_server_test (uri_udp_test_main_t * utm)
892{
893
Florin Corasa5464812017-04-19 13:00:05 -0700894 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700895
896 /* Bind to uri */
897 server_listen (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500898
899 if (wait_for_state_change (utm, STATE_READY))
900 {
901 clib_warning ("timeout waiting for STATE_READY");
902 return;
903 }
904
Florin Corase04c2992017-03-01 08:17:34 -0800905 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500906
Florin Coras6cf30ad2017-04-04 23:08:23 -0700907 /* Cleanup */
908 server_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500909
910 if (wait_for_state_change (utm, STATE_START))
911 {
912 clib_warning ("timeout waiting for STATE_START");
913 return;
914 }
915
Florin Coras6cf30ad2017-04-04 23:08:23 -0700916 application_detach (utm);
917
Dave Barach68b0fb02017-02-28 15:15:56 -0500918 fformat (stdout, "Test complete...\n");
919}
920
921int
922main (int argc, char **argv)
923{
924 uri_udp_test_main_t *utm = &uri_udp_test_main;
925 unformat_input_t _argv, *a = &_argv;
926 u8 *chroot_prefix;
927 u8 *heap;
Florin Corase04c2992017-03-01 08:17:34 -0800928 u8 *bind_name = (u8 *) "udp://0.0.0.0/1234";
929 u32 tmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500930 mheap_t *h;
931 session_t *session;
932 int i;
Florin Corase04c2992017-03-01 08:17:34 -0800933 int i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500934
935 clib_mem_init (0, 256 << 20);
936
937 heap = clib_mem_get_per_cpu_heap ();
938 h = mheap_header (heap);
939
940 /* make the main heap thread-safe */
941 h->flags |= MHEAP_FLAG_THREAD_SAFE;
942
943 vec_validate (utm->rx_buf, 8192);
944
945 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
946
Florin Corase04c2992017-03-01 08:17:34 -0800947 utm->my_pid = getpid ();
948 utm->configured_segment_size = 1 << 20;
949
Dave Barach68b0fb02017-02-28 15:15:56 -0500950 clib_time_init (&utm->clib_time);
951 init_error_string_table (utm);
952 svm_fifo_segment_init (0x200000000ULL, 20);
953 unformat_init_command_line (a, argv);
954
955 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
956 {
957 if (unformat (a, "chroot prefix %s", &chroot_prefix))
958 {
959 vl_set_memory_root_path ((char *) chroot_prefix);
960 }
961 else if (unformat (a, "uri %s", &bind_name))
962 ;
Florin Corase04c2992017-03-01 08:17:34 -0800963 else if (unformat (a, "segment-size %dM", &tmp))
964 utm->configured_segment_size = tmp << 20;
965 else if (unformat (a, "segment-size %dG", &tmp))
966 utm->configured_segment_size = tmp << 30;
967 else if (unformat (a, "master"))
968 i_am_master = 1;
969 else if (unformat (a, "slave"))
970 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500971 else
972 {
973 fformat (stderr, "%s: usage [master|slave]\n");
974 exit (1);
975 }
976 }
977
Florin Corase04c2992017-03-01 08:17:34 -0800978 utm->cut_through_session_index = ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500979 utm->uri = format (0, "%s%c", bind_name, 0);
Florin Corase04c2992017-03-01 08:17:34 -0800980 utm->i_am_master = i_am_master;
981 utm->segment_main = &svm_fifo_segment_main;
982
Florin Coras6cf30ad2017-04-04 23:08:23 -0700983 utm->connect_uri = format (0, "udp://6.0.0.1/1234%c", 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500984
985 setup_signal_handlers ();
986
987 uri_api_hookup (utm);
988
Florin Corase04c2992017-03-01 08:17:34 -0800989 if (connect_to_vpp (i_am_master ? "uri_udp_master" : "uri_udp_slave") < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500990 {
991 svm_region_exit ();
992 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
993 exit (1);
994 }
995
Florin Corase04c2992017-03-01 08:17:34 -0800996 if (i_am_master == 0)
997 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700998 uri_udp_client_test (utm);
Florin Corase04c2992017-03-01 08:17:34 -0800999 exit (0);
1000 }
1001
Dave Barach68b0fb02017-02-28 15:15:56 -05001002 /* $$$$ hack preallocation */
1003 for (i = 0; i < 200000; i++)
1004 {
1005 pool_get (utm->sessions, session);
1006 memset (session, 0, sizeof (*session));
1007 }
1008 for (i = 0; i < 200000; i++)
1009 pool_put_index (utm->sessions, i);
1010
Florin Coras6cf30ad2017-04-04 23:08:23 -07001011 udp_server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001012
1013 vl_client_disconnect_from_vlib ();
1014 exit (0);
1015}
1016
1017#undef vl_api_version
1018#define vl_api_version(n,v) static u32 vpe_api_version = v;
Florin Corase04c2992017-03-01 08:17:34 -08001019#include <vpp/api/vpe.api.h>
Dave Barach68b0fb02017-02-28 15:15:56 -05001020#undef vl_api_version
1021
1022void
1023vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1024{
1025 /*
1026 * Send the main API signature in slot 0. This bit of code must
1027 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1028 */
1029 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1030}
1031
Florin Corase04c2992017-03-01 08:17:34 -08001032u32
1033vl (void *p)
1034{
1035 return vec_len (p);
1036}
1037
Dave Barach68b0fb02017-02-28 15:15:56 -05001038/*
1039 * fd.io coding-style-patch-verification: ON
1040 *
1041 * Local Variables:
1042 * eval: (c-set-style "gnu")
1043 * End:
1044 */