blob: 266215c847c1d82942fcc0cba1036d97372c6b8b [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;
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;
183 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
184}
185
186void
187application_detach (uri_udp_test_main_t * utm)
188{
189 vl_api_application_detach_t *bmp;
190 bmp = vl_msg_api_alloc (sizeof (*bmp));
191 memset (bmp, 0, sizeof (*bmp));
192
193 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
194 bmp->client_index = utm->my_client_index;
195 bmp->context = ntohl (0xfeedface);
196 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
197}
198
199static void
200vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
201 mp)
202{
203 uri_udp_test_main_t *utm = &uri_udp_test_main;
204 svm_fifo_segment_create_args_t _a, *a = &_a;
205 int rv;
206
207 if (mp->retval)
208 {
209 clib_warning ("attach failed: %d", mp->retval);
210 utm->state = STATE_FAILED;
211 return;
212 }
213
214 if (mp->segment_name_length == 0)
215 {
216 clib_warning ("segment_name_length zero");
217 return;
218 }
219
220 a->segment_name = (char *) mp->segment_name;
221 a->segment_size = mp->segment_size;
222
223 ASSERT (mp->app_event_queue_address);
224
225 /* Attach to the segment vpp created */
226 rv = svm_fifo_segment_attach (a);
227 if (rv)
228 {
229 clib_warning ("svm_fifo_segment_attach ('%s') failed",
230 mp->segment_name);
231 return;
232 }
233
234 utm->our_event_queue =
235 (unix_shared_memory_queue_t *) mp->app_event_queue_address;
236}
237
238static void
239vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
240 mp)
241{
242 if (mp->retval)
243 clib_warning ("detach returned with err: %d", mp->retval);
244}
245
Dave Barach68b0fb02017-02-28 15:15:56 -0500246u8 *
247format_api_error (u8 * s, va_list * args)
248{
249 uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *);
250 i32 error = va_arg (*args, u32);
251 uword *p;
252
253 p = hash_get (utm->error_string_by_error_number, -error);
254
255 if (p)
256 s = format (s, "%s", p[0]);
257 else
258 s = format (s, "%d", error);
259 return s;
260}
261
262int
263wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state)
264{
Florin Corase04c2992017-03-01 08:17:34 -0800265#if CLIB_DEBUG > 0
266#define TIMEOUT 600.0
267#else
268#define TIMEOUT 600.0
269#endif
270
271 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500272
273 while (clib_time_now (&utm->clib_time) < timeout)
274 {
275 if (utm->state == state)
276 return 0;
277 }
278 return -1;
279}
280
Florin Corase04c2992017-03-01 08:17:34 -0800281u64 server_bytes_received, server_bytes_sent;
282
283static void *
284cut_through_thread_fn (void *arg)
285{
286 session_t *s;
287 svm_fifo_t *rx_fifo;
288 svm_fifo_t *tx_fifo;
289 u8 *my_copy_buffer = 0;
290 uri_udp_test_main_t *utm = &uri_udp_test_main;
291 i32 actual_transfer;
292 int rv;
293 u32 buffer_offset;
294
295 while (utm->cut_through_session_index == ~0)
296 ;
297
298 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
299
300 rx_fifo = s->server_rx_fifo;
301 tx_fifo = s->server_tx_fifo;
302
303 vec_validate (my_copy_buffer, 64 * 1024 - 1);
304
305 while (true)
306 {
307 /* We read from the tx fifo and write to the rx fifo */
308 do
309 {
Florin Corasa5464812017-04-19 13:00:05 -0700310 actual_transfer = svm_fifo_dequeue_nowait (tx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800311 vec_len (my_copy_buffer),
312 my_copy_buffer);
313 }
314 while (actual_transfer <= 0);
315
316 server_bytes_received += actual_transfer;
317
318 buffer_offset = 0;
319 while (actual_transfer > 0)
320 {
Florin Corasa5464812017-04-19 13:00:05 -0700321 rv = svm_fifo_enqueue_nowait (rx_fifo, actual_transfer,
Florin Corase04c2992017-03-01 08:17:34 -0800322 my_copy_buffer + buffer_offset);
323 if (rv > 0)
324 {
325 actual_transfer -= rv;
326 buffer_offset += rv;
327 server_bytes_sent += rv;
328 }
329
330 }
331 if (PREDICT_FALSE (utm->time_to_stop))
332 break;
333 }
334
335 pthread_exit (0);
336}
337
338static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700339udp_client_connect (uri_udp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800340{
341 vl_api_connect_uri_t *cmp;
Florin Corase04c2992017-03-01 08:17:34 -0800342 cmp = vl_msg_api_alloc (sizeof (*cmp));
343 memset (cmp, 0, sizeof (*cmp));
344
345 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
346 cmp->client_index = utm->my_client_index;
347 cmp->context = ntohl (0xfeedface);
348 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
349 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700350}
Florin Corase04c2992017-03-01 08:17:34 -0800351
Florin Coras6cf30ad2017-04-04 23:08:23 -0700352static void
353client_send (uri_udp_test_main_t * utm, session_t * session)
354{
355 int i;
356 u8 *test_data = 0;
357 u64 bytes_received = 0, bytes_sent = 0;
358 i32 bytes_to_read;
359 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700360 f64 before, after, delta, bytes_per_second;
361 svm_fifo_t *rx_fifo, *tx_fifo;
362 int buffer_offset, bytes_to_send = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800363
Florin Coras6cf30ad2017-04-04 23:08:23 -0700364 /*
365 * Prepare test data
366 */
367 vec_validate (test_data, 64 * 1024 - 1);
368 for (i = 0; i < vec_len (test_data); i++)
369 test_data[i] = i & 0xff;
370
Florin Corase04c2992017-03-01 08:17:34 -0800371 rx_fifo = session->server_rx_fifo;
372 tx_fifo = session->server_tx_fifo;
373
374 before = clib_time_now (&utm->clib_time);
375
376 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
377
378 for (i = 0; i < NITER; i++)
379 {
380 bytes_to_send = vec_len (test_data);
381 buffer_offset = 0;
382 while (bytes_to_send > 0)
383 {
Florin Corasa5464812017-04-19 13:00:05 -0700384 rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
Florin Corase04c2992017-03-01 08:17:34 -0800385 test_data + buffer_offset);
386
387 if (rv > 0)
388 {
389 bytes_to_send -= rv;
390 buffer_offset += rv;
391 bytes_sent += rv;
392 }
393 }
394
395 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
396
397 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 Coras6cf30ad2017-04-04 23:08:23 -0700456uri_udp_client_test (uri_udp_test_main_t * utm)
457{
458 session_t *session;
459
Florin Corasa5464812017-04-19 13:00:05 -0700460 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700461 udp_client_connect (utm);
462
463 if (wait_for_state_change (utm, STATE_READY))
464 {
465 clib_warning ("timeout waiting for STATE_READY");
466 return;
467 }
468
469 /* Only works with cut through sessions */
470 session = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
471
472 client_send (utm, session);
473 application_detach (utm);
474}
475
476static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500477vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
478{
479 uri_udp_test_main_t *utm = &uri_udp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500480
Florin Coras6cf30ad2017-04-04 23:08:23 -0700481 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500482 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700483 clib_warning ("bind failed: %d", mp->retval);
484 utm->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500485 return;
486 }
487
Dave Barach68b0fb02017-02-28 15:15:56 -0500488 utm->state = STATE_READY;
489}
490
491static void
Florin Corase04c2992017-03-01 08:17:34 -0800492vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
493{
494 svm_fifo_segment_create_args_t _a, *a = &_a;
495 int rv;
496
497 a->segment_name = (char *) mp->segment_name;
498 a->segment_size = mp->segment_size;
499 /* Attach to the segment vpp created */
500 rv = svm_fifo_segment_attach (a);
501 if (rv)
502 {
503 clib_warning ("svm_fifo_segment_attach ('%s') failed",
504 mp->segment_name);
505 return;
506 }
507 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
508 mp->segment_size);
509}
510
Florin Coras6cf30ad2017-04-04 23:08:23 -0700511/**
512 * Acting as server for redirected connect requests
513 */
Florin Corase04c2992017-03-01 08:17:34 -0800514static void
515vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
516{
517 u32 segment_index;
518 uri_udp_test_main_t *utm = &uri_udp_test_main;
519 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
520 svm_fifo_segment_create_args_t _a, *a = &_a;
521 svm_fifo_segment_private_t *seg;
522 unix_shared_memory_queue_t *client_q;
523 vl_api_connect_uri_reply_t *rmp;
524 session_t *session;
525 int rv = 0;
526
527 /* Create the segment */
528 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
529 utm->unique_segment_index++, 0);
530 a->segment_size = utm->configured_segment_size;
531
532 rv = svm_fifo_segment_create (a);
533 if (rv)
534 {
535 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
536 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
537 goto send_reply;
538 }
539
540 vec_add2 (utm->seg, seg, 1);
541
542 segment_index = vec_len (sm->segments) - 1;
Florin Corase04c2992017-03-01 08:17:34 -0800543 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
544
545 pool_get (utm->sessions, session);
546
547 /*
548 * By construction the master's idea of the rx fifo ends up in
549 * fsh->fifos[0], and the master's idea of the tx fifo ends up in
550 * fsh->fifos[1].
551 */
552 session->server_rx_fifo = svm_fifo_segment_alloc_fifo (utm->seg,
553 128 * 1024);
554 ASSERT (session->server_rx_fifo);
555
556 session->server_tx_fifo = svm_fifo_segment_alloc_fifo (utm->seg,
557 128 * 1024);
558 ASSERT (session->server_tx_fifo);
559
Florin Corasa5464812017-04-19 13:00:05 -0700560 session->server_rx_fifo->master_session_index = session - utm->sessions;
561 session->server_tx_fifo->master_session_index = session - utm->sessions;
Florin Corase04c2992017-03-01 08:17:34 -0800562 utm->cut_through_session_index = session - utm->sessions;
563
564 rv = pthread_create (&utm->cut_through_thread_handle,
565 NULL /*attr */ , cut_through_thread_fn, 0);
566 if (rv)
567 {
568 clib_warning ("pthread_create returned %d", rv);
569 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
570 }
571
572send_reply:
573 rmp = vl_msg_api_alloc (sizeof (*rmp));
574 memset (rmp, 0, sizeof (*rmp));
575
576 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI_REPLY);
577 rmp->context = mp->context;
578 rmp->retval = ntohl (rv);
579 rmp->segment_name_length = vec_len (a->segment_name);
580 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
581
582 vec_free (a->segment_name);
583
584 client_q = (unix_shared_memory_queue_t *) mp->client_queue_address;
585 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
586}
587
588static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500589vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
590{
591 uri_udp_test_main_t *utm = &uri_udp_test_main;
592
593 if (mp->retval != 0)
594 clib_warning ("returned %d", ntohl (mp->retval));
595
596 utm->state = STATE_START;
597}
598
599static void
600vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
601{
602 uri_udp_test_main_t *utm = &uri_udp_test_main;
603 vl_api_accept_session_reply_t *rmp;
604 svm_fifo_t *rx_fifo, *tx_fifo;
605 session_t *session;
606 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500607
608 if (start_time == 0.0)
609 start_time = clib_time_now (&utm->clib_time);
610
611 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
612 mp->vpp_event_queue_address;
613
614 pool_get (utm->sessions, session);
615
616 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
617 rx_fifo->client_session_index = session - utm->sessions;
618 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
619 tx_fifo->client_session_index = session - utm->sessions;
620
621 session->server_rx_fifo = rx_fifo;
622 session->server_tx_fifo = tx_fifo;
623
Florin Coras6cf30ad2017-04-04 23:08:23 -0700624 hash_set (utm->session_index_by_vpp_handles, mp->handle,
625 session - utm->sessions);
Dave Barach68b0fb02017-02-28 15:15:56 -0500626
627 utm->state = STATE_READY;
628
629 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
630 {
631 f64 now = clib_time_now (&utm->clib_time);
632 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
633 pool_elts (utm->sessions), now - start_time,
634 (f64) pool_elts (utm->sessions) / (now - start_time));
635 }
636
637 rmp = vl_msg_api_alloc (sizeof (*rmp));
638 memset (rmp, 0, sizeof (*rmp));
639 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700640 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500641 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
642}
643
644static void
645vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
646{
647 uri_udp_test_main_t *utm = &uri_udp_test_main;
648 session_t *session;
649 vl_api_disconnect_session_reply_t *rmp;
650 uword *p;
651 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500652
Florin Coras6cf30ad2017-04-04 23:08:23 -0700653 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500654
655 if (p)
656 {
657 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700658 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500659 pool_put (utm->sessions, session);
660 }
661 else
662 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700663 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500664 rv = -11;
665 }
666
667 rmp = vl_msg_api_alloc (sizeof (*rmp));
668 memset (rmp, 0, sizeof (*rmp));
669 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
670 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700671 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500672 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
673}
674
Florin Corase04c2992017-03-01 08:17:34 -0800675static void
676vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
677{
Florin Corase04c2992017-03-01 08:17:34 -0800678 uri_udp_test_main_t *utm = &uri_udp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800679
680 ASSERT (utm->i_am_master == 0);
681
Florin Coras6cf30ad2017-04-04 23:08:23 -0700682 /* We've been redirected */
683 if (mp->segment_name_length > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800684 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700685 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
686 svm_fifo_segment_create_args_t _a, *a = &_a;
687 u32 segment_index;
688 session_t *session;
689 ssvm_shared_header_t *sh;
690 svm_fifo_segment_private_t *seg;
691 svm_fifo_segment_header_t *fsh;
692 int rv;
693
694 memset (a, 0, sizeof (*a));
695 a->segment_name = (char *) mp->segment_name;
696
697 sleep (1);
698
699 rv = svm_fifo_segment_attach (a);
700 if (rv)
701 {
702 clib_warning ("sm_fifo_segment_create ('%v') failed",
703 mp->segment_name);
704 return;
705 }
706
707 segment_index = vec_len (sm->segments) - 1;
708 vec_add2 (utm->seg, seg, 1);
709
710 memcpy (seg, sm->segments + segment_index, sizeof (*seg));
711 sh = seg->ssvm.sh;
712 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
713
714 while (vec_len (fsh->fifos) < 2)
715 sleep (1);
716
717 pool_get (utm->sessions, session);
718 utm->cut_through_session_index = session - utm->sessions;
719
720 session->server_rx_fifo = (svm_fifo_t *) fsh->fifos[0];
721 ASSERT (session->server_rx_fifo);
722 session->server_tx_fifo = (svm_fifo_t *) fsh->fifos[1];
723 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 */