blob: 686c93f9bda1e2228c4853f47a0aadadbdea3fc2 [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;
Dave Barach68b0fb02017-02-28 15:15:56 -050049} session_t;
50
51typedef enum
52{
53 STATE_START,
Florin Corasa5464812017-04-19 13:00:05 -070054 STATE_ATTACHED,
Dave Barach68b0fb02017-02-28 15:15:56 -050055 STATE_READY,
56 STATE_DISCONNECTING,
57 STATE_FAILED
58} connection_state_t;
59
60typedef struct
61{
62 /* vpe input queue */
63 unix_shared_memory_queue_t *vl_input_queue;
64
65 /* API client handle */
66 u32 my_client_index;
67
68 /* The URI we're playing with */
Florin Corase04c2992017-03-01 08:17:34 -080069 u8 *uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050070
71 /* Session pool */
Florin Corase04c2992017-03-01 08:17:34 -080072 session_t *sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -050073
74 /* Hash table for disconnect processing */
Florin Corase04c2992017-03-01 08:17:34 -080075 uword *session_index_by_vpp_handles;
Dave Barach68b0fb02017-02-28 15:15:56 -050076
77 /* intermediate rx buffer */
Florin Corase04c2992017-03-01 08:17:34 -080078 u8 *rx_buf;
Dave Barach68b0fb02017-02-28 15:15:56 -050079
80 /* URI for slave's connect */
Florin Corase04c2992017-03-01 08:17:34 -080081 u8 *connect_uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050082
83 u32 connected_session_index;
84
85 int i_am_master;
86
87 /* drop all packets */
88 int drop_packets;
89
90 /* Our event queue */
Florin Corase04c2992017-03-01 08:17:34 -080091 unix_shared_memory_queue_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050092
93 /* $$$ single thread only for the moment */
Florin Corase04c2992017-03-01 08:17:34 -080094 unix_shared_memory_queue_t *vpp_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050095
96 pid_t my_pid;
97
98 /* For deadman timers */
99 clib_time_t clib_time;
100
101 /* State of the connection, shared between msg RX thread and main thread */
102 volatile connection_state_t state;
103
104 /* Signal variables */
105 volatile int time_to_stop;
106 volatile int time_to_print_stats;
107
108 u32 configured_segment_size;
109
110 /* VNET_API_ERROR_FOO -> "Foo" hash table */
Florin Corase04c2992017-03-01 08:17:34 -0800111 uword *error_string_by_error_number;
Dave Barach68b0fb02017-02-28 15:15:56 -0500112
113 u8 *connect_test_data;
Florin Corase04c2992017-03-01 08:17:34 -0800114 pthread_t client_rx_thread_handle;
115 u32 client_bytes_received;
116 u8 test_return_packets;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700117 u64 bytes_to_send;
Florin Corase04c2992017-03-01 08:17:34 -0800118
119 /* convenience */
120 svm_fifo_segment_main_t *segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500121} uri_tcp_test_main_t;
122
123uri_tcp_test_main_t uri_tcp_test_main;
124
125#if CLIB_DEBUG > 0
126#define NITER 10000
127#else
128#define NITER 4000000
129#endif
130
Florin Corasa5464812017-04-19 13:00:05 -0700131static u8 *
132format_api_error (u8 * s, va_list * args)
133{
134 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
135 i32 error = va_arg (*args, u32);
136 uword *p;
137
138 p = hash_get (utm->error_string_by_error_number, -error);
139
140 if (p)
141 s = format (s, "%s", p[0]);
142 else
143 s = format (s, "%d", error);
144 return s;
145}
146
147static void
148init_error_string_table (uri_tcp_test_main_t * utm)
149{
150 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
151
152#define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
153 foreach_vnet_api_error;
154#undef _
155
156 hash_set (utm->error_string_by_error_number, 99, "Misc");
157}
158
Dave Barach68b0fb02017-02-28 15:15:56 -0500159int
160wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
161{
162#if CLIB_DEBUG > 0
163#define TIMEOUT 600.0
164#else
165#define TIMEOUT 600.0
166#endif
167
168 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
169
170 while (clib_time_now (&utm->clib_time) < timeout)
171 {
172 if (utm->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800173 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500174 if (utm->state == STATE_FAILED)
175 return -1;
176 }
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;
185 u32 fifo_size = 3 << 20;
186 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;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700194 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
195 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
196 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
197 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
198 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
199}
200
Florin Corasa5464812017-04-19 13:00:05 -0700201int
202application_attach (uri_tcp_test_main_t * utm)
203{
204 application_send_attach (utm);
205 if (wait_for_state_change (utm, STATE_ATTACHED))
206 {
207 clib_warning ("timeout waiting for STATE_ATTACHED");
208 return -1;
209 }
210 return 0;
211}
212
Florin Coras6cf30ad2017-04-04 23:08:23 -0700213void
214application_detach (uri_tcp_test_main_t * utm)
215{
216 vl_api_application_detach_t *bmp;
217 bmp = vl_msg_api_alloc (sizeof (*bmp));
218 memset (bmp, 0, sizeof (*bmp));
219
220 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
221 bmp->client_index = utm->my_client_index;
222 bmp->context = ntohl (0xfeedface);
223 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
224}
225
226static void
227vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
228 mp)
229{
230 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
231 svm_fifo_segment_create_args_t _a, *a = &_a;
232 int rv;
233
234 if (mp->retval)
235 {
Florin Corasa5464812017-04-19 13:00:05 -0700236 clib_warning ("attach failed: %U", format_api_error,
237 clib_net_to_host_u32 (mp->retval));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700238 utm->state = STATE_FAILED;
239 return;
240 }
241
242 if (mp->segment_name_length == 0)
243 {
244 clib_warning ("segment_name_length zero");
245 return;
246 }
247
248 a->segment_name = (char *) mp->segment_name;
249 a->segment_size = mp->segment_size;
250
251 ASSERT (mp->app_event_queue_address);
252
253 /* Attach to the segment vpp created */
254 rv = svm_fifo_segment_attach (a);
255 if (rv)
256 {
257 clib_warning ("svm_fifo_segment_attach ('%s') failed",
258 mp->segment_name);
259 return;
260 }
261
262 utm->our_event_queue =
263 (unix_shared_memory_queue_t *) mp->app_event_queue_address;
Florin Corasa5464812017-04-19 13:00:05 -0700264 utm->state = STATE_ATTACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700265}
266
267static void
268vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
269 mp)
270{
271 if (mp->retval)
272 clib_warning ("detach returned with err: %d", mp->retval);
273}
274
Dave Barach68b0fb02017-02-28 15:15:56 -0500275static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500276stop_signal (int signum)
277{
278 uri_tcp_test_main_t *um = &uri_tcp_test_main;
279
280 um->time_to_stop = 1;
281}
282
283static void
284stats_signal (int signum)
285{
286 uri_tcp_test_main_t *um = &uri_tcp_test_main;
287
288 um->time_to_print_stats = 1;
289}
290
291static clib_error_t *
292setup_signal_handlers (void)
293{
294 signal (SIGINT, stats_signal);
295 signal (SIGQUIT, stop_signal);
296 signal (SIGTERM, stop_signal);
297
298 return 0;
299}
300
301void
302vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
303{
304 clib_warning ("BUG");
305}
306
307int
308connect_to_vpp (char *name)
309{
310 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
311 api_main_t *am = &api_main;
312
313 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
314 return -1;
315
316 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
317 utm->my_client_index = am->my_client_index;
318
319 return 0;
320}
321
322static void
Florin Corase04c2992017-03-01 08:17:34 -0800323vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500324{
325 svm_fifo_segment_create_args_t _a, *a = &_a;
326 int rv;
327
328 a->segment_name = (char *) mp->segment_name;
329 a->segment_size = mp->segment_size;
330 /* Attach to the segment vpp created */
331 rv = svm_fifo_segment_attach (a);
332 if (rv)
333 {
334 clib_warning ("svm_fifo_segment_attach ('%s') failed",
Florin Corase04c2992017-03-01 08:17:34 -0800335 mp->segment_name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500336 return;
337 }
338 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
Florin Corase04c2992017-03-01 08:17:34 -0800339 mp->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500340}
341
342static void
343vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
344{
345 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800346 session_t *session;
347 vl_api_disconnect_session_reply_t *rmp;
348 uword *p;
Dave Barach68b0fb02017-02-28 15:15:56 -0500349 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500350
Florin Coras6cf30ad2017-04-04 23:08:23 -0700351 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800352
353 if (p)
354 {
355 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700356 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800357 pool_put (utm->sessions, session);
358 }
359 else
360 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700361 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800362 rv = -11;
363 }
364
365 utm->time_to_stop = 1;
366
367 rmp = vl_msg_api_alloc (sizeof (*rmp));
368 memset (rmp, 0, sizeof (*rmp));
369
370 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
371 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700372 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800373 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
374}
375
376static void
377vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
378{
379 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
380 session_t *session;
381 vl_api_reset_session_reply_t *rmp;
382 uword *p;
383 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800384
Florin Coras6cf30ad2017-04-04 23:08:23 -0700385 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500386
387 if (p)
388 {
389 session = pool_elt_at_index (utm->sessions, p[0]);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700390 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500391 pool_put (utm->sessions, session);
Florin Corasd79b41e2017-03-04 05:37:52 -0800392 utm->time_to_stop = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500393 }
394 else
395 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700396 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500397 rv = -11;
398 }
399
400 rmp = vl_msg_api_alloc (sizeof (*rmp));
401 memset (rmp, 0, sizeof (*rmp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800402 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500403 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700404 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800405 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500406}
407
408void
Florin Corase04c2992017-03-01 08:17:34 -0800409client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
410 session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -0500411{
Florin Corase04c2992017-03-01 08:17:34 -0800412 svm_fifo_t *rx_fifo;
413 int n_read, bytes, i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500414
415 rx_fifo = e->fifo;
416
Florin Coras6792ec02017-03-13 03:49:51 -0700417 bytes = svm_fifo_max_dequeue (rx_fifo);
418 /* Allow enqueuing of new event */
419 svm_fifo_unset_event (rx_fifo);
420
421 /* Read the bytes */
Dave Barach68b0fb02017-02-28 15:15:56 -0500422 do
423 {
Florin Corasa5464812017-04-19 13:00:05 -0700424 n_read = svm_fifo_dequeue_nowait (rx_fifo,
Florin Coras6792ec02017-03-13 03:49:51 -0700425 clib_min (vec_len (utm->rx_buf),
426 bytes), utm->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500427 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800428 {
429 bytes -= n_read;
430 for (i = 0; i < n_read; i++)
431 {
432 if (utm->rx_buf[i] != ((utm->client_bytes_received + i) & 0xff))
433 {
434 clib_warning ("error at byte %lld, 0x%x not 0x%x",
435 utm->client_bytes_received + i,
436 utm->rx_buf[i],
437 ((utm->client_bytes_received + i) & 0xff));
438 }
439 }
440 utm->client_bytes_received += n_read;
441 }
Florin Coras6792ec02017-03-13 03:49:51 -0700442 else
443 {
444 if (n_read == -2)
445 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700446// clib_warning ("weird!");
Florin Coras6792ec02017-03-13 03:49:51 -0700447 break;
448 }
449 }
Florin Corase04c2992017-03-01 08:17:34 -0800450
Dave Barach68b0fb02017-02-28 15:15:56 -0500451 }
Florin Coras6792ec02017-03-13 03:49:51 -0700452 while (bytes > 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500453}
454
455void
Florin Corase04c2992017-03-01 08:17:34 -0800456client_handle_event_queue (uri_tcp_test_main_t * utm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500457{
458 session_fifo_event_t _e, *e = &_e;;
459
Florin Corase04c2992017-03-01 08:17:34 -0800460 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
461 0 /* nowait */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500462 switch (e->event_type)
463 {
Florin Corasa5464812017-04-19 13:00:05 -0700464 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800465 client_handle_fifo_event_rx (utm, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500466 break;
467
Florin Corasa5464812017-04-19 13:00:05 -0700468 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500469 return;
470
471 default:
Florin Corase04c2992017-03-01 08:17:34 -0800472 clib_warning ("unknown event type %d", e->event_type);
Dave Barach68b0fb02017-02-28 15:15:56 -0500473 break;
474 }
475}
476
Florin Corase04c2992017-03-01 08:17:34 -0800477static void *
478client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500479{
Florin Corase04c2992017-03-01 08:17:34 -0800480 session_fifo_event_t _e, *e = &_e;
481 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500482
Florin Corase04c2992017-03-01 08:17:34 -0800483 utm->client_bytes_received = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500484 while (1)
485 {
Florin Corase04c2992017-03-01 08:17:34 -0800486 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
487 0 /* nowait */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500488 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800489 {
Florin Corasa5464812017-04-19 13:00:05 -0700490 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800491 client_handle_fifo_event_rx (utm, e);
492 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500493
Florin Corasa5464812017-04-19 13:00:05 -0700494 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -0800495 return 0;
496 default:
497 clib_warning ("unknown event type %d", e->event_type);
498 break;
499 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500500
Florin Corase04c2992017-03-01 08:17:34 -0800501 if (PREDICT_FALSE (utm->time_to_stop == 1))
502 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500503 }
Florin Corase04c2992017-03-01 08:17:34 -0800504 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500505}
506
Dave Barach68b0fb02017-02-28 15:15:56 -0500507
508static void
509vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
510{
511 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500512 session_t *session;
513 u32 session_index;
514 svm_fifo_t *rx_fifo, *tx_fifo;
515 int rv;
516
517 if (mp->retval)
518 {
Florin Corasa5464812017-04-19 13:00:05 -0700519 clib_warning ("connection failed with code: %U", format_api_error,
520 clib_net_to_host_u32 (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500521 utm->state = STATE_FAILED;
522 return;
523 }
Florin Corase04c2992017-03-01 08:17:34 -0800524
Dave Barach68b0fb02017-02-28 15:15:56 -0500525 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
526 mp->vpp_event_queue_address;
527
528 /*
529 * Setup session
530 */
531
532 pool_get (utm->sessions, session);
533 session_index = session - utm->sessions;
534
Florin Corase04c2992017-03-01 08:17:34 -0800535 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -0500536 rx_fifo->client_session_index = session_index;
Florin Corase04c2992017-03-01 08:17:34 -0800537 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -0500538 tx_fifo->client_session_index = session_index;
539
540 session->server_rx_fifo = rx_fifo;
541 session->server_tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700542 session->vpp_session_handle = mp->handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500543
544 /* Save handle */
545 utm->connected_session_index = session_index;
Florin Corase04c2992017-03-01 08:17:34 -0800546 utm->state = STATE_READY;
547
548 /* Add it to lookup table */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700549 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800550
551 /* Start RX thread */
552 rv = pthread_create (&utm->client_rx_thread_handle,
553 NULL /*attr */ , client_rx_thread_fn, 0);
554 if (rv)
555 {
556 clib_warning ("pthread_create returned %d", rv);
557 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
558 }
559}
560
Florin Coras6792ec02017-03-13 03:49:51 -0700561static void
562send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
563 u32 bytes)
Florin Corase04c2992017-03-01 08:17:34 -0800564{
565 u8 *test_data = utm->connect_test_data;
566 u64 bytes_sent = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700567 int test_buf_offset = 0;
568 u32 bytes_to_snd;
569 u32 queue_max_chunk = 64 << 10, actual_write;
Florin Corase04c2992017-03-01 08:17:34 -0800570 session_fifo_event_t evt;
571 static int serial_number = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700572 int rv;
Florin Corase04c2992017-03-01 08:17:34 -0800573
Florin Coras6792ec02017-03-13 03:49:51 -0700574 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
575 if (bytes_to_snd > vec_len (test_data))
576 bytes_to_snd = vec_len (test_data);
Florin Corase04c2992017-03-01 08:17:34 -0800577
Florin Coras6792ec02017-03-13 03:49:51 -0700578 while (bytes_to_snd > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800579 {
Florin Coras6792ec02017-03-13 03:49:51 -0700580 actual_write =
581 bytes_to_snd > queue_max_chunk ? queue_max_chunk : bytes_to_snd;
Florin Corasa5464812017-04-19 13:00:05 -0700582 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
Florin Coras6792ec02017-03-13 03:49:51 -0700583 test_data + test_buf_offset);
584
585 if (rv > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800586 {
Florin Coras6792ec02017-03-13 03:49:51 -0700587 bytes_to_snd -= rv;
588 test_buf_offset += rv;
589 bytes_sent += rv;
Florin Corase04c2992017-03-01 08:17:34 -0800590
Florin Coras6792ec02017-03-13 03:49:51 -0700591 if (svm_fifo_set_event (tx_fifo))
Florin Corase04c2992017-03-01 08:17:34 -0800592 {
Florin Corase04c2992017-03-01 08:17:34 -0800593 /* Fabricate TX event, send to vpp */
594 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700595 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase04c2992017-03-01 08:17:34 -0800596 evt.event_id = serial_number++;
597
598 unix_shared_memory_queue_add (utm->vpp_event_queue,
599 (u8 *) & evt,
600 0 /* do wait for mutex */ );
601 }
602 }
603 }
Florin Coras6792ec02017-03-13 03:49:51 -0700604}
605
606void
607client_send_data (uri_tcp_test_main_t * utm)
608{
609 u8 *test_data = utm->connect_test_data;
610 int mypid = getpid ();
611 session_t *session;
612 svm_fifo_t *tx_fifo;
613 u32 n_iterations, leftover;
614 int i;
615
616 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
617 tx_fifo = session->server_tx_fifo;
618
619 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
620 n_iterations = utm->bytes_to_send / vec_len (test_data);
621
622 for (i = 0; i < n_iterations; i++)
623 {
624 send_test_chunk (utm, tx_fifo, mypid, 0);
625 }
626
627 leftover = utm->bytes_to_send % vec_len (test_data);
628 if (leftover)
629 send_test_chunk (utm, tx_fifo, mypid, leftover);
Florin Corase04c2992017-03-01 08:17:34 -0800630
631 if (utm->test_return_packets)
632 {
633 f64 timeout = clib_time_now (&utm->clib_time) + 2;
634
635 /* Wait for the outstanding packets */
Florin Coras6792ec02017-03-13 03:49:51 -0700636 while (utm->client_bytes_received <
637 vec_len (test_data) * n_iterations + leftover)
Florin Corase04c2992017-03-01 08:17:34 -0800638 {
639 if (clib_time_now (&utm->clib_time) > timeout)
640 {
641 clib_warning ("timed out waiting for the missing packets");
642 break;
643 }
644 }
Florin Corase04c2992017-03-01 08:17:34 -0800645 }
Florin Coras6792ec02017-03-13 03:49:51 -0700646 utm->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800647}
648
649void
Florin Corasa5464812017-04-19 13:00:05 -0700650client_send_connect (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800651{
652 vl_api_connect_uri_t *cmp;
653 cmp = vl_msg_api_alloc (sizeof (*cmp));
654 memset (cmp, 0, sizeof (*cmp));
655
656 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
657 cmp->client_index = utm->my_client_index;
658 cmp->context = ntohl (0xfeedface);
659 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
660 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
661}
662
Florin Corasa5464812017-04-19 13:00:05 -0700663int
664client_connect (uri_tcp_test_main_t * utm)
665{
666 client_send_connect (utm);
667 if (wait_for_state_change (utm, STATE_READY))
668 {
669 clib_warning ("Connect failed");
670 return -1;
671 }
672 return 0;
673}
674
Florin Corase04c2992017-03-01 08:17:34 -0800675void
Florin Corasa5464812017-04-19 13:00:05 -0700676client_send_disconnect (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800677{
678 session_t *connected_session;
679 vl_api_disconnect_session_t *dmp;
680 connected_session = pool_elt_at_index (utm->sessions,
681 utm->connected_session_index);
682 dmp = vl_msg_api_alloc (sizeof (*dmp));
683 memset (dmp, 0, sizeof (*dmp));
684 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
685 dmp->client_index = utm->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700686 dmp->handle = connected_session->vpp_session_handle;
Florin Corase04c2992017-03-01 08:17:34 -0800687 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
688}
689
Florin Corasa5464812017-04-19 13:00:05 -0700690int
691client_disconnect (uri_tcp_test_main_t * utm)
692{
693 client_send_disconnect (utm);
694 if (wait_for_state_change (utm, STATE_START))
695 {
696 clib_warning ("Disconnect failed");
697 return -1;
698 }
699 return 0;
700}
701
Florin Corase04c2992017-03-01 08:17:34 -0800702static void
703client_test (uri_tcp_test_main_t * utm)
704{
705 int i;
706
Florin Corasa5464812017-04-19 13:00:05 -0700707 if (application_attach (utm))
708 return;
Florin Corase04c2992017-03-01 08:17:34 -0800709
Florin Corasa5464812017-04-19 13:00:05 -0700710 if (client_connect (utm))
Florin Corase04c2992017-03-01 08:17:34 -0800711 {
Florin Corasa5464812017-04-19 13:00:05 -0700712 application_detach (utm);
Florin Corase04c2992017-03-01 08:17:34 -0800713 return;
714 }
715
716 /* Init test data */
717 vec_validate (utm->connect_test_data, 64 * 1024 - 1);
718 for (i = 0; i < vec_len (utm->connect_test_data); i++)
719 utm->connect_test_data[i] = i & 0xff;
720
721 /* Start send */
722 client_send_data (utm);
723
724 /* Disconnect */
725 client_disconnect (utm);
Florin Coras6792ec02017-03-13 03:49:51 -0700726
Florin Coras6cf30ad2017-04-04 23:08:23 -0700727 application_detach (utm);
Florin Corase04c2992017-03-01 08:17:34 -0800728}
729
730static void
731vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
732{
733 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
Florin Corase04c2992017-03-01 08:17:34 -0800734
735 if (mp->retval)
736 {
Florin Corasa5464812017-04-19 13:00:05 -0700737 clib_warning ("bind failed: %s", format_api_error,
738 clib_net_to_host_u32 (mp->retval));
Florin Corase04c2992017-03-01 08:17:34 -0800739 utm->state = STATE_FAILED;
740 return;
741 }
742
Dave Barach68b0fb02017-02-28 15:15:56 -0500743 utm->state = STATE_READY;
744}
745
Dave Barach68b0fb02017-02-28 15:15:56 -0500746static void
Florin Corase04c2992017-03-01 08:17:34 -0800747vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500748{
749 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
750
751 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -0800752 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500753
754 utm->state = STATE_START;
755}
756
Florin Coras6cf30ad2017-04-04 23:08:23 -0700757u8 *
758format_ip4_address (u8 * s, va_list * args)
759{
760 u8 *a = va_arg (*args, u8 *);
761 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
762}
763
764u8 *
765format_ip6_address (u8 * s, va_list * args)
766{
767 ip6_address_t *a = va_arg (*args, ip6_address_t *);
768 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
769
770 i_max_n_zero = ARRAY_LEN (a->as_u16);
771 max_n_zeros = 0;
772 i_first_zero = i_max_n_zero;
773 n_zeros = 0;
774 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
775 {
776 u32 is_zero = a->as_u16[i] == 0;
777 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
778 {
779 i_first_zero = i;
780 n_zeros = 0;
781 }
782 n_zeros += is_zero;
783 if ((!is_zero && n_zeros > max_n_zeros)
784 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
785 {
786 i_max_n_zero = i_first_zero;
787 max_n_zeros = n_zeros;
788 i_first_zero = ARRAY_LEN (a->as_u16);
789 n_zeros = 0;
790 }
791 }
792
793 last_double_colon = 0;
794 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
795 {
796 if (i == i_max_n_zero && max_n_zeros > 1)
797 {
798 s = format (s, "::");
799 i += max_n_zeros - 1;
800 last_double_colon = 1;
801 }
802 else
803 {
804 s = format (s, "%s%x",
805 (last_double_colon || i == 0) ? "" : ":",
806 clib_net_to_host_u16 (a->as_u16[i]));
807 last_double_colon = 0;
808 }
809 }
810
811 return s;
812}
813
814/* Format an IP46 address. */
815u8 *
816format_ip46_address (u8 * s, va_list * args)
817{
818 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
819 ip46_type_t type = va_arg (*args, ip46_type_t);
820 int is_ip4 = 1;
821
822 switch (type)
823 {
824 case IP46_TYPE_ANY:
825 is_ip4 = ip46_address_is_ip4 (ip46);
826 break;
827 case IP46_TYPE_IP4:
828 is_ip4 = 1;
829 break;
830 case IP46_TYPE_IP6:
831 is_ip4 = 0;
832 break;
833 }
834
835 return is_ip4 ?
836 format (s, "%U", format_ip4_address, &ip46->ip4) :
837 format (s, "%U", format_ip6_address, &ip46->ip6);
838}
839
Dave Barach68b0fb02017-02-28 15:15:56 -0500840static void
841vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
842{
843 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
844 vl_api_accept_session_reply_t *rmp;
Florin Corase04c2992017-03-01 08:17:34 -0800845 svm_fifo_t *rx_fifo, *tx_fifo;
846 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500847 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500848 u32 session_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700849 u8 *ip_str;
Dave Barach68b0fb02017-02-28 15:15:56 -0500850
851 if (start_time == 0.0)
Florin Corase04c2992017-03-01 08:17:34 -0800852 start_time = clib_time_now (&utm->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500853
Florin Coras6cf30ad2017-04-04 23:08:23 -0700854 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
855 clib_warning ("Accepted session from: %s:%d", ip_str,
856 clib_net_to_host_u16 (mp->port));
Dave Barach68b0fb02017-02-28 15:15:56 -0500857 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
858 mp->vpp_event_queue_address;
859
860 /* Allocate local session and set it up */
861 pool_get (utm->sessions, session);
862 session_index = session - utm->sessions;
863
Florin Corase04c2992017-03-01 08:17:34 -0800864 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -0500865 rx_fifo->client_session_index = session_index;
Florin Corase04c2992017-03-01 08:17:34 -0800866 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -0500867 tx_fifo->client_session_index = session_index;
868
869 session->server_rx_fifo = rx_fifo;
870 session->server_tx_fifo = tx_fifo;
871
872 /* Add it to lookup table */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700873 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500874
875 utm->state = STATE_READY;
876
877 /* Stats printing */
Florin Corase04c2992017-03-01 08:17:34 -0800878 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500879 {
880 f64 now = clib_time_now (&utm->clib_time);
881 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
Florin Corase04c2992017-03-01 08:17:34 -0800882 pool_elts (utm->sessions), now - start_time,
883 (f64) pool_elts (utm->sessions) / (now - start_time));
Dave Barach68b0fb02017-02-28 15:15:56 -0500884 }
885
Florin Corase04c2992017-03-01 08:17:34 -0800886 /*
887 * Send accept reply to vpp
888 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500889 rmp = vl_msg_api_alloc (sizeof (*rmp));
890 memset (rmp, 0, sizeof (*rmp));
891 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700892 rmp->handle = mp->handle;
Florin Corase04c2992017-03-01 08:17:34 -0800893 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500894}
895
896void
Florin Corase04c2992017-03-01 08:17:34 -0800897server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
898 session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -0500899{
Florin Corase04c2992017-03-01 08:17:34 -0800900 svm_fifo_t *rx_fifo, *tx_fifo;
901 int n_read;
Florin Corase04c2992017-03-01 08:17:34 -0800902 session_fifo_event_t evt;
903 unix_shared_memory_queue_t *q;
904 int rv, bytes;
905
906 rx_fifo = e->fifo;
907 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
908
Florin Coras6792ec02017-03-13 03:49:51 -0700909 bytes = svm_fifo_max_dequeue (rx_fifo);
910 /* Allow enqueuing of a new event */
911 svm_fifo_unset_event (rx_fifo);
912
913 if (bytes == 0)
914 return;
915
916 /* Read the bytes */
Florin Corase04c2992017-03-01 08:17:34 -0800917 do
918 {
Florin Corasa5464812017-04-19 13:00:05 -0700919 n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
Florin Corase04c2992017-03-01 08:17:34 -0800920 utm->rx_buf);
Florin Coras6792ec02017-03-13 03:49:51 -0700921 if (n_read > 0)
922 bytes -= n_read;
923
924 if (utm->drop_packets)
925 continue;
Florin Corase04c2992017-03-01 08:17:34 -0800926
927 /* Reflect if a non-drop session */
Florin Coras6792ec02017-03-13 03:49:51 -0700928 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800929 {
930 do
931 {
Florin Corasa5464812017-04-19 13:00:05 -0700932 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read, utm->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800933 }
Florin Coras6792ec02017-03-13 03:49:51 -0700934 while (rv <= 0 && !utm->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -0800935
Florin Coras6792ec02017-03-13 03:49:51 -0700936 /* If event wasn't set, add one */
937 if (svm_fifo_set_event (tx_fifo))
938 {
939 /* Fabricate TX event, send to vpp */
940 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700941 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -0700942 evt.event_id = e->event_id;
943
944 q = utm->vpp_event_queue;
945 unix_shared_memory_queue_add (q, (u8 *) & evt,
946 0 /* do wait for mutex */ );
947 }
Florin Corase04c2992017-03-01 08:17:34 -0800948 }
Florin Corase04c2992017-03-01 08:17:34 -0800949 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800950 while ((n_read < 0 || bytes > 0) && !utm->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -0800951}
952
953void
954server_handle_event_queue (uri_tcp_test_main_t * utm)
955{
956 session_fifo_event_t _e, *e = &_e;;
957
958 while (1)
959 {
960 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
961 0 /* nowait */ );
962 switch (e->event_type)
963 {
Florin Corasa5464812017-04-19 13:00:05 -0700964 case FIFO_EVENT_APP_RX:
Florin Corase04c2992017-03-01 08:17:34 -0800965 server_handle_fifo_event_rx (utm, e);
966 break;
967
Florin Corasa5464812017-04-19 13:00:05 -0700968 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -0800969 return;
970
971 default:
972 clib_warning ("unknown event type %d", e->event_type);
973 break;
974 }
975 if (PREDICT_FALSE (utm->time_to_stop == 1))
976 break;
977 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
978 {
979 utm->time_to_print_stats = 0;
980 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
981 }
982 }
983}
984
985void
Florin Corasa5464812017-04-19 13:00:05 -0700986server_send_listen (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -0800987{
988 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -0800989 bmp = vl_msg_api_alloc (sizeof (*bmp));
990 memset (bmp, 0, sizeof (*bmp));
991
992 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
993 bmp->client_index = utm->my_client_index;
994 bmp->context = ntohl (0xfeedface);
Florin Corase04c2992017-03-01 08:17:34 -0800995 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
996 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
997}
998
Florin Corasa5464812017-04-19 13:00:05 -0700999int
1000server_listen (uri_tcp_test_main_t * utm)
1001{
1002 server_send_listen (utm);
1003 if (wait_for_state_change (utm, STATE_READY))
1004 {
1005 clib_warning ("timeout waiting for STATE_READY");
1006 return -1;
1007 }
1008 return 0;
1009}
1010
Florin Corase04c2992017-03-01 08:17:34 -08001011void
Florin Corasa5464812017-04-19 13:00:05 -07001012server_send_unbind (uri_tcp_test_main_t * utm)
Florin Corase04c2992017-03-01 08:17:34 -08001013{
1014 vl_api_unbind_uri_t *ump;
1015
1016 ump = vl_msg_api_alloc (sizeof (*ump));
1017 memset (ump, 0, sizeof (*ump));
1018
1019 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1020 ump->client_index = utm->my_client_index;
1021 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1022 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1023}
1024
Florin Corasa5464812017-04-19 13:00:05 -07001025int
1026server_unbind (uri_tcp_test_main_t * utm)
1027{
1028 server_send_unbind (utm);
1029 if (wait_for_state_change (utm, STATE_START))
1030 {
1031 clib_warning ("timeout waiting for STATE_START");
1032 return -1;
1033 }
1034 return 0;
1035}
1036
Florin Corase04c2992017-03-01 08:17:34 -08001037void
1038server_test (uri_tcp_test_main_t * utm)
1039{
Florin Corasa5464812017-04-19 13:00:05 -07001040 if (application_attach (utm))
1041 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001042
Dave Barach68b0fb02017-02-28 15:15:56 -05001043 /* Bind to uri */
Florin Corasa5464812017-04-19 13:00:05 -07001044 if (server_listen (utm))
1045 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001046
1047 /* Enter handle event loop */
Florin Corase04c2992017-03-01 08:17:34 -08001048 server_handle_event_queue (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001049
1050 /* Cleanup */
Florin Corasa5464812017-04-19 13:00:05 -07001051 server_send_unbind (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001052
Florin Coras6cf30ad2017-04-04 23:08:23 -07001053 application_detach (utm);
1054
Dave Barach68b0fb02017-02-28 15:15:56 -05001055 fformat (stdout, "Test complete...\n");
1056}
1057
Florin Corase69f4952017-03-07 10:06:24 -08001058static void
1059vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1060 mp)
1061{
Florin Coras6792ec02017-03-13 03:49:51 -07001062 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1063
Florin Corase69f4952017-03-07 10:06:24 -08001064 clib_warning ("retval %d", ntohl (mp->retval));
Florin Coras6792ec02017-03-13 03:49:51 -07001065 utm->state = STATE_START;
Florin Corase69f4952017-03-07 10:06:24 -08001066}
1067
1068#define foreach_uri_msg \
1069_(BIND_URI_REPLY, bind_uri_reply) \
1070_(UNBIND_URI_REPLY, unbind_uri_reply) \
1071_(ACCEPT_SESSION, accept_session) \
1072_(CONNECT_URI_REPLY, connect_uri_reply) \
1073_(DISCONNECT_SESSION, disconnect_session) \
1074_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1075_(RESET_SESSION, reset_session) \
Florin Coras6cf30ad2017-04-04 23:08:23 -07001076_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1077_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1078_(MAP_ANOTHER_SEGMENT, map_another_segment) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001079
1080void
1081uri_api_hookup (uri_tcp_test_main_t * utm)
1082{
1083#define _(N,n) \
1084 vl_msg_api_set_handlers(VL_API_##N, #n, \
1085 vl_api_##n##_t_handler, \
1086 vl_noop_handler, \
1087 vl_api_##n##_t_endian, \
1088 vl_api_##n##_t_print, \
1089 sizeof(vl_api_##n##_t), 1);
1090 foreach_uri_msg;
1091#undef _
1092}
1093
1094int
1095main (int argc, char **argv)
1096{
1097 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1098 unformat_input_t _argv, *a = &_argv;
1099 u8 *chroot_prefix;
Florin Corase69f4952017-03-07 10:06:24 -08001100 u8 *heap, *uri = 0;
1101 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1102 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001103 u64 bytes_to_send = 64 << 10, mbytes;
Dave Barach68b0fb02017-02-28 15:15:56 -05001104 u32 tmp;
1105 mheap_t *h;
Florin Corase04c2992017-03-01 08:17:34 -08001106 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -05001107 int i;
Florin Corase04c2992017-03-01 08:17:34 -08001108 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001109
1110 clib_mem_init (0, 256 << 20);
1111
1112 heap = clib_mem_get_per_cpu_heap ();
1113 h = mheap_header (heap);
1114
1115 /* make the main heap thread-safe */
1116 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1117
1118 vec_validate (utm->rx_buf, 65536);
1119
Florin Corase04c2992017-03-01 08:17:34 -08001120 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Dave Barach68b0fb02017-02-28 15:15:56 -05001121
Florin Corase04c2992017-03-01 08:17:34 -08001122 utm->my_pid = getpid ();
1123 utm->configured_segment_size = 1 << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001124
1125 clib_time_init (&utm->clib_time);
1126 init_error_string_table (utm);
Florin Corase04c2992017-03-01 08:17:34 -08001127 svm_fifo_segment_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001128 unformat_init_command_line (a, argv);
1129
1130 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1131 {
1132 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001133 {
1134 vl_set_memory_root_path ((char *) chroot_prefix);
1135 }
Florin Corase69f4952017-03-07 10:06:24 -08001136 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001137 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001138 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corase04c2992017-03-01 08:17:34 -08001139 utm->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001140 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corase04c2992017-03-01 08:17:34 -08001141 utm->configured_segment_size = tmp << 30;
Dave Barach68b0fb02017-02-28 15:15:56 -05001142 else if (unformat (a, "master"))
Florin Corase04c2992017-03-01 08:17:34 -08001143 i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001144 else if (unformat (a, "slave"))
Florin Corase04c2992017-03-01 08:17:34 -08001145 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001146 else if (unformat (a, "drop"))
Florin Corase04c2992017-03-01 08:17:34 -08001147 drop_packets = 1;
1148 else if (unformat (a, "test"))
1149 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001150 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001151 {
1152 bytes_to_send = mbytes << 20;
1153 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001154 else if (unformat (a, "gbytes %lld", &mbytes))
1155 {
1156 bytes_to_send = mbytes << 30;
1157 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001158 else
Florin Corase04c2992017-03-01 08:17:34 -08001159 {
1160 fformat (stderr, "%s: usage [master|slave]\n");
1161 exit (1);
1162 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001163 }
1164
Florin Corase69f4952017-03-07 10:06:24 -08001165 if (uri)
1166 {
1167 utm->uri = format (0, "%s%c", uri, 0);
1168 utm->connect_uri = format (0, "%s%c", uri, 0);
1169 }
1170 else
1171 {
1172 utm->uri = format (0, "%s%c", bind_uri, 0);
1173 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1174 }
1175
Dave Barach68b0fb02017-02-28 15:15:56 -05001176 utm->i_am_master = i_am_master;
1177 utm->segment_main = &svm_fifo_segment_main;
1178 utm->drop_packets = drop_packets;
Florin Corase04c2992017-03-01 08:17:34 -08001179 utm->test_return_packets = test_return_packets;
Florin Coras6792ec02017-03-13 03:49:51 -07001180 utm->bytes_to_send = bytes_to_send;
Dave Barach68b0fb02017-02-28 15:15:56 -05001181
Florin Corase04c2992017-03-01 08:17:34 -08001182 setup_signal_handlers ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001183 uri_api_hookup (utm);
1184
Florin Corase04c2992017-03-01 08:17:34 -08001185 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001186 {
1187 svm_region_exit ();
1188 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1189 exit (1);
1190 }
1191
1192 if (i_am_master == 0)
1193 {
Florin Corase04c2992017-03-01 08:17:34 -08001194 client_test (utm);
Florin Corase69f4952017-03-07 10:06:24 -08001195 vl_client_disconnect_from_vlib ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001196 exit (0);
1197 }
1198
1199 /* $$$$ hack preallocation */
1200 for (i = 0; i < 200000; i++)
1201 {
1202 pool_get (utm->sessions, session);
1203 memset (session, 0, sizeof (*session));
1204 }
1205 for (i = 0; i < 200000; i++)
1206 pool_put_index (utm->sessions, i);
1207
Florin Corase04c2992017-03-01 08:17:34 -08001208 server_test (utm);
Dave Barach68b0fb02017-02-28 15:15:56 -05001209
1210 vl_client_disconnect_from_vlib ();
1211 exit (0);
1212}
Florin Corase04c2992017-03-01 08:17:34 -08001213
1214/*
1215 * fd.io coding-style-patch-verification: ON
1216 *
1217 * Local Variables:
1218 * eval: (c-set-style "gnu")
1219 * End:
1220 */