From cdb7170ab44affe51c574eb2218744fff1bdd369 Mon Sep 17 00:00:00 2001 From: Aloys Augustin Date: Mon, 8 Apr 2019 17:54:39 +0200 Subject: Add get_endpoint in transport vft This allows QUIC & TLS specific logic to be implemented, and meaningfull IP/port to be returned when connection is overridden. Change-Id: Id79c59fe4d7b16d36f0e96ad3e281c4026b5fe65 Signed-off-by: Aloys Augustin --- src/plugins/quic/quic.c | 36 +++++++++++++++++++++++++++ src/tests/vnet/session/quic_echo.c | 3 ++- src/vnet/session/session.c | 16 +++++++++++- src/vnet/session/session.h | 6 +++-- src/vnet/session/session_api.c | 17 ++++++------- src/vnet/session/transport.c | 51 ++++++++++++++++++++++++++++++++++++++ src/vnet/session/transport.h | 11 ++++++++ 7 files changed, 126 insertions(+), 14 deletions(-) diff --git a/src/plugins/quic/quic.c b/src/plugins/quic/quic.c index a2aa17a3ca8..c066b0d955e 100644 --- a/src/plugins/quic/quic.c +++ b/src/plugins/quic/quic.c @@ -1620,6 +1620,40 @@ quic_app_rx_callback (session_t * udp_session) return 0; } +always_inline void +quic_common_get_transport_endpoint (quic_ctx_t * ctx, ip46_address_t * ip, + u16 * port, u8 * is_ip4, u8 is_lcl) +{ + session_t *udp_session; + QUIC_DBG (2, "Called quic_get_transport_endpoint"); + if (ctx->c_quic_ctx_id.is_stream) + *is_ip4 = 255; /* well this is ugly */ + else + { + udp_session = + session_get_from_handle (ctx->c_quic_ctx_id.udp_session_handle); + session_get_endpoint (udp_session, ip, port, is_ip4, is_lcl); + } +} + +static void +quic_get_transport_listener_endpoint (u32 listener_index, ip46_address_t * ip, + u16 * port, u8 * is_ip4, u8 is_lcl) +{ + quic_ctx_t *ctx; + ctx = quic_ctx_get (listener_index); + quic_common_get_transport_endpoint (ctx, ip, port, is_ip4, is_lcl); +} + +static void +quic_get_transport_endpoint (u32 ctx_index, u32 thread_index, + ip46_address_t * ip, u16 * port, u8 * is_ip4, + u8 is_lcl) +{ + quic_ctx_t *ctx; + ctx = quic_ctx_get_w_thread (ctx_index, thread_index); + quic_common_get_transport_endpoint (ctx, ip, port, is_ip4, is_lcl); +} /***************************************************************************** * END TRANSPORT PROTO FUNCTIONS @@ -1650,6 +1684,8 @@ const static transport_proto_vft_t quic_proto = { .format_connection = format_quic_connection, .format_half_open = format_quic_half_open, .format_listener = format_quic_listener, + .get_transport_endpoint = quic_get_transport_endpoint, + .get_transport_listener_endpoint = quic_get_transport_listener_endpoint, }; /* *INDENT-ON* */ diff --git a/src/tests/vnet/session/quic_echo.c b/src/tests/vnet/session/quic_echo.c index e109fba9ad8..41ea5aae19f 100644 --- a/src/tests/vnet/session/quic_echo.c +++ b/src/tests/vnet/session/quic_echo.c @@ -635,7 +635,8 @@ session_bound_handler (session_bound_msg_t * mp) } clib_warning ("listening on %U:%u", format_ip46_address, mp->lcl_ip, - mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, mp->lcl_port); + mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, + clib_net_to_host_u16 (mp->lcl_port)); em->state = STATE_READY; } diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c index 58ec1c01369..c1d02ab8f09 100644 --- a/src/vnet/session/session.c +++ b/src/vnet/session/session.c @@ -1228,7 +1228,7 @@ session_segment_handle (session_t * s) { svm_fifo_t *f; - if (s->session_state == SESSION_STATE_LISTENING) + if (!s->rx_fifo) return SESSION_INVALID_HANDLE; f = s->rx_fifo; @@ -1291,6 +1291,20 @@ session_get_transport (session_t * s) s->connection_index); } +void +session_get_endpoint (session_t * s, ip46_address_t * ip, u16 * port, + u8 * is_ip4, u8 is_lcl) +{ + if (s->session_state != SESSION_STATE_LISTENING) + return transport_get_endpoint (session_get_transport_proto (s), + s->connection_index, s->thread_index, ip, + port, is_ip4, is_lcl); + else + return transport_get_listener_endpoint (session_get_transport_proto (s), + s->connection_index, ip, port, + is_ip4, is_lcl); +} + transport_connection_t * listen_session_get_transport (session_t * s) { diff --git a/src/vnet/session/session.h b/src/vnet/session/session.h index 997d15395ef..375b47a60cd 100644 --- a/src/vnet/session/session.h +++ b/src/vnet/session/session.h @@ -338,7 +338,8 @@ void session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp, void *rpc_args); transport_connection_t *session_get_transport (session_t * s); - +void session_get_endpoint (session_t * s, ip46_address_t * ip, u16 * port, + u8 * is_ip4, u8 is_lcl); u8 *format_session (u8 * s, va_list * args); uword unformat_session (unformat_input_t * input, va_list * args); @@ -444,7 +445,8 @@ transport_add_tx_event (transport_connection_t * tc) always_inline u64 listen_session_get_handle (session_t * s) { - ASSERT (s->session_state == SESSION_STATE_LISTENING); + ASSERT (s->session_state == SESSION_STATE_LISTENING || + session_get_transport_proto (s) == TRANSPORT_PROTO_QUIC); return session_handle (s); } diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c index 8e37d428982..d0af0fac279 100755 --- a/src/vnet/session/session_api.c +++ b/src/vnet/session/session_api.c @@ -174,7 +174,6 @@ mq_send_session_accepted_cb (session_t * s) app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); svm_msg_q_msg_t _msg, *msg = &_msg; svm_msg_q_t *vpp_queue, *app_mq; - transport_connection_t *tc; session_t *listener; session_accepted_msg_t *mp; session_event_t *evt; @@ -212,11 +211,9 @@ mq_send_session_accepted_cb (session_t * s) vpp_queue = session_main_get_vpp_event_queue (s->thread_index); mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); mp->handle = session_handle (s); - tc = transport_get_connection (session_get_transport_proto (s), - s->connection_index, s->thread_index); - mp->port = tc->rmt_port; - mp->is_ip4 = tc->is_ip4; - clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip)); + + session_get_endpoint (s, (ip46_address_t *) mp->ip, &mp->port, + &mp->is_ip4, 0 /* is_lcl */ ); } else { @@ -350,9 +347,10 @@ mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context, vpp_mq = session_main_get_vpp_event_queue (s->thread_index); mp->handle = session_handle (s); mp->vpp_event_queue_address = pointer_to_uword (vpp_mq); - clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip)); - mp->is_ip4 = tc->is_ip4; - mp->lcl_port = tc->lcl_port; + + session_get_endpoint (s, (ip46_address_t *) mp->lcl_ip, &mp->lcl_port, + &mp->is_ip4, 1 /* is_lcl */ ); + mp->server_rx_fifo = pointer_to_uword (s->rx_fifo); mp->server_tx_fifo = pointer_to_uword (s->tx_fifo); mp->segment_handle = session_segment_handle (s); @@ -397,7 +395,6 @@ mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context, session_event_t *evt; app_listener_t *al; session_t *ls = 0; - app_wrk = app_worker_get (app_wrk_index); app_mq = app_wrk->event_queue; if (!app_mq) diff --git a/src/vnet/session/transport.c b/src/vnet/session/transport.c index d83ecfb133c..9efe0e17307 100644 --- a/src/vnet/session/transport.c +++ b/src/vnet/session/transport.c @@ -319,6 +319,57 @@ 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, + ip46_address_t * ip, u16 * port, u8 * is_ip4, + u8 is_lcl) +{ + if (is_lcl) + { + *port = tc->lcl_port; + *is_ip4 = tc->is_ip4; + clib_memcpy_fast (ip, &tc->lcl_ip, sizeof (tc->lcl_ip)); + } + else + { + *port = tc->rmt_port; + *is_ip4 = tc->is_ip4; + clib_memcpy_fast (ip, &tc->rmt_ip, sizeof (tc->rmt_ip)); + } +} + +void +transport_get_endpoint (transport_proto_t tp, u32 conn_index, + u32 thread_index, ip46_address_t * ip, u16 * port, + u8 * is_ip4, u8 is_lcl) +{ + if (tp_vfts[tp].get_transport_endpoint) + tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, ip, port, + is_ip4, is_lcl); + else + { + transport_connection_t *tc; + tc = transport_get_connection (tp, conn_index, thread_index); + default_get_transport_endpoint (tc, ip, port, is_ip4, is_lcl); + } +} + +void +transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index, + ip46_address_t * ip, u16 * port, u8 * is_ip4, + u8 is_lcl) +{ + if (tp_vfts[tp].get_transport_listener_endpoint) + tp_vfts[tp].get_transport_listener_endpoint (conn_index, ip, port, is_ip4, + is_lcl); + else + { + transport_connection_t *tc; + tc = transport_get_listener (tp, conn_index); + default_get_transport_endpoint (tc, ip, port, is_ip4, is_lcl); + } +} + #define PORT_MASK ((1 << 16)- 1) void diff --git a/src/vnet/session/transport.h b/src/vnet/session/transport.h index 993b8bd7354..73d7fba3c49 100644 --- a/src/vnet/session/transport.h +++ b/src/vnet/session/transport.h @@ -62,6 +62,11 @@ typedef struct _transport_proto_vft u8 *(*format_listener) (u8 * s, va_list * args); u8 *(*format_half_open) (u8 * s, va_list * args); + /* External API response */ + + void (*get_transport_endpoint) (u32 conn_index, u32 thread_index, ip46_address_t * ip, u16 * port, u8 * is_ip4, u8 is_lcl); + void (*get_transport_listener_endpoint) (u32 conn_index, ip46_address_t * ip, u16 * port, u8 * is_ip4, u8 is_lcl); + /* * Properties */ @@ -86,6 +91,12 @@ u32 transport_start_listen (transport_proto_t tp, u32 session_index, u32 transport_stop_listen (transport_proto_t tp, u32 conn_index); void transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index); +void transport_get_endpoint (transport_proto_t tp, u32 conn_index, + u32 thread_index, ip46_address_t * ip, + u16 * port, u8 * is_ip4, u8 is_lcl); +void transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index, + ip46_address_t * ip, u16 * port, + u8 * is_ip4, u8 is_lcl); static inline transport_connection_t * transport_get_connection (transport_proto_t tp, u32 conn_index, -- cgit 1.2.3-korg