blob: 8fb12ed27a76524abdca29f769b81e0292981ed6 [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 =
Damjan Marion7bee80c2017-04-26 15:32:12 +0200235 uword_to_pointer (mp->app_event_queue_address,
236 unix_shared_memory_queue_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700237}
238
239static void
240vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
241 mp)
242{
243 if (mp->retval)
244 clib_warning ("detach returned with err: %d", mp->retval);
245}
246
Dave Barach68b0fb02017-02-28 15:15:56 -0500247u8 *
248format_api_error (u8 * s, va_list * args)
249{
250 uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *);
251 i32 error = va_arg (*args, u32);
252 uword *p;
253
254 p = hash_get (utm->error_string_by_error_number, -error);
255
256 if (p)
257 s = format (s, "%s", p[0]);
258 else
259 s = format (s, "%d", error);
260 return s;
261}
262
263int
264wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state)
265{
Florin Corase04c2992017-03-01 08:17:34 -0800266#if CLIB_DEBUG > 0
267#define TIMEOUT 600.0
268#else
269#define TIMEOUT 600.0
270#endif
271
272 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500273
274 while (clib_time_now (&utm->clib_time) < timeout)
275 {
276 if (utm->state == state)
277 return 0;
278 }
279 return -1;
280}
281
Florin Corase04c2992017-03-01 08:17:34 -0800282u64 server_bytes_received, server_bytes_sent;
283
284static void *
285cut_through_thread_fn (void *arg)
286{
287 session_t *s;
288 svm_fifo_t *rx_fifo;
289 svm_fifo_t *tx_fifo;
290 u8 *my_copy_buffer = 0;
291 uri_udp_test_main_t *utm = &uri_udp_test_main;
292 i32 actual_transfer;
293 int rv;
294 u32 buffer_offset;
295
296 while (utm->cut_through_session_index == ~0)
297 ;
298
299 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
300
301 rx_fifo = s->server_rx_fifo;
302 tx_fifo = s->server_tx_fifo;
303
304 vec_validate (my_copy_buffer, 64 * 1024 - 1);
305
306 while (true)
307 {
308 /* We read from the tx fifo and write to the rx fifo */
309 do
310 {
Florin Corasa5464812017-04-19 13:00:05 -0700311 actual_transfer = svm_fifo_dequeue_nowait (tx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800312 vec_len (my_copy_buffer),
313 my_copy_buffer);
314 }
315 while (actual_transfer <= 0);
316
317 server_bytes_received += actual_transfer;
318
319 buffer_offset = 0;
320 while (actual_transfer > 0)
321 {
Florin Corasa5464812017-04-19 13:00:05 -0700322 rv = svm_fifo_enqueue_nowait (rx_fifo, actual_transfer,
Florin Corase04c2992017-03-01 08:17:34 -0800323 my_copy_buffer + buffer_offset);
324 if (rv > 0)
325 {
326 actual_transfer -= rv;
327 buffer_offset += rv;
328 server_bytes_sent += rv;
329 }
330
331 }
332 if (PREDICT_FALSE (utm->time_to_stop))
333 break;
334 }
335
336 pthread_exit (0);
337}
338
339static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700340udp_client_connect (uri_udp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800341{
342 vl_api_connect_uri_t *cmp;
Florin Corase04c2992017-03-01 08:17:34 -0800343 cmp = vl_msg_api_alloc (sizeof (*cmp));
344 memset (cmp, 0, sizeof (*cmp));
345
346 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
347 cmp->client_index = utm->my_client_index;
348 cmp->context = ntohl (0xfeedface);
349 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
350 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700351}
Florin Corase04c2992017-03-01 08:17:34 -0800352
Florin Coras6cf30ad2017-04-04 23:08:23 -0700353static void
354client_send (uri_udp_test_main_t * utm, session_t * session)
355{
356 int i;
357 u8 *test_data = 0;
358 u64 bytes_received = 0, bytes_sent = 0;
359 i32 bytes_to_read;
360 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700361 f64 before, after, delta, bytes_per_second;
362 svm_fifo_t *rx_fifo, *tx_fifo;
363 int buffer_offset, bytes_to_send = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800364
Florin Coras6cf30ad2017-04-04 23:08:23 -0700365 /*
366 * Prepare test data
367 */
368 vec_validate (test_data, 64 * 1024 - 1);
369 for (i = 0; i < vec_len (test_data); i++)
370 test_data[i] = i & 0xff;
371
Florin Corase04c2992017-03-01 08:17:34 -0800372 rx_fifo = session->server_rx_fifo;
373 tx_fifo = session->server_tx_fifo;
374
375 before = clib_time_now (&utm->clib_time);
376
377 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
378
379 for (i = 0; i < NITER; i++)
380 {
381 bytes_to_send = vec_len (test_data);
382 buffer_offset = 0;
383 while (bytes_to_send > 0)
384 {
Florin Corasa5464812017-04-19 13:00:05 -0700385 rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
Florin Corase04c2992017-03-01 08:17:34 -0800386 test_data + buffer_offset);
387
388 if (rv > 0)
389 {
390 bytes_to_send -= rv;
391 buffer_offset += rv;
392 bytes_sent += rv;
393 }
394 }
395
396 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
397
398 bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ?
399 bytes_to_read : vec_len (utm->rx_buf);
400
401 buffer_offset = 0;
402 while (bytes_to_read > 0)
403 {
Florin Corasa5464812017-04-19 13:00:05 -0700404 rv = svm_fifo_dequeue_nowait (rx_fifo,
Florin Corase04c2992017-03-01 08:17:34 -0800405 bytes_to_read,
406 utm->rx_buf + buffer_offset);
407 if (rv > 0)
408 {
409 bytes_to_read -= rv;
410 buffer_offset += rv;
411 bytes_received += rv;
412 }
413 }
414 }
415 while (bytes_received < bytes_sent)
416 {
Florin Corasa5464812017-04-19 13:00:05 -0700417 rv =
418 svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800419 if (rv > 0)
420 {
421#if CLIB_DEBUG > 0
422 int j;
423 for (j = 0; j < rv; j++)
424 {
425 if (utm->rx_buf[j] != ((bytes_received + j) & 0xff))
426 {
427 clib_warning ("error at byte %lld, 0x%x not 0x%x",
428 bytes_received + j,
429 utm->rx_buf[j],
430 ((bytes_received + j) & 0xff));
431 }
432 }
433#endif
434 bytes_received += (u64) rv;
435 }
436 }
437
438 after = clib_time_now (&utm->clib_time);
439 delta = after - before;
440 bytes_per_second = 0.0;
441
442 if (delta > 0.0)
443 bytes_per_second = (f64) bytes_received / delta;
444
445 fformat (stdout,
446 "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n",
447 bytes_received, delta, bytes_per_second);
448 fformat (stdout,
449 "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n",
450 bytes_sent, delta, bytes_per_second);
451 fformat (stdout,
452 "client -> server -> client round trip: %.2f Gbit/sec \n\n",
453 (bytes_per_second * 8.0) / 1e9);
454}
455
Dave Barach68b0fb02017-02-28 15:15:56 -0500456static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700457uri_udp_client_test (uri_udp_test_main_t * utm)
458{
459 session_t *session;
460
Florin Corasa5464812017-04-19 13:00:05 -0700461 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700462 udp_client_connect (utm);
463
464 if (wait_for_state_change (utm, STATE_READY))
465 {
466 clib_warning ("timeout waiting for STATE_READY");
467 return;
468 }
469
470 /* Only works with cut through sessions */
471 session = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
472
473 client_send (utm, session);
474 application_detach (utm);
475}
476
477static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500478vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
479{
480 uri_udp_test_main_t *utm = &uri_udp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500481
Florin Coras6cf30ad2017-04-04 23:08:23 -0700482 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500483 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700484 clib_warning ("bind failed: %d", mp->retval);
485 utm->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500486 return;
487 }
488
Dave Barach68b0fb02017-02-28 15:15:56 -0500489 utm->state = STATE_READY;
490}
491
492static void
Florin Corase04c2992017-03-01 08:17:34 -0800493vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
494{
495 svm_fifo_segment_create_args_t _a, *a = &_a;
496 int rv;
497
498 a->segment_name = (char *) mp->segment_name;
499 a->segment_size = mp->segment_size;
500 /* Attach to the segment vpp created */
501 rv = svm_fifo_segment_attach (a);
502 if (rv)
503 {
504 clib_warning ("svm_fifo_segment_attach ('%s') failed",
505 mp->segment_name);
506 return;
507 }
508 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
509 mp->segment_size);
510}
511
Florin Coras6cf30ad2017-04-04 23:08:23 -0700512/**
513 * Acting as server for redirected connect requests
514 */
Florin Corase04c2992017-03-01 08:17:34 -0800515static void
516vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
517{
518 u32 segment_index;
519 uri_udp_test_main_t *utm = &uri_udp_test_main;
520 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
521 svm_fifo_segment_create_args_t _a, *a = &_a;
522 svm_fifo_segment_private_t *seg;
523 unix_shared_memory_queue_t *client_q;
524 vl_api_connect_uri_reply_t *rmp;
525 session_t *session;
526 int rv = 0;
527
528 /* Create the segment */
529 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
530 utm->unique_segment_index++, 0);
531 a->segment_size = utm->configured_segment_size;
532
533 rv = svm_fifo_segment_create (a);
534 if (rv)
535 {
536 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
537 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
538 goto send_reply;
539 }
540
541 vec_add2 (utm->seg, seg, 1);
542
543 segment_index = vec_len (sm->segments) - 1;
Florin Corase04c2992017-03-01 08:17:34 -0800544 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
545
546 pool_get (utm->sessions, session);
547
548 /*
549 * By construction the master's idea of the rx fifo ends up in
550 * fsh->fifos[0], and the master's idea of the tx fifo ends up in
551 * fsh->fifos[1].
552 */
553 session->server_rx_fifo = svm_fifo_segment_alloc_fifo (utm->seg,
554 128 * 1024);
555 ASSERT (session->server_rx_fifo);
556
557 session->server_tx_fifo = svm_fifo_segment_alloc_fifo (utm->seg,
558 128 * 1024);
559 ASSERT (session->server_tx_fifo);
560
Florin Corasa5464812017-04-19 13:00:05 -0700561 session->server_rx_fifo->master_session_index = session - utm->sessions;
562 session->server_tx_fifo->master_session_index = session - utm->sessions;
Florin Corase04c2992017-03-01 08:17:34 -0800563 utm->cut_through_session_index = session - utm->sessions;
564
565 rv = pthread_create (&utm->cut_through_thread_handle,
566 NULL /*attr */ , cut_through_thread_fn, 0);
567 if (rv)
568 {
569 clib_warning ("pthread_create returned %d", rv);
570 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
571 }
572
573send_reply:
574 rmp = vl_msg_api_alloc (sizeof (*rmp));
575 memset (rmp, 0, sizeof (*rmp));
576
577 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI_REPLY);
578 rmp->context = mp->context;
579 rmp->retval = ntohl (rv);
580 rmp->segment_name_length = vec_len (a->segment_name);
581 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
582
583 vec_free (a->segment_name);
584
Damjan Marion7bee80c2017-04-26 15:32:12 +0200585 client_q =
586 uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
Florin Corase04c2992017-03-01 08:17:34 -0800587 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
588}
589
590static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500591vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
592{
593 uri_udp_test_main_t *utm = &uri_udp_test_main;
594
595 if (mp->retval != 0)
596 clib_warning ("returned %d", ntohl (mp->retval));
597
598 utm->state = STATE_START;
599}
600
601static void
602vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
603{
604 uri_udp_test_main_t *utm = &uri_udp_test_main;
605 vl_api_accept_session_reply_t *rmp;
606 svm_fifo_t *rx_fifo, *tx_fifo;
607 session_t *session;
608 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500609
610 if (start_time == 0.0)
611 start_time = clib_time_now (&utm->clib_time);
612
Damjan Marion7bee80c2017-04-26 15:32:12 +0200613 utm->vpp_event_queue =
614 uword_to_pointer (mp->vpp_event_queue_address,
615 unix_shared_memory_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500616
617 pool_get (utm->sessions, session);
618
Damjan Marion7bee80c2017-04-26 15:32:12 +0200619 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500620 rx_fifo->client_session_index = session - utm->sessions;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200621 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500622 tx_fifo->client_session_index = session - utm->sessions;
623
624 session->server_rx_fifo = rx_fifo;
625 session->server_tx_fifo = tx_fifo;
626
Florin Coras6cf30ad2017-04-04 23:08:23 -0700627 hash_set (utm->session_index_by_vpp_handles, mp->handle,
628 session - utm->sessions);
Dave Barach68b0fb02017-02-28 15:15:56 -0500629
630 utm->state = STATE_READY;
631
632 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
633 {
634 f64 now = clib_time_now (&utm->clib_time);
635 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
636 pool_elts (utm->sessions), now - start_time,
637 (f64) pool_elts (utm->sessions) / (now - start_time));
638 }
639
640 rmp = vl_msg_api_alloc (sizeof (*rmp));
641 memset (rmp, 0, sizeof (*rmp));
642 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700643 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500644 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
645}
646
647static void
648vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
649{
650 uri_udp_test_main_t *utm = &uri_udp_test_main;
651 session_t *session;
652 vl_api_disconnect_session_reply_t *rmp;
653 uword *p;
654 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500655
Florin Coras6cf30ad2017-04-04 23:08:23 -0700656 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500657
658 if (p)
659 {
660 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700661 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500662 pool_put (utm->sessions, session);
663 }
664 else
665 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700666 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500667 rv = -11;
668 }
669
670 rmp = vl_msg_api_alloc (sizeof (*rmp));
671 memset (rmp, 0, sizeof (*rmp));
672 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
673 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700674 rmp->handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500675 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
676}
677
Florin Corase04c2992017-03-01 08:17:34 -0800678static void
679vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
680{
Florin Corase04c2992017-03-01 08:17:34 -0800681 uri_udp_test_main_t *utm = &uri_udp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800682
683 ASSERT (utm->i_am_master == 0);
684
Florin Coras6cf30ad2017-04-04 23:08:23 -0700685 /* We've been redirected */
686 if (mp->segment_name_length > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800687 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700688 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
689 svm_fifo_segment_create_args_t _a, *a = &_a;
690 u32 segment_index;
691 session_t *session;
692 ssvm_shared_header_t *sh;
693 svm_fifo_segment_private_t *seg;
694 svm_fifo_segment_header_t *fsh;
695 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
710 segment_index = vec_len (sm->segments) - 1;
711 vec_add2 (utm->seg, seg, 1);
712
713 memcpy (seg, sm->segments + segment_index, sizeof (*seg));
714 sh = seg->ssvm.sh;
715 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
716
717 while (vec_len (fsh->fifos) < 2)
718 sleep (1);
719
720 pool_get (utm->sessions, session);
721 utm->cut_through_session_index = session - utm->sessions;
722
723 session->server_rx_fifo = (svm_fifo_t *) fsh->fifos[0];
724 ASSERT (session->server_rx_fifo);
725 session->server_tx_fifo = (svm_fifo_t *) fsh->fifos[1];
726 ASSERT (session->server_tx_fifo);
Florin Corase04c2992017-03-01 08:17:34 -0800727 }
728
Florin Corase04c2992017-03-01 08:17:34 -0800729 /* security: could unlink /dev/shm/<mp->segment_name> here, maybe */
730
731 utm->state = STATE_READY;
732}
733
Florin Coras6cf30ad2017-04-04 23:08:23 -0700734#define foreach_uri_msg \
735_(BIND_URI_REPLY, bind_uri_reply) \
736_(CONNECT_URI, connect_uri) \
737_(CONNECT_URI_REPLY, connect_uri_reply) \
738_(UNBIND_URI_REPLY, unbind_uri_reply) \
739_(ACCEPT_SESSION, accept_session) \
740_(DISCONNECT_SESSION, disconnect_session) \
741_(MAP_ANOTHER_SEGMENT, map_another_segment) \
742_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
743_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -0500744
745void
746uri_api_hookup (uri_udp_test_main_t * utm)
747{
748#define _(N,n) \
749 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Corase04c2992017-03-01 08:17:34 -0800750 vl_api_##n##_t_handler, \
Dave Barach68b0fb02017-02-28 15:15:56 -0500751 vl_noop_handler, \
752 vl_api_##n##_t_endian, \
753 vl_api_##n##_t_print, \
754 sizeof(vl_api_##n##_t), 1);
755 foreach_uri_msg;
756#undef _
757
758}
759
Dave Barach68b0fb02017-02-28 15:15:56 -0500760int
761connect_to_vpp (char *name)
762{
763 uri_udp_test_main_t *utm = &uri_udp_test_main;
764 api_main_t *am = &api_main;
765
766 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
767 return -1;
768
769 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
770 utm->my_client_index = am->my_client_index;
771
772 return 0;
773}
774
775void
776vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
777{
778 clib_warning ("BUG");
779}
780
781static void
782init_error_string_table (uri_udp_test_main_t * utm)
783{
784 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
785
786#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
787 foreach_vnet_api_error;
788#undef _
789
790 hash_set (utm->error_string_by_error_number, 99, "Misc");
791}
792
793void
Florin Corase04c2992017-03-01 08:17:34 -0800794server_handle_fifo_event_rx (uri_udp_test_main_t * utm,
Dave Barach68b0fb02017-02-28 15:15:56 -0500795 session_fifo_event_t * e)
796{
797 svm_fifo_t *rx_fifo, *tx_fifo;
798 int nbytes;
799
800 session_fifo_event_t evt;
801 unix_shared_memory_queue_t *q;
802 int rv;
803
804 rx_fifo = e->fifo;
805 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
806
807 do
808 {
Florin Corasa5464812017-04-19 13:00:05 -0700809 nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
810 utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500811 }
812 while (nbytes <= 0);
813 do
814 {
Florin Corasa5464812017-04-19 13:00:05 -0700815 rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500816 }
817 while (rv == -2);
818
819 /* Fabricate TX event, send to vpp */
820 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700821 evt.event_type = FIFO_EVENT_APP_TX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500822 evt.event_id = e->event_id;
Florin Coras6792ec02017-03-13 03:49:51 -0700823
824 if (svm_fifo_set_event (tx_fifo))
825 {
826 q = utm->vpp_event_queue;
827 unix_shared_memory_queue_add (q, (u8 *) & evt,
828 0 /* do wait for mutex */ );
829 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500830}
831
832void
Florin Corase04c2992017-03-01 08:17:34 -0800833server_handle_event_queue (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500834{
Florin Coras6792ec02017-03-13 03:49:51 -0700835 session_fifo_event_t _e, *e = &_e;
Dave Barach68b0fb02017-02-28 15:15:56 -0500836
837 while (1)
838 {
839 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
840 0 /* nowait */ );
841 switch (e->event_type)
842 {
Florin Corasa5464812017-04-19 13:00:05 -0700843 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800844 server_handle_fifo_event_rx (utm, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500845 break;
846
Florin Corasa5464812017-04-19 13:00:05 -0700847 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500848 return;
849
850 default:
851 clib_warning ("unknown event type %d", e->event_type);
852 break;
853 }
854 if (PREDICT_FALSE (utm->time_to_stop == 1))
855 break;
856 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
857 {
858 utm->time_to_print_stats = 0;
859 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
860 }
861 }
862}
863
Florin Coras6cf30ad2017-04-04 23:08:23 -0700864static void
865server_unbind (uri_udp_test_main_t * utm)
866{
867 vl_api_unbind_uri_t *ump;
868
869 ump = vl_msg_api_alloc (sizeof (*ump));
870 memset (ump, 0, sizeof (*ump));
871
872 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
873 ump->client_index = utm->my_client_index;
874 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
875 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
876}
877
878static void
879server_listen (uri_udp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500880{
881 vl_api_bind_uri_t *bmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500882
883 bmp = vl_msg_api_alloc (sizeof (*bmp));
884 memset (bmp, 0, sizeof (*bmp));
885
886 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
887 bmp->client_index = utm->my_client_index;
888 bmp->context = ntohl (0xfeedface);
Dave Barach68b0fb02017-02-28 15:15:56 -0500889 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
890 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700891}
892
893void
894udp_server_test (uri_udp_test_main_t * utm)
895{
896
Florin Corasa5464812017-04-19 13:00:05 -0700897 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700898
899 /* Bind to uri */
900 server_listen (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500901
902 if (wait_for_state_change (utm, STATE_READY))
903 {
904 clib_warning ("timeout waiting for STATE_READY");
905 return;
906 }
907
Florin Corase04c2992017-03-01 08:17:34 -0800908 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500909
Florin Coras6cf30ad2017-04-04 23:08:23 -0700910 /* Cleanup */
911 server_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500912
913 if (wait_for_state_change (utm, STATE_START))
914 {
915 clib_warning ("timeout waiting for STATE_START");
916 return;
917 }
918
Florin Coras6cf30ad2017-04-04 23:08:23 -0700919 application_detach (utm);
920
Dave Barach68b0fb02017-02-28 15:15:56 -0500921 fformat (stdout, "Test complete...\n");
922}
923
924int
925main (int argc, char **argv)
926{
927 uri_udp_test_main_t *utm = &uri_udp_test_main;
928 unformat_input_t _argv, *a = &_argv;
929 u8 *chroot_prefix;
930 u8 *heap;
Florin Corase04c2992017-03-01 08:17:34 -0800931 u8 *bind_name = (u8 *) "udp://0.0.0.0/1234";
932 u32 tmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500933 mheap_t *h;
934 session_t *session;
935 int i;
Florin Corase04c2992017-03-01 08:17:34 -0800936 int i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500937
938 clib_mem_init (0, 256 << 20);
939
940 heap = clib_mem_get_per_cpu_heap ();
941 h = mheap_header (heap);
942
943 /* make the main heap thread-safe */
944 h->flags |= MHEAP_FLAG_THREAD_SAFE;
945
946 vec_validate (utm->rx_buf, 8192);
947
948 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
949
Florin Corase04c2992017-03-01 08:17:34 -0800950 utm->my_pid = getpid ();
951 utm->configured_segment_size = 1 << 20;
952
Dave Barach68b0fb02017-02-28 15:15:56 -0500953 clib_time_init (&utm->clib_time);
954 init_error_string_table (utm);
955 svm_fifo_segment_init (0x200000000ULL, 20);
956 unformat_init_command_line (a, argv);
957
958 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
959 {
960 if (unformat (a, "chroot prefix %s", &chroot_prefix))
961 {
962 vl_set_memory_root_path ((char *) chroot_prefix);
963 }
964 else if (unformat (a, "uri %s", &bind_name))
965 ;
Florin Corase04c2992017-03-01 08:17:34 -0800966 else if (unformat (a, "segment-size %dM", &tmp))
967 utm->configured_segment_size = tmp << 20;
968 else if (unformat (a, "segment-size %dG", &tmp))
969 utm->configured_segment_size = tmp << 30;
970 else if (unformat (a, "master"))
971 i_am_master = 1;
972 else if (unformat (a, "slave"))
973 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500974 else
975 {
976 fformat (stderr, "%s: usage [master|slave]\n");
977 exit (1);
978 }
979 }
980
Florin Corase04c2992017-03-01 08:17:34 -0800981 utm->cut_through_session_index = ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500982 utm->uri = format (0, "%s%c", bind_name, 0);
Florin Corase04c2992017-03-01 08:17:34 -0800983 utm->i_am_master = i_am_master;
984 utm->segment_main = &svm_fifo_segment_main;
985
Florin Coras6cf30ad2017-04-04 23:08:23 -0700986 utm->connect_uri = format (0, "udp://6.0.0.1/1234%c", 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500987
988 setup_signal_handlers ();
989
990 uri_api_hookup (utm);
991
Florin Corase04c2992017-03-01 08:17:34 -0800992 if (connect_to_vpp (i_am_master ? "uri_udp_master" : "uri_udp_slave") < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500993 {
994 svm_region_exit ();
995 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
996 exit (1);
997 }
998
Florin Corase04c2992017-03-01 08:17:34 -0800999 if (i_am_master == 0)
1000 {
Florin Coras6cf30ad2017-04-04 23:08:23 -07001001 uri_udp_client_test (utm);
Florin Corase04c2992017-03-01 08:17:34 -08001002 exit (0);
1003 }
1004
Dave Barach68b0fb02017-02-28 15:15:56 -05001005 /* $$$$ hack preallocation */
1006 for (i = 0; i < 200000; i++)
1007 {
1008 pool_get (utm->sessions, session);
1009 memset (session, 0, sizeof (*session));
1010 }
1011 for (i = 0; i < 200000; i++)
1012 pool_put_index (utm->sessions, i);
1013
Florin Coras6cf30ad2017-04-04 23:08:23 -07001014 udp_server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001015
1016 vl_client_disconnect_from_vlib ();
1017 exit (0);
1018}
1019
1020#undef vl_api_version
1021#define vl_api_version(n,v) static u32 vpe_api_version = v;
Florin Corase04c2992017-03-01 08:17:34 -08001022#include <vpp/api/vpe.api.h>
Dave Barach68b0fb02017-02-28 15:15:56 -05001023#undef vl_api_version
1024
1025void
1026vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1027{
1028 /*
1029 * Send the main API signature in slot 0. This bit of code must
1030 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1031 */
1032 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1033}
1034
Florin Corase04c2992017-03-01 08:17:34 -08001035u32
1036vl (void *p)
1037{
1038 return vec_len (p);
1039}
1040
Dave Barach68b0fb02017-02-28 15:15:56 -05001041/*
1042 * fd.io coding-style-patch-verification: ON
1043 *
1044 * Local Variables:
1045 * eval: (c-set-style "gnu")
1046 * End:
1047 */