From 18eedde9f2c73735628627cffb6565b3573abc0b Mon Sep 17 00:00:00 2001 From: Ole Troan Date: Fri, 22 Nov 2024 09:22:20 +0100 Subject: [PATCH] ip: add enable ip4 api A philosophical question. Do an interface have to have an IPv4 address to process IPv4 packets? For ICMP error generation it's sufficient that it has an address available on the node. More concretely this patch is to allow an extern DHCP client to process IP packets before it configures an address on the interface, without having to have an node early in the ip4-unicast feature-arc like ip4-dhcp-client-detect to intercept the packets. Type: improvement Change-Id: I780c579eec28ba564cf8417fbcc87e7a7876fdd2 Signed-off-by: Ole Troan --- src/vnet/ip/ip.api | 14 ++++++++++++++ src/vnet/ip/ip46_cli.c | 39 +++++++++++++++++++++++++++++++++++++++ src/vnet/ip/ip_api.c | 20 ++++++++++++++++++++ src/vnet/ip/ip_test.c | 5 +++++ 4 files changed, 78 insertions(+) diff --git a/src/vnet/ip/ip.api b/src/vnet/ip/ip.api index fc7d7582dec..be151bdf4f4 100644 --- a/src/vnet/ip/ip.api +++ b/src/vnet/ip/ip.api @@ -446,6 +446,20 @@ autoreply define sw_interface_ip6_enable_disable bool enable; /* set to true if enable */ }; +/** \brief IPv4 interface enable / disable request + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - interface used to reach neighbor + @param enable - if non-zero enable ip4 on interface, else disable +*/ +autoendian autoreply define sw_interface_ip4_enable_disable +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + bool enable; /* set to true if enable */ +}; + /** \brief Dump IP multicast fib table @param client_index - opaque cookie to identify the sender */ diff --git a/src/vnet/ip/ip46_cli.c b/src/vnet/ip/ip46_cli.c index e3da27914bd..022c4174174 100644 --- a/src/vnet/ip/ip46_cli.c +++ b/src/vnet/ip/ip46_cli.c @@ -292,6 +292,45 @@ VLIB_CLI_COMMAND (set_reassembly_command, static) = { .function = set_reassembly_command_fn, }; +static clib_error_t * +enable_ip4_interface_cmd (vlib_main_t *vm, unformat_input_t *input, + vlib_cli_command_t *cmd) +{ + vnet_main_t *vnm = vnet_get_main (); + clib_error_t *error = NULL; + u32 sw_if_index; + + sw_if_index = ~0; + + if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) + { + vnet_feature_enable_disable ("ip4-unicast", "ip4-not-enabled", + sw_if_index, 0, 0, 0); + + vnet_feature_enable_disable ("ip4-multicast", "ip4-not-enabled", + sw_if_index, 0, 0, 0); + } + else + { + error = clib_error_return (0, "unknown interface\n'", + format_unformat_error, input); + } + return error; +} + +/*? + * This command is used to enable IPv4 on a given interface. + * + * @cliexpar + * Example of how enable IPv4 on a given interface: + * @cliexcmd{enable ip4 interface GigabitEthernet2/0/0} +?*/ +VLIB_CLI_COMMAND (enable_ip4_interface_command, static) = { + .path = "enable ip4 interface", + .function = enable_ip4_interface_cmd, + .short_help = "enable ip4 interface ", +}; + /* Dummy init function to get us linked in. */ static clib_error_t * ip4_cli_init (vlib_main_t * vm) diff --git a/src/vnet/ip/ip_api.c b/src/vnet/ip/ip_api.c index 1f025fa1113..e7133a5021f 100644 --- a/src/vnet/ip/ip_api.c +++ b/src/vnet/ip/ip_api.c @@ -73,6 +73,26 @@ static void REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY); } +static void +vl_api_sw_interface_ip4_enable_disable_t_handler ( + vl_api_sw_interface_ip4_enable_disable_t *mp) +{ + vl_api_sw_interface_ip4_enable_disable_reply_t *rmp; + int rv = 0; + + VALIDATE_SW_IF_INDEX_END (mp); + + vnet_feature_enable_disable ("ip4-unicast", "ip4-not-enabled", + mp->sw_if_index, mp->enable, 0, 0); + + vnet_feature_enable_disable ("ip4-multicast", "ip4-not-enabled", + mp->sw_if_index, mp->enable, 0, 0); + + BAD_SW_IF_INDEX_LABEL; + + REPLY_MACRO_END (VL_API_SW_INTERFACE_IP4_ENABLE_DISABLE_REPLY); +} + static void send_ip_table_details (vpe_api_main_t * am, vl_api_registration_t * reg, diff --git a/src/vnet/ip/ip_test.c b/src/vnet/ip/ip_test.c index 0d1c71063ae..1e803dd4501 100644 --- a/src/vnet/ip/ip_test.c +++ b/src/vnet/ip/ip_test.c @@ -1324,6 +1324,11 @@ api_sw_interface_ip6_enable_disable (vat_main_t *vam) return ret; } +static int +api_sw_interface_ip4_enable_disable (vat_main_t *vam) +{ + return -1; +} static int api_set_ip_flow_hash_v2 (vat_main_t *vat) {