]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vxlan.c
Merge pull request #12657 from anlancs/fix/lib-debug-empty-ip
[mirror_frr.git] / zebra / zebra_vxlan.c
CommitLineData
13d60d35 1/*
2 * Zebra EVPN for VxLAN code
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
4 *
5 * This file is part of FRR.
6 *
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * FRR is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
43e52561 25#include "hash.h"
13d60d35 26#include "if.h"
43e52561 27#include "jhash.h"
13d60d35 28#include "linklist.h"
43e52561
QY
29#include "log.h"
30#include "memory.h"
31#include "prefix.h"
13d60d35 32#include "stream.h"
43e52561 33#include "table.h"
13d60d35 34#include "vlan.h"
35#include "vxlan.h"
68e33151
CS
36#ifdef GNU_LINUX
37#include <linux/neighbour.h>
38#endif
9bee0232 39#include "lib/printfrr.h"
13d60d35 40
89272910 41#include "zebra/zebra_router.h"
43e52561
QY
42#include "zebra/debug.h"
43#include "zebra/interface.h"
13d60d35 44#include "zebra/rib.h"
45#include "zebra/rt.h"
43e52561
QY
46#include "zebra/rt_netlink.h"
47#include "zebra/zebra_errors.h"
48#include "zebra/zebra_l2.h"
13d60d35 49#include "zebra/zebra_ns.h"
13d60d35 50#include "zebra/zebra_vrf.h"
13d60d35 51#include "zebra/zebra_vxlan.h"
b2998086
PR
52#include "zebra/zebra_evpn.h"
53#include "zebra/zebra_evpn_mac.h"
54#include "zebra/zebra_evpn_neigh.h"
43e52561 55#include "zebra/zebra_vxlan_private.h"
ce5160c0 56#include "zebra/zebra_evpn_mh.h"
8b5fdf2e 57#include "zebra/zebra_evpn_vxlan.h"
3801e764 58#include "zebra/zebra_router.h"
13d60d35 59
6134fd82 60DEFINE_MTYPE_STATIC(ZEBRA, HOST_PREFIX, "host prefix");
b7cfce93 61DEFINE_MTYPE_STATIC(ZEBRA, ZL3VNI, "L3 VNI hash");
b2998086 62DEFINE_MTYPE_STATIC(ZEBRA, L3VNI_MAC, "EVPN L3VNI MAC");
7cbae20a 63DEFINE_MTYPE_STATIC(ZEBRA, L3NEIGH, "EVPN Neighbor");
8a93734c 64DEFINE_MTYPE_STATIC(ZEBRA, ZVXLAN_SG, "zebra VxLAN multicast group");
db889978 65DEFINE_MTYPE_STATIC(ZEBRA, EVPN_VTEP, "zebra VxLAN VTEP IP");
13d60d35 66
3198b2b3 67DEFINE_HOOK(zebra_rmac_update,
05843a27 68 (struct zebra_mac * rmac, struct zebra_l3vni *zl3vni, bool delete,
3198b2b3
DS
69 const char *reason),
70 (rmac, zl3vni, delete, reason));
a780a738 71
7d99ad7f
SW
72/* config knobs */
73static bool accept_bgp_seq = true;
74
13d60d35 75/* static function declarations */
87d76d54
PR
76static void zevpn_print_neigh_hash_all_evpn(struct hash_bucket *bucket,
77 void **args);
72de4110 78static void zl3vni_print_nh(struct zebra_neigh *n, struct vty *vty,
c0e519d3 79 json_object *json);
3198b2b3 80static void zl3vni_print_rmac(struct zebra_mac *zrmac, struct vty *vty,
316f4ca4 81 json_object *json);
87d76d54 82static void zevpn_print_mac_hash_all_evpn(struct hash_bucket *bucket, void *ctxt);
d62a17ae 83
b7cfce93 84/* l3-vni next-hop neigh related APIs */
72de4110
DS
85static struct zebra_neigh *zl3vni_nh_lookup(struct zebra_l3vni *zl3vni,
86 const struct ipaddr *ip);
b7cfce93 87static void *zl3vni_nh_alloc(void *p);
72de4110
DS
88static struct zebra_neigh *zl3vni_nh_add(struct zebra_l3vni *zl3vni,
89 const struct ipaddr *vtep_ip,
90 const struct ethaddr *rmac);
91static int zl3vni_nh_del(struct zebra_l3vni *zl3vni, struct zebra_neigh *n);
92static int zl3vni_nh_install(struct zebra_l3vni *zl3vni, struct zebra_neigh *n);
93static int zl3vni_nh_uninstall(struct zebra_l3vni *zl3vni,
94 struct zebra_neigh *n);
b7cfce93
MK
95
96/* l3-vni rmac related APIs */
e3b78da8 97static void zl3vni_print_rmac_hash(struct hash_bucket *, void *);
05843a27 98static struct zebra_mac *zl3vni_rmac_lookup(struct zebra_l3vni *zl3vni,
3198b2b3 99 const struct ethaddr *rmac);
b7cfce93 100static void *zl3vni_rmac_alloc(void *p);
05843a27 101static struct zebra_mac *zl3vni_rmac_add(struct zebra_l3vni *zl3vni,
3198b2b3 102 const struct ethaddr *rmac);
05843a27
DS
103static int zl3vni_rmac_del(struct zebra_l3vni *zl3vni, struct zebra_mac *zrmac);
104static int zl3vni_rmac_install(struct zebra_l3vni *zl3vni,
105 struct zebra_mac *zrmac);
106static int zl3vni_rmac_uninstall(struct zebra_l3vni *zl3vni,
3198b2b3 107 struct zebra_mac *zrmac);
b7cfce93
MK
108
109/* l3-vni related APIs*/
b7cfce93 110static void *zl3vni_alloc(void *p);
05843a27
DS
111static struct zebra_l3vni *zl3vni_add(vni_t vni, vrf_id_t vrf_id);
112static int zl3vni_del(struct zebra_l3vni *zl3vni);
113static void zebra_vxlan_process_l3vni_oper_up(struct zebra_l3vni *zl3vni);
114static void zebra_vxlan_process_l3vni_oper_down(struct zebra_l3vni *zl3vni);
b7cfce93 115
87d76d54 116static void zevpn_build_hash_table(void);
d8b87afe 117static unsigned int zebra_vxlan_sg_hash_key_make(const void *p);
015d264c
AK
118static bool zebra_vxlan_sg_hash_eq(const void *p1, const void *p2);
119static void zebra_vxlan_sg_do_deref(struct zebra_vrf *zvrf,
120 struct in_addr sip, struct in_addr mcast_grp);
847f168d
DS
121static struct zebra_vxlan_sg *zebra_vxlan_sg_do_ref(struct zebra_vrf *vrf,
122 struct in_addr sip,
123 struct in_addr mcast_grp);
abfa0a96
AK
124static void zebra_vxlan_sg_deref(struct in_addr local_vtep_ip,
125 struct in_addr mcast_grp);
126static void zebra_vxlan_sg_ref(struct in_addr local_vtep_ip,
127 struct in_addr mcast_grp);
0c16fb72 128static void zebra_vxlan_cleanup_sg_table(struct zebra_vrf *zvrf);
13d60d35 129
b2ee2b71
AK
130bool zebra_evpn_do_dup_addr_detect(struct zebra_vrf *zvrf)
131{
132 return zvrf->dup_addr_detect && zebra_evpn_mh_do_dup_addr_detect();
133}
134
13d60d35 135/* Private functions */
5e1b0650 136static int host_rb_entry_compare(const struct host_rb_entry *hle1,
85442b09 137 const struct host_rb_entry *hle2)
5e1b0650
DS
138{
139 if (hle1->p.family < hle2->p.family)
140 return -1;
141
142 if (hle1->p.family > hle2->p.family)
143 return 1;
144
145 if (hle1->p.prefixlen < hle2->p.prefixlen)
146 return -1;
147
148 if (hle1->p.prefixlen > hle2->p.prefixlen)
149 return 1;
150
151 if (hle1->p.family == AF_INET) {
152 if (hle1->p.u.prefix4.s_addr < hle2->p.u.prefix4.s_addr)
153 return -1;
154
155 if (hle1->p.u.prefix4.s_addr > hle2->p.u.prefix4.s_addr)
156 return 1;
157
158 return 0;
2acc2310
DS
159 } else if (hle1->p.family == AF_INET6) {
160 return memcmp(&hle1->p.u.prefix6, &hle2->p.u.prefix6,
161 IPV6_MAX_BYTELEN);
7bfa7d02 162 } else if (hle1->p.family == AF_EVPN) {
82732723
AK
163 uint8_t family1;
164 uint8_t family2;
165
166 /* two (v4/v6) dummy prefixes of route_type BGP_EVPN_AD_ROUTE
167 * are used for all nexthops associated with a non-zero ESI
7bfa7d02 168 */
82732723
AK
169 family1 = is_evpn_prefix_ipaddr_v4(
170 (const struct prefix_evpn *)&hle1->p)
171 ? AF_INET
172 : AF_INET6;
173 family2 = is_evpn_prefix_ipaddr_v4(
174 (const struct prefix_evpn *)&hle2->p)
175 ? AF_INET
176 : AF_INET6;
177
178
179 if (family1 < family2)
180 return -1;
181
182 if (family1 > family2)
183 return 1;
184
7bfa7d02 185 return 0;
5e1b0650 186 } else {
15569c58
DA
187 zlog_debug("%s: Unexpected family type: %d", __func__,
188 hle1->p.family);
5e1b0650
DS
189 return 0;
190 }
191}
85442b09 192RB_GENERATE(host_rb_tree_entry, host_rb_entry, hl_entry, host_rb_entry_compare);
5e1b0650 193
85442b09 194static uint32_t rb_host_count(struct host_rb_tree_entry *hrbe)
5e1b0650
DS
195{
196 struct host_rb_entry *hle;
197 uint32_t count = 0;
198
85442b09 199 RB_FOREACH (hle, host_rb_tree_entry, hrbe)
5e1b0650
DS
200 count++;
201
202 return count;
203}
13d60d35 204
db889978
CS
205static int l3vni_rmac_nh_list_cmp(void *p1, void *p2)
206{
207 const struct ipaddr *vtep_ip1 = p1;
208 const struct ipaddr *vtep_ip2 = p2;
209
210 return !ipaddr_cmp(vtep_ip1, vtep_ip2);
211}
212
213static void l3vni_rmac_nh_free(struct ipaddr *vtep_ip)
214{
215 XFREE(MTYPE_EVPN_VTEP, vtep_ip);
216}
217
218static void l3vni_rmac_nh_list_nh_delete(struct zebra_l3vni *zl3vni,
219 struct zebra_mac *zrmac,
220 struct ipaddr *vtep_ip)
221{
222 struct listnode *node = NULL, *nnode = NULL;
223 struct ipaddr *vtep = NULL;
224
225 for (ALL_LIST_ELEMENTS(zrmac->nh_list, node, nnode, vtep)) {
226 if (ipaddr_cmp(vtep, vtep_ip) == 0)
227 break;
228 }
229
230 if (node) {
231 l3vni_rmac_nh_free(vtep);
232 list_delete_node(zrmac->nh_list, node);
233 }
234}
235
cec2e17d 236/*
87d76d54 237 * Print neighbors for all EVPN.
cec2e17d 238 */
87d76d54 239static void zevpn_print_neigh_hash_all_evpn(struct hash_bucket *bucket,
cd233079 240 void **args)
cec2e17d 241{
d62a17ae 242 struct vty *vty;
87d76d54 243 json_object *json = NULL, *json_evpn = NULL;
f6371c34 244 struct zebra_evpn *zevpn;
d7c0a89a 245 uint32_t num_neigh;
d62a17ae 246 struct neigh_walk_ctx wctx;
cd233079 247 char vni_str[VNI_STR_LEN];
1374d4db 248 uint32_t print_dup;
cd233079
CS
249
250 vty = (struct vty *)args[0];
251 json = (json_object *)args[1];
1374d4db 252 print_dup = (uint32_t)(uintptr_t)args[2];
cec2e17d 253
f6371c34 254 zevpn = (struct zebra_evpn *)bucket->data;
9ea660be 255
87d76d54 256 num_neigh = hashcount(zevpn->neigh_table);
1374d4db
CS
257
258 if (print_dup)
87d76d54 259 num_neigh = num_dup_detected_neighs(zevpn);
1374d4db 260
68e33151 261 if (json == NULL) {
cd233079
CS
262 vty_out(vty,
263 "\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
87d76d54 264 zevpn->vni, num_neigh);
68e33151 265 } else {
87d76d54
PR
266 json_evpn = json_object_new_object();
267 json_object_int_add(json_evpn, "numArpNd", num_neigh);
268 snprintf(vni_str, VNI_STR_LEN, "%u", zevpn->vni);
cd233079 269 }
1374d4db 270
cd233079
CS
271 if (!num_neigh) {
272 if (json)
87d76d54 273 json_object_object_add(json, vni_str, json_evpn);
d62a17ae 274 return;
cd233079 275 }
cec2e17d 276
d62a17ae 277 /* Since we have IPv6 addresses to deal with which can vary widely in
278 * size, we try to be a bit more elegant in display by first computing
279 * the maximum width.
280 */
6006b807 281 memset(&wctx, 0, sizeof(wctx));
87d76d54 282 wctx.zevpn = zevpn;
d62a17ae 283 wctx.vty = vty;
284 wctx.addr_width = 15;
87d76d54 285 wctx.json = json_evpn;
7cbae20a
PR
286 hash_iterate(zevpn->neigh_table, zebra_evpn_find_neigh_addr_width,
287 &wctx);
cec2e17d 288
b169fd6f 289 if (json == NULL)
7cbae20a 290 zebra_evpn_print_neigh_hdr(vty, &wctx);
b169fd6f 291
1374d4db 292 if (print_dup)
7cbae20a
PR
293 hash_iterate(zevpn->neigh_table,
294 zebra_evpn_print_dad_neigh_hash, &wctx);
1374d4db 295 else
7cbae20a
PR
296 hash_iterate(zevpn->neigh_table, zebra_evpn_print_neigh_hash,
297 &wctx);
cd233079
CS
298
299 if (json)
87d76d54 300 json_object_object_add(json, vni_str, json_evpn);
cec2e17d 301}
302
e3fac919 303/*
87d76d54 304 * Print neighbors for all EVPNs in detail.
e3fac919 305 */
87d76d54 306static void zevpn_print_neigh_hash_all_evpn_detail(struct hash_bucket *bucket,
e3fac919
NS
307 void **args)
308{
309 struct vty *vty;
87d76d54 310 json_object *json = NULL, *json_evpn = NULL;
f6371c34 311 struct zebra_evpn *zevpn;
e3fac919
NS
312 uint32_t num_neigh;
313 struct neigh_walk_ctx wctx;
314 char vni_str[VNI_STR_LEN];
315 uint32_t print_dup;
316
317 vty = (struct vty *)args[0];
318 json = (json_object *)args[1];
319 print_dup = (uint32_t)(uintptr_t)args[2];
320
f6371c34 321 zevpn = (struct zebra_evpn *)bucket->data;
87d76d54 322 if (!zevpn) {
e3fac919
NS
323 if (json)
324 vty_out(vty, "{}\n");
325 return;
326 }
87d76d54 327 num_neigh = hashcount(zevpn->neigh_table);
e3fac919 328
87d76d54 329 if (print_dup && num_dup_detected_neighs(zevpn) == 0)
e3fac919
NS
330 return;
331
332 if (json == NULL) {
333 vty_out(vty,
334 "\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
87d76d54 335 zevpn->vni, num_neigh);
e3fac919 336 } else {
87d76d54
PR
337 json_evpn = json_object_new_object();
338 json_object_int_add(json_evpn, "numArpNd", num_neigh);
339 snprintf(vni_str, VNI_STR_LEN, "%u", zevpn->vni);
e3fac919
NS
340 }
341 if (!num_neigh) {
342 if (json)
87d76d54 343 json_object_object_add(json, vni_str, json_evpn);
e3fac919
NS
344 return;
345 }
346
6006b807 347 memset(&wctx, 0, sizeof(wctx));
87d76d54 348 wctx.zevpn = zevpn;
e3fac919
NS
349 wctx.vty = vty;
350 wctx.addr_width = 15;
87d76d54 351 wctx.json = json_evpn;
e3fac919
NS
352
353 if (print_dup)
87d76d54 354 hash_iterate(zevpn->neigh_table,
7cbae20a 355 zebra_evpn_print_dad_neigh_hash_detail, &wctx);
e3fac919 356 else
7cbae20a
PR
357 hash_iterate(zevpn->neigh_table,
358 zebra_evpn_print_neigh_hash_detail, &wctx);
e3fac919
NS
359
360 if (json)
87d76d54 361 json_object_object_add(json, vni_str, json_evpn);
e3fac919
NS
362}
363
9aa741ea 364/* print a specific next hop for an l3vni */
72de4110 365static void zl3vni_print_nh(struct zebra_neigh *n, struct vty *vty,
c0e519d3 366 json_object *json)
9aa741ea
MK
367{
368 char buf1[ETHER_ADDR_STRLEN];
369 char buf2[INET6_ADDRSTRLEN];
c0e519d3 370 json_object *json_hosts = NULL;
f2a503f0 371 struct host_rb_entry *hle;
9aa741ea 372
c0e519d3
MK
373 if (!json) {
374 vty_out(vty, "Ip: %s\n",
375 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
376 vty_out(vty, " RMAC: %s\n",
996c9314 377 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
f2a503f0
DS
378 vty_out(vty, " Refcount: %d\n",
379 rb_host_count(&n->host_rb));
4cce389e 380 vty_out(vty, " Prefixes:\n");
85442b09 381 RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
2dbe669b 382 vty_out(vty, " %pFX\n", &hle->p);
c0e519d3
MK
383 } else {
384 json_hosts = json_object_new_array();
996c9314
LB
385 json_object_string_add(
386 json, "ip", ipaddr2str(&(n->ip), buf2, sizeof(buf2)));
387 json_object_string_add(
388 json, "routerMac",
389 prefix_mac2str(&n->emac, buf2, sizeof(buf2)));
f2a503f0
DS
390 json_object_int_add(json, "refCount",
391 rb_host_count(&n->host_rb));
85442b09 392 RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
c0e519d3 393 json_object_array_add(json_hosts,
996c9314 394 json_object_new_string(prefix2str(
f2a503f0 395 &hle->p, buf2, sizeof(buf2))));
4cce389e 396 json_object_object_add(json, "prefixList", json_hosts);
c0e519d3 397 }
9aa741ea
MK
398}
399
400/* Print a specific RMAC entry */
3198b2b3 401static void zl3vni_print_rmac(struct zebra_mac *zrmac, struct vty *vty,
316f4ca4 402 json_object *json)
9aa741ea 403{
4a8e182a
CS
404 struct listnode *node = NULL;
405 struct ipaddr *vtep = NULL;
406 json_object *json_nhs = NULL;
9aa741ea 407
316f4ca4 408 if (!json) {
3d43b95c 409 vty_out(vty, "MAC: %pEA\n", &zrmac->macaddr);
9bcef951
MS
410 vty_out(vty, " Remote VTEP: %pI4\n",
411 &zrmac->fwd_info.r_vtep_ip);
316f4ca4 412 } else {
4a8e182a 413 json_nhs = json_object_new_array();
3d43b95c
CS
414 json_object_string_addf(json, "routerMac", "%pEA",
415 &zrmac->macaddr);
08edf9c6
DA
416 json_object_string_addf(json, "vtepIp", "%pI4",
417 &zrmac->fwd_info.r_vtep_ip);
4a8e182a 418 for (ALL_LIST_ELEMENTS_RO(zrmac->nh_list, node, vtep)) {
3d43b95c
CS
419 json_object_array_add(json_nhs, json_object_new_stringf(
420 "%pIA", vtep));
4a8e182a
CS
421 }
422 json_object_object_add(json, "nexthops", json_nhs);
316f4ca4 423 }
9aa741ea
MK
424}
425
cec2e17d 426/*
87d76d54 427 * Print MACs for all EVPNs.
cec2e17d 428 */
87d76d54 429static void zevpn_print_mac_hash_all_evpn(struct hash_bucket *bucket, void *ctxt)
cec2e17d 430{
d62a17ae 431 struct vty *vty;
87d76d54 432 json_object *json = NULL, *json_evpn = NULL;
cd233079 433 json_object *json_mac = NULL;
f6371c34 434 struct zebra_evpn *zevpn;
d7c0a89a 435 uint32_t num_macs;
d62a17ae 436 struct mac_walk_ctx *wctx = ctxt;
cd233079 437 char vni_str[VNI_STR_LEN];
cec2e17d 438
c4efd0f4
DA
439 vty = wctx->vty;
440 json = wctx->json;
cec2e17d 441
f6371c34 442 zevpn = (struct zebra_evpn *)bucket->data;
87d76d54 443 wctx->zevpn = zevpn;
cec2e17d 444
d62a17ae 445 /*We are iterating over a new VNI, set the count to 0*/
446 wctx->count = 0;
cec2e17d 447
87d76d54 448 num_macs = num_valid_macs(zevpn);
d62a17ae 449 if (!num_macs)
450 return;
cd233079 451
1374d4db 452 if (wctx->print_dup)
87d76d54 453 num_macs = num_dup_detected_macs(zevpn);
1374d4db 454
cd233079 455 if (json) {
87d76d54 456 json_evpn = json_object_new_object();
cd233079 457 json_mac = json_object_new_object();
87d76d54 458 snprintf(vni_str, VNI_STR_LEN, "%u", zevpn->vni);
d62a17ae 459 }
cec2e17d 460
cd233079
CS
461 if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
462 if (json == NULL) {
463 vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
87d76d54 464 zevpn->vni, num_macs);
b169fd6f
AK
465 vty_out(vty,
466 "Flags: N=sync-neighs, I=local-inactive, P=peer-active, X=peer-proxy\n");
467 vty_out(vty, "%-17s %-6s %-5s %-30s %-5s %s\n", "MAC",
468 "Type", "Flags", "Intf/Remote ES/VTEP",
469 "VLAN", "Seq #'s");
cd233079 470 } else
87d76d54 471 json_object_int_add(json_evpn, "numMacs", num_macs);
cd233079 472 }
1374d4db
CS
473
474 if (!num_macs) {
475 if (json) {
87d76d54
PR
476 json_object_int_add(json_evpn, "numMacs", num_macs);
477 json_object_object_add(json, vni_str, json_evpn);
1374d4db
CS
478 }
479 return;
480 }
481
87d76d54
PR
482 /* assign per-evpn to wctx->json object to fill macs
483 * under the evpn. Re-assign primary json object to fill
484 * next evpn information.
cd233079
CS
485 */
486 wctx->json = json_mac;
1374d4db 487 if (wctx->print_dup)
b2998086
PR
488 hash_iterate(zevpn->mac_table, zebra_evpn_print_dad_mac_hash,
489 wctx);
1374d4db 490 else
b2998086 491 hash_iterate(zevpn->mac_table, zebra_evpn_print_mac_hash, wctx);
cd233079
CS
492 wctx->json = json;
493 if (json) {
494 if (wctx->count)
87d76d54
PR
495 json_object_object_add(json_evpn, "macs", json_mac);
496 json_object_object_add(json, vni_str, json_evpn);
cd233079 497 }
cec2e17d 498}
499
cffe7580 500/*
87d76d54 501 * Print MACs in detail for all EVPNs.
cffe7580 502 */
87d76d54 503static void zevpn_print_mac_hash_all_evpn_detail(struct hash_bucket *bucket,
cffe7580
NS
504 void *ctxt)
505{
506 struct vty *vty;
87d76d54 507 json_object *json = NULL, *json_evpn = NULL;
cffe7580 508 json_object *json_mac = NULL;
f6371c34 509 struct zebra_evpn *zevpn;
cffe7580
NS
510 uint32_t num_macs;
511 struct mac_walk_ctx *wctx = ctxt;
512 char vni_str[VNI_STR_LEN];
513
c4efd0f4
DA
514 vty = wctx->vty;
515 json = wctx->json;
cffe7580 516
f6371c34 517 zevpn = (struct zebra_evpn *)bucket->data;
87d76d54 518 if (!zevpn) {
cffe7580
NS
519 if (json)
520 vty_out(vty, "{}\n");
521 return;
522 }
87d76d54 523 wctx->zevpn = zevpn;
cffe7580 524
87d76d54 525 /*We are iterating over a new EVPN, set the count to 0*/
cffe7580
NS
526 wctx->count = 0;
527
87d76d54 528 num_macs = num_valid_macs(zevpn);
cffe7580
NS
529 if (!num_macs)
530 return;
531
87d76d54 532 if (wctx->print_dup && (num_dup_detected_macs(zevpn) == 0))
cffe7580
NS
533 return;
534
535 if (json) {
87d76d54 536 json_evpn = json_object_new_object();
cffe7580 537 json_mac = json_object_new_object();
87d76d54 538 snprintf(vni_str, VNI_STR_LEN, "%u", zevpn->vni);
cffe7580
NS
539 }
540
541 if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
542 if (json == NULL) {
543 vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
87d76d54 544 zevpn->vni, num_macs);
cffe7580 545 } else
87d76d54 546 json_object_int_add(json_evpn, "numMacs", num_macs);
cffe7580 547 }
87d76d54
PR
548 /* assign per-evpn to wctx->json object to fill macs
549 * under the evpn. Re-assign primary json object to fill
550 * next evpn information.
cffe7580
NS
551 */
552 wctx->json = json_mac;
553 if (wctx->print_dup)
b2998086
PR
554 hash_iterate(zevpn->mac_table,
555 zebra_evpn_print_dad_mac_hash_detail, wctx);
cffe7580 556 else
b2998086
PR
557 hash_iterate(zevpn->mac_table, zebra_evpn_print_mac_hash_detail,
558 wctx);
cffe7580
NS
559 wctx->json = json;
560 if (json) {
561 if (wctx->count)
87d76d54
PR
562 json_object_object_add(json_evpn, "macs", json_mac);
563 json_object_object_add(json, vni_str, json_evpn);
cffe7580
NS
564 }
565}
566
e3b78da8 567static void zl3vni_print_nh_hash(struct hash_bucket *bucket, void *ctx)
b7cfce93
MK
568{
569 struct nh_walk_ctx *wctx = NULL;
570 struct vty *vty = NULL;
87d76d54 571 struct json_object *json_evpn = NULL;
b7cfce93 572 struct json_object *json_nh = NULL;
72de4110 573 struct zebra_neigh *n = NULL;
b7cfce93 574 char buf1[ETHER_ADDR_STRLEN];
2dbad57f 575 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
576
577 wctx = (struct nh_walk_ctx *)ctx;
578 vty = wctx->vty;
87d76d54
PR
579 json_evpn = wctx->json;
580 if (json_evpn)
b7cfce93 581 json_nh = json_object_new_object();
72de4110 582 n = (struct zebra_neigh *)bucket->data;
b7cfce93 583
87d76d54 584 if (!json_evpn) {
4cce389e 585 vty_out(vty, "%-15s %-17s\n",
2dbad57f 586 ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
4cce389e 587 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
b7cfce93 588 } else {
4cce389e 589 json_object_string_add(json_nh, "nexthopIp",
32798965 590 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
996c9314
LB
591 json_object_string_add(
592 json_nh, "routerMac",
593 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
87d76d54 594 json_object_object_add(json_evpn,
32798965
MK
595 ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
596 json_nh);
b7cfce93
MK
597 }
598}
599
e3b78da8 600static void zl3vni_print_nh_hash_all_vni(struct hash_bucket *bucket,
32798965 601 void **args)
2dbad57f 602{
603 struct vty *vty = NULL;
604 json_object *json = NULL;
87d76d54 605 json_object *json_evpn = NULL;
05843a27 606 struct zebra_l3vni *zl3vni = NULL;
2dbad57f 607 uint32_t num_nh = 0;
32798965 608 struct nh_walk_ctx wctx;
2dbad57f 609 char vni_str[VNI_STR_LEN];
610
32798965
MK
611 vty = (struct vty *)args[0];
612 json = (struct json_object *)args[1];
2dbad57f 613
05843a27 614 zl3vni = (struct zebra_l3vni *)bucket->data;
2dbad57f 615
616 num_nh = hashcount(zl3vni->nh_table);
617 if (!num_nh)
618 return;
619
620 if (json) {
87d76d54
PR
621 json_evpn = json_object_new_object();
622 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
2dbad57f 623 }
624
625 if (json == NULL) {
996c9314 626 vty_out(vty, "\nVNI %u #Next-Hops %u\n\n", zl3vni->vni, num_nh);
4cce389e 627 vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
2dbad57f 628 } else
87d76d54 629 json_object_int_add(json_evpn, "numNextHops", num_nh);
2dbad57f 630
6006b807 631 memset(&wctx, 0, sizeof(wctx));
32798965 632 wctx.vty = vty;
87d76d54 633 wctx.json = json_evpn;
32798965
MK
634 hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
635 if (json)
87d76d54 636 json_object_object_add(json, vni_str, json_evpn);
2dbad57f 637}
638
e3b78da8 639static void zl3vni_print_rmac_hash_all_vni(struct hash_bucket *bucket,
c0b4eaa4 640 void **args)
b7cfce93
MK
641{
642 struct vty *vty = NULL;
643 json_object *json = NULL;
87d76d54 644 json_object *json_evpn = NULL;
05843a27 645 struct zebra_l3vni *zl3vni = NULL;
d7c0a89a 646 uint32_t num_rmacs;
c0b4eaa4 647 struct rmac_walk_ctx wctx;
b7cfce93
MK
648 char vni_str[VNI_STR_LEN];
649
c0b4eaa4
MK
650 vty = (struct vty *)args[0];
651 json = (struct json_object *)args[1];
b7cfce93 652
05843a27 653 zl3vni = (struct zebra_l3vni *)bucket->data;
b7cfce93
MK
654
655 num_rmacs = hashcount(zl3vni->rmac_table);
656 if (!num_rmacs)
657 return;
658
659 if (json) {
87d76d54
PR
660 json_evpn = json_object_new_object();
661 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
b7cfce93
MK
662 }
663
664 if (json == NULL) {
996c9314 665 vty_out(vty, "\nVNI %u #RMACs %u\n\n", zl3vni->vni, num_rmacs);
4cce389e 666 vty_out(vty, "%-17s %-21s\n", "RMAC", "Remote VTEP");
b7cfce93 667 } else
87d76d54 668 json_object_int_add(json_evpn, "numRmacs", num_rmacs);
b7cfce93
MK
669
670 /* assign per-vni to wctx->json object to fill macs
671 * under the vni. Re-assign primary json object to fill
672 * next vni information.
673 */
6006b807 674 memset(&wctx, 0, sizeof(wctx));
c0b4eaa4 675 wctx.vty = vty;
87d76d54 676 wctx.json = json_evpn;
c0b4eaa4
MK
677 hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
678 if (json)
87d76d54 679 json_object_object_add(json, vni_str, json_evpn);
b7cfce93
MK
680}
681
e3b78da8 682static void zl3vni_print_rmac_hash(struct hash_bucket *bucket, void *ctx)
b7cfce93 683{
3198b2b3 684 struct zebra_mac *zrmac = NULL;
b7cfce93
MK
685 struct rmac_walk_ctx *wctx = NULL;
686 struct vty *vty = NULL;
687 struct json_object *json = NULL;
688 struct json_object *json_rmac = NULL;
9bcef951 689 char buf[PREFIX_STRLEN];
b7cfce93
MK
690
691 wctx = (struct rmac_walk_ctx *)ctx;
692 vty = wctx->vty;
693 json = wctx->json;
694 if (json)
695 json_rmac = json_object_new_object();
3198b2b3 696 zrmac = (struct zebra_mac *)bucket->data;
b7cfce93
MK
697
698 if (!json) {
9bcef951 699 vty_out(vty, "%-17s %-21pI4\n",
b7cfce93 700 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
9bcef951 701 &zrmac->fwd_info.r_vtep_ip);
b7cfce93 702 } else {
996c9314
LB
703 json_object_string_add(
704 json_rmac, "routerMac",
705 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)));
08edf9c6
DA
706 json_object_string_addf(json_rmac, "vtepIp", "%pI4",
707 &zrmac->fwd_info.r_vtep_ip);
996c9314
LB
708 json_object_object_add(
709 json, prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
710 json_rmac);
b7cfce93
MK
711 }
712}
713
714/* print a specific L3 VNI entry */
05843a27 715static void zl3vni_print(struct zebra_l3vni *zl3vni, void **ctx)
b7cfce93 716{
9bcef951 717 char buf[PREFIX_STRLEN];
b7cfce93
MK
718 struct vty *vty = NULL;
719 json_object *json = NULL;
f6371c34 720 struct zebra_evpn *zevpn = NULL;
87d76d54 721 json_object *json_evpn_list = NULL;
b7cfce93
MK
722 struct listnode *node = NULL, *nnode = NULL;
723
724 vty = ctx[0];
725 json = ctx[1];
726
727 if (!json) {
728 vty_out(vty, "VNI: %u\n", zl3vni->vni);
4cce389e 729 vty_out(vty, " Type: %s\n", "L3");
996c9314 730 vty_out(vty, " Tenant VRF: %s\n", zl3vni_vrf_name(zl3vni));
9bcef951
MS
731 vty_out(vty, " Local Vtep Ip: %pI4\n",
732 &zl3vni->local_vtep_ip);
b7cfce93
MK
733 vty_out(vty, " Vxlan-Intf: %s\n",
734 zl3vni_vxlan_if_name(zl3vni));
996c9314
LB
735 vty_out(vty, " SVI-If: %s\n", zl3vni_svi_if_name(zl3vni));
736 vty_out(vty, " State: %s\n", zl3vni_state2str(zl3vni));
c48d9f5f 737 vty_out(vty, " VNI Filter: %s\n",
996c9314
LB
738 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
739 ? "prefix-routes-only"
740 : "none");
28ad0501
CS
741 vty_out(vty, " System MAC: %s\n",
742 zl3vni_sysmac2str(zl3vni, buf, sizeof(buf)));
4cce389e 743 vty_out(vty, " Router MAC: %s\n",
b7cfce93 744 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
4cce389e 745 vty_out(vty, " L2 VNIs: ");
87d76d54
PR
746 for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zevpn))
747 vty_out(vty, "%u ", zevpn->vni);
b7cfce93
MK
748 vty_out(vty, "\n");
749 } else {
87d76d54 750 json_evpn_list = json_object_new_array();
b7cfce93 751 json_object_int_add(json, "vni", zl3vni->vni);
4cce389e 752 json_object_string_add(json, "type", "L3");
08edf9c6
DA
753 json_object_string_addf(json, "localVtepIp", "%pI4",
754 &zl3vni->local_vtep_ip);
4cce389e 755 json_object_string_add(json, "vxlanIntf",
b7cfce93 756 zl3vni_vxlan_if_name(zl3vni));
4cce389e 757 json_object_string_add(json, "sviIntf",
b7cfce93 758 zl3vni_svi_if_name(zl3vni));
996c9314
LB
759 json_object_string_add(json, "state", zl3vni_state2str(zl3vni));
760 json_object_string_add(json, "vrf", zl3vni_vrf_name(zl3vni));
28ad0501
CS
761 json_object_string_add(
762 json, "sysMac",
763 zl3vni_sysmac2str(zl3vni, buf, sizeof(buf)));
996c9314
LB
764 json_object_string_add(
765 json, "routerMac",
766 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
767 json_object_string_add(
768 json, "vniFilter",
769 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
770 ? "prefix-routes-only"
771 : "none");
87d76d54
PR
772 for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zevpn)) {
773 json_object_array_add(json_evpn_list,
774 json_object_new_int(zevpn->vni));
b7cfce93 775 }
87d76d54 776 json_object_object_add(json, "l2Vnis", json_evpn_list);
b7cfce93
MK
777 }
778}
779
b7cfce93 780/* print a L3 VNI hash entry */
e3b78da8 781static void zl3vni_print_hash(struct hash_bucket *bucket, void *ctx[])
b7cfce93 782{
b7cfce93
MK
783 struct vty *vty = NULL;
784 json_object *json = NULL;
87d76d54 785 json_object *json_evpn = NULL;
05843a27 786 struct zebra_l3vni *zl3vni = NULL;
b7cfce93 787
51d8de8f
MK
788 vty = (struct vty *)ctx[0];
789 json = (json_object *)ctx[1];
b7cfce93 790
05843a27 791 zl3vni = (struct zebra_l3vni *)bucket->data;
b7cfce93
MK
792
793 if (!json) {
996c9314
LB
794 vty_out(vty, "%-10u %-4s %-21s %-8lu %-8lu %-15s %-37s\n",
795 zl3vni->vni, "L3", zl3vni_vxlan_if_name(zl3vni),
4cce389e 796 hashcount(zl3vni->rmac_table),
996c9314 797 hashcount(zl3vni->nh_table), "n/a",
4cce389e 798 zl3vni_vrf_name(zl3vni));
b7cfce93 799 } else {
51d8de8f
MK
800 char vni_str[VNI_STR_LEN];
801
87d76d54
PR
802 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
803 json_evpn = json_object_new_object();
804 json_object_int_add(json_evpn, "vni", zl3vni->vni);
805 json_object_string_add(json_evpn, "vxlanIf",
b7cfce93 806 zl3vni_vxlan_if_name(zl3vni));
87d76d54 807 json_object_int_add(json_evpn, "numMacs",
4cce389e 808 hashcount(zl3vni->rmac_table));
87d76d54 809 json_object_int_add(json_evpn, "numArpNd",
4cce389e 810 hashcount(zl3vni->nh_table));
87d76d54
PR
811 json_object_string_add(json_evpn, "numRemoteVteps", "n/a");
812 json_object_string_add(json_evpn, "type", "L3");
813 json_object_string_add(json_evpn, "tenantVrf",
b7cfce93 814 zl3vni_vrf_name(zl3vni));
87d76d54 815 json_object_object_add(json, vni_str, json_evpn);
b7cfce93 816 }
b7cfce93
MK
817}
818
09af6961 819/* print a L3 VNI hash entry in detail*/
e3b78da8 820static void zl3vni_print_hash_detail(struct hash_bucket *bucket, void *data)
09af6961
NS
821{
822 struct vty *vty = NULL;
05843a27 823 struct zebra_l3vni *zl3vni = NULL;
06931fdb 824 json_object *json_array = NULL;
09af6961 825 bool use_json = false;
8b5fdf2e 826 struct zebra_evpn_show *zes = data;
09af6961
NS
827
828 vty = zes->vty;
06931fdb
LK
829 json_array = zes->json;
830 use_json = zes->use_json;
09af6961 831
05843a27 832 zl3vni = (struct zebra_l3vni *)bucket->data;
09af6961 833
06931fdb
LK
834 zebra_vxlan_print_vni(vty, zes->zvrf, zl3vni->vni,
835 use_json, json_array);
836
837 if (!use_json)
838 vty_out(vty, "\n");
09af6961
NS
839}
840
2961d060 841static int zvni_map_to_svi_ns(struct ns *ns,
a237058f
PG
842 void *_in_param,
843 void **_p_ifp)
844{
2961d060 845 struct zebra_ns *zns = ns->info;
a237058f
PG
846 struct route_node *rn;
847 struct zebra_from_svi_param *in_param =
848 (struct zebra_from_svi_param *)_in_param;
849 struct zebra_l2info_vlan *vl;
850 struct interface *tmp_if = NULL;
851 struct interface **p_ifp = (struct interface **)_p_ifp;
852 struct zebra_if *zif;
853
df447830 854 assert(in_param && p_ifp);
a237058f
PG
855
856 /* TODO: Optimize with a hash. */
857 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
858 tmp_if = (struct interface *)rn->info;
859 /* Check oper status of the SVI. */
860 if (!tmp_if || !if_is_operative(tmp_if))
861 continue;
862 zif = tmp_if->info;
863 if (!zif || zif->zif_type != ZEBRA_IF_VLAN
864 || zif->link != in_param->br_if)
865 continue;
866 vl = (struct zebra_l2info_vlan *)&zif->l2info.vl;
867
868 if (vl->vid == in_param->vid) {
df447830 869 *p_ifp = tmp_if;
2961d060 870 return NS_WALK_STOP;
a237058f
PG
871 }
872 }
2961d060 873 return NS_WALK_CONTINUE;
a237058f
PG
874}
875
8b5fdf2e
PR
876/* Map to SVI on bridge corresponding to specified VLAN. This can be one
877 * of two cases:
878 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN interface
879 * linked to the bridge
880 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge interface
881 * itself
09af6961 882 */
8b5fdf2e 883struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if)
13d60d35 884{
7cbae20a 885 struct interface *tmp_if = NULL;
8b5fdf2e
PR
886 struct zebra_if *zif;
887 struct zebra_l2info_bridge *br;
a237058f
PG
888 struct zebra_from_svi_param in_param;
889 struct interface **p_ifp;
8b5fdf2e
PR
890 /* Defensive check, caller expected to invoke only with valid bridge. */
891 if (!br_if)
892 return NULL;
1a98c087 893
8b5fdf2e
PR
894 /* Determine if bridge is VLAN-aware or not */
895 zif = br_if->info;
896 assert(zif);
897 br = &zif->l2info.br;
a237058f 898 in_param.bridge_vlan_aware = br->vlan_aware;
8b5fdf2e 899 /* Check oper status of the SVI. */
a237058f 900 if (!in_param.bridge_vlan_aware)
8b5fdf2e 901 return if_is_operative(br_if) ? br_if : NULL;
d62a17ae 902
a237058f
PG
903 in_param.vid = vid;
904 in_param.br_if = br_if;
905 in_param.zif = NULL;
906 p_ifp = &tmp_if;
bf69e212 907 /* Identify corresponding VLAN interface. */
2961d060
PG
908 ns_walk_func(zvni_map_to_svi_ns, (void *)&in_param,
909 (void **)p_ifp);
a237058f 910 return tmp_if;
8b5fdf2e 911}
2232a77c 912
f6371c34 913static int zebra_evpn_vxlan_del(struct zebra_evpn *zevpn)
8b5fdf2e
PR
914{
915 zevpn_vxlan_if_set(zevpn, zevpn->vxlan_if, false /* set */);
2232a77c 916
8b5fdf2e
PR
917 /* Remove references to the BUM mcast grp */
918 zebra_vxlan_sg_deref(zevpn->local_vtep_ip, zevpn->mcast_grp);
2232a77c 919
8b5fdf2e 920 return zebra_evpn_del(zevpn);
2232a77c 921}
07509878 922
2961d060 923static int zevpn_build_hash_table_zns(struct ns *ns,
07509878
PG
924 void *param_in __attribute__((unused)),
925 void **param_out __attribute__((unused)))
2232a77c 926{
2961d060 927 struct zebra_ns *zns = ns->info;
2853fed6 928 struct route_node *rn;
d62a17ae 929 struct interface *ifp;
07509878
PG
930 struct zebra_vrf *zvrf;
931
932 zvrf = zebra_vrf_get_evpn();
933
87d76d54 934 /* Walk VxLAN interfaces and create EVPN hash. */
2853fed6 935 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
b7cfce93 936 vni_t vni;
f6371c34 937 struct zebra_evpn *zevpn = NULL;
05843a27 938 struct zebra_l3vni *zl3vni = NULL;
d62a17ae 939 struct zebra_if *zif;
940 struct zebra_l2info_vxlan *vxl;
2232a77c 941
2853fed6 942 ifp = (struct interface *)rn->info;
943 if (!ifp)
944 continue;
d62a17ae 945 zif = ifp->info;
946 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
947 continue;
2232a77c 948
b7cfce93 949 vxl = &zif->l2info.vxl;
d62a17ae 950 vni = vxl->vni;
07509878
PG
951 /* link of VXLAN interface should be in zebra_evpn_vrf */
952 if (zvrf->zns->ns_id != vxl->link_nsid) {
953 if (IS_ZEBRA_DEBUG_VXLAN)
954 zlog_debug(
955 "Intf %s(%u) VNI %u, link not in same "
956 "namespace than BGP EVPN core instance ",
957 ifp->name, ifp->ifindex, vni);
958 continue;
959 }
643215ce 960 /* L3-VNI and L2-VNI are handled seperately */
961 zl3vni = zl3vni_lookup(vni);
962 if (zl3vni) {
2232a77c 963
b7cfce93 964 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
965 zlog_debug(
966 "create L3-VNI hash for Intf %s(%u) L3-VNI %u",
967 ifp->name, ifp->ifindex, vni);
2232a77c 968
b7cfce93 969 /* associate with vxlan_if */
b67a60d2 970 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93 971 zl3vni->vxlan_if = ifp;
2232a77c 972
523cafc4 973 /*
974 * we need to associate with SVI.
b7cfce93 975 * we can associate with svi-if only after association
523cafc4 976 * with vxlan-intf is complete
977 */
b7cfce93
MK
978 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
979
06d9cde5
CS
980 /* Associate l3vni to mac-vlan and extract VRR MAC */
981 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
982
983 if (IS_ZEBRA_DEBUG_VXLAN)
984 zlog_debug("create l3vni %u svi_if %s mac_vlan_if %s",
985 vni, zl3vni->svi_if ? zl3vni->svi_if->name
986 : "NIL",
987 zl3vni->mac_vlan_if ?
988 zl3vni->mac_vlan_if->name : "NIL");
989
b7cfce93
MK
990 if (is_l3vni_oper_up(zl3vni))
991 zebra_vxlan_process_l3vni_oper_up(zl3vni);
992
993 } else {
b7cfce93
MK
994 struct interface *vlan_if = NULL;
995
996 if (IS_ZEBRA_DEBUG_VXLAN)
997 zlog_debug(
9bcef951 998 "Create L2-VNI hash for intf %s(%u) L2-VNI %u local IP %pI4",
b7cfce93 999 ifp->name, ifp->ifindex, vni,
9bcef951 1000 &vxl->vtep_ip);
b7cfce93 1001
87d76d54 1002 /* EVPN hash entry is expected to exist, if the BGP process is killed */
8b5fdf2e 1003 zevpn = zebra_evpn_lookup(vni);
87d76d54 1004 if (zevpn) {
9df414fe 1005 zlog_debug(
87d76d54 1006 "EVPN hash already present for IF %s(%u) L2-VNI %u",
b7cfce93 1007 ifp->name, ifp->ifindex, vni);
b7cfce93 1008
27627f9a
KA
1009 /*
1010 * Inform BGP if intf is up and mapped to
1011 * bridge.
1012 */
1013 if (if_is_operative(ifp) &&
1014 zif->brslave_info.br_if)
8b5fdf2e 1015 zebra_evpn_send_add_to_client(zevpn);
b7cfce93 1016
27627f9a 1017 /* Send Local MAC-entries to client */
b2998086 1018 zebra_evpn_send_mac_list_to_client(zevpn);
27627f9a
KA
1019
1020 /* Send Loval Neighbor entries to client */
7cbae20a 1021 zebra_evpn_send_neigh_to_client(zevpn);
27627f9a 1022 } else {
8b5fdf2e 1023 zevpn = zebra_evpn_add(vni);
87d76d54 1024 if (!zevpn) {
27627f9a 1025 zlog_debug(
87d76d54 1026 "Failed to add EVPN hash, IF %s(%u) L2-VNI %u",
27627f9a 1027 ifp->name, ifp->ifindex, vni);
2961d060 1028 return NS_WALK_CONTINUE;
27627f9a 1029 }
b7cfce93 1030
87d76d54 1031 if (zevpn->local_vtep_ip.s_addr !=
27627f9a 1032 vxl->vtep_ip.s_addr ||
87d76d54 1033 zevpn->mcast_grp.s_addr !=
27627f9a
KA
1034 vxl->mcast_grp.s_addr) {
1035 zebra_vxlan_sg_deref(
87d76d54
PR
1036 zevpn->local_vtep_ip,
1037 zevpn->mcast_grp);
27627f9a
KA
1038 zebra_vxlan_sg_ref(vxl->vtep_ip,
1039 vxl->mcast_grp);
87d76d54
PR
1040 zevpn->local_vtep_ip = vxl->vtep_ip;
1041 zevpn->mcast_grp = vxl->mcast_grp;
ce5160c0
AK
1042 /* on local vtep-ip check if ES
1043 * orig-ip needs to be updated
1044 */
87d76d54 1045 zebra_evpn_es_set_base_evpn(zevpn);
27627f9a 1046 }
87d76d54 1047 zevpn_vxlan_if_set(zevpn, ifp, true /* set */);
7cbae20a
PR
1048 vlan_if = zvni_map_to_svi(
1049 vxl->access_vlan,
1050 zif->brslave_info.br_if);
27627f9a 1051 if (vlan_if) {
9daa5d47 1052 zevpn->svi_if = vlan_if;
096f7609 1053 zevpn->vrf_id = vlan_if->vrf->vrf_id;
27627f9a 1054 zl3vni = zl3vni_from_vrf(
096f7609 1055 vlan_if->vrf->vrf_id);
27627f9a
KA
1056 if (zl3vni)
1057 listnode_add_sort(
87d76d54 1058 zl3vni->l2vnis, zevpn);
27627f9a 1059 }
b7cfce93 1060
27627f9a
KA
1061 /*
1062 * Inform BGP if intf is up and mapped to
1063 * bridge.
1064 */
1065 if (if_is_operative(ifp) &&
1066 zif->brslave_info.br_if)
8b5fdf2e 1067 zebra_evpn_send_add_to_client(zevpn);
27627f9a 1068 }
b7cfce93 1069 }
d62a17ae 1070 }
2961d060 1071 return NS_WALK_CONTINUE;
07509878
PG
1072}
1073
1074/*
1075 * Build the VNI hash table by going over the VxLAN interfaces. This
1076 * is called when EVPN (advertise-all-vni) is enabled.
1077 */
1078
1079static void zevpn_build_hash_table(void)
1080{
8998807f 1081 ns_walk_func(zevpn_build_hash_table_zns, NULL, NULL);
2232a77c 1082}
1083
2232a77c 1084/*
87d76d54 1085 * Cleanup EVPN/VTEP and update kernel
2232a77c 1086 */
8b5fdf2e 1087static void zebra_evpn_vxlan_cleanup_all(struct hash_bucket *bucket, void *arg)
2232a77c 1088{
f6371c34 1089 struct zebra_evpn *zevpn = NULL;
05843a27 1090 struct zebra_l3vni *zl3vni = NULL;
2232a77c 1091
f6371c34 1092 zevpn = (struct zebra_evpn *)bucket->data;
2232a77c 1093
b27be4db
CS
1094 /* remove l2vni from l2vni's tenant-vrf l3-vni list */
1095 zl3vni = zl3vni_from_vrf(zevpn->vrf_id);
b7cfce93 1096 if (zl3vni)
87d76d54 1097 listnode_delete(zl3vni->l2vnis, zevpn);
b7cfce93 1098
8b5fdf2e 1099 zebra_evpn_cleanup_all(bucket, arg);
2232a77c 1100}
1101
655b04d1 1102/* cleanup L3VNI */
e3b78da8 1103static void zl3vni_cleanup_all(struct hash_bucket *bucket, void *args)
655b04d1 1104{
05843a27 1105 struct zebra_l3vni *zl3vni = NULL;
655b04d1 1106
05843a27 1107 zl3vni = (struct zebra_l3vni *)bucket->data;
655b04d1
MK
1108
1109 zebra_vxlan_process_l3vni_oper_down(zl3vni);
1110}
1111
85442b09 1112static void rb_find_or_add_host(struct host_rb_tree_entry *hrbe,
e4a1ec74 1113 const struct prefix *host)
5e1b0650
DS
1114{
1115 struct host_rb_entry lookup;
1116 struct host_rb_entry *hle;
1117
1118 memset(&lookup, 0, sizeof(lookup));
1119 memcpy(&lookup.p, host, sizeof(*host));
1120
85442b09 1121 hle = RB_FIND(host_rb_tree_entry, hrbe, &lookup);
5e1b0650
DS
1122 if (hle)
1123 return;
1124
1125 hle = XCALLOC(MTYPE_HOST_PREFIX, sizeof(struct host_rb_entry));
1126 memcpy(hle, &lookup, sizeof(lookup));
1127
85442b09 1128 RB_INSERT(host_rb_tree_entry, hrbe, hle);
5e1b0650
DS
1129}
1130
85442b09 1131static void rb_delete_host(struct host_rb_tree_entry *hrbe, struct prefix *host)
5e1b0650
DS
1132{
1133 struct host_rb_entry lookup;
1134 struct host_rb_entry *hle;
1135
1136 memset(&lookup, 0, sizeof(lookup));
1137 memcpy(&lookup.p, host, sizeof(*host));
1138
85442b09 1139 hle = RB_FIND(host_rb_tree_entry, hrbe, &lookup);
10ac2516 1140 if (hle) {
85442b09 1141 RB_REMOVE(host_rb_tree_entry, hrbe, hle);
10ac2516
DS
1142 XFREE(MTYPE_HOST_PREFIX, hle);
1143 }
5e1b0650
DS
1144
1145 return;
1146}
1147
b7cfce93
MK
1148/*
1149 * Look up MAC hash entry.
1150 */
05843a27 1151static struct zebra_mac *zl3vni_rmac_lookup(struct zebra_l3vni *zl3vni,
3198b2b3 1152 const struct ethaddr *rmac)
b7cfce93 1153{
3198b2b3
DS
1154 struct zebra_mac tmp;
1155 struct zebra_mac *pmac;
b7cfce93
MK
1156
1157 memset(&tmp, 0, sizeof(tmp));
1158 memcpy(&tmp.macaddr, rmac, ETH_ALEN);
1159 pmac = hash_lookup(zl3vni->rmac_table, &tmp);
2232a77c 1160
b7cfce93 1161 return pmac;
2dbad57f 1162}
2232a77c 1163
cec2e17d 1164/*
b7cfce93 1165 * Callback to allocate RMAC hash entry.
cec2e17d 1166 */
2dbad57f 1167static void *zl3vni_rmac_alloc(void *p)
d62a17ae 1168{
3198b2b3
DS
1169 const struct zebra_mac *tmp_rmac = p;
1170 struct zebra_mac *zrmac;
d62a17ae 1171
3198b2b3 1172 zrmac = XCALLOC(MTYPE_L3VNI_MAC, sizeof(struct zebra_mac));
b7cfce93 1173 *zrmac = *tmp_rmac;
d62a17ae 1174
b7cfce93 1175 return ((void *)zrmac);
2dbad57f 1176}
cd233079 1177
b7cfce93
MK
1178/*
1179 * Add RMAC entry to l3-vni
1180 */
05843a27 1181static struct zebra_mac *zl3vni_rmac_add(struct zebra_l3vni *zl3vni,
3198b2b3 1182 const struct ethaddr *rmac)
b7cfce93 1183{
3198b2b3
DS
1184 struct zebra_mac tmp_rmac;
1185 struct zebra_mac *zrmac = NULL;
d62a17ae 1186
6006b807 1187 memset(&tmp_rmac, 0, sizeof(tmp_rmac));
b7cfce93
MK
1188 memcpy(&tmp_rmac.macaddr, rmac, ETH_ALEN);
1189 zrmac = hash_get(zl3vni->rmac_table, &tmp_rmac, zl3vni_rmac_alloc);
db889978
CS
1190 zrmac->nh_list = list_new();
1191 zrmac->nh_list->cmp = (int (*)(void *, void *))l3vni_rmac_nh_list_cmp;
1192 zrmac->nh_list->del = (void (*)(void *))l3vni_rmac_nh_free;
b7cfce93 1193
2dbad57f 1194 SET_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE);
1195 SET_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC);
1196
b7cfce93 1197 return zrmac;
2dbad57f 1198}
cec2e17d 1199
1200/*
b7cfce93 1201 * Delete MAC entry.
cec2e17d 1202 */
05843a27 1203static int zl3vni_rmac_del(struct zebra_l3vni *zl3vni, struct zebra_mac *zrmac)
cec2e17d 1204{
3198b2b3 1205 struct zebra_mac *tmp_rmac;
cd233079 1206
db889978
CS
1207 /* free the list of nh list*/
1208 list_delete(&zrmac->nh_list);
cd233079 1209
b7cfce93 1210 tmp_rmac = hash_release(zl3vni->rmac_table, zrmac);
b2998086 1211 XFREE(MTYPE_L3VNI_MAC, tmp_rmac);
cd233079 1212
b7cfce93 1213 return 0;
2dbad57f 1214}
cec2e17d 1215
1216/*
036d93c0 1217 * Install remote RMAC into the forwarding plane.
cec2e17d 1218 */
05843a27
DS
1219static int zl3vni_rmac_install(struct zebra_l3vni *zl3vni,
1220 struct zebra_mac *zrmac)
cec2e17d 1221{
478566d6
MS
1222 const struct zebra_if *zif = NULL, *br_zif = NULL;
1223 const struct zebra_l2info_vxlan *vxl = NULL;
1224 const struct interface *br_ifp;
036d93c0 1225 enum zebra_dplane_result res;
478566d6 1226 vlanid_t vid;
cec2e17d 1227
996c9314
LB
1228 if (!(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE))
1229 || !(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC)))
b7cfce93
MK
1230 return 0;
1231
1232 zif = zl3vni->vxlan_if->info;
1233 if (!zif)
1234 return -1;
1235
478566d6
MS
1236 br_ifp = zif->brslave_info.br_if;
1237 if (br_ifp == NULL)
1238 return -1;
1239
b7cfce93
MK
1240 vxl = &zif->l2info.vxl;
1241
478566d6
MS
1242 br_zif = (const struct zebra_if *)br_ifp->info;
1243
1244 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
1245 vid = vxl->access_vlan;
1246 else
1247 vid = 0;
1248
f188e68e
AK
1249 res = dplane_rem_mac_add(zl3vni->vxlan_if, br_ifp, vid,
1250 &zrmac->macaddr, zrmac->fwd_info.r_vtep_ip, 0, 0,
1251 false /*was_static*/);
036d93c0
MS
1252 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
1253 return 0;
1254 else
1255 return -1;
2dbad57f 1256}
b7cfce93
MK
1257
1258/*
036d93c0 1259 * Uninstall remote RMAC from the forwarding plane.
b7cfce93 1260 */
05843a27
DS
1261static int zl3vni_rmac_uninstall(struct zebra_l3vni *zl3vni,
1262 struct zebra_mac *zrmac)
b7cfce93 1263{
478566d6
MS
1264 const struct zebra_if *zif = NULL, *br_zif;
1265 const struct zebra_l2info_vxlan *vxl = NULL;
1266 const struct interface *br_ifp;
1267 vlanid_t vid;
036d93c0 1268 enum zebra_dplane_result res;
b7cfce93 1269
996c9314
LB
1270 if (!(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE))
1271 || !(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC)))
b7cfce93
MK
1272 return 0;
1273
1274 if (!zl3vni->vxlan_if) {
e0e140a7
MS
1275 if (IS_ZEBRA_DEBUG_VXLAN)
1276 zlog_debug(
ef7b8be4
DL
1277 "RMAC %pEA on L3-VNI %u hash %p couldn't be uninstalled - no vxlan_if",
1278 &zrmac->macaddr, zl3vni->vni, zl3vni);
b7cfce93
MK
1279 return -1;
1280 }
1281
1282 zif = zl3vni->vxlan_if->info;
1283 if (!zif)
1284 return -1;
1285
478566d6
MS
1286 br_ifp = zif->brslave_info.br_if;
1287 if (br_ifp == NULL)
1288 return -1;
1289
b7cfce93
MK
1290 vxl = &zif->l2info.vxl;
1291
478566d6
MS
1292 br_zif = (const struct zebra_if *)br_ifp->info;
1293 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
1294 vid = vxl->access_vlan;
1295 else
1296 vid = 0;
1297
f188e68e 1298 res = dplane_rem_mac_del(zl3vni->vxlan_if, br_ifp, vid,
036d93c0
MS
1299 &zrmac->macaddr, zrmac->fwd_info.r_vtep_ip);
1300 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
1301 return 0;
1302 else
1303 return -1;
2dbad57f 1304}
1305
1306/* handle rmac add */
05843a27 1307static int zl3vni_remote_rmac_add(struct zebra_l3vni *zl3vni,
e4a1ec74 1308 const struct ethaddr *rmac,
3d43b95c 1309 const struct ipaddr *vtep_ip)
2dbad57f 1310{
3198b2b3 1311 struct zebra_mac *zrmac = NULL;
db889978 1312 struct ipaddr *vtep = NULL;
2dbad57f 1313
1314 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
1315 if (!zrmac) {
1316
651fa905 1317 /* Create the RMAC entry, or update its vtep, if necessary. */
2dbad57f 1318 zrmac = zl3vni_rmac_add(zl3vni, rmac);
1319 if (!zrmac) {
9df414fe 1320 zlog_debug(
3d43b95c
CS
1321 "Failed to add RMAC %pEA L3VNI %u Remote VTEP %pIA",
1322 rmac, zl3vni->vni, vtep_ip);
2dbad57f 1323 return -1;
1324 }
1325 memset(&zrmac->fwd_info, 0, sizeof(zrmac->fwd_info));
1326 zrmac->fwd_info.r_vtep_ip = vtep_ip->ipaddr_v4;
1327
db889978
CS
1328 vtep = XCALLOC(MTYPE_EVPN_VTEP, sizeof(struct ipaddr));
1329 memcpy(vtep, vtep_ip, sizeof(struct ipaddr));
1330 if (!listnode_add_sort_nodup(zrmac->nh_list, (void *)vtep))
1331 XFREE(MTYPE_EVPN_VTEP, vtep);
1332
a780a738
AD
1333 /* Send RMAC for FPM processing */
1334 hook_call(zebra_rmac_update, zrmac, zl3vni, false,
1335 "new RMAC added");
1336
651fa905
CS
1337 /* install rmac in kernel */
1338 zl3vni_rmac_install(zl3vni, zrmac);
1339 } else if (!IPV4_ADDR_SAME(&zrmac->fwd_info.r_vtep_ip,
1340 &vtep_ip->ipaddr_v4)) {
1341 if (IS_ZEBRA_DEBUG_VXLAN)
1342 zlog_debug(
3d43b95c 1343 "L3VNI %u Remote VTEP change(%pI4 -> %pIA) for RMAC %pEA",
ef7b8be4 1344 zl3vni->vni, &zrmac->fwd_info.r_vtep_ip,
3d43b95c 1345 vtep_ip, rmac);
651fa905
CS
1346
1347 zrmac->fwd_info.r_vtep_ip = vtep_ip->ipaddr_v4;
1348
db889978
CS
1349 vtep = XCALLOC(MTYPE_EVPN_VTEP, sizeof(struct ipaddr));
1350 memcpy(vtep, vtep_ip, sizeof(struct ipaddr));
1351 if (!listnode_add_sort_nodup(zrmac->nh_list, (void *)vtep))
1352 XFREE(MTYPE_EVPN_VTEP, vtep);
1353
2dbad57f 1354 /* install rmac in kernel */
1355 zl3vni_rmac_install(zl3vni, zrmac);
1356 }
6134fd82 1357
2dbad57f 1358 return 0;
1359}
1360
1361
1362/* handle rmac delete */
05843a27 1363static void zl3vni_remote_rmac_del(struct zebra_l3vni *zl3vni,
3198b2b3 1364 struct zebra_mac *zrmac,
3d43b95c 1365 struct ipaddr *vtep_ip)
2dbad57f 1366{
db889978 1367 struct ipaddr ipv4_vtep;
2dbad57f 1368
ae9e6bea 1369 if (!zl3vni_nh_lookup(zl3vni, vtep_ip)) {
6006b807 1370 memset(&ipv4_vtep, 0, sizeof(ipv4_vtep));
db889978
CS
1371 ipv4_vtep.ipa_type = IPADDR_V4;
1372 if (vtep_ip->ipa_type == IPADDR_V6)
1373 ipv4_mapped_ipv6_to_ipv4(&vtep_ip->ipaddr_v6,
1374 &ipv4_vtep.ipaddr_v4);
1375 else
1376 memcpy(&(ipv4_vtep.ipaddr_v4), &vtep_ip->ipaddr_v4,
1377 sizeof(struct in_addr));
2dbad57f 1378
db889978
CS
1379 /* remove nh from rmac's list */
1380 l3vni_rmac_nh_list_nh_delete(zl3vni, zrmac, &ipv4_vtep);
1381 /* delete nh is same as current selected, fall back to
1382 * one present in the list
1383 */
1384 if (IPV4_ADDR_SAME(&zrmac->fwd_info.r_vtep_ip,
1385 &ipv4_vtep.ipaddr_v4) &&
1386 listcount(zrmac->nh_list)) {
1387 struct ipaddr *vtep;
a780a738 1388
db889978
CS
1389 vtep = listgetdata(listhead(zrmac->nh_list));
1390 zrmac->fwd_info.r_vtep_ip = vtep->ipaddr_v4;
1391 if (IS_ZEBRA_DEBUG_VXLAN)
1392 zlog_debug(
3d43b95c 1393 "L3VNI %u Remote VTEP nh change(%pIA -> %pI4) for RMAC %pEA",
db889978
CS
1394 zl3vni->vni, &ipv4_vtep,
1395 &zrmac->fwd_info.r_vtep_ip,
3d43b95c 1396 &zrmac->macaddr);
db889978
CS
1397
1398 /* install rmac in kernel */
1399 zl3vni_rmac_install(zl3vni, zrmac);
1400 }
ae9e6bea
CS
1401
1402 if (!listcount(zrmac->nh_list)) {
1403 /* uninstall from kernel */
1404 zl3vni_rmac_uninstall(zl3vni, zrmac);
1405
1406 /* Send RMAC for FPM processing */
1407 hook_call(zebra_rmac_update, zrmac, zl3vni, true,
1408 "RMAC deleted");
1409
1410 if (IS_ZEBRA_DEBUG_VXLAN)
1411 zlog_debug(
1412 "L3VNI %u RMAC %pEA vtep_ip %pIA delete",
1413 zl3vni->vni, &zrmac->macaddr, vtep_ip);
1414
1415 /* del the rmac entry */
1416 zl3vni_rmac_del(zl3vni, zrmac);
1417 }
2dbad57f 1418 }
2dbad57f 1419}
b7cfce93
MK
1420
1421/*
1422 * Look up nh hash entry on a l3-vni.
1423 */
72de4110
DS
1424static struct zebra_neigh *zl3vni_nh_lookup(struct zebra_l3vni *zl3vni,
1425 const struct ipaddr *ip)
b7cfce93 1426{
72de4110
DS
1427 struct zebra_neigh tmp;
1428 struct zebra_neigh *n;
b7cfce93
MK
1429
1430 memset(&tmp, 0, sizeof(tmp));
1431 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
1432 n = hash_lookup(zl3vni->nh_table, &tmp);
1433
1434 return n;
2dbad57f 1435}
b7cfce93
MK
1436
1437
1438/*
1439 * Callback to allocate NH hash entry on L3-VNI.
1440 */
2dbad57f 1441static void *zl3vni_nh_alloc(void *p)
b7cfce93 1442{
72de4110
DS
1443 const struct zebra_neigh *tmp_n = p;
1444 struct zebra_neigh *n;
b7cfce93 1445
72de4110 1446 n = XCALLOC(MTYPE_L3NEIGH, sizeof(struct zebra_neigh));
b7cfce93
MK
1447 *n = *tmp_n;
1448
1449 return ((void *)n);
2dbad57f 1450}
b7cfce93
MK
1451
1452/*
1453 * Add neighbor entry.
1454 */
72de4110
DS
1455static struct zebra_neigh *zl3vni_nh_add(struct zebra_l3vni *zl3vni,
1456 const struct ipaddr *ip,
1457 const struct ethaddr *mac)
b7cfce93 1458{
72de4110
DS
1459 struct zebra_neigh tmp_n;
1460 struct zebra_neigh *n = NULL;
b7cfce93 1461
6006b807 1462 memset(&tmp_n, 0, sizeof(tmp_n));
b7cfce93
MK
1463 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
1464 n = hash_get(zl3vni->nh_table, &tmp_n, zl3vni_nh_alloc);
b7cfce93 1465
85442b09 1466 RB_INIT(host_rb_tree_entry, &n->host_rb);
6134fd82 1467
b7cfce93 1468 memcpy(&n->emac, mac, ETH_ALEN);
2dbad57f 1469 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
1470 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE_NH);
b7cfce93
MK
1471
1472 return n;
2dbad57f 1473}
b7cfce93
MK
1474
1475/*
1476 * Delete neighbor entry.
1477 */
72de4110 1478static int zl3vni_nh_del(struct zebra_l3vni *zl3vni, struct zebra_neigh *n)
b7cfce93 1479{
72de4110 1480 struct zebra_neigh *tmp_n;
f2a503f0 1481 struct host_rb_entry *hle;
b7cfce93 1482
85442b09
DS
1483 while (!RB_EMPTY(host_rb_tree_entry, &n->host_rb)) {
1484 hle = RB_ROOT(host_rb_tree_entry, &n->host_rb);
f2a503f0 1485
85442b09 1486 RB_REMOVE(host_rb_tree_entry, &n->host_rb, hle);
f2a503f0
DS
1487 XFREE(MTYPE_HOST_PREFIX, hle);
1488 }
55aabf6f 1489
b7cfce93 1490 tmp_n = hash_release(zl3vni->nh_table, n);
7cbae20a 1491 XFREE(MTYPE_L3NEIGH, tmp_n);
b7cfce93
MK
1492
1493 return 0;
2dbad57f 1494}
b7cfce93
MK
1495
1496/*
1497 * Install remote nh as neigh into the kernel.
1498 */
72de4110 1499static int zl3vni_nh_install(struct zebra_l3vni *zl3vni, struct zebra_neigh *n)
b7cfce93 1500{
68e33151
CS
1501 uint8_t flags;
1502 int ret = 0;
1503
b7cfce93
MK
1504 if (!is_l3vni_oper_up(zl3vni))
1505 return -1;
1506
996c9314
LB
1507 if (!(n->flags & ZEBRA_NEIGH_REMOTE)
1508 || !(n->flags & ZEBRA_NEIGH_REMOTE_NH))
b7cfce93 1509 return 0;
931fa60c
MS
1510
1511 flags = DPLANE_NTF_EXT_LEARNED;
68e33151 1512 if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
931fa60c
MS
1513 flags |= DPLANE_NTF_ROUTER;
1514
f188e68e
AK
1515 dplane_rem_neigh_add(zl3vni->svi_if, &n->ip, &n->emac, flags,
1516 false /*was_static*/);
931fa60c 1517
68e33151 1518 return ret;
2dbad57f 1519}
b7cfce93
MK
1520
1521/*
1522 * Uninstall remote nh from the kernel.
1523 */
72de4110
DS
1524static int zl3vni_nh_uninstall(struct zebra_l3vni *zl3vni,
1525 struct zebra_neigh *n)
b7cfce93 1526{
996c9314
LB
1527 if (!(n->flags & ZEBRA_NEIGH_REMOTE)
1528 || !(n->flags & ZEBRA_NEIGH_REMOTE_NH))
b7cfce93
MK
1529 return 0;
1530
fa409e1e 1531 if (!zl3vni->svi_if || !if_is_operative(zl3vni->svi_if))
1532 return 0;
1533
f188e68e 1534 dplane_rem_neigh_delete(zl3vni->svi_if, &n->ip);
931fa60c
MS
1535
1536 return 0;
2dbad57f 1537}
1538
1539/* add remote vtep as a neigh entry */
05843a27 1540static int zl3vni_remote_nh_add(struct zebra_l3vni *zl3vni,
e4a1ec74
MS
1541 const struct ipaddr *vtep_ip,
1542 const struct ethaddr *rmac,
1543 const struct prefix *host_prefix)
2dbad57f 1544{
72de4110 1545 struct zebra_neigh *nh = NULL;
2dbad57f 1546
651fa905 1547 /* Create the next hop entry, or update its mac, if necessary. */
2dbad57f 1548 nh = zl3vni_nh_lookup(zl3vni, vtep_ip);
1549 if (!nh) {
1550 nh = zl3vni_nh_add(zl3vni, vtep_ip, rmac);
1551 if (!nh) {
9df414fe 1552 zlog_debug(
ef7b8be4
DL
1553 "Failed to add NH %pIA as Neigh (RMAC %pEA L3-VNI %u prefix %pFX)",
1554 vtep_ip, rmac, zl3vni->vni, host_prefix);
2dbad57f 1555 return -1;
1556 }
1557
1558 /* install the nh neigh in kernel */
1559 zl3vni_nh_install(zl3vni, nh);
651fa905
CS
1560 } else if (memcmp(&nh->emac, rmac, ETH_ALEN) != 0) {
1561 if (IS_ZEBRA_DEBUG_VXLAN)
2dbe669b 1562 zlog_debug(
ef7b8be4
DL
1563 "L3VNI %u RMAC change(%pEA --> %pEA) for nexthop %pIA, prefix %pFX",
1564 zl3vni->vni, &nh->emac, rmac, vtep_ip,
2dbe669b 1565 host_prefix);
651fa905
CS
1566
1567 memcpy(&nh->emac, rmac, ETH_ALEN);
1568 /* install (update) the nh neigh in kernel */
1569 zl3vni_nh_install(zl3vni, nh);
2dbad57f 1570 }
6134fd82 1571
f2a503f0 1572 rb_find_or_add_host(&nh->host_rb, host_prefix);
6134fd82 1573
2dbad57f 1574 return 0;
1575}
1576
1577/* handle nh neigh delete */
72de4110
DS
1578static void zl3vni_remote_nh_del(struct zebra_l3vni *zl3vni,
1579 struct zebra_neigh *nh,
22e63104 1580 struct prefix *host_prefix)
2dbad57f 1581{
f2a503f0 1582 rb_delete_host(&nh->host_rb, host_prefix);
2dbad57f 1583
85442b09 1584 if (RB_EMPTY(host_rb_tree_entry, &nh->host_rb)) {
2dbad57f 1585 /* uninstall from kernel */
1586 zl3vni_nh_uninstall(zl3vni, nh);
1587
1588 /* delete the nh entry */
1589 zl3vni_nh_del(zl3vni, nh);
1590 }
2dbad57f 1591}
b7cfce93 1592
3bcbba10 1593/* handle neigh update from kernel - the only thing of interest is to
1594 * readd stale entries.
1595 */
05843a27
DS
1596static int zl3vni_local_nh_add_update(struct zebra_l3vni *zl3vni,
1597 struct ipaddr *ip, uint16_t state)
3bcbba10 1598{
c05a738b 1599#ifdef GNU_LINUX
72de4110 1600 struct zebra_neigh *n = NULL;
3bcbba10 1601
1602 n = zl3vni_nh_lookup(zl3vni, ip);
1603 if (!n)
1604 return 0;
1605
1606 /* all next hop neigh are remote and installed by frr.
1607 * If the kernel has aged this entry, re-install.
1608 */
1609 if (state & NUD_STALE)
1610 zl3vni_nh_install(zl3vni, n);
c05a738b 1611#endif
3bcbba10 1612 return 0;
1613}
1614
8c9b80b9 1615/* handle neigh delete from kernel */
05843a27 1616static int zl3vni_local_nh_del(struct zebra_l3vni *zl3vni, struct ipaddr *ip)
8c9b80b9 1617{
72de4110 1618 struct zebra_neigh *n = NULL;
8c9b80b9
MK
1619
1620 n = zl3vni_nh_lookup(zl3vni, ip);
1621 if (!n)
1622 return 0;
1623
1624 /* all next hop neigh are remote and installed by frr.
1625 * If we get an age out notification for these neigh entries, we have to
523cafc4 1626 * install it back
1627 */
8c9b80b9
MK
1628 zl3vni_nh_install(zl3vni, n);
1629
1630 return 0;
1631}
1632
b7cfce93
MK
1633/*
1634 * Hash function for L3 VNI.
1635 */
d8b87afe 1636static unsigned int l3vni_hash_keymake(const void *p)
b7cfce93 1637{
05843a27 1638 const struct zebra_l3vni *zl3vni = p;
b7cfce93
MK
1639
1640 return jhash_1word(zl3vni->vni, 0);
1641}
1642
1643/*
1644 * Compare 2 L3 VNI hash entries.
1645 */
74df8d6d 1646static bool l3vni_hash_cmp(const void *p1, const void *p2)
b7cfce93 1647{
05843a27
DS
1648 const struct zebra_l3vni *zl3vni1 = p1;
1649 const struct zebra_l3vni *zl3vni2 = p2;
b7cfce93
MK
1650
1651 return (zl3vni1->vni == zl3vni2->vni);
1652}
1653
1654/*
1655 * Callback to allocate L3 VNI hash entry.
1656 */
1657static void *zl3vni_alloc(void *p)
1658{
05843a27
DS
1659 struct zebra_l3vni *zl3vni = NULL;
1660 const struct zebra_l3vni *tmp_l3vni = p;
b7cfce93 1661
05843a27 1662 zl3vni = XCALLOC(MTYPE_ZL3VNI, sizeof(struct zebra_l3vni));
b7cfce93
MK
1663 zl3vni->vni = tmp_l3vni->vni;
1664 return ((void *)zl3vni);
1665}
1666
1667/*
1668 * Look up L3 VNI hash entry.
1669 */
05843a27 1670struct zebra_l3vni *zl3vni_lookup(vni_t vni)
b7cfce93 1671{
05843a27
DS
1672 struct zebra_l3vni tmp_l3vni;
1673 struct zebra_l3vni *zl3vni = NULL;
b7cfce93 1674
6006b807 1675 memset(&tmp_l3vni, 0, sizeof(tmp_l3vni));
b7cfce93 1676 tmp_l3vni.vni = vni;
89272910 1677 zl3vni = hash_lookup(zrouter.l3vni_table, &tmp_l3vni);
b7cfce93
MK
1678
1679 return zl3vni;
1680}
1681
1682/*
1683 * Add L3 VNI hash entry.
1684 */
05843a27 1685static struct zebra_l3vni *zl3vni_add(vni_t vni, vrf_id_t vrf_id)
b7cfce93 1686{
05843a27
DS
1687 struct zebra_l3vni tmp_zl3vni;
1688 struct zebra_l3vni *zl3vni = NULL;
b7cfce93 1689
6006b807 1690 memset(&tmp_zl3vni, 0, sizeof(tmp_zl3vni));
b7cfce93
MK
1691 tmp_zl3vni.vni = vni;
1692
89272910 1693 zl3vni = hash_get(zrouter.l3vni_table, &tmp_zl3vni, zl3vni_alloc);
b7cfce93
MK
1694
1695 zl3vni->vrf_id = vrf_id;
1696 zl3vni->svi_if = NULL;
1697 zl3vni->vxlan_if = NULL;
1698 zl3vni->l2vnis = list_new();
8b5fdf2e 1699 zl3vni->l2vnis->cmp = zebra_evpn_list_cmp;
b7cfce93
MK
1700
1701 /* Create hash table for remote RMAC */
b2998086 1702 zl3vni->rmac_table = zebra_mac_db_create("Zebra L3-VNI RMAC-Table");
b7cfce93
MK
1703
1704 /* Create hash table for neighbors */
7cbae20a 1705 zl3vni->nh_table = zebra_neigh_db_create("Zebra L3-VNI next-hop table");
b7cfce93
MK
1706
1707 return zl3vni;
1708}
1709
1710/*
1711 * Delete L3 VNI hash entry.
1712 */
05843a27 1713static int zl3vni_del(struct zebra_l3vni *zl3vni)
b7cfce93 1714{
05843a27 1715 struct zebra_l3vni *tmp_zl3vni;
b7cfce93 1716
b7cfce93 1717 /* free the list of l2vnis */
6a154c88 1718 list_delete(&zl3vni->l2vnis);
b7cfce93
MK
1719 zl3vni->l2vnis = NULL;
1720
1721 /* Free the rmac table */
1722 hash_free(zl3vni->rmac_table);
1723 zl3vni->rmac_table = NULL;
1724
1725 /* Free the nh table */
1726 hash_free(zl3vni->nh_table);
1727 zl3vni->nh_table = NULL;
1728
1729 /* Free the VNI hash entry and allocated memory. */
89272910 1730 tmp_zl3vni = hash_release(zrouter.l3vni_table, zl3vni);
0a22ddfb 1731 XFREE(MTYPE_ZL3VNI, tmp_zl3vni);
b7cfce93
MK
1732
1733 return 0;
1734}
1735
2961d060
PG
1736static int zl3vni_map_to_vxlan_if_ns(struct ns *ns,
1737 void *_zl3vni,
1738 void **_pifp)
b7cfce93 1739{
2961d060 1740 struct zebra_ns *zns = ns->info;
05843a27 1741 struct zebra_l3vni *zl3vni = (struct zebra_l3vni *)_zl3vni;
b7cfce93
MK
1742 struct route_node *rn = NULL;
1743 struct interface *ifp = NULL;
07509878
PG
1744 struct zebra_vrf *zvrf;
1745
1746 zvrf = zebra_vrf_get_evpn();
1747
44a84850 1748 assert(_pifp);
1749
b7cfce93 1750 /* loop through all vxlan-interface */
b7cfce93
MK
1751 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
1752
1753 struct zebra_if *zif = NULL;
1754 struct zebra_l2info_vxlan *vxl = NULL;
1755
1756 ifp = (struct interface *)rn->info;
1757 if (!ifp)
1758 continue;
1759
1760 zif = ifp->info;
1761 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
1762 continue;
1763
1764 vxl = &zif->l2info.vxl;
07509878
PG
1765 if (vxl->vni != zl3vni->vni)
1766 continue;
1767
1768 /* link of VXLAN interface should be in zebra_evpn_vrf */
1769 if (zvrf->zns->ns_id != vxl->link_nsid) {
1770 if (IS_ZEBRA_DEBUG_VXLAN)
1771 zlog_debug(
1772 "Intf %s(%u) VNI %u, link not in same "
1773 "namespace than BGP EVPN core instance ",
1774 ifp->name, ifp->ifindex, vxl->vni);
1775 continue;
b67a60d2 1776 }
07509878
PG
1777
1778
1779 zl3vni->local_vtep_ip = vxl->vtep_ip;
44a84850 1780 *_pifp = (void *)ifp;
2961d060 1781 return NS_WALK_STOP;
b7cfce93
MK
1782 }
1783
2961d060 1784 return NS_WALK_CONTINUE;
07509878
PG
1785}
1786
05843a27 1787struct interface *zl3vni_map_to_vxlan_if(struct zebra_l3vni *zl3vni)
07509878
PG
1788{
1789 struct interface **p_ifp;
1790 struct interface *ifp = NULL;
1791
1792 p_ifp = &ifp;
1793
2961d060
PG
1794 ns_walk_func(zl3vni_map_to_vxlan_if_ns,
1795 (void *)zl3vni, (void **)p_ifp);
07509878 1796 return ifp;
b7cfce93
MK
1797}
1798
05843a27 1799struct interface *zl3vni_map_to_svi_if(struct zebra_l3vni *zl3vni)
b7cfce93 1800{
996c9314 1801 struct zebra_if *zif = NULL; /* zebra_if for vxlan_if */
b7cfce93
MK
1802 struct zebra_l2info_vxlan *vxl = NULL; /* l2 info for vxlan_if */
1803
2aeb403d 1804 if (!zl3vni)
1805 return NULL;
1806
b7cfce93
MK
1807 if (!zl3vni->vxlan_if)
1808 return NULL;
1809
1810 zif = zl3vni->vxlan_if->info;
1811 if (!zif)
1812 return NULL;
1813
1814 vxl = &zif->l2info.vxl;
1815
7cbae20a 1816 return zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
b7cfce93
MK
1817}
1818
05843a27 1819struct interface *zl3vni_map_to_mac_vlan_if(struct zebra_l3vni *zl3vni)
06d9cde5
CS
1820{
1821 struct zebra_if *zif = NULL; /* zebra_if for vxlan_if */
1822
1823 if (!zl3vni)
1824 return NULL;
1825
1826 if (!zl3vni->vxlan_if)
1827 return NULL;
1828
1829 zif = zl3vni->vxlan_if->info;
1830 if (!zif)
1831 return NULL;
1832
8b5fdf2e
PR
1833 return zebra_evpn_map_to_macvlan(zif->brslave_info.br_if,
1834 zl3vni->svi_if);
06d9cde5
CS
1835}
1836
1837
05843a27 1838struct zebra_l3vni *zl3vni_from_vrf(vrf_id_t vrf_id)
b7cfce93
MK
1839{
1840 struct zebra_vrf *zvrf = NULL;
1841
1842 zvrf = zebra_vrf_lookup_by_id(vrf_id);
1843 if (!zvrf)
1844 return NULL;
1845
1846 return zl3vni_lookup(zvrf->l3vni);
1847}
1848
24907262
PG
1849static int zl3vni_from_svi_ns(struct ns *ns, void *_in_param, void **_p_zl3vni)
1850{
24907262
PG
1851 struct zebra_ns *zns = ns->info;
1852 struct zebra_l3vni **p_zl3vni = (struct zebra_l3vni **)_p_zl3vni;
1853 struct zebra_from_svi_param *in_param =
1854 (struct zebra_from_svi_param *)_in_param;
1855 struct route_node *rn = NULL;
1856 struct interface *tmp_if = NULL;
1857 struct zebra_if *zif = NULL;
1858 struct zebra_l2info_vxlan *vxl = NULL;
1859
44a84850 1860 assert(in_param && p_zl3vni);
24907262
PG
1861
1862 /* loop through all vxlan-interface */
1863 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
1864 tmp_if = (struct interface *)rn->info;
1865 if (!tmp_if)
1866 continue;
1867 zif = tmp_if->info;
1868 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
1869 continue;
1870 if (!if_is_operative(tmp_if))
1871 continue;
1872 vxl = &zif->l2info.vxl;
1873
1874 if (zif->brslave_info.br_if != in_param->br_if)
1875 continue;
1876
1877 if (!in_param->bridge_vlan_aware
1878 || vxl->access_vlan == in_param->vid) {
44a84850 1879 *p_zl3vni = zl3vni_lookup(vxl->vni);
1880 return NS_WALK_STOP;
24907262
PG
1881 }
1882 }
1883
44a84850 1884 return NS_WALK_CONTINUE;
24907262
PG
1885}
1886
b7cfce93
MK
1887/*
1888 * Map SVI and associated bridge to a VNI. This is invoked upon getting
1889 * neighbor notifications, to see if they are of interest.
1890 */
05843a27
DS
1891static struct zebra_l3vni *zl3vni_from_svi(struct interface *ifp,
1892 struct interface *br_if)
b7cfce93 1893{
05843a27 1894 struct zebra_l3vni *zl3vni = NULL;
b7cfce93 1895 struct zebra_if *zif = NULL;
b7cfce93 1896 struct zebra_l2info_bridge *br = NULL;
24907262
PG
1897 struct zebra_from_svi_param in_param = {};
1898 struct zebra_l3vni **p_zl3vni;
b7cfce93
MK
1899
1900 if (!br_if)
1901 return NULL;
1902
1903 /* Make sure the linked interface is a bridge. */
1904 if (!IS_ZEBRA_IF_BRIDGE(br_if))
1905 return NULL;
24907262 1906 in_param.br_if = br_if;
b7cfce93
MK
1907
1908 /* Determine if bridge is VLAN-aware or not */
1909 zif = br_if->info;
1910 assert(zif);
1911 br = &zif->l2info.br;
24907262
PG
1912 in_param.bridge_vlan_aware = br->vlan_aware;
1913 if (in_param.bridge_vlan_aware) {
b7cfce93
MK
1914 struct zebra_l2info_vlan *vl;
1915
1916 if (!IS_ZEBRA_IF_VLAN(ifp))
1917 return NULL;
1918
1919 zif = ifp->info;
1920 assert(zif);
1921 vl = &zif->l2info.vl;
24907262 1922 in_param.vid = vl->vid;
b7cfce93
MK
1923 }
1924
1925 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
1926 /* TODO: Optimize with a hash. */
b7cfce93 1927
24907262 1928 p_zl3vni = &zl3vni;
b7cfce93 1929
24907262 1930 ns_walk_func(zl3vni_from_svi_ns, (void *)&in_param, (void **)p_zl3vni);
b7cfce93
MK
1931 return zl3vni;
1932}
1933
1b09e77e
AD
1934vni_t vni_id_from_svi(struct interface *ifp, struct interface *br_if)
1935{
1936 vni_t vni = 0;
f6371c34 1937 struct zebra_evpn *zevpn = NULL;
05843a27 1938 struct zebra_l3vni *zl3vni = NULL;
1b09e77e
AD
1939
1940 /* Check if an L3VNI belongs to this SVI interface.
1941 * If not, check if an L2VNI belongs to this SVI interface.
1942 */
1943 zl3vni = zl3vni_from_svi(ifp, br_if);
1944 if (zl3vni)
1945 vni = zl3vni->vni;
1946 else {
1947 zevpn = zebra_evpn_from_svi(ifp, br_if);
1948 if (zevpn)
1949 vni = zevpn->vni;
1950 }
1951
1952 return vni;
1953}
1954
05843a27 1955static inline void zl3vni_get_vrr_rmac(struct zebra_l3vni *zl3vni,
06d9cde5
CS
1956 struct ethaddr *rmac)
1957{
1958 if (!zl3vni)
1959 return;
1960
1961 if (!is_l3vni_oper_up(zl3vni))
1962 return;
1963
1964 if (zl3vni->mac_vlan_if && if_is_operative(zl3vni->mac_vlan_if))
1965 memcpy(rmac->octet, zl3vni->mac_vlan_if->hw_addr, ETH_ALEN);
1966}
1967
b7cfce93
MK
1968/*
1969 * Inform BGP about l3-vni.
1970 */
05843a27 1971static int zl3vni_send_add_to_client(struct zebra_l3vni *zl3vni)
b7cfce93
MK
1972{
1973 struct stream *s = NULL;
1974 struct zserv *client = NULL;
06d9cde5
CS
1975 struct ethaddr svi_rmac, vrr_rmac = {.octet = {0} };
1976 struct zebra_vrf *zvrf;
06d9cde5 1977 bool is_anycast_mac = true;
b7cfce93 1978
21ccc0cf 1979 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
b7cfce93
MK
1980 /* BGP may not be running. */
1981 if (!client)
1982 return 0;
1983
06d9cde5
CS
1984 zvrf = zebra_vrf_lookup_by_id(zl3vni->vrf_id);
1985 assert(zvrf);
1986
1987 /* get the svi and vrr rmac values */
6006b807 1988 memset(&svi_rmac, 0, sizeof(svi_rmac));
06d9cde5
CS
1989 zl3vni_get_svi_rmac(zl3vni, &svi_rmac);
1990 zl3vni_get_vrr_rmac(zl3vni, &vrr_rmac);
1991
1992 /* In absence of vrr mac use svi mac as anycast MAC value */
1993 if (is_zero_mac(&vrr_rmac)) {
1994 memcpy(&vrr_rmac, &svi_rmac, ETH_ALEN);
1995 is_anycast_mac = false;
1996 }
b7cfce93 1997
1002497a 1998 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
b7cfce93 1999
06d9cde5
CS
2000 /* The message is used for both vni add and/or update like
2001 * vrr mac is added for l3vni SVI.
2002 */
996c9314 2003 zclient_create_header(s, ZEBRA_L3VNI_ADD, zl3vni_vrf_id(zl3vni));
b7cfce93 2004 stream_putl(s, zl3vni->vni);
06d9cde5 2005 stream_put(s, &svi_rmac, sizeof(struct ethaddr));
b67a60d2 2006 stream_put_in_addr(s, &zl3vni->local_vtep_ip);
c48d9f5f 2007 stream_put(s, &zl3vni->filter, sizeof(int));
0483af6e 2008 stream_putl(s, zl3vni->svi_if->ifindex);
06d9cde5
CS
2009 stream_put(s, &vrr_rmac, sizeof(struct ethaddr));
2010 stream_putl(s, is_anycast_mac);
b7cfce93
MK
2011
2012 /* Write packet size. */
2013 stream_putw_at(s, 0, stream_get_endp(s));
2014
2015 if (IS_ZEBRA_DEBUG_VXLAN)
c48d9f5f 2016 zlog_debug(
ef7b8be4 2017 "Send L3_VNI_ADD %u VRF %s RMAC %pEA VRR %pEA local-ip %pI4 filter %s to %s",
996c9314 2018 zl3vni->vni, vrf_id_to_name(zl3vni_vrf_id(zl3vni)),
ef7b8be4 2019 &svi_rmac, &vrr_rmac, &zl3vni->local_vtep_ip,
996c9314
LB
2020 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
2021 ? "prefix-routes-only"
2022 : "none",
2023 zebra_route_string(client->proto));
b7cfce93
MK
2024
2025 client->l3vniadd_cnt++;
21ccc0cf 2026 return zserv_send_message(client, s);
b7cfce93
MK
2027}
2028
2029/*
2030 * Inform BGP about local l3-VNI deletion.
2031 */
05843a27 2032static int zl3vni_send_del_to_client(struct zebra_l3vni *zl3vni)
b7cfce93
MK
2033{
2034 struct stream *s = NULL;
2035 struct zserv *client = NULL;
2036
21ccc0cf 2037 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
b7cfce93
MK
2038 /* BGP may not be running. */
2039 if (!client)
2040 return 0;
2041
1002497a 2042 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
b7cfce93 2043
996c9314 2044 zclient_create_header(s, ZEBRA_L3VNI_DEL, zl3vni_vrf_id(zl3vni));
b7cfce93
MK
2045 stream_putl(s, zl3vni->vni);
2046
2047 /* Write packet size. */
2048 stream_putw_at(s, 0, stream_get_endp(s));
2049
2050 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 2051 zlog_debug("Send L3_VNI_DEL %u VRF %s to %s", zl3vni->vni,
b7cfce93
MK
2052 vrf_id_to_name(zl3vni_vrf_id(zl3vni)),
2053 zebra_route_string(client->proto));
2054
2055 client->l3vnidel_cnt++;
21ccc0cf 2056 return zserv_send_message(client, s);
b7cfce93
MK
2057}
2058
05843a27 2059static void zebra_vxlan_process_l3vni_oper_up(struct zebra_l3vni *zl3vni)
b7cfce93 2060{
2aeb403d 2061 if (!zl3vni)
2062 return;
2063
b7cfce93
MK
2064 /* send l3vni add to BGP */
2065 zl3vni_send_add_to_client(zl3vni);
2066}
2067
05843a27 2068static void zebra_vxlan_process_l3vni_oper_down(struct zebra_l3vni *zl3vni)
b7cfce93 2069{
2aeb403d 2070 if (!zl3vni)
2071 return;
2072
b7cfce93
MK
2073 /* send l3-vni del to BGP*/
2074 zl3vni_send_del_to_client(zl3vni);
2075}
2076
7cbae20a 2077static void zevpn_add_to_l3vni_list(struct hash_bucket *bucket, void *ctxt)
b169fd6f 2078{
f6371c34 2079 struct zebra_evpn *zevpn = (struct zebra_evpn *)bucket->data;
05843a27 2080 struct zebra_l3vni *zl3vni = (struct zebra_l3vni *)ctxt;
b169fd6f 2081
7cbae20a
PR
2082 if (zevpn->vrf_id == zl3vni_vrf_id(zl3vni))
2083 listnode_add_sort(zl3vni->l2vnis, zevpn);
b169fd6f
AK
2084}
2085
7cbae20a 2086/*
3b0a590b
AD
2087 * Handle transition of vni from l2 to l3 and vice versa.
2088 * This function handles only the L2VNI add/delete part of
2089 * the above transition.
2090 * L3VNI add/delete is handled by the calling functions.
7cbae20a
PR
2091 */
2092static int zebra_vxlan_handle_vni_transition(struct zebra_vrf *zvrf, vni_t vni,
2093 int add)
b169fd6f 2094{
f6371c34 2095 struct zebra_evpn *zevpn = NULL;
df78d91d 2096 struct zebra_l3vni *zl3vni = NULL;
b169fd6f 2097
7cbae20a
PR
2098 /* There is a possibility that VNI notification was already received
2099 * from kernel and we programmed it as L2-VNI
2100 * In such a case we need to delete this L2-VNI first, so
2101 * that it can be reprogrammed as L3-VNI in the system. It is also
2102 * possible that the vrf-vni mapping is removed from FRR while the vxlan
2103 * interface is still present in kernel. In this case to keep it
2104 * symmetric, we will delete the l3-vni and reprogram it as l2-vni
2105 */
2106 if (add) {
2107 /* Locate hash entry */
8b5fdf2e 2108 zevpn = zebra_evpn_lookup(vni);
7cbae20a
PR
2109 if (!zevpn)
2110 return 0;
b169fd6f 2111
7cbae20a
PR
2112 if (IS_ZEBRA_DEBUG_VXLAN)
2113 zlog_debug("Del L2-VNI %u - transition to L3-VNI", vni);
b169fd6f 2114
7cbae20a 2115 /* Delete EVPN from BGP. */
8b5fdf2e 2116 zebra_evpn_send_del_to_client(zevpn);
b169fd6f 2117
7cbae20a
PR
2118 zebra_evpn_neigh_del_all(zevpn, 0, 0, DEL_ALL_NEIGH);
2119 zebra_evpn_mac_del_all(zevpn, 0, 0, DEL_ALL_MAC);
b169fd6f 2120
7cbae20a 2121 /* Free up all remote VTEPs, if any. */
2fe5a02e 2122 zebra_evpn_vtep_del_all(zevpn, 1);
b169fd6f 2123
df78d91d 2124 zl3vni = zl3vni_from_vrf(zevpn->vrf_id);
2125 if (zl3vni)
2126 listnode_delete(zl3vni->l2vnis, zevpn);
2127
7cbae20a 2128 /* Delete the hash entry. */
8b5fdf2e 2129 if (zebra_evpn_vxlan_del(zevpn)) {
7cbae20a
PR
2130 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
2131 "Failed to del EVPN hash %p, VNI %u", zevpn,
2132 zevpn->vni);
2133 return -1;
b169fd6f 2134 }
7cbae20a 2135 } else {
3b0a590b
AD
2136 struct zebra_ns *zns;
2137 struct route_node *rn;
2138 struct interface *ifp;
2139 struct zebra_if *zif;
2140 struct zebra_l2info_vxlan *vxl;
2141 struct interface *vlan_if;
2142 bool found = false;
2143
2144 if (IS_ZEBRA_DEBUG_VXLAN)
2145 zlog_debug("Adding L2-VNI %u - transition from L3-VNI",
2146 vni);
2147
2148 /* Find VxLAN interface for this VNI. */
2149 zns = zebra_ns_lookup(NS_DEFAULT);
2150 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
2151 ifp = (struct interface *)rn->info;
2152 if (!ifp)
2153 continue;
2154 zif = ifp->info;
2155 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
2156 continue;
2157
2158 vxl = &zif->l2info.vxl;
2159 if (vxl->vni == vni) {
2160 found = true;
2161 break;
2162 }
2163 }
2164
2165 if (!found) {
2166 if (IS_ZEBRA_DEBUG_VXLAN)
2167 zlog_err(
2168 "Adding L2-VNI - Failed to find VxLAN interface for VNI %u",
2169 vni);
2170 return -1;
2171 }
2172
2173 /* Create VNI hash entry for L2VNI */
2174 zevpn = zebra_evpn_lookup(vni);
2175 if (zevpn)
2176 return 0;
2177
2178 zevpn = zebra_evpn_add(vni);
3b0a590b
AD
2179
2180 /* Find bridge interface for the VNI */
2181 vlan_if = zvni_map_to_svi(vxl->access_vlan,
2182 zif->brslave_info.br_if);
df78d91d 2183 if (vlan_if) {
096f7609 2184 zevpn->vrf_id = vlan_if->vrf->vrf_id;
df78d91d 2185 zl3vni = zl3vni_from_vrf(vlan_if->vrf->vrf_id);
2186 if (zl3vni)
2187 listnode_add_sort_nodup(zl3vni->l2vnis, zevpn);
2188 }
3b0a590b
AD
2189
2190 zevpn->vxlan_if = ifp;
2191 zevpn->local_vtep_ip = vxl->vtep_ip;
2192
2193 /* Inform BGP if the VNI is up and mapped to a bridge. */
2194 if (if_is_operative(ifp) && zif->brslave_info.br_if) {
2195 zebra_evpn_send_add_to_client(zevpn);
2196 zebra_evpn_read_mac_neigh(zevpn, ifp);
2197 }
b169fd6f
AK
2198 }
2199
7cbae20a 2200 return 0;
b169fd6f
AK
2201}
2202
7cbae20a
PR
2203/* delete and uninstall rmac hash entry */
2204static void zl3vni_del_rmac_hash_entry(struct hash_bucket *bucket, void *ctx)
b169fd6f 2205{
3198b2b3 2206 struct zebra_mac *zrmac = NULL;
05843a27 2207 struct zebra_l3vni *zl3vni = NULL;
b169fd6f 2208
3198b2b3 2209 zrmac = (struct zebra_mac *)bucket->data;
05843a27 2210 zl3vni = (struct zebra_l3vni *)ctx;
7cbae20a 2211 zl3vni_rmac_uninstall(zl3vni, zrmac);
b169fd6f 2212
7cbae20a
PR
2213 /* Send RMAC for FPM processing */
2214 hook_call(zebra_rmac_update, zrmac, zl3vni, true, "RMAC deleted");
b169fd6f 2215
7cbae20a 2216 zl3vni_rmac_del(zl3vni, zrmac);
b169fd6f
AK
2217}
2218
7cbae20a
PR
2219/* delete and uninstall nh hash entry */
2220static void zl3vni_del_nh_hash_entry(struct hash_bucket *bucket, void *ctx)
b169fd6f 2221{
72de4110 2222 struct zebra_neigh *n = NULL;
05843a27 2223 struct zebra_l3vni *zl3vni = NULL;
b169fd6f 2224
72de4110 2225 n = (struct zebra_neigh *)bucket->data;
05843a27 2226 zl3vni = (struct zebra_l3vni *)ctx;
7cbae20a
PR
2227 zl3vni_nh_uninstall(zl3vni, n);
2228 zl3vni_nh_del(zl3vni, n);
2229}
b169fd6f 2230
7cbae20a 2231/* re-add remote rmac if needed */
05843a27 2232static int zebra_vxlan_readd_remote_rmac(struct zebra_l3vni *zl3vni,
7cbae20a
PR
2233 struct ethaddr *rmac)
2234{
3198b2b3 2235 struct zebra_mac *zrmac = NULL;
7cbae20a
PR
2236
2237 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
2238 if (!zrmac)
2239 return 0;
2240
2241 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
2242 zlog_debug("Del remote RMAC %pEA L3VNI %u - readd",
2243 rmac, zl3vni->vni);
7cbae20a
PR
2244
2245 zl3vni_rmac_install(zl3vni, zrmac);
2246 return 0;
b169fd6f
AK
2247}
2248
b7cfce93
MK
2249/* Public functions */
2250
c48d9f5f
MK
2251int is_l3vni_for_prefix_routes_only(vni_t vni)
2252{
05843a27 2253 struct zebra_l3vni *zl3vni = NULL;
c48d9f5f
MK
2254
2255 zl3vni = zl3vni_lookup(vni);
2256 if (!zl3vni)
2257 return 0;
2258
2259 return CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY) ? 1 : 0;
2260}
2261
2dbad57f 2262/* handle evpn route in vrf table */
e4a1ec74
MS
2263void zebra_vxlan_evpn_vrf_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac,
2264 const struct ipaddr *vtep_ip,
2265 const struct prefix *host_prefix)
2dbad57f 2266{
05843a27 2267 struct zebra_l3vni *zl3vni = NULL;
f50dc5e6 2268 struct ipaddr ipv4_vtep;
2dbad57f 2269
2270 zl3vni = zl3vni_from_vrf(vrf_id);
2271 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
2272 return;
2273
3518f352 2274 /*
f50dc5e6
MK
2275 * add the next hop neighbor -
2276 * neigh to be installed is the ipv6 nexthop neigh
2277 */
3518f352 2278 zl3vni_remote_nh_add(zl3vni, vtep_ip, rmac, host_prefix);
2dbad57f 2279
f50dc5e6
MK
2280 /*
2281 * if the remote vtep is a ipv4 mapped ipv6 address convert it to ipv4
2282 * address. Rmac is programmed against the ipv4 vtep because we only
2283 * support ipv4 tunnels in the h/w right now
2284 */
6006b807 2285 memset(&ipv4_vtep, 0, sizeof(ipv4_vtep));
f50dc5e6
MK
2286 ipv4_vtep.ipa_type = IPADDR_V4;
2287 if (vtep_ip->ipa_type == IPADDR_V6)
2288 ipv4_mapped_ipv6_to_ipv4(&vtep_ip->ipaddr_v6,
2289 &(ipv4_vtep.ipaddr_v4));
2290 else
2291 memcpy(&(ipv4_vtep.ipaddr_v4), &vtep_ip->ipaddr_v4,
2292 sizeof(struct in_addr));
2293
3518f352
DS
2294 /*
2295 * add the rmac - remote rmac to be installed is against the ipv4
f50dc5e6
MK
2296 * nexthop address
2297 */
3d43b95c 2298 zl3vni_remote_rmac_add(zl3vni, rmac, &ipv4_vtep);
2dbad57f 2299}
2300
2301/* handle evpn vrf route delete */
22e63104 2302void zebra_vxlan_evpn_vrf_route_del(vrf_id_t vrf_id,
996c9314
LB
2303 struct ipaddr *vtep_ip,
2304 struct prefix *host_prefix)
2dbad57f 2305{
05843a27 2306 struct zebra_l3vni *zl3vni = NULL;
72de4110 2307 struct zebra_neigh *nh = NULL;
3198b2b3 2308 struct zebra_mac *zrmac = NULL;
2dbad57f 2309
2310 zl3vni = zl3vni_from_vrf(vrf_id);
5e06422c 2311 if (!zl3vni)
2dbad57f 2312 return;
2313
22e63104 2314 /* find the next hop entry and rmac entry */
2315 nh = zl3vni_nh_lookup(zl3vni, vtep_ip);
2316 if (!nh)
2317 return;
2318 zrmac = zl3vni_rmac_lookup(zl3vni, &nh->emac);
2319
2dbad57f 2320 /* delete the next hop entry */
22e63104 2321 zl3vni_remote_nh_del(zl3vni, nh, host_prefix);
2dbad57f 2322
2323 /* delete the rmac entry */
22e63104 2324 if (zrmac)
3d43b95c 2325 zl3vni_remote_rmac_del(zl3vni, zrmac, vtep_ip);
2dbad57f 2326}
2327
996c9314 2328void zebra_vxlan_print_specific_rmac_l3vni(struct vty *vty, vni_t l3vni,
9f049418 2329 struct ethaddr *rmac, bool use_json)
9aa741ea 2330{
05843a27 2331 struct zebra_l3vni *zl3vni = NULL;
3198b2b3 2332 struct zebra_mac *zrmac = NULL;
316f4ca4 2333 json_object *json = NULL;
9aa741ea 2334
316f4ca4
MK
2335 if (!is_evpn_enabled()) {
2336 if (use_json)
2337 vty_out(vty, "{}\n");
9aa741ea 2338 return;
316f4ca4
MK
2339 }
2340
2341 if (use_json)
2342 json = json_object_new_object();
9aa741ea
MK
2343
2344 zl3vni = zl3vni_lookup(l3vni);
2345 if (!zl3vni) {
316f4ca4
MK
2346 if (use_json)
2347 vty_out(vty, "{}\n");
2348 else
0437e105 2349 vty_out(vty, "%% L3-VNI %u doesn't exist\n", l3vni);
9aa741ea
MK
2350 return;
2351 }
2352
2353 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
2354 if (!zrmac) {
316f4ca4
MK
2355 if (use_json)
2356 vty_out(vty, "{}\n");
2357 else
2358 vty_out(vty,
db889978 2359 "%% Requested RMAC doesn't exist in L3-VNI %u\n",
316f4ca4 2360 l3vni);
9aa741ea
MK
2361 return;
2362 }
2363
316f4ca4
MK
2364 zl3vni_print_rmac(zrmac, vty, json);
2365
c48349e3 2366 if (use_json)
962af8a8 2367 vty_json(vty, json);
9aa741ea 2368}
2dbad57f 2369
9f049418 2370void zebra_vxlan_print_rmacs_l3vni(struct vty *vty, vni_t l3vni, bool use_json)
b7cfce93 2371{
05843a27 2372 struct zebra_l3vni *zl3vni;
d7c0a89a 2373 uint32_t num_rmacs;
b7cfce93
MK
2374 struct rmac_walk_ctx wctx;
2375 json_object *json = NULL;
b7cfce93
MK
2376
2377 if (!is_evpn_enabled())
2378 return;
2379
2380 zl3vni = zl3vni_lookup(l3vni);
2381 if (!zl3vni) {
2382 if (use_json)
2383 vty_out(vty, "{}\n");
2384 else
2385 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
2386 return;
2387 }
2388 num_rmacs = hashcount(zl3vni->rmac_table);
2389 if (!num_rmacs)
2390 return;
2391
75223c9e 2392 if (use_json)
b7cfce93 2393 json = json_object_new_object();
b7cfce93 2394
6006b807 2395 memset(&wctx, 0, sizeof(wctx));
b7cfce93 2396 wctx.vty = vty;
75223c9e 2397 wctx.json = json;
b7cfce93 2398 if (!use_json) {
996c9314 2399 vty_out(vty, "Number of Remote RMACs known for this VNI: %u\n",
b7cfce93 2400 num_rmacs);
4cce389e 2401 vty_out(vty, "%-17s %-21s\n", "MAC", "Remote VTEP");
b7cfce93
MK
2402 } else
2403 json_object_int_add(json, "numRmacs", num_rmacs);
2404
2405 hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
2406
c48349e3 2407 if (use_json)
962af8a8 2408 vty_json(vty, json);
b7cfce93
MK
2409}
2410
9f049418 2411void zebra_vxlan_print_rmacs_all_l3vni(struct vty *vty, bool use_json)
b7cfce93 2412{
b7cfce93 2413 json_object *json = NULL;
c0b4eaa4 2414 void *args[2];
b7cfce93
MK
2415
2416 if (!is_evpn_enabled()) {
2417 if (use_json)
2418 vty_out(vty, "{}\n");
2419 return;
2420 }
2421
b7cfce93
MK
2422 if (use_json)
2423 json = json_object_new_object();
2424
c0b4eaa4
MK
2425 args[0] = vty;
2426 args[1] = json;
89272910 2427 hash_iterate(zrouter.l3vni_table,
e3b78da8 2428 (void (*)(struct hash_bucket *,
c0b4eaa4
MK
2429 void *))zl3vni_print_rmac_hash_all_vni,
2430 args);
b7cfce93 2431
c48349e3 2432 if (use_json)
962af8a8 2433 vty_json(vty, json);
b7cfce93
MK
2434}
2435
996c9314 2436void zebra_vxlan_print_specific_nh_l3vni(struct vty *vty, vni_t l3vni,
9f049418 2437 struct ipaddr *ip, bool use_json)
9aa741ea 2438{
05843a27 2439 struct zebra_l3vni *zl3vni = NULL;
72de4110 2440 struct zebra_neigh *n = NULL;
c0e519d3 2441 json_object *json = NULL;
9aa741ea 2442
c0e519d3
MK
2443 if (!is_evpn_enabled()) {
2444 if (use_json)
2445 vty_out(vty, "{}\n");
9aa741ea 2446 return;
c0e519d3
MK
2447 }
2448
2449 if (use_json)
2450 json = json_object_new_object();
9aa741ea
MK
2451
2452 zl3vni = zl3vni_lookup(l3vni);
2453 if (!zl3vni) {
c0e519d3
MK
2454 if (use_json)
2455 vty_out(vty, "{}\n");
2456 else
2457 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
9aa741ea
MK
2458 return;
2459 }
2460
2461 n = zl3vni_nh_lookup(zl3vni, ip);
2462 if (!n) {
c0e519d3
MK
2463 if (use_json)
2464 vty_out(vty, "{}\n");
2465 else
2466 vty_out(vty,
2467 "%% Requested next-hop not present for L3-VNI %u",
2468 l3vni);
9aa741ea
MK
2469 return;
2470 }
2471
c0e519d3
MK
2472 zl3vni_print_nh(n, vty, json);
2473
c48349e3 2474 if (use_json)
962af8a8 2475 vty_json(vty, json);
9aa741ea
MK
2476}
2477
9f049418 2478void zebra_vxlan_print_nh_l3vni(struct vty *vty, vni_t l3vni, bool use_json)
b7cfce93 2479{
d7c0a89a 2480 uint32_t num_nh;
2dbad57f 2481 struct nh_walk_ctx wctx;
b7cfce93 2482 json_object *json = NULL;
05843a27 2483 struct zebra_l3vni *zl3vni = NULL;
b7cfce93
MK
2484
2485 if (!is_evpn_enabled())
2486 return;
2487
2488 zl3vni = zl3vni_lookup(l3vni);
2489 if (!zl3vni) {
2490 if (use_json)
2491 vty_out(vty, "{}\n");
2492 else
2493 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
2494 return;
2495 }
2496
2497 num_nh = hashcount(zl3vni->nh_table);
2498 if (!num_nh)
2499 return;
2500
9187f600 2501 if (use_json)
b7cfce93 2502 json = json_object_new_object();
b7cfce93 2503
2dbad57f 2504 wctx.vty = vty;
9187f600 2505 wctx.json = json;
b7cfce93 2506 if (!use_json) {
996c9314 2507 vty_out(vty, "Number of NH Neighbors known for this VNI: %u\n",
b7cfce93 2508 num_nh);
4cce389e 2509 vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
b7cfce93 2510 } else
4cce389e 2511 json_object_int_add(json, "numNextHops", num_nh);
b7cfce93
MK
2512
2513 hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
2514
c48349e3 2515 if (use_json)
962af8a8 2516 vty_json(vty, json);
b7cfce93
MK
2517}
2518
9f049418 2519void zebra_vxlan_print_nh_all_l3vni(struct vty *vty, bool use_json)
b7cfce93 2520{
2dbad57f 2521 json_object *json = NULL;
32798965 2522 void *args[2];
2dbad57f 2523
2524 if (!is_evpn_enabled()) {
2525 if (use_json)
2526 vty_out(vty, "{}\n");
2527 return;
2528 }
2529
2dbad57f 2530 if (use_json)
2531 json = json_object_new_object();
2532
32798965
MK
2533 args[0] = vty;
2534 args[1] = json;
89272910 2535 hash_iterate(zrouter.l3vni_table,
e3b78da8 2536 (void (*)(struct hash_bucket *,
32798965
MK
2537 void *))zl3vni_print_nh_hash_all_vni,
2538 args);
2dbad57f 2539
c48349e3 2540 if (use_json)
962af8a8 2541 vty_json(vty, json);
b7cfce93
MK
2542}
2543
2544/*
2545 * Display L3 VNI information (VTY command handler).
2546 */
9f049418 2547void zebra_vxlan_print_l3vni(struct vty *vty, vni_t vni, bool use_json)
b7cfce93
MK
2548{
2549 void *args[2];
2550 json_object *json = NULL;
05843a27 2551 struct zebra_l3vni *zl3vni = NULL;
b7cfce93 2552
b2ee5a13
MK
2553 if (!is_evpn_enabled()) {
2554 if (use_json)
2555 vty_out(vty, "{}\n");
b7cfce93 2556 return;
b2ee5a13 2557 }
b7cfce93
MK
2558
2559 zl3vni = zl3vni_lookup(vni);
2560 if (!zl3vni) {
2561 if (use_json)
2562 vty_out(vty, "{}\n");
2563 else
2564 vty_out(vty, "%% VNI %u does not exist\n", vni);
2565 return;
2566 }
2567
2568 if (use_json)
2569 json = json_object_new_object();
2570
b2ee5a13
MK
2571 args[0] = vty;
2572 args[1] = json;
b7cfce93
MK
2573 zl3vni_print(zl3vni, (void *)args);
2574
c48349e3 2575 if (use_json)
962af8a8 2576 vty_json(vty, json);
b7cfce93
MK
2577}
2578
4cce389e
MK
2579void zebra_vxlan_print_vrf_vni(struct vty *vty, struct zebra_vrf *zvrf,
2580 json_object *json_vrfs)
b7cfce93 2581{
4cce389e 2582 char buf[ETHER_ADDR_STRLEN];
05843a27 2583 struct zebra_l3vni *zl3vni = NULL;
b7cfce93 2584
4cce389e
MK
2585 zl3vni = zl3vni_lookup(zvrf->l3vni);
2586 if (!zl3vni)
b7cfce93 2587 return;
b7cfce93 2588
4cce389e
MK
2589 if (!json_vrfs) {
2590 vty_out(vty, "%-37s %-10u %-20s %-20s %-5s %-18s\n",
996c9314 2591 zvrf_name(zvrf), zl3vni->vni,
4cce389e 2592 zl3vni_vxlan_if_name(zl3vni),
996c9314 2593 zl3vni_svi_if_name(zl3vni), zl3vni_state2str(zl3vni),
4cce389e 2594 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
b7cfce93 2595 } else {
4cce389e 2596 json_object *json_vrf = NULL;
9df2b997 2597
4cce389e 2598 json_vrf = json_object_new_object();
996c9314 2599 json_object_string_add(json_vrf, "vrf", zvrf_name(zvrf));
4cce389e
MK
2600 json_object_int_add(json_vrf, "vni", zl3vni->vni);
2601 json_object_string_add(json_vrf, "vxlanIntf",
2602 zl3vni_vxlan_if_name(zl3vni));
2603 json_object_string_add(json_vrf, "sviIntf",
2604 zl3vni_svi_if_name(zl3vni));
2605 json_object_string_add(json_vrf, "state",
2606 zl3vni_state2str(zl3vni));
996c9314
LB
2607 json_object_string_add(
2608 json_vrf, "routerMac",
2609 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
4cce389e 2610 json_object_array_add(json_vrfs, json_vrf);
b7cfce93
MK
2611 }
2612}
2613
2614/*
2615 * Display Neighbors for a VNI (VTY command handler).
2616 */
2617void zebra_vxlan_print_neigh_vni(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 2618 vni_t vni, bool use_json)
b7cfce93 2619{
f6371c34 2620 struct zebra_evpn *zevpn;
d7c0a89a 2621 uint32_t num_neigh;
b7cfce93
MK
2622 struct neigh_walk_ctx wctx;
2623 json_object *json = NULL;
2624
2625 if (!is_evpn_enabled())
2626 return;
8b5fdf2e 2627 zevpn = zebra_evpn_lookup(vni);
87d76d54 2628 if (!zevpn) {
b7cfce93
MK
2629 if (use_json)
2630 vty_out(vty, "{}\n");
2631 else
2632 vty_out(vty, "%% VNI %u does not exist\n", vni);
2633 return;
2634 }
87d76d54 2635 num_neigh = hashcount(zevpn->neigh_table);
b7cfce93
MK
2636 if (!num_neigh)
2637 return;
2638
2639 if (use_json)
2640 json = json_object_new_object();
2641
2642 /* Since we have IPv6 addresses to deal with which can vary widely in
2643 * size, we try to be a bit more elegant in display by first computing
2644 * the maximum width.
2645 */
6006b807 2646 memset(&wctx, 0, sizeof(wctx));
87d76d54 2647 wctx.zevpn = zevpn;
b7cfce93
MK
2648 wctx.vty = vty;
2649 wctx.addr_width = 15;
2650 wctx.json = json;
7cbae20a
PR
2651 hash_iterate(zevpn->neigh_table, zebra_evpn_find_neigh_addr_width,
2652 &wctx);
b7cfce93
MK
2653
2654 if (!use_json) {
2655 vty_out(vty,
2656 "Number of ARPs (local and remote) known for this VNI: %u\n",
2657 num_neigh);
7cbae20a 2658 zebra_evpn_print_neigh_hdr(vty, &wctx);
b7cfce93
MK
2659 } else
2660 json_object_int_add(json, "numArpNd", num_neigh);
2661
7cbae20a 2662 hash_iterate(zevpn->neigh_table, zebra_evpn_print_neigh_hash, &wctx);
c48349e3 2663 if (use_json)
962af8a8 2664 vty_json(vty, json);
b7cfce93
MK
2665}
2666
2667/*
2668 * Display neighbors across all VNIs (VTY command handler).
2669 */
2670void zebra_vxlan_print_neigh_all_vni(struct vty *vty, struct zebra_vrf *zvrf,
1374d4db 2671 bool print_dup, bool use_json)
b7cfce93
MK
2672{
2673 json_object *json = NULL;
1374d4db 2674 void *args[3];
b7cfce93
MK
2675
2676 if (!is_evpn_enabled())
2677 return;
2678
2679 if (use_json)
2680 json = json_object_new_object();
2681
2682 args[0] = vty;
2683 args[1] = json;
1374d4db
CS
2684 args[2] = (void *)(ptrdiff_t)print_dup;
2685
87d76d54 2686 hash_iterate(zvrf->evpn_table,
e3b78da8 2687 (void (*)(struct hash_bucket *,
87d76d54 2688 void *))zevpn_print_neigh_hash_all_evpn,
b7cfce93 2689 args);
c48349e3 2690 if (use_json)
962af8a8 2691 vty_json(vty, json);
b7cfce93
MK
2692}
2693
e3fac919
NS
2694/*
2695 * Display neighbors across all VNIs in detail(VTY command handler).
2696 */
2697void zebra_vxlan_print_neigh_all_vni_detail(struct vty *vty,
2698 struct zebra_vrf *zvrf,
2699 bool print_dup, bool use_json)
2700{
2701 json_object *json = NULL;
2702 void *args[3];
2703
2704 if (!is_evpn_enabled())
2705 return;
2706
2707 if (use_json)
2708 json = json_object_new_object();
2709
2710 args[0] = vty;
2711 args[1] = json;
2712 args[2] = (void *)(ptrdiff_t)print_dup;
2713
87d76d54 2714 hash_iterate(zvrf->evpn_table,
e3b78da8 2715 (void (*)(struct hash_bucket *,
87d76d54 2716 void *))zevpn_print_neigh_hash_all_evpn_detail,
e3fac919 2717 args);
c48349e3 2718 if (use_json)
962af8a8 2719 vty_json(vty, json);
e3fac919
NS
2720}
2721
b7cfce93
MK
2722/*
2723 * Display specific neighbor for a VNI, if present (VTY command handler).
2724 */
2725void zebra_vxlan_print_specific_neigh_vni(struct vty *vty,
2726 struct zebra_vrf *zvrf, vni_t vni,
9f049418 2727 struct ipaddr *ip, bool use_json)
b7cfce93 2728{
f6371c34 2729 struct zebra_evpn *zevpn;
72de4110 2730 struct zebra_neigh *n;
b7cfce93
MK
2731 json_object *json = NULL;
2732
2733 if (!is_evpn_enabled())
2734 return;
8b5fdf2e 2735 zevpn = zebra_evpn_lookup(vni);
87d76d54 2736 if (!zevpn) {
b7cfce93 2737 if (use_json)
cd233079
CS
2738 vty_out(vty, "{}\n");
2739 else
2740 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 2741 return;
2742 }
7cbae20a 2743 n = zebra_evpn_neigh_lookup(zevpn, ip);
d62a17ae 2744 if (!n) {
cd233079
CS
2745 if (!use_json)
2746 vty_out(vty,
2747 "%% Requested neighbor does not exist in VNI %u\n",
2748 vni);
d62a17ae 2749 return;
2750 }
cd233079
CS
2751 if (use_json)
2752 json = json_object_new_object();
2753
7cbae20a 2754 zebra_evpn_print_neigh(n, vty, json);
cec2e17d 2755
c48349e3 2756 if (use_json)
962af8a8 2757 vty_json(vty, json);
cec2e17d 2758}
2759
2760/*
2761 * Display neighbors for a VNI from specific VTEP (VTY command handler).
2762 * By definition, these are remote neighbors.
2763 */
d62a17ae 2764void zebra_vxlan_print_neigh_vni_vtep(struct vty *vty, struct zebra_vrf *zvrf,
cd233079 2765 vni_t vni, struct in_addr vtep_ip,
9f049418 2766 bool use_json)
cec2e17d 2767{
f6371c34 2768 struct zebra_evpn *zevpn;
d7c0a89a 2769 uint32_t num_neigh;
d62a17ae 2770 struct neigh_walk_ctx wctx;
cd233079 2771 json_object *json = NULL;
cec2e17d 2772
2853fed6 2773 if (!is_evpn_enabled())
d62a17ae 2774 return;
8b5fdf2e 2775 zevpn = zebra_evpn_lookup(vni);
87d76d54 2776 if (!zevpn) {
cd233079
CS
2777 if (use_json)
2778 vty_out(vty, "{}\n");
2779 else
2780 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 2781 return;
2782 }
87d76d54 2783 num_neigh = hashcount(zevpn->neigh_table);
d62a17ae 2784 if (!num_neigh)
2785 return;
cec2e17d 2786
1cc5b093
PR
2787 if (use_json)
2788 json = json_object_new_object();
2789
6006b807 2790 memset(&wctx, 0, sizeof(wctx));
87d76d54 2791 wctx.zevpn = zevpn;
d62a17ae 2792 wctx.vty = vty;
68e33151 2793 wctx.addr_width = 15;
d62a17ae 2794 wctx.flags = SHOW_REMOTE_NEIGH_FROM_VTEP;
2795 wctx.r_vtep_ip = vtep_ip;
cd233079 2796 wctx.json = json;
7cbae20a
PR
2797 hash_iterate(zevpn->neigh_table, zebra_evpn_find_neigh_addr_width,
2798 &wctx);
2799 hash_iterate(zevpn->neigh_table, zebra_evpn_print_neigh_hash, &wctx);
cd233079 2800
c48349e3 2801 if (use_json)
962af8a8 2802 vty_json(vty, json);
cec2e17d 2803}
2804
1374d4db
CS
2805/*
2806 * Display Duplicate detected Neighbors for a VNI
2807 * (VTY command handler).
2808 */
2809void zebra_vxlan_print_neigh_vni_dad(struct vty *vty,
2810 struct zebra_vrf *zvrf,
2811 vni_t vni,
2812 bool use_json)
2813{
f6371c34 2814 struct zebra_evpn *zevpn;
1374d4db
CS
2815 uint32_t num_neigh;
2816 struct neigh_walk_ctx wctx;
2817 json_object *json = NULL;
2818
2819 if (!is_evpn_enabled())
2820 return;
2821
8b5fdf2e 2822 zevpn = zebra_evpn_lookup(vni);
87d76d54 2823 if (!zevpn) {
1374d4db
CS
2824 vty_out(vty, "%% VNI %u does not exist\n", vni);
2825 return;
2826 }
2827
87d76d54 2828 num_neigh = hashcount(zevpn->neigh_table);
1374d4db
CS
2829 if (!num_neigh)
2830 return;
2831
87d76d54 2832 num_neigh = num_dup_detected_neighs(zevpn);
1374d4db
CS
2833 if (!num_neigh)
2834 return;
2835
2836 if (use_json)
2837 json = json_object_new_object();
2838
2839 /* Since we have IPv6 addresses to deal with which can vary widely in
2840 * size, we try to be a bit more elegant in display by first computing
2841 * the maximum width.
2842 */
6006b807 2843 memset(&wctx, 0, sizeof(wctx));
87d76d54 2844 wctx.zevpn = zevpn;
1374d4db
CS
2845 wctx.vty = vty;
2846 wctx.addr_width = 15;
2847 wctx.json = json;
7cbae20a
PR
2848 hash_iterate(zevpn->neigh_table, zebra_evpn_find_neigh_addr_width,
2849 &wctx);
1374d4db
CS
2850
2851 if (!use_json) {
2852 vty_out(vty,
2853 "Number of ARPs (local and remote) known for this VNI: %u\n",
2854 num_neigh);
ce5160c0 2855 vty_out(vty, "%*s %-6s %-8s %-17s %-30s\n",
1374d4db 2856 -wctx.addr_width, "IP", "Type",
ce5160c0 2857 "State", "MAC", "Remote ES/VTEP");
1374d4db
CS
2858 } else
2859 json_object_int_add(json, "numArpNd", num_neigh);
2860
7cbae20a
PR
2861 hash_iterate(zevpn->neigh_table, zebra_evpn_print_dad_neigh_hash,
2862 &wctx);
1374d4db 2863
c48349e3 2864 if (use_json)
962af8a8 2865 vty_json(vty, json);
1374d4db
CS
2866}
2867
cec2e17d 2868/*
2869 * Display MACs for a VNI (VTY command handler).
2870 */
d62a17ae 2871void zebra_vxlan_print_macs_vni(struct vty *vty, struct zebra_vrf *zvrf,
283ef1b0 2872 vni_t vni, bool use_json, bool detail)
cec2e17d 2873{
f6371c34 2874 struct zebra_evpn *zevpn;
d7c0a89a 2875 uint32_t num_macs;
d62a17ae 2876 struct mac_walk_ctx wctx;
cd233079
CS
2877 json_object *json = NULL;
2878 json_object *json_mac = NULL;
cec2e17d 2879
2853fed6 2880 if (!is_evpn_enabled())
d62a17ae 2881 return;
8b5fdf2e 2882 zevpn = zebra_evpn_lookup(vni);
87d76d54 2883 if (!zevpn) {
cd233079
CS
2884 if (use_json)
2885 vty_out(vty, "{}\n");
2886 else
2887 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 2888 return;
2889 }
87d76d54 2890 num_macs = num_valid_macs(zevpn);
d62a17ae 2891 if (!num_macs)
2892 return;
cec2e17d 2893
cd233079
CS
2894 if (use_json) {
2895 json = json_object_new_object();
2896 json_mac = json_object_new_object();
2897 }
2898
6006b807 2899 memset(&wctx, 0, sizeof(wctx));
87d76d54 2900 wctx.zevpn = zevpn;
d62a17ae 2901 wctx.vty = vty;
cd233079 2902 wctx.json = json_mac;
cec2e17d 2903
cd233079 2904 if (!use_json) {
283ef1b0
PJD
2905 if (detail) {
2906 vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
2907 zevpn->vni, num_macs);
2908 } else {
2909 vty_out(vty,
2910 "Number of MACs (local and remote) known for this VNI: %u\n",
2911 num_macs);
2912 vty_out(vty,
2913 "Flags: N=sync-neighs, I=local-inactive, P=peer-active, X=peer-proxy\n");
2914 vty_out(vty, "%-17s %-6s %-5s %-30s %-5s %s\n", "MAC",
2915 "Type", "Flags", "Intf/Remote ES/VTEP", "VLAN",
2916 "Seq #'s");
2917 }
cd233079
CS
2918 } else
2919 json_object_int_add(json, "numMacs", num_macs);
cec2e17d 2920
283ef1b0
PJD
2921 if (detail)
2922 hash_iterate(zevpn->mac_table, zebra_evpn_print_mac_hash_detail,
2923 &wctx);
2924 else
2925 hash_iterate(zevpn->mac_table, zebra_evpn_print_mac_hash,
2926 &wctx);
cd233079
CS
2927
2928 if (use_json) {
2929 json_object_object_add(json, "macs", json_mac);
962af8a8 2930 vty_json(vty, json);
cd233079 2931 }
cec2e17d 2932}
2933
2934/*
2935 * Display MACs for all VNIs (VTY command handler).
2936 */
cd233079 2937void zebra_vxlan_print_macs_all_vni(struct vty *vty, struct zebra_vrf *zvrf,
1374d4db 2938 bool print_dup, bool use_json)
cec2e17d 2939{
d62a17ae 2940 struct mac_walk_ctx wctx;
cd233079 2941 json_object *json = NULL;
cec2e17d 2942
2853fed6 2943 if (!is_evpn_enabled()) {
cd233079
CS
2944 if (use_json)
2945 vty_out(vty, "{}\n");
d62a17ae 2946 return;
cd233079
CS
2947 }
2948 if (use_json)
2949 json = json_object_new_object();
2950
6006b807 2951 memset(&wctx, 0, sizeof(wctx));
d62a17ae 2952 wctx.vty = vty;
cd233079 2953 wctx.json = json;
1374d4db 2954 wctx.print_dup = print_dup;
87d76d54 2955 hash_iterate(zvrf->evpn_table, zevpn_print_mac_hash_all_evpn, &wctx);
cd233079 2956
c48349e3 2957 if (use_json)
962af8a8 2958 vty_json(vty, json);
cec2e17d 2959}
2960
cffe7580
NS
2961/*
2962 * Display MACs in detail for all VNIs (VTY command handler).
2963 */
2964void zebra_vxlan_print_macs_all_vni_detail(struct vty *vty,
2965 struct zebra_vrf *zvrf,
2966 bool print_dup, bool use_json)
2967{
2968 struct mac_walk_ctx wctx;
2969 json_object *json = NULL;
2970
2971 if (!is_evpn_enabled()) {
2972 if (use_json)
2973 vty_out(vty, "{}\n");
2974 return;
2975 }
2976 if (use_json)
2977 json = json_object_new_object();
2978
6006b807 2979 memset(&wctx, 0, sizeof(wctx));
cffe7580
NS
2980 wctx.vty = vty;
2981 wctx.json = json;
2982 wctx.print_dup = print_dup;
87d76d54 2983 hash_iterate(zvrf->evpn_table, zevpn_print_mac_hash_all_evpn_detail,
cffe7580
NS
2984 &wctx);
2985
c48349e3 2986 if (use_json)
962af8a8 2987 vty_json(vty, json);
cffe7580
NS
2988}
2989
cec2e17d 2990/*
2991 * Display MACs for all VNIs (VTY command handler).
2992 */
d62a17ae 2993void zebra_vxlan_print_macs_all_vni_vtep(struct vty *vty,
2994 struct zebra_vrf *zvrf,
9f049418 2995 struct in_addr vtep_ip, bool use_json)
cec2e17d 2996{
d62a17ae 2997 struct mac_walk_ctx wctx;
cd233079 2998 json_object *json = NULL;
cec2e17d 2999
2853fed6 3000 if (!is_evpn_enabled())
d62a17ae 3001 return;
cd233079
CS
3002
3003 if (use_json)
3004 json = json_object_new_object();
3005
6006b807 3006 memset(&wctx, 0, sizeof(wctx));
d62a17ae 3007 wctx.vty = vty;
3008 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
3009 wctx.r_vtep_ip = vtep_ip;
cd233079 3010 wctx.json = json;
87d76d54 3011 hash_iterate(zvrf->evpn_table, zevpn_print_mac_hash_all_evpn, &wctx);
cd233079 3012
c48349e3 3013 if (use_json)
962af8a8 3014 vty_json(vty, json);
cec2e17d 3015}
3016
3017/*
3018 * Display specific MAC for a VNI, if present (VTY command handler).
3019 */
d62a17ae 3020void zebra_vxlan_print_specific_mac_vni(struct vty *vty, struct zebra_vrf *zvrf,
24cdbd0d
DS
3021 vni_t vni, struct ethaddr *macaddr,
3022 bool use_json)
cec2e17d 3023{
f6371c34 3024 struct zebra_evpn *zevpn;
3198b2b3 3025 struct zebra_mac *mac;
24cdbd0d 3026 json_object *json = NULL;
cec2e17d 3027
2853fed6 3028 if (!is_evpn_enabled())
d62a17ae 3029 return;
24cdbd0d 3030
8b5fdf2e 3031 zevpn = zebra_evpn_lookup(vni);
87d76d54 3032 if (!zevpn) {
24cdbd0d
DS
3033 if (use_json)
3034 vty_out(vty, "{}\n");
3035 else
3036 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 3037 return;
3038 }
b2998086 3039 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
d62a17ae 3040 if (!mac) {
24cdbd0d
DS
3041 if (use_json)
3042 vty_out(vty, "{}\n");
3043 else
3044 vty_out(vty,
3045 "%% Requested MAC does not exist in VNI %u\n",
3046 vni);
d62a17ae 3047 return;
3048 }
cec2e17d 3049
24cdbd0d
DS
3050 if (use_json)
3051 json = json_object_new_object();
3052
b2998086 3053 zebra_evpn_print_mac(mac, vty, json);
c48349e3 3054 if (use_json)
962af8a8 3055 vty_json(vty, json);
cec2e17d 3056}
3057
1374d4db
CS
3058/* Print Duplicate MACs per VNI */
3059void zebra_vxlan_print_macs_vni_dad(struct vty *vty,
3060 struct zebra_vrf *zvrf,
3061 vni_t vni, bool use_json)
3062{
f6371c34 3063 struct zebra_evpn *zevpn;
1374d4db
CS
3064 struct mac_walk_ctx wctx;
3065 uint32_t num_macs;
3066 json_object *json = NULL;
3067 json_object *json_mac = NULL;
3068
3069 if (!is_evpn_enabled())
3070 return;
3071
8b5fdf2e 3072 zevpn = zebra_evpn_lookup(vni);
87d76d54 3073 if (!zevpn) {
1374d4db
CS
3074 vty_out(vty, "%% VNI %u does not exist\n", vni);
3075 return;
3076 }
3077
87d76d54 3078 num_macs = num_valid_macs(zevpn);
1374d4db
CS
3079 if (!num_macs)
3080 return;
3081
87d76d54 3082 num_macs = num_dup_detected_macs(zevpn);
1374d4db
CS
3083 if (!num_macs)
3084 return;
3085
3086 if (use_json) {
3087 json = json_object_new_object();
3088 json_mac = json_object_new_object();
3089 }
3090
6006b807 3091 memset(&wctx, 0, sizeof(wctx));
87d76d54 3092 wctx.zevpn = zevpn;
1374d4db
CS
3093 wctx.vty = vty;
3094 wctx.json = json_mac;
3095
3096 if (!use_json) {
3097 vty_out(vty,
3098 "Number of MACs (local and remote) known for this VNI: %u\n",
3099 num_macs);
b169fd6f
AK
3100 vty_out(vty, "%-17s %-6s %-5s %-30s %-5s\n", "MAC", "Type",
3101 "Flags", "Intf/Remote ES/VTEP", "VLAN");
1374d4db
CS
3102 } else
3103 json_object_int_add(json, "numMacs", num_macs);
3104
b2998086 3105 hash_iterate(zevpn->mac_table, zebra_evpn_print_dad_mac_hash, &wctx);
1374d4db
CS
3106
3107 if (use_json) {
3108 json_object_object_add(json, "macs", json_mac);
962af8a8 3109 vty_json(vty, json);
1374d4db
CS
3110 }
3111
3112}
3113
e20755b2 3114int zebra_vxlan_clear_dup_detect_vni_mac(struct zebra_vrf *zvrf, vni_t vni,
9bee0232
CS
3115 struct ethaddr *macaddr, char *errmsg,
3116 size_t errmsg_len)
09de9258 3117{
f6371c34 3118 struct zebra_evpn *zevpn;
3198b2b3 3119 struct zebra_mac *mac;
09de9258 3120 struct listnode *node = NULL;
72de4110 3121 struct zebra_neigh *nbr = NULL;
09de9258
CS
3122
3123 if (!is_evpn_enabled())
e20755b2 3124 return 0;
1883de66 3125
8b5fdf2e 3126 zevpn = zebra_evpn_lookup(vni);
87d76d54 3127 if (!zevpn) {
9bee0232 3128 snprintfrr(errmsg, errmsg_len, "VNI %u does not exist", vni);
e20755b2 3129 return -1;
09de9258
CS
3130 }
3131
b2998086 3132 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
09de9258 3133 if (!mac) {
9bee0232
CS
3134 snprintf(errmsg, errmsg_len,
3135 "Requested MAC does not exist in VNI %u\n", vni);
e20755b2 3136 return -1;
09de9258
CS
3137 }
3138
3139 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
9bee0232
CS
3140 snprintfrr(errmsg, errmsg_len,
3141 "Requested MAC is not duplicate detected\n");
e20755b2 3142 return -1;
09de9258
CS
3143 }
3144
3145 /* Remove all IPs as duplicate associcated with this MAC */
3146 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
3147 /* For local neigh mark inactive so MACIP update is generated
3148 * to BGP. This is a scenario where MAC update received
3149 * and detected as duplicate which marked neigh as duplicate.
3150 * Later local neigh update did not get a chance to relay
3151 * to BGP. Similarly remote macip update, neigh needs to be
3152 * installed locally.
3153 */
d4199657
CS
3154 if (zvrf->dad_freeze &&
3155 CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
09de9258
CS
3156 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
3157 ZEBRA_NEIGH_SET_INACTIVE(nbr);
3158 else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE))
b2998086
PR
3159 zebra_evpn_rem_neigh_install(
3160 zevpn, nbr, false /*was_static*/);
09de9258
CS
3161 }
3162
3163 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
3164 nbr->dad_count = 0;
3165 nbr->detect_start_time.tv_sec = 0;
3166 nbr->dad_dup_detect_time = 0;
3167 }
3168
3169 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
3170 mac->dad_count = 0;
3171 mac->detect_start_time.tv_sec = 0;
3172 mac->detect_start_time.tv_usec = 0;
3173 mac->dad_dup_detect_time = 0;
3174 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
3175
d4199657
CS
3176 /* warn-only action return */
3177 if (!zvrf->dad_freeze)
e20755b2 3178 return 0;
d4199657 3179
09de9258
CS
3180 /* Local: Notify Peer VTEPs, Remote: Install the entry */
3181 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
3182 /* Inform to BGP */
b2998086
PR
3183 if (zebra_evpn_mac_send_add_to_client(zevpn->vni, &mac->macaddr,
3184 mac->flags, mac->loc_seq,
3185 mac->es))
e20755b2 3186 return 0;
09de9258
CS
3187
3188 /* Process all neighbors associated with this MAC. */
b2998086
PR
3189 zebra_evpn_process_neigh_on_local_mac_change(zevpn, mac, 0,
3190 0 /*es_change*/);
09de9258
CS
3191
3192 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
b2998086 3193 zebra_evpn_process_neigh_on_remote_mac_add(zevpn, mac);
09de9258
CS
3194
3195 /* Install the entry. */
b2998086 3196 zebra_evpn_rem_mac_install(zevpn, mac, false /* was_static */);
09de9258
CS
3197 }
3198
e20755b2 3199 return 0;
09de9258
CS
3200}
3201
e20755b2 3202int zebra_vxlan_clear_dup_detect_vni_ip(struct zebra_vrf *zvrf, vni_t vni,
9bee0232
CS
3203 struct ipaddr *ip, char *errmsg,
3204 size_t errmsg_len)
09de9258 3205{
f6371c34 3206 struct zebra_evpn *zevpn;
72de4110 3207 struct zebra_neigh *nbr;
3198b2b3 3208 struct zebra_mac *mac;
09de9258
CS
3209 char buf[INET6_ADDRSTRLEN];
3210 char buf2[ETHER_ADDR_STRLEN];
3211
3212 if (!is_evpn_enabled())
e20755b2 3213 return 0;
09de9258 3214
8b5fdf2e 3215 zevpn = zebra_evpn_lookup(vni);
87d76d54 3216 if (!zevpn) {
9bee0232 3217 snprintfrr(errmsg, errmsg_len, "VNI %u does not exist\n", vni);
e20755b2 3218 return -1;
09de9258
CS
3219 }
3220
7cbae20a 3221 nbr = zebra_evpn_neigh_lookup(zevpn, ip);
09de9258 3222 if (!nbr) {
9bee0232
CS
3223 snprintfrr(errmsg, errmsg_len,
3224 "Requested host IP does not exist in VNI %u\n", vni);
e20755b2 3225 return -1;
09de9258
CS
3226 }
3227
3228 ipaddr2str(&nbr->ip, buf, sizeof(buf));
3229
3230 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
9bee0232
CS
3231 snprintfrr(errmsg, errmsg_len,
3232 "Requested host IP %s is not duplicate detected\n",
3233 buf);
e20755b2 3234 return -1;
09de9258
CS
3235 }
3236
b2998086 3237 mac = zebra_evpn_mac_lookup(zevpn, &nbr->emac);
09de9258
CS
3238
3239 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
9bee0232
CS
3240 snprintfrr(
3241 errmsg, errmsg_len,
e20755b2 3242 "Requested IP's associated MAC %s is still in duplicate state\n",
09de9258 3243 prefix_mac2str(&nbr->emac, buf2, sizeof(buf2)));
e20755b2 3244 return -1;
09de9258
CS
3245 }
3246
3247 if (IS_ZEBRA_DEBUG_VXLAN)
3248 zlog_debug("%s: clear neigh %s in dup state, flags 0x%x seq %u",
15569c58 3249 __func__, buf, nbr->flags, nbr->loc_seq);
09de9258
CS
3250
3251 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
3252 nbr->dad_count = 0;
3253 nbr->detect_start_time.tv_sec = 0;
3254 nbr->detect_start_time.tv_usec = 0;
3255 nbr->dad_dup_detect_time = 0;
3256 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
3257
3258 if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
7cbae20a
PR
3259 zebra_evpn_neigh_send_add_to_client(zevpn->vni, ip, &nbr->emac,
3260 nbr->mac, nbr->flags,
3261 nbr->loc_seq);
09de9258 3262 } else if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
b2998086 3263 zebra_evpn_rem_neigh_install(zevpn, nbr, false /*was_static*/);
09de9258
CS
3264 }
3265
e20755b2 3266 return 0;
09de9258
CS
3267}
3268
87d76d54 3269static void zevpn_clear_dup_mac_hash(struct hash_bucket *bucket, void *ctxt)
09de9258
CS
3270{
3271 struct mac_walk_ctx *wctx = ctxt;
3198b2b3 3272 struct zebra_mac *mac;
f6371c34 3273 struct zebra_evpn *zevpn;
09de9258 3274 struct listnode *node = NULL;
72de4110 3275 struct zebra_neigh *nbr = NULL;
09de9258 3276
3198b2b3 3277 mac = (struct zebra_mac *)bucket->data;
09de9258
CS
3278 if (!mac)
3279 return;
3280
87d76d54 3281 zevpn = wctx->zevpn;
09de9258
CS
3282
3283 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
3284 return;
3285
3286 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
3287 mac->dad_count = 0;
3288 mac->detect_start_time.tv_sec = 0;
3289 mac->detect_start_time.tv_usec = 0;
3290 mac->dad_dup_detect_time = 0;
3291 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
3292
3293 /* Remove all IPs as duplicate associcated with this MAC */
3294 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
3295 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)
3296 && nbr->dad_count)
3297 ZEBRA_NEIGH_SET_INACTIVE(nbr);
3298
3299 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
3300 nbr->dad_count = 0;
3301 nbr->detect_start_time.tv_sec = 0;
3302 nbr->dad_dup_detect_time = 0;
3303 }
3304
3305 /* Local: Notify Peer VTEPs, Remote: Install the entry */
3306 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
3307 /* Inform to BGP */
b2998086
PR
3308 if (zebra_evpn_mac_send_add_to_client(zevpn->vni, &mac->macaddr,
3309 mac->flags, mac->loc_seq,
3310 mac->es))
09de9258
CS
3311 return;
3312
3313 /* Process all neighbors associated with this MAC. */
b2998086
PR
3314 zebra_evpn_process_neigh_on_local_mac_change(zevpn, mac, 0,
3315 0 /*es_change*/);
09de9258
CS
3316
3317 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
b2998086 3318 zebra_evpn_process_neigh_on_remote_mac_add(zevpn, mac);
09de9258
CS
3319
3320 /* Install the entry. */
b2998086 3321 zebra_evpn_rem_mac_install(zevpn, mac, false /* was_static */);
09de9258
CS
3322 }
3323}
3324
87d76d54 3325static void zevpn_clear_dup_detect_hash_vni_all(struct hash_bucket *bucket,
09de9258
CS
3326 void **args)
3327{
f6371c34 3328 struct zebra_evpn *zevpn;
09de9258
CS
3329 struct zebra_vrf *zvrf;
3330 struct mac_walk_ctx m_wctx;
3331 struct neigh_walk_ctx n_wctx;
3332
f6371c34 3333 zevpn = (struct zebra_evpn *)bucket->data;
87d76d54 3334 if (!zevpn)
09de9258
CS
3335 return;
3336
e20755b2 3337 zvrf = (struct zebra_vrf *)args[0];
09de9258 3338
87d76d54 3339 if (hashcount(zevpn->neigh_table)) {
6006b807 3340 memset(&n_wctx, 0, sizeof(n_wctx));
87d76d54 3341 n_wctx.zevpn = zevpn;
09de9258 3342 n_wctx.zvrf = zvrf;
7cbae20a
PR
3343 hash_iterate(zevpn->neigh_table,
3344 zebra_evpn_clear_dup_neigh_hash, &n_wctx);
09de9258
CS
3345 }
3346
87d76d54 3347 if (num_valid_macs(zevpn)) {
6006b807 3348 memset(&m_wctx, 0, sizeof(m_wctx));
87d76d54 3349 m_wctx.zevpn = zevpn;
09de9258 3350 m_wctx.zvrf = zvrf;
87d76d54 3351 hash_iterate(zevpn->mac_table, zevpn_clear_dup_mac_hash, &m_wctx);
09de9258
CS
3352 }
3353
3354}
3355
e20755b2 3356int zebra_vxlan_clear_dup_detect_vni_all(struct zebra_vrf *zvrf)
09de9258 3357{
e20755b2 3358 void *args[1];
09de9258
CS
3359
3360 if (!is_evpn_enabled())
e20755b2 3361 return 0;
09de9258 3362
e20755b2 3363 args[0] = zvrf;
09de9258 3364
87d76d54 3365 hash_iterate(zvrf->evpn_table,
e3b78da8 3366 (void (*)(struct hash_bucket *, void *))
87d76d54 3367 zevpn_clear_dup_detect_hash_vni_all, args);
09de9258 3368
e20755b2 3369 return 0;
09de9258
CS
3370}
3371
e20755b2 3372int zebra_vxlan_clear_dup_detect_vni(struct zebra_vrf *zvrf, vni_t vni)
09de9258 3373{
f6371c34 3374 struct zebra_evpn *zevpn;
09de9258
CS
3375 struct mac_walk_ctx m_wctx;
3376 struct neigh_walk_ctx n_wctx;
3377
3378 if (!is_evpn_enabled())
e20755b2 3379 return 0;
09de9258 3380
8b5fdf2e 3381 zevpn = zebra_evpn_lookup(vni);
87d76d54 3382 if (!zevpn) {
1d5453d6 3383 zlog_warn("VNI %u does not exist", vni);
87d76d54 3384 return CMD_WARNING;
09de9258
CS
3385 }
3386
87d76d54 3387 if (hashcount(zevpn->neigh_table)) {
6006b807 3388 memset(&n_wctx, 0, sizeof(n_wctx));
87d76d54 3389 n_wctx.zevpn = zevpn;
09de9258 3390 n_wctx.zvrf = zvrf;
7cbae20a
PR
3391 hash_iterate(zevpn->neigh_table,
3392 zebra_evpn_clear_dup_neigh_hash, &n_wctx);
09de9258
CS
3393 }
3394
87d76d54 3395 if (num_valid_macs(zevpn)) {
6006b807 3396 memset(&m_wctx, 0, sizeof(m_wctx));
87d76d54 3397 m_wctx.zevpn = zevpn;
09de9258 3398 m_wctx.zvrf = zvrf;
87d76d54 3399 hash_iterate(zevpn->mac_table, zevpn_clear_dup_mac_hash, &m_wctx);
09de9258
CS
3400 }
3401
e20755b2 3402 return 0;
09de9258
CS
3403}
3404
cec2e17d 3405/*
3406 * Display MACs for a VNI from specific VTEP (VTY command handler).
3407 */
d62a17ae 3408void zebra_vxlan_print_macs_vni_vtep(struct vty *vty, struct zebra_vrf *zvrf,
cd233079 3409 vni_t vni, struct in_addr vtep_ip,
9f049418 3410 bool use_json)
cec2e17d 3411{
f6371c34 3412 struct zebra_evpn *zevpn;
d7c0a89a 3413 uint32_t num_macs;
d62a17ae 3414 struct mac_walk_ctx wctx;
cd233079
CS
3415 json_object *json = NULL;
3416 json_object *json_mac = NULL;
cec2e17d 3417
2853fed6 3418 if (!is_evpn_enabled())
d62a17ae 3419 return;
8b5fdf2e 3420 zevpn = zebra_evpn_lookup(vni);
87d76d54 3421 if (!zevpn) {
cd233079
CS
3422 if (use_json)
3423 vty_out(vty, "{}\n");
3424 else
3425 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 3426 return;
3427 }
87d76d54 3428 num_macs = num_valid_macs(zevpn);
d62a17ae 3429 if (!num_macs)
3430 return;
cd233079
CS
3431
3432 if (use_json) {
3433 json = json_object_new_object();
3434 json_mac = json_object_new_object();
3435 }
3436
6006b807 3437 memset(&wctx, 0, sizeof(wctx));
87d76d54 3438 wctx.zevpn = zevpn;
d62a17ae 3439 wctx.vty = vty;
3440 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
3441 wctx.r_vtep_ip = vtep_ip;
cd233079 3442 wctx.json = json_mac;
b2998086 3443 hash_iterate(zevpn->mac_table, zebra_evpn_print_mac_hash, &wctx);
cd233079
CS
3444
3445 if (use_json) {
3446 json_object_int_add(json, "numMacs", wctx.count);
3447 if (wctx.count)
3448 json_object_object_add(json, "macs", json_mac);
962af8a8 3449 vty_json(vty, json);
cd233079 3450 }
cec2e17d 3451}
3452
3453
3454/*
3455 * Display VNI information (VTY command handler).
06931fdb
LK
3456 *
3457 * use_json flag indicates that output should be in JSON format.
3458 * json_array is non NULL when JSON output needs to be aggregated (by the
3459 * caller) and then printed, otherwise, JSON evpn vni info is printed
3460 * right away.
cec2e17d 3461 */
cd233079 3462void zebra_vxlan_print_vni(struct vty *vty, struct zebra_vrf *zvrf, vni_t vni,
06931fdb 3463 bool use_json, json_object *json_array)
cec2e17d 3464{
cd233079
CS
3465 json_object *json = NULL;
3466 void *args[2];
05843a27 3467 struct zebra_l3vni *zl3vni = NULL;
f6371c34 3468 struct zebra_evpn *zevpn = NULL;
cec2e17d 3469
2853fed6 3470 if (!is_evpn_enabled())
d62a17ae 3471 return;
4cce389e 3472
cd233079
CS
3473 if (use_json)
3474 json = json_object_new_object();
06931fdb 3475
cd233079
CS
3476 args[0] = vty;
3477 args[1] = json;
4cce389e 3478
1f2129ec 3479 zl3vni = zl3vni_lookup(vni);
3480 if (zl3vni) {
4cce389e
MK
3481 zl3vni_print(zl3vni, (void *)args);
3482 } else {
8b5fdf2e 3483 zevpn = zebra_evpn_lookup(vni);
87d76d54 3484 if (zevpn)
8b5fdf2e 3485 zebra_evpn_print(zevpn, (void *)args);
06931fdb
LK
3486 else if (!json)
3487 vty_out(vty, "%% VNI %u does not exist\n", vni);
4cce389e
MK
3488 }
3489
cd233079 3490 if (use_json) {
06931fdb
LK
3491 /*
3492 * Each "json" object contains info about 1 VNI.
3493 * When "json_array" is non-null, we aggreggate the json output
3494 * into json_array and print it as a JSON array.
3495 */
3496 if (json_array)
3497 json_object_array_add(json_array, json);
c48349e3 3498 else
962af8a8 3499 vty_json(vty, json);
cd233079 3500 }
cec2e17d 3501}
3502
4cce389e 3503/* Display all global details for EVPN */
088f1098 3504void zebra_vxlan_print_evpn(struct vty *vty, bool uj)
cec2e17d 3505{
4cce389e
MK
3506 int num_l2vnis = 0;
3507 int num_l3vnis = 0;
d4454626 3508 int num_vnis = 0;
cd233079 3509 json_object *json = NULL;
4cce389e 3510 struct zebra_vrf *zvrf = NULL;
cec2e17d 3511
2853fed6 3512 if (!is_evpn_enabled())
d62a17ae 3513 return;
4cce389e 3514
530db8dc 3515 zvrf = zebra_vrf_get_evpn();
4cce389e 3516
89272910 3517 num_l3vnis = hashcount(zrouter.l3vni_table);
87d76d54 3518 num_l2vnis = hashcount(zvrf->evpn_table);
d4454626 3519 num_vnis = num_l2vnis + num_l3vnis;
4cce389e
MK
3520
3521 if (uj) {
cd233079 3522 json = json_object_new_object();
ddd16ed5
MK
3523 json_object_string_add(json, "advertiseGatewayMacip",
3524 zvrf->advertise_gw_macip ? "Yes" : "No");
826beeff
SPG
3525 json_object_string_add(json, "advertiseSviMacip",
3526 zvrf->advertise_svi_macip ? "Yes"
3527 : "No");
3528 json_object_string_add(json, "advertiseSviMac",
3529 zebra_evpn_mh_do_adv_svi_mac() ? "Yes"
3530 : "No");
d4454626 3531 json_object_int_add(json, "numVnis", num_vnis);
4cce389e
MK
3532 json_object_int_add(json, "numL2Vnis", num_l2vnis);
3533 json_object_int_add(json, "numL3Vnis", num_l3vnis);
b2ee2b71 3534 if (zebra_evpn_do_dup_addr_detect(zvrf))
61d46eda
CS
3535 json_object_boolean_true_add(json,
3536 "isDuplicateAddrDetection");
3537 else
3538 json_object_boolean_false_add(json,
3539 "isDuplicateAddrDetection");
3540 json_object_int_add(json, "maxMoves", zvrf->dad_max_moves);
3541 json_object_int_add(json, "detectionTime", zvrf->dad_time);
3542 json_object_int_add(json, "detectionFreezeTime",
3543 zvrf->dad_freeze_time);
c36e442c 3544 zebra_evpn_mh_json(json);
cd233079 3545 } else {
4cce389e
MK
3546 vty_out(vty, "L2 VNIs: %u\n", num_l2vnis);
3547 vty_out(vty, "L3 VNIs: %u\n", num_l3vnis);
ddd16ed5
MK
3548 vty_out(vty, "Advertise gateway mac-ip: %s\n",
3549 zvrf->advertise_gw_macip ? "Yes" : "No");
278e26de
CS
3550 vty_out(vty, "Advertise svi mac-ip: %s\n",
3551 zvrf->advertise_svi_macip ? "Yes" : "No");
243b74ed
AK
3552 vty_out(vty, "Advertise svi mac: %s\n",
3553 zebra_evpn_mh_do_adv_svi_mac() ? "Yes" : "No");
61d46eda 3554 vty_out(vty, "Duplicate address detection: %s\n",
b2ee2b71
AK
3555 zebra_evpn_do_dup_addr_detect(zvrf) ? "Enable"
3556 : "Disable");
61d46eda
CS
3557 vty_out(vty, " Detection max-moves %u, time %d\n",
3558 zvrf->dad_max_moves, zvrf->dad_time);
3559 if (zvrf->dad_freeze) {
3560 if (zvrf->dad_freeze_time)
3561 vty_out(vty, " Detection freeze %u\n",
3562 zvrf->dad_freeze_time);
3563 else
3564 vty_out(vty, " Detection freeze %s\n",
3565 "permanent");
3566 }
c36e442c 3567 zebra_evpn_mh_print(vty);
cd233079 3568 }
4cce389e 3569
c48349e3 3570 if (uj)
962af8a8 3571 vty_json(vty, json);
4cce389e
MK
3572}
3573
3574/*
3575 * Display VNI hash table (VTY command handler).
3576 */
3577void zebra_vxlan_print_vnis(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 3578 bool use_json)
4cce389e
MK
3579{
3580 json_object *json = NULL;
4cce389e
MK
3581 void *args[2];
3582
3583 if (!is_evpn_enabled())
3584 return;
3585
4cce389e
MK
3586 if (use_json)
3587 json = json_object_new_object();
3588 else
996c9314
LB
3589 vty_out(vty, "%-10s %-4s %-21s %-8s %-8s %-15s %-37s\n", "VNI",
3590 "Type", "VxLAN IF", "# MACs", "# ARPs",
3591 "# Remote VTEPs", "Tenant VRF");
4cce389e 3592
cd233079
CS
3593 args[0] = vty;
3594 args[1] = json;
3595
4cce389e 3596 /* Display all L2-VNIs */
8b5fdf2e
PR
3597 hash_iterate(
3598 zvrf->evpn_table,
3599 (void (*)(struct hash_bucket *, void *))zebra_evpn_print_hash,
3600 args);
cd233079 3601
4cce389e 3602 /* Display all L3-VNIs */
89272910 3603 hash_iterate(zrouter.l3vni_table,
e3b78da8 3604 (void (*)(struct hash_bucket *, void *))zl3vni_print_hash,
4cce389e
MK
3605 args);
3606
c48349e3 3607 if (use_json)
962af8a8 3608 vty_json(vty, json);
cec2e17d 3609}
3610
3950b52c
CS
3611void zebra_vxlan_dup_addr_detection(ZAPI_HANDLER_ARGS)
3612{
3613 struct stream *s;
3614 int time = 0;
3615 uint32_t max_moves = 0;
3616 uint32_t freeze_time = 0;
3617 bool dup_addr_detect = false;
3618 bool freeze = false;
b2ee2b71 3619 bool old_addr_detect;
3950b52c
CS
3620
3621 s = msg;
3622 STREAM_GETL(s, dup_addr_detect);
3623 STREAM_GETL(s, time);
3624 STREAM_GETL(s, max_moves);
3625 STREAM_GETL(s, freeze);
3626 STREAM_GETL(s, freeze_time);
3627
b2ee2b71
AK
3628 old_addr_detect = zebra_evpn_do_dup_addr_detect(zvrf);
3629 zvrf->dup_addr_detect = dup_addr_detect;
3630 dup_addr_detect = zebra_evpn_do_dup_addr_detect(zvrf);
3631
09de9258
CS
3632 /* DAD previous state was enabled, and new state is disable,
3633 * clear all duplicate detected addresses.
3634 */
b2ee2b71 3635 if (old_addr_detect && !dup_addr_detect)
e20755b2 3636 zebra_vxlan_clear_dup_detect_vni_all(zvrf);
09de9258 3637
3950b52c
CS
3638 zvrf->dad_time = time;
3639 zvrf->dad_max_moves = max_moves;
3640 zvrf->dad_freeze = freeze;
3641 zvrf->dad_freeze_time = freeze_time;
3642
3643 if (IS_ZEBRA_DEBUG_VXLAN)
3644 zlog_debug(
6005fe55
CS
3645 "VRF %s duplicate detect %s max_moves %u timeout %u freeze %s freeze_time %u",
3646 vrf_id_to_name(zvrf->vrf->vrf_id),
b2ee2b71
AK
3647 dup_addr_detect ? "enable" : "disable",
3648 zvrf->dad_max_moves, zvrf->dad_time,
3950b52c
CS
3649 zvrf->dad_freeze ? "enable" : "disable",
3650 zvrf->dad_freeze_time);
3651
3652stream_failure:
3653 return;
3654}
3655
09af6961
NS
3656/*
3657 * Display VNI hash table in detail(VTY command handler).
3658 */
3659void zebra_vxlan_print_vnis_detail(struct vty *vty, struct zebra_vrf *zvrf,
3660 bool use_json)
3661{
06931fdb 3662 json_object *json_array = NULL;
09af6961 3663 struct zebra_ns *zns = NULL;
8b5fdf2e 3664 struct zebra_evpn_show zes;
09af6961
NS
3665
3666 if (!is_evpn_enabled())
3667 return;
3668
3669 zns = zebra_ns_lookup(NS_DEFAULT);
3670 if (!zns)
3671 return;
3672
09af6961 3673 if (use_json)
06931fdb 3674 json_array = json_object_new_array();
09af6961
NS
3675
3676 zes.vty = vty;
06931fdb 3677 zes.json = json_array;
09af6961 3678 zes.zvrf = zvrf;
06931fdb 3679 zes.use_json = use_json;
09af6961
NS
3680
3681 /* Display all L2-VNIs */
8b5fdf2e
PR
3682 hash_iterate(zvrf->evpn_table,
3683 (void (*)(struct hash_bucket *,
3684 void *))zebra_evpn_print_hash_detail,
3685 &zes);
09af6961
NS
3686
3687 /* Display all L3-VNIs */
3688 hash_iterate(zrouter.l3vni_table,
e3b78da8 3689 (void (*)(struct hash_bucket *,
09af6961
NS
3690 void *))zl3vni_print_hash_detail,
3691 &zes);
3692
c48349e3 3693 if (use_json)
962af8a8 3694 vty_json(vty, json_array);
09af6961
NS
3695}
3696
2232a77c 3697/*
ee69da27
MK
3698 * Handle neighbor delete notification from the kernel (on a VLAN device
3699 * / L3 interface). This may result in either the neighbor getting deleted
3700 * from our database or being re-added to the kernel (if it is a valid
2232a77c 3701 * remote neighbor).
3702 */
ee69da27
MK
3703int zebra_vxlan_handle_kernel_neigh_del(struct interface *ifp,
3704 struct interface *link_if,
3705 struct ipaddr *ip)
d62a17ae 3706{
f6371c34 3707 struct zebra_evpn *zevpn = NULL;
05843a27 3708 struct zebra_l3vni *zl3vni = NULL;
b7cfce93 3709
8c9b80b9 3710 /* check if this is a remote neigh entry corresponding to remote
523cafc4 3711 * next-hop
3712 */
8c9b80b9
MK
3713 zl3vni = zl3vni_from_svi(ifp, link_if);
3714 if (zl3vni)
3715 return zl3vni_local_nh_del(zl3vni, ip);
d62a17ae 3716
3717 /* We are only interested in neighbors on an SVI that resides on top
3718 * of a VxLAN bridge.
3719 */
8b5fdf2e 3720 zevpn = zebra_evpn_from_svi(ifp, link_if);
87d76d54 3721 if (!zevpn) {
6041b686 3722 if (IS_ZEBRA_DEBUG_VXLAN)
15569c58 3723 zlog_debug(
ef7b8be4
DL
3724 "%s: Del neighbor %pIA EVPN is not present for interface %s",
3725 __func__, ip, ifp->name);
d62a17ae 3726 return 0;
6041b686 3727 }
8c9b80b9 3728
87d76d54 3729 if (!zevpn->vxlan_if) {
9df414fe 3730 zlog_debug(
d62a17ae 3731 "VNI %u hash %p doesn't have intf upon local neighbor DEL",
87d76d54 3732 zevpn->vni, zevpn);
d62a17ae 3733 return -1;
3734 }
3735
3736 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
3737 zlog_debug("Del neighbor %pIA intf %s(%u) -> L2-VNI %u",
3738 ip, ifp->name, ifp->ifindex, zevpn->vni);
d62a17ae 3739
33064a62 3740 return zebra_evpn_neigh_del_ip(zevpn, ip);
2232a77c 3741}
3742
3743/*
ee69da27
MK
3744 * Handle neighbor add or update notification from the kernel (on a VLAN
3745 * device / L3 interface). This is typically for a local neighbor but can
3746 * also be for a remote neighbor (e.g., ageout notification). It could
3747 * also be a "move" scenario.
2232a77c 3748 */
ee69da27
MK
3749int zebra_vxlan_handle_kernel_neigh_update(struct interface *ifp,
3750 struct interface *link_if,
3751 struct ipaddr *ip,
3752 struct ethaddr *macaddr,
3753 uint16_t state,
a37f4598 3754 bool is_ext,
b169fd6f
AK
3755 bool is_router,
3756 bool local_inactive, bool dp_static)
d62a17ae 3757{
f6371c34 3758 struct zebra_evpn *zevpn = NULL;
05843a27 3759 struct zebra_l3vni *zl3vni = NULL;
3bcbba10 3760
3761 /* check if this is a remote neigh entry corresponding to remote
3762 * next-hop
3763 */
3764 zl3vni = zl3vni_from_svi(ifp, link_if);
3765 if (zl3vni)
3766 return zl3vni_local_nh_add_update(zl3vni, ip, state);
b7cfce93 3767
d62a17ae 3768 /* We are only interested in neighbors on an SVI that resides on top
3769 * of a VxLAN bridge.
3770 */
8b5fdf2e 3771 zevpn = zebra_evpn_from_svi(ifp, link_if);
87d76d54 3772 if (!zevpn)
d62a17ae 3773 return 0;
3774
b169fd6f 3775 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
d62a17ae 3776 zlog_debug(
ef7b8be4
DL
3777 "Add/Update neighbor %pIA MAC %pEA intf %s(%u) state 0x%x %s%s%s%s-> L2-VNI %u",
3778 ip, macaddr, ifp->name,
a37f4598 3779 ifp->ifindex, state, is_ext ? "ext-learned " : "",
3780 is_router ? "router " : "",
b169fd6f 3781 local_inactive ? "local_inactive " : "",
7c0e4dc6 3782 dp_static ? "peer_sync " : "", zevpn->vni);
d62a17ae 3783
ee69da27 3784 /* Is this about a local neighbor or a remote one? */
a37f4598 3785 if (!is_ext)
7cbae20a
PR
3786 return zebra_evpn_local_neigh_update(zevpn, ifp, ip, macaddr,
3787 is_router, local_inactive,
3788 dp_static);
b7cfce93 3789
7cbae20a 3790 return zebra_evpn_remote_neigh_update(zevpn, ifp, ip, macaddr, state);
2232a77c 3791}
3792
0bd371c6
DS
3793static int32_t
3794zebra_vxlan_remote_macip_helper(bool add, struct stream *s, vni_t *vni,
3795 struct ethaddr *macaddr, uint16_t *ipa_len,
3796 struct ipaddr *ip, struct in_addr *vtep_ip,
ce5160c0 3797 uint8_t *flags, uint32_t *seq, esi_t *esi)
0bd371c6
DS
3798{
3799 uint16_t l = 0;
3800
3801 /*
3802 * Obtain each remote MACIP and process.
3803 * Message contains VNI, followed by MAC followed by IP (if any)
3804 * followed by remote VTEP IP.
3805 */
3806 memset(ip, 0, sizeof(*ip));
3807 STREAM_GETL(s, *vni);
3808 STREAM_GET(macaddr->octet, s, ETH_ALEN);
0ffd0fb5 3809 STREAM_GETW(s, *ipa_len);
0bd371c6
DS
3810
3811 if (*ipa_len) {
3812 if (*ipa_len == IPV4_MAX_BYTELEN)
3813 ip->ipa_type = IPADDR_V4;
3814 else if (*ipa_len == IPV6_MAX_BYTELEN)
3815 ip->ipa_type = IPADDR_V6;
3816 else {
3817 if (IS_ZEBRA_DEBUG_VXLAN)
3818 zlog_debug(
3819 "ipa_len *must* be %d or %d bytes in length not %d",
3820 IPV4_MAX_BYTELEN, IPV6_MAX_BYTELEN,
3821 *ipa_len);
3822 goto stream_failure;
3823 }
3824
3825 STREAM_GET(&ip->ip.addr, s, *ipa_len);
3826 }
3827 l += 4 + ETH_ALEN + 4 + *ipa_len;
3828 STREAM_GET(&vtep_ip->s_addr, s, IPV4_MAX_BYTELEN);
3829 l += IPV4_MAX_BYTELEN;
3830
3831 if (add) {
3832 STREAM_GETC(s, *flags);
3833 STREAM_GETL(s, *seq);
3834 l += 5;
ce5160c0
AK
3835 STREAM_GET(esi, s, sizeof(esi_t));
3836 l += sizeof(esi_t);
0bd371c6
DS
3837 }
3838
3839 return l;
3840
3841stream_failure:
3842 return -1;
3843}
b682f6de 3844
2232a77c 3845/*
3846 * Handle message from client to delete a remote MACIP for a VNI.
3847 */
89f4e507 3848void zebra_vxlan_remote_macip_del(ZAPI_HANDLER_ARGS)
d62a17ae 3849{
3850 struct stream *s;
3851 vni_t vni;
3852 struct ethaddr macaddr;
3853 struct ipaddr ip;
3854 struct in_addr vtep_ip;
9df2b997 3855 uint16_t l = 0, ipa_len;
d62a17ae 3856 char buf1[INET6_ADDRSTRLEN];
3857
1002497a 3858 s = msg;
d62a17ae 3859
89f4e507 3860 while (l < hdr->length) {
0bd371c6
DS
3861 int res_length = zebra_vxlan_remote_macip_helper(
3862 false, s, &vni, &macaddr, &ipa_len, &ip, &vtep_ip, NULL,
ce5160c0 3863 NULL, NULL);
4824d144 3864
0bd371c6
DS
3865 if (res_length == -1)
3866 goto stream_failure;
d62a17ae 3867
0bd371c6 3868 l += res_length;
d62a17ae 3869 if (IS_ZEBRA_DEBUG_VXLAN)
3870 zlog_debug(
ef7b8be4
DL
3871 "Recv MACIP DEL VNI %u MAC %pEA%s%s Remote VTEP %pI4 from %s",
3872 vni, &macaddr,
f07e1c99 3873 ipa_len ? " IP " : "",
3874 ipa_len ?
3875 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
9bcef951 3876 &vtep_ip, zebra_route_string(client->proto));
d62a17ae 3877
7f7e49d1
MS
3878 /* Enqueue to workqueue for processing */
3879 zebra_rib_queue_evpn_rem_macip_del(vni, &macaddr, &ip, vtep_ip);
d62a17ae 3880 }
3881
ec93aa12 3882stream_failure:
8068a649 3883 return;
2232a77c 3884}
3885
3886/*
3887 * Handle message from client to add a remote MACIP for a VNI. This
3888 * could be just the add of a MAC address or the add of a neighbor
3889 * (IP+MAC).
3890 */
89f4e507 3891void zebra_vxlan_remote_macip_add(ZAPI_HANDLER_ARGS)
d62a17ae 3892{
3893 struct stream *s;
3894 vni_t vni;
3895 struct ethaddr macaddr;
3896 struct ipaddr ip;
3897 struct in_addr vtep_ip;
9df2b997 3898 uint16_t l = 0, ipa_len;
f07e1c99 3899 uint8_t flags = 0;
3900 uint32_t seq;
d62a17ae 3901 char buf1[INET6_ADDRSTRLEN];
ce5160c0
AK
3902 esi_t esi;
3903 char esi_buf[ESI_STR_LEN];
d62a17ae 3904
ec93aa12 3905 if (!EVPN_ENABLED(zvrf)) {
9df414fe 3906 zlog_debug("EVPN not enabled, ignoring remote MACIP ADD");
8068a649 3907 return;
ec93aa12 3908 }
d62a17ae 3909
1002497a 3910 s = msg;
d62a17ae 3911
89f4e507 3912 while (l < hdr->length) {
7f7e49d1 3913
0bd371c6
DS
3914 int res_length = zebra_vxlan_remote_macip_helper(
3915 true, s, &vni, &macaddr, &ipa_len, &ip, &vtep_ip,
ce5160c0 3916 &flags, &seq, &esi);
d62a17ae 3917
0bd371c6
DS
3918 if (res_length == -1)
3919 goto stream_failure;
d62a17ae 3920
0bd371c6 3921 l += res_length;
ce5160c0
AK
3922 if (IS_ZEBRA_DEBUG_VXLAN) {
3923 if (memcmp(&esi, zero_esi, sizeof(esi_t)))
3924 esi_to_str(&esi, esi_buf, sizeof(esi_buf));
3925 else
9e0c2fd1 3926 strlcpy(esi_buf, "-", ESI_STR_LEN);
d62a17ae 3927 zlog_debug(
ef7b8be4 3928 "Recv %sMACIP ADD VNI %u MAC %pEA%s%s flags 0x%x seq %u VTEP %pI4 ESI %s from %s",
b169fd6f
AK
3929 (flags & ZEBRA_MACIP_TYPE_SYNC_PATH) ?
3930 "sync-" : "",
ef7b8be4 3931 vni, &macaddr,
f07e1c99 3932 ipa_len ? " IP " : "",
3933 ipa_len ?
3934 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
9bcef951 3935 flags, seq, &vtep_ip, esi_buf,
d62a17ae 3936 zebra_route_string(client->proto));
ce5160c0 3937 }
d62a17ae 3938
7f7e49d1
MS
3939 /* Enqueue to workqueue for processing */
3940 zebra_rib_queue_evpn_rem_macip_add(vni, &macaddr, &ip, flags,
3941 seq, vtep_ip, &esi);
d62a17ae 3942 }
3943
ec93aa12 3944stream_failure:
8068a649 3945 return;
13d60d35 3946}
3947
4b3f26f4 3948/*
3949 * Handle remote vtep delete by kernel; re-add the vtep if we have it
3950 */
3951int zebra_vxlan_check_readd_vtep(struct interface *ifp,
3952 struct in_addr vtep_ip)
3953{
3954 struct zebra_if *zif;
3955 struct zebra_vrf *zvrf = NULL;
3956 struct zebra_l2info_vxlan *vxl;
3957 vni_t vni;
f6371c34 3958 struct zebra_evpn *zevpn = NULL;
c172c032 3959 struct zebra_vtep *zvtep = NULL;
4b3f26f4 3960
3961 zif = ifp->info;
3962 assert(zif);
3963 vxl = &zif->l2info.vxl;
3964 vni = vxl->vni;
3965
3966 /* If EVPN is not enabled, nothing to do. */
3967 if (!is_evpn_enabled())
3968 return 0;
3969
3970 /* Locate VRF corresponding to interface. */
096f7609 3971 zvrf = ifp->vrf->info;
4b3f26f4 3972 if (!zvrf)
3973 return -1;
3974
3975 /* Locate hash entry; it is expected to exist. */
8b5fdf2e 3976 zevpn = zebra_evpn_lookup(vni);
87d76d54 3977 if (!zevpn)
4b3f26f4 3978 return 0;
3979
3980 /* If the remote vtep entry doesn't exists nothing to do */
8b5fdf2e 3981 zvtep = zebra_evpn_vtep_find(zevpn, &vtep_ip);
4b3f26f4 3982 if (!zvtep)
3983 return 0;
3984
3985 if (IS_ZEBRA_DEBUG_VXLAN)
3986 zlog_debug(
9bcef951
MS
3987 "Del MAC for remote VTEP %pI4 intf %s(%u) VNI %u - readd",
3988 &vtep_ip, ifp->name, ifp->ifindex, vni);
4b3f26f4 3989
8b5fdf2e 3990 zebra_evpn_vtep_install(zevpn, zvtep);
4b3f26f4 3991 return 0;
3992}
3993
13d60d35 3994/*
2232a77c 3995 * Handle notification of MAC add/update over VxLAN. If the kernel is notifying
3996 * us, this must involve a multihoming scenario. Treat this as implicit delete
3997 * of any prior local MAC.
13d60d35 3998 */
15400f95
AK
3999static int zebra_vxlan_check_del_local_mac(struct interface *ifp,
4000 struct interface *br_if,
4001 struct ethaddr *macaddr,
4002 vlanid_t vid)
13d60d35 4003{
d62a17ae 4004 struct zebra_if *zif;
d62a17ae 4005 struct zebra_l2info_vxlan *vxl;
4006 vni_t vni;
f6371c34 4007 struct zebra_evpn *zevpn;
3198b2b3 4008 struct zebra_mac *mac;
13d60d35 4009
d62a17ae 4010 zif = ifp->info;
4011 assert(zif);
4012 vxl = &zif->l2info.vxl;
4013 vni = vxl->vni;
13d60d35 4014
2853fed6 4015 /* Check if EVPN is enabled. */
4016 if (!is_evpn_enabled())
d62a17ae 4017 return 0;
13d60d35 4018
d62a17ae 4019 /* Locate hash entry; it is expected to exist. */
8b5fdf2e 4020 zevpn = zebra_evpn_lookup(vni);
87d76d54 4021 if (!zevpn)
d62a17ae 4022 return 0;
13d60d35 4023
d62a17ae 4024 /* If entry doesn't exist, nothing to do. */
b2998086 4025 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
d62a17ae 4026 if (!mac)
4027 return 0;
13d60d35 4028
d62a17ae 4029 /* Is it a local entry? */
4030 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
4031 return 0;
13d60d35 4032
d62a17ae 4033 if (IS_ZEBRA_DEBUG_VXLAN)
4034 zlog_debug(
ef7b8be4
DL
4035 "Add/update remote MAC %pEA intf %s(%u) VNI %u flags 0x%x - del local",
4036 macaddr, ifp->name, ifp->ifindex, vni, mac->flags);
13d60d35 4037
d62a17ae 4038 /* Remove MAC from BGP. */
b2998086
PR
4039 zebra_evpn_mac_send_del_to_client(zevpn->vni, macaddr, mac->flags,
4040 false /* force */);
13d60d35 4041
b6938a74
MK
4042 /*
4043 * If there are no neigh associated with the mac delete the mac
4044 * else mark it as AUTO for forward reference
4045 */
4046 if (!listcount(mac->neigh_list)) {
b2998086 4047 zebra_evpn_mac_del(zevpn, mac);
b6938a74 4048 } else {
8b07f173 4049 zebra_evpn_mac_clear_fwd_info(mac);
b169fd6f 4050 UNSET_FLAG(mac->flags, ZEBRA_MAC_ALL_LOCAL_FLAGS);
5756dd1d 4051 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
b6938a74
MK
4052 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
4053 }
13d60d35 4054
d62a17ae 4055 return 0;
13d60d35 4056}
4057
15400f95
AK
4058/* MAC notification from the dataplane with a network dest port -
4059 * 1. This can be a local MAC on a down ES (if fast-failover is not possible
4060 * 2. Or it can be a remote MAC
4061 */
4062int zebra_vxlan_dp_network_mac_add(struct interface *ifp,
4063 struct interface *br_if,
4064 struct ethaddr *macaddr, vlanid_t vid,
4065 uint32_t nhg_id, bool sticky, bool dp_static)
4066{
4067 struct zebra_evpn_es *es;
4068 struct interface *acc_ifp;
4069
df6c1982 4070 /* If netlink message is with vid, it will have no nexthop.
4071 * So skip it.
4072 */
4073 if (vid) {
4074 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC)
4075 zlog_debug("dpAdd MAC %pEA VID %u - ignore as no nhid",
4076 macaddr, vid);
4077 return 0;
4078 }
4079
4080 /* Get vxlan's vid for netlink message has no it. */
4081 vid = ((struct zebra_if *)ifp->info)->l2info.vxl.access_vlan;
4082
15400f95
AK
4083 /* if remote mac delete the local entry */
4084 if (!nhg_id || !zebra_evpn_nhg_is_local_es(nhg_id, &es)
4085 || !zebra_evpn_es_local_mac_via_network_port(es)) {
4086 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC)
4087 zlog_debug("dpAdd remote MAC %pEA VID %u", macaddr,
4088 vid);
4089 return zebra_vxlan_check_del_local_mac(ifp, br_if, macaddr,
4090 vid);
4091 }
4092
4093 /* If local MAC on a down local ES translate the network-mac-add
209813e9 4094 * to a local-active-mac-add
15400f95
AK
4095 */
4096 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC)
4097 zlog_debug("dpAdd local-nw-MAC %pEA VID %u", macaddr, vid);
4098 acc_ifp = es->zif->ifp;
4099 return zebra_vxlan_local_mac_add_update(
4100 acc_ifp, br_if, macaddr, vid, sticky,
4101 false /* local_inactive */, dp_static);
4102}
4103
13d60d35 4104/*
15400f95
AK
4105 * Handle network MAC delete by kernel -
4106 * 1. readd the remote MAC if we have it
4107 * 2. local MAC with does ES may also need to be re-installed
13d60d35 4108 */
15400f95
AK
4109int zebra_vxlan_dp_network_mac_del(struct interface *ifp,
4110 struct interface *br_if,
4111 struct ethaddr *macaddr, vlanid_t vid)
13d60d35 4112{
a9a76262
MK
4113 struct zebra_if *zif = NULL;
4114 struct zebra_l2info_vxlan *vxl = NULL;
d62a17ae 4115 vni_t vni;
f6371c34 4116 struct zebra_evpn *zevpn = NULL;
05843a27 4117 struct zebra_l3vni *zl3vni = NULL;
3198b2b3 4118 struct zebra_mac *mac = NULL;
2232a77c 4119
d62a17ae 4120 zif = ifp->info;
4121 assert(zif);
4122 vxl = &zif->l2info.vxl;
4123 vni = vxl->vni;
2232a77c 4124
2853fed6 4125 /* Check if EVPN is enabled. */
4126 if (!is_evpn_enabled())
d62a17ae 4127 return 0;
2232a77c 4128
a9a76262
MK
4129 /* check if this is a remote RMAC and readd simillar to remote macs */
4130 zl3vni = zl3vni_lookup(vni);
4131 if (zl3vni)
4132 return zebra_vxlan_readd_remote_rmac(zl3vni, macaddr);
4133
d62a17ae 4134 /* Locate hash entry; it is expected to exist. */
8b5fdf2e 4135 zevpn = zebra_evpn_lookup(vni);
87d76d54 4136 if (!zevpn)
d62a17ae 4137 return 0;
13d60d35 4138
d62a17ae 4139 /* If entry doesn't exist, nothing to do. */
b2998086 4140 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
d62a17ae 4141 if (!mac)
4142 return 0;
2232a77c 4143
15400f95
AK
4144 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
4145 /* If remote entry simply re-install */
4146 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC)
4147 zlog_debug(
4148 "dpDel remote MAC %pEA intf %s(%u) VNI %u - readd",
4149 macaddr, ifp->name, ifp->ifindex, vni);
4150 zebra_evpn_rem_mac_install(zevpn, mac, false /* was_static */);
4151 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL) && mac->es
4152 && zebra_evpn_es_local_mac_via_network_port(mac->es)) {
4153 /* If local entry via nw-port call local-del which will
4154 * re-install entry in the dataplane is needed
4155 */
4156 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC)
4157 zlog_debug("dpDel local-nw-MAC %pEA VNI %u", macaddr,
4158 vni);
00a7710c
AK
4159
4160 zebra_evpn_del_local_mac(zevpn, mac, false);
15400f95 4161 }
13d60d35 4162
d62a17ae 4163 return 0;
13d60d35 4164}
4165
4166/*
2232a77c 4167 * Handle local MAC delete (on a port or VLAN corresponding to this VNI).
13d60d35 4168 */
d62a17ae 4169int zebra_vxlan_local_mac_del(struct interface *ifp, struct interface *br_if,
4170 struct ethaddr *macaddr, vlanid_t vid)
13d60d35 4171{
f6371c34 4172 struct zebra_evpn *zevpn;
3198b2b3 4173 struct zebra_mac *mac;
13d60d35 4174
d62a17ae 4175 /* We are interested in MACs only on ports or (port, VLAN) that
4176 * map to a VNI.
4177 */
8b5fdf2e 4178 zevpn = zebra_evpn_map_vlan(ifp, br_if, vid);
87d76d54 4179 if (!zevpn)
d62a17ae 4180 return 0;
87d76d54 4181 if (!zevpn->vxlan_if) {
9df414fe
QY
4182 zlog_debug(
4183 "VNI %u hash %p doesn't have intf upon local MAC DEL",
87d76d54 4184 zevpn->vni, zevpn);
d62a17ae 4185 return -1;
4186 }
13d60d35 4187
15400f95
AK
4188 /* If entry doesn't exist, nothing to do. */
4189 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
4190 if (!mac)
4191 return 0;
4192
4193 /* Is it a local entry? */
4194 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
4195 return 0;
4196
00a7710c 4197 return zebra_evpn_del_local_mac(zevpn, mac, false);
13d60d35 4198}
4199
4200/*
2232a77c 4201 * Handle local MAC add (on a port or VLAN corresponding to this VNI).
13d60d35 4202 */
d62a17ae 4203int zebra_vxlan_local_mac_add_update(struct interface *ifp,
4204 struct interface *br_if,
4205 struct ethaddr *macaddr, vlanid_t vid,
b169fd6f
AK
4206 bool sticky, bool local_inactive,
4207 bool dp_static)
d62a17ae 4208{
f6371c34 4209 struct zebra_evpn *zevpn;
e22a946a 4210 struct zebra_vrf *zvrf;
d62a17ae 4211
2bdd4461 4212 assert(ifp);
b7895aad 4213
d62a17ae 4214 /* We are interested in MACs only on ports or (port, VLAN) that
87d76d54 4215 * map to an EVPN.
d62a17ae 4216 */
8b5fdf2e 4217 zevpn = zebra_evpn_map_vlan(ifp, br_if, vid);
87d76d54 4218 if (!zevpn) {
d62a17ae 4219 if (IS_ZEBRA_DEBUG_VXLAN)
4220 zlog_debug(
ef7b8be4
DL
4221 " Add/Update %sMAC %pEA intf %s(%u) VID %u, could not find EVPN",
4222 sticky ? "sticky " : "", macaddr,
d62a17ae 4223 ifp->name, ifp->ifindex, vid);
4224 return 0;
4225 }
4226
87d76d54 4227 if (!zevpn->vxlan_if) {
28bd0652
DS
4228 if (IS_ZEBRA_DEBUG_VXLAN)
4229 zlog_debug(
d6951e5e 4230 " VNI %u hash %p doesn't have intf upon local MAC ADD",
87d76d54 4231 zevpn->vni, zevpn);
d62a17ae 4232 return -1;
4233 }
4234
d6bf8f13 4235 zvrf = zebra_vrf_get_evpn();
d9d3455e
PR
4236 return zebra_evpn_add_update_local_mac(zvrf, zevpn, ifp, macaddr, vid,
4237 sticky, local_inactive,
00a7710c 4238 dp_static, NULL);
2232a77c 4239}
13d60d35 4240
4241/*
87d76d54 4242 * Handle message from client to delete a remote VTEP for an EVPN.
13d60d35 4243 */
7e5b0b2b 4244void zebra_vxlan_remote_vtep_del_zapi(ZAPI_HANDLER_ARGS)
d62a17ae 4245{
4246 struct stream *s;
d7c0a89a 4247 unsigned short l = 0;
d62a17ae 4248 vni_t vni;
4249 struct in_addr vtep_ip;
d62a17ae 4250
ec93aa12 4251 if (!is_evpn_enabled()) {
9df414fe 4252 zlog_debug(
7e5b0b2b 4253 "%s: EVPN is not enabled yet we have received a VTEP DEL msg",
15569c58 4254 __func__);
8068a649 4255 return;
ec93aa12
DS
4256 }
4257
986512a3 4258 if (!EVPN_ENABLED(zvrf)) {
7e5b0b2b
MS
4259 zlog_debug("Recv VTEP DEL zapi for non-EVPN VRF %u",
4260 zvrf_id(zvrf));
8068a649 4261 return;
2853fed6 4262 }
4263
1002497a 4264 s = msg;
d62a17ae 4265
89f4e507 4266 while (l < hdr->length) {
694bd4ce 4267 int flood_control __attribute__((unused));
8a64de72 4268
d62a17ae 4269 /* Obtain each remote VTEP and process. */
ec93aa12 4270 STREAM_GETL(s, vni);
d62a17ae 4271 l += 4;
ec93aa12 4272 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 4273 l += IPV4_MAX_BYTELEN;
4274
8a64de72
DS
4275 /* Flood control is intentionally ignored right now */
4276 STREAM_GETL(s, flood_control);
4277 l += 4;
4278
d62a17ae 4279 if (IS_ZEBRA_DEBUG_VXLAN)
7e5b0b2b 4280 zlog_debug("Recv VTEP DEL %pI4 VNI %u from %s",
9bcef951 4281 &vtep_ip, vni,
d62a17ae 4282 zebra_route_string(client->proto));
4283
7e5b0b2b
MS
4284 /* Enqueue for processing */
4285 zebra_rib_queue_evpn_rem_vtep_del(zvrf_id(zvrf), vni, vtep_ip);
4286 }
4287
4288stream_failure:
4289 return;
4290}
4291
4292/*
4293 * Handle message from client to delete a remote VTEP for an EVPN.
4294 */
4295void zebra_vxlan_remote_vtep_del(vrf_id_t vrf_id, vni_t vni,
4296 struct in_addr vtep_ip)
4297{
f6371c34 4298 struct zebra_evpn *zevpn;
c172c032 4299 struct zebra_vtep *zvtep;
7e5b0b2b
MS
4300 struct interface *ifp;
4301 struct zebra_if *zif;
4302 struct zebra_vrf *zvrf;
4303
4304 if (!is_evpn_enabled()) {
4305 zlog_debug("%s: Can't process vtep del: EVPN is not enabled",
4306 __func__);
4307 return;
4308 }
4309
4310 zvrf = zebra_vrf_lookup_by_id(vrf_id);
4311 if (!zvrf)
4312 return;
d62a17ae 4313
7e5b0b2b
MS
4314 if (!EVPN_ENABLED(zvrf)) {
4315 zlog_debug("Can't process VTEP DEL for non-EVPN VRF %u",
4316 zvrf_id(zvrf));
4317 return;
4318 }
4319
4320 /* Locate VNI hash entry - expected to exist. */
4321 zevpn = zebra_evpn_lookup(vni);
4322 if (!zevpn) {
4323 if (IS_ZEBRA_DEBUG_VXLAN)
9df414fe 4324 zlog_debug(
7e5b0b2b
MS
4325 "Failed to locate VNI hash for remote VTEP DEL, VNI %u",
4326 vni);
4327 return;
4328 }
b5ebdc9b 4329
7e5b0b2b
MS
4330 ifp = zevpn->vxlan_if;
4331 if (!ifp) {
4332 zlog_debug(
4333 "VNI %u hash %p doesn't have intf upon remote VTEP DEL",
4334 zevpn->vni, zevpn);
4335 return;
4336 }
4337 zif = ifp->info;
b5ebdc9b 4338
7e5b0b2b
MS
4339 /* If down or not mapped to a bridge, we're done. */
4340 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
4341 return;
4342
4343 /* If the remote VTEP does not exist, there's nothing more to
4344 * do.
4345 * Otherwise, uninstall any remote MACs pointing to this VTEP
4346 * and then, the VTEP entry itself and remove it.
4347 */
4348 zvtep = zebra_evpn_vtep_find(zevpn, &vtep_ip);
4349 if (!zvtep)
4350 return;
4351
4352 zebra_evpn_vtep_uninstall(zevpn, &vtep_ip);
4353 zebra_evpn_vtep_del(zevpn, zvtep);
4354}
4355
4356/*
4357 * Handle message from client to add a remote VTEP for an EVPN.
4358 */
4359void zebra_vxlan_remote_vtep_add(vrf_id_t vrf_id, vni_t vni,
4360 struct in_addr vtep_ip, int flood_control)
4361{
f6371c34 4362 struct zebra_evpn *zevpn;
7e5b0b2b
MS
4363 struct interface *ifp;
4364 struct zebra_if *zif;
c172c032 4365 struct zebra_vtep *zvtep;
7e5b0b2b 4366 struct zebra_vrf *zvrf;
d62a17ae 4367
7e5b0b2b
MS
4368 if (!is_evpn_enabled()) {
4369 zlog_debug("%s: EVPN not enabled: can't process a VTEP ADD",
4370 __func__);
4371 return;
d62a17ae 4372 }
4373
7e5b0b2b
MS
4374 zvrf = zebra_vrf_lookup_by_id(vrf_id);
4375 if (!zvrf)
4376 return;
4377
4378 if (!EVPN_ENABLED(zvrf)) {
4379 zlog_debug("Can't process VTEP ADD for non-EVPN VRF %u",
4380 zvrf_id(zvrf));
4381 return;
4382 }
4383
4384 /* Locate VNI hash entry - expected to exist. */
4385 zevpn = zebra_evpn_lookup(vni);
4386 if (!zevpn) {
4387 flog_err(
4388 EC_ZEBRA_VTEP_ADD_FAILED,
4389 "Failed to locate EVPN hash upon remote VTEP ADD, VNI %u",
4390 vni);
4391 return;
4392 }
4393
4394 ifp = zevpn->vxlan_if;
4395 if (!ifp) {
4396 flog_err(
4397 EC_ZEBRA_VTEP_ADD_FAILED,
4398 "VNI %u hash %p doesn't have intf upon remote VTEP ADD",
4399 zevpn->vni, zevpn);
4400 return;
4401 }
4402
4403 zif = ifp->info;
4404
4405 /* If down or not mapped to a bridge, we're done. */
4406 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
4407 return;
4408
4409 zvtep = zebra_evpn_vtep_find(zevpn, &vtep_ip);
4410 if (zvtep) {
4411 /* If the remote VTEP already exists check if
4412 * the flood mode has changed
4413 */
4414 if (zvtep->flood_control != flood_control) {
4415 if (zvtep->flood_control == VXLAN_FLOOD_DISABLED)
4416 /* old mode was head-end-replication but
4417 * is no longer; get rid of the HER fdb
4418 * entry installed before
4419 */
4420 zebra_evpn_vtep_uninstall(zevpn, &vtep_ip);
4421 zvtep->flood_control = flood_control;
4422 zebra_evpn_vtep_install(zevpn, zvtep);
4423 }
4424 } else {
4425 zvtep = zebra_evpn_vtep_add(zevpn, &vtep_ip, flood_control);
4426 if (zvtep)
4427 zebra_evpn_vtep_install(zevpn, zvtep);
4428 else
4429 flog_err(EC_ZEBRA_VTEP_ADD_FAILED,
4430 "Failed to add remote VTEP, VNI %u zevpn %p",
4431 vni, zevpn);
4432 }
13d60d35 4433}
4434
4435/*
87d76d54 4436 * Handle message from client to add a remote VTEP for an EVPN.
13d60d35 4437 */
7e5b0b2b 4438void zebra_vxlan_remote_vtep_add_zapi(ZAPI_HANDLER_ARGS)
d62a17ae 4439{
4440 struct stream *s;
d7c0a89a 4441 unsigned short l = 0;
d62a17ae 4442 vni_t vni;
4443 struct in_addr vtep_ip;
9718c54e 4444 int flood_control;
d62a17ae 4445
ec93aa12 4446 if (!is_evpn_enabled()) {
9df414fe 4447 zlog_debug(
7e5b0b2b 4448 "%s: EVPN not enabled yet we received a VTEP ADD zapi msg",
15569c58 4449 __func__);
8068a649 4450 return;
ec93aa12
DS
4451 }
4452
986512a3 4453 if (!EVPN_ENABLED(zvrf)) {
7e5b0b2b
MS
4454 zlog_debug("Recv VTEP ADD zapi for non-EVPN VRF %u",
4455 zvrf_id(zvrf));
8068a649 4456 return;
2853fed6 4457 }
d62a17ae 4458
1002497a 4459 s = msg;
d62a17ae 4460
89f4e507 4461 while (l < hdr->length) {
d62a17ae 4462 /* Obtain each remote VTEP and process. */
ec93aa12 4463 STREAM_GETL(s, vni);
d62a17ae 4464 l += 4;
ec93aa12 4465 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
9718c54e 4466 STREAM_GETL(s, flood_control);
8a64de72 4467 l += IPV4_MAX_BYTELEN + 4;
d62a17ae 4468
4469 if (IS_ZEBRA_DEBUG_VXLAN)
7e5b0b2b
MS
4470 zlog_debug("Recv VTEP ADD %pI4 VNI %u flood %d from %s",
4471 &vtep_ip, vni, flood_control,
4472 zebra_route_string(client->proto));
d62a17ae 4473
7e5b0b2b
MS
4474 /* Enqueue for processing */
4475 zebra_rib_queue_evpn_rem_vtep_add(zvrf_id(zvrf), vni, vtep_ip,
4476 flood_control);
d62a17ae 4477 }
4478
ec93aa12 4479stream_failure:
8068a649 4480 return;
13d60d35 4481}
4482
1a98c087
MK
4483/*
4484 * Add/Del gateway macip to evpn
4485 * g/w can be:
4486 * 1. SVI interface on a vlan aware bridge
4487 * 2. SVI interface on a vlan unaware bridge
4488 * 3. vrr interface (MACVLAN) associated to a SVI
4489 * We advertise macip routes for an interface if it is associated to VxLan vlan
4490 */
7e5b0b2b 4491int zebra_vxlan_add_del_gw_macip(struct interface *ifp, const struct prefix *p,
1a98c087
MK
4492 int add)
4493{
4494 struct ipaddr ip;
4495 struct ethaddr macaddr;
f6371c34 4496 struct zebra_evpn *zevpn = NULL;
1a98c087 4497
6006b807
DA
4498 memset(&ip, 0, sizeof(ip));
4499 memset(&macaddr, 0, sizeof(macaddr));
1a98c087 4500
2853fed6 4501 /* Check if EVPN is enabled. */
4502 if (!is_evpn_enabled())
297a21b6
MK
4503 return 0;
4504
1a98c087
MK
4505 if (IS_ZEBRA_IF_MACVLAN(ifp)) {
4506 struct interface *svi_if =
4507 NULL; /* SVI corresponding to the MACVLAN */
4508 struct zebra_if *ifp_zif =
4509 NULL; /* Zebra daemon specific info for MACVLAN */
4510 struct zebra_if *svi_if_zif =
4511 NULL; /* Zebra daemon specific info for SVI*/
4512
4513 ifp_zif = ifp->info;
4514 if (!ifp_zif)
4515 return -1;
4516
71349e03
MK
4517 /*
4518 * for a MACVLAN interface the link represents the svi_if
4519 */
4520 svi_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
4521 ifp_zif->link_ifindex);
1a98c087 4522 if (!svi_if) {
9df414fe
QY
4523 zlog_debug("MACVLAN %s(%u) without link information",
4524 ifp->name, ifp->ifindex);
1a98c087
MK
4525 return -1;
4526 }
4527
4528 if (IS_ZEBRA_IF_VLAN(svi_if)) {
71349e03
MK
4529 /*
4530 * If it is a vlan aware bridge then the link gives the
4531 * bridge information
4532 */
4533 struct interface *svi_if_link = NULL;
4534
1a98c087 4535 svi_if_zif = svi_if->info;
71349e03
MK
4536 if (svi_if_zif) {
4537 svi_if_link = if_lookup_by_index_per_ns(
60466a63
QY
4538 zebra_ns_lookup(NS_DEFAULT),
4539 svi_if_zif->link_ifindex);
8b5fdf2e
PR
4540 zevpn = zebra_evpn_from_svi(svi_if,
4541 svi_if_link);
71349e03 4542 }
1a98c087 4543 } else if (IS_ZEBRA_IF_BRIDGE(svi_if)) {
71349e03
MK
4544 /*
4545 * If it is a vlan unaware bridge then svi is the bridge
4546 * itself
4547 */
8b5fdf2e 4548 zevpn = zebra_evpn_from_svi(svi_if, svi_if);
1a98c087
MK
4549 }
4550 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
4551 struct zebra_if *svi_if_zif =
71349e03
MK
4552 NULL; /* Zebra daemon specific info for SVI */
4553 struct interface *svi_if_link =
4554 NULL; /* link info for the SVI = bridge info */
1a98c087
MK
4555
4556 svi_if_zif = ifp->info;
e3bb770c
IS
4557 if (svi_if_zif) {
4558 svi_if_link = if_lookup_by_index_per_ns(
cef91a18
QY
4559 zebra_ns_lookup(NS_DEFAULT),
4560 svi_if_zif->link_ifindex);
e3bb770c 4561 if (svi_if_link)
8b5fdf2e 4562 zevpn = zebra_evpn_from_svi(ifp, svi_if_link);
e3bb770c 4563 }
1a98c087 4564 } else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
8b5fdf2e 4565 zevpn = zebra_evpn_from_svi(ifp, ifp);
1a98c087
MK
4566 }
4567
87d76d54 4568 if (!zevpn)
1a98c087
MK
4569 return 0;
4570
87d76d54 4571 if (!zevpn->vxlan_if) {
9df414fe 4572 zlog_debug("VNI %u hash %p doesn't have intf upon MACVLAN up",
87d76d54 4573 zevpn->vni, zevpn);
1a98c087
MK
4574 return -1;
4575 }
4576
c0c7707d
AK
4577 /* VRR IP is advertised only if gw-macip-adv-enabled */
4578 if (IS_ZEBRA_IF_MACVLAN(ifp)) {
4579 if (!advertise_gw_macip_enabled(zevpn))
4580 return 0;
4581 } else {
4582 /* SVI IP is advertised if gw or svi macip-adv-enabled */
4583 if (!advertise_svi_macip_enabled(zevpn)
4584 && !advertise_gw_macip_enabled(zevpn))
4585 return 0;
4586 }
1a98c087 4587
1a98c087
MK
4588 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
4589
4590 if (p->family == AF_INET) {
4591 ip.ipa_type = IPADDR_V4;
4592 memcpy(&(ip.ipaddr_v4), &(p->u.prefix4),
4593 sizeof(struct in_addr));
4594 } else if (p->family == AF_INET6) {
4595 ip.ipa_type = IPADDR_V6;
4596 memcpy(&(ip.ipaddr_v6), &(p->u.prefix6),
4597 sizeof(struct in6_addr));
4598 }
4599
4600
4601 if (add)
8b5fdf2e 4602 zebra_evpn_gw_macip_add(ifp, zevpn, &macaddr, &ip);
1a98c087 4603 else
8b5fdf2e 4604 zebra_evpn_gw_macip_del(ifp, zevpn, &ip);
1a98c087
MK
4605
4606 return 0;
4607}
4608
2232a77c 4609/*
b7cfce93
MK
4610 * Handle SVI interface going down.
4611 * SVI can be associated to either L3-VNI or L2-VNI.
4612 * For L2-VNI: At this point, this is a NOP since
4613 * the kernel deletes the neighbor entries on this SVI (if any).
87d76d54 4614 * We only need to update the vrf corresponding to zevpn.
b7cfce93
MK
4615 * For L3-VNI: L3-VNI is operationally down, update mac-ip routes and delete
4616 * from bgp
2232a77c 4617 */
d62a17ae 4618int zebra_vxlan_svi_down(struct interface *ifp, struct interface *link_if)
2232a77c 4619{
05843a27 4620 struct zebra_l3vni *zl3vni = NULL;
b7cfce93
MK
4621
4622 zl3vni = zl3vni_from_svi(ifp, link_if);
4623 if (zl3vni) {
4624
4625 /* process l3-vni down */
4626 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4627
4628 /* remove association with svi-if */
4629 zl3vni->svi_if = NULL;
4630 } else {
f6371c34 4631 struct zebra_evpn *zevpn = NULL;
b7cfce93 4632
243b74ed
AK
4633 /* Unlink the SVI from the access VLAN */
4634 zebra_evpn_acc_bd_svi_set(ifp->info, link_if->info, false);
4635
87d76d54 4636 /* since we dont have svi corresponding to zevpn, we associate it
b7cfce93
MK
4637 * to default vrf. Note: the corresponding neigh entries on the
4638 * SVI would have already been deleted */
8b5fdf2e 4639 zevpn = zebra_evpn_from_svi(ifp, link_if);
243b74ed 4640
87d76d54 4641 if (zevpn) {
b0b77855
QY
4642 /* remove from l3-vni list */
4643 zl3vni = zl3vni_from_vrf(zevpn->vrf_id);
4644 if (zl3vni)
4645 listnode_delete(zl3vni->l2vnis, zevpn);
b13f35ec 4646
9daa5d47 4647 zevpn->svi_if = NULL;
87d76d54 4648 zevpn->vrf_id = VRF_DEFAULT;
b7cfce93
MK
4649
4650 /* update the tenant vrf in BGP */
196d7a86
CS
4651 if (if_is_operative(zevpn->vxlan_if))
4652 zebra_evpn_send_add_to_client(zevpn);
b7cfce93
MK
4653 }
4654 }
d62a17ae 4655 return 0;
2232a77c 4656}
4657
4658/*
b7cfce93
MK
4659 * Handle SVI interface coming up.
4660 * SVI can be associated to L3-VNI (l3vni vxlan interface) or L2-VNI (l2-vni
4661 * vxlan intf).
4662 * For L2-VNI: we need to install any remote neighbors entried (used for
b816de62 4663 * arp-suppression)
b7cfce93 4664 * For L3-VNI: SVI will be used to get the rmac to be used with L3-VNI
2232a77c 4665 */
d62a17ae 4666int zebra_vxlan_svi_up(struct interface *ifp, struct interface *link_if)
2232a77c 4667{
f6371c34 4668 struct zebra_evpn *zevpn = NULL;
05843a27 4669 struct zebra_l3vni *zl3vni = NULL;
2232a77c 4670
b7cfce93
MK
4671 zl3vni = zl3vni_from_svi(ifp, link_if);
4672 if (zl3vni) {
2232a77c 4673
b7cfce93
MK
4674 /* associate with svi */
4675 zl3vni->svi_if = ifp;
2232a77c 4676
b7cfce93
MK
4677 /* process oper-up */
4678 if (is_l3vni_oper_up(zl3vni))
4679 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4680 } else {
4681
4682 /* process SVI up for l2-vni */
4683 struct neigh_walk_ctx n_wctx;
4684
8b5fdf2e 4685 zevpn = zebra_evpn_from_svi(ifp, link_if);
87d76d54 4686 if (!zevpn)
b7cfce93
MK
4687 return 0;
4688
87d76d54 4689 if (!zevpn->vxlan_if) {
9df414fe 4690 zlog_debug(
43e52561 4691 "VNI %u hash %p doesn't have intf upon SVI up",
87d76d54 4692 zevpn->vni, zevpn);
b7cfce93
MK
4693 return -1;
4694 }
4695
4696 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
4697 zlog_debug(
4698 "SVI %s(%u) VNI %u VRF %s is UP, installing neighbors",
87d76d54 4699 ifp->name, ifp->ifindex, zevpn->vni,
096f7609 4700 ifp->vrf->name);
2232a77c 4701
b7cfce93 4702 /* update the vrf information for l2-vni and inform bgp */
9daa5d47 4703 zevpn->svi_if = ifp;
096f7609 4704 zevpn->vrf_id = ifp->vrf->vrf_id;
196d7a86 4705
b0b77855
QY
4706 zl3vni = zl3vni_from_vrf(zevpn->vrf_id);
4707 if (zl3vni)
4708 listnode_add_sort_nodup(zl3vni->l2vnis, zevpn);
b13f35ec 4709
196d7a86
CS
4710 if (if_is_operative(zevpn->vxlan_if))
4711 zebra_evpn_send_add_to_client(zevpn);
b7cfce93
MK
4712
4713 /* Install any remote neighbors for this VNI. */
6006b807 4714 memset(&n_wctx, 0, sizeof(n_wctx));
87d76d54 4715 n_wctx.zevpn = zevpn;
7cbae20a 4716 hash_iterate(zevpn->neigh_table, zebra_evpn_install_neigh_hash,
b7cfce93 4717 &n_wctx);
243b74ed
AK
4718
4719 /* Link the SVI from the access VLAN */
4720 zebra_evpn_acc_bd_svi_set(ifp->info, link_if->info, true);
56599dd9
TA
4721
4722 /* Update MACIP routes created by advertise-svi-ip */
4723 if (advertise_svi_macip_enabled(zevpn)) {
4724 zebra_evpn_del_macip_for_intf(ifp, zevpn);
4725 zebra_evpn_add_macip_for_intf(ifp, zevpn);
4726 }
b7cfce93 4727 }
2232a77c 4728
d62a17ae 4729 return 0;
2232a77c 4730}
4731
0056f687
CS
4732/*
4733 * Handle MAC-VLAN interface going down.
4734 * L3VNI: When MAC-VLAN interface goes down,
4735 * find its associated SVI and update type2/type-5 routes
4736 * with SVI as RMAC
4737 */
4738void zebra_vxlan_macvlan_down(struct interface *ifp)
4739{
05843a27 4740 struct zebra_l3vni *zl3vni = NULL;
0056f687
CS
4741 struct zebra_if *zif, *link_zif;
4742 struct interface *link_ifp, *link_if;
4743
4744 zif = ifp->info;
4745 assert(zif);
4746 link_ifp = zif->link;
59260d4a 4747 if (!link_ifp) {
a2df495f
IR
4748 if (IS_ZEBRA_DEBUG_VXLAN)
4749 zlog_debug(
4750 "macvlan parent link is not found. Parent index %d ifp %s",
4751 zif->link_ifindex,
096f7609
IR
4752 ifindex2ifname(zif->link_ifindex,
4753 ifp->vrf->vrf_id));
59260d4a
CS
4754 return;
4755 }
0056f687
CS
4756 link_zif = link_ifp->info;
4757 assert(link_zif);
4758
4759 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
4760 link_zif->link_ifindex);
4761
4762 zl3vni = zl3vni_from_svi(link_ifp, link_if);
4763 if (zl3vni) {
4764 zl3vni->mac_vlan_if = NULL;
4765 if (is_l3vni_oper_up(zl3vni))
4766 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4767 }
4768}
4769
4770/*
4771 * Handle MAC-VLAN interface going up.
4772 * L3VNI: When MAC-VLAN interface comes up,
4773 * find its associated SVI and update type-2 routes
4774 * with MAC-VLAN's MAC as RMAC and for type-5 routes
4775 * use SVI's MAC as RMAC.
4776 */
4777void zebra_vxlan_macvlan_up(struct interface *ifp)
4778{
05843a27 4779 struct zebra_l3vni *zl3vni = NULL;
0056f687
CS
4780 struct zebra_if *zif, *link_zif;
4781 struct interface *link_ifp, *link_if;
4782
4783 zif = ifp->info;
4784 assert(zif);
4785 link_ifp = zif->link;
4786 link_zif = link_ifp->info;
4787 assert(link_zif);
4788
4789 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
4790 link_zif->link_ifindex);
4791 zl3vni = zl3vni_from_svi(link_ifp, link_if);
4792 if (zl3vni) {
4793 /* associate with macvlan (VRR) interface */
4794 zl3vni->mac_vlan_if = ifp;
4795
4796 /* process oper-up */
4797 if (is_l3vni_oper_up(zl3vni))
4798 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4799 }
4800}
4801
13d60d35 4802/*
b7cfce93 4803 * Handle VxLAN interface down
13d60d35 4804 */
d62a17ae 4805int zebra_vxlan_if_down(struct interface *ifp)
13d60d35 4806{
d62a17ae 4807 vni_t vni;
b7cfce93
MK
4808 struct zebra_if *zif = NULL;
4809 struct zebra_l2info_vxlan *vxl = NULL;
05843a27 4810 struct zebra_l3vni *zl3vni = NULL;
f6371c34 4811 struct zebra_evpn *zevpn;
13d60d35 4812
2853fed6 4813 /* Check if EVPN is enabled. */
4814 if (!is_evpn_enabled())
d62a17ae 4815 return 0;
13d60d35 4816
d62a17ae 4817 zif = ifp->info;
4818 assert(zif);
4819 vxl = &zif->l2info.vxl;
4820 vni = vxl->vni;
13d60d35 4821
643215ce 4822 zl3vni = zl3vni_lookup(vni);
4823 if (zl3vni) {
b7cfce93 4824 /* process-if-down for l3-vni */
b7cfce93 4825 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
4826 zlog_debug("Intf %s(%u) L3-VNI %u is DOWN", ifp->name,
4827 ifp->ifindex, vni);
b7cfce93 4828
b7cfce93 4829 zebra_vxlan_process_l3vni_oper_down(zl3vni);
b7cfce93
MK
4830 } else {
4831 /* process if-down for l2-vni */
b7cfce93 4832 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
4833 zlog_debug("Intf %s(%u) L2-VNI %u is DOWN", ifp->name,
4834 ifp->ifindex, vni);
13d60d35 4835
b7cfce93 4836 /* Locate hash entry; it is expected to exist. */
8b5fdf2e 4837 zevpn = zebra_evpn_lookup(vni);
87d76d54 4838 if (!zevpn) {
9df414fe 4839 zlog_debug(
b7cfce93
MK
4840 "Failed to locate VNI hash at DOWN, IF %s(%u) VNI %u",
4841 ifp->name, ifp->ifindex, vni);
4842 return -1;
4843 }
13d60d35 4844
87d76d54 4845 assert(zevpn->vxlan_if == ifp);
13d60d35 4846
c7e83a4e
CS
4847 /* remove from l3-vni list */
4848 zl3vni = zl3vni_from_vrf(zevpn->vrf_id);
4849 if (zl3vni)
4850 listnode_delete(zl3vni->l2vnis, zevpn);
4851
b7cfce93 4852 /* Delete this VNI from BGP. */
8b5fdf2e 4853 zebra_evpn_send_del_to_client(zevpn);
2232a77c 4854
b7cfce93 4855 /* Free up all neighbors and MACs, if any. */
7cbae20a 4856 zebra_evpn_neigh_del_all(zevpn, 1, 0, DEL_ALL_NEIGH);
b2998086 4857 zebra_evpn_mac_del_all(zevpn, 1, 0, DEL_ALL_MAC);
13d60d35 4858
b7cfce93 4859 /* Free up all remote VTEPs, if any. */
8b5fdf2e 4860 zebra_evpn_vtep_del_all(zevpn, 1);
b7cfce93 4861 }
d62a17ae 4862 return 0;
13d60d35 4863}
4864
4865/*
4866 * Handle VxLAN interface up - update BGP if required.
4867 */
d62a17ae 4868int zebra_vxlan_if_up(struct interface *ifp)
13d60d35 4869{
d62a17ae 4870 vni_t vni;
b7cfce93
MK
4871 struct zebra_if *zif = NULL;
4872 struct zebra_l2info_vxlan *vxl = NULL;
f6371c34 4873 struct zebra_evpn *zevpn = NULL;
05843a27 4874 struct zebra_l3vni *zl3vni = NULL;
13d60d35 4875
2853fed6 4876 /* Check if EVPN is enabled. */
4877 if (!is_evpn_enabled())
d62a17ae 4878 return 0;
13d60d35 4879
d62a17ae 4880 zif = ifp->info;
4881 assert(zif);
4882 vxl = &zif->l2info.vxl;
4883 vni = vxl->vni;
13d60d35 4884
643215ce 4885 zl3vni = zl3vni_lookup(vni);
4886 if (zl3vni) {
b7cfce93 4887 /* we need to associate with SVI, if any, we can associate with
523cafc4 4888 * svi-if only after association with vxlan-intf is complete
4889 */
b7cfce93 4890 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
06d9cde5
CS
4891 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
4892
4893 if (IS_ZEBRA_DEBUG_VXLAN)
4894 zlog_debug("Intf %s(%u) L3-VNI %u is UP svi_if %s mac_vlan_if %s"
4895 , ifp->name, ifp->ifindex, vni,
4896 zl3vni->svi_if ? zl3vni->svi_if->name : "NIL",
4897 zl3vni->mac_vlan_if ?
4898 zl3vni->mac_vlan_if->name : "NIL");
b7cfce93
MK
4899
4900 if (is_l3vni_oper_up(zl3vni))
4901 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4902 } else {
4903 /* Handle L2-VNI add */
b7cfce93
MK
4904 struct interface *vlan_if = NULL;
4905
4906 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
4907 zlog_debug("Intf %s(%u) L2-VNI %u is UP", ifp->name,
4908 ifp->ifindex, vni);
b7cfce93
MK
4909
4910 /* Locate hash entry; it is expected to exist. */
8b5fdf2e 4911 zevpn = zebra_evpn_lookup(vni);
87d76d54 4912 if (!zevpn) {
9df414fe 4913 zlog_debug(
87d76d54 4914 "Failed to locate EVPN hash at UP, IF %s(%u) VNI %u",
b7cfce93
MK
4915 ifp->name, ifp->ifindex, vni);
4916 return -1;
4917 }
4918
87d76d54 4919 assert(zevpn->vxlan_if == ifp);
7cbae20a 4920 vlan_if = zvni_map_to_svi(vxl->access_vlan,
b7cfce93
MK
4921 zif->brslave_info.br_if);
4922 if (vlan_if) {
9daa5d47 4923 zevpn->svi_if = vlan_if;
096f7609
IR
4924 zevpn->vrf_id = vlan_if->vrf->vrf_id;
4925 zl3vni = zl3vni_from_vrf(vlan_if->vrf->vrf_id);
b7cfce93 4926 if (zl3vni)
c7e83a4e 4927 listnode_add_sort_nodup(zl3vni->l2vnis, zevpn);
b7cfce93
MK
4928 }
4929
4930 /* If part of a bridge, inform BGP about this VNI. */
4931 /* Also, read and populate local MACs and neighbors. */
4932 if (zif->brslave_info.br_if) {
8b5fdf2e
PR
4933 zebra_evpn_send_add_to_client(zevpn);
4934 zebra_evpn_read_mac_neigh(zevpn, ifp);
b7cfce93 4935 }
d62a17ae 4936 }
13d60d35 4937
d62a17ae 4938 return 0;
13d60d35 4939}
4940
4941/*
4942 * Handle VxLAN interface delete. Locate and remove entry in hash table
4943 * and update BGP, if required.
4944 */
d62a17ae 4945int zebra_vxlan_if_del(struct interface *ifp)
13d60d35 4946{
d62a17ae 4947 vni_t vni;
b7cfce93
MK
4948 struct zebra_if *zif = NULL;
4949 struct zebra_l2info_vxlan *vxl = NULL;
f6371c34 4950 struct zebra_evpn *zevpn = NULL;
05843a27 4951 struct zebra_l3vni *zl3vni = NULL;
13d60d35 4952
2853fed6 4953 /* Check if EVPN is enabled. */
4954 if (!is_evpn_enabled())
d62a17ae 4955 return 0;
13d60d35 4956
d62a17ae 4957 zif = ifp->info;
4958 assert(zif);
4959 vxl = &zif->l2info.vxl;
4960 vni = vxl->vni;
13d60d35 4961
643215ce 4962 zl3vni = zl3vni_lookup(vni);
4963 if (zl3vni) {
b7cfce93
MK
4964
4965 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
4966 zlog_debug("Del L3-VNI %u intf %s(%u)", vni, ifp->name,
4967 ifp->ifindex);
13d60d35 4968
b7cfce93
MK
4969 /* process oper-down for l3-vni */
4970 zebra_vxlan_process_l3vni_oper_down(zl3vni);
2232a77c 4971
b7cfce93 4972 /* remove the association with vxlan_if */
b67a60d2 4973 memset(&zl3vni->local_vtep_ip, 0, sizeof(struct in_addr));
b7cfce93
MK
4974 zl3vni->vxlan_if = NULL;
4975 } else {
13d60d35 4976
b7cfce93 4977 /* process if-del for l2-vni*/
b7cfce93 4978 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
4979 zlog_debug("Del L2-VNI %u intf %s(%u)", vni, ifp->name,
4980 ifp->ifindex);
b7cfce93
MK
4981
4982 /* Locate hash entry; it is expected to exist. */
8b5fdf2e 4983 zevpn = zebra_evpn_lookup(vni);
87d76d54 4984 if (!zevpn) {
9df414fe 4985 zlog_debug(
b7cfce93
MK
4986 "Failed to locate VNI hash at del, IF %s(%u) VNI %u",
4987 ifp->name, ifp->ifindex, vni);
4988 return 0;
4989 }
4990
4991 /* remove from l3-vni list */
87d76d54 4992 zl3vni = zl3vni_from_vrf(zevpn->vrf_id);
b7cfce93 4993 if (zl3vni)
87d76d54 4994 listnode_delete(zl3vni->l2vnis, zevpn);
b7cfce93 4995 /* Delete VNI from BGP. */
8b5fdf2e 4996 zebra_evpn_send_del_to_client(zevpn);
b7cfce93
MK
4997
4998 /* Free up all neighbors and MAC, if any. */
7cbae20a 4999 zebra_evpn_neigh_del_all(zevpn, 0, 0, DEL_ALL_NEIGH);
b2998086 5000 zebra_evpn_mac_del_all(zevpn, 0, 0, DEL_ALL_MAC);
b7cfce93
MK
5001
5002 /* Free up all remote VTEPs, if any. */
8b5fdf2e 5003 zebra_evpn_vtep_del_all(zevpn, 0);
b7cfce93
MK
5004
5005 /* Delete the hash entry. */
8b5fdf2e 5006 if (zebra_evpn_vxlan_del(zevpn)) {
e914ccbe 5007 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
87d76d54
PR
5008 "Failed to del EVPN hash %p, IF %s(%u) VNI %u",
5009 zevpn, ifp->name, ifp->ifindex, zevpn->vni);
b7cfce93
MK
5010 return -1;
5011 }
d62a17ae 5012 }
d62a17ae 5013 return 0;
13d60d35 5014}
5015
5016/*
5017 * Handle VxLAN interface update - change to tunnel IP, master or VLAN.
5018 */
d7c0a89a 5019int zebra_vxlan_if_update(struct interface *ifp, uint16_t chgflags)
d62a17ae 5020{
d62a17ae 5021 vni_t vni;
b7cfce93
MK
5022 struct zebra_if *zif = NULL;
5023 struct zebra_l2info_vxlan *vxl = NULL;
f6371c34 5024 struct zebra_evpn *zevpn = NULL;
05843a27 5025 struct zebra_l3vni *zl3vni = NULL;
9daa5d47 5026 struct interface *vlan_if = NULL;
d62a17ae 5027
2853fed6 5028 /* Check if EVPN is enabled. */
5029 if (!is_evpn_enabled())
d62a17ae 5030 return 0;
5031
5032 zif = ifp->info;
5033 assert(zif);
5034 vxl = &zif->l2info.vxl;
5035 vni = vxl->vni;
5036
643215ce 5037 zl3vni = zl3vni_lookup(vni);
5038 if (zl3vni) {
af026ae4 5039
b7cfce93
MK
5040 if (IS_ZEBRA_DEBUG_VXLAN)
5041 zlog_debug(
9bcef951 5042 "Update L3-VNI %u intf %s(%u) VLAN %u local IP %pI4 master %u chg 0x%x",
996c9314 5043 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
9bcef951 5044 &vxl->vtep_ip,
b7cfce93
MK
5045 zif->brslave_info.bridge_ifindex, chgflags);
5046
5047 /* Removed from bridge? Cleanup and return */
5048 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
5049 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
5050 zebra_vxlan_process_l3vni_oper_down(zl3vni);
5051 return 0;
5052 }
5053
c7620108
PG
5054 if ((chgflags & ZEBRA_VXLIF_MASTER_MAC_CHANGE)
5055 && if_is_operative(ifp) && is_l3vni_oper_up(zl3vni)) {
5056 zebra_vxlan_process_l3vni_oper_down(zl3vni);
5057 zebra_vxlan_process_l3vni_oper_up(zl3vni);
5058 return 0;
5059 }
5060
b7cfce93 5061 /* access-vlan change - process oper down, associate with new
523cafc4 5062 * svi_if and then process oper up again
5063 */
b7cfce93
MK
5064 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
5065 if (if_is_operative(ifp)) {
5066 zebra_vxlan_process_l3vni_oper_down(zl3vni);
5067 zl3vni->svi_if = NULL;
5068 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
06d9cde5
CS
5069 zl3vni->mac_vlan_if =
5070 zl3vni_map_to_mac_vlan_if(zl3vni);
bca63dc8 5071 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93
MK
5072 if (is_l3vni_oper_up(zl3vni))
5073 zebra_vxlan_process_l3vni_oper_up(
996c9314 5074 zl3vni);
b7cfce93
MK
5075 }
5076 }
d62a17ae 5077
12eeac84
MK
5078 /*
5079 * local-ip change - process oper down, associate with new
5080 * local-ip and then process oper up again
5081 */
5082 if (chgflags & ZEBRA_VXLIF_LOCAL_IP_CHANGE) {
5083 if (if_is_operative(ifp)) {
5084 zebra_vxlan_process_l3vni_oper_down(zl3vni);
5085 zl3vni->local_vtep_ip = vxl->vtep_ip;
5086 if (is_l3vni_oper_up(zl3vni))
5087 zebra_vxlan_process_l3vni_oper_up(
996c9314 5088 zl3vni);
12eeac84
MK
5089 }
5090 }
5091
bca63dc8
MK
5092 /* Update local tunnel IP. */
5093 zl3vni->local_vtep_ip = vxl->vtep_ip;
5094
12eeac84
MK
5095 /* if we have a valid new master, process l3-vni oper up */
5096 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE) {
5097 if (if_is_operative(ifp) && is_l3vni_oper_up(zl3vni))
b7cfce93
MK
5098 zebra_vxlan_process_l3vni_oper_up(zl3vni);
5099 }
5100 } else {
d62a17ae 5101
b7cfce93 5102 /* Update VNI hash. */
8b5fdf2e 5103 zevpn = zebra_evpn_lookup(vni);
87d76d54 5104 if (!zevpn) {
9df414fe 5105 zlog_debug(
87d76d54 5106 "Failed to find EVPN hash on update, IF %s(%u) VNI %u",
b7cfce93
MK
5107 ifp->name, ifp->ifindex, vni);
5108 return -1;
5109 }
d62a17ae 5110
b7cfce93
MK
5111 if (IS_ZEBRA_DEBUG_VXLAN)
5112 zlog_debug(
9bcef951 5113 "Update L2-VNI %u intf %s(%u) VLAN %u local IP %pI4 master %u chg 0x%x",
996c9314 5114 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
9bcef951 5115 &vxl->vtep_ip,
b7cfce93
MK
5116 zif->brslave_info.bridge_ifindex, chgflags);
5117
5118 /* Removed from bridge? Cleanup and return */
5119 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
5120 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
5121 /* Delete from client, remove all remote VTEPs */
5122 /* Also, free up all MACs and neighbors. */
9daa5d47 5123 zevpn->svi_if = NULL;
8b5fdf2e 5124 zebra_evpn_send_del_to_client(zevpn);
7cbae20a 5125 zebra_evpn_neigh_del_all(zevpn, 1, 0, DEL_ALL_NEIGH);
b2998086 5126 zebra_evpn_mac_del_all(zevpn, 1, 0, DEL_ALL_MAC);
8b5fdf2e 5127 zebra_evpn_vtep_del_all(zevpn, 1);
b7cfce93
MK
5128 return 0;
5129 }
d62a17ae 5130
b7cfce93
MK
5131 /* Handle other changes. */
5132 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
5133 /* Remove all existing local neigh and MACs for this VNI
5134 * (including from BGP)
5135 */
7cbae20a 5136 zebra_evpn_neigh_del_all(zevpn, 0, 1, DEL_LOCAL_MAC);
b2998086 5137 zebra_evpn_mac_del_all(zevpn, 0, 1, DEL_LOCAL_MAC);
b7cfce93 5138 }
d62a17ae 5139
87d76d54
PR
5140 if (zevpn->local_vtep_ip.s_addr != vxl->vtep_ip.s_addr ||
5141 zevpn->mcast_grp.s_addr != vxl->mcast_grp.s_addr) {
5142 zebra_vxlan_sg_deref(zevpn->local_vtep_ip,
5143 zevpn->mcast_grp);
abfa0a96 5144 zebra_vxlan_sg_ref(vxl->vtep_ip, vxl->mcast_grp);
87d76d54
PR
5145 zevpn->local_vtep_ip = vxl->vtep_ip;
5146 zevpn->mcast_grp = vxl->mcast_grp;
ce5160c0
AK
5147 /* on local vtep-ip check if ES orig-ip
5148 * needs to be updated
5149 */
87d76d54 5150 zebra_evpn_es_set_base_evpn(zevpn);
abfa0a96 5151 }
87d76d54 5152 zevpn_vxlan_if_set(zevpn, ifp, true /* set */);
9daa5d47
AD
5153 vlan_if = zvni_map_to_svi(vxl->access_vlan,
5154 zif->brslave_info.br_if);
c4992a2f 5155 if (vlan_if) {
9daa5d47 5156 zevpn->svi_if = vlan_if;
c4992a2f 5157 zevpn->vrf_id = vlan_if->vrf->vrf_id;
5158 zl3vni = zl3vni_from_vrf(vlan_if->vrf->vrf_id);
5159 if (zl3vni)
5160 listnode_add_sort_nodup(zl3vni->l2vnis, zevpn);
5161 }
9daa5d47 5162
b7cfce93
MK
5163 /* Take further actions needed.
5164 * Note that if we are here, there is a change of interest.
5165 */
5166 /* If down or not mapped to a bridge, we're done. */
5167 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
5168 return 0;
d62a17ae 5169
b7cfce93 5170 /* Inform BGP, if there is a change of interest. */
a606d915 5171 if (chgflags &
5172 (ZEBRA_VXLIF_MASTER_CHANGE | ZEBRA_VXLIF_LOCAL_IP_CHANGE |
5173 ZEBRA_VXLIF_MCAST_GRP_CHANGE | ZEBRA_VXLIF_VLAN_CHANGE))
8b5fdf2e 5174 zebra_evpn_send_add_to_client(zevpn);
b7cfce93
MK
5175
5176 /* If there is a valid new master or a VLAN mapping change,
5177 * read and populate local MACs and neighbors.
5178 * Also, reinstall any remote MACs and neighbors
5179 * for this VNI (based on new VLAN).
5180 */
5181 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
8b5fdf2e 5182 zebra_evpn_read_mac_neigh(zevpn, ifp);
b7cfce93
MK
5183 else if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
5184 struct mac_walk_ctx m_wctx;
5185 struct neigh_walk_ctx n_wctx;
5186
8b5fdf2e 5187 zebra_evpn_read_mac_neigh(zevpn, ifp);
b7cfce93 5188
6006b807 5189 memset(&m_wctx, 0, sizeof(m_wctx));
87d76d54 5190 m_wctx.zevpn = zevpn;
8b5fdf2e
PR
5191 hash_iterate(zevpn->mac_table,
5192 zebra_evpn_install_mac_hash, &m_wctx);
b7cfce93 5193
6006b807 5194 memset(&n_wctx, 0, sizeof(n_wctx));
87d76d54 5195 n_wctx.zevpn = zevpn;
7cbae20a
PR
5196 hash_iterate(zevpn->neigh_table,
5197 zebra_evpn_install_neigh_hash, &n_wctx);
b7cfce93 5198 }
d62a17ae 5199 }
5200
5201 return 0;
13d60d35 5202}
5203
5204/*
5205 * Handle VxLAN interface add.
5206 */
d62a17ae 5207int zebra_vxlan_if_add(struct interface *ifp)
13d60d35 5208{
d62a17ae 5209 vni_t vni;
b7cfce93
MK
5210 struct zebra_if *zif = NULL;
5211 struct zebra_l2info_vxlan *vxl = NULL;
f6371c34 5212 struct zebra_evpn *zevpn = NULL;
05843a27 5213 struct zebra_l3vni *zl3vni = NULL;
13d60d35 5214
2853fed6 5215 /* Check if EVPN is enabled. */
5216 if (!is_evpn_enabled())
d62a17ae 5217 return 0;
13d60d35 5218
d62a17ae 5219 zif = ifp->info;
5220 assert(zif);
5221 vxl = &zif->l2info.vxl;
5222 vni = vxl->vni;
13d60d35 5223
643215ce 5224 zl3vni = zl3vni_lookup(vni);
5225 if (zl3vni) {
13d60d35 5226
b7cfce93 5227 /* process if-add for l3-vni*/
b7cfce93
MK
5228 if (IS_ZEBRA_DEBUG_VXLAN)
5229 zlog_debug(
9bcef951 5230 "Add L3-VNI %u intf %s(%u) VLAN %u local IP %pI4 master %u",
996c9314 5231 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
9bcef951 5232 &vxl->vtep_ip,
b7cfce93
MK
5233 zif->brslave_info.bridge_ifindex);
5234
b7cfce93 5235 /* associate with vxlan_if */
b67a60d2 5236 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93
MK
5237 zl3vni->vxlan_if = ifp;
5238
5239 /* Associate with SVI, if any. We can associate with svi-if only
5240 * after association with vxlan_if is complete */
5241 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
5242
06d9cde5
CS
5243 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
5244
b7cfce93
MK
5245 if (is_l3vni_oper_up(zl3vni))
5246 zebra_vxlan_process_l3vni_oper_up(zl3vni);
5247 } else {
5248
5249 /* process if-add for l2-vni */
b7cfce93
MK
5250 struct interface *vlan_if = NULL;
5251
87d76d54 5252 /* Create or update EVPN hash. */
8b5fdf2e 5253 zevpn = zebra_evpn_lookup(vni);
97511d01 5254 if (!zevpn)
8b5fdf2e 5255 zevpn = zebra_evpn_add(vni);
b7cfce93 5256
87d76d54
PR
5257 if (zevpn->local_vtep_ip.s_addr != vxl->vtep_ip.s_addr ||
5258 zevpn->mcast_grp.s_addr != vxl->mcast_grp.s_addr) {
5259 zebra_vxlan_sg_deref(zevpn->local_vtep_ip,
5260 zevpn->mcast_grp);
abfa0a96 5261 zebra_vxlan_sg_ref(vxl->vtep_ip, vxl->mcast_grp);
87d76d54
PR
5262 zevpn->local_vtep_ip = vxl->vtep_ip;
5263 zevpn->mcast_grp = vxl->mcast_grp;
ce5160c0
AK
5264 /* on local vtep-ip check if ES orig-ip
5265 * needs to be updated
5266 */
87d76d54 5267 zebra_evpn_es_set_base_evpn(zevpn);
abfa0a96 5268 }
87d76d54 5269 zevpn_vxlan_if_set(zevpn, ifp, true /* set */);
7cbae20a 5270 vlan_if = zvni_map_to_svi(vxl->access_vlan,
b7cfce93
MK
5271 zif->brslave_info.br_if);
5272 if (vlan_if) {
9daa5d47 5273 zevpn->svi_if = vlan_if;
096f7609
IR
5274 zevpn->vrf_id = vlan_if->vrf->vrf_id;
5275 zl3vni = zl3vni_from_vrf(vlan_if->vrf->vrf_id);
b7cfce93 5276 if (zl3vni)
c7e83a4e 5277 listnode_add_sort_nodup(zl3vni->l2vnis, zevpn);
b7cfce93
MK
5278 }
5279
f6371c34 5280 if (IS_ZEBRA_DEBUG_VXLAN)
b7cfce93 5281 zlog_debug(
f6371c34 5282 "Add L2-VNI %u VRF %s intf %s(%u) VLAN %u local IP %pI4 mcast_grp %pI4 master %u",
b7cfce93 5283 vni,
096f7609 5284 vlan_if ? vlan_if->vrf->name : VRF_DEFAULT_NAME,
996c9314 5285 ifp->name, ifp->ifindex, vxl->access_vlan,
f6371c34 5286 &vxl->vtep_ip, &vxl->mcast_grp,
b7cfce93
MK
5287 zif->brslave_info.bridge_ifindex);
5288
5289 /* If down or not mapped to a bridge, we're done. */
5290 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
5291 return 0;
5292
5293 /* Inform BGP */
8b5fdf2e 5294 zebra_evpn_send_add_to_client(zevpn);
b7cfce93
MK
5295
5296 /* Read and populate local MACs and neighbors */
8b5fdf2e 5297 zebra_evpn_read_mac_neigh(zevpn, ifp);
b7cfce93
MK
5298 }
5299
5300 return 0;
5301}
5302
996c9314
LB
5303int zebra_vxlan_process_vrf_vni_cmd(struct zebra_vrf *zvrf, vni_t vni,
5304 char *err, int err_str_sz, int filter,
5305 int add)
b7cfce93 5306{
05843a27 5307 struct zebra_l3vni *zl3vni = NULL;
5e53dce3 5308 struct zebra_vrf *zvrf_evpn = NULL;
b7cfce93 5309
5e53dce3 5310 zvrf_evpn = zebra_vrf_get_evpn();
b7cfce93
MK
5311
5312 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 5313 zlog_debug("vrf %s vni %u %s", zvrf_name(zvrf), vni,
b7cfce93
MK
5314 add ? "ADD" : "DEL");
5315
5316 if (add) {
b7cfce93
MK
5317 /* check if the vni is already present under zvrf */
5318 if (zvrf->l3vni) {
3f02fbab 5319 snprintf(err, err_str_sz,
b7cfce93
MK
5320 "VNI is already configured under the vrf");
5321 return -1;
5322 }
5323
5324 /* check if this VNI is already present in the system */
5325 zl3vni = zl3vni_lookup(vni);
5326 if (zl3vni) {
3f02fbab 5327 snprintf(err, err_str_sz,
b7cfce93
MK
5328 "VNI is already configured as L3-VNI");
5329 return -1;
5330 }
5331
41a8b88c 5332 /* Remove L2VNI if present */
5333 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
5334
b7cfce93
MK
5335 /* add the L3-VNI to the global table */
5336 zl3vni = zl3vni_add(vni, zvrf_id(zvrf));
b7cfce93
MK
5337
5338 /* associate the vrf with vni */
5339 zvrf->l3vni = vni;
5340
c48d9f5f
MK
5341 /* set the filter in l3vni to denote if we are using l3vni only
5342 * for prefix routes
5343 */
5344 if (filter)
5345 SET_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY);
5346
b7cfce93 5347 /* associate with vxlan-intf;
523cafc4 5348 * we need to associate with the vxlan-intf first
5349 */
b7cfce93
MK
5350 zl3vni->vxlan_if = zl3vni_map_to_vxlan_if(zl3vni);
5351
5352 /* associate with corresponding SVI interface, we can associate
5353 * with svi-if only after vxlan interface association is
523cafc4 5354 * complete
5355 */
b7cfce93
MK
5356 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
5357
06d9cde5
CS
5358 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
5359
5360 if (IS_ZEBRA_DEBUG_VXLAN)
15569c58
DA
5361 zlog_debug(
5362 "%s: l3vni %u svi_if %s mac_vlan_if %s",
5363 __func__, vni,
5364 zl3vni->svi_if ? zl3vni->svi_if->name : "NIL",
5365 zl3vni->mac_vlan_if ? zl3vni->mac_vlan_if->name
5366 : "NIL");
06d9cde5 5367
b7cfce93 5368 /* formulate l2vni list */
87d76d54 5369 hash_iterate(zvrf_evpn->evpn_table, zevpn_add_to_l3vni_list,
996c9314 5370 zl3vni);
b7cfce93
MK
5371
5372 if (is_l3vni_oper_up(zl3vni))
5373 zebra_vxlan_process_l3vni_oper_up(zl3vni);
5374
5375 } else {
5376 zl3vni = zl3vni_lookup(vni);
5377 if (!zl3vni) {
3f02fbab 5378 snprintf(err, err_str_sz, "VNI doesn't exist");
d62a17ae 5379 return -1;
5380 }
b7cfce93 5381
7a6ca8a6
KA
5382 if (zvrf->l3vni != vni) {
5383 snprintf(err, err_str_sz,
5384 "VNI %d doesn't exist in VRF: %s",
5385 vni, zvrf->vrf->name);
5386 return -1;
5387 }
5388
cf299714
MK
5389 if (filter && !CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)) {
5390 snprintf(err, ERR_STR_SZ,
5391 "prefix-routes-only is not set for the vni");
5392 return -1;
5393 }
5394
b7cfce93
MK
5395 zebra_vxlan_process_l3vni_oper_down(zl3vni);
5396
5e06422c 5397 /* delete and uninstall all rmacs */
996c9314 5398 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
5e06422c
MK
5399 zl3vni);
5400
5401 /* delete and uninstall all next-hops */
996c9314 5402 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
5e06422c
MK
5403 zl3vni);
5404
b7cfce93
MK
5405 zvrf->l3vni = 0;
5406 zl3vni_del(zl3vni);
5407
3b0a590b 5408 /* Add L2VNI for this VNI */
b7cfce93 5409 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
d62a17ae 5410 }
b7cfce93
MK
5411 return 0;
5412}
13d60d35 5413
84915b0a 5414int zebra_vxlan_vrf_enable(struct zebra_vrf *zvrf)
5415{
05843a27 5416 struct zebra_l3vni *zl3vni = NULL;
84915b0a 5417
5418 if (zvrf->l3vni)
5419 zl3vni = zl3vni_lookup(zvrf->l3vni);
5420 if (!zl3vni)
5421 return 0;
5422
5423 zl3vni->vrf_id = zvrf_id(zvrf);
5424 if (is_l3vni_oper_up(zl3vni))
5425 zebra_vxlan_process_l3vni_oper_up(zl3vni);
5426 return 0;
5427}
5428
5429int zebra_vxlan_vrf_disable(struct zebra_vrf *zvrf)
b7cfce93 5430{
05843a27 5431 struct zebra_l3vni *zl3vni = NULL;
13d60d35 5432
84915b0a 5433 if (zvrf->l3vni)
5434 zl3vni = zl3vni_lookup(zvrf->l3vni);
b7cfce93 5435 if (!zl3vni)
d62a17ae 5436 return 0;
13d60d35 5437
b7cfce93 5438 zebra_vxlan_process_l3vni_oper_down(zl3vni);
92475ca4
CS
5439
5440 /* delete and uninstall all rmacs */
5441 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry, zl3vni);
5442 /* delete and uninstall all next-hops */
5443 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry, zl3vni);
5444
5445 zl3vni->vrf_id = VRF_UNKNOWN;
5446
84915b0a 5447 return 0;
5448}
5449
5450int zebra_vxlan_vrf_delete(struct zebra_vrf *zvrf)
5451{
05843a27 5452 struct zebra_l3vni *zl3vni = NULL;
84915b0a 5453 vni_t vni;
5454
5455 if (zvrf->l3vni)
5456 zl3vni = zl3vni_lookup(zvrf->l3vni);
5457 if (!zl3vni)
5458 return 0;
5459
5460 vni = zl3vni->vni;
b7cfce93 5461 zl3vni_del(zl3vni);
84915b0a 5462 zebra_vxlan_handle_vni_transition(zvrf, vni, 0);
2232a77c 5463
d62a17ae 5464 return 0;
13d60d35 5465}
5466
fbac9605
DS
5467/*
5468 * Handle message from client to specify the flooding mechanism for
5469 * BUM packets. The default is to do head-end (ingress) replication
5470 * and the other supported option is to disable it. This applies to
5471 * all BUM traffic and disabling it applies to both the transmit and
5472 * receive direction.
5473 */
5474void zebra_vxlan_flood_control(ZAPI_HANDLER_ARGS)
5475{
5476 struct stream *s;
5477 enum vxlan_flood_control flood_ctrl;
5478
986512a3 5479 if (!EVPN_ENABLED(zvrf)) {
a0b0b5c8 5480 zlog_err("EVPN flood control for non-EVPN VRF %u",
fbac9605
DS
5481 zvrf_id(zvrf));
5482 return;
5483 }
5484
5485 s = msg;
5486 STREAM_GETC(s, flood_ctrl);
5487
5488 if (IS_ZEBRA_DEBUG_VXLAN)
5489 zlog_debug("EVPN flood control %u, currently %u",
5490 flood_ctrl, zvrf->vxlan_flood_ctrl);
5491
5492 if (zvrf->vxlan_flood_ctrl == flood_ctrl)
5493 return;
5494
5495 zvrf->vxlan_flood_ctrl = flood_ctrl;
5496
5497 /* Install or uninstall flood entries corresponding to
5498 * remote VTEPs.
5499 */
8b5fdf2e 5500 hash_iterate(zvrf->evpn_table, zebra_evpn_handle_flooding_remote_vteps,
fbac9605
DS
5501 zvrf);
5502
5503stream_failure:
5504 return;
5505}
5506
278e26de
CS
5507/*
5508 * Handle message from client to enable/disable advertisement of svi macip
5509 * routes
5510 */
5511void zebra_vxlan_advertise_svi_macip(ZAPI_HANDLER_ARGS)
5512{
5513 struct stream *s;
5514 int advertise;
5515 vni_t vni = 0;
f6371c34 5516 struct zebra_evpn *zevpn = NULL;
278e26de
CS
5517 struct interface *ifp = NULL;
5518
986512a3 5519 if (!EVPN_ENABLED(zvrf)) {
27627f9a 5520 zlog_debug("EVPN SVI-MACIP Adv for non-EVPN VRF %u",
a0b0b5c8 5521 zvrf_id(zvrf));
278e26de
CS
5522 return;
5523 }
5524
5525 s = msg;
5526 STREAM_GETC(s, advertise);
5527 STREAM_GETL(s, vni);
5528
5529 if (!vni) {
5530 if (IS_ZEBRA_DEBUG_VXLAN)
27627f9a 5531 zlog_debug("EVPN SVI-MACIP Adv %s, currently %s",
278e26de 5532 advertise ? "enabled" : "disabled",
838cef6d 5533 advertise_svi_macip_enabled(NULL)
278e26de
CS
5534 ? "enabled"
5535 : "disabled");
5536
5537 if (zvrf->advertise_svi_macip == advertise)
5538 return;
5539
5540
5541 if (advertise) {
5542 zvrf->advertise_svi_macip = advertise;
87d76d54 5543 hash_iterate(zvrf->evpn_table,
8b5fdf2e
PR
5544 zebra_evpn_gw_macip_add_for_evpn_hash,
5545 NULL);
278e26de 5546 } else {
87d76d54 5547 hash_iterate(zvrf->evpn_table,
8b5fdf2e
PR
5548 zebra_evpn_svi_macip_del_for_evpn_hash,
5549 NULL);
278e26de
CS
5550 zvrf->advertise_svi_macip = advertise;
5551 }
5552
5553 } else {
5554 struct zebra_if *zif = NULL;
5555 struct zebra_l2info_vxlan zl2_info;
5556 struct interface *vlan_if = NULL;
5557
8b5fdf2e 5558 zevpn = zebra_evpn_lookup(vni);
87d76d54 5559 if (!zevpn)
278e26de
CS
5560 return;
5561
5562 if (IS_ZEBRA_DEBUG_VXLAN)
5563 zlog_debug(
5564 "EVPN SVI macip Adv %s on VNI %d , currently %s",
5565 advertise ? "enabled" : "disabled", vni,
87d76d54 5566 advertise_svi_macip_enabled(zevpn)
278e26de
CS
5567 ? "enabled"
5568 : "disabled");
5569
87d76d54 5570 if (zevpn->advertise_svi_macip == advertise)
278e26de
CS
5571 return;
5572
3dacbb9d
CS
5573 /* Store flag even though SVI is not present.
5574 * Once SVI comes up triggers self MAC-IP route add.
5575 */
87d76d54 5576 zevpn->advertise_svi_macip = advertise;
3dacbb9d 5577
87d76d54 5578 ifp = zevpn->vxlan_if;
278e26de
CS
5579 if (!ifp)
5580 return;
5581
5582 zif = ifp->info;
5583
5584 /* If down or not mapped to a bridge, we're done. */
5585 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
5586 return;
5587
5588 zl2_info = zif->l2info.vxl;
7cbae20a 5589 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
278e26de
CS
5590 zif->brslave_info.br_if);
5591 if (!vlan_if)
5592 return;
5593
5594 if (advertise) {
278e26de 5595 /* Add primary SVI MAC-IP */
8b5fdf2e 5596 zebra_evpn_add_macip_for_intf(vlan_if, zevpn);
278e26de 5597 } else {
3dacbb9d 5598 /* Del primary SVI MAC-IP */
8b5fdf2e 5599 zebra_evpn_del_macip_for_intf(vlan_if, zevpn);
278e26de
CS
5600 }
5601 }
5602
5603stream_failure:
5604 return;
5605}
5606
31310b25
MK
5607/*
5608 * Handle message from client to enable/disable advertisement of g/w macip
5609 * routes
5610 */
89f4e507 5611void zebra_vxlan_advertise_subnet(ZAPI_HANDLER_ARGS)
31310b25
MK
5612{
5613 struct stream *s;
5614 int advertise;
5615 vni_t vni = 0;
f6371c34 5616 struct zebra_evpn *zevpn = NULL;
31310b25
MK
5617 struct interface *ifp = NULL;
5618 struct zebra_if *zif = NULL;
5619 struct zebra_l2info_vxlan zl2_info;
5620 struct interface *vlan_if = NULL;
5621
986512a3 5622 if (!EVPN_ENABLED(zvrf)) {
a0b0b5c8
T
5623 zlog_debug("EVPN GW-MACIP Adv for non-EVPN VRF %u",
5624 zvrf_id(zvrf));
8068a649 5625 return;
31310b25
MK
5626 }
5627
1002497a 5628 s = msg;
2017b3ea 5629 STREAM_GETC(s, advertise);
7fb29f49 5630 STREAM_GET(&vni, s, 3);
31310b25 5631
8b5fdf2e 5632 zevpn = zebra_evpn_lookup(vni);
87d76d54 5633 if (!zevpn)
8068a649 5634 return;
31310b25 5635
87d76d54 5636 if (zevpn->advertise_subnet == advertise)
8068a649 5637 return;
31310b25
MK
5638
5639 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
5640 zlog_debug("EVPN subnet Adv %s on VNI %d , currently %s",
5641 advertise ? "enabled" : "disabled", vni,
87d76d54 5642 zevpn->advertise_subnet ? "enabled" : "disabled");
31310b25
MK
5643
5644
87d76d54 5645 zevpn->advertise_subnet = advertise;
31310b25 5646
87d76d54 5647 ifp = zevpn->vxlan_if;
31310b25 5648 if (!ifp)
8068a649 5649 return;
31310b25
MK
5650
5651 zif = ifp->info;
5652
5653 /* If down or not mapped to a bridge, we're done. */
5654 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8068a649 5655 return;
31310b25
MK
5656
5657 zl2_info = zif->l2info.vxl;
5658
996c9314 5659 vlan_if =
7cbae20a 5660 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
31310b25 5661 if (!vlan_if)
8068a649 5662 return;
31310b25 5663
87d76d54 5664 if (zevpn->advertise_subnet)
8b5fdf2e 5665 zebra_evpn_advertise_subnet(zevpn, vlan_if, 1);
31310b25 5666 else
8b5fdf2e 5667 zebra_evpn_advertise_subnet(zevpn, vlan_if, 0);
2017b3ea
DS
5668
5669stream_failure:
5670 return;
31310b25
MK
5671}
5672
1a98c087
MK
5673/*
5674 * Handle message from client to enable/disable advertisement of g/w macip
5675 * routes
5676 */
89f4e507 5677void zebra_vxlan_advertise_gw_macip(ZAPI_HANDLER_ARGS)
1a98c087
MK
5678{
5679 struct stream *s;
5680 int advertise;
5681 vni_t vni = 0;
f6371c34 5682 struct zebra_evpn *zevpn = NULL;
b5ebdc9b 5683 struct interface *ifp = NULL;
1a98c087 5684
986512a3 5685 if (!EVPN_ENABLED(zvrf)) {
a0b0b5c8 5686 zlog_debug("EVPN GW-MACIP Adv for non-EVPN VRF %u",
9df414fe 5687 zvrf_id(zvrf));
8068a649 5688 return;
2853fed6 5689 }
5690
1002497a 5691 s = msg;
ec93aa12 5692 STREAM_GETC(s, advertise);
cc6d5476 5693 STREAM_GETL(s, vni);
1a98c087
MK
5694
5695 if (!vni) {
5696 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 5697 zlog_debug("EVPN gateway macip Adv %s, currently %s",
1a98c087 5698 advertise ? "enabled" : "disabled",
2853fed6 5699 advertise_gw_macip_enabled(NULL)
1a98c087
MK
5700 ? "enabled"
5701 : "disabled");
5702
5703 if (zvrf->advertise_gw_macip == advertise)
8068a649 5704 return;
1a98c087
MK
5705
5706 zvrf->advertise_gw_macip = advertise;
5707
87d76d54
PR
5708 if (advertise_gw_macip_enabled(zevpn))
5709 hash_iterate(zvrf->evpn_table,
8b5fdf2e
PR
5710 zebra_evpn_gw_macip_add_for_evpn_hash,
5711 NULL);
1a98c087 5712 else
87d76d54 5713 hash_iterate(zvrf->evpn_table,
8b5fdf2e
PR
5714 zebra_evpn_gw_macip_del_for_evpn_hash,
5715 NULL);
1a98c087
MK
5716
5717 } else {
5718 struct zebra_if *zif = NULL;
5719 struct zebra_l2info_vxlan zl2_info;
5720 struct interface *vlan_if = NULL;
5721 struct interface *vrr_if = NULL;
5722
8b5fdf2e 5723 zevpn = zebra_evpn_lookup(vni);
87d76d54 5724 if (!zevpn)
124ead27 5725 return;
01a6143b 5726
1a98c087
MK
5727 if (IS_ZEBRA_DEBUG_VXLAN)
5728 zlog_debug(
2853fed6 5729 "EVPN gateway macip Adv %s on VNI %d , currently %s",
1a98c087 5730 advertise ? "enabled" : "disabled", vni,
87d76d54 5731 advertise_gw_macip_enabled(zevpn) ? "enabled"
996c9314 5732 : "disabled");
1a98c087 5733
87d76d54 5734 if (zevpn->advertise_gw_macip == advertise)
8068a649 5735 return;
1a98c087 5736
87d76d54 5737 zevpn->advertise_gw_macip = advertise;
1a98c087 5738
87d76d54 5739 ifp = zevpn->vxlan_if;
b5ebdc9b 5740 if (!ifp)
8068a649 5741 return;
b5ebdc9b 5742
5743 zif = ifp->info;
5744
5745 /* If down or not mapped to a bridge, we're done. */
b682f6de 5746 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8068a649 5747 return;
b5ebdc9b 5748
1a98c087
MK
5749 zl2_info = zif->l2info.vxl;
5750
7cbae20a 5751 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
1a98c087
MK
5752 zif->brslave_info.br_if);
5753 if (!vlan_if)
8068a649 5754 return;
1a98c087 5755
87d76d54 5756 if (advertise_gw_macip_enabled(zevpn)) {
1a98c087 5757 /* Add primary SVI MAC-IP */
8b5fdf2e 5758 zebra_evpn_add_macip_for_intf(vlan_if, zevpn);
1a98c087
MK
5759
5760 /* Add VRR MAC-IP - if any*/
5761 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
5762 if (vrr_if)
8b5fdf2e 5763 zebra_evpn_add_macip_for_intf(vrr_if, zevpn);
1a98c087
MK
5764 } else {
5765 /* Del primary MAC-IP */
8b5fdf2e 5766 zebra_evpn_del_macip_for_intf(vlan_if, zevpn);
1a98c087
MK
5767
5768 /* Del VRR MAC-IP - if any*/
5769 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
5770 if (vrr_if)
8b5fdf2e 5771 zebra_evpn_del_macip_for_intf(vrr_if, zevpn);
1a98c087
MK
5772 }
5773 }
5774
ec93aa12 5775stream_failure:
8068a649 5776 return;
1a98c087
MK
5777}
5778
2961d060
PG
5779static int macfdb_read_ns(struct ns *ns,
5780 void *_in_param __attribute__((unused)),
5781 void **out_param __attribute__((unused)))
6265fbe5 5782{
2961d060
PG
5783 struct zebra_ns *zns = ns->info;
5784
6265fbe5 5785 macfdb_read(zns);
2961d060 5786 return NS_WALK_CONTINUE;
6265fbe5
PG
5787}
5788
2961d060
PG
5789static int neigh_read_ns(struct ns *ns,
5790 void *_in_param __attribute__((unused)),
5791 void **out_param __attribute__((unused)))
6265fbe5 5792{
2961d060
PG
5793 struct zebra_ns *zns = ns->info;
5794
6265fbe5 5795 neigh_read(zns);
2961d060 5796 return NS_WALK_CONTINUE;
6265fbe5 5797}
1a98c087 5798
13d60d35 5799/*
5800 * Handle message from client to learn (or stop learning) about VNIs and MACs.
5801 * When enabled, the VNI hash table will be built and MAC FDB table read;
5802 * when disabled, the entries should be deleted and remote VTEPs and MACs
5803 * uninstalled from the kernel.
fbac9605
DS
5804 * This also informs the setting for BUM handling at the time this change
5805 * occurs; it is relevant only when specifying "learn".
13d60d35 5806 */
89f4e507 5807void zebra_vxlan_advertise_all_vni(ZAPI_HANDLER_ARGS)
13d60d35 5808{
655b04d1
MK
5809 struct stream *s = NULL;
5810 int advertise = 0;
fbac9605 5811 enum vxlan_flood_control flood_ctrl;
13d60d35 5812
77b998fb
T
5813 /* Mismatch between EVPN VRF and current VRF (should be prevented by
5814 * bgpd's cli) */
f920dd6d 5815 if (is_evpn_enabled() && !EVPN_ENABLED(zvrf))
8068a649 5816 return;
2853fed6 5817
1002497a 5818 s = msg;
ec93aa12 5819 STREAM_GETC(s, advertise);
fbac9605 5820 STREAM_GETC(s, flood_ctrl);
13d60d35 5821
d62a17ae 5822 if (IS_ZEBRA_DEBUG_VXLAN)
150971b5
T
5823 zlog_debug("EVPN VRF %s(%u) VNI Adv %s, currently %s, flood control %u",
5824 zvrf_name(zvrf), zvrf_id(zvrf),
d62a17ae 5825 advertise ? "enabled" : "disabled",
fbac9605
DS
5826 is_evpn_enabled() ? "enabled" : "disabled",
5827 flood_ctrl);
13d60d35 5828
d62a17ae 5829 if (zvrf->advertise_all_vni == advertise)
8068a649 5830 return;
13d60d35 5831
d62a17ae 5832 zvrf->advertise_all_vni = advertise;
f920dd6d 5833 if (EVPN_ENABLED(zvrf)) {
0fb2ad05 5834 zrouter.evpn_vrf = zvrf;
150971b5 5835
fbac9605
DS
5836 /* Note BUM handling */
5837 zvrf->vxlan_flood_ctrl = flood_ctrl;
5838
ce5160c0
AK
5839 /* Replay all ESs */
5840 zebra_evpn_es_send_all_to_client(true /* add */);
5841
87d76d54
PR
5842 /* Build EVPN hash table and inform BGP. */
5843 zevpn_build_hash_table();
2232a77c 5844
1a98c087 5845 /* Add all SVI (L3 GW) MACs to BGP*/
8b5fdf2e
PR
5846 hash_iterate(zvrf->evpn_table,
5847 zebra_evpn_gw_macip_add_for_evpn_hash, NULL);
1a98c087 5848
d62a17ae 5849 /* Read the MAC FDB */
2961d060 5850 ns_walk_func(macfdb_read_ns, NULL, NULL);
2232a77c 5851
d62a17ae 5852 /* Read neighbors */
2961d060 5853 ns_walk_func(neigh_read_ns, NULL, NULL);
d62a17ae 5854 } else {
87d76d54 5855 /* Cleanup VTEPs for all EVPNs - uninstall from
d62a17ae 5856 * kernel and free entries.
5857 */
8b5fdf2e
PR
5858 hash_iterate(zvrf->evpn_table, zebra_evpn_vxlan_cleanup_all,
5859 zvrf);
655b04d1 5860
ce5160c0
AK
5861 /* Delete all ESs in BGP */
5862 zebra_evpn_es_send_all_to_client(false /* add */);
5863
655b04d1 5864 /* cleanup all l3vnis */
89272910 5865 hash_iterate(zrouter.l3vni_table, zl3vni_cleanup_all, NULL);
150971b5 5866
0fb2ad05
T
5867 /* Mark as "no EVPN VRF" */
5868 zrouter.evpn_vrf = NULL;
d62a17ae 5869 }
13d60d35 5870
ec93aa12 5871stream_failure:
8068a649 5872 return;
13d60d35 5873}
5874
5875/*
87d76d54 5876 * Allocate EVPN hash table for this VRF and do other initialization.
13d60d35 5877 * NOTE: Currently supported only for default VRF.
5878 */
d62a17ae 5879void zebra_vxlan_init_tables(struct zebra_vrf *zvrf)
13d60d35 5880{
4a73887e
DS
5881 char buffer[80];
5882
d62a17ae 5883 if (!zvrf)
5884 return;
4a73887e
DS
5885
5886 snprintf(buffer, sizeof(buffer), "Zebra VRF EVPN Table: %s",
5887 zvrf->vrf->name);
5888 zvrf->evpn_table = hash_create_size(8, zebra_evpn_hash_keymake,
5889 zebra_evpn_hash_cmp, buffer);
5890
5891 snprintf(buffer, sizeof(buffer), "Zebra VxLAN SG Table: %s",
5892 zvrf->vrf->name);
5893 zvrf->vxlan_sg_table = hash_create_size(8, zebra_vxlan_sg_hash_key_make,
e2071325 5894 zebra_vxlan_sg_hash_eq, buffer);
13d60d35 5895}
5896
87d76d54 5897/* Cleanup EVPN info, but don't free the table. */
84915b0a 5898void zebra_vxlan_cleanup_tables(struct zebra_vrf *zvrf)
5899{
ce5160c0
AK
5900 struct zebra_vrf *evpn_zvrf = zebra_vrf_get_evpn();
5901
8b5fdf2e 5902 hash_iterate(zvrf->evpn_table, zebra_evpn_vxlan_cleanup_all, zvrf);
0c16fb72 5903 zebra_vxlan_cleanup_sg_table(zvrf);
ce5160c0
AK
5904
5905 if (zvrf == evpn_zvrf)
5906 zebra_evpn_es_cleanup();
84915b0a 5907}
5908
87d76d54 5909/* Close all EVPN handling */
d62a17ae 5910void zebra_vxlan_close_tables(struct zebra_vrf *zvrf)
13d60d35 5911{
2853fed6 5912 if (!zvrf)
5913 return;
8b5fdf2e 5914 hash_iterate(zvrf->evpn_table, zebra_evpn_vxlan_cleanup_all, zvrf);
87d76d54 5915 hash_free(zvrf->evpn_table);
0c16fb72
AK
5916 if (zvrf->vxlan_sg_table) {
5917 zebra_vxlan_cleanup_sg_table(zvrf);
5918 hash_free(zvrf->vxlan_sg_table);
5919 zvrf->vxlan_sg_table = NULL;
5920 }
13d60d35 5921}
b7cfce93
MK
5922
5923/* init the l3vni table */
6548050a 5924void zebra_vxlan_init(void)
b7cfce93 5925{
89272910
DS
5926 zrouter.l3vni_table = hash_create(l3vni_hash_keymake, l3vni_hash_cmp,
5927 "Zebra VRF L3 VNI table");
0fb2ad05 5928 zrouter.evpn_vrf = NULL;
ce5160c0 5929 zebra_evpn_mh_init();
b7cfce93
MK
5930}
5931
5932/* free l3vni table */
6548050a 5933void zebra_vxlan_disable(void)
b7cfce93 5934{
89272910 5935 hash_free(zrouter.l3vni_table);
ce5160c0 5936 zebra_evpn_mh_terminate();
b7cfce93 5937}
d3135ba3 5938
5939/* get the l3vni svi ifindex */
5940ifindex_t get_l3vni_svi_ifindex(vrf_id_t vrf_id)
5941{
05843a27 5942 struct zebra_l3vni *zl3vni = NULL;
d3135ba3 5943
5944 zl3vni = zl3vni_from_vrf(vrf_id);
5945 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
5946 return 0;
5947
5948 return zl3vni->svi_if->ifindex;
5949}
c80a972c 5950
015d264c 5951/************************** vxlan SG cache management ************************/
4ab3321f 5952/* Inform PIM about the mcast group */
ecbbc3a7
AK
5953static int zebra_vxlan_sg_send(struct zebra_vrf *zvrf,
5954 struct prefix_sg *sg,
5955 char *sg_str, uint16_t cmd)
4ab3321f
AK
5956{
5957 struct zserv *client = NULL;
5958 struct stream *s = NULL;
5959
5960 client = zserv_find_client(ZEBRA_ROUTE_PIM, 0);
5961 if (!client)
5962 return 0;
5963
ecbbc3a7
AK
5964 if (!CHECK_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG))
5965 return 0;
5966
4ab3321f
AK
5967 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
5968
5969 zclient_create_header(s, cmd, VRF_DEFAULT);
5970 stream_putl(s, IPV4_MAX_BYTELEN);
5971 stream_put(s, &sg->src.s_addr, IPV4_MAX_BYTELEN);
5972 stream_put(s, &sg->grp.s_addr, IPV4_MAX_BYTELEN);
5973
5974 /* Write packet size. */
5975 stream_putw_at(s, 0, stream_get_endp(s));
5976
5977 if (IS_ZEBRA_DEBUG_VXLAN)
5978 zlog_debug(
5979 "Send %s %s to %s",
5980 (cmd == ZEBRA_VXLAN_SG_ADD) ? "add" : "del", sg_str,
5981 zebra_route_string(client->proto));
5982
5983 if (cmd == ZEBRA_VXLAN_SG_ADD)
5984 client->vxlan_sg_add_cnt++;
5985 else
5986 client->vxlan_sg_del_cnt++;
5987
5988 return zserv_send_message(client, s);
5989}
5990
d8b87afe 5991static unsigned int zebra_vxlan_sg_hash_key_make(const void *p)
015d264c 5992{
847f168d 5993 const struct zebra_vxlan_sg *vxlan_sg = p;
015d264c
AK
5994
5995 return (jhash_2words(vxlan_sg->sg.src.s_addr,
5996 vxlan_sg->sg.grp.s_addr, 0));
5997}
5998
5999static bool zebra_vxlan_sg_hash_eq(const void *p1, const void *p2)
6000{
847f168d
DS
6001 const struct zebra_vxlan_sg *sg1 = p1;
6002 const struct zebra_vxlan_sg *sg2 = p2;
015d264c
AK
6003
6004 return ((sg1->sg.src.s_addr == sg2->sg.src.s_addr)
6005 && (sg1->sg.grp.s_addr == sg2->sg.grp.s_addr));
6006}
6007
847f168d
DS
6008static struct zebra_vxlan_sg *zebra_vxlan_sg_new(struct zebra_vrf *zvrf,
6009 struct prefix_sg *sg)
015d264c 6010{
847f168d 6011 struct zebra_vxlan_sg *vxlan_sg;
015d264c
AK
6012
6013 vxlan_sg = XCALLOC(MTYPE_ZVXLAN_SG, sizeof(*vxlan_sg));
6014
6015 vxlan_sg->zvrf = zvrf;
6016 vxlan_sg->sg = *sg;
6017 prefix_sg2str(sg, vxlan_sg->sg_str);
6018
6019 vxlan_sg = hash_get(zvrf->vxlan_sg_table, vxlan_sg, hash_alloc_intern);
6020
6021 if (IS_ZEBRA_DEBUG_VXLAN)
6022 zlog_debug("vxlan SG %s created", vxlan_sg->sg_str);
6023
6024 return vxlan_sg;
6025}
6026
847f168d
DS
6027static struct zebra_vxlan_sg *zebra_vxlan_sg_find(struct zebra_vrf *zvrf,
6028 struct prefix_sg *sg)
015d264c 6029{
847f168d 6030 struct zebra_vxlan_sg lookup;
015d264c
AK
6031
6032 lookup.sg = *sg;
6033 return hash_lookup(zvrf->vxlan_sg_table, &lookup);
6034}
6035
847f168d
DS
6036static struct zebra_vxlan_sg *zebra_vxlan_sg_add(struct zebra_vrf *zvrf,
6037 struct prefix_sg *sg)
015d264c 6038{
847f168d
DS
6039 struct zebra_vxlan_sg *vxlan_sg;
6040 struct zebra_vxlan_sg *parent = NULL;
015d264c
AK
6041 struct in_addr sip;
6042
6043 vxlan_sg = zebra_vxlan_sg_find(zvrf, sg);
6044 if (vxlan_sg)
6045 return vxlan_sg;
6046
6047 /* create a *G entry for every BUM group implicitly -
6048 * 1. The SG entry is used by pimd to setup the vxlan-origination-mroute
6049 * 2. the XG entry is used by pimd to setup the
6050 * vxlan-termination-mroute
6051 */
975a328e 6052 if (sg->src.s_addr != INADDR_ANY) {
015d264c
AK
6053 memset(&sip, 0, sizeof(sip));
6054 parent = zebra_vxlan_sg_do_ref(zvrf, sip, sg->grp);
6055 if (!parent)
6056 return NULL;
6057 }
6058
6059 vxlan_sg = zebra_vxlan_sg_new(zvrf, sg);
015d264c 6060
ecbbc3a7
AK
6061 zebra_vxlan_sg_send(zvrf, sg, vxlan_sg->sg_str,
6062 ZEBRA_VXLAN_SG_ADD);
4ab3321f 6063
015d264c
AK
6064 return vxlan_sg;
6065}
6066
847f168d 6067static void zebra_vxlan_sg_del(struct zebra_vxlan_sg *vxlan_sg)
015d264c
AK
6068{
6069 struct in_addr sip;
6070 struct zebra_vrf *zvrf;
6071
6072 zvrf = vrf_info_lookup(VRF_DEFAULT);
6073 if (!zvrf)
6074 return;
6075
6076 /* On SG entry deletion remove the reference to its parent XG
6077 * entry
6078 */
975a328e 6079 if (vxlan_sg->sg.src.s_addr != INADDR_ANY) {
015d264c
AK
6080 memset(&sip, 0, sizeof(sip));
6081 zebra_vxlan_sg_do_deref(zvrf, sip, vxlan_sg->sg.grp);
6082 }
6083
ecbbc3a7
AK
6084 zebra_vxlan_sg_send(zvrf, &vxlan_sg->sg,
6085 vxlan_sg->sg_str, ZEBRA_VXLAN_SG_DEL);
4ab3321f 6086
015d264c
AK
6087 hash_release(vxlan_sg->zvrf->vxlan_sg_table, vxlan_sg);
6088
6089 if (IS_ZEBRA_DEBUG_VXLAN)
6090 zlog_debug("VXLAN SG %s deleted", vxlan_sg->sg_str);
6091
6092 XFREE(MTYPE_ZVXLAN_SG, vxlan_sg);
6093}
6094
6095static void zebra_vxlan_sg_do_deref(struct zebra_vrf *zvrf,
6096 struct in_addr sip, struct in_addr mcast_grp)
6097{
847f168d 6098 struct zebra_vxlan_sg *vxlan_sg;
015d264c
AK
6099 struct prefix_sg sg;
6100
6101 sg.family = AF_INET;
6102 sg.prefixlen = IPV4_MAX_BYTELEN;
6103 sg.src = sip;
6104 sg.grp = mcast_grp;
6105 vxlan_sg = zebra_vxlan_sg_find(zvrf, &sg);
6106 if (!vxlan_sg)
6107 return;
6108
6109 if (vxlan_sg->ref_cnt)
6110 --vxlan_sg->ref_cnt;
6111
6112 if (!vxlan_sg->ref_cnt)
6113 zebra_vxlan_sg_del(vxlan_sg);
6114}
6115
847f168d
DS
6116static struct zebra_vxlan_sg *zebra_vxlan_sg_do_ref(struct zebra_vrf *zvrf,
6117 struct in_addr sip,
6118 struct in_addr mcast_grp)
015d264c 6119{
847f168d 6120 struct zebra_vxlan_sg *vxlan_sg;
015d264c
AK
6121 struct prefix_sg sg;
6122
6123 sg.family = AF_INET;
6124 sg.prefixlen = IPV4_MAX_BYTELEN;
6125 sg.src = sip;
6126 sg.grp = mcast_grp;
6127 vxlan_sg = zebra_vxlan_sg_add(zvrf, &sg);
6128 if (vxlan_sg)
6129 ++vxlan_sg->ref_cnt;
6130
6131 return vxlan_sg;
6132}
abfa0a96
AK
6133
6134static void zebra_vxlan_sg_deref(struct in_addr local_vtep_ip,
6135 struct in_addr mcast_grp)
6136{
6137 struct zebra_vrf *zvrf;
6138
975a328e
DA
6139 if (local_vtep_ip.s_addr == INADDR_ANY
6140 || mcast_grp.s_addr == INADDR_ANY)
abfa0a96
AK
6141 return;
6142
6143 zvrf = vrf_info_lookup(VRF_DEFAULT);
6144 if (!zvrf)
6145 return;
6146
6147 zebra_vxlan_sg_do_deref(zvrf, local_vtep_ip, mcast_grp);
6148}
6149
6150static void zebra_vxlan_sg_ref(struct in_addr local_vtep_ip,
6151 struct in_addr mcast_grp)
6152{
6153 struct zebra_vrf *zvrf;
6154
975a328e
DA
6155 if (local_vtep_ip.s_addr == INADDR_ANY
6156 || mcast_grp.s_addr == INADDR_ANY)
abfa0a96
AK
6157 return;
6158
6159 zvrf = vrf_info_lookup(VRF_DEFAULT);
6160 if (!zvrf)
6161 return;
6162 zebra_vxlan_sg_do_ref(zvrf, local_vtep_ip, mcast_grp);
6163}
6164
1ac88792 6165static void zebra_vxlan_xg_pre_cleanup(struct hash_bucket *bucket, void *arg)
0c16fb72 6166{
847f168d 6167 struct zebra_vxlan_sg *vxlan_sg = (struct zebra_vxlan_sg *)bucket->data;
0c16fb72
AK
6168
6169 /* increment the ref count against (*,G) to prevent them from being
6170 * deleted
6171 */
6172 if (vxlan_sg->sg.src.s_addr == INADDR_ANY)
6173 ++vxlan_sg->ref_cnt;
6174}
6175
1ac88792 6176static void zebra_vxlan_xg_post_cleanup(struct hash_bucket *bucket, void *arg)
0c16fb72 6177{
847f168d 6178 struct zebra_vxlan_sg *vxlan_sg = (struct zebra_vxlan_sg *)bucket->data;
0c16fb72
AK
6179
6180 /* decrement the dummy ref count against (*,G) to delete them */
6181 if (vxlan_sg->sg.src.s_addr == INADDR_ANY) {
6182 if (vxlan_sg->ref_cnt)
6183 --vxlan_sg->ref_cnt;
6184 if (!vxlan_sg->ref_cnt)
6185 zebra_vxlan_sg_del(vxlan_sg);
6186 }
6187}
6188
1ac88792 6189static void zebra_vxlan_sg_cleanup(struct hash_bucket *bucket, void *arg)
abfa0a96 6190{
847f168d 6191 struct zebra_vxlan_sg *vxlan_sg = (struct zebra_vxlan_sg *)bucket->data;
abfa0a96
AK
6192
6193 zebra_vxlan_sg_del(vxlan_sg);
6194}
27627f9a 6195
0c16fb72
AK
6196static void zebra_vxlan_cleanup_sg_table(struct zebra_vrf *zvrf)
6197{
6198 /* increment the ref count against (*,G) to prevent them from being
6199 * deleted
6200 */
6201 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_xg_pre_cleanup, NULL);
6202
6203 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_sg_cleanup, NULL);
6204
6205 /* decrement the dummy ref count against the XG entries */
6206 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_xg_post_cleanup, NULL);
6207}
6208
1ac88792 6209static void zebra_vxlan_sg_replay_send(struct hash_bucket *bucket, void *arg)
ecbbc3a7 6210{
847f168d 6211 struct zebra_vxlan_sg *vxlan_sg = (struct zebra_vxlan_sg *)bucket->data;
ecbbc3a7
AK
6212
6213 zebra_vxlan_sg_send(vxlan_sg->zvrf, &vxlan_sg->sg,
6214 vxlan_sg->sg_str, ZEBRA_VXLAN_SG_ADD);
6215}
6216
6217/* Handle message from client to replay vxlan SG entries */
6218void zebra_vxlan_sg_replay(ZAPI_HANDLER_ARGS)
6219{
6220 if (IS_ZEBRA_DEBUG_VXLAN)
6221 zlog_debug("VxLAN SG updates to PIM, start");
6222
6223 SET_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG);
6224
6225 if (!EVPN_ENABLED(zvrf)) {
8f86bb06
DS
6226 if (IS_ZEBRA_DEBUG_VXLAN)
6227 zlog_debug("VxLAN SG replay request on unexpected vrf %d",
6228 zvrf->vrf->vrf_id);
ecbbc3a7
AK
6229 return;
6230 }
6231
6232 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_sg_replay_send, NULL);
6233}
6234
27627f9a
KA
6235
6236/* Cleanup EVPN configuration of a specific VRF */
6237static void zebra_evpn_vrf_cfg_cleanup(struct zebra_vrf *zvrf)
6238{
05843a27 6239 struct zebra_l3vni *zl3vni = NULL;
b6587fc2 6240
27627f9a
KA
6241 zvrf->advertise_all_vni = 0;
6242 zvrf->advertise_gw_macip = 0;
6243 zvrf->advertise_svi_macip = 0;
6244 zvrf->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
6245
8b5fdf2e 6246 hash_iterate(zvrf->evpn_table, zebra_evpn_cfg_cleanup, NULL);
b6587fc2
CS
6247
6248 if (zvrf->l3vni)
6249 zl3vni = zl3vni_lookup(zvrf->l3vni);
6250 if (zl3vni) {
6251 /* delete and uninstall all rmacs */
6252 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
6253 zl3vni);
6254 /* delete and uninstall all next-hops */
6255 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
6256 zl3vni);
6257 }
27627f9a
KA
6258}
6259
6260/* Cleanup BGP EVPN configuration upon client disconnect */
ecbbc3a7 6261static int zebra_evpn_bgp_cfg_clean_up(struct zserv *client)
27627f9a
KA
6262{
6263 struct vrf *vrf;
6264 struct zebra_vrf *zvrf;
6265
27627f9a
KA
6266 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
6267 zvrf = vrf->info;
6268 if (zvrf)
6269 zebra_evpn_vrf_cfg_cleanup(zvrf);
6270 }
6271
6272 return 0;
6273}
6274
ecbbc3a7
AK
6275static int zebra_evpn_pim_cfg_clean_up(struct zserv *client)
6276{
6277 struct zebra_vrf *zvrf = zebra_vrf_get_evpn();
6278
81157cbd 6279 if (CHECK_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG)) {
ecbbc3a7
AK
6280 if (IS_ZEBRA_DEBUG_VXLAN)
6281 zlog_debug("VxLAN SG updates to PIM, stop");
6282 UNSET_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG);
6283 }
6284
6285 return 0;
6286}
6287
6288static int zebra_evpn_cfg_clean_up(struct zserv *client)
6289{
6290 if (client->proto == ZEBRA_ROUTE_BGP)
6291 return zebra_evpn_bgp_cfg_clean_up(client);
6292
6293 if (client->proto == ZEBRA_ROUTE_PIM)
6294 return zebra_evpn_pim_cfg_clean_up(client);
6295
6296 return 0;
6297}
6298
036d93c0
MS
6299/*
6300 * Handle results for vxlan dataplane operations.
6301 */
6302extern void zebra_vxlan_handle_result(struct zebra_dplane_ctx *ctx)
6303{
f00b37e7 6304 return;
036d93c0
MS
6305}
6306
7d99ad7f
SW
6307/* Config knob for accepting lower sequence numbers */
6308void zebra_vxlan_set_accept_bgp_seq(bool set)
6309{
6310 accept_bgp_seq = set;
6311}
6312
da823882 6313bool zebra_vxlan_get_accept_bgp_seq(void)
7d99ad7f
SW
6314{
6315 return accept_bgp_seq;
6316}
6317
27627f9a
KA
6318/* Cleanup BGP EVPN configuration upon client disconnect */
6319extern void zebra_evpn_init(void)
6320{
6321 hook_register(zserv_client_close, zebra_evpn_cfg_clean_up);
6322}