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