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 <arthurdekerhor@gmail.com>
diff --git a/src/vnet/CMakeLists.txt b/src/vnet/CMakeLists.txt
index 47d7b56..a24b87b 100644
--- a/src/vnet/CMakeLists.txt
+++ b/src/vnet/CMakeLists.txt
@@ -660,6 +660,7 @@
udp/udp_pg.c
udp/udp_encap_node.c
udp/udp_encap.c
+ udp/udp_decap.c
udp/udp_api.c
)
diff --git a/src/vnet/fib/fib_path.c b/src/vnet/fib/fib_path.c
index 01140d5..c580045 100644
--- a/src/vnet/fib/fib_path.c
+++ b/src/vnet/fib/fib_path.c
@@ -2328,10 +2328,12 @@
case FIB_PATH_TYPE_DVR:
fib_urpf_list_append(urpf, path->dvr.fp_interface);
break;
+ case FIB_PATH_TYPE_UDP_ENCAP:
+ fib_urpf_list_append(urpf, path->udp_encap.fp_udp_encap_id);
+ break;
case FIB_PATH_TYPE_DEAG:
case FIB_PATH_TYPE_RECEIVE:
case FIB_PATH_TYPE_INTF_RX:
- case FIB_PATH_TYPE_UDP_ENCAP:
case FIB_PATH_TYPE_BIER_FMASK:
case FIB_PATH_TYPE_BIER_TABLE:
case FIB_PATH_TYPE_BIER_IMP:
diff --git a/src/vnet/udp/udp.api b/src/vnet/udp/udp.api
index 2cd2bd5..02176be 100644
--- a/src/vnet/udp/udp.api
+++ b/src/vnet/udp/udp.api
@@ -45,6 +45,26 @@
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 @@
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 5b9d894..6ba92bb 100644
--- a/src/vnet/udp/udp_api.c
+++ b/src/vnet/udp/udp_api.c
@@ -16,6 +16,7 @@
#include <vnet/vnet.h>
#include <vlibmemory/api.h>
+#include <vnet/udp/udp_local.h>
#include <vnet/udp/udp_encap.h>
#include <vnet/fib/fib_table.h>
#include <vnet/ip/ip_types_api.h>
@@ -38,11 +39,11 @@
#include <vlibapi/api_helper_macros.h>
-
-#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 @@
}
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 @@
}
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 @@
}
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 @@
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 <vnet/udp/udp.api.h>
#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 0000000..7337ac1
--- /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 <vnet/udp/udp.h>
+
+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] <dst-port> next-proto
+<inner-protocol>}
+?*/
+
+VLIB_CLI_COMMAND (udp_decap_add_command, static) = {
+ .path = "udp decap",
+ .short_help =
+ "udp decap [add|del] [ipv4|ipv6] <dst-port> next-proto <inner-protocol>",
+ .function = udp_decap_cli,
+};
\ No newline at end of file