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