summaryrefslogtreecommitdiffstats
path: root/src/vnet/session
diff options
context:
space:
mode:
Diffstat (limited to 'src/vnet/session')
-rw-r--r--src/vnet/session/application.c343
-rw-r--r--src/vnet/session/application.h120
-rw-r--r--src/vnet/session/application_interface.c459
-rw-r--r--src/vnet/session/application_interface.h136
-rw-r--r--src/vnet/session/hashes.c28
-rw-r--r--src/vnet/session/node.c435
-rw-r--r--src/vnet/session/session.api429
-rw-r--r--src/vnet/session/session.c1286
-rw-r--r--src/vnet/session/session.h380
-rw-r--r--src/vnet/session/session_api.c821
-rw-r--r--src/vnet/session/session_cli.c189
-rw-r--r--src/vnet/session/transport.c64
-rw-r--r--src/vnet/session/transport.h250
13 files changed, 4940 insertions, 0 deletions
diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c
new file mode 100644
index 00000000000..a561e7d17f0
--- /dev/null
+++ b/src/vnet/session/application.c
@@ -0,0 +1,343 @@
+/*
+ * 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>
+
+/*
+ * 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;
+
+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;
+}
+
+void
+application_del (application_t * app)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ api_main_t *am = &api_main;
+ void *oldheap;
+ session_manager_t *sm;
+
+ if (app->mode == APP_SERVER)
+ {
+ sm = session_manager_get (app->session_manager_index);
+ session_manager_del (smm, sm);
+ }
+
+ /* Free the event fifo in the /vpe-api shared-memory segment */
+ oldheap = svm_push_data_heap (am->vlib_rp);
+ if (app->event_queue)
+ unix_shared_memory_queue_free (app->event_queue);
+ svm_pop_heap (oldheap);
+
+ application_table_del (app);
+
+ pool_put (app_pool, app);
+}
+
+application_t *
+application_new (application_type_t type, session_type_t sst,
+ u32 api_client_index, u32 flags, session_cb_vft_t * cb_fns)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ api_main_t *am = &api_main;
+ application_t *app;
+ void *oldheap;
+ session_manager_t *sm;
+
+ pool_get (app_pool, app);
+ memset (app, 0, sizeof (*app));
+
+ /* Allocate event fifo in the /vpe-api shared-memory segment */
+ oldheap = svm_push_data_heap (am->vlib_rp);
+
+ /* Allocate server event queue */
+ app->event_queue =
+ unix_shared_memory_queue_init (128 /* nels $$$$ config */ ,
+ sizeof (session_fifo_event_t),
+ 0 /* consumer pid */ ,
+ 0
+ /* (do not) signal when queue non-empty */
+ );
+
+ svm_pop_heap (oldheap);
+
+ /* If a server, allocate session manager */
+ if (type == APP_SERVER)
+ {
+ pool_get (smm->session_managers, sm);
+ memset (sm, 0, sizeof (*sm));
+
+ app->session_manager_index = sm - smm->session_managers;
+ }
+ else if (type == APP_CLIENT)
+ {
+ /* Allocate connect session manager if needed */
+ if (smm->connect_manager_index[sst] == INVALID_INDEX)
+ connects_session_manager_init (smm, sst);
+ app->session_manager_index = smm->connect_manager_index[sst];
+ }
+
+ app->mode = type;
+ app->index = application_get_index (app);
+ app->session_type = sst;
+ app->api_client_index = api_client_index;
+ app->flags = flags;
+ app->cb_fns = *cb_fns;
+
+ /* Add app to lookup by api_client_index table */
+ application_table_add (app);
+
+ return app;
+}
+
+application_t *
+application_get (u32 index)
+{
+ return pool_elt_at_index (app_pool, index);
+}
+
+u32
+application_get_index (application_t * app)
+{
+ return app - app_pool;
+}
+
+int
+application_server_init (application_t * server, u32 segment_size,
+ u32 add_segment_size, u32 rx_fifo_size,
+ u32 tx_fifo_size, u8 ** segment_name)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ session_manager_t *sm;
+ int rv;
+
+ sm = session_manager_get (server->session_manager_index);
+
+ /* Add first segment */
+ if ((rv = session_manager_add_first_segment (smm, sm, segment_size,
+ segment_name)))
+ {
+ return rv;
+ }
+
+ /* Setup session manager */
+ sm->add_segment_size = add_segment_size;
+ sm->rx_fifo_size = rx_fifo_size;
+ sm->tx_fifo_size = tx_fifo_size;
+ sm->add_segment = sm->add_segment_size != 0;
+ return 0;
+}
+
+u8 *
+format_application_server (u8 * s, va_list * args)
+{
+ application_t *srv = va_arg (*args, application_t *);
+ int verbose = va_arg (*args, int);
+ vl_api_registration_t *regp;
+ stream_session_t *listener;
+ u8 *server_name, *str, *seg_name;
+ u32 segment_size;
+
+ if (srv == 0)
+ {
+ if (verbose)
+ s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "Server",
+ "Segment", "API Client", "Cookie");
+ else
+ s = format (s, "%-40s%-20s", "Connection", "Server");
+
+ return s;
+ }
+
+ regp = vl_api_client_index_to_registration (srv->api_client_index);
+ if (!regp)
+ server_name = format (0, "%s%c", regp->name, 0);
+ else
+ server_name = regp->name;
+
+ listener = stream_session_listener_get (srv->session_type,
+ srv->session_index);
+ str = format (0, "%U", format_stream_session, listener, verbose);
+
+ session_manager_get_segment_info (listener->server_segment_index, &seg_name,
+ &segment_size);
+ if (verbose)
+ {
+ s = format (s, "%-40s%-20s%-20s%-10d%-10d", str, server_name,
+ seg_name, srv->api_client_index, srv->accept_cookie);
+ }
+ else
+ s = format (s, "%-40s%-20s", str, server_name);
+ return s;
+}
+
+u8 *
+format_application_client (u8 * s, va_list * args)
+{
+ application_t *client = va_arg (*args, application_t *);
+ int verbose = va_arg (*args, int);
+ stream_session_t *session;
+ u8 *str, *seg_name;
+ u32 segment_size;
+
+ if (client == 0)
+ {
+ if (verbose)
+ s =
+ format (s, "%-40s%-20s%-10s", "Connection", "Segment",
+ "API Client");
+ else
+ s = format (s, "%-40s", "Connection");
+
+ return s;
+ }
+
+ session = stream_session_get (client->session_index, client->thread_index);
+ str = format (0, "%U", format_stream_session, session, verbose);
+
+ session_manager_get_segment_info (session->server_segment_index, &seg_name,
+ &segment_size);
+ if (verbose)
+ {
+ s = format (s, "%-40s%-20s%-10d%", str, seg_name,
+ client->api_client_index);
+ }
+ else
+ s = format (s, "%-40s", str);
+ 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;
+
+ 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)
+ {
+ if (pool_elts (app_pool))
+ {
+ vlib_cli_output (vm, "%U", format_application_server,
+ 0 /* header */ ,
+ verbose);
+ /* *INDENT-OFF* */
+ pool_foreach (app, app_pool,
+ ({
+ if (app->mode == APP_SERVER)
+ vlib_cli_output (vm, "%U", format_application_server, app,
+ verbose);
+ }));
+ /* *INDENT-ON* */
+ }
+ else
+ vlib_cli_output (vm, "No active server bindings");
+ }
+
+ if (do_client)
+ {
+ if (pool_elts (app_pool))
+ {
+ vlib_cli_output (vm, "%U", format_application_client,
+ 0 /* header */ ,
+ verbose);
+ /* *INDENT-OFF* */
+ pool_foreach (app, app_pool,
+ ({
+ if (app->mode == APP_CLIENT)
+ vlib_cli_output (vm, "%U", format_application_client, app,
+ verbose);
+ }));
+ /* *INDENT-ON* */
+ }
+ else
+ vlib_cli_output (vm, "No active server bindings");
+ }
+
+ return 0;
+}
+
+VLIB_CLI_COMMAND (show_app_command, static) =
+{
+.path = "show app",.short_help =
+ "show app [server|client] [verbose]",.function = show_app_command_fn,};
+
+/*
+ * 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 00000000000..027d6967fd7
--- /dev/null
+++ b/src/vnet/session/application.h
@@ -0,0 +1,120 @@
+/*
+ * 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>
+
+typedef enum
+{
+ APP_SERVER,
+ APP_CLIENT
+} 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 api_client_index,
+ 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 connection index, ~0 if internal */
+ u32 api_client_index;
+
+ /* */
+ u32 api_context;
+
+ /** Application listens for events on this svm queue */
+ unix_shared_memory_queue_t *event_queue;
+
+ /** Stream session type */
+ u8 session_type;
+
+ /* Stream server mode: accept or connect */
+ u8 mode;
+
+ u32 session_manager_index;
+
+ /*
+ * Bind/Listen specific
+ */
+
+ /** Accept cookie, for multiple session flavors ($$$ maybe) */
+ u32 accept_cookie;
+
+ /** Index of the listen session or connect session */
+ u32 session_index;
+
+ /** Session thread index for client connect sessions */
+ u32 thread_index;
+
+ /*
+ * Callbacks: shoulder-taps for the server/client
+ */
+ session_cb_vft_t cb_fns;
+} application_t;
+
+application_t *application_new (application_type_t type, session_type_t sst,
+ u32 api_client_index, u32 flags,
+ session_cb_vft_t * cb_fns);
+void application_del (application_t * app);
+application_t *application_get (u32 index);
+application_t *application_lookup (u32 api_client_index);
+u32 application_get_index (application_t * app);
+
+int
+application_server_init (application_t * server, u32 segment_size,
+ u32 add_segment_size, u32 rx_fifo_size,
+ u32 tx_fifo_size, u8 ** segment_name);
+int application_api_queue_is_full (application_t * app);
+
+#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 00000000000..0ea77fd8c4e
--- /dev/null
+++ b/src/vnet/session/application_interface.c
@@ -0,0 +1,459 @@
+/*
+ * 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));
+ 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 api_client_index, ip46_address_t * ip46, u16 port_host_order,
+ session_type_t sst, u64 * options, session_cb_vft_t * cb_fns,
+ application_t ** app, u32 * len_seg_name, char *seg_name)
+{
+ u8 *segment_name = 0;
+ application_t *server = 0;
+ stream_session_t *listener;
+ u8 is_ip4;
+
+ listener =
+ stream_session_lookup_listener (ip46,
+ clib_host_to_net_u16 (port_host_order),
+ sst);
+
+ if (listener)
+ return VNET_API_ERROR_ADDRESS_IN_USE;
+
+ if (application_lookup (api_client_index))
+ {
+ clib_warning ("Only one bind supported for now");
+ return VNET_API_ERROR_ADDRESS_IN_USE;
+ }
+
+ is_ip4 = SESSION_TYPE_IP4_UDP == sst || SESSION_TYPE_IP4_TCP == sst;
+ if (!ip_is_zero (ip46, is_ip4) && !ip_is_local (ip46, is_ip4))
+ return VNET_API_ERROR_INVALID_VALUE;
+
+ /* Allocate and initialize stream server */
+ server = application_new (APP_SERVER, sst, api_client_index,
+ options[SESSION_OPTIONS_FLAGS], cb_fns);
+
+ application_server_init (server, options[SESSION_OPTIONS_SEGMENT_SIZE],
+ options[SESSION_OPTIONS_ADD_SEGMENT_SIZE],
+ options[SESSION_OPTIONS_RX_FIFO_SIZE],
+ options[SESSION_OPTIONS_TX_FIFO_SIZE],
+ &segment_name);
+
+ /* Setup listen path down to transport */
+ stream_session_start_listen (server->index, ip46, port_host_order);
+
+ /*
+ * Return values
+ */
+
+ ASSERT (vec_len (segment_name) <= 128);
+ *len_seg_name = vec_len (segment_name);
+ memcpy (seg_name, segment_name, *len_seg_name);
+ *app = server;
+
+ return 0;
+}
+
+int
+vnet_unbind_i (u32 api_client_index)
+{
+ application_t *server;
+
+ /*
+ * Find the stream_server_t corresponding to the api client
+ */
+ server = application_lookup (api_client_index);
+ if (!server)
+ return VNET_API_ERROR_INVALID_VALUE_2;
+
+ /* Clear the listener */
+ stream_session_stop_listen (server->index);
+ application_del (server);
+
+ return 0;
+}
+
+int
+vnet_connect_i (u32 api_client_index, u32 api_context, session_type_t sst,
+ ip46_address_t * ip46, u16 port, u64 * options, void *mp,
+ session_cb_vft_t * cb_fns)
+{
+ stream_session_t *listener;
+ application_t *server, *app;
+
+ /*
+ * Figure out if connecting to a local server
+ */
+ listener = stream_session_lookup_listener (ip46,
+ clib_host_to_net_u16 (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 & SESSION_OPTIONS_FLAGS_USE_FIFO)
+ return server->cb_fns.
+ redirect_connect_callback (server->api_client_index, mp);
+ }
+
+ /* Create client app */
+ app = application_new (APP_CLIENT, sst, api_client_index,
+ options[SESSION_OPTIONS_FLAGS], cb_fns);
+
+ app->api_context = api_context;
+
+ /*
+ * Not connecting to a local server. Create regular session
+ */
+ stream_session_open (sst, ip46, port, app->index);
+
+ return 0;
+}
+
+/**
+ * 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)
+{
+ ip46_address_t *address = va_arg (*args, ip46_address_t *);
+ session_type_t *sst = va_arg (*args, session_type_t *);
+ u16 *port = va_arg (*args, u16 *);
+
+ if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &address->ip4,
+ port))
+ {
+ *sst = SESSION_TYPE_IP4_TCP;
+ return 1;
+ }
+ if (unformat (input, "udp://%U/%d", unformat_ip4_address, &address->ip4,
+ port))
+ {
+ *sst = SESSION_TYPE_IP4_UDP;
+ return 1;
+ }
+ if (unformat (input, "udp://%U/%d", unformat_ip6_address, &address->ip6,
+ port))
+ {
+ *sst = SESSION_TYPE_IP6_UDP;
+ return 1;
+ }
+ if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &address->ip6,
+ port))
+ {
+ *sst = SESSION_TYPE_IP6_TCP;
+ return 1;
+ }
+
+ return 0;
+}
+
+int
+parse_uri (char *uri, session_type_t * sst, ip46_address_t * addr,
+ u16 * port_number_host_byte_order)
+{
+ unformat_input_t _input, *input = &_input;
+
+ /* 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, addr, sst,
+ port_number_host_byte_order))
+ {
+ unformat_free (input);
+ return VNET_API_ERROR_INVALID_VALUE;
+ }
+ unformat_free (input);
+
+ return 0;
+}
+
+int
+vnet_bind_uri (vnet_bind_args_t * a)
+{
+ application_t *server = 0;
+ u16 port_host_order;
+ session_type_t sst = SESSION_N_TYPES;
+ ip46_address_t ip46;
+ int rv;
+
+ memset (&ip46, 0, sizeof (ip46));
+ rv = parse_uri (a->uri, &sst, &ip46, &port_host_order);
+ if (rv)
+ return rv;
+
+ if ((rv = vnet_bind_i (a->api_client_index, &ip46, port_host_order, sst,
+ a->options, a->session_cb_vft, &server,
+ &a->segment_name_length, a->segment_name)))
+ return rv;
+
+ a->server_event_queue_address = (u64) server->event_queue;
+ return 0;
+}
+
+session_type_t
+session_type_from_proto_and_ip (session_api_proto_t proto, u8 is_ip4)
+{
+ if (proto == SESSION_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;
+}
+
+int
+vnet_unbind_uri (char *uri, u32 api_client_index)
+{
+ u16 port_number_host_byte_order;
+ session_type_t sst = SESSION_N_TYPES;
+ ip46_address_t ip46_address;
+ stream_session_t *listener;
+ int rv;
+
+ rv = parse_uri (uri, &sst, &ip46_address, &port_number_host_byte_order);
+ if (rv)
+ return rv;
+
+ listener =
+ stream_session_lookup_listener (&ip46_address,
+ clib_host_to_net_u16
+ (port_number_host_byte_order), sst);
+
+ if (!listener)
+ return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
+
+ /* External client? */
+ if (api_client_index != ~0)
+ {
+ ASSERT (vl_api_client_index_to_registration (api_client_index));
+ }
+
+ return vnet_unbind_i (api_client_index);
+}
+
+int
+vnet_connect_uri (vnet_connect_args_t * a)
+{
+ ip46_address_t ip46_address;
+ u16 port;
+ session_type_t sst;
+ application_t *app;
+ int rv;
+
+ app = application_lookup (a->api_client_index);
+ if (app)
+ {
+ clib_warning ("Already have a connect from this app");
+ return VNET_API_ERROR_INVALID_VALUE_2;
+ }
+
+ /* Parse uri */
+ rv = parse_uri (a->uri, &sst, &ip46_address, &port);
+ if (rv)
+ return rv;
+
+ return vnet_connect_i (a->api_client_index, a->api_context, sst,
+ &ip46_address, port, a->options, a->mp,
+ a->session_cb_vft);
+}
+
+int
+vnet_disconnect_session (u32 client_index, u32 session_index,
+ u32 thread_index)
+{
+ stream_session_t *session;
+
+ session = stream_session_get (session_index, thread_index);
+ stream_session_disconnect (session);
+
+ return 0;
+}
+
+
+int
+vnet_bind (vnet_bind_args_t * a)
+{
+ application_t *server = 0;
+ 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->api_client_index, &a->tep.ip, a->tep.port, sst,
+ a->options, a->session_cb_vft, &server,
+ &a->segment_name_length, a->segment_name)))
+ return rv;
+
+ a->server_event_queue_address = (u64) server->event_queue;
+ a->handle = (u64) a->tep.vrf << 32 | (u64) server->session_index;
+ return 0;
+}
+
+int
+vnet_unbind (vnet_unbind_args_t * a)
+{
+ application_t *server;
+
+ if (a->api_client_index != ~0)
+ {
+ ASSERT (vl_api_client_index_to_registration (a->api_client_index));
+ }
+
+ /* Make sure this is the right one */
+ server = application_lookup (a->api_client_index);
+ ASSERT (server->session_index == (0xFFFFFFFF & a->handle));
+
+ /* TODO use handle to disambiguate namespaces/vrfs */
+ return vnet_unbind_i (a->api_client_index);
+}
+
+int
+vnet_connect (vnet_connect_args_t * a)
+{
+ session_type_t sst;
+ application_t *app;
+
+ app = application_lookup (a->api_client_index);
+ if (app)
+ {
+ clib_warning ("Already have a connect from this app");
+ return VNET_API_ERROR_INVALID_VALUE_2;
+ }
+
+ sst = session_type_from_proto_and_ip (a->proto, a->tep.is_ip4);
+ return vnet_connect_i (a->api_client_index, a->api_context, sst, &a->tep.ip,
+ a->tep.port, a->options, a->mp, a->session_cb_vft);
+}
+
+int
+vnet_disconnect (vnet_disconnect_args_t * a)
+{
+ stream_session_t *session;
+ u32 session_index, thread_index;
+
+ if (api_parse_session_handle (a->handle, &session_index, &thread_index))
+ {
+ clib_warning ("Invalid handle");
+ return -1;
+ }
+
+ session = stream_session_get (session_index, thread_index);
+ stream_session_disconnect (session);
+
+ return 0;
+}
+
+/*
+ * 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 00000000000..8d87c067841
--- /dev/null
+++ b/src/vnet/session/application_interface.h
@@ -0,0 +1,136 @@
+/*
+ * 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 enum _session_api_proto
+{
+ SESSION_PROTO_TCP,
+ SESSION_PROTO_UDP
+} session_api_proto_t;
+
+typedef struct _vnet_bind_args_t
+{
+ union
+ {
+ char *uri;
+ struct
+ {
+ transport_endpoint_t tep;
+ session_api_proto_t proto;
+ };
+ };
+
+ u32 api_client_index;
+ u64 *options;
+ session_cb_vft_t *session_cb_vft;
+
+ /*
+ * 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 api_client_index;
+} vnet_unbind_args_t;
+
+typedef struct _vnet_connect_args
+{
+ union
+ {
+ char *uri;
+ struct
+ {
+ transport_endpoint_t tep;
+ session_api_proto_t proto;
+ };
+ };
+ u32 api_client_index;
+ u32 api_context;
+ u64 *options;
+ session_cb_vft_t *session_cb_vft;
+
+ /* Used for redirects */
+ void *mp;
+} vnet_connect_args_t;
+
+typedef struct _vnet_disconnect_args_t
+{
+ u64 handle;
+ u32 api_client_index;
+} vnet_disconnect_args_t;
+
+/* Bind / connect options */
+typedef enum
+{
+ SESSION_OPTIONS_FLAGS,
+ SESSION_OPTIONS_SEGMENT_SIZE,
+ SESSION_OPTIONS_ADD_SEGMENT_SIZE,
+ SESSION_OPTIONS_RX_FIFO_SIZE,
+ SESSION_OPTIONS_TX_FIFO_SIZE,
+ SESSION_OPTIONS_ACCEPT_COOKIE,
+ SESSION_OPTIONS_N_OPTIONS
+} session_options_index_t;
+
+/** Server can handle delegated connect requests from local clients */
+#define SESSION_OPTIONS_FLAGS_USE_FIFO (1<<0)
+
+/** Server wants vpp to add segments when out of memory for fifos */
+#define SESSION_OPTIONS_FLAGS_ADD_SEGMENT (1<<1)
+
+#define VNET_CONNECT_REDIRECTED 123
+
+int vnet_bind_uri (vnet_bind_args_t *);
+int vnet_unbind_uri (char *uri, u32 api_client_index);
+int vnet_connect_uri (vnet_connect_args_t * a);
+int
+vnet_disconnect_session (u32 client_index, u32 session_index,
+ u32 thread_index);
+
+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 vnet_disconnect (vnet_disconnect_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/hashes.c b/src/vnet/session/hashes.c
new file mode 100644
index 00000000000..1808dd73f90
--- /dev/null
+++ b/src/vnet/session/hashes.c
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+
+/** 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>
diff --git a/src/vnet/session/node.c b/src/vnet/session/node.c
new file mode 100644
index 00000000000..e467f4e99e2
--- /dev/null
+++ b/src/vnet/session/node.c
@@ -0,0 +1,435 @@
+/*
+ * 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 <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include <vnet/pg/pg.h>
+#include <vnet/ip/ip.h>
+
+#include <vnet/tcp/tcp.h>
+
+#include <vppinfra/hash.h>
+#include <vppinfra/error.h>
+#include <vppinfra/elog.h>
+#include <vlibmemory/unix_shared_memory_queue.h>
+
+#include <vnet/udp/udp_packet.h>
+#include <vnet/lisp-cp/packets.h>
+#include <math.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")
+
+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 int
+session_fifo_rx_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, n_bufs, snd_space0;
+ u32 n_frame_bytes, n_frames_per_evt;
+ 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 rx_offset;
+ u16 snd_mss0;
+ u8 *data0;
+ int i;
+
+ 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_space0 = transport_vft->send_space (tc0);
+ snd_mss0 = transport_vft->send_mss (tc0);
+
+ if (snd_space0 == 0 || svm_fifo_max_dequeue (s0->server_tx_fifo) == 0
+ || snd_mss0 == 0)
+ return 0;
+
+ ASSERT (e0->enqueue_length > 0);
+
+ /* Ensure we're not writing more than transport window allows */
+ max_len_to_snd0 = clib_min (e0->enqueue_length, snd_space0);
+
+ if (peek_data)
+ {
+ /* Offset in rx fifo from where to peek data */
+ rx_offset = transport_vft->rx_fifo_offset (tc0);
+ }
+
+ /* TODO check if transport is willing to send len_to_snd0
+ * bytes (Nagle) */
+
+ n_frame_bytes = snd_mss0 * VLIB_FRAME_SIZE;
+ n_frames_per_evt = ceil ((double) max_len_to_snd0 / n_frame_bytes);
+
+ 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 < VLIB_FRAME_SIZE))
+ {
+ vec_validate (smm->tx_buffers[thread_index],
+ n_bufs + VLIB_FRAME_SIZE - 1);
+ n_bufs +=
+ vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][n_bufs],
+ VLIB_FRAME_SIZE);
+
+ /* buffer shortage
+ * XXX 0.9 because when debugging we might not get a full frame */
+ if (PREDICT_FALSE (n_bufs < 0.9 * VLIB_FRAME_SIZE))
+ {
+ /* Keep track of how much we've dequeued and exit */
+ e0->enqueue_length -= max_len_to_snd0 - left_to_snd0;
+ return -1;
+ }
+
+ _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
+ }
+
+ vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
+ while (left_to_snd0 && n_left_to_next)
+ {
+ /* Get free buffer */
+ n_bufs--;
+ bi0 = smm->tx_buffers[thread_index][n_bufs];
+ _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
+
+ b0 = vlib_get_buffer (vm, bi0);
+ b0->error = 0;
+ b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID
+ | VNET_BUFFER_LOCALLY_ORIGINATED;
+ b0->current_data = 0;
+
+ /* RX on the local interface. tx in default fib */
+ vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
+ vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
+
+ /* usual speculation, or the enqueue_x1 macro will barf */
+ to_next[0] = bi0;
+ to_next += 1;
+ n_left_to_next -= 1;
+
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
+ 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;
+ }
+
+ if (1)
+ {
+ ELOG_TYPE_DECLARE (e) =
+ {
+ .format = "evt-dequeue: id %d length %d",.format_args =
+ "i4i4",};
+ struct
+ {
+ u32 data[2];
+ } *ed;
+ ed = ELOG_DATA (&vm->elog_main, e);
+ ed->data[0] = e0->event_id;
+ ed->data[1] = e0->enqueue_length;
+ }
+
+ len_to_deq0 = (left_to_snd0 < snd_mss0) ? left_to_snd0 : snd_mss0;
+
+ /* Make room for headers */
+ data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
+
+ /* Dequeue the data
+ * TODO 1) peek instead of dequeue
+ * 2) buffer chains */
+ if (peek_data)
+ {
+ int n_bytes_read;
+ n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, s0->pid,
+ rx_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 header */
+ rx_offset += n_bytes_read;
+ }
+ else
+ {
+ if (svm_fifo_dequeue_nowait (s0->server_tx_fifo, s0->pid,
+ len_to_deq0, data0) < 0)
+ goto dequeue_fail;
+ }
+
+ b0->current_length = len_to_deq0;
+
+ /* Ask transport to push header */
+ transport_vft->push_header (tc0, b0);
+
+ left_to_snd0 -= len_to_deq0;
+ *n_tx_packets = *n_tx_packets + 1;
+
+ 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 store progress */
+ if (max_len_to_snd0 < e0->enqueue_length)
+ {
+ e0->enqueue_length -= max_len_to_snd0;
+ vec_add1 (smm->evts_partially_read[thread_index], *e0);
+ }
+ return 0;
+
+dequeue_fail:
+ /* Can't read from fifo. Store event rx progress, save as partially read,
+ * return buff to free list and return */
+ e0->enqueue_length -= max_len_to_snd0 - left_to_snd0;
+ vec_add1 (smm->evts_partially_read[thread_index], *e0);
+
+ to_next -= 1;
+ n_left_to_next += 1;
+ _vec_len (smm->tx_buffers[thread_index]) += 1;
+
+ clib_warning ("dequeue fail");
+ return 0;
+}
+
+int
+session_fifo_rx_peek (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_fifo_rx_i (vm, node, smm, e0, s0, thread_index, n_tx_pkts,
+ 1);
+}
+
+int
+session_fifo_rx_dequeue (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_fifo_rx_i (vm, node, smm, e0, s0, thread_index, n_tx_pkts,
+ 0);
+}
+
+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_fifo_events, *e;
+ u32 n_to_dequeue;
+ unix_shared_memory_queue_t *q;
+ int n_tx_packets = 0;
+ u32 my_thread_index = vm->cpu_index;
+ int i, rv;
+
+ /*
+ * Update TCP time
+ */
+ tcp_update_time (vlib_time_now (vm), my_thread_index);
+
+ /*
+ * Get vpp queue events
+ */
+ q = smm->vpp_event_queues[my_thread_index];
+ if (PREDICT_FALSE (q == 0))
+ return 0;
+
+ /* min number of events we can dequeue without blocking */
+ n_to_dequeue = q->cursize;
+ if (n_to_dequeue == 0)
+ return 0;
+
+ my_fifo_events = smm->fifo_events[my_thread_index];
+
+ /* 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 (vec_len (my_fifo_events) >= 100)
+ 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);
+
+ smm->fifo_events[my_thread_index] = my_fifo_events;
+
+skip_dequeue:
+
+ for (i = 0; i < n_to_dequeue; i++)
+ {
+ svm_fifo_t *f0; /* $$$ prefetch 1 ahead maybe */
+ stream_session_t *s0;
+ u32 server_session_index0, server_thread_index0;
+ session_fifo_event_t *e0;
+
+ e0 = &my_fifo_events[i];
+ f0 = e0->fifo;
+ server_session_index0 = f0->server_session_index;
+ server_thread_index0 = f0->server_thread_index;
+
+ /* $$$ add multiple event queues, per vpp worker thread */
+ ASSERT (server_thread_index0 == my_thread_index);
+
+ s0 = pool_elt_at_index (smm->sessions[my_thread_index],
+ server_session_index0);
+
+ ASSERT (s0->thread_index == my_thread_index);
+
+ switch (e0->event_type)
+ {
+ case FIFO_EVENT_SERVER_TX:
+ /* Spray packets in per session type frames, since they go to
+ * different nodes */
+ rv = (smm->session_rx_fns[s0->session_type]) (vm, node, smm, e0, s0,
+ my_thread_index,
+ &n_tx_packets);
+ if (rv < 0)
+ goto done;
+
+ break;
+
+ default:
+ clib_warning ("unhandled event type %d", e0->event_type);
+ }
+ }
+
+done:
+
+ /* Couldn't process all events. Probably out of buffers */
+ if (PREDICT_FALSE (i < n_to_dequeue))
+ {
+ session_fifo_event_t *partially_read =
+ smm->evts_partially_read[my_thread_index];
+ vec_add (partially_read, &my_fifo_events[i], n_to_dequeue - i);
+ vec_free (my_fifo_events);
+ smm->fifo_events[my_thread_index] = partially_read;
+ smm->evts_partially_read[my_thread_index] = 0;
+ }
+ else
+ {
+ vec_free (smm->fifo_events[my_thread_index]);
+ smm->fifo_events[my_thread_index] =
+ smm->evts_partially_read[my_thread_index];
+ smm->evts_partially_read[my_thread_index] = 0;
+ }
+
+ vlib_node_increment_counter (vm, session_queue_node.index,
+ SESSION_QUEUE_ERROR_TX, n_tx_packets);
+
+ 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, enable on-demand? */
+ /* edit / add dispositions here */
+ .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/session.api b/src/vnet/session/session.api
new file mode 100644
index 00000000000..a7b28c1dba0
--- /dev/null
+++ b/src/vnet/session/session.api
@@ -0,0 +1,429 @@
+/*
+ * 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 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.
+*/
+define bind_uri {
+ u32 client_index;
+ u32 context;
+ u32 accept_cookie;
+ u32 initial_segment_size;
+ u8 uri[128];
+ u64 options[16];
+};
+
+/** \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.
+*/
+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.
+*/
+define connect_uri {
+ u32 client_index;
+ u32 context;
+ u8 uri[128];
+ u64 client_queue_address;
+ u64 options[16];
+};
+
+/** \brief Bind reply
+ @param context - sender context, to match reply w/ request
+ @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_uri_reply {
+ u32 context;
+ i32 retval;
+ u64 server_event_queue_address;
+ u8 segment_name_length;
+ u32 segment_size;
+ u8 segment_name[128];
+};
+
+/** \brief unbind reply
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+*/
+define unbind_uri_reply {
+ u32 context;
+ i32 retval;
+};
+
+/** \brief vpp->client, connect reply
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+ @param server_rx_fifo - rx (vpp -> vpp-client) fifo address
+ @param server_tx_fifo - tx (vpp-client -> vpp) fifo address
+ @param session_index - session index;
+ @param session_thread_index - session thread index
+ @param session_type - session thread type
+ @param vpp_event_queue_address - vpp's event queue address
+ @param client_event_queue_address - client's event queue address
+ @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_uri_reply {
+ u32 context;
+ i32 retval;
+ u64 server_rx_fifo;
+ u64 server_tx_fifo;
+ u32 session_index;
+ u32 session_thread_index;
+ u8 session_type;
+ u64 client_event_queue_address;
+ u64 vpp_event_queue_address;
+ u32 segment_size;
+ u8 segment_name_length;
+ u8 segment_name[128];
+};
+
+/** \brief vpp->client, please map an additional shared memory segment
+ @param context - sender context, to match reply w/ request
+ @param segment_name -
+*/
+define map_another_segment {
+ u32 client_index;
+ u32 context;
+ u32 segment_size;
+ u8 segment_name[128];
+};
+
+/** \brief client->vpp
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+*/
+define map_another_segment_reply {
+ u32 context;
+ i32 retval;
+};
+
+/** \brief vpp->client, accept this session
+ @param context - sender context, to match reply w/ request
+ @param accept_cookie - tells client which bind flavor just occurred
+ @param rx_fifo_address - rx (vpp -> vpp-client) fifo address
+ @param tx_fifo_address - tx (vpp-client -> vpp) fifo address
+ @param session_index - index of new session
+ @param session_thread_index - thread index of new session
+ @param vpp_event_queue_address - vpp's event queue address
+ @param session_type - type of session
+
+*/
+define accept_session {
+ u32 client_index;
+ u32 context;
+ u32 accept_cookie;
+ u64 server_rx_fifo;
+ u64 server_tx_fifo;
+ u32 session_index;
+ u32 session_thread_index;
+ u64 vpp_event_queue_address;
+ u8 session_type;
+};
+
+/** \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;
+ u8 session_type;
+ u8 session_thread_index;
+ u32 session_index;
+};
+
+/** \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 session_index - cookie #1 from accept_session / connect_reply
+ @param session_thread_index - cookie #2
+*/
+define disconnect_session {
+ u32 client_index;
+ u32 context;
+ u32 session_index;
+ u32 session_thread_index;
+};
+
+/** \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 session_index - session index from accept_session / connect_reply
+ @param session_thread_index - thread index from accept_session /
+ connect_reply
+*/
+define disconnect_session_reply {
+ u32 client_index;
+ u32 context;
+ i32 retval;
+ u32 session_index;
+ u32 session_thread_index;
+};
+
+/** \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 session_index - session index from accept_session / connect_reply
+ @param session_thread_index - thread index from accept_session /
+ connect_reply
+*/
+define reset_session {
+ u32 client_index;
+ u32 context;
+ u32 session_index;
+ u32 session_thread_index;
+};
+
+/** \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 session_index - session index from accept_session / connect_reply
+ @param session_thread_index - thread index from accept_session /
+ connect_reply
+*/
+define reset_session_reply {
+ u32 client_index;
+ u32 context;
+ i32 retval;
+ u32 session_index;
+ u32 session_thread_index;
+};
+
+/** \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
+*/
+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 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.
+*/
+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];
+};
+
+/** \brief unbind reply
+ @param context - sender context, to match reply w/ request
+ @param retval - return code for the request
+*/
+define unbind_sock_reply {
+ u32 context;
+ i32 retval;
+};
+
+/** \brief vpp/server->client, connect reply
+ @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 client_event_queue_address - client's event queue address
+ @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_sock_reply {
+ u32 context;
+ i32 retval;
+ u64 handle;
+ u64 server_rx_fifo;
+ u64 server_tx_fifo;
+ u64 client_event_queue_address;
+ u64 vpp_event_queue_address;
+ u32 segment_size;
+ u8 segment_name_length;
+ u8 segment_name[128];
+};
+
+/** \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 through accept/connect
+*/
+define disconnect_sock {
+ 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 client_context - sender context, to match reply w/ request
+ @param handle - session handle obtained through accept/connect
+*/
+define disconnect_sock_reply {
+ u32 client_index;
+ u32 context;
+ i32 retval;
+ u64 handle;
+};
+
+/** \brief vpp->client, accept this session
+ @param context - sender context, to match reply w/ request
+ @param accept_cookie - tells client which bind flavor just occurred
+ @param handle - session handle obtained through accept/connect
+ @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
+*/
+define accept_sock {
+ u32 client_index;
+ u32 context;
+ u32 accept_cookie;
+ u64 handle;
+ u64 server_rx_fifo;
+ u64 server_tx_fifo;
+ u64 vpp_event_queue_address;
+};
+
+/** \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 handle - session handle obtained through accept/connect
+*/
+define accept_sock_reply {
+ 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 through accept/connect
+*/
+define reset_sock {
+ 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 handle - session handle obtained through accept/connect
+*/
+define reset_sock_reply {
+ u32 client_index;
+ u32 context;
+ i32 retval;
+ u64 handle;
+};
+/*
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */ \ No newline at end of file
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
new file mode 100644
index 00000000000..539da613367
--- /dev/null
+++ b/src/vnet/session/session.c
@@ -0,0 +1,1286 @@
+/*
+ * 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 <vlibmemory/api.h>
+#include <vnet/dpo/load_balance.h>
+#include <vnet/fib/ip4_fib.h>
+#include <vnet/session/application.h>
+
+/**
+ * Per-type vector of transport protocol virtual function tables
+ */
+static transport_proto_vft_t *tp_vfts;
+
+session_manager_main_t session_manager_main;
+
+/*
+ * Session lookup key; (src-ip, dst-ip, src-port, dst-port, session-type)
+ * Value: (owner thread index << 32 | session_index);
+ */
+static void
+stream_session_table_add_for_tc (u8 sst, transport_connection_t * tc,
+ u64 value)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+
+ switch (sst)
+ {
+ case SESSION_TYPE_IP4_UDP:
+ case SESSION_TYPE_IP4_TCP:
+ make_v4_ss_kv_from_tc (&kv4, tc);
+ kv4.value = value;
+ clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4, 1 /* is_add */ );
+ break;
+ case SESSION_TYPE_IP6_UDP:
+ case SESSION_TYPE_IP6_TCP:
+ make_v6_ss_kv_from_tc (&kv6, tc);
+ kv6.value = value;
+ clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6, 1 /* is_add */ );
+ break;
+ default:
+ clib_warning ("Session type not supported");
+ ASSERT (0);
+ }
+}
+
+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 (s->session_type, tc, value);
+}
+
+static void
+stream_session_half_open_table_add (u8 sst, transport_connection_t * tc,
+ u64 value)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+
+ switch (sst)
+ {
+ case SESSION_TYPE_IP4_UDP:
+ case SESSION_TYPE_IP4_TCP:
+ make_v4_ss_kv_from_tc (&kv4, tc);
+ kv4.value = value;
+ clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
+ 1 /* is_add */ );
+ break;
+ case SESSION_TYPE_IP6_UDP:
+ case SESSION_TYPE_IP6_TCP:
+ make_v6_ss_kv_from_tc (&kv6, tc);
+ kv6.value = value;
+ clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
+ 1 /* is_add */ );
+ break;
+ default:
+ clib_warning ("Session type not supported");
+ ASSERT (0);
+ }
+}
+
+static int
+stream_session_table_del_for_tc (session_manager_main_t * smm, u8 sst,
+ transport_connection_t * tc)
+{
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+
+ switch (sst)
+ {
+ case SESSION_TYPE_IP4_UDP:
+ case SESSION_TYPE_IP4_TCP:
+ make_v4_ss_kv_from_tc (&kv4, tc);
+ return clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4,
+ 0 /* is_add */ );
+ break;
+ case SESSION_TYPE_IP6_UDP:
+ case SESSION_TYPE_IP6_TCP:
+ make_v6_ss_kv_from_tc (&kv6, tc);
+ return clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6,
+ 0 /* is_add */ );
+ break;
+ default:
+ clib_warning ("Session type not supported");
+ ASSERT (0);
+ }
+
+ return 0;
+}
+
+static int
+stream_session_table_del (session_manager_main_t * smm, 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 (smm, s->session_type, ts);
+}
+
+static void
+stream_session_half_open_table_del (session_manager_main_t * smm, u8 sst,
+ transport_connection_t * tc)
+{
+ session_kv4_t kv4;
+ session_kv6_t kv6;
+
+ switch (sst)
+ {
+ case SESSION_TYPE_IP4_UDP:
+ case SESSION_TYPE_IP4_TCP:
+ make_v4_ss_kv_from_tc (&kv4, tc);
+ clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
+ 0 /* is_add */ );
+ break;
+ case SESSION_TYPE_IP6_UDP:
+ case SESSION_TYPE_IP6_TCP:
+ make_v6_ss_kv_from_tc (&kv6, tc);
+ clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
+ 0 /* is_add */ );
+ break;
+ default:
+ clib_warning ("Session type not supported");
+ ASSERT (0);
+ }
+}
+
+stream_session_t *
+stream_session_lookup_listener4 (ip4_address_t * lcl, u16 lcl_port, u8 proto)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ session_kv4_t kv4;
+ int rv;
+
+ make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
+ rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
+ if (rv == 0)
+ return pool_elt_at_index (smm->listen_sessions[proto], (u32) kv4.value);
+
+ /* Zero out the lcl ip */
+ kv4.key[0] = 0;
+ rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
+ if (rv == 0)
+ return pool_elt_at_index (smm->listen_sessions[proto], 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,
+ u32 my_thread_index)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ session_kv4_t kv4;
+ 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 (&smm->v4_session_hash, &kv4);
+ if (rv == 0)
+ return stream_session_get_tsi (kv4.value, my_thread_index);
+
+ /* If nothing is found, check if any listener is available */
+ return stream_session_lookup_listener4 (lcl, lcl_port, proto);
+}
+
+stream_session_t *
+stream_session_lookup_listener6 (ip6_address_t * lcl, u16 lcl_port, u8 proto)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ session_kv6_t kv6;
+ int rv;
+
+ make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
+ rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
+ if (rv == 0)
+ return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
+
+ /* Zero out the lcl ip */
+ kv6.key[0] = kv6.key[1] = 0;
+ rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
+ if (rv == 0)
+ return pool_elt_at_index (smm->listen_sessions[proto], 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,
+ u32 my_thread_index)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ 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 (&smm->v6_session_hash, &kv6);
+ if (rv == 0)
+ return stream_session_get_tsi (kv6.value, my_thread_index);
+
+ /* If nothing is found, check if any listener is available */
+ return stream_session_lookup_listener6 (lcl, lcl_port, proto);
+}
+
+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;
+}
+
+static u64
+stream_session_half_open_lookup (session_manager_main_t * smm,
+ ip46_address_t * lcl, ip46_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ 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 (&smm->v4_half_open_hash, &kv4);
+
+ if (rv == 0)
+ return kv4.value;
+
+ return (u64) ~ 0;
+ 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 (&smm->v6_half_open_hash, &kv6);
+
+ if (rv == 0)
+ return kv6.value;
+
+ return (u64) ~ 0;
+ break;
+ }
+ return 0;
+}
+
+transport_connection_t *
+stream_session_lookup_transport4 (session_manager_main_t * smm,
+ ip4_address_t * lcl, ip4_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto,
+ u32 my_thread_index)
+{
+ 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 (&smm->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 (&smm->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_transport6 (session_manager_main_t * smm,
+ ip6_address_t * lcl, ip6_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto,
+ u32 my_thread_index)
+{
+ 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 (&smm->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 (&smm->v6_half_open_hash, &kv6);
+ if (rv == 0)
+ return tp_vfts[s->session_type].get_half_open (kv6.value & 0xFFFFFFFF);
+
+ return 0;
+}
+
+/**
+ * Allocate vpp event queue (once) per worker thread
+ */
+void
+vpp_session_event_queue_allocate (session_manager_main_t * smm,
+ u32 thread_index)
+{
+ api_main_t *am = &api_main;
+ void *oldheap;
+
+ 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);
+
+ smm->vpp_event_queues[thread_index] =
+ unix_shared_memory_queue_init (2048 /* nels $$$$ config */ ,
+ sizeof (session_fifo_event_t),
+ 0 /* consumer pid */ ,
+ 0
+ /* (do not) send signal when queue non-empty */
+ );
+
+ svm_pop_heap (oldheap);
+ }
+}
+
+void
+session_manager_get_segment_info (u32 index, u8 ** name, u32 * size)
+{
+ svm_fifo_segment_private_t *s;
+ s = svm_fifo_get_segment (index);
+ *name = s->h->segment_name;
+ *size = s->ssvm.ssvm_size;
+}
+
+always_inline int
+session_manager_add_segment_i (session_manager_main_t * smm,
+ session_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));
+
+ ca->segment_name = (char *) segment_name;
+ ca->segment_size = segment_size;
+
+ rv = svm_fifo_segment_create (ca);
+ if (rv)
+ {
+ clib_warning ("svm_fifo_segment_create ('%s', %d) failed",
+ ca->segment_name, ca->segment_size);
+ vec_free (segment_name);
+ return -1;
+ }
+
+ vec_add1 (sm->segment_indices, ca->new_segment_index);
+
+ return 0;
+}
+
+static int
+session_manager_add_segment (session_manager_main_t * smm,
+ session_manager_t * sm)
+{
+ u8 *segment_name;
+ svm_fifo_segment_create_args_t _ca, *ca = &_ca;
+ u32 add_segment_size;
+ u32 default_segment_size = 128 << 10;
+
+ memset (ca, 0, sizeof (*ca));
+ segment_name = format (0, "%d-%d%c", getpid (),
+ smm->unique_segment_name_counter++, 0);
+ add_segment_size =
+ sm->add_segment_size ? sm->add_segment_size : default_segment_size;
+
+ return session_manager_add_segment_i (smm, sm, add_segment_size,
+ segment_name);
+}
+
+int
+session_manager_add_first_segment (session_manager_main_t * smm,
+ session_manager_t * sm, u32 segment_size,
+ u8 ** segment_name)
+{
+ svm_fifo_segment_create_args_t _ca, *ca = &_ca;
+ memset (ca, 0, sizeof (*ca));
+ *segment_name = format (0, "%d-%d%c", getpid (),
+ smm->unique_segment_name_counter++, 0);
+ return session_manager_add_segment_i (smm, sm, segment_size, *segment_name);
+}
+
+void
+session_manager_del (session_manager_main_t * smm, session_manager_t * sm)
+{
+ u32 *deleted_sessions = 0;
+ u32 *deleted_thread_indices = 0;
+ int i, j;
+
+ /* Across all fifo segments used by the server */
+ for (j = 0; j < vec_len (sm->segment_indices); j++)
+ {
+ svm_fifo_segment_private_t *fifo_segment;
+ svm_fifo_t **fifos;
+ /* Vector of fifos allocated in the segment */
+ fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]);
+ fifos = (svm_fifo_t **) fifo_segment->h->fifos;
+
+ /*
+ * 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.
+ */
+ for (i = 0; i < vec_len (fifos); i++)
+ {
+ svm_fifo_t *fifo;
+ u32 session_index, thread_index;
+ stream_session_t *session;
+
+ fifo = fifos[i];
+ session_index = fifo->server_session_index;
+ thread_index = fifo->server_thread_index;
+
+ session = pool_elt_at_index (smm->sessions[thread_index],
+ session_index);
+
+ /* Add to the deleted_sessions vector (once!) */
+ if (!session->is_deleted)
+ {
+ session->is_deleted = 1;
+ vec_add1 (deleted_sessions,
+ session - smm->sessions[thread_index]);
+ vec_add1 (deleted_thread_indices, thread_index);
+ }
+ }
+
+ for (i = 0; i < vec_len (deleted_sessions); i++)
+ {
+ stream_session_t *session;
+
+ session =
+ pool_elt_at_index (smm->sessions[deleted_thread_indices[i]],
+ deleted_sessions[i]);
+
+ /* Instead of directly removing the session call disconnect */
+ stream_session_disconnect (session);
+
+ /*
+ stream_session_table_del (smm, session);
+ pool_put(smm->sessions[deleted_thread_indices[i]], session);
+ */
+ }
+
+ vec_reset_length (deleted_sessions);
+ vec_reset_length (deleted_thread_indices);
+
+ /* Instead of removing the segment, test when removing the session if
+ * the segment can be removed
+ */
+ /* svm_fifo_segment_delete (fifo_segment); */
+ }
+
+ vec_free (deleted_sessions);
+ vec_free (deleted_thread_indices);
+}
+
+int
+session_manager_allocate_session_fifos (session_manager_main_t * smm,
+ session_manager_t * sm,
+ svm_fifo_t ** server_rx_fifo,
+ svm_fifo_t ** server_tx_fifo,
+ u32 * fifo_segment_index,
+ u8 * added_a_segment)
+{
+ svm_fifo_segment_private_t *fifo_segment;
+ u32 fifo_size, default_fifo_size = 8192 /* TODO config */ ;
+ int i;
+
+ *added_a_segment = 0;
+
+ /* Allocate svm fifos */
+ ASSERT (vec_len (sm->segment_indices));
+
+again:
+ for (i = 0; i < vec_len (sm->segment_indices); i++)
+ {
+ *fifo_segment_index = sm->segment_indices[i];
+ fifo_segment = svm_fifo_get_segment (*fifo_segment_index);
+
+ fifo_size = sm->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_size = sm->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);
+
+ 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);
+ *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);
+ *server_rx_fifo = 0;
+ }
+ continue;
+ }
+ break;
+ }
+
+ /* See if we're supposed to create another segment */
+ if (*server_rx_fifo == 0)
+ {
+ if (sm->add_segment)
+ {
+ if (*added_a_segment)
+ {
+ clib_warning ("added a segment, still cant allocate a fifo");
+ return SESSION_ERROR_NEW_SEG_NO_SPACE;
+ }
+
+ if (session_manager_add_segment (smm, sm))
+ return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
+
+ *added_a_segment = 1;
+ goto again;
+ }
+ else
+ return SESSION_ERROR_NO_SPACE;
+ }
+ return 0;
+}
+
+int
+stream_session_create_i (session_manager_main_t * smm, application_t * app,
+ transport_connection_t * tc,
+ stream_session_t ** ret_s)
+{
+ int rv;
+ svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0;
+ u32 fifo_segment_index;
+ u32 pool_index, seg_size;
+ stream_session_t *s;
+ u64 value;
+ u32 thread_index = tc->thread_index;
+ session_manager_t *sm;
+ u8 segment_added;
+ u8 *seg_name;
+
+ sm = session_manager_get (app->session_manager_index);
+
+ /* Check the API queue */
+ if (app->mode == APP_SERVER && application_api_queue_is_full (app))
+ return SESSION_ERROR_API_QUEUE_FULL;
+
+ if ((rv = session_manager_allocate_session_fifos (smm, sm, &server_rx_fifo,
+ &server_tx_fifo,
+ &fifo_segment_index,
+ &segment_added)))
+ return rv;
+
+ if (segment_added && app->mode == APP_SERVER)
+ {
+ /* Send an API message to the external server, to map new segment */
+ ASSERT (app->cb_fns.add_segment_callback);
+
+ session_manager_get_segment_info (fifo_segment_index, &seg_name,
+ &seg_size);
+ if (app->cb_fns.add_segment_callback (app->api_client_index, seg_name,
+ seg_size))
+ return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
+ }
+
+ /* Create the session */
+ pool_get (smm->sessions[thread_index], s);
+ memset (s, 0, sizeof (*s));
+
+ /* Initialize backpointers */
+ pool_index = s - smm->sessions[thread_index];
+ server_rx_fifo->server_session_index = pool_index;
+ server_rx_fifo->server_thread_index = thread_index;
+
+ server_tx_fifo->server_session_index = pool_index;
+ server_tx_fifo->server_thread_index = thread_index;
+
+ s->server_rx_fifo = server_rx_fifo;
+ s->server_tx_fifo = server_tx_fifo;
+
+ /* Initialize state machine, such as it is... */
+ s->session_type = app->session_type;
+ s->session_state = SESSION_STATE_CONNECTING;
+ s->app_index = application_get_index (app);
+ s->server_segment_index = fifo_segment_index;
+ 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 = (((u64) thread_index) << 32) | (u64) s->session_index;
+ stream_session_table_add_for_tc (app->session_type, tc, value);
+
+ *ret_s = s;
+
+ 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 data Data to be enqueued
+ * @param len Length of data to be enqueued
+ * @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.
+ * @return Number of bytes enqueued or a negative value if enqueueing failed.
+ */
+int
+stream_session_enqueue_data (transport_connection_t * tc, u8 * data, u16 len,
+ u8 queue_event)
+{
+ stream_session_t *s;
+ int enqueued;
+
+ s = stream_session_get (tc->s_index, tc->thread_index);
+
+ /* Make sure there's enough space left. We might've filled the pipes */
+ if (PREDICT_FALSE (len > svm_fifo_max_enqueue (s->server_rx_fifo)))
+ return -1;
+
+ enqueued = svm_fifo_enqueue_nowait (s->server_rx_fifo, s->pid, len, data);
+
+ 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->c_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_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, s->pid, 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, s->pid, 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))
+ return 0;
+
+ /* Get session's server */
+ app = application_get (s->app_index);
+
+ /* Fabricate event */
+ evt.fifo = s->server_rx_fifo;
+ evt.event_type = FIFO_EVENT_SERVER_RX;
+ evt.event_id = serial_number++;
+ evt.enqueue_length = svm_fifo_max_dequeue (s->server_rx_fifo);
+
+ /* 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
+ return -1;
+
+ if (1)
+ {
+ ELOG_TYPE_DECLARE (e) =
+ {
+ .format = "evt-enqueue: id %d length %d",.format_args = "i4i4",};
+ struct
+ {
+ u32 data[2];
+ } *ed;
+ ed = ELOG_DATA (&vlib_global_main.elog_main, e);
+ ed->data[0] = evt.event_id;
+ ed->data[1] = evt.enqueue_length;
+ }
+
+ 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 (session_indices_to_enqueue[i], thread_index);
+ if (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;
+}
+
+/*
+ * Start listening on server's ip/port pair 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
+stream_session_start_listen (u32 server_index, ip46_address_t * ip, u16 port)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ stream_session_t *s;
+ transport_connection_t *tc;
+ application_t *srv;
+ u32 tci;
+
+ srv = application_get (server_index);
+
+ pool_get (smm->listen_sessions[srv->session_type], s);
+ memset (s, 0, sizeof (*s));
+
+ s->session_type = srv->session_type;
+ s->session_state = SESSION_STATE_LISTENING;
+ s->session_index = s - smm->listen_sessions[srv->session_type];
+ s->app_index = srv->index;
+
+ /* Transport bind/listen */
+ tci = tp_vfts[srv->session_type].bind (smm->vlib_main, s->session_index, ip,
+ port);
+
+ /* Attach transport to session */
+ s->connection_index = tci;
+ tc = tp_vfts[srv->session_type].get_listener (tci);
+
+ srv->session_index = s->session_index;
+
+ /* Add to the main lookup table */
+ stream_session_table_add_for_tc (s->session_type, tc, s->session_index);
+
+ return 0;
+}
+
+void
+stream_session_stop_listen (u32 server_index)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ stream_session_t *listener;
+ transport_connection_t *tc;
+ application_t *srv;
+
+ srv = application_get (server_index);
+ listener = pool_elt_at_index (smm->listen_sessions[srv->session_type],
+ srv->session_index);
+
+ tc = tp_vfts[srv->session_type].get_listener (listener->connection_index);
+ stream_session_table_del_for_tc (smm, listener->session_type, tc);
+
+ tp_vfts[srv->session_type].unbind (smm->vlib_main,
+ listener->connection_index);
+ pool_put (smm->listen_sessions[srv->session_type], listener);
+}
+
+int
+connect_server_add_segment_cb (application_t * ss, char *segment_name,
+ u32 segment_size)
+{
+ /* Does exactly nothing, but die */
+ ASSERT (0);
+ return 0;
+}
+
+void
+connects_session_manager_init (session_manager_main_t * smm, u8 session_type)
+{
+ session_manager_t *sm;
+ u32 connect_fifo_size = 8 << 10; /* Config? */
+ u32 default_segment_size = 1 << 20;
+
+ pool_get (smm->session_managers, sm);
+ memset (sm, 0, sizeof (*sm));
+
+ sm->add_segment_size = default_segment_size;
+ sm->rx_fifo_size = connect_fifo_size;
+ sm->tx_fifo_size = connect_fifo_size;
+ sm->add_segment = 1;
+
+ session_manager_add_segment (smm, sm);
+ smm->connect_manager_index[session_type] = sm - smm->session_managers;
+}
+
+void
+stream_session_connect_notify (transport_connection_t * tc, u8 sst,
+ u8 is_fail)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ application_t *app;
+ stream_session_t *new_s = 0;
+ u64 value;
+
+ value = stream_session_half_open_lookup (smm, &tc->lcl_ip, &tc->rmt_ip,
+ tc->lcl_port, tc->rmt_port,
+ tc->proto);
+ if (value == HALF_OPEN_LOOKUP_INVALID_VALUE)
+ {
+ clib_warning ("This can't be good!");
+ return;
+ }
+
+ app = application_get (value >> 32);
+
+ if (!is_fail)
+ {
+ /* Create new session (server segments are allocated if needed) */
+ if (stream_session_create_i (smm, app, tc, &new_s))
+ return;
+
+ app->session_index = stream_session_get_index (new_s);
+ app->thread_index = new_s->thread_index;
+
+ /* Allocate vpp event queue for this thread if needed */
+ vpp_session_event_queue_allocate (smm, tc->thread_index);
+ }
+
+ /* Notify client */
+ app->cb_fns.session_connected_callback (app->api_client_index, new_s,
+ is_fail);
+
+ /* Cleanup session lookup */
+ stream_session_half_open_table_del (smm, sst, tc);
+}
+
+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 associated app if needed.
+ */
+void
+stream_session_delete (stream_session_t * s)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ svm_fifo_segment_private_t *fifo_segment;
+ application_t *app;
+ int rv;
+
+ /* delete from the main lookup table */
+ rv = stream_session_table_del (smm, s);
+
+ if (rv)
+ clib_warning ("hash delete error, rv %d", rv);
+
+ /* Cleanup fifo segments */
+ fifo_segment = svm_fifo_get_segment (s->server_segment_index);
+ svm_fifo_segment_free_fifo (fifo_segment, s->server_rx_fifo);
+ svm_fifo_segment_free_fifo (fifo_segment, s->server_tx_fifo);
+
+ /* Cleanup app if client */
+ app = application_get (s->app_index);
+ if (app->mode == APP_CLIENT)
+ {
+ application_del (app);
+ }
+ else if (app->mode == APP_SERVER)
+ {
+ session_manager_t *sm;
+ svm_fifo_segment_private_t *fifo_segment;
+ svm_fifo_t **fifos;
+ u32 fifo_index;
+
+ sm = session_manager_get (app->session_manager_index);
+
+ /* Delete fifo */
+ fifo_segment = svm_fifo_get_segment (s->server_segment_index);
+ fifos = (svm_fifo_t **) fifo_segment->h->fifos;
+
+ fifo_index = svm_fifo_segment_index (fifo_segment);
+
+ /* Remove segment only if it holds no fifos and not the first */
+ if (sm->segment_indices[0] != fifo_index && vec_len (fifos) == 0)
+ svm_fifo_segment_delete (fifo_segment);
+ }
+
+ pool_put (smm->sessions[s->thread_index], s);
+}
+
+/**
+ * Notification from transport that connection is being deleted
+ *
+ * This 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;
+
+ s = stream_session_get_if_valid (tc->s_index, tc->thread_index);
+ if (!s)
+ {
+ clib_warning ("Surprised!");
+ 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)
+{
+ session_manager_main_t *smm = &session_manager_main;
+ application_t *server;
+ stream_session_t *s, *listener;
+
+ int rv;
+
+ /* Find the server */
+ listener = pool_elt_at_index (smm->listen_sessions[sst], listener_index);
+ server = application_get (listener->app_index);
+
+ if ((rv = stream_session_create_i (smm, server, tc, &s)))
+ return rv;
+
+ /* Allocate vpp event queue for this thread if needed */
+ vpp_session_event_queue_allocate (smm, tc->thread_index);
+
+ /* Shoulder-tap the server */
+ if (notify)
+ {
+ server->cb_fns.session_accept_callback (s);
+ }
+
+ return 0;
+}
+
+void
+stream_session_open (u8 sst, ip46_address_t * addr, u16 port_host_byte_order,
+ u32 app_index)
+{
+ transport_connection_t *tc;
+ u32 tci;
+ u64 value;
+
+ /* Ask transport to open connection */
+ tci = tp_vfts[sst].open (addr, port_host_byte_order);
+
+ /* Get transport connection */
+ tc = tp_vfts[sst].get_half_open (tci);
+
+ /* Store api_client_index and transport connection index */
+ value = (((u64) app_index) << 32) | (u64) tc->c_index;
+
+ /* Add to the half-open lookup table */
+ stream_session_half_open_table_add (sst, tc, value);
+}
+
+/**
+ * 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.
+ */
+void
+stream_session_disconnect (stream_session_t * s)
+{
+ tp_vfts[s->session_type].close (s->connection_index, s->thread_index);
+ s->session_state = SESSION_STATE_CLOSED;
+}
+
+/**
+ * Cleanup transport and session state.
+ */
+void
+stream_session_cleanup (stream_session_t * s)
+{
+ tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index);
+ stream_session_delete (s);
+}
+
+void
+session_register_transport (u8 type, const transport_proto_vft_t * vft)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+
+ vec_validate (tp_vfts, type);
+ tp_vfts[type] = *vft;
+
+ /* If an offset function is provided, then peek instead of dequeue */
+ smm->session_rx_fns[type] =
+ (vft->rx_fifo_offset) ? session_fifo_rx_peek : session_fifo_rx_dequeue;
+}
+
+transport_proto_vft_t *
+session_get_transport_vft (u8 type)
+{
+ if (type >= vec_len (tp_vfts))
+ return 0;
+ return &tp_vfts[type];
+}
+
+static clib_error_t *
+session_manager_main_init (vlib_main_t * vm)
+{
+ u32 num_threads;
+ vlib_thread_main_t *vtm = vlib_get_thread_main ();
+ session_manager_main_t *smm = &session_manager_main;
+ int i;
+
+ smm->vlib_main = vm;
+ smm->vnet_main = vnet_get_main ();
+
+ 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->fifo_events, num_threads - 1);
+ vec_validate (smm->evts_partially_read, num_threads - 1);
+ vec_validate (smm->current_enqueue_epoch, num_threads - 1);
+ vec_validate (smm->vpp_event_queues, num_threads - 1);
+
+ /* $$$$ preallocate hack config parameter */
+ for (i = 0; i < 200000; i++)
+ {
+ stream_session_t *ss;
+ pool_get (smm->sessions[0], ss);
+ memset (ss, 0, sizeof (*ss));
+ }
+
+ for (i = 0; i < 200000; i++)
+ pool_put_index (smm->sessions[0], i);
+
+ clib_bihash_init_16_8 (&smm->v4_session_hash, "v4 session table",
+ 200000 /* $$$$ config parameter nbuckets */ ,
+ (64 << 20) /*$$$ config parameter table size */ );
+ clib_bihash_init_48_8 (&smm->v6_session_hash, "v6 session table",
+ 200000 /* $$$$ config parameter nbuckets */ ,
+ (64 << 20) /*$$$ config parameter table size */ );
+
+ clib_bihash_init_16_8 (&smm->v4_half_open_hash, "v4 half-open table",
+ 200000 /* $$$$ config parameter nbuckets */ ,
+ (64 << 20) /*$$$ config parameter table size */ );
+ clib_bihash_init_48_8 (&smm->v6_half_open_hash, "v6 half-open table",
+ 200000 /* $$$$ config parameter nbuckets */ ,
+ (64 << 20) /*$$$ config parameter table size */ );
+
+ for (i = 0; i < SESSION_N_TYPES; i++)
+ smm->connect_manager_index[i] = INVALID_INDEX;
+
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (session_manager_main_init);
+
+/*
+ * 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 00000000000..cf14cca9f30
--- /dev/null
+++ b/src/vnet/session/session.h
@@ -0,0 +1,380 @@
+/*
+ * 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/transport.h>
+#include <vlibmemory/unix_shared_memory_queue.h>
+#include <vlibmemory/api.h>
+#include <vppinfra/sparse_vec.h>
+#include <svm/svm_fifo_segment.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_SERVER_RX,
+ FIFO_EVENT_SERVER_TX,
+ FIFO_EVENT_TIMEOUT,
+ FIFO_EVENT_SERVER_EXIT,
+} fifo_event_type_t;
+
+#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;
+
+#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_READY,
+ SESSION_STATE_CLOSED,
+ SESSION_STATE_N_STATES,
+} stream_session_state_t;
+
+typedef CLIB_PACKED (struct
+ {
+ svm_fifo_t * fifo;
+ u8 event_type;
+ /* $$$$ for event logging */
+ u16 event_id;
+ u32 enqueue_length;
+ }) session_fifo_event_t;
+
+typedef struct _stream_session_t
+{
+ /** Type */
+ u8 session_type;
+
+ /** State */
+ u8 session_state;
+
+ /** Session index in per_thread pool */
+ u32 session_index;
+
+ /** Transport specific */
+ u32 connection_index;
+
+ u8 thread_index;
+
+ /** Application specific */
+ u32 pid;
+
+ /** fifo pointers. Once allocated, these do not move */
+ svm_fifo_t *server_rx_fifo;
+ svm_fifo_t *server_tx_fifo;
+
+ /** To avoid n**2 "one event per frame" check */
+ u8 enqueue_epoch;
+
+ /** used during unbind processing */
+ u8 is_deleted;
+
+ /** stream server pool index */
+ u32 app_index;
+
+ /** svm segment index */
+ u32 server_segment_index;
+} stream_session_t;
+
+typedef struct _session_manager
+{
+ /** segments mapped by this server */
+ u32 *segment_indices;
+
+ /** Session fifo sizes. They are provided for binds and take default
+ * values for connects */
+ u32 rx_fifo_size;
+ u32 tx_fifo_size;
+
+ /** Configured additional segment size */
+ u32 add_segment_size;
+
+ /** Flag that indicates if additional segments should be created */
+ u8 add_segment;
+} session_manager_t;
+
+/* 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_fifo_rx_peek;
+extern session_fifo_rx_fn session_fifo_rx_dequeue;
+
+struct _session_manager_main
+{
+ /** 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;
+
+ /** 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 **evts_partially_read;
+
+ /** per-worker active event vectors */
+ session_fifo_event_t **fifo_events;
+
+ /** vpp fifo event queue */
+ unix_shared_memory_queue_t **vpp_event_queues;
+
+ /** Unique segment name counter */
+ u32 unique_segment_name_counter;
+
+ /* Connection manager used by incoming connects */
+ u32 connect_manager_index[SESSION_N_TYPES];
+
+ session_manager_t *session_managers;
+
+ /** Per transport rx function that can either dequeue or peek */
+ session_fifo_rx_fn *session_rx_fns[SESSION_N_TYPES];
+
+ /* Convenience */
+ vlib_main_t *vlib_main;
+ vnet_main_t *vnet_main;
+};
+
+extern session_manager_main_t session_manager_main;
+
+/*
+ * Session manager function
+ */
+always_inline session_manager_main_t *
+vnet_get_session_manager_main ()
+{
+ return &session_manager_main;
+}
+
+always_inline session_manager_t *
+session_manager_get (u32 index)
+{
+ return pool_elt_at_index (session_manager_main.session_managers, index);
+}
+
+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];
+}
+
+always_inline session_manager_t *
+connects_session_manager_get (session_manager_main_t * smm,
+ session_type_t session_type)
+{
+ return pool_elt_at_index (smm->session_managers,
+ smm->connect_manager_index[session_type]);
+}
+
+void session_manager_get_segment_info (u32 index, u8 ** name, u32 * size);
+int session_manager_flush_enqueue_events (u32 thread_index);
+int
+session_manager_add_first_segment (session_manager_main_t * smm,
+ session_manager_t * sm, u32 segment_size,
+ u8 ** segment_name);
+void
+session_manager_del (session_manager_main_t * smm, session_manager_t * sm);
+void
+connects_session_manager_init (session_manager_main_t * smm, u8 session_type);
+
+/*
+ * Stream session functions
+ */
+
+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,
+ u32 thread_index);
+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, u32 thread_index);
+transport_connection_t
+ * stream_session_lookup_transport4 (session_manager_main_t * smm,
+ 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_transport6 (session_manager_main_t * smm,
+ ip6_address_t * lcl,
+ ip6_address_t * rmt, u16 lcl_port,
+ u16 rmt_port, u8 proto,
+ u32 thread_index);
+stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
+ u16 lcl_port, u8 proto);
+
+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);
+}
+
+always_inline stream_session_t *
+stream_session_get (u64 si, u32 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;
+
+ return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
+}
+
+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_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);
+}
+
+int
+stream_session_enqueue_data (transport_connection_t * tc, u8 * data, u16 len,
+ u8 queue_event);
+u32
+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);
+
+void
+stream_session_connect_notify (transport_connection_t * tc, u8 sst,
+ u8 is_fail);
+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);
+void stream_session_open (u8 sst, ip46_address_t * addr,
+ u16 port_host_byte_order, u32 api_client_index);
+void stream_session_disconnect (stream_session_t * s);
+void stream_session_cleanup (stream_session_t * s);
+int
+stream_session_start_listen (u32 server_index, ip46_address_t * ip, u16 port);
+void stream_session_stop_listen (u32 server_index);
+
+u8 *format_stream_session (u8 * s, va_list * args);
+
+void session_register_transport (u8 type, const transport_proto_vft_t * vft);
+transport_proto_vft_t *session_get_transport_vft (u8 type);
+
+#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 100644
index 00000000000..9d068684636
--- /dev/null
+++ b/src/vnet/session/session_api.c
@@ -0,0 +1,821 @@
+/*
+ * 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) \
+_(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) \
+_(DISCONNECT_SOCK, disconnect_sock) \
+_(DISCONNECT_SOCK_REPLY, disconnect_sock_reply) \
+_(ACCEPT_SOCK_REPLY, accept_sock_reply) \
+_(RESET_SOCK_REPLY, reset_sock_reply) \
+
+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_uri_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);
+
+ 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));
+ mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
+
+ /* Note: session_type is the first octet in all types of sessions */
+
+ mp->accept_cookie = server->accept_cookie;
+ mp->server_rx_fifo = (u64) s->server_rx_fifo;
+ mp->server_tx_fifo = (u64) s->server_tx_fifo;
+ mp->session_thread_index = s->thread_index;
+ mp->session_index = s->session_index;
+ mp->session_type = s->session_type;
+ mp->vpp_event_queue_address = (u64) vpp_queue;
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+
+ return 0;
+}
+
+static void
+send_session_disconnect_uri_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->session_thread_index = s->thread_index;
+ mp->session_index = s->session_index;
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+}
+
+static int
+send_session_connected_uri_callback (u32 api_client_index,
+ stream_session_t * s, u8 is_fail)
+{
+ vl_api_connect_uri_reply_t *mp;
+ unix_shared_memory_queue_t *q;
+ application_t *app = application_lookup (api_client_index);
+ u8 *seg_name;
+ unix_shared_memory_queue_t *vpp_queue;
+
+ 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_URI_REPLY);
+ mp->context = app->api_context;
+ mp->retval = is_fail;
+ if (!is_fail)
+ {
+ vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
+ mp->server_rx_fifo = (u64) s->server_rx_fifo;
+ mp->server_tx_fifo = (u64) s->server_tx_fifo;
+ mp->session_thread_index = s->thread_index;
+ mp->session_index = s->session_index;
+ mp->session_type = s->session_type;
+ mp->vpp_event_queue_address = (u64) vpp_queue;
+ mp->client_event_queue_address = (u64) app->event_queue;
+
+ session_manager_get_segment_info (s->server_segment_index, &seg_name,
+ &mp->segment_size);
+ mp->segment_name_length = vec_len (seg_name);
+ if (mp->segment_name_length)
+ clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
+ }
+
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+
+ /* Remove client if connect failed */
+ if (is_fail)
+ application_del (app);
+
+ 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_uri_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;
+ 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 = (u64) client_q;
+
+ /*
+ * 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 u64
+make_session_handle (stream_session_t * s)
+{
+ return (u64) s->session_index << 32 | (u64) s->thread_index;
+}
+
+static int
+send_session_accept_callback (stream_session_t * s)
+{
+ vl_api_accept_sock_t *mp;
+ unix_shared_memory_queue_t *q, *vpp_queue;
+ application_t *server = application_get (s->app_index);
+
+ 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));
+ mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SOCK);
+
+ /* Note: session_type is the first octet in all types of sessions */
+
+ mp->accept_cookie = server->accept_cookie;
+ mp->server_rx_fifo = (u64) s->server_rx_fifo;
+ mp->server_tx_fifo = (u64) s->server_tx_fifo;
+ mp->handle = make_session_handle (s);
+ mp->vpp_event_queue_address = (u64) vpp_queue;
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+
+ return 0;
+}
+
+static int
+send_session_connected_callback (u32 api_client_index, stream_session_t * s,
+ u8 is_fail)
+{
+ vl_api_connect_sock_reply_t *mp;
+ unix_shared_memory_queue_t *q;
+ application_t *app = application_lookup (api_client_index);
+ u8 *seg_name;
+ unix_shared_memory_queue_t *vpp_queue;
+
+ 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_SOCK_REPLY);
+ mp->context = app->api_context;
+ mp->retval = is_fail;
+ if (!is_fail)
+ {
+ vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
+ mp->server_rx_fifo = (u64) s->server_rx_fifo;
+ mp->server_tx_fifo = (u64) s->server_tx_fifo;
+ mp->handle = make_session_handle (s);
+ mp->vpp_event_queue_address = (u64) vpp_queue;
+ mp->client_event_queue_address = (u64) app->event_queue;
+
+ session_manager_get_segment_info (s->server_segment_index, &seg_name,
+ &mp->segment_size);
+ mp->segment_name_length = vec_len (seg_name);
+ if (mp->segment_name_length)
+ clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
+ }
+
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+
+ /* Remove client if connect failed */
+ if (is_fail)
+ application_del (app);
+
+ return 0;
+}
+
+static void
+send_session_disconnect_callback (stream_session_t * s)
+{
+ vl_api_disconnect_sock_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_SOCK);
+
+ mp->handle = make_session_handle (s);
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+}
+
+/**
+ * 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_sock_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;
+ 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 = (u64) client_q;
+
+ /*
+ * 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_uri_callback,
+ .session_disconnect_callback = send_session_disconnect_uri_callback,
+ .session_connected_callback = send_session_connected_uri_callback,
+ .add_segment_callback = send_add_segment_callback,
+ .redirect_connect_callback = redirect_connect_uri_callback
+};
+
+static session_cb_vft_t session_cb_vft = {
+ .session_accept_callback = send_session_accept_callback,
+ .session_disconnect_callback = send_session_disconnect_callback,
+ .session_connected_callback = send_session_connected_callback,
+ .add_segment_callback = send_add_segment_callback,
+ .redirect_connect_callback = redirect_connect_callback
+};
+
+static int
+api_session_not_valid (u32 session_index, u32 thread_index)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ stream_session_t *pool;
+
+ 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;
+}
+
+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;
+ char segment_name[128];
+ u32 segment_name_length;
+ int rv;
+
+ _Static_assert (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
+ sizeof (mp->options),
+ "Out of options, fix api message definition");
+
+ segment_name_length = ARRAY_LEN (segment_name);
+
+ memset (a, 0, sizeof (*a));
+
+ a->uri = (char *) mp->uri;
+ a->api_client_index = mp->client_index;
+ a->options = mp->options;
+ a->segment_name = segment_name;
+ a->segment_name_length = segment_name_length;
+ a->session_cb_vft = &uri_session_cb_vft;
+
+ a->options[SESSION_OPTIONS_SEGMENT_SIZE] = mp->initial_segment_size;
+ a->options[SESSION_OPTIONS_ACCEPT_COOKIE] = mp->accept_cookie;
+ rv = vnet_bind_uri (a);
+
+ /* *INDENT-OFF* */
+ REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
+ rmp->retval = rv;
+ if (!rv)
+ {
+ rmp->segment_name_length = 0;
+ /* $$$$ policy? */
+ rmp->segment_size = mp->initial_segment_size;
+ if (segment_name_length)
+ {
+ memcpy (rmp->segment_name, segment_name, segment_name_length);
+ rmp->segment_name_length = segment_name_length;
+ }
+ rmp->server_event_queue_address = a->server_event_queue_address;
+ }
+ }));
+ /* *INDENT-ON* */
+
+}
+
+static void
+vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
+{
+ vl_api_unbind_uri_reply_t *rmp;
+ int rv;
+
+ rv = vnet_unbind_uri ((char *) mp->uri, mp->client_index);
+
+ REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
+}
+
+static void
+vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
+{
+ vnet_connect_args_t _a, *a = &_a;
+
+ a->uri = (char *) mp->uri;
+ a->api_client_index = mp->client_index;
+ a->api_context = mp->context;
+ a->options = mp->options;
+ a->session_cb_vft = &uri_session_cb_vft;
+ a->mp = mp;
+ vnet_connect_uri (a);
+}
+
+static void
+vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
+{
+ vl_api_disconnect_session_reply_t *rmp;
+ int rv;
+
+ rv = api_session_not_valid (mp->session_index, mp->session_thread_index);
+ if (!rv)
+ rv = vnet_disconnect_session (mp->client_index, mp->session_index,
+ mp->session_thread_index);
+
+ REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
+}
+
+static void
+vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
+ mp)
+{
+ if (api_session_not_valid (mp->session_index, mp->session_thread_index))
+ {
+ clib_warning ("Invalid session!");
+ return;
+ }
+
+ /* 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 */
+ vnet_disconnect_session (mp->client_index, mp->session_index,
+ mp->session_thread_index);
+}
+
+static void
+vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
+{
+ stream_session_t *s;
+
+ if (api_session_not_valid (mp->session_index, mp->session_thread_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;
+ }
+
+ s = stream_session_get (mp->session_index, mp->session_thread_index);
+
+ /* 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;
+ int rv;
+
+ if (api_session_not_valid (mp->session_index, mp->session_thread_index))
+ return;
+
+ s = stream_session_get (mp->session_index, mp->session_thread_index);
+ rv = mp->retval;
+
+ if (rv)
+ {
+ /* Server isn't interested, kill the session */
+ stream_session_disconnect (s);
+ return;
+ }
+
+ 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;
+ char segment_name[128];
+ u32 segment_name_length;
+ int rv;
+
+ STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
+ sizeof (mp->options),
+ "Out of options, fix api message definition");
+
+ segment_name_length = ARRAY_LEN (segment_name);
+
+ memset (a, 0, sizeof (*a));
+
+ clib_memcpy (&a->tep.ip, mp->ip,
+ (mp->is_ip4 ? sizeof (ip4_address_t) :
+ sizeof (ip6_address_t)));
+ a->tep.is_ip4 = mp->is_ip4;
+ a->tep.port = mp->port;
+ a->tep.vrf = mp->vrf;
+
+ a->api_client_index = mp->client_index;
+ a->options = mp->options;
+ a->segment_name = segment_name;
+ a->segment_name_length = segment_name_length;
+ a->session_cb_vft = &session_cb_vft;
+
+ rv = vnet_bind_uri (a);
+
+ /* *INDENT-OFF* */
+ REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY, ({
+ rmp->retval = rv;
+ if (!rv)
+ {
+ rmp->segment_name_length = 0;
+ rmp->segment_size = mp->options[SESSION_OPTIONS_SEGMENT_SIZE];
+ if (segment_name_length)
+ {
+ memcpy(rmp->segment_name, segment_name, segment_name_length);
+ rmp->segment_name_length = segment_name_length;
+ }
+ rmp->server_event_queue_address = a->server_event_queue_address;
+ }
+ }));
+ /* *INDENT-ON* */
+}
+
+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;
+ int rv;
+
+ a->api_client_index = mp->client_index;
+ a->handle = mp->handle;
+
+ rv = vnet_unbind (a);
+
+ REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
+}
+
+static void
+vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
+{
+ vnet_connect_args_t _a, *a = &_a;
+
+ clib_memcpy (&a->tep.ip, mp->ip,
+ (mp->is_ip4 ? sizeof (ip4_address_t) :
+ sizeof (ip6_address_t)));
+ a->tep.is_ip4 = mp->is_ip4;
+ a->tep.port = mp->port;
+ a->tep.vrf = mp->vrf;
+ a->options = mp->options;
+ a->session_cb_vft = &session_cb_vft;
+ a->api_context = mp->context;
+ a->mp = mp;
+
+ vnet_connect (a);
+}
+
+static void
+vl_api_disconnect_sock_t_handler (vl_api_disconnect_sock_t * mp)
+{
+ vnet_disconnect_args_t _a, *a = &_a;
+ vl_api_disconnect_sock_reply_t *rmp;
+ int rv;
+
+ a->api_client_index = mp->client_index;
+ a->handle = mp->handle;
+ rv = vnet_disconnect (a);
+
+ REPLY_MACRO (VL_API_DISCONNECT_SOCK_REPLY);
+}
+
+static void
+vl_api_disconnect_sock_reply_t_handler (vl_api_disconnect_sock_reply_t * mp)
+{
+ vnet_disconnect_args_t _a, *a = &_a;
+
+ /* Client objected to disconnecting the session, log and continue */
+ if (mp->retval)
+ {
+ clib_warning ("client retval %d", mp->retval);
+ return;
+ }
+
+ a->api_client_index = mp->client_index;
+ a->handle = mp->handle;
+
+ vnet_disconnect (a);
+}
+
+static void
+vl_api_reset_sock_reply_t_handler (vl_api_reset_sock_reply_t * mp)
+{
+ stream_session_t *s;
+ u32 session_index, thread_index;
+
+ /* Client objected to resetting the session, log and continue */
+ if (mp->retval)
+ {
+ clib_warning ("client retval %d", mp->retval);
+ return;
+ }
+
+ if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
+ {
+ clib_warning ("Invalid handle");
+ return;
+ }
+
+ s = stream_session_get (session_index, thread_index);
+
+ /* 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_sock_reply_t_handler (vl_api_accept_sock_reply_t * mp)
+{
+ stream_session_t *s;
+ u32 session_index, thread_index;
+
+ if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
+ {
+ clib_warning ("Invalid handle");
+ return;
+ }
+ s = stream_session_get (session_index, thread_index);
+
+ if (mp->retval)
+ {
+ /* Server isn't interested, kill the session */
+ stream_session_disconnect (s);
+ return;
+ }
+
+ s->session_state = SESSION_STATE_READY;
+}
+
+#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 100644
index 00000000000..b2943a1ccc5
--- /dev/null
+++ b/src/vnet/session/session_cli.c
@@ -0,0 +1,189 @@
+/*
+ * 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>
+
+/**
+ * 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)
+ str = format (0, "%-20llp%-20llp%-15lld", ss->server_rx_fifo,
+ ss->server_tx_fifo, stream_session_get_index (ss));
+
+ if (ss->session_state == SESSION_STATE_READY)
+ {
+ s = format (s, "%-40U%v", tp_vft->format_connection,
+ ss->connection_index, ss->thread_index, str);
+ }
+ 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_READY)
+ {
+ s =
+ format (s, "%-40U%v", tp_vft->format_half_open, ss->connection_index,
+ str);
+ }
+ else if (ss->session_state == SESSION_STATE_CLOSED)
+ {
+ s = format (s, "[CL] %-40U%v", tp_vft->format_connection,
+ ss->connection_index, ss->thread_index, str);
+ }
+ else
+ {
+ clib_warning ("Session in unknown state!");
+ }
+
+ vec_free (str);
+
+ return s;
+}
+
+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;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "verbose"))
+ verbose = 1;
+ else
+ break;
+ }
+
+ 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)
+ {
+ str = format (str, "%-40s%-20s%-20s%-15s",
+ "Connection", "Rx fifo", "Tx fifo",
+ "Session Index");
+ vlib_cli_output (vm, "%v", str);
+ vec_reset_length (str);
+ once_per_pool = 0;
+ }
+
+ /* *INDENT-OFF* */
+ pool_foreach (s, pool,
+ ({
+ vlib_cli_output (vm, "%U", format_stream_session, s, verbose);
+ }));
+ /* *INDENT-ON* */
+ }
+ }
+ else
+ vlib_cli_output (vm, "Thread %d: no active sessions", i);
+ }
+ vec_free (str);
+
+ return 0;
+}
+
+VLIB_CLI_COMMAND (show_uri_command, static) =
+{
+.path = "show session",.short_help = "show session [verbose]",.function =
+ show_session_command_fn,};
+
+
+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;
+ u32 session_index = ~0;
+ stream_session_t *pool, *session;
+ application_t *server;
+
+ 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
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+
+ if (session_index == ~0)
+ return clib_error_return (0, "session <nn> required, but not set.");
+
+ if (thread_index > vec_len (smm->sessions))
+ return clib_error_return (0, "thread %d out of range [0-%d]",
+ thread_index, vec_len (smm->sessions));
+
+ pool = smm->sessions[thread_index];
+
+ if (pool_is_free_index (pool, session_index))
+ return clib_error_return (0, "session %d not active", session_index);
+
+ session = pool_elt_at_index (pool, session_index);
+ server = application_get (session->app_index);
+
+ /* Disconnect both app and transport */
+ server->cb_fns.session_disconnect_callback (session);
+
+ return 0;
+}
+
+VLIB_CLI_COMMAND (clear_uri_session_command, static) =
+{
+.path = "clear session",.short_help =
+ "clear session thread <thread> session <index>",.function =
+ clear_session_command_fn,};
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/session/transport.c b/src/vnet/session/transport.c
new file mode 100644
index 00000000000..abd94ba4f1d
--- /dev/null
+++ b/src/vnet/session/transport.c
@@ -0,0 +1,64 @@
+/*
+ * 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.h>
+
+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);
+}
+
+
+
diff --git a/src/vnet/session/transport.h b/src/vnet/session/transport.h
new file mode 100644
index 00000000000..2d4415ba6a3
--- /dev/null
+++ b/src/vnet/session/transport.h
@@ -0,0 +1,250 @@
+/*
+ * 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>
+
+/*
+ * 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 proto; /**< Transport protocol id */
+
+ u32 s_index; /**< Parent session index */
+ u32 c_index; /**< Connection index in transport pool */
+ u8 is_ip4; /**< Flag if IP4 connection */
+ u32 thread_index; /**< Worker-thread index */
+
+ /** 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_proto connection.proto
+#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
+} transport_connection_t;
+
+/*
+ * Transport protocol virtual function table
+ */
+typedef struct _transport_proto_vft
+{
+ /*
+ * Setup
+ */
+ u32 (*bind) (vlib_main_t *, u32, ip46_address_t *, u16);
+ u32 (*unbind) (vlib_main_t *, u32);
+ int (*open) (ip46_address_t * addr, u16 port_host_byte_order);
+ 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 (*rx_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;
+
+/* 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; u8 unused_for_now[8];
+ }; u64 as_u64[6];
+ };
+ }) v6_connection_key_t;
+
+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;
+ memset (&key, 0, sizeof (v4_connection_key_t));
+
+ 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->key[0] = key.as_u64[0];
+ kv->key[1] = key.as_u64[1];
+ 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;
+ memset (&key, 0, sizeof (v4_connection_key_t));
+
+ 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->key[0] = key.as_u64[0];
+ kv->key[1] = key.as_u64[1];
+ kv->value = ~0ULL;
+}
+
+always_inline void
+make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
+{
+ return make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port,
+ t->rmt_port, t->proto);
+}
+
+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;
+ memset (&key, 0, sizeof (v6_connection_key_t));
+
+ 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;
+
+ kv->key[0] = key.as_u64[0];
+ kv->key[1] = key.as_u64[1];
+ 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;
+ memset (&key, 0, sizeof (v6_connection_key_t));
+
+ 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;
+
+ kv->key[0] = key.as_u64[0];
+ kv->key[1] = key.as_u64[1];
+ 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, t->proto);
+}
+
+typedef struct _transport_endpoint
+{
+ ip46_address_t ip;
+ u16 port;
+ u8 is_ip4;
+ u32 vrf;
+} transport_endpoint_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);
+
+#endif /* VNET_VNET_URI_TRANSPORT_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */