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 u32 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 if (session_send_io_evt_to_thread (rx_fifo,
232 SESSION_IO_EVT_BUILTIN_RX))
233 clib_warning ("failed to enqueue self-tap");
235 vec_validate (esm->rx_retries[s->thread_index], s->session_index);
236 if (esm->rx_retries[thread_index][s->session_index] == 500000)
238 clib_warning ("session stuck: %U", format_session, s, 2);
240 if (esm->rx_retries[thread_index][s->session_index] < 500001)
241 esm->rx_retries[thread_index][s->session_index]++;
247 vec_validate (esm->rx_buf[thread_index], max_transfer);
250 actual_transfer = app_recv_stream_raw (rx_fifo,
251 esm->rx_buf[thread_index],
253 0 /* don't clear event */ ,
258 actual_transfer = app_recv_dgram_raw (rx_fifo,
259 esm->rx_buf[thread_index],
261 0 /* don't clear event */ ,
264 ASSERT (actual_transfer == max_transfer);
265 /* test_bytes (esm, actual_transfer); */
273 n_written = app_send_stream_raw (tx_fifo,
274 esm->vpp_queue[thread_index],
275 esm->rx_buf[thread_index],
276 actual_transfer, SESSION_IO_EVT_TX,
281 n_written = app_send_dgram_raw (tx_fifo, &at,
282 esm->vpp_queue[s->thread_index],
283 esm->rx_buf[thread_index],
284 actual_transfer, SESSION_IO_EVT_TX,
288 if (n_written != max_transfer)
289 clib_warning ("short trout! written %u read %u", n_written, max_transfer);
291 if (PREDICT_FALSE (svm_fifo_max_dequeue_cons (rx_fifo)))
297 static session_cb_vft_t echo_server_session_cb_vft = {
298 .session_accept_callback = echo_server_session_accept_callback,
299 .session_disconnect_callback = echo_server_session_disconnect_callback,
300 .session_connected_callback = echo_server_session_connected_callback,
301 .add_segment_callback = echo_server_add_segment_callback,
302 .builtin_app_rx_callback = echo_server_rx_callback,
303 .session_reset_callback = echo_server_session_reset_callback
307 echo_server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
309 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
310 echo_server_main_t *esm = &echo_server_main;
311 vnet_app_attach_args_t _a, *a = &_a;
312 u64 options[APP_OPTIONS_N_OPTIONS];
313 u32 segment_size = 512 << 20;
315 clib_memset (a, 0, sizeof (*a));
316 clib_memset (options, 0, sizeof (options));
319 echo_server_session_cb_vft.builtin_app_rx_callback =
320 echo_server_builtin_server_rx_callback_no_echo;
322 echo_server_session_cb_vft.builtin_app_rx_callback =
323 echo_server_rx_callback;
324 if (esm->transport_proto == TRANSPORT_PROTO_QUIC)
325 echo_server_session_cb_vft.session_accept_callback =
326 quic_echo_server_session_accept_callback;
328 if (esm->private_segment_size)
329 segment_size = esm->private_segment_size;
331 a->api_client_index = ~0;
332 a->name = format (0, "echo_server");
333 a->session_cb_vft = &echo_server_session_cb_vft;
334 a->options = options;
335 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
336 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
337 a->options[APP_OPTIONS_RX_FIFO_SIZE] = esm->fifo_size;
338 a->options[APP_OPTIONS_TX_FIFO_SIZE] = esm->fifo_size;
339 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = esm->private_segment_count;
340 a->options[APP_OPTIONS_TLS_ENGINE] = esm->tls_engine;
341 a->options[APP_OPTIONS_PCT_FIRST_ALLOC] = 100;
342 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
343 esm->prealloc_fifos ? esm->prealloc_fifos : 1;
345 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
348 a->namespace_id = appns_id;
349 a->options[APP_OPTIONS_FLAGS] |= appns_flags;
350 a->options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
353 if (vnet_application_attach (a))
355 clib_warning ("failed to attach server");
358 esm->app_index = a->app_index;
361 clib_memset (ck_pair, 0, sizeof (*ck_pair));
362 ck_pair->cert = (u8 *) test_srv_crt_rsa;
363 ck_pair->key = (u8 *) test_srv_key_rsa;
364 ck_pair->cert_len = test_srv_crt_rsa_len;
365 ck_pair->key_len = test_srv_key_rsa_len;
366 vnet_app_add_cert_key_pair (ck_pair);
367 esm->ckpair_index = ck_pair->index;
373 echo_server_detach (void)
375 echo_server_main_t *esm = &echo_server_main;
376 vnet_app_detach_args_t _da, *da = &_da;
379 da->app_index = esm->app_index;
380 da->api_client_index = ~0;
381 rv = vnet_application_detach (da);
383 vnet_app_del_cert_key_pair (esm->ckpair_index);
388 echo_client_transport_needs_crypto (transport_proto_t proto)
390 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
391 proto == TRANSPORT_PROTO_QUIC;
395 echo_server_listen ()
398 echo_server_main_t *esm = &echo_server_main;
399 vnet_listen_args_t _args = { 0 }, *args = &_args;
401 args->sep_ext.app_wrk_index = 0;
403 if ((rv = parse_uri (esm->server_uri, &args->sep_ext)))
407 args->app_index = esm->app_index;
408 if (echo_client_transport_needs_crypto (args->sep_ext.transport_proto))
410 session_endpoint_alloc_ext_cfg (&args->sep_ext,
411 TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
412 args->sep_ext.ext_cfg->crypto.ckpair_index = esm->ckpair_index;
415 if (args->sep_ext.transport_proto == TRANSPORT_PROTO_UDP)
417 args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
420 rv = vnet_listen (args);
421 esm->listener_handle = args->handle;
422 if (args->sep_ext.ext_cfg)
423 clib_mem_free (args->sep_ext.ext_cfg);
428 echo_server_create (vlib_main_t * vm, u8 * appns_id, u64 appns_flags,
431 echo_server_main_t *esm = &echo_server_main;
432 vlib_thread_main_t *vtm = vlib_get_thread_main ();
436 num_threads = 1 /* main thread */ + vtm->n_threads;
437 vec_validate (echo_server_main.vpp_queue, num_threads - 1);
438 vec_validate (esm->rx_buf, num_threads - 1);
439 vec_validate (esm->rx_retries, num_threads - 1);
440 for (i = 0; i < vec_len (esm->rx_retries); i++)
441 vec_validate (esm->rx_retries[i],
442 pool_elts (session_main.wrk[i].sessions));
443 esm->rcv_buffer_size = clib_max (esm->rcv_buffer_size, esm->fifo_size);
444 for (i = 0; i < num_threads; i++)
445 vec_validate (esm->rx_buf[i], esm->rcv_buffer_size);
447 if (echo_server_attach (appns_id, appns_flags, appns_secret))
449 clib_warning ("failed to attach server");
452 if (echo_server_listen ())
454 clib_warning ("failed to start listening");
455 if (echo_server_detach ())
456 clib_warning ("failed to detach");
462 static clib_error_t *
463 echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
464 vlib_cli_command_t * cmd)
466 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
467 echo_server_main_t *esm = &echo_server_main;
468 u8 server_uri_set = 0, *appns_id = 0;
469 u64 tmp, appns_flags = 0, appns_secret = 0;
470 char *default_uri = "tcp://0.0.0.0/1234";
471 int rv, is_stop = 0, barrier_acq_needed = 0;
472 clib_error_t *error = 0;
474 /* The request came over the binary api and the inband cli handler
475 * is not mp_safe. Drop the barrier to make sure the workers are not
478 if (vlib_num_workers () && vlib_thread_is_main_w_barrier ())
480 barrier_acq_needed = 1;
481 vlib_worker_thread_barrier_release (vm);
485 esm->fifo_size = 64 << 10;
486 esm->rcv_buffer_size = 128 << 10;
487 esm->prealloc_fifos = 0;
488 esm->private_segment_count = 0;
489 esm->private_segment_size = 0;
490 esm->tls_engine = CRYPTO_ENGINE_OPENSSL;
491 vec_free (esm->server_uri);
493 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
495 if (unformat (input, "uri %s", &esm->server_uri))
497 else if (unformat (input, "no-echo"))
499 else if (unformat (input, "fifo-size %d", &esm->fifo_size))
500 esm->fifo_size <<= 10;
501 else if (unformat (input, "rcv-buf-size %d", &esm->rcv_buffer_size))
503 else if (unformat (input, "prealloc-fifos %d", &esm->prealloc_fifos))
505 else if (unformat (input, "private-segment-count %d",
506 &esm->private_segment_count))
508 else if (unformat (input, "private-segment-size %U",
509 unformat_memory_size, &tmp))
511 if (tmp >= 0x100000000ULL)
513 error = clib_error_return (
514 0, "private segment size %lld (%llu) too large", tmp, tmp);
517 esm->private_segment_size = tmp;
519 else if (unformat (input, "appns %_%v%_", &appns_id))
521 else if (unformat (input, "all-scope"))
522 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
523 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
524 else if (unformat (input, "local-scope"))
525 appns_flags |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
526 else if (unformat (input, "global-scope"))
527 appns_flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
528 else if (unformat (input, "secret %lu", &appns_secret))
530 else if (unformat (input, "stop"))
532 else if (unformat (input, "tls-engine %d", &esm->tls_engine))
536 error = clib_error_return (0, "failed: unknown input `%U'",
537 format_unformat_error, input);
544 if (esm->app_index == (u32) ~ 0)
546 clib_warning ("server not running");
547 error = clib_error_return (0, "failed: server not running");
550 rv = echo_server_detach ();
553 clib_warning ("failed: detach");
554 error = clib_error_return (0, "failed: server detach %d", rv);
560 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
564 clib_warning ("No uri provided! Using default: %s", default_uri);
565 esm->server_uri = (char *) format (0, "%s%c", default_uri, 0);
568 if ((rv = parse_uri ((char *) esm->server_uri, &sep)))
570 error = clib_error_return (0, "Uri parse error: %d", rv);
573 esm->transport_proto = sep.transport_proto;
574 esm->is_dgram = (sep.transport_proto == TRANSPORT_PROTO_UDP);
576 rv = echo_server_create (vm, appns_id, appns_flags, appns_secret);
579 vec_free (esm->server_uri);
580 error = clib_error_return (0, "failed: server_create returned %d", rv);
587 if (barrier_acq_needed)
588 vlib_worker_thread_barrier_sync (vm);
594 VLIB_CLI_COMMAND (echo_server_create_command, static) =
596 .path = "test echo server",
597 .short_help = "test echo server proto <proto> [no echo][fifo-size <mbytes>]"
598 "[rcv-buf-size <bytes>][prealloc-fifos <count>]"
599 "[private-segment-count <count>][private-segment-size <bytes[m|g]>]"
600 "[uri <tcp://ip/port>]",
601 .function = echo_server_create_command_fn,
606 echo_server_main_init (vlib_main_t * vm)
608 echo_server_main_t *esm = &echo_server_main;
609 esm->my_client_index = ~0;
613 VLIB_INIT_FUNCTION (echo_server_main_init);
616 * fd.io coding-style-patch-verification: ON
619 * eval: (c-set-style "gnu")