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