]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vxlan.c
Merge pull request #7352 from mjstapp/fix_rt_netlink_indent
[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
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 }
3451
3452 if (uj) {
3453 vty_out(vty, "%s\n", json_object_to_json_string_ext(
3454 json, JSON_C_TO_STRING_PRETTY));
3455 json_object_free(json);
3456 }
3457 }
3458
3459 /*
3460 * Display VNI hash table (VTY command handler).
3461 */
3462 void zebra_vxlan_print_vnis(struct vty *vty, struct zebra_vrf *zvrf,
3463 bool use_json)
3464 {
3465 json_object *json = NULL;
3466 void *args[2];
3467
3468 if (!is_evpn_enabled())
3469 return;
3470
3471 if (use_json)
3472 json = json_object_new_object();
3473 else
3474 vty_out(vty, "%-10s %-4s %-21s %-8s %-8s %-15s %-37s\n", "VNI",
3475 "Type", "VxLAN IF", "# MACs", "# ARPs",
3476 "# Remote VTEPs", "Tenant VRF");
3477
3478 args[0] = vty;
3479 args[1] = json;
3480
3481 /* Display all L2-VNIs */
3482 hash_iterate(
3483 zvrf->evpn_table,
3484 (void (*)(struct hash_bucket *, void *))zebra_evpn_print_hash,
3485 args);
3486
3487 /* Display all L3-VNIs */
3488 hash_iterate(zrouter.l3vni_table,
3489 (void (*)(struct hash_bucket *, void *))zl3vni_print_hash,
3490 args);
3491
3492 if (use_json) {
3493 vty_out(vty, "%s\n", json_object_to_json_string_ext(
3494 json, JSON_C_TO_STRING_PRETTY));
3495 json_object_free(json);
3496 }
3497 }
3498
3499 void zebra_vxlan_dup_addr_detection(ZAPI_HANDLER_ARGS)
3500 {
3501 struct stream *s;
3502 int time = 0;
3503 uint32_t max_moves = 0;
3504 uint32_t freeze_time = 0;
3505 bool dup_addr_detect = false;
3506 bool freeze = false;
3507
3508 s = msg;
3509 STREAM_GETL(s, dup_addr_detect);
3510 STREAM_GETL(s, time);
3511 STREAM_GETL(s, max_moves);
3512 STREAM_GETL(s, freeze);
3513 STREAM_GETL(s, freeze_time);
3514
3515 /* DAD previous state was enabled, and new state is disable,
3516 * clear all duplicate detected addresses.
3517 */
3518 if (zvrf->dup_addr_detect && !dup_addr_detect)
3519 zebra_vxlan_clear_dup_detect_vni_all(zvrf);
3520
3521 zvrf->dup_addr_detect = dup_addr_detect;
3522 zvrf->dad_time = time;
3523 zvrf->dad_max_moves = max_moves;
3524 zvrf->dad_freeze = freeze;
3525 zvrf->dad_freeze_time = freeze_time;
3526
3527 if (IS_ZEBRA_DEBUG_VXLAN)
3528 zlog_debug(
3529 "VRF %s duplicate detect %s max_moves %u timeout %u freeze %s freeze_time %u",
3530 vrf_id_to_name(zvrf->vrf->vrf_id),
3531 zvrf->dup_addr_detect ? "enable" : "disable",
3532 zvrf->dad_max_moves,
3533 zvrf->dad_time,
3534 zvrf->dad_freeze ? "enable" : "disable",
3535 zvrf->dad_freeze_time);
3536
3537 stream_failure:
3538 return;
3539 }
3540
3541 /*
3542 * Display VNI hash table in detail(VTY command handler).
3543 */
3544 void zebra_vxlan_print_vnis_detail(struct vty *vty, struct zebra_vrf *zvrf,
3545 bool use_json)
3546 {
3547 json_object *json_array = NULL;
3548 struct zebra_ns *zns = NULL;
3549 struct zebra_evpn_show zes;
3550
3551 if (!is_evpn_enabled())
3552 return;
3553
3554 zns = zebra_ns_lookup(NS_DEFAULT);
3555 if (!zns)
3556 return;
3557
3558 if (use_json)
3559 json_array = json_object_new_array();
3560
3561 zes.vty = vty;
3562 zes.json = json_array;
3563 zes.zvrf = zvrf;
3564 zes.use_json = use_json;
3565
3566 /* Display all L2-VNIs */
3567 hash_iterate(zvrf->evpn_table,
3568 (void (*)(struct hash_bucket *,
3569 void *))zebra_evpn_print_hash_detail,
3570 &zes);
3571
3572 /* Display all L3-VNIs */
3573 hash_iterate(zrouter.l3vni_table,
3574 (void (*)(struct hash_bucket *,
3575 void *))zl3vni_print_hash_detail,
3576 &zes);
3577
3578 if (use_json) {
3579 vty_out(vty, "%s\n",
3580 json_object_to_json_string_ext(
3581 json_array, JSON_C_TO_STRING_PRETTY));
3582 json_object_free(json_array);
3583 }
3584 }
3585
3586 /*
3587 * Handle neighbor delete notification from the kernel (on a VLAN device
3588 * / L3 interface). This may result in either the neighbor getting deleted
3589 * from our database or being re-added to the kernel (if it is a valid
3590 * remote neighbor).
3591 */
3592 int zebra_vxlan_handle_kernel_neigh_del(struct interface *ifp,
3593 struct interface *link_if,
3594 struct ipaddr *ip)
3595 {
3596 char buf[INET6_ADDRSTRLEN];
3597 zebra_evpn_t *zevpn = NULL;
3598 zebra_l3vni_t *zl3vni = NULL;
3599
3600 /* check if this is a remote neigh entry corresponding to remote
3601 * next-hop
3602 */
3603 zl3vni = zl3vni_from_svi(ifp, link_if);
3604 if (zl3vni)
3605 return zl3vni_local_nh_del(zl3vni, ip);
3606
3607 /* We are only interested in neighbors on an SVI that resides on top
3608 * of a VxLAN bridge.
3609 */
3610 zevpn = zebra_evpn_from_svi(ifp, link_if);
3611 if (!zevpn) {
3612 if (IS_ZEBRA_DEBUG_VXLAN)
3613 zlog_debug(
3614 "%s: Del neighbor %s EVPN is not present for interface %s",
3615 __func__, ipaddr2str(ip, buf, sizeof(buf)),
3616 ifp->name);
3617 return 0;
3618 }
3619
3620 if (!zevpn->vxlan_if) {
3621 zlog_debug(
3622 "VNI %u hash %p doesn't have intf upon local neighbor DEL",
3623 zevpn->vni, zevpn);
3624 return -1;
3625 }
3626
3627 if (IS_ZEBRA_DEBUG_VXLAN)
3628 zlog_debug("Del neighbor %s intf %s(%u) -> L2-VNI %u",
3629 ipaddr2str(ip, buf, sizeof(buf)), ifp->name,
3630 ifp->ifindex, zevpn->vni);
3631
3632 return zebra_evpn_neigh_del_ip(zevpn, ip);
3633 }
3634
3635 /*
3636 * Handle neighbor add or update notification from the kernel (on a VLAN
3637 * device / L3 interface). This is typically for a local neighbor but can
3638 * also be for a remote neighbor (e.g., ageout notification). It could
3639 * also be a "move" scenario.
3640 */
3641 int zebra_vxlan_handle_kernel_neigh_update(struct interface *ifp,
3642 struct interface *link_if,
3643 struct ipaddr *ip,
3644 struct ethaddr *macaddr,
3645 uint16_t state,
3646 bool is_ext,
3647 bool is_router,
3648 bool local_inactive, bool dp_static)
3649 {
3650 char buf[ETHER_ADDR_STRLEN];
3651 char buf2[INET6_ADDRSTRLEN];
3652 zebra_evpn_t *zevpn = NULL;
3653 zebra_l3vni_t *zl3vni = NULL;
3654
3655 /* check if this is a remote neigh entry corresponding to remote
3656 * next-hop
3657 */
3658 zl3vni = zl3vni_from_svi(ifp, link_if);
3659 if (zl3vni)
3660 return zl3vni_local_nh_add_update(zl3vni, ip, state);
3661
3662 /* We are only interested in neighbors on an SVI that resides on top
3663 * of a VxLAN bridge.
3664 */
3665 zevpn = zebra_evpn_from_svi(ifp, link_if);
3666 if (!zevpn)
3667 return 0;
3668
3669 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_NEIGH)
3670 zlog_debug(
3671 "Add/Update neighbor %s MAC %s intf %s(%u) state 0x%x %s%s%s-> L2-VNI %u",
3672 ipaddr2str(ip, buf2, sizeof(buf2)),
3673 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
3674 ifp->ifindex, state, is_ext ? "ext-learned " : "",
3675 is_router ? "router " : "",
3676 local_inactive ? "local_inactive " : "",
3677 zevpn->vni);
3678
3679 /* Is this about a local neighbor or a remote one? */
3680 if (!is_ext)
3681 return zebra_evpn_local_neigh_update(zevpn, ifp, ip, macaddr,
3682 is_router, local_inactive,
3683 dp_static);
3684
3685 return zebra_evpn_remote_neigh_update(zevpn, ifp, ip, macaddr, state);
3686 }
3687
3688 static int32_t
3689 zebra_vxlan_remote_macip_helper(bool add, struct stream *s, vni_t *vni,
3690 struct ethaddr *macaddr, uint16_t *ipa_len,
3691 struct ipaddr *ip, struct in_addr *vtep_ip,
3692 uint8_t *flags, uint32_t *seq, esi_t *esi)
3693 {
3694 uint16_t l = 0;
3695
3696 /*
3697 * Obtain each remote MACIP and process.
3698 * Message contains VNI, followed by MAC followed by IP (if any)
3699 * followed by remote VTEP IP.
3700 */
3701 memset(ip, 0, sizeof(*ip));
3702 STREAM_GETL(s, *vni);
3703 STREAM_GET(macaddr->octet, s, ETH_ALEN);
3704 STREAM_GETL(s, *ipa_len);
3705
3706 if (*ipa_len) {
3707 if (*ipa_len == IPV4_MAX_BYTELEN)
3708 ip->ipa_type = IPADDR_V4;
3709 else if (*ipa_len == IPV6_MAX_BYTELEN)
3710 ip->ipa_type = IPADDR_V6;
3711 else {
3712 if (IS_ZEBRA_DEBUG_VXLAN)
3713 zlog_debug(
3714 "ipa_len *must* be %d or %d bytes in length not %d",
3715 IPV4_MAX_BYTELEN, IPV6_MAX_BYTELEN,
3716 *ipa_len);
3717 goto stream_failure;
3718 }
3719
3720 STREAM_GET(&ip->ip.addr, s, *ipa_len);
3721 }
3722 l += 4 + ETH_ALEN + 4 + *ipa_len;
3723 STREAM_GET(&vtep_ip->s_addr, s, IPV4_MAX_BYTELEN);
3724 l += IPV4_MAX_BYTELEN;
3725
3726 if (add) {
3727 STREAM_GETC(s, *flags);
3728 STREAM_GETL(s, *seq);
3729 l += 5;
3730 STREAM_GET(esi, s, sizeof(esi_t));
3731 l += sizeof(esi_t);
3732 }
3733
3734 return l;
3735
3736 stream_failure:
3737 return -1;
3738 }
3739
3740 /*
3741 * Handle message from client to delete a remote MACIP for a VNI.
3742 */
3743 void zebra_vxlan_remote_macip_del(ZAPI_HANDLER_ARGS)
3744 {
3745 struct stream *s;
3746 vni_t vni;
3747 struct ethaddr macaddr;
3748 struct ipaddr ip;
3749 struct in_addr vtep_ip;
3750 uint16_t l = 0, ipa_len;
3751 char buf[ETHER_ADDR_STRLEN];
3752 char buf1[INET6_ADDRSTRLEN];
3753
3754 memset(&macaddr, 0, sizeof(struct ethaddr));
3755 memset(&ip, 0, sizeof(struct ipaddr));
3756 memset(&vtep_ip, 0, sizeof(struct in_addr));
3757
3758 s = msg;
3759
3760 while (l < hdr->length) {
3761 int res_length = zebra_vxlan_remote_macip_helper(
3762 false, s, &vni, &macaddr, &ipa_len, &ip, &vtep_ip, NULL,
3763 NULL, NULL);
3764
3765 if (res_length == -1)
3766 goto stream_failure;
3767
3768 l += res_length;
3769 if (IS_ZEBRA_DEBUG_VXLAN)
3770 zlog_debug(
3771 "Recv MACIP DEL VNI %u MAC %s%s%s Remote VTEP %pI4 from %s",
3772 vni,
3773 prefix_mac2str(&macaddr, buf, sizeof(buf)),
3774 ipa_len ? " IP " : "",
3775 ipa_len ?
3776 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
3777 &vtep_ip, zebra_route_string(client->proto));
3778
3779 process_remote_macip_del(vni, &macaddr, ipa_len, &ip, vtep_ip);
3780 }
3781
3782 stream_failure:
3783 return;
3784 }
3785
3786 /*
3787 * Handle message from client to add a remote MACIP for a VNI. This
3788 * could be just the add of a MAC address or the add of a neighbor
3789 * (IP+MAC).
3790 */
3791 void zebra_vxlan_remote_macip_add(ZAPI_HANDLER_ARGS)
3792 {
3793 struct stream *s;
3794 vni_t vni;
3795 struct ethaddr macaddr;
3796 struct ipaddr ip;
3797 struct in_addr vtep_ip;
3798 uint16_t l = 0, ipa_len;
3799 uint8_t flags = 0;
3800 uint32_t seq;
3801 char buf[ETHER_ADDR_STRLEN];
3802 char buf1[INET6_ADDRSTRLEN];
3803 esi_t esi;
3804 char esi_buf[ESI_STR_LEN];
3805
3806 memset(&macaddr, 0, sizeof(struct ethaddr));
3807 memset(&ip, 0, sizeof(struct ipaddr));
3808 memset(&vtep_ip, 0, sizeof(struct in_addr));
3809
3810 if (!EVPN_ENABLED(zvrf)) {
3811 zlog_debug("EVPN not enabled, ignoring remote MACIP ADD");
3812 return;
3813 }
3814
3815 s = msg;
3816
3817 while (l < hdr->length) {
3818 int res_length = zebra_vxlan_remote_macip_helper(
3819 true, s, &vni, &macaddr, &ipa_len, &ip, &vtep_ip,
3820 &flags, &seq, &esi);
3821
3822 if (res_length == -1)
3823 goto stream_failure;
3824
3825 l += res_length;
3826 if (IS_ZEBRA_DEBUG_VXLAN) {
3827 if (memcmp(&esi, zero_esi, sizeof(esi_t)))
3828 esi_to_str(&esi, esi_buf, sizeof(esi_buf));
3829 else
3830 strlcpy(esi_buf, "-", ESI_STR_LEN);
3831 zlog_debug(
3832 "Recv %sMACIP ADD VNI %u MAC %s%s%s flags 0x%x seq %u VTEP %pI4 ESI %s from %s",
3833 (flags & ZEBRA_MACIP_TYPE_SYNC_PATH) ?
3834 "sync-" : "",
3835 vni,
3836 prefix_mac2str(&macaddr, buf, sizeof(buf)),
3837 ipa_len ? " IP " : "",
3838 ipa_len ?
3839 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
3840 flags, seq, &vtep_ip, esi_buf,
3841 zebra_route_string(client->proto));
3842 }
3843
3844 process_remote_macip_add(vni, &macaddr, ipa_len, &ip,
3845 flags, seq, vtep_ip, &esi);
3846 }
3847
3848 stream_failure:
3849 return;
3850 }
3851
3852 /*
3853 * Handle remote vtep delete by kernel; re-add the vtep if we have it
3854 */
3855 int zebra_vxlan_check_readd_vtep(struct interface *ifp,
3856 struct in_addr vtep_ip)
3857 {
3858 struct zebra_if *zif;
3859 struct zebra_vrf *zvrf = NULL;
3860 struct zebra_l2info_vxlan *vxl;
3861 vni_t vni;
3862 zebra_evpn_t *zevpn = NULL;
3863 zebra_vtep_t *zvtep = NULL;
3864
3865 zif = ifp->info;
3866 assert(zif);
3867 vxl = &zif->l2info.vxl;
3868 vni = vxl->vni;
3869
3870 /* If EVPN is not enabled, nothing to do. */
3871 if (!is_evpn_enabled())
3872 return 0;
3873
3874 /* Locate VRF corresponding to interface. */
3875 zvrf = vrf_info_lookup(ifp->vrf_id);
3876 if (!zvrf)
3877 return -1;
3878
3879 /* Locate hash entry; it is expected to exist. */
3880 zevpn = zebra_evpn_lookup(vni);
3881 if (!zevpn)
3882 return 0;
3883
3884 /* If the remote vtep entry doesn't exists nothing to do */
3885 zvtep = zebra_evpn_vtep_find(zevpn, &vtep_ip);
3886 if (!zvtep)
3887 return 0;
3888
3889 if (IS_ZEBRA_DEBUG_VXLAN)
3890 zlog_debug(
3891 "Del MAC for remote VTEP %pI4 intf %s(%u) VNI %u - readd",
3892 &vtep_ip, ifp->name, ifp->ifindex, vni);
3893
3894 zebra_evpn_vtep_install(zevpn, zvtep);
3895 return 0;
3896 }
3897
3898 /*
3899 * Handle notification of MAC add/update over VxLAN. If the kernel is notifying
3900 * us, this must involve a multihoming scenario. Treat this as implicit delete
3901 * of any prior local MAC.
3902 */
3903 int zebra_vxlan_check_del_local_mac(struct interface *ifp,
3904 struct interface *br_if,
3905 struct ethaddr *macaddr, vlanid_t vid)
3906 {
3907 struct zebra_if *zif;
3908 struct zebra_l2info_vxlan *vxl;
3909 vni_t vni;
3910 zebra_evpn_t *zevpn;
3911 zebra_mac_t *mac;
3912 char buf[ETHER_ADDR_STRLEN];
3913
3914 zif = ifp->info;
3915 assert(zif);
3916 vxl = &zif->l2info.vxl;
3917 vni = vxl->vni;
3918
3919 /* Check if EVPN is enabled. */
3920 if (!is_evpn_enabled())
3921 return 0;
3922
3923 /* Locate hash entry; it is expected to exist. */
3924 zevpn = zebra_evpn_lookup(vni);
3925 if (!zevpn)
3926 return 0;
3927
3928 /* If entry doesn't exist, nothing to do. */
3929 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
3930 if (!mac)
3931 return 0;
3932
3933 /* Is it a local entry? */
3934 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
3935 return 0;
3936
3937 if (IS_ZEBRA_DEBUG_VXLAN)
3938 zlog_debug(
3939 "Add/update remote MAC %s intf %s(%u) VNI %u flags 0x%x - del local",
3940 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
3941 ifp->ifindex, vni, mac->flags);
3942
3943 /* Remove MAC from BGP. */
3944 zebra_evpn_mac_send_del_to_client(zevpn->vni, macaddr, mac->flags,
3945 false /* force */);
3946
3947 /*
3948 * If there are no neigh associated with the mac delete the mac
3949 * else mark it as AUTO for forward reference
3950 */
3951 if (!listcount(mac->neigh_list)) {
3952 zebra_evpn_mac_del(zevpn, mac);
3953 } else {
3954 UNSET_FLAG(mac->flags, ZEBRA_MAC_ALL_LOCAL_FLAGS);
3955 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
3956 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
3957 }
3958
3959 return 0;
3960 }
3961
3962 /*
3963 * Handle remote MAC delete by kernel; readd the remote MAC if we have it.
3964 * This can happen because the remote MAC entries are also added as "dynamic",
3965 * so the kernel can ageout the entry.
3966 */
3967 int zebra_vxlan_check_readd_remote_mac(struct interface *ifp,
3968 struct interface *br_if,
3969 struct ethaddr *macaddr, vlanid_t vid)
3970 {
3971 struct zebra_if *zif = NULL;
3972 struct zebra_l2info_vxlan *vxl = NULL;
3973 vni_t vni;
3974 zebra_evpn_t *zevpn = NULL;
3975 zebra_l3vni_t *zl3vni = NULL;
3976 zebra_mac_t *mac = NULL;
3977 char buf[ETHER_ADDR_STRLEN];
3978
3979 zif = ifp->info;
3980 assert(zif);
3981 vxl = &zif->l2info.vxl;
3982 vni = vxl->vni;
3983
3984 /* Check if EVPN is enabled. */
3985 if (!is_evpn_enabled())
3986 return 0;
3987
3988 /* check if this is a remote RMAC and readd simillar to remote macs */
3989 zl3vni = zl3vni_lookup(vni);
3990 if (zl3vni)
3991 return zebra_vxlan_readd_remote_rmac(zl3vni, macaddr);
3992
3993 /* Locate hash entry; it is expected to exist. */
3994 zevpn = zebra_evpn_lookup(vni);
3995 if (!zevpn)
3996 return 0;
3997
3998 /* If entry doesn't exist, nothing to do. */
3999 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
4000 if (!mac)
4001 return 0;
4002
4003 /* Is it a remote entry? */
4004 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
4005 return 0;
4006
4007 if (IS_ZEBRA_DEBUG_VXLAN)
4008 zlog_debug("Del remote MAC %s intf %s(%u) VNI %u - readd",
4009 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
4010 ifp->ifindex, vni);
4011
4012 zebra_evpn_rem_mac_install(zevpn, mac, false /* was_static */);
4013 return 0;
4014 }
4015
4016 /*
4017 * Handle local MAC delete (on a port or VLAN corresponding to this VNI).
4018 */
4019 int zebra_vxlan_local_mac_del(struct interface *ifp, struct interface *br_if,
4020 struct ethaddr *macaddr, vlanid_t vid)
4021 {
4022 zebra_evpn_t *zevpn;
4023
4024 /* We are interested in MACs only on ports or (port, VLAN) that
4025 * map to a VNI.
4026 */
4027 zevpn = zebra_evpn_map_vlan(ifp, br_if, vid);
4028 if (!zevpn)
4029 return 0;
4030 if (!zevpn->vxlan_if) {
4031 zlog_debug(
4032 "VNI %u hash %p doesn't have intf upon local MAC DEL",
4033 zevpn->vni, zevpn);
4034 return -1;
4035 }
4036
4037 return zebra_evpn_del_local_mac(zevpn, macaddr, ifp);
4038 }
4039
4040 /*
4041 * Handle local MAC add (on a port or VLAN corresponding to this VNI).
4042 */
4043 int zebra_vxlan_local_mac_add_update(struct interface *ifp,
4044 struct interface *br_if,
4045 struct ethaddr *macaddr, vlanid_t vid,
4046 bool sticky, bool local_inactive,
4047 bool dp_static)
4048 {
4049 zebra_evpn_t *zevpn;
4050 struct zebra_vrf *zvrf;
4051 char buf[ETHER_ADDR_STRLEN];
4052
4053 assert(ifp);
4054
4055 /* We are interested in MACs only on ports or (port, VLAN) that
4056 * map to an EVPN.
4057 */
4058 zevpn = zebra_evpn_map_vlan(ifp, br_if, vid);
4059 if (!zevpn) {
4060 if (IS_ZEBRA_DEBUG_VXLAN)
4061 zlog_debug(
4062 " Add/Update %sMAC %s intf %s(%u) VID %u, could not find EVPN",
4063 sticky ? "sticky " : "",
4064 prefix_mac2str(macaddr, buf, sizeof(buf)),
4065 ifp->name, ifp->ifindex, vid);
4066 return 0;
4067 }
4068
4069 if (!zevpn->vxlan_if) {
4070 if (IS_ZEBRA_DEBUG_VXLAN)
4071 zlog_debug(
4072 " VNI %u hash %p doesn't have intf upon local MAC ADD",
4073 zevpn->vni, zevpn);
4074 return -1;
4075 }
4076
4077 zvrf = zebra_vrf_get_evpn();
4078 if (!zvrf) {
4079 if (IS_ZEBRA_DEBUG_VXLAN)
4080 zlog_debug(" No Evpn Global Vrf found");
4081 return -1;
4082 }
4083
4084 return zebra_evpn_add_update_local_mac(zvrf, zevpn, ifp, macaddr, vid,
4085 sticky, local_inactive,
4086 dp_static);
4087 }
4088
4089 /*
4090 * Handle message from client to delete a remote VTEP for an EVPN.
4091 */
4092 void zebra_vxlan_remote_vtep_del(ZAPI_HANDLER_ARGS)
4093 {
4094 struct stream *s;
4095 unsigned short l = 0;
4096 vni_t vni;
4097 struct in_addr vtep_ip;
4098 zebra_evpn_t *zevpn;
4099 zebra_vtep_t *zvtep;
4100 struct interface *ifp;
4101 struct zebra_if *zif;
4102
4103 if (!is_evpn_enabled()) {
4104 zlog_debug(
4105 "%s: EVPN is not enabled yet we have received a vtep del command",
4106 __func__);
4107 return;
4108 }
4109
4110 if (!EVPN_ENABLED(zvrf)) {
4111 zlog_debug("Recv MACIP DEL for non-EVPN VRF %u",
4112 zvrf_id(zvrf));
4113 return;
4114 }
4115
4116 s = msg;
4117
4118 while (l < hdr->length) {
4119 int flood_control __attribute__((unused));
4120
4121 /* Obtain each remote VTEP and process. */
4122 STREAM_GETL(s, vni);
4123 l += 4;
4124 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
4125 l += IPV4_MAX_BYTELEN;
4126
4127 /* Flood control is intentionally ignored right now */
4128 STREAM_GETL(s, flood_control);
4129 l += 4;
4130
4131 if (IS_ZEBRA_DEBUG_VXLAN)
4132 zlog_debug("Recv VTEP_DEL %pI4 VNI %u from %s",
4133 &vtep_ip, vni,
4134 zebra_route_string(client->proto));
4135
4136 /* Locate VNI hash entry - expected to exist. */
4137 zevpn = zebra_evpn_lookup(vni);
4138 if (!zevpn) {
4139 if (IS_ZEBRA_DEBUG_VXLAN)
4140 zlog_debug(
4141 "Failed to locate VNI hash upon remote VTEP DEL, VNI %u",
4142 vni);
4143 continue;
4144 }
4145
4146 ifp = zevpn->vxlan_if;
4147 if (!ifp) {
4148 zlog_debug(
4149 "VNI %u hash %p doesn't have intf upon remote VTEP DEL",
4150 zevpn->vni, zevpn);
4151 continue;
4152 }
4153 zif = ifp->info;
4154
4155 /* If down or not mapped to a bridge, we're done. */
4156 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
4157 continue;
4158
4159 /* If the remote VTEP does not exist, there's nothing more to
4160 * do.
4161 * Otherwise, uninstall any remote MACs pointing to this VTEP
4162 * and
4163 * then, the VTEP entry itself and remove it.
4164 */
4165 zvtep = zebra_evpn_vtep_find(zevpn, &vtep_ip);
4166 if (!zvtep)
4167 continue;
4168
4169 zebra_evpn_vtep_uninstall(zevpn, &vtep_ip);
4170 zebra_evpn_vtep_del(zevpn, zvtep);
4171 }
4172
4173 stream_failure:
4174 return;
4175 }
4176
4177 /*
4178 * Handle message from client to add a remote VTEP for an EVPN.
4179 */
4180 void zebra_vxlan_remote_vtep_add(ZAPI_HANDLER_ARGS)
4181 {
4182 struct stream *s;
4183 unsigned short l = 0;
4184 vni_t vni;
4185 struct in_addr vtep_ip;
4186 zebra_evpn_t *zevpn;
4187 struct interface *ifp;
4188 struct zebra_if *zif;
4189 int flood_control;
4190 zebra_vtep_t *zvtep;
4191
4192 if (!is_evpn_enabled()) {
4193 zlog_debug(
4194 "%s: EVPN not enabled yet we received a vtep_add zapi call",
4195 __func__);
4196 return;
4197 }
4198
4199 if (!EVPN_ENABLED(zvrf)) {
4200 zlog_debug("Recv MACIP ADD for non-EVPN VRF %u",
4201 zvrf_id(zvrf));
4202 return;
4203 }
4204
4205 s = msg;
4206
4207 while (l < hdr->length) {
4208 /* Obtain each remote VTEP and process. */
4209 STREAM_GETL(s, vni);
4210 l += 4;
4211 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
4212 STREAM_GETL(s, flood_control);
4213 l += IPV4_MAX_BYTELEN + 4;
4214
4215 if (IS_ZEBRA_DEBUG_VXLAN)
4216 zlog_debug("Recv VTEP_ADD %pI4 VNI %u flood %d from %s",
4217 &vtep_ip, vni, flood_control,
4218 zebra_route_string(client->proto));
4219
4220 /* Locate VNI hash entry - expected to exist. */
4221 zevpn = zebra_evpn_lookup(vni);
4222 if (!zevpn) {
4223 flog_err(
4224 EC_ZEBRA_VTEP_ADD_FAILED,
4225 "Failed to locate EVPN hash upon remote VTEP ADD, VNI %u",
4226 vni);
4227 continue;
4228 }
4229
4230 ifp = zevpn->vxlan_if;
4231 if (!ifp) {
4232 flog_err(
4233 EC_ZEBRA_VTEP_ADD_FAILED,
4234 "VNI %u hash %p doesn't have intf upon remote VTEP ADD",
4235 zevpn->vni, zevpn);
4236 continue;
4237 }
4238
4239 zif = ifp->info;
4240
4241 /* If down or not mapped to a bridge, we're done. */
4242 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
4243 continue;
4244
4245 zvtep = zebra_evpn_vtep_find(zevpn, &vtep_ip);
4246 if (zvtep) {
4247 /* If the remote VTEP already exists check if
4248 * the flood mode has changed
4249 */
4250 if (zvtep->flood_control != flood_control) {
4251 if (zvtep->flood_control
4252 == VXLAN_FLOOD_DISABLED)
4253 /* old mode was head-end-replication but
4254 * is no longer; get rid of the HER fdb
4255 * entry installed before
4256 */
4257 zebra_evpn_vtep_uninstall(zevpn,
4258 &vtep_ip);
4259 zvtep->flood_control = flood_control;
4260 zebra_evpn_vtep_install(zevpn, zvtep);
4261 }
4262 } else {
4263 zvtep = zebra_evpn_vtep_add(zevpn, &vtep_ip,
4264 flood_control);
4265 if (zvtep)
4266 zebra_evpn_vtep_install(zevpn, zvtep);
4267 else
4268 flog_err(EC_ZEBRA_VTEP_ADD_FAILED,
4269 "Failed to add remote VTEP, VNI %u zevpn %p",
4270 vni, zevpn);
4271 }
4272 }
4273
4274 stream_failure:
4275 return;
4276 }
4277
4278 /*
4279 * Add/Del gateway macip to evpn
4280 * g/w can be:
4281 * 1. SVI interface on a vlan aware bridge
4282 * 2. SVI interface on a vlan unaware bridge
4283 * 3. vrr interface (MACVLAN) associated to a SVI
4284 * We advertise macip routes for an interface if it is associated to VxLan vlan
4285 */
4286 int zebra_vxlan_add_del_gw_macip(struct interface *ifp, struct prefix *p,
4287 int add)
4288 {
4289 struct ipaddr ip;
4290 struct ethaddr macaddr;
4291 zebra_evpn_t *zevpn = NULL;
4292
4293 memset(&ip, 0, sizeof(struct ipaddr));
4294 memset(&macaddr, 0, sizeof(struct ethaddr));
4295
4296 /* Check if EVPN is enabled. */
4297 if (!is_evpn_enabled())
4298 return 0;
4299
4300 if (IS_ZEBRA_IF_MACVLAN(ifp)) {
4301 struct interface *svi_if =
4302 NULL; /* SVI corresponding to the MACVLAN */
4303 struct zebra_if *ifp_zif =
4304 NULL; /* Zebra daemon specific info for MACVLAN */
4305 struct zebra_if *svi_if_zif =
4306 NULL; /* Zebra daemon specific info for SVI*/
4307
4308 ifp_zif = ifp->info;
4309 if (!ifp_zif)
4310 return -1;
4311
4312 /*
4313 * for a MACVLAN interface the link represents the svi_if
4314 */
4315 svi_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
4316 ifp_zif->link_ifindex);
4317 if (!svi_if) {
4318 zlog_debug("MACVLAN %s(%u) without link information",
4319 ifp->name, ifp->ifindex);
4320 return -1;
4321 }
4322
4323 if (IS_ZEBRA_IF_VLAN(svi_if)) {
4324 /*
4325 * If it is a vlan aware bridge then the link gives the
4326 * bridge information
4327 */
4328 struct interface *svi_if_link = NULL;
4329
4330 svi_if_zif = svi_if->info;
4331 if (svi_if_zif) {
4332 svi_if_link = if_lookup_by_index_per_ns(
4333 zebra_ns_lookup(NS_DEFAULT),
4334 svi_if_zif->link_ifindex);
4335 zevpn = zebra_evpn_from_svi(svi_if,
4336 svi_if_link);
4337 }
4338 } else if (IS_ZEBRA_IF_BRIDGE(svi_if)) {
4339 /*
4340 * If it is a vlan unaware bridge then svi is the bridge
4341 * itself
4342 */
4343 zevpn = zebra_evpn_from_svi(svi_if, svi_if);
4344 }
4345 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
4346 struct zebra_if *svi_if_zif =
4347 NULL; /* Zebra daemon specific info for SVI */
4348 struct interface *svi_if_link =
4349 NULL; /* link info for the SVI = bridge info */
4350
4351 svi_if_zif = ifp->info;
4352 if (svi_if_zif) {
4353 svi_if_link = if_lookup_by_index_per_ns(
4354 zebra_ns_lookup(NS_DEFAULT),
4355 svi_if_zif->link_ifindex);
4356 if (svi_if_link)
4357 zevpn = zebra_evpn_from_svi(ifp, svi_if_link);
4358 }
4359 } else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
4360 zevpn = zebra_evpn_from_svi(ifp, ifp);
4361 }
4362
4363 if (!zevpn)
4364 return 0;
4365
4366 if (!zevpn->vxlan_if) {
4367 zlog_debug("VNI %u hash %p doesn't have intf upon MACVLAN up",
4368 zevpn->vni, zevpn);
4369 return -1;
4370 }
4371
4372
4373 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
4374
4375 if (p->family == AF_INET) {
4376 ip.ipa_type = IPADDR_V4;
4377 memcpy(&(ip.ipaddr_v4), &(p->u.prefix4),
4378 sizeof(struct in_addr));
4379 } else if (p->family == AF_INET6) {
4380 ip.ipa_type = IPADDR_V6;
4381 memcpy(&(ip.ipaddr_v6), &(p->u.prefix6),
4382 sizeof(struct in6_addr));
4383 }
4384
4385
4386 if (add)
4387 zebra_evpn_gw_macip_add(ifp, zevpn, &macaddr, &ip);
4388 else
4389 zebra_evpn_gw_macip_del(ifp, zevpn, &ip);
4390
4391 return 0;
4392 }
4393
4394 /*
4395 * Handle SVI interface going down.
4396 * SVI can be associated to either L3-VNI or L2-VNI.
4397 * For L2-VNI: At this point, this is a NOP since
4398 * the kernel deletes the neighbor entries on this SVI (if any).
4399 * We only need to update the vrf corresponding to zevpn.
4400 * For L3-VNI: L3-VNI is operationally down, update mac-ip routes and delete
4401 * from bgp
4402 */
4403 int zebra_vxlan_svi_down(struct interface *ifp, struct interface *link_if)
4404 {
4405 zebra_l3vni_t *zl3vni = NULL;
4406
4407 zl3vni = zl3vni_from_svi(ifp, link_if);
4408 if (zl3vni) {
4409
4410 /* process l3-vni down */
4411 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4412
4413 /* remove association with svi-if */
4414 zl3vni->svi_if = NULL;
4415 } else {
4416 zebra_evpn_t *zevpn = NULL;
4417
4418 /* since we dont have svi corresponding to zevpn, we associate it
4419 * to default vrf. Note: the corresponding neigh entries on the
4420 * SVI would have already been deleted */
4421 zevpn = zebra_evpn_from_svi(ifp, link_if);
4422 if (zevpn) {
4423 zevpn->vrf_id = VRF_DEFAULT;
4424
4425 /* update the tenant vrf in BGP */
4426 zebra_evpn_send_add_to_client(zevpn);
4427 }
4428 }
4429 return 0;
4430 }
4431
4432 /*
4433 * Handle SVI interface coming up.
4434 * SVI can be associated to L3-VNI (l3vni vxlan interface) or L2-VNI (l2-vni
4435 * vxlan intf).
4436 * For L2-VNI: we need to install any remote neighbors entried (used for
4437 * apr-suppression)
4438 * For L3-VNI: SVI will be used to get the rmac to be used with L3-VNI
4439 */
4440 int zebra_vxlan_svi_up(struct interface *ifp, struct interface *link_if)
4441 {
4442 zebra_evpn_t *zevpn = NULL;
4443 zebra_l3vni_t *zl3vni = NULL;
4444
4445 zl3vni = zl3vni_from_svi(ifp, link_if);
4446 if (zl3vni) {
4447
4448 /* associate with svi */
4449 zl3vni->svi_if = ifp;
4450
4451 /* process oper-up */
4452 if (is_l3vni_oper_up(zl3vni))
4453 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4454 } else {
4455
4456 /* process SVI up for l2-vni */
4457 struct neigh_walk_ctx n_wctx;
4458
4459 zevpn = zebra_evpn_from_svi(ifp, link_if);
4460 if (!zevpn)
4461 return 0;
4462
4463 if (!zevpn->vxlan_if) {
4464 zlog_debug(
4465 "VNI %u hash %p doesn't have intf upon SVI up",
4466 zevpn->vni, zevpn);
4467 return -1;
4468 }
4469
4470 if (IS_ZEBRA_DEBUG_VXLAN)
4471 zlog_debug(
4472 "SVI %s(%u) VNI %u VRF %s is UP, installing neighbors",
4473 ifp->name, ifp->ifindex, zevpn->vni,
4474 vrf_id_to_name(ifp->vrf_id));
4475
4476 /* update the vrf information for l2-vni and inform bgp */
4477 zevpn->vrf_id = ifp->vrf_id;
4478 zebra_evpn_send_add_to_client(zevpn);
4479
4480 /* Install any remote neighbors for this VNI. */
4481 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
4482 n_wctx.zevpn = zevpn;
4483 hash_iterate(zevpn->neigh_table, zebra_evpn_install_neigh_hash,
4484 &n_wctx);
4485 }
4486
4487 return 0;
4488 }
4489
4490 /*
4491 * Handle MAC-VLAN interface going down.
4492 * L3VNI: When MAC-VLAN interface goes down,
4493 * find its associated SVI and update type2/type-5 routes
4494 * with SVI as RMAC
4495 */
4496 void zebra_vxlan_macvlan_down(struct interface *ifp)
4497 {
4498 zebra_l3vni_t *zl3vni = NULL;
4499 struct zebra_if *zif, *link_zif;
4500 struct interface *link_ifp, *link_if;
4501
4502 zif = ifp->info;
4503 assert(zif);
4504 link_ifp = zif->link;
4505 if (!link_ifp) {
4506 if (IS_ZEBRA_DEBUG_VXLAN) {
4507 struct interface *ifp;
4508
4509 ifp = if_lookup_by_index_all_vrf(zif->link_ifindex);
4510 zlog_debug("macvlan parent link is not found. Parent index %d ifp %s",
4511 zif->link_ifindex, ifp ? ifp->name : " ");
4512 }
4513 return;
4514 }
4515 link_zif = link_ifp->info;
4516 assert(link_zif);
4517
4518 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
4519 link_zif->link_ifindex);
4520
4521 zl3vni = zl3vni_from_svi(link_ifp, link_if);
4522 if (zl3vni) {
4523 zl3vni->mac_vlan_if = NULL;
4524 if (is_l3vni_oper_up(zl3vni))
4525 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4526 }
4527 }
4528
4529 /*
4530 * Handle MAC-VLAN interface going up.
4531 * L3VNI: When MAC-VLAN interface comes up,
4532 * find its associated SVI and update type-2 routes
4533 * with MAC-VLAN's MAC as RMAC and for type-5 routes
4534 * use SVI's MAC as RMAC.
4535 */
4536 void zebra_vxlan_macvlan_up(struct interface *ifp)
4537 {
4538 zebra_l3vni_t *zl3vni = NULL;
4539 struct zebra_if *zif, *link_zif;
4540 struct interface *link_ifp, *link_if;
4541
4542 zif = ifp->info;
4543 assert(zif);
4544 link_ifp = zif->link;
4545 link_zif = link_ifp->info;
4546 assert(link_zif);
4547
4548 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
4549 link_zif->link_ifindex);
4550 zl3vni = zl3vni_from_svi(link_ifp, link_if);
4551 if (zl3vni) {
4552 /* associate with macvlan (VRR) interface */
4553 zl3vni->mac_vlan_if = ifp;
4554
4555 /* process oper-up */
4556 if (is_l3vni_oper_up(zl3vni))
4557 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4558 }
4559 }
4560
4561 /*
4562 * Handle VxLAN interface down
4563 */
4564 int zebra_vxlan_if_down(struct interface *ifp)
4565 {
4566 vni_t vni;
4567 struct zebra_if *zif = NULL;
4568 struct zebra_l2info_vxlan *vxl = NULL;
4569 zebra_l3vni_t *zl3vni = NULL;
4570 zebra_evpn_t *zevpn;
4571
4572 /* Check if EVPN is enabled. */
4573 if (!is_evpn_enabled())
4574 return 0;
4575
4576 zif = ifp->info;
4577 assert(zif);
4578 vxl = &zif->l2info.vxl;
4579 vni = vxl->vni;
4580
4581 zl3vni = zl3vni_lookup(vni);
4582 if (zl3vni) {
4583 /* process-if-down for l3-vni */
4584 if (IS_ZEBRA_DEBUG_VXLAN)
4585 zlog_debug("Intf %s(%u) L3-VNI %u is DOWN", ifp->name,
4586 ifp->ifindex, vni);
4587
4588 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4589 } else {
4590 /* process if-down for l2-vni */
4591 if (IS_ZEBRA_DEBUG_VXLAN)
4592 zlog_debug("Intf %s(%u) L2-VNI %u is DOWN", ifp->name,
4593 ifp->ifindex, vni);
4594
4595 /* Locate hash entry; it is expected to exist. */
4596 zevpn = zebra_evpn_lookup(vni);
4597 if (!zevpn) {
4598 zlog_debug(
4599 "Failed to locate VNI hash at DOWN, IF %s(%u) VNI %u",
4600 ifp->name, ifp->ifindex, vni);
4601 return -1;
4602 }
4603
4604 assert(zevpn->vxlan_if == ifp);
4605
4606 /* remove from l3-vni list */
4607 zl3vni = zl3vni_from_vrf(zevpn->vrf_id);
4608 if (zl3vni)
4609 listnode_delete(zl3vni->l2vnis, zevpn);
4610
4611 /* Delete this VNI from BGP. */
4612 zebra_evpn_send_del_to_client(zevpn);
4613
4614 /* Free up all neighbors and MACs, if any. */
4615 zebra_evpn_neigh_del_all(zevpn, 1, 0, DEL_ALL_NEIGH);
4616 zebra_evpn_mac_del_all(zevpn, 1, 0, DEL_ALL_MAC);
4617
4618 /* Free up all remote VTEPs, if any. */
4619 zebra_evpn_vtep_del_all(zevpn, 1);
4620 }
4621 return 0;
4622 }
4623
4624 /*
4625 * Handle VxLAN interface up - update BGP if required.
4626 */
4627 int zebra_vxlan_if_up(struct interface *ifp)
4628 {
4629 vni_t vni;
4630 struct zebra_if *zif = NULL;
4631 struct zebra_l2info_vxlan *vxl = NULL;
4632 zebra_evpn_t *zevpn = NULL;
4633 zebra_l3vni_t *zl3vni = NULL;
4634
4635 /* Check if EVPN is enabled. */
4636 if (!is_evpn_enabled())
4637 return 0;
4638
4639 zif = ifp->info;
4640 assert(zif);
4641 vxl = &zif->l2info.vxl;
4642 vni = vxl->vni;
4643
4644 zl3vni = zl3vni_lookup(vni);
4645 if (zl3vni) {
4646 /* we need to associate with SVI, if any, we can associate with
4647 * svi-if only after association with vxlan-intf is complete
4648 */
4649 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
4650 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
4651
4652 if (IS_ZEBRA_DEBUG_VXLAN)
4653 zlog_debug("Intf %s(%u) L3-VNI %u is UP svi_if %s mac_vlan_if %s"
4654 , ifp->name, ifp->ifindex, vni,
4655 zl3vni->svi_if ? zl3vni->svi_if->name : "NIL",
4656 zl3vni->mac_vlan_if ?
4657 zl3vni->mac_vlan_if->name : "NIL");
4658
4659 if (is_l3vni_oper_up(zl3vni))
4660 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4661 } else {
4662 /* Handle L2-VNI add */
4663 struct interface *vlan_if = NULL;
4664
4665 if (IS_ZEBRA_DEBUG_VXLAN)
4666 zlog_debug("Intf %s(%u) L2-VNI %u is UP", ifp->name,
4667 ifp->ifindex, vni);
4668
4669 /* Locate hash entry; it is expected to exist. */
4670 zevpn = zebra_evpn_lookup(vni);
4671 if (!zevpn) {
4672 zlog_debug(
4673 "Failed to locate EVPN hash at UP, IF %s(%u) VNI %u",
4674 ifp->name, ifp->ifindex, vni);
4675 return -1;
4676 }
4677
4678 assert(zevpn->vxlan_if == ifp);
4679 vlan_if = zvni_map_to_svi(vxl->access_vlan,
4680 zif->brslave_info.br_if);
4681 if (vlan_if) {
4682 zevpn->vrf_id = vlan_if->vrf_id;
4683 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
4684 if (zl3vni)
4685 listnode_add_sort_nodup(zl3vni->l2vnis, zevpn);
4686 }
4687
4688 /* If part of a bridge, inform BGP about this VNI. */
4689 /* Also, read and populate local MACs and neighbors. */
4690 if (zif->brslave_info.br_if) {
4691 zebra_evpn_send_add_to_client(zevpn);
4692 zebra_evpn_read_mac_neigh(zevpn, ifp);
4693 }
4694 }
4695
4696 return 0;
4697 }
4698
4699 /*
4700 * Handle VxLAN interface delete. Locate and remove entry in hash table
4701 * and update BGP, if required.
4702 */
4703 int zebra_vxlan_if_del(struct interface *ifp)
4704 {
4705 vni_t vni;
4706 struct zebra_if *zif = NULL;
4707 struct zebra_l2info_vxlan *vxl = NULL;
4708 zebra_evpn_t *zevpn = NULL;
4709 zebra_l3vni_t *zl3vni = NULL;
4710
4711 /* Check if EVPN is enabled. */
4712 if (!is_evpn_enabled())
4713 return 0;
4714
4715 zif = ifp->info;
4716 assert(zif);
4717 vxl = &zif->l2info.vxl;
4718 vni = vxl->vni;
4719
4720 zl3vni = zl3vni_lookup(vni);
4721 if (zl3vni) {
4722
4723 if (IS_ZEBRA_DEBUG_VXLAN)
4724 zlog_debug("Del L3-VNI %u intf %s(%u)", vni, ifp->name,
4725 ifp->ifindex);
4726
4727 /* process oper-down for l3-vni */
4728 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4729
4730 /* remove the association with vxlan_if */
4731 memset(&zl3vni->local_vtep_ip, 0, sizeof(struct in_addr));
4732 zl3vni->vxlan_if = NULL;
4733 } else {
4734
4735 /* process if-del for l2-vni*/
4736 if (IS_ZEBRA_DEBUG_VXLAN)
4737 zlog_debug("Del L2-VNI %u intf %s(%u)", vni, ifp->name,
4738 ifp->ifindex);
4739
4740 /* Locate hash entry; it is expected to exist. */
4741 zevpn = zebra_evpn_lookup(vni);
4742 if (!zevpn) {
4743 zlog_debug(
4744 "Failed to locate VNI hash at del, IF %s(%u) VNI %u",
4745 ifp->name, ifp->ifindex, vni);
4746 return 0;
4747 }
4748
4749 /* remove from l3-vni list */
4750 zl3vni = zl3vni_from_vrf(zevpn->vrf_id);
4751 if (zl3vni)
4752 listnode_delete(zl3vni->l2vnis, zevpn);
4753 /* Delete VNI from BGP. */
4754 zebra_evpn_send_del_to_client(zevpn);
4755
4756 /* Free up all neighbors and MAC, if any. */
4757 zebra_evpn_neigh_del_all(zevpn, 0, 0, DEL_ALL_NEIGH);
4758 zebra_evpn_mac_del_all(zevpn, 0, 0, DEL_ALL_MAC);
4759
4760 /* Free up all remote VTEPs, if any. */
4761 zebra_evpn_vtep_del_all(zevpn, 0);
4762
4763 /* Delete the hash entry. */
4764 if (zebra_evpn_vxlan_del(zevpn)) {
4765 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
4766 "Failed to del EVPN hash %p, IF %s(%u) VNI %u",
4767 zevpn, ifp->name, ifp->ifindex, zevpn->vni);
4768 return -1;
4769 }
4770 }
4771 return 0;
4772 }
4773
4774 /*
4775 * Handle VxLAN interface update - change to tunnel IP, master or VLAN.
4776 */
4777 int zebra_vxlan_if_update(struct interface *ifp, uint16_t chgflags)
4778 {
4779 vni_t vni;
4780 struct zebra_if *zif = NULL;
4781 struct zebra_l2info_vxlan *vxl = NULL;
4782 zebra_evpn_t *zevpn = NULL;
4783 zebra_l3vni_t *zl3vni = NULL;
4784
4785 /* Check if EVPN is enabled. */
4786 if (!is_evpn_enabled())
4787 return 0;
4788
4789 zif = ifp->info;
4790 assert(zif);
4791 vxl = &zif->l2info.vxl;
4792 vni = vxl->vni;
4793
4794 zl3vni = zl3vni_lookup(vni);
4795 if (zl3vni) {
4796
4797 if (IS_ZEBRA_DEBUG_VXLAN)
4798 zlog_debug(
4799 "Update L3-VNI %u intf %s(%u) VLAN %u local IP %pI4 master %u chg 0x%x",
4800 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
4801 &vxl->vtep_ip,
4802 zif->brslave_info.bridge_ifindex, chgflags);
4803
4804 /* Removed from bridge? Cleanup and return */
4805 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
4806 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
4807 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4808 return 0;
4809 }
4810
4811 /* access-vlan change - process oper down, associate with new
4812 * svi_if and then process oper up again
4813 */
4814 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
4815 if (if_is_operative(ifp)) {
4816 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4817 zl3vni->svi_if = NULL;
4818 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
4819 zl3vni->mac_vlan_if =
4820 zl3vni_map_to_mac_vlan_if(zl3vni);
4821 zl3vni->local_vtep_ip = vxl->vtep_ip;
4822 if (is_l3vni_oper_up(zl3vni))
4823 zebra_vxlan_process_l3vni_oper_up(
4824 zl3vni);
4825 }
4826 }
4827
4828 /*
4829 * local-ip change - process oper down, associate with new
4830 * local-ip and then process oper up again
4831 */
4832 if (chgflags & ZEBRA_VXLIF_LOCAL_IP_CHANGE) {
4833 if (if_is_operative(ifp)) {
4834 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4835 zl3vni->local_vtep_ip = vxl->vtep_ip;
4836 if (is_l3vni_oper_up(zl3vni))
4837 zebra_vxlan_process_l3vni_oper_up(
4838 zl3vni);
4839 }
4840 }
4841
4842 /* Update local tunnel IP. */
4843 zl3vni->local_vtep_ip = vxl->vtep_ip;
4844
4845 /* if we have a valid new master, process l3-vni oper up */
4846 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE) {
4847 if (if_is_operative(ifp) && is_l3vni_oper_up(zl3vni))
4848 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4849 }
4850 } else {
4851
4852 /* Update VNI hash. */
4853 zevpn = zebra_evpn_lookup(vni);
4854 if (!zevpn) {
4855 zlog_debug(
4856 "Failed to find EVPN hash on update, IF %s(%u) VNI %u",
4857 ifp->name, ifp->ifindex, vni);
4858 return -1;
4859 }
4860
4861 if (IS_ZEBRA_DEBUG_VXLAN)
4862 zlog_debug(
4863 "Update L2-VNI %u intf %s(%u) VLAN %u local IP %pI4 master %u chg 0x%x",
4864 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
4865 &vxl->vtep_ip,
4866 zif->brslave_info.bridge_ifindex, chgflags);
4867
4868 /* Removed from bridge? Cleanup and return */
4869 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
4870 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
4871 /* Delete from client, remove all remote VTEPs */
4872 /* Also, free up all MACs and neighbors. */
4873 zebra_evpn_send_del_to_client(zevpn);
4874 zebra_evpn_neigh_del_all(zevpn, 1, 0, DEL_ALL_NEIGH);
4875 zebra_evpn_mac_del_all(zevpn, 1, 0, DEL_ALL_MAC);
4876 zebra_evpn_vtep_del_all(zevpn, 1);
4877 return 0;
4878 }
4879
4880 /* Handle other changes. */
4881 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
4882 /* Remove all existing local neigh and MACs for this VNI
4883 * (including from BGP)
4884 */
4885 zebra_evpn_neigh_del_all(zevpn, 0, 1, DEL_LOCAL_MAC);
4886 zebra_evpn_mac_del_all(zevpn, 0, 1, DEL_LOCAL_MAC);
4887 }
4888
4889 if (zevpn->local_vtep_ip.s_addr != vxl->vtep_ip.s_addr ||
4890 zevpn->mcast_grp.s_addr != vxl->mcast_grp.s_addr) {
4891 zebra_vxlan_sg_deref(zevpn->local_vtep_ip,
4892 zevpn->mcast_grp);
4893 zebra_vxlan_sg_ref(vxl->vtep_ip, vxl->mcast_grp);
4894 zevpn->local_vtep_ip = vxl->vtep_ip;
4895 zevpn->mcast_grp = vxl->mcast_grp;
4896 /* on local vtep-ip check if ES orig-ip
4897 * needs to be updated
4898 */
4899 zebra_evpn_es_set_base_evpn(zevpn);
4900 }
4901 zevpn_vxlan_if_set(zevpn, ifp, true /* set */);
4902 /* Take further actions needed.
4903 * Note that if we are here, there is a change of interest.
4904 */
4905 /* If down or not mapped to a bridge, we're done. */
4906 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
4907 return 0;
4908
4909 /* Inform BGP, if there is a change of interest. */
4910 if (chgflags
4911 & (ZEBRA_VXLIF_MASTER_CHANGE |
4912 ZEBRA_VXLIF_LOCAL_IP_CHANGE |
4913 ZEBRA_VXLIF_MCAST_GRP_CHANGE))
4914 zebra_evpn_send_add_to_client(zevpn);
4915
4916 /* If there is a valid new master or a VLAN mapping change,
4917 * read and populate local MACs and neighbors.
4918 * Also, reinstall any remote MACs and neighbors
4919 * for this VNI (based on new VLAN).
4920 */
4921 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
4922 zebra_evpn_read_mac_neigh(zevpn, ifp);
4923 else if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
4924 struct mac_walk_ctx m_wctx;
4925 struct neigh_walk_ctx n_wctx;
4926
4927 zebra_evpn_read_mac_neigh(zevpn, ifp);
4928
4929 memset(&m_wctx, 0, sizeof(struct mac_walk_ctx));
4930 m_wctx.zevpn = zevpn;
4931 hash_iterate(zevpn->mac_table,
4932 zebra_evpn_install_mac_hash, &m_wctx);
4933
4934 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
4935 n_wctx.zevpn = zevpn;
4936 hash_iterate(zevpn->neigh_table,
4937 zebra_evpn_install_neigh_hash, &n_wctx);
4938 }
4939 }
4940
4941 return 0;
4942 }
4943
4944 /*
4945 * Handle VxLAN interface add.
4946 */
4947 int zebra_vxlan_if_add(struct interface *ifp)
4948 {
4949 vni_t vni;
4950 struct zebra_if *zif = NULL;
4951 struct zebra_l2info_vxlan *vxl = NULL;
4952 zebra_evpn_t *zevpn = NULL;
4953 zebra_l3vni_t *zl3vni = NULL;
4954
4955 /* Check if EVPN is enabled. */
4956 if (!is_evpn_enabled())
4957 return 0;
4958
4959 zif = ifp->info;
4960 assert(zif);
4961 vxl = &zif->l2info.vxl;
4962 vni = vxl->vni;
4963
4964 zl3vni = zl3vni_lookup(vni);
4965 if (zl3vni) {
4966
4967 /* process if-add for l3-vni*/
4968 if (IS_ZEBRA_DEBUG_VXLAN)
4969 zlog_debug(
4970 "Add L3-VNI %u intf %s(%u) VLAN %u local IP %pI4 master %u",
4971 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
4972 &vxl->vtep_ip,
4973 zif->brslave_info.bridge_ifindex);
4974
4975 /* associate with vxlan_if */
4976 zl3vni->local_vtep_ip = vxl->vtep_ip;
4977 zl3vni->vxlan_if = ifp;
4978
4979 /* Associate with SVI, if any. We can associate with svi-if only
4980 * after association with vxlan_if is complete */
4981 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
4982
4983 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
4984
4985 if (is_l3vni_oper_up(zl3vni))
4986 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4987 } else {
4988
4989 /* process if-add for l2-vni */
4990 struct interface *vlan_if = NULL;
4991
4992 /* Create or update EVPN hash. */
4993 zevpn = zebra_evpn_lookup(vni);
4994 if (!zevpn) {
4995 zevpn = zebra_evpn_add(vni);
4996 if (!zevpn) {
4997 flog_err(
4998 EC_ZEBRA_VNI_ADD_FAILED,
4999 "Failed to add EVPN hash, IF %s(%u) VNI %u",
5000 ifp->name, ifp->ifindex, vni);
5001 return -1;
5002 }
5003 }
5004
5005 if (zevpn->local_vtep_ip.s_addr != vxl->vtep_ip.s_addr ||
5006 zevpn->mcast_grp.s_addr != vxl->mcast_grp.s_addr) {
5007 zebra_vxlan_sg_deref(zevpn->local_vtep_ip,
5008 zevpn->mcast_grp);
5009 zebra_vxlan_sg_ref(vxl->vtep_ip, vxl->mcast_grp);
5010 zevpn->local_vtep_ip = vxl->vtep_ip;
5011 zevpn->mcast_grp = vxl->mcast_grp;
5012 /* on local vtep-ip check if ES orig-ip
5013 * needs to be updated
5014 */
5015 zebra_evpn_es_set_base_evpn(zevpn);
5016 }
5017 zevpn_vxlan_if_set(zevpn, ifp, true /* set */);
5018 vlan_if = zvni_map_to_svi(vxl->access_vlan,
5019 zif->brslave_info.br_if);
5020 if (vlan_if) {
5021 zevpn->vrf_id = vlan_if->vrf_id;
5022 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
5023 if (zl3vni)
5024 listnode_add_sort_nodup(zl3vni->l2vnis, zevpn);
5025 }
5026
5027 if (IS_ZEBRA_DEBUG_VXLAN) {
5028 char addr_buf1[INET_ADDRSTRLEN];
5029 char addr_buf2[INET_ADDRSTRLEN];
5030
5031 inet_ntop(AF_INET, &vxl->vtep_ip,
5032 addr_buf1, INET_ADDRSTRLEN);
5033 inet_ntop(AF_INET, &vxl->mcast_grp,
5034 addr_buf2, INET_ADDRSTRLEN);
5035
5036 zlog_debug(
5037 "Add L2-VNI %u VRF %s intf %s(%u) VLAN %u local IP %s mcast_grp %s master %u",
5038 vni,
5039 vlan_if ? vrf_id_to_name(vlan_if->vrf_id)
5040 : VRF_DEFAULT_NAME,
5041 ifp->name, ifp->ifindex, vxl->access_vlan,
5042 addr_buf1, addr_buf2,
5043 zif->brslave_info.bridge_ifindex);
5044 }
5045
5046 /* If down or not mapped to a bridge, we're done. */
5047 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
5048 return 0;
5049
5050 /* Inform BGP */
5051 zebra_evpn_send_add_to_client(zevpn);
5052
5053 /* Read and populate local MACs and neighbors */
5054 zebra_evpn_read_mac_neigh(zevpn, ifp);
5055 }
5056
5057 return 0;
5058 }
5059
5060 int zebra_vxlan_process_vrf_vni_cmd(struct zebra_vrf *zvrf, vni_t vni,
5061 char *err, int err_str_sz, int filter,
5062 int add)
5063 {
5064 zebra_l3vni_t *zl3vni = NULL;
5065 struct zebra_vrf *zvrf_evpn = NULL;
5066
5067 zvrf_evpn = zebra_vrf_get_evpn();
5068 if (!zvrf_evpn)
5069 return -1;
5070
5071 if (IS_ZEBRA_DEBUG_VXLAN)
5072 zlog_debug("vrf %s vni %u %s", zvrf_name(zvrf), vni,
5073 add ? "ADD" : "DEL");
5074
5075 if (add) {
5076
5077 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
5078
5079 /* check if the vni is already present under zvrf */
5080 if (zvrf->l3vni) {
5081 snprintf(err, err_str_sz,
5082 "VNI is already configured under the vrf");
5083 return -1;
5084 }
5085
5086 /* check if this VNI is already present in the system */
5087 zl3vni = zl3vni_lookup(vni);
5088 if (zl3vni) {
5089 snprintf(err, err_str_sz,
5090 "VNI is already configured as L3-VNI");
5091 return -1;
5092 }
5093
5094 /* add the L3-VNI to the global table */
5095 zl3vni = zl3vni_add(vni, zvrf_id(zvrf));
5096 if (!zl3vni) {
5097 snprintf(err, err_str_sz, "Could not add L3-VNI");
5098 return -1;
5099 }
5100
5101 /* associate the vrf with vni */
5102 zvrf->l3vni = vni;
5103
5104 /* set the filter in l3vni to denote if we are using l3vni only
5105 * for prefix routes
5106 */
5107 if (filter)
5108 SET_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY);
5109
5110 /* associate with vxlan-intf;
5111 * we need to associate with the vxlan-intf first
5112 */
5113 zl3vni->vxlan_if = zl3vni_map_to_vxlan_if(zl3vni);
5114
5115 /* associate with corresponding SVI interface, we can associate
5116 * with svi-if only after vxlan interface association is
5117 * complete
5118 */
5119 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
5120
5121 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
5122
5123 if (IS_ZEBRA_DEBUG_VXLAN)
5124 zlog_debug(
5125 "%s: l3vni %u svi_if %s mac_vlan_if %s",
5126 __func__, vni,
5127 zl3vni->svi_if ? zl3vni->svi_if->name : "NIL",
5128 zl3vni->mac_vlan_if ? zl3vni->mac_vlan_if->name
5129 : "NIL");
5130
5131 /* formulate l2vni list */
5132 hash_iterate(zvrf_evpn->evpn_table, zevpn_add_to_l3vni_list,
5133 zl3vni);
5134
5135 if (is_l3vni_oper_up(zl3vni))
5136 zebra_vxlan_process_l3vni_oper_up(zl3vni);
5137
5138 } else {
5139 zl3vni = zl3vni_lookup(vni);
5140 if (!zl3vni) {
5141 snprintf(err, err_str_sz, "VNI doesn't exist");
5142 return -1;
5143 }
5144
5145 if (zvrf->l3vni != vni) {
5146 snprintf(err, err_str_sz,
5147 "VNI %d doesn't exist in VRF: %s",
5148 vni, zvrf->vrf->name);
5149 return -1;
5150 }
5151
5152 if (filter && !CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)) {
5153 snprintf(err, ERR_STR_SZ,
5154 "prefix-routes-only is not set for the vni");
5155 return -1;
5156 }
5157
5158 zebra_vxlan_process_l3vni_oper_down(zl3vni);
5159
5160 /* delete and uninstall all rmacs */
5161 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
5162 zl3vni);
5163
5164 /* delete and uninstall all next-hops */
5165 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
5166 zl3vni);
5167
5168 zvrf->l3vni = 0;
5169 zl3vni_del(zl3vni);
5170
5171 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
5172 }
5173 return 0;
5174 }
5175
5176 int zebra_vxlan_vrf_enable(struct zebra_vrf *zvrf)
5177 {
5178 zebra_l3vni_t *zl3vni = NULL;
5179
5180 if (zvrf->l3vni)
5181 zl3vni = zl3vni_lookup(zvrf->l3vni);
5182 if (!zl3vni)
5183 return 0;
5184
5185 zl3vni->vrf_id = zvrf_id(zvrf);
5186 if (is_l3vni_oper_up(zl3vni))
5187 zebra_vxlan_process_l3vni_oper_up(zl3vni);
5188 return 0;
5189 }
5190
5191 int zebra_vxlan_vrf_disable(struct zebra_vrf *zvrf)
5192 {
5193 zebra_l3vni_t *zl3vni = NULL;
5194
5195 if (zvrf->l3vni)
5196 zl3vni = zl3vni_lookup(zvrf->l3vni);
5197 if (!zl3vni)
5198 return 0;
5199
5200 zebra_vxlan_process_l3vni_oper_down(zl3vni);
5201
5202 /* delete and uninstall all rmacs */
5203 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry, zl3vni);
5204 /* delete and uninstall all next-hops */
5205 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry, zl3vni);
5206
5207 zl3vni->vrf_id = VRF_UNKNOWN;
5208
5209 return 0;
5210 }
5211
5212 int zebra_vxlan_vrf_delete(struct zebra_vrf *zvrf)
5213 {
5214 zebra_l3vni_t *zl3vni = NULL;
5215 vni_t vni;
5216
5217 if (zvrf->l3vni)
5218 zl3vni = zl3vni_lookup(zvrf->l3vni);
5219 if (!zl3vni)
5220 return 0;
5221
5222 vni = zl3vni->vni;
5223 zl3vni_del(zl3vni);
5224 zebra_vxlan_handle_vni_transition(zvrf, vni, 0);
5225
5226 return 0;
5227 }
5228
5229 /*
5230 * Handle message from client to specify the flooding mechanism for
5231 * BUM packets. The default is to do head-end (ingress) replication
5232 * and the other supported option is to disable it. This applies to
5233 * all BUM traffic and disabling it applies to both the transmit and
5234 * receive direction.
5235 */
5236 void zebra_vxlan_flood_control(ZAPI_HANDLER_ARGS)
5237 {
5238 struct stream *s;
5239 enum vxlan_flood_control flood_ctrl;
5240
5241 if (!EVPN_ENABLED(zvrf)) {
5242 zlog_err("EVPN flood control for non-EVPN VRF %u",
5243 zvrf_id(zvrf));
5244 return;
5245 }
5246
5247 s = msg;
5248 STREAM_GETC(s, flood_ctrl);
5249
5250 if (IS_ZEBRA_DEBUG_VXLAN)
5251 zlog_debug("EVPN flood control %u, currently %u",
5252 flood_ctrl, zvrf->vxlan_flood_ctrl);
5253
5254 if (zvrf->vxlan_flood_ctrl == flood_ctrl)
5255 return;
5256
5257 zvrf->vxlan_flood_ctrl = flood_ctrl;
5258
5259 /* Install or uninstall flood entries corresponding to
5260 * remote VTEPs.
5261 */
5262 hash_iterate(zvrf->evpn_table, zebra_evpn_handle_flooding_remote_vteps,
5263 zvrf);
5264
5265 stream_failure:
5266 return;
5267 }
5268
5269 /*
5270 * Handle message from client to enable/disable advertisement of svi macip
5271 * routes
5272 */
5273 void zebra_vxlan_advertise_svi_macip(ZAPI_HANDLER_ARGS)
5274 {
5275 struct stream *s;
5276 int advertise;
5277 vni_t vni = 0;
5278 zebra_evpn_t *zevpn = NULL;
5279 struct interface *ifp = NULL;
5280
5281 if (!EVPN_ENABLED(zvrf)) {
5282 zlog_debug("EVPN SVI-MACIP Adv for non-EVPN VRF %u",
5283 zvrf_id(zvrf));
5284 return;
5285 }
5286
5287 s = msg;
5288 STREAM_GETC(s, advertise);
5289 STREAM_GETL(s, vni);
5290
5291 if (!vni) {
5292 if (IS_ZEBRA_DEBUG_VXLAN)
5293 zlog_debug("EVPN SVI-MACIP Adv %s, currently %s",
5294 advertise ? "enabled" : "disabled",
5295 advertise_svi_macip_enabled(NULL)
5296 ? "enabled"
5297 : "disabled");
5298
5299 if (zvrf->advertise_svi_macip == advertise)
5300 return;
5301
5302
5303 if (advertise) {
5304 zvrf->advertise_svi_macip = advertise;
5305 hash_iterate(zvrf->evpn_table,
5306 zebra_evpn_gw_macip_add_for_evpn_hash,
5307 NULL);
5308 } else {
5309 hash_iterate(zvrf->evpn_table,
5310 zebra_evpn_svi_macip_del_for_evpn_hash,
5311 NULL);
5312 zvrf->advertise_svi_macip = advertise;
5313 }
5314
5315 } else {
5316 struct zebra_if *zif = NULL;
5317 struct zebra_l2info_vxlan zl2_info;
5318 struct interface *vlan_if = NULL;
5319
5320 zevpn = zebra_evpn_lookup(vni);
5321 if (!zevpn)
5322 return;
5323
5324 if (IS_ZEBRA_DEBUG_VXLAN)
5325 zlog_debug(
5326 "EVPN SVI macip Adv %s on VNI %d , currently %s",
5327 advertise ? "enabled" : "disabled", vni,
5328 advertise_svi_macip_enabled(zevpn)
5329 ? "enabled"
5330 : "disabled");
5331
5332 if (zevpn->advertise_svi_macip == advertise)
5333 return;
5334
5335 /* Store flag even though SVI is not present.
5336 * Once SVI comes up triggers self MAC-IP route add.
5337 */
5338 zevpn->advertise_svi_macip = advertise;
5339
5340 ifp = zevpn->vxlan_if;
5341 if (!ifp)
5342 return;
5343
5344 zif = ifp->info;
5345
5346 /* If down or not mapped to a bridge, we're done. */
5347 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
5348 return;
5349
5350 zl2_info = zif->l2info.vxl;
5351 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
5352 zif->brslave_info.br_if);
5353 if (!vlan_if)
5354 return;
5355
5356 if (advertise) {
5357 /* Add primary SVI MAC-IP */
5358 zebra_evpn_add_macip_for_intf(vlan_if, zevpn);
5359 } else {
5360 /* Del primary SVI MAC-IP */
5361 zebra_evpn_del_macip_for_intf(vlan_if, zevpn);
5362 }
5363 }
5364
5365 stream_failure:
5366 return;
5367 }
5368
5369 /*
5370 * Handle message from client to enable/disable advertisement of g/w macip
5371 * routes
5372 */
5373 void zebra_vxlan_advertise_subnet(ZAPI_HANDLER_ARGS)
5374 {
5375 struct stream *s;
5376 int advertise;
5377 vni_t vni = 0;
5378 zebra_evpn_t *zevpn = NULL;
5379 struct interface *ifp = NULL;
5380 struct zebra_if *zif = NULL;
5381 struct zebra_l2info_vxlan zl2_info;
5382 struct interface *vlan_if = NULL;
5383
5384 if (!EVPN_ENABLED(zvrf)) {
5385 zlog_debug("EVPN GW-MACIP Adv for non-EVPN VRF %u",
5386 zvrf_id(zvrf));
5387 return;
5388 }
5389
5390 s = msg;
5391 STREAM_GETC(s, advertise);
5392 STREAM_GET(&vni, s, 3);
5393
5394 zevpn = zebra_evpn_lookup(vni);
5395 if (!zevpn)
5396 return;
5397
5398 if (zevpn->advertise_subnet == advertise)
5399 return;
5400
5401 if (IS_ZEBRA_DEBUG_VXLAN)
5402 zlog_debug("EVPN subnet Adv %s on VNI %d , currently %s",
5403 advertise ? "enabled" : "disabled", vni,
5404 zevpn->advertise_subnet ? "enabled" : "disabled");
5405
5406
5407 zevpn->advertise_subnet = advertise;
5408
5409 ifp = zevpn->vxlan_if;
5410 if (!ifp)
5411 return;
5412
5413 zif = ifp->info;
5414
5415 /* If down or not mapped to a bridge, we're done. */
5416 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
5417 return;
5418
5419 zl2_info = zif->l2info.vxl;
5420
5421 vlan_if =
5422 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
5423 if (!vlan_if)
5424 return;
5425
5426 if (zevpn->advertise_subnet)
5427 zebra_evpn_advertise_subnet(zevpn, vlan_if, 1);
5428 else
5429 zebra_evpn_advertise_subnet(zevpn, vlan_if, 0);
5430
5431 stream_failure:
5432 return;
5433 }
5434
5435 /*
5436 * Handle message from client to enable/disable advertisement of g/w macip
5437 * routes
5438 */
5439 void zebra_vxlan_advertise_gw_macip(ZAPI_HANDLER_ARGS)
5440 {
5441 struct stream *s;
5442 int advertise;
5443 vni_t vni = 0;
5444 zebra_evpn_t *zevpn = NULL;
5445 struct interface *ifp = NULL;
5446
5447 if (!EVPN_ENABLED(zvrf)) {
5448 zlog_debug("EVPN GW-MACIP Adv for non-EVPN VRF %u",
5449 zvrf_id(zvrf));
5450 return;
5451 }
5452
5453 s = msg;
5454 STREAM_GETC(s, advertise);
5455 STREAM_GETL(s, vni);
5456
5457 if (!vni) {
5458 if (IS_ZEBRA_DEBUG_VXLAN)
5459 zlog_debug("EVPN gateway macip Adv %s, currently %s",
5460 advertise ? "enabled" : "disabled",
5461 advertise_gw_macip_enabled(NULL)
5462 ? "enabled"
5463 : "disabled");
5464
5465 if (zvrf->advertise_gw_macip == advertise)
5466 return;
5467
5468 zvrf->advertise_gw_macip = advertise;
5469
5470 if (advertise_gw_macip_enabled(zevpn))
5471 hash_iterate(zvrf->evpn_table,
5472 zebra_evpn_gw_macip_add_for_evpn_hash,
5473 NULL);
5474 else
5475 hash_iterate(zvrf->evpn_table,
5476 zebra_evpn_gw_macip_del_for_evpn_hash,
5477 NULL);
5478
5479 } else {
5480 struct zebra_if *zif = NULL;
5481 struct zebra_l2info_vxlan zl2_info;
5482 struct interface *vlan_if = NULL;
5483 struct interface *vrr_if = NULL;
5484
5485 zevpn = zebra_evpn_lookup(vni);
5486 if (!zevpn)
5487 return;
5488
5489 if (IS_ZEBRA_DEBUG_VXLAN)
5490 zlog_debug(
5491 "EVPN gateway macip Adv %s on VNI %d , currently %s",
5492 advertise ? "enabled" : "disabled", vni,
5493 advertise_gw_macip_enabled(zevpn) ? "enabled"
5494 : "disabled");
5495
5496 if (zevpn->advertise_gw_macip == advertise)
5497 return;
5498
5499 zevpn->advertise_gw_macip = advertise;
5500
5501 ifp = zevpn->vxlan_if;
5502 if (!ifp)
5503 return;
5504
5505 zif = ifp->info;
5506
5507 /* If down or not mapped to a bridge, we're done. */
5508 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
5509 return;
5510
5511 zl2_info = zif->l2info.vxl;
5512
5513 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
5514 zif->brslave_info.br_if);
5515 if (!vlan_if)
5516 return;
5517
5518 if (advertise_gw_macip_enabled(zevpn)) {
5519 /* Add primary SVI MAC-IP */
5520 zebra_evpn_add_macip_for_intf(vlan_if, zevpn);
5521
5522 /* Add VRR MAC-IP - if any*/
5523 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
5524 if (vrr_if)
5525 zebra_evpn_add_macip_for_intf(vrr_if, zevpn);
5526 } else {
5527 /* Del primary MAC-IP */
5528 zebra_evpn_del_macip_for_intf(vlan_if, zevpn);
5529
5530 /* Del VRR MAC-IP - if any*/
5531 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
5532 if (vrr_if)
5533 zebra_evpn_del_macip_for_intf(vrr_if, zevpn);
5534 }
5535 }
5536
5537 stream_failure:
5538 return;
5539 }
5540
5541 static int macfdb_read_ns(struct ns *ns,
5542 void *_in_param __attribute__((unused)),
5543 void **out_param __attribute__((unused)))
5544 {
5545 struct zebra_ns *zns = ns->info;
5546
5547 macfdb_read(zns);
5548 return NS_WALK_CONTINUE;
5549 }
5550
5551 static int neigh_read_ns(struct ns *ns,
5552 void *_in_param __attribute__((unused)),
5553 void **out_param __attribute__((unused)))
5554 {
5555 struct zebra_ns *zns = ns->info;
5556
5557 neigh_read(zns);
5558 return NS_WALK_CONTINUE;
5559 }
5560
5561 /*
5562 * Handle message from client to learn (or stop learning) about VNIs and MACs.
5563 * When enabled, the VNI hash table will be built and MAC FDB table read;
5564 * when disabled, the entries should be deleted and remote VTEPs and MACs
5565 * uninstalled from the kernel.
5566 * This also informs the setting for BUM handling at the time this change
5567 * occurs; it is relevant only when specifying "learn".
5568 */
5569 void zebra_vxlan_advertise_all_vni(ZAPI_HANDLER_ARGS)
5570 {
5571 struct stream *s = NULL;
5572 int advertise = 0;
5573 enum vxlan_flood_control flood_ctrl;
5574
5575 /* Mismatch between EVPN VRF and current VRF (should be prevented by
5576 * bgpd's cli) */
5577 if (is_evpn_enabled() && !EVPN_ENABLED(zvrf))
5578 return;
5579
5580 s = msg;
5581 STREAM_GETC(s, advertise);
5582 STREAM_GETC(s, flood_ctrl);
5583
5584 if (IS_ZEBRA_DEBUG_VXLAN)
5585 zlog_debug("EVPN VRF %s(%u) VNI Adv %s, currently %s, flood control %u",
5586 zvrf_name(zvrf), zvrf_id(zvrf),
5587 advertise ? "enabled" : "disabled",
5588 is_evpn_enabled() ? "enabled" : "disabled",
5589 flood_ctrl);
5590
5591 if (zvrf->advertise_all_vni == advertise)
5592 return;
5593
5594 zvrf->advertise_all_vni = advertise;
5595 if (EVPN_ENABLED(zvrf)) {
5596 zrouter.evpn_vrf = zvrf;
5597
5598 /* Note BUM handling */
5599 zvrf->vxlan_flood_ctrl = flood_ctrl;
5600
5601 /* Replay all ESs */
5602 zebra_evpn_es_send_all_to_client(true /* add */);
5603
5604 /* Build EVPN hash table and inform BGP. */
5605 zevpn_build_hash_table();
5606
5607 /* Add all SVI (L3 GW) MACs to BGP*/
5608 hash_iterate(zvrf->evpn_table,
5609 zebra_evpn_gw_macip_add_for_evpn_hash, NULL);
5610
5611 /* Read the MAC FDB */
5612 ns_walk_func(macfdb_read_ns, NULL, NULL);
5613
5614 /* Read neighbors */
5615 ns_walk_func(neigh_read_ns, NULL, NULL);
5616 } else {
5617 /* Cleanup VTEPs for all EVPNs - uninstall from
5618 * kernel and free entries.
5619 */
5620 hash_iterate(zvrf->evpn_table, zebra_evpn_vxlan_cleanup_all,
5621 zvrf);
5622
5623 /* Delete all ESs in BGP */
5624 zebra_evpn_es_send_all_to_client(false /* add */);
5625
5626 /* cleanup all l3vnis */
5627 hash_iterate(zrouter.l3vni_table, zl3vni_cleanup_all, NULL);
5628
5629 /* Mark as "no EVPN VRF" */
5630 zrouter.evpn_vrf = NULL;
5631 }
5632
5633 stream_failure:
5634 return;
5635 }
5636
5637 /*
5638 * Allocate EVPN hash table for this VRF and do other initialization.
5639 * NOTE: Currently supported only for default VRF.
5640 */
5641 void zebra_vxlan_init_tables(struct zebra_vrf *zvrf)
5642 {
5643 if (!zvrf)
5644 return;
5645 zvrf->evpn_table =
5646 hash_create(zebra_evpn_hash_keymake, zebra_evpn_hash_cmp,
5647 "Zebra VRF EVPN Table");
5648 zvrf->vxlan_sg_table = hash_create(zebra_vxlan_sg_hash_key_make,
5649 zebra_vxlan_sg_hash_eq, "Zebra VxLAN SG Table");
5650 }
5651
5652 /* Cleanup EVPN info, but don't free the table. */
5653 void zebra_vxlan_cleanup_tables(struct zebra_vrf *zvrf)
5654 {
5655 struct zebra_vrf *evpn_zvrf = zebra_vrf_get_evpn();
5656
5657 if (!zvrf)
5658 return;
5659 hash_iterate(zvrf->evpn_table, zebra_evpn_vxlan_cleanup_all, zvrf);
5660 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_sg_cleanup, NULL);
5661
5662 if (zvrf == evpn_zvrf)
5663 zebra_evpn_es_cleanup();
5664 }
5665
5666 /* Close all EVPN handling */
5667 void zebra_vxlan_close_tables(struct zebra_vrf *zvrf)
5668 {
5669 if (!zvrf)
5670 return;
5671 hash_iterate(zvrf->evpn_table, zebra_evpn_vxlan_cleanup_all, zvrf);
5672 hash_free(zvrf->evpn_table);
5673 }
5674
5675 /* init the l3vni table */
5676 void zebra_vxlan_init(void)
5677 {
5678 zrouter.l3vni_table = hash_create(l3vni_hash_keymake, l3vni_hash_cmp,
5679 "Zebra VRF L3 VNI table");
5680 zrouter.evpn_vrf = NULL;
5681 zebra_evpn_mh_init();
5682 }
5683
5684 /* free l3vni table */
5685 void zebra_vxlan_disable(void)
5686 {
5687 hash_free(zrouter.l3vni_table);
5688 zebra_evpn_mh_terminate();
5689 }
5690
5691 /* get the l3vni svi ifindex */
5692 ifindex_t get_l3vni_svi_ifindex(vrf_id_t vrf_id)
5693 {
5694 zebra_l3vni_t *zl3vni = NULL;
5695
5696 zl3vni = zl3vni_from_vrf(vrf_id);
5697 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
5698 return 0;
5699
5700 return zl3vni->svi_if->ifindex;
5701 }
5702
5703 /************************** vxlan SG cache management ************************/
5704 /* Inform PIM about the mcast group */
5705 static int zebra_vxlan_sg_send(struct zebra_vrf *zvrf,
5706 struct prefix_sg *sg,
5707 char *sg_str, uint16_t cmd)
5708 {
5709 struct zserv *client = NULL;
5710 struct stream *s = NULL;
5711
5712 client = zserv_find_client(ZEBRA_ROUTE_PIM, 0);
5713 if (!client)
5714 return 0;
5715
5716 if (!CHECK_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG))
5717 return 0;
5718
5719 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
5720
5721 zclient_create_header(s, cmd, VRF_DEFAULT);
5722 stream_putl(s, IPV4_MAX_BYTELEN);
5723 stream_put(s, &sg->src.s_addr, IPV4_MAX_BYTELEN);
5724 stream_put(s, &sg->grp.s_addr, IPV4_MAX_BYTELEN);
5725
5726 /* Write packet size. */
5727 stream_putw_at(s, 0, stream_get_endp(s));
5728
5729 if (IS_ZEBRA_DEBUG_VXLAN)
5730 zlog_debug(
5731 "Send %s %s to %s",
5732 (cmd == ZEBRA_VXLAN_SG_ADD) ? "add" : "del", sg_str,
5733 zebra_route_string(client->proto));
5734
5735 if (cmd == ZEBRA_VXLAN_SG_ADD)
5736 client->vxlan_sg_add_cnt++;
5737 else
5738 client->vxlan_sg_del_cnt++;
5739
5740 return zserv_send_message(client, s);
5741 }
5742
5743 static unsigned int zebra_vxlan_sg_hash_key_make(const void *p)
5744 {
5745 const zebra_vxlan_sg_t *vxlan_sg = p;
5746
5747 return (jhash_2words(vxlan_sg->sg.src.s_addr,
5748 vxlan_sg->sg.grp.s_addr, 0));
5749 }
5750
5751 static bool zebra_vxlan_sg_hash_eq(const void *p1, const void *p2)
5752 {
5753 const zebra_vxlan_sg_t *sg1 = p1;
5754 const zebra_vxlan_sg_t *sg2 = p2;
5755
5756 return ((sg1->sg.src.s_addr == sg2->sg.src.s_addr)
5757 && (sg1->sg.grp.s_addr == sg2->sg.grp.s_addr));
5758 }
5759
5760 static zebra_vxlan_sg_t *zebra_vxlan_sg_new(struct zebra_vrf *zvrf,
5761 struct prefix_sg *sg)
5762 {
5763 zebra_vxlan_sg_t *vxlan_sg;
5764
5765 vxlan_sg = XCALLOC(MTYPE_ZVXLAN_SG, sizeof(*vxlan_sg));
5766
5767 vxlan_sg->zvrf = zvrf;
5768 vxlan_sg->sg = *sg;
5769 prefix_sg2str(sg, vxlan_sg->sg_str);
5770
5771 vxlan_sg = hash_get(zvrf->vxlan_sg_table, vxlan_sg, hash_alloc_intern);
5772
5773 if (IS_ZEBRA_DEBUG_VXLAN)
5774 zlog_debug("vxlan SG %s created", vxlan_sg->sg_str);
5775
5776 return vxlan_sg;
5777 }
5778
5779 static zebra_vxlan_sg_t *zebra_vxlan_sg_find(struct zebra_vrf *zvrf,
5780 struct prefix_sg *sg)
5781 {
5782 zebra_vxlan_sg_t lookup;
5783
5784 lookup.sg = *sg;
5785 return hash_lookup(zvrf->vxlan_sg_table, &lookup);
5786 }
5787
5788 static zebra_vxlan_sg_t *zebra_vxlan_sg_add(struct zebra_vrf *zvrf,
5789 struct prefix_sg *sg)
5790 {
5791 zebra_vxlan_sg_t *vxlan_sg;
5792 zebra_vxlan_sg_t *parent = NULL;
5793 struct in_addr sip;
5794
5795 vxlan_sg = zebra_vxlan_sg_find(zvrf, sg);
5796 if (vxlan_sg)
5797 return vxlan_sg;
5798
5799 /* create a *G entry for every BUM group implicitly -
5800 * 1. The SG entry is used by pimd to setup the vxlan-origination-mroute
5801 * 2. the XG entry is used by pimd to setup the
5802 * vxlan-termination-mroute
5803 */
5804 if (sg->src.s_addr != INADDR_ANY) {
5805 memset(&sip, 0, sizeof(sip));
5806 parent = zebra_vxlan_sg_do_ref(zvrf, sip, sg->grp);
5807 if (!parent)
5808 return NULL;
5809 }
5810
5811 vxlan_sg = zebra_vxlan_sg_new(zvrf, sg);
5812 if (!vxlan_sg) {
5813 if (parent)
5814 zebra_vxlan_sg_do_deref(zvrf, sip, sg->grp);
5815 return vxlan_sg;
5816 }
5817
5818 zebra_vxlan_sg_send(zvrf, sg, vxlan_sg->sg_str,
5819 ZEBRA_VXLAN_SG_ADD);
5820
5821 return vxlan_sg;
5822 }
5823
5824 static void zebra_vxlan_sg_del(zebra_vxlan_sg_t *vxlan_sg)
5825 {
5826 struct in_addr sip;
5827 struct zebra_vrf *zvrf;
5828
5829 zvrf = vrf_info_lookup(VRF_DEFAULT);
5830 if (!zvrf)
5831 return;
5832
5833 /* On SG entry deletion remove the reference to its parent XG
5834 * entry
5835 */
5836 if (vxlan_sg->sg.src.s_addr != INADDR_ANY) {
5837 memset(&sip, 0, sizeof(sip));
5838 zebra_vxlan_sg_do_deref(zvrf, sip, vxlan_sg->sg.grp);
5839 }
5840
5841 zebra_vxlan_sg_send(zvrf, &vxlan_sg->sg,
5842 vxlan_sg->sg_str, ZEBRA_VXLAN_SG_DEL);
5843
5844 hash_release(vxlan_sg->zvrf->vxlan_sg_table, vxlan_sg);
5845
5846 if (IS_ZEBRA_DEBUG_VXLAN)
5847 zlog_debug("VXLAN SG %s deleted", vxlan_sg->sg_str);
5848
5849 XFREE(MTYPE_ZVXLAN_SG, vxlan_sg);
5850 }
5851
5852 static void zebra_vxlan_sg_do_deref(struct zebra_vrf *zvrf,
5853 struct in_addr sip, struct in_addr mcast_grp)
5854 {
5855 zebra_vxlan_sg_t *vxlan_sg;
5856 struct prefix_sg sg;
5857
5858 sg.family = AF_INET;
5859 sg.prefixlen = IPV4_MAX_BYTELEN;
5860 sg.src = sip;
5861 sg.grp = mcast_grp;
5862 vxlan_sg = zebra_vxlan_sg_find(zvrf, &sg);
5863 if (!vxlan_sg)
5864 return;
5865
5866 if (vxlan_sg->ref_cnt)
5867 --vxlan_sg->ref_cnt;
5868
5869 if (!vxlan_sg->ref_cnt)
5870 zebra_vxlan_sg_del(vxlan_sg);
5871 }
5872
5873 static zebra_vxlan_sg_t *zebra_vxlan_sg_do_ref(struct zebra_vrf *zvrf,
5874 struct in_addr sip, struct in_addr mcast_grp)
5875 {
5876 zebra_vxlan_sg_t *vxlan_sg;
5877 struct prefix_sg sg;
5878
5879 sg.family = AF_INET;
5880 sg.prefixlen = IPV4_MAX_BYTELEN;
5881 sg.src = sip;
5882 sg.grp = mcast_grp;
5883 vxlan_sg = zebra_vxlan_sg_add(zvrf, &sg);
5884 if (vxlan_sg)
5885 ++vxlan_sg->ref_cnt;
5886
5887 return vxlan_sg;
5888 }
5889
5890 static void zebra_vxlan_sg_deref(struct in_addr local_vtep_ip,
5891 struct in_addr mcast_grp)
5892 {
5893 struct zebra_vrf *zvrf;
5894
5895 if (local_vtep_ip.s_addr == INADDR_ANY
5896 || mcast_grp.s_addr == INADDR_ANY)
5897 return;
5898
5899 zvrf = vrf_info_lookup(VRF_DEFAULT);
5900 if (!zvrf)
5901 return;
5902
5903 zebra_vxlan_sg_do_deref(zvrf, local_vtep_ip, mcast_grp);
5904 }
5905
5906 static void zebra_vxlan_sg_ref(struct in_addr local_vtep_ip,
5907 struct in_addr mcast_grp)
5908 {
5909 struct zebra_vrf *zvrf;
5910
5911 if (local_vtep_ip.s_addr == INADDR_ANY
5912 || mcast_grp.s_addr == INADDR_ANY)
5913 return;
5914
5915 zvrf = vrf_info_lookup(VRF_DEFAULT);
5916 if (!zvrf)
5917 return;
5918 zebra_vxlan_sg_do_ref(zvrf, local_vtep_ip, mcast_grp);
5919 }
5920
5921 static void zebra_vxlan_sg_cleanup(struct hash_bucket *backet, void *arg)
5922 {
5923 zebra_vxlan_sg_t *vxlan_sg = (zebra_vxlan_sg_t *)backet->data;
5924
5925 zebra_vxlan_sg_del(vxlan_sg);
5926 }
5927
5928 static void zebra_vxlan_sg_replay_send(struct hash_bucket *backet, void *arg)
5929 {
5930 zebra_vxlan_sg_t *vxlan_sg = (zebra_vxlan_sg_t *)backet->data;
5931
5932 zebra_vxlan_sg_send(vxlan_sg->zvrf, &vxlan_sg->sg,
5933 vxlan_sg->sg_str, ZEBRA_VXLAN_SG_ADD);
5934 }
5935
5936 /* Handle message from client to replay vxlan SG entries */
5937 void zebra_vxlan_sg_replay(ZAPI_HANDLER_ARGS)
5938 {
5939 if (IS_ZEBRA_DEBUG_VXLAN)
5940 zlog_debug("VxLAN SG updates to PIM, start");
5941
5942 SET_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG);
5943
5944 if (!EVPN_ENABLED(zvrf)) {
5945 if (IS_ZEBRA_DEBUG_VXLAN)
5946 zlog_debug("VxLAN SG replay request on unexpected vrf %d",
5947 zvrf->vrf->vrf_id);
5948 return;
5949 }
5950
5951 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_sg_replay_send, NULL);
5952 }
5953
5954
5955 /* Cleanup EVPN configuration of a specific VRF */
5956 static void zebra_evpn_vrf_cfg_cleanup(struct zebra_vrf *zvrf)
5957 {
5958 zebra_l3vni_t *zl3vni = NULL;
5959
5960 zvrf->advertise_all_vni = 0;
5961 zvrf->advertise_gw_macip = 0;
5962 zvrf->advertise_svi_macip = 0;
5963 zvrf->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
5964
5965 hash_iterate(zvrf->evpn_table, zebra_evpn_cfg_cleanup, NULL);
5966
5967 if (zvrf->l3vni)
5968 zl3vni = zl3vni_lookup(zvrf->l3vni);
5969 if (zl3vni) {
5970 /* delete and uninstall all rmacs */
5971 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
5972 zl3vni);
5973 /* delete and uninstall all next-hops */
5974 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
5975 zl3vni);
5976 }
5977 }
5978
5979 /* Cleanup BGP EVPN configuration upon client disconnect */
5980 static int zebra_evpn_bgp_cfg_clean_up(struct zserv *client)
5981 {
5982 struct vrf *vrf;
5983 struct zebra_vrf *zvrf;
5984
5985 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
5986 zvrf = vrf->info;
5987 if (zvrf)
5988 zebra_evpn_vrf_cfg_cleanup(zvrf);
5989 }
5990
5991 return 0;
5992 }
5993
5994 static int zebra_evpn_pim_cfg_clean_up(struct zserv *client)
5995 {
5996 struct zebra_vrf *zvrf = zebra_vrf_get_evpn();
5997
5998 if (zvrf && CHECK_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG)) {
5999 if (IS_ZEBRA_DEBUG_VXLAN)
6000 zlog_debug("VxLAN SG updates to PIM, stop");
6001 UNSET_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG);
6002 }
6003
6004 return 0;
6005 }
6006
6007 static int zebra_evpn_cfg_clean_up(struct zserv *client)
6008 {
6009 if (client->proto == ZEBRA_ROUTE_BGP)
6010 return zebra_evpn_bgp_cfg_clean_up(client);
6011
6012 if (client->proto == ZEBRA_ROUTE_PIM)
6013 return zebra_evpn_pim_cfg_clean_up(client);
6014
6015 return 0;
6016 }
6017
6018 /*
6019 * Handle results for vxlan dataplane operations.
6020 */
6021 extern void zebra_vxlan_handle_result(struct zebra_dplane_ctx *ctx)
6022 {
6023 /* TODO -- anything other than freeing the context? */
6024 dplane_ctx_fini(&ctx);
6025 }
6026
6027 /* Cleanup BGP EVPN configuration upon client disconnect */
6028 extern void zebra_evpn_init(void)
6029 {
6030 hook_register(zserv_client_close, zebra_evpn_cfg_clean_up);
6031 }