blob: f34a4909a34600ed196436e7eee3fed92a6b63bd [file] [log] [blame]
Florin Coras4399c2e2018-01-25 06:34:42 -08001/*
2* Copyright (c) 2015-2017 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 <vnet/vnet.h>
17#include <vlibmemory/api.h>
18#include <vnet/session/application.h>
19#include <vnet/session/application_interface.h>
20
21typedef struct
22{
23 /*
24 * Server app parameters
25 */
26 svm_queue_t **vpp_queue;
27 svm_queue_t *vl_input_queue; /**< Sever's event queue */
28
29 u32 app_index; /**< Server app index */
30 u32 my_client_index; /**< API client handle */
31 u32 node_index; /**< process node index for evnt scheduling */
32
33 /*
34 * Config params
35 */
36 u8 no_echo; /**< Don't echo traffic */
Florin Coras7fb0fe12018-04-09 09:24:52 -070037 u32 fifo_size; /**< Fifo size */
Florin Coras4399c2e2018-01-25 06:34:42 -080038 u32 rcv_buffer_size; /**< Rcv buffer size */
39 u32 prealloc_fifos; /**< Preallocate fifos */
40 u32 private_segment_count; /**< Number of private segments */
41 u32 private_segment_size; /**< Size of private segments */
42 char *server_uri; /**< Server URI */
Florin Coras58d36f02018-03-09 13:05:53 -080043 u32 tls_engine; /**< TLS engine: mbedtls/openssl */
Florin Coras7fb0fe12018-04-09 09:24:52 -070044 u8 is_dgram; /**< set if transport is dgram */
Florin Coras4399c2e2018-01-25 06:34:42 -080045 /*
46 * Test state
47 */
48 u8 **rx_buf; /**< Per-thread RX buffer */
49 u64 byte_index;
50 u32 **rx_retries;
51
52 vlib_main_t *vlib_main;
53} echo_server_main_t;
54
55echo_server_main_t echo_server_main;
56
57int
58echo_server_session_accept_callback (stream_session_t * s)
59{
60 echo_server_main_t *esm = &echo_server_main;
61
62 esm->vpp_queue[s->thread_index] =
63 session_manager_get_vpp_event_queue (s->thread_index);
64 s->session_state = SESSION_STATE_READY;
65 esm->byte_index = 0;
Florin Coras4399c2e2018-01-25 06:34:42 -080066 esm->rx_retries[s->thread_index][s->session_index] = 0;
67 return 0;
68}
69
70void
71echo_server_session_disconnect_callback (stream_session_t * s)
72{
73 echo_server_main_t *esm = &echo_server_main;
74 vnet_disconnect_args_t _a, *a = &_a;
75
76 a->handle = session_handle (s);
77 a->app_index = esm->app_index;
78 vnet_disconnect_session (a);
79}
80
81void
82echo_server_session_reset_callback (stream_session_t * s)
83{
84 clib_warning ("Reset session %U", format_stream_session, s, 2);
85 stream_session_cleanup (s);
86}
87
88int
89echo_server_session_connected_callback (u32 app_index, u32 api_context,
90 stream_session_t * s, u8 is_fail)
91{
92 clib_warning ("called...");
93 return -1;
94}
95
96int
97echo_server_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
98{
Florin Corasa332c462018-01-31 06:52:17 -080099 /* New heaps may be added */
100 return 0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800101}
102
103int
104echo_server_redirect_connect_callback (u32 client_index, void *mp)
105{
106 clib_warning ("called...");
107 return -1;
108}
109
110void
111test_bytes (echo_server_main_t * esm, int actual_transfer)
112{
113 int i;
114 u32 my_thread_id = vlib_get_thread_index ();
115
116 for (i = 0; i < actual_transfer; i++)
117 {
118 if (esm->rx_buf[my_thread_id][i] != ((esm->byte_index + i) & 0xff))
119 {
120 clib_warning ("at %lld expected %d got %d", esm->byte_index + i,
121 (esm->byte_index + i) & 0xff,
122 esm->rx_buf[my_thread_id][i]);
123 }
124 }
125 esm->byte_index += actual_transfer;
126}
127
128/*
Florin Coras7fb0fe12018-04-09 09:24:52 -0700129 * If no-echo, just drop the data and be done with it.
Florin Coras4399c2e2018-01-25 06:34:42 -0800130 */
131int
132echo_server_builtin_server_rx_callback_no_echo (stream_session_t * s)
133{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700134 svm_fifo_t *rx_fifo = s->server_rx_fifo;
135 svm_fifo_dequeue_drop (rx_fifo, svm_fifo_max_dequeue (rx_fifo));
Florin Coras4399c2e2018-01-25 06:34:42 -0800136 return 0;
137}
138
139int
140echo_server_rx_callback (stream_session_t * s)
141{
142 u32 n_written, max_dequeue, max_enqueue, max_transfer;
143 int actual_transfer;
144 svm_fifo_t *tx_fifo, *rx_fifo;
145 echo_server_main_t *esm = &echo_server_main;
146 session_fifo_event_t evt;
147 u32 thread_index = vlib_get_thread_index ();
Florin Coras7fb0fe12018-04-09 09:24:52 -0700148 app_session_transport_t at;
149 svm_queue_t *q;
Florin Coras4399c2e2018-01-25 06:34:42 -0800150
151 ASSERT (s->thread_index == thread_index);
152
153 rx_fifo = s->server_rx_fifo;
154 tx_fifo = s->server_tx_fifo;
155
156 ASSERT (rx_fifo->master_thread_index == thread_index);
157 ASSERT (tx_fifo->master_thread_index == thread_index);
158
Florin Coras7fb0fe12018-04-09 09:24:52 -0700159 max_enqueue = svm_fifo_max_enqueue (tx_fifo);
160 if (!esm->is_dgram)
161 {
162 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
163 }
164 else
165 {
166 session_dgram_pre_hdr_t ph;
167 svm_fifo_peek (rx_fifo, 0, sizeof (ph), (u8 *) & ph);
168 max_dequeue = ph.data_length - ph.data_offset;
169 if (!esm->vpp_queue[s->thread_index])
170 {
171 q = session_manager_get_vpp_event_queue (s->thread_index);
172 esm->vpp_queue[s->thread_index] = q;
173 }
174 max_enqueue -= sizeof (session_dgram_hdr_t);
175 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800176
177 if (PREDICT_FALSE (max_dequeue == 0))
178 return 0;
179
180 /* Number of bytes we're going to copy */
Florin Coras7fb0fe12018-04-09 09:24:52 -0700181 max_transfer = clib_min (max_dequeue, max_enqueue);
Florin Coras4399c2e2018-01-25 06:34:42 -0800182
183 /* No space in tx fifo */
184 if (PREDICT_FALSE (max_transfer == 0))
185 {
186 /* XXX timeout for session that are stuck */
187
188 rx_event:
189 /* Program self-tap to retry */
190 if (svm_fifo_set_event (rx_fifo))
191 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800192 evt.fifo = rx_fifo;
193 evt.event_type = FIFO_EVENT_BUILTIN_RX;
194
Florin Coras7fb0fe12018-04-09 09:24:52 -0700195 q = esm->vpp_queue[s->thread_index];
Florin Coras4399c2e2018-01-25 06:34:42 -0800196 if (PREDICT_FALSE (q->cursize == q->maxsize))
197 clib_warning ("out of event queue space");
198 else if (svm_queue_add (q, (u8 *) & evt, 0))
199 clib_warning ("failed to enqueue self-tap");
200
Florin Coras7fb0fe12018-04-09 09:24:52 -0700201 vec_validate (esm->rx_retries[s->thread_index], s->session_index);
Florin Coras4399c2e2018-01-25 06:34:42 -0800202 if (esm->rx_retries[thread_index][s->session_index] == 500000)
203 {
204 clib_warning ("session stuck: %U", format_stream_session, s, 2);
205 }
206 if (esm->rx_retries[thread_index][s->session_index] < 500001)
207 esm->rx_retries[thread_index][s->session_index]++;
208 }
209
210 return 0;
211 }
212
Florin Coras7fb0fe12018-04-09 09:24:52 -0700213 vec_validate (esm->rx_buf[thread_index], max_transfer);
214 if (!esm->is_dgram)
215 {
216 actual_transfer = app_recv_stream_raw (rx_fifo,
217 esm->rx_buf[thread_index],
218 max_transfer,
219 0 /* don't clear event */ );
220 }
221 else
222 {
223 actual_transfer = app_recv_dgram_raw (rx_fifo,
224 esm->rx_buf[thread_index],
225 max_transfer, &at,
226 0 /* don't clear event */ );
227 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800228 ASSERT (actual_transfer == max_transfer);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700229 /* test_bytes (esm, actual_transfer); */
Florin Coras4399c2e2018-01-25 06:34:42 -0800230
231 /*
232 * Echo back
233 */
234
Florin Coras7fb0fe12018-04-09 09:24:52 -0700235 if (!esm->is_dgram)
Florin Coras4399c2e2018-01-25 06:34:42 -0800236 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700237 n_written = app_send_stream_raw (tx_fifo,
238 esm->vpp_queue[thread_index],
239 esm->rx_buf[thread_index],
240 actual_transfer, 0);
241 }
242 else
243 {
244 n_written = app_send_dgram_raw (tx_fifo, &at,
245 esm->vpp_queue[s->thread_index],
246 esm->rx_buf[thread_index],
247 actual_transfer, 0);
Florin Coras4399c2e2018-01-25 06:34:42 -0800248 }
249
Florin Coras7fb0fe12018-04-09 09:24:52 -0700250 if (n_written != max_transfer)
251 clib_warning ("short trout! written %u read %u", n_written, max_transfer);
252
253 if (PREDICT_FALSE (svm_fifo_max_dequeue (rx_fifo)))
Florin Coras4399c2e2018-01-25 06:34:42 -0800254 goto rx_event;
255
256 return 0;
257}
258
259static session_cb_vft_t echo_server_session_cb_vft = {
260 .session_accept_callback = echo_server_session_accept_callback,
261 .session_disconnect_callback = echo_server_session_disconnect_callback,
262 .session_connected_callback = echo_server_session_connected_callback,
263 .add_segment_callback = echo_server_add_segment_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800264 .builtin_app_rx_callback = echo_server_rx_callback,
Florin Coras4399c2e2018-01-25 06:34:42 -0800265 .session_reset_callback = echo_server_session_reset_callback
266};
267
268/* Abuse VPP's input queue */
269static int
270create_api_loopback (vlib_main_t * vm)
271{
272 echo_server_main_t *esm = &echo_server_main;
273 api_main_t *am = &api_main;
274 vl_shmem_hdr_t *shmem_hdr;
275
276 shmem_hdr = am->shmem_hdr;
277 esm->vl_input_queue = shmem_hdr->vl_input_queue;
278 esm->my_client_index = vl_api_memclnt_create_internal ("echo_server",
279 esm->vl_input_queue);
280 return 0;
281}
282
283static int
284echo_server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
285{
Florin Coras371ca502018-02-21 12:07:41 -0800286 vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
287 vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
Florin Coras4399c2e2018-01-25 06:34:42 -0800288 echo_server_main_t *esm = &echo_server_main;
Florin Coras4399c2e2018-01-25 06:34:42 -0800289 vnet_app_attach_args_t _a, *a = &_a;
Florin Coras371ca502018-02-21 12:07:41 -0800290 u64 options[APP_OPTIONS_N_OPTIONS];
Florin Coras4399c2e2018-01-25 06:34:42 -0800291 u32 segment_size = 512 << 20;
292
293 memset (a, 0, sizeof (*a));
294 memset (options, 0, sizeof (options));
295
296 if (esm->no_echo)
Florin Coras371ca502018-02-21 12:07:41 -0800297 echo_server_session_cb_vft.builtin_app_rx_callback =
Florin Coras4399c2e2018-01-25 06:34:42 -0800298 echo_server_builtin_server_rx_callback_no_echo;
299 else
Florin Coras371ca502018-02-21 12:07:41 -0800300 echo_server_session_cb_vft.builtin_app_rx_callback =
Florin Coras4399c2e2018-01-25 06:34:42 -0800301 echo_server_rx_callback;
302
303 if (esm->private_segment_size)
304 segment_size = esm->private_segment_size;
305
306 a->api_client_index = esm->my_client_index;
307 a->session_cb_vft = &echo_server_session_cb_vft;
308 a->options = options;
309 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Corasa332c462018-01-31 06:52:17 -0800310 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800311 a->options[APP_OPTIONS_RX_FIFO_SIZE] = esm->fifo_size;
312 a->options[APP_OPTIONS_TX_FIFO_SIZE] = esm->fifo_size;
313 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = esm->private_segment_count;
Florin Coras58d36f02018-03-09 13:05:53 -0800314 a->options[APP_OPTIONS_TLS_ENGINE] = esm->tls_engine;
Florin Coras4399c2e2018-01-25 06:34:42 -0800315 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
316 esm->prealloc_fifos ? esm->prealloc_fifos : 1;
317
318 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
319 if (appns_id)
320 {
321 a->namespace_id = appns_id;
322 a->options[APP_OPTIONS_FLAGS] |= appns_flags;
323 a->options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
324 }
325
326 if (vnet_application_attach (a))
327 {
328 clib_warning ("failed to attach server");
329 return -1;
330 }
331 esm->app_index = a->app_index;
Florin Coras371ca502018-02-21 12:07:41 -0800332
333 memset (a_cert, 0, sizeof (*a_cert));
334 a_cert->app_index = a->app_index;
335 vec_validate (a_cert->cert, test_srv_crt_rsa_len);
336 clib_memcpy (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
337 vnet_app_add_tls_cert (a_cert);
338
339 memset (a_key, 0, sizeof (*a_key));
340 a_key->app_index = a->app_index;
341 vec_validate (a_key->key, test_srv_key_rsa_len);
342 clib_memcpy (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
343 vnet_app_add_tls_key (a_key);
Florin Coras4399c2e2018-01-25 06:34:42 -0800344 return 0;
345}
346
347static int
348echo_server_detach (void)
349{
350 echo_server_main_t *esm = &echo_server_main;
351 vnet_app_detach_args_t _da, *da = &_da;
352 int rv;
353
354 da->app_index = esm->app_index;
355 rv = vnet_application_detach (da);
356 esm->app_index = ~0;
357 return rv;
358}
359
360static int
361echo_server_listen ()
362{
363 echo_server_main_t *esm = &echo_server_main;
364 vnet_bind_args_t _a, *a = &_a;
365 memset (a, 0, sizeof (*a));
366 a->app_index = esm->app_index;
367 a->uri = esm->server_uri;
368 return vnet_bind_uri (a);
369}
370
371static int
372echo_server_create (vlib_main_t * vm, u8 * appns_id, u64 appns_flags,
373 u64 appns_secret)
374{
375 echo_server_main_t *esm = &echo_server_main;
376 vlib_thread_main_t *vtm = vlib_get_thread_main ();
377 u32 num_threads;
378 int i;
379
380 if (esm->my_client_index == (u32) ~ 0)
381 {
382 if (create_api_loopback (vm))
383 {
384 clib_warning ("failed to create api loopback");
385 return -1;
386 }
387 }
388
389 num_threads = 1 /* main thread */ + vtm->n_threads;
390 vec_validate (echo_server_main.vpp_queue, num_threads - 1);
391 vec_validate (esm->rx_buf, num_threads - 1);
392 vec_validate (esm->rx_retries, num_threads - 1);
Florin Corasc1a448b2018-04-20 10:51:49 -0700393 for (i = 0; i < vec_len (esm->rx_retries); i++)
394 vec_validate (esm->rx_retries[i],
395 pool_elts (session_manager_main.sessions[i]));
Florin Coras81a13db2018-03-16 08:48:31 -0700396 esm->rcv_buffer_size = clib_max (esm->rcv_buffer_size, esm->fifo_size);
Florin Coras4399c2e2018-01-25 06:34:42 -0800397 for (i = 0; i < num_threads; i++)
398 vec_validate (esm->rx_buf[i], esm->rcv_buffer_size);
399
400 if (echo_server_attach (appns_id, appns_flags, appns_secret))
401 {
402 clib_warning ("failed to attach server");
403 return -1;
404 }
405 if (echo_server_listen ())
406 {
407 clib_warning ("failed to start listening");
408 if (echo_server_detach ())
409 clib_warning ("failed to detach");
410 return -1;
411 }
412 return 0;
413}
414
415static clib_error_t *
416echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
417 vlib_cli_command_t * cmd)
418{
419 echo_server_main_t *esm = &echo_server_main;
420 u8 server_uri_set = 0, *appns_id = 0;
421 u64 tmp, appns_flags = 0, appns_secret = 0;
422 char *default_uri = "tcp://0.0.0.0/1234";
Florin Corasf8f516a2018-02-08 15:10:09 -0800423 int rv, is_stop = 0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800424
425 esm->no_echo = 0;
426 esm->fifo_size = 64 << 10;
427 esm->rcv_buffer_size = 128 << 10;
428 esm->prealloc_fifos = 0;
429 esm->private_segment_count = 0;
430 esm->private_segment_size = 0;
Florin Coras58d36f02018-03-09 13:05:53 -0800431 esm->tls_engine = TLS_ENGINE_OPENSSL;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700432 esm->is_dgram = 0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800433 vec_free (esm->server_uri);
434
435 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
436 {
437 if (unformat (input, "uri %s", &esm->server_uri))
438 server_uri_set = 1;
439 else if (unformat (input, "no-echo"))
440 esm->no_echo = 1;
441 else if (unformat (input, "fifo-size %d", &esm->fifo_size))
442 esm->fifo_size <<= 10;
443 else if (unformat (input, "rcv-buf-size %d", &esm->rcv_buffer_size))
444 ;
445 else if (unformat (input, "prealloc-fifos %d", &esm->prealloc_fifos))
446 ;
447 else if (unformat (input, "private-segment-count %d",
448 &esm->private_segment_count))
449 ;
450 else if (unformat (input, "private-segment-size %U",
451 unformat_memory_size, &tmp))
452 {
453 if (tmp >= 0x100000000ULL)
454 return clib_error_return
455 (0, "private segment size %lld (%llu) too large", tmp, tmp);
456 esm->private_segment_size = tmp;
457 }
458 else if (unformat (input, "appns %_%v%_", &appns_id))
459 ;
460 else if (unformat (input, "all-scope"))
461 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
462 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
463 else if (unformat (input, "local-scope"))
464 appns_flags |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
465 else if (unformat (input, "global-scope"))
466 appns_flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
467 else if (unformat (input, "secret %lu", &appns_secret))
468 ;
Florin Corasf8f516a2018-02-08 15:10:09 -0800469 else if (unformat (input, "stop"))
470 is_stop = 1;
Florin Coras58d36f02018-03-09 13:05:53 -0800471 else if (unformat (input, "tls-engine %d", &esm->tls_engine))
472 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800473 else
474 return clib_error_return (0, "failed: unknown input `%U'",
475 format_unformat_error, input);
476 }
477
Florin Corasf8f516a2018-02-08 15:10:09 -0800478 if (is_stop)
479 {
480 if (esm->app_index == (u32) ~ 0)
481 {
482 clib_warning ("server not running");
483 return clib_error_return (0, "failed: server not running");
484 }
485 rv = echo_server_detach ();
486 if (rv)
487 {
488 clib_warning ("failed: detach");
489 return clib_error_return (0, "failed: server detach %d", rv);
490 }
491 return 0;
492 }
493
Florin Coras4399c2e2018-01-25 06:34:42 -0800494 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
495
496 if (!server_uri_set)
497 {
498 clib_warning ("No uri provided! Using default: %s", default_uri);
499 esm->server_uri = (char *) format (0, "%s%c", default_uri, 0);
500 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700501 if (esm->server_uri[0] == 'u' && esm->server_uri[3] != 'c')
502 esm->is_dgram = 1;
Florin Coras4399c2e2018-01-25 06:34:42 -0800503
504 rv = echo_server_create (vm, appns_id, appns_flags, appns_secret);
505 vec_free (appns_id);
506 if (rv)
507 {
508 vec_free (esm->server_uri);
509 return clib_error_return (0, "failed: server_create returned %d", rv);
510 }
511
512 return 0;
513}
514
515/* *INDENT-OFF* */
516VLIB_CLI_COMMAND (echo_server_create_command, static) =
517{
518 .path = "test echo server",
519 .short_help = "test echo server proto <proto> [no echo][fifo-size <mbytes>]"
520 "[rcv-buf-size <bytes>][prealloc-fifos <count>]"
521 "[private-segment-count <count>][private-segment-size <bytes[m|g]>]"
522 "[uri <tcp://ip/port>]",
523 .function = echo_server_create_command_fn,
524};
525/* *INDENT-ON* */
526
527clib_error_t *
528echo_server_main_init (vlib_main_t * vm)
529{
530 echo_server_main_t *esm = &echo_server_main;
531 esm->my_client_index = ~0;
532 return 0;
533}
534
535VLIB_INIT_FUNCTION (echo_server_main_init);
536
537/*
538* fd.io coding-style-patch-verification: ON
539*
540* Local Variables:
541* eval: (c-set-style "gnu")
542* End:
543*/