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