+/**
+ * Register transport virtual function table.
+ *
+ * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
+ * @param vft - virtual function table for transport proto
+ * @param fib_proto - network layer protocol
+ * @param output_node - output node index that session layer will hand off
+ * buffers to, for requested fib proto
+ */
+void
+transport_register_protocol (transport_proto_t transport_proto,
+ const transport_proto_vft_t * vft,
+ fib_protocol_t fib_proto, u32 output_node)
+{
+ u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
+
+ vec_validate (tp_vfts, transport_proto);
+ tp_vfts[transport_proto] = *vft;
+
+ session_register_transport (transport_proto, vft, is_ip4, output_node);
+}
+
+/**
+ * Get transport virtual function table
+ *
+ * @param type - session type (not protocol type)
+ */
+transport_proto_vft_t *
+transport_protocol_get_vft (transport_proto_t transport_proto)
+{
+ if (transport_proto >= vec_len (tp_vfts))
+ return 0;
+ return &tp_vfts[transport_proto];
+}
+
+transport_service_type_t
+transport_protocol_service_type (transport_proto_t tp)
+{
+ return tp_vfts[tp].service_type;
+}
+
+transport_tx_fn_type_t
+transport_protocol_tx_fn_type (transport_proto_t tp)
+{
+ return tp_vfts[tp].tx_type;
+}
+
+void
+transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
+{
+ tp_vfts[tp].cleanup (conn_index, thread_index);
+}
+
+int
+transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
+{
+ return tp_vfts[tp].connect (tep);
+}
+
+void
+transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
+{
+ tp_vfts[tp].close (conn_index, thread_index);
+}
+
+u32
+transport_start_listen (transport_proto_t tp, u32 session_index,
+ transport_endpoint_t * tep)
+{
+ return tp_vfts[tp].start_listen (session_index, tep);
+}
+
+u32
+transport_stop_listen (transport_proto_t tp, u32 conn_index)
+{
+ return tp_vfts[tp].stop_listen (conn_index);
+}
+
+u8
+transport_protocol_is_cl (transport_proto_t tp)
+{
+ return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
+}
+
+always_inline void
+default_get_transport_endpoint (transport_connection_t * tc,
+ transport_endpoint_t * tep, u8 is_lcl)
+{
+ if (is_lcl)
+ {
+ tep->port = tc->lcl_port;
+ tep->is_ip4 = tc->is_ip4;
+ clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
+ }
+ else
+ {
+ tep->port = tc->rmt_port;
+ tep->is_ip4 = tc->is_ip4;
+ clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
+ }
+}
+
+void
+transport_get_endpoint (transport_proto_t tp, u32 conn_index,
+ u32 thread_index, transport_endpoint_t * tep,
+ u8 is_lcl)
+{
+ if (tp_vfts[tp].get_transport_endpoint)
+ tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
+ is_lcl);
+ else
+ {
+ transport_connection_t *tc;
+ tc = transport_get_connection (tp, conn_index, thread_index);
+ default_get_transport_endpoint (tc, tep, is_lcl);
+ }
+}
+
+void
+transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
+ transport_endpoint_t * tep, u8 is_lcl)
+{
+ if (tp_vfts[tp].get_transport_listener_endpoint)
+ tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
+ else
+ {
+ transport_connection_t *tc;
+ tc = transport_get_listener (tp, conn_index);
+ default_get_transport_endpoint (tc, tep, is_lcl);
+ }
+}
+
+#define PORT_MASK ((1 << 16)- 1)
+
+void
+transport_endpoint_del (u32 tepi)
+{
+ clib_spinlock_lock_if_init (&local_endpoints_lock);
+ pool_put_index (local_endpoints, tepi);
+ clib_spinlock_unlock_if_init (&local_endpoints_lock);
+}
+
+always_inline transport_endpoint_t *
+transport_endpoint_new (void)
+{
+ transport_endpoint_t *tep;
+ pool_get_zero (local_endpoints, tep);
+ return tep;
+}
+
+void
+transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
+{
+ u32 tepi;
+ transport_endpoint_t *tep;
+
+ /* Cleanup local endpoint if this was an active connect */
+ tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
+ clib_net_to_host_u16 (port));
+ if (tepi != ENDPOINT_INVALID_INDEX)
+ {
+ tep = pool_elt_at_index (local_endpoints, tepi);
+ transport_endpoint_table_del (&local_endpoints_table, proto, tep);
+ transport_endpoint_del (tepi);
+ }
+}
+
+static void
+transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
+{
+ transport_endpoint_t *tep;
+ clib_spinlock_lock_if_init (&local_endpoints_lock);
+ tep = transport_endpoint_new ();
+ clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
+ tep->port = port;
+ transport_endpoint_table_add (&local_endpoints_table, proto, tep,
+ tep - local_endpoints);
+ clib_spinlock_unlock_if_init (&local_endpoints_lock);
+}
+
+/**
+ * Allocate local port and add if successful add entry to local endpoint
+ * table to mark the pair as used.
+ */
+int
+transport_alloc_local_port (u8 proto, ip46_address_t * ip)
+{
+ u16 min = 1024, max = 65535; /* XXX configurable ? */
+ int tries, limit;
+ u32 tei;
+
+ limit = max - min;
+
+ /* Only support active opens from thread 0 */
+ ASSERT (vlib_get_thread_index () == 0);
+
+ /* Search for first free slot */
+ for (tries = 0; tries < limit; tries++)
+ {
+ u16 port = 0;
+
+ /* Find a port in the specified range */
+ while (1)
+ {
+ port = random_u32 (&port_allocator_seed) & PORT_MASK;
+ if (PREDICT_TRUE (port >= min && port < max))
+ break;
+ }
+
+ /* Look it up. If not found, we're done */
+ tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
+ port);
+ if (tei == ENDPOINT_INVALID_INDEX)
+ {
+ transport_endpoint_mark_used (proto, ip, port);
+ return port;
+ }
+ }
+ return -1;
+}
+
+static clib_error_t *
+transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
+{
+ if (is_ip4)
+ {
+ ip4_address_t *ip4;
+ ip4 = ip_interface_get_first_ip (sw_if_index, 1);
+ if (!ip4)
+ return clib_error_return (0, "no routable ip4 address on %U",
+ format_vnet_sw_if_index_name,
+ vnet_get_main (), sw_if_index);
+ addr->ip4.as_u32 = ip4->as_u32;
+ }
+ else
+ {
+ ip6_address_t *ip6;
+ ip6 = ip_interface_get_first_ip (sw_if_index, 0);
+ if (ip6 == 0)
+ return clib_error_return (0, "no routable ip6 addresses on %U",
+ format_vnet_sw_if_index_name,
+ vnet_get_main (), sw_if_index);
+ clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
+ }
+ return 0;
+}