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>
21 #include <vnet/tcp/tcp.h>
23 proxy_main_t proxy_main;
32 } proxy_connect_args_t;
35 proxy_cb_fn (void *data, u32 data_len)
37 proxy_connect_args_t *pa = (proxy_connect_args_t *) data;
38 vnet_connect_args_t a;
40 memset (&a, 0, sizeof (a));
41 a.api_context = pa->api_context;
42 a.app_index = pa->app_index;
44 vnet_connect_uri (&a);
48 proxy_call_main_thread (vnet_connect_args_t * a)
50 if (vlib_get_thread_index () == 0)
56 proxy_connect_args_t args;
57 args.api_context = a->api_context;
58 args.app_index = a->app_index;
59 clib_memcpy (args.uri, a->uri, vec_len (a->uri));
60 vl_api_rpc_call_main_thread (proxy_cb_fn, (u8 *) & args, sizeof (args));
65 delete_proxy_session (session_t * s, int is_active_open)
67 proxy_main_t *pm = &proxy_main;
68 proxy_session_t *ps = 0;
69 vnet_disconnect_args_t _a, *a = &_a;
70 session_t *active_open_session = 0;
71 session_t *server_session = 0;
75 clib_spinlock_lock_if_init (&pm->sessions_lock);
77 handle = session_handle (s);
81 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
84 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
85 is_active_open ? "active open" : "server",
88 else if (!pool_is_free_index (pm->sessions, p[0]))
90 active_open_session = s;
91 ps = pool_elt_at_index (pm->sessions, p[0]);
92 if (ps->vpp_server_handle != ~0)
93 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]))
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);
118 clib_memset (ps, 0xFE, sizeof (*ps));
119 pool_put (pm->sessions, ps);
122 if (active_open_session)
124 a->handle = session_handle (active_open_session);
125 a->app_index = pm->active_open_app_index;
126 hash_unset (pm->proxy_session_by_active_open_handle,
127 session_handle (active_open_session));
128 vnet_disconnect_session (a);
133 a->handle = session_handle (server_session);
134 a->app_index = pm->server_app_index;
135 hash_unset (pm->proxy_session_by_server_handle,
136 session_handle (server_session));
137 vnet_disconnect_session (a);
140 clib_spinlock_unlock_if_init (&pm->sessions_lock);
144 proxy_accept_callback (session_t * s)
146 proxy_main_t *pm = &proxy_main;
148 s->session_state = SESSION_STATE_READY;
150 clib_spinlock_lock_if_init (&pm->sessions_lock);
156 proxy_disconnect_callback (session_t * s)
158 delete_proxy_session (s, 0 /* is_active_open */ );
162 proxy_reset_callback (session_t * s)
164 clib_warning ("Reset session %U", format_session, s, 2);
165 delete_proxy_session (s, 0 /* is_active_open */ );
169 proxy_connected_callback (u32 app_index, u32 api_context,
170 session_t * s, u8 is_fail)
172 clib_warning ("called...");
177 proxy_add_segment_callback (u32 client_index, u64 segment_handle)
179 clib_warning ("called...");
184 proxy_rx_callback (session_t * s)
187 int actual_transfer __attribute__ ((unused));
188 svm_fifo_t *tx_fifo, *rx_fifo;
189 proxy_main_t *pm = &proxy_main;
190 u32 thread_index = vlib_get_thread_index ();
191 vnet_connect_args_t _a, *a = &_a;
195 svm_fifo_t *ao_tx_fifo;
197 ASSERT (s->thread_index == thread_index);
199 clib_spinlock_lock_if_init (&pm->sessions_lock);
200 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
202 if (PREDICT_TRUE (p != 0))
204 clib_spinlock_unlock_if_init (&pm->sessions_lock);
205 ao_tx_fifo = s->rx_fifo;
208 * Send event for active open tx fifo
210 if (svm_fifo_set_event (ao_tx_fifo))
212 u32 ao_thread_index = ao_tx_fifo->master_thread_index;
213 u32 ao_session_index = ao_tx_fifo->master_session_index;
214 if (session_send_io_evt_to_thread_custom (&ao_session_index,
217 clib_warning ("failed to enqueue tx evt");
220 if (svm_fifo_max_enqueue (ao_tx_fifo) <= TCP_MSS)
221 svm_fifo_add_want_deq_ntf (ao_tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
225 rx_fifo = s->rx_fifo;
226 tx_fifo = s->tx_fifo;
228 ASSERT (rx_fifo->master_thread_index == thread_index);
229 ASSERT (tx_fifo->master_thread_index == thread_index);
231 max_dequeue = svm_fifo_max_dequeue_cons (s->rx_fifo);
233 if (PREDICT_FALSE (max_dequeue == 0))
236 max_dequeue = clib_min (pm->rcv_buffer_size, max_dequeue);
237 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
238 max_dequeue, pm->rx_buf[thread_index]);
240 /* $$$ your message in this space: parse url, etc. */
242 clib_memset (a, 0, sizeof (*a));
244 pool_get (pm->sessions, ps);
245 clib_memset (ps, 0, sizeof (*ps));
246 ps->server_rx_fifo = rx_fifo;
247 ps->server_tx_fifo = tx_fifo;
248 ps->vpp_server_handle = session_handle (s);
250 proxy_index = ps - pm->sessions;
252 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
255 clib_spinlock_unlock_if_init (&pm->sessions_lock);
257 a->uri = (char *) pm->client_uri;
258 a->api_context = proxy_index;
259 a->app_index = pm->active_open_app_index;
260 proxy_call_main_thread (a);
267 proxy_tx_callback (session_t * proxy_s)
269 proxy_main_t *pm = &proxy_main;
270 transport_connection_t *tc;
271 session_handle_t handle;
277 min_free = clib_min (proxy_s->tx_fifo->nitems >> 3, 128 << 10);
278 if (svm_fifo_max_enqueue (proxy_s->tx_fifo) < min_free)
280 svm_fifo_add_want_deq_ntf (proxy_s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
284 clib_spinlock_lock_if_init (&pm->sessions_lock);
286 handle = session_handle (proxy_s);
287 p = hash_get (pm->proxy_session_by_server_handle, handle);
291 if (pool_is_free_index (pm->sessions, p[0]))
294 ps = pool_elt_at_index (pm->sessions, p[0]);
295 if (ps->vpp_active_open_handle == ~0)
298 ao_s = session_get_from_handle (ps->vpp_active_open_handle);
300 /* Force ack on active open side to update rcv wnd */
301 tc = session_get_transport (ao_s);
302 tcp_send_ack ((tcp_connection_t *) tc);
304 clib_spinlock_unlock_if_init (&pm->sessions_lock);
309 static session_cb_vft_t proxy_session_cb_vft = {
310 .session_accept_callback = proxy_accept_callback,
311 .session_disconnect_callback = proxy_disconnect_callback,
312 .session_connected_callback = proxy_connected_callback,
313 .add_segment_callback = proxy_add_segment_callback,
314 .builtin_app_rx_callback = proxy_rx_callback,
315 .builtin_app_tx_callback = proxy_tx_callback,
316 .session_reset_callback = proxy_reset_callback
320 active_open_connected_callback (u32 app_index, u32 opaque,
321 session_t * s, u8 is_fail)
323 proxy_main_t *pm = &proxy_main;
325 u8 thread_index = vlib_get_thread_index ();
329 clib_warning ("connection %d failed!", opaque);
334 * Setup proxy session handle.
336 clib_spinlock_lock_if_init (&pm->sessions_lock);
338 ps = pool_elt_at_index (pm->sessions, opaque);
339 ps->vpp_active_open_handle = session_handle (s);
341 s->tx_fifo = ps->server_rx_fifo;
342 s->rx_fifo = ps->server_tx_fifo;
345 * Reset the active-open tx-fifo master indices so the active-open session
346 * will receive data, etc.
348 s->tx_fifo->master_session_index = s->session_index;
349 s->tx_fifo->master_thread_index = s->thread_index;
352 * Account for the active-open session's use of the fifos
353 * so they won't disappear until the last session which uses
356 s->tx_fifo->refcnt++;
357 s->rx_fifo->refcnt++;
359 hash_set (pm->proxy_session_by_active_open_handle,
360 ps->vpp_active_open_handle, opaque);
362 clib_spinlock_unlock_if_init (&pm->sessions_lock);
365 * Send event for active open tx fifo
367 ASSERT (s->thread_index == thread_index);
368 if (svm_fifo_set_event (s->tx_fifo))
369 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
375 active_open_reset_callback (session_t * s)
377 delete_proxy_session (s, 1 /* is_active_open */ );
381 active_open_create_callback (session_t * s)
387 active_open_disconnect_callback (session_t * s)
389 delete_proxy_session (s, 1 /* is_active_open */ );
393 active_open_rx_callback (session_t * s)
395 svm_fifo_t *proxy_tx_fifo;
397 proxy_tx_fifo = s->rx_fifo;
400 * Send event for server tx fifo
402 if (svm_fifo_set_event (proxy_tx_fifo))
404 u8 thread_index = proxy_tx_fifo->master_thread_index;
405 u32 session_index = proxy_tx_fifo->master_session_index;
406 return session_send_io_evt_to_thread_custom (&session_index,
411 if (svm_fifo_max_enqueue (proxy_tx_fifo) <= TCP_MSS)
412 svm_fifo_add_want_deq_ntf (proxy_tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
418 active_open_tx_callback (session_t * ao_s)
420 proxy_main_t *pm = &proxy_main;
421 transport_connection_t *tc;
422 session_handle_t handle;
428 min_free = clib_min (ao_s->tx_fifo->nitems >> 3, 128 << 10);
429 if (svm_fifo_max_enqueue (ao_s->tx_fifo) < min_free)
431 svm_fifo_add_want_deq_ntf (ao_s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
435 clib_spinlock_lock_if_init (&pm->sessions_lock);
437 handle = session_handle (ao_s);
438 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
442 if (pool_is_free_index (pm->sessions, p[0]))
445 ps = pool_elt_at_index (pm->sessions, p[0]);
446 if (ps->vpp_server_handle == ~0)
449 proxy_s = session_get_from_handle (ps->vpp_server_handle);
451 /* Force ack on proxy side to update rcv wnd */
452 tc = session_get_transport (proxy_s);
453 tcp_send_ack ((tcp_connection_t *) tc);
455 clib_spinlock_unlock_if_init (&pm->sessions_lock);
461 static session_cb_vft_t active_open_clients = {
462 .session_reset_callback = active_open_reset_callback,
463 .session_connected_callback = active_open_connected_callback,
464 .session_accept_callback = active_open_create_callback,
465 .session_disconnect_callback = active_open_disconnect_callback,
466 .builtin_app_rx_callback = active_open_rx_callback,
467 .builtin_app_tx_callback = active_open_tx_callback,
472 proxy_server_attach ()
474 proxy_main_t *pm = &proxy_main;
475 u64 options[APP_OPTIONS_N_OPTIONS];
476 vnet_app_attach_args_t _a, *a = &_a;
477 u32 segment_size = 512 << 20;
479 clib_memset (a, 0, sizeof (*a));
480 clib_memset (options, 0, sizeof (options));
482 if (pm->private_segment_size)
483 segment_size = pm->private_segment_size;
484 a->name = format (0, "proxy-server");
485 a->api_client_index = pm->server_client_index;
486 a->session_cb_vft = &proxy_session_cb_vft;
487 a->options = options;
488 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
489 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
490 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
491 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
492 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
493 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
495 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
497 if (vnet_application_attach (a))
499 clib_warning ("failed to attach server");
502 pm->server_app_index = a->app_index;
509 active_open_attach (void)
511 proxy_main_t *pm = &proxy_main;
512 vnet_app_attach_args_t _a, *a = &_a;
515 clib_memset (a, 0, sizeof (*a));
516 clib_memset (options, 0, sizeof (options));
518 a->api_client_index = pm->active_open_client_index;
519 a->session_cb_vft = &active_open_clients;
520 a->name = format (0, "proxy-active-open");
522 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
523 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
524 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
525 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
526 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
527 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
528 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
530 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
531 | APP_OPTIONS_FLAGS_IS_PROXY;
533 a->options = options;
535 if (vnet_application_attach (a))
538 pm->active_open_app_index = a->app_index;
546 proxy_server_listen ()
548 proxy_main_t *pm = &proxy_main;
549 vnet_listen_args_t _a, *a = &_a;
550 clib_memset (a, 0, sizeof (*a));
551 a->app_index = pm->server_app_index;
552 a->uri = (char *) pm->server_uri;
553 return vnet_bind_uri (a);
557 proxy_server_create (vlib_main_t * vm)
559 proxy_main_t *pm = &proxy_main;
560 vlib_thread_main_t *vtm = vlib_get_thread_main ();
564 num_threads = 1 /* main thread */ + vtm->n_threads;
565 vec_validate (proxy_main.server_event_queue, num_threads - 1);
566 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
567 vec_validate (pm->rx_buf, num_threads - 1);
569 for (i = 0; i < num_threads; i++)
570 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
572 if (proxy_server_attach ())
574 clib_warning ("failed to attach server app");
577 if (proxy_server_listen ())
579 clib_warning ("failed to start listening");
582 if (active_open_attach ())
584 clib_warning ("failed to attach active open app");
588 for (i = 0; i < num_threads; i++)
590 pm->active_open_event_queue[i] = session_main_get_vpp_event_queue (i);
592 ASSERT (pm->active_open_event_queue[i]);
594 pm->server_event_queue[i] = session_main_get_vpp_event_queue (i);
600 static clib_error_t *
601 proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
602 vlib_cli_command_t * cmd)
604 proxy_main_t *pm = &proxy_main;
605 char *default_server_uri = "tcp://0.0.0.0/23";
606 char *default_client_uri = "tcp://6.0.2.2/23";
610 pm->fifo_size = 64 << 10;
611 pm->rcv_buffer_size = 1024;
612 pm->prealloc_fifos = 0;
613 pm->private_segment_count = 0;
614 pm->private_segment_size = 0;
618 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
620 if (unformat (input, "fifo-size %d", &pm->fifo_size))
621 pm->fifo_size <<= 10;
622 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
624 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
626 else if (unformat (input, "private-segment-count %d",
627 &pm->private_segment_count))
629 else if (unformat (input, "private-segment-size %U",
630 unformat_memory_size, &tmp))
632 if (tmp >= 0x100000000ULL)
633 return clib_error_return
634 (0, "private segment size %lld (%llu) too large", tmp, tmp);
635 pm->private_segment_size = tmp;
637 else if (unformat (input, "server-uri %s", &pm->server_uri))
638 vec_add1 (pm->server_uri, 0);
639 else if (unformat (input, "client-uri %s", &pm->client_uri))
640 vec_add1 (pm->client_uri, 0);
642 return clib_error_return (0, "unknown input `%U'",
643 format_unformat_error, input);
648 clib_warning ("No server-uri provided, Using default: %s",
650 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
654 clib_warning ("No client-uri provided, Using default: %s",
656 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
659 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
661 rv = proxy_server_create (vm);
667 return clib_error_return (0, "server_create returned %d", rv);
674 VLIB_CLI_COMMAND (proxy_create_command, static) =
676 .path = "test proxy server",
677 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
678 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
679 "[prealloc-fifos <nn>][private-segment-size <mem>]"
680 "[private-segment-count <nn>]",
681 .function = proxy_server_create_command_fn,
686 proxy_main_init (vlib_main_t * vm)
688 proxy_main_t *pm = &proxy_main;
689 pm->server_client_index = ~0;
690 pm->active_open_client_index = ~0;
691 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
692 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
697 VLIB_INIT_FUNCTION (proxy_main_init);
700 * fd.io coding-style-patch-verification: ON
703 * eval: (c-set-style "gnu")