blob: 7459d03202f848b0f7cfbceaa41bcda334aec5d3 [file] [log] [blame]
Florin Coras4399c2e2018-01-25 06:34:42 -08001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002* Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Coras4399c2e2018-01-25 06:34:42 -08003* 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 <vnet/vnet.h>
17#include <vlibmemory/api.h>
18#include <vnet/session/application.h>
19#include <vnet/session/application_interface.h>
Florin Coras54a51fd2019-02-07 15:34:52 -080020#include <vnet/session/session.h>
Florin Coras4399c2e2018-01-25 06:34:42 -080021
Aloys Augustin502785b2019-04-09 11:40:57 +020022#define ECHO_SERVER_DBG (0)
23#define DBG(_fmt, _args...) \
24 if (ECHO_SERVER_DBG) \
25 clib_warning (_fmt, ##_args)
26
Florin Coras4399c2e2018-01-25 06:34:42 -080027typedef struct
28{
29 /*
30 * Server app parameters
31 */
Florin Coras3c2fed52018-07-04 04:15:05 -070032 svm_msg_q_t **vpp_queue;
Florin Coras4399c2e2018-01-25 06:34:42 -080033 svm_queue_t *vl_input_queue; /**< Sever's event queue */
34
35 u32 app_index; /**< Server app index */
36 u32 my_client_index; /**< API client handle */
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070037 u32 node_index; /**< process node index for event scheduling */
Florin Coras4399c2e2018-01-25 06:34:42 -080038
39 /*
40 * Config params
41 */
42 u8 no_echo; /**< Don't echo traffic */
Florin Coras7fb0fe12018-04-09 09:24:52 -070043 u32 fifo_size; /**< Fifo size */
Florin Coras4399c2e2018-01-25 06:34:42 -080044 u32 rcv_buffer_size; /**< Rcv buffer size */
45 u32 prealloc_fifos; /**< Preallocate fifos */
46 u32 private_segment_count; /**< Number of private segments */
47 u32 private_segment_size; /**< Size of private segments */
48 char *server_uri; /**< Server URI */
Florin Coras58d36f02018-03-09 13:05:53 -080049 u32 tls_engine; /**< TLS engine: mbedtls/openssl */
Florin Coras7fb0fe12018-04-09 09:24:52 -070050 u8 is_dgram; /**< set if transport is dgram */
Florin Coras4399c2e2018-01-25 06:34:42 -080051 /*
52 * Test state
53 */
54 u8 **rx_buf; /**< Per-thread RX buffer */
55 u64 byte_index;
56 u32 **rx_retries;
Aloys Augustin502785b2019-04-09 11:40:57 +020057 u8 transport_proto;
Florin Coras4399c2e2018-01-25 06:34:42 -080058
59 vlib_main_t *vlib_main;
60} echo_server_main_t;
61
62echo_server_main_t echo_server_main;
63
64int
Aloys Augustin502785b2019-04-09 11:40:57 +020065quic_echo_server_qsession_accept_callback (session_t * s)
66{
67 DBG ("QSession %u accept w/opaque %d", s->session_index, s->opaque);
68 return 0;
69}
70
71int
72quic_echo_server_session_accept_callback (session_t * s)
73{
74 echo_server_main_t *esm = &echo_server_main;
75 if (!(s->flags & SESSION_F_QUIC_STREAM))
76 return quic_echo_server_qsession_accept_callback (s);
77 DBG ("SSESSION %u accept w/opaque %d", s->session_index, s->opaque);
78
79 esm->vpp_queue[s->thread_index] =
80 session_main_get_vpp_event_queue (s->thread_index);
81 s->session_state = SESSION_STATE_READY;
82 esm->byte_index = 0;
83 ASSERT (vec_len (esm->rx_retries) > s->thread_index);
84 vec_validate (esm->rx_retries[s->thread_index], s->session_index);
85 esm->rx_retries[s->thread_index][s->session_index] = 0;
86 return 0;
87}
88
89int
Florin Coras288eaab2019-02-03 15:26:14 -080090echo_server_session_accept_callback (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -080091{
92 echo_server_main_t *esm = &echo_server_main;
Florin Coras4399c2e2018-01-25 06:34:42 -080093 esm->vpp_queue[s->thread_index] =
Florin Coras31c99552019-03-01 13:00:58 -080094 session_main_get_vpp_event_queue (s->thread_index);
Florin Coras4399c2e2018-01-25 06:34:42 -080095 s->session_state = SESSION_STATE_READY;
96 esm->byte_index = 0;
Florin Coras6f1c48d2018-05-02 14:34:32 -070097 ASSERT (vec_len (esm->rx_retries) > s->thread_index);
98 vec_validate (esm->rx_retries[s->thread_index], s->session_index);
Florin Coras4399c2e2018-01-25 06:34:42 -080099 esm->rx_retries[s->thread_index][s->session_index] = 0;
100 return 0;
101}
102
103void
Florin Coras288eaab2019-02-03 15:26:14 -0800104echo_server_session_disconnect_callback (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800105{
106 echo_server_main_t *esm = &echo_server_main;
Florin Coras4af830c2018-12-04 09:21:36 -0800107 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras4399c2e2018-01-25 06:34:42 -0800108
109 a->handle = session_handle (s);
110 a->app_index = esm->app_index;
111 vnet_disconnect_session (a);
112}
113
114void
Florin Coras288eaab2019-02-03 15:26:14 -0800115echo_server_session_reset_callback (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800116{
Florin Coras4af830c2018-12-04 09:21:36 -0800117 echo_server_main_t *esm = &echo_server_main;
118 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras31c99552019-03-01 13:00:58 -0800119 clib_warning ("Reset session %U", format_session, s, 2);
Florin Coras4af830c2018-12-04 09:21:36 -0800120 a->handle = session_handle (s);
121 a->app_index = esm->app_index;
122 vnet_disconnect_session (a);
Florin Coras4399c2e2018-01-25 06:34:42 -0800123}
124
125int
126echo_server_session_connected_callback (u32 app_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800127 session_t * s, u8 is_fail)
Florin Coras4399c2e2018-01-25 06:34:42 -0800128{
129 clib_warning ("called...");
130 return -1;
131}
132
133int
Florin Corasfa76a762018-11-29 12:40:10 -0800134echo_server_add_segment_callback (u32 client_index, u64 segment_handle)
Florin Coras4399c2e2018-01-25 06:34:42 -0800135{
Florin Corasa332c462018-01-31 06:52:17 -0800136 /* New heaps may be added */
137 return 0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800138}
139
140int
141echo_server_redirect_connect_callback (u32 client_index, void *mp)
142{
143 clib_warning ("called...");
144 return -1;
145}
146
147void
148test_bytes (echo_server_main_t * esm, int actual_transfer)
149{
150 int i;
151 u32 my_thread_id = vlib_get_thread_index ();
152
153 for (i = 0; i < actual_transfer; i++)
154 {
155 if (esm->rx_buf[my_thread_id][i] != ((esm->byte_index + i) & 0xff))
156 {
157 clib_warning ("at %lld expected %d got %d", esm->byte_index + i,
158 (esm->byte_index + i) & 0xff,
159 esm->rx_buf[my_thread_id][i]);
160 }
161 }
162 esm->byte_index += actual_transfer;
163}
164
165/*
Florin Coras7fb0fe12018-04-09 09:24:52 -0700166 * If no-echo, just drop the data and be done with it.
Florin Coras4399c2e2018-01-25 06:34:42 -0800167 */
168int
Florin Coras288eaab2019-02-03 15:26:14 -0800169echo_server_builtin_server_rx_callback_no_echo (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800170{
Florin Coras288eaab2019-02-03 15:26:14 -0800171 svm_fifo_t *rx_fifo = s->rx_fifo;
Sirshak Das28aa5392019-02-05 01:33:33 -0600172 svm_fifo_dequeue_drop (rx_fifo, svm_fifo_max_dequeue_cons (rx_fifo));
Florin Coras4399c2e2018-01-25 06:34:42 -0800173 return 0;
174}
175
176int
Florin Coras288eaab2019-02-03 15:26:14 -0800177echo_server_rx_callback (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800178{
179 u32 n_written, max_dequeue, max_enqueue, max_transfer;
180 int actual_transfer;
181 svm_fifo_t *tx_fifo, *rx_fifo;
182 echo_server_main_t *esm = &echo_server_main;
Florin Coras4399c2e2018-01-25 06:34:42 -0800183 u32 thread_index = vlib_get_thread_index ();
Florin Coras7fb0fe12018-04-09 09:24:52 -0700184 app_session_transport_t at;
Florin Coras4399c2e2018-01-25 06:34:42 -0800185
186 ASSERT (s->thread_index == thread_index);
187
Florin Coras288eaab2019-02-03 15:26:14 -0800188 rx_fifo = s->rx_fifo;
189 tx_fifo = s->tx_fifo;
Florin Coras4399c2e2018-01-25 06:34:42 -0800190
191 ASSERT (rx_fifo->master_thread_index == thread_index);
192 ASSERT (tx_fifo->master_thread_index == thread_index);
193
Sirshak Das28aa5392019-02-05 01:33:33 -0600194 max_enqueue = svm_fifo_max_enqueue_prod (tx_fifo);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700195 if (!esm->is_dgram)
196 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600197 max_dequeue = svm_fifo_max_dequeue_cons (rx_fifo);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700198 }
199 else
200 {
201 session_dgram_pre_hdr_t ph;
202 svm_fifo_peek (rx_fifo, 0, sizeof (ph), (u8 *) & ph);
203 max_dequeue = ph.data_length - ph.data_offset;
204 if (!esm->vpp_queue[s->thread_index])
205 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700206 svm_msg_q_t *mq;
Florin Coras31c99552019-03-01 13:00:58 -0800207 mq = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700208 esm->vpp_queue[s->thread_index] = mq;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700209 }
210 max_enqueue -= sizeof (session_dgram_hdr_t);
211 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800212
213 if (PREDICT_FALSE (max_dequeue == 0))
214 return 0;
215
216 /* Number of bytes we're going to copy */
Florin Coras7fb0fe12018-04-09 09:24:52 -0700217 max_transfer = clib_min (max_dequeue, max_enqueue);
Florin Coras4399c2e2018-01-25 06:34:42 -0800218
219 /* No space in tx fifo */
220 if (PREDICT_FALSE (max_transfer == 0))
221 {
222 /* XXX timeout for session that are stuck */
223
224 rx_event:
225 /* Program self-tap to retry */
226 if (svm_fifo_set_event (rx_fifo))
227 {
Florin Corasf6c43132019-03-01 12:41:21 -0800228 if (session_send_io_evt_to_thread (rx_fifo,
229 SESSION_IO_EVT_BUILTIN_RX))
Florin Coras4399c2e2018-01-25 06:34:42 -0800230 clib_warning ("failed to enqueue self-tap");
231
Florin Coras7fb0fe12018-04-09 09:24:52 -0700232 vec_validate (esm->rx_retries[s->thread_index], s->session_index);
Florin Coras4399c2e2018-01-25 06:34:42 -0800233 if (esm->rx_retries[thread_index][s->session_index] == 500000)
234 {
Florin Coras31c99552019-03-01 13:00:58 -0800235 clib_warning ("session stuck: %U", format_session, s, 2);
Florin Coras4399c2e2018-01-25 06:34:42 -0800236 }
237 if (esm->rx_retries[thread_index][s->session_index] < 500001)
238 esm->rx_retries[thread_index][s->session_index]++;
239 }
240
241 return 0;
242 }
243
Florin Coras7fb0fe12018-04-09 09:24:52 -0700244 vec_validate (esm->rx_buf[thread_index], max_transfer);
245 if (!esm->is_dgram)
246 {
247 actual_transfer = app_recv_stream_raw (rx_fifo,
248 esm->rx_buf[thread_index],
249 max_transfer,
Florin Coras460dce62018-07-27 05:45:06 -0700250 0 /* don't clear event */ ,
251 0 /* peek */ );
Florin Coras7fb0fe12018-04-09 09:24:52 -0700252 }
253 else
254 {
255 actual_transfer = app_recv_dgram_raw (rx_fifo,
256 esm->rx_buf[thread_index],
257 max_transfer, &at,
Florin Coras460dce62018-07-27 05:45:06 -0700258 0 /* don't clear event */ ,
259 0 /* peek */ );
Florin Coras7fb0fe12018-04-09 09:24:52 -0700260 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800261 ASSERT (actual_transfer == max_transfer);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700262 /* test_bytes (esm, actual_transfer); */
Florin Coras4399c2e2018-01-25 06:34:42 -0800263
264 /*
265 * Echo back
266 */
267
Florin Coras7fb0fe12018-04-09 09:24:52 -0700268 if (!esm->is_dgram)
Florin Coras4399c2e2018-01-25 06:34:42 -0800269 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700270 n_written = app_send_stream_raw (tx_fifo,
271 esm->vpp_queue[thread_index],
272 esm->rx_buf[thread_index],
Florin Coras653e43f2019-03-04 10:56:23 -0800273 actual_transfer, SESSION_IO_EVT_TX,
274 1 /* do_evt */ , 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700275 }
276 else
277 {
278 n_written = app_send_dgram_raw (tx_fifo, &at,
279 esm->vpp_queue[s->thread_index],
280 esm->rx_buf[thread_index],
Florin Coras653e43f2019-03-04 10:56:23 -0800281 actual_transfer, SESSION_IO_EVT_TX,
282 1 /* do_evt */ , 0);
Florin Coras4399c2e2018-01-25 06:34:42 -0800283 }
284
Florin Coras7fb0fe12018-04-09 09:24:52 -0700285 if (n_written != max_transfer)
286 clib_warning ("short trout! written %u read %u", n_written, max_transfer);
287
Sirshak Das28aa5392019-02-05 01:33:33 -0600288 if (PREDICT_FALSE (svm_fifo_max_dequeue_cons (rx_fifo)))
Florin Coras4399c2e2018-01-25 06:34:42 -0800289 goto rx_event;
290
291 return 0;
292}
293
294static session_cb_vft_t echo_server_session_cb_vft = {
295 .session_accept_callback = echo_server_session_accept_callback,
296 .session_disconnect_callback = echo_server_session_disconnect_callback,
297 .session_connected_callback = echo_server_session_connected_callback,
298 .add_segment_callback = echo_server_add_segment_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800299 .builtin_app_rx_callback = echo_server_rx_callback,
Florin Coras4399c2e2018-01-25 06:34:42 -0800300 .session_reset_callback = echo_server_session_reset_callback
301};
302
303/* Abuse VPP's input queue */
304static int
305create_api_loopback (vlib_main_t * vm)
306{
307 echo_server_main_t *esm = &echo_server_main;
308 api_main_t *am = &api_main;
309 vl_shmem_hdr_t *shmem_hdr;
310
311 shmem_hdr = am->shmem_hdr;
312 esm->vl_input_queue = shmem_hdr->vl_input_queue;
313 esm->my_client_index = vl_api_memclnt_create_internal ("echo_server",
314 esm->vl_input_queue);
315 return 0;
316}
317
318static int
319echo_server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
320{
Florin Coras371ca502018-02-21 12:07:41 -0800321 vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
322 vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
Florin Coras4399c2e2018-01-25 06:34:42 -0800323 echo_server_main_t *esm = &echo_server_main;
Florin Coras4399c2e2018-01-25 06:34:42 -0800324 vnet_app_attach_args_t _a, *a = &_a;
Florin Coras371ca502018-02-21 12:07:41 -0800325 u64 options[APP_OPTIONS_N_OPTIONS];
Florin Coras4399c2e2018-01-25 06:34:42 -0800326 u32 segment_size = 512 << 20;
327
Dave Barachb7b92992018-10-17 10:38:51 -0400328 clib_memset (a, 0, sizeof (*a));
329 clib_memset (options, 0, sizeof (options));
Florin Coras4399c2e2018-01-25 06:34:42 -0800330
331 if (esm->no_echo)
Florin Coras371ca502018-02-21 12:07:41 -0800332 echo_server_session_cb_vft.builtin_app_rx_callback =
Florin Coras4399c2e2018-01-25 06:34:42 -0800333 echo_server_builtin_server_rx_callback_no_echo;
334 else
Florin Coras371ca502018-02-21 12:07:41 -0800335 echo_server_session_cb_vft.builtin_app_rx_callback =
Florin Coras4399c2e2018-01-25 06:34:42 -0800336 echo_server_rx_callback;
Aloys Augustin502785b2019-04-09 11:40:57 +0200337 if (esm->transport_proto == TRANSPORT_PROTO_QUIC)
338 echo_server_session_cb_vft.session_accept_callback =
339 quic_echo_server_session_accept_callback;
Florin Coras4399c2e2018-01-25 06:34:42 -0800340
341 if (esm->private_segment_size)
342 segment_size = esm->private_segment_size;
343
344 a->api_client_index = esm->my_client_index;
345 a->session_cb_vft = &echo_server_session_cb_vft;
346 a->options = options;
347 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Corasa332c462018-01-31 06:52:17 -0800348 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800349 a->options[APP_OPTIONS_RX_FIFO_SIZE] = esm->fifo_size;
350 a->options[APP_OPTIONS_TX_FIFO_SIZE] = esm->fifo_size;
351 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = esm->private_segment_count;
Florin Coras58d36f02018-03-09 13:05:53 -0800352 a->options[APP_OPTIONS_TLS_ENGINE] = esm->tls_engine;
Florin Coras4399c2e2018-01-25 06:34:42 -0800353 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
354 esm->prealloc_fifos ? esm->prealloc_fifos : 1;
355
356 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
357 if (appns_id)
358 {
359 a->namespace_id = appns_id;
360 a->options[APP_OPTIONS_FLAGS] |= appns_flags;
361 a->options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
362 }
363
364 if (vnet_application_attach (a))
365 {
366 clib_warning ("failed to attach server");
367 return -1;
368 }
369 esm->app_index = a->app_index;
Florin Coras371ca502018-02-21 12:07:41 -0800370
Dave Barachb7b92992018-10-17 10:38:51 -0400371 clib_memset (a_cert, 0, sizeof (*a_cert));
Florin Coras371ca502018-02-21 12:07:41 -0800372 a_cert->app_index = a->app_index;
373 vec_validate (a_cert->cert, test_srv_crt_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500374 clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
Florin Coras371ca502018-02-21 12:07:41 -0800375 vnet_app_add_tls_cert (a_cert);
376
Dave Barachb7b92992018-10-17 10:38:51 -0400377 clib_memset (a_key, 0, sizeof (*a_key));
Florin Coras371ca502018-02-21 12:07:41 -0800378 a_key->app_index = a->app_index;
379 vec_validate (a_key->key, test_srv_key_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500380 clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
Florin Coras371ca502018-02-21 12:07:41 -0800381 vnet_app_add_tls_key (a_key);
Florin Coras4399c2e2018-01-25 06:34:42 -0800382 return 0;
383}
384
385static int
386echo_server_detach (void)
387{
388 echo_server_main_t *esm = &echo_server_main;
389 vnet_app_detach_args_t _da, *da = &_da;
390 int rv;
391
392 da->app_index = esm->app_index;
393 rv = vnet_application_detach (da);
394 esm->app_index = ~0;
395 return rv;
396}
397
398static int
399echo_server_listen ()
400{
401 echo_server_main_t *esm = &echo_server_main;
Florin Corasc9940fc2019-02-05 20:55:11 -0800402 vnet_listen_args_t _a, *a = &_a;
Dave Barachb7b92992018-10-17 10:38:51 -0400403 clib_memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800404 a->app_index = esm->app_index;
405 a->uri = esm->server_uri;
406 return vnet_bind_uri (a);
407}
408
409static int
410echo_server_create (vlib_main_t * vm, u8 * appns_id, u64 appns_flags,
411 u64 appns_secret)
412{
413 echo_server_main_t *esm = &echo_server_main;
414 vlib_thread_main_t *vtm = vlib_get_thread_main ();
415 u32 num_threads;
416 int i;
417
418 if (esm->my_client_index == (u32) ~ 0)
419 {
420 if (create_api_loopback (vm))
421 {
422 clib_warning ("failed to create api loopback");
423 return -1;
424 }
425 }
426
427 num_threads = 1 /* main thread */ + vtm->n_threads;
428 vec_validate (echo_server_main.vpp_queue, num_threads - 1);
429 vec_validate (esm->rx_buf, num_threads - 1);
430 vec_validate (esm->rx_retries, num_threads - 1);
Florin Corasc1a448b2018-04-20 10:51:49 -0700431 for (i = 0; i < vec_len (esm->rx_retries); i++)
432 vec_validate (esm->rx_retries[i],
Florin Coras31c99552019-03-01 13:00:58 -0800433 pool_elts (session_main.wrk[i].sessions));
Florin Coras81a13db2018-03-16 08:48:31 -0700434 esm->rcv_buffer_size = clib_max (esm->rcv_buffer_size, esm->fifo_size);
Florin Coras4399c2e2018-01-25 06:34:42 -0800435 for (i = 0; i < num_threads; i++)
436 vec_validate (esm->rx_buf[i], esm->rcv_buffer_size);
437
438 if (echo_server_attach (appns_id, appns_flags, appns_secret))
439 {
440 clib_warning ("failed to attach server");
441 return -1;
442 }
443 if (echo_server_listen ())
444 {
445 clib_warning ("failed to start listening");
446 if (echo_server_detach ())
447 clib_warning ("failed to detach");
448 return -1;
449 }
450 return 0;
451}
452
453static clib_error_t *
454echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
455 vlib_cli_command_t * cmd)
456{
457 echo_server_main_t *esm = &echo_server_main;
458 u8 server_uri_set = 0, *appns_id = 0;
459 u64 tmp, appns_flags = 0, appns_secret = 0;
460 char *default_uri = "tcp://0.0.0.0/1234";
Florin Corasf8f516a2018-02-08 15:10:09 -0800461 int rv, is_stop = 0;
Aloys Augustin502785b2019-04-09 11:40:57 +0200462 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
Florin Coras4399c2e2018-01-25 06:34:42 -0800463
464 esm->no_echo = 0;
465 esm->fifo_size = 64 << 10;
466 esm->rcv_buffer_size = 128 << 10;
467 esm->prealloc_fifos = 0;
468 esm->private_segment_count = 0;
469 esm->private_segment_size = 0;
Florin Coras58d36f02018-03-09 13:05:53 -0800470 esm->tls_engine = TLS_ENGINE_OPENSSL;
Florin Coras4399c2e2018-01-25 06:34:42 -0800471 vec_free (esm->server_uri);
472
473 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
474 {
475 if (unformat (input, "uri %s", &esm->server_uri))
476 server_uri_set = 1;
477 else if (unformat (input, "no-echo"))
478 esm->no_echo = 1;
479 else if (unformat (input, "fifo-size %d", &esm->fifo_size))
480 esm->fifo_size <<= 10;
481 else if (unformat (input, "rcv-buf-size %d", &esm->rcv_buffer_size))
482 ;
483 else if (unformat (input, "prealloc-fifos %d", &esm->prealloc_fifos))
484 ;
485 else if (unformat (input, "private-segment-count %d",
486 &esm->private_segment_count))
487 ;
488 else if (unformat (input, "private-segment-size %U",
489 unformat_memory_size, &tmp))
490 {
491 if (tmp >= 0x100000000ULL)
492 return clib_error_return
493 (0, "private segment size %lld (%llu) too large", tmp, tmp);
494 esm->private_segment_size = tmp;
495 }
496 else if (unformat (input, "appns %_%v%_", &appns_id))
497 ;
498 else if (unformat (input, "all-scope"))
499 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
500 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
501 else if (unformat (input, "local-scope"))
502 appns_flags |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
503 else if (unformat (input, "global-scope"))
504 appns_flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
505 else if (unformat (input, "secret %lu", &appns_secret))
506 ;
Florin Corasf8f516a2018-02-08 15:10:09 -0800507 else if (unformat (input, "stop"))
508 is_stop = 1;
Florin Coras58d36f02018-03-09 13:05:53 -0800509 else if (unformat (input, "tls-engine %d", &esm->tls_engine))
510 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800511 else
512 return clib_error_return (0, "failed: unknown input `%U'",
513 format_unformat_error, input);
514 }
515
Florin Corasf8f516a2018-02-08 15:10:09 -0800516 if (is_stop)
517 {
518 if (esm->app_index == (u32) ~ 0)
519 {
520 clib_warning ("server not running");
521 return clib_error_return (0, "failed: server not running");
522 }
523 rv = echo_server_detach ();
524 if (rv)
525 {
526 clib_warning ("failed: detach");
527 return clib_error_return (0, "failed: server detach %d", rv);
528 }
529 return 0;
530 }
531
Florin Coras4399c2e2018-01-25 06:34:42 -0800532 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
533
534 if (!server_uri_set)
535 {
536 clib_warning ("No uri provided! Using default: %s", default_uri);
537 esm->server_uri = (char *) format (0, "%s%c", default_uri, 0);
538 }
Aloys Augustin502785b2019-04-09 11:40:57 +0200539
540 if ((rv = parse_uri ((char *) esm->server_uri, &sep)))
541 return clib_error_return (0, "Uri parse error: %d", rv);
542 esm->transport_proto = sep.transport_proto;
543 esm->is_dgram = (sep.transport_proto == TRANSPORT_PROTO_UDP);
Florin Coras4399c2e2018-01-25 06:34:42 -0800544
545 rv = echo_server_create (vm, appns_id, appns_flags, appns_secret);
546 vec_free (appns_id);
547 if (rv)
548 {
549 vec_free (esm->server_uri);
550 return clib_error_return (0, "failed: server_create returned %d", rv);
551 }
552
553 return 0;
554}
555
556/* *INDENT-OFF* */
557VLIB_CLI_COMMAND (echo_server_create_command, static) =
558{
559 .path = "test echo server",
560 .short_help = "test echo server proto <proto> [no echo][fifo-size <mbytes>]"
561 "[rcv-buf-size <bytes>][prealloc-fifos <count>]"
562 "[private-segment-count <count>][private-segment-size <bytes[m|g]>]"
563 "[uri <tcp://ip/port>]",
564 .function = echo_server_create_command_fn,
565};
566/* *INDENT-ON* */
567
568clib_error_t *
569echo_server_main_init (vlib_main_t * vm)
570{
571 echo_server_main_t *esm = &echo_server_main;
572 esm->my_client_index = ~0;
573 return 0;
574}
575
576VLIB_INIT_FUNCTION (echo_server_main_init);
577
578/*
579* fd.io coding-style-patch-verification: ON
580*
581* Local Variables:
582* eval: (c-set-style "gnu")
583* End:
584*/