blob: 59314f943f0737de8594cb78a5021f7191a499a3 [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)
Florin Coras8b20bf52018-06-14 14:55:50 -0700921 if (em->our_event_queue->cursize)
922 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_NOWAIT, 0);
Florin Corasa849b7b2018-05-18 00:41:55 -0700923
924
925 for (i = 0; i < em->n_clients; i++)
926 {
927 s = pool_elt_at_index (em->sessions, i);
928 client_disconnect (em, s);
929 }
930
931 /*
932 * Stats and detach
933 */
934 deltat = clib_time_now (&em->clib_time) - start_time;
935 fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds\n",
936 em->tx_total, em->tx_total / (1ULL << 20),
937 em->tx_total / (1ULL << 30), deltat);
938 fformat (stdout, "%.4f Gbit/second\n", (em->tx_total * 8.0) / deltat / 1e9);
939
Florin Corasb384b542018-01-15 01:08:33 -0800940 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -0800941}
942
943static void
944vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
945{
Florin Corasb384b542018-01-15 01:08:33 -0800946 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800947
948 if (mp->retval)
949 {
flyingeagle23a07779f2017-04-26 20:22:04 +0800950 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -0700951 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800952 em->state = STATE_FAILED;
Florin Corase04c2992017-03-01 08:17:34 -0800953 return;
954 }
955
Florin Corasb384b542018-01-15 01:08:33 -0800956 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500957}
958
Dave Barach68b0fb02017-02-28 15:15:56 -0500959static void
Florin Corase04c2992017-03-01 08:17:34 -0800960vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500961{
Florin Corasb384b542018-01-15 01:08:33 -0800962 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500963
964 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -0800965 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500966
Florin Corasb384b542018-01-15 01:08:33 -0800967 em->state = STATE_START;
Dave Barach68b0fb02017-02-28 15:15:56 -0500968}
969
Florin Coras6cf30ad2017-04-04 23:08:23 -0700970u8 *
971format_ip4_address (u8 * s, va_list * args)
972{
973 u8 *a = va_arg (*args, u8 *);
974 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
975}
976
977u8 *
978format_ip6_address (u8 * s, va_list * args)
979{
980 ip6_address_t *a = va_arg (*args, ip6_address_t *);
981 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
982
983 i_max_n_zero = ARRAY_LEN (a->as_u16);
984 max_n_zeros = 0;
985 i_first_zero = i_max_n_zero;
986 n_zeros = 0;
987 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
988 {
989 u32 is_zero = a->as_u16[i] == 0;
990 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
991 {
992 i_first_zero = i;
993 n_zeros = 0;
994 }
995 n_zeros += is_zero;
996 if ((!is_zero && n_zeros > max_n_zeros)
997 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
998 {
999 i_max_n_zero = i_first_zero;
1000 max_n_zeros = n_zeros;
1001 i_first_zero = ARRAY_LEN (a->as_u16);
1002 n_zeros = 0;
1003 }
1004 }
1005
1006 last_double_colon = 0;
1007 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1008 {
1009 if (i == i_max_n_zero && max_n_zeros > 1)
1010 {
1011 s = format (s, "::");
1012 i += max_n_zeros - 1;
1013 last_double_colon = 1;
1014 }
1015 else
1016 {
1017 s = format (s, "%s%x",
1018 (last_double_colon || i == 0) ? "" : ":",
1019 clib_net_to_host_u16 (a->as_u16[i]));
1020 last_double_colon = 0;
1021 }
1022 }
1023
1024 return s;
1025}
1026
1027/* Format an IP46 address. */
1028u8 *
1029format_ip46_address (u8 * s, va_list * args)
1030{
1031 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1032 ip46_type_t type = va_arg (*args, ip46_type_t);
1033 int is_ip4 = 1;
1034
1035 switch (type)
1036 {
1037 case IP46_TYPE_ANY:
1038 is_ip4 = ip46_address_is_ip4 (ip46);
1039 break;
1040 case IP46_TYPE_IP4:
1041 is_ip4 = 1;
1042 break;
1043 case IP46_TYPE_IP6:
1044 is_ip4 = 0;
1045 break;
1046 }
1047
1048 return is_ip4 ?
1049 format (s, "%U", format_ip4_address, &ip46->ip4) :
1050 format (s, "%U", format_ip6_address, &ip46->ip6);
1051}
1052
Dave Barach68b0fb02017-02-28 15:15:56 -05001053static void
1054vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1055{
Florin Corasb384b542018-01-15 01:08:33 -08001056 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001057 vl_api_accept_session_reply_t *rmp;
Florin Corase04c2992017-03-01 08:17:34 -08001058 svm_fifo_t *rx_fifo, *tx_fifo;
1059 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -05001060 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -05001061 u32 session_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001062 u8 *ip_str;
Dave Barach68b0fb02017-02-28 15:15:56 -05001063
1064 if (start_time == 0.0)
Florin Corasb384b542018-01-15 01:08:33 -08001065 start_time = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001066
Florin Coras6cf30ad2017-04-04 23:08:23 -07001067 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1068 clib_warning ("Accepted session from: %s:%d", ip_str,
1069 clib_net_to_host_u16 (mp->port));
Florin Corasb384b542018-01-15 01:08:33 -08001070 em->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -08001071 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001072
1073 /* Allocate local session and set it up */
Florin Corasb384b542018-01-15 01:08:33 -08001074 pool_get (em->sessions, session);
1075 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -05001076
Damjan Marion7bee80c2017-04-26 15:32:12 +02001077 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001078 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +02001079 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001080 tx_fifo->client_session_index = session_index;
1081
1082 session->server_rx_fifo = rx_fifo;
1083 session->server_tx_fifo = tx_fifo;
1084
1085 /* Add it to lookup table */
Florin Corasb384b542018-01-15 01:08:33 -08001086 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001087
Florin Corasb384b542018-01-15 01:08:33 -08001088 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001089
1090 /* Stats printing */
Florin Corasb384b542018-01-15 01:08:33 -08001091 if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001092 {
Florin Corasb384b542018-01-15 01:08:33 -08001093 f64 now = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001094 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
Florin Corasb384b542018-01-15 01:08:33 -08001095 pool_elts (em->sessions), now - start_time,
1096 (f64) pool_elts (em->sessions) / (now - start_time));
Dave Barach68b0fb02017-02-28 15:15:56 -05001097 }
1098
Florin Corase04c2992017-03-01 08:17:34 -08001099 /*
1100 * Send accept reply to vpp
1101 */
Dave Barach68b0fb02017-02-28 15:15:56 -05001102 rmp = vl_msg_api_alloc (sizeof (*rmp));
1103 memset (rmp, 0, sizeof (*rmp));
1104 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001105 rmp->handle = mp->handle;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001106 rmp->context = mp->context;
Florin Corasb384b542018-01-15 01:08:33 -08001107 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -07001108
1109 session->bytes_received = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001110 session->start = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001111}
1112
1113void
Florin Corasb384b542018-01-15 01:08:33 -08001114server_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -05001115{
Florin Corase04c2992017-03-01 08:17:34 -08001116 svm_fifo_t *rx_fifo, *tx_fifo;
1117 int n_read;
Florin Corase04c2992017-03-01 08:17:34 -08001118 session_fifo_event_t evt;
Florin Corase86a8ed2018-01-05 03:20:25 -08001119 svm_queue_t *q;
Florin Corasf03a59a2017-06-09 21:07:32 -07001120 session_t *session;
1121 int rv;
1122 u32 max_dequeue, offset, max_transfer, rx_buf_len;
Florin Corase04c2992017-03-01 08:17:34 -08001123
Florin Corasb384b542018-01-15 01:08:33 -08001124 rx_buf_len = vec_len (em->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -08001125 rx_fifo = e->fifo;
Florin Corasa849b7b2018-05-18 00:41:55 -07001126 session = pool_elt_at_index (em->sessions, rx_fifo->client_session_index);
Florin Corasf03a59a2017-06-09 21:07:32 -07001127 tx_fifo = session->server_tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -08001128
Florin Corasf03a59a2017-06-09 21:07:32 -07001129 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -07001130 /* Allow enqueuing of a new event */
1131 svm_fifo_unset_event (rx_fifo);
1132
Florin Corasa849b7b2018-05-18 00:41:55 -07001133 if (PREDICT_FALSE (!max_dequeue))
1134 return;
Florin Coras6792ec02017-03-13 03:49:51 -07001135
Florin Corasf03a59a2017-06-09 21:07:32 -07001136 /* Read the max_dequeue */
Florin Corase04c2992017-03-01 08:17:34 -08001137 do
1138 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001139 max_transfer = clib_min (rx_buf_len, max_dequeue);
Florin Corasb384b542018-01-15 01:08:33 -08001140 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, em->rx_buf);
Florin Coras6792ec02017-03-13 03:49:51 -07001141 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -08001142 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001143 max_dequeue -= n_read;
1144 session->bytes_received += n_read;
Florin Corasa849b7b2018-05-18 00:41:55 -07001145 session->bytes_to_receive -= n_read;
Florin Corasf03a59a2017-06-09 21:07:32 -07001146 }
1147
1148 /* Reflect if a non-drop session */
Florin Coras8e43d042018-05-04 15:46:57 -07001149 if (!em->no_return && n_read > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001150 {
1151 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001152 do
1153 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001154 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
Florin Corasb384b542018-01-15 01:08:33 -08001155 &em->rx_buf[offset]);
Florin Corasf03a59a2017-06-09 21:07:32 -07001156 if (rv > 0)
1157 {
1158 n_read -= rv;
1159 offset += rv;
1160 }
Florin Corase04c2992017-03-01 08:17:34 -08001161 }
Florin Corasb384b542018-01-15 01:08:33 -08001162 while ((rv <= 0 || n_read > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001163
Florin Coras6792ec02017-03-13 03:49:51 -07001164 /* If event wasn't set, add one */
1165 if (svm_fifo_set_event (tx_fifo))
1166 {
1167 /* Fabricate TX event, send to vpp */
1168 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -07001169 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -07001170
Florin Corasb384b542018-01-15 01:08:33 -08001171 q = em->vpp_event_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -08001172 svm_queue_add (q, (u8 *) & evt, 1 /* do wait for mutex */ );
Florin Coras6792ec02017-03-13 03:49:51 -07001173 }
Florin Corase04c2992017-03-01 08:17:34 -08001174 }
Florin Corase04c2992017-03-01 08:17:34 -08001175 }
Florin Corasb384b542018-01-15 01:08:33 -08001176 while ((n_read < 0 || max_dequeue > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001177}
1178
1179void
Florin Corasb384b542018-01-15 01:08:33 -08001180server_handle_event_queue (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001181{
Florin Coras3cbc04b2017-10-02 00:18:51 -07001182 session_fifo_event_t _e, *e = &_e;
Florin Corase04c2992017-03-01 08:17:34 -08001183
1184 while (1)
1185 {
Florin Corasb384b542018-01-15 01:08:33 -08001186 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Florin Corase04c2992017-03-01 08:17:34 -08001187 switch (e->event_type)
1188 {
Florin Corasa5464812017-04-19 13:00:05 -07001189 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -08001190 server_handle_fifo_event_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -08001191 break;
1192
Florin Corasa5464812017-04-19 13:00:05 -07001193 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -08001194 return;
1195
1196 default:
1197 clib_warning ("unknown event type %d", e->event_type);
1198 break;
1199 }
Florin Corasb384b542018-01-15 01:08:33 -08001200 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001201 break;
Florin Corasb384b542018-01-15 01:08:33 -08001202 if (PREDICT_FALSE (em->time_to_print_stats == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001203 {
Florin Corasb384b542018-01-15 01:08:33 -08001204 em->time_to_print_stats = 0;
1205 fformat (stdout, "%d connections\n", pool_elts (em->sessions));
Florin Corase04c2992017-03-01 08:17:34 -08001206 }
1207 }
1208}
1209
1210void
Florin Corasb384b542018-01-15 01:08:33 -08001211server_send_listen (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001212{
1213 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001214 bmp = vl_msg_api_alloc (sizeof (*bmp));
1215 memset (bmp, 0, sizeof (*bmp));
1216
1217 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001218 bmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -08001219 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -08001220 memcpy (bmp->uri, em->uri, vec_len (em->uri));
1221 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase04c2992017-03-01 08:17:34 -08001222}
1223
Florin Corasa5464812017-04-19 13:00:05 -07001224int
Florin Corasb384b542018-01-15 01:08:33 -08001225server_listen (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001226{
Florin Corasb384b542018-01-15 01:08:33 -08001227 server_send_listen (em);
1228 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -07001229 {
1230 clib_warning ("timeout waiting for STATE_READY");
1231 return -1;
1232 }
1233 return 0;
1234}
1235
Florin Corase04c2992017-03-01 08:17:34 -08001236void
Florin Corasb384b542018-01-15 01:08:33 -08001237server_send_unbind (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001238{
1239 vl_api_unbind_uri_t *ump;
1240
1241 ump = vl_msg_api_alloc (sizeof (*ump));
1242 memset (ump, 0, sizeof (*ump));
1243
1244 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001245 ump->client_index = em->my_client_index;
1246 memcpy (ump->uri, em->uri, vec_len (em->uri));
1247 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
Florin Corase04c2992017-03-01 08:17:34 -08001248}
1249
Florin Corasa5464812017-04-19 13:00:05 -07001250int
Florin Corasb384b542018-01-15 01:08:33 -08001251server_unbind (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001252{
Florin Corasb384b542018-01-15 01:08:33 -08001253 server_send_unbind (em);
1254 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -07001255 {
1256 clib_warning ("timeout waiting for STATE_START");
1257 return -1;
1258 }
1259 return 0;
1260}
1261
Florin Corase04c2992017-03-01 08:17:34 -08001262void
Florin Corasb384b542018-01-15 01:08:33 -08001263server_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001264{
Florin Coras90a63982017-12-19 04:50:01 -08001265 session_t *session;
1266 int i;
1267
1268 /* $$$$ hack preallocation */
1269 for (i = 0; i < 200000; i++)
1270 {
Florin Corasb384b542018-01-15 01:08:33 -08001271 pool_get (em->sessions, session);
Florin Coras90a63982017-12-19 04:50:01 -08001272 memset (session, 0, sizeof (*session));
1273 }
1274 for (i = 0; i < 200000; i++)
Florin Corasb384b542018-01-15 01:08:33 -08001275 pool_put_index (em->sessions, i);
Florin Coras90a63982017-12-19 04:50:01 -08001276
Florin Corasb384b542018-01-15 01:08:33 -08001277 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -07001278 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001279
Dave Barach68b0fb02017-02-28 15:15:56 -05001280 /* Bind to uri */
Florin Corasb384b542018-01-15 01:08:33 -08001281 if (server_listen (em))
Florin Corasa5464812017-04-19 13:00:05 -07001282 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001283
1284 /* Enter handle event loop */
Florin Corasb384b542018-01-15 01:08:33 -08001285 server_handle_event_queue (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001286
1287 /* Cleanup */
Florin Corasb384b542018-01-15 01:08:33 -08001288 server_send_unbind (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001289
Florin Corasb384b542018-01-15 01:08:33 -08001290 application_detach (em);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001291
Dave Barach68b0fb02017-02-28 15:15:56 -05001292 fformat (stdout, "Test complete...\n");
1293}
1294
Florin Corase69f4952017-03-07 10:06:24 -08001295static void
1296vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1297 mp)
1298{
Florin Corasb384b542018-01-15 01:08:33 -08001299 echo_main_t *em = &echo_main;
Florin Corasa849b7b2018-05-18 00:41:55 -07001300 uword *p;
Florin Coras6792ec02017-03-13 03:49:51 -07001301
Florin Corasf03a59a2017-06-09 21:07:32 -07001302 if (mp->retval)
1303 {
1304 clib_warning ("vpp complained about disconnect: %d",
1305 ntohl (mp->retval));
Florin Corasa849b7b2018-05-18 00:41:55 -07001306 return;
Florin Corasf03a59a2017-06-09 21:07:32 -07001307 }
1308
Florin Corasb384b542018-01-15 01:08:33 -08001309 em->state = STATE_START;
Florin Corasa849b7b2018-05-18 00:41:55 -07001310
1311 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
1312 if (p)
1313 {
1314 hash_unset (em->session_index_by_vpp_handles, mp->handle);
1315 }
1316 else
1317 {
1318 clib_warning ("couldn't find session key %llx", mp->handle);
1319 }
Florin Corase69f4952017-03-07 10:06:24 -08001320}
1321
Florin Corase3e2f072018-03-04 07:24:30 -08001322static void
1323 vl_api_application_tls_cert_add_reply_t_handler
1324 (vl_api_application_tls_cert_add_reply_t * mp)
1325{
1326 if (mp->retval)
1327 clib_warning ("failed to add tls cert");
1328}
1329
1330static void
1331 vl_api_application_tls_key_add_reply_t_handler
1332 (vl_api_application_tls_key_add_reply_t * mp)
1333{
1334 if (mp->retval)
1335 clib_warning ("failed to add tls key");
1336}
1337
1338#define foreach_tcp_echo_msg \
1339_(BIND_URI_REPLY, bind_uri_reply) \
1340_(UNBIND_URI_REPLY, unbind_uri_reply) \
1341_(ACCEPT_SESSION, accept_session) \
1342_(CONNECT_SESSION_REPLY, connect_session_reply) \
1343_(DISCONNECT_SESSION, disconnect_session) \
1344_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1345_(RESET_SESSION, reset_session) \
1346_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1347_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1348_(MAP_ANOTHER_SEGMENT, map_another_segment) \
1349_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
1350_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001351
1352void
Florin Corasb384b542018-01-15 01:08:33 -08001353tcp_echo_api_hookup (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -05001354{
1355#define _(N,n) \
1356 vl_msg_api_set_handlers(VL_API_##N, #n, \
1357 vl_api_##n##_t_handler, \
1358 vl_noop_handler, \
1359 vl_api_##n##_t_endian, \
1360 vl_api_##n##_t_print, \
1361 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -08001362 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -05001363#undef _
1364}
1365
1366int
1367main (int argc, char **argv)
1368{
Florin Coras8e43d042018-05-04 15:46:57 -07001369 int i_am_server = 1, test_return_packets = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001370 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001371 unformat_input_t _argv, *a = &_argv;
1372 u8 *chroot_prefix;
Florin Corase69f4952017-03-07 10:06:24 -08001373 u8 *heap, *uri = 0;
1374 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
Florin Coras8e43d042018-05-04 15:46:57 -07001375 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001376 u64 bytes_to_send = 64 << 10, mbytes;
Florin Corasb384b542018-01-15 01:08:33 -08001377 char *app_name;
Dave Barach68b0fb02017-02-28 15:15:56 -05001378 u32 tmp;
1379 mheap_t *h;
Dave Barach68b0fb02017-02-28 15:15:56 -05001380
1381 clib_mem_init (0, 256 << 20);
1382
1383 heap = clib_mem_get_per_cpu_heap ();
1384 h = mheap_header (heap);
1385
1386 /* make the main heap thread-safe */
1387 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1388
Florin Corasb384b542018-01-15 01:08:33 -08001389 vec_validate (em->rx_buf, 128 << 10);
Dave Barach68b0fb02017-02-28 15:15:56 -05001390
Florin Corasa849b7b2018-05-18 00:41:55 -07001391 memset (em, 0, sizeof (*em));
Florin Corasb384b542018-01-15 01:08:33 -08001392 em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Corasb384b542018-01-15 01:08:33 -08001393 em->my_pid = getpid ();
1394 em->configured_segment_size = 1 << 20;
1395 em->socket_name = 0;
1396 em->use_sock_api = 1;
Florin Coras8e43d042018-05-04 15:46:57 -07001397 em->fifo_size = 64 << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001398 em->n_clients = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001399
Florin Corasb384b542018-01-15 01:08:33 -08001400 clib_time_init (&em->clib_time);
1401 init_error_string_table (em);
Florin Corasa332c462018-01-31 06:52:17 -08001402 svm_fifo_segment_main_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001403 unformat_init_command_line (a, argv);
1404
1405 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1406 {
1407 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001408 {
1409 vl_set_memory_root_path ((char *) chroot_prefix);
1410 }
Florin Corase69f4952017-03-07 10:06:24 -08001411 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001412 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001413 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001414 em->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001415 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001416 em->configured_segment_size = tmp << 30;
Florin Coras8e43d042018-05-04 15:46:57 -07001417 else if (unformat (a, "server"))
1418 i_am_server = 1;
1419 else if (unformat (a, "client"))
1420 i_am_server = 0;
1421 else if (unformat (a, "no-return"))
1422 em->no_return = 1;
Florin Corase04c2992017-03-01 08:17:34 -08001423 else if (unformat (a, "test"))
1424 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001425 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001426 {
1427 bytes_to_send = mbytes << 20;
1428 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001429 else if (unformat (a, "gbytes %lld", &mbytes))
1430 {
1431 bytes_to_send = mbytes << 30;
1432 }
Florin Corasb384b542018-01-15 01:08:33 -08001433 else if (unformat (a, "socket-name %s", &em->socket_name))
Florin Coras90a63982017-12-19 04:50:01 -08001434 ;
1435 else if (unformat (a, "use-svm-api"))
Florin Corasb384b542018-01-15 01:08:33 -08001436 em->use_sock_api = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001437 else if (unformat (a, "fifo-size %d", &tmp))
1438 em->fifo_size = tmp << 10;
Florin Corasa849b7b2018-05-18 00:41:55 -07001439 else if (unformat (a, "nclients %d", &em->n_clients))
1440 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001441 else
Florin Corase04c2992017-03-01 08:17:34 -08001442 {
Jerome Tollet61f79122018-06-04 08:31:33 +01001443 fformat (stderr, "%s: usage [master|slave]\n", argv[0]);
Florin Corase04c2992017-03-01 08:17:34 -08001444 exit (1);
1445 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001446 }
1447
Florin Corasb384b542018-01-15 01:08:33 -08001448 if (!em->socket_name)
1449 em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
Florin Coras90a63982017-12-19 04:50:01 -08001450
Florin Corase69f4952017-03-07 10:06:24 -08001451 if (uri)
1452 {
Florin Corasb384b542018-01-15 01:08:33 -08001453 em->uri = format (0, "%s%c", uri, 0);
1454 em->connect_uri = format (0, "%s%c", uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001455 }
1456 else
1457 {
Florin Corasb384b542018-01-15 01:08:33 -08001458 em->uri = format (0, "%s%c", bind_uri, 0);
1459 em->connect_uri = format (0, "%s%c", connect_uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001460 }
1461
Florin Coras8e43d042018-05-04 15:46:57 -07001462 em->i_am_master = i_am_server;
Florin Corasb384b542018-01-15 01:08:33 -08001463 em->segment_main = &svm_fifo_segment_main;
Florin Corasb384b542018-01-15 01:08:33 -08001464 em->test_return_packets = test_return_packets;
1465 em->bytes_to_send = bytes_to_send;
1466 em->time_to_stop = 0;
Florin Corasa849b7b2018-05-18 00:41:55 -07001467 vec_validate (em->client_thread_handles, em->n_clients - 1);
1468 vec_validate (em->thread_args, em->n_clients - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001469
Florin Corase04c2992017-03-01 08:17:34 -08001470 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001471 tcp_echo_api_hookup (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001472
Florin Coras8e43d042018-05-04 15:46:57 -07001473 app_name = i_am_server ? "tcp_echo_server" : "tcp_echo_client";
Florin Corasb384b542018-01-15 01:08:33 -08001474 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001475 {
1476 svm_region_exit ();
1477 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1478 exit (1);
1479 }
1480
Florin Coras8e43d042018-05-04 15:46:57 -07001481 if (i_am_server == 0)
Florin Corasa849b7b2018-05-18 00:41:55 -07001482 clients_run (em);
Florin Coras90a63982017-12-19 04:50:01 -08001483 else
Florin Corasb384b542018-01-15 01:08:33 -08001484 server_run (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001485
Florin Corasa849b7b2018-05-18 00:41:55 -07001486 /* Make sure detach finishes */
1487 wait_for_state_change (em, STATE_DETACHED);
1488
Florin Corasb384b542018-01-15 01:08:33 -08001489 disconnect_from_vpp (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001490 exit (0);
1491}
Florin Corase04c2992017-03-01 08:17:34 -08001492
1493/*
1494 * fd.io coding-style-patch-verification: ON
1495 *
1496 * Local Variables:
1497 * eval: (c-set-style "gnu")
1498 * End:
1499 */