From 8a6f5d394c69eba3bfaddfe36a93d1013cba3ce3 Mon Sep 17 00:00:00 2001 From: Arthur de Kerhor Date: Thu, 20 May 2021 11:48:00 +0200 Subject: udp: add udp decapsulation Possibility to register a port via CLI or API to decap incoming UDP packets: - For CLI, a user needs to specify the inner protocol (only MPLS supported for now) - For API, the protocol is specified by index Added unittests Type: feature Change-Id: Ifedd86d8db2e355b7618472554fd67d77a13a4aa Signed-off-by: Arthur de Kerhor --- src/vnet/udp/udp.api | 35 ++++++++++++++++ src/vnet/udp/udp_api.c | 57 +++++++++++++++++++++---- src/vnet/udp/udp_decap.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 src/vnet/udp/udp_decap.c (limited to 'src/vnet/udp') diff --git a/src/vnet/udp/udp.api b/src/vnet/udp/udp.api index 2cd2bd50272..02176be7c2b 100644 --- a/src/vnet/udp/udp.api +++ b/src/vnet/udp/udp.api @@ -45,6 +45,26 @@ typedef udp_encap u32 id; }; +enum udp_decap_next_proto +{ + UDP_API_DECAP_PROTO_IP4, + UDP_API_DECAP_PROTO_IP6, + UDP_API_DECAP_PROTO_MPLS, +}; + +/** + * @brief UDP Decap object + * @param is_ip4 - IPv4 if non-zero, else IPv6 + * @param port - port to listen on for the decap + * @param next_proto - the protocol of the inner header + */ +typedef udp_decap +{ + u8 is_ip4; + u16 port; + vl_api_udp_decap_next_proto_t next_proto; +}; + /** * @brief Add UDP encap * @param client_index - opaque cookie to identify the sender @@ -103,6 +123,21 @@ define udp_encap_details vl_api_udp_encap_t udp_encap; }; +/** + * @brief Add/Del UDP decap + * @param client_index - opaque cookie to identify the sender + * @param context - sender context, to match reply w/ request + * @param is_add - add decap if non-zero, else delete + * @param udp_decap - UDP decap description + */ +autoreply define udp_decap_add_del +{ + u32 client_index; + u32 context; + bool is_add; + vl_api_udp_decap_t udp_decap; +}; + /* * Local Variables: * eval: (c-set-style "gnu") diff --git a/src/vnet/udp/udp_api.c b/src/vnet/udp/udp_api.c index 5b9d8945642..6ba92bbf207 100644 --- a/src/vnet/udp/udp_api.c +++ b/src/vnet/udp/udp_api.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -38,11 +39,11 @@ #include - -#define foreach_udp_api_msg \ -_(UDP_ENCAP_DEL, udp_encap_del) \ -_(UDP_ENCAP_ADD, udp_encap_add) \ -_(UDP_ENCAP_DUMP, udp_encap_dump) +#define foreach_udp_api_msg \ + _ (UDP_ENCAP_DEL, udp_encap_del) \ + _ (UDP_ENCAP_ADD, udp_encap_add) \ + _ (UDP_ENCAP_DUMP, udp_encap_dump) \ + _ (UDP_DECAP_ADD_DEL, udp_decap_add_del) static void send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg, @@ -92,8 +93,7 @@ send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg, } static void -vl_api_udp_encap_dump_t_handler (vl_api_udp_encap_dump_t * mp, - vlib_main_t * vm) +vl_api_udp_encap_dump_t_handler (vl_api_udp_encap_dump_t *mp) { vl_api_registration_t *reg; udp_encap_t *ue; @@ -111,7 +111,7 @@ vl_api_udp_encap_dump_t_handler (vl_api_udp_encap_dump_t * mp, } static void -vl_api_udp_encap_add_t_handler (vl_api_udp_encap_add_t * mp, vlib_main_t * vm) +vl_api_udp_encap_add_t_handler (vl_api_udp_encap_add_t *mp) { vl_api_udp_encap_add_reply_t *rmp; ip46_address_t src_ip, dst_ip; @@ -152,7 +152,7 @@ done: } static void -vl_api_udp_encap_del_t_handler (vl_api_udp_encap_del_t * mp, vlib_main_t * vm) +vl_api_udp_encap_del_t_handler (vl_api_udp_encap_del_t *mp) { vl_api_udp_encap_del_reply_t *rmp; int rv = 0; @@ -162,6 +162,45 @@ vl_api_udp_encap_del_t_handler (vl_api_udp_encap_del_t * mp, vlib_main_t * vm) REPLY_MACRO (VL_API_UDP_ENCAP_DEL_REPLY); } +u32 +udp_api_decap_proto_to_index (vlib_main_t *vm, + vl_api_udp_decap_next_proto_t iproto) +{ + switch (iproto) + { + case UDP_API_DECAP_PROTO_IP4: + return vlib_get_node_by_name (vm, (u8 *) "ip4-input")->index; + case UDP_API_DECAP_PROTO_IP6: + return vlib_get_node_by_name (vm, (u8 *) "ip6-input")->index; + case UDP_API_DECAP_PROTO_MPLS: + return vlib_get_node_by_name (vm, (u8 *) "mpls-input")->index; + } + return ~0; +} + +static void +vl_api_udp_decap_add_del_t_handler (vl_api_udp_decap_add_del_t *mp) +{ + vl_api_udp_decap_add_del_reply_t *rmp; + vlib_main_t *vm = vlib_get_main (); + int rv = 0; + + if (mp->is_add) + { + u32 node_index = + udp_api_decap_proto_to_index (vm, ntohl (mp->udp_decap.next_proto)); + if (node_index == ~0) + rv = VNET_API_ERROR_INVALID_PROTOCOL; + else + udp_register_dst_port (vm, ntohs (mp->udp_decap.port), node_index, + mp->udp_decap.is_ip4); + } + else + udp_unregister_dst_port (vm, ntohs (mp->udp_decap.port), + mp->udp_decap.is_ip4); + REPLY_MACRO (VL_API_UDP_DECAP_ADD_DEL_REPLY); +} + #define vl_msg_name_crc_list #include #undef vl_msg_name_crc_list diff --git a/src/vnet/udp/udp_decap.c b/src/vnet/udp/udp_decap.c new file mode 100644 index 00000000000..7337ac17ee9 --- /dev/null +++ b/src/vnet/udp/udp_decap.c @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 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 + +uword +unformat_next_node (unformat_input_t *input, va_list *args) +{ + vlib_main_t *vm = va_arg (*args, vlib_main_t *); + u32 *node_index = va_arg (*args, u32 *); + if (unformat (input, "mpls")) + *node_index = vlib_get_node_by_name (vm, (u8 *) "mpls-input")->index; + else if (unformat (input, "ip4")) + *node_index = vlib_get_node_by_name (vm, (u8 *) "ip4-input")->index; + else if (unformat (input, "ip6")) + *node_index = vlib_get_node_by_name (vm, (u8 *) "ip6-input")->index; + else + return 0; + return 1; +} + +static clib_error_t * +udp_decap_cli (vlib_main_t *vm, unformat_input_t *input, + vlib_cli_command_t *cmd_arg) +{ + unformat_input_t _line_input, *line_input = &_line_input; + clib_error_t *error = NULL; + u8 is_add = 1, is_ip4 = 1; + int i = 0; + u16 port = 0; + u32 node_index = ~0; + + if (!unformat_user (input, unformat_line_input, line_input)) + return 0; + + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "add")) + is_add = 1; + else if (unformat (line_input, "del")) + is_add = 0; + else if (unformat (line_input, "ipv4")) + is_ip4 = 1; + else if (unformat (line_input, "ipv6")) + is_ip4 = 0; + else if (unformat (line_input, "%d", &i)) + port = i; + else if (unformat (line_input, "next-proto %U", unformat_next_node, vm, + &node_index)) + ; + else + { + error = clib_error_return (0, "parse error: '%U'", + format_unformat_error, line_input); + goto done; + } + } + if (port == 0) + { + error = clib_error_return (0, "missing port"); + goto done; + } + if (is_add && node_index == ~0) + { + error = clib_error_return (0, "missing protocol"); + goto done; + } + if (is_add) + udp_register_dst_port (vm, port, node_index, is_ip4); + else + udp_unregister_dst_port (vm, port, is_ip4); + +done: + unformat_free (line_input); + return error; +} + +/*? + * Register a port to decapsulate incoming UDP encapsulated packets. + * + * @cliexpar + * @clistart + * udp decap add ipv4 1234 next-proto mpls + * @cliend + * @cliexcmd{udp decap [add|del] [ipv4|ipv6] next-proto +} +?*/ + +VLIB_CLI_COMMAND (udp_decap_add_command, static) = { + .path = "udp decap", + .short_help = + "udp decap [add|del] [ipv4|ipv6] next-proto ", + .function = udp_decap_cli, +}; \ No newline at end of file -- cgit 1.2.3-korg