blob: 91d5dccedccfcad9ac13f3f248f7086b93b170b3 [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
Florin Corasadc74d72018-12-02 13:36:00 -0800124 svm_fifo_segment_main_t segment_main;
Florin Corase04c2992017-03-01 08:17:34 -0800125
Florin Coras3cbc04b2017-10-02 00:18:51 -0700126 u8 *connect_test_data;
Florin Corasf8f516a2018-02-08 15:10:09 -0800127
128 uword *segments_table;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700129 u8 do_echo;
Florin Coras8e43d042018-05-04 15:46:57 -0700130 u8 have_return;
Florin Corasa44d6b12018-10-03 14:29:10 -0700131 u64 total_to_send;
Florin Coras8e43d042018-05-04 15:46:57 -0700132 u64 bytes_to_send;
Florin Corasa44d6b12018-10-03 14:29:10 -0700133 u64 bytes_sent;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700134} udp_echo_main_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500135
Florin Coras7fb0fe12018-04-09 09:24:52 -0700136udp_echo_main_t udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500137
138static void
139stop_signal (int signum)
140{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700141 udp_echo_main_t *um = &udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500142
143 um->time_to_stop = 1;
144}
145
146static void
147stats_signal (int signum)
148{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700149 udp_echo_main_t *um = &udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500150 um->time_to_print_stats = 1;
151}
152
153static clib_error_t *
154setup_signal_handlers (void)
155{
156 signal (SIGINT, stats_signal);
157 signal (SIGQUIT, stop_signal);
158 signal (SIGTERM, stop_signal);
159
160 return 0;
161}
162
Florin Coras7fb0fe12018-04-09 09:24:52 -0700163uword
164unformat_ip4_address (unformat_input_t * input, va_list * args)
165{
166 u8 *result = va_arg (*args, u8 *);
167 unsigned a[4];
168
169 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
170 return 0;
171
172 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
173 return 0;
174
175 result[0] = a[0];
176 result[1] = a[1];
177 result[2] = a[2];
178 result[3] = a[3];
179
180 return 1;
181}
182
183uword
184unformat_ip6_address (unformat_input_t * input, va_list * args)
185{
186 ip6_address_t *result = va_arg (*args, ip6_address_t *);
187 u16 hex_quads[8];
188 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
189 uword c, n_colon, double_colon_index;
190
191 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
192 double_colon_index = ARRAY_LEN (hex_quads);
193 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
194 {
195 hex_digit = 16;
196 if (c >= '0' && c <= '9')
197 hex_digit = c - '0';
198 else if (c >= 'a' && c <= 'f')
199 hex_digit = c + 10 - 'a';
200 else if (c >= 'A' && c <= 'F')
201 hex_digit = c + 10 - 'A';
202 else if (c == ':' && n_colon < 2)
203 n_colon++;
204 else
205 {
206 unformat_put_input (input);
207 break;
208 }
209
210 /* Too many hex quads. */
211 if (n_hex_quads >= ARRAY_LEN (hex_quads))
212 return 0;
213
214 if (hex_digit < 16)
215 {
216 hex_quad = (hex_quad << 4) | hex_digit;
217
218 /* Hex quad must fit in 16 bits. */
219 if (n_hex_digits >= 4)
220 return 0;
221
222 n_colon = 0;
223 n_hex_digits++;
224 }
225
226 /* Save position of :: */
227 if (n_colon == 2)
228 {
229 /* More than one :: ? */
230 if (double_colon_index < ARRAY_LEN (hex_quads))
231 return 0;
232 double_colon_index = n_hex_quads;
233 }
234
235 if (n_colon > 0 && n_hex_digits > 0)
236 {
237 hex_quads[n_hex_quads++] = hex_quad;
238 hex_quad = 0;
239 n_hex_digits = 0;
240 }
241 }
242
243 if (n_hex_digits > 0)
244 hex_quads[n_hex_quads++] = hex_quad;
245
246 {
247 word i;
248
249 /* Expand :: to appropriate number of zero hex quads. */
250 if (double_colon_index < ARRAY_LEN (hex_quads))
251 {
252 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
253
254 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
255 hex_quads[n_zero + i] = hex_quads[i];
256
257 for (i = 0; i < n_zero; i++)
258 hex_quads[double_colon_index + i] = 0;
259
260 n_hex_quads = ARRAY_LEN (hex_quads);
261 }
262
263 /* Too few hex quads given. */
264 if (n_hex_quads < ARRAY_LEN (hex_quads))
265 return 0;
266
267 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
268 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
269
270 return 1;
271 }
272}
273
274uword
275unformat_uri (unformat_input_t * input, va_list * args)
276{
Florin Coras5665ced2018-10-25 18:03:45 -0700277 session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700278 u32 port;
279 char *tmp;
280
281 if (unformat (input, "%s://%U/%d", &tmp, unformat_ip4_address, &sep->ip.ip4,
282 &port))
283 {
284 sep->port = clib_host_to_net_u16 (port);
285 sep->is_ip4 = 1;
286 return 1;
287 }
288 else if (unformat (input, "%s://%U/%d", &tmp, unformat_ip6_address,
289 &sep->ip.ip6, &port))
290 {
291 sep->port = clib_host_to_net_u16 (port);
292 sep->is_ip4 = 0;
293 return 1;
294 }
295 return 0;
296}
297
Florin Coras52207f12018-07-12 14:48:06 -0700298static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700299application_send_attach (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700300{
301 vl_api_application_attach_t *bmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700302 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400303 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700304
305 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
306 bmp->client_index = utm->my_client_index;
307 bmp->context = ntohl (0xfeedface);
Florin Corasf8f516a2018-02-08 15:10:09 -0800308 bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ADD_SEGMENT;
309 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
310 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700311 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700312 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = utm->fifo_size;
313 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = utm->fifo_size;
Florin Corasff6e7692017-12-11 04:59:01 -0800314 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
315 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
316 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 16768;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700317 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
318}
319
320void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700321application_detach (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700322{
323 vl_api_application_detach_t *bmp;
324 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400325 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700326
327 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
328 bmp->client_index = utm->my_client_index;
329 bmp->context = ntohl (0xfeedface);
330 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
331}
332
333static void
334vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
335 mp)
336{
Florin Coras84a30ef2018-01-24 10:49:23 -0800337 svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -0800338 udp_echo_main_t *utm = &udp_echo_main;
339 svm_fifo_segment_main_t *sm = &utm->segment_main;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700340 int rv;
341
342 if (mp->retval)
343 {
344 clib_warning ("attach failed: %d", mp->retval);
345 utm->state = STATE_FAILED;
346 return;
347 }
348
349 if (mp->segment_name_length == 0)
350 {
351 clib_warning ("segment_name_length zero");
352 return;
353 }
354
355 a->segment_name = (char *) mp->segment_name;
356 a->segment_size = mp->segment_size;
357
358 ASSERT (mp->app_event_queue_address);
359
360 /* Attach to the segment vpp created */
Florin Corasadc74d72018-12-02 13:36:00 -0800361 rv = svm_fifo_segment_attach (sm, a);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700362 if (rv)
363 {
364 clib_warning ("svm_fifo_segment_attach ('%s') failed",
365 mp->segment_name);
366 return;
367 }
368
Florin Corasa44d6b12018-10-03 14:29:10 -0700369 utm->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
370 svm_msg_q_t *);
Florin Coras52207f12018-07-12 14:48:06 -0700371 utm->state = STATE_ATTACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700372}
373
374static void
375vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
376 mp)
377{
378 if (mp->retval)
379 clib_warning ("detach returned with err: %d", mp->retval);
Florin Coras52207f12018-07-12 14:48:06 -0700380 udp_echo_main.state = STATE_DETACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700381}
382
Dave Barach68b0fb02017-02-28 15:15:56 -0500383u8 *
384format_api_error (u8 * s, va_list * args)
385{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700386 udp_echo_main_t *utm = va_arg (*args, udp_echo_main_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500387 i32 error = va_arg (*args, u32);
388 uword *p;
389
390 p = hash_get (utm->error_string_by_error_number, -error);
391
392 if (p)
393 s = format (s, "%s", p[0]);
394 else
395 s = format (s, "%d", error);
396 return s;
397}
398
399int
Florin Coras7fb0fe12018-04-09 09:24:52 -0700400wait_for_state_change (udp_echo_main_t * utm, connection_state_t state)
Dave Barach68b0fb02017-02-28 15:15:56 -0500401{
Florin Corase04c2992017-03-01 08:17:34 -0800402#if CLIB_DEBUG > 0
403#define TIMEOUT 600.0
404#else
405#define TIMEOUT 600.0
406#endif
407
408 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500409
410 while (clib_time_now (&utm->clib_time) < timeout)
411 {
412 if (utm->state == state)
413 return 0;
Florin Corasa44d6b12018-10-03 14:29:10 -0700414 if (utm->state == STATE_FAILED)
415 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500416 }
417 return -1;
418}
419
Florin Corase04c2992017-03-01 08:17:34 -0800420u64 server_bytes_received, server_bytes_sent;
421
422static void *
423cut_through_thread_fn (void *arg)
424{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700425 app_session_t *s;
Florin Corase04c2992017-03-01 08:17:34 -0800426 svm_fifo_t *rx_fifo;
427 svm_fifo_t *tx_fifo;
428 u8 *my_copy_buffer = 0;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700429 udp_echo_main_t *utm = &udp_echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800430 i32 actual_transfer;
Florin Corasa44d6b12018-10-03 14:29:10 -0700431 int rv, do_dequeue = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800432 u32 buffer_offset;
433
434 while (utm->cut_through_session_index == ~0)
435 ;
436
437 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
438
Florin Coras7fb0fe12018-04-09 09:24:52 -0700439 rx_fifo = s->rx_fifo;
440 tx_fifo = s->tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -0800441
442 vec_validate (my_copy_buffer, 64 * 1024 - 1);
443
Florin Corasa44d6b12018-10-03 14:29:10 -0700444 while (1)
Florin Corase04c2992017-03-01 08:17:34 -0800445 {
Florin Corase04c2992017-03-01 08:17:34 -0800446 do
447 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700448 /* We read from the tx fifo and write to the rx fifo */
449 if (utm->have_return || do_dequeue)
450 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo,
451 vec_len
452 (my_copy_buffer),
453 my_copy_buffer);
454 else
455 {
456 /* We don't do anything with the data, drop it */
Sirshak Das28aa5392019-02-05 01:33:33 -0600457 actual_transfer = svm_fifo_max_dequeue_cons (rx_fifo);
Florin Corasa44d6b12018-10-03 14:29:10 -0700458 svm_fifo_dequeue_drop (rx_fifo, actual_transfer);
459 }
Florin Corase04c2992017-03-01 08:17:34 -0800460 }
461 while (actual_transfer <= 0);
462
463 server_bytes_received += actual_transfer;
464
Florin Corasa44d6b12018-10-03 14:29:10 -0700465 if (utm->have_return)
Florin Corase04c2992017-03-01 08:17:34 -0800466 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700467 buffer_offset = 0;
468 while (actual_transfer > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800469 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700470 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer,
471 my_copy_buffer + buffer_offset);
472 if (rv > 0)
473 {
474 actual_transfer -= rv;
475 buffer_offset += rv;
476 server_bytes_sent += rv;
477 }
Florin Corase04c2992017-03-01 08:17:34 -0800478
Florin Corasa44d6b12018-10-03 14:29:10 -0700479 }
Florin Corase04c2992017-03-01 08:17:34 -0800480 }
481 if (PREDICT_FALSE (utm->time_to_stop))
482 break;
483 }
484
485 pthread_exit (0);
486}
487
488static void
Florin Coras52207f12018-07-12 14:48:06 -0700489session_accepted_handler (session_accepted_msg_t * mp)
490{
491 app_session_evt_t _app_evt, *app_evt = &_app_evt;
492 udp_echo_main_t *utm = &udp_echo_main;
493 session_accepted_reply_msg_t *rmp;
494 svm_fifo_t *rx_fifo, *tx_fifo;
495 app_session_t *session;
496 static f64 start_time;
497 u32 session_index;
498 int rv = 0;
499
500 if (start_time == 0.0)
501 start_time = clib_time_now (&utm->clib_time);
502
503 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
504 svm_msg_q_t *);
505 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
506 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
507
508 pool_get (utm->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -0400509 clib_memset (session, 0, sizeof (*session));
Florin Coras52207f12018-07-12 14:48:06 -0700510 session_index = session - utm->sessions;
Florin Corasa44d6b12018-10-03 14:29:10 -0700511 session->session_index = session_index;
Florin Coras52207f12018-07-12 14:48:06 -0700512
Florin Coras653e43f2019-03-04 10:56:23 -0800513 rx_fifo->client_session_index = session_index;
514 tx_fifo->client_session_index = session_index;
515 session->rx_fifo = rx_fifo;
516 session->tx_fifo = tx_fifo;
517 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
518 sizeof (ip46_address_t));
519 session->transport.is_ip4 = mp->is_ip4;
520 session->transport.rmt_port = mp->port;
Florin Coras52207f12018-07-12 14:48:06 -0700521
522 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
523 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
524 {
525 f64 now = clib_time_now (&utm->clib_time);
526 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
527 pool_elts (utm->sessions), now - start_time,
528 (f64) pool_elts (utm->sessions) / (now - start_time));
529 }
530
Florin Corasa44d6b12018-10-03 14:29:10 -0700531 app_alloc_ctrl_evt_to_vpp (utm->vpp_event_queue, app_evt,
Florin Coras52207f12018-07-12 14:48:06 -0700532 SESSION_CTRL_EVT_ACCEPTED_REPLY);
533 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
534 rmp->handle = mp->handle;
535 rmp->context = mp->context;
536 rmp->retval = rv;
Florin Corasa44d6b12018-10-03 14:29:10 -0700537 app_send_ctrl_evt_to_vpp (utm->vpp_event_queue, app_evt);
Florin Coras52207f12018-07-12 14:48:06 -0700538
539 CLIB_MEMORY_BARRIER ();
540 utm->state = STATE_READY;
541}
542
543static void
544session_disconnected_handler (session_disconnected_msg_t * mp)
545{
546 app_session_evt_t _app_evt, *app_evt = &_app_evt;
547 udp_echo_main_t *utm = &udp_echo_main;
548 session_disconnected_reply_msg_t *rmp;
549 app_session_t *session;
550 uword *p;
551 int rv = 0;
552
553 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
554
555 if (p)
556 {
557 session = pool_elt_at_index (utm->sessions, p[0]);
558 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Florin Corasa44d6b12018-10-03 14:29:10 -0700559 clib_warning ("disconnecting %u", session->session_index);
Florin Coras52207f12018-07-12 14:48:06 -0700560 pool_put (utm->sessions, session);
561 }
562 else
563 {
564 clib_warning ("couldn't find session key %llx", mp->handle);
565 return;
566 }
567
568 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
569 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
570 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
571 rmp->retval = rv;
572 rmp->handle = mp->handle;
573 rmp->context = mp->context;
574 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
575}
576
577static void
578session_connected_handler (session_connected_msg_t * mp)
579{
580 udp_echo_main_t *utm = &udp_echo_main;
581 unformat_input_t _input, *input = &_input;
Florin Coras5665ced2018-10-25 18:03:45 -0700582 session_endpoint_cfg_t _sep, *sep = &_sep;
Florin Coras52207f12018-07-12 14:48:06 -0700583 app_session_t *session;
584
585 ASSERT (utm->i_am_server == 0);
586
587 if (mp->retval)
588 {
589 clib_warning ("failed connect");
590 return;
591 }
592
593 ASSERT (mp->server_rx_fifo && mp->server_tx_fifo);
594
595 pool_get (utm->sessions, session);
Florin Corasa44d6b12018-10-03 14:29:10 -0700596 session->session_index = session - utm->sessions;
Florin Coras52207f12018-07-12 14:48:06 -0700597 session->rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
598 session->tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasa44d6b12018-10-03 14:29:10 -0700599
Florin Coras52207f12018-07-12 14:48:06 -0700600 /* Cut-through case */
Florin Coras653e43f2019-03-04 10:56:23 -0800601 if (mp->ct_rx_fifo)
Florin Coras52207f12018-07-12 14:48:06 -0700602 {
603 clib_warning ("cut-through session");
Florin Coras653e43f2019-03-04 10:56:23 -0800604 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
Florin Corasa44d6b12018-10-03 14:29:10 -0700605 svm_msg_q_t *);
Florin Corasa44d6b12018-10-03 14:29:10 -0700606 utm->cut_through_session_index = session->session_index;
607 session->is_dgram = 0;
608 sleep (1);
609 session->rx_fifo->client_session_index = session->session_index;
610 session->tx_fifo->client_session_index = session->session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800611 /* TODO use ct fifos */
Florin Coras52207f12018-07-12 14:48:06 -0700612 }
613 else
614 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700615 utm->connected_session = session->session_index;
Florin Coras52207f12018-07-12 14:48:06 -0700616 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
617 svm_msg_q_t *);
618
Florin Corasa44d6b12018-10-03 14:29:10 -0700619 session->rx_fifo->client_session_index = session->session_index;
620 session->tx_fifo->client_session_index = session->session_index;
Dave Barach178cf492018-11-13 16:34:13 -0500621 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
622 sizeof (ip46_address_t));
Florin Coras52207f12018-07-12 14:48:06 -0700623 session->transport.is_ip4 = mp->is_ip4;
624 session->transport.lcl_port = mp->lcl_port;
625
626 unformat_init_vector (input, utm->connect_uri);
627 if (!unformat (input, "%U", unformat_uri, sep))
628 {
629 clib_warning ("can't figure out remote ip and port");
630 utm->state = STATE_FAILED;
631 unformat_free (input);
632 return;
633 }
634 unformat_free (input);
Dave Barach178cf492018-11-13 16:34:13 -0500635 clib_memcpy_fast (&session->transport.rmt_ip, &sep->ip,
636 sizeof (ip46_address_t));
Florin Coras52207f12018-07-12 14:48:06 -0700637 session->transport.rmt_port = sep->port;
638 session->is_dgram = !utm->is_connected;
639 }
640 utm->state = STATE_READY;
641}
642
643static void
Florin Corasa44d6b12018-10-03 14:29:10 -0700644session_bound_handler (session_bound_msg_t * mp)
645{
646 udp_echo_main_t *utm = &udp_echo_main;
647 svm_fifo_t *rx_fifo, *tx_fifo;
648 app_session_t *session;
649 u32 session_index;
650
651 if (mp->retval)
652 {
653 clib_warning ("bind failed: %d", mp->retval);
654 utm->state = STATE_FAILED;
655 return;
656 }
657
658 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
659 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
660
661 pool_get (utm->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -0400662 clib_memset (session, 0, sizeof (*session));
Florin Corasa44d6b12018-10-03 14:29:10 -0700663 session_index = session - utm->sessions;
664
665 rx_fifo->client_session_index = session_index;
666 tx_fifo->client_session_index = session_index;
667 session->rx_fifo = rx_fifo;
668 session->tx_fifo = tx_fifo;
Dave Barach178cf492018-11-13 16:34:13 -0500669 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
670 sizeof (ip46_address_t));
Florin Corasa44d6b12018-10-03 14:29:10 -0700671 session->transport.is_ip4 = mp->lcl_is_ip4;
672 session->transport.lcl_port = mp->lcl_port;
673 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
674
675 utm->state = utm->is_connected ? STATE_BOUND : STATE_READY;
676}
677
678static void
Florin Coras52207f12018-07-12 14:48:06 -0700679handle_mq_event (session_event_t * e)
680{
681 switch (e->event_type)
682 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700683 case SESSION_CTRL_EVT_BOUND:
684 session_bound_handler ((session_bound_msg_t *) e->data);
685 break;
Florin Coras52207f12018-07-12 14:48:06 -0700686 case SESSION_CTRL_EVT_ACCEPTED:
687 session_accepted_handler ((session_accepted_msg_t *) e->data);
688 break;
689 case SESSION_CTRL_EVT_CONNECTED:
690 session_connected_handler ((session_connected_msg_t *) e->data);
691 break;
692 case SESSION_CTRL_EVT_DISCONNECTED:
693 session_disconnected_handler ((session_disconnected_msg_t *) e->data);
694 break;
695 default:
696 clib_warning ("unhandled %u", e->event_type);
697 }
698}
699
700static void
701udp_client_send_connect (udp_echo_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800702{
703 vl_api_connect_uri_t *cmp;
Florin Corase04c2992017-03-01 08:17:34 -0800704 cmp = vl_msg_api_alloc (sizeof (*cmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400705 clib_memset (cmp, 0, sizeof (*cmp));
Florin Corase04c2992017-03-01 08:17:34 -0800706
707 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
708 cmp->client_index = utm->my_client_index;
709 cmp->context = ntohl (0xfeedface);
710 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
711 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700712}
Florin Corase04c2992017-03-01 08:17:34 -0800713
Florin Coras6cf30ad2017-04-04 23:08:23 -0700714static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700715send_test_chunk (udp_echo_main_t * utm, app_session_t * s, u32 bytes)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700716{
Florin Corasa44d6b12018-10-03 14:29:10 -0700717 u64 test_buf_len, bytes_this_chunk, test_buf_offset;
718
Florin Coras3cbc04b2017-10-02 00:18:51 -0700719 u8 *test_data = utm->connect_test_data;
Florin Coras8e43d042018-05-04 15:46:57 -0700720 u32 bytes_to_snd, enq_space, min_chunk;
Florin Corasa44d6b12018-10-03 14:29:10 -0700721 int written;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700722
Florin Corasa44d6b12018-10-03 14:29:10 -0700723 test_buf_len = vec_len (test_data);
724 test_buf_offset = utm->bytes_sent % test_buf_len;
725 bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
726 utm->bytes_to_send);
Sirshak Das28aa5392019-02-05 01:33:33 -0600727 enq_space = svm_fifo_max_enqueue_prod (s->tx_fifo);
Florin Corasa44d6b12018-10-03 14:29:10 -0700728 bytes_this_chunk = clib_min (bytes_this_chunk, enq_space);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700729
Florin Coras653e43f2019-03-04 10:56:23 -0800730 written = app_send (s, test_data + test_buf_offset, bytes_this_chunk,
731 SVM_Q_WAIT);
Florin Corasa44d6b12018-10-03 14:29:10 -0700732 if (written > 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700733 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700734 utm->bytes_to_send -= written;
735 utm->bytes_sent += written;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700736 }
737}
738
739static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700740recv_test_chunk (udp_echo_main_t * utm, app_session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700741{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700742 app_recv (s, utm->rx_buf, vec_len (utm->rx_buf));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700743}
744
745void
Florin Corasa44d6b12018-10-03 14:29:10 -0700746client_send_data (udp_echo_main_t * utm, u32 session_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700747{
Florin Coras8e43d042018-05-04 15:46:57 -0700748 f64 start_time, end_time, delta;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700749 app_session_t *session;
Florin Coras8e43d042018-05-04 15:46:57 -0700750 char *transfer_type;
Florin Coras8e43d042018-05-04 15:46:57 -0700751 u8 *test_data;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700752 int i;
753
Florin Corasa44d6b12018-10-03 14:29:10 -0700754 vec_validate_aligned (utm->connect_test_data, 1024 * 1024 - 1,
755 CLIB_CACHE_LINE_BYTES);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700756 for (i = 0; i < vec_len (utm->connect_test_data); i++)
757 utm->connect_test_data[i] = i & 0xff;
758
759 test_data = utm->connect_test_data;
Florin Corasa44d6b12018-10-03 14:29:10 -0700760 session = pool_elt_at_index (utm->sessions, session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700761 ASSERT (vec_len (test_data) > 0);
762
Florin Corasa44d6b12018-10-03 14:29:10 -0700763 utm->total_to_send = utm->bytes_to_send;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700764 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
Florin Coras8e43d042018-05-04 15:46:57 -0700765 start_time = clib_time_now (&utm->clib_time);
Florin Corasa44d6b12018-10-03 14:29:10 -0700766 while (!utm->time_to_stop && utm->bytes_to_send)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700767 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700768 send_test_chunk (utm, session, 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700769 if (utm->have_return)
770 recv_test_chunk (utm, session);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700771 if (utm->time_to_stop)
772 break;
773 }
774
Florin Coras8e43d042018-05-04 15:46:57 -0700775 if (utm->have_return)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700776 {
Florin Coras8e43d042018-05-04 15:46:57 -0700777 f64 timeout = clib_time_now (&utm->clib_time) + 5;
778 while (clib_time_now (&utm->clib_time) < timeout)
779 recv_test_chunk (utm, session);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700780 }
781
Florin Coras8e43d042018-05-04 15:46:57 -0700782 end_time = clib_time_now (&utm->clib_time);
783 delta = end_time - start_time;
784 transfer_type = utm->have_return ? "full-duplex" : "half-duplex";
785 clib_warning ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
Florin Corasa44d6b12018-10-03 14:29:10 -0700786 utm->total_to_send, utm->total_to_send / (1ULL << 20),
787 utm->total_to_send / (1ULL << 30), delta);
788 clib_warning ("%.2f bytes/second %s", ((f64) utm->total_to_send) / (delta),
Florin Coras8e43d042018-05-04 15:46:57 -0700789 transfer_type);
790 clib_warning ("%.4f gbit/second %s",
Florin Corasa44d6b12018-10-03 14:29:10 -0700791 (((f64) utm->total_to_send * 8.0) / delta / 1e9),
Florin Coras8e43d042018-05-04 15:46:57 -0700792 transfer_type);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700793}
794
Florin Coras52207f12018-07-12 14:48:06 -0700795static int
796application_attach (udp_echo_main_t * utm)
797{
798 application_send_attach (utm);
799 if (wait_for_state_change (utm, STATE_ATTACHED))
800 {
801 clib_warning ("timeout waiting for STATE_ATTACHED");
802 return -1;
803 }
804 return 0;
805}
806
Florin Coras3cbc04b2017-10-02 00:18:51 -0700807static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700808client_test (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700809{
Florin Coras52207f12018-07-12 14:48:06 -0700810 f64 start_time, timeout = 100.0;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700811 app_session_t *session;
Florin Coras52207f12018-07-12 14:48:06 -0700812 svm_msg_q_msg_t msg;
813 session_event_t *e;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700814
Florin Coras52207f12018-07-12 14:48:06 -0700815 if (application_attach (utm))
816 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700817
Florin Coras52207f12018-07-12 14:48:06 -0700818 udp_client_send_connect (utm);
819
820 start_time = clib_time_now (&utm->clib_time);
Florin Corasa44d6b12018-10-03 14:29:10 -0700821 while (pool_elts (utm->sessions) != 1 && utm->state != STATE_FAILED)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700822 {
Florin Coras52207f12018-07-12 14:48:06 -0700823 svm_msg_q_sub (utm->our_event_queue, &msg, SVM_Q_WAIT, 0);
824 e = svm_msg_q_msg_data (utm->our_event_queue, &msg);
825 handle_mq_event (e);
826 svm_msg_q_free_msg (utm->our_event_queue, &msg);
Florin Corasa44d6b12018-10-03 14:29:10 -0700827
828 if (clib_time_now (&utm->clib_time) - start_time >= timeout)
829 break;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700830 }
831
Florin Coras3cbc04b2017-10-02 00:18:51 -0700832 if (utm->cut_through_session_index != ~0)
Florin Corasa44d6b12018-10-03 14:29:10 -0700833 client_send_data (utm, utm->cut_through_session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700834 else
Florin Corasa44d6b12018-10-03 14:29:10 -0700835 client_send_data (utm, utm->connected_session);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700836
Florin Coras6cf30ad2017-04-04 23:08:23 -0700837 application_detach (utm);
Florin Coras52207f12018-07-12 14:48:06 -0700838 wait_for_state_change (utm, STATE_DETACHED);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700839}
840
841static void
Florin Corase04c2992017-03-01 08:17:34 -0800842vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
843{
844 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -0800845 udp_echo_main_t *utm = &udp_echo_main;
846 svm_fifo_segment_main_t *sm = &utm->segment_main;
Florin Corasf8f516a2018-02-08 15:10:09 -0800847 svm_fifo_segment_private_t *seg;
Florin Corase04c2992017-03-01 08:17:34 -0800848 int rv;
849
Dave Barachb7b92992018-10-17 10:38:51 -0400850 clib_memset (a, 0, sizeof (*a));
Florin Corase04c2992017-03-01 08:17:34 -0800851 a->segment_name = (char *) mp->segment_name;
852 a->segment_size = mp->segment_size;
853 /* Attach to the segment vpp created */
Florin Corasadc74d72018-12-02 13:36:00 -0800854 rv = svm_fifo_segment_attach (sm, a);
Florin Corase04c2992017-03-01 08:17:34 -0800855 if (rv)
856 {
857 clib_warning ("svm_fifo_segment_attach ('%s') failed",
858 mp->segment_name);
859 return;
860 }
Florin Corasadc74d72018-12-02 13:36:00 -0800861 seg = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
Florin Corasf8f516a2018-02-08 15:10:09 -0800862 clib_warning ("Mapped new segment '%s' size %d", seg->ssvm.name,
863 seg->ssvm.ssvm_size);
Florin Corasfa76a762018-11-29 12:40:10 -0800864 hash_set (utm->segments_table, clib_net_to_host_u64 (mp->segment_handle),
865 a->new_segment_indices[0]);
Florin Corasf8f516a2018-02-08 15:10:09 -0800866}
867
868static void
869vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
870{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700871 udp_echo_main_t *utm = &udp_echo_main;
Florin Corasadc74d72018-12-02 13:36:00 -0800872 svm_fifo_segment_main_t *sm = &utm->segment_main;
Florin Corasf8f516a2018-02-08 15:10:09 -0800873 svm_fifo_segment_private_t *seg;
David Johnsond9818dd2018-12-14 14:53:41 -0500874 uword *seg_indexp;
875 u64 segment_handle;
Florin Corasf8f516a2018-02-08 15:10:09 -0800876
Florin Corasfa76a762018-11-29 12:40:10 -0800877 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
878 seg_indexp = hash_get (utm->segments_table, segment_handle);
Florin Corasf8f516a2018-02-08 15:10:09 -0800879 if (!seg_indexp)
880 {
Florin Corasfa76a762018-11-29 12:40:10 -0800881 clib_warning ("segment not mapped: %s", segment_handle);
Florin Corasf8f516a2018-02-08 15:10:09 -0800882 return;
883 }
Florin Corasfa76a762018-11-29 12:40:10 -0800884 hash_unset (utm->segments_table, segment_handle);
Florin Corasadc74d72018-12-02 13:36:00 -0800885 seg = svm_fifo_segment_get_segment (sm, (u32) seg_indexp[0]);
886 svm_fifo_segment_delete (sm, seg);
Florin Corasfa76a762018-11-29 12:40:10 -0800887 clib_warning ("Unmapped segment '%s'", segment_handle);
Florin Corase04c2992017-03-01 08:17:34 -0800888}
889
Florin Corase04c2992017-03-01 08:17:34 -0800890static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500891vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
892{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700893 udp_echo_main_t *utm = &udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500894
895 if (mp->retval != 0)
896 clib_warning ("returned %d", ntohl (mp->retval));
897
898 utm->state = STATE_START;
899}
900
Florin Corasa44d6b12018-10-03 14:29:10 -0700901static void
902 vl_api_app_cut_through_registration_add_t_handler
903 (vl_api_app_cut_through_registration_add_t * mp)
904{
905
906}
907
908#define foreach_tcp_echo_msg \
Florin Corasa44d6b12018-10-03 14:29:10 -0700909_(UNBIND_URI_REPLY, unbind_uri_reply) \
910_(MAP_ANOTHER_SEGMENT, map_another_segment) \
911_(UNMAP_SEGMENT, unmap_segment) \
912_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
913_(APPLICATION_DETACH_REPLY, application_detach_reply) \
914_(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
Dave Barach68b0fb02017-02-28 15:15:56 -0500915
916void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700917tcp_echo_api_hookup (udp_echo_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500918{
919#define _(N,n) \
920 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Corase04c2992017-03-01 08:17:34 -0800921 vl_api_##n##_t_handler, \
Dave Barach68b0fb02017-02-28 15:15:56 -0500922 vl_noop_handler, \
923 vl_api_##n##_t_endian, \
924 vl_api_##n##_t_print, \
925 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -0800926 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500927#undef _
928
929}
930
Dave Barach68b0fb02017-02-28 15:15:56 -0500931int
932connect_to_vpp (char *name)
933{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700934 udp_echo_main_t *utm = &udp_echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500935 api_main_t *am = &api_main;
936
937 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
938 return -1;
939
940 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
941 utm->my_client_index = am->my_client_index;
942
943 return 0;
944}
945
946void
947vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
948{
949 clib_warning ("BUG");
950}
951
952static void
Florin Coras7fb0fe12018-04-09 09:24:52 -0700953init_error_string_table (udp_echo_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500954{
955 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
956
957#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
958 foreach_vnet_api_error;
959#undef _
960
961 hash_set (utm->error_string_by_error_number, 99, "Misc");
962}
963
964void
Florin Corasa44d6b12018-10-03 14:29:10 -0700965server_handle_fifo_event_rx (udp_echo_main_t * utm, u32 session_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500966{
Florin Corasa44d6b12018-10-03 14:29:10 -0700967 svm_fifo_t *rx_fifo, *tx_fifo;
968 int n_read;
969 app_session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500970 int rv;
Florin Corasa44d6b12018-10-03 14:29:10 -0700971 u32 max_dequeue, offset, max_transfer, rx_buf_len;
Dave Barach68b0fb02017-02-28 15:15:56 -0500972
Florin Corasa44d6b12018-10-03 14:29:10 -0700973 session = pool_elt_at_index (utm->sessions, session_index);
974 rx_buf_len = vec_len (utm->rx_buf);
975 rx_fifo = session->rx_fifo;
976 tx_fifo = session->tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -0500977
Sirshak Das28aa5392019-02-05 01:33:33 -0600978
979 max_dequeue = svm_fifo_max_dequeue_cons (rx_fifo);
Florin Corasa44d6b12018-10-03 14:29:10 -0700980 /* Allow enqueuing of a new event */
981 svm_fifo_unset_event (rx_fifo);
982
983 if (PREDICT_FALSE (!max_dequeue))
984 return;
985
986 /* Read the max_dequeue */
987 do
Dave Barach68b0fb02017-02-28 15:15:56 -0500988 {
Florin Corasa44d6b12018-10-03 14:29:10 -0700989 max_transfer = clib_min (rx_buf_len, max_dequeue);
990 if (session->is_dgram)
991 n_read = app_recv_dgram_raw (rx_fifo, utm->rx_buf, max_transfer,
992 &session->transport, 0, 0);
993 else
994 n_read = app_recv_stream_raw (rx_fifo, utm->rx_buf, max_transfer, 0,
995 0);
996
997 if (n_read > 0)
998 max_dequeue -= n_read;
999
1000 /* Reflect if a non-drop session */
1001 if (utm->have_return && n_read > 0)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001002 {
Florin Corasa44d6b12018-10-03 14:29:10 -07001003 offset = 0;
1004 do
1005 {
Florin Coras653e43f2019-03-04 10:56:23 -08001006 rv = app_send (session, &utm->rx_buf[offset], n_read,
1007 SVM_Q_WAIT);
Florin Corasa44d6b12018-10-03 14:29:10 -07001008 if (rv > 0)
1009 {
1010 n_read -= rv;
1011 offset += rv;
1012 }
1013 }
1014 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
1015
1016 /* If event wasn't set, add one */
1017 if (svm_fifo_set_event (tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001018 app_send_io_evt_to_vpp (session->vpp_evt_q,
1019 tx_fifo->master_session_index,
Florin Coras653e43f2019-03-04 10:56:23 -08001020 SESSION_IO_EVT_TX, SVM_Q_WAIT);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001021 }
Florin Coras6792ec02017-03-13 03:49:51 -07001022 }
Florin Corasa44d6b12018-10-03 14:29:10 -07001023 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
Dave Barach68b0fb02017-02-28 15:15:56 -05001024}
1025
Florin Corasa44d6b12018-10-03 14:29:10 -07001026static void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001027server_handle_event_queue (udp_echo_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001028{
Florin Coras52207f12018-07-12 14:48:06 -07001029 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001030 svm_msg_q_msg_t msg;
Florin Corasa44d6b12018-10-03 14:29:10 -07001031 svm_msg_q_t *mq = utm->our_event_queue;
1032 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001033
Florin Coras3cbc04b2017-10-02 00:18:51 -07001034 while (utm->state != STATE_READY)
1035 sleep (5);
1036
Dave Barach68b0fb02017-02-28 15:15:56 -05001037 while (1)
1038 {
Florin Corasa44d6b12018-10-03 14:29:10 -07001039 if (svm_msg_q_sub (mq, &msg, SVM_Q_WAIT, 0))
Florin Coras3c2fed52018-07-04 04:15:05 -07001040 {
1041 clib_warning ("svm msg q returned");
1042 continue;
1043 }
Florin Corasa44d6b12018-10-03 14:29:10 -07001044 e = svm_msg_q_msg_data (mq, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -05001045 switch (e->event_type)
1046 {
Florin Coras653e43f2019-03-04 10:56:23 -08001047 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08001048 server_handle_fifo_event_rx (utm, e->session_index);
Florin Corasa44d6b12018-10-03 14:29:10 -07001049 break;
Dave Barach68b0fb02017-02-28 15:15:56 -05001050
Dave Barach68b0fb02017-02-28 15:15:56 -05001051 default:
Florin Coras52207f12018-07-12 14:48:06 -07001052 handle_mq_event (e);
Dave Barach68b0fb02017-02-28 15:15:56 -05001053 break;
1054 }
Florin Corasa44d6b12018-10-03 14:29:10 -07001055 svm_msg_q_free_msg (mq, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -05001056 if (PREDICT_FALSE (utm->time_to_stop == 1))
Florin Coras3cbc04b2017-10-02 00:18:51 -07001057 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001058 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1059 {
1060 utm->time_to_print_stats = 0;
1061 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1062 }
1063 }
1064}
1065
Florin Coras6cf30ad2017-04-04 23:08:23 -07001066static void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001067server_unbind (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001068{
1069 vl_api_unbind_uri_t *ump;
1070
1071 ump = vl_msg_api_alloc (sizeof (*ump));
Dave Barachb7b92992018-10-17 10:38:51 -04001072 clib_memset (ump, 0, sizeof (*ump));
Florin Coras6cf30ad2017-04-04 23:08:23 -07001073
1074 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1075 ump->client_index = utm->my_client_index;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001076 memcpy (ump->uri, utm->listen_uri, vec_len (utm->listen_uri));
Florin Coras6cf30ad2017-04-04 23:08:23 -07001077 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1078}
1079
1080static void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001081server_bind (udp_echo_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001082{
1083 vl_api_bind_uri_t *bmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001084
1085 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001086 clib_memset (bmp, 0, sizeof (*bmp));
Dave Barach68b0fb02017-02-28 15:15:56 -05001087
1088 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1089 bmp->client_index = utm->my_client_index;
1090 bmp->context = ntohl (0xfeedface);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001091 memcpy (bmp->uri, utm->listen_uri, vec_len (utm->listen_uri));
Dave Barach68b0fb02017-02-28 15:15:56 -05001092 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001093}
1094
1095void
Florin Coras7fb0fe12018-04-09 09:24:52 -07001096udp_server_test (udp_echo_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001097{
Florin Coras7fb0fe12018-04-09 09:24:52 -07001098 u8 wait_for_state = utm->is_connected ? STATE_BOUND : STATE_READY;
Florin Corasa5464812017-04-19 13:00:05 -07001099 application_send_attach (utm);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001100
1101 /* Bind to uri */
Florin Coras3cbc04b2017-10-02 00:18:51 -07001102 server_bind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001103
Florin Coras7fb0fe12018-04-09 09:24:52 -07001104 if (wait_for_state_change (utm, wait_for_state))
Dave Barach68b0fb02017-02-28 15:15:56 -05001105 {
Florin Coras7fb0fe12018-04-09 09:24:52 -07001106 clib_warning ("timeout waiting for state change");
Dave Barach68b0fb02017-02-28 15:15:56 -05001107 return;
1108 }
1109
Florin Corase04c2992017-03-01 08:17:34 -08001110 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001111
Florin Coras6cf30ad2017-04-04 23:08:23 -07001112 /* Cleanup */
1113 server_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001114
1115 if (wait_for_state_change (utm, STATE_START))
1116 {
1117 clib_warning ("timeout waiting for STATE_START");
1118 return;
1119 }
1120
Florin Coras6cf30ad2017-04-04 23:08:23 -07001121 application_detach (utm);
1122
Dave Barach68b0fb02017-02-28 15:15:56 -05001123 fformat (stdout, "Test complete...\n");
1124}
1125
1126int
1127main (int argc, char **argv)
1128{
Florin Coras7fb0fe12018-04-09 09:24:52 -07001129 udp_echo_main_t *utm = &udp_echo_main;
Florin Corasadc74d72018-12-02 13:36:00 -08001130 svm_fifo_segment_main_t *sm = &utm->segment_main;
Florin Coras8e43d042018-05-04 15:46:57 -07001131 u8 *uri = (u8 *) "udp://6.0.1.1/1234";
Florin Corasb384b542018-01-15 01:08:33 -08001132 unformat_input_t _argv, *a = &_argv;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001133 int i_am_server = 1;
1134 app_session_t *session;
Florin Corasb384b542018-01-15 01:08:33 -08001135 u8 *chroot_prefix;
1136 char *app_name;
Florin Corasb384b542018-01-15 01:08:33 -08001137 u32 tmp;
1138 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001139
Dave Barach6a5adc32018-07-04 10:56:23 -04001140 clib_mem_init_thread_safe (0, 256 << 20);
1141
David Johnsond9818dd2018-12-14 14:53:41 -05001142 svm_fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001143
Florin Corasa44d6b12018-10-03 14:29:10 -07001144 vec_validate (utm->rx_buf, 128 << 10);
Dave Barach68b0fb02017-02-28 15:15:56 -05001145 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Corase04c2992017-03-01 08:17:34 -08001146 utm->my_pid = getpid ();
1147 utm->configured_segment_size = 1 << 20;
Florin Coras8e43d042018-05-04 15:46:57 -07001148 utm->have_return = 1;
1149 utm->bytes_to_send = 1024;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001150 utm->fifo_size = 128 << 10;
Florin Coras8e43d042018-05-04 15:46:57 -07001151 utm->cut_through_session_index = ~0;
1152 clib_time_init (&utm->clib_time);
1153
1154 init_error_string_table (utm);
1155 unformat_init_command_line (a, argv);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001156
Dave Barach68b0fb02017-02-28 15:15:56 -05001157 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1158 {
1159 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1160 {
1161 vl_set_memory_root_path ((char *) chroot_prefix);
1162 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001163 else if (unformat (a, "uri %s", &uri))
Dave Barach68b0fb02017-02-28 15:15:56 -05001164 ;
Florin Corase04c2992017-03-01 08:17:34 -08001165 else if (unformat (a, "segment-size %dM", &tmp))
1166 utm->configured_segment_size = tmp << 20;
1167 else if (unformat (a, "segment-size %dG", &tmp))
1168 utm->configured_segment_size = tmp << 30;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001169 else if (unformat (a, "server"))
1170 i_am_server = 1;
1171 else if (unformat (a, "client"))
1172 i_am_server = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001173 else if (unformat (a, "no-return"))
1174 utm->have_return = 0;
1175 else if (unformat (a, "mbytes %d", &tmp))
1176 utm->bytes_to_send = (u64) tmp << 20;
1177 else if (unformat (a, "fifo-size %d", &tmp))
1178 utm->fifo_size = tmp << 10;
Dave Barach68b0fb02017-02-28 15:15:56 -05001179 else
1180 {
Florin Coras7fb0fe12018-04-09 09:24:52 -07001181 fformat (stderr, "%s: usage [server|client]\n");
Dave Barach68b0fb02017-02-28 15:15:56 -05001182 exit (1);
1183 }
1184 }
1185
Florin Coras7fb0fe12018-04-09 09:24:52 -07001186 utm->i_am_server = i_am_server;
Dave Barach68b0fb02017-02-28 15:15:56 -05001187
1188 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001189 tcp_echo_api_hookup (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001190
Florin Coras7fb0fe12018-04-09 09:24:52 -07001191 if (i_am_server)
1192 {
1193 utm->listen_uri = format (0, "%s%c", uri, 0);
1194 utm->is_connected = (utm->listen_uri[4] == 'c');
1195 app_name = "udp_echo_server";
1196 }
1197 else
1198 {
1199 app_name = "udp_echo_client";
1200 utm->connect_uri = format (0, "%s%c", uri, 0);
1201 utm->is_connected = (utm->connect_uri[4] == 'c');
1202 }
Florin Corasb384b542018-01-15 01:08:33 -08001203 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001204 {
1205 svm_region_exit ();
1206 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1207 exit (1);
1208 }
1209
Florin Coras7fb0fe12018-04-09 09:24:52 -07001210 if (i_am_server == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001211 {
Florin Coras3cbc04b2017-10-02 00:18:51 -07001212 client_test (utm);
Florin Coras8e43d042018-05-04 15:46:57 -07001213 goto done;
Florin Corase04c2992017-03-01 08:17:34 -08001214 }
1215
Dave Barach68b0fb02017-02-28 15:15:56 -05001216 /* $$$$ hack preallocation */
1217 for (i = 0; i < 200000; i++)
1218 {
1219 pool_get (utm->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -04001220 clib_memset (session, 0, sizeof (*session));
Dave Barach68b0fb02017-02-28 15:15:56 -05001221 }
1222 for (i = 0; i < 200000; i++)
1223 pool_put_index (utm->sessions, i);
1224
Florin Coras6cf30ad2017-04-04 23:08:23 -07001225 udp_server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001226
Florin Coras8e43d042018-05-04 15:46:57 -07001227done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001228 vl_client_disconnect_from_vlib ();
1229 exit (0);
1230}
1231
1232#undef vl_api_version
1233#define vl_api_version(n,v) static u32 vpe_api_version = v;
Florin Corase04c2992017-03-01 08:17:34 -08001234#include <vpp/api/vpe.api.h>
Dave Barach68b0fb02017-02-28 15:15:56 -05001235#undef vl_api_version
1236
1237void
1238vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1239{
1240 /*
1241 * Send the main API signature in slot 0. This bit of code must
1242 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1243 */
1244 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1245}
1246
Florin Corase04c2992017-03-01 08:17:34 -08001247u32
1248vl (void *p)
1249{
1250 return vec_len (p);
1251}
1252
Dave Barach68b0fb02017-02-28 15:15:56 -05001253/*
1254 * fd.io coding-style-patch-verification: ON
1255 *
1256 * Local Variables:
1257 * eval: (c-set-style "gnu")
1258 * End:
1259 */