blob: d559d5726c7c12be7cd4cbe67303a503889fd73f [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,
53 STATE_READY,
Florin Coras6cf30ad2017-04-04 23:08:23 -070054 STATE_FAILED,
Dave Barach68b0fb02017-02-28 15:15:56 -050055 STATE_DISCONNECTING,
56} connection_state_t;
57
58typedef struct
59{
60 svm_fifo_t *server_rx_fifo;
61 svm_fifo_t *server_tx_fifo;
62} session_t;
63
64typedef struct
65{
66 /* vpe input queue */
67 unix_shared_memory_queue_t *vl_input_queue;
68
69 /* API client handle */
70 u32 my_client_index;
71
72 /* The URI we're playing with */
73 u8 *uri;
74
75 /* Session pool */
76 session_t *sessions;
77
78 /* Hash table for disconnect processing */
79 uword *session_index_by_vpp_handles;
80
81 /* fifo segment */
82 svm_fifo_segment_private_t *seg;
83
84 /* intermediate rx buffer */
85 u8 *rx_buf;
86
Florin Corase04c2992017-03-01 08:17:34 -080087 /* URI for connect */
88 u8 *connect_uri;
89
90 int i_am_master;
91
Dave Barach68b0fb02017-02-28 15:15:56 -050092 /* Our event queue */
93 unix_shared_memory_queue_t *our_event_queue;
94
95 /* $$$ single thread only for the moment */
96 unix_shared_memory_queue_t *vpp_event_queue;
97
Florin Corase04c2992017-03-01 08:17:34 -080098 /* $$$$ hack: cut-through session index */
99 volatile u32 cut_through_session_index;
100
101 /* unique segment name counter */
102 u32 unique_segment_index;
103
104 pid_t my_pid;
105
106 /* pthread handle */
107 pthread_t cut_through_thread_handle;
108
Dave Barach68b0fb02017-02-28 15:15:56 -0500109 /* For deadman timers */
110 clib_time_t clib_time;
111
112 /* State of the connection, shared between msg RX thread and main thread */
113 volatile connection_state_t state;
114
115 volatile int time_to_stop;
116 volatile int time_to_print_stats;
117
Florin Corase04c2992017-03-01 08:17:34 -0800118 u32 configured_segment_size;
119
Dave Barach68b0fb02017-02-28 15:15:56 -0500120 /* VNET_API_ERROR_FOO -> "Foo" hash table */
121 uword *error_string_by_error_number;
Florin Corase04c2992017-03-01 08:17:34 -0800122
123 /* convenience */
124 svm_fifo_segment_main_t *segment_main;
125
Dave Barach68b0fb02017-02-28 15:15:56 -0500126} uri_udp_test_main_t;
127
128#if CLIB_DEBUG > 0
Florin Corase04c2992017-03-01 08:17:34 -0800129#define NITER 10000
Dave Barach68b0fb02017-02-28 15:15:56 -0500130#else
Florin Corase04c2992017-03-01 08:17:34 -0800131#define NITER 4000000
Dave Barach68b0fb02017-02-28 15:15:56 -0500132#endif
133
134uri_udp_test_main_t uri_udp_test_main;
135
136static void
137stop_signal (int signum)
138{
139 uri_udp_test_main_t *um = &uri_udp_test_main;
140
141 um->time_to_stop = 1;
142}
143
144static void
145stats_signal (int signum)
146{
147 uri_udp_test_main_t *um = &uri_udp_test_main;
148
149 um->time_to_print_stats = 1;
150}
151
152static clib_error_t *
153setup_signal_handlers (void)
154{
155 signal (SIGINT, stats_signal);
156 signal (SIGQUIT, stop_signal);
157 signal (SIGTERM, stop_signal);
158
159 return 0;
160}
161
Florin Coras6cf30ad2017-04-04 23:08:23 -0700162void
Florin Corasa5464812017-04-19 13:00:05 -0700163application_send_attach (uri_udp_test_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700164{
165 vl_api_application_attach_t *bmp;
166 u32 fifo_size = 3 << 20;
167 bmp = vl_msg_api_alloc (sizeof (*bmp));
168 memset (bmp, 0, sizeof (*bmp));
169
170 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
171 bmp->client_index = utm->my_client_index;
172 bmp->context = ntohl (0xfeedface);
Florin Corasa5464812017-04-19 13:00:05 -0700173 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700174 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
Dave Barach10d8cc62017-05-30 09:30:07 -0400175 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700176 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
177 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
178 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
179 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
180 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
181}
182
183void
184application_detach (uri_udp_test_main_t * utm)
185{
186 vl_api_application_detach_t *bmp;
187 bmp = vl_msg_api_alloc (sizeof (*bmp));
188 memset (bmp, 0, sizeof (*bmp));
189
190 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
191 bmp->client_index = utm->my_client_index;
192 bmp->context = ntohl (0xfeedface);
193 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
194}
195
196static void
197vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
198 mp)
199{
200 uri_udp_test_main_t *utm = &uri_udp_test_main;
201 svm_fifo_segment_create_args_t _a, *a = &_a;
202 int rv;
203
204 if (mp->retval)
205 {
206 clib_warning ("attach failed: %d", mp->retval);
207 utm->state = STATE_FAILED;
208 return;
209 }
210
211 if (mp->segment_name_length == 0)
212 {
213 clib_warning ("segment_name_length zero");
214 return;
215 }
216
217 a->segment_name = (char *) mp->segment_name;
218 a->segment_size = mp->segment_size;
219
220 ASSERT (mp->app_event_queue_address);
221
222 /* Attach to the segment vpp created */
223 rv = svm_fifo_segment_attach (a);
224 if (rv)
225 {
226 clib_warning ("svm_fifo_segment_attach ('%s') failed",
227 mp->segment_name);
228 return;
229 }
230
231 utm->our_event_queue =
Damjan Marion7bee80c2017-04-26 15:32:12 +0200232 uword_to_pointer (mp->app_event_queue_address,
233 unix_shared_memory_queue_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700234}
235
236static void
237vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
238 mp)
239{
240 if (mp->retval)
241 clib_warning ("detach returned with err: %d", mp->retval);
242}
243
Dave Barach68b0fb02017-02-28 15:15:56 -0500244u8 *
245format_api_error (u8 * s, va_list * args)
246{
247 uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *);
248 i32 error = va_arg (*args, u32);
249 uword *p;
250
251 p = hash_get (utm->error_string_by_error_number, -error);
252
253 if (p)
254 s = format (s, "%s", p[0]);
255 else
256 s = format (s, "%d", error);
257 return s;
258}
259
260int
261wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state)
262{
Florin Corase04c2992017-03-01 08:17:34 -0800263#if CLIB_DEBUG > 0
264#define TIMEOUT 600.0
265#else
266#define TIMEOUT 600.0
267#endif
268
269 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500270
271 while (clib_time_now (&utm->clib_time) < timeout)
272 {
273 if (utm->state == state)
274 return 0;
275 }
276 return -1;
277}
278
Florin Corase04c2992017-03-01 08:17:34 -0800279u64 server_bytes_received, server_bytes_sent;
280
281static void *
282cut_through_thread_fn (void *arg)
283{
284 session_t *s;
285 svm_fifo_t *rx_fifo;
286 svm_fifo_t *tx_fifo;
287 u8 *my_copy_buffer = 0;
288 uri_udp_test_main_t *utm = &uri_udp_test_main;
289 i32 actual_transfer;
290 int rv;
291 u32 buffer_offset;
292
293 while (utm->cut_through_session_index == ~0)
294 ;
295
296 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
297
298 rx_fifo = s->server_rx_fifo;
299 tx_fifo = s->server_tx_fifo;
300
301 vec_validate (my_copy_buffer, 64 * 1024 - 1);
302
303 while (true)
304 {
305 /* We read from the tx fifo and write to the rx fifo */
306 do
307 {
Florin Corasa5464812017-04-19 13:00:05 -0700308 actual_transfer = svm_fifo_dequeue_nowait (tx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800309 vec_len (my_copy_buffer),
310 my_copy_buffer);
311 }
312 while (actual_transfer <= 0);
313
314 server_bytes_received += actual_transfer;
315
316 buffer_offset = 0;
317 while (actual_transfer > 0)
318 {
Florin Corasa5464812017-04-19 13:00:05 -0700319 rv = svm_fifo_enqueue_nowait (rx_fifo, actual_transfer,
Florin Corase04c2992017-03-01 08:17:34 -0800320 my_copy_buffer + buffer_offset);
321 if (rv > 0)
322 {
323 actual_transfer -= rv;
324 buffer_offset += rv;
325 server_bytes_sent += rv;
326 }
327
328 }
329 if (PREDICT_FALSE (utm->time_to_stop))
330 break;
331 }
332
333 pthread_exit (0);
334}
335
336static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700337udp_client_connect (uri_udp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800338{
339 vl_api_connect_uri_t *cmp;
Florin Corase04c2992017-03-01 08:17:34 -0800340 cmp = vl_msg_api_alloc (sizeof (*cmp));
341 memset (cmp, 0, sizeof (*cmp));
342
343 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
344 cmp->client_index = utm->my_client_index;
345 cmp->context = ntohl (0xfeedface);
346 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
347 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700348}
Florin Corase04c2992017-03-01 08:17:34 -0800349
Florin Coras6cf30ad2017-04-04 23:08:23 -0700350static void
351client_send (uri_udp_test_main_t * utm, session_t * session)
352{
353 int i;
354 u8 *test_data = 0;
355 u64 bytes_received = 0, bytes_sent = 0;
356 i32 bytes_to_read;
357 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700358 f64 before, after, delta, bytes_per_second;
359 svm_fifo_t *rx_fifo, *tx_fifo;
360 int buffer_offset, bytes_to_send = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800361
Florin Coras6cf30ad2017-04-04 23:08:23 -0700362 /*
363 * Prepare test data
364 */
365 vec_validate (test_data, 64 * 1024 - 1);
366 for (i = 0; i < vec_len (test_data); i++)
367 test_data[i] = i & 0xff;
368
Florin Corase04c2992017-03-01 08:17:34 -0800369 rx_fifo = session->server_rx_fifo;
370 tx_fifo = session->server_tx_fifo;
371
372 before = clib_time_now (&utm->clib_time);
373
374 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
375
376 for (i = 0; i < NITER; i++)
377 {
378 bytes_to_send = vec_len (test_data);
379 buffer_offset = 0;
380 while (bytes_to_send > 0)
381 {
Florin Corasa5464812017-04-19 13:00:05 -0700382 rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
Florin Corase04c2992017-03-01 08:17:34 -0800383 test_data + buffer_offset);
384
385 if (rv > 0)
386 {
387 bytes_to_send -= rv;
388 buffer_offset += rv;
389 bytes_sent += rv;
390 }
391 }
392
393 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
394
395 bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ?
396 bytes_to_read : vec_len (utm->rx_buf);
397
398 buffer_offset = 0;
399 while (bytes_to_read > 0)
400 {
Florin Corasa5464812017-04-19 13:00:05 -0700401 rv = svm_fifo_dequeue_nowait (rx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800402 bytes_to_read,
403 utm->rx_buf + buffer_offset);
404 if (rv > 0)
405 {
406 bytes_to_read -= rv;
407 buffer_offset += rv;
408 bytes_received += rv;
409 }
410 }
411 }
412 while (bytes_received < bytes_sent)
413 {
Florin Corasa5464812017-04-19 13:00:05 -0700414 rv =
415 svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800416 if (rv > 0)
417 {
418#if CLIB_DEBUG > 0
419 int j;
420 for (j = 0; j < rv; j++)
421 {
422 if (utm->rx_buf[j] != ((bytes_received + j) & 0xff))
423 {
424 clib_warning ("error at byte %lld, 0x%x not 0x%x",
425 bytes_received + j,
426 utm->rx_buf[j],
427 ((bytes_received + j) & 0xff));
428 }
429 }
430#endif
431 bytes_received += (u64) rv;
432 }
433 }
434
435 after = clib_time_now (&utm->clib_time);
436 delta = after - before;
437 bytes_per_second = 0.0;
438
439 if (delta > 0.0)
440 bytes_per_second = (f64) bytes_received / delta;
441
442 fformat (stdout,
443 "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n",
444 bytes_received, delta, bytes_per_second);
445 fformat (stdout,
446 "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n",
447 bytes_sent, delta, bytes_per_second);
448 fformat (stdout,
449 "client -> server -> client round trip: %.2f Gbit/sec \n\n",
450 (bytes_per_second * 8.0) / 1e9);
451}
452
Dave Barach68b0fb02017-02-28 15:15:56 -0500453static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700454uri_udp_client_test (uri_udp_test_main_t * utm)
455{
456 session_t *session;
457
Florin Corasa5464812017-04-19 13:00:05 -0700458 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700459 udp_client_connect (utm);
460
461 if (wait_for_state_change (utm, STATE_READY))
462 {
463 clib_warning ("timeout waiting for STATE_READY");
464 return;
465 }
466
467 /* Only works with cut through sessions */
468 session = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
469
470 client_send (utm, session);
471 application_detach (utm);
472}
473
474static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500475vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
476{
477 uri_udp_test_main_t *utm = &uri_udp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500478
Florin Coras6cf30ad2017-04-04 23:08:23 -0700479 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500480 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700481 clib_warning ("bind failed: %d", mp->retval);
482 utm->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500483 return;
484 }
485
Dave Barach68b0fb02017-02-28 15:15:56 -0500486 utm->state = STATE_READY;
487}
488
489static void
Florin Corase04c2992017-03-01 08:17:34 -0800490vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
491{
492 svm_fifo_segment_create_args_t _a, *a = &_a;
493 int rv;
494
495 a->segment_name = (char *) mp->segment_name;
496 a->segment_size = mp->segment_size;
497 /* Attach to the segment vpp created */
498 rv = svm_fifo_segment_attach (a);
499 if (rv)
500 {
501 clib_warning ("svm_fifo_segment_attach ('%s') failed",
502 mp->segment_name);
503 return;
504 }
505 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
506 mp->segment_size);
507}
508
Florin Coras6cf30ad2017-04-04 23:08:23 -0700509/**
510 * Acting as server for redirected connect requests
511 */
Florin Corase04c2992017-03-01 08:17:34 -0800512static void
513vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
514{
515 u32 segment_index;
516 uri_udp_test_main_t *utm = &uri_udp_test_main;
517 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
518 svm_fifo_segment_create_args_t _a, *a = &_a;
519 svm_fifo_segment_private_t *seg;
520 unix_shared_memory_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -0400521 vl_api_connect_session_reply_t *rmp;
Dave Barach10d8cc62017-05-30 09:30:07 -0400522 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800523 int rv = 0;
524
525 /* Create the segment */
526 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
527 utm->unique_segment_index++, 0);
528 a->segment_size = utm->configured_segment_size;
529
530 rv = svm_fifo_segment_create (a);
531 if (rv)
532 {
533 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
534 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
535 goto send_reply;
536 }
537
538 vec_add2 (utm->seg, seg, 1);
539
540 segment_index = vec_len (sm->segments) - 1;
Florin Corase04c2992017-03-01 08:17:34 -0800541 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
542
543 pool_get (utm->sessions, session);
544
Dave Barach10d8cc62017-05-30 09:30:07 -0400545 session->server_rx_fifo = svm_fifo_segment_alloc_fifo
546 (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
Florin Corase04c2992017-03-01 08:17:34 -0800547 ASSERT (session->server_rx_fifo);
548
Dave Barach10d8cc62017-05-30 09:30:07 -0400549 session->server_tx_fifo = svm_fifo_segment_alloc_fifo
550 (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
Florin Corase04c2992017-03-01 08:17:34 -0800551 ASSERT (session->server_tx_fifo);
552
Florin Corasa5464812017-04-19 13:00:05 -0700553 session->server_rx_fifo->master_session_index = session - utm->sessions;
554 session->server_tx_fifo->master_session_index = session - utm->sessions;
Florin Corase04c2992017-03-01 08:17:34 -0800555 utm->cut_through_session_index = session - utm->sessions;
556
557 rv = pthread_create (&utm->cut_through_thread_handle,
558 NULL /*attr */ , cut_through_thread_fn, 0);
559 if (rv)
560 {
561 clib_warning ("pthread_create returned %d", rv);
562 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
563 }
564
565send_reply:
566 rmp = vl_msg_api_alloc (sizeof (*rmp));
567 memset (rmp, 0, sizeof (*rmp));
568
Dave Wallace33e002b2017-09-06 01:20:02 -0400569 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800570 rmp->context = mp->context;
571 rmp->retval = ntohl (rv);
572 rmp->segment_name_length = vec_len (a->segment_name);
Dave Barach10d8cc62017-05-30 09:30:07 -0400573 if (session)
574 {
575 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
576 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
577 }
578
Florin Corase04c2992017-03-01 08:17:34 -0800579 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
580
581 vec_free (a->segment_name);
582
Damjan Marion7bee80c2017-04-26 15:32:12 +0200583 client_q =
584 uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
Florin Corase04c2992017-03-01 08:17:34 -0800585 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
Damjan Marion7bee80c2017-04-26 15:32:12 +0200611 utm->vpp_event_queue =
612 uword_to_pointer (mp->vpp_event_queue_address,
613 unix_shared_memory_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500614
615 pool_get (utm->sessions, session);
616
Damjan Marion7bee80c2017-04-26 15:32:12 +0200617 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500618 rx_fifo->client_session_index = session - utm->sessions;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200619 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500620 tx_fifo->client_session_index = session - utm->sessions;
621
622 session->server_rx_fifo = rx_fifo;
623 session->server_tx_fifo = tx_fifo;
624
Florin Coras6cf30ad2017-04-04 23:08:23 -0700625 hash_set (utm->session_index_by_vpp_handles, mp->handle,
626 session - utm->sessions);
Dave Barach68b0fb02017-02-28 15:15:56 -0500627
628 utm->state = STATE_READY;
629
630 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
631 {
632 f64 now = clib_time_now (&utm->clib_time);
633 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
634 pool_elts (utm->sessions), now - start_time,
635 (f64) pool_elts (utm->sessions) / (now - start_time));
636 }
637
638 rmp = vl_msg_api_alloc (sizeof (*rmp));
639 memset (rmp, 0, sizeof (*rmp));
640 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700641 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500642 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
643}
644
645static void
646vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
647{
648 uri_udp_test_main_t *utm = &uri_udp_test_main;
649 session_t *session;
650 vl_api_disconnect_session_reply_t *rmp;
651 uword *p;
652 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500653
Florin Coras6cf30ad2017-04-04 23:08:23 -0700654 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500655
656 if (p)
657 {
658 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700659 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500660 pool_put (utm->sessions, session);
661 }
662 else
663 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700664 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500665 rv = -11;
666 }
667
668 rmp = vl_msg_api_alloc (sizeof (*rmp));
669 memset (rmp, 0, sizeof (*rmp));
670 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
671 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700672 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500673 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
674}
675
Florin Corase04c2992017-03-01 08:17:34 -0800676static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400677vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800678{
Florin Corase04c2992017-03-01 08:17:34 -0800679 uri_udp_test_main_t *utm = &uri_udp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800680
681 ASSERT (utm->i_am_master == 0);
682
Florin Coras6cf30ad2017-04-04 23:08:23 -0700683 /* We've been redirected */
684 if (mp->segment_name_length > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800685 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700686 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
687 svm_fifo_segment_create_args_t _a, *a = &_a;
688 u32 segment_index;
689 session_t *session;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700690 svm_fifo_segment_private_t *seg;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700691 int rv;
692
693 memset (a, 0, sizeof (*a));
694 a->segment_name = (char *) mp->segment_name;
695
696 sleep (1);
697
698 rv = svm_fifo_segment_attach (a);
699 if (rv)
700 {
701 clib_warning ("sm_fifo_segment_create ('%v') failed",
702 mp->segment_name);
703 return;
704 }
705
Dave Barach2c25a622017-06-26 11:35:07 -0400706 segment_index = a->new_segment_indices[0];
Florin Coras6cf30ad2017-04-04 23:08:23 -0700707 vec_add2 (utm->seg, seg, 1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700708 memcpy (seg, sm->segments + segment_index, sizeof (*seg));
Dave Barach10d8cc62017-05-30 09:30:07 -0400709 sleep (1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700710
711 pool_get (utm->sessions, session);
712 utm->cut_through_session_index = session - utm->sessions;
713
Dave Barach10d8cc62017-05-30 09:30:07 -0400714 session->server_rx_fifo = uword_to_pointer (mp->server_rx_fifo,
715 svm_fifo_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700716 ASSERT (session->server_rx_fifo);
Dave Barach10d8cc62017-05-30 09:30:07 -0400717 session->server_tx_fifo = uword_to_pointer (mp->server_tx_fifo,
718 svm_fifo_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700719 ASSERT (session->server_tx_fifo);
Florin Corase04c2992017-03-01 08:17:34 -0800720 }
721
Florin Corase04c2992017-03-01 08:17:34 -0800722 /* security: could unlink /dev/shm/<mp->segment_name> here, maybe */
723
724 utm->state = STATE_READY;
725}
726
Florin Coras6cf30ad2017-04-04 23:08:23 -0700727#define foreach_uri_msg \
728_(BIND_URI_REPLY, bind_uri_reply) \
729_(CONNECT_URI, connect_uri) \
Dave Wallace33e002b2017-09-06 01:20:02 -0400730_(CONNECT_SESSION_REPLY, connect_session_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -0700731_(UNBIND_URI_REPLY, unbind_uri_reply) \
732_(ACCEPT_SESSION, accept_session) \
733_(DISCONNECT_SESSION, disconnect_session) \
734_(MAP_ANOTHER_SEGMENT, map_another_segment) \
735_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
736_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -0500737
738void
739uri_api_hookup (uri_udp_test_main_t * utm)
740{
741#define _(N,n) \
742 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Corase04c2992017-03-01 08:17:34 -0800743 vl_api_##n##_t_handler, \
Dave Barach68b0fb02017-02-28 15:15:56 -0500744 vl_noop_handler, \
745 vl_api_##n##_t_endian, \
746 vl_api_##n##_t_print, \
747 sizeof(vl_api_##n##_t), 1);
748 foreach_uri_msg;
749#undef _
750
751}
752
Dave Barach68b0fb02017-02-28 15:15:56 -0500753int
754connect_to_vpp (char *name)
755{
756 uri_udp_test_main_t *utm = &uri_udp_test_main;
757 api_main_t *am = &api_main;
758
759 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
760 return -1;
761
762 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
763 utm->my_client_index = am->my_client_index;
764
765 return 0;
766}
767
768void
769vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
770{
771 clib_warning ("BUG");
772}
773
774static void
775init_error_string_table (uri_udp_test_main_t * utm)
776{
777 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
778
779#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
780 foreach_vnet_api_error;
781#undef _
782
783 hash_set (utm->error_string_by_error_number, 99, "Misc");
784}
785
786void
Florin Corase04c2992017-03-01 08:17:34 -0800787server_handle_fifo_event_rx (uri_udp_test_main_t * utm,
Dave Barach68b0fb02017-02-28 15:15:56 -0500788 session_fifo_event_t * e)
789{
790 svm_fifo_t *rx_fifo, *tx_fifo;
791 int nbytes;
792
793 session_fifo_event_t evt;
794 unix_shared_memory_queue_t *q;
795 int rv;
796
797 rx_fifo = e->fifo;
798 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
799
800 do
801 {
Florin Corasa5464812017-04-19 13:00:05 -0700802 nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
803 utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500804 }
805 while (nbytes <= 0);
806 do
807 {
Florin Corasa5464812017-04-19 13:00:05 -0700808 rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500809 }
810 while (rv == -2);
811
812 /* Fabricate TX event, send to vpp */
813 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700814 evt.event_type = FIFO_EVENT_APP_TX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500815 evt.event_id = e->event_id;
Florin Coras6792ec02017-03-13 03:49:51 -0700816
817 if (svm_fifo_set_event (tx_fifo))
818 {
819 q = utm->vpp_event_queue;
820 unix_shared_memory_queue_add (q, (u8 *) & evt,
821 0 /* do wait for mutex */ );
822 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500823}
824
825void
Florin Corase04c2992017-03-01 08:17:34 -0800826server_handle_event_queue (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500827{
Florin Coras6792ec02017-03-13 03:49:51 -0700828 session_fifo_event_t _e, *e = &_e;
Dave Barach68b0fb02017-02-28 15:15:56 -0500829
830 while (1)
831 {
832 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
833 0 /* nowait */ );
834 switch (e->event_type)
835 {
Florin Corasa5464812017-04-19 13:00:05 -0700836 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800837 server_handle_fifo_event_rx (utm, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500838 break;
839
Florin Corasa5464812017-04-19 13:00:05 -0700840 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500841 return;
842
843 default:
844 clib_warning ("unknown event type %d", e->event_type);
845 break;
846 }
847 if (PREDICT_FALSE (utm->time_to_stop == 1))
848 break;
849 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
850 {
851 utm->time_to_print_stats = 0;
852 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
853 }
854 }
855}
856
Florin Coras6cf30ad2017-04-04 23:08:23 -0700857static void
858server_unbind (uri_udp_test_main_t * utm)
859{
860 vl_api_unbind_uri_t *ump;
861
862 ump = vl_msg_api_alloc (sizeof (*ump));
863 memset (ump, 0, sizeof (*ump));
864
865 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
866 ump->client_index = utm->my_client_index;
867 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
868 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
869}
870
871static void
872server_listen (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500873{
874 vl_api_bind_uri_t *bmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500875
876 bmp = vl_msg_api_alloc (sizeof (*bmp));
877 memset (bmp, 0, sizeof (*bmp));
878
879 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
880 bmp->client_index = utm->my_client_index;
881 bmp->context = ntohl (0xfeedface);
Dave Barach68b0fb02017-02-28 15:15:56 -0500882 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
883 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700884}
885
886void
887udp_server_test (uri_udp_test_main_t * utm)
888{
889
Florin Corasa5464812017-04-19 13:00:05 -0700890 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700891
892 /* Bind to uri */
893 server_listen (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500894
895 if (wait_for_state_change (utm, STATE_READY))
896 {
897 clib_warning ("timeout waiting for STATE_READY");
898 return;
899 }
900
Florin Corase04c2992017-03-01 08:17:34 -0800901 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500902
Florin Coras6cf30ad2017-04-04 23:08:23 -0700903 /* Cleanup */
904 server_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500905
906 if (wait_for_state_change (utm, STATE_START))
907 {
908 clib_warning ("timeout waiting for STATE_START");
909 return;
910 }
911
Florin Coras6cf30ad2017-04-04 23:08:23 -0700912 application_detach (utm);
913
Dave Barach68b0fb02017-02-28 15:15:56 -0500914 fformat (stdout, "Test complete...\n");
915}
916
917int
918main (int argc, char **argv)
919{
920 uri_udp_test_main_t *utm = &uri_udp_test_main;
921 unformat_input_t _argv, *a = &_argv;
922 u8 *chroot_prefix;
923 u8 *heap;
Florin Corase04c2992017-03-01 08:17:34 -0800924 u8 *bind_name = (u8 *) "udp://0.0.0.0/1234";
925 u32 tmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500926 mheap_t *h;
927 session_t *session;
928 int i;
Florin Corase04c2992017-03-01 08:17:34 -0800929 int i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500930
931 clib_mem_init (0, 256 << 20);
932
933 heap = clib_mem_get_per_cpu_heap ();
934 h = mheap_header (heap);
935
936 /* make the main heap thread-safe */
937 h->flags |= MHEAP_FLAG_THREAD_SAFE;
938
939 vec_validate (utm->rx_buf, 8192);
940
941 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
942
Florin Corase04c2992017-03-01 08:17:34 -0800943 utm->my_pid = getpid ();
944 utm->configured_segment_size = 1 << 20;
945
Dave Barach68b0fb02017-02-28 15:15:56 -0500946 clib_time_init (&utm->clib_time);
947 init_error_string_table (utm);
948 svm_fifo_segment_init (0x200000000ULL, 20);
949 unformat_init_command_line (a, argv);
950
951 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
952 {
953 if (unformat (a, "chroot prefix %s", &chroot_prefix))
954 {
955 vl_set_memory_root_path ((char *) chroot_prefix);
956 }
957 else if (unformat (a, "uri %s", &bind_name))
958 ;
Florin Corase04c2992017-03-01 08:17:34 -0800959 else if (unformat (a, "segment-size %dM", &tmp))
960 utm->configured_segment_size = tmp << 20;
961 else if (unformat (a, "segment-size %dG", &tmp))
962 utm->configured_segment_size = tmp << 30;
963 else if (unformat (a, "master"))
964 i_am_master = 1;
965 else if (unformat (a, "slave"))
966 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500967 else
968 {
969 fformat (stderr, "%s: usage [master|slave]\n");
970 exit (1);
971 }
972 }
973
Florin Corase04c2992017-03-01 08:17:34 -0800974 utm->cut_through_session_index = ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500975 utm->uri = format (0, "%s%c", bind_name, 0);
Florin Corase04c2992017-03-01 08:17:34 -0800976 utm->i_am_master = i_am_master;
977 utm->segment_main = &svm_fifo_segment_main;
978
Florin Coras6cf30ad2017-04-04 23:08:23 -0700979 utm->connect_uri = format (0, "udp://6.0.0.1/1234%c", 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500980
981 setup_signal_handlers ();
982
983 uri_api_hookup (utm);
984
Florin Corase04c2992017-03-01 08:17:34 -0800985 if (connect_to_vpp (i_am_master ? "uri_udp_master" : "uri_udp_slave") < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500986 {
987 svm_region_exit ();
988 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
989 exit (1);
990 }
991
Florin Corase04c2992017-03-01 08:17:34 -0800992 if (i_am_master == 0)
993 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700994 uri_udp_client_test (utm);
Florin Corase04c2992017-03-01 08:17:34 -0800995 exit (0);
996 }
997
Dave Barach68b0fb02017-02-28 15:15:56 -0500998 /* $$$$ hack preallocation */
999 for (i = 0; i < 200000; i++)
1000 {
1001 pool_get (utm->sessions, session);
1002 memset (session, 0, sizeof (*session));
1003 }
1004 for (i = 0; i < 200000; i++)
1005 pool_put_index (utm->sessions, i);
1006
Florin Coras6cf30ad2017-04-04 23:08:23 -07001007 udp_server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001008
1009 vl_client_disconnect_from_vlib ();
1010 exit (0);
1011}
1012
1013#undef vl_api_version
1014#define vl_api_version(n,v) static u32 vpe_api_version = v;
Florin Corase04c2992017-03-01 08:17:34 -08001015#include <vpp/api/vpe.api.h>
Dave Barach68b0fb02017-02-28 15:15:56 -05001016#undef vl_api_version
1017
1018void
1019vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1020{
1021 /*
1022 * Send the main API signature in slot 0. This bit of code must
1023 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1024 */
1025 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1026}
1027
Florin Corase04c2992017-03-01 08:17:34 -08001028u32
1029vl (void *p)
1030{
1031 return vec_len (p);
1032}
1033
Dave Barach68b0fb02017-02-28 15:15:56 -05001034/*
1035 * fd.io coding-style-patch-verification: ON
1036 *
1037 * Local Variables:
1038 * eval: (c-set-style "gnu")
1039 * End:
1040 */