]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_evpn_neigh.c
*: Properly use memset() when zeroing
[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{
72de4110 51 const struct zebra_neigh *n = p;
7cbae20a 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{
72de4110
DS
66 const struct zebra_neigh *n1 = p1;
67 const struct zebra_neigh *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
60cda04d 75 return ipaddr_cmp(&n1->ip, &n2->ip) == 0;
6336e12b 76}
6336e12b 77
7cbae20a 78int neigh_list_cmp(void *p1, void *p2)
6336e12b 79{
72de4110
DS
80 const struct zebra_neigh *n1 = p1;
81 const struct zebra_neigh *n2 = p2;
6336e12b 82
60cda04d 83 return ipaddr_cmp(&n1->ip, &n2->ip);
7cbae20a 84}
6336e12b 85
7cbae20a
PR
86struct hash *zebra_neigh_db_create(const char *desc)
87{
da55bcbc 88 return hash_create_size(8, neigh_hash_keymake, neigh_cmp, desc);
6336e12b
PR
89}
90
f6371c34 91uint32_t num_dup_detected_neighs(struct zebra_evpn *zevpn)
6336e12b
PR
92{
93 unsigned int i;
94 uint32_t num_neighs = 0;
95 struct hash *hash;
96 struct hash_bucket *hb;
72de4110 97 struct zebra_neigh *nbr;
6336e12b
PR
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) {
72de4110 104 nbr = (struct zebra_neigh *)hb->data;
6336e12b
PR
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{
72de4110 120 struct zebra_neigh *n;
7cbae20a
PR
121 char buf[INET6_ADDRSTRLEN];
122 struct neigh_walk_ctx *wctx = ctxt;
123 int width;
6336e12b 124
72de4110 125 n = (struct zebra_neigh *)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 */
3198b2b3 136int remote_neigh_count(struct zebra_mac *zmac)
7cbae20a 137{
72de4110 138 struct zebra_neigh *n = NULL;
7cbae20a
PR
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 */
72de4110
DS
153int zebra_evpn_rem_neigh_install(struct zebra_evpn *zevpn,
154 struct zebra_neigh *n, 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{
72de4110 182 struct zebra_neigh *n;
7cbae20a 183 struct neigh_walk_ctx *wctx = ctxt;
6336e12b 184
72de4110 185 n = (struct zebra_neigh *)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{
72de4110
DS
197 const struct zebra_neigh *tmp_n = p;
198 struct zebra_neigh *n;
6336e12b 199
72de4110 200 n = XCALLOC(MTYPE_NEIGH, sizeof(struct zebra_neigh));
7cbae20a 201 *n = *tmp_n;
6336e12b 202
7cbae20a
PR
203 return ((void *)n);
204}
205
72de4110 206static void zebra_evpn_local_neigh_ref_mac(struct zebra_neigh *n,
1a3bd37f 207 const struct ethaddr *macaddr,
3198b2b3 208 struct zebra_mac *mac,
7cbae20a
PR
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 */
72de4110 240static void zebra_evpn_sync_neigh_dp_install(struct zebra_neigh *n,
33064a62
PR
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 */
1a3bd37f
MS
287int zebra_evpn_neigh_send_add_to_client(vni_t vni, const struct ipaddr *ip,
288 const struct ethaddr *macaddr,
3198b2b3
DS
289 struct zebra_mac *zmac,
290 uint32_t neigh_flags, 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
72de4110 340static void zebra_evpn_neigh_send_add_del_to_client(struct zebra_neigh *n,
33064a62
PR
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 */
72de4110 358void zebra_evpn_sync_neigh_static_chg(struct zebra_neigh *n, bool old_n_static,
7cbae20a
PR
359 bool new_n_static, bool defer_n_dp,
360 bool defer_mac_dp, const char *caller)
6336e12b 361{
3198b2b3 362 struct zebra_mac *mac = n->mac;
7cbae20a
PR
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 */
cc9f21da 414static void zebra_evpn_neigh_hold_exp_cb(struct thread *t)
6336e12b 415{
72de4110 416 struct zebra_neigh *n;
7cbae20a
PR
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))
cc9f21da 427 return;
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
PR
451}
452
72de4110 453static inline void zebra_evpn_neigh_start_hold_timer(struct zebra_neigh *n)
6336e12b 454{
7cbae20a
PR
455 if (n->hold_timer)
456 return;
6336e12b 457
7cbae20a 458 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
ef7b8be4
DL
459 zlog_debug("sync-neigh vni %u ip %pIA mac %pEA 0x%x hold start",
460 n->zevpn->vni, &n->ip, &n->emac, n->flags);
7cbae20a
PR
461 thread_add_timer(zrouter.master, zebra_evpn_neigh_hold_exp_cb, n,
462 zmh_info->neigh_hold_time, &n->hold_timer);
463}
6336e12b 464
72de4110 465static void zebra_evpn_local_neigh_deref_mac(struct zebra_neigh *n,
7cbae20a
PR
466 bool send_mac_update)
467{
3198b2b3 468 struct zebra_mac *mac = n->mac;
f6371c34 469 struct zebra_evpn *zevpn = n->zevpn;
7cbae20a
PR
470 bool old_static;
471 bool new_static;
6336e12b 472
7cbae20a
PR
473 n->mac = NULL;
474 if (!mac)
475 return;
6336e12b 476
7cbae20a
PR
477 if ((n->flags & ZEBRA_NEIGH_ALL_PEER_FLAGS) && mac->sync_neigh_cnt) {
478 old_static = zebra_evpn_mac_is_static(mac);
479 --mac->sync_neigh_cnt;
480 new_static = zebra_evpn_mac_is_static(mac);
481 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
6336e12b 482 zlog_debug(
ef7b8be4
DL
483 "sync-neigh deref mac vni %u ip %pIA mac %pEA ref %d",
484 n->zevpn->vni, &n->ip, &n->emac,
7cbae20a
PR
485 mac->sync_neigh_cnt);
486 if ((old_static != new_static) && send_mac_update)
487 /* program the local mac in the kernel */
488 zebra_evpn_sync_mac_dp_install(
489 mac, false /* set_inactive */,
490 false /* force_clear_static */, __func__);
6336e12b
PR
491 }
492
7cbae20a
PR
493 listnode_delete(mac->neigh_list, n);
494 zebra_evpn_deref_ip2mac(zevpn, mac);
6336e12b
PR
495}
496
72de4110
DS
497bool zebra_evpn_neigh_is_bgp_seq_ok(struct zebra_evpn *zevpn,
498 struct zebra_neigh *n,
1a3bd37f 499 const struct ethaddr *macaddr, uint32_t seq,
16de1338 500 bool sync)
6336e12b 501{
7cbae20a 502 uint32_t tmp_seq;
16de1338 503 const char *n_type;
6336e12b 504
16de1338 505 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
7cbae20a 506 tmp_seq = n->loc_seq;
16de1338
AK
507 n_type = "local";
508 } else {
7cbae20a 509 tmp_seq = n->rem_seq;
16de1338
AK
510 n_type = "remote";
511 }
6336e12b 512
7cbae20a
PR
513 if (seq < tmp_seq) {
514 /* if the neigh was never advertised to bgp we must accept
515 * whatever sequence number bgp sends
516 * XXX - check with Vivek
517 */
518 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)
519 && !zebra_evpn_neigh_is_ready_for_bgp(n)) {
16de1338
AK
520 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH
521 || IS_ZEBRA_DEBUG_VXLAN)
7cbae20a 522 zlog_debug(
ef7b8be4 523 "%s-macip accept vni %u %s mac %pEA IP %pIA lower seq %u f 0x%x",
16de1338 524 sync ? "sync" : "remote", zevpn->vni,
ef7b8be4 525 n_type, macaddr, &n->ip,
7cbae20a
PR
526 tmp_seq, n->flags);
527 return true;
528 }
6336e12b 529
16de1338 530 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH || IS_ZEBRA_DEBUG_VXLAN)
7cbae20a 531 zlog_debug(
ef7b8be4 532 "%s-macip ignore vni %u %s mac %pEA IP %pIA as existing has higher seq %u f 0x%x",
16de1338 533 sync ? "sync" : "remote", zevpn->vni, n_type,
ef7b8be4 534 macaddr, &n->ip, tmp_seq, n->flags);
7cbae20a 535 return false;
6336e12b 536 }
7cbae20a
PR
537
538 return true;
6336e12b
PR
539}
540
541/*
7cbae20a 542 * Add neighbor entry.
6336e12b 543 */
72de4110
DS
544static struct zebra_neigh *zebra_evpn_neigh_add(struct zebra_evpn *zevpn,
545 const struct ipaddr *ip,
546 const struct ethaddr *mac,
547 struct zebra_mac *zmac,
548 uint32_t n_flags)
6336e12b 549{
72de4110
DS
550 struct zebra_neigh tmp_n;
551 struct zebra_neigh *n = NULL;
6336e12b 552
6006b807 553 memset(&tmp_n, 0, sizeof(tmp_n));
7cbae20a
PR
554 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
555 n = hash_get(zevpn->neigh_table, &tmp_n, zebra_evpn_neigh_alloc);
6336e12b 556
7cbae20a
PR
557 n->state = ZEBRA_NEIGH_INACTIVE;
558 n->zevpn = zevpn;
559 n->dad_ip_auto_recovery_timer = NULL;
560 n->flags = n_flags;
a05111ba 561 n->uptime = monotime(NULL);
6336e12b 562
7cbae20a
PR
563 if (!zmac)
564 zmac = zebra_evpn_mac_lookup(zevpn, mac);
565 zebra_evpn_local_neigh_ref_mac(n, mac, zmac,
566 false /* send_mac_update */);
6336e12b 567
7cbae20a 568 return n;
6336e12b
PR
569}
570
571/*
7cbae20a 572 * Delete neighbor entry.
6336e12b 573 */
72de4110 574int zebra_evpn_neigh_del(struct zebra_evpn *zevpn, struct zebra_neigh *n)
6336e12b 575{
72de4110 576 struct zebra_neigh *tmp_n;
6336e12b 577
7cbae20a
PR
578 if (n->mac)
579 listnode_delete(n->mac->neigh_list, n);
6336e12b 580
7cbae20a
PR
581 /* Cancel auto recovery */
582 THREAD_OFF(n->dad_ip_auto_recovery_timer);
6336e12b 583
2b9e207e
AK
584 /* Cancel proxy hold timer */
585 zebra_evpn_neigh_stop_hold_timer(n);
586
7cbae20a
PR
587 /* Free the VNI hash entry and allocated memory. */
588 tmp_n = hash_release(zevpn->neigh_table, n);
589 XFREE(MTYPE_NEIGH, tmp_n);
6336e12b
PR
590
591 return 0;
592}
593
72de4110 594void zebra_evpn_sync_neigh_del(struct zebra_neigh *n)
6336e12b 595{
7cbae20a
PR
596 bool old_n_static;
597 bool new_n_static;
6336e12b 598
7cbae20a 599 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
ef7b8be4
DL
600 zlog_debug("sync-neigh del vni %u ip %pIA mac %pEA f 0x%x",
601 n->zevpn->vni, &n->ip, &n->emac, n->flags);
6336e12b 602
7cbae20a
PR
603 old_n_static = zebra_evpn_neigh_is_static(n);
604 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY);
605 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE))
606 zebra_evpn_neigh_start_hold_timer(n);
607 new_n_static = zebra_evpn_neigh_is_static(n);
6336e12b 608
7cbae20a
PR
609 if (old_n_static != new_n_static)
610 zebra_evpn_sync_neigh_static_chg(
611 n, old_n_static, new_n_static, false /*defer-dp*/,
612 false /*defer_mac_dp*/, __func__);
613}
6336e12b 614
72de4110
DS
615struct zebra_neigh *zebra_evpn_proc_sync_neigh_update(
616 struct zebra_evpn *zevpn, struct zebra_neigh *n, uint16_t ipa_len,
617 const struct ipaddr *ipaddr, uint8_t flags, uint32_t seq,
618 const esi_t *esi, struct sync_mac_ip_ctx *ctx)
7cbae20a
PR
619{
620 struct interface *ifp = NULL;
621 bool is_router;
3198b2b3 622 struct zebra_mac *mac = ctx->mac;
7cbae20a
PR
623 uint32_t tmp_seq;
624 bool old_router = false;
625 bool old_bgp_ready = false;
626 bool new_bgp_ready;
627 bool inform_dataplane = false;
628 bool inform_bgp = false;
629 bool old_mac_static;
630 bool new_mac_static;
631 bool set_dp_inactive = false;
7cbae20a
PR
632 bool created;
633 ifindex_t ifindex = 0;
6336e12b 634
7cbae20a
PR
635 /* locate l3-svi */
636 ifp = zevpn_map_to_svi(zevpn);
637 if (ifp)
638 ifindex = ifp->ifindex;
6336e12b 639
7cbae20a
PR
640 is_router = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
641 old_mac_static = zebra_evpn_mac_is_static(mac);
6336e12b 642
7cbae20a
PR
643 if (!n) {
644 uint32_t n_flags = 0;
6336e12b 645
7cbae20a
PR
646 /* New neighbor - create */
647 SET_FLAG(n_flags, ZEBRA_NEIGH_LOCAL);
648 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
649 SET_FLAG(n_flags, ZEBRA_NEIGH_ES_PEER_PROXY);
650 else
651 SET_FLAG(n_flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
652 SET_FLAG(n_flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
6336e12b 653
7cbae20a
PR
654 n = zebra_evpn_neigh_add(zevpn, ipaddr, &mac->macaddr, mac,
655 n_flags);
656 n->ifindex = ifindex;
657 ZEBRA_NEIGH_SET_ACTIVE(n);
6336e12b 658
7cbae20a
PR
659 created = true;
660 inform_dataplane = true;
661 inform_bgp = true;
662 set_dp_inactive = true;
663 } else {
664 bool mac_change;
665 uint32_t old_flags = n->flags;
666 bool old_n_static;
667 bool new_n_static;
6336e12b 668
7cbae20a
PR
669 created = false;
670 old_n_static = zebra_evpn_neigh_is_static(n);
671 old_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
672 old_router = !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
6336e12b 673
7cbae20a
PR
674 mac_change = !!memcmp(&n->emac, &mac->macaddr, ETH_ALEN);
675
676 /* deref and clear old info */
677 if (mac_change) {
678 if (old_bgp_ready) {
679 zebra_evpn_neigh_send_del_to_client(
680 zevpn->vni, &n->ip, &n->emac, n->flags,
681 n->state, false /*force*/);
682 old_bgp_ready = false;
6336e12b 683 }
bc3cd39b
DS
684 zebra_evpn_local_neigh_deref_mac(n,
685 false /*send_mac_update*/);
6336e12b 686 }
7cbae20a
PR
687 /* clear old fwd info */
688 n->rem_seq = 0;
689 n->r_vtep_ip.s_addr = 0;
6336e12b 690
7cbae20a
PR
691 /* setup new flags */
692 n->flags = 0;
693 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
694 /* retain activity flag if the neigh was
695 * previously local
6336e12b 696 */
7cbae20a
PR
697 if (old_flags & ZEBRA_NEIGH_LOCAL) {
698 n->flags |= (old_flags & ZEBRA_NEIGH_LOCAL_INACTIVE);
699 } else {
700 inform_dataplane = true;
701 set_dp_inactive = true;
702 n->flags |= ZEBRA_NEIGH_LOCAL_INACTIVE;
6336e12b
PR
703 }
704
7cbae20a
PR
705 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
706 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY);
707 else
708 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
6336e12b 709
7cbae20a
PR
710 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT)) {
711 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY);
712 /* if the neigh was peer-active previously we
713 * need to keep the flag and start the
714 * holdtimer on it. the peer-active flag is
715 * cleared on holdtimer expiry.
716 */
717 if (CHECK_FLAG(old_flags, ZEBRA_NEIGH_ES_PEER_ACTIVE)) {
718 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
719 zebra_evpn_neigh_start_hold_timer(n);
720 }
721 } else {
722 SET_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE);
723 /* stop hold timer if a peer has verified
724 * reachability
725 */
726 zebra_evpn_neigh_stop_hold_timer(n);
6336e12b 727 }
7cbae20a 728 ZEBRA_NEIGH_SET_ACTIVE(n);
6336e12b 729
7cbae20a 730 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH && (old_flags != n->flags))
6336e12b 731 zlog_debug(
ef7b8be4
DL
732 "sync-neigh vni %u ip %pIA mac %pEA old_f 0x%x new_f 0x%x",
733 n->zevpn->vni, &n->ip, &n->emac,
7cbae20a 734 old_flags, n->flags);
6336e12b 735
7cbae20a
PR
736 new_n_static = zebra_evpn_neigh_is_static(n);
737 if (mac_change) {
738 set_dp_inactive = true;
739 n->flags |= ZEBRA_NEIGH_LOCAL_INACTIVE;
740 inform_dataplane = true;
741 zebra_evpn_local_neigh_ref_mac(
742 n, &mac->macaddr, mac,
743 false /*send_mac_update*/);
744 } else if (old_n_static != new_n_static) {
745 inform_dataplane = true;
746 /* if static flags have changed without a mac change
747 * we need to create the correct sync-refs against
748 * the existing mac
6336e12b 749 */
7cbae20a
PR
750 zebra_evpn_sync_neigh_static_chg(
751 n, old_n_static, new_n_static,
752 true /*defer_dp*/, true /*defer_mac_dp*/,
753 __func__);
6336e12b 754 }
6336e12b 755
7cbae20a
PR
756 /* Update the forwarding info. */
757 if (n->ifindex != ifindex) {
758 n->ifindex = ifindex;
759 inform_dataplane = true;
6336e12b 760 }
a05111ba
DS
761
762 n->uptime = monotime(NULL);
6336e12b
PR
763 }
764
7cbae20a
PR
765 /* update the neigh seq. we don't bother with the mac seq as
766 * sync_mac_update already took care of that
767 */
768 tmp_seq = MAX(n->loc_seq, seq);
769 if (tmp_seq != n->loc_seq) {
770 n->loc_seq = tmp_seq;
771 inform_bgp = true;
772 }
6336e12b 773
7cbae20a
PR
774 /* Mark Router flag (R-bit) */
775 if (is_router)
776 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
777 else
778 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
6336e12b 779
7cbae20a
PR
780 if (old_router != is_router)
781 inform_dataplane = true;
6336e12b 782
7cbae20a
PR
783 new_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
784 if (old_bgp_ready != new_bgp_ready)
785 inform_bgp = true;
6336e12b 786
7cbae20a
PR
787 new_mac_static = zebra_evpn_mac_is_static(mac);
788 if ((old_mac_static != new_mac_static) || ctx->mac_dp_update_deferred)
789 zebra_evpn_sync_mac_dp_install(mac, ctx->mac_inactive,
790 false /* force_clear_static */,
791 __func__);
6336e12b 792
7cbae20a
PR
793 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
794 zlog_debug(
ef7b8be4 795 "sync-neigh %s vni %u ip %pIA mac %pEA if %s(%d) seq %d f 0x%x%s%s",
7cbae20a 796 created ? "created" : "updated", n->zevpn->vni,
ef7b8be4 797 &n->ip, &n->emac,
7cbae20a
PR
798 ifp ? ifp->name : "", ifindex, n->loc_seq, n->flags,
799 inform_bgp ? " inform_bgp" : "",
800 inform_dataplane ? " inform_dp" : "");
6336e12b 801
7cbae20a
PR
802 if (inform_dataplane)
803 zebra_evpn_sync_neigh_dp_install(n, set_dp_inactive,
804 false /* force_clear_static */,
805 __func__);
6336e12b 806
7cbae20a
PR
807 if (inform_bgp)
808 zebra_evpn_neigh_send_add_del_to_client(n, old_bgp_ready,
809 new_bgp_ready);
6336e12b 810
7cbae20a
PR
811 return n;
812}
6336e12b 813
7cbae20a
PR
814/*
815 * Uninstall remote neighbor from the kernel.
816 */
f6371c34 817static int zebra_evpn_neigh_uninstall(struct zebra_evpn *zevpn,
72de4110 818 struct zebra_neigh *n)
7cbae20a
PR
819{
820 struct interface *vlan_if;
6336e12b 821
7cbae20a
PR
822 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
823 return 0;
6336e12b 824
7cbae20a
PR
825 vlan_if = zevpn_map_to_svi(zevpn);
826 if (!vlan_if)
827 return -1;
6336e12b 828
7cbae20a
PR
829 ZEBRA_NEIGH_SET_INACTIVE(n);
830 n->loc_seq = 0;
6336e12b 831
7cbae20a 832 dplane_rem_neigh_delete(vlan_if, &n->ip);
6336e12b
PR
833
834 return 0;
835}
836
7cbae20a
PR
837/*
838 * Free neighbor hash entry (callback)
839 */
840static void zebra_evpn_neigh_del_hash_entry(struct hash_bucket *bucket,
841 void *arg)
6336e12b 842{
7cbae20a 843 struct neigh_walk_ctx *wctx = arg;
72de4110 844 struct zebra_neigh *n = bucket->data;
6336e12b 845
7cbae20a
PR
846 if (((wctx->flags & DEL_LOCAL_NEIGH) && (n->flags & ZEBRA_NEIGH_LOCAL))
847 || ((wctx->flags & DEL_REMOTE_NEIGH)
848 && (n->flags & ZEBRA_NEIGH_REMOTE))
849 || ((wctx->flags & DEL_REMOTE_NEIGH_FROM_VTEP)
850 && (n->flags & ZEBRA_NEIGH_REMOTE)
851 && IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))) {
852 if (wctx->upd_client && (n->flags & ZEBRA_NEIGH_LOCAL))
853 zebra_evpn_neigh_send_del_to_client(
854 wctx->zevpn->vni, &n->ip, &n->emac, n->flags,
855 n->state, false /*force*/);
6336e12b 856
7cbae20a
PR
857 if (wctx->uninstall) {
858 if (zebra_evpn_neigh_is_static(n))
859 zebra_evpn_sync_neigh_dp_install(
860 n, false /* set_inactive */,
861 true /* force_clear_static */,
862 __func__);
863 if ((n->flags & ZEBRA_NEIGH_REMOTE))
864 zebra_evpn_neigh_uninstall(wctx->zevpn, n);
6336e12b
PR
865 }
866
7cbae20a
PR
867 zebra_evpn_neigh_del(wctx->zevpn, n);
868 }
6336e12b 869
7cbae20a
PR
870 return;
871}
6336e12b 872
7cbae20a
PR
873/*
874 * Delete all neighbor entries for this EVPN.
875 */
f6371c34 876void zebra_evpn_neigh_del_all(struct zebra_evpn *zevpn, int uninstall,
7cbae20a
PR
877 int upd_client, uint32_t flags)
878{
879 struct neigh_walk_ctx wctx;
6336e12b 880
7cbae20a
PR
881 if (!zevpn->neigh_table)
882 return;
6336e12b 883
6006b807 884 memset(&wctx, 0, sizeof(wctx));
7cbae20a
PR
885 wctx.zevpn = zevpn;
886 wctx.uninstall = uninstall;
887 wctx.upd_client = upd_client;
888 wctx.flags = flags;
6336e12b 889
7cbae20a
PR
890 hash_iterate(zevpn->neigh_table, zebra_evpn_neigh_del_hash_entry,
891 &wctx);
892}
6336e12b 893
7cbae20a
PR
894/*
895 * Look up neighbor hash entry.
896 */
72de4110
DS
897struct zebra_neigh *zebra_evpn_neigh_lookup(struct zebra_evpn *zevpn,
898 const struct ipaddr *ip)
7cbae20a 899{
72de4110
DS
900 struct zebra_neigh tmp;
901 struct zebra_neigh *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 */
f6371c34 914void zebra_evpn_process_neigh_on_local_mac_change(struct zebra_evpn *zevpn,
3198b2b3 915 struct zebra_mac *zmac,
7cbae20a
PR
916 bool seq_change,
917 bool es_change)
918{
72de4110 919 struct zebra_neigh *n = NULL;
7cbae20a
PR
920 struct listnode *node = NULL;
921 struct zebra_vrf *zvrf = NULL;
6336e12b 922
096f7609 923 zvrf = zevpn->vxlan_if->vrf->info;
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 */
f6371c34 958void zebra_evpn_process_neigh_on_local_mac_del(struct zebra_evpn *zevpn,
3198b2b3 959 struct zebra_mac *zmac)
7cbae20a 960{
72de4110 961 struct zebra_neigh *n = NULL;
7cbae20a 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 */
f6371c34 991void zebra_evpn_process_neigh_on_remote_mac_add(struct zebra_evpn *zevpn,
3198b2b3 992 struct zebra_mac *zmac)
6336e12b 993{
72de4110 994 struct zebra_neigh *n = NULL;
7cbae20a 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 */
f6371c34 1021void zebra_evpn_process_neigh_on_remote_mac_del(struct zebra_evpn *zevpn,
3198b2b3 1022 struct zebra_mac *zmac)
6336e12b 1023{
7cbae20a 1024 /* NOTE: Currently a NO-OP. */
6336e12b
PR
1025}
1026
7cbae20a 1027static inline void zebra_evpn_local_neigh_update_log(
72de4110
DS
1028 const char *pfx, struct zebra_neigh *n, bool is_router,
1029 bool local_inactive, bool old_bgp_ready, bool new_bgp_ready,
1030 bool inform_dataplane, bool inform_bgp, const char *sfx)
7cbae20a 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 1049static int zebra_evpn_ip_inherit_dad_from_mac(struct zebra_vrf *zvrf,
3198b2b3
DS
1050 struct zebra_mac *old_zmac,
1051 struct zebra_mac *new_zmac,
72de4110 1052 struct zebra_neigh *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
cc9f21da 1092static void zebra_evpn_dad_ip_auto_recovery_exp(struct thread *t)
6336e12b 1093{
7cbae20a 1094 struct zebra_vrf *zvrf = NULL;
72de4110 1095 struct zebra_neigh *nbr = NULL;
f6371c34 1096 struct zebra_evpn *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)
cc9f21da 1103 return;
6336e12b 1104
8b5fdf2e 1105 zevpn = zebra_evpn_lookup(nbr->zevpn->vni);
7cbae20a 1106 if (!zevpn)
cc9f21da 1107 return;
6336e12b 1108
7cbae20a
PR
1109 nbr = zebra_evpn_neigh_lookup(zevpn, &nbr->ip);
1110 if (!nbr)
cc9f21da 1111 return;
7cbae20a
PR
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 }
7cbae20a 1135}
6336e12b 1136
72de4110
DS
1137static void zebra_evpn_dup_addr_detect_for_neigh(
1138 struct zebra_vrf *zvrf, struct zebra_neigh *nbr, struct in_addr vtep_ip,
1139 bool do_dad, bool *is_dup_detect, bool is_local)
7cbae20a
PR
1140{
1141
1142 struct timeval elapsed = {0, 0};
7cbae20a 1143 bool reset_params = false;
6336e12b 1144
b2ee2b71 1145 if (!zebra_evpn_do_dup_addr_detect(zvrf))
7cbae20a
PR
1146 return;
1147
1148 /* IP is detected as duplicate or inherit dup
1149 * state, hold on to install as remote entry
1150 * only if freeze is enabled.
1151 */
1152 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
6336e12b
PR
1153 if (IS_ZEBRA_DEBUG_VXLAN)
1154 zlog_debug(
ef7b8be4
DL
1155 "%s: duplicate addr MAC %pEA IP %pIA flags 0x%x skip installing, learn count %u recover time %u",
1156 __func__, &nbr->emac, &nbr->ip,
7cbae20a
PR
1157 nbr->flags, nbr->dad_count,
1158 zvrf->dad_freeze_time);
6336e12b 1159
7cbae20a
PR
1160 if (zvrf->dad_freeze)
1161 *is_dup_detect = true;
6336e12b 1162
7cbae20a
PR
1163 /* warn-only action, neigh will be installed.
1164 * freeze action, it wil not be installed.
6336e12b 1165 */
7cbae20a
PR
1166 return;
1167 }
6336e12b 1168
7cbae20a
PR
1169 if (!do_dad)
1170 return;
6336e12b 1171
7cbae20a
PR
1172 /* Check if detection time (M-secs) expired.
1173 * Reset learn count and detection start time.
1174 * During remote mac add, count should already be 1
1175 * via local learning.
1176 */
1177 monotime_since(&nbr->detect_start_time, &elapsed);
1178 reset_params = (elapsed.tv_sec > zvrf->dad_time);
6336e12b 1179
7cbae20a
PR
1180 if (is_local && !reset_params) {
1181 /* RFC-7432: A PE/VTEP that detects a MAC mobility
1182 * event via LOCAL learning starts an M-second timer.
1183 *
1184 * NOTE: This is the START of the probe with count is
1185 * 0 during LOCAL learn event.
1186 */
1187 reset_params = !nbr->dad_count;
6336e12b
PR
1188 }
1189
7cbae20a
PR
1190 if (reset_params) {
1191 if (IS_ZEBRA_DEBUG_VXLAN)
1192 zlog_debug(
ef7b8be4
DL
1193 "%s: duplicate addr MAC %pEA IP %pIA flags 0x%x detection time passed, reset learn count %u",
1194 __func__, &nbr->emac, &nbr->ip,
7cbae20a
PR
1195 nbr->flags, nbr->dad_count);
1196 /* Reset learn count but do not start detection
1197 * during REMOTE learn event.
1198 */
1199 nbr->dad_count = 0;
1200 /* Start dup. addr detection (DAD) start time,
1201 * ONLY during LOCAL learn.
1202 */
1203 if (is_local)
1204 monotime(&nbr->detect_start_time);
6336e12b 1205
7cbae20a
PR
1206 } else if (!is_local) {
1207 /* For REMOTE IP/Neigh, increment detection count
1208 * ONLY while in probe window, once window passed,
1209 * next local learn event should trigger DAD.
1210 */
1211 nbr->dad_count++;
6336e12b
PR
1212 }
1213
7cbae20a
PR
1214 /* For LOCAL IP/Neigh learn event, once count is reset above via either
1215 * initial/start detection time or passed the probe time, the count
1216 * needs to be incremented.
1217 */
1218 if (is_local)
1219 nbr->dad_count++;
6336e12b 1220
7cbae20a
PR
1221 if (nbr->dad_count >= zvrf->dad_max_moves) {
1222 flog_warn(
1223 EC_ZEBRA_DUP_IP_DETECTED,
ef7b8be4
DL
1224 "VNI %u: MAC %pEA IP %pIA detected as duplicate during %s VTEP %pI4",
1225 nbr->zevpn->vni, &nbr->emac, &nbr->ip,
7cbae20a 1226 is_local ? "local update, last" : "remote update, from",
9bcef951 1227 &vtep_ip);
6336e12b 1228
7cbae20a 1229 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
6336e12b 1230
7cbae20a
PR
1231 /* Capture Duplicate detection time */
1232 nbr->dad_dup_detect_time = monotime(NULL);
6336e12b 1233
7cbae20a
PR
1234 /* Start auto recovery timer for this IP */
1235 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
1236 if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
1237 if (IS_ZEBRA_DEBUG_VXLAN)
1238 zlog_debug(
ef7b8be4
DL
1239 "%s: duplicate addr MAC %pEA IP %pIA flags 0x%x auto recovery time %u start",
1240 __func__, &nbr->emac, &nbr->ip,
7cbae20a 1241 nbr->flags, zvrf->dad_freeze_time);
6336e12b 1242
7cbae20a
PR
1243 thread_add_timer(zrouter.master,
1244 zebra_evpn_dad_ip_auto_recovery_exp,
1245 nbr, zvrf->dad_freeze_time,
1246 &nbr->dad_ip_auto_recovery_timer);
1247 }
1248 if (zvrf->dad_freeze)
1249 *is_dup_detect = true;
1250 }
6336e12b
PR
1251}
1252
f6371c34
DS
1253int zebra_evpn_local_neigh_update(struct zebra_evpn *zevpn,
1254 struct interface *ifp,
1a3bd37f
MS
1255 const struct ipaddr *ip,
1256 const struct ethaddr *macaddr, bool is_router,
1257 bool local_inactive, bool dp_static)
6336e12b 1258{
7cbae20a 1259 struct zebra_vrf *zvrf;
72de4110 1260 struct zebra_neigh *n = NULL;
3198b2b3 1261 struct zebra_mac *zmac = NULL, *old_zmac = NULL;
7cbae20a
PR
1262 uint32_t old_mac_seq = 0, mac_new_seq = 0;
1263 bool upd_mac_seq = false;
1264 bool neigh_mac_change = false;
1265 bool neigh_on_hold = false;
1266 bool neigh_was_remote = false;
1267 bool do_dad = false;
1268 struct in_addr vtep_ip = {.s_addr = 0};
1269 bool inform_dataplane = false;
1270 bool created = false;
1271 bool new_static = false;
1272 bool old_bgp_ready = false;
1273 bool new_bgp_ready;
6336e12b 1274
7cbae20a
PR
1275 /* Check if the MAC exists. */
1276 zmac = zebra_evpn_mac_lookup(zevpn, macaddr);
1277 if (!zmac) {
1278 /* create a dummy MAC if the MAC is not already present */
6336e12b 1279 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
1280 zlog_debug("AUTO MAC %pEA created for neigh %pIA on VNI %u",
1281 macaddr, ip, zevpn->vni);
6336e12b 1282
7cbae20a 1283 zmac = zebra_evpn_mac_add(zevpn, macaddr);
8b07f173 1284 zebra_evpn_mac_clear_fwd_info(zmac);
7cbae20a
PR
1285 memset(&zmac->flags, 0, sizeof(uint32_t));
1286 SET_FLAG(zmac->flags, ZEBRA_MAC_AUTO);
1287 } else {
1288 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
1289 /*
1290 * We don't change the MAC to local upon a neighbor
1291 * learn event, we wait for the explicit local MAC
1292 * learn. However, we have to compute its sequence
1293 * number in preparation for when it actually turns
1294 * local.
1295 */
1296 upd_mac_seq = true;
1297 }
1298 }
6336e12b 1299
096f7609 1300 zvrf = zevpn->vxlan_if->vrf->info;
7cbae20a
PR
1301 if (!zvrf) {
1302 if (IS_ZEBRA_DEBUG_VXLAN)
1303 zlog_debug(" Unable to find vrf for: %d",
096f7609 1304 zevpn->vxlan_if->vrf->vrf_id);
7cbae20a
PR
1305 return -1;
1306 }
6336e12b 1307
7cbae20a
PR
1308 /* Check if the neighbor exists. */
1309 n = zebra_evpn_neigh_lookup(zevpn, ip);
1310 if (!n) {
1311 /* New neighbor - create */
1312 n = zebra_evpn_neigh_add(zevpn, ip, macaddr, zmac, 0);
97511d01 1313
7cbae20a
PR
1314 /* Set "local" forwarding info. */
1315 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
1316 n->ifindex = ifp->ifindex;
1317 created = true;
6336e12b 1318 } else {
7cbae20a
PR
1319 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
1320 bool mac_different;
1321 bool cur_is_router;
1322 bool old_local_inactive;
6336e12b 1323
7cbae20a
PR
1324 old_local_inactive = !!CHECK_FLAG(
1325 n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
6336e12b 1326
7cbae20a 1327 old_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
6336e12b 1328
7cbae20a
PR
1329 /* Note any changes and see if of interest to BGP. */
1330 mac_different = !!memcmp(&n->emac, macaddr, ETH_ALEN);
1331 cur_is_router =
1332 !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
1333 new_static = zebra_evpn_neigh_is_static(n);
1334 if (!mac_different && is_router == cur_is_router
1335 && old_local_inactive == local_inactive
1336 && dp_static != new_static) {
1337 if (IS_ZEBRA_DEBUG_VXLAN)
1338 zlog_debug(
1339 " Ignoring entry mac is the same and is_router == cur_is_router");
1340 n->ifindex = ifp->ifindex;
1341 return 0;
1342 }
6336e12b 1343
7cbae20a
PR
1344 old_zmac = n->mac;
1345 if (!mac_different) {
1346 /* XXX - cleanup this code duplication */
1347 bool is_neigh_freezed = false;
6336e12b 1348
7cbae20a
PR
1349 /* Only the router flag has changed. */
1350 if (is_router)
1351 SET_FLAG(n->flags,
1352 ZEBRA_NEIGH_ROUTER_FLAG);
1353 else
1354 UNSET_FLAG(n->flags,
1355 ZEBRA_NEIGH_ROUTER_FLAG);
6336e12b 1356
7cbae20a
PR
1357 if (local_inactive)
1358 SET_FLAG(n->flags,
1359 ZEBRA_NEIGH_LOCAL_INACTIVE);
1360 else
1361 UNSET_FLAG(n->flags,
1362 ZEBRA_NEIGH_LOCAL_INACTIVE);
1363 new_bgp_ready =
1364 zebra_evpn_neigh_is_ready_for_bgp(n);
6336e12b 1365
7c0e4dc6
AK
1366 if (dp_static != new_static)
1367 inform_dataplane = true;
1368
7cbae20a
PR
1369 /* Neigh is in freeze state and freeze action
1370 * is enabled, do not send update to client.
1371 */
1372 is_neigh_freezed =
b2ee2b71 1373 (zebra_evpn_do_dup_addr_detect(zvrf)
7cbae20a
PR
1374 && zvrf->dad_freeze
1375 && CHECK_FLAG(n->flags,
1376 ZEBRA_NEIGH_DUPLICATE));
6336e12b 1377
7cbae20a
PR
1378 zebra_evpn_local_neigh_update_log(
1379 "local", n, is_router, local_inactive,
1380 old_bgp_ready, new_bgp_ready, false,
1381 false, "flag-update");
6336e12b 1382
7c0e4dc6
AK
1383 if (inform_dataplane)
1384 zebra_evpn_sync_neigh_dp_install(
1385 n, false /* set_inactive */,
1386 false /* force_clear_static */,
1387 __func__);
1388
7cbae20a
PR
1389 /* if the neigh can no longer be advertised
1390 * remove it from bgp
1391 */
1392 if (!is_neigh_freezed) {
1393 zebra_evpn_neigh_send_add_del_to_client(
1394 n, old_bgp_ready,
1395 new_bgp_ready);
1396 } else {
1397 if (IS_ZEBRA_DEBUG_VXLAN
1398 && IS_ZEBRA_NEIGH_ACTIVE(n))
1399 zlog_debug(
1400 " Neighbor active and frozen");
1401 }
1402 return 0;
1403 }
6336e12b 1404
7cbae20a
PR
1405 /* The MAC has changed, need to issue a delete
1406 * first as this means a different MACIP route.
1407 * Also, need to do some unlinking/relinking.
1408 * We also need to update the MAC's sequence number
1409 * in different situations.
1410 */
1411 if (old_bgp_ready) {
1412 zebra_evpn_neigh_send_del_to_client(
1413 zevpn->vni, &n->ip, &n->emac, n->flags,
1414 n->state, false /*force*/);
1415 old_bgp_ready = false;
1416 }
1417 if (old_zmac) {
1418 old_mac_seq = CHECK_FLAG(old_zmac->flags,
1419 ZEBRA_MAC_REMOTE)
1420 ? old_zmac->rem_seq
1421 : old_zmac->loc_seq;
1422 neigh_mac_change = upd_mac_seq = true;
1423 zebra_evpn_local_neigh_deref_mac(
1424 n, true /* send_mac_update */);
1425 }
6336e12b 1426
7cbae20a
PR
1427 /* if mac changes abandon peer flags and tell
1428 * dataplane to clear the static flag
1429 */
1430 if (zebra_evpn_neigh_clear_sync_info(n))
1431 inform_dataplane = true;
1432 /* Update the forwarding info. */
1433 n->ifindex = ifp->ifindex;
1434
1435 /* Link to new MAC */
1436 zebra_evpn_local_neigh_ref_mac(
1437 n, macaddr, zmac, true /* send_mac_update */);
1438 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
1439 /*
1440 * Neighbor has moved from remote to local. Its
1441 * MAC could have also changed as part of the move.
1442 */
1443 if (memcmp(n->emac.octet, macaddr->octet, ETH_ALEN)
1444 != 0) {
1445 old_zmac = n->mac;
1446 if (old_zmac) {
1447 old_mac_seq =
1448 CHECK_FLAG(old_zmac->flags,
1449 ZEBRA_MAC_REMOTE)
1450 ? old_zmac->rem_seq
1451 : old_zmac->loc_seq;
1452 neigh_mac_change = upd_mac_seq = true;
1453 zebra_evpn_local_neigh_deref_mac(
1454 n, true /* send_update */);
1455 }
6336e12b 1456
7cbae20a
PR
1457 /* Link to new MAC */
1458 zebra_evpn_local_neigh_ref_mac(
1459 n, macaddr, zmac, true /*send_update*/);
1460 }
1461 /* Based on Mobility event Scenario-B from the
1462 * draft, neigh's previous state was remote treat this
1463 * event for DAD.
1464 */
1465 neigh_was_remote = true;
1466 vtep_ip = n->r_vtep_ip;
1467 /* Mark appropriately */
1468 UNSET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
1469 n->r_vtep_ip.s_addr = INADDR_ANY;
1470 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
1471 n->ifindex = ifp->ifindex;
6336e12b
PR
1472 }
1473 }
1474
7cbae20a
PR
1475 /* If MAC was previously remote, or the neighbor had a different
1476 * MAC earlier, recompute the sequence number.
1477 */
1478 if (upd_mac_seq) {
1479 uint32_t seq1, seq2;
6336e12b 1480
7cbae20a
PR
1481 seq1 = CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)
1482 ? zmac->rem_seq + 1
1483 : zmac->loc_seq;
1484 seq2 = neigh_mac_change ? old_mac_seq + 1 : 0;
1485 mac_new_seq = zmac->loc_seq < MAX(seq1, seq2) ? MAX(seq1, seq2)
1486 : zmac->loc_seq;
1487 }
6336e12b 1488
7cbae20a
PR
1489 if (local_inactive)
1490 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
1491 else
1492 UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
1493
1494 /* Mark Router flag (R-bit) */
1495 if (is_router)
1496 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
1497 else
1498 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
1499
7c0e4dc6
AK
1500 /* if zebra and dataplane don't agree this is a sync entry
1501 * re-install in the dataplane */
1502 new_static = zebra_evpn_neigh_is_static(n);
1503 if (dp_static != new_static)
1504 inform_dataplane = true;
7cbae20a
PR
1505
1506 /* Check old and/or new MAC detected as duplicate mark
1507 * the neigh as duplicate
1508 */
1509 if (zebra_evpn_ip_inherit_dad_from_mac(zvrf, old_zmac, zmac, n)) {
1510 flog_warn(
1511 EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
ef7b8be4
DL
1512 "VNI %u: MAC %pEA IP %pIA detected as duplicate during local update, inherit duplicate from MAC",
1513 zevpn->vni, macaddr, &n->ip);
7cbae20a 1514 }
6336e12b 1515
7cbae20a
PR
1516 /* For IP Duplicate Address Detection (DAD) is trigger,
1517 * when the event is extended mobility based on scenario-B
1518 * from the draft, IP/Neigh's MAC binding changed and
1519 * neigh's previous state was remote.
1520 */
1521 if (neigh_mac_change && neigh_was_remote)
1522 do_dad = true;
6336e12b 1523
7cbae20a
PR
1524 zebra_evpn_dup_addr_detect_for_neigh(zvrf, n, vtep_ip, do_dad,
1525 &neigh_on_hold, true);
6336e12b 1526
7cbae20a
PR
1527 if (inform_dataplane)
1528 zebra_evpn_sync_neigh_dp_install(n, false /* set_inactive */,
1529 false /* force_clear_static */,
1530 __func__);
6336e12b 1531
7cbae20a
PR
1532 /* Before we program this in BGP, we need to check if MAC is locally
1533 * learnt. If not, force neighbor to be inactive and reset its seq.
1534 */
1535 if (!CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL)) {
1536 zebra_evpn_local_neigh_update_log(
1537 "local", n, is_router, local_inactive, false, false,
1538 inform_dataplane, false, "auto-mac");
1539 ZEBRA_NEIGH_SET_INACTIVE(n);
1540 n->loc_seq = 0;
1541 zmac->loc_seq = mac_new_seq;
1542 return 0;
1543 }
6336e12b 1544
7cbae20a
PR
1545 zebra_evpn_local_neigh_update_log("local", n, is_router, local_inactive,
1546 false, false, inform_dataplane, true,
1547 created ? "created" : "updated");
6336e12b 1548
7cbae20a
PR
1549 /* If the MAC's sequence number has changed, inform the MAC and all
1550 * neighbors associated with the MAC to BGP, else just inform this
1551 * neighbor.
1552 */
1553 if (upd_mac_seq && zmac->loc_seq != mac_new_seq) {
1554 if (IS_ZEBRA_DEBUG_VXLAN)
1555 zlog_debug(
ef7b8be4
DL
1556 "Seq changed for MAC %pEA VNI %u - old %u new %u",
1557 macaddr, zevpn->vni,
1558 zmac->loc_seq, mac_new_seq);
7cbae20a
PR
1559 zmac->loc_seq = mac_new_seq;
1560 if (zebra_evpn_mac_send_add_to_client(zevpn->vni, macaddr,
1561 zmac->flags,
1562 zmac->loc_seq, zmac->es))
1563 return -1;
1564 zebra_evpn_process_neigh_on_local_mac_change(zevpn, zmac, 1,
1565 0 /*es_change*/);
1566 return 0;
1567 }
6336e12b 1568
7cbae20a 1569 n->loc_seq = zmac->loc_seq;
6336e12b 1570
7cbae20a
PR
1571 if (!neigh_on_hold) {
1572 ZEBRA_NEIGH_SET_ACTIVE(n);
1573 new_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
1574 zebra_evpn_neigh_send_add_del_to_client(n, old_bgp_ready,
1575 new_bgp_ready);
1576 } else {
1577 if (IS_ZEBRA_DEBUG_VXLAN)
1578 zlog_debug(" Neighbor on hold not sending");
1579 }
1580 return 0;
1581}
6336e12b 1582
f6371c34
DS
1583int zebra_evpn_remote_neigh_update(struct zebra_evpn *zevpn,
1584 struct interface *ifp,
1a3bd37f
MS
1585 const struct ipaddr *ip,
1586 const struct ethaddr *macaddr,
7cbae20a
PR
1587 uint16_t state)
1588{
72de4110 1589 struct zebra_neigh *n = NULL;
3198b2b3 1590 struct zebra_mac *zmac = NULL;
6336e12b 1591
7cbae20a
PR
1592 /* If the neighbor is unknown, there is no further action. */
1593 n = zebra_evpn_neigh_lookup(zevpn, ip);
1594 if (!n)
1595 return 0;
6336e12b 1596
7cbae20a
PR
1597 /* If a remote entry, see if it needs to be refreshed */
1598 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
1599#ifdef GNU_LINUX
1600 if (state & NUD_STALE)
1601 zebra_evpn_rem_neigh_install(zevpn, n,
1602 false /*was_static*/);
1603#endif
6336e12b 1604 } else {
7cbae20a
PR
1605 /* We got a "remote" neighbor notification for an entry
1606 * we think is local. This can happen in a multihoming
1607 * scenario - but only if the MAC is already "remote".
1608 * Just mark our entry as "remote".
6336e12b 1609 */
7cbae20a
PR
1610 zmac = zebra_evpn_mac_lookup(zevpn, macaddr);
1611 if (!zmac || !CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
1612 zlog_debug(
ef7b8be4
DL
1613 "Ignore remote neigh %pIA (MAC %pEA) on L2-VNI %u - MAC unknown or local",
1614 &n->ip, macaddr, zevpn->vni);
7cbae20a
PR
1615 return -1;
1616 }
6336e12b 1617
7cbae20a
PR
1618 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ALL_LOCAL_FLAGS);
1619 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
1620 ZEBRA_NEIGH_SET_ACTIVE(n);
1621 n->r_vtep_ip = zmac->fwd_info.r_vtep_ip;
6336e12b
PR
1622 }
1623
7cbae20a 1624 return 0;
6336e12b
PR
1625}
1626
7cbae20a
PR
1627/* Notify Neighbor entries to the Client, skips the GW entry */
1628static void
1629zebra_evpn_send_neigh_hash_entry_to_client(struct hash_bucket *bucket,
1630 void *arg)
6336e12b 1631{
7cbae20a 1632 struct mac_walk_ctx *wctx = arg;
72de4110 1633 struct zebra_neigh *zn = bucket->data;
3198b2b3 1634 struct zebra_mac *zmac = NULL;
6336e12b 1635
7cbae20a 1636 if (CHECK_FLAG(zn->flags, ZEBRA_NEIGH_DEF_GW))
6336e12b 1637 return;
6336e12b 1638
7cbae20a
PR
1639 if (CHECK_FLAG(zn->flags, ZEBRA_NEIGH_LOCAL)
1640 && IS_ZEBRA_NEIGH_ACTIVE(zn)) {
1641 zmac = zebra_evpn_mac_lookup(wctx->zevpn, &zn->emac);
1642 if (!zmac)
1643 return;
6336e12b 1644
7cbae20a
PR
1645 zebra_evpn_neigh_send_add_to_client(wctx->zevpn->vni, &zn->ip,
1646 &zn->emac, zn->mac,
1647 zn->flags, zn->loc_seq);
1648 }
6336e12b
PR
1649}
1650
7cbae20a 1651/* Iterator of a specific EVPN */
f6371c34 1652void zebra_evpn_send_neigh_to_client(struct zebra_evpn *zevpn)
6336e12b 1653{
7cbae20a 1654 struct neigh_walk_ctx wctx;
6336e12b 1655
6006b807 1656 memset(&wctx, 0, sizeof(wctx));
7cbae20a 1657 wctx.zevpn = zevpn;
6336e12b 1658
7cbae20a
PR
1659 hash_iterate(zevpn->neigh_table,
1660 zebra_evpn_send_neigh_hash_entry_to_client, &wctx);
6336e12b
PR
1661}
1662
7cbae20a 1663void zebra_evpn_clear_dup_neigh_hash(struct hash_bucket *bucket, void *ctxt)
6336e12b 1664{
7cbae20a 1665 struct neigh_walk_ctx *wctx = ctxt;
72de4110 1666 struct zebra_neigh *nbr;
f6371c34 1667 struct zebra_evpn *zevpn;
7cbae20a 1668 char buf[INET6_ADDRSTRLEN];
6336e12b 1669
72de4110 1670 nbr = (struct zebra_neigh *)bucket->data;
7cbae20a
PR
1671 if (!nbr)
1672 return;
6336e12b 1673
7cbae20a 1674 zevpn = wctx->zevpn;
6336e12b 1675
7cbae20a
PR
1676 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
1677 return;
6336e12b 1678
7cbae20a
PR
1679 if (IS_ZEBRA_DEBUG_VXLAN) {
1680 ipaddr2str(&nbr->ip, buf, sizeof(buf));
1681 zlog_debug("%s: clear neigh %s dup state, flags 0x%x seq %u",
1682 __func__, buf, nbr->flags, nbr->loc_seq);
1683 }
6336e12b
PR
1684
1685 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
1686 nbr->dad_count = 0;
1687 nbr->detect_start_time.tv_sec = 0;
1688 nbr->detect_start_time.tv_usec = 0;
1689 nbr->dad_dup_detect_time = 0;
7cbae20a 1690 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
6336e12b 1691
6336e12b 1692 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
7cbae20a
PR
1693 zebra_evpn_neigh_send_add_to_client(zevpn->vni, &nbr->ip,
1694 &nbr->emac, nbr->mac,
1695 nbr->flags, nbr->loc_seq);
1696 } else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
6336e12b
PR
1697 zebra_evpn_rem_neigh_install(zevpn, nbr, false /*was_static*/);
1698 }
6336e12b
PR
1699}
1700
7cbae20a
PR
1701/*
1702 * Print a specific neighbor entry.
1703 */
72de4110
DS
1704void zebra_evpn_print_neigh(struct zebra_neigh *n, void *ctxt,
1705 json_object *json)
6336e12b 1706{
7cbae20a
PR
1707 struct vty *vty;
1708 char buf1[ETHER_ADDR_STRLEN];
1709 char buf2[INET6_ADDRSTRLEN];
1710 const char *type_str;
1711 const char *state_str;
1712 bool flags_present = false;
1713 struct zebra_vrf *zvrf = NULL;
1714 struct timeval detect_start_time = {0, 0};
1715 char timebuf[MONOTIME_STRLEN];
1716 char thread_buf[THREAD_TIMER_STRLEN];
a05111ba
DS
1717 time_t uptime;
1718 char up_str[MONOTIME_STRLEN];
6336e12b 1719
7cbae20a
PR
1720 zvrf = zebra_vrf_get_evpn();
1721 if (!zvrf)
1722 return;
6336e12b 1723
a05111ba
DS
1724 uptime = monotime(NULL);
1725 uptime -= n->uptime;
1726
1727 frrtime_to_interval(uptime, up_str, sizeof(up_str));
1728
7cbae20a
PR
1729 ipaddr2str(&n->ip, buf2, sizeof(buf2));
1730 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
1731 type_str = CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) ? "local" : "remote";
1732 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
1733 vty = (struct vty *)ctxt;
1734 if (json == NULL) {
1735 bool sync_info = false;
6336e12b 1736
7cbae20a
PR
1737 vty_out(vty, "IP: %s\n",
1738 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
1739 vty_out(vty, " Type: %s\n", type_str);
1740 vty_out(vty, " State: %s\n", state_str);
a05111ba 1741 vty_out(vty, " Uptime: %s\n", up_str);
7cbae20a
PR
1742 vty_out(vty, " MAC: %s\n",
1743 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
1744 vty_out(vty, " Sync-info:");
1745 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE)) {
1746 vty_out(vty, " local-inactive");
1747 sync_info = true;
1748 }
1749 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY)) {
1750 vty_out(vty, " peer-proxy");
1751 sync_info = true;
1752 }
1753 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE)) {
1754 vty_out(vty, " peer-active");
1755 sync_info = true;
1756 }
1757 if (n->hold_timer) {
1758 vty_out(vty, " (ht: %s)",
1759 thread_timer_to_hhmmss(thread_buf,
1760 sizeof(thread_buf),
1761 n->hold_timer));
1762 sync_info = true;
1763 }
1764 if (!sync_info)
1765 vty_out(vty, " -");
1766 vty_out(vty, "\n");
1767 } else {
a05111ba 1768 json_object_string_add(json, "uptime", up_str);
7cbae20a
PR
1769 json_object_string_add(json, "ip", buf2);
1770 json_object_string_add(json, "type", type_str);
1771 json_object_string_add(json, "state", state_str);
1772 json_object_string_add(json, "mac", buf1);
1773 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE))
1774 json_object_boolean_true_add(json, "localInactive");
1775 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_PROXY))
1776 json_object_boolean_true_add(json, "peerProxy");
1777 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ES_PEER_ACTIVE))
1778 json_object_boolean_true_add(json, "peerActive");
1779 if (n->hold_timer)
1780 json_object_string_add(
1781 json, "peerActiveHold",
1782 thread_timer_to_hhmmss(thread_buf,
1783 sizeof(thread_buf),
1784 n->hold_timer));
6336e12b 1785 }
7cbae20a
PR
1786 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
1787 if (n->mac->es) {
1788 if (json)
1789 json_object_string_add(json, "remoteEs",
1790 n->mac->es->esi_str);
1791 else
1792 vty_out(vty, " Remote ES: %s\n",
1793 n->mac->es->esi_str);
1794 } else {
1795 if (json)
08edf9c6
DA
1796 json_object_string_addf(json, "remoteVtep",
1797 "%pI4", &n->r_vtep_ip);
7cbae20a 1798 else
9bcef951
MS
1799 vty_out(vty, " Remote VTEP: %pI4\n",
1800 &n->r_vtep_ip);
7cbae20a 1801 }
6336e12b 1802 }
7cbae20a
PR
1803 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW)) {
1804 if (!json) {
1805 vty_out(vty, " Flags: Default-gateway");
1806 flags_present = true;
1807 } else
1808 json_object_boolean_true_add(json, "defaultGateway");
6336e12b 1809 }
7cbae20a
PR
1810 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)) {
1811 if (!json) {
1812 vty_out(vty,
1813 flags_present ? " ,Router" : " Flags: Router");
1814 flags_present = true;
1815 }
1816 }
1817 if (json == NULL) {
1818 if (flags_present)
1819 vty_out(vty, "\n");
1820 vty_out(vty, " Local Seq: %u Remote Seq: %u\n", n->loc_seq,
1821 n->rem_seq);
6336e12b 1822
7cbae20a
PR
1823 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE)) {
1824 vty_out(vty, " Duplicate, detected at %s",
1825 time_to_string(n->dad_dup_detect_time,
1826 timebuf));
1827 } else if (n->dad_count) {
1828 monotime_since(&n->detect_start_time,
1829 &detect_start_time);
1830 if (detect_start_time.tv_sec <= zvrf->dad_time) {
1831 time_to_string(n->detect_start_time.tv_sec,
1832 timebuf);
1833 vty_out(vty,
1834 " Duplicate detection started at %s, detection count %u\n",
1835 timebuf, n->dad_count);
1836 }
1837 }
1838 } else {
1839 json_object_int_add(json, "localSequence", n->loc_seq);
1840 json_object_int_add(json, "remoteSequence", n->rem_seq);
1841 json_object_int_add(json, "detectionCount", n->dad_count);
1842 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
1843 json_object_boolean_true_add(json, "isDuplicate");
1844 else
1845 json_object_boolean_false_add(json, "isDuplicate");
1846 }
6336e12b
PR
1847}
1848
7cbae20a 1849void zebra_evpn_print_neigh_hdr(struct vty *vty, struct neigh_walk_ctx *wctx)
6336e12b 1850{
7cbae20a
PR
1851 vty_out(vty, "Flags: I=local-inactive, P=peer-active, X=peer-proxy\n");
1852 vty_out(vty, "%*s %-6s %-5s %-8s %-17s %-30s %s\n", -wctx->addr_width,
1853 "Neighbor", "Type", "Flags", "State", "MAC", "Remote ES/VTEP",
1854 "Seq #'s");
6336e12b
PR
1855}
1856
72de4110
DS
1857static char *zebra_evpn_print_neigh_flags(struct zebra_neigh *n,
1858 char *flags_buf,
1859 uint32_t flags_buf_sz)
6336e12b 1860{
7cbae20a
PR
1861 snprintf(flags_buf, flags_buf_sz, "%s%s%s",
1862 (n->flags & ZEBRA_NEIGH_ES_PEER_ACTIVE) ?
1863 "P" : "",
1864 (n->flags & ZEBRA_NEIGH_ES_PEER_PROXY) ?
1865 "X" : "",
1866 (n->flags & ZEBRA_NEIGH_LOCAL_INACTIVE) ?
1867 "I" : "");
6336e12b 1868
7cbae20a 1869 return flags_buf;
6336e12b
PR
1870}
1871
7cbae20a
PR
1872/*
1873 * Print neighbor hash entry - called for display of all neighbors.
1874 */
1875void zebra_evpn_print_neigh_hash(struct hash_bucket *bucket, void *ctxt)
6336e12b 1876{
7cbae20a
PR
1877 struct vty *vty;
1878 json_object *json_evpn = NULL, *json_row = NULL;
72de4110 1879 struct zebra_neigh *n;
7cbae20a
PR
1880 char buf1[ETHER_ADDR_STRLEN];
1881 char buf2[INET6_ADDRSTRLEN];
9bcef951 1882 char addr_buf[PREFIX_STRLEN];
7cbae20a
PR
1883 struct neigh_walk_ctx *wctx = ctxt;
1884 const char *state_str;
1885 char flags_buf[6];
6336e12b 1886
7cbae20a
PR
1887 vty = wctx->vty;
1888 json_evpn = wctx->json;
72de4110 1889 n = (struct zebra_neigh *)bucket->data;
6336e12b 1890
7cbae20a
PR
1891 if (json_evpn)
1892 json_row = json_object_new_object();
6336e12b 1893
7cbae20a
PR
1894 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
1895 ipaddr2str(&n->ip, buf2, sizeof(buf2));
1896 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
1897 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
1898 if (wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
1899 return;
6336e12b 1900
7cbae20a
PR
1901 if (json_evpn == NULL) {
1902 vty_out(vty, "%*s %-6s %-5s %-8s %-17s %-30s %u/%u\n",
1903 -wctx->addr_width, buf2, "local",
1904 zebra_evpn_print_neigh_flags(n, flags_buf,
1905 sizeof(flags_buf)), state_str, buf1,
1906 "", n->loc_seq, n->rem_seq);
1907 } else {
1908 json_object_string_add(json_row, "type", "local");
1909 json_object_string_add(json_row, "state", state_str);
1910 json_object_string_add(json_row, "mac", buf1);
1911 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
1912 json_object_boolean_true_add(json_row,
1913 "defaultGateway");
1914 json_object_int_add(json_row, "localSequence",
1915 n->loc_seq);
1916 json_object_int_add(json_row, "remoteSequence",
1917 n->rem_seq);
1918 json_object_int_add(json_row, "detectionCount",
1919 n->dad_count);
1920 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
1921 json_object_boolean_true_add(json_row,
1922 "isDuplicate");
1923 else
1924 json_object_boolean_false_add(json_row,
1925 "isDuplicate");
1926 }
1927 wctx->count++;
1928 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
1929 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
1930 && !IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))
6336e12b
PR
1931 return;
1932
7cbae20a
PR
1933 if (json_evpn == NULL) {
1934 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
1935 && (wctx->count == 0))
1936 zebra_evpn_print_neigh_hdr(vty, wctx);
9bcef951
MS
1937
1938 if (n->mac->es == NULL)
1939 inet_ntop(AF_INET, &n->r_vtep_ip,
1940 addr_buf, sizeof(addr_buf));
1941
7cbae20a
PR
1942 vty_out(vty, "%*s %-6s %-5s %-8s %-17s %-30s %u/%u\n",
1943 -wctx->addr_width, buf2, "remote",
1944 zebra_evpn_print_neigh_flags(n, flags_buf,
1945 sizeof(flags_buf)), state_str, buf1,
9bcef951 1946 n->mac->es ? n->mac->es->esi_str : addr_buf,
7cbae20a
PR
1947 n->loc_seq, n->rem_seq);
1948 } else {
1949 json_object_string_add(json_row, "type", "remote");
1950 json_object_string_add(json_row, "state", state_str);
1951 json_object_string_add(json_row, "mac", buf1);
1952 if (n->mac->es)
1953 json_object_string_add(json_row, "remoteEs",
1954 n->mac->es->esi_str);
1955 else
08edf9c6
DA
1956 json_object_string_addf(json_row, "remoteVtep",
1957 "%pI4", &n->r_vtep_ip);
7cbae20a
PR
1958 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
1959 json_object_boolean_true_add(json_row,
1960 "defaultGateway");
1961 json_object_int_add(json_row, "localSequence",
1962 n->loc_seq);
1963 json_object_int_add(json_row, "remoteSequence",
1964 n->rem_seq);
1965 json_object_int_add(json_row, "detectionCount",
1966 n->dad_count);
1967 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
1968 json_object_boolean_true_add(json_row,
1969 "isDuplicate");
1970 else
1971 json_object_boolean_false_add(json_row,
1972 "isDuplicate");
1973 }
1974 wctx->count++;
6336e12b 1975 }
6336e12b 1976
7cbae20a
PR
1977 if (json_evpn)
1978 json_object_object_add(json_evpn, buf2, json_row);
6336e12b
PR
1979}
1980
7cbae20a
PR
1981/*
1982 * Print neighbor hash entry in detail - called for display of all neighbors.
1983 */
1984void zebra_evpn_print_neigh_hash_detail(struct hash_bucket *bucket, void *ctxt)
6336e12b 1985{
7cbae20a
PR
1986 struct vty *vty;
1987 json_object *json_evpn = NULL, *json_row = NULL;
72de4110 1988 struct zebra_neigh *n;
7cbae20a
PR
1989 char buf[INET6_ADDRSTRLEN];
1990 struct neigh_walk_ctx *wctx = ctxt;
6336e12b 1991
7cbae20a
PR
1992 vty = wctx->vty;
1993 json_evpn = wctx->json;
72de4110 1994 n = (struct zebra_neigh *)bucket->data;
7cbae20a
PR
1995 if (!n)
1996 return;
6336e12b 1997
7cbae20a
PR
1998 ipaddr2str(&n->ip, buf, sizeof(buf));
1999 if (json_evpn)
2000 json_row = json_object_new_object();
6336e12b 2001
7cbae20a 2002 zebra_evpn_print_neigh(n, vty, json_row);
6336e12b 2003
7cbae20a
PR
2004 if (json_evpn)
2005 json_object_object_add(json_evpn, buf, json_row);
6336e12b
PR
2006}
2007
7cbae20a 2008void zebra_evpn_print_dad_neigh_hash(struct hash_bucket *bucket, void *ctxt)
6336e12b 2009{
72de4110 2010 struct zebra_neigh *nbr;
6336e12b 2011
72de4110 2012 nbr = (struct zebra_neigh *)bucket->data;
7cbae20a
PR
2013 if (!nbr)
2014 return;
6336e12b 2015
7cbae20a
PR
2016 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
2017 zebra_evpn_print_neigh_hash(bucket, ctxt);
6336e12b
PR
2018}
2019
7cbae20a
PR
2020void zebra_evpn_print_dad_neigh_hash_detail(struct hash_bucket *bucket,
2021 void *ctxt)
6336e12b 2022{
72de4110 2023 struct zebra_neigh *nbr;
6336e12b 2024
72de4110 2025 nbr = (struct zebra_neigh *)bucket->data;
7cbae20a
PR
2026 if (!nbr)
2027 return;
6336e12b 2028
7cbae20a
PR
2029 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
2030 zebra_evpn_print_neigh_hash_detail(bucket, ctxt);
6336e12b 2031}
036daaca 2032
f6371c34 2033void zebra_evpn_neigh_remote_macip_add(struct zebra_evpn *zevpn,
272e11bf 2034 struct zebra_vrf *zvrf,
1a3bd37f 2035 const struct ipaddr *ipaddr,
3198b2b3
DS
2036 struct zebra_mac *mac,
2037 struct in_addr vtep_ip, uint8_t flags,
2038 uint32_t seq)
036daaca 2039{
72de4110 2040 struct zebra_neigh *n;
036daaca 2041 int update_neigh = 0;
3198b2b3 2042 struct zebra_mac *old_mac = NULL;
036daaca
PR
2043 bool old_static = false;
2044 bool do_dad = false;
2045 bool is_dup_detect = false;
2046 bool is_router;
2047
2bdd4461 2048 assert(mac);
036daaca
PR
2049 is_router = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
2050
2051 /* Check if the remote neighbor itself is unknown or has a
2052 * change. If so, create or update and then install the entry.
2053 */
2054 n = zebra_evpn_neigh_lookup(zevpn, ipaddr);
2055 if (!n || !CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
2056 || is_router != !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)
2057 || (memcmp(&n->emac, &mac->macaddr, sizeof(struct ethaddr)) != 0)
2058 || !IPV4_ADDR_SAME(&n->r_vtep_ip, &vtep_ip) || seq != n->rem_seq)
2059 update_neigh = 1;
2060
2061 if (update_neigh) {
2062 if (!n) {
2063 n = zebra_evpn_neigh_add(zevpn, ipaddr, &mac->macaddr,
2064 mac, 0);
036daaca 2065 } else {
036daaca
PR
2066 /* When host moves but changes its (MAC,IP)
2067 * binding, BGP may install a MACIP entry that
2068 * corresponds to "older" location of the host
2069 * in transient situations (because {IP1,M1}
2070 * is a different route from {IP1,M2}). Check
2071 * the sequence number and ignore this update
2072 * if appropriate.
2073 */
16de1338
AK
2074
2075 if (!zebra_evpn_neigh_is_bgp_seq_ok(
2076 zevpn, n, &mac->macaddr, seq, false))
036daaca 2077 return;
036daaca
PR
2078 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2079 old_static = zebra_evpn_neigh_is_static(n);
2080 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
2081 zlog_debug(
ef7b8be4
DL
2082 "sync->remote neigh vni %u ip %pIA mac %pEA seq %d f0x%x",
2083 n->zevpn->vni, &n->ip, &n->emac,
036daaca 2084 seq, n->flags);
036daaca 2085 if (IS_ZEBRA_NEIGH_ACTIVE(n))
fb8f609d
AK
2086 zebra_evpn_neigh_send_del_to_client(
2087 zevpn->vni, &n->ip, &n->emac,
2088 n->flags, n->state,
2089 false /*force*/);
bda6be1c 2090 zebra_evpn_neigh_clear_sync_info(n);
036daaca
PR
2091 }
2092 if (memcmp(&n->emac, &mac->macaddr,
2093 sizeof(struct ethaddr))
2094 != 0) {
2095 /* update neigh list for macs */
2096 old_mac =
2097 zebra_evpn_mac_lookup(zevpn, &n->emac);
2098 if (old_mac) {
2099 listnode_delete(old_mac->neigh_list, n);
2100 n->mac = NULL;
2101 zebra_evpn_deref_ip2mac(zevpn, old_mac);
2102 }
2103 n->mac = mac;
2104 listnode_add_sort(mac->neigh_list, n);
2105 memcpy(&n->emac, &mac->macaddr, ETH_ALEN);
2106
2107 /* Check Neigh's curent state is local
2108 * (this is the case where neigh/host has moved
2109 * from L->R) and check previous detction
2110 * started via local learning.
2111 *
2112 * RFC-7432: A PE/VTEP that detects a MAC
2113 * mobilit event via local learning starts
2114 * an M-second timer.
2115 * VTEP-IP or seq. change along is not
2116 * considered for dup. detection.
2117 *
2118 * Mobilty event scenario-B IP-MAC binding
2119 * changed.
2120 */
2121 if ((!CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
2122 && n->dad_count)
2123 do_dad = true;
2124 }
2125 }
2126
2127 /* Set "remote" forwarding info. */
2128 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ALL_LOCAL_FLAGS);
2129 n->r_vtep_ip = vtep_ip;
2130 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
2131
2132 /* Set router flag (R-bit) to this Neighbor entry */
2133 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG))
2134 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2135 else
2136 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2137
2138 /* Check old or new MAC detected as duplicate,
2139 * inherit duplicate flag to this neigh.
2140 */
2141 if (zebra_evpn_ip_inherit_dad_from_mac(zvrf, old_mac, mac, n)) {
2142 flog_warn(
2143 EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
ef7b8be4
DL
2144 "VNI %u: MAC %pEA IP %pIA detected as duplicate during remote update, inherit duplicate from MAC",
2145 zevpn->vni, &mac->macaddr, &n->ip);
036daaca
PR
2146 }
2147
2148 /* Check duplicate address detection for IP */
2149 zebra_evpn_dup_addr_detect_for_neigh(
2150 zvrf, n, n->r_vtep_ip, do_dad, &is_dup_detect, false);
2151 /* Install the entry. */
2152 if (!is_dup_detect)
2153 zebra_evpn_rem_neigh_install(zevpn, n, old_static);
2154 }
2155
036daaca
PR
2156 /* Update seq number. */
2157 n->rem_seq = seq;
2158}
224315f3 2159
f6371c34
DS
2160int zebra_evpn_neigh_gw_macip_add(struct interface *ifp,
2161 struct zebra_evpn *zevpn, struct ipaddr *ip,
3198b2b3 2162 struct zebra_mac *mac)
224315f3 2163{
72de4110 2164 struct zebra_neigh *n;
224315f3 2165
2bdd4461 2166 assert(mac);
224315f3
PR
2167
2168 n = zebra_evpn_neigh_lookup(zevpn, ip);
97511d01 2169 if (!n)
224315f3 2170 n = zebra_evpn_neigh_add(zevpn, ip, &mac->macaddr, mac, 0);
224315f3
PR
2171
2172 /* Set "local" forwarding info. */
2173 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
2174 ZEBRA_NEIGH_SET_ACTIVE(n);
2175 memcpy(&n->emac, &mac->macaddr, ETH_ALEN);
2176 n->ifindex = ifp->ifindex;
2177
2178 /* Only advertise in BGP if the knob is enabled */
2179 if (advertise_gw_macip_enabled(zevpn)) {
2180
224315f3
PR
2181 SET_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW);
2182 /* Set Router flag (R-bit) */
2183 if (ip->ipa_type == IPADDR_V6)
2184 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2185
2186 if (IS_ZEBRA_DEBUG_VXLAN)
2187 zlog_debug(
ef7b8be4 2188 "SVI %s(%u) L2-VNI %u, sending GW MAC %pEA IP %pIA add to BGP with flags 0x%x",
224315f3 2189 ifp->name, ifp->ifindex, zevpn->vni,
ef7b8be4 2190 &mac->macaddr, ip, n->flags);
224315f3
PR
2191
2192 zebra_evpn_neigh_send_add_to_client(
2193 zevpn->vni, ip, &n->emac, n->mac, n->flags, n->loc_seq);
2194 } else if (advertise_svi_macip_enabled(zevpn)) {
2195
2196 SET_FLAG(n->flags, ZEBRA_NEIGH_SVI_IP);
2197 if (IS_ZEBRA_DEBUG_VXLAN)
2198 zlog_debug(
ef7b8be4 2199 "SVI %s(%u) L2-VNI %u, sending SVI MAC %pEA IP %pIA add to BGP with flags 0x%x",
224315f3 2200 ifp->name, ifp->ifindex, zevpn->vni,
ef7b8be4 2201 &mac->macaddr, ip, n->flags);
224315f3
PR
2202
2203 zebra_evpn_neigh_send_add_to_client(
2204 zevpn->vni, ip, &n->emac, n->mac, n->flags, n->loc_seq);
2205 }
2206
2207 return 0;
2208}
32fe7dfd 2209
f6371c34 2210void zebra_evpn_neigh_remote_uninstall(struct zebra_evpn *zevpn,
72de4110
DS
2211 struct zebra_vrf *zvrf,
2212 struct zebra_neigh *n,
3198b2b3 2213 struct zebra_mac *mac,
1a3bd37f 2214 const struct ipaddr *ipaddr)
32fe7dfd 2215{
32fe7dfd
PR
2216 if (zvrf->dad_freeze && CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE)
2217 && CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
2218 && (memcmp(n->emac.octet, mac->macaddr.octet, ETH_ALEN) == 0)) {
2219 struct interface *vlan_if;
2220
2221 vlan_if = zevpn_map_to_svi(zevpn);
2222 if (IS_ZEBRA_DEBUG_VXLAN)
2223 zlog_debug(
ef7b8be4
DL
2224 "%s: IP %pIA (flags 0x%x intf %s) is remote and duplicate, read kernel for local entry",
2225 __func__, ipaddr, n->flags,
2226 vlan_if ? vlan_if->name : "Unknown");
32fe7dfd
PR
2227 if (vlan_if)
2228 neigh_read_specific_ip(ipaddr, vlan_if);
2229 }
2230
2231 /* When the MAC changes for an IP, it is possible the
2232 * client may update the new MAC before trying to delete the
2233 * "old" neighbor (as these are two different MACIP routes).
2234 * Do the delete only if the MAC matches.
2235 */
2236 if (!memcmp(n->emac.octet, mac->macaddr.octet, ETH_ALEN)) {
2237 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2238 zebra_evpn_sync_neigh_del(n);
2239 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
2240 zebra_evpn_neigh_uninstall(zevpn, n);
2241 zebra_evpn_neigh_del(zevpn, n);
2242 zebra_evpn_deref_ip2mac(zevpn, mac);
2243 }
2244 }
2245}
33064a62 2246
f6371c34 2247int zebra_evpn_neigh_del_ip(struct zebra_evpn *zevpn, const struct ipaddr *ip)
33064a62 2248{
72de4110 2249 struct zebra_neigh *n;
3198b2b3 2250 struct zebra_mac *zmac;
33064a62
PR
2251 bool old_bgp_ready;
2252 bool new_bgp_ready;
33064a62
PR
2253 struct zebra_vrf *zvrf;
2254
2255 /* If entry doesn't exist, nothing to do. */
2256 n = zebra_evpn_neigh_lookup(zevpn, ip);
2257 if (!n)
2258 return 0;
2259
2260 zmac = zebra_evpn_mac_lookup(zevpn, &n->emac);
2261 if (!zmac) {
2262 if (IS_ZEBRA_DEBUG_VXLAN)
2263 zlog_debug(
ef7b8be4
DL
2264 "Trying to del a neigh %pIA without a mac %pEA on VNI %u",
2265 ip, &n->emac,
33064a62
PR
2266 zevpn->vni);
2267
2268 return 0;
2269 }
2270
2271 /* If it is a remote entry, the kernel has aged this out or someone has
32367e7a 2272 * deleted it, it needs to be re-installed as FRR is the owner.
33064a62
PR
2273 */
2274 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
2275 zebra_evpn_rem_neigh_install(zevpn, n, false /*was_static*/);
2276 return 0;
2277 }
2278
2279 /* if this is a sync entry it cannot be dropped re-install it in
2280 * the dataplane
2281 */
2282 old_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
2283 if (zebra_evpn_neigh_is_static(n)) {
2284 if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
ef7b8be4
DL
2285 zlog_debug("re-add sync neigh vni %u ip %pIA mac %pEA 0x%x",
2286 n->zevpn->vni, &n->ip, &n->emac,
33064a62
PR
2287 n->flags);
2288
2289 if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE))
2290 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL_INACTIVE);
2291 /* inform-bgp about change in local-activity if any */
2292 new_bgp_ready = zebra_evpn_neigh_is_ready_for_bgp(n);
2293 zebra_evpn_neigh_send_add_del_to_client(n, old_bgp_ready,
2294 new_bgp_ready);
2295
2296 /* re-install the entry in the kernel */
2297 zebra_evpn_sync_neigh_dp_install(n, false /* set_inactive */,
2298 false /* force_clear_static */,
2299 __func__);
2300
2301 return 0;
2302 }
2303
096f7609 2304 zvrf = zevpn->vxlan_if->vrf->info;
33064a62
PR
2305 if (!zvrf) {
2306 zlog_debug("%s: VNI %u vrf lookup failed.", __func__,
2307 zevpn->vni);
2308 return -1;
2309 }
2310
2311 /* In case of feeze action, if local neigh is in duplicate state,
2312 * Mark the Neigh as inactive before sending delete request to BGPd,
2313 * If BGPd has remote entry, it will re-install
2314 */
2315 if (zvrf->dad_freeze && CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
2316 ZEBRA_NEIGH_SET_INACTIVE(n);
2317
2318 /* Remove neighbor from BGP. */
2319 zebra_evpn_neigh_send_del_to_client(zevpn->vni, &n->ip, &n->emac,
2320 n->flags, n->state,
2321 false /* force */);
2322
2323 /* Delete this neighbor entry. */
2324 zebra_evpn_neigh_del(zevpn, n);
2325
2326 /* see if the AUTO mac needs to be deleted */
2327 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_AUTO)
243b74ed 2328 && !zebra_evpn_mac_in_use(zmac))
33064a62
PR
2329 zebra_evpn_mac_del(zevpn, zmac);
2330
2331 return 0;
2332}