2 * Zebra EVPN for VxLAN code
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
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
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.
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
37 #include "zebra/rib.h"
39 #include "zebra/zebra_ns.h"
40 #include "zebra/zserv.h"
41 #include "zebra/debug.h"
42 #include "zebra/interface.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/rt_netlink.h"
45 #include "zebra/zebra_vxlan_private.h"
46 #include "zebra/zebra_vxlan.h"
47 #include "zebra/zebra_memory.h"
48 #include "zebra/zebra_l2.h"
51 DEFINE_MTYPE_STATIC(ZEBRA
, ZVNI
, "VNI hash");
52 DEFINE_MTYPE_STATIC(ZEBRA
, ZVNI_VTEP
, "VNI remote VTEP");
53 DEFINE_MTYPE_STATIC(ZEBRA
, MAC
, "VNI MAC");
54 DEFINE_MTYPE_STATIC(ZEBRA
, NEIGH
, "VNI Neighbor");
59 /* static function declarations */
60 static void zvni_print_neigh(zebra_neigh_t
*n
, void *ctxt
, json_object
*json
);
61 static void zvni_print_neigh_hash(struct hash_backet
*backet
, void *ctxt
);
62 static void zvni_print_neigh_hash_all_vni(struct hash_backet
*backet
,
64 static void zvni_print_mac(zebra_mac_t
*mac
, void *ctxt
);
65 static void zvni_print_mac_hash(struct hash_backet
*backet
, void *ctxt
);
66 static void zvni_print_mac_hash_all_vni(struct hash_backet
*backet
, void *ctxt
);
67 static void zvni_print(zebra_vni_t
*zvni
, void **ctxt
);
68 static void zvni_print_hash(struct hash_backet
*backet
, void *ctxt
[]);
70 static int zvni_macip_send_msg_to_client(vni_t vni
,
71 struct ethaddr
*macaddr
,
72 struct ipaddr
*ip
, u_char flags
,
74 static unsigned int neigh_hash_keymake(void *p
);
75 static int neigh_cmp(const void *p1
, const void *p2
);
76 static void *zvni_neigh_alloc(void *p
);
77 static zebra_neigh_t
*zvni_neigh_add(zebra_vni_t
*zvni
, struct ipaddr
*ip
,
79 static int zvni_neigh_del(zebra_vni_t
*zvni
, zebra_neigh_t
*n
);
80 static int zvni_neigh_del_hash_entry(struct hash_backet
*backet
, void *arg
);
81 static void zvni_neigh_del_from_vtep(zebra_vni_t
*zvni
, int uninstall
,
82 struct in_addr
*r_vtep_ip
);
83 static void zvni_neigh_del_all(zebra_vni_t
*zvni
,
84 int uninstall
, int upd_client
, u_int32_t flags
);
85 static zebra_neigh_t
*zvni_neigh_lookup(zebra_vni_t
*zvni
, struct ipaddr
*ip
);
86 static int zvni_neigh_send_add_to_client(vni_t vni
,
88 struct ethaddr
*macaddr
, u_char flags
);
89 static int zvni_neigh_send_del_to_client(vni_t vni
,
91 struct ethaddr
*macaddr
, u_char flags
);
92 static int zvni_neigh_install(zebra_vni_t
*zvni
, zebra_neigh_t
*n
);
93 static int zvni_neigh_uninstall(zebra_vni_t
*zvni
, zebra_neigh_t
*n
);
94 static zebra_vni_t
*zvni_map_svi(struct interface
*ifp
,
95 struct interface
*br_if
);
96 static struct interface
*zvni_map_to_svi(vlanid_t vid
,
97 struct interface
*br_if
);
99 static unsigned int mac_hash_keymake(void *p
);
100 static int mac_cmp(const void *p1
, const void *p2
);
101 static void *zvni_mac_alloc(void *p
);
102 static zebra_mac_t
*zvni_mac_add(zebra_vni_t
*zvni
, struct ethaddr
*macaddr
);
103 static int zvni_mac_del(zebra_vni_t
*zvni
, zebra_mac_t
*mac
);
104 static int zvni_mac_del_hash_entry(struct hash_backet
*backet
, void *arg
);
105 static void zvni_mac_del_from_vtep(zebra_vni_t
*zvni
, int uninstall
,
106 struct in_addr
*r_vtep_ip
);
107 static void zvni_mac_del_all(zebra_vni_t
*zvni
,
108 int uninstall
, int upd_client
, u_int32_t flags
);
109 static zebra_mac_t
*zvni_mac_lookup(zebra_vni_t
*zvni
, struct ethaddr
*macaddr
);
110 static int zvni_mac_send_add_to_client(vni_t vni
,
111 struct ethaddr
*macaddr
, u_char flags
);
112 static int zvni_mac_send_del_to_client(vni_t vni
,
113 struct ethaddr
*macaddr
, u_char flags
);
114 static zebra_vni_t
*zvni_map_vlan(struct interface
*ifp
,
115 struct interface
*br_if
, vlanid_t vid
);
116 static int zvni_mac_install(zebra_vni_t
*zvni
, zebra_mac_t
*mac
);
117 static int zvni_mac_uninstall(zebra_vni_t
*zvni
, zebra_mac_t
*mac
, int local
);
118 static void zvni_install_mac_hash(struct hash_backet
*backet
, void *ctxt
);
120 static unsigned int vni_hash_keymake(void *p
);
121 static int vni_hash_cmp(const void *p1
, const void *p2
);
122 static void *zvni_alloc(void *p
);
123 static zebra_vni_t
*zvni_lookup(vni_t vni
);
124 static zebra_vni_t
*zvni_add(vni_t vni
);
125 static int zvni_del(zebra_vni_t
*zvni
);
126 static int zvni_send_add_to_client(zebra_vni_t
*zvni
);
127 static int zvni_send_del_to_client(vni_t vni
);
128 static void zvni_build_hash_table();
129 static int zvni_vtep_match(struct in_addr
*vtep_ip
, zebra_vtep_t
*zvtep
);
130 static zebra_vtep_t
*zvni_vtep_find(zebra_vni_t
*zvni
, struct in_addr
*vtep_ip
);
131 static zebra_vtep_t
*zvni_vtep_add(zebra_vni_t
*zvni
, struct in_addr
*vtep_ip
);
132 static int zvni_vtep_del(zebra_vni_t
*zvni
, zebra_vtep_t
*zvtep
);
133 static int zvni_vtep_del_all(zebra_vni_t
*zvni
, int uninstall
);
134 static int zvni_vtep_install(zebra_vni_t
*zvni
, struct in_addr
*vtep_ip
);
135 static int zvni_vtep_uninstall(zebra_vni_t
*zvni
, struct in_addr
*vtep_ip
);
136 static int zvni_del_macip_for_intf(struct interface
*ifp
, zebra_vni_t
*zvni
);
137 static int zvni_add_macip_for_intf(struct interface
*ifp
, zebra_vni_t
*zvni
);
138 static int zvni_gw_macip_add(struct interface
*ifp
, zebra_vni_t
*zvni
,
139 struct ethaddr
*macaddr
, struct ipaddr
*ip
);
140 static int zvni_gw_macip_del(struct interface
*ifp
, zebra_vni_t
*zvni
,
142 struct interface
*zebra_get_vrr_intf_for_svi(struct interface
*ifp
);
143 static int advertise_gw_macip_enabled(zebra_vni_t
*zvni
);
144 static void zvni_deref_ip2mac(zebra_vni_t
*zvni
, zebra_mac_t
*mac
,
147 /* Private functions */
150 * Return number of valid MACs in a VNI's MAC hash table - all
151 * remote MACs and non-internal (auto) local MACs count.
153 static u_int32_t
num_valid_macs(zebra_vni_t
*zvni
)
156 u_int32_t num_macs
= 0;
158 struct hash_backet
*hb
;
161 hash
= zvni
->mac_table
;
164 for (i
= 0; i
< hash
->size
; i
++) {
165 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
) {
166 mac
= (zebra_mac_t
*)hb
->data
;
167 if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
)
168 || !CHECK_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
))
176 static int advertise_gw_macip_enabled(zebra_vni_t
*zvni
)
178 struct zebra_vrf
*zvrf
;
180 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
181 if (zvrf
&& zvrf
->advertise_gw_macip
)
184 if (zvni
&& zvni
->advertise_gw_macip
)
191 * Helper function to determine maximum width of neighbor IP address for
192 * display - just because we're dealing with IPv6 addresses that can
195 static void zvni_find_neigh_addr_width(struct hash_backet
*backet
, void *ctxt
)
198 char buf
[INET6_ADDRSTRLEN
];
199 struct neigh_walk_ctx
*wctx
= ctxt
;
202 n
= (zebra_neigh_t
*)backet
->data
;
206 ipaddr2str(&n
->ip
, buf
, sizeof(buf
)), width
= strlen(buf
);
207 if (width
> wctx
->addr_width
)
208 wctx
->addr_width
= width
;
212 * Print a specific neighbor entry.
214 static void zvni_print_neigh(zebra_neigh_t
*n
, void *ctxt
, json_object
*json
)
217 char buf1
[ETHER_ADDR_STRLEN
];
218 char buf2
[INET6_ADDRSTRLEN
];
220 ipaddr2str(&n
->ip
, buf2
, sizeof(buf2
));
221 prefix_mac2str(&n
->emac
, buf1
, sizeof(buf1
));
222 vty
= (struct vty
*)ctxt
;
224 vty_out(vty
, "IP: %s\n",
225 ipaddr2str(&n
->ip
, buf2
, sizeof(buf2
)));
226 vty_out(vty
, " MAC: %s",
227 prefix_mac2str(&n
->emac
, buf1
, sizeof(buf1
)));
229 json_object_string_add(json
, "ip", buf2
);
230 json_object_string_add(json
, "mac", buf1
);
232 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
)) {
234 vty_out(vty
, " Remote VTEP: %s",
235 inet_ntoa(n
->r_vtep_ip
));
237 json_object_string_add(json
, "remoteVtep",
238 inet_ntoa(n
->r_vtep_ip
));
240 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
)) {
243 vty_out(vty
, " State: %s",
244 IS_ZEBRA_NEIGH_ACTIVE(n
) ? "Active"
253 * Print neighbor hash entry - called for display of all neighbors.
255 static void zvni_print_neigh_hash(struct hash_backet
*backet
, void *ctxt
)
258 json_object
*json_vni
= NULL
, *json_row
= NULL
;
260 char buf1
[ETHER_ADDR_STRLEN
];
261 char buf2
[INET6_ADDRSTRLEN
];
262 struct neigh_walk_ctx
*wctx
= ctxt
;
265 json_vni
= wctx
->json
;
266 n
= (zebra_neigh_t
*)backet
->data
;
271 json_row
= json_object_new_object();
273 prefix_mac2str(&n
->emac
, buf1
, sizeof(buf1
));
274 ipaddr2str(&n
->ip
, buf2
, sizeof(buf2
));
275 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
)
276 && !(wctx
->flags
& SHOW_REMOTE_NEIGH_FROM_VTEP
)) {
277 if (json_vni
== NULL
) {
278 vty_out(vty
, "%*s %-6s %-17s\n", -wctx
->addr_width
,
279 buf2
, "local", buf1
);
281 json_object_string_add(json_row
, "type", "local");
282 json_object_string_add(json_row
, "mac", buf1
);
286 if (wctx
->flags
& SHOW_REMOTE_NEIGH_FROM_VTEP
) {
287 if (IPV4_ADDR_SAME(&n
->r_vtep_ip
, &wctx
->r_vtep_ip
)) {
288 if (json_vni
== NULL
) {
289 if (wctx
->count
== 0)
291 "%*s %-6s %-17s %-21s\n",
294 "MAC", "Remote VTEP");
295 vty_out(vty
, "%*s %-6s %-17s %-21s\n",
296 -wctx
->addr_width
, buf2
,
298 inet_ntoa(n
->r_vtep_ip
));
300 json_object_string_add(json_row
, "type",
302 json_object_string_add(json_row
, "mac",
304 json_object_string_add(
305 json_row
, "remoteVtep",
306 inet_ntoa(n
->r_vtep_ip
));
311 if (json_vni
== NULL
) {
312 vty_out(vty
, "%*s %-6s %-17s %-21s\n",
313 -wctx
->addr_width
, buf2
, "remote", buf1
,
314 inet_ntoa(n
->r_vtep_ip
));
316 json_object_string_add(json_row
, "type",
318 json_object_string_add(json_row
, "mac", buf1
);
319 json_object_string_add(json_row
, "remoteVtep",
320 inet_ntoa(n
->r_vtep_ip
));
327 json_object_object_add(json_vni
, buf2
, json_row
);
331 * Print neighbors for all VNI.
333 static void zvni_print_neigh_hash_all_vni(struct hash_backet
*backet
,
337 json_object
*json
= NULL
, *json_vni
= NULL
;
340 struct neigh_walk_ctx wctx
;
341 char vni_str
[VNI_STR_LEN
];
343 vty
= (struct vty
*)args
[0];
344 json
= (json_object
*)args
[1];
346 zvni
= (zebra_vni_t
*)backet
->data
;
349 vty_out(vty
, "{}\n");
352 num_neigh
= hashcount(zvni
->neigh_table
);
355 "\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
356 zvni
->vni
, num_neigh
);
358 json_vni
= json_object_new_object();
359 json_object_int_add(json_vni
, "numArpNd", num_neigh
);
360 snprintf(vni_str
, VNI_STR_LEN
, "%u", zvni
->vni
);
364 json_object_object_add(json
, vni_str
, json_vni
);
368 /* Since we have IPv6 addresses to deal with which can vary widely in
369 * size, we try to be a bit more elegant in display by first computing
372 memset(&wctx
, 0, sizeof(struct neigh_walk_ctx
));
375 wctx
.addr_width
= 15;
376 wctx
.json
= json_vni
;
377 hash_iterate(zvni
->neigh_table
, zvni_find_neigh_addr_width
, &wctx
);
380 vty_out(vty
, "%*s %-6s %-17s %-21s\n", -wctx
.addr_width
, "IP",
381 "Type", "MAC", "Remote VTEP");
382 hash_iterate(zvni
->neigh_table
, zvni_print_neigh_hash
, &wctx
);
385 json_object_object_add(json
, vni_str
, json_vni
);
389 * Print a specific MAC entry.
391 static void zvni_print_mac(zebra_mac_t
*mac
, void *ctxt
)
394 zebra_neigh_t
*n
= NULL
;
395 struct listnode
*node
= NULL
;
397 char buf2
[INET6_ADDRSTRLEN
];
399 vty
= (struct vty
*)ctxt
;
400 vty_out(vty
, "MAC: %s",
401 prefix_mac2str(&mac
->macaddr
, buf1
, sizeof(buf1
)));
402 if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
)) {
403 struct zebra_ns
*zns
;
404 struct interface
*ifp
;
407 ifindex
= mac
->fwd_info
.local
.ifindex
;
408 zns
= zebra_ns_lookup(NS_DEFAULT
);
409 ifp
= if_lookup_by_index_per_ns(zns
, ifindex
);
410 if (!ifp
) // unexpected
412 vty_out(vty
, " Intf: %s(%u)", ifp
->name
, ifindex
);
413 if (mac
->fwd_info
.local
.vid
)
414 vty_out(vty
, " VLAN: %u", mac
->fwd_info
.local
.vid
);
415 } else if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
)) {
416 vty_out(vty
, " Remote VTEP: %s",
417 inet_ntoa(mac
->fwd_info
.r_vtep_ip
));
418 } else if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
)) {
419 vty_out(vty
, " Auto Mac ");
423 /* print all the associated neigh */
424 vty_out(vty
, " Neighbors:\n");
425 if (!listcount(mac
->neigh_list
))
426 vty_out(vty
, " No Neighbors\n");
428 for (ALL_LIST_ELEMENTS_RO(mac
->neigh_list
, node
, n
)) {
429 vty_out(vty
, " %s %s\n",
430 ipaddr2str(&n
->ip
, buf2
, sizeof(buf2
)),
431 CHECK_FLAG(n
->flags
, ZEBRA_MAC_LOCAL
)
432 ? (IS_ZEBRA_NEIGH_ACTIVE(n
)
443 * Print MAC hash entry - called for display of all MACs.
445 static void zvni_print_mac_hash(struct hash_backet
*backet
, void *ctxt
)
448 json_object
*json_mac_hdr
= NULL
, *json_mac
= NULL
;
451 struct mac_walk_ctx
*wctx
= ctxt
;
454 json_mac_hdr
= wctx
->json
;
455 mac
= (zebra_mac_t
*)backet
->data
;
459 prefix_mac2str(&mac
->macaddr
, buf1
, sizeof(buf1
));
462 json_mac
= json_object_new_object();
464 if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
)
465 && !(wctx
->flags
& SHOW_REMOTE_MAC_FROM_VTEP
)) {
466 struct zebra_ns
*zns
;
468 struct interface
*ifp
;
471 zns
= zebra_ns_lookup(NS_DEFAULT
);
472 ifindex
= mac
->fwd_info
.local
.ifindex
;
473 ifp
= if_lookup_by_index_per_ns(zns
, ifindex
);
474 if (!ifp
) // unexpected
476 vid
= mac
->fwd_info
.local
.vid
;
477 if (json_mac_hdr
== NULL
)
478 vty_out(vty
, "%-17s %-6s %-21s", buf1
, "local",
481 json_object_string_add(json_mac
, "type", "local");
482 json_object_string_add(json_mac
, "intf", ifp
->name
);
485 if (json_mac_hdr
== NULL
)
486 vty_out(vty
, " %-5u", vid
);
488 json_object_int_add(json_mac
, "vlan", vid
);
490 if (json_mac_hdr
== NULL
)
493 json_object_object_add(json_mac_hdr
, buf1
, json_mac
);
495 } else if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
)) {
496 if (wctx
->flags
& SHOW_REMOTE_MAC_FROM_VTEP
) {
497 if (IPV4_ADDR_SAME(&mac
->fwd_info
.r_vtep_ip
,
499 if (wctx
->count
== 0) {
500 if (json_mac_hdr
== NULL
) {
501 vty_out(vty
, "\nVNI %u\n\n",
504 "%-17s %-6s %-21s %-5s\n",
510 if (json_mac_hdr
== NULL
)
511 vty_out(vty
, "%-17s %-6s %-21s\n", buf1
,
513 inet_ntoa(mac
->fwd_info
516 json_object_string_add(json_mac
, "type",
518 json_object_string_add(
519 json_mac
, "remoteVtep",
520 inet_ntoa(mac
->fwd_info
522 json_object_object_add(json_mac_hdr
,
528 if (json_mac_hdr
== NULL
)
529 vty_out(vty
, "%-17s %-6s %-21s\n", buf1
,
531 inet_ntoa(mac
->fwd_info
.r_vtep_ip
));
533 json_object_string_add(json_mac
, "type",
535 json_object_string_add(
536 json_mac
, "remoteVtep",
537 inet_ntoa(mac
->fwd_info
.r_vtep_ip
));
538 json_object_object_add(json_mac_hdr
, buf1
,
547 * Print MACs for all VNI.
549 static void zvni_print_mac_hash_all_vni(struct hash_backet
*backet
, void *ctxt
)
552 json_object
*json
= NULL
, *json_vni
= NULL
;
553 json_object
*json_mac
= NULL
;
556 struct mac_walk_ctx
*wctx
= ctxt
;
557 char vni_str
[VNI_STR_LEN
];
559 vty
= (struct vty
*)wctx
->vty
;
560 json
= (struct json_object
*)wctx
->json
;
562 zvni
= (zebra_vni_t
*)backet
->data
;
565 vty_out(vty
, "{}\n");
570 /*We are iterating over a new VNI, set the count to 0*/
573 num_macs
= num_valid_macs(zvni
);
578 json_vni
= json_object_new_object();
579 json_mac
= json_object_new_object();
580 snprintf(vni_str
, VNI_STR_LEN
, "%u", zvni
->vni
);
583 if (!CHECK_FLAG(wctx
->flags
, SHOW_REMOTE_MAC_FROM_VTEP
)) {
585 vty_out(vty
, "\nVNI %u #MACs (local and remote) %u\n\n",
586 zvni
->vni
, num_macs
);
587 vty_out(vty
, "%-17s %-6s %-21s %-5s\n", "MAC", "Type",
588 "Intf/Remote VTEP", "VLAN");
590 json_object_int_add(json_vni
, "numMacs", num_macs
);
592 /* assign per-vni to wctx->json object to fill macs
593 * under the vni. Re-assign primary json object to fill
594 * next vni information.
596 wctx
->json
= json_mac
;
597 hash_iterate(zvni
->mac_table
, zvni_print_mac_hash
, wctx
);
601 json_object_object_add(json_vni
, "macs", json_mac
);
602 json_object_object_add(json
, vni_str
, json_vni
);
607 * Print a specific VNI entry.
609 static void zvni_print(zebra_vni_t
*zvni
, void **ctxt
)
615 json_object
*json
= NULL
;
616 json_object
*json_vtep_list
= NULL
;
617 json_object
*json_ip_str
= NULL
;
623 vty_out(vty
, "VNI: %u\n", zvni
->vni
);
625 json_object_int_add(json
, "vni", zvni
->vni
);
627 if (!zvni
->vxlan_if
) { // unexpected
629 vty_out(vty
, " VxLAN interface: unknown\n");
632 num_macs
= num_valid_macs(zvni
);
633 num_neigh
= hashcount(zvni
->neigh_table
);
635 vty_out(vty
, " VxLAN interface: %s ifIndex: %u VTEP IP: %s\n",
636 zvni
->vxlan_if
->name
, zvni
->vxlan_if
->ifindex
,
637 inet_ntoa(zvni
->local_vtep_ip
));
639 json_object_string_add(json
, "vxlanInterface",
640 zvni
->vxlan_if
->name
);
641 json_object_int_add(json
, "ifindex", zvni
->vxlan_if
->ifindex
);
642 json_object_string_add(json
, "vtepIp",
643 inet_ntoa(zvni
->local_vtep_ip
));
644 json_object_string_add(json
, "advertiseGatewayMacip",
645 zvni
->advertise_gw_macip
? "Yes" : "No");
646 json_object_int_add(json
, "numMacs", num_macs
);
647 json_object_int_add(json
, "numArpNd", num_neigh
);
651 vty_out(vty
, " No remote VTEPs known for this VNI\n");
654 vty_out(vty
, " Remote VTEPs for this VNI:\n");
656 json_vtep_list
= json_object_new_array();
657 for (zvtep
= zvni
->vteps
; zvtep
; zvtep
= zvtep
->next
) {
659 vty_out(vty
, " %s\n",
660 inet_ntoa(zvtep
->vtep_ip
));
662 json_ip_str
= json_object_new_string(
663 inet_ntoa(zvtep
->vtep_ip
));
664 json_object_array_add(json_vtep_list
,
669 json_object_object_add(json
, "numRemoteVteps",
674 " Number of MACs (local and remote) known for this VNI: %u\n",
677 " Number of ARPs (IPv4 and IPv6, local and remote) "
678 "known for this VNI: %u\n",
680 vty_out(vty
, " Advertise-gw-macip: %s\n",
681 zvni
->advertise_gw_macip
? "Yes" : "No");
686 * Print a VNI hash entry - called for display of all VNIs.
688 static void zvni_print_hash(struct hash_backet
*backet
, void *ctxt
[])
693 u_int32_t num_vteps
= 0;
694 u_int32_t num_macs
= 0;
695 u_int32_t num_neigh
= 0;
696 json_object
*json
= NULL
;
697 json_object
*json_vni
= NULL
;
698 json_object
*json_ip_str
= NULL
;
699 json_object
*json_vtep_list
= NULL
;
704 zvni
= (zebra_vni_t
*)backet
->data
;
714 num_macs
= num_valid_macs(zvni
);
715 num_neigh
= hashcount(zvni
->neigh_table
);
717 vty_out(vty
, "%-10u %-21s %-15s %-8u %-8u %-15u\n", zvni
->vni
,
718 zvni
->vxlan_if
? zvni
->vxlan_if
->name
: "unknown",
719 inet_ntoa(zvni
->local_vtep_ip
), num_macs
, num_neigh
,
722 char vni_str
[VNI_STR_LEN
];
723 snprintf(vni_str
, VNI_STR_LEN
, "%u", zvni
->vni
);
724 json_vni
= json_object_new_object();
725 json_object_string_add(json_vni
, "vxlanIf",
726 zvni
->vxlan_if
? zvni
->vxlan_if
->name
728 json_object_string_add(json_vni
, "vtepIp",
729 inet_ntoa(zvni
->local_vtep_ip
));
730 json_object_int_add(json_vni
, "numMacs", num_macs
);
731 json_object_int_add(json_vni
, "numArpNd", num_neigh
);
732 json_object_int_add(json_vni
, "numRemoteVteps", num_vteps
);
734 json_vtep_list
= json_object_new_array();
735 for (zvtep
= zvni
->vteps
; zvtep
; zvtep
= zvtep
->next
) {
736 json_ip_str
= json_object_new_string(
737 inet_ntoa(zvtep
->vtep_ip
));
738 json_object_array_add(json_vtep_list
,
741 json_object_object_add(json_vni
, "remoteVteps",
744 json_object_object_add(json
, vni_str
, json_vni
);
749 * Inform BGP about local MACIP.
751 static int zvni_macip_send_msg_to_client(vni_t vni
,
752 struct ethaddr
*macaddr
,
753 struct ipaddr
*ip
, u_char flags
,
756 struct zserv
*client
;
759 char buf
[ETHER_ADDR_STRLEN
];
760 char buf2
[INET6_ADDRSTRLEN
];
762 client
= zebra_find_client(ZEBRA_ROUTE_BGP
);
763 /* BGP may not be running. */
770 zserv_create_header(s
, cmd
, VRF_DEFAULT
);
772 stream_put(s
, macaddr
->octet
, ETH_ALEN
);
775 if (IS_IPADDR_V4(ip
))
776 ipa_len
= IPV4_MAX_BYTELEN
;
777 else if (IS_IPADDR_V6(ip
))
778 ipa_len
= IPV6_MAX_BYTELEN
;
780 stream_putl(s
, ipa_len
); /* IP address length */
782 stream_put(s
, &ip
->ip
.addr
, ipa_len
); /* IP address */
784 stream_putl(s
, 0); /* Just MAC. */
786 stream_putc(s
, flags
); /* sticky mac/gateway mac */
788 /* Write packet size. */
789 stream_putw_at(s
, 0, stream_get_endp(s
));
791 if (IS_ZEBRA_DEBUG_VXLAN
)
793 "Send MACIP %s flags 0x%x MAC %s IP %s VNI %u to %s",
794 (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
795 flags
, prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
796 ipaddr2str(ip
, buf2
, sizeof(buf2
)), vni
,
797 zebra_route_string(client
->proto
));
799 if (cmd
== ZEBRA_MACIP_ADD
)
800 client
->macipadd_cnt
++;
802 client
->macipdel_cnt
++;
804 return zebra_server_send_message(client
);
808 * Make hash key for neighbors.
810 static unsigned int neigh_hash_keymake(void *p
)
812 zebra_neigh_t
*n
= p
;
813 struct ipaddr
*ip
= &n
->ip
;
815 if (IS_IPADDR_V4(ip
))
816 return jhash_1word(ip
->ipaddr_v4
.s_addr
, 0);
818 return jhash2(ip
->ipaddr_v6
.s6_addr32
,
819 ZEBRA_NUM_OF(ip
->ipaddr_v6
.s6_addr32
), 0);
823 * Compare two neighbor hash structures.
825 static int neigh_cmp(const void *p1
, const void *p2
)
827 const zebra_neigh_t
*n1
= p1
;
828 const zebra_neigh_t
*n2
= p2
;
830 if (n1
== NULL
&& n2
== NULL
)
833 if (n1
== NULL
|| n2
== NULL
)
836 return (memcmp(&n1
->ip
, &n2
->ip
, sizeof(struct ipaddr
)) == 0);
840 * Callback to allocate neighbor hash entry.
842 static void *zvni_neigh_alloc(void *p
)
844 const zebra_neigh_t
*tmp_n
= p
;
847 n
= XCALLOC(MTYPE_NEIGH
, sizeof(zebra_neigh_t
));
854 * Add neighbor entry.
856 static zebra_neigh_t
*zvni_neigh_add(zebra_vni_t
*zvni
, struct ipaddr
*ip
,
860 zebra_neigh_t
*n
= NULL
;
861 zebra_mac_t
*zmac
= NULL
;
863 memset(&tmp_n
, 0, sizeof(zebra_neigh_t
));
864 memcpy(&tmp_n
.ip
, ip
, sizeof(struct ipaddr
));
865 n
= hash_get(zvni
->neigh_table
, &tmp_n
, zvni_neigh_alloc
);
868 memcpy(&n
->emac
, mac
, ETH_ALEN
);
869 n
->state
= ZEBRA_NEIGH_INACTIVE
;
871 /* Associate the neigh to mac */
872 zmac
= zvni_mac_lookup(zvni
, mac
);
874 listnode_add_sort(zmac
->neigh_list
, n
);
880 * Delete neighbor entry.
882 static int zvni_neigh_del(zebra_vni_t
*zvni
, zebra_neigh_t
*n
)
884 zebra_neigh_t
*tmp_n
;
885 zebra_mac_t
*zmac
= NULL
;
887 zmac
= zvni_mac_lookup(zvni
, &n
->emac
);
889 listnode_delete(zmac
->neigh_list
, n
);
891 /* Free the VNI hash entry and allocated memory. */
892 tmp_n
= hash_release(zvni
->neigh_table
, n
);
894 XFREE(MTYPE_NEIGH
, tmp_n
);
900 * Free neighbor hash entry (callback)
902 static int zvni_neigh_del_hash_entry(struct hash_backet
*backet
, void *arg
)
904 struct neigh_walk_ctx
*wctx
= arg
;
905 zebra_neigh_t
*n
= backet
->data
;
907 if (((wctx
->flags
& DEL_LOCAL_NEIGH
) && (n
->flags
& ZEBRA_NEIGH_LOCAL
))
908 || ((wctx
->flags
& DEL_REMOTE_NEIGH
)
909 && (n
->flags
& ZEBRA_NEIGH_REMOTE
))
910 || ((wctx
->flags
& DEL_REMOTE_NEIGH_FROM_VTEP
)
911 && (n
->flags
& ZEBRA_NEIGH_REMOTE
)
912 && IPV4_ADDR_SAME(&n
->r_vtep_ip
, &wctx
->r_vtep_ip
))) {
913 if (wctx
->upd_client
&& (n
->flags
& ZEBRA_NEIGH_LOCAL
))
914 zvni_neigh_send_del_to_client(wctx
->zvni
->vni
, &n
->ip
,
918 zvni_neigh_uninstall(wctx
->zvni
, n
);
920 return zvni_neigh_del(wctx
->zvni
, n
);
927 * Delete all neighbor entries from specific VTEP for a particular VNI.
929 static void zvni_neigh_del_from_vtep(zebra_vni_t
*zvni
, int uninstall
,
930 struct in_addr
*r_vtep_ip
)
932 struct neigh_walk_ctx wctx
;
934 if (!zvni
->neigh_table
)
937 memset(&wctx
, 0, sizeof(struct neigh_walk_ctx
));
939 wctx
.uninstall
= uninstall
;
940 wctx
.flags
= DEL_REMOTE_NEIGH_FROM_VTEP
;
941 wctx
.r_vtep_ip
= *r_vtep_ip
;
943 hash_iterate(zvni
->neigh_table
,
944 (void (*)(struct hash_backet
*,
945 void *))zvni_neigh_del_hash_entry
,
950 * Delete all neighbor entries for this VNI.
952 static void zvni_neigh_del_all(zebra_vni_t
*zvni
,
953 int uninstall
, int upd_client
, u_int32_t flags
)
955 struct neigh_walk_ctx wctx
;
957 if (!zvni
->neigh_table
)
960 memset(&wctx
, 0, sizeof(struct neigh_walk_ctx
));
962 wctx
.uninstall
= uninstall
;
963 wctx
.upd_client
= upd_client
;
966 hash_iterate(zvni
->neigh_table
,
967 (void (*)(struct hash_backet
*,
968 void *))zvni_neigh_del_hash_entry
,
973 * Look up neighbor hash entry.
975 static zebra_neigh_t
*zvni_neigh_lookup(zebra_vni_t
*zvni
, struct ipaddr
*ip
)
980 memset(&tmp
, 0, sizeof(tmp
));
981 memcpy(&tmp
.ip
, ip
, sizeof(struct ipaddr
));
982 n
= hash_lookup(zvni
->neigh_table
, &tmp
);
987 /* Process all neigh associated to a mac upon local mac add event */
988 static void zvni_process_neigh_on_local_mac_add(zebra_vni_t
*zvni
,
991 zebra_neigh_t
*n
= NULL
;
992 struct listnode
*node
= NULL
;
993 char buf
[ETHER_ADDR_STRLEN
];
994 char buf2
[INET6_ADDRSTRLEN
];
996 for (ALL_LIST_ELEMENTS_RO(zmac
->neigh_list
, node
, n
)) {
997 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
)) {
998 /* MAC is learnt locally, program all inactive neigh
999 * pointing to this mac */
1000 if (IS_ZEBRA_NEIGH_INACTIVE(n
)) {
1001 if (IS_ZEBRA_DEBUG_VXLAN
)
1003 "neigh %s (MAC %s) on VNI %u is now ACTIVE",
1004 ipaddr2str(&n
->ip
, buf2
,
1006 prefix_mac2str(&n
->emac
, buf
,
1010 ZEBRA_NEIGH_SET_ACTIVE(n
);
1011 zvni_neigh_send_add_to_client(
1012 zvni
->vni
, &n
->ip
, &n
->emac
, 0);
1014 if (IS_ZEBRA_DEBUG_VXLAN
)
1016 "neigh %s (MAC %s) on VNI %u should NOT be ACTIVE",
1017 ipaddr2str(&n
->ip
, buf2
,
1019 prefix_mac2str(&n
->emac
, buf
,
1023 } else if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
)) {
1024 /* TODO: assume the neigh has moved too ?? */
1029 /* Process all neigh associated to a mac upon local mac del event */
1030 static void zvni_process_neigh_on_local_mac_del(zebra_vni_t
*zvni
,
1033 zebra_neigh_t
*n
= NULL
;
1034 struct listnode
*node
= NULL
;
1035 char buf
[ETHER_ADDR_STRLEN
];
1036 char buf2
[INET6_ADDRSTRLEN
];
1038 for (ALL_LIST_ELEMENTS_RO(zmac
->neigh_list
, node
, n
)) {
1039 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
)) {
1040 if (IS_ZEBRA_NEIGH_ACTIVE(n
)) {
1041 if (IS_ZEBRA_DEBUG_VXLAN
)
1043 "neigh %s (MAC %s) on VNI %u is now INACTIVE",
1044 ipaddr2str(&n
->ip
, buf2
,
1046 prefix_mac2str(&n
->emac
, buf
,
1050 ZEBRA_NEIGH_SET_INACTIVE(n
);
1051 zvni_neigh_send_del_to_client(
1052 zvni
->vni
, &n
->ip
, &n
->emac
, 0);
1054 } else if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
)) {
1055 if (IS_ZEBRA_DEBUG_VXLAN
)
1057 "local MAC %s getting deleted on VNI %u has remote neigh %s",
1058 prefix_mac2str(&n
->emac
, buf
,
1061 ipaddr2str(&n
->ip
, buf2
, sizeof(buf2
)));
1066 /* process all neigh associated to a mac entry upon remote mac add */
1067 static void zvni_process_neigh_on_remote_mac_add(zebra_vni_t
*zvni
,
1070 zebra_neigh_t
*n
= NULL
;
1071 struct listnode
*node
= NULL
;
1072 char buf
[ETHER_ADDR_STRLEN
];
1073 char buf2
[INET6_ADDRSTRLEN
];
1075 for (ALL_LIST_ELEMENTS_RO(zmac
->neigh_list
, node
, n
)) {
1076 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
)) {
1077 if (IS_ZEBRA_NEIGH_ACTIVE(n
)) {
1078 if (IS_ZEBRA_DEBUG_VXLAN
)
1080 "neigh %s (MAC %s) on VNI %u INACTIVE",
1081 ipaddr2str(&n
->ip
, buf2
,
1083 prefix_mac2str(&n
->emac
, buf
,
1087 ZEBRA_NEIGH_SET_INACTIVE(n
);
1088 zvni_neigh_send_del_to_client(
1089 zvni
->vni
, &n
->ip
, &n
->emac
, 0);
1095 /* process all neigh associated to mac entry upon remote mac del */
1096 static void zvni_process_neigh_on_remote_mac_del(zebra_vni_t
*zvni
,
1099 zebra_neigh_t
*n
= NULL
;
1100 struct listnode
*node
= NULL
;
1101 char buf
[ETHER_ADDR_STRLEN
];
1102 char buf2
[INET6_ADDRSTRLEN
];
1104 for (ALL_LIST_ELEMENTS_RO(zmac
->neigh_list
, node
, n
)) {
1105 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
)) {
1106 if (IS_ZEBRA_DEBUG_VXLAN
)
1108 "remote MAC %s getting deleted on VNI %u has local neigh %s",
1109 prefix_mac2str(&n
->emac
, buf
,
1112 ipaddr2str(&n
->ip
, buf2
, sizeof(buf2
)));
1118 * Inform BGP about local neighbor addition.
1120 static int zvni_neigh_send_add_to_client(vni_t vni
,
1122 struct ethaddr
*macaddr
, u_char flags
)
1124 return zvni_macip_send_msg_to_client(vni
, macaddr
, ip
, flags
,
1129 * Inform BGP about local neighbor deletion.
1131 static int zvni_neigh_send_del_to_client(vni_t vni
,
1133 struct ethaddr
*macaddr
, u_char flags
)
1135 return zvni_macip_send_msg_to_client(vni
, macaddr
, ip
, flags
,
1140 * Install remote neighbor into the kernel.
1142 static int zvni_neigh_install(zebra_vni_t
*zvni
, zebra_neigh_t
*n
)
1144 struct zebra_if
*zif
;
1145 struct zebra_l2info_vxlan
*vxl
;
1146 struct interface
*vlan_if
;
1148 if (!(n
->flags
& ZEBRA_NEIGH_REMOTE
))
1151 zif
= zvni
->vxlan_if
->info
;
1154 vxl
= &zif
->l2info
.vxl
;
1156 vlan_if
= zvni_map_to_svi(vxl
->access_vlan
, zif
->brslave_info
.br_if
);
1160 return kernel_add_neigh(vlan_if
, &n
->ip
, &n
->emac
);
1164 * Uninstall remote neighbor from the kernel.
1166 static int zvni_neigh_uninstall(zebra_vni_t
*zvni
, zebra_neigh_t
*n
)
1168 struct zebra_if
*zif
;
1169 struct zebra_l2info_vxlan
*vxl
;
1170 struct interface
*vlan_if
;
1172 if (!(n
->flags
& ZEBRA_NEIGH_REMOTE
))
1175 if (!zvni
->vxlan_if
) {
1176 zlog_err("VNI %u hash %p couldn't be uninstalled - no intf",
1181 zif
= zvni
->vxlan_if
->info
;
1184 vxl
= &zif
->l2info
.vxl
;
1185 vlan_if
= zvni_map_to_svi(vxl
->access_vlan
, zif
->brslave_info
.br_if
);
1189 return kernel_del_neigh(vlan_if
, &n
->ip
);
1193 * Install neighbor hash entry - called upon access VLAN change.
1195 static void zvni_install_neigh_hash(struct hash_backet
*backet
, void *ctxt
)
1198 struct neigh_walk_ctx
*wctx
= ctxt
;
1200 n
= (zebra_neigh_t
*)backet
->data
;
1204 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
))
1205 zvni_neigh_install(wctx
->zvni
, n
);
1208 /* Get the VRR interface for SVI if any */
1209 struct interface
*zebra_get_vrr_intf_for_svi(struct interface
*ifp
)
1211 struct zebra_vrf
*zvrf
= NULL
;
1212 struct interface
*tmp_if
= NULL
;
1213 struct zebra_if
*zif
= NULL
;
1214 struct listnode
*node
;
1216 zvrf
= vrf_info_lookup(ifp
->vrf_id
);
1219 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(zvrf_id(zvrf
)), node
, tmp_if
)) {
1224 if (!IS_ZEBRA_IF_MACVLAN(tmp_if
))
1227 if (zif
->link
== ifp
)
1234 static int zvni_del_macip_for_intf(struct interface
*ifp
, zebra_vni_t
*zvni
)
1236 struct listnode
*cnode
= NULL
, *cnnode
= NULL
;
1237 struct connected
*c
= NULL
;
1238 struct ethaddr macaddr
;
1240 memcpy(&macaddr
.octet
, ifp
->hw_addr
, ETH_ALEN
);
1242 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
1245 memset(&ip
, 0, sizeof(struct ipaddr
));
1246 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
1249 if (c
->address
->family
== AF_INET
) {
1250 ip
.ipa_type
= IPADDR_V4
;
1251 memcpy(&(ip
.ipaddr_v4
), &(c
->address
->u
.prefix4
),
1252 sizeof(struct in_addr
));
1253 } else if (c
->address
->family
== AF_INET6
) {
1254 ip
.ipa_type
= IPADDR_V6
;
1255 memcpy(&(ip
.ipaddr_v6
), &(c
->address
->u
.prefix6
),
1256 sizeof(struct in6_addr
));
1261 zvni_gw_macip_del(ifp
, zvni
, &ip
);
1267 static int zvni_add_macip_for_intf(struct interface
*ifp
, zebra_vni_t
*zvni
)
1269 struct listnode
*cnode
= NULL
, *cnnode
= NULL
;
1270 struct connected
*c
= NULL
;
1271 struct ethaddr macaddr
;
1273 memcpy(&macaddr
.octet
, ifp
->hw_addr
, ETH_ALEN
);
1275 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
1278 memset(&ip
, 0, sizeof(struct ipaddr
));
1279 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
1282 if (c
->address
->family
== AF_INET
) {
1283 ip
.ipa_type
= IPADDR_V4
;
1284 memcpy(&(ip
.ipaddr_v4
), &(c
->address
->u
.prefix4
),
1285 sizeof(struct in_addr
));
1286 } else if (c
->address
->family
== AF_INET6
) {
1287 ip
.ipa_type
= IPADDR_V6
;
1288 memcpy(&(ip
.ipaddr_v6
), &(c
->address
->u
.prefix6
),
1289 sizeof(struct in6_addr
));
1294 zvni_gw_macip_add(ifp
, zvni
, &macaddr
, &ip
);
1301 * zvni_gw_macip_add_to_client
1303 static int zvni_gw_macip_add(struct interface
*ifp
, zebra_vni_t
*zvni
,
1304 struct ethaddr
*macaddr
, struct ipaddr
*ip
)
1306 struct zebra_if
*zif
= NULL
;
1307 struct zebra_l2info_vxlan
*vxl
= NULL
;
1308 zebra_neigh_t
*n
= NULL
;
1309 zebra_mac_t
*mac
= NULL
;
1310 char buf
[ETHER_ADDR_STRLEN
];
1311 char buf2
[INET6_ADDRSTRLEN
];
1313 zif
= zvni
->vxlan_if
->info
;
1317 vxl
= &zif
->l2info
.vxl
;
1319 mac
= zvni_mac_lookup(zvni
, macaddr
);
1321 mac
= zvni_mac_add(zvni
, macaddr
);
1323 zlog_err("Failed to add MAC %s intf %s(%u) VID %u",
1324 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
1325 ifp
->name
, ifp
->ifindex
, vxl
->access_vlan
);
1330 /* Set "local" forwarding info. */
1331 SET_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
);
1332 SET_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
);
1333 memset(&mac
->fwd_info
, 0, sizeof(mac
->fwd_info
));
1334 mac
->fwd_info
.local
.ifindex
= ifp
->ifindex
;
1335 mac
->fwd_info
.local
.vid
= vxl
->access_vlan
;
1337 n
= zvni_neigh_lookup(zvni
, ip
);
1339 n
= zvni_neigh_add(zvni
, ip
, macaddr
);
1342 "Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
1343 ipaddr2str(ip
, buf2
, sizeof(buf2
)),
1344 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
1345 ifp
->name
, ifp
->ifindex
, zvni
->vni
);
1350 /* Set "local" forwarding info. */
1351 SET_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
);
1352 memcpy(&n
->emac
, macaddr
, ETH_ALEN
);
1353 n
->ifindex
= ifp
->ifindex
;
1355 if (IS_ZEBRA_DEBUG_VXLAN
)
1357 "SVI %s(%u) VNI %u, sending GW MAC %s IP %s add to BGP",
1358 ifp
->name
, ifp
->ifindex
, zvni
->vni
,
1359 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
1360 ipaddr2str(ip
, buf2
, sizeof(buf2
)));
1362 zvni_neigh_send_add_to_client(zvni
->vni
, ip
, macaddr
,
1369 * zvni_gw_macip_del_from_client
1371 static int zvni_gw_macip_del(struct interface
*ifp
, zebra_vni_t
*zvni
,
1374 zebra_neigh_t
*n
= NULL
;
1375 zebra_mac_t
*mac
= NULL
;
1376 char buf1
[ETHER_ADDR_STRLEN
];
1377 char buf2
[INET6_ADDRSTRLEN
];
1379 /* If the neigh entry is not present nothing to do*/
1380 n
= zvni_neigh_lookup(zvni
, ip
);
1384 /* mac entry should be present */
1385 mac
= zvni_mac_lookup(zvni
, &n
->emac
);
1387 zlog_err("MAC %s doesnt exists for neigh %s on VNI %u",
1388 prefix_mac2str(&n
->emac
, buf1
, sizeof(buf1
)),
1389 ipaddr2str(ip
, buf2
, sizeof(buf2
)), zvni
->vni
);
1393 /* If the entry is not local nothing to do*/
1394 if (!CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
))
1397 if (IS_ZEBRA_DEBUG_VXLAN
)
1399 "SVI %s(%u) VNI %u, sending GW MAC %s IP %s del to BGP",
1400 ifp
->name
, ifp
->ifindex
, zvni
->vni
,
1401 prefix_mac2str(&(n
->emac
), buf1
, sizeof(buf1
)),
1402 ipaddr2str(ip
, buf2
, sizeof(buf2
)));
1404 /* Remove neighbor from BGP. */
1405 zvni_neigh_send_del_to_client(zvni
->vni
, &n
->ip
, &n
->emac
,
1408 /* Delete this neighbor entry. */
1409 zvni_neigh_del(zvni
, n
);
1411 /* see if the mac needs to be deleted as well*/
1413 zvni_deref_ip2mac(zvni
, mac
, 0);
1418 static void zvni_gw_macip_del_for_vni_hash(struct hash_backet
*backet
,
1421 zebra_vni_t
*zvni
= NULL
;
1422 struct zebra_if
*zif
= NULL
;
1423 struct zebra_l2info_vxlan zl2_info
;
1424 struct interface
*vlan_if
= NULL
;
1425 struct interface
*vrr_if
= NULL
;
1426 struct interface
*ifp
;
1428 /* Add primary SVI MAC*/
1429 zvni
= (zebra_vni_t
*)backet
->data
;
1433 ifp
= zvni
->vxlan_if
;
1438 /* If down or not mapped to a bridge, we're done. */
1439 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
1442 zl2_info
= zif
->l2info
.vxl
;
1444 vlan_if
= zvni_map_to_svi(zl2_info
.access_vlan
, zif
->brslave_info
.br_if
);
1448 /* Del primary MAC-IP */
1449 zvni_del_macip_for_intf(vlan_if
, zvni
);
1451 /* Del VRR MAC-IP - if any*/
1452 vrr_if
= zebra_get_vrr_intf_for_svi(vlan_if
);
1454 zvni_del_macip_for_intf(vrr_if
, zvni
);
1459 static void zvni_gw_macip_add_for_vni_hash(struct hash_backet
*backet
,
1462 zebra_vni_t
*zvni
= NULL
;
1463 struct zebra_if
*zif
= NULL
;
1464 struct zebra_l2info_vxlan zl2_info
;
1465 struct interface
*vlan_if
= NULL
;
1466 struct interface
*vrr_if
= NULL
;
1467 struct interface
*ifp
= NULL
;
1469 zvni
= (zebra_vni_t
*)backet
->data
;
1473 if (!advertise_gw_macip_enabled(zvni
))
1476 ifp
= zvni
->vxlan_if
;
1481 /* If down or not mapped to a bridge, we're done. */
1482 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
1484 zl2_info
= zif
->l2info
.vxl
;
1486 vlan_if
= zvni_map_to_svi(zl2_info
.access_vlan
,
1487 zif
->brslave_info
.br_if
);
1491 /* Add primary SVI MAC-IP */
1492 zvni_add_macip_for_intf(vlan_if
, zvni
);
1494 /* Add VRR MAC-IP - if any*/
1495 vrr_if
= zebra_get_vrr_intf_for_svi(vlan_if
);
1497 zvni_add_macip_for_intf(vrr_if
, zvni
);
1503 * Make hash key for MAC.
1505 static unsigned int mac_hash_keymake(void *p
)
1507 zebra_mac_t
*pmac
= p
;
1508 const void *pnt
= (void *)pmac
->macaddr
.octet
;
1510 return jhash(pnt
, ETH_ALEN
, 0xa5a5a55a);
1514 * Compare two MAC addresses.
1516 static int mac_cmp(const void *p1
, const void *p2
)
1518 const zebra_mac_t
*pmac1
= p1
;
1519 const zebra_mac_t
*pmac2
= p2
;
1521 if (pmac1
== NULL
&& pmac2
== NULL
)
1524 if (pmac1
== NULL
|| pmac2
== NULL
)
1527 return (memcmp(pmac1
->macaddr
.octet
, pmac2
->macaddr
.octet
,
1533 * Callback to allocate MAC hash entry.
1535 static void *zvni_mac_alloc(void *p
)
1537 const zebra_mac_t
*tmp_mac
= p
;
1540 mac
= XCALLOC(MTYPE_MAC
, sizeof(zebra_mac_t
));
1543 return ((void *)mac
);
1549 static zebra_mac_t
*zvni_mac_add(zebra_vni_t
*zvni
, struct ethaddr
*macaddr
)
1551 zebra_mac_t tmp_mac
;
1552 zebra_mac_t
*mac
= NULL
;
1554 memset(&tmp_mac
, 0, sizeof(zebra_mac_t
));
1555 memcpy(&tmp_mac
.macaddr
, macaddr
, ETH_ALEN
);
1556 mac
= hash_get(zvni
->mac_table
, &tmp_mac
, zvni_mac_alloc
);
1559 mac
->neigh_list
= list_new();
1560 mac
->neigh_list
->cmp
= (int (*)(void *, void *))neigh_cmp
;
1568 static int zvni_mac_del(zebra_vni_t
*zvni
, zebra_mac_t
*mac
)
1570 zebra_mac_t
*tmp_mac
;
1572 list_delete_and_null(&mac
->neigh_list
);
1574 /* Free the VNI hash entry and allocated memory. */
1575 tmp_mac
= hash_release(zvni
->mac_table
, mac
);
1577 XFREE(MTYPE_MAC
, tmp_mac
);
1583 * Free MAC hash entry (callback)
1585 static int zvni_mac_del_hash_entry(struct hash_backet
*backet
, void *arg
)
1587 struct mac_walk_ctx
*wctx
= arg
;
1588 zebra_mac_t
*mac
= backet
->data
;
1591 if (((wctx
->flags
& DEL_LOCAL_MAC
) && (mac
->flags
& ZEBRA_MAC_LOCAL
))
1592 || ((wctx
->flags
& DEL_REMOTE_MAC
)
1593 && (mac
->flags
& ZEBRA_MAC_REMOTE
))
1594 || ((wctx
->flags
& DEL_REMOTE_MAC_FROM_VTEP
)
1595 && (mac
->flags
& ZEBRA_MAC_REMOTE
)
1596 && IPV4_ADDR_SAME(&mac
->fwd_info
.r_vtep_ip
,
1597 &wctx
->r_vtep_ip
))) {
1598 if (wctx
->upd_client
&& (mac
->flags
& ZEBRA_MAC_LOCAL
)) {
1599 sticky
= CHECK_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
) ? 1
1601 zvni_mac_send_del_to_client(
1602 wctx
->zvni
->vni
, &mac
->macaddr
,
1603 (sticky
? ZEBRA_MAC_TYPE_STICKY
: 0));
1606 if (wctx
->uninstall
)
1607 zvni_mac_uninstall(wctx
->zvni
, mac
, 0);
1609 return zvni_mac_del(wctx
->zvni
, mac
);
1616 * Delete all MAC entries from specific VTEP for a particular VNI.
1618 static void zvni_mac_del_from_vtep(zebra_vni_t
*zvni
, int uninstall
,
1619 struct in_addr
*r_vtep_ip
)
1621 struct mac_walk_ctx wctx
;
1623 if (!zvni
->mac_table
)
1626 memset(&wctx
, 0, sizeof(struct mac_walk_ctx
));
1628 wctx
.uninstall
= uninstall
;
1629 wctx
.flags
= DEL_REMOTE_MAC_FROM_VTEP
;
1630 wctx
.r_vtep_ip
= *r_vtep_ip
;
1632 hash_iterate(zvni
->mac_table
, (void (*)(struct hash_backet
*,
1633 void *))zvni_mac_del_hash_entry
,
1638 * Delete all MAC entries for this VNI.
1640 static void zvni_mac_del_all(zebra_vni_t
*zvni
,
1641 int uninstall
, int upd_client
, u_int32_t flags
)
1643 struct mac_walk_ctx wctx
;
1645 if (!zvni
->mac_table
)
1648 memset(&wctx
, 0, sizeof(struct mac_walk_ctx
));
1650 wctx
.uninstall
= uninstall
;
1651 wctx
.upd_client
= upd_client
;
1654 hash_iterate(zvni
->mac_table
, (void (*)(struct hash_backet
*,
1655 void *))zvni_mac_del_hash_entry
,
1660 * Look up MAC hash entry.
1662 static zebra_mac_t
*zvni_mac_lookup(zebra_vni_t
*zvni
, struct ethaddr
*mac
)
1667 memset(&tmp
, 0, sizeof(tmp
));
1668 memcpy(&tmp
.macaddr
, mac
, ETH_ALEN
);
1669 pmac
= hash_lookup(zvni
->mac_table
, &tmp
);
1675 * Inform BGP about local MAC addition.
1677 static int zvni_mac_send_add_to_client(vni_t vni
,
1678 struct ethaddr
*macaddr
, u_char flags
)
1680 return zvni_macip_send_msg_to_client(vni
, macaddr
, NULL
, flags
,
1685 * Inform BGP about local MAC deletion.
1687 static int zvni_mac_send_del_to_client(vni_t vni
,
1688 struct ethaddr
*macaddr
, u_char flags
)
1690 return zvni_macip_send_msg_to_client(vni
, macaddr
, NULL
, flags
,
1695 * Map port or (port, VLAN) to a VNI. This is invoked upon getting MAC
1696 * notifications, to see if they are of interest.
1698 static zebra_vni_t
*zvni_map_vlan(struct interface
*ifp
,
1699 struct interface
*br_if
, vlanid_t vid
)
1701 struct zebra_ns
*zns
;
1702 struct route_node
*rn
;
1703 struct interface
*tmp_if
= NULL
;
1704 struct zebra_if
*zif
;
1705 struct zebra_l2info_bridge
*br
;
1706 struct zebra_l2info_vxlan
*vxl
= NULL
;
1707 u_char bridge_vlan_aware
;
1711 /* Determine if bridge is VLAN-aware or not */
1714 br
= &zif
->l2info
.br
;
1715 bridge_vlan_aware
= br
->vlan_aware
;
1717 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
1718 /* TODO: Optimize with a hash. */
1719 zns
= zebra_ns_lookup(NS_DEFAULT
);
1720 for (rn
= route_top(zns
->if_table
); rn
; rn
= route_next(rn
)) {
1721 tmp_if
= (struct interface
*)rn
->info
;
1725 if (!zif
|| zif
->zif_type
!= ZEBRA_IF_VXLAN
)
1727 if (!if_is_operative(tmp_if
))
1729 vxl
= &zif
->l2info
.vxl
;
1731 if (zif
->brslave_info
.br_if
!= br_if
)
1734 if (!bridge_vlan_aware
|| vxl
->access_vlan
== vid
) {
1743 zvni
= zvni_lookup(vxl
->vni
);
1748 * Map SVI and associated bridge to a VNI. This is invoked upon getting
1749 * neighbor notifications, to see if they are of interest.
1751 static zebra_vni_t
*zvni_map_svi(struct interface
*ifp
, struct interface
*br_if
)
1753 struct zebra_ns
*zns
;
1754 struct route_node
*rn
;
1755 struct interface
*tmp_if
= NULL
;
1756 struct zebra_if
*zif
;
1757 struct zebra_l2info_bridge
*br
;
1758 struct zebra_l2info_vxlan
*vxl
= NULL
;
1759 u_char bridge_vlan_aware
;
1767 /* Make sure the linked interface is a bridge. */
1768 if (!IS_ZEBRA_IF_BRIDGE(br_if
))
1771 /* Determine if bridge is VLAN-aware or not */
1774 br
= &zif
->l2info
.br
;
1775 bridge_vlan_aware
= br
->vlan_aware
;
1776 if (bridge_vlan_aware
) {
1777 struct zebra_l2info_vlan
*vl
;
1779 if (!IS_ZEBRA_IF_VLAN(ifp
))
1784 vl
= &zif
->l2info
.vl
;
1788 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
1789 /* TODO: Optimize with a hash. */
1790 zns
= zebra_ns_lookup(NS_DEFAULT
);
1791 for (rn
= route_top(zns
->if_table
); rn
; rn
= route_next(rn
)) {
1792 tmp_if
= (struct interface
*)rn
->info
;
1796 if (!zif
|| zif
->zif_type
!= ZEBRA_IF_VXLAN
)
1798 if (!if_is_operative(tmp_if
))
1800 vxl
= &zif
->l2info
.vxl
;
1802 if (zif
->brslave_info
.br_if
!= br_if
)
1805 if (!bridge_vlan_aware
|| vxl
->access_vlan
== vid
) {
1814 zvni
= zvni_lookup(vxl
->vni
);
1818 /* Map to SVI on bridge corresponding to specified VLAN. This can be one
1820 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN interface
1821 * linked to the bridge
1822 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge inteface
1825 static struct interface
*zvni_map_to_svi(vlanid_t vid
, struct interface
*br_if
)
1827 struct zebra_ns
*zns
;
1828 struct route_node
*rn
;
1829 struct interface
*tmp_if
= NULL
;
1830 struct zebra_if
*zif
;
1831 struct zebra_l2info_bridge
*br
;
1832 struct zebra_l2info_vlan
*vl
;
1833 u_char bridge_vlan_aware
;
1836 /* Defensive check, caller expected to invoke only with valid bridge. */
1840 /* Determine if bridge is VLAN-aware or not */
1843 br
= &zif
->l2info
.br
;
1844 bridge_vlan_aware
= br
->vlan_aware
;
1846 /* Check oper status of the SVI. */
1847 if (!bridge_vlan_aware
)
1848 return if_is_operative(br_if
) ? br_if
: NULL
;
1850 /* Identify corresponding VLAN interface. */
1851 /* TODO: Optimize with a hash. */
1852 zns
= zebra_ns_lookup(NS_DEFAULT
);
1853 for (rn
= route_top(zns
->if_table
); rn
; rn
= route_next(rn
)) {
1854 tmp_if
= (struct interface
*)rn
->info
;
1855 /* Check oper status of the SVI. */
1856 if (!tmp_if
|| !if_is_operative(tmp_if
))
1859 if (!zif
|| zif
->zif_type
!= ZEBRA_IF_VLAN
1860 || zif
->link
!= br_if
)
1862 vl
= (struct zebra_l2info_vlan
*)&zif
->l2info
.vl
;
1864 if (vl
->vid
== vid
) {
1870 return found
? tmp_if
: NULL
;
1874 * Install remote MAC into the kernel.
1876 static int zvni_mac_install(zebra_vni_t
*zvni
, zebra_mac_t
*mac
)
1878 struct zebra_if
*zif
;
1879 struct zebra_l2info_vxlan
*vxl
;
1882 if (!(mac
->flags
& ZEBRA_MAC_REMOTE
))
1885 zif
= zvni
->vxlan_if
->info
;
1888 vxl
= &zif
->l2info
.vxl
;
1890 sticky
= CHECK_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
) ? 1 : 0;
1892 return kernel_add_mac(zvni
->vxlan_if
, vxl
->access_vlan
, &mac
->macaddr
,
1893 mac
->fwd_info
.r_vtep_ip
, sticky
);
1897 * Uninstall remote MAC from the kernel. In the scenario where the MAC
1898 * moves to remote, we have to uninstall any existing local entry first.
1900 static int zvni_mac_uninstall(zebra_vni_t
*zvni
, zebra_mac_t
*mac
, int local
)
1902 struct zebra_if
*zif
;
1903 struct zebra_l2info_vxlan
*vxl
;
1904 struct in_addr vtep_ip
= {.s_addr
= 0};
1905 struct zebra_ns
*zns
;
1906 struct interface
*ifp
;
1908 if (!local
&& !(mac
->flags
& ZEBRA_MAC_REMOTE
))
1911 if (!zvni
->vxlan_if
) {
1912 zlog_err("VNI %u hash %p couldn't be uninstalled - no intf",
1917 zif
= zvni
->vxlan_if
->info
;
1920 vxl
= &zif
->l2info
.vxl
;
1923 zns
= zebra_ns_lookup(NS_DEFAULT
);
1924 ifp
= if_lookup_by_index_per_ns(zns
,
1925 mac
->fwd_info
.local
.ifindex
);
1926 if (!ifp
) // unexpected
1929 ifp
= zvni
->vxlan_if
;
1930 vtep_ip
= mac
->fwd_info
.r_vtep_ip
;
1933 return kernel_del_mac(ifp
, vxl
->access_vlan
, &mac
->macaddr
, vtep_ip
,
1938 * Install MAC hash entry - called upon access VLAN change.
1940 static void zvni_install_mac_hash(struct hash_backet
*backet
, void *ctxt
)
1943 struct mac_walk_ctx
*wctx
= ctxt
;
1945 mac
= (zebra_mac_t
*)backet
->data
;
1949 if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
))
1950 zvni_mac_install(wctx
->zvni
, mac
);
1954 * Decrement neighbor refcount of MAC; uninstall and free it if
1957 static void zvni_deref_ip2mac(zebra_vni_t
*zvni
, zebra_mac_t
*mac
,
1960 if (!CHECK_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
)
1961 || !list_isempty(mac
->neigh_list
))
1965 zvni_mac_uninstall(zvni
, mac
, 0);
1967 zvni_mac_del(zvni
, mac
);
1971 * Read and populate local MACs and neighbors corresponding to this VNI.
1973 static void zvni_read_mac_neigh(zebra_vni_t
*zvni
,
1974 struct interface
*ifp
)
1976 struct zebra_ns
*zns
;
1977 struct zebra_if
*zif
;
1978 struct interface
*vlan_if
;
1979 struct zebra_l2info_vxlan
*vxl
;
1980 struct interface
*vrr_if
;
1983 vxl
= &zif
->l2info
.vxl
;
1984 zns
= zebra_ns_lookup(NS_DEFAULT
);
1986 if (IS_ZEBRA_DEBUG_VXLAN
)
1988 "Reading MAC FDB and Neighbors for intf %s(%u) VNI %u master %u",
1989 ifp
->name
, ifp
->ifindex
, zvni
->vni
,
1990 zif
->brslave_info
.bridge_ifindex
);
1992 macfdb_read_for_bridge(zns
, ifp
, zif
->brslave_info
.br_if
);
1993 vlan_if
= zvni_map_to_svi(vxl
->access_vlan
, zif
->brslave_info
.br_if
);
1996 if (advertise_gw_macip_enabled(zvni
)) {
1997 /* Add SVI MAC-IP */
1998 zvni_add_macip_for_intf(vlan_if
, zvni
);
2000 /* Add VRR MAC-IP - if any*/
2001 vrr_if
= zebra_get_vrr_intf_for_svi(vlan_if
);
2003 zvni_add_macip_for_intf(vrr_if
, zvni
);
2006 neigh_read_for_vlan(zns
, vlan_if
);
2011 * Hash function for VNI.
2013 static unsigned int vni_hash_keymake(void *p
)
2015 const zebra_vni_t
*zvni
= p
;
2017 return (jhash_1word(zvni
->vni
, 0));
2021 * Compare 2 VNI hash entries.
2023 static int vni_hash_cmp(const void *p1
, const void *p2
)
2025 const zebra_vni_t
*zvni1
= p1
;
2026 const zebra_vni_t
*zvni2
= p2
;
2028 return (zvni1
->vni
== zvni2
->vni
);
2032 * Callback to allocate VNI hash entry.
2034 static void *zvni_alloc(void *p
)
2036 const zebra_vni_t
*tmp_vni
= p
;
2039 zvni
= XCALLOC(MTYPE_ZVNI
, sizeof(zebra_vni_t
));
2040 zvni
->vni
= tmp_vni
->vni
;
2041 return ((void *)zvni
);
2045 * Look up VNI hash entry.
2047 static zebra_vni_t
*zvni_lookup(vni_t vni
)
2049 struct zebra_vrf
*zvrf
;
2050 zebra_vni_t tmp_vni
;
2051 zebra_vni_t
*zvni
= NULL
;
2053 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2055 memset(&tmp_vni
, 0, sizeof(zebra_vni_t
));
2057 zvni
= hash_lookup(zvrf
->vni_table
, &tmp_vni
);
2063 * Add VNI hash entry.
2065 static zebra_vni_t
*zvni_add(vni_t vni
)
2067 struct zebra_vrf
*zvrf
;
2068 zebra_vni_t tmp_zvni
;
2069 zebra_vni_t
*zvni
= NULL
;
2071 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2073 memset(&tmp_zvni
, 0, sizeof(zebra_vni_t
));
2075 zvni
= hash_get(zvrf
->vni_table
, &tmp_zvni
, zvni_alloc
);
2078 /* Create hash table for MAC */
2080 hash_create(mac_hash_keymake
, mac_cmp
, "Zebra VNI MAC Table");
2082 /* Create hash table for neighbors */
2083 zvni
->neigh_table
= hash_create(neigh_hash_keymake
, neigh_cmp
,
2084 "Zebra VNI Neighbor Table");
2090 * Delete VNI hash entry.
2092 static int zvni_del(zebra_vni_t
*zvni
)
2094 struct zebra_vrf
*zvrf
;
2095 zebra_vni_t
*tmp_zvni
;
2097 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2100 zvni
->vxlan_if
= NULL
;
2102 /* Free the neighbor hash table. */
2103 hash_free(zvni
->neigh_table
);
2104 zvni
->neigh_table
= NULL
;
2106 /* Free the MAC hash table. */
2107 hash_free(zvni
->mac_table
);
2108 zvni
->mac_table
= NULL
;
2110 /* Free the VNI hash entry and allocated memory. */
2111 tmp_zvni
= hash_release(zvrf
->vni_table
, zvni
);
2113 XFREE(MTYPE_ZVNI
, tmp_zvni
);
2119 * Inform BGP about local VNI addition.
2121 static int zvni_send_add_to_client(zebra_vni_t
*zvni
)
2123 struct zserv
*client
;
2126 client
= zebra_find_client(ZEBRA_ROUTE_BGP
);
2127 /* BGP may not be running. */
2134 zserv_create_header(s
, ZEBRA_VNI_ADD
, VRF_DEFAULT
);
2135 stream_putl(s
, zvni
->vni
);
2136 stream_put_in_addr(s
, &zvni
->local_vtep_ip
);
2138 /* Write packet size. */
2139 stream_putw_at(s
, 0, stream_get_endp(s
));
2141 if (IS_ZEBRA_DEBUG_VXLAN
)
2142 zlog_debug("Send VNI_ADD %u %s to %s",
2143 zvni
->vni
, inet_ntoa(zvni
->local_vtep_ip
),
2144 zebra_route_string(client
->proto
));
2146 client
->vniadd_cnt
++;
2147 return zebra_server_send_message(client
);
2151 * Inform BGP about local VNI deletion.
2153 static int zvni_send_del_to_client(vni_t vni
)
2155 struct zserv
*client
;
2158 client
= zebra_find_client(ZEBRA_ROUTE_BGP
);
2159 /* BGP may not be running. */
2166 zserv_create_header(s
, ZEBRA_VNI_DEL
, VRF_DEFAULT
);
2167 stream_putl(s
, vni
);
2169 /* Write packet size. */
2170 stream_putw_at(s
, 0, stream_get_endp(s
));
2172 if (IS_ZEBRA_DEBUG_VXLAN
)
2173 zlog_debug("Send VNI_DEL %u to %s", vni
,
2174 zebra_route_string(client
->proto
));
2176 client
->vnidel_cnt
++;
2177 return zebra_server_send_message(client
);
2181 * Build the VNI hash table by going over the VxLAN interfaces. This
2182 * is called when EVPN (advertise-all-vni) is enabled.
2184 static void zvni_build_hash_table()
2186 struct zebra_ns
*zns
;
2187 struct route_node
*rn
;
2188 struct interface
*ifp
;
2190 /* Walk VxLAN interfaces and create VNI hash. */
2191 zns
= zebra_ns_lookup(NS_DEFAULT
);
2192 for (rn
= route_top(zns
->if_table
); rn
; rn
= route_next(rn
)) {
2193 struct zebra_if
*zif
;
2194 struct zebra_l2info_vxlan
*vxl
;
2198 ifp
= (struct interface
*)rn
->info
;
2202 if (!zif
|| zif
->zif_type
!= ZEBRA_IF_VXLAN
)
2204 vxl
= &zif
->l2info
.vxl
;
2208 if (IS_ZEBRA_DEBUG_VXLAN
)
2210 "Create VNI hash for intf %s(%u) VNI %u local IP %s",
2211 ifp
->name
, ifp
->ifindex
, vni
,
2212 inet_ntoa(vxl
->vtep_ip
));
2214 /* VNI hash entry is not expected to exist. */
2215 zvni
= zvni_lookup(vni
);
2218 "VNI hash already present for IF %s(%u) VNI %u",
2219 ifp
->name
, ifp
->ifindex
, vni
);
2223 zvni
= zvni_add(vni
);
2226 "Failed to add VNI hash, IF %s(%u) VNI %u",
2227 ifp
->name
, ifp
->ifindex
, vni
);
2231 zvni
->local_vtep_ip
= vxl
->vtep_ip
;
2232 zvni
->vxlan_if
= ifp
;
2234 /* Inform BGP if interface is up and mapped to bridge. */
2235 if (if_is_operative(ifp
) && zif
->brslave_info
.br_if
)
2236 zvni_send_add_to_client(zvni
);
2241 * See if remote VTEP matches with prefix.
2243 static int zvni_vtep_match(struct in_addr
*vtep_ip
, zebra_vtep_t
*zvtep
)
2245 return (IPV4_ADDR_SAME(vtep_ip
, &zvtep
->vtep_ip
));
2249 * Locate remote VTEP in VNI hash table.
2251 static zebra_vtep_t
*zvni_vtep_find(zebra_vni_t
*zvni
, struct in_addr
*vtep_ip
)
2253 zebra_vtep_t
*zvtep
;
2258 for (zvtep
= zvni
->vteps
; zvtep
; zvtep
= zvtep
->next
) {
2259 if (zvni_vtep_match(vtep_ip
, zvtep
))
2267 * Add remote VTEP to VNI hash table.
2269 static zebra_vtep_t
*zvni_vtep_add(zebra_vni_t
*zvni
, struct in_addr
*vtep_ip
)
2271 zebra_vtep_t
*zvtep
;
2273 zvtep
= XCALLOC(MTYPE_ZVNI_VTEP
, sizeof(zebra_vtep_t
));
2275 zlog_err("Failed to alloc VTEP entry, VNI %u", zvni
->vni
);
2279 zvtep
->vtep_ip
= *vtep_ip
;
2282 zvni
->vteps
->prev
= zvtep
;
2283 zvtep
->next
= zvni
->vteps
;
2284 zvni
->vteps
= zvtep
;
2290 * Remove remote VTEP from VNI hash table.
2292 static int zvni_vtep_del(zebra_vni_t
*zvni
, zebra_vtep_t
*zvtep
)
2295 zvtep
->next
->prev
= zvtep
->prev
;
2297 zvtep
->prev
->next
= zvtep
->next
;
2299 zvni
->vteps
= zvtep
->next
;
2301 zvtep
->prev
= zvtep
->next
= NULL
;
2302 XFREE(MTYPE_ZVNI_VTEP
, zvtep
);
2308 * Delete all remote VTEPs for this VNI (upon VNI delete). Also
2309 * uninstall from kernel if asked to.
2311 static int zvni_vtep_del_all(zebra_vni_t
*zvni
, int uninstall
)
2313 zebra_vtep_t
*zvtep
, *zvtep_next
;
2318 for (zvtep
= zvni
->vteps
; zvtep
; zvtep
= zvtep_next
) {
2319 zvtep_next
= zvtep
->next
;
2321 zvni_vtep_uninstall(zvni
, &zvtep
->vtep_ip
);
2322 zvni_vtep_del(zvni
, zvtep
);
2329 * Install remote VTEP into the kernel.
2331 static int zvni_vtep_install(zebra_vni_t
*zvni
, struct in_addr
*vtep_ip
)
2333 return kernel_add_vtep(zvni
->vni
, zvni
->vxlan_if
, vtep_ip
);
2337 * Uninstall remote VTEP from the kernel.
2339 static int zvni_vtep_uninstall(zebra_vni_t
*zvni
, struct in_addr
*vtep_ip
)
2341 if (!zvni
->vxlan_if
) {
2342 zlog_err("VNI %u hash %p couldn't be uninstalled - no intf",
2347 return kernel_del_vtep(zvni
->vni
, zvni
->vxlan_if
, vtep_ip
);
2351 * Cleanup VNI/VTEP and update kernel
2353 static void zvni_cleanup_all(struct hash_backet
*backet
, void *zvrf
)
2357 zvni
= (zebra_vni_t
*)backet
->data
;
2361 /* Free up all neighbors and MACs, if any. */
2362 zvni_neigh_del_all(zvni
, 1, 0, DEL_ALL_NEIGH
);
2363 zvni_mac_del_all(zvni
, 1, 0, DEL_ALL_MAC
);
2365 /* Free up all remote VTEPs, if any. */
2366 zvni_vtep_del_all(zvni
, 1);
2368 /* Delete the hash entry. */
2373 /* Public functions */
2376 * Display Neighbors for a VNI (VTY command handler).
2378 void zebra_vxlan_print_neigh_vni(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2379 vni_t vni
, u_char use_json
)
2382 u_int32_t num_neigh
;
2383 struct neigh_walk_ctx wctx
;
2384 json_object
*json
= NULL
;
2386 if (!is_evpn_enabled())
2388 zvni
= zvni_lookup(vni
);
2391 vty_out(vty
, "{}\n");
2393 vty_out(vty
, "%% VNI %u does not exist\n", vni
);
2396 num_neigh
= hashcount(zvni
->neigh_table
);
2401 json
= json_object_new_object();
2403 /* Since we have IPv6 addresses to deal with which can vary widely in
2404 * size, we try to be a bit more elegant in display by first computing
2405 * the maximum width.
2407 memset(&wctx
, 0, sizeof(struct neigh_walk_ctx
));
2410 wctx
.addr_width
= 15;
2412 hash_iterate(zvni
->neigh_table
, zvni_find_neigh_addr_width
, &wctx
);
2416 "Number of ARPs (local and remote) known for this VNI: %u\n",
2418 vty_out(vty
, "%*s %-6s %-17s %-21s\n", -wctx
.addr_width
, "IP",
2419 "Type", "MAC", "Remote VTEP");
2421 json_object_int_add(json
, "numArpNd", num_neigh
);
2423 hash_iterate(zvni
->neigh_table
, zvni_print_neigh_hash
, &wctx
);
2425 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2426 json
, JSON_C_TO_STRING_PRETTY
));
2427 json_object_free(json
);
2432 * Display neighbors across all VNIs (VTY command handler).
2434 void zebra_vxlan_print_neigh_all_vni(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2437 json_object
*json
= NULL
;
2440 if (!is_evpn_enabled())
2444 json
= json_object_new_object();
2448 hash_iterate(zvrf
->vni_table
,
2449 (void (*)(struct hash_backet
*,
2450 void *))zvni_print_neigh_hash_all_vni
,
2453 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2454 json
, JSON_C_TO_STRING_PRETTY
));
2455 json_object_free(json
);
2460 * Display specific neighbor for a VNI, if present (VTY command handler).
2462 void zebra_vxlan_print_specific_neigh_vni(struct vty
*vty
,
2463 struct zebra_vrf
*zvrf
, vni_t vni
,
2464 struct ipaddr
*ip
, u_char use_json
)
2468 json_object
*json
= NULL
;
2470 if (!is_evpn_enabled())
2472 zvni
= zvni_lookup(vni
);
2475 vty_out(vty
, "{}\n");
2477 vty_out(vty
, "%% VNI %u does not exist\n", vni
);
2480 n
= zvni_neigh_lookup(zvni
, ip
);
2484 "%% Requested neighbor does not exist in VNI %u\n",
2489 json
= json_object_new_object();
2491 zvni_print_neigh(n
, vty
, json
);
2494 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2495 json
, JSON_C_TO_STRING_PRETTY
));
2496 json_object_free(json
);
2501 * Display neighbors for a VNI from specific VTEP (VTY command handler).
2502 * By definition, these are remote neighbors.
2504 void zebra_vxlan_print_neigh_vni_vtep(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2505 vni_t vni
, struct in_addr vtep_ip
,
2509 u_int32_t num_neigh
;
2510 struct neigh_walk_ctx wctx
;
2511 json_object
*json
= NULL
;
2513 if (!is_evpn_enabled())
2515 zvni
= zvni_lookup(vni
);
2518 vty_out(vty
, "{}\n");
2520 vty_out(vty
, "%% VNI %u does not exist\n", vni
);
2523 num_neigh
= hashcount(zvni
->neigh_table
);
2527 memset(&wctx
, 0, sizeof(struct neigh_walk_ctx
));
2530 wctx
.flags
= SHOW_REMOTE_NEIGH_FROM_VTEP
;
2531 wctx
.r_vtep_ip
= vtep_ip
;
2533 hash_iterate(zvni
->neigh_table
, zvni_print_neigh_hash
, &wctx
);
2536 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2537 json
, JSON_C_TO_STRING_PRETTY
));
2538 json_object_free(json
);
2543 * Display MACs for a VNI (VTY command handler).
2545 void zebra_vxlan_print_macs_vni(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2546 vni_t vni
, u_char use_json
)
2550 struct mac_walk_ctx wctx
;
2551 json_object
*json
= NULL
;
2552 json_object
*json_mac
= NULL
;
2554 if (!is_evpn_enabled())
2556 zvni
= zvni_lookup(vni
);
2559 vty_out(vty
, "{}\n");
2561 vty_out(vty
, "%% VNI %u does not exist\n", vni
);
2564 num_macs
= num_valid_macs(zvni
);
2569 json
= json_object_new_object();
2570 json_mac
= json_object_new_object();
2573 memset(&wctx
, 0, sizeof(struct mac_walk_ctx
));
2576 wctx
.json
= json_mac
;
2580 "Number of MACs (local and remote) known for this VNI: %u\n",
2582 vty_out(vty
, "%-17s %-6s %-21s %-5s\n", "MAC", "Type",
2583 "Intf/Remote VTEP", "VLAN");
2585 json_object_int_add(json
, "numMacs", num_macs
);
2587 hash_iterate(zvni
->mac_table
, zvni_print_mac_hash
, &wctx
);
2590 json_object_object_add(json
, "macs", json_mac
);
2591 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2592 json
, JSON_C_TO_STRING_PRETTY
));
2593 json_object_free(json
);
2598 * Display MACs for all VNIs (VTY command handler).
2600 void zebra_vxlan_print_macs_all_vni(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2603 struct mac_walk_ctx wctx
;
2604 json_object
*json
= NULL
;
2606 if (!is_evpn_enabled()) {
2608 vty_out(vty
, "{}\n");
2612 json
= json_object_new_object();
2614 memset(&wctx
, 0, sizeof(struct mac_walk_ctx
));
2617 hash_iterate(zvrf
->vni_table
, zvni_print_mac_hash_all_vni
, &wctx
);
2620 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2621 json
, JSON_C_TO_STRING_PRETTY
));
2622 json_object_free(json
);
2627 * Display MACs for all VNIs (VTY command handler).
2629 void zebra_vxlan_print_macs_all_vni_vtep(struct vty
*vty
,
2630 struct zebra_vrf
*zvrf
,
2631 struct in_addr vtep_ip
,
2634 struct mac_walk_ctx wctx
;
2635 json_object
*json
= NULL
;
2637 if (!is_evpn_enabled())
2641 json
= json_object_new_object();
2643 memset(&wctx
, 0, sizeof(struct mac_walk_ctx
));
2645 wctx
.flags
= SHOW_REMOTE_MAC_FROM_VTEP
;
2646 wctx
.r_vtep_ip
= vtep_ip
;
2648 hash_iterate(zvrf
->vni_table
, zvni_print_mac_hash_all_vni
, &wctx
);
2651 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2652 json
, JSON_C_TO_STRING_PRETTY
));
2653 json_object_free(json
);
2658 * Display specific MAC for a VNI, if present (VTY command handler).
2660 void zebra_vxlan_print_specific_mac_vni(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2661 vni_t vni
, struct ethaddr
*macaddr
)
2666 if (!is_evpn_enabled())
2668 zvni
= zvni_lookup(vni
);
2670 vty_out(vty
, "%% VNI %u does not exist\n", vni
);
2673 mac
= zvni_mac_lookup(zvni
, macaddr
);
2675 vty_out(vty
, "%% Requested MAC does not exist in VNI %u\n",
2680 zvni_print_mac(mac
, vty
);
2684 * Display MACs for a VNI from specific VTEP (VTY command handler).
2686 void zebra_vxlan_print_macs_vni_vtep(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2687 vni_t vni
, struct in_addr vtep_ip
,
2692 struct mac_walk_ctx wctx
;
2693 json_object
*json
= NULL
;
2694 json_object
*json_mac
= NULL
;
2696 if (!is_evpn_enabled())
2698 zvni
= zvni_lookup(vni
);
2701 vty_out(vty
, "{}\n");
2703 vty_out(vty
, "%% VNI %u does not exist\n", vni
);
2706 num_macs
= num_valid_macs(zvni
);
2711 json
= json_object_new_object();
2712 json_mac
= json_object_new_object();
2715 memset(&wctx
, 0, sizeof(struct mac_walk_ctx
));
2718 wctx
.flags
= SHOW_REMOTE_MAC_FROM_VTEP
;
2719 wctx
.r_vtep_ip
= vtep_ip
;
2720 wctx
.json
= json_mac
;
2721 hash_iterate(zvni
->mac_table
, zvni_print_mac_hash
, &wctx
);
2724 json_object_int_add(json
, "numMacs", wctx
.count
);
2726 json_object_object_add(json
, "macs", json_mac
);
2727 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2728 json
, JSON_C_TO_STRING_PRETTY
));
2729 json_object_free(json
);
2735 * Display VNI information (VTY command handler).
2737 void zebra_vxlan_print_vni(struct vty
*vty
, struct zebra_vrf
*zvrf
, vni_t vni
,
2741 json_object
*json
= NULL
;
2744 if (!is_evpn_enabled())
2746 zvni
= zvni_lookup(vni
);
2749 vty_out(vty
, "{}\n");
2751 vty_out(vty
, "%% VNI %u does not exist\n", vni
);
2755 json
= json_object_new_object();
2758 zvni_print(zvni
, (void *)args
);
2760 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2761 json
, JSON_C_TO_STRING_PRETTY
));
2762 json_object_free(json
);
2767 * Display VNI hash table (VTY command handler).
2769 void zebra_vxlan_print_vnis(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2773 json_object
*json
= NULL
;
2776 if (!is_evpn_enabled())
2778 num_vnis
= hashcount(zvrf
->vni_table
);
2781 vty_out(vty
, "{}\n");
2785 json
= json_object_new_object();
2786 json_object_string_add(json
, "advertiseGatewayMacip",
2787 zvrf
->advertise_gw_macip
? "Yes" : "No");
2788 json_object_int_add(json
, "numVnis", num_vnis
);
2790 vty_out(vty
, "Advertise gateway mac-ip: %s\n",
2791 zvrf
->advertise_gw_macip
? "Yes" : "No");
2792 vty_out(vty
, "Number of VNIs: %u\n", num_vnis
);
2793 vty_out(vty
, "%-10s %-21s %-15s %-8s %-8s %-15s\n", "VNI",
2794 "VxLAN IF", "VTEP IP", "# MACs", "# ARPs",
2800 hash_iterate(zvrf
->vni_table
,
2801 (void (*)(struct hash_backet
*, void *))zvni_print_hash
,
2805 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2806 json
, JSON_C_TO_STRING_PRETTY
));
2807 json_object_free(json
);
2812 * Handle neighbor delete (on a VLAN device / L3 interface) from the
2813 * kernel. This may result in either the neighbor getting deleted from
2814 * our database or being re-added to the kernel (if it is a valid
2817 int zebra_vxlan_local_neigh_del(struct interface
*ifp
,
2818 struct interface
*link_if
, struct ipaddr
*ip
)
2822 char buf
[INET6_ADDRSTRLEN
];
2823 char buf2
[ETHER_ADDR_STRLEN
];
2826 /* We are only interested in neighbors on an SVI that resides on top
2827 * of a VxLAN bridge.
2829 zvni
= zvni_map_svi(ifp
, link_if
);
2832 if (!zvni
->vxlan_if
) {
2834 "VNI %u hash %p doesn't have intf upon local neighbor DEL",
2839 if (IS_ZEBRA_DEBUG_VXLAN
)
2840 zlog_debug("Del neighbor %s intf %s(%u) -> VNI %u",
2841 ipaddr2str(ip
, buf
, sizeof(buf
)),
2842 ifp
->name
, ifp
->ifindex
, zvni
->vni
);
2844 /* If entry doesn't exist, nothing to do. */
2845 n
= zvni_neigh_lookup(zvni
, ip
);
2849 zmac
= zvni_mac_lookup(zvni
, &n
->emac
);
2851 if (IS_ZEBRA_DEBUG_VXLAN
)
2853 "Trying to del a neigh %s without a mac %s on VNI %u",
2854 ipaddr2str(ip
, buf
, sizeof(buf
)),
2855 prefix_mac2str(&n
->emac
, buf2
, sizeof(buf2
)),
2861 /* If it is a remote entry, the kernel has aged this out or someone has
2862 * deleted it, it needs to be re-installed as Quagga is the owner.
2864 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
)) {
2865 zvni_neigh_install(zvni
, n
);
2869 /* Remove neighbor from BGP. */
2870 if (IS_ZEBRA_NEIGH_ACTIVE(n
))
2871 zvni_neigh_send_del_to_client(zvni
->vni
, &n
->ip
, &n
->emac
,
2874 /* Delete this neighbor entry. */
2875 zvni_neigh_del(zvni
, n
);
2877 /* see if the AUTO mac needs to be deleted */
2878 if (CHECK_FLAG(zmac
->flags
, ZEBRA_MAC_AUTO
)
2879 && !listcount(zmac
->neigh_list
))
2880 zvni_mac_del(zvni
, zmac
);
2886 * Handle neighbor add or update (on a VLAN device / L3 interface)
2889 int zebra_vxlan_local_neigh_add_update(struct interface
*ifp
,
2890 struct interface
*link_if
,
2892 struct ethaddr
*macaddr
, u_int16_t state
,
2897 zebra_mac_t
*zmac
, *old_zmac
;
2898 char buf
[ETHER_ADDR_STRLEN
];
2899 char buf2
[INET6_ADDRSTRLEN
];
2901 /* We are only interested in neighbors on an SVI that resides on top
2902 * of a VxLAN bridge.
2904 zvni
= zvni_map_svi(ifp
, link_if
);
2908 if (IS_ZEBRA_DEBUG_VXLAN
)
2910 "Add/Update neighbor %s MAC %s intf %s(%u) state 0x%x "
2912 ipaddr2str(ip
, buf2
, sizeof(buf2
)),
2913 prefix_mac2str(macaddr
, buf
, sizeof(buf
)), ifp
->name
,
2914 ifp
->ifindex
, state
, ext_learned
? "ext-learned " : "",
2917 /* create a dummy MAC if the MAC is not already present */
2918 zmac
= zvni_mac_lookup(zvni
, macaddr
);
2920 if (IS_ZEBRA_DEBUG_VXLAN
)
2922 "AUTO MAC %s created for neigh %s on VNI %u",
2923 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
2924 ipaddr2str(ip
, buf2
, sizeof(buf2
)), zvni
->vni
);
2926 zmac
= zvni_mac_add(zvni
, macaddr
);
2928 zlog_warn("Failed to add MAC %s VNI %u",
2929 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
2934 memset(&zmac
->fwd_info
, 0, sizeof(zmac
->fwd_info
));
2935 memset(&zmac
->flags
, 0, sizeof(u_int32_t
));
2936 SET_FLAG(zmac
->flags
, ZEBRA_MAC_AUTO
);
2939 /* If same entry already exists, it might be a change or it might be a
2940 * move from remote to local.
2942 n
= zvni_neigh_lookup(zvni
, ip
);
2944 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
)) {
2945 if (memcmp(n
->emac
.octet
, macaddr
->octet
,
2948 /* Update any params and return - client doesn't
2949 * care about a purely local change.
2951 n
->ifindex
= ifp
->ifindex
;
2955 /* If the MAC has changed,
2956 * need to issue a delete first
2957 * as this means a different MACIP route.
2958 * Also, need to do some unlinking/relinking.
2960 zvni_neigh_send_del_to_client(zvni
->vni
, &n
->ip
,
2962 old_zmac
= zvni_mac_lookup(zvni
, &n
->emac
);
2964 listnode_delete(old_zmac
->neigh_list
, n
);
2965 zvni_deref_ip2mac(zvni
, old_zmac
, 0);
2968 /* Set "local" forwarding info. */
2969 SET_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
);
2970 n
->ifindex
= ifp
->ifindex
;
2971 memcpy(&n
->emac
, macaddr
, ETH_ALEN
);
2973 /* Link to new MAC */
2974 listnode_add_sort(zmac
->neigh_list
, n
);
2975 } else if (ext_learned
)
2976 /* The neighbor is remote and that is the notification we got.
2979 /* TODO: Evaluate if we need to do anything here. */
2982 /* Neighbor has moved from remote to local. */
2984 UNSET_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
);
2985 n
->r_vtep_ip
.s_addr
= 0;
2986 SET_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
);
2987 n
->ifindex
= ifp
->ifindex
;
2990 n
= zvni_neigh_add(zvni
, ip
, macaddr
);
2993 "Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
2994 ipaddr2str(ip
, buf2
, sizeof(buf2
)),
2995 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
2996 ifp
->name
, ifp
->ifindex
, zvni
->vni
);
2999 /* Set "local" forwarding info. */
3000 SET_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
);
3001 n
->ifindex
= ifp
->ifindex
;
3004 /* Before we program this in BGP, we need to check if MAC is locally
3006 if (!CHECK_FLAG(zmac
->flags
, ZEBRA_MAC_LOCAL
)) {
3007 if (IS_ZEBRA_DEBUG_VXLAN
)
3009 "Skipping neigh %s add to client as MAC %s is not local on VNI %u",
3010 ipaddr2str(ip
, buf2
, sizeof(buf2
)),
3011 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
3018 if (IS_ZEBRA_DEBUG_VXLAN
)
3019 zlog_debug("neigh %s (MAC %s) is now ACTIVE on VNI %u",
3020 ipaddr2str(ip
, buf2
, sizeof(buf2
)),
3021 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
3024 ZEBRA_NEIGH_SET_ACTIVE(n
);
3025 return zvni_neigh_send_add_to_client(zvni
->vni
, ip
, macaddr
, 0);
3030 * Handle message from client to delete a remote MACIP for a VNI.
3032 int zebra_vxlan_remote_macip_del(struct zserv
*client
, int sock
, u_short length
,
3033 struct zebra_vrf
*zvrf
)
3037 struct ethaddr macaddr
;
3039 struct in_addr vtep_ip
;
3043 u_short l
= 0, ipa_len
;
3044 char buf
[ETHER_ADDR_STRLEN
];
3045 char buf1
[INET6_ADDRSTRLEN
];
3046 struct interface
*ifp
= NULL
;
3047 struct zebra_if
*zif
= NULL
;
3051 while (l
< length
) {
3052 /* Obtain each remote MACIP and process. */
3053 /* Message contains VNI, followed by MAC followed by IP (if any)
3054 * followed by remote VTEP IP.
3058 memset(&ip
, 0, sizeof(ip
));
3059 vni
= (vni_t
)stream_getl(s
);
3060 stream_get(&macaddr
.octet
, s
, ETH_ALEN
);
3061 ipa_len
= stream_getl(s
);
3063 ip
.ipa_type
= (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
3065 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
3067 l
+= 4 + ETH_ALEN
+ 4 + ipa_len
;
3068 vtep_ip
.s_addr
= stream_get_ipv4(s
);
3069 l
+= IPV4_MAX_BYTELEN
;
3071 if (IS_ZEBRA_DEBUG_VXLAN
)
3073 "Recv MACIP Del MAC %s IP %s VNI %u Remote VTEP %s from %s",
3074 prefix_mac2str(&macaddr
, buf
, sizeof(buf
)),
3075 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
,
3077 zebra_route_string(client
->proto
));
3079 /* Locate VNI hash entry - expected to exist. */
3080 zvni
= zvni_lookup(vni
);
3082 if (IS_ZEBRA_DEBUG_VXLAN
)
3084 "Failed to locate VNI hash upon remote MACIP DEL, "
3089 ifp
= zvni
->vxlan_if
;
3092 "VNI %u hash %p doesn't have intf upon remote MACIP DEL",
3098 /* If down or not mapped to a bridge, we're done. */
3099 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
3102 /* The remote VTEP specified is normally expected to exist, but
3104 * possible that the peer may delete the VTEP before deleting
3106 * referring to the VTEP, in which case the handler (see
3108 * would have already deleted the MACs.
3110 if (!zvni_vtep_find(zvni
, &vtep_ip
))
3113 mac
= zvni_mac_lookup(zvni
, &macaddr
);
3115 n
= zvni_neigh_lookup(zvni
, &ip
);
3119 "Failed to locate MAC %s for neigh %s VNI %u",
3120 prefix_mac2str(&macaddr
, buf
, sizeof(buf
)),
3121 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
3125 /* If the remote mac or neighbor doesn't exist there is nothing
3127 * to do. Otherwise, uninstall the entry and then remove it.
3132 /* Uninstall remote neighbor or MAC. */
3134 /* When the MAC changes for an IP, it is possible the
3136 * update the new MAC before trying to delete the "old"
3138 * (as these are two different MACIP routes). Do the
3140 * if the MAC matches.
3142 if (CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
)
3143 && (memcmp(n
->emac
.octet
, macaddr
.octet
,
3146 zvni_neigh_uninstall(zvni
, n
);
3147 zvni_neigh_del(zvni
, n
);
3148 zvni_deref_ip2mac(zvni
, mac
, 1);
3151 if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
)) {
3152 zvni_process_neigh_on_remote_mac_del(zvni
,
3155 if (list_isempty(mac
->neigh_list
)) {
3156 zvni_mac_uninstall(zvni
, mac
, 0);
3157 zvni_mac_del(zvni
, mac
);
3159 SET_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
);
3168 * Handle message from client to add a remote MACIP for a VNI. This
3169 * could be just the add of a MAC address or the add of a neighbor
3172 int zebra_vxlan_remote_macip_add(struct zserv
*client
, int sock
, u_short length
,
3173 struct zebra_vrf
*zvrf
)
3177 struct ethaddr macaddr
;
3179 struct in_addr vtep_ip
;
3181 zebra_vtep_t
*zvtep
;
3182 zebra_mac_t
*mac
, *old_mac
;
3184 u_short l
= 0, ipa_len
;
3185 int update_mac
= 0, update_neigh
= 0;
3186 char buf
[ETHER_ADDR_STRLEN
];
3187 char buf1
[INET6_ADDRSTRLEN
];
3189 struct interface
*ifp
= NULL
;
3190 struct zebra_if
*zif
= NULL
;
3192 assert(EVPN_ENABLED(zvrf
));
3196 while (l
< length
) {
3197 /* Obtain each remote MACIP and process. */
3198 /* Message contains VNI, followed by MAC followed by IP (if any)
3199 * followed by remote VTEP IP.
3201 update_mac
= update_neigh
= 0;
3204 memset(&ip
, 0, sizeof(ip
));
3205 vni
= (vni_t
)stream_getl(s
);
3206 stream_get(&macaddr
.octet
, s
, ETH_ALEN
);
3207 ipa_len
= stream_getl(s
);
3209 ip
.ipa_type
= (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
3211 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
3213 l
+= 4 + ETH_ALEN
+ 4 + ipa_len
;
3214 vtep_ip
.s_addr
= stream_get_ipv4(s
);
3215 l
+= IPV4_MAX_BYTELEN
;
3217 /* Get 'sticky' flag. */
3218 sticky
= stream_getc(s
);
3221 if (IS_ZEBRA_DEBUG_VXLAN
)
3223 "Recv MACIP Add %sMAC %s IP %s VNI %u Remote VTEP %s from %s",
3224 sticky
? "sticky " : "",
3225 prefix_mac2str(&macaddr
, buf
, sizeof(buf
)),
3226 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
,
3228 zebra_route_string(client
->proto
));
3230 /* Locate VNI hash entry - expected to exist. */
3231 zvni
= zvni_lookup(vni
);
3234 "Failed to locate VNI hash upon remote MACIP ADD, VNI %u",
3238 ifp
= zvni
->vxlan_if
;
3241 "VNI %u hash %p doesn't have intf upon remote MACIP add",
3247 /* If down or not mapped to a bridge, we're done. */
3248 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
3251 /* The remote VTEP specified should normally exist, but it is
3253 * that when peering comes up, peer may advertise MACIP routes
3255 * advertising type-3 routes.
3257 zvtep
= zvni_vtep_find(zvni
, &vtep_ip
);
3259 if (zvni_vtep_add(zvni
, &vtep_ip
) == NULL
) {
3261 "Failed to add remote VTEP, VNI %u zvni %p",
3266 zvni_vtep_install(zvni
, &vtep_ip
);
3269 /* First, check if the remote MAC is unknown or has a change. If
3271 * that needs to be updated first. Note that client could
3273 * MAC and MACIP separately or just install the latter.
3275 mac
= zvni_mac_lookup(zvni
, &macaddr
);
3276 if (!mac
|| !CHECK_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
)
3277 || (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
) ? 1 : 0)
3279 || !IPV4_ADDR_SAME(&mac
->fwd_info
.r_vtep_ip
, &vtep_ip
))
3284 mac
= zvni_mac_add(zvni
, &macaddr
);
3287 "Failed to add MAC %s VNI %u Remote VTEP %s",
3288 prefix_mac2str(&macaddr
, buf
,
3290 vni
, inet_ntoa(vtep_ip
));
3294 /* Is this MAC created for a MACIP? */
3296 SET_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
);
3299 /* Set "auto" and "remote" forwarding info. */
3300 UNSET_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
);
3301 memset(&mac
->fwd_info
, 0, sizeof(mac
->fwd_info
));
3302 SET_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
);
3303 mac
->fwd_info
.r_vtep_ip
= vtep_ip
;
3306 SET_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
);
3308 UNSET_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
);
3310 zvni_process_neigh_on_remote_mac_add(zvni
, mac
);
3312 /* Install the entry. */
3313 zvni_mac_install(zvni
, mac
);
3316 /* If there is no IP, continue - after clearing AUTO flag of
3319 UNSET_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
);
3323 /* Check if the remote neighbor itself is unknown or has a
3325 * If so, create or update and then install the entry.
3327 n
= zvni_neigh_lookup(zvni
, &ip
);
3328 if (!n
|| !CHECK_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
)
3329 || (memcmp(&n
->emac
, &macaddr
, sizeof(macaddr
)) != 0)
3330 || !IPV4_ADDR_SAME(&n
->r_vtep_ip
, &vtep_ip
))
3335 n
= zvni_neigh_add(zvni
, &ip
, &macaddr
);
3338 "Failed to add Neigh %s MAC %s VNI %u Remote VTEP %s",
3339 ipaddr2str(&ip
, buf1
,
3341 prefix_mac2str(&macaddr
, buf
,
3343 vni
, inet_ntoa(vtep_ip
));
3347 } else if (memcmp(&n
->emac
, &macaddr
, sizeof(macaddr
))
3349 /* MAC change, update neigh list for old and new
3351 old_mac
= zvni_mac_lookup(zvni
, &n
->emac
);
3353 listnode_delete(old_mac
->neigh_list
, n
);
3354 zvni_deref_ip2mac(zvni
, old_mac
, 1);
3356 listnode_add_sort(mac
->neigh_list
, n
);
3357 memcpy(&n
->emac
, &macaddr
, ETH_ALEN
);
3360 /* Set "remote" forwarding info. */
3361 UNSET_FLAG(n
->flags
, ZEBRA_NEIGH_LOCAL
);
3362 /* TODO: Handle MAC change. */
3363 n
->r_vtep_ip
= vtep_ip
;
3364 SET_FLAG(n
->flags
, ZEBRA_NEIGH_REMOTE
);
3366 /* Install the entry. */
3367 zvni_neigh_install(zvni
, n
);
3375 * Handle notification of MAC add/update over VxLAN. If the kernel is notifying
3376 * us, this must involve a multihoming scenario. Treat this as implicit delete
3377 * of any prior local MAC.
3379 int zebra_vxlan_check_del_local_mac(struct interface
*ifp
,
3380 struct interface
*br_if
,
3381 struct ethaddr
*macaddr
, vlanid_t vid
)
3383 struct zebra_if
*zif
;
3384 struct zebra_l2info_vxlan
*vxl
;
3388 char buf
[ETHER_ADDR_STRLEN
];
3393 vxl
= &zif
->l2info
.vxl
;
3396 /* Check if EVPN is enabled. */
3397 if (!is_evpn_enabled())
3400 /* Locate hash entry; it is expected to exist. */
3401 zvni
= zvni_lookup(vni
);
3405 /* If entry doesn't exist, nothing to do. */
3406 mac
= zvni_mac_lookup(zvni
, macaddr
);
3410 /* Is it a local entry? */
3411 if (!CHECK_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
))
3414 if (IS_ZEBRA_DEBUG_VXLAN
)
3416 "Add/update remote MAC %s intf %s(%u) VNI %u - del local",
3417 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
3418 ifp
->name
, ifp
->ifindex
, vni
);
3420 /* Remove MAC from BGP. */
3421 sticky
= CHECK_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
) ? 1 : 0;
3422 zvni_mac_send_del_to_client(zvni
->vni
, macaddr
,
3423 (sticky
? ZEBRA_MAC_TYPE_STICKY
: 0));
3426 * If there are no neigh associated with the mac delete the mac
3427 * else mark it as AUTO for forward reference
3429 if (!listcount(mac
->neigh_list
)) {
3430 zvni_mac_del(zvni
, mac
);
3432 UNSET_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
);
3433 SET_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
);
3440 * Handle remote MAC delete by kernel; readd the remote MAC if we have it.
3441 * This can happen because the remote MAC entries are also added as "dynamic",
3442 * so the kernel can ageout the entry.
3444 int zebra_vxlan_check_readd_remote_mac(struct interface
*ifp
,
3445 struct interface
*br_if
,
3446 struct ethaddr
*macaddr
, vlanid_t vid
)
3448 struct zebra_if
*zif
;
3449 struct zebra_l2info_vxlan
*vxl
;
3453 char buf
[ETHER_ADDR_STRLEN
];
3457 vxl
= &zif
->l2info
.vxl
;
3460 /* Check if EVPN is enabled. */
3461 if (!is_evpn_enabled())
3464 /* Locate hash entry; it is expected to exist. */
3465 zvni
= zvni_lookup(vni
);
3469 /* If entry doesn't exist, nothing to do. */
3470 mac
= zvni_mac_lookup(zvni
, macaddr
);
3474 /* Is it a remote entry? */
3475 if (!CHECK_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
))
3478 if (IS_ZEBRA_DEBUG_VXLAN
)
3479 zlog_debug("Del remote MAC %s intf %s(%u) VNI %u - readd",
3480 prefix_mac2str(macaddr
, buf
, sizeof(buf
)), ifp
->name
,
3483 zvni_mac_install(zvni
, mac
);
3488 * Handle local MAC delete (on a port or VLAN corresponding to this VNI).
3490 int zebra_vxlan_local_mac_del(struct interface
*ifp
, struct interface
*br_if
,
3491 struct ethaddr
*macaddr
, vlanid_t vid
)
3495 char buf
[ETHER_ADDR_STRLEN
];
3498 /* We are interested in MACs only on ports or (port, VLAN) that
3501 zvni
= zvni_map_vlan(ifp
, br_if
, vid
);
3504 if (!zvni
->vxlan_if
) {
3505 zlog_err("VNI %u hash %p doesn't have intf upon local MAC DEL",
3510 if (IS_ZEBRA_DEBUG_VXLAN
)
3511 zlog_debug("Del MAC %s intf %s(%u) VID %u -> VNI %u",
3512 prefix_mac2str(macaddr
, buf
, sizeof(buf
)), ifp
->name
,
3513 ifp
->ifindex
, vid
, zvni
->vni
);
3515 /* If entry doesn't exist, nothing to do. */
3516 mac
= zvni_mac_lookup(zvni
, macaddr
);
3520 /* Is it a local entry? */
3521 if (!CHECK_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
))
3524 /* Remove MAC from BGP. */
3525 sticky
= CHECK_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
) ? 1 : 0;
3526 zvni_mac_send_del_to_client(zvni
->vni
, macaddr
,
3527 (sticky
? ZEBRA_MAC_TYPE_STICKY
: 0));
3529 /* Update all the neigh entries associated with this mac */
3530 zvni_process_neigh_on_local_mac_del(zvni
, mac
);
3533 * If there are no neigh associated with the mac delete the mac
3534 * else mark it as AUTO for forward reference
3536 if (!listcount(mac
->neigh_list
)) {
3537 zvni_mac_del(zvni
, mac
);
3539 UNSET_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
);
3540 SET_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
);
3547 * Handle local MAC add (on a port or VLAN corresponding to this VNI).
3549 int zebra_vxlan_local_mac_add_update(struct interface
*ifp
,
3550 struct interface
*br_if
,
3551 struct ethaddr
*macaddr
, vlanid_t vid
,
3556 char buf
[ETHER_ADDR_STRLEN
];
3560 /* We are interested in MACs only on ports or (port, VLAN) that
3563 zvni
= zvni_map_vlan(ifp
, br_if
, vid
);
3565 if (IS_ZEBRA_DEBUG_VXLAN
)
3567 "Add/Update %sMAC %s intf %s(%u) VID %u, could not find VNI",
3568 sticky
? "sticky " : "",
3569 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
3570 ifp
->name
, ifp
->ifindex
, vid
);
3574 if (!zvni
->vxlan_if
) {
3575 zlog_err("VNI %u hash %p doesn't have intf upon local MAC ADD",
3580 if (IS_ZEBRA_DEBUG_VXLAN
)
3582 "Add/Update %sMAC %s intf %s(%u) VID %u -> VNI %u",
3583 sticky
? "sticky " : "",
3584 prefix_mac2str(macaddr
, buf
, sizeof(buf
)), ifp
->name
,
3585 ifp
->ifindex
, vid
, zvni
->vni
);
3587 /* If same entry already exists, nothing to do. */
3588 mac
= zvni_mac_lookup(zvni
, macaddr
);
3590 if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
)) {
3591 mac_sticky
= CHECK_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
)
3597 * return if nothing has changed.
3598 * inform bgp if sticky flag has changed
3599 * update locally and do not inform bgp if local
3600 * parameters like interface has changed
3602 if (mac_sticky
== sticky
3603 && mac
->fwd_info
.local
.ifindex
== ifp
->ifindex
3604 && mac
->fwd_info
.local
.vid
== vid
) {
3605 if (IS_ZEBRA_DEBUG_VXLAN
)
3607 "Add/Update %sMAC %s intf %s(%u) VID %u -> VNI %u, "
3608 "entry exists and has not changed ",
3609 sticky
? "sticky " : "",
3610 prefix_mac2str(macaddr
, buf
,
3612 ifp
->name
, ifp
->ifindex
, vid
,
3615 } else if (mac_sticky
!= sticky
) {
3618 add
= 0; /* This is an update of local
3621 } else if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
)) {
3623 * If we have already learned the MAC as a remote sticky
3625 * this is a operator error and we must log a warning
3627 if (CHECK_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
)) {
3629 "MAC %s is already learnt as a remote sticky mac behind VTEP %s VNI %d",
3630 prefix_mac2str(macaddr
, buf
,
3632 inet_ntoa(mac
->fwd_info
.r_vtep_ip
),
3640 mac
= zvni_mac_add(zvni
, macaddr
);
3642 zlog_err("Failed to add MAC %s intf %s(%u) VID %u",
3643 prefix_mac2str(macaddr
, buf
, sizeof(buf
)),
3644 ifp
->name
, ifp
->ifindex
, vid
);
3649 /* Set "local" forwarding info. */
3650 UNSET_FLAG(mac
->flags
, ZEBRA_MAC_REMOTE
);
3651 UNSET_FLAG(mac
->flags
, ZEBRA_MAC_AUTO
);
3652 SET_FLAG(mac
->flags
, ZEBRA_MAC_LOCAL
);
3653 memset(&mac
->fwd_info
, 0, sizeof(mac
->fwd_info
));
3654 mac
->fwd_info
.local
.ifindex
= ifp
->ifindex
;
3655 mac
->fwd_info
.local
.vid
= vid
;
3658 SET_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
);
3660 UNSET_FLAG(mac
->flags
, ZEBRA_MAC_STICKY
);
3662 /* Inform BGP if required. */
3664 zvni_process_neigh_on_local_mac_add(zvni
, mac
);
3665 return zvni_mac_send_add_to_client(zvni
->vni
, macaddr
,
3673 * Handle message from client to delete a remote VTEP for a VNI.
3675 int zebra_vxlan_remote_vtep_del(struct zserv
*client
, int sock
, u_short length
,
3676 struct zebra_vrf
*zvrf
)
3681 struct in_addr vtep_ip
;
3683 zebra_vtep_t
*zvtep
;
3684 struct interface
*ifp
;
3685 struct zebra_if
*zif
;
3687 assert(is_evpn_enabled());
3688 if (zvrf_id(zvrf
) != VRF_DEFAULT
) {
3689 zlog_err("Recv MACIP DEL for non-default VRF %u",
3696 while (l
< length
) {
3697 /* Obtain each remote VTEP and process. */
3698 vni
= (vni_t
)stream_getl(s
);
3700 vtep_ip
.s_addr
= stream_get_ipv4(s
);
3701 l
+= IPV4_MAX_BYTELEN
;
3703 if (IS_ZEBRA_DEBUG_VXLAN
)
3704 zlog_debug("Recv VTEP_DEL %s VNI %u from %s",
3705 inet_ntoa(vtep_ip
), vni
,
3706 zebra_route_string(client
->proto
));
3708 /* Locate VNI hash entry - expected to exist. */
3709 zvni
= zvni_lookup(vni
);
3711 if (IS_ZEBRA_DEBUG_VXLAN
)
3713 "Failed to locate VNI hash upon remote VTEP DEL, "
3719 ifp
= zvni
->vxlan_if
;
3722 "VNI %u hash %p doesn't have intf upon remote VTEP DEL",
3728 /* If down or not mapped to a bridge, we're done. */
3729 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
3732 /* If the remote VTEP does not exist, there's nothing more to
3734 * Otherwise, uninstall any remote MACs pointing to this VTEP
3736 * then, the VTEP entry itself and remove it.
3738 zvtep
= zvni_vtep_find(zvni
, &vtep_ip
);
3742 zvni_neigh_del_from_vtep(zvni
, 1, &vtep_ip
);
3743 zvni_mac_del_from_vtep(zvni
, 1, &vtep_ip
);
3744 zvni_vtep_uninstall(zvni
, &vtep_ip
);
3745 zvni_vtep_del(zvni
, zvtep
);
3752 * Handle message from client to add a remote VTEP for a VNI.
3754 int zebra_vxlan_remote_vtep_add(struct zserv
*client
, int sock
, u_short length
,
3755 struct zebra_vrf
*zvrf
)
3760 struct in_addr vtep_ip
;
3762 struct interface
*ifp
;
3763 struct zebra_if
*zif
;
3765 assert(is_evpn_enabled());
3766 if (zvrf_id(zvrf
) != VRF_DEFAULT
) {
3767 zlog_err("Recv MACIP ADD for non-default VRF %u",
3774 while (l
< length
) {
3775 /* Obtain each remote VTEP and process. */
3776 vni
= (vni_t
)stream_getl(s
);
3778 vtep_ip
.s_addr
= stream_get_ipv4(s
);
3779 l
+= IPV4_MAX_BYTELEN
;
3781 if (IS_ZEBRA_DEBUG_VXLAN
)
3782 zlog_debug("Recv VTEP_ADD %s VNI %u from %s",
3783 inet_ntoa(vtep_ip
), vni
,
3784 zebra_route_string(client
->proto
));
3786 /* Locate VNI hash entry - expected to exist. */
3787 zvni
= zvni_lookup(vni
);
3790 "Failed to locate VNI hash upon remote VTEP ADD, VNI %u",
3795 ifp
= zvni
->vxlan_if
;
3798 "VNI %u hash %p doesn't have intf upon remote VTEP ADD",
3805 /* If down or not mapped to a bridge, we're done. */
3806 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
3809 /* If the remote VTEP already exists,
3810 there's nothing more to do. */
3811 if (zvni_vtep_find(zvni
, &vtep_ip
))
3814 if (zvni_vtep_add(zvni
, &vtep_ip
) == NULL
) {
3816 "Failed to add remote VTEP, VNI %u zvni %p",
3821 zvni_vtep_install(zvni
, &vtep_ip
);
3828 * Add/Del gateway macip to evpn
3830 * 1. SVI interface on a vlan aware bridge
3831 * 2. SVI interface on a vlan unaware bridge
3832 * 3. vrr interface (MACVLAN) associated to a SVI
3833 * We advertise macip routes for an interface if it is associated to VxLan vlan
3835 int zebra_vxlan_add_del_gw_macip(struct interface
*ifp
, struct prefix
*p
,
3839 struct ethaddr macaddr
;
3840 zebra_vni_t
*zvni
= NULL
;
3842 memset(&ip
, 0, sizeof(struct ipaddr
));
3843 memset(&macaddr
, 0, sizeof(struct ethaddr
));
3845 /* Check if EVPN is enabled. */
3846 if (!is_evpn_enabled())
3849 if (IS_ZEBRA_IF_MACVLAN(ifp
)) {
3850 struct interface
*svi_if
=
3851 NULL
; /* SVI corresponding to the MACVLAN */
3852 struct zebra_if
*ifp_zif
=
3853 NULL
; /* Zebra daemon specific info for MACVLAN */
3854 struct zebra_if
*svi_if_zif
=
3855 NULL
; /* Zebra daemon specific info for SVI*/
3857 ifp_zif
= ifp
->info
;
3862 * for a MACVLAN interface the link represents the svi_if
3864 svi_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
),
3865 ifp_zif
->link_ifindex
);
3867 zlog_err("MACVLAN %s(%u) without link information",
3868 ifp
->name
, ifp
->ifindex
);
3872 if (IS_ZEBRA_IF_VLAN(svi_if
)) {
3874 * If it is a vlan aware bridge then the link gives the
3875 * bridge information
3877 struct interface
*svi_if_link
= NULL
;
3879 svi_if_zif
= svi_if
->info
;
3881 svi_if_link
= if_lookup_by_index_per_ns(
3882 zebra_ns_lookup(NS_DEFAULT
),
3883 svi_if_zif
->link_ifindex
);
3884 zvni
= zvni_map_svi(svi_if
, svi_if_link
);
3886 } else if (IS_ZEBRA_IF_BRIDGE(svi_if
)) {
3888 * If it is a vlan unaware bridge then svi is the bridge
3891 zvni
= zvni_map_svi(svi_if
, svi_if
);
3893 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
3894 struct zebra_if
*svi_if_zif
=
3895 NULL
; /* Zebra daemon specific info for SVI */
3896 struct interface
*svi_if_link
=
3897 NULL
; /* link info for the SVI = bridge info */
3899 svi_if_zif
= ifp
->info
;
3900 svi_if_link
= if_lookup_by_index_per_ns(
3901 zebra_ns_lookup(NS_DEFAULT
), svi_if_zif
->link_ifindex
);
3902 if (svi_if_zif
&& svi_if_link
)
3903 zvni
= zvni_map_svi(ifp
, svi_if_link
);
3904 } else if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
3905 zvni
= zvni_map_svi(ifp
, ifp
);
3911 if (!zvni
->vxlan_if
) {
3912 zlog_err("VNI %u hash %p doesn't have intf upon MACVLAN up",
3918 /* check if we are advertising gw macip routes */
3919 if (!advertise_gw_macip_enabled(zvni
))
3922 memcpy(&macaddr
.octet
, ifp
->hw_addr
, ETH_ALEN
);
3924 if (p
->family
== AF_INET
) {
3925 ip
.ipa_type
= IPADDR_V4
;
3926 memcpy(&(ip
.ipaddr_v4
), &(p
->u
.prefix4
),
3927 sizeof(struct in_addr
));
3928 } else if (p
->family
== AF_INET6
) {
3929 ip
.ipa_type
= IPADDR_V6
;
3930 memcpy(&(ip
.ipaddr_v6
), &(p
->u
.prefix6
),
3931 sizeof(struct in6_addr
));
3936 zvni_gw_macip_add(ifp
, zvni
, &macaddr
, &ip
);
3938 zvni_gw_macip_del(ifp
, zvni
, &ip
);
3944 * Handle SVI interface going down. At this point, this is a NOP since
3945 * the kernel deletes the neighbor entries on this SVI (if any).
3947 int zebra_vxlan_svi_down(struct interface
*ifp
, struct interface
*link_if
)
3953 * Handle SVI interface coming up. This may or may not be of interest,
3954 * but if this is a SVI on a VxLAN bridge, we need to install any remote
3955 * neighbor entries (which will be used for EVPN ARP suppression).
3957 int zebra_vxlan_svi_up(struct interface
*ifp
, struct interface
*link_if
)
3960 struct neigh_walk_ctx n_wctx
;
3962 zvni
= zvni_map_svi(ifp
, link_if
);
3966 if (!zvni
->vxlan_if
) {
3967 zlog_err("VNI %u hash %p doesn't have intf upon SVI up",
3972 if (IS_ZEBRA_DEBUG_VXLAN
)
3973 zlog_debug("SVI %s(%u) VNI %u is UP, installing neighbors",
3974 ifp
->name
, ifp
->ifindex
, zvni
->vni
);
3976 /* Install any remote neighbors for this VNI. */
3977 memset(&n_wctx
, 0, sizeof(struct neigh_walk_ctx
));
3979 hash_iterate(zvni
->neigh_table
, zvni_install_neigh_hash
, &n_wctx
);
3985 * Handle VxLAN interface down - update BGP if required, and do
3988 int zebra_vxlan_if_down(struct interface
*ifp
)
3990 struct zebra_if
*zif
;
3992 struct zebra_l2info_vxlan
*vxl
;
3995 /* Check if EVPN is enabled. */
3996 if (!is_evpn_enabled())
4001 vxl
= &zif
->l2info
.vxl
;
4004 if (IS_ZEBRA_DEBUG_VXLAN
)
4005 zlog_debug("Intf %s(%u) VNI %u is DOWN",
4006 ifp
->name
, ifp
->ifindex
, vni
);
4008 /* Locate hash entry; it is expected to exist. */
4009 zvni
= zvni_lookup(vni
);
4012 "Failed to locate VNI hash at DOWN, IF %s(%u) VNI %u",
4013 ifp
->name
, ifp
->ifindex
, vni
);
4017 assert(zvni
->vxlan_if
== ifp
);
4019 /* Delete this VNI from BGP. */
4020 zvni_send_del_to_client(zvni
->vni
);
4022 /* Free up all neighbors and MACs, if any. */
4023 zvni_neigh_del_all(zvni
, 1, 0, DEL_ALL_NEIGH
);
4024 zvni_mac_del_all(zvni
, 1, 0, DEL_ALL_MAC
);
4026 /* Free up all remote VTEPs, if any. */
4027 zvni_vtep_del_all(zvni
, 1);
4033 * Handle VxLAN interface up - update BGP if required.
4035 int zebra_vxlan_if_up(struct interface
*ifp
)
4037 struct zebra_if
*zif
;
4039 struct zebra_l2info_vxlan
*vxl
;
4042 /* Check if EVPN is enabled. */
4043 if (!is_evpn_enabled())
4048 vxl
= &zif
->l2info
.vxl
;
4051 if (IS_ZEBRA_DEBUG_VXLAN
)
4052 zlog_debug("Intf %s(%u) VNI %u is UP",
4053 ifp
->name
, ifp
->ifindex
, vni
);
4055 /* Locate hash entry; it is expected to exist. */
4056 zvni
= zvni_lookup(vni
);
4059 "Failed to locate VNI hash at UP, IF %s(%u) VNI %u",
4060 ifp
->name
, ifp
->ifindex
, vni
);
4064 assert(zvni
->vxlan_if
== ifp
);
4066 /* If part of a bridge, inform BGP about this VNI. */
4067 /* Also, read and populate local MACs and neighbors. */
4068 if (zif
->brslave_info
.br_if
) {
4069 zvni_send_add_to_client(zvni
);
4070 zvni_read_mac_neigh(zvni
, ifp
);
4077 * Handle VxLAN interface delete. Locate and remove entry in hash table
4078 * and update BGP, if required.
4080 int zebra_vxlan_if_del(struct interface
*ifp
)
4082 struct zebra_if
*zif
;
4084 struct zebra_l2info_vxlan
*vxl
;
4087 /* Check if EVPN is enabled. */
4088 if (!is_evpn_enabled())
4093 vxl
= &zif
->l2info
.vxl
;
4096 if (IS_ZEBRA_DEBUG_VXLAN
)
4097 zlog_debug("Del VNI %u intf %s(%u)",
4098 vni
, ifp
->name
, ifp
->ifindex
);
4100 /* Locate hash entry; it is expected to exist. */
4101 zvni
= zvni_lookup(vni
);
4104 "Failed to locate VNI hash at del, IF %s(%u) VNI %u",
4105 ifp
->name
, ifp
->ifindex
, vni
);
4109 /* Delete VNI from BGP. */
4110 zvni_send_del_to_client(zvni
->vni
);
4112 /* Free up all neighbors and MAC, if any. */
4113 zvni_neigh_del_all(zvni
, 0, 0, DEL_ALL_NEIGH
);
4114 zvni_mac_del_all(zvni
, 0, 0, DEL_ALL_MAC
);
4116 /* Free up all remote VTEPs, if any. */
4117 zvni_vtep_del_all(zvni
, 0);
4119 /* Delete the hash entry. */
4120 if (zvni_del(zvni
)) {
4121 zlog_err("Failed to del VNI hash %p, IF %s(%u) VNI %u",
4122 zvni
, ifp
->name
, ifp
->ifindex
, zvni
->vni
);
4130 * Handle VxLAN interface update - change to tunnel IP, master or VLAN.
4132 int zebra_vxlan_if_update(struct interface
*ifp
, u_int16_t chgflags
)
4134 struct zebra_if
*zif
;
4136 struct zebra_l2info_vxlan
*vxl
;
4139 /* Check if EVPN is enabled. */
4140 if (!is_evpn_enabled())
4145 vxl
= &zif
->l2info
.vxl
;
4148 /* Update VNI hash. */
4149 zvni
= zvni_lookup(vni
);
4152 "Failed to find VNI hash on update, IF %s(%u) VNI %u",
4153 ifp
->name
, ifp
->ifindex
, vni
);
4157 if (IS_ZEBRA_DEBUG_VXLAN
)
4159 "Update VNI %u intf %s(%u) VLAN %u local IP %s "
4160 "master %u chg 0x%x",
4161 vni
, ifp
->name
, ifp
->ifindex
,
4162 vxl
->access_vlan
, inet_ntoa(vxl
->vtep_ip
),
4163 zif
->brslave_info
.bridge_ifindex
, chgflags
);
4165 /* Removed from bridge? Cleanup and return */
4166 if ((chgflags
& ZEBRA_VXLIF_MASTER_CHANGE
)
4167 && (zif
->brslave_info
.bridge_ifindex
== IFINDEX_INTERNAL
)) {
4168 /* Delete from client, remove all remote VTEPs */
4169 /* Also, free up all MACs and neighbors. */
4170 zvni_send_del_to_client(zvni
->vni
);
4171 zvni_neigh_del_all(zvni
, 1, 0, DEL_ALL_NEIGH
);
4172 zvni_mac_del_all(zvni
, 1, 0, DEL_ALL_MAC
);
4173 zvni_vtep_del_all(zvni
, 1);
4177 /* Handle other changes. */
4178 if (chgflags
& ZEBRA_VXLIF_VLAN_CHANGE
) {
4179 /* Remove all existing local neighbors and MACs for this VNI
4180 * (including from BGP)
4182 zvni_neigh_del_all(zvni
, 0, 1, DEL_LOCAL_MAC
);
4183 zvni_mac_del_all(zvni
, 0, 1, DEL_LOCAL_MAC
);
4186 zvni
->local_vtep_ip
= vxl
->vtep_ip
;
4187 zvni
->vxlan_if
= ifp
;
4189 /* Take further actions needed. Note that if we are here, there is a
4190 * change of interest.
4192 /* If down or not mapped to a bridge, we're done. */
4193 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
4196 /* Inform BGP, if there is a change of interest. */
4198 & (ZEBRA_VXLIF_MASTER_CHANGE
| ZEBRA_VXLIF_LOCAL_IP_CHANGE
))
4199 zvni_send_add_to_client(zvni
);
4201 /* If there is a valid new master or a VLAN mapping change, read and
4202 * populate local MACs and neighbors. Also, reinstall any remote MACs
4203 * and neighbors for this VNI (based on new VLAN).
4205 if (chgflags
& ZEBRA_VXLIF_MASTER_CHANGE
)
4206 zvni_read_mac_neigh(zvni
, ifp
);
4207 else if (chgflags
& ZEBRA_VXLIF_VLAN_CHANGE
) {
4208 struct mac_walk_ctx m_wctx
;
4209 struct neigh_walk_ctx n_wctx
;
4211 zvni_read_mac_neigh(zvni
, ifp
);
4213 memset(&m_wctx
, 0, sizeof(struct mac_walk_ctx
));
4215 hash_iterate(zvni
->mac_table
, zvni_install_mac_hash
, &m_wctx
);
4217 memset(&n_wctx
, 0, sizeof(struct neigh_walk_ctx
));
4219 hash_iterate(zvni
->neigh_table
, zvni_install_neigh_hash
,
4227 * Handle VxLAN interface add.
4229 int zebra_vxlan_if_add(struct interface
*ifp
)
4231 struct zebra_if
*zif
;
4233 struct zebra_l2info_vxlan
*vxl
;
4236 /* Check if EVPN is enabled. */
4237 if (!is_evpn_enabled())
4242 vxl
= &zif
->l2info
.vxl
;
4245 if (IS_ZEBRA_DEBUG_VXLAN
)
4247 "Add VNI %u intf %s(%u) VLAN %u local IP %s master %u",
4248 vni
, ifp
->name
, ifp
->ifindex
,
4249 vxl
->access_vlan
, inet_ntoa(vxl
->vtep_ip
),
4250 zif
->brslave_info
.bridge_ifindex
);
4252 /* Create or update VNI hash. */
4253 zvni
= zvni_lookup(vni
);
4255 zvni
= zvni_add(vni
);
4258 "Failed to add VNI hash, IF %s(%u) VNI %u",
4259 ifp
->name
, ifp
->ifindex
, vni
);
4264 zvni
->local_vtep_ip
= vxl
->vtep_ip
;
4265 zvni
->vxlan_if
= ifp
;
4267 /* If down or not mapped to a bridge, we're done. */
4268 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
4272 zvni_send_add_to_client(zvni
);
4274 /* Read and populate local MACs and neighbors */
4275 zvni_read_mac_neigh(zvni
, ifp
);
4281 * Handle message from client to enable/disable advertisement of g/w macip
4284 int zebra_vxlan_advertise_gw_macip(struct zserv
*client
, int sock
,
4285 u_short length
, struct zebra_vrf
*zvrf
)
4290 zebra_vni_t
*zvni
= NULL
;
4291 struct interface
*ifp
= NULL
;
4293 if (zvrf_id(zvrf
) != VRF_DEFAULT
) {
4294 zlog_err("EVPN GW-MACIP Adv for non-default VRF %u",
4300 advertise
= stream_getc(s
);
4301 vni
= stream_get3(s
);
4304 if (IS_ZEBRA_DEBUG_VXLAN
)
4305 zlog_debug("EVPN gateway macip Adv %s, currently %s",
4306 advertise
? "enabled" : "disabled",
4307 advertise_gw_macip_enabled(NULL
)
4311 if (zvrf
->advertise_gw_macip
== advertise
)
4314 zvrf
->advertise_gw_macip
= advertise
;
4316 if (advertise_gw_macip_enabled(zvni
))
4317 hash_iterate(zvrf
->vni_table
,
4318 zvni_gw_macip_add_for_vni_hash
, NULL
);
4320 hash_iterate(zvrf
->vni_table
,
4321 zvni_gw_macip_del_for_vni_hash
, NULL
);
4324 struct zebra_if
*zif
= NULL
;
4325 struct zebra_l2info_vxlan zl2_info
;
4326 struct interface
*vlan_if
= NULL
;
4327 struct interface
*vrr_if
= NULL
;
4329 if (IS_ZEBRA_DEBUG_VXLAN
)
4331 "EVPN gateway macip Adv %s on VNI %d , currently %s",
4332 advertise
? "enabled" : "disabled", vni
,
4333 advertise_gw_macip_enabled(zvni
)
4337 zvni
= zvni_lookup(vni
);
4341 if (zvni
->advertise_gw_macip
== advertise
)
4344 zvni
->advertise_gw_macip
= advertise
;
4346 ifp
= zvni
->vxlan_if
;
4352 /* If down or not mapped to a bridge, we're done. */
4353 if (!if_is_operative(ifp
) || !zif
->brslave_info
.br_if
)
4356 zl2_info
= zif
->l2info
.vxl
;
4358 vlan_if
= zvni_map_to_svi(zl2_info
.access_vlan
,
4359 zif
->brslave_info
.br_if
);
4363 if (advertise_gw_macip_enabled(zvni
)) {
4364 /* Add primary SVI MAC-IP */
4365 zvni_add_macip_for_intf(vlan_if
, zvni
);
4367 /* Add VRR MAC-IP - if any*/
4368 vrr_if
= zebra_get_vrr_intf_for_svi(vlan_if
);
4370 zvni_add_macip_for_intf(vrr_if
, zvni
);
4372 /* Del primary MAC-IP */
4373 zvni_del_macip_for_intf(vlan_if
, zvni
);
4375 /* Del VRR MAC-IP - if any*/
4376 vrr_if
= zebra_get_vrr_intf_for_svi(vlan_if
);
4378 zvni_del_macip_for_intf(vrr_if
, zvni
);
4387 * Handle message from client to learn (or stop learning) about VNIs and MACs.
4388 * When enabled, the VNI hash table will be built and MAC FDB table read;
4389 * when disabled, the entries should be deleted and remote VTEPs and MACs
4390 * uninstalled from the kernel.
4392 int zebra_vxlan_advertise_all_vni(struct zserv
*client
, int sock
,
4393 u_short length
, struct zebra_vrf
*zvrf
)
4398 if (zvrf_id(zvrf
) != VRF_DEFAULT
) {
4399 zlog_err("EVPN VNI Adv for non-default VRF %u",
4405 advertise
= stream_getc(s
);
4407 if (IS_ZEBRA_DEBUG_VXLAN
)
4408 zlog_debug("EVPN VNI Adv %s, currently %s",
4409 advertise
? "enabled" : "disabled",
4410 is_evpn_enabled() ? "enabled" : "disabled");
4412 if (zvrf
->advertise_all_vni
== advertise
)
4415 zvrf
->advertise_all_vni
= advertise
;
4416 if (is_evpn_enabled()) {
4417 /* Build VNI hash table and inform BGP. */
4418 zvni_build_hash_table();
4420 /* Add all SVI (L3 GW) MACs to BGP*/
4421 hash_iterate(zvrf
->vni_table
, zvni_gw_macip_add_for_vni_hash
,
4424 /* Read the MAC FDB */
4425 macfdb_read(zvrf
->zns
);
4427 /* Read neighbors */
4428 neigh_read(zvrf
->zns
);
4430 /* Cleanup VTEPs for all VNIs - uninstall from
4431 * kernel and free entries.
4433 hash_iterate(zvrf
->vni_table
, zvni_cleanup_all
, zvrf
);
4440 * Allocate VNI hash table for this VRF and do other initialization.
4441 * NOTE: Currently supported only for default VRF.
4443 void zebra_vxlan_init_tables(struct zebra_vrf
*zvrf
)
4447 zvrf
->vni_table
= hash_create(vni_hash_keymake
, vni_hash_cmp
,
4448 "Zebra VRF VNI Table");
4451 /* Close all VNI handling */
4452 void zebra_vxlan_close_tables(struct zebra_vrf
*zvrf
)
4456 hash_iterate(zvrf
->vni_table
, zvni_cleanup_all
, zvrf
);
4457 hash_free(zvrf
->vni_table
);