blob: 932918963bcc42f789597133838590182ee6a7c1 [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 Corasa849b7b2018-05-18 00:41:55 -070051 svm_queue_t *vpp_evt_q;
52
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,
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 Corase04c2992017-03-01 08:17:34 -080083 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 Corase86a8ed2018-01-05 03:20:25 -0800102 svm_queue_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500103
104 /* $$$ single thread only for the moment */
Florin Corase86a8ed2018-01-05 03:20:25 -0800105 svm_queue_t *vpp_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500106
Florin Coras90a63982017-12-19 04:50:01 -0800107 u8 *socket_name;
108
Dave Barach68b0fb02017-02-28 15:15:56 -0500109 pid_t my_pid;
110
111 /* For deadman timers */
112 clib_time_t clib_time;
113
114 /* State of the connection, shared between msg RX thread and main thread */
115 volatile connection_state_t state;
116
117 /* Signal variables */
118 volatile int time_to_stop;
119 volatile int time_to_print_stats;
120
121 u32 configured_segment_size;
122
123 /* VNET_API_ERROR_FOO -> "Foo" hash table */
Florin Corase04c2992017-03-01 08:17:34 -0800124 uword *error_string_by_error_number;
Dave Barach68b0fb02017-02-28 15:15:56 -0500125
126 u8 *connect_test_data;
Florin Corasa849b7b2018-05-18 00:41:55 -0700127 pthread_t *client_thread_handles;
128 u32 *thread_args;
Florin Corase04c2992017-03-01 08:17:34 -0800129 u32 client_bytes_received;
130 u8 test_return_packets;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700131 u64 bytes_to_send;
Florin Coras8e43d042018-05-04 15:46:57 -0700132 u32 fifo_size;
Florin Corase04c2992017-03-01 08:17:34 -0800133
Florin Corasa849b7b2018-05-18 00:41:55 -0700134 u32 n_clients;
135 u64 tx_total;
136 u64 rx_total;
137
138 volatile u32 n_clients_connected;
139 volatile u32 n_active_clients;
140
141
Florin Coras90a63982017-12-19 04:50:01 -0800142 /** Flag that decides if socket, instead of svm, api is used to connect to
143 * vpp. If sock api is used, shm binary api is subsequently bootstrapped
144 * and all other messages are exchanged using shm IPC. */
145 u8 use_sock_api;
146
Florin Corase04c2992017-03-01 08:17:34 -0800147 /* convenience */
148 svm_fifo_segment_main_t *segment_main;
Florin Corasb384b542018-01-15 01:08:33 -0800149} echo_main_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500150
Florin Corasb384b542018-01-15 01:08:33 -0800151echo_main_t echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500152
153#if CLIB_DEBUG > 0
154#define NITER 10000
155#else
156#define NITER 4000000
157#endif
158
Florin Corase3e2f072018-03-04 07:24:30 -0800159const char test_srv_crt_rsa[] =
160 "-----BEGIN CERTIFICATE-----\r\n"
Florin Coras8f89dd02018-03-05 16:53:07 -0800161 "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
162 "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
163 "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n"
164 "IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n"
165 "NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n"
166 "MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n"
167 "LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n"
168 "ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n"
169 "4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n"
170 "cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n"
171 "Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n"
172 "ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n"
173 "+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n"
174 "Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n"
175 "aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n"
176 "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n"
177 "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
178 "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
179 "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
180 "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
181 "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
182 "-----END CERTIFICATE-----\r\n";
Florin Corase3e2f072018-03-04 07:24:30 -0800183const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
184
185const char test_srv_key_rsa[] =
Florin Coras8f89dd02018-03-05 16:53:07 -0800186 "-----BEGIN PRIVATE KEY-----\r\n"
187 "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
188 "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
189 "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
190 "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
191 "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
192 "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
193 "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
194 "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
195 "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
196 "LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n"
197 "BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n"
198 "CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n"
199 "Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n"
200 "nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n"
201 "5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n"
202 "ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n"
203 "sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n"
204 "gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n"
205 "Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n"
206 "CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n"
207 "K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n"
208 "VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n"
209 "EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n"
210 "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
211 "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
212 "oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n";
Florin Corase3e2f072018-03-04 07:24:30 -0800213const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
214
Florin Corasa5464812017-04-19 13:00:05 -0700215static u8 *
216format_api_error (u8 * s, va_list * args)
217{
Florin Corasb384b542018-01-15 01:08:33 -0800218 echo_main_t *em = &echo_main;
Florin Corasa5464812017-04-19 13:00:05 -0700219 i32 error = va_arg (*args, u32);
220 uword *p;
221
Florin Corasb384b542018-01-15 01:08:33 -0800222 p = hash_get (em->error_string_by_error_number, -error);
Florin Corasa5464812017-04-19 13:00:05 -0700223
224 if (p)
225 s = format (s, "%s", p[0]);
226 else
227 s = format (s, "%d", error);
228 return s;
229}
230
231static void
Florin Corasb384b542018-01-15 01:08:33 -0800232init_error_string_table (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700233{
Florin Corasb384b542018-01-15 01:08:33 -0800234 em->error_string_by_error_number = hash_create (0, sizeof (uword));
Florin Corasa5464812017-04-19 13:00:05 -0700235
Florin Corasb384b542018-01-15 01:08:33 -0800236#define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
Florin Corasa5464812017-04-19 13:00:05 -0700237 foreach_vnet_api_error;
238#undef _
239
Florin Corasb384b542018-01-15 01:08:33 -0800240 hash_set (em->error_string_by_error_number, 99, "Misc");
Florin Corasa5464812017-04-19 13:00:05 -0700241}
242
Dave Barach68b0fb02017-02-28 15:15:56 -0500243int
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{
246#if CLIB_DEBUG > 0
247#define TIMEOUT 600.0
248#else
249#define TIMEOUT 600.0
250#endif
251
Florin Corasb384b542018-01-15 01:08:33 -0800252 f64 timeout = clib_time_now (&em->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500253
Florin Corasb384b542018-01-15 01:08:33 -0800254 while (clib_time_now (&em->clib_time) < timeout)
Dave Barach68b0fb02017-02-28 15:15:56 -0500255 {
Florin Corasb384b542018-01-15 01:08:33 -0800256 if (em->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800257 return 0;
Florin Corasb384b542018-01-15 01:08:33 -0800258 if (em->state == STATE_FAILED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500259 return -1;
Florin Corasb384b542018-01-15 01:08:33 -0800260 if (em->time_to_stop == 1)
Florin Corasf03a59a2017-06-09 21:07:32 -0700261 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500262 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700263 clib_warning ("timeout waiting for state %d", state);
Dave Barach68b0fb02017-02-28 15:15:56 -0500264 return -1;
265}
266
Florin Coras6cf30ad2017-04-04 23:08:23 -0700267void
Florin Corasb384b542018-01-15 01:08:33 -0800268application_send_attach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700269{
270 vl_api_application_attach_t *bmp;
Florin Corase3e2f072018-03-04 07:24:30 -0800271 vl_api_application_tls_cert_add_t *cert_mp;
272 vl_api_application_tls_key_add_t *key_mp;
273
Florin Coras6cf30ad2017-04-04 23:08:23 -0700274 bmp = vl_msg_api_alloc (sizeof (*bmp));
275 memset (bmp, 0, sizeof (*bmp));
276
277 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Corasb384b542018-01-15 01:08:33 -0800278 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700279 bmp->context = ntohl (0xfeedface);
Florin Corasa5464812017-04-19 13:00:05 -0700280 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700281 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
Dave Barach10d8cc62017-05-30 09:30:07 -0400282 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Coras8e43d042018-05-04 15:46:57 -0700283 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = em->fifo_size;
284 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = em->fifo_size;
Florin Corasff6e7692017-12-11 04:59:01 -0800285 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
286 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
Florin Corasa849b7b2018-05-18 00:41:55 -0700287 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 256;
Florin Corasb384b542018-01-15 01:08:33 -0800288 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase3e2f072018-03-04 07:24:30 -0800289
290 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
291 memset (cert_mp, 0, sizeof (*cert_mp));
292 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
293 cert_mp->client_index = em->my_client_index;
294 cert_mp->context = ntohl (0xfeedface);
295 cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
296 clib_memcpy (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
297 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
298
299 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
300 memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
301 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
302 key_mp->client_index = em->my_client_index;
303 key_mp->context = ntohl (0xfeedface);
304 key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
305 clib_memcpy (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
306 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700307}
308
Florin Corasa5464812017-04-19 13:00:05 -0700309int
Florin Corasb384b542018-01-15 01:08:33 -0800310application_attach (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700311{
Florin Corasb384b542018-01-15 01:08:33 -0800312 application_send_attach (em);
313 if (wait_for_state_change (em, STATE_ATTACHED))
Florin Corasa5464812017-04-19 13:00:05 -0700314 {
315 clib_warning ("timeout waiting for STATE_ATTACHED");
316 return -1;
317 }
318 return 0;
319}
320
Florin Coras6cf30ad2017-04-04 23:08:23 -0700321void
Florin Corasb384b542018-01-15 01:08:33 -0800322application_detach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700323{
324 vl_api_application_detach_t *bmp;
325 bmp = vl_msg_api_alloc (sizeof (*bmp));
326 memset (bmp, 0, sizeof (*bmp));
327
328 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Corasb384b542018-01-15 01:08:33 -0800329 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700330 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800331 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
332
Florin Corasa849b7b2018-05-18 00:41:55 -0700333 DBG ("%s", "Sent detach");
Florin Corasb384b542018-01-15 01:08:33 -0800334}
335
336static int
337memfd_segment_attach (void)
338{
339 ssvm_private_t _ssvm = { 0 }, *ssvm = &_ssvm;
340 clib_error_t *error;
341 int rv;
342
343 if ((error = vl_socket_client_recv_fd_msg (&ssvm->fd, 5)))
344 {
345 clib_error_report (error);
346 return -1;
347 }
348
349 if ((rv = ssvm_slave_init_memfd (ssvm)))
350 return rv;
351
352 return 0;
353}
354
355static int
356fifo_segment_attach (char *name, u32 size, ssvm_segment_type_t type)
357{
358 svm_fifo_segment_create_args_t _a, *a = &_a;
359 clib_error_t *error;
360 int rv;
361
362 memset (a, 0, sizeof (*a));
363 a->segment_name = (char *) name;
364 a->segment_size = size;
365 a->segment_type = type;
366
367 if (type == SSVM_SEGMENT_MEMFD)
368 {
369 if ((error = vl_socket_client_recv_fd_msg (&a->memfd_fd, 5)))
370 {
371 clib_error_report (error);
372 return -1;
373 }
374 }
375
376 if ((rv = svm_fifo_segment_attach (a)))
377 {
378 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
379 return rv;
380 }
381
382 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700383}
384
385static void
386vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
387 mp)
388{
Florin Corasb384b542018-01-15 01:08:33 -0800389 echo_main_t *em = &echo_main;
390 ssvm_segment_type_t seg_type;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700391
392 if (mp->retval)
393 {
Florin Corasa5464812017-04-19 13:00:05 -0700394 clib_warning ("attach failed: %U", format_api_error,
395 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800396 em->state = STATE_FAILED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700397 return;
398 }
399
400 if (mp->segment_name_length == 0)
401 {
402 clib_warning ("segment_name_length zero");
403 return;
404 }
405
Florin Corasb384b542018-01-15 01:08:33 -0800406 seg_type = em->use_sock_api ? SSVM_SEGMENT_MEMFD : SSVM_SEGMENT_SHM;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700407
Florin Corasb384b542018-01-15 01:08:33 -0800408 /* Attach to fifo segment */
409 if (fifo_segment_attach ((char *) mp->segment_name, mp->segment_size,
410 seg_type))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700411 {
Florin Corasb384b542018-01-15 01:08:33 -0800412 em->state = STATE_FAILED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700413 return;
414 }
415
Florin Corasb384b542018-01-15 01:08:33 -0800416 /* If we're using memfd segments, read and attach to event qs segment */
417 if (seg_type == SSVM_SEGMENT_MEMFD)
418 {
419 if (memfd_segment_attach ())
420 {
421 clib_warning ("failed to attach to evt q segment");
422 em->state = STATE_FAILED;
423 return;
424 }
425 }
426
427 ASSERT (mp->app_event_queue_address);
428 em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
429 svm_queue_t *);
430 em->state = STATE_ATTACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700431}
432
433static void
434vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
435 mp)
436{
437 if (mp->retval)
438 clib_warning ("detach returned with err: %d", mp->retval);
Florin Corasa849b7b2018-05-18 00:41:55 -0700439 echo_main.state = STATE_DETACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700440}
441
Dave Barach68b0fb02017-02-28 15:15:56 -0500442static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500443stop_signal (int signum)
444{
Florin Corasb384b542018-01-15 01:08:33 -0800445 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500446
447 um->time_to_stop = 1;
448}
449
450static void
451stats_signal (int signum)
452{
Florin Corasb384b542018-01-15 01:08:33 -0800453 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500454
455 um->time_to_print_stats = 1;
456}
457
458static clib_error_t *
459setup_signal_handlers (void)
460{
461 signal (SIGINT, stats_signal);
462 signal (SIGQUIT, stop_signal);
463 signal (SIGTERM, stop_signal);
464
465 return 0;
466}
467
468void
469vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
470{
471 clib_warning ("BUG");
472}
473
474int
475connect_to_vpp (char *name)
476{
Florin Corasb384b542018-01-15 01:08:33 -0800477 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500478 api_main_t *am = &api_main;
479
Florin Corasb384b542018-01-15 01:08:33 -0800480 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800481 {
Florin Corasb384b542018-01-15 01:08:33 -0800482 if (vl_socket_client_connect ((char *) em->socket_name, name,
Florin Coras90a63982017-12-19 04:50:01 -0800483 0 /* default rx, tx buffer */ ))
Florin Corasb384b542018-01-15 01:08:33 -0800484 {
485 clib_warning ("socket connect failed");
486 return -1;
487 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500488
Florin Corasb384b542018-01-15 01:08:33 -0800489 if (vl_socket_client_init_shm (0))
490 {
491 clib_warning ("init shm api failed");
492 return -1;
493 }
Florin Coras90a63982017-12-19 04:50:01 -0800494 }
495 else
496 {
497 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
Florin Corasb384b542018-01-15 01:08:33 -0800498 {
499 clib_warning ("shmem connect failed");
500 return -1;
501 }
Florin Coras90a63982017-12-19 04:50:01 -0800502 }
Florin Corasb384b542018-01-15 01:08:33 -0800503 em->vl_input_queue = am->shmem_hdr->vl_input_queue;
504 em->my_client_index = am->my_client_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500505 return 0;
506}
507
Florin Coras90a63982017-12-19 04:50:01 -0800508void
Florin Corasb384b542018-01-15 01:08:33 -0800509disconnect_from_vpp (echo_main_t * em)
Florin Coras90a63982017-12-19 04:50:01 -0800510{
Florin Corasb384b542018-01-15 01:08:33 -0800511 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800512 vl_socket_client_disconnect ();
513 else
514 vl_client_disconnect_from_vlib ();
515}
516
Dave Barach68b0fb02017-02-28 15:15:56 -0500517static void
Florin Corase04c2992017-03-01 08:17:34 -0800518vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500519{
520 svm_fifo_segment_create_args_t _a, *a = &_a;
521 int rv;
522
Florin Coras3cbc04b2017-10-02 00:18:51 -0700523 memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500524 a->segment_name = (char *) mp->segment_name;
525 a->segment_size = mp->segment_size;
526 /* Attach to the segment vpp created */
527 rv = svm_fifo_segment_attach (a);
528 if (rv)
529 {
530 clib_warning ("svm_fifo_segment_attach ('%s') failed",
Florin Corase04c2992017-03-01 08:17:34 -0800531 mp->segment_name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500532 return;
533 }
534 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
Florin Corase04c2992017-03-01 08:17:34 -0800535 mp->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500536}
537
538static void
Florin Corasb384b542018-01-15 01:08:33 -0800539session_print_stats (echo_main_t * em, session_t * session)
Florin Corasf03a59a2017-06-09 21:07:32 -0700540{
541 f64 deltat;
542 u64 bytes;
543
Florin Corasb384b542018-01-15 01:08:33 -0800544 deltat = clib_time_now (&em->clib_time) - session->start;
545 bytes = em->i_am_master ? session->bytes_received : em->bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -0700546 fformat (stdout, "Finished in %.6f\n", deltat);
547 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
548}
549
550static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500551vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
552{
Florin Corasb384b542018-01-15 01:08:33 -0800553 echo_main_t *em = &echo_main;
Florin Corasf03a59a2017-06-09 21:07:32 -0700554 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800555 vl_api_disconnect_session_reply_t *rmp;
556 uword *p;
Dave Barach68b0fb02017-02-28 15:15:56 -0500557 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500558
Florin Corasb384b542018-01-15 01:08:33 -0800559 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800560
561 if (p)
562 {
Florin Corasb384b542018-01-15 01:08:33 -0800563 session = pool_elt_at_index (em->sessions, p[0]);
564 hash_unset (em->session_index_by_vpp_handles, mp->handle);
565 pool_put (em->sessions, session);
Florin Corase04c2992017-03-01 08:17:34 -0800566 }
567 else
568 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700569 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800570 rv = -11;
571 }
572
Florin Corasb384b542018-01-15 01:08:33 -0800573// em->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800574
575 rmp = vl_msg_api_alloc (sizeof (*rmp));
576 memset (rmp, 0, sizeof (*rmp));
577
578 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
579 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700580 rmp->handle = mp->handle;
Florin Corasf8f516a2018-02-08 15:10:09 -0800581 rmp->context = mp->context;
Florin Corasb384b542018-01-15 01:08:33 -0800582 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -0700583
584 if (session)
Florin Corasb384b542018-01-15 01:08:33 -0800585 session_print_stats (em, session);
Florin Corase04c2992017-03-01 08:17:34 -0800586}
587
588static void
589vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
590{
Florin Corasb384b542018-01-15 01:08:33 -0800591 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800592 vl_api_reset_session_reply_t *rmp;
593 uword *p;
594 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800595
Florin Corasb384b542018-01-15 01:08:33 -0800596 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500597
598 if (p)
599 {
Florin Corasf6359c82017-06-19 12:26:09 -0400600 clib_warning ("got reset");
601 /* Cleanup later */
Florin Corasb384b542018-01-15 01:08:33 -0800602 em->time_to_stop = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500603 }
604 else
605 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700606 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500607 rv = -11;
608 }
609
610 rmp = vl_msg_api_alloc (sizeof (*rmp));
611 memset (rmp, 0, sizeof (*rmp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800612 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500613 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700614 rmp->handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800615 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500616}
617
Florin Corasa849b7b2018-05-18 00:41:55 -0700618static void
619test_recv_bytes (session_t * s, u8 * rx_buf, u32 n_read)
Dave Barach68b0fb02017-02-28 15:15:56 -0500620{
Florin Corasa849b7b2018-05-18 00:41:55 -0700621 int i;
622 for (i = 0; i < n_read; i++)
623 {
624 if (rx_buf[i] != ((s->bytes_received + i) & 0xff))
625 {
626 clib_warning ("error at byte %lld, 0x%x not 0x%x",
627 s->bytes_received + i, rx_buf[i],
628 ((s->bytes_received + i) & 0xff));
629 }
630 }
631}
Dave Barach68b0fb02017-02-28 15:15:56 -0500632
Florin Corasa849b7b2018-05-18 00:41:55 -0700633static void
634recv_test_chunk (echo_main_t * em, session_t * s, u8 * rx_buf)
635{
636 svm_fifo_t *rx_fifo = s->server_rx_fifo;
637 u32 n_read_now, n_to_read;
638 int n_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500639
Florin Corasa849b7b2018-05-18 00:41:55 -0700640 n_to_read = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700641 svm_fifo_unset_event (rx_fifo);
642
Dave Barach68b0fb02017-02-28 15:15:56 -0500643 do
644 {
Florin Corasa849b7b2018-05-18 00:41:55 -0700645 n_read_now = clib_min (vec_len (rx_buf), n_to_read);
646 n_read = svm_fifo_dequeue_nowait (rx_fifo, n_read_now, rx_buf);
647 if (n_read <= 0)
648 break;
Florin Corase04c2992017-03-01 08:17:34 -0800649
Florin Corasa849b7b2018-05-18 00:41:55 -0700650 if (n_read_now != n_read)
651 clib_warning ("huh?");
652
653 if (em->test_return_packets)
654 test_recv_bytes (s, rx_buf, n_read);
655
656 n_to_read -= n_read;
657 s->bytes_received += n_read;
658 s->bytes_to_receive -= n_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500659 }
Florin Corasa849b7b2018-05-18 00:41:55 -0700660 while (n_to_read > 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500661}
662
663void
Florin Corasa849b7b2018-05-18 00:41:55 -0700664client_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e,
665 u8 * rx_buf)
Dave Barach68b0fb02017-02-28 15:15:56 -0500666{
Florin Corasa849b7b2018-05-18 00:41:55 -0700667 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500668
Florin Corasa849b7b2018-05-18 00:41:55 -0700669 s = pool_elt_at_index (em->sessions, e->fifo->client_session_index);
670 recv_test_chunk (em, s, rx_buf);
671}
672
673static void
674send_test_chunk (echo_main_t * em, session_t * s)
675{
676 u64 test_buf_len, bytes_this_chunk, test_buf_offset;
677 svm_fifo_t *tx_fifo = s->server_tx_fifo;
678 u8 *test_data = em->connect_test_data;
679 u32 enq_space, min_chunk = 16 << 10;
680 session_fifo_event_t evt;
681 int written;
682
683 test_buf_len = vec_len (test_data);
684 test_buf_offset = s->bytes_sent % test_buf_len;
685 bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
686 s->bytes_to_send);
687 enq_space = svm_fifo_max_enqueue (tx_fifo);
688 if (enq_space < clib_min (bytes_this_chunk, min_chunk))
689 return;
690
691 bytes_this_chunk = clib_min (bytes_this_chunk, enq_space);
692 written = svm_fifo_enqueue_nowait (tx_fifo, bytes_this_chunk,
693 test_data + test_buf_offset);
694
695 if (written > 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500696 {
Florin Corasa849b7b2018-05-18 00:41:55 -0700697 s->bytes_to_send -= written;
698 s->bytes_sent += written;
Dave Barach68b0fb02017-02-28 15:15:56 -0500699
Florin Corasa849b7b2018-05-18 00:41:55 -0700700 if (svm_fifo_set_event (tx_fifo))
701 {
702 /* Fabricate TX event, send to vpp */
703 evt.fifo = tx_fifo;
704 evt.event_type = FIFO_EVENT_APP_TX;
705 svm_queue_add (s->vpp_evt_q, (u8 *) & evt, 0 /* wait for mutex */ );
706 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500707 }
708}
709
Florin Corasa849b7b2018-05-18 00:41:55 -0700710/*
711 * Rx/Tx polling thread per connection
712 */
Florin Corase04c2992017-03-01 08:17:34 -0800713static void *
Florin Corasa849b7b2018-05-18 00:41:55 -0700714client_thread_fn (void *arg)
715{
716 echo_main_t *em = &echo_main;
717 static u8 *rx_buf = 0;
718 u32 session_index = *(u32 *) arg;
719 session_t *s;
720
721 vec_validate (rx_buf, 1 << 20);
722
723 while (!em->time_to_stop && em->state != STATE_READY)
724 ;
725
726 s = pool_elt_at_index (em->sessions, session_index);
727 while (!em->time_to_stop)
728 {
729 send_test_chunk (em, s);
730 recv_test_chunk (em, s, rx_buf);
731 if (!s->bytes_to_send && !s->bytes_to_receive)
732 break;
733 }
734
735 DBG ("session %d done", session_index);
736 em->tx_total += s->bytes_sent;
737 em->rx_total += s->bytes_received;
738 em->n_active_clients--;
739
740 pthread_exit (0);
741}
742
743/*
744 * Rx thread that handles all connections.
745 *
746 * Not used.
747 */
748void *
Florin Corase04c2992017-03-01 08:17:34 -0800749client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500750{
Florin Corase04c2992017-03-01 08:17:34 -0800751 session_fifo_event_t _e, *e = &_e;
Florin Corasb384b542018-01-15 01:08:33 -0800752 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -0700753 static u8 *rx_buf = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500754
Florin Corasa849b7b2018-05-18 00:41:55 -0700755 vec_validate (rx_buf, 1 << 20);
756
757 while (!em->time_to_stop && em->state != STATE_READY)
758 ;
759
760 while (!em->time_to_stop)
Dave Barach68b0fb02017-02-28 15:15:56 -0500761 {
Florin Corasb384b542018-01-15 01:08:33 -0800762 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500763 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800764 {
Florin Corasa5464812017-04-19 13:00:05 -0700765 case FIFO_EVENT_APP_RX:
Florin Corasa849b7b2018-05-18 00:41:55 -0700766 client_handle_fifo_event_rx (em, e, rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -0800767 break;
Florin Corase04c2992017-03-01 08:17:34 -0800768 default:
769 clib_warning ("unknown event type %d", e->event_type);
770 break;
771 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500772 }
Florin Corase04c2992017-03-01 08:17:34 -0800773 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500774}
775
Dave Barach68b0fb02017-02-28 15:15:56 -0500776static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400777vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500778{
Florin Corasb384b542018-01-15 01:08:33 -0800779 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500780 session_t *session;
781 u32 session_index;
782 svm_fifo_t *rx_fifo, *tx_fifo;
783 int rv;
784
785 if (mp->retval)
786 {
Florin Corasa5464812017-04-19 13:00:05 -0700787 clib_warning ("connection failed with code: %U", format_api_error,
788 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800789 em->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500790 return;
791 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500792
793 /*
794 * Setup session
795 */
796
Florin Corasb384b542018-01-15 01:08:33 -0800797 pool_get (em->sessions, session);
Florin Corasa849b7b2018-05-18 00:41:55 -0700798 memset (session, 0, sizeof (*session));
Florin Corasb384b542018-01-15 01:08:33 -0800799 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -0500800
Damjan Marion7bee80c2017-04-26 15:32:12 +0200801 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500802 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200803 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500804 tx_fifo->client_session_index = session_index;
805
806 session->server_rx_fifo = rx_fifo;
807 session->server_tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700808 session->vpp_session_handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800809 session->start = clib_time_now (&em->clib_time);
Florin Corasa849b7b2018-05-18 00:41:55 -0700810 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
811 svm_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500812
Florin Corasb384b542018-01-15 01:08:33 -0800813 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800814
Florin Corasa849b7b2018-05-18 00:41:55 -0700815 /*
816 * Start RX thread
817 */
818 em->thread_args[em->n_clients_connected] = session_index;
819 rv = pthread_create (&em->client_thread_handles[em->n_clients_connected],
820 NULL /*attr */ , client_thread_fn,
821 (void *) &em->thread_args[em->n_clients_connected]);
Florin Corase04c2992017-03-01 08:17:34 -0800822 if (rv)
823 {
824 clib_warning ("pthread_create returned %d", rv);
Florin Corasa849b7b2018-05-18 00:41:55 -0700825 return;
Florin Coras6792ec02017-03-13 03:49:51 -0700826 }
827
Florin Corasa849b7b2018-05-18 00:41:55 -0700828 em->n_clients_connected += 1;
829 clib_warning ("session %u (0x%llx) connected with local ip %U port %d",
830 session_index, mp->handle, format_ip46_address, mp->lcl_ip,
831 mp->is_ip4, clib_net_to_host_u16 (mp->lcl_port));
Florin Corase04c2992017-03-01 08:17:34 -0800832}
833
834void
Florin Corasb384b542018-01-15 01:08:33 -0800835client_send_connect (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800836{
837 vl_api_connect_uri_t *cmp;
838 cmp = vl_msg_api_alloc (sizeof (*cmp));
839 memset (cmp, 0, sizeof (*cmp));
840
841 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
Florin Corasb384b542018-01-15 01:08:33 -0800842 cmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -0800843 cmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800844 memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri));
845 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
Florin Corase04c2992017-03-01 08:17:34 -0800846}
847
848void
Florin Corasa849b7b2018-05-18 00:41:55 -0700849client_send_disconnect (echo_main_t * em, session_t * s)
Florin Corase04c2992017-03-01 08:17:34 -0800850{
Florin Corase04c2992017-03-01 08:17:34 -0800851 vl_api_disconnect_session_t *dmp;
Florin Corase04c2992017-03-01 08:17:34 -0800852 dmp = vl_msg_api_alloc (sizeof (*dmp));
853 memset (dmp, 0, sizeof (*dmp));
854 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
Florin Corasb384b542018-01-15 01:08:33 -0800855 dmp->client_index = em->my_client_index;
Florin Corasa849b7b2018-05-18 00:41:55 -0700856 dmp->handle = s->vpp_session_handle;
Florin Corasb384b542018-01-15 01:08:33 -0800857 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
Florin Corase04c2992017-03-01 08:17:34 -0800858}
859
Florin Corasa5464812017-04-19 13:00:05 -0700860int
Florin Corasa849b7b2018-05-18 00:41:55 -0700861client_disconnect (echo_main_t * em, session_t * s)
Florin Corasa5464812017-04-19 13:00:05 -0700862{
Florin Corasa849b7b2018-05-18 00:41:55 -0700863 client_send_disconnect (em, s);
864 pool_put (em->sessions, s);
865 memset (s, 0xfe, sizeof (*s));
Florin Corasa5464812017-04-19 13:00:05 -0700866 return 0;
867}
868
Florin Corase04c2992017-03-01 08:17:34 -0800869static void
Florin Corasa849b7b2018-05-18 00:41:55 -0700870clients_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800871{
Florin Corasa849b7b2018-05-18 00:41:55 -0700872 session_fifo_event_t _e, *e = &_e;
873 f64 start_time, deltat, timeout = 100.0;
874 session_t *s;
Florin Corase04c2992017-03-01 08:17:34 -0800875 int i;
876
Florin Corase04c2992017-03-01 08:17:34 -0800877 /* Init test data */
Florin Coras8e43d042018-05-04 15:46:57 -0700878 vec_validate (em->connect_test_data, 1024 * 1024 - 1);
Florin Corasb384b542018-01-15 01:08:33 -0800879 for (i = 0; i < vec_len (em->connect_test_data); i++)
880 em->connect_test_data[i] = i & 0xff;
Florin Corase04c2992017-03-01 08:17:34 -0800881
Florin Corasa849b7b2018-05-18 00:41:55 -0700882 /*
883 * Attach and connect the clients
884 */
885 if (application_attach (em))
886 return;
Florin Corase04c2992017-03-01 08:17:34 -0800887
Florin Corasa849b7b2018-05-18 00:41:55 -0700888 for (i = 0; i < em->n_clients; i++)
889 client_send_connect (em);
890
891 start_time = clib_time_now (&em->clib_time);
892 while (em->n_clients_connected < em->n_clients
893 && (clib_time_now (&em->clib_time) - start_time < timeout)
894 && em->state != STATE_FAILED)
895 ;
896
897 if (em->n_clients_connected != em->n_clients)
898 {
899 clib_warning ("failed to initialize all connections");
900 return;
901 }
902
903 /*
904 * Initialize connections
905 */
906 for (i = 0; i < em->n_clients; i++)
907 {
908 s = pool_elt_at_index (em->sessions, i);
909 s->bytes_to_send = em->bytes_to_send;
910 if (!em->no_return)
911 s->bytes_to_receive = em->bytes_to_send;
912 }
913 em->n_active_clients = em->n_clients_connected;
914
915 /*
916 * Wait for client threads to send the data
917 */
918 start_time = clib_time_now (&em->clib_time);
919 em->state = STATE_READY;
920 while (em->n_active_clients)
921 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_NOWAIT, 0);
922
923
924 for (i = 0; i < em->n_clients; i++)
925 {
926 s = pool_elt_at_index (em->sessions, i);
927 client_disconnect (em, s);
928 }
929
930 /*
931 * Stats and detach
932 */
933 deltat = clib_time_now (&em->clib_time) - start_time;
934 fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds\n",
935 em->tx_total, em->tx_total / (1ULL << 20),
936 em->tx_total / (1ULL << 30), deltat);
937 fformat (stdout, "%.4f Gbit/second\n", (em->tx_total * 8.0) / deltat / 1e9);
938
Florin Corasb384b542018-01-15 01:08:33 -0800939 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -0800940}
941
942static void
943vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
944{
Florin Corasb384b542018-01-15 01:08:33 -0800945 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800946
947 if (mp->retval)
948 {
flyingeagle23a07779f2017-04-26 20:22:04 +0800949 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -0700950 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800951 em->state = STATE_FAILED;
Florin Corase04c2992017-03-01 08:17:34 -0800952 return;
953 }
954
Florin Corasb384b542018-01-15 01:08:33 -0800955 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500956}
957
Dave Barach68b0fb02017-02-28 15:15:56 -0500958static void
Florin Corase04c2992017-03-01 08:17:34 -0800959vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500960{
Florin Corasb384b542018-01-15 01:08:33 -0800961 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500962
963 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -0800964 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500965
Florin Corasb384b542018-01-15 01:08:33 -0800966 em->state = STATE_START;
Dave Barach68b0fb02017-02-28 15:15:56 -0500967}
968
Florin Coras6cf30ad2017-04-04 23:08:23 -0700969u8 *
970format_ip4_address (u8 * s, va_list * args)
971{
972 u8 *a = va_arg (*args, u8 *);
973 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
974}
975
976u8 *
977format_ip6_address (u8 * s, va_list * args)
978{
979 ip6_address_t *a = va_arg (*args, ip6_address_t *);
980 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
981
982 i_max_n_zero = ARRAY_LEN (a->as_u16);
983 max_n_zeros = 0;
984 i_first_zero = i_max_n_zero;
985 n_zeros = 0;
986 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
987 {
988 u32 is_zero = a->as_u16[i] == 0;
989 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
990 {
991 i_first_zero = i;
992 n_zeros = 0;
993 }
994 n_zeros += is_zero;
995 if ((!is_zero && n_zeros > max_n_zeros)
996 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
997 {
998 i_max_n_zero = i_first_zero;
999 max_n_zeros = n_zeros;
1000 i_first_zero = ARRAY_LEN (a->as_u16);
1001 n_zeros = 0;
1002 }
1003 }
1004
1005 last_double_colon = 0;
1006 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1007 {
1008 if (i == i_max_n_zero && max_n_zeros > 1)
1009 {
1010 s = format (s, "::");
1011 i += max_n_zeros - 1;
1012 last_double_colon = 1;
1013 }
1014 else
1015 {
1016 s = format (s, "%s%x",
1017 (last_double_colon || i == 0) ? "" : ":",
1018 clib_net_to_host_u16 (a->as_u16[i]));
1019 last_double_colon = 0;
1020 }
1021 }
1022
1023 return s;
1024}
1025
1026/* Format an IP46 address. */
1027u8 *
1028format_ip46_address (u8 * s, va_list * args)
1029{
1030 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1031 ip46_type_t type = va_arg (*args, ip46_type_t);
1032 int is_ip4 = 1;
1033
1034 switch (type)
1035 {
1036 case IP46_TYPE_ANY:
1037 is_ip4 = ip46_address_is_ip4 (ip46);
1038 break;
1039 case IP46_TYPE_IP4:
1040 is_ip4 = 1;
1041 break;
1042 case IP46_TYPE_IP6:
1043 is_ip4 = 0;
1044 break;
1045 }
1046
1047 return is_ip4 ?
1048 format (s, "%U", format_ip4_address, &ip46->ip4) :
1049 format (s, "%U", format_ip6_address, &ip46->ip6);
1050}
1051
Dave Barach68b0fb02017-02-28 15:15:56 -05001052static void
1053vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1054{
Florin Corasb384b542018-01-15 01:08:33 -08001055 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001056 vl_api_accept_session_reply_t *rmp;
Florin Corase04c2992017-03-01 08:17:34 -08001057 svm_fifo_t *rx_fifo, *tx_fifo;
1058 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -05001059 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -05001060 u32 session_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001061 u8 *ip_str;
Dave Barach68b0fb02017-02-28 15:15:56 -05001062
1063 if (start_time == 0.0)
Florin Corasb384b542018-01-15 01:08:33 -08001064 start_time = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001065
Florin Coras6cf30ad2017-04-04 23:08:23 -07001066 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1067 clib_warning ("Accepted session from: %s:%d", ip_str,
1068 clib_net_to_host_u16 (mp->port));
Florin Corasb384b542018-01-15 01:08:33 -08001069 em->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -08001070 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001071
1072 /* Allocate local session and set it up */
Florin Corasb384b542018-01-15 01:08:33 -08001073 pool_get (em->sessions, session);
1074 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -05001075
Damjan Marion7bee80c2017-04-26 15:32:12 +02001076 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001077 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +02001078 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001079 tx_fifo->client_session_index = session_index;
1080
1081 session->server_rx_fifo = rx_fifo;
1082 session->server_tx_fifo = tx_fifo;
1083
1084 /* Add it to lookup table */
Florin Corasb384b542018-01-15 01:08:33 -08001085 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001086
Florin Corasb384b542018-01-15 01:08:33 -08001087 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001088
1089 /* Stats printing */
Florin Corasb384b542018-01-15 01:08:33 -08001090 if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001091 {
Florin Corasb384b542018-01-15 01:08:33 -08001092 f64 now = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001093 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
Florin Corasb384b542018-01-15 01:08:33 -08001094 pool_elts (em->sessions), now - start_time,
1095 (f64) pool_elts (em->sessions) / (now - start_time));
Dave Barach68b0fb02017-02-28 15:15:56 -05001096 }
1097
Florin Corase04c2992017-03-01 08:17:34 -08001098 /*
1099 * Send accept reply to vpp
1100 */
Dave Barach68b0fb02017-02-28 15:15:56 -05001101 rmp = vl_msg_api_alloc (sizeof (*rmp));
1102 memset (rmp, 0, sizeof (*rmp));
1103 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001104 rmp->handle = mp->handle;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001105 rmp->context = mp->context;
Florin Corasb384b542018-01-15 01:08:33 -08001106 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -07001107
1108 session->bytes_received = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001109 session->start = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001110}
1111
1112void
Florin Corasb384b542018-01-15 01:08:33 -08001113server_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -05001114{
Florin Corase04c2992017-03-01 08:17:34 -08001115 svm_fifo_t *rx_fifo, *tx_fifo;
1116 int n_read;
Florin Corase04c2992017-03-01 08:17:34 -08001117 session_fifo_event_t evt;
Florin Corase86a8ed2018-01-05 03:20:25 -08001118 svm_queue_t *q;
Florin Corasf03a59a2017-06-09 21:07:32 -07001119 session_t *session;
1120 int rv;
1121 u32 max_dequeue, offset, max_transfer, rx_buf_len;
Florin Corase04c2992017-03-01 08:17:34 -08001122
Florin Corasb384b542018-01-15 01:08:33 -08001123 rx_buf_len = vec_len (em->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -08001124 rx_fifo = e->fifo;
Florin Corasa849b7b2018-05-18 00:41:55 -07001125 session = pool_elt_at_index (em->sessions, rx_fifo->client_session_index);
Florin Corasf03a59a2017-06-09 21:07:32 -07001126 tx_fifo = session->server_tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -08001127
Florin Corasf03a59a2017-06-09 21:07:32 -07001128 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -07001129 /* Allow enqueuing of a new event */
1130 svm_fifo_unset_event (rx_fifo);
1131
Florin Corasa849b7b2018-05-18 00:41:55 -07001132 if (PREDICT_FALSE (!max_dequeue))
1133 return;
Florin Coras6792ec02017-03-13 03:49:51 -07001134
Florin Corasf03a59a2017-06-09 21:07:32 -07001135 /* Read the max_dequeue */
Florin Corase04c2992017-03-01 08:17:34 -08001136 do
1137 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001138 max_transfer = clib_min (rx_buf_len, max_dequeue);
Florin Corasb384b542018-01-15 01:08:33 -08001139 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, em->rx_buf);
Florin Coras6792ec02017-03-13 03:49:51 -07001140 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -08001141 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001142 max_dequeue -= n_read;
1143 session->bytes_received += n_read;
Florin Corasa849b7b2018-05-18 00:41:55 -07001144 session->bytes_to_receive -= n_read;
Florin Corasf03a59a2017-06-09 21:07:32 -07001145 }
1146
1147 /* Reflect if a non-drop session */
Florin Coras8e43d042018-05-04 15:46:57 -07001148 if (!em->no_return && n_read > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001149 {
1150 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001151 do
1152 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001153 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
Florin Corasb384b542018-01-15 01:08:33 -08001154 &em->rx_buf[offset]);
Florin Corasf03a59a2017-06-09 21:07:32 -07001155 if (rv > 0)
1156 {
1157 n_read -= rv;
1158 offset += rv;
1159 }
Florin Corase04c2992017-03-01 08:17:34 -08001160 }
Florin Corasb384b542018-01-15 01:08:33 -08001161 while ((rv <= 0 || n_read > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001162
Florin Coras6792ec02017-03-13 03:49:51 -07001163 /* If event wasn't set, add one */
1164 if (svm_fifo_set_event (tx_fifo))
1165 {
1166 /* Fabricate TX event, send to vpp */
1167 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -07001168 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -07001169
Florin Corasb384b542018-01-15 01:08:33 -08001170 q = em->vpp_event_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -08001171 svm_queue_add (q, (u8 *) & evt, 1 /* do wait for mutex */ );
Florin Coras6792ec02017-03-13 03:49:51 -07001172 }
Florin Corase04c2992017-03-01 08:17:34 -08001173 }
Florin Corase04c2992017-03-01 08:17:34 -08001174 }
Florin Corasb384b542018-01-15 01:08:33 -08001175 while ((n_read < 0 || max_dequeue > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001176}
1177
1178void
Florin Corasb384b542018-01-15 01:08:33 -08001179server_handle_event_queue (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001180{
Florin Coras3cbc04b2017-10-02 00:18:51 -07001181 session_fifo_event_t _e, *e = &_e;
Florin Corase04c2992017-03-01 08:17:34 -08001182
1183 while (1)
1184 {
Florin Corasb384b542018-01-15 01:08:33 -08001185 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Florin Corase04c2992017-03-01 08:17:34 -08001186 switch (e->event_type)
1187 {
Florin Corasa5464812017-04-19 13:00:05 -07001188 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -08001189 server_handle_fifo_event_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -08001190 break;
1191
Florin Corasa5464812017-04-19 13:00:05 -07001192 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -08001193 return;
1194
1195 default:
1196 clib_warning ("unknown event type %d", e->event_type);
1197 break;
1198 }
Florin Corasb384b542018-01-15 01:08:33 -08001199 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001200 break;
Florin Corasb384b542018-01-15 01:08:33 -08001201 if (PREDICT_FALSE (em->time_to_print_stats == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001202 {
Florin Corasb384b542018-01-15 01:08:33 -08001203 em->time_to_print_stats = 0;
1204 fformat (stdout, "%d connections\n", pool_elts (em->sessions));
Florin Corase04c2992017-03-01 08:17:34 -08001205 }
1206 }
1207}
1208
1209void
Florin Corasb384b542018-01-15 01:08:33 -08001210server_send_listen (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001211{
1212 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001213 bmp = vl_msg_api_alloc (sizeof (*bmp));
1214 memset (bmp, 0, sizeof (*bmp));
1215
1216 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001217 bmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -08001218 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -08001219 memcpy (bmp->uri, em->uri, vec_len (em->uri));
1220 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase04c2992017-03-01 08:17:34 -08001221}
1222
Florin Corasa5464812017-04-19 13:00:05 -07001223int
Florin Corasb384b542018-01-15 01:08:33 -08001224server_listen (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001225{
Florin Corasb384b542018-01-15 01:08:33 -08001226 server_send_listen (em);
1227 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -07001228 {
1229 clib_warning ("timeout waiting for STATE_READY");
1230 return -1;
1231 }
1232 return 0;
1233}
1234
Florin Corase04c2992017-03-01 08:17:34 -08001235void
Florin Corasb384b542018-01-15 01:08:33 -08001236server_send_unbind (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001237{
1238 vl_api_unbind_uri_t *ump;
1239
1240 ump = vl_msg_api_alloc (sizeof (*ump));
1241 memset (ump, 0, sizeof (*ump));
1242
1243 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001244 ump->client_index = em->my_client_index;
1245 memcpy (ump->uri, em->uri, vec_len (em->uri));
1246 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
Florin Corase04c2992017-03-01 08:17:34 -08001247}
1248
Florin Corasa5464812017-04-19 13:00:05 -07001249int
Florin Corasb384b542018-01-15 01:08:33 -08001250server_unbind (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001251{
Florin Corasb384b542018-01-15 01:08:33 -08001252 server_send_unbind (em);
1253 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -07001254 {
1255 clib_warning ("timeout waiting for STATE_START");
1256 return -1;
1257 }
1258 return 0;
1259}
1260
Florin Corase04c2992017-03-01 08:17:34 -08001261void
Florin Corasb384b542018-01-15 01:08:33 -08001262server_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001263{
Florin Coras90a63982017-12-19 04:50:01 -08001264 session_t *session;
1265 int i;
1266
1267 /* $$$$ hack preallocation */
1268 for (i = 0; i < 200000; i++)
1269 {
Florin Corasb384b542018-01-15 01:08:33 -08001270 pool_get (em->sessions, session);
Florin Coras90a63982017-12-19 04:50:01 -08001271 memset (session, 0, sizeof (*session));
1272 }
1273 for (i = 0; i < 200000; i++)
Florin Corasb384b542018-01-15 01:08:33 -08001274 pool_put_index (em->sessions, i);
Florin Coras90a63982017-12-19 04:50:01 -08001275
Florin Corasb384b542018-01-15 01:08:33 -08001276 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -07001277 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001278
Dave Barach68b0fb02017-02-28 15:15:56 -05001279 /* Bind to uri */
Florin Corasb384b542018-01-15 01:08:33 -08001280 if (server_listen (em))
Florin Corasa5464812017-04-19 13:00:05 -07001281 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001282
1283 /* Enter handle event loop */
Florin Corasb384b542018-01-15 01:08:33 -08001284 server_handle_event_queue (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001285
1286 /* Cleanup */
Florin Corasb384b542018-01-15 01:08:33 -08001287 server_send_unbind (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001288
Florin Corasb384b542018-01-15 01:08:33 -08001289 application_detach (em);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001290
Dave Barach68b0fb02017-02-28 15:15:56 -05001291 fformat (stdout, "Test complete...\n");
1292}
1293
Florin Corase69f4952017-03-07 10:06:24 -08001294static void
1295vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1296 mp)
1297{
Florin Corasb384b542018-01-15 01:08:33 -08001298 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -07001299 uword *p;
Florin Coras6792ec02017-03-13 03:49:51 -07001300
Florin Corasf03a59a2017-06-09 21:07:32 -07001301 if (mp->retval)
1302 {
1303 clib_warning ("vpp complained about disconnect: %d",
1304 ntohl (mp->retval));
Florin Corasa849b7b2018-05-18 00:41:55 -07001305 return;
Florin Corasf03a59a2017-06-09 21:07:32 -07001306 }
1307
Florin Corasb384b542018-01-15 01:08:33 -08001308 em->state = STATE_START;
Florin Corasa849b7b2018-05-18 00:41:55 -07001309
1310 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
1311 if (p)
1312 {
1313 hash_unset (em->session_index_by_vpp_handles, mp->handle);
1314 }
1315 else
1316 {
1317 clib_warning ("couldn't find session key %llx", mp->handle);
1318 }
Florin Corase69f4952017-03-07 10:06:24 -08001319}
1320
Florin Corase3e2f072018-03-04 07:24:30 -08001321static void
1322 vl_api_application_tls_cert_add_reply_t_handler
1323 (vl_api_application_tls_cert_add_reply_t * mp)
1324{
1325 if (mp->retval)
1326 clib_warning ("failed to add tls cert");
1327}
1328
1329static void
1330 vl_api_application_tls_key_add_reply_t_handler
1331 (vl_api_application_tls_key_add_reply_t * mp)
1332{
1333 if (mp->retval)
1334 clib_warning ("failed to add tls key");
1335}
1336
1337#define foreach_tcp_echo_msg \
1338_(BIND_URI_REPLY, bind_uri_reply) \
1339_(UNBIND_URI_REPLY, unbind_uri_reply) \
1340_(ACCEPT_SESSION, accept_session) \
1341_(CONNECT_SESSION_REPLY, connect_session_reply) \
1342_(DISCONNECT_SESSION, disconnect_session) \
1343_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1344_(RESET_SESSION, reset_session) \
1345_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1346_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1347_(MAP_ANOTHER_SEGMENT, map_another_segment) \
1348_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
1349_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001350
1351void
Florin Corasb384b542018-01-15 01:08:33 -08001352tcp_echo_api_hookup (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -05001353{
1354#define _(N,n) \
1355 vl_msg_api_set_handlers(VL_API_##N, #n, \
1356 vl_api_##n##_t_handler, \
1357 vl_noop_handler, \
1358 vl_api_##n##_t_endian, \
1359 vl_api_##n##_t_print, \
1360 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -08001361 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -05001362#undef _
1363}
1364
1365int
1366main (int argc, char **argv)
1367{
Florin Coras8e43d042018-05-04 15:46:57 -07001368 int i_am_server = 1, test_return_packets = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001369 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001370 unformat_input_t _argv, *a = &_argv;
1371 u8 *chroot_prefix;
Florin Corase69f4952017-03-07 10:06:24 -08001372 u8 *heap, *uri = 0;
1373 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
Florin Coras8e43d042018-05-04 15:46:57 -07001374 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001375 u64 bytes_to_send = 64 << 10, mbytes;
Florin Corasb384b542018-01-15 01:08:33 -08001376 char *app_name;
Dave Barach68b0fb02017-02-28 15:15:56 -05001377 u32 tmp;
1378 mheap_t *h;
Dave Barach68b0fb02017-02-28 15:15:56 -05001379
1380 clib_mem_init (0, 256 << 20);
1381
1382 heap = clib_mem_get_per_cpu_heap ();
1383 h = mheap_header (heap);
1384
1385 /* make the main heap thread-safe */
1386 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1387
Florin Corasb384b542018-01-15 01:08:33 -08001388 vec_validate (em->rx_buf, 128 << 10);
Dave Barach68b0fb02017-02-28 15:15:56 -05001389
Florin Corasa849b7b2018-05-18 00:41:55 -07001390 memset (em, 0, sizeof (*em));
Florin Corasb384b542018-01-15 01:08:33 -08001391 em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Corasb384b542018-01-15 01:08:33 -08001392 em->my_pid = getpid ();
1393 em->configured_segment_size = 1 << 20;
1394 em->socket_name = 0;
1395 em->use_sock_api = 1;
Florin Coras8e43d042018-05-04 15:46:57 -07001396 em->fifo_size = 64 << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001397 em->n_clients = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001398
Florin Corasb384b542018-01-15 01:08:33 -08001399 clib_time_init (&em->clib_time);
1400 init_error_string_table (em);
Florin Corasa332c462018-01-31 06:52:17 -08001401 svm_fifo_segment_main_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001402 unformat_init_command_line (a, argv);
1403
1404 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1405 {
1406 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001407 {
1408 vl_set_memory_root_path ((char *) chroot_prefix);
1409 }
Florin Corase69f4952017-03-07 10:06:24 -08001410 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001411 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001412 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001413 em->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001414 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001415 em->configured_segment_size = tmp << 30;
Florin Coras8e43d042018-05-04 15:46:57 -07001416 else if (unformat (a, "server"))
1417 i_am_server = 1;
1418 else if (unformat (a, "client"))
1419 i_am_server = 0;
1420 else if (unformat (a, "no-return"))
1421 em->no_return = 1;
Florin Corase04c2992017-03-01 08:17:34 -08001422 else if (unformat (a, "test"))
1423 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001424 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001425 {
1426 bytes_to_send = mbytes << 20;
1427 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001428 else if (unformat (a, "gbytes %lld", &mbytes))
1429 {
1430 bytes_to_send = mbytes << 30;
1431 }
Florin Corasb384b542018-01-15 01:08:33 -08001432 else if (unformat (a, "socket-name %s", &em->socket_name))
Florin Coras90a63982017-12-19 04:50:01 -08001433 ;
1434 else if (unformat (a, "use-svm-api"))
Florin Corasb384b542018-01-15 01:08:33 -08001435 em->use_sock_api = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001436 else if (unformat (a, "fifo-size %d", &tmp))
1437 em->fifo_size = tmp << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001438 else if (unformat (a, "nclients %d", &em->n_clients))
1439 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001440 else
Florin Corase04c2992017-03-01 08:17:34 -08001441 {
Jerome Tollet61f79122018-06-04 08:31:33 +01001442 fformat (stderr, "%s: usage [master|slave]\n", argv[0]);
Florin Corase04c2992017-03-01 08:17:34 -08001443 exit (1);
1444 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001445 }
1446
Florin Corasb384b542018-01-15 01:08:33 -08001447 if (!em->socket_name)
1448 em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
Florin Coras90a63982017-12-19 04:50:01 -08001449
Florin Corase69f4952017-03-07 10:06:24 -08001450 if (uri)
1451 {
Florin Corasb384b542018-01-15 01:08:33 -08001452 em->uri = format (0, "%s%c", uri, 0);
1453 em->connect_uri = format (0, "%s%c", uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001454 }
1455 else
1456 {
Florin Corasb384b542018-01-15 01:08:33 -08001457 em->uri = format (0, "%s%c", bind_uri, 0);
1458 em->connect_uri = format (0, "%s%c", connect_uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001459 }
1460
Florin Coras8e43d042018-05-04 15:46:57 -07001461 em->i_am_master = i_am_server;
Florin Corasb384b542018-01-15 01:08:33 -08001462 em->segment_main = &svm_fifo_segment_main;
Florin Corasb384b542018-01-15 01:08:33 -08001463 em->test_return_packets = test_return_packets;
1464 em->bytes_to_send = bytes_to_send;
1465 em->time_to_stop = 0;
Florin Corasa849b7b2018-05-18 00:41:55 -07001466 vec_validate (em->client_thread_handles, em->n_clients - 1);
1467 vec_validate (em->thread_args, em->n_clients - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001468
Florin Corase04c2992017-03-01 08:17:34 -08001469 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001470 tcp_echo_api_hookup (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001471
Florin Coras8e43d042018-05-04 15:46:57 -07001472 app_name = i_am_server ? "tcp_echo_server" : "tcp_echo_client";
Florin Corasb384b542018-01-15 01:08:33 -08001473 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001474 {
1475 svm_region_exit ();
1476 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1477 exit (1);
1478 }
1479
Florin Coras8e43d042018-05-04 15:46:57 -07001480 if (i_am_server == 0)
Florin Corasa849b7b2018-05-18 00:41:55 -07001481 clients_run (em);
Florin Coras90a63982017-12-19 04:50:01 -08001482 else
Florin Corasb384b542018-01-15 01:08:33 -08001483 server_run (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001484
Florin Corasa849b7b2018-05-18 00:41:55 -07001485 /* Make sure detach finishes */
1486 wait_for_state_change (em, STATE_DETACHED);
1487
Florin Corasb384b542018-01-15 01:08:33 -08001488 disconnect_from_vpp (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001489 exit (0);
1490}
Florin Corase04c2992017-03-01 08:17:34 -08001491
1492/*
1493 * fd.io coding-style-patch-verification: ON
1494 *
1495 * Local Variables:
1496 * eval: (c-set-style "gnu")
1497 * End:
1498 */