blob: bde55ad37f43f9d05c5e1c572e3c741346e27b2d [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 Corase04c2992017-03-01 08:17:34 -080048 svm_fifo_t *server_rx_fifo;
49 svm_fifo_t *server_tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -050050
Florin Coras3c2fed52018-07-04 04:15:05 -070051 svm_msg_q_t *vpp_evt_q;
Florin Corasa849b7b2018-05-18 00:41:55 -070052
Florin Corasa5464812017-04-19 13:00:05 -070053 u64 vpp_session_handle;
Florin Corasa849b7b2018-05-18 00:41:55 -070054 u64 bytes_sent;
55 u64 bytes_to_send;
56 volatile u64 bytes_received;
57 volatile u64 bytes_to_receive;
Florin Corasf03a59a2017-06-09 21:07:32 -070058 f64 start;
Dave Barach68b0fb02017-02-28 15:15:56 -050059} session_t;
60
61typedef enum
62{
63 STATE_START,
Florin Corasa5464812017-04-19 13:00:05 -070064 STATE_ATTACHED,
Florin Coras52207f12018-07-12 14:48:06 -070065 STATE_LISTEN,
Dave Barach68b0fb02017-02-28 15:15:56 -050066 STATE_READY,
67 STATE_DISCONNECTING,
Florin Corasa849b7b2018-05-18 00:41:55 -070068 STATE_FAILED,
69 STATE_DETACHED
Dave Barach68b0fb02017-02-28 15:15:56 -050070} connection_state_t;
71
72typedef struct
73{
74 /* vpe input queue */
Florin Corase86a8ed2018-01-05 03:20:25 -080075 svm_queue_t *vl_input_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050076
77 /* API client handle */
78 u32 my_client_index;
79
80 /* The URI we're playing with */
Florin Corase04c2992017-03-01 08:17:34 -080081 u8 *uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050082
83 /* Session pool */
Florin Corase04c2992017-03-01 08:17:34 -080084 session_t *sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -050085
86 /* Hash table for disconnect processing */
Florin Corase04c2992017-03-01 08:17:34 -080087 uword *session_index_by_vpp_handles;
Dave Barach68b0fb02017-02-28 15:15:56 -050088
89 /* intermediate rx buffer */
Florin Corase04c2992017-03-01 08:17:34 -080090 u8 *rx_buf;
Dave Barach68b0fb02017-02-28 15:15:56 -050091
92 /* URI for slave's connect */
Florin Corase04c2992017-03-01 08:17:34 -080093 u8 *connect_uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050094
95 u32 connected_session_index;
96
97 int i_am_master;
98
99 /* drop all packets */
Florin Coras8e43d042018-05-04 15:46:57 -0700100 int no_return;
Dave Barach68b0fb02017-02-28 15:15:56 -0500101
102 /* Our event queue */
Florin Coras3c2fed52018-07-04 04:15:05 -0700103 svm_msg_q_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500104
Florin Coras90a63982017-12-19 04:50:01 -0800105 u8 *socket_name;
106
Dave Barach68b0fb02017-02-28 15:15:56 -0500107 pid_t my_pid;
108
109 /* For deadman timers */
110 clib_time_t clib_time;
111
112 /* State of the connection, shared between msg RX thread and main thread */
113 volatile connection_state_t state;
114
115 /* Signal variables */
116 volatile int time_to_stop;
117 volatile int time_to_print_stats;
118
119 u32 configured_segment_size;
120
121 /* VNET_API_ERROR_FOO -> "Foo" hash table */
Florin Corase04c2992017-03-01 08:17:34 -0800122 uword *error_string_by_error_number;
Dave Barach68b0fb02017-02-28 15:15:56 -0500123
124 u8 *connect_test_data;
Florin Corasa849b7b2018-05-18 00:41:55 -0700125 pthread_t *client_thread_handles;
126 u32 *thread_args;
Florin Corase04c2992017-03-01 08:17:34 -0800127 u32 client_bytes_received;
128 u8 test_return_packets;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700129 u64 bytes_to_send;
Florin Coras8e43d042018-05-04 15:46:57 -0700130 u32 fifo_size;
Florin Corase04c2992017-03-01 08:17:34 -0800131
Florin Corasa849b7b2018-05-18 00:41:55 -0700132 u32 n_clients;
133 u64 tx_total;
134 u64 rx_total;
135
136 volatile u32 n_clients_connected;
137 volatile u32 n_active_clients;
138
139
Florin Coras90a63982017-12-19 04:50:01 -0800140 /** Flag that decides if socket, instead of svm, api is used to connect to
141 * vpp. If sock api is used, shm binary api is subsequently bootstrapped
142 * and all other messages are exchanged using shm IPC. */
143 u8 use_sock_api;
144
Florin Corase04c2992017-03-01 08:17:34 -0800145 /* convenience */
146 svm_fifo_segment_main_t *segment_main;
Florin Corasb384b542018-01-15 01:08:33 -0800147} echo_main_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500148
Florin Corasb384b542018-01-15 01:08:33 -0800149echo_main_t echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500150
151#if CLIB_DEBUG > 0
152#define NITER 10000
153#else
154#define NITER 4000000
155#endif
156
Florin Corase3e2f072018-03-04 07:24:30 -0800157const char test_srv_crt_rsa[] =
158 "-----BEGIN CERTIFICATE-----\r\n"
Florin Coras8f89dd02018-03-05 16:53:07 -0800159 "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
160 "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
161 "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n"
162 "IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n"
163 "NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n"
164 "MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n"
165 "LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n"
166 "ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n"
167 "4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n"
168 "cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n"
169 "Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n"
170 "ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n"
171 "+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n"
172 "Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n"
173 "aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n"
174 "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n"
175 "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
176 "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
177 "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
178 "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
179 "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
180 "-----END CERTIFICATE-----\r\n";
Florin Corase3e2f072018-03-04 07:24:30 -0800181const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
182
183const char test_srv_key_rsa[] =
Florin Coras8f89dd02018-03-05 16:53:07 -0800184 "-----BEGIN PRIVATE KEY-----\r\n"
185 "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
186 "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
187 "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
188 "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
189 "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
190 "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
191 "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
192 "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
193 "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
194 "LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n"
195 "BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n"
196 "CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n"
197 "Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n"
198 "nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n"
199 "5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n"
200 "ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n"
201 "sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n"
202 "gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n"
203 "Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n"
204 "CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n"
205 "K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n"
206 "VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n"
207 "EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n"
208 "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
209 "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
210 "oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n";
Florin Corase3e2f072018-03-04 07:24:30 -0800211const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
212
Florin Corasa5464812017-04-19 13:00:05 -0700213static u8 *
214format_api_error (u8 * s, va_list * args)
215{
Florin Corasb384b542018-01-15 01:08:33 -0800216 echo_main_t *em = &echo_main;
Florin Corasa5464812017-04-19 13:00:05 -0700217 i32 error = va_arg (*args, u32);
218 uword *p;
219
Florin Corasb384b542018-01-15 01:08:33 -0800220 p = hash_get (em->error_string_by_error_number, -error);
Florin Corasa5464812017-04-19 13:00:05 -0700221
222 if (p)
223 s = format (s, "%s", p[0]);
224 else
225 s = format (s, "%d", error);
226 return s;
227}
228
229static void
Florin Corasb384b542018-01-15 01:08:33 -0800230init_error_string_table (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700231{
Florin Corasb384b542018-01-15 01:08:33 -0800232 em->error_string_by_error_number = hash_create (0, sizeof (uword));
Florin Corasa5464812017-04-19 13:00:05 -0700233
Florin Corasb384b542018-01-15 01:08:33 -0800234#define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
Florin Corasa5464812017-04-19 13:00:05 -0700235 foreach_vnet_api_error;
236#undef _
237
Florin Corasb384b542018-01-15 01:08:33 -0800238 hash_set (em->error_string_by_error_number, 99, "Misc");
Florin Corasa5464812017-04-19 13:00:05 -0700239}
240
Florin Coras52207f12018-07-12 14:48:06 -0700241static void handle_mq_event (session_event_t * e);
242
243static int
Florin Corasb384b542018-01-15 01:08:33 -0800244wait_for_state_change (echo_main_t * em, connection_state_t state)
Dave Barach68b0fb02017-02-28 15:15:56 -0500245{
Florin Coras52207f12018-07-12 14:48:06 -0700246 svm_msg_q_msg_t msg;
247 session_event_t *e;
248 f64 timeout;
249
Dave Barach68b0fb02017-02-28 15:15:56 -0500250#if CLIB_DEBUG > 0
251#define TIMEOUT 600.0
252#else
253#define TIMEOUT 600.0
254#endif
255
Florin Coras52207f12018-07-12 14:48:06 -0700256 timeout = clib_time_now (&em->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500257
Florin Corasb384b542018-01-15 01:08:33 -0800258 while (clib_time_now (&em->clib_time) < timeout)
Dave Barach68b0fb02017-02-28 15:15:56 -0500259 {
Florin Corasb384b542018-01-15 01:08:33 -0800260 if (em->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800261 return 0;
Florin Corasb384b542018-01-15 01:08:33 -0800262 if (em->state == STATE_FAILED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500263 return -1;
Florin Corasb384b542018-01-15 01:08:33 -0800264 if (em->time_to_stop == 1)
Florin Corasf03a59a2017-06-09 21:07:32 -0700265 return 0;
Florin Coras99368312018-08-02 10:45:44 -0700266 if (!em->our_event_queue || em->state < STATE_ATTACHED)
Florin Coras52207f12018-07-12 14:48:06 -0700267 continue;
268
269 if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_NOWAIT, 0))
270 continue;
271 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
272 handle_mq_event (e);
273 svm_msg_q_free_msg (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500274 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700275 clib_warning ("timeout waiting for state %d", state);
Dave Barach68b0fb02017-02-28 15:15:56 -0500276 return -1;
277}
278
Florin Coras6cf30ad2017-04-04 23:08:23 -0700279void
Florin Corasb384b542018-01-15 01:08:33 -0800280application_send_attach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700281{
282 vl_api_application_attach_t *bmp;
Florin Corase3e2f072018-03-04 07:24:30 -0800283 vl_api_application_tls_cert_add_t *cert_mp;
284 vl_api_application_tls_key_add_t *key_mp;
285
Florin Coras6cf30ad2017-04-04 23:08:23 -0700286 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400287 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700288
289 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Corasb384b542018-01-15 01:08:33 -0800290 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700291 bmp->context = ntohl (0xfeedface);
Florin Coras52207f12018-07-12 14:48:06 -0700292 bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
293 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT;
294 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
Dave Barach10d8cc62017-05-30 09:30:07 -0400295 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Coras8e43d042018-05-04 15:46:57 -0700296 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = em->fifo_size;
297 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = em->fifo_size;
Florin Corasff6e7692017-12-11 04:59:01 -0800298 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
299 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
Florin Corasa849b7b2018-05-18 00:41:55 -0700300 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 256;
Florin Corasb384b542018-01-15 01:08:33 -0800301 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase3e2f072018-03-04 07:24:30 -0800302
303 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
Dave Barachb7b92992018-10-17 10:38:51 -0400304 clib_memset (cert_mp, 0, sizeof (*cert_mp));
Florin Corase3e2f072018-03-04 07:24:30 -0800305 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
306 cert_mp->client_index = em->my_client_index;
307 cert_mp->context = ntohl (0xfeedface);
308 cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500309 clib_memcpy_fast (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800310 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
311
312 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
Dave Barachb7b92992018-10-17 10:38:51 -0400313 clib_memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800314 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
315 key_mp->client_index = em->my_client_index;
316 key_mp->context = ntohl (0xfeedface);
317 key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500318 clib_memcpy_fast (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800319 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700320}
321
Florin Coras52207f12018-07-12 14:48:06 -0700322static int
Florin Corasb384b542018-01-15 01:08:33 -0800323application_attach (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700324{
Florin Corasb384b542018-01-15 01:08:33 -0800325 application_send_attach (em);
326 if (wait_for_state_change (em, STATE_ATTACHED))
Florin Corasa5464812017-04-19 13:00:05 -0700327 {
328 clib_warning ("timeout waiting for STATE_ATTACHED");
329 return -1;
330 }
331 return 0;
332}
333
Florin Coras6cf30ad2017-04-04 23:08:23 -0700334void
Florin Corasb384b542018-01-15 01:08:33 -0800335application_detach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700336{
337 vl_api_application_detach_t *bmp;
338 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400339 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700340
341 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Corasb384b542018-01-15 01:08:33 -0800342 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700343 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800344 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
345
Florin Corasa849b7b2018-05-18 00:41:55 -0700346 DBG ("%s", "Sent detach");
Florin Corasb384b542018-01-15 01:08:33 -0800347}
348
349static int
Florin Coras99368312018-08-02 10:45:44 -0700350ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
Florin Corasb384b542018-01-15 01:08:33 -0800351{
352 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800353 int rv;
354
Dave Barachb7b92992018-10-17 10:38:51 -0400355 clib_memset (a, 0, sizeof (*a));
Florin Corasb384b542018-01-15 01:08:33 -0800356 a->segment_name = (char *) name;
Florin Corasb384b542018-01-15 01:08:33 -0800357 a->segment_type = type;
358
359 if (type == SSVM_SEGMENT_MEMFD)
Florin Coras99368312018-08-02 10:45:44 -0700360 a->memfd_fd = fd;
Florin Corasb384b542018-01-15 01:08:33 -0800361
362 if ((rv = svm_fifo_segment_attach (a)))
363 {
364 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
365 return rv;
366 }
367
Florin Coras99368312018-08-02 10:45:44 -0700368 vec_reset_length (a->new_segment_indices);
Florin Corasb384b542018-01-15 01:08:33 -0800369 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700370}
371
372static void
373vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
374 mp)
375{
Florin Corasb384b542018-01-15 01:08:33 -0800376 echo_main_t *em = &echo_main;
Florin Coras99368312018-08-02 10:45:44 -0700377 int *fds = 0;
378 u32 n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700379
380 if (mp->retval)
381 {
Florin Corasa5464812017-04-19 13:00:05 -0700382 clib_warning ("attach failed: %U", format_api_error,
383 clib_net_to_host_u32 (mp->retval));
Florin Coras99368312018-08-02 10:45:44 -0700384 goto failed;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700385 }
386
387 if (mp->segment_name_length == 0)
388 {
389 clib_warning ("segment_name_length zero");
Florin Coras99368312018-08-02 10:45:44 -0700390 goto failed;
Florin Corasb384b542018-01-15 01:08:33 -0800391 }
392
393 ASSERT (mp->app_event_queue_address);
394 em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
Florin Coras3c2fed52018-07-04 04:15:05 -0700395 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700396
397 if (mp->n_fds)
398 {
399 vec_validate (fds, mp->n_fds);
400 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
401
402 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
403 if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
404 goto failed;
405
406 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
407 if (ssvm_segment_attach ((char *) mp->segment_name,
408 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
409 goto failed;
410
411 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
412 svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
413
414 vec_free (fds);
415 }
416 else
417 {
418 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
419 -1))
420 goto failed;
421 }
422
Florin Corasb384b542018-01-15 01:08:33 -0800423 em->state = STATE_ATTACHED;
Florin Coras99368312018-08-02 10:45:44 -0700424 return;
425failed:
426 em->state = STATE_FAILED;
427 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700428}
429
430static void
431vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
432 mp)
433{
434 if (mp->retval)
435 clib_warning ("detach returned with err: %d", mp->retval);
Florin Corasa849b7b2018-05-18 00:41:55 -0700436 echo_main.state = STATE_DETACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700437}
438
Dave Barach68b0fb02017-02-28 15:15:56 -0500439static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500440stop_signal (int signum)
441{
Florin Corasb384b542018-01-15 01:08:33 -0800442 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500443
444 um->time_to_stop = 1;
445}
446
447static void
448stats_signal (int signum)
449{
Florin Corasb384b542018-01-15 01:08:33 -0800450 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500451
452 um->time_to_print_stats = 1;
453}
454
455static clib_error_t *
456setup_signal_handlers (void)
457{
458 signal (SIGINT, stats_signal);
459 signal (SIGQUIT, stop_signal);
460 signal (SIGTERM, stop_signal);
461
462 return 0;
463}
464
465void
466vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
467{
468 clib_warning ("BUG");
469}
470
471int
472connect_to_vpp (char *name)
473{
Florin Corasb384b542018-01-15 01:08:33 -0800474 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500475 api_main_t *am = &api_main;
476
Florin Corasb384b542018-01-15 01:08:33 -0800477 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800478 {
Florin Corasb384b542018-01-15 01:08:33 -0800479 if (vl_socket_client_connect ((char *) em->socket_name, name,
Florin Coras90a63982017-12-19 04:50:01 -0800480 0 /* default rx, tx buffer */ ))
Florin Corasb384b542018-01-15 01:08:33 -0800481 {
482 clib_warning ("socket connect failed");
483 return -1;
484 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500485
Florin Corasb384b542018-01-15 01:08:33 -0800486 if (vl_socket_client_init_shm (0))
487 {
488 clib_warning ("init shm api failed");
489 return -1;
490 }
Florin Coras90a63982017-12-19 04:50:01 -0800491 }
492 else
493 {
494 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
Florin Corasb384b542018-01-15 01:08:33 -0800495 {
496 clib_warning ("shmem connect failed");
497 return -1;
498 }
Florin Coras90a63982017-12-19 04:50:01 -0800499 }
Florin Corasb384b542018-01-15 01:08:33 -0800500 em->vl_input_queue = am->shmem_hdr->vl_input_queue;
501 em->my_client_index = am->my_client_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500502 return 0;
503}
504
Florin Coras90a63982017-12-19 04:50:01 -0800505void
Florin Corasb384b542018-01-15 01:08:33 -0800506disconnect_from_vpp (echo_main_t * em)
Florin Coras90a63982017-12-19 04:50:01 -0800507{
Florin Corasb384b542018-01-15 01:08:33 -0800508 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800509 vl_socket_client_disconnect ();
510 else
511 vl_client_disconnect_from_vlib ();
512}
513
Dave Barach68b0fb02017-02-28 15:15:56 -0500514static void
Florin Corase04c2992017-03-01 08:17:34 -0800515vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500516{
517 svm_fifo_segment_create_args_t _a, *a = &_a;
518 int rv;
519
Dave Barachb7b92992018-10-17 10:38:51 -0400520 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500521 a->segment_name = (char *) mp->segment_name;
522 a->segment_size = mp->segment_size;
523 /* Attach to the segment vpp created */
524 rv = svm_fifo_segment_attach (a);
525 if (rv)
526 {
527 clib_warning ("svm_fifo_segment_attach ('%s') failed",
Florin Corase04c2992017-03-01 08:17:34 -0800528 mp->segment_name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500529 return;
530 }
531 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
Florin Corase04c2992017-03-01 08:17:34 -0800532 mp->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500533}
534
535static void
Florin Corasb384b542018-01-15 01:08:33 -0800536session_print_stats (echo_main_t * em, session_t * session)
Florin Corasf03a59a2017-06-09 21:07:32 -0700537{
538 f64 deltat;
539 u64 bytes;
540
Florin Corasb384b542018-01-15 01:08:33 -0800541 deltat = clib_time_now (&em->clib_time) - session->start;
542 bytes = em->i_am_master ? session->bytes_received : em->bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -0700543 fformat (stdout, "Finished in %.6f\n", deltat);
544 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
545}
546
547static void
Florin Corasa849b7b2018-05-18 00:41:55 -0700548test_recv_bytes (session_t * s, u8 * rx_buf, u32 n_read)
Dave Barach68b0fb02017-02-28 15:15:56 -0500549{
Florin Corasa849b7b2018-05-18 00:41:55 -0700550 int i;
551 for (i = 0; i < n_read; i++)
552 {
553 if (rx_buf[i] != ((s->bytes_received + i) & 0xff))
554 {
555 clib_warning ("error at byte %lld, 0x%x not 0x%x",
556 s->bytes_received + i, rx_buf[i],
557 ((s->bytes_received + i) & 0xff));
558 }
559 }
560}
Dave Barach68b0fb02017-02-28 15:15:56 -0500561
Florin Corasa849b7b2018-05-18 00:41:55 -0700562static void
563recv_test_chunk (echo_main_t * em, session_t * s, u8 * rx_buf)
564{
565 svm_fifo_t *rx_fifo = s->server_rx_fifo;
566 u32 n_read_now, n_to_read;
567 int n_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500568
Florin Corasa849b7b2018-05-18 00:41:55 -0700569 n_to_read = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700570 svm_fifo_unset_event (rx_fifo);
571
Dave Barach68b0fb02017-02-28 15:15:56 -0500572 do
573 {
Florin Corasa849b7b2018-05-18 00:41:55 -0700574 n_read_now = clib_min (vec_len (rx_buf), n_to_read);
575 n_read = svm_fifo_dequeue_nowait (rx_fifo, n_read_now, rx_buf);
576 if (n_read <= 0)
577 break;
Florin Corase04c2992017-03-01 08:17:34 -0800578
Florin Corasa849b7b2018-05-18 00:41:55 -0700579 if (n_read_now != n_read)
580 clib_warning ("huh?");
581
582 if (em->test_return_packets)
583 test_recv_bytes (s, rx_buf, n_read);
584
585 n_to_read -= n_read;
586 s->bytes_received += n_read;
587 s->bytes_to_receive -= n_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500588 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700589 while (n_to_read > 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500590}
591
592void
Florin Coras52207f12018-07-12 14:48:06 -0700593client_handle_fifo_event_rx (echo_main_t * em, session_event_t * e,
Florin Corasa849b7b2018-05-18 00:41:55 -0700594 u8 * rx_buf)
Dave Barach68b0fb02017-02-28 15:15:56 -0500595{
Florin Corasa849b7b2018-05-18 00:41:55 -0700596 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500597
Florin Corasa849b7b2018-05-18 00:41:55 -0700598 s = pool_elt_at_index (em->sessions, e->fifo->client_session_index);
599 recv_test_chunk (em, s, rx_buf);
600}
601
602static void
603send_test_chunk (echo_main_t * em, session_t * s)
604{
605 u64 test_buf_len, bytes_this_chunk, test_buf_offset;
606 svm_fifo_t *tx_fifo = s->server_tx_fifo;
607 u8 *test_data = em->connect_test_data;
Yalei Wangf7f4e392018-08-09 10:09:38 +0800608 u32 enq_space = 16 << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -0700609 int written;
610
611 test_buf_len = vec_len (test_data);
612 test_buf_offset = s->bytes_sent % test_buf_len;
613 bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
614 s->bytes_to_send);
615 enq_space = svm_fifo_max_enqueue (tx_fifo);
Florin Corasa849b7b2018-05-18 00:41:55 -0700616
617 bytes_this_chunk = clib_min (bytes_this_chunk, enq_space);
618 written = svm_fifo_enqueue_nowait (tx_fifo, bytes_this_chunk,
619 test_data + test_buf_offset);
620
621 if (written > 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500622 {
Florin Corasa849b7b2018-05-18 00:41:55 -0700623 s->bytes_to_send -= written;
624 s->bytes_sent += written;
Dave Barach68b0fb02017-02-28 15:15:56 -0500625
Florin Corasa849b7b2018-05-18 00:41:55 -0700626 if (svm_fifo_set_event (tx_fifo))
Florin Coras3c2fed52018-07-04 04:15:05 -0700627 app_send_io_evt_to_vpp (s->vpp_evt_q, tx_fifo, FIFO_EVENT_APP_TX,
628 0 /* do wait for mutex */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500629 }
630}
631
Florin Corasa849b7b2018-05-18 00:41:55 -0700632/*
633 * Rx/Tx polling thread per connection
634 */
Florin Corase04c2992017-03-01 08:17:34 -0800635static void *
Florin Corasa849b7b2018-05-18 00:41:55 -0700636client_thread_fn (void *arg)
637{
638 echo_main_t *em = &echo_main;
639 static u8 *rx_buf = 0;
640 u32 session_index = *(u32 *) arg;
641 session_t *s;
642
643 vec_validate (rx_buf, 1 << 20);
644
645 while (!em->time_to_stop && em->state != STATE_READY)
646 ;
647
648 s = pool_elt_at_index (em->sessions, session_index);
649 while (!em->time_to_stop)
650 {
651 send_test_chunk (em, s);
652 recv_test_chunk (em, s, rx_buf);
653 if (!s->bytes_to_send && !s->bytes_to_receive)
654 break;
655 }
656
657 DBG ("session %d done", session_index);
658 em->tx_total += s->bytes_sent;
659 em->rx_total += s->bytes_received;
660 em->n_active_clients--;
661
662 pthread_exit (0);
663}
664
665/*
666 * Rx thread that handles all connections.
667 *
668 * Not used.
669 */
670void *
Florin Corase04c2992017-03-01 08:17:34 -0800671client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500672{
Florin Coras52207f12018-07-12 14:48:06 -0700673 session_event_t _e, *e = &_e;
Florin Corasb384b542018-01-15 01:08:33 -0800674 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -0700675 static u8 *rx_buf = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700676 svm_msg_q_msg_t msg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500677
Florin Corasa849b7b2018-05-18 00:41:55 -0700678 vec_validate (rx_buf, 1 << 20);
679
680 while (!em->time_to_stop && em->state != STATE_READY)
681 ;
682
683 while (!em->time_to_stop)
Dave Barach68b0fb02017-02-28 15:15:56 -0500684 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700685 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
686 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500687 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800688 {
Florin Corasa5464812017-04-19 13:00:05 -0700689 case FIFO_EVENT_APP_RX:
Florin Corasa849b7b2018-05-18 00:41:55 -0700690 client_handle_fifo_event_rx (em, e, rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800691 break;
Florin Corase04c2992017-03-01 08:17:34 -0800692 default:
693 clib_warning ("unknown event type %d", e->event_type);
694 break;
695 }
Florin Coras3c2fed52018-07-04 04:15:05 -0700696 svm_msg_q_free_msg (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500697 }
Florin Corase04c2992017-03-01 08:17:34 -0800698 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500699}
700
Florin Coras52207f12018-07-12 14:48:06 -0700701void
702client_send_connect (echo_main_t * em)
703{
704 vl_api_connect_uri_t *cmp;
705 cmp = vl_msg_api_alloc (sizeof (*cmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400706 clib_memset (cmp, 0, sizeof (*cmp));
Florin Coras52207f12018-07-12 14:48:06 -0700707
708 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
709 cmp->client_index = em->my_client_index;
710 cmp->context = ntohl (0xfeedface);
711 memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri));
712 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
713}
714
715void
716client_send_disconnect (echo_main_t * em, session_t * s)
717{
718 vl_api_disconnect_session_t *dmp;
719 dmp = vl_msg_api_alloc (sizeof (*dmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400720 clib_memset (dmp, 0, sizeof (*dmp));
Florin Coras52207f12018-07-12 14:48:06 -0700721 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
722 dmp->client_index = em->my_client_index;
723 dmp->handle = s->vpp_session_handle;
724 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
725}
726
727int
728client_disconnect (echo_main_t * em, session_t * s)
729{
730 client_send_disconnect (em, s);
731 pool_put (em->sessions, s);
Dave Barachb7b92992018-10-17 10:38:51 -0400732 clib_memset (s, 0xfe, sizeof (*s));
Florin Coras52207f12018-07-12 14:48:06 -0700733 return 0;
734}
735
Dave Barach68b0fb02017-02-28 15:15:56 -0500736static void
Florin Coras52207f12018-07-12 14:48:06 -0700737session_accepted_handler (session_accepted_msg_t * mp)
738{
739 app_session_evt_t _app_evt, *app_evt = &_app_evt;
740 session_accepted_reply_msg_t *rmp;
741 svm_fifo_t *rx_fifo, *tx_fifo;
742 echo_main_t *em = &echo_main;
743 session_t *session;
744 static f64 start_time;
745 u32 session_index;
746 u8 *ip_str;
747
748 if (start_time == 0.0)
749 start_time = clib_time_now (&em->clib_time);
750
751 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
752 clib_warning ("Accepted session from: %s:%d", ip_str,
753 clib_net_to_host_u16 (mp->port));
754
755 /* Allocate local session and set it up */
756 pool_get (em->sessions, session);
757 session_index = session - em->sessions;
758
759 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
760 rx_fifo->client_session_index = session_index;
761 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
762 tx_fifo->client_session_index = session_index;
763
764 session->server_rx_fifo = rx_fifo;
765 session->server_tx_fifo = tx_fifo;
766 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
767 svm_msg_q_t *);
768
769 /* Add it to lookup table */
770 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
771
772 em->state = STATE_READY;
773
774 /* Stats printing */
775 if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
776 {
777 f64 now = clib_time_now (&em->clib_time);
778 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
779 pool_elts (em->sessions), now - start_time,
780 (f64) pool_elts (em->sessions) / (now - start_time));
781 }
782
783 /*
784 * Send accept reply to vpp
785 */
786 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
787 SESSION_CTRL_EVT_ACCEPTED_REPLY);
788 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
789 rmp->handle = mp->handle;
790 rmp->context = mp->context;
791 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
792
793 session->bytes_received = 0;
794 session->start = clib_time_now (&em->clib_time);
795}
796
797static void
798session_connected_handler (session_connected_msg_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500799{
Florin Corasb384b542018-01-15 01:08:33 -0800800 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500801 session_t *session;
802 u32 session_index;
803 svm_fifo_t *rx_fifo, *tx_fifo;
804 int rv;
805
806 if (mp->retval)
807 {
Florin Corasa5464812017-04-19 13:00:05 -0700808 clib_warning ("connection failed with code: %U", format_api_error,
809 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800810 em->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500811 return;
812 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500813
814 /*
815 * Setup session
816 */
817
Florin Corasb384b542018-01-15 01:08:33 -0800818 pool_get (em->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -0400819 clib_memset (session, 0, sizeof (*session));
Florin Corasb384b542018-01-15 01:08:33 -0800820 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -0500821
Damjan Marion7bee80c2017-04-26 15:32:12 +0200822 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500823 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200824 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500825 tx_fifo->client_session_index = session_index;
826
827 session->server_rx_fifo = rx_fifo;
828 session->server_tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700829 session->vpp_session_handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800830 session->start = clib_time_now (&em->clib_time);
Florin Corasa849b7b2018-05-18 00:41:55 -0700831 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
Florin Coras3c2fed52018-07-04 04:15:05 -0700832 svm_msg_q_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500833
Florin Corasb384b542018-01-15 01:08:33 -0800834 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800835
Florin Corasa849b7b2018-05-18 00:41:55 -0700836 /*
837 * Start RX thread
838 */
839 em->thread_args[em->n_clients_connected] = session_index;
840 rv = pthread_create (&em->client_thread_handles[em->n_clients_connected],
841 NULL /*attr */ , client_thread_fn,
842 (void *) &em->thread_args[em->n_clients_connected]);
Florin Corase04c2992017-03-01 08:17:34 -0800843 if (rv)
844 {
845 clib_warning ("pthread_create returned %d", rv);
Florin Corasa849b7b2018-05-18 00:41:55 -0700846 return;
Florin Coras6792ec02017-03-13 03:49:51 -0700847 }
848
Florin Corasa849b7b2018-05-18 00:41:55 -0700849 em->n_clients_connected += 1;
850 clib_warning ("session %u (0x%llx) connected with local ip %U port %d",
851 session_index, mp->handle, format_ip46_address, mp->lcl_ip,
852 mp->is_ip4, clib_net_to_host_u16 (mp->lcl_port));
Florin Corase04c2992017-03-01 08:17:34 -0800853}
854
Florin Coras52207f12018-07-12 14:48:06 -0700855static void
856session_disconnected_handler (session_disconnected_msg_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800857{
Florin Coras52207f12018-07-12 14:48:06 -0700858 app_session_evt_t _app_evt, *app_evt = &_app_evt;
859 session_disconnected_reply_msg_t *rmp;
860 echo_main_t *em = &echo_main;
861 session_t *session = 0;
862 uword *p;
863 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800864
Florin Coras52207f12018-07-12 14:48:06 -0700865 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700866 if (!p)
Florin Coras52207f12018-07-12 14:48:06 -0700867 {
868 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700869 return;
Florin Coras52207f12018-07-12 14:48:06 -0700870 }
871
Florin Coras3d0fadc2018-07-17 05:35:47 -0700872 session = pool_elt_at_index (em->sessions, p[0]);
873 hash_unset (em->session_index_by_vpp_handles, mp->handle);
874 pool_put (em->sessions, session);
875
Florin Coras52207f12018-07-12 14:48:06 -0700876 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
877 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
878 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
879 rmp->retval = rv;
880 rmp->handle = mp->handle;
881 rmp->context = mp->context;
882 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
883
Florin Coras3d0fadc2018-07-17 05:35:47 -0700884 session_print_stats (em, session);
Florin Corase04c2992017-03-01 08:17:34 -0800885}
886
Florin Coras52207f12018-07-12 14:48:06 -0700887static void
888session_reset_handler (session_reset_msg_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800889{
Florin Coras52207f12018-07-12 14:48:06 -0700890 app_session_evt_t _app_evt, *app_evt = &_app_evt;
891 echo_main_t *em = &echo_main;
892 session_reset_reply_msg_t *rmp;
893 session_t *session = 0;
894 uword *p;
895 int rv = 0;
896
897 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
898
899 if (p)
900 {
901 session = pool_elt_at_index (em->sessions, p[0]);
902 clib_warning ("got reset");
903 /* Cleanup later */
904 em->time_to_stop = 1;
905 }
906 else
907 {
908 clib_warning ("couldn't find session key %llx", mp->handle);
909 return;
910 }
911
912 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
913 SESSION_CTRL_EVT_RESET_REPLY);
914 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
915 rmp->retval = rv;
916 rmp->handle = mp->handle;
917 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
Florin Corase04c2992017-03-01 08:17:34 -0800918}
919
Florin Coras52207f12018-07-12 14:48:06 -0700920static void
921handle_mq_event (session_event_t * e)
Florin Corasa5464812017-04-19 13:00:05 -0700922{
Florin Coras52207f12018-07-12 14:48:06 -0700923 switch (e->event_type)
924 {
925 case SESSION_CTRL_EVT_ACCEPTED:
926 session_accepted_handler ((session_accepted_msg_t *) e->data);
927 break;
928 case SESSION_CTRL_EVT_CONNECTED:
929 session_connected_handler ((session_connected_msg_t *) e->data);
930 break;
931 case SESSION_CTRL_EVT_DISCONNECTED:
932 session_disconnected_handler ((session_disconnected_msg_t *) e->data);
933 break;
934 case SESSION_CTRL_EVT_RESET:
935 session_reset_handler ((session_reset_msg_t *) e->data);
936 break;
937 default:
938 clib_warning ("unhandled %u", e->event_type);
939 }
Florin Corasa5464812017-04-19 13:00:05 -0700940}
941
Florin Corase04c2992017-03-01 08:17:34 -0800942static void
Florin Corasa849b7b2018-05-18 00:41:55 -0700943clients_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800944{
Florin Corasa849b7b2018-05-18 00:41:55 -0700945 f64 start_time, deltat, timeout = 100.0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700946 svm_msg_q_msg_t msg;
Florin Coras52207f12018-07-12 14:48:06 -0700947 session_event_t *e;
Florin Corasa849b7b2018-05-18 00:41:55 -0700948 session_t *s;
Florin Corase04c2992017-03-01 08:17:34 -0800949 int i;
950
Florin Corase04c2992017-03-01 08:17:34 -0800951 /* Init test data */
Florin Coras8e43d042018-05-04 15:46:57 -0700952 vec_validate (em->connect_test_data, 1024 * 1024 - 1);
Florin Corasb384b542018-01-15 01:08:33 -0800953 for (i = 0; i < vec_len (em->connect_test_data); i++)
954 em->connect_test_data[i] = i & 0xff;
Florin Corase04c2992017-03-01 08:17:34 -0800955
Florin Corasa849b7b2018-05-18 00:41:55 -0700956 /*
957 * Attach and connect the clients
958 */
959 if (application_attach (em))
960 return;
Florin Corase04c2992017-03-01 08:17:34 -0800961
Florin Corasa849b7b2018-05-18 00:41:55 -0700962 for (i = 0; i < em->n_clients; i++)
963 client_send_connect (em);
964
965 start_time = clib_time_now (&em->clib_time);
966 while (em->n_clients_connected < em->n_clients
967 && (clib_time_now (&em->clib_time) - start_time < timeout)
968 && em->state != STATE_FAILED)
Florin Coras52207f12018-07-12 14:48:06 -0700969
970 {
971 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
972 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
973 handle_mq_event (e);
974 svm_msg_q_free_msg (em->our_event_queue, &msg);
975 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700976
977 if (em->n_clients_connected != em->n_clients)
978 {
979 clib_warning ("failed to initialize all connections");
980 return;
981 }
982
983 /*
984 * Initialize connections
985 */
986 for (i = 0; i < em->n_clients; i++)
987 {
988 s = pool_elt_at_index (em->sessions, i);
989 s->bytes_to_send = em->bytes_to_send;
990 if (!em->no_return)
991 s->bytes_to_receive = em->bytes_to_send;
992 }
993 em->n_active_clients = em->n_clients_connected;
994
995 /*
996 * Wait for client threads to send the data
997 */
998 start_time = clib_time_now (&em->clib_time);
999 em->state = STATE_READY;
1000 while (em->n_active_clients)
Florin Coras3c2fed52018-07-04 04:15:05 -07001001 if (!svm_msg_q_is_empty (em->our_event_queue))
1002 {
1003 if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0))
1004 {
1005 clib_warning ("svm msg q returned");
Florin Coras52207f12018-07-12 14:48:06 -07001006 continue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001007 }
Florin Coras52207f12018-07-12 14:48:06 -07001008 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
1009 if (e->event_type != FIFO_EVENT_APP_RX)
1010 handle_mq_event (e);
1011 svm_msg_q_free_msg (em->our_event_queue, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -07001012 }
Florin Corasa849b7b2018-05-18 00:41:55 -07001013
1014 for (i = 0; i < em->n_clients; i++)
1015 {
1016 s = pool_elt_at_index (em->sessions, i);
1017 client_disconnect (em, s);
1018 }
1019
1020 /*
1021 * Stats and detach
1022 */
1023 deltat = clib_time_now (&em->clib_time) - start_time;
1024 fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds\n",
1025 em->tx_total, em->tx_total / (1ULL << 20),
1026 em->tx_total / (1ULL << 30), deltat);
1027 fformat (stdout, "%.4f Gbit/second\n", (em->tx_total * 8.0) / deltat / 1e9);
1028
Florin Corasb384b542018-01-15 01:08:33 -08001029 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -08001030}
1031
1032static void
1033vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
1034{
Florin Corasb384b542018-01-15 01:08:33 -08001035 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -08001036
1037 if (mp->retval)
1038 {
flyingeagle23a07779f2017-04-26 20:22:04 +08001039 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -07001040 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -08001041 em->state = STATE_FAILED;
Florin Corase04c2992017-03-01 08:17:34 -08001042 return;
1043 }
1044
Florin Corasb384b542018-01-15 01:08:33 -08001045 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001046}
1047
Dave Barach68b0fb02017-02-28 15:15:56 -05001048static void
Florin Corase04c2992017-03-01 08:17:34 -08001049vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -05001050{
Florin Corasb384b542018-01-15 01:08:33 -08001051 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001052
1053 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -08001054 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -05001055
Florin Corasb384b542018-01-15 01:08:33 -08001056 em->state = STATE_START;
Dave Barach68b0fb02017-02-28 15:15:56 -05001057}
1058
Florin Coras6cf30ad2017-04-04 23:08:23 -07001059u8 *
1060format_ip4_address (u8 * s, va_list * args)
1061{
1062 u8 *a = va_arg (*args, u8 *);
1063 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1064}
1065
1066u8 *
1067format_ip6_address (u8 * s, va_list * args)
1068{
1069 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1070 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1071
1072 i_max_n_zero = ARRAY_LEN (a->as_u16);
1073 max_n_zeros = 0;
1074 i_first_zero = i_max_n_zero;
1075 n_zeros = 0;
1076 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1077 {
1078 u32 is_zero = a->as_u16[i] == 0;
1079 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1080 {
1081 i_first_zero = i;
1082 n_zeros = 0;
1083 }
1084 n_zeros += is_zero;
1085 if ((!is_zero && n_zeros > max_n_zeros)
1086 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1087 {
1088 i_max_n_zero = i_first_zero;
1089 max_n_zeros = n_zeros;
1090 i_first_zero = ARRAY_LEN (a->as_u16);
1091 n_zeros = 0;
1092 }
1093 }
1094
1095 last_double_colon = 0;
1096 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1097 {
1098 if (i == i_max_n_zero && max_n_zeros > 1)
1099 {
1100 s = format (s, "::");
1101 i += max_n_zeros - 1;
1102 last_double_colon = 1;
1103 }
1104 else
1105 {
1106 s = format (s, "%s%x",
1107 (last_double_colon || i == 0) ? "" : ":",
1108 clib_net_to_host_u16 (a->as_u16[i]));
1109 last_double_colon = 0;
1110 }
1111 }
1112
1113 return s;
1114}
1115
1116/* Format an IP46 address. */
1117u8 *
1118format_ip46_address (u8 * s, va_list * args)
1119{
1120 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1121 ip46_type_t type = va_arg (*args, ip46_type_t);
1122 int is_ip4 = 1;
1123
1124 switch (type)
1125 {
1126 case IP46_TYPE_ANY:
1127 is_ip4 = ip46_address_is_ip4 (ip46);
1128 break;
1129 case IP46_TYPE_IP4:
1130 is_ip4 = 1;
1131 break;
1132 case IP46_TYPE_IP6:
1133 is_ip4 = 0;
1134 break;
1135 }
1136
1137 return is_ip4 ?
1138 format (s, "%U", format_ip4_address, &ip46->ip4) :
1139 format (s, "%U", format_ip6_address, &ip46->ip6);
1140}
1141
Dave Barach68b0fb02017-02-28 15:15:56 -05001142static void
Florin Coras52207f12018-07-12 14:48:06 -07001143server_handle_fifo_event_rx (echo_main_t * em, session_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -05001144{
Florin Corase04c2992017-03-01 08:17:34 -08001145 svm_fifo_t *rx_fifo, *tx_fifo;
1146 int n_read;
Florin Corasf03a59a2017-06-09 21:07:32 -07001147 session_t *session;
1148 int rv;
1149 u32 max_dequeue, offset, max_transfer, rx_buf_len;
Florin Corase04c2992017-03-01 08:17:34 -08001150
Florin Corasb384b542018-01-15 01:08:33 -08001151 rx_buf_len = vec_len (em->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -08001152 rx_fifo = e->fifo;
Florin Corasa849b7b2018-05-18 00:41:55 -07001153 session = pool_elt_at_index (em->sessions, rx_fifo->client_session_index);
Florin Corasf03a59a2017-06-09 21:07:32 -07001154 tx_fifo = session->server_tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -08001155
Florin Corasf03a59a2017-06-09 21:07:32 -07001156 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -07001157 /* Allow enqueuing of a new event */
1158 svm_fifo_unset_event (rx_fifo);
1159
Florin Corasa849b7b2018-05-18 00:41:55 -07001160 if (PREDICT_FALSE (!max_dequeue))
1161 return;
Florin Coras6792ec02017-03-13 03:49:51 -07001162
Florin Corasf03a59a2017-06-09 21:07:32 -07001163 /* Read the max_dequeue */
Florin Corase04c2992017-03-01 08:17:34 -08001164 do
1165 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001166 max_transfer = clib_min (rx_buf_len, max_dequeue);
Florin Corasb384b542018-01-15 01:08:33 -08001167 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, em->rx_buf);
Florin Coras6792ec02017-03-13 03:49:51 -07001168 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -08001169 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001170 max_dequeue -= n_read;
1171 session->bytes_received += n_read;
Florin Corasa849b7b2018-05-18 00:41:55 -07001172 session->bytes_to_receive -= n_read;
Florin Corasf03a59a2017-06-09 21:07:32 -07001173 }
1174
1175 /* Reflect if a non-drop session */
Florin Coras8e43d042018-05-04 15:46:57 -07001176 if (!em->no_return && n_read > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001177 {
1178 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001179 do
1180 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001181 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
Florin Corasb384b542018-01-15 01:08:33 -08001182 &em->rx_buf[offset]);
Florin Corasf03a59a2017-06-09 21:07:32 -07001183 if (rv > 0)
1184 {
1185 n_read -= rv;
1186 offset += rv;
1187 }
Florin Corase04c2992017-03-01 08:17:34 -08001188 }
Florin Corasb384b542018-01-15 01:08:33 -08001189 while ((rv <= 0 || n_read > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001190
Florin Coras6792ec02017-03-13 03:49:51 -07001191 /* If event wasn't set, add one */
1192 if (svm_fifo_set_event (tx_fifo))
Florin Coras52207f12018-07-12 14:48:06 -07001193 app_send_io_evt_to_vpp (session->vpp_evt_q, tx_fifo,
1194 FIFO_EVENT_APP_TX, SVM_Q_WAIT);
Florin Corase04c2992017-03-01 08:17:34 -08001195 }
Florin Corase04c2992017-03-01 08:17:34 -08001196 }
Florin Corasb384b542018-01-15 01:08:33 -08001197 while ((n_read < 0 || max_dequeue > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001198}
1199
Florin Coras52207f12018-07-12 14:48:06 -07001200static void
Florin Corasb384b542018-01-15 01:08:33 -08001201server_handle_event_queue (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001202{
Florin Coras3c2fed52018-07-04 04:15:05 -07001203 svm_msg_q_msg_t msg;
Florin Coras52207f12018-07-12 14:48:06 -07001204 session_event_t *e;
Florin Corase04c2992017-03-01 08:17:34 -08001205
1206 while (1)
1207 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001208 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
1209 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
Florin Corase04c2992017-03-01 08:17:34 -08001210 switch (e->event_type)
1211 {
Florin Corasa5464812017-04-19 13:00:05 -07001212 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -08001213 server_handle_fifo_event_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -08001214 break;
Florin Corase04c2992017-03-01 08:17:34 -08001215 default:
Florin Coras52207f12018-07-12 14:48:06 -07001216 handle_mq_event (e);
Florin Corase04c2992017-03-01 08:17:34 -08001217 break;
1218 }
Florin Corasb384b542018-01-15 01:08:33 -08001219 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001220 break;
Florin Corasb384b542018-01-15 01:08:33 -08001221 if (PREDICT_FALSE (em->time_to_print_stats == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001222 {
Florin Corasb384b542018-01-15 01:08:33 -08001223 em->time_to_print_stats = 0;
1224 fformat (stdout, "%d connections\n", pool_elts (em->sessions));
Florin Corase04c2992017-03-01 08:17:34 -08001225 }
Florin Coras3c2fed52018-07-04 04:15:05 -07001226 svm_msg_q_free_msg (em->our_event_queue, &msg);
Florin Corase04c2992017-03-01 08:17:34 -08001227 }
1228}
1229
1230void
Florin Corasb384b542018-01-15 01:08:33 -08001231server_send_listen (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001232{
1233 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001234 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001235 clib_memset (bmp, 0, sizeof (*bmp));
Florin Corase04c2992017-03-01 08:17:34 -08001236
1237 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001238 bmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -08001239 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -08001240 memcpy (bmp->uri, em->uri, vec_len (em->uri));
1241 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase04c2992017-03-01 08:17:34 -08001242}
1243
Florin Corasa5464812017-04-19 13:00:05 -07001244int
Florin Corasb384b542018-01-15 01:08:33 -08001245server_listen (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001246{
Florin Corasb384b542018-01-15 01:08:33 -08001247 server_send_listen (em);
1248 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -07001249 {
1250 clib_warning ("timeout waiting for STATE_READY");
1251 return -1;
1252 }
1253 return 0;
1254}
1255
Florin Corase04c2992017-03-01 08:17:34 -08001256void
Florin Corasb384b542018-01-15 01:08:33 -08001257server_send_unbind (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001258{
1259 vl_api_unbind_uri_t *ump;
1260
1261 ump = vl_msg_api_alloc (sizeof (*ump));
Dave Barachb7b92992018-10-17 10:38:51 -04001262 clib_memset (ump, 0, sizeof (*ump));
Florin Corase04c2992017-03-01 08:17:34 -08001263
1264 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001265 ump->client_index = em->my_client_index;
1266 memcpy (ump->uri, em->uri, vec_len (em->uri));
1267 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
Florin Corase04c2992017-03-01 08:17:34 -08001268}
1269
Florin Corasa5464812017-04-19 13:00:05 -07001270int
Florin Corasb384b542018-01-15 01:08:33 -08001271server_unbind (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001272{
Florin Corasb384b542018-01-15 01:08:33 -08001273 server_send_unbind (em);
1274 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -07001275 {
1276 clib_warning ("timeout waiting for STATE_START");
1277 return -1;
1278 }
1279 return 0;
1280}
1281
Florin Corase04c2992017-03-01 08:17:34 -08001282void
Florin Corasb384b542018-01-15 01:08:33 -08001283server_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001284{
Florin Coras90a63982017-12-19 04:50:01 -08001285 session_t *session;
1286 int i;
1287
1288 /* $$$$ hack preallocation */
1289 for (i = 0; i < 200000; i++)
1290 {
Florin Corasb384b542018-01-15 01:08:33 -08001291 pool_get (em->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -04001292 clib_memset (session, 0, sizeof (*session));
Florin Coras90a63982017-12-19 04:50:01 -08001293 }
1294 for (i = 0; i < 200000; i++)
Florin Corasb384b542018-01-15 01:08:33 -08001295 pool_put_index (em->sessions, i);
Florin Coras90a63982017-12-19 04:50:01 -08001296
Florin Corasb384b542018-01-15 01:08:33 -08001297 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -07001298 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001299
Dave Barach68b0fb02017-02-28 15:15:56 -05001300 /* Bind to uri */
Florin Corasb384b542018-01-15 01:08:33 -08001301 if (server_listen (em))
Florin Corasa5464812017-04-19 13:00:05 -07001302 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001303
1304 /* Enter handle event loop */
Florin Corasb384b542018-01-15 01:08:33 -08001305 server_handle_event_queue (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001306
1307 /* Cleanup */
Florin Corasb384b542018-01-15 01:08:33 -08001308 server_send_unbind (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001309
Florin Corasb384b542018-01-15 01:08:33 -08001310 application_detach (em);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001311
Dave Barach68b0fb02017-02-28 15:15:56 -05001312 fformat (stdout, "Test complete...\n");
1313}
1314
Florin Corase69f4952017-03-07 10:06:24 -08001315static void
1316vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1317 mp)
1318{
Florin Corasb384b542018-01-15 01:08:33 -08001319 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -07001320 uword *p;
Florin Coras6792ec02017-03-13 03:49:51 -07001321
Florin Corasf03a59a2017-06-09 21:07:32 -07001322 if (mp->retval)
1323 {
1324 clib_warning ("vpp complained about disconnect: %d",
1325 ntohl (mp->retval));
Florin Corasa849b7b2018-05-18 00:41:55 -07001326 return;
Florin Corasf03a59a2017-06-09 21:07:32 -07001327 }
1328
Florin Corasb384b542018-01-15 01:08:33 -08001329 em->state = STATE_START;
Florin Corasa849b7b2018-05-18 00:41:55 -07001330
1331 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
1332 if (p)
1333 {
1334 hash_unset (em->session_index_by_vpp_handles, mp->handle);
1335 }
1336 else
1337 {
1338 clib_warning ("couldn't find session key %llx", mp->handle);
1339 }
Florin Corase69f4952017-03-07 10:06:24 -08001340}
1341
Florin Corase3e2f072018-03-04 07:24:30 -08001342static void
1343 vl_api_application_tls_cert_add_reply_t_handler
1344 (vl_api_application_tls_cert_add_reply_t * mp)
1345{
1346 if (mp->retval)
1347 clib_warning ("failed to add tls cert");
1348}
1349
1350static void
1351 vl_api_application_tls_key_add_reply_t_handler
1352 (vl_api_application_tls_key_add_reply_t * mp)
1353{
1354 if (mp->retval)
1355 clib_warning ("failed to add tls key");
1356}
1357
1358#define foreach_tcp_echo_msg \
1359_(BIND_URI_REPLY, bind_uri_reply) \
1360_(UNBIND_URI_REPLY, unbind_uri_reply) \
Florin Corase3e2f072018-03-04 07:24:30 -08001361_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Corase3e2f072018-03-04 07:24:30 -08001362_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1363_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1364_(MAP_ANOTHER_SEGMENT, map_another_segment) \
1365_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
1366_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001367
1368void
Florin Corasb384b542018-01-15 01:08:33 -08001369tcp_echo_api_hookup (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -05001370{
1371#define _(N,n) \
1372 vl_msg_api_set_handlers(VL_API_##N, #n, \
1373 vl_api_##n##_t_handler, \
1374 vl_noop_handler, \
1375 vl_api_##n##_t_endian, \
1376 vl_api_##n##_t_print, \
1377 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -08001378 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -05001379#undef _
1380}
1381
1382int
1383main (int argc, char **argv)
1384{
Florin Coras8e43d042018-05-04 15:46:57 -07001385 int i_am_server = 1, test_return_packets = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001386 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001387 unformat_input_t _argv, *a = &_argv;
1388 u8 *chroot_prefix;
Dave Barach6a5adc32018-07-04 10:56:23 -04001389 u8 *uri = 0;
Florin Corase69f4952017-03-07 10:06:24 -08001390 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
Florin Coras8e43d042018-05-04 15:46:57 -07001391 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001392 u64 bytes_to_send = 64 << 10, mbytes;
Florin Corasb384b542018-01-15 01:08:33 -08001393 char *app_name;
Dave Barach68b0fb02017-02-28 15:15:56 -05001394 u32 tmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001395
Dave Barach6a5adc32018-07-04 10:56:23 -04001396 clib_mem_init_thread_safe (0, 256 << 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001397
Dave Barachb7b92992018-10-17 10:38:51 -04001398 clib_memset (em, 0, sizeof (*em));
Florin Corasb384b542018-01-15 01:08:33 -08001399 em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Corasb384b542018-01-15 01:08:33 -08001400 em->my_pid = getpid ();
1401 em->configured_segment_size = 1 << 20;
1402 em->socket_name = 0;
1403 em->use_sock_api = 1;
Florin Coras8e43d042018-05-04 15:46:57 -07001404 em->fifo_size = 64 << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001405 em->n_clients = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001406
Florin Corasb384b542018-01-15 01:08:33 -08001407 clib_time_init (&em->clib_time);
1408 init_error_string_table (em);
Florin Corasa332c462018-01-31 06:52:17 -08001409 svm_fifo_segment_main_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001410 unformat_init_command_line (a, argv);
1411
1412 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1413 {
1414 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001415 {
1416 vl_set_memory_root_path ((char *) chroot_prefix);
1417 }
Florin Corase69f4952017-03-07 10:06:24 -08001418 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001419 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001420 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001421 em->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001422 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001423 em->configured_segment_size = tmp << 30;
Florin Coras8e43d042018-05-04 15:46:57 -07001424 else if (unformat (a, "server"))
1425 i_am_server = 1;
1426 else if (unformat (a, "client"))
1427 i_am_server = 0;
1428 else if (unformat (a, "no-return"))
1429 em->no_return = 1;
Florin Corase04c2992017-03-01 08:17:34 -08001430 else if (unformat (a, "test"))
1431 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001432 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001433 {
1434 bytes_to_send = mbytes << 20;
1435 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001436 else if (unformat (a, "gbytes %lld", &mbytes))
1437 {
1438 bytes_to_send = mbytes << 30;
1439 }
Florin Corasb384b542018-01-15 01:08:33 -08001440 else if (unformat (a, "socket-name %s", &em->socket_name))
Florin Coras90a63982017-12-19 04:50:01 -08001441 ;
1442 else if (unformat (a, "use-svm-api"))
Florin Corasb384b542018-01-15 01:08:33 -08001443 em->use_sock_api = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001444 else if (unformat (a, "fifo-size %d", &tmp))
1445 em->fifo_size = tmp << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001446 else if (unformat (a, "nclients %d", &em->n_clients))
1447 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001448 else
Florin Corase04c2992017-03-01 08:17:34 -08001449 {
Jerome Tollet61f79122018-06-04 08:31:33 +01001450 fformat (stderr, "%s: usage [master|slave]\n", argv[0]);
Florin Corase04c2992017-03-01 08:17:34 -08001451 exit (1);
1452 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001453 }
1454
Florin Corasb384b542018-01-15 01:08:33 -08001455 if (!em->socket_name)
1456 em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
Florin Coras90a63982017-12-19 04:50:01 -08001457
Florin Corase69f4952017-03-07 10:06:24 -08001458 if (uri)
1459 {
Florin Corasb384b542018-01-15 01:08:33 -08001460 em->uri = format (0, "%s%c", uri, 0);
1461 em->connect_uri = format (0, "%s%c", uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001462 }
1463 else
1464 {
Florin Corasb384b542018-01-15 01:08:33 -08001465 em->uri = format (0, "%s%c", bind_uri, 0);
1466 em->connect_uri = format (0, "%s%c", connect_uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001467 }
1468
Florin Coras8e43d042018-05-04 15:46:57 -07001469 em->i_am_master = i_am_server;
Florin Corasb384b542018-01-15 01:08:33 -08001470 em->segment_main = &svm_fifo_segment_main;
Florin Corasb384b542018-01-15 01:08:33 -08001471 em->test_return_packets = test_return_packets;
1472 em->bytes_to_send = bytes_to_send;
1473 em->time_to_stop = 0;
Florin Coras52207f12018-07-12 14:48:06 -07001474 vec_validate (em->rx_buf, 128 << 10);
Florin Corasa849b7b2018-05-18 00:41:55 -07001475 vec_validate (em->client_thread_handles, em->n_clients - 1);
1476 vec_validate (em->thread_args, em->n_clients - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001477
Florin Corase04c2992017-03-01 08:17:34 -08001478 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001479 tcp_echo_api_hookup (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001480
Florin Coras8e43d042018-05-04 15:46:57 -07001481 app_name = i_am_server ? "tcp_echo_server" : "tcp_echo_client";
Florin Corasb384b542018-01-15 01:08:33 -08001482 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001483 {
1484 svm_region_exit ();
1485 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1486 exit (1);
1487 }
1488
Florin Coras8e43d042018-05-04 15:46:57 -07001489 if (i_am_server == 0)
Florin Corasa849b7b2018-05-18 00:41:55 -07001490 clients_run (em);
Florin Coras90a63982017-12-19 04:50:01 -08001491 else
Florin Corasb384b542018-01-15 01:08:33 -08001492 server_run (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001493
Florin Corasa849b7b2018-05-18 00:41:55 -07001494 /* Make sure detach finishes */
1495 wait_for_state_change (em, STATE_DETACHED);
1496
Florin Corasb384b542018-01-15 01:08:33 -08001497 disconnect_from_vpp (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001498 exit (0);
1499}
Florin Corase04c2992017-03-01 08:17:34 -08001500
1501/*
1502 * fd.io coding-style-patch-verification: ON
1503 *
1504 * Local Variables:
1505 * eval: (c-set-style "gnu")
1506 * End:
1507 */