2 * Copyright (c) 2017-2019 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/application_interface.h>
20 #include <vnet/session/session.h>
22 #define ECHO_SERVER_DBG (0)
23 #define DBG(_fmt, _args...) \
24 if (ECHO_SERVER_DBG) \
25 clib_warning (_fmt, ##_args)
30 * Server app parameters
32 svm_msg_q_t **vpp_queue;
33 svm_queue_t *vl_input_queue; /**< Sever's event queue */
35 u32 app_index; /**< Server app index */
36 u32 my_client_index; /**< API client handle */
37 u32 node_index; /**< process node index for event scheduling */
42 u8 no_echo; /**< Don't echo traffic */
43 u32 fifo_size; /**< Fifo size */
44 u32 rcv_buffer_size; /**< Rcv buffer size */
45 u32 prealloc_fifos; /**< Preallocate fifos */
46 u32 private_segment_count; /**< Number of private segments */
47 u64 private_segment_size; /**< Size of private segments */
48 char *server_uri; /**< Server URI */
49 u32 tls_engine; /**< TLS engine: mbedtls/openssl */
50 u32 ckpair_index; /**< Cert and key for tls/quic */
51 u8 is_dgram; /**< set if transport is dgram */
56 u8 **rx_buf; /**< Per-thread RX buffer */
60 u64 listener_handle; /**< Session handle of the root listener */
62 vlib_main_t *vlib_main;
65 echo_server_main_t echo_server_main;
68 quic_echo_server_qsession_accept_callback (session_t * s)
70 DBG ("QSession %u accept w/opaque %d", s->session_index, s->opaque);
75 quic_echo_server_session_accept_callback (session_t * s)
77 echo_server_main_t *esm = &echo_server_main;
78 if (s->listener_handle == esm->listener_handle)
79 return quic_echo_server_qsession_accept_callback (s);
80 DBG ("SSESSION %u accept w/opaque %d", s->session_index, s->opaque);
82 esm->vpp_queue[s->thread_index] =
83 session_main_get_vpp_event_queue (s->thread_index);
84 s->session_state = SESSION_STATE_READY;
86 ASSERT (vec_len (esm->rx_retries) > s->thread_index);
87 vec_validate (esm->rx_retries[s->thread_index], s->session_index);
88 esm->rx_retries[s->thread_index][s->session_index] = 0;
93 echo_server_session_accept_callback (session_t * s)
95 echo_server_main_t *esm = &echo_server_main;
96 esm->vpp_queue[s->thread_index] =
97 session_main_get_vpp_event_queue (s->thread_index);
98 s->session_state = SESSION_STATE_READY;
100 ASSERT (vec_len (esm->rx_retries) > s->thread_index);
101 vec_validate (esm->rx_retries[s->thread_index], s->session_index);
102 esm->rx_retries[s->thread_index][s->session_index] = 0;
107 echo_server_session_disconnect_callback (session_t * s)
109 echo_server_main_t *esm = &echo_server_main;
110 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
112 a->handle = session_handle (s);
113 a->app_index = esm->app_index;
114 vnet_disconnect_session (a);
118 echo_server_session_reset_callback (session_t * s)
120 echo_server_main_t *esm = &echo_server_main;
121 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
122 clib_warning ("Reset session %U", format_session, s, 2);
123 a->handle = session_handle (s);
124 a->app_index = esm->app_index;
125 vnet_disconnect_session (a);
129 echo_server_session_connected_callback (u32 app_index, u32 api_context,
130 session_t * s, session_error_t err)
132 clib_warning ("called...");
137 echo_server_add_segment_callback (u32 client_index, u64 segment_handle)
139 /* New heaps may be added */
144 echo_server_redirect_connect_callback (u32 client_index, void *mp)
146 clib_warning ("called...");
151 test_bytes (echo_server_main_t * esm, int actual_transfer)
154 u32 my_thread_id = vlib_get_thread_index ();
156 for (i = 0; i < actual_transfer; i++)
158 if (esm->rx_buf[my_thread_id][i] != ((esm->byte_index + i) & 0xff))
160 clib_warning ("at %lld expected %d got %d", esm->byte_index + i,
161 (esm->byte_index + i) & 0xff,
162 esm->rx_buf[my_thread_id][i]);
165 esm->byte_index += actual_transfer;
169 * If no-echo, just drop the data and be done with it.
172 echo_server_builtin_server_rx_callback_no_echo (session_t * s)
174 svm_fifo_t *rx_fifo = s->rx_fifo;
175 svm_fifo_dequeue_drop (rx_fifo, svm_fifo_max_dequeue_cons (rx_fifo));
180 echo_server_rx_callback (session_t * s)
182 u32 n_written, max_dequeue, max_enqueue, max_transfer;
184 svm_fifo_t *tx_fifo, *rx_fifo;
185 echo_server_main_t *esm = &echo_server_main;
186 u32 thread_index = vlib_get_thread_index ();
187 app_session_transport_t at;
189 ASSERT (s->thread_index == thread_index);
191 rx_fifo = s->rx_fifo;
192 tx_fifo = s->tx_fifo;
194 ASSERT (rx_fifo->master_thread_index == thread_index);
195 ASSERT (tx_fifo->master_thread_index == thread_index);
197 max_enqueue = svm_fifo_max_enqueue_prod (tx_fifo);
200 max_dequeue = svm_fifo_max_dequeue_cons (rx_fifo);
204 session_dgram_pre_hdr_t ph;
205 svm_fifo_peek (rx_fifo, 0, sizeof (ph), (u8 *) & ph);
206 max_dequeue = ph.data_length - ph.data_offset;
207 if (!esm->vpp_queue[s->thread_index])
210 mq = session_main_get_vpp_event_queue (s->thread_index);
211 esm->vpp_queue[s->thread_index] = mq;
213 max_enqueue -= sizeof (session_dgram_hdr_t);
216 if (PREDICT_FALSE (max_dequeue == 0))
219 /* Number of bytes we're going to copy */
220 max_transfer = clib_min (max_dequeue, max_enqueue);
222 /* No space in tx fifo */
223 if (PREDICT_FALSE (max_transfer == 0))
225 /* XXX timeout for session that are stuck */
228 /* Program self-tap to retry */
229 if (svm_fifo_set_event (rx_fifo))
231 /* TODO should be session_enqueue_notify(s) but quic tests seem
232 * to fail if that's the case */
233 if (session_send_io_evt_to_thread (rx_fifo,
234 SESSION_IO_EVT_BUILTIN_RX))
235 clib_warning ("failed to enqueue self-tap");
237 vec_validate (esm->rx_retries[s->thread_index], s->session_index);
238 if (esm->rx_retries[thread_index][s->session_index] == 500000)
240 clib_warning ("session stuck: %U", format_session, s, 2);
242 if (esm->rx_retries[thread_index][s->session_index] < 500001)
243 esm->rx_retries[thread_index][s->session_index]++;
249 vec_validate (esm->rx_buf[thread_index], max_transfer);
252 actual_transfer = app_recv_stream_raw (rx_fifo,
253 esm->rx_buf[thread_index],
255 0 /* don't clear event */ ,
260 actual_transfer = app_recv_dgram_raw (rx_fifo,
261 esm->rx_buf[thread_index],
263 0 /* don't clear event */ ,
266 ASSERT (actual_transfer == max_transfer);
267 /* test_bytes (esm, actual_transfer); */
275 n_written = app_send_stream_raw (tx_fifo,
276 esm->vpp_queue[thread_index],
277 esm->rx_buf[thread_index],
278 actual_transfer, SESSION_IO_EVT_TX,
283 n_written = app_send_dgram_raw (tx_fifo, &at,
284 esm->vpp_queue[s->thread_index],
285 esm->rx_buf[thread_index],
286 actual_transfer, SESSION_IO_EVT_TX,
290 if (n_written != max_transfer)
291 clib_warning ("short trout! written %u read %u", n_written, max_transfer);
293 if (PREDICT_FALSE (svm_fifo_max_dequeue_cons (rx_fifo)))
299 static session_cb_vft_t echo_server_session_cb_vft = {
300 .session_accept_callback = echo_server_session_accept_callback,
301 .session_disconnect_callback = echo_server_session_disconnect_callback,
302 .session_connected_callback = echo_server_session_connected_callback,
303 .add_segment_callback = echo_server_add_segment_callback,
304 .builtin_app_rx_callback = echo_server_rx_callback,
305 .session_reset_callback = echo_server_session_reset_callback
309 echo_server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
311 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
312 echo_server_main_t *esm = &echo_server_main;
313 vnet_app_attach_args_t _a, *a = &_a;
314 u64 options[APP_OPTIONS_N_OPTIONS];
316 clib_memset (a, 0, sizeof (*a));
317 clib_memset (options, 0, sizeof (options));
320 echo_server_session_cb_vft.builtin_app_rx_callback =
321 echo_server_builtin_server_rx_callback_no_echo;
323 echo_server_session_cb_vft.builtin_app_rx_callback =
324 echo_server_rx_callback;
325 if (esm->transport_proto == TRANSPORT_PROTO_QUIC)
326 echo_server_session_cb_vft.session_accept_callback =
327 quic_echo_server_session_accept_callback;
329 a->api_client_index = ~0;
330 a->name = format (0, "echo_server");
331 a->session_cb_vft = &echo_server_session_cb_vft;
332 a->options = options;
333 a->options[APP_OPTIONS_SEGMENT_SIZE] = esm->private_segment_size;
334 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = esm->private_segment_size;
335 a->options[APP_OPTIONS_RX_FIFO_SIZE] = esm->fifo_size;
336 a->options[APP_OPTIONS_TX_FIFO_SIZE] = esm->fifo_size;
337 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = esm->private_segment_count;
338 a->options[APP_OPTIONS_TLS_ENGINE] = esm->tls_engine;
339 a->options[APP_OPTIONS_PCT_FIRST_ALLOC] = 100;
340 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
341 esm->prealloc_fifos ? esm->prealloc_fifos : 1;
343 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
346 a->namespace_id = appns_id;
347 a->options[APP_OPTIONS_FLAGS] |= appns_flags;
348 a->options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
351 if (vnet_application_attach (a))
353 clib_warning ("failed to attach server");
356 esm->app_index = a->app_index;
359 clib_memset (ck_pair, 0, sizeof (*ck_pair));
360 ck_pair->cert = (u8 *) test_srv_crt_rsa;
361 ck_pair->key = (u8 *) test_srv_key_rsa;
362 ck_pair->cert_len = test_srv_crt_rsa_len;
363 ck_pair->key_len = test_srv_key_rsa_len;
364 vnet_app_add_cert_key_pair (ck_pair);
365 esm->ckpair_index = ck_pair->index;
371 echo_server_detach (void)
373 echo_server_main_t *esm = &echo_server_main;
374 vnet_app_detach_args_t _da, *da = &_da;
377 da->app_index = esm->app_index;
378 da->api_client_index = ~0;
379 rv = vnet_application_detach (da);
381 vnet_app_del_cert_key_pair (esm->ckpair_index);
386 echo_client_transport_needs_crypto (transport_proto_t proto)
388 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
389 proto == TRANSPORT_PROTO_QUIC;
393 echo_server_listen ()
396 echo_server_main_t *esm = &echo_server_main;
397 vnet_listen_args_t _args = { 0 }, *args = &_args;
399 args->sep_ext.app_wrk_index = 0;
401 if ((rv = parse_uri (esm->server_uri, &args->sep_ext)))
405 args->app_index = esm->app_index;
406 if (echo_client_transport_needs_crypto (args->sep_ext.transport_proto))
408 session_endpoint_alloc_ext_cfg (&args->sep_ext,
409 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
410 args->sep_ext.ext_cfg->crypto.ckpair_index = esm->ckpair_index;
413 if (args->sep_ext.transport_proto == TRANSPORT_PROTO_UDP)
415 args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
418 rv = vnet_listen (args);
419 esm->listener_handle = args->handle;
420 if (args->sep_ext.ext_cfg)
421 clib_mem_free (args->sep_ext.ext_cfg);
426 echo_server_create (vlib_main_t * vm, u8 * appns_id, u64 appns_flags,
429 echo_server_main_t *esm = &echo_server_main;
430 vlib_thread_main_t *vtm = vlib_get_thread_main ();
434 num_threads = 1 /* main thread */ + vtm->n_threads;
435 vec_validate (echo_server_main.vpp_queue, num_threads - 1);
436 vec_validate (esm->rx_buf, num_threads - 1);
437 vec_validate (esm->rx_retries, num_threads - 1);
438 for (i = 0; i < vec_len (esm->rx_retries); i++)
439 vec_validate (esm->rx_retries[i],
440 pool_elts (session_main.wrk[i].sessions));
441 esm->rcv_buffer_size = clib_max (esm->rcv_buffer_size, esm->fifo_size);
442 for (i = 0; i < num_threads; i++)
443 vec_validate (esm->rx_buf[i], esm->rcv_buffer_size);
445 if (echo_server_attach (appns_id, appns_flags, appns_secret))
447 clib_warning ("failed to attach server");
450 if (echo_server_listen ())
452 clib_warning ("failed to start listening");
453 if (echo_server_detach ())
454 clib_warning ("failed to detach");
460 static clib_error_t *
461 echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
462 vlib_cli_command_t * cmd)
464 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
465 echo_server_main_t *esm = &echo_server_main;
466 u8 server_uri_set = 0, *appns_id = 0;
467 u64 appns_flags = 0, appns_secret = 0;
468 char *default_uri = "tcp://0.0.0.0/1234";
470 clib_error_t *error = 0;
473 esm->fifo_size = 64 << 10;
474 esm->rcv_buffer_size = 128 << 10;
475 esm->prealloc_fifos = 0;
476 esm->private_segment_count = 0;
477 esm->private_segment_size = 512 << 20;
478 esm->tls_engine = CRYPTO_ENGINE_OPENSSL;
479 vec_free (esm->server_uri);
481 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
483 if (unformat (input, "uri %s", &esm->server_uri))
485 else if (unformat (input, "no-echo"))
487 else if (unformat (input, "fifo-size %d", &esm->fifo_size))
488 esm->fifo_size <<= 10;
489 else if (unformat (input, "rcv-buf-size %d", &esm->rcv_buffer_size))
491 else if (unformat (input, "prealloc-fifos %d", &esm->prealloc_fifos))
493 else if (unformat (input, "private-segment-count %d",
494 &esm->private_segment_count))
496 else if (unformat (input, "private-segment-size %U",
497 unformat_memory_size, &esm->private_segment_size))
499 else if (unformat (input, "appns %_%v%_", &appns_id))
501 else if (unformat (input, "all-scope"))
502 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
503 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
504 else if (unformat (input, "local-scope"))
505 appns_flags |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
506 else if (unformat (input, "global-scope"))
507 appns_flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
508 else if (unformat (input, "secret %lu", &appns_secret))
510 else if (unformat (input, "stop"))
512 else if (unformat (input, "tls-engine %d", &esm->tls_engine))
516 error = clib_error_return (0, "failed: unknown input `%U'",
517 format_unformat_error, input);
524 if (esm->app_index == (u32) ~ 0)
526 clib_warning ("server not running");
527 error = clib_error_return (0, "failed: server not running");
530 rv = echo_server_detach ();
533 clib_warning ("failed: detach");
534 error = clib_error_return (0, "failed: server detach %d", rv);
540 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
544 clib_warning ("No uri provided! Using default: %s", default_uri);
545 esm->server_uri = (char *) format (0, "%s%c", default_uri, 0);
548 if ((rv = parse_uri ((char *) esm->server_uri, &sep)))
550 error = clib_error_return (0, "Uri parse error: %d", rv);
553 esm->transport_proto = sep.transport_proto;
554 esm->is_dgram = (sep.transport_proto == TRANSPORT_PROTO_UDP);
556 rv = echo_server_create (vm, appns_id, appns_flags, appns_secret);
559 vec_free (esm->server_uri);
560 error = clib_error_return (0, "failed: server_create returned %d", rv);
571 VLIB_CLI_COMMAND (echo_server_create_command, static) =
573 .path = "test echo server",
574 .short_help = "test echo server proto <proto> [no echo][fifo-size <mbytes>]"
575 "[rcv-buf-size <bytes>][prealloc-fifos <count>]"
576 "[private-segment-count <count>][private-segment-size <bytes[m|g]>]"
577 "[uri <tcp://ip/port>]",
578 .function = echo_server_create_command_fn,
583 echo_server_main_init (vlib_main_t * vm)
585 echo_server_main_t *esm = &echo_server_main;
586 esm->my_client_index = ~0;
590 VLIB_INIT_FUNCTION (echo_server_main_init);
593 * fd.io coding-style-patch-verification: ON
596 * eval: (c-set-style "gnu")