blob: bd05345cc5c7b73543aaefa2ecc8893ef8de931e [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 Corase3e2f072018-03-04 07:24:30 -0800155const char test_srv_crt_rsa[] =
156 "-----BEGIN CERTIFICATE-----\r\n"
Florin Coras8f89dd02018-03-05 16:53:07 -0800157 "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
158 "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
159 "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n"
160 "IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n"
161 "NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n"
162 "MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n"
163 "LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n"
164 "ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n"
165 "4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n"
166 "cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n"
167 "Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n"
168 "ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n"
169 "+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n"
170 "Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n"
171 "aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n"
172 "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n"
173 "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
174 "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
175 "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
176 "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
177 "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
178 "-----END CERTIFICATE-----\r\n";
Florin Corase3e2f072018-03-04 07:24:30 -0800179const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
180
181const char test_srv_key_rsa[] =
Florin Coras8f89dd02018-03-05 16:53:07 -0800182 "-----BEGIN PRIVATE KEY-----\r\n"
183 "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
184 "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
185 "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
186 "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
187 "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
188 "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
189 "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
190 "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
191 "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
192 "LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n"
193 "BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n"
194 "CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n"
195 "Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n"
196 "nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n"
197 "5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n"
198 "ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n"
199 "sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n"
200 "gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n"
201 "Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n"
202 "CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n"
203 "K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n"
204 "VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n"
205 "EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n"
206 "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
207 "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
208 "oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n";
Florin Corase3e2f072018-03-04 07:24:30 -0800209const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
210
Florin Corasa5464812017-04-19 13:00:05 -0700211static u8 *
212format_api_error (u8 * s, va_list * args)
213{
Florin Corasb384b542018-01-15 01:08:33 -0800214 echo_main_t *em = &echo_main;
Florin Corasa5464812017-04-19 13:00:05 -0700215 i32 error = va_arg (*args, u32);
216 uword *p;
217
Florin Corasb384b542018-01-15 01:08:33 -0800218 p = hash_get (em->error_string_by_error_number, -error);
Florin Corasa5464812017-04-19 13:00:05 -0700219
220 if (p)
221 s = format (s, "%s", p[0]);
222 else
223 s = format (s, "%d", error);
224 return s;
225}
226
227static void
Florin Corasb384b542018-01-15 01:08:33 -0800228init_error_string_table (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700229{
Florin Corasb384b542018-01-15 01:08:33 -0800230 em->error_string_by_error_number = hash_create (0, sizeof (uword));
Florin Corasa5464812017-04-19 13:00:05 -0700231
Florin Corasb384b542018-01-15 01:08:33 -0800232#define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
Florin Corasa5464812017-04-19 13:00:05 -0700233 foreach_vnet_api_error;
234#undef _
235
Florin Corasb384b542018-01-15 01:08:33 -0800236 hash_set (em->error_string_by_error_number, 99, "Misc");
Florin Corasa5464812017-04-19 13:00:05 -0700237}
238
Florin Coras52207f12018-07-12 14:48:06 -0700239static void handle_mq_event (session_event_t * e);
240
241static int
Florin Corasb384b542018-01-15 01:08:33 -0800242wait_for_state_change (echo_main_t * em, connection_state_t state)
Dave Barach68b0fb02017-02-28 15:15:56 -0500243{
Florin Coras52207f12018-07-12 14:48:06 -0700244 svm_msg_q_msg_t msg;
245 session_event_t *e;
246 f64 timeout;
247
Dave Barach68b0fb02017-02-28 15:15:56 -0500248#if CLIB_DEBUG > 0
249#define TIMEOUT 600.0
250#else
251#define TIMEOUT 600.0
252#endif
253
Florin Coras52207f12018-07-12 14:48:06 -0700254 timeout = clib_time_now (&em->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500255
Florin Corasb384b542018-01-15 01:08:33 -0800256 while (clib_time_now (&em->clib_time) < timeout)
Dave Barach68b0fb02017-02-28 15:15:56 -0500257 {
Florin Corasb384b542018-01-15 01:08:33 -0800258 if (em->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800259 return 0;
Florin Corasb384b542018-01-15 01:08:33 -0800260 if (em->state == STATE_FAILED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500261 return -1;
Florin Corasb384b542018-01-15 01:08:33 -0800262 if (em->time_to_stop == 1)
Florin Corasf03a59a2017-06-09 21:07:32 -0700263 return 0;
Florin Coras99368312018-08-02 10:45:44 -0700264 if (!em->our_event_queue || em->state < STATE_ATTACHED)
Florin Coras52207f12018-07-12 14:48:06 -0700265 continue;
266
267 if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_NOWAIT, 0))
268 continue;
269 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
270 handle_mq_event (e);
271 svm_msg_q_free_msg (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500272 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700273 clib_warning ("timeout waiting for state %d", state);
Dave Barach68b0fb02017-02-28 15:15:56 -0500274 return -1;
275}
276
Florin Coras6cf30ad2017-04-04 23:08:23 -0700277void
Florin Corasb384b542018-01-15 01:08:33 -0800278application_send_attach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700279{
280 vl_api_application_attach_t *bmp;
Florin Corase3e2f072018-03-04 07:24:30 -0800281 vl_api_application_tls_cert_add_t *cert_mp;
282 vl_api_application_tls_key_add_t *key_mp;
283
Florin Coras6cf30ad2017-04-04 23:08:23 -0700284 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400285 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700286
287 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Corasb384b542018-01-15 01:08:33 -0800288 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700289 bmp->context = ntohl (0xfeedface);
Florin Coras52207f12018-07-12 14:48:06 -0700290 bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
291 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT;
292 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
Dave Barach10d8cc62017-05-30 09:30:07 -0400293 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Coras8e43d042018-05-04 15:46:57 -0700294 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = em->fifo_size;
295 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = em->fifo_size;
Florin Corasff6e7692017-12-11 04:59:01 -0800296 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
297 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
Florin Corasa849b7b2018-05-18 00:41:55 -0700298 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 256;
Florin Corasb384b542018-01-15 01:08:33 -0800299 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase3e2f072018-03-04 07:24:30 -0800300
301 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
Dave Barachb7b92992018-10-17 10:38:51 -0400302 clib_memset (cert_mp, 0, sizeof (*cert_mp));
Florin Corase3e2f072018-03-04 07:24:30 -0800303 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
304 cert_mp->client_index = em->my_client_index;
305 cert_mp->context = ntohl (0xfeedface);
306 cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500307 clib_memcpy_fast (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800308 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
309
310 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
Dave Barachb7b92992018-10-17 10:38:51 -0400311 clib_memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800312 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
313 key_mp->client_index = em->my_client_index;
314 key_mp->context = ntohl (0xfeedface);
315 key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500316 clib_memcpy_fast (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
Florin Corase3e2f072018-03-04 07:24:30 -0800317 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700318}
319
Florin Coras52207f12018-07-12 14:48:06 -0700320static int
Florin Corasb384b542018-01-15 01:08:33 -0800321application_attach (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700322{
Florin Corasb384b542018-01-15 01:08:33 -0800323 application_send_attach (em);
324 if (wait_for_state_change (em, STATE_ATTACHED))
Florin Corasa5464812017-04-19 13:00:05 -0700325 {
326 clib_warning ("timeout waiting for STATE_ATTACHED");
327 return -1;
328 }
329 return 0;
330}
331
Florin Coras6cf30ad2017-04-04 23:08:23 -0700332void
Florin Corasb384b542018-01-15 01:08:33 -0800333application_detach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700334{
335 vl_api_application_detach_t *bmp;
336 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400337 clib_memset (bmp, 0, sizeof (*bmp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700338
339 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Corasb384b542018-01-15 01:08:33 -0800340 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700341 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800342 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
343
Florin Corasa849b7b2018-05-18 00:41:55 -0700344 DBG ("%s", "Sent detach");
Florin Corasb384b542018-01-15 01:08:33 -0800345}
346
347static int
Florin Coras99368312018-08-02 10:45:44 -0700348ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
Florin Corasb384b542018-01-15 01:08:33 -0800349{
350 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -0800351 svm_fifo_segment_main_t *sm = &echo_main.segment_main;
Florin Corasb384b542018-01-15 01:08:33 -0800352 int rv;
353
Dave Barachb7b92992018-10-17 10:38:51 -0400354 clib_memset (a, 0, sizeof (*a));
Florin Corasb384b542018-01-15 01:08:33 -0800355 a->segment_name = (char *) name;
Florin Corasb384b542018-01-15 01:08:33 -0800356 a->segment_type = type;
357
358 if (type == SSVM_SEGMENT_MEMFD)
Florin Coras99368312018-08-02 10:45:44 -0700359 a->memfd_fd = fd;
Florin Corasb384b542018-01-15 01:08:33 -0800360
Florin Corasadc74d72018-12-02 13:36:00 -0800361 if ((rv = svm_fifo_segment_attach (sm, a)))
Florin Corasb384b542018-01-15 01:08:33 -0800362 {
363 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
364 return rv;
365 }
366
Florin Coras99368312018-08-02 10:45:44 -0700367 vec_reset_length (a->new_segment_indices);
Florin Corasb384b542018-01-15 01:08:33 -0800368 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700369}
370
371static void
372vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
373 mp)
374{
Florin Corasb384b542018-01-15 01:08:33 -0800375 echo_main_t *em = &echo_main;
Florin Coras99368312018-08-02 10:45:44 -0700376 int *fds = 0;
377 u32 n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700378
379 if (mp->retval)
380 {
Florin Corasa5464812017-04-19 13:00:05 -0700381 clib_warning ("attach failed: %U", format_api_error,
382 clib_net_to_host_u32 (mp->retval));
Florin Coras99368312018-08-02 10:45:44 -0700383 goto failed;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700384 }
385
386 if (mp->segment_name_length == 0)
387 {
388 clib_warning ("segment_name_length zero");
Florin Coras99368312018-08-02 10:45:44 -0700389 goto failed;
Florin Corasb384b542018-01-15 01:08:33 -0800390 }
391
392 ASSERT (mp->app_event_queue_address);
393 em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
Florin Coras3c2fed52018-07-04 04:15:05 -0700394 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700395
396 if (mp->n_fds)
397 {
398 vec_validate (fds, mp->n_fds);
399 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
400
401 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
402 if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
403 goto failed;
404
405 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
406 if (ssvm_segment_attach ((char *) mp->segment_name,
407 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
408 goto failed;
409
410 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
411 svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
412
413 vec_free (fds);
414 }
415 else
416 {
417 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
418 -1))
419 goto failed;
420 }
421
Florin Corasb384b542018-01-15 01:08:33 -0800422 em->state = STATE_ATTACHED;
Florin Coras99368312018-08-02 10:45:44 -0700423 return;
424failed:
425 em->state = STATE_FAILED;
426 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700427}
428
429static void
430vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
431 mp)
432{
433 if (mp->retval)
434 clib_warning ("detach returned with err: %d", mp->retval);
Florin Corasa849b7b2018-05-18 00:41:55 -0700435 echo_main.state = STATE_DETACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700436}
437
Dave Barach68b0fb02017-02-28 15:15:56 -0500438static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500439stop_signal (int signum)
440{
Florin Corasb384b542018-01-15 01:08:33 -0800441 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500442
443 um->time_to_stop = 1;
444}
445
446static void
447stats_signal (int signum)
448{
Florin Corasb384b542018-01-15 01:08:33 -0800449 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500450
451 um->time_to_print_stats = 1;
452}
453
454static clib_error_t *
455setup_signal_handlers (void)
456{
457 signal (SIGINT, stats_signal);
458 signal (SIGQUIT, stop_signal);
459 signal (SIGTERM, stop_signal);
460
461 return 0;
462}
463
464void
465vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
466{
467 clib_warning ("BUG");
468}
469
470int
471connect_to_vpp (char *name)
472{
Florin Corasb384b542018-01-15 01:08:33 -0800473 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500474 api_main_t *am = &api_main;
475
Florin Corasb384b542018-01-15 01:08:33 -0800476 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800477 {
Florin Corasb384b542018-01-15 01:08:33 -0800478 if (vl_socket_client_connect ((char *) em->socket_name, name,
Florin Coras90a63982017-12-19 04:50:01 -0800479 0 /* default rx, tx buffer */ ))
Florin Corasb384b542018-01-15 01:08:33 -0800480 {
481 clib_warning ("socket connect failed");
482 return -1;
483 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500484
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100485 if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
Florin Corasb384b542018-01-15 01:08:33 -0800486 {
487 clib_warning ("init shm api failed");
488 return -1;
489 }
Florin Coras90a63982017-12-19 04:50:01 -0800490 }
491 else
492 {
493 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
Florin Corasb384b542018-01-15 01:08:33 -0800494 {
495 clib_warning ("shmem connect failed");
496 return -1;
497 }
Florin Coras90a63982017-12-19 04:50:01 -0800498 }
Florin Corasb384b542018-01-15 01:08:33 -0800499 em->vl_input_queue = am->shmem_hdr->vl_input_queue;
500 em->my_client_index = am->my_client_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500501 return 0;
502}
503
Florin Coras90a63982017-12-19 04:50:01 -0800504void
Florin Corasb384b542018-01-15 01:08:33 -0800505disconnect_from_vpp (echo_main_t * em)
Florin Coras90a63982017-12-19 04:50:01 -0800506{
Florin Corasb384b542018-01-15 01:08:33 -0800507 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800508 vl_socket_client_disconnect ();
509 else
510 vl_client_disconnect_from_vlib ();
511}
512
Dave Barach68b0fb02017-02-28 15:15:56 -0500513static void
Florin Corase04c2992017-03-01 08:17:34 -0800514vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500515{
Florin Corasadc74d72018-12-02 13:36:00 -0800516 svm_fifo_segment_main_t *sm = &echo_main.segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500517 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 */
Florin Corasadc74d72018-12-02 13:36:00 -0800524 rv = svm_fifo_segment_attach (sm, a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500525 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 Coras9e47ac52019-01-24 23:22:37 -0800536session_print_stats (echo_main_t * em, echo_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 Coras9e47ac52019-01-24 23:22:37 -0800548test_recv_bytes (echo_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
Florin Coras9e47ac52019-01-24 23:22:37 -0800563recv_data_chunk (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
Florin Corasa849b7b2018-05-18 00:41:55 -0700564{
Florin Coras9e47ac52019-01-24 23:22:37 -0800565 int n_to_read, n_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500566
Florin Coras9e47ac52019-01-24 23:22:37 -0800567 n_to_read = svm_fifo_max_dequeue (s->rx_fifo);
568 if (!n_to_read)
569 return;
Florin Coras6792ec02017-03-13 03:49:51 -0700570
Dave Barach68b0fb02017-02-28 15:15:56 -0500571 do
572 {
Florin Coras9e47ac52019-01-24 23:22:37 -0800573 n_read = app_recv_stream ((app_session_t *) s, rx_buf,
574 vec_len (rx_buf));
575
576 if (n_read > 0)
577 {
578 if (em->test_return_packets)
579 test_recv_bytes (s, rx_buf, n_read);
580
581 n_to_read -= n_read;
582
583 s->bytes_received += n_read;
584 s->bytes_to_receive -= n_read;
585 }
586 else
Florin Corasa849b7b2018-05-18 00:41:55 -0700587 break;
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 Coras9e47ac52019-01-24 23:22:37 -0800593client_handle_rx (echo_main_t * em, session_event_t * e, u8 * rx_buf)
Dave Barach68b0fb02017-02-28 15:15:56 -0500594{
Florin Coras9e47ac52019-01-24 23:22:37 -0800595 echo_session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500596
Florin Corasa849b7b2018-05-18 00:41:55 -0700597 s = pool_elt_at_index (em->sessions, e->fifo->client_session_index);
Florin Coras9e47ac52019-01-24 23:22:37 -0800598 recv_data_chunk (em, s, rx_buf);
Florin Corasa849b7b2018-05-18 00:41:55 -0700599}
600
601static void
Florin Coras9e47ac52019-01-24 23:22:37 -0800602send_data_chunk (echo_main_t * em, echo_session_t * s)
Florin Corasa849b7b2018-05-18 00:41:55 -0700603{
604 u64 test_buf_len, bytes_this_chunk, test_buf_offset;
Florin Corasa849b7b2018-05-18 00:41:55 -0700605 u8 *test_data = em->connect_test_data;
Florin Coras9e47ac52019-01-24 23:22:37 -0800606 int n_sent;
Florin Corasa849b7b2018-05-18 00:41:55 -0700607
608 test_buf_len = vec_len (test_data);
609 test_buf_offset = s->bytes_sent % test_buf_len;
610 bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
611 s->bytes_to_send);
Florin Corasa849b7b2018-05-18 00:41:55 -0700612
Florin Coras9e47ac52019-01-24 23:22:37 -0800613 n_sent = app_send_stream ((app_session_t *) s, test_data + test_buf_offset,
614 bytes_this_chunk, 0);
Florin Corasa849b7b2018-05-18 00:41:55 -0700615
Florin Coras9e47ac52019-01-24 23:22:37 -0800616 if (n_sent > 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500617 {
Florin Coras9e47ac52019-01-24 23:22:37 -0800618 s->bytes_to_send -= n_sent;
619 s->bytes_sent += n_sent;
Dave Barach68b0fb02017-02-28 15:15:56 -0500620 }
621}
622
Florin Corasa849b7b2018-05-18 00:41:55 -0700623/*
624 * Rx/Tx polling thread per connection
625 */
Florin Corase04c2992017-03-01 08:17:34 -0800626static void *
Florin Corasa849b7b2018-05-18 00:41:55 -0700627client_thread_fn (void *arg)
628{
629 echo_main_t *em = &echo_main;
630 static u8 *rx_buf = 0;
631 u32 session_index = *(u32 *) arg;
Florin Coras9e47ac52019-01-24 23:22:37 -0800632 echo_session_t *s;
Florin Corasa849b7b2018-05-18 00:41:55 -0700633
634 vec_validate (rx_buf, 1 << 20);
635
636 while (!em->time_to_stop && em->state != STATE_READY)
637 ;
638
639 s = pool_elt_at_index (em->sessions, session_index);
640 while (!em->time_to_stop)
641 {
Florin Coras9e47ac52019-01-24 23:22:37 -0800642 send_data_chunk (em, s);
643 recv_data_chunk (em, s, rx_buf);
Florin Corasa849b7b2018-05-18 00:41:55 -0700644 if (!s->bytes_to_send && !s->bytes_to_receive)
645 break;
646 }
647
648 DBG ("session %d done", session_index);
649 em->tx_total += s->bytes_sent;
650 em->rx_total += s->bytes_received;
651 em->n_active_clients--;
652
653 pthread_exit (0);
654}
655
656/*
657 * Rx thread that handles all connections.
658 *
659 * Not used.
660 */
661void *
Florin Corase04c2992017-03-01 08:17:34 -0800662client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500663{
Florin Coras52207f12018-07-12 14:48:06 -0700664 session_event_t _e, *e = &_e;
Florin Corasb384b542018-01-15 01:08:33 -0800665 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -0700666 static u8 *rx_buf = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700667 svm_msg_q_msg_t msg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500668
Florin Corasa849b7b2018-05-18 00:41:55 -0700669 vec_validate (rx_buf, 1 << 20);
670
671 while (!em->time_to_stop && em->state != STATE_READY)
672 ;
673
674 while (!em->time_to_stop)
Dave Barach68b0fb02017-02-28 15:15:56 -0500675 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700676 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
677 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500678 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800679 {
Florin Corasa5464812017-04-19 13:00:05 -0700680 case FIFO_EVENT_APP_RX:
Florin Coras9e47ac52019-01-24 23:22:37 -0800681 client_handle_rx (em, e, rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800682 break;
Florin Corase04c2992017-03-01 08:17:34 -0800683 default:
684 clib_warning ("unknown event type %d", e->event_type);
685 break;
686 }
Florin Coras3c2fed52018-07-04 04:15:05 -0700687 svm_msg_q_free_msg (em->our_event_queue, &msg);
Dave Barach68b0fb02017-02-28 15:15:56 -0500688 }
Florin Corase04c2992017-03-01 08:17:34 -0800689 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500690}
691
Florin Coras52207f12018-07-12 14:48:06 -0700692void
693client_send_connect (echo_main_t * em)
694{
695 vl_api_connect_uri_t *cmp;
696 cmp = vl_msg_api_alloc (sizeof (*cmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400697 clib_memset (cmp, 0, sizeof (*cmp));
Florin Coras52207f12018-07-12 14:48:06 -0700698
699 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
700 cmp->client_index = em->my_client_index;
701 cmp->context = ntohl (0xfeedface);
702 memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri));
703 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
704}
705
706void
Florin Coras9e47ac52019-01-24 23:22:37 -0800707client_send_disconnect (echo_main_t * em, echo_session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700708{
709 vl_api_disconnect_session_t *dmp;
710 dmp = vl_msg_api_alloc (sizeof (*dmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400711 clib_memset (dmp, 0, sizeof (*dmp));
Florin Coras52207f12018-07-12 14:48:06 -0700712 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
713 dmp->client_index = em->my_client_index;
714 dmp->handle = s->vpp_session_handle;
715 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
716}
717
718int
Florin Coras9e47ac52019-01-24 23:22:37 -0800719client_disconnect (echo_main_t * em, echo_session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700720{
721 client_send_disconnect (em, s);
722 pool_put (em->sessions, s);
Dave Barachb7b92992018-10-17 10:38:51 -0400723 clib_memset (s, 0xfe, sizeof (*s));
Florin Coras52207f12018-07-12 14:48:06 -0700724 return 0;
725}
726
Dave Barach68b0fb02017-02-28 15:15:56 -0500727static void
Florin Coras9e47ac52019-01-24 23:22:37 -0800728session_bound_handler (session_bound_msg_t * mp)
729{
730 echo_main_t *em = &echo_main;
731
732 if (mp->retval)
733 {
734 clib_warning ("bind failed: %U", format_api_error,
735 clib_net_to_host_u32 (mp->retval));
736 em->state = STATE_FAILED;
737 return;
738 }
739
740 clib_warning ("listening on %U:%u", format_ip46_address, mp->lcl_ip,
741 mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, mp->lcl_port);
742 em->state = STATE_READY;
743}
744
745static void
Florin Coras52207f12018-07-12 14:48:06 -0700746session_accepted_handler (session_accepted_msg_t * mp)
747{
748 app_session_evt_t _app_evt, *app_evt = &_app_evt;
749 session_accepted_reply_msg_t *rmp;
750 svm_fifo_t *rx_fifo, *tx_fifo;
751 echo_main_t *em = &echo_main;
Florin Coras9e47ac52019-01-24 23:22:37 -0800752 echo_session_t *session;
Florin Coras52207f12018-07-12 14:48:06 -0700753 static f64 start_time;
754 u32 session_index;
755 u8 *ip_str;
756
757 if (start_time == 0.0)
758 start_time = clib_time_now (&em->clib_time);
759
760 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
761 clib_warning ("Accepted session from: %s:%d", ip_str,
762 clib_net_to_host_u16 (mp->port));
763
764 /* Allocate local session and set it up */
765 pool_get (em->sessions, session);
766 session_index = session - em->sessions;
767
768 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
769 rx_fifo->client_session_index = session_index;
770 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
771 tx_fifo->client_session_index = session_index;
772
Florin Coras9e47ac52019-01-24 23:22:37 -0800773 session->rx_fifo = rx_fifo;
774 session->tx_fifo = tx_fifo;
Florin Coras52207f12018-07-12 14:48:06 -0700775 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
776 svm_msg_q_t *);
777
778 /* Add it to lookup table */
779 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
780
781 em->state = STATE_READY;
782
783 /* Stats printing */
784 if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
785 {
786 f64 now = clib_time_now (&em->clib_time);
787 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
788 pool_elts (em->sessions), now - start_time,
789 (f64) pool_elts (em->sessions) / (now - start_time));
790 }
791
792 /*
793 * Send accept reply to vpp
794 */
795 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
796 SESSION_CTRL_EVT_ACCEPTED_REPLY);
797 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
798 rmp->handle = mp->handle;
799 rmp->context = mp->context;
800 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
801
802 session->bytes_received = 0;
803 session->start = clib_time_now (&em->clib_time);
804}
805
806static void
807session_connected_handler (session_connected_msg_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500808{
Florin Corasb384b542018-01-15 01:08:33 -0800809 echo_main_t *em = &echo_main;
Florin Coras9e47ac52019-01-24 23:22:37 -0800810 echo_session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500811 u32 session_index;
812 svm_fifo_t *rx_fifo, *tx_fifo;
813 int rv;
814
815 if (mp->retval)
816 {
Florin Corasa5464812017-04-19 13:00:05 -0700817 clib_warning ("connection failed with code: %U", format_api_error,
818 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800819 em->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500820 return;
821 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500822
823 /*
824 * Setup session
825 */
826
Florin Corasb384b542018-01-15 01:08:33 -0800827 pool_get (em->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -0400828 clib_memset (session, 0, sizeof (*session));
Florin Corasb384b542018-01-15 01:08:33 -0800829 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -0500830
Damjan Marion7bee80c2017-04-26 15:32:12 +0200831 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500832 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200833 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500834 tx_fifo->client_session_index = session_index;
835
Florin Coras9e47ac52019-01-24 23:22:37 -0800836 session->rx_fifo = rx_fifo;
837 session->tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700838 session->vpp_session_handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800839 session->start = clib_time_now (&em->clib_time);
Florin Corasa849b7b2018-05-18 00:41:55 -0700840 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
Florin Coras3c2fed52018-07-04 04:15:05 -0700841 svm_msg_q_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500842
Florin Corasb384b542018-01-15 01:08:33 -0800843 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800844
Florin Corasa849b7b2018-05-18 00:41:55 -0700845 /*
846 * Start RX thread
847 */
848 em->thread_args[em->n_clients_connected] = session_index;
849 rv = pthread_create (&em->client_thread_handles[em->n_clients_connected],
850 NULL /*attr */ , client_thread_fn,
851 (void *) &em->thread_args[em->n_clients_connected]);
Florin Corase04c2992017-03-01 08:17:34 -0800852 if (rv)
853 {
854 clib_warning ("pthread_create returned %d", rv);
Florin Corasa849b7b2018-05-18 00:41:55 -0700855 return;
Florin Coras6792ec02017-03-13 03:49:51 -0700856 }
857
Florin Corasa849b7b2018-05-18 00:41:55 -0700858 em->n_clients_connected += 1;
859 clib_warning ("session %u (0x%llx) connected with local ip %U port %d",
860 session_index, mp->handle, format_ip46_address, mp->lcl_ip,
861 mp->is_ip4, clib_net_to_host_u16 (mp->lcl_port));
Florin Corase04c2992017-03-01 08:17:34 -0800862}
863
Florin Coras52207f12018-07-12 14:48:06 -0700864static void
865session_disconnected_handler (session_disconnected_msg_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800866{
Florin Coras52207f12018-07-12 14:48:06 -0700867 app_session_evt_t _app_evt, *app_evt = &_app_evt;
868 session_disconnected_reply_msg_t *rmp;
869 echo_main_t *em = &echo_main;
Florin Coras9e47ac52019-01-24 23:22:37 -0800870 echo_session_t *session = 0;
Florin Coras52207f12018-07-12 14:48:06 -0700871 uword *p;
872 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800873
Florin Coras52207f12018-07-12 14:48:06 -0700874 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700875 if (!p)
Florin Coras52207f12018-07-12 14:48:06 -0700876 {
877 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700878 return;
Florin Coras52207f12018-07-12 14:48:06 -0700879 }
880
Florin Coras3d0fadc2018-07-17 05:35:47 -0700881 session = pool_elt_at_index (em->sessions, p[0]);
882 hash_unset (em->session_index_by_vpp_handles, mp->handle);
883 pool_put (em->sessions, session);
884
Florin Coras52207f12018-07-12 14:48:06 -0700885 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
886 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
887 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
888 rmp->retval = rv;
889 rmp->handle = mp->handle;
890 rmp->context = mp->context;
891 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
892
Florin Coras3d0fadc2018-07-17 05:35:47 -0700893 session_print_stats (em, session);
Florin Corase04c2992017-03-01 08:17:34 -0800894}
895
Florin Coras52207f12018-07-12 14:48:06 -0700896static void
897session_reset_handler (session_reset_msg_t * mp)
Florin Corase04c2992017-03-01 08:17:34 -0800898{
Florin Coras52207f12018-07-12 14:48:06 -0700899 app_session_evt_t _app_evt, *app_evt = &_app_evt;
900 echo_main_t *em = &echo_main;
901 session_reset_reply_msg_t *rmp;
Florin Coras9e47ac52019-01-24 23:22:37 -0800902 echo_session_t *session = 0;
Florin Coras52207f12018-07-12 14:48:06 -0700903 uword *p;
904 int rv = 0;
905
906 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
907
908 if (p)
909 {
910 session = pool_elt_at_index (em->sessions, p[0]);
911 clib_warning ("got reset");
912 /* Cleanup later */
913 em->time_to_stop = 1;
914 }
915 else
916 {
917 clib_warning ("couldn't find session key %llx", mp->handle);
918 return;
919 }
920
921 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
922 SESSION_CTRL_EVT_RESET_REPLY);
923 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
924 rmp->retval = rv;
925 rmp->handle = mp->handle;
926 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
Florin Corase04c2992017-03-01 08:17:34 -0800927}
928
Florin Coras52207f12018-07-12 14:48:06 -0700929static void
930handle_mq_event (session_event_t * e)
Florin Corasa5464812017-04-19 13:00:05 -0700931{
Florin Coras52207f12018-07-12 14:48:06 -0700932 switch (e->event_type)
933 {
Florin Coras9e47ac52019-01-24 23:22:37 -0800934 case SESSION_CTRL_EVT_BOUND:
935 session_bound_handler ((session_bound_msg_t *) e->data);
936 break;
Florin Coras52207f12018-07-12 14:48:06 -0700937 case SESSION_CTRL_EVT_ACCEPTED:
938 session_accepted_handler ((session_accepted_msg_t *) e->data);
939 break;
940 case SESSION_CTRL_EVT_CONNECTED:
941 session_connected_handler ((session_connected_msg_t *) e->data);
942 break;
943 case SESSION_CTRL_EVT_DISCONNECTED:
944 session_disconnected_handler ((session_disconnected_msg_t *) e->data);
945 break;
946 case SESSION_CTRL_EVT_RESET:
947 session_reset_handler ((session_reset_msg_t *) e->data);
948 break;
949 default:
950 clib_warning ("unhandled %u", e->event_type);
951 }
Florin Corasa5464812017-04-19 13:00:05 -0700952}
953
Florin Corase04c2992017-03-01 08:17:34 -0800954static void
Florin Corasa849b7b2018-05-18 00:41:55 -0700955clients_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800956{
Florin Corasa849b7b2018-05-18 00:41:55 -0700957 f64 start_time, deltat, timeout = 100.0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700958 svm_msg_q_msg_t msg;
Florin Coras52207f12018-07-12 14:48:06 -0700959 session_event_t *e;
Florin Coras9e47ac52019-01-24 23:22:37 -0800960 echo_session_t *s;
Florin Corase04c2992017-03-01 08:17:34 -0800961 int i;
962
Florin Corase04c2992017-03-01 08:17:34 -0800963 /* Init test data */
Florin Coras8e43d042018-05-04 15:46:57 -0700964 vec_validate (em->connect_test_data, 1024 * 1024 - 1);
Florin Corasb384b542018-01-15 01:08:33 -0800965 for (i = 0; i < vec_len (em->connect_test_data); i++)
966 em->connect_test_data[i] = i & 0xff;
Florin Corase04c2992017-03-01 08:17:34 -0800967
Florin Corasa849b7b2018-05-18 00:41:55 -0700968 /*
969 * Attach and connect the clients
970 */
971 if (application_attach (em))
972 return;
Florin Corase04c2992017-03-01 08:17:34 -0800973
Florin Corasa849b7b2018-05-18 00:41:55 -0700974 for (i = 0; i < em->n_clients; i++)
975 client_send_connect (em);
976
977 start_time = clib_time_now (&em->clib_time);
978 while (em->n_clients_connected < em->n_clients
979 && (clib_time_now (&em->clib_time) - start_time < timeout)
980 && em->state != STATE_FAILED)
Florin Coras52207f12018-07-12 14:48:06 -0700981
982 {
983 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
984 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
985 handle_mq_event (e);
986 svm_msg_q_free_msg (em->our_event_queue, &msg);
987 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700988
989 if (em->n_clients_connected != em->n_clients)
990 {
991 clib_warning ("failed to initialize all connections");
992 return;
993 }
994
995 /*
996 * Initialize connections
997 */
998 for (i = 0; i < em->n_clients; i++)
999 {
1000 s = pool_elt_at_index (em->sessions, i);
1001 s->bytes_to_send = em->bytes_to_send;
1002 if (!em->no_return)
1003 s->bytes_to_receive = em->bytes_to_send;
1004 }
1005 em->n_active_clients = em->n_clients_connected;
1006
1007 /*
1008 * Wait for client threads to send the data
1009 */
1010 start_time = clib_time_now (&em->clib_time);
1011 em->state = STATE_READY;
1012 while (em->n_active_clients)
Florin Coras3c2fed52018-07-04 04:15:05 -07001013 if (!svm_msg_q_is_empty (em->our_event_queue))
1014 {
1015 if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0))
1016 {
1017 clib_warning ("svm msg q returned");
Florin Coras52207f12018-07-12 14:48:06 -07001018 continue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001019 }
Florin Coras52207f12018-07-12 14:48:06 -07001020 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
1021 if (e->event_type != FIFO_EVENT_APP_RX)
1022 handle_mq_event (e);
1023 svm_msg_q_free_msg (em->our_event_queue, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -07001024 }
Florin Corasa849b7b2018-05-18 00:41:55 -07001025
1026 for (i = 0; i < em->n_clients; i++)
1027 {
1028 s = pool_elt_at_index (em->sessions, i);
1029 client_disconnect (em, s);
1030 }
1031
1032 /*
1033 * Stats and detach
1034 */
1035 deltat = clib_time_now (&em->clib_time) - start_time;
1036 fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds\n",
1037 em->tx_total, em->tx_total / (1ULL << 20),
1038 em->tx_total / (1ULL << 30), deltat);
1039 fformat (stdout, "%.4f Gbit/second\n", (em->tx_total * 8.0) / deltat / 1e9);
1040
Florin Corasb384b542018-01-15 01:08:33 -08001041 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -08001042}
1043
1044static void
1045vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
1046{
Florin Corasb384b542018-01-15 01:08:33 -08001047 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -08001048
1049 if (mp->retval)
1050 {
flyingeagle23a07779f2017-04-26 20:22:04 +08001051 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -07001052 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -08001053 em->state = STATE_FAILED;
Florin Corase04c2992017-03-01 08:17:34 -08001054 return;
1055 }
1056
Florin Corasb384b542018-01-15 01:08:33 -08001057 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001058}
1059
Dave Barach68b0fb02017-02-28 15:15:56 -05001060static void
Florin Corase04c2992017-03-01 08:17:34 -08001061vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -05001062{
Florin Corasb384b542018-01-15 01:08:33 -08001063 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001064
1065 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -08001066 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -05001067
Florin Corasb384b542018-01-15 01:08:33 -08001068 em->state = STATE_START;
Dave Barach68b0fb02017-02-28 15:15:56 -05001069}
1070
Florin Coras6cf30ad2017-04-04 23:08:23 -07001071u8 *
1072format_ip4_address (u8 * s, va_list * args)
1073{
1074 u8 *a = va_arg (*args, u8 *);
1075 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1076}
1077
1078u8 *
1079format_ip6_address (u8 * s, va_list * args)
1080{
1081 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1082 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1083
1084 i_max_n_zero = ARRAY_LEN (a->as_u16);
1085 max_n_zeros = 0;
1086 i_first_zero = i_max_n_zero;
1087 n_zeros = 0;
1088 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1089 {
1090 u32 is_zero = a->as_u16[i] == 0;
1091 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1092 {
1093 i_first_zero = i;
1094 n_zeros = 0;
1095 }
1096 n_zeros += is_zero;
1097 if ((!is_zero && n_zeros > max_n_zeros)
1098 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1099 {
1100 i_max_n_zero = i_first_zero;
1101 max_n_zeros = n_zeros;
1102 i_first_zero = ARRAY_LEN (a->as_u16);
1103 n_zeros = 0;
1104 }
1105 }
1106
1107 last_double_colon = 0;
1108 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1109 {
1110 if (i == i_max_n_zero && max_n_zeros > 1)
1111 {
1112 s = format (s, "::");
1113 i += max_n_zeros - 1;
1114 last_double_colon = 1;
1115 }
1116 else
1117 {
1118 s = format (s, "%s%x",
1119 (last_double_colon || i == 0) ? "" : ":",
1120 clib_net_to_host_u16 (a->as_u16[i]));
1121 last_double_colon = 0;
1122 }
1123 }
1124
1125 return s;
1126}
1127
1128/* Format an IP46 address. */
1129u8 *
1130format_ip46_address (u8 * s, va_list * args)
1131{
1132 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1133 ip46_type_t type = va_arg (*args, ip46_type_t);
1134 int is_ip4 = 1;
1135
1136 switch (type)
1137 {
1138 case IP46_TYPE_ANY:
1139 is_ip4 = ip46_address_is_ip4 (ip46);
1140 break;
1141 case IP46_TYPE_IP4:
1142 is_ip4 = 1;
1143 break;
1144 case IP46_TYPE_IP6:
1145 is_ip4 = 0;
1146 break;
1147 }
1148
1149 return is_ip4 ?
1150 format (s, "%U", format_ip4_address, &ip46->ip4) :
1151 format (s, "%U", format_ip6_address, &ip46->ip6);
1152}
1153
Dave Barach68b0fb02017-02-28 15:15:56 -05001154static void
Florin Coras9e47ac52019-01-24 23:22:37 -08001155server_handle_rx (echo_main_t * em, session_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -05001156{
Florin Coras9e47ac52019-01-24 23:22:37 -08001157 int n_read, max_dequeue, n_sent;
1158 u32 offset, to_dequeue;
1159 echo_session_t *s;
Florin Corase04c2992017-03-01 08:17:34 -08001160
Florin Coras9e47ac52019-01-24 23:22:37 -08001161 s = pool_elt_at_index (em->sessions, e->fifo->client_session_index);
Florin Corase04c2992017-03-01 08:17:34 -08001162
Florin Coras9e47ac52019-01-24 23:22:37 -08001163 /* Clear event only once. Otherwise, if we do it in the loop by calling
1164 * app_recv_stream, we may end up with a lot of unhandled rx events on the
1165 * message queue */
1166 svm_fifo_unset_event (s->rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -07001167
Florin Coras9e47ac52019-01-24 23:22:37 -08001168 max_dequeue = svm_fifo_max_dequeue (s->rx_fifo);
Florin Corasa849b7b2018-05-18 00:41:55 -07001169 if (PREDICT_FALSE (!max_dequeue))
1170 return;
Florin Coras6792ec02017-03-13 03:49:51 -07001171
Florin Corase04c2992017-03-01 08:17:34 -08001172 do
1173 {
Florin Coras9e47ac52019-01-24 23:22:37 -08001174 /* The options here are to limit ourselves to max_dequeue or read
1175 * even the data that was enqueued while we were dequeueing and which
1176 * now has an rx event in the mq. Either of the two work. */
1177 to_dequeue = clib_min (max_dequeue, vec_len (em->rx_buf));
1178 n_read = app_recv_stream_raw (s->rx_fifo, em->rx_buf, to_dequeue,
1179 0 /* clear evt */ , 0 /* peek */ );
Florin Coras6792ec02017-03-13 03:49:51 -07001180 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -08001181 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001182 max_dequeue -= n_read;
Florin Coras9e47ac52019-01-24 23:22:37 -08001183 s->bytes_received += n_read;
Florin Corasf03a59a2017-06-09 21:07:32 -07001184 }
Florin Coras9e47ac52019-01-24 23:22:37 -08001185 else
1186 break;
Florin Corasf03a59a2017-06-09 21:07:32 -07001187
1188 /* Reflect if a non-drop session */
Florin Coras8e43d042018-05-04 15:46:57 -07001189 if (!em->no_return && n_read > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001190 {
1191 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001192 do
1193 {
Florin Coras9e47ac52019-01-24 23:22:37 -08001194 n_sent = app_send_stream ((app_session_t *) s,
1195 &em->rx_buf[offset],
1196 n_read, SVM_Q_WAIT);
1197 if (n_sent > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001198 {
Florin Coras9e47ac52019-01-24 23:22:37 -08001199 n_read -= n_sent;
1200 offset += n_sent;
Florin Corasf03a59a2017-06-09 21:07:32 -07001201 }
Florin Corase04c2992017-03-01 08:17:34 -08001202 }
Florin Coras9e47ac52019-01-24 23:22:37 -08001203 while ((n_sent <= 0 || n_read > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001204 }
Florin Corase04c2992017-03-01 08:17:34 -08001205 }
Florin Coras9e47ac52019-01-24 23:22:37 -08001206 while (max_dequeue > 0 && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001207}
1208
Florin Coras52207f12018-07-12 14:48:06 -07001209static void
Florin Coras9e47ac52019-01-24 23:22:37 -08001210server_handle_mq (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001211{
Florin Coras3c2fed52018-07-04 04:15:05 -07001212 svm_msg_q_msg_t msg;
Florin Coras52207f12018-07-12 14:48:06 -07001213 session_event_t *e;
Florin Corase04c2992017-03-01 08:17:34 -08001214
1215 while (1)
1216 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001217 svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
1218 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
Florin Corase04c2992017-03-01 08:17:34 -08001219 switch (e->event_type)
1220 {
Florin Corasa5464812017-04-19 13:00:05 -07001221 case FIFO_EVENT_APP_RX:
Florin Coras9e47ac52019-01-24 23:22:37 -08001222 server_handle_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -08001223 break;
Florin Corase04c2992017-03-01 08:17:34 -08001224 default:
Florin Coras52207f12018-07-12 14:48:06 -07001225 handle_mq_event (e);
Florin Corase04c2992017-03-01 08:17:34 -08001226 break;
1227 }
Florin Corasb384b542018-01-15 01:08:33 -08001228 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001229 break;
Florin Corasb384b542018-01-15 01:08:33 -08001230 if (PREDICT_FALSE (em->time_to_print_stats == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001231 {
Florin Corasb384b542018-01-15 01:08:33 -08001232 em->time_to_print_stats = 0;
1233 fformat (stdout, "%d connections\n", pool_elts (em->sessions));
Florin Corase04c2992017-03-01 08:17:34 -08001234 }
Florin Coras3c2fed52018-07-04 04:15:05 -07001235 svm_msg_q_free_msg (em->our_event_queue, &msg);
Florin Corase04c2992017-03-01 08:17:34 -08001236 }
1237}
1238
1239void
Florin Corasb384b542018-01-15 01:08:33 -08001240server_send_listen (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001241{
1242 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001243 bmp = vl_msg_api_alloc (sizeof (*bmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001244 clib_memset (bmp, 0, sizeof (*bmp));
Florin Corase04c2992017-03-01 08:17:34 -08001245
1246 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001247 bmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -08001248 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -08001249 memcpy (bmp->uri, em->uri, vec_len (em->uri));
1250 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase04c2992017-03-01 08:17:34 -08001251}
1252
Florin Corasa5464812017-04-19 13:00:05 -07001253int
Florin Corasb384b542018-01-15 01:08:33 -08001254server_listen (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001255{
Florin Corasb384b542018-01-15 01:08:33 -08001256 server_send_listen (em);
1257 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -07001258 {
1259 clib_warning ("timeout waiting for STATE_READY");
1260 return -1;
1261 }
1262 return 0;
1263}
1264
Florin Corase04c2992017-03-01 08:17:34 -08001265void
Florin Corasb384b542018-01-15 01:08:33 -08001266server_send_unbind (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001267{
1268 vl_api_unbind_uri_t *ump;
1269
1270 ump = vl_msg_api_alloc (sizeof (*ump));
Dave Barachb7b92992018-10-17 10:38:51 -04001271 clib_memset (ump, 0, sizeof (*ump));
Florin Corase04c2992017-03-01 08:17:34 -08001272
1273 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001274 ump->client_index = em->my_client_index;
1275 memcpy (ump->uri, em->uri, vec_len (em->uri));
1276 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
Florin Corase04c2992017-03-01 08:17:34 -08001277}
1278
Florin Corasa5464812017-04-19 13:00:05 -07001279int
Florin Corasb384b542018-01-15 01:08:33 -08001280server_unbind (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001281{
Florin Corasb384b542018-01-15 01:08:33 -08001282 server_send_unbind (em);
1283 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -07001284 {
1285 clib_warning ("timeout waiting for STATE_START");
1286 return -1;
1287 }
1288 return 0;
1289}
1290
Florin Corase04c2992017-03-01 08:17:34 -08001291void
Florin Corasb384b542018-01-15 01:08:33 -08001292server_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001293{
Florin Coras9e47ac52019-01-24 23:22:37 -08001294 echo_session_t *session;
Florin Coras90a63982017-12-19 04:50:01 -08001295 int i;
1296
1297 /* $$$$ hack preallocation */
1298 for (i = 0; i < 200000; i++)
1299 {
Florin Corasb384b542018-01-15 01:08:33 -08001300 pool_get (em->sessions, session);
Dave Barachb7b92992018-10-17 10:38:51 -04001301 clib_memset (session, 0, sizeof (*session));
Florin Coras90a63982017-12-19 04:50:01 -08001302 }
1303 for (i = 0; i < 200000; i++)
Florin Corasb384b542018-01-15 01:08:33 -08001304 pool_put_index (em->sessions, i);
Florin Coras90a63982017-12-19 04:50:01 -08001305
Florin Corasb384b542018-01-15 01:08:33 -08001306 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -07001307 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001308
Dave Barach68b0fb02017-02-28 15:15:56 -05001309 /* Bind to uri */
Florin Corasb384b542018-01-15 01:08:33 -08001310 if (server_listen (em))
Florin Corasa5464812017-04-19 13:00:05 -07001311 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001312
1313 /* Enter handle event loop */
Florin Coras9e47ac52019-01-24 23:22:37 -08001314 server_handle_mq (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001315
1316 /* Cleanup */
Florin Corasb384b542018-01-15 01:08:33 -08001317 server_send_unbind (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001318
Florin Corasb384b542018-01-15 01:08:33 -08001319 application_detach (em);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001320
Dave Barach68b0fb02017-02-28 15:15:56 -05001321 fformat (stdout, "Test complete...\n");
1322}
1323
Florin Corase69f4952017-03-07 10:06:24 -08001324static void
1325vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1326 mp)
1327{
Florin Corasb384b542018-01-15 01:08:33 -08001328 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -07001329 uword *p;
Florin Coras6792ec02017-03-13 03:49:51 -07001330
Florin Corasf03a59a2017-06-09 21:07:32 -07001331 if (mp->retval)
1332 {
1333 clib_warning ("vpp complained about disconnect: %d",
1334 ntohl (mp->retval));
Florin Corasa849b7b2018-05-18 00:41:55 -07001335 return;
Florin Corasf03a59a2017-06-09 21:07:32 -07001336 }
1337
Florin Corasb384b542018-01-15 01:08:33 -08001338 em->state = STATE_START;
Florin Corasa849b7b2018-05-18 00:41:55 -07001339
1340 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
1341 if (p)
1342 {
1343 hash_unset (em->session_index_by_vpp_handles, mp->handle);
1344 }
1345 else
1346 {
1347 clib_warning ("couldn't find session key %llx", mp->handle);
1348 }
Florin Corase69f4952017-03-07 10:06:24 -08001349}
1350
Florin Corase3e2f072018-03-04 07:24:30 -08001351static void
1352 vl_api_application_tls_cert_add_reply_t_handler
1353 (vl_api_application_tls_cert_add_reply_t * mp)
1354{
1355 if (mp->retval)
1356 clib_warning ("failed to add tls cert");
1357}
1358
1359static void
1360 vl_api_application_tls_key_add_reply_t_handler
1361 (vl_api_application_tls_key_add_reply_t * mp)
1362{
1363 if (mp->retval)
1364 clib_warning ("failed to add tls key");
1365}
1366
1367#define foreach_tcp_echo_msg \
1368_(BIND_URI_REPLY, bind_uri_reply) \
1369_(UNBIND_URI_REPLY, unbind_uri_reply) \
Florin Corase3e2f072018-03-04 07:24:30 -08001370_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Corase3e2f072018-03-04 07:24:30 -08001371_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1372_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1373_(MAP_ANOTHER_SEGMENT, map_another_segment) \
1374_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
1375_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001376
1377void
Florin Corasb384b542018-01-15 01:08:33 -08001378tcp_echo_api_hookup (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -05001379{
1380#define _(N,n) \
1381 vl_msg_api_set_handlers(VL_API_##N, #n, \
1382 vl_api_##n##_t_handler, \
1383 vl_noop_handler, \
1384 vl_api_##n##_t_endian, \
1385 vl_api_##n##_t_print, \
1386 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -08001387 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -05001388#undef _
1389}
1390
1391int
1392main (int argc, char **argv)
1393{
Florin Coras8e43d042018-05-04 15:46:57 -07001394 int i_am_server = 1, test_return_packets = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001395 echo_main_t *em = &echo_main;
Florin Corasadc74d72018-12-02 13:36:00 -08001396 svm_fifo_segment_main_t *sm = &em->segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001397 unformat_input_t _argv, *a = &_argv;
1398 u8 *chroot_prefix;
Dave Barach6a5adc32018-07-04 10:56:23 -04001399 u8 *uri = 0;
Florin Corase69f4952017-03-07 10:06:24 -08001400 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
Florin Coras8e43d042018-05-04 15:46:57 -07001401 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001402 u64 bytes_to_send = 64 << 10, mbytes;
Florin Corasb384b542018-01-15 01:08:33 -08001403 char *app_name;
Dave Barach68b0fb02017-02-28 15:15:56 -05001404 u32 tmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001405
Dave Barach6a5adc32018-07-04 10:56:23 -04001406 clib_mem_init_thread_safe (0, 256 << 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001407
Dave Barachb7b92992018-10-17 10:38:51 -04001408 clib_memset (em, 0, sizeof (*em));
Florin Corasb384b542018-01-15 01:08:33 -08001409 em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Corasb384b542018-01-15 01:08:33 -08001410 em->my_pid = getpid ();
1411 em->configured_segment_size = 1 << 20;
1412 em->socket_name = 0;
1413 em->use_sock_api = 1;
Florin Coras8e43d042018-05-04 15:46:57 -07001414 em->fifo_size = 64 << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001415 em->n_clients = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001416
Florin Corasb384b542018-01-15 01:08:33 -08001417 clib_time_init (&em->clib_time);
1418 init_error_string_table (em);
David Johnsond9818dd2018-12-14 14:53:41 -05001419 svm_fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001420 unformat_init_command_line (a, argv);
1421
1422 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1423 {
1424 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001425 {
1426 vl_set_memory_root_path ((char *) chroot_prefix);
1427 }
Florin Corase69f4952017-03-07 10:06:24 -08001428 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001429 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001430 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001431 em->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001432 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001433 em->configured_segment_size = tmp << 30;
Florin Coras8e43d042018-05-04 15:46:57 -07001434 else if (unformat (a, "server"))
1435 i_am_server = 1;
1436 else if (unformat (a, "client"))
1437 i_am_server = 0;
1438 else if (unformat (a, "no-return"))
1439 em->no_return = 1;
Florin Corase04c2992017-03-01 08:17:34 -08001440 else if (unformat (a, "test"))
1441 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001442 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001443 {
1444 bytes_to_send = mbytes << 20;
1445 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001446 else if (unformat (a, "gbytes %lld", &mbytes))
1447 {
1448 bytes_to_send = mbytes << 30;
1449 }
Florin Corasb384b542018-01-15 01:08:33 -08001450 else if (unformat (a, "socket-name %s", &em->socket_name))
Florin Coras90a63982017-12-19 04:50:01 -08001451 ;
1452 else if (unformat (a, "use-svm-api"))
Florin Corasb384b542018-01-15 01:08:33 -08001453 em->use_sock_api = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001454 else if (unformat (a, "fifo-size %d", &tmp))
1455 em->fifo_size = tmp << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001456 else if (unformat (a, "nclients %d", &em->n_clients))
1457 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001458 else
Florin Corase04c2992017-03-01 08:17:34 -08001459 {
Jerome Tollet61f79122018-06-04 08:31:33 +01001460 fformat (stderr, "%s: usage [master|slave]\n", argv[0]);
Florin Corase04c2992017-03-01 08:17:34 -08001461 exit (1);
1462 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001463 }
1464
Florin Corasb384b542018-01-15 01:08:33 -08001465 if (!em->socket_name)
1466 em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
Florin Coras90a63982017-12-19 04:50:01 -08001467
Florin Corase69f4952017-03-07 10:06:24 -08001468 if (uri)
1469 {
Florin Corasb384b542018-01-15 01:08:33 -08001470 em->uri = format (0, "%s%c", uri, 0);
1471 em->connect_uri = format (0, "%s%c", uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001472 }
1473 else
1474 {
Florin Corasb384b542018-01-15 01:08:33 -08001475 em->uri = format (0, "%s%c", bind_uri, 0);
1476 em->connect_uri = format (0, "%s%c", connect_uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001477 }
1478
Florin Coras8e43d042018-05-04 15:46:57 -07001479 em->i_am_master = i_am_server;
Florin Corasb384b542018-01-15 01:08:33 -08001480 em->test_return_packets = test_return_packets;
1481 em->bytes_to_send = bytes_to_send;
1482 em->time_to_stop = 0;
Florin Coras9e47ac52019-01-24 23:22:37 -08001483 vec_validate (em->rx_buf, 4 << 20);
Florin Corasa849b7b2018-05-18 00:41:55 -07001484 vec_validate (em->client_thread_handles, em->n_clients - 1);
1485 vec_validate (em->thread_args, em->n_clients - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001486
Florin Corase04c2992017-03-01 08:17:34 -08001487 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001488 tcp_echo_api_hookup (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001489
Florin Coras8e43d042018-05-04 15:46:57 -07001490 app_name = i_am_server ? "tcp_echo_server" : "tcp_echo_client";
Florin Corasb384b542018-01-15 01:08:33 -08001491 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001492 {
1493 svm_region_exit ();
1494 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1495 exit (1);
1496 }
1497
Florin Coras8e43d042018-05-04 15:46:57 -07001498 if (i_am_server == 0)
Florin Corasa849b7b2018-05-18 00:41:55 -07001499 clients_run (em);
Florin Coras90a63982017-12-19 04:50:01 -08001500 else
Florin Corasb384b542018-01-15 01:08:33 -08001501 server_run (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001502
Florin Corasa849b7b2018-05-18 00:41:55 -07001503 /* Make sure detach finishes */
1504 wait_for_state_change (em, STATE_DETACHED);
1505
Florin Corasb384b542018-01-15 01:08:33 -08001506 disconnect_from_vpp (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001507 exit (0);
1508}
Florin Corase04c2992017-03-01 08:17:34 -08001509
1510/*
1511 * fd.io coding-style-patch-verification: ON
1512 *
1513 * Local Variables:
1514 * eval: (c-set-style "gnu")
1515 * End:
1516 */