diff options
Diffstat (limited to 'extras/router-plugin/rtinject')
-rw-r--r-- | extras/router-plugin/rtinject/tap_inject.c | 161 | ||||
-rw-r--r-- | extras/router-plugin/rtinject/tap_inject.h | 40 | ||||
-rw-r--r-- | extras/router-plugin/rtinject/tap_inject_netlink.c | 227 | ||||
-rw-r--r-- | extras/router-plugin/rtinject/tap_inject_node.c | 172 | ||||
-rw-r--r-- | extras/router-plugin/rtinject/tap_inject_tap.c | 30 |
5 files changed, 306 insertions, 324 deletions
diff --git a/extras/router-plugin/rtinject/tap_inject.c b/extras/router-plugin/rtinject/tap_inject.c index f41ae86c8..7befb3862 100644 --- a/extras/router-plugin/rtinject/tap_inject.c +++ b/extras/router-plugin/rtinject/tap_inject.c @@ -16,10 +16,10 @@ #include "tap_inject.h" -#include <vnet/mfib/mfib_table.h> +#include <vnet/fib/fib.h> #include <vnet/ip/ip.h> #include <vnet/ip/lookup.h> -#include <vnet/fib/fib.h> +#include <vnet/mfib/mfib_table.h> static tap_inject_main_t tap_inject_main; extern dpo_type_t tap_inject_dpo_type; @@ -33,7 +33,7 @@ tap_inject_get_main (void) void tap_inject_insert_tap (u32 sw_if_index, u32 tap_fd, u32 tap_if_index) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); vec_validate_init_empty (im->sw_if_index_to_tap_fd, sw_if_index, ~0); vec_validate_init_empty (im->sw_if_index_to_tap_if_index, sw_if_index, ~0); @@ -51,7 +51,7 @@ tap_inject_insert_tap (u32 sw_if_index, u32 tap_fd, u32 tap_if_index) void tap_inject_delete_tap (u32 sw_if_index) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); u32 tap_fd = im->sw_if_index_to_tap_fd[sw_if_index]; u32 tap_if_index = im->sw_if_index_to_tap_if_index[sw_if_index]; @@ -65,7 +65,7 @@ tap_inject_delete_tap (u32 sw_if_index) u32 tap_inject_lookup_tap_fd (u32 sw_if_index) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); vec_validate_init_empty (im->sw_if_index_to_tap_fd, sw_if_index, ~0); return im->sw_if_index_to_tap_fd[sw_if_index]; @@ -74,7 +74,7 @@ tap_inject_lookup_tap_fd (u32 sw_if_index) u32 tap_inject_lookup_sw_if_index_from_tap_fd (u32 tap_fd) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); vec_validate_init_empty (im->tap_fd_to_sw_if_index, tap_fd, ~0); return im->tap_fd_to_sw_if_index[tap_fd]; @@ -83,25 +83,24 @@ tap_inject_lookup_sw_if_index_from_tap_fd (u32 tap_fd) u32 tap_inject_lookup_sw_if_index_from_tap_if_index (u32 tap_if_index) { - tap_inject_main_t * im = tap_inject_get_main (); - uword * sw_if_index; + tap_inject_main_t *im = tap_inject_get_main (); + uword *sw_if_index; sw_if_index = hash_get (im->tap_if_index_to_sw_if_index, tap_if_index); - return sw_if_index ? *(u32 *)sw_if_index : ~0; + return sw_if_index ? *(u32 *) sw_if_index : ~0; } /* *INDENT-OFF* */ VLIB_PLUGIN_REGISTER () = { - // .version = VPP_BUILD_VER, FIXME - .description = "router", + // .version = VPP_BUILD_VER, FIXME + .description = "router", }; /* *INDENT-ON* */ - static void tap_inject_disable (void) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); im->flags &= ~TAP_INJECT_F_ENABLED; @@ -111,8 +110,8 @@ tap_inject_disable (void) static clib_error_t * tap_inject_enable (void) { - vlib_main_t * vm = vlib_get_main (); - tap_inject_main_t * im = tap_inject_get_main (); + vlib_main_t *vm = vlib_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); if (tap_inject_is_enabled ()) return 0; @@ -127,7 +126,8 @@ tap_inject_enable (void) } /* Register ARP and ICMP6 as neighbor nodes. */ - ethernet_register_input_type (vm, ETHERNET_TYPE_ARP, im->neighbor_node_index); + ethernet_register_input_type (vm, ETHERNET_TYPE_ARP, + im->neighbor_node_index); ip6_register_protocol (IP_PROTOCOL_ICMP6, im->neighbor_node_index); /* Register remaining protocols. */ @@ -147,26 +147,25 @@ tap_inject_enable (void) const mfib_prefix_t pfx_224_0_0_0 = { .fp_len = 24, .fp_proto = FIB_PROTOCOL_IP4, - .fp_grp_addr = { - .ip4.as_u32 = clib_host_to_net_u32(0xe0000000), - }, - .fp_src_addr = { - .ip4.as_u32 = 0, - }, + .fp_grp_addr = + { + .ip4.as_u32 = clib_host_to_net_u32(0xe0000000), + }, + .fp_src_addr = + { + .ip4.as_u32 = 0, + }, }; - dpo_set(&dpo, tap_inject_dpo_type, DPO_PROTO_IP4, ~0); + dpo_set (&dpo, tap_inject_dpo_type, DPO_PROTO_IP4, ~0); - index_t repi = replicate_create(1, DPO_PROTO_IP4); - replicate_set_bucket(repi, 0, &dpo); + index_t repi = replicate_create (1, DPO_PROTO_IP4); + replicate_set_bucket (repi, 0, &dpo); - mfib_table_entry_special_add(0, - &pfx_224_0_0_0, - MFIB_SOURCE_API, - MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF, - repi); + mfib_table_entry_special_add (0, &pfx_224_0_0_0, MFIB_SOURCE_API, + MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF, repi); - dpo_reset(&dpo); + dpo_reset (&dpo); } im->flags |= TAP_INJECT_F_ENABLED; @@ -175,24 +174,24 @@ tap_inject_enable (void) } static uword -tap_inject_iface_isr (vlib_main_t * vm, vlib_node_runtime_t * node, - vlib_frame_t * f) +tap_inject_iface_isr (vlib_main_t *vm, vlib_node_runtime_t *node, + vlib_frame_t *f) { - tap_inject_main_t * im = tap_inject_get_main (); - vnet_hw_interface_t * hw; - u32 * hw_if_index; - clib_error_t * err = 0; + tap_inject_main_t *im = tap_inject_get_main (); + vnet_hw_interface_t *hw; + u32 *hw_if_index; + clib_error_t *err = 0; vec_foreach (hw_if_index, im->interfaces_to_enable) { hw = vnet_get_hw_interface (vnet_get_main (), *hw_if_index); if (hw->hw_class_index == ethernet_hw_interface_class.index) - { - err = tap_inject_tap_connect (hw); - if (err) - break; - } + { + err = tap_inject_tap_connect (hw); + if (err) + break; + } } vec_foreach (hw_if_index, im->interfaces_to_disable) @@ -212,13 +211,12 @@ VLIB_REGISTER_NODE (tap_inject_iface_isr_node, static) = { .vector_size = sizeof (u32), }; - static clib_error_t * -tap_inject_interface_add_del (struct vnet_main_t * vnet_main, u32 hw_if_index, - u32 add) +tap_inject_interface_add_del (struct vnet_main_t *vnet_main, u32 hw_if_index, + u32 add) { - vlib_main_t * vm = vlib_get_main (); - tap_inject_main_t * im = tap_inject_get_main (); + vlib_main_t *vm = vlib_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); if (!tap_inject_is_config_enabled ()) return 0; @@ -237,15 +235,14 @@ tap_inject_interface_add_del (struct vnet_main_t * vnet_main, u32 hw_if_index, VNET_HW_INTERFACE_ADD_DEL_FUNCTION (tap_inject_interface_add_del); - static clib_error_t * tap_inject_enable_disable_all_interfaces (int enable) { - vnet_main_t * vnet_main = vnet_get_main (); - tap_inject_main_t * im = tap_inject_get_main (); - vnet_hw_interface_t * interfaces; - vnet_hw_interface_t * hw; - u32 ** indices; + vnet_main_t *vnet_main = vnet_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); + vnet_hw_interface_t *interfaces; + vnet_hw_interface_t *hw; + u32 **indices; if (enable) tap_inject_enable (); @@ -255,7 +252,8 @@ tap_inject_enable_disable_all_interfaces (int enable) /* Collect all the interface indices. */ interfaces = vnet_main->interface_main.hw_interfaces; indices = enable ? &im->interfaces_to_enable : &im->interfaces_to_disable; - pool_foreach (hw, interfaces, vec_add1 (*indices, hw - interfaces)); + pool_foreach (hw, interfaces, vec_add1 (*indices, hw - interfaces)) + ; if (tap_inject_iface_isr (vlib_get_main (), 0, 0)) return clib_error_return (0, "tap-inject interface add del isr failed"); @@ -264,26 +262,26 @@ tap_inject_enable_disable_all_interfaces (int enable) } static clib_error_t * -tap_inject_cli (vlib_main_t * vm, unformat_input_t * input, - vlib_cli_command_t * cmd) +tap_inject_cli (vlib_main_t *vm, unformat_input_t *input, + vlib_cli_command_t *cmd) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); if (cmd->function_arg) { - clib_error_t * err; + clib_error_t *err; if (tap_inject_is_config_disabled ()) - return clib_error_return (0, - "tap-inject is disabled in config, thus cannot be enabled."); + return clib_error_return ( + 0, "tap-inject is disabled in config, thus cannot be enabled."); /* Enable */ err = tap_inject_enable_disable_all_interfaces (1); if (err) - { - tap_inject_enable_disable_all_interfaces (0); - return err; - } + { + tap_inject_enable_disable_all_interfaces (0); + return err; + } im->flags |= TAP_INJECT_F_CONFIG_ENABLE; } @@ -311,13 +309,12 @@ VLIB_CLI_COMMAND (tap_inject_disable_cmd, static) = { .function_arg = 0, }; - static clib_error_t * -show_tap_inject (vlib_main_t * vm, unformat_input_t * input, - vlib_cli_command_t * cmd) +show_tap_inject (vlib_main_t *vm, unformat_input_t *input, + vlib_cli_command_t *cmd) { - vnet_main_t * vnet_main = vnet_get_main (); - tap_inject_main_t * im = tap_inject_get_main (); + vnet_main_t *vnet_main = vnet_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); u32 k, v; if (tap_inject_is_config_disabled ()) @@ -333,10 +330,9 @@ show_tap_inject (vlib_main_t * vm, unformat_input_t * input, } hash_foreach (k, v, im->tap_if_index_to_sw_if_index, { - vlib_cli_output (vm, "%U -> %U", - format_vnet_sw_interface_name, vnet_main, - vnet_get_sw_interface (vnet_main, v), - format_tap_inject_tap_name, k); + vlib_cli_output (vm, "%U -> %U", format_vnet_sw_interface_name, vnet_main, + vnet_get_sw_interface (vnet_main, v), + format_tap_inject_tap_name, k); }); return 0; @@ -348,31 +344,30 @@ VLIB_CLI_COMMAND (show_tap_inject_cmd, static) = { .function = show_tap_inject, }; - static clib_error_t * -tap_inject_config (vlib_main_t * vm, unformat_input_t * input) +tap_inject_config (vlib_main_t *vm, unformat_input_t *input) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "enable")) - im->flags |= TAP_INJECT_F_CONFIG_ENABLE; + im->flags |= TAP_INJECT_F_CONFIG_ENABLE; else if (unformat (input, "disable")) - im->flags |= TAP_INJECT_F_CONFIG_DISABLE; + im->flags |= TAP_INJECT_F_CONFIG_DISABLE; else if (unformat (input, "netlink-only")) - im->flags |= TAP_INJECT_F_CONFIG_NETLINK; + im->flags |= TAP_INJECT_F_CONFIG_NETLINK; else - return clib_error_return (0, "syntax error `%U'", - format_unformat_error, input); + return clib_error_return (0, "syntax error `%U'", + format_unformat_error, input); } if (tap_inject_is_config_enabled () && tap_inject_is_config_disabled ()) - return clib_error_return (0, - "tap-inject cannot be both enabled and disabled."); + return clib_error_return ( + 0, "tap-inject cannot be both enabled and disabled."); return 0; } diff --git a/extras/router-plugin/rtinject/tap_inject.h b/extras/router-plugin/rtinject/tap_inject.h index ec5121a09..6226a0aad 100644 --- a/extras/router-plugin/rtinject/tap_inject.h +++ b/extras/router-plugin/rtinject/tap_inject.h @@ -17,14 +17,15 @@ #ifndef _TAP_INJECT_H #define _TAP_INJECT_H -#include <vnet/plugin/plugin.h> #include <vnet/ip/ip.h> +#include <vnet/plugin/plugin.h> #ifndef ETHER_ADDR_LEN #define ETHER_ADDR_LEN 6 #endif -typedef struct { +typedef struct +{ /* * tap-inject can be enabled or disabled in config file or during runtime. * When disabled in config, it is not possible to enable during runtime. @@ -36,30 +37,29 @@ typedef struct { #define TAP_INJECT_F_CONFIG_ENABLE (1U << 0) #define TAP_INJECT_F_CONFIG_DISABLE (1U << 1) #define TAP_INJECT_F_CONFIG_NETLINK (1U << 2) -#define TAP_INJECT_F_ENABLED (1U << 3) +#define TAP_INJECT_F_ENABLED (1U << 3) u32 flags; - u32 * sw_if_index_to_tap_fd; - u32 * sw_if_index_to_tap_if_index; - u32 * tap_fd_to_sw_if_index; - u32 * tap_if_index_to_sw_if_index; + u32 *sw_if_index_to_tap_fd; + u32 *sw_if_index_to_tap_if_index; + u32 *tap_fd_to_sw_if_index; + u32 *tap_if_index_to_sw_if_index; - u32 * interfaces_to_enable; - u32 * interfaces_to_disable; + u32 *interfaces_to_enable; + u32 *interfaces_to_disable; - u32 * rx_file_descriptors; + u32 *rx_file_descriptors; u32 rx_node_index; u32 tx_node_index; u32 neighbor_node_index; - u32 * rx_buffers; + u32 *rx_buffers; } tap_inject_main_t; - -tap_inject_main_t * tap_inject_get_main (void); +tap_inject_main_t *tap_inject_get_main (void); void tap_inject_insert_tap (u32 sw_if_index, u32 tap_fd, u32 tap_if_index); void tap_inject_delete_tap (u32 sw_if_index); @@ -71,7 +71,7 @@ u32 tap_inject_lookup_sw_if_index_from_tap_if_index (u32 tap_if_index); static inline int tap_inject_is_enabled (void) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); return !!(im->flags & TAP_INJECT_F_ENABLED); } @@ -79,7 +79,7 @@ tap_inject_is_enabled (void) static inline int tap_inject_is_config_enabled (void) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); return !!(im->flags & TAP_INJECT_F_CONFIG_ENABLE); } @@ -87,22 +87,20 @@ tap_inject_is_config_enabled (void) static inline int tap_inject_is_config_disabled (void) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); return !!(im->flags & TAP_INJECT_F_CONFIG_DISABLE); } - /* Netlink */ void tap_inject_enable_netlink (void); - /* Tap */ -clib_error_t * tap_inject_tap_connect (vnet_hw_interface_t * hw); -clib_error_t * tap_inject_tap_disconnect (u32 sw_if_index); +clib_error_t *tap_inject_tap_connect (vnet_hw_interface_t *hw); +clib_error_t *tap_inject_tap_disconnect (u32 sw_if_index); -u8 * format_tap_inject_tap_name (u8 * s, va_list * args); +u8 *format_tap_inject_tap_name (u8 *s, va_list *args); #endif /* _TAP_INJECT_H */ diff --git a/extras/router-plugin/rtinject/tap_inject_netlink.c b/extras/router-plugin/rtinject/tap_inject_netlink.c index a221e8eaa..92ffdc6e5 100644 --- a/extras/router-plugin/rtinject/tap_inject_netlink.c +++ b/extras/router-plugin/rtinject/tap_inject_netlink.c @@ -15,25 +15,25 @@ */ #include "../devices/rtnetlink/netns.h" +#include <arpa/inet.h> +#include <linux/mpls.h> #include <vlibmemory/api.h> +#include <vnet/ethernet/arp.h> +#include <vnet/fib/fib.h> #include <vnet/ip/ip6_neighbor.h> #include <vnet/ip/lookup.h> -#include <vnet/fib/fib.h> -#include <vnet/ethernet/arp.h> -#include <arpa/inet.h> -#include <linux/mpls.h> #include <vnet/mpls/packet.h> #include "tap_inject.h" static void -add_del_addr (ns_addr_t * a, int is_del) +add_del_addr (ns_addr_t *a, int is_del) { - vlib_main_t * vm = vlib_get_main (); + vlib_main_t *vm = vlib_get_main (); u32 sw_if_index; - sw_if_index = tap_inject_lookup_sw_if_index_from_tap_if_index ( - a->ifaddr.ifa_index); + sw_if_index = + tap_inject_lookup_sw_if_index_from_tap_if_index (a->ifaddr.ifa_index); if (sw_if_index == ~0) return; @@ -41,37 +41,39 @@ add_del_addr (ns_addr_t * a, int is_del) if (a->ifaddr.ifa_family == AF_INET) { ip4_add_del_interface_address (vm, sw_if_index, - (ip4_address_t *) a->local, a->ifaddr.ifa_prefixlen, is_del); + (ip4_address_t *) a->local, + a->ifaddr.ifa_prefixlen, is_del); } else if (a->ifaddr.ifa_family == AF_INET6) { ip6_add_del_interface_address (vm, sw_if_index, - (ip6_address_t *) a->addr, a->ifaddr.ifa_prefixlen, is_del); + (ip6_address_t *) a->addr, + a->ifaddr.ifa_prefixlen, is_del); } } - -struct set_flags_args { +struct set_flags_args +{ u32 index; u8 flags; }; static void -set_flags_cb (struct set_flags_args * a) +set_flags_cb (struct set_flags_args *a) { vnet_sw_interface_set_flags (vnet_get_main (), a->index, a->flags); } static void -add_del_link (ns_link_t * l, int is_del) +add_del_link (ns_link_t *l, int is_del) { struct set_flags_args args = { ~0, 0 }; - vnet_sw_interface_t * sw; + vnet_sw_interface_t *sw; u8 flags = 0; u32 sw_if_index; - sw_if_index = tap_inject_lookup_sw_if_index_from_tap_if_index ( - l->ifi.ifi_index); + sw_if_index = + tap_inject_lookup_sw_if_index_from_tap_if_index (l->ifi.ifi_index); if (sw_if_index == ~0) return; @@ -88,19 +90,18 @@ add_del_link (ns_link_t * l, int is_del) args.index = sw_if_index; args.flags = flags; - vl_api_rpc_call_main_thread (set_flags_cb, (u8 *)&args, sizeof (args)); + vl_api_rpc_call_main_thread (set_flags_cb, (u8 *) &args, sizeof (args)); } - static void -add_del_neigh (ns_neigh_t * n, int is_del) +add_del_neigh (ns_neigh_t *n, int is_del) { - vnet_main_t * vnet_main = vnet_get_main (); - vlib_main_t * vm = vlib_get_main (); + vnet_main_t *vnet_main = vnet_get_main (); + vlib_main_t *vm = vlib_get_main (); u32 sw_if_index; - sw_if_index = tap_inject_lookup_sw_if_index_from_tap_if_index ( - n->nd.ndm_ifindex); + sw_if_index = + tap_inject_lookup_sw_if_index_from_tap_if_index (n->nd.ndm_ifindex); if (sw_if_index == ~0) return; @@ -114,57 +115,54 @@ add_del_neigh (ns_neigh_t * n, int is_del) clib_memcpy (&a.mac, n->lladdr, ETHER_ADDR_LEN); clib_memcpy (&a.ip4, n->dst, sizeof (a.ip4)); - if (n->nd.ndm_state & NUD_REACHABLE) - { - vnet_arp_set_ip4_over_ethernet (vnet_main, sw_if_index, - &a, - IP_NEIGHBOR_FLAG_NO_FIB_ENTRY); - - } + { + vnet_arp_set_ip4_over_ethernet (vnet_main, sw_if_index, &a, + IP_NEIGHBOR_FLAG_NO_FIB_ENTRY); + } else if (n->nd.ndm_state & NUD_FAILED) - { - vnet_arp_unset_ip4_over_ethernet (vnet_main, sw_if_index, &a); - } + { + vnet_arp_unset_ip4_over_ethernet (vnet_main, sw_if_index, &a); + } } else if (n->nd.ndm_family == AF_INET6) { if (n->nd.ndm_state & NUD_REACHABLE) - { - - mac_address_t * mac1; - mac1=malloc(sizeof(mac_address_t)); - memcpy (mac1, n->lladdr, ETHER_ADDR_LEN); - vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, - (ip6_address_t *) n->dst, (mac_address_t *) mac1, - IP_NEIGHBOR_FLAG_NONE); - } + { + + mac_address_t *mac1; + mac1 = malloc (sizeof (mac_address_t)); + memcpy (mac1, n->lladdr, ETHER_ADDR_LEN); + vnet_set_ip6_ethernet_neighbor ( + vm, sw_if_index, (ip6_address_t *) n->dst, (mac_address_t *) mac1, + IP_NEIGHBOR_FLAG_NONE); + } else - vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, - (ip6_address_t *) n->dst); + vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, + (ip6_address_t *) n->dst); } } - #define TAP_INJECT_HOST_ROUTE_TABLE_MAIN 254 static void -get_mpls_label_stack(struct mpls_label *addr, u32* l) +get_mpls_label_stack (struct mpls_label *addr, u32 *l) { - u32 entry = ntohl(addr[0].entry); + u32 entry = ntohl (addr[0].entry); u32 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT; - for(int i = 1; label != 0; i++) { - *l++ = label; - if(entry & MPLS_LS_S_MASK) - return; - entry = ntohl(addr[i].entry); - label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT; - } + for (int i = 1; label != 0; i++) + { + *l++ = label; + if (entry & MPLS_LS_S_MASK) + return; + entry = ntohl (addr[i].entry); + label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT; + } } static void -add_del_route (ns_route_t * r, int is_del) +add_del_route (ns_route_t *r, int is_del) { u32 sw_if_index; @@ -175,101 +173,90 @@ add_del_route (ns_route_t * r, int is_del) if (r->rtm.rtm_family == AF_INET) { - u32 stack[MPLS_STACK_DEPTH] = {0}; + u32 stack[MPLS_STACK_DEPTH] = { 0 }; fib_prefix_t prefix; - ip46_address_t nh; + ip_address_t nh; memset (&prefix, 0, sizeof (prefix)); prefix.fp_len = r->rtm.rtm_dst_len; prefix.fp_proto = FIB_PROTOCOL_IP4; clib_memcpy (&prefix.fp_addr.ip4, r->dst, sizeof (prefix.fp_addr.ip4)); - get_mpls_label_stack(r->encap, stack); + get_mpls_label_stack (r->encap, stack); memset (&nh, 0, sizeof (nh)); clib_memcpy (&nh.ip4, r->gateway, sizeof (nh.ip4)); - if(*stack == 0) - fib_table_entry_path_add (0, &prefix, FIB_SOURCE_API, - FIB_ENTRY_FLAG_NONE, prefix.fp_proto, - &nh, sw_if_index, 0, - 0 /* weight */, NULL, - FIB_ROUTE_PATH_FLAG_NONE); - else { - fib_route_path_t *rpaths = NULL, rpath; - memset(&rpath, 0, sizeof(rpath)); - rpath.frp_weight = 1; - rpath.frp_proto = DPO_PROTO_IP4; - clib_memcpy(&rpath.frp_addr.ip4, r->gateway, sizeof(rpath.frp_addr.ip4)); - rpath.frp_sw_if_index = sw_if_index; - for(int i = 0; i < MPLS_STACK_DEPTH && stack[i] != 0; i++) { - fib_mpls_label_t fib_label = {stack[i],0,0,0}; - vec_add1(rpath.frp_label_stack, fib_label); - } - vec_add1(rpaths, rpath); - fib_table_entry_path_add2(0, - &prefix, - FIB_SOURCE_API, - FIB_ENTRY_FLAG_NONE, - rpaths); - } + if (*stack == 0) + fib_table_entry_path_add ( + 0, &prefix, FIB_SOURCE_API, FIB_ENTRY_FLAG_NONE, prefix.fp_proto, + &nh, sw_if_index, 0, 0 /* weight */, NULL, FIB_ROUTE_PATH_FLAG_NONE); + else + { + fib_route_path_t *rpaths = NULL, rpath; + memset (&rpath, 0, sizeof (rpath)); + rpath.frp_weight = 1; + rpath.frp_proto = DPO_PROTO_IP4; + clib_memcpy (&rpath.frp_addr.ip4, r->gateway, + sizeof (rpath.frp_addr.ip4)); + rpath.frp_sw_if_index = sw_if_index; + for (int i = 0; i < MPLS_STACK_DEPTH && stack[i] != 0; i++) + { + fib_mpls_label_t fib_label = { stack[i], 0, 0, 0 }; + vec_add1 (rpath.frp_label_stack, fib_label); + } + vec_add1 (rpaths, rpath); + fib_table_entry_path_add2 (0, &prefix, FIB_SOURCE_API, + FIB_ENTRY_FLAG_NONE, rpaths); + } } else if (r->rtm.rtm_family == AF_INET6) { fib_prefix_t prefix; - ip46_address_t nh; + ip_address_t nh; memset (&prefix, 0, sizeof (prefix)); prefix.fp_len = r->rtm.rtm_dst_len; prefix.fp_proto = FIB_PROTOCOL_IP6; clib_memcpy (&prefix.fp_addr.ip6, r->dst, sizeof (prefix.fp_addr.ip6)); memset (&nh, 0, sizeof (nh)); clib_memcpy (&nh.ip6, r->gateway, sizeof (nh.ip6)); - fib_table_entry_path_add (0, &prefix, FIB_SOURCE_API, - FIB_ENTRY_FLAG_NONE, prefix.fp_proto, - &nh, sw_if_index, 0, - 0 /* weight */, NULL, - FIB_ROUTE_PATH_FLAG_NONE); + fib_table_entry_path_add ( + 0, &prefix, FIB_SOURCE_API, FIB_ENTRY_FLAG_NONE, prefix.fp_proto, &nh, + sw_if_index, 0, 0 /* weight */, NULL, FIB_ROUTE_PATH_FLAG_NONE); } -/* else if (r->rtm.rtm_family == AF_MPLS) - { - u32 dst_label; - get_mpls_label_stack((struct mpls_label*) r->dst, &dst_label); - struct rtvia *via = (struct rtvia*) r->via; - fib_prefix_t prefix; - fib_route_path_t *rpaths = NULL, rpath; - memset (&prefix, 0, sizeof (prefix)); - prefix.fp_len = 21; - prefix.fp_label = dst_label; - prefix.fp_proto = FIB_PROTOCOL_MPLS; - prefix.fp_payload_proto = DPO_PROTO_IP4; - memset(&rpath, 0, sizeof(rpath)); - clib_memcpy (&rpath.frp_addr.ip4, via->rtvia_addr, sizeof (rpath.frp_addr.ip4)); - rpath.frp_weight = 1; - rpath.frp_proto = DPO_PROTO_IP4; - rpath.frp_fib_index = 0; - rpath.frp_sw_if_index = sw_if_index; - vec_add1(rpaths, rpath); - fib_table_entry_path_add2(0, - &prefix, - FIB_SOURCE_API, - FIB_ENTRY_FLAG_NONE, - rpaths); - }*/ + /* else if (r->rtm.rtm_family == AF_MPLS) + { + u32 dst_label; + get_mpls_label_stack((struct mpls_label*) r->dst, &dst_label); + struct rtvia *via = (struct rtvia*) r->via; + fib_prefix_t prefix; + fib_route_path_t *rpaths = NULL, rpath; + memset (&prefix, 0, sizeof (prefix)); + prefix.fp_len = 21; + prefix.fp_label = dst_label; + prefix.fp_proto = FIB_PROTOCOL_MPLS; + prefix.fp_payload_proto = DPO_PROTO_IP4; + memset(&rpath, 0, sizeof(rpath)); + clib_memcpy (&rpath.frp_addr.ip4, via->rtvia_addr, sizeof + (rpath.frp_addr.ip4)); rpath.frp_weight = 1; rpath.frp_proto = + DPO_PROTO_IP4; rpath.frp_fib_index = 0; rpath.frp_sw_if_index = + sw_if_index; vec_add1(rpaths, rpath); fib_table_entry_path_add2(0, + &prefix, FIB_SOURCE_API, FIB_ENTRY_FLAG_NONE, rpaths); + }*/ } - static void -netns_notify_cb (void * obj, netns_type_t type, u32 flags, uword opaque) +netns_notify_cb (void *obj, netns_type_t type, u32 flags, uword opaque) { if (type == NETNS_TYPE_ADDR) - add_del_addr ((ns_addr_t *)obj, flags & NETNS_F_DEL); + add_del_addr ((ns_addr_t *) obj, flags & NETNS_F_DEL); else if (type == NETNS_TYPE_LINK) - add_del_link ((ns_link_t *)obj, flags & NETNS_F_DEL); + add_del_link ((ns_link_t *) obj, flags & NETNS_F_DEL); else if (type == NETNS_TYPE_NEIGH) - add_del_neigh ((ns_neigh_t *)obj, flags & NETNS_F_DEL); + add_del_neigh ((ns_neigh_t *) obj, flags & NETNS_F_DEL); else if (type == NETNS_TYPE_ROUTE) - add_del_route ((ns_route_t *)obj, flags & NETNS_F_DEL); + add_del_route ((ns_route_t *) obj, flags & NETNS_F_DEL); } void diff --git a/extras/router-plugin/rtinject/tap_inject_node.c b/extras/router-plugin/rtinject/tap_inject_node.c index 73c296451..b888b9223 100644 --- a/extras/router-plugin/rtinject/tap_inject_node.c +++ b/extras/router-plugin/rtinject/tap_inject_node.c @@ -15,15 +15,16 @@ */ #include "tap_inject.h" -#include <sys/uio.h> #include <netinet/in.h> +#include <sys/uio.h> #include <vnet/ethernet/arp_packet.h> vlib_node_registration_t tap_inject_rx_node; vlib_node_registration_t tap_inject_tx_node; vlib_node_registration_t tap_inject_neighbor_node; -enum { +enum +{ NEXT_NEIGHBOR_ARP, NEXT_NEIGHBOR_ICMP6, }; @@ -34,7 +35,7 @@ enum { dpo_type_t tap_inject_dpo_type; static inline void -tap_inject_tap_send_buffer (int fd, vlib_buffer_t * b) +tap_inject_tap_send_buffer (int fd, vlib_buffer_t *b) { struct iovec iov; ssize_t n_bytes; @@ -51,10 +52,10 @@ tap_inject_tap_send_buffer (int fd, vlib_buffer_t * b) } static uword -tap_inject_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f) +tap_inject_tx (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f) { - vlib_buffer_t * b; - u32 * pkts; + vlib_buffer_t *b; + u32 *pkts; u32 fd; u32 i; @@ -66,7 +67,7 @@ tap_inject_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f) fd = tap_inject_lookup_tap_fd (vnet_buffer (b)->sw_if_index[VLIB_RX]); if (fd == ~0) - continue; + continue; /* Re-wind the buffer to the start of the Ethernet header. */ vlib_buffer_advance (b, -b->current_data); @@ -85,20 +86,19 @@ VLIB_REGISTER_NODE (tap_inject_tx_node) = { .type = VLIB_NODE_TYPE_INTERNAL, }; - static uword -tap_inject_neighbor (vlib_main_t * vm, - vlib_node_runtime_t * node, vlib_frame_t * f) +tap_inject_neighbor (vlib_main_t *vm, vlib_node_runtime_t *node, + vlib_frame_t *f) { - vlib_buffer_t * b; - u32 * pkts; + vlib_buffer_t *b; + u32 *pkts; u32 fd; u32 i; u32 bi; u32 next_index = node->cached_next_index; u32 next = ~0; u32 n_left; - u32 * to_next; + u32 *to_next; pkts = vlib_frame_vector_args (f); @@ -109,10 +109,10 @@ tap_inject_neighbor (vlib_main_t * vm, fd = tap_inject_lookup_tap_fd (vnet_buffer (b)->sw_if_index[VLIB_RX]); if (fd == ~0) - { - vlib_buffer_free (vm, &bi, 1); - continue; - } + { + vlib_buffer_free (vm, &bi, 1); + continue; + } /* Re-wind the buffer to the start of the Ethernet header. */ vlib_buffer_advance (b, -b->current_data); @@ -121,32 +121,32 @@ tap_inject_neighbor (vlib_main_t * vm, /* Send the buffer to a neighbor node too? */ { - ethernet_header_t * eth = vlib_buffer_get_current (b); - u16 ether_type = htons (eth->type); - - if (ether_type == ETHERNET_TYPE_ARP) - { - ethernet_arp_header_t * arp = (void *)(eth + 1); - - if (arp->opcode == ntohs (ETHERNET_ARP_OPCODE_reply)) - next = NEXT_NEIGHBOR_ARP; - } - else if (ether_type == ETHERNET_TYPE_IP6) - { - ip6_header_t * ip = (void *)(eth + 1); - icmp46_header_t * icmp = (void *)(ip + 1); - - if (ip->protocol == IP_PROTOCOL_ICMP6 && - icmp->type == ICMP6_neighbor_advertisement) - next = NEXT_NEIGHBOR_ICMP6; - } + ethernet_header_t *eth = vlib_buffer_get_current (b); + u16 ether_type = htons (eth->type); + + if (ether_type == ETHERNET_TYPE_ARP) + { + ethernet_arp_header_t *arp = (void *) (eth + 1); + + if (arp->opcode == ntohs (ETHERNET_ARP_OPCODE_reply)) + next = NEXT_NEIGHBOR_ARP; + } + else if (ether_type == ETHERNET_TYPE_IP6) + { + ip6_header_t *ip = (void *) (eth + 1); + icmp46_header_t *icmp = (void *) (ip + 1); + + if (ip->protocol == IP_PROTOCOL_ICMP6 && + icmp->type == ICMP6_neighbor_advertisement) + next = NEXT_NEIGHBOR_ICMP6; + } } if (next == ~0) - { - vlib_buffer_free (vm, &bi, 1); - continue; - } + { + vlib_buffer_free (vm, &bi, 1); + continue; + } /* ARP and ICMP6 expect to start processing after the Ethernet header. */ vlib_buffer_advance (b, sizeof (ethernet_header_t)); @@ -156,39 +156,41 @@ tap_inject_neighbor (vlib_main_t * vm, *(to_next++) = bi; --n_left; - vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, - n_left, bi, next); + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left, + bi, next); vlib_put_next_frame (vm, node, next_index, n_left); } return f->n_vectors; } -VLIB_REGISTER_NODE (tap_inject_neighbor_node) = { - .function = tap_inject_neighbor, - .name = "tap-inject-neighbor", - .vector_size = sizeof (u32), - .type = VLIB_NODE_TYPE_INTERNAL, - .n_next_nodes = 2, - .next_nodes = { - [NEXT_NEIGHBOR_ARP] = "arp-input", - [NEXT_NEIGHBOR_ICMP6] = "icmp6-neighbor-solicitation", - }, +VLIB_REGISTER_NODE(tap_inject_neighbor_node) = { + .function = tap_inject_neighbor, + .name = "tap-inject-neighbor", + .vector_size = sizeof(u32), + .type = VLIB_NODE_TYPE_INTERNAL, + .n_next_nodes = 2, + .next_nodes = + { + [NEXT_NEIGHBOR_ARP] = "arp-input", + [NEXT_NEIGHBOR_ICMP6] = "icmp6-neighbor-solicitation", + }, }; - #define MTU 1500 -#define MTU_BUFFERS ((MTU + vlib_buffer_get_default_data_size(vm) - 1) / vlib_buffer_get_default_data_size(vm)) +#define MTU_BUFFERS \ + ((MTU + vlib_buffer_get_default_data_size (vm) - 1) / \ + vlib_buffer_get_default_data_size (vm)) #define NUM_BUFFERS_TO_ALLOC 32 static inline uword -tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) +tap_rx (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f, int fd) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); u32 sw_if_index; struct iovec iov[MTU_BUFFERS]; u32 bi[MTU_BUFFERS]; - vlib_buffer_t * b; + vlib_buffer_t *b; ssize_t n_bytes; ssize_t n_bytes_left; u32 i, j; @@ -202,25 +204,23 @@ tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) { u32 len = vec_len (im->rx_buffers); - - u8 index = vlib_buffer_pool_get_default_for_numa (vm,0); - len = vlib_buffer_alloc_from_pool(vm, - &im->rx_buffers[len], NUM_BUFFERS_TO_ALLOC, - index); + u8 index = vlib_buffer_pool_get_default_for_numa (vm, 0); + len = vlib_buffer_alloc_from_pool (vm, &im->rx_buffers[len], + NUM_BUFFERS_TO_ALLOC, index); _vec_len (im->rx_buffers) += len; if (vec_len (im->rx_buffers) < MTU_BUFFERS) - { - clib_warning ("failed to allocate buffers"); - return 0; - } + { + clib_warning ("failed to allocate buffers"); + return 0; + } } /* Fill buffers from the end of the list to make it easier to resize. */ for (i = 0, j = vec_len (im->rx_buffers) - 1; i < MTU_BUFFERS; ++i, --j) { - vlib_buffer_t * b; + vlib_buffer_t *b; bi[i] = im->rx_buffers[j]; @@ -253,7 +253,8 @@ tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) b->current_length = n_bytes; /* If necessary, configure any remaining buffers in the chain. */ - for (i = 1; n_bytes_left > 0; ++i, n_bytes_left -= VLIB_BUFFER_DEFAULT_DATA_SIZE) + for (i = 1; n_bytes_left > 0; + ++i, n_bytes_left -= VLIB_BUFFER_DEFAULT_DATA_SIZE) { b = vlib_get_buffer (vm, bi[i - 1]); b->current_length = VLIB_BUFFER_DEFAULT_DATA_SIZE; @@ -268,9 +269,9 @@ tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) /* Get the packet to the output node. */ { - vnet_hw_interface_t * hw; - vlib_frame_t * new_frame; - u32 * to_next; + vnet_hw_interface_t *hw; + vlib_frame_t *new_frame; + u32 *to_next; hw = vnet_get_hw_interface (vnet_get_main (), sw_if_index); @@ -286,20 +287,20 @@ tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) } static uword -tap_inject_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f) +tap_inject_rx (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f) { - tap_inject_main_t * im = tap_inject_get_main (); - u32 * fd; + tap_inject_main_t *im = tap_inject_get_main (); + u32 *fd; uword count = 0; vec_foreach (fd, im->rx_file_descriptors) { if (tap_rx (vm, node, f, *fd) != 1) - { - clib_warning ("rx failed"); - count = 0; - break; - } + { + clib_warning ("rx failed"); + count = 0; + break; + } ++count; } @@ -320,7 +321,7 @@ VLIB_REGISTER_NODE (tap_inject_rx_node) = { * @brief no-op lock function. */ static void -tap_inject_dpo_lock (dpo_id_t * dpo) +tap_inject_dpo_lock (dpo_id_t *dpo) { } @@ -328,12 +329,12 @@ tap_inject_dpo_lock (dpo_id_t * dpo) * @brief no-op unlock function. */ static void -tap_inject_dpo_unlock (dpo_id_t * dpo) +tap_inject_dpo_unlock (dpo_id_t *dpo) { } u8 * -format_tap_inject_dpo (u8 * s, va_list * args) +format_tap_inject_dpo (u8 *s, va_list *args) { return (format (s, "tap-inject:[%d]", 0)); } @@ -355,15 +356,16 @@ const static char *const *const tap_inject_nodes[DPO_PROTO_NUM] = { }; static clib_error_t * -tap_inject_init (vlib_main_t * vm) +tap_inject_init (vlib_main_t *vm) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); im->rx_node_index = tap_inject_rx_node.index; im->tx_node_index = tap_inject_tx_node.index; im->neighbor_node_index = tap_inject_neighbor_node.index; - tap_inject_dpo_type = dpo_register_new_type (&tap_inject_vft, tap_inject_nodes); + tap_inject_dpo_type = + dpo_register_new_type (&tap_inject_vft, tap_inject_nodes); vec_alloc (im->rx_buffers, NUM_BUFFERS_TO_ALLOC); vec_reset_length (im->rx_buffers); diff --git a/extras/router-plugin/rtinject/tap_inject_tap.c b/extras/router-plugin/rtinject/tap_inject_tap.c index a3ec9ffef..44c494347 100644 --- a/extras/router-plugin/rtinject/tap_inject_tap.c +++ b/extras/router-plugin/rtinject/tap_inject_tap.c @@ -17,23 +17,23 @@ #include "tap_inject.h" #include <fcntl.h> -#include <sys/ioctl.h> -#include <sys/socket.h> #include <linux/if.h> #include <linux/if_arp.h> #include <linux/if_ether.h> #include <linux/if_tun.h> #include <netinet/in.h> +#include <sys/ioctl.h> +#include <sys/socket.h> #include <vnet/unix/tuntap.h> +#include <hicn/util/sstrncpy.h> #include <vlib/unix/unix.h> - static clib_error_t * -tap_inject_tap_read (clib_file_t * f) +tap_inject_tap_read (clib_file_t *f) { - vlib_main_t * vm = vlib_get_main (); - tap_inject_main_t * im = tap_inject_get_main (); + vlib_main_t *vm = vlib_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); vec_add1 (im->rx_file_descriptors, f->file_descriptor); @@ -45,16 +45,16 @@ tap_inject_tap_read (clib_file_t * f) #define TAP_INJECT_TAP_BASE_NAME "vpp" clib_error_t * -tap_inject_tap_connect (vnet_hw_interface_t * hw) +tap_inject_tap_connect (vnet_hw_interface_t *hw) { - vnet_main_t * vnet_main = vnet_get_main (); - vnet_sw_interface_t * sw = vnet_get_sw_interface (vnet_main, hw->hw_if_index); + vnet_main_t *vnet_main = vnet_get_main (); + vnet_sw_interface_t *sw = vnet_get_sw_interface (vnet_main, hw->hw_if_index); static const int one = 1; int fd; struct ifreq ifr; clib_file_t template; u32 tap_fd; - u8 * name; + u8 *name; memset (&ifr, 0, sizeof (ifr)); memset (&template, 0, sizeof (template)); @@ -64,15 +64,16 @@ tap_inject_tap_connect (vnet_hw_interface_t * hw) /* Create the tap. */ tap_fd = open ("/dev/net/tun", O_RDWR); - if ((int)tap_fd < 0) + if ((int) tap_fd < 0) return clib_error_return (0, "failed to open tun device"); name = format (0, TAP_INJECT_TAP_BASE_NAME "%u%c", hw->hw_instance, 0); - strncpy (ifr.ifr_name, (char *) name, sizeof (ifr.ifr_name) - 1); + strcpy_s (ifr.ifr_name, IFNAMSIZ, (char *) name); + ifr.ifr_name[IFNAMSIZ - 1] = '\0'; ifr.ifr_flags = IFF_TAP | IFF_NO_PI; - if (ioctl (tap_fd, TUNSETIFF, (void *)&ifr) < 0) + if (ioctl (tap_fd, TUNSETIFF, (void *) &ifr) < 0) { close (tap_fd); return clib_error_return (0, "failed to create tap"); @@ -142,9 +143,8 @@ tap_inject_tap_disconnect (u32 sw_if_index) return 0; } - u8 * -format_tap_inject_tap_name (u8 * s, va_list * args) +format_tap_inject_tap_name (u8 *s, va_list *args) { int fd; struct ifreq ifr; |