blob: d48c5d9de03d35d186e31f26cfa5a04d73223efc [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>
Florin Coras90a63982017-12-19 04:50:01 -080018
19#include <vnet/session/application_interface.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050020#include <svm/svm_fifo_segment.h>
21#include <vlibmemory/api.h>
Florin Coras90a63982017-12-19 04:50:01 -080022
Dave Barach68b0fb02017-02-28 15:15:56 -050023#include <vpp/api/vpe_msg_enum.h>
24
Florin Corase04c2992017-03-01 08:17:34 -080025#define vl_typedefs /* define message structures */
Dave Barach68b0fb02017-02-28 15:15:56 -050026#include <vpp/api/vpe_all_api_h.h>
27#undef vl_typedefs
28
29/* declare message handlers for each api */
30
Florin Corase04c2992017-03-01 08:17:34 -080031#define vl_endianfun /* define message structures */
Dave Barach68b0fb02017-02-28 15:15:56 -050032#include <vpp/api/vpe_all_api_h.h>
33#undef vl_endianfun
34
35/* instantiate all the print functions we know about */
36#define vl_print(handle, ...)
37#define vl_printfun
38#include <vpp/api/vpe_all_api_h.h>
39#undef vl_printfun
40
Dave Barach68b0fb02017-02-28 15:15:56 -050041typedef struct
42{
Florin Corase04c2992017-03-01 08:17:34 -080043 svm_fifo_t *server_rx_fifo;
44 svm_fifo_t *server_tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -050045
Florin Corasa5464812017-04-19 13:00:05 -070046 u64 vpp_session_handle;
Florin Corasf03a59a2017-06-09 21:07:32 -070047 u64 bytes_received;
48 f64 start;
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 */
Florin Corase86a8ed2018-01-05 03:20:25 -080063 svm_queue_t *vl_input_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050064
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 Corase86a8ed2018-01-05 03:20:25 -080091 svm_queue_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050092
93 /* $$$ single thread only for the moment */
Florin Corase86a8ed2018-01-05 03:20:25 -080094 svm_queue_t *vpp_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050095
Florin Coras90a63982017-12-19 04:50:01 -080096 u8 *socket_name;
97
Dave Barach68b0fb02017-02-28 15:15:56 -050098 pid_t my_pid;
99
100 /* For deadman timers */
101 clib_time_t clib_time;
102
103 /* State of the connection, shared between msg RX thread and main thread */
104 volatile connection_state_t state;
105
106 /* Signal variables */
107 volatile int time_to_stop;
108 volatile int time_to_print_stats;
109
110 u32 configured_segment_size;
111
112 /* VNET_API_ERROR_FOO -> "Foo" hash table */
Florin Corase04c2992017-03-01 08:17:34 -0800113 uword *error_string_by_error_number;
Dave Barach68b0fb02017-02-28 15:15:56 -0500114
115 u8 *connect_test_data;
Florin Corase04c2992017-03-01 08:17:34 -0800116 pthread_t client_rx_thread_handle;
117 u32 client_bytes_received;
118 u8 test_return_packets;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700119 u64 bytes_to_send;
Florin Corase04c2992017-03-01 08:17:34 -0800120
Florin Coras90a63982017-12-19 04:50:01 -0800121 /** Flag that decides if socket, instead of svm, api is used to connect to
122 * vpp. If sock api is used, shm binary api is subsequently bootstrapped
123 * and all other messages are exchanged using shm IPC. */
124 u8 use_sock_api;
125
Florin Corase04c2992017-03-01 08:17:34 -0800126 /* convenience */
127 svm_fifo_segment_main_t *segment_main;
Florin Corasb384b542018-01-15 01:08:33 -0800128} echo_main_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500129
Florin Corasb384b542018-01-15 01:08:33 -0800130echo_main_t echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500131
132#if CLIB_DEBUG > 0
133#define NITER 10000
134#else
135#define NITER 4000000
136#endif
137
Florin Corasa5464812017-04-19 13:00:05 -0700138static u8 *
139format_api_error (u8 * s, va_list * args)
140{
Florin Corasb384b542018-01-15 01:08:33 -0800141 echo_main_t *em = &echo_main;
Florin Corasa5464812017-04-19 13:00:05 -0700142 i32 error = va_arg (*args, u32);
143 uword *p;
144
Florin Corasb384b542018-01-15 01:08:33 -0800145 p = hash_get (em->error_string_by_error_number, -error);
Florin Corasa5464812017-04-19 13:00:05 -0700146
147 if (p)
148 s = format (s, "%s", p[0]);
149 else
150 s = format (s, "%d", error);
151 return s;
152}
153
154static void
Florin Corasb384b542018-01-15 01:08:33 -0800155init_error_string_table (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700156{
Florin Corasb384b542018-01-15 01:08:33 -0800157 em->error_string_by_error_number = hash_create (0, sizeof (uword));
Florin Corasa5464812017-04-19 13:00:05 -0700158
Florin Corasb384b542018-01-15 01:08:33 -0800159#define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
Florin Corasa5464812017-04-19 13:00:05 -0700160 foreach_vnet_api_error;
161#undef _
162
Florin Corasb384b542018-01-15 01:08:33 -0800163 hash_set (em->error_string_by_error_number, 99, "Misc");
Florin Corasa5464812017-04-19 13:00:05 -0700164}
165
Dave Barach68b0fb02017-02-28 15:15:56 -0500166int
Florin Corasb384b542018-01-15 01:08:33 -0800167wait_for_state_change (echo_main_t * em, connection_state_t state)
Dave Barach68b0fb02017-02-28 15:15:56 -0500168{
169#if CLIB_DEBUG > 0
170#define TIMEOUT 600.0
171#else
172#define TIMEOUT 600.0
173#endif
174
Florin Corasb384b542018-01-15 01:08:33 -0800175 f64 timeout = clib_time_now (&em->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500176
Florin Corasb384b542018-01-15 01:08:33 -0800177 while (clib_time_now (&em->clib_time) < timeout)
Dave Barach68b0fb02017-02-28 15:15:56 -0500178 {
Florin Corasb384b542018-01-15 01:08:33 -0800179 if (em->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800180 return 0;
Florin Corasb384b542018-01-15 01:08:33 -0800181 if (em->state == STATE_FAILED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500182 return -1;
Florin Corasb384b542018-01-15 01:08:33 -0800183 if (em->time_to_stop == 1)
Florin Corasf03a59a2017-06-09 21:07:32 -0700184 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500185 }
186 clib_warning ("timeout waiting for STATE_READY");
187 return -1;
188}
189
Florin Coras6cf30ad2017-04-04 23:08:23 -0700190void
Florin Corasb384b542018-01-15 01:08:33 -0800191application_send_attach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700192{
193 vl_api_application_attach_t *bmp;
Florin Corasf03a59a2017-06-09 21:07:32 -0700194 u32 fifo_size = 4 << 20;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700195 bmp = vl_msg_api_alloc (sizeof (*bmp));
196 memset (bmp, 0, sizeof (*bmp));
197
198 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Corasb384b542018-01-15 01:08:33 -0800199 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700200 bmp->context = ntohl (0xfeedface);
Florin Corasa5464812017-04-19 13:00:05 -0700201 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700202 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
Dave Barach10d8cc62017-05-30 09:30:07 -0400203 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Corasff6e7692017-12-11 04:59:01 -0800204 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
205 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
206 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
207 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
Florin Corasb384b542018-01-15 01:08:33 -0800208 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700209}
210
Florin Corasa5464812017-04-19 13:00:05 -0700211int
Florin Corasb384b542018-01-15 01:08:33 -0800212application_attach (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700213{
Florin Corasb384b542018-01-15 01:08:33 -0800214 application_send_attach (em);
215 if (wait_for_state_change (em, STATE_ATTACHED))
Florin Corasa5464812017-04-19 13:00:05 -0700216 {
217 clib_warning ("timeout waiting for STATE_ATTACHED");
218 return -1;
219 }
220 return 0;
221}
222
Florin Coras6cf30ad2017-04-04 23:08:23 -0700223void
Florin Corasb384b542018-01-15 01:08:33 -0800224application_detach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700225{
226 vl_api_application_detach_t *bmp;
227 bmp = vl_msg_api_alloc (sizeof (*bmp));
228 memset (bmp, 0, sizeof (*bmp));
229
230 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Corasb384b542018-01-15 01:08:33 -0800231 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700232 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800233 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
234
235 clib_warning ("Sent detach");
236}
237
238static int
239memfd_segment_attach (void)
240{
241 ssvm_private_t _ssvm = { 0 }, *ssvm = &_ssvm;
242 clib_error_t *error;
243 int rv;
244
245 if ((error = vl_socket_client_recv_fd_msg (&ssvm->fd, 5)))
246 {
247 clib_error_report (error);
248 return -1;
249 }
250
251 if ((rv = ssvm_slave_init_memfd (ssvm)))
252 return rv;
253
254 return 0;
255}
256
257static int
258fifo_segment_attach (char *name, u32 size, ssvm_segment_type_t type)
259{
260 svm_fifo_segment_create_args_t _a, *a = &_a;
261 clib_error_t *error;
262 int rv;
263
264 memset (a, 0, sizeof (*a));
265 a->segment_name = (char *) name;
266 a->segment_size = size;
267 a->segment_type = type;
268
269 if (type == SSVM_SEGMENT_MEMFD)
270 {
271 if ((error = vl_socket_client_recv_fd_msg (&a->memfd_fd, 5)))
272 {
273 clib_error_report (error);
274 return -1;
275 }
276 }
277
278 if ((rv = svm_fifo_segment_attach (a)))
279 {
280 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
281 return rv;
282 }
283
284 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700285}
286
287static void
288vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
289 mp)
290{
Florin Corasb384b542018-01-15 01:08:33 -0800291 echo_main_t *em = &echo_main;
292 ssvm_segment_type_t seg_type;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700293
294 if (mp->retval)
295 {
Florin Corasa5464812017-04-19 13:00:05 -0700296 clib_warning ("attach failed: %U", format_api_error,
297 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800298 em->state = STATE_FAILED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700299 return;
300 }
301
302 if (mp->segment_name_length == 0)
303 {
304 clib_warning ("segment_name_length zero");
305 return;
306 }
307
Florin Corasb384b542018-01-15 01:08:33 -0800308 seg_type = em->use_sock_api ? SSVM_SEGMENT_MEMFD : SSVM_SEGMENT_SHM;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700309
Florin Corasb384b542018-01-15 01:08:33 -0800310 /* Attach to fifo segment */
311 if (fifo_segment_attach ((char *) mp->segment_name, mp->segment_size,
312 seg_type))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700313 {
Florin Corasb384b542018-01-15 01:08:33 -0800314 em->state = STATE_FAILED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700315 return;
316 }
317
Florin Corasb384b542018-01-15 01:08:33 -0800318 /* If we're using memfd segments, read and attach to event qs segment */
319 if (seg_type == SSVM_SEGMENT_MEMFD)
320 {
321 if (memfd_segment_attach ())
322 {
323 clib_warning ("failed to attach to evt q segment");
324 em->state = STATE_FAILED;
325 return;
326 }
327 }
328
329 ASSERT (mp->app_event_queue_address);
330 em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
331 svm_queue_t *);
332 em->state = STATE_ATTACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700333}
334
335static void
336vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
337 mp)
338{
339 if (mp->retval)
340 clib_warning ("detach returned with err: %d", mp->retval);
341}
342
Dave Barach68b0fb02017-02-28 15:15:56 -0500343static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500344stop_signal (int signum)
345{
Florin Corasb384b542018-01-15 01:08:33 -0800346 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500347
348 um->time_to_stop = 1;
349}
350
351static void
352stats_signal (int signum)
353{
Florin Corasb384b542018-01-15 01:08:33 -0800354 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500355
356 um->time_to_print_stats = 1;
357}
358
359static clib_error_t *
360setup_signal_handlers (void)
361{
362 signal (SIGINT, stats_signal);
363 signal (SIGQUIT, stop_signal);
364 signal (SIGTERM, stop_signal);
365
366 return 0;
367}
368
369void
370vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
371{
372 clib_warning ("BUG");
373}
374
375int
376connect_to_vpp (char *name)
377{
Florin Corasb384b542018-01-15 01:08:33 -0800378 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500379 api_main_t *am = &api_main;
380
Florin Corasb384b542018-01-15 01:08:33 -0800381 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800382 {
Florin Corasb384b542018-01-15 01:08:33 -0800383 if (vl_socket_client_connect ((char *) em->socket_name, name,
Florin Coras90a63982017-12-19 04:50:01 -0800384 0 /* default rx, tx buffer */ ))
Florin Corasb384b542018-01-15 01:08:33 -0800385 {
386 clib_warning ("socket connect failed");
387 return -1;
388 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500389
Florin Corasb384b542018-01-15 01:08:33 -0800390 if (vl_socket_client_init_shm (0))
391 {
392 clib_warning ("init shm api failed");
393 return -1;
394 }
Florin Coras90a63982017-12-19 04:50:01 -0800395 }
396 else
397 {
398 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
Florin Corasb384b542018-01-15 01:08:33 -0800399 {
400 clib_warning ("shmem connect failed");
401 return -1;
402 }
Florin Coras90a63982017-12-19 04:50:01 -0800403 }
Florin Corasb384b542018-01-15 01:08:33 -0800404 em->vl_input_queue = am->shmem_hdr->vl_input_queue;
405 em->my_client_index = am->my_client_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500406 return 0;
407}
408
Florin Coras90a63982017-12-19 04:50:01 -0800409void
Florin Corasb384b542018-01-15 01:08:33 -0800410disconnect_from_vpp (echo_main_t * em)
Florin Coras90a63982017-12-19 04:50:01 -0800411{
Florin Corasb384b542018-01-15 01:08:33 -0800412 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800413 vl_socket_client_disconnect ();
414 else
415 vl_client_disconnect_from_vlib ();
416}
417
Dave Barach68b0fb02017-02-28 15:15:56 -0500418static void
Florin Corase04c2992017-03-01 08:17:34 -0800419vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500420{
421 svm_fifo_segment_create_args_t _a, *a = &_a;
422 int rv;
423
Florin Coras3cbc04b2017-10-02 00:18:51 -0700424 memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500425 a->segment_name = (char *) mp->segment_name;
426 a->segment_size = mp->segment_size;
427 /* Attach to the segment vpp created */
428 rv = svm_fifo_segment_attach (a);
429 if (rv)
430 {
431 clib_warning ("svm_fifo_segment_attach ('%s') failed",
Florin Corase04c2992017-03-01 08:17:34 -0800432 mp->segment_name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500433 return;
434 }
435 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
Florin Corase04c2992017-03-01 08:17:34 -0800436 mp->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500437}
438
439static void
Florin Corasb384b542018-01-15 01:08:33 -0800440session_print_stats (echo_main_t * em, session_t * session)
Florin Corasf03a59a2017-06-09 21:07:32 -0700441{
442 f64 deltat;
443 u64 bytes;
444
Florin Corasb384b542018-01-15 01:08:33 -0800445 deltat = clib_time_now (&em->clib_time) - session->start;
446 bytes = em->i_am_master ? session->bytes_received : em->bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -0700447 fformat (stdout, "Finished in %.6f\n", deltat);
448 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
449}
450
451static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500452vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
453{
Florin Corasb384b542018-01-15 01:08:33 -0800454 echo_main_t *em = &echo_main;
Florin Corasf03a59a2017-06-09 21:07:32 -0700455 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800456 vl_api_disconnect_session_reply_t *rmp;
457 uword *p;
Dave Barach68b0fb02017-02-28 15:15:56 -0500458 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500459
Florin Corasb384b542018-01-15 01:08:33 -0800460 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800461
462 if (p)
463 {
Florin Corasb384b542018-01-15 01:08:33 -0800464 session = pool_elt_at_index (em->sessions, p[0]);
465 hash_unset (em->session_index_by_vpp_handles, mp->handle);
466 pool_put (em->sessions, session);
Florin Corase04c2992017-03-01 08:17:34 -0800467 }
468 else
469 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700470 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800471 rv = -11;
472 }
473
Florin Corasb384b542018-01-15 01:08:33 -0800474// em->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800475
476 rmp = vl_msg_api_alloc (sizeof (*rmp));
477 memset (rmp, 0, sizeof (*rmp));
478
479 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
480 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700481 rmp->handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800482 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -0700483
484 if (session)
Florin Corasb384b542018-01-15 01:08:33 -0800485 session_print_stats (em, session);
Florin Corase04c2992017-03-01 08:17:34 -0800486}
487
488static void
489vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
490{
Florin Corasb384b542018-01-15 01:08:33 -0800491 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800492 vl_api_reset_session_reply_t *rmp;
493 uword *p;
494 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800495
Florin Corasb384b542018-01-15 01:08:33 -0800496 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500497
498 if (p)
499 {
Florin Corasf6359c82017-06-19 12:26:09 -0400500 clib_warning ("got reset");
501 /* Cleanup later */
Florin Corasb384b542018-01-15 01:08:33 -0800502 em->time_to_stop = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500503 }
504 else
505 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700506 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500507 rv = -11;
508 }
509
510 rmp = vl_msg_api_alloc (sizeof (*rmp));
511 memset (rmp, 0, sizeof (*rmp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800512 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500513 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700514 rmp->handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800515 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500516}
517
518void
Florin Corasb384b542018-01-15 01:08:33 -0800519client_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -0500520{
Florin Corase04c2992017-03-01 08:17:34 -0800521 svm_fifo_t *rx_fifo;
522 int n_read, bytes, i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500523
524 rx_fifo = e->fifo;
525
Florin Coras6792ec02017-03-13 03:49:51 -0700526 bytes = svm_fifo_max_dequeue (rx_fifo);
527 /* Allow enqueuing of new event */
528 svm_fifo_unset_event (rx_fifo);
529
530 /* Read the bytes */
Dave Barach68b0fb02017-02-28 15:15:56 -0500531 do
532 {
Florin Corasa5464812017-04-19 13:00:05 -0700533 n_read = svm_fifo_dequeue_nowait (rx_fifo,
Florin Corasb384b542018-01-15 01:08:33 -0800534 clib_min (vec_len (em->rx_buf),
535 bytes), em->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500536 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800537 {
538 bytes -= n_read;
Florin Corasb384b542018-01-15 01:08:33 -0800539 if (em->test_return_packets)
Florin Corase04c2992017-03-01 08:17:34 -0800540 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700541 for (i = 0; i < n_read; i++)
Florin Corase04c2992017-03-01 08:17:34 -0800542 {
Florin Corasb384b542018-01-15 01:08:33 -0800543 if (em->rx_buf[i]
544 != ((em->client_bytes_received + i) & 0xff))
Florin Corasf03a59a2017-06-09 21:07:32 -0700545 {
546 clib_warning ("error at byte %lld, 0x%x not 0x%x",
Florin Corasb384b542018-01-15 01:08:33 -0800547 em->client_bytes_received + i,
548 em->rx_buf[i],
549 ((em->client_bytes_received + i) & 0xff));
Florin Corasf03a59a2017-06-09 21:07:32 -0700550 }
Florin Corase04c2992017-03-01 08:17:34 -0800551 }
552 }
Florin Corasb384b542018-01-15 01:08:33 -0800553 em->client_bytes_received += n_read;
Florin Corase04c2992017-03-01 08:17:34 -0800554 }
Florin Coras6792ec02017-03-13 03:49:51 -0700555 else
556 {
557 if (n_read == -2)
558 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700559// clib_warning ("weird!");
Florin Coras6792ec02017-03-13 03:49:51 -0700560 break;
561 }
562 }
Florin Corase04c2992017-03-01 08:17:34 -0800563
Dave Barach68b0fb02017-02-28 15:15:56 -0500564 }
Florin Coras6792ec02017-03-13 03:49:51 -0700565 while (bytes > 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500566}
567
568void
Florin Corasb384b542018-01-15 01:08:33 -0800569client_handle_event_queue (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -0500570{
571 session_fifo_event_t _e, *e = &_e;;
572
Florin Corasb384b542018-01-15 01:08:33 -0800573 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500574 switch (e->event_type)
575 {
Florin Corasa5464812017-04-19 13:00:05 -0700576 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -0800577 client_handle_fifo_event_rx (em, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500578 break;
579
Florin Corasa5464812017-04-19 13:00:05 -0700580 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500581 return;
582
583 default:
Florin Corase04c2992017-03-01 08:17:34 -0800584 clib_warning ("unknown event type %d", e->event_type);
Dave Barach68b0fb02017-02-28 15:15:56 -0500585 break;
586 }
587}
588
Florin Corase04c2992017-03-01 08:17:34 -0800589static void *
590client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500591{
Florin Corase04c2992017-03-01 08:17:34 -0800592 session_fifo_event_t _e, *e = &_e;
Florin Corasb384b542018-01-15 01:08:33 -0800593 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500594
Florin Corasb384b542018-01-15 01:08:33 -0800595 em->client_bytes_received = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500596 while (1)
597 {
Florin Corasb384b542018-01-15 01:08:33 -0800598 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500599 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800600 {
Florin Corasa5464812017-04-19 13:00:05 -0700601 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -0800602 client_handle_fifo_event_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -0800603 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500604
Florin Corasa5464812017-04-19 13:00:05 -0700605 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -0800606 return 0;
607 default:
608 clib_warning ("unknown event type %d", e->event_type);
609 break;
610 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500611
Florin Corasb384b542018-01-15 01:08:33 -0800612 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -0800613 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500614 }
Florin Corase04c2992017-03-01 08:17:34 -0800615 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500616}
617
Dave Barach68b0fb02017-02-28 15:15:56 -0500618
619static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400620vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500621{
Florin Corasb384b542018-01-15 01:08:33 -0800622 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500623 session_t *session;
624 u32 session_index;
625 svm_fifo_t *rx_fifo, *tx_fifo;
626 int rv;
627
628 if (mp->retval)
629 {
Florin Corasa5464812017-04-19 13:00:05 -0700630 clib_warning ("connection failed with code: %U", format_api_error,
631 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800632 em->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500633 return;
634 }
Florin Corasade70e42017-10-14 18:56:41 -0700635 else
636 {
637 clib_warning ("connected with local ip %U port %d", format_ip46_address,
638 mp->lcl_ip, mp->is_ip4,
639 clib_net_to_host_u16 (mp->lcl_port));
640 }
Florin Corase04c2992017-03-01 08:17:34 -0800641
Florin Corasb384b542018-01-15 01:08:33 -0800642 em->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -0800643 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500644
645 /*
646 * Setup session
647 */
648
Florin Corasb384b542018-01-15 01:08:33 -0800649 pool_get (em->sessions, session);
650 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -0500651
Damjan Marion7bee80c2017-04-26 15:32:12 +0200652 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500653 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200654 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500655 tx_fifo->client_session_index = session_index;
656
657 session->server_rx_fifo = rx_fifo;
658 session->server_tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700659 session->vpp_session_handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800660 session->start = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500661
662 /* Save handle */
Florin Corasb384b542018-01-15 01:08:33 -0800663 em->connected_session_index = session_index;
664 em->state = STATE_READY;
Florin Corase04c2992017-03-01 08:17:34 -0800665
666 /* Add it to lookup table */
Florin Corasb384b542018-01-15 01:08:33 -0800667 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800668
669 /* Start RX thread */
Florin Corasb384b542018-01-15 01:08:33 -0800670 rv = pthread_create (&em->client_rx_thread_handle,
Florin Corase04c2992017-03-01 08:17:34 -0800671 NULL /*attr */ , client_rx_thread_fn, 0);
672 if (rv)
673 {
674 clib_warning ("pthread_create returned %d", rv);
675 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
676 }
677}
678
Florin Coras6792ec02017-03-13 03:49:51 -0700679static void
Florin Corasb384b542018-01-15 01:08:33 -0800680send_test_chunk (echo_main_t * em, svm_fifo_t * tx_fifo, int mypid, u32 bytes)
Florin Corase04c2992017-03-01 08:17:34 -0800681{
Florin Corasb384b542018-01-15 01:08:33 -0800682 u8 *test_data = em->connect_test_data;
Florin Corase04c2992017-03-01 08:17:34 -0800683 u64 bytes_sent = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700684 int test_buf_offset = 0;
685 u32 bytes_to_snd;
Florin Corasf03a59a2017-06-09 21:07:32 -0700686 u32 queue_max_chunk = 128 << 10, actual_write;
Florin Corase04c2992017-03-01 08:17:34 -0800687 session_fifo_event_t evt;
Florin Coras6792ec02017-03-13 03:49:51 -0700688 int rv;
Florin Corase04c2992017-03-01 08:17:34 -0800689
Florin Coras6792ec02017-03-13 03:49:51 -0700690 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
691 if (bytes_to_snd > vec_len (test_data))
692 bytes_to_snd = vec_len (test_data);
Florin Corase04c2992017-03-01 08:17:34 -0800693
Florin Corasb384b542018-01-15 01:08:33 -0800694 while (bytes_to_snd > 0 && !em->time_to_stop)
Florin Corase04c2992017-03-01 08:17:34 -0800695 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700696 actual_write = (bytes_to_snd > queue_max_chunk) ?
697 queue_max_chunk : bytes_to_snd;
Florin Corasa5464812017-04-19 13:00:05 -0700698 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
Florin Coras6792ec02017-03-13 03:49:51 -0700699 test_data + test_buf_offset);
700
701 if (rv > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800702 {
Florin Coras6792ec02017-03-13 03:49:51 -0700703 bytes_to_snd -= rv;
704 test_buf_offset += rv;
705 bytes_sent += rv;
Florin Corase04c2992017-03-01 08:17:34 -0800706
Florin Coras6792ec02017-03-13 03:49:51 -0700707 if (svm_fifo_set_event (tx_fifo))
Florin Corase04c2992017-03-01 08:17:34 -0800708 {
Florin Corase04c2992017-03-01 08:17:34 -0800709 /* Fabricate TX event, send to vpp */
710 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700711 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase04c2992017-03-01 08:17:34 -0800712
Florin Corasb384b542018-01-15 01:08:33 -0800713 svm_queue_add (em->vpp_event_queue,
Florin Corase86a8ed2018-01-05 03:20:25 -0800714 (u8 *) & evt, 0 /* do wait for mutex */ );
Florin Corase04c2992017-03-01 08:17:34 -0800715 }
716 }
717 }
Florin Coras6792ec02017-03-13 03:49:51 -0700718}
719
720void
Florin Corasb384b542018-01-15 01:08:33 -0800721client_send_data (echo_main_t * em)
Florin Coras6792ec02017-03-13 03:49:51 -0700722{
Florin Corasb384b542018-01-15 01:08:33 -0800723 u8 *test_data = em->connect_test_data;
Florin Coras6792ec02017-03-13 03:49:51 -0700724 int mypid = getpid ();
725 session_t *session;
726 svm_fifo_t *tx_fifo;
727 u32 n_iterations, leftover;
728 int i;
729
Florin Corasb384b542018-01-15 01:08:33 -0800730 session = pool_elt_at_index (em->sessions, em->connected_session_index);
Florin Coras6792ec02017-03-13 03:49:51 -0700731 tx_fifo = session->server_tx_fifo;
732
Florin Coras82b13a82017-04-25 11:58:06 -0700733 ASSERT (vec_len (test_data) > 0);
734
Florin Corasb384b542018-01-15 01:08:33 -0800735 vec_validate (em->rx_buf, vec_len (test_data) - 1);
736 n_iterations = em->bytes_to_send / vec_len (test_data);
Florin Coras6792ec02017-03-13 03:49:51 -0700737
738 for (i = 0; i < n_iterations; i++)
739 {
Florin Corasb384b542018-01-15 01:08:33 -0800740 send_test_chunk (em, tx_fifo, mypid, 0);
741 if (em->time_to_stop)
Florin Corasf6359c82017-06-19 12:26:09 -0400742 break;
Florin Coras6792ec02017-03-13 03:49:51 -0700743 }
744
Florin Corasb384b542018-01-15 01:08:33 -0800745 leftover = em->bytes_to_send % vec_len (test_data);
Florin Coras6792ec02017-03-13 03:49:51 -0700746 if (leftover)
Florin Corasb384b542018-01-15 01:08:33 -0800747 send_test_chunk (em, tx_fifo, mypid, leftover);
Florin Corase04c2992017-03-01 08:17:34 -0800748
Florin Corasb384b542018-01-15 01:08:33 -0800749 if (!em->drop_packets)
Florin Corase04c2992017-03-01 08:17:34 -0800750 {
Florin Corasb384b542018-01-15 01:08:33 -0800751 f64 timeout = clib_time_now (&em->clib_time) + 10;
Florin Corase04c2992017-03-01 08:17:34 -0800752
753 /* Wait for the outstanding packets */
Florin Corasb384b542018-01-15 01:08:33 -0800754 while (em->client_bytes_received <
Florin Coras6792ec02017-03-13 03:49:51 -0700755 vec_len (test_data) * n_iterations + leftover)
Florin Corase04c2992017-03-01 08:17:34 -0800756 {
Florin Corasb384b542018-01-15 01:08:33 -0800757 if (clib_time_now (&em->clib_time) > timeout)
Florin Corase04c2992017-03-01 08:17:34 -0800758 {
759 clib_warning ("timed out waiting for the missing packets");
760 break;
761 }
762 }
Florin Corase04c2992017-03-01 08:17:34 -0800763 }
Florin Corasb384b542018-01-15 01:08:33 -0800764 em->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800765}
766
767void
Florin Corasb384b542018-01-15 01:08:33 -0800768client_send_connect (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800769{
770 vl_api_connect_uri_t *cmp;
771 cmp = vl_msg_api_alloc (sizeof (*cmp));
772 memset (cmp, 0, sizeof (*cmp));
773
774 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
Florin Corasb384b542018-01-15 01:08:33 -0800775 cmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -0800776 cmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800777 memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri));
778 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
Florin Corase04c2992017-03-01 08:17:34 -0800779}
780
Florin Corasa5464812017-04-19 13:00:05 -0700781int
Florin Corasb384b542018-01-15 01:08:33 -0800782client_connect (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700783{
Florin Corasb384b542018-01-15 01:08:33 -0800784 client_send_connect (em);
785 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -0700786 {
787 clib_warning ("Connect failed");
788 return -1;
789 }
790 return 0;
791}
792
Florin Corase04c2992017-03-01 08:17:34 -0800793void
Florin Corasb384b542018-01-15 01:08:33 -0800794client_send_disconnect (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800795{
796 session_t *connected_session;
797 vl_api_disconnect_session_t *dmp;
Florin Corasb384b542018-01-15 01:08:33 -0800798 connected_session = pool_elt_at_index (em->sessions,
799 em->connected_session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800800 dmp = vl_msg_api_alloc (sizeof (*dmp));
801 memset (dmp, 0, sizeof (*dmp));
802 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
Florin Corasb384b542018-01-15 01:08:33 -0800803 dmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700804 dmp->handle = connected_session->vpp_session_handle;
Florin Corasb384b542018-01-15 01:08:33 -0800805 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
Florin Corase04c2992017-03-01 08:17:34 -0800806}
807
Florin Corasa5464812017-04-19 13:00:05 -0700808int
Florin Corasb384b542018-01-15 01:08:33 -0800809client_disconnect (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700810{
Florin Corasb384b542018-01-15 01:08:33 -0800811 client_send_disconnect (em);
Florin Corasf03a59a2017-06-09 21:07:32 -0700812 clib_warning ("Sent disconnect");
Florin Corasb384b542018-01-15 01:08:33 -0800813 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -0700814 {
815 clib_warning ("Disconnect failed");
816 return -1;
817 }
818 return 0;
819}
820
Florin Corase04c2992017-03-01 08:17:34 -0800821static void
Florin Corasb384b542018-01-15 01:08:33 -0800822client_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800823{
824 int i;
825
Florin Corasb384b542018-01-15 01:08:33 -0800826 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -0700827 return;
Florin Corase04c2992017-03-01 08:17:34 -0800828
Florin Corasb384b542018-01-15 01:08:33 -0800829 if (client_connect (em))
Florin Corase04c2992017-03-01 08:17:34 -0800830 {
Florin Corasb384b542018-01-15 01:08:33 -0800831 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -0800832 return;
833 }
834
835 /* Init test data */
Florin Corasb384b542018-01-15 01:08:33 -0800836 vec_validate (em->connect_test_data, 128 * 1024 - 1);
837 for (i = 0; i < vec_len (em->connect_test_data); i++)
838 em->connect_test_data[i] = i & 0xff;
Florin Corase04c2992017-03-01 08:17:34 -0800839
840 /* Start send */
Florin Corasb384b542018-01-15 01:08:33 -0800841 client_send_data (em);
Florin Corase04c2992017-03-01 08:17:34 -0800842
Florin Corasb384b542018-01-15 01:08:33 -0800843 /* Disconnect and detach */
844 client_disconnect (em);
845 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -0800846}
847
848static void
849vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
850{
Florin Corasb384b542018-01-15 01:08:33 -0800851 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800852
853 if (mp->retval)
854 {
flyingeagle23a07779f2017-04-26 20:22:04 +0800855 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -0700856 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800857 em->state = STATE_FAILED;
Florin Corase04c2992017-03-01 08:17:34 -0800858 return;
859 }
860
Florin Corasb384b542018-01-15 01:08:33 -0800861 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500862}
863
Dave Barach68b0fb02017-02-28 15:15:56 -0500864static void
Florin Corase04c2992017-03-01 08:17:34 -0800865vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500866{
Florin Corasb384b542018-01-15 01:08:33 -0800867 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500868
869 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -0800870 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500871
Florin Corasb384b542018-01-15 01:08:33 -0800872 em->state = STATE_START;
Dave Barach68b0fb02017-02-28 15:15:56 -0500873}
874
Florin Coras6cf30ad2017-04-04 23:08:23 -0700875u8 *
876format_ip4_address (u8 * s, va_list * args)
877{
878 u8 *a = va_arg (*args, u8 *);
879 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
880}
881
882u8 *
883format_ip6_address (u8 * s, va_list * args)
884{
885 ip6_address_t *a = va_arg (*args, ip6_address_t *);
886 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
887
888 i_max_n_zero = ARRAY_LEN (a->as_u16);
889 max_n_zeros = 0;
890 i_first_zero = i_max_n_zero;
891 n_zeros = 0;
892 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
893 {
894 u32 is_zero = a->as_u16[i] == 0;
895 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
896 {
897 i_first_zero = i;
898 n_zeros = 0;
899 }
900 n_zeros += is_zero;
901 if ((!is_zero && n_zeros > max_n_zeros)
902 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
903 {
904 i_max_n_zero = i_first_zero;
905 max_n_zeros = n_zeros;
906 i_first_zero = ARRAY_LEN (a->as_u16);
907 n_zeros = 0;
908 }
909 }
910
911 last_double_colon = 0;
912 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
913 {
914 if (i == i_max_n_zero && max_n_zeros > 1)
915 {
916 s = format (s, "::");
917 i += max_n_zeros - 1;
918 last_double_colon = 1;
919 }
920 else
921 {
922 s = format (s, "%s%x",
923 (last_double_colon || i == 0) ? "" : ":",
924 clib_net_to_host_u16 (a->as_u16[i]));
925 last_double_colon = 0;
926 }
927 }
928
929 return s;
930}
931
932/* Format an IP46 address. */
933u8 *
934format_ip46_address (u8 * s, va_list * args)
935{
936 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
937 ip46_type_t type = va_arg (*args, ip46_type_t);
938 int is_ip4 = 1;
939
940 switch (type)
941 {
942 case IP46_TYPE_ANY:
943 is_ip4 = ip46_address_is_ip4 (ip46);
944 break;
945 case IP46_TYPE_IP4:
946 is_ip4 = 1;
947 break;
948 case IP46_TYPE_IP6:
949 is_ip4 = 0;
950 break;
951 }
952
953 return is_ip4 ?
954 format (s, "%U", format_ip4_address, &ip46->ip4) :
955 format (s, "%U", format_ip6_address, &ip46->ip6);
956}
957
Dave Barach68b0fb02017-02-28 15:15:56 -0500958static void
959vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
960{
Florin Corasb384b542018-01-15 01:08:33 -0800961 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500962 vl_api_accept_session_reply_t *rmp;
Florin Corase04c2992017-03-01 08:17:34 -0800963 svm_fifo_t *rx_fifo, *tx_fifo;
964 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500965 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -0500966 u32 session_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700967 u8 *ip_str;
Dave Barach68b0fb02017-02-28 15:15:56 -0500968
969 if (start_time == 0.0)
Florin Corasb384b542018-01-15 01:08:33 -0800970 start_time = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500971
Florin Coras6cf30ad2017-04-04 23:08:23 -0700972 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
973 clib_warning ("Accepted session from: %s:%d", ip_str,
974 clib_net_to_host_u16 (mp->port));
Florin Corasb384b542018-01-15 01:08:33 -0800975 em->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -0800976 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500977
978 /* Allocate local session and set it up */
Florin Corasb384b542018-01-15 01:08:33 -0800979 pool_get (em->sessions, session);
980 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -0500981
Damjan Marion7bee80c2017-04-26 15:32:12 +0200982 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500983 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200984 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500985 tx_fifo->client_session_index = session_index;
986
987 session->server_rx_fifo = rx_fifo;
988 session->server_tx_fifo = tx_fifo;
989
990 /* Add it to lookup table */
Florin Corasb384b542018-01-15 01:08:33 -0800991 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500992
Florin Corasb384b542018-01-15 01:08:33 -0800993 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500994
995 /* Stats printing */
Florin Corasb384b542018-01-15 01:08:33 -0800996 if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500997 {
Florin Corasb384b542018-01-15 01:08:33 -0800998 f64 now = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500999 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
Florin Corasb384b542018-01-15 01:08:33 -08001000 pool_elts (em->sessions), now - start_time,
1001 (f64) pool_elts (em->sessions) / (now - start_time));
Dave Barach68b0fb02017-02-28 15:15:56 -05001002 }
1003
Florin Corase04c2992017-03-01 08:17:34 -08001004 /*
1005 * Send accept reply to vpp
1006 */
Dave Barach68b0fb02017-02-28 15:15:56 -05001007 rmp = vl_msg_api_alloc (sizeof (*rmp));
1008 memset (rmp, 0, sizeof (*rmp));
1009 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001010 rmp->handle = mp->handle;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001011 rmp->context = mp->context;
Florin Corasb384b542018-01-15 01:08:33 -08001012 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -07001013
1014 session->bytes_received = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001015 session->start = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001016}
1017
1018void
Florin Corasb384b542018-01-15 01:08:33 -08001019server_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -05001020{
Florin Corase04c2992017-03-01 08:17:34 -08001021 svm_fifo_t *rx_fifo, *tx_fifo;
1022 int n_read;
Florin Corase04c2992017-03-01 08:17:34 -08001023 session_fifo_event_t evt;
Florin Corase86a8ed2018-01-05 03:20:25 -08001024 svm_queue_t *q;
Florin Corasf03a59a2017-06-09 21:07:32 -07001025 session_t *session;
1026 int rv;
1027 u32 max_dequeue, offset, max_transfer, rx_buf_len;
Florin Corase04c2992017-03-01 08:17:34 -08001028
Florin Corasb384b542018-01-15 01:08:33 -08001029 rx_buf_len = vec_len (em->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -08001030 rx_fifo = e->fifo;
Florin Corasb384b542018-01-15 01:08:33 -08001031 session = &em->sessions[rx_fifo->client_session_index];
Florin Corasf03a59a2017-06-09 21:07:32 -07001032 tx_fifo = session->server_tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -08001033
Florin Corasf03a59a2017-06-09 21:07:32 -07001034 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -07001035 /* Allow enqueuing of a new event */
1036 svm_fifo_unset_event (rx_fifo);
1037
Florin Corasf03a59a2017-06-09 21:07:32 -07001038 if (PREDICT_FALSE (max_dequeue == 0))
1039 {
1040 return;
1041 }
Florin Coras6792ec02017-03-13 03:49:51 -07001042
Florin Corasf03a59a2017-06-09 21:07:32 -07001043 /* Read the max_dequeue */
Florin Corase04c2992017-03-01 08:17:34 -08001044 do
1045 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001046 max_transfer = clib_min (rx_buf_len, max_dequeue);
Florin Corasb384b542018-01-15 01:08:33 -08001047 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, em->rx_buf);
Florin Coras6792ec02017-03-13 03:49:51 -07001048 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -08001049 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001050 max_dequeue -= n_read;
1051 session->bytes_received += n_read;
1052 }
1053
1054 /* Reflect if a non-drop session */
Florin Corasb384b542018-01-15 01:08:33 -08001055 if (!em->drop_packets && n_read > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001056 {
1057 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001058 do
1059 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001060 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
Florin Corasb384b542018-01-15 01:08:33 -08001061 &em->rx_buf[offset]);
Florin Corasf03a59a2017-06-09 21:07:32 -07001062 if (rv > 0)
1063 {
1064 n_read -= rv;
1065 offset += rv;
1066 }
Florin Corase04c2992017-03-01 08:17:34 -08001067 }
Florin Corasb384b542018-01-15 01:08:33 -08001068 while ((rv <= 0 || n_read > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001069
Florin Coras6792ec02017-03-13 03:49:51 -07001070 /* If event wasn't set, add one */
1071 if (svm_fifo_set_event (tx_fifo))
1072 {
1073 /* Fabricate TX event, send to vpp */
1074 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -07001075 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -07001076
Florin Corasb384b542018-01-15 01:08:33 -08001077 q = em->vpp_event_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -08001078 svm_queue_add (q, (u8 *) & evt, 1 /* do wait for mutex */ );
Florin Coras6792ec02017-03-13 03:49:51 -07001079 }
Florin Corase04c2992017-03-01 08:17:34 -08001080 }
Florin Corase04c2992017-03-01 08:17:34 -08001081 }
Florin Corasb384b542018-01-15 01:08:33 -08001082 while ((n_read < 0 || max_dequeue > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001083}
1084
1085void
Florin Corasb384b542018-01-15 01:08:33 -08001086server_handle_event_queue (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001087{
Florin Coras3cbc04b2017-10-02 00:18:51 -07001088 session_fifo_event_t _e, *e = &_e;
Florin Corase04c2992017-03-01 08:17:34 -08001089
1090 while (1)
1091 {
Florin Corasb384b542018-01-15 01:08:33 -08001092 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Florin Corase04c2992017-03-01 08:17:34 -08001093 switch (e->event_type)
1094 {
Florin Corasa5464812017-04-19 13:00:05 -07001095 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -08001096 server_handle_fifo_event_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -08001097 break;
1098
Florin Corasa5464812017-04-19 13:00:05 -07001099 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -08001100 return;
1101
1102 default:
1103 clib_warning ("unknown event type %d", e->event_type);
1104 break;
1105 }
Florin Corasb384b542018-01-15 01:08:33 -08001106 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001107 break;
Florin Corasb384b542018-01-15 01:08:33 -08001108 if (PREDICT_FALSE (em->time_to_print_stats == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001109 {
Florin Corasb384b542018-01-15 01:08:33 -08001110 em->time_to_print_stats = 0;
1111 fformat (stdout, "%d connections\n", pool_elts (em->sessions));
Florin Corase04c2992017-03-01 08:17:34 -08001112 }
1113 }
1114}
1115
1116void
Florin Corasb384b542018-01-15 01:08:33 -08001117server_send_listen (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001118{
1119 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001120 bmp = vl_msg_api_alloc (sizeof (*bmp));
1121 memset (bmp, 0, sizeof (*bmp));
1122
1123 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001124 bmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -08001125 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -08001126 memcpy (bmp->uri, em->uri, vec_len (em->uri));
1127 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase04c2992017-03-01 08:17:34 -08001128}
1129
Florin Corasa5464812017-04-19 13:00:05 -07001130int
Florin Corasb384b542018-01-15 01:08:33 -08001131server_listen (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001132{
Florin Corasb384b542018-01-15 01:08:33 -08001133 server_send_listen (em);
1134 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -07001135 {
1136 clib_warning ("timeout waiting for STATE_READY");
1137 return -1;
1138 }
1139 return 0;
1140}
1141
Florin Corase04c2992017-03-01 08:17:34 -08001142void
Florin Corasb384b542018-01-15 01:08:33 -08001143server_send_unbind (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001144{
1145 vl_api_unbind_uri_t *ump;
1146
1147 ump = vl_msg_api_alloc (sizeof (*ump));
1148 memset (ump, 0, sizeof (*ump));
1149
1150 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001151 ump->client_index = em->my_client_index;
1152 memcpy (ump->uri, em->uri, vec_len (em->uri));
1153 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
Florin Corase04c2992017-03-01 08:17:34 -08001154}
1155
Florin Corasa5464812017-04-19 13:00:05 -07001156int
Florin Corasb384b542018-01-15 01:08:33 -08001157server_unbind (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001158{
Florin Corasb384b542018-01-15 01:08:33 -08001159 server_send_unbind (em);
1160 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -07001161 {
1162 clib_warning ("timeout waiting for STATE_START");
1163 return -1;
1164 }
1165 return 0;
1166}
1167
Florin Corase04c2992017-03-01 08:17:34 -08001168void
Florin Corasb384b542018-01-15 01:08:33 -08001169server_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001170{
Florin Coras90a63982017-12-19 04:50:01 -08001171 session_t *session;
1172 int i;
1173
1174 /* $$$$ hack preallocation */
1175 for (i = 0; i < 200000; i++)
1176 {
Florin Corasb384b542018-01-15 01:08:33 -08001177 pool_get (em->sessions, session);
Florin Coras90a63982017-12-19 04:50:01 -08001178 memset (session, 0, sizeof (*session));
1179 }
1180 for (i = 0; i < 200000; i++)
Florin Corasb384b542018-01-15 01:08:33 -08001181 pool_put_index (em->sessions, i);
Florin Coras90a63982017-12-19 04:50:01 -08001182
Florin Corasb384b542018-01-15 01:08:33 -08001183 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -07001184 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001185
Dave Barach68b0fb02017-02-28 15:15:56 -05001186 /* Bind to uri */
Florin Corasb384b542018-01-15 01:08:33 -08001187 if (server_listen (em))
Florin Corasa5464812017-04-19 13:00:05 -07001188 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001189
1190 /* Enter handle event loop */
Florin Corasb384b542018-01-15 01:08:33 -08001191 server_handle_event_queue (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001192
1193 /* Cleanup */
Florin Corasb384b542018-01-15 01:08:33 -08001194 server_send_unbind (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001195
Florin Corasb384b542018-01-15 01:08:33 -08001196 application_detach (em);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001197
Dave Barach68b0fb02017-02-28 15:15:56 -05001198 fformat (stdout, "Test complete...\n");
1199}
1200
Florin Corase69f4952017-03-07 10:06:24 -08001201static void
1202vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1203 mp)
1204{
Florin Corasb384b542018-01-15 01:08:33 -08001205 echo_main_t *em = &echo_main;
Florin Corasf03a59a2017-06-09 21:07:32 -07001206 session_t *session;
Florin Coras6792ec02017-03-13 03:49:51 -07001207
Florin Corasf03a59a2017-06-09 21:07:32 -07001208 if (mp->retval)
1209 {
1210 clib_warning ("vpp complained about disconnect: %d",
1211 ntohl (mp->retval));
1212 }
1213
Florin Corasb384b542018-01-15 01:08:33 -08001214 em->state = STATE_START;
1215 session = pool_elt_at_index (em->sessions, em->connected_session_index);
Florin Corasf03a59a2017-06-09 21:07:32 -07001216 if (session)
Florin Corasb384b542018-01-15 01:08:33 -08001217 session_print_stats (em, session);
Florin Corase69f4952017-03-07 10:06:24 -08001218}
1219
Florin Corasb384b542018-01-15 01:08:33 -08001220#define foreach_tcp_echo_msg \
Florin Coras90a63982017-12-19 04:50:01 -08001221_(BIND_URI_REPLY, bind_uri_reply) \
1222_(UNBIND_URI_REPLY, unbind_uri_reply) \
1223_(ACCEPT_SESSION, accept_session) \
1224_(CONNECT_SESSION_REPLY, connect_session_reply) \
1225_(DISCONNECT_SESSION, disconnect_session) \
1226_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1227_(RESET_SESSION, reset_session) \
1228_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1229_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1230_(MAP_ANOTHER_SEGMENT, map_another_segment) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001231
1232void
Florin Corasb384b542018-01-15 01:08:33 -08001233tcp_echo_api_hookup (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -05001234{
1235#define _(N,n) \
1236 vl_msg_api_set_handlers(VL_API_##N, #n, \
1237 vl_api_##n##_t_handler, \
1238 vl_noop_handler, \
1239 vl_api_##n##_t_endian, \
1240 vl_api_##n##_t_print, \
1241 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -08001242 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -05001243#undef _
1244}
1245
1246int
1247main (int argc, char **argv)
1248{
Florin Corasb384b542018-01-15 01:08:33 -08001249 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1250 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001251 unformat_input_t _argv, *a = &_argv;
1252 u8 *chroot_prefix;
Florin Corase69f4952017-03-07 10:06:24 -08001253 u8 *heap, *uri = 0;
1254 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1255 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001256 u64 bytes_to_send = 64 << 10, mbytes;
Florin Corasb384b542018-01-15 01:08:33 -08001257 char *app_name;
Dave Barach68b0fb02017-02-28 15:15:56 -05001258 u32 tmp;
1259 mheap_t *h;
Dave Barach68b0fb02017-02-28 15:15:56 -05001260
1261 clib_mem_init (0, 256 << 20);
1262
1263 heap = clib_mem_get_per_cpu_heap ();
1264 h = mheap_header (heap);
1265
1266 /* make the main heap thread-safe */
1267 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1268
Florin Corasb384b542018-01-15 01:08:33 -08001269 vec_validate (em->rx_buf, 128 << 10);
Dave Barach68b0fb02017-02-28 15:15:56 -05001270
Florin Corasb384b542018-01-15 01:08:33 -08001271 em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Dave Barach68b0fb02017-02-28 15:15:56 -05001272
Florin Corasb384b542018-01-15 01:08:33 -08001273 em->my_pid = getpid ();
1274 em->configured_segment_size = 1 << 20;
1275 em->socket_name = 0;
1276 em->use_sock_api = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001277
Florin Corasb384b542018-01-15 01:08:33 -08001278 clib_time_init (&em->clib_time);
1279 init_error_string_table (em);
Florin Corasa332c462018-01-31 06:52:17 -08001280 svm_fifo_segment_main_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001281 unformat_init_command_line (a, argv);
1282
1283 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1284 {
1285 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001286 {
1287 vl_set_memory_root_path ((char *) chroot_prefix);
1288 }
Florin Corase69f4952017-03-07 10:06:24 -08001289 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001290 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001291 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001292 em->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001293 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001294 em->configured_segment_size = tmp << 30;
Dave Barach68b0fb02017-02-28 15:15:56 -05001295 else if (unformat (a, "master"))
Florin Corase04c2992017-03-01 08:17:34 -08001296 i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001297 else if (unformat (a, "slave"))
Florin Corase04c2992017-03-01 08:17:34 -08001298 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001299 else if (unformat (a, "drop"))
Florin Corase04c2992017-03-01 08:17:34 -08001300 drop_packets = 1;
1301 else if (unformat (a, "test"))
1302 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001303 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001304 {
1305 bytes_to_send = mbytes << 20;
1306 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001307 else if (unformat (a, "gbytes %lld", &mbytes))
1308 {
1309 bytes_to_send = mbytes << 30;
1310 }
Florin Corasb384b542018-01-15 01:08:33 -08001311 else if (unformat (a, "socket-name %s", &em->socket_name))
Florin Coras90a63982017-12-19 04:50:01 -08001312 ;
1313 else if (unformat (a, "use-svm-api"))
Florin Corasb384b542018-01-15 01:08:33 -08001314 em->use_sock_api = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001315 else
Florin Corase04c2992017-03-01 08:17:34 -08001316 {
1317 fformat (stderr, "%s: usage [master|slave]\n");
1318 exit (1);
1319 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001320 }
1321
Florin Corasb384b542018-01-15 01:08:33 -08001322 if (!em->socket_name)
1323 em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
Florin Coras90a63982017-12-19 04:50:01 -08001324
Florin Corase69f4952017-03-07 10:06:24 -08001325 if (uri)
1326 {
Florin Corasb384b542018-01-15 01:08:33 -08001327 em->uri = format (0, "%s%c", uri, 0);
1328 em->connect_uri = format (0, "%s%c", uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001329 }
1330 else
1331 {
Florin Corasb384b542018-01-15 01:08:33 -08001332 em->uri = format (0, "%s%c", bind_uri, 0);
1333 em->connect_uri = format (0, "%s%c", connect_uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001334 }
1335
Florin Corasb384b542018-01-15 01:08:33 -08001336 em->i_am_master = i_am_master;
1337 em->segment_main = &svm_fifo_segment_main;
1338 em->drop_packets = drop_packets;
1339 em->test_return_packets = test_return_packets;
1340 em->bytes_to_send = bytes_to_send;
1341 em->time_to_stop = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001342
Florin Corase04c2992017-03-01 08:17:34 -08001343 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001344 tcp_echo_api_hookup (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001345
Florin Corasb384b542018-01-15 01:08:33 -08001346 app_name = i_am_master ? "tcp_echo_server" : "tcp_echo_client";
1347 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001348 {
1349 svm_region_exit ();
1350 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1351 exit (1);
1352 }
1353
1354 if (i_am_master == 0)
Florin Corasb384b542018-01-15 01:08:33 -08001355 client_run (em);
Florin Coras90a63982017-12-19 04:50:01 -08001356 else
Florin Corasb384b542018-01-15 01:08:33 -08001357 server_run (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001358
Florin Corasb384b542018-01-15 01:08:33 -08001359 disconnect_from_vpp (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001360 exit (0);
1361}
Florin Corase04c2992017-03-01 08:17:34 -08001362
1363/*
1364 * fd.io coding-style-patch-verification: ON
1365 *
1366 * Local Variables:
1367 * eval: (c-set-style "gnu")
1368 * End:
1369 */