Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
9292 lines (7869 sloc) 232 KB
/*
* Zebra EVPN for VxLAN code
* Copyright (C) 2016, 2017 Cumulus Networks, Inc.
*
* This file is part of FRR.
*
* FRR is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* FRR is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FRR; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#include "hash.h"
#include "if.h"
#include "jhash.h"
#include "linklist.h"
#include "log.h"
#include "memory.h"
#include "prefix.h"
#include "stream.h"
#include "table.h"
#include "vlan.h"
#include "vxlan.h"
#ifdef GNU_LINUX
#include <linux/neighbour.h>
#endif
#include "zebra/zebra_router.h"
#include "zebra/debug.h"
#include "zebra/interface.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/rt_netlink.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_l2.h"
#include "zebra/zebra_memory.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/zebra_vxlan_private.h"
#include "zebra/zebra_router.h"
DEFINE_MTYPE_STATIC(ZEBRA, HOST_PREFIX, "host prefix");
DEFINE_MTYPE_STATIC(ZEBRA, ZVNI, "VNI hash");
DEFINE_MTYPE_STATIC(ZEBRA, ZL3VNI, "L3 VNI hash");
DEFINE_MTYPE_STATIC(ZEBRA, ZVNI_VTEP, "VNI remote VTEP");
DEFINE_MTYPE_STATIC(ZEBRA, MAC, "VNI MAC");
DEFINE_MTYPE_STATIC(ZEBRA, NEIGH, "VNI Neighbor");
/* definitions */
/* static function declarations */
static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
uint16_t cmd);
static void zvni_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json);
static void zvni_print_neigh_hash(struct hash_bucket *bucket, void *ctxt);
static void zvni_print_dad_neigh_hash(struct hash_bucket *bucket, void *ctxt);
static void zvni_print_neigh_hash_all_vni(struct hash_bucket *bucket,
void **args);
static void zl3vni_print_nh(zebra_neigh_t *n, struct vty *vty,
json_object *json);
static void zl3vni_print_rmac(zebra_mac_t *zrmac, struct vty *vty,
json_object *json);
static void zvni_print_mac(zebra_mac_t *mac, void *ctxt, json_object *json);
static void zvni_print_mac_hash(struct hash_bucket *bucket, void *ctxt);
static void zvni_print_mac_hash_all_vni(struct hash_bucket *bucket, void *ctxt);
static void zvni_print(zebra_vni_t *zvni, void **ctxt);
static void zvni_print_hash(struct hash_bucket *bucket, void *ctxt[]);
static int zvni_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr,
struct ipaddr *ip, uint8_t flags,
uint32_t seq, int state, uint16_t cmd);
static unsigned int neigh_hash_keymake(void *p);
static void *zvni_neigh_alloc(void *p);
static zebra_neigh_t *zvni_neigh_add(zebra_vni_t *zvni, struct ipaddr *ip,
struct ethaddr *mac);
static int zvni_neigh_del(zebra_vni_t *zvni, zebra_neigh_t *n);
static void zvni_neigh_del_from_vtep(zebra_vni_t *zvni, int uninstall,
struct in_addr *r_vtep_ip);
static void zvni_neigh_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
uint32_t flags);
static zebra_neigh_t *zvni_neigh_lookup(zebra_vni_t *zvni, struct ipaddr *ip);
static int zvni_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
struct ethaddr *macaddr,
uint8_t flags, uint32_t seq);
static int zvni_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
struct ethaddr *macaddr,
uint8_t flags, int state);
static int zvni_neigh_install(zebra_vni_t *zvni, zebra_neigh_t *n);
static int zvni_neigh_uninstall(zebra_vni_t *zvni, zebra_neigh_t *n);
static int zvni_neigh_probe(zebra_vni_t *zvni, zebra_neigh_t *n);
static zebra_vni_t *zvni_from_svi(struct interface *ifp,
struct interface *br_if);
static struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if);
/* l3-vni next-hop neigh related APIs */
static zebra_neigh_t *zl3vni_nh_lookup(zebra_l3vni_t *zl3vni,
struct ipaddr *ip);
static void *zl3vni_nh_alloc(void *p);
static zebra_neigh_t *zl3vni_nh_add(zebra_l3vni_t *zl3vni,
struct ipaddr *vtep_ip,
struct ethaddr *rmac);
static int zl3vni_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
static int zl3vni_nh_install(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
static int zl3vni_nh_uninstall(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
/* l3-vni rmac related APIs */
static void zl3vni_print_rmac_hash(struct hash_bucket *, void *);
static zebra_mac_t *zl3vni_rmac_lookup(zebra_l3vni_t *zl3vni,
struct ethaddr *rmac);
static void *zl3vni_rmac_alloc(void *p);
static zebra_mac_t *zl3vni_rmac_add(zebra_l3vni_t *zl3vni,
struct ethaddr *rmac);
static int zl3vni_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
static int zl3vni_rmac_install(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
static int zl3vni_rmac_uninstall(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
/* l3-vni related APIs*/
static zebra_l3vni_t *zl3vni_lookup(vni_t vni);
static void *zl3vni_alloc(void *p);
static zebra_l3vni_t *zl3vni_add(vni_t vni, vrf_id_t vrf_id);
static int zl3vni_del(zebra_l3vni_t *zl3vni);
static zebra_l3vni_t *zl3vni_from_vrf(vrf_id_t);
static struct interface *zl3vni_map_to_svi_if(zebra_l3vni_t *zl3vni);
static struct interface *zl3vni_map_to_vxlan_if(zebra_l3vni_t *zl3vni);
static void zebra_vxlan_process_l3vni_oper_up(zebra_l3vni_t *zl3vni);
static void zebra_vxlan_process_l3vni_oper_down(zebra_l3vni_t *zl3vni);
static unsigned int mac_hash_keymake(void *p);
static bool mac_cmp(const void *p1, const void *p2);
static void *zvni_mac_alloc(void *p);
static zebra_mac_t *zvni_mac_add(zebra_vni_t *zvni, struct ethaddr *macaddr);
static int zvni_mac_del(zebra_vni_t *zvni, zebra_mac_t *mac);
static void zvni_mac_del_from_vtep(zebra_vni_t *zvni, int uninstall,
struct in_addr *r_vtep_ip);
static void zvni_mac_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
uint32_t flags);
static zebra_mac_t *zvni_mac_lookup(zebra_vni_t *zvni, struct ethaddr *macaddr);
static int zvni_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr,
uint8_t flags, uint32_t seq);
static int zvni_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr);
static zebra_vni_t *zvni_map_vlan(struct interface *ifp,
struct interface *br_if, vlanid_t vid);
static int zvni_mac_install(zebra_vni_t *zvni, zebra_mac_t *mac);
static int zvni_mac_uninstall(zebra_vni_t *zvni, zebra_mac_t *mac);
static void zvni_install_mac_hash(struct hash_bucket *bucket, void *ctxt);
static unsigned int vni_hash_keymake(void *p);
static void *zvni_alloc(void *p);
static zebra_vni_t *zvni_lookup(vni_t vni);
static zebra_vni_t *zvni_add(vni_t vni);
static int zvni_del(zebra_vni_t *zvni);
static int zvni_send_add_to_client(zebra_vni_t *zvni);
static int zvni_send_del_to_client(vni_t vni);
static void zvni_build_hash_table(void);
static int zvni_vtep_match(struct in_addr *vtep_ip, zebra_vtep_t *zvtep);
static zebra_vtep_t *zvni_vtep_find(zebra_vni_t *zvni, struct in_addr *vtep_ip);
static zebra_vtep_t *zvni_vtep_add(zebra_vni_t *zvni, struct in_addr *vtep_ip);
static int zvni_vtep_del(zebra_vni_t *zvni, zebra_vtep_t *zvtep);
static int zvni_vtep_del_all(zebra_vni_t *zvni, int uninstall);
static int zvni_vtep_install(zebra_vni_t *zvni, struct in_addr *vtep_ip);
static int zvni_vtep_uninstall(zebra_vni_t *zvni, struct in_addr *vtep_ip);
static int zvni_del_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni);
static int zvni_add_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni);
static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni,
struct ethaddr *macaddr, struct ipaddr *ip);
static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni,
struct ipaddr *ip);
struct interface *zebra_get_vrr_intf_for_svi(struct interface *ifp);
static int advertise_gw_macip_enabled(zebra_vni_t *zvni);
static int advertise_svi_macip_enabled(zebra_vni_t *zvni);
static int zebra_vxlan_ip_inherit_dad_from_mac(struct zebra_vrf *zvrf,
zebra_mac_t *old_zmac,
zebra_mac_t *new_zmac,
zebra_neigh_t *nbr);
static int remote_neigh_count(zebra_mac_t *zmac);
static void zvni_deref_ip2mac(zebra_vni_t *zvni, zebra_mac_t *mac);
static int zebra_vxlan_dad_mac_auto_recovery_exp(struct thread *t);
static int zebra_vxlan_dad_ip_auto_recovery_exp(struct thread *t);
static void zebra_vxlan_dup_addr_detect_for_neigh(struct zebra_vrf *zvrf,
zebra_neigh_t *nbr,
struct in_addr vtep_ip,
bool do_dad,
bool *is_dup_detect,
bool is_local);
static void zebra_vxlan_dup_addr_detect_for_mac(struct zebra_vrf *zvrf,
zebra_mac_t *mac,
struct in_addr vtep_ip,
bool do_dad,
bool *is_dup_detect,
bool is_local);
/* Private functions */
static int host_rb_entry_compare(const struct host_rb_entry *hle1,
const struct host_rb_entry *hle2)
{
if (hle1->p.family < hle2->p.family)
return -1;
if (hle1->p.family > hle2->p.family)
return 1;
if (hle1->p.prefixlen < hle2->p.prefixlen)
return -1;
if (hle1->p.prefixlen > hle2->p.prefixlen)
return 1;
if (hle1->p.family == AF_INET) {
if (hle1->p.u.prefix4.s_addr < hle2->p.u.prefix4.s_addr)
return -1;
if (hle1->p.u.prefix4.s_addr > hle2->p.u.prefix4.s_addr)
return 1;
return 0;
} else if (hle1->p.family == AF_INET6) {
return memcmp(&hle1->p.u.prefix6, &hle2->p.u.prefix6,
IPV6_MAX_BYTELEN);
} else {
zlog_debug("%s: Unexpected family type: %d",
__PRETTY_FUNCTION__, hle1->p.family);
return 0;
}
}
RB_GENERATE(host_rb_tree_entry, host_rb_entry, hl_entry, host_rb_entry_compare);
static uint32_t rb_host_count(struct host_rb_tree_entry *hrbe)
{
struct host_rb_entry *hle;
uint32_t count = 0;
RB_FOREACH (hle, host_rb_tree_entry, hrbe)
count++;
return count;
}
/*
* Return number of valid MACs in a VNI's MAC hash table - all
* remote MACs and non-internal (auto) local MACs count.
*/
static uint32_t num_valid_macs(zebra_vni_t *zvni)
{
unsigned int i;
uint32_t num_macs = 0;
struct hash *hash;
struct hash_bucket *hb;
zebra_mac_t *mac;
hash = zvni->mac_table;
if (!hash)
return num_macs;
for (i = 0; i < hash->size; i++) {
for (hb = hash->index[i]; hb; hb = hb->next) {
mac = (zebra_mac_t *)hb->data;
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
|| CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
|| !CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
num_macs++;
}
}
return num_macs;
}
static uint32_t num_dup_detected_macs(zebra_vni_t *zvni)
{
unsigned int i;
uint32_t num_macs = 0;
struct hash *hash;
struct hash_bucket *hb;
zebra_mac_t *mac;
hash = zvni->mac_table;
if (!hash)
return num_macs;
for (i = 0; i < hash->size; i++) {
for (hb = hash->index[i]; hb; hb = hb->next) {
mac = (zebra_mac_t *)hb->data;
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
num_macs++;
}
}
return num_macs;
}
static uint32_t num_dup_detected_neighs(zebra_vni_t *zvni)
{
unsigned int i;
uint32_t num_neighs = 0;
struct hash *hash;
struct hash_bucket *hb;
zebra_neigh_t *nbr;
hash = zvni->neigh_table;
if (!hash)
return num_neighs;
for (i = 0; i < hash->size; i++) {
for (hb = hash->index[i]; hb; hb = hb->next) {
nbr = (zebra_neigh_t *)hb->data;
if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
num_neighs++;
}
}
return num_neighs;
}
static int advertise_gw_macip_enabled(zebra_vni_t *zvni)
{
struct zebra_vrf *zvrf;
zvrf = vrf_info_lookup(VRF_DEFAULT);
if (zvrf && zvrf->advertise_gw_macip)
return 1;
if (zvni && zvni->advertise_gw_macip)
return 1;
return 0;
}
static int advertise_svi_macip_enabled(zebra_vni_t *zvni)
{
struct zebra_vrf *zvrf;
zvrf = vrf_info_lookup(VRF_DEFAULT);
if (zvrf && zvrf->advertise_svi_macip)
return 1;
if (zvni && zvni->advertise_svi_macip)
return 1;
return 0;
}
/* As part Duplicate Address Detection (DAD) for IP mobility
* MAC binding changes, ensure to inherit duplicate flag
* from MAC.
*/
static int zebra_vxlan_ip_inherit_dad_from_mac(struct zebra_vrf *zvrf,
zebra_mac_t *old_zmac,
zebra_mac_t *new_zmac,
zebra_neigh_t *nbr)
{
bool is_old_mac_dup = false;
bool is_new_mac_dup = false;
if (!zvrf->dup_addr_detect)
return 0;
/* Check old or new MAC is detected as duplicate
* mark this neigh as duplicate
*/
if (old_zmac)
is_old_mac_dup = CHECK_FLAG(old_zmac->flags,
ZEBRA_MAC_DUPLICATE);
if (new_zmac)
is_new_mac_dup = CHECK_FLAG(new_zmac->flags,
ZEBRA_MAC_DUPLICATE);
/* Old and/or new MAC can be in duplicate state,
* based on that IP/Neigh Inherits the flag.
* If New MAC is marked duplicate, inherit to the IP.
* If old MAC is duplicate but new MAC is not, clear
* duplicate flag for IP and reset detection params
* and let IP DAD retrigger.
*/
if (is_new_mac_dup && !CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
/* Capture Duplicate detection time */
nbr->dad_dup_detect_time = monotime(NULL);
return 1;
} else if (is_old_mac_dup && !is_new_mac_dup) {
UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
nbr->dad_count = 0;
nbr->detect_start_time.tv_sec = 0;
nbr->detect_start_time.tv_usec = 0;
}
return 0;
}
static void zebra_vxlan_dup_addr_detect_for_mac(struct zebra_vrf *zvrf,
zebra_mac_t *mac,
struct in_addr vtep_ip,
bool do_dad,
bool *is_dup_detect,
bool is_local)
{
zebra_neigh_t *nbr;
struct listnode *node = NULL;
struct timeval elapsed = {0, 0};
char buf[ETHER_ADDR_STRLEN];
char buf1[INET6_ADDRSTRLEN];
bool reset_params = false;
if (!(zvrf->dup_addr_detect && do_dad))
return;
/* MAC is detected as duplicate,
* Local MAC event -> hold on advertising to BGP.
* Remote MAC event -> hold on installing it.
*/
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"%s: duplicate addr MAC %s flags 0x%x skip update to client, learn count %u recover time %u",
__PRETTY_FUNCTION__,
prefix_mac2str(&mac->macaddr, buf,
sizeof(buf)),
mac->flags, mac->dad_count,
zvrf->dad_freeze_time);
/* For duplicate MAC do not update
* client but update neigh due to
* this MAC update.
*/
if (zvrf->dad_freeze)
*is_dup_detect = true;
return;
}
/* Check if detection time (M-secs) expired.
* Reset learn count and detection start time.
*/
monotime_since(&mac->detect_start_time, &elapsed);
reset_params = (elapsed.tv_sec > zvrf->dad_time);
if (is_local && !reset_params) {
/* RFC-7432: A PE/VTEP that detects a MAC mobility
* event via LOCAL learning starts an M-second timer.
*
* NOTE: This is the START of the probe with count is
* 0 during LOCAL learn event.
* (mac->dad_count == 0 || elapsed.tv_sec >= zvrf->dad_time)
*/
reset_params = !mac->dad_count;
}
if (reset_params) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"%s: duplicate addr MAC %s flags 0x%x detection time passed, reset learn count %u"
, __PRETTY_FUNCTION__,
prefix_mac2str(&mac->macaddr, buf,
sizeof(buf)),
mac->flags, mac->dad_count);
mac->dad_count = 0;
/* Start dup. addr detection (DAD) start time,
* ONLY during LOCAL learn.
*/
if (is_local)
monotime(&mac->detect_start_time);
} else if (!is_local) {
/* For REMOTE MAC, increment detection count
* ONLY while in probe window, once window passed,
* next local learn event should trigger DAD.
*/
mac->dad_count++;
}
/* For LOCAL MAC learn event, once count is reset above via either
* initial/start detection time or passed the probe time, the count
* needs to be incremented.
*/
if (is_local)
mac->dad_count++;
if (mac->dad_count >= zvrf->dad_max_moves) {
flog_warn(EC_ZEBRA_DUP_MAC_DETECTED,
"VNI %u: MAC %s detected as duplicate during %s VTEP %s",
mac->zvni->vni,
prefix_mac2str(&mac->macaddr, buf, sizeof(buf)),
is_local ? "local update, last" :
"remote update, from", inet_ntoa(vtep_ip));
SET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
/* Capture Duplicate detection time */
mac->dad_dup_detect_time = monotime(NULL);
/* Mark all IPs/Neighs as duplicate
* associcated with this MAC
*/
for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
/* Ony Mark IPs which are Local */
if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
continue;
SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
nbr->dad_dup_detect_time = monotime(NULL);
flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
"VNI %u: MAC %s IP %s detected as duplicate during %s update, inherit duplicate from MAC",
mac->zvni->vni,
prefix_mac2str(&mac->macaddr,
buf, sizeof(buf)),
ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
is_local ? "local" : "remote");
}
/* Start auto recovery timer for this MAC */
THREAD_OFF(mac->dad_mac_auto_recovery_timer);
if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"%s: duplicate addr MAC %s flags 0x%x auto recovery time %u start"
, __PRETTY_FUNCTION__,
prefix_mac2str(&mac->macaddr, buf,
sizeof(buf)),
mac->flags, zvrf->dad_freeze_time);
thread_add_timer(zrouter.master,
zebra_vxlan_dad_mac_auto_recovery_exp,
mac, zvrf->dad_freeze_time,
&mac->dad_mac_auto_recovery_timer);
}
/* In case of local update, do not inform to client (BGPd),
* upd_neigh for neigh sequence change.
*/
if (zvrf->dad_freeze)
*is_dup_detect = true;
}
}
static void zebra_vxlan_dup_addr_detect_for_neigh(struct zebra_vrf *zvrf,
zebra_neigh_t *nbr,
struct in_addr vtep_ip,
bool do_dad,
bool *is_dup_detect,
bool is_local)
{
struct timeval elapsed = {0, 0};
char buf[ETHER_ADDR_STRLEN];
char buf1[INET6_ADDRSTRLEN];
bool reset_params = false;
if (!zvrf->dup_addr_detect)
return;
/* IP is detected as duplicate or inherit dup
* state, hold on to install as remote entry
* only if freeze is enabled.
*/
if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"%s: duplicate addr MAC %s IP %s flags 0x%x skip installing, learn count %u recover time %u",
__PRETTY_FUNCTION__,
prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
nbr->flags, nbr->dad_count,
zvrf->dad_freeze_time);
if (zvrf->dad_freeze)
*is_dup_detect = true;
/* warn-only action, neigh will be installed.
* freeze action, it wil not be installed.
*/
return;
}
if (!do_dad)
return;
/* Check if detection time (M-secs) expired.
* Reset learn count and detection start time.
* During remote mac add, count should already be 1
* via local learning.
*/
monotime_since(&nbr->detect_start_time, &elapsed);
reset_params = (elapsed.tv_sec > zvrf->dad_time);
if (is_local && !reset_params) {
/* RFC-7432: A PE/VTEP that detects a MAC mobility
* event via LOCAL learning starts an M-second timer.
*
* NOTE: This is the START of the probe with count is
* 0 during LOCAL learn event.
*/
reset_params = !nbr->dad_count;
}
if (reset_params) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"%s: duplicate addr MAC %s IP %s flags 0x%x detection time passed, reset learn count %u",
__PRETTY_FUNCTION__,
prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
nbr->flags, nbr->dad_count);
/* Reset learn count but do not start detection
* during REMOTE learn event.
*/
nbr->dad_count = 0;
/* Start dup. addr detection (DAD) start time,
* ONLY during LOCAL learn.
*/
if (is_local)
monotime(&nbr->detect_start_time);
} else if (!is_local) {
/* For REMOTE IP/Neigh, increment detection count
* ONLY while in probe window, once window passed,
* next local learn event should trigger DAD.
*/
nbr->dad_count++;
}
/* For LOCAL IP/Neigh learn event, once count is reset above via either
* initial/start detection time or passed the probe time, the count
* needs to be incremented.
*/
if (is_local)
nbr->dad_count++;
if (nbr->dad_count >= zvrf->dad_max_moves) {
flog_warn(EC_ZEBRA_DUP_IP_DETECTED,
"VNI %u: MAC %s IP %s detected as duplicate during %s VTEP %s",
nbr->zvni->vni,
prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
is_local ? "local update, last" :
"remote update, from",
inet_ntoa(vtep_ip));
SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
/* Capture Duplicate detection time */
nbr->dad_dup_detect_time = monotime(NULL);
/* Start auto recovery timer for this IP */
THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"%s: duplicate addr MAC %s IP %s flags 0x%x auto recovery time %u start",
__PRETTY_FUNCTION__,
prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
nbr->flags, zvrf->dad_freeze_time);
thread_add_timer(zrouter.master,
zebra_vxlan_dad_ip_auto_recovery_exp,
nbr, zvrf->dad_freeze_time,
&nbr->dad_ip_auto_recovery_timer);
}
if (zvrf->dad_freeze)
*is_dup_detect = true;
}
}
/*
* Helper function to determine maximum width of neighbor IP address for
* display - just because we're dealing with IPv6 addresses that can
* widely vary.
*/
static void zvni_find_neigh_addr_width(struct hash_bucket *bucket, void *ctxt)
{
zebra_neigh_t *n;
char buf[INET6_ADDRSTRLEN];
struct neigh_walk_ctx *wctx = ctxt;
int width;
n = (zebra_neigh_t *)bucket->data;
ipaddr2str(&n->ip, buf, sizeof(buf));
width = strlen(buf);
if (width > wctx->addr_width)
wctx->addr_width = width;
}
/*
* Print a specific neighbor entry.
*/
static void zvni_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json)
{
struct vty *vty;
char buf1[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
const char *type_str;
const char *state_str;
bool flags_present = false;
struct zebra_vrf *zvrf = NULL;
struct timeval detect_start_time = {0, 0};
zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
if (!zvrf)
return;
zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
ipaddr2str(&n->ip, buf2, sizeof(buf2));
prefix_mac2str(&n->emac, buf1, sizeof(buf1));
type_str = CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) ?
"local" : "remote";
state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
vty = (struct vty *)ctxt;
if (json == NULL) {
vty_out(vty, "IP: %s\n",
ipaddr2str(&n->ip, buf2, sizeof(buf2)));
vty_out(vty, " Type: %s\n", type_str);
vty_out(vty, " State: %s\n", state_str);
vty_out(vty, " MAC: %s\n",
prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
} else {
json_object_string_add(json, "ip", buf2);
json_object_string_add(json, "type", type_str);
json_object_string_add(json, "state", state_str);
json_object_string_add(json, "mac", buf1);
}
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
if (json == NULL) {
vty_out(vty, " Remote VTEP: %s\n",
inet_ntoa(n->r_vtep_ip));
} else
json_object_string_add(json, "remoteVtep",
inet_ntoa(n->r_vtep_ip));
}
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW)) {
if (!json) {
vty_out(vty, " Flags: Default-gateway");
flags_present = true;
} else
json_object_boolean_true_add(json, "defaultGateway");
}
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)) {
if (!json) {
vty_out(vty,
flags_present ? " ,Router" : " Flags: Router");
flags_present = true;
}
}
if (json == NULL) {
if (flags_present)
vty_out(vty, "\n");
vty_out(vty, " Local Seq: %u Remote Seq: %u\n",
n->loc_seq, n->rem_seq);
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE)) {
vty_out(vty, " Duplicate, detected at %s",
time_to_string(n->dad_dup_detect_time));
} else if (n->dad_count) {
monotime_since(&n->detect_start_time,
&detect_start_time);
if (detect_start_time.tv_sec <= zvrf->dad_time) {
char *buf = time_to_string(
n->detect_start_time.tv_sec);
char tmp_buf[30];
strlcpy(tmp_buf, buf, sizeof(tmp_buf));
vty_out(vty,
" Duplicate detection started at %s, detection count %u\n",
tmp_buf, n->dad_count);
}
}
} else {
json_object_int_add(json, "localSequence", n->loc_seq);
json_object_int_add(json, "remoteSequence", n->rem_seq);
json_object_int_add(json, "detectionCount",
n->dad_count);
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
json_object_boolean_true_add(json, "isDuplicate");
else
json_object_boolean_false_add(json, "isDuplicate");
}
}
/*
* Print neighbor hash entry - called for display of all neighbors.
*/
static void zvni_print_neigh_hash(struct hash_bucket *bucket, void *ctxt)
{
struct vty *vty;
json_object *json_vni = NULL, *json_row = NULL;
zebra_neigh_t *n;
char buf1[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
struct neigh_walk_ctx *wctx = ctxt;
const char *state_str;
vty = wctx->vty;
json_vni = wctx->json;
n = (zebra_neigh_t *)bucket->data;
if (json_vni)
json_row = json_object_new_object();
prefix_mac2str(&n->emac, buf1, sizeof(buf1));
ipaddr2str(&n->ip, buf2, sizeof(buf2));
state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
if (wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
return;
if (json_vni == NULL) {
vty_out(vty, "%*s %-6s %-8s %-17s\n",
-wctx->addr_width, buf2, "local",
state_str, buf1);
} else {
json_object_string_add(json_row, "type", "local");
json_object_string_add(json_row, "state", state_str);
json_object_string_add(json_row, "mac", buf1);
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
json_object_boolean_true_add(
json_row, "defaultGateway");
json_object_int_add(json_row, "localSequence",
n->loc_seq);
json_object_int_add(json_row, "remoteSequence",
n->rem_seq);
json_object_int_add(json_row, "detectionCount",
n->dad_count);
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
json_object_boolean_true_add(json_row,
"isDuplicate");
else
json_object_boolean_false_add(json_row,
"isDuplicate");
}
wctx->count++;
} else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP) &&
!IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))
return;
if (json_vni == NULL) {
if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP) &&
(wctx->count == 0))
vty_out(vty,
"%*s %-6s %-8s %-17s %-21s\n",
-wctx->addr_width, "Neighbor", "Type",
"State", "MAC", "Remote VTEP");
vty_out(vty, "%*s %-6s %-8s %-17s %-21s\n",
-wctx->addr_width, buf2, "remote", state_str,
buf1, inet_ntoa(n->r_vtep_ip));
} else {
json_object_string_add(json_row, "type", "remote");
json_object_string_add(json_row, "state", state_str);
json_object_string_add(json_row, "mac", buf1);
json_object_string_add(json_row, "remoteVtep",
inet_ntoa(n->r_vtep_ip));
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
json_object_boolean_true_add(json_row,
"defaultGateway");
json_object_int_add(json_row, "localSequence",
n->loc_seq);
json_object_int_add(json_row, "remoteSequence",
n->rem_seq);
json_object_int_add(json_row, "detectionCount",
n->dad_count);
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
json_object_boolean_true_add(json_row,
"isDuplicate");
else
json_object_boolean_false_add(json_row,
"isDuplicate");
}
wctx->count++;
}
if (json_vni)
json_object_object_add(json_vni, buf2, json_row);
}
/*
* Print neighbor hash entry in detail - called for display of all neighbors.
*/
static void zvni_print_neigh_hash_detail(struct hash_bucket *bucket, void *ctxt)
{
struct vty *vty;
json_object *json_vni = NULL, *json_row = NULL;
zebra_neigh_t *n;
char buf[INET6_ADDRSTRLEN];
struct neigh_walk_ctx *wctx = ctxt;
vty = wctx->vty;
json_vni = wctx->json;
n = (zebra_neigh_t *)bucket->data;
if (!n)
return;
ipaddr2str(&n->ip, buf, sizeof(buf));
if (json_vni)
json_row = json_object_new_object();
zvni_print_neigh(n, vty, json_row);
if (json_vni)
json_object_object_add(json_vni, buf, json_row);
}
/*
* Print neighbors for all VNI.
*/
static void zvni_print_neigh_hash_all_vni(struct hash_bucket *bucket,
void **args)
{
struct vty *vty;
json_object *json = NULL, *json_vni = NULL;
zebra_vni_t *zvni;
uint32_t num_neigh;
struct neigh_walk_ctx wctx;
char vni_str[VNI_STR_LEN];
uint32_t print_dup;
vty = (struct vty *)args[0];
json = (json_object *)args[1];
print_dup = (uint32_t)(uintptr_t)args[2];
zvni = (zebra_vni_t *)bucket->data;
num_neigh = hashcount(zvni->neigh_table);
if (print_dup)
num_neigh = num_dup_detected_neighs(zvni);
if (json == NULL) {
vty_out(vty,
"\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
zvni->vni, num_neigh);
} else {
json_vni = json_object_new_object();
json_object_int_add(json_vni, "numArpNd", num_neigh);
snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
}
if (!num_neigh) {
if (json)
json_object_object_add(json, vni_str, json_vni);
return;
}
/* Since we have IPv6 addresses to deal with which can vary widely in
* size, we try to be a bit more elegant in display by first computing
* the maximum width.
*/
memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
wctx.zvni = zvni;
wctx.vty = vty;
wctx.addr_width = 15;
wctx.json = json_vni;
hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
if (json == NULL) {
vty_out(vty, "%*s %-6s %-8s %-17s %-21s\n",
-wctx.addr_width, "IP", "Type",
"State", "MAC", "Remote VTEP");
}
if (print_dup)
hash_iterate(zvni->neigh_table, zvni_print_dad_neigh_hash,
&wctx);
else
hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
if (json)
json_object_object_add(json, vni_str, json_vni);
}
static void zvni_print_dad_neigh_hash(struct hash_bucket *bucket, void *ctxt)
{
zebra_neigh_t *nbr;
nbr = (zebra_neigh_t *)bucket->data;
if (!nbr)
return;
if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
zvni_print_neigh_hash(bucket, ctxt);
}
static void zvni_print_dad_neigh_hash_detail(struct hash_bucket *bucket,
void *ctxt)
{
zebra_neigh_t *nbr;
nbr = (zebra_neigh_t *)bucket->data;
if (!nbr)
return;
if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
zvni_print_neigh_hash_detail(bucket, ctxt);
}
/*
* Print neighbors for all VNIs in detail.
*/
static void zvni_print_neigh_hash_all_vni_detail(struct hash_bucket *bucket,
void **args)
{
struct vty *vty;
json_object *json = NULL, *json_vni = NULL;
zebra_vni_t *zvni;
uint32_t num_neigh;
struct neigh_walk_ctx wctx;
char vni_str[VNI_STR_LEN];
uint32_t print_dup;
vty = (struct vty *)args[0];
json = (json_object *)args[1];
print_dup = (uint32_t)(uintptr_t)args[2];
zvni = (zebra_vni_t *)bucket->data;
if (!zvni) {
if (json)
vty_out(vty, "{}\n");
return;
}
num_neigh = hashcount(zvni->neigh_table);
if (print_dup && num_dup_detected_neighs(zvni) == 0)
return;
if (json == NULL) {
vty_out(vty,
"\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
zvni->vni, num_neigh);
} else {
json_vni = json_object_new_object();
json_object_int_add(json_vni, "numArpNd", num_neigh);
snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
}
if (!num_neigh) {
if (json)
json_object_object_add(json, vni_str, json_vni);
return;
}
memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
wctx.zvni = zvni;
wctx.vty = vty;
wctx.addr_width = 15;
wctx.json = json_vni;
if (print_dup)
hash_iterate(zvni->neigh_table,
zvni_print_dad_neigh_hash_detail, &wctx);
else
hash_iterate(zvni->neigh_table, zvni_print_neigh_hash_detail,
&wctx);
if (json)
json_object_object_add(json, vni_str, json_vni);
}
/* print a specific next hop for an l3vni */
static void zl3vni_print_nh(zebra_neigh_t *n, struct vty *vty,
json_object *json)
{
char buf1[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
json_object *json_hosts = NULL;
struct host_rb_entry *hle;
if (!json) {
vty_out(vty, "Ip: %s\n",
ipaddr2str(&n->ip, buf2, sizeof(buf2)));
vty_out(vty, " RMAC: %s\n",
prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
vty_out(vty, " Refcount: %d\n",
rb_host_count(&n->host_rb));
vty_out(vty, " Prefixes:\n");
RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
vty_out(vty, " %s\n",
prefix2str(&hle->p, buf2, sizeof(buf2)));
} else {
json_hosts = json_object_new_array();
json_object_string_add(
json, "ip", ipaddr2str(&(n->ip), buf2, sizeof(buf2)));
json_object_string_add(
json, "routerMac",
prefix_mac2str(&n->emac, buf2, sizeof(buf2)));
json_object_int_add(json, "refCount",
rb_host_count(&n->host_rb));
RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
json_object_array_add(json_hosts,
json_object_new_string(prefix2str(
&hle->p, buf2, sizeof(buf2))));
json_object_object_add(json, "prefixList", json_hosts);
}
}
/* Print a specific RMAC entry */
static void zl3vni_print_rmac(zebra_mac_t *zrmac, struct vty *vty,
json_object *json)
{
char buf1[ETHER_ADDR_STRLEN];
char buf2[PREFIX_STRLEN];
json_object *json_hosts = NULL;
struct host_rb_entry *hle;
if (!json) {
vty_out(vty, "MAC: %s\n",
prefix_mac2str(&zrmac->macaddr, buf1, sizeof(buf1)));
vty_out(vty, " Remote VTEP: %s\n",
inet_ntoa(zrmac->fwd_info.r_vtep_ip));
vty_out(vty, " Refcount: %d\n", rb_host_count(&zrmac->host_rb));
vty_out(vty, " Prefixes:\n");
RB_FOREACH (hle, host_rb_tree_entry, &zrmac->host_rb)
vty_out(vty, " %s\n",
prefix2str(&hle->p, buf2, sizeof(buf2)));
} else {
json_hosts = json_object_new_array();
json_object_string_add(
json, "routerMac",
prefix_mac2str(&zrmac->macaddr, buf1, sizeof(buf1)));
json_object_string_add(json, "vtepIp",
inet_ntoa(zrmac->fwd_info.r_vtep_ip));
json_object_int_add(json, "refCount",
rb_host_count(&zrmac->host_rb));
json_object_int_add(json, "localSequence", zrmac->loc_seq);
json_object_int_add(json, "remoteSequence", zrmac->rem_seq);
RB_FOREACH (hle, host_rb_tree_entry, &zrmac->host_rb)
json_object_array_add(
json_hosts,
json_object_new_string(prefix2str(
&hle->p, buf2, sizeof(buf2))));
json_object_object_add(json, "prefixList", json_hosts);
}
}
/*
* Print a specific MAC entry.
*/
static void zvni_print_mac(zebra_mac_t *mac, void *ctxt, json_object *json)
{
struct vty *vty;
zebra_neigh_t *n = NULL;
struct listnode *node = NULL;
char buf1[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
struct zebra_vrf *zvrf;
struct timeval detect_start_time = {0, 0};
zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
vty = (struct vty *)ctxt;
prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
if (json) {
json_object *json_mac = json_object_new_object();
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
struct zebra_ns *zns;
struct interface *ifp;
ifindex_t ifindex;
ifindex = mac->fwd_info.local.ifindex;
zns = zebra_ns_lookup(NS_DEFAULT);
ifp = if_lookup_by_index_per_ns(zns, ifindex);
if (!ifp)
return;
json_object_string_add(json_mac, "type", "local");
json_object_string_add(json_mac, "intf", ifp->name);
json_object_int_add(json_mac, "ifindex", ifindex);
if (mac->fwd_info.local.vid)
json_object_int_add(json_mac, "vlan",
mac->fwd_info.local.vid);
} else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
json_object_string_add(json_mac, "type", "remote");
json_object_string_add(
json_mac, "remoteVtep",
inet_ntoa(mac->fwd_info.r_vtep_ip));
} else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
json_object_string_add(json_mac, "type", "auto");
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
json_object_boolean_true_add(json_mac, "stickyMac");
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
json_object_boolean_true_add(json_mac,
"defaultGateway");
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
json_object_boolean_true_add(json_mac,
"remoteGatewayMac");
json_object_int_add(json_mac, "localSequence", mac->loc_seq);
json_object_int_add(json_mac, "remoteSequence", mac->rem_seq);
json_object_int_add(json_mac, "detectionCount", mac->dad_count);
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
json_object_boolean_true_add(json_mac, "isDuplicate");
else
json_object_boolean_false_add(json_mac, "isDuplicate");
/* print all the associated neigh */
if (!listcount(mac->neigh_list))
json_object_string_add(json_mac, "neighbors", "none");
else {
json_object *json_active_nbrs = json_object_new_array();
json_object *json_inactive_nbrs =
json_object_new_array();
json_object *json_nbrs = json_object_new_object();
for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
if (IS_ZEBRA_NEIGH_ACTIVE(n))
json_object_array_add(
json_active_nbrs,
json_object_new_string(
ipaddr2str(
&n->ip, buf2,
sizeof(buf2))));
else
json_object_array_add(
json_inactive_nbrs,
json_object_new_string(
ipaddr2str(
&n->ip, buf2,
sizeof(buf2))));
}
json_object_object_add(json_nbrs, "active",
json_active_nbrs);
json_object_object_add(json_nbrs, "inactive",
json_inactive_nbrs);
json_object_object_add(json_mac, "neighbors",
json_nbrs);
}
json_object_object_add(json, buf1, json_mac);
} else {
vty_out(vty, "MAC: %s\n", buf1);
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
struct zebra_ns *zns;
struct interface *ifp;
ifindex_t ifindex;
ifindex = mac->fwd_info.local.ifindex;
zns = zebra_ns_lookup(NS_DEFAULT);
ifp = if_lookup_by_index_per_ns(zns, ifindex);
if (!ifp)
return;
vty_out(vty, " Intf: %s(%u)", ifp->name, ifindex);
if (mac->fwd_info.local.vid)
vty_out(vty, " VLAN: %u",
mac->fwd_info.local.vid);
} else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
vty_out(vty, " Remote VTEP: %s",
inet_ntoa(mac->fwd_info.r_vtep_ip));
} else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
vty_out(vty, " Auto Mac ");
}
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
vty_out(vty, " Sticky Mac ");
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
vty_out(vty, " Default-gateway Mac ");
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
vty_out(vty, " Remote-gateway Mac ");
vty_out(vty, "\n");
vty_out(vty, " Local Seq: %u Remote Seq: %u", mac->loc_seq,
mac->rem_seq);
vty_out(vty, "\n");
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
vty_out(vty, " Duplicate, detected at %s",
time_to_string(mac->dad_dup_detect_time));
} else if (mac->dad_count) {
monotime_since(&mac->detect_start_time,
&detect_start_time);
if (detect_start_time.tv_sec <= zvrf->dad_time) {
char *buf = time_to_string(
mac->detect_start_time.tv_sec);
char tmp_buf[30];
strlcpy(tmp_buf, buf, sizeof(tmp_buf));
vty_out(vty,
" Duplicate detection started at %s, detection count %u\n",
tmp_buf, mac->dad_count);
}
}
/* print all the associated neigh */
vty_out(vty, " Neighbors:\n");
if (!listcount(mac->neigh_list))
vty_out(vty, " No Neighbors\n");
else {
for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
vty_out(vty, " %s %s\n",
ipaddr2str(&n->ip, buf2, sizeof(buf2)),
(IS_ZEBRA_NEIGH_ACTIVE(n)
? "Active"
: "Inactive"));
}
}
vty_out(vty, "\n");
}
}
/*
* Print MAC hash entry - called for display of all MACs.
*/
static void zvni_print_mac_hash(struct hash_bucket *bucket, void *ctxt)
{
struct vty *vty;
json_object *json_mac_hdr = NULL, *json_mac = NULL;
zebra_mac_t *mac;
char buf1[ETHER_ADDR_STRLEN];
struct mac_walk_ctx *wctx = ctxt;
vty = wctx->vty;
json_mac_hdr = wctx->json;
mac = (zebra_mac_t *)bucket->data;
prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
if (json_mac_hdr)
json_mac = json_object_new_object();
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
struct zebra_ns *zns;
ifindex_t ifindex;
struct interface *ifp;
vlanid_t vid;
if (wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP)
return;
zns = zebra_ns_lookup(NS_DEFAULT);
ifindex = mac->fwd_info.local.ifindex;
ifp = if_lookup_by_index_per_ns(zns, ifindex);
if (!ifp) // unexpected
return;
vid = mac->fwd_info.local.vid;
if (json_mac_hdr == NULL)
vty_out(vty, "%-17s %-6s %-21s", buf1, "local",
ifp->name);
else {
json_object_string_add(json_mac, "type", "local");
json_object_string_add(json_mac, "intf", ifp->name);
}
if (vid) {
if (json_mac_hdr == NULL)
vty_out(vty, " %-5u", vid);
else
json_object_int_add(json_mac, "vlan", vid);
}
if (json_mac_hdr == NULL) {
vty_out(vty, "\n");
} else {
json_object_int_add(json_mac, "localSequence",
mac->loc_seq);
json_object_int_add(json_mac, "remoteSequence",
mac->rem_seq);
json_object_int_add(json_mac, "detectionCount",
mac->dad_count);
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
json_object_boolean_true_add(json_mac,
"isDuplicate");
else
json_object_boolean_false_add(json_mac,
"isDuplicate");
json_object_object_add(json_mac_hdr, buf1, json_mac);
}
wctx->count++;
} else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) &&
!IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip,
&wctx->r_vtep_ip))
return;
if (json_mac_hdr == NULL) {
if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) &&
(wctx->count == 0)) {
vty_out(vty, "\nVNI %u\n\n", wctx->zvni->vni);
vty_out(vty, "%-17s %-6s %-21s %-5s\n", "MAC",
"Type", "Intf/Remote VTEP", "VLAN");
}
vty_out(vty, "%-17s %-6s %-21s\n", buf1, "remote",
inet_ntoa(mac->fwd_info.r_vtep_ip));
} else {
json_object_string_add(json_mac, "type", "remote");
json_object_string_add(json_mac, "remoteVtep",
inet_ntoa(mac->fwd_info.r_vtep_ip));
json_object_object_add(json_mac_hdr, buf1, json_mac);
json_object_int_add(json_mac, "localSequence",
mac->loc_seq);
json_object_int_add(json_mac, "remoteSequence",
mac->rem_seq);
json_object_int_add(json_mac, "detectionCount",
mac->dad_count);
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
json_object_boolean_true_add(json_mac,
"isDuplicate");
else
json_object_boolean_false_add(json_mac,
"isDuplicate");
}
wctx->count++;
}
}
/* Print Duplicate MAC */
static void zvni_print_dad_mac_hash(struct hash_bucket *bucket, void *ctxt)
{
zebra_mac_t *mac;
mac = (zebra_mac_t *)bucket->data;
if (!mac)
return;
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
zvni_print_mac_hash(bucket, ctxt);
}
/*
* Print MAC hash entry in detail - called for display of all MACs.
*/
static void zvni_print_mac_hash_detail(struct hash_bucket *bucket, void *ctxt)
{
struct vty *vty;
json_object *json_mac_hdr = NULL;
zebra_mac_t *mac;
struct mac_walk_ctx *wctx = ctxt;
char buf1[ETHER_ADDR_STRLEN];
vty = wctx->vty;
json_mac_hdr = wctx->json;
mac = (zebra_mac_t *)bucket->data;
if (!mac)
return;
wctx->count++;
prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
zvni_print_mac(mac, vty, json_mac_hdr);
}
/* Print Duplicate MAC in detail */
static void zvni_print_dad_mac_hash_detail(struct hash_bucket *bucket,
void *ctxt)
{
zebra_mac_t *mac;
mac = (zebra_mac_t *)bucket->data;
if (!mac)
return;
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
zvni_print_mac_hash_detail(bucket, ctxt);
}
/*
* Print MACs for all VNI.
*/
static void zvni_print_mac_hash_all_vni(struct hash_bucket *bucket, void *ctxt)
{
struct vty *vty;
json_object *json = NULL, *json_vni = NULL;
json_object *json_mac = NULL;
zebra_vni_t *zvni;
uint32_t num_macs;
struct mac_walk_ctx *wctx = ctxt;
char vni_str[VNI_STR_LEN];
vty = (struct vty *)wctx->vty;
json = (struct json_object *)wctx->json;
zvni = (zebra_vni_t *)bucket->data;
wctx->zvni = zvni;
/*We are iterating over a new VNI, set the count to 0*/
wctx->count = 0;
num_macs = num_valid_macs(zvni);
if (!num_macs)
return;
if (wctx->print_dup)
num_macs = num_dup_detected_macs(zvni);
if (json) {
json_vni = json_object_new_object();
json_mac = json_object_new_object();
snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
}
if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
if (json == NULL) {
vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
zvni->vni, num_macs);
vty_out(vty, "%-17s %-6s %-21s %-5s\n", "MAC", "Type",
"Intf/Remote VTEP", "VLAN");
} else
json_object_int_add(json_vni, "numMacs", num_macs);
}
if (!num_macs) {
if (json) {
json_object_int_add(json_vni, "numMacs", num_macs);
json_object_object_add(json, vni_str, json_vni);
}
return;
}
/* assign per-vni to wctx->json object to fill macs
* under the vni. Re-assign primary json object to fill
* next vni information.
*/
wctx->json = json_mac;
if (wctx->print_dup)
hash_iterate(zvni->mac_table, zvni_print_dad_mac_hash, wctx);
else
hash_iterate(zvni->mac_table, zvni_print_mac_hash, wctx);
wctx->json = json;
if (json) {
if (wctx->count)
json_object_object_add(json_vni, "macs", json_mac);
json_object_object_add(json, vni_str, json_vni);
}
}
/*
* Print MACs in detail for all VNI.
*/
static void zvni_print_mac_hash_all_vni_detail(struct hash_bucket *bucket,
void *ctxt)
{
struct vty *vty;
json_object *json = NULL, *json_vni = NULL;
json_object *json_mac = NULL;
zebra_vni_t *zvni;
uint32_t num_macs;
struct mac_walk_ctx *wctx = ctxt;
char vni_str[VNI_STR_LEN];
vty = (struct vty *)wctx->vty;
json = (struct json_object *)wctx->json;
zvni = (zebra_vni_t *)bucket->data;
if (!zvni) {
if (json)
vty_out(vty, "{}\n");
return;
}
wctx->zvni = zvni;
/*We are iterating over a new VNI, set the count to 0*/
wctx->count = 0;
num_macs = num_valid_macs(zvni);
if (!num_macs)
return;
if (wctx->print_dup && (num_dup_detected_macs(zvni) == 0))
return;
if (json) {
json_vni = json_object_new_object();
json_mac = json_object_new_object();
snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
}
if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
if (json == NULL) {
vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
zvni->vni, num_macs);
} else
json_object_int_add(json_vni, "numMacs", num_macs);
}
/* assign per-vni to wctx->json object to fill macs
* under the vni. Re-assign primary json object to fill
* next vni information.
*/
wctx->json = json_mac;
if (wctx->print_dup)
hash_iterate(zvni->mac_table, zvni_print_dad_mac_hash_detail,
wctx);
else
hash_iterate(zvni->mac_table, zvni_print_mac_hash_detail, wctx);
wctx->json = json;
if (json) {
if (wctx->count)
json_object_object_add(json_vni, "macs", json_mac);
json_object_object_add(json, vni_str, json_vni);
}
}
static void zl3vni_print_nh_hash(struct hash_bucket *bucket, void *ctx)
{
struct nh_walk_ctx *wctx = NULL;
struct vty *vty = NULL;
struct json_object *json_vni = NULL;
struct json_object *json_nh = NULL;
zebra_neigh_t *n = NULL;
char buf1[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
wctx = (struct nh_walk_ctx *)ctx;
vty = wctx->vty;
json_vni = wctx->json;
if (json_vni)
json_nh = json_object_new_object();
n = (zebra_neigh_t *)bucket->data;
if (!json_vni) {
vty_out(vty, "%-15s %-17s\n",
ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
} else {
json_object_string_add(json_nh, "nexthopIp",
ipaddr2str(&n->ip, buf2, sizeof(buf2)));
json_object_string_add(
json_nh, "routerMac",
prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
json_object_object_add(json_vni,
ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
json_nh);
}
}
static void zl3vni_print_nh_hash_all_vni(struct hash_bucket *bucket,
void **args)
{
struct vty *vty = NULL;
json_object *json = NULL;
json_object *json_vni = NULL;
zebra_l3vni_t *zl3vni = NULL;
uint32_t num_nh = 0;
struct nh_walk_ctx wctx;
char vni_str[VNI_STR_LEN];
vty = (struct vty *)args[0];
json = (struct json_object *)args[1];
zl3vni = (zebra_l3vni_t *)bucket->data;
num_nh = hashcount(zl3vni->nh_table);
if (!num_nh)
return;
if (json) {
json_vni = json_object_new_object();
snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
}
if (json == NULL) {
vty_out(vty, "\nVNI %u #Next-Hops %u\n\n", zl3vni->vni, num_nh);
vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
} else
json_object_int_add(json_vni, "numNextHops", num_nh);
memset(&wctx, 0, sizeof(struct nh_walk_ctx));
wctx.vty = vty;
wctx.json = json_vni;
hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
if (json)
json_object_object_add(json, vni_str, json_vni);
}
static void zl3vni_print_rmac_hash_all_vni(struct hash_bucket *bucket,
void **args)
{
struct vty *vty = NULL;
json_object *json = NULL;
json_object *json_vni = NULL;
zebra_l3vni_t *zl3vni = NULL;
uint32_t num_rmacs;
struct rmac_walk_ctx wctx;
char vni_str[VNI_STR_LEN];
vty = (struct vty *)args[0];
json = (struct json_object *)args[1];
zl3vni = (zebra_l3vni_t *)bucket->data;
num_rmacs = hashcount(zl3vni->rmac_table);
if (!num_rmacs)
return;
if (json) {
json_vni = json_object_new_object();
snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
}
if (json == NULL) {
vty_out(vty, "\nVNI %u #RMACs %u\n\n", zl3vni->vni, num_rmacs);
vty_out(vty, "%-17s %-21s\n", "RMAC", "Remote VTEP");
} else
json_object_int_add(json_vni, "numRmacs", num_rmacs);
/* assign per-vni to wctx->json object to fill macs
* under the vni. Re-assign primary json object to fill
* next vni information.
*/
memset(&wctx, 0, sizeof(struct rmac_walk_ctx));
wctx.vty = vty;
wctx.json = json_vni;
hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
if (json)
json_object_object_add(json, vni_str, json_vni);
}
static void zl3vni_print_rmac_hash(struct hash_bucket *bucket, void *ctx)
{
zebra_mac_t *zrmac = NULL;
struct rmac_walk_ctx *wctx = NULL;
struct vty *vty = NULL;
struct json_object *json = NULL;
struct json_object *json_rmac = NULL;
char buf[ETHER_ADDR_STRLEN];
wctx = (struct rmac_walk_ctx *)ctx;
vty = wctx->vty;
json = wctx->json;
if (json)
json_rmac = json_object_new_object();
zrmac = (zebra_mac_t *)bucket->data;
if (!json) {
vty_out(vty, "%-17s %-21s\n",
prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
inet_ntoa(zrmac->fwd_info.r_vtep_ip));
} else {
json_object_string_add(
json_rmac, "routerMac",
prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)));
json_object_string_add(json_rmac, "vtepIp",
inet_ntoa(zrmac->fwd_info.r_vtep_ip));
json_object_object_add(
json, prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
json_rmac);
}
}
/* print a specific L3 VNI entry */
static void zl3vni_print(zebra_l3vni_t *zl3vni, void **ctx)
{
char buf[ETHER_ADDR_STRLEN];
struct vty *vty = NULL;
json_object *json = NULL;
zebra_vni_t *zvni = NULL;
json_object *json_vni_list = NULL;
struct listnode *node = NULL, *nnode = NULL;
vty = ctx[0];
json = ctx[1];
if (!json) {
vty_out(vty, "VNI: %u\n", zl3vni->vni);
vty_out(vty, " Type: %s\n", "L3");
vty_out(vty, " Tenant VRF: %s\n", zl3vni_vrf_name(zl3vni));
vty_out(vty, " Local Vtep Ip: %s\n",
inet_ntoa(zl3vni->local_vtep_ip));
vty_out(vty, " Vxlan-Intf: %s\n",
zl3vni_vxlan_if_name(zl3vni));
vty_out(vty, " SVI-If: %s\n", zl3vni_svi_if_name(zl3vni));
vty_out(vty, " State: %s\n", zl3vni_state2str(zl3vni));
vty_out(vty, " VNI Filter: %s\n",
CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
? "prefix-routes-only"
: "none");
vty_out(vty, " Router MAC: %s\n",
zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
vty_out(vty, " L2 VNIs: ");
for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zvni))
vty_out(vty, "%u ", zvni->vni);
vty_out(vty, "\n");
} else {
json_vni_list = json_object_new_array();
json_object_int_add(json, "vni", zl3vni->vni);
json_object_string_add(json, "type", "L3");
json_object_string_add(json, "localVtepIp",
inet_ntoa(zl3vni->local_vtep_ip));
json_object_string_add(json, "vxlanIntf",
zl3vni_vxlan_if_name(zl3vni));
json_object_string_add(json, "sviIntf",
zl3vni_svi_if_name(zl3vni));
json_object_string_add(json, "state", zl3vni_state2str(zl3vni));
json_object_string_add(json, "vrf", zl3vni_vrf_name(zl3vni));
json_object_string_add(
json, "routerMac",
zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
json_object_string_add(
json, "vniFilter",
CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
? "prefix-routes-only"
: "none");
for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zvni)) {
json_object_array_add(json_vni_list,
json_object_new_int(zvni->vni));
}
json_object_object_add(json, "l2Vnis", json_vni_list);
}
}
/*
* Print a specific VNI entry.
*/
static void zvni_print(zebra_vni_t *zvni, void **ctxt)
{
struct vty *vty;
zebra_vtep_t *zvtep;
uint32_t num_macs;
uint32_t num_neigh;
json_object *json = NULL;
json_object *json_vtep_list = NULL;
json_object *json_ip_str = NULL;
vty = ctxt[0];
json = ctxt[1];
if (json == NULL) {
vty_out(vty, "VNI: %u\n", zvni->vni);
vty_out(vty, " Type: %s\n", "L2");
vty_out(vty, " Tenant VRF: %s\n", vrf_id_to_name(zvni->vrf_id));
} else {
json_object_int_add(json, "vni", zvni->vni);
json_object_string_add(json, "type", "L2");
json_object_string_add(json, "vrf",
vrf_id_to_name(zvni->vrf_id));
}
if (!zvni->vxlan_if) { // unexpected
if (json == NULL)
vty_out(vty, " VxLAN interface: unknown\n");
return;
}
num_macs = num_valid_macs(zvni);
num_neigh = hashcount(zvni->neigh_table);
if (json == NULL) {
vty_out(vty, " VxLAN interface: %s\n", zvni->vxlan_if->name);
vty_out(vty, " VxLAN ifIndex: %u\n", zvni->vxlan_if->ifindex);
vty_out(vty, " Local VTEP IP: %s\n",
inet_ntoa(zvni->local_vtep_ip));
} else {
json_object_string_add(json, "vxlanInterface",
zvni->vxlan_if->name);
json_object_int_add(json, "ifindex", zvni->vxlan_if->ifindex);
json_object_string_add(json, "vtepIp",
inet_ntoa(zvni->local_vtep_ip));
json_object_string_add(json, "advertiseGatewayMacip",
zvni->advertise_gw_macip ? "Yes" : "No");
json_object_int_add(json, "numMacs", num_macs);
json_object_int_add(json, "numArpNd", num_neigh);
}
if (!zvni->vteps) {
if (json == NULL)
vty_out(vty, " No remote VTEPs known for this VNI\n");
} else {
if (json == NULL)
vty_out(vty, " Remote VTEPs for this VNI:\n");
else
json_vtep_list = json_object_new_array();
for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
if (json == NULL)
vty_out(vty, " %s\n",
inet_ntoa(zvtep->vtep_ip));
else {
json_ip_str = json_object_new_string(
inet_ntoa(zvtep->vtep_ip));
json_object_array_add(json_vtep_list,
json_ip_str);
}
}
if (json)
json_object_object_add(json, "numRemoteVteps",
json_vtep_list);
}
if (json == NULL) {
vty_out(vty,
" Number of MACs (local and remote) known for this VNI: %u\n",
num_macs);
vty_out(vty,
" Number of ARPs (IPv4 and IPv6, local and remote) "
"known for this VNI: %u\n",
num_neigh);
vty_out(vty, " Advertise-gw-macip: %s\n",
zvni->advertise_gw_macip ? "Yes" : "No");
}
}
/* print a L3 VNI hash entry */
static void zl3vni_print_hash(struct hash_bucket *bucket, void *ctx[])
{
struct vty *vty = NULL;
json_object *json = NULL;
json_object *json_vni = NULL;
zebra_l3vni_t *zl3vni = NULL;
vty = (struct vty *)ctx[0];
json = (json_object *)ctx[1];
zl3vni = (zebra_l3vni_t *)bucket->data;
if (!json) {
vty_out(vty, "%-10u %-4s %-21s %-8lu %-8lu %-15s %-37s\n",
zl3vni->vni, "L3", zl3vni_vxlan_if_name(zl3vni),
hashcount(zl3vni->rmac_table),
hashcount(zl3vni->nh_table), "n/a",
zl3vni_vrf_name(zl3vni));
} else {
char vni_str[VNI_STR_LEN];
snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
json_vni = json_object_new_object();
json_object_int_add(json_vni, "vni", zl3vni->vni);
json_object_string_add(json_vni, "vxlanIf",
zl3vni_vxlan_if_name(zl3vni));
json_object_int_add(json_vni, "numMacs",
hashcount(zl3vni->rmac_table));
json_object_int_add(json_vni, "numArpNd",
hashcount(zl3vni->nh_table));
json_object_string_add(json_vni, "numRemoteVteps", "n/a");
json_object_string_add(json_vni, "type", "L3");
json_object_string_add(json_vni, "tenantVrf",
zl3vni_vrf_name(zl3vni));
json_object_object_add(json, vni_str, json_vni);
}
}
/* Private Structure to pass callback data for hash iterator */
struct zvni_evpn_show {
struct vty *vty;
json_object *json;
struct zebra_vrf *zvrf;
};
/* print a L3 VNI hash entry in detail*/
static void zl3vni_print_hash_detail(struct hash_bucket *bucket, void *data)
{
struct vty *vty = NULL;
zebra_l3vni_t *zl3vni = NULL;
json_object *json = NULL;
bool use_json = false;
struct zvni_evpn_show *zes = data;
vty = zes->vty;
json = zes->json;
if (json)
use_json = true;
zl3vni = (zebra_l3vni_t *)bucket->data;
zebra_vxlan_print_vni(vty, zes->zvrf, zl3vni->vni, use_json);
vty_out(vty, "\n");
}
/*
* Print a VNI hash entry - called for display of all VNIs.
*/
static void zvni_print_hash(struct hash_bucket *bucket, void *ctxt[])
{
struct vty *vty;
zebra_vni_t *zvni;
zebra_vtep_t *zvtep;
uint32_t num_vteps = 0;
uint32_t num_macs = 0;
uint32_t num_neigh = 0;
json_object *json = NULL;
json_object *json_vni = NULL;
json_object *json_ip_str = NULL;
json_object *json_vtep_list = NULL;
vty = ctxt[0];
json = ctxt[1];
zvni = (zebra_vni_t *)bucket->data;
zvtep = zvni->vteps;
while (zvtep) {
num_vteps++;
zvtep = zvtep->next;
}
num_macs = num_valid_macs(zvni);
num_neigh = hashcount(zvni->neigh_table);
if (json == NULL)
vty_out(vty, "%-10u %-4s %-21s %-8u %-8u %-15u %-37s\n",
zvni->vni, "L2",
zvni->vxlan_if ? zvni->vxlan_if->name : "unknown",
num_macs, num_neigh, num_vteps,
vrf_id_to_name(zvni->vrf_id));
else {
char vni_str[VNI_STR_LEN];
snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
json_vni = json_object_new_object();
json_object_int_add(json_vni, "vni", zvni->vni);
json_object_string_add(json_vni, "type", "L2");
json_object_string_add(json_vni, "vxlanIf",
zvni->vxlan_if ? zvni->vxlan_if->name
: "unknown");
json_object_int_add(json_vni, "numMacs", num_macs);
json_object_int_add(json_vni, "numArpNd", num_neigh);
json_object_int_add(json_vni, "numRemoteVteps", num_vteps);
json_object_string_add(json_vni, "tenantVrf",
vrf_id_to_name(zvni->vrf_id));
if (num_vteps) {
json_vtep_list = json_object_new_array();
for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
json_ip_str = json_object_new_string(
inet_ntoa(zvtep->vtep_ip));
json_object_array_add(json_vtep_list,
json_ip_str);
}
json_object_object_add(json_vni, "remoteVteps",
json_vtep_list);
}
json_object_object_add(json, vni_str, json_vni);
}
}
/*
* Print a VNI hash entry in detail - called for display of all VNIs.
*/
static void zvni_print_hash_detail(struct hash_bucket *bucket, void *data)
{
struct vty *vty;
zebra_vni_t *zvni;
json_object *json = NULL;
bool use_json = false;
struct zvni_evpn_show *zes = data;
vty = zes->vty;
json = zes->json;
if (json)
use_json = true;
zvni = (zebra_vni_t *)bucket->data;
zebra_vxlan_print_vni(vty, zes->zvrf, zvni->vni, use_json);
vty_out(vty, "\n");
}
/*
* Inform BGP about local MACIP.
*/
static int zvni_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr,
struct ipaddr *ip, uint8_t flags,
uint32_t seq, int state, uint16_t cmd)
{
char buf[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
int ipa_len;
struct zserv *client = NULL;
struct stream *s = NULL;
client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
/* BGP may not be running. */
if (!client)
return 0;
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, cmd, VRF_DEFAULT);
stream_putl(s, vni);
stream_put(s, macaddr->octet, ETH_ALEN);
if (ip) {
ipa_len = 0;
if (IS_IPADDR_V4(ip))
ipa_len = IPV4_MAX_BYTELEN;
else if (IS_IPADDR_V6(ip))
ipa_len = IPV6_MAX_BYTELEN;
stream_putl(s, ipa_len); /* IP address length */
if (ipa_len)
stream_put(s, &ip->ip.addr, ipa_len); /* IP address */
} else
stream_putl(s, 0); /* Just MAC. */
if (cmd == ZEBRA_MACIP_ADD) {
stream_putc(s, flags); /* sticky mac/gateway mac */
stream_putl(s, seq); /* sequence number */
} else {
stream_putl(s, state); /* state - active/inactive */
}
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"Send MACIP %s flags 0x%x MAC %s IP %s seq %u L2-VNI %u to %s",
(cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
prefix_mac2str(macaddr, buf, sizeof(buf)),
ipaddr2str(ip, buf2, sizeof(buf2)), seq, vni,
zebra_route_string(client->proto));
if (cmd == ZEBRA_MACIP_ADD)
client->macipadd_cnt++;
else
client->macipdel_cnt++;
return zserv_send_message(client, s);
}
/*
* Make hash key for neighbors.
*/
static unsigned int neigh_hash_keymake(void *p)
{
zebra_neigh_t *n = p;
struct ipaddr *ip = &n->ip;
if (IS_IPADDR_V4(ip))
return jhash_1word(ip->ipaddr_v4.s_addr, 0);
return jhash2(ip->ipaddr_v6.s6_addr32,
ZEBRA_NUM_OF(ip->ipaddr_v6.s6_addr32), 0);
}
/*
* Compare two neighbor hash structures.
*/
static bool neigh_cmp(const void *p1, const void *p2)
{
const zebra_neigh_t *n1 = p1;
const zebra_neigh_t *n2 = p2;
if (n1 == NULL && n2 == NULL)
return true;
if (n1 == NULL || n2 == NULL)
return false;
return (memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr)) == 0);
}
static int neigh_list_cmp(void *p1, void *p2)
{
const zebra_neigh_t *n1 = p1;
const zebra_neigh_t *n2 = p2;
return memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr));
}
/*
* Callback to allocate neighbor hash entry.
*/
static void *zvni_neigh_alloc(void *p)
{
const zebra_neigh_t *tmp_n = p;
zebra_neigh_t *n;
n = XCALLOC(MTYPE_NEIGH, sizeof(zebra_neigh_t));
*n = *tmp_n;
return ((void *)n);
}
/*
* Add neighbor entry.
*/
static zebra_neigh_t *zvni_neigh_add(zebra_vni_t *zvni, struct ipaddr *ip,
struct ethaddr *mac)
{
zebra_neigh_t tmp_n;
zebra_neigh_t *n = NULL;
zebra_mac_t *zmac = NULL;
memset(&tmp_n, 0, sizeof(zebra_neigh_t));
memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
n = hash_get(zvni->neigh_table, &tmp_n, zvni_neigh_alloc);
assert(n);
memcpy(&n->emac, mac, ETH_ALEN);
n->state = ZEBRA_NEIGH_INACTIVE;
n->zvni = zvni;
/* Associate the neigh to mac */
zmac = zvni_mac_lookup(zvni, mac);
if (zmac)
listnode_add_sort(zmac->neigh_list, n);
return n;
}
/*
* Delete neighbor entry.
*/
static int zvni_neigh_del(zebra_vni_t *zvni, zebra_neigh_t *n)
{
zebra_neigh_t *tmp_n;
zebra_mac_t *zmac = NULL;
zmac = zvni_mac_lookup(zvni, &n->emac);
if (zmac)
listnode_delete(zmac->neigh_list, n);
/* Free the VNI hash entry and allocated memory. */
tmp_n = hash_release(zvni->neigh_table, n);
XFREE(MTYPE_NEIGH, tmp_n);
return 0;
}
/*
* Free neighbor hash entry (callback)
*/
static void zvni_neigh_del_hash_entry(struct hash_bucket *bucket, void *arg)
{
struct neigh_walk_ctx *wctx = arg;
zebra_neigh_t *n = bucket->data;
if (((wctx->flags & DEL_LOCAL_NEIGH) && (n->flags & ZEBRA_NEIGH_LOCAL))
|| ((wctx->flags & DEL_REMOTE_NEIGH)
&& (n->flags & ZEBRA_NEIGH_REMOTE))
|| ((wctx->flags & DEL_REMOTE_NEIGH_FROM_VTEP)
&& (n->flags & ZEBRA_NEIGH_REMOTE)
&& IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))) {
if (wctx->upd_client && (n->flags & ZEBRA_NEIGH_LOCAL))
zvni_neigh_send_del_to_client(wctx->zvni->vni, &n->ip,
&n->emac, 0, n->state);
if (wctx->uninstall)
zvni_neigh_uninstall(wctx->zvni, n);
zvni_neigh_del(wctx->zvni, n);
}
return;
}
/*
* Delete all neighbor entries from specific VTEP for a particular VNI.
*/
static void zvni_neigh_del_from_vtep(zebra_vni_t *zvni, int uninstall,
struct in_addr *r_vtep_ip)
{
struct neigh_walk_ctx wctx;
if (!zvni->neigh_table)
return;
memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
wctx.zvni = zvni;
wctx.uninstall = uninstall;
wctx.flags = DEL_REMOTE_NEIGH_FROM_VTEP;
wctx.r_vtep_ip = *r_vtep_ip;
hash_iterate(zvni->neigh_table, zvni_neigh_del_hash_entry, &wctx);
}
/*
* Delete all neighbor entries for this VNI.
*/
static void zvni_neigh_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
uint32_t flags)
{
struct neigh_walk_ctx wctx;
if (!zvni->neigh_table)
return;
memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
wctx.zvni = zvni;
wctx.uninstall = uninstall;
wctx.upd_client = upd_client;
wctx.flags = flags;
hash_iterate(zvni->neigh_table, zvni_neigh_del_hash_entry, &wctx);
}
/*
* Look up neighbor hash entry.
*/
static zebra_neigh_t *zvni_neigh_lookup(zebra_vni_t *zvni, struct ipaddr *ip)
{
zebra_neigh_t tmp;
zebra_neigh_t *n;
memset(&tmp, 0, sizeof(tmp));
memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
n = hash_lookup(zvni->neigh_table, &tmp);
return n;
}
/*
* Process all neighbors associated with a MAC upon the MAC being learnt
* locally or undergoing any other change (such as sequence number).
*/
static void zvni_process_neigh_on_local_mac_change(zebra_vni_t *zvni,
zebra_mac_t *zmac,
bool seq_change)
{
zebra_neigh_t *n = NULL;
struct listnode *node = NULL;
struct zebra_vrf *zvrf = NULL;
char buf[ETHER_ADDR_STRLEN];
zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug("Processing neighbors on local MAC %s %s, VNI %u",
prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
seq_change ? "CHANGE" : "ADD", zvni->vni);
/* Walk all neighbors and mark any inactive local neighbors as
* active and/or update sequence number upon a move, and inform BGP.
* The action for remote neighbors is TBD.
* NOTE: We can't simply uninstall remote neighbors as the kernel may
* accidentally end up deleting a just-learnt local neighbor.
*/
for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
if (IS_ZEBRA_NEIGH_INACTIVE(n) || seq_change) {
ZEBRA_NEIGH_SET_ACTIVE(n);
n->loc_seq = zmac->loc_seq;
if (!(zvrf->dup_addr_detect &&
zvrf->dad_freeze && !!CHECK_FLAG(n->flags,
ZEBRA_NEIGH_DUPLICATE)))
zvni_neigh_send_add_to_client(
zvni->vni, &n->ip, &n->emac,
n->flags, n->loc_seq);
}
}
}
}
/*
* Process all neighbors associated with a local MAC upon the MAC being
* deleted.
*/
static void zvni_process_neigh_on_local_mac_del(zebra_vni_t *zvni,
zebra_mac_t *zmac)
{
zebra_neigh_t *n = NULL;
struct listnode *node = NULL;
char buf[ETHER_ADDR_STRLEN];
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug("Processing neighbors on local MAC %s DEL, VNI %u",
prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
zvni->vni);
/* Walk all local neighbors and mark as inactive and inform
* BGP, if needed.
* TBD: There is currently no handling for remote neighbors. We
* don't expect them to exist, if they do, do we install the MAC
* as a remote MAC and the neighbor as remote?
*/
for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
ZEBRA_NEIGH_SET_INACTIVE(n);
n->loc_seq = 0;
zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
&n->emac, 0, ZEBRA_NEIGH_ACTIVE);
}
}
}
}
/*
* Process all neighbors associated with a MAC upon the MAC being remotely
* learnt.
*/
static void zvni_process_neigh_on_remote_mac_add(zebra_vni_t *zvni,
zebra_mac_t *zmac)
{
zebra_neigh_t *n = NULL;
struct listnode *node = NULL;
char buf[ETHER_ADDR_STRLEN];
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug("Processing neighbors on remote MAC %s ADD, VNI %u",
prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
zvni->vni);
/* Walk all local neighbors and mark as inactive and inform
* BGP, if needed.
*/
for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
ZEBRA_NEIGH_SET_INACTIVE(n);
n->loc_seq = 0;
zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
&n->emac, 0, ZEBRA_NEIGH_ACTIVE);
}
}
}
}
/*
* Process all neighbors associated with a remote MAC upon the MAC being
* deleted.
*/
static void zvni_process_neigh_on_remote_mac_del(zebra_vni_t *zvni,
zebra_mac_t *zmac)
{
/* NOTE: Currently a NO-OP. */
}
static void zvni_probe_neigh_on_mac_add(zebra_vni_t *zvni, zebra_mac_t *zmac)
{
zebra_neigh_t *nbr = NULL;
struct listnode *node = NULL;
for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, nbr)) {
if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL) &&
IS_ZEBRA_NEIGH_INACTIVE(nbr))
zvni_neigh_probe(zvni, nbr);
}
}
/*
* Inform BGP about local neighbor addition.
*/
static int zvni_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
struct ethaddr *macaddr,
uint8_t neigh_flags,
uint32_t seq)
{
uint8_t flags = 0;
if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_DEF_GW))
SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
/* Set router flag (R-bit) based on local neigh entry add */
if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_ROUTER_FLAG))
SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
return zvni_macip_send_msg_to_client(vni, macaddr, ip, flags,
seq, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_ADD);
}
/*
* Inform BGP about local neighbor deletion.
*/
static int zvni_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
struct ethaddr *macaddr, uint8_t flags,
int state)
{
return zvni_macip_send_msg_to_client(vni, macaddr, ip, flags,
0, state, ZEBRA_MACIP_DEL);
}
/*
* Install remote neighbor into the kernel.
*/
static int zvni_neigh_install(zebra_vni_t *zvni, zebra_neigh_t *n)
{
struct zebra_if *zif;
struct zebra_l2info_vxlan *vxl;
struct interface *vlan_if;
#ifdef GNU_LINUX
uint8_t flags;
#endif
int ret = 0;
if (!(n->flags & ZEBRA_NEIGH_REMOTE))
return 0;
zif = zvni->vxlan_if->info;
if (!zif)
return -1;
vxl = &zif->l2info.vxl;
vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
if (!vlan_if)
return -1;
#ifdef GNU_LINUX
flags = NTF_EXT_LEARNED;
if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
flags |= NTF_ROUTER;
ZEBRA_NEIGH_SET_ACTIVE(n);
ret = kernel_add_neigh(vlan_if, &n->ip, &n->emac, flags);
#endif
return ret;
}
/*
* Uninstall remote neighbor from the kernel.
*/
static int zvni_neigh_uninstall(zebra_vni_t *zvni, zebra_neigh_t *n)
{
struct zebra_if *zif;
struct zebra_l2info_vxlan *vxl;
struct interface *vlan_if;
if (!(n->flags & ZEBRA_NEIGH_REMOTE))
return 0;
if (!zvni->vxlan_if) {
zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
zvni->vni, zvni);
return -1;
}
zif = zvni->vxlan_if->info;
if (!zif)
return -1;
vxl = &zif->l2info.vxl;
vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
if (!vlan_if)
return -1;
ZEBRA_NEIGH_SET_INACTIVE(n);
n->loc_seq = 0;
return kernel_del_neigh(vlan_if, &n->ip);
}
/*
* Probe neighbor from the kernel.
*/
static int zvni_neigh_probe(zebra_vni_t *zvni, zebra_neigh_t *n)
{
struct zebra_if *zif;
struct zebra_l2info_vxlan *vxl;
struct interface *vlan_if;
zif = zvni->vxlan_if->info;
if (!zif)
return -1;
vxl = &zif->l2info.vxl;
vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
if (!vlan_if)
return -1;
#ifdef GNU_LINUX
return kernel_upd_neigh(vlan_if, &n->ip, &n->emac,
0, NUD_PROBE);
#else
return 0;
#endif
}
/*
* Install neighbor hash entry - called upon access VLAN change.
*/
static void zvni_install_neigh_hash(struct hash_bucket *bucket, void *ctxt)
{
zebra_neigh_t *n;
struct neigh_walk_ctx *wctx = ctxt;
n = (zebra_neigh_t *)bucket->data;
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
zvni_neigh_install(wctx->zvni, n);
}
/* Get the VRR interface for SVI if any */
struct interface *zebra_get_vrr_intf_for_svi(struct interface *ifp)
{
struct zebra_vrf *zvrf = NULL;
struct interface *tmp_if = NULL;
struct zebra_if *zif = NULL;
zvrf = vrf_info_lookup(ifp->vrf_id);
assert(zvrf);
FOR_ALL_INTERFACES (zvrf->vrf, tmp_if) {
zif = tmp_if->info;
if (!zif)
continue;
if (!IS_ZEBRA_IF_MACVLAN(tmp_if))
continue;
if (zif->link == ifp)
return tmp_if;
}
return NULL;
}
static int zvni_del_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni)
{
struct listnode *cnode = NULL, *cnnode = NULL;
struct connected *c = NULL;
struct ethaddr macaddr;
memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
struct ipaddr ip;
memset(&ip, 0, sizeof(struct ipaddr));
if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
continue;
if (c->address->family == AF_INET) {
ip.ipa_type = IPADDR_V4;
memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
sizeof(struct in_addr));
} else if (c->address->family == AF_INET6) {
ip.ipa_type = IPADDR_V6;
memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
sizeof(struct in6_addr));
} else {
continue;
}
zvni_gw_macip_del(ifp, zvni, &ip);
}
return 0;
}
static int zvni_add_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni)
{
struct listnode *cnode = NULL, *cnnode = NULL;
struct connected *c = NULL;
struct ethaddr macaddr;
memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
struct ipaddr ip;
memset(&ip, 0, sizeof(struct ipaddr));
if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
continue;
if (c->address->family == AF_INET) {
ip.ipa_type = IPADDR_V4;
memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
sizeof(struct in_addr));
} else if (c->address->family == AF_INET6) {
ip.ipa_type = IPADDR_V6;
memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
sizeof(struct in6_addr));
} else {
continue;
}
zvni_gw_macip_add(ifp, zvni, &macaddr, &ip);
}
return 0;
}
static int zvni_advertise_subnet(zebra_vni_t *zvni, struct interface *ifp,
int advertise)
{
struct listnode *cnode = NULL, *cnnode = NULL;
struct connected *c = NULL;
struct ethaddr macaddr;
memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
struct prefix p;
memcpy(&p, c->address, sizeof(struct prefix));
/* skip link local address */
if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
continue;
apply_mask(&p);
if (advertise)
ip_prefix_send_to_client(ifp->vrf_id, &p,
ZEBRA_IP_PREFIX_ROUTE_ADD);
else
ip_prefix_send_to_client(ifp->vrf_id, &p,
ZEBRA_IP_PREFIX_ROUTE_DEL);
}
return 0;
}
/*
* zvni_gw_macip_add_to_client
*/
static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni,
struct ethaddr *macaddr, struct ipaddr *ip)
{
char buf[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
zebra_neigh_t *n = NULL;
zebra_mac_t *mac = NULL;
struct zebra_if *zif = NULL;
struct zebra_l2info_vxlan *vxl = NULL;
zif = zvni->vxlan_if->info;
if (!zif)
return -1;
vxl = &zif->l2info.vxl;
mac = zvni_mac_lookup(zvni, macaddr);
if (!mac) {
mac = zvni_mac_add(zvni, macaddr);
if (!mac) {
flog_err(EC_ZEBRA_MAC_ADD_FAILED,
"Failed to add MAC %s intf %s(%u) VID %u",
prefix_mac2str(macaddr, buf, sizeof(buf)),
ifp->name, ifp->ifindex, vxl->access_vlan);
return -1;
}
}
/* Set "local" forwarding info. */
SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW);
memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
mac->fwd_info.local.ifindex = ifp->ifindex;
mac->fwd_info.local.vid = vxl->access_vlan;
n = zvni_neigh_lookup(zvni, ip);
if (!n) {
n = zvni_neigh_add(zvni, ip, macaddr);
if (!n) {
flog_err(
EC_ZEBRA_MAC_ADD_FAILED,
"Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
ipaddr2str(ip, buf2, sizeof(buf2)),
prefix_mac2str(macaddr, buf, sizeof(buf)),
ifp->name, ifp->ifindex, zvni->vni);
return -1;
}
}
/* Set "local" forwarding info. */
SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
SET_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW);
ZEBRA_NEIGH_SET_ACTIVE(n);
/* Set Router flag (R-bit) */
if (ip->ipa_type == IPADDR_V6)
SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
memcpy(&n->emac, macaddr, ETH_ALEN);
n->ifindex = ifp->ifindex;
/* Only advertise in BGP if the knob is enabled */
if (!advertise_gw_macip_enabled(zvni))
return 0;
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"SVI %s(%u) L2-VNI %u, sending GW MAC %s IP %s add to BGP with flags 0x%x",
ifp->name, ifp->ifindex, zvni->vni,
prefix_mac2str(macaddr, buf, sizeof(buf)),
ipaddr2str(ip, buf2, sizeof(buf2)), n->flags);
zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
n->flags, n->loc_seq);
return 0;
}
/*
* zvni_gw_macip_del_from_client
*/
static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni,
struct ipaddr *ip)
{
char buf1[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
zebra_neigh_t *n = NULL;
zebra_mac_t *mac = NULL;
/* If the neigh entry is not present nothing to do*/
n = zvni_neigh_lookup(zvni, ip);
if (!n)
return 0;
/* mac entry should be present */
mac = zvni_mac_lookup(zvni, &n->emac);
if (!mac) {
zlog_debug("MAC %s doesn't exist for neigh %s on VNI %u",
prefix_mac2str(&n->emac, buf1, sizeof(buf1)),
ipaddr2str(ip, buf2, sizeof(buf2)), zvni->vni);
return -1;
}
/* If the entry is not local nothing to do*/
if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL))
return -1;
/* only need to delete the entry from bgp if we sent it before */
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"%u:SVI %s(%u) VNI %u, sending GW MAC %s IP %s del to BGP",
ifp->vrf_id, ifp->name, ifp->ifindex, zvni->vni,
prefix_mac2str(&(n->emac), buf1, sizeof(buf1)),
ipaddr2str(ip, buf2, sizeof(buf2)));
/* Remove neighbor from BGP. */
zvni_neigh_send_del_to_client(zvni->vni, &n->ip, &n->emac,
ZEBRA_MACIP_TYPE_GW, ZEBRA_NEIGH_ACTIVE);
/* Delete this neighbor entry. */
zvni_neigh_del(zvni, n);
/* see if the mac needs to be deleted as well*/
if (mac)
zvni_deref_ip2mac(zvni, mac);
return 0;
}
static void zvni_gw_macip_del_for_vni_hash(struct hash_bucket *bucket,
void *ctxt)
{
zebra_vni_t *zvni = NULL;
struct zebra_if *zif = NULL;
struct zebra_l2info_vxlan zl2_info;
struct interface *vlan_if = NULL;
struct interface *vrr_if = NULL;
struct interface *ifp;
/* Add primary SVI MAC*/
zvni = (zebra_vni_t *)bucket->data;
ifp = zvni->vxlan_if;
if (!ifp)
return;
zif = ifp->info;
/* If down or not mapped to a bridge, we're done. */
if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
return;
zl2_info = zif->l2info.vxl;
vlan_if =
zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
if (!vlan_if)
return;
/* Del primary MAC-IP */
zvni_del_macip_for_intf(vlan_if, zvni);
/* Del VRR MAC-IP - if any*/
vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
if (vrr_if)
zvni_del_macip_for_intf(vrr_if, zvni);
return;
}
static void zvni_gw_macip_add_for_vni_hash(struct hash_bucket *bucket,
void *ctxt)
{
zebra_vni_t *zvni = NULL;
struct zebra_if *zif = NULL;
struct zebra_l2info_vxlan zl2_info;
struct interface *vlan_if = NULL;
struct interface *vrr_if = NULL;
struct interface *ifp = NULL;
zvni = (zebra_vni_t *)bucket->data;
ifp = zvni->vxlan_if;
if (!ifp)
return;
zif = ifp->info;
/* If down or not mapped to a bridge, we're done. */
if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
return;
zl2_info = zif->l2info.vxl;
vlan_if =
zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
if (!vlan_if)
return;
/* Add primary SVI MAC-IP */
zvni_add_macip_for_intf(vlan_if, zvni);
if (advertise_gw_macip_enabled(zvni)) {
/* Add VRR MAC-IP - if any*/
vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
if (vrr_if)
zvni_add_macip_for_intf(vrr_if, zvni);
}
return;
}
static void zvni_svi_macip_del_for_vni_hash(struct hash_bucket *bucket,
void *ctxt)
{
zebra_vni_t *zvni = NULL;
struct zebra_if *zif = NULL;
struct zebra_l2info_vxlan zl2_info;
struct interface *vlan_if = NULL;
struct interface *ifp;
/* Add primary SVI MAC*/
zvni = (zebra_vni_t *)bucket->data;
if (!zvni)
return;
ifp = zvni->vxlan_if;
if (!ifp)
return;
zif = ifp->info;
/* If down or not mapped to a bridge, we're done. */
if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
return;
zl2_info = zif->l2info.vxl;
vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
zif->brslave_info.br_if);
if (!vlan_if)
return;
/* Del primary MAC-IP */
zvni_del_macip_for_intf(vlan_if, zvni);
return;
}
static int zvni_local_neigh_update(zebra_vni_t *zvni,
struct interface *ifp,
struct ipaddr *ip,
struct ethaddr *macaddr,
bool is_router)
{
char buf[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
struct zebra_vrf *zvrf;
zebra_neigh_t *n = NULL;
zebra_mac_t *zmac = NULL, *old_zmac = NULL;
uint32_t old_mac_seq = 0, mac_new_seq = 0;
bool upd_mac_seq = false;
bool neigh_mac_change = false;
bool neigh_on_hold = false;
bool neigh_was_remote = false;
bool do_dad = false;
struct in_addr vtep_ip = {.s_addr = 0};
/* Check if the MAC exists. */
zmac = zvni_mac_lookup(zvni, macaddr);
if (!zmac) {
/* create a dummy MAC if the MAC is not already present */
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"AUTO MAC %s created for neigh %s on VNI %u",
prefix_mac2str(macaddr, buf, sizeof(buf)),
ipaddr2str(ip, buf2, sizeof(buf2)), zvni->vni);
zmac = zvni_mac_add(zvni, macaddr);
if (!zmac) {
zlog_debug("Failed to add MAC %s VNI %u",
prefix_mac2str(macaddr, buf, sizeof(buf)),
zvni->vni);
return -1;
}
memset(&zmac->fwd_info, 0, sizeof(zmac->fwd_info));
memset(&zmac->flags, 0, sizeof(uint32_t));
SET_FLAG(zmac->flags, ZEBRA_MAC_AUTO);
} else {
if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
/*
* We don't change the MAC to local upon a neighbor
* learn event, we wait for the explicit local MAC
* learn. However, we have to compute its sequence
* number in preparation for when it actually turns
* local.
*/
upd_mac_seq = true;
}
}
zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
if (!zvrf) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug("\tUnable to find vrf for: %d",
zvni->vxlan_if->vrf_id);
return -1;
}
/* Check if the neighbor exists. */
n = zvni_neigh_lookup(zvni, ip);
if (!n) {
/* New neighbor - create */
n = zvni_neigh_add(zvni, ip, macaddr);
if (!n) {
flog_err(
EC_ZEBRA_MAC_ADD_FAILED,
"Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
ipaddr2str(ip, buf2, sizeof(buf2)),
prefix_mac2str(macaddr, buf, sizeof(buf)),
ifp->name, ifp->ifindex, zvni->vni);
return -1;
}
/* Set "local" forwarding info. */
SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
n->ifindex = ifp->ifindex;
} else {
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
bool mac_different;
bool cur_is_router;
/* Note any changes and see if of interest to BGP. */
mac_different = (memcmp(n->emac.octet,
macaddr->octet, ETH_ALEN) != 0) ? 1 : 0;
cur_is_router = !!CHECK_FLAG(n->flags,
ZEBRA_NEIGH_ROUTER_FLAG);
if (!mac_different && is_router == cur_is_router) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"\tIgnoring entry mac is the same and is_router == cur_is_router");
n->ifindex = ifp->ifindex;
return 0;
}
if (!mac_different) {
bool is_neigh_freezed = false;
/* Only the router flag has changed. */
if (is_router)
SET_FLAG(n->flags,
ZEBRA_NEIGH_ROUTER_FLAG);
else
UNSET_FLAG(n->flags,
ZEBRA_NEIGH_ROUTER_FLAG);
/* Neigh is in freeze state and freeze action
* is enabled, do not send update to client.
*/
is_neigh_freezed = (zvrf->dup_addr_detect &&
zvrf->dad_freeze &&
CHECK_FLAG(n->flags,
ZEBRA_NEIGH_DUPLICATE));
if (IS_ZEBRA_NEIGH_ACTIVE(n) &&
!is_neigh_freezed)
return zvni_neigh_send_add_to_client(
zvni->vni, ip, macaddr,
n->flags, n->loc_seq);
else {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"\tNeighbor active and frozen");
}
return 0;
}
/* The MAC has changed, need to issue a delete
* first as this means a different MACIP route.
* Also, need to do some unlinking/relinking.
* We also need to update the MAC's sequence number
* in different situations.
*/
if (IS_ZEBRA_NEIGH_ACTIVE(n))
zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
&n->emac, 0, n->state);
old_zmac = zvni_mac_lookup(zvni, &n->emac);
if (old_zmac) {
old_mac_seq = CHECK_FLAG(old_zmac->flags,
ZEBRA_MAC_REMOTE) ?
old_zmac->rem_seq : old_zmac->loc_seq;
neigh_mac_change = upd_mac_seq = true;
listnode_delete(old_zmac->neigh_list, n);
zvni_deref_ip2mac(zvni, old_zmac);
}
/* Update the forwarding info. */
n->ifindex = ifp->ifindex;
memcpy(&n->emac, macaddr, ETH_ALEN);
/* Link to new MAC */
listnode_add_sort(zmac->neigh_list, n);
} else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
/*
* Neighbor has moved from remote to local. Its
* MAC could have also changed as part of the move.
*/
if (memcmp(n->emac.octet, macaddr->octet,
ETH_ALEN) != 0) {
old_zmac = zvni_mac_lookup(zvni, &n->emac);
if (old_zmac) {
old_mac_seq = CHECK_FLAG(
old_zmac->flags,
ZEBRA_MAC_REMOTE) ?
old_zmac->rem_seq :
old_zmac->loc_seq;
neigh_mac_change = upd_mac_seq = true;
listnode_delete(old_zmac->neigh_list,
n);
zvni_deref_ip2mac(zvni, old_zmac);
}
/* Link to new MAC */
memcpy(&n->emac, macaddr, ETH_ALEN);
listnode_add_sort(zmac->neigh_list, n);
}
/* Based on Mobility event Scenario-B from the
* draft, neigh's previous state was remote treat this
* event for DAD.
*/
neigh_was_remote = true;
vtep_ip = n->r_vtep_ip;
/* Mark appropriately */
UNSET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
n->r_vtep_ip.s_addr = 0;
SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
n->ifindex = ifp->ifindex;
}
}
/* If MAC was previously remote, or the neighbor had a different
* MAC earlier, recompute the sequence number.
*/
if (upd_mac_seq) {
uint32_t seq1, seq2;
seq1 = CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE) ?
zmac->rem_seq + 1 : zmac->loc_seq;
seq2 = neigh_mac_change ? old_mac_seq + 1 : 0;
mac_new_seq = zmac->loc_seq < MAX(seq1, seq2) ?
MAX(seq1, seq2) : zmac->loc_seq;
}
/* Mark Router flag (R-bit) */
if (is_router)
SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
else
UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
/* Check old and/or new MAC detected as duplicate mark
* the neigh as duplicate
*/
if (zebra_vxlan_ip_inherit_dad_from_mac(zvrf, old_zmac, zmac, n)) {
flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
"VNI %u: MAC %s IP %s detected as duplicate during local update, inherit duplicate from MAC",
zvni->vni,
prefix_mac2str(macaddr, buf, sizeof(buf)),
ipaddr2str(&n->ip, buf2, sizeof(buf2)));
}
/* For IP Duplicate Address Detection (DAD) is trigger,
* when the event is extended mobility based on scenario-B
* from the draft, IP/Neigh's MAC binding changed and
* neigh's previous state was remote.
*/
if (neigh_mac_change && neigh_was_remote)
do_dad = true;
zebra_vxlan_dup_addr_detect_for_neigh(zvrf, n, vtep_ip, do_dad,
&neigh_on_hold, true);
/* Before we program this in BGP, we need to check if MAC is locally
* learnt. If not, force neighbor to be inactive and reset its seq.
*/
if (!CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL)) {
ZEBRA_NEIGH_SET_INACTIVE(n);
n->loc_seq = 0;
zmac->loc_seq = mac_new_seq;
return 0;
}
/* If the MAC's sequence number has changed, inform the MAC and all
* neighbors associated with the MAC to BGP, else just inform this
* neighbor.
*/
if (upd_mac_seq && zmac->loc_seq != mac_new_seq) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug("Seq changed for MAC %s VNI %u - old %u new %u",
prefix_mac2str(macaddr, buf, sizeof(buf)),
zvni->vni, zmac->loc_seq, mac_new_seq);
zmac->loc_seq = mac_new_seq;
if (zvni_mac_send_add_to_client(zvni->vni, macaddr,
zmac->flags, zmac->loc_seq))
return -1;
zvni_process_neigh_on_local_mac_change(zvni, zmac, 1);
return 0;
}
ZEBRA_NEIGH_SET_ACTIVE(n);
n->loc_seq = zmac->loc_seq;
if (!neigh_on_hold)
return zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
n->flags, n->loc_seq);
else {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug("\tNeighbor on hold not sending");
}
return 0;
}
static int zvni_remote_neigh_update(zebra_vni_t *zvni,
struct interface *ifp,
struct ipaddr *ip,
struct ethaddr *macaddr,
uint16_t state)
{
char buf[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
zebra_neigh_t *n = NULL;
zebra_mac_t *zmac = NULL;
/* If the neighbor is unknown, there is no further action. */
n = zvni_neigh_lookup(zvni, ip);
if (!n)
return 0;
/* If a remote entry, see if it needs to be refreshed */
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
#ifdef GNU_LINUX
if (state & NUD_STALE)
zvni_neigh_install(zvni, n);
#endif
} else {
/* We got a "remote" neighbor notification for an entry
* we think is local. This can happen in a multihoming
* scenario - but only if the MAC is already "remote".
* Just mark our entry as "remote".
*/
zmac = zvni_mac_lookup(zvni, macaddr);
if (!zmac || !CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
zlog_debug(
"Ignore remote neigh %s (MAC %s) on L2-VNI %u - MAC unknown or local",
ipaddr2str(&n->ip, buf2, sizeof(buf2)),
prefix_mac2str(macaddr, buf, sizeof(buf)),
zvni->vni);
return -1;
}
UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
ZEBRA_NEIGH_SET_ACTIVE(n);
n->r_vtep_ip = zmac->fwd_info.r_vtep_ip;
}
return 0;
}
/*
* Make hash key for MAC.
*/
static unsigned int mac_hash_keymake(void *p)
{
zebra_mac_t *pmac = p;
const void *pnt = (void *)pmac->macaddr.octet;
return jhash(pnt, ETH_ALEN, 0xa5a5a55a);
}
/*
* Compare two MAC addresses.
*/
static bool mac_cmp(const void *p1, const void *p2)
{
const zebra_mac_t *pmac1 = p1;
const zebra_mac_t *pmac2 = p2;
if (pmac1 == NULL && pmac2 == NULL)
return true;
if (pmac1 == NULL || pmac2 == NULL)
return false;
return (memcmp(pmac1->macaddr.octet, pmac2->macaddr.octet, ETH_ALEN)
== 0);
}
/*
* Callback to allocate MAC hash entry.
*/
static void *zvni_mac_alloc(void *p)
{
const zebra_mac_t *tmp_mac = p;
zebra_mac_t *mac;
mac = XCALLOC(MTYPE_MAC, sizeof(zebra_mac_t));
*mac = *tmp_mac;
return ((void *)mac);
}
/*
* Add MAC entry.
*/
static zebra_mac_t *zvni_mac_add(zebra_vni_t *zvni, struct ethaddr *macaddr)
{
zebra_mac_t tmp_mac;
zebra_mac_t *mac = NULL;
memset(&tmp_mac, 0, sizeof(zebra_mac_t));
memcpy(&tmp_mac.macaddr, macaddr, ETH_ALEN);
mac = hash_get(zvni->mac_table, &tmp_mac, zvni_mac_alloc);
assert(mac);
mac->neigh_list = list_new();
mac->neigh_list->cmp = neigh_list_cmp;
return mac;
}
/*
* Delete MAC entry.
*/
static int zvni_mac_del(zebra_vni_t *zvni, zebra_mac_t *mac)
{
zebra_mac_t *tmp_mac;
list_delete(&mac->neigh_list);
/* Free the VNI hash entry and allocated memory. */
tmp_mac = hash_release(zvni->mac_table, mac);
XFREE(MTYPE_MAC, tmp_mac);
return 0;
}
/*
* Free MAC hash entry (callback)
*/
static void zvni_mac_del_hash_entry(struct hash_bucket *bucket, void *arg)
{
struct mac_walk_ctx *wctx = arg;
zebra_mac_t *mac = bucket->data;
if (((wctx->flags & DEL_LOCAL_MAC) && (mac->flags & ZEBRA_MAC_LOCAL))
|| ((wctx->flags & DEL_REMOTE_MAC)
&& (mac->flags & ZEBRA_MAC_REMOTE))
|| ((wctx->flags & DEL_REMOTE_MAC_FROM_VTEP)
&& (mac->flags & ZEBRA_MAC_REMOTE)
&& IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip,
&wctx->r_vtep_ip))) {
if (wctx->upd_client && (mac->flags & ZEBRA_MAC_LOCAL)) {
zvni_mac_send_del_to_client(wctx->zvni->vni,
&mac->macaddr);
}
if (wctx->uninstall)
zvni_mac_uninstall(wctx->zvni, mac);
zvni_mac_del(wctx->zvni, mac);
}
return;
}
/*
* Delete all MAC entries from specific VTEP for a particular VNI.
*/
static void zvni_mac_del_from_vtep(zebra_vni_t *zvni, int uninstall,
struct in_addr *r_vtep_ip)
{
struct mac_walk_ctx wctx;
if (!zvni->mac_table)
return;
memset(&wctx, 0, sizeof(struct mac_walk_ctx));
wctx.zvni = zvni;
wctx.uninstall = uninstall;
wctx.flags = DEL_REMOTE_MAC_FROM_VTEP;
wctx.r_vtep_ip = *r_vtep_ip;
hash_iterate(zvni->mac_table, zvni_mac_del_hash_entry, &wctx);
}
/*
* Delete all MAC entries for this VNI.
*/
static void zvni_mac_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
uint32_t flags)
{
struct mac_walk_ctx wctx;
if (!zvni->mac_table)
return;
memset(&wctx, 0, sizeof(struct mac_walk_ctx));
wctx.zvni = zvni;
wctx.uninstall = uninstall;
wctx.upd_client = upd_client;
wctx.flags = flags;
hash_iterate(zvni->mac_table, zvni_mac_del_hash_entry, &wctx);
}
/*
* Look up MAC hash entry.
*/
static zebra_mac_t *zvni_mac_lookup(zebra_vni_t *zvni, struct ethaddr *mac)
{
zebra_mac_t tmp;
zebra_mac_t *pmac;
memset(&tmp, 0, sizeof(tmp));
memcpy(&tmp.macaddr, mac, ETH_ALEN);
pmac = hash_lookup(zvni->mac_table, &tmp);
return pmac;
}
/*
* Inform BGP about local MAC addition.
*/
static int zvni_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr,
uint8_t mac_flags, uint32_t seq)
{
uint8_t flags = 0;
if (CHECK_FLAG(mac_flags, ZEBRA_MAC_STICKY))
SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
if (CHECK_FLAG(mac_flags, ZEBRA_MAC_DEF_GW))
SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
return zvni_macip_send_msg_to_client(vni, macaddr, NULL, flags,
seq, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_ADD);
}
/*
* Inform BGP about local MAC deletion.
*/
static int zvni_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr)
{
return zvni_macip_send_msg_to_client(vni, macaddr, NULL, 0 /* flags */,
0 /* seq */, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_DEL);
}
/*
* Map port or (port, VLAN) to a VNI. This is invoked upon getting MAC
* notifications, to see if they are of interest.
*/
static zebra_vni_t *zvni_map_vlan(struct interface *ifp,
struct interface *br_if, vlanid_t vid)
{
struct zebra_ns *zns;
struct route_node *rn;
struct interface *tmp_if = NULL;
struct zebra_if *zif;
struct zebra_l2info_bridge *br;
struct zebra_l2info_vxlan *vxl = NULL;
uint8_t bridge_vlan_aware;
zebra_vni_t *zvni;
int found = 0;
/* Determine if bridge is VLAN-aware or not */
zif = br_if->info;
assert(zif);
br = &zif->l2info.br;
bridge_vlan_aware = br->vlan_aware;
/* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
/* TODO: Optimize with a hash. */
zns = zebra_ns_lookup(NS_DEFAULT);
for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
tmp_if = (struct interface *)rn->info;
if (!tmp_if)
continue;
zif = tmp_if->info;
if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
continue;
if (!if_is_operative(tmp_if))
continue;
vxl = &zif->l2info.vxl;
if (zif->brslave_info.br_if != br_if)
continue;
if (!bridge_vlan_aware || vxl->access_vlan == vid) {
found = 1;
break;
}
}
if (!found)
return NULL;
zvni = zvni_lookup(vxl->vni);
return zvni;
}
/*
* Map SVI and associated bridge to a VNI. This is invoked upon getting
* neighbor notifications, to see if they are of interest.
*/
static zebra_vni_t *zvni_from_svi(struct interface *ifp,
struct interface *br_if)
{
struct zebra_ns *zns;
struct route_node *rn;
struct interface *tmp_if = NULL;
struct zebra_if *zif;
struct zebra_l2info_bridge *br;
struct zebra_l2info_vxlan *vxl = NULL;
uint8_t bridge_vlan_aware;
vlanid_t vid = 0;
zebra_vni_t *zvni;
int found = 0;
if (!br_if)
return NULL;
/* Make sure the linked interface is a bridge. */
if (!IS_ZEBRA_IF_BRIDGE(br_if))
return NULL;
/* Determine if bridge is VLAN-aware or not */
zif = br_if->info;
assert(zif);
br = &zif->l2info.br;
bridge_vlan_aware = br->vlan_aware;
if (bridge_vlan_aware) {
struct zebra_l2info_vlan *vl;
if (!IS_ZEBRA_IF_VLAN(ifp))
return NULL;
zif = ifp->info;
assert(zif);
vl = &zif->l2info.vl;
vid = vl->vid;
}
/* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
/* TODO: Optimize with a hash. */
zns = zebra_ns_lookup(NS_DEFAULT);
for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
tmp_if = (struct interface *)rn->info;
if (!tmp_if)
continue;
zif = tmp_if->info;
if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
continue;
if (!if_is_operative(tmp_if))
continue;
vxl = &zif->l2info.vxl;
if (zif->brslave_info.br_if != br_if)
continue;
if (!bridge_vlan_aware || vxl->access_vlan == vid) {
found = 1;
break;
}
}
if (!found)
return NULL;
zvni = zvni_lookup(vxl->vni);
return zvni;
}
/* Map to SVI on bridge corresponding to specified VLAN. This can be one
* of two cases:
* (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN interface
* linked to the bridge
* (b) In the case of a VLAN-unaware bridge, the SVI is the bridge inteface
* itself
*/
static struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if)
{
struct zebra_ns *zns;
struct route_node *rn;
struct interface *tmp_if = NULL;
struct zebra_if *zif;
struct zebra_l2info_bridge *br;
struct zebra_l2info_vlan *vl;
uint8_t bridge_vlan_aware;
int found = 0;
/* Defensive check, caller expected to invoke only with valid bridge. */
if (!br_if)
return NULL;
/* Determine if bridge is VLAN-aware or not */
zif = br_if->info;
assert(zif);
br = &zif->l2info.br;
bridge_vlan_aware = br->vlan_aware;
/* Check oper status of the SVI. */
if (!bridge_vlan_aware)
return if_is_operative(br_if) ? br_if : NULL;
/* Identify corresponding VLAN interface. */
/* TODO: Optimize with a hash. */
zns = zebra_ns_lookup(NS_DEFAULT);
for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
tmp_if = (struct interface *)rn->info;
/* Check oper status of the SVI. */
if (!tmp_if || !if_is_operative(tmp_if))
continue;
zif = tmp_if->info;
if (!zif || zif->zif_type != ZEBRA_IF_VLAN
|| zif->link != br_if)
continue;
vl = (struct zebra_l2info_vlan *)&zif->l2info.vl;
if (vl->vid == vid) {
found = 1;
break;
}
}
return found ? tmp_if : NULL;
}
/*
* Install remote MAC into the kernel.
*/
static int zvni_mac_install(zebra_vni_t *zvni, zebra_mac_t *mac)
{
struct zebra_if *zif;
struct zebra_l2info_vxlan *vxl;
bool sticky;
if (!(mac->flags & ZEBRA_MAC_REMOTE))
return 0;
zif = zvni->vxlan_if->info;
if (!zif)
return -1;
vxl = &zif->l2info.vxl;
sticky = !!CHECK_FLAG(mac->flags,
(ZEBRA_MAC_STICKY | ZEBRA_MAC_REMOTE_DEF_GW));
return kernel_add_mac(zvni->vxlan_if, vxl->access_vlan, &mac->macaddr,
mac->fwd_info.r_vtep_ip, sticky);
}
/*
* Uninstall remote MAC from the kernel.
*/
static int zvni_mac_uninstall(zebra_vni_t *zvni, zebra_mac_t *mac)
{
struct zebra_if *zif;
struct zebra_l2info_vxlan *vxl;
struct in_addr vtep_ip;
struct interface *ifp;
if (!(mac->flags & ZEBRA_MAC_REMOTE))
return 0;
if (!zvni->vxlan_if) {
zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
zvni->vni, zvni);
return -1;
}
zif = zvni->vxlan_if->info;
if (!zif)
return -1;
vxl = &zif->l2info.vxl;
ifp = zvni->vxlan_if;
vtep_ip = mac->fwd_info.r_vtep_ip;
return kernel_del_mac(ifp, vxl->access_vlan, &mac->macaddr, vtep_ip);
}
/*
* Install MAC hash entry - called upon access VLAN change.
*/
static void zvni_install_mac_hash(struct hash_bucket *bucket, void *ctxt)
{
zebra_mac_t *mac;
struct mac_walk_ctx *wctx = ctxt;
mac = (zebra_mac_t *)bucket->data;
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
zvni_mac_install(wctx->zvni, mac);
}
/*
* Count of remote neighbors referencing this MAC.
*/
static int remote_neigh_count(zebra_mac_t *zmac)
{
zebra_neigh_t *n = NULL;
struct listnode *node = NULL;
int count = 0;
for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
count++;
}
return count;
}
/*
* Decrement neighbor refcount of MAC; uninstall and free it if
* appropriate.
*/
static void zvni_deref_ip2mac(zebra_vni_t *zvni, zebra_mac_t *mac)
{
if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
return;
/* If all remote neighbors referencing a remote MAC go away,
* we need to uninstall the MAC.
*/
if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) &&
remote_neigh_count(mac) == 0) {
zvni_mac_uninstall(zvni, mac);
UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
}
/* If no neighbors, delete the MAC. */
if (list_isempty(mac->neigh_list))
zvni_mac_del(zvni, mac);
}
/*
* Read and populate local MACs and neighbors corresponding to this VNI.
*/
static void zvni_read_mac_neigh(zebra_vni_t *zvni, struct interface *ifp)
{
struct zebra_ns *zns;
struct zebra_if *zif;
struct interface *vlan_if;
struct zebra_l2info_vxlan *vxl;
struct interface *vrr_if;
zif = ifp->info;
vxl = &zif->l2info.vxl;
zns = zebra_ns_lookup(NS_DEFAULT);
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"Reading MAC FDB and Neighbors for intf %s(%u) VNI %u master %u",
ifp->name, ifp->ifindex, zvni->vni,
zif->brslave_info.bridge_ifindex);
macfdb_read_for_bridge(zns, ifp, zif->brslave_info.br_if);
vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
if (vlan_if) {
/* Add SVI MAC-IP */
zvni_add_macip_for_intf(vlan_if, zvni);
/* Add VRR MAC-IP - if any*/
vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
if (vrr_if)
zvni_add_macip_for_intf(vrr_if, zvni);
neigh_read_for_vlan(zns, vlan_if);
}
}
/*
* Hash function for VNI.
*/
static unsigned int vni_hash_keymake(void *p)
{
const zebra_vni_t *zvni = p;
return (jhash_1word(zvni->vni, 0));
}
/*
* Compare 2 VNI hash entries.
*/
static bool vni_hash_cmp(const void *p1, const void *p2)
{
const zebra_vni_t *zvni1 = p1;
const zebra_vni_t *zvni2 = p2;
return (zvni1->vni == zvni2->vni);
}
static int vni_list_cmp(void *p1, void *p2)
{
const zebra_vni_t *zvni1 = p1;
const zebra_vni_t *zvni2 = p2;
if (zvni1->vni == zvni2->vni)
return 0;
return (zvni1->vni < zvni2->vni) ? -1 : 1;
}
/*
* Callback to allocate VNI hash entry.
*/
static void *zvni_alloc(void *p)
{
const zebra_vni_t *tmp_vni = p;
zebra_vni_t *zvni;
zvni = XCALLOC(MTYPE_ZVNI, sizeof(zebra_vni_t));
zvni->vni = tmp_vni->vni;
return ((void *)zvni);
}
/*
* Look up VNI hash entry.
*/
static zebra_vni_t *zvni_lookup(vni_t vni)
{
struct zebra_vrf *zvrf;
zebra_vni_t tmp_vni;
zebra_vni_t *zvni = NULL;
zvrf = vrf_info_lookup(VRF_DEFAULT);
assert(zvrf);
memset(&tmp_vni, 0, sizeof(zebra_vni_t));
tmp_vni.vni = vni;
zvni = hash_lookup(zvrf->vni_table, &tmp_vni);
return zvni;
}
/*
* Add VNI hash entry.
*/
static zebra_vni_t *zvni_add(vni_t vni)
{
struct zebra_vrf *zvrf;
zebra_vni_t tmp_zvni;
zebra_vni_t *zvni = NULL;
zvrf = vrf_info_lookup(VRF_DEFAULT);
assert(zvrf);
memset(&tmp_zvni, 0, sizeof(zebra_vni_t));
tmp_zvni.vni = vni;
zvni = hash_get(zvrf->vni_table, &tmp_zvni, zvni_alloc);
assert(zvni);
/* Create hash table for MAC */
zvni->mac_table =
hash_create(mac_hash_keymake, mac_cmp, "Zebra VNI MAC Table");
/* Create hash table for neighbors */
zvni->neigh_table = hash_create(neigh_hash_keymake, neigh_cmp,
"Zebra VNI Neighbor Table");
return zvni;
}
/*
* Delete VNI hash entry.
*/
static int zvni_del(