aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/session
diff options
context:
space:
mode:
Diffstat (limited to 'src/vnet/session')
-rw-r--r--src/vnet/session/application.c657
-rw-r--r--src/vnet/session/application.h136
-rw-r--r--src/vnet/session/application_interface.c406
-rw-r--r--src/vnet/session/application_interface.h183
-rw-r--r--src/vnet/session/segment_manager.c636
-rw-r--r--src/vnet/session/segment_manager.h131
-rw-r--r--src/vnet/session/session.api331
-rw-r--r--src/vnet/session/session.c1036
-rw-r--r--src/vnet/session/session.h436
-rwxr-xr-xsrc/vnet/session/session_api.c763
-rwxr-xr-xsrc/vnet/session/session_cli.c494
-rw-r--r--src/vnet/session/session_debug.h142
-rw-r--r--src/vnet/session/session_lookup.c619
-rw-r--r--src/vnet/session/session_lookup.h100
-rw-r--r--src/vnet/session/session_node.c707
-rw-r--r--src/vnet/session/stream_session.h92
-rw-r--r--src/vnet/session/transport.h94
-rw-r--r--src/vnet/session/transport_interface.c109
-rw-r--r--src/vnet/session/transport_interface.h82
19 files changed, 7154 insertions, 0 deletions
diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c
new file mode 100644
index 00000000..2b789c5f
--- /dev/null
+++ b/src/vnet/session/application.c
@@ -0,0 +1,657 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <vnet/session/application.h>
+#include <vnet/session/application_interface.h>
+#include <vnet/session/session.h>
+
+/**
+ * Pool from which we allocate all applications
+ */
+static application_t *app_pool;
+
+/**
+ * Hash table of apps by api client index
+ */
+static uword *app_by_api_client_index;
+
+/**
+ * Default application event queue size
+ */
+static u32 default_app_evt_queue_size = 128;
+
+int
+application_api_queue_is_full (application_t * app)
+{
+ unix_shared_memory_queue_t *q;
+
+ /* builtin servers are always OK */
+ if (app->api_client_index == ~0)
+ return 0;
+
+ q = vl_api_client_index_to_input_queue (app->api_client_index);
+ if (!q)
+ return 1;
+
+ if (q->cursize == q->maxsize)
+ return 1;
+ return 0;
+}
+
+static void
+application_table_add (application_t * app)
+{
+ hash_set (app_by_api_client_index, app->api_client_index, app->index);
+}
+
+static void
+application_table_del (application_t * app)
+{
+ hash_unset (app_by_api_client_index, app->api_client_index);
+}
+
+application_t *
+application_lookup (u32 api_client_index)
+{
+ uword *p;
+ p = hash_get (app_by_api_client_index, api_client_index);
+ if (p)
+ return application_get (p[0]);
+
+ return 0;
+}
+
+application_t *
+application_new ()
+{
+ application_t *app;
+ pool_get (app_pool, app);
+ memset (app, 0, sizeof (*app));
+ app->index = application_get_index (app);
+ app->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
+ app->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
+ if (CLIB_DEBUG > 1)
+ clib_warning ("[%d] New app (%d)", getpid (), app->index);
+ return app;
+}
+
+void
+application_del (application_t * app)
+{
+ segment_manager_t *sm;
+ u64 handle;
+ u32 index, *handles = 0;
+ int i;
+ vnet_unbind_args_t _a, *a = &_a;
+
+ /*
+ * The app event queue allocated in first segment is cleared with
+ * the segment manager. No need to explicitly free it.
+ */
+ if (CLIB_DEBUG > 1)
+ clib_warning ("[%d] Delete app (%d)", getpid (), app->index);
+
+ /*
+ * Listener cleanup
+ */
+
+ /* *INDENT-OFF* */
+ hash_foreach (handle, index, app->listeners_table,
+ ({
+ vec_add1 (handles, handle);
+ sm = segment_manager_get (index);
+ sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
+ }));
+ /* *INDENT-ON* */
+
+ for (i = 0; i < vec_len (handles); i++)
+ {
+ a->app_index = app->index;
+ a->handle = handles[i];
+ /* seg manager is removed when unbind completes */
+ vnet_unbind (a);
+ }
+
+ /*
+ * Connects segment manager cleanup
+ */
+
+ if (app->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
+ {
+ sm = segment_manager_get (app->connects_seg_manager);
+ sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
+ segment_manager_init_del (sm);
+ }
+
+
+ /* If first segment manager is used by a listener */
+ if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
+ && app->first_segment_manager != app->connects_seg_manager)
+ {
+ sm = segment_manager_get (app->first_segment_manager);
+ /* .. and has no fifos, e.g. it might be used for redirected sessions,
+ * remove it */
+ if (!segment_manager_has_fifos (sm))
+ {
+ sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
+ segment_manager_del (sm);
+ }
+ }
+
+ application_table_del (app);
+ pool_put (app_pool, app);
+}
+
+static void
+application_verify_cb_fns (session_cb_vft_t * cb_fns)
+{
+ if (cb_fns->session_accept_callback == 0)
+ clib_warning ("No accept callback function provided");
+ if (cb_fns->session_connected_callback == 0)
+ clib_warning ("No session connected callback function provided");
+ if (cb_fns->session_disconnect_callback == 0)
+ clib_warning ("No session disconnect callback function provided");
+ if (cb_fns->session_reset_callback == 0)
+ clib_warning ("No session reset callback function provided");
+}
+
+int
+application_init (application_t * app, u32 api_client_index, u64 * options,
+ session_cb_vft_t * cb_fns)
+{
+ segment_manager_t *sm;
+ segment_manager_properties_t *props;
+ u32 app_evt_queue_size, first_seg_size;
+ u32 default_rx_fifo_size = 16 << 10, default_tx_fifo_size = 16 << 10;
+ int rv;
+
+ app_evt_queue_size = options[APP_EVT_QUEUE_SIZE] > 0 ?
+ options[APP_EVT_QUEUE_SIZE] : default_app_evt_queue_size;
+
+ /* Setup segment manager */
+ sm = segment_manager_new ();
+ sm->app_index = app->index;
+ props = &app->sm_properties;
+ props->add_segment_size = options[SESSION_OPTIONS_ADD_SEGMENT_SIZE];
+ props->rx_fifo_size = options[SESSION_OPTIONS_RX_FIFO_SIZE];
+ props->rx_fifo_size =
+ props->rx_fifo_size ? props->rx_fifo_size : default_rx_fifo_size;
+ props->tx_fifo_size = options[SESSION_OPTIONS_TX_FIFO_SIZE];
+ props->tx_fifo_size =
+ props->tx_fifo_size ? props->tx_fifo_size : default_tx_fifo_size;
+ props->add_segment = props->add_segment_size != 0;
+ props->preallocated_fifo_pairs = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
+ props->use_private_segment = options[APP_OPTIONS_FLAGS]
+ & APP_OPTIONS_FLAGS_BUILTIN_APP;
+ props->private_segment_count = options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT];
+ props->private_segment_size = options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE];
+
+ first_seg_size = options[SESSION_OPTIONS_SEGMENT_SIZE];
+ if ((rv = segment_manager_init (sm, props, first_seg_size)))
+ return rv;
+ sm->first_is_protected = 1;
+
+ app->first_segment_manager = segment_manager_index (sm);
+ app->api_client_index = api_client_index;
+ app->flags = options[APP_OPTIONS_FLAGS];
+ app->cb_fns = *cb_fns;
+
+ /* Allocate app event queue in the first shared-memory segment */
+ app->event_queue = segment_manager_alloc_queue (sm, app_evt_queue_size);
+
+ /* Check that the obvious things are properly set up */
+ application_verify_cb_fns (cb_fns);
+
+ /* Add app to lookup by api_client_index table */
+ application_table_add (app);
+
+ return 0;
+}
+
+application_t *
+application_get (u32 index)
+{
+ return pool_elt_at_index (app_pool, index);
+}
+
+application_t *
+application_get_if_valid (u32 index)
+{
+ if (pool_is_free_index (app_pool, index))
+ return 0;
+
+ return pool_elt_at_index (app_pool, index);
+}
+
+u32
+application_get_index (application_t * app)
+{
+ return app - app_pool;
+}
+
+static segment_manager_t *
+application_alloc_segment_manager (application_t * app)
+{
+ segment_manager_t *sm = 0;
+
+ /* If the first segment manager is not in use, don't allocate a new one */
+ if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
+ && app->first_segment_manager_in_use == 0)
+ {
+ sm = segment_manager_get (app->first_segment_manager);
+ app->first_segment_manager_in_use = 1;
+ return sm;
+ }
+
+ sm = segment_manager_new ();
+ sm->properties = &app->sm_properties;
+
+ return sm;
+}
+
+/**
+ * Start listening local transport endpoint for requested transport.
+ *
+ * Creates a 'dummy' stream session with state LISTENING to be used in session
+ * lookups, prior to establishing connection. Requests transport to build
+ * it's own specific listening connection.
+ */
+int
+application_start_listen (application_t * srv, session_type_t session_type,
+ transport_endpoint_t * tep, u64 * res)
+{
+ segment_manager_t *sm;
+ stream_session_t *s;
+ u64 handle;
+
+ s = listen_session_new (session_type);
+ s->app_index = srv->index;
+
+ if (stream_session_listen (s, tep))
+ goto err;
+
+ /* Allocate segment manager. All sessions derived out of a listen session
+ * have fifos allocated by the same segment manager. */
+ sm = application_alloc_segment_manager (srv);
+ if (sm == 0)
+ goto err;
+
+ /* Add to app's listener table. Useful to find all child listeners
+ * when app goes down, although, just for unbinding this is not needed */
+ handle = listen_session_get_handle (s);
+ hash_set (srv->listeners_table, handle, segment_manager_index (sm));
+
+ *res = handle;
+ return 0;
+
+err:
+ listen_session_del (s);
+ return -1;
+}
+
+/**
+ * Stop listening on session associated to handle
+ */
+int
+application_stop_listen (application_t * srv, u64 handle)
+{
+ stream_session_t *listener;
+ uword *indexp;
+ segment_manager_t *sm;
+
+ if (srv && hash_get (srv->listeners_table, handle) == 0)
+ {
+ clib_warning ("app doesn't own handle %llu!", handle);
+ return -1;
+ }
+
+ listener = listen_session_get_from_handle (handle);
+ stream_session_stop_listen (listener);
+
+ indexp = hash_get (srv->listeners_table, handle);
+ ASSERT (indexp);
+
+ sm = segment_manager_get (*indexp);
+ if (srv->first_segment_manager == *indexp)
+ {
+ /* Delete sessions but don't remove segment manager */
+ srv->first_segment_manager_in_use = 0;
+ segment_manager_del_sessions (sm);
+ }
+ else
+ {
+ segment_manager_init_del (sm);
+ }
+ hash_unset (srv->listeners_table, handle);
+ listen_session_del (listener);
+
+ return 0;
+}
+
+int
+application_open_session (application_t * app, session_type_t sst,
+ transport_endpoint_t * tep, u32 api_context)
+{
+ segment_manager_t *sm;
+ transport_connection_t *tc = 0;
+ int rv;
+
+ /* Make sure we have a segment manager for connects */
+ if (app->connects_seg_manager == (u32) ~ 0)
+ {
+ sm = application_alloc_segment_manager (app);
+ if (sm == 0)
+ return -1;
+ app->connects_seg_manager = segment_manager_index (sm);
+ }
+
+ if ((rv = stream_session_open (app->index, sst, tep, &tc)))
+ return rv;
+
+ /* Store api_context for when the reply comes. Not the nicest thing
+ * but better than allocating a separate half-open pool. */
+ tc->s_index = api_context;
+
+ return 0;
+}
+
+segment_manager_t *
+application_get_connect_segment_manager (application_t * app)
+{
+ ASSERT (app->connects_seg_manager != (u32) ~ 0);
+ return segment_manager_get (app->connects_seg_manager);
+}
+
+segment_manager_t *
+application_get_listen_segment_manager (application_t * app,
+ stream_session_t * s)
+{
+ uword *smp;
+ smp = hash_get (app->listeners_table, listen_session_get_handle (s));
+ ASSERT (smp != 0);
+ return segment_manager_get (*smp);
+}
+
+static u8 *
+app_get_name_from_reg_index (application_t * app)
+{
+ u8 *app_name;
+
+ vl_api_registration_t *regp;
+ regp = vl_api_client_index_to_registration (app->api_client_index);
+ if (!regp)
+ app_name = format (0, "builtin-%d%c", app->index, 0);
+ else
+ app_name = format (0, "%s%c", regp->name, 0);
+
+ return app_name;
+}
+
+int
+application_is_proxy (application_t * app)
+{
+ return !(app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
+}
+
+int
+application_add_segment_notify (u32 app_index, u32 fifo_segment_index)
+{
+ application_t *app = application_get (app_index);
+ u32 seg_size = 0;
+ u8 *seg_name;
+
+ /* Send an API message to the external app, to map new segment */
+ ASSERT (app->cb_fns.add_segment_callback);
+
+ segment_manager_get_segment_info (fifo_segment_index, &seg_name, &seg_size);
+ return app->cb_fns.add_segment_callback (app->api_client_index, seg_name,
+ seg_size);
+}
+
+u8 *
+format_application_listener (u8 * s, va_list * args)
+{
+ application_t *app = va_arg (*args, application_t *);
+ u64 handle = va_arg (*args, u64);
+ u32 index = va_arg (*args, u32);
+ int verbose = va_arg (*args, int);
+ stream_session_t *listener;
+ u8 *app_name, *str;
+
+ if (app == 0)
+ {
+ if (verbose)
+ s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App",
+ "API Client", "ListenerID", "SegManager");
+ else
+ s = format (s, "%-40s%-20s", "Connection", "App");
+
+ return s;
+ }
+
+ app_name = app_get_name_from_reg_index (app);
+ listener = listen_session_get_from_handle (handle);
+ str = format (0, "%U", format_stream_session, listener, verbose);
+
+ if (verbose)
+ {
+ s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name,
+ app->api_client_index, handle, index);
+ }
+ else
+ s = format (s, "%-40s%-20s", str, app_name);
+
+ vec_free (app_name);
+ return s;
+}
+
+void
+application_format_connects (application_t * app, int verbose)
+{
+ vlib_main_t *vm = vlib_get_main ();
+ segment_manager_t *sm;
+ u8 *app_name, *s = 0;
+ int j;
+
+ /* Header */
+ if (app == 0)
+ {
+ if (verbose)
+ vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
+ "API Client", "SegManager");
+ else
+ vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
+ return;
+ }
+
+ /* make sure */
+ if (app->connects_seg_manager == (u32) ~ 0)
+ return;
+
+ app_name = app_get_name_from_reg_index (app);
+
+ /* Across all fifo segments */
+ sm = segment_manager_get (app->connects_seg_manager);
+ for (j = 0; j < vec_len (sm->segment_indices); j++)
+ {
+ svm_fifo_segment_private_t *fifo_segment;
+ svm_fifo_t *fifo;
+ u8 *str;
+
+ fifo_segment = svm_fifo_segment_get_segment (sm->segment_indices[j]);
+ fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
+ while (fifo)
+ {
+ u32 session_index, thread_index;
+ stream_session_t *session;
+
+ session_index = fifo->master_session_index;
+ thread_index = fifo->master_thread_index;
+
+ session = stream_session_get (session_index, thread_index);
+ str = format (0, "%U", format_stream_session, session, verbose);
+
+ if (verbose)
+ s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
+ app->api_client_index, app->connects_seg_manager);
+ else
+ s = format (s, "%-40s%-20s", str, app_name);
+
+ vlib_cli_output (vm, "%v", s);
+ vec_reset_length (s);
+ vec_free (str);
+
+ fifo = fifo->next;
+ }
+ vec_free (s);
+ }
+
+ vec_free (app_name);
+}
+
+u8 *
+format_application (u8 * s, va_list * args)
+{
+ application_t *app = va_arg (*args, application_t *);
+ CLIB_UNUSED (int verbose) = va_arg (*args, int);
+ u8 *app_name;
+
+ if (app == 0)
+ {
+ if (verbose)
+ s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s", "Index", "Name",
+ "API Client", "Add seg size", "Rx fifo size",
+ "Tx fifo size");
+ else
+ s = format (s, "%-10s%-20s%-20s", "Index", "Name", "API Client");
+ return s;
+ }
+
+ app_name = app_get_name_from_reg_index (app);
+ if (verbose)
+ s = format (s, "%-10d%-20s%-15d%-15d%-15d%-15d", app->index, app_name,
+ app->api_client_index, app->sm_properties.add_segment_size,
+ app->sm_properties.rx_fifo_size,
+ app->sm_properties.tx_fifo_size);
+ else
+ s = format (s, "%-10d%-20s%-20d", app->index, app_name,
+ app->api_client_index);
+ return s;
+}
+
+static clib_error_t *
+show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ application_t *app;
+ int do_server = 0;
+ int do_client = 0;
+ int verbose = 0;
+
+ if (!session_manager_is_enabled ())
+ {
+ clib_error_return (0, "session layer is not enabled");
+ }
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "server"))
+ do_server = 1;
+ else if (unformat (input, "client"))
+ do_client = 1;
+ else if (unformat (input, "verbose"))
+ verbose = 1;
+ else
+ break;
+ }
+
+ if (do_server)
+ {
+ u64 handle;
+ u32 index;
+ if (pool_elts (app_pool))
+ {
+ vlib_cli_output (vm, "%U", format_application_listener,
+ 0 /* header */ , 0, 0,
+ verbose);
+ /* *INDENT-OFF* */
+ pool_foreach (app, app_pool,
+ ({
+ /* App's listener sessions */
+ if (hash_elts (app->listeners_table) == 0)
+ continue;
+ hash_foreach (handle, index, app->listeners_table,
+ ({
+ vlib_cli_output (vm, "%U", format_application_listener, app,
+ handle, index, verbose);
+ }));
+ }));
+ /* *INDENT-ON* */
+ }
+ else
+ vlib_cli_output (vm, "No active server bindings");
+ }
+
+ if (do_client)
+ {
+ if (pool_elts (app_pool))
+ {
+ application_format_connects (0, verbose);
+
+ /* *INDENT-OFF* */
+ pool_foreach (app, app_pool,
+ ({
+ if (app->connects_seg_manager == (u32)~0)
+ continue;
+ application_format_connects (app, verbose);
+ }));
+ /* *INDENT-ON* */
+ }
+ else
+ vlib_cli_output (vm, "No active client bindings");
+ }
+
+ /* Print app related info */
+ if (!do_server && !do_client)
+ {
+ vlib_cli_output (vm, "%U", format_application, 0, verbose);
+ pool_foreach (app, app_pool, (
+ {
+ vlib_cli_output (vm, "%U",
+ format_application, app,
+ verbose);
+ }
+ ));
+ }
+
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_app_command, static) =
+{
+ .path = "show app",
+ .short_help = "show app [server|client] [verbose]",
+ .function = show_app_command_fn,
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/application.h b/src/vnet/session/application.h
new file mode 100644
index 00000000..e030c376
--- /dev/null
+++ b/src/vnet/session/application.h
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_VNET_SESSION_APPLICATION_H_
+#define SRC_VNET_SESSION_APPLICATION_H_
+
+#include <vnet/vnet.h>
+#include <vnet/session/session.h>
+#include <vnet/session/segment_manager.h>
+
+typedef enum
+{
+ APP_SERVER,
+ APP_CLIENT,
+ APP_N_TYPES
+} application_type_t;
+
+typedef struct _stream_session_cb_vft
+{
+ /** Notify server of new segment */
+ int (*add_segment_callback) (u32 api_client_index, const u8 * seg_name,
+ u32 seg_size);
+
+ /** Notify server of newly accepted session */
+ int (*session_accept_callback) (stream_session_t * new_session);
+
+ /* Connection request callback */
+ int (*session_connected_callback) (u32 app_index, u32 opaque,
+ stream_session_t * s, u8 code);
+
+ /** Notify app that session is closing */
+ void (*session_disconnect_callback) (stream_session_t * s);
+
+ /** Notify app that session was reset */
+ void (*session_reset_callback) (stream_session_t * s);
+
+ /* Direct RX callback, for built-in servers */
+ int (*builtin_server_rx_callback) (stream_session_t * session);
+
+ /* Redirect connection to local server */
+ int (*redirect_connect_callback) (u32 api_client_index, void *mp);
+} session_cb_vft_t;
+
+typedef struct _application
+{
+ /** Index in server pool */
+ u32 index;
+
+ /** Flags */
+ u32 flags;
+
+ /*
+ * Binary API interface to external app
+ */
+
+ /** Binary API connection index, ~0 if internal */
+ u32 api_client_index;
+
+ /** Application listens for events on this svm queue */
+ unix_shared_memory_queue_t *event_queue;
+
+ /*
+ * Callbacks: shoulder-taps for the server/client
+ */
+
+ session_cb_vft_t cb_fns;
+
+ /*
+ * svm segment management
+ */
+ u32 connects_seg_manager;
+
+ /** Lookup tables for listeners. Value is segment manager index */
+ uword *listeners_table;
+
+ /** First segment manager has in the the first segment the application's
+ * event fifo. Depending on what the app does, it may be either used for
+ * a listener or for connects. */
+ u32 first_segment_manager;
+ u8 first_segment_manager_in_use;
+
+ /** Segment manager properties. Shared by all segment managers */
+ segment_manager_properties_t sm_properties;
+} application_t;
+
+#define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
+
+application_t *application_new ();
+int
+application_init (application_t * app, u32 api_client_index, u64 * options,
+ session_cb_vft_t * cb_fns);
+void application_del (application_t * app);
+application_t *application_get (u32 index);
+application_t *application_get_if_valid (u32 index);
+application_t *application_lookup (u32 api_client_index);
+u32 application_get_index (application_t * app);
+
+int
+application_start_listen (application_t * app, session_type_t session_type,
+ transport_endpoint_t * tep, u64 * handle);
+int application_stop_listen (application_t * srv, u64 handle);
+int
+application_open_session (application_t * app, session_type_t sst,
+ transport_endpoint_t * tep, u32 api_context);
+int application_api_queue_is_full (application_t * app);
+
+segment_manager_t *application_get_listen_segment_manager (application_t *
+ app,
+ stream_session_t *
+ s);
+segment_manager_t *application_get_connect_segment_manager (application_t *
+ app);
+int application_is_proxy (application_t * app);
+int application_add_segment_notify (u32 app_index, u32 fifo_segment_index);
+
+#endif /* SRC_VNET_SESSION_APPLICATION_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/application_interface.c b/src/vnet/session/application_interface.c
new file mode 100644
index 00000000..7e7449aa
--- /dev/null
+++ b/src/vnet/session/application_interface.c
@@ -0,0 +1,406 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <vnet/session/application_interface.h>
+
+#include <vnet/session/session.h>
+#include <vlibmemory/api.h>
+#include <vnet/dpo/load_balance.h>
+#include <vnet/fib/ip4_fib.h>
+
+/** @file
+ VPP's application/session API bind/unbind/connect/disconnect calls
+*/
+
+static u8
+ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4)
+{
+ if (is_ip4)
+ return (ip46_address->ip4.as_u32 == 0);
+ else
+ return (ip46_address->as_u64[0] == 0 && ip46_address->as_u64[1] == 0);
+}
+
+static u8
+ip_is_local (ip46_address_t * ip46_address, u8 is_ip4)
+{
+ fib_node_index_t fei;
+ fib_entry_flag_t flags;
+ fib_prefix_t prefix;
+
+ /* Check if requester is local */
+ if (is_ip4)
+ {
+ prefix.fp_len = 32;
+ prefix.fp_proto = FIB_PROTOCOL_IP4;
+ }
+ else
+ {
+ prefix.fp_len = 128;
+ prefix.fp_proto = FIB_PROTOCOL_IP6;
+ }
+
+ clib_memcpy (&prefix.fp_addr, ip46_address, sizeof (ip46_address_t));
+ fei = fib_table_lookup (0, &prefix);
+ flags = fib_entry_get_flags (fei);
+
+ return (flags & FIB_ENTRY_FLAG_LOCAL);
+}
+
+int
+api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ stream_session_t *pool;
+
+ *thread_index = handle & 0xFFFFFFFF;
+ *session_index = handle >> 32;
+
+ if (*thread_index >= vec_len (smm->sessions))
+ return VNET_API_ERROR_INVALID_VALUE;
+
+ pool = smm->sessions[*thread_index];
+
+ if (pool_is_free_index (pool, *session_index))
+ return VNET_API_ERROR_INVALID_VALUE_2;
+
+ return 0;
+}
+
+int
+vnet_bind_i (u32 app_index, session_type_t sst,
+ transport_endpoint_t * tep, u64 * handle)
+{
+ application_t *app;
+ stream_session_t *listener;
+
+ app = application_get_if_valid (app_index);
+ if (!app)
+ {
+ clib_warning ("app not attached");
+ return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ }
+
+ listener = stream_session_lookup_listener (&tep->ip, tep->port, sst);
+ if (listener)
+ return VNET_API_ERROR_ADDRESS_IN_USE;
+
+ if (!ip_is_zero (&tep->ip, tep->is_ip4)
+ && !ip_is_local (&tep->ip, tep->is_ip4))
+ return VNET_API_ERROR_INVALID_VALUE_2;
+
+ /* Setup listen path down to transport */
+ return application_start_listen (app, sst, tep, handle);
+}
+
+int
+vnet_unbind_i (u32 app_index, u64 handle)
+{
+ application_t *app = application_get_if_valid (app_index);
+
+ if (!app)
+ {
+ clib_warning ("app (%d) not attached", app_index);
+ return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ }
+
+ /* Clear the listener */
+ return application_stop_listen (app, handle);
+}
+
+int
+vnet_connect_i (u32 app_index, u32 api_context, session_type_t sst,
+ transport_endpoint_t * tep, void *mp)
+{
+ stream_session_t *listener;
+ application_t *server, *app;
+
+ /*
+ * Figure out if connecting to a local server
+ */
+ listener = stream_session_lookup_listener (&tep->ip, tep->port, sst);
+ if (listener)
+ {
+ server = application_get (listener->app_index);
+
+ /*
+ * Server is willing to have a direct fifo connection created
+ * instead of going through the state machine, etc.
+ */
+ if (server->flags & APP_OPTIONS_FLAGS_USE_FIFO)
+ return server->cb_fns.
+ redirect_connect_callback (server->api_client_index, mp);
+ }
+
+ /*
+ * Not connecting to a local server. Create regular session
+ */
+ app = application_get (app_index);
+ return application_open_session (app, sst, tep, api_context);
+}
+
+/**
+ * unformat a vnet URI
+ *
+ * fifo://name
+ * tcp://ip46-addr:port
+ * udp://ip46-addr:port
+ *
+ * u8 ip46_address[16];
+ * u16 port_in_host_byte_order;
+ * stream_session_type_t sst;
+ * u8 *fifo_name;
+ *
+ * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
+ * &sst, &port, &fifo_name))
+ * etc...
+ *
+ */
+uword
+unformat_vnet_uri (unformat_input_t * input, va_list * args)
+{
+ session_type_t *sst = va_arg (*args, session_type_t *);
+ transport_endpoint_t *tep = va_arg (*args, transport_endpoint_t *);
+
+ if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &tep->ip.ip4,
+ &tep->port))
+ {
+ *sst = SESSION_TYPE_IP4_TCP;
+ tep->port = clib_host_to_net_u16 (tep->port);
+ tep->is_ip4 = 1;
+ return 1;
+ }
+ if (unformat (input, "udp://%U/%d", unformat_ip4_address, &tep->ip.ip4,
+ &tep->port))
+ {
+ *sst = SESSION_TYPE_IP4_UDP;
+ tep->port = clib_host_to_net_u16 (tep->port);
+ tep->is_ip4 = 1;
+ return 1;
+ }
+ if (unformat (input, "udp://%U/%d", unformat_ip6_address, &tep->ip.ip6,
+ &tep->port))
+ {
+ *sst = SESSION_TYPE_IP6_UDP;
+ tep->port = clib_host_to_net_u16 (tep->port);
+ return 1;
+ }
+ if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &tep->ip.ip6,
+ &tep->port))
+ {
+ *sst = SESSION_TYPE_IP6_TCP;
+ tep->port = clib_host_to_net_u16 (tep->port);
+ return 1;
+ }
+
+ return 0;
+}
+
+static u8 *cache_uri;
+static session_type_t cache_sst;
+static transport_endpoint_t *cache_tep;
+
+int
+parse_uri (char *uri, session_type_t * sst, transport_endpoint_t * tep)
+{
+ unformat_input_t _input, *input = &_input;
+
+ if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
+ {
+ *sst = cache_sst;
+ *tep = *cache_tep;
+ return 0;
+ }
+
+ /* Make sure */
+ uri = (char *) format (0, "%s%c", uri, 0);
+
+ /* Parse uri */
+ unformat_init_string (input, uri, strlen (uri));
+ if (!unformat (input, "%U", unformat_vnet_uri, sst, tep))
+ {
+ unformat_free (input);
+ return VNET_API_ERROR_INVALID_VALUE;
+ }
+ unformat_free (input);
+
+ vec_free (cache_uri);
+ cache_uri = (u8 *) uri;
+ cache_sst = *sst;
+ if (cache_tep)
+ clib_mem_free (cache_tep);
+ cache_tep = clib_mem_alloc (sizeof (*tep));
+ *cache_tep = *tep;
+
+ return 0;
+}
+
+/**
+ * Attaches application.
+ *
+ * Allocates a vpp app, i.e., a structure that keeps back pointers
+ * to external app and a segment manager for shared memory fifo based
+ * communication with the external app.
+ */
+int
+vnet_application_attach (vnet_app_attach_args_t * a)
+{
+ application_t *app = 0;
+ segment_manager_t *sm;
+ u8 *seg_name;
+ int rv;
+
+ app = application_new ();
+ if ((rv = application_init (app, a->api_client_index, a->options,
+ a->session_cb_vft)))
+ return rv;
+
+ a->app_event_queue_address = pointer_to_uword (app->event_queue);
+ sm = segment_manager_get (app->first_segment_manager);
+ segment_manager_get_segment_info (sm->segment_indices[0],
+ &seg_name, &a->segment_size);
+
+ a->segment_name_length = vec_len (seg_name);
+ a->segment_name = seg_name;
+ ASSERT (vec_len (a->segment_name) <= 128);
+ a->app_index = app->index;
+ return 0;
+}
+
+int
+vnet_application_detach (vnet_app_detach_args_t * a)
+{
+ application_t *app;
+ app = application_get_if_valid (a->app_index);
+
+ if (!app)
+ {
+ clib_warning ("app not attached");
+ return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ }
+
+ application_del (app);
+ return 0;
+}
+
+int
+vnet_bind_uri (vnet_bind_args_t * a)
+{
+ session_type_t sst = SESSION_N_TYPES;
+ transport_endpoint_t tep;
+ int rv;
+
+ memset (&tep, 0, sizeof (tep));
+ rv = parse_uri (a->uri, &sst, &tep);
+ if (rv)
+ return rv;
+
+ if ((rv = vnet_bind_i (a->app_index, sst, &tep, &a->handle)))
+ return rv;
+
+ return 0;
+}
+
+int
+vnet_unbind_uri (vnet_unbind_args_t * a)
+{
+ session_type_t sst = SESSION_N_TYPES;
+ stream_session_t *listener;
+ transport_endpoint_t tep;
+ int rv;
+
+ rv = parse_uri (a->uri, &sst, &tep);
+ if (rv)
+ return rv;
+
+ listener = stream_session_lookup_listener (&tep.ip,
+ clib_host_to_net_u16 (tep.port),
+ sst);
+ if (!listener)
+ return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
+
+ return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
+}
+
+int
+vnet_connect_uri (vnet_connect_args_t * a)
+{
+ transport_endpoint_t tep;
+ session_type_t sst;
+ int rv;
+
+ /* Parse uri */
+ memset (&tep, 0, sizeof (tep));
+ rv = parse_uri (a->uri, &sst, &tep);
+ if (rv)
+ return rv;
+
+ return vnet_connect_i (a->app_index, a->api_context, sst, &tep, a->mp);
+}
+
+int
+vnet_disconnect_session (vnet_disconnect_args_t * a)
+{
+ u32 index, thread_index;
+ stream_session_t *s;
+
+ stream_session_parse_handle (a->handle, &index, &thread_index);
+ s = stream_session_get_if_valid (index, thread_index);
+
+ if (!s || s->app_index != a->app_index)
+ return VNET_API_ERROR_INVALID_VALUE;
+
+ /* We're peeking into another's thread pool. Make sure */
+ ASSERT (s->session_index == index);
+
+ session_send_session_evt_to_thread (a->handle, FIFO_EVENT_DISCONNECT,
+ thread_index);
+ return 0;
+}
+
+int
+vnet_bind (vnet_bind_args_t * a)
+{
+ session_type_t sst = SESSION_N_TYPES;
+ int rv;
+
+ sst = session_type_from_proto_and_ip (a->proto, a->tep.is_ip4);
+ if ((rv = vnet_bind_i (a->app_index, sst, &a->tep, &a->handle)))
+ return rv;
+
+ return 0;
+}
+
+int
+vnet_unbind (vnet_unbind_args_t * a)
+{
+ return vnet_unbind_i (a->app_index, a->handle);
+}
+
+int
+vnet_connect (vnet_connect_args_t * a)
+{
+ session_type_t sst;
+
+ sst = session_type_from_proto_and_ip (a->proto, a->tep.is_ip4);
+ return vnet_connect_i (a->app_index, a->api_context, sst, &a->tep, a->mp);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/application_interface.h b/src/vnet/session/application_interface.h
new file mode 100644
index 00000000..1d63f6cc
--- /dev/null
+++ b/src/vnet/session/application_interface.h
@@ -0,0 +1,183 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef __included_uri_h__
+#define __included_uri_h__
+
+#include <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include <svm/svm_fifo_segment.h>
+#include <vnet/session/session.h>
+#include <vnet/session/application.h>
+#include <vnet/session/transport.h>
+
+typedef struct _vnet_app_attach_args_t
+{
+ /** Binary API client index */
+ u32 api_client_index;
+
+ /** Application and segment manager options */
+ u64 *options;
+
+ /** Session to application callback functions */
+ session_cb_vft_t *session_cb_vft;
+
+ /** Flag that indicates if app is builtin */
+ u8 builtin;
+
+ /*
+ * Results
+ */
+ u8 *segment_name;
+ u32 segment_name_length;
+ u32 segment_size;
+ u64 app_event_queue_address;
+ u32 app_index;
+} vnet_app_attach_args_t;
+
+typedef struct _vnet_app_detach_args_t
+{
+ u32 app_index;
+} vnet_app_detach_args_t;
+
+typedef struct _vnet_bind_args_t
+{
+ union
+ {
+ char *uri;
+ struct
+ {
+ transport_endpoint_t tep;
+ transport_proto_t proto;
+ };
+ };
+
+ u32 app_index;
+
+ /*
+ * Results
+ */
+ char *segment_name;
+ u32 segment_name_length;
+ u64 server_event_queue_address;
+ u64 handle;
+} vnet_bind_args_t;
+
+typedef struct _vnet_unbind_args_t
+{
+ union
+ {
+ char *uri;
+ u64 handle;
+ };
+ u32 app_index;
+} vnet_unbind_args_t;
+
+typedef struct _vnet_connect_args
+{
+ union
+ {
+ char *uri;
+ struct
+ {
+ transport_endpoint_t tep;
+ transport_proto_t proto;
+ };
+ };
+ u32 app_index;
+ u32 api_context;
+
+ /* Used for redirects */
+ void *mp;
+
+ /* used for proxy connections */
+ u64 server_handle;
+} vnet_connect_args_t;
+
+typedef struct _vnet_disconnect_args_t
+{
+ u64 handle;
+ u32 app_index;
+} vnet_disconnect_args_t;
+
+/* Application attach options */
+typedef enum
+{
+ APP_EVT_QUEUE_SIZE,
+ APP_OPTIONS_FLAGS,
+ APP_OPTIONS_PREALLOC_FIFO_PAIRS,
+ APP_OPTIONS_PRIVATE_SEGMENT_COUNT,
+ APP_OPTIONS_PRIVATE_SEGMENT_SIZE,
+ SESSION_OPTIONS_SEGMENT_SIZE,
+ SESSION_OPTIONS_ADD_SEGMENT_SIZE,
+ SESSION_OPTIONS_RX_FIFO_SIZE,
+ SESSION_OPTIONS_TX_FIFO_SIZE,
+ SESSION_OPTIONS_PREALLOCATED_FIFO_PAIRS,
+ SESSION_OPTIONS_ACCEPT_COOKIE,
+ SESSION_OPTIONS_N_OPTIONS
+} app_attach_options_index_t;
+
+#define foreach_app_options_flags \
+ _(USE_FIFO, "Use FIFO with redirects") \
+ _(ADD_SEGMENT, "Add segment and signal app if needed") \
+ _(BUILTIN_APP, "Application is builtin") \
+ _(IS_PROXY, "Application is proxying")
+
+typedef enum _app_options
+{
+#define _(sym, str) APP_OPTIONS_##sym,
+ foreach_app_options_flags
+#undef _
+} app_options_t;
+
+typedef enum _app_options_flags
+{
+#define _(sym, str) APP_OPTIONS_FLAGS_##sym = 1 << APP_OPTIONS_##sym,
+ foreach_app_options_flags
+#undef _
+} app_options_flags_t;
+
+///** Server can handle delegated connect requests from local clients */
+//#define APP_OPTIONS_FLAGS_USE_FIFO (1<<0)
+//
+///** Server wants vpp to add segments when out of memory for fifos */
+//#define APP_OPTIONS_FLAGS_ADD_SEGMENT (1<<1)
+
+#define VNET_CONNECT_REDIRECTED 123
+
+int vnet_application_attach (vnet_app_attach_args_t * a);
+int vnet_application_detach (vnet_app_detach_args_t * a);
+
+int vnet_bind_uri (vnet_bind_args_t *);
+int vnet_unbind_uri (vnet_unbind_args_t * a);
+int vnet_connect_uri (vnet_connect_args_t * a);
+int vnet_disconnect_session (vnet_disconnect_args_t * a);
+
+int vnet_bind (vnet_bind_args_t * a);
+int vnet_connect (vnet_connect_args_t * a);
+int vnet_unbind (vnet_unbind_args_t * a);
+
+int
+api_parse_session_handle (u64 handle, u32 * session_index,
+ u32 * thread_index);
+
+#endif /* __included_uri_h__ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c
new file mode 100644
index 00000000..48d02755
--- /dev/null
+++ b/src/vnet/session/segment_manager.c
@@ -0,0 +1,636 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <vnet/session/segment_manager.h>
+#include <vnet/session/session.h>
+#include <vnet/session/application.h>
+
+/**
+ * Counter used to build segment names
+ */
+u32 segment_name_counter = 0;
+
+/**
+ * Pool of segment managers
+ */
+segment_manager_t *segment_managers = 0;
+
+/**
+ * Process private segment index
+ */
+u32 *private_segment_indices;
+
+/**
+ * Default fifo and segment size. TODO config.
+ */
+u32 default_fifo_size = 1 << 16;
+u32 default_segment_size = 1 << 20;
+
+void
+segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size)
+{
+ svm_fifo_segment_private_t *s;
+ s = svm_fifo_segment_get_segment (index);
+ *name = s->h->segment_name;
+ *size = s->ssvm.ssvm_size;
+}
+
+always_inline int
+session_manager_add_segment_i (segment_manager_t * sm, u32 segment_size,
+ u8 * segment_name)
+{
+ svm_fifo_segment_create_args_t _ca, *ca = &_ca;
+ int rv;
+
+ memset (ca, 0, sizeof (*ca));
+
+ if (!sm->properties->use_private_segment)
+ {
+ ca->segment_name = (char *) segment_name;
+ ca->segment_size = segment_size;
+ ca->rx_fifo_size = sm->properties->rx_fifo_size;
+ ca->tx_fifo_size = sm->properties->tx_fifo_size;
+ ca->preallocated_fifo_pairs = sm->properties->preallocated_fifo_pairs;
+
+ rv = svm_fifo_segment_create (ca);
+ if (rv)
+ {
+ clib_warning ("svm_fifo_segment_create ('%s', %d) failed",
+ ca->segment_name, ca->segment_size);
+ return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
+ }
+ }
+ else
+ {
+ u32 rx_fifo_size, tx_fifo_size, rx_rounded_data_size,
+ tx_rounded_data_size;
+ u32 approx_segment_count;
+ u64 approx_total_size;
+
+ ca->segment_name = "process-private-segment";
+ ca->segment_size = ~0;
+ ca->rx_fifo_size = sm->properties->rx_fifo_size;
+ ca->tx_fifo_size = sm->properties->tx_fifo_size;
+ ca->preallocated_fifo_pairs = sm->properties->preallocated_fifo_pairs;
+ ca->private_segment_count = sm->properties->private_segment_count;
+ ca->private_segment_size = sm->properties->private_segment_size;
+
+ /* Default to a small private segment */
+ if (ca->private_segment_size == 0)
+ ca->private_segment_size = 128 << 20;
+
+ /* Calculate space requirements */
+ rx_rounded_data_size = (1 << (max_log2 (ca->rx_fifo_size)));
+ tx_rounded_data_size = (1 << (max_log2 (ca->tx_fifo_size)));
+
+ rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
+ tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
+
+ approx_total_size = (u64) ca->preallocated_fifo_pairs
+ * (rx_fifo_size + tx_fifo_size);
+ approx_segment_count =
+ (approx_total_size +
+ (ca->private_segment_size - 1)) / (u64) ca->private_segment_size;
+
+ /* The user asked us to figure it out... */
+ if (ca->private_segment_count == 0)
+ {
+ ca->private_segment_count = approx_segment_count;
+ }
+ /* Follow directions, but issue a warning */
+ else if (approx_segment_count != ca->private_segment_count)
+ {
+ clib_warning
+ ("Honoring segment count %u, but calculated count was %u",
+ ca->private_segment_count, approx_segment_count);
+ }
+
+ if (svm_fifo_segment_create_process_private (ca))
+ clib_warning ("Failed to create process private segment");
+
+ ASSERT (vec_len (ca->new_segment_indices));
+ }
+ vec_append (sm->segment_indices, ca->new_segment_indices);
+ vec_free (ca->new_segment_indices);
+ return 0;
+}
+
+int
+session_manager_add_segment (segment_manager_t * sm)
+{
+ u8 *segment_name;
+ svm_fifo_segment_create_args_t _ca, *ca = &_ca;
+ u32 add_segment_size;
+ int rv;
+
+ memset (ca, 0, sizeof (*ca));
+ segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
+ add_segment_size = sm->properties->add_segment_size ?
+ sm->properties->add_segment_size : default_segment_size;
+
+ rv = session_manager_add_segment_i (sm, add_segment_size, segment_name);
+ vec_free (segment_name);
+ return rv;
+}
+
+int
+session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size)
+{
+ u8 *segment_name;
+ int rv;
+
+ segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
+ rv = session_manager_add_segment_i (sm, segment_size, segment_name);
+ vec_free (segment_name);
+ return rv;
+}
+
+segment_manager_t *
+segment_manager_new ()
+{
+ segment_manager_t *sm;
+ pool_get (segment_managers, sm);
+ memset (sm, 0, sizeof (*sm));
+ return sm;
+}
+
+/**
+ * Initializes segment manager based on options provided.
+ * Returns error if svm segment allocation fails.
+ */
+int
+segment_manager_init (segment_manager_t * sm,
+ segment_manager_properties_t * properties,
+ u32 first_seg_size)
+{
+ int rv;
+
+ /* app allocates these */
+ sm->properties = properties;
+
+ first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size;
+
+ rv = session_manager_add_first_segment (sm, first_seg_size);
+ if (rv)
+ {
+ clib_warning ("Failed to allocate segment");
+ return rv;
+ }
+
+ clib_spinlock_init (&sm->lockp);
+ return 0;
+}
+
+u8
+segment_manager_has_fifos (segment_manager_t * sm)
+{
+ svm_fifo_segment_private_t *segment;
+ int i;
+
+ for (i = 0; i < vec_len (sm->segment_indices); i++)
+ {
+ segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
+ if (CLIB_DEBUG && i && !svm_fifo_segment_has_fifos (segment)
+ && !(segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED))
+ clib_warning ("segment %d has no fifos!", sm->segment_indices[i]);
+ if (svm_fifo_segment_has_fifos (segment))
+ return 1;
+ }
+ return 0;
+}
+
+static u8
+segment_manager_app_detached (segment_manager_t * sm)
+{
+ return (sm->app_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
+}
+
+static void
+segment_manager_del_segment (segment_manager_t * sm, u32 segment_index)
+{
+ svm_fifo_segment_private_t *fifo_segment;
+ u32 svm_segment_index;
+ clib_spinlock_lock (&sm->lockp);
+ svm_segment_index = sm->segment_indices[segment_index];
+ fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
+ if (!fifo_segment
+ || ((fifo_segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED)
+ && !segment_manager_app_detached (sm)))
+ {
+ clib_spinlock_unlock (&sm->lockp);
+ return;
+ }
+ svm_fifo_segment_delete (fifo_segment);
+ vec_del1 (sm->segment_indices, segment_index);
+ clib_spinlock_unlock (&sm->lockp);
+}
+
+/**
+ * Initiate disconnects for all sessions 'owned' by a segment manager
+ */
+void
+segment_manager_del_sessions (segment_manager_t * sm)
+{
+ int j;
+ svm_fifo_segment_private_t *fifo_segment;
+ svm_fifo_t *fifo;
+
+ ASSERT (vec_len (sm->segment_indices));
+
+ /* Across all fifo segments used by the server */
+ for (j = 0; j < vec_len (sm->segment_indices); j++)
+ {
+ fifo_segment = svm_fifo_segment_get_segment (sm->segment_indices[j]);
+ fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
+
+ /*
+ * Remove any residual sessions from the session lookup table
+ * Don't bother deleting the individual fifos, we're going to
+ * throw away the fifo segment in a minute.
+ */
+ while (fifo)
+ {
+ u32 session_index, thread_index;
+ stream_session_t *session;
+
+ session_index = fifo->master_session_index;
+ thread_index = fifo->master_thread_index;
+ session = stream_session_get (session_index, thread_index);
+
+ /* Instead of directly removing the session call disconnect */
+ if (session->session_state != SESSION_STATE_CLOSED)
+ {
+ session->session_state = SESSION_STATE_CLOSED;
+ session_send_session_evt_to_thread (stream_session_handle
+ (session),
+ FIFO_EVENT_DISCONNECT,
+ thread_index);
+ }
+ fifo = fifo->next;
+ }
+
+ /* Instead of removing the segment, test when cleaning up disconnected
+ * sessions if the segment can be removed.
+ */
+ }
+}
+
+/**
+ * Removes segment manager.
+ *
+ * Since the fifos allocated in the segment keep backpointers to the sessions
+ * prior to removing the segment, we call session disconnect. This
+ * subsequently propagates into transport.
+ */
+void
+segment_manager_del (segment_manager_t * sm)
+{
+ int i;
+
+ ASSERT (!segment_manager_has_fifos (sm)
+ && segment_manager_app_detached (sm));
+
+ /* If we have empty preallocated segments that haven't been removed, remove
+ * them now. Apart from that, the first segment in the first segment manager
+ * is not removed when all fifos are removed. It can only be removed when
+ * the manager is explicitly deleted/detached by the app. */
+ for (i = vec_len (sm->segment_indices) - 1; i >= 0; i--)
+ {
+ if (CLIB_DEBUG)
+ {
+ svm_fifo_segment_private_t *segment;
+ segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
+ ASSERT (!svm_fifo_segment_has_fifos (segment));
+ }
+ segment_manager_del_segment (sm, i);
+ }
+ clib_spinlock_free (&sm->lockp);
+ if (CLIB_DEBUG)
+ memset (sm, 0xfe, sizeof (*sm));
+ pool_put (segment_managers, sm);
+}
+
+void
+segment_manager_init_del (segment_manager_t * sm)
+{
+ if (segment_manager_has_fifos (sm))
+ segment_manager_del_sessions (sm);
+ else
+ {
+ ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
+ segment_manager_del (sm);
+ }
+}
+
+int
+segment_manager_alloc_session_fifos (segment_manager_t * sm,
+ svm_fifo_t ** server_rx_fifo,
+ svm_fifo_t ** server_tx_fifo,
+ u32 * fifo_segment_index)
+{
+ svm_fifo_segment_private_t *fifo_segment;
+ u32 fifo_size, sm_index;
+ u8 added_a_segment = 0;
+ int i;
+
+ ASSERT (vec_len (sm->segment_indices));
+
+ /* Make sure we don't have multiple threads trying to allocate segments
+ * at the same time. */
+ clib_spinlock_lock (&sm->lockp);
+
+ /* Allocate svm fifos */
+again:
+ for (i = 0; i < vec_len (sm->segment_indices); i++)
+ {
+ *fifo_segment_index = sm->segment_indices[i];
+ fifo_segment = svm_fifo_segment_get_segment (*fifo_segment_index);
+
+ fifo_size = sm->properties->rx_fifo_size;
+ fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
+ *server_rx_fifo =
+ svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
+ FIFO_SEGMENT_RX_FREELIST);
+
+ fifo_size = sm->properties->tx_fifo_size;
+ fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
+ *server_tx_fifo =
+ svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
+ FIFO_SEGMENT_TX_FREELIST);
+
+ if (*server_rx_fifo == 0)
+ {
+ /* This would be very odd, but handle it... */
+ if (*server_tx_fifo != 0)
+ {
+ svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo,
+ FIFO_SEGMENT_TX_FREELIST);
+ *server_tx_fifo = 0;
+ }
+ continue;
+ }
+ if (*server_tx_fifo == 0)
+ {
+ if (*server_rx_fifo != 0)
+ {
+ svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo,
+ FIFO_SEGMENT_RX_FREELIST);
+ *server_rx_fifo = 0;
+ }
+ continue;
+ }
+ break;
+ }
+
+ /* See if we're supposed to create another segment */
+ if (*server_rx_fifo == 0)
+ {
+ if (sm->properties->add_segment && !sm->properties->use_private_segment)
+ {
+ if (added_a_segment)
+ {
+ clib_warning ("added a segment, still can't allocate a fifo");
+ clib_spinlock_unlock (&sm->lockp);
+ return SESSION_ERROR_NEW_SEG_NO_SPACE;
+ }
+
+ if (session_manager_add_segment (sm))
+ {
+ clib_spinlock_unlock (&sm->lockp);
+ return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
+ }
+
+ added_a_segment = 1;
+ goto again;
+ }
+ else
+ {
+ clib_warning ("No space to allocate fifos!");
+ clib_spinlock_unlock (&sm->lockp);
+ return SESSION_ERROR_NO_SPACE;
+ }
+ }
+
+ /* Backpointers to segment manager */
+ sm_index = segment_manager_index (sm);
+ (*server_tx_fifo)->segment_manager = sm_index;
+ (*server_rx_fifo)->segment_manager = sm_index;
+
+ clib_spinlock_unlock (&sm->lockp);
+
+ if (added_a_segment)
+ return application_add_segment_notify (sm->app_index,
+ *fifo_segment_index);
+
+ return 0;
+}
+
+void
+segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
+ svm_fifo_t * tx_fifo)
+{
+ segment_manager_t *sm;
+ svm_fifo_segment_private_t *fifo_segment;
+ u32 i, segment_index = ~0;
+ u8 is_first;
+
+ sm = segment_manager_get_if_valid (rx_fifo->segment_manager);
+
+ /* It's possible to have no segment manager if the session was removed
+ * as result of a detach. */
+ if (!sm)
+ return;
+
+ fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
+ svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
+ FIFO_SEGMENT_RX_FREELIST);
+ svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
+ FIFO_SEGMENT_TX_FREELIST);
+
+ /*
+ * Try to remove svm segment if it has no fifos. This can be done only if
+ * the segment is not the first in the segment manager or if it is first
+ * and it is not protected. Moreover, if the segment is first and the app
+ * has detached from the segment manager, remove the segment manager.
+ */
+ if (!svm_fifo_segment_has_fifos (fifo_segment))
+ {
+ is_first = sm->segment_indices[0] == svm_segment_index;
+
+ /* Remove segment if it holds no fifos or first but not protected */
+ if (!is_first || !sm->first_is_protected)
+ {
+ /* Find the segment manager segment index */
+ for (i = 0; i < vec_len (sm->segment_indices); i++)
+ if (sm->segment_indices[i] == svm_segment_index)
+ {
+ segment_index = i;
+ break;
+ }
+ ASSERT (segment_index != (u32) ~ 0);
+ segment_manager_del_segment (sm, segment_index);
+ }
+
+ /* Remove segment manager if no sessions and detached from app */
+ if (segment_manager_app_detached (sm)
+ && !segment_manager_has_fifos (sm))
+ segment_manager_del (sm);
+ }
+}
+
+/**
+ * Allocates shm queue in the first segment
+ */
+unix_shared_memory_queue_t *
+segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size)
+{
+ ssvm_shared_header_t *sh;
+ svm_fifo_segment_private_t *segment;
+ unix_shared_memory_queue_t *q;
+ void *oldheap;
+
+ ASSERT (sm->segment_indices != 0);
+
+ segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
+ sh = segment->ssvm.sh;
+
+ oldheap = ssvm_push_heap (sh);
+ q = unix_shared_memory_queue_init (queue_size,
+ sizeof (session_fifo_event_t),
+ 0 /* consumer pid */ ,
+ 0 /* signal when queue non-empty */ );
+ ssvm_pop_heap (oldheap);
+ return q;
+}
+
+/**
+ * Frees shm queue allocated in the first segment
+ */
+void
+segment_manager_dealloc_queue (segment_manager_t * sm,
+ unix_shared_memory_queue_t * q)
+{
+ ssvm_shared_header_t *sh;
+ svm_fifo_segment_private_t *segment;
+ void *oldheap;
+
+ ASSERT (sm->segment_indices != 0);
+
+ segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
+ sh = segment->ssvm.sh;
+
+ oldheap = ssvm_push_heap (sh);
+ unix_shared_memory_queue_free (q);
+ ssvm_pop_heap (oldheap);
+}
+
+static clib_error_t *
+segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ svm_fifo_segment_private_t *segments, *seg;
+ segment_manager_t *sm;
+ u8 show_segments = 0, verbose = 0, *name;
+ uword address;
+ u64 size;
+ u32 active_fifos;
+ u32 free_fifos;
+
+ mheap_t *heap_header;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "segments"))
+ show_segments = 1;
+ else if (unformat (input, "verbose"))
+ verbose = 1;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+ vlib_cli_output (vm, "%d segment managers allocated",
+ pool_elts (segment_managers));
+ if (verbose && pool_elts (segment_managers))
+ {
+ vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
+ "Segments");
+
+ /* *INDENT-OFF* */
+ pool_foreach (sm, segment_managers, ({
+ vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index(sm),
+ sm->app_index, vec_len (sm->segment_indices));
+ }));
+ /* *INDENT-ON* */
+
+ }
+ if (show_segments)
+ {
+ segments = svm_fifo_segment_segments_pool ();
+ vlib_cli_output (vm, "%d svm fifo segments allocated",
+ pool_elts (segments));
+ vlib_cli_output (vm, "%-20s%=12s%=16s%=16s%=16s", "Name",
+ "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
+
+ /* *INDENT-OFF* */
+ pool_foreach (seg, segments, ({
+ if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
+ {
+ address = pointer_to_uword (seg->ssvm.sh->heap);
+ if (seg->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)
+ name = format (0, "main heap");
+ else
+ name = format (0, "private heap");
+ heap_header = mheap_header (seg->ssvm.sh->heap);
+ size = heap_header->max_size;
+ }
+ else
+ {
+ address = seg->ssvm.sh->ssvm_va;
+ size = seg->ssvm.ssvm_size;
+ name = seg->ssvm.sh->name;
+ }
+ active_fifos = svm_fifo_segment_num_fifos (seg);
+ free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
+ vlib_cli_output (vm, "%-20v%=16llu%=16u%=16u%16llx",
+ name, size >> 20ULL, active_fifos, free_fifos,
+ address);
+ if (verbose)
+ vlib_cli_output (vm, "%U",
+ format_svm_fifo_segment, seg, verbose);
+ if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
+ vec_free (name);
+ }));
+ /* *INDENT-ON* */
+
+ }
+ return 0;
+}
+
+ /* *INDENT-OFF* */
+VLIB_CLI_COMMAND (segment_manager_show_command, static) =
+{
+ .path = "show segment-manager",
+ .short_help = "show segment-manager [segments][verbose]",
+ .function = segment_manager_show_fn,
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/segment_manager.h b/src/vnet/session/segment_manager.h
new file mode 100644
index 00000000..6e5b8989
--- /dev/null
+++ b/src/vnet/session/segment_manager.h
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef SRC_VNET_SESSION_SEGMENT_MANAGER_H_
+#define SRC_VNET_SESSION_SEGMENT_MANAGER_H_
+
+#include <vnet/vnet.h>
+#include <svm/svm_fifo_segment.h>
+
+#include <vlibmemory/unix_shared_memory_queue.h>
+#include <vlibmemory/api.h>
+#include <vppinfra/lock.h>
+
+typedef struct _segment_manager_properties
+{
+ /** Session fifo sizes. */
+ u32 rx_fifo_size;
+ u32 tx_fifo_size;
+
+ /** Preallocated pool sizes */
+ u32 preallocated_fifo_pairs;
+
+ /** Configured additional segment size */
+ u32 add_segment_size;
+
+ /** Flag that indicates if additional segments should be created */
+ u8 add_segment;
+
+ /** Use private memory segment instead of shared memory */
+ u8 use_private_segment;
+
+ /** Use one or more private mheaps, instead of the global heap */
+ u32 private_segment_count;
+ u32 private_segment_size;
+} segment_manager_properties_t;
+
+typedef struct _segment_manager
+{
+ clib_spinlock_t lockp;
+
+ /** segments mapped by this manager */
+ u32 *segment_indices;
+
+ /** Owner app index */
+ u32 app_index;
+
+ /**
+ * Pointer to manager properties. Could be shared among all of
+ * an app's segment managers s
+ */
+ segment_manager_properties_t *properties;
+
+ /**
+ * First segment should not be deleted unless segment manger is deleted.
+ * This also indicates that the segment manager is the first to have been
+ * allocated for the app.
+ */
+ u8 first_is_protected;
+} segment_manager_t;
+
+#define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
+
+/** Pool of segment managers */
+extern segment_manager_t *segment_managers;
+
+always_inline segment_manager_t *
+segment_manager_get (u32 index)
+{
+ return pool_elt_at_index (segment_managers, index);
+}
+
+always_inline segment_manager_t *
+segment_manager_get_if_valid (u32 index)
+{
+ if (pool_is_free_index (segment_managers, index))
+ return 0;
+ return pool_elt_at_index (segment_managers, index);
+}
+
+always_inline u32
+segment_manager_index (segment_manager_t * sm)
+{
+ return sm - segment_managers;
+}
+
+segment_manager_t *segment_manager_new ();
+int
+segment_manager_init (segment_manager_t * sm,
+ segment_manager_properties_t * properties,
+ u32 seg_size);
+
+void segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size);
+int
+session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size);
+int session_manager_add_segment (segment_manager_t * sm);
+void segment_manager_del_sessions (segment_manager_t * sm);
+void segment_manager_del (segment_manager_t * sm);
+void segment_manager_init_del (segment_manager_t * sm);
+u8 segment_manager_has_fifos (segment_manager_t * sm);
+int
+segment_manager_alloc_session_fifos (segment_manager_t * sm,
+ svm_fifo_t ** server_rx_fifo,
+ svm_fifo_t ** server_tx_fifo,
+ u32 * fifo_segment_index);
+void
+segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
+ svm_fifo_t * tx_fifo);
+unix_shared_memory_queue_t *segment_manager_alloc_queue (segment_manager_t *
+ sm, u32 queue_size);
+void segment_manager_dealloc_queue (segment_manager_t * sm,
+ unix_shared_memory_queue_t * q);
+
+#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session.api b/src/vnet/session/session.api
new file mode 100644
index 00000000..30d2ae96
--- /dev/null
+++ b/src/vnet/session/session.api
@@ -0,0 +1,331 @@
+/*
+ * Copyright (c) 2015-2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/** \brief client->vpp, attach application to session layer
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param initial_segment_size - size of the initial shm segment to be
+ allocated
+ @param options - segment size, fifo sizes, etc.
+*/
+ define application_attach {
+ u32 client_index;
+ u32 context;
+ u32 initial_segment_size;
+ u64 options[16];
+ };
+
+ /** \brief Application attach reply
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+ @param app_event_queue_address - vpp event queue address or 0 if this
+ connection shouldn't send events
+ @param segment_size - size of first shm segment
+ @param segment_name_length - length of segment name
+ @param segment_name - name of segment client needs to attach to
+*/
+define application_attach_reply {
+ u32 context;
+ i32 retval;
+ u64 app_event_queue_address;
+ u32 segment_size;
+ u8 segment_name_length;
+ u8 segment_name[128];
+};
+
+ /** \brief client->vpp, attach application to session layer
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+*/
+autoreply define application_detach {
+ u32 client_index;
+ u32 context;
+ };
+
+/** \brief vpp->client, please map an additional shared memory segment
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param segment_name -
+*/
+autoreply define map_another_segment {
+ u32 client_index;
+ u32 context;
+ u32 segment_size;
+ u8 segment_name[128];
+};
+
+ /** \brief Bind to a given URI
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param accept_cookie - sender accept cookie, to identify this bind flavor
+ @param uri - a URI, e.g. "tcp://0.0.0.0/0/80" [ipv4]
+ "tcp://::/0/80" [ipv6] etc.
+ @param options - socket options, fifo sizes, etc.
+*/
+autoreply define bind_uri {
+ u32 client_index;
+ u32 context;
+ u32 accept_cookie;
+ u8 uri[128];
+};
+
+/** \brief Unbind a given URI
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param uri - a URI, e.g. "tcp://0.0.0.0/0/80" [ipv4]
+ "tcp://::/0/80" [ipv6], etc.
+ @param options - socket options, fifo sizes, etc.
+*/
+autoreply define unbind_uri {
+ u32 client_index;
+ u32 context;
+ u8 uri[128];
+};
+
+/** \brief Connect to a given URI
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param accept_cookie - sender accept cookie, to identify this bind flavor
+ @param uri - a URI, e.g. "tcp4://0.0.0.0/0/80"
+ "tcp6://::/0/80" [ipv6], etc.
+ @param options - socket options, fifo sizes, etc. passed by vpp to the
+ server when redirecting connects
+ @param client_queue_address - binary API client queue address. Used by
+ local server when connect was redirected.
+*/
+autoreply define connect_uri {
+ u32 client_index;
+ u32 context;
+ u8 uri[128];
+ u64 client_queue_address;
+ u64 options[16];
+};
+
+/** \brief vpp->client, accept this session
+ @param context - sender context, to match reply w/ request
+ @param listener_handle - tells client which listener this pertains to
+ @param handle - unique session identifier
+ @param session_thread_index - thread index of new session
+ @param rx_fifo_address - rx (vpp -> vpp-client) fifo address
+ @param tx_fifo_address - tx (vpp-client -> vpp) fifo address
+ @param vpp_event_queue_address - vpp's event queue address
+ @param port - remote port
+ @param is_ip4 - 1 if the ip is ip4
+ @param ip - remote ip
+*/
+define accept_session {
+ u32 client_index;
+ u32 context;
+ u64 listener_handle;
+ u64 handle;
+ u64 server_rx_fifo;
+ u64 server_tx_fifo;
+ u64 vpp_event_queue_address;
+ u16 port;
+ u8 is_ip4;
+ u8 ip[16];
+};
+
+/** \brief client->vpp, reply to an accept message
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+ @param session_index - session index from accept_session / connect_reply
+ @param session_thread_index - thread index from accept_session /
+ connect_reply
+*/
+define accept_session_reply {
+ u32 context;
+ i32 retval;
+ u64 handle;
+};
+
+/** \brief bidirectional disconnect API
+ @param client_index - opaque cookie to identify the sender
+ client to vpp direction only
+ @param context - sender context, to match reply w/ request
+ @param handle - session handle obtained from accept/connect
+*/
+define disconnect_session {
+ u32 client_index;
+ u32 context;
+ u64 handle;
+};
+
+/** \brief bidirectional disconnect reply API
+ @param client_index - opaque cookie to identify the sender
+ client to vpp direction only
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+ @param handle - session handle
+*/
+define disconnect_session_reply {
+ u32 client_index;
+ u32 context;
+ i32 retval;
+ u64 handle;
+};
+
+/** \brief vpp->client reset session API
+ @param client_index - opaque cookie to identify the sender
+ client to vpp direction only
+ @param context - sender context, to match reply w/ request
+ @param handle - session handle obtained via accept/connects
+*/
+define reset_session {
+ u32 client_index;
+ u32 context;
+ u64 handle;
+};
+
+/** \brief client->vpp reset session reply
+ @param client_index - opaque cookie to identify the sender
+ client to vpp direction only
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+ @param handle - session handle obtained via accept/connect
+*/
+define reset_session_reply {
+ u32 client_index;
+ u32 context;
+ i32 retval;
+ u64 handle;
+};
+
+/** \brief Bind to an ip:port pair for a given transport protocol
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param vrf - bind namespace
+ @param is_ip4 - flag that is 1 if ip address family is IPv4
+ @param ip - ip address
+ @param port - port
+ @param proto - protocol 0 - TCP 1 - UDP
+ @param options - socket options, fifo sizes, etc.
+*/
+define bind_sock {
+ u32 client_index;
+ u32 context;
+ u32 vrf;
+ u8 is_ip4;
+ u8 ip[16];
+ u16 port;
+ u8 proto;
+ u64 options[16];
+};
+
+/** \brief Unbind
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param handle - bind handle obtained from bind reply
+*/
+autoreply define unbind_sock {
+ u32 client_index;
+ u32 context;
+ u64 handle;
+};
+
+/** \brief Connect to a remote peer
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param app_connect - application connection id to be returned in reply
+ @param vrf - connection namespace
+ @param is_ip4 - flag that is 1 if ip address family is IPv4
+ @param ip - ip address
+ @param port - port
+ @param proto - protocol 0 - TCP 1 - UDP
+ @param client_queue_address - client's API queue address. Non-zero when
+ used to perform redirects
+ @param options - socket options, fifo sizes, etc. when doing redirects
+*/
+autoreply define connect_sock {
+ u32 client_index;
+ u32 context;
+ u32 vrf;
+ u8 is_ip4;
+ u8 ip[16];
+ u16 port;
+ u8 proto;
+ u64 client_queue_address;
+ u64 options[16];
+};
+
+/** \brief Bind reply
+ @param context - sender context, to match reply w/ request
+ @param handle - bind handle
+ @param retval - return code for the request
+ @param event_queue_address - vpp event queue address or 0 if this
+ connection shouldn't send events
+ @param segment_name_length - length of segment name
+ @param segment_name - name of segment client needs to attach to
+*/
+define bind_sock_reply {
+ u32 context;
+ u64 handle;
+ i32 retval;
+ u64 server_event_queue_address;
+ u32 segment_size;
+ u8 segment_name_length;
+ u8 segment_name[128];
+};
+
+/* Dummy connect message -- needed to satisfy api generators
+*
+* NEVER USED, doxygen tags elided on purpose.
+*/
+define connect_session {
+ u32 client_index;
+ u32 context;
+};
+
+/** \brief vpp/server->client, connect reply -- used for all connect_* messages
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+ @param handle - connection handle
+ @param server_rx_fifo - rx (vpp -> vpp-client) fifo address
+ @param server_tx_fifo - tx (vpp-client -> vpp) fifo address
+ @param vpp_event_queue_address - vpp's event queue address
+ @param segment_size - size of segment to be attached. Only for redirects.
+ @param segment_name_length - non-zero if the client needs to attach to
+ the fifo segment
+ @param segment_name - set if the client needs to attach to the segment
+*/
+define connect_session_reply {
+ u32 context;
+ i32 retval;
+ u64 handle;
+ u64 server_rx_fifo;
+ u64 server_tx_fifo;
+ u64 vpp_event_queue_address;
+ u32 segment_size;
+ u8 segment_name_length;
+ u8 segment_name[128];
+};
+
+/** \brief enable/disable session layer
+ @param client_index - opaque cookie to identify the sender
+ client to vpp direction only
+ @param context - sender context, to match reply w/ request
+ @param is_enable - disable session layer if 0, enable otherwise
+*/
+autoreply define session_enable_disable {
+ u32 client_index;
+ u32 context;
+ u8 is_enable;
+};
+
+/*
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
new file mode 100644
index 00000000..dc930ce8
--- /dev/null
+++ b/src/vnet/session/session.c
@@ -0,0 +1,1036 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * @file
+ * @brief Session and session manager
+ */
+
+#include <vnet/session/session.h>
+#include <vnet/session/session_debug.h>
+#include <vnet/session/application.h>
+#include <vlibmemory/api.h>
+#include <vnet/dpo/load_balance.h>
+#include <vnet/fib/ip4_fib.h>
+#include <vnet/tcp/tcp.h>
+
+session_manager_main_t session_manager_main;
+extern transport_proto_vft_t *tp_vfts;
+
+int
+stream_session_create_i (segment_manager_t * sm, transport_connection_t * tc,
+ u8 alloc_fifos, stream_session_t ** ret_s)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0;
+ u32 fifo_segment_index;
+ u32 pool_index;
+ stream_session_t *s;
+ u64 value;
+ u32 thread_index = tc->thread_index;
+ int rv;
+
+ ASSERT (thread_index == vlib_get_thread_index ());
+
+ /* Create the session */
+ pool_get_aligned (smm->sessions[thread_index], s, CLIB_CACHE_LINE_BYTES);
+ memset (s, 0, sizeof (*s));
+ pool_index = s - smm->sessions[thread_index];
+
+ /* Allocate fifos */
+ if (alloc_fifos)
+ {
+ if ((rv = segment_manager_alloc_session_fifos (sm, &server_rx_fifo,
+ &server_tx_fifo,
+ &fifo_segment_index)))
+ {
+ pool_put (smm->sessions[thread_index], s);
+ return rv;
+ }
+ /* Initialize backpointers */
+ server_rx_fifo->master_session_index = pool_index;
+ server_rx_fifo->master_thread_index = thread_index;
+
+ server_tx_fifo->master_session_index = pool_index;
+ server_tx_fifo->master_thread_index = thread_index;
+
+ s->server_rx_fifo = server_rx_fifo;
+ s->server_tx_fifo = server_tx_fifo;
+ s->svm_segment_index = fifo_segment_index;
+ }
+
+ /* Initialize state machine, such as it is... */
+ s->session_type = session_type_from_proto_and_ip (tc->transport_proto,
+ tc->is_ip4);
+ s->session_state = SESSION_STATE_CONNECTING;
+ s->thread_index = thread_index;
+ s->session_index = pool_index;
+
+ /* Attach transport to session */
+ s->connection_index = tc->c_index;
+
+ /* Attach session to transport */
+ tc->s_index = s->session_index;
+
+ /* Add to the main lookup table */
+ value = stream_session_handle (s);
+ stream_session_table_add_for_tc (tc, value);
+
+ *ret_s = s;
+
+ return 0;
+}
+
+/**
+ * Discards bytes from buffer chain
+ *
+ * It discards n_bytes_to_drop starting at first buffer after chain_b
+ */
+always_inline void
+session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
+ vlib_buffer_t ** chain_b,
+ u32 n_bytes_to_drop)
+{
+ vlib_buffer_t *next = *chain_b;
+ u32 to_drop = n_bytes_to_drop;
+ ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
+ while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
+ {
+ next = vlib_get_buffer (vm, next->next_buffer);
+ if (next->current_length > to_drop)
+ {
+ vlib_buffer_advance (next, to_drop);
+ to_drop = 0;
+ }
+ else
+ {
+ to_drop -= next->current_length;
+ next->current_length = 0;
+ }
+ }
+ *chain_b = next;
+
+ if (to_drop == 0)
+ b->total_length_not_including_first_buffer -= n_bytes_to_drop;
+}
+
+/**
+ * Enqueue buffer chain tail
+ */
+always_inline int
+session_enqueue_chain_tail (stream_session_t * s, vlib_buffer_t * b,
+ u32 offset, u8 is_in_order)
+{
+ vlib_buffer_t *chain_b;
+ u32 chain_bi, len, diff;
+ vlib_main_t *vm = vlib_get_main ();
+ u8 *data;
+ u32 written = 0;
+ int rv = 0;
+
+ if (is_in_order && offset)
+ {
+ diff = offset - b->current_length;
+ if (diff > b->total_length_not_including_first_buffer)
+ return 0;
+ chain_b = b;
+ session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
+ chain_bi = vlib_get_buffer_index (vm, chain_b);
+ }
+ else
+ chain_bi = b->next_buffer;
+
+ do
+ {
+ chain_b = vlib_get_buffer (vm, chain_bi);
+ data = vlib_buffer_get_current (chain_b);
+ len = chain_b->current_length;
+ if (!len)
+ continue;
+ if (is_in_order)
+ {
+ rv = svm_fifo_enqueue_nowait (s->server_rx_fifo, len, data);
+ if (rv == len)
+ {
+ written += rv;
+ }
+ else if (rv < len)
+ {
+ return (rv > 0) ? (written + rv) : written;
+ }
+ else if (rv > len)
+ {
+ written += rv;
+
+ /* written more than what was left in chain */
+ if (written > b->total_length_not_including_first_buffer)
+ return written;
+
+ /* drop the bytes that have already been delivered */
+ session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
+ }
+ }
+ else
+ {
+ rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset, len,
+ data);
+ if (rv)
+ {
+ clib_warning ("failed to enqueue multi-buffer seg");
+ return -1;
+ }
+ offset += len;
+ }
+ }
+ while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
+ ? chain_b->next_buffer : 0));
+
+ if (is_in_order)
+ return written;
+
+ return 0;
+}
+
+/*
+ * Enqueue data for delivery to session peer. Does not notify peer of enqueue
+ * event but on request can queue notification events for later delivery by
+ * calling stream_server_flush_enqueue_events().
+ *
+ * @param tc Transport connection which is to be enqueued data
+ * @param b Buffer to be enqueued
+ * @param offset Offset at which to start enqueueing if out-of-order
+ * @param queue_event Flag to indicate if peer is to be notified or if event
+ * is to be queued. The former is useful when more data is
+ * enqueued and only one event is to be generated.
+ * @param is_in_order Flag to indicate if data is in order
+ * @return Number of bytes enqueued or a negative value if enqueueing failed.
+ */
+int
+stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
+ u32 offset, u8 queue_event, u8 is_in_order)
+{
+ stream_session_t *s;
+ int enqueued = 0, rv, in_order_off;
+
+ s = stream_session_get (tc->s_index, tc->thread_index);
+
+ if (is_in_order)
+ {
+ enqueued = svm_fifo_enqueue_nowait (s->server_rx_fifo,
+ b->current_length,
+ vlib_buffer_get_current (b));
+ if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
+ && enqueued >= 0))
+ {
+ in_order_off = enqueued > b->current_length ? enqueued : 0;
+ rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
+ if (rv > 0)
+ enqueued += rv;
+ }
+ }
+ else
+ {
+ rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset,
+ b->current_length,
+ vlib_buffer_get_current (b));
+ if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
+ session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
+ /* if something was enqueued, report even this as success for ooo
+ * segment handling */
+ return rv;
+ }
+
+ if (queue_event)
+ {
+ /* Queue RX event on this fifo. Eventually these will need to be flushed
+ * by calling stream_server_flush_enqueue_events () */
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ u32 thread_index = s->thread_index;
+ u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index];
+
+ if (s->enqueue_epoch != my_enqueue_epoch)
+ {
+ s->enqueue_epoch = my_enqueue_epoch;
+ vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index],
+ s - smm->sessions[thread_index]);
+ }
+ }
+
+ return enqueued;
+}
+
+/** Check if we have space in rx fifo to push more bytes */
+u8
+stream_session_no_space (transport_connection_t * tc, u32 thread_index,
+ u16 data_len)
+{
+ stream_session_t *s = stream_session_get (tc->s_index, thread_index);
+
+ if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY))
+ return 1;
+
+ if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo))
+ return 1;
+
+ return 0;
+}
+
+u32
+stream_session_tx_fifo_max_dequeue (transport_connection_t * tc)
+{
+ stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
+ if (!s->server_tx_fifo)
+ return 0;
+ return svm_fifo_max_dequeue (s->server_tx_fifo);
+}
+
+int
+stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
+ u32 offset, u32 max_bytes)
+{
+ stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
+ return svm_fifo_peek (s->server_tx_fifo, offset, max_bytes, buffer);
+}
+
+u32
+stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
+{
+ stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
+ return svm_fifo_dequeue_drop (s->server_tx_fifo, max_bytes);
+}
+
+/**
+ * Notify session peer that new data has been enqueued.
+ *
+ * @param s Stream session for which the event is to be generated.
+ * @param block Flag to indicate if call should block if event queue is full.
+ *
+ * @return 0 on succes or negative number if failed to send notification.
+ */
+static int
+stream_session_enqueue_notify (stream_session_t * s, u8 block)
+{
+ application_t *app;
+ session_fifo_event_t evt;
+ unix_shared_memory_queue_t *q;
+ static u32 serial_number;
+
+ if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
+ {
+ /* Session is closed so app will never clean up. Flush rx fifo */
+ u32 to_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
+ if (to_dequeue)
+ svm_fifo_dequeue_drop (s->server_rx_fifo, to_dequeue);
+ return 0;
+ }
+
+ /* Get session's server */
+ app = application_get_if_valid (s->app_index);
+
+ if (PREDICT_FALSE (app == 0))
+ {
+ clib_warning ("invalid s->app_index = %d", s->app_index);
+ return 0;
+ }
+
+ /* Built-in server? Hand event to the callback... */
+ if (app->cb_fns.builtin_server_rx_callback)
+ return app->cb_fns.builtin_server_rx_callback (s);
+
+ /* If no event, send one */
+ if (svm_fifo_set_event (s->server_rx_fifo))
+ {
+ /* Fabricate event */
+ evt.fifo = s->server_rx_fifo;
+ evt.event_type = FIFO_EVENT_APP_RX;
+ evt.event_id = serial_number++;
+
+ /* Add event to server's event queue */
+ q = app->event_queue;
+
+ /* Based on request block (or not) for lack of space */
+ if (block || PREDICT_TRUE (q->cursize < q->maxsize))
+ unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt,
+ 0 /* do wait for mutex */ );
+ else
+ {
+ clib_warning ("fifo full");
+ return -1;
+ }
+ }
+
+ /* *INDENT-OFF* */
+ SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
+ ed->data[0] = evt.event_id;
+ ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo);
+ }));
+ /* *INDENT-ON* */
+
+ return 0;
+}
+
+/**
+ * Flushes queue of sessions that are to be notified of new data
+ * enqueued events.
+ *
+ * @param thread_index Thread index for which the flush is to be performed.
+ * @return 0 on success or a positive number indicating the number of
+ * failures due to API queue being full.
+ */
+int
+session_manager_flush_enqueue_events (u32 thread_index)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ u32 *session_indices_to_enqueue;
+ int i, errors = 0;
+
+ session_indices_to_enqueue =
+ smm->session_indices_to_enqueue_by_thread[thread_index];
+
+ for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
+ {
+ stream_session_t *s0;
+
+ /* Get session */
+ s0 = stream_session_get_if_valid (session_indices_to_enqueue[i],
+ thread_index);
+ if (s0 == 0 || stream_session_enqueue_notify (s0, 0 /* don't block */ ))
+ {
+ errors++;
+ }
+ }
+
+ vec_reset_length (session_indices_to_enqueue);
+
+ smm->session_indices_to_enqueue_by_thread[thread_index] =
+ session_indices_to_enqueue;
+
+ /* Increment enqueue epoch for next round */
+ smm->current_enqueue_epoch[thread_index]++;
+
+ return errors;
+}
+
+/**
+ * Init fifo tail and head pointers
+ *
+ * Useful if transport uses absolute offsets for tracking ooo segments.
+ */
+void
+stream_session_init_fifos_pointers (transport_connection_t * tc,
+ u32 rx_pointer, u32 tx_pointer)
+{
+ stream_session_t *s;
+ s = stream_session_get (tc->s_index, tc->thread_index);
+ svm_fifo_init_pointers (s->server_rx_fifo, rx_pointer);
+ svm_fifo_init_pointers (s->server_tx_fifo, tx_pointer);
+}
+
+int
+stream_session_connect_notify (transport_connection_t * tc, u8 is_fail)
+{
+ application_t *app;
+ stream_session_t *new_s = 0;
+ u64 handle;
+ u32 opaque = 0;
+ int error = 0;
+ u8 st;
+
+ st = session_type_from_proto_and_ip (tc->transport_proto, tc->is_ip4);
+ handle = stream_session_half_open_lookup_handle (&tc->lcl_ip, &tc->rmt_ip,
+ tc->lcl_port, tc->rmt_port,
+ st);
+ if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
+ {
+ TCP_DBG ("half-open was removed!");
+ return -1;
+ }
+
+ /* Cleanup half-open table */
+ stream_session_half_open_table_del (tc);
+
+ /* Get the app's index from the handle we stored when opening connection
+ * and the opaque (api_context for external apps) from transport session
+ * index */
+ app = application_get_if_valid (handle >> 32);
+ if (!app)
+ return -1;
+
+ opaque = tc->s_index;
+
+ if (!is_fail)
+ {
+ segment_manager_t *sm;
+ u8 alloc_fifos;
+ sm = application_get_connect_segment_manager (app);
+ alloc_fifos = application_is_proxy (app);
+ /* Create new session (svm segments are allocated if needed) */
+ if (stream_session_create_i (sm, tc, alloc_fifos, &new_s))
+ {
+ is_fail = 1;
+ error = -1;
+ }
+ else
+ new_s->app_index = app->index;
+ }
+
+ /* Notify client application */
+ if (app->cb_fns.session_connected_callback (app->index, opaque, new_s,
+ is_fail))
+ {
+ clib_warning ("failed to notify app");
+ if (!is_fail)
+ stream_session_disconnect (new_s);
+ }
+ else
+ {
+ if (!is_fail)
+ new_s->session_state = SESSION_STATE_READY;
+ }
+
+ return error;
+}
+
+void
+stream_session_accept_notify (transport_connection_t * tc)
+{
+ application_t *server;
+ stream_session_t *s;
+
+ s = stream_session_get (tc->s_index, tc->thread_index);
+ server = application_get (s->app_index);
+ server->cb_fns.session_accept_callback (s);
+}
+
+/**
+ * Notification from transport that connection is being closed.
+ *
+ * A disconnect is sent to application but state is not removed. Once
+ * disconnect is acknowledged by application, session disconnect is called.
+ * Ultimately this leads to close being called on transport (passive close).
+ */
+void
+stream_session_disconnect_notify (transport_connection_t * tc)
+{
+ application_t *server;
+ stream_session_t *s;
+
+ s = stream_session_get (tc->s_index, tc->thread_index);
+ server = application_get (s->app_index);
+ server->cb_fns.session_disconnect_callback (s);
+}
+
+/**
+ * Cleans up session and lookup table.
+ */
+void
+stream_session_delete (stream_session_t * s)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ int rv;
+
+ /* Delete from the main lookup table. */
+ if ((rv = stream_session_table_del (s)))
+ clib_warning ("hash delete error, rv %d", rv);
+
+ /* Cleanup fifo segments */
+ segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo,
+ s->server_tx_fifo);
+
+ pool_put (smm->sessions[s->thread_index], s);
+ if (CLIB_DEBUG)
+ memset (s, 0xFA, sizeof (*s));
+}
+
+/**
+ * Notification from transport that connection is being deleted
+ *
+ * This removes the session if it is still valid. It should be called only on
+ * previously fully established sessions. For instance failed connects should
+ * call stream_session_connect_notify and indicate that the connect has
+ * failed.
+ */
+void
+stream_session_delete_notify (transport_connection_t * tc)
+{
+ stream_session_t *s;
+
+ /* App might've been removed already */
+ s = stream_session_get_if_valid (tc->s_index, tc->thread_index);
+ if (!s)
+ return;
+ stream_session_delete (s);
+}
+
+/**
+ * Notify application that connection has been reset.
+ */
+void
+stream_session_reset_notify (transport_connection_t * tc)
+{
+ stream_session_t *s;
+ application_t *app;
+ s = stream_session_get (tc->s_index, tc->thread_index);
+
+ app = application_get (s->app_index);
+ app->cb_fns.session_reset_callback (s);
+}
+
+/**
+ * Accept a stream session. Optionally ping the server by callback.
+ */
+int
+stream_session_accept (transport_connection_t * tc, u32 listener_index,
+ u8 sst, u8 notify)
+{
+ application_t *server;
+ stream_session_t *s, *listener;
+ segment_manager_t *sm;
+
+ int rv;
+
+ /* Find the server */
+ listener = listen_session_get (sst, listener_index);
+ server = application_get (listener->app_index);
+
+ sm = application_get_listen_segment_manager (server, listener);
+ if ((rv = stream_session_create_i (sm, tc, 1, &s)))
+ return rv;
+
+ s->app_index = server->index;
+ s->listener_index = listener_index;
+ s->session_state = SESSION_STATE_ACCEPTING;
+
+ /* Shoulder-tap the server */
+ if (notify)
+ {
+ server->cb_fns.session_accept_callback (s);
+ }
+
+ return 0;
+}
+
+/**
+ * Ask transport to open connection to remote transport endpoint.
+ *
+ * Stores handle for matching request with reply since the call can be
+ * asynchronous. For instance, for TCP the 3-way handshake must complete
+ * before reply comes. Session is only created once connection is established.
+ *
+ * @param app_index Index of the application requesting the connect
+ * @param st Session type requested.
+ * @param tep Remote transport endpoint
+ * @param res Resulting transport connection .
+ */
+int
+stream_session_open (u32 app_index, session_type_t st,
+ transport_endpoint_t * rmt,
+ transport_connection_t ** res)
+{
+ transport_connection_t *tc;
+ int rv;
+ u64 handle;
+
+ rv = tp_vfts[st].open (rmt);
+ if (rv < 0)
+ {
+ clib_warning ("Transport failed to open connection.");
+ return VNET_API_ERROR_SESSION_CONNECT_FAIL;
+ }
+
+ tc = tp_vfts[st].get_half_open ((u32) rv);
+
+ /* Save app and tc index. The latter is needed to help establish the
+ * connection while the former is needed when the connect notify comes
+ * and we have to notify the external app */
+ handle = (((u64) app_index) << 32) | (u64) tc->c_index;
+
+ /* Add to the half-open lookup table */
+ stream_session_half_open_table_add (tc, handle);
+
+ *res = tc;
+
+ return 0;
+}
+
+/**
+ * Ask transport to listen on local transport endpoint.
+ *
+ * @param s Session for which listen will be called. Note that unlike
+ * established sessions, listen sessions are not associated to a
+ * thread.
+ * @param tep Local endpoint to be listened on.
+ */
+int
+stream_session_listen (stream_session_t * s, transport_endpoint_t * tep)
+{
+ transport_connection_t *tc;
+ u32 tci;
+
+ /* Transport bind/listen */
+ tci = tp_vfts[s->session_type].bind (s->session_index, tep);
+
+ if (tci == (u32) ~ 0)
+ return -1;
+
+ /* Attach transport to session */
+ s->connection_index = tci;
+ tc = tp_vfts[s->session_type].get_listener (tci);
+
+ /* Weird but handle it ... */
+ if (tc == 0)
+ return -1;
+
+ /* Add to the main lookup table */
+ stream_session_table_add_for_tc (tc, s->session_index);
+
+ return 0;
+}
+
+/**
+ * Ask transport to stop listening on local transport endpoint.
+ *
+ * @param s Session to stop listening on. It must be in state LISTENING.
+ */
+int
+stream_session_stop_listen (stream_session_t * s)
+{
+ transport_connection_t *tc;
+
+ if (s->session_state != SESSION_STATE_LISTENING)
+ {
+ clib_warning ("not a listening session");
+ return -1;
+ }
+
+ tc = tp_vfts[s->session_type].get_listener (s->connection_index);
+ if (!tc)
+ {
+ clib_warning ("no transport");
+ return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
+ }
+
+ stream_session_table_del_for_tc (tc);
+ tp_vfts[s->session_type].unbind (s->connection_index);
+ return 0;
+}
+
+void
+session_send_session_evt_to_thread (u64 session_handle,
+ fifo_event_type_t evt_type,
+ u32 thread_index)
+{
+ static u16 serial_number = 0;
+ u32 tries = 0;
+ session_fifo_event_t evt;
+ unix_shared_memory_queue_t *q;
+
+ /* Fabricate event */
+ evt.session_handle = session_handle;
+ evt.event_type = evt_type;
+ evt.event_id = serial_number++;
+
+ q = session_manager_get_vpp_event_queue (thread_index);
+ while (unix_shared_memory_queue_add (q, (u8 *) & evt, 1))
+ {
+ if (tries++ == 3)
+ {
+ TCP_DBG ("failed to enqueue evt");
+ break;
+ }
+ }
+}
+
+/**
+ * Disconnect session and propagate to transport. This should eventually
+ * result in a delete notification that allows us to cleanup session state.
+ * Called for both active/passive disconnects.
+ *
+ * Should be called from the session's thread.
+ */
+void
+stream_session_disconnect (stream_session_t * s)
+{
+ s->session_state = SESSION_STATE_CLOSED;
+ tp_vfts[s->session_type].close (s->connection_index, s->thread_index);
+}
+
+/**
+ * Cleanup transport and session state.
+ *
+ * Notify transport of the cleanup, wait for a delete notify to actually
+ * remove the session state.
+ */
+void
+stream_session_cleanup (stream_session_t * s)
+{
+ int rv;
+
+ s->session_state = SESSION_STATE_CLOSED;
+
+ /* Delete from the main lookup table to avoid more enqueues */
+ rv = stream_session_table_del (s);
+ if (rv)
+ clib_warning ("hash delete error, rv %d", rv);
+
+ tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index);
+}
+
+/**
+ * Allocate vpp event queue (once) per worker thread
+ */
+void
+session_vpp_event_queue_allocate (session_manager_main_t * smm,
+ u32 thread_index)
+{
+ api_main_t *am = &api_main;
+ void *oldheap;
+ u32 event_queue_length = 2048;
+
+ if (smm->vpp_event_queues[thread_index] == 0)
+ {
+ /* Allocate event fifo in the /vpe-api shared-memory segment */
+ oldheap = svm_push_data_heap (am->vlib_rp);
+
+ if (smm->configured_event_queue_length)
+ event_queue_length = smm->configured_event_queue_length;
+
+ smm->vpp_event_queues[thread_index] =
+ unix_shared_memory_queue_init
+ (event_queue_length,
+ sizeof (session_fifo_event_t), 0 /* consumer pid */ ,
+ 0 /* (do not) send signal when queue non-empty */ );
+
+ svm_pop_heap (oldheap);
+ }
+}
+
+session_type_t
+session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
+{
+ if (proto == TRANSPORT_PROTO_TCP)
+ {
+ if (is_ip4)
+ return SESSION_TYPE_IP4_TCP;
+ else
+ return SESSION_TYPE_IP6_TCP;
+ }
+ else
+ {
+ if (is_ip4)
+ return SESSION_TYPE_IP4_UDP;
+ else
+ return SESSION_TYPE_IP6_UDP;
+ }
+
+ return SESSION_N_TYPES;
+}
+
+static clib_error_t *
+session_manager_main_enable (vlib_main_t * vm)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ vlib_thread_main_t *vtm = vlib_get_thread_main ();
+ u32 num_threads;
+ u32 preallocated_sessions_per_worker;
+ int i;
+
+ num_threads = 1 /* main thread */ + vtm->n_threads;
+
+ if (num_threads < 1)
+ return clib_error_return (0, "n_thread_stacks not set");
+
+ /* $$$ config parameters */
+ svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ ,
+ 20 /* timeout in seconds */ );
+
+ /* configure per-thread ** vectors */
+ vec_validate (smm->sessions, num_threads - 1);
+ vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1);
+ vec_validate (smm->tx_buffers, num_threads - 1);
+ vec_validate (smm->pending_event_vector, num_threads - 1);
+ vec_validate (smm->free_event_vector, num_threads - 1);
+ vec_validate (smm->current_enqueue_epoch, num_threads - 1);
+ vec_validate (smm->vpp_event_queues, num_threads - 1);
+
+ for (i = 0; i < num_threads; i++)
+ {
+ vec_validate (smm->free_event_vector[i], 0);
+ _vec_len (smm->free_event_vector[i]) = 0;
+ vec_validate (smm->pending_event_vector[i], 0);
+ _vec_len (smm->pending_event_vector[i]) = 0;
+ }
+
+#if SESSION_DBG
+ vec_validate (smm->last_event_poll_by_thread, num_threads - 1);
+#endif
+
+ /* Allocate vpp event queues */
+ for (i = 0; i < vec_len (smm->vpp_event_queues); i++)
+ session_vpp_event_queue_allocate (smm, i);
+
+ /* Preallocate sessions */
+ if (smm->preallocated_sessions)
+ {
+ if (num_threads == 1)
+ {
+ pool_init_fixed (smm->sessions[0], smm->preallocated_sessions);
+ }
+ else
+ {
+ int j;
+ preallocated_sessions_per_worker =
+ (1.1 * (f64) smm->preallocated_sessions /
+ (f64) (num_threads - 1));
+
+ for (j = 1; j < num_threads; j++)
+ {
+ pool_init_fixed (smm->sessions[j],
+ preallocated_sessions_per_worker);
+ }
+ }
+ }
+
+ session_lookup_init ();
+
+ smm->is_enabled = 1;
+
+ /* Enable TCP transport */
+ vnet_tcp_enable_disable (vm, 1);
+
+ return 0;
+}
+
+void
+session_node_enable_disable (u8 is_en)
+{
+ u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
+ /* *INDENT-OFF* */
+ foreach_vlib_main (({
+ vlib_node_set_state (this_vlib_main, session_queue_node.index,
+ state);
+ }));
+ /* *INDENT-ON* */
+}
+
+clib_error_t *
+vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
+{
+ if (is_en)
+ {
+ if (session_manager_main.is_enabled)
+ return 0;
+
+ session_node_enable_disable (is_en);
+
+ return session_manager_main_enable (vm);
+ }
+ else
+ {
+ session_manager_main.is_enabled = 0;
+ session_node_enable_disable (is_en);
+ }
+
+ return 0;
+}
+
+clib_error_t *
+session_manager_main_init (vlib_main_t * vm)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ smm->is_enabled = 0;
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (session_manager_main_init);
+
+static clib_error_t *
+session_config_fn (vlib_main_t * vm, unformat_input_t * input)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ u32 nitems;
+ uword tmp;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "event-queue-length %d", &nitems))
+ {
+ if (nitems >= 2048)
+ smm->configured_event_queue_length = nitems;
+ else
+ clib_warning ("event queue length %d too small, ignored", nitems);
+ }
+ else if (unformat (input, "preallocated-sessions %d",
+ &smm->preallocated_sessions))
+ ;
+ else if (unformat (input, "v4-session-table-buckets %d",
+ &smm->configured_v4_session_table_buckets))
+ ;
+ else if (unformat (input, "v4-halfopen-table-buckets %d",
+ &smm->configured_v4_halfopen_table_buckets))
+ ;
+ else if (unformat (input, "v6-session-table-buckets %d",
+ &smm->configured_v6_session_table_buckets))
+ ;
+ else if (unformat (input, "v6-halfopen-table-buckets %d",
+ &smm->configured_v6_halfopen_table_buckets))
+ ;
+ else if (unformat (input, "v4-session-table-memory %U",
+ unformat_memory_size, &tmp))
+ {
+ if (tmp >= 0x100000000)
+ return clib_error_return (0, "memory size %llx (%lld) too large",
+ tmp, tmp);
+ smm->configured_v4_session_table_memory = tmp;
+ }
+ else if (unformat (input, "v4-halfopen-table-memory %U",
+ unformat_memory_size, &tmp))
+ {
+ if (tmp >= 0x100000000)
+ return clib_error_return (0, "memory size %llx (%lld) too large",
+ tmp, tmp);
+ smm->configured_v4_halfopen_table_memory = tmp;
+ }
+ else if (unformat (input, "v6-session-table-memory %U",
+ unformat_memory_size, &tmp))
+ {
+ if (tmp >= 0x100000000)
+ return clib_error_return (0, "memory size %llx (%lld) too large",
+ tmp, tmp);
+ smm->configured_v6_session_table_memory = tmp;
+ }
+ else if (unformat (input, "v6-halfopen-table-memory %U",
+ unformat_memory_size, &tmp))
+ {
+ if (tmp >= 0x100000000)
+ return clib_error_return (0, "memory size %llx (%lld) too large",
+ tmp, tmp);
+ smm->configured_v6_halfopen_table_memory = tmp;
+ }
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+ return 0;
+}
+
+VLIB_CONFIG_FUNCTION (session_config_fn, "session");
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session.h b/src/vnet/session/session.h
new file mode 100644
index 00000000..83addec2
--- /dev/null
+++ b/src/vnet/session/session.h
@@ -0,0 +1,436 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef __included_session_h__
+#define __included_session_h__
+
+#include <vnet/session/stream_session.h>
+#include <vnet/session/session_lookup.h>
+#include <vnet/session/transport_interface.h>
+#include <vlibmemory/unix_shared_memory_queue.h>
+#include <vnet/session/session_debug.h>
+#include <vnet/session/segment_manager.h>
+
+#define HALF_OPEN_LOOKUP_INVALID_VALUE ((u64)~0)
+#define INVALID_INDEX ((u32)~0)
+
+/* TODO decide how much since we have pre-data as well */
+#define MAX_HDRS_LEN 100 /* Max number of bytes for headers */
+
+typedef enum
+{
+ FIFO_EVENT_APP_RX,
+ FIFO_EVENT_APP_TX,
+ FIFO_EVENT_TIMEOUT,
+ FIFO_EVENT_DISCONNECT,
+ FIFO_EVENT_BUILTIN_RX,
+ FIFO_EVENT_RPC,
+} fifo_event_type_t;
+
+static inline const char *
+fifo_event_type_str (fifo_event_type_t et)
+{
+ switch (et)
+ {
+ case FIFO_EVENT_APP_RX:
+ return "FIFO_EVENT_APP_RX";
+ case FIFO_EVENT_APP_TX:
+ return "FIFO_EVENT_APP_TX";
+ case FIFO_EVENT_TIMEOUT:
+ return "FIFO_EVENT_TIMEOUT";
+ case FIFO_EVENT_DISCONNECT:
+ return "FIFO_EVENT_DISCONNECT";
+ case FIFO_EVENT_BUILTIN_RX:
+ return "FIFO_EVENT_BUILTIN_RX";
+ case FIFO_EVENT_RPC:
+ return "FIFO_EVENT_RPC";
+ default:
+ return "UNKNOWN FIFO EVENT";
+ }
+}
+
+#define foreach_session_input_error \
+_(NO_SESSION, "No session drops") \
+_(NO_LISTENER, "No listener for dst port drops") \
+_(ENQUEUED, "Packets pushed into rx fifo") \
+_(NOT_READY, "Session not ready packets") \
+_(FIFO_FULL, "Packets dropped for lack of rx fifo space") \
+_(EVENT_FIFO_FULL, "Events not sent for lack of event fifo space") \
+_(API_QUEUE_FULL, "Sessions not created for lack of API queue space") \
+_(NEW_SEG_NO_SPACE, "Created segment, couldn't allocate a fifo pair") \
+_(NO_SPACE, "Couldn't allocate a fifo pair")
+
+typedef enum
+{
+#define _(sym,str) SESSION_ERROR_##sym,
+ foreach_session_input_error
+#undef _
+ SESSION_N_ERROR,
+} session_error_t;
+
+/* Event queue input node static next indices */
+typedef enum
+{
+ SESSION_QUEUE_NEXT_DROP,
+ SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
+ SESSION_QUEUE_NEXT_IP4_LOOKUP,
+ SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
+ SESSION_QUEUE_NEXT_IP6_LOOKUP,
+ SESSION_QUEUE_N_NEXT,
+} session_queue_next_t;
+
+typedef struct
+{
+ void *fp;
+ void *arg;
+} rpc_args_t;
+
+/* *INDENT-OFF* */
+typedef CLIB_PACKED (struct {
+ union
+ {
+ svm_fifo_t * fifo;
+ u64 session_handle;
+ rpc_args_t rpc_args;
+ };
+ u8 event_type;
+ u16 event_id;
+}) session_fifo_event_t;
+/* *INDENT-ON* */
+
+/* Forward definition */
+typedef struct _session_manager_main session_manager_main_t;
+
+typedef int
+ (session_fifo_rx_fn) (vlib_main_t * vm, vlib_node_runtime_t * node,
+ session_manager_main_t * smm,
+ session_fifo_event_t * e0, stream_session_t * s0,
+ u32 thread_index, int *n_tx_pkts);
+
+extern session_fifo_rx_fn session_tx_fifo_peek_and_snd;
+extern session_fifo_rx_fn session_tx_fifo_dequeue_and_snd;
+
+u8 session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e);
+
+struct _session_manager_main
+{
+ /** Per worker thread session pools */
+ stream_session_t **sessions;
+
+ /** Pool of listen sessions. Same type as stream sessions to ease lookups */
+ stream_session_t *listen_sessions[SESSION_N_TYPES];
+
+ /** Sparse vector to map dst port to stream server */
+ u16 *stream_server_by_dst_port[SESSION_N_TYPES];
+
+ /** per-worker enqueue epoch counters */
+ u8 *current_enqueue_epoch;
+
+ /** Per-worker thread vector of sessions to enqueue */
+ u32 **session_indices_to_enqueue_by_thread;
+
+ /** per-worker tx buffer free lists */
+ u32 **tx_buffers;
+
+ /** Per worker-thread vector of partially read events */
+ session_fifo_event_t **free_event_vector;
+
+ /** per-worker active event vectors */
+ session_fifo_event_t **pending_event_vector;
+
+ /** vpp fifo event queue */
+ unix_shared_memory_queue_t **vpp_event_queues;
+
+ /** vpp fifo event queue configured length */
+ u32 configured_event_queue_length;
+
+ /** session table size parameters */
+ u32 configured_v4_session_table_buckets;
+ u32 configured_v4_session_table_memory;
+ u32 configured_v4_halfopen_table_buckets;
+ u32 configured_v4_halfopen_table_memory;
+ u32 configured_v6_session_table_buckets;
+ u32 configured_v6_session_table_memory;
+ u32 configured_v6_halfopen_table_buckets;
+ u32 configured_v6_halfopen_table_memory;
+
+ /** Unique segment name counter */
+ u32 unique_segment_name_counter;
+
+ /** Per transport rx function that can either dequeue or peek */
+ session_fifo_rx_fn *session_tx_fns[SESSION_N_TYPES];
+
+ /** Session manager is enabled */
+ u8 is_enabled;
+
+ /** Preallocate session config parameter */
+ u32 preallocated_sessions;
+
+#if SESSION_DBG
+ /**
+ * last event poll time by thread
+ * Debug only. Will cause false cache-line sharing as-is
+ */
+ f64 *last_event_poll_by_thread;
+#endif
+
+};
+
+extern session_manager_main_t session_manager_main;
+extern vlib_node_registration_t session_queue_node;
+
+/*
+ * Session manager function
+ */
+always_inline session_manager_main_t *
+vnet_get_session_manager_main ()
+{
+ return &session_manager_main;
+}
+
+always_inline u8
+stream_session_is_valid (u32 si, u8 thread_index)
+{
+ stream_session_t *s;
+ s = pool_elt_at_index (session_manager_main.sessions[thread_index], si);
+ if (s->thread_index != thread_index || s->session_index != si
+ /* || s->server_rx_fifo->master_session_index != si
+ || s->server_tx_fifo->master_session_index != si
+ || s->server_rx_fifo->master_thread_index != thread_index
+ || s->server_tx_fifo->master_thread_index != thread_index */ )
+ return 0;
+ return 1;
+}
+
+always_inline stream_session_t *
+stream_session_get (u32 si, u32 thread_index)
+{
+ ASSERT (stream_session_is_valid (si, thread_index));
+ return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
+}
+
+always_inline stream_session_t *
+stream_session_get_if_valid (u64 si, u32 thread_index)
+{
+ if (thread_index >= vec_len (session_manager_main.sessions))
+ return 0;
+
+ if (pool_is_free_index (session_manager_main.sessions[thread_index], si))
+ return 0;
+
+ ASSERT (stream_session_is_valid (si, thread_index));
+ return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
+}
+
+always_inline u64
+stream_session_handle (stream_session_t * s)
+{
+ return ((u64) s->thread_index << 32) | (u64) s->session_index;
+}
+
+always_inline u32
+stream_session_index_from_handle (u64 handle)
+{
+ return handle & 0xFFFFFFFF;
+}
+
+always_inline u32
+stream_session_thread_from_handle (u64 handle)
+{
+ return handle >> 32;
+}
+
+always_inline void
+stream_session_parse_handle (u64 handle, u32 * index, u32 * thread_index)
+{
+ *index = stream_session_index_from_handle (handle);
+ *thread_index = stream_session_thread_from_handle (handle);
+}
+
+always_inline stream_session_t *
+stream_session_get_from_handle (u64 handle)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ return pool_elt_at_index (smm->sessions[stream_session_thread_from_handle
+ (handle)],
+ stream_session_index_from_handle (handle));
+}
+
+always_inline stream_session_t *
+stream_session_listener_get (u8 sst, u64 si)
+{
+ return pool_elt_at_index (session_manager_main.listen_sessions[sst], si);
+}
+
+always_inline u32
+stream_session_get_index (stream_session_t * s)
+{
+ if (s->session_state == SESSION_STATE_LISTENING)
+ return s - session_manager_main.listen_sessions[s->session_type];
+
+ return s - session_manager_main.sessions[s->thread_index];
+}
+
+always_inline u32
+stream_session_max_rx_enqueue (transport_connection_t * tc)
+{
+ stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
+ return svm_fifo_max_enqueue (s->server_rx_fifo);
+}
+
+always_inline u32
+stream_session_rx_fifo_size (transport_connection_t * tc)
+{
+ stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
+ return s->server_rx_fifo->nitems;
+}
+
+u32 stream_session_tx_fifo_max_dequeue (transport_connection_t * tc);
+
+int
+stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
+ u32 offset, u8 queue_event, u8 is_in_order);
+int
+stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
+ u32 offset, u32 max_bytes);
+u32 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
+
+int stream_session_connect_notify (transport_connection_t * tc, u8 is_fail);
+void stream_session_init_fifos_pointers (transport_connection_t * tc,
+ u32 rx_pointer, u32 tx_pointer);
+
+void stream_session_accept_notify (transport_connection_t * tc);
+void stream_session_disconnect_notify (transport_connection_t * tc);
+void stream_session_delete_notify (transport_connection_t * tc);
+void stream_session_reset_notify (transport_connection_t * tc);
+int
+stream_session_accept (transport_connection_t * tc, u32 listener_index,
+ u8 sst, u8 notify);
+int
+stream_session_open (u32 app_index, session_type_t st,
+ transport_endpoint_t * tep,
+ transport_connection_t ** tc);
+int stream_session_listen (stream_session_t * s, transport_endpoint_t * tep);
+int stream_session_stop_listen (stream_session_t * s);
+void stream_session_disconnect (stream_session_t * s);
+void stream_session_cleanup (stream_session_t * s);
+void session_send_session_evt_to_thread (u64 session_handle,
+ fifo_event_type_t evt_type,
+ u32 thread_index);
+
+u8 *format_stream_session (u8 * s, va_list * args);
+uword unformat_stream_session (unformat_input_t * input, va_list * args);
+uword unformat_transport_connection (unformat_input_t * input,
+ va_list * args);
+
+int
+send_session_connected_callback (u32 app_index, u32 api_context,
+ stream_session_t * s, u8 is_fail);
+
+
+clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
+
+always_inline unix_shared_memory_queue_t *
+session_manager_get_vpp_event_queue (u32 thread_index)
+{
+ return session_manager_main.vpp_event_queues[thread_index];
+}
+
+int session_manager_flush_enqueue_events (u32 thread_index);
+
+always_inline u64
+listen_session_get_handle (stream_session_t * s)
+{
+ ASSERT (s->session_state == SESSION_STATE_LISTENING);
+ return ((u64) s->session_type << 32) | s->session_index;
+}
+
+always_inline stream_session_t *
+listen_session_get_from_handle (u64 handle)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ stream_session_t *s;
+ u32 type, index;
+ type = handle >> 32;
+ index = handle & 0xFFFFFFFF;
+
+ if (pool_is_free_index (smm->listen_sessions[type], index))
+ return 0;
+
+ s = pool_elt_at_index (smm->listen_sessions[type], index);
+ ASSERT (s->session_state == SESSION_STATE_LISTENING);
+ return s;
+}
+
+always_inline stream_session_t *
+listen_session_new (session_type_t type)
+{
+ stream_session_t *s;
+ pool_get_aligned (session_manager_main.listen_sessions[type], s,
+ CLIB_CACHE_LINE_BYTES);
+ memset (s, 0, sizeof (*s));
+
+ s->session_type = type;
+ s->session_state = SESSION_STATE_LISTENING;
+ s->session_index = s - session_manager_main.listen_sessions[type];
+
+ return s;
+}
+
+always_inline stream_session_t *
+listen_session_get (session_type_t type, u32 index)
+{
+ return pool_elt_at_index (session_manager_main.listen_sessions[type],
+ index);
+}
+
+always_inline void
+listen_session_del (stream_session_t * s)
+{
+ pool_put (session_manager_main.listen_sessions[s->session_type], s);
+}
+
+always_inline stream_session_t *
+session_manager_get_listener (u8 type, u32 index)
+{
+ return pool_elt_at_index (session_manager_main.listen_sessions[type],
+ index);
+}
+
+always_inline void
+session_manager_set_transport_rx_fn (u8 type, u8 is_peek)
+{
+ /* If an offset function is provided, then peek instead of dequeue */
+ session_manager_main.session_tx_fns[type] = (is_peek) ?
+ session_tx_fifo_peek_and_snd : session_tx_fifo_dequeue_and_snd;
+}
+
+session_type_t
+session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4);
+
+always_inline u8
+session_manager_is_enabled ()
+{
+ return session_manager_main.is_enabled == 1;
+}
+
+#endif /* __included_session_h__ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c
new file mode 100755
index 00000000..250f9906
--- /dev/null
+++ b/src/vnet/session/session_api.c
@@ -0,0 +1,763 @@
+/*
+ * Copyright (c) 2015-2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <vnet/vnet.h>
+#include <vlibmemory/api.h>
+#include <vnet/session/application.h>
+
+#include <vnet/vnet_msg_enum.h>
+#include "application_interface.h"
+
+#define vl_typedefs /* define message structures */
+#include <vnet/vnet_all_api_h.h>
+#undef vl_typedefs
+
+#define vl_endianfun /* define message structures */
+#include <vnet/vnet_all_api_h.h>
+#undef vl_endianfun
+
+/* instantiate all the print functions we know about */
+#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
+#define vl_printfun
+#include <vnet/vnet_all_api_h.h>
+#undef vl_printfun
+
+#include <vlibapi/api_helper_macros.h>
+
+#define foreach_session_api_msg \
+_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
+_(APPLICATION_ATTACH, application_attach) \
+_(APPLICATION_DETACH, application_detach) \
+_(BIND_URI, bind_uri) \
+_(UNBIND_URI, unbind_uri) \
+_(CONNECT_URI, connect_uri) \
+_(DISCONNECT_SESSION, disconnect_session) \
+_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
+_(ACCEPT_SESSION_REPLY, accept_session_reply) \
+_(RESET_SESSION_REPLY, reset_session_reply) \
+_(BIND_SOCK, bind_sock) \
+_(UNBIND_SOCK, unbind_sock) \
+_(CONNECT_SOCK, connect_sock) \
+_(SESSION_ENABLE_DISABLE, session_enable_disable) \
+
+static int
+send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
+ u32 segment_size)
+{
+ vl_api_map_another_segment_t *mp;
+ unix_shared_memory_queue_t *q;
+
+ q = vl_api_client_index_to_input_queue (api_client_index);
+
+ if (!q)
+ return -1;
+
+ mp = vl_msg_api_alloc (sizeof (*mp));
+ memset (mp, 0, sizeof (*mp));
+ mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
+ mp->segment_size = segment_size;
+ strncpy ((char *) mp->segment_name, (char *) segment_name,
+ sizeof (mp->segment_name) - 1);
+
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+
+ return 0;
+}
+
+static int
+send_session_accept_callback (stream_session_t * s)
+{
+ vl_api_accept_session_t *mp;
+ unix_shared_memory_queue_t *q, *vpp_queue;
+ application_t *server = application_get (s->app_index);
+ transport_connection_t *tc;
+ transport_proto_vft_t *tp_vft;
+ stream_session_t *listener;
+
+ q = vl_api_client_index_to_input_queue (server->api_client_index);
+ vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
+
+ if (!q)
+ return -1;
+
+ mp = vl_msg_api_alloc (sizeof (*mp));
+ memset (mp, 0, sizeof (*mp));
+
+ mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
+ mp->context = server->index;
+ listener = listen_session_get (s->session_type, s->listener_index);
+ tp_vft = session_get_transport_vft (s->session_type);
+ tc = tp_vft->get_connection (s->connection_index, s->thread_index);
+ mp->listener_handle = listen_session_get_handle (listener);
+ mp->handle = stream_session_handle (s);
+ mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
+ mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
+ mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
+ mp->port = tc->rmt_port;
+ mp->is_ip4 = tc->is_ip4;
+ clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+
+ return 0;
+}
+
+static void
+send_session_disconnect_callback (stream_session_t * s)
+{
+ vl_api_disconnect_session_t *mp;
+ unix_shared_memory_queue_t *q;
+ application_t *app = application_get (s->app_index);
+
+ q = vl_api_client_index_to_input_queue (app->api_client_index);
+
+ if (!q)
+ return;
+
+ mp = vl_msg_api_alloc (sizeof (*mp));
+ memset (mp, 0, sizeof (*mp));
+ mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
+ mp->handle = stream_session_handle (s);
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+}
+
+static void
+send_session_reset_callback (stream_session_t * s)
+{
+ vl_api_reset_session_t *mp;
+ unix_shared_memory_queue_t *q;
+ application_t *app = application_get (s->app_index);
+
+ q = vl_api_client_index_to_input_queue (app->api_client_index);
+
+ if (!q)
+ return;
+
+ mp = vl_msg_api_alloc (sizeof (*mp));
+ memset (mp, 0, sizeof (*mp));
+ mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
+ mp->handle = stream_session_handle (s);
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+}
+
+int
+send_session_connected_callback (u32 app_index, u32 api_context,
+ stream_session_t * s, u8 is_fail)
+{
+ vl_api_connect_session_reply_t *mp;
+ unix_shared_memory_queue_t *q;
+ application_t *app;
+ unix_shared_memory_queue_t *vpp_queue;
+
+ app = application_get (app_index);
+ q = vl_api_client_index_to_input_queue (app->api_client_index);
+
+ if (!q)
+ return -1;
+
+ mp = vl_msg_api_alloc (sizeof (*mp));
+ mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
+ mp->context = api_context;
+ if (!is_fail)
+ {
+ vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
+ mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
+ mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
+ mp->handle = stream_session_handle (s);
+ mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
+ mp->retval = 0;
+ }
+ else
+ {
+ mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT_FAIL);
+ }
+
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+ return 0;
+}
+
+/**
+ * Redirect a connect_uri message to the indicated server.
+ * Only sent if the server has bound the related port with
+ * URI_OPTIONS_FLAGS_USE_FIFO
+ */
+static int
+redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
+{
+ vl_api_connect_uri_t *mp = mp_arg;
+ unix_shared_memory_queue_t *server_q, *client_q;
+ vlib_main_t *vm = vlib_get_main ();
+ f64 timeout = vlib_time_now (vm) + 0.5;
+ application_t *app;
+ int rv = 0;
+
+ server_q = vl_api_client_index_to_input_queue (server_api_client_index);
+
+ if (!server_q)
+ {
+ rv = VNET_API_ERROR_INVALID_VALUE;
+ goto out;
+ }
+
+ client_q = vl_api_client_index_to_input_queue (mp->client_index);
+ if (!client_q)
+ {
+ rv = VNET_API_ERROR_INVALID_VALUE_2;
+ goto out;
+ }
+
+ /* Tell the server the client's API queue address, so it can reply */
+ mp->client_queue_address = pointer_to_uword (client_q);
+ app = application_lookup (mp->client_index);
+ if (!app)
+ {
+ clib_warning ("no client application");
+ return -1;
+ }
+
+ mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
+ mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
+
+ /*
+ * Bounce message handlers MUST NOT block the data-plane.
+ * Spin waiting for the queue lock, but
+ */
+
+ while (vlib_time_now (vm) < timeout)
+ {
+ rv =
+ unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
+ switch (rv)
+ {
+ /* correctly enqueued */
+ case 0:
+ return VNET_CONNECT_REDIRECTED;
+
+ /* continue spinning, wait for pthread_mutex_trylock to work */
+ case -1:
+ continue;
+
+ /* queue stuffed, drop the msg */
+ case -2:
+ rv = VNET_API_ERROR_QUEUE_FULL;
+ goto out;
+ }
+ }
+out:
+ /* Dispose of the message */
+ vl_msg_api_free (mp);
+ return rv;
+}
+
+static session_cb_vft_t uri_session_cb_vft = {
+ .session_accept_callback = send_session_accept_callback,
+ .session_disconnect_callback = send_session_disconnect_callback,
+ .session_connected_callback = send_session_connected_callback,
+ .session_reset_callback = send_session_reset_callback,
+ .add_segment_callback = send_add_segment_callback,
+ .redirect_connect_callback = redirect_connect_callback
+};
+
+static void
+vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
+{
+ vl_api_session_enable_disable_reply_t *rmp;
+ vlib_main_t *vm = vlib_get_main ();
+ int rv = 0;
+
+ vnet_session_enable_disable (vm, mp->is_enable);
+ REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
+}
+
+static void
+vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
+{
+ vl_api_application_attach_reply_t *rmp;
+ vnet_app_attach_args_t _a, *a = &_a;
+ int rv;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
+ sizeof (mp->options),
+ "Out of options, fix api message definition");
+
+ memset (a, 0, sizeof (*a));
+
+ a->api_client_index = mp->client_index;
+ a->options = mp->options;
+ a->session_cb_vft = &uri_session_cb_vft;
+
+ rv = vnet_application_attach (a);
+
+done:
+
+ /* *INDENT-OFF* */
+ REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
+ if (!rv)
+ {
+ rmp->segment_name_length = 0;
+ /* $$$$ policy? */
+ rmp->segment_size = a->segment_size;
+ if (a->segment_name_length)
+ {
+ memcpy (rmp->segment_name, a->segment_name,
+ a->segment_name_length);
+ rmp->segment_name_length = a->segment_name_length;
+ }
+ rmp->app_event_queue_address = a->app_event_queue_address;
+ }
+ }));
+ /* *INDENT-ON* */
+}
+
+static void
+vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
+{
+ vl_api_application_detach_reply_t *rmp;
+ int rv = VNET_API_ERROR_INVALID_VALUE_2;
+ vnet_app_detach_args_t _a, *a = &_a;
+ application_t *app;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ a->app_index = app->index;
+ rv = vnet_application_detach (a);
+ }
+
+done:
+ REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
+}
+
+static void
+vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
+{
+ vl_api_bind_uri_reply_t *rmp;
+ vnet_bind_args_t _a, *a = &_a;
+ application_t *app;
+ int rv;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ memset (a, 0, sizeof (*a));
+ a->uri = (char *) mp->uri;
+ a->app_index = app->index;
+ rv = vnet_bind_uri (a);
+ }
+ else
+ {
+ rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ }
+
+done:
+ REPLY_MACRO (VL_API_BIND_URI_REPLY);
+}
+
+static void
+vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
+{
+ vl_api_unbind_uri_reply_t *rmp;
+ application_t *app;
+ vnet_unbind_args_t _a, *a = &_a;
+ int rv;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ a->uri = (char *) mp->uri;
+ a->app_index = app->index;
+ rv = vnet_unbind_uri (a);
+ }
+ else
+ {
+ rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ }
+
+done:
+ REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
+}
+
+static void
+vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
+{
+ vl_api_connect_session_reply_t *rmp;
+ vnet_connect_args_t _a, *a = &_a;
+ application_t *app;
+ int rv;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ a->uri = (char *) mp->uri;
+ a->api_context = mp->context;
+ a->app_index = app->index;
+ a->mp = mp;
+ rv = vnet_connect_uri (a);
+ }
+ else
+ {
+ rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ }
+
+ if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
+ return;
+
+ /* Got some error, relay it */
+
+done:
+ /* *INDENT-OFF* */
+ REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
+ /* *INDENT-ON* */
+}
+
+static void
+vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
+{
+ vl_api_disconnect_session_reply_t *rmp;
+ vnet_disconnect_args_t _a, *a = &_a;
+ application_t *app;
+ int rv = 0;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ a->handle = mp->handle;
+ a->app_index = app->index;
+ rv = vnet_disconnect_session (a);
+ }
+ else
+ {
+ rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ }
+
+done:
+ REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
+}
+
+static void
+vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
+ mp)
+{
+ vnet_disconnect_args_t _a, *a = &_a;
+ application_t *app;
+
+ /* Client objected to disconnecting the session, log and continue */
+ if (mp->retval)
+ {
+ clib_warning ("client retval %d", mp->retval);
+ return;
+ }
+
+ /* Disconnect has been confirmed. Confirm close to transport */
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ a->handle = mp->handle;
+ a->app_index = app->index;
+ vnet_disconnect_session (a);
+ }
+}
+
+static void
+vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
+{
+ application_t *app;
+ stream_session_t *s;
+ u32 index, thread_index;
+
+ app = application_lookup (mp->client_index);
+ if (!app)
+ return;
+
+ stream_session_parse_handle (mp->handle, &index, &thread_index);
+ s = stream_session_get_if_valid (index, thread_index);
+ if (s == 0 || app->index != s->app_index)
+ {
+ clib_warning ("Invalid session!");
+ return;
+ }
+
+ /* Client objected to resetting the session, log and continue */
+ if (mp->retval)
+ {
+ clib_warning ("client retval %d", mp->retval);
+ return;
+ }
+
+ /* This comes as a response to a reset, transport only waiting for
+ * confirmation to remove connection state, no need to disconnect */
+ stream_session_cleanup (s);
+}
+
+static void
+vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
+{
+ stream_session_t *s;
+ u32 session_index, thread_index;
+ vnet_disconnect_args_t _a, *a = &_a;
+
+ /* Server isn't interested, kill the session */
+ if (mp->retval)
+ {
+ a->app_index = mp->context;
+ a->handle = mp->handle;
+ vnet_disconnect_session (a);
+ }
+ else
+ {
+ stream_session_parse_handle (mp->handle, &session_index, &thread_index);
+ s = stream_session_get_if_valid (session_index, thread_index);
+ if (!s)
+ {
+ clib_warning ("session doesn't exist");
+ return;
+ }
+ if (s->app_index != mp->context)
+ {
+ clib_warning ("app doesn't own session");
+ return;
+ }
+ /* XXX volatile? */
+ s->session_state = SESSION_STATE_READY;
+ }
+}
+
+static void
+vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
+ * mp)
+{
+ clib_warning ("not implemented");
+}
+
+static void
+vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
+{
+ vl_api_bind_sock_reply_t *rmp;
+ vnet_bind_args_t _a, *a = &_a;
+ int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ application_t *app;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
+
+ memset (a, 0, sizeof (*a));
+ a->tep.is_ip4 = mp->is_ip4;
+ a->tep.ip = *ip46;
+ a->tep.port = mp->port;
+ a->tep.vrf = mp->vrf;
+ a->app_index = app->index;
+
+ rv = vnet_bind (a);
+ }
+done:
+ /* *INDENT-OFF* */
+ REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
+ if (!rv)
+ rmp->handle = a->handle;
+ }));
+ /* *INDENT-ONF* */
+}
+
+static void
+vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
+{
+ vl_api_unbind_sock_reply_t *rmp;
+ vnet_unbind_args_t _a, *a = &_a;
+ application_t *app;
+ int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ a->app_index = mp->client_index;
+ a->handle = mp->handle;
+ rv = vnet_unbind (a);
+ }
+
+done:
+ REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
+}
+
+static void
+vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
+{
+ vl_api_connect_session_reply_t *rmp;
+ vnet_connect_args_t _a, *a = &_a;
+ application_t *app;
+ int rv;
+
+ if (session_manager_is_enabled () == 0)
+ {
+ rv = VNET_API_ERROR_FEATURE_DISABLED;
+ goto done;
+ }
+
+ app = application_lookup (mp->client_index);
+ if (app)
+ {
+ unix_shared_memory_queue_t *client_q;
+ ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
+
+ client_q = vl_api_client_index_to_input_queue (mp->client_index);
+ mp->client_queue_address = pointer_to_uword (client_q);
+ a->tep.is_ip4 = mp->is_ip4;
+ a->tep.ip = *ip46;
+ a->tep.port = mp->port;
+ a->tep.vrf = mp->vrf;
+ a->api_context = mp->context;
+ a->app_index = app->index;
+ a->proto = mp->proto;
+ a->mp = mp;
+ rv = vnet_connect (a);
+ }
+ else
+ {
+ rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+ }
+
+ if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
+ return;
+
+ /* Got some error, relay it */
+
+done:
+ REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
+}
+
+static clib_error_t *
+application_reaper_cb (u32 client_index)
+{
+ application_t *app = application_lookup (client_index);
+ vnet_app_detach_args_t _a, *a = &_a;
+ if (app)
+ {
+ a->app_index = app->index;
+ vnet_application_detach (a);
+ }
+ return 0;
+}
+
+VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
+
+#define vl_msg_name_crc_list
+#include <vnet/vnet_all_api_h.h>
+#undef vl_msg_name_crc_list
+
+static void
+setup_message_id_table (api_main_t * am)
+{
+#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
+ foreach_vl_msg_name_crc_session;
+#undef _
+}
+
+/*
+ * session_api_hookup
+ * Add uri's API message handlers to the table.
+ * vlib has alread mapped shared memory and
+ * added the client registration handlers.
+ * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
+ */
+static clib_error_t *
+session_api_hookup (vlib_main_t * vm)
+{
+ api_main_t *am = &api_main;
+
+#define _(N,n) \
+ vl_msg_api_set_handlers(VL_API_##N, #n, \
+ vl_api_##n##_t_handler, \
+ vl_noop_handler, \
+ vl_api_##n##_t_endian, \
+ vl_api_##n##_t_print, \
+ sizeof(vl_api_##n##_t), 1);
+ foreach_session_api_msg;
+#undef _
+
+ /*
+ * Messages which bounce off the data-plane to
+ * an API client. Simply tells the message handling infra not
+ * to free the message.
+ *
+ * Bounced message handlers MUST NOT block the data plane
+ */
+ am->message_bounce[VL_API_CONNECT_URI] = 1;
+ am->message_bounce[VL_API_CONNECT_SOCK] = 1;
+
+ /*
+ * Set up the (msg_name, crc, message-id) table
+ */
+ setup_message_id_table (am);
+
+ return 0;
+}
+
+VLIB_API_INIT_FUNCTION (session_api_hookup);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session_cli.c b/src/vnet/session/session_cli.c
new file mode 100755
index 00000000..8c30a1df
--- /dev/null
+++ b/src/vnet/session/session_cli.c
@@ -0,0 +1,494 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <vnet/session/application.h>
+#include <vnet/session/session.h>
+
+u8 *
+format_stream_session_fifos (u8 * s, va_list * args)
+{
+ stream_session_t *ss = va_arg (*args, stream_session_t *);
+ int verbose = va_arg (*args, int);
+ session_fifo_event_t _e, *e = &_e;
+ u8 found;
+
+ s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo, 1);
+ if (verbose > 2 && ss->server_rx_fifo->has_event)
+ {
+ found = session_node_lookup_fifo_event (ss->server_rx_fifo, e);
+ s = format (s, " session node event: %s\n",
+ found ? "found" : "not found");
+ }
+ s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo, 1);
+ if (verbose > 2 && ss->server_tx_fifo->has_event)
+ {
+ found = session_node_lookup_fifo_event (ss->server_tx_fifo, e);
+ s = format (s, " session node event: %s\n",
+ found ? "found" : "not found");
+ }
+ return s;
+}
+
+/**
+ * Format stream session as per the following format
+ *
+ * verbose:
+ * "Connection", "Rx fifo", "Tx fifo", "Session Index"
+ * non-verbose:
+ * "Connection"
+ */
+u8 *
+format_stream_session (u8 * s, va_list * args)
+{
+ stream_session_t *ss = va_arg (*args, stream_session_t *);
+ int verbose = va_arg (*args, int);
+ transport_proto_vft_t *tp_vft;
+ u8 *str = 0;
+ tp_vft = session_get_transport_vft (ss->session_type);
+
+ if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
+ str = format (0, "%-10u%-10u%-10lld",
+ svm_fifo_max_dequeue (ss->server_rx_fifo),
+ svm_fifo_max_enqueue (ss->server_tx_fifo),
+ stream_session_get_index (ss));
+
+ if (ss->session_state == SESSION_STATE_READY
+ || ss->session_state == SESSION_STATE_ACCEPTING
+ || ss->session_state == SESSION_STATE_CLOSED)
+ {
+ s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
+ ss->thread_index, verbose);
+ if (verbose == 1)
+ s = format (s, "%v", str);
+ if (verbose > 1)
+ s = format (s, "%U", format_stream_session_fifos, ss, verbose);
+ }
+ else if (ss->session_state == SESSION_STATE_LISTENING)
+ {
+ s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
+ str);
+ }
+ else if (ss->session_state == SESSION_STATE_CONNECTING)
+ {
+ s = format (s, "%-40U%v", tp_vft->format_half_open,
+ ss->connection_index, str);
+ }
+ else
+ {
+ clib_warning ("Session in state: %d!", ss->session_state);
+ }
+ vec_free (str);
+
+ return s;
+}
+
+uword
+unformat_stream_session_id (unformat_input_t * input, va_list * args)
+{
+ u8 *proto = va_arg (*args, u8 *);
+ ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
+ ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
+ u16 *lcl_port = va_arg (*args, u16 *);
+ u16 *rmt_port = va_arg (*args, u16 *);
+ u8 *is_ip4 = va_arg (*args, u8 *);
+ u8 tuple_is_set = 0;
+
+ memset (lcl, 0, sizeof (*lcl));
+ memset (rmt, 0, sizeof (*rmt));
+
+ if (unformat (input, "tcp"))
+ {
+ *proto = TRANSPORT_PROTO_TCP;
+ }
+ if (unformat (input, "udp"))
+ {
+ *proto = TRANSPORT_PROTO_UDP;
+ }
+ if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
+ lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
+ {
+ *is_ip4 = 1;
+ tuple_is_set = 1;
+ }
+ else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
+ lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
+ {
+ *is_ip4 = 0;
+ tuple_is_set = 1;
+ }
+
+ return tuple_is_set;
+}
+
+uword
+unformat_stream_session (unformat_input_t * input, va_list * args)
+{
+ stream_session_t **result = va_arg (*args, stream_session_t **);
+ stream_session_t *s;
+ u8 proto = ~0;
+ ip46_address_t lcl, rmt;
+ u32 lcl_port = 0, rmt_port = 0;
+ u8 is_ip4 = 0, s_type = ~0;
+
+ if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
+ &lcl_port, &rmt_port, &is_ip4))
+ return 0;
+
+ s_type = session_type_from_proto_and_ip (proto, is_ip4);
+ if (is_ip4)
+ s = stream_session_lookup4 (&lcl.ip4, &rmt.ip4,
+ clib_host_to_net_u16 (lcl_port),
+ clib_host_to_net_u16 (rmt_port), s_type);
+ else
+ s = stream_session_lookup6 (&lcl.ip6, &rmt.ip6,
+ clib_host_to_net_u16 (lcl_port),
+ clib_host_to_net_u16 (rmt_port), s_type);
+ if (s)
+ {
+ *result = s;
+ return 1;
+ }
+ return 0;
+}
+
+uword
+unformat_transport_connection (unformat_input_t * input, va_list * args)
+{
+ transport_connection_t **result = va_arg (*args, transport_connection_t **);
+ u32 suggested_proto = va_arg (*args, u32);
+ transport_connection_t *tc;
+ u8 proto = ~0;
+ ip46_address_t lcl, rmt;
+ u32 lcl_port = 0, rmt_port = 0;
+ u8 is_ip4 = 0, s_type = ~0;
+
+ if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
+ &lcl_port, &rmt_port, &is_ip4))
+ return 0;
+
+ proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
+ if (proto == (u8) ~ 0)
+ return 0;
+ s_type = session_type_from_proto_and_ip (proto, is_ip4);
+ if (is_ip4)
+ tc = stream_session_lookup_transport4 (&lcl.ip4, &rmt.ip4,
+ clib_host_to_net_u16 (lcl_port),
+ clib_host_to_net_u16 (rmt_port),
+ s_type);
+ else
+ tc = stream_session_lookup_transport6 (&lcl.ip6, &rmt.ip6,
+ clib_host_to_net_u16 (lcl_port),
+ clib_host_to_net_u16 (rmt_port),
+ s_type);
+
+ if (tc)
+ {
+ *result = tc;
+ return 1;
+ }
+ return 0;
+}
+
+static clib_error_t *
+show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ int verbose = 0, i;
+ stream_session_t *pool;
+ stream_session_t *s;
+ u8 *str = 0, one_session = 0;
+
+ if (!smm->is_enabled)
+ {
+ return clib_error_return (0, "session layer is not enabled");
+ }
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "verbose %d", &verbose))
+ ;
+ else if (unformat (input, "verbose"))
+ verbose = 1;
+ else if (unformat (input, "%U", unformat_stream_session, &s))
+ {
+ one_session = 1;
+ }
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+
+ if (one_session)
+ {
+ vlib_cli_output (vm, "%U", format_stream_session, s, 3);
+ return 0;
+ }
+
+ for (i = 0; i < vec_len (smm->sessions); i++)
+ {
+ u32 once_per_pool;
+ pool = smm->sessions[i];
+
+ once_per_pool = 1;
+
+ if (pool_elts (pool))
+ {
+
+ vlib_cli_output (vm, "Thread %d: %d active sessions",
+ i, pool_elts (pool));
+ if (verbose)
+ {
+ if (once_per_pool && verbose == 1)
+ {
+ str = format (str, "%-50s%-15s%-10s%-10s%-10s",
+ "Connection", "State", "Rx-f", "Tx-f",
+ "S-idx");
+ vlib_cli_output (vm, "%v", str);
+ vec_reset_length (str);
+ once_per_pool = 0;
+ }
+
+ /* *INDENT-OFF* */
+ pool_foreach (s, pool,
+ ({
+ vec_reset_length (str);
+ str = format (str, "%U", format_stream_session, s, verbose);
+ vlib_cli_output (vm, "%v", str);
+ }));
+ /* *INDENT-ON* */
+ }
+ }
+ else
+ vlib_cli_output (vm, "Thread %d: no active sessions", i);
+ vec_reset_length (str);
+ }
+ vec_free (str);
+
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
+{
+ .path = "show session",
+ .short_help = "show session [verbose [nnn]]",
+ .function = show_session_command_fn,
+};
+/* *INDENT-ON* */
+
+static int
+clear_session (stream_session_t * s)
+{
+ application_t *server = application_get (s->app_index);
+ server->cb_fns.session_disconnect_callback (s);
+ return 0;
+}
+
+static clib_error_t *
+clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ u32 thread_index = 0, clear_all = 0;
+ u32 session_index = ~0;
+ stream_session_t **pool, *session;
+
+ if (!smm->is_enabled)
+ {
+ return clib_error_return (0, "session layer is not enabled");
+ }
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "thread %d", &thread_index))
+ ;
+ else if (unformat (input, "session %d", &session_index))
+ ;
+ else if (unformat (input, "all"))
+ clear_all = 1;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+
+ if (!clear_all && session_index == ~0)
+ return clib_error_return (0, "session <nn> required, but not set.");
+
+ if (session_index != ~0)
+ {
+ session = stream_session_get_if_valid (session_index, thread_index);
+ if (!session)
+ return clib_error_return (0, "no session %d on thread %d",
+ session_index, thread_index);
+ clear_session (session);
+ }
+
+ if (clear_all)
+ {
+ /* *INDENT-OFF* */
+ vec_foreach (pool, smm->sessions)
+ {
+ pool_foreach(session, *pool, ({
+ clear_session (session);
+ }));
+ };
+ /* *INDENT-ON* */
+ }
+
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (clear_session_command, static) =
+{
+ .path = "clear session",
+ .short_help = "clear session thread <thread> session <index>",
+ .function = clear_session_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+show_session_fifo_trace_command_fn (vlib_main_t * vm,
+ unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ stream_session_t *s = 0;
+ u8 is_rx = 0, *str = 0;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "%U", unformat_stream_session, &s))
+ ;
+ else if (unformat (input, "rx"))
+ is_rx = 1;
+ else if (unformat (input, "tx"))
+ is_rx = 0;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+
+ if (!SVM_FIFO_TRACE)
+ {
+ vlib_cli_output (vm, "fifo tracing not enabled");
+ return 0;
+ }
+
+ if (!s)
+ {
+ vlib_cli_output (vm, "could not find session");
+ return 0;
+ }
+
+ str = is_rx ?
+ svm_fifo_dump_trace (str, s->server_rx_fifo) :
+ svm_fifo_dump_trace (str, s->server_tx_fifo);
+
+ vlib_cli_output (vm, "%v", str);
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
+{
+ .path = "show session fifo trace",
+ .short_help = "show session fifo trace <session>",
+ .function = show_session_fifo_trace_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ stream_session_t *s = 0;
+ u8 is_rx = 0, *str = 0;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "%U", unformat_stream_session, &s))
+ ;
+ else if (unformat (input, "rx"))
+ is_rx = 1;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+
+ if (!SVM_FIFO_TRACE)
+ {
+ vlib_cli_output (vm, "fifo tracing not enabled");
+ return 0;
+ }
+
+ if (!s)
+ {
+ vlib_cli_output (vm, "could not find session");
+ return 0;
+ }
+
+ str = is_rx ?
+ svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
+ svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
+
+ vlib_cli_output (vm, "%v", str);
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
+{
+ .path = "session replay fifo",
+ .short_help = "session replay fifo <session>",
+ .function = session_replay_fifo_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ u8 is_en = 1;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "enable"))
+ is_en = 1;
+ else if (unformat (input, "disable"))
+ is_en = 0;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+
+ return vnet_session_enable_disable (vm, is_en);
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (session_enable_disable_command, static) =
+{
+ .path = "session",
+ .short_help = "session [enable|disable]",
+ .function = session_enable_disable_fn,
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session_debug.h b/src/vnet/session/session_debug.h
new file mode 100644
index 00000000..eb11f1a0
--- /dev/null
+++ b/src/vnet/session/session_debug.h
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef SRC_VNET_SESSION_SESSION_DEBUG_H_
+#define SRC_VNET_SESSION_SESSION_DEBUG_H_
+
+#include <vnet/session/transport.h>
+#include <vlib/vlib.h>
+
+#define foreach_session_dbg_evt \
+ _(ENQ, "enqueue") \
+ _(DEQ, "dequeue") \
+ _(DEQ_NODE, "dequeue") \
+ _(POLL_GAP_TRACK, "poll gap track") \
+
+typedef enum _session_evt_dbg
+{
+#define _(sym, str) SESSION_EVT_##sym,
+ foreach_session_dbg_evt
+#undef _
+} session_evt_dbg_e;
+
+#define SESSION_DBG (0)
+#define SESSION_DEQ_NODE_EVTS (0)
+#define SESSION_EVT_POLL_DBG (1)
+
+#if TRANSPORT_DEBUG && SESSION_DBG
+
+#define DEC_SESSION_ETD(_s, _e, _size) \
+ struct \
+ { \
+ u32 data[_size]; \
+ } * ed; \
+ transport_proto_vft_t *vft = \
+ session_get_transport_vft (_s->session_type); \
+ transport_connection_t *_tc = \
+ vft->get_connection (_s->connection_index, _s->thread_index); \
+ ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, \
+ _e, _tc->elog_track)
+
+#define DEC_SESSION_ED(_e, _size) \
+ struct \
+ { \
+ u32 data[_size]; \
+ } * ed; \
+ ed = ELOG_DATA (&vlib_global_main.elog_main, _e)
+
+#define SESSION_EVT_DEQ_HANDLER(_s, _body) \
+{ \
+ ELOG_TYPE_DECLARE (_e) = \
+ { \
+ .format = "deq: id %d len %d rd %d wnd %d", \
+ .format_args = "i4i4i4i4", \
+ }; \
+ DEC_SESSION_ETD(_s, _e, 4); \
+ do { _body; } while (0); \
+}
+
+#define SESSION_EVT_ENQ_HANDLER(_s, _body) \
+{ \
+ ELOG_TYPE_DECLARE (_e) = \
+ { \
+ .format = "enq: id %d length %d", \
+ .format_args = "i4i4", \
+ }; \
+ DEC_SESSION_ETD(_s, _e, 2); \
+ do { _body; } while (0); \
+}
+
+#if SESSION_DEQ_NODE_EVTS
+#define SESSION_EVT_DEQ_NODE_HANDLER(_node_evt) \
+{ \
+ ELOG_TYPE_DECLARE (_e) = \
+ { \
+ .format = "deq-node: %s", \
+ .format_args = "t4", \
+ .n_enum_strings = 2, \
+ .enum_strings = { \
+ "start", \
+ "end", \
+ }, \
+ }; \
+ DEC_SESSION_ED(_e, 1); \
+ ed->data[0] = _node_evt; \
+}
+#else
+#define SESSION_EVT_DEQ_NODE_HANDLER(_node_evt)
+#endif
+
+#if SESSION_DBG && SESSION_EVT_POLL_DBG
+#define SESSION_EVT_POLL_GAP(_smm, _my_thread_index) \
+{ \
+ ELOG_TYPE_DECLARE (_e) = \
+ { \
+ .format = "nixon-gap: %d MS", \
+ .format_args = "i4", \
+ }; \
+ DEC_SESSION_ED(_e, 1); \
+ ed->data[0] = (u32) ((now - \
+ _smm->last_event_poll_by_thread[my_thread_index])*1000.0); \
+}
+#define SESSION_EVT_POLL_GAP_TRACK_HANDLER(_smm, _my_thread_index) \
+{ \
+ if (PREDICT_TRUE( \
+ smm->last_event_poll_by_thread[my_thread_index] != 0.0)) \
+ if (now > smm->last_event_poll_by_thread[_my_thread_index] + 500e-6)\
+ SESSION_EVT_POLL_GAP(smm, my_thread_index); \
+ _smm->last_event_poll_by_thread[my_thread_index] = now; \
+}
+
+#else
+#define SESSION_EVT_POLL_GAP(_smm, _my_thread_index)
+#define SESSION_EVT_POLL_GAP_TRACK_HANDLER(_smm, _my_thread_index)
+#endif
+
+#define CONCAT_HELPER(_a, _b) _a##_b
+#define CC(_a, _b) CONCAT_HELPER(_a, _b)
+#define SESSION_EVT_DBG(_evt, _args...) CC(_evt, _HANDLER)(_args)
+
+#else
+#define SESSION_EVT_DBG(_evt, _args...)
+#endif
+
+#endif /* SRC_VNET_SESSION_SESSION_DEBUG_H_ */
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session_lookup.c b/src/vnet/session/session_lookup.c
new file mode 100644
index 00000000..4487b1c3
--- /dev/null
+++ b/src/vnet/session/session_lookup.c
@@ -0,0 +1,619 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/** Generate typed init functions for multiple hash table styles... */
+#include <vppinfra/bihash_16_8.h>
+#include <vppinfra/bihash_template.h>
+
+#include <vppinfra/bihash_template.c>
+
+#undef __included_bihash_template_h__
+
+#include <vppinfra/bihash_48_8.h>
+#include <vppinfra/bihash_template.h>
+
+#include <vppinfra/bihash_template.c>
+#include <vnet/session/session_lookup.h>
+#include <vnet/session/session.h>
+
+static session_lookup_t session_lookup;
+extern transport_proto_vft_t *tp_vfts;
+
+/* *INDENT-OFF* */
+/* 16 octets */
+typedef CLIB_PACKED (struct {
+ union
+ {
+ struct
+ {
+ ip4_address_t src;
+ ip4_address_t dst;
+ u16 src_port;
+ u16 dst_port;
+ /* align by making this 4 octets even though its a 1-bit field
+ * NOTE: avoid key overlap with other transports that use 5 tuples for
+ * session identification.
+ */
+ u32 proto;
+ };
+ u64 as_u64[2];
+ };
+}) v4_connection_key_t;
+
+typedef CLIB_PACKED (struct {
+ union
+ {
+ struct
+ {
+ /* 48 octets */
+ ip6_address_t src;
+ ip6_address_t dst;
+ u16 src_port;
+ u16 dst_port;
+ u32 proto;
+ u64 unused;
+ };
+ u64 as_u64[6];
+ };
+}) v6_connection_key_t;
+/* *INDENT-ON* */
+
+typedef clib_bihash_kv_16_8_t session_kv4_t;
+typedef clib_bihash_kv_48_8_t session_kv6_t;
+
+always_inline void
+make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
+
+ key->src.as_u32 = lcl->as_u32;
+ key->dst.as_u32 = rmt->as_u32;
+ key->src_port = lcl_port;
+ key->dst_port = rmt_port;
+ key->proto = proto;
+
+ kv->value = ~0ULL;
+}
+
+always_inline void
+make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
+ u8 proto)
+{
+ v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
+
+ key->src.as_u32 = lcl->as_u32;
+ key->dst.as_u32 = 0;
+ key->src_port = lcl_port;
+ key->dst_port = 0;
+ key->proto = proto;
+
+ kv->value = ~0ULL;
+}
+
+always_inline void
+make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
+{
+ make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port, t->rmt_port,
+ session_type_from_proto_and_ip (t->transport_proto, 1));
+}
+
+always_inline void
+make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
+
+ key->src.as_u64[0] = lcl->as_u64[0];
+ key->src.as_u64[1] = lcl->as_u64[1];
+ key->dst.as_u64[0] = rmt->as_u64[0];
+ key->dst.as_u64[1] = rmt->as_u64[1];
+ key->src_port = lcl_port;
+ key->dst_port = rmt_port;
+ key->proto = proto;
+ key->unused = 0;
+
+ kv->value = ~0ULL;
+}
+
+always_inline void
+make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
+ u8 proto)
+{
+ v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
+
+ key->src.as_u64[0] = lcl->as_u64[0];
+ key->src.as_u64[1] = lcl->as_u64[1];
+ key->dst.as_u64[0] = 0;
+ key->dst.as_u64[1] = 0;
+ key->src_port = lcl_port;
+ key->dst_port = 0;
+ key->proto = proto;
+ key->unused = 0;
+
+ kv->value = ~0ULL;
+}
+
+always_inline void
+make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t)
+{
+ make_v6_ss_kv (kv, &t->lcl_ip.ip6, &t->rmt_ip.ip6, t->lcl_port, t->rmt_port,
+ session_type_from_proto_and_ip (t->transport_proto, 0));
+}
+
+/*
+ * Session lookup key; (src-ip, dst-ip, src-port, dst-port, session-type)
+ * Value: (owner thread index << 32 | session_index);
+ */
+void
+stream_session_table_add_for_tc (transport_connection_t * tc, u64 value)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+
+ if (tc->is_ip4)
+ {
+ make_v4_ss_kv_from_tc (&kv4, tc);
+ kv4.value = value;
+ clib_bihash_add_del_16_8 (&sl->v4_session_hash, &kv4, 1 /* is_add */ );
+ }
+ else
+ {
+ make_v6_ss_kv_from_tc (&kv6, tc);
+ kv6.value = value;
+ clib_bihash_add_del_48_8 (&sl->v6_session_hash, &kv6, 1 /* is_add */ );
+ }
+}
+
+void
+stream_session_table_add (session_manager_main_t * smm, stream_session_t * s,
+ u64 value)
+{
+ transport_connection_t *tc;
+
+ tc = tp_vfts[s->session_type].get_connection (s->connection_index,
+ s->thread_index);
+ stream_session_table_add_for_tc (tc, value);
+}
+
+int
+stream_session_table_del_for_tc (transport_connection_t * tc)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+
+ if (tc->is_ip4)
+ {
+ make_v4_ss_kv_from_tc (&kv4, tc);
+ return clib_bihash_add_del_16_8 (&sl->v4_session_hash, &kv4,
+ 0 /* is_add */ );
+ }
+ else
+ {
+ make_v6_ss_kv_from_tc (&kv6, tc);
+ return clib_bihash_add_del_48_8 (&sl->v6_session_hash, &kv6,
+ 0 /* is_add */ );
+ }
+
+ return 0;
+}
+
+int
+stream_session_table_del (stream_session_t * s)
+{
+ transport_connection_t *ts;
+ ts = tp_vfts[s->session_type].get_connection (s->connection_index,
+ s->thread_index);
+ return stream_session_table_del_for_tc (ts);
+}
+
+
+void
+stream_session_half_open_table_add (transport_connection_t * tc, u64 value)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+
+ if (tc->is_ip4)
+ {
+ make_v4_ss_kv_from_tc (&kv4, tc);
+ kv4.value = value;
+ (void) clib_bihash_add_del_16_8 (&sl->v4_half_open_hash, &kv4,
+ 1 /* is_add */ );
+ }
+ else
+ {
+ make_v6_ss_kv_from_tc (&kv6, tc);
+ kv6.value = value;
+ (void) clib_bihash_add_del_48_8 (&sl->v6_half_open_hash, &kv6,
+ 1 /* is_add */ );
+ }
+}
+
+void
+stream_session_half_open_table_del (transport_connection_t * tc)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+
+ if (tc->is_ip4)
+ {
+ make_v4_ss_kv_from_tc (&kv4, tc);
+ clib_bihash_add_del_16_8 (&sl->v4_half_open_hash, &kv4,
+ 0 /* is_add */ );
+ }
+ else
+ {
+ make_v6_ss_kv_from_tc (&kv6, tc);
+ clib_bihash_add_del_48_8 (&sl->v6_half_open_hash, &kv6,
+ 0 /* is_add */ );
+ }
+}
+
+stream_session_t *
+stream_session_lookup_listener4 (ip4_address_t * lcl, u16 lcl_port, u8 proto)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ int rv;
+
+ make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_session_hash, &kv4);
+ if (rv == 0)
+ return session_manager_get_listener (proto, (u32) kv4.value);
+
+ /* Zero out the lcl ip */
+ kv4.key[0] = 0;
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_session_hash, &kv4);
+ if (rv == 0)
+ return session_manager_get_listener (proto, (u32) kv4.value);
+
+ return 0;
+}
+
+/** Looks up a session based on the 5-tuple passed as argument.
+ *
+ * First it tries to find an established session, if this fails, it tries
+ * finding a listener session if this fails, it tries a lookup with a
+ * wildcarded local source (listener bound to all interfaces)
+ */
+stream_session_t *
+stream_session_lookup4 (ip4_address_t * lcl, ip4_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ stream_session_t *s;
+ int rv;
+
+ /* Lookup session amongst established ones */
+ make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_session_hash, &kv4);
+ if (rv == 0)
+ return stream_session_get_from_handle (kv4.value);
+
+ /* If nothing is found, check if any listener is available */
+ if ((s = stream_session_lookup_listener4 (lcl, lcl_port, proto)))
+ return s;
+
+ /* Finally, try half-open connections */
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_half_open_hash, &kv4);
+ if (rv == 0)
+ return stream_session_get_from_handle (kv4.value);
+ return 0;
+}
+
+stream_session_t *
+stream_session_lookup_listener6 (ip6_address_t * lcl, u16 lcl_port, u8 proto)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv6_t kv6;
+ int rv;
+
+ make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_session_hash, &kv6);
+ if (rv == 0)
+ return session_manager_get_listener (proto, (u32) kv6.value);
+
+ /* Zero out the lcl ip */
+ kv6.key[0] = kv6.key[1] = 0;
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_session_hash, &kv6);
+ if (rv == 0)
+ return session_manager_get_listener (proto, (u32) kv6.value);
+
+ return 0;
+}
+
+/* Looks up a session based on the 5-tuple passed as argument.
+ * First it tries to find an established session, if this fails, it tries
+ * finding a listener session if this fails, it tries a lookup with a
+ * wildcarded local source (listener bound to all interfaces) */
+stream_session_t *
+stream_session_lookup6 (ip6_address_t * lcl, ip6_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv6_t kv6;
+ stream_session_t *s;
+ int rv;
+
+ make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_session_hash, &kv6);
+ if (rv == 0)
+ return stream_session_get_from_handle (kv6.value);
+
+ /* If nothing is found, check if any listener is available */
+ if ((s = stream_session_lookup_listener6 (lcl, lcl_port, proto)))
+ return s;
+
+ /* Finally, try half-open connections */
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_half_open_hash, &kv6);
+ if (rv == 0)
+ return stream_session_get_from_handle (kv6.value);
+ return 0;
+}
+
+stream_session_t *
+stream_session_lookup_listener (ip46_address_t * lcl, u16 lcl_port, u8 proto)
+{
+ switch (proto)
+ {
+ case SESSION_TYPE_IP4_UDP:
+ case SESSION_TYPE_IP4_TCP:
+ return stream_session_lookup_listener4 (&lcl->ip4, lcl_port, proto);
+ break;
+ case SESSION_TYPE_IP6_UDP:
+ case SESSION_TYPE_IP6_TCP:
+ return stream_session_lookup_listener6 (&lcl->ip6, lcl_port, proto);
+ break;
+ }
+ return 0;
+}
+
+u64
+stream_session_half_open_lookup_handle (ip46_address_t * lcl,
+ ip46_address_t * rmt, u16 lcl_port,
+ u16 rmt_port, u8 proto)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+ int rv;
+
+ switch (proto)
+ {
+ case SESSION_TYPE_IP4_UDP:
+ case SESSION_TYPE_IP4_TCP:
+ make_v4_ss_kv (&kv4, &lcl->ip4, &rmt->ip4, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_half_open_hash, &kv4);
+
+ if (rv == 0)
+ return kv4.value;
+
+ return HALF_OPEN_LOOKUP_INVALID_VALUE;
+ break;
+ case SESSION_TYPE_IP6_UDP:
+ case SESSION_TYPE_IP6_TCP:
+ make_v6_ss_kv (&kv6, &lcl->ip6, &rmt->ip6, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_half_open_hash, &kv6);
+
+ if (rv == 0)
+ return kv6.value;
+
+ return HALF_OPEN_LOOKUP_INVALID_VALUE;
+ break;
+ }
+ return HALF_OPEN_LOOKUP_INVALID_VALUE;
+}
+
+transport_connection_t *
+stream_session_half_open_lookup (ip46_address_t * lcl, ip46_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ u64 handle;
+ handle =
+ stream_session_half_open_lookup_handle (lcl, rmt, lcl_port, rmt_port,
+ proto);
+ if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
+ return tp_vfts[proto].get_half_open (handle & 0xFFFFFFFF);
+ return 0;
+}
+
+always_inline stream_session_t *
+stream_session_get_tsi (u64 ti_and_si, u32 thread_index)
+{
+ ASSERT ((u32) (ti_and_si >> 32) == thread_index);
+ return pool_elt_at_index (session_manager_main.sessions[thread_index],
+ ti_and_si & 0xFFFFFFFFULL);
+}
+
+transport_connection_t *
+stream_session_lookup_transport_wt4 (ip4_address_t * lcl, ip4_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto,
+ u32 my_thread_index)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ stream_session_t *s;
+ int rv;
+
+ /* Lookup session amongst established ones */
+ make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_session_hash, &kv4);
+ if (rv == 0)
+ {
+ s = stream_session_get_tsi (kv4.value, my_thread_index);
+ return tp_vfts[s->session_type].get_connection (s->connection_index,
+ my_thread_index);
+ }
+
+ /* If nothing is found, check if any listener is available */
+ s = stream_session_lookup_listener4 (lcl, lcl_port, proto);
+ if (s)
+ return tp_vfts[s->session_type].get_listener (s->connection_index);
+
+ /* Finally, try half-open connections */
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_half_open_hash, &kv4);
+ if (rv == 0)
+ return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
+ return 0;
+}
+
+transport_connection_t *
+stream_session_lookup_transport4 (ip4_address_t * lcl, ip4_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ session_lookup_t *sl = &session_lookup;
+ session_kv4_t kv4;
+ stream_session_t *s;
+ int rv;
+
+ /* Lookup session amongst established ones */
+ make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_session_hash, &kv4);
+ if (rv == 0)
+ {
+ s = stream_session_get_from_handle (kv4.value);
+ return tp_vfts[s->session_type].get_connection (s->connection_index,
+ s->thread_index);
+ }
+
+ /* If nothing is found, check if any listener is available */
+ s = stream_session_lookup_listener4 (lcl, lcl_port, proto);
+ if (s)
+ return tp_vfts[s->session_type].get_listener (s->connection_index);
+
+ /* Finally, try half-open connections */
+ rv = clib_bihash_search_inline_16_8 (&sl->v4_half_open_hash, &kv4);
+ if (rv == 0)
+ return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
+ return 0;
+}
+
+transport_connection_t *
+stream_session_lookup_transport_wt6 (ip6_address_t * lcl, ip6_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto,
+ u32 my_thread_index)
+{
+ session_lookup_t *sl = &session_lookup;
+ stream_session_t *s;
+ session_kv6_t kv6;
+ int rv;
+
+ make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_session_hash, &kv6);
+ if (rv == 0)
+ {
+ s = stream_session_get_tsi (kv6.value, my_thread_index);
+ return tp_vfts[s->session_type].get_connection (s->connection_index,
+ my_thread_index);
+ }
+
+ /* If nothing is found, check if any listener is available */
+ s = stream_session_lookup_listener6 (lcl, lcl_port, proto);
+ if (s)
+ return tp_vfts[s->session_type].get_listener (s->connection_index);
+
+ /* Finally, try half-open connections */
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_half_open_hash, &kv6);
+ if (rv == 0)
+ return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
+
+ return 0;
+}
+
+transport_connection_t *
+stream_session_lookup_transport6 (ip6_address_t * lcl, ip6_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ session_lookup_t *sl = &session_lookup;
+ stream_session_t *s;
+ session_kv6_t kv6;
+ int rv;
+
+ make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_session_hash, &kv6);
+ if (rv == 0)
+ {
+ s = stream_session_get_from_handle (kv6.value);
+ return tp_vfts[s->session_type].get_connection (s->connection_index,
+ s->thread_index);
+ }
+
+ /* If nothing is found, check if any listener is available */
+ s = stream_session_lookup_listener6 (lcl, lcl_port, proto);
+ if (s)
+ return tp_vfts[s->session_type].get_listener (s->connection_index);
+
+ /* Finally, try half-open connections */
+ rv = clib_bihash_search_inline_48_8 (&sl->v6_half_open_hash, &kv6);
+ if (rv == 0)
+ return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
+
+ return 0;
+}
+
+#define foreach_hash_table_parameter \
+ _(v4,session,buckets,20000) \
+ _(v4,session,memory,(64<<20)) \
+ _(v6,session,buckets,20000) \
+ _(v6,session,memory,(64<<20)) \
+ _(v4,halfopen,buckets,20000) \
+ _(v4,halfopen,memory,(64<<20)) \
+ _(v6,halfopen,buckets,20000) \
+ _(v6,halfopen,memory,(64<<20))
+
+void
+session_lookup_init (void)
+{
+ session_lookup_t *sl = &session_lookup;
+
+#define _(af,table,parm,value) \
+ u32 configured_##af##_##table##_table_##parm = value;
+ foreach_hash_table_parameter;
+#undef _
+
+#define _(af,table,parm,value) \
+ if (session_manager_main.configured_##af##_##table##_table_##parm) \
+ configured_##af##_##table##_table_##parm = \
+ session_manager_main.configured_##af##_##table##_table_##parm;
+ foreach_hash_table_parameter;
+#undef _
+
+ clib_bihash_init_16_8 (&sl->v4_session_hash, "v4 session table",
+ configured_v4_session_table_buckets,
+ configured_v4_session_table_memory);
+ clib_bihash_init_48_8 (&sl->v6_session_hash, "v6 session table",
+ configured_v6_session_table_buckets,
+ configured_v6_session_table_memory);
+ clib_bihash_init_16_8 (&sl->v4_half_open_hash, "v4 half-open table",
+ configured_v4_halfopen_table_buckets,
+ configured_v4_halfopen_table_memory);
+ clib_bihash_init_48_8 (&sl->v6_half_open_hash, "v6 half-open table",
+ configured_v6_halfopen_table_buckets,
+ configured_v6_halfopen_table_memory);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session_lookup.h b/src/vnet/session/session_lookup.h
new file mode 100644
index 00000000..cf1dc013
--- /dev/null
+++ b/src/vnet/session/session_lookup.h
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_VNET_SESSION_SESSION_LOOKUP_H_
+#define SRC_VNET_SESSION_SESSION_LOOKUP_H_
+
+#include <vnet/session/stream_session.h>
+#include <vnet/session/transport.h>
+
+typedef struct _session_lookup
+{
+ /** Lookup tables for established sessions and listeners */
+ clib_bihash_16_8_t v4_session_hash;
+ clib_bihash_48_8_t v6_session_hash;
+
+ /** Lookup tables for half-open sessions */
+ clib_bihash_16_8_t v4_half_open_hash;
+ clib_bihash_48_8_t v6_half_open_hash;
+} session_lookup_t;
+
+stream_session_t *stream_session_lookup_listener4 (ip4_address_t * lcl,
+ u16 lcl_port, u8 proto);
+stream_session_t *stream_session_lookup4 (ip4_address_t * lcl,
+ ip4_address_t * rmt, u16 lcl_port,
+ u16 rmt_port, u8 proto);
+stream_session_t *stream_session_lookup_listener6 (ip6_address_t * lcl,
+ u16 lcl_port, u8 proto);
+stream_session_t *stream_session_lookup6 (ip6_address_t * lcl,
+ ip6_address_t * rmt, u16 lcl_port,
+ u16 rmt_port, u8 proto);
+transport_connection_t *stream_session_lookup_transport_wt4 (ip4_address_t *
+ lcl,
+ ip4_address_t *
+ rmt,
+ u16 lcl_port,
+ u16 rmt_port,
+ u8 proto,
+ u32
+ thread_index);
+transport_connection_t *stream_session_lookup_transport4 (ip4_address_t * lcl,
+ ip4_address_t * rmt,
+ u16 lcl_port,
+ u16 rmt_port,
+ u8 proto);
+transport_connection_t *stream_session_lookup_transport_wt6 (ip6_address_t *
+ lcl,
+ ip6_address_t *
+ rmt,
+ u16 lcl_port,
+ u16 rmt_port,
+ u8 proto,
+ u32
+ thread_index);
+transport_connection_t *stream_session_lookup_transport6 (ip6_address_t * lcl,
+ ip6_address_t * rmt,
+ u16 lcl_port,
+ u16 rmt_port,
+ u8 proto);
+
+stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
+ u16 lcl_port, u8 proto);
+u64 stream_session_half_open_lookup_handle (ip46_address_t * lcl,
+ ip46_address_t * rmt,
+ u16 lcl_port,
+ u16 rmt_port, u8 proto);
+transport_connection_t *stream_session_half_open_lookup (ip46_address_t * lcl,
+ ip46_address_t * rmt,
+ u16 lcl_port,
+ u16 rmt_port,
+ u8 proto);
+void stream_session_table_add_for_tc (transport_connection_t * tc, u64 value);
+int stream_session_table_del_for_tc (transport_connection_t * tc);
+int stream_session_table_del (stream_session_t * s);
+void stream_session_half_open_table_del (transport_connection_t * tc);
+void stream_session_half_open_table_add (transport_connection_t * tc,
+ u64 value);
+
+void session_lookup_init (void);
+
+#endif /* SRC_VNET_SESSION_SESSION_LOOKUP_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/session_node.c b/src/vnet/session/session_node.c
new file mode 100644
index 00000000..d0155849
--- /dev/null
+++ b/src/vnet/session/session_node.c
@@ -0,0 +1,707 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <math.h>
+#include <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include <vnet/tcp/tcp.h>
+#include <vppinfra/elog.h>
+#include <vnet/session/application.h>
+#include <vnet/session/session_debug.h>
+#include <vlibmemory/unix_shared_memory_queue.h>
+
+vlib_node_registration_t session_queue_node;
+
+typedef struct
+{
+ u32 session_index;
+ u32 server_thread_index;
+} session_queue_trace_t;
+
+/* packet trace format function */
+static u8 *
+format_session_queue_trace (u8 * s, va_list * args)
+{
+ CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+ CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+ session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
+
+ s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
+ t->session_index, t->server_thread_index);
+ return s;
+}
+
+vlib_node_registration_t session_queue_node;
+
+#define foreach_session_queue_error \
+_(TX, "Packets transmitted") \
+_(TIMER, "Timer events") \
+_(NO_BUFFER, "Out of buffers")
+
+typedef enum
+{
+#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
+ foreach_session_queue_error
+#undef _
+ SESSION_QUEUE_N_ERROR,
+} session_queue_error_t;
+
+static char *session_queue_error_strings[] = {
+#define _(sym,string) string,
+ foreach_session_queue_error
+#undef _
+};
+
+static u32 session_type_to_next[] = {
+ SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
+ SESSION_QUEUE_NEXT_IP4_LOOKUP,
+ SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
+ SESSION_QUEUE_NEXT_IP6_LOOKUP,
+};
+
+always_inline void
+session_tx_fifo_chain_tail (session_manager_main_t * smm, vlib_main_t * vm,
+ u8 thread_index, svm_fifo_t * fifo,
+ vlib_buffer_t * b0, u32 bi0, u8 n_bufs_per_seg,
+ u32 left_from_seg, u32 * left_to_snd0,
+ u16 * n_bufs, u32 * tx_offset, u16 deq_per_buf,
+ u8 peek_data)
+{
+ vlib_buffer_t *chain_b0, *prev_b0;
+ u32 chain_bi0, to_deq;
+ u16 len_to_deq0, n_bytes_read;
+ u8 *data0, j;
+
+ b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
+ b0->total_length_not_including_first_buffer = 0;
+
+ chain_bi0 = bi0;
+ chain_b0 = b0;
+ to_deq = left_from_seg;
+ for (j = 1; j < n_bufs_per_seg; j++)
+ {
+ prev_b0 = chain_b0;
+ len_to_deq0 = clib_min (to_deq, deq_per_buf);
+
+ *n_bufs -= 1;
+ chain_bi0 = smm->tx_buffers[thread_index][*n_bufs];
+ _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
+
+ chain_b0 = vlib_get_buffer (vm, chain_bi0);
+ chain_b0->current_data = 0;
+ data0 = vlib_buffer_get_current (chain_b0);
+ if (peek_data)
+ {
+ n_bytes_read = svm_fifo_peek (fifo, *tx_offset, len_to_deq0, data0);
+ *tx_offset += n_bytes_read;
+ }
+ else
+ {
+ n_bytes_read = svm_fifo_dequeue_nowait (fifo, len_to_deq0, data0);
+ }
+ ASSERT (n_bytes_read == len_to_deq0);
+ chain_b0->current_length = n_bytes_read;
+ b0->total_length_not_including_first_buffer += chain_b0->current_length;
+
+ /* update previous buffer */
+ prev_b0->next_buffer = chain_bi0;
+ prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
+
+ /* update current buffer */
+ chain_b0->next_buffer = 0;
+
+ to_deq -= n_bytes_read;
+ if (to_deq == 0)
+ break;
+ }
+ ASSERT (to_deq == 0
+ && b0->total_length_not_including_first_buffer == left_from_seg);
+ *left_to_snd0 -= left_from_seg;
+}
+
+always_inline int
+session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
+ session_manager_main_t * smm,
+ session_fifo_event_t * e0,
+ stream_session_t * s0, u32 thread_index,
+ int *n_tx_packets, u8 peek_data)
+{
+ u32 n_trace = vlib_get_trace_count (vm, node);
+ u32 left_to_snd0, max_len_to_snd0, len_to_deq0, snd_space0;
+ u32 n_bufs_per_evt, n_frames_per_evt, n_bufs_per_frame;
+ transport_connection_t *tc0;
+ transport_proto_vft_t *transport_vft;
+ u32 next_index, next0, *to_next, n_left_to_next, bi0;
+ vlib_buffer_t *b0;
+ u32 tx_offset = 0, max_dequeue0, n_bytes_per_seg, left_for_seg;
+ u16 snd_mss0, n_bufs_per_seg, n_bufs;
+ u8 *data0;
+ int i, n_bytes_read;
+ u32 n_bytes_per_buf, deq_per_buf, deq_per_first_buf;
+ u32 buffers_allocated, buffers_allocated_this_call;
+
+ next_index = next0 = session_type_to_next[s0->session_type];
+
+ transport_vft = session_get_transport_vft (s0->session_type);
+ tc0 = transport_vft->get_connection (s0->connection_index, thread_index);
+
+ /* Make sure we have space to send and there's something to dequeue */
+ snd_mss0 = transport_vft->send_mss (tc0);
+ snd_space0 = transport_vft->send_space (tc0);
+
+ /* Can't make any progress */
+ if (snd_space0 == 0 || snd_mss0 == 0)
+ {
+ vec_add1 (smm->pending_event_vector[thread_index], *e0);
+ return 0;
+ }
+
+ /* Check how much we can pull. */
+ max_dequeue0 = svm_fifo_max_dequeue (s0->server_tx_fifo);
+
+ if (peek_data)
+ {
+ /* Offset in rx fifo from where to peek data */
+ tx_offset = transport_vft->tx_fifo_offset (tc0);
+ if (PREDICT_FALSE (tx_offset >= max_dequeue0))
+ max_dequeue0 = 0;
+ else
+ max_dequeue0 -= tx_offset;
+ }
+
+ /* Nothing to read return */
+ if (max_dequeue0 == 0)
+ {
+ svm_fifo_unset_event (s0->server_tx_fifo);
+ return 0;
+ }
+
+ /* Ensure we're not writing more than transport window allows */
+ if (max_dequeue0 < snd_space0)
+ {
+ /* Constrained by tx queue. Try to send only fully formed segments */
+ max_len_to_snd0 = (max_dequeue0 > snd_mss0) ?
+ max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0;
+ /* TODO Nagle ? */
+ }
+ else
+ {
+ /* Expectation is that snd_space0 is already a multiple of snd_mss */
+ max_len_to_snd0 = snd_space0;
+ }
+
+ n_bytes_per_buf = vlib_buffer_free_list_buffer_size
+ (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+ ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
+ n_bytes_per_seg = MAX_HDRS_LEN + snd_mss0;
+ n_bufs_per_seg = ceil ((double) n_bytes_per_seg / n_bytes_per_buf);
+ n_bufs_per_evt = ceil ((double) max_len_to_snd0 / n_bytes_per_seg);
+ n_frames_per_evt = ceil ((double) n_bufs_per_evt / VLIB_FRAME_SIZE);
+ n_bufs_per_frame = n_bufs_per_seg * VLIB_FRAME_SIZE;
+
+ deq_per_buf = clib_min (snd_mss0, n_bytes_per_buf);
+ deq_per_first_buf = clib_min (snd_mss0, n_bytes_per_buf - MAX_HDRS_LEN);
+
+ n_bufs = vec_len (smm->tx_buffers[thread_index]);
+ left_to_snd0 = max_len_to_snd0;
+ for (i = 0; i < n_frames_per_evt; i++)
+ {
+ /* Make sure we have at least one full frame of buffers ready */
+ if (PREDICT_FALSE (n_bufs < n_bufs_per_frame))
+ {
+ vec_validate (smm->tx_buffers[thread_index],
+ n_bufs + n_bufs_per_frame - 1);
+ buffers_allocated = 0;
+ do
+ {
+ buffers_allocated_this_call = vlib_buffer_alloc (vm,
+ &smm->tx_buffers
+ [thread_index]
+ [n_bufs +
+ buffers_allocated],
+ n_bufs_per_frame
+ -
+ buffers_allocated);
+ buffers_allocated += buffers_allocated_this_call;
+ }
+ while (buffers_allocated_this_call > 0
+ && ((buffers_allocated + n_bufs < n_bufs_per_frame)));
+
+ n_bufs += buffers_allocated;
+ _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
+
+ if (PREDICT_FALSE (n_bufs < n_bufs_per_frame))
+ {
+ vec_add1 (smm->pending_event_vector[thread_index], *e0);
+ return -1;
+ }
+ ASSERT (n_bufs >= n_bufs_per_frame);
+ }
+ /* Allow enqueuing of a new event */
+ svm_fifo_unset_event (s0->server_tx_fifo);
+
+ vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
+ while (left_to_snd0 && n_left_to_next)
+ {
+ /*
+ * Handle first buffer in chain separately
+ */
+
+ /* Get free buffer */
+ ASSERT (n_bufs >= 1);
+ bi0 = smm->tx_buffers[thread_index][--n_bufs];
+ _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
+
+ /* usual speculation, or the enqueue_x1 macro will barf */
+ to_next[0] = bi0;
+ to_next += 1;
+ n_left_to_next -= 1;
+
+ b0 = vlib_get_buffer (vm, bi0);
+ b0->error = 0;
+ b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
+ b0->current_data = 0;
+ b0->total_length_not_including_first_buffer = 0;
+
+ len_to_deq0 = clib_min (left_to_snd0, deq_per_first_buf);
+ data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
+ if (peek_data)
+ {
+ n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, tx_offset,
+ len_to_deq0, data0);
+ if (n_bytes_read <= 0)
+ goto dequeue_fail;
+ /* Keep track of progress locally, transport is also supposed to
+ * increment it independently when pushing the header */
+ tx_offset += n_bytes_read;
+ }
+ else
+ {
+ n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo,
+ len_to_deq0, data0);
+ if (n_bytes_read <= 0)
+ goto dequeue_fail;
+ }
+
+ b0->current_length = n_bytes_read;
+
+ left_to_snd0 -= n_bytes_read;
+ *n_tx_packets = *n_tx_packets + 1;
+
+ /*
+ * Fill in the remaining buffers in the chain, if any
+ */
+ if (PREDICT_FALSE (n_bufs_per_seg > 1 && left_to_snd0))
+ {
+ left_for_seg = clib_min (snd_mss0 - n_bytes_read, left_to_snd0);
+ session_tx_fifo_chain_tail (smm, vm, thread_index,
+ s0->server_tx_fifo, b0, bi0,
+ n_bufs_per_seg, left_for_seg,
+ &left_to_snd0, &n_bufs, &tx_offset,
+ deq_per_buf, peek_data);
+ }
+
+ /* Ask transport to push header after current_length and
+ * total_length_not_including_first_buffer are updated */
+ transport_vft->push_header (tc0, b0);
+
+ /* *INDENT-OFF* */
+ SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({
+ ed->data[0] = e0->event_id;
+ ed->data[1] = max_dequeue0;
+ ed->data[2] = len_to_deq0;
+ ed->data[3] = left_to_snd0;
+ }));
+ /* *INDENT-ON* */
+
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
+ if (VLIB_BUFFER_TRACE_TRAJECTORY)
+ b0->pre_data[1] = 3;
+
+ if (PREDICT_FALSE (n_trace > 0))
+ {
+ session_queue_trace_t *t0;
+ vlib_trace_buffer (vm, node, next_index, b0,
+ 1 /* follow_chain */ );
+ vlib_set_trace_count (vm, node, --n_trace);
+ t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
+ t0->session_index = s0->session_index;
+ t0->server_thread_index = s0->thread_index;
+ }
+
+ vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+ to_next, n_left_to_next,
+ bi0, next0);
+ }
+ vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+ }
+
+ /* If we couldn't dequeue all bytes mark as partially read */
+ if (max_len_to_snd0 < max_dequeue0)
+ {
+ /* If we don't already have new event */
+ if (svm_fifo_set_event (s0->server_tx_fifo))
+ {
+ vec_add1 (smm->pending_event_vector[thread_index], *e0);
+ }
+ }
+ return 0;
+
+dequeue_fail:
+ /*
+ * Can't read from fifo. If we don't already have an event, save as partially
+ * read, return buff to free list and return
+ */
+ clib_warning ("dequeue fail");
+
+ if (svm_fifo_set_event (s0->server_tx_fifo))
+ {
+ vec_add1 (smm->pending_event_vector[thread_index], *e0);
+ }
+ vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
+ _vec_len (smm->tx_buffers[thread_index]) += 1;
+
+ return 0;
+}
+
+int
+session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
+ session_manager_main_t * smm,
+ session_fifo_event_t * e0,
+ stream_session_t * s0, u32 thread_index,
+ int *n_tx_pkts)
+{
+ return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
+ n_tx_pkts, 1);
+}
+
+int
+session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
+ session_manager_main_t * smm,
+ session_fifo_event_t * e0,
+ stream_session_t * s0, u32 thread_index,
+ int *n_tx_pkts)
+{
+ return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
+ n_tx_pkts, 0);
+}
+
+always_inline stream_session_t *
+session_event_get_session (session_fifo_event_t * e, u8 thread_index)
+{
+ return stream_session_get_if_valid (e->fifo->master_session_index,
+ thread_index);
+}
+
+void
+dump_thread_0_event_queue (void)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ vlib_main_t *vm = &vlib_global_main;
+ u32 my_thread_index = vm->thread_index;
+ session_fifo_event_t _e, *e = &_e;
+ stream_session_t *s0;
+ int i, index;
+ i8 *headp;
+
+ unix_shared_memory_queue_t *q;
+ q = smm->vpp_event_queues[my_thread_index];
+
+ index = q->head;
+
+ for (i = 0; i < q->cursize; i++)
+ {
+ headp = (i8 *) (&q->data[0] + q->elsize * index);
+ clib_memcpy (e, headp, q->elsize);
+
+ switch (e->event_type)
+ {
+ case FIFO_EVENT_APP_TX:
+ s0 = session_event_get_session (e, my_thread_index);
+ fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
+ break;
+
+ case FIFO_EVENT_DISCONNECT:
+ s0 = stream_session_get_from_handle (e->session_handle);
+ fformat (stdout, "[%04d] disconnect session %d\n", i,
+ s0->session_index);
+ break;
+
+ case FIFO_EVENT_BUILTIN_RX:
+ s0 = session_event_get_session (e, my_thread_index);
+ fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
+ break;
+
+ case FIFO_EVENT_RPC:
+ fformat (stdout, "[%04d] RPC call %llx with %llx\n",
+ i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
+ break;
+
+ default:
+ fformat (stdout, "[%04d] unhandled event type %d\n",
+ i, e->event_type);
+ break;
+ }
+
+ index++;
+
+ if (index == q->maxsize)
+ index = 0;
+ }
+}
+
+static u8
+session_node_cmp_event (session_fifo_event_t * e, svm_fifo_t * f)
+{
+ stream_session_t *s;
+ switch (e->event_type)
+ {
+ case FIFO_EVENT_APP_RX:
+ case FIFO_EVENT_APP_TX:
+ case FIFO_EVENT_BUILTIN_RX:
+ if (e->fifo == f)
+ return 1;
+ break;
+ case FIFO_EVENT_DISCONNECT:
+ break;
+ case FIFO_EVENT_RPC:
+ s = stream_session_get_from_handle (e->session_handle);
+ if (!s)
+ {
+ clib_warning ("session has event but doesn't exist!");
+ break;
+ }
+ if (s->server_rx_fifo == f || s->server_tx_fifo == f)
+ return 1;
+ break;
+ default:
+ break;
+ }
+ return 0;
+}
+
+u8
+session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ unix_shared_memory_queue_t *q;
+ session_fifo_event_t *pending_event_vector, *evt;
+ int i, index, found = 0;
+ i8 *headp;
+ u8 thread_index;
+
+ ASSERT (e);
+ thread_index = f->master_thread_index;
+ /*
+ * Search evt queue
+ */
+ q = smm->vpp_event_queues[thread_index];
+ index = q->head;
+ for (i = 0; i < q->cursize; i++)
+ {
+ headp = (i8 *) (&q->data[0] + q->elsize * index);
+ clib_memcpy (e, headp, q->elsize);
+ found = session_node_cmp_event (e, f);
+ if (found)
+ break;
+ if (++index == q->maxsize)
+ index = 0;
+ }
+ /*
+ * Search pending events vector
+ */
+ pending_event_vector = smm->pending_event_vector[thread_index];
+ vec_foreach (evt, pending_event_vector)
+ {
+ found = session_node_cmp_event (evt, f);
+ if (found)
+ {
+ clib_memcpy (e, evt, sizeof (*evt));
+ break;
+ }
+ }
+ return found;
+}
+
+static uword
+session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
+ vlib_frame_t * frame)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ session_fifo_event_t *my_pending_event_vector, *e;
+ session_fifo_event_t *my_fifo_events;
+ u32 n_to_dequeue, n_events;
+ unix_shared_memory_queue_t *q;
+ application_t *app;
+ int n_tx_packets = 0;
+ u32 my_thread_index = vm->thread_index;
+ int i, rv;
+ f64 now = vlib_time_now (vm);
+ void (*fp) (void *);
+
+ SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index);
+
+ /*
+ * Update TCP time
+ */
+ tcp_update_time (now, my_thread_index);
+
+ /*
+ * Get vpp queue events
+ */
+ q = smm->vpp_event_queues[my_thread_index];
+ if (PREDICT_FALSE (q == 0))
+ return 0;
+
+ my_fifo_events = smm->free_event_vector[my_thread_index];
+
+ /* min number of events we can dequeue without blocking */
+ n_to_dequeue = q->cursize;
+ my_pending_event_vector = smm->pending_event_vector[my_thread_index];
+
+ if (n_to_dequeue == 0 && vec_len (my_pending_event_vector) == 0)
+ return 0;
+
+ SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
+
+ /*
+ * If we didn't manage to process previous events try going
+ * over them again without dequeuing new ones.
+ */
+ /* XXX: Block senders to sessions that can't keep up */
+ if (0 && vec_len (my_pending_event_vector) >= 100)
+ {
+ clib_warning ("too many fifo events unsolved");
+ goto skip_dequeue;
+ }
+
+ /* See you in the next life, don't be late */
+ if (pthread_mutex_trylock (&q->mutex))
+ return 0;
+
+ for (i = 0; i < n_to_dequeue; i++)
+ {
+ vec_add2 (my_fifo_events, e, 1);
+ unix_shared_memory_queue_sub_raw (q, (u8 *) e);
+ }
+
+ /* The other side of the connection is not polling */
+ if (q->cursize < (q->maxsize / 8))
+ (void) pthread_cond_broadcast (&q->condvar);
+ pthread_mutex_unlock (&q->mutex);
+
+ vec_append (my_fifo_events, my_pending_event_vector);
+
+ _vec_len (my_pending_event_vector) = 0;
+ smm->pending_event_vector[my_thread_index] = my_pending_event_vector;
+
+skip_dequeue:
+ n_events = vec_len (my_fifo_events);
+ for (i = 0; i < n_events; i++)
+ {
+ stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */
+ session_fifo_event_t *e0;
+
+ e0 = &my_fifo_events[i];
+
+ switch (e0->event_type)
+ {
+ case FIFO_EVENT_APP_TX:
+ s0 = session_event_get_session (e0, my_thread_index);
+
+ if (PREDICT_FALSE (!s0))
+ {
+ clib_warning ("It's dead, Jim!");
+ continue;
+ }
+ /* Can retransmit for closed sessions but can't do anything if
+ * session is not ready or closed */
+ if (PREDICT_FALSE (s0->session_state < SESSION_STATE_READY))
+ continue;
+ /* Spray packets in per session type frames, since they go to
+ * different nodes */
+ rv = (smm->session_tx_fns[s0->session_type]) (vm, node, smm, e0, s0,
+ my_thread_index,
+ &n_tx_packets);
+ /* Out of buffers */
+ if (PREDICT_FALSE (rv < 0))
+ {
+ vlib_node_increment_counter (vm, node->node_index,
+ SESSION_QUEUE_ERROR_NO_BUFFER, 1);
+ continue;
+ }
+ break;
+ case FIFO_EVENT_DISCONNECT:
+ s0 = stream_session_get_from_handle (e0->session_handle);
+ stream_session_disconnect (s0);
+ break;
+ case FIFO_EVENT_BUILTIN_RX:
+ s0 = session_event_get_session (e0, my_thread_index);
+ if (PREDICT_FALSE (!s0))
+ continue;
+ svm_fifo_unset_event (s0->server_rx_fifo);
+ app = application_get (s0->app_index);
+ app->cb_fns.builtin_server_rx_callback (s0);
+ break;
+ case FIFO_EVENT_RPC:
+ fp = e0->rpc_args.fp;
+ (*fp) (e0->rpc_args.arg);
+ break;
+
+ default:
+ clib_warning ("unhandled event type %d", e0->event_type);
+ }
+ }
+
+ _vec_len (my_fifo_events) = 0;
+ smm->free_event_vector[my_thread_index] = my_fifo_events;
+
+ vlib_node_increment_counter (vm, session_queue_node.index,
+ SESSION_QUEUE_ERROR_TX, n_tx_packets);
+
+ SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1);
+
+ return n_tx_packets;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (session_queue_node) =
+{
+ .function = session_queue_node_fn,
+ .name = "session-queue",
+ .format_trace = format_session_queue_trace,
+ .type = VLIB_NODE_TYPE_INPUT,
+ .n_errors = ARRAY_LEN (session_queue_error_strings),
+ .error_strings = session_queue_error_strings,
+ .n_next_nodes = SESSION_QUEUE_N_NEXT,
+ .state = VLIB_NODE_STATE_DISABLED,
+ .next_nodes =
+ {
+ [SESSION_QUEUE_NEXT_DROP] = "error-drop",
+ [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup",
+ [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup",
+ [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output",
+ [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output",
+ },
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/stream_session.h b/src/vnet/session/stream_session.h
new file mode 100644
index 00000000..275052d3
--- /dev/null
+++ b/src/vnet/session/stream_session.h
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_VNET_SESSION_STREAM_SESSION_H_
+#define SRC_VNET_SESSION_STREAM_SESSION_H_
+
+#include <vnet/vnet.h>
+#include <svm/svm_fifo.h>
+
+#define foreach_session_type \
+ _(IP4_TCP, ip4_tcp) \
+ _(IP4_UDP, ip4_udp) \
+ _(IP6_TCP, ip6_tcp) \
+ _(IP6_UDP, ip6_udp)
+
+typedef enum
+{
+#define _(A, a) SESSION_TYPE_##A,
+ foreach_session_type
+#undef _
+ SESSION_N_TYPES,
+} session_type_t;
+
+/*
+ * Application session state
+ */
+typedef enum
+{
+ SESSION_STATE_LISTENING,
+ SESSION_STATE_CONNECTING,
+ SESSION_STATE_ACCEPTING,
+ SESSION_STATE_READY,
+ SESSION_STATE_CLOSED,
+ SESSION_STATE_N_STATES,
+} stream_session_state_t;
+
+typedef struct _stream_session_t
+{
+ /** fifo pointers. Once allocated, these do not move */
+ svm_fifo_t *server_rx_fifo;
+ svm_fifo_t *server_tx_fifo;
+
+ /** Type */
+ u8 session_type;
+
+ /** State */
+ volatile u8 session_state;
+
+ u8 thread_index;
+
+ /** To avoid n**2 "one event per frame" check */
+ u8 enqueue_epoch;
+
+ /** svm segment index where fifos were allocated */
+ u32 svm_segment_index;
+
+ /** Session index in per_thread pool */
+ u32 session_index;
+
+ /** Transport specific */
+ u32 connection_index;
+
+ /** stream server pool index */
+ u32 app_index;
+
+ /** Parent listener session if the result of an accept */
+ u32 listener_index;
+
+ CLIB_CACHE_LINE_ALIGN_MARK (pad);
+} stream_session_t;
+
+#endif /* SRC_VNET_SESSION_STREAM_SESSION_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/transport.h b/src/vnet/session/transport.h
new file mode 100644
index 00000000..e2c47949
--- /dev/null
+++ b/src/vnet/session/transport.h
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef VNET_VNET_URI_TRANSPORT_H_
+#define VNET_VNET_URI_TRANSPORT_H_
+
+#include <vnet/vnet.h>
+#include <vnet/ip/ip.h>
+#include <vppinfra/bihash_16_8.h>
+#include <vppinfra/bihash_48_8.h>
+#include <vnet/tcp/tcp_debug.h>
+
+/*
+ * Protocol independent transport properties associated to a session
+ */
+typedef struct _transport_connection
+{
+ ip46_address_t rmt_ip; /**< Remote IP */
+ ip46_address_t lcl_ip; /**< Local IP */
+ u16 lcl_port; /**< Local port */
+ u16 rmt_port; /**< Remote port */
+ u8 transport_proto; /**< Protocol id */
+ u8 is_ip4; /**< Flag if IP4 connection */
+ u32 vrf; /**< FIB table id */
+
+ u32 s_index; /**< Parent session index */
+ u32 c_index; /**< Connection index in transport pool */
+ u32 thread_index; /**< Worker-thread index */
+
+ fib_node_index_t rmt_fei; /**< FIB entry index for rmt */
+ dpo_id_t rmt_dpo; /**< Forwarding DPO for rmt */
+
+#if TRANSPORT_DEBUG
+ elog_track_t elog_track; /**< Event logging */
+ u32 cc_stat_tstamp; /**< CC stats timestamp */
+#endif
+
+ /** Macros for 'derived classes' where base is named "connection" */
+#define c_lcl_ip connection.lcl_ip
+#define c_rmt_ip connection.rmt_ip
+#define c_lcl_ip4 connection.lcl_ip.ip4
+#define c_rmt_ip4 connection.rmt_ip.ip4
+#define c_lcl_ip6 connection.lcl_ip.ip6
+#define c_rmt_ip6 connection.rmt_ip.ip6
+#define c_lcl_port connection.lcl_port
+#define c_rmt_port connection.rmt_port
+#define c_transport_proto connection.transport_proto
+#define c_vrf connection.vrf
+#define c_state connection.state
+#define c_s_index connection.s_index
+#define c_c_index connection.c_index
+#define c_is_ip4 connection.is_ip4
+#define c_thread_index connection.thread_index
+#define c_elog_track connection.elog_track
+#define c_cc_stat_tstamp connection.cc_stat_tstamp
+#define c_rmt_fei connection.rmt_fei
+#define c_rmt_dpo connection.rmt_dpo
+} transport_connection_t;
+
+typedef enum _transport_proto
+{
+ TRANSPORT_PROTO_TCP,
+ TRANSPORT_PROTO_UDP
+} transport_proto_t;
+
+typedef struct _transport_endpoint
+{
+ ip46_address_t ip; /** ip address */
+ u16 port; /** port in net order */
+ u8 is_ip4; /** 1 if ip4 */
+ u32 vrf; /** fib table the endpoint is associated with */
+} transport_endpoint_t;
+
+#endif /* VNET_VNET_URI_TRANSPORT_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/transport_interface.c b/src/vnet/session/transport_interface.c
new file mode 100644
index 00000000..ef8d1e49
--- /dev/null
+++ b/src/vnet/session/transport_interface.c
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <vnet/session/transport_interface.h>
+#include <vnet/session/session.h>
+
+/**
+ * Per-type vector of transport protocol virtual function tables
+ */
+transport_proto_vft_t *tp_vfts;
+
+u32
+transport_endpoint_lookup (transport_endpoint_table_t * ht,
+ ip46_address_t * ip, u16 port)
+{
+ clib_bihash_kv_24_8_t kv;
+ int rv;
+
+ kv.key[0] = ip->as_u64[0];
+ kv.key[1] = ip->as_u64[1];
+ kv.key[2] = port;
+
+ rv = clib_bihash_search_inline_24_8 (ht, &kv);
+ if (rv == 0)
+ return kv.value;
+
+ return TRANSPORT_ENDPOINT_INVALID_INDEX;
+}
+
+void
+transport_endpoint_table_add (transport_endpoint_table_t * ht,
+ transport_endpoint_t * te, u32 value)
+{
+ clib_bihash_kv_24_8_t kv;
+
+ kv.key[0] = te->ip.as_u64[0];
+ kv.key[1] = te->ip.as_u64[1];
+ kv.key[2] = te->port;
+ kv.value = value;
+
+ clib_bihash_add_del_24_8 (ht, &kv, 1);
+}
+
+void
+transport_endpoint_table_del (transport_endpoint_table_t * ht,
+ transport_endpoint_t * te)
+{
+ clib_bihash_kv_24_8_t kv;
+
+ kv.key[0] = te->ip.as_u64[0];
+ kv.key[1] = te->ip.as_u64[1];
+ kv.key[2] = te->port;
+
+ clib_bihash_add_del_24_8 (ht, &kv, 0);
+}
+
+/**
+ * Register transport virtual function table.
+ *
+ * @param type - session type (not protocol type)
+ * @param vft - virtual function table
+ */
+void
+session_register_transport (transport_proto_t transport_proto, u8 is_ip4,
+ const transport_proto_vft_t * vft)
+{
+ u8 session_type;
+ session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
+
+ vec_validate (tp_vfts, session_type);
+ tp_vfts[session_type] = *vft;
+
+ /* If an offset function is provided, then peek instead of dequeue */
+ session_manager_set_transport_rx_fn (session_type,
+ vft->tx_fifo_offset != 0);
+}
+
+/**
+ * Get transport virtual function table
+ *
+ * @param type - session type (not protocol type)
+ */
+transport_proto_vft_t *
+session_get_transport_vft (u8 session_type)
+{
+ if (session_type >= vec_len (tp_vfts))
+ return 0;
+ return &tp_vfts[session_type];
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/transport_interface.h b/src/vnet/session/transport_interface.h
new file mode 100644
index 00000000..661221c4
--- /dev/null
+++ b/src/vnet/session/transport_interface.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_VNET_SESSION_TRANSPORT_INTERFACE_H_
+#define SRC_VNET_SESSION_TRANSPORT_INTERFACE_H_
+
+#include <vnet/vnet.h>
+#include <vnet/session/transport.h>
+
+/*
+ * Transport protocol virtual function table
+ */
+typedef struct _transport_proto_vft
+{
+ /*
+ * Setup
+ */
+ u32 (*bind) (u32 session_index, transport_endpoint_t * lcl);
+ u32 (*unbind) (u32);
+ int (*open) (transport_endpoint_t * rmt);
+ void (*close) (u32 conn_index, u32 thread_index);
+ void (*cleanup) (u32 conn_index, u32 thread_index);
+
+ /*
+ * Transmission
+ */
+ u32 (*push_header) (transport_connection_t * tconn, vlib_buffer_t * b);
+ u16 (*send_mss) (transport_connection_t * tc);
+ u32 (*send_space) (transport_connection_t * tc);
+ u32 (*tx_fifo_offset) (transport_connection_t * tc);
+
+ /*
+ * Connection retrieval
+ */
+ transport_connection_t *(*get_connection) (u32 conn_idx, u32 thread_idx);
+ transport_connection_t *(*get_listener) (u32 conn_index);
+ transport_connection_t *(*get_half_open) (u32 conn_index);
+
+ /*
+ * Format
+ */
+ u8 *(*format_connection) (u8 * s, va_list * args);
+ u8 *(*format_listener) (u8 * s, va_list * args);
+ u8 *(*format_half_open) (u8 * s, va_list * args);
+} transport_proto_vft_t;
+
+typedef clib_bihash_24_8_t transport_endpoint_table_t;
+
+#define TRANSPORT_ENDPOINT_INVALID_INDEX ((u32)~0)
+
+u32 transport_endpoint_lookup (transport_endpoint_table_t * ht,
+ ip46_address_t * ip, u16 port);
+void transport_endpoint_table_add (transport_endpoint_table_t * ht,
+ transport_endpoint_t * te, u32 value);
+void transport_endpoint_table_del (transport_endpoint_table_t * ht,
+ transport_endpoint_t * te);
+
+void session_register_transport (transport_proto_t transport_proto, u8 is_ip4,
+ const transport_proto_vft_t * vft);
+transport_proto_vft_t *session_get_transport_vft (u8 session_type);
+
+#endif /* SRC_VNET_SESSION_TRANSPORT_INTERFACE_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */