blob: 7cfd0ea025c978feceabce77cf186f33606a207a [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
Dave Barach68b0fb02017-02-28 15:15:56 -050041typedef struct
42{
Florin Corase04c2992017-03-01 08:17:34 -080043 svm_fifo_t *server_rx_fifo;
44 svm_fifo_t *server_tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -050045
Florin Corasa5464812017-04-19 13:00:05 -070046 u64 vpp_session_handle;
Florin Corasf03a59a2017-06-09 21:07:32 -070047 u64 bytes_received;
48 f64 start;
Dave Barach68b0fb02017-02-28 15:15:56 -050049} session_t;
50
51typedef enum
52{
53 STATE_START,
Florin Corasa5464812017-04-19 13:00:05 -070054 STATE_ATTACHED,
Dave Barach68b0fb02017-02-28 15:15:56 -050055 STATE_READY,
56 STATE_DISCONNECTING,
57 STATE_FAILED
58} connection_state_t;
59
60typedef struct
61{
62 /* vpe input queue */
Florin Corase86a8ed2018-01-05 03:20:25 -080063 svm_queue_t *vl_input_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050064
65 /* API client handle */
66 u32 my_client_index;
67
68 /* The URI we're playing with */
Florin Corase04c2992017-03-01 08:17:34 -080069 u8 *uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050070
71 /* Session pool */
Florin Corase04c2992017-03-01 08:17:34 -080072 session_t *sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -050073
74 /* Hash table for disconnect processing */
Florin Corase04c2992017-03-01 08:17:34 -080075 uword *session_index_by_vpp_handles;
Dave Barach68b0fb02017-02-28 15:15:56 -050076
77 /* intermediate rx buffer */
Florin Corase04c2992017-03-01 08:17:34 -080078 u8 *rx_buf;
Dave Barach68b0fb02017-02-28 15:15:56 -050079
80 /* URI for slave's connect */
Florin Corase04c2992017-03-01 08:17:34 -080081 u8 *connect_uri;
Dave Barach68b0fb02017-02-28 15:15:56 -050082
83 u32 connected_session_index;
84
85 int i_am_master;
86
87 /* drop all packets */
88 int drop_packets;
89
90 /* Our event queue */
Florin Corase86a8ed2018-01-05 03:20:25 -080091 svm_queue_t *our_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050092
93 /* $$$ single thread only for the moment */
Florin Corase86a8ed2018-01-05 03:20:25 -080094 svm_queue_t *vpp_event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050095
Florin Coras90a63982017-12-19 04:50:01 -080096 u8 *socket_name;
97
Dave Barach68b0fb02017-02-28 15:15:56 -050098 pid_t my_pid;
99
100 /* For deadman timers */
101 clib_time_t clib_time;
102
103 /* State of the connection, shared between msg RX thread and main thread */
104 volatile connection_state_t state;
105
106 /* Signal variables */
107 volatile int time_to_stop;
108 volatile int time_to_print_stats;
109
110 u32 configured_segment_size;
111
112 /* VNET_API_ERROR_FOO -> "Foo" hash table */
Florin Corase04c2992017-03-01 08:17:34 -0800113 uword *error_string_by_error_number;
Dave Barach68b0fb02017-02-28 15:15:56 -0500114
115 u8 *connect_test_data;
Florin Corase04c2992017-03-01 08:17:34 -0800116 pthread_t client_rx_thread_handle;
117 u32 client_bytes_received;
118 u8 test_return_packets;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700119 u64 bytes_to_send;
Florin Corase04c2992017-03-01 08:17:34 -0800120
Florin Coras90a63982017-12-19 04:50:01 -0800121 /** Flag that decides if socket, instead of svm, api is used to connect to
122 * vpp. If sock api is used, shm binary api is subsequently bootstrapped
123 * and all other messages are exchanged using shm IPC. */
124 u8 use_sock_api;
125
Florin Corase04c2992017-03-01 08:17:34 -0800126 /* convenience */
127 svm_fifo_segment_main_t *segment_main;
Florin Corasb384b542018-01-15 01:08:33 -0800128} echo_main_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500129
Florin Corasb384b542018-01-15 01:08:33 -0800130echo_main_t echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500131
132#if CLIB_DEBUG > 0
133#define NITER 10000
134#else
135#define NITER 4000000
136#endif
137
Florin Corase3e2f072018-03-04 07:24:30 -0800138const char test_srv_crt_rsa[] =
139 "-----BEGIN CERTIFICATE-----\r\n"
140 "MIIDNzCCAh+gAwIBAgIBAjANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n"
141 "MA8GA1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n"
142 "MTEwMjEyMTQ0NDA2WhcNMjEwMjEyMTQ0NDA2WjA0MQswCQYDVQQGEwJOTDERMA8G\r\n"
143 "A1UEChMIUG9sYXJTU0wxEjAQBgNVBAMTCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcN\r\n"
144 "AQEBBQADggEPADCCAQoCggEBAMFNo93nzR3RBNdJcriZrA545Do8Ss86ExbQWuTN\r\n"
145 "owCIp+4ea5anUrSQ7y1yej4kmvy2NKwk9XfgJmSMnLAofaHa6ozmyRyWvP7BBFKz\r\n"
146 "NtSj+uGxdtiQwWG0ZlI2oiZTqqt0Xgd9GYLbKtgfoNkNHC1JZvdbJXNG6AuKT2kM\r\n"
147 "tQCQ4dqCEGZ9rlQri2V5kaHiYcPNQEkI7mgM8YuG0ka/0LiqEQMef1aoGh5EGA8P\r\n"
148 "hYvai0Re4hjGYi/HZo36Xdh98yeJKQHFkA4/J/EwyEoO79bex8cna8cFPXrEAjya\r\n"
149 "HT4P6DSYW8tzS1KW2BGiLICIaTla0w+w3lkvEcf36hIBMJcCAwEAAaNNMEswCQYD\r\n"
150 "VR0TBAIwADAdBgNVHQ4EFgQUpQXoZLjc32APUBJNYKhkr02LQ5MwHwYDVR0jBBgw\r\n"
151 "FoAUtFrkpbPe0lL2udWmlQ/rPrzH/f8wDQYJKoZIhvcNAQEFBQADggEBAJxnXClY\r\n"
152 "oHkbp70cqBrsGXLybA74czbO5RdLEgFs7rHVS9r+c293luS/KdliLScZqAzYVylw\r\n"
153 "UfRWvKMoWhHYKp3dEIS4xTXk6/5zXxhv9Rw8SGc8qn6vITHk1S1mPevtekgasY5Y\r\n"
154 "iWQuM3h4YVlRH3HHEMAD1TnAexfXHHDFQGe+Bd1iAbz1/sH9H8l4StwX6egvTK3M\r\n"
155 "wXRwkKkvjKaEDA9ATbZx0mI8LGsxSuCqe9r9dyjmttd47J1p1Rulz3CLzaRcVIuS\r\n"
156 "RRQfaD8neM9c1S/iJ/amTVqJxA1KOdOS5780WhPfSArA+g4qAmSjelc3p4wWpha8\r\n"
157 "zhuYwjVuX6JHG0c=\r\n" "-----END CERTIFICATE-----\r\n";
158const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
159
160const char test_srv_key_rsa[] =
161 "-----BEGIN RSA PRIVATE KEY-----\r\n"
162 "MIIEpAIBAAKCAQEAwU2j3efNHdEE10lyuJmsDnjkOjxKzzoTFtBa5M2jAIin7h5r\r\n"
163 "lqdStJDvLXJ6PiSa/LY0rCT1d+AmZIycsCh9odrqjObJHJa8/sEEUrM21KP64bF2\r\n"
164 "2JDBYbRmUjaiJlOqq3ReB30Zgtsq2B+g2Q0cLUlm91slc0boC4pPaQy1AJDh2oIQ\r\n"
165 "Zn2uVCuLZXmRoeJhw81ASQjuaAzxi4bSRr/QuKoRAx5/VqgaHkQYDw+Fi9qLRF7i\r\n"
166 "GMZiL8dmjfpd2H3zJ4kpAcWQDj8n8TDISg7v1t7HxydrxwU9esQCPJodPg/oNJhb\r\n"
167 "y3NLUpbYEaIsgIhpOVrTD7DeWS8Rx/fqEgEwlwIDAQABAoIBAQCXR0S8EIHFGORZ\r\n"
168 "++AtOg6eENxD+xVs0f1IeGz57Tjo3QnXX7VBZNdj+p1ECvhCE/G7XnkgU5hLZX+G\r\n"
169 "Z0jkz/tqJOI0vRSdLBbipHnWouyBQ4e/A1yIJdlBtqXxJ1KE/ituHRbNc4j4kL8Z\r\n"
170 "/r6pvwnTI0PSx2Eqs048YdS92LT6qAv4flbNDxMn2uY7s4ycS4Q8w1JXnCeaAnYm\r\n"
171 "WYI5wxO+bvRELR2Mcz5DmVnL8jRyml6l6582bSv5oufReFIbyPZbQWlXgYnpu6He\r\n"
172 "GTc7E1zKYQGG/9+DQUl/1vQuCPqQwny0tQoX2w5tdYpdMdVm+zkLtbajzdTviJJa\r\n"
173 "TWzL6lt5AoGBAN86+SVeJDcmQJcv4Eq6UhtRr4QGMiQMz0Sod6ettYxYzMgxtw28\r\n"
174 "CIrgpozCc+UaZJLo7UxvC6an85r1b2nKPCLQFaggJ0H4Q0J/sZOhBIXaoBzWxveK\r\n"
175 "nupceKdVxGsFi8CDy86DBfiyFivfBj+47BbaQzPBj7C4rK7UlLjab2rDAoGBAN2u\r\n"
176 "AM2gchoFiu4v1HFL8D7lweEpi6ZnMJjnEu/dEgGQJFjwdpLnPbsj4c75odQ4Gz8g\r\n"
177 "sw9lao9VVzbusoRE/JGI4aTdO0pATXyG7eG1Qu+5Yc1YGXcCrliA2xM9xx+d7f+s\r\n"
178 "mPzN+WIEg5GJDYZDjAzHG5BNvi/FfM1C9dOtjv2dAoGAF0t5KmwbjWHBhcVqO4Ic\r\n"
179 "BVvN3BIlc1ue2YRXEDlxY5b0r8N4XceMgKmW18OHApZxfl8uPDauWZLXOgl4uepv\r\n"
180 "whZC3EuWrSyyICNhLY21Ah7hbIEBPF3L3ZsOwC+UErL+dXWLdB56Jgy3gZaBeW7b\r\n"
181 "vDrEnocJbqCm7IukhXHOBK8CgYEAwqdHB0hqyNSzIOGY7v9abzB6pUdA3BZiQvEs\r\n"
182 "3LjHVd4HPJ2x0N8CgrBIWOE0q8+0hSMmeE96WW/7jD3fPWwCR5zlXknxBQsfv0gP\r\n"
183 "3BC5PR0Qdypz+d+9zfMf625kyit4T/hzwhDveZUzHnk1Cf+IG7Q+TOEnLnWAWBED\r\n"
184 "ISOWmrUCgYAFEmRxgwAc/u+D6t0syCwAYh6POtscq9Y0i9GyWk89NzgC4NdwwbBH\r\n"
185 "4AgahOxIxXx2gxJnq3yfkJfIjwf0s2DyP0kY2y6Ua1OeomPeY9mrIS4tCuDQ6LrE\r\n"
186 "TB6l9VGoxJL4fyHnZb8L5gGvnB1bbD8cL6YPaDiOhcRseC9vBiEuVg==\r\n"
187 "-----END RSA PRIVATE KEY-----\r\n";
188const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
189
Florin Corasa5464812017-04-19 13:00:05 -0700190static u8 *
191format_api_error (u8 * s, va_list * args)
192{
Florin Corasb384b542018-01-15 01:08:33 -0800193 echo_main_t *em = &echo_main;
Florin Corasa5464812017-04-19 13:00:05 -0700194 i32 error = va_arg (*args, u32);
195 uword *p;
196
Florin Corasb384b542018-01-15 01:08:33 -0800197 p = hash_get (em->error_string_by_error_number, -error);
Florin Corasa5464812017-04-19 13:00:05 -0700198
199 if (p)
200 s = format (s, "%s", p[0]);
201 else
202 s = format (s, "%d", error);
203 return s;
204}
205
206static void
Florin Corasb384b542018-01-15 01:08:33 -0800207init_error_string_table (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700208{
Florin Corasb384b542018-01-15 01:08:33 -0800209 em->error_string_by_error_number = hash_create (0, sizeof (uword));
Florin Corasa5464812017-04-19 13:00:05 -0700210
Florin Corasb384b542018-01-15 01:08:33 -0800211#define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
Florin Corasa5464812017-04-19 13:00:05 -0700212 foreach_vnet_api_error;
213#undef _
214
Florin Corasb384b542018-01-15 01:08:33 -0800215 hash_set (em->error_string_by_error_number, 99, "Misc");
Florin Corasa5464812017-04-19 13:00:05 -0700216}
217
Dave Barach68b0fb02017-02-28 15:15:56 -0500218int
Florin Corasb384b542018-01-15 01:08:33 -0800219wait_for_state_change (echo_main_t * em, connection_state_t state)
Dave Barach68b0fb02017-02-28 15:15:56 -0500220{
221#if CLIB_DEBUG > 0
222#define TIMEOUT 600.0
223#else
224#define TIMEOUT 600.0
225#endif
226
Florin Corasb384b542018-01-15 01:08:33 -0800227 f64 timeout = clib_time_now (&em->clib_time) + TIMEOUT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500228
Florin Corasb384b542018-01-15 01:08:33 -0800229 while (clib_time_now (&em->clib_time) < timeout)
Dave Barach68b0fb02017-02-28 15:15:56 -0500230 {
Florin Corasb384b542018-01-15 01:08:33 -0800231 if (em->state == state)
Florin Corase04c2992017-03-01 08:17:34 -0800232 return 0;
Florin Corasb384b542018-01-15 01:08:33 -0800233 if (em->state == STATE_FAILED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500234 return -1;
Florin Corasb384b542018-01-15 01:08:33 -0800235 if (em->time_to_stop == 1)
Florin Corasf03a59a2017-06-09 21:07:32 -0700236 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500237 }
238 clib_warning ("timeout waiting for STATE_READY");
239 return -1;
240}
241
Florin Coras6cf30ad2017-04-04 23:08:23 -0700242void
Florin Corasb384b542018-01-15 01:08:33 -0800243application_send_attach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700244{
245 vl_api_application_attach_t *bmp;
Florin Corase3e2f072018-03-04 07:24:30 -0800246 vl_api_application_tls_cert_add_t *cert_mp;
247 vl_api_application_tls_key_add_t *key_mp;
248
Florin Corasf03a59a2017-06-09 21:07:32 -0700249 u32 fifo_size = 4 << 20;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700250 bmp = vl_msg_api_alloc (sizeof (*bmp));
251 memset (bmp, 0, sizeof (*bmp));
252
253 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Corasb384b542018-01-15 01:08:33 -0800254 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700255 bmp->context = ntohl (0xfeedface);
Florin Corasa5464812017-04-19 13:00:05 -0700256 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700257 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
Dave Barach10d8cc62017-05-30 09:30:07 -0400258 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Florin Corasff6e7692017-12-11 04:59:01 -0800259 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
260 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
261 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
262 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
Florin Corasb384b542018-01-15 01:08:33 -0800263 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase3e2f072018-03-04 07:24:30 -0800264
265 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
266 memset (cert_mp, 0, sizeof (*cert_mp));
267 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
268 cert_mp->client_index = em->my_client_index;
269 cert_mp->context = ntohl (0xfeedface);
270 cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
271 clib_memcpy (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
272 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
273
274 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
275 memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
276 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
277 key_mp->client_index = em->my_client_index;
278 key_mp->context = ntohl (0xfeedface);
279 key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
280 clib_memcpy (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
281 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700282}
283
Florin Corasa5464812017-04-19 13:00:05 -0700284int
Florin Corasb384b542018-01-15 01:08:33 -0800285application_attach (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700286{
Florin Corasb384b542018-01-15 01:08:33 -0800287 application_send_attach (em);
288 if (wait_for_state_change (em, STATE_ATTACHED))
Florin Corasa5464812017-04-19 13:00:05 -0700289 {
290 clib_warning ("timeout waiting for STATE_ATTACHED");
291 return -1;
292 }
293 return 0;
294}
295
Florin Coras6cf30ad2017-04-04 23:08:23 -0700296void
Florin Corasb384b542018-01-15 01:08:33 -0800297application_detach (echo_main_t * em)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700298{
299 vl_api_application_detach_t *bmp;
300 bmp = vl_msg_api_alloc (sizeof (*bmp));
301 memset (bmp, 0, sizeof (*bmp));
302
303 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Corasb384b542018-01-15 01:08:33 -0800304 bmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800306 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
307
308 clib_warning ("Sent detach");
309}
310
311static int
312memfd_segment_attach (void)
313{
314 ssvm_private_t _ssvm = { 0 }, *ssvm = &_ssvm;
315 clib_error_t *error;
316 int rv;
317
318 if ((error = vl_socket_client_recv_fd_msg (&ssvm->fd, 5)))
319 {
320 clib_error_report (error);
321 return -1;
322 }
323
324 if ((rv = ssvm_slave_init_memfd (ssvm)))
325 return rv;
326
327 return 0;
328}
329
330static int
331fifo_segment_attach (char *name, u32 size, ssvm_segment_type_t type)
332{
333 svm_fifo_segment_create_args_t _a, *a = &_a;
334 clib_error_t *error;
335 int rv;
336
337 memset (a, 0, sizeof (*a));
338 a->segment_name = (char *) name;
339 a->segment_size = size;
340 a->segment_type = type;
341
342 if (type == SSVM_SEGMENT_MEMFD)
343 {
344 if ((error = vl_socket_client_recv_fd_msg (&a->memfd_fd, 5)))
345 {
346 clib_error_report (error);
347 return -1;
348 }
349 }
350
351 if ((rv = svm_fifo_segment_attach (a)))
352 {
353 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
354 return rv;
355 }
356
357 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700358}
359
360static void
361vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
362 mp)
363{
Florin Corasb384b542018-01-15 01:08:33 -0800364 echo_main_t *em = &echo_main;
365 ssvm_segment_type_t seg_type;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700366
367 if (mp->retval)
368 {
Florin Corasa5464812017-04-19 13:00:05 -0700369 clib_warning ("attach failed: %U", format_api_error,
370 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800371 em->state = STATE_FAILED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700372 return;
373 }
374
375 if (mp->segment_name_length == 0)
376 {
377 clib_warning ("segment_name_length zero");
378 return;
379 }
380
Florin Corasb384b542018-01-15 01:08:33 -0800381 seg_type = em->use_sock_api ? SSVM_SEGMENT_MEMFD : SSVM_SEGMENT_SHM;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700382
Florin Corasb384b542018-01-15 01:08:33 -0800383 /* Attach to fifo segment */
384 if (fifo_segment_attach ((char *) mp->segment_name, mp->segment_size,
385 seg_type))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700386 {
Florin Corasb384b542018-01-15 01:08:33 -0800387 em->state = STATE_FAILED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700388 return;
389 }
390
Florin Corasb384b542018-01-15 01:08:33 -0800391 /* If we're using memfd segments, read and attach to event qs segment */
392 if (seg_type == SSVM_SEGMENT_MEMFD)
393 {
394 if (memfd_segment_attach ())
395 {
396 clib_warning ("failed to attach to evt q segment");
397 em->state = STATE_FAILED;
398 return;
399 }
400 }
401
402 ASSERT (mp->app_event_queue_address);
403 em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
404 svm_queue_t *);
405 em->state = STATE_ATTACHED;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700406}
407
408static void
409vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
410 mp)
411{
412 if (mp->retval)
413 clib_warning ("detach returned with err: %d", mp->retval);
414}
415
Dave Barach68b0fb02017-02-28 15:15:56 -0500416static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500417stop_signal (int signum)
418{
Florin Corasb384b542018-01-15 01:08:33 -0800419 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500420
421 um->time_to_stop = 1;
422}
423
424static void
425stats_signal (int signum)
426{
Florin Corasb384b542018-01-15 01:08:33 -0800427 echo_main_t *um = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500428
429 um->time_to_print_stats = 1;
430}
431
432static clib_error_t *
433setup_signal_handlers (void)
434{
435 signal (SIGINT, stats_signal);
436 signal (SIGQUIT, stop_signal);
437 signal (SIGTERM, stop_signal);
438
439 return 0;
440}
441
442void
443vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
444{
445 clib_warning ("BUG");
446}
447
448int
449connect_to_vpp (char *name)
450{
Florin Corasb384b542018-01-15 01:08:33 -0800451 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500452 api_main_t *am = &api_main;
453
Florin Corasb384b542018-01-15 01:08:33 -0800454 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800455 {
Florin Corasb384b542018-01-15 01:08:33 -0800456 if (vl_socket_client_connect ((char *) em->socket_name, name,
Florin Coras90a63982017-12-19 04:50:01 -0800457 0 /* default rx, tx buffer */ ))
Florin Corasb384b542018-01-15 01:08:33 -0800458 {
459 clib_warning ("socket connect failed");
460 return -1;
461 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500462
Florin Corasb384b542018-01-15 01:08:33 -0800463 if (vl_socket_client_init_shm (0))
464 {
465 clib_warning ("init shm api failed");
466 return -1;
467 }
Florin Coras90a63982017-12-19 04:50:01 -0800468 }
469 else
470 {
471 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
Florin Corasb384b542018-01-15 01:08:33 -0800472 {
473 clib_warning ("shmem connect failed");
474 return -1;
475 }
Florin Coras90a63982017-12-19 04:50:01 -0800476 }
Florin Corasb384b542018-01-15 01:08:33 -0800477 em->vl_input_queue = am->shmem_hdr->vl_input_queue;
478 em->my_client_index = am->my_client_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500479 return 0;
480}
481
Florin Coras90a63982017-12-19 04:50:01 -0800482void
Florin Corasb384b542018-01-15 01:08:33 -0800483disconnect_from_vpp (echo_main_t * em)
Florin Coras90a63982017-12-19 04:50:01 -0800484{
Florin Corasb384b542018-01-15 01:08:33 -0800485 if (em->use_sock_api)
Florin Coras90a63982017-12-19 04:50:01 -0800486 vl_socket_client_disconnect ();
487 else
488 vl_client_disconnect_from_vlib ();
489}
490
Dave Barach68b0fb02017-02-28 15:15:56 -0500491static void
Florin Corase04c2992017-03-01 08:17:34 -0800492vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500493{
494 svm_fifo_segment_create_args_t _a, *a = &_a;
495 int rv;
496
Florin Coras3cbc04b2017-10-02 00:18:51 -0700497 memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500498 a->segment_name = (char *) mp->segment_name;
499 a->segment_size = mp->segment_size;
500 /* Attach to the segment vpp created */
501 rv = svm_fifo_segment_attach (a);
502 if (rv)
503 {
504 clib_warning ("svm_fifo_segment_attach ('%s') failed",
Florin Corase04c2992017-03-01 08:17:34 -0800505 mp->segment_name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500506 return;
507 }
508 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
Florin Corase04c2992017-03-01 08:17:34 -0800509 mp->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500510}
511
512static void
Florin Corasb384b542018-01-15 01:08:33 -0800513session_print_stats (echo_main_t * em, session_t * session)
Florin Corasf03a59a2017-06-09 21:07:32 -0700514{
515 f64 deltat;
516 u64 bytes;
517
Florin Corasb384b542018-01-15 01:08:33 -0800518 deltat = clib_time_now (&em->clib_time) - session->start;
519 bytes = em->i_am_master ? session->bytes_received : em->bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -0700520 fformat (stdout, "Finished in %.6f\n", deltat);
521 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
522}
523
524static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500525vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
526{
Florin Corasb384b542018-01-15 01:08:33 -0800527 echo_main_t *em = &echo_main;
Florin Corasf03a59a2017-06-09 21:07:32 -0700528 session_t *session = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800529 vl_api_disconnect_session_reply_t *rmp;
530 uword *p;
Dave Barach68b0fb02017-02-28 15:15:56 -0500531 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500532
Florin Corasb384b542018-01-15 01:08:33 -0800533 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800534
535 if (p)
536 {
Florin Corasb384b542018-01-15 01:08:33 -0800537 session = pool_elt_at_index (em->sessions, p[0]);
538 hash_unset (em->session_index_by_vpp_handles, mp->handle);
539 pool_put (em->sessions, session);
Florin Corase04c2992017-03-01 08:17:34 -0800540 }
541 else
542 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700543 clib_warning ("couldn't find session key %llx", mp->handle);
Florin Corase04c2992017-03-01 08:17:34 -0800544 rv = -11;
545 }
546
Florin Corasb384b542018-01-15 01:08:33 -0800547// em->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800548
549 rmp = vl_msg_api_alloc (sizeof (*rmp));
550 memset (rmp, 0, sizeof (*rmp));
551
552 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
553 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700554 rmp->handle = mp->handle;
Florin Corasf8f516a2018-02-08 15:10:09 -0800555 rmp->context = mp->context;
Florin Corasb384b542018-01-15 01:08:33 -0800556 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -0700557
558 if (session)
Florin Corasb384b542018-01-15 01:08:33 -0800559 session_print_stats (em, session);
Florin Corase04c2992017-03-01 08:17:34 -0800560}
561
562static void
563vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
564{
Florin Corasb384b542018-01-15 01:08:33 -0800565 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800566 vl_api_reset_session_reply_t *rmp;
567 uword *p;
568 int rv = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800569
Florin Corasb384b542018-01-15 01:08:33 -0800570 p = hash_get (em->session_index_by_vpp_handles, mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500571
572 if (p)
573 {
Florin Corasf6359c82017-06-19 12:26:09 -0400574 clib_warning ("got reset");
575 /* Cleanup later */
Florin Corasb384b542018-01-15 01:08:33 -0800576 em->time_to_stop = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500577 }
578 else
579 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700580 clib_warning ("couldn't find session key %llx", mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500581 rv = -11;
582 }
583
584 rmp = vl_msg_api_alloc (sizeof (*rmp));
585 memset (rmp, 0, sizeof (*rmp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800586 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500587 rmp->retval = rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700588 rmp->handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800589 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500590}
591
592void
Florin Corasb384b542018-01-15 01:08:33 -0800593client_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -0500594{
Florin Corase04c2992017-03-01 08:17:34 -0800595 svm_fifo_t *rx_fifo;
596 int n_read, bytes, i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500597
598 rx_fifo = e->fifo;
599
Florin Coras6792ec02017-03-13 03:49:51 -0700600 bytes = svm_fifo_max_dequeue (rx_fifo);
601 /* Allow enqueuing of new event */
602 svm_fifo_unset_event (rx_fifo);
603
604 /* Read the bytes */
Dave Barach68b0fb02017-02-28 15:15:56 -0500605 do
606 {
Florin Corasa5464812017-04-19 13:00:05 -0700607 n_read = svm_fifo_dequeue_nowait (rx_fifo,
Florin Corasb384b542018-01-15 01:08:33 -0800608 clib_min (vec_len (em->rx_buf),
609 bytes), em->rx_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500610 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800611 {
612 bytes -= n_read;
Florin Corasb384b542018-01-15 01:08:33 -0800613 if (em->test_return_packets)
Florin Corase04c2992017-03-01 08:17:34 -0800614 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700615 for (i = 0; i < n_read; i++)
Florin Corase04c2992017-03-01 08:17:34 -0800616 {
Florin Corasb384b542018-01-15 01:08:33 -0800617 if (em->rx_buf[i]
618 != ((em->client_bytes_received + i) & 0xff))
Florin Corasf03a59a2017-06-09 21:07:32 -0700619 {
620 clib_warning ("error at byte %lld, 0x%x not 0x%x",
Florin Corasb384b542018-01-15 01:08:33 -0800621 em->client_bytes_received + i,
622 em->rx_buf[i],
623 ((em->client_bytes_received + i) & 0xff));
Florin Corasf03a59a2017-06-09 21:07:32 -0700624 }
Florin Corase04c2992017-03-01 08:17:34 -0800625 }
626 }
Florin Corasb384b542018-01-15 01:08:33 -0800627 em->client_bytes_received += n_read;
Florin Corase04c2992017-03-01 08:17:34 -0800628 }
Florin Coras6792ec02017-03-13 03:49:51 -0700629 else
630 {
631 if (n_read == -2)
632 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700633// clib_warning ("weird!");
Florin Coras6792ec02017-03-13 03:49:51 -0700634 break;
635 }
636 }
Florin Corase04c2992017-03-01 08:17:34 -0800637
Dave Barach68b0fb02017-02-28 15:15:56 -0500638 }
Florin Coras6792ec02017-03-13 03:49:51 -0700639 while (bytes > 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500640}
641
642void
Florin Corasb384b542018-01-15 01:08:33 -0800643client_handle_event_queue (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -0500644{
645 session_fifo_event_t _e, *e = &_e;;
646
Florin Corasb384b542018-01-15 01:08:33 -0800647 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500648 switch (e->event_type)
649 {
Florin Corasa5464812017-04-19 13:00:05 -0700650 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -0800651 client_handle_fifo_event_rx (em, e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500652 break;
653
Florin Corasa5464812017-04-19 13:00:05 -0700654 case FIFO_EVENT_DISCONNECT:
Dave Barach68b0fb02017-02-28 15:15:56 -0500655 return;
656
657 default:
Florin Corase04c2992017-03-01 08:17:34 -0800658 clib_warning ("unknown event type %d", e->event_type);
Dave Barach68b0fb02017-02-28 15:15:56 -0500659 break;
660 }
661}
662
Florin Corase04c2992017-03-01 08:17:34 -0800663static void *
664client_rx_thread_fn (void *arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500665{
Florin Corase04c2992017-03-01 08:17:34 -0800666 session_fifo_event_t _e, *e = &_e;
Florin Corasb384b542018-01-15 01:08:33 -0800667 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500668
Florin Corasb384b542018-01-15 01:08:33 -0800669 em->client_bytes_received = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500670 while (1)
671 {
Florin Corasb384b542018-01-15 01:08:33 -0800672 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500673 switch (e->event_type)
Florin Corase04c2992017-03-01 08:17:34 -0800674 {
Florin Corasa5464812017-04-19 13:00:05 -0700675 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -0800676 client_handle_fifo_event_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -0800677 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500678
Florin Corasa5464812017-04-19 13:00:05 -0700679 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -0800680 return 0;
681 default:
682 clib_warning ("unknown event type %d", e->event_type);
683 break;
684 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500685
Florin Corasb384b542018-01-15 01:08:33 -0800686 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -0800687 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500688 }
Florin Corase04c2992017-03-01 08:17:34 -0800689 pthread_exit (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500690}
691
Dave Barach68b0fb02017-02-28 15:15:56 -0500692
693static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400694vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500695{
Florin Corasb384b542018-01-15 01:08:33 -0800696 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500697 session_t *session;
698 u32 session_index;
699 svm_fifo_t *rx_fifo, *tx_fifo;
700 int rv;
701
702 if (mp->retval)
703 {
Florin Corasa5464812017-04-19 13:00:05 -0700704 clib_warning ("connection failed with code: %U", format_api_error,
705 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800706 em->state = STATE_FAILED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500707 return;
708 }
Florin Corasade70e42017-10-14 18:56:41 -0700709 else
710 {
711 clib_warning ("connected with local ip %U port %d", format_ip46_address,
712 mp->lcl_ip, mp->is_ip4,
713 clib_net_to_host_u16 (mp->lcl_port));
714 }
Florin Corase04c2992017-03-01 08:17:34 -0800715
Florin Corasb384b542018-01-15 01:08:33 -0800716 em->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -0800717 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500718
719 /*
720 * Setup session
721 */
722
Florin Corasb384b542018-01-15 01:08:33 -0800723 pool_get (em->sessions, session);
724 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -0500725
Damjan Marion7bee80c2017-04-26 15:32:12 +0200726 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500727 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200728 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500729 tx_fifo->client_session_index = session_index;
730
731 session->server_rx_fifo = rx_fifo;
732 session->server_tx_fifo = tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700733 session->vpp_session_handle = mp->handle;
Florin Corasb384b542018-01-15 01:08:33 -0800734 session->start = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -0500735
736 /* Save handle */
Florin Corasb384b542018-01-15 01:08:33 -0800737 em->connected_session_index = session_index;
738 em->state = STATE_READY;
Florin Corase04c2992017-03-01 08:17:34 -0800739
740 /* Add it to lookup table */
Florin Corasb384b542018-01-15 01:08:33 -0800741 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800742
743 /* Start RX thread */
Florin Corasb384b542018-01-15 01:08:33 -0800744 rv = pthread_create (&em->client_rx_thread_handle,
Florin Corase04c2992017-03-01 08:17:34 -0800745 NULL /*attr */ , client_rx_thread_fn, 0);
746 if (rv)
747 {
748 clib_warning ("pthread_create returned %d", rv);
749 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
750 }
751}
752
Florin Coras6792ec02017-03-13 03:49:51 -0700753static void
Florin Corasb384b542018-01-15 01:08:33 -0800754send_test_chunk (echo_main_t * em, svm_fifo_t * tx_fifo, int mypid, u32 bytes)
Florin Corase04c2992017-03-01 08:17:34 -0800755{
Florin Corasb384b542018-01-15 01:08:33 -0800756 u8 *test_data = em->connect_test_data;
Florin Corase04c2992017-03-01 08:17:34 -0800757 u64 bytes_sent = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700758 int test_buf_offset = 0;
759 u32 bytes_to_snd;
Florin Corasf03a59a2017-06-09 21:07:32 -0700760 u32 queue_max_chunk = 128 << 10, actual_write;
Florin Corase04c2992017-03-01 08:17:34 -0800761 session_fifo_event_t evt;
Florin Coras6792ec02017-03-13 03:49:51 -0700762 int rv;
Florin Corase04c2992017-03-01 08:17:34 -0800763
Florin Coras6792ec02017-03-13 03:49:51 -0700764 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
765 if (bytes_to_snd > vec_len (test_data))
766 bytes_to_snd = vec_len (test_data);
Florin Corase04c2992017-03-01 08:17:34 -0800767
Florin Corasb384b542018-01-15 01:08:33 -0800768 while (bytes_to_snd > 0 && !em->time_to_stop)
Florin Corase04c2992017-03-01 08:17:34 -0800769 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700770 actual_write = (bytes_to_snd > queue_max_chunk) ?
771 queue_max_chunk : bytes_to_snd;
Florin Corasa5464812017-04-19 13:00:05 -0700772 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
Florin Coras6792ec02017-03-13 03:49:51 -0700773 test_data + test_buf_offset);
774
775 if (rv > 0)
Florin Corase04c2992017-03-01 08:17:34 -0800776 {
Florin Coras6792ec02017-03-13 03:49:51 -0700777 bytes_to_snd -= rv;
778 test_buf_offset += rv;
779 bytes_sent += rv;
Florin Corase04c2992017-03-01 08:17:34 -0800780
Florin Coras6792ec02017-03-13 03:49:51 -0700781 if (svm_fifo_set_event (tx_fifo))
Florin Corase04c2992017-03-01 08:17:34 -0800782 {
Florin Corase04c2992017-03-01 08:17:34 -0800783 /* Fabricate TX event, send to vpp */
784 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700785 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase04c2992017-03-01 08:17:34 -0800786
Florin Corasb384b542018-01-15 01:08:33 -0800787 svm_queue_add (em->vpp_event_queue,
Florin Corase86a8ed2018-01-05 03:20:25 -0800788 (u8 *) & evt, 0 /* do wait for mutex */ );
Florin Corase04c2992017-03-01 08:17:34 -0800789 }
790 }
791 }
Florin Coras6792ec02017-03-13 03:49:51 -0700792}
793
794void
Florin Corasb384b542018-01-15 01:08:33 -0800795client_send_data (echo_main_t * em)
Florin Coras6792ec02017-03-13 03:49:51 -0700796{
Florin Corasb384b542018-01-15 01:08:33 -0800797 u8 *test_data = em->connect_test_data;
Florin Coras6792ec02017-03-13 03:49:51 -0700798 int mypid = getpid ();
799 session_t *session;
800 svm_fifo_t *tx_fifo;
801 u32 n_iterations, leftover;
802 int i;
803
Florin Corasb384b542018-01-15 01:08:33 -0800804 session = pool_elt_at_index (em->sessions, em->connected_session_index);
Florin Coras6792ec02017-03-13 03:49:51 -0700805 tx_fifo = session->server_tx_fifo;
806
Florin Coras82b13a82017-04-25 11:58:06 -0700807 ASSERT (vec_len (test_data) > 0);
808
Florin Corasb384b542018-01-15 01:08:33 -0800809 vec_validate (em->rx_buf, vec_len (test_data) - 1);
810 n_iterations = em->bytes_to_send / vec_len (test_data);
Florin Coras6792ec02017-03-13 03:49:51 -0700811
812 for (i = 0; i < n_iterations; i++)
813 {
Florin Corasb384b542018-01-15 01:08:33 -0800814 send_test_chunk (em, tx_fifo, mypid, 0);
815 if (em->time_to_stop)
Florin Corasf6359c82017-06-19 12:26:09 -0400816 break;
Florin Coras6792ec02017-03-13 03:49:51 -0700817 }
818
Florin Corasb384b542018-01-15 01:08:33 -0800819 leftover = em->bytes_to_send % vec_len (test_data);
Florin Coras6792ec02017-03-13 03:49:51 -0700820 if (leftover)
Florin Corasb384b542018-01-15 01:08:33 -0800821 send_test_chunk (em, tx_fifo, mypid, leftover);
Florin Corase04c2992017-03-01 08:17:34 -0800822
Florin Corasb384b542018-01-15 01:08:33 -0800823 if (!em->drop_packets)
Florin Corase04c2992017-03-01 08:17:34 -0800824 {
Florin Corasb384b542018-01-15 01:08:33 -0800825 f64 timeout = clib_time_now (&em->clib_time) + 10;
Florin Corase04c2992017-03-01 08:17:34 -0800826
827 /* Wait for the outstanding packets */
Florin Corasb384b542018-01-15 01:08:33 -0800828 while (em->client_bytes_received <
Florin Coras6792ec02017-03-13 03:49:51 -0700829 vec_len (test_data) * n_iterations + leftover)
Florin Corase04c2992017-03-01 08:17:34 -0800830 {
Florin Corasb384b542018-01-15 01:08:33 -0800831 if (clib_time_now (&em->clib_time) > timeout)
Florin Corase04c2992017-03-01 08:17:34 -0800832 {
833 clib_warning ("timed out waiting for the missing packets");
834 break;
835 }
836 }
Florin Corase04c2992017-03-01 08:17:34 -0800837 }
Florin Corasb384b542018-01-15 01:08:33 -0800838 em->time_to_stop = 1;
Florin Corase04c2992017-03-01 08:17:34 -0800839}
840
841void
Florin Corasb384b542018-01-15 01:08:33 -0800842client_send_connect (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800843{
844 vl_api_connect_uri_t *cmp;
845 cmp = vl_msg_api_alloc (sizeof (*cmp));
846 memset (cmp, 0, sizeof (*cmp));
847
848 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
Florin Corasb384b542018-01-15 01:08:33 -0800849 cmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -0800850 cmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -0800851 memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri));
852 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
Florin Corase04c2992017-03-01 08:17:34 -0800853}
854
Florin Corasa5464812017-04-19 13:00:05 -0700855int
Florin Corasb384b542018-01-15 01:08:33 -0800856client_connect (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700857{
Florin Corasb384b542018-01-15 01:08:33 -0800858 client_send_connect (em);
859 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -0700860 {
861 clib_warning ("Connect failed");
862 return -1;
863 }
864 return 0;
865}
866
Florin Corase04c2992017-03-01 08:17:34 -0800867void
Florin Corasb384b542018-01-15 01:08:33 -0800868client_send_disconnect (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800869{
870 session_t *connected_session;
871 vl_api_disconnect_session_t *dmp;
Florin Corasb384b542018-01-15 01:08:33 -0800872 connected_session = pool_elt_at_index (em->sessions,
873 em->connected_session_index);
Florin Corase04c2992017-03-01 08:17:34 -0800874 dmp = vl_msg_api_alloc (sizeof (*dmp));
875 memset (dmp, 0, sizeof (*dmp));
876 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
Florin Corasb384b542018-01-15 01:08:33 -0800877 dmp->client_index = em->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700878 dmp->handle = connected_session->vpp_session_handle;
Florin Corasb384b542018-01-15 01:08:33 -0800879 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
Florin Corase04c2992017-03-01 08:17:34 -0800880}
881
Florin Corasa5464812017-04-19 13:00:05 -0700882int
Florin Corasb384b542018-01-15 01:08:33 -0800883client_disconnect (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -0700884{
Florin Corasb384b542018-01-15 01:08:33 -0800885 client_send_disconnect (em);
Florin Corasf03a59a2017-06-09 21:07:32 -0700886 clib_warning ("Sent disconnect");
Florin Corasb384b542018-01-15 01:08:33 -0800887 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -0700888 {
889 clib_warning ("Disconnect failed");
890 return -1;
891 }
892 return 0;
893}
894
Florin Corase04c2992017-03-01 08:17:34 -0800895static void
Florin Corasb384b542018-01-15 01:08:33 -0800896client_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -0800897{
898 int i;
899
Florin Corasb384b542018-01-15 01:08:33 -0800900 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -0700901 return;
Florin Corase04c2992017-03-01 08:17:34 -0800902
Florin Corasb384b542018-01-15 01:08:33 -0800903 if (client_connect (em))
Florin Corase04c2992017-03-01 08:17:34 -0800904 {
Florin Corasb384b542018-01-15 01:08:33 -0800905 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -0800906 return;
907 }
908
909 /* Init test data */
Florin Corasb384b542018-01-15 01:08:33 -0800910 vec_validate (em->connect_test_data, 128 * 1024 - 1);
911 for (i = 0; i < vec_len (em->connect_test_data); i++)
912 em->connect_test_data[i] = i & 0xff;
Florin Corase04c2992017-03-01 08:17:34 -0800913
914 /* Start send */
Florin Corasb384b542018-01-15 01:08:33 -0800915 client_send_data (em);
Florin Corase04c2992017-03-01 08:17:34 -0800916
Florin Corasb384b542018-01-15 01:08:33 -0800917 /* Disconnect and detach */
918 client_disconnect (em);
919 application_detach (em);
Florin Corase04c2992017-03-01 08:17:34 -0800920}
921
922static void
923vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
924{
Florin Corasb384b542018-01-15 01:08:33 -0800925 echo_main_t *em = &echo_main;
Florin Corase04c2992017-03-01 08:17:34 -0800926
927 if (mp->retval)
928 {
flyingeagle23a07779f2017-04-26 20:22:04 +0800929 clib_warning ("bind failed: %U", format_api_error,
Florin Corasa5464812017-04-19 13:00:05 -0700930 clib_net_to_host_u32 (mp->retval));
Florin Corasb384b542018-01-15 01:08:33 -0800931 em->state = STATE_FAILED;
Florin Corase04c2992017-03-01 08:17:34 -0800932 return;
933 }
934
Florin Corasb384b542018-01-15 01:08:33 -0800935 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500936}
937
Dave Barach68b0fb02017-02-28 15:15:56 -0500938static void
Florin Corase04c2992017-03-01 08:17:34 -0800939vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500940{
Florin Corasb384b542018-01-15 01:08:33 -0800941 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500942
943 if (mp->retval != 0)
Florin Corase04c2992017-03-01 08:17:34 -0800944 clib_warning ("returned %d", ntohl (mp->retval));
Dave Barach68b0fb02017-02-28 15:15:56 -0500945
Florin Corasb384b542018-01-15 01:08:33 -0800946 em->state = STATE_START;
Dave Barach68b0fb02017-02-28 15:15:56 -0500947}
948
Florin Coras6cf30ad2017-04-04 23:08:23 -0700949u8 *
950format_ip4_address (u8 * s, va_list * args)
951{
952 u8 *a = va_arg (*args, u8 *);
953 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
954}
955
956u8 *
957format_ip6_address (u8 * s, va_list * args)
958{
959 ip6_address_t *a = va_arg (*args, ip6_address_t *);
960 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
961
962 i_max_n_zero = ARRAY_LEN (a->as_u16);
963 max_n_zeros = 0;
964 i_first_zero = i_max_n_zero;
965 n_zeros = 0;
966 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
967 {
968 u32 is_zero = a->as_u16[i] == 0;
969 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
970 {
971 i_first_zero = i;
972 n_zeros = 0;
973 }
974 n_zeros += is_zero;
975 if ((!is_zero && n_zeros > max_n_zeros)
976 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
977 {
978 i_max_n_zero = i_first_zero;
979 max_n_zeros = n_zeros;
980 i_first_zero = ARRAY_LEN (a->as_u16);
981 n_zeros = 0;
982 }
983 }
984
985 last_double_colon = 0;
986 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
987 {
988 if (i == i_max_n_zero && max_n_zeros > 1)
989 {
990 s = format (s, "::");
991 i += max_n_zeros - 1;
992 last_double_colon = 1;
993 }
994 else
995 {
996 s = format (s, "%s%x",
997 (last_double_colon || i == 0) ? "" : ":",
998 clib_net_to_host_u16 (a->as_u16[i]));
999 last_double_colon = 0;
1000 }
1001 }
1002
1003 return s;
1004}
1005
1006/* Format an IP46 address. */
1007u8 *
1008format_ip46_address (u8 * s, va_list * args)
1009{
1010 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1011 ip46_type_t type = va_arg (*args, ip46_type_t);
1012 int is_ip4 = 1;
1013
1014 switch (type)
1015 {
1016 case IP46_TYPE_ANY:
1017 is_ip4 = ip46_address_is_ip4 (ip46);
1018 break;
1019 case IP46_TYPE_IP4:
1020 is_ip4 = 1;
1021 break;
1022 case IP46_TYPE_IP6:
1023 is_ip4 = 0;
1024 break;
1025 }
1026
1027 return is_ip4 ?
1028 format (s, "%U", format_ip4_address, &ip46->ip4) :
1029 format (s, "%U", format_ip6_address, &ip46->ip6);
1030}
1031
Dave Barach68b0fb02017-02-28 15:15:56 -05001032static void
1033vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1034{
Florin Corasb384b542018-01-15 01:08:33 -08001035 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001036 vl_api_accept_session_reply_t *rmp;
Florin Corase04c2992017-03-01 08:17:34 -08001037 svm_fifo_t *rx_fifo, *tx_fifo;
1038 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -05001039 static f64 start_time;
Dave Barach68b0fb02017-02-28 15:15:56 -05001040 u32 session_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001041 u8 *ip_str;
Dave Barach68b0fb02017-02-28 15:15:56 -05001042
1043 if (start_time == 0.0)
Florin Corasb384b542018-01-15 01:08:33 -08001044 start_time = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001045
Florin Coras6cf30ad2017-04-04 23:08:23 -07001046 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1047 clib_warning ("Accepted session from: %s:%d", ip_str,
1048 clib_net_to_host_u16 (mp->port));
Florin Corasb384b542018-01-15 01:08:33 -08001049 em->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -08001050 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001051
1052 /* Allocate local session and set it up */
Florin Corasb384b542018-01-15 01:08:33 -08001053 pool_get (em->sessions, session);
1054 session_index = session - em->sessions;
Dave Barach68b0fb02017-02-28 15:15:56 -05001055
Damjan Marion7bee80c2017-04-26 15:32:12 +02001056 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001057 rx_fifo->client_session_index = session_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +02001058 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -05001059 tx_fifo->client_session_index = session_index;
1060
1061 session->server_rx_fifo = rx_fifo;
1062 session->server_tx_fifo = tx_fifo;
1063
1064 /* Add it to lookup table */
Florin Corasb384b542018-01-15 01:08:33 -08001065 hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001066
Florin Corasb384b542018-01-15 01:08:33 -08001067 em->state = STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001068
1069 /* Stats printing */
Florin Corasb384b542018-01-15 01:08:33 -08001070 if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001071 {
Florin Corasb384b542018-01-15 01:08:33 -08001072 f64 now = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001073 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
Florin Corasb384b542018-01-15 01:08:33 -08001074 pool_elts (em->sessions), now - start_time,
1075 (f64) pool_elts (em->sessions) / (now - start_time));
Dave Barach68b0fb02017-02-28 15:15:56 -05001076 }
1077
Florin Corase04c2992017-03-01 08:17:34 -08001078 /*
1079 * Send accept reply to vpp
1080 */
Dave Barach68b0fb02017-02-28 15:15:56 -05001081 rmp = vl_msg_api_alloc (sizeof (*rmp));
1082 memset (rmp, 0, sizeof (*rmp));
1083 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001084 rmp->handle = mp->handle;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001085 rmp->context = mp->context;
Florin Corasb384b542018-01-15 01:08:33 -08001086 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
Florin Corasf03a59a2017-06-09 21:07:32 -07001087
1088 session->bytes_received = 0;
Florin Corasb384b542018-01-15 01:08:33 -08001089 session->start = clib_time_now (&em->clib_time);
Dave Barach68b0fb02017-02-28 15:15:56 -05001090}
1091
1092void
Florin Corasb384b542018-01-15 01:08:33 -08001093server_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e)
Dave Barach68b0fb02017-02-28 15:15:56 -05001094{
Florin Corase04c2992017-03-01 08:17:34 -08001095 svm_fifo_t *rx_fifo, *tx_fifo;
1096 int n_read;
Florin Corase04c2992017-03-01 08:17:34 -08001097 session_fifo_event_t evt;
Florin Corase86a8ed2018-01-05 03:20:25 -08001098 svm_queue_t *q;
Florin Corasf03a59a2017-06-09 21:07:32 -07001099 session_t *session;
1100 int rv;
1101 u32 max_dequeue, offset, max_transfer, rx_buf_len;
Florin Corase04c2992017-03-01 08:17:34 -08001102
Florin Corasb384b542018-01-15 01:08:33 -08001103 rx_buf_len = vec_len (em->rx_buf);
Florin Corase04c2992017-03-01 08:17:34 -08001104 rx_fifo = e->fifo;
Florin Corasb384b542018-01-15 01:08:33 -08001105 session = &em->sessions[rx_fifo->client_session_index];
Florin Corasf03a59a2017-06-09 21:07:32 -07001106 tx_fifo = session->server_tx_fifo;
Florin Corase04c2992017-03-01 08:17:34 -08001107
Florin Corasf03a59a2017-06-09 21:07:32 -07001108 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -07001109 /* Allow enqueuing of a new event */
1110 svm_fifo_unset_event (rx_fifo);
1111
Florin Corasf03a59a2017-06-09 21:07:32 -07001112 if (PREDICT_FALSE (max_dequeue == 0))
1113 {
1114 return;
1115 }
Florin Coras6792ec02017-03-13 03:49:51 -07001116
Florin Corasf03a59a2017-06-09 21:07:32 -07001117 /* Read the max_dequeue */
Florin Corase04c2992017-03-01 08:17:34 -08001118 do
1119 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001120 max_transfer = clib_min (rx_buf_len, max_dequeue);
Florin Corasb384b542018-01-15 01:08:33 -08001121 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, em->rx_buf);
Florin Coras6792ec02017-03-13 03:49:51 -07001122 if (n_read > 0)
Florin Corase04c2992017-03-01 08:17:34 -08001123 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001124 max_dequeue -= n_read;
1125 session->bytes_received += n_read;
1126 }
1127
1128 /* Reflect if a non-drop session */
Florin Corasb384b542018-01-15 01:08:33 -08001129 if (!em->drop_packets && n_read > 0)
Florin Corasf03a59a2017-06-09 21:07:32 -07001130 {
1131 offset = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001132 do
1133 {
Florin Corasf03a59a2017-06-09 21:07:32 -07001134 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
Florin Corasb384b542018-01-15 01:08:33 -08001135 &em->rx_buf[offset]);
Florin Corasf03a59a2017-06-09 21:07:32 -07001136 if (rv > 0)
1137 {
1138 n_read -= rv;
1139 offset += rv;
1140 }
Florin Corase04c2992017-03-01 08:17:34 -08001141 }
Florin Corasb384b542018-01-15 01:08:33 -08001142 while ((rv <= 0 || n_read > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001143
Florin Coras6792ec02017-03-13 03:49:51 -07001144 /* If event wasn't set, add one */
1145 if (svm_fifo_set_event (tx_fifo))
1146 {
1147 /* Fabricate TX event, send to vpp */
1148 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -07001149 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -07001150
Florin Corasb384b542018-01-15 01:08:33 -08001151 q = em->vpp_event_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -08001152 svm_queue_add (q, (u8 *) & evt, 1 /* do wait for mutex */ );
Florin Coras6792ec02017-03-13 03:49:51 -07001153 }
Florin Corase04c2992017-03-01 08:17:34 -08001154 }
Florin Corase04c2992017-03-01 08:17:34 -08001155 }
Florin Corasb384b542018-01-15 01:08:33 -08001156 while ((n_read < 0 || max_dequeue > 0) && !em->time_to_stop);
Florin Corase04c2992017-03-01 08:17:34 -08001157}
1158
1159void
Florin Corasb384b542018-01-15 01:08:33 -08001160server_handle_event_queue (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001161{
Florin Coras3cbc04b2017-10-02 00:18:51 -07001162 session_fifo_event_t _e, *e = &_e;
Florin Corase04c2992017-03-01 08:17:34 -08001163
1164 while (1)
1165 {
Florin Corasb384b542018-01-15 01:08:33 -08001166 svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
Florin Corase04c2992017-03-01 08:17:34 -08001167 switch (e->event_type)
1168 {
Florin Corasa5464812017-04-19 13:00:05 -07001169 case FIFO_EVENT_APP_RX:
Florin Corasb384b542018-01-15 01:08:33 -08001170 server_handle_fifo_event_rx (em, e);
Florin Corase04c2992017-03-01 08:17:34 -08001171 break;
1172
Florin Corasa5464812017-04-19 13:00:05 -07001173 case FIFO_EVENT_DISCONNECT:
Florin Corase04c2992017-03-01 08:17:34 -08001174 return;
1175
1176 default:
1177 clib_warning ("unknown event type %d", e->event_type);
1178 break;
1179 }
Florin Corasb384b542018-01-15 01:08:33 -08001180 if (PREDICT_FALSE (em->time_to_stop == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001181 break;
Florin Corasb384b542018-01-15 01:08:33 -08001182 if (PREDICT_FALSE (em->time_to_print_stats == 1))
Florin Corase04c2992017-03-01 08:17:34 -08001183 {
Florin Corasb384b542018-01-15 01:08:33 -08001184 em->time_to_print_stats = 0;
1185 fformat (stdout, "%d connections\n", pool_elts (em->sessions));
Florin Corase04c2992017-03-01 08:17:34 -08001186 }
1187 }
1188}
1189
1190void
Florin Corasb384b542018-01-15 01:08:33 -08001191server_send_listen (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001192{
1193 vl_api_bind_uri_t *bmp;
Florin Corase04c2992017-03-01 08:17:34 -08001194 bmp = vl_msg_api_alloc (sizeof (*bmp));
1195 memset (bmp, 0, sizeof (*bmp));
1196
1197 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001198 bmp->client_index = em->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -08001199 bmp->context = ntohl (0xfeedface);
Florin Corasb384b542018-01-15 01:08:33 -08001200 memcpy (bmp->uri, em->uri, vec_len (em->uri));
1201 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
Florin Corase04c2992017-03-01 08:17:34 -08001202}
1203
Florin Corasa5464812017-04-19 13:00:05 -07001204int
Florin Corasb384b542018-01-15 01:08:33 -08001205server_listen (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001206{
Florin Corasb384b542018-01-15 01:08:33 -08001207 server_send_listen (em);
1208 if (wait_for_state_change (em, STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -07001209 {
1210 clib_warning ("timeout waiting for STATE_READY");
1211 return -1;
1212 }
1213 return 0;
1214}
1215
Florin Corase04c2992017-03-01 08:17:34 -08001216void
Florin Corasb384b542018-01-15 01:08:33 -08001217server_send_unbind (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001218{
1219 vl_api_unbind_uri_t *ump;
1220
1221 ump = vl_msg_api_alloc (sizeof (*ump));
1222 memset (ump, 0, sizeof (*ump));
1223
1224 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
Florin Corasb384b542018-01-15 01:08:33 -08001225 ump->client_index = em->my_client_index;
1226 memcpy (ump->uri, em->uri, vec_len (em->uri));
1227 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
Florin Corase04c2992017-03-01 08:17:34 -08001228}
1229
Florin Corasa5464812017-04-19 13:00:05 -07001230int
Florin Corasb384b542018-01-15 01:08:33 -08001231server_unbind (echo_main_t * em)
Florin Corasa5464812017-04-19 13:00:05 -07001232{
Florin Corasb384b542018-01-15 01:08:33 -08001233 server_send_unbind (em);
1234 if (wait_for_state_change (em, STATE_START))
Florin Corasa5464812017-04-19 13:00:05 -07001235 {
1236 clib_warning ("timeout waiting for STATE_START");
1237 return -1;
1238 }
1239 return 0;
1240}
1241
Florin Corase04c2992017-03-01 08:17:34 -08001242void
Florin Corasb384b542018-01-15 01:08:33 -08001243server_run (echo_main_t * em)
Florin Corase04c2992017-03-01 08:17:34 -08001244{
Florin Coras90a63982017-12-19 04:50:01 -08001245 session_t *session;
1246 int i;
1247
1248 /* $$$$ hack preallocation */
1249 for (i = 0; i < 200000; i++)
1250 {
Florin Corasb384b542018-01-15 01:08:33 -08001251 pool_get (em->sessions, session);
Florin Coras90a63982017-12-19 04:50:01 -08001252 memset (session, 0, sizeof (*session));
1253 }
1254 for (i = 0; i < 200000; i++)
Florin Corasb384b542018-01-15 01:08:33 -08001255 pool_put_index (em->sessions, i);
Florin Coras90a63982017-12-19 04:50:01 -08001256
Florin Corasb384b542018-01-15 01:08:33 -08001257 if (application_attach (em))
Florin Corasa5464812017-04-19 13:00:05 -07001258 return;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001259
Dave Barach68b0fb02017-02-28 15:15:56 -05001260 /* Bind to uri */
Florin Corasb384b542018-01-15 01:08:33 -08001261 if (server_listen (em))
Florin Corasa5464812017-04-19 13:00:05 -07001262 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001263
1264 /* Enter handle event loop */
Florin Corasb384b542018-01-15 01:08:33 -08001265 server_handle_event_queue (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001266
1267 /* Cleanup */
Florin Corasb384b542018-01-15 01:08:33 -08001268 server_send_unbind (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001269
Florin Corasb384b542018-01-15 01:08:33 -08001270 application_detach (em);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001271
Dave Barach68b0fb02017-02-28 15:15:56 -05001272 fformat (stdout, "Test complete...\n");
1273}
1274
Florin Corase69f4952017-03-07 10:06:24 -08001275static void
1276vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1277 mp)
1278{
Florin Corasb384b542018-01-15 01:08:33 -08001279 echo_main_t *em = &echo_main;
Florin Corasf03a59a2017-06-09 21:07:32 -07001280 session_t *session;
Florin Coras6792ec02017-03-13 03:49:51 -07001281
Florin Corasf03a59a2017-06-09 21:07:32 -07001282 if (mp->retval)
1283 {
1284 clib_warning ("vpp complained about disconnect: %d",
1285 ntohl (mp->retval));
1286 }
1287
Florin Corasb384b542018-01-15 01:08:33 -08001288 em->state = STATE_START;
1289 session = pool_elt_at_index (em->sessions, em->connected_session_index);
Florin Corasf03a59a2017-06-09 21:07:32 -07001290 if (session)
Florin Corasb384b542018-01-15 01:08:33 -08001291 session_print_stats (em, session);
Florin Corase69f4952017-03-07 10:06:24 -08001292}
1293
Florin Corase3e2f072018-03-04 07:24:30 -08001294static void
1295 vl_api_application_tls_cert_add_reply_t_handler
1296 (vl_api_application_tls_cert_add_reply_t * mp)
1297{
1298 if (mp->retval)
1299 clib_warning ("failed to add tls cert");
1300}
1301
1302static void
1303 vl_api_application_tls_key_add_reply_t_handler
1304 (vl_api_application_tls_key_add_reply_t * mp)
1305{
1306 if (mp->retval)
1307 clib_warning ("failed to add tls key");
1308}
1309
1310#define foreach_tcp_echo_msg \
1311_(BIND_URI_REPLY, bind_uri_reply) \
1312_(UNBIND_URI_REPLY, unbind_uri_reply) \
1313_(ACCEPT_SESSION, accept_session) \
1314_(CONNECT_SESSION_REPLY, connect_session_reply) \
1315_(DISCONNECT_SESSION, disconnect_session) \
1316_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1317_(RESET_SESSION, reset_session) \
1318_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1319_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1320_(MAP_ANOTHER_SEGMENT, map_another_segment) \
1321_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
1322_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Dave Barach68b0fb02017-02-28 15:15:56 -05001323
1324void
Florin Corasb384b542018-01-15 01:08:33 -08001325tcp_echo_api_hookup (echo_main_t * em)
Dave Barach68b0fb02017-02-28 15:15:56 -05001326{
1327#define _(N,n) \
1328 vl_msg_api_set_handlers(VL_API_##N, #n, \
1329 vl_api_##n##_t_handler, \
1330 vl_noop_handler, \
1331 vl_api_##n##_t_endian, \
1332 vl_api_##n##_t_print, \
1333 sizeof(vl_api_##n##_t), 1);
Florin Corasb384b542018-01-15 01:08:33 -08001334 foreach_tcp_echo_msg;
Dave Barach68b0fb02017-02-28 15:15:56 -05001335#undef _
1336}
1337
1338int
1339main (int argc, char **argv)
1340{
Florin Corasb384b542018-01-15 01:08:33 -08001341 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1342 echo_main_t *em = &echo_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001343 unformat_input_t _argv, *a = &_argv;
1344 u8 *chroot_prefix;
Florin Corase69f4952017-03-07 10:06:24 -08001345 u8 *heap, *uri = 0;
1346 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1347 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
Florin Coras6cf30ad2017-04-04 23:08:23 -07001348 u64 bytes_to_send = 64 << 10, mbytes;
Florin Corasb384b542018-01-15 01:08:33 -08001349 char *app_name;
Dave Barach68b0fb02017-02-28 15:15:56 -05001350 u32 tmp;
1351 mheap_t *h;
Dave Barach68b0fb02017-02-28 15:15:56 -05001352
1353 clib_mem_init (0, 256 << 20);
1354
1355 heap = clib_mem_get_per_cpu_heap ();
1356 h = mheap_header (heap);
1357
1358 /* make the main heap thread-safe */
1359 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1360
Florin Corasb384b542018-01-15 01:08:33 -08001361 vec_validate (em->rx_buf, 128 << 10);
Dave Barach68b0fb02017-02-28 15:15:56 -05001362
Florin Corasb384b542018-01-15 01:08:33 -08001363 em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Dave Barach68b0fb02017-02-28 15:15:56 -05001364
Florin Corasb384b542018-01-15 01:08:33 -08001365 em->my_pid = getpid ();
1366 em->configured_segment_size = 1 << 20;
1367 em->socket_name = 0;
1368 em->use_sock_api = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001369
Florin Corasb384b542018-01-15 01:08:33 -08001370 clib_time_init (&em->clib_time);
1371 init_error_string_table (em);
Florin Corasa332c462018-01-31 06:52:17 -08001372 svm_fifo_segment_main_init (0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -05001373 unformat_init_command_line (a, argv);
1374
1375 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1376 {
1377 if (unformat (a, "chroot prefix %s", &chroot_prefix))
Florin Corase04c2992017-03-01 08:17:34 -08001378 {
1379 vl_set_memory_root_path ((char *) chroot_prefix);
1380 }
Florin Corase69f4952017-03-07 10:06:24 -08001381 else if (unformat (a, "uri %s", &uri))
Florin Corase04c2992017-03-01 08:17:34 -08001382 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001383 else if (unformat (a, "segment-size %dM", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001384 em->configured_segment_size = tmp << 20;
Dave Barach68b0fb02017-02-28 15:15:56 -05001385 else if (unformat (a, "segment-size %dG", &tmp))
Florin Corasb384b542018-01-15 01:08:33 -08001386 em->configured_segment_size = tmp << 30;
Dave Barach68b0fb02017-02-28 15:15:56 -05001387 else if (unformat (a, "master"))
Florin Corase04c2992017-03-01 08:17:34 -08001388 i_am_master = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001389 else if (unformat (a, "slave"))
Florin Corase04c2992017-03-01 08:17:34 -08001390 i_am_master = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001391 else if (unformat (a, "drop"))
Florin Corase04c2992017-03-01 08:17:34 -08001392 drop_packets = 1;
1393 else if (unformat (a, "test"))
1394 test_return_packets = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001395 else if (unformat (a, "mbytes %lld", &mbytes))
Florin Coras6792ec02017-03-13 03:49:51 -07001396 {
1397 bytes_to_send = mbytes << 20;
1398 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001399 else if (unformat (a, "gbytes %lld", &mbytes))
1400 {
1401 bytes_to_send = mbytes << 30;
1402 }
Florin Corasb384b542018-01-15 01:08:33 -08001403 else if (unformat (a, "socket-name %s", &em->socket_name))
Florin Coras90a63982017-12-19 04:50:01 -08001404 ;
1405 else if (unformat (a, "use-svm-api"))
Florin Corasb384b542018-01-15 01:08:33 -08001406 em->use_sock_api = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001407 else
Florin Corase04c2992017-03-01 08:17:34 -08001408 {
1409 fformat (stderr, "%s: usage [master|slave]\n");
1410 exit (1);
1411 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001412 }
1413
Florin Corasb384b542018-01-15 01:08:33 -08001414 if (!em->socket_name)
1415 em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
Florin Coras90a63982017-12-19 04:50:01 -08001416
Florin Corase69f4952017-03-07 10:06:24 -08001417 if (uri)
1418 {
Florin Corasb384b542018-01-15 01:08:33 -08001419 em->uri = format (0, "%s%c", uri, 0);
1420 em->connect_uri = format (0, "%s%c", uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001421 }
1422 else
1423 {
Florin Corasb384b542018-01-15 01:08:33 -08001424 em->uri = format (0, "%s%c", bind_uri, 0);
1425 em->connect_uri = format (0, "%s%c", connect_uri, 0);
Florin Corase69f4952017-03-07 10:06:24 -08001426 }
1427
Florin Corasb384b542018-01-15 01:08:33 -08001428 em->i_am_master = i_am_master;
1429 em->segment_main = &svm_fifo_segment_main;
1430 em->drop_packets = drop_packets;
1431 em->test_return_packets = test_return_packets;
1432 em->bytes_to_send = bytes_to_send;
1433 em->time_to_stop = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001434
Florin Corase04c2992017-03-01 08:17:34 -08001435 setup_signal_handlers ();
Florin Corasb384b542018-01-15 01:08:33 -08001436 tcp_echo_api_hookup (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001437
Florin Corasb384b542018-01-15 01:08:33 -08001438 app_name = i_am_master ? "tcp_echo_server" : "tcp_echo_client";
1439 if (connect_to_vpp (app_name) < 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001440 {
1441 svm_region_exit ();
1442 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1443 exit (1);
1444 }
1445
1446 if (i_am_master == 0)
Florin Corasb384b542018-01-15 01:08:33 -08001447 client_run (em);
Florin Coras90a63982017-12-19 04:50:01 -08001448 else
Florin Corasb384b542018-01-15 01:08:33 -08001449 server_run (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001450
Florin Corasb384b542018-01-15 01:08:33 -08001451 disconnect_from_vpp (em);
Dave Barach68b0fb02017-02-28 15:15:56 -05001452 exit (0);
1453}
Florin Corase04c2992017-03-01 08:17:34 -08001454
1455/*
1456 * fd.io coding-style-patch-verification: ON
1457 *
1458 * Local Variables:
1459 * eval: (c-set-style "gnu")
1460 * End:
1461 */