]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_evpn_mac.c
zebra:add df flag into evpn esi json output
[mirror_frr.git] / zebra / zebra_evpn_mac.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
24268cd0
PR
2/*
3 * Zebra EVPN for VxLAN code
4 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
24268cd0
PR
5 */
6
7#include <zebra.h>
8
9#include "hash.h"
b2998086 10#include "interface.h"
24268cd0 11#include "jhash.h"
24268cd0
PR
12#include "memory.h"
13#include "prefix.h"
24268cd0 14#include "vlan.h"
b2998086 15#include "json.h"
bf902d4c 16#include "printfrr.h"
24268cd0 17
b2998086 18#include "zebra/zserv.h"
24268cd0 19#include "zebra/debug.h"
b2998086 20#include "zebra/zebra_router.h"
b2998086 21#include "zebra/zebra_errors.h"
24268cd0 22#include "zebra/zebra_vrf.h"
0adeb5fd
SR
23#include "zebra/zebra_vxlan.h"
24#include "zebra/zebra_vxlan_if.h"
b2998086 25#include "zebra/zebra_evpn.h"
24268cd0 26#include "zebra/zebra_evpn_mh.h"
b2998086
PR
27#include "zebra/zebra_evpn_mac.h"
28#include "zebra/zebra_evpn_neigh.h"
24268cd0 29
24268cd0 30DEFINE_MTYPE_STATIC(ZEBRA, MAC, "EVPN MAC");
24268cd0
PR
31
32/*
33 * Return number of valid MACs in an EVPN's MAC hash table - all
34 * remote MACs and non-internal (auto) local MACs count.
35 */
f6371c34 36uint32_t num_valid_macs(struct zebra_evpn *zevpn)
24268cd0
PR
37{
38 unsigned int i;
39 uint32_t num_macs = 0;
40 struct hash *hash;
41 struct hash_bucket *hb;
3198b2b3 42 struct zebra_mac *mac;
24268cd0
PR
43
44 hash = zevpn->mac_table;
45 if (!hash)
46 return num_macs;
47 for (i = 0; i < hash->size; i++) {
48 for (hb = hash->index[i]; hb; hb = hb->next) {
3198b2b3 49 mac = (struct zebra_mac *)hb->data;
24268cd0
PR
50 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
51 || CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
52 || !CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
53 num_macs++;
54 }
55 }
56
57 return num_macs;
58}
59
f6371c34 60uint32_t num_dup_detected_macs(struct zebra_evpn *zevpn)
24268cd0
PR
61{
62 unsigned int i;
63 uint32_t num_macs = 0;
64 struct hash *hash;
65 struct hash_bucket *hb;
3198b2b3 66 struct zebra_mac *mac;
24268cd0
PR
67
68 hash = zevpn->mac_table;
69 if (!hash)
70 return num_macs;
71 for (i = 0; i < hash->size; i++) {
72 for (hb = hash->index[i]; hb; hb = hb->next) {
3198b2b3 73 mac = (struct zebra_mac *)hb->data;
24268cd0
PR
74 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
75 num_macs++;
76 }
77 }
78
79 return num_macs;
80}
81
8b07f173
AK
82/* Setup mac_list against the access port. This is done when a mac uses
83 * the ifp as destination for the first time
84 */
85static void zebra_evpn_mac_ifp_new(struct zebra_if *zif)
86{
87 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC)
88 zlog_debug("MAC list created for ifp %s (%u)", zif->ifp->name,
89 zif->ifp->ifindex);
90
91 zif->mac_list = list_new();
92 listset_app_node_mem(zif->mac_list);
93}
94
8b07f173 95/* Unlink local mac from a destination access port */
3198b2b3 96static void zebra_evpn_mac_ifp_unlink(struct zebra_mac *zmac)
8b07f173
AK
97{
98 struct zebra_if *zif;
99 struct interface *ifp = zmac->ifp;
100
101 if (!ifp)
102 return;
103
104 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC)
105 zlog_debug("VNI %d MAC %pEA unlinked from ifp %s (%u)",
106 zmac->zevpn->vni,
107 &zmac->macaddr,
108 ifp->name, ifp->ifindex);
109
110 zif = ifp->info;
111 list_delete_node(zif->mac_list, &zmac->ifp_listnode);
112 zmac->ifp = NULL;
113}
114
51aa2693
AK
115/* Free up the mac_list if any as a part of the interface del/cleanup */
116void zebra_evpn_mac_ifp_del(struct interface *ifp)
117{
118 struct zebra_if *zif = ifp->info;
119 struct listnode *node;
120 struct zebra_mac *zmac;
121
122 if (zif->mac_list) {
123 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC)
124 zlog_debug("MAC list deleted for ifp %s (%u)",
125 zif->ifp->name, zif->ifp->ifindex);
126
127 for (ALL_LIST_ELEMENTS_RO(zif->mac_list, node, zmac)) {
128 zebra_evpn_mac_ifp_unlink(zmac);
129 }
130 list_delete(&zif->mac_list);
131 }
132}
133
8b07f173
AK
134/* Link local mac to destination access port. This is done only if the
135 * local mac is associated with a zero ESI i.e. single attach or lacp-bypass
136 * bridge port member
137 */
3198b2b3
DS
138static void zebra_evpn_mac_ifp_link(struct zebra_mac *zmac,
139 struct interface *ifp)
8b07f173
AK
140{
141 struct zebra_if *zif;
142
143 if (!CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL))
144 return;
145
146 /* already linked to the destination */
147 if (zmac->ifp == ifp)
148 return;
149
150 /* unlink the mac from any old destination */
151 if (zmac->ifp)
152 zebra_evpn_mac_ifp_unlink(zmac);
153
154 if (!ifp)
155 return;
156
157 zif = ifp->info;
158 /* the interface mac_list is created on first mac link attempt */
159 if (!zif->mac_list)
160 zebra_evpn_mac_ifp_new(zif);
161
162 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC)
163 zlog_debug("VNI %d MAC %pEA linked to ifp %s (%u)",
164 zmac->zevpn->vni,
165 &zmac->macaddr,
166 ifp->name, ifp->ifindex);
167
168 zmac->ifp = ifp;
169 listnode_init(&zmac->ifp_listnode, zmac);
170 listnode_add(zif->mac_list, &zmac->ifp_listnode);
171}
172
173/* If the mac is a local mac clear links to destination access port */
3198b2b3 174void zebra_evpn_mac_clear_fwd_info(struct zebra_mac *zmac)
8b07f173
AK
175{
176 zebra_evpn_mac_ifp_unlink(zmac);
177 memset(&zmac->fwd_info, 0, sizeof(zmac->fwd_info));
178}
179
b2998086
PR
180/*
181 * Install remote MAC into the forwarding plane.
182 */
3198b2b3 183int zebra_evpn_rem_mac_install(struct zebra_evpn *zevpn, struct zebra_mac *mac,
b2998086 184 bool was_static)
24268cd0 185{
b2998086 186 const struct zebra_if *zif, *br_zif;
8d30ff3b 187 const struct zebra_vxlan_vni *vni;
b2998086
PR
188 bool sticky;
189 enum zebra_dplane_result res;
190 const struct interface *br_ifp;
191 vlanid_t vid;
192 uint32_t nhg_id;
193 struct in_addr vtep_ip;
24268cd0 194
b2998086
PR
195 zif = zevpn->vxlan_if->info;
196 if (!zif)
197 return -1;
198
199 br_ifp = zif->brslave_info.br_if;
200 if (br_ifp == NULL)
201 return -1;
202
8d30ff3b
SR
203 vni = zebra_vxlan_if_vni_find(zif, zevpn->vni);
204 if (!vni)
205 return -1;
b2998086
PR
206
207 sticky = !!CHECK_FLAG(mac->flags,
208 (ZEBRA_MAC_STICKY | ZEBRA_MAC_REMOTE_DEF_GW));
209
210 /* If nexthop group for the FDB entry is inactive (not programmed in
211 * the dataplane) the MAC entry cannot be installed
212 */
213 if (mac->es) {
214 if (!(mac->es->flags & ZEBRA_EVPNES_NHG_ACTIVE))
215 return -1;
216 nhg_id = mac->es->nhg_id;
217 vtep_ip.s_addr = 0;
218 } else {
219 nhg_id = 0;
220 vtep_ip = mac->fwd_info.r_vtep_ip;
24268cd0
PR
221 }
222
b2998086
PR
223 br_zif = (const struct zebra_if *)(br_ifp->info);
224
225 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
8d30ff3b 226 vid = vni->access_vlan;
b2998086
PR
227 else
228 vid = 0;
229
230 res = dplane_rem_mac_add(zevpn->vxlan_if, br_ifp, vid, &mac->macaddr,
b95ce8fa 231 vni->vni, vtep_ip, sticky, nhg_id, was_static);
b2998086
PR
232 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
233 return 0;
234 else
235 return -1;
24268cd0
PR
236}
237
b2998086
PR
238/*
239 * Uninstall remote MAC from the forwarding plane.
240 */
3198b2b3
DS
241int zebra_evpn_rem_mac_uninstall(struct zebra_evpn *zevpn,
242 struct zebra_mac *mac, bool force)
24268cd0 243{
b2998086 244 const struct zebra_if *zif, *br_zif;
8d30ff3b 245 struct zebra_vxlan_vni *vni;
b2998086
PR
246 struct in_addr vtep_ip;
247 const struct interface *ifp, *br_ifp;
248 vlanid_t vid;
249 enum zebra_dplane_result res;
24268cd0 250
f3722826
AK
251 /* If the MAC was not installed there is no need to uninstall it */
252 if (!force && mac->es && !(mac->es->flags & ZEBRA_EVPNES_NHG_ACTIVE))
253 return -1;
254
b2998086
PR
255 if (!zevpn->vxlan_if) {
256 if (IS_ZEBRA_DEBUG_VXLAN)
257 zlog_debug(
258 "VNI %u hash %p couldn't be uninstalled - no intf",
259 zevpn->vni, zevpn);
260 return -1;
261 }
24268cd0 262
b2998086
PR
263 zif = zevpn->vxlan_if->info;
264 if (!zif)
265 return -1;
24268cd0 266
b2998086
PR
267 br_ifp = zif->brslave_info.br_if;
268 if (br_ifp == NULL)
269 return -1;
24268cd0 270
8d30ff3b
SR
271 vni = zebra_vxlan_if_vni_find(zif, zevpn->vni);
272 if (!vni)
273 return -1;
24268cd0 274
b2998086 275 br_zif = (const struct zebra_if *)br_ifp->info;
24268cd0 276
b2998086 277 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
8d30ff3b 278 vid = vni->access_vlan;
b2998086
PR
279 else
280 vid = 0;
24268cd0 281
b2998086
PR
282 ifp = zevpn->vxlan_if;
283 vtep_ip = mac->fwd_info.r_vtep_ip;
284
b95ce8fa
SR
285 res = dplane_rem_mac_del(ifp, br_ifp, vid, &mac->macaddr, vni->vni,
286 vtep_ip);
b2998086
PR
287 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
288 return 0;
289 else
290 return -1;
24268cd0
PR
291}
292
b2998086
PR
293/*
294 * Decrement neighbor refcount of MAC; uninstall and free it if
295 * appropriate.
24268cd0 296 */
3198b2b3 297void zebra_evpn_deref_ip2mac(struct zebra_evpn *zevpn, struct zebra_mac *mac)
24268cd0 298{
b2998086
PR
299 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
300 return;
24268cd0 301
b2998086
PR
302 /* If all remote neighbors referencing a remote MAC go away,
303 * we need to uninstall the MAC.
24268cd0 304 */
b2998086
PR
305 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
306 && remote_neigh_count(mac) == 0) {
f3722826 307 zebra_evpn_rem_mac_uninstall(zevpn, mac, false /*force*/);
b2998086
PR
308 zebra_evpn_es_mac_deref_entry(mac);
309 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
310 }
311
243b74ed
AK
312 /* If no references, delete the MAC. */
313 if (!zebra_evpn_mac_in_use(mac))
b2998086
PR
314 zebra_evpn_mac_del(zevpn, mac);
315}
316
3198b2b3 317static void zebra_evpn_mac_get_access_info(struct zebra_mac *mac,
e3f05a8a 318 struct interface **p_ifp,
d9d3455e 319 vlanid_t *vid)
b2998086 320{
8d30ff3b
SR
321 struct zebra_vxlan_vni *vni;
322
b2998086
PR
323 /* if the mac is associated with an ES we must get the access
324 * info from the ES
24268cd0 325 */
b2998086
PR
326 if (mac->es) {
327 struct zebra_if *zif;
328
329 /* get the access port from the es */
e3f05a8a 330 *p_ifp = mac->es->zif ? mac->es->zif->ifp : NULL;
b2998086
PR
331 /* get the vlan from the EVPN */
332 if (mac->zevpn->vxlan_if) {
333 zif = mac->zevpn->vxlan_if->info;
8d30ff3b
SR
334 vni = zebra_vxlan_if_vni_find(zif, mac->zevpn->vni);
335 *vid = vni->access_vlan;
b2998086
PR
336 } else {
337 *vid = 0;
338 }
339 } else {
340 struct zebra_ns *zns;
341
342 *vid = mac->fwd_info.local.vid;
47c58929 343 zns = zebra_ns_lookup(mac->fwd_info.local.ns_id);
e3f05a8a 344 *p_ifp = if_lookup_by_index_per_ns(zns,
345 mac->fwd_info.local.ifindex);
b2998086
PR
346 }
347}
348
b16e8004 349#define MAC_BUF_SIZE 256
3198b2b3 350static char *zebra_evpn_zebra_mac_flag_dump(struct zebra_mac *mac, char *buf,
b16e8004
DS
351 size_t len)
352{
353 if (mac->flags == 0) {
354 snprintfrr(buf, len, "None ");
355 return buf;
356 }
357
358 snprintfrr(
359 buf, len, "%s%s%s%s%s%s%s%s%s%s%s%s",
360 CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL) ? "LOC " : "",
361 CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) ? "REM " : "",
362 CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO) ? "AUTO " : "",
363 CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY) ? "STICKY " : "",
364 CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_RMAC) ? "REM Router "
365 : "",
366 CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW) ? "Default GW " : "",
367 CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW) ? "REM DEF GW "
368 : "",
369 CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE) ? "DUP " : "",
370 CHECK_FLAG(mac->flags, ZEBRA_MAC_FPM_SENT) ? "FPM " : "",
0ef63eb5 371 CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE)
372 ? "PEER Active "
373 : "",
b16e8004
DS
374 CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY) ? "PROXY " : "",
375 CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE)
376 ? "LOC Inactive "
377 : "");
378 return buf;
379}
380
e6685141 381static void zebra_evpn_dad_mac_auto_recovery_exp(struct event *t)
b2998086
PR
382{
383 struct zebra_vrf *zvrf = NULL;
3198b2b3 384 struct zebra_mac *mac = NULL;
f6371c34 385 struct zebra_evpn *zevpn = NULL;
b2998086 386 struct listnode *node = NULL;
72de4110 387 struct zebra_neigh *nbr = NULL;
b2998086 388
e16d030c 389 mac = EVENT_ARG(t);
b2998086
PR
390
391 /* since this is asynchronous we need sanity checks*/
9a7d1e74 392 zvrf = zebra_vrf_lookup_by_id(mac->zevpn->vrf_id);
b2998086 393 if (!zvrf)
cc9f21da 394 return;
b2998086 395
8b5fdf2e 396 zevpn = zebra_evpn_lookup(mac->zevpn->vni);
b2998086 397 if (!zevpn)
cc9f21da 398 return;
b2998086
PR
399
400 mac = zebra_evpn_mac_lookup(zevpn, &mac->macaddr);
401 if (!mac)
cc9f21da 402 return;
b2998086 403
b16e8004
DS
404 if (IS_ZEBRA_DEBUG_VXLAN) {
405 char mac_buf[MAC_BUF_SIZE];
406
b2998086 407 zlog_debug(
ef7b8be4
DL
408 "%s: duplicate addr mac %pEA flags %slearn count %u host count %u auto recovery expired",
409 __func__, &mac->macaddr,
b16e8004
DS
410 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
411 sizeof(mac_buf)),
412 mac->dad_count, listcount(mac->neigh_list));
413 }
b2998086
PR
414
415 /* Remove all IPs as duplicate associcated with this MAC */
416 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
417 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
418 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
419 ZEBRA_NEIGH_SET_INACTIVE(nbr);
420 else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE))
421 zebra_evpn_rem_neigh_install(
422 zevpn, nbr, false /*was_static*/);
423 }
24268cd0 424
24268cd0
PR
425 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
426 nbr->dad_count = 0;
427 nbr->detect_start_time.tv_sec = 0;
b2998086
PR
428 nbr->dad_dup_detect_time = 0;
429 }
430
431 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
432 mac->dad_count = 0;
433 mac->detect_start_time.tv_sec = 0;
434 mac->detect_start_time.tv_usec = 0;
435 mac->dad_dup_detect_time = 0;
436 mac->dad_mac_auto_recovery_timer = NULL;
437
438 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
439 /* Inform to BGP */
440 if (zebra_evpn_mac_send_add_to_client(zevpn->vni, &mac->macaddr,
441 mac->flags, mac->loc_seq,
442 mac->es))
cc9f21da 443 return;
b2998086
PR
444
445 /* Process all neighbors associated with this MAC. */
446 zebra_evpn_process_neigh_on_local_mac_change(zevpn, mac, 0,
447 0 /*es_change*/);
448
449 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
450 zebra_evpn_process_neigh_on_remote_mac_add(zevpn, mac);
451
452 /* Install the entry. */
453 zebra_evpn_rem_mac_install(zevpn, mac, false /* was_static */);
24268cd0 454 }
24268cd0
PR
455}
456
d9d3455e 457static void zebra_evpn_dup_addr_detect_for_mac(struct zebra_vrf *zvrf,
3198b2b3 458 struct zebra_mac *mac,
d9d3455e
PR
459 struct in_addr vtep_ip,
460 bool do_dad, bool *is_dup_detect,
461 bool is_local)
24268cd0 462{
72de4110 463 struct zebra_neigh *nbr;
24268cd0
PR
464 struct listnode *node = NULL;
465 struct timeval elapsed = {0, 0};
24268cd0
PR
466 bool reset_params = false;
467
b2ee2b71 468 if (!(zebra_evpn_do_dup_addr_detect(zvrf) && do_dad))
24268cd0
PR
469 return;
470
471 /* MAC is detected as duplicate,
472 * Local MAC event -> hold on advertising to BGP.
473 * Remote MAC event -> hold on installing it.
474 */
475 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
b16e8004
DS
476 if (IS_ZEBRA_DEBUG_VXLAN) {
477 char mac_buf[MAC_BUF_SIZE];
478
24268cd0 479 zlog_debug(
ef7b8be4
DL
480 "%s: duplicate addr MAC %pEA flags %sskip update to client, learn count %u recover time %u",
481 __func__, &mac->macaddr,
b16e8004
DS
482 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
483 sizeof(mac_buf)),
484 mac->dad_count, zvrf->dad_freeze_time);
485 }
24268cd0
PR
486 /* For duplicate MAC do not update
487 * client but update neigh due to
488 * this MAC update.
489 */
490 if (zvrf->dad_freeze)
491 *is_dup_detect = true;
492
493 return;
494 }
495
496 /* Check if detection time (M-secs) expired.
497 * Reset learn count and detection start time.
498 */
499 monotime_since(&mac->detect_start_time, &elapsed);
500 reset_params = (elapsed.tv_sec > zvrf->dad_time);
501 if (is_local && !reset_params) {
502 /* RFC-7432: A PE/VTEP that detects a MAC mobility
503 * event via LOCAL learning starts an M-second timer.
504 *
505 * NOTE: This is the START of the probe with count is
506 * 0 during LOCAL learn event.
507 * (mac->dad_count == 0 || elapsed.tv_sec >= zvrf->dad_time)
508 */
509 reset_params = !mac->dad_count;
510 }
511
512 if (reset_params) {
b16e8004
DS
513 if (IS_ZEBRA_DEBUG_VXLAN) {
514 char mac_buf[MAC_BUF_SIZE];
515
24268cd0 516 zlog_debug(
ef7b8be4
DL
517 "%s: duplicate addr MAC %pEA flags %sdetection time passed, reset learn count %u",
518 __func__, &mac->macaddr,
b16e8004
DS
519 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
520 sizeof(mac_buf)),
521 mac->dad_count);
522 }
24268cd0
PR
523
524 mac->dad_count = 0;
525 /* Start dup. addr detection (DAD) start time,
526 * ONLY during LOCAL learn.
527 */
528 if (is_local)
529 monotime(&mac->detect_start_time);
530
531 } else if (!is_local) {
532 /* For REMOTE MAC, increment detection count
533 * ONLY while in probe window, once window passed,
534 * next local learn event should trigger DAD.
535 */
536 mac->dad_count++;
537 }
538
539 /* For LOCAL MAC learn event, once count is reset above via either
540 * initial/start detection time or passed the probe time, the count
541 * needs to be incremented.
542 */
543 if (is_local)
544 mac->dad_count++;
545
546 if (mac->dad_count >= zvrf->dad_max_moves) {
547 flog_warn(EC_ZEBRA_DUP_MAC_DETECTED,
ef7b8be4
DL
548 "VNI %u: MAC %pEA detected as duplicate during %s VTEP %pI4",
549 mac->zevpn->vni, &mac->macaddr,
24268cd0 550 is_local ? "local update, last" :
9bcef951 551 "remote update, from", &vtep_ip);
24268cd0
PR
552
553 SET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
554
555 /* Capture Duplicate detection time */
556 mac->dad_dup_detect_time = monotime(NULL);
557
558 /* Mark all IPs/Neighs as duplicate
559 * associcated with this MAC
560 */
561 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
562
563 /* Ony Mark IPs which are Local */
564 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
565 continue;
566
567 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
568
569 nbr->dad_dup_detect_time = monotime(NULL);
570
571 flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
ef7b8be4
DL
572 "VNI %u: MAC %pEA IP %pIA detected as duplicate during %s update, inherit duplicate from MAC",
573 mac->zevpn->vni, &mac->macaddr, &nbr->ip,
24268cd0
PR
574 is_local ? "local" : "remote");
575 }
576
577 /* Start auto recovery timer for this MAC */
e16d030c 578 EVENT_OFF(mac->dad_mac_auto_recovery_timer);
24268cd0 579 if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
b16e8004
DS
580 if (IS_ZEBRA_DEBUG_VXLAN) {
581 char mac_buf[MAC_BUF_SIZE];
582
24268cd0 583 zlog_debug(
ef7b8be4
DL
584 "%s: duplicate addr MAC %pEA flags %sauto recovery time %u start",
585 __func__, &mac->macaddr,
b16e8004
DS
586 zebra_evpn_zebra_mac_flag_dump(
587 mac, mac_buf, sizeof(mac_buf)),
588 zvrf->dad_freeze_time);
589 }
24268cd0 590
907a2395
DS
591 event_add_timer(zrouter.master,
592 zebra_evpn_dad_mac_auto_recovery_exp,
593 mac, zvrf->dad_freeze_time,
594 &mac->dad_mac_auto_recovery_timer);
24268cd0
PR
595 }
596
597 /* In case of local update, do not inform to client (BGPd),
598 * upd_neigh for neigh sequence change.
599 */
600 if (zvrf->dad_freeze)
601 *is_dup_detect = true;
602 }
603}
604
b2998086
PR
605/*
606 * Print a specific MAC entry.
607 */
3198b2b3 608void zebra_evpn_print_mac(struct zebra_mac *mac, void *ctxt, json_object *json)
24268cd0 609{
b2998086 610 struct vty *vty;
72de4110 611 struct zebra_neigh *n = NULL;
b2998086
PR
612 struct listnode *node = NULL;
613 char buf1[ETHER_ADDR_STRLEN];
614 char buf2[INET6_ADDRSTRLEN];
615 struct zebra_vrf *zvrf;
616 struct timeval detect_start_time = {0, 0};
617 char timebuf[MONOTIME_STRLEN];
70d4d90c 618 char thread_buf[EVENT_TIMER_STRLEN];
f1dbb1c7
DS
619 time_t uptime;
620 char up_str[MONOTIME_STRLEN];
24268cd0 621
b2998086 622 zvrf = zebra_vrf_get_evpn();
b2998086
PR
623 vty = (struct vty *)ctxt;
624 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
24268cd0 625
f1dbb1c7
DS
626 uptime = monotime(NULL);
627 uptime -= mac->uptime;
628
629 frrtime_to_interval(uptime, up_str, sizeof(up_str));
630
b2998086
PR
631 if (json) {
632 json_object *json_mac = json_object_new_object();
24268cd0 633
b2998086
PR
634 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
635 struct interface *ifp;
636 vlanid_t vid;
24268cd0 637
b2998086
PR
638 zebra_evpn_mac_get_access_info(mac, &ifp, &vid);
639 json_object_string_add(json_mac, "type", "local");
640 if (ifp) {
641 json_object_string_add(json_mac, "intf",
642 ifp->name);
643 json_object_int_add(json_mac, "ifindex",
644 ifp->ifindex);
645 }
646 if (vid)
647 json_object_int_add(json_mac, "vlan", vid);
648 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
649 json_object_string_add(json_mac, "type", "remote");
283ef1b0
PJD
650 if (mac->es)
651 json_object_string_add(json_mac, "remoteEs",
652 mac->es->esi_str);
653 else
654 json_object_string_addf(
655 json_mac, "remoteVtep", "%pI4",
656 &mac->fwd_info.r_vtep_ip);
b2998086
PR
657 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
658 json_object_string_add(json_mac, "type", "auto");
24268cd0 659
b2998086
PR
660 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
661 json_object_boolean_true_add(json_mac, "stickyMac");
24268cd0 662
243b74ed
AK
663 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_SVI))
664 json_object_boolean_true_add(json_mac, "sviMac");
665
b2998086
PR
666 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
667 json_object_boolean_true_add(json_mac,
668 "defaultGateway");
24268cd0 669
b2998086
PR
670 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
671 json_object_boolean_true_add(json_mac,
672 "remoteGatewayMac");
24268cd0 673
f1dbb1c7 674 json_object_string_add(json_mac, "uptime", up_str);
b2998086
PR
675 json_object_int_add(json_mac, "localSequence", mac->loc_seq);
676 json_object_int_add(json_mac, "remoteSequence", mac->rem_seq);
24268cd0 677
b2998086
PR
678 json_object_int_add(json_mac, "detectionCount", mac->dad_count);
679 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
680 json_object_boolean_true_add(json_mac, "isDuplicate");
681 else
682 json_object_boolean_false_add(json_mac, "isDuplicate");
24268cd0 683
b2998086
PR
684 json_object_int_add(json_mac, "syncNeighCount",
685 mac->sync_neigh_cnt);
686 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE))
687 json_object_boolean_true_add(json_mac, "localInactive");
688 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY))
689 json_object_boolean_true_add(json_mac, "peerProxy");
690 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE))
691 json_object_boolean_true_add(json_mac, "peerActive");
692 if (mac->hold_timer)
693 json_object_string_add(
694 json_mac, "peerActiveHold",
5f6eaa9b
DS
695 event_timer_to_hhmmss(thread_buf,
696 sizeof(thread_buf),
697 mac->hold_timer));
b2998086
PR
698 if (mac->es)
699 json_object_string_add(json_mac, "esi",
700 mac->es->esi_str);
701 /* print all the associated neigh */
702 if (!listcount(mac->neigh_list))
703 json_object_string_add(json_mac, "neighbors", "none");
704 else {
705 json_object *json_active_nbrs = json_object_new_array();
706 json_object *json_inactive_nbrs =
707 json_object_new_array();
708 json_object *json_nbrs = json_object_new_object();
24268cd0 709
b2998086
PR
710 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
711 if (IS_ZEBRA_NEIGH_ACTIVE(n))
712 json_object_array_add(
713 json_active_nbrs,
714 json_object_new_string(
715 ipaddr2str(
716 &n->ip, buf2,
717 sizeof(buf2))));
718 else
719 json_object_array_add(
720 json_inactive_nbrs,
721 json_object_new_string(
722 ipaddr2str(
723 &n->ip, buf2,
724 sizeof(buf2))));
725 }
24268cd0 726
b2998086
PR
727 json_object_object_add(json_nbrs, "active",
728 json_active_nbrs);
729 json_object_object_add(json_nbrs, "inactive",
730 json_inactive_nbrs);
731 json_object_object_add(json_mac, "neighbors",
732 json_nbrs);
24268cd0 733 }
24268cd0 734
b2998086
PR
735 json_object_object_add(json, buf1, json_mac);
736 } else {
737 vty_out(vty, "MAC: %s\n", buf1);
24268cd0 738
b2998086
PR
739 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
740 struct interface *ifp;
741 vlanid_t vid;
24268cd0 742
b2998086 743 zebra_evpn_mac_get_access_info(mac, &ifp, &vid);
24268cd0 744
b2998086
PR
745 if (mac->es)
746 vty_out(vty, " ESI: %s\n", mac->es->esi_str);
24268cd0 747
b2998086
PR
748 if (ifp)
749 vty_out(vty, " Intf: %s(%u)", ifp->name,
750 ifp->ifindex);
751 else
752 vty_out(vty, " Intf: -");
753 vty_out(vty, " VLAN: %u", vid);
754 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
755 if (mac->es)
756 vty_out(vty, " Remote ES: %s",
757 mac->es->esi_str);
758 else
9bcef951
MS
759 vty_out(vty, " Remote VTEP: %pI4",
760 &mac->fwd_info.r_vtep_ip);
b2998086
PR
761 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
762 vty_out(vty, " Auto Mac ");
763 }
24268cd0 764
b2998086
PR
765 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
766 vty_out(vty, " Sticky Mac ");
24268cd0 767
243b74ed
AK
768 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_SVI))
769 vty_out(vty, " SVI-Mac ");
770
b2998086
PR
771 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
772 vty_out(vty, " Default-gateway Mac ");
773
774 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
775 vty_out(vty, " Remote-gateway Mac ");
776
777 vty_out(vty, "\n");
778 vty_out(vty, " Sync-info: neigh#: %u", mac->sync_neigh_cnt);
779 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE))
24268cd0 780 vty_out(vty, " local-inactive");
b2998086 781 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY))
24268cd0 782 vty_out(vty, " peer-proxy");
b2998086 783 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE))
24268cd0 784 vty_out(vty, " peer-active");
b2998086 785 if (mac->hold_timer)
24268cd0 786 vty_out(vty, " (ht: %s)",
5f6eaa9b
DS
787 event_timer_to_hhmmss(thread_buf,
788 sizeof(thread_buf),
789 mac->hold_timer));
b2998086 790 vty_out(vty, "\n");
f1dbb1c7 791 vty_out(vty, " Local Seq: %u Remote Seq: %u\n", mac->loc_seq,
b2998086 792 mac->rem_seq);
f1dbb1c7 793 vty_out(vty, " Uptime: %s\n", up_str);
24268cd0 794
b2998086 795 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
24268cd0 796 vty_out(vty, " Duplicate, detected at %s",
b2998086 797 time_to_string(mac->dad_dup_detect_time,
24268cd0 798 timebuf));
b2998086
PR
799 } else if (mac->dad_count) {
800 monotime_since(&mac->detect_start_time,
24268cd0
PR
801 &detect_start_time);
802 if (detect_start_time.tv_sec <= zvrf->dad_time) {
b2998086 803 time_to_string(mac->detect_start_time.tv_sec,
24268cd0
PR
804 timebuf);
805 vty_out(vty,
806 " Duplicate detection started at %s, detection count %u\n",
b2998086 807 timebuf, mac->dad_count);
24268cd0
PR
808 }
809 }
24268cd0 810
b2998086
PR
811 /* print all the associated neigh */
812 vty_out(vty, " Neighbors:\n");
813 if (!listcount(mac->neigh_list))
814 vty_out(vty, " No Neighbors\n");
815 else {
816 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
817 vty_out(vty, " %s %s\n",
818 ipaddr2str(&n->ip, buf2, sizeof(buf2)),
819 (IS_ZEBRA_NEIGH_ACTIVE(n)
820 ? "Active"
821 : "Inactive"));
822 }
823 }
24268cd0 824
b2998086 825 vty_out(vty, "\n");
24268cd0
PR
826 }
827}
828
3198b2b3 829static char *zebra_evpn_print_mac_flags(struct zebra_mac *mac, char *flags_buf,
700cae76 830 size_t flags_buf_sz)
24268cd0 831{
b2998086 832 snprintf(flags_buf, flags_buf_sz, "%s%s%s%s",
00a7710c
AK
833 mac->sync_neigh_cnt ? "N" : "",
834 (mac->flags & ZEBRA_MAC_ES_PEER_ACTIVE) ? "P" : "",
835 (mac->flags & ZEBRA_MAC_ES_PEER_PROXY) ? "X" : "",
836 (mac->flags & ZEBRA_MAC_LOCAL_INACTIVE) ? "I" : "");
24268cd0
PR
837
838 return flags_buf;
839}
840
841/*
b2998086 842 * Print MAC hash entry - called for display of all MACs.
24268cd0 843 */
b2998086 844void zebra_evpn_print_mac_hash(struct hash_bucket *bucket, void *ctxt)
24268cd0
PR
845{
846 struct vty *vty;
b2998086 847 json_object *json_mac_hdr = NULL, *json_mac = NULL;
3198b2b3 848 struct zebra_mac *mac;
24268cd0 849 char buf1[ETHER_ADDR_STRLEN];
9bcef951 850 char addr_buf[PREFIX_STRLEN];
b2998086 851 struct mac_walk_ctx *wctx = ctxt;
24268cd0
PR
852 char flags_buf[6];
853
854 vty = wctx->vty;
b2998086 855 json_mac_hdr = wctx->json;
3198b2b3 856 mac = (struct zebra_mac *)bucket->data;
b2998086
PR
857
858 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
859
860 if (json_mac_hdr)
861 json_mac = json_object_new_object();
24268cd0 862
b2998086
PR
863 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
864 struct interface *ifp;
865 vlanid_t vid;
24268cd0 866
b2998086 867 if (wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP)
24268cd0
PR
868 return;
869
b2998086
PR
870 zebra_evpn_mac_get_access_info(mac, &ifp, &vid);
871 if (json_mac_hdr == NULL) {
872 vty_out(vty, "%-17s %-6s %-5s %-30s", buf1, "local",
873 zebra_evpn_print_mac_flags(mac, flags_buf,
874 sizeof(flags_buf)),
875 ifp ? ifp->name : "-");
876 } else {
877 json_object_string_add(json_mac, "type", "local");
878 if (ifp)
879 json_object_string_add(json_mac, "intf",
880 ifp->name);
881 }
882 if (vid) {
883 if (json_mac_hdr == NULL)
884 vty_out(vty, " %-5u", vid);
885 else
886 json_object_int_add(json_mac, "vlan", vid);
887 } else /* No vid? fill out the space */
888 if (json_mac_hdr == NULL)
889 vty_out(vty, " %-5s", "");
890 if (json_mac_hdr == NULL) {
891 vty_out(vty, " %u/%u", mac->loc_seq, mac->rem_seq);
892 vty_out(vty, "\n");
24268cd0 893 } else {
b2998086
PR
894 json_object_int_add(json_mac, "localSequence",
895 mac->loc_seq);
896 json_object_int_add(json_mac, "remoteSequence",
897 mac->rem_seq);
898 json_object_int_add(json_mac, "detectionCount",
899 mac->dad_count);
900 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
901 json_object_boolean_true_add(json_mac,
24268cd0
PR
902 "isDuplicate");
903 else
b2998086 904 json_object_boolean_false_add(json_mac,
24268cd0 905 "isDuplicate");
b2998086 906 json_object_object_add(json_mac_hdr, buf1, json_mac);
24268cd0 907 }
b2998086 908
24268cd0 909 wctx->count++;
b2998086
PR
910
911 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
912
913 if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP)
914 && !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip,
915 &wctx->r_vtep_ip))
24268cd0
PR
916 return;
917
b2998086
PR
918 if (json_mac_hdr == NULL) {
919 if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP)
920 && (wctx->count == 0)) {
921 vty_out(vty, "\nVNI %u\n\n", wctx->zevpn->vni);
922 vty_out(vty, "%-17s %-6s %-5s%-30s %-5s %s\n",
923 "MAC", "Type", "Flags",
924 "Intf/Remote ES/VTEP", "VLAN",
925 "Seq #'s");
24268cd0 926 }
9bcef951
MS
927 if (mac->es == NULL)
928 inet_ntop(AF_INET, &mac->fwd_info.r_vtep_ip,
929 addr_buf, sizeof(addr_buf));
930
b2998086
PR
931 vty_out(vty, "%-17s %-6s %-5s %-30s %-5s %u/%u\n", buf1,
932 "remote",
933 zebra_evpn_print_mac_flags(mac, flags_buf,
9bcef951
MS
934 sizeof(flags_buf)),
935 mac->es ? mac->es->esi_str : addr_buf,
b2998086
PR
936 "", mac->loc_seq, mac->rem_seq);
937 } else {
938 json_object_string_add(json_mac, "type", "remote");
283ef1b0
PJD
939 if (mac->es)
940 json_object_string_add(json_mac, "remoteEs",
941 mac->es->esi_str);
942 else
943 json_object_string_addf(
944 json_mac, "remoteVtep", "%pI4",
945 &mac->fwd_info.r_vtep_ip);
b2998086
PR
946 json_object_object_add(json_mac_hdr, buf1, json_mac);
947 json_object_int_add(json_mac, "localSequence",
948 mac->loc_seq);
949 json_object_int_add(json_mac, "remoteSequence",
950 mac->rem_seq);
951 json_object_int_add(json_mac, "detectionCount",
952 mac->dad_count);
953 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
954 json_object_boolean_true_add(json_mac,
955 "isDuplicate");
956 else
957 json_object_boolean_false_add(json_mac,
958 "isDuplicate");
959 }
24268cd0 960
b2998086
PR
961 wctx->count++;
962 }
24268cd0
PR
963}
964
965/*
b2998086 966 * Print MAC hash entry in detail - called for display of all MACs.
24268cd0 967 */
b2998086 968void zebra_evpn_print_mac_hash_detail(struct hash_bucket *bucket, void *ctxt)
24268cd0 969{
b2998086
PR
970 struct vty *vty;
971 json_object *json_mac_hdr = NULL;
3198b2b3 972 struct zebra_mac *mac;
b2998086
PR
973 struct mac_walk_ctx *wctx = ctxt;
974 char buf1[ETHER_ADDR_STRLEN];
24268cd0 975
b2998086
PR
976 vty = wctx->vty;
977 json_mac_hdr = wctx->json;
3198b2b3 978 mac = (struct zebra_mac *)bucket->data;
b2998086 979 if (!mac)
24268cd0
PR
980 return;
981
b2998086
PR
982 wctx->count++;
983 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
24268cd0 984
b2998086 985 zebra_evpn_print_mac(mac, vty, json_mac_hdr);
24268cd0
PR
986}
987
988/*
b2998086 989 * Inform BGP about local MACIP.
24268cd0 990 */
7f7e49d1
MS
991int zebra_evpn_macip_send_msg_to_client(vni_t vni,
992 const struct ethaddr *macaddr,
1a3bd37f 993 const struct ipaddr *ip, uint8_t flags,
b2998086
PR
994 uint32_t seq, int state,
995 struct zebra_evpn_es *es, uint16_t cmd)
24268cd0 996{
b2998086
PR
997 int ipa_len;
998 struct zserv *client = NULL;
999 struct stream *s = NULL;
1000 esi_t *esi = es ? &es->esi : zero_esi;
24268cd0 1001
b2998086
PR
1002 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
1003 /* BGP may not be running. */
1004 if (!client)
1005 return 0;
24268cd0 1006
b2998086 1007 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
24268cd0 1008
b2998086
PR
1009 zclient_create_header(s, cmd, zebra_vrf_get_evpn_id());
1010 stream_putl(s, vni);
1011 stream_put(s, macaddr->octet, ETH_ALEN);
1012 if (ip) {
1013 ipa_len = 0;
1014 if (IS_IPADDR_V4(ip))
1015 ipa_len = IPV4_MAX_BYTELEN;
1016 else if (IS_IPADDR_V6(ip))
1017 ipa_len = IPV6_MAX_BYTELEN;
24268cd0 1018
b2998086
PR
1019 stream_putl(s, ipa_len); /* IP address length */
1020 if (ipa_len)
1021 stream_put(s, &ip->ip.addr, ipa_len); /* IP address */
1022 } else
1023 stream_putl(s, 0); /* Just MAC. */
24268cd0 1024
b2998086
PR
1025 if (cmd == ZEBRA_MACIP_ADD) {
1026 stream_putc(s, flags); /* sticky mac/gateway mac */
1027 stream_putl(s, seq); /* sequence number */
1028 stream_put(s, esi, sizeof(esi_t));
24268cd0 1029 } else {
b2998086
PR
1030 stream_putl(s, state); /* state - active/inactive */
1031 }
24268cd0 1032
24268cd0 1033
b2998086
PR
1034 /* Write packet size. */
1035 stream_putw_at(s, 0, stream_get_endp(s));
24268cd0 1036
bf902d4c
DS
1037 if (IS_ZEBRA_DEBUG_VXLAN) {
1038 char flag_buf[MACIP_BUF_SIZE];
1039
b2998086 1040 zlog_debug(
89844a96 1041 "Send MACIP %s f %s state %u MAC %pEA IP %pIA seq %u L2-VNI %u ESI %s to %s",
bf902d4c
DS
1042 (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1043 zclient_evpn_dump_macip_flags(flags, flag_buf,
1044 sizeof(flag_buf)),
89844a96 1045 state, macaddr, ip, seq, vni, es ? es->esi_str : "-",
b2998086 1046 zebra_route_string(client->proto));
bf902d4c 1047 }
24268cd0 1048
b2998086
PR
1049 if (cmd == ZEBRA_MACIP_ADD)
1050 client->macipadd_cnt++;
1051 else
1052 client->macipdel_cnt++;
24268cd0 1053
b2998086
PR
1054 return zserv_send_message(client, s);
1055}
24268cd0 1056
b2998086
PR
1057static unsigned int mac_hash_keymake(const void *p)
1058{
3198b2b3 1059 const struct zebra_mac *pmac = p;
b2998086 1060 const void *pnt = (void *)pmac->macaddr.octet;
24268cd0 1061
b2998086 1062 return jhash(pnt, ETH_ALEN, 0xa5a5a55a);
24268cd0
PR
1063}
1064
1065/*
b2998086 1066 * Compare two MAC addresses.
24268cd0 1067 */
b2998086 1068static bool mac_cmp(const void *p1, const void *p2)
24268cd0 1069{
3198b2b3
DS
1070 const struct zebra_mac *pmac1 = p1;
1071 const struct zebra_mac *pmac2 = p2;
24268cd0 1072
b2998086
PR
1073 if (pmac1 == NULL && pmac2 == NULL)
1074 return true;
24268cd0 1075
b2998086
PR
1076 if (pmac1 == NULL || pmac2 == NULL)
1077 return false;
24268cd0 1078
b2998086
PR
1079 return (memcmp(pmac1->macaddr.octet, pmac2->macaddr.octet, ETH_ALEN)
1080 == 0);
1081}
24268cd0 1082
b2998086
PR
1083/*
1084 * Callback to allocate MAC hash entry.
1085 */
1086static void *zebra_evpn_mac_alloc(void *p)
1087{
3198b2b3
DS
1088 const struct zebra_mac *tmp_mac = p;
1089 struct zebra_mac *mac;
24268cd0 1090
3198b2b3 1091 mac = XCALLOC(MTYPE_MAC, sizeof(struct zebra_mac));
b2998086 1092 *mac = *tmp_mac;
24268cd0 1093
b2998086
PR
1094 return ((void *)mac);
1095}
24268cd0 1096
b2998086
PR
1097/*
1098 * Add MAC entry.
1099 */
3198b2b3
DS
1100struct zebra_mac *zebra_evpn_mac_add(struct zebra_evpn *zevpn,
1101 const struct ethaddr *macaddr)
b2998086 1102{
3198b2b3
DS
1103 struct zebra_mac tmp_mac;
1104 struct zebra_mac *mac = NULL;
24268cd0 1105
6006b807 1106 memset(&tmp_mac, 0, sizeof(tmp_mac));
b2998086
PR
1107 memcpy(&tmp_mac.macaddr, macaddr, ETH_ALEN);
1108 mac = hash_get(zevpn->mac_table, &tmp_mac, zebra_evpn_mac_alloc);
24268cd0 1109
b2998086
PR
1110 mac->zevpn = zevpn;
1111 mac->dad_mac_auto_recovery_timer = NULL;
24268cd0 1112
b2998086
PR
1113 mac->neigh_list = list_new();
1114 mac->neigh_list->cmp = neigh_list_cmp;
24268cd0 1115
f1dbb1c7 1116 mac->uptime = monotime(NULL);
b2998086 1117 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
b16e8004 1118 char mac_buf[MAC_BUF_SIZE];
24268cd0 1119
ef7b8be4
DL
1120 zlog_debug("%s: MAC %pEA flags %s", __func__,
1121 &mac->macaddr,
b16e8004
DS
1122 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1123 sizeof(mac_buf)));
b2998086
PR
1124 }
1125 return mac;
24268cd0
PR
1126}
1127
1128/*
b2998086 1129 * Delete MAC entry.
24268cd0 1130 */
3198b2b3 1131int zebra_evpn_mac_del(struct zebra_evpn *zevpn, struct zebra_mac *mac)
24268cd0 1132{
3198b2b3 1133 struct zebra_mac *tmp_mac;
24268cd0 1134
b2998086 1135 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
b16e8004 1136 char mac_buf[MAC_BUF_SIZE];
b2998086 1137
ef7b8be4
DL
1138 zlog_debug("%s: MAC %pEA flags %s", __func__,
1139 &mac->macaddr,
b16e8004
DS
1140 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1141 sizeof(mac_buf)));
24268cd0
PR
1142 }
1143
b2998086
PR
1144 /* force de-ref any ES entry linked to the MAC */
1145 zebra_evpn_es_mac_deref_entry(mac);
24268cd0 1146
8b07f173
AK
1147 /* remove links to the destination access port */
1148 zebra_evpn_mac_clear_fwd_info(mac);
1149
b2998086
PR
1150 /* Cancel proxy hold timer */
1151 zebra_evpn_mac_stop_hold_timer(mac);
24268cd0 1152
b2998086 1153 /* Cancel auto recovery */
e16d030c 1154 EVENT_OFF(mac->dad_mac_auto_recovery_timer);
24268cd0 1155
d5fdae8f
CS
1156 /* If the MAC is freed before the neigh we will end up
1157 * with a stale pointer against the neigh.
1158 * The situation can arise when a MAC is in remote state
1159 * and its associated neigh is local state.
1160 * zebra_evpn_cfg_cleanup() cleans up remote neighs and MACs.
1161 * Instead of deleting remote MAC, if its neigh list is non-empty
1162 * (associated to local neighs), mark the MAC as AUTO.
1163 */
1164 if (!list_isempty(mac->neigh_list)) {
1165 if (IS_ZEBRA_DEBUG_VXLAN)
1166 zlog_debug(
1167 "MAC %pEA (flags 0x%x vni %u) has non-empty neigh list "
1168 "count %u, mark MAC as AUTO",
1169 &mac->macaddr, mac->flags, zevpn->vni,
1170 listcount(mac->neigh_list));
1171
1172 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
1173 return 0;
1174 }
1175
b2998086 1176 list_delete(&mac->neigh_list);
24268cd0 1177
b2998086
PR
1178 /* Free the VNI hash entry and allocated memory. */
1179 tmp_mac = hash_release(zevpn->mac_table, mac);
1180 XFREE(MTYPE_MAC, tmp_mac);
24268cd0 1181
b2998086
PR
1182 return 0;
1183}
24268cd0 1184
852d9f97
SW
1185/*
1186 * Add Auto MAC entry.
1187 */
1188struct zebra_mac *zebra_evpn_mac_add_auto(struct zebra_evpn *zevpn,
1189 const struct ethaddr *macaddr)
1190{
1191 struct zebra_mac *mac;
1192
1193 mac = zebra_evpn_mac_add(zevpn, macaddr);
1194 if (!mac)
1195 return NULL;
1196
1197 zebra_evpn_mac_clear_fwd_info(mac);
1198 memset(&mac->flags, 0, sizeof(uint32_t));
1199 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
1200
1201 return mac;
1202}
1203
b2998086 1204static bool zebra_evpn_check_mac_del_from_db(struct mac_walk_ctx *wctx,
3198b2b3 1205 struct zebra_mac *mac)
b2998086
PR
1206{
1207 if ((wctx->flags & DEL_LOCAL_MAC) && (mac->flags & ZEBRA_MAC_LOCAL))
1208 return true;
1209 else if ((wctx->flags & DEL_REMOTE_MAC)
1210 && (mac->flags & ZEBRA_MAC_REMOTE))
1211 return true;
1212 else if ((wctx->flags & DEL_REMOTE_MAC_FROM_VTEP)
1213 && (mac->flags & ZEBRA_MAC_REMOTE)
1214 && IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &wctx->r_vtep_ip))
1215 return true;
1216 else if ((wctx->flags & DEL_LOCAL_MAC) && (mac->flags & ZEBRA_MAC_AUTO)
1217 && !listcount(mac->neigh_list)) {
1218 if (IS_ZEBRA_DEBUG_VXLAN) {
b16e8004 1219 char mac_buf[MAC_BUF_SIZE];
24268cd0 1220
24268cd0 1221 zlog_debug(
ef7b8be4
DL
1222 "%s: Del MAC %pEA flags %s", __func__,
1223 &mac->macaddr,
b16e8004
DS
1224 zebra_evpn_zebra_mac_flag_dump(
1225 mac, mac_buf, sizeof(mac_buf)));
b2998086
PR
1226 }
1227 wctx->uninstall = 0;
24268cd0 1228
b2998086
PR
1229 return true;
1230 }
24268cd0 1231
b2998086
PR
1232 return false;
1233}
24268cd0 1234
b2998086
PR
1235/*
1236 * Free MAC hash entry (callback)
1237 */
1238static void zebra_evpn_mac_del_hash_entry(struct hash_bucket *bucket, void *arg)
1239{
1240 struct mac_walk_ctx *wctx = arg;
3198b2b3 1241 struct zebra_mac *mac = bucket->data;
24268cd0 1242
b2998086
PR
1243 if (zebra_evpn_check_mac_del_from_db(wctx, mac)) {
1244 if (wctx->upd_client && (mac->flags & ZEBRA_MAC_LOCAL)) {
1245 zebra_evpn_mac_send_del_to_client(wctx->zevpn->vni,
1246 &mac->macaddr,
1247 mac->flags, false);
1248 }
1249 if (wctx->uninstall) {
1250 if (zebra_evpn_mac_is_static(mac))
1251 zebra_evpn_sync_mac_dp_install(
1252 mac, false /* set_inactive */,
1253 true /* force_clear_static */,
1254 __func__);
24268cd0 1255
b2998086 1256 if (mac->flags & ZEBRA_MAC_REMOTE)
f3722826
AK
1257 zebra_evpn_rem_mac_uninstall(wctx->zevpn, mac,
1258 false /*force*/);
24268cd0 1259 }
b2998086
PR
1260
1261 zebra_evpn_mac_del(wctx->zevpn, mac);
24268cd0
PR
1262 }
1263
24268cd0
PR
1264 return;
1265}
1266
24268cd0 1267/*
b2998086 1268 * Delete all MAC entries for this EVPN.
24268cd0 1269 */
f6371c34
DS
1270void zebra_evpn_mac_del_all(struct zebra_evpn *zevpn, int uninstall,
1271 int upd_client, uint32_t flags)
24268cd0 1272{
b2998086 1273 struct mac_walk_ctx wctx;
24268cd0 1274
b2998086 1275 if (!zevpn->mac_table)
24268cd0
PR
1276 return;
1277
6006b807 1278 memset(&wctx, 0, sizeof(wctx));
b2998086
PR
1279 wctx.zevpn = zevpn;
1280 wctx.uninstall = uninstall;
1281 wctx.upd_client = upd_client;
1282 wctx.flags = flags;
24268cd0 1283
b2998086
PR
1284 hash_iterate(zevpn->mac_table, zebra_evpn_mac_del_hash_entry, &wctx);
1285}
24268cd0 1286
b2998086
PR
1287/*
1288 * Look up MAC hash entry.
1289 */
3198b2b3
DS
1290struct zebra_mac *zebra_evpn_mac_lookup(struct zebra_evpn *zevpn,
1291 const struct ethaddr *mac)
b2998086 1292{
3198b2b3
DS
1293 struct zebra_mac tmp;
1294 struct zebra_mac *pmac;
24268cd0 1295
b2998086
PR
1296 memset(&tmp, 0, sizeof(tmp));
1297 memcpy(&tmp.macaddr, mac, ETH_ALEN);
1298 pmac = hash_lookup(zevpn->mac_table, &tmp);
24268cd0 1299
b2998086
PR
1300 return pmac;
1301}
24268cd0 1302
b2998086
PR
1303/*
1304 * Inform BGP about local MAC addition.
1305 */
1a3bd37f 1306int zebra_evpn_mac_send_add_to_client(vni_t vni, const struct ethaddr *macaddr,
b2998086
PR
1307 uint32_t mac_flags, uint32_t seq,
1308 struct zebra_evpn_es *es)
1309{
1310 uint8_t flags = 0;
24268cd0 1311
b2998086
PR
1312 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_LOCAL_INACTIVE)) {
1313 /* host reachability has not been verified locally */
24268cd0 1314
b2998086
PR
1315 /* if no ES peer is claiming reachability we can't advertise the
1316 * entry
1317 */
1318 if (!CHECK_FLAG(mac_flags, ZEBRA_MAC_ES_PEER_ACTIVE))
1319 return 0;
24268cd0 1320
b2998086
PR
1321 /* ES peers are claiming reachability; we will
1322 * advertise the entry but with a proxy flag
24268cd0 1323 */
b2998086
PR
1324 SET_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT);
1325 }
24268cd0 1326
b2998086
PR
1327 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_STICKY))
1328 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
1329 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_DEF_GW))
1330 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
24268cd0 1331
b2998086
PR
1332 return zebra_evpn_macip_send_msg_to_client(vni, macaddr, NULL, flags,
1333 seq, ZEBRA_NEIGH_ACTIVE, es,
1334 ZEBRA_MACIP_ADD);
1335}
24268cd0 1336
b2998086
PR
1337/*
1338 * Inform BGP about local MAC deletion.
1339 */
1a3bd37f 1340int zebra_evpn_mac_send_del_to_client(vni_t vni, const struct ethaddr *macaddr,
b2998086
PR
1341 uint32_t flags, bool force)
1342{
ad7685de
CS
1343 int state = ZEBRA_NEIGH_ACTIVE;
1344
b2998086
PR
1345 if (!force) {
1346 if (CHECK_FLAG(flags, ZEBRA_MAC_LOCAL_INACTIVE)
1347 && !CHECK_FLAG(flags, ZEBRA_MAC_ES_PEER_ACTIVE))
1348 /* the host was not advertised - nothing to delete */
1349 return 0;
ad7685de
CS
1350
1351 /* MAC is LOCAL and DUP_DETECTED, this local mobility event
1352 * is not known to bgpd. Upon receiving local delete
1353 * ask bgp to reinstall the best route (remote entry).
1354 */
1355 if (CHECK_FLAG(flags, ZEBRA_MAC_LOCAL) &&
1356 CHECK_FLAG(flags, ZEBRA_MAC_DUPLICATE))
1357 state = ZEBRA_NEIGH_INACTIVE;
24268cd0
PR
1358 }
1359
b2998086 1360 return zebra_evpn_macip_send_msg_to_client(
ad7685de
CS
1361 vni, macaddr, NULL, 0 /* flags */, 0 /* seq */, state, NULL,
1362 ZEBRA_MACIP_DEL);
24268cd0
PR
1363}
1364
1365/*
b2998086 1366 * wrapper to create a MAC hash table
24268cd0 1367 */
b2998086 1368struct hash *zebra_mac_db_create(const char *desc)
24268cd0 1369{
da55bcbc 1370 return hash_create_size(8, mac_hash_keymake, mac_cmp, desc);
24268cd0
PR
1371}
1372
b2998086 1373/* program sync mac flags in the dataplane */
3198b2b3 1374int zebra_evpn_sync_mac_dp_install(struct zebra_mac *mac, bool set_inactive,
15400f95 1375 bool force_clear_static, const char *caller)
24268cd0 1376{
b2998086
PR
1377 struct interface *ifp;
1378 bool sticky;
1379 bool set_static;
f6371c34 1380 struct zebra_evpn *zevpn = mac->zevpn;
b2998086
PR
1381 vlanid_t vid;
1382 struct zebra_if *zif;
1383 struct interface *br_ifp;
24268cd0 1384
09de6e45
AK
1385 /* If the ES-EVI doesn't exist defer install. When the ES-EVI is
1386 * created we will attempt to install the mac entry again
1387 */
1388 if (mac->es) {
1389 struct zebra_evpn_es_evi *es_evi;
1390
1391 es_evi = zebra_evpn_es_evi_find(mac->es, mac->zevpn);
1392 if (!es_evi) {
1393 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC)
1394 zlog_debug(
1395 "%s: dp-install sync-mac vni %u mac %pEA es %s 0x%x %sskipped, no es-evi",
1396 caller, zevpn->vni, &mac->macaddr,
1397 mac->es ? mac->es->esi_str : "-",
1398 mac->flags,
1399 set_inactive ? "inactive " : "");
1400 return -1;
1401 }
1402 }
1403
b2998086
PR
1404 /* get the access vlan from the vxlan_device */
1405 zebra_evpn_mac_get_access_info(mac, &ifp, &vid);
24268cd0 1406
b2998086 1407 if (!ifp) {
b16e8004
DS
1408 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1409 char mac_buf[MAC_BUF_SIZE];
1410
b2998086 1411 zlog_debug(
b16e8004 1412 "%s: dp-install sync-mac vni %u mac %pEA es %s %s%sskipped, no access-port",
15400f95 1413 caller, zevpn->vni, &mac->macaddr,
b16e8004
DS
1414 mac->es ? mac->es->esi_str : "-",
1415 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1416 sizeof(mac_buf)),
b2998086 1417 set_inactive ? "inactive " : "");
b16e8004 1418 }
15400f95 1419 return -1;
b2998086 1420 }
24268cd0 1421
b2998086
PR
1422 zif = ifp->info;
1423 br_ifp = zif->brslave_info.br_if;
1424 if (!br_ifp) {
b16e8004
DS
1425 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1426 char mac_buf[MAC_BUF_SIZE];
1427
b2998086 1428 zlog_debug(
b16e8004 1429 "%s: dp-install sync-mac vni %u mac %pEA es %s %s%sskipped, no br",
15400f95 1430 caller, zevpn->vni, &mac->macaddr,
b16e8004
DS
1431 mac->es ? mac->es->esi_str : "-",
1432 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1433 sizeof(mac_buf)),
b2998086 1434 set_inactive ? "inactive " : "");
b16e8004 1435 }
15400f95 1436 return -1;
b2998086 1437 }
24268cd0 1438
b2998086
PR
1439 sticky = !!CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY);
1440 if (force_clear_static)
1441 set_static = false;
1442 else
1443 set_static = zebra_evpn_mac_is_static(mac);
24268cd0 1444
15400f95
AK
1445 /* We can install a local mac that has been synced from the peer
1446 * over the VxLAN-overlay/network-port if fast failover is not
1447 * supported and if the local ES is oper-down.
1448 */
1449 if (mac->es && zebra_evpn_es_local_mac_via_network_port(mac->es)) {
b16e8004
DS
1450 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1451 char mac_buf[MAC_BUF_SIZE];
1452
15400f95 1453 zlog_debug(
b16e8004 1454 "dp-%s sync-nw-mac vni %u mac %pEA es %s %s%s",
15400f95
AK
1455 set_static ? "install" : "uninstall",
1456 zevpn->vni, &mac->macaddr,
b16e8004
DS
1457 mac->es ? mac->es->esi_str : "-",
1458 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1459 sizeof(mac_buf)),
15400f95 1460 set_inactive ? "inactive " : "");
b16e8004 1461 }
15400f95
AK
1462 if (set_static)
1463 /* XXX - old_static needs to be computed more
1464 * accurately
1465 */
1466 zebra_evpn_rem_mac_install(zevpn, mac,
1467 true /* old_static */);
1468 else
1469 zebra_evpn_rem_mac_uninstall(zevpn, mac,
1470 false /* force */);
1471
1472 return 0;
1473 }
1474
b16e8004
DS
1475 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1476 char mac_buf[MAC_BUF_SIZE];
1477
1478 zlog_debug("dp-install sync-mac vni %u mac %pEA es %s %s%s%s",
1479 zevpn->vni, &mac->macaddr,
1480 mac->es ? mac->es->esi_str : "-",
1481 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1482 sizeof(mac_buf)),
1483 set_static ? "static " : "",
1484 set_inactive ? "inactive " : "");
1485 }
24268cd0 1486
b2998086
PR
1487 dplane_local_mac_add(ifp, br_ifp, vid, &mac->macaddr, sticky,
1488 set_static, set_inactive);
15400f95 1489 return 0;
24268cd0
PR
1490}
1491
3198b2b3
DS
1492void zebra_evpn_mac_send_add_del_to_client(struct zebra_mac *mac,
1493 bool old_bgp_ready,
b2998086 1494 bool new_bgp_ready)
24268cd0 1495{
b2998086
PR
1496 if (new_bgp_ready)
1497 zebra_evpn_mac_send_add_to_client(mac->zevpn->vni,
1498 &mac->macaddr, mac->flags,
1499 mac->loc_seq, mac->es);
1500 else if (old_bgp_ready)
1501 zebra_evpn_mac_send_del_to_client(mac->zevpn->vni,
1502 &mac->macaddr, mac->flags,
1503 true /* force */);
24268cd0
PR
1504}
1505
b2998086
PR
1506/* MAC hold timer is used to age out peer-active flag.
1507 *
1508 * During this wait time we expect the dataplane component or an
1509 * external neighmgr daemon to probe existing hosts to independently
1510 * establish their presence on the ES.
1511 */
e6685141 1512static void zebra_evpn_mac_hold_exp_cb(struct event *t)
24268cd0 1513{
3198b2b3 1514 struct zebra_mac *mac;
b2998086
PR
1515 bool old_bgp_ready;
1516 bool new_bgp_ready;
1517 bool old_static;
1518 bool new_static;
24268cd0 1519
e16d030c 1520 mac = EVENT_ARG(t);
b2998086
PR
1521 /* the purpose of the hold timer is to age out the peer-active
1522 * flag
1523 */
1524 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE))
cc9f21da 1525 return;
24268cd0 1526
b2998086
PR
1527 old_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags);
1528 old_static = zebra_evpn_mac_is_static(mac);
1529 UNSET_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE);
1530 new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags);
1531 new_static = zebra_evpn_mac_is_static(mac);
24268cd0 1532
b16e8004
DS
1533 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1534 char mac_buf[MAC_BUF_SIZE];
1535
24268cd0 1536 zlog_debug(
ef7b8be4
DL
1537 "sync-mac vni %u mac %pEA es %s %shold expired",
1538 mac->zevpn->vni, &mac->macaddr,
b16e8004
DS
1539 mac->es ? mac->es->esi_str : "-",
1540 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1541 sizeof(mac_buf)));
1542 }
24268cd0 1543
b2998086
PR
1544 /* re-program the local mac in the dataplane if the mac is no
1545 * longer static
1546 */
1547 if (old_static != new_static)
1548 zebra_evpn_sync_mac_dp_install(mac, false /* set_inactive */,
1549 false /* force_clear_static */,
1550 __func__);
24268cd0 1551
b2998086
PR
1552 /* inform bgp if needed */
1553 if (old_bgp_ready != new_bgp_ready)
1554 zebra_evpn_mac_send_add_del_to_client(mac, old_bgp_ready,
1555 new_bgp_ready);
24268cd0
PR
1556}
1557
3198b2b3 1558static inline void zebra_evpn_mac_start_hold_timer(struct zebra_mac *mac)
24268cd0 1559{
b2998086
PR
1560 if (mac->hold_timer)
1561 return;
24268cd0 1562
b16e8004
DS
1563 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1564 char mac_buf[MAC_BUF_SIZE];
1565
24268cd0 1566 zlog_debug(
ef7b8be4
DL
1567 "sync-mac vni %u mac %pEA es %s %shold started",
1568 mac->zevpn->vni, &mac->macaddr,
b16e8004
DS
1569 mac->es ? mac->es->esi_str : "-",
1570 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1571 sizeof(mac_buf)));
1572 }
907a2395
DS
1573 event_add_timer(zrouter.master, zebra_evpn_mac_hold_exp_cb, mac,
1574 zmh_info->mac_hold_time, &mac->hold_timer);
24268cd0
PR
1575}
1576
3198b2b3 1577void zebra_evpn_mac_stop_hold_timer(struct zebra_mac *mac)
24268cd0 1578{
b2998086
PR
1579 if (!mac->hold_timer)
1580 return;
24268cd0 1581
b16e8004
DS
1582 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1583 char mac_buf[MAC_BUF_SIZE];
1584
b2998086 1585 zlog_debug(
ef7b8be4
DL
1586 "sync-mac vni %u mac %pEA es %s %shold stopped",
1587 mac->zevpn->vni, &mac->macaddr,
b16e8004
DS
1588 mac->es ? mac->es->esi_str : "-",
1589 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1590 sizeof(mac_buf)));
1591 }
1592
e16d030c 1593 EVENT_OFF(mac->hold_timer);
24268cd0
PR
1594}
1595
3198b2b3 1596void zebra_evpn_sync_mac_del(struct zebra_mac *mac)
24268cd0 1597{
b2998086
PR
1598 bool old_static;
1599 bool new_static;
24268cd0 1600
b16e8004
DS
1601 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1602 char mac_buf[MAC_BUF_SIZE];
1603
b2998086 1604 zlog_debug(
ef7b8be4
DL
1605 "sync-mac del vni %u mac %pEA es %s seq %d f %s",
1606 mac->zevpn->vni, &mac->macaddr,
b2998086 1607 mac->es ? mac->es->esi_str : "-", mac->loc_seq,
b16e8004
DS
1608 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1609 sizeof(mac_buf)));
1610 }
1611
b2998086
PR
1612 old_static = zebra_evpn_mac_is_static(mac);
1613 UNSET_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY);
1614 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE))
1615 zebra_evpn_mac_start_hold_timer(mac);
1616 new_static = zebra_evpn_mac_is_static(mac);
24268cd0 1617
b2998086
PR
1618 if (old_static != new_static)
1619 /* program the local mac in the kernel */
1620 zebra_evpn_sync_mac_dp_install(mac, false /* set_inactive */,
1621 false /* force_clear_static */,
1622 __func__);
24268cd0
PR
1623}
1624
f6371c34 1625static inline bool zebra_evpn_mac_is_bgp_seq_ok(struct zebra_evpn *zevpn,
3198b2b3 1626 struct zebra_mac *mac,
852d9f97 1627 uint32_t seq, bool sync)
24268cd0 1628{
7d99ad7f 1629 char mac_buf[MAC_BUF_SIZE];
b2998086 1630 uint32_t tmp_seq;
16de1338 1631 const char *n_type;
1e1398e3 1632 bool is_local = false;
24268cd0 1633
16de1338 1634 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
b2998086 1635 tmp_seq = mac->loc_seq;
16de1338 1636 n_type = "local";
1e1398e3 1637 is_local = true;
16de1338 1638 } else {
b2998086 1639 tmp_seq = mac->rem_seq;
16de1338
AK
1640 n_type = "remote";
1641 }
24268cd0 1642
b2998086 1643 if (seq < tmp_seq) {
1e1398e3
SW
1644
1645 if (is_local && !zebra_evpn_mac_is_ready_for_bgp(mac->flags)) {
1646 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC || IS_ZEBRA_DEBUG_VXLAN)
1647 zlog_debug(
1648 "%s-macip not ready vni %u %s-mac %pEA lower seq %u f 0x%x",
1649 sync ? "sync" : "rem", zevpn->vni,
1650 n_type, &mac->macaddr, tmp_seq,
1651 mac->flags);
1652 return true;
1653 }
1654
b2998086
PR
1655 /* if the mac was never advertised to bgp we must accept
1656 * whatever sequence number bgp sends
b2998086 1657 */
da823882 1658 if (!is_local && zebra_vxlan_get_accept_bgp_seq()) {
7d99ad7f
SW
1659 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC ||
1660 IS_ZEBRA_DEBUG_VXLAN) {
b2998086 1661 zlog_debug(
852d9f97 1662 "%s-macip accept vni %u %s-mac %pEA lower seq %u f %s",
16de1338 1663 sync ? "sync" : "rem", zevpn->vni,
852d9f97 1664 n_type, &mac->macaddr, tmp_seq,
b16e8004
DS
1665 zebra_evpn_zebra_mac_flag_dump(
1666 mac, mac_buf, sizeof(mac_buf)));
1667 }
1668
b2998086
PR
1669 return true;
1670 }
24268cd0 1671
b16e8004 1672 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC || IS_ZEBRA_DEBUG_VXLAN) {
b2998086 1673 zlog_debug(
852d9f97 1674 "%s-macip ignore vni %u %s-mac %pEA as existing has higher seq %u f %s",
16de1338 1675 sync ? "sync" : "rem", zevpn->vni, n_type,
852d9f97 1676 &mac->macaddr, tmp_seq,
b16e8004
DS
1677 zebra_evpn_zebra_mac_flag_dump(
1678 mac, mac_buf, sizeof(mac_buf)));
1679 }
7d99ad7f 1680
b2998086 1681 return false;
24268cd0
PR
1682 }
1683
b2998086 1684 return true;
24268cd0
PR
1685}
1686
852d9f97
SW
1687struct zebra_mac *zebra_evpn_proc_sync_mac_update(struct zebra_evpn *zevpn,
1688 const struct ethaddr *macaddr,
1689 uint16_t ipa_len,
1690 const struct ipaddr *ipaddr,
1691 uint8_t flags, uint32_t seq,
1692 const esi_t *esi)
24268cd0 1693{
3198b2b3 1694 struct zebra_mac *mac;
b2998086
PR
1695 bool inform_bgp = false;
1696 bool inform_dataplane = false;
4a1f91a3 1697 bool mac_inactive = false;
b2998086
PR
1698 bool seq_change = false;
1699 bool es_change = false;
1700 uint32_t tmp_seq;
b2998086
PR
1701 char ipbuf[INET6_ADDRSTRLEN];
1702 bool old_local = false;
1703 bool old_bgp_ready;
1704 bool new_bgp_ready;
852d9f97 1705 bool created = false;
24268cd0 1706
b2998086
PR
1707 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
1708 if (!mac) {
1709 /* if it is a new local path we need to inform both
1710 * the control protocol and the data-plane
1711 */
1712 inform_bgp = true;
1713 inform_dataplane = true;
4a1f91a3 1714 mac_inactive = true;
24268cd0 1715
b2998086
PR
1716 /* create the MAC and associate it with the dest ES */
1717 mac = zebra_evpn_mac_add(zevpn, macaddr);
1718 zebra_evpn_es_mac_ref(mac, esi);
24268cd0 1719
b2998086
PR
1720 /* local mac activated by an ES peer */
1721 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
1722 /* if mac-only route setup peer flags */
1723 if (!ipa_len) {
1724 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
1725 SET_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY);
1726 else
1727 SET_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE);
1728 }
1729 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE);
1730 old_bgp_ready = false;
1731 new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags);
852d9f97 1732 created = true;
b2998086
PR
1733 } else {
1734 uint32_t old_flags;
1735 uint32_t new_flags;
1736 bool old_static;
1737 bool new_static;
1738 bool sticky;
1739 bool remote_gw;
24268cd0 1740
f1dbb1c7
DS
1741 mac->uptime = monotime(NULL);
1742
b2998086
PR
1743 old_flags = mac->flags;
1744 sticky = !!CHECK_FLAG(old_flags, ZEBRA_MAC_STICKY);
1745 remote_gw = !!CHECK_FLAG(old_flags, ZEBRA_MAC_REMOTE_DEF_GW);
1746 if (sticky || remote_gw) {
1747 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
1748 zlog_debug(
ef7b8be4
DL
1749 "Ignore sync-macip vni %u mac %pEA%s%s%s%s",
1750 zevpn->vni, macaddr,
b2998086
PR
1751 ipa_len ? " IP " : "",
1752 ipa_len ? ipaddr2str(ipaddr, ipbuf,
1753 sizeof(ipbuf))
1754 : "",
1755 sticky ? " sticky" : "",
1756 remote_gw ? " remote_gw" : "");
b2998086
PR
1757 return NULL;
1758 }
852d9f97 1759 if (!zebra_evpn_mac_is_bgp_seq_ok(zevpn, mac, seq, true))
b2998086 1760 return NULL;
24268cd0 1761
b2998086
PR
1762 old_local = !!CHECK_FLAG(old_flags, ZEBRA_MAC_LOCAL);
1763 old_static = zebra_evpn_mac_is_static(mac);
24268cd0 1764
b2998086
PR
1765 /* re-build the mac flags */
1766 new_flags = 0;
1767 SET_FLAG(new_flags, ZEBRA_MAC_LOCAL);
1768 /* retain old local activity flag */
852d9f97 1769 if (old_flags & ZEBRA_MAC_LOCAL)
b2998086 1770 new_flags |= (old_flags & ZEBRA_MAC_LOCAL_INACTIVE);
852d9f97 1771 else
b2998086 1772 new_flags |= ZEBRA_MAC_LOCAL_INACTIVE;
852d9f97 1773
b2998086
PR
1774 if (ipa_len) {
1775 /* if mac-ip route do NOT update the peer flags
1776 * i.e. retain only flags as is
1777 */
1778 new_flags |= (old_flags & ZEBRA_MAC_ALL_PEER_FLAGS);
1779 } else {
1780 /* if mac-only route update peer flags */
1781 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT)) {
1782 SET_FLAG(new_flags, ZEBRA_MAC_ES_PEER_PROXY);
1783 /* if the mac was peer-active previously we
1784 * need to keep the flag and start the
1785 * holdtimer on it. the peer-active flag is
1786 * cleared on holdtimer expiry.
1787 */
1788 if (CHECK_FLAG(old_flags,
1789 ZEBRA_MAC_ES_PEER_ACTIVE)) {
1790 SET_FLAG(new_flags,
1791 ZEBRA_MAC_ES_PEER_ACTIVE);
1792 zebra_evpn_mac_start_hold_timer(mac);
1793 }
1794 } else {
1795 SET_FLAG(new_flags, ZEBRA_MAC_ES_PEER_ACTIVE);
1796 /* stop hold timer if a peer has verified
1797 * reachability
1798 */
1799 zebra_evpn_mac_stop_hold_timer(mac);
1800 }
1801 }
1802 mac->rem_seq = 0;
8b07f173 1803 zebra_evpn_mac_clear_fwd_info(mac);
b2998086 1804 mac->flags = new_flags;
24268cd0 1805
b16e8004
DS
1806 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC && (old_flags != new_flags)) {
1807 char mac_buf[MAC_BUF_SIZE], omac_buf[MAC_BUF_SIZE];
3198b2b3 1808 struct zebra_mac omac;
b16e8004
DS
1809
1810 omac.flags = old_flags;
b2998086 1811 zlog_debug(
ef7b8be4
DL
1812 "sync-mac vni %u mac %pEA old_f %snew_f %s",
1813 zevpn->vni, macaddr,
b16e8004
DS
1814 zebra_evpn_zebra_mac_flag_dump(
1815 &omac, omac_buf, sizeof(omac_buf)),
1816 zebra_evpn_zebra_mac_flag_dump(
1817 mac, mac_buf, sizeof(mac_buf)));
1818 }
24268cd0 1819
b2998086
PR
1820 /* update es */
1821 es_change = zebra_evpn_es_mac_ref(mac, esi);
1822 /* if mac dest change - inform both sides */
1823 if (es_change) {
1824 inform_bgp = true;
1825 inform_dataplane = true;
4a1f91a3 1826 mac_inactive = true;
b2998086 1827 }
8b07f173 1828
b2998086
PR
1829 /* if peer-flag is being set notify dataplane that the
1830 * entry must not be expired because of local inactivity
1831 */
1832 new_static = zebra_evpn_mac_is_static(mac);
1833 if (old_static != new_static)
1834 inform_dataplane = true;
24268cd0 1835
b2998086
PR
1836 old_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(old_flags);
1837 new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags);
1838 if (old_bgp_ready != new_bgp_ready)
1839 inform_bgp = true;
1840 }
24268cd0 1841
24268cd0 1842
b2998086
PR
1843 /* update sequence number; if that results in a new local sequence
1844 * inform bgp
1845 */
1846 tmp_seq = MAX(mac->loc_seq, seq);
1847 if (tmp_seq != mac->loc_seq) {
1848 mac->loc_seq = tmp_seq;
1849 seq_change = true;
1850 inform_bgp = true;
1851 }
24268cd0 1852
b16e8004
DS
1853 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
1854 char mac_buf[MAC_BUF_SIZE];
1855
ef7b8be4 1856 zlog_debug("sync-mac %s vni %u mac %pEA es %s seq %d f %s%s%s",
852d9f97 1857 created ? "created" : "updated", zevpn->vni, macaddr,
b2998086 1858 mac->es ? mac->es->esi_str : "-", mac->loc_seq,
b16e8004
DS
1859 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
1860 sizeof(mac_buf)),
1861 inform_bgp ? "inform_bgp" : "",
b2998086 1862 inform_dataplane ? " inform_dp" : "");
b16e8004 1863 }
24268cd0 1864
b2998086
PR
1865 if (inform_bgp)
1866 zebra_evpn_mac_send_add_del_to_client(mac, old_bgp_ready,
1867 new_bgp_ready);
24268cd0 1868
b2998086
PR
1869 /* neighs using the mac may need to be re-sent to
1870 * bgp with updated info
1871 */
1872 if (seq_change || es_change || !old_local)
1873 zebra_evpn_process_neigh_on_local_mac_change(
1874 zevpn, mac, seq_change, es_change);
24268cd0 1875
852d9f97
SW
1876 if (inform_dataplane && !ipa_len) {
1877 /* program the local mac in the kernel. when the ES
1878 * change we need to force the dataplane to reset
1879 * the activity as we are yet to establish activity
1880 * locally
1881 */
4a1f91a3
CS
1882 zebra_evpn_sync_mac_dp_install(
1883 mac, mac_inactive /* set_inactive */,
1884 false /* force_clear_static */, __func__);
b2998086 1885 }
24268cd0 1886
b2998086 1887 return mac;
24268cd0
PR
1888}
1889
07b12758 1890/* update local forwarding info. return true if a dest-ES change
b2998086
PR
1891 * is detected
1892 */
3198b2b3 1893static bool zebra_evpn_local_mac_update_fwd_info(struct zebra_mac *mac,
d9d3455e
PR
1894 struct interface *ifp,
1895 vlanid_t vid)
24268cd0 1896{
b2998086
PR
1897 struct zebra_if *zif = ifp->info;
1898 bool es_change;
47c58929
PG
1899 ns_id_t local_ns_id = NS_DEFAULT;
1900 struct zebra_vrf *zvrf;
00a7710c 1901 struct zebra_evpn_es *es;
47c58929 1902
096f7609 1903 zvrf = ifp->vrf->info;
47c58929
PG
1904 if (zvrf && zvrf->zns)
1905 local_ns_id = zvrf->zns->ns_id;
24268cd0 1906
8b07f173 1907 zebra_evpn_mac_clear_fwd_info(mac);
24268cd0 1908
00a7710c
AK
1909 es = zif->es_info.es;
1910 if (es && (es->flags & ZEBRA_EVPNES_BYPASS))
1911 es = NULL;
1912 es_change = zebra_evpn_es_mac_ref_entry(mac, es);
24268cd0 1913
b2998086
PR
1914 if (!mac->es) {
1915 /* if es is set fwd_info is not-relevant/taped-out */
1916 mac->fwd_info.local.ifindex = ifp->ifindex;
47c58929 1917 mac->fwd_info.local.ns_id = local_ns_id;
b2998086 1918 mac->fwd_info.local.vid = vid;
8b07f173 1919 zebra_evpn_mac_ifp_link(mac, ifp);
24268cd0
PR
1920 }
1921
b2998086 1922 return es_change;
24268cd0
PR
1923}
1924
24268cd0 1925/* Notify Local MACs to the clienti, skips GW MAC */
b2998086
PR
1926static void zebra_evpn_send_mac_hash_entry_to_client(struct hash_bucket *bucket,
1927 void *arg)
24268cd0
PR
1928{
1929 struct mac_walk_ctx *wctx = arg;
3198b2b3 1930 struct zebra_mac *zmac = bucket->data;
24268cd0
PR
1931
1932 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_DEF_GW))
1933 return;
1934
1935 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL))
b2998086
PR
1936 zebra_evpn_mac_send_add_to_client(wctx->zevpn->vni,
1937 &zmac->macaddr, zmac->flags,
1938 zmac->loc_seq, zmac->es);
24268cd0
PR
1939}
1940
1941/* Iterator to Notify Local MACs of a EVPN */
f6371c34 1942void zebra_evpn_send_mac_list_to_client(struct zebra_evpn *zevpn)
24268cd0
PR
1943{
1944 struct mac_walk_ctx wctx;
1945
1946 if (!zevpn->mac_table)
1947 return;
1948
6006b807 1949 memset(&wctx, 0, sizeof(wctx));
24268cd0
PR
1950 wctx.zevpn = zevpn;
1951
b2998086
PR
1952 hash_iterate(zevpn->mac_table, zebra_evpn_send_mac_hash_entry_to_client,
1953 &wctx);
24268cd0
PR
1954}
1955
3198b2b3 1956void zebra_evpn_rem_mac_del(struct zebra_evpn *zevpn, struct zebra_mac *mac)
24268cd0 1957{
b2998086
PR
1958 zebra_evpn_process_neigh_on_remote_mac_del(zevpn, mac);
1959 /* the remote sequence number in the auto mac entry
1960 * needs to be reset to 0 as the mac entry may have
1961 * been removed on all VTEPs (including
1962 * the originating one)
1963 */
1964 mac->rem_seq = 0;
24268cd0 1965
b2998086
PR
1966 /* If all remote neighbors referencing a remote MAC
1967 * go away, we need to uninstall the MAC.
1968 */
1969 if (remote_neigh_count(mac) == 0) {
f3722826 1970 zebra_evpn_rem_mac_uninstall(zevpn, mac, false /*force*/);
b2998086
PR
1971 zebra_evpn_es_mac_deref_entry(mac);
1972 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
24268cd0
PR
1973 }
1974
b2998086
PR
1975 if (list_isempty(mac->neigh_list))
1976 zebra_evpn_mac_del(zevpn, mac);
1977 else
1978 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
24268cd0
PR
1979}
1980
b2998086
PR
1981/* Print Duplicate MAC */
1982void zebra_evpn_print_dad_mac_hash(struct hash_bucket *bucket, void *ctxt)
24268cd0 1983{
3198b2b3 1984 struct zebra_mac *mac;
24268cd0 1985
3198b2b3 1986 mac = (struct zebra_mac *)bucket->data;
b2998086
PR
1987 if (!mac)
1988 return;
24268cd0 1989
b2998086
PR
1990 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1991 zebra_evpn_print_mac_hash(bucket, ctxt);
24268cd0
PR
1992}
1993
b2998086
PR
1994/* Print Duplicate MAC in detail */
1995void zebra_evpn_print_dad_mac_hash_detail(struct hash_bucket *bucket,
1996 void *ctxt)
24268cd0 1997{
3198b2b3 1998 struct zebra_mac *mac;
24268cd0 1999
3198b2b3 2000 mac = (struct zebra_mac *)bucket->data;
b2998086
PR
2001 if (!mac)
2002 return;
24268cd0 2003
b2998086
PR
2004 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
2005 zebra_evpn_print_mac_hash_detail(bucket, ctxt);
24268cd0 2006}
19fdd1be 2007
852d9f97
SW
2008int zebra_evpn_mac_remote_macip_add(struct zebra_evpn *zevpn,
2009 struct zebra_vrf *zvrf,
2010 const struct ethaddr *macaddr,
2011 struct in_addr vtep_ip, uint8_t flags,
2012 uint32_t seq, const esi_t *esi)
19fdd1be 2013{
19fdd1be
PR
2014 bool sticky;
2015 bool remote_gw;
2016 int update_mac = 0;
2017 bool do_dad = false;
2018 bool is_dup_detect = false;
2019 esi_t *old_esi;
2020 bool old_static = false;
3198b2b3 2021 struct zebra_mac *mac;
8e1337c5
AK
2022 bool old_es_present;
2023 bool new_es_present;
19fdd1be
PR
2024
2025 sticky = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
2026 remote_gw = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
2027
2028 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
2029
2030 /* Ignore if the mac is already present as a gateway mac */
2031 if (mac && CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW)
2032 && CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) {
2033 if (IS_ZEBRA_DEBUG_VXLAN)
2034 zlog_debug(
852d9f97
SW
2035 "Ignore remote MACIP ADD VNI %u MAC %pEA as MAC is already configured as gateway MAC",
2036 zevpn->vni, macaddr);
19fdd1be
PR
2037 return -1;
2038 }
2039
2040 old_esi = (mac && mac->es) ? &mac->es->esi : zero_esi;
2041
2042 /* check if the remote MAC is unknown or has a change.
2043 * If so, that needs to be updated first. Note that client could
2044 * install MAC and MACIP separately or just install the latter.
2045 */
2046 if (!mac || !CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
2047 || sticky != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)
2048 || remote_gw != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW)
2049 || !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &vtep_ip)
2050 || memcmp(old_esi, esi, sizeof(esi_t)) || seq != mac->rem_seq)
2051 update_mac = 1;
2052
2053 if (update_mac) {
2054 if (!mac) {
2055 mac = zebra_evpn_mac_add(zevpn, macaddr);
19fdd1be 2056 zebra_evpn_es_mac_ref(mac, esi);
19fdd1be 2057 } else {
19fdd1be
PR
2058 /* When host moves but changes its (MAC,IP)
2059 * binding, BGP may install a MACIP entry that
2060 * corresponds to "older" location of the host
2061 * in transient situations (because {IP1,M1}
2062 * is a different route from {IP1,M2}). Check
2063 * the sequence number and ignore this update
2064 * if appropriate.
2065 */
852d9f97
SW
2066 if (!zebra_evpn_mac_is_bgp_seq_ok(zevpn, mac, seq,
2067 false))
19fdd1be 2068 return -1;
c1735c08 2069
8e1337c5 2070 old_es_present = !!mac->es;
c1735c08 2071 zebra_evpn_es_mac_ref(mac, esi);
8e1337c5
AK
2072 new_es_present = !!mac->es;
2073 /* XXX - dataplane is curently not able to handle a MAC
2074 * replace if the destination changes from L2-NHG to
2075 * single VTEP and vice-versa. So delete the old entry
2076 * and re-install
2077 */
2078 if (old_es_present != new_es_present)
2079 zebra_evpn_rem_mac_uninstall(zevpn, mac, false);
19fdd1be
PR
2080 }
2081
2082 /* Check MAC's curent state is local (this is the case
2083 * where MAC has moved from L->R) and check previous
2084 * detection started via local learning.
2085 * RFC-7432: A PE/VTEP that detects a MAC mobility
2086 * event via local learning starts an M-second timer.
2087 *
2088 * VTEP-IP or seq. change alone is not considered
2089 * for dup. detection.
2090 *
2091 * MAC is already marked duplicate set dad, then
2092 * is_dup_detect will be set to not install the entry.
2093 */
2094 if ((!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
2095 && mac->dad_count)
2096 || CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
2097 do_dad = true;
2098
2099 /* Remove local MAC from BGP. */
2100 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
2101 /* force drop the sync flags */
2102 old_static = zebra_evpn_mac_is_static(mac);
b16e8004
DS
2103 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
2104 char mac_buf[MAC_BUF_SIZE];
2105
19fdd1be 2106 zlog_debug(
ef7b8be4
DL
2107 "sync-mac->remote vni %u mac %pEA es %s seq %d f %s",
2108 zevpn->vni, macaddr,
19fdd1be 2109 mac->es ? mac->es->esi_str : "-",
b16e8004
DS
2110 mac->loc_seq,
2111 zebra_evpn_zebra_mac_flag_dump(
2112 mac, mac_buf, sizeof(mac_buf)));
2113 }
2114
19fdd1be
PR
2115 zebra_evpn_mac_clear_sync_info(mac);
2116 zebra_evpn_mac_send_del_to_client(zevpn->vni, macaddr,
2117 mac->flags,
2118 false /* force */);
2119 }
2120
2121 /* Set "auto" and "remote" forwarding info. */
8b07f173 2122 zebra_evpn_mac_clear_fwd_info(mac);
19fdd1be 2123 UNSET_FLAG(mac->flags, ZEBRA_MAC_ALL_LOCAL_FLAGS);
19fdd1be
PR
2124 SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
2125 mac->fwd_info.r_vtep_ip = vtep_ip;
2126
2127 if (sticky)
2128 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
2129 else
2130 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
2131
2132 if (remote_gw)
2133 SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW);
2134 else
2135 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW);
2136
2137 zebra_evpn_dup_addr_detect_for_mac(
2138 zvrf, mac, mac->fwd_info.r_vtep_ip, do_dad,
2139 &is_dup_detect, false);
2140
2141 if (!is_dup_detect) {
2142 zebra_evpn_process_neigh_on_remote_mac_add(zevpn, mac);
2143 /* Install the entry. */
2144 zebra_evpn_rem_mac_install(zevpn, mac, old_static);
2145 }
2146 }
2147
2148 /* Update seq number. */
2149 mac->rem_seq = seq;
2150
852d9f97 2151 UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
19fdd1be
PR
2152 return 0;
2153}
d9d3455e 2154
f6371c34
DS
2155int zebra_evpn_add_update_local_mac(struct zebra_vrf *zvrf,
2156 struct zebra_evpn *zevpn,
d9d3455e 2157 struct interface *ifp,
1a3bd37f 2158 const struct ethaddr *macaddr, vlanid_t vid,
d9d3455e 2159 bool sticky, bool local_inactive,
3198b2b3 2160 bool dp_static, struct zebra_mac *mac)
d9d3455e 2161{
d9d3455e
PR
2162 bool mac_sticky = false;
2163 bool inform_client = false;
2164 bool upd_neigh = false;
2165 bool is_dup_detect = false;
2166 struct in_addr vtep_ip = {.s_addr = 0};
2167 bool es_change = false;
2168 bool new_bgp_ready;
2169 /* assume inactive if not present or if not local */
2170 bool old_local_inactive = true;
2171 bool old_bgp_ready = false;
2172 bool inform_dataplane = false;
2173 bool new_static = false;
2174
2bdd4461 2175 assert(ifp);
d9d3455e 2176 /* Check if we need to create or update or it is a NO-OP. */
00a7710c
AK
2177 if (!mac)
2178 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
d9d3455e
PR
2179 if (!mac) {
2180 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC)
2181 zlog_debug(
ef7b8be4
DL
2182 "ADD %sMAC %pEA intf %s(%u) VID %u -> VNI %u%s",
2183 sticky ? "sticky " : "", macaddr,
d9d3455e
PR
2184 ifp->name, ifp->ifindex, vid, zevpn->vni,
2185 local_inactive ? " local-inactive" : "");
2186
2187 mac = zebra_evpn_mac_add(zevpn, macaddr);
d9d3455e
PR
2188 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
2189 es_change = zebra_evpn_local_mac_update_fwd_info(mac, ifp, vid);
2190 if (sticky)
2191 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
2192 inform_client = true;
2193 } else {
b16e8004
DS
2194 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
2195 char mac_buf[MAC_BUF_SIZE];
2196
d9d3455e 2197 zlog_debug(
ef7b8be4
DL
2198 "UPD %sMAC %pEA intf %s(%u) VID %u -> VNI %u %scurFlags %s",
2199 sticky ? "sticky " : "", macaddr,
d9d3455e
PR
2200 ifp->name, ifp->ifindex, vid, zevpn->vni,
2201 local_inactive ? "local-inactive " : "",
b16e8004
DS
2202 zebra_evpn_zebra_mac_flag_dump(
2203 mac, mac_buf, sizeof(mac_buf)));
2204 }
d9d3455e
PR
2205
2206 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
2207 struct interface *old_ifp;
2208 vlanid_t old_vid;
2209 bool old_static;
2210
2211 zebra_evpn_mac_get_access_info(mac, &old_ifp, &old_vid);
2212 old_bgp_ready =
2213 zebra_evpn_mac_is_ready_for_bgp(mac->flags);
2214 old_local_inactive =
2215 !!(mac->flags & ZEBRA_MAC_LOCAL_INACTIVE);
2216 old_static = zebra_evpn_mac_is_static(mac);
2217 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
2218 mac_sticky = true;
00a7710c
AK
2219 es_change = zebra_evpn_local_mac_update_fwd_info(
2220 mac, ifp, vid);
d9d3455e
PR
2221
2222 /*
2223 * Update any changes and if changes are relevant to
2224 * BGP, note it.
2225 */
2226 if (mac_sticky == sticky && old_ifp == ifp
2227 && old_vid == vid
2228 && old_local_inactive == local_inactive
00a7710c 2229 && dp_static == old_static && !es_change) {
d9d3455e
PR
2230 if (IS_ZEBRA_DEBUG_VXLAN)
2231 zlog_debug(
ef7b8be4 2232 " Add/Update %sMAC %pEA intf %s(%u) VID %u -> VNI %u%s, "
d9d3455e 2233 "entry exists and has not changed ",
4e9a9863 2234 sticky ? "sticky " : "",
ef7b8be4
DL
2235 macaddr, ifp->name,
2236 ifp->ifindex, vid, zevpn->vni,
d9d3455e
PR
2237 local_inactive
2238 ? " local_inactive"
2239 : "");
2240 return 0;
2241 }
2242 if (mac_sticky != sticky) {
2243 if (sticky)
2244 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
2245 else
2246 UNSET_FLAG(mac->flags,
2247 ZEBRA_MAC_STICKY);
2248 inform_client = true;
2249 }
2250
d9d3455e
PR
2251 /* If an es_change is detected we need to advertise
2252 * the route with a sequence that is one
2253 * greater. This is need to indicate a mac-move
2254 * to the ES peers
2255 */
2256 if (es_change) {
00a7710c
AK
2257 /* update the sequence number only if the entry
2258 * is locally active
2259 */
2260 if (!local_inactive)
2261 mac->loc_seq = mac->loc_seq + 1;
d9d3455e
PR
2262 /* force drop the peer/sync info as it is
2263 * simply no longer relevant
2264 */
2265 if (CHECK_FLAG(mac->flags,
2266 ZEBRA_MAC_ALL_PEER_FLAGS)) {
2267 zebra_evpn_mac_clear_sync_info(mac);
2268 new_static =
2269 zebra_evpn_mac_is_static(mac);
2270 /* if we clear peer-flags we
2271 * also need to notify the dataplane
2272 * to drop the static flag
2273 */
2274 if (old_static != new_static)
2275 inform_dataplane = true;
2276 }
2277 }
2278 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
2279 || CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
2280 bool do_dad = false;
2281
2282 /*
2283 * MAC has either moved or was "internally" created due
2284 * to a neighbor learn and is now actually learnt. If
2285 * it was learnt as a remote sticky MAC, this is an
2286 * operator error.
2287 */
2288 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)) {
2289 flog_warn(
2290 EC_ZEBRA_STICKY_MAC_ALREADY_LEARNT,
ef7b8be4
DL
2291 "MAC %pEA already learnt as remote sticky MAC behind VTEP %pI4 VNI %u",
2292 macaddr,
9bcef951 2293 &mac->fwd_info.r_vtep_ip,
d9d3455e
PR
2294 zevpn->vni);
2295 return 0;
2296 }
2297
2298 /* If an actual move, compute MAC's seq number */
2299 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
2300 mac->loc_seq =
2301 MAX(mac->rem_seq + 1, mac->loc_seq);
2302 vtep_ip = mac->fwd_info.r_vtep_ip;
2303 /* Trigger DAD for remote MAC */
2304 do_dad = true;
2305 }
2306
2307 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
2308 UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
2309 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
2310 es_change = zebra_evpn_local_mac_update_fwd_info(
2311 mac, ifp, vid);
2312 if (sticky)
2313 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
2314 else
2315 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
2316 /*
2317 * We have to inform BGP of this MAC as well as process
2318 * all neighbors.
2319 */
2320 inform_client = true;
2321 upd_neigh = true;
2322
2323 zebra_evpn_dup_addr_detect_for_mac(
2324 zvrf, mac, vtep_ip, do_dad, &is_dup_detect,
2325 true);
2326 if (is_dup_detect) {
2327 inform_client = false;
2328 upd_neigh = false;
839dfe29 2329 es_change = false;
d9d3455e
PR
2330 }
2331 }
2332 }
2333
2334 /* if the dataplane thinks the entry is sync but it is
69711b3f
AK
2335 * not sync in zebra (or vice-versa) we need to re-install
2336 * to fixup
d9d3455e 2337 */
69711b3f
AK
2338 new_static = zebra_evpn_mac_is_static(mac);
2339 if (dp_static != new_static)
2340 inform_dataplane = true;
d9d3455e
PR
2341
2342 if (local_inactive)
2343 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE);
2344 else
2345 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE);
2346
2347 new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags);
2348 /* if local-activity has changed we need update bgp
2349 * even if bgp already knows about the mac
2350 */
2351 if ((old_local_inactive != local_inactive)
2352 || (new_bgp_ready != old_bgp_ready)) {
b16e8004
DS
2353 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
2354 char mac_buf[MAC_BUF_SIZE];
2355
d9d3455e 2356 zlog_debug(
ef7b8be4
DL
2357 "local mac vni %u mac %pEA es %s seq %d f %s%s",
2358 zevpn->vni, macaddr,
d9d3455e 2359 mac->es ? mac->es->esi_str : "", mac->loc_seq,
b16e8004
DS
2360 zebra_evpn_zebra_mac_flag_dump(mac, mac_buf,
2361 sizeof(mac_buf)),
2362 local_inactive ? "local-inactive" : "");
2363 }
2364
839dfe29
CS
2365 if (!is_dup_detect)
2366 inform_client = true;
d9d3455e
PR
2367 }
2368
2369 if (es_change) {
2370 inform_client = true;
2371 upd_neigh = true;
2372 }
2373
2374 /* Inform dataplane if required. */
2375 if (inform_dataplane)
2376 zebra_evpn_sync_mac_dp_install(mac, false /* set_inactive */,
2377 false /* force_clear_static */,
2378 __func__);
2379
2380 /* Inform BGP if required. */
2381 if (inform_client)
2382 zebra_evpn_mac_send_add_del_to_client(mac, old_bgp_ready,
2383 new_bgp_ready);
2384
2385 /* Process all neighbors associated with this MAC, if required. */
2386 if (upd_neigh)
2387 zebra_evpn_process_neigh_on_local_mac_change(zevpn, mac, 0,
2388 es_change);
2389
2390 return 0;
2391}
ad6ca5f4 2392
3198b2b3 2393int zebra_evpn_del_local_mac(struct zebra_evpn *zevpn, struct zebra_mac *mac,
00a7710c 2394 bool clear_static)
ad6ca5f4 2395{
ad6ca5f4
PR
2396 bool old_bgp_ready;
2397 bool new_bgp_ready;
b16e8004 2398
46d6f5a2 2399 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
2400 zlog_debug("DEL MAC %pEA VNI %u seq %u flags 0x%x nbr count %u",
2401 &mac->macaddr, zevpn->vni, mac->loc_seq, mac->flags,
46d6f5a2 2402 listcount(mac->neigh_list));
ad6ca5f4
PR
2403
2404 old_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags);
00a7710c 2405 if (!clear_static && zebra_evpn_mac_is_static(mac)) {
ad6ca5f4
PR
2406 /* this is a synced entry and can only be removed when the
2407 * es-peers stop advertising it.
2408 */
8b07f173 2409 zebra_evpn_mac_clear_fwd_info(mac);
ad6ca5f4 2410
b16e8004
DS
2411 if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) {
2412 char mac_buf[MAC_BUF_SIZE];
2413
ad6ca5f4 2414 zlog_debug(
ef7b8be4
DL
2415 "re-add sync-mac vni %u mac %pEA es %s seq %d f %s",
2416 zevpn->vni, &mac->macaddr,
ad6ca5f4 2417 mac->es ? mac->es->esi_str : "-", mac->loc_seq,
b16e8004
DS
2418 zebra_evpn_zebra_mac_flag_dump(
2419 mac, mac_buf, sizeof(mac_buf)));
2420 }
ad6ca5f4
PR
2421
2422 /* inform-bgp about change in local-activity if any */
2423 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE)) {
2424 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE);
2425 new_bgp_ready =
2426 zebra_evpn_mac_is_ready_for_bgp(mac->flags);
2427 zebra_evpn_mac_send_add_del_to_client(
2428 mac, old_bgp_ready, new_bgp_ready);
2429 }
2430
1a4f9efd
AK
2431 /* re-install the inactive entry in the kernel */
2432 zebra_evpn_sync_mac_dp_install(mac, true /* set_inactive */,
ad6ca5f4
PR
2433 false /* force_clear_static */,
2434 __func__);
2435
2436 return 0;
2437 }
2438
00a7710c
AK
2439 /* flush the peer info */
2440 zebra_evpn_mac_clear_sync_info(mac);
2441
ad6ca5f4
PR
2442 /* Update all the neigh entries associated with this mac */
2443 zebra_evpn_process_neigh_on_local_mac_del(zevpn, mac);
2444
2445 /* Remove MAC from BGP. */
46d6f5a2 2446 zebra_evpn_mac_send_del_to_client(zevpn->vni, &mac->macaddr, mac->flags,
89844a96 2447 clear_static /* force */);
ad6ca5f4
PR
2448
2449 zebra_evpn_es_mac_deref_entry(mac);
2450
8b07f173
AK
2451 /* remove links to the destination access port */
2452 zebra_evpn_mac_clear_fwd_info(mac);
2453
ad6ca5f4
PR
2454 /*
2455 * If there are no neigh associated with the mac delete the mac
2456 * else mark it as AUTO for forward reference
2457 */
2458 if (!listcount(mac->neigh_list)) {
2459 zebra_evpn_mac_del(zevpn, mac);
2460 } else {
2461 UNSET_FLAG(mac->flags, ZEBRA_MAC_ALL_LOCAL_FLAGS);
2462 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
2463 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
2464 }
2465
2466 return 0;
2467}
7bce3535 2468
5ff58d0a 2469void zebra_evpn_mac_gw_macip_add(struct interface *ifp,
2470 struct zebra_evpn *zevpn,
2471 const struct ipaddr *ip,
2472 struct zebra_mac **macp,
2473 const struct ethaddr *macaddr,
2474 vlanid_t vlan_id, bool def_gw)
7bce3535 2475{
3198b2b3 2476 struct zebra_mac *mac;
47c58929
PG
2477 ns_id_t local_ns_id = NS_DEFAULT;
2478 struct zebra_vrf *zvrf;
2479
096f7609 2480 zvrf = ifp->vrf->info;
47c58929
PG
2481 if (zvrf && zvrf->zns)
2482 local_ns_id = zvrf->zns->ns_id;
7bce3535 2483
b83d220a 2484 if (!*macp) {
2485 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
2486 if (!mac)
2487 mac = zebra_evpn_mac_add(zevpn, macaddr);
2488 *macp = mac;
2489 } else
2490 mac = *macp;
7bce3535
PR
2491
2492 /* Set "local" forwarding info. */
8b07f173 2493 zebra_evpn_mac_clear_fwd_info(mac);
7bce3535
PR
2494 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
2495 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
e4c3ece6
AK
2496 if (def_gw)
2497 SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW);
5ff58d0a 2498 else
2499 SET_FLAG(mac->flags, ZEBRA_MAC_SVI);
7bce3535 2500 mac->fwd_info.local.ifindex = ifp->ifindex;
47c58929 2501 mac->fwd_info.local.ns_id = local_ns_id;
7bce3535 2502 mac->fwd_info.local.vid = vlan_id;
7bce3535 2503}
243b74ed 2504
f6371c34 2505void zebra_evpn_mac_svi_del(struct interface *ifp, struct zebra_evpn *zevpn)
243b74ed 2506{
3198b2b3 2507 struct zebra_mac *mac;
243b74ed 2508 struct ethaddr macaddr;
e4c3ece6 2509 bool old_bgp_ready;
243b74ed
AK
2510
2511 if (!zebra_evpn_mh_do_adv_svi_mac())
2512 return;
2513
2514 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
2515 mac = zebra_evpn_mac_lookup(zevpn, &macaddr);
2516 if (mac && CHECK_FLAG(mac->flags, ZEBRA_MAC_SVI)) {
2517 if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
2518 zlog_debug("SVI %s mac free", ifp->name);
2519
e4c3ece6 2520 old_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags);
243b74ed 2521 UNSET_FLAG(mac->flags, ZEBRA_MAC_SVI);
e4c3ece6
AK
2522 zebra_evpn_mac_send_add_del_to_client(mac, old_bgp_ready,
2523 false);
243b74ed
AK
2524 zebra_evpn_deref_ip2mac(mac->zevpn, mac);
2525 }
2526}
2527
f6371c34 2528void zebra_evpn_mac_svi_add(struct interface *ifp, struct zebra_evpn *zevpn)
243b74ed 2529{
3198b2b3 2530 struct zebra_mac *mac = NULL;
243b74ed
AK
2531 struct ethaddr macaddr;
2532 struct zebra_if *zif = ifp->info;
e4c3ece6
AK
2533 bool old_bgp_ready;
2534 bool new_bgp_ready;
243b74ed 2535
e4c3ece6
AK
2536 if (!zebra_evpn_mh_do_adv_svi_mac()
2537 || !zebra_evpn_send_to_client_ok(zevpn))
243b74ed
AK
2538 return;
2539
2540 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
2541
2542 /* dup check */
2543 mac = zebra_evpn_mac_lookup(zevpn, &macaddr);
2544 if (mac && CHECK_FLAG(mac->flags, ZEBRA_MAC_SVI))
2545 return;
2546
2547 /* add/update mac */
2548 if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
2549 zlog_debug("SVI %s mac add", zif->ifp->name);
2550
e4c3ece6
AK
2551 old_bgp_ready = (mac && zebra_evpn_mac_is_ready_for_bgp(mac->flags))
2552 ? true
2553 : false;
2554
e4c3ece6 2555 zebra_evpn_mac_gw_macip_add(ifp, zevpn, NULL, &mac, &macaddr, 0, false);
e4c3ece6
AK
2556
2557 new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags);
2558 zebra_evpn_mac_send_add_del_to_client(mac, old_bgp_ready,
2559 new_bgp_ready);
243b74ed 2560}