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 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 (stream_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 stream_session_t *active_open_session = 0;
68 stream_session_t *server_session = 0;
72 handle = session_handle (s);
74 clib_spinlock_lock_if_init (&pm->sessions_lock);
77 active_open_session = s;
79 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
82 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
83 is_active_open ? "active open" : "server",
86 else if (!pool_is_free_index (pm->sessions, p[0]))
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);
99 p = hash_get (pm->proxy_session_by_server_handle, handle);
102 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
103 is_active_open ? "active open" : "server",
106 else if (!pool_is_free_index (pm->sessions, p[0]))
108 ps = pool_elt_at_index (pm->sessions, p[0]);
109 if (ps->vpp_active_open_handle != ~0)
110 active_open_session = session_get_from_handle
111 (ps->vpp_active_open_handle);
113 active_open_session = 0;
120 clib_memset (ps, 0xFE, sizeof (*ps));
121 pool_put (pm->sessions, ps);
124 clib_spinlock_unlock_if_init (&pm->sessions_lock);
126 if (active_open_session)
128 a->handle = session_handle (active_open_session);
129 a->app_index = pm->active_open_app_index;
130 hash_unset (pm->proxy_session_by_active_open_handle,
131 session_handle (active_open_session));
132 vnet_disconnect_session (a);
137 a->handle = session_handle (server_session);
138 a->app_index = pm->server_app_index;
139 hash_unset (pm->proxy_session_by_server_handle,
140 session_handle (server_session));
141 vnet_disconnect_session (a);
146 proxy_accept_callback (stream_session_t * s)
148 proxy_main_t *pm = &proxy_main;
150 s->session_state = SESSION_STATE_READY;
152 clib_spinlock_lock_if_init (&pm->sessions_lock);
158 proxy_disconnect_callback (stream_session_t * s)
160 delete_proxy_session (s, 0 /* is_active_open */ );
164 proxy_reset_callback (stream_session_t * s)
166 clib_warning ("Reset session %U", format_stream_session, s, 2);
167 delete_proxy_session (s, 0 /* is_active_open */ );
171 proxy_connected_callback (u32 app_index, u32 api_context,
172 stream_session_t * s, u8 is_fail)
174 clib_warning ("called...");
179 proxy_add_segment_callback (u32 client_index, u64 segment_handle)
181 clib_warning ("called...");
186 proxy_rx_callback (stream_session_t * s)
189 int actual_transfer __attribute__ ((unused));
190 svm_fifo_t *tx_fifo, *rx_fifo;
191 proxy_main_t *pm = &proxy_main;
192 u32 thread_index = vlib_get_thread_index ();
193 vnet_connect_args_t _a, *a = &_a;
197 svm_fifo_t *active_open_tx_fifo;
199 ASSERT (s->thread_index == thread_index);
201 clib_spinlock_lock_if_init (&pm->sessions_lock);
202 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
204 if (PREDICT_TRUE (p != 0))
206 clib_spinlock_unlock_if_init (&pm->sessions_lock);
207 active_open_tx_fifo = s->server_rx_fifo;
210 * Send event for active open tx fifo
212 if (svm_fifo_set_event (active_open_tx_fifo))
214 u32 ao_thread_index = active_open_tx_fifo->master_thread_index;
215 if (session_send_io_evt_to_thread_custom (active_open_tx_fifo,
218 clib_warning ("failed to enqueue tx evt");
223 rx_fifo = s->server_rx_fifo;
224 tx_fifo = s->server_tx_fifo;
226 ASSERT (rx_fifo->master_thread_index == thread_index);
227 ASSERT (tx_fifo->master_thread_index == thread_index);
229 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
231 if (PREDICT_FALSE (max_dequeue == 0))
234 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
235 max_dequeue, pm->rx_buf[thread_index]);
237 /* $$$ your message in this space: parse url, etc. */
239 clib_memset (a, 0, sizeof (*a));
241 clib_spinlock_lock_if_init (&pm->sessions_lock);
242 pool_get (pm->sessions, ps);
243 clib_memset (ps, 0, sizeof (*ps));
244 ps->server_rx_fifo = rx_fifo;
245 ps->server_tx_fifo = tx_fifo;
246 ps->vpp_server_handle = session_handle (s);
248 proxy_index = ps - pm->sessions;
250 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
253 clib_spinlock_unlock_if_init (&pm->sessions_lock);
255 a->uri = (char *) pm->client_uri;
256 a->api_context = proxy_index;
257 a->app_index = pm->active_open_app_index;
258 proxy_call_main_thread (a);
264 static session_cb_vft_t proxy_session_cb_vft = {
265 .session_accept_callback = proxy_accept_callback,
266 .session_disconnect_callback = proxy_disconnect_callback,
267 .session_connected_callback = proxy_connected_callback,
268 .add_segment_callback = proxy_add_segment_callback,
269 .builtin_app_rx_callback = proxy_rx_callback,
270 .session_reset_callback = proxy_reset_callback
274 active_open_connected_callback (u32 app_index, u32 opaque,
275 stream_session_t * s, u8 is_fail)
277 proxy_main_t *pm = &proxy_main;
279 u8 thread_index = vlib_get_thread_index ();
283 clib_warning ("connection %d failed!", opaque);
288 * Setup proxy session handle.
290 clib_spinlock_lock_if_init (&pm->sessions_lock);
292 ps = pool_elt_at_index (pm->sessions, opaque);
293 ps->vpp_active_open_handle = session_handle (s);
295 s->server_tx_fifo = ps->server_rx_fifo;
296 s->server_rx_fifo = ps->server_tx_fifo;
299 * Reset the active-open tx-fifo master indices so the active-open session
300 * will receive data, etc.
302 s->server_tx_fifo->master_session_index = s->session_index;
303 s->server_tx_fifo->master_thread_index = s->thread_index;
306 * Account for the active-open session's use of the fifos
307 * so they won't disappear until the last session which uses
310 s->server_tx_fifo->refcnt++;
311 s->server_rx_fifo->refcnt++;
313 hash_set (pm->proxy_session_by_active_open_handle,
314 ps->vpp_active_open_handle, opaque);
316 clib_spinlock_unlock_if_init (&pm->sessions_lock);
319 * Send event for active open tx fifo
321 ASSERT (s->thread_index == thread_index);
322 if (svm_fifo_set_event (s->server_tx_fifo))
323 session_send_io_evt_to_thread (s->server_tx_fifo, FIFO_EVENT_APP_TX);
329 active_open_reset_callback (stream_session_t * s)
331 delete_proxy_session (s, 1 /* is_active_open */ );
335 active_open_create_callback (stream_session_t * s)
341 active_open_disconnect_callback (stream_session_t * s)
343 delete_proxy_session (s, 1 /* is_active_open */ );
347 active_open_rx_callback (stream_session_t * s)
349 svm_fifo_t *proxy_tx_fifo;
351 proxy_tx_fifo = s->server_rx_fifo;
354 * Send event for server tx fifo
356 if (svm_fifo_set_event (proxy_tx_fifo))
358 u8 thread_index = proxy_tx_fifo->master_thread_index;
359 return session_send_io_evt_to_thread_custom (proxy_tx_fifo,
368 static session_cb_vft_t active_open_clients = {
369 .session_reset_callback = active_open_reset_callback,
370 .session_connected_callback = active_open_connected_callback,
371 .session_accept_callback = active_open_create_callback,
372 .session_disconnect_callback = active_open_disconnect_callback,
373 .builtin_app_rx_callback = active_open_rx_callback
379 create_api_loopbacks (vlib_main_t * vm)
381 proxy_main_t *pm = &proxy_main;
382 api_main_t *am = &api_main;
383 vl_shmem_hdr_t *shmem_hdr;
385 shmem_hdr = am->shmem_hdr;
386 pm->vl_input_queue = shmem_hdr->vl_input_queue;
387 pm->server_client_index =
388 vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
389 pm->active_open_client_index =
390 vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
394 proxy_server_attach ()
396 proxy_main_t *pm = &proxy_main;
397 u64 options[APP_OPTIONS_N_OPTIONS];
398 vnet_app_attach_args_t _a, *a = &_a;
399 u32 segment_size = 512 << 20;
401 clib_memset (a, 0, sizeof (*a));
402 clib_memset (options, 0, sizeof (options));
404 if (pm->private_segment_size)
405 segment_size = pm->private_segment_size;
406 a->api_client_index = pm->server_client_index;
407 a->session_cb_vft = &proxy_session_cb_vft;
408 a->options = options;
409 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
410 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
411 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
412 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
413 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
414 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
416 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
418 if (vnet_application_attach (a))
420 clib_warning ("failed to attach server");
423 pm->server_app_index = a->app_index;
429 active_open_attach (void)
431 proxy_main_t *pm = &proxy_main;
432 vnet_app_attach_args_t _a, *a = &_a;
435 clib_memset (a, 0, sizeof (*a));
436 clib_memset (options, 0, sizeof (options));
438 a->api_client_index = pm->active_open_client_index;
439 a->session_cb_vft = &active_open_clients;
441 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
442 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
443 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
444 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
445 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
446 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
447 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
449 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
450 | APP_OPTIONS_FLAGS_IS_PROXY;
452 a->options = options;
454 if (vnet_application_attach (a))
457 pm->active_open_app_index = a->app_index;
463 proxy_server_listen ()
465 proxy_main_t *pm = &proxy_main;
466 vnet_bind_args_t _a, *a = &_a;
467 clib_memset (a, 0, sizeof (*a));
468 a->app_index = pm->server_app_index;
469 a->uri = (char *) pm->server_uri;
470 return vnet_bind_uri (a);
474 proxy_server_create (vlib_main_t * vm)
476 proxy_main_t *pm = &proxy_main;
477 vlib_thread_main_t *vtm = vlib_get_thread_main ();
481 if (pm->server_client_index == (u32) ~ 0)
482 create_api_loopbacks (vm);
484 num_threads = 1 /* main thread */ + vtm->n_threads;
485 vec_validate (proxy_main.server_event_queue, num_threads - 1);
486 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
487 vec_validate (pm->rx_buf, num_threads - 1);
489 for (i = 0; i < num_threads; i++)
490 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
492 if (proxy_server_attach ())
494 clib_warning ("failed to attach server app");
497 if (proxy_server_listen ())
499 clib_warning ("failed to start listening");
502 if (active_open_attach ())
504 clib_warning ("failed to attach active open app");
508 for (i = 0; i < num_threads; i++)
510 pm->active_open_event_queue[i] =
511 session_manager_get_vpp_event_queue (i);
513 ASSERT (pm->active_open_event_queue[i]);
515 pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
521 static clib_error_t *
522 proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
523 vlib_cli_command_t * cmd)
525 proxy_main_t *pm = &proxy_main;
526 char *default_server_uri = "tcp://0.0.0.0/23";
527 char *default_client_uri = "tcp://6.0.2.2/23";
531 pm->fifo_size = 64 << 10;
532 pm->rcv_buffer_size = 1024;
533 pm->prealloc_fifos = 0;
534 pm->private_segment_count = 0;
535 pm->private_segment_size = 0;
538 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
540 if (unformat (input, "fifo-size %d", &pm->fifo_size))
541 pm->fifo_size <<= 10;
542 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
544 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
546 else if (unformat (input, "private-segment-count %d",
547 &pm->private_segment_count))
549 else if (unformat (input, "private-segment-size %U",
550 unformat_memory_size, &tmp))
552 if (tmp >= 0x100000000ULL)
553 return clib_error_return
554 (0, "private segment size %lld (%llu) too large", tmp, tmp);
555 pm->private_segment_size = tmp;
557 else if (unformat (input, "server-uri %s", &pm->server_uri))
559 else if (unformat (input, "client-uri %s", &pm->client_uri))
560 pm->client_uri = format (0, "%s%c", pm->client_uri, 0);
562 return clib_error_return (0, "unknown input `%U'",
563 format_unformat_error, input);
568 clib_warning ("No server-uri provided, Using default: %s",
570 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
574 clib_warning ("No client-uri provided, Using default: %s",
576 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
579 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
581 rv = proxy_server_create (vm);
587 return clib_error_return (0, "server_create returned %d", rv);
594 VLIB_CLI_COMMAND (proxy_create_command, static) =
596 .path = "test proxy server",
597 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
598 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
599 "[prealloc-fifos <nn>][private-segment-size <mem>]"
600 "[private-segment-count <nn>]",
601 .function = proxy_server_create_command_fn,
606 proxy_main_init (vlib_main_t * vm)
608 proxy_main_t *pm = &proxy_main;
609 pm->server_client_index = ~0;
610 pm->active_open_client_index = ~0;
611 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
612 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
617 VLIB_INIT_FUNCTION (proxy_main_init);
620 * fd.io coding-style-patch-verification: ON
623 * eval: (c-set-style "gnu")