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