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