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:
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-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 a.api_context = pa->api_context;
38 a.app_index = pa->app_index;
40 vnet_connect_uri (&a);
44 proxy_call_main_thread (vnet_connect_args_t * a)
46 if (vlib_get_thread_index () == 0)
52 proxy_connect_args_t args;
53 args.api_context = a->api_context;
54 args.app_index = a->app_index;
55 clib_memcpy (args.uri, a->uri, vec_len (a->uri));
56 vl_api_rpc_call_main_thread (proxy_cb_fn, (u8 *) & args, sizeof (args));
61 delete_proxy_session (stream_session_t * s, int is_active_open)
63 proxy_main_t *pm = &proxy_main;
64 proxy_session_t *ps = 0;
65 vnet_disconnect_args_t _a, *a = &_a;
66 stream_session_t *active_open_session = 0;
67 stream_session_t *server_session = 0;
71 handle = session_handle (s);
73 clib_spinlock_lock_if_init (&pm->sessions_lock);
76 active_open_session = 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 ps = pool_elt_at_index (pm->sessions, p[0]);
88 if (ps->vpp_server_handle != ~0)
89 server_session = session_get_from_handle (ps->vpp_server_handle);
98 p = hash_get (pm->proxy_session_by_server_handle, handle);
101 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
102 is_active_open ? "active open" : "server",
105 else if (!pool_is_free_index (pm->sessions, p[0]))
107 ps = pool_elt_at_index (pm->sessions, p[0]);
108 if (ps->vpp_active_open_handle != ~0)
109 active_open_session = session_get_from_handle
110 (ps->vpp_active_open_handle);
112 active_open_session = 0;
119 memset (ps, 0xFE, sizeof (*ps));
120 pool_put (pm->sessions, ps);
123 clib_spinlock_unlock_if_init (&pm->sessions_lock);
125 if (active_open_session)
127 a->handle = session_handle (active_open_session);
128 a->app_index = pm->active_open_app_index;
129 hash_unset (pm->proxy_session_by_active_open_handle,
130 session_handle (active_open_session));
131 vnet_disconnect_session (a);
136 a->handle = session_handle (server_session);
137 a->app_index = pm->server_app_index;
138 hash_unset (pm->proxy_session_by_server_handle,
139 session_handle (server_session));
140 vnet_disconnect_session (a);
145 proxy_accept_callback (stream_session_t * s)
147 proxy_main_t *pm = &proxy_main;
149 s->session_state = SESSION_STATE_READY;
151 clib_spinlock_lock_if_init (&pm->sessions_lock);
157 proxy_disconnect_callback (stream_session_t * s)
159 delete_proxy_session (s, 0 /* is_active_open */ );
163 proxy_reset_callback (stream_session_t * s)
165 clib_warning ("Reset session %U", format_stream_session, s, 2);
166 delete_proxy_session (s, 0 /* is_active_open */ );
170 proxy_connected_callback (u32 app_index, u32 api_context,
171 stream_session_t * s, u8 is_fail)
173 clib_warning ("called...");
178 proxy_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
180 clib_warning ("called...");
185 proxy_rx_callback (stream_session_t * s)
188 int actual_transfer __attribute__ ((unused));
189 svm_fifo_t *tx_fifo, *rx_fifo;
190 proxy_main_t *pm = &proxy_main;
191 u32 thread_index = vlib_get_thread_index ();
192 vnet_connect_args_t _a, *a = &_a;
196 svm_fifo_t *active_open_tx_fifo;
198 ASSERT (s->thread_index == thread_index);
200 clib_spinlock_lock_if_init (&pm->sessions_lock);
201 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
203 if (PREDICT_TRUE (p != 0))
205 clib_spinlock_unlock_if_init (&pm->sessions_lock);
206 active_open_tx_fifo = s->server_rx_fifo;
209 * Send event for active open tx fifo
211 if (svm_fifo_set_event (active_open_tx_fifo))
213 u32 ao_thread_index = active_open_tx_fifo->master_thread_index;
214 if (session_send_io_evt_to_thread_custom (active_open_tx_fifo,
217 clib_warning ("failed to enqueue tx evt");
222 rx_fifo = s->server_rx_fifo;
223 tx_fifo = s->server_tx_fifo;
225 ASSERT (rx_fifo->master_thread_index == thread_index);
226 ASSERT (tx_fifo->master_thread_index == thread_index);
228 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
230 if (PREDICT_FALSE (max_dequeue == 0))
233 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
234 max_dequeue, pm->rx_buf[thread_index]);
236 /* $$$ your message in this space: parse url, etc. */
238 memset (a, 0, sizeof (*a));
240 clib_spinlock_lock_if_init (&pm->sessions_lock);
241 pool_get (pm->sessions, ps);
242 memset (ps, 0, sizeof (*ps));
243 ps->server_rx_fifo = rx_fifo;
244 ps->server_tx_fifo = tx_fifo;
245 ps->vpp_server_handle = session_handle (s);
247 proxy_index = ps - pm->sessions;
249 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
252 clib_spinlock_unlock_if_init (&pm->sessions_lock);
254 a->uri = (char *) pm->client_uri;
255 a->api_context = proxy_index;
256 a->app_index = pm->active_open_app_index;
257 proxy_call_main_thread (a);
263 static session_cb_vft_t proxy_session_cb_vft = {
264 .session_accept_callback = proxy_accept_callback,
265 .session_disconnect_callback = proxy_disconnect_callback,
266 .session_connected_callback = proxy_connected_callback,
267 .add_segment_callback = proxy_add_segment_callback,
268 .builtin_app_rx_callback = proxy_rx_callback,
269 .session_reset_callback = proxy_reset_callback
273 active_open_connected_callback (u32 app_index, u32 opaque,
274 stream_session_t * s, u8 is_fail)
276 proxy_main_t *pm = &proxy_main;
278 u8 thread_index = vlib_get_thread_index ();
282 clib_warning ("connection %d failed!", opaque);
287 * Setup proxy session handle.
289 clib_spinlock_lock_if_init (&pm->sessions_lock);
291 ps = pool_elt_at_index (pm->sessions, opaque);
292 ps->vpp_active_open_handle = session_handle (s);
294 s->server_tx_fifo = ps->server_rx_fifo;
295 s->server_rx_fifo = ps->server_tx_fifo;
298 * Reset the active-open tx-fifo master indices so the active-open session
299 * will receive data, etc.
301 s->server_tx_fifo->master_session_index = s->session_index;
302 s->server_tx_fifo->master_thread_index = s->thread_index;
305 * Account for the active-open session's use of the fifos
306 * so they won't disappear until the last session which uses
309 s->server_tx_fifo->refcnt++;
310 s->server_rx_fifo->refcnt++;
312 hash_set (pm->proxy_session_by_active_open_handle,
313 ps->vpp_active_open_handle, opaque);
315 clib_spinlock_unlock_if_init (&pm->sessions_lock);
318 * Send event for active open tx fifo
320 ASSERT (s->thread_index == thread_index);
321 if (svm_fifo_set_event (s->server_tx_fifo))
322 session_send_io_evt_to_thread (s->server_tx_fifo, FIFO_EVENT_APP_TX);
328 active_open_reset_callback (stream_session_t * s)
330 delete_proxy_session (s, 1 /* is_active_open */ );
334 active_open_create_callback (stream_session_t * s)
340 active_open_disconnect_callback (stream_session_t * s)
342 delete_proxy_session (s, 1 /* is_active_open */ );
346 active_open_rx_callback (stream_session_t * s)
348 svm_fifo_t *proxy_tx_fifo;
350 proxy_tx_fifo = s->server_rx_fifo;
353 * Send event for server tx fifo
355 if (svm_fifo_set_event (proxy_tx_fifo))
357 u8 thread_index = proxy_tx_fifo->master_thread_index;
358 return session_send_io_evt_to_thread_custom (proxy_tx_fifo,
367 static session_cb_vft_t active_open_clients = {
368 .session_reset_callback = active_open_reset_callback,
369 .session_connected_callback = active_open_connected_callback,
370 .session_accept_callback = active_open_create_callback,
371 .session_disconnect_callback = active_open_disconnect_callback,
372 .builtin_app_rx_callback = active_open_rx_callback
378 create_api_loopbacks (vlib_main_t * vm)
380 proxy_main_t *pm = &proxy_main;
381 api_main_t *am = &api_main;
382 vl_shmem_hdr_t *shmem_hdr;
384 shmem_hdr = am->shmem_hdr;
385 pm->vl_input_queue = shmem_hdr->vl_input_queue;
386 pm->server_client_index =
387 vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
388 pm->active_open_client_index =
389 vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
393 proxy_server_attach ()
395 proxy_main_t *pm = &proxy_main;
396 u64 options[APP_OPTIONS_N_OPTIONS];
397 vnet_app_attach_args_t _a, *a = &_a;
398 u32 segment_size = 512 << 20;
400 memset (a, 0, sizeof (*a));
401 memset (options, 0, sizeof (options));
403 if (pm->private_segment_size)
404 segment_size = pm->private_segment_size;
405 a->api_client_index = pm->server_client_index;
406 a->session_cb_vft = &proxy_session_cb_vft;
407 a->options = options;
408 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
409 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
410 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
411 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
412 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
413 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
415 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
417 if (vnet_application_attach (a))
419 clib_warning ("failed to attach server");
422 pm->server_app_index = a->app_index;
428 active_open_attach (void)
430 proxy_main_t *pm = &proxy_main;
431 vnet_app_attach_args_t _a, *a = &_a;
434 memset (a, 0, sizeof (*a));
435 memset (options, 0, sizeof (options));
437 a->api_client_index = pm->active_open_client_index;
438 a->session_cb_vft = &active_open_clients;
440 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
441 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
442 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
443 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
444 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
445 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
446 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
448 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
449 | APP_OPTIONS_FLAGS_IS_PROXY;
451 a->options = options;
453 if (vnet_application_attach (a))
456 pm->active_open_app_index = a->app_index;
462 proxy_server_listen ()
464 proxy_main_t *pm = &proxy_main;
465 vnet_bind_args_t _a, *a = &_a;
466 memset (a, 0, sizeof (*a));
467 a->app_index = pm->server_app_index;
468 a->uri = (char *) pm->server_uri;
469 return vnet_bind_uri (a);
473 proxy_server_create (vlib_main_t * vm)
475 proxy_main_t *pm = &proxy_main;
476 vlib_thread_main_t *vtm = vlib_get_thread_main ();
480 if (pm->server_client_index == (u32) ~ 0)
481 create_api_loopbacks (vm);
483 num_threads = 1 /* main thread */ + vtm->n_threads;
484 vec_validate (proxy_main.server_event_queue, num_threads - 1);
485 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
486 vec_validate (pm->rx_buf, num_threads - 1);
488 for (i = 0; i < num_threads; i++)
489 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
491 if (proxy_server_attach ())
493 clib_warning ("failed to attach server app");
496 if (proxy_server_listen ())
498 clib_warning ("failed to start listening");
501 if (active_open_attach ())
503 clib_warning ("failed to attach active open app");
507 for (i = 0; i < num_threads; i++)
509 pm->active_open_event_queue[i] =
510 session_manager_get_vpp_event_queue (i);
512 ASSERT (pm->active_open_event_queue[i]);
514 pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
520 static clib_error_t *
521 proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
522 vlib_cli_command_t * cmd)
524 proxy_main_t *pm = &proxy_main;
525 char *default_server_uri = "tcp://0.0.0.0/23";
526 char *default_client_uri = "tcp://6.0.2.2/23";
530 pm->fifo_size = 64 << 10;
531 pm->rcv_buffer_size = 1024;
532 pm->prealloc_fifos = 0;
533 pm->private_segment_count = 0;
534 pm->private_segment_size = 0;
537 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
539 if (unformat (input, "fifo-size %d", &pm->fifo_size))
540 pm->fifo_size <<= 10;
541 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
543 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
545 else if (unformat (input, "private-segment-count %d",
546 &pm->private_segment_count))
548 else if (unformat (input, "private-segment-size %U",
549 unformat_memory_size, &tmp))
551 if (tmp >= 0x100000000ULL)
552 return clib_error_return
553 (0, "private segment size %lld (%llu) too large", tmp, tmp);
554 pm->private_segment_size = tmp;
556 else if (unformat (input, "server-uri %s", &pm->server_uri))
558 else if (unformat (input, "client-uri %s", &pm->client_uri))
559 pm->client_uri = format (0, "%s%c", pm->client_uri, 0);
561 return clib_error_return (0, "unknown input `%U'",
562 format_unformat_error, input);
567 clib_warning ("No server-uri provided, Using default: %s",
569 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
573 clib_warning ("No client-uri provided, Using default: %s",
575 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
578 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
580 rv = proxy_server_create (vm);
586 return clib_error_return (0, "server_create returned %d", rv);
593 VLIB_CLI_COMMAND (proxy_create_command, static) =
595 .path = "test proxy server",
596 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
597 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
598 "[prealloc-fifos <nn>][private-segment-size <mem>]"
599 "[private-segment-count <nn>]",
600 .function = proxy_server_create_command_fn,
605 proxy_main_init (vlib_main_t * vm)
607 proxy_main_t *pm = &proxy_main;
608 pm->server_client_index = ~0;
609 pm->active_open_client_index = ~0;
610 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
611 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
616 VLIB_INIT_FUNCTION (proxy_main_init);
619 * fd.io coding-style-patch-verification: ON
622 * eval: (c-set-style "gnu")