blob: cb297b55b98e90c7f200642840887c6f39a3381f [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
Dave Barach68b0fb02017-02-28 15:15:56 -050039typedef struct
40{
Florin Corase04c2992017-03-01 08:17:34 -080041 svm_fifo_t *server_rx_fifo;
42 svm_fifo_t *server_tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -050043
Florin Corasa5464812017-04-19 13:00:05 -070044 u64 vpp_session_handle;
Florin Corasf03a59a2017-06-09 21:07:32 -070045 u64 bytes_received;
46 f64 start;
Dave Barach68b0fb02017-02-28 15:15:56 -050047} session_t;
48
49typedef enum
50{
51 STATE_START,
Florin Corasa5464812017-04-19 13:00:05 -070052 STATE_ATTACHED,
Dave Barach68b0fb02017-02-28 15:15:56 -050053 STATE_READY,
54 STATE_DISCONNECTING,
55 STATE_FAILED
56} connection_state_t;
57
58typedef struct
59{
60 /* vpe input queue */
61 unix_shared_memory_queue_t *vl_input_queue;
62
63 /* API client handle */
64 u32 my_client_index;
65
66 /* The URI we're playing with */
Florin Corase04c2992017-03-01 08:17:34 -080067 u8 *uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050068
69 /* Session pool */
Florin Corase04c2992017-03-01 08:17:34 -080070 session_t *sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -050071
72 /* Hash table for disconnect processing */
Florin Corase04c2992017-03-01 08:17:34 -080073 uword *session_index_by_vpp_handles;
Dave Barach68b0fb02017-02-28 15:15:56 -050074
75 /* intermediate rx buffer */
Florin Corase04c2992017-03-01 08:17:34 -080076 u8 *rx_buf;
Dave Barach68b0fb02017-02-28 15:15:56 -050077
78 /* URI for slave's connect */
Florin Corase04c2992017-03-01 08:17:34 -080079 u8 *connect_uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050080
81 u32 connected_session_index;
82
83 int i_am_master;
84
85 /* drop all packets */
86 int drop_packets;
87
88 /* Our event queue */
Florin Corase04c2992017-03-01 08:17:34 -080089 unix_shared_memory_queue_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050090
91 /* $$$ single thread only for the moment */
Florin Corase04c2992017-03-01 08:17:34 -080092 unix_shared_memory_queue_t *vpp_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050093
94 pid_t my_pid;
95
96 /* For deadman timers */
97 clib_time_t clib_time;
98
99 /* State of the connection, shared between msg RX thread and main thread */
100 volatile connection_state_t state;
101
102 /* Signal variables */
103 volatile int time_to_stop;
104 volatile int time_to_print_stats;
105
106 u32 configured_segment_size;
107
108 /* VNET_API_ERROR_FOO -> "Foo" hash table */
Florin Corase04c2992017-03-01 08:17:34 -0800109 uword *error_string_by_error_number;
Dave Barach68b0fb02017-02-28 15:15:56 -0500110
111 u8 *connect_test_data;
Florin Corase04c2992017-03-01 08:17:34 -0800112 pthread_t client_rx_thread_handle;
113 u32 client_bytes_received;
114 u8 test_return_packets;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700115 u64 bytes_to_send;
Florin Corase04c2992017-03-01 08:17:34 -0800116
117 /* convenience */
118 svm_fifo_segment_main_t *segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500119} uri_tcp_test_main_t;
120
121uri_tcp_test_main_t uri_tcp_test_main;
122
123#if CLIB_DEBUG > 0
124#define NITER 10000
125#else
126#define NITER 4000000
127#endif
128
Florin Corasa5464812017-04-19 13:00:05 -0700129static u8 *
130format_api_error (u8 * s, va_list * args)
131{
132 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
133 i32 error = va_arg (*args, u32);
134 uword *p;
135
136 p = hash_get (utm->error_string_by_error_number, -error);
137
138 if (p)
139 s = format (s, "%s", p[0]);
140 else
141 s = format (s, "%d", error);
142 return s;
143}
144
145static void
146init_error_string_table (uri_tcp_test_main_t * utm)
147{
148 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
149
150#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
151 foreach_vnet_api_error;
152#undef _
153
154 hash_set (utm->error_string_by_error_number, 99, "Misc");
155}
156
Dave Barach68b0fb02017-02-28 15:15:56 -0500157int
158wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
159{
160#if CLIB_DEBUG > 0
161#define TIMEOUT 600.0
162#else
163#define TIMEOUT 600.0
164#endif
165
166 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
167
168 while (clib_time_now (&utm->clib_time) < timeout)
169 {
170 if (utm->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800171 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500172 if (utm->state == STATE_FAILED)
173 return -1;
flyingeagle23a07779f2017-04-26 20:22:04 +0800174 if (utm->time_to_stop == 1)
Florin Corasf03a59a2017-06-09 21:07:32 -0700175 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500176 }
177 clib_warning ("timeout waiting for STATE_READY");
178 return -1;
179}
180
Florin Coras6cf30ad2017-04-04 23:08:23 -0700181void
Florin Corasa5464812017-04-19 13:00:05 -0700182application_send_attach (uri_tcp_test_main_t * utm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700183{
184 vl_api_application_attach_t *bmp;
Florin Corasf03a59a2017-06-09 21:07:32 -0700185 u32 fifo_size = 4 << 20;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700186 bmp = vl_msg_api_alloc (sizeof (*bmp));
187 memset (bmp, 0, sizeof (*bmp));
188
189 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
190 bmp->client_index = utm->my_client_index;
191 bmp->context = ntohl (0xfeedface);
Florin Corasa5464812017-04-19 13:00:05 -0700192 bmp->options[APP_OPTIONS_FLAGS] =
193 APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT;
Dave Barach10d8cc62017-05-30 09:30:07 -0400194 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700195 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
196 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
197 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
198 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
199 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
200}
201
Florin Corasa5464812017-04-19 13:00:05 -0700202int
203application_attach (uri_tcp_test_main_t * utm)
204{
205 application_send_attach (utm);
206 if (wait_for_state_change (utm, STATE_ATTACHED))
207 {
208 clib_warning ("timeout waiting for STATE_ATTACHED");
209 return -1;
210 }
211 return 0;
212}
213
Florin Coras6cf30ad2017-04-04 23:08:23 -0700214void
215application_detach (uri_tcp_test_main_t * utm)
216{
217 vl_api_application_detach_t *bmp;
218 bmp = vl_msg_api_alloc (sizeof (*bmp));
219 memset (bmp, 0, sizeof (*bmp));
220
221 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
222 bmp->client_index = utm->my_client_index;
223 bmp->context = ntohl (0xfeedface);
224 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
225}
226
227static void
228vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
229 mp)
230{
231 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
232 svm_fifo_segment_create_args_t _a, *a = &_a;
233 int rv;
234
235 if (mp->retval)
236 {
Florin Corasa5464812017-04-19 13:00:05 -0700237 clib_warning ("attach failed: %U", format_api_error,
238 clib_net_to_host_u32 (mp->retval));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700239 utm->state = STATE_FAILED;
240 return;
241 }
242
243 if (mp->segment_name_length == 0)
244 {
245 clib_warning ("segment_name_length zero");
246 return;
247 }
248
249 a->segment_name = (char *) mp->segment_name;
250 a->segment_size = mp->segment_size;
251
252 ASSERT (mp->app_event_queue_address);
253
254 /* Attach to the segment vpp created */
255 rv = svm_fifo_segment_attach (a);
256 if (rv)
257 {
258 clib_warning ("svm_fifo_segment_attach ('%s') failed",
259 mp->segment_name);
260 return;
261 }
262
263 utm->our_event_queue =
Damjan Marion7bee80c2017-04-26 15:32:12 +0200264 uword_to_pointer (mp->app_event_queue_address,
265 unix_shared_memory_queue_t *);
Florin Corasa5464812017-04-19 13:00:05 -0700266 utm->state = STATE_ATTACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700267}
268
269static void
270vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
271 mp)
272{
273 if (mp->retval)
274 clib_warning ("detach returned with err: %d", mp->retval);
275}
276
Dave Barach68b0fb02017-02-28 15:15:56 -0500277static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500278stop_signal (int signum)
279{
280 uri_tcp_test_main_t *um = &uri_tcp_test_main;
281
282 um->time_to_stop = 1;
283}
284
285static void
286stats_signal (int signum)
287{
288 uri_tcp_test_main_t *um = &uri_tcp_test_main;
289
290 um->time_to_print_stats = 1;
291}
292
293static clib_error_t *
294setup_signal_handlers (void)
295{
296 signal (SIGINT, stats_signal);
297 signal (SIGQUIT, stop_signal);
298 signal (SIGTERM, stop_signal);
299
300 return 0;
301}
302
303void
304vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
305{
306 clib_warning ("BUG");
307}
308
309int
310connect_to_vpp (char *name)
311{
312 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
313 api_main_t *am = &api_main;
314
315 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
316 return -1;
317
318 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
319 utm->my_client_index = am->my_client_index;
320
321 return 0;
322}
323
324static void
Florin Corase04c2992017-03-01 08:17:34 -0800325vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500326{
327 svm_fifo_segment_create_args_t _a, *a = &_a;
328 int rv;
329
330 a->segment_name = (char *) mp->segment_name;
331 a->segment_size = mp->segment_size;
332 /* Attach to the segment vpp created */
333 rv = svm_fifo_segment_attach (a);
334 if (rv)
335 {
336 clib_warning ("svm_fifo_segment_attach ('%s') failed",
Florin Corase04c2992017-03-01 08:17:34 -0800337 mp->segment_name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500338 return;
339 }
340 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
Florin Corase04c2992017-03-01 08:17:34 -0800341 mp->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500342}
343
344static void
Florin Corasf03a59a2017-06-09 21:07:32 -0700345session_print_stats (uri_tcp_test_main_t * utm, session_t * session)
346{
347 f64 deltat;
348 u64 bytes;
349
350 deltat = clib_time_now (&utm->clib_time) - session->start;
351 bytes = utm->i_am_master ? session->bytes_received : utm->bytes_to_send;
352 fformat (stdout, "Finished in %.6f\n", deltat);
353 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
354}
355
356static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500357vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
358{
359 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corasf03a59a2017-06-09 21:07:32 -0700360 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800361 vl_api_disconnect_session_reply_t *rmp;
362 uword *p;
Dave Barach68b0fb02017-02-28 15:15:56 -0500363 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500364
Florin Coras6cf30ad2017-04-04 23:08:23 -0700365 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800366
367 if (p)
368 {
369 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700370 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800371 pool_put (utm->sessions, session);
372 }
373 else
374 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700375 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800376 rv = -11;
377 }
378
Florin Corasf03a59a2017-06-09 21:07:32 -0700379// utm->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800380
381 rmp = vl_msg_api_alloc (sizeof (*rmp));
382 memset (rmp, 0, sizeof (*rmp));
383
384 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
385 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700386 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800387 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -0700388
389 if (session)
390 session_print_stats (utm, session);
Florin Corase04c2992017-03-01 08:17:34 -0800391}
392
393static void
394vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
395{
396 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800397 vl_api_reset_session_reply_t *rmp;
398 uword *p;
399 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800400
Florin Coras6cf30ad2017-04-04 23:08:23 -0700401 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500402
403 if (p)
404 {
Florin Corasf6359c82017-06-19 12:26:09 -0400405 clib_warning ("got reset");
406 /* Cleanup later */
Florin Corasd79b41e2017-03-04 05:37:52 -0800407 utm->time_to_stop = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500408 }
409 else
410 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700411 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500412 rv = -11;
413 }
414
415 rmp = vl_msg_api_alloc (sizeof (*rmp));
416 memset (rmp, 0, sizeof (*rmp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800417 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500418 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700419 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800420 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500421}
422
423void
Florin Corase04c2992017-03-01 08:17:34 -0800424client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
425 session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -0500426{
Florin Corase04c2992017-03-01 08:17:34 -0800427 svm_fifo_t *rx_fifo;
428 int n_read, bytes, i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500429
430 rx_fifo = e->fifo;
431
Florin Coras6792ec02017-03-13 03:49:51 -0700432 bytes = svm_fifo_max_dequeue (rx_fifo);
433 /* Allow enqueuing of new event */
434 svm_fifo_unset_event (rx_fifo);
435
436 /* Read the bytes */
Dave Barach68b0fb02017-02-28 15:15:56 -0500437 do
438 {
Florin Corasa5464812017-04-19 13:00:05 -0700439 n_read = svm_fifo_dequeue_nowait (rx_fifo,
Florin Coras6792ec02017-03-13 03:49:51 -0700440 clib_min (vec_len (utm->rx_buf),
441 bytes), utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500442 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800443 {
444 bytes -= n_read;
Florin Corasf03a59a2017-06-09 21:07:32 -0700445 if (utm->test_return_packets)
Florin Corase04c2992017-03-01 08:17:34 -0800446 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700447 for (i = 0; i < n_read; i++)
Florin Corase04c2992017-03-01 08:17:34 -0800448 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700449 if (utm->rx_buf[i]
450 != ((utm->client_bytes_received + i) & 0xff))
451 {
452 clib_warning ("error at byte %lld, 0x%x not 0x%x",
453 utm->client_bytes_received + i,
454 utm->rx_buf[i],
455 ((utm->client_bytes_received +
456 i) & 0xff));
457 }
Florin Corase04c2992017-03-01 08:17:34 -0800458 }
459 }
460 utm->client_bytes_received += n_read;
461 }
Florin Coras6792ec02017-03-13 03:49:51 -0700462 else
463 {
464 if (n_read == -2)
465 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700466// clib_warning ("weird!");
Florin Coras6792ec02017-03-13 03:49:51 -0700467 break;
468 }
469 }
Florin Corase04c2992017-03-01 08:17:34 -0800470
Dave Barach68b0fb02017-02-28 15:15:56 -0500471 }
Florin Coras6792ec02017-03-13 03:49:51 -0700472 while (bytes > 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500473}
474
475void
Florin Corase04c2992017-03-01 08:17:34 -0800476client_handle_event_queue (uri_tcp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500477{
478 session_fifo_event_t _e, *e = &_e;;
479
Florin Corase04c2992017-03-01 08:17:34 -0800480 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
481 0 /* nowait */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500482 switch (e->event_type)
483 {
Florin Corasa5464812017-04-19 13:00:05 -0700484 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800485 client_handle_fifo_event_rx (utm, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500486 break;
487
Florin Corasa5464812017-04-19 13:00:05 -0700488 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500489 return;
490
491 default:
Florin Corase04c2992017-03-01 08:17:34 -0800492 clib_warning ("unknown event type %d", e->event_type);
Dave Barach68b0fb02017-02-28 15:15:56 -0500493 break;
494 }
495}
496
Florin Corase04c2992017-03-01 08:17:34 -0800497static void *
498client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500499{
Florin Corase04c2992017-03-01 08:17:34 -0800500 session_fifo_event_t _e, *e = &_e;
501 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500502
Florin Corase04c2992017-03-01 08:17:34 -0800503 utm->client_bytes_received = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500504 while (1)
505 {
Florin Corase04c2992017-03-01 08:17:34 -0800506 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
507 0 /* nowait */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500508 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800509 {
Florin Corasa5464812017-04-19 13:00:05 -0700510 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800511 client_handle_fifo_event_rx (utm, e);
512 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500513
Florin Corasa5464812017-04-19 13:00:05 -0700514 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -0800515 return 0;
516 default:
517 clib_warning ("unknown event type %d", e->event_type);
518 break;
519 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500520
Florin Corase04c2992017-03-01 08:17:34 -0800521 if (PREDICT_FALSE (utm->time_to_stop == 1))
522 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500523 }
Florin Corase04c2992017-03-01 08:17:34 -0800524 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500525}
526
Dave Barach68b0fb02017-02-28 15:15:56 -0500527
528static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400529vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500530{
531 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500532 session_t *session;
533 u32 session_index;
534 svm_fifo_t *rx_fifo, *tx_fifo;
535 int rv;
536
537 if (mp->retval)
538 {
Florin Corasa5464812017-04-19 13:00:05 -0700539 clib_warning ("connection failed with code: %U", format_api_error,
540 clib_net_to_host_u32 (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500541 utm->state = STATE_FAILED;
542 return;
543 }
Florin Corase04c2992017-03-01 08:17:34 -0800544
Damjan Marion7bee80c2017-04-26 15:32:12 +0200545 utm->vpp_event_queue =
546 uword_to_pointer (mp->vpp_event_queue_address,
547 unix_shared_memory_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500548
549 /*
550 * Setup session
551 */
552
553 pool_get (utm->sessions, session);
554 session_index = session - utm->sessions;
555
Damjan Marion7bee80c2017-04-26 15:32:12 +0200556 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500557 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200558 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500559 tx_fifo->client_session_index = session_index;
560
561 session->server_rx_fifo = rx_fifo;
562 session->server_tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700563 session->vpp_session_handle = mp->handle;
Florin Corasf03a59a2017-06-09 21:07:32 -0700564 session->start = clib_time_now (&utm->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500565
566 /* Save handle */
567 utm->connected_session_index = session_index;
Florin Corase04c2992017-03-01 08:17:34 -0800568 utm->state = STATE_READY;
569
570 /* Add it to lookup table */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700571 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800572
573 /* Start RX thread */
574 rv = pthread_create (&utm->client_rx_thread_handle,
575 NULL /*attr */ , client_rx_thread_fn, 0);
576 if (rv)
577 {
578 clib_warning ("pthread_create returned %d", rv);
579 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
580 }
581}
582
Florin Coras6792ec02017-03-13 03:49:51 -0700583static void
584send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
585 u32 bytes)
Florin Corase04c2992017-03-01 08:17:34 -0800586{
587 u8 *test_data = utm->connect_test_data;
588 u64 bytes_sent = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700589 int test_buf_offset = 0;
590 u32 bytes_to_snd;
Florin Corasf03a59a2017-06-09 21:07:32 -0700591 u32 queue_max_chunk = 128 << 10, actual_write;
Florin Corase04c2992017-03-01 08:17:34 -0800592 session_fifo_event_t evt;
593 static int serial_number = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700594 int rv;
Florin Corase04c2992017-03-01 08:17:34 -0800595
Florin Coras6792ec02017-03-13 03:49:51 -0700596 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
597 if (bytes_to_snd > vec_len (test_data))
598 bytes_to_snd = vec_len (test_data);
Florin Corase04c2992017-03-01 08:17:34 -0800599
Florin Corasf6359c82017-06-19 12:26:09 -0400600 while (bytes_to_snd > 0 && !utm->time_to_stop)
Florin Corase04c2992017-03-01 08:17:34 -0800601 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700602 actual_write = (bytes_to_snd > queue_max_chunk) ?
603 queue_max_chunk : bytes_to_snd;
Florin Corasa5464812017-04-19 13:00:05 -0700604 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
Florin Coras6792ec02017-03-13 03:49:51 -0700605 test_data + test_buf_offset);
606
607 if (rv > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800608 {
Florin Coras6792ec02017-03-13 03:49:51 -0700609 bytes_to_snd -= rv;
610 test_buf_offset += rv;
611 bytes_sent += rv;
Florin Corase04c2992017-03-01 08:17:34 -0800612
Florin Coras6792ec02017-03-13 03:49:51 -0700613 if (svm_fifo_set_event (tx_fifo))
Florin Corase04c2992017-03-01 08:17:34 -0800614 {
Florin Corase04c2992017-03-01 08:17:34 -0800615 /* Fabricate TX event, send to vpp */
616 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700617 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase04c2992017-03-01 08:17:34 -0800618 evt.event_id = serial_number++;
619
620 unix_shared_memory_queue_add (utm->vpp_event_queue,
621 (u8 *) & evt,
622 0 /* do wait for mutex */ );
623 }
624 }
625 }
Florin Coras6792ec02017-03-13 03:49:51 -0700626}
627
628void
629client_send_data (uri_tcp_test_main_t * utm)
630{
631 u8 *test_data = utm->connect_test_data;
632 int mypid = getpid ();
633 session_t *session;
634 svm_fifo_t *tx_fifo;
635 u32 n_iterations, leftover;
636 int i;
637
638 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
639 tx_fifo = session->server_tx_fifo;
640
Florin Coras82b13a82017-04-25 11:58:06 -0700641 ASSERT (vec_len (test_data) > 0);
642
Florin Coras6792ec02017-03-13 03:49:51 -0700643 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
644 n_iterations = utm->bytes_to_send / vec_len (test_data);
645
646 for (i = 0; i < n_iterations; i++)
647 {
648 send_test_chunk (utm, tx_fifo, mypid, 0);
Florin Corasf6359c82017-06-19 12:26:09 -0400649 if (utm->time_to_stop)
650 break;
Florin Coras6792ec02017-03-13 03:49:51 -0700651 }
652
653 leftover = utm->bytes_to_send % vec_len (test_data);
654 if (leftover)
655 send_test_chunk (utm, tx_fifo, mypid, leftover);
Florin Corase04c2992017-03-01 08:17:34 -0800656
Florin Corasf03a59a2017-06-09 21:07:32 -0700657 if (!utm->drop_packets)
Florin Corase04c2992017-03-01 08:17:34 -0800658 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700659 f64 timeout = clib_time_now (&utm->clib_time) + 10;
Florin Corase04c2992017-03-01 08:17:34 -0800660
661 /* Wait for the outstanding packets */
Florin Coras6792ec02017-03-13 03:49:51 -0700662 while (utm->client_bytes_received <
663 vec_len (test_data) * n_iterations + leftover)
Florin Corase04c2992017-03-01 08:17:34 -0800664 {
665 if (clib_time_now (&utm->clib_time) > timeout)
666 {
667 clib_warning ("timed out waiting for the missing packets");
668 break;
669 }
670 }
Florin Corase04c2992017-03-01 08:17:34 -0800671 }
Florin Coras6792ec02017-03-13 03:49:51 -0700672 utm->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800673}
674
675void
Florin Corasa5464812017-04-19 13:00:05 -0700676client_send_connect (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800677{
678 vl_api_connect_uri_t *cmp;
679 cmp = vl_msg_api_alloc (sizeof (*cmp));
680 memset (cmp, 0, sizeof (*cmp));
681
682 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
683 cmp->client_index = utm->my_client_index;
684 cmp->context = ntohl (0xfeedface);
685 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
686 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
687}
688
Florin Corasa5464812017-04-19 13:00:05 -0700689int
690client_connect (uri_tcp_test_main_t * utm)
691{
692 client_send_connect (utm);
693 if (wait_for_state_change (utm, STATE_READY))
694 {
695 clib_warning ("Connect failed");
696 return -1;
697 }
698 return 0;
699}
700
Florin Corase04c2992017-03-01 08:17:34 -0800701void
Florin Corasa5464812017-04-19 13:00:05 -0700702client_send_disconnect (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800703{
704 session_t *connected_session;
705 vl_api_disconnect_session_t *dmp;
706 connected_session = pool_elt_at_index (utm->sessions,
707 utm->connected_session_index);
708 dmp = vl_msg_api_alloc (sizeof (*dmp));
709 memset (dmp, 0, sizeof (*dmp));
710 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
711 dmp->client_index = utm->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700712 dmp->handle = connected_session->vpp_session_handle;
Florin Corase04c2992017-03-01 08:17:34 -0800713 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
714}
715
Florin Corasa5464812017-04-19 13:00:05 -0700716int
717client_disconnect (uri_tcp_test_main_t * utm)
718{
719 client_send_disconnect (utm);
Florin Corasf03a59a2017-06-09 21:07:32 -0700720 clib_warning ("Sent disconnect");
Florin Corasa5464812017-04-19 13:00:05 -0700721 if (wait_for_state_change (utm, STATE_START))
722 {
723 clib_warning ("Disconnect failed");
724 return -1;
725 }
726 return 0;
727}
728
Florin Corase04c2992017-03-01 08:17:34 -0800729static void
730client_test (uri_tcp_test_main_t * utm)
731{
732 int i;
733
Florin Corasa5464812017-04-19 13:00:05 -0700734 if (application_attach (utm))
735 return;
Florin Corase04c2992017-03-01 08:17:34 -0800736
Florin Corasa5464812017-04-19 13:00:05 -0700737 if (client_connect (utm))
Florin Corase04c2992017-03-01 08:17:34 -0800738 {
Florin Corasa5464812017-04-19 13:00:05 -0700739 application_detach (utm);
Florin Corase04c2992017-03-01 08:17:34 -0800740 return;
741 }
742
743 /* Init test data */
Florin Corasf03a59a2017-06-09 21:07:32 -0700744 vec_validate (utm->connect_test_data, 128 * 1024 - 1);
Florin Corase04c2992017-03-01 08:17:34 -0800745 for (i = 0; i < vec_len (utm->connect_test_data); i++)
746 utm->connect_test_data[i] = i & 0xff;
747
748 /* Start send */
749 client_send_data (utm);
750
751 /* Disconnect */
752 client_disconnect (utm);
Florin Coras6792ec02017-03-13 03:49:51 -0700753
Florin Coras6cf30ad2017-04-04 23:08:23 -0700754 application_detach (utm);
Florin Corase04c2992017-03-01 08:17:34 -0800755}
756
757static void
758vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
759{
760 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800761
762 if (mp->retval)
763 {
flyingeagle23a07779f2017-04-26 20:22:04 +0800764 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -0700765 clib_net_to_host_u32 (mp->retval));
Florin Corase04c2992017-03-01 08:17:34 -0800766 utm->state = STATE_FAILED;
767 return;
768 }
769
Dave Barach68b0fb02017-02-28 15:15:56 -0500770 utm->state = STATE_READY;
771}
772
Dave Barach68b0fb02017-02-28 15:15:56 -0500773static void
Florin Corase04c2992017-03-01 08:17:34 -0800774vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500775{
776 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
777
778 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -0800779 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500780
781 utm->state = STATE_START;
782}
783
Florin Coras6cf30ad2017-04-04 23:08:23 -0700784u8 *
785format_ip4_address (u8 * s, va_list * args)
786{
787 u8 *a = va_arg (*args, u8 *);
788 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
789}
790
791u8 *
792format_ip6_address (u8 * s, va_list * args)
793{
794 ip6_address_t *a = va_arg (*args, ip6_address_t *);
795 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
796
797 i_max_n_zero = ARRAY_LEN (a->as_u16);
798 max_n_zeros = 0;
799 i_first_zero = i_max_n_zero;
800 n_zeros = 0;
801 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
802 {
803 u32 is_zero = a->as_u16[i] == 0;
804 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
805 {
806 i_first_zero = i;
807 n_zeros = 0;
808 }
809 n_zeros += is_zero;
810 if ((!is_zero && n_zeros > max_n_zeros)
811 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
812 {
813 i_max_n_zero = i_first_zero;
814 max_n_zeros = n_zeros;
815 i_first_zero = ARRAY_LEN (a->as_u16);
816 n_zeros = 0;
817 }
818 }
819
820 last_double_colon = 0;
821 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
822 {
823 if (i == i_max_n_zero && max_n_zeros > 1)
824 {
825 s = format (s, "::");
826 i += max_n_zeros - 1;
827 last_double_colon = 1;
828 }
829 else
830 {
831 s = format (s, "%s%x",
832 (last_double_colon || i == 0) ? "" : ":",
833 clib_net_to_host_u16 (a->as_u16[i]));
834 last_double_colon = 0;
835 }
836 }
837
838 return s;
839}
840
841/* Format an IP46 address. */
842u8 *
843format_ip46_address (u8 * s, va_list * args)
844{
845 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
846 ip46_type_t type = va_arg (*args, ip46_type_t);
847 int is_ip4 = 1;
848
849 switch (type)
850 {
851 case IP46_TYPE_ANY:
852 is_ip4 = ip46_address_is_ip4 (ip46);
853 break;
854 case IP46_TYPE_IP4:
855 is_ip4 = 1;
856 break;
857 case IP46_TYPE_IP6:
858 is_ip4 = 0;
859 break;
860 }
861
862 return is_ip4 ?
863 format (s, "%U", format_ip4_address, &ip46->ip4) :
864 format (s, "%U", format_ip6_address, &ip46->ip6);
865}
866
Dave Barach68b0fb02017-02-28 15:15:56 -0500867static void
868vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
869{
870 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
871 vl_api_accept_session_reply_t *rmp;
Florin Corase04c2992017-03-01 08:17:34 -0800872 svm_fifo_t *rx_fifo, *tx_fifo;
873 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500874 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500875 u32 session_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700876 u8 *ip_str;
Dave Barach68b0fb02017-02-28 15:15:56 -0500877
878 if (start_time == 0.0)
Florin Corase04c2992017-03-01 08:17:34 -0800879 start_time = clib_time_now (&utm->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500880
Florin Coras6cf30ad2017-04-04 23:08:23 -0700881 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
882 clib_warning ("Accepted session from: %s:%d", ip_str,
883 clib_net_to_host_u16 (mp->port));
Damjan Marion7bee80c2017-04-26 15:32:12 +0200884 utm->vpp_event_queue =
885 uword_to_pointer (mp->vpp_event_queue_address,
886 unix_shared_memory_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500887
888 /* Allocate local session and set it up */
889 pool_get (utm->sessions, session);
890 session_index = session - utm->sessions;
891
Damjan Marion7bee80c2017-04-26 15:32:12 +0200892 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500893 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200894 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500895 tx_fifo->client_session_index = session_index;
896
897 session->server_rx_fifo = rx_fifo;
898 session->server_tx_fifo = tx_fifo;
899
900 /* Add it to lookup table */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700901 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500902
903 utm->state = STATE_READY;
904
905 /* Stats printing */
Florin Corase04c2992017-03-01 08:17:34 -0800906 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500907 {
908 f64 now = clib_time_now (&utm->clib_time);
909 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
Florin Corase04c2992017-03-01 08:17:34 -0800910 pool_elts (utm->sessions), now - start_time,
911 (f64) pool_elts (utm->sessions) / (now - start_time));
Dave Barach68b0fb02017-02-28 15:15:56 -0500912 }
913
Florin Corase04c2992017-03-01 08:17:34 -0800914 /*
915 * Send accept reply to vpp
916 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500917 rmp = vl_msg_api_alloc (sizeof (*rmp));
918 memset (rmp, 0, sizeof (*rmp));
919 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700920 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800921 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -0700922
923 session->bytes_received = 0;
924 session->start = clib_time_now (&utm->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500925}
926
927void
Florin Corase04c2992017-03-01 08:17:34 -0800928server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
929 session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -0500930{
Florin Corase04c2992017-03-01 08:17:34 -0800931 svm_fifo_t *rx_fifo, *tx_fifo;
932 int n_read;
Florin Corase04c2992017-03-01 08:17:34 -0800933 session_fifo_event_t evt;
934 unix_shared_memory_queue_t *q;
Florin Corasf03a59a2017-06-09 21:07:32 -0700935 session_t *session;
936 int rv;
937 u32 max_dequeue, offset, max_transfer, rx_buf_len;
Florin Corase04c2992017-03-01 08:17:34 -0800938
Florin Corasf03a59a2017-06-09 21:07:32 -0700939 rx_buf_len = vec_len (utm->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800940 rx_fifo = e->fifo;
Florin Corasf03a59a2017-06-09 21:07:32 -0700941 session = &utm->sessions[rx_fifo->client_session_index];
942 tx_fifo = session->server_tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -0800943
Florin Corasf03a59a2017-06-09 21:07:32 -0700944 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700945 /* Allow enqueuing of a new event */
946 svm_fifo_unset_event (rx_fifo);
947
Florin Corasf03a59a2017-06-09 21:07:32 -0700948 if (PREDICT_FALSE (max_dequeue == 0))
949 {
950 return;
951 }
Florin Coras6792ec02017-03-13 03:49:51 -0700952
Florin Corasf03a59a2017-06-09 21:07:32 -0700953 /* Read the max_dequeue */
Florin Corase04c2992017-03-01 08:17:34 -0800954 do
955 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700956 max_transfer = clib_min (rx_buf_len, max_dequeue);
957 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, utm->rx_buf);
Florin Coras6792ec02017-03-13 03:49:51 -0700958 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800959 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700960 max_dequeue -= n_read;
961 session->bytes_received += n_read;
962 }
963
964 /* Reflect if a non-drop session */
965 if (!utm->drop_packets && n_read > 0)
966 {
967 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800968 do
969 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700970 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
971 &utm->rx_buf[offset]);
972 if (rv > 0)
973 {
974 n_read -= rv;
975 offset += rv;
976 }
Florin Corase04c2992017-03-01 08:17:34 -0800977 }
Florin Corasf03a59a2017-06-09 21:07:32 -0700978 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -0800979
Florin Coras6792ec02017-03-13 03:49:51 -0700980 /* If event wasn't set, add one */
981 if (svm_fifo_set_event (tx_fifo))
982 {
983 /* Fabricate TX event, send to vpp */
984 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700985 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -0700986 evt.event_id = e->event_id;
987
988 q = utm->vpp_event_queue;
989 unix_shared_memory_queue_add (q, (u8 *) & evt,
Florin Corasf03a59a2017-06-09 21:07:32 -0700990 1 /* do wait for mutex */ );
Florin Coras6792ec02017-03-13 03:49:51 -0700991 }
Florin Corase04c2992017-03-01 08:17:34 -0800992 }
Florin Corase04c2992017-03-01 08:17:34 -0800993 }
Florin Corasf03a59a2017-06-09 21:07:32 -0700994 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -0800995}
996
997void
998server_handle_event_queue (uri_tcp_test_main_t * utm)
999{
1000 session_fifo_event_t _e, *e = &_e;;
1001
1002 while (1)
1003 {
1004 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
1005 0 /* nowait */ );
1006 switch (e->event_type)
1007 {
Florin Corasa5464812017-04-19 13:00:05 -07001008 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -08001009 server_handle_fifo_event_rx (utm, e);
1010 break;
1011
Florin Corasa5464812017-04-19 13:00:05 -07001012 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -08001013 return;
1014
1015 default:
1016 clib_warning ("unknown event type %d", e->event_type);
1017 break;
1018 }
1019 if (PREDICT_FALSE (utm->time_to_stop == 1))
1020 break;
1021 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1022 {
1023 utm->time_to_print_stats = 0;
1024 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1025 }
1026 }
1027}
1028
1029void
Florin Corasa5464812017-04-19 13:00:05 -07001030server_send_listen (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -08001031{
1032 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001033 bmp = vl_msg_api_alloc (sizeof (*bmp));
1034 memset (bmp, 0, sizeof (*bmp));
1035
1036 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1037 bmp->client_index = utm->my_client_index;
1038 bmp->context = ntohl (0xfeedface);
Florin Corase04c2992017-03-01 08:17:34 -08001039 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1040 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1041}
1042
Florin Corasa5464812017-04-19 13:00:05 -07001043int
1044server_listen (uri_tcp_test_main_t * utm)
1045{
1046 server_send_listen (utm);
1047 if (wait_for_state_change (utm, STATE_READY))
1048 {
1049 clib_warning ("timeout waiting for STATE_READY");
1050 return -1;
1051 }
1052 return 0;
1053}
1054
Florin Corase04c2992017-03-01 08:17:34 -08001055void
Florin Corasa5464812017-04-19 13:00:05 -07001056server_send_unbind (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -08001057{
1058 vl_api_unbind_uri_t *ump;
1059
1060 ump = vl_msg_api_alloc (sizeof (*ump));
1061 memset (ump, 0, sizeof (*ump));
1062
1063 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1064 ump->client_index = utm->my_client_index;
1065 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1066 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1067}
1068
Florin Corasa5464812017-04-19 13:00:05 -07001069int
1070server_unbind (uri_tcp_test_main_t * utm)
1071{
1072 server_send_unbind (utm);
1073 if (wait_for_state_change (utm, STATE_START))
1074 {
1075 clib_warning ("timeout waiting for STATE_START");
1076 return -1;
1077 }
1078 return 0;
1079}
1080
Florin Corase04c2992017-03-01 08:17:34 -08001081void
1082server_test (uri_tcp_test_main_t * utm)
1083{
Florin Corasa5464812017-04-19 13:00:05 -07001084 if (application_attach (utm))
1085 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001086
Dave Barach68b0fb02017-02-28 15:15:56 -05001087 /* Bind to uri */
Florin Corasa5464812017-04-19 13:00:05 -07001088 if (server_listen (utm))
1089 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001090
1091 /* Enter handle event loop */
Florin Corase04c2992017-03-01 08:17:34 -08001092 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001093
1094 /* Cleanup */
Florin Corasa5464812017-04-19 13:00:05 -07001095 server_send_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001096
Florin Coras6cf30ad2017-04-04 23:08:23 -07001097 application_detach (utm);
1098
Dave Barach68b0fb02017-02-28 15:15:56 -05001099 fformat (stdout, "Test complete...\n");
1100}
1101
Florin Corase69f4952017-03-07 10:06:24 -08001102static void
1103vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1104 mp)
1105{
Florin Coras6792ec02017-03-13 03:49:51 -07001106 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corasf03a59a2017-06-09 21:07:32 -07001107 session_t *session;
Florin Coras6792ec02017-03-13 03:49:51 -07001108
Florin Corasf03a59a2017-06-09 21:07:32 -07001109 if (mp->retval)
1110 {
1111 clib_warning ("vpp complained about disconnect: %d",
1112 ntohl (mp->retval));
1113 }
1114
Florin Coras6792ec02017-03-13 03:49:51 -07001115 utm->state = STATE_START;
Florin Corasf03a59a2017-06-09 21:07:32 -07001116 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
1117 if (session)
1118 session_print_stats (utm, session);
Florin Corase69f4952017-03-07 10:06:24 -08001119}
1120
1121#define foreach_uri_msg \
1122_(BIND_URI_REPLY, bind_uri_reply) \
1123_(UNBIND_URI_REPLY, unbind_uri_reply) \
1124_(ACCEPT_SESSION, accept_session) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001125_(CONNECT_SESSION_REPLY, connect_session_reply) \
Florin Corase69f4952017-03-07 10:06:24 -08001126_(DISCONNECT_SESSION, disconnect_session) \
1127_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1128_(RESET_SESSION, reset_session) \
Florin Coras6cf30ad2017-04-04 23:08:23 -07001129_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1130_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1131_(MAP_ANOTHER_SEGMENT, map_another_segment) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001132
1133void
1134uri_api_hookup (uri_tcp_test_main_t * utm)
1135{
1136#define _(N,n) \
1137 vl_msg_api_set_handlers(VL_API_##N, #n, \
1138 vl_api_##n##_t_handler, \
1139 vl_noop_handler, \
1140 vl_api_##n##_t_endian, \
1141 vl_api_##n##_t_print, \
1142 sizeof(vl_api_##n##_t), 1);
1143 foreach_uri_msg;
1144#undef _
1145}
1146
1147int
1148main (int argc, char **argv)
1149{
1150 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1151 unformat_input_t _argv, *a = &_argv;
1152 u8 *chroot_prefix;
Florin Corase69f4952017-03-07 10:06:24 -08001153 u8 *heap, *uri = 0;
1154 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1155 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001156 u64 bytes_to_send = 64 << 10, mbytes;
Dave Barach68b0fb02017-02-28 15:15:56 -05001157 u32 tmp;
1158 mheap_t *h;
Florin Corase04c2992017-03-01 08:17:34 -08001159 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -05001160 int i;
Florin Corase04c2992017-03-01 08:17:34 -08001161 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001162
1163 clib_mem_init (0, 256 << 20);
1164
1165 heap = clib_mem_get_per_cpu_heap ();
1166 h = mheap_header (heap);
1167
1168 /* make the main heap thread-safe */
1169 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1170
Florin Corasf03a59a2017-06-09 21:07:32 -07001171 vec_validate (utm->rx_buf, 128 << 10);
Dave Barach68b0fb02017-02-28 15:15:56 -05001172
Florin Corase04c2992017-03-01 08:17:34 -08001173 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Dave Barach68b0fb02017-02-28 15:15:56 -05001174
Florin Corase04c2992017-03-01 08:17:34 -08001175 utm->my_pid = getpid ();
1176 utm->configured_segment_size = 1 << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001177
1178 clib_time_init (&utm->clib_time);
1179 init_error_string_table (utm);
Florin Corase04c2992017-03-01 08:17:34 -08001180 svm_fifo_segment_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001181 unformat_init_command_line (a, argv);
1182
1183 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1184 {
1185 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001186 {
1187 vl_set_memory_root_path ((char *) chroot_prefix);
1188 }
Florin Corase69f4952017-03-07 10:06:24 -08001189 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001190 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001191 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corase04c2992017-03-01 08:17:34 -08001192 utm->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001193 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corase04c2992017-03-01 08:17:34 -08001194 utm->configured_segment_size = tmp << 30;
Dave Barach68b0fb02017-02-28 15:15:56 -05001195 else if (unformat (a, "master"))
Florin Corase04c2992017-03-01 08:17:34 -08001196 i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001197 else if (unformat (a, "slave"))
Florin Corase04c2992017-03-01 08:17:34 -08001198 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001199 else if (unformat (a, "drop"))
Florin Corase04c2992017-03-01 08:17:34 -08001200 drop_packets = 1;
1201 else if (unformat (a, "test"))
1202 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001203 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001204 {
1205 bytes_to_send = mbytes << 20;
1206 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001207 else if (unformat (a, "gbytes %lld", &mbytes))
1208 {
1209 bytes_to_send = mbytes << 30;
1210 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001211 else
Florin Corase04c2992017-03-01 08:17:34 -08001212 {
1213 fformat (stderr, "%s: usage [master|slave]\n");
1214 exit (1);
1215 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001216 }
1217
Florin Corase69f4952017-03-07 10:06:24 -08001218 if (uri)
1219 {
1220 utm->uri = format (0, "%s%c", uri, 0);
1221 utm->connect_uri = format (0, "%s%c", uri, 0);
1222 }
1223 else
1224 {
1225 utm->uri = format (0, "%s%c", bind_uri, 0);
1226 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1227 }
1228
Dave Barach68b0fb02017-02-28 15:15:56 -05001229 utm->i_am_master = i_am_master;
1230 utm->segment_main = &svm_fifo_segment_main;
1231 utm->drop_packets = drop_packets;
Florin Corase04c2992017-03-01 08:17:34 -08001232 utm->test_return_packets = test_return_packets;
Florin Coras6792ec02017-03-13 03:49:51 -07001233 utm->bytes_to_send = bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -07001234 utm->time_to_stop = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001235
Florin Corase04c2992017-03-01 08:17:34 -08001236 setup_signal_handlers ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001237 uri_api_hookup (utm);
1238
Florin Corase04c2992017-03-01 08:17:34 -08001239 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001240 {
1241 svm_region_exit ();
1242 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1243 exit (1);
1244 }
1245
1246 if (i_am_master == 0)
1247 {
Florin Corase04c2992017-03-01 08:17:34 -08001248 client_test (utm);
Florin Corase69f4952017-03-07 10:06:24 -08001249 vl_client_disconnect_from_vlib ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001250 exit (0);
1251 }
1252
1253 /* $$$$ hack preallocation */
1254 for (i = 0; i < 200000; i++)
1255 {
1256 pool_get (utm->sessions, session);
1257 memset (session, 0, sizeof (*session));
1258 }
1259 for (i = 0; i < 200000; i++)
1260 pool_put_index (utm->sessions, i);
1261
Florin Corase04c2992017-03-01 08:17:34 -08001262 server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001263
1264 vl_client_disconnect_from_vlib ();
1265 exit (0);
1266}
Florin Corase04c2992017-03-01 08:17:34 -08001267
1268/*
1269 * fd.io coding-style-patch-verification: ON
1270 *
1271 * Local Variables:
1272 * eval: (c-set-style "gnu")
1273 * End:
1274 */