From 30bab1d3f9ab06670fbef2c7c6a658e7b77f7738 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Mon, 23 Aug 2021 23:58:03 +0200 Subject: [PATCH] Our first Netlink syncer! Add lcpng_nl_sync.c that will house these functions. Their purpose is to take state learned from netlink messages, and apply that state to VPP. Some rearranging/plumbing was necessary to get logging to be visible in this new source file. Then, we add lcp_nl_neigh_add() and _del() which look up the LIP, convert the lladdr and ip address from Netlink into VPP variants, and then add or remove the ip4/ip6 neighbor adjacency. --- CMakeLists.txt | 1 + lcpng_netlink.c | 9 +++- lcpng_netlink.h | 9 ++-- lcpng_nl_sync.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 lcpng_nl_sync.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c2460bf..1f2149a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ add_vpp_plugin(lcpng_if add_vpp_plugin(lcpng_nl SOURCES lcpng_netlink.c + lcpng_nl_sync.c LINK_LIBRARIES lcpng diff --git a/lcpng_netlink.c b/lcpng_netlink.c index 57eb69b..7e6dbf4 100644 --- a/lcpng_netlink.c +++ b/lcpng_netlink.c @@ -40,7 +40,10 @@ #include #include -static lcp_nl_main_t lcp_nl_main = { +static void lcp_nl_open_socket (u8 *ns); +static void lcp_nl_close_socket (void); + +lcp_nl_main_t lcp_nl_main = { .rx_buf_size = NL_RX_BUF_SIZE_DEF, .tx_buf_size = NL_TX_BUF_SIZE_DEF, .batch_size = NL_BATCH_SIZE_DEF, @@ -200,6 +203,10 @@ lcp_nl_dispatch (struct nl_object *obj, void *arg) /* Here is where we'll sync the netlink messages into VPP */ switch (nl_object_get_msgtype (obj)) { + case RTM_NEWNEIGH: + return lcp_nl_neigh_add ((struct rtnl_neigh *) obj); + case RTM_DELNEIGH: + return lcp_nl_neigh_del ((struct rtnl_neigh *) obj); default: NL_WARN ("dispatch: ignored %U", format_nl_object, obj); break; diff --git a/lcpng_netlink.h b/lcpng_netlink.h index 989a5ab..9ba384a 100644 --- a/lcpng_netlink.h +++ b/lcpng_netlink.h @@ -75,12 +75,15 @@ typedef struct lcp_nl_main u32 batch_delay_ms; } lcp_nl_main_t; - -static void lcp_nl_open_socket (u8 *ns); -static void lcp_nl_close_socket (void); +extern lcp_nl_main_t lcp_nl_main; u8 *format_nl_object (u8 *s, va_list *args); +/* Functions from lcpng_nl_sync.c + */ +void lcp_nl_neigh_add (struct rtnl_neigh *rn); +void lcp_nl_neigh_del (struct rtnl_neigh *rn); + /* * fd.io coding-style-patch-verification: ON * diff --git a/lcpng_nl_sync.c b/lcpng_nl_sync.c new file mode 100644 index 0000000..7dcbad2 --- /dev/null +++ b/lcpng_nl_sync.c @@ -0,0 +1,138 @@ +/* Hey Emacs use -*- mode: C -*- */ +/* + * Copyright 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 +#include + +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include + +#ifndef NUD_VALID +#define NUD_VALID \ + (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE | \ + NUD_DELAY) +#endif + +static void +lcp_nl_mk_ip_addr (const struct nl_addr *rna, ip_address_t *ia) +{ + ip_address_reset (ia); + ip_address_set (ia, nl_addr_get_binary_addr (rna), + nl_addr_get_family (rna) == AF_INET6 ? AF_IP6 : AF_IP4); +} + +static void +lcp_nl_mk_mac_addr (const struct nl_addr *rna, mac_address_t *mac) +{ + mac_address_from_bytes (mac, nl_addr_get_binary_addr (rna)); +} + +void +lcp_nl_neigh_add (struct rtnl_neigh *rn) +{ + lcp_itf_pair_t *lip; + struct nl_addr *ll; + ip_address_t nh; + int state; + + NL_DBG ("neigh_add: netlink %U", format_nl_object, rn); + + if (!(lip = lcp_itf_pair_get ( + lcp_itf_pair_find_by_vif (rtnl_neigh_get_ifindex (rn))))) + { + NL_WARN ("neigh_add: no LCP for %U ", format_nl_object, rn); + return; + } + + lcp_nl_mk_ip_addr (rtnl_neigh_get_dst (rn), &nh); + ll = rtnl_neigh_get_lladdr (rn); + state = rtnl_neigh_get_state (rn); + + if (ll && (state & NUD_VALID)) + { + mac_address_t mac; + ip_neighbor_flags_t flags; + int rv; + + lcp_nl_mk_mac_addr (ll, &mac); + + if (state & (NUD_NOARP | NUD_PERMANENT)) + flags = IP_NEIGHBOR_FLAG_STATIC; + else + flags = IP_NEIGHBOR_FLAG_DYNAMIC; + + rv = ip_neighbor_add (&nh, &mac, lip->lip_phy_sw_if_index, flags, NULL); + + if (rv) + { + NL_ERROR ("neigh_add: Failed %U lladdr %U iface %U", + format_ip_address, &nh, format_mac_address, &mac, + format_vnet_sw_if_index_name, vnet_get_main (), + lip->lip_phy_sw_if_index); + } + else + { + NL_NOTICE ("neigh_add: Added %U lladdr %U iface %U", + format_ip_address, &nh, format_mac_address, &mac, + format_vnet_sw_if_index_name, vnet_get_main (), + lip->lip_phy_sw_if_index); + } + } +} + +void +lcp_nl_neigh_del (struct rtnl_neigh *rn) +{ + ip_address_t nh; + int rv; + NL_DBG ("neigh_del: netlink %U", format_nl_object, rn); + + lcp_itf_pair_t *lip; + if (!(lip = lcp_itf_pair_get ( + lcp_itf_pair_find_by_vif (rtnl_neigh_get_ifindex (rn))))) + { + NL_WARN ("neigh_del: no LCP for %U ", format_nl_object, rn); + return; + } + + lcp_nl_mk_ip_addr (rtnl_neigh_get_dst (rn), &nh); + rv = ip_neighbor_del (&nh, lip->lip_phy_sw_if_index); + + if (rv) + { + NL_ERROR ("neigh_del: Failed %U iface %U", format_ip_address, &nh, + format_vnet_sw_if_index_name, vnet_get_main (), + lip->lip_phy_sw_if_index); + } + else + { + NL_NOTICE ("neigh_del: Deleted %U iface %U", format_ip_address, &nh, + format_vnet_sw_if_index_name, vnet_get_main (), + lip->lip_phy_sw_if_index); + } +}