blob: 5249b8d607685a65fe144949ba03619d68a1fd16 [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
Florin Corasa849b7b2018-05-18 00:41:55 -070041#define TCP_ECHO_DBG 0
42#define DBG(_fmt,_args...) \
43 if (TCP_ECHO_DBG) \
44 clib_warning (_fmt, _args)
45
Dave Barach68b0fb02017-02-28 15:15:56 -050046typedef struct
47{
Florin Coras9e47ac52019-01-24 23:22:37 -080048 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
49#define _(type, name) type name;
50 foreach_app_session_field
51#undef _
Florin Corasa5464812017-04-19 13:00:05 -070052 u64 vpp_session_handle;
Florin Corasa849b7b2018-05-18 00:41:55 -070053 u64 bytes_sent;
54 u64 bytes_to_send;
55 volatile u64 bytes_received;
56 volatile u64 bytes_to_receive;
Florin Corasf03a59a2017-06-09 21:07:32 -070057 f64 start;
Florin Coras9e47ac52019-01-24 23:22:37 -080058} echo_session_t;
Dave Barach68b0fb02017-02-28 15:15:56 -050059
60typedef enum
61{
62 STATE_START,
Florin Corasa5464812017-04-19 13:00:05 -070063 STATE_ATTACHED,
Florin Coras52207f12018-07-12 14:48:06 -070064 STATE_LISTEN,
Dave Barach68b0fb02017-02-28 15:15:56 -050065 STATE_READY,
66 STATE_DISCONNECTING,
Florin Corasa849b7b2018-05-18 00:41:55 -070067 STATE_FAILED,
68 STATE_DETACHED
Dave Barach68b0fb02017-02-28 15:15:56 -050069} connection_state_t;
70
71typedef struct
72{
73 /* vpe input queue */
Florin Corase86a8ed2018-01-05 03:20:25 -080074 svm_queue_t *vl_input_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050075
76 /* API client handle */
77 u32 my_client_index;
78
79 /* The URI we're playing with */
Florin Corase04c2992017-03-01 08:17:34 -080080 u8 *uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050081
82 /* Session pool */
Florin Coras9e47ac52019-01-24 23:22:37 -080083 echo_session_t *sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -050084
85 /* Hash table for disconnect processing */
Florin Corase04c2992017-03-01 08:17:34 -080086 uword *session_index_by_vpp_handles;
Dave Barach68b0fb02017-02-28 15:15:56 -050087
88 /* intermediate rx buffer */
Florin Corase04c2992017-03-01 08:17:34 -080089 u8 *rx_buf;
Dave Barach68b0fb02017-02-28 15:15:56 -050090
91 /* URI for slave's connect */
Florin Corase04c2992017-03-01 08:17:34 -080092 u8 *connect_uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050093
94 u32 connected_session_index;
95
96 int i_am_master;
97
98 /* drop all packets */
Florin Coras8e43d042018-05-04 15:46:57 -070099 int no_return;
Dave Barach68b0fb02017-02-28 15:15:56 -0500100
101 /* Our event queue */
Florin Coras3c2fed52018-07-04 04:15:05 -0700102 svm_msg_q_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500103
Florin Coras90a63982017-12-19 04:50:01 -0800104 u8 *socket_name;
105
Dave Barach68b0fb02017-02-28 15:15:56 -0500106 pid_t my_pid;
107
108 /* For deadman timers */
109 clib_time_t clib_time;
110
111 /* State of the connection, shared between msg RX thread and main thread */
112 volatile connection_state_t state;
113
114 /* Signal variables */
115 volatile int time_to_stop;
116 volatile int time_to_print_stats;
117
118 u32 configured_segment_size;
119
120 /* VNET_API_ERROR_FOO -> "Foo" hash table */
Florin Corase04c2992017-03-01 08:17:34 -0800121 uword *error_string_by_error_number;
Dave Barach68b0fb02017-02-28 15:15:56 -0500122
123 u8 *connect_test_data;
Florin Corasa849b7b2018-05-18 00:41:55 -0700124 pthread_t *client_thread_handles;
125 u32 *thread_args;
Florin Corase04c2992017-03-01 08:17:34 -0800126 u32 client_bytes_received;
127 u8 test_return_packets;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700128 u64 bytes_to_send;
Florin Coras8e43d042018-05-04 15:46:57 -0700129 u32 fifo_size;
Florin Corase04c2992017-03-01 08:17:34 -0800130
Florin Corasa849b7b2018-05-18 00:41:55 -0700131 u32 n_clients;
132 u64 tx_total;
133 u64 rx_total;
134
135 volatile u32 n_clients_connected;
136 volatile u32 n_active_clients;
137
138
Florin Coras90a63982017-12-19 04:50:01 -0800139 /** Flag that decides if socket, instead of svm, api is used to connect to
140 * vpp. If sock api is used, shm binary api is subsequently bootstrapped
141 * and all other messages are exchanged using shm IPC. */
142 u8 use_sock_api;
143
Florin Corasadc74d72018-12-02 13:36:00 -0800144 svm_fifo_segment_main_t segment_main;
Florin Corasb384b542018-01-15 01:08:33 -0800145} echo_main_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500146
Florin Corasb384b542018-01-15 01:08:33 -0800147echo_main_t echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500148
149#if CLIB_DEBUG > 0
150#define NITER 10000
151#else
152#define NITER 4000000
153#endif
154
Florin Corasa5464812017-04-19 13:00:05 -0700155static u8 *
156format_api_error (u8 * s, va_list * args)
157{
Florin Corasb384b542018-01-15 01:08:33 -0800158 echo_main_t *em = &echo_main;
Florin Corasa5464812017-04-19 13:00:05 -0700159 i32 error = va_arg (*args, u32);
160 uword *p;
161
Florin Corasb384b542018-01-15 01:08:33 -0800162 p = hash_get (em->error_string_by_error_number, -error);
Florin Corasa5464812017-04-19 13:00:05 -0700163
164 if (p)
165 s = format (s, "%s", p[0]);
166 else
167 s = format (s, "%d", error);
168 return s;
169}
170
171static void
Florin Corasb384b542018-01-15 01:08:33 -0800172init_error_string_table (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700173{
Florin Corasb384b542018-01-15 01:08:33 -0800174 em->error_string_by_error_number = hash_create (0, sizeof (uword));
Florin Corasa5464812017-04-19 13:00:05 -0700175
Florin Corasb384b542018-01-15 01:08:33 -0800176#define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
Florin Corasa5464812017-04-19 13:00:05 -0700177 foreach_vnet_api_error;
178#undef _
179
Florin Corasb384b542018-01-15 01:08:33 -0800180 hash_set (em->error_string_by_error_number, 99, "Misc");
Florin Corasa5464812017-04-19 13:00:05 -0700181}
182
Florin Coras52207f12018-07-12 14:48:06 -0700183static void handle_mq_event (session_event_t * e);
184
185static int
Florin Corasb384b542018-01-15 01:08:33 -0800186wait_for_state_change (echo_main_t * em, connection_state_t state)
Dave Barach68b0fb02017-02-28 15:15:56 -0500187{
Florin Coras52207f12018-07-12 14:48:06 -0700188 svm_msg_q_msg_t msg;
189 session_event_t *e;
190 f64 timeout;
191
Dave Barach68b0fb02017-02-28 15:15:56 -0500192#if CLIB_DEBUG > 0
193#define TIMEOUT 600.0
194#else
195#define TIMEOUT 600.0
196#endif
197
Florin Coras52207f12018-07-12 14:48:06 -0700198 timeout = clib_time_now (&em->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500199
Florin Corasb384b542018-01-15 01:08:33 -0800200 while (clib_time_now (&em->clib_time) < timeout)
Dave Barach68b0fb02017-02-28 15:15:56 -0500201 {
Florin Corasb384b542018-01-15 01:08:33 -0800202 if (em->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800203 return 0;
Florin Corasb384b542018-01-15 01:08:33 -0800204 if (em->state == STATE_FAILED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500205 return -1;
Florin Corasb384b542018-01-15 01:08:33 -0800206 if (em->time_to_stop == 1)
Florin Corasf03a59a2017-06-09 21:07:32 -0700207 return 0;
Florin Coras99368312018-08-02 10:45:44 -0700208 if (!em->our_event_queue || em->state < STATE_ATTACHED)
Florin Coras52207f12018-07-12 14:48:06 -0700209 continue;
210
211 if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_NOWAIT, 0))
212 continue;
213 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
214 handle_mq_event (e);
215 svm_msg_q_free_msg (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500216 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700217 clib_warning ("timeout waiting for state %d", state);
Dave Barach68b0fb02017-02-28 15:15:56 -0500218 return -1;
219}
220
Florin Coras6cf30ad2017-04-04 23:08:23 -0700221void
Florin Corasb384b542018-01-15 01:08:33 -0800222application_send_attach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700223{
224 vl_api_application_attach_t *bmp;
Florin Corase3e2f072018-03-04 07:24:30 -0800225 vl_api_application_tls_cert_add_t *cert_mp;
226 vl_api_application_tls_key_add_t *key_mp;
227
Florin Coras6cf30ad2017-04-04 23:08:23 -0700228 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400229 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700230
231 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Corasb384b542018-01-15 01:08:33 -0800232 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700233 bmp->context = ntohl (0xfeedface);
Florin Coras52207f12018-07-12 14:48:06 -0700234 bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
235 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT;
236 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
Dave Barach10d8cc62017-05-30 09:30:07 -0400237 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Coras8e43d042018-05-04 15:46:57 -0700238 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = em->fifo_size;
239 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = em->fifo_size;
Florin Corasff6e7692017-12-11 04:59:01 -0800240 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
241 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
Florin Corasa849b7b2018-05-18 00:41:55 -0700242 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 256;
Florin Corasb384b542018-01-15 01:08:33 -0800243 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase3e2f072018-03-04 07:24:30 -0800244
245 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
Dave Barachb7b92992018-10-17 10:38:51 -0400246 clib_memset (cert_mp, 0, sizeof (*cert_mp));
Florin Corase3e2f072018-03-04 07:24:30 -0800247 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
248 cert_mp->client_index = em->my_client_index;
249 cert_mp->context = ntohl (0xfeedface);
250 cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500251 clib_memcpy_fast (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800252 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
253
254 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
Dave Barachb7b92992018-10-17 10:38:51 -0400255 clib_memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800256 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
257 key_mp->client_index = em->my_client_index;
258 key_mp->context = ntohl (0xfeedface);
259 key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500260 clib_memcpy_fast (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800261 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700262}
263
Florin Coras52207f12018-07-12 14:48:06 -0700264static int
Florin Corasb384b542018-01-15 01:08:33 -0800265application_attach (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700266{
Florin Corasb384b542018-01-15 01:08:33 -0800267 application_send_attach (em);
268 if (wait_for_state_change (em, STATE_ATTACHED))
Florin Corasa5464812017-04-19 13:00:05 -0700269 {
270 clib_warning ("timeout waiting for STATE_ATTACHED");
271 return -1;
272 }
273 return 0;
274}
275
Florin Coras6cf30ad2017-04-04 23:08:23 -0700276void
Florin Corasb384b542018-01-15 01:08:33 -0800277application_detach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700278{
279 vl_api_application_detach_t *bmp;
280 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400281 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700282
283 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Corasb384b542018-01-15 01:08:33 -0800284 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700285 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800286 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
287
Florin Corasa849b7b2018-05-18 00:41:55 -0700288 DBG ("%s", "Sent detach");
Florin Corasb384b542018-01-15 01:08:33 -0800289}
290
291static int
Florin Coras99368312018-08-02 10:45:44 -0700292ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
Florin Corasb384b542018-01-15 01:08:33 -0800293{
294 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -0800295 svm_fifo_segment_main_t *sm = &echo_main.segment_main;
Florin Corasb384b542018-01-15 01:08:33 -0800296 int rv;
297
Dave Barachb7b92992018-10-17 10:38:51 -0400298 clib_memset (a, 0, sizeof (*a));
Florin Corasb384b542018-01-15 01:08:33 -0800299 a->segment_name = (char *) name;
Florin Corasb384b542018-01-15 01:08:33 -0800300 a->segment_type = type;
301
302 if (type == SSVM_SEGMENT_MEMFD)
Florin Coras99368312018-08-02 10:45:44 -0700303 a->memfd_fd = fd;
Florin Corasb384b542018-01-15 01:08:33 -0800304
Florin Corasadc74d72018-12-02 13:36:00 -0800305 if ((rv = svm_fifo_segment_attach (sm, a)))
Florin Corasb384b542018-01-15 01:08:33 -0800306 {
307 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
308 return rv;
309 }
310
Florin Coras99368312018-08-02 10:45:44 -0700311 vec_reset_length (a->new_segment_indices);
Florin Corasb384b542018-01-15 01:08:33 -0800312 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700313}
314
315static void
316vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
317 mp)
318{
Florin Corasb384b542018-01-15 01:08:33 -0800319 echo_main_t *em = &echo_main;
Florin Coras99368312018-08-02 10:45:44 -0700320 int *fds = 0;
321 u32 n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700322
323 if (mp->retval)
324 {
Florin Corasa5464812017-04-19 13:00:05 -0700325 clib_warning ("attach failed: %U", format_api_error,
326 clib_net_to_host_u32 (mp->retval));
Florin Coras99368312018-08-02 10:45:44 -0700327 goto failed;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700328 }
329
330 if (mp->segment_name_length == 0)
331 {
332 clib_warning ("segment_name_length zero");
Florin Coras99368312018-08-02 10:45:44 -0700333 goto failed;
Florin Corasb384b542018-01-15 01:08:33 -0800334 }
335
336 ASSERT (mp->app_event_queue_address);
337 em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
Florin Coras3c2fed52018-07-04 04:15:05 -0700338 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700339
340 if (mp->n_fds)
341 {
342 vec_validate (fds, mp->n_fds);
343 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
344
345 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
346 if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
347 goto failed;
348
349 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
350 if (ssvm_segment_attach ((char *) mp->segment_name,
351 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
352 goto failed;
353
354 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
355 svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
356
357 vec_free (fds);
358 }
359 else
360 {
361 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
362 -1))
363 goto failed;
364 }
365
Florin Corasb384b542018-01-15 01:08:33 -0800366 em->state = STATE_ATTACHED;
Florin Coras99368312018-08-02 10:45:44 -0700367 return;
368failed:
369 em->state = STATE_FAILED;
370 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700371}
372
373static void
374vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
375 mp)
376{
377 if (mp->retval)
378 clib_warning ("detach returned with err: %d", mp->retval);
Florin Corasa849b7b2018-05-18 00:41:55 -0700379 echo_main.state = STATE_DETACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700380}
381
Dave Barach68b0fb02017-02-28 15:15:56 -0500382static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500383stop_signal (int signum)
384{
Florin Corasb384b542018-01-15 01:08:33 -0800385 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500386
387 um->time_to_stop = 1;
388}
389
390static void
391stats_signal (int signum)
392{
Florin Corasb384b542018-01-15 01:08:33 -0800393 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500394
395 um->time_to_print_stats = 1;
396}
397
398static clib_error_t *
399setup_signal_handlers (void)
400{
401 signal (SIGINT, stats_signal);
402 signal (SIGQUIT, stop_signal);
403 signal (SIGTERM, stop_signal);
404
405 return 0;
406}
407
408void
409vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
410{
411 clib_warning ("BUG");
412}
413
414int
415connect_to_vpp (char *name)
416{
Florin Corasb384b542018-01-15 01:08:33 -0800417 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500418 api_main_t *am = &api_main;
419
Florin Corasb384b542018-01-15 01:08:33 -0800420 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800421 {
Florin Corasb384b542018-01-15 01:08:33 -0800422 if (vl_socket_client_connect ((char *) em->socket_name, name,
Florin Coras90a63982017-12-19 04:50:01 -0800423 0 /* default rx, tx buffer */ ))
Florin Corasb384b542018-01-15 01:08:33 -0800424 {
425 clib_warning ("socket connect failed");
426 return -1;
427 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500428
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100429 if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
Florin Corasb384b542018-01-15 01:08:33 -0800430 {
431 clib_warning ("init shm api failed");
432 return -1;
433 }
Florin Coras90a63982017-12-19 04:50:01 -0800434 }
435 else
436 {
437 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
Florin Corasb384b542018-01-15 01:08:33 -0800438 {
439 clib_warning ("shmem connect failed");
440 return -1;
441 }
Florin Coras90a63982017-12-19 04:50:01 -0800442 }
Florin Corasb384b542018-01-15 01:08:33 -0800443 em->vl_input_queue = am->shmem_hdr->vl_input_queue;
444 em->my_client_index = am->my_client_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500445 return 0;
446}
447
Florin Coras90a63982017-12-19 04:50:01 -0800448void
Florin Corasb384b542018-01-15 01:08:33 -0800449disconnect_from_vpp (echo_main_t * em)
Florin Coras90a63982017-12-19 04:50:01 -0800450{
Florin Corasb384b542018-01-15 01:08:33 -0800451 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800452 vl_socket_client_disconnect ();
453 else
454 vl_client_disconnect_from_vlib ();
455}
456
Dave Barach68b0fb02017-02-28 15:15:56 -0500457static void
Florin Corase04c2992017-03-01 08:17:34 -0800458vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500459{
Florin Corasadc74d72018-12-02 13:36:00 -0800460 svm_fifo_segment_main_t *sm = &echo_main.segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500461 svm_fifo_segment_create_args_t _a, *a = &_a;
462 int rv;
463
Dave Barachb7b92992018-10-17 10:38:51 -0400464 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500465 a->segment_name = (char *) mp->segment_name;
466 a->segment_size = mp->segment_size;
467 /* Attach to the segment vpp created */
Florin Corasadc74d72018-12-02 13:36:00 -0800468 rv = svm_fifo_segment_attach (sm, a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500469 if (rv)
470 {
471 clib_warning ("svm_fifo_segment_attach ('%s') failed",
Florin Corase04c2992017-03-01 08:17:34 -0800472 mp->segment_name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500473 return;
474 }
475 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
Florin Corase04c2992017-03-01 08:17:34 -0800476 mp->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500477}
478
479static void
Florin Coras9e47ac52019-01-24 23:22:37 -0800480session_print_stats (echo_main_t * em, echo_session_t * session)
Florin Corasf03a59a2017-06-09 21:07:32 -0700481{
482 f64 deltat;
483 u64 bytes;
484
Florin Corasb384b542018-01-15 01:08:33 -0800485 deltat = clib_time_now (&em->clib_time) - session->start;
486 bytes = em->i_am_master ? session->bytes_received : em->bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -0700487 fformat (stdout, "Finished in %.6f\n", deltat);
488 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
489}
490
491static void
Florin Coras9e47ac52019-01-24 23:22:37 -0800492test_recv_bytes (echo_session_t * s, u8 * rx_buf, u32 n_read)
Dave Barach68b0fb02017-02-28 15:15:56 -0500493{
Florin Corasa849b7b2018-05-18 00:41:55 -0700494 int i;
495 for (i = 0; i < n_read; i++)
496 {
497 if (rx_buf[i] != ((s->bytes_received + i) & 0xff))
498 {
499 clib_warning ("error at byte %lld, 0x%x not 0x%x",
500 s->bytes_received + i, rx_buf[i],
501 ((s->bytes_received + i) & 0xff));
502 }
503 }
504}
Dave Barach68b0fb02017-02-28 15:15:56 -0500505
Florin Corasa849b7b2018-05-18 00:41:55 -0700506static void
Florin Coras9e47ac52019-01-24 23:22:37 -0800507recv_data_chunk (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
Florin Corasa849b7b2018-05-18 00:41:55 -0700508{
Florin Coras9e47ac52019-01-24 23:22:37 -0800509 int n_to_read, n_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500510
Florin Coras9e47ac52019-01-24 23:22:37 -0800511 n_to_read = svm_fifo_max_dequeue (s->rx_fifo);
512 if (!n_to_read)
513 return;
Florin Coras6792ec02017-03-13 03:49:51 -0700514
Dave Barach68b0fb02017-02-28 15:15:56 -0500515 do
516 {
Florin Coras9e47ac52019-01-24 23:22:37 -0800517 n_read = app_recv_stream ((app_session_t *) s, rx_buf,
518 vec_len (rx_buf));
519
520 if (n_read > 0)
521 {
522 if (em->test_return_packets)
523 test_recv_bytes (s, rx_buf, n_read);
524
525 n_to_read -= n_read;
526
527 s->bytes_received += n_read;
528 s->bytes_to_receive -= n_read;
529 }
530 else
Florin Corasa849b7b2018-05-18 00:41:55 -0700531 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500532 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700533 while (n_to_read > 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500534}
535
536void
Florin Coras9e47ac52019-01-24 23:22:37 -0800537client_handle_rx (echo_main_t * em, session_event_t * e, u8 * rx_buf)
Dave Barach68b0fb02017-02-28 15:15:56 -0500538{
Florin Coras9e47ac52019-01-24 23:22:37 -0800539 echo_session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500540
Florin Corasa849b7b2018-05-18 00:41:55 -0700541 s = pool_elt_at_index (em->sessions, e->fifo->client_session_index);
Florin Coras9e47ac52019-01-24 23:22:37 -0800542 recv_data_chunk (em, s, rx_buf);
Florin Corasa849b7b2018-05-18 00:41:55 -0700543}
544
545static void
Florin Coras9e47ac52019-01-24 23:22:37 -0800546send_data_chunk (echo_main_t * em, echo_session_t * s)
Florin Corasa849b7b2018-05-18 00:41:55 -0700547{
548 u64 test_buf_len, bytes_this_chunk, test_buf_offset;
Florin Corasa849b7b2018-05-18 00:41:55 -0700549 u8 *test_data = em->connect_test_data;
Florin Coras9e47ac52019-01-24 23:22:37 -0800550 int n_sent;
Florin Corasa849b7b2018-05-18 00:41:55 -0700551
552 test_buf_len = vec_len (test_data);
553 test_buf_offset = s->bytes_sent % test_buf_len;
554 bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
555 s->bytes_to_send);
Florin Corasa849b7b2018-05-18 00:41:55 -0700556
Florin Coras9e47ac52019-01-24 23:22:37 -0800557 n_sent = app_send_stream ((app_session_t *) s, test_data + test_buf_offset,
558 bytes_this_chunk, 0);
Florin Corasa849b7b2018-05-18 00:41:55 -0700559
Florin Coras9e47ac52019-01-24 23:22:37 -0800560 if (n_sent > 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500561 {
Florin Coras9e47ac52019-01-24 23:22:37 -0800562 s->bytes_to_send -= n_sent;
563 s->bytes_sent += n_sent;
Dave Barach68b0fb02017-02-28 15:15:56 -0500564 }
565}
566
Florin Corasa849b7b2018-05-18 00:41:55 -0700567/*
568 * Rx/Tx polling thread per connection
569 */
Florin Corase04c2992017-03-01 08:17:34 -0800570static void *
Florin Corasa849b7b2018-05-18 00:41:55 -0700571client_thread_fn (void *arg)
572{
573 echo_main_t *em = &echo_main;
574 static u8 *rx_buf = 0;
575 u32 session_index = *(u32 *) arg;
Florin Coras9e47ac52019-01-24 23:22:37 -0800576 echo_session_t *s;
Florin Corasa849b7b2018-05-18 00:41:55 -0700577
578 vec_validate (rx_buf, 1 << 20);
579
580 while (!em->time_to_stop && em->state != STATE_READY)
581 ;
582
583 s = pool_elt_at_index (em->sessions, session_index);
584 while (!em->time_to_stop)
585 {
Florin Coras9e47ac52019-01-24 23:22:37 -0800586 send_data_chunk (em, s);
587 recv_data_chunk (em, s, rx_buf);
Florin Corasa849b7b2018-05-18 00:41:55 -0700588 if (!s->bytes_to_send && !s->bytes_to_receive)
589 break;
590 }
591
592 DBG ("session %d done", session_index);
593 em->tx_total += s->bytes_sent;
594 em->rx_total += s->bytes_received;
595 em->n_active_clients--;
596
597 pthread_exit (0);
598}
599
600/*
601 * Rx thread that handles all connections.
602 *
603 * Not used.
604 */
605void *
Florin Corase04c2992017-03-01 08:17:34 -0800606client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500607{
Florin Coras52207f12018-07-12 14:48:06 -0700608 session_event_t _e, *e = &_e;
Florin Corasb384b542018-01-15 01:08:33 -0800609 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -0700610 static u8 *rx_buf = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700611 svm_msg_q_msg_t msg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500612
Florin Corasa849b7b2018-05-18 00:41:55 -0700613 vec_validate (rx_buf, 1 << 20);
614
615 while (!em->time_to_stop && em->state != STATE_READY)
616 ;
617
618 while (!em->time_to_stop)
Dave Barach68b0fb02017-02-28 15:15:56 -0500619 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700620 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
621 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500622 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800623 {
Florin Corasa5464812017-04-19 13:00:05 -0700624 case FIFO_EVENT_APP_RX:
Florin Coras9e47ac52019-01-24 23:22:37 -0800625 client_handle_rx (em, e, rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800626 break;
Florin Corase04c2992017-03-01 08:17:34 -0800627 default:
628 clib_warning ("unknown event type %d", e->event_type);
629 break;
630 }
Florin Coras3c2fed52018-07-04 04:15:05 -0700631 svm_msg_q_free_msg (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500632 }
Florin Corase04c2992017-03-01 08:17:34 -0800633 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500634}
635
Florin Coras52207f12018-07-12 14:48:06 -0700636void
637client_send_connect (echo_main_t * em)
638{
639 vl_api_connect_uri_t *cmp;
640 cmp = vl_msg_api_alloc (sizeof (*cmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400641 clib_memset (cmp, 0, sizeof (*cmp));
Florin Coras52207f12018-07-12 14:48:06 -0700642
643 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
644 cmp->client_index = em->my_client_index;
645 cmp->context = ntohl (0xfeedface);
646 memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri));
647 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
648}
649
650void
Florin Coras9e47ac52019-01-24 23:22:37 -0800651client_send_disconnect (echo_main_t * em, echo_session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700652{
653 vl_api_disconnect_session_t *dmp;
654 dmp = vl_msg_api_alloc (sizeof (*dmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400655 clib_memset (dmp, 0, sizeof (*dmp));
Florin Coras52207f12018-07-12 14:48:06 -0700656 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
657 dmp->client_index = em->my_client_index;
658 dmp->handle = s->vpp_session_handle;
659 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
660}
661
662int
Florin Coras9e47ac52019-01-24 23:22:37 -0800663client_disconnect (echo_main_t * em, echo_session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700664{
665 client_send_disconnect (em, s);
666 pool_put (em->sessions, s);
Dave Barachb7b92992018-10-17 10:38:51 -0400667 clib_memset (s, 0xfe, sizeof (*s));
Florin Coras52207f12018-07-12 14:48:06 -0700668 return 0;
669}
670
Dave Barach68b0fb02017-02-28 15:15:56 -0500671static void
Florin Coras9e47ac52019-01-24 23:22:37 -0800672session_bound_handler (session_bound_msg_t * mp)
673{
674 echo_main_t *em = &echo_main;
675
676 if (mp->retval)
677 {
678 clib_warning ("bind failed: %U", format_api_error,
679 clib_net_to_host_u32 (mp->retval));
680 em->state = STATE_FAILED;
681 return;
682 }
683
684 clib_warning ("listening on %U:%u", format_ip46_address, mp->lcl_ip,
685 mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, mp->lcl_port);
686 em->state = STATE_READY;
687}
688
689static void
Florin Coras52207f12018-07-12 14:48:06 -0700690session_accepted_handler (session_accepted_msg_t * mp)
691{
692 app_session_evt_t _app_evt, *app_evt = &_app_evt;
693 session_accepted_reply_msg_t *rmp;
694 svm_fifo_t *rx_fifo, *tx_fifo;
695 echo_main_t *em = &echo_main;
Florin Coras9e47ac52019-01-24 23:22:37 -0800696 echo_session_t *session;
Florin Coras52207f12018-07-12 14:48:06 -0700697 static f64 start_time;
698 u32 session_index;
699 u8 *ip_str;
700
701 if (start_time == 0.0)
702 start_time = clib_time_now (&em->clib_time);
703
704 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
705 clib_warning ("Accepted session from: %s:%d", ip_str,
706 clib_net_to_host_u16 (mp->port));
707
708 /* Allocate local session and set it up */
709 pool_get (em->sessions, session);
710 session_index = session - em->sessions;
711
712 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
713 rx_fifo->client_session_index = session_index;
714 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
715 tx_fifo->client_session_index = session_index;
716
Florin Coras9e47ac52019-01-24 23:22:37 -0800717 session->rx_fifo = rx_fifo;
718 session->tx_fifo = tx_fifo;
Florin Coras52207f12018-07-12 14:48:06 -0700719 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
720 svm_msg_q_t *);
721
722 /* Add it to lookup table */
723 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
724
725 em->state = STATE_READY;
726
727 /* Stats printing */
728 if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
729 {
730 f64 now = clib_time_now (&em->clib_time);
731 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
732 pool_elts (em->sessions), now - start_time,
733 (f64) pool_elts (em->sessions) / (now - start_time));
734 }
735
736 /*
737 * Send accept reply to vpp
738 */
739 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
740 SESSION_CTRL_EVT_ACCEPTED_REPLY);
741 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
742 rmp->handle = mp->handle;
743 rmp->context = mp->context;
744 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
745
746 session->bytes_received = 0;
747 session->start = clib_time_now (&em->clib_time);
748}
749
750static void
751session_connected_handler (session_connected_msg_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500752{
Florin Corasb384b542018-01-15 01:08:33 -0800753 echo_main_t *em = &echo_main;
Florin Coras9e47ac52019-01-24 23:22:37 -0800754 echo_session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500755 u32 session_index;
756 svm_fifo_t *rx_fifo, *tx_fifo;
757 int rv;
758
759 if (mp->retval)
760 {
Florin Corasa5464812017-04-19 13:00:05 -0700761 clib_warning ("connection failed with code: %U", format_api_error,
762 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800763 em->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500764 return;
765 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500766
767 /*
768 * Setup session
769 */
770
Florin Corasb384b542018-01-15 01:08:33 -0800771 pool_get (em->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -0400772 clib_memset (session, 0, sizeof (*session));
Florin Corasb384b542018-01-15 01:08:33 -0800773 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -0500774
Damjan Marion7bee80c2017-04-26 15:32:12 +0200775 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500776 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200777 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500778 tx_fifo->client_session_index = session_index;
779
Florin Coras9e47ac52019-01-24 23:22:37 -0800780 session->rx_fifo = rx_fifo;
781 session->tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700782 session->vpp_session_handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800783 session->start = clib_time_now (&em->clib_time);
Florin Corasa849b7b2018-05-18 00:41:55 -0700784 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
Florin Coras3c2fed52018-07-04 04:15:05 -0700785 svm_msg_q_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500786
Florin Corasb384b542018-01-15 01:08:33 -0800787 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800788
Florin Corasa849b7b2018-05-18 00:41:55 -0700789 /*
790 * Start RX thread
791 */
792 em->thread_args[em->n_clients_connected] = session_index;
793 rv = pthread_create (&em->client_thread_handles[em->n_clients_connected],
794 NULL /*attr */ , client_thread_fn,
795 (void *) &em->thread_args[em->n_clients_connected]);
Florin Corase04c2992017-03-01 08:17:34 -0800796 if (rv)
797 {
798 clib_warning ("pthread_create returned %d", rv);
Florin Corasa849b7b2018-05-18 00:41:55 -0700799 return;
Florin Coras6792ec02017-03-13 03:49:51 -0700800 }
801
Florin Corasa849b7b2018-05-18 00:41:55 -0700802 em->n_clients_connected += 1;
803 clib_warning ("session %u (0x%llx) connected with local ip %U port %d",
804 session_index, mp->handle, format_ip46_address, mp->lcl_ip,
805 mp->is_ip4, clib_net_to_host_u16 (mp->lcl_port));
Florin Corase04c2992017-03-01 08:17:34 -0800806}
807
Florin Coras52207f12018-07-12 14:48:06 -0700808static void
809session_disconnected_handler (session_disconnected_msg_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800810{
Florin Coras52207f12018-07-12 14:48:06 -0700811 app_session_evt_t _app_evt, *app_evt = &_app_evt;
812 session_disconnected_reply_msg_t *rmp;
813 echo_main_t *em = &echo_main;
Florin Coras9e47ac52019-01-24 23:22:37 -0800814 echo_session_t *session = 0;
Florin Coras52207f12018-07-12 14:48:06 -0700815 uword *p;
816 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800817
Florin Coras52207f12018-07-12 14:48:06 -0700818 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700819 if (!p)
Florin Coras52207f12018-07-12 14:48:06 -0700820 {
821 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700822 return;
Florin Coras52207f12018-07-12 14:48:06 -0700823 }
824
Florin Coras3d0fadc2018-07-17 05:35:47 -0700825 session = pool_elt_at_index (em->sessions, p[0]);
826 hash_unset (em->session_index_by_vpp_handles, mp->handle);
827 pool_put (em->sessions, session);
828
Florin Coras52207f12018-07-12 14:48:06 -0700829 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
830 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
831 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
832 rmp->retval = rv;
833 rmp->handle = mp->handle;
834 rmp->context = mp->context;
835 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
836
Florin Coras3d0fadc2018-07-17 05:35:47 -0700837 session_print_stats (em, session);
Florin Corase04c2992017-03-01 08:17:34 -0800838}
839
Florin Coras52207f12018-07-12 14:48:06 -0700840static void
841session_reset_handler (session_reset_msg_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800842{
Florin Coras52207f12018-07-12 14:48:06 -0700843 app_session_evt_t _app_evt, *app_evt = &_app_evt;
844 echo_main_t *em = &echo_main;
845 session_reset_reply_msg_t *rmp;
Florin Coras9e47ac52019-01-24 23:22:37 -0800846 echo_session_t *session = 0;
Florin Coras52207f12018-07-12 14:48:06 -0700847 uword *p;
848 int rv = 0;
849
850 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
851
852 if (p)
853 {
854 session = pool_elt_at_index (em->sessions, p[0]);
855 clib_warning ("got reset");
856 /* Cleanup later */
857 em->time_to_stop = 1;
858 }
859 else
860 {
861 clib_warning ("couldn't find session key %llx", mp->handle);
862 return;
863 }
864
865 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
866 SESSION_CTRL_EVT_RESET_REPLY);
867 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
868 rmp->retval = rv;
869 rmp->handle = mp->handle;
870 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
Florin Corase04c2992017-03-01 08:17:34 -0800871}
872
Florin Coras52207f12018-07-12 14:48:06 -0700873static void
874handle_mq_event (session_event_t * e)
Florin Corasa5464812017-04-19 13:00:05 -0700875{
Florin Coras52207f12018-07-12 14:48:06 -0700876 switch (e->event_type)
877 {
Florin Coras9e47ac52019-01-24 23:22:37 -0800878 case SESSION_CTRL_EVT_BOUND:
879 session_bound_handler ((session_bound_msg_t *) e->data);
880 break;
Florin Coras52207f12018-07-12 14:48:06 -0700881 case SESSION_CTRL_EVT_ACCEPTED:
882 session_accepted_handler ((session_accepted_msg_t *) e->data);
883 break;
884 case SESSION_CTRL_EVT_CONNECTED:
885 session_connected_handler ((session_connected_msg_t *) e->data);
886 break;
887 case SESSION_CTRL_EVT_DISCONNECTED:
888 session_disconnected_handler ((session_disconnected_msg_t *) e->data);
889 break;
890 case SESSION_CTRL_EVT_RESET:
891 session_reset_handler ((session_reset_msg_t *) e->data);
892 break;
893 default:
894 clib_warning ("unhandled %u", e->event_type);
895 }
Florin Corasa5464812017-04-19 13:00:05 -0700896}
897
Florin Corase04c2992017-03-01 08:17:34 -0800898static void
Florin Corasa849b7b2018-05-18 00:41:55 -0700899clients_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800900{
Florin Corasa849b7b2018-05-18 00:41:55 -0700901 f64 start_time, deltat, timeout = 100.0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700902 svm_msg_q_msg_t msg;
Florin Coras52207f12018-07-12 14:48:06 -0700903 session_event_t *e;
Florin Coras9e47ac52019-01-24 23:22:37 -0800904 echo_session_t *s;
Florin Corase04c2992017-03-01 08:17:34 -0800905 int i;
906
Florin Corase04c2992017-03-01 08:17:34 -0800907 /* Init test data */
Florin Coras8e43d042018-05-04 15:46:57 -0700908 vec_validate (em->connect_test_data, 1024 * 1024 - 1);
Florin Corasb384b542018-01-15 01:08:33 -0800909 for (i = 0; i < vec_len (em->connect_test_data); i++)
910 em->connect_test_data[i] = i & 0xff;
Florin Corase04c2992017-03-01 08:17:34 -0800911
Florin Corasa849b7b2018-05-18 00:41:55 -0700912 /*
913 * Attach and connect the clients
914 */
915 if (application_attach (em))
916 return;
Florin Corase04c2992017-03-01 08:17:34 -0800917
Florin Corasa849b7b2018-05-18 00:41:55 -0700918 for (i = 0; i < em->n_clients; i++)
919 client_send_connect (em);
920
921 start_time = clib_time_now (&em->clib_time);
922 while (em->n_clients_connected < em->n_clients
923 && (clib_time_now (&em->clib_time) - start_time < timeout)
924 && em->state != STATE_FAILED)
Florin Coras52207f12018-07-12 14:48:06 -0700925
926 {
927 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
928 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
929 handle_mq_event (e);
930 svm_msg_q_free_msg (em->our_event_queue, &msg);
931 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700932
933 if (em->n_clients_connected != em->n_clients)
934 {
935 clib_warning ("failed to initialize all connections");
936 return;
937 }
938
939 /*
940 * Initialize connections
941 */
942 for (i = 0; i < em->n_clients; i++)
943 {
944 s = pool_elt_at_index (em->sessions, i);
945 s->bytes_to_send = em->bytes_to_send;
946 if (!em->no_return)
947 s->bytes_to_receive = em->bytes_to_send;
948 }
949 em->n_active_clients = em->n_clients_connected;
950
951 /*
952 * Wait for client threads to send the data
953 */
954 start_time = clib_time_now (&em->clib_time);
955 em->state = STATE_READY;
956 while (em->n_active_clients)
Florin Coras3c2fed52018-07-04 04:15:05 -0700957 if (!svm_msg_q_is_empty (em->our_event_queue))
958 {
959 if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0))
960 {
961 clib_warning ("svm msg q returned");
Florin Coras52207f12018-07-12 14:48:06 -0700962 continue;
Florin Coras3c2fed52018-07-04 04:15:05 -0700963 }
Florin Coras52207f12018-07-12 14:48:06 -0700964 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
965 if (e->event_type != FIFO_EVENT_APP_RX)
966 handle_mq_event (e);
967 svm_msg_q_free_msg (em->our_event_queue, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -0700968 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700969
970 for (i = 0; i < em->n_clients; i++)
971 {
972 s = pool_elt_at_index (em->sessions, i);
973 client_disconnect (em, s);
974 }
975
976 /*
977 * Stats and detach
978 */
979 deltat = clib_time_now (&em->clib_time) - start_time;
980 fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds\n",
981 em->tx_total, em->tx_total / (1ULL << 20),
982 em->tx_total / (1ULL << 30), deltat);
983 fformat (stdout, "%.4f Gbit/second\n", (em->tx_total * 8.0) / deltat / 1e9);
984
Florin Corasb384b542018-01-15 01:08:33 -0800985 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -0800986}
987
988static void
989vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
990{
Florin Corasb384b542018-01-15 01:08:33 -0800991 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800992
993 if (mp->retval)
994 {
flyingeagle23a07779f2017-04-26 20:22:04 +0800995 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -0700996 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800997 em->state = STATE_FAILED;
Florin Corase04c2992017-03-01 08:17:34 -0800998 return;
999 }
1000
Florin Corasb384b542018-01-15 01:08:33 -08001001 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001002}
1003
Dave Barach68b0fb02017-02-28 15:15:56 -05001004static void
Florin Corase04c2992017-03-01 08:17:34 -08001005vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -05001006{
Florin Corasb384b542018-01-15 01:08:33 -08001007 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001008
1009 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -08001010 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -05001011
Florin Corasb384b542018-01-15 01:08:33 -08001012 em->state = STATE_START;
Dave Barach68b0fb02017-02-28 15:15:56 -05001013}
1014
Florin Coras6cf30ad2017-04-04 23:08:23 -07001015u8 *
1016format_ip4_address (u8 * s, va_list * args)
1017{
1018 u8 *a = va_arg (*args, u8 *);
1019 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1020}
1021
1022u8 *
1023format_ip6_address (u8 * s, va_list * args)
1024{
1025 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1026 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1027
1028 i_max_n_zero = ARRAY_LEN (a->as_u16);
1029 max_n_zeros = 0;
1030 i_first_zero = i_max_n_zero;
1031 n_zeros = 0;
1032 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1033 {
1034 u32 is_zero = a->as_u16[i] == 0;
1035 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1036 {
1037 i_first_zero = i;
1038 n_zeros = 0;
1039 }
1040 n_zeros += is_zero;
1041 if ((!is_zero && n_zeros > max_n_zeros)
1042 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1043 {
1044 i_max_n_zero = i_first_zero;
1045 max_n_zeros = n_zeros;
1046 i_first_zero = ARRAY_LEN (a->as_u16);
1047 n_zeros = 0;
1048 }
1049 }
1050
1051 last_double_colon = 0;
1052 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1053 {
1054 if (i == i_max_n_zero && max_n_zeros > 1)
1055 {
1056 s = format (s, "::");
1057 i += max_n_zeros - 1;
1058 last_double_colon = 1;
1059 }
1060 else
1061 {
1062 s = format (s, "%s%x",
1063 (last_double_colon || i == 0) ? "" : ":",
1064 clib_net_to_host_u16 (a->as_u16[i]));
1065 last_double_colon = 0;
1066 }
1067 }
1068
1069 return s;
1070}
1071
1072/* Format an IP46 address. */
1073u8 *
1074format_ip46_address (u8 * s, va_list * args)
1075{
1076 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1077 ip46_type_t type = va_arg (*args, ip46_type_t);
1078 int is_ip4 = 1;
1079
1080 switch (type)
1081 {
1082 case IP46_TYPE_ANY:
1083 is_ip4 = ip46_address_is_ip4 (ip46);
1084 break;
1085 case IP46_TYPE_IP4:
1086 is_ip4 = 1;
1087 break;
1088 case IP46_TYPE_IP6:
1089 is_ip4 = 0;
1090 break;
1091 }
1092
1093 return is_ip4 ?
1094 format (s, "%U", format_ip4_address, &ip46->ip4) :
1095 format (s, "%U", format_ip6_address, &ip46->ip6);
1096}
1097
Dave Barach68b0fb02017-02-28 15:15:56 -05001098static void
Florin Coras9e47ac52019-01-24 23:22:37 -08001099server_handle_rx (echo_main_t * em, session_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -05001100{
Florin Coras9e47ac52019-01-24 23:22:37 -08001101 int n_read, max_dequeue, n_sent;
1102 u32 offset, to_dequeue;
1103 echo_session_t *s;
Florin Corase04c2992017-03-01 08:17:34 -08001104
Florin Coras9e47ac52019-01-24 23:22:37 -08001105 s = pool_elt_at_index (em->sessions, e->fifo->client_session_index);
Florin Corase04c2992017-03-01 08:17:34 -08001106
Florin Coras9e47ac52019-01-24 23:22:37 -08001107 /* Clear event only once. Otherwise, if we do it in the loop by calling
1108 * app_recv_stream, we may end up with a lot of unhandled rx events on the
1109 * message queue */
1110 svm_fifo_unset_event (s->rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -07001111
Florin Coras9e47ac52019-01-24 23:22:37 -08001112 max_dequeue = svm_fifo_max_dequeue (s->rx_fifo);
Florin Corasa849b7b2018-05-18 00:41:55 -07001113 if (PREDICT_FALSE (!max_dequeue))
1114 return;
Florin Coras6792ec02017-03-13 03:49:51 -07001115
Florin Corase04c2992017-03-01 08:17:34 -08001116 do
1117 {
Florin Coras9e47ac52019-01-24 23:22:37 -08001118 /* The options here are to limit ourselves to max_dequeue or read
1119 * even the data that was enqueued while we were dequeueing and which
1120 * now has an rx event in the mq. Either of the two work. */
1121 to_dequeue = clib_min (max_dequeue, vec_len (em->rx_buf));
1122 n_read = app_recv_stream_raw (s->rx_fifo, em->rx_buf, to_dequeue,
1123 0 /* clear evt */ , 0 /* peek */ );
Florin Coras6792ec02017-03-13 03:49:51 -07001124 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -08001125 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001126 max_dequeue -= n_read;
Florin Coras9e47ac52019-01-24 23:22:37 -08001127 s->bytes_received += n_read;
Florin Corasf03a59a2017-06-09 21:07:32 -07001128 }
Florin Coras9e47ac52019-01-24 23:22:37 -08001129 else
1130 break;
Florin Corasf03a59a2017-06-09 21:07:32 -07001131
1132 /* Reflect if a non-drop session */
Florin Coras8e43d042018-05-04 15:46:57 -07001133 if (!em->no_return && n_read > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001134 {
1135 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001136 do
1137 {
Florin Coras9e47ac52019-01-24 23:22:37 -08001138 n_sent = app_send_stream ((app_session_t *) s,
1139 &em->rx_buf[offset],
1140 n_read, SVM_Q_WAIT);
1141 if (n_sent > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001142 {
Florin Coras9e47ac52019-01-24 23:22:37 -08001143 n_read -= n_sent;
1144 offset += n_sent;
Florin Corasf03a59a2017-06-09 21:07:32 -07001145 }
Florin Corase04c2992017-03-01 08:17:34 -08001146 }
Florin Coras9e47ac52019-01-24 23:22:37 -08001147 while ((n_sent <= 0 || n_read > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001148 }
Florin Corase04c2992017-03-01 08:17:34 -08001149 }
Florin Coras9e47ac52019-01-24 23:22:37 -08001150 while (max_dequeue > 0 && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001151}
1152
Florin Coras52207f12018-07-12 14:48:06 -07001153static void
Florin Coras9e47ac52019-01-24 23:22:37 -08001154server_handle_mq (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001155{
Florin Coras3c2fed52018-07-04 04:15:05 -07001156 svm_msg_q_msg_t msg;
Florin Coras52207f12018-07-12 14:48:06 -07001157 session_event_t *e;
Florin Corase04c2992017-03-01 08:17:34 -08001158
1159 while (1)
1160 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001161 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
1162 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
Florin Corase04c2992017-03-01 08:17:34 -08001163 switch (e->event_type)
1164 {
Florin Corasa5464812017-04-19 13:00:05 -07001165 case FIFO_EVENT_APP_RX:
Florin Coras9e47ac52019-01-24 23:22:37 -08001166 server_handle_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -08001167 break;
Florin Corase04c2992017-03-01 08:17:34 -08001168 default:
Florin Coras52207f12018-07-12 14:48:06 -07001169 handle_mq_event (e);
Florin Corase04c2992017-03-01 08:17:34 -08001170 break;
1171 }
Florin Corasb384b542018-01-15 01:08:33 -08001172 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001173 break;
Florin Corasb384b542018-01-15 01:08:33 -08001174 if (PREDICT_FALSE (em->time_to_print_stats == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001175 {
Florin Corasb384b542018-01-15 01:08:33 -08001176 em->time_to_print_stats = 0;
1177 fformat (stdout, "%d connections\n", pool_elts (em->sessions));
Florin Corase04c2992017-03-01 08:17:34 -08001178 }
Florin Coras3c2fed52018-07-04 04:15:05 -07001179 svm_msg_q_free_msg (em->our_event_queue, &msg);
Florin Corase04c2992017-03-01 08:17:34 -08001180 }
1181}
1182
1183void
Florin Corasb384b542018-01-15 01:08:33 -08001184server_send_listen (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001185{
1186 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001187 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001188 clib_memset (bmp, 0, sizeof (*bmp));
Florin Corase04c2992017-03-01 08:17:34 -08001189
1190 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001191 bmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -08001192 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -08001193 memcpy (bmp->uri, em->uri, vec_len (em->uri));
1194 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase04c2992017-03-01 08:17:34 -08001195}
1196
Florin Corasa5464812017-04-19 13:00:05 -07001197int
Florin Corasb384b542018-01-15 01:08:33 -08001198server_listen (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001199{
Florin Corasb384b542018-01-15 01:08:33 -08001200 server_send_listen (em);
1201 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -07001202 {
1203 clib_warning ("timeout waiting for STATE_READY");
1204 return -1;
1205 }
1206 return 0;
1207}
1208
Florin Corase04c2992017-03-01 08:17:34 -08001209void
Florin Corasb384b542018-01-15 01:08:33 -08001210server_send_unbind (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001211{
1212 vl_api_unbind_uri_t *ump;
1213
1214 ump = vl_msg_api_alloc (sizeof (*ump));
Dave Barachb7b92992018-10-17 10:38:51 -04001215 clib_memset (ump, 0, sizeof (*ump));
Florin Corase04c2992017-03-01 08:17:34 -08001216
1217 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001218 ump->client_index = em->my_client_index;
1219 memcpy (ump->uri, em->uri, vec_len (em->uri));
1220 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
Florin Corase04c2992017-03-01 08:17:34 -08001221}
1222
Florin Corasa5464812017-04-19 13:00:05 -07001223int
Florin Corasb384b542018-01-15 01:08:33 -08001224server_unbind (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001225{
Florin Corasb384b542018-01-15 01:08:33 -08001226 server_send_unbind (em);
1227 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -07001228 {
1229 clib_warning ("timeout waiting for STATE_START");
1230 return -1;
1231 }
1232 return 0;
1233}
1234
Florin Corase04c2992017-03-01 08:17:34 -08001235void
Florin Corasb384b542018-01-15 01:08:33 -08001236server_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001237{
Florin Coras9e47ac52019-01-24 23:22:37 -08001238 echo_session_t *session;
Florin Coras90a63982017-12-19 04:50:01 -08001239 int i;
1240
1241 /* $$$$ hack preallocation */
1242 for (i = 0; i < 200000; i++)
1243 {
Florin Corasb384b542018-01-15 01:08:33 -08001244 pool_get (em->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -04001245 clib_memset (session, 0, sizeof (*session));
Florin Coras90a63982017-12-19 04:50:01 -08001246 }
1247 for (i = 0; i < 200000; i++)
Florin Corasb384b542018-01-15 01:08:33 -08001248 pool_put_index (em->sessions, i);
Florin Coras90a63982017-12-19 04:50:01 -08001249
Florin Corasb384b542018-01-15 01:08:33 -08001250 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -07001251 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001252
Dave Barach68b0fb02017-02-28 15:15:56 -05001253 /* Bind to uri */
Florin Corasb384b542018-01-15 01:08:33 -08001254 if (server_listen (em))
Florin Corasa5464812017-04-19 13:00:05 -07001255 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001256
1257 /* Enter handle event loop */
Florin Coras9e47ac52019-01-24 23:22:37 -08001258 server_handle_mq (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001259
1260 /* Cleanup */
Florin Corasb384b542018-01-15 01:08:33 -08001261 server_send_unbind (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001262
Florin Corasb384b542018-01-15 01:08:33 -08001263 application_detach (em);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001264
Dave Barach68b0fb02017-02-28 15:15:56 -05001265 fformat (stdout, "Test complete...\n");
1266}
1267
Florin Corase69f4952017-03-07 10:06:24 -08001268static void
1269vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1270 mp)
1271{
Florin Corasb384b542018-01-15 01:08:33 -08001272 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -07001273 uword *p;
Florin Coras6792ec02017-03-13 03:49:51 -07001274
Florin Corasf03a59a2017-06-09 21:07:32 -07001275 if (mp->retval)
1276 {
1277 clib_warning ("vpp complained about disconnect: %d",
1278 ntohl (mp->retval));
Florin Corasa849b7b2018-05-18 00:41:55 -07001279 return;
Florin Corasf03a59a2017-06-09 21:07:32 -07001280 }
1281
Florin Corasb384b542018-01-15 01:08:33 -08001282 em->state = STATE_START;
Florin Corasa849b7b2018-05-18 00:41:55 -07001283
1284 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
1285 if (p)
1286 {
1287 hash_unset (em->session_index_by_vpp_handles, mp->handle);
1288 }
1289 else
1290 {
1291 clib_warning ("couldn't find session key %llx", mp->handle);
1292 }
Florin Corase69f4952017-03-07 10:06:24 -08001293}
1294
Florin Corase3e2f072018-03-04 07:24:30 -08001295static void
1296 vl_api_application_tls_cert_add_reply_t_handler
1297 (vl_api_application_tls_cert_add_reply_t * mp)
1298{
1299 if (mp->retval)
1300 clib_warning ("failed to add tls cert");
1301}
1302
1303static void
1304 vl_api_application_tls_key_add_reply_t_handler
1305 (vl_api_application_tls_key_add_reply_t * mp)
1306{
1307 if (mp->retval)
1308 clib_warning ("failed to add tls key");
1309}
1310
1311#define foreach_tcp_echo_msg \
1312_(BIND_URI_REPLY, bind_uri_reply) \
1313_(UNBIND_URI_REPLY, unbind_uri_reply) \
Florin Corase3e2f072018-03-04 07:24:30 -08001314_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Corase3e2f072018-03-04 07:24:30 -08001315_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1316_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1317_(MAP_ANOTHER_SEGMENT, map_another_segment) \
1318_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
1319_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001320
1321void
Florin Corasb384b542018-01-15 01:08:33 -08001322tcp_echo_api_hookup (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -05001323{
1324#define _(N,n) \
1325 vl_msg_api_set_handlers(VL_API_##N, #n, \
1326 vl_api_##n##_t_handler, \
1327 vl_noop_handler, \
1328 vl_api_##n##_t_endian, \
1329 vl_api_##n##_t_print, \
1330 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -08001331 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -05001332#undef _
1333}
1334
1335int
1336main (int argc, char **argv)
1337{
Florin Coras8e43d042018-05-04 15:46:57 -07001338 int i_am_server = 1, test_return_packets = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001339 echo_main_t *em = &echo_main;
Florin Corasadc74d72018-12-02 13:36:00 -08001340 svm_fifo_segment_main_t *sm = &em->segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001341 unformat_input_t _argv, *a = &_argv;
1342 u8 *chroot_prefix;
Dave Barach6a5adc32018-07-04 10:56:23 -04001343 u8 *uri = 0;
Florin Corase69f4952017-03-07 10:06:24 -08001344 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
Florin Coras8e43d042018-05-04 15:46:57 -07001345 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001346 u64 bytes_to_send = 64 << 10, mbytes;
Florin Corasb384b542018-01-15 01:08:33 -08001347 char *app_name;
Dave Barach68b0fb02017-02-28 15:15:56 -05001348 u32 tmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001349
Dave Barach6a5adc32018-07-04 10:56:23 -04001350 clib_mem_init_thread_safe (0, 256 << 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001351
Dave Barachb7b92992018-10-17 10:38:51 -04001352 clib_memset (em, 0, sizeof (*em));
Florin Corasb384b542018-01-15 01:08:33 -08001353 em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Corasb384b542018-01-15 01:08:33 -08001354 em->my_pid = getpid ();
1355 em->configured_segment_size = 1 << 20;
1356 em->socket_name = 0;
1357 em->use_sock_api = 1;
Florin Coras8e43d042018-05-04 15:46:57 -07001358 em->fifo_size = 64 << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001359 em->n_clients = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001360
Florin Corasb384b542018-01-15 01:08:33 -08001361 clib_time_init (&em->clib_time);
1362 init_error_string_table (em);
David Johnsond9818dd2018-12-14 14:53:41 -05001363 svm_fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001364 unformat_init_command_line (a, argv);
1365
1366 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1367 {
1368 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001369 {
1370 vl_set_memory_root_path ((char *) chroot_prefix);
1371 }
Florin Corase69f4952017-03-07 10:06:24 -08001372 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001373 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001374 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001375 em->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001376 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001377 em->configured_segment_size = tmp << 30;
Florin Coras8e43d042018-05-04 15:46:57 -07001378 else if (unformat (a, "server"))
1379 i_am_server = 1;
1380 else if (unformat (a, "client"))
1381 i_am_server = 0;
1382 else if (unformat (a, "no-return"))
1383 em->no_return = 1;
Florin Corase04c2992017-03-01 08:17:34 -08001384 else if (unformat (a, "test"))
1385 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001386 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001387 {
1388 bytes_to_send = mbytes << 20;
1389 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001390 else if (unformat (a, "gbytes %lld", &mbytes))
1391 {
1392 bytes_to_send = mbytes << 30;
1393 }
Florin Corasb384b542018-01-15 01:08:33 -08001394 else if (unformat (a, "socket-name %s", &em->socket_name))
Florin Coras90a63982017-12-19 04:50:01 -08001395 ;
1396 else if (unformat (a, "use-svm-api"))
Florin Corasb384b542018-01-15 01:08:33 -08001397 em->use_sock_api = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001398 else if (unformat (a, "fifo-size %d", &tmp))
1399 em->fifo_size = tmp << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001400 else if (unformat (a, "nclients %d", &em->n_clients))
1401 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001402 else
Florin Corase04c2992017-03-01 08:17:34 -08001403 {
Jerome Tollet61f79122018-06-04 08:31:33 +01001404 fformat (stderr, "%s: usage [master|slave]\n", argv[0]);
Florin Corase04c2992017-03-01 08:17:34 -08001405 exit (1);
1406 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001407 }
1408
Florin Corasb384b542018-01-15 01:08:33 -08001409 if (!em->socket_name)
1410 em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
Florin Coras90a63982017-12-19 04:50:01 -08001411
Florin Corase69f4952017-03-07 10:06:24 -08001412 if (uri)
1413 {
Florin Corasb384b542018-01-15 01:08:33 -08001414 em->uri = format (0, "%s%c", uri, 0);
1415 em->connect_uri = format (0, "%s%c", uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001416 }
1417 else
1418 {
Florin Corasb384b542018-01-15 01:08:33 -08001419 em->uri = format (0, "%s%c", bind_uri, 0);
1420 em->connect_uri = format (0, "%s%c", connect_uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001421 }
1422
Florin Coras8e43d042018-05-04 15:46:57 -07001423 em->i_am_master = i_am_server;
Florin Corasb384b542018-01-15 01:08:33 -08001424 em->test_return_packets = test_return_packets;
1425 em->bytes_to_send = bytes_to_send;
1426 em->time_to_stop = 0;
Florin Coras9e47ac52019-01-24 23:22:37 -08001427 vec_validate (em->rx_buf, 4 << 20);
Florin Corasa849b7b2018-05-18 00:41:55 -07001428 vec_validate (em->client_thread_handles, em->n_clients - 1);
1429 vec_validate (em->thread_args, em->n_clients - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001430
Florin Corase04c2992017-03-01 08:17:34 -08001431 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001432 tcp_echo_api_hookup (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001433
Florin Coras8e43d042018-05-04 15:46:57 -07001434 app_name = i_am_server ? "tcp_echo_server" : "tcp_echo_client";
Florin Corasb384b542018-01-15 01:08:33 -08001435 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001436 {
1437 svm_region_exit ();
1438 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1439 exit (1);
1440 }
1441
Florin Coras8e43d042018-05-04 15:46:57 -07001442 if (i_am_server == 0)
Florin Corasa849b7b2018-05-18 00:41:55 -07001443 clients_run (em);
Florin Coras90a63982017-12-19 04:50:01 -08001444 else
Florin Corasb384b542018-01-15 01:08:33 -08001445 server_run (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001446
Florin Corasa849b7b2018-05-18 00:41:55 -07001447 /* Make sure detach finishes */
1448 wait_for_state_change (em, STATE_DETACHED);
1449
Florin Corasb384b542018-01-15 01:08:33 -08001450 disconnect_from_vpp (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001451 exit (0);
1452}
Florin Corase04c2992017-03-01 08:17:34 -08001453
1454/*
1455 * fd.io coding-style-patch-verification: ON
1456 *
1457 * Local Variables:
1458 * eval: (c-set-style "gnu")
1459 * End:
1460 */