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