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 <hs_apps/proxy.h>
22 proxy_main_t proxy_main;
29 } proxy_connect_args_t;
32 proxy_cb_fn (void *data, u32 data_len)
34 proxy_connect_args_t *pa = (proxy_connect_args_t *) data;
35 vnet_connect_args_t a;
37 memset (&a, 0, sizeof (a));
38 a.api_context = pa->api_context;
39 a.app_index = pa->app_index;
41 vnet_connect_uri (&a);
45 proxy_call_main_thread (vnet_connect_args_t * a)
47 if (vlib_get_thread_index () == 0)
53 proxy_connect_args_t args;
54 args.api_context = a->api_context;
55 args.app_index = a->app_index;
56 clib_memcpy (args.uri, a->uri, vec_len (a->uri));
57 vl_api_rpc_call_main_thread (proxy_cb_fn, (u8 *) & args, sizeof (args));
62 delete_proxy_session (session_t * s, int is_active_open)
64 proxy_main_t *pm = &proxy_main;
65 proxy_session_t *ps = 0;
66 vnet_disconnect_args_t _a, *a = &_a;
67 session_t *active_open_session = 0;
68 session_t *server_session = 0;
72 clib_spinlock_lock_if_init (&pm->sessions_lock);
74 handle = session_handle (s);
78 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
81 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
82 is_active_open ? "active open" : "server",
85 else if (!pool_is_free_index (pm->sessions, p[0]))
87 active_open_session = s;
88 ps = pool_elt_at_index (pm->sessions, p[0]);
89 if (ps->vpp_server_handle != ~0)
90 server_session = session_get_from_handle (ps->vpp_server_handle);
95 p = hash_get (pm->proxy_session_by_server_handle, handle);
98 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
99 is_active_open ? "active open" : "server",
102 else if (!pool_is_free_index (pm->sessions, p[0]))
105 ps = pool_elt_at_index (pm->sessions, p[0]);
106 if (ps->vpp_active_open_handle != ~0)
107 active_open_session = session_get_from_handle
108 (ps->vpp_active_open_handle);
115 clib_memset (ps, 0xFE, sizeof (*ps));
116 pool_put (pm->sessions, ps);
119 if (active_open_session)
121 a->handle = session_handle (active_open_session);
122 a->app_index = pm->active_open_app_index;
123 hash_unset (pm->proxy_session_by_active_open_handle,
124 session_handle (active_open_session));
125 vnet_disconnect_session (a);
130 a->handle = session_handle (server_session);
131 a->app_index = pm->server_app_index;
132 hash_unset (pm->proxy_session_by_server_handle,
133 session_handle (server_session));
134 vnet_disconnect_session (a);
137 clib_spinlock_unlock_if_init (&pm->sessions_lock);
141 proxy_accept_callback (session_t * s)
143 proxy_main_t *pm = &proxy_main;
145 s->session_state = SESSION_STATE_READY;
147 clib_spinlock_lock_if_init (&pm->sessions_lock);
153 proxy_disconnect_callback (session_t * s)
155 delete_proxy_session (s, 0 /* is_active_open */ );
159 proxy_reset_callback (session_t * s)
161 clib_warning ("Reset session %U", format_session, s, 2);
162 delete_proxy_session (s, 0 /* is_active_open */ );
166 proxy_connected_callback (u32 app_index, u32 api_context,
167 session_t * s, u8 is_fail)
169 clib_warning ("called...");
174 proxy_add_segment_callback (u32 client_index, u64 segment_handle)
176 clib_warning ("called...");
181 proxy_rx_callback (session_t * s)
184 int actual_transfer __attribute__ ((unused));
185 svm_fifo_t *tx_fifo, *rx_fifo;
186 proxy_main_t *pm = &proxy_main;
187 u32 thread_index = vlib_get_thread_index ();
188 vnet_connect_args_t _a, *a = &_a;
192 svm_fifo_t *active_open_tx_fifo;
194 ASSERT (s->thread_index == thread_index);
196 clib_spinlock_lock_if_init (&pm->sessions_lock);
197 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
199 if (PREDICT_TRUE (p != 0))
201 clib_spinlock_unlock_if_init (&pm->sessions_lock);
202 active_open_tx_fifo = s->rx_fifo;
205 * Send event for active open tx fifo
207 if (svm_fifo_set_event (active_open_tx_fifo))
209 u32 ao_thread_index = active_open_tx_fifo->master_thread_index;
210 u32 ao_session_index = active_open_tx_fifo->master_session_index;
211 if (session_send_io_evt_to_thread_custom (&ao_session_index,
214 clib_warning ("failed to enqueue tx evt");
219 rx_fifo = s->rx_fifo;
220 tx_fifo = s->tx_fifo;
222 ASSERT (rx_fifo->master_thread_index == thread_index);
223 ASSERT (tx_fifo->master_thread_index == thread_index);
225 max_dequeue = svm_fifo_max_dequeue_cons (s->rx_fifo);
227 if (PREDICT_FALSE (max_dequeue == 0))
230 max_dequeue = clib_min (pm->rcv_buffer_size, max_dequeue);
231 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
232 max_dequeue, pm->rx_buf[thread_index]);
234 /* $$$ your message in this space: parse url, etc. */
236 clib_memset (a, 0, sizeof (*a));
238 pool_get (pm->sessions, ps);
239 clib_memset (ps, 0, sizeof (*ps));
240 ps->server_rx_fifo = rx_fifo;
241 ps->server_tx_fifo = tx_fifo;
242 ps->vpp_server_handle = session_handle (s);
244 proxy_index = ps - pm->sessions;
246 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
249 clib_spinlock_unlock_if_init (&pm->sessions_lock);
251 a->uri = (char *) pm->client_uri;
252 a->api_context = proxy_index;
253 a->app_index = pm->active_open_app_index;
254 proxy_call_main_thread (a);
260 static session_cb_vft_t proxy_session_cb_vft = {
261 .session_accept_callback = proxy_accept_callback,
262 .session_disconnect_callback = proxy_disconnect_callback,
263 .session_connected_callback = proxy_connected_callback,
264 .add_segment_callback = proxy_add_segment_callback,
265 .builtin_app_rx_callback = proxy_rx_callback,
266 .session_reset_callback = proxy_reset_callback
270 active_open_connected_callback (u32 app_index, u32 opaque,
271 session_t * s, u8 is_fail)
273 proxy_main_t *pm = &proxy_main;
275 u8 thread_index = vlib_get_thread_index ();
279 clib_warning ("connection %d failed!", opaque);
284 * Setup proxy session handle.
286 clib_spinlock_lock_if_init (&pm->sessions_lock);
288 ps = pool_elt_at_index (pm->sessions, opaque);
289 ps->vpp_active_open_handle = session_handle (s);
291 s->tx_fifo = ps->server_rx_fifo;
292 s->rx_fifo = ps->server_tx_fifo;
295 * Reset the active-open tx-fifo master indices so the active-open session
296 * will receive data, etc.
298 s->tx_fifo->master_session_index = s->session_index;
299 s->tx_fifo->master_thread_index = s->thread_index;
302 * Account for the active-open session's use of the fifos
303 * so they won't disappear until the last session which uses
306 s->tx_fifo->refcnt++;
307 s->rx_fifo->refcnt++;
309 hash_set (pm->proxy_session_by_active_open_handle,
310 ps->vpp_active_open_handle, opaque);
312 clib_spinlock_unlock_if_init (&pm->sessions_lock);
315 * Send event for active open tx fifo
317 ASSERT (s->thread_index == thread_index);
318 if (svm_fifo_set_event (s->tx_fifo))
319 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
325 active_open_reset_callback (session_t * s)
327 delete_proxy_session (s, 1 /* is_active_open */ );
331 active_open_create_callback (session_t * s)
337 active_open_disconnect_callback (session_t * s)
339 delete_proxy_session (s, 1 /* is_active_open */ );
343 active_open_rx_callback (session_t * s)
345 svm_fifo_t *proxy_tx_fifo;
347 proxy_tx_fifo = s->rx_fifo;
350 * Send event for server tx fifo
352 if (svm_fifo_set_event (proxy_tx_fifo))
354 u8 thread_index = proxy_tx_fifo->master_thread_index;
355 u32 session_index = proxy_tx_fifo->master_session_index;
356 return session_send_io_evt_to_thread_custom (&session_index,
365 static session_cb_vft_t active_open_clients = {
366 .session_reset_callback = active_open_reset_callback,
367 .session_connected_callback = active_open_connected_callback,
368 .session_accept_callback = active_open_create_callback,
369 .session_disconnect_callback = active_open_disconnect_callback,
370 .builtin_app_rx_callback = active_open_rx_callback
375 proxy_server_attach ()
377 proxy_main_t *pm = &proxy_main;
378 u64 options[APP_OPTIONS_N_OPTIONS];
379 vnet_app_attach_args_t _a, *a = &_a;
380 u32 segment_size = 512 << 20;
382 clib_memset (a, 0, sizeof (*a));
383 clib_memset (options, 0, sizeof (options));
385 if (pm->private_segment_size)
386 segment_size = pm->private_segment_size;
387 a->name = format (0, "proxy-server");
388 a->api_client_index = pm->server_client_index;
389 a->session_cb_vft = &proxy_session_cb_vft;
390 a->options = options;
391 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
392 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
393 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
394 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
395 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
396 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
398 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
400 if (vnet_application_attach (a))
402 clib_warning ("failed to attach server");
405 pm->server_app_index = a->app_index;
412 active_open_attach (void)
414 proxy_main_t *pm = &proxy_main;
415 vnet_app_attach_args_t _a, *a = &_a;
418 clib_memset (a, 0, sizeof (*a));
419 clib_memset (options, 0, sizeof (options));
421 a->api_client_index = pm->active_open_client_index;
422 a->session_cb_vft = &active_open_clients;
423 a->name = format (0, "proxy-active-open");
425 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
426 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
427 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
428 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
429 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
430 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
431 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
433 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
434 | APP_OPTIONS_FLAGS_IS_PROXY;
436 a->options = options;
438 if (vnet_application_attach (a))
441 pm->active_open_app_index = a->app_index;
449 proxy_server_listen ()
451 proxy_main_t *pm = &proxy_main;
452 vnet_listen_args_t _a, *a = &_a;
453 clib_memset (a, 0, sizeof (*a));
454 a->app_index = pm->server_app_index;
455 a->uri = (char *) pm->server_uri;
456 return vnet_bind_uri (a);
460 proxy_server_create (vlib_main_t * vm)
462 proxy_main_t *pm = &proxy_main;
463 vlib_thread_main_t *vtm = vlib_get_thread_main ();
467 num_threads = 1 /* main thread */ + vtm->n_threads;
468 vec_validate (proxy_main.server_event_queue, num_threads - 1);
469 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
470 vec_validate (pm->rx_buf, num_threads - 1);
472 for (i = 0; i < num_threads; i++)
473 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
475 if (proxy_server_attach ())
477 clib_warning ("failed to attach server app");
480 if (proxy_server_listen ())
482 clib_warning ("failed to start listening");
485 if (active_open_attach ())
487 clib_warning ("failed to attach active open app");
491 for (i = 0; i < num_threads; i++)
493 pm->active_open_event_queue[i] = session_main_get_vpp_event_queue (i);
495 ASSERT (pm->active_open_event_queue[i]);
497 pm->server_event_queue[i] = session_main_get_vpp_event_queue (i);
503 static clib_error_t *
504 proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
505 vlib_cli_command_t * cmd)
507 proxy_main_t *pm = &proxy_main;
508 char *default_server_uri = "tcp://0.0.0.0/23";
509 char *default_client_uri = "tcp://6.0.2.2/23";
513 pm->fifo_size = 64 << 10;
514 pm->rcv_buffer_size = 1024;
515 pm->prealloc_fifos = 0;
516 pm->private_segment_count = 0;
517 pm->private_segment_size = 0;
521 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
523 if (unformat (input, "fifo-size %d", &pm->fifo_size))
524 pm->fifo_size <<= 10;
525 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
527 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
529 else if (unformat (input, "private-segment-count %d",
530 &pm->private_segment_count))
532 else if (unformat (input, "private-segment-size %U",
533 unformat_memory_size, &tmp))
535 if (tmp >= 0x100000000ULL)
536 return clib_error_return
537 (0, "private segment size %lld (%llu) too large", tmp, tmp);
538 pm->private_segment_size = tmp;
540 else if (unformat (input, "server-uri %s", &pm->server_uri))
541 vec_add1 (pm->server_uri, 0);
542 else if (unformat (input, "client-uri %s", &pm->client_uri))
543 vec_add1 (pm->client_uri, 0);
545 return clib_error_return (0, "unknown input `%U'",
546 format_unformat_error, input);
551 clib_warning ("No server-uri provided, Using default: %s",
553 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
557 clib_warning ("No client-uri provided, Using default: %s",
559 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
562 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
564 rv = proxy_server_create (vm);
570 return clib_error_return (0, "server_create returned %d", rv);
577 VLIB_CLI_COMMAND (proxy_create_command, static) =
579 .path = "test proxy server",
580 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
581 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
582 "[prealloc-fifos <nn>][private-segment-size <mem>]"
583 "[private-segment-count <nn>]",
584 .function = proxy_server_create_command_fn,
589 proxy_main_init (vlib_main_t * vm)
591 proxy_main_t *pm = &proxy_main;
592 pm->server_client_index = ~0;
593 pm->active_open_client_index = ~0;
594 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
595 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
600 VLIB_INIT_FUNCTION (proxy_main_init);
603 * fd.io coding-style-patch-verification: ON
606 * eval: (c-set-style "gnu")