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