session: make alpn more generic for transports 61/43861/2
authorMatus Fabian <[email protected]>
Wed, 8 Oct 2025 10:00:02 +0000 (06:00 -0400)
committerFlorin Coras <[email protected]>
Wed, 8 Oct 2025 16:22:57 +0000 (16:22 +0000)
- move all type definitions from tls_types to transport types
- move hash table for alpn proto reverse lookup to transport_main
- add get_alpn_selected to transport_proto_vft_t
- add transport_get_alpn_selected as common function for
  negotiated alpn proto retrieval

Type: refactor

Change-Id: I8bde0844a6263f6ba023837b405c5b66c4781955
Signed-off-by: Matus Fabian <[email protected]>
15 files changed:
src/plugins/hs_apps/alpn_client.c
src/plugins/hs_apps/alpn_server.c
src/plugins/hs_apps/http_cli.c
src/plugins/hs_apps/http_client.c
src/plugins/hs_apps/http_connect_proxy_client.c
src/plugins/hs_apps/proxy.c
src/plugins/http/http.c
src/plugins/http_static/static_server.c
src/vnet/CMakeLists.txt
src/vnet/session/transport.c
src/vnet/session/transport.h
src/vnet/session/transport_types.h
src/vnet/tls/tls.c
src/vnet/tls/tls.h
src/vnet/tls/tls_types.h [deleted file]

index 8f744e1..6195e94 100644 (file)
@@ -5,7 +5,6 @@
 #include <vnet/session/application_interface.h>
 #include <vnet/session/application.h>
 #include <vnet/session/session.h>
-#include <vnet/tls/tls_types.h>
 
 typedef struct
 {
@@ -63,7 +62,8 @@ ac_ts_connected_callback (u32 app_index, u32 api_context, session_t *s,
       return -1;
     }
 
-  cm->alpn_proto_selected = tls_get_alpn_selected (s->connection_index);
+  cm->alpn_proto_selected = transport_get_alpn_selected (
+    session_get_transport_proto (s), s->connection_index, s->thread_index);
 
   a->handle = session_handle (s);
   a->app_index = cm->app_index;
index ba0cc14..5ac82da 100644 (file)
@@ -4,7 +4,6 @@
 
 #include <vnet/session/application_interface.h>
 #include <vnet/session/session.h>
-#include <vnet/tls/tls_types.h>
 
 typedef struct
 {
@@ -40,7 +39,8 @@ as_ts_accept_callback (session_t *ts)
 
   ts->session_state = SESSION_STATE_READY;
 
-  alpn_proto = tls_get_alpn_selected (ts->connection_index);
+  alpn_proto = transport_get_alpn_selected (
+    session_get_transport_proto (ts), ts->connection_index, ts->thread_index);
   clib_warning ("ALPN selected: %U", format_tls_alpn_proto, alpn_proto);
 
   return 0;
index fdc549f..127fffb 100644 (file)
@@ -16,7 +16,6 @@
 #include <vnet/session/application.h>
 #include <vnet/session/application_interface.h>
 #include <vnet/session/session.h>
-#include <vnet/tls/tls_types.h>
 #include <http/http.h>
 #include <http/http_header_names.h>
 #include <http/http_content_types.h>
index 8a047fc..36ee5fb 100644 (file)
@@ -10,7 +10,6 @@
 #include <http/http_content_types.h>
 #include <http/http_status_codes.h>
 #include <vppinfra/unix.h>
-#include <vnet/tls/tls_types.h>
 
 #define foreach_hc_s_flag                                                     \
   _ (1, IS_CLOSED)                                                            \
index c063243..fa850ec 100644 (file)
@@ -7,7 +7,6 @@
 #include <vnet/session/session.h>
 #include <http/http.h>
 #include <http/http_status_codes.h>
-#include <vnet/tls/tls_types.h>
 #include <vnet/tcp/tcp.h>
 #include <vppinfra/tw_timer_2t_1w_2048sl.h>
 
index 7d4e2f0..5bbac45 100644 (file)
@@ -20,7 +20,6 @@
 #include <hs_apps/proxy.h>
 #include <vnet/tcp/tcp.h>
 #include <http/http_header_names.h>
-#include <vnet/tls/tls_types.h>
 
 proxy_main_t proxy_main;
 
index 71baccf..0ef19a5 100644 (file)
@@ -16,7 +16,6 @@
 #include <vpp/app/version.h>
 #include <vnet/session/application_interface.h>
 #include <vnet/session/application.h>
-#include <vnet/tls/tls_types.h>
 
 #include <http/http.h>
 #include <http/http_private.h>
@@ -457,7 +456,8 @@ http_ts_accept_callback (session_t *ts)
   tp = session_get_transport_proto (ts);
   if (tp == TRANSPORT_PROTO_TLS)
     {
-      alpn_proto = tls_get_alpn_selected (ts->connection_index);
+      alpn_proto = transport_get_alpn_selected (tp, ts->connection_index,
+                                               ts->thread_index);
       HTTP_DBG (1, "ALPN selected: %U", format_tls_alpn_proto, alpn_proto);
       switch (alpn_proto)
        {
@@ -544,7 +544,8 @@ http_ts_connected_callback (u32 http_app_index, u32 ho_hc_index, session_t *ts,
   /* TLS set by ALPN result, TCP: prior knowledge (set in ho) */
   if (tp == TRANSPORT_PROTO_TLS)
     {
-      alpn_proto = tls_get_alpn_selected (ts->connection_index);
+      alpn_proto = transport_get_alpn_selected (tp, ts->connection_index,
+                                               ts->thread_index);
       HTTP_DBG (1, "ALPN selected: %U", format_tls_alpn_proto, alpn_proto);
       switch (alpn_proto)
        {
index 25606ec..47c6cc0 100644 (file)
@@ -15,7 +15,6 @@
 
 #include <http_static/http_static.h>
 #include <vnet/session/application.h>
-#include <vnet/tls/tls_types.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
index 891ed71..8d62922 100644 (file)
@@ -1028,7 +1028,6 @@ list(APPEND VNET_HEADERS
   tls/tls_inlines.h
   tls/tls_record.h
   tls/tls_test.h
-  tls/tls_types.h
 )
 
 
index fa3106a..e85eec4 100644 (file)
@@ -40,6 +40,7 @@ typedef struct transport_main_
   u16 port_allocator_max_src_port;
   u8 lcl_endpts_cleanup_pending;
   clib_spinlock_t local_endpoints_lock;
+  uword *alpn_proto_by_str;
 } transport_main_t;
 
 static transport_main_t tp_main;
@@ -470,6 +471,16 @@ transport_connection_attribute (transport_proto_t tp, u32 conn_index,
   return tp_vfts[tp].attribute (conn_index, thread_index, is_get, attr);
 }
 
+tls_alpn_proto_t
+transport_get_alpn_selected (transport_proto_t tp, u32 conn_index,
+                            clib_thread_index_t thread_index)
+{
+  if (!tp_vfts[tp].get_alpn_selected)
+    return TLS_ALPN_PROTO_NONE;
+
+  return tp_vfts[tp].get_alpn_selected (conn_index, thread_index);
+}
+
 #define PORT_MASK ((1 << 16)- 1)
 
 void
@@ -989,6 +1000,53 @@ transport_connection_reschedule (transport_connection_t * tc)
     }
 }
 
+tls_alpn_proto_t
+tls_alpn_proto_by_str (tls_alpn_proto_id_t *alpn_id)
+{
+  transport_main_t *tm = &tp_main;
+  uword *p;
+
+  p = hash_get_mem (tm->alpn_proto_by_str, alpn_id);
+  if (p)
+    return p[0];
+
+  return TLS_ALPN_PROTO_NONE;
+}
+
+u8 *
+format_tls_alpn_proto (u8 *s, va_list *args)
+{
+  tls_alpn_proto_t alpn_proto = va_arg (*args, int);
+  u8 *t = 0;
+
+  switch (alpn_proto)
+    {
+#define _(sym, str)                                                           \
+  case TLS_ALPN_PROTO_##sym:                                                  \
+    t = (u8 *) str;                                                           \
+    break;
+      foreach_tls_alpn_protos
+#undef _
+       default : return format (s, "BUG: unknown");
+    }
+  return format (s, "%s", t);
+}
+
+static uword
+tls_alpn_proto_hash_key_sum (hash_t *h, uword key)
+{
+  tls_alpn_proto_id_t *id = uword_to_pointer (key, tls_alpn_proto_id_t *);
+  return hash_memory (id->base, id->len, 0);
+}
+
+static uword
+tls_alpn_proto_hash_key_equal (hash_t *h, uword key1, uword key2)
+{
+  tls_alpn_proto_id_t *id1 = uword_to_pointer (key1, tls_alpn_proto_id_t *);
+  tls_alpn_proto_id_t *id2 = uword_to_pointer (key2, tls_alpn_proto_id_t *);
+  return id1 && id2 && tls_alpn_proto_id_eq (id1, id2);
+}
+
 void
 transport_fifos_init_ooo (transport_connection_t * tc)
 {
@@ -1029,6 +1087,7 @@ transport_init (void)
   vlib_thread_main_t *vtm = vlib_get_thread_main ();
   session_main_t *smm = vnet_get_session_main ();
   transport_main_t *tm = &tp_main;
+  const tls_alpn_proto_id_t *alpn_proto;
   u32 num_threads;
 
   if (smm->local_endpoints_table_buckets == 0)
@@ -1052,6 +1111,16 @@ transport_init (void)
       /* Main not polled if there are workers */
       smm->transport_cl_thread = 1;
     }
+
+  tm->alpn_proto_by_str = hash_create2 (
+    0, sizeof (tls_alpn_proto_id_t), sizeof (uword),
+    tls_alpn_proto_hash_key_sum, tls_alpn_proto_hash_key_equal, 0, 0);
+
+#define _(sym, str)                                                           \
+  alpn_proto = &tls_alpn_proto_ids[TLS_ALPN_PROTO_##sym];                     \
+  hash_set_mem (tm->alpn_proto_by_str, alpn_proto, TLS_ALPN_PROTO_##sym);
+  foreach_tls_alpn_protos
+#undef _
 }
 
 /*
index 31ad36b..10477b9 100644 (file)
@@ -120,6 +120,8 @@ typedef struct _transport_proto_vft
                                           u8 is_lcl);
   int (*attribute) (u32 conn_index, clib_thread_index_t thread_index,
                    u8 is_get, transport_endpt_attr_t *attr);
+  tls_alpn_proto_t (*get_alpn_selected) (u32 conn_index,
+                                        clib_thread_index_t thread_index);
 
   /*
    * Properties
@@ -153,6 +155,9 @@ void transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
 int transport_connection_attribute (transport_proto_t tp, u32 conn_index,
                                    u8 thread_index, u8 is_get,
                                    transport_endpt_attr_t *attr);
+tls_alpn_proto_t
+transport_get_alpn_selected (transport_proto_t tp, u32 conn_index,
+                            clib_thread_index_t thread_index);
 
 static inline transport_connection_t *
 transport_get_connection (transport_proto_t tp, u32 conn_index,
index 71e1975..5c86fd6 100644 (file)
@@ -350,6 +350,30 @@ typedef enum tls_alpn_proto_
 #undef _
 } __clib_packed tls_alpn_proto_t;
 
+typedef struct tls_alpn_proto_id_
+{
+  u8 len;
+  u8 *base;
+} tls_alpn_proto_id_t;
+
+static const tls_alpn_proto_id_t tls_alpn_proto_ids[] = {
+#define _(sym, str) { (u8) (sizeof (str) - 1), (u8 *) str },
+  foreach_tls_alpn_protos
+#undef _
+};
+
+static_always_inline u8
+tls_alpn_proto_id_eq (tls_alpn_proto_id_t *actual,
+                     tls_alpn_proto_id_t *expected)
+{
+  if (actual->len != expected->len)
+    return 0;
+  return memcmp (actual->base, expected->base, expected->len) == 0 ? 1 : 0;
+}
+
+tls_alpn_proto_t tls_alpn_proto_by_str (tls_alpn_proto_id_t *alpn_id);
+format_function_t format_tls_alpn_proto;
+
 typedef struct transport_endpt_crypto_cfg_
 {
   u32 ckpair_index;   /**< index of ck pair in application crypto layer */
index 3cdc4c1..745c96c 100644 (file)
@@ -23,12 +23,6 @@ tls_engine_vft_t *tls_vfts;
 
 void tls_disconnect (u32 ctx_handle, clib_thread_index_t thread_index);
 
-static const tls_alpn_proto_id_t tls_alpn_proto_ids[] = {
-#define _(sym, str) { (u8) (sizeof (str) - 1), (u8 *) str },
-  foreach_tls_alpn_protos
-#undef _
-};
-
 void
 tls_disconnect_transport (tls_ctx_t * ctx)
 {
@@ -111,45 +105,12 @@ tls_add_app_q_evt (app_worker_t *app_wrk, session_t *app_session)
 }
 
 tls_alpn_proto_t
-tls_alpn_proto_by_str (tls_alpn_proto_id_t *alpn_id)
-{
-  tls_main_t *tm = &tls_main;
-  uword *p;
-
-  p = hash_get_mem (tm->alpn_proto_by_str, alpn_id);
-  if (p)
-    return p[0];
-
-  return TLS_ALPN_PROTO_NONE;
-}
-
-tls_alpn_proto_t
-tls_get_alpn_selected (u32 ctx_handle)
+tls_get_alpn_selected (u32 ctx_handle, clib_thread_index_t thread_index)
 {
-  tls_ctx_t *ctx;
-  ctx = tls_ctx_get (ctx_handle);
+  tls_ctx_t *ctx = tls_ctx_get_w_thread (ctx_handle, thread_index);
   return ctx->alpn_selected;
 }
 
-u8 *
-format_tls_alpn_proto (u8 *s, va_list *args)
-{
-  tls_alpn_proto_t alpn_proto = va_arg (*args, int);
-  u8 *t = 0;
-
-  switch (alpn_proto)
-    {
-#define _(sym, str)                                                           \
-  case TLS_ALPN_PROTO_##sym:                                                  \
-    t = (u8 *) str;                                                           \
-    break;
-      foreach_tls_alpn_protos
-#undef _
-       default : return format (s, "BUG: unknown");
-    }
-  return format (s, "%s", t);
-}
-
 u32
 tls_listener_ctx_alloc (void)
 {
@@ -1190,6 +1151,7 @@ static const transport_proto_vft_t tls_proto = {
   .format_listener = format_tls_listener,
   .get_transport_endpoint = tls_transport_endpoint_get,
   .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
+  .get_alpn_selected = tls_get_alpn_selected,
   .transport_options = {
     .name = "tls",
     .short_name = "J",
@@ -1319,6 +1281,7 @@ static const transport_proto_vft_t dtls_proto = {
   .format_listener = format_tls_listener,
   .get_transport_endpoint = tls_transport_endpoint_get,
   .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
+  .get_alpn_selected = tls_get_alpn_selected,
   .transport_options = {
     .name = "dtls",
     .short_name = "D",
@@ -1334,28 +1297,12 @@ tls_register_engine (const tls_engine_vft_t * vft, crypto_engine_type_t type)
   tls_vfts[type] = *vft;
 }
 
-static uword
-tls_alpn_proto_hash_key_sum (hash_t *h, uword key)
-{
-  tls_alpn_proto_id_t *id = uword_to_pointer (key, tls_alpn_proto_id_t *);
-  return hash_memory (id->base, id->len, 0);
-}
-
-static uword
-tls_alpn_proto_hash_key_equal (hash_t *h, uword key1, uword key2)
-{
-  tls_alpn_proto_id_t *id1 = uword_to_pointer (key1, tls_alpn_proto_id_t *);
-  tls_alpn_proto_id_t *id2 = uword_to_pointer (key2, tls_alpn_proto_id_t *);
-  return id1 && id2 && tls_alpn_proto_id_eq (id1, id2);
-}
-
 static clib_error_t *
 tls_init (vlib_main_t * vm)
 {
   vlib_thread_main_t *vtm = vlib_get_thread_main ();
   tls_main_t *tm = &tls_main;
   u32 num_threads;
-  const tls_alpn_proto_id_t *alpn_proto;
 
   num_threads = 1 /* main thread */  + vtm->n_threads;
 
@@ -1382,16 +1329,6 @@ tls_init (vlib_main_t * vm)
   transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
                               FIB_PROTOCOL_IP6, ~0);
 
-  tm->alpn_proto_by_str = hash_create2 (
-    0, sizeof (tls_alpn_proto_id_t), sizeof (uword),
-    tls_alpn_proto_hash_key_sum, tls_alpn_proto_hash_key_equal, 0, 0);
-
-#define _(sym, str)                                                           \
-  alpn_proto = &tls_alpn_proto_ids[TLS_ALPN_PROTO_##sym];                     \
-  hash_set_mem (tm->alpn_proto_by_str, alpn_proto, TLS_ALPN_PROTO_##sym);
-  foreach_tls_alpn_protos
-#undef _
-
     return 0;
 }
 
index c575fc7..4e7c6f9 100644 (file)
@@ -16,7 +16,6 @@
 #include <vnet/session/application_interface.h>
 #include <vnet/session/application.h>
 #include <vnet/session/session.h>
-#include <vnet/tls/tls_types.h>
 #include <vppinfra/lock.h>
 
 #ifndef SRC_VNET_TLS_TLS_H_
@@ -140,7 +139,6 @@ typedef struct tls_main_
   u8 **rx_bufs;
   u8 **tx_bufs;
 
-  uword *alpn_proto_by_str;
   /*
    * Config
    */
diff --git a/src/vnet/tls/tls_types.h b/src/vnet/tls/tls_types.h
deleted file mode 100644 (file)
index bf201b1..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/* SPDX-License-Identifier: Apache-2.0
- * Copyright(c) 2025 Cisco Systems, Inc.
- */
-
-#ifndef SRC_VNET_TLS_TLS_TYPES_H_
-#define SRC_VNET_TLS_TLS_TYPES_H_
-
-#include <vppinfra/types.h>
-
-typedef struct tls_alpn_proto_id_
-{
-  u8 len;
-  u8 *base;
-} tls_alpn_proto_id_t;
-
-static inline u8
-tls_alpn_proto_id_eq (tls_alpn_proto_id_t *actual,
-                     tls_alpn_proto_id_t *expected)
-{
-  if (actual->len != expected->len)
-    return 0;
-  return memcmp (actual->base, expected->base, expected->len) == 0 ? 1 : 0;
-}
-
-tls_alpn_proto_t tls_alpn_proto_by_str (tls_alpn_proto_id_t *alpn_id);
-
-tls_alpn_proto_t tls_get_alpn_selected (u32 ctx_handle);
-
-format_function_t format_tls_alpn_proto;
-
-#endif /* SRC_VNET_TLS_TLS_TYPES_H_ */