blob: 887150cca82329db5267df6f66336723a80e7e29 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <setjmp.h>
18#include <signal.h>
19#include <vppinfra/clib.h>
20#include <vppinfra/format.h>
21#include <vppinfra/error.h>
22#include <vppinfra/time.h>
23#include <vppinfra/macros.h>
24#include <vnet/vnet.h>
25#include <vlib/vlib.h>
26#include <vlib/unix/unix.h>
27#include <vlibapi/api.h>
28#include <vlibmemory/api.h>
Florin Corase04c2992017-03-01 08:17:34 -080029#include <vpp/api/vpe_msg_enum.h>
30#include <svm/svm_fifo_segment.h>
31#include <pthread.h>
32#include <vnet/session/application_interface.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050033
34#define vl_typedefs /* define message structures */
Florin Corase04c2992017-03-01 08:17:34 -080035#include <vpp/api/vpe_all_api_h.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050036#undef vl_typedefs
37
38/* declare message handlers for each api */
39
40#define vl_endianfun /* define message structures */
Florin Corase04c2992017-03-01 08:17:34 -080041#include <vpp/api/vpe_all_api_h.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050042#undef vl_endianfun
43
44/* instantiate all the print functions we know about */
45#define vl_print(handle, ...)
46#define vl_printfun
Florin Corase04c2992017-03-01 08:17:34 -080047#include <vpp/api/vpe_all_api_h.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050048#undef vl_printfun
49
Dave Barach68b0fb02017-02-28 15:15:56 -050050typedef enum
51{
52 STATE_START,
Florin Coras52207f12018-07-12 14:48:06 -070053 STATE_ATTACHED,
Florin Coras3cbc04b2017-10-02 00:18:51 -070054 STATE_BOUND,
Dave Barach68b0fb02017-02-28 15:15:56 -050055 STATE_READY,
Florin Coras6cf30ad2017-04-04 23:08:23 -070056 STATE_FAILED,
Dave Barach68b0fb02017-02-28 15:15:56 -050057 STATE_DISCONNECTING,
Florin Coras52207f12018-07-12 14:48:06 -070058 STATE_DETACHED
Dave Barach68b0fb02017-02-28 15:15:56 -050059} connection_state_t;
60
61typedef struct
62{
Dave Barach68b0fb02017-02-28 15:15:56 -050063 /* vpe input queue */
Florin Corase86a8ed2018-01-05 03:20:25 -080064 svm_queue_t *vl_input_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050065
66 /* API client handle */
67 u32 my_client_index;
68
69 /* The URI we're playing with */
Florin Coras7fb0fe12018-04-09 09:24:52 -070070 u8 *listen_uri;
71
72 /* URI for connect */
73 u8 *connect_uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050074
75 /* Session pool */
Florin Coras7fb0fe12018-04-09 09:24:52 -070076 app_session_t *sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -050077
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 Coras7fb0fe12018-04-09 09:24:52 -070087 u32 fifo_size;
88 int i_am_server;
89 u8 is_connected;
Florin Corase04c2992017-03-01 08:17:34 -080090
Dave Barach68b0fb02017-02-28 15:15:56 -050091 /* Our event queue */
Florin Coras3c2fed52018-07-04 04:15:05 -070092 svm_msg_q_t *our_event_queue;
Florin Corasa44d6b12018-10-03 14:29:10 -070093 svm_msg_q_t *ct_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050094
95 /* $$$ single thread only for the moment */
Florin Coras3c2fed52018-07-04 04:15:05 -070096 svm_msg_q_t *vpp_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050097
Florin Corase04c2992017-03-01 08:17:34 -080098 /* $$$$ hack: cut-through session index */
99 volatile u32 cut_through_session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700100 volatile u32 connected_session;
Florin Corase04c2992017-03-01 08:17:34 -0800101
102 /* unique segment name counter */
103 u32 unique_segment_index;
104
105 pid_t my_pid;
106
107 /* pthread handle */
108 pthread_t cut_through_thread_handle;
109
Dave Barach68b0fb02017-02-28 15:15:56 -0500110 /* For deadman timers */
111 clib_time_t clib_time;
112
113 /* State of the connection, shared between msg RX thread and main thread */
114 volatile connection_state_t state;
115
116 volatile int time_to_stop;
117 volatile int time_to_print_stats;
118
Florin Corase04c2992017-03-01 08:17:34 -0800119 u32 configured_segment_size;
120
Dave Barach68b0fb02017-02-28 15:15:56 -0500121 /* VNET_API_ERROR_FOO -> "Foo" hash table */
122 uword *error_string_by_error_number;
Florin Corase04c2992017-03-01 08:17:34 -0800123
124 /* convenience */
125 svm_fifo_segment_main_t *segment_main;
126
Florin Coras3cbc04b2017-10-02 00:18:51 -0700127 u8 *connect_test_data;
Florin Corasf8f516a2018-02-08 15:10:09 -0800128
129 uword *segments_table;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700130 u8 do_echo;
Florin Coras8e43d042018-05-04 15:46:57 -0700131 u8 have_return;
Florin Corasa44d6b12018-10-03 14:29:10 -0700132 u64 total_to_send;
Florin Coras8e43d042018-05-04 15:46:57 -0700133 u64 bytes_to_send;
Florin Corasa44d6b12018-10-03 14:29:10 -0700134 u64 bytes_sent;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700135} udp_echo_main_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500136
Florin Coras7fb0fe12018-04-09 09:24:52 -0700137udp_echo_main_t udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500138
139static void
140stop_signal (int signum)
141{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700142 udp_echo_main_t *um = &udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500143
144 um->time_to_stop = 1;
145}
146
147static void
148stats_signal (int signum)
149{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700150 udp_echo_main_t *um = &udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500151 um->time_to_print_stats = 1;
152}
153
154static clib_error_t *
155setup_signal_handlers (void)
156{
157 signal (SIGINT, stats_signal);
158 signal (SIGQUIT, stop_signal);
159 signal (SIGTERM, stop_signal);
160
161 return 0;
162}
163
Florin Coras7fb0fe12018-04-09 09:24:52 -0700164uword
165unformat_ip4_address (unformat_input_t * input, va_list * args)
166{
167 u8 *result = va_arg (*args, u8 *);
168 unsigned a[4];
169
170 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
171 return 0;
172
173 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
174 return 0;
175
176 result[0] = a[0];
177 result[1] = a[1];
178 result[2] = a[2];
179 result[3] = a[3];
180
181 return 1;
182}
183
184uword
185unformat_ip6_address (unformat_input_t * input, va_list * args)
186{
187 ip6_address_t *result = va_arg (*args, ip6_address_t *);
188 u16 hex_quads[8];
189 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
190 uword c, n_colon, double_colon_index;
191
192 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
193 double_colon_index = ARRAY_LEN (hex_quads);
194 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
195 {
196 hex_digit = 16;
197 if (c >= '0' && c <= '9')
198 hex_digit = c - '0';
199 else if (c >= 'a' && c <= 'f')
200 hex_digit = c + 10 - 'a';
201 else if (c >= 'A' && c <= 'F')
202 hex_digit = c + 10 - 'A';
203 else if (c == ':' && n_colon < 2)
204 n_colon++;
205 else
206 {
207 unformat_put_input (input);
208 break;
209 }
210
211 /* Too many hex quads. */
212 if (n_hex_quads >= ARRAY_LEN (hex_quads))
213 return 0;
214
215 if (hex_digit < 16)
216 {
217 hex_quad = (hex_quad << 4) | hex_digit;
218
219 /* Hex quad must fit in 16 bits. */
220 if (n_hex_digits >= 4)
221 return 0;
222
223 n_colon = 0;
224 n_hex_digits++;
225 }
226
227 /* Save position of :: */
228 if (n_colon == 2)
229 {
230 /* More than one :: ? */
231 if (double_colon_index < ARRAY_LEN (hex_quads))
232 return 0;
233 double_colon_index = n_hex_quads;
234 }
235
236 if (n_colon > 0 && n_hex_digits > 0)
237 {
238 hex_quads[n_hex_quads++] = hex_quad;
239 hex_quad = 0;
240 n_hex_digits = 0;
241 }
242 }
243
244 if (n_hex_digits > 0)
245 hex_quads[n_hex_quads++] = hex_quad;
246
247 {
248 word i;
249
250 /* Expand :: to appropriate number of zero hex quads. */
251 if (double_colon_index < ARRAY_LEN (hex_quads))
252 {
253 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
254
255 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
256 hex_quads[n_zero + i] = hex_quads[i];
257
258 for (i = 0; i < n_zero; i++)
259 hex_quads[double_colon_index + i] = 0;
260
261 n_hex_quads = ARRAY_LEN (hex_quads);
262 }
263
264 /* Too few hex quads given. */
265 if (n_hex_quads < ARRAY_LEN (hex_quads))
266 return 0;
267
268 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
269 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
270
271 return 1;
272 }
273}
274
275uword
276unformat_uri (unformat_input_t * input, va_list * args)
277{
Florin Coras5665ced2018-10-25 18:03:45 -0700278 session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700279 u32 port;
280 char *tmp;
281
282 if (unformat (input, "%s://%U/%d", &tmp, unformat_ip4_address, &sep->ip.ip4,
283 &port))
284 {
285 sep->port = clib_host_to_net_u16 (port);
286 sep->is_ip4 = 1;
287 return 1;
288 }
289 else if (unformat (input, "%s://%U/%d", &tmp, unformat_ip6_address,
290 &sep->ip.ip6, &port))
291 {
292 sep->port = clib_host_to_net_u16 (port);
293 sep->is_ip4 = 0;
294 return 1;
295 }
296 return 0;
297}
298
Florin Coras52207f12018-07-12 14:48:06 -0700299static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700300application_send_attach (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700301{
302 vl_api_application_attach_t *bmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700303 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400304 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305
306 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
307 bmp->client_index = utm->my_client_index;
308 bmp->context = ntohl (0xfeedface);
Florin Corasf8f516a2018-02-08 15:10:09 -0800309 bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ADD_SEGMENT;
310 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
311 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
Florin Coras52207f12018-07-12 14:48:06 -0700312 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700313 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700314 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = utm->fifo_size;
315 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = utm->fifo_size;
Florin Corasff6e7692017-12-11 04:59:01 -0800316 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
317 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
318 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 16768;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700319 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
320}
321
322void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700323application_detach (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700324{
325 vl_api_application_detach_t *bmp;
326 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400327 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700328
329 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
330 bmp->client_index = utm->my_client_index;
331 bmp->context = ntohl (0xfeedface);
332 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
333}
334
335static void
336vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
337 mp)
338{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700339 udp_echo_main_t *utm = &udp_echo_main;
Florin Coras84a30ef2018-01-24 10:49:23 -0800340 svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700341 int rv;
342
343 if (mp->retval)
344 {
345 clib_warning ("attach failed: %d", mp->retval);
346 utm->state = STATE_FAILED;
347 return;
348 }
349
350 if (mp->segment_name_length == 0)
351 {
352 clib_warning ("segment_name_length zero");
353 return;
354 }
355
356 a->segment_name = (char *) mp->segment_name;
357 a->segment_size = mp->segment_size;
358
359 ASSERT (mp->app_event_queue_address);
360
361 /* Attach to the segment vpp created */
362 rv = svm_fifo_segment_attach (a);
363 if (rv)
364 {
365 clib_warning ("svm_fifo_segment_attach ('%s') failed",
366 mp->segment_name);
367 return;
368 }
369
Florin Corasa44d6b12018-10-03 14:29:10 -0700370 utm->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
371 svm_msg_q_t *);
Florin Coras52207f12018-07-12 14:48:06 -0700372 utm->state = STATE_ATTACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700373}
374
375static void
376vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
377 mp)
378{
379 if (mp->retval)
380 clib_warning ("detach returned with err: %d", mp->retval);
Florin Coras52207f12018-07-12 14:48:06 -0700381 udp_echo_main.state = STATE_DETACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700382}
383
Dave Barach68b0fb02017-02-28 15:15:56 -0500384u8 *
385format_api_error (u8 * s, va_list * args)
386{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700387 udp_echo_main_t *utm = va_arg (*args, udp_echo_main_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500388 i32 error = va_arg (*args, u32);
389 uword *p;
390
391 p = hash_get (utm->error_string_by_error_number, -error);
392
393 if (p)
394 s = format (s, "%s", p[0]);
395 else
396 s = format (s, "%d", error);
397 return s;
398}
399
400int
Florin Coras7fb0fe12018-04-09 09:24:52 -0700401wait_for_state_change (udp_echo_main_t * utm, connection_state_t state)
Dave Barach68b0fb02017-02-28 15:15:56 -0500402{
Florin Corase04c2992017-03-01 08:17:34 -0800403#if CLIB_DEBUG > 0
404#define TIMEOUT 600.0
405#else
406#define TIMEOUT 600.0
407#endif
408
409 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500410
411 while (clib_time_now (&utm->clib_time) < timeout)
412 {
413 if (utm->state == state)
414 return 0;
Florin Corasa44d6b12018-10-03 14:29:10 -0700415 if (utm->state == STATE_FAILED)
416 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500417 }
418 return -1;
419}
420
Florin Corase04c2992017-03-01 08:17:34 -0800421u64 server_bytes_received, server_bytes_sent;
422
423static void *
424cut_through_thread_fn (void *arg)
425{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700426 app_session_t *s;
Florin Corase04c2992017-03-01 08:17:34 -0800427 svm_fifo_t *rx_fifo;
428 svm_fifo_t *tx_fifo;
429 u8 *my_copy_buffer = 0;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700430 udp_echo_main_t *utm = &udp_echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800431 i32 actual_transfer;
Florin Corasa44d6b12018-10-03 14:29:10 -0700432 int rv, do_dequeue = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800433 u32 buffer_offset;
434
435 while (utm->cut_through_session_index == ~0)
436 ;
437
438 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
439
Florin Coras7fb0fe12018-04-09 09:24:52 -0700440 rx_fifo = s->rx_fifo;
441 tx_fifo = s->tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -0800442
443 vec_validate (my_copy_buffer, 64 * 1024 - 1);
444
Florin Corasa44d6b12018-10-03 14:29:10 -0700445 while (1)
Florin Corase04c2992017-03-01 08:17:34 -0800446 {
Florin Corase04c2992017-03-01 08:17:34 -0800447 do
448 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700449 /* We read from the tx fifo and write to the rx fifo */
450 if (utm->have_return || do_dequeue)
451 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo,
452 vec_len
453 (my_copy_buffer),
454 my_copy_buffer);
455 else
456 {
457 /* We don't do anything with the data, drop it */
458 actual_transfer = svm_fifo_max_dequeue (rx_fifo);
459 svm_fifo_dequeue_drop (rx_fifo, actual_transfer);
460 }
Florin Corase04c2992017-03-01 08:17:34 -0800461 }
462 while (actual_transfer <= 0);
463
464 server_bytes_received += actual_transfer;
465
Florin Corasa44d6b12018-10-03 14:29:10 -0700466 if (utm->have_return)
Florin Corase04c2992017-03-01 08:17:34 -0800467 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700468 buffer_offset = 0;
469 while (actual_transfer > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800470 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700471 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer,
472 my_copy_buffer + buffer_offset);
473 if (rv > 0)
474 {
475 actual_transfer -= rv;
476 buffer_offset += rv;
477 server_bytes_sent += rv;
478 }
Florin Corase04c2992017-03-01 08:17:34 -0800479
Florin Corasa44d6b12018-10-03 14:29:10 -0700480 }
Florin Corase04c2992017-03-01 08:17:34 -0800481 }
482 if (PREDICT_FALSE (utm->time_to_stop))
483 break;
484 }
485
486 pthread_exit (0);
487}
488
489static void
Florin Coras52207f12018-07-12 14:48:06 -0700490session_accepted_handler (session_accepted_msg_t * mp)
491{
492 app_session_evt_t _app_evt, *app_evt = &_app_evt;
493 udp_echo_main_t *utm = &udp_echo_main;
494 session_accepted_reply_msg_t *rmp;
495 svm_fifo_t *rx_fifo, *tx_fifo;
496 app_session_t *session;
497 static f64 start_time;
498 u32 session_index;
499 int rv = 0;
500
501 if (start_time == 0.0)
502 start_time = clib_time_now (&utm->clib_time);
503
504 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
505 svm_msg_q_t *);
506 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
507 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
508
509 pool_get (utm->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -0400510 clib_memset (session, 0, sizeof (*session));
Florin Coras52207f12018-07-12 14:48:06 -0700511 session_index = session - utm->sessions;
Florin Corasa44d6b12018-10-03 14:29:10 -0700512 session->session_index = session_index;
Florin Coras52207f12018-07-12 14:48:06 -0700513
514 /* Cut-through case */
515 if (mp->server_event_queue_address)
516 {
517 clib_warning ("cut-through session");
Florin Corasa44d6b12018-10-03 14:29:10 -0700518 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
519 svm_msg_q_t *);
520 sleep (1);
Florin Coras52207f12018-07-12 14:48:06 -0700521 rx_fifo->master_session_index = session_index;
522 tx_fifo->master_session_index = session_index;
523 utm->cut_through_session_index = session_index;
524 session->rx_fifo = rx_fifo;
525 session->tx_fifo = tx_fifo;
Florin Corasa44d6b12018-10-03 14:29:10 -0700526 session->is_dgram = 0;
Florin Coras52207f12018-07-12 14:48:06 -0700527
528 rv = pthread_create (&utm->cut_through_thread_handle,
529 NULL /*attr */ , cut_through_thread_fn, 0);
530 if (rv)
531 {
532 clib_warning ("pthread_create returned %d", rv);
533 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
534 }
Florin Coras52207f12018-07-12 14:48:06 -0700535 }
536 else
537 {
538 rx_fifo->client_session_index = session_index;
539 tx_fifo->client_session_index = session_index;
540 session->rx_fifo = rx_fifo;
541 session->tx_fifo = tx_fifo;
542 clib_memcpy (&session->transport.rmt_ip, mp->ip,
543 sizeof (ip46_address_t));
544 session->transport.is_ip4 = mp->is_ip4;
545 session->transport.rmt_port = mp->port;
546 }
547
548 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
549 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
550 {
551 f64 now = clib_time_now (&utm->clib_time);
552 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
553 pool_elts (utm->sessions), now - start_time,
554 (f64) pool_elts (utm->sessions) / (now - start_time));
555 }
556
Florin Corasa44d6b12018-10-03 14:29:10 -0700557 app_alloc_ctrl_evt_to_vpp (utm->vpp_event_queue, app_evt,
Florin Coras52207f12018-07-12 14:48:06 -0700558 SESSION_CTRL_EVT_ACCEPTED_REPLY);
559 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
560 rmp->handle = mp->handle;
561 rmp->context = mp->context;
562 rmp->retval = rv;
Florin Corasa44d6b12018-10-03 14:29:10 -0700563 app_send_ctrl_evt_to_vpp (utm->vpp_event_queue, app_evt);
Florin Coras52207f12018-07-12 14:48:06 -0700564
565 CLIB_MEMORY_BARRIER ();
566 utm->state = STATE_READY;
567}
568
569static void
570session_disconnected_handler (session_disconnected_msg_t * mp)
571{
572 app_session_evt_t _app_evt, *app_evt = &_app_evt;
573 udp_echo_main_t *utm = &udp_echo_main;
574 session_disconnected_reply_msg_t *rmp;
575 app_session_t *session;
576 uword *p;
577 int rv = 0;
578
579 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
580
581 if (p)
582 {
583 session = pool_elt_at_index (utm->sessions, p[0]);
584 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Florin Corasa44d6b12018-10-03 14:29:10 -0700585 clib_warning ("disconnecting %u", session->session_index);
Florin Coras52207f12018-07-12 14:48:06 -0700586 pool_put (utm->sessions, session);
587 }
588 else
589 {
590 clib_warning ("couldn't find session key %llx", mp->handle);
591 return;
592 }
593
594 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
595 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
596 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
597 rmp->retval = rv;
598 rmp->handle = mp->handle;
599 rmp->context = mp->context;
600 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
601}
602
603static void
604session_connected_handler (session_connected_msg_t * mp)
605{
606 udp_echo_main_t *utm = &udp_echo_main;
607 unformat_input_t _input, *input = &_input;
Florin Coras5665ced2018-10-25 18:03:45 -0700608 session_endpoint_cfg_t _sep, *sep = &_sep;
Florin Coras52207f12018-07-12 14:48:06 -0700609 app_session_t *session;
610
611 ASSERT (utm->i_am_server == 0);
612
613 if (mp->retval)
614 {
615 clib_warning ("failed connect");
616 return;
617 }
618
619 ASSERT (mp->server_rx_fifo && mp->server_tx_fifo);
620
621 pool_get (utm->sessions, session);
Florin Corasa44d6b12018-10-03 14:29:10 -0700622 session->session_index = session - utm->sessions;
Florin Coras52207f12018-07-12 14:48:06 -0700623 session->rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
624 session->tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasa44d6b12018-10-03 14:29:10 -0700625
Florin Coras52207f12018-07-12 14:48:06 -0700626 /* Cut-through case */
627 if (mp->client_event_queue_address)
628 {
629 clib_warning ("cut-through session");
Florin Corasa44d6b12018-10-03 14:29:10 -0700630 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
631 svm_msg_q_t *);
632 utm->ct_event_queue = uword_to_pointer (mp->client_event_queue_address,
633 svm_msg_q_t *);
634 utm->cut_through_session_index = session->session_index;
635 session->is_dgram = 0;
636 sleep (1);
637 session->rx_fifo->client_session_index = session->session_index;
638 session->tx_fifo->client_session_index = session->session_index;
Florin Coras52207f12018-07-12 14:48:06 -0700639 }
640 else
641 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700642 utm->connected_session = session->session_index;
Florin Coras52207f12018-07-12 14:48:06 -0700643 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
644 svm_msg_q_t *);
645
Florin Corasa44d6b12018-10-03 14:29:10 -0700646 session->rx_fifo->client_session_index = session->session_index;
647 session->tx_fifo->client_session_index = session->session_index;
Florin Coras52207f12018-07-12 14:48:06 -0700648 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
649 sizeof (ip46_address_t));
650 session->transport.is_ip4 = mp->is_ip4;
651 session->transport.lcl_port = mp->lcl_port;
652
653 unformat_init_vector (input, utm->connect_uri);
654 if (!unformat (input, "%U", unformat_uri, sep))
655 {
656 clib_warning ("can't figure out remote ip and port");
657 utm->state = STATE_FAILED;
658 unformat_free (input);
659 return;
660 }
661 unformat_free (input);
662 clib_memcpy (&session->transport.rmt_ip, &sep->ip,
663 sizeof (ip46_address_t));
664 session->transport.rmt_port = sep->port;
665 session->is_dgram = !utm->is_connected;
666 }
667 utm->state = STATE_READY;
668}
669
670static void
Florin Corasa44d6b12018-10-03 14:29:10 -0700671session_bound_handler (session_bound_msg_t * mp)
672{
673 udp_echo_main_t *utm = &udp_echo_main;
674 svm_fifo_t *rx_fifo, *tx_fifo;
675 app_session_t *session;
676 u32 session_index;
677
678 if (mp->retval)
679 {
680 clib_warning ("bind failed: %d", mp->retval);
681 utm->state = STATE_FAILED;
682 return;
683 }
684
685 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
686 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
687
688 pool_get (utm->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -0400689 clib_memset (session, 0, sizeof (*session));
Florin Corasa44d6b12018-10-03 14:29:10 -0700690 session_index = session - utm->sessions;
691
692 rx_fifo->client_session_index = session_index;
693 tx_fifo->client_session_index = session_index;
694 session->rx_fifo = rx_fifo;
695 session->tx_fifo = tx_fifo;
696 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
697 sizeof (ip46_address_t));
698 session->transport.is_ip4 = mp->lcl_is_ip4;
699 session->transport.lcl_port = mp->lcl_port;
700 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
701
702 utm->state = utm->is_connected ? STATE_BOUND : STATE_READY;
703}
704
705static void
Florin Coras52207f12018-07-12 14:48:06 -0700706handle_mq_event (session_event_t * e)
707{
708 switch (e->event_type)
709 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700710 case SESSION_CTRL_EVT_BOUND:
711 session_bound_handler ((session_bound_msg_t *) e->data);
712 break;
Florin Coras52207f12018-07-12 14:48:06 -0700713 case SESSION_CTRL_EVT_ACCEPTED:
714 session_accepted_handler ((session_accepted_msg_t *) e->data);
715 break;
716 case SESSION_CTRL_EVT_CONNECTED:
717 session_connected_handler ((session_connected_msg_t *) e->data);
718 break;
719 case SESSION_CTRL_EVT_DISCONNECTED:
720 session_disconnected_handler ((session_disconnected_msg_t *) e->data);
721 break;
722 default:
723 clib_warning ("unhandled %u", e->event_type);
724 }
725}
726
727static void
728udp_client_send_connect (udp_echo_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800729{
730 vl_api_connect_uri_t *cmp;
Florin Corase04c2992017-03-01 08:17:34 -0800731 cmp = vl_msg_api_alloc (sizeof (*cmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400732 clib_memset (cmp, 0, sizeof (*cmp));
Florin Corase04c2992017-03-01 08:17:34 -0800733
734 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
735 cmp->client_index = utm->my_client_index;
736 cmp->context = ntohl (0xfeedface);
737 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
738 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700739}
Florin Corase04c2992017-03-01 08:17:34 -0800740
Florin Coras6cf30ad2017-04-04 23:08:23 -0700741static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700742send_test_chunk (udp_echo_main_t * utm, app_session_t * s, u32 bytes)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700743{
Florin Corasa44d6b12018-10-03 14:29:10 -0700744 u64 test_buf_len, bytes_this_chunk, test_buf_offset;
745
Florin Coras3cbc04b2017-10-02 00:18:51 -0700746 u8 *test_data = utm->connect_test_data;
Florin Coras8e43d042018-05-04 15:46:57 -0700747 u32 bytes_to_snd, enq_space, min_chunk;
Florin Corasa44d6b12018-10-03 14:29:10 -0700748 session_evt_type_t et = FIFO_EVENT_APP_TX;
749 int written;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700750
Florin Corasa44d6b12018-10-03 14:29:10 -0700751 test_buf_len = vec_len (test_data);
752 test_buf_offset = utm->bytes_sent % test_buf_len;
753 bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
754 utm->bytes_to_send);
755 enq_space = svm_fifo_max_enqueue (s->tx_fifo);
756 bytes_this_chunk = clib_min (bytes_this_chunk, enq_space);
757 et += (s->session_index == utm->cut_through_session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700758
Florin Corasa44d6b12018-10-03 14:29:10 -0700759 if (s->is_dgram)
760 written = app_send_dgram_raw (s->tx_fifo, &s->transport, s->vpp_evt_q,
761 test_data + test_buf_offset,
762 bytes_this_chunk, et, SVM_Q_WAIT);
763 else
764 written = app_send_stream_raw (s->tx_fifo, s->vpp_evt_q,
765 test_data + test_buf_offset,
766 bytes_this_chunk, et, SVM_Q_WAIT);
767
768 if (written > 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700769 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700770 utm->bytes_to_send -= written;
771 utm->bytes_sent += written;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700772 }
773}
774
775static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700776recv_test_chunk (udp_echo_main_t * utm, app_session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700777{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700778 app_recv (s, utm->rx_buf, vec_len (utm->rx_buf));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700779}
780
781void
Florin Corasa44d6b12018-10-03 14:29:10 -0700782client_send_data (udp_echo_main_t * utm, u32 session_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700783{
Florin Coras8e43d042018-05-04 15:46:57 -0700784 f64 start_time, end_time, delta;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700785 app_session_t *session;
Florin Coras8e43d042018-05-04 15:46:57 -0700786 char *transfer_type;
Florin Coras8e43d042018-05-04 15:46:57 -0700787 u8 *test_data;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700788 int i;
789
Florin Corasa44d6b12018-10-03 14:29:10 -0700790 vec_validate_aligned (utm->connect_test_data, 1024 * 1024 - 1,
791 CLIB_CACHE_LINE_BYTES);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700792 for (i = 0; i < vec_len (utm->connect_test_data); i++)
793 utm->connect_test_data[i] = i & 0xff;
794
795 test_data = utm->connect_test_data;
Florin Corasa44d6b12018-10-03 14:29:10 -0700796 session = pool_elt_at_index (utm->sessions, session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700797 ASSERT (vec_len (test_data) > 0);
798
Florin Corasa44d6b12018-10-03 14:29:10 -0700799 utm->total_to_send = utm->bytes_to_send;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700800 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
Florin Coras8e43d042018-05-04 15:46:57 -0700801 start_time = clib_time_now (&utm->clib_time);
Florin Corasa44d6b12018-10-03 14:29:10 -0700802 while (!utm->time_to_stop && utm->bytes_to_send)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700803 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700804 send_test_chunk (utm, session, 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700805 if (utm->have_return)
806 recv_test_chunk (utm, session);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700807 if (utm->time_to_stop)
808 break;
809 }
810
Florin Coras8e43d042018-05-04 15:46:57 -0700811 if (utm->have_return)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700812 {
Florin Coras8e43d042018-05-04 15:46:57 -0700813 f64 timeout = clib_time_now (&utm->clib_time) + 5;
814 while (clib_time_now (&utm->clib_time) < timeout)
815 recv_test_chunk (utm, session);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700816 }
817
Florin Coras8e43d042018-05-04 15:46:57 -0700818 end_time = clib_time_now (&utm->clib_time);
819 delta = end_time - start_time;
820 transfer_type = utm->have_return ? "full-duplex" : "half-duplex";
821 clib_warning ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
Florin Corasa44d6b12018-10-03 14:29:10 -0700822 utm->total_to_send, utm->total_to_send / (1ULL << 20),
823 utm->total_to_send / (1ULL << 30), delta);
824 clib_warning ("%.2f bytes/second %s", ((f64) utm->total_to_send) / (delta),
Florin Coras8e43d042018-05-04 15:46:57 -0700825 transfer_type);
826 clib_warning ("%.4f gbit/second %s",
Florin Corasa44d6b12018-10-03 14:29:10 -0700827 (((f64) utm->total_to_send * 8.0) / delta / 1e9),
Florin Coras8e43d042018-05-04 15:46:57 -0700828 transfer_type);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700829}
830
Florin Coras52207f12018-07-12 14:48:06 -0700831static int
832application_attach (udp_echo_main_t * utm)
833{
834 application_send_attach (utm);
835 if (wait_for_state_change (utm, STATE_ATTACHED))
836 {
837 clib_warning ("timeout waiting for STATE_ATTACHED");
838 return -1;
839 }
840 return 0;
841}
842
Florin Coras3cbc04b2017-10-02 00:18:51 -0700843static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700844client_test (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700845{
Florin Coras52207f12018-07-12 14:48:06 -0700846 f64 start_time, timeout = 100.0;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700847 app_session_t *session;
Florin Coras52207f12018-07-12 14:48:06 -0700848 svm_msg_q_msg_t msg;
849 session_event_t *e;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700850
Florin Coras52207f12018-07-12 14:48:06 -0700851 if (application_attach (utm))
852 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700853
Florin Coras52207f12018-07-12 14:48:06 -0700854 udp_client_send_connect (utm);
855
856 start_time = clib_time_now (&utm->clib_time);
Florin Corasa44d6b12018-10-03 14:29:10 -0700857 while (pool_elts (utm->sessions) != 1 && utm->state != STATE_FAILED)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700858 {
Florin Coras52207f12018-07-12 14:48:06 -0700859 svm_msg_q_sub (utm->our_event_queue, &msg, SVM_Q_WAIT, 0);
860 e = svm_msg_q_msg_data (utm->our_event_queue, &msg);
861 handle_mq_event (e);
862 svm_msg_q_free_msg (utm->our_event_queue, &msg);
Florin Corasa44d6b12018-10-03 14:29:10 -0700863
864 if (clib_time_now (&utm->clib_time) - start_time >= timeout)
865 break;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700866 }
867
Florin Coras3cbc04b2017-10-02 00:18:51 -0700868 if (utm->cut_through_session_index != ~0)
Florin Corasa44d6b12018-10-03 14:29:10 -0700869 client_send_data (utm, utm->cut_through_session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700870 else
Florin Corasa44d6b12018-10-03 14:29:10 -0700871 client_send_data (utm, utm->connected_session);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700872
Florin Coras6cf30ad2017-04-04 23:08:23 -0700873 application_detach (utm);
Florin Coras52207f12018-07-12 14:48:06 -0700874 wait_for_state_change (utm, STATE_DETACHED);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700875}
876
877static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500878vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
879{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700880 udp_echo_main_t *utm = &udp_echo_main;
881 svm_fifo_t *rx_fifo, *tx_fifo;
882 app_session_t *session;
883 u32 session_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500884
Florin Coras6cf30ad2017-04-04 23:08:23 -0700885 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500886 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700887 clib_warning ("bind failed: %d", mp->retval);
888 utm->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500889 return;
890 }
891
Florin Coras7fb0fe12018-04-09 09:24:52 -0700892 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
893 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
894
895 pool_get (utm->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -0400896 clib_memset (session, 0, sizeof (*session));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700897 session_index = session - utm->sessions;
898
899 rx_fifo->client_session_index = session_index;
900 tx_fifo->client_session_index = session_index;
901 session->rx_fifo = rx_fifo;
902 session->tx_fifo = tx_fifo;
903 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
904 sizeof (ip46_address_t));
905 session->transport.is_ip4 = mp->lcl_is_ip4;
906 session->transport.lcl_port = mp->lcl_port;
Florin Coras3c2fed52018-07-04 04:15:05 -0700907 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700908
909 utm->state = utm->is_connected ? STATE_BOUND : STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500910}
911
912static void
Florin Corase04c2992017-03-01 08:17:34 -0800913vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
914{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700915 udp_echo_main_t *utm = &udp_echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800916 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasf8f516a2018-02-08 15:10:09 -0800917 svm_fifo_segment_private_t *seg;
918 u8 *seg_name;
Florin Corase04c2992017-03-01 08:17:34 -0800919 int rv;
920
Dave Barachb7b92992018-10-17 10:38:51 -0400921 clib_memset (a, 0, sizeof (*a));
Florin Corase04c2992017-03-01 08:17:34 -0800922 a->segment_name = (char *) mp->segment_name;
923 a->segment_size = mp->segment_size;
924 /* Attach to the segment vpp created */
925 rv = svm_fifo_segment_attach (a);
926 if (rv)
927 {
928 clib_warning ("svm_fifo_segment_attach ('%s') failed",
929 mp->segment_name);
930 return;
931 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800932 seg = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
933 clib_warning ("Mapped new segment '%s' size %d", seg->ssvm.name,
934 seg->ssvm.ssvm_size);
935 seg_name = format (0, "%s", (char *) mp->segment_name);
936 hash_set_mem (utm->segments_table, seg_name, a->new_segment_indices[0]);
937 vec_free (seg_name);
938}
939
940static void
941vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
942{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700943 udp_echo_main_t *utm = &udp_echo_main;
Florin Corasf8f516a2018-02-08 15:10:09 -0800944 svm_fifo_segment_private_t *seg;
945 u64 *seg_indexp;
946 u8 *seg_name;
947
948
949 seg_name = format (0, "%s", mp->segment_name);
950 seg_indexp = hash_get_mem (utm->segments_table, seg_name);
951 if (!seg_indexp)
952 {
953 clib_warning ("segment not mapped: %s", seg_name);
954 return;
955 }
956 hash_unset_mem (utm->segments_table, seg_name);
957 seg = svm_fifo_segment_get_segment ((u32) seg_indexp[0]);
958 svm_fifo_segment_delete (seg);
959 clib_warning ("Unmapped segment '%s'", seg_name);
960 vec_free (seg_name);
Florin Corase04c2992017-03-01 08:17:34 -0800961}
962
Florin Corase04c2992017-03-01 08:17:34 -0800963static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500964vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
965{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700966 udp_echo_main_t *utm = &udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500967
968 if (mp->retval != 0)
969 clib_warning ("returned %d", ntohl (mp->retval));
970
971 utm->state = STATE_START;
972}
973
Florin Corasa44d6b12018-10-03 14:29:10 -0700974static void
975 vl_api_app_cut_through_registration_add_t_handler
976 (vl_api_app_cut_through_registration_add_t * mp)
977{
978
979}
980
981#define foreach_tcp_echo_msg \
982_(BIND_URI_REPLY, bind_uri_reply) \
983_(UNBIND_URI_REPLY, unbind_uri_reply) \
984_(MAP_ANOTHER_SEGMENT, map_another_segment) \
985_(UNMAP_SEGMENT, unmap_segment) \
986_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
987_(APPLICATION_DETACH_REPLY, application_detach_reply) \
988_(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
Dave Barach68b0fb02017-02-28 15:15:56 -0500989
990void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700991tcp_echo_api_hookup (udp_echo_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500992{
993#define _(N,n) \
994 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Corase04c2992017-03-01 08:17:34 -0800995 vl_api_##n##_t_handler, \
Dave Barach68b0fb02017-02-28 15:15:56 -0500996 vl_noop_handler, \
997 vl_api_##n##_t_endian, \
998 vl_api_##n##_t_print, \
999 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -08001000 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -05001001#undef _
1002
1003}
1004
Dave Barach68b0fb02017-02-28 15:15:56 -05001005int
1006connect_to_vpp (char *name)
1007{
Florin Coras7fb0fe12018-04-09 09:24:52 -07001008 udp_echo_main_t *utm = &udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001009 api_main_t *am = &api_main;
1010
1011 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
1012 return -1;
1013
1014 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
1015 utm->my_client_index = am->my_client_index;
1016
1017 return 0;
1018}
1019
1020void
1021vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
1022{
1023 clib_warning ("BUG");
1024}
1025
1026static void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001027init_error_string_table (udp_echo_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001028{
1029 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
1030
1031#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
1032 foreach_vnet_api_error;
1033#undef _
1034
1035 hash_set (utm->error_string_by_error_number, 99, "Misc");
1036}
1037
1038void
Florin Corasa44d6b12018-10-03 14:29:10 -07001039server_handle_fifo_event_rx (udp_echo_main_t * utm, u32 session_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001040{
Florin Corasa44d6b12018-10-03 14:29:10 -07001041 svm_fifo_t *rx_fifo, *tx_fifo;
1042 int n_read;
1043 app_session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -05001044 int rv;
Florin Corasa44d6b12018-10-03 14:29:10 -07001045 u32 max_dequeue, offset, max_transfer, rx_buf_len;
1046 session_evt_type_t et = FIFO_EVENT_APP_TX;
Dave Barach68b0fb02017-02-28 15:15:56 -05001047
Florin Corasa44d6b12018-10-03 14:29:10 -07001048 session = pool_elt_at_index (utm->sessions, session_index);
1049 rx_buf_len = vec_len (utm->rx_buf);
1050 rx_fifo = session->rx_fifo;
1051 tx_fifo = session->tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -05001052
Florin Corasa44d6b12018-10-03 14:29:10 -07001053 et += (session->session_index == utm->cut_through_session_index);
1054
1055 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
1056 /* Allow enqueuing of a new event */
1057 svm_fifo_unset_event (rx_fifo);
1058
1059 if (PREDICT_FALSE (!max_dequeue))
1060 return;
1061
1062 /* Read the max_dequeue */
1063 do
Dave Barach68b0fb02017-02-28 15:15:56 -05001064 {
Florin Corasa44d6b12018-10-03 14:29:10 -07001065 max_transfer = clib_min (rx_buf_len, max_dequeue);
1066 if (session->is_dgram)
1067 n_read = app_recv_dgram_raw (rx_fifo, utm->rx_buf, max_transfer,
1068 &session->transport, 0, 0);
1069 else
1070 n_read = app_recv_stream_raw (rx_fifo, utm->rx_buf, max_transfer, 0,
1071 0);
1072
1073 if (n_read > 0)
1074 max_dequeue -= n_read;
1075
1076 /* Reflect if a non-drop session */
1077 if (utm->have_return && n_read > 0)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001078 {
Florin Corasa44d6b12018-10-03 14:29:10 -07001079 offset = 0;
1080 do
1081 {
1082 if (session->is_dgram)
1083 rv = app_send_dgram_raw (tx_fifo, &session->transport,
1084 session->vpp_evt_q,
1085 &utm->rx_buf[offset], n_read, et,
1086 SVM_Q_WAIT);
1087 else
1088 rv = app_send_stream_raw (tx_fifo, session->vpp_evt_q,
1089 &utm->rx_buf[offset], n_read, et,
1090 SVM_Q_WAIT);
1091 if (rv > 0)
1092 {
1093 n_read -= rv;
1094 offset += rv;
1095 }
1096 }
1097 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
1098
1099 /* If event wasn't set, add one */
1100 if (svm_fifo_set_event (tx_fifo))
1101 app_send_io_evt_to_vpp (session->vpp_evt_q, tx_fifo,
1102 et, SVM_Q_WAIT);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001103 }
Florin Coras6792ec02017-03-13 03:49:51 -07001104 }
Florin Corasa44d6b12018-10-03 14:29:10 -07001105 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
Dave Barach68b0fb02017-02-28 15:15:56 -05001106}
1107
Florin Corasa44d6b12018-10-03 14:29:10 -07001108static void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001109server_handle_event_queue (udp_echo_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001110{
Florin Coras52207f12018-07-12 14:48:06 -07001111 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001112 svm_msg_q_msg_t msg;
Florin Corasa44d6b12018-10-03 14:29:10 -07001113 svm_msg_q_t *mq = utm->our_event_queue;
1114 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001115
Florin Coras3cbc04b2017-10-02 00:18:51 -07001116 while (utm->state != STATE_READY)
1117 sleep (5);
1118
Dave Barach68b0fb02017-02-28 15:15:56 -05001119 while (1)
1120 {
Florin Corasa44d6b12018-10-03 14:29:10 -07001121 if (svm_msg_q_sub (mq, &msg, SVM_Q_WAIT, 0))
Florin Coras3c2fed52018-07-04 04:15:05 -07001122 {
1123 clib_warning ("svm msg q returned");
1124 continue;
1125 }
Florin Corasa44d6b12018-10-03 14:29:10 -07001126 e = svm_msg_q_msg_data (mq, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -05001127 switch (e->event_type)
1128 {
Florin Corasa5464812017-04-19 13:00:05 -07001129 case FIFO_EVENT_APP_RX:
Florin Corasa44d6b12018-10-03 14:29:10 -07001130 server_handle_fifo_event_rx (utm, e->fifo->client_session_index);
1131 break;
1132 case SESSION_IO_EVT_CT_TX:
Dave Barach68b0fb02017-02-28 15:15:56 -05001133 break;
1134
Dave Barach68b0fb02017-02-28 15:15:56 -05001135 default:
Florin Coras52207f12018-07-12 14:48:06 -07001136 handle_mq_event (e);
Dave Barach68b0fb02017-02-28 15:15:56 -05001137 break;
1138 }
Florin Corasa44d6b12018-10-03 14:29:10 -07001139 svm_msg_q_free_msg (mq, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -05001140 if (PREDICT_FALSE (utm->time_to_stop == 1))
Florin Coras3cbc04b2017-10-02 00:18:51 -07001141 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001142 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1143 {
1144 utm->time_to_print_stats = 0;
1145 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1146 }
1147 }
1148}
1149
Florin Coras6cf30ad2017-04-04 23:08:23 -07001150static void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001151server_unbind (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001152{
1153 vl_api_unbind_uri_t *ump;
1154
1155 ump = vl_msg_api_alloc (sizeof (*ump));
Dave Barachb7b92992018-10-17 10:38:51 -04001156 clib_memset (ump, 0, sizeof (*ump));
Florin Coras6cf30ad2017-04-04 23:08:23 -07001157
1158 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1159 ump->client_index = utm->my_client_index;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001160 memcpy (ump->uri, utm->listen_uri, vec_len (utm->listen_uri));
Florin Coras6cf30ad2017-04-04 23:08:23 -07001161 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1162}
1163
1164static void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001165server_bind (udp_echo_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001166{
1167 vl_api_bind_uri_t *bmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001168
1169 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001170 clib_memset (bmp, 0, sizeof (*bmp));
Dave Barach68b0fb02017-02-28 15:15:56 -05001171
1172 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1173 bmp->client_index = utm->my_client_index;
1174 bmp->context = ntohl (0xfeedface);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001175 memcpy (bmp->uri, utm->listen_uri, vec_len (utm->listen_uri));
Dave Barach68b0fb02017-02-28 15:15:56 -05001176 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001177}
1178
1179void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001180udp_server_test (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001181{
Florin Coras7fb0fe12018-04-09 09:24:52 -07001182 u8 wait_for_state = utm->is_connected ? STATE_BOUND : STATE_READY;
Florin Corasa5464812017-04-19 13:00:05 -07001183 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001184
1185 /* Bind to uri */
Florin Coras3cbc04b2017-10-02 00:18:51 -07001186 server_bind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001187
Florin Coras7fb0fe12018-04-09 09:24:52 -07001188 if (wait_for_state_change (utm, wait_for_state))
Dave Barach68b0fb02017-02-28 15:15:56 -05001189 {
Florin Coras7fb0fe12018-04-09 09:24:52 -07001190 clib_warning ("timeout waiting for state change");
Dave Barach68b0fb02017-02-28 15:15:56 -05001191 return;
1192 }
1193
Florin Corase04c2992017-03-01 08:17:34 -08001194 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001195
Florin Coras6cf30ad2017-04-04 23:08:23 -07001196 /* Cleanup */
1197 server_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001198
1199 if (wait_for_state_change (utm, STATE_START))
1200 {
1201 clib_warning ("timeout waiting for STATE_START");
1202 return;
1203 }
1204
Florin Coras6cf30ad2017-04-04 23:08:23 -07001205 application_detach (utm);
1206
Dave Barach68b0fb02017-02-28 15:15:56 -05001207 fformat (stdout, "Test complete...\n");
1208}
1209
1210int
1211main (int argc, char **argv)
1212{
Florin Coras7fb0fe12018-04-09 09:24:52 -07001213 udp_echo_main_t *utm = &udp_echo_main;
Florin Coras8e43d042018-05-04 15:46:57 -07001214 u8 *uri = (u8 *) "udp://6.0.1.1/1234";
Florin Corasb384b542018-01-15 01:08:33 -08001215 unformat_input_t _argv, *a = &_argv;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001216 int i_am_server = 1;
1217 app_session_t *session;
Florin Corasb384b542018-01-15 01:08:33 -08001218 u8 *chroot_prefix;
1219 char *app_name;
Florin Corasb384b542018-01-15 01:08:33 -08001220 u32 tmp;
1221 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001222
Dave Barach6a5adc32018-07-04 10:56:23 -04001223 clib_mem_init_thread_safe (0, 256 << 20);
1224
Florin Coras8e43d042018-05-04 15:46:57 -07001225 svm_fifo_segment_main_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001226
Florin Corasa44d6b12018-10-03 14:29:10 -07001227 vec_validate (utm->rx_buf, 128 << 10);
Dave Barach68b0fb02017-02-28 15:15:56 -05001228 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Corase04c2992017-03-01 08:17:34 -08001229 utm->my_pid = getpid ();
1230 utm->configured_segment_size = 1 << 20;
Florin Corasf8f516a2018-02-08 15:10:09 -08001231 utm->segments_table = hash_create_vec (0, sizeof (u8), sizeof (u64));
Florin Coras8e43d042018-05-04 15:46:57 -07001232 utm->have_return = 1;
1233 utm->bytes_to_send = 1024;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001234 utm->fifo_size = 128 << 10;
Florin Coras8e43d042018-05-04 15:46:57 -07001235 utm->segment_main = &svm_fifo_segment_main;
1236 utm->cut_through_session_index = ~0;
1237 clib_time_init (&utm->clib_time);
1238
1239 init_error_string_table (utm);
1240 unformat_init_command_line (a, argv);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001241
Dave Barach68b0fb02017-02-28 15:15:56 -05001242 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1243 {
1244 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1245 {
1246 vl_set_memory_root_path ((char *) chroot_prefix);
1247 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001248 else if (unformat (a, "uri %s", &uri))
Dave Barach68b0fb02017-02-28 15:15:56 -05001249 ;
Florin Corase04c2992017-03-01 08:17:34 -08001250 else if (unformat (a, "segment-size %dM", &tmp))
1251 utm->configured_segment_size = tmp << 20;
1252 else if (unformat (a, "segment-size %dG", &tmp))
1253 utm->configured_segment_size = tmp << 30;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001254 else if (unformat (a, "server"))
1255 i_am_server = 1;
1256 else if (unformat (a, "client"))
1257 i_am_server = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001258 else if (unformat (a, "no-return"))
1259 utm->have_return = 0;
1260 else if (unformat (a, "mbytes %d", &tmp))
1261 utm->bytes_to_send = (u64) tmp << 20;
1262 else if (unformat (a, "fifo-size %d", &tmp))
1263 utm->fifo_size = tmp << 10;
Dave Barach68b0fb02017-02-28 15:15:56 -05001264 else
1265 {
Florin Coras7fb0fe12018-04-09 09:24:52 -07001266 fformat (stderr, "%s: usage [server|client]\n");
Dave Barach68b0fb02017-02-28 15:15:56 -05001267 exit (1);
1268 }
1269 }
1270
Florin Coras7fb0fe12018-04-09 09:24:52 -07001271 utm->i_am_server = i_am_server;
Dave Barach68b0fb02017-02-28 15:15:56 -05001272
1273 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001274 tcp_echo_api_hookup (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001275
Florin Coras7fb0fe12018-04-09 09:24:52 -07001276 if (i_am_server)
1277 {
1278 utm->listen_uri = format (0, "%s%c", uri, 0);
1279 utm->is_connected = (utm->listen_uri[4] == 'c');
1280 app_name = "udp_echo_server";
1281 }
1282 else
1283 {
1284 app_name = "udp_echo_client";
1285 utm->connect_uri = format (0, "%s%c", uri, 0);
1286 utm->is_connected = (utm->connect_uri[4] == 'c');
1287 }
Florin Corasb384b542018-01-15 01:08:33 -08001288 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001289 {
1290 svm_region_exit ();
1291 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1292 exit (1);
1293 }
1294
Florin Coras7fb0fe12018-04-09 09:24:52 -07001295 if (i_am_server == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001296 {
Florin Coras3cbc04b2017-10-02 00:18:51 -07001297 client_test (utm);
Florin Coras8e43d042018-05-04 15:46:57 -07001298 goto done;
Florin Corase04c2992017-03-01 08:17:34 -08001299 }
1300
Dave Barach68b0fb02017-02-28 15:15:56 -05001301 /* $$$$ hack preallocation */
1302 for (i = 0; i < 200000; i++)
1303 {
1304 pool_get (utm->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -04001305 clib_memset (session, 0, sizeof (*session));
Dave Barach68b0fb02017-02-28 15:15:56 -05001306 }
1307 for (i = 0; i < 200000; i++)
1308 pool_put_index (utm->sessions, i);
1309
Florin Coras6cf30ad2017-04-04 23:08:23 -07001310 udp_server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001311
Florin Coras8e43d042018-05-04 15:46:57 -07001312done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001313 vl_client_disconnect_from_vlib ();
1314 exit (0);
1315}
1316
1317#undef vl_api_version
1318#define vl_api_version(n,v) static u32 vpe_api_version = v;
Florin Corase04c2992017-03-01 08:17:34 -08001319#include <vpp/api/vpe.api.h>
Dave Barach68b0fb02017-02-28 15:15:56 -05001320#undef vl_api_version
1321
1322void
1323vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1324{
1325 /*
1326 * Send the main API signature in slot 0. This bit of code must
1327 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1328 */
1329 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1330}
1331
Florin Corase04c2992017-03-01 08:17:34 -08001332u32
1333vl (void *p)
1334{
1335 return vec_len (p);
1336}
1337
Dave Barach68b0fb02017-02-28 15:15:56 -05001338/*
1339 * fd.io coding-style-patch-verification: ON
1340 *
1341 * Local Variables:
1342 * eval: (c-set-style "gnu")
1343 * End:
1344 */