]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vxlan.c
Merge pull request #5034 from opensourcerouting/rm-topotest-cruft
[mirror_frr.git] / zebra / zebra_vxlan.c
CommitLineData
13d60d35 1/*
2 * Zebra EVPN for VxLAN code
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
4 *
5 * This file is part of FRR.
6 *
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * FRR is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
43e52561 25#include "hash.h"
13d60d35 26#include "if.h"
43e52561 27#include "jhash.h"
13d60d35 28#include "linklist.h"
43e52561
QY
29#include "log.h"
30#include "memory.h"
31#include "prefix.h"
13d60d35 32#include "stream.h"
43e52561 33#include "table.h"
13d60d35 34#include "vlan.h"
35#include "vxlan.h"
68e33151
CS
36#ifdef GNU_LINUX
37#include <linux/neighbour.h>
38#endif
13d60d35 39
89272910 40#include "zebra/zebra_router.h"
43e52561
QY
41#include "zebra/debug.h"
42#include "zebra/interface.h"
13d60d35 43#include "zebra/rib.h"
44#include "zebra/rt.h"
43e52561
QY
45#include "zebra/rt_netlink.h"
46#include "zebra/zebra_errors.h"
47#include "zebra/zebra_l2.h"
48#include "zebra/zebra_memory.h"
13d60d35 49#include "zebra/zebra_ns.h"
13d60d35 50#include "zebra/zebra_vrf.h"
13d60d35 51#include "zebra/zebra_vxlan.h"
43e52561 52#include "zebra/zebra_vxlan_private.h"
3801e764 53#include "zebra/zebra_router.h"
13d60d35 54
6134fd82 55DEFINE_MTYPE_STATIC(ZEBRA, HOST_PREFIX, "host prefix");
d62a17ae 56DEFINE_MTYPE_STATIC(ZEBRA, ZVNI, "VNI hash");
b7cfce93 57DEFINE_MTYPE_STATIC(ZEBRA, ZL3VNI, "L3 VNI hash");
13d60d35 58DEFINE_MTYPE_STATIC(ZEBRA, ZVNI_VTEP, "VNI remote VTEP");
d62a17ae 59DEFINE_MTYPE_STATIC(ZEBRA, MAC, "VNI MAC");
60DEFINE_MTYPE_STATIC(ZEBRA, NEIGH, "VNI Neighbor");
8a93734c 61DEFINE_MTYPE_STATIC(ZEBRA, ZVXLAN_SG, "zebra VxLAN multicast group");
13d60d35 62
a780a738
AD
63DEFINE_HOOK(zebra_rmac_update, (zebra_mac_t *rmac, zebra_l3vni_t *zl3vni,
64 bool delete, const char *reason), (rmac, zl3vni, delete, reason))
65
13d60d35 66/* definitions */
9718c54e
AK
67/* PMSI strings. */
68#define VXLAN_FLOOD_STR_NO_INFO "-"
69#define VXLAN_FLOOD_STR_DEFAULT VXLAN_FLOOD_STR_NO_INFO
70static const struct message zvtep_flood_str[] = {
71 {VXLAN_FLOOD_DISABLED, VXLAN_FLOOD_STR_NO_INFO},
72 {VXLAN_FLOOD_PIM_SM, "PIM-SM"},
73 {VXLAN_FLOOD_HEAD_END_REPL, "HER"},
74 {0}
75};
76
13d60d35 77
13d60d35 78/* static function declarations */
996c9314
LB
79static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
80 uint16_t cmd);
cd233079 81static void zvni_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json);
e3b78da8
TB
82static void zvni_print_neigh_hash(struct hash_bucket *bucket, void *ctxt);
83static void zvni_print_dad_neigh_hash(struct hash_bucket *bucket, void *ctxt);
84static void zvni_print_neigh_hash_all_vni(struct hash_bucket *bucket,
cd233079 85 void **args);
c0e519d3
MK
86static void zl3vni_print_nh(zebra_neigh_t *n, struct vty *vty,
87 json_object *json);
316f4ca4
MK
88static void zl3vni_print_rmac(zebra_mac_t *zrmac, struct vty *vty,
89 json_object *json);
24cdbd0d 90static void zvni_print_mac(zebra_mac_t *mac, void *ctxt, json_object *json);
e3b78da8
TB
91static void zvni_print_mac_hash(struct hash_bucket *bucket, void *ctxt);
92static void zvni_print_mac_hash_all_vni(struct hash_bucket *bucket, void *ctxt);
cd233079 93static void zvni_print(zebra_vni_t *zvni, void **ctxt);
e3b78da8 94static void zvni_print_hash(struct hash_bucket *bucket, void *ctxt[]);
d62a17ae 95
996c9314 96static int zvni_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr,
d7c0a89a 97 struct ipaddr *ip, uint8_t flags,
2c476b72 98 uint32_t seq, int state, uint16_t cmd);
d8b87afe 99static unsigned int neigh_hash_keymake(const void *p);
d62a17ae 100static void *zvni_neigh_alloc(void *p);
b6938a74
MK
101static zebra_neigh_t *zvni_neigh_add(zebra_vni_t *zvni, struct ipaddr *ip,
102 struct ethaddr *mac);
d62a17ae 103static int zvni_neigh_del(zebra_vni_t *zvni, zebra_neigh_t *n);
d62a17ae 104static void zvni_neigh_del_from_vtep(zebra_vni_t *zvni, int uninstall,
105 struct in_addr *r_vtep_ip);
996c9314 106static void zvni_neigh_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
d7c0a89a 107 uint32_t flags);
d62a17ae 108static zebra_neigh_t *zvni_neigh_lookup(zebra_vni_t *zvni, struct ipaddr *ip);
996c9314 109static int zvni_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
d7c0a89a 110 struct ethaddr *macaddr,
f07e1c99 111 uint8_t flags, uint32_t seq);
996c9314 112static int zvni_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
d7c0a89a 113 struct ethaddr *macaddr,
2c476b72 114 uint8_t flags, int state);
d62a17ae 115static int zvni_neigh_install(zebra_vni_t *zvni, zebra_neigh_t *n);
116static int zvni_neigh_uninstall(zebra_vni_t *zvni, zebra_neigh_t *n);
44bc8ae5 117static int zvni_neigh_probe(zebra_vni_t *zvni, zebra_neigh_t *n);
b7cfce93 118static zebra_vni_t *zvni_from_svi(struct interface *ifp,
996c9314
LB
119 struct interface *br_if);
120static struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if);
d62a17ae 121
b7cfce93 122/* l3-vni next-hop neigh related APIs */
2dbad57f 123static zebra_neigh_t *zl3vni_nh_lookup(zebra_l3vni_t *zl3vni,
b7cfce93
MK
124 struct ipaddr *ip);
125static void *zl3vni_nh_alloc(void *p);
126static zebra_neigh_t *zl3vni_nh_add(zebra_l3vni_t *zl3vni,
127 struct ipaddr *vtep_ip,
128 struct ethaddr *rmac);
129static int zl3vni_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
130static int zl3vni_nh_install(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
2dbad57f 131static int zl3vni_nh_uninstall(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
b7cfce93
MK
132
133/* l3-vni rmac related APIs */
e3b78da8 134static void zl3vni_print_rmac_hash(struct hash_bucket *, void *);
2dbad57f 135static zebra_mac_t *zl3vni_rmac_lookup(zebra_l3vni_t *zl3vni,
b7cfce93
MK
136 struct ethaddr *rmac);
137static void *zl3vni_rmac_alloc(void *p);
138static zebra_mac_t *zl3vni_rmac_add(zebra_l3vni_t *zl3vni,
139 struct ethaddr *rmac);
140static int zl3vni_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
141static int zl3vni_rmac_install(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
996c9314 142static int zl3vni_rmac_uninstall(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
b7cfce93
MK
143
144/* l3-vni related APIs*/
b7cfce93
MK
145static zebra_l3vni_t *zl3vni_lookup(vni_t vni);
146static void *zl3vni_alloc(void *p);
147static zebra_l3vni_t *zl3vni_add(vni_t vni, vrf_id_t vrf_id);
148static int zl3vni_del(zebra_l3vni_t *zl3vni);
b7cfce93
MK
149static void zebra_vxlan_process_l3vni_oper_up(zebra_l3vni_t *zl3vni);
150static void zebra_vxlan_process_l3vni_oper_down(zebra_l3vni_t *zl3vni);
151
d8b87afe 152static unsigned int mac_hash_keymake(const void *p);
74df8d6d 153static bool mac_cmp(const void *p1, const void *p2);
d62a17ae 154static void *zvni_mac_alloc(void *p);
155static zebra_mac_t *zvni_mac_add(zebra_vni_t *zvni, struct ethaddr *macaddr);
156static int zvni_mac_del(zebra_vni_t *zvni, zebra_mac_t *mac);
d62a17ae 157static void zvni_mac_del_from_vtep(zebra_vni_t *zvni, int uninstall,
158 struct in_addr *r_vtep_ip);
996c9314 159static void zvni_mac_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
d7c0a89a 160 uint32_t flags);
d62a17ae 161static zebra_mac_t *zvni_mac_lookup(zebra_vni_t *zvni, struct ethaddr *macaddr);
996c9314 162static int zvni_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr,
f07e1c99 163 uint8_t flags, uint32_t seq);
e98e4b88 164static int zvni_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr);
d62a17ae 165static zebra_vni_t *zvni_map_vlan(struct interface *ifp,
166 struct interface *br_if, vlanid_t vid);
167static int zvni_mac_install(zebra_vni_t *zvni, zebra_mac_t *mac);
d63c1b18 168static int zvni_mac_uninstall(zebra_vni_t *zvni, zebra_mac_t *mac);
e3b78da8 169static void zvni_install_mac_hash(struct hash_bucket *bucket, void *ctxt);
d62a17ae 170
d8b87afe 171static unsigned int vni_hash_keymake(const void *p);
d62a17ae 172static void *zvni_alloc(void *p);
2853fed6 173static zebra_vni_t *zvni_lookup(vni_t vni);
174static zebra_vni_t *zvni_add(vni_t vni);
175static int zvni_del(zebra_vni_t *zvni);
176static int zvni_send_add_to_client(zebra_vni_t *zvni);
177static int zvni_send_del_to_client(vni_t vni);
4d762f26 178static void zvni_build_hash_table(void);
d62a17ae 179static int zvni_vtep_match(struct in_addr *vtep_ip, zebra_vtep_t *zvtep);
180static zebra_vtep_t *zvni_vtep_find(zebra_vni_t *zvni, struct in_addr *vtep_ip);
9718c54e
AK
181static zebra_vtep_t *zvni_vtep_add(zebra_vni_t *zvni, struct in_addr *vtep_ip,
182 int flood_control);
d62a17ae 183static int zvni_vtep_del(zebra_vni_t *zvni, zebra_vtep_t *zvtep);
184static int zvni_vtep_del_all(zebra_vni_t *zvni, int uninstall);
9718c54e 185static int zvni_vtep_install(zebra_vni_t *zvni, zebra_vtep_t *zvtep);
d62a17ae 186static int zvni_vtep_uninstall(zebra_vni_t *zvni, struct in_addr *vtep_ip);
1a98c087
MK
187static int zvni_del_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni);
188static int zvni_add_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni);
189static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni,
190 struct ethaddr *macaddr, struct ipaddr *ip);
191static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni,
192 struct ipaddr *ip);
193struct interface *zebra_get_vrr_intf_for_svi(struct interface *ifp);
2853fed6 194static int advertise_gw_macip_enabled(zebra_vni_t *zvni);
278e26de 195static int advertise_svi_macip_enabled(zebra_vni_t *zvni);
e22a946a
CS
196static int zebra_vxlan_ip_inherit_dad_from_mac(struct zebra_vrf *zvrf,
197 zebra_mac_t *old_zmac,
198 zebra_mac_t *new_zmac,
199 zebra_neigh_t *nbr);
fe697c6b 200static int remote_neigh_count(zebra_mac_t *zmac);
201static void zvni_deref_ip2mac(zebra_vni_t *zvni, zebra_mac_t *mac);
c80a972c
CS
202static int zebra_vxlan_dad_mac_auto_recovery_exp(struct thread *t);
203static int zebra_vxlan_dad_ip_auto_recovery_exp(struct thread *t);
7510e459
CS
204static void zebra_vxlan_dup_addr_detect_for_neigh(struct zebra_vrf *zvrf,
205 zebra_neigh_t *nbr,
206 struct in_addr vtep_ip,
207 bool do_dad,
208 bool *is_dup_detect,
209 bool is_local);
bdca1974
CS
210static void zebra_vxlan_dup_addr_detect_for_mac(struct zebra_vrf *zvrf,
211 zebra_mac_t *mac,
212 struct in_addr vtep_ip,
213 bool do_dad,
214 bool *is_dup_detect,
215 bool is_local);
d8b87afe 216static unsigned int zebra_vxlan_sg_hash_key_make(const void *p);
015d264c
AK
217static bool zebra_vxlan_sg_hash_eq(const void *p1, const void *p2);
218static void zebra_vxlan_sg_do_deref(struct zebra_vrf *zvrf,
219 struct in_addr sip, struct in_addr mcast_grp);
220static zebra_vxlan_sg_t *zebra_vxlan_sg_do_ref(struct zebra_vrf *vrf,
221 struct in_addr sip, struct in_addr mcast_grp);
abfa0a96
AK
222static void zebra_vxlan_sg_deref(struct in_addr local_vtep_ip,
223 struct in_addr mcast_grp);
224static void zebra_vxlan_sg_ref(struct in_addr local_vtep_ip,
225 struct in_addr mcast_grp);
226static void zebra_vxlan_sg_cleanup(struct hash_backet *backet, void *arg);
13d60d35 227
27627f9a
KA
228static void zvni_send_mac_to_client(zebra_vni_t *zvn);
229static void zvni_send_neigh_to_client(zebra_vni_t *zvni);
230
13d60d35 231/* Private functions */
5e1b0650 232static int host_rb_entry_compare(const struct host_rb_entry *hle1,
85442b09 233 const struct host_rb_entry *hle2)
5e1b0650
DS
234{
235 if (hle1->p.family < hle2->p.family)
236 return -1;
237
238 if (hle1->p.family > hle2->p.family)
239 return 1;
240
241 if (hle1->p.prefixlen < hle2->p.prefixlen)
242 return -1;
243
244 if (hle1->p.prefixlen > hle2->p.prefixlen)
245 return 1;
246
247 if (hle1->p.family == AF_INET) {
248 if (hle1->p.u.prefix4.s_addr < hle2->p.u.prefix4.s_addr)
249 return -1;
250
251 if (hle1->p.u.prefix4.s_addr > hle2->p.u.prefix4.s_addr)
252 return 1;
253
254 return 0;
2acc2310
DS
255 } else if (hle1->p.family == AF_INET6) {
256 return memcmp(&hle1->p.u.prefix6, &hle2->p.u.prefix6,
257 IPV6_MAX_BYTELEN);
5e1b0650 258 } else {
9df414fe
QY
259 zlog_debug("%s: Unexpected family type: %d",
260 __PRETTY_FUNCTION__, hle1->p.family);
5e1b0650
DS
261 return 0;
262 }
263}
85442b09 264RB_GENERATE(host_rb_tree_entry, host_rb_entry, hl_entry, host_rb_entry_compare);
5e1b0650 265
85442b09 266static uint32_t rb_host_count(struct host_rb_tree_entry *hrbe)
5e1b0650
DS
267{
268 struct host_rb_entry *hle;
269 uint32_t count = 0;
270
85442b09 271 RB_FOREACH (hle, host_rb_tree_entry, hrbe)
5e1b0650
DS
272 count++;
273
274 return count;
275}
13d60d35 276
790f8dc5 277/*
278 * Return number of valid MACs in a VNI's MAC hash table - all
279 * remote MACs and non-internal (auto) local MACs count.
280 */
d7c0a89a 281static uint32_t num_valid_macs(zebra_vni_t *zvni)
790f8dc5 282{
283 unsigned int i;
d7c0a89a 284 uint32_t num_macs = 0;
790f8dc5 285 struct hash *hash;
e3b78da8 286 struct hash_bucket *hb;
790f8dc5 287 zebra_mac_t *mac;
288
289 hash = zvni->mac_table;
290 if (!hash)
291 return num_macs;
292 for (i = 0; i < hash->size; i++) {
293 for (hb = hash->index[i]; hb; hb = hb->next) {
294 mac = (zebra_mac_t *)hb->data;
295 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
63ce2ee7 296 || CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
790f8dc5 297 || !CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
298 num_macs++;
299 }
300 }
301
302 return num_macs;
303}
304
1374d4db
CS
305static uint32_t num_dup_detected_macs(zebra_vni_t *zvni)
306{
307 unsigned int i;
308 uint32_t num_macs = 0;
309 struct hash *hash;
e3b78da8 310 struct hash_bucket *hb;
1374d4db
CS
311 zebra_mac_t *mac;
312
313 hash = zvni->mac_table;
314 if (!hash)
315 return num_macs;
316 for (i = 0; i < hash->size; i++) {
317 for (hb = hash->index[i]; hb; hb = hb->next) {
318 mac = (zebra_mac_t *)hb->data;
319 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
320 num_macs++;
321 }
322 }
323
324 return num_macs;
325}
326
327static uint32_t num_dup_detected_neighs(zebra_vni_t *zvni)
328{
329 unsigned int i;
330 uint32_t num_neighs = 0;
331 struct hash *hash;
e3b78da8 332 struct hash_bucket *hb;
1374d4db
CS
333 zebra_neigh_t *nbr;
334
335 hash = zvni->neigh_table;
336 if (!hash)
337 return num_neighs;
338 for (i = 0; i < hash->size; i++) {
339 for (hb = hash->index[i]; hb; hb = hb->next) {
340 nbr = (zebra_neigh_t *)hb->data;
341 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
342 num_neighs++;
343 }
344 }
345
346 return num_neighs;
347}
348
2853fed6 349static int advertise_gw_macip_enabled(zebra_vni_t *zvni)
1a98c087 350{
2853fed6 351 struct zebra_vrf *zvrf;
352
150971b5 353 zvrf = zebra_vrf_get_evpn();
1a98c087
MK
354 if (zvrf && zvrf->advertise_gw_macip)
355 return 1;
356
357 if (zvni && zvni->advertise_gw_macip)
358 return 1;
359
360 return 0;
361}
362
278e26de
CS
363static int advertise_svi_macip_enabled(zebra_vni_t *zvni)
364{
365 struct zebra_vrf *zvrf;
366
a0b0b5c8 367 zvrf = zebra_vrf_get_evpn();
278e26de
CS
368 if (zvrf && zvrf->advertise_svi_macip)
369 return 1;
370
371 if (zvni && zvni->advertise_svi_macip)
372 return 1;
373
374 return 0;
375}
376
e22a946a 377/* As part Duplicate Address Detection (DAD) for IP mobility
979777b2
CS
378 * MAC binding changes, ensure to inherit duplicate flag
379 * from MAC.
e22a946a
CS
380 */
381static int zebra_vxlan_ip_inherit_dad_from_mac(struct zebra_vrf *zvrf,
382 zebra_mac_t *old_zmac,
383 zebra_mac_t *new_zmac,
384 zebra_neigh_t *nbr)
385{
386 bool is_old_mac_dup = false;
387 bool is_new_mac_dup = false;
388
389 if (!zvrf->dup_addr_detect)
390 return 0;
391 /* Check old or new MAC is detected as duplicate
392 * mark this neigh as duplicate
393 */
394 if (old_zmac)
395 is_old_mac_dup = CHECK_FLAG(old_zmac->flags,
396 ZEBRA_MAC_DUPLICATE);
397 if (new_zmac)
398 is_new_mac_dup = CHECK_FLAG(new_zmac->flags,
399 ZEBRA_MAC_DUPLICATE);
400 /* Old and/or new MAC can be in duplicate state,
401 * based on that IP/Neigh Inherits the flag.
402 * If New MAC is marked duplicate, inherit to the IP.
403 * If old MAC is duplicate but new MAC is not, clear
404 * duplicate flag for IP and reset detection params
405 * and let IP DAD retrigger.
406 */
407 if (is_new_mac_dup && !CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
408 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
409 /* Capture Duplicate detection time */
410 nbr->dad_dup_detect_time = monotime(NULL);
c34e362b
CS
411 /* Mark neigh inactive */
412 ZEBRA_NEIGH_SET_INACTIVE(nbr);
413
e22a946a
CS
414 return 1;
415 } else if (is_old_mac_dup && !is_new_mac_dup) {
416 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
417 nbr->dad_count = 0;
418 nbr->detect_start_time.tv_sec = 0;
419 nbr->detect_start_time.tv_usec = 0;
420 }
421 return 0;
422}
423
bdca1974
CS
424static void zebra_vxlan_dup_addr_detect_for_mac(struct zebra_vrf *zvrf,
425 zebra_mac_t *mac,
426 struct in_addr vtep_ip,
427 bool do_dad,
428 bool *is_dup_detect,
429 bool is_local)
430{
431 zebra_neigh_t *nbr;
432 struct listnode *node = NULL;
433 struct timeval elapsed = {0, 0};
434 char buf[ETHER_ADDR_STRLEN];
435 char buf1[INET6_ADDRSTRLEN];
436 bool reset_params = false;
437
438 if (!(zvrf->dup_addr_detect && do_dad))
439 return;
440
441 /* MAC is detected as duplicate,
442 * Local MAC event -> hold on advertising to BGP.
443 * Remote MAC event -> hold on installing it.
444 */
445 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
446 if (IS_ZEBRA_DEBUG_VXLAN)
447 zlog_debug(
448 "%s: duplicate addr MAC %s flags 0x%x skip update to client, learn count %u recover time %u",
449 __PRETTY_FUNCTION__,
450 prefix_mac2str(&mac->macaddr, buf,
451 sizeof(buf)),
452 mac->flags, mac->dad_count,
453 zvrf->dad_freeze_time);
454
455 /* For duplicate MAC do not update
456 * client but update neigh due to
457 * this MAC update.
458 */
459 if (zvrf->dad_freeze)
a4445ece 460 *is_dup_detect = true;
bdca1974
CS
461
462 return;
463 }
464
465 /* Check if detection time (M-secs) expired.
466 * Reset learn count and detection start time.
467 */
468 monotime_since(&mac->detect_start_time, &elapsed);
469 reset_params = (elapsed.tv_sec > zvrf->dad_time);
470 if (is_local && !reset_params) {
471 /* RFC-7432: A PE/VTEP that detects a MAC mobility
472 * event via LOCAL learning starts an M-second timer.
473 *
474 * NOTE: This is the START of the probe with count is
475 * 0 during LOCAL learn event.
476 * (mac->dad_count == 0 || elapsed.tv_sec >= zvrf->dad_time)
477 */
478 reset_params = !mac->dad_count;
479 }
480
481 if (reset_params) {
482 if (IS_ZEBRA_DEBUG_VXLAN)
483 zlog_debug(
484 "%s: duplicate addr MAC %s flags 0x%x detection time passed, reset learn count %u"
485 , __PRETTY_FUNCTION__,
486 prefix_mac2str(&mac->macaddr, buf,
487 sizeof(buf)),
488 mac->flags, mac->dad_count);
489
490 mac->dad_count = 0;
491 /* Start dup. addr detection (DAD) start time,
492 * ONLY during LOCAL learn.
493 */
494 if (is_local)
495 monotime(&mac->detect_start_time);
496
497 } else if (!is_local) {
498 /* For REMOTE MAC, increment detection count
499 * ONLY while in probe window, once window passed,
500 * next local learn event should trigger DAD.
501 */
502 mac->dad_count++;
503 }
504
505 /* For LOCAL MAC learn event, once count is reset above via either
506 * initial/start detection time or passed the probe time, the count
507 * needs to be incremented.
508 */
509 if (is_local)
510 mac->dad_count++;
511
bdca1974
CS
512 if (mac->dad_count >= zvrf->dad_max_moves) {
513 flog_warn(EC_ZEBRA_DUP_MAC_DETECTED,
514 "VNI %u: MAC %s detected as duplicate during %s VTEP %s",
515 mac->zvni->vni,
516 prefix_mac2str(&mac->macaddr, buf, sizeof(buf)),
517 is_local ? "local update, last" :
518 "remote update, from", inet_ntoa(vtep_ip));
519
520 SET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
521
522 /* Capture Duplicate detection time */
523 mac->dad_dup_detect_time = monotime(NULL);
524
525 /* Mark all IPs/Neighs as duplicate
526 * associcated with this MAC
527 */
528 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
529
530 /* Ony Mark IPs which are Local */
531 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
532 continue;
533
534 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
535
536 nbr->dad_dup_detect_time = monotime(NULL);
537
538 flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
539 "VNI %u: MAC %s IP %s detected as duplicate during %s update, inherit duplicate from MAC",
540 mac->zvni->vni,
541 prefix_mac2str(&mac->macaddr,
542 buf, sizeof(buf)),
543 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
544 is_local ? "local" : "remote");
545 }
546
547 /* Start auto recovery timer for this MAC */
548 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
549 if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
550 if (IS_ZEBRA_DEBUG_VXLAN)
551 zlog_debug(
552 "%s: duplicate addr MAC %s flags 0x%x auto recovery time %u start"
553 , __PRETTY_FUNCTION__,
554 prefix_mac2str(&mac->macaddr, buf,
555 sizeof(buf)),
556 mac->flags, zvrf->dad_freeze_time);
557
3801e764 558 thread_add_timer(zrouter.master,
bdca1974
CS
559 zebra_vxlan_dad_mac_auto_recovery_exp,
560 mac, zvrf->dad_freeze_time,
561 &mac->dad_mac_auto_recovery_timer);
562 }
563
a4445ece 564 /* In case of local update, do not inform to client (BGPd),
bdca1974
CS
565 * upd_neigh for neigh sequence change.
566 */
567 if (zvrf->dad_freeze)
a4445ece 568 *is_dup_detect = true;
bdca1974
CS
569 }
570}
571
7510e459
CS
572static void zebra_vxlan_dup_addr_detect_for_neigh(struct zebra_vrf *zvrf,
573 zebra_neigh_t *nbr,
574 struct in_addr vtep_ip,
575 bool do_dad,
576 bool *is_dup_detect,
577 bool is_local)
578{
579
580 struct timeval elapsed = {0, 0};
581 char buf[ETHER_ADDR_STRLEN];
582 char buf1[INET6_ADDRSTRLEN];
583 bool reset_params = false;
584
585 if (!zvrf->dup_addr_detect)
586 return;
587
588 /* IP is detected as duplicate or inherit dup
589 * state, hold on to install as remote entry
590 * only if freeze is enabled.
591 */
592 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
593 if (IS_ZEBRA_DEBUG_VXLAN)
594 zlog_debug(
595 "%s: duplicate addr MAC %s IP %s flags 0x%x skip installing, learn count %u recover time %u",
596 __PRETTY_FUNCTION__,
597 prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
598 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
599 nbr->flags, nbr->dad_count,
600 zvrf->dad_freeze_time);
601
602 if (zvrf->dad_freeze)
603 *is_dup_detect = true;
753ad4fe 604
7510e459
CS
605 /* warn-only action, neigh will be installed.
606 * freeze action, it wil not be installed.
607 */
608 return;
609 }
610
611 if (!do_dad)
612 return;
613
614 /* Check if detection time (M-secs) expired.
615 * Reset learn count and detection start time.
616 * During remote mac add, count should already be 1
617 * via local learning.
618 */
619 monotime_since(&nbr->detect_start_time, &elapsed);
620 reset_params = (elapsed.tv_sec > zvrf->dad_time);
621
622 if (is_local && !reset_params) {
623 /* RFC-7432: A PE/VTEP that detects a MAC mobility
624 * event via LOCAL learning starts an M-second timer.
625 *
626 * NOTE: This is the START of the probe with count is
627 * 0 during LOCAL learn event.
628 */
629 reset_params = !nbr->dad_count;
630 }
631
632 if (reset_params) {
633 if (IS_ZEBRA_DEBUG_VXLAN)
634 zlog_debug(
635 "%s: duplicate addr MAC %s IP %s flags 0x%x detection time passed, reset learn count %u",
636 __PRETTY_FUNCTION__,
637 prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
638 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
639 nbr->flags, nbr->dad_count);
640 /* Reset learn count but do not start detection
641 * during REMOTE learn event.
642 */
643 nbr->dad_count = 0;
644 /* Start dup. addr detection (DAD) start time,
645 * ONLY during LOCAL learn.
646 */
647 if (is_local)
648 monotime(&nbr->detect_start_time);
649
650 } else if (!is_local) {
651 /* For REMOTE IP/Neigh, increment detection count
652 * ONLY while in probe window, once window passed,
653 * next local learn event should trigger DAD.
654 */
655 nbr->dad_count++;
656 }
657
658 /* For LOCAL IP/Neigh learn event, once count is reset above via either
659 * initial/start detection time or passed the probe time, the count
660 * needs to be incremented.
661 */
662 if (is_local)
663 nbr->dad_count++;
664
665 if (nbr->dad_count >= zvrf->dad_max_moves) {
666 flog_warn(EC_ZEBRA_DUP_IP_DETECTED,
667 "VNI %u: MAC %s IP %s detected as duplicate during %s VTEP %s",
668 nbr->zvni->vni,
669 prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
670 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
671 is_local ? "local update, last" :
672 "remote update, from",
673 inet_ntoa(vtep_ip));
674
675 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
676
677 /* Capture Duplicate detection time */
678 nbr->dad_dup_detect_time = monotime(NULL);
679
680 /* Start auto recovery timer for this IP */
681 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
682 if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
683 if (IS_ZEBRA_DEBUG_VXLAN)
684 zlog_debug(
685 "%s: duplicate addr MAC %s IP %s flags 0x%x auto recovery time %u start",
686 __PRETTY_FUNCTION__,
687 prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
688 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
689 nbr->flags, zvrf->dad_freeze_time);
690
3801e764 691 thread_add_timer(zrouter.master,
7510e459
CS
692 zebra_vxlan_dad_ip_auto_recovery_exp,
693 nbr, zvrf->dad_freeze_time,
694 &nbr->dad_ip_auto_recovery_timer);
695 }
696 if (zvrf->dad_freeze)
697 *is_dup_detect = true;
698 }
699}
700
cec2e17d 701/*
702 * Helper function to determine maximum width of neighbor IP address for
703 * display - just because we're dealing with IPv6 addresses that can
704 * widely vary.
705 */
e3b78da8 706static void zvni_find_neigh_addr_width(struct hash_bucket *bucket, void *ctxt)
cec2e17d 707{
d62a17ae 708 zebra_neigh_t *n;
709 char buf[INET6_ADDRSTRLEN];
710 struct neigh_walk_ctx *wctx = ctxt;
711 int width;
cec2e17d 712
e3b78da8 713 n = (zebra_neigh_t *)bucket->data;
cec2e17d 714
1445b43c
A
715 ipaddr2str(&n->ip, buf, sizeof(buf));
716 width = strlen(buf);
d62a17ae 717 if (width > wctx->addr_width)
718 wctx->addr_width = width;
68e33151 719
cec2e17d 720}
721
722/*
723 * Print a specific neighbor entry.
724 */
cd233079 725static void zvni_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json)
cec2e17d 726{
d62a17ae 727 struct vty *vty;
728 char buf1[ETHER_ADDR_STRLEN];
729 char buf2[INET6_ADDRSTRLEN];
1a8c5c38 730 const char *type_str;
731 const char *state_str;
732 bool flags_present = false;
979777b2 733 struct zebra_vrf *zvrf = NULL;
87454e6b 734 struct timeval detect_start_time = {0, 0};
c9049b92 735 char timebuf[MONOTIME_STRLEN];
cec2e17d 736
cff8f33b 737 zvrf = zebra_vrf_get_evpn();
979777b2
CS
738 if (!zvrf)
739 return;
740
cd233079
CS
741 ipaddr2str(&n->ip, buf2, sizeof(buf2));
742 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
1a8c5c38 743 type_str = CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) ?
744 "local" : "remote";
745 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
cd233079
CS
746 vty = (struct vty *)ctxt;
747 if (json == NULL) {
748 vty_out(vty, "IP: %s\n",
749 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
1a8c5c38 750 vty_out(vty, " Type: %s\n", type_str);
751 vty_out(vty, " State: %s\n", state_str);
752 vty_out(vty, " MAC: %s\n",
cd233079
CS
753 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
754 } else {
755 json_object_string_add(json, "ip", buf2);
1a8c5c38 756 json_object_string_add(json, "type", type_str);
757 json_object_string_add(json, "state", state_str);
cd233079
CS
758 json_object_string_add(json, "mac", buf1);
759 }
760 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
b6938a74 761 if (json == NULL) {
1a8c5c38 762 vty_out(vty, " Remote VTEP: %s\n",
cd233079 763 inet_ntoa(n->r_vtep_ip));
b6938a74 764 } else
cd233079
CS
765 json_object_string_add(json, "remoteVtep",
766 inet_ntoa(n->r_vtep_ip));
767 }
ead40654 768 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW)) {
1a8c5c38 769 if (!json) {
770 vty_out(vty, " Flags: Default-gateway");
771 flags_present = true;
772 } else
ead40654
MK
773 json_object_boolean_true_add(json, "defaultGateway");
774 }
68e33151 775 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)) {
1a8c5c38 776 if (!json) {
777 vty_out(vty,
778 flags_present ? " ,Router" : " Flags: Router");
779 flags_present = true;
780 }
68e33151 781 }
f07e1c99 782 if (json == NULL) {
1a8c5c38 783 if (flags_present)
784 vty_out(vty, "\n");
785 vty_out(vty, " Local Seq: %u Remote Seq: %u\n",
f07e1c99 786 n->loc_seq, n->rem_seq);
87454e6b
CS
787
788 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE)) {
789 vty_out(vty, " Duplicate, detected at %s",
c9049b92
MS
790 time_to_string(n->dad_dup_detect_time,
791 timebuf));
87454e6b
CS
792 } else if (n->dad_count) {
793 monotime_since(&n->detect_start_time,
794 &detect_start_time);
795 if (detect_start_time.tv_sec <= zvrf->dad_time) {
c9049b92
MS
796 time_to_string(n->detect_start_time.tv_sec,
797 timebuf);
87454e6b
CS
798 vty_out(vty,
799 " Duplicate detection started at %s, detection count %u\n",
c9049b92 800 timebuf, n->dad_count);
87454e6b
CS
801 }
802 }
1a8c5c38 803 } else {
804 json_object_int_add(json, "localSequence", n->loc_seq);
805 json_object_int_add(json, "remoteSequence", n->rem_seq);
87454e6b
CS
806 json_object_int_add(json, "detectionCount",
807 n->dad_count);
808 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
809 json_object_boolean_true_add(json, "isDuplicate");
810 else
811 json_object_boolean_false_add(json, "isDuplicate");
812
813
68e33151 814 }
cec2e17d 815}
816
817/*
818 * Print neighbor hash entry - called for display of all neighbors.
819 */
e3b78da8 820static void zvni_print_neigh_hash(struct hash_bucket *bucket, void *ctxt)
d62a17ae 821{
822 struct vty *vty;
cd233079 823 json_object *json_vni = NULL, *json_row = NULL;
d62a17ae 824 zebra_neigh_t *n;
825 char buf1[ETHER_ADDR_STRLEN];
826 char buf2[INET6_ADDRSTRLEN];
827 struct neigh_walk_ctx *wctx = ctxt;
1a8c5c38 828 const char *state_str;
d62a17ae 829
830 vty = wctx->vty;
cd233079 831 json_vni = wctx->json;
e3b78da8 832 n = (zebra_neigh_t *)bucket->data;
d62a17ae 833
cd233079
CS
834 if (json_vni)
835 json_row = json_object_new_object();
836
d62a17ae 837 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
838 ipaddr2str(&n->ip, buf2, sizeof(buf2));
1a8c5c38 839 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
840 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
841 if (wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
842 return;
843
cd233079 844 if (json_vni == NULL) {
93b35b87 845 vty_out(vty, "%*s %-6s %-8s %-17s %u/%u\n",
1a8c5c38 846 -wctx->addr_width, buf2, "local",
93b35b87 847 state_str, buf1, n->loc_seq, n->rem_seq);
cd233079
CS
848 } else {
849 json_object_string_add(json_row, "type", "local");
1a8c5c38 850 json_object_string_add(json_row, "state", state_str);
cd233079 851 json_object_string_add(json_row, "mac", buf1);
1a8c5c38 852 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
853 json_object_boolean_true_add(
854 json_row, "defaultGateway");
855 json_object_int_add(json_row, "localSequence",
856 n->loc_seq);
857 json_object_int_add(json_row, "remoteSequence",
858 n->rem_seq);
87454e6b
CS
859 json_object_int_add(json_row, "detectionCount",
860 n->dad_count);
861 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
862 json_object_boolean_true_add(json_row,
863 "isDuplicate");
864 else
865 json_object_boolean_false_add(json_row,
866 "isDuplicate");
cd233079 867 }
d62a17ae 868 wctx->count++;
1a8c5c38 869 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
870 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP) &&
871 !IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))
872 return;
873
874 if (json_vni == NULL) {
875 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP) &&
876 (wctx->count == 0))
40e0224a 877 vty_out(vty, "%*s %-6s %-8s %-17s %-21s %s\n",
1a8c5c38 878 -wctx->addr_width, "Neighbor", "Type",
40e0224a
NS
879 "State", "MAC", "Remote VTEP",
880 "Seq #'s");
93b35b87 881 vty_out(vty, "%*s %-6s %-8s %-17s %-21s %u/%u\n",
1a8c5c38 882 -wctx->addr_width, buf2, "remote", state_str,
93b35b87 883 buf1, inet_ntoa(n->r_vtep_ip), n->loc_seq, n->rem_seq);
cd233079 884 } else {
1a8c5c38 885 json_object_string_add(json_row, "type", "remote");
886 json_object_string_add(json_row, "state", state_str);
887 json_object_string_add(json_row, "mac", buf1);
888 json_object_string_add(json_row, "remoteVtep",
889 inet_ntoa(n->r_vtep_ip));
890 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
891 json_object_boolean_true_add(json_row,
892 "defaultGateway");
893 json_object_int_add(json_row, "localSequence",
894 n->loc_seq);
895 json_object_int_add(json_row, "remoteSequence",
896 n->rem_seq);
87454e6b
CS
897 json_object_int_add(json_row, "detectionCount",
898 n->dad_count);
899 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
900 json_object_boolean_true_add(json_row,
901 "isDuplicate");
902 else
903 json_object_boolean_false_add(json_row,
904 "isDuplicate");
d62a17ae 905 }
1a8c5c38 906 wctx->count++;
d62a17ae 907 }
cd233079
CS
908
909 if (json_vni)
910 json_object_object_add(json_vni, buf2, json_row);
cec2e17d 911}
912
e3fac919
NS
913/*
914 * Print neighbor hash entry in detail - called for display of all neighbors.
915 */
e3b78da8 916static void zvni_print_neigh_hash_detail(struct hash_bucket *bucket, void *ctxt)
e3fac919
NS
917{
918 struct vty *vty;
919 json_object *json_vni = NULL, *json_row = NULL;
920 zebra_neigh_t *n;
921 char buf[INET6_ADDRSTRLEN];
922 struct neigh_walk_ctx *wctx = ctxt;
923
924 vty = wctx->vty;
925 json_vni = wctx->json;
e3b78da8 926 n = (zebra_neigh_t *)bucket->data;
e3fac919
NS
927 if (!n)
928 return;
929
930 ipaddr2str(&n->ip, buf, sizeof(buf));
931 if (json_vni)
932 json_row = json_object_new_object();
933
934 zvni_print_neigh(n, vty, json_row);
935
936 if (json_vni)
937 json_object_object_add(json_vni, buf, json_row);
938}
939
cec2e17d 940/*
941 * Print neighbors for all VNI.
942 */
e3b78da8 943static void zvni_print_neigh_hash_all_vni(struct hash_bucket *bucket,
cd233079 944 void **args)
cec2e17d 945{
d62a17ae 946 struct vty *vty;
cd233079 947 json_object *json = NULL, *json_vni = NULL;
d62a17ae 948 zebra_vni_t *zvni;
d7c0a89a 949 uint32_t num_neigh;
d62a17ae 950 struct neigh_walk_ctx wctx;
cd233079 951 char vni_str[VNI_STR_LEN];
1374d4db 952 uint32_t print_dup;
cd233079
CS
953
954 vty = (struct vty *)args[0];
955 json = (json_object *)args[1];
1374d4db 956 print_dup = (uint32_t)(uintptr_t)args[2];
cec2e17d 957
e3b78da8 958 zvni = (zebra_vni_t *)bucket->data;
9ea660be 959
d62a17ae 960 num_neigh = hashcount(zvni->neigh_table);
1374d4db
CS
961
962 if (print_dup)
963 num_neigh = num_dup_detected_neighs(zvni);
964
68e33151 965 if (json == NULL) {
cd233079
CS
966 vty_out(vty,
967 "\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
968 zvni->vni, num_neigh);
68e33151 969 } else {
cd233079
CS
970 json_vni = json_object_new_object();
971 json_object_int_add(json_vni, "numArpNd", num_neigh);
972 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
973 }
1374d4db 974
cd233079
CS
975 if (!num_neigh) {
976 if (json)
977 json_object_object_add(json, vni_str, json_vni);
d62a17ae 978 return;
cd233079 979 }
cec2e17d 980
d62a17ae 981 /* Since we have IPv6 addresses to deal with which can vary widely in
982 * size, we try to be a bit more elegant in display by first computing
983 * the maximum width.
984 */
985 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
986 wctx.zvni = zvni;
987 wctx.vty = vty;
988 wctx.addr_width = 15;
cd233079 989 wctx.json = json_vni;
d62a17ae 990 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
cec2e17d 991
68e33151 992 if (json == NULL) {
93b35b87 993 vty_out(vty, "%*s %-6s %-8s %-17s %-21s %s\n",
1a8c5c38 994 -wctx.addr_width, "IP", "Type",
93b35b87 995 "State", "MAC", "Remote VTEP", "Seq #'s");
68e33151 996 }
1374d4db
CS
997 if (print_dup)
998 hash_iterate(zvni->neigh_table, zvni_print_dad_neigh_hash,
999 &wctx);
1000 else
1001 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
cd233079
CS
1002
1003 if (json)
1004 json_object_object_add(json, vni_str, json_vni);
cec2e17d 1005}
1006
e3b78da8 1007static void zvni_print_dad_neigh_hash(struct hash_bucket *bucket, void *ctxt)
1374d4db
CS
1008{
1009 zebra_neigh_t *nbr;
1010
e3b78da8 1011 nbr = (zebra_neigh_t *)bucket->data;
1374d4db
CS
1012 if (!nbr)
1013 return;
1014
1015 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
e3b78da8 1016 zvni_print_neigh_hash(bucket, ctxt);
1374d4db
CS
1017}
1018
e3b78da8 1019static void zvni_print_dad_neigh_hash_detail(struct hash_bucket *bucket,
e3fac919
NS
1020 void *ctxt)
1021{
1022 zebra_neigh_t *nbr;
1023
e3b78da8 1024 nbr = (zebra_neigh_t *)bucket->data;
e3fac919
NS
1025 if (!nbr)
1026 return;
1027
1028 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
e3b78da8 1029 zvni_print_neigh_hash_detail(bucket, ctxt);
e3fac919
NS
1030}
1031
1032/*
1033 * Print neighbors for all VNIs in detail.
1034 */
e3b78da8 1035static void zvni_print_neigh_hash_all_vni_detail(struct hash_bucket *bucket,
e3fac919
NS
1036 void **args)
1037{
1038 struct vty *vty;
1039 json_object *json = NULL, *json_vni = NULL;
1040 zebra_vni_t *zvni;
1041 uint32_t num_neigh;
1042 struct neigh_walk_ctx wctx;
1043 char vni_str[VNI_STR_LEN];
1044 uint32_t print_dup;
1045
1046 vty = (struct vty *)args[0];
1047 json = (json_object *)args[1];
1048 print_dup = (uint32_t)(uintptr_t)args[2];
1049
e3b78da8 1050 zvni = (zebra_vni_t *)bucket->data;
e3fac919
NS
1051 if (!zvni) {
1052 if (json)
1053 vty_out(vty, "{}\n");
1054 return;
1055 }
1056 num_neigh = hashcount(zvni->neigh_table);
1057
1058 if (print_dup && num_dup_detected_neighs(zvni) == 0)
1059 return;
1060
1061 if (json == NULL) {
1062 vty_out(vty,
1063 "\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
1064 zvni->vni, num_neigh);
1065 } else {
1066 json_vni = json_object_new_object();
1067 json_object_int_add(json_vni, "numArpNd", num_neigh);
1068 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
1069 }
1070 if (!num_neigh) {
1071 if (json)
1072 json_object_object_add(json, vni_str, json_vni);
1073 return;
1074 }
1075
1076 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
1077 wctx.zvni = zvni;
1078 wctx.vty = vty;
1079 wctx.addr_width = 15;
1080 wctx.json = json_vni;
1081
1082 if (print_dup)
1083 hash_iterate(zvni->neigh_table,
1084 zvni_print_dad_neigh_hash_detail, &wctx);
1085 else
1086 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash_detail,
1087 &wctx);
1088
1089 if (json)
1090 json_object_object_add(json, vni_str, json_vni);
1091}
1092
9aa741ea 1093/* print a specific next hop for an l3vni */
996c9314 1094static void zl3vni_print_nh(zebra_neigh_t *n, struct vty *vty,
c0e519d3 1095 json_object *json)
9aa741ea
MK
1096{
1097 char buf1[ETHER_ADDR_STRLEN];
1098 char buf2[INET6_ADDRSTRLEN];
c0e519d3 1099 json_object *json_hosts = NULL;
f2a503f0 1100 struct host_rb_entry *hle;
9aa741ea 1101
c0e519d3
MK
1102 if (!json) {
1103 vty_out(vty, "Ip: %s\n",
1104 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
1105 vty_out(vty, " RMAC: %s\n",
996c9314 1106 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
f2a503f0
DS
1107 vty_out(vty, " Refcount: %d\n",
1108 rb_host_count(&n->host_rb));
4cce389e 1109 vty_out(vty, " Prefixes:\n");
85442b09 1110 RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
c0e519d3 1111 vty_out(vty, " %s\n",
f2a503f0 1112 prefix2str(&hle->p, buf2, sizeof(buf2)));
c0e519d3
MK
1113 } else {
1114 json_hosts = json_object_new_array();
996c9314
LB
1115 json_object_string_add(
1116 json, "ip", ipaddr2str(&(n->ip), buf2, sizeof(buf2)));
1117 json_object_string_add(
1118 json, "routerMac",
1119 prefix_mac2str(&n->emac, buf2, sizeof(buf2)));
f2a503f0
DS
1120 json_object_int_add(json, "refCount",
1121 rb_host_count(&n->host_rb));
85442b09 1122 RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
c0e519d3 1123 json_object_array_add(json_hosts,
996c9314 1124 json_object_new_string(prefix2str(
f2a503f0 1125 &hle->p, buf2, sizeof(buf2))));
4cce389e 1126 json_object_object_add(json, "prefixList", json_hosts);
c0e519d3 1127 }
9aa741ea
MK
1128}
1129
1130/* Print a specific RMAC entry */
996c9314 1131static void zl3vni_print_rmac(zebra_mac_t *zrmac, struct vty *vty,
316f4ca4 1132 json_object *json)
9aa741ea
MK
1133{
1134 char buf1[ETHER_ADDR_STRLEN];
1135 char buf2[PREFIX_STRLEN];
316f4ca4 1136 json_object *json_hosts = NULL;
5e1b0650 1137 struct host_rb_entry *hle;
9aa741ea 1138
316f4ca4
MK
1139 if (!json) {
1140 vty_out(vty, "MAC: %s\n",
1141 prefix_mac2str(&zrmac->macaddr, buf1, sizeof(buf1)));
1142 vty_out(vty, " Remote VTEP: %s\n",
1143 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
41db76c2 1144 vty_out(vty, " Refcount: %d\n", rb_host_count(&zrmac->host_rb));
4cce389e 1145 vty_out(vty, " Prefixes:\n");
85442b09 1146 RB_FOREACH (hle, host_rb_tree_entry, &zrmac->host_rb)
316f4ca4 1147 vty_out(vty, " %s\n",
5e1b0650 1148 prefix2str(&hle->p, buf2, sizeof(buf2)));
316f4ca4
MK
1149 } else {
1150 json_hosts = json_object_new_array();
996c9314
LB
1151 json_object_string_add(
1152 json, "routerMac",
1153 prefix_mac2str(&zrmac->macaddr, buf1, sizeof(buf1)));
4cce389e 1154 json_object_string_add(json, "vtepIp",
316f4ca4 1155 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
41db76c2
DS
1156 json_object_int_add(json, "refCount",
1157 rb_host_count(&zrmac->host_rb));
1a8c5c38 1158 json_object_int_add(json, "localSequence", zrmac->loc_seq);
1159 json_object_int_add(json, "remoteSequence", zrmac->rem_seq);
85442b09 1160 RB_FOREACH (hle, host_rb_tree_entry, &zrmac->host_rb)
5e1b0650
DS
1161 json_object_array_add(
1162 json_hosts,
1163 json_object_new_string(prefix2str(
1164 &hle->p, buf2, sizeof(buf2))));
4cce389e 1165 json_object_object_add(json, "prefixList", json_hosts);
316f4ca4 1166 }
9aa741ea
MK
1167}
1168
cec2e17d 1169/*
1170 * Print a specific MAC entry.
1171 */
24cdbd0d 1172static void zvni_print_mac(zebra_mac_t *mac, void *ctxt, json_object *json)
d62a17ae 1173{
1174 struct vty *vty;
b6938a74
MK
1175 zebra_neigh_t *n = NULL;
1176 struct listnode *node = NULL;
138a4965 1177 char buf1[ETHER_ADDR_STRLEN];
b6938a74 1178 char buf2[INET6_ADDRSTRLEN];
87454e6b
CS
1179 struct zebra_vrf *zvrf;
1180 struct timeval detect_start_time = {0, 0};
c9049b92 1181 char timebuf[MONOTIME_STRLEN];
87454e6b 1182
cff8f33b
T
1183 zvrf = zebra_vrf_get_evpn();
1184 if (!zvrf)
1185 return;
d62a17ae 1186
1187 vty = (struct vty *)ctxt;
24cdbd0d 1188 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
d62a17ae 1189
24cdbd0d
DS
1190 if (json) {
1191 json_object *json_mac = json_object_new_object();
1192
1193 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
1194 struct zebra_ns *zns;
1195 struct interface *ifp;
1196 ifindex_t ifindex;
1197
1198 ifindex = mac->fwd_info.local.ifindex;
1199 zns = zebra_ns_lookup(NS_DEFAULT);
1200 ifp = if_lookup_by_index_per_ns(zns, ifindex);
1201 if (!ifp)
1202 return;
1203 json_object_string_add(json_mac, "type", "local");
1204 json_object_string_add(json_mac, "intf", ifp->name);
1205 json_object_int_add(json_mac, "ifindex", ifindex);
1206 if (mac->fwd_info.local.vid)
1207 json_object_int_add(json_mac, "vlan",
1208 mac->fwd_info.local.vid);
1209 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1210 json_object_string_add(json_mac, "type", "remote");
1211 json_object_string_add(
1212 json_mac, "remoteVtep",
1213 inet_ntoa(mac->fwd_info.r_vtep_ip));
1214 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
29611019 1215 json_object_string_add(json_mac, "type", "auto");
b6938a74 1216
24cdbd0d
DS
1217 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
1218 json_object_boolean_true_add(json_mac, "stickyMac");
ead40654 1219
24cdbd0d
DS
1220 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
1221 json_object_boolean_true_add(json_mac,
1222 "defaultGateway");
ead40654 1223
24cdbd0d
DS
1224 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
1225 json_object_boolean_true_add(json_mac,
1226 "remoteGatewayMac");
51f4dab4 1227
24cdbd0d
DS
1228 json_object_int_add(json_mac, "localSequence", mac->loc_seq);
1229 json_object_int_add(json_mac, "remoteSequence", mac->rem_seq);
f07e1c99 1230
87454e6b
CS
1231 json_object_int_add(json_mac, "detectionCount", mac->dad_count);
1232 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1233 json_object_boolean_true_add(json_mac, "isDuplicate");
1234 else
1235 json_object_boolean_false_add(json_mac, "isDuplicate");
1236
24cdbd0d
DS
1237 /* print all the associated neigh */
1238 if (!listcount(mac->neigh_list))
1239 json_object_string_add(json_mac, "neighbors", "none");
1240 else {
1241 json_object *json_active_nbrs = json_object_new_array();
1242 json_object *json_inactive_nbrs =
1243 json_object_new_array();
1244 json_object *json_nbrs = json_object_new_object();
1245
1246 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
1247 if (IS_ZEBRA_NEIGH_ACTIVE(n))
1248 json_object_array_add(
1249 json_active_nbrs,
1250 json_object_new_string(
1251 ipaddr2str(
1252 &n->ip, buf2,
1253 sizeof(buf2))));
1254 else
1255 json_object_array_add(
1256 json_inactive_nbrs,
1257 json_object_new_string(
1258 ipaddr2str(
1259 &n->ip, buf2,
1260 sizeof(buf2))));
1261 }
1262
1263 json_object_object_add(json_nbrs, "active",
1264 json_active_nbrs);
1265 json_object_object_add(json_nbrs, "inactive",
1266 json_inactive_nbrs);
1267 json_object_object_add(json_mac, "neighbors",
1268 json_nbrs);
b6938a74 1269 }
b6938a74 1270
24cdbd0d 1271 json_object_object_add(json, buf1, json_mac);
24cdbd0d
DS
1272 } else {
1273 vty_out(vty, "MAC: %s\n", buf1);
1274
1275 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
1276 struct zebra_ns *zns;
1277 struct interface *ifp;
1278 ifindex_t ifindex;
1279
1280 ifindex = mac->fwd_info.local.ifindex;
1281 zns = zebra_ns_lookup(NS_DEFAULT);
1282 ifp = if_lookup_by_index_per_ns(zns, ifindex);
1283 if (!ifp)
1284 return;
1285 vty_out(vty, " Intf: %s(%u)", ifp->name, ifindex);
1286 if (mac->fwd_info.local.vid)
1287 vty_out(vty, " VLAN: %u",
1288 mac->fwd_info.local.vid);
1289 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1290 vty_out(vty, " Remote VTEP: %s",
1291 inet_ntoa(mac->fwd_info.r_vtep_ip));
1292 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
1293 vty_out(vty, " Auto Mac ");
1294 }
1295
1296 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
1297 vty_out(vty, " Sticky Mac ");
1298
1299 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
1300 vty_out(vty, " Default-gateway Mac ");
1301
1302 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
1303 vty_out(vty, " Remote-gateway Mac ");
1304
1305 vty_out(vty, "\n");
1306 vty_out(vty, " Local Seq: %u Remote Seq: %u", mac->loc_seq,
1307 mac->rem_seq);
1308 vty_out(vty, "\n");
1309
87454e6b
CS
1310 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
1311 vty_out(vty, " Duplicate, detected at %s",
c9049b92
MS
1312 time_to_string(mac->dad_dup_detect_time,
1313 timebuf));
87454e6b
CS
1314 } else if (mac->dad_count) {
1315 monotime_since(&mac->detect_start_time,
1316 &detect_start_time);
1317 if (detect_start_time.tv_sec <= zvrf->dad_time) {
c9049b92
MS
1318 time_to_string(mac->detect_start_time.tv_sec,
1319 timebuf);
87454e6b
CS
1320 vty_out(vty,
1321 " Duplicate detection started at %s, detection count %u\n",
c9049b92 1322 timebuf, mac->dad_count);
87454e6b
CS
1323 }
1324 }
1325
24cdbd0d
DS
1326 /* print all the associated neigh */
1327 vty_out(vty, " Neighbors:\n");
1328 if (!listcount(mac->neigh_list))
1329 vty_out(vty, " No Neighbors\n");
1330 else {
1331 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
1332 vty_out(vty, " %s %s\n",
1333 ipaddr2str(&n->ip, buf2, sizeof(buf2)),
1334 (IS_ZEBRA_NEIGH_ACTIVE(n)
1335 ? "Active"
1336 : "Inactive"));
1337 }
1338 }
1339
1340 vty_out(vty, "\n");
1341 }
cec2e17d 1342}
1343
1344/*
1345 * Print MAC hash entry - called for display of all MACs.
1346 */
e3b78da8 1347static void zvni_print_mac_hash(struct hash_bucket *bucket, void *ctxt)
d62a17ae 1348{
1349 struct vty *vty;
cd233079 1350 json_object *json_mac_hdr = NULL, *json_mac = NULL;
d62a17ae 1351 zebra_mac_t *mac;
138a4965 1352 char buf1[ETHER_ADDR_STRLEN];
d62a17ae 1353 struct mac_walk_ctx *wctx = ctxt;
1354
1355 vty = wctx->vty;
cd233079 1356 json_mac_hdr = wctx->json;
e3b78da8 1357 mac = (zebra_mac_t *)bucket->data;
d62a17ae 1358
1359 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
cd233079
CS
1360
1361 if (json_mac_hdr)
1362 json_mac = json_object_new_object();
1363
1a8c5c38 1364 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
d62a17ae 1365 struct zebra_ns *zns;
1366 ifindex_t ifindex;
1367 struct interface *ifp;
1368 vlanid_t vid;
1369
1a8c5c38 1370 if (wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP)
1371 return;
1372
d62a17ae 1373 zns = zebra_ns_lookup(NS_DEFAULT);
1374 ifindex = mac->fwd_info.local.ifindex;
1375 ifp = if_lookup_by_index_per_ns(zns, ifindex);
1376 if (!ifp) // unexpected
1377 return;
1378 vid = mac->fwd_info.local.vid;
cd233079
CS
1379 if (json_mac_hdr == NULL)
1380 vty_out(vty, "%-17s %-6s %-21s", buf1, "local",
1381 ifp->name);
1382 else {
1383 json_object_string_add(json_mac, "type", "local");
1384 json_object_string_add(json_mac, "intf", ifp->name);
1385 }
1386 if (vid) {
1387 if (json_mac_hdr == NULL)
1388 vty_out(vty, " %-5u", vid);
1389 else
1390 json_object_int_add(json_mac, "vlan", vid);
503cf3fe 1391 } else /* No vid? fill out the space */
40e0224a
NS
1392 if (json_mac_hdr == NULL)
1393 vty_out(vty, " %-5s", "");
1a8c5c38 1394 if (json_mac_hdr == NULL) {
40e0224a 1395 vty_out(vty, " %u/%u", mac->loc_seq, mac->rem_seq);
cd233079 1396 vty_out(vty, "\n");
1a8c5c38 1397 } else {
1398 json_object_int_add(json_mac, "localSequence",
1399 mac->loc_seq);
1400 json_object_int_add(json_mac, "remoteSequence",
1401 mac->rem_seq);
87454e6b
CS
1402 json_object_int_add(json_mac, "detectionCount",
1403 mac->dad_count);
1404 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1405 json_object_boolean_true_add(json_mac,
1406 "isDuplicate");
1407 else
1408 json_object_boolean_false_add(json_mac,
1409 "isDuplicate");
cd233079 1410 json_object_object_add(json_mac_hdr, buf1, json_mac);
1a8c5c38 1411 }
1412
d62a17ae 1413 wctx->count++;
1a8c5c38 1414
b6938a74 1415 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1a8c5c38 1416
1417 if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) &&
1418 !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip,
1419 &wctx->r_vtep_ip))
1420 return;
1421
1422 if (json_mac_hdr == NULL) {
1423 if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) &&
1424 (wctx->count == 0)) {
1425 vty_out(vty, "\nVNI %u\n\n", wctx->zvni->vni);
503cf3fe
NS
1426 vty_out(vty, "%-17s %-6s %-21s %-5s %s\n",
1427 "MAC", "Type", "Intf/Remote VTEP",
1428 "VLAN", "Seq #'s");
d62a17ae 1429 }
503cf3fe
NS
1430 vty_out(vty, "%-17s %-6s %-21s %-5s %u/%u\n", buf1,
1431 "remote", inet_ntoa(mac->fwd_info.r_vtep_ip),
1432 "", mac->loc_seq, mac->rem_seq);
d62a17ae 1433 } else {
1a8c5c38 1434 json_object_string_add(json_mac, "type", "remote");
1435 json_object_string_add(json_mac, "remoteVtep",
cd233079 1436 inet_ntoa(mac->fwd_info.r_vtep_ip));
1a8c5c38 1437 json_object_object_add(json_mac_hdr, buf1, json_mac);
1438 json_object_int_add(json_mac, "localSequence",
1439 mac->loc_seq);
1440 json_object_int_add(json_mac, "remoteSequence",
1441 mac->rem_seq);
87454e6b
CS
1442 json_object_int_add(json_mac, "detectionCount",
1443 mac->dad_count);
1444 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1445 json_object_boolean_true_add(json_mac,
1446 "isDuplicate");
1447 else
1448 json_object_boolean_false_add(json_mac,
1449 "isDuplicate");
1450
d62a17ae 1451 }
1a8c5c38 1452
1453 wctx->count++;
d62a17ae 1454 }
cec2e17d 1455}
1456
1374d4db 1457/* Print Duplicate MAC */
e3b78da8 1458static void zvni_print_dad_mac_hash(struct hash_bucket *bucket, void *ctxt)
1374d4db
CS
1459{
1460 zebra_mac_t *mac;
1461
e3b78da8 1462 mac = (zebra_mac_t *)bucket->data;
1374d4db
CS
1463 if (!mac)
1464 return;
1465
1466 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
e3b78da8 1467 zvni_print_mac_hash(bucket, ctxt);
1374d4db
CS
1468}
1469
cffe7580
NS
1470/*
1471 * Print MAC hash entry in detail - called for display of all MACs.
1472 */
e3b78da8 1473static void zvni_print_mac_hash_detail(struct hash_bucket *bucket, void *ctxt)
cffe7580
NS
1474{
1475 struct vty *vty;
1476 json_object *json_mac_hdr = NULL;
1477 zebra_mac_t *mac;
1478 struct mac_walk_ctx *wctx = ctxt;
138a4965 1479 char buf1[ETHER_ADDR_STRLEN];
cffe7580
NS
1480
1481 vty = wctx->vty;
1482 json_mac_hdr = wctx->json;
e3b78da8 1483 mac = (zebra_mac_t *)bucket->data;
cffe7580
NS
1484 if (!mac)
1485 return;
1486
1487 wctx->count++;
1488 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
1489
1490 zvni_print_mac(mac, vty, json_mac_hdr);
1491}
1492
1493/* Print Duplicate MAC in detail */
e3b78da8 1494static void zvni_print_dad_mac_hash_detail(struct hash_bucket *bucket,
cffe7580
NS
1495 void *ctxt)
1496{
1497 zebra_mac_t *mac;
1498
e3b78da8 1499 mac = (zebra_mac_t *)bucket->data;
cffe7580
NS
1500 if (!mac)
1501 return;
1502
1503 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
e3b78da8 1504 zvni_print_mac_hash_detail(bucket, ctxt);
cffe7580
NS
1505}
1506
cec2e17d 1507/*
1508 * Print MACs for all VNI.
1509 */
e3b78da8 1510static void zvni_print_mac_hash_all_vni(struct hash_bucket *bucket, void *ctxt)
cec2e17d 1511{
d62a17ae 1512 struct vty *vty;
cd233079
CS
1513 json_object *json = NULL, *json_vni = NULL;
1514 json_object *json_mac = NULL;
d62a17ae 1515 zebra_vni_t *zvni;
d7c0a89a 1516 uint32_t num_macs;
d62a17ae 1517 struct mac_walk_ctx *wctx = ctxt;
cd233079 1518 char vni_str[VNI_STR_LEN];
cec2e17d 1519
d62a17ae 1520 vty = (struct vty *)wctx->vty;
cd233079 1521 json = (struct json_object *)wctx->json;
cec2e17d 1522
e3b78da8 1523 zvni = (zebra_vni_t *)bucket->data;
d62a17ae 1524 wctx->zvni = zvni;
cec2e17d 1525
d62a17ae 1526 /*We are iterating over a new VNI, set the count to 0*/
1527 wctx->count = 0;
cec2e17d 1528
790f8dc5 1529 num_macs = num_valid_macs(zvni);
d62a17ae 1530 if (!num_macs)
1531 return;
cd233079 1532
1374d4db
CS
1533 if (wctx->print_dup)
1534 num_macs = num_dup_detected_macs(zvni);
1535
cd233079
CS
1536 if (json) {
1537 json_vni = json_object_new_object();
1538 json_mac = json_object_new_object();
1539 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
d62a17ae 1540 }
cec2e17d 1541
cd233079
CS
1542 if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
1543 if (json == NULL) {
1544 vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
1545 zvni->vni, num_macs);
503cf3fe
NS
1546 vty_out(vty, "%-17s %-6s %-21s %-5s %s\n", "MAC",
1547 "Type", "Intf/Remote VTEP", "VLAN", "Seq #'s");
cd233079
CS
1548 } else
1549 json_object_int_add(json_vni, "numMacs", num_macs);
1550 }
1374d4db
CS
1551
1552 if (!num_macs) {
1553 if (json) {
1554 json_object_int_add(json_vni, "numMacs", num_macs);
1555 json_object_object_add(json, vni_str, json_vni);
1556 }
1557 return;
1558 }
1559
cd233079
CS
1560 /* assign per-vni to wctx->json object to fill macs
1561 * under the vni. Re-assign primary json object to fill
1562 * next vni information.
1563 */
1564 wctx->json = json_mac;
1374d4db
CS
1565 if (wctx->print_dup)
1566 hash_iterate(zvni->mac_table, zvni_print_dad_mac_hash, wctx);
1567 else
1568 hash_iterate(zvni->mac_table, zvni_print_mac_hash, wctx);
cd233079
CS
1569 wctx->json = json;
1570 if (json) {
1571 if (wctx->count)
1572 json_object_object_add(json_vni, "macs", json_mac);
1573 json_object_object_add(json, vni_str, json_vni);
1574 }
cec2e17d 1575}
1576
cffe7580
NS
1577/*
1578 * Print MACs in detail for all VNI.
1579 */
e3b78da8 1580static void zvni_print_mac_hash_all_vni_detail(struct hash_bucket *bucket,
cffe7580
NS
1581 void *ctxt)
1582{
1583 struct vty *vty;
1584 json_object *json = NULL, *json_vni = NULL;
1585 json_object *json_mac = NULL;
1586 zebra_vni_t *zvni;
1587 uint32_t num_macs;
1588 struct mac_walk_ctx *wctx = ctxt;
1589 char vni_str[VNI_STR_LEN];
1590
1591 vty = (struct vty *)wctx->vty;
1592 json = (struct json_object *)wctx->json;
1593
e3b78da8 1594 zvni = (zebra_vni_t *)bucket->data;
cffe7580
NS
1595 if (!zvni) {
1596 if (json)
1597 vty_out(vty, "{}\n");
1598 return;
1599 }
1600 wctx->zvni = zvni;
1601
1602 /*We are iterating over a new VNI, set the count to 0*/
1603 wctx->count = 0;
1604
1605 num_macs = num_valid_macs(zvni);
1606 if (!num_macs)
1607 return;
1608
1609 if (wctx->print_dup && (num_dup_detected_macs(zvni) == 0))
1610 return;
1611
1612 if (json) {
1613 json_vni = json_object_new_object();
1614 json_mac = json_object_new_object();
1615 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
1616 }
1617
1618 if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
1619 if (json == NULL) {
1620 vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
1621 zvni->vni, num_macs);
1622 } else
1623 json_object_int_add(json_vni, "numMacs", num_macs);
1624 }
1625 /* assign per-vni to wctx->json object to fill macs
1626 * under the vni. Re-assign primary json object to fill
1627 * next vni information.
1628 */
1629 wctx->json = json_mac;
1630 if (wctx->print_dup)
1631 hash_iterate(zvni->mac_table, zvni_print_dad_mac_hash_detail,
1632 wctx);
1633 else
1634 hash_iterate(zvni->mac_table, zvni_print_mac_hash_detail, wctx);
1635 wctx->json = json;
1636 if (json) {
1637 if (wctx->count)
1638 json_object_object_add(json_vni, "macs", json_mac);
1639 json_object_object_add(json, vni_str, json_vni);
1640 }
1641}
1642
e3b78da8 1643static void zl3vni_print_nh_hash(struct hash_bucket *bucket, void *ctx)
b7cfce93
MK
1644{
1645 struct nh_walk_ctx *wctx = NULL;
1646 struct vty *vty = NULL;
32798965 1647 struct json_object *json_vni = NULL;
b7cfce93
MK
1648 struct json_object *json_nh = NULL;
1649 zebra_neigh_t *n = NULL;
1650 char buf1[ETHER_ADDR_STRLEN];
2dbad57f 1651 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
1652
1653 wctx = (struct nh_walk_ctx *)ctx;
1654 vty = wctx->vty;
32798965
MK
1655 json_vni = wctx->json;
1656 if (json_vni)
b7cfce93 1657 json_nh = json_object_new_object();
e3b78da8 1658 n = (zebra_neigh_t *)bucket->data;
b7cfce93 1659
32798965 1660 if (!json_vni) {
4cce389e 1661 vty_out(vty, "%-15s %-17s\n",
2dbad57f 1662 ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
4cce389e 1663 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
b7cfce93 1664 } else {
4cce389e 1665 json_object_string_add(json_nh, "nexthopIp",
32798965 1666 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
996c9314
LB
1667 json_object_string_add(
1668 json_nh, "routerMac",
1669 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
32798965
MK
1670 json_object_object_add(json_vni,
1671 ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
1672 json_nh);
b7cfce93
MK
1673 }
1674}
1675
e3b78da8 1676static void zl3vni_print_nh_hash_all_vni(struct hash_bucket *bucket,
32798965 1677 void **args)
2dbad57f 1678{
1679 struct vty *vty = NULL;
1680 json_object *json = NULL;
1681 json_object *json_vni = NULL;
2dbad57f 1682 zebra_l3vni_t *zl3vni = NULL;
1683 uint32_t num_nh = 0;
32798965 1684 struct nh_walk_ctx wctx;
2dbad57f 1685 char vni_str[VNI_STR_LEN];
1686
32798965
MK
1687 vty = (struct vty *)args[0];
1688 json = (struct json_object *)args[1];
2dbad57f 1689
e3b78da8 1690 zl3vni = (zebra_l3vni_t *)bucket->data;
2dbad57f 1691
1692 num_nh = hashcount(zl3vni->nh_table);
1693 if (!num_nh)
1694 return;
1695
1696 if (json) {
1697 json_vni = json_object_new_object();
2dbad57f 1698 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
1699 }
1700
1701 if (json == NULL) {
996c9314 1702 vty_out(vty, "\nVNI %u #Next-Hops %u\n\n", zl3vni->vni, num_nh);
4cce389e 1703 vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
2dbad57f 1704 } else
4cce389e 1705 json_object_int_add(json_vni, "numNextHops", num_nh);
2dbad57f 1706
32798965
MK
1707 memset(&wctx, 0, sizeof(struct nh_walk_ctx));
1708 wctx.vty = vty;
1709 wctx.json = json_vni;
1710 hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
1711 if (json)
2dbad57f 1712 json_object_object_add(json, vni_str, json_vni);
2dbad57f 1713}
1714
e3b78da8 1715static void zl3vni_print_rmac_hash_all_vni(struct hash_bucket *bucket,
c0b4eaa4 1716 void **args)
b7cfce93
MK
1717{
1718 struct vty *vty = NULL;
1719 json_object *json = NULL;
1720 json_object *json_vni = NULL;
b7cfce93 1721 zebra_l3vni_t *zl3vni = NULL;
d7c0a89a 1722 uint32_t num_rmacs;
c0b4eaa4 1723 struct rmac_walk_ctx wctx;
b7cfce93
MK
1724 char vni_str[VNI_STR_LEN];
1725
c0b4eaa4
MK
1726 vty = (struct vty *)args[0];
1727 json = (struct json_object *)args[1];
b7cfce93 1728
e3b78da8 1729 zl3vni = (zebra_l3vni_t *)bucket->data;
b7cfce93
MK
1730
1731 num_rmacs = hashcount(zl3vni->rmac_table);
1732 if (!num_rmacs)
1733 return;
1734
1735 if (json) {
1736 json_vni = json_object_new_object();
b7cfce93
MK
1737 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
1738 }
1739
1740 if (json == NULL) {
996c9314 1741 vty_out(vty, "\nVNI %u #RMACs %u\n\n", zl3vni->vni, num_rmacs);
4cce389e 1742 vty_out(vty, "%-17s %-21s\n", "RMAC", "Remote VTEP");
b7cfce93
MK
1743 } else
1744 json_object_int_add(json_vni, "numRmacs", num_rmacs);
1745
1746 /* assign per-vni to wctx->json object to fill macs
1747 * under the vni. Re-assign primary json object to fill
1748 * next vni information.
1749 */
c0b4eaa4
MK
1750 memset(&wctx, 0, sizeof(struct rmac_walk_ctx));
1751 wctx.vty = vty;
1752 wctx.json = json_vni;
1753 hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
1754 if (json)
b7cfce93 1755 json_object_object_add(json, vni_str, json_vni);
b7cfce93
MK
1756}
1757
e3b78da8 1758static void zl3vni_print_rmac_hash(struct hash_bucket *bucket, void *ctx)
b7cfce93
MK
1759{
1760 zebra_mac_t *zrmac = NULL;
1761 struct rmac_walk_ctx *wctx = NULL;
1762 struct vty *vty = NULL;
1763 struct json_object *json = NULL;
1764 struct json_object *json_rmac = NULL;
1765 char buf[ETHER_ADDR_STRLEN];
1766
1767 wctx = (struct rmac_walk_ctx *)ctx;
1768 vty = wctx->vty;
1769 json = wctx->json;
1770 if (json)
1771 json_rmac = json_object_new_object();
e3b78da8 1772 zrmac = (zebra_mac_t *)bucket->data;
b7cfce93
MK
1773
1774 if (!json) {
4cce389e 1775 vty_out(vty, "%-17s %-21s\n",
b7cfce93 1776 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
996c9314 1777 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
b7cfce93 1778 } else {
996c9314
LB
1779 json_object_string_add(
1780 json_rmac, "routerMac",
1781 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)));
4cce389e 1782 json_object_string_add(json_rmac, "vtepIp",
b7cfce93 1783 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
996c9314
LB
1784 json_object_object_add(
1785 json, prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
1786 json_rmac);
b7cfce93
MK
1787 }
1788}
1789
1790/* print a specific L3 VNI entry */
1791static void zl3vni_print(zebra_l3vni_t *zl3vni, void **ctx)
1792{
1793 char buf[ETHER_ADDR_STRLEN];
1794 struct vty *vty = NULL;
1795 json_object *json = NULL;
1796 zebra_vni_t *zvni = NULL;
1797 json_object *json_vni_list = NULL;
1798 struct listnode *node = NULL, *nnode = NULL;
1799
1800 vty = ctx[0];
1801 json = ctx[1];
1802
1803 if (!json) {
1804 vty_out(vty, "VNI: %u\n", zl3vni->vni);
4cce389e 1805 vty_out(vty, " Type: %s\n", "L3");
996c9314 1806 vty_out(vty, " Tenant VRF: %s\n", zl3vni_vrf_name(zl3vni));
4cce389e 1807 vty_out(vty, " Local Vtep Ip: %s\n",
b67a60d2 1808 inet_ntoa(zl3vni->local_vtep_ip));
b7cfce93
MK
1809 vty_out(vty, " Vxlan-Intf: %s\n",
1810 zl3vni_vxlan_if_name(zl3vni));
996c9314
LB
1811 vty_out(vty, " SVI-If: %s\n", zl3vni_svi_if_name(zl3vni));
1812 vty_out(vty, " State: %s\n", zl3vni_state2str(zl3vni));
c48d9f5f 1813 vty_out(vty, " VNI Filter: %s\n",
996c9314
LB
1814 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
1815 ? "prefix-routes-only"
1816 : "none");
4cce389e 1817 vty_out(vty, " Router MAC: %s\n",
b7cfce93 1818 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
4cce389e 1819 vty_out(vty, " L2 VNIs: ");
b7cfce93
MK
1820 for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zvni))
1821 vty_out(vty, "%u ", zvni->vni);
1822 vty_out(vty, "\n");
1823 } else {
1824 json_vni_list = json_object_new_array();
1825 json_object_int_add(json, "vni", zl3vni->vni);
4cce389e
MK
1826 json_object_string_add(json, "type", "L3");
1827 json_object_string_add(json, "localVtepIp",
b67a60d2 1828 inet_ntoa(zl3vni->local_vtep_ip));
4cce389e 1829 json_object_string_add(json, "vxlanIntf",
b7cfce93 1830 zl3vni_vxlan_if_name(zl3vni));
4cce389e 1831 json_object_string_add(json, "sviIntf",
b7cfce93 1832 zl3vni_svi_if_name(zl3vni));
996c9314
LB
1833 json_object_string_add(json, "state", zl3vni_state2str(zl3vni));
1834 json_object_string_add(json, "vrf", zl3vni_vrf_name(zl3vni));
1835 json_object_string_add(
1836 json, "routerMac",
1837 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
1838 json_object_string_add(
1839 json, "vniFilter",
1840 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
1841 ? "prefix-routes-only"
1842 : "none");
b7cfce93
MK
1843 for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zvni)) {
1844 json_object_array_add(json_vni_list,
1845 json_object_new_int(zvni->vni));
1846 }
4cce389e 1847 json_object_object_add(json, "l2Vnis", json_vni_list);
b7cfce93
MK
1848 }
1849}
1850
cec2e17d 1851/*
1852 * Print a specific VNI entry.
1853 */
cd233079 1854static void zvni_print(zebra_vni_t *zvni, void **ctxt)
d62a17ae 1855{
1856 struct vty *vty;
1857 zebra_vtep_t *zvtep;
d7c0a89a
QY
1858 uint32_t num_macs;
1859 uint32_t num_neigh;
cd233079
CS
1860 json_object *json = NULL;
1861 json_object *json_vtep_list = NULL;
1862 json_object *json_ip_str = NULL;
d62a17ae 1863
cd233079
CS
1864 vty = ctxt[0];
1865 json = ctxt[1];
1866
b7cfce93 1867 if (json == NULL) {
cd233079 1868 vty_out(vty, "VNI: %u\n", zvni->vni);
4cce389e
MK
1869 vty_out(vty, " Type: %s\n", "L2");
1870 vty_out(vty, " Tenant VRF: %s\n", vrf_id_to_name(zvni->vrf_id));
b7cfce93 1871 } else {
cd233079 1872 json_object_int_add(json, "vni", zvni->vni);
4cce389e 1873 json_object_string_add(json, "type", "L2");
b7cfce93
MK
1874 json_object_string_add(json, "vrf",
1875 vrf_id_to_name(zvni->vrf_id));
1876 }
d62a17ae 1877
d62a17ae 1878 if (!zvni->vxlan_if) { // unexpected
cd233079
CS
1879 if (json == NULL)
1880 vty_out(vty, " VxLAN interface: unknown\n");
d62a17ae 1881 return;
1882 }
790f8dc5 1883 num_macs = num_valid_macs(zvni);
cd233079 1884 num_neigh = hashcount(zvni->neigh_table);
4cce389e 1885 if (json == NULL) {
996c9314 1886 vty_out(vty, " VxLAN interface: %s\n", zvni->vxlan_if->name);
4cce389e 1887 vty_out(vty, " VxLAN ifIndex: %u\n", zvni->vxlan_if->ifindex);
996c9314 1888 vty_out(vty, " Local VTEP IP: %s\n",
cd233079 1889 inet_ntoa(zvni->local_vtep_ip));
39c46ff1
AK
1890 vty_out(vty, " Mcast group: %s\n",
1891 inet_ntoa(zvni->mcast_grp));
4cce389e 1892 } else {
cd233079
CS
1893 json_object_string_add(json, "vxlanInterface",
1894 zvni->vxlan_if->name);
1895 json_object_int_add(json, "ifindex", zvni->vxlan_if->ifindex);
1896 json_object_string_add(json, "vtepIp",
1897 inet_ntoa(zvni->local_vtep_ip));
39c46ff1
AK
1898 json_object_string_add(json, "mcastGroup",
1899 inet_ntoa(zvni->mcast_grp));
ddd16ed5
MK
1900 json_object_string_add(json, "advertiseGatewayMacip",
1901 zvni->advertise_gw_macip ? "Yes" : "No");
cd233079
CS
1902 json_object_int_add(json, "numMacs", num_macs);
1903 json_object_int_add(json, "numArpNd", num_neigh);
1904 }
d62a17ae 1905 if (!zvni->vteps) {
cd233079
CS
1906 if (json == NULL)
1907 vty_out(vty, " No remote VTEPs known for this VNI\n");
d62a17ae 1908 } else {
cd233079
CS
1909 if (json == NULL)
1910 vty_out(vty, " Remote VTEPs for this VNI:\n");
1911 else
1912 json_vtep_list = json_object_new_array();
1913 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
9718c54e
AK
1914 const char *flood_str = lookup_msg(zvtep_flood_str,
1915 zvtep->flood_control,
1916 VXLAN_FLOOD_STR_DEFAULT);
1917
1918 if (json == NULL) {
1919 vty_out(vty, " %s flood: %s\n",
1920 inet_ntoa(zvtep->vtep_ip),
1921 flood_str);
1922 } else {
cd233079 1923 json_ip_str = json_object_new_string(
9718c54e 1924 inet_ntoa(zvtep->vtep_ip));
cd233079 1925 json_object_array_add(json_vtep_list,
9718c54e 1926 json_ip_str);
cd233079
CS
1927 }
1928 }
1929 if (json)
1930 json_object_object_add(json, "numRemoteVteps",
1931 json_vtep_list);
1932 }
1933 if (json == NULL) {
1934 vty_out(vty,
1935 " Number of MACs (local and remote) known for this VNI: %u\n",
1936 num_macs);
1937 vty_out(vty,
1938 " Number of ARPs (IPv4 and IPv6, local and remote) "
1939 "known for this VNI: %u\n",
1940 num_neigh);
ddd16ed5
MK
1941 vty_out(vty, " Advertise-gw-macip: %s\n",
1942 zvni->advertise_gw_macip ? "Yes" : "No");
d62a17ae 1943 }
cec2e17d 1944}
1945
b7cfce93 1946/* print a L3 VNI hash entry */
e3b78da8 1947static void zl3vni_print_hash(struct hash_bucket *bucket, void *ctx[])
b7cfce93 1948{
b7cfce93
MK
1949 struct vty *vty = NULL;
1950 json_object *json = NULL;
51d8de8f 1951 json_object *json_vni = NULL;
b7cfce93
MK
1952 zebra_l3vni_t *zl3vni = NULL;
1953
51d8de8f
MK
1954 vty = (struct vty *)ctx[0];
1955 json = (json_object *)ctx[1];
b7cfce93 1956
e3b78da8 1957 zl3vni = (zebra_l3vni_t *)bucket->data;
b7cfce93
MK
1958
1959 if (!json) {
996c9314
LB
1960 vty_out(vty, "%-10u %-4s %-21s %-8lu %-8lu %-15s %-37s\n",
1961 zl3vni->vni, "L3", zl3vni_vxlan_if_name(zl3vni),
4cce389e 1962 hashcount(zl3vni->rmac_table),
996c9314 1963 hashcount(zl3vni->nh_table), "n/a",
4cce389e 1964 zl3vni_vrf_name(zl3vni));
b7cfce93 1965 } else {
51d8de8f
MK
1966 char vni_str[VNI_STR_LEN];
1967
1968 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
1969 json_vni = json_object_new_object();
1970 json_object_int_add(json_vni, "vni", zl3vni->vni);
4cce389e 1971 json_object_string_add(json_vni, "vxlanIf",
b7cfce93 1972 zl3vni_vxlan_if_name(zl3vni));
4cce389e
MK
1973 json_object_int_add(json_vni, "numMacs",
1974 hashcount(zl3vni->rmac_table));
1975 json_object_int_add(json_vni, "numArpNd",
1976 hashcount(zl3vni->nh_table));
1977 json_object_string_add(json_vni, "numRemoteVteps", "n/a");
1978 json_object_string_add(json_vni, "type", "L3");
1979 json_object_string_add(json_vni, "tenantVrf",
b7cfce93 1980 zl3vni_vrf_name(zl3vni));
51d8de8f 1981 json_object_object_add(json, vni_str, json_vni);
b7cfce93 1982 }
b7cfce93
MK
1983}
1984
09af6961
NS
1985/* Private Structure to pass callback data for hash iterator */
1986struct zvni_evpn_show {
1987 struct vty *vty;
1988 json_object *json;
1989 struct zebra_vrf *zvrf;
1990};
1991
1992/* print a L3 VNI hash entry in detail*/
e3b78da8 1993static void zl3vni_print_hash_detail(struct hash_bucket *bucket, void *data)
09af6961
NS
1994{
1995 struct vty *vty = NULL;
1996 zebra_l3vni_t *zl3vni = NULL;
1997 json_object *json = NULL;
1998 bool use_json = false;
1999 struct zvni_evpn_show *zes = data;
2000
2001 vty = zes->vty;
2002 json = zes->json;
2003
2004 if (json)
2005 use_json = true;
2006
e3b78da8 2007 zl3vni = (zebra_l3vni_t *)bucket->data;
09af6961
NS
2008
2009 zebra_vxlan_print_vni(vty, zes->zvrf, zl3vni->vni, use_json);
2010 vty_out(vty, "\n");
2011}
2012
2013
cec2e17d 2014/*
2015 * Print a VNI hash entry - called for display of all VNIs.
2016 */
e3b78da8 2017static void zvni_print_hash(struct hash_bucket *bucket, void *ctxt[])
cec2e17d 2018{
d62a17ae 2019 struct vty *vty;
2020 zebra_vni_t *zvni;
2021 zebra_vtep_t *zvtep;
d7c0a89a
QY
2022 uint32_t num_vteps = 0;
2023 uint32_t num_macs = 0;
2024 uint32_t num_neigh = 0;
cd233079
CS
2025 json_object *json = NULL;
2026 json_object *json_vni = NULL;
2027 json_object *json_ip_str = NULL;
2028 json_object *json_vtep_list = NULL;
2029
2030 vty = ctxt[0];
2031 json = ctxt[1];
cec2e17d 2032
e3b78da8 2033 zvni = (zebra_vni_t *)bucket->data;
cec2e17d 2034
d62a17ae 2035 zvtep = zvni->vteps;
2036 while (zvtep) {
2037 num_vteps++;
2038 zvtep = zvtep->next;
2039 }
cec2e17d 2040
790f8dc5 2041 num_macs = num_valid_macs(zvni);
d62a17ae 2042 num_neigh = hashcount(zvni->neigh_table);
cd233079 2043 if (json == NULL)
996c9314 2044 vty_out(vty, "%-10u %-4s %-21s %-8u %-8u %-15u %-37s\n",
4cce389e 2045 zvni->vni, "L2",
cd233079 2046 zvni->vxlan_if ? zvni->vxlan_if->name : "unknown",
996c9314 2047 num_macs, num_neigh, num_vteps,
b7cfce93 2048 vrf_id_to_name(zvni->vrf_id));
cd233079
CS
2049 else {
2050 char vni_str[VNI_STR_LEN];
2051 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
2052 json_vni = json_object_new_object();
4cce389e
MK
2053 json_object_int_add(json_vni, "vni", zvni->vni);
2054 json_object_string_add(json_vni, "type", "L2");
cd233079
CS
2055 json_object_string_add(json_vni, "vxlanIf",
2056 zvni->vxlan_if ? zvni->vxlan_if->name
2057 : "unknown");
cd233079
CS
2058 json_object_int_add(json_vni, "numMacs", num_macs);
2059 json_object_int_add(json_vni, "numArpNd", num_neigh);
2060 json_object_int_add(json_vni, "numRemoteVteps", num_vteps);
4cce389e
MK
2061 json_object_string_add(json_vni, "tenantVrf",
2062 vrf_id_to_name(zvni->vrf_id));
cd233079
CS
2063 if (num_vteps) {
2064 json_vtep_list = json_object_new_array();
2065 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
2066 json_ip_str = json_object_new_string(
2067 inet_ntoa(zvtep->vtep_ip));
2068 json_object_array_add(json_vtep_list,
2069 json_ip_str);
2070 }
2071 json_object_object_add(json_vni, "remoteVteps",
2072 json_vtep_list);
2073 }
2074 json_object_object_add(json, vni_str, json_vni);
2075 }
cec2e17d 2076}
2077
09af6961
NS
2078/*
2079 * Print a VNI hash entry in detail - called for display of all VNIs.
2080 */
e3b78da8 2081static void zvni_print_hash_detail(struct hash_bucket *bucket, void *data)
09af6961
NS
2082{
2083 struct vty *vty;
2084 zebra_vni_t *zvni;
2085 json_object *json = NULL;
2086 bool use_json = false;
2087 struct zvni_evpn_show *zes = data;
2088
2089 vty = zes->vty;
2090 json = zes->json;
2091
2092 if (json)
2093 use_json = true;
2094
e3b78da8 2095 zvni = (zebra_vni_t *)bucket->data;
09af6961
NS
2096
2097 zebra_vxlan_print_vni(vty, zes->zvrf, zvni->vni, use_json);
2098 vty_out(vty, "\n");
2099}
2100
13d60d35 2101/*
2232a77c 2102 * Inform BGP about local MACIP.
2103 */
996c9314 2104static int zvni_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr,
d7c0a89a 2105 struct ipaddr *ip, uint8_t flags,
2c476b72 2106 uint32_t seq, int state, uint16_t cmd)
d62a17ae 2107{
d62a17ae 2108 char buf[ETHER_ADDR_STRLEN];
2109 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
2110 int ipa_len;
2111 struct zserv *client = NULL;
2112 struct stream *s = NULL;
d62a17ae 2113
21ccc0cf 2114 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
d62a17ae 2115 /* BGP may not be running. */
2116 if (!client)
2117 return 0;
2118
1002497a 2119 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
d62a17ae 2120
43779a11 2121 zclient_create_header(s, cmd, zebra_vrf_get_evpn_id());
d62a17ae 2122 stream_putl(s, vni);
ff8b7eb8 2123 stream_put(s, macaddr->octet, ETH_ALEN);
d62a17ae 2124 if (ip) {
2125 ipa_len = 0;
2126 if (IS_IPADDR_V4(ip))
2127 ipa_len = IPV4_MAX_BYTELEN;
2128 else if (IS_IPADDR_V6(ip))
2129 ipa_len = IPV6_MAX_BYTELEN;
2130
2131 stream_putl(s, ipa_len); /* IP address length */
2132 if (ipa_len)
2133 stream_put(s, &ip->ip.addr, ipa_len); /* IP address */
2134 } else
2135 stream_putl(s, 0); /* Just MAC. */
2136
f07e1c99 2137 if (cmd == ZEBRA_MACIP_ADD) {
2138 stream_putc(s, flags); /* sticky mac/gateway mac */
2139 stream_putl(s, seq); /* sequence number */
2c476b72
AK
2140 } else {
2141 stream_putl(s, state); /* state - active/inactive */
f07e1c99 2142 }
d62a17ae 2143
b7cfce93 2144
d62a17ae 2145 /* Write packet size. */
2146 stream_putw_at(s, 0, stream_get_endp(s));
2147
2148 if (IS_ZEBRA_DEBUG_VXLAN)
1a98c087 2149 zlog_debug(
f07e1c99 2150 "Send MACIP %s flags 0x%x MAC %s IP %s seq %u L2-VNI %u to %s",
996c9314
LB
2151 (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
2152 prefix_mac2str(macaddr, buf, sizeof(buf)),
f07e1c99 2153 ipaddr2str(ip, buf2, sizeof(buf2)), seq, vni,
1a98c087 2154 zebra_route_string(client->proto));
d62a17ae 2155
2156 if (cmd == ZEBRA_MACIP_ADD)
2157 client->macipadd_cnt++;
2158 else
2159 client->macipdel_cnt++;
2160
21ccc0cf 2161 return zserv_send_message(client, s);
2232a77c 2162}
2163
2164/*
2165 * Make hash key for neighbors.
13d60d35 2166 */
d8b87afe 2167static unsigned int neigh_hash_keymake(const void *p)
13d60d35 2168{
d8b87afe
QY
2169 const zebra_neigh_t *n = p;
2170 const struct ipaddr *ip = &n->ip;
13d60d35 2171
d62a17ae 2172 if (IS_IPADDR_V4(ip))
2173 return jhash_1word(ip->ipaddr_v4.s_addr, 0);
2232a77c 2174
d62a17ae 2175 return jhash2(ip->ipaddr_v6.s6_addr32,
7e3a1ec7 2176 array_size(ip->ipaddr_v6.s6_addr32), 0);
13d60d35 2177}
2178
2179/*
2232a77c 2180 * Compare two neighbor hash structures.
13d60d35 2181 */
74df8d6d 2182static bool neigh_cmp(const void *p1, const void *p2)
13d60d35 2183{
d62a17ae 2184 const zebra_neigh_t *n1 = p1;
2185 const zebra_neigh_t *n2 = p2;
13d60d35 2186
d62a17ae 2187 if (n1 == NULL && n2 == NULL)
74df8d6d 2188 return true;
2232a77c 2189
d62a17ae 2190 if (n1 == NULL || n2 == NULL)
74df8d6d 2191 return false;
2232a77c 2192
d62a17ae 2193 return (memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr)) == 0);
13d60d35 2194}
2195
27fa3398
DS
2196static int neigh_list_cmp(void *p1, void *p2)
2197{
2198 const zebra_neigh_t *n1 = p1;
2199 const zebra_neigh_t *n2 = p2;
2200
2201 return memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr));
2202}
2203
13d60d35 2204/*
2232a77c 2205 * Callback to allocate neighbor hash entry.
13d60d35 2206 */
d62a17ae 2207static void *zvni_neigh_alloc(void *p)
13d60d35 2208{
d62a17ae 2209 const zebra_neigh_t *tmp_n = p;
2210 zebra_neigh_t *n;
13d60d35 2211
d62a17ae 2212 n = XCALLOC(MTYPE_NEIGH, sizeof(zebra_neigh_t));
2213 *n = *tmp_n;
2232a77c 2214
d62a17ae 2215 return ((void *)n);
13d60d35 2216}
2217
2218/*
2232a77c 2219 * Add neighbor entry.
13d60d35 2220 */
b6938a74
MK
2221static zebra_neigh_t *zvni_neigh_add(zebra_vni_t *zvni, struct ipaddr *ip,
2222 struct ethaddr *mac)
13d60d35 2223{
d62a17ae 2224 zebra_neigh_t tmp_n;
2225 zebra_neigh_t *n = NULL;
b6938a74 2226 zebra_mac_t *zmac = NULL;
13d60d35 2227
d62a17ae 2228 memset(&tmp_n, 0, sizeof(zebra_neigh_t));
2229 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
2230 n = hash_get(zvni->neigh_table, &tmp_n, zvni_neigh_alloc);
2231 assert(n);
13d60d35 2232
b6938a74
MK
2233 memcpy(&n->emac, mac, ETH_ALEN);
2234 n->state = ZEBRA_NEIGH_INACTIVE;
d346c2e9 2235 n->zvni = zvni;
55328d8a 2236 n->dad_ip_auto_recovery_timer = NULL;
b6938a74
MK
2237
2238 /* Associate the neigh to mac */
2239 zmac = zvni_mac_lookup(zvni, mac);
2240 if (zmac)
2241 listnode_add_sort(zmac->neigh_list, n);
2242
d62a17ae 2243 return n;
13d60d35 2244}
2245
2246/*
2232a77c 2247 * Delete neighbor entry.
13d60d35 2248 */
d62a17ae 2249static int zvni_neigh_del(zebra_vni_t *zvni, zebra_neigh_t *n)
13d60d35 2250{
d62a17ae 2251 zebra_neigh_t *tmp_n;
b6938a74
MK
2252 zebra_mac_t *zmac = NULL;
2253
2254 zmac = zvni_mac_lookup(zvni, &n->emac);
2255 if (zmac)
2256 listnode_delete(zmac->neigh_list, n);
13d60d35 2257
55328d8a
CS
2258 /* Cancel auto recovery */
2259 THREAD_OFF(n->dad_ip_auto_recovery_timer);
2260
d62a17ae 2261 /* Free the VNI hash entry and allocated memory. */
2262 tmp_n = hash_release(zvni->neigh_table, n);
0a22ddfb 2263 XFREE(MTYPE_NEIGH, tmp_n);
13d60d35 2264
d62a17ae 2265 return 0;
13d60d35 2266}
2267
2268/*
2232a77c 2269 * Free neighbor hash entry (callback)
13d60d35 2270 */
e3b78da8 2271static void zvni_neigh_del_hash_entry(struct hash_bucket *bucket, void *arg)
13d60d35 2272{
d62a17ae 2273 struct neigh_walk_ctx *wctx = arg;
e3b78da8 2274 zebra_neigh_t *n = bucket->data;
2232a77c 2275
d62a17ae 2276 if (((wctx->flags & DEL_LOCAL_NEIGH) && (n->flags & ZEBRA_NEIGH_LOCAL))
2277 || ((wctx->flags & DEL_REMOTE_NEIGH)
2278 && (n->flags & ZEBRA_NEIGH_REMOTE))
2279 || ((wctx->flags & DEL_REMOTE_NEIGH_FROM_VTEP)
2280 && (n->flags & ZEBRA_NEIGH_REMOTE)
2281 && IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))) {
2282 if (wctx->upd_client && (n->flags & ZEBRA_NEIGH_LOCAL))
2853fed6 2283 zvni_neigh_send_del_to_client(wctx->zvni->vni, &n->ip,
2c476b72 2284 &n->emac, 0, n->state);
13d60d35 2285
d62a17ae 2286 if (wctx->uninstall)
2287 zvni_neigh_uninstall(wctx->zvni, n);
13d60d35 2288
b1599bb6 2289 zvni_neigh_del(wctx->zvni, n);
d62a17ae 2290 }
13d60d35 2291
b1599bb6 2292 return;
13d60d35 2293}
2294
2295/*
2232a77c 2296 * Delete all neighbor entries from specific VTEP for a particular VNI.
13d60d35 2297 */
d62a17ae 2298static void zvni_neigh_del_from_vtep(zebra_vni_t *zvni, int uninstall,
2299 struct in_addr *r_vtep_ip)
13d60d35 2300{
d62a17ae 2301 struct neigh_walk_ctx wctx;
13d60d35 2302
d62a17ae 2303 if (!zvni->neigh_table)
2304 return;
13d60d35 2305
d62a17ae 2306 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
2307 wctx.zvni = zvni;
2308 wctx.uninstall = uninstall;
2309 wctx.flags = DEL_REMOTE_NEIGH_FROM_VTEP;
2310 wctx.r_vtep_ip = *r_vtep_ip;
13d60d35 2311
b1599bb6 2312 hash_iterate(zvni->neigh_table, zvni_neigh_del_hash_entry, &wctx);
2232a77c 2313}
13d60d35 2314
2232a77c 2315/*
2316 * Delete all neighbor entries for this VNI.
2317 */
996c9314 2318static void zvni_neigh_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
d7c0a89a 2319 uint32_t flags)
2232a77c 2320{
d62a17ae 2321 struct neigh_walk_ctx wctx;
13d60d35 2322
d62a17ae 2323 if (!zvni->neigh_table)
2324 return;
13d60d35 2325
d62a17ae 2326 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
2327 wctx.zvni = zvni;
d62a17ae 2328 wctx.uninstall = uninstall;
2329 wctx.upd_client = upd_client;
2330 wctx.flags = flags;
2232a77c 2331
b1599bb6 2332 hash_iterate(zvni->neigh_table, zvni_neigh_del_hash_entry, &wctx);
13d60d35 2333}
2334
2335/*
2232a77c 2336 * Look up neighbor hash entry.
2337 */
d62a17ae 2338static zebra_neigh_t *zvni_neigh_lookup(zebra_vni_t *zvni, struct ipaddr *ip)
2232a77c 2339{
d62a17ae 2340 zebra_neigh_t tmp;
2341 zebra_neigh_t *n;
2232a77c 2342
d62a17ae 2343 memset(&tmp, 0, sizeof(tmp));
2344 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
2345 n = hash_lookup(zvni->neigh_table, &tmp);
2232a77c 2346
d62a17ae 2347 return n;
2232a77c 2348}
2349
f07e1c99 2350/*
2351 * Process all neighbors associated with a MAC upon the MAC being learnt
2352 * locally or undergoing any other change (such as sequence number).
2353 */
2354static void zvni_process_neigh_on_local_mac_change(zebra_vni_t *zvni,
2355 zebra_mac_t *zmac,
2356 bool seq_change)
b6938a74
MK
2357{
2358 zebra_neigh_t *n = NULL;
2359 struct listnode *node = NULL;
c80a972c 2360 struct zebra_vrf *zvrf = NULL;
b6938a74 2361 char buf[ETHER_ADDR_STRLEN];
b6938a74 2362
a36898e7 2363 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
c80a972c 2364
f07e1c99 2365 if (IS_ZEBRA_DEBUG_VXLAN)
2366 zlog_debug("Processing neighbors on local MAC %s %s, VNI %u",
2367 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
2368 seq_change ? "CHANGE" : "ADD", zvni->vni);
2369
2370 /* Walk all neighbors and mark any inactive local neighbors as
2371 * active and/or update sequence number upon a move, and inform BGP.
2372 * The action for remote neighbors is TBD.
2373 * NOTE: We can't simply uninstall remote neighbors as the kernel may
2374 * accidentally end up deleting a just-learnt local neighbor.
2375 */
b6938a74
MK
2376 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
2377 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
f07e1c99 2378 if (IS_ZEBRA_NEIGH_INACTIVE(n) || seq_change) {
b6938a74 2379 ZEBRA_NEIGH_SET_ACTIVE(n);
f07e1c99 2380 n->loc_seq = zmac->loc_seq;
c80a972c
CS
2381 if (!(zvrf->dup_addr_detect &&
2382 zvrf->dad_freeze && !!CHECK_FLAG(n->flags,
2383 ZEBRA_NEIGH_DUPLICATE)))
2384 zvni_neigh_send_add_to_client(
2385 zvni->vni, &n->ip, &n->emac,
2386 n->flags, n->loc_seq);
b6938a74 2387 }
b6938a74
MK
2388 }
2389 }
2390}
2391
f07e1c99 2392/*
2393 * Process all neighbors associated with a local MAC upon the MAC being
2394 * deleted.
2395 */
2853fed6 2396static void zvni_process_neigh_on_local_mac_del(zebra_vni_t *zvni,
b6938a74
MK
2397 zebra_mac_t *zmac)
2398{
2399 zebra_neigh_t *n = NULL;
2400 struct listnode *node = NULL;
2401 char buf[ETHER_ADDR_STRLEN];
b6938a74 2402
f07e1c99 2403 if (IS_ZEBRA_DEBUG_VXLAN)
2404 zlog_debug("Processing neighbors on local MAC %s DEL, VNI %u",
2405 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
2406 zvni->vni);
2407
2408 /* Walk all local neighbors and mark as inactive and inform
2409 * BGP, if needed.
2410 * TBD: There is currently no handling for remote neighbors. We
2411 * don't expect them to exist, if they do, do we install the MAC
2412 * as a remote MAC and the neighbor as remote?
2413 */
b6938a74
MK
2414 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
2415 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2416 if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
b6938a74 2417 ZEBRA_NEIGH_SET_INACTIVE(n);
f07e1c99 2418 n->loc_seq = 0;
996c9314 2419 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
2c476b72 2420 &n->emac, 0, ZEBRA_NEIGH_ACTIVE);
b6938a74 2421 }
b6938a74
MK
2422 }
2423 }
2424}
2425
f07e1c99 2426/*
2427 * Process all neighbors associated with a MAC upon the MAC being remotely
2428 * learnt.
2429 */
2853fed6 2430static void zvni_process_neigh_on_remote_mac_add(zebra_vni_t *zvni,
b6938a74
MK
2431 zebra_mac_t *zmac)
2432{
2433 zebra_neigh_t *n = NULL;
2434 struct listnode *node = NULL;
2435 char buf[ETHER_ADDR_STRLEN];
b6938a74 2436
f07e1c99 2437 if (IS_ZEBRA_DEBUG_VXLAN)
2438 zlog_debug("Processing neighbors on remote MAC %s ADD, VNI %u",
2439 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
2440 zvni->vni);
2441
2442 /* Walk all local neighbors and mark as inactive and inform
2443 * BGP, if needed.
2444 */
b6938a74
MK
2445 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
2446 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2447 if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
b6938a74 2448 ZEBRA_NEIGH_SET_INACTIVE(n);
f07e1c99 2449 n->loc_seq = 0;
996c9314 2450 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
2c476b72 2451 &n->emac, 0, ZEBRA_NEIGH_ACTIVE);
b6938a74
MK
2452 }
2453 }
2454 }
2455}
2456
f07e1c99 2457/*
2458 * Process all neighbors associated with a remote MAC upon the MAC being
2459 * deleted.
2460 */
2853fed6 2461static void zvni_process_neigh_on_remote_mac_del(zebra_vni_t *zvni,
b6938a74
MK
2462 zebra_mac_t *zmac)
2463{
f07e1c99 2464 /* NOTE: Currently a NO-OP. */
b6938a74
MK
2465}
2466
44bc8ae5
CS
2467static void zvni_probe_neigh_on_mac_add(zebra_vni_t *zvni, zebra_mac_t *zmac)
2468{
2469 zebra_neigh_t *nbr = NULL;
2470 struct listnode *node = NULL;
2471
2472 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, nbr)) {
2473 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL) &&
2474 IS_ZEBRA_NEIGH_INACTIVE(nbr))
2475 zvni_neigh_probe(zvni, nbr);
2476 }
2477}
2478
2232a77c 2479/*
2480 * Inform BGP about local neighbor addition.
13d60d35 2481 */
996c9314 2482static int zvni_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
ead40654 2483 struct ethaddr *macaddr,
f07e1c99 2484 uint8_t neigh_flags,
2485 uint32_t seq)
13d60d35 2486{
d7c0a89a 2487 uint8_t flags = 0;
ead40654
MK
2488
2489 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_DEF_GW))
2490 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
68e33151
CS
2491 /* Set router flag (R-bit) based on local neigh entry add */
2492 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_ROUTER_FLAG))
2493 SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
ead40654 2494
2853fed6 2495 return zvni_macip_send_msg_to_client(vni, macaddr, ip, flags,
2c476b72 2496 seq, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_ADD);
2232a77c 2497}
13d60d35 2498
2232a77c 2499/*
2500 * Inform BGP about local neighbor deletion.
2501 */
996c9314 2502static int zvni_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
2c476b72
AK
2503 struct ethaddr *macaddr, uint8_t flags,
2504 int state)
2232a77c 2505{
2853fed6 2506 return zvni_macip_send_msg_to_client(vni, macaddr, ip, flags,
2c476b72 2507 0, state, ZEBRA_MACIP_DEL);
2232a77c 2508}
2509
2510/*
2511 * Install remote neighbor into the kernel.
2512 */
d62a17ae 2513static int zvni_neigh_install(zebra_vni_t *zvni, zebra_neigh_t *n)
2232a77c 2514{
d62a17ae 2515 struct zebra_if *zif;
2516 struct zebra_l2info_vxlan *vxl;
2517 struct interface *vlan_if;
931fa60c 2518 int flags;
68e33151 2519 int ret = 0;
2232a77c 2520
d62a17ae 2521 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
2522 return 0;
13d60d35 2523
d62a17ae 2524 zif = zvni->vxlan_if->info;
2525 if (!zif)
2526 return -1;
2527 vxl = &zif->l2info.vxl;
13d60d35 2528
2853fed6 2529 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
d62a17ae 2530 if (!vlan_if)
2531 return -1;
931fa60c
MS
2532
2533 flags = DPLANE_NTF_EXT_LEARNED;
68e33151 2534 if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
931fa60c 2535 flags |= DPLANE_NTF_ROUTER;
f07e1c99 2536 ZEBRA_NEIGH_SET_ACTIVE(n);
931fa60c
MS
2537
2538 dplane_neigh_add(vlan_if, &n->ip, &n->emac, flags);
2539
68e33151 2540 return ret;
2232a77c 2541}
13d60d35 2542
2232a77c 2543/*
2544 * Uninstall remote neighbor from the kernel.
2545 */
d62a17ae 2546static int zvni_neigh_uninstall(zebra_vni_t *zvni, zebra_neigh_t *n)
2232a77c 2547{
d62a17ae 2548 struct zebra_if *zif;
2549 struct zebra_l2info_vxlan *vxl;
2550 struct interface *vlan_if;
13d60d35 2551
d62a17ae 2552 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
2553 return 0;
2232a77c 2554
d62a17ae 2555 if (!zvni->vxlan_if) {
e0e140a7
MS
2556 if (IS_ZEBRA_DEBUG_VXLAN)
2557 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
2558 zvni->vni, zvni);
d62a17ae 2559 return -1;
2560 }
2232a77c 2561
d62a17ae 2562 zif = zvni->vxlan_if->info;
2563 if (!zif)
2564 return -1;
2565 vxl = &zif->l2info.vxl;
2853fed6 2566 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
d62a17ae 2567 if (!vlan_if)
2568 return -1;
2232a77c 2569
f07e1c99 2570 ZEBRA_NEIGH_SET_INACTIVE(n);
2571 n->loc_seq = 0;
931fa60c
MS
2572
2573 dplane_neigh_delete(vlan_if, &n->ip);
2574
2575 return 0;
13d60d35 2576}
2577
44bc8ae5
CS
2578/*
2579 * Probe neighbor from the kernel.
2580 */
2581static int zvni_neigh_probe(zebra_vni_t *zvni, zebra_neigh_t *n)
2582{
2583 struct zebra_if *zif;
2584 struct zebra_l2info_vxlan *vxl;
2585 struct interface *vlan_if;
2586
2587 zif = zvni->vxlan_if->info;
2588 if (!zif)
2589 return -1;
2590 vxl = &zif->l2info.vxl;
2591
2592 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
2593 if (!vlan_if)
2594 return -1;
2595
931fa60c
MS
2596 dplane_neigh_update(vlan_if, &n->ip, &n->emac);
2597
44bc8ae5 2598 return 0;
44bc8ae5
CS
2599}
2600
13d60d35 2601/*
2232a77c 2602 * Install neighbor hash entry - called upon access VLAN change.
13d60d35 2603 */
e3b78da8 2604static void zvni_install_neigh_hash(struct hash_bucket *bucket, void *ctxt)
13d60d35 2605{
d62a17ae 2606 zebra_neigh_t *n;
2607 struct neigh_walk_ctx *wctx = ctxt;
13d60d35 2608
e3b78da8 2609 n = (zebra_neigh_t *)bucket->data;
13d60d35 2610
d62a17ae 2611 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
2612 zvni_neigh_install(wctx->zvni, n);
2232a77c 2613}
13d60d35 2614
1a98c087
MK
2615/* Get the VRR interface for SVI if any */
2616struct interface *zebra_get_vrr_intf_for_svi(struct interface *ifp)
2617{
2618 struct zebra_vrf *zvrf = NULL;
2619 struct interface *tmp_if = NULL;
2620 struct zebra_if *zif = NULL;
1a98c087 2621
a36898e7 2622 zvrf = vrf_info_lookup(ifp->vrf_id);
1a98c087
MK
2623 assert(zvrf);
2624
451fda4f 2625 FOR_ALL_INTERFACES (zvrf->vrf, tmp_if) {
1a98c087
MK
2626 zif = tmp_if->info;
2627 if (!zif)
2628 continue;
2629
2630 if (!IS_ZEBRA_IF_MACVLAN(tmp_if))
2631 continue;
2632
2633 if (zif->link == ifp)
2634 return tmp_if;
2635 }
2636
2637 return NULL;
2638}
2639
2640static int zvni_del_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni)
2641{
1a98c087
MK
2642 struct listnode *cnode = NULL, *cnnode = NULL;
2643 struct connected *c = NULL;
2644 struct ethaddr macaddr;
2645
1a98c087
MK
2646 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
2647
2648 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
2649 struct ipaddr ip;
2650
2651 memset(&ip, 0, sizeof(struct ipaddr));
2652 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
2653 continue;
2654
2655 if (c->address->family == AF_INET) {
2656 ip.ipa_type = IPADDR_V4;
2657 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
2658 sizeof(struct in_addr));
2659 } else if (c->address->family == AF_INET6) {
2660 ip.ipa_type = IPADDR_V6;
2661 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
2662 sizeof(struct in6_addr));
2663 } else {
2664 continue;
2665 }
2666
2667 zvni_gw_macip_del(ifp, zvni, &ip);
2668 }
2669
2670 return 0;
2671}
2672
2673static int zvni_add_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni)
2674{
1a98c087
MK
2675 struct listnode *cnode = NULL, *cnnode = NULL;
2676 struct connected *c = NULL;
2677 struct ethaddr macaddr;
2678
1a98c087
MK
2679 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
2680
2681 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
2682 struct ipaddr ip;
2683
2684 memset(&ip, 0, sizeof(struct ipaddr));
2685 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
2686 continue;
2687
2688 if (c->address->family == AF_INET) {
2689 ip.ipa_type = IPADDR_V4;
2690 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
2691 sizeof(struct in_addr));
2692 } else if (c->address->family == AF_INET6) {
2693 ip.ipa_type = IPADDR_V6;
2694 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
2695 sizeof(struct in6_addr));
2696 } else {
2697 continue;
2698 }
2699
2700 zvni_gw_macip_add(ifp, zvni, &macaddr, &ip);
2701 }
31310b25
MK
2702 return 0;
2703}
2704
2705
996c9314 2706static int zvni_advertise_subnet(zebra_vni_t *zvni, struct interface *ifp,
31310b25
MK
2707 int advertise)
2708{
2709 struct listnode *cnode = NULL, *cnnode = NULL;
2710 struct connected *c = NULL;
2711 struct ethaddr macaddr;
2712
2713 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
2714
2715 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
2716 struct prefix p;
1a98c087 2717
31310b25
MK
2718 memcpy(&p, c->address, sizeof(struct prefix));
2719
2720 /* skip link local address */
2721 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
2722 continue;
2723
2724 apply_mask(&p);
2725 if (advertise)
a36898e7 2726 ip_prefix_send_to_client(ifp->vrf_id, &p,
996c9314 2727 ZEBRA_IP_PREFIX_ROUTE_ADD);
31310b25 2728 else
a36898e7 2729 ip_prefix_send_to_client(ifp->vrf_id, &p,
31310b25
MK
2730 ZEBRA_IP_PREFIX_ROUTE_DEL);
2731 }
1a98c087
MK
2732 return 0;
2733}
2734
2735/*
2736 * zvni_gw_macip_add_to_client
2737 */
2738static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni,
2739 struct ethaddr *macaddr, struct ipaddr *ip)
2740{
1a98c087
MK
2741 char buf[ETHER_ADDR_STRLEN];
2742 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
2743 zebra_neigh_t *n = NULL;
2744 zebra_mac_t *mac = NULL;
2745 struct zebra_if *zif = NULL;
2746 struct zebra_l2info_vxlan *vxl = NULL;
2747
1a98c087
MK
2748 zif = zvni->vxlan_if->info;
2749 if (!zif)
2750 return -1;
2751
2752 vxl = &zif->l2info.vxl;
2753
2754 mac = zvni_mac_lookup(zvni, macaddr);
2755 if (!mac) {
2756 mac = zvni_mac_add(zvni, macaddr);
2757 if (!mac) {
e914ccbe 2758 flog_err(EC_ZEBRA_MAC_ADD_FAILED,
1c50c1c0
QY
2759 "Failed to add MAC %s intf %s(%u) VID %u",
2760 prefix_mac2str(macaddr, buf, sizeof(buf)),
2761 ifp->name, ifp->ifindex, vxl->access_vlan);
1a98c087
MK
2762 return -1;
2763 }
2764 }
2765
2766 /* Set "local" forwarding info. */
2767 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
2768 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
ead40654 2769 SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW);
1a98c087
MK
2770 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
2771 mac->fwd_info.local.ifindex = ifp->ifindex;
2772 mac->fwd_info.local.vid = vxl->access_vlan;
2773
2774 n = zvni_neigh_lookup(zvni, ip);
2775 if (!n) {
b6938a74 2776 n = zvni_neigh_add(zvni, ip, macaddr);
1a98c087 2777 if (!n) {
af4c2728 2778 flog_err(
e914ccbe 2779 EC_ZEBRA_MAC_ADD_FAILED,
2853fed6 2780 "Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
2781 ipaddr2str(ip, buf2, sizeof(buf2)),
0af35d90 2782 prefix_mac2str(macaddr, buf, sizeof(buf)),
1a98c087
MK
2783 ifp->name, ifp->ifindex, zvni->vni);
2784 return -1;
2785 }
2786 }
2787
2788 /* Set "local" forwarding info. */
2789 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
1a8c5c38 2790 ZEBRA_NEIGH_SET_ACTIVE(n);
1a98c087
MK
2791 memcpy(&n->emac, macaddr, ETH_ALEN);
2792 n->ifindex = ifp->ifindex;
2793
ead40654 2794 /* Only advertise in BGP if the knob is enabled */
838cef6d 2795 if (advertise_gw_macip_enabled(zvni)) {
ead40654 2796
838cef6d
CS
2797 SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW);
2798 SET_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW);
2799 /* Set Router flag (R-bit) */
2800 if (ip->ipa_type == IPADDR_V6)
2801 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2802
2803 if (IS_ZEBRA_DEBUG_VXLAN)
2804 zlog_debug(
68e33151 2805 "SVI %s(%u) L2-VNI %u, sending GW MAC %s IP %s add to BGP with flags 0x%x",
2853fed6 2806 ifp->name, ifp->ifindex, zvni->vni,
1e9f448f 2807 prefix_mac2str(macaddr, buf, sizeof(buf)),
68e33151 2808 ipaddr2str(ip, buf2, sizeof(buf2)), n->flags);
1a98c087 2809
838cef6d
CS
2810 zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
2811 n->flags, n->loc_seq);
2812 } else if (advertise_svi_macip_enabled(zvni)) {
2813
2814 if (IS_ZEBRA_DEBUG_VXLAN)
2815 zlog_debug(
2816 "SVI %s(%u) L2-VNI %u, sending SVI MAC %s IP %s add to BGP with flags 0x%x",
2817 ifp->name, ifp->ifindex, zvni->vni,
2818 prefix_mac2str(macaddr, buf, sizeof(buf)),
2819 ipaddr2str(ip, buf2, sizeof(buf2)), n->flags);
2820
2821 zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
2822 n->flags, n->loc_seq);
2823 }
1a98c087
MK
2824
2825 return 0;
2826}
2827
2828/*
2829 * zvni_gw_macip_del_from_client
2830 */
2831static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni,
2832 struct ipaddr *ip)
2833{
0af35d90 2834 char buf1[ETHER_ADDR_STRLEN];
1a98c087 2835 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
2836 zebra_neigh_t *n = NULL;
2837 zebra_mac_t *mac = NULL;
2838
1a98c087
MK
2839 /* If the neigh entry is not present nothing to do*/
2840 n = zvni_neigh_lookup(zvni, ip);
2841 if (!n)
2842 return 0;
2843
2844 /* mac entry should be present */
2845 mac = zvni_mac_lookup(zvni, &n->emac);
0af35d90 2846 if (!mac) {
e0e140a7
MS
2847 if (IS_ZEBRA_DEBUG_VXLAN)
2848 zlog_debug("MAC %s doesn't exist for neigh %s on VNI %u",
2849 prefix_mac2str(&n->emac,
2850 buf1, sizeof(buf1)),
2851 ipaddr2str(ip, buf2, sizeof(buf2)),
2852 zvni->vni);
0af35d90
RW
2853 return -1;
2854 }
1a98c087
MK
2855
2856 /* If the entry is not local nothing to do*/
2857 if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL))
2858 return -1;
2859
ead40654 2860 /* only need to delete the entry from bgp if we sent it before */
01a6143b 2861 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
2862 zlog_debug(
2863 "%u:SVI %s(%u) VNI %u, sending GW MAC %s IP %s del to BGP",
a36898e7 2864 ifp->vrf_id, ifp->name, ifp->ifindex, zvni->vni,
ee496c3b 2865 prefix_mac2str(&(n->emac), buf1, sizeof(buf1)),
996c9314 2866 ipaddr2str(ip, buf2, sizeof(buf2)));
01a6143b
MK
2867
2868 /* Remove neighbor from BGP. */
2869 zvni_neigh_send_del_to_client(zvni->vni, &n->ip, &n->emac,
2c476b72 2870 ZEBRA_MACIP_TYPE_GW, ZEBRA_NEIGH_ACTIVE);
1a98c087
MK
2871
2872 /* Delete this neighbor entry. */
2873 zvni_neigh_del(zvni, n);
2874
2875 /* see if the mac needs to be deleted as well*/
1e9f448f 2876 if (mac)
fe697c6b 2877 zvni_deref_ip2mac(zvni, mac);
1a98c087
MK
2878
2879 return 0;
2880}
2881
e3b78da8 2882static void zvni_gw_macip_del_for_vni_hash(struct hash_bucket *bucket,
2853fed6 2883 void *ctxt)
1a98c087
MK
2884{
2885 zebra_vni_t *zvni = NULL;
2886 struct zebra_if *zif = NULL;
2887 struct zebra_l2info_vxlan zl2_info;
2888 struct interface *vlan_if = NULL;
2889 struct interface *vrr_if = NULL;
b5ebdc9b 2890 struct interface *ifp;
1a98c087
MK
2891
2892 /* Add primary SVI MAC*/
e3b78da8 2893 zvni = (zebra_vni_t *)bucket->data;
1a98c087 2894
27627f9a
KA
2895 /* Global (Zvrf) advertise-default-gw is disabled,
2896 * but zvni advertise-default-gw is enabled
2897 */
2898 if (zvni->advertise_gw_macip) {
2899 if (IS_ZEBRA_DEBUG_VXLAN)
2900 zlog_debug("VNI: %u GW-MACIP enabled, retain gw-macip",
2901 zvni->vni);
2902 return;
2903 }
2904
b5ebdc9b 2905 ifp = zvni->vxlan_if;
2906 if (!ifp)
2907 return;
2908 zif = ifp->info;
2909
2910 /* If down or not mapped to a bridge, we're done. */
b682f6de 2911 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
b5ebdc9b 2912 return;
2913
1a98c087
MK
2914 zl2_info = zif->l2info.vxl;
2915
996c9314
LB
2916 vlan_if =
2917 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
1a98c087
MK
2918 if (!vlan_if)
2919 return;
2920
2921 /* Del primary MAC-IP */
2922 zvni_del_macip_for_intf(vlan_if, zvni);
2923
2924 /* Del VRR MAC-IP - if any*/
2925 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
2926 if (vrr_if)
2927 zvni_del_macip_for_intf(vrr_if, zvni);
2928
2929 return;
2930}
2931
e3b78da8 2932static void zvni_gw_macip_add_for_vni_hash(struct hash_bucket *bucket,
2853fed6 2933 void *ctxt)
1a98c087
MK
2934{
2935 zebra_vni_t *zvni = NULL;
2936 struct zebra_if *zif = NULL;
2937 struct zebra_l2info_vxlan zl2_info;
2938 struct interface *vlan_if = NULL;
2939 struct interface *vrr_if = NULL;
b5ebdc9b 2940 struct interface *ifp = NULL;
1a98c087 2941
e3b78da8 2942 zvni = (zebra_vni_t *)bucket->data;
1a98c087 2943
b5ebdc9b 2944 ifp = zvni->vxlan_if;
2945 if (!ifp)
2946 return;
2947 zif = ifp->info;
2948
2949 /* If down or not mapped to a bridge, we're done. */
b682f6de 2950 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
b5ebdc9b 2951 return;
1a98c087
MK
2952 zl2_info = zif->l2info.vxl;
2953
996c9314
LB
2954 vlan_if =
2955 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
1a98c087
MK
2956 if (!vlan_if)
2957 return;
2958
1a98c087
MK
2959 /* Add primary SVI MAC-IP */
2960 zvni_add_macip_for_intf(vlan_if, zvni);
2961
278e26de
CS
2962 if (advertise_gw_macip_enabled(zvni)) {
2963 /* Add VRR MAC-IP - if any*/
2964 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
2965 if (vrr_if)
2966 zvni_add_macip_for_intf(vrr_if, zvni);
2967 }
2968
2969 return;
2970}
2971
e3b78da8 2972static void zvni_svi_macip_del_for_vni_hash(struct hash_bucket *bucket,
278e26de
CS
2973 void *ctxt)
2974{
2975 zebra_vni_t *zvni = NULL;
2976 struct zebra_if *zif = NULL;
2977 struct zebra_l2info_vxlan zl2_info;
2978 struct interface *vlan_if = NULL;
2979 struct interface *ifp;
2980
2981 /* Add primary SVI MAC*/
e3b78da8 2982 zvni = (zebra_vni_t *)bucket->data;
278e26de
CS
2983 if (!zvni)
2984 return;
2985
27627f9a
KA
2986 /* Global(vrf) advertise-svi-ip disabled, but zvni advertise-svi-ip
2987 * enabled
2988 */
2989 if (zvni->advertise_svi_macip) {
2990 if (IS_ZEBRA_DEBUG_VXLAN)
2991 zlog_debug("VNI: %u SVI-MACIP enabled, retain svi-macip",
2992 zvni->vni);
2993 return;
2994 }
2995
278e26de
CS
2996 ifp = zvni->vxlan_if;
2997 if (!ifp)
2998 return;
2999 zif = ifp->info;
3000
3001 /* If down or not mapped to a bridge, we're done. */
3002 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
3003 return;
3004
3005 zl2_info = zif->l2info.vxl;
3006
3007 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
3008 zif->brslave_info.br_if);
3009 if (!vlan_if)
3010 return;
3011
3012 /* Del primary MAC-IP */
3013 zvni_del_macip_for_intf(vlan_if, zvni);
1a98c087
MK
3014
3015 return;
3016}
3017
ee69da27
MK
3018static int zvni_local_neigh_update(zebra_vni_t *zvni,
3019 struct interface *ifp,
3020 struct ipaddr *ip,
68e33151 3021 struct ethaddr *macaddr,
a37f4598 3022 bool is_router)
ee69da27
MK
3023{
3024 char buf[ETHER_ADDR_STRLEN];
3025 char buf2[INET6_ADDRSTRLEN];
e22a946a 3026 struct zebra_vrf *zvrf;
ee69da27
MK
3027 zebra_neigh_t *n = NULL;
3028 zebra_mac_t *zmac = NULL, *old_zmac = NULL;
f07e1c99 3029 uint32_t old_mac_seq = 0, mac_new_seq = 0;
3030 bool upd_mac_seq = false;
3031 bool neigh_mac_change = false;
e22a946a
CS
3032 bool neigh_on_hold = false;
3033 bool neigh_was_remote = false;
7510e459 3034 bool do_dad = false;
e22a946a 3035 struct in_addr vtep_ip = {.s_addr = 0};
ee69da27 3036
f07e1c99 3037 /* Check if the MAC exists. */
ee69da27
MK
3038 zmac = zvni_mac_lookup(zvni, macaddr);
3039 if (!zmac) {
f07e1c99 3040 /* create a dummy MAC if the MAC is not already present */
ee69da27
MK
3041 if (IS_ZEBRA_DEBUG_VXLAN)
3042 zlog_debug(
3043 "AUTO MAC %s created for neigh %s on VNI %u",
3044 prefix_mac2str(macaddr, buf, sizeof(buf)),
3045 ipaddr2str(ip, buf2, sizeof(buf2)), zvni->vni);
3046
3047 zmac = zvni_mac_add(zvni, macaddr);
3048 if (!zmac) {
9df414fe
QY
3049 zlog_debug("Failed to add MAC %s VNI %u",
3050 prefix_mac2str(macaddr, buf, sizeof(buf)),
3051 zvni->vni);
ee69da27
MK
3052 return -1;
3053 }
3054
3055 memset(&zmac->fwd_info, 0, sizeof(zmac->fwd_info));
3056 memset(&zmac->flags, 0, sizeof(uint32_t));
3057 SET_FLAG(zmac->flags, ZEBRA_MAC_AUTO);
f07e1c99 3058 } else {
3059 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
3060 /*
3061 * We don't change the MAC to local upon a neighbor
3062 * learn event, we wait for the explicit local MAC
3063 * learn. However, we have to compute its sequence
3064 * number in preparation for when it actually turns
3065 * local.
3066 */
3067 upd_mac_seq = true;
3068 }
ee69da27
MK
3069 }
3070
a36898e7 3071 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
28bd0652
DS
3072 if (!zvrf) {
3073 if (IS_ZEBRA_DEBUG_VXLAN)
a36898e7
DS
3074 zlog_debug("\tUnable to find vrf for: %d",
3075 zvni->vxlan_if->vrf_id);
e22a946a 3076 return -1;
28bd0652 3077 }
e22a946a 3078
f07e1c99 3079 /* Check if the neighbor exists. */
ee69da27 3080 n = zvni_neigh_lookup(zvni, ip);
f07e1c99 3081 if (!n) {
3082 /* New neighbor - create */
3083 n = zvni_neigh_add(zvni, ip, macaddr);
3084 if (!n) {
3085 flog_err(
e914ccbe 3086 EC_ZEBRA_MAC_ADD_FAILED,
f07e1c99 3087 "Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
3088 ipaddr2str(ip, buf2, sizeof(buf2)),
3089 prefix_mac2str(macaddr, buf, sizeof(buf)),
3090 ifp->name, ifp->ifindex, zvni->vni);
3091 return -1;
3092 }
3093 /* Set "local" forwarding info. */
3094 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
3095 n->ifindex = ifp->ifindex;
3096 } else {
ee69da27 3097 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
66e37987 3098 bool mac_different;
3099 bool cur_is_router;
3100
3101 /* Note any changes and see if of interest to BGP. */
3102 mac_different = (memcmp(n->emac.octet,
3103 macaddr->octet, ETH_ALEN) != 0) ? 1 : 0;
3104 cur_is_router = !!CHECK_FLAG(n->flags,
3105 ZEBRA_NEIGH_ROUTER_FLAG);
3106 if (!mac_different && is_router == cur_is_router) {
28bd0652
DS
3107 if (IS_ZEBRA_DEBUG_VXLAN)
3108 zlog_debug(
3109 "\tIgnoring entry mac is the same and is_router == cur_is_router");
ee69da27 3110 n->ifindex = ifp->ifindex;
66e37987 3111 return 0;
3112 }
ee69da27 3113
66e37987 3114 if (!mac_different) {
e22a946a
CS
3115 bool is_neigh_freezed = false;
3116
66e37987 3117 /* Only the router flag has changed. */
3118 if (is_router)
3119 SET_FLAG(n->flags,
3120 ZEBRA_NEIGH_ROUTER_FLAG);
3121 else
3122 UNSET_FLAG(n->flags,
3123 ZEBRA_NEIGH_ROUTER_FLAG);
ee69da27 3124
e22a946a
CS
3125 /* Neigh is in freeze state and freeze action
3126 * is enabled, do not send update to client.
3127 */
3128 is_neigh_freezed = (zvrf->dup_addr_detect &&
3129 zvrf->dad_freeze &&
3130 CHECK_FLAG(n->flags,
3131 ZEBRA_NEIGH_DUPLICATE));
3132
3133 if (IS_ZEBRA_NEIGH_ACTIVE(n) &&
3134 !is_neigh_freezed)
66e37987 3135 return zvni_neigh_send_add_to_client(
3136 zvni->vni, ip, macaddr,
3137 n->flags, n->loc_seq);
28bd0652
DS
3138 else {
3139 if (IS_ZEBRA_DEBUG_VXLAN)
3140 zlog_debug(
3141 "\tNeighbor active and frozen");
3142 }
66e37987 3143 return 0;
3144 }
3145
3146 /* The MAC has changed, need to issue a delete
3147 * first as this means a different MACIP route.
3148 * Also, need to do some unlinking/relinking.
3149 * We also need to update the MAC's sequence number
3150 * in different situations.
3151 */
3152 if (IS_ZEBRA_NEIGH_ACTIVE(n))
3153 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
2c476b72 3154 &n->emac, 0, n->state);
66e37987 3155 old_zmac = zvni_mac_lookup(zvni, &n->emac);
3156 if (old_zmac) {
3157 old_mac_seq = CHECK_FLAG(old_zmac->flags,
3158 ZEBRA_MAC_REMOTE) ?
3159 old_zmac->rem_seq : old_zmac->loc_seq;
3160 neigh_mac_change = upd_mac_seq = true;
3161 listnode_delete(old_zmac->neigh_list, n);
3162 zvni_deref_ip2mac(zvni, old_zmac);
29c2ce7c 3163 }
ee69da27 3164
66e37987 3165 /* Update the forwarding info. */
3166 n->ifindex = ifp->ifindex;
3167 memcpy(&n->emac, macaddr, ETH_ALEN);
3168
3169 /* Link to new MAC */
3170 listnode_add_sort(zmac->neigh_list, n);
f07e1c99 3171 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
3172 /*
3173 * Neighbor has moved from remote to local. Its
3174 * MAC could have also changed as part of the move.
3175 */
ee69da27
MK
3176 if (memcmp(n->emac.octet, macaddr->octet,
3177 ETH_ALEN) != 0) {
3178 old_zmac = zvni_mac_lookup(zvni, &n->emac);
3179 if (old_zmac) {
9df2b997 3180 old_mac_seq = CHECK_FLAG(
3181 old_zmac->flags,
3182 ZEBRA_MAC_REMOTE) ?
f07e1c99 3183 old_zmac->rem_seq :
3184 old_zmac->loc_seq;
3185 neigh_mac_change = upd_mac_seq = true;
9fc1522c
DS
3186 listnode_delete(old_zmac->neigh_list,
3187 n);
fe697c6b 3188 zvni_deref_ip2mac(zvni, old_zmac);
ee69da27
MK
3189 }
3190
3191 /* Link to new MAC */
3192 memcpy(&n->emac, macaddr, ETH_ALEN);
3193 listnode_add_sort(zmac->neigh_list, n);
3194 }
e22a946a
CS
3195 /* Based on Mobility event Scenario-B from the
3196 * draft, neigh's previous state was remote treat this
3197 * event for DAD.
3198 */
3199 neigh_was_remote = true;
3200 vtep_ip = n->r_vtep_ip;
ee69da27
MK
3201 /* Mark appropriately */
3202 UNSET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
3203 n->r_vtep_ip.s_addr = 0;
3204 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
3205 n->ifindex = ifp->ifindex;
3206 }
f07e1c99 3207 }
3208
3209 /* If MAC was previously remote, or the neighbor had a different
3210 * MAC earlier, recompute the sequence number.
3211 */
3212 if (upd_mac_seq) {
3213 uint32_t seq1, seq2;
3214
3215 seq1 = CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE) ?
3216 zmac->rem_seq + 1 : zmac->loc_seq;
3217 seq2 = neigh_mac_change ? old_mac_seq + 1 : 0;
3218 mac_new_seq = zmac->loc_seq < MAX(seq1, seq2) ?
3219 MAX(seq1, seq2) : zmac->loc_seq;
ee69da27
MK
3220 }
3221
e22a946a 3222 /* Mark Router flag (R-bit) */
a37f4598 3223 if (is_router)
54c17425
CS
3224 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
3225 else
3226 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
3227
e22a946a
CS
3228 /* Check old and/or new MAC detected as duplicate mark
3229 * the neigh as duplicate
3230 */
3231 if (zebra_vxlan_ip_inherit_dad_from_mac(zvrf, old_zmac, zmac, n)) {
3232 flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
3233 "VNI %u: MAC %s IP %s detected as duplicate during local update, inherit duplicate from MAC",
3234 zvni->vni,
3235 prefix_mac2str(macaddr, buf, sizeof(buf)),
3236 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
3237 }
3238
7510e459
CS
3239 /* For IP Duplicate Address Detection (DAD) is trigger,
3240 * when the event is extended mobility based on scenario-B
3241 * from the draft, IP/Neigh's MAC binding changed and
3242 * neigh's previous state was remote.
e22a946a 3243 */
7510e459
CS
3244 if (neigh_mac_change && neigh_was_remote)
3245 do_dad = true;
e22a946a 3246
7510e459
CS
3247 zebra_vxlan_dup_addr_detect_for_neigh(zvrf, n, vtep_ip, do_dad,
3248 &neigh_on_hold, true);
c80a972c 3249
ee69da27 3250 /* Before we program this in BGP, we need to check if MAC is locally
f07e1c99 3251 * learnt. If not, force neighbor to be inactive and reset its seq.
ee69da27
MK
3252 */
3253 if (!CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL)) {
8b44d564 3254 ZEBRA_NEIGH_SET_INACTIVE(n);
f07e1c99 3255 n->loc_seq = 0;
3256 zmac->loc_seq = mac_new_seq;
ee69da27
MK
3257 return 0;
3258 }
3259
f07e1c99 3260 /* If the MAC's sequence number has changed, inform the MAC and all
3261 * neighbors associated with the MAC to BGP, else just inform this
3262 * neighbor.
3263 */
3264 if (upd_mac_seq && zmac->loc_seq != mac_new_seq) {
29c2ce7c 3265 if (IS_ZEBRA_DEBUG_VXLAN)
f07e1c99 3266 zlog_debug("Seq changed for MAC %s VNI %u - old %u new %u",
3267 prefix_mac2str(macaddr, buf, sizeof(buf)),
3268 zvni->vni, zmac->loc_seq, mac_new_seq);
3269 zmac->loc_seq = mac_new_seq;
3270 if (zvni_mac_send_add_to_client(zvni->vni, macaddr,
3271 zmac->flags, zmac->loc_seq))
3272 return -1;
3273 zvni_process_neigh_on_local_mac_change(zvni, zmac, 1);
29c2ce7c
CS
3274 return 0;
3275 }
3276
f07e1c99 3277 n->loc_seq = zmac->loc_seq;
ee69da27 3278
c34e362b
CS
3279 if (!neigh_on_hold) {
3280 ZEBRA_NEIGH_SET_ACTIVE(n);
3281
e22a946a 3282 return zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
f07e1c99 3283 n->flags, n->loc_seq);
c34e362b 3284 } else {
28bd0652
DS
3285 if (IS_ZEBRA_DEBUG_VXLAN)
3286 zlog_debug("\tNeighbor on hold not sending");
3287 }
e22a946a 3288 return 0;
ee69da27
MK
3289}
3290
3291static int zvni_remote_neigh_update(zebra_vni_t *zvni,
3292 struct interface *ifp,
3293 struct ipaddr *ip,
3294 struct ethaddr *macaddr,
3295 uint16_t state)
3296{
3297 char buf[ETHER_ADDR_STRLEN];
3298 char buf2[INET6_ADDRSTRLEN];
3299 zebra_neigh_t *n = NULL;
3300 zebra_mac_t *zmac = NULL;
3301
3302 /* If the neighbor is unknown, there is no further action. */
3303 n = zvni_neigh_lookup(zvni, ip);
3304 if (!n)
3305 return 0;
3306
3307 /* If a remote entry, see if it needs to be refreshed */
3308 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
e9d2cbde 3309#ifdef GNU_LINUX
ee69da27
MK
3310 if (state & NUD_STALE)
3311 zvni_neigh_install(zvni, n);
e9d2cbde 3312#endif
ee69da27
MK
3313 } else {
3314 /* We got a "remote" neighbor notification for an entry
3315 * we think is local. This can happen in a multihoming
3316 * scenario - but only if the MAC is already "remote".
3317 * Just mark our entry as "remote".
3318 */
3319 zmac = zvni_mac_lookup(zvni, macaddr);
3320 if (!zmac || !CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
9df414fe 3321 zlog_debug(
43e52561
QY
3322 "Ignore remote neigh %s (MAC %s) on L2-VNI %u - MAC unknown or local",
3323 ipaddr2str(&n->ip, buf2, sizeof(buf2)),
3324 prefix_mac2str(macaddr, buf, sizeof(buf)),
3325 zvni->vni);
ee69da27
MK
3326 return -1;
3327 }
3328
3329 UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
3330 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
4fd5ea4b 3331 ZEBRA_NEIGH_SET_ACTIVE(n);
ee69da27
MK
3332 n->r_vtep_ip = zmac->fwd_info.r_vtep_ip;
3333 }
3334
3335 return 0;
3336}
3337
2232a77c 3338/*
3339 * Make hash key for MAC.
3340 */
d8b87afe 3341static unsigned int mac_hash_keymake(const void *p)
2232a77c 3342{
d8b87afe 3343 const zebra_mac_t *pmac = p;
25331def
DS
3344 const void *pnt = (void *)pmac->macaddr.octet;
3345
ff8b7eb8 3346 return jhash(pnt, ETH_ALEN, 0xa5a5a55a);
2232a77c 3347}
13d60d35 3348
2232a77c 3349/*
3350 * Compare two MAC addresses.
3351 */
74df8d6d 3352static bool mac_cmp(const void *p1, const void *p2)
2232a77c 3353{
d62a17ae 3354 const zebra_mac_t *pmac1 = p1;
3355 const zebra_mac_t *pmac2 = p2;
2232a77c 3356
d62a17ae 3357 if (pmac1 == NULL && pmac2 == NULL)
74df8d6d 3358 return true;
2232a77c 3359
d62a17ae 3360 if (pmac1 == NULL || pmac2 == NULL)
74df8d6d 3361 return false;
2232a77c 3362
996c9314 3363 return (memcmp(pmac1->macaddr.octet, pmac2->macaddr.octet, ETH_ALEN)
d62a17ae 3364 == 0);
2232a77c 3365}
3366
3367/*
3368 * Callback to allocate MAC hash entry.
3369 */
d62a17ae 3370static void *zvni_mac_alloc(void *p)
2232a77c 3371{
d62a17ae 3372 const zebra_mac_t *tmp_mac = p;
3373 zebra_mac_t *mac;
2232a77c 3374
d62a17ae 3375 mac = XCALLOC(MTYPE_MAC, sizeof(zebra_mac_t));
3376 *mac = *tmp_mac;
2232a77c 3377
d62a17ae 3378 return ((void *)mac);
2232a77c 3379}
3380
3381/*
3382 * Add MAC entry.
3383 */
d62a17ae 3384static zebra_mac_t *zvni_mac_add(zebra_vni_t *zvni, struct ethaddr *macaddr)
2232a77c 3385{
d62a17ae 3386 zebra_mac_t tmp_mac;
3387 zebra_mac_t *mac = NULL;
2232a77c 3388
d62a17ae 3389 memset(&tmp_mac, 0, sizeof(zebra_mac_t));
ff8b7eb8 3390 memcpy(&tmp_mac.macaddr, macaddr, ETH_ALEN);
d62a17ae 3391 mac = hash_get(zvni->mac_table, &tmp_mac, zvni_mac_alloc);
3392 assert(mac);
2232a77c 3393
55328d8a
CS
3394 mac->zvni = zvni;
3395 mac->dad_mac_auto_recovery_timer = NULL;
3396
b6938a74 3397 mac->neigh_list = list_new();
27fa3398 3398 mac->neigh_list->cmp = neigh_list_cmp;
b6938a74 3399
d62a17ae 3400 return mac;
2232a77c 3401}
3402
3403/*
3404 * Delete MAC entry.
3405 */
d62a17ae 3406static int zvni_mac_del(zebra_vni_t *zvni, zebra_mac_t *mac)
2232a77c 3407{
d62a17ae 3408 zebra_mac_t *tmp_mac;
2232a77c 3409
55328d8a
CS
3410 /* Cancel auto recovery */
3411 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
3412
6a154c88 3413 list_delete(&mac->neigh_list);
b6938a74 3414
d62a17ae 3415 /* Free the VNI hash entry and allocated memory. */
3416 tmp_mac = hash_release(zvni->mac_table, mac);
0a22ddfb 3417 XFREE(MTYPE_MAC, tmp_mac);
2232a77c 3418
d62a17ae 3419 return 0;
2232a77c 3420}
3421
6041b686
CS
3422static bool zvni_check_mac_del_from_db(struct mac_walk_ctx *wctx,
3423 zebra_mac_t *mac)
3424{
3425 if ((wctx->flags & DEL_LOCAL_MAC) &&
3426 (mac->flags & ZEBRA_MAC_LOCAL))
3427 return true;
3428 else if ((wctx->flags & DEL_REMOTE_MAC) &&
3429 (mac->flags & ZEBRA_MAC_REMOTE))
3430 return true;
3431 else if ((wctx->flags & DEL_REMOTE_MAC_FROM_VTEP) &&
3432 (mac->flags & ZEBRA_MAC_REMOTE) &&
3433 IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &wctx->r_vtep_ip))
3434 return true;
3435 else if ((wctx->flags & DEL_LOCAL_MAC) &&
3436 (mac->flags & ZEBRA_MAC_AUTO) &&
3437 !listcount(mac->neigh_list)) {
3438 if (IS_ZEBRA_DEBUG_VXLAN) {
3439 char buf[ETHER_ADDR_STRLEN];
3440
3441 zlog_debug("%s: Del MAC %s flags 0x%x",
3442 __PRETTY_FUNCTION__,
3443 prefix_mac2str(&mac->macaddr,
3444 buf, sizeof(buf)),
3445 mac->flags);
3446 }
3447 wctx->uninstall = 0;
3448
3449 return true;
3450 }
3451
3452 return false;
3453}
3454
2232a77c 3455/*
3456 * Free MAC hash entry (callback)
3457 */
e3b78da8 3458static void zvni_mac_del_hash_entry(struct hash_bucket *bucket, void *arg)
2232a77c 3459{
d62a17ae 3460 struct mac_walk_ctx *wctx = arg;
e3b78da8 3461 zebra_mac_t *mac = bucket->data;
2232a77c 3462
6041b686 3463 if (zvni_check_mac_del_from_db(wctx, mac)) {
d62a17ae 3464 if (wctx->upd_client && (mac->flags & ZEBRA_MAC_LOCAL)) {
996c9314 3465 zvni_mac_send_del_to_client(wctx->zvni->vni,
e98e4b88 3466 &mac->macaddr);
d62a17ae 3467 }
d62a17ae 3468 if (wctx->uninstall)
d63c1b18 3469 zvni_mac_uninstall(wctx->zvni, mac);
2232a77c 3470
b1599bb6 3471 zvni_mac_del(wctx->zvni, mac);
d62a17ae 3472 }
2232a77c 3473
b1599bb6 3474 return;
2232a77c 3475}
3476
3477/*
3478 * Delete all MAC entries from specific VTEP for a particular VNI.
3479 */
d62a17ae 3480static void zvni_mac_del_from_vtep(zebra_vni_t *zvni, int uninstall,
3481 struct in_addr *r_vtep_ip)
2232a77c 3482{
d62a17ae 3483 struct mac_walk_ctx wctx;
2232a77c 3484
d62a17ae 3485 if (!zvni->mac_table)
3486 return;
2232a77c 3487
d62a17ae 3488 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
3489 wctx.zvni = zvni;
3490 wctx.uninstall = uninstall;
3491 wctx.flags = DEL_REMOTE_MAC_FROM_VTEP;
3492 wctx.r_vtep_ip = *r_vtep_ip;
2232a77c 3493
b1599bb6 3494 hash_iterate(zvni->mac_table, zvni_mac_del_hash_entry, &wctx);
2232a77c 3495}
3496
3497/*
3498 * Delete all MAC entries for this VNI.
3499 */
996c9314 3500static void zvni_mac_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
d7c0a89a 3501 uint32_t flags)
2232a77c 3502{
d62a17ae 3503 struct mac_walk_ctx wctx;
2232a77c 3504
d62a17ae 3505 if (!zvni->mac_table)
3506 return;
2232a77c 3507
d62a17ae 3508 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
3509 wctx.zvni = zvni;
d62a17ae 3510 wctx.uninstall = uninstall;
3511 wctx.upd_client = upd_client;
3512 wctx.flags = flags;
2232a77c 3513
b1599bb6 3514 hash_iterate(zvni->mac_table, zvni_mac_del_hash_entry, &wctx);
2232a77c 3515}
3516
3517/*
3518 * Look up MAC hash entry.
3519 */
d62a17ae 3520static zebra_mac_t *zvni_mac_lookup(zebra_vni_t *zvni, struct ethaddr *mac)
2232a77c 3521{
d62a17ae 3522 zebra_mac_t tmp;
3523 zebra_mac_t *pmac;
2232a77c 3524
d62a17ae 3525 memset(&tmp, 0, sizeof(tmp));
ff8b7eb8 3526 memcpy(&tmp.macaddr, mac, ETH_ALEN);
d62a17ae 3527 pmac = hash_lookup(zvni->mac_table, &tmp);
2232a77c 3528
d62a17ae 3529 return pmac;
2232a77c 3530}
3531
3532/*
3533 * Inform BGP about local MAC addition.
3534 */
996c9314 3535static int zvni_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr,
f07e1c99 3536 uint8_t mac_flags, uint32_t seq)
2232a77c 3537{
d7c0a89a 3538 uint8_t flags = 0;
ead40654
MK
3539
3540 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_STICKY))
3541 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
3542 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_DEF_GW))
3543 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
3544
2853fed6 3545 return zvni_macip_send_msg_to_client(vni, macaddr, NULL, flags,
2c476b72 3546 seq, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_ADD);
2232a77c 3547}
3548
3549/*
3550 * Inform BGP about local MAC deletion.
3551 */
e98e4b88 3552static int zvni_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr)
2232a77c 3553{
e98e4b88 3554 return zvni_macip_send_msg_to_client(vni, macaddr, NULL, 0 /* flags */,
2c476b72 3555 0 /* seq */, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_DEL);
2232a77c 3556}
3557
3558/*
3559 * Map port or (port, VLAN) to a VNI. This is invoked upon getting MAC
2853fed6 3560 * notifications, to see if they are of interest.
2232a77c 3561 */
d62a17ae 3562static zebra_vni_t *zvni_map_vlan(struct interface *ifp,
3563 struct interface *br_if, vlanid_t vid)
2232a77c 3564{
2853fed6 3565 struct zebra_ns *zns;
3566 struct route_node *rn;
3567 struct interface *tmp_if = NULL;
d62a17ae 3568 struct zebra_if *zif;
3569 struct zebra_l2info_bridge *br;
2853fed6 3570 struct zebra_l2info_vxlan *vxl = NULL;
d7c0a89a 3571 uint8_t bridge_vlan_aware;
d62a17ae 3572 zebra_vni_t *zvni;
2853fed6 3573 int found = 0;
2232a77c 3574
d62a17ae 3575 /* Determine if bridge is VLAN-aware or not */
3576 zif = br_if->info;
3577 assert(zif);
3578 br = &zif->l2info.br;
3579 bridge_vlan_aware = br->vlan_aware;
2232a77c 3580
d62a17ae 3581 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
3582 /* TODO: Optimize with a hash. */
2853fed6 3583 zns = zebra_ns_lookup(NS_DEFAULT);
3584 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3585 tmp_if = (struct interface *)rn->info;
3586 if (!tmp_if)
3587 continue;
d62a17ae 3588 zif = tmp_if->info;
3589 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
3590 continue;
3591 if (!if_is_operative(tmp_if))
3592 continue;
3593 vxl = &zif->l2info.vxl;
2232a77c 3594
d62a17ae 3595 if (zif->brslave_info.br_if != br_if)
3596 continue;
2232a77c 3597
2853fed6 3598 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
3599 found = 1;
d62a17ae 3600 break;
2853fed6 3601 }
d62a17ae 3602 }
2232a77c 3603
2853fed6 3604 if (!found)
d62a17ae 3605 return NULL;
2232a77c 3606
2853fed6 3607 zvni = zvni_lookup(vxl->vni);
d62a17ae 3608 return zvni;
2232a77c 3609}
3610
3611/*
3612 * Map SVI and associated bridge to a VNI. This is invoked upon getting
3613 * neighbor notifications, to see if they are of interest.
2232a77c 3614 */
b7cfce93
MK
3615static zebra_vni_t *zvni_from_svi(struct interface *ifp,
3616 struct interface *br_if)
d62a17ae 3617{
2853fed6 3618 struct zebra_ns *zns;
3619 struct route_node *rn;
3620 struct interface *tmp_if = NULL;
d62a17ae 3621 struct zebra_if *zif;
3622 struct zebra_l2info_bridge *br;
2853fed6 3623 struct zebra_l2info_vxlan *vxl = NULL;
d7c0a89a 3624 uint8_t bridge_vlan_aware;
d62a17ae 3625 vlanid_t vid = 0;
3626 zebra_vni_t *zvni;
2853fed6 3627 int found = 0;
d62a17ae 3628
71349e03
MK
3629 if (!br_if)
3630 return NULL;
3631
d62a17ae 3632 /* Make sure the linked interface is a bridge. */
3633 if (!IS_ZEBRA_IF_BRIDGE(br_if))
3634 return NULL;
3635
d62a17ae 3636 /* Determine if bridge is VLAN-aware or not */
3637 zif = br_if->info;
3638 assert(zif);
3639 br = &zif->l2info.br;
3640 bridge_vlan_aware = br->vlan_aware;
3641 if (bridge_vlan_aware) {
3642 struct zebra_l2info_vlan *vl;
3643
3644 if (!IS_ZEBRA_IF_VLAN(ifp))
3645 return NULL;
3646
3647 zif = ifp->info;
3648 assert(zif);
3649 vl = &zif->l2info.vl;
3650 vid = vl->vid;
3651 }
3652
3653 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
3654 /* TODO: Optimize with a hash. */
2853fed6 3655 zns = zebra_ns_lookup(NS_DEFAULT);
3656 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3657 tmp_if = (struct interface *)rn->info;
3658 if (!tmp_if)
3659 continue;
d62a17ae 3660 zif = tmp_if->info;
3661 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
3662 continue;
3663 if (!if_is_operative(tmp_if))
3664 continue;
3665 vxl = &zif->l2info.vxl;
3666
3667 if (zif->brslave_info.br_if != br_if)
3668 continue;
3669
2853fed6 3670 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
3671 found = 1;
d62a17ae 3672 break;
2853fed6 3673 }
d62a17ae 3674 }
3675
2853fed6 3676 if (!found)
d62a17ae 3677 return NULL;
3678
2853fed6 3679 zvni = zvni_lookup(vxl->vni);
d62a17ae 3680 return zvni;
2232a77c 3681}
3682
3683/* Map to SVI on bridge corresponding to specified VLAN. This can be one
3684 * of two cases:
3685 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN interface
3686 * linked to the bridge
3687 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge inteface
3688 * itself
3689 */
2853fed6 3690static struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if)
d62a17ae 3691{
2853fed6 3692 struct zebra_ns *zns;
3693 struct route_node *rn;
3694 struct interface *tmp_if = NULL;
d62a17ae 3695 struct zebra_if *zif;
3696 struct zebra_l2info_bridge *br;
3697 struct zebra_l2info_vlan *vl;
d7c0a89a 3698 uint8_t bridge_vlan_aware;
2853fed6 3699 int found = 0;
d62a17ae 3700
b5ebdc9b 3701 /* Defensive check, caller expected to invoke only with valid bridge. */
3702 if (!br_if)
3703 return NULL;
3704
d62a17ae 3705 /* Determine if bridge is VLAN-aware or not */
3706 zif = br_if->info;
3707 assert(zif);
3708 br = &zif->l2info.br;
3709 bridge_vlan_aware = br->vlan_aware;
3710
3711 /* Check oper status of the SVI. */
3712 if (!bridge_vlan_aware)
3713 return if_is_operative(br_if) ? br_if : NULL;
3714
3715 /* Identify corresponding VLAN interface. */
3716 /* TODO: Optimize with a hash. */
2853fed6 3717 zns = zebra_ns_lookup(NS_DEFAULT);
3718 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3719 tmp_if = (struct interface *)rn->info;
d62a17ae 3720 /* Check oper status of the SVI. */
2853fed6 3721 if (!tmp_if || !if_is_operative(tmp_if))
d62a17ae 3722 continue;
3723 zif = tmp_if->info;
3724 if (!zif || zif->zif_type != ZEBRA_IF_VLAN
3725 || zif->link != br_if)
3726 continue;
3727 vl = (struct zebra_l2info_vlan *)&zif->l2info.vl;
3728
2853fed6 3729 if (vl->vid == vid) {
3730 found = 1;
d62a17ae 3731 break;
2853fed6 3732 }
d62a17ae 3733 }
3734
2853fed6 3735 return found ? tmp_if : NULL;
2232a77c 3736}
3737
3738/*
036d93c0 3739 * Install remote MAC into the forwarding plane.
2232a77c 3740 */
d62a17ae 3741static int zvni_mac_install(zebra_vni_t *zvni, zebra_mac_t *mac)
2232a77c 3742{
478566d6
MS
3743 const struct zebra_if *zif, *br_zif;
3744 const struct zebra_l2info_vxlan *vxl;
a37f4598 3745 bool sticky;
036d93c0 3746 enum zebra_dplane_result res;
478566d6
MS
3747 const struct interface *br_ifp;
3748 vlanid_t vid;
2232a77c 3749
d62a17ae 3750 if (!(mac->flags & ZEBRA_MAC_REMOTE))
3751 return 0;
2232a77c 3752
d62a17ae 3753 zif = zvni->vxlan_if->info;
3754 if (!zif)
3755 return -1;
478566d6
MS
3756
3757 br_ifp = zif->brslave_info.br_if;
3758 if (br_ifp == NULL)
3759 return -1;
3760
d62a17ae 3761 vxl = &zif->l2info.vxl;
2232a77c 3762
a37f4598 3763 sticky = !!CHECK_FLAG(mac->flags,
3764 (ZEBRA_MAC_STICKY | ZEBRA_MAC_REMOTE_DEF_GW));
c85c03c7 3765
478566d6
MS
3766 br_zif = (const struct zebra_if *)(br_ifp->info);
3767
3768 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
3769 vid = vxl->access_vlan;
3770 else
3771 vid = 0;
3772
3773 res = dplane_mac_add(zvni->vxlan_if, br_ifp, vid,
3774 &mac->macaddr, mac->fwd_info.r_vtep_ip, sticky);
036d93c0
MS
3775 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
3776 return 0;
3777 else
3778 return -1;
2232a77c 3779}
3780
3781/*
036d93c0 3782 * Uninstall remote MAC from the forwarding plane.
2232a77c 3783 */
d63c1b18 3784static int zvni_mac_uninstall(zebra_vni_t *zvni, zebra_mac_t *mac)
2232a77c 3785{
478566d6
MS
3786 const struct zebra_if *zif, *br_zif;
3787 const struct zebra_l2info_vxlan *vxl;
98efddf1 3788 struct in_addr vtep_ip;
478566d6
MS
3789 const struct interface *ifp, *br_ifp;
3790 vlanid_t vid;
036d93c0 3791 enum zebra_dplane_result res;
2232a77c 3792
d63c1b18 3793 if (!(mac->flags & ZEBRA_MAC_REMOTE))
d62a17ae 3794 return 0;
2232a77c 3795
d62a17ae 3796 if (!zvni->vxlan_if) {
e0e140a7
MS
3797 if (IS_ZEBRA_DEBUG_VXLAN)
3798 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
3799 zvni->vni, zvni);
d62a17ae 3800 return -1;
3801 }
2232a77c 3802
d62a17ae 3803 zif = zvni->vxlan_if->info;
3804 if (!zif)
3805 return -1;
478566d6
MS
3806
3807 br_ifp = zif->brslave_info.br_if;
3808 if (br_ifp == NULL)
3809 return -1;
3810
d62a17ae 3811 vxl = &zif->l2info.vxl;
2232a77c 3812
478566d6
MS
3813 br_zif = (const struct zebra_if *)br_ifp->info;
3814
3815 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
3816 vid = vxl->access_vlan;
3817 else
3818 vid = 0;
3819
d63c1b18 3820 ifp = zvni->vxlan_if;
3821 vtep_ip = mac->fwd_info.r_vtep_ip;
2232a77c 3822
478566d6 3823 res = dplane_mac_del(ifp, br_ifp, vid, &mac->macaddr, vtep_ip);
036d93c0
MS
3824 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
3825 return 0;
3826 else
3827 return -1;
2232a77c 3828}
3829
3830/*
3831 * Install MAC hash entry - called upon access VLAN change.
3832 */
e3b78da8 3833static void zvni_install_mac_hash(struct hash_bucket *bucket, void *ctxt)
2232a77c 3834{
d62a17ae 3835 zebra_mac_t *mac;
3836 struct mac_walk_ctx *wctx = ctxt;
2232a77c 3837
e3b78da8 3838 mac = (zebra_mac_t *)bucket->data;
2232a77c 3839
d62a17ae 3840 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
3841 zvni_mac_install(wctx->zvni, mac);
2232a77c 3842}
3843
fe697c6b 3844/*
3845 * Count of remote neighbors referencing this MAC.
3846 */
3847static int remote_neigh_count(zebra_mac_t *zmac)
3848{
3849 zebra_neigh_t *n = NULL;
3850 struct listnode *node = NULL;
3851 int count = 0;
3852
3853 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
3854 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
3855 count++;
3856 }
3857
3858 return count;
3859}
3860
2232a77c 3861/*
3862 * Decrement neighbor refcount of MAC; uninstall and free it if
3863 * appropriate.
3864 */
fe697c6b 3865static void zvni_deref_ip2mac(zebra_vni_t *zvni, zebra_mac_t *mac)
2232a77c 3866{
fe697c6b 3867 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
d62a17ae 3868 return;
2232a77c 3869
fe697c6b 3870 /* If all remote neighbors referencing a remote MAC go away,
3871 * we need to uninstall the MAC.
3872 */
3873 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) &&
3874 remote_neigh_count(mac) == 0) {
d63c1b18 3875 zvni_mac_uninstall(zvni, mac);
fe697c6b 3876 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
3877 }
2232a77c 3878
fe697c6b 3879 /* If no neighbors, delete the MAC. */
3880 if (list_isempty(mac->neigh_list))
3881 zvni_mac_del(zvni, mac);
2232a77c 3882}
3883
3884/*
3885 * Read and populate local MACs and neighbors corresponding to this VNI.
3886 */
996c9314 3887static void zvni_read_mac_neigh(zebra_vni_t *zvni, struct interface *ifp)
2232a77c 3888{
2853fed6 3889 struct zebra_ns *zns;
d62a17ae 3890 struct zebra_if *zif;
3891 struct interface *vlan_if;
3892 struct zebra_l2info_vxlan *vxl;
1a98c087 3893 struct interface *vrr_if;
2232a77c 3894
d62a17ae 3895 zif = ifp->info;
3896 vxl = &zif->l2info.vxl;
2853fed6 3897 zns = zebra_ns_lookup(NS_DEFAULT);
2232a77c 3898
d62a17ae 3899 if (IS_ZEBRA_DEBUG_VXLAN)
3900 zlog_debug(
2853fed6 3901 "Reading MAC FDB and Neighbors for intf %s(%u) VNI %u master %u",
3902 ifp->name, ifp->ifindex, zvni->vni,
d62a17ae 3903 zif->brslave_info.bridge_ifindex);
2232a77c 3904
2853fed6 3905 macfdb_read_for_bridge(zns, ifp, zif->brslave_info.br_if);
3906 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
1a98c087
MK
3907 if (vlan_if) {
3908
ead40654
MK
3909 /* Add SVI MAC-IP */
3910 zvni_add_macip_for_intf(vlan_if, zvni);
1a98c087 3911
ead40654
MK
3912 /* Add VRR MAC-IP - if any*/
3913 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
3914 if (vrr_if)
3915 zvni_add_macip_for_intf(vrr_if, zvni);
1a98c087 3916
2853fed6 3917 neigh_read_for_vlan(zns, vlan_if);
1a98c087 3918 }
2232a77c 3919}
3920
3921/*
3922 * Hash function for VNI.
3923 */
d8b87afe 3924static unsigned int vni_hash_keymake(const void *p)
2232a77c 3925{
d62a17ae 3926 const zebra_vni_t *zvni = p;
2232a77c 3927
d62a17ae 3928 return (jhash_1word(zvni->vni, 0));
2232a77c 3929}
3930
3931/*
3932 * Compare 2 VNI hash entries.
3933 */
74df8d6d 3934static bool vni_hash_cmp(const void *p1, const void *p2)
2232a77c 3935{
d62a17ae 3936 const zebra_vni_t *zvni1 = p1;
3937 const zebra_vni_t *zvni2 = p2;
2232a77c 3938
d62a17ae 3939 return (zvni1->vni == zvni2->vni);
2232a77c 3940}
3941
27fa3398
DS
3942static int vni_list_cmp(void *p1, void *p2)
3943{
3944 const zebra_vni_t *zvni1 = p1;
3945 const zebra_vni_t *zvni2 = p2;
3946
3947 if (zvni1->vni == zvni2->vni)
3948 return 0;
3949 return (zvni1->vni < zvni2->vni) ? -1 : 1;
3950}
3951
2232a77c 3952/*
3953 * Callback to allocate VNI hash entry.
3954 */
d62a17ae 3955static void *zvni_alloc(void *p)
2232a77c 3956{
d62a17ae 3957 const zebra_vni_t *tmp_vni = p;
3958 zebra_vni_t *zvni;
2232a77c 3959
d62a17ae 3960 zvni = XCALLOC(MTYPE_ZVNI, sizeof(zebra_vni_t));
3961 zvni->vni = tmp_vni->vni;
3962 return ((void *)zvni);
2232a77c 3963}
3964
3965/*
3966 * Look up VNI hash entry.
3967 */
2853fed6 3968static zebra_vni_t *zvni_lookup(vni_t vni)
2232a77c 3969{
2853fed6 3970 struct zebra_vrf *zvrf;
d62a17ae 3971 zebra_vni_t tmp_vni;
3972 zebra_vni_t *zvni = NULL;
2232a77c 3973
530db8dc 3974 zvrf = zebra_vrf_get_evpn();
2853fed6 3975 assert(zvrf);
d62a17ae 3976 memset(&tmp_vni, 0, sizeof(zebra_vni_t));
3977 tmp_vni.vni = vni;
3978 zvni = hash_lookup(zvrf->vni_table, &tmp_vni);
2232a77c 3979
d62a17ae 3980 return zvni;
2232a77c 3981}
3982
3983/*
3984 * Add VNI hash entry.
3985 */
2853fed6 3986static zebra_vni_t *zvni_add(vni_t vni)
2232a77c 3987{
2853fed6 3988 struct zebra_vrf *zvrf;
d62a17ae 3989 zebra_vni_t tmp_zvni;
3990 zebra_vni_t *zvni = NULL;
2232a77c 3991
530db8dc 3992 zvrf = zebra_vrf_get_evpn();
2853fed6 3993 assert(zvrf);
d62a17ae 3994 memset(&tmp_zvni, 0, sizeof(zebra_vni_t));
3995 tmp_zvni.vni = vni;
3996 zvni = hash_get(zvrf->vni_table, &tmp_zvni, zvni_alloc);
3997 assert(zvni);
2232a77c 3998
d62a17ae 3999 /* Create hash table for MAC */
4000 zvni->mac_table =
4001 hash_create(mac_hash_keymake, mac_cmp, "Zebra VNI MAC Table");
2232a77c 4002
d62a17ae 4003 /* Create hash table for neighbors */
4004 zvni->neigh_table = hash_create(neigh_hash_keymake, neigh_cmp,
4005 "Zebra VNI Neighbor Table");
2232a77c 4006
d62a17ae 4007 return zvni;
2232a77c 4008}
4009
4010/*
4011 * Delete VNI hash entry.
4012 */
2853fed6 4013static int zvni_del(zebra_vni_t *zvni)
2232a77c 4014{
2853fed6 4015 struct zebra_vrf *zvrf;
d62a17ae 4016 zebra_vni_t *tmp_zvni;
2232a77c 4017
530db8dc 4018 zvrf = zebra_vrf_get_evpn();
2853fed6 4019 assert(zvrf);
4020
d62a17ae 4021 zvni->vxlan_if = NULL;
2232a77c 4022
abfa0a96
AK
4023 /* Remove references to the BUM mcast grp */
4024 zebra_vxlan_sg_deref(zvni->local_vtep_ip, zvni->mcast_grp);
4025
d62a17ae 4026 /* Free the neighbor hash table. */
4027 hash_free(zvni->neigh_table);
4028 zvni->neigh_table = NULL;
2232a77c 4029
d62a17ae 4030 /* Free the MAC hash table. */
4031 hash_free(zvni->mac_table);
4032 zvni->mac_table = NULL;
2232a77c 4033
d62a17ae 4034 /* Free the VNI hash entry and allocated memory. */
4035 tmp_zvni = hash_release(zvrf->vni_table, zvni);
0a22ddfb 4036 XFREE(MTYPE_ZVNI, tmp_zvni);
2232a77c 4037
d62a17ae 4038 return 0;
2232a77c 4039}
4040
4041/*
4042 * Inform BGP about local VNI addition.
4043 */
2853fed6 4044static int zvni_send_add_to_client(zebra_vni_t *zvni)
2232a77c 4045{
d62a17ae 4046 struct zserv *client;
4047 struct stream *s;
2232a77c 4048
21ccc0cf 4049 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
d62a17ae 4050 /* BGP may not be running. */
4051 if (!client)
4052 return 0;
2232a77c 4053
1002497a 4054 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2232a77c 4055
43779a11 4056 zclient_create_header(s, ZEBRA_VNI_ADD, zebra_vrf_get_evpn_id());
d62a17ae 4057 stream_putl(s, zvni->vni);
4058 stream_put_in_addr(s, &zvni->local_vtep_ip);
b7cfce93 4059 stream_put(s, &zvni->vrf_id, sizeof(vrf_id_t)); /* tenant vrf */
39c46ff1 4060 stream_put_in_addr(s, &zvni->mcast_grp);
2232a77c 4061
d62a17ae 4062 /* Write packet size. */
4063 stream_putw_at(s, 0, stream_get_endp(s));
2232a77c 4064
d62a17ae 4065 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
4066 zlog_debug("Send VNI_ADD %u %s tenant vrf %s to %s", zvni->vni,
4067 inet_ntoa(zvni->local_vtep_ip),
b7cfce93 4068 vrf_id_to_name(zvni->vrf_id),
d62a17ae 4069 zebra_route_string(client->proto));
2232a77c 4070
d62a17ae 4071 client->vniadd_cnt++;
21ccc0cf 4072 return zserv_send_message(client, s);
2232a77c 4073}
4074
4075/*
4076 * Inform BGP about local VNI deletion.
4077 */
2853fed6 4078static int zvni_send_del_to_client(vni_t vni)
2232a77c 4079{
d62a17ae 4080 struct zserv *client;
4081 struct stream *s;
2232a77c 4082
21ccc0cf 4083 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
d62a17ae 4084 /* BGP may not be running. */
4085 if (!client)
4086 return 0;
2232a77c 4087
1002497a 4088 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
d62a17ae 4089 stream_reset(s);
2232a77c 4090
43779a11 4091 zclient_create_header(s, ZEBRA_VNI_DEL, zebra_vrf_get_evpn_id());
d62a17ae 4092 stream_putl(s, vni);
2232a77c 4093
d62a17ae 4094 /* Write packet size. */
4095 stream_putw_at(s, 0, stream_get_endp(s));
2232a77c 4096
d62a17ae 4097 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 4098 zlog_debug("Send VNI_DEL %u to %s", vni,
d62a17ae 4099 zebra_route_string(client->proto));
2232a77c 4100
d62a17ae 4101 client->vnidel_cnt++;
21ccc0cf 4102 return zserv_send_message(client, s);
2232a77c 4103}
4104
4105/*
4106 * Build the VNI hash table by going over the VxLAN interfaces. This
4107 * is called when EVPN (advertise-all-vni) is enabled.
4108 */
4d762f26 4109static void zvni_build_hash_table(void)
2232a77c 4110{
2853fed6 4111 struct zebra_ns *zns;
4112 struct route_node *rn;
d62a17ae 4113 struct interface *ifp;
2232a77c 4114
d62a17ae 4115 /* Walk VxLAN interfaces and create VNI hash. */
2853fed6 4116 zns = zebra_ns_lookup(NS_DEFAULT);
4117 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
b7cfce93 4118 vni_t vni;
643215ce 4119 zebra_vni_t *zvni = NULL;
4120 zebra_l3vni_t *zl3vni = NULL;
d62a17ae 4121 struct zebra_if *zif;
4122 struct zebra_l2info_vxlan *vxl;
2232a77c 4123
2853fed6 4124 ifp = (struct interface *)rn->info;
4125 if (!ifp)
4126 continue;
d62a17ae 4127 zif = ifp->info;
4128 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
4129 continue;
2232a77c 4130
b7cfce93 4131 vxl = &zif->l2info.vxl;
d62a17ae 4132 vni = vxl->vni;
2232a77c 4133
643215ce 4134 /* L3-VNI and L2-VNI are handled seperately */
4135 zl3vni = zl3vni_lookup(vni);
4136 if (zl3vni) {
2232a77c 4137
b7cfce93 4138 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
4139 zlog_debug(
4140 "create L3-VNI hash for Intf %s(%u) L3-VNI %u",
4141 ifp->name, ifp->ifindex, vni);
2232a77c 4142
b7cfce93 4143 /* associate with vxlan_if */
b67a60d2 4144 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93 4145 zl3vni->vxlan_if = ifp;
2232a77c 4146
523cafc4 4147 /*
4148 * we need to associate with SVI.
b7cfce93 4149 * we can associate with svi-if only after association
523cafc4 4150 * with vxlan-intf is complete
4151 */
b7cfce93
MK
4152 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
4153
4154 if (is_l3vni_oper_up(zl3vni))
4155 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4156
4157 } else {
b7cfce93
MK
4158 struct interface *vlan_if = NULL;
4159
4160 if (IS_ZEBRA_DEBUG_VXLAN)
4161 zlog_debug(
4162 "Create L2-VNI hash for intf %s(%u) L2-VNI %u local IP %s",
4163 ifp->name, ifp->ifindex, vni,
4164 inet_ntoa(vxl->vtep_ip));
4165
27627f9a 4166 /* VNI hash entry is expected to exist, if the BGP process is killed */
b7cfce93
MK
4167 zvni = zvni_lookup(vni);
4168 if (zvni) {
9df414fe 4169 zlog_debug(
b7cfce93
MK
4170 "VNI hash already present for IF %s(%u) L2-VNI %u",
4171 ifp->name, ifp->ifindex, vni);
b7cfce93 4172
27627f9a
KA
4173 /*
4174 * Inform BGP if intf is up and mapped to
4175 * bridge.
4176 */
4177 if (if_is_operative(ifp) &&
4178 zif->brslave_info.br_if)
4179 zvni_send_add_to_client(zvni);
b7cfce93 4180
27627f9a
KA
4181 /* Send Local MAC-entries to client */
4182 zvni_send_mac_to_client(zvni);
4183
4184 /* Send Loval Neighbor entries to client */
4185 zvni_send_neigh_to_client(zvni);
4186 } else {
4187 zvni = zvni_add(vni);
4188 if (!zvni) {
4189 zlog_debug(
4190 "Failed to add VNI hash, IF %s(%u) L2-VNI %u",
4191 ifp->name, ifp->ifindex, vni);
4192 return;
4193 }
b7cfce93 4194
27627f9a
KA
4195 if (zvni->local_vtep_ip.s_addr !=
4196 vxl->vtep_ip.s_addr ||
4197 zvni->mcast_grp.s_addr !=
4198 vxl->mcast_grp.s_addr) {
4199 zebra_vxlan_sg_deref(
4200 zvni->local_vtep_ip,
4201 zvni->mcast_grp);
4202 zebra_vxlan_sg_ref(vxl->vtep_ip,
4203 vxl->mcast_grp);
4204 zvni->local_vtep_ip = vxl->vtep_ip;
4205 zvni->mcast_grp = vxl->mcast_grp;
4206 }
4207 zvni->vxlan_if = ifp;
4208 vlan_if = zvni_map_to_svi(vxl->access_vlan,
4209 zif->brslave_info.br_if);
4210 if (vlan_if) {
4211 zvni->vrf_id = vlan_if->vrf_id;
4212 zl3vni = zl3vni_from_vrf(
4213 vlan_if->vrf_id);
4214 if (zl3vni)
4215 listnode_add_sort(
4216 zl3vni->l2vnis, zvni);
4217 }
b7cfce93 4218
27627f9a
KA
4219 /*
4220 * Inform BGP if intf is up and mapped to
4221 * bridge.
4222 */
4223 if (if_is_operative(ifp) &&
4224 zif->brslave_info.br_if)
4225 zvni_send_add_to_client(zvni);
4226 }
b7cfce93 4227 }
d62a17ae 4228 }
2232a77c 4229}
4230
4231/*
4232 * See if remote VTEP matches with prefix.
4233 */
d62a17ae 4234static int zvni_vtep_match(struct in_addr *vtep_ip, zebra_vtep_t *zvtep)
2232a77c 4235{
d62a17ae 4236 return (IPV4_ADDR_SAME(vtep_ip, &zvtep->vtep_ip));
2232a77c 4237}
4238
4239/*
4240 * Locate remote VTEP in VNI hash table.
4241 */
d62a17ae 4242static zebra_vtep_t *zvni_vtep_find(zebra_vni_t *zvni, struct in_addr *vtep_ip)
2232a77c 4243{
d62a17ae 4244 zebra_vtep_t *zvtep;
2232a77c 4245
d62a17ae 4246 if (!zvni)
4247 return NULL;
2232a77c 4248
d62a17ae 4249 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
4250 if (zvni_vtep_match(vtep_ip, zvtep))
4251 break;
4252 }
2232a77c 4253
d62a17ae 4254 return zvtep;
2232a77c 4255}
4256
4257/*
4258 * Add remote VTEP to VNI hash table.
4259 */
9718c54e
AK
4260static zebra_vtep_t *zvni_vtep_add(zebra_vni_t *zvni, struct in_addr *vtep_ip,
4261 int flood_control)
4262
2232a77c 4263{
d62a17ae 4264 zebra_vtep_t *zvtep;
2232a77c 4265
d62a17ae 4266 zvtep = XCALLOC(MTYPE_ZVNI_VTEP, sizeof(zebra_vtep_t));
2232a77c 4267
d62a17ae 4268 zvtep->vtep_ip = *vtep_ip;
9718c54e 4269 zvtep->flood_control = flood_control;
2232a77c 4270
d62a17ae 4271 if (zvni->vteps)
4272 zvni->vteps->prev = zvtep;
4273 zvtep->next = zvni->vteps;
4274 zvni->vteps = zvtep;
2232a77c 4275
d62a17ae 4276 return zvtep;
2232a77c 4277}
4278
4279/*
4280 * Remove remote VTEP from VNI hash table.
4281 */
d62a17ae 4282static int zvni_vtep_del(zebra_vni_t *zvni, zebra_vtep_t *zvtep)
2232a77c 4283{
d62a17ae 4284 if (zvtep->next)
4285 zvtep->next->prev = zvtep->prev;
4286 if (zvtep->prev)
4287 zvtep->prev->next = zvtep->next;
4288 else
4289 zvni->vteps = zvtep->next;
2232a77c 4290
d62a17ae 4291 zvtep->prev = zvtep->next = NULL;
4292 XFREE(MTYPE_ZVNI_VTEP, zvtep);
2232a77c 4293
d62a17ae 4294 return 0;
2232a77c 4295}
4296
4297/*
4298 * Delete all remote VTEPs for this VNI (upon VNI delete). Also
4299 * uninstall from kernel if asked to.
4300 */
d62a17ae 4301static int zvni_vtep_del_all(zebra_vni_t *zvni, int uninstall)
2232a77c 4302{
d62a17ae 4303 zebra_vtep_t *zvtep, *zvtep_next;
2232a77c 4304
d62a17ae 4305 if (!zvni)
4306 return -1;
2232a77c 4307
d62a17ae 4308 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep_next) {
4309 zvtep_next = zvtep->next;
4310 if (uninstall)
4311 zvni_vtep_uninstall(zvni, &zvtep->vtep_ip);
4312 zvni_vtep_del(zvni, zvtep);
4313 }
2232a77c 4314
d62a17ae 4315 return 0;
2232a77c 4316}
4317
4318/*
9718c54e
AK
4319 * Install remote VTEP into the kernel if the remote VTEP has asked
4320 * for head-end-replication.
2232a77c 4321 */
9718c54e 4322static int zvni_vtep_install(zebra_vni_t *zvni, zebra_vtep_t *zvtep)
2232a77c 4323{
9718c54e 4324 if (is_vxlan_flooding_head_end() &&
0bbd4ff4
MS
4325 (zvtep->flood_control == VXLAN_FLOOD_HEAD_END_REPL)) {
4326 if (ZEBRA_DPLANE_REQUEST_FAILURE ==
4327 dplane_vtep_add(zvni->vxlan_if,
4328 &zvtep->vtep_ip, zvni->vni))
4329 return -1;
4330 }
4331
fbac9605 4332 return 0;
2232a77c 4333}
4334
4335/*
4336 * Uninstall remote VTEP from the kernel.
4337 */
d62a17ae 4338static int zvni_vtep_uninstall(zebra_vni_t *zvni, struct in_addr *vtep_ip)
2232a77c 4339{
d62a17ae 4340 if (!zvni->vxlan_if) {
9df414fe
QY
4341 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
4342 zvni->vni, zvni);
d62a17ae 4343 return -1;
4344 }
2232a77c 4345
0bbd4ff4
MS
4346 if (ZEBRA_DPLANE_REQUEST_FAILURE ==
4347 dplane_vtep_delete(zvni->vxlan_if, vtep_ip, zvni->vni))
4348 return -1;
4349
4350 return 0;
2232a77c 4351}
4352
fbac9605
DS
4353/*
4354 * Install or uninstall flood entries in the kernel corresponding to
4355 * remote VTEPs. This is invoked upon change to BUM handling.
4356 */
e3b78da8 4357static void zvni_handle_flooding_remote_vteps(struct hash_bucket *bucket,
fbac9605
DS
4358 void *zvrf)
4359{
4360 zebra_vni_t *zvni;
4361 zebra_vtep_t *zvtep;
4362
e3b78da8 4363 zvni = (zebra_vni_t *)bucket->data;
fbac9605
DS
4364 if (!zvni)
4365 return;
4366
4367 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
4368 if (is_vxlan_flooding_head_end())
9718c54e 4369 zvni_vtep_install(zvni, zvtep);
fbac9605
DS
4370 else
4371 zvni_vtep_uninstall(zvni, &zvtep->vtep_ip);
4372 }
4373}
4374
2232a77c 4375/*
4376 * Cleanup VNI/VTEP and update kernel
4377 */
e3b78da8 4378static void zvni_cleanup_all(struct hash_bucket *bucket, void *arg)
2232a77c 4379{
b7cfce93
MK
4380 zebra_vni_t *zvni = NULL;
4381 zebra_l3vni_t *zl3vni = NULL;
84915b0a 4382 struct zebra_vrf *zvrf = (struct zebra_vrf *)arg;
2232a77c 4383
e3b78da8 4384 zvni = (zebra_vni_t *)bucket->data;
2232a77c 4385
b7cfce93 4386 /* remove from l3-vni list */
84915b0a 4387 if (zvrf->l3vni)
4388 zl3vni = zl3vni_lookup(zvrf->l3vni);
b7cfce93
MK
4389 if (zl3vni)
4390 listnode_delete(zl3vni->l2vnis, zvni);
4391
d62a17ae 4392 /* Free up all neighbors and MACs, if any. */
2853fed6 4393 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
4394 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
2232a77c 4395
d62a17ae 4396 /* Free up all remote VTEPs, if any. */
4397 zvni_vtep_del_all(zvni, 1);
2232a77c 4398
d62a17ae 4399 /* Delete the hash entry. */
2853fed6 4400 zvni_del(zvni);
2232a77c 4401}
4402
655b04d1 4403/* cleanup L3VNI */
e3b78da8 4404static void zl3vni_cleanup_all(struct hash_bucket *bucket, void *args)
655b04d1
MK
4405{
4406 zebra_l3vni_t *zl3vni = NULL;
4407
e3b78da8 4408 zl3vni = (zebra_l3vni_t *)bucket->data;
655b04d1
MK
4409
4410 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4411}
4412
85442b09 4413static void rb_find_or_add_host(struct host_rb_tree_entry *hrbe,
41db76c2 4414 struct prefix *host)
5e1b0650
DS
4415{
4416 struct host_rb_entry lookup;
4417 struct host_rb_entry *hle;
4418
4419 memset(&lookup, 0, sizeof(lookup));
4420 memcpy(&lookup.p, host, sizeof(*host));
4421
85442b09 4422 hle = RB_FIND(host_rb_tree_entry, hrbe, &lookup);
5e1b0650
DS
4423 if (hle)
4424 return;
4425
4426 hle = XCALLOC(MTYPE_HOST_PREFIX, sizeof(struct host_rb_entry));
4427 memcpy(hle, &lookup, sizeof(lookup));
4428
85442b09 4429 RB_INSERT(host_rb_tree_entry, hrbe, hle);
5e1b0650
DS
4430}
4431
85442b09 4432static void rb_delete_host(struct host_rb_tree_entry *hrbe, struct prefix *host)
5e1b0650
DS
4433{
4434 struct host_rb_entry lookup;
4435 struct host_rb_entry *hle;
4436
4437 memset(&lookup, 0, sizeof(lookup));
4438 memcpy(&lookup.p, host, sizeof(*host));
4439
85442b09 4440 hle = RB_FIND(host_rb_tree_entry, hrbe, &lookup);
10ac2516 4441 if (hle) {
85442b09 4442 RB_REMOVE(host_rb_tree_entry, hrbe, hle);
10ac2516
DS
4443 XFREE(MTYPE_HOST_PREFIX, hle);
4444 }
5e1b0650
DS
4445
4446 return;
4447}
4448
b7cfce93
MK
4449/*
4450 * Look up MAC hash entry.
4451 */
2dbad57f 4452static zebra_mac_t *zl3vni_rmac_lookup(zebra_l3vni_t *zl3vni,
b7cfce93
MK
4453 struct ethaddr *rmac)
4454{
4455 zebra_mac_t tmp;
4456 zebra_mac_t *pmac;
4457
4458 memset(&tmp, 0, sizeof(tmp));
4459 memcpy(&tmp.macaddr, rmac, ETH_ALEN);
4460 pmac = hash_lookup(zl3vni->rmac_table, &tmp);
2232a77c 4461
b7cfce93 4462 return pmac;
2dbad57f 4463}
2232a77c 4464
cec2e17d 4465/*
b7cfce93 4466 * Callback to allocate RMAC hash entry.
cec2e17d 4467 */
2dbad57f 4468static void *zl3vni_rmac_alloc(void *p)
d62a17ae 4469{
b7cfce93
MK
4470 const zebra_mac_t *tmp_rmac = p;
4471 zebra_mac_t *zrmac;
d62a17ae 4472
b7cfce93
MK
4473 zrmac = XCALLOC(MTYPE_MAC, sizeof(zebra_mac_t));
4474 *zrmac = *tmp_rmac;
d62a17ae 4475
b7cfce93 4476 return ((void *)zrmac);
2dbad57f 4477}
cd233079 4478
b7cfce93
MK
4479/*
4480 * Add RMAC entry to l3-vni
4481 */
996c9314 4482static zebra_mac_t *zl3vni_rmac_add(zebra_l3vni_t *zl3vni, struct ethaddr *rmac)
b7cfce93
MK
4483{
4484 zebra_mac_t tmp_rmac;
4485 zebra_mac_t *zrmac = NULL;
d62a17ae 4486
b7cfce93
MK
4487 memset(&tmp_rmac, 0, sizeof(zebra_mac_t));
4488 memcpy(&tmp_rmac.macaddr, rmac, ETH_ALEN);
4489 zrmac = hash_get(zl3vni->rmac_table, &tmp_rmac, zl3vni_rmac_alloc);
4490 assert(zrmac);
d62a17ae 4491
85442b09 4492 RB_INIT(host_rb_tree_entry, &zrmac->host_rb);
b7cfce93 4493
2dbad57f 4494 SET_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE);
4495 SET_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC);
4496
b7cfce93 4497 return zrmac;
2dbad57f 4498}
cec2e17d 4499
4500/*
b7cfce93 4501 * Delete MAC entry.
cec2e17d 4502 */
996c9314 4503static int zl3vni_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
cec2e17d 4504{
b7cfce93 4505 zebra_mac_t *tmp_rmac;
5e1b0650 4506 struct host_rb_entry *hle;
cd233079 4507
85442b09
DS
4508 while (!RB_EMPTY(host_rb_tree_entry, &zrmac->host_rb)) {
4509 hle = RB_ROOT(host_rb_tree_entry, &zrmac->host_rb);
5e1b0650 4510
85442b09 4511 RB_REMOVE(host_rb_tree_entry, &zrmac->host_rb, hle);
5e1b0650
DS
4512 XFREE(MTYPE_HOST_PREFIX, hle);
4513 }
cd233079 4514
b7cfce93 4515 tmp_rmac = hash_release(zl3vni->rmac_table, zrmac);
0a22ddfb 4516 XFREE(MTYPE_MAC, tmp_rmac);
cd233079 4517
b7cfce93 4518 return 0;
2dbad57f 4519}
cec2e17d 4520
4521/*
036d93c0 4522 * Install remote RMAC into the forwarding plane.
cec2e17d 4523 */
996c9314 4524static int zl3vni_rmac_install(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
cec2e17d 4525{
478566d6
MS
4526 const struct zebra_if *zif = NULL, *br_zif = NULL;
4527 const struct zebra_l2info_vxlan *vxl = NULL;
4528 const struct interface *br_ifp;
036d93c0 4529 enum zebra_dplane_result res;
478566d6 4530 vlanid_t vid;
cec2e17d 4531
996c9314
LB
4532 if (!(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE))
4533 || !(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC)))
b7cfce93
MK
4534 return 0;
4535
4536 zif = zl3vni->vxlan_if->info;
4537 if (!zif)
4538 return -1;
4539
478566d6
MS
4540 br_ifp = zif->brslave_info.br_if;
4541 if (br_ifp == NULL)
4542 return -1;
4543
b7cfce93
MK
4544 vxl = &zif->l2info.vxl;
4545
478566d6
MS
4546 br_zif = (const struct zebra_if *)br_ifp->info;
4547
4548 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
4549 vid = vxl->access_vlan;
4550 else
4551 vid = 0;
4552
4553 res = dplane_mac_add(zl3vni->vxlan_if, br_ifp, vid,
036d93c0
MS
4554 &zrmac->macaddr, zrmac->fwd_info.r_vtep_ip, 0);
4555 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
4556 return 0;
4557 else
4558 return -1;
2dbad57f 4559}
b7cfce93
MK
4560
4561/*
036d93c0 4562 * Uninstall remote RMAC from the forwarding plane.
b7cfce93 4563 */
996c9314 4564static int zl3vni_rmac_uninstall(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
b7cfce93
MK
4565{
4566 char buf[ETHER_ADDR_STRLEN];
478566d6
MS
4567 const struct zebra_if *zif = NULL, *br_zif;
4568 const struct zebra_l2info_vxlan *vxl = NULL;
4569 const struct interface *br_ifp;
4570 vlanid_t vid;
036d93c0 4571 enum zebra_dplane_result res;
b7cfce93 4572
996c9314
LB
4573 if (!(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE))
4574 || !(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC)))
b7cfce93
MK
4575 return 0;
4576
4577 if (!zl3vni->vxlan_if) {
e0e140a7
MS
4578 if (IS_ZEBRA_DEBUG_VXLAN)
4579 zlog_debug(
4580 "RMAC %s on L3-VNI %u hash %p couldn't be uninstalled - no vxlan_if",
4581 prefix_mac2str(&zrmac->macaddr,
4582 buf, sizeof(buf)),
4583 zl3vni->vni, zl3vni);
b7cfce93
MK
4584 return -1;
4585 }
4586
4587 zif = zl3vni->vxlan_if->info;
4588 if (!zif)
4589 return -1;
4590
478566d6
MS
4591 br_ifp = zif->brslave_info.br_if;
4592 if (br_ifp == NULL)
4593 return -1;
4594
b7cfce93
MK
4595 vxl = &zif->l2info.vxl;
4596
478566d6
MS
4597 br_zif = (const struct zebra_if *)br_ifp->info;
4598 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
4599 vid = vxl->access_vlan;
4600 else
4601 vid = 0;
4602
4603 res = dplane_mac_del(zl3vni->vxlan_if, br_ifp, vid,
036d93c0
MS
4604 &zrmac->macaddr, zrmac->fwd_info.r_vtep_ip);
4605 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
4606 return 0;
4607 else
4608 return -1;
2dbad57f 4609}
4610
4611/* handle rmac add */
996c9314 4612static int zl3vni_remote_rmac_add(zebra_l3vni_t *zl3vni, struct ethaddr *rmac,
6134fd82 4613 struct ipaddr *vtep_ip,
4614 struct prefix *host_prefix)
2dbad57f 4615{
4616 char buf[ETHER_ADDR_STRLEN];
4617 char buf1[INET6_ADDRSTRLEN];
4618 zebra_mac_t *zrmac = NULL;
4619
4620 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
4621 if (!zrmac) {
4622
4623 zrmac = zl3vni_rmac_add(zl3vni, rmac);
4624 if (!zrmac) {
9df414fe 4625 zlog_debug(
2dbad57f 4626 "Failed to add RMAC %s L3VNI %u Remote VTEP %s",
996c9314
LB
4627 prefix_mac2str(rmac, buf, sizeof(buf)),
4628 zl3vni->vni,
4629 ipaddr2str(vtep_ip, buf1, sizeof(buf1)));
2dbad57f 4630 return -1;
4631 }
4632 memset(&zrmac->fwd_info, 0, sizeof(zrmac->fwd_info));
4633 zrmac->fwd_info.r_vtep_ip = vtep_ip->ipaddr_v4;
4634
a780a738
AD
4635 /* Send RMAC for FPM processing */
4636 hook_call(zebra_rmac_update, zrmac, zl3vni, false,
4637 "new RMAC added");
4638
2dbad57f 4639 /* install rmac in kernel */
4640 zl3vni_rmac_install(zl3vni, zrmac);
4641 }
6134fd82 4642
41db76c2 4643 rb_find_or_add_host(&zrmac->host_rb, host_prefix);
5e1b0650 4644
2dbad57f 4645 return 0;
4646}
4647
4648
4649/* handle rmac delete */
22e63104 4650static void zl3vni_remote_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac,
6134fd82 4651 struct prefix *host_prefix)
2dbad57f 4652{
41db76c2 4653 rb_delete_host(&zrmac->host_rb, host_prefix);
2dbad57f 4654
85442b09 4655 if (RB_EMPTY(host_rb_tree_entry, &zrmac->host_rb)) {
2dbad57f 4656 /* uninstall from kernel */
4657 zl3vni_rmac_uninstall(zl3vni, zrmac);
4658
a780a738
AD
4659 /* Send RMAC for FPM processing */
4660 hook_call(zebra_rmac_update, zrmac, zl3vni, true,
4661 "RMAC deleted");
4662
2dbad57f 4663 /* del the rmac entry */
4664 zl3vni_rmac_del(zl3vni, zrmac);
4665 }
2dbad57f 4666}
b7cfce93
MK
4667
4668/*
4669 * Look up nh hash entry on a l3-vni.
4670 */
996c9314 4671static zebra_neigh_t *zl3vni_nh_lookup(zebra_l3vni_t *zl3vni, struct ipaddr *ip)
b7cfce93
MK
4672{
4673 zebra_neigh_t tmp;
4674 zebra_neigh_t *n;
4675
4676 memset(&tmp, 0, sizeof(tmp));
4677 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
4678 n = hash_lookup(zl3vni->nh_table, &tmp);
4679
4680 return n;
2dbad57f 4681}
b7cfce93
MK
4682
4683
4684/*
4685 * Callback to allocate NH hash entry on L3-VNI.
4686 */
2dbad57f 4687static void *zl3vni_nh_alloc(void *p)
b7cfce93
MK
4688{
4689 const zebra_neigh_t *tmp_n = p;
4690 zebra_neigh_t *n;
4691
4692 n = XCALLOC(MTYPE_NEIGH, sizeof(zebra_neigh_t));
4693 *n = *tmp_n;
4694
4695 return ((void *)n);
2dbad57f 4696}
b7cfce93
MK
4697
4698/*
4699 * Add neighbor entry.
4700 */
996c9314 4701static zebra_neigh_t *zl3vni_nh_add(zebra_l3vni_t *zl3vni, struct ipaddr *ip,
b7cfce93
MK
4702 struct ethaddr *mac)
4703{
4704 zebra_neigh_t tmp_n;
4705 zebra_neigh_t *n = NULL;
4706
4707 memset(&tmp_n, 0, sizeof(zebra_neigh_t));
4708 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
4709 n = hash_get(zl3vni->nh_table, &tmp_n, zl3vni_nh_alloc);
4710 assert(n);
4711
85442b09 4712 RB_INIT(host_rb_tree_entry, &n->host_rb);
6134fd82 4713
b7cfce93 4714 memcpy(&n->emac, mac, ETH_ALEN);
2dbad57f 4715 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
4716 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE_NH);
b7cfce93
MK
4717
4718 return n;
2dbad57f 4719}
b7cfce93
MK
4720
4721/*
4722 * Delete neighbor entry.
4723 */
996c9314 4724static int zl3vni_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
b7cfce93
MK
4725{
4726 zebra_neigh_t *tmp_n;
f2a503f0 4727 struct host_rb_entry *hle;
b7cfce93 4728
85442b09
DS
4729 while (!RB_EMPTY(host_rb_tree_entry, &n->host_rb)) {
4730 hle = RB_ROOT(host_rb_tree_entry, &n->host_rb);
f2a503f0 4731
85442b09 4732 RB_REMOVE(host_rb_tree_entry, &n->host_rb, hle);
f2a503f0
DS
4733 XFREE(MTYPE_HOST_PREFIX, hle);
4734 }
55aabf6f 4735
b7cfce93 4736 tmp_n = hash_release(zl3vni->nh_table, n);
0a22ddfb 4737 XFREE(MTYPE_NEIGH, tmp_n);
b7cfce93
MK
4738
4739 return 0;
2dbad57f 4740}
b7cfce93
MK
4741
4742/*
4743 * Install remote nh as neigh into the kernel.
4744 */
996c9314 4745static int zl3vni_nh_install(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
b7cfce93 4746{
68e33151
CS
4747 uint8_t flags;
4748 int ret = 0;
4749
b7cfce93
MK
4750 if (!is_l3vni_oper_up(zl3vni))
4751 return -1;
4752
996c9314
LB
4753 if (!(n->flags & ZEBRA_NEIGH_REMOTE)
4754 || !(n->flags & ZEBRA_NEIGH_REMOTE_NH))
b7cfce93 4755 return 0;
931fa60c
MS
4756
4757 flags = DPLANE_NTF_EXT_LEARNED;
68e33151 4758 if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
931fa60c
MS
4759 flags |= DPLANE_NTF_ROUTER;
4760
4761 dplane_neigh_add(zl3vni->svi_if, &n->ip, &n->emac, flags);
4762
68e33151 4763 return ret;
2dbad57f 4764}
b7cfce93
MK
4765
4766/*
4767 * Uninstall remote nh from the kernel.
4768 */
996c9314 4769static int zl3vni_nh_uninstall(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
b7cfce93 4770{
996c9314
LB
4771 if (!(n->flags & ZEBRA_NEIGH_REMOTE)
4772 || !(n->flags & ZEBRA_NEIGH_REMOTE_NH))
b7cfce93
MK
4773 return 0;
4774
fa409e1e 4775 if (!zl3vni->svi_if || !if_is_operative(zl3vni->svi_if))
4776 return 0;
4777
931fa60c
MS
4778 dplane_neigh_delete(zl3vni->svi_if, &n->ip);
4779
4780 return 0;
2dbad57f 4781}
4782
4783/* add remote vtep as a neigh entry */
996c9314 4784static int zl3vni_remote_nh_add(zebra_l3vni_t *zl3vni, struct ipaddr *vtep_ip,
6134fd82 4785 struct ethaddr *rmac,
4786 struct prefix *host_prefix)
2dbad57f 4787{
4788 char buf[ETHER_ADDR_STRLEN];
4789 char buf1[INET6_ADDRSTRLEN];
4790 zebra_neigh_t *nh = NULL;
4791
4792 nh = zl3vni_nh_lookup(zl3vni, vtep_ip);
4793 if (!nh) {
4794 nh = zl3vni_nh_add(zl3vni, vtep_ip, rmac);
4795 if (!nh) {
4796
9df414fe 4797 zlog_debug(
2dbad57f 4798 "Failed to add NH as Neigh (IP %s MAC %s L3-VNI %u)",
996c9314
LB
4799 ipaddr2str(vtep_ip, buf1, sizeof(buf1)),
4800 prefix_mac2str(rmac, buf, sizeof(buf)),
2dbad57f 4801 zl3vni->vni);
4802 return -1;
4803 }
4804
4805 /* install the nh neigh in kernel */
4806 zl3vni_nh_install(zl3vni, nh);
4807 }
6134fd82 4808
f2a503f0 4809 rb_find_or_add_host(&nh->host_rb, host_prefix);
6134fd82 4810
2dbad57f 4811 return 0;
4812}
4813
4814/* handle nh neigh delete */
22e63104 4815static void zl3vni_remote_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *nh,
4816 struct prefix *host_prefix)
2dbad57f 4817{
f2a503f0 4818 rb_delete_host(&nh->host_rb, host_prefix);
2dbad57f 4819
85442b09 4820 if (RB_EMPTY(host_rb_tree_entry, &nh->host_rb)) {
2dbad57f 4821 /* uninstall from kernel */
4822 zl3vni_nh_uninstall(zl3vni, nh);
4823
4824 /* delete the nh entry */
4825 zl3vni_nh_del(zl3vni, nh);
4826 }
2dbad57f 4827}
b7cfce93 4828
3bcbba10 4829/* handle neigh update from kernel - the only thing of interest is to
4830 * readd stale entries.
4831 */
996c9314 4832static int zl3vni_local_nh_add_update(zebra_l3vni_t *zl3vni, struct ipaddr *ip,
d7c0a89a 4833 uint16_t state)
3bcbba10 4834{
c05a738b 4835#ifdef GNU_LINUX
3bcbba10 4836 zebra_neigh_t *n = NULL;
4837
4838 n = zl3vni_nh_lookup(zl3vni, ip);
4839 if (!n)
4840 return 0;
4841
4842 /* all next hop neigh are remote and installed by frr.
4843 * If the kernel has aged this entry, re-install.
4844 */
4845 if (state & NUD_STALE)
4846 zl3vni_nh_install(zl3vni, n);
c05a738b 4847#endif
3bcbba10 4848 return 0;
4849}
4850
8c9b80b9 4851/* handle neigh delete from kernel */
996c9314 4852static int zl3vni_local_nh_del(zebra_l3vni_t *zl3vni, struct ipaddr *ip)
8c9b80b9
MK
4853{
4854 zebra_neigh_t *n = NULL;
4855
4856 n = zl3vni_nh_lookup(zl3vni, ip);
4857 if (!n)
4858 return 0;
4859
4860 /* all next hop neigh are remote and installed by frr.
4861 * If we get an age out notification for these neigh entries, we have to
523cafc4 4862 * install it back
4863 */
8c9b80b9
MK
4864 zl3vni_nh_install(zl3vni, n);
4865
4866 return 0;
4867}
4868
b7cfce93
MK
4869/*
4870 * Hash function for L3 VNI.
4871 */
d8b87afe 4872static unsigned int l3vni_hash_keymake(const void *p)
b7cfce93
MK
4873{
4874 const zebra_l3vni_t *zl3vni = p;
4875
4876 return jhash_1word(zl3vni->vni, 0);
4877}
4878
4879/*
4880 * Compare 2 L3 VNI hash entries.
4881 */
74df8d6d 4882static bool l3vni_hash_cmp(const void *p1, const void *p2)
b7cfce93
MK
4883{
4884 const zebra_l3vni_t *zl3vni1 = p1;
4885 const zebra_l3vni_t *zl3vni2 = p2;
4886
4887 return (zl3vni1->vni == zl3vni2->vni);
4888}
4889
4890/*
4891 * Callback to allocate L3 VNI hash entry.
4892 */
4893static void *zl3vni_alloc(void *p)
4894{
4895 zebra_l3vni_t *zl3vni = NULL;
4896 const zebra_l3vni_t *tmp_l3vni = p;
4897
4898 zl3vni = XCALLOC(MTYPE_ZL3VNI, sizeof(zebra_l3vni_t));
4899 zl3vni->vni = tmp_l3vni->vni;
4900 return ((void *)zl3vni);
4901}
4902
4903/*
4904 * Look up L3 VNI hash entry.
4905 */
4906static zebra_l3vni_t *zl3vni_lookup(vni_t vni)
4907{
b7cfce93
MK
4908 zebra_l3vni_t tmp_l3vni;
4909 zebra_l3vni_t *zl3vni = NULL;
4910
b7cfce93
MK
4911 memset(&tmp_l3vni, 0, sizeof(zebra_l3vni_t));
4912 tmp_l3vni.vni = vni;
89272910 4913 zl3vni = hash_lookup(zrouter.l3vni_table, &tmp_l3vni);
b7cfce93
MK
4914
4915 return zl3vni;
4916}
4917
4918/*
4919 * Add L3 VNI hash entry.
4920 */
4921static zebra_l3vni_t *zl3vni_add(vni_t vni, vrf_id_t vrf_id)
4922{
4923 zebra_l3vni_t tmp_zl3vni;
b7cfce93
MK
4924 zebra_l3vni_t *zl3vni = NULL;
4925
b7cfce93
MK
4926 memset(&tmp_zl3vni, 0, sizeof(zebra_l3vni_t));
4927 tmp_zl3vni.vni = vni;
4928
89272910 4929 zl3vni = hash_get(zrouter.l3vni_table, &tmp_zl3vni, zl3vni_alloc);
b7cfce93
MK
4930 assert(zl3vni);
4931
4932 zl3vni->vrf_id = vrf_id;
4933 zl3vni->svi_if = NULL;
4934 zl3vni->vxlan_if = NULL;
4935 zl3vni->l2vnis = list_new();
27fa3398 4936 zl3vni->l2vnis->cmp = vni_list_cmp;
b7cfce93
MK
4937
4938 /* Create hash table for remote RMAC */
996c9314
LB
4939 zl3vni->rmac_table = hash_create(mac_hash_keymake, mac_cmp,
4940 "Zebra L3-VNI RMAC-Table");
b7cfce93
MK
4941
4942 /* Create hash table for neighbors */
4943 zl3vni->nh_table = hash_create(neigh_hash_keymake, neigh_cmp,
996c9314 4944 "Zebra L3-VNI next-hop table");
b7cfce93
MK
4945
4946 return zl3vni;
4947}
4948
4949/*
4950 * Delete L3 VNI hash entry.
4951 */
4952static int zl3vni_del(zebra_l3vni_t *zl3vni)
4953{
b7cfce93
MK
4954 zebra_l3vni_t *tmp_zl3vni;
4955
b7cfce93 4956 /* free the list of l2vnis */
6a154c88 4957 list_delete(&zl3vni->l2vnis);
b7cfce93
MK
4958 zl3vni->l2vnis = NULL;
4959
4960 /* Free the rmac table */
4961 hash_free(zl3vni->rmac_table);
4962 zl3vni->rmac_table = NULL;
4963
4964 /* Free the nh table */
4965 hash_free(zl3vni->nh_table);
4966 zl3vni->nh_table = NULL;
4967
4968 /* Free the VNI hash entry and allocated memory. */
89272910 4969 tmp_zl3vni = hash_release(zrouter.l3vni_table, zl3vni);
0a22ddfb 4970 XFREE(MTYPE_ZL3VNI, tmp_zl3vni);
b7cfce93
MK
4971
4972 return 0;
4973}
4974
a780a738 4975struct interface *zl3vni_map_to_vxlan_if(zebra_l3vni_t *zl3vni)
b7cfce93
MK
4976{
4977 struct zebra_ns *zns = NULL;
4978 struct route_node *rn = NULL;
4979 struct interface *ifp = NULL;
4980
4981 /* loop through all vxlan-interface */
4982 zns = zebra_ns_lookup(NS_DEFAULT);
4983 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
4984
4985 struct zebra_if *zif = NULL;
4986 struct zebra_l2info_vxlan *vxl = NULL;
4987
4988 ifp = (struct interface *)rn->info;
4989 if (!ifp)
4990 continue;
4991
4992 zif = ifp->info;
4993 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
4994 continue;
4995
4996 vxl = &zif->l2info.vxl;
b67a60d2 4997 if (vxl->vni == zl3vni->vni) {
4998 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93 4999 return ifp;
b67a60d2 5000 }
b7cfce93
MK
5001 }
5002
5003 return NULL;
5004}
5005
a780a738 5006struct interface *zl3vni_map_to_svi_if(zebra_l3vni_t *zl3vni)
b7cfce93 5007{
996c9314 5008 struct zebra_if *zif = NULL; /* zebra_if for vxlan_if */
b7cfce93
MK
5009 struct zebra_l2info_vxlan *vxl = NULL; /* l2 info for vxlan_if */
5010
2aeb403d 5011 if (!zl3vni)
5012 return NULL;
5013
b7cfce93
MK
5014 if (!zl3vni->vxlan_if)
5015 return NULL;
5016
5017 zif = zl3vni->vxlan_if->info;
5018 if (!zif)
5019 return NULL;
5020
5021 vxl = &zif->l2info.vxl;
5022
5023 return zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
5024}
5025
9d21b7c6 5026zebra_l3vni_t *zl3vni_from_vrf(vrf_id_t vrf_id)
b7cfce93
MK
5027{
5028 struct zebra_vrf *zvrf = NULL;
5029
5030 zvrf = zebra_vrf_lookup_by_id(vrf_id);
5031 if (!zvrf)
5032 return NULL;
5033
5034 return zl3vni_lookup(zvrf->l3vni);
5035}
5036
5037/*
5038 * Map SVI and associated bridge to a VNI. This is invoked upon getting
5039 * neighbor notifications, to see if they are of interest.
5040 */
5041static zebra_l3vni_t *zl3vni_from_svi(struct interface *ifp,
5042 struct interface *br_if)
5043{
5044 int found = 0;
5045 vlanid_t vid = 0;
d7c0a89a 5046 uint8_t bridge_vlan_aware = 0;
b7cfce93
MK
5047 zebra_l3vni_t *zl3vni = NULL;
5048 struct zebra_ns *zns = NULL;
5049 struct route_node *rn = NULL;
5050 struct zebra_if *zif = NULL;
5051 struct interface *tmp_if = NULL;
5052 struct zebra_l2info_bridge *br = NULL;
5053 struct zebra_l2info_vxlan *vxl = NULL;
5054
5055 if (!br_if)
5056 return NULL;
5057
5058 /* Make sure the linked interface is a bridge. */
5059 if (!IS_ZEBRA_IF_BRIDGE(br_if))
5060 return NULL;
5061
5062 /* Determine if bridge is VLAN-aware or not */
5063 zif = br_if->info;
5064 assert(zif);
5065 br = &zif->l2info.br;
5066 bridge_vlan_aware = br->vlan_aware;
5067 if (bridge_vlan_aware) {
5068 struct zebra_l2info_vlan *vl;
5069
5070 if (!IS_ZEBRA_IF_VLAN(ifp))
5071 return NULL;
5072
5073 zif = ifp->info;
5074 assert(zif);
5075 vl = &zif->l2info.vl;
5076 vid = vl->vid;
5077 }
5078
5079 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
5080 /* TODO: Optimize with a hash. */
5081 zns = zebra_ns_lookup(NS_DEFAULT);
5082 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
5083 tmp_if = (struct interface *)rn->info;
5084 if (!tmp_if)
5085 continue;
5086 zif = tmp_if->info;
5087 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
5088 continue;
5089 if (!if_is_operative(tmp_if))
5090 continue;
5091 vxl = &zif->l2info.vxl;
5092
5093 if (zif->brslave_info.br_if != br_if)
5094 continue;
5095
5096 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
5097 found = 1;
5098 break;
5099 }
5100 }
5101
5102 if (!found)
5103 return NULL;
5104
5105 zl3vni = zl3vni_lookup(vxl->vni);
5106 return zl3vni;
5107}
5108
5109/*
5110 * Inform BGP about l3-vni.
5111 */
5112static int zl3vni_send_add_to_client(zebra_l3vni_t *zl3vni)
5113{
5114 struct stream *s = NULL;
5115 struct zserv *client = NULL;
5116 struct ethaddr rmac;
5117 char buf[ETHER_ADDR_STRLEN];
5118
21ccc0cf 5119 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
b7cfce93
MK
5120 /* BGP may not be running. */
5121 if (!client)
5122 return 0;
5123
5124 /* get the rmac */
5125 memset(&rmac, 0, sizeof(struct ethaddr));
5126 zl3vni_get_rmac(zl3vni, &rmac);
5127
1002497a 5128 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
b7cfce93 5129
996c9314 5130 zclient_create_header(s, ZEBRA_L3VNI_ADD, zl3vni_vrf_id(zl3vni));
b7cfce93
MK
5131 stream_putl(s, zl3vni->vni);
5132 stream_put(s, &rmac, sizeof(struct ethaddr));
b67a60d2 5133 stream_put_in_addr(s, &zl3vni->local_vtep_ip);
c48d9f5f 5134 stream_put(s, &zl3vni->filter, sizeof(int));
0483af6e 5135 stream_putl(s, zl3vni->svi_if->ifindex);
b7cfce93
MK
5136
5137 /* Write packet size. */
5138 stream_putw_at(s, 0, stream_get_endp(s));
5139
5140 if (IS_ZEBRA_DEBUG_VXLAN)
c48d9f5f 5141 zlog_debug(
996c9314
LB
5142 "Send L3_VNI_ADD %u VRF %s RMAC %s local-ip %s filter %s to %s",
5143 zl3vni->vni, vrf_id_to_name(zl3vni_vrf_id(zl3vni)),
5144 prefix_mac2str(&rmac, buf, sizeof(buf)),
5145 inet_ntoa(zl3vni->local_vtep_ip),
5146 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
5147 ? "prefix-routes-only"
5148 : "none",
5149 zebra_route_string(client->proto));
b7cfce93
MK
5150
5151 client->l3vniadd_cnt++;
21ccc0cf 5152 return zserv_send_message(client, s);
b7cfce93
MK
5153}
5154
5155/*
5156 * Inform BGP about local l3-VNI deletion.
5157 */
5158static int zl3vni_send_del_to_client(zebra_l3vni_t *zl3vni)
5159{
5160 struct stream *s = NULL;
5161 struct zserv *client = NULL;
5162
21ccc0cf 5163 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
b7cfce93
MK
5164 /* BGP may not be running. */
5165 if (!client)
5166 return 0;
5167
1002497a 5168 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
b7cfce93 5169
996c9314 5170 zclient_create_header(s, ZEBRA_L3VNI_DEL, zl3vni_vrf_id(zl3vni));
b7cfce93
MK
5171 stream_putl(s, zl3vni->vni);
5172
5173 /* Write packet size. */
5174 stream_putw_at(s, 0, stream_get_endp(s));
5175
5176 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 5177 zlog_debug("Send L3_VNI_DEL %u VRF %s to %s", zl3vni->vni,
b7cfce93
MK
5178 vrf_id_to_name(zl3vni_vrf_id(zl3vni)),
5179 zebra_route_string(client->proto));
5180
5181 client->l3vnidel_cnt++;
21ccc0cf 5182 return zserv_send_message(client, s);
b7cfce93
MK
5183}
5184
5185static void zebra_vxlan_process_l3vni_oper_up(zebra_l3vni_t *zl3vni)
5186{
2aeb403d 5187 if (!zl3vni)
5188 return;
5189
b7cfce93
MK
5190 /* send l3vni add to BGP */
5191 zl3vni_send_add_to_client(zl3vni);
5192}
5193
5194static void zebra_vxlan_process_l3vni_oper_down(zebra_l3vni_t *zl3vni)
5195{
2aeb403d 5196 if (!zl3vni)
5197 return;
5198
b7cfce93
MK
5199 /* send l3-vni del to BGP*/
5200 zl3vni_send_del_to_client(zl3vni);
5201}
5202
e3b78da8 5203static void zvni_add_to_l3vni_list(struct hash_bucket *bucket, void *ctxt)
b7cfce93 5204{
e3b78da8 5205 zebra_vni_t *zvni = (zebra_vni_t *)bucket->data;
996c9314 5206 zebra_l3vni_t *zl3vni = (zebra_l3vni_t *)ctxt;
b7cfce93
MK
5207
5208 if (zvni->vrf_id == zl3vni_vrf_id(zl3vni))
5209 listnode_add_sort(zl3vni->l2vnis, zvni);
5210}
5211
b7cfce93
MK
5212/*
5213 * handle transition of vni from l2 to l3 and vice versa
5214 */
996c9314
LB
5215static int zebra_vxlan_handle_vni_transition(struct zebra_vrf *zvrf, vni_t vni,
5216 int add)
b7cfce93
MK
5217{
5218 zebra_vni_t *zvni = NULL;
5219
5220 /* There is a possibility that VNI notification was already received
5221 * from kernel and we programmed it as L2-VNI
5222 * In such a case we need to delete this L2-VNI first, so
5223 * that it can be reprogrammed as L3-VNI in the system. It is also
5224 * possible that the vrf-vni mapping is removed from FRR while the vxlan
5225 * interface is still present in kernel. In this case to keep it
523cafc4 5226 * symmetric, we will delete the l3-vni and reprogram it as l2-vni
5227 */
b7cfce93
MK
5228 if (add) {
5229 /* Locate hash entry */
5230 zvni = zvni_lookup(vni);
5231 if (!zvni)
5232 return 0;
5233
5234 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 5235 zlog_debug("Del L2-VNI %u - transition to L3-VNI", vni);
b7cfce93
MK
5236
5237 /* Delete VNI from BGP. */
5238 zvni_send_del_to_client(zvni->vni);
5239
5240 /* Free up all neighbors and MAC, if any. */
5241 zvni_neigh_del_all(zvni, 0, 0, DEL_ALL_NEIGH);
5242 zvni_mac_del_all(zvni, 0, 0, DEL_ALL_MAC);
5243
5244 /* Free up all remote VTEPs, if any. */
5245 zvni_vtep_del_all(zvni, 0);
5246
5247 /* Delete the hash entry. */
5248 if (zvni_del(zvni)) {
e914ccbe 5249 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
1c50c1c0
QY
5250 "Failed to del VNI hash %p, VNI %u", zvni,
5251 zvni->vni);
b7cfce93
MK
5252 return -1;
5253 }
5254 } else {
5255 /* TODO_MITESH: This needs to be thought through. We don't have
5256 * enough information at this point to reprogram the vni as
5257 * l2-vni. One way is to store the required info in l3-vni and
523cafc4 5258 * used it solely for this purpose
5259 */
b7cfce93
MK
5260 }
5261
5262 return 0;
5263}
5264
5e06422c 5265/* delete and uninstall rmac hash entry */
e3b78da8 5266static void zl3vni_del_rmac_hash_entry(struct hash_bucket *bucket, void *ctx)
5e06422c
MK
5267{
5268 zebra_mac_t *zrmac = NULL;
5269 zebra_l3vni_t *zl3vni = NULL;
5270
e3b78da8 5271 zrmac = (zebra_mac_t *)bucket->data;
5e06422c
MK
5272 zl3vni = (zebra_l3vni_t *)ctx;
5273 zl3vni_rmac_uninstall(zl3vni, zrmac);
a780a738
AD
5274
5275 /* Send RMAC for FPM processing */
5276 hook_call(zebra_rmac_update, zrmac, zl3vni, true, "RMAC deleted");
5277
5e06422c
MK
5278 zl3vni_rmac_del(zl3vni, zrmac);
5279}
5280
5281/* delete and uninstall nh hash entry */
e3b78da8 5282static void zl3vni_del_nh_hash_entry(struct hash_bucket *bucket, void *ctx)
5e06422c
MK
5283{
5284 zebra_neigh_t *n = NULL;
5285 zebra_l3vni_t *zl3vni = NULL;
5286
e3b78da8 5287 n = (zebra_neigh_t *)bucket->data;
5e06422c
MK
5288 zl3vni = (zebra_l3vni_t *)ctx;
5289 zl3vni_nh_uninstall(zl3vni, n);
5290 zl3vni_nh_del(zl3vni, n);
5291}
5292
996c9314
LB
5293static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
5294 uint16_t cmd)
31310b25
MK
5295{
5296 struct zserv *client = NULL;
5297 struct stream *s = NULL;
5298 char buf[PREFIX_STRLEN];
5299
21ccc0cf 5300 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
31310b25
MK
5301 /* BGP may not be running. */
5302 if (!client)
5303 return 0;
5304
1002497a 5305 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
31310b25 5306
22bd3e94 5307 zclient_create_header(s, cmd, vrf_id);
31310b25
MK
5308 stream_put(s, p, sizeof(struct prefix));
5309
5310 /* Write packet size. */
5311 stream_putw_at(s, 0, stream_get_endp(s));
5312
5313 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 5314 zlog_debug("Send ip prefix %s %s on vrf %s",
31310b25
MK
5315 prefix2str(p, buf, sizeof(buf)),
5316 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
5317 vrf_id_to_name(vrf_id));
5318
5319 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD)
5320 client->prefixadd_cnt++;
5321 else
5322 client->prefixdel_cnt++;
5323
21ccc0cf 5324 return zserv_send_message(client, s);
31310b25
MK
5325}
5326
a9a76262
MK
5327/* re-add remote rmac if needed */
5328static int zebra_vxlan_readd_remote_rmac(zebra_l3vni_t *zl3vni,
996c9314 5329 struct ethaddr *rmac)
a9a76262
MK
5330{
5331 char buf[ETHER_ADDR_STRLEN];
5332 zebra_mac_t *zrmac = NULL;
5333
5334 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
5335 if (!zrmac)
5336 return 0;
5337
5338 if (IS_ZEBRA_DEBUG_VXLAN)
5339 zlog_debug("Del remote RMAC %s L3VNI %u - readd",
996c9314 5340 prefix_mac2str(rmac, buf, sizeof(buf)), zl3vni->vni);
a9a76262
MK
5341
5342 zl3vni_rmac_install(zl3vni, zrmac);
5343 return 0;
5344}
5345
f07e1c99 5346/* Process a remote MACIP add from BGP. */
5347static void process_remote_macip_add(vni_t vni,
5348 struct ethaddr *macaddr,
9df2b997 5349 uint16_t ipa_len,
f07e1c99 5350 struct ipaddr *ipaddr,
9df2b997 5351 uint8_t flags,
f07e1c99 5352 uint32_t seq,
5353 struct in_addr vtep_ip)
c48d9f5f 5354{
f07e1c99 5355 zebra_vni_t *zvni;
5356 zebra_vtep_t *zvtep;
e22a946a 5357 zebra_mac_t *mac = NULL, *old_mac = NULL;
f07e1c99 5358 zebra_neigh_t *n = NULL;
5359 int update_mac = 0, update_neigh = 0;
5360 char buf[ETHER_ADDR_STRLEN];
5361 char buf1[INET6_ADDRSTRLEN];
5362 struct interface *ifp = NULL;
5363 struct zebra_if *zif = NULL;
e22a946a 5364 struct zebra_vrf *zvrf;
f07e1c99 5365 uint32_t tmp_seq;
a37f4598 5366 bool sticky;
5367 bool remote_gw;
5368 bool is_router;
e22a946a
CS
5369 bool do_dad = false;
5370 bool is_dup_detect = false;
c48d9f5f 5371
f07e1c99 5372 /* Locate VNI hash entry - expected to exist. */
5373 zvni = zvni_lookup(vni);
5374 if (!zvni) {
5375 zlog_warn("Unknown VNI %u upon remote MACIP ADD", vni);
5376 return;
5377 }
2dbad57f 5378
f07e1c99 5379 ifp = zvni->vxlan_if;
5380 if (ifp)
5381 zif = ifp->info;
5382 if (!ifp ||
5383 !if_is_operative(ifp) ||
5384 !zif ||
5385 !zif->brslave_info.br_if) {
5386 zlog_warn("Ignoring remote MACIP ADD VNI %u, invalid interface state or info",
5387 vni);
2dbad57f 5388 return;
f07e1c99 5389 }
2dbad57f 5390
f07e1c99 5391 /* The remote VTEP specified should normally exist, but it is
5392 * possible that when peering comes up, peer may advertise MACIP
5393 * routes before advertising type-3 routes.
f50dc5e6 5394 */
f07e1c99 5395 zvtep = zvni_vtep_find(zvni, &vtep_ip);
5396 if (!zvtep) {
9718c54e
AK
5397 zvtep = zvni_vtep_add(zvni, &vtep_ip, VXLAN_FLOOD_DISABLED);
5398 if (!zvtep) {
f07e1c99 5399 flog_err(
e914ccbe 5400 EC_ZEBRA_VTEP_ADD_FAILED,
f07e1c99 5401 "Failed to add remote VTEP, VNI %u zvni %p upon remote MACIP ADD",
5402 vni, zvni);
5403 return;
5404 }
2dbad57f 5405
9718c54e 5406 zvni_vtep_install(zvni, zvtep);
f07e1c99 5407 }
f50dc5e6 5408
a37f4598 5409 sticky = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
5410 remote_gw = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
5411 is_router = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
2dbad57f 5412
f07e1c99 5413 mac = zvni_mac_lookup(zvni, macaddr);
2dbad57f 5414
f07e1c99 5415 /* Ignore if the mac is already present as a gateway mac */
5416 if (mac &&
5417 CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW) &&
5418 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) {
5419 if (IS_ZEBRA_DEBUG_VXLAN)
5420 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as MAC is already configured as gateway MAC",
5421 vni,
5422 prefix_mac2str(macaddr, buf, sizeof(buf)),
5423 ipa_len ? " IP " : "",
5424 ipa_len ?
5425 ipaddr2str(ipaddr, buf1, sizeof(buf1)) : "");
2dbad57f 5426 return;
f07e1c99 5427 }
2dbad57f 5428
a36898e7 5429 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
e22a946a
CS
5430 if (!zvrf)
5431 return;
5432
f07e1c99 5433 /* check if the remote MAC is unknown or has a change.
5434 * If so, that needs to be updated first. Note that client could
5435 * install MAC and MACIP separately or just install the latter.
5436 */
5437 if (!mac
5438 || !CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
a37f4598 5439 || sticky != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)
5440 || remote_gw != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW)
f07e1c99 5441 || !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &vtep_ip)
5442 || seq != mac->rem_seq)
5443 update_mac = 1;
5444
5445 if (update_mac) {
5446 if (!mac) {
5447 mac = zvni_mac_add(zvni, macaddr);
5448 if (!mac) {
5449 zlog_warn(
5450 "Failed to add MAC %s VNI %u Remote VTEP %s",
5451 prefix_mac2str(macaddr, buf,
5452 sizeof(buf)),
5453 vni, inet_ntoa(vtep_ip));
5454 return;
5455 }
5456
5457 /* Is this MAC created for a MACIP? */
5458 if (ipa_len)
5459 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5460 } else {
f07e1c99 5461 /* When host moves but changes its (MAC,IP)
5462 * binding, BGP may install a MACIP entry that
5463 * corresponds to "older" location of the host
5464 * in transient situations (because {IP1,M1}
5465 * is a different route from {IP1,M2}). Check
5466 * the sequence number and ignore this update
5467 * if appropriate.
5468 */
27547880 5469 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
f07e1c99 5470 tmp_seq = mac->loc_seq;
27547880 5471 else
f07e1c99 5472 tmp_seq = mac->rem_seq;
27547880 5473
f07e1c99 5474 if (seq < tmp_seq) {
5475 if (IS_ZEBRA_DEBUG_VXLAN)
27547880 5476 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as existing MAC has higher seq %u flags 0x%x",
f07e1c99 5477 vni,
5478 prefix_mac2str(macaddr,
5479 buf, sizeof(buf)),
5480 ipa_len ? " IP " : "",
5481 ipa_len ?
5482 ipaddr2str(ipaddr,
5483 buf1, sizeof(buf1)) : "",
27547880 5484 tmp_seq, mac->flags);
f07e1c99 5485 return;
5486 }
5487 }
5488
e22a946a
CS
5489 /* Check MAC's curent state is local (this is the case
5490 * where MAC has moved from L->R) and check previous
5491 * detection started via local learning.
5492 * RFC-7432: A PE/VTEP that detects a MAC mobility
5493 * event via local learning starts an M-second timer.
5494 *
753ad4fe 5495 * VTEP-IP or seq. change alone is not considered
e22a946a 5496 * for dup. detection.
753ad4fe
CS
5497 *
5498 * MAC is already marked duplicate set dad, then
5499 * is_dup_detect will be set to not install the entry.
e22a946a 5500 */
753ad4fe
CS
5501 if ((!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) &&
5502 mac->dad_count) ||
5503 CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
e22a946a
CS
5504 do_dad = true;
5505
e98e4b88
AK
5506 /* Remove local MAC from BGP. */
5507 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
5508 zvni_mac_send_del_to_client(zvni->vni, macaddr);
5509
f07e1c99 5510 /* Set "auto" and "remote" forwarding info. */
5511 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
5512 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
5513 SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
5514 mac->fwd_info.r_vtep_ip = vtep_ip;
5515
5516 if (sticky)
5517 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
5518 else
5519 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
5520
5521 if (remote_gw)
5522 SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW);
5523 else
5524 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW);
5525
bdca1974
CS
5526 zebra_vxlan_dup_addr_detect_for_mac(zvrf, mac,
5527 mac->fwd_info.r_vtep_ip,
5528 do_dad, &is_dup_detect,
5529 false);
e22a946a 5530
a4445ece
CS
5531 if (!is_dup_detect) {
5532 zvni_process_neigh_on_remote_mac_add(zvni, mac);
5533 /* Install the entry. */
e22a946a 5534 zvni_mac_install(zvni, mac);
a4445ece 5535 }
f07e1c99 5536 }
5537
5538 /* Update seq number. */
5539 mac->rem_seq = seq;
5540
5541 /* If there is no IP, return after clearing AUTO flag of MAC. */
5542 if (!ipa_len) {
5543 UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5544 return;
5545 }
5546
e22a946a
CS
5547 /* Reset flag */
5548 do_dad = false;
5549
f07e1c99 5550 /* Check if the remote neighbor itself is unknown or has a
5551 * change. If so, create or update and then install the entry.
5552 */
5553 n = zvni_neigh_lookup(zvni, ipaddr);
5554 if (!n
5555 || !CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
a37f4598 5556 || is_router != !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)
f07e1c99 5557 || (memcmp(&n->emac, macaddr, sizeof(*macaddr)) != 0)
5558 || !IPV4_ADDR_SAME(&n->r_vtep_ip, &vtep_ip)
5559 || seq != n->rem_seq)
5560 update_neigh = 1;
5561
5562 if (update_neigh) {
5563 if (!n) {
5564 n = zvni_neigh_add(zvni, ipaddr, macaddr);
5565 if (!n) {
5566 zlog_warn(
5567 "Failed to add Neigh %s MAC %s VNI %u Remote VTEP %s",
5568 ipaddr2str(ipaddr, buf1,
5569 sizeof(buf1)),
5570 prefix_mac2str(macaddr, buf,
5571 sizeof(buf)),
5572 vni, inet_ntoa(vtep_ip));
5573 return;
5574 }
5575
5576 } else {
5577 const char *n_type;
5578
5579 /* When host moves but changes its (MAC,IP)
5580 * binding, BGP may install a MACIP entry that
5581 * corresponds to "older" location of the host
5582 * in transient situations (because {IP1,M1}
5583 * is a different route from {IP1,M2}). Check
5584 * the sequence number and ignore this update
5585 * if appropriate.
5586 */
5587 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
5588 tmp_seq = n->loc_seq;
5589 n_type = "local";
5590 } else {
5591 tmp_seq = n->rem_seq;
5592 n_type = "remote";
5593 }
5594 if (seq < tmp_seq) {
5595 if (IS_ZEBRA_DEBUG_VXLAN)
5596 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as existing %s Neigh has higher seq %u",
5597 vni,
5598 prefix_mac2str(macaddr,
5599 buf, sizeof(buf)),
4848ef74
A
5600 " IP ",
5601 ipaddr2str(ipaddr, buf1, sizeof(buf1)),
f07e1c99 5602 n_type,
5603 tmp_seq);
5604 return;
5605 }
5606 if (memcmp(&n->emac, macaddr, sizeof(*macaddr)) != 0) {
5607 /* MAC change, send a delete for old
5608 * neigh if learnt locally.
5609 */
5610 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) &&
5611 IS_ZEBRA_NEIGH_ACTIVE(n))
5612 zvni_neigh_send_del_to_client(
5613 zvni->vni, &n->ip,
2c476b72 5614 &n->emac, 0, n->state);
f07e1c99 5615
5616 /* update neigh list for macs */
5617 old_mac = zvni_mac_lookup(zvni, &n->emac);
5618 if (old_mac) {
5619 listnode_delete(old_mac->neigh_list, n);
fe697c6b 5620 zvni_deref_ip2mac(zvni, old_mac);
f07e1c99 5621 }
5622 listnode_add_sort(mac->neigh_list, n);
5623 memcpy(&n->emac, macaddr, ETH_ALEN);
e22a946a
CS
5624
5625 /* Check Neigh's curent state is local
5626 * (this is the case where neigh/host has moved
5627 * from L->R) and check previous detction
5628 * started via local learning.
5629 *
5630 * RFC-7432: A PE/VTEP that detects a MAC
5631 * mobilit event via local learning starts
5632 * an M-second timer.
5633 * VTEP-IP or seq. change along is not
5634 * considered for dup. detection.
5635 *
5636 * Mobilty event scenario-B IP-MAC binding
5637 * changed.
5638 */
5639 if ((!CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
5640 && n->dad_count)
5641 do_dad = true;
5642
f07e1c99 5643 }
5644 }
5645
5646 /* Set "remote" forwarding info. */
5647 UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
5648 n->r_vtep_ip = vtep_ip;
5649 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
5650
5651 /* Set router flag (R-bit) to this Neighbor entry */
5652 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG))
5653 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
f190902f 5654 else
5655 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
f07e1c99 5656
e22a946a
CS
5657 /* Check old or new MAC detected as duplicate,
5658 * inherit duplicate flag to this neigh.
5659 */
5660 if (zebra_vxlan_ip_inherit_dad_from_mac(zvrf, old_mac,
5661 mac, n)) {
5662 flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
5663 "VNI %u: MAC %s IP %s detected as duplicate during remote update, inherit duplicate from MAC",
5664 zvni->vni,
5665 prefix_mac2str(&mac->macaddr, buf, sizeof(buf)),
5666 ipaddr2str(&n->ip, buf1, sizeof(buf1)));
5667 }
5668
7510e459
CS
5669 /* Check duplicate address detection for IP */
5670 zebra_vxlan_dup_addr_detect_for_neigh(zvrf, n,
5671 n->r_vtep_ip,
5672 do_dad,
5673 &is_dup_detect,
5674 false);
f07e1c99 5675 /* Install the entry. */
e22a946a
CS
5676 if (!is_dup_detect)
5677 zvni_neigh_install(zvni, n);
f07e1c99 5678 }
5679
44bc8ae5
CS
5680 zvni_probe_neigh_on_mac_add(zvni, mac);
5681
f07e1c99 5682 /* Update seq number. */
5683 n->rem_seq = seq;
5684}
5685
5686/* Process a remote MACIP delete from BGP. */
5687static void process_remote_macip_del(vni_t vni,
5688 struct ethaddr *macaddr,
9df2b997 5689 uint16_t ipa_len,
f07e1c99 5690 struct ipaddr *ipaddr,
5691 struct in_addr vtep_ip)
5692{
5693 zebra_vni_t *zvni;
5694 zebra_mac_t *mac = NULL;
5695 zebra_neigh_t *n = NULL;
5696 struct interface *ifp = NULL;
5697 struct zebra_if *zif = NULL;
67fb9374
CS
5698 struct zebra_ns *zns;
5699 struct zebra_l2info_vxlan *vxl;
5700 struct zebra_vrf *zvrf;
f07e1c99 5701 char buf[ETHER_ADDR_STRLEN];
5702 char buf1[INET6_ADDRSTRLEN];
5703
5704 /* Locate VNI hash entry - expected to exist. */
5705 zvni = zvni_lookup(vni);
5706 if (!zvni) {
5707 if (IS_ZEBRA_DEBUG_VXLAN)
5708 zlog_debug("Unknown VNI %u upon remote MACIP DEL", vni);
5709 return;
5710 }
5711
5712 ifp = zvni->vxlan_if;
5713 if (ifp)
5714 zif = ifp->info;
5715 if (!ifp ||
5716 !if_is_operative(ifp) ||
5717 !zif ||
5718 !zif->brslave_info.br_if) {
5719 if (IS_ZEBRA_DEBUG_VXLAN)
5720 zlog_debug("Ignoring remote MACIP DEL VNI %u, invalid interface state or info",
5721 vni);
5722 return;
5723 }
67fb9374
CS
5724 zns = zebra_ns_lookup(NS_DEFAULT);
5725 vxl = &zif->l2info.vxl;
f07e1c99 5726
5727 /* The remote VTEP specified is normally expected to exist, but
5728 * it is possible that the peer may delete the VTEP before deleting
5729 * any MACs referring to the VTEP, in which case the handler (see
5730 * remote_vtep_del) would have already deleted the MACs.
5731 */
5732 if (!zvni_vtep_find(zvni, &vtep_ip))
5733 return;
5734
5735 mac = zvni_mac_lookup(zvni, macaddr);
5736 if (ipa_len)
5737 n = zvni_neigh_lookup(zvni, ipaddr);
5738
5739 if (n && !mac) {
5740 zlog_warn("Failed to locate MAC %s for neigh %s VNI %u upon remote MACIP DEL",
5741 prefix_mac2str(macaddr, buf, sizeof(buf)),
5742 ipaddr2str(ipaddr, buf1, sizeof(buf1)), vni);
5743 return;
5744 }
5745
5746 /* If the remote mac or neighbor doesn't exist there is nothing
5747 * more to do. Otherwise, uninstall the entry and then remove it.
5748 */
5749 if (!mac && !n)
5750 return;
5751
a36898e7 5752 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
67fb9374 5753
f07e1c99 5754 /* Ignore the delete if this mac is a gateway mac-ip */
67fb9374 5755 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
f07e1c99 5756 && CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW)) {
5757 zlog_warn(
5758 "Ignore remote MACIP DEL VNI %u MAC %s%s%s as MAC is already configured as gateway MAC",
5759 vni,
5760 prefix_mac2str(macaddr, buf, sizeof(buf)),
5761 ipa_len ? " IP " : "",
5762 ipa_len ?
5763 ipaddr2str(ipaddr, buf1, sizeof(buf1)) : "");
5764 return;
5765 }
5766
5767 /* Uninstall remote neighbor or MAC. */
5768 if (n) {
67fb9374
CS
5769 if (zvrf->dad_freeze &&
5770 CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE) &&
5771 CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE) &&
5772 (memcmp(n->emac.octet, macaddr->octet, ETH_ALEN) == 0)) {
5773 struct interface *vlan_if;
5774
5775 vlan_if = zvni_map_to_svi(vxl->access_vlan,
5776 zif->brslave_info.br_if);
5777 if (IS_ZEBRA_DEBUG_VXLAN)
5778 zlog_debug("%s: IP %s (flags 0x%x intf %s) is remote and duplicate, read kernel for local entry",
5779 __PRETTY_FUNCTION__,
5780 ipaddr2str(ipaddr, buf1,
5781 sizeof(buf1)), n->flags,
5782 vlan_if->name);
5783 neigh_read_specific_ip(ipaddr, vlan_if);
5784 }
5785
f07e1c99 5786 /* When the MAC changes for an IP, it is possible the
5787 * client may update the new MAC before trying to delete the
5788 * "old" neighbor (as these are two different MACIP routes).
5789 * Do the delete only if the MAC matches.
5790 */
5791 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
5792 && (memcmp(n->emac.octet, macaddr->octet, ETH_ALEN) == 0)) {
5793 zvni_neigh_uninstall(zvni, n);
5794 zvni_neigh_del(zvni, n);
fe697c6b 5795 zvni_deref_ip2mac(zvni, mac);
f07e1c99 5796 }
5797 } else {
67fb9374
CS
5798 /* DAD: when MAC is freeze state as remote learn event,
5799 * remote mac-ip delete event is received will result in freeze
5800 * entry removal, first fetch kernel for the same entry present
5801 * as LOCAL and reachable, avoid deleting this entry instead
5802 * use kerenel local entry to update during unfreeze time.
5803 */
5804 if (zvrf->dad_freeze &&
5805 CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE) &&
5806 CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
5807 if (IS_ZEBRA_DEBUG_VXLAN)
5808 zlog_debug("%s: MAC %s (flags 0x%x) is remote and duplicate, read kernel for local entry",
5809 __PRETTY_FUNCTION__,
5810 prefix_mac2str(macaddr, buf,
5811 sizeof(buf)),
5812 mac->flags);
5813 macfdb_read_specific_mac(zns, zif->brslave_info.br_if,
5814 macaddr, vxl->access_vlan);
5815 }
5816
f07e1c99 5817 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
5818 zvni_process_neigh_on_remote_mac_del(zvni, mac);
f3a930da
AK
5819 /*
5820 * the remote sequence number in the auto mac entry
5821 * needs to be reset to 0 as the mac entry may have
5822 * been removed on all VTEPs (including
5823 * the originating one)
5824 */
5825 mac->rem_seq = 0;
f07e1c99 5826
fe697c6b 5827 /* If all remote neighbors referencing a remote MAC
5828 * go away, we need to uninstall the MAC.
5829 */
5830 if (remote_neigh_count(mac) == 0) {
d63c1b18 5831 zvni_mac_uninstall(zvni, mac);
fe697c6b 5832 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
5833 }
5834 if (list_isempty(mac->neigh_list))
f07e1c99 5835 zvni_mac_del(zvni, mac);
fe697c6b 5836 else
f07e1c99 5837 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5838 }
5839 }
5840}
5841
5842
b7cfce93
MK
5843/* Public functions */
5844
c48d9f5f
MK
5845int is_l3vni_for_prefix_routes_only(vni_t vni)
5846{
5847 zebra_l3vni_t *zl3vni = NULL;
5848
5849 zl3vni = zl3vni_lookup(vni);
5850 if (!zl3vni)
5851 return 0;
5852
5853 return CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY) ? 1 : 0;
5854}
5855
2dbad57f 5856/* handle evpn route in vrf table */
996c9314
LB
5857void zebra_vxlan_evpn_vrf_route_add(vrf_id_t vrf_id, struct ethaddr *rmac,
5858 struct ipaddr *vtep_ip,
5859 struct prefix *host_prefix)
2dbad57f 5860{
5861 zebra_l3vni_t *zl3vni = NULL;
f50dc5e6 5862 struct ipaddr ipv4_vtep;
2dbad57f 5863
5864 zl3vni = zl3vni_from_vrf(vrf_id);
5865 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
5866 return;
5867
3518f352 5868 /*
f50dc5e6
MK
5869 * add the next hop neighbor -
5870 * neigh to be installed is the ipv6 nexthop neigh
5871 */
3518f352 5872 zl3vni_remote_nh_add(zl3vni, vtep_ip, rmac, host_prefix);
2dbad57f 5873
f50dc5e6
MK
5874 /*
5875 * if the remote vtep is a ipv4 mapped ipv6 address convert it to ipv4
5876 * address. Rmac is programmed against the ipv4 vtep because we only
5877 * support ipv4 tunnels in the h/w right now
5878 */
5879 memset(&ipv4_vtep, 0, sizeof(struct ipaddr));
5880 ipv4_vtep.ipa_type = IPADDR_V4;
5881 if (vtep_ip->ipa_type == IPADDR_V6)
5882 ipv4_mapped_ipv6_to_ipv4(&vtep_ip->ipaddr_v6,
5883 &(ipv4_vtep.ipaddr_v4));
5884 else
5885 memcpy(&(ipv4_vtep.ipaddr_v4), &vtep_ip->ipaddr_v4,
5886 sizeof(struct in_addr));
5887
3518f352
DS
5888 /*
5889 * add the rmac - remote rmac to be installed is against the ipv4
f50dc5e6
MK
5890 * nexthop address
5891 */
3518f352 5892 zl3vni_remote_rmac_add(zl3vni, rmac, &ipv4_vtep, host_prefix);
2dbad57f 5893}
5894
5895/* handle evpn vrf route delete */
22e63104 5896void zebra_vxlan_evpn_vrf_route_del(vrf_id_t vrf_id,
996c9314
LB
5897 struct ipaddr *vtep_ip,
5898 struct prefix *host_prefix)
2dbad57f 5899{
5900 zebra_l3vni_t *zl3vni = NULL;
22e63104 5901 zebra_neigh_t *nh = NULL;
5902 zebra_mac_t *zrmac = NULL;
2dbad57f 5903
5904 zl3vni = zl3vni_from_vrf(vrf_id);
5e06422c 5905 if (!zl3vni)
2dbad57f 5906 return;
5907
22e63104 5908 /* find the next hop entry and rmac entry */
5909 nh = zl3vni_nh_lookup(zl3vni, vtep_ip);
5910 if (!nh)
5911 return;
5912 zrmac = zl3vni_rmac_lookup(zl3vni, &nh->emac);
5913
2dbad57f 5914 /* delete the next hop entry */
22e63104 5915 zl3vni_remote_nh_del(zl3vni, nh, host_prefix);
2dbad57f 5916
5917 /* delete the rmac entry */
22e63104 5918 if (zrmac)
5919 zl3vni_remote_rmac_del(zl3vni, zrmac, host_prefix);
5920
2dbad57f 5921}
5922
996c9314 5923void zebra_vxlan_print_specific_rmac_l3vni(struct vty *vty, vni_t l3vni,
9f049418 5924 struct ethaddr *rmac, bool use_json)
9aa741ea
MK
5925{
5926 zebra_l3vni_t *zl3vni = NULL;
5927 zebra_mac_t *zrmac = NULL;
316f4ca4 5928 json_object *json = NULL;
9aa741ea 5929
316f4ca4
MK
5930 if (!is_evpn_enabled()) {
5931 if (use_json)
5932 vty_out(vty, "{}\n");
9aa741ea 5933 return;
316f4ca4
MK
5934 }
5935
5936 if (use_json)
5937 json = json_object_new_object();
9aa741ea
MK
5938
5939 zl3vni = zl3vni_lookup(l3vni);
5940 if (!zl3vni) {
316f4ca4
MK
5941 if (use_json)
5942 vty_out(vty, "{}\n");
5943 else
0437e105 5944 vty_out(vty, "%% L3-VNI %u doesn't exist\n", l3vni);
9aa741ea
MK
5945 return;
5946 }
5947
5948 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
5949 if (!zrmac) {
316f4ca4
MK
5950 if (use_json)
5951 vty_out(vty, "{}\n");
5952 else
5953 vty_out(vty,
0437e105 5954 "%% Requested RMAC doesn't exist in L3-VNI %u",
316f4ca4 5955 l3vni);
9aa741ea
MK
5956 return;
5957 }
5958
316f4ca4
MK
5959 zl3vni_print_rmac(zrmac, vty, json);
5960
5961 if (use_json) {
5962 vty_out(vty, "%s\n", json_object_to_json_string_ext(
5963 json, JSON_C_TO_STRING_PRETTY));
5964 json_object_free(json);
5965 }
9aa741ea 5966}
2dbad57f 5967
9f049418 5968void zebra_vxlan_print_rmacs_l3vni(struct vty *vty, vni_t l3vni, bool use_json)
b7cfce93
MK
5969{
5970 zebra_l3vni_t *zl3vni;
d7c0a89a 5971 uint32_t num_rmacs;
b7cfce93
MK
5972 struct rmac_walk_ctx wctx;
5973 json_object *json = NULL;
b7cfce93
MK
5974
5975 if (!is_evpn_enabled())
5976 return;
5977
5978 zl3vni = zl3vni_lookup(l3vni);
5979 if (!zl3vni) {
5980 if (use_json)
5981 vty_out(vty, "{}\n");
5982 else
5983 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
5984 return;
5985 }
5986 num_rmacs = hashcount(zl3vni->rmac_table);
5987 if (!num_rmacs)
5988 return;
5989
75223c9e 5990 if (use_json)
b7cfce93 5991 json = json_object_new_object();
b7cfce93
MK
5992
5993 memset(&wctx, 0, sizeof(struct rmac_walk_ctx));
5994 wctx.vty = vty;
75223c9e 5995 wctx.json = json;
b7cfce93 5996 if (!use_json) {
996c9314 5997 vty_out(vty, "Number of Remote RMACs known for this VNI: %u\n",
b7cfce93 5998 num_rmacs);
4cce389e 5999 vty_out(vty, "%-17s %-21s\n", "MAC", "Remote VTEP");
b7cfce93
MK
6000 } else
6001 json_object_int_add(json, "numRmacs", num_rmacs);
6002
6003 hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
6004
6005 if (use_json) {
b7cfce93
MK
6006 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6007 json, JSON_C_TO_STRING_PRETTY));
6008 json_object_free(json);
6009 }
6010}
6011
9f049418 6012void zebra_vxlan_print_rmacs_all_l3vni(struct vty *vty, bool use_json)
b7cfce93 6013{
b7cfce93 6014 json_object *json = NULL;
c0b4eaa4 6015 void *args[2];
b7cfce93
MK
6016
6017 if (!is_evpn_enabled()) {
6018 if (use_json)
6019 vty_out(vty, "{}\n");
6020 return;
6021 }
6022
b7cfce93
MK
6023 if (use_json)
6024 json = json_object_new_object();
6025
c0b4eaa4
MK
6026 args[0] = vty;
6027 args[1] = json;
89272910 6028 hash_iterate(zrouter.l3vni_table,
e3b78da8 6029 (void (*)(struct hash_bucket *,
c0b4eaa4
MK
6030 void *))zl3vni_print_rmac_hash_all_vni,
6031 args);
b7cfce93
MK
6032
6033 if (use_json) {
6034 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6035 json, JSON_C_TO_STRING_PRETTY));
6036 json_object_free(json);
6037 }
6038}
6039
996c9314 6040void zebra_vxlan_print_specific_nh_l3vni(struct vty *vty, vni_t l3vni,
9f049418 6041 struct ipaddr *ip, bool use_json)
9aa741ea
MK
6042{
6043 zebra_l3vni_t *zl3vni = NULL;
6044 zebra_neigh_t *n = NULL;
c0e519d3 6045 json_object *json = NULL;
9aa741ea 6046
c0e519d3
MK
6047 if (!is_evpn_enabled()) {
6048 if (use_json)
6049 vty_out(vty, "{}\n");
9aa741ea 6050 return;
c0e519d3
MK
6051 }
6052
6053 if (use_json)
6054 json = json_object_new_object();
9aa741ea
MK
6055
6056 zl3vni = zl3vni_lookup(l3vni);
6057 if (!zl3vni) {
c0e519d3
MK
6058 if (use_json)
6059 vty_out(vty, "{}\n");
6060 else
6061 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
9aa741ea
MK
6062 return;
6063 }
6064
6065 n = zl3vni_nh_lookup(zl3vni, ip);
6066 if (!n) {
c0e519d3
MK
6067 if (use_json)
6068 vty_out(vty, "{}\n");
6069 else
6070 vty_out(vty,
6071 "%% Requested next-hop not present for L3-VNI %u",
6072 l3vni);
9aa741ea
MK
6073 return;
6074 }
6075
c0e519d3
MK
6076 zl3vni_print_nh(n, vty, json);
6077
6078 if (use_json) {
6079 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6080 json, JSON_C_TO_STRING_PRETTY));
6081 json_object_free(json);
6082 }
9aa741ea
MK
6083}
6084
9f049418 6085void zebra_vxlan_print_nh_l3vni(struct vty *vty, vni_t l3vni, bool use_json)
b7cfce93 6086{
d7c0a89a 6087 uint32_t num_nh;
2dbad57f 6088 struct nh_walk_ctx wctx;
b7cfce93 6089 json_object *json = NULL;
b7cfce93
MK
6090 zebra_l3vni_t *zl3vni = NULL;
6091
6092 if (!is_evpn_enabled())
6093 return;
6094
6095 zl3vni = zl3vni_lookup(l3vni);
6096 if (!zl3vni) {
6097 if (use_json)
6098 vty_out(vty, "{}\n");
6099 else
6100 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
6101 return;
6102 }
6103
6104 num_nh = hashcount(zl3vni->nh_table);
6105 if (!num_nh)
6106 return;
6107
9187f600 6108 if (use_json)
b7cfce93 6109 json = json_object_new_object();
b7cfce93 6110
2dbad57f 6111 wctx.vty = vty;
9187f600 6112 wctx.json = json;
b7cfce93 6113 if (!use_json) {
996c9314 6114 vty_out(vty, "Number of NH Neighbors known for this VNI: %u\n",
b7cfce93 6115 num_nh);
4cce389e 6116 vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
b7cfce93 6117 } else
4cce389e 6118 json_object_int_add(json, "numNextHops", num_nh);
b7cfce93
MK
6119
6120 hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
6121
6122 if (use_json) {
b7cfce93
MK
6123 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6124 json, JSON_C_TO_STRING_PRETTY));
6125 json_object_free(json);
6126 }
6127}
6128
9f049418 6129void zebra_vxlan_print_nh_all_l3vni(struct vty *vty, bool use_json)
b7cfce93 6130{
2dbad57f 6131 json_object *json = NULL;
32798965 6132 void *args[2];
2dbad57f 6133
6134 if (!is_evpn_enabled()) {
6135 if (use_json)
6136 vty_out(vty, "{}\n");
6137 return;
6138 }
6139
2dbad57f 6140 if (use_json)
6141 json = json_object_new_object();
6142
32798965
MK
6143 args[0] = vty;
6144 args[1] = json;
89272910 6145 hash_iterate(zrouter.l3vni_table,
e3b78da8 6146 (void (*)(struct hash_bucket *,
32798965
MK
6147 void *))zl3vni_print_nh_hash_all_vni,
6148 args);
2dbad57f 6149
6150 if (use_json) {
6151 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6152 json, JSON_C_TO_STRING_PRETTY));
6153 json_object_free(json);
6154 }
b7cfce93
MK
6155}
6156
6157/*
6158 * Display L3 VNI information (VTY command handler).
6159 */
9f049418 6160void zebra_vxlan_print_l3vni(struct vty *vty, vni_t vni, bool use_json)
b7cfce93
MK
6161{
6162 void *args[2];
6163 json_object *json = NULL;
6164 zebra_l3vni_t *zl3vni = NULL;
6165
b2ee5a13
MK
6166 if (!is_evpn_enabled()) {
6167 if (use_json)
6168 vty_out(vty, "{}\n");
b7cfce93 6169 return;
b2ee5a13 6170 }
b7cfce93
MK
6171
6172 zl3vni = zl3vni_lookup(vni);
6173 if (!zl3vni) {
6174 if (use_json)
6175 vty_out(vty, "{}\n");
6176 else
6177 vty_out(vty, "%% VNI %u does not exist\n", vni);
6178 return;
6179 }
6180
6181 if (use_json)
6182 json = json_object_new_object();
6183
b2ee5a13
MK
6184 args[0] = vty;
6185 args[1] = json;
b7cfce93
MK
6186 zl3vni_print(zl3vni, (void *)args);
6187
6188 if (use_json) {
6189 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6190 json, JSON_C_TO_STRING_PRETTY));
6191 json_object_free(json);
6192 }
6193}
6194
4cce389e
MK
6195void zebra_vxlan_print_vrf_vni(struct vty *vty, struct zebra_vrf *zvrf,
6196 json_object *json_vrfs)
b7cfce93 6197{
4cce389e
MK
6198 char buf[ETHER_ADDR_STRLEN];
6199 zebra_l3vni_t *zl3vni = NULL;
b7cfce93 6200
4cce389e
MK
6201 zl3vni = zl3vni_lookup(zvrf->l3vni);
6202 if (!zl3vni)
b7cfce93 6203 return;
b7cfce93 6204
4cce389e
MK
6205 if (!json_vrfs) {
6206 vty_out(vty, "%-37s %-10u %-20s %-20s %-5s %-18s\n",
996c9314 6207 zvrf_name(zvrf), zl3vni->vni,
4cce389e 6208 zl3vni_vxlan_if_name(zl3vni),
996c9314 6209 zl3vni_svi_if_name(zl3vni), zl3vni_state2str(zl3vni),
4cce389e 6210 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
b7cfce93 6211 } else {
4cce389e 6212 json_object *json_vrf = NULL;
9df2b997 6213
4cce389e 6214 json_vrf = json_object_new_object();
996c9314 6215 json_object_string_add(json_vrf, "vrf", zvrf_name(zvrf));
4cce389e
MK
6216 json_object_int_add(json_vrf, "vni", zl3vni->vni);
6217 json_object_string_add(json_vrf, "vxlanIntf",
6218 zl3vni_vxlan_if_name(zl3vni));
6219 json_object_string_add(json_vrf, "sviIntf",
6220 zl3vni_svi_if_name(zl3vni));
6221 json_object_string_add(json_vrf, "state",
6222 zl3vni_state2str(zl3vni));
996c9314
LB
6223 json_object_string_add(
6224 json_vrf, "routerMac",
6225 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
4cce389e 6226 json_object_array_add(json_vrfs, json_vrf);
b7cfce93
MK
6227 }
6228}
6229
6230/*
6231 * Display Neighbors for a VNI (VTY command handler).
6232 */
6233void zebra_vxlan_print_neigh_vni(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 6234 vni_t vni, bool use_json)
b7cfce93
MK
6235{
6236 zebra_vni_t *zvni;
d7c0a89a 6237 uint32_t num_neigh;
b7cfce93
MK
6238 struct neigh_walk_ctx wctx;
6239 json_object *json = NULL;
6240
6241 if (!is_evpn_enabled())
6242 return;
6243 zvni = zvni_lookup(vni);
6244 if (!zvni) {
6245 if (use_json)
6246 vty_out(vty, "{}\n");
6247 else
6248 vty_out(vty, "%% VNI %u does not exist\n", vni);
6249 return;
6250 }
6251 num_neigh = hashcount(zvni->neigh_table);
6252 if (!num_neigh)
6253 return;
6254
6255 if (use_json)
6256 json = json_object_new_object();
6257
6258 /* Since we have IPv6 addresses to deal with which can vary widely in
6259 * size, we try to be a bit more elegant in display by first computing
6260 * the maximum width.
6261 */
6262 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
6263 wctx.zvni = zvni;
6264 wctx.vty = vty;
6265 wctx.addr_width = 15;
6266 wctx.json = json;
6267 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
6268
6269 if (!use_json) {
6270 vty_out(vty,
6271 "Number of ARPs (local and remote) known for this VNI: %u\n",
6272 num_neigh);
40e0224a
NS
6273 vty_out(vty, "%*s %-6s %-8s %-17s %-21s %s\n", -wctx.addr_width,
6274 "IP", "Type", "State", "MAC", "Remote VTEP", "Seq #'s");
b7cfce93
MK
6275 } else
6276 json_object_int_add(json, "numArpNd", num_neigh);
6277
6278 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
6279 if (use_json) {
6280 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6281 json, JSON_C_TO_STRING_PRETTY));
6282 json_object_free(json);
6283 }
6284}
6285
6286/*
6287 * Display neighbors across all VNIs (VTY command handler).
6288 */
6289void zebra_vxlan_print_neigh_all_vni(struct vty *vty, struct zebra_vrf *zvrf,
1374d4db 6290 bool print_dup, bool use_json)
b7cfce93
MK
6291{
6292 json_object *json = NULL;
1374d4db 6293 void *args[3];
b7cfce93
MK
6294
6295 if (!is_evpn_enabled())
6296 return;
6297
6298 if (use_json)
6299 json = json_object_new_object();
6300
6301 args[0] = vty;
6302 args[1] = json;
1374d4db
CS
6303 args[2] = (void *)(ptrdiff_t)print_dup;
6304
b7cfce93 6305 hash_iterate(zvrf->vni_table,
e3b78da8 6306 (void (*)(struct hash_bucket *,
b7cfce93
MK
6307 void *))zvni_print_neigh_hash_all_vni,
6308 args);
6309 if (use_json) {
6310 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6311 json, JSON_C_TO_STRING_PRETTY));
6312 json_object_free(json);
6313 }
6314}
6315
e3fac919
NS
6316/*
6317 * Display neighbors across all VNIs in detail(VTY command handler).
6318 */
6319void zebra_vxlan_print_neigh_all_vni_detail(struct vty *vty,
6320 struct zebra_vrf *zvrf,
6321 bool print_dup, bool use_json)
6322{
6323 json_object *json = NULL;
6324 void *args[3];
6325
6326 if (!is_evpn_enabled())
6327 return;
6328
6329 if (use_json)
6330 json = json_object_new_object();
6331
6332 args[0] = vty;
6333 args[1] = json;
6334 args[2] = (void *)(ptrdiff_t)print_dup;
6335
6336 hash_iterate(zvrf->vni_table,
e3b78da8 6337 (void (*)(struct hash_bucket *,
e3fac919
NS
6338 void *))zvni_print_neigh_hash_all_vni_detail,
6339 args);
6340 if (use_json) {
6341 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6342 json, JSON_C_TO_STRING_PRETTY));
6343 json_object_free(json);
6344 }
6345}
6346
b7cfce93
MK
6347/*
6348 * Display specific neighbor for a VNI, if present (VTY command handler).
6349 */
6350void zebra_vxlan_print_specific_neigh_vni(struct vty *vty,
6351 struct zebra_vrf *zvrf, vni_t vni,
9f049418 6352 struct ipaddr *ip, bool use_json)
b7cfce93
MK
6353{
6354 zebra_vni_t *zvni;
6355 zebra_neigh_t *n;
6356 json_object *json = NULL;
6357
6358 if (!is_evpn_enabled())
6359 return;
6360 zvni = zvni_lookup(vni);
6361 if (!zvni) {
6362 if (use_json)
cd233079
CS
6363 vty_out(vty, "{}\n");
6364 else
6365 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 6366 return;
6367 }
6368 n = zvni_neigh_lookup(zvni, ip);
6369 if (!n) {
cd233079
CS
6370 if (!use_json)
6371 vty_out(vty,
6372 "%% Requested neighbor does not exist in VNI %u\n",
6373 vni);
d62a17ae 6374 return;
6375 }
cd233079
CS
6376 if (use_json)
6377 json = json_object_new_object();
6378
6379 zvni_print_neigh(n, vty, json);
cec2e17d 6380
cd233079
CS
6381 if (use_json) {
6382 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6383 json, JSON_C_TO_STRING_PRETTY));
6384 json_object_free(json);
6385 }
cec2e17d 6386}
6387
6388/*
6389 * Display neighbors for a VNI from specific VTEP (VTY command handler).
6390 * By definition, these are remote neighbors.
6391 */
d62a17ae 6392void zebra_vxlan_print_neigh_vni_vtep(struct vty *vty, struct zebra_vrf *zvrf,
cd233079 6393 vni_t vni, struct in_addr vtep_ip,
9f049418 6394 bool use_json)
cec2e17d 6395{
d62a17ae 6396 zebra_vni_t *zvni;
d7c0a89a 6397 uint32_t num_neigh;
d62a17ae 6398 struct neigh_walk_ctx wctx;
cd233079 6399 json_object *json = NULL;
cec2e17d 6400
2853fed6 6401 if (!is_evpn_enabled())
d62a17ae 6402 return;
2853fed6 6403 zvni = zvni_lookup(vni);
d62a17ae 6404 if (!zvni) {
cd233079
CS
6405 if (use_json)
6406 vty_out(vty, "{}\n");
6407 else
6408 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 6409 return;
6410 }
6411 num_neigh = hashcount(zvni->neigh_table);
6412 if (!num_neigh)
6413 return;
cec2e17d 6414
d62a17ae 6415 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
6416 wctx.zvni = zvni;
6417 wctx.vty = vty;
68e33151 6418 wctx.addr_width = 15;
d62a17ae 6419 wctx.flags = SHOW_REMOTE_NEIGH_FROM_VTEP;
6420 wctx.r_vtep_ip = vtep_ip;
cd233079 6421 wctx.json = json;
68e33151 6422 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
d62a17ae 6423 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
cd233079
CS
6424
6425 if (use_json) {
6426 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6427 json, JSON_C_TO_STRING_PRETTY));
6428 json_object_free(json);
6429 }
cec2e17d 6430}
6431
1374d4db
CS
6432/*
6433 * Display Duplicate detected Neighbors for a VNI
6434 * (VTY command handler).
6435 */
6436void zebra_vxlan_print_neigh_vni_dad(struct vty *vty,
6437 struct zebra_vrf *zvrf,
6438 vni_t vni,
6439 bool use_json)
6440{
6441 zebra_vni_t *zvni;
6442 uint32_t num_neigh;
6443 struct neigh_walk_ctx wctx;
6444 json_object *json = NULL;
6445
6446 if (!is_evpn_enabled())
6447 return;
6448
6449 zvni = zvni_lookup(vni);
6450 if (!zvni) {
6451 vty_out(vty, "%% VNI %u does not exist\n", vni);
6452 return;
6453 }
6454
6455 num_neigh = hashcount(zvni->neigh_table);
6456 if (!num_neigh)
6457 return;
6458
6459 num_neigh = num_dup_detected_neighs(zvni);
6460 if (!num_neigh)
6461 return;
6462
6463 if (use_json)
6464 json = json_object_new_object();
6465
6466 /* Since we have IPv6 addresses to deal with which can vary widely in
6467 * size, we try to be a bit more elegant in display by first computing
6468 * the maximum width.
6469 */
6470 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
6471 wctx.zvni = zvni;
6472 wctx.vty = vty;
6473 wctx.addr_width = 15;
6474 wctx.json = json;
6475 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
6476
6477 if (!use_json) {
6478 vty_out(vty,
6479 "Number of ARPs (local and remote) known for this VNI: %u\n",
6480 num_neigh);
6481 vty_out(vty, "%*s %-6s %-8s %-17s %-21s\n",
6482 -wctx.addr_width, "IP", "Type",
6483 "State", "MAC", "Remote VTEP");
6484 } else
6485 json_object_int_add(json, "numArpNd", num_neigh);
6486
6487 hash_iterate(zvni->neigh_table, zvni_print_dad_neigh_hash, &wctx);
6488
6489 if (use_json) {
6490 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6491 json, JSON_C_TO_STRING_PRETTY));
6492 json_object_free(json);
6493 }
6494}
6495
cec2e17d 6496/*
6497 * Display MACs for a VNI (VTY command handler).
6498 */
d62a17ae 6499void zebra_vxlan_print_macs_vni(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 6500 vni_t vni, bool use_json)
cec2e17d 6501{
d62a17ae 6502 zebra_vni_t *zvni;
d7c0a89a 6503 uint32_t num_macs;
d62a17ae 6504 struct mac_walk_ctx wctx;
cd233079
CS
6505 json_object *json = NULL;
6506 json_object *json_mac = NULL;
cec2e17d 6507
2853fed6 6508 if (!is_evpn_enabled())
d62a17ae 6509 return;
2853fed6 6510 zvni = zvni_lookup(vni);
d62a17ae 6511 if (!zvni) {
cd233079
CS
6512 if (use_json)
6513 vty_out(vty, "{}\n");
6514 else
6515 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 6516 return;
6517 }
790f8dc5 6518 num_macs = num_valid_macs(zvni);
d62a17ae 6519 if (!num_macs)
6520 return;
cec2e17d 6521
cd233079
CS
6522 if (use_json) {
6523 json = json_object_new_object();
6524 json_mac = json_object_new_object();
6525 }
6526
d62a17ae 6527 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6528 wctx.zvni = zvni;
6529 wctx.vty = vty;
cd233079 6530 wctx.json = json_mac;
cec2e17d 6531
cd233079
CS
6532 if (!use_json) {
6533 vty_out(vty,
6534 "Number of MACs (local and remote) known for this VNI: %u\n",
6535 num_macs);
40e0224a
NS
6536 vty_out(vty, "%-17s %-6s %-21s %-5s %s\n", "MAC", "Type",
6537 "Intf/Remote VTEP", "VLAN", "Seq #'s");
cd233079
CS
6538 } else
6539 json_object_int_add(json, "numMacs", num_macs);
cec2e17d 6540
d62a17ae 6541 hash_iterate(zvni->mac_table, zvni_print_mac_hash, &wctx);
cd233079
CS
6542
6543 if (use_json) {
6544 json_object_object_add(json, "macs", json_mac);
6545 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6546 json, JSON_C_TO_STRING_PRETTY));
6547 json_object_free(json);
6548 }
cec2e17d 6549}
6550
6551/*
6552 * Display MACs for all VNIs (VTY command handler).
6553 */
cd233079 6554void zebra_vxlan_print_macs_all_vni(struct vty *vty, struct zebra_vrf *zvrf,
1374d4db 6555 bool print_dup, bool use_json)
cec2e17d 6556{
d62a17ae 6557 struct mac_walk_ctx wctx;
cd233079 6558 json_object *json = NULL;
cec2e17d 6559
2853fed6 6560 if (!is_evpn_enabled()) {
cd233079
CS
6561 if (use_json)
6562 vty_out(vty, "{}\n");
d62a17ae 6563 return;
cd233079
CS
6564 }
6565 if (use_json)
6566 json = json_object_new_object();
6567
d62a17ae 6568 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6569 wctx.vty = vty;
cd233079 6570 wctx.json = json;
1374d4db 6571 wctx.print_dup = print_dup;
d62a17ae 6572 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni, &wctx);
cd233079
CS
6573
6574 if (use_json) {
6575 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6576 json, JSON_C_TO_STRING_PRETTY));
6577 json_object_free(json);
6578 }
cec2e17d 6579}
6580
cffe7580
NS
6581/*
6582 * Display MACs in detail for all VNIs (VTY command handler).
6583 */
6584void zebra_vxlan_print_macs_all_vni_detail(struct vty *vty,
6585 struct zebra_vrf *zvrf,
6586 bool print_dup, bool use_json)
6587{
6588 struct mac_walk_ctx wctx;
6589 json_object *json = NULL;
6590
6591 if (!is_evpn_enabled()) {
6592 if (use_json)
6593 vty_out(vty, "{}\n");
6594 return;
6595 }
6596 if (use_json)
6597 json = json_object_new_object();
6598
6599 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6600 wctx.vty = vty;
6601 wctx.json = json;
6602 wctx.print_dup = print_dup;
6603 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni_detail,
6604 &wctx);
6605
6606 if (use_json) {
6607 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6608 json, JSON_C_TO_STRING_PRETTY));
6609 json_object_free(json);
6610 }
6611}
6612
cec2e17d 6613/*
6614 * Display MACs for all VNIs (VTY command handler).
6615 */
d62a17ae 6616void zebra_vxlan_print_macs_all_vni_vtep(struct vty *vty,
6617 struct zebra_vrf *zvrf,
9f049418 6618 struct in_addr vtep_ip, bool use_json)
cec2e17d 6619{
d62a17ae 6620 struct mac_walk_ctx wctx;
cd233079 6621 json_object *json = NULL;
cec2e17d 6622
2853fed6 6623 if (!is_evpn_enabled())
d62a17ae 6624 return;
cd233079
CS
6625
6626 if (use_json)
6627 json = json_object_new_object();
6628
d62a17ae 6629 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6630 wctx.vty = vty;
6631 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
6632 wctx.r_vtep_ip = vtep_ip;
cd233079 6633 wctx.json = json;
d62a17ae 6634 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni, &wctx);
cd233079
CS
6635
6636 if (use_json) {
6637 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6638 json, JSON_C_TO_STRING_PRETTY));
6639 json_object_free(json);
6640 }
cec2e17d 6641}
6642
6643/*
6644 * Display specific MAC for a VNI, if present (VTY command handler).
6645 */
d62a17ae 6646void zebra_vxlan_print_specific_mac_vni(struct vty *vty, struct zebra_vrf *zvrf,
24cdbd0d
DS
6647 vni_t vni, struct ethaddr *macaddr,
6648 bool use_json)
cec2e17d 6649{
d62a17ae 6650 zebra_vni_t *zvni;
6651 zebra_mac_t *mac;
24cdbd0d 6652 json_object *json = NULL;
cec2e17d 6653
2853fed6 6654 if (!is_evpn_enabled())
d62a17ae 6655 return;
24cdbd0d 6656
2853fed6 6657 zvni = zvni_lookup(vni);
d62a17ae 6658 if (!zvni) {
24cdbd0d
DS
6659 if (use_json)
6660 vty_out(vty, "{}\n");
6661 else
6662 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 6663 return;
6664 }
6665 mac = zvni_mac_lookup(zvni, macaddr);
6666 if (!mac) {
24cdbd0d
DS
6667 if (use_json)
6668 vty_out(vty, "{}\n");
6669 else
6670 vty_out(vty,
6671 "%% Requested MAC does not exist in VNI %u\n",
6672 vni);
d62a17ae 6673 return;
6674 }
cec2e17d 6675
24cdbd0d
DS
6676 if (use_json)
6677 json = json_object_new_object();
6678
6679 zvni_print_mac(mac, vty, json);
cffe7580
NS
6680 if (use_json) {
6681 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6682 json, JSON_C_TO_STRING_PRETTY));
6683 json_object_free(json);
6684 }
cec2e17d 6685}
6686
1374d4db
CS
6687/* Print Duplicate MACs per VNI */
6688void zebra_vxlan_print_macs_vni_dad(struct vty *vty,
6689 struct zebra_vrf *zvrf,
6690 vni_t vni, bool use_json)
6691{
6692 zebra_vni_t *zvni;
6693 struct mac_walk_ctx wctx;
6694 uint32_t num_macs;
6695 json_object *json = NULL;
6696 json_object *json_mac = NULL;
6697
6698 if (!is_evpn_enabled())
6699 return;
6700
6701 zvni = zvni_lookup(vni);
6702 if (!zvni) {
6703 vty_out(vty, "%% VNI %u does not exist\n", vni);
6704 return;
6705 }
6706
6707 num_macs = num_valid_macs(zvni);
6708 if (!num_macs)
6709 return;
6710
6711 num_macs = num_dup_detected_macs(zvni);
6712 if (!num_macs)
6713 return;
6714
6715 if (use_json) {
6716 json = json_object_new_object();
6717 json_mac = json_object_new_object();
6718 }
6719
6720 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6721 wctx.zvni = zvni;
6722 wctx.vty = vty;
6723 wctx.json = json_mac;
6724
6725 if (!use_json) {
6726 vty_out(vty,
6727 "Number of MACs (local and remote) known for this VNI: %u\n",
6728 num_macs);
6729 vty_out(vty, "%-17s %-6s %-21s %-5s\n", "MAC", "Type",
6730 "Intf/Remote VTEP", "VLAN");
6731 } else
6732 json_object_int_add(json, "numMacs", num_macs);
6733
6734 hash_iterate(zvni->mac_table, zvni_print_dad_mac_hash, &wctx);
6735
6736 if (use_json) {
6737 json_object_object_add(json, "macs", json_mac);
6738 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6739 json, JSON_C_TO_STRING_PRETTY));
6740 json_object_free(json);
6741 }
6742
6743}
6744
1883de66
CS
6745int zebra_vxlan_clear_dup_detect_vni_mac(struct vty *vty,
6746 struct zebra_vrf *zvrf,
6747 vni_t vni, struct ethaddr *macaddr)
09de9258
CS
6748{
6749 zebra_vni_t *zvni;
6750 zebra_mac_t *mac;
6751 struct listnode *node = NULL;
6752 zebra_neigh_t *nbr = NULL;
6753
6754 if (!is_evpn_enabled())
1883de66
CS
6755 return CMD_SUCCESS;
6756
09de9258
CS
6757 zvni = zvni_lookup(vni);
6758 if (!zvni) {
6759 vty_out(vty, "%% VNI %u does not exist\n", vni);
1883de66 6760 return CMD_WARNING;
09de9258
CS
6761 }
6762
6763 mac = zvni_mac_lookup(zvni, macaddr);
6764 if (!mac) {
6765 vty_out(vty, "%% Requested MAC does not exist in VNI %u\n",
6766 vni);
1883de66 6767 return CMD_WARNING;
09de9258
CS
6768 }
6769
6770 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
6771 vty_out(vty, "%% Requested MAC is not duplicate detected\n");
1883de66 6772 return CMD_WARNING;
09de9258
CS
6773 }
6774
6775 /* Remove all IPs as duplicate associcated with this MAC */
6776 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
6777 /* For local neigh mark inactive so MACIP update is generated
6778 * to BGP. This is a scenario where MAC update received
6779 * and detected as duplicate which marked neigh as duplicate.
6780 * Later local neigh update did not get a chance to relay
6781 * to BGP. Similarly remote macip update, neigh needs to be
6782 * installed locally.
6783 */
d4199657
CS
6784 if (zvrf->dad_freeze &&
6785 CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
09de9258
CS
6786 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
6787 ZEBRA_NEIGH_SET_INACTIVE(nbr);
6788 else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE))
6789 zvni_neigh_install(zvni, nbr);
6790 }
6791
6792 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
6793 nbr->dad_count = 0;
6794 nbr->detect_start_time.tv_sec = 0;
6795 nbr->dad_dup_detect_time = 0;
6796 }
6797
6798 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
6799 mac->dad_count = 0;
6800 mac->detect_start_time.tv_sec = 0;
6801 mac->detect_start_time.tv_usec = 0;
6802 mac->dad_dup_detect_time = 0;
6803 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
6804
d4199657
CS
6805 /* warn-only action return */
6806 if (!zvrf->dad_freeze)
6807 return CMD_SUCCESS;
6808
09de9258
CS
6809 /* Local: Notify Peer VTEPs, Remote: Install the entry */
6810 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
6811 /* Inform to BGP */
6812 if (zvni_mac_send_add_to_client(zvni->vni,
6813 &mac->macaddr,
6814 mac->flags,
6815 mac->loc_seq))
1883de66 6816 return CMD_SUCCESS;
09de9258
CS
6817
6818 /* Process all neighbors associated with this MAC. */
6819 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
6820
6821 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
6822 zvni_process_neigh_on_remote_mac_add(zvni, mac);
6823
6824 /* Install the entry. */
6825 zvni_mac_install(zvni, mac);
6826 }
6827
1883de66 6828 return CMD_SUCCESS;
09de9258
CS
6829}
6830
1883de66
CS
6831int zebra_vxlan_clear_dup_detect_vni_ip(struct vty *vty,
6832 struct zebra_vrf *zvrf,
6833 vni_t vni, struct ipaddr *ip)
09de9258
CS
6834{
6835 zebra_vni_t *zvni;
6836 zebra_neigh_t *nbr;
6837 zebra_mac_t *mac;
6838 char buf[INET6_ADDRSTRLEN];
6839 char buf2[ETHER_ADDR_STRLEN];
6840
6841 if (!is_evpn_enabled())
1883de66 6842 return CMD_SUCCESS;
09de9258
CS
6843
6844 zvni = zvni_lookup(vni);
6845 if (!zvni) {
6846 vty_out(vty, "%% VNI %u does not exist\n", vni);
1883de66 6847 return CMD_WARNING;
09de9258
CS
6848 }
6849
6850 nbr = zvni_neigh_lookup(zvni, ip);
6851 if (!nbr) {
6852 vty_out(vty,
6853 "%% Requested host IP does not exist in VNI %u\n",
6854 vni);
1883de66 6855 return CMD_WARNING;
09de9258
CS
6856 }
6857
6858 ipaddr2str(&nbr->ip, buf, sizeof(buf));
6859
6860 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
6861 vty_out(vty,
f79f7a7b 6862 "%% Requested host IP %s is not duplicate detected\n",
09de9258 6863 buf);
1883de66 6864 return CMD_WARNING;
09de9258
CS
6865 }
6866
6867 mac = zvni_mac_lookup(zvni, &nbr->emac);
6868
6869 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
6870 vty_out(vty,
6871 "%% Requested IP's associated MAC %s is still in duplicate state\n",
6872 prefix_mac2str(&nbr->emac, buf2, sizeof(buf2)));
1883de66 6873 return CMD_WARNING_CONFIG_FAILED;
09de9258
CS
6874 }
6875
6876 if (IS_ZEBRA_DEBUG_VXLAN)
6877 zlog_debug("%s: clear neigh %s in dup state, flags 0x%x seq %u",
6878 __PRETTY_FUNCTION__, buf, nbr->flags,
6879 nbr->loc_seq);
6880
6881 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
6882 nbr->dad_count = 0;
6883 nbr->detect_start_time.tv_sec = 0;
6884 nbr->detect_start_time.tv_usec = 0;
6885 nbr->dad_dup_detect_time = 0;
6886 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
6887
6888 if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
6889 zvni_neigh_send_add_to_client(zvni->vni, ip,
6890 &nbr->emac,
6891 nbr->flags, nbr->loc_seq);
6892 } else if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
6893 zvni_neigh_install(zvni, nbr);
6894 }
6895
1883de66 6896 return CMD_SUCCESS;
09de9258
CS
6897}
6898
e3b78da8 6899static void zvni_clear_dup_mac_hash(struct hash_bucket *bucket, void *ctxt)
09de9258
CS
6900{
6901 struct mac_walk_ctx *wctx = ctxt;
6902 zebra_mac_t *mac;
6903 zebra_vni_t *zvni;
6904 struct listnode *node = NULL;
6905 zebra_neigh_t *nbr = NULL;
6906
e3b78da8 6907 mac = (zebra_mac_t *)bucket->data;
09de9258
CS
6908 if (!mac)
6909 return;
6910
6911 zvni = wctx->zvni;
6912
6913 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
6914 return;
6915
6916 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
6917 mac->dad_count = 0;
6918 mac->detect_start_time.tv_sec = 0;
6919 mac->detect_start_time.tv_usec = 0;
6920 mac->dad_dup_detect_time = 0;
6921 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
6922
6923 /* Remove all IPs as duplicate associcated with this MAC */
6924 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
6925 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)
6926 && nbr->dad_count)
6927 ZEBRA_NEIGH_SET_INACTIVE(nbr);
6928
6929 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
6930 nbr->dad_count = 0;
6931 nbr->detect_start_time.tv_sec = 0;
6932 nbr->dad_dup_detect_time = 0;
6933 }
6934
6935 /* Local: Notify Peer VTEPs, Remote: Install the entry */
6936 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
6937 /* Inform to BGP */
6938 if (zvni_mac_send_add_to_client(zvni->vni,
6939 &mac->macaddr,
6940 mac->flags, mac->loc_seq))
6941 return;
6942
6943 /* Process all neighbors associated with this MAC. */
6944 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
6945
6946 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
6947 zvni_process_neigh_on_remote_mac_add(zvni, mac);
6948
6949 /* Install the entry. */
6950 zvni_mac_install(zvni, mac);
6951 }
6952}
6953
e3b78da8 6954static void zvni_clear_dup_neigh_hash(struct hash_bucket *bucket, void *ctxt)
09de9258
CS
6955{
6956 struct neigh_walk_ctx *wctx = ctxt;
6957 zebra_neigh_t *nbr;
6958 zebra_vni_t *zvni;
6959 char buf[INET6_ADDRSTRLEN];
6960
e3b78da8 6961 nbr = (zebra_neigh_t *)bucket->data;
09de9258
CS
6962 if (!nbr)
6963 return;
6964
6965 zvni = wctx->zvni;
6966
6967 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
6968 return;
6969
6970 if (IS_ZEBRA_DEBUG_VXLAN) {
6971 ipaddr2str(&nbr->ip, buf, sizeof(buf));
6972 zlog_debug(
6973 "%s: clear neigh %s dup state, flags 0x%x seq %u",
6974 __PRETTY_FUNCTION__, buf,
6975 nbr->flags, nbr->loc_seq);
6976 }
6977
6978 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
6979 nbr->dad_count = 0;
6980 nbr->detect_start_time.tv_sec = 0;
6981 nbr->detect_start_time.tv_usec = 0;
6982 nbr->dad_dup_detect_time = 0;
6983 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
6984
6985 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
6986 zvni_neigh_send_add_to_client(zvni->vni, &nbr->ip,
6987 &nbr->emac,
6988 nbr->flags, nbr->loc_seq);
6989 } else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
6990 zvni_neigh_install(zvni, nbr);
6991 }
6992}
6993
e3b78da8 6994static void zvni_clear_dup_detect_hash_vni_all(struct hash_bucket *bucket,
09de9258
CS
6995 void **args)
6996{
6997 struct vty *vty;
6998 zebra_vni_t *zvni;
6999 struct zebra_vrf *zvrf;
7000 struct mac_walk_ctx m_wctx;
7001 struct neigh_walk_ctx n_wctx;
7002
e3b78da8 7003 zvni = (zebra_vni_t *)bucket->data;
09de9258
CS
7004 if (!zvni)
7005 return;
7006
7007 vty = (struct vty *)args[0];
7008 zvrf = (struct zebra_vrf *)args[1];
7009
7010 if (hashcount(zvni->neigh_table)) {
7011 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
7012 n_wctx.vty = vty;
7013 n_wctx.zvni = zvni;
7014 n_wctx.zvrf = zvrf;
7015 hash_iterate(zvni->neigh_table, zvni_clear_dup_neigh_hash,
7016 &n_wctx);
7017 }
7018
7019 if (num_valid_macs(zvni)) {
7020 memset(&m_wctx, 0, sizeof(struct mac_walk_ctx));
7021 m_wctx.zvni = zvni;
7022 m_wctx.vty = vty;
7023 m_wctx.zvrf = zvrf;
7024 hash_iterate(zvni->mac_table, zvni_clear_dup_mac_hash, &m_wctx);
7025 }
7026
7027}
7028
1883de66 7029int zebra_vxlan_clear_dup_detect_vni_all(struct vty *vty,
09de9258
CS
7030 struct zebra_vrf *zvrf)
7031{
7032 void *args[2];
7033
7034 if (!is_evpn_enabled())
1883de66 7035 return CMD_SUCCESS;
09de9258
CS
7036
7037 args[0] = vty;
7038 args[1] = zvrf;
7039
7040 hash_iterate(zvrf->vni_table,
e3b78da8 7041 (void (*)(struct hash_bucket *, void *))
09de9258
CS
7042 zvni_clear_dup_detect_hash_vni_all, args);
7043
1883de66 7044 return CMD_SUCCESS;
09de9258
CS
7045}
7046
1883de66 7047int zebra_vxlan_clear_dup_detect_vni(struct vty *vty,
09de9258
CS
7048 struct zebra_vrf *zvrf,
7049 vni_t vni)
7050{
7051 zebra_vni_t *zvni;
7052 struct mac_walk_ctx m_wctx;
7053 struct neigh_walk_ctx n_wctx;
7054
7055 if (!is_evpn_enabled())
1883de66 7056 return CMD_SUCCESS;
09de9258
CS
7057
7058 zvni = zvni_lookup(vni);
7059 if (!zvni) {
7060 vty_out(vty, "%% VNI %u does not exist\n", vni);
1883de66 7061 return CMD_WARNING;
09de9258
CS
7062 }
7063
7064 if (hashcount(zvni->neigh_table)) {
7065 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
7066 n_wctx.vty = vty;
7067 n_wctx.zvni = zvni;
7068 n_wctx.zvrf = zvrf;
7069 hash_iterate(zvni->neigh_table, zvni_clear_dup_neigh_hash,
7070 &n_wctx);
7071 }
7072
7073 if (num_valid_macs(zvni)) {
7074 memset(&m_wctx, 0, sizeof(struct mac_walk_ctx));
7075 m_wctx.zvni = zvni;
7076 m_wctx.vty = vty;
7077 m_wctx.zvrf = zvrf;
7078 hash_iterate(zvni->mac_table, zvni_clear_dup_mac_hash, &m_wctx);
7079 }
7080
1883de66 7081 return CMD_SUCCESS;
09de9258
CS
7082}
7083
cec2e17d 7084/*
7085 * Display MACs for a VNI from specific VTEP (VTY command handler).
7086 */
d62a17ae 7087void zebra_vxlan_print_macs_vni_vtep(struct vty *vty, struct zebra_vrf *zvrf,
cd233079 7088 vni_t vni, struct in_addr vtep_ip,
9f049418 7089 bool use_json)
cec2e17d 7090{
d62a17ae 7091 zebra_vni_t *zvni;
d7c0a89a 7092 uint32_t num_macs;
d62a17ae 7093 struct mac_walk_ctx wctx;
cd233079
CS
7094 json_object *json = NULL;
7095 json_object *json_mac = NULL;
cec2e17d 7096
2853fed6 7097 if (!is_evpn_enabled())
d62a17ae 7098 return;
2853fed6 7099 zvni = zvni_lookup(vni);
d62a17ae 7100 if (!zvni) {
cd233079
CS
7101 if (use_json)
7102 vty_out(vty, "{}\n");
7103 else
7104 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 7105 return;
7106 }
790f8dc5 7107 num_macs = num_valid_macs(zvni);
d62a17ae 7108 if (!num_macs)
7109 return;
cd233079
CS
7110
7111 if (use_json) {
7112 json = json_object_new_object();
7113 json_mac = json_object_new_object();
7114 }
7115
d62a17ae 7116 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
7117 wctx.zvni = zvni;
7118 wctx.vty = vty;
7119 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
7120 wctx.r_vtep_ip = vtep_ip;
cd233079 7121 wctx.json = json_mac;
d62a17ae 7122 hash_iterate(zvni->mac_table, zvni_print_mac_hash, &wctx);
cd233079
CS
7123
7124 if (use_json) {
7125 json_object_int_add(json, "numMacs", wctx.count);
7126 if (wctx.count)
7127 json_object_object_add(json, "macs", json_mac);
7128 vty_out(vty, "%s\n", json_object_to_json_string_ext(
7129 json, JSON_C_TO_STRING_PRETTY));
7130 json_object_free(json);
7131 }
cec2e17d 7132}
7133
7134
7135/*
7136 * Display VNI information (VTY command handler).
7137 */
cd233079 7138void zebra_vxlan_print_vni(struct vty *vty, struct zebra_vrf *zvrf, vni_t vni,
9f049418 7139 bool use_json)
cec2e17d 7140{
cd233079
CS
7141 json_object *json = NULL;
7142 void *args[2];
1f2129ec 7143 zebra_l3vni_t *zl3vni = NULL;
7144 zebra_vni_t *zvni = NULL;
cec2e17d 7145
2853fed6 7146 if (!is_evpn_enabled())
d62a17ae 7147 return;
4cce389e 7148
cd233079
CS
7149 if (use_json)
7150 json = json_object_new_object();
7151 args[0] = vty;
7152 args[1] = json;
4cce389e 7153
1f2129ec 7154 zl3vni = zl3vni_lookup(vni);
7155 if (zl3vni) {
4cce389e
MK
7156 zl3vni_print(zl3vni, (void *)args);
7157 } else {
4cce389e
MK
7158 zvni = zvni_lookup(vni);
7159 if (!zvni) {
7160 if (use_json)
7161 vty_out(vty, "{}\n");
7162 else
7163 vty_out(vty, "%% VNI %u does not exist\n", vni);
7164 return;
7165 }
7166
7167 zvni_print(zvni, (void *)args);
7168 }
7169
cd233079
CS
7170 if (use_json) {
7171 vty_out(vty, "%s\n", json_object_to_json_string_ext(
7172 json, JSON_C_TO_STRING_PRETTY));
7173 json_object_free(json);
7174 }
cec2e17d 7175}
7176
4cce389e 7177/* Display all global details for EVPN */
088f1098 7178void zebra_vxlan_print_evpn(struct vty *vty, bool uj)
cec2e17d 7179{
4cce389e
MK
7180 int num_l2vnis = 0;
7181 int num_l3vnis = 0;
d4454626 7182 int num_vnis = 0;
cd233079 7183 json_object *json = NULL;
4cce389e 7184 struct zebra_vrf *zvrf = NULL;
cec2e17d 7185
2853fed6 7186 if (!is_evpn_enabled())
d62a17ae 7187 return;
4cce389e 7188
530db8dc 7189 zvrf = zebra_vrf_get_evpn();
4cce389e
MK
7190 if (!zvrf)
7191 return;
7192
89272910 7193 num_l3vnis = hashcount(zrouter.l3vni_table);
4cce389e 7194 num_l2vnis = hashcount(zvrf->vni_table);
d4454626 7195 num_vnis = num_l2vnis + num_l3vnis;
4cce389e
MK
7196
7197 if (uj) {
cd233079 7198 json = json_object_new_object();
ddd16ed5
MK
7199 json_object_string_add(json, "advertiseGatewayMacip",
7200 zvrf->advertise_gw_macip ? "Yes" : "No");
d4454626 7201 json_object_int_add(json, "numVnis", num_vnis);
4cce389e
MK
7202 json_object_int_add(json, "numL2Vnis", num_l2vnis);
7203 json_object_int_add(json, "numL3Vnis", num_l3vnis);
61d46eda
CS
7204 if (zvrf->dup_addr_detect)
7205 json_object_boolean_true_add(json,
7206 "isDuplicateAddrDetection");
7207 else
7208 json_object_boolean_false_add(json,
7209 "isDuplicateAddrDetection");
7210 json_object_int_add(json, "maxMoves", zvrf->dad_max_moves);
7211 json_object_int_add(json, "detectionTime", zvrf->dad_time);
7212 json_object_int_add(json, "detectionFreezeTime",
7213 zvrf->dad_freeze_time);
7214
cd233079 7215 } else {
4cce389e
MK
7216 vty_out(vty, "L2 VNIs: %u\n", num_l2vnis);
7217 vty_out(vty, "L3 VNIs: %u\n", num_l3vnis);
ddd16ed5
MK
7218 vty_out(vty, "Advertise gateway mac-ip: %s\n",
7219 zvrf->advertise_gw_macip ? "Yes" : "No");
278e26de
CS
7220 vty_out(vty, "Advertise svi mac-ip: %s\n",
7221 zvrf->advertise_svi_macip ? "Yes" : "No");
61d46eda
CS
7222 vty_out(vty, "Duplicate address detection: %s\n",
7223 zvrf->dup_addr_detect ? "Enable" : "Disable");
7224 vty_out(vty, " Detection max-moves %u, time %d\n",
7225 zvrf->dad_max_moves, zvrf->dad_time);
7226 if (zvrf->dad_freeze) {
7227 if (zvrf->dad_freeze_time)
7228 vty_out(vty, " Detection freeze %u\n",
7229 zvrf->dad_freeze_time);
7230 else
7231 vty_out(vty, " Detection freeze %s\n",
7232 "permanent");
7233 }
cd233079 7234 }
4cce389e
MK
7235
7236 if (uj) {
7237 vty_out(vty, "%s\n", json_object_to_json_string_ext(
7238 json, JSON_C_TO_STRING_PRETTY));
7239 json_object_free(json);
7240 }
7241}
7242
7243/*
7244 * Display VNI hash table (VTY command handler).
7245 */
7246void zebra_vxlan_print_vnis(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 7247 bool use_json)
4cce389e
MK
7248{
7249 json_object *json = NULL;
4cce389e
MK
7250 void *args[2];
7251
7252 if (!is_evpn_enabled())
7253 return;
7254
4cce389e
MK
7255 if (use_json)
7256 json = json_object_new_object();
7257 else
996c9314
LB
7258 vty_out(vty, "%-10s %-4s %-21s %-8s %-8s %-15s %-37s\n", "VNI",
7259 "Type", "VxLAN IF", "# MACs", "# ARPs",
7260 "# Remote VTEPs", "Tenant VRF");
4cce389e 7261
cd233079
CS
7262 args[0] = vty;
7263 args[1] = json;
7264
4cce389e 7265 /* Display all L2-VNIs */
cd233079 7266 hash_iterate(zvrf->vni_table,
e3b78da8 7267 (void (*)(struct hash_bucket *, void *))zvni_print_hash,
cd233079
CS
7268 args);
7269
4cce389e 7270 /* Display all L3-VNIs */
89272910 7271 hash_iterate(zrouter.l3vni_table,
e3b78da8 7272 (void (*)(struct hash_bucket *, void *))zl3vni_print_hash,
4cce389e
MK
7273 args);
7274
cd233079
CS
7275 if (use_json) {
7276 vty_out(vty, "%s\n", json_object_to_json_string_ext(
7277 json, JSON_C_TO_STRING_PRETTY));
7278 json_object_free(json);
7279 }
cec2e17d 7280}
7281
3950b52c
CS
7282void zebra_vxlan_dup_addr_detection(ZAPI_HANDLER_ARGS)
7283{
7284 struct stream *s;
7285 int time = 0;
7286 uint32_t max_moves = 0;
7287 uint32_t freeze_time = 0;
7288 bool dup_addr_detect = false;
7289 bool freeze = false;
7290
7291 s = msg;
7292 STREAM_GETL(s, dup_addr_detect);
7293 STREAM_GETL(s, time);
7294 STREAM_GETL(s, max_moves);
7295 STREAM_GETL(s, freeze);
7296 STREAM_GETL(s, freeze_time);
7297
09de9258
CS
7298 /* DAD previous state was enabled, and new state is disable,
7299 * clear all duplicate detected addresses.
7300 */
7301 if (zvrf->dup_addr_detect && !dup_addr_detect)
7302 zebra_vxlan_clear_dup_detect_vni_all(NULL, zvrf);
7303
3950b52c
CS
7304 zvrf->dup_addr_detect = dup_addr_detect;
7305 zvrf->dad_time = time;
7306 zvrf->dad_max_moves = max_moves;
7307 zvrf->dad_freeze = freeze;
7308 zvrf->dad_freeze_time = freeze_time;
7309
7310 if (IS_ZEBRA_DEBUG_VXLAN)
7311 zlog_debug(
6005fe55
CS
7312 "VRF %s duplicate detect %s max_moves %u timeout %u freeze %s freeze_time %u",
7313 vrf_id_to_name(zvrf->vrf->vrf_id),
3950b52c
CS
7314 zvrf->dup_addr_detect ? "enable" : "disable",
7315 zvrf->dad_max_moves,
7316 zvrf->dad_time,
7317 zvrf->dad_freeze ? "enable" : "disable",
7318 zvrf->dad_freeze_time);
7319
7320stream_failure:
7321 return;
7322}
7323
09af6961
NS
7324/*
7325 * Display VNI hash table in detail(VTY command handler).
7326 */
7327void zebra_vxlan_print_vnis_detail(struct vty *vty, struct zebra_vrf *zvrf,
7328 bool use_json)
7329{
7330 json_object *json = NULL;
7331 struct zebra_ns *zns = NULL;
7332 struct zvni_evpn_show zes;
7333
7334 if (!is_evpn_enabled())
7335 return;
7336
7337 zns = zebra_ns_lookup(NS_DEFAULT);
7338 if (!zns)
7339 return;
7340
7341
7342 if (use_json)
7343 json = json_object_new_object();
7344
7345 zes.vty = vty;
7346 zes.json = json;
7347 zes.zvrf = zvrf;
7348
7349 /* Display all L2-VNIs */
2bcb1a7f
QY
7350 hash_iterate(
7351 zvrf->vni_table,
7352 (void (*)(struct hash_bucket *, void *))zvni_print_hash_detail,
7353 &zes);
09af6961
NS
7354
7355 /* Display all L3-VNIs */
7356 hash_iterate(zrouter.l3vni_table,
e3b78da8 7357 (void (*)(struct hash_bucket *,
09af6961
NS
7358 void *))zl3vni_print_hash_detail,
7359 &zes);
7360
7361 if (use_json) {
2bcb1a7f
QY
7362 vty_out(vty, "%s\n",
7363 json_object_to_json_string_ext(
7364 json, JSON_C_TO_STRING_PRETTY));
09af6961
NS
7365 json_object_free(json);
7366 }
7367}
7368
2232a77c 7369/*
ee69da27
MK
7370 * Handle neighbor delete notification from the kernel (on a VLAN device
7371 * / L3 interface). This may result in either the neighbor getting deleted
7372 * from our database or being re-added to the kernel (if it is a valid
2232a77c 7373 * remote neighbor).
7374 */
ee69da27
MK
7375int zebra_vxlan_handle_kernel_neigh_del(struct interface *ifp,
7376 struct interface *link_if,
7377 struct ipaddr *ip)
d62a17ae 7378{
d62a17ae 7379 char buf[INET6_ADDRSTRLEN];
b6938a74 7380 char buf2[ETHER_ADDR_STRLEN];
b7cfce93
MK
7381 zebra_neigh_t *n = NULL;
7382 zebra_vni_t *zvni = NULL;
7383 zebra_mac_t *zmac = NULL;
8c9b80b9 7384 zebra_l3vni_t *zl3vni = NULL;
280c70ea 7385 struct zebra_vrf *zvrf;
b7cfce93 7386
8c9b80b9 7387 /* check if this is a remote neigh entry corresponding to remote
523cafc4 7388 * next-hop
7389 */
8c9b80b9
MK
7390 zl3vni = zl3vni_from_svi(ifp, link_if);
7391 if (zl3vni)
7392 return zl3vni_local_nh_del(zl3vni, ip);
d62a17ae 7393
7394 /* We are only interested in neighbors on an SVI that resides on top
7395 * of a VxLAN bridge.
7396 */
b7cfce93 7397 zvni = zvni_from_svi(ifp, link_if);
6041b686
CS
7398 if (!zvni) {
7399 if (IS_ZEBRA_DEBUG_VXLAN)
7400 zlog_debug("%s: Del neighbor %s VNI is not present for interface %s",
7401 __PRETTY_FUNCTION__,
7402 ipaddr2str(ip, buf, sizeof(buf)), ifp->name);
d62a17ae 7403 return 0;
6041b686 7404 }
8c9b80b9 7405
d62a17ae 7406 if (!zvni->vxlan_if) {
9df414fe 7407 zlog_debug(
d62a17ae 7408 "VNI %u hash %p doesn't have intf upon local neighbor DEL",
7409 zvni->vni, zvni);
7410 return -1;
7411 }
7412
7413 if (IS_ZEBRA_DEBUG_VXLAN)
8c9b80b9 7414 zlog_debug("Del neighbor %s intf %s(%u) -> L2-VNI %u",
996c9314
LB
7415 ipaddr2str(ip, buf, sizeof(buf)), ifp->name,
7416 ifp->ifindex, zvni->vni);
d62a17ae 7417
7418 /* If entry doesn't exist, nothing to do. */
7419 n = zvni_neigh_lookup(zvni, ip);
7420 if (!n)
7421 return 0;
7422
b6938a74
MK
7423 zmac = zvni_mac_lookup(zvni, &n->emac);
7424 if (!zmac) {
7425 if (IS_ZEBRA_DEBUG_VXLAN)
9df414fe 7426 zlog_debug(
2853fed6 7427 "Trying to del a neigh %s without a mac %s on VNI %u",
7428 ipaddr2str(ip, buf, sizeof(buf)),
b6938a74
MK
7429 prefix_mac2str(&n->emac, buf2, sizeof(buf2)),
7430 zvni->vni);
7431
7432 return 0;
7433 }
7434
d62a17ae 7435 /* If it is a remote entry, the kernel has aged this out or someone has
7436 * deleted it, it needs to be re-installed as Quagga is the owner.
7437 */
7438 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
7439 zvni_neigh_install(zvni, n);
7440 return 0;
7441 }
7442
a36898e7 7443 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
280c70ea
CS
7444 if (!zvrf) {
7445 zlog_debug("%s: VNI %u vrf lookup failed.",
7446 __PRETTY_FUNCTION__, zvni->vni);
7447 return -1;
7448 }
7449
7450 /* In case of feeze action, if local neigh is in duplicate state,
7451 * Mark the Neigh as inactive before sending delete request to BGPd,
7452 * If BGPd has remote entry, it will re-install
7453 */
7454 if (zvrf->dad_freeze &&
7455 CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
7456 ZEBRA_NEIGH_SET_INACTIVE(n);
7457
d62a17ae 7458 /* Remove neighbor from BGP. */
280c70ea 7459 zvni_neigh_send_del_to_client(zvni->vni, &n->ip, &n->emac, 0, n->state);
d62a17ae 7460
7461 /* Delete this neighbor entry. */
7462 zvni_neigh_del(zvni, n);
7463
b6938a74
MK
7464 /* see if the AUTO mac needs to be deleted */
7465 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_AUTO)
f51d8a27 7466 && !listcount(zmac->neigh_list))
b6938a74
MK
7467 zvni_mac_del(zvni, zmac);
7468
d62a17ae 7469 return 0;
2232a77c 7470}
7471
7472/*
ee69da27
MK
7473 * Handle neighbor add or update notification from the kernel (on a VLAN
7474 * device / L3 interface). This is typically for a local neighbor but can
7475 * also be for a remote neighbor (e.g., ageout notification). It could
7476 * also be a "move" scenario.
2232a77c 7477 */
ee69da27
MK
7478int zebra_vxlan_handle_kernel_neigh_update(struct interface *ifp,
7479 struct interface *link_if,
7480 struct ipaddr *ip,
7481 struct ethaddr *macaddr,
7482 uint16_t state,
a37f4598 7483 bool is_ext,
7484 bool is_router)
d62a17ae 7485{
d62a17ae 7486 char buf[ETHER_ADDR_STRLEN];
7487 char buf2[INET6_ADDRSTRLEN];
b7cfce93 7488 zebra_vni_t *zvni = NULL;
3bcbba10 7489 zebra_l3vni_t *zl3vni = NULL;
7490
7491 /* check if this is a remote neigh entry corresponding to remote
7492 * next-hop
7493 */
7494 zl3vni = zl3vni_from_svi(ifp, link_if);
7495 if (zl3vni)
7496 return zl3vni_local_nh_add_update(zl3vni, ip, state);
b7cfce93 7497
d62a17ae 7498 /* We are only interested in neighbors on an SVI that resides on top
7499 * of a VxLAN bridge.
7500 */
b7cfce93 7501 zvni = zvni_from_svi(ifp, link_if);
d62a17ae 7502 if (!zvni)
7503 return 0;
7504
d62a17ae 7505 if (IS_ZEBRA_DEBUG_VXLAN)
7506 zlog_debug(
54c17425 7507 "Add/Update neighbor %s MAC %s intf %s(%u) state 0x%x %s %s-> L2-VNI %u",
2853fed6 7508 ipaddr2str(ip, buf2, sizeof(buf2)),
d62a17ae 7509 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
a37f4598 7510 ifp->ifindex, state, is_ext ? "ext-learned " : "",
7511 is_router ? "router " : "",
8c9b80b9 7512 zvni->vni);
d62a17ae 7513
ee69da27 7514 /* Is this about a local neighbor or a remote one? */
a37f4598 7515 if (!is_ext)
68e33151 7516 return zvni_local_neigh_update(zvni, ifp, ip, macaddr,
a37f4598 7517 is_router);
b7cfce93 7518
ee69da27 7519 return zvni_remote_neigh_update(zvni, ifp, ip, macaddr, state);
2232a77c 7520}
7521
b682f6de 7522
2232a77c 7523/*
7524 * Handle message from client to delete a remote MACIP for a VNI.
7525 */
89f4e507 7526void zebra_vxlan_remote_macip_del(ZAPI_HANDLER_ARGS)
d62a17ae 7527{
7528 struct stream *s;
7529 vni_t vni;
7530 struct ethaddr macaddr;
7531 struct ipaddr ip;
7532 struct in_addr vtep_ip;
9df2b997 7533 uint16_t l = 0, ipa_len;
d62a17ae 7534 char buf[ETHER_ADDR_STRLEN];
7535 char buf1[INET6_ADDRSTRLEN];
7536
b7cfce93 7537 memset(&macaddr, 0, sizeof(struct ethaddr));
b7cfce93
MK
7538 memset(&ip, 0, sizeof(struct ipaddr));
7539 memset(&vtep_ip, 0, sizeof(struct in_addr));
7540
1002497a 7541 s = msg;
d62a17ae 7542
89f4e507 7543 while (l < hdr->length) {
d62a17ae 7544 /* Obtain each remote MACIP and process. */
7545 /* Message contains VNI, followed by MAC followed by IP (if any)
7546 * followed by remote VTEP IP.
7547 */
d62a17ae 7548 memset(&ip, 0, sizeof(ip));
ec93aa12
DS
7549 STREAM_GETL(s, vni);
7550 STREAM_GET(&macaddr.octet, s, ETH_ALEN);
7551 STREAM_GETL(s, ipa_len);
d62a17ae 7552 if (ipa_len) {
7553 ip.ipa_type = (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4
7554 : IPADDR_V6;
ec93aa12 7555 STREAM_GET(&ip.ip.addr, s, ipa_len);
d62a17ae 7556 }
ff8b7eb8 7557 l += 4 + ETH_ALEN + 4 + ipa_len;
ec93aa12 7558 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 7559 l += IPV4_MAX_BYTELEN;
7560
7561 if (IS_ZEBRA_DEBUG_VXLAN)
7562 zlog_debug(
f07e1c99 7563 "Recv MACIP DEL VNI %u MAC %s%s%s Remote VTEP %s from %s",
7564 vni,
d62a17ae 7565 prefix_mac2str(&macaddr, buf, sizeof(buf)),
f07e1c99 7566 ipa_len ? " IP " : "",
7567 ipa_len ?
7568 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
d62a17ae 7569 inet_ntoa(vtep_ip),
7570 zebra_route_string(client->proto));
7571
f07e1c99 7572 process_remote_macip_del(vni, &macaddr, ipa_len, &ip, vtep_ip);
d62a17ae 7573 }
7574
ec93aa12 7575stream_failure:
8068a649 7576 return;
2232a77c 7577}
7578
7579/*
7580 * Handle message from client to add a remote MACIP for a VNI. This
7581 * could be just the add of a MAC address or the add of a neighbor
7582 * (IP+MAC).
7583 */
89f4e507 7584void zebra_vxlan_remote_macip_add(ZAPI_HANDLER_ARGS)
d62a17ae 7585{
7586 struct stream *s;
7587 vni_t vni;
7588 struct ethaddr macaddr;
7589 struct ipaddr ip;
7590 struct in_addr vtep_ip;
9df2b997 7591 uint16_t l = 0, ipa_len;
f07e1c99 7592 uint8_t flags = 0;
7593 uint32_t seq;
d62a17ae 7594 char buf[ETHER_ADDR_STRLEN];
7595 char buf1[INET6_ADDRSTRLEN];
d62a17ae 7596
b7cfce93 7597 memset(&macaddr, 0, sizeof(struct ethaddr));
b7cfce93
MK
7598 memset(&ip, 0, sizeof(struct ipaddr));
7599 memset(&vtep_ip, 0, sizeof(struct in_addr));
7600
ec93aa12 7601 if (!EVPN_ENABLED(zvrf)) {
9df414fe 7602 zlog_debug("EVPN not enabled, ignoring remote MACIP ADD");
8068a649 7603 return;
ec93aa12 7604 }
d62a17ae 7605
1002497a 7606 s = msg;
d62a17ae 7607
89f4e507 7608 while (l < hdr->length) {
d62a17ae 7609 /* Obtain each remote MACIP and process. */
7610 /* Message contains VNI, followed by MAC followed by IP (if any)
7611 * followed by remote VTEP IP.
7612 */
d62a17ae 7613 memset(&ip, 0, sizeof(ip));
ec93aa12
DS
7614 STREAM_GETL(s, vni);
7615 STREAM_GET(&macaddr.octet, s, ETH_ALEN);
7616 STREAM_GETL(s, ipa_len);
d62a17ae 7617 if (ipa_len) {
7618 ip.ipa_type = (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4
7619 : IPADDR_V6;
ec93aa12 7620 STREAM_GET(&ip.ip.addr, s, ipa_len);
d62a17ae 7621 }
ff8b7eb8 7622 l += 4 + ETH_ALEN + 4 + ipa_len;
ec93aa12 7623 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 7624 l += IPV4_MAX_BYTELEN;
7625
ead40654 7626 /* Get flags - sticky mac and/or gateway mac */
2017b3ea 7627 STREAM_GETC(s, flags);
d62a17ae 7628 l++;
f07e1c99 7629 STREAM_GETL(s, seq);
7630 l += 4;
d62a17ae 7631
7632 if (IS_ZEBRA_DEBUG_VXLAN)
7633 zlog_debug(
f07e1c99 7634 "Recv MACIP ADD VNI %u MAC %s%s%s flags 0x%x seq %u VTEP %s from %s",
7635 vni,
d62a17ae 7636 prefix_mac2str(&macaddr, buf, sizeof(buf)),
f07e1c99 7637 ipa_len ? " IP " : "",
7638 ipa_len ?
7639 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
7640 flags, seq, inet_ntoa(vtep_ip),
d62a17ae 7641 zebra_route_string(client->proto));
7642
f07e1c99 7643 process_remote_macip_add(vni, &macaddr, ipa_len, &ip,
7644 flags, seq, vtep_ip);
d62a17ae 7645 }
7646
ec93aa12 7647stream_failure:
8068a649 7648 return;
13d60d35 7649}
7650
7651/*
2232a77c 7652 * Handle notification of MAC add/update over VxLAN. If the kernel is notifying
7653 * us, this must involve a multihoming scenario. Treat this as implicit delete
7654 * of any prior local MAC.
13d60d35 7655 */
d62a17ae 7656int zebra_vxlan_check_del_local_mac(struct interface *ifp,
7657 struct interface *br_if,
7658 struct ethaddr *macaddr, vlanid_t vid)
13d60d35 7659{
d62a17ae 7660 struct zebra_if *zif;
d62a17ae 7661 struct zebra_l2info_vxlan *vxl;
7662 vni_t vni;
7663 zebra_vni_t *zvni;
7664 zebra_mac_t *mac;
7665 char buf[ETHER_ADDR_STRLEN];
13d60d35 7666
d62a17ae 7667 zif = ifp->info;
7668 assert(zif);
7669 vxl = &zif->l2info.vxl;
7670 vni = vxl->vni;
13d60d35 7671
2853fed6 7672 /* Check if EVPN is enabled. */
7673 if (!is_evpn_enabled())
d62a17ae 7674 return 0;
13d60d35 7675
d62a17ae 7676 /* Locate hash entry; it is expected to exist. */
2853fed6 7677 zvni = zvni_lookup(vni);
d62a17ae 7678 if (!zvni)
7679 return 0;
13d60d35 7680
d62a17ae 7681 /* If entry doesn't exist, nothing to do. */
7682 mac = zvni_mac_lookup(zvni, macaddr);
7683 if (!mac)
7684 return 0;
13d60d35 7685
d62a17ae 7686 /* Is it a local entry? */
7687 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
7688 return 0;
13d60d35 7689
d62a17ae 7690 if (IS_ZEBRA_DEBUG_VXLAN)
7691 zlog_debug(
5756dd1d 7692 "Add/update remote MAC %s intf %s(%u) VNI %u flags 0x%x - del local",
996c9314 7693 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
5756dd1d 7694 ifp->ifindex, vni, mac->flags);
13d60d35 7695
d62a17ae 7696 /* Remove MAC from BGP. */
e98e4b88 7697 zvni_mac_send_del_to_client(zvni->vni, macaddr);
13d60d35 7698
b6938a74
MK
7699 /*
7700 * If there are no neigh associated with the mac delete the mac
7701 * else mark it as AUTO for forward reference
7702 */
7703 if (!listcount(mac->neigh_list)) {
7704 zvni_mac_del(zvni, mac);
7705 } else {
7706 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
5756dd1d 7707 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
b6938a74
MK
7708 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
7709 }
13d60d35 7710
d62a17ae 7711 return 0;
13d60d35 7712}
7713
7714/*
2232a77c 7715 * Handle remote MAC delete by kernel; readd the remote MAC if we have it.
7716 * This can happen because the remote MAC entries are also added as "dynamic",
7717 * so the kernel can ageout the entry.
13d60d35 7718 */
d62a17ae 7719int zebra_vxlan_check_readd_remote_mac(struct interface *ifp,
7720 struct interface *br_if,
7721 struct ethaddr *macaddr, vlanid_t vid)
13d60d35 7722{
a9a76262
MK
7723 struct zebra_if *zif = NULL;
7724 struct zebra_l2info_vxlan *vxl = NULL;
d62a17ae 7725 vni_t vni;
a9a76262
MK
7726 zebra_vni_t *zvni = NULL;
7727 zebra_l3vni_t *zl3vni = NULL;
7728 zebra_mac_t *mac = NULL;
d62a17ae 7729 char buf[ETHER_ADDR_STRLEN];
2232a77c 7730
d62a17ae 7731 zif = ifp->info;
7732 assert(zif);
7733 vxl = &zif->l2info.vxl;
7734 vni = vxl->vni;
2232a77c 7735
2853fed6 7736 /* Check if EVPN is enabled. */
7737 if (!is_evpn_enabled())
d62a17ae 7738 return 0;
2232a77c 7739
a9a76262
MK
7740 /* check if this is a remote RMAC and readd simillar to remote macs */
7741 zl3vni = zl3vni_lookup(vni);
7742 if (zl3vni)
7743 return zebra_vxlan_readd_remote_rmac(zl3vni, macaddr);
7744
d62a17ae 7745 /* Locate hash entry; it is expected to exist. */
2853fed6 7746 zvni = zvni_lookup(vni);
d62a17ae 7747 if (!zvni)
7748 return 0;
13d60d35 7749
d62a17ae 7750 /* If entry doesn't exist, nothing to do. */
7751 mac = zvni_mac_lookup(zvni, macaddr);
7752 if (!mac)
7753 return 0;
2232a77c 7754
d62a17ae 7755 /* Is it a remote entry? */
7756 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
7757 return 0;
2232a77c 7758
d62a17ae 7759 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 7760 zlog_debug("Del remote MAC %s intf %s(%u) VNI %u - readd",
d62a17ae 7761 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
7762 ifp->ifindex, vni);
13d60d35 7763
d62a17ae 7764 zvni_mac_install(zvni, mac);
7765 return 0;
13d60d35 7766}
7767
7768/*
2232a77c 7769 * Handle local MAC delete (on a port or VLAN corresponding to this VNI).
13d60d35 7770 */
d62a17ae 7771int zebra_vxlan_local_mac_del(struct interface *ifp, struct interface *br_if,
7772 struct ethaddr *macaddr, vlanid_t vid)
13d60d35 7773{
d62a17ae 7774 zebra_vni_t *zvni;
7775 zebra_mac_t *mac;
d62a17ae 7776 char buf[ETHER_ADDR_STRLEN];
13d60d35 7777
d62a17ae 7778 /* We are interested in MACs only on ports or (port, VLAN) that
7779 * map to a VNI.
7780 */
7781 zvni = zvni_map_vlan(ifp, br_if, vid);
7782 if (!zvni)
7783 return 0;
7784 if (!zvni->vxlan_if) {
9df414fe
QY
7785 zlog_debug(
7786 "VNI %u hash %p doesn't have intf upon local MAC DEL",
7787 zvni->vni, zvni);
d62a17ae 7788 return -1;
7789 }
13d60d35 7790
d62a17ae 7791 /* If entry doesn't exist, nothing to do. */
7792 mac = zvni_mac_lookup(zvni, macaddr);
7793 if (!mac)
7794 return 0;
2232a77c 7795
d62a17ae 7796 /* Is it a local entry? */
7797 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
7798 return 0;
2232a77c 7799
5756dd1d 7800 if (IS_ZEBRA_DEBUG_VXLAN)
27547880 7801 zlog_debug("DEL MAC %s intf %s(%u) VID %u -> VNI %u seq %u flags 0x%x nbr count %u",
5756dd1d 7802 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
27547880
CS
7803 ifp->ifindex, vid, zvni->vni, mac->loc_seq,
7804 mac->flags, listcount(mac->neigh_list));
5756dd1d 7805
b6938a74 7806 /* Update all the neigh entries associated with this mac */
2853fed6 7807 zvni_process_neigh_on_local_mac_del(zvni, mac);
b6938a74 7808
f07e1c99 7809 /* Remove MAC from BGP. */
e98e4b88 7810 zvni_mac_send_del_to_client(zvni->vni, macaddr);
f07e1c99 7811
b6938a74
MK
7812 /*
7813 * If there are no neigh associated with the mac delete the mac
7814 * else mark it as AUTO for forward reference
7815 */
7816 if (!listcount(mac->neigh_list)) {
7817 zvni_mac_del(zvni, mac);
7818 } else {
7819 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
5756dd1d 7820 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
b6938a74
MK
7821 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
7822 }
2232a77c 7823
d62a17ae 7824 return 0;
13d60d35 7825}
7826
7827/*
2232a77c 7828 * Handle local MAC add (on a port or VLAN corresponding to this VNI).
13d60d35 7829 */
d62a17ae 7830int zebra_vxlan_local_mac_add_update(struct interface *ifp,
7831 struct interface *br_if,
7832 struct ethaddr *macaddr, vlanid_t vid,
a37f4598 7833 bool sticky)
d62a17ae 7834{
7835 zebra_vni_t *zvni;
7836 zebra_mac_t *mac;
e22a946a 7837 struct zebra_vrf *zvrf;
d62a17ae 7838 char buf[ETHER_ADDR_STRLEN];
f07e1c99 7839 bool mac_sticky = false;
7840 bool inform_client = false;
7841 bool upd_neigh = false;
a4445ece 7842 bool is_dup_detect = false;
e22a946a 7843 struct in_addr vtep_ip = {.s_addr = 0};
d62a17ae 7844
7845 /* We are interested in MACs only on ports or (port, VLAN) that
7846 * map to a VNI.
7847 */
7848 zvni = zvni_map_vlan(ifp, br_if, vid);
7849 if (!zvni) {
7850 if (IS_ZEBRA_DEBUG_VXLAN)
7851 zlog_debug(
28bd0652 7852 "\tAdd/Update %sMAC %s intf %s(%u) VID %u, could not find VNI",
2853fed6 7853 sticky ? "sticky " : "",
d62a17ae 7854 prefix_mac2str(macaddr, buf, sizeof(buf)),
7855 ifp->name, ifp->ifindex, vid);
7856 return 0;
7857 }
7858
7859 if (!zvni->vxlan_if) {
28bd0652
DS
7860 if (IS_ZEBRA_DEBUG_VXLAN)
7861 zlog_debug(
7862 "\tVNI %u hash %p doesn't have intf upon local MAC ADD",
7863 zvni->vni, zvni);
d62a17ae 7864 return -1;
7865 }
7866
a36898e7 7867 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
28bd0652
DS
7868 if (!zvrf) {
7869 if (IS_ZEBRA_DEBUG_VXLAN)
a36898e7
DS
7870 zlog_debug("\tNo Vrf found for vrf_id: %d",
7871 zvni->vxlan_if->vrf_id);
e22a946a 7872 return -1;
28bd0652 7873 }
e22a946a 7874
f07e1c99 7875 /* Check if we need to create or update or it is a NO-OP. */
d62a17ae 7876 mac = zvni_mac_lookup(zvni, macaddr);
f07e1c99 7877 if (!mac) {
7878 if (IS_ZEBRA_DEBUG_VXLAN)
7879 zlog_debug(
7880 "ADD %sMAC %s intf %s(%u) VID %u -> VNI %u",
7881 sticky ? "sticky " : "",
7882 prefix_mac2str(macaddr, buf, sizeof(buf)),
7883 ifp->name, ifp->ifindex, vid, zvni->vni);
d62a17ae 7884
f07e1c99 7885 mac = zvni_mac_add(zvni, macaddr);
7886 if (!mac) {
7887 flog_err(
e914ccbe 7888 EC_ZEBRA_MAC_ADD_FAILED,
f07e1c99 7889 "Failed to add MAC %s intf %s(%u) VID %u VNI %u",
7890 prefix_mac2str(macaddr, buf, sizeof(buf)),
7891 ifp->name, ifp->ifindex, vid, zvni->vni);
7892 return -1;
7893 }
7894 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
7895 mac->fwd_info.local.ifindex = ifp->ifindex;
7896 mac->fwd_info.local.vid = vid;
7897 if (sticky)
7898 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
7899 inform_client = true;
7900
7901 } else {
7902 if (IS_ZEBRA_DEBUG_VXLAN)
7903 zlog_debug(
7904 "UPD %sMAC %s intf %s(%u) VID %u -> VNI %u curFlags 0x%x",
7905 sticky ? "sticky " : "",
7906 prefix_mac2str(macaddr, buf, sizeof(buf)),
7907 ifp->name, ifp->ifindex, vid, zvni->vni,
7908 mac->flags);
7909
7910 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
7911 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
7912 mac_sticky = true;
b6938a74 7913
b8ce75a5 7914 /*
f07e1c99 7915 * Update any changes and if changes are relevant to
7916 * BGP, note it.
b8ce75a5 7917 */
d62a17ae 7918 if (mac_sticky == sticky
7919 && mac->fwd_info.local.ifindex == ifp->ifindex
7920 && mac->fwd_info.local.vid == vid) {
7921 if (IS_ZEBRA_DEBUG_VXLAN)
7922 zlog_debug(
28bd0652 7923 "\tAdd/Update %sMAC %s intf %s(%u) VID %u -> VNI %u, "
d62a17ae 7924 "entry exists and has not changed ",
d62a17ae 7925 sticky ? "sticky " : "",
7926 prefix_mac2str(macaddr, buf,
7927 sizeof(buf)),
7928 ifp->name, ifp->ifindex, vid,
7929 zvni->vni);
7930 return 0;
b6938a74 7931 }
f07e1c99 7932 if (mac_sticky != sticky) {
7933 if (sticky)
7934 SET_FLAG(mac->flags,
7935 ZEBRA_MAC_STICKY);
7936 else
7937 UNSET_FLAG(mac->flags,
7938 ZEBRA_MAC_STICKY);
7939 inform_client = true;
7940 }
7941
7942 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
7943 mac->fwd_info.local.ifindex = ifp->ifindex;
7944 mac->fwd_info.local.vid = vid;
7945
7946 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) ||
7947 CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
e22a946a 7948 bool do_dad = false;
f07e1c99 7949
421bb26a 7950 /*
f07e1c99 7951 * MAC has either moved or was "internally" created due
7952 * to a neighbor learn and is now actually learnt. If
7953 * it was learnt as a remote sticky MAC, this is an
7954 * operator error.
421bb26a 7955 */
8f4b98ee 7956 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)) {
9df414fe 7957 flog_warn(
e914ccbe 7958 EC_ZEBRA_STICKY_MAC_ALREADY_LEARNT,
9df414fe 7959 "MAC %s already learnt as remote sticky MAC behind VTEP %s VNI %u",
421bb26a
MK
7960 prefix_mac2str(macaddr, buf,
7961 sizeof(buf)),
7962 inet_ntoa(mac->fwd_info.r_vtep_ip),
7963 zvni->vni);
8f4b98ee
MK
7964 return 0;
7965 }
d62a17ae 7966
f07e1c99 7967 /* If an actual move, compute MAC's seq number */
e22a946a 7968 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
f07e1c99 7969 mac->loc_seq = MAX(mac->rem_seq + 1,
7970 mac->loc_seq);
e22a946a
CS
7971 vtep_ip = mac->fwd_info.r_vtep_ip;
7972 /* Trigger DAD for remote MAC */
7973 do_dad = true;
7974 }
7975
f07e1c99 7976 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
7977 UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
7978 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
7979 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
7980 mac->fwd_info.local.ifindex = ifp->ifindex;
7981 mac->fwd_info.local.vid = vid;
7982 if (sticky)
7983 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
7984 else
7985 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
7986 /*
7987 * We have to inform BGP of this MAC as well as process
7988 * all neighbors.
7989 */
7990 inform_client = true;
7991 upd_neigh = true;
e22a946a 7992
bdca1974
CS
7993 zebra_vxlan_dup_addr_detect_for_mac(zvrf, mac, vtep_ip,
7994 do_dad,
a4445ece 7995 &is_dup_detect,
bdca1974 7996 true);
a4445ece
CS
7997 if (is_dup_detect) {
7998 inform_client = false;
7999 upd_neigh = false;
8000 }
d62a17ae 8001 }
8002 }
8003
d62a17ae 8004 /* Inform BGP if required. */
f07e1c99 8005 if (inform_client) {
8006 if (zvni_mac_send_add_to_client(zvni->vni, macaddr,
8007 mac->flags, mac->loc_seq))
8008 return -1;
b6938a74 8009 }
d62a17ae 8010
f07e1c99 8011 /* Process all neighbors associated with this MAC, if required. */
8012 if (upd_neigh)
8013 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
8014
d62a17ae 8015 return 0;
2232a77c 8016}
13d60d35 8017
8018/*
8019 * Handle message from client to delete a remote VTEP for a VNI.
8020 */
89f4e507 8021void zebra_vxlan_remote_vtep_del(ZAPI_HANDLER_ARGS)
d62a17ae 8022{
8023 struct stream *s;
d7c0a89a 8024 unsigned short l = 0;
d62a17ae 8025 vni_t vni;
8026 struct in_addr vtep_ip;
8027 zebra_vni_t *zvni;
8028 zebra_vtep_t *zvtep;
b5ebdc9b 8029 struct interface *ifp;
8030 struct zebra_if *zif;
d62a17ae 8031
ec93aa12 8032 if (!is_evpn_enabled()) {
9df414fe 8033 zlog_debug(
996c9314
LB
8034 "%s: EVPN is not enabled yet we have received a vtep del command",
8035 __PRETTY_FUNCTION__);
8068a649 8036 return;
ec93aa12
DS
8037 }
8038
986512a3 8039 if (!EVPN_ENABLED(zvrf)) {
a0b0b5c8
T
8040 zlog_debug("Recv MACIP DEL for non-EVPN VRF %u",
8041 zvrf_id(zvrf));
8068a649 8042 return;
2853fed6 8043 }
8044
1002497a 8045 s = msg;
d62a17ae 8046
89f4e507 8047 while (l < hdr->length) {
694bd4ce 8048 int flood_control __attribute__((unused));
8a64de72 8049
d62a17ae 8050 /* Obtain each remote VTEP and process. */
ec93aa12 8051 STREAM_GETL(s, vni);
d62a17ae 8052 l += 4;
ec93aa12 8053 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 8054 l += IPV4_MAX_BYTELEN;
8055
8a64de72
DS
8056 /* Flood control is intentionally ignored right now */
8057 STREAM_GETL(s, flood_control);
8058 l += 4;
8059
d62a17ae 8060 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 8061 zlog_debug("Recv VTEP_DEL %s VNI %u from %s",
8062 inet_ntoa(vtep_ip), vni,
d62a17ae 8063 zebra_route_string(client->proto));
8064
8065 /* Locate VNI hash entry - expected to exist. */
2853fed6 8066 zvni = zvni_lookup(vni);
d62a17ae 8067 if (!zvni) {
8068 if (IS_ZEBRA_DEBUG_VXLAN)
8069 zlog_debug(
8070 "Failed to locate VNI hash upon remote VTEP DEL, "
2853fed6 8071 "VNI %u",
8072 vni);
d62a17ae 8073 continue;
8074 }
8075
b5ebdc9b 8076 ifp = zvni->vxlan_if;
8077 if (!ifp) {
9df414fe 8078 zlog_debug(
60466a63
QY
8079 "VNI %u hash %p doesn't have intf upon remote VTEP DEL",
8080 zvni->vni, zvni);
b682f6de 8081 continue;
b5ebdc9b 8082 }
8083 zif = ifp->info;
8084
8085 /* If down or not mapped to a bridge, we're done. */
b682f6de 8086 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
b5ebdc9b 8087 continue;
8088
d62a17ae 8089 /* If the remote VTEP does not exist, there's nothing more to
8090 * do.
8091 * Otherwise, uninstall any remote MACs pointing to this VTEP
8092 * and
8093 * then, the VTEP entry itself and remove it.
8094 */
8095 zvtep = zvni_vtep_find(zvni, &vtep_ip);
8096 if (!zvtep)
8097 continue;
8098
8099 zvni_neigh_del_from_vtep(zvni, 1, &vtep_ip);
8100 zvni_mac_del_from_vtep(zvni, 1, &vtep_ip);
8101 zvni_vtep_uninstall(zvni, &vtep_ip);
8102 zvni_vtep_del(zvni, zvtep);
8103 }
8104
ec93aa12 8105stream_failure:
8068a649 8106 return;
13d60d35 8107}
8108
8109/*
8110 * Handle message from client to add a remote VTEP for a VNI.
8111 */
89f4e507 8112void zebra_vxlan_remote_vtep_add(ZAPI_HANDLER_ARGS)
d62a17ae 8113{
8114 struct stream *s;
d7c0a89a 8115 unsigned short l = 0;
d62a17ae 8116 vni_t vni;
8117 struct in_addr vtep_ip;
8118 zebra_vni_t *zvni;
b5ebdc9b 8119 struct interface *ifp;
8120 struct zebra_if *zif;
9718c54e
AK
8121 int flood_control;
8122 zebra_vtep_t *zvtep;
d62a17ae 8123
ec93aa12 8124 if (!is_evpn_enabled()) {
9df414fe 8125 zlog_debug(
996c9314
LB
8126 "%s: EVPN not enabled yet we received a vtep_add zapi call",
8127 __PRETTY_FUNCTION__);
8068a649 8128 return;
ec93aa12
DS
8129 }
8130
986512a3 8131 if (!EVPN_ENABLED(zvrf)) {
a0b0b5c8
T
8132 zlog_debug("Recv MACIP ADD for non-EVPN VRF %u",
8133 zvrf_id(zvrf));
8068a649 8134 return;
2853fed6 8135 }
d62a17ae 8136
1002497a 8137 s = msg;
d62a17ae 8138
89f4e507 8139 while (l < hdr->length) {
d62a17ae 8140 /* Obtain each remote VTEP and process. */
ec93aa12 8141 STREAM_GETL(s, vni);
d62a17ae 8142 l += 4;
ec93aa12 8143 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
9718c54e 8144 STREAM_GETL(s, flood_control);
8a64de72 8145 l += IPV4_MAX_BYTELEN + 4;
d62a17ae 8146
8147 if (IS_ZEBRA_DEBUG_VXLAN)
9718c54e
AK
8148 zlog_debug("Recv VTEP_ADD %s VNI %u flood %d from %s",
8149 inet_ntoa(vtep_ip), vni, flood_control,
8150 zebra_route_string(client->proto));
d62a17ae 8151
8152 /* Locate VNI hash entry - expected to exist. */
2853fed6 8153 zvni = zvni_lookup(vni);
d62a17ae 8154 if (!zvni) {
af4c2728 8155 flog_err(
e914ccbe 8156 EC_ZEBRA_VTEP_ADD_FAILED,
2853fed6 8157 "Failed to locate VNI hash upon remote VTEP ADD, VNI %u",
8158 vni);
d62a17ae 8159 continue;
8160 }
b5ebdc9b 8161
8162 ifp = zvni->vxlan_if;
8163 if (!ifp) {
af4c2728 8164 flog_err(
e914ccbe 8165 EC_ZEBRA_VTEP_ADD_FAILED,
d62a17ae 8166 "VNI %u hash %p doesn't have intf upon remote VTEP ADD",
8167 zvni->vni, zvni);
8168 continue;
8169 }
8170
b5ebdc9b 8171 zif = ifp->info;
d62a17ae 8172
b5ebdc9b 8173 /* If down or not mapped to a bridge, we're done. */
b682f6de 8174 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
d62a17ae 8175 continue;
8176
9718c54e
AK
8177 zvtep = zvni_vtep_find(zvni, &vtep_ip);
8178 if (zvtep) {
8179 /* If the remote VTEP already exists check if
8180 * the flood mode has changed
8181 */
8182 if (zvtep->flood_control != flood_control) {
8183 if (zvtep->flood_control
8184 == VXLAN_FLOOD_DISABLED)
8185 /* old mode was head-end-replication but
8186 * is no longer; get rid of the HER fdb
8187 * entry installed before
8188 */
8189 zvni_vtep_uninstall(zvni, &vtep_ip);
8190 zvtep->flood_control = flood_control;
8191 zvni_vtep_install(zvni, zvtep);
8192 }
8193 } else {
8194 zvtep = zvni_vtep_add(zvni, &vtep_ip, flood_control);
8195 if (zvtep)
8196 zvni_vtep_install(zvni, zvtep);
8197 else
8198 flog_err(EC_ZEBRA_VTEP_ADD_FAILED,
8199 "Failed to add remote VTEP, VNI %u zvni %p",
8200 vni, zvni);
d62a17ae 8201 }
d62a17ae 8202 }
8203
ec93aa12 8204stream_failure:
8068a649 8205 return;
13d60d35 8206}
8207
1a98c087
MK
8208/*
8209 * Add/Del gateway macip to evpn
8210 * g/w can be:
8211 * 1. SVI interface on a vlan aware bridge
8212 * 2. SVI interface on a vlan unaware bridge
8213 * 3. vrr interface (MACVLAN) associated to a SVI
8214 * We advertise macip routes for an interface if it is associated to VxLan vlan
8215 */
8216int zebra_vxlan_add_del_gw_macip(struct interface *ifp, struct prefix *p,
8217 int add)
8218{
8219 struct ipaddr ip;
8220 struct ethaddr macaddr;
8221 zebra_vni_t *zvni = NULL;
1a98c087
MK
8222
8223 memset(&ip, 0, sizeof(struct ipaddr));
8224 memset(&macaddr, 0, sizeof(struct ethaddr));
8225
2853fed6 8226 /* Check if EVPN is enabled. */
8227 if (!is_evpn_enabled())
297a21b6
MK
8228 return 0;
8229
1a98c087
MK
8230 if (IS_ZEBRA_IF_MACVLAN(ifp)) {
8231 struct interface *svi_if =
8232 NULL; /* SVI corresponding to the MACVLAN */
8233 struct zebra_if *ifp_zif =
8234 NULL; /* Zebra daemon specific info for MACVLAN */
8235 struct zebra_if *svi_if_zif =
8236 NULL; /* Zebra daemon specific info for SVI*/
8237
8238 ifp_zif = ifp->info;
8239 if (!ifp_zif)
8240 return -1;
8241
71349e03
MK
8242 /*
8243 * for a MACVLAN interface the link represents the svi_if
8244 */
8245 svi_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
8246 ifp_zif->link_ifindex);
1a98c087 8247 if (!svi_if) {
9df414fe
QY
8248 zlog_debug("MACVLAN %s(%u) without link information",
8249 ifp->name, ifp->ifindex);
1a98c087
MK
8250 return -1;
8251 }
8252
8253 if (IS_ZEBRA_IF_VLAN(svi_if)) {
71349e03
MK
8254 /*
8255 * If it is a vlan aware bridge then the link gives the
8256 * bridge information
8257 */
8258 struct interface *svi_if_link = NULL;
8259
1a98c087 8260 svi_if_zif = svi_if->info;
71349e03
MK
8261 if (svi_if_zif) {
8262 svi_if_link = if_lookup_by_index_per_ns(
60466a63
QY
8263 zebra_ns_lookup(NS_DEFAULT),
8264 svi_if_zif->link_ifindex);
b7cfce93 8265 zvni = zvni_from_svi(svi_if, svi_if_link);
71349e03 8266 }
1a98c087 8267 } else if (IS_ZEBRA_IF_BRIDGE(svi_if)) {
71349e03
MK
8268 /*
8269 * If it is a vlan unaware bridge then svi is the bridge
8270 * itself
8271 */
b7cfce93 8272 zvni = zvni_from_svi(svi_if, svi_if);
1a98c087
MK
8273 }
8274 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
8275 struct zebra_if *svi_if_zif =
71349e03
MK
8276 NULL; /* Zebra daemon specific info for SVI */
8277 struct interface *svi_if_link =
8278 NULL; /* link info for the SVI = bridge info */
1a98c087
MK
8279
8280 svi_if_zif = ifp->info;
e3bb770c
IS
8281 if (svi_if_zif) {
8282 svi_if_link = if_lookup_by_index_per_ns(
cef91a18
QY
8283 zebra_ns_lookup(NS_DEFAULT),
8284 svi_if_zif->link_ifindex);
e3bb770c
IS
8285 if (svi_if_link)
8286 zvni = zvni_from_svi(ifp, svi_if_link);
8287 }
1a98c087 8288 } else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
b7cfce93 8289 zvni = zvni_from_svi(ifp, ifp);
1a98c087
MK
8290 }
8291
8292 if (!zvni)
8293 return 0;
8294
8295 if (!zvni->vxlan_if) {
9df414fe
QY
8296 zlog_debug("VNI %u hash %p doesn't have intf upon MACVLAN up",
8297 zvni->vni, zvni);
1a98c087
MK
8298 return -1;
8299 }
8300
1a98c087 8301
1a98c087
MK
8302 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
8303
8304 if (p->family == AF_INET) {
8305 ip.ipa_type = IPADDR_V4;
8306 memcpy(&(ip.ipaddr_v4), &(p->u.prefix4),
8307 sizeof(struct in_addr));
8308 } else if (p->family == AF_INET6) {
8309 ip.ipa_type = IPADDR_V6;
8310 memcpy(&(ip.ipaddr_v6), &(p->u.prefix6),
8311 sizeof(struct in6_addr));
8312 }
8313
8314
8315 if (add)
8316 zvni_gw_macip_add(ifp, zvni, &macaddr, &ip);
8317 else
8318 zvni_gw_macip_del(ifp, zvni, &ip);
8319
8320 return 0;
8321}
8322
2232a77c 8323/*
b7cfce93
MK
8324 * Handle SVI interface going down.
8325 * SVI can be associated to either L3-VNI or L2-VNI.
8326 * For L2-VNI: At this point, this is a NOP since
8327 * the kernel deletes the neighbor entries on this SVI (if any).
8328 * We only need to update the vrf corresponding to zvni.
8329 * For L3-VNI: L3-VNI is operationally down, update mac-ip routes and delete
8330 * from bgp
2232a77c 8331 */
d62a17ae 8332int zebra_vxlan_svi_down(struct interface *ifp, struct interface *link_if)
2232a77c 8333{
b7cfce93
MK
8334 zebra_l3vni_t *zl3vni = NULL;
8335
8336 zl3vni = zl3vni_from_svi(ifp, link_if);
8337 if (zl3vni) {
8338
8339 /* process l3-vni down */
8340 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8341
8342 /* remove association with svi-if */
8343 zl3vni->svi_if = NULL;
8344 } else {
8345 zebra_vni_t *zvni = NULL;
8346
8347 /* since we dont have svi corresponding to zvni, we associate it
8348 * to default vrf. Note: the corresponding neigh entries on the
8349 * SVI would have already been deleted */
8350 zvni = zvni_from_svi(ifp, link_if);
8351 if (zvni) {
8352 zvni->vrf_id = VRF_DEFAULT;
8353
8354 /* update the tenant vrf in BGP */
8355 zvni_send_add_to_client(zvni);
8356 }
8357 }
d62a17ae 8358 return 0;
2232a77c 8359}
8360
8361/*
b7cfce93
MK
8362 * Handle SVI interface coming up.
8363 * SVI can be associated to L3-VNI (l3vni vxlan interface) or L2-VNI (l2-vni
8364 * vxlan intf).
8365 * For L2-VNI: we need to install any remote neighbors entried (used for
8366 * apr-suppression)
8367 * For L3-VNI: SVI will be used to get the rmac to be used with L3-VNI
2232a77c 8368 */
d62a17ae 8369int zebra_vxlan_svi_up(struct interface *ifp, struct interface *link_if)
2232a77c 8370{
b7cfce93
MK
8371 zebra_vni_t *zvni = NULL;
8372 zebra_l3vni_t *zl3vni = NULL;
2232a77c 8373
b7cfce93
MK
8374 zl3vni = zl3vni_from_svi(ifp, link_if);
8375 if (zl3vni) {
2232a77c 8376
b7cfce93
MK
8377 /* associate with svi */
8378 zl3vni->svi_if = ifp;
2232a77c 8379
b7cfce93
MK
8380 /* process oper-up */
8381 if (is_l3vni_oper_up(zl3vni))
8382 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8383 } else {
8384
8385 /* process SVI up for l2-vni */
8386 struct neigh_walk_ctx n_wctx;
8387
8388 zvni = zvni_from_svi(ifp, link_if);
8389 if (!zvni)
8390 return 0;
8391
8392 if (!zvni->vxlan_if) {
9df414fe 8393 zlog_debug(
43e52561
QY
8394 "VNI %u hash %p doesn't have intf upon SVI up",
8395 zvni->vni, zvni);
b7cfce93
MK
8396 return -1;
8397 }
8398
8399 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
8400 zlog_debug(
8401 "SVI %s(%u) VNI %u VRF %s is UP, installing neighbors",
8402 ifp->name, ifp->ifindex, zvni->vni,
a36898e7 8403 vrf_id_to_name(ifp->vrf_id));
2232a77c 8404
b7cfce93 8405 /* update the vrf information for l2-vni and inform bgp */
a36898e7 8406 zvni->vrf_id = ifp->vrf_id;
b7cfce93
MK
8407 zvni_send_add_to_client(zvni);
8408
8409 /* Install any remote neighbors for this VNI. */
8410 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
8411 n_wctx.zvni = zvni;
996c9314 8412 hash_iterate(zvni->neigh_table, zvni_install_neigh_hash,
b7cfce93
MK
8413 &n_wctx);
8414 }
2232a77c 8415
d62a17ae 8416 return 0;
2232a77c 8417}
8418
13d60d35 8419/*
b7cfce93 8420 * Handle VxLAN interface down
13d60d35 8421 */
d62a17ae 8422int zebra_vxlan_if_down(struct interface *ifp)
13d60d35 8423{
d62a17ae 8424 vni_t vni;
b7cfce93
MK
8425 struct zebra_if *zif = NULL;
8426 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 8427 zebra_l3vni_t *zl3vni = NULL;
8428 zebra_vni_t *zvni;
13d60d35 8429
2853fed6 8430 /* Check if EVPN is enabled. */
8431 if (!is_evpn_enabled())
d62a17ae 8432 return 0;
13d60d35 8433
d62a17ae 8434 zif = ifp->info;
8435 assert(zif);
8436 vxl = &zif->l2info.vxl;
8437 vni = vxl->vni;
13d60d35 8438
643215ce 8439 zl3vni = zl3vni_lookup(vni);
8440 if (zl3vni) {
b7cfce93 8441 /* process-if-down for l3-vni */
b7cfce93 8442 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
8443 zlog_debug("Intf %s(%u) L3-VNI %u is DOWN", ifp->name,
8444 ifp->ifindex, vni);
b7cfce93 8445
b7cfce93 8446 zebra_vxlan_process_l3vni_oper_down(zl3vni);
b7cfce93
MK
8447 } else {
8448 /* process if-down for l2-vni */
b7cfce93 8449 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
8450 zlog_debug("Intf %s(%u) L2-VNI %u is DOWN", ifp->name,
8451 ifp->ifindex, vni);
13d60d35 8452
b7cfce93
MK
8453 /* Locate hash entry; it is expected to exist. */
8454 zvni = zvni_lookup(vni);
8455 if (!zvni) {
9df414fe 8456 zlog_debug(
b7cfce93
MK
8457 "Failed to locate VNI hash at DOWN, IF %s(%u) VNI %u",
8458 ifp->name, ifp->ifindex, vni);
8459 return -1;
8460 }
13d60d35 8461
b7cfce93 8462 assert(zvni->vxlan_if == ifp);
13d60d35 8463
b7cfce93
MK
8464 /* Delete this VNI from BGP. */
8465 zvni_send_del_to_client(zvni->vni);
2232a77c 8466
b7cfce93
MK
8467 /* Free up all neighbors and MACs, if any. */
8468 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
8469 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
13d60d35 8470
b7cfce93
MK
8471 /* Free up all remote VTEPs, if any. */
8472 zvni_vtep_del_all(zvni, 1);
8473 }
d62a17ae 8474 return 0;
13d60d35 8475}
8476
8477/*
8478 * Handle VxLAN interface up - update BGP if required.
8479 */
d62a17ae 8480int zebra_vxlan_if_up(struct interface *ifp)
13d60d35 8481{
d62a17ae 8482 vni_t vni;
b7cfce93
MK
8483 struct zebra_if *zif = NULL;
8484 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 8485 zebra_vni_t *zvni = NULL;
8486 zebra_l3vni_t *zl3vni = NULL;
13d60d35 8487
2853fed6 8488 /* Check if EVPN is enabled. */
8489 if (!is_evpn_enabled())
d62a17ae 8490 return 0;
13d60d35 8491
d62a17ae 8492 zif = ifp->info;
8493 assert(zif);
8494 vxl = &zif->l2info.vxl;
8495 vni = vxl->vni;
13d60d35 8496
643215ce 8497 zl3vni = zl3vni_lookup(vni);
8498 if (zl3vni) {
13d60d35 8499
b7cfce93 8500 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
8501 zlog_debug("Intf %s(%u) L3-VNI %u is UP", ifp->name,
8502 ifp->ifindex, vni);
13d60d35 8503
b7cfce93 8504 /* we need to associate with SVI, if any, we can associate with
523cafc4 8505 * svi-if only after association with vxlan-intf is complete
8506 */
b7cfce93
MK
8507 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
8508
8509 if (is_l3vni_oper_up(zl3vni))
8510 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8511 } else {
8512 /* Handle L2-VNI add */
b7cfce93
MK
8513 struct interface *vlan_if = NULL;
8514
8515 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
8516 zlog_debug("Intf %s(%u) L2-VNI %u is UP", ifp->name,
8517 ifp->ifindex, vni);
b7cfce93
MK
8518
8519 /* Locate hash entry; it is expected to exist. */
8520 zvni = zvni_lookup(vni);
8521 if (!zvni) {
9df414fe 8522 zlog_debug(
b7cfce93
MK
8523 "Failed to locate VNI hash at UP, IF %s(%u) VNI %u",
8524 ifp->name, ifp->ifindex, vni);
8525 return -1;
8526 }
8527
8528 assert(zvni->vxlan_if == ifp);
8529 vlan_if = zvni_map_to_svi(vxl->access_vlan,
8530 zif->brslave_info.br_if);
8531 if (vlan_if) {
a36898e7
DS
8532 zvni->vrf_id = vlan_if->vrf_id;
8533 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
b7cfce93
MK
8534 if (zl3vni)
8535 listnode_add_sort(zl3vni->l2vnis, zvni);
8536 }
8537
8538 /* If part of a bridge, inform BGP about this VNI. */
8539 /* Also, read and populate local MACs and neighbors. */
8540 if (zif->brslave_info.br_if) {
8541 zvni_send_add_to_client(zvni);
8542 zvni_read_mac_neigh(zvni, ifp);
8543 }
d62a17ae 8544 }
13d60d35 8545
d62a17ae 8546 return 0;
13d60d35 8547}
8548
8549/*
8550 * Handle VxLAN interface delete. Locate and remove entry in hash table
8551 * and update BGP, if required.
8552 */
d62a17ae 8553int zebra_vxlan_if_del(struct interface *ifp)
13d60d35 8554{
d62a17ae 8555 vni_t vni;
b7cfce93
MK
8556 struct zebra_if *zif = NULL;
8557 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 8558 zebra_vni_t *zvni = NULL;
8559 zebra_l3vni_t *zl3vni = NULL;
13d60d35 8560
2853fed6 8561 /* Check if EVPN is enabled. */
8562 if (!is_evpn_enabled())
d62a17ae 8563 return 0;
13d60d35 8564
d62a17ae 8565 zif = ifp->info;
8566 assert(zif);
8567 vxl = &zif->l2info.vxl;
8568 vni = vxl->vni;
13d60d35 8569
643215ce 8570 zl3vni = zl3vni_lookup(vni);
8571 if (zl3vni) {
b7cfce93
MK
8572
8573 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
8574 zlog_debug("Del L3-VNI %u intf %s(%u)", vni, ifp->name,
8575 ifp->ifindex);
13d60d35 8576
b7cfce93
MK
8577 /* process oper-down for l3-vni */
8578 zebra_vxlan_process_l3vni_oper_down(zl3vni);
2232a77c 8579
b7cfce93 8580 /* remove the association with vxlan_if */
b67a60d2 8581 memset(&zl3vni->local_vtep_ip, 0, sizeof(struct in_addr));
b7cfce93
MK
8582 zl3vni->vxlan_if = NULL;
8583 } else {
13d60d35 8584
b7cfce93 8585 /* process if-del for l2-vni*/
b7cfce93 8586 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
8587 zlog_debug("Del L2-VNI %u intf %s(%u)", vni, ifp->name,
8588 ifp->ifindex);
b7cfce93
MK
8589
8590 /* Locate hash entry; it is expected to exist. */
8591 zvni = zvni_lookup(vni);
8592 if (!zvni) {
9df414fe 8593 zlog_debug(
b7cfce93
MK
8594 "Failed to locate VNI hash at del, IF %s(%u) VNI %u",
8595 ifp->name, ifp->ifindex, vni);
8596 return 0;
8597 }
8598
8599 /* remove from l3-vni list */
8600 zl3vni = zl3vni_from_vrf(zvni->vrf_id);
8601 if (zl3vni)
8602 listnode_delete(zl3vni->l2vnis, zvni);
8603
8604 /* Delete VNI from BGP. */
8605 zvni_send_del_to_client(zvni->vni);
8606
8607 /* Free up all neighbors and MAC, if any. */
8608 zvni_neigh_del_all(zvni, 0, 0, DEL_ALL_NEIGH);
8609 zvni_mac_del_all(zvni, 0, 0, DEL_ALL_MAC);
8610
8611 /* Free up all remote VTEPs, if any. */
8612 zvni_vtep_del_all(zvni, 0);
8613
8614 /* Delete the hash entry. */
8615 if (zvni_del(zvni)) {
e914ccbe 8616 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
1c50c1c0
QY
8617 "Failed to del VNI hash %p, IF %s(%u) VNI %u",
8618 zvni, ifp->name, ifp->ifindex, zvni->vni);
b7cfce93
MK
8619 return -1;
8620 }
d62a17ae 8621 }
d62a17ae 8622 return 0;
13d60d35 8623}
8624
8625/*
8626 * Handle VxLAN interface update - change to tunnel IP, master or VLAN.
8627 */
d7c0a89a 8628int zebra_vxlan_if_update(struct interface *ifp, uint16_t chgflags)
d62a17ae 8629{
d62a17ae 8630 vni_t vni;
b7cfce93
MK
8631 struct zebra_if *zif = NULL;
8632 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 8633 zebra_vni_t *zvni = NULL;
8634 zebra_l3vni_t *zl3vni = NULL;
d62a17ae 8635
2853fed6 8636 /* Check if EVPN is enabled. */
8637 if (!is_evpn_enabled())
d62a17ae 8638 return 0;
8639
8640 zif = ifp->info;
8641 assert(zif);
8642 vxl = &zif->l2info.vxl;
8643 vni = vxl->vni;
8644
643215ce 8645 zl3vni = zl3vni_lookup(vni);
8646 if (zl3vni) {
af026ae4 8647
b7cfce93
MK
8648 if (IS_ZEBRA_DEBUG_VXLAN)
8649 zlog_debug(
8650 "Update L3-VNI %u intf %s(%u) VLAN %u local IP %s master %u chg 0x%x",
996c9314
LB
8651 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
8652 inet_ntoa(vxl->vtep_ip),
b7cfce93
MK
8653 zif->brslave_info.bridge_ifindex, chgflags);
8654
8655 /* Removed from bridge? Cleanup and return */
8656 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
8657 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
8658 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8659 return 0;
8660 }
8661
8662 /* access-vlan change - process oper down, associate with new
523cafc4 8663 * svi_if and then process oper up again
8664 */
b7cfce93
MK
8665 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
8666 if (if_is_operative(ifp)) {
8667 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8668 zl3vni->svi_if = NULL;
8669 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
bca63dc8 8670 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93
MK
8671 if (is_l3vni_oper_up(zl3vni))
8672 zebra_vxlan_process_l3vni_oper_up(
996c9314 8673 zl3vni);
b7cfce93
MK
8674 }
8675 }
d62a17ae 8676
12eeac84
MK
8677 /*
8678 * local-ip change - process oper down, associate with new
8679 * local-ip and then process oper up again
8680 */
8681 if (chgflags & ZEBRA_VXLIF_LOCAL_IP_CHANGE) {
8682 if (if_is_operative(ifp)) {
8683 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8684 zl3vni->local_vtep_ip = vxl->vtep_ip;
8685 if (is_l3vni_oper_up(zl3vni))
8686 zebra_vxlan_process_l3vni_oper_up(
996c9314 8687 zl3vni);
12eeac84
MK
8688 }
8689 }
8690
bca63dc8
MK
8691 /* Update local tunnel IP. */
8692 zl3vni->local_vtep_ip = vxl->vtep_ip;
8693
12eeac84
MK
8694 /* if we have a valid new master, process l3-vni oper up */
8695 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE) {
8696 if (if_is_operative(ifp) && is_l3vni_oper_up(zl3vni))
b7cfce93
MK
8697 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8698 }
8699 } else {
d62a17ae 8700
b7cfce93
MK
8701 /* Update VNI hash. */
8702 zvni = zvni_lookup(vni);
8703 if (!zvni) {
9df414fe 8704 zlog_debug(
b7cfce93
MK
8705 "Failed to find L2-VNI hash on update, IF %s(%u) VNI %u",
8706 ifp->name, ifp->ifindex, vni);
8707 return -1;
8708 }
d62a17ae 8709
b7cfce93
MK
8710 if (IS_ZEBRA_DEBUG_VXLAN)
8711 zlog_debug(
8712 "Update L2-VNI %u intf %s(%u) VLAN %u local IP %s master %u chg 0x%x",
996c9314
LB
8713 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
8714 inet_ntoa(vxl->vtep_ip),
b7cfce93
MK
8715 zif->brslave_info.bridge_ifindex, chgflags);
8716
8717 /* Removed from bridge? Cleanup and return */
8718 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
8719 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
8720 /* Delete from client, remove all remote VTEPs */
8721 /* Also, free up all MACs and neighbors. */
8722 zvni_send_del_to_client(zvni->vni);
8723 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
8724 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
8725 zvni_vtep_del_all(zvni, 1);
8726 return 0;
8727 }
d62a17ae 8728
b7cfce93
MK
8729 /* Handle other changes. */
8730 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
8731 /* Remove all existing local neigh and MACs for this VNI
8732 * (including from BGP)
8733 */
8734 zvni_neigh_del_all(zvni, 0, 1, DEL_LOCAL_MAC);
8735 zvni_mac_del_all(zvni, 0, 1, DEL_LOCAL_MAC);
8736 }
d62a17ae 8737
abfa0a96
AK
8738 if (zvni->local_vtep_ip.s_addr != vxl->vtep_ip.s_addr ||
8739 zvni->mcast_grp.s_addr != vxl->mcast_grp.s_addr) {
8740 zebra_vxlan_sg_deref(zvni->local_vtep_ip,
8741 zvni->mcast_grp);
8742 zebra_vxlan_sg_ref(vxl->vtep_ip, vxl->mcast_grp);
8743 zvni->local_vtep_ip = vxl->vtep_ip;
8744 zvni->mcast_grp = vxl->mcast_grp;
8745 }
b7cfce93 8746 zvni->vxlan_if = ifp;
d62a17ae 8747
b7cfce93
MK
8748 /* Take further actions needed.
8749 * Note that if we are here, there is a change of interest.
8750 */
8751 /* If down or not mapped to a bridge, we're done. */
8752 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8753 return 0;
d62a17ae 8754
b7cfce93
MK
8755 /* Inform BGP, if there is a change of interest. */
8756 if (chgflags
39c46ff1
AK
8757 & (ZEBRA_VXLIF_MASTER_CHANGE |
8758 ZEBRA_VXLIF_LOCAL_IP_CHANGE |
8759 ZEBRA_VXLIF_MCAST_GRP_CHANGE))
b7cfce93
MK
8760 zvni_send_add_to_client(zvni);
8761
8762 /* If there is a valid new master or a VLAN mapping change,
8763 * read and populate local MACs and neighbors.
8764 * Also, reinstall any remote MACs and neighbors
8765 * for this VNI (based on new VLAN).
8766 */
8767 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
8768 zvni_read_mac_neigh(zvni, ifp);
8769 else if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
8770 struct mac_walk_ctx m_wctx;
8771 struct neigh_walk_ctx n_wctx;
8772
8773 zvni_read_mac_neigh(zvni, ifp);
8774
8775 memset(&m_wctx, 0, sizeof(struct mac_walk_ctx));
8776 m_wctx.zvni = zvni;
996c9314 8777 hash_iterate(zvni->mac_table, zvni_install_mac_hash,
b7cfce93
MK
8778 &m_wctx);
8779
8780 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
8781 n_wctx.zvni = zvni;
8782 hash_iterate(zvni->neigh_table, zvni_install_neigh_hash,
8783 &n_wctx);
8784 }
d62a17ae 8785 }
8786
8787 return 0;
13d60d35 8788}
8789
8790/*
8791 * Handle VxLAN interface add.
8792 */
d62a17ae 8793int zebra_vxlan_if_add(struct interface *ifp)
13d60d35 8794{
d62a17ae 8795 vni_t vni;
b7cfce93
MK
8796 struct zebra_if *zif = NULL;
8797 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 8798 zebra_vni_t *zvni = NULL;
8799 zebra_l3vni_t *zl3vni = NULL;
13d60d35 8800
2853fed6 8801 /* Check if EVPN is enabled. */
8802 if (!is_evpn_enabled())
d62a17ae 8803 return 0;
13d60d35 8804
d62a17ae 8805 zif = ifp->info;
8806 assert(zif);
8807 vxl = &zif->l2info.vxl;
8808 vni = vxl->vni;
13d60d35 8809
643215ce 8810 zl3vni = zl3vni_lookup(vni);
8811 if (zl3vni) {
13d60d35 8812
b7cfce93 8813 /* process if-add for l3-vni*/
b7cfce93
MK
8814 if (IS_ZEBRA_DEBUG_VXLAN)
8815 zlog_debug(
8816 "Add L3-VNI %u intf %s(%u) VLAN %u local IP %s master %u",
996c9314
LB
8817 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
8818 inet_ntoa(vxl->vtep_ip),
b7cfce93
MK
8819 zif->brslave_info.bridge_ifindex);
8820
b7cfce93 8821 /* associate with vxlan_if */
b67a60d2 8822 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93
MK
8823 zl3vni->vxlan_if = ifp;
8824
8825 /* Associate with SVI, if any. We can associate with svi-if only
8826 * after association with vxlan_if is complete */
8827 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
8828
8829 if (is_l3vni_oper_up(zl3vni))
8830 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8831 } else {
8832
8833 /* process if-add for l2-vni */
b7cfce93
MK
8834 struct interface *vlan_if = NULL;
8835
8836 /* Create or update VNI hash. */
8837 zvni = zvni_lookup(vni);
8838 if (!zvni) {
8839 zvni = zvni_add(vni);
8840 if (!zvni) {
af4c2728 8841 flog_err(
e914ccbe 8842 EC_ZEBRA_VNI_ADD_FAILED,
b7cfce93
MK
8843 "Failed to add VNI hash, IF %s(%u) VNI %u",
8844 ifp->name, ifp->ifindex, vni);
8845 return -1;
8846 }
8847 }
8848
abfa0a96
AK
8849 if (zvni->local_vtep_ip.s_addr != vxl->vtep_ip.s_addr ||
8850 zvni->mcast_grp.s_addr != vxl->mcast_grp.s_addr) {
8851 zebra_vxlan_sg_deref(zvni->local_vtep_ip,
8852 zvni->mcast_grp);
8853 zebra_vxlan_sg_ref(vxl->vtep_ip, vxl->mcast_grp);
8854 zvni->local_vtep_ip = vxl->vtep_ip;
8855 zvni->mcast_grp = vxl->mcast_grp;
8856 }
b7cfce93
MK
8857 zvni->vxlan_if = ifp;
8858 vlan_if = zvni_map_to_svi(vxl->access_vlan,
8859 zif->brslave_info.br_if);
8860 if (vlan_if) {
a36898e7
DS
8861 zvni->vrf_id = vlan_if->vrf_id;
8862 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
b7cfce93
MK
8863 if (zl3vni)
8864 listnode_add_sort(zl3vni->l2vnis, zvni);
8865 }
8866
39c46ff1
AK
8867 if (IS_ZEBRA_DEBUG_VXLAN) {
8868 char addr_buf1[INET_ADDRSTRLEN];
8869 char addr_buf2[INET_ADDRSTRLEN];
8870
8871 inet_ntop(AF_INET, &vxl->vtep_ip,
8872 addr_buf1, INET_ADDRSTRLEN);
8873 inet_ntop(AF_INET, &vxl->mcast_grp,
8874 addr_buf2, INET_ADDRSTRLEN);
8875
b7cfce93 8876 zlog_debug(
aa0677b4 8877 "Add L2-VNI %u VRF %s intf %s(%u) VLAN %u local IP %s mcast_grp %s master %u",
b7cfce93 8878 vni,
a36898e7 8879 vlan_if ? vrf_id_to_name(vlan_if->vrf_id)
08ab35fe 8880 : VRF_DEFAULT_NAME,
996c9314 8881 ifp->name, ifp->ifindex, vxl->access_vlan,
39c46ff1 8882 addr_buf1, addr_buf2,
b7cfce93 8883 zif->brslave_info.bridge_ifindex);
39c46ff1 8884 }
b7cfce93
MK
8885
8886 /* If down or not mapped to a bridge, we're done. */
8887 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8888 return 0;
8889
8890 /* Inform BGP */
8891 zvni_send_add_to_client(zvni);
8892
8893 /* Read and populate local MACs and neighbors */
8894 zvni_read_mac_neigh(zvni, ifp);
8895 }
8896
8897 return 0;
8898}
8899
996c9314
LB
8900int zebra_vxlan_process_vrf_vni_cmd(struct zebra_vrf *zvrf, vni_t vni,
8901 char *err, int err_str_sz, int filter,
8902 int add)
b7cfce93
MK
8903{
8904 zebra_l3vni_t *zl3vni = NULL;
5e53dce3 8905 struct zebra_vrf *zvrf_evpn = NULL;
b7cfce93 8906
5e53dce3
T
8907 zvrf_evpn = zebra_vrf_get_evpn();
8908 if (!zvrf_evpn)
b7cfce93
MK
8909 return -1;
8910
8911 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 8912 zlog_debug("vrf %s vni %u %s", zvrf_name(zvrf), vni,
b7cfce93
MK
8913 add ? "ADD" : "DEL");
8914
8915 if (add) {
8916
8917 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
8918
8919 /* check if the vni is already present under zvrf */
8920 if (zvrf->l3vni) {
3f02fbab 8921 snprintf(err, err_str_sz,
b7cfce93
MK
8922 "VNI is already configured under the vrf");
8923 return -1;
8924 }
8925
8926 /* check if this VNI is already present in the system */
8927 zl3vni = zl3vni_lookup(vni);
8928 if (zl3vni) {
3f02fbab 8929 snprintf(err, err_str_sz,
b7cfce93
MK
8930 "VNI is already configured as L3-VNI");
8931 return -1;
8932 }
8933
8934 /* add the L3-VNI to the global table */
8935 zl3vni = zl3vni_add(vni, zvrf_id(zvrf));
8936 if (!zl3vni) {
996c9314 8937 snprintf(err, err_str_sz, "Could not add L3-VNI");
b7cfce93
MK
8938 return -1;
8939 }
8940
8941 /* associate the vrf with vni */
8942 zvrf->l3vni = vni;
8943
c48d9f5f
MK
8944 /* set the filter in l3vni to denote if we are using l3vni only
8945 * for prefix routes
8946 */
8947 if (filter)
8948 SET_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY);
8949
b7cfce93 8950 /* associate with vxlan-intf;
523cafc4 8951 * we need to associate with the vxlan-intf first
8952 */
b7cfce93
MK
8953 zl3vni->vxlan_if = zl3vni_map_to_vxlan_if(zl3vni);
8954
8955 /* associate with corresponding SVI interface, we can associate
8956 * with svi-if only after vxlan interface association is
523cafc4 8957 * complete
8958 */
b7cfce93
MK
8959 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
8960
8961 /* formulate l2vni list */
5e53dce3 8962 hash_iterate(zvrf_evpn->vni_table, zvni_add_to_l3vni_list,
996c9314 8963 zl3vni);
b7cfce93
MK
8964
8965 if (is_l3vni_oper_up(zl3vni))
8966 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8967
8968 } else {
8969 zl3vni = zl3vni_lookup(vni);
8970 if (!zl3vni) {
3f02fbab 8971 snprintf(err, err_str_sz, "VNI doesn't exist");
d62a17ae 8972 return -1;
8973 }
b7cfce93 8974
7a6ca8a6
KA
8975 if (zvrf->l3vni != vni) {
8976 snprintf(err, err_str_sz,
8977 "VNI %d doesn't exist in VRF: %s",
8978 vni, zvrf->vrf->name);
8979 return -1;
8980 }
8981
cf299714
MK
8982 if (filter && !CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)) {
8983 snprintf(err, ERR_STR_SZ,
8984 "prefix-routes-only is not set for the vni");
8985 return -1;
8986 }
8987
b7cfce93
MK
8988 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8989
5e06422c 8990 /* delete and uninstall all rmacs */
996c9314 8991 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
5e06422c
MK
8992 zl3vni);
8993
8994 /* delete and uninstall all next-hops */
996c9314 8995 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
5e06422c
MK
8996 zl3vni);
8997
b7cfce93
MK
8998 zvrf->l3vni = 0;
8999 zl3vni_del(zl3vni);
9000
9001 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
d62a17ae 9002 }
b7cfce93
MK
9003 return 0;
9004}
13d60d35 9005
84915b0a 9006int zebra_vxlan_vrf_enable(struct zebra_vrf *zvrf)
9007{
9008 zebra_l3vni_t *zl3vni = NULL;
9009
9010 if (zvrf->l3vni)
9011 zl3vni = zl3vni_lookup(zvrf->l3vni);
9012 if (!zl3vni)
9013 return 0;
9014
9015 zl3vni->vrf_id = zvrf_id(zvrf);
9016 if (is_l3vni_oper_up(zl3vni))
9017 zebra_vxlan_process_l3vni_oper_up(zl3vni);
9018 return 0;
9019}
9020
9021int zebra_vxlan_vrf_disable(struct zebra_vrf *zvrf)
b7cfce93
MK
9022{
9023 zebra_l3vni_t *zl3vni = NULL;
13d60d35 9024
84915b0a 9025 if (zvrf->l3vni)
9026 zl3vni = zl3vni_lookup(zvrf->l3vni);
b7cfce93 9027 if (!zl3vni)
d62a17ae 9028 return 0;
13d60d35 9029
84915b0a 9030 zl3vni->vrf_id = VRF_UNKNOWN;
b7cfce93 9031 zebra_vxlan_process_l3vni_oper_down(zl3vni);
84915b0a 9032 return 0;
9033}
9034
9035int zebra_vxlan_vrf_delete(struct zebra_vrf *zvrf)
9036{
9037 zebra_l3vni_t *zl3vni = NULL;
9038 vni_t vni;
9039
9040 if (zvrf->l3vni)
9041 zl3vni = zl3vni_lookup(zvrf->l3vni);
9042 if (!zl3vni)
9043 return 0;
9044
9045 vni = zl3vni->vni;
b7cfce93 9046 zl3vni_del(zl3vni);
84915b0a 9047 zebra_vxlan_handle_vni_transition(zvrf, vni, 0);
2232a77c 9048
d62a17ae 9049 return 0;
13d60d35 9050}
9051
fbac9605
DS
9052/*
9053 * Handle message from client to specify the flooding mechanism for
9054 * BUM packets. The default is to do head-end (ingress) replication
9055 * and the other supported option is to disable it. This applies to
9056 * all BUM traffic and disabling it applies to both the transmit and
9057 * receive direction.
9058 */
9059void zebra_vxlan_flood_control(ZAPI_HANDLER_ARGS)
9060{
9061 struct stream *s;
9062 enum vxlan_flood_control flood_ctrl;
9063
986512a3 9064 if (!EVPN_ENABLED(zvrf)) {
a0b0b5c8 9065 zlog_err("EVPN flood control for non-EVPN VRF %u",
fbac9605
DS
9066 zvrf_id(zvrf));
9067 return;
9068 }
9069
9070 s = msg;
9071 STREAM_GETC(s, flood_ctrl);
9072
9073 if (IS_ZEBRA_DEBUG_VXLAN)
9074 zlog_debug("EVPN flood control %u, currently %u",
9075 flood_ctrl, zvrf->vxlan_flood_ctrl);
9076
9077 if (zvrf->vxlan_flood_ctrl == flood_ctrl)
9078 return;
9079
9080 zvrf->vxlan_flood_ctrl = flood_ctrl;
9081
9082 /* Install or uninstall flood entries corresponding to
9083 * remote VTEPs.
9084 */
9085 hash_iterate(zvrf->vni_table, zvni_handle_flooding_remote_vteps,
9086 zvrf);
9087
9088stream_failure:
9089 return;
9090}
9091
278e26de
CS
9092/*
9093 * Handle message from client to enable/disable advertisement of svi macip
9094 * routes
9095 */
9096void zebra_vxlan_advertise_svi_macip(ZAPI_HANDLER_ARGS)
9097{
9098 struct stream *s;
9099 int advertise;
9100 vni_t vni = 0;
9101 zebra_vni_t *zvni = NULL;
9102 struct interface *ifp = NULL;
9103
986512a3 9104 if (!EVPN_ENABLED(zvrf)) {
27627f9a 9105 zlog_debug("EVPN SVI-MACIP Adv for non-EVPN VRF %u",
a0b0b5c8 9106 zvrf_id(zvrf));
278e26de
CS
9107 return;
9108 }
9109
9110 s = msg;
9111 STREAM_GETC(s, advertise);
9112 STREAM_GETL(s, vni);
9113
9114 if (!vni) {
9115 if (IS_ZEBRA_DEBUG_VXLAN)
27627f9a 9116 zlog_debug("EVPN SVI-MACIP Adv %s, currently %s",
278e26de 9117 advertise ? "enabled" : "disabled",
838cef6d 9118 advertise_svi_macip_enabled(NULL)
278e26de
CS
9119 ? "enabled"
9120 : "disabled");
9121
9122 if (zvrf->advertise_svi_macip == advertise)
9123 return;
9124
9125
9126 if (advertise) {
9127 zvrf->advertise_svi_macip = advertise;
9128 hash_iterate(zvrf->vni_table,
9129 zvni_gw_macip_add_for_vni_hash, NULL);
9130 } else {
9131 hash_iterate(zvrf->vni_table,
9132 zvni_svi_macip_del_for_vni_hash, NULL);
9133 zvrf->advertise_svi_macip = advertise;
9134 }
9135
9136 } else {
9137 struct zebra_if *zif = NULL;
9138 struct zebra_l2info_vxlan zl2_info;
9139 struct interface *vlan_if = NULL;
9140
9141 zvni = zvni_lookup(vni);
9142 if (!zvni)
9143 return;
9144
9145 if (IS_ZEBRA_DEBUG_VXLAN)
9146 zlog_debug(
9147 "EVPN SVI macip Adv %s on VNI %d , currently %s",
9148 advertise ? "enabled" : "disabled", vni,
9149 advertise_svi_macip_enabled(zvni)
9150 ? "enabled"
9151 : "disabled");
9152
9153 if (zvni->advertise_svi_macip == advertise)
9154 return;
9155
9156 ifp = zvni->vxlan_if;
9157 if (!ifp)
9158 return;
9159
9160 zif = ifp->info;
9161
9162 /* If down or not mapped to a bridge, we're done. */
9163 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
9164 return;
9165
9166 zl2_info = zif->l2info.vxl;
9167
9168 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
9169 zif->brslave_info.br_if);
9170 if (!vlan_if)
9171 return;
9172
9173 if (advertise) {
9174 zvni->advertise_svi_macip = advertise;
9175 /* Add primary SVI MAC-IP */
9176 zvni_add_macip_for_intf(vlan_if, zvni);
9177 } else {
9178 /* Del primary MAC-IP */
9179 zvni_del_macip_for_intf(vlan_if, zvni);
9180 zvni->advertise_svi_macip = advertise;
9181 }
9182 }
9183
9184stream_failure:
9185 return;
9186}
9187
31310b25
MK
9188/*
9189 * Handle message from client to enable/disable advertisement of g/w macip
9190 * routes
9191 */
89f4e507 9192void zebra_vxlan_advertise_subnet(ZAPI_HANDLER_ARGS)
31310b25
MK
9193{
9194 struct stream *s;
9195 int advertise;
9196 vni_t vni = 0;
9197 zebra_vni_t *zvni = NULL;
9198 struct interface *ifp = NULL;
9199 struct zebra_if *zif = NULL;
9200 struct zebra_l2info_vxlan zl2_info;
9201 struct interface *vlan_if = NULL;
9202
986512a3 9203 if (!EVPN_ENABLED(zvrf)) {
a0b0b5c8
T
9204 zlog_debug("EVPN GW-MACIP Adv for non-EVPN VRF %u",
9205 zvrf_id(zvrf));
8068a649 9206 return;
31310b25
MK
9207 }
9208
1002497a 9209 s = msg;
2017b3ea 9210 STREAM_GETC(s, advertise);
31310b25
MK
9211 vni = stream_get3(s);
9212
9213 zvni = zvni_lookup(vni);
9214 if (!zvni)
8068a649 9215 return;
31310b25
MK
9216
9217 if (zvni->advertise_subnet == advertise)
8068a649 9218 return;
31310b25
MK
9219
9220 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
9221 zlog_debug("EVPN subnet Adv %s on VNI %d , currently %s",
9222 advertise ? "enabled" : "disabled", vni,
9223 zvni->advertise_subnet ? "enabled" : "disabled");
31310b25
MK
9224
9225
9226 zvni->advertise_subnet = advertise;
9227
9228 ifp = zvni->vxlan_if;
9229 if (!ifp)
8068a649 9230 return;
31310b25
MK
9231
9232 zif = ifp->info;
9233
9234 /* If down or not mapped to a bridge, we're done. */
9235 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8068a649 9236 return;
31310b25
MK
9237
9238 zl2_info = zif->l2info.vxl;
9239
996c9314
LB
9240 vlan_if =
9241 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
31310b25 9242 if (!vlan_if)
8068a649 9243 return;
31310b25
MK
9244
9245 if (zvni->advertise_subnet)
9246 zvni_advertise_subnet(zvni, vlan_if, 1);
9247 else
9248 zvni_advertise_subnet(zvni, vlan_if, 0);
2017b3ea
DS
9249
9250stream_failure:
9251 return;
31310b25
MK
9252}
9253
1a98c087
MK
9254/*
9255 * Handle message from client to enable/disable advertisement of g/w macip
9256 * routes
9257 */
89f4e507 9258void zebra_vxlan_advertise_gw_macip(ZAPI_HANDLER_ARGS)
1a98c087
MK
9259{
9260 struct stream *s;
9261 int advertise;
9262 vni_t vni = 0;
9263 zebra_vni_t *zvni = NULL;
b5ebdc9b 9264 struct interface *ifp = NULL;
1a98c087 9265
986512a3 9266 if (!EVPN_ENABLED(zvrf)) {
a0b0b5c8 9267 zlog_debug("EVPN GW-MACIP Adv for non-EVPN VRF %u",
9df414fe 9268 zvrf_id(zvrf));
8068a649 9269 return;
2853fed6 9270 }
9271
1002497a 9272 s = msg;
ec93aa12 9273 STREAM_GETC(s, advertise);
cc6d5476 9274 STREAM_GETL(s, vni);
1a98c087
MK
9275
9276 if (!vni) {
9277 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 9278 zlog_debug("EVPN gateway macip Adv %s, currently %s",
1a98c087 9279 advertise ? "enabled" : "disabled",
2853fed6 9280 advertise_gw_macip_enabled(NULL)
1a98c087
MK
9281 ? "enabled"
9282 : "disabled");
9283
9284 if (zvrf->advertise_gw_macip == advertise)
8068a649 9285 return;
1a98c087
MK
9286
9287 zvrf->advertise_gw_macip = advertise;
9288
2853fed6 9289 if (advertise_gw_macip_enabled(zvni))
1a98c087 9290 hash_iterate(zvrf->vni_table,
2853fed6 9291 zvni_gw_macip_add_for_vni_hash, NULL);
1a98c087
MK
9292 else
9293 hash_iterate(zvrf->vni_table,
2853fed6 9294 zvni_gw_macip_del_for_vni_hash, NULL);
1a98c087
MK
9295
9296 } else {
9297 struct zebra_if *zif = NULL;
9298 struct zebra_l2info_vxlan zl2_info;
9299 struct interface *vlan_if = NULL;
9300 struct interface *vrr_if = NULL;
9301
01a6143b
MK
9302 zvni = zvni_lookup(vni);
9303 if (!zvni)
124ead27 9304 return;
01a6143b 9305
1a98c087
MK
9306 if (IS_ZEBRA_DEBUG_VXLAN)
9307 zlog_debug(
2853fed6 9308 "EVPN gateway macip Adv %s on VNI %d , currently %s",
1a98c087 9309 advertise ? "enabled" : "disabled", vni,
996c9314
LB
9310 advertise_gw_macip_enabled(zvni) ? "enabled"
9311 : "disabled");
1a98c087 9312
1a98c087 9313 if (zvni->advertise_gw_macip == advertise)
8068a649 9314 return;
1a98c087
MK
9315
9316 zvni->advertise_gw_macip = advertise;
9317
b5ebdc9b 9318 ifp = zvni->vxlan_if;
9319 if (!ifp)
8068a649 9320 return;
b5ebdc9b 9321
9322 zif = ifp->info;
9323
9324 /* If down or not mapped to a bridge, we're done. */
b682f6de 9325 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8068a649 9326 return;
b5ebdc9b 9327
1a98c087
MK
9328 zl2_info = zif->l2info.vxl;
9329
2853fed6 9330 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
1a98c087
MK
9331 zif->brslave_info.br_if);
9332 if (!vlan_if)
8068a649 9333 return;
1a98c087 9334
2853fed6 9335 if (advertise_gw_macip_enabled(zvni)) {
1a98c087
MK
9336 /* Add primary SVI MAC-IP */
9337 zvni_add_macip_for_intf(vlan_if, zvni);
9338
9339 /* Add VRR MAC-IP - if any*/
9340 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
9341 if (vrr_if)
9342 zvni_add_macip_for_intf(vrr_if, zvni);
9343 } else {
9344 /* Del primary MAC-IP */
9345 zvni_del_macip_for_intf(vlan_if, zvni);
9346
9347 /* Del VRR MAC-IP - if any*/
9348 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
9349 if (vrr_if)
9350 zvni_del_macip_for_intf(vrr_if, zvni);
9351 }
9352 }
9353
ec93aa12 9354stream_failure:
8068a649 9355 return;
1a98c087
MK
9356}
9357
9358
13d60d35 9359/*
9360 * Handle message from client to learn (or stop learning) about VNIs and MACs.
9361 * When enabled, the VNI hash table will be built and MAC FDB table read;
9362 * when disabled, the entries should be deleted and remote VTEPs and MACs
9363 * uninstalled from the kernel.
fbac9605
DS
9364 * This also informs the setting for BUM handling at the time this change
9365 * occurs; it is relevant only when specifying "learn".
13d60d35 9366 */
89f4e507 9367void zebra_vxlan_advertise_all_vni(ZAPI_HANDLER_ARGS)
13d60d35 9368{
655b04d1
MK
9369 struct stream *s = NULL;
9370 int advertise = 0;
fbac9605 9371 enum vxlan_flood_control flood_ctrl;
13d60d35 9372
77b998fb
T
9373 /* Mismatch between EVPN VRF and current VRF (should be prevented by
9374 * bgpd's cli) */
f920dd6d 9375 if (is_evpn_enabled() && !EVPN_ENABLED(zvrf))
8068a649 9376 return;
2853fed6 9377
1002497a 9378 s = msg;
ec93aa12 9379 STREAM_GETC(s, advertise);
fbac9605 9380 STREAM_GETC(s, flood_ctrl);
13d60d35 9381
d62a17ae 9382 if (IS_ZEBRA_DEBUG_VXLAN)
150971b5
T
9383 zlog_debug("EVPN VRF %s(%u) VNI Adv %s, currently %s, flood control %u",
9384 zvrf_name(zvrf), zvrf_id(zvrf),
d62a17ae 9385 advertise ? "enabled" : "disabled",
fbac9605
DS
9386 is_evpn_enabled() ? "enabled" : "disabled",
9387 flood_ctrl);
13d60d35 9388
d62a17ae 9389 if (zvrf->advertise_all_vni == advertise)
8068a649 9390 return;
13d60d35 9391
d62a17ae 9392 zvrf->advertise_all_vni = advertise;
f920dd6d 9393 if (EVPN_ENABLED(zvrf)) {
0fb2ad05 9394 zrouter.evpn_vrf = zvrf;
150971b5 9395
fbac9605
DS
9396 /* Note BUM handling */
9397 zvrf->vxlan_flood_ctrl = flood_ctrl;
9398
d62a17ae 9399 /* Build VNI hash table and inform BGP. */
2853fed6 9400 zvni_build_hash_table();
2232a77c 9401
1a98c087
MK
9402 /* Add all SVI (L3 GW) MACs to BGP*/
9403 hash_iterate(zvrf->vni_table, zvni_gw_macip_add_for_vni_hash,
2853fed6 9404 NULL);
1a98c087 9405
d62a17ae 9406 /* Read the MAC FDB */
9407 macfdb_read(zvrf->zns);
2232a77c 9408
d62a17ae 9409 /* Read neighbors */
9410 neigh_read(zvrf->zns);
9411 } else {
9412 /* Cleanup VTEPs for all VNIs - uninstall from
9413 * kernel and free entries.
9414 */
9415 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
655b04d1
MK
9416
9417 /* cleanup all l3vnis */
89272910 9418 hash_iterate(zrouter.l3vni_table, zl3vni_cleanup_all, NULL);
150971b5 9419
0fb2ad05
T
9420 /* Mark as "no EVPN VRF" */
9421 zrouter.evpn_vrf = NULL;
d62a17ae 9422 }
13d60d35 9423
ec93aa12 9424stream_failure:
8068a649 9425 return;
13d60d35 9426}
9427
9428/*
9429 * Allocate VNI hash table for this VRF and do other initialization.
9430 * NOTE: Currently supported only for default VRF.
9431 */
d62a17ae 9432void zebra_vxlan_init_tables(struct zebra_vrf *zvrf)
13d60d35 9433{
d62a17ae 9434 if (!zvrf)
9435 return;
9436 zvrf->vni_table = hash_create(vni_hash_keymake, vni_hash_cmp,
9437 "Zebra VRF VNI Table");
015d264c
AK
9438 zvrf->vxlan_sg_table = hash_create(zebra_vxlan_sg_hash_key_make,
9439 zebra_vxlan_sg_hash_eq, "Zebra VxLAN SG Table");
13d60d35 9440}
9441
84915b0a 9442/* Cleanup VNI info, but don't free the table. */
9443void zebra_vxlan_cleanup_tables(struct zebra_vrf *zvrf)
9444{
9445 if (!zvrf)
9446 return;
9447 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
abfa0a96 9448 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_sg_cleanup, NULL);
84915b0a 9449}
9450
13d60d35 9451/* Close all VNI handling */
d62a17ae 9452void zebra_vxlan_close_tables(struct zebra_vrf *zvrf)
13d60d35 9453{
2853fed6 9454 if (!zvrf)
9455 return;
d62a17ae 9456 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
9b67b514 9457 hash_free(zvrf->vni_table);
13d60d35 9458}
b7cfce93
MK
9459
9460/* init the l3vni table */
6548050a 9461void zebra_vxlan_init(void)
b7cfce93 9462{
89272910
DS
9463 zrouter.l3vni_table = hash_create(l3vni_hash_keymake, l3vni_hash_cmp,
9464 "Zebra VRF L3 VNI table");
0fb2ad05 9465 zrouter.evpn_vrf = NULL;
b7cfce93
MK
9466}
9467
9468/* free l3vni table */
6548050a 9469void zebra_vxlan_disable(void)
b7cfce93 9470{
89272910 9471 hash_free(zrouter.l3vni_table);
b7cfce93 9472}
d3135ba3 9473
9474/* get the l3vni svi ifindex */
9475ifindex_t get_l3vni_svi_ifindex(vrf_id_t vrf_id)
9476{
9477 zebra_l3vni_t *zl3vni = NULL;
9478
9479 zl3vni = zl3vni_from_vrf(vrf_id);
9480 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
9481 return 0;
9482
9483 return zl3vni->svi_if->ifindex;
9484}
c80a972c
CS
9485
9486static int zebra_vxlan_dad_ip_auto_recovery_exp(struct thread *t)
9487{
9488 struct zebra_vrf *zvrf = NULL;
9489 zebra_neigh_t *nbr = NULL;
9490 zebra_vni_t *zvni = NULL;
9491 char buf1[INET6_ADDRSTRLEN];
9492 char buf2[ETHER_ADDR_STRLEN];
9493
9494 nbr = THREAD_ARG(t);
9495
9496 /* since this is asynchronous we need sanity checks*/
8a3bc58e
DS
9497 zvrf = vrf_info_lookup(nbr->zvni->vrf_id);
9498 if (!zvrf)
979777b2 9499 return 0;
c80a972c
CS
9500
9501 zvni = zvni_lookup(nbr->zvni->vni);
9502 if (!zvni)
979777b2 9503 return 0;
c80a972c 9504
8a3bc58e
DS
9505 nbr = zvni_neigh_lookup(zvni, &nbr->ip);
9506 if (!nbr)
979777b2 9507 return 0;
c80a972c
CS
9508
9509 if (IS_ZEBRA_DEBUG_VXLAN)
9510 zlog_debug("%s: duplicate addr MAC %s IP %s flags 0x%x learn count %u vni %u auto recovery expired",
9511 __PRETTY_FUNCTION__,
c34e362b
CS
9512 prefix_mac2str(&nbr->emac, buf2, sizeof(buf2)),
9513 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
c80a972c
CS
9514 nbr->flags,
9515 nbr->dad_count, zvni->vni);
9516
9517 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
9518 nbr->dad_count = 0;
9519 nbr->detect_start_time.tv_sec = 0;
9520 nbr->detect_start_time.tv_usec = 0;
9521 nbr->dad_dup_detect_time = 0;
9522 nbr->dad_ip_auto_recovery_timer = NULL;
c34e362b 9523 ZEBRA_NEIGH_SET_ACTIVE(nbr);
c80a972c
CS
9524
9525 /* Send to BGP */
9526 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
9527 zvni_neigh_send_add_to_client(zvni->vni, &nbr->ip, &nbr->emac,
9528 nbr->flags, nbr->loc_seq);
9529 } else if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
9530 zvni_neigh_install(zvni, nbr);
9531 }
9532
c80a972c
CS
9533 return 0;
9534}
9535
9536static int zebra_vxlan_dad_mac_auto_recovery_exp(struct thread *t)
9537{
9538 struct zebra_vrf *zvrf = NULL;
9539 zebra_mac_t *mac = NULL;
9540 zebra_vni_t *zvni = NULL;
9541 struct listnode *node = NULL;
9542 zebra_neigh_t *nbr = NULL;
9543 char buf[ETHER_ADDR_STRLEN];
9544
9545 mac = THREAD_ARG(t);
9546
9547 /* since this is asynchronous we need sanity checks*/
8a3bc58e
DS
9548 zvrf = vrf_info_lookup(mac->zvni->vrf_id);
9549 if (!zvrf)
979777b2 9550 return 0;
c80a972c
CS
9551
9552 zvni = zvni_lookup(mac->zvni->vni);
9553 if (!zvni)
979777b2 9554 return 0;
c80a972c 9555
8a3bc58e
DS
9556 mac = zvni_mac_lookup(zvni, &mac->macaddr);
9557 if (!mac)
979777b2 9558 return 0;
c80a972c
CS
9559
9560 if (IS_ZEBRA_DEBUG_VXLAN)
9561 zlog_debug("%s: duplicate addr mac %s flags 0x%x learn count %u host count %u auto recovery expired",
9562 __PRETTY_FUNCTION__,
9563 prefix_mac2str(&mac->macaddr, buf, sizeof(buf)),
9564 mac->flags,
9565 mac->dad_count,
9566 listcount(mac->neigh_list));
9567
9568 /* Remove all IPs as duplicate associcated with this MAC */
9569 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
a4445ece 9570 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
c80a972c
CS
9571 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
9572 ZEBRA_NEIGH_SET_INACTIVE(nbr);
9573 else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE))
9574 zvni_neigh_install(zvni, nbr);
9575 }
9576
9577 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
9578 nbr->dad_count = 0;
9579 nbr->detect_start_time.tv_sec = 0;
9580 nbr->dad_dup_detect_time = 0;
9581 }
9582
9583 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
9584 mac->dad_count = 0;
9585 mac->detect_start_time.tv_sec = 0;
9586 mac->detect_start_time.tv_usec = 0;
9587 mac->dad_dup_detect_time = 0;
9588 mac->dad_mac_auto_recovery_timer = NULL;
9589
9590 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
9591 /* Inform to BGP */
9592 if (zvni_mac_send_add_to_client(zvni->vni, &mac->macaddr,
9593 mac->flags, mac->loc_seq))
9594 return -1;
9595
9596 /* Process all neighbors associated with this MAC. */
9597 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
9598
9599 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
9600 zvni_process_neigh_on_remote_mac_add(zvni, mac);
9601
9602 /* Install the entry. */
9603 zvni_mac_install(zvni, mac);
9604 }
9605
c80a972c
CS
9606 return 0;
9607}
015d264c
AK
9608
9609/************************** vxlan SG cache management ************************/
4ab3321f 9610/* Inform PIM about the mcast group */
ecbbc3a7
AK
9611static int zebra_vxlan_sg_send(struct zebra_vrf *zvrf,
9612 struct prefix_sg *sg,
9613 char *sg_str, uint16_t cmd)
4ab3321f
AK
9614{
9615 struct zserv *client = NULL;
9616 struct stream *s = NULL;
9617
9618 client = zserv_find_client(ZEBRA_ROUTE_PIM, 0);
9619 if (!client)
9620 return 0;
9621
ecbbc3a7
AK
9622 if (!CHECK_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG))
9623 return 0;
9624
4ab3321f
AK
9625 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
9626
9627 zclient_create_header(s, cmd, VRF_DEFAULT);
9628 stream_putl(s, IPV4_MAX_BYTELEN);
9629 stream_put(s, &sg->src.s_addr, IPV4_MAX_BYTELEN);
9630 stream_put(s, &sg->grp.s_addr, IPV4_MAX_BYTELEN);
9631
9632 /* Write packet size. */
9633 stream_putw_at(s, 0, stream_get_endp(s));
9634
9635 if (IS_ZEBRA_DEBUG_VXLAN)
9636 zlog_debug(
9637 "Send %s %s to %s",
9638 (cmd == ZEBRA_VXLAN_SG_ADD) ? "add" : "del", sg_str,
9639 zebra_route_string(client->proto));
9640
9641 if (cmd == ZEBRA_VXLAN_SG_ADD)
9642 client->vxlan_sg_add_cnt++;
9643 else
9644 client->vxlan_sg_del_cnt++;
9645
9646 return zserv_send_message(client, s);
9647}
9648
d8b87afe 9649static unsigned int zebra_vxlan_sg_hash_key_make(const void *p)
015d264c 9650{
d8b87afe 9651 const zebra_vxlan_sg_t *vxlan_sg = p;
015d264c
AK
9652
9653 return (jhash_2words(vxlan_sg->sg.src.s_addr,
9654 vxlan_sg->sg.grp.s_addr, 0));
9655}
9656
9657static bool zebra_vxlan_sg_hash_eq(const void *p1, const void *p2)
9658{
9659 const zebra_vxlan_sg_t *sg1 = p1;
9660 const zebra_vxlan_sg_t *sg2 = p2;
9661
9662 return ((sg1->sg.src.s_addr == sg2->sg.src.s_addr)
9663 && (sg1->sg.grp.s_addr == sg2->sg.grp.s_addr));
9664}
9665
9666static zebra_vxlan_sg_t *zebra_vxlan_sg_new(struct zebra_vrf *zvrf,
9667 struct prefix_sg *sg)
9668{
9669 zebra_vxlan_sg_t *vxlan_sg;
9670
9671 vxlan_sg = XCALLOC(MTYPE_ZVXLAN_SG, sizeof(*vxlan_sg));
9672
9673 vxlan_sg->zvrf = zvrf;
9674 vxlan_sg->sg = *sg;
9675 prefix_sg2str(sg, vxlan_sg->sg_str);
9676
9677 vxlan_sg = hash_get(zvrf->vxlan_sg_table, vxlan_sg, hash_alloc_intern);
9678
9679 if (IS_ZEBRA_DEBUG_VXLAN)
9680 zlog_debug("vxlan SG %s created", vxlan_sg->sg_str);
9681
9682 return vxlan_sg;
9683}
9684
9685static zebra_vxlan_sg_t *zebra_vxlan_sg_find(struct zebra_vrf *zvrf,
9686 struct prefix_sg *sg)
9687{
9688 zebra_vxlan_sg_t lookup;
9689
9690 lookup.sg = *sg;
9691 return hash_lookup(zvrf->vxlan_sg_table, &lookup);
9692}
9693
9694static zebra_vxlan_sg_t *zebra_vxlan_sg_add(struct zebra_vrf *zvrf,
9695 struct prefix_sg *sg)
9696{
9697 zebra_vxlan_sg_t *vxlan_sg;
9698 zebra_vxlan_sg_t *parent = NULL;
9699 struct in_addr sip;
9700
9701 vxlan_sg = zebra_vxlan_sg_find(zvrf, sg);
9702 if (vxlan_sg)
9703 return vxlan_sg;
9704
9705 /* create a *G entry for every BUM group implicitly -
9706 * 1. The SG entry is used by pimd to setup the vxlan-origination-mroute
9707 * 2. the XG entry is used by pimd to setup the
9708 * vxlan-termination-mroute
9709 */
9710 if (sg->src.s_addr) {
9711 memset(&sip, 0, sizeof(sip));
9712 parent = zebra_vxlan_sg_do_ref(zvrf, sip, sg->grp);
9713 if (!parent)
9714 return NULL;
9715 }
9716
9717 vxlan_sg = zebra_vxlan_sg_new(zvrf, sg);
9718 if (!vxlan_sg) {
9719 if (parent)
9720 zebra_vxlan_sg_do_deref(zvrf, sip, sg->grp);
9721 return vxlan_sg;
9722 }
9723
ecbbc3a7
AK
9724 zebra_vxlan_sg_send(zvrf, sg, vxlan_sg->sg_str,
9725 ZEBRA_VXLAN_SG_ADD);
4ab3321f 9726
015d264c
AK
9727 return vxlan_sg;
9728}
9729
9730static void zebra_vxlan_sg_del(zebra_vxlan_sg_t *vxlan_sg)
9731{
9732 struct in_addr sip;
9733 struct zebra_vrf *zvrf;
9734
9735 zvrf = vrf_info_lookup(VRF_DEFAULT);
9736 if (!zvrf)
9737 return;
9738
9739 /* On SG entry deletion remove the reference to its parent XG
9740 * entry
9741 */
9742 if (vxlan_sg->sg.src.s_addr) {
9743 memset(&sip, 0, sizeof(sip));
9744 zebra_vxlan_sg_do_deref(zvrf, sip, vxlan_sg->sg.grp);
9745 }
9746
ecbbc3a7
AK
9747 zebra_vxlan_sg_send(zvrf, &vxlan_sg->sg,
9748 vxlan_sg->sg_str, ZEBRA_VXLAN_SG_DEL);
4ab3321f 9749
015d264c
AK
9750 hash_release(vxlan_sg->zvrf->vxlan_sg_table, vxlan_sg);
9751
9752 if (IS_ZEBRA_DEBUG_VXLAN)
9753 zlog_debug("VXLAN SG %s deleted", vxlan_sg->sg_str);
9754
9755 XFREE(MTYPE_ZVXLAN_SG, vxlan_sg);
9756}
9757
9758static void zebra_vxlan_sg_do_deref(struct zebra_vrf *zvrf,
9759 struct in_addr sip, struct in_addr mcast_grp)
9760{
9761 zebra_vxlan_sg_t *vxlan_sg;
9762 struct prefix_sg sg;
9763
9764 sg.family = AF_INET;
9765 sg.prefixlen = IPV4_MAX_BYTELEN;
9766 sg.src = sip;
9767 sg.grp = mcast_grp;
9768 vxlan_sg = zebra_vxlan_sg_find(zvrf, &sg);
9769 if (!vxlan_sg)
9770 return;
9771
9772 if (vxlan_sg->ref_cnt)
9773 --vxlan_sg->ref_cnt;
9774
9775 if (!vxlan_sg->ref_cnt)
9776 zebra_vxlan_sg_del(vxlan_sg);
9777}
9778
9779static zebra_vxlan_sg_t *zebra_vxlan_sg_do_ref(struct zebra_vrf *zvrf,
9780 struct in_addr sip, struct in_addr mcast_grp)
9781{
9782 zebra_vxlan_sg_t *vxlan_sg;
9783 struct prefix_sg sg;
9784
9785 sg.family = AF_INET;
9786 sg.prefixlen = IPV4_MAX_BYTELEN;
9787 sg.src = sip;
9788 sg.grp = mcast_grp;
9789 vxlan_sg = zebra_vxlan_sg_add(zvrf, &sg);
9790 if (vxlan_sg)
9791 ++vxlan_sg->ref_cnt;
9792
9793 return vxlan_sg;
9794}
abfa0a96
AK
9795
9796static void zebra_vxlan_sg_deref(struct in_addr local_vtep_ip,
9797 struct in_addr mcast_grp)
9798{
9799 struct zebra_vrf *zvrf;
9800
9801 if (!local_vtep_ip.s_addr || !mcast_grp.s_addr)
9802 return;
9803
9804 zvrf = vrf_info_lookup(VRF_DEFAULT);
9805 if (!zvrf)
9806 return;
9807
9808 zebra_vxlan_sg_do_deref(zvrf, local_vtep_ip, mcast_grp);
9809}
9810
9811static void zebra_vxlan_sg_ref(struct in_addr local_vtep_ip,
9812 struct in_addr mcast_grp)
9813{
9814 struct zebra_vrf *zvrf;
9815
9816 if (!local_vtep_ip.s_addr || !mcast_grp.s_addr)
9817 return;
9818
9819 zvrf = vrf_info_lookup(VRF_DEFAULT);
9820 if (!zvrf)
9821 return;
9822 zebra_vxlan_sg_do_ref(zvrf, local_vtep_ip, mcast_grp);
9823}
9824
9825static void zebra_vxlan_sg_cleanup(struct hash_backet *backet, void *arg)
9826{
9827 zebra_vxlan_sg_t *vxlan_sg = (zebra_vxlan_sg_t *)backet->data;
9828
9829 zebra_vxlan_sg_del(vxlan_sg);
9830}
27627f9a 9831
ecbbc3a7
AK
9832static void zebra_vxlan_sg_replay_send(struct hash_backet *backet, void *arg)
9833{
9834 zebra_vxlan_sg_t *vxlan_sg = (zebra_vxlan_sg_t *)backet->data;
9835
9836 zebra_vxlan_sg_send(vxlan_sg->zvrf, &vxlan_sg->sg,
9837 vxlan_sg->sg_str, ZEBRA_VXLAN_SG_ADD);
9838}
9839
9840/* Handle message from client to replay vxlan SG entries */
9841void zebra_vxlan_sg_replay(ZAPI_HANDLER_ARGS)
9842{
9843 if (IS_ZEBRA_DEBUG_VXLAN)
9844 zlog_debug("VxLAN SG updates to PIM, start");
9845
9846 SET_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG);
9847
9848 if (!EVPN_ENABLED(zvrf)) {
8f86bb06
DS
9849 if (IS_ZEBRA_DEBUG_VXLAN)
9850 zlog_debug("VxLAN SG replay request on unexpected vrf %d",
9851 zvrf->vrf->vrf_id);
ecbbc3a7
AK
9852 return;
9853 }
9854
9855 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_sg_replay_send, NULL);
9856}
9857
27627f9a
KA
9858/************************** EVPN BGP config management ************************/
9859/* Notify Local MACs to the clienti, skips GW MAC */
9860static void zvni_send_mac_hash_entry_to_client(struct hash_bucket *bucket,
9861 void *arg)
9862{
9863 struct mac_walk_ctx *wctx = arg;
9864 zebra_mac_t *zmac = bucket->data;
9865
9866 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_DEF_GW))
9867 return;
9868
9869 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL))
9870 zvni_mac_send_add_to_client(wctx->zvni->vni, &zmac->macaddr,
9871 zmac->flags, zmac->loc_seq);
9872}
9873
9874/* Iterator to Notify Local MACs of a L2VNI */
9875static void zvni_send_mac_to_client(zebra_vni_t *zvni)
9876{
9877 struct mac_walk_ctx wctx;
9878
9879 if (!zvni->mac_table)
9880 return;
9881
9882 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
9883 wctx.zvni = zvni;
9884
9885 hash_iterate(zvni->mac_table, zvni_send_mac_hash_entry_to_client,
9886 &wctx);
9887}
9888
9889/* Notify Neighbor entries to the Client, skips the GW entry */
9890static void zvni_send_neigh_hash_entry_to_client(struct hash_bucket *bucket,
9891 void *arg)
9892{
9893 struct mac_walk_ctx *wctx = arg;
9894 zebra_neigh_t *zn = bucket->data;
9895 zebra_mac_t *zmac = NULL;
9896
9897 if (CHECK_FLAG(zn->flags, ZEBRA_NEIGH_DEF_GW))
9898 return;
9899
9900 if (CHECK_FLAG(zn->flags, ZEBRA_NEIGH_LOCAL) &&
9901 IS_ZEBRA_NEIGH_ACTIVE(zn)) {
9902 zmac = zvni_mac_lookup(wctx->zvni, &zn->emac);
9903 if (!zmac)
9904 return;
9905
9906 zvni_neigh_send_add_to_client(wctx->zvni->vni, &zn->ip,
9907 &zn->emac, zn->flags,
9908 zn->loc_seq);
9909 }
9910}
9911
9912/* Iterator of a specific L2VNI */
9913static void zvni_send_neigh_to_client(zebra_vni_t *zvni)
9914{
9915 struct neigh_walk_ctx wctx;
9916
9917 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
9918 wctx.zvni = zvni;
9919
9920 hash_iterate(zvni->neigh_table, zvni_send_neigh_hash_entry_to_client,
9921 &wctx);
9922}
9923
9924static void zvni_evpn_cfg_cleanup(struct hash_bucket *bucket, void *ctxt)
9925{
9926 zebra_vni_t *zvni = NULL;
9927
9928 zvni = (zebra_vni_t *)bucket->data;
9929 zvni->advertise_gw_macip = 0;
9930 zvni->advertise_svi_macip = 0;
9931 zvni->advertise_subnet = 0;
9932
b6587fc2 9933 zvni_neigh_del_all(zvni, 1, 0,
27627f9a 9934 DEL_REMOTE_NEIGH | DEL_REMOTE_NEIGH_FROM_VTEP);
b6587fc2 9935 zvni_mac_del_all(zvni, 1, 0,
27627f9a 9936 DEL_REMOTE_MAC | DEL_REMOTE_MAC_FROM_VTEP);
b6587fc2 9937 zvni_vtep_del_all(zvni, 1);
27627f9a
KA
9938}
9939
9940/* Cleanup EVPN configuration of a specific VRF */
9941static void zebra_evpn_vrf_cfg_cleanup(struct zebra_vrf *zvrf)
9942{
b6587fc2
CS
9943 zebra_l3vni_t *zl3vni = NULL;
9944
27627f9a
KA
9945 zvrf->advertise_all_vni = 0;
9946 zvrf->advertise_gw_macip = 0;
9947 zvrf->advertise_svi_macip = 0;
9948 zvrf->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
9949
9950 hash_iterate(zvrf->vni_table, zvni_evpn_cfg_cleanup, NULL);
b6587fc2
CS
9951
9952 if (zvrf->l3vni)
9953 zl3vni = zl3vni_lookup(zvrf->l3vni);
9954 if (zl3vni) {
9955 /* delete and uninstall all rmacs */
9956 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
9957 zl3vni);
9958 /* delete and uninstall all next-hops */
9959 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
9960 zl3vni);
9961 }
27627f9a
KA
9962}
9963
9964/* Cleanup BGP EVPN configuration upon client disconnect */
ecbbc3a7 9965static int zebra_evpn_bgp_cfg_clean_up(struct zserv *client)
27627f9a
KA
9966{
9967 struct vrf *vrf;
9968 struct zebra_vrf *zvrf;
9969
27627f9a
KA
9970 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
9971 zvrf = vrf->info;
9972 if (zvrf)
9973 zebra_evpn_vrf_cfg_cleanup(zvrf);
9974 }
9975
9976 return 0;
9977}
9978
ecbbc3a7
AK
9979static int zebra_evpn_pim_cfg_clean_up(struct zserv *client)
9980{
9981 struct zebra_vrf *zvrf = zebra_vrf_get_evpn();
9982
9983 if (CHECK_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG)) {
9984 if (IS_ZEBRA_DEBUG_VXLAN)
9985 zlog_debug("VxLAN SG updates to PIM, stop");
9986 UNSET_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG);
9987 }
9988
9989 return 0;
9990}
9991
9992static int zebra_evpn_cfg_clean_up(struct zserv *client)
9993{
9994 if (client->proto == ZEBRA_ROUTE_BGP)
9995 return zebra_evpn_bgp_cfg_clean_up(client);
9996
9997 if (client->proto == ZEBRA_ROUTE_PIM)
9998 return zebra_evpn_pim_cfg_clean_up(client);
9999
10000 return 0;
10001}
10002
036d93c0
MS
10003/*
10004 * Handle results for vxlan dataplane operations.
10005 */
10006extern void zebra_vxlan_handle_result(struct zebra_dplane_ctx *ctx)
10007{
10008 /* TODO -- anything other than freeing the context? */
10009 dplane_ctx_fini(&ctx);
10010}
10011
27627f9a
KA
10012/* Cleanup BGP EVPN configuration upon client disconnect */
10013extern void zebra_evpn_init(void)
10014{
10015 hook_register(zserv_client_close, zebra_evpn_cfg_clean_up);
10016}