blob: 80aab183b0a70c8c8e0b659cdbe8f29c07260001 [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 <signal.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050018#include <svm/svm_fifo_segment.h>
19#include <vlibmemory/api.h>
20#include <vpp/api/vpe_msg_enum.h>
Florin Corase04c2992017-03-01 08:17:34 -080021#include <vnet/session/application_interface.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
Florin Corase04c2992017-03-01 08:17:34 -080023#define vl_typedefs /* define message structures */
Dave Barach68b0fb02017-02-28 15:15:56 -050024#include <vpp/api/vpe_all_api_h.h>
25#undef vl_typedefs
26
27/* declare message handlers for each api */
28
Florin Corase04c2992017-03-01 08:17:34 -080029#define vl_endianfun /* define message structures */
Dave Barach68b0fb02017-02-28 15:15:56 -050030#include <vpp/api/vpe_all_api_h.h>
31#undef vl_endianfun
32
33/* instantiate all the print functions we know about */
34#define vl_print(handle, ...)
35#define vl_printfun
36#include <vpp/api/vpe_all_api_h.h>
37#undef vl_printfun
38
39/* Satisfy external references when not linking with -lvlib */
40vlib_main_t vlib_global_main;
41vlib_main_t **vlib_mains;
42
43typedef struct
44{
Florin Corase04c2992017-03-01 08:17:34 -080045 svm_fifo_t *server_rx_fifo;
46 svm_fifo_t *server_tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -050047
Florin Corasa5464812017-04-19 13:00:05 -070048 u64 vpp_session_handle;
Florin Corasf03a59a2017-06-09 21:07:32 -070049 u64 bytes_received;
50 f64 start;
Dave Barach68b0fb02017-02-28 15:15:56 -050051} session_t;
52
53typedef enum
54{
55 STATE_START,
Florin Corasa5464812017-04-19 13:00:05 -070056 STATE_ATTACHED,
Dave Barach68b0fb02017-02-28 15:15:56 -050057 STATE_READY,
58 STATE_DISCONNECTING,
59 STATE_FAILED
60} connection_state_t;
61
62typedef struct
63{
64 /* vpe input queue */
65 unix_shared_memory_queue_t *vl_input_queue;
66
67 /* API client handle */
68 u32 my_client_index;
69
70 /* The URI we're playing with */
Florin Corase04c2992017-03-01 08:17:34 -080071 u8 *uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050072
73 /* Session pool */
Florin Corase04c2992017-03-01 08:17:34 -080074 session_t *sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -050075
76 /* Hash table for disconnect processing */
Florin Corase04c2992017-03-01 08:17:34 -080077 uword *session_index_by_vpp_handles;
Dave Barach68b0fb02017-02-28 15:15:56 -050078
79 /* intermediate rx buffer */
Florin Corase04c2992017-03-01 08:17:34 -080080 u8 *rx_buf;
Dave Barach68b0fb02017-02-28 15:15:56 -050081
82 /* URI for slave's connect */
Florin Corase04c2992017-03-01 08:17:34 -080083 u8 *connect_uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050084
85 u32 connected_session_index;
86
87 int i_am_master;
88
89 /* drop all packets */
90 int drop_packets;
91
92 /* Our event queue */
Florin Corase04c2992017-03-01 08:17:34 -080093 unix_shared_memory_queue_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050094
95 /* $$$ single thread only for the moment */
Florin Corase04c2992017-03-01 08:17:34 -080096 unix_shared_memory_queue_t *vpp_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050097
98 pid_t my_pid;
99
100 /* For deadman timers */
101 clib_time_t clib_time;
102
103 /* State of the connection, shared between msg RX thread and main thread */
104 volatile connection_state_t state;
105
106 /* Signal variables */
107 volatile int time_to_stop;
108 volatile int time_to_print_stats;
109
110 u32 configured_segment_size;
111
112 /* VNET_API_ERROR_FOO -> "Foo" hash table */
Florin Corase04c2992017-03-01 08:17:34 -0800113 uword *error_string_by_error_number;
Dave Barach68b0fb02017-02-28 15:15:56 -0500114
115 u8 *connect_test_data;
Florin Corase04c2992017-03-01 08:17:34 -0800116 pthread_t client_rx_thread_handle;
117 u32 client_bytes_received;
118 u8 test_return_packets;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700119 u64 bytes_to_send;
Florin Corase04c2992017-03-01 08:17:34 -0800120
121 /* convenience */
122 svm_fifo_segment_main_t *segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500123} uri_tcp_test_main_t;
124
125uri_tcp_test_main_t uri_tcp_test_main;
126
127#if CLIB_DEBUG > 0
128#define NITER 10000
129#else
130#define NITER 4000000
131#endif
132
Florin Corasa5464812017-04-19 13:00:05 -0700133static u8 *
134format_api_error (u8 * s, va_list * args)
135{
136 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
137 i32 error = va_arg (*args, u32);
138 uword *p;
139
140 p = hash_get (utm->error_string_by_error_number, -error);
141
142 if (p)
143 s = format (s, "%s", p[0]);
144 else
145 s = format (s, "%d", error);
146 return s;
147}
148
149static void
150init_error_string_table (uri_tcp_test_main_t * utm)
151{
152 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
153
154#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
155 foreach_vnet_api_error;
156#undef _
157
158 hash_set (utm->error_string_by_error_number, 99, "Misc");
159}
160
Dave Barach68b0fb02017-02-28 15:15:56 -0500161int
162wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
163{
164#if CLIB_DEBUG > 0
165#define TIMEOUT 600.0
166#else
167#define TIMEOUT 600.0
168#endif
169
170 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
171
172 while (clib_time_now (&utm->clib_time) < timeout)
173 {
174 if (utm->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800175 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500176 if (utm->state == STATE_FAILED)
177 return -1;
flyingeagle23a07779f2017-04-26 20:22:04 +0800178 if (utm->time_to_stop == 1)
Florin Corasf03a59a2017-06-09 21:07:32 -0700179 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500180 }
181 clib_warning ("timeout waiting for STATE_READY");
182 return -1;
183}
184
Florin Coras6cf30ad2017-04-04 23:08:23 -0700185void
Florin Corasa5464812017-04-19 13:00:05 -0700186application_send_attach (uri_tcp_test_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700187{
188 vl_api_application_attach_t *bmp;
Florin Corasf03a59a2017-06-09 21:07:32 -0700189 u32 fifo_size = 4 << 20;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700190 bmp = vl_msg_api_alloc (sizeof (*bmp));
191 memset (bmp, 0, sizeof (*bmp));
192
193 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
194 bmp->client_index = utm->my_client_index;
195 bmp->context = ntohl (0xfeedface);
Florin Corasa5464812017-04-19 13:00:05 -0700196 bmp->options[APP_OPTIONS_FLAGS] =
197 APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT;
Dave Barach10d8cc62017-05-30 09:30:07 -0400198 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700199 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
200 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
201 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
202 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
203 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
204}
205
Florin Corasa5464812017-04-19 13:00:05 -0700206int
207application_attach (uri_tcp_test_main_t * utm)
208{
209 application_send_attach (utm);
210 if (wait_for_state_change (utm, STATE_ATTACHED))
211 {
212 clib_warning ("timeout waiting for STATE_ATTACHED");
213 return -1;
214 }
215 return 0;
216}
217
Florin Coras6cf30ad2017-04-04 23:08:23 -0700218void
219application_detach (uri_tcp_test_main_t * utm)
220{
221 vl_api_application_detach_t *bmp;
222 bmp = vl_msg_api_alloc (sizeof (*bmp));
223 memset (bmp, 0, sizeof (*bmp));
224
225 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
226 bmp->client_index = utm->my_client_index;
227 bmp->context = ntohl (0xfeedface);
228 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
229}
230
231static void
232vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
233 mp)
234{
235 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
236 svm_fifo_segment_create_args_t _a, *a = &_a;
237 int rv;
238
239 if (mp->retval)
240 {
Florin Corasa5464812017-04-19 13:00:05 -0700241 clib_warning ("attach failed: %U", format_api_error,
242 clib_net_to_host_u32 (mp->retval));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700243 utm->state = STATE_FAILED;
244 return;
245 }
246
247 if (mp->segment_name_length == 0)
248 {
249 clib_warning ("segment_name_length zero");
250 return;
251 }
252
253 a->segment_name = (char *) mp->segment_name;
254 a->segment_size = mp->segment_size;
255
256 ASSERT (mp->app_event_queue_address);
257
258 /* Attach to the segment vpp created */
259 rv = svm_fifo_segment_attach (a);
260 if (rv)
261 {
262 clib_warning ("svm_fifo_segment_attach ('%s') failed",
263 mp->segment_name);
264 return;
265 }
266
267 utm->our_event_queue =
Damjan Marion7bee80c2017-04-26 15:32:12 +0200268 uword_to_pointer (mp->app_event_queue_address,
269 unix_shared_memory_queue_t *);
Florin Corasa5464812017-04-19 13:00:05 -0700270 utm->state = STATE_ATTACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700271}
272
273static void
274vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
275 mp)
276{
277 if (mp->retval)
278 clib_warning ("detach returned with err: %d", mp->retval);
279}
280
Dave Barach68b0fb02017-02-28 15:15:56 -0500281static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500282stop_signal (int signum)
283{
284 uri_tcp_test_main_t *um = &uri_tcp_test_main;
285
286 um->time_to_stop = 1;
287}
288
289static void
290stats_signal (int signum)
291{
292 uri_tcp_test_main_t *um = &uri_tcp_test_main;
293
294 um->time_to_print_stats = 1;
295}
296
297static clib_error_t *
298setup_signal_handlers (void)
299{
300 signal (SIGINT, stats_signal);
301 signal (SIGQUIT, stop_signal);
302 signal (SIGTERM, stop_signal);
303
304 return 0;
305}
306
307void
308vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
309{
310 clib_warning ("BUG");
311}
312
313int
314connect_to_vpp (char *name)
315{
316 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
317 api_main_t *am = &api_main;
318
319 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
320 return -1;
321
322 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
323 utm->my_client_index = am->my_client_index;
324
325 return 0;
326}
327
328static void
Florin Corase04c2992017-03-01 08:17:34 -0800329vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500330{
331 svm_fifo_segment_create_args_t _a, *a = &_a;
332 int rv;
333
334 a->segment_name = (char *) mp->segment_name;
335 a->segment_size = mp->segment_size;
336 /* Attach to the segment vpp created */
337 rv = svm_fifo_segment_attach (a);
338 if (rv)
339 {
340 clib_warning ("svm_fifo_segment_attach ('%s') failed",
Florin Corase04c2992017-03-01 08:17:34 -0800341 mp->segment_name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500342 return;
343 }
344 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
Florin Corase04c2992017-03-01 08:17:34 -0800345 mp->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500346}
347
348static void
Florin Corasf03a59a2017-06-09 21:07:32 -0700349session_print_stats (uri_tcp_test_main_t * utm, session_t * session)
350{
351 f64 deltat;
352 u64 bytes;
353
354 deltat = clib_time_now (&utm->clib_time) - session->start;
355 bytes = utm->i_am_master ? session->bytes_received : utm->bytes_to_send;
356 fformat (stdout, "Finished in %.6f\n", deltat);
357 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
358}
359
360static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500361vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
362{
363 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corasf03a59a2017-06-09 21:07:32 -0700364 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800365 vl_api_disconnect_session_reply_t *rmp;
366 uword *p;
Dave Barach68b0fb02017-02-28 15:15:56 -0500367 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500368
Florin Coras6cf30ad2017-04-04 23:08:23 -0700369 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800370
371 if (p)
372 {
373 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700374 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800375 pool_put (utm->sessions, session);
376 }
377 else
378 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700379 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800380 rv = -11;
381 }
382
Florin Corasf03a59a2017-06-09 21:07:32 -0700383// utm->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800384
385 rmp = vl_msg_api_alloc (sizeof (*rmp));
386 memset (rmp, 0, sizeof (*rmp));
387
388 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
389 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700390 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800391 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -0700392
393 if (session)
394 session_print_stats (utm, session);
Florin Corase04c2992017-03-01 08:17:34 -0800395}
396
397static void
398vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
399{
400 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800401 vl_api_reset_session_reply_t *rmp;
402 uword *p;
403 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800404
Florin Coras6cf30ad2017-04-04 23:08:23 -0700405 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500406
407 if (p)
408 {
Florin Corasf6359c82017-06-19 12:26:09 -0400409 clib_warning ("got reset");
410 /* Cleanup later */
Florin Corasd79b41e2017-03-04 05:37:52 -0800411 utm->time_to_stop = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500412 }
413 else
414 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700415 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500416 rv = -11;
417 }
418
419 rmp = vl_msg_api_alloc (sizeof (*rmp));
420 memset (rmp, 0, sizeof (*rmp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800421 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500422 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700423 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800424 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500425}
426
427void
Florin Corase04c2992017-03-01 08:17:34 -0800428client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
429 session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -0500430{
Florin Corase04c2992017-03-01 08:17:34 -0800431 svm_fifo_t *rx_fifo;
432 int n_read, bytes, i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500433
434 rx_fifo = e->fifo;
435
Florin Coras6792ec02017-03-13 03:49:51 -0700436 bytes = svm_fifo_max_dequeue (rx_fifo);
437 /* Allow enqueuing of new event */
438 svm_fifo_unset_event (rx_fifo);
439
440 /* Read the bytes */
Dave Barach68b0fb02017-02-28 15:15:56 -0500441 do
442 {
Florin Corasa5464812017-04-19 13:00:05 -0700443 n_read = svm_fifo_dequeue_nowait (rx_fifo,
Florin Coras6792ec02017-03-13 03:49:51 -0700444 clib_min (vec_len (utm->rx_buf),
445 bytes), utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500446 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800447 {
448 bytes -= n_read;
Florin Corasf03a59a2017-06-09 21:07:32 -0700449 if (utm->test_return_packets)
Florin Corase04c2992017-03-01 08:17:34 -0800450 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700451 for (i = 0; i < n_read; i++)
Florin Corase04c2992017-03-01 08:17:34 -0800452 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700453 if (utm->rx_buf[i]
454 != ((utm->client_bytes_received + i) & 0xff))
455 {
456 clib_warning ("error at byte %lld, 0x%x not 0x%x",
457 utm->client_bytes_received + i,
458 utm->rx_buf[i],
459 ((utm->client_bytes_received +
460 i) & 0xff));
461 }
Florin Corase04c2992017-03-01 08:17:34 -0800462 }
463 }
464 utm->client_bytes_received += n_read;
465 }
Florin Coras6792ec02017-03-13 03:49:51 -0700466 else
467 {
468 if (n_read == -2)
469 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700470// clib_warning ("weird!");
Florin Coras6792ec02017-03-13 03:49:51 -0700471 break;
472 }
473 }
Florin Corase04c2992017-03-01 08:17:34 -0800474
Dave Barach68b0fb02017-02-28 15:15:56 -0500475 }
Florin Coras6792ec02017-03-13 03:49:51 -0700476 while (bytes > 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500477}
478
479void
Florin Corase04c2992017-03-01 08:17:34 -0800480client_handle_event_queue (uri_tcp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500481{
482 session_fifo_event_t _e, *e = &_e;;
483
Florin Corase04c2992017-03-01 08:17:34 -0800484 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
485 0 /* nowait */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500486 switch (e->event_type)
487 {
Florin Corasa5464812017-04-19 13:00:05 -0700488 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800489 client_handle_fifo_event_rx (utm, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500490 break;
491
Florin Corasa5464812017-04-19 13:00:05 -0700492 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500493 return;
494
495 default:
Florin Corase04c2992017-03-01 08:17:34 -0800496 clib_warning ("unknown event type %d", e->event_type);
Dave Barach68b0fb02017-02-28 15:15:56 -0500497 break;
498 }
499}
500
Florin Corase04c2992017-03-01 08:17:34 -0800501static void *
502client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500503{
Florin Corase04c2992017-03-01 08:17:34 -0800504 session_fifo_event_t _e, *e = &_e;
505 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500506
Florin Corase04c2992017-03-01 08:17:34 -0800507 utm->client_bytes_received = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500508 while (1)
509 {
Florin Corase04c2992017-03-01 08:17:34 -0800510 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
511 0 /* nowait */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500512 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800513 {
Florin Corasa5464812017-04-19 13:00:05 -0700514 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800515 client_handle_fifo_event_rx (utm, e);
516 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500517
Florin Corasa5464812017-04-19 13:00:05 -0700518 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -0800519 return 0;
520 default:
521 clib_warning ("unknown event type %d", e->event_type);
522 break;
523 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500524
Florin Corase04c2992017-03-01 08:17:34 -0800525 if (PREDICT_FALSE (utm->time_to_stop == 1))
526 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500527 }
Florin Corase04c2992017-03-01 08:17:34 -0800528 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500529}
530
Dave Barach68b0fb02017-02-28 15:15:56 -0500531
532static void
533vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
534{
535 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500536 session_t *session;
537 u32 session_index;
538 svm_fifo_t *rx_fifo, *tx_fifo;
539 int rv;
540
541 if (mp->retval)
542 {
Florin Corasa5464812017-04-19 13:00:05 -0700543 clib_warning ("connection failed with code: %U", format_api_error,
544 clib_net_to_host_u32 (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500545 utm->state = STATE_FAILED;
546 return;
547 }
Florin Corase04c2992017-03-01 08:17:34 -0800548
Damjan Marion7bee80c2017-04-26 15:32:12 +0200549 utm->vpp_event_queue =
550 uword_to_pointer (mp->vpp_event_queue_address,
551 unix_shared_memory_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500552
553 /*
554 * Setup session
555 */
556
557 pool_get (utm->sessions, session);
558 session_index = session - utm->sessions;
559
Damjan Marion7bee80c2017-04-26 15:32:12 +0200560 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500561 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200562 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500563 tx_fifo->client_session_index = session_index;
564
565 session->server_rx_fifo = rx_fifo;
566 session->server_tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700567 session->vpp_session_handle = mp->handle;
Florin Corasf03a59a2017-06-09 21:07:32 -0700568 session->start = clib_time_now (&utm->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500569
570 /* Save handle */
571 utm->connected_session_index = session_index;
Florin Corase04c2992017-03-01 08:17:34 -0800572 utm->state = STATE_READY;
573
574 /* Add it to lookup table */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700575 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800576
577 /* Start RX thread */
578 rv = pthread_create (&utm->client_rx_thread_handle,
579 NULL /*attr */ , client_rx_thread_fn, 0);
580 if (rv)
581 {
582 clib_warning ("pthread_create returned %d", rv);
583 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
584 }
585}
586
Florin Coras6792ec02017-03-13 03:49:51 -0700587static void
588send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
589 u32 bytes)
Florin Corase04c2992017-03-01 08:17:34 -0800590{
591 u8 *test_data = utm->connect_test_data;
592 u64 bytes_sent = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700593 int test_buf_offset = 0;
594 u32 bytes_to_snd;
Florin Corasf03a59a2017-06-09 21:07:32 -0700595 u32 queue_max_chunk = 128 << 10, actual_write;
Florin Corase04c2992017-03-01 08:17:34 -0800596 session_fifo_event_t evt;
597 static int serial_number = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700598 int rv;
Florin Corase04c2992017-03-01 08:17:34 -0800599
Florin Coras6792ec02017-03-13 03:49:51 -0700600 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
601 if (bytes_to_snd > vec_len (test_data))
602 bytes_to_snd = vec_len (test_data);
Florin Corase04c2992017-03-01 08:17:34 -0800603
Florin Corasf6359c82017-06-19 12:26:09 -0400604 while (bytes_to_snd > 0 && !utm->time_to_stop)
Florin Corase04c2992017-03-01 08:17:34 -0800605 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700606 actual_write = (bytes_to_snd > queue_max_chunk) ?
607 queue_max_chunk : bytes_to_snd;
Florin Corasa5464812017-04-19 13:00:05 -0700608 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
Florin Coras6792ec02017-03-13 03:49:51 -0700609 test_data + test_buf_offset);
610
611 if (rv > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800612 {
Florin Coras6792ec02017-03-13 03:49:51 -0700613 bytes_to_snd -= rv;
614 test_buf_offset += rv;
615 bytes_sent += rv;
Florin Corase04c2992017-03-01 08:17:34 -0800616
Florin Coras6792ec02017-03-13 03:49:51 -0700617 if (svm_fifo_set_event (tx_fifo))
Florin Corase04c2992017-03-01 08:17:34 -0800618 {
Florin Corase04c2992017-03-01 08:17:34 -0800619 /* Fabricate TX event, send to vpp */
620 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700621 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase04c2992017-03-01 08:17:34 -0800622 evt.event_id = serial_number++;
623
624 unix_shared_memory_queue_add (utm->vpp_event_queue,
625 (u8 *) & evt,
626 0 /* do wait for mutex */ );
627 }
628 }
629 }
Florin Coras6792ec02017-03-13 03:49:51 -0700630}
631
632void
633client_send_data (uri_tcp_test_main_t * utm)
634{
635 u8 *test_data = utm->connect_test_data;
636 int mypid = getpid ();
637 session_t *session;
638 svm_fifo_t *tx_fifo;
639 u32 n_iterations, leftover;
640 int i;
641
642 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
643 tx_fifo = session->server_tx_fifo;
644
Florin Coras82b13a82017-04-25 11:58:06 -0700645 ASSERT (vec_len (test_data) > 0);
646
Florin Coras6792ec02017-03-13 03:49:51 -0700647 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
648 n_iterations = utm->bytes_to_send / vec_len (test_data);
649
650 for (i = 0; i < n_iterations; i++)
651 {
652 send_test_chunk (utm, tx_fifo, mypid, 0);
Florin Corasf6359c82017-06-19 12:26:09 -0400653 if (utm->time_to_stop)
654 break;
Florin Coras6792ec02017-03-13 03:49:51 -0700655 }
656
657 leftover = utm->bytes_to_send % vec_len (test_data);
658 if (leftover)
659 send_test_chunk (utm, tx_fifo, mypid, leftover);
Florin Corase04c2992017-03-01 08:17:34 -0800660
Florin Corasf03a59a2017-06-09 21:07:32 -0700661 if (!utm->drop_packets)
Florin Corase04c2992017-03-01 08:17:34 -0800662 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700663 f64 timeout = clib_time_now (&utm->clib_time) + 10;
Florin Corase04c2992017-03-01 08:17:34 -0800664
665 /* Wait for the outstanding packets */
Florin Coras6792ec02017-03-13 03:49:51 -0700666 while (utm->client_bytes_received <
667 vec_len (test_data) * n_iterations + leftover)
Florin Corase04c2992017-03-01 08:17:34 -0800668 {
669 if (clib_time_now (&utm->clib_time) > timeout)
670 {
671 clib_warning ("timed out waiting for the missing packets");
672 break;
673 }
674 }
Florin Corase04c2992017-03-01 08:17:34 -0800675 }
Florin Coras6792ec02017-03-13 03:49:51 -0700676 utm->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800677}
678
679void
Florin Corasa5464812017-04-19 13:00:05 -0700680client_send_connect (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800681{
682 vl_api_connect_uri_t *cmp;
683 cmp = vl_msg_api_alloc (sizeof (*cmp));
684 memset (cmp, 0, sizeof (*cmp));
685
686 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
687 cmp->client_index = utm->my_client_index;
688 cmp->context = ntohl (0xfeedface);
689 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
690 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
691}
692
Florin Corasa5464812017-04-19 13:00:05 -0700693int
694client_connect (uri_tcp_test_main_t * utm)
695{
696 client_send_connect (utm);
697 if (wait_for_state_change (utm, STATE_READY))
698 {
699 clib_warning ("Connect failed");
700 return -1;
701 }
702 return 0;
703}
704
Florin Corase04c2992017-03-01 08:17:34 -0800705void
Florin Corasa5464812017-04-19 13:00:05 -0700706client_send_disconnect (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800707{
708 session_t *connected_session;
709 vl_api_disconnect_session_t *dmp;
710 connected_session = pool_elt_at_index (utm->sessions,
711 utm->connected_session_index);
712 dmp = vl_msg_api_alloc (sizeof (*dmp));
713 memset (dmp, 0, sizeof (*dmp));
714 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
715 dmp->client_index = utm->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700716 dmp->handle = connected_session->vpp_session_handle;
Florin Corase04c2992017-03-01 08:17:34 -0800717 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
718}
719
Florin Corasa5464812017-04-19 13:00:05 -0700720int
721client_disconnect (uri_tcp_test_main_t * utm)
722{
723 client_send_disconnect (utm);
Florin Corasf03a59a2017-06-09 21:07:32 -0700724 clib_warning ("Sent disconnect");
Florin Corasa5464812017-04-19 13:00:05 -0700725 if (wait_for_state_change (utm, STATE_START))
726 {
727 clib_warning ("Disconnect failed");
728 return -1;
729 }
730 return 0;
731}
732
Florin Corase04c2992017-03-01 08:17:34 -0800733static void
734client_test (uri_tcp_test_main_t * utm)
735{
736 int i;
737
Florin Corasa5464812017-04-19 13:00:05 -0700738 if (application_attach (utm))
739 return;
Florin Corase04c2992017-03-01 08:17:34 -0800740
Florin Corasa5464812017-04-19 13:00:05 -0700741 if (client_connect (utm))
Florin Corase04c2992017-03-01 08:17:34 -0800742 {
Florin Corasa5464812017-04-19 13:00:05 -0700743 application_detach (utm);
Florin Corase04c2992017-03-01 08:17:34 -0800744 return;
745 }
746
747 /* Init test data */
Florin Corasf03a59a2017-06-09 21:07:32 -0700748 vec_validate (utm->connect_test_data, 128 * 1024 - 1);
Florin Corase04c2992017-03-01 08:17:34 -0800749 for (i = 0; i < vec_len (utm->connect_test_data); i++)
750 utm->connect_test_data[i] = i & 0xff;
751
752 /* Start send */
753 client_send_data (utm);
754
755 /* Disconnect */
756 client_disconnect (utm);
Florin Coras6792ec02017-03-13 03:49:51 -0700757
Florin Coras6cf30ad2017-04-04 23:08:23 -0700758 application_detach (utm);
Florin Corase04c2992017-03-01 08:17:34 -0800759}
760
761static void
762vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
763{
764 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800765
766 if (mp->retval)
767 {
flyingeagle23a07779f2017-04-26 20:22:04 +0800768 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -0700769 clib_net_to_host_u32 (mp->retval));
Florin Corase04c2992017-03-01 08:17:34 -0800770 utm->state = STATE_FAILED;
771 return;
772 }
773
Dave Barach68b0fb02017-02-28 15:15:56 -0500774 utm->state = STATE_READY;
775}
776
Dave Barach68b0fb02017-02-28 15:15:56 -0500777static void
Florin Corase04c2992017-03-01 08:17:34 -0800778vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500779{
780 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
781
782 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -0800783 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500784
785 utm->state = STATE_START;
786}
787
Florin Coras6cf30ad2017-04-04 23:08:23 -0700788u8 *
789format_ip4_address (u8 * s, va_list * args)
790{
791 u8 *a = va_arg (*args, u8 *);
792 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
793}
794
795u8 *
796format_ip6_address (u8 * s, va_list * args)
797{
798 ip6_address_t *a = va_arg (*args, ip6_address_t *);
799 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
800
801 i_max_n_zero = ARRAY_LEN (a->as_u16);
802 max_n_zeros = 0;
803 i_first_zero = i_max_n_zero;
804 n_zeros = 0;
805 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
806 {
807 u32 is_zero = a->as_u16[i] == 0;
808 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
809 {
810 i_first_zero = i;
811 n_zeros = 0;
812 }
813 n_zeros += is_zero;
814 if ((!is_zero && n_zeros > max_n_zeros)
815 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
816 {
817 i_max_n_zero = i_first_zero;
818 max_n_zeros = n_zeros;
819 i_first_zero = ARRAY_LEN (a->as_u16);
820 n_zeros = 0;
821 }
822 }
823
824 last_double_colon = 0;
825 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
826 {
827 if (i == i_max_n_zero && max_n_zeros > 1)
828 {
829 s = format (s, "::");
830 i += max_n_zeros - 1;
831 last_double_colon = 1;
832 }
833 else
834 {
835 s = format (s, "%s%x",
836 (last_double_colon || i == 0) ? "" : ":",
837 clib_net_to_host_u16 (a->as_u16[i]));
838 last_double_colon = 0;
839 }
840 }
841
842 return s;
843}
844
845/* Format an IP46 address. */
846u8 *
847format_ip46_address (u8 * s, va_list * args)
848{
849 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
850 ip46_type_t type = va_arg (*args, ip46_type_t);
851 int is_ip4 = 1;
852
853 switch (type)
854 {
855 case IP46_TYPE_ANY:
856 is_ip4 = ip46_address_is_ip4 (ip46);
857 break;
858 case IP46_TYPE_IP4:
859 is_ip4 = 1;
860 break;
861 case IP46_TYPE_IP6:
862 is_ip4 = 0;
863 break;
864 }
865
866 return is_ip4 ?
867 format (s, "%U", format_ip4_address, &ip46->ip4) :
868 format (s, "%U", format_ip6_address, &ip46->ip6);
869}
870
Dave Barach68b0fb02017-02-28 15:15:56 -0500871static void
872vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
873{
874 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
875 vl_api_accept_session_reply_t *rmp;
Florin Corase04c2992017-03-01 08:17:34 -0800876 svm_fifo_t *rx_fifo, *tx_fifo;
877 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500878 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500879 u32 session_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700880 u8 *ip_str;
Dave Barach68b0fb02017-02-28 15:15:56 -0500881
882 if (start_time == 0.0)
Florin Corase04c2992017-03-01 08:17:34 -0800883 start_time = clib_time_now (&utm->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500884
Florin Coras6cf30ad2017-04-04 23:08:23 -0700885 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
886 clib_warning ("Accepted session from: %s:%d", ip_str,
887 clib_net_to_host_u16 (mp->port));
Damjan Marion7bee80c2017-04-26 15:32:12 +0200888 utm->vpp_event_queue =
889 uword_to_pointer (mp->vpp_event_queue_address,
890 unix_shared_memory_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500891
892 /* Allocate local session and set it up */
893 pool_get (utm->sessions, session);
894 session_index = session - utm->sessions;
895
Damjan Marion7bee80c2017-04-26 15:32:12 +0200896 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500897 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200898 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500899 tx_fifo->client_session_index = session_index;
900
901 session->server_rx_fifo = rx_fifo;
902 session->server_tx_fifo = tx_fifo;
903
904 /* Add it to lookup table */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700905 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500906
907 utm->state = STATE_READY;
908
909 /* Stats printing */
Florin Corase04c2992017-03-01 08:17:34 -0800910 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500911 {
912 f64 now = clib_time_now (&utm->clib_time);
913 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
Florin Corase04c2992017-03-01 08:17:34 -0800914 pool_elts (utm->sessions), now - start_time,
915 (f64) pool_elts (utm->sessions) / (now - start_time));
Dave Barach68b0fb02017-02-28 15:15:56 -0500916 }
917
Florin Corase04c2992017-03-01 08:17:34 -0800918 /*
919 * Send accept reply to vpp
920 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500921 rmp = vl_msg_api_alloc (sizeof (*rmp));
922 memset (rmp, 0, sizeof (*rmp));
923 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700924 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800925 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -0700926
927 session->bytes_received = 0;
928 session->start = clib_time_now (&utm->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500929}
930
931void
Florin Corase04c2992017-03-01 08:17:34 -0800932server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
933 session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -0500934{
Florin Corase04c2992017-03-01 08:17:34 -0800935 svm_fifo_t *rx_fifo, *tx_fifo;
936 int n_read;
Florin Corase04c2992017-03-01 08:17:34 -0800937 session_fifo_event_t evt;
938 unix_shared_memory_queue_t *q;
Florin Corasf03a59a2017-06-09 21:07:32 -0700939 session_t *session;
940 int rv;
941 u32 max_dequeue, offset, max_transfer, rx_buf_len;
Florin Corase04c2992017-03-01 08:17:34 -0800942
Florin Corasf03a59a2017-06-09 21:07:32 -0700943 rx_buf_len = vec_len (utm->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800944 rx_fifo = e->fifo;
Florin Corasf03a59a2017-06-09 21:07:32 -0700945 session = &utm->sessions[rx_fifo->client_session_index];
946 tx_fifo = session->server_tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -0800947
Florin Corasf03a59a2017-06-09 21:07:32 -0700948 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700949 /* Allow enqueuing of a new event */
950 svm_fifo_unset_event (rx_fifo);
951
Florin Corasf03a59a2017-06-09 21:07:32 -0700952 if (PREDICT_FALSE (max_dequeue == 0))
953 {
954 return;
955 }
Florin Coras6792ec02017-03-13 03:49:51 -0700956
Florin Corasf03a59a2017-06-09 21:07:32 -0700957 /* Read the max_dequeue */
Florin Corase04c2992017-03-01 08:17:34 -0800958 do
959 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700960 max_transfer = clib_min (rx_buf_len, max_dequeue);
961 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, utm->rx_buf);
Florin Coras6792ec02017-03-13 03:49:51 -0700962 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800963 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700964 max_dequeue -= n_read;
965 session->bytes_received += n_read;
966 }
967
968 /* Reflect if a non-drop session */
969 if (!utm->drop_packets && n_read > 0)
970 {
971 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800972 do
973 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700974 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
975 &utm->rx_buf[offset]);
976 if (rv > 0)
977 {
978 n_read -= rv;
979 offset += rv;
980 }
Florin Corase04c2992017-03-01 08:17:34 -0800981 }
Florin Corasf03a59a2017-06-09 21:07:32 -0700982 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -0800983
Florin Coras6792ec02017-03-13 03:49:51 -0700984 /* If event wasn't set, add one */
985 if (svm_fifo_set_event (tx_fifo))
986 {
987 /* Fabricate TX event, send to vpp */
988 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700989 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -0700990 evt.event_id = e->event_id;
991
992 q = utm->vpp_event_queue;
993 unix_shared_memory_queue_add (q, (u8 *) & evt,
Florin Corasf03a59a2017-06-09 21:07:32 -0700994 1 /* do wait for mutex */ );
Florin Coras6792ec02017-03-13 03:49:51 -0700995 }
Florin Corase04c2992017-03-01 08:17:34 -0800996 }
Florin Corase04c2992017-03-01 08:17:34 -0800997 }
Florin Corasf03a59a2017-06-09 21:07:32 -0700998 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -0800999}
1000
1001void
1002server_handle_event_queue (uri_tcp_test_main_t * utm)
1003{
1004 session_fifo_event_t _e, *e = &_e;;
1005
1006 while (1)
1007 {
1008 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
1009 0 /* nowait */ );
1010 switch (e->event_type)
1011 {
Florin Corasa5464812017-04-19 13:00:05 -07001012 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -08001013 server_handle_fifo_event_rx (utm, e);
1014 break;
1015
Florin Corasa5464812017-04-19 13:00:05 -07001016 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -08001017 return;
1018
1019 default:
1020 clib_warning ("unknown event type %d", e->event_type);
1021 break;
1022 }
1023 if (PREDICT_FALSE (utm->time_to_stop == 1))
1024 break;
1025 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1026 {
1027 utm->time_to_print_stats = 0;
1028 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1029 }
1030 }
1031}
1032
1033void
Florin Corasa5464812017-04-19 13:00:05 -07001034server_send_listen (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -08001035{
1036 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001037 bmp = vl_msg_api_alloc (sizeof (*bmp));
1038 memset (bmp, 0, sizeof (*bmp));
1039
1040 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1041 bmp->client_index = utm->my_client_index;
1042 bmp->context = ntohl (0xfeedface);
Florin Corase04c2992017-03-01 08:17:34 -08001043 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1044 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1045}
1046
Florin Corasa5464812017-04-19 13:00:05 -07001047int
1048server_listen (uri_tcp_test_main_t * utm)
1049{
1050 server_send_listen (utm);
1051 if (wait_for_state_change (utm, STATE_READY))
1052 {
1053 clib_warning ("timeout waiting for STATE_READY");
1054 return -1;
1055 }
1056 return 0;
1057}
1058
Florin Corase04c2992017-03-01 08:17:34 -08001059void
Florin Corasa5464812017-04-19 13:00:05 -07001060server_send_unbind (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -08001061{
1062 vl_api_unbind_uri_t *ump;
1063
1064 ump = vl_msg_api_alloc (sizeof (*ump));
1065 memset (ump, 0, sizeof (*ump));
1066
1067 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1068 ump->client_index = utm->my_client_index;
1069 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1070 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1071}
1072
Florin Corasa5464812017-04-19 13:00:05 -07001073int
1074server_unbind (uri_tcp_test_main_t * utm)
1075{
1076 server_send_unbind (utm);
1077 if (wait_for_state_change (utm, STATE_START))
1078 {
1079 clib_warning ("timeout waiting for STATE_START");
1080 return -1;
1081 }
1082 return 0;
1083}
1084
Florin Corase04c2992017-03-01 08:17:34 -08001085void
1086server_test (uri_tcp_test_main_t * utm)
1087{
Florin Corasa5464812017-04-19 13:00:05 -07001088 if (application_attach (utm))
1089 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001090
Dave Barach68b0fb02017-02-28 15:15:56 -05001091 /* Bind to uri */
Florin Corasa5464812017-04-19 13:00:05 -07001092 if (server_listen (utm))
1093 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001094
1095 /* Enter handle event loop */
Florin Corase04c2992017-03-01 08:17:34 -08001096 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001097
1098 /* Cleanup */
Florin Corasa5464812017-04-19 13:00:05 -07001099 server_send_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001100
Florin Coras6cf30ad2017-04-04 23:08:23 -07001101 application_detach (utm);
1102
Dave Barach68b0fb02017-02-28 15:15:56 -05001103 fformat (stdout, "Test complete...\n");
1104}
1105
Florin Corase69f4952017-03-07 10:06:24 -08001106static void
1107vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1108 mp)
1109{
Florin Coras6792ec02017-03-13 03:49:51 -07001110 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corasf03a59a2017-06-09 21:07:32 -07001111 session_t *session;
Florin Coras6792ec02017-03-13 03:49:51 -07001112
Florin Corasf03a59a2017-06-09 21:07:32 -07001113 if (mp->retval)
1114 {
1115 clib_warning ("vpp complained about disconnect: %d",
1116 ntohl (mp->retval));
1117 }
1118
Florin Coras6792ec02017-03-13 03:49:51 -07001119 utm->state = STATE_START;
Florin Corasf03a59a2017-06-09 21:07:32 -07001120 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
1121 if (session)
1122 session_print_stats (utm, session);
Florin Corase69f4952017-03-07 10:06:24 -08001123}
1124
1125#define foreach_uri_msg \
1126_(BIND_URI_REPLY, bind_uri_reply) \
1127_(UNBIND_URI_REPLY, unbind_uri_reply) \
1128_(ACCEPT_SESSION, accept_session) \
1129_(CONNECT_URI_REPLY, connect_uri_reply) \
1130_(DISCONNECT_SESSION, disconnect_session) \
1131_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1132_(RESET_SESSION, reset_session) \
Florin Coras6cf30ad2017-04-04 23:08:23 -07001133_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1134_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1135_(MAP_ANOTHER_SEGMENT, map_another_segment) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001136
1137void
1138uri_api_hookup (uri_tcp_test_main_t * utm)
1139{
1140#define _(N,n) \
1141 vl_msg_api_set_handlers(VL_API_##N, #n, \
1142 vl_api_##n##_t_handler, \
1143 vl_noop_handler, \
1144 vl_api_##n##_t_endian, \
1145 vl_api_##n##_t_print, \
1146 sizeof(vl_api_##n##_t), 1);
1147 foreach_uri_msg;
1148#undef _
1149}
1150
1151int
1152main (int argc, char **argv)
1153{
1154 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1155 unformat_input_t _argv, *a = &_argv;
1156 u8 *chroot_prefix;
Florin Corase69f4952017-03-07 10:06:24 -08001157 u8 *heap, *uri = 0;
1158 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1159 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001160 u64 bytes_to_send = 64 << 10, mbytes;
Dave Barach68b0fb02017-02-28 15:15:56 -05001161 u32 tmp;
1162 mheap_t *h;
Florin Corase04c2992017-03-01 08:17:34 -08001163 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -05001164 int i;
Florin Corase04c2992017-03-01 08:17:34 -08001165 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001166
1167 clib_mem_init (0, 256 << 20);
1168
1169 heap = clib_mem_get_per_cpu_heap ();
1170 h = mheap_header (heap);
1171
1172 /* make the main heap thread-safe */
1173 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1174
Florin Corasf03a59a2017-06-09 21:07:32 -07001175 vec_validate (utm->rx_buf, 128 << 10);
Dave Barach68b0fb02017-02-28 15:15:56 -05001176
Florin Corase04c2992017-03-01 08:17:34 -08001177 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Dave Barach68b0fb02017-02-28 15:15:56 -05001178
Florin Corase04c2992017-03-01 08:17:34 -08001179 utm->my_pid = getpid ();
1180 utm->configured_segment_size = 1 << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001181
1182 clib_time_init (&utm->clib_time);
1183 init_error_string_table (utm);
Florin Corase04c2992017-03-01 08:17:34 -08001184 svm_fifo_segment_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001185 unformat_init_command_line (a, argv);
1186
1187 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1188 {
1189 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001190 {
1191 vl_set_memory_root_path ((char *) chroot_prefix);
1192 }
Florin Corase69f4952017-03-07 10:06:24 -08001193 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001194 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001195 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corase04c2992017-03-01 08:17:34 -08001196 utm->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001197 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corase04c2992017-03-01 08:17:34 -08001198 utm->configured_segment_size = tmp << 30;
Dave Barach68b0fb02017-02-28 15:15:56 -05001199 else if (unformat (a, "master"))
Florin Corase04c2992017-03-01 08:17:34 -08001200 i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001201 else if (unformat (a, "slave"))
Florin Corase04c2992017-03-01 08:17:34 -08001202 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001203 else if (unformat (a, "drop"))
Florin Corase04c2992017-03-01 08:17:34 -08001204 drop_packets = 1;
1205 else if (unformat (a, "test"))
1206 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001207 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001208 {
1209 bytes_to_send = mbytes << 20;
1210 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001211 else if (unformat (a, "gbytes %lld", &mbytes))
1212 {
1213 bytes_to_send = mbytes << 30;
1214 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001215 else
Florin Corase04c2992017-03-01 08:17:34 -08001216 {
1217 fformat (stderr, "%s: usage [master|slave]\n");
1218 exit (1);
1219 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001220 }
1221
Florin Corase69f4952017-03-07 10:06:24 -08001222 if (uri)
1223 {
1224 utm->uri = format (0, "%s%c", uri, 0);
1225 utm->connect_uri = format (0, "%s%c", uri, 0);
1226 }
1227 else
1228 {
1229 utm->uri = format (0, "%s%c", bind_uri, 0);
1230 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1231 }
1232
Dave Barach68b0fb02017-02-28 15:15:56 -05001233 utm->i_am_master = i_am_master;
1234 utm->segment_main = &svm_fifo_segment_main;
1235 utm->drop_packets = drop_packets;
Florin Corase04c2992017-03-01 08:17:34 -08001236 utm->test_return_packets = test_return_packets;
Florin Coras6792ec02017-03-13 03:49:51 -07001237 utm->bytes_to_send = bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -07001238 utm->time_to_stop = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001239
Florin Corase04c2992017-03-01 08:17:34 -08001240 setup_signal_handlers ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001241 uri_api_hookup (utm);
1242
Florin Corase04c2992017-03-01 08:17:34 -08001243 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001244 {
1245 svm_region_exit ();
1246 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1247 exit (1);
1248 }
1249
1250 if (i_am_master == 0)
1251 {
Florin Corase04c2992017-03-01 08:17:34 -08001252 client_test (utm);
Florin Corase69f4952017-03-07 10:06:24 -08001253 vl_client_disconnect_from_vlib ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001254 exit (0);
1255 }
1256
1257 /* $$$$ hack preallocation */
1258 for (i = 0; i < 200000; i++)
1259 {
1260 pool_get (utm->sessions, session);
1261 memset (session, 0, sizeof (*session));
1262 }
1263 for (i = 0; i < 200000; i++)
1264 pool_put_index (utm->sessions, i);
1265
Florin Corase04c2992017-03-01 08:17:34 -08001266 server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001267
1268 vl_client_disconnect_from_vlib ();
1269 exit (0);
1270}
Florin Corase04c2992017-03-01 08:17:34 -08001271
1272/*
1273 * fd.io coding-style-patch-verification: ON
1274 *
1275 * Local Variables:
1276 * eval: (c-set-style "gnu")
1277 * End:
1278 */