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