]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_evpn_neigh.c
zebra: kill zebra_memory.h, use MTYPE_STATIC
[mirror_frr.git] / zebra / zebra_evpn_neigh.c
CommitLineData
6336e12b 1/*
7cbae20a 2 * Zebra EVPN Neighbor code
6336e12b
PR
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"
7cbae20a 26#include "interface.h"
6336e12b 27#include "jhash.h"
6336e12b
PR
28#include "memory.h"
29#include "prefix.h"
6336e12b 30#include "vlan.h"
7cbae20a 31#include "json.h"
6336e12b 32
7cbae20a 33#include "zebra/zserv.h"
6336e12b 34#include "zebra/debug.h"
7cbae20a 35#include "zebra/zebra_router.h"
6336e12b 36#include "zebra/rt.h"
7cbae20a 37#include "zebra/zebra_errors.h"
6336e12b 38#include "zebra/zebra_vrf.h"
6336e12b 39#include "zebra/zebra_evpn.h"
6336e12b 40#include "zebra/zebra_evpn_mh.h"
7cbae20a
PR
41#include "zebra/zebra_evpn_neigh.h"
42#include "zebra/zebra_evpn_mac.h"
43
44DEFINE_MTYPE_STATIC(ZEBRA, NEIGH, "EVI Neighbor");
6336e12b 45
7cbae20a
PR
46/*
47 * Make hash key for neighbors.
48 */
49static unsigned int neigh_hash_keymake(const void *p)
6336e12b 50{
7cbae20a
PR
51 const zebra_neigh_t *n = p;
52 const struct ipaddr *ip = &n->ip;
6336e12b 53
7cbae20a
PR
54 if (IS_IPADDR_V4(ip))
55 return jhash_1word(ip->ipaddr_v4.s_addr, 0);
6336e12b 56
7cbae20a
PR
57 return jhash2(ip->ipaddr_v6.s6_addr32,
58 array_size(ip->ipaddr_v6.s6_addr32), 0);
59}
6336e12b 60
7cbae20a
PR
61/*
62 * Compare two neighbor hash structures.
63 */
64static bool neigh_cmp(const void *p1, const void *p2)
65{
66 const zebra_neigh_t *n1 = p1;
67 const zebra_neigh_t *n2 = p2;
6336e12b 68
7cbae20a
PR
69 if (n1 == NULL && n2 == NULL)
70 return true;
6336e12b 71
7cbae20a
PR
72 if (n1 == NULL || n2 == NULL)
73 return false;
6336e12b 74
7cbae20a 75 return (memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr)) == 0);
6336e12b 76}
6336e12b 77
7cbae20a 78int neigh_list_cmp(void *p1, void *p2)
6336e12b 79{
7cbae20a
PR
80 const zebra_neigh_t *n1 = p1;
81 const zebra_neigh_t *n2 = p2;
6336e12b 82
7cbae20a
PR
83 return memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr));
84}
6336e12b 85
7cbae20a
PR
86struct hash *zebra_neigh_db_create(const char *desc)
87{
88 return hash_create(neigh_hash_keymake, neigh_cmp, desc);
6336e12b
PR
89}
90
7cbae20a 91uint32_t num_dup_detected_neighs(zebra_evpn_t *zevpn)
6336e12b
PR
92{
93 unsigned int i;
94 uint32_t num_neighs = 0;
95 struct hash *hash;
96 struct hash_bucket *hb;
97 zebra_neigh_t *nbr;
98
99 hash = zevpn->neigh_table;
100 if (!hash)
101 return num_neighs;
102 for (i = 0; i < hash->size; i++) {
103 for (hb = hash->index[i]; hb; hb = hb->next) {
104 nbr = (zebra_neigh_t *)hb->data;
105 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
106 num_neighs++;
107 }
108 }
109
110 return num_neighs;
111}
112
7cbae20a
PR
113/*
114 * Helper function to determine maximum width of neighbor IP address for
115 * display - just because we're dealing with IPv6 addresses that can
116 * widely vary.
117 */
118void zebra_evpn_find_neigh_addr_width(struct hash_bucket *bucket, void *ctxt)
6336e12b 119{
7cbae20a
PR
120 zebra_neigh_t *n;
121 char buf[INET6_ADDRSTRLEN];
122 struct neigh_walk_ctx *wctx = ctxt;
123 int width;
6336e12b 124
7cbae20a 125 n = (zebra_neigh_t *)bucket->data;
6336e12b 126
7cbae20a
PR
127 ipaddr2str(&n->ip, buf, sizeof(buf));
128 width = strlen(buf);
129 if (width > wctx->addr_width)
130 wctx->addr_width = width;
131}
6336e12b 132
7cbae20a
PR
133/*
134 * Count of remote neighbors referencing this MAC.
135 */
136int remote_neigh_count(zebra_mac_t *zmac)
137{
138 zebra_neigh_t *n = NULL;
139 struct listnode *node = NULL;
140 int count = 0;
141
142 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
143 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
144 count++;
145 }
146
147 return count;
6336e12b
PR
148}
149
7cbae20a
PR
150/*
151 * Install remote neighbor into the kernel.
152 */
153int zebra_evpn_rem_neigh_install(zebra_evpn_t *zevpn, zebra_neigh_t *n,
154 bool was_static)
6336e12b 155{
7cbae20a
PR
156 struct interface *vlan_if;
157 int flags;
158 int ret = 0;
6336e12b 159
7cbae20a
PR
160 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
161 return 0;
6336e12b 162
7cbae20a
PR
163 vlan_if = zevpn_map_to_svi(zevpn);
164 if (!vlan_if)
165 return -1;
6336e12b 166
7cbae20a
PR
167 flags = DPLANE_NTF_EXT_LEARNED;
168 if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
169 flags |= DPLANE_NTF_ROUTER;
170 ZEBRA_NEIGH_SET_ACTIVE(n);
171
172 dplane_rem_neigh_add(vlan_if, &n->ip, &n->emac, flags, was_static);
173
174 return ret;
6336e12b
PR
175}
176
7cbae20a
PR
177/*
178 * Install neighbor hash entry - called upon access VLAN change.
6336e12b 179 */
7cbae20a 180void zebra_evpn_install_neigh_hash(struct hash_bucket *bucket, void *ctxt)
6336e12b 181{
7cbae20a
PR
182 zebra_neigh_t *n;
183 struct neigh_walk_ctx *wctx = ctxt;
6336e12b 184
7cbae20a 185 n = (zebra_neigh_t *)bucket->data;
6336e12b 186
7cbae20a
PR
187 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
188 zebra_evpn_rem_neigh_install(wctx->zevpn, n,
189 false /*was_static*/);
6336e12b
PR
190}
191
7cbae20a
PR
192/*
193 * Callback to allocate neighbor hash entry.
194 */
195static void *zebra_evpn_neigh_alloc(void *p)
6336e12b 196{
7cbae20a
PR
197 const zebra_neigh_t *tmp_n = p;
198 zebra_neigh_t *n;
6336e12b 199
7cbae20a
PR
200 n = XCALLOC(MTYPE_NEIGH, sizeof(zebra_neigh_t));
201 *n = *tmp_n;
6336e12b 202
7cbae20a
PR
203 return ((void *)n);
204}
205
206static void zebra_evpn_local_neigh_ref_mac(zebra_neigh_t *n,
207 struct ethaddr *macaddr,
208 zebra_mac_t *mac,
209 bool send_mac_update)
210{
7cbae20a
PR
211 bool old_static;
212 bool new_static;
213
214 memcpy(&n->emac, macaddr, ETH_ALEN);
215 n->mac = mac;
216
217 /* Link to new MAC */
218 if (!mac)
6336e12b
PR
219 return;
220
7cbae20a
PR
221 listnode_add_sort(mac->neigh_list, n);
222 if (n->flags & ZEBRA_NEIGH_ALL_PEER_FLAGS) {
223 old_static = zebra_evpn_mac_is_static(mac);
224 ++mac->sync_neigh_cnt;
225 new_static = zebra_evpn_mac_is_static(mac);
226 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
6336e12b 227 zlog_debug(
ef7b8be4
DL
228 "sync-neigh ref mac vni %u ip %pIA mac %pEA ref %d",
229 n->zevpn->vni, &n->ip, &n->emac,
7cbae20a
PR
230 mac->sync_neigh_cnt);
231 if ((old_static != new_static) && send_mac_update)
232 /* program the local mac in the kernel */
233 zebra_evpn_sync_mac_dp_install(
234 mac, false /*set_inactive*/,
235 false /*force_clear_static*/, __func__);
236 }
237}
6336e12b 238
7cbae20a 239/* sync-path that is active on an ES peer */
33064a62
PR
240static void zebra_evpn_sync_neigh_dp_install(zebra_neigh_t *n,
241 bool set_inactive,
242 bool force_clear_static,
243 const char *caller)
7cbae20a 244{
7cbae20a
PR
245 struct zebra_ns *zns;
246 struct interface *ifp;
247 bool set_static;
248 bool set_router;
6336e12b 249
7cbae20a
PR
250 zns = zebra_ns_lookup(NS_DEFAULT);
251 ifp = if_lookup_by_index_per_ns(zns, n->ifindex);
252 if (!ifp) {
253 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
254 zlog_debug(
ef7b8be4
DL
255 "%s: dp-install sync-neigh vni %u ip %pIA mac %pEA if %d f 0x%x skipped",
256 caller, n->zevpn->vni, &n->ip, &n->emac,
7cbae20a 257 n->ifindex, n->flags);
6336e12b
PR
258 return;
259 }
260
7cbae20a
PR
261 if (force_clear_static)
262 set_static = false;
263 else
264 set_static = zebra_evpn_neigh_is_static(n);
6336e12b 265
7cbae20a 266 set_router = !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
6336e12b 267
7cbae20a
PR
268 /* XXX - this will change post integration with the new kernel */
269 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE))
270 set_inactive = true;
6336e12b 271
7cbae20a 272 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
6336e12b 273 zlog_debug(
ef7b8be4
DL
274 "%s: dp-install sync-neigh vni %u ip %pIA mac %pEA if %s(%d) f 0x%x%s%s%s",
275 caller, n->zevpn->vni, &n->ip, &n->emac,
7cbae20a
PR
276 ifp->name, n->ifindex, n->flags,
277 set_router ? " router" : "",
278 set_static ? " static" : "",
279 set_inactive ? " inactive" : "");
280 dplane_local_neigh_add(ifp, &n->ip, &n->emac, set_router, set_static,
281 set_inactive);
6336e12b
PR
282}
283
284/*
7cbae20a 285 * Inform BGP about local neighbor addition.
6336e12b 286 */
7cbae20a
PR
287int zebra_evpn_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
288 struct ethaddr *macaddr,
289 zebra_mac_t *zmac, uint32_t neigh_flags,
290 uint32_t seq)
6336e12b 291{
7cbae20a 292 uint8_t flags = 0;
6336e12b 293
7cbae20a
PR
294 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_LOCAL_INACTIVE)) {
295 /* host reachability has not been verified locally */
6336e12b 296
7cbae20a
PR
297 /* if no ES peer is claiming reachability we can't advertise
298 * the entry
299 */
300 if (!CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_ES_PEER_ACTIVE))
301 return 0;
6336e12b 302
7cbae20a
PR
303 /* ES peers are claiming reachability; we will
304 * advertise the entry but with a proxy flag
305 */
306 SET_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT);
6336e12b
PR
307 }
308
7cbae20a
PR
309 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_DEF_GW))
310 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
311 /* Set router flag (R-bit) based on local neigh entry add */
312 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_ROUTER_FLAG))
313 SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
314 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_SVI_IP))
315 SET_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP);
6336e12b 316
00a7710c
AK
317 return zebra_evpn_macip_send_msg_to_client(vni, macaddr, ip, flags, seq,
318 ZEBRA_NEIGH_ACTIVE, zmac->es,
319 ZEBRA_MACIP_ADD);
6336e12b
PR
320}
321
322/*
7cbae20a 323 * Inform BGP about local neighbor deletion.
6336e12b 324 */
7cbae20a
PR
325int zebra_evpn_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
326 struct ethaddr *macaddr, uint32_t flags,
327 int state, bool force)
6336e12b 328{
7cbae20a
PR
329 if (!force) {
330 if (CHECK_FLAG(flags, ZEBRA_NEIGH_LOCAL_INACTIVE)
331 && !CHECK_FLAG(flags, ZEBRA_NEIGH_ES_PEER_ACTIVE))
332 /* the neigh was not advertised - nothing to delete */
333 return 0;
6336e12b
PR
334 }
335
7cbae20a
PR
336 return zebra_evpn_macip_send_msg_to_client(
337 vni, macaddr, ip, flags, 0, state, NULL, ZEBRA_MACIP_DEL);
6336e12b
PR
338}
339
33064a62
PR
340static void zebra_evpn_neigh_send_add_del_to_client(zebra_neigh_t *n,
341 bool old_bgp_ready,
342 bool new_bgp_ready)
6336e12b 343{
7cbae20a
PR
344 if (new_bgp_ready)
345 zebra_evpn_neigh_send_add_to_client(n->zevpn->vni, &n->ip,
346 &n->emac, n->mac, n->flags,
347 n->loc_seq);
348 else if (old_bgp_ready)
349 zebra_evpn_neigh_send_del_to_client(n->zevpn->vni, &n->ip,
350 &n->emac, n->flags,
351 n->state, true /*force*/);
6336e12b
PR
352}
353
7cbae20a
PR
354/* if the static flag associated with the neigh changes we need
355 * to update the sync-neigh references against the MAC
356 * and inform the dataplane about the static flag changes.
6336e12b 357 */
7cbae20a
PR
358void zebra_evpn_sync_neigh_static_chg(zebra_neigh_t *n, bool old_n_static,
359 bool new_n_static, bool defer_n_dp,
360 bool defer_mac_dp, const char *caller)
6336e12b 361{
7cbae20a
PR
362 zebra_mac_t *mac = n->mac;
363 bool old_mac_static;
364 bool new_mac_static;
6336e12b 365
7cbae20a
PR
366 if (old_n_static == new_n_static)
367 return;
6336e12b 368
7cbae20a
PR
369 /* update the neigh sync references in the dataplane. if
370 * the neigh is in the middle of updates the caller can
371 * request for a defer
372 */
373 if (!defer_n_dp)
374 zebra_evpn_sync_neigh_dp_install(n, false /* set_inactive */,
375 false /* force_clear_static */,
376 __func__);
6336e12b 377
7cbae20a
PR
378 if (!mac)
379 return;
6336e12b 380
7cbae20a
PR
381 /* update the mac sync ref cnt */
382 old_mac_static = zebra_evpn_mac_is_static(mac);
383 if (new_n_static) {
384 ++mac->sync_neigh_cnt;
385 } else if (old_n_static) {
386 if (mac->sync_neigh_cnt)
387 --mac->sync_neigh_cnt;
6336e12b 388 }
7cbae20a 389 new_mac_static = zebra_evpn_mac_is_static(mac);
6336e12b 390
7cbae20a
PR
391 /* update the mac sync references in the dataplane */
392 if ((old_mac_static != new_mac_static) && !defer_mac_dp)
393 zebra_evpn_sync_mac_dp_install(mac, false /* set_inactive */,
394 false /* force_clear_static */,
395 __func__);
6336e12b 396
7cbae20a
PR
397 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
398 zlog_debug(
ef7b8be4
DL
399 "sync-neigh ref-chg vni %u ip %pIA mac %pEA f 0x%x %d%s%s%s%s by %s",
400 n->zevpn->vni, &n->ip, &n->emac, n->flags,
401 mac->sync_neigh_cnt,
7cbae20a
PR
402 old_n_static ? " old_n_static" : "",
403 new_n_static ? " new_n_static" : "",
404 old_mac_static ? " old_mac_static" : "",
405 new_mac_static ? " new_mac_static" : "", caller);
6336e12b
PR
406}
407
7cbae20a
PR
408/* Neigh hold timer is used to age out peer-active flag.
409 *
410 * During this wait time we expect the dataplane component or an
411 * external neighmgr daemon to probe existing hosts to independently
412 * establish their presence on the ES.
6336e12b 413 */
7cbae20a 414static int zebra_evpn_neigh_hold_exp_cb(struct thread *t)
6336e12b 415{
7cbae20a
PR
416 zebra_neigh_t *n;
417 bool old_bgp_ready;
418 bool new_bgp_ready;
419 bool old_n_static;
420 bool new_n_static;
7cbae20a
PR
421
422 n = THREAD_ARG(t);
423 /* the purpose of the hold timer is to age out the peer-active
424 * flag
425 */
426 if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE))
427 return 0;
6336e12b 428
7cbae20a
PR
429 old_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
430 old_n_static = zebra_evpn_neigh_is_static(n);
431 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
432 new_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
433 new_n_static = zebra_evpn_neigh_is_static(n);
6336e12b 434
7cbae20a 435 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
ef7b8be4
DL
436 zlog_debug("sync-neigh vni %u ip %pIA mac %pEA 0x%x hold expired",
437 n->zevpn->vni, &n->ip, &n->emac, n->flags);
6336e12b 438
7cbae20a
PR
439 /* re-program the local neigh in the dataplane if the neigh is no
440 * longer static
441 */
442 if (old_n_static != new_n_static)
443 zebra_evpn_sync_neigh_static_chg(
444 n, old_n_static, new_n_static, false /*defer_n_dp*/,
445 false /*defer_mac_dp*/, __func__);
6336e12b 446
7cbae20a
PR
447 /* inform bgp if needed */
448 if (old_bgp_ready != new_bgp_ready)
449 zebra_evpn_neigh_send_add_del_to_client(n, old_bgp_ready,
450 new_bgp_ready);
6336e12b 451
6336e12b
PR
452 return 0;
453}
454
7cbae20a 455static inline void zebra_evpn_neigh_start_hold_timer(zebra_neigh_t *n)
6336e12b 456{
7cbae20a
PR
457 if (n->hold_timer)
458 return;
6336e12b 459
7cbae20a 460 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
ef7b8be4
DL
461 zlog_debug("sync-neigh vni %u ip %pIA mac %pEA 0x%x hold start",
462 n->zevpn->vni, &n->ip, &n->emac, n->flags);
7cbae20a
PR
463 thread_add_timer(zrouter.master, zebra_evpn_neigh_hold_exp_cb, n,
464 zmh_info->neigh_hold_time, &n->hold_timer);
465}
6336e12b 466
7cbae20a
PR
467static void zebra_evpn_local_neigh_deref_mac(zebra_neigh_t *n,
468 bool send_mac_update)
469{
470 zebra_mac_t *mac = n->mac;
471 zebra_evpn_t *zevpn = n->zevpn;
7cbae20a
PR
472 bool old_static;
473 bool new_static;
6336e12b 474
7cbae20a
PR
475 n->mac = NULL;
476 if (!mac)
477 return;
6336e12b 478
7cbae20a
PR
479 if ((n->flags & ZEBRA_NEIGH_ALL_PEER_FLAGS) && mac->sync_neigh_cnt) {
480 old_static = zebra_evpn_mac_is_static(mac);
481 --mac->sync_neigh_cnt;
482 new_static = zebra_evpn_mac_is_static(mac);
483 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
6336e12b 484 zlog_debug(
ef7b8be4
DL
485 "sync-neigh deref mac vni %u ip %pIA mac %pEA ref %d",
486 n->zevpn->vni, &n->ip, &n->emac,
7cbae20a
PR
487 mac->sync_neigh_cnt);
488 if ((old_static != new_static) && send_mac_update)
489 /* program the local mac in the kernel */
490 zebra_evpn_sync_mac_dp_install(
491 mac, false /* set_inactive */,
492 false /* force_clear_static */, __func__);
6336e12b
PR
493 }
494
7cbae20a
PR
495 listnode_delete(mac->neigh_list, n);
496 zebra_evpn_deref_ip2mac(zevpn, mac);
6336e12b
PR
497}
498
7cbae20a 499bool zebra_evpn_neigh_is_bgp_seq_ok(zebra_evpn_t *zevpn, zebra_neigh_t *n,
16de1338
AK
500 struct ethaddr *macaddr, uint32_t seq,
501 bool sync)
6336e12b 502{
7cbae20a 503 uint32_t tmp_seq;
16de1338 504 const char *n_type;
6336e12b 505
16de1338 506 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
7cbae20a 507 tmp_seq = n->loc_seq;
16de1338
AK
508 n_type = "local";
509 } else {
7cbae20a 510 tmp_seq = n->rem_seq;
16de1338
AK
511 n_type = "remote";
512 }
6336e12b 513
7cbae20a
PR
514 if (seq < tmp_seq) {
515 /* if the neigh was never advertised to bgp we must accept
516 * whatever sequence number bgp sends
517 * XXX - check with Vivek
518 */
519 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)
520 && !zebra_evpn_neigh_is_ready_for_bgp(n)) {
16de1338
AK
521 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH
522 || IS_ZEBRA_DEBUG_VXLAN)
7cbae20a 523 zlog_debug(
ef7b8be4 524 "%s-macip accept vni %u %s mac %pEA IP %pIA lower seq %u f 0x%x",
16de1338 525 sync ? "sync" : "remote", zevpn->vni,
ef7b8be4 526 n_type, macaddr, &n->ip,
7cbae20a
PR
527 tmp_seq, n->flags);
528 return true;
529 }
6336e12b 530
16de1338 531 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH || IS_ZEBRA_DEBUG_VXLAN)
7cbae20a 532 zlog_debug(
ef7b8be4 533 "%s-macip ignore vni %u %s mac %pEA IP %pIA as existing has higher seq %u f 0x%x",
16de1338 534 sync ? "sync" : "remote", zevpn->vni, n_type,
ef7b8be4 535 macaddr, &n->ip, tmp_seq, n->flags);
7cbae20a 536 return false;
6336e12b 537 }
7cbae20a
PR
538
539 return true;
6336e12b
PR
540}
541
542/*
7cbae20a 543 * Add neighbor entry.
6336e12b 544 */
32fe7dfd
PR
545static zebra_neigh_t *zebra_evpn_neigh_add(zebra_evpn_t *zevpn,
546 struct ipaddr *ip,
547 struct ethaddr *mac,
548 zebra_mac_t *zmac, uint32_t n_flags)
6336e12b 549{
7cbae20a
PR
550 zebra_neigh_t tmp_n;
551 zebra_neigh_t *n = NULL;
6336e12b 552
7cbae20a
PR
553 memset(&tmp_n, 0, sizeof(zebra_neigh_t));
554 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
555 n = hash_get(zevpn->neigh_table, &tmp_n, zebra_evpn_neigh_alloc);
556 assert(n);
6336e12b 557
7cbae20a
PR
558 n->state = ZEBRA_NEIGH_INACTIVE;
559 n->zevpn = zevpn;
560 n->dad_ip_auto_recovery_timer = NULL;
561 n->flags = n_flags;
a05111ba 562 n->uptime = monotime(NULL);
6336e12b 563
7cbae20a
PR
564 if (!zmac)
565 zmac = zebra_evpn_mac_lookup(zevpn, mac);
566 zebra_evpn_local_neigh_ref_mac(n, mac, zmac,
567 false /* send_mac_update */);
6336e12b 568
7cbae20a 569 return n;
6336e12b
PR
570}
571
572/*
7cbae20a 573 * Delete neighbor entry.
6336e12b 574 */
7cbae20a 575int zebra_evpn_neigh_del(zebra_evpn_t *zevpn, zebra_neigh_t *n)
6336e12b 576{
7cbae20a 577 zebra_neigh_t *tmp_n;
6336e12b 578
7cbae20a
PR
579 if (n->mac)
580 listnode_delete(n->mac->neigh_list, n);
6336e12b 581
7cbae20a
PR
582 /* Cancel auto recovery */
583 THREAD_OFF(n->dad_ip_auto_recovery_timer);
6336e12b 584
2b9e207e
AK
585 /* Cancel proxy hold timer */
586 zebra_evpn_neigh_stop_hold_timer(n);
587
7cbae20a
PR
588 /* Free the VNI hash entry and allocated memory. */
589 tmp_n = hash_release(zevpn->neigh_table, n);
590 XFREE(MTYPE_NEIGH, tmp_n);
6336e12b
PR
591
592 return 0;
593}
594
7cbae20a 595void zebra_evpn_sync_neigh_del(zebra_neigh_t *n)
6336e12b 596{
7cbae20a
PR
597 bool old_n_static;
598 bool new_n_static;
6336e12b 599
7cbae20a 600 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
ef7b8be4
DL
601 zlog_debug("sync-neigh del vni %u ip %pIA mac %pEA f 0x%x",
602 n->zevpn->vni, &n->ip, &n->emac, n->flags);
6336e12b 603
7cbae20a
PR
604 old_n_static = zebra_evpn_neigh_is_static(n);
605 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY);
606 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE))
607 zebra_evpn_neigh_start_hold_timer(n);
608 new_n_static = zebra_evpn_neigh_is_static(n);
6336e12b 609
7cbae20a
PR
610 if (old_n_static != new_n_static)
611 zebra_evpn_sync_neigh_static_chg(
612 n, old_n_static, new_n_static, false /*defer-dp*/,
613 false /*defer_mac_dp*/, __func__);
614}
6336e12b 615
7cbae20a
PR
616zebra_neigh_t *
617zebra_evpn_proc_sync_neigh_update(zebra_evpn_t *zevpn, zebra_neigh_t *n,
618 uint16_t ipa_len, struct ipaddr *ipaddr,
619 uint8_t flags, uint32_t seq, esi_t *esi,
620 struct sync_mac_ip_ctx *ctx)
621{
622 struct interface *ifp = NULL;
623 bool is_router;
624 zebra_mac_t *mac = ctx->mac;
625 uint32_t tmp_seq;
626 bool old_router = false;
627 bool old_bgp_ready = false;
628 bool new_bgp_ready;
629 bool inform_dataplane = false;
630 bool inform_bgp = false;
631 bool old_mac_static;
632 bool new_mac_static;
633 bool set_dp_inactive = false;
7cbae20a
PR
634 bool created;
635 ifindex_t ifindex = 0;
6336e12b 636
7cbae20a
PR
637 /* locate l3-svi */
638 ifp = zevpn_map_to_svi(zevpn);
639 if (ifp)
640 ifindex = ifp->ifindex;
6336e12b 641
7cbae20a
PR
642 is_router = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
643 old_mac_static = zebra_evpn_mac_is_static(mac);
6336e12b 644
7cbae20a
PR
645 if (!n) {
646 uint32_t n_flags = 0;
6336e12b 647
7cbae20a
PR
648 /* New neighbor - create */
649 SET_FLAG(n_flags, ZEBRA_NEIGH_LOCAL);
650 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
651 SET_FLAG(n_flags, ZEBRA_NEIGH_ES_PEER_PROXY);
652 else
653 SET_FLAG(n_flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
654 SET_FLAG(n_flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
6336e12b 655
7cbae20a
PR
656 n = zebra_evpn_neigh_add(zevpn, ipaddr, &mac->macaddr, mac,
657 n_flags);
658 n->ifindex = ifindex;
659 ZEBRA_NEIGH_SET_ACTIVE(n);
6336e12b 660
7cbae20a
PR
661 created = true;
662 inform_dataplane = true;
663 inform_bgp = true;
664 set_dp_inactive = true;
665 } else {
666 bool mac_change;
667 uint32_t old_flags = n->flags;
668 bool old_n_static;
669 bool new_n_static;
6336e12b 670
7cbae20a
PR
671 created = false;
672 old_n_static = zebra_evpn_neigh_is_static(n);
673 old_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
674 old_router = !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
6336e12b 675
7cbae20a
PR
676 mac_change = !!memcmp(&n->emac, &mac->macaddr, ETH_ALEN);
677
678 /* deref and clear old info */
679 if (mac_change) {
680 if (old_bgp_ready) {
681 zebra_evpn_neigh_send_del_to_client(
682 zevpn->vni, &n->ip, &n->emac, n->flags,
683 n->state, false /*force*/);
684 old_bgp_ready = false;
6336e12b 685 }
bc3cd39b
DS
686 zebra_evpn_local_neigh_deref_mac(n,
687 false /*send_mac_update*/);
6336e12b 688 }
7cbae20a
PR
689 /* clear old fwd info */
690 n->rem_seq = 0;
691 n->r_vtep_ip.s_addr = 0;
6336e12b 692
7cbae20a
PR
693 /* setup new flags */
694 n->flags = 0;
695 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
696 /* retain activity flag if the neigh was
697 * previously local
6336e12b 698 */
7cbae20a
PR
699 if (old_flags & ZEBRA_NEIGH_LOCAL) {
700 n->flags |= (old_flags & ZEBRA_NEIGH_LOCAL_INACTIVE);
701 } else {
702 inform_dataplane = true;
703 set_dp_inactive = true;
704 n->flags |= ZEBRA_NEIGH_LOCAL_INACTIVE;
6336e12b
PR
705 }
706
7cbae20a
PR
707 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
708 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY);
709 else
710 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
6336e12b 711
7cbae20a
PR
712 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT)) {
713 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY);
714 /* if the neigh was peer-active previously we
715 * need to keep the flag and start the
716 * holdtimer on it. the peer-active flag is
717 * cleared on holdtimer expiry.
718 */
719 if (CHECK_FLAG(old_flags, ZEBRA_NEIGH_ES_PEER_ACTIVE)) {
720 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
721 zebra_evpn_neigh_start_hold_timer(n);
722 }
723 } else {
724 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
725 /* stop hold timer if a peer has verified
726 * reachability
727 */
728 zebra_evpn_neigh_stop_hold_timer(n);
6336e12b 729 }
7cbae20a 730 ZEBRA_NEIGH_SET_ACTIVE(n);
6336e12b 731
7cbae20a 732 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH && (old_flags != n->flags))
6336e12b 733 zlog_debug(
ef7b8be4
DL
734 "sync-neigh vni %u ip %pIA mac %pEA old_f 0x%x new_f 0x%x",
735 n->zevpn->vni, &n->ip, &n->emac,
7cbae20a 736 old_flags, n->flags);
6336e12b 737
7cbae20a
PR
738 new_n_static = zebra_evpn_neigh_is_static(n);
739 if (mac_change) {
740 set_dp_inactive = true;
741 n->flags |= ZEBRA_NEIGH_LOCAL_INACTIVE;
742 inform_dataplane = true;
743 zebra_evpn_local_neigh_ref_mac(
744 n, &mac->macaddr, mac,
745 false /*send_mac_update*/);
746 } else if (old_n_static != new_n_static) {
747 inform_dataplane = true;
748 /* if static flags have changed without a mac change
749 * we need to create the correct sync-refs against
750 * the existing mac
6336e12b 751 */
7cbae20a
PR
752 zebra_evpn_sync_neigh_static_chg(
753 n, old_n_static, new_n_static,
754 true /*defer_dp*/, true /*defer_mac_dp*/,
755 __func__);
6336e12b 756 }
6336e12b 757
7cbae20a
PR
758 /* Update the forwarding info. */
759 if (n->ifindex != ifindex) {
760 n->ifindex = ifindex;
761 inform_dataplane = true;
6336e12b 762 }
a05111ba
DS
763
764 n->uptime = monotime(NULL);
6336e12b
PR
765 }
766
7cbae20a
PR
767 /* update the neigh seq. we don't bother with the mac seq as
768 * sync_mac_update already took care of that
769 */
770 tmp_seq = MAX(n->loc_seq, seq);
771 if (tmp_seq != n->loc_seq) {
772 n->loc_seq = tmp_seq;
773 inform_bgp = true;
774 }
6336e12b 775
7cbae20a
PR
776 /* Mark Router flag (R-bit) */
777 if (is_router)
778 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
779 else
780 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
6336e12b 781
7cbae20a
PR
782 if (old_router != is_router)
783 inform_dataplane = true;
6336e12b 784
7cbae20a
PR
785 new_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
786 if (old_bgp_ready != new_bgp_ready)
787 inform_bgp = true;
6336e12b 788
7cbae20a
PR
789 new_mac_static = zebra_evpn_mac_is_static(mac);
790 if ((old_mac_static != new_mac_static) || ctx->mac_dp_update_deferred)
791 zebra_evpn_sync_mac_dp_install(mac, ctx->mac_inactive,
792 false /* force_clear_static */,
793 __func__);
6336e12b 794
7cbae20a
PR
795 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
796 zlog_debug(
ef7b8be4 797 "sync-neigh %s vni %u ip %pIA mac %pEA if %s(%d) seq %d f 0x%x%s%s",
7cbae20a 798 created ? "created" : "updated", n->zevpn->vni,
ef7b8be4 799 &n->ip, &n->emac,
7cbae20a
PR
800 ifp ? ifp->name : "", ifindex, n->loc_seq, n->flags,
801 inform_bgp ? " inform_bgp" : "",
802 inform_dataplane ? " inform_dp" : "");
6336e12b 803
7cbae20a
PR
804 if (inform_dataplane)
805 zebra_evpn_sync_neigh_dp_install(n, set_dp_inactive,
806 false /* force_clear_static */,
807 __func__);
6336e12b 808
7cbae20a
PR
809 if (inform_bgp)
810 zebra_evpn_neigh_send_add_del_to_client(n, old_bgp_ready,
811 new_bgp_ready);
6336e12b 812
7cbae20a
PR
813 return n;
814}
6336e12b 815
7cbae20a
PR
816/*
817 * Uninstall remote neighbor from the kernel.
818 */
32fe7dfd 819static int zebra_evpn_neigh_uninstall(zebra_evpn_t *zevpn, zebra_neigh_t *n)
7cbae20a
PR
820{
821 struct interface *vlan_if;
6336e12b 822
7cbae20a
PR
823 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
824 return 0;
6336e12b 825
7cbae20a
PR
826 vlan_if = zevpn_map_to_svi(zevpn);
827 if (!vlan_if)
828 return -1;
6336e12b 829
7cbae20a
PR
830 ZEBRA_NEIGH_SET_INACTIVE(n);
831 n->loc_seq = 0;
6336e12b 832
7cbae20a 833 dplane_rem_neigh_delete(vlan_if, &n->ip);
6336e12b
PR
834
835 return 0;
836}
837
7cbae20a
PR
838/*
839 * Free neighbor hash entry (callback)
840 */
841static void zebra_evpn_neigh_del_hash_entry(struct hash_bucket *bucket,
842 void *arg)
6336e12b 843{
7cbae20a
PR
844 struct neigh_walk_ctx *wctx = arg;
845 zebra_neigh_t *n = bucket->data;
6336e12b 846
7cbae20a
PR
847 if (((wctx->flags & DEL_LOCAL_NEIGH) && (n->flags & ZEBRA_NEIGH_LOCAL))
848 || ((wctx->flags & DEL_REMOTE_NEIGH)
849 && (n->flags & ZEBRA_NEIGH_REMOTE))
850 || ((wctx->flags & DEL_REMOTE_NEIGH_FROM_VTEP)
851 && (n->flags & ZEBRA_NEIGH_REMOTE)
852 && IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))) {
853 if (wctx->upd_client && (n->flags & ZEBRA_NEIGH_LOCAL))
854 zebra_evpn_neigh_send_del_to_client(
855 wctx->zevpn->vni, &n->ip, &n->emac, n->flags,
856 n->state, false /*force*/);
6336e12b 857
7cbae20a
PR
858 if (wctx->uninstall) {
859 if (zebra_evpn_neigh_is_static(n))
860 zebra_evpn_sync_neigh_dp_install(
861 n, false /* set_inactive */,
862 true /* force_clear_static */,
863 __func__);
864 if ((n->flags & ZEBRA_NEIGH_REMOTE))
865 zebra_evpn_neigh_uninstall(wctx->zevpn, n);
6336e12b
PR
866 }
867
7cbae20a
PR
868 zebra_evpn_neigh_del(wctx->zevpn, n);
869 }
6336e12b 870
7cbae20a
PR
871 return;
872}
6336e12b 873
7cbae20a
PR
874/*
875 * Delete all neighbor entries for this EVPN.
876 */
877void zebra_evpn_neigh_del_all(zebra_evpn_t *zevpn, int uninstall,
878 int upd_client, uint32_t flags)
879{
880 struct neigh_walk_ctx wctx;
6336e12b 881
7cbae20a
PR
882 if (!zevpn->neigh_table)
883 return;
6336e12b 884
7cbae20a
PR
885 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
886 wctx.zevpn = zevpn;
887 wctx.uninstall = uninstall;
888 wctx.upd_client = upd_client;
889 wctx.flags = flags;
6336e12b 890
7cbae20a
PR
891 hash_iterate(zevpn->neigh_table, zebra_evpn_neigh_del_hash_entry,
892 &wctx);
893}
6336e12b 894
7cbae20a
PR
895/*
896 * Look up neighbor hash entry.
897 */
898zebra_neigh_t *zebra_evpn_neigh_lookup(zebra_evpn_t *zevpn, struct ipaddr *ip)
899{
900 zebra_neigh_t tmp;
901 zebra_neigh_t *n;
6336e12b 902
7cbae20a
PR
903 memset(&tmp, 0, sizeof(tmp));
904 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
905 n = hash_lookup(zevpn->neigh_table, &tmp);
6336e12b 906
7cbae20a
PR
907 return n;
908}
6336e12b 909
7cbae20a
PR
910/*
911 * Process all neighbors associated with a MAC upon the MAC being learnt
912 * locally or undergoing any other change (such as sequence number).
913 */
914void zebra_evpn_process_neigh_on_local_mac_change(zebra_evpn_t *zevpn,
915 zebra_mac_t *zmac,
916 bool seq_change,
917 bool es_change)
918{
919 zebra_neigh_t *n = NULL;
920 struct listnode *node = NULL;
921 struct zebra_vrf *zvrf = NULL;
6336e12b 922
7cbae20a 923 zvrf = vrf_info_lookup(zevpn->vxlan_if->vrf_id);
6336e12b 924
7cbae20a 925 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
926 zlog_debug("Processing neighbors on local MAC %pEA %s, VNI %u",
927 &zmac->macaddr, seq_change ? "CHANGE" : "ADD",
928 zevpn->vni);
6336e12b 929
7cbae20a
PR
930 /* Walk all neighbors and mark any inactive local neighbors as
931 * active and/or update sequence number upon a move, and inform BGP.
932 * The action for remote neighbors is TBD.
933 * NOTE: We can't simply uninstall remote neighbors as the kernel may
934 * accidentally end up deleting a just-learnt local neighbor.
935 */
936 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
937 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
938 if (IS_ZEBRA_NEIGH_INACTIVE(n) || seq_change
939 || es_change) {
940 ZEBRA_NEIGH_SET_ACTIVE(n);
941 n->loc_seq = zmac->loc_seq;
b2ee2b71
AK
942 if (!(zebra_evpn_do_dup_addr_detect(zvrf)
943 && zvrf->dad_freeze
7cbae20a
PR
944 && !!CHECK_FLAG(n->flags,
945 ZEBRA_NEIGH_DUPLICATE)))
946 zebra_evpn_neigh_send_add_to_client(
947 zevpn->vni, &n->ip, &n->emac,
948 n->mac, n->flags, n->loc_seq);
949 }
6336e12b 950 }
7cbae20a
PR
951 }
952}
6336e12b 953
7cbae20a
PR
954/*
955 * Process all neighbors associated with a local MAC upon the MAC being
956 * deleted.
957 */
958void zebra_evpn_process_neigh_on_local_mac_del(zebra_evpn_t *zevpn,
959 zebra_mac_t *zmac)
960{
961 zebra_neigh_t *n = NULL;
962 struct listnode *node = NULL;
6336e12b 963
7cbae20a 964 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
965 zlog_debug("Processing neighbors on local MAC %pEA DEL, VNI %u",
966 &zmac->macaddr, zevpn->vni);
6336e12b 967
7cbae20a
PR
968 /* Walk all local neighbors and mark as inactive and inform
969 * BGP, if needed.
970 * TBD: There is currently no handling for remote neighbors. We
971 * don't expect them to exist, if they do, do we install the MAC
972 * as a remote MAC and the neighbor as remote?
973 */
974 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
975 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
976 if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
977 ZEBRA_NEIGH_SET_INACTIVE(n);
978 n->loc_seq = 0;
979 zebra_evpn_neigh_send_del_to_client(
980 zevpn->vni, &n->ip, &n->emac, n->flags,
981 ZEBRA_NEIGH_ACTIVE, false /*force*/);
982 }
983 }
6336e12b 984 }
6336e12b
PR
985}
986
7cbae20a
PR
987/*
988 * Process all neighbors associated with a MAC upon the MAC being remotely
989 * learnt.
990 */
991void zebra_evpn_process_neigh_on_remote_mac_add(zebra_evpn_t *zevpn,
992 zebra_mac_t *zmac)
6336e12b 993{
7cbae20a
PR
994 zebra_neigh_t *n = NULL;
995 struct listnode *node = NULL;
6336e12b 996
7cbae20a 997 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
998 zlog_debug("Processing neighbors on remote MAC %pEA ADD, VNI %u",
999 &zmac->macaddr, zevpn->vni);
6336e12b 1000
7cbae20a
PR
1001 /* Walk all local neighbors and mark as inactive and inform
1002 * BGP, if needed.
1003 */
1004 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
1005 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
1006 if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
1007 ZEBRA_NEIGH_SET_INACTIVE(n);
1008 n->loc_seq = 0;
1009 zebra_evpn_neigh_send_del_to_client(
1010 zevpn->vni, &n->ip, &n->emac, n->flags,
1011 ZEBRA_NEIGH_ACTIVE, false /* force */);
1012 }
1013 }
1014 }
6336e12b
PR
1015}
1016
7cbae20a
PR
1017/*
1018 * Process all neighbors associated with a remote MAC upon the MAC being
1019 * deleted.
1020 */
1021void zebra_evpn_process_neigh_on_remote_mac_del(zebra_evpn_t *zevpn,
1022 zebra_mac_t *zmac)
6336e12b 1023{
7cbae20a 1024 /* NOTE: Currently a NO-OP. */
6336e12b
PR
1025}
1026
7cbae20a
PR
1027static inline void zebra_evpn_local_neigh_update_log(
1028 const char *pfx, zebra_neigh_t *n, bool is_router, bool local_inactive,
1029 bool old_bgp_ready, bool new_bgp_ready, bool inform_dataplane,
1030 bool inform_bgp, const char *sfx)
1031{
7cbae20a 1032 if (!IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
6336e12b
PR
1033 return;
1034
ef7b8be4
DL
1035 zlog_debug("%s neigh vni %u ip %pIA mac %pEA f 0x%x%s%s%s%s%s%s %s", pfx,
1036 n->zevpn->vni, &n->ip, &n->emac, n->flags,
7cbae20a
PR
1037 is_router ? " router" : "",
1038 local_inactive ? " local-inactive" : "",
1039 old_bgp_ready ? " old_bgp_ready" : "",
1040 new_bgp_ready ? " new_bgp_ready" : "",
1041 inform_dataplane ? " inform_dp" : "",
1042 inform_bgp ? " inform_bgp" : "", sfx);
1043}
1044
1045/* As part Duplicate Address Detection (DAD) for IP mobility
1046 * MAC binding changes, ensure to inherit duplicate flag
1047 * from MAC.
1048 */
036daaca
PR
1049static int zebra_evpn_ip_inherit_dad_from_mac(struct zebra_vrf *zvrf,
1050 zebra_mac_t *old_zmac,
1051 zebra_mac_t *new_zmac,
1052 zebra_neigh_t *nbr)
7cbae20a
PR
1053{
1054 bool is_old_mac_dup = false;
1055 bool is_new_mac_dup = false;
6336e12b 1056
b2ee2b71 1057 if (!zebra_evpn_do_dup_addr_detect(zvrf))
7cbae20a
PR
1058 return 0;
1059 /* Check old or new MAC is detected as duplicate
1060 * mark this neigh as duplicate
1061 */
1062 if (old_zmac)
1063 is_old_mac_dup =
1064 CHECK_FLAG(old_zmac->flags, ZEBRA_MAC_DUPLICATE);
1065 if (new_zmac)
1066 is_new_mac_dup =
1067 CHECK_FLAG(new_zmac->flags, ZEBRA_MAC_DUPLICATE);
1068 /* Old and/or new MAC can be in duplicate state,
1069 * based on that IP/Neigh Inherits the flag.
1070 * If New MAC is marked duplicate, inherit to the IP.
1071 * If old MAC is duplicate but new MAC is not, clear
1072 * duplicate flag for IP and reset detection params
1073 * and let IP DAD retrigger.
6336e12b 1074 */
7cbae20a
PR
1075 if (is_new_mac_dup && !CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
1076 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
1077 /* Capture Duplicate detection time */
1078 nbr->dad_dup_detect_time = monotime(NULL);
1079 /* Mark neigh inactive */
1080 ZEBRA_NEIGH_SET_INACTIVE(nbr);
6336e12b 1081
7cbae20a
PR
1082 return 1;
1083 } else if (is_old_mac_dup && !is_new_mac_dup) {
1084 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
1085 nbr->dad_count = 0;
1086 nbr->detect_start_time.tv_sec = 0;
1087 nbr->detect_start_time.tv_usec = 0;
1088 }
1089 return 0;
6336e12b
PR
1090}
1091
7cbae20a 1092static int zebra_evpn_dad_ip_auto_recovery_exp(struct thread *t)
6336e12b 1093{
7cbae20a
PR
1094 struct zebra_vrf *zvrf = NULL;
1095 zebra_neigh_t *nbr = NULL;
6336e12b 1096 zebra_evpn_t *zevpn = NULL;
6336e12b 1097
7cbae20a 1098 nbr = THREAD_ARG(t);
6336e12b 1099
7cbae20a
PR
1100 /* since this is asynchronous we need sanity checks*/
1101 zvrf = vrf_info_lookup(nbr->zevpn->vrf_id);
1102 if (!zvrf)
1103 return 0;
6336e12b 1104
8b5fdf2e 1105 zevpn = zebra_evpn_lookup(nbr->zevpn->vni);
7cbae20a
PR
1106 if (!zevpn)
1107 return 0;
6336e12b 1108
7cbae20a
PR
1109 nbr = zebra_evpn_neigh_lookup(zevpn, &nbr->ip);
1110 if (!nbr)
1111 return 0;
1112
1113 if (IS_ZEBRA_DEBUG_VXLAN)
1114 zlog_debug(
ef7b8be4
DL
1115 "%s: duplicate addr MAC %pEA IP %pIA flags 0x%x learn count %u vni %u auto recovery expired",
1116 __func__, &nbr->emac, &nbr->ip, nbr->flags,
7cbae20a 1117 nbr->dad_count, zevpn->vni);
6336e12b 1118
7cbae20a
PR
1119 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
1120 nbr->dad_count = 0;
1121 nbr->detect_start_time.tv_sec = 0;
1122 nbr->detect_start_time.tv_usec = 0;
1123 nbr->dad_dup_detect_time = 0;
1124 nbr->dad_ip_auto_recovery_timer = NULL;
1125 ZEBRA_NEIGH_SET_ACTIVE(nbr);
6336e12b 1126
7cbae20a
PR
1127 /* Send to BGP */
1128 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
1129 zebra_evpn_neigh_send_add_to_client(zevpn->vni, &nbr->ip,
1130 &nbr->emac, nbr->mac,
1131 nbr->flags, nbr->loc_seq);
1132 } else if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
1133 zebra_evpn_rem_neigh_install(zevpn, nbr, false /*was_static*/);
1134 }
6336e12b 1135
7cbae20a
PR
1136 return 0;
1137}
6336e12b 1138
036daaca
PR
1139static void
1140zebra_evpn_dup_addr_detect_for_neigh(struct zebra_vrf *zvrf, zebra_neigh_t *nbr,
1141 struct in_addr vtep_ip, bool do_dad,
1142 bool *is_dup_detect, bool is_local)
7cbae20a
PR
1143{
1144
1145 struct timeval elapsed = {0, 0};
7cbae20a 1146 bool reset_params = false;
6336e12b 1147
b2ee2b71 1148 if (!zebra_evpn_do_dup_addr_detect(zvrf))
7cbae20a
PR
1149 return;
1150
1151 /* IP is detected as duplicate or inherit dup
1152 * state, hold on to install as remote entry
1153 * only if freeze is enabled.
1154 */
1155 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
6336e12b
PR
1156 if (IS_ZEBRA_DEBUG_VXLAN)
1157 zlog_debug(
ef7b8be4
DL
1158 "%s: duplicate addr MAC %pEA IP %pIA flags 0x%x skip installing, learn count %u recover time %u",
1159 __func__, &nbr->emac, &nbr->ip,
7cbae20a
PR
1160 nbr->flags, nbr->dad_count,
1161 zvrf->dad_freeze_time);
6336e12b 1162
7cbae20a
PR
1163 if (zvrf->dad_freeze)
1164 *is_dup_detect = true;
6336e12b 1165
7cbae20a
PR
1166 /* warn-only action, neigh will be installed.
1167 * freeze action, it wil not be installed.
6336e12b 1168 */
7cbae20a
PR
1169 return;
1170 }
6336e12b 1171
7cbae20a
PR
1172 if (!do_dad)
1173 return;
6336e12b 1174
7cbae20a
PR
1175 /* Check if detection time (M-secs) expired.
1176 * Reset learn count and detection start time.
1177 * During remote mac add, count should already be 1
1178 * via local learning.
1179 */
1180 monotime_since(&nbr->detect_start_time, &elapsed);
1181 reset_params = (elapsed.tv_sec > zvrf->dad_time);
6336e12b 1182
7cbae20a
PR
1183 if (is_local && !reset_params) {
1184 /* RFC-7432: A PE/VTEP that detects a MAC mobility
1185 * event via LOCAL learning starts an M-second timer.
1186 *
1187 * NOTE: This is the START of the probe with count is
1188 * 0 during LOCAL learn event.
1189 */
1190 reset_params = !nbr->dad_count;
6336e12b
PR
1191 }
1192
7cbae20a
PR
1193 if (reset_params) {
1194 if (IS_ZEBRA_DEBUG_VXLAN)
1195 zlog_debug(
ef7b8be4
DL
1196 "%s: duplicate addr MAC %pEA IP %pIA flags 0x%x detection time passed, reset learn count %u",
1197 __func__, &nbr->emac, &nbr->ip,
7cbae20a
PR
1198 nbr->flags, nbr->dad_count);
1199 /* Reset learn count but do not start detection
1200 * during REMOTE learn event.
1201 */
1202 nbr->dad_count = 0;
1203 /* Start dup. addr detection (DAD) start time,
1204 * ONLY during LOCAL learn.
1205 */
1206 if (is_local)
1207 monotime(&nbr->detect_start_time);
6336e12b 1208
7cbae20a
PR
1209 } else if (!is_local) {
1210 /* For REMOTE IP/Neigh, increment detection count
1211 * ONLY while in probe window, once window passed,
1212 * next local learn event should trigger DAD.
1213 */
1214 nbr->dad_count++;
6336e12b
PR
1215 }
1216
7cbae20a
PR
1217 /* For LOCAL IP/Neigh learn event, once count is reset above via either
1218 * initial/start detection time or passed the probe time, the count
1219 * needs to be incremented.
1220 */
1221 if (is_local)
1222 nbr->dad_count++;
6336e12b 1223
7cbae20a
PR
1224 if (nbr->dad_count >= zvrf->dad_max_moves) {
1225 flog_warn(
1226 EC_ZEBRA_DUP_IP_DETECTED,
ef7b8be4
DL
1227 "VNI %u: MAC %pEA IP %pIA detected as duplicate during %s VTEP %pI4",
1228 nbr->zevpn->vni, &nbr->emac, &nbr->ip,
7cbae20a 1229 is_local ? "local update, last" : "remote update, from",
9bcef951 1230 &vtep_ip);
6336e12b 1231
7cbae20a 1232 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
6336e12b 1233
7cbae20a
PR
1234 /* Capture Duplicate detection time */
1235 nbr->dad_dup_detect_time = monotime(NULL);
6336e12b 1236
7cbae20a
PR
1237 /* Start auto recovery timer for this IP */
1238 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
1239 if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
1240 if (IS_ZEBRA_DEBUG_VXLAN)
1241 zlog_debug(
ef7b8be4
DL
1242 "%s: duplicate addr MAC %pEA IP %pIA flags 0x%x auto recovery time %u start",
1243 __func__, &nbr->emac, &nbr->ip,
7cbae20a 1244 nbr->flags, zvrf->dad_freeze_time);
6336e12b 1245
7cbae20a
PR
1246 thread_add_timer(zrouter.master,
1247 zebra_evpn_dad_ip_auto_recovery_exp,
1248 nbr, zvrf->dad_freeze_time,
1249 &nbr->dad_ip_auto_recovery_timer);
1250 }
1251 if (zvrf->dad_freeze)
1252 *is_dup_detect = true;
1253 }
6336e12b
PR
1254}
1255
7cbae20a
PR
1256int zebra_evpn_local_neigh_update(zebra_evpn_t *zevpn, struct interface *ifp,
1257 struct ipaddr *ip, struct ethaddr *macaddr,
1258 bool is_router, bool local_inactive,
1259 bool dp_static)
6336e12b 1260{
7cbae20a
PR
1261 struct zebra_vrf *zvrf;
1262 zebra_neigh_t *n = NULL;
1263 zebra_mac_t *zmac = NULL, *old_zmac = NULL;
1264 uint32_t old_mac_seq = 0, mac_new_seq = 0;
1265 bool upd_mac_seq = false;
1266 bool neigh_mac_change = false;
1267 bool neigh_on_hold = false;
1268 bool neigh_was_remote = false;
1269 bool do_dad = false;
1270 struct in_addr vtep_ip = {.s_addr = 0};
1271 bool inform_dataplane = false;
1272 bool created = false;
1273 bool new_static = false;
1274 bool old_bgp_ready = false;
1275 bool new_bgp_ready;
6336e12b 1276
7cbae20a
PR
1277 /* Check if the MAC exists. */
1278 zmac = zebra_evpn_mac_lookup(zevpn, macaddr);
1279 if (!zmac) {
1280 /* create a dummy MAC if the MAC is not already present */
6336e12b 1281 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
1282 zlog_debug("AUTO MAC %pEA created for neigh %pIA on VNI %u",
1283 macaddr, ip, zevpn->vni);
6336e12b 1284
7cbae20a
PR
1285 zmac = zebra_evpn_mac_add(zevpn, macaddr);
1286 if (!zmac) {
ef7b8be4 1287 zlog_debug("Failed to add MAC %pEA VNI %u", macaddr,
7cbae20a
PR
1288 zevpn->vni);
1289 return -1;
1290 }
6336e12b 1291
8b07f173 1292 zebra_evpn_mac_clear_fwd_info(zmac);
7cbae20a
PR
1293 memset(&zmac->flags, 0, sizeof(uint32_t));
1294 SET_FLAG(zmac->flags, ZEBRA_MAC_AUTO);
1295 } else {
1296 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
1297 /*
1298 * We don't change the MAC to local upon a neighbor
1299 * learn event, we wait for the explicit local MAC
1300 * learn. However, we have to compute its sequence
1301 * number in preparation for when it actually turns
1302 * local.
1303 */
1304 upd_mac_seq = true;
1305 }
1306 }
6336e12b 1307
7cbae20a
PR
1308 zvrf = vrf_info_lookup(zevpn->vxlan_if->vrf_id);
1309 if (!zvrf) {
1310 if (IS_ZEBRA_DEBUG_VXLAN)
1311 zlog_debug(" Unable to find vrf for: %d",
1312 zevpn->vxlan_if->vrf_id);
1313 return -1;
1314 }
6336e12b 1315
7cbae20a
PR
1316 /* Check if the neighbor exists. */
1317 n = zebra_evpn_neigh_lookup(zevpn, ip);
1318 if (!n) {
1319 /* New neighbor - create */
1320 n = zebra_evpn_neigh_add(zevpn, ip, macaddr, zmac, 0);
1321 if (!n) {
1322 flog_err(
1323 EC_ZEBRA_MAC_ADD_FAILED,
ef7b8be4
DL
1324 "Failed to add neighbor %pIA MAC %pEA intf %s(%u) -> VNI %u",
1325 ip, macaddr, ifp->name, ifp->ifindex,
1326 zevpn->vni);
7cbae20a
PR
1327 return -1;
1328 }
1329 /* Set "local" forwarding info. */
1330 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
1331 n->ifindex = ifp->ifindex;
1332 created = true;
6336e12b 1333 } else {
7cbae20a
PR
1334 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
1335 bool mac_different;
1336 bool cur_is_router;
1337 bool old_local_inactive;
6336e12b 1338
7cbae20a
PR
1339 old_local_inactive = !!CHECK_FLAG(
1340 n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
6336e12b 1341
7cbae20a 1342 old_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
6336e12b 1343
7cbae20a
PR
1344 /* Note any changes and see if of interest to BGP. */
1345 mac_different = !!memcmp(&n->emac, macaddr, ETH_ALEN);
1346 cur_is_router =
1347 !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
1348 new_static = zebra_evpn_neigh_is_static(n);
1349 if (!mac_different && is_router == cur_is_router
1350 && old_local_inactive == local_inactive
1351 && dp_static != new_static) {
1352 if (IS_ZEBRA_DEBUG_VXLAN)
1353 zlog_debug(
1354 " Ignoring entry mac is the same and is_router == cur_is_router");
1355 n->ifindex = ifp->ifindex;
1356 return 0;
1357 }
6336e12b 1358
7cbae20a
PR
1359 old_zmac = n->mac;
1360 if (!mac_different) {
1361 /* XXX - cleanup this code duplication */
1362 bool is_neigh_freezed = false;
6336e12b 1363
7cbae20a
PR
1364 /* Only the router flag has changed. */
1365 if (is_router)
1366 SET_FLAG(n->flags,
1367 ZEBRA_NEIGH_ROUTER_FLAG);
1368 else
1369 UNSET_FLAG(n->flags,
1370 ZEBRA_NEIGH_ROUTER_FLAG);
6336e12b 1371
7cbae20a
PR
1372 if (local_inactive)
1373 SET_FLAG(n->flags,
1374 ZEBRA_NEIGH_LOCAL_INACTIVE);
1375 else
1376 UNSET_FLAG(n->flags,
1377 ZEBRA_NEIGH_LOCAL_INACTIVE);
1378 new_bgp_ready =
1379 zebra_evpn_neigh_is_ready_for_bgp(n);
6336e12b 1380
7c0e4dc6
AK
1381 if (dp_static != new_static)
1382 inform_dataplane = true;
1383
7cbae20a
PR
1384 /* Neigh is in freeze state and freeze action
1385 * is enabled, do not send update to client.
1386 */
1387 is_neigh_freezed =
b2ee2b71 1388 (zebra_evpn_do_dup_addr_detect(zvrf)
7cbae20a
PR
1389 && zvrf->dad_freeze
1390 && CHECK_FLAG(n->flags,
1391 ZEBRA_NEIGH_DUPLICATE));
6336e12b 1392
7cbae20a
PR
1393 zebra_evpn_local_neigh_update_log(
1394 "local", n, is_router, local_inactive,
1395 old_bgp_ready, new_bgp_ready, false,
1396 false, "flag-update");
6336e12b 1397
7c0e4dc6
AK
1398 if (inform_dataplane)
1399 zebra_evpn_sync_neigh_dp_install(
1400 n, false /* set_inactive */,
1401 false /* force_clear_static */,
1402 __func__);
1403
7cbae20a
PR
1404 /* if the neigh can no longer be advertised
1405 * remove it from bgp
1406 */
1407 if (!is_neigh_freezed) {
1408 zebra_evpn_neigh_send_add_del_to_client(
1409 n, old_bgp_ready,
1410 new_bgp_ready);
1411 } else {
1412 if (IS_ZEBRA_DEBUG_VXLAN
1413 && IS_ZEBRA_NEIGH_ACTIVE(n))
1414 zlog_debug(
1415 " Neighbor active and frozen");
1416 }
1417 return 0;
1418 }
6336e12b 1419
7cbae20a
PR
1420 /* The MAC has changed, need to issue a delete
1421 * first as this means a different MACIP route.
1422 * Also, need to do some unlinking/relinking.
1423 * We also need to update the MAC's sequence number
1424 * in different situations.
1425 */
1426 if (old_bgp_ready) {
1427 zebra_evpn_neigh_send_del_to_client(
1428 zevpn->vni, &n->ip, &n->emac, n->flags,
1429 n->state, false /*force*/);
1430 old_bgp_ready = false;
1431 }
1432 if (old_zmac) {
1433 old_mac_seq = CHECK_FLAG(old_zmac->flags,
1434 ZEBRA_MAC_REMOTE)
1435 ? old_zmac->rem_seq
1436 : old_zmac->loc_seq;
1437 neigh_mac_change = upd_mac_seq = true;
1438 zebra_evpn_local_neigh_deref_mac(
1439 n, true /* send_mac_update */);
1440 }
6336e12b 1441
7cbae20a
PR
1442 /* if mac changes abandon peer flags and tell
1443 * dataplane to clear the static flag
1444 */
1445 if (zebra_evpn_neigh_clear_sync_info(n))
1446 inform_dataplane = true;
1447 /* Update the forwarding info. */
1448 n->ifindex = ifp->ifindex;
1449
1450 /* Link to new MAC */
1451 zebra_evpn_local_neigh_ref_mac(
1452 n, macaddr, zmac, true /* send_mac_update */);
1453 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
1454 /*
1455 * Neighbor has moved from remote to local. Its
1456 * MAC could have also changed as part of the move.
1457 */
1458 if (memcmp(n->emac.octet, macaddr->octet, ETH_ALEN)
1459 != 0) {
1460 old_zmac = n->mac;
1461 if (old_zmac) {
1462 old_mac_seq =
1463 CHECK_FLAG(old_zmac->flags,
1464 ZEBRA_MAC_REMOTE)
1465 ? old_zmac->rem_seq
1466 : old_zmac->loc_seq;
1467 neigh_mac_change = upd_mac_seq = true;
1468 zebra_evpn_local_neigh_deref_mac(
1469 n, true /* send_update */);
1470 }
6336e12b 1471
7cbae20a
PR
1472 /* Link to new MAC */
1473 zebra_evpn_local_neigh_ref_mac(
1474 n, macaddr, zmac, true /*send_update*/);
1475 }
1476 /* Based on Mobility event Scenario-B from the
1477 * draft, neigh's previous state was remote treat this
1478 * event for DAD.
1479 */
1480 neigh_was_remote = true;
1481 vtep_ip = n->r_vtep_ip;
1482 /* Mark appropriately */
1483 UNSET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
1484 n->r_vtep_ip.s_addr = INADDR_ANY;
1485 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
1486 n->ifindex = ifp->ifindex;
6336e12b
PR
1487 }
1488 }
1489
7cbae20a
PR
1490 /* If MAC was previously remote, or the neighbor had a different
1491 * MAC earlier, recompute the sequence number.
1492 */
1493 if (upd_mac_seq) {
1494 uint32_t seq1, seq2;
6336e12b 1495
7cbae20a
PR
1496 seq1 = CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)
1497 ? zmac->rem_seq + 1
1498 : zmac->loc_seq;
1499 seq2 = neigh_mac_change ? old_mac_seq + 1 : 0;
1500 mac_new_seq = zmac->loc_seq < MAX(seq1, seq2) ? MAX(seq1, seq2)
1501 : zmac->loc_seq;
1502 }
6336e12b 1503
7cbae20a
PR
1504 if (local_inactive)
1505 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
1506 else
1507 UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
1508
1509 /* Mark Router flag (R-bit) */
1510 if (is_router)
1511 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
1512 else
1513 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
1514
7c0e4dc6
AK
1515 /* if zebra and dataplane don't agree this is a sync entry
1516 * re-install in the dataplane */
1517 new_static = zebra_evpn_neigh_is_static(n);
1518 if (dp_static != new_static)
1519 inform_dataplane = true;
7cbae20a
PR
1520
1521 /* Check old and/or new MAC detected as duplicate mark
1522 * the neigh as duplicate
1523 */
1524 if (zebra_evpn_ip_inherit_dad_from_mac(zvrf, old_zmac, zmac, n)) {
1525 flog_warn(
1526 EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
ef7b8be4
DL
1527 "VNI %u: MAC %pEA IP %pIA detected as duplicate during local update, inherit duplicate from MAC",
1528 zevpn->vni, macaddr, &n->ip);
7cbae20a 1529 }
6336e12b 1530
7cbae20a
PR
1531 /* For IP Duplicate Address Detection (DAD) is trigger,
1532 * when the event is extended mobility based on scenario-B
1533 * from the draft, IP/Neigh's MAC binding changed and
1534 * neigh's previous state was remote.
1535 */
1536 if (neigh_mac_change && neigh_was_remote)
1537 do_dad = true;
6336e12b 1538
7cbae20a
PR
1539 zebra_evpn_dup_addr_detect_for_neigh(zvrf, n, vtep_ip, do_dad,
1540 &neigh_on_hold, true);
6336e12b 1541
7cbae20a
PR
1542 if (inform_dataplane)
1543 zebra_evpn_sync_neigh_dp_install(n, false /* set_inactive */,
1544 false /* force_clear_static */,
1545 __func__);
6336e12b 1546
7cbae20a
PR
1547 /* Before we program this in BGP, we need to check if MAC is locally
1548 * learnt. If not, force neighbor to be inactive and reset its seq.
1549 */
1550 if (!CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL)) {
1551 zebra_evpn_local_neigh_update_log(
1552 "local", n, is_router, local_inactive, false, false,
1553 inform_dataplane, false, "auto-mac");
1554 ZEBRA_NEIGH_SET_INACTIVE(n);
1555 n->loc_seq = 0;
1556 zmac->loc_seq = mac_new_seq;
1557 return 0;
1558 }
6336e12b 1559
7cbae20a
PR
1560 zebra_evpn_local_neigh_update_log("local", n, is_router, local_inactive,
1561 false, false, inform_dataplane, true,
1562 created ? "created" : "updated");
6336e12b 1563
7cbae20a
PR
1564 /* If the MAC's sequence number has changed, inform the MAC and all
1565 * neighbors associated with the MAC to BGP, else just inform this
1566 * neighbor.
1567 */
1568 if (upd_mac_seq && zmac->loc_seq != mac_new_seq) {
1569 if (IS_ZEBRA_DEBUG_VXLAN)
1570 zlog_debug(
ef7b8be4
DL
1571 "Seq changed for MAC %pEA VNI %u - old %u new %u",
1572 macaddr, zevpn->vni,
1573 zmac->loc_seq, mac_new_seq);
7cbae20a
PR
1574 zmac->loc_seq = mac_new_seq;
1575 if (zebra_evpn_mac_send_add_to_client(zevpn->vni, macaddr,
1576 zmac->flags,
1577 zmac->loc_seq, zmac->es))
1578 return -1;
1579 zebra_evpn_process_neigh_on_local_mac_change(zevpn, zmac, 1,
1580 0 /*es_change*/);
1581 return 0;
1582 }
6336e12b 1583
7cbae20a 1584 n->loc_seq = zmac->loc_seq;
6336e12b 1585
7cbae20a
PR
1586 if (!neigh_on_hold) {
1587 ZEBRA_NEIGH_SET_ACTIVE(n);
1588 new_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
1589 zebra_evpn_neigh_send_add_del_to_client(n, old_bgp_ready,
1590 new_bgp_ready);
1591 } else {
1592 if (IS_ZEBRA_DEBUG_VXLAN)
1593 zlog_debug(" Neighbor on hold not sending");
1594 }
1595 return 0;
1596}
6336e12b 1597
7cbae20a
PR
1598int zebra_evpn_remote_neigh_update(zebra_evpn_t *zevpn, struct interface *ifp,
1599 struct ipaddr *ip, struct ethaddr *macaddr,
1600 uint16_t state)
1601{
7cbae20a
PR
1602 zebra_neigh_t *n = NULL;
1603 zebra_mac_t *zmac = NULL;
6336e12b 1604
7cbae20a
PR
1605 /* If the neighbor is unknown, there is no further action. */
1606 n = zebra_evpn_neigh_lookup(zevpn, ip);
1607 if (!n)
1608 return 0;
6336e12b 1609
7cbae20a
PR
1610 /* If a remote entry, see if it needs to be refreshed */
1611 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
1612#ifdef GNU_LINUX
1613 if (state & NUD_STALE)
1614 zebra_evpn_rem_neigh_install(zevpn, n,
1615 false /*was_static*/);
1616#endif
6336e12b 1617 } else {
7cbae20a
PR
1618 /* We got a "remote" neighbor notification for an entry
1619 * we think is local. This can happen in a multihoming
1620 * scenario - but only if the MAC is already "remote".
1621 * Just mark our entry as "remote".
6336e12b 1622 */
7cbae20a
PR
1623 zmac = zebra_evpn_mac_lookup(zevpn, macaddr);
1624 if (!zmac || !CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
1625 zlog_debug(
ef7b8be4
DL
1626 "Ignore remote neigh %pIA (MAC %pEA) on L2-VNI %u - MAC unknown or local",
1627 &n->ip, macaddr, zevpn->vni);
7cbae20a
PR
1628 return -1;
1629 }
6336e12b 1630
7cbae20a
PR
1631 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ALL_LOCAL_FLAGS);
1632 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
1633 ZEBRA_NEIGH_SET_ACTIVE(n);
1634 n->r_vtep_ip = zmac->fwd_info.r_vtep_ip;
6336e12b
PR
1635 }
1636
7cbae20a 1637 return 0;
6336e12b
PR
1638}
1639
7cbae20a
PR
1640/* Notify Neighbor entries to the Client, skips the GW entry */
1641static void
1642zebra_evpn_send_neigh_hash_entry_to_client(struct hash_bucket *bucket,
1643 void *arg)
6336e12b 1644{
7cbae20a
PR
1645 struct mac_walk_ctx *wctx = arg;
1646 zebra_neigh_t *zn = bucket->data;
1647 zebra_mac_t *zmac = NULL;
6336e12b 1648
7cbae20a 1649 if (CHECK_FLAG(zn->flags, ZEBRA_NEIGH_DEF_GW))
6336e12b 1650 return;
6336e12b 1651
7cbae20a
PR
1652 if (CHECK_FLAG(zn->flags, ZEBRA_NEIGH_LOCAL)
1653 && IS_ZEBRA_NEIGH_ACTIVE(zn)) {
1654 zmac = zebra_evpn_mac_lookup(wctx->zevpn, &zn->emac);
1655 if (!zmac)
1656 return;
6336e12b 1657
7cbae20a
PR
1658 zebra_evpn_neigh_send_add_to_client(wctx->zevpn->vni, &zn->ip,
1659 &zn->emac, zn->mac,
1660 zn->flags, zn->loc_seq);
1661 }
6336e12b
PR
1662}
1663
7cbae20a
PR
1664/* Iterator of a specific EVPN */
1665void zebra_evpn_send_neigh_to_client(zebra_evpn_t *zevpn)
6336e12b 1666{
7cbae20a 1667 struct neigh_walk_ctx wctx;
6336e12b 1668
7cbae20a
PR
1669 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
1670 wctx.zevpn = zevpn;
6336e12b 1671
7cbae20a
PR
1672 hash_iterate(zevpn->neigh_table,
1673 zebra_evpn_send_neigh_hash_entry_to_client, &wctx);
6336e12b
PR
1674}
1675
7cbae20a 1676void zebra_evpn_clear_dup_neigh_hash(struct hash_bucket *bucket, void *ctxt)
6336e12b 1677{
7cbae20a
PR
1678 struct neigh_walk_ctx *wctx = ctxt;
1679 zebra_neigh_t *nbr;
1680 zebra_evpn_t *zevpn;
1681 char buf[INET6_ADDRSTRLEN];
6336e12b 1682
7cbae20a
PR
1683 nbr = (zebra_neigh_t *)bucket->data;
1684 if (!nbr)
1685 return;
6336e12b 1686
7cbae20a 1687 zevpn = wctx->zevpn;
6336e12b 1688
7cbae20a
PR
1689 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
1690 return;
6336e12b 1691
7cbae20a
PR
1692 if (IS_ZEBRA_DEBUG_VXLAN) {
1693 ipaddr2str(&nbr->ip, buf, sizeof(buf));
1694 zlog_debug("%s: clear neigh %s dup state, flags 0x%x seq %u",
1695 __func__, buf, nbr->flags, nbr->loc_seq);
1696 }
6336e12b
PR
1697
1698 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
1699 nbr->dad_count = 0;
1700 nbr->detect_start_time.tv_sec = 0;
1701 nbr->detect_start_time.tv_usec = 0;
1702 nbr->dad_dup_detect_time = 0;
7cbae20a 1703 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
6336e12b 1704
6336e12b 1705 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
7cbae20a
PR
1706 zebra_evpn_neigh_send_add_to_client(zevpn->vni, &nbr->ip,
1707 &nbr->emac, nbr->mac,
1708 nbr->flags, nbr->loc_seq);
1709 } else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
6336e12b
PR
1710 zebra_evpn_rem_neigh_install(zevpn, nbr, false /*was_static*/);
1711 }
6336e12b
PR
1712}
1713
7cbae20a
PR
1714/*
1715 * Print a specific neighbor entry.
1716 */
1717void zebra_evpn_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json)
6336e12b 1718{
7cbae20a
PR
1719 struct vty *vty;
1720 char buf1[ETHER_ADDR_STRLEN];
1721 char buf2[INET6_ADDRSTRLEN];
9bcef951 1722 char addr_buf[PREFIX_STRLEN];
7cbae20a
PR
1723 const char *type_str;
1724 const char *state_str;
1725 bool flags_present = false;
1726 struct zebra_vrf *zvrf = NULL;
1727 struct timeval detect_start_time = {0, 0};
1728 char timebuf[MONOTIME_STRLEN];
1729 char thread_buf[THREAD_TIMER_STRLEN];
a05111ba
DS
1730 time_t uptime;
1731 char up_str[MONOTIME_STRLEN];
6336e12b 1732
7cbae20a
PR
1733 zvrf = zebra_vrf_get_evpn();
1734 if (!zvrf)
1735 return;
6336e12b 1736
a05111ba
DS
1737 uptime = monotime(NULL);
1738 uptime -= n->uptime;
1739
1740 frrtime_to_interval(uptime, up_str, sizeof(up_str));
1741
7cbae20a
PR
1742 ipaddr2str(&n->ip, buf2, sizeof(buf2));
1743 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
1744 type_str = CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) ? "local" : "remote";
1745 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
1746 vty = (struct vty *)ctxt;
1747 if (json == NULL) {
1748 bool sync_info = false;
6336e12b 1749
7cbae20a
PR
1750 vty_out(vty, "IP: %s\n",
1751 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
1752 vty_out(vty, " Type: %s\n", type_str);
1753 vty_out(vty, " State: %s\n", state_str);
a05111ba 1754 vty_out(vty, " Uptime: %s\n", up_str);
7cbae20a
PR
1755 vty_out(vty, " MAC: %s\n",
1756 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
1757 vty_out(vty, " Sync-info:");
1758 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE)) {
1759 vty_out(vty, " local-inactive");
1760 sync_info = true;
1761 }
1762 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY)) {
1763 vty_out(vty, " peer-proxy");
1764 sync_info = true;
1765 }
1766 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE)) {
1767 vty_out(vty, " peer-active");
1768 sync_info = true;
1769 }
1770 if (n->hold_timer) {
1771 vty_out(vty, " (ht: %s)",
1772 thread_timer_to_hhmmss(thread_buf,
1773 sizeof(thread_buf),
1774 n->hold_timer));
1775 sync_info = true;
1776 }
1777 if (!sync_info)
1778 vty_out(vty, " -");
1779 vty_out(vty, "\n");
1780 } else {
a05111ba 1781 json_object_string_add(json, "uptime", up_str);
7cbae20a
PR
1782 json_object_string_add(json, "ip", buf2);
1783 json_object_string_add(json, "type", type_str);
1784 json_object_string_add(json, "state", state_str);
1785 json_object_string_add(json, "mac", buf1);
1786 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE))
1787 json_object_boolean_true_add(json, "localInactive");
1788 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY))
1789 json_object_boolean_true_add(json, "peerProxy");
1790 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE))
1791 json_object_boolean_true_add(json, "peerActive");
1792 if (n->hold_timer)
1793 json_object_string_add(
1794 json, "peerActiveHold",
1795 thread_timer_to_hhmmss(thread_buf,
1796 sizeof(thread_buf),
1797 n->hold_timer));
6336e12b 1798 }
7cbae20a
PR
1799 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
1800 if (n->mac->es) {
1801 if (json)
1802 json_object_string_add(json, "remoteEs",
1803 n->mac->es->esi_str);
1804 else
1805 vty_out(vty, " Remote ES: %s\n",
1806 n->mac->es->esi_str);
1807 } else {
1808 if (json)
9bcef951
MS
1809 json_object_string_add(
1810 json, "remoteVtep",
1811 inet_ntop(AF_INET, &n->r_vtep_ip,
1812 addr_buf, sizeof(addr_buf)));
7cbae20a 1813 else
9bcef951
MS
1814 vty_out(vty, " Remote VTEP: %pI4\n",
1815 &n->r_vtep_ip);
7cbae20a 1816 }
6336e12b 1817 }
7cbae20a
PR
1818 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW)) {
1819 if (!json) {
1820 vty_out(vty, " Flags: Default-gateway");
1821 flags_present = true;
1822 } else
1823 json_object_boolean_true_add(json, "defaultGateway");
6336e12b 1824 }
7cbae20a
PR
1825 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)) {
1826 if (!json) {
1827 vty_out(vty,
1828 flags_present ? " ,Router" : " Flags: Router");
1829 flags_present = true;
1830 }
1831 }
1832 if (json == NULL) {
1833 if (flags_present)
1834 vty_out(vty, "\n");
1835 vty_out(vty, " Local Seq: %u Remote Seq: %u\n", n->loc_seq,
1836 n->rem_seq);
6336e12b 1837
7cbae20a
PR
1838 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE)) {
1839 vty_out(vty, " Duplicate, detected at %s",
1840 time_to_string(n->dad_dup_detect_time,
1841 timebuf));
1842 } else if (n->dad_count) {
1843 monotime_since(&n->detect_start_time,
1844 &detect_start_time);
1845 if (detect_start_time.tv_sec <= zvrf->dad_time) {
1846 time_to_string(n->detect_start_time.tv_sec,
1847 timebuf);
1848 vty_out(vty,
1849 " Duplicate detection started at %s, detection count %u\n",
1850 timebuf, n->dad_count);
1851 }
1852 }
1853 } else {
1854 json_object_int_add(json, "localSequence", n->loc_seq);
1855 json_object_int_add(json, "remoteSequence", n->rem_seq);
1856 json_object_int_add(json, "detectionCount", n->dad_count);
1857 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
1858 json_object_boolean_true_add(json, "isDuplicate");
1859 else
1860 json_object_boolean_false_add(json, "isDuplicate");
1861 }
6336e12b
PR
1862}
1863
7cbae20a 1864void zebra_evpn_print_neigh_hdr(struct vty *vty, struct neigh_walk_ctx *wctx)
6336e12b 1865{
7cbae20a
PR
1866 vty_out(vty, "Flags: I=local-inactive, P=peer-active, X=peer-proxy\n");
1867 vty_out(vty, "%*s %-6s %-5s %-8s %-17s %-30s %s\n", -wctx->addr_width,
1868 "Neighbor", "Type", "Flags", "State", "MAC", "Remote ES/VTEP",
1869 "Seq #'s");
6336e12b
PR
1870}
1871
7cbae20a
PR
1872static char *zebra_evpn_print_neigh_flags(zebra_neigh_t *n, char *flags_buf,
1873 uint32_t flags_buf_sz)
6336e12b 1874{
7cbae20a
PR
1875 snprintf(flags_buf, flags_buf_sz, "%s%s%s",
1876 (n->flags & ZEBRA_NEIGH_ES_PEER_ACTIVE) ?
1877 "P" : "",
1878 (n->flags & ZEBRA_NEIGH_ES_PEER_PROXY) ?
1879 "X" : "",
1880 (n->flags & ZEBRA_NEIGH_LOCAL_INACTIVE) ?
1881 "I" : "");
6336e12b 1882
7cbae20a 1883 return flags_buf;
6336e12b
PR
1884}
1885
7cbae20a
PR
1886/*
1887 * Print neighbor hash entry - called for display of all neighbors.
1888 */
1889void zebra_evpn_print_neigh_hash(struct hash_bucket *bucket, void *ctxt)
6336e12b 1890{
7cbae20a
PR
1891 struct vty *vty;
1892 json_object *json_evpn = NULL, *json_row = NULL;
1893 zebra_neigh_t *n;
1894 char buf1[ETHER_ADDR_STRLEN];
1895 char buf2[INET6_ADDRSTRLEN];
9bcef951 1896 char addr_buf[PREFIX_STRLEN];
7cbae20a
PR
1897 struct neigh_walk_ctx *wctx = ctxt;
1898 const char *state_str;
1899 char flags_buf[6];
6336e12b 1900
7cbae20a
PR
1901 vty = wctx->vty;
1902 json_evpn = wctx->json;
1903 n = (zebra_neigh_t *)bucket->data;
6336e12b 1904
7cbae20a
PR
1905 if (json_evpn)
1906 json_row = json_object_new_object();
6336e12b 1907
7cbae20a
PR
1908 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
1909 ipaddr2str(&n->ip, buf2, sizeof(buf2));
1910 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
1911 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
1912 if (wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
1913 return;
6336e12b 1914
7cbae20a
PR
1915 if (json_evpn == NULL) {
1916 vty_out(vty, "%*s %-6s %-5s %-8s %-17s %-30s %u/%u\n",
1917 -wctx->addr_width, buf2, "local",
1918 zebra_evpn_print_neigh_flags(n, flags_buf,
1919 sizeof(flags_buf)), state_str, buf1,
1920 "", n->loc_seq, n->rem_seq);
1921 } else {
1922 json_object_string_add(json_row, "type", "local");
1923 json_object_string_add(json_row, "state", state_str);
1924 json_object_string_add(json_row, "mac", buf1);
1925 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
1926 json_object_boolean_true_add(json_row,
1927 "defaultGateway");
1928 json_object_int_add(json_row, "localSequence",
1929 n->loc_seq);
1930 json_object_int_add(json_row, "remoteSequence",
1931 n->rem_seq);
1932 json_object_int_add(json_row, "detectionCount",
1933 n->dad_count);
1934 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
1935 json_object_boolean_true_add(json_row,
1936 "isDuplicate");
1937 else
1938 json_object_boolean_false_add(json_row,
1939 "isDuplicate");
1940 }
1941 wctx->count++;
1942 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
1943 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
1944 && !IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))
6336e12b
PR
1945 return;
1946
7cbae20a
PR
1947 if (json_evpn == NULL) {
1948 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
1949 && (wctx->count == 0))
1950 zebra_evpn_print_neigh_hdr(vty, wctx);
9bcef951
MS
1951
1952 if (n->mac->es == NULL)
1953 inet_ntop(AF_INET, &n->r_vtep_ip,
1954 addr_buf, sizeof(addr_buf));
1955
7cbae20a
PR
1956 vty_out(vty, "%*s %-6s %-5s %-8s %-17s %-30s %u/%u\n",
1957 -wctx->addr_width, buf2, "remote",
1958 zebra_evpn_print_neigh_flags(n, flags_buf,
1959 sizeof(flags_buf)), state_str, buf1,
9bcef951 1960 n->mac->es ? n->mac->es->esi_str : addr_buf,
7cbae20a
PR
1961 n->loc_seq, n->rem_seq);
1962 } else {
1963 json_object_string_add(json_row, "type", "remote");
1964 json_object_string_add(json_row, "state", state_str);
1965 json_object_string_add(json_row, "mac", buf1);
1966 if (n->mac->es)
1967 json_object_string_add(json_row, "remoteEs",
1968 n->mac->es->esi_str);
1969 else
9bcef951
MS
1970 json_object_string_add(
1971 json_row, "remoteVtep",
1972 inet_ntop(AF_INET, &n->r_vtep_ip,
1973 addr_buf, sizeof(addr_buf)));
7cbae20a
PR
1974 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
1975 json_object_boolean_true_add(json_row,
1976 "defaultGateway");
1977 json_object_int_add(json_row, "localSequence",
1978 n->loc_seq);
1979 json_object_int_add(json_row, "remoteSequence",
1980 n->rem_seq);
1981 json_object_int_add(json_row, "detectionCount",
1982 n->dad_count);
1983 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
1984 json_object_boolean_true_add(json_row,
1985 "isDuplicate");
1986 else
1987 json_object_boolean_false_add(json_row,
1988 "isDuplicate");
1989 }
1990 wctx->count++;
6336e12b 1991 }
6336e12b 1992
7cbae20a
PR
1993 if (json_evpn)
1994 json_object_object_add(json_evpn, buf2, json_row);
6336e12b
PR
1995}
1996
7cbae20a
PR
1997/*
1998 * Print neighbor hash entry in detail - called for display of all neighbors.
1999 */
2000void zebra_evpn_print_neigh_hash_detail(struct hash_bucket *bucket, void *ctxt)
6336e12b 2001{
7cbae20a
PR
2002 struct vty *vty;
2003 json_object *json_evpn = NULL, *json_row = NULL;
2004 zebra_neigh_t *n;
2005 char buf[INET6_ADDRSTRLEN];
2006 struct neigh_walk_ctx *wctx = ctxt;
6336e12b 2007
7cbae20a
PR
2008 vty = wctx->vty;
2009 json_evpn = wctx->json;
2010 n = (zebra_neigh_t *)bucket->data;
2011 if (!n)
2012 return;
6336e12b 2013
7cbae20a
PR
2014 ipaddr2str(&n->ip, buf, sizeof(buf));
2015 if (json_evpn)
2016 json_row = json_object_new_object();
6336e12b 2017
7cbae20a 2018 zebra_evpn_print_neigh(n, vty, json_row);
6336e12b 2019
7cbae20a
PR
2020 if (json_evpn)
2021 json_object_object_add(json_evpn, buf, json_row);
6336e12b
PR
2022}
2023
7cbae20a 2024void zebra_evpn_print_dad_neigh_hash(struct hash_bucket *bucket, void *ctxt)
6336e12b 2025{
7cbae20a 2026 zebra_neigh_t *nbr;
6336e12b 2027
7cbae20a
PR
2028 nbr = (zebra_neigh_t *)bucket->data;
2029 if (!nbr)
2030 return;
6336e12b 2031
7cbae20a
PR
2032 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
2033 zebra_evpn_print_neigh_hash(bucket, ctxt);
6336e12b
PR
2034}
2035
7cbae20a
PR
2036void zebra_evpn_print_dad_neigh_hash_detail(struct hash_bucket *bucket,
2037 void *ctxt)
6336e12b 2038{
7cbae20a 2039 zebra_neigh_t *nbr;
6336e12b 2040
7cbae20a
PR
2041 nbr = (zebra_neigh_t *)bucket->data;
2042 if (!nbr)
2043 return;
6336e12b 2044
7cbae20a
PR
2045 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
2046 zebra_evpn_print_neigh_hash_detail(bucket, ctxt);
6336e12b 2047}
036daaca
PR
2048
2049void process_neigh_remote_macip_add(zebra_evpn_t *zevpn, struct zebra_vrf *zvrf,
2050 struct ipaddr *ipaddr, zebra_mac_t *mac,
2051 struct in_addr vtep_ip, uint8_t flags,
2052 uint32_t seq)
2053{
2054 zebra_neigh_t *n;
2055 int update_neigh = 0;
036daaca
PR
2056 zebra_mac_t *old_mac = NULL;
2057 bool old_static = false;
2058 bool do_dad = false;
2059 bool is_dup_detect = false;
2060 bool is_router;
2061
2bdd4461 2062 assert(mac);
036daaca
PR
2063 is_router = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
2064
2065 /* Check if the remote neighbor itself is unknown or has a
2066 * change. If so, create or update and then install the entry.
2067 */
2068 n = zebra_evpn_neigh_lookup(zevpn, ipaddr);
2069 if (!n || !CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
2070 || is_router != !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)
2071 || (memcmp(&n->emac, &mac->macaddr, sizeof(struct ethaddr)) != 0)
2072 || !IPV4_ADDR_SAME(&n->r_vtep_ip, &vtep_ip) || seq != n->rem_seq)
2073 update_neigh = 1;
2074
2075 if (update_neigh) {
2076 if (!n) {
2077 n = zebra_evpn_neigh_add(zevpn, ipaddr, &mac->macaddr,
2078 mac, 0);
2079 if (!n) {
2080 zlog_warn(
ef7b8be4
DL
2081 "Failed to add Neigh %pIA MAC %pEA VNI %u Remote VTEP %pI4",
2082 ipaddr, &mac->macaddr, zevpn->vni,
2083 &vtep_ip);
036daaca
PR
2084 return;
2085 }
2086
2087 } else {
036daaca
PR
2088 /* When host moves but changes its (MAC,IP)
2089 * binding, BGP may install a MACIP entry that
2090 * corresponds to "older" location of the host
2091 * in transient situations (because {IP1,M1}
2092 * is a different route from {IP1,M2}). Check
2093 * the sequence number and ignore this update
2094 * if appropriate.
2095 */
16de1338
AK
2096
2097 if (!zebra_evpn_neigh_is_bgp_seq_ok(
2098 zevpn, n, &mac->macaddr, seq, false))
036daaca 2099 return;
036daaca
PR
2100 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2101 old_static = zebra_evpn_neigh_is_static(n);
2102 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
2103 zlog_debug(
ef7b8be4
DL
2104 "sync->remote neigh vni %u ip %pIA mac %pEA seq %d f0x%x",
2105 n->zevpn->vni, &n->ip, &n->emac,
036daaca
PR
2106 seq, n->flags);
2107 zebra_evpn_neigh_clear_sync_info(n);
2108 if (IS_ZEBRA_NEIGH_ACTIVE(n))
fb8f609d
AK
2109 zebra_evpn_neigh_send_del_to_client(
2110 zevpn->vni, &n->ip, &n->emac,
2111 n->flags, n->state,
2112 false /*force*/);
036daaca
PR
2113 }
2114 if (memcmp(&n->emac, &mac->macaddr,
2115 sizeof(struct ethaddr))
2116 != 0) {
2117 /* update neigh list for macs */
2118 old_mac =
2119 zebra_evpn_mac_lookup(zevpn, &n->emac);
2120 if (old_mac) {
2121 listnode_delete(old_mac->neigh_list, n);
2122 n->mac = NULL;
2123 zebra_evpn_deref_ip2mac(zevpn, old_mac);
2124 }
2125 n->mac = mac;
2126 listnode_add_sort(mac->neigh_list, n);
2127 memcpy(&n->emac, &mac->macaddr, ETH_ALEN);
2128
2129 /* Check Neigh's curent state is local
2130 * (this is the case where neigh/host has moved
2131 * from L->R) and check previous detction
2132 * started via local learning.
2133 *
2134 * RFC-7432: A PE/VTEP that detects a MAC
2135 * mobilit event via local learning starts
2136 * an M-second timer.
2137 * VTEP-IP or seq. change along is not
2138 * considered for dup. detection.
2139 *
2140 * Mobilty event scenario-B IP-MAC binding
2141 * changed.
2142 */
2143 if ((!CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
2144 && n->dad_count)
2145 do_dad = true;
2146 }
2147 }
2148
2149 /* Set "remote" forwarding info. */
2150 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ALL_LOCAL_FLAGS);
2151 n->r_vtep_ip = vtep_ip;
2152 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
2153
2154 /* Set router flag (R-bit) to this Neighbor entry */
2155 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG))
2156 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2157 else
2158 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2159
2160 /* Check old or new MAC detected as duplicate,
2161 * inherit duplicate flag to this neigh.
2162 */
2163 if (zebra_evpn_ip_inherit_dad_from_mac(zvrf, old_mac, mac, n)) {
2164 flog_warn(
2165 EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
ef7b8be4
DL
2166 "VNI %u: MAC %pEA IP %pIA detected as duplicate during remote update, inherit duplicate from MAC",
2167 zevpn->vni, &mac->macaddr, &n->ip);
036daaca
PR
2168 }
2169
2170 /* Check duplicate address detection for IP */
2171 zebra_evpn_dup_addr_detect_for_neigh(
2172 zvrf, n, n->r_vtep_ip, do_dad, &is_dup_detect, false);
2173 /* Install the entry. */
2174 if (!is_dup_detect)
2175 zebra_evpn_rem_neigh_install(zevpn, n, old_static);
2176 }
2177
036daaca
PR
2178 /* Update seq number. */
2179 n->rem_seq = seq;
2180}
224315f3
PR
2181
2182int zebra_evpn_neigh_gw_macip_add(struct interface *ifp, zebra_evpn_t *zevpn,
2183 struct ipaddr *ip, zebra_mac_t *mac)
2184{
2185 zebra_neigh_t *n;
224315f3 2186
2bdd4461 2187 assert(mac);
224315f3
PR
2188
2189 n = zebra_evpn_neigh_lookup(zevpn, ip);
2190 if (!n) {
2191 n = zebra_evpn_neigh_add(zevpn, ip, &mac->macaddr, mac, 0);
2192 if (!n) {
2193 flog_err(
2194 EC_ZEBRA_MAC_ADD_FAILED,
ef7b8be4
DL
2195 "Failed to add neighbor %pIA MAC %pEA intf %s(%u) -> VNI %u",
2196 ip, &mac->macaddr,
224315f3
PR
2197 ifp->name, ifp->ifindex, zevpn->vni);
2198 return -1;
2199 }
2200 }
2201
2202 /* Set "local" forwarding info. */
2203 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
2204 ZEBRA_NEIGH_SET_ACTIVE(n);
2205 memcpy(&n->emac, &mac->macaddr, ETH_ALEN);
2206 n->ifindex = ifp->ifindex;
2207
2208 /* Only advertise in BGP if the knob is enabled */
2209 if (advertise_gw_macip_enabled(zevpn)) {
2210
2211 SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW);
2212 SET_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW);
2213 /* Set Router flag (R-bit) */
2214 if (ip->ipa_type == IPADDR_V6)
2215 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2216
2217 if (IS_ZEBRA_DEBUG_VXLAN)
2218 zlog_debug(
ef7b8be4 2219 "SVI %s(%u) L2-VNI %u, sending GW MAC %pEA IP %pIA add to BGP with flags 0x%x",
224315f3 2220 ifp->name, ifp->ifindex, zevpn->vni,
ef7b8be4 2221 &mac->macaddr, ip, n->flags);
224315f3
PR
2222
2223 zebra_evpn_neigh_send_add_to_client(
2224 zevpn->vni, ip, &n->emac, n->mac, n->flags, n->loc_seq);
2225 } else if (advertise_svi_macip_enabled(zevpn)) {
2226
2227 SET_FLAG(n->flags, ZEBRA_NEIGH_SVI_IP);
2228 if (IS_ZEBRA_DEBUG_VXLAN)
2229 zlog_debug(
ef7b8be4 2230 "SVI %s(%u) L2-VNI %u, sending SVI MAC %pEA IP %pIA add to BGP with flags 0x%x",
224315f3 2231 ifp->name, ifp->ifindex, zevpn->vni,
ef7b8be4 2232 &mac->macaddr, ip, n->flags);
224315f3
PR
2233
2234 zebra_evpn_neigh_send_add_to_client(
2235 zevpn->vni, ip, &n->emac, n->mac, n->flags, n->loc_seq);
2236 }
2237
2238 return 0;
2239}
32fe7dfd
PR
2240
2241void zebra_evpn_neigh_remote_uninstall(zebra_evpn_t *zevpn,
2242 struct zebra_vrf *zvrf, zebra_neigh_t *n,
2243 zebra_mac_t *mac, struct ipaddr *ipaddr)
2244{
32fe7dfd
PR
2245 if (zvrf->dad_freeze && CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE)
2246 && CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
2247 && (memcmp(n->emac.octet, mac->macaddr.octet, ETH_ALEN) == 0)) {
2248 struct interface *vlan_if;
2249
2250 vlan_if = zevpn_map_to_svi(zevpn);
2251 if (IS_ZEBRA_DEBUG_VXLAN)
2252 zlog_debug(
ef7b8be4
DL
2253 "%s: IP %pIA (flags 0x%x intf %s) is remote and duplicate, read kernel for local entry",
2254 __func__, ipaddr, n->flags,
2255 vlan_if ? vlan_if->name : "Unknown");
32fe7dfd
PR
2256 if (vlan_if)
2257 neigh_read_specific_ip(ipaddr, vlan_if);
2258 }
2259
2260 /* When the MAC changes for an IP, it is possible the
2261 * client may update the new MAC before trying to delete the
2262 * "old" neighbor (as these are two different MACIP routes).
2263 * Do the delete only if the MAC matches.
2264 */
2265 if (!memcmp(n->emac.octet, mac->macaddr.octet, ETH_ALEN)) {
2266 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2267 zebra_evpn_sync_neigh_del(n);
2268 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
2269 zebra_evpn_neigh_uninstall(zevpn, n);
2270 zebra_evpn_neigh_del(zevpn, n);
2271 zebra_evpn_deref_ip2mac(zevpn, mac);
2272 }
2273 }
2274}
33064a62
PR
2275
2276int zebra_evpn_neigh_del_ip(zebra_evpn_t *zevpn, struct ipaddr *ip)
2277{
2278 zebra_neigh_t *n;
2279 zebra_mac_t *zmac;
2280 bool old_bgp_ready;
2281 bool new_bgp_ready;
33064a62
PR
2282 struct zebra_vrf *zvrf;
2283
2284 /* If entry doesn't exist, nothing to do. */
2285 n = zebra_evpn_neigh_lookup(zevpn, ip);
2286 if (!n)
2287 return 0;
2288
2289 zmac = zebra_evpn_mac_lookup(zevpn, &n->emac);
2290 if (!zmac) {
2291 if (IS_ZEBRA_DEBUG_VXLAN)
2292 zlog_debug(
ef7b8be4
DL
2293 "Trying to del a neigh %pIA without a mac %pEA on VNI %u",
2294 ip, &n->emac,
33064a62
PR
2295 zevpn->vni);
2296
2297 return 0;
2298 }
2299
2300 /* If it is a remote entry, the kernel has aged this out or someone has
2301 * deleted it, it needs to be re-installed as Quagga is the owner.
2302 */
2303 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
2304 zebra_evpn_rem_neigh_install(zevpn, n, false /*was_static*/);
2305 return 0;
2306 }
2307
2308 /* if this is a sync entry it cannot be dropped re-install it in
2309 * the dataplane
2310 */
2311 old_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
2312 if (zebra_evpn_neigh_is_static(n)) {
2313 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
ef7b8be4
DL
2314 zlog_debug("re-add sync neigh vni %u ip %pIA mac %pEA 0x%x",
2315 n->zevpn->vni, &n->ip, &n->emac,
33064a62
PR
2316 n->flags);
2317
2318 if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE))
2319 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
2320 /* inform-bgp about change in local-activity if any */
2321 new_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
2322 zebra_evpn_neigh_send_add_del_to_client(n, old_bgp_ready,
2323 new_bgp_ready);
2324
2325 /* re-install the entry in the kernel */
2326 zebra_evpn_sync_neigh_dp_install(n, false /* set_inactive */,
2327 false /* force_clear_static */,
2328 __func__);
2329
2330 return 0;
2331 }
2332
2333 zvrf = vrf_info_lookup(zevpn->vxlan_if->vrf_id);
2334 if (!zvrf) {
2335 zlog_debug("%s: VNI %u vrf lookup failed.", __func__,
2336 zevpn->vni);
2337 return -1;
2338 }
2339
2340 /* In case of feeze action, if local neigh is in duplicate state,
2341 * Mark the Neigh as inactive before sending delete request to BGPd,
2342 * If BGPd has remote entry, it will re-install
2343 */
2344 if (zvrf->dad_freeze && CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
2345 ZEBRA_NEIGH_SET_INACTIVE(n);
2346
2347 /* Remove neighbor from BGP. */
2348 zebra_evpn_neigh_send_del_to_client(zevpn->vni, &n->ip, &n->emac,
2349 n->flags, n->state,
2350 false /* force */);
2351
2352 /* Delete this neighbor entry. */
2353 zebra_evpn_neigh_del(zevpn, n);
2354
2355 /* see if the AUTO mac needs to be deleted */
2356 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_AUTO)
243b74ed 2357 && !zebra_evpn_mac_in_use(zmac))
33064a62
PR
2358 zebra_evpn_mac_del(zevpn, zmac);
2359
2360 return 0;
2361}