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