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