]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vxlan.c
zebra: resolve link dependencies post nldump
[mirror_frr.git] / zebra / zebra_vxlan.c
CommitLineData
13d60d35 1/*
2 * Zebra EVPN for VxLAN code
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
4 *
5 * This file is part of FRR.
6 *
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * FRR is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
43e52561 25#include "hash.h"
13d60d35 26#include "if.h"
43e52561 27#include "jhash.h"
13d60d35 28#include "linklist.h"
43e52561
QY
29#include "log.h"
30#include "memory.h"
31#include "prefix.h"
13d60d35 32#include "stream.h"
43e52561 33#include "table.h"
13d60d35 34#include "vlan.h"
35#include "vxlan.h"
68e33151
CS
36#ifdef GNU_LINUX
37#include <linux/neighbour.h>
38#endif
13d60d35 39
43e52561
QY
40#include "zebra/debug.h"
41#include "zebra/interface.h"
13d60d35 42#include "zebra/rib.h"
43#include "zebra/rt.h"
43e52561
QY
44#include "zebra/rt_netlink.h"
45#include "zebra/zebra_errors.h"
46#include "zebra/zebra_l2.h"
47#include "zebra/zebra_memory.h"
13d60d35 48#include "zebra/zebra_ns.h"
13d60d35 49#include "zebra/zebra_vrf.h"
13d60d35 50#include "zebra/zebra_vxlan.h"
43e52561
QY
51#include "zebra/zebra_vxlan_private.h"
52#include "zebra/zserv.h"
13d60d35 53
6134fd82 54DEFINE_MTYPE_STATIC(ZEBRA, HOST_PREFIX, "host prefix");
d62a17ae 55DEFINE_MTYPE_STATIC(ZEBRA, ZVNI, "VNI hash");
b7cfce93 56DEFINE_MTYPE_STATIC(ZEBRA, ZL3VNI, "L3 VNI hash");
13d60d35 57DEFINE_MTYPE_STATIC(ZEBRA, ZVNI_VTEP, "VNI remote VTEP");
d62a17ae 58DEFINE_MTYPE_STATIC(ZEBRA, MAC, "VNI MAC");
59DEFINE_MTYPE_STATIC(ZEBRA, NEIGH, "VNI Neighbor");
13d60d35 60
61/* definitions */
62
13d60d35 63/* static function declarations */
996c9314
LB
64static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
65 uint16_t cmd);
cd233079 66static void zvni_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json);
d62a17ae 67static void zvni_print_neigh_hash(struct hash_backet *backet, void *ctxt);
68static void zvni_print_neigh_hash_all_vni(struct hash_backet *backet,
cd233079 69 void **args);
c0e519d3
MK
70static void zl3vni_print_nh(zebra_neigh_t *n, struct vty *vty,
71 json_object *json);
316f4ca4
MK
72static void zl3vni_print_rmac(zebra_mac_t *zrmac, struct vty *vty,
73 json_object *json);
d62a17ae 74static void zvni_print_mac(zebra_mac_t *mac, void *ctxt);
75static void zvni_print_mac_hash(struct hash_backet *backet, void *ctxt);
76static void zvni_print_mac_hash_all_vni(struct hash_backet *backet, void *ctxt);
cd233079
CS
77static void zvni_print(zebra_vni_t *zvni, void **ctxt);
78static void zvni_print_hash(struct hash_backet *backet, void *ctxt[]);
d62a17ae 79
996c9314 80static int zvni_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr,
d7c0a89a 81 struct ipaddr *ip, uint8_t flags,
f07e1c99 82 uint32_t seq, uint16_t cmd);
d62a17ae 83static unsigned int neigh_hash_keymake(void *p);
84static int neigh_cmp(const void *p1, const void *p2);
85static void *zvni_neigh_alloc(void *p);
b6938a74
MK
86static zebra_neigh_t *zvni_neigh_add(zebra_vni_t *zvni, struct ipaddr *ip,
87 struct ethaddr *mac);
d62a17ae 88static int zvni_neigh_del(zebra_vni_t *zvni, zebra_neigh_t *n);
d62a17ae 89static void zvni_neigh_del_from_vtep(zebra_vni_t *zvni, int uninstall,
90 struct in_addr *r_vtep_ip);
996c9314 91static void zvni_neigh_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
d7c0a89a 92 uint32_t flags);
d62a17ae 93static zebra_neigh_t *zvni_neigh_lookup(zebra_vni_t *zvni, struct ipaddr *ip);
996c9314 94static int zvni_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
d7c0a89a 95 struct ethaddr *macaddr,
f07e1c99 96 uint8_t flags, uint32_t seq);
996c9314 97static int zvni_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
d7c0a89a
QY
98 struct ethaddr *macaddr,
99 uint8_t flags);
d62a17ae 100static int zvni_neigh_install(zebra_vni_t *zvni, zebra_neigh_t *n);
101static int zvni_neigh_uninstall(zebra_vni_t *zvni, zebra_neigh_t *n);
b7cfce93 102static zebra_vni_t *zvni_from_svi(struct interface *ifp,
996c9314
LB
103 struct interface *br_if);
104static struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if);
d62a17ae 105
b7cfce93 106/* l3-vni next-hop neigh related APIs */
2dbad57f 107static zebra_neigh_t *zl3vni_nh_lookup(zebra_l3vni_t *zl3vni,
b7cfce93
MK
108 struct ipaddr *ip);
109static void *zl3vni_nh_alloc(void *p);
110static zebra_neigh_t *zl3vni_nh_add(zebra_l3vni_t *zl3vni,
111 struct ipaddr *vtep_ip,
112 struct ethaddr *rmac);
113static int zl3vni_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
114static int zl3vni_nh_install(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
2dbad57f 115static int zl3vni_nh_uninstall(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
b7cfce93
MK
116
117/* l3-vni rmac related APIs */
118static void zl3vni_print_rmac_hash(struct hash_backet *, void *);
2dbad57f 119static zebra_mac_t *zl3vni_rmac_lookup(zebra_l3vni_t *zl3vni,
b7cfce93
MK
120 struct ethaddr *rmac);
121static void *zl3vni_rmac_alloc(void *p);
122static zebra_mac_t *zl3vni_rmac_add(zebra_l3vni_t *zl3vni,
123 struct ethaddr *rmac);
124static int zl3vni_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
125static int zl3vni_rmac_install(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
996c9314 126static int zl3vni_rmac_uninstall(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
b7cfce93
MK
127
128/* l3-vni related APIs*/
b7cfce93
MK
129static zebra_l3vni_t *zl3vni_lookup(vni_t vni);
130static void *zl3vni_alloc(void *p);
131static zebra_l3vni_t *zl3vni_add(vni_t vni, vrf_id_t vrf_id);
132static int zl3vni_del(zebra_l3vni_t *zl3vni);
133static zebra_l3vni_t *zl3vni_from_vrf(vrf_id_t);
b7cfce93
MK
134static struct interface *zl3vni_map_to_svi_if(zebra_l3vni_t *zl3vni);
135static struct interface *zl3vni_map_to_vxlan_if(zebra_l3vni_t *zl3vni);
b7cfce93
MK
136static void zebra_vxlan_process_l3vni_oper_up(zebra_l3vni_t *zl3vni);
137static void zebra_vxlan_process_l3vni_oper_down(zebra_l3vni_t *zl3vni);
138
d62a17ae 139static unsigned int mac_hash_keymake(void *p);
140static int mac_cmp(const void *p1, const void *p2);
141static void *zvni_mac_alloc(void *p);
142static zebra_mac_t *zvni_mac_add(zebra_vni_t *zvni, struct ethaddr *macaddr);
143static int zvni_mac_del(zebra_vni_t *zvni, zebra_mac_t *mac);
d62a17ae 144static void zvni_mac_del_from_vtep(zebra_vni_t *zvni, int uninstall,
145 struct in_addr *r_vtep_ip);
996c9314 146static void zvni_mac_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
d7c0a89a 147 uint32_t flags);
d62a17ae 148static zebra_mac_t *zvni_mac_lookup(zebra_vni_t *zvni, struct ethaddr *macaddr);
996c9314 149static int zvni_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr,
f07e1c99 150 uint8_t flags, uint32_t seq);
996c9314 151static int zvni_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr,
d7c0a89a 152 uint8_t flags);
d62a17ae 153static zebra_vni_t *zvni_map_vlan(struct interface *ifp,
154 struct interface *br_if, vlanid_t vid);
155static int zvni_mac_install(zebra_vni_t *zvni, zebra_mac_t *mac);
d63c1b18 156static int zvni_mac_uninstall(zebra_vni_t *zvni, zebra_mac_t *mac);
d62a17ae 157static void zvni_install_mac_hash(struct hash_backet *backet, void *ctxt);
158
159static unsigned int vni_hash_keymake(void *p);
160static int vni_hash_cmp(const void *p1, const void *p2);
161static void *zvni_alloc(void *p);
2853fed6 162static zebra_vni_t *zvni_lookup(vni_t vni);
163static zebra_vni_t *zvni_add(vni_t vni);
164static int zvni_del(zebra_vni_t *zvni);
165static int zvni_send_add_to_client(zebra_vni_t *zvni);
166static int zvni_send_del_to_client(vni_t vni);
167static void zvni_build_hash_table();
d62a17ae 168static int zvni_vtep_match(struct in_addr *vtep_ip, zebra_vtep_t *zvtep);
169static zebra_vtep_t *zvni_vtep_find(zebra_vni_t *zvni, struct in_addr *vtep_ip);
170static zebra_vtep_t *zvni_vtep_add(zebra_vni_t *zvni, struct in_addr *vtep_ip);
171static int zvni_vtep_del(zebra_vni_t *zvni, zebra_vtep_t *zvtep);
172static int zvni_vtep_del_all(zebra_vni_t *zvni, int uninstall);
173static int zvni_vtep_install(zebra_vni_t *zvni, struct in_addr *vtep_ip);
174static int zvni_vtep_uninstall(zebra_vni_t *zvni, struct in_addr *vtep_ip);
1a98c087
MK
175static int zvni_del_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni);
176static int zvni_add_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni);
177static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni,
178 struct ethaddr *macaddr, struct ipaddr *ip);
179static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni,
180 struct ipaddr *ip);
181struct interface *zebra_get_vrr_intf_for_svi(struct interface *ifp);
2853fed6 182static int advertise_gw_macip_enabled(zebra_vni_t *zvni);
fe697c6b 183static int remote_neigh_count(zebra_mac_t *zmac);
184static void zvni_deref_ip2mac(zebra_vni_t *zvni, zebra_mac_t *mac);
13d60d35 185
186/* Private functions */
5e1b0650 187static int host_rb_entry_compare(const struct host_rb_entry *hle1,
85442b09 188 const struct host_rb_entry *hle2)
5e1b0650
DS
189{
190 if (hle1->p.family < hle2->p.family)
191 return -1;
192
193 if (hle1->p.family > hle2->p.family)
194 return 1;
195
196 if (hle1->p.prefixlen < hle2->p.prefixlen)
197 return -1;
198
199 if (hle1->p.prefixlen > hle2->p.prefixlen)
200 return 1;
201
202 if (hle1->p.family == AF_INET) {
203 if (hle1->p.u.prefix4.s_addr < hle2->p.u.prefix4.s_addr)
204 return -1;
205
206 if (hle1->p.u.prefix4.s_addr > hle2->p.u.prefix4.s_addr)
207 return 1;
208
209 return 0;
2acc2310
DS
210 } else if (hle1->p.family == AF_INET6) {
211 return memcmp(&hle1->p.u.prefix6, &hle2->p.u.prefix6,
212 IPV6_MAX_BYTELEN);
5e1b0650 213 } else {
9df414fe
QY
214 zlog_debug("%s: Unexpected family type: %d",
215 __PRETTY_FUNCTION__, hle1->p.family);
5e1b0650
DS
216 return 0;
217 }
218}
85442b09 219RB_GENERATE(host_rb_tree_entry, host_rb_entry, hl_entry, host_rb_entry_compare);
5e1b0650 220
85442b09 221static uint32_t rb_host_count(struct host_rb_tree_entry *hrbe)
5e1b0650
DS
222{
223 struct host_rb_entry *hle;
224 uint32_t count = 0;
225
85442b09 226 RB_FOREACH (hle, host_rb_tree_entry, hrbe)
5e1b0650
DS
227 count++;
228
229 return count;
230}
13d60d35 231
790f8dc5 232/*
233 * Return number of valid MACs in a VNI's MAC hash table - all
234 * remote MACs and non-internal (auto) local MACs count.
235 */
d7c0a89a 236static uint32_t num_valid_macs(zebra_vni_t *zvni)
790f8dc5 237{
238 unsigned int i;
d7c0a89a 239 uint32_t num_macs = 0;
790f8dc5 240 struct hash *hash;
241 struct hash_backet *hb;
242 zebra_mac_t *mac;
243
244 hash = zvni->mac_table;
245 if (!hash)
246 return num_macs;
247 for (i = 0; i < hash->size; i++) {
248 for (hb = hash->index[i]; hb; hb = hb->next) {
249 mac = (zebra_mac_t *)hb->data;
250 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
63ce2ee7 251 || CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
790f8dc5 252 || !CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
253 num_macs++;
254 }
255 }
256
257 return num_macs;
258}
259
2853fed6 260static int advertise_gw_macip_enabled(zebra_vni_t *zvni)
1a98c087 261{
2853fed6 262 struct zebra_vrf *zvrf;
263
264 zvrf = vrf_info_lookup(VRF_DEFAULT);
1a98c087
MK
265 if (zvrf && zvrf->advertise_gw_macip)
266 return 1;
267
268 if (zvni && zvni->advertise_gw_macip)
269 return 1;
270
271 return 0;
272}
273
cec2e17d 274/*
275 * Helper function to determine maximum width of neighbor IP address for
276 * display - just because we're dealing with IPv6 addresses that can
277 * widely vary.
278 */
d62a17ae 279static void zvni_find_neigh_addr_width(struct hash_backet *backet, void *ctxt)
cec2e17d 280{
d62a17ae 281 zebra_neigh_t *n;
282 char buf[INET6_ADDRSTRLEN];
283 struct neigh_walk_ctx *wctx = ctxt;
284 int width;
cec2e17d 285
d62a17ae 286 n = (zebra_neigh_t *)backet->data;
cec2e17d 287
1445b43c
A
288 ipaddr2str(&n->ip, buf, sizeof(buf));
289 width = strlen(buf);
d62a17ae 290 if (width > wctx->addr_width)
291 wctx->addr_width = width;
68e33151 292
cec2e17d 293}
294
295/*
296 * Print a specific neighbor entry.
297 */
cd233079 298static void zvni_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json)
cec2e17d 299{
d62a17ae 300 struct vty *vty;
301 char buf1[ETHER_ADDR_STRLEN];
302 char buf2[INET6_ADDRSTRLEN];
1a8c5c38 303 const char *type_str;
304 const char *state_str;
305 bool flags_present = false;
cec2e17d 306
cd233079
CS
307 ipaddr2str(&n->ip, buf2, sizeof(buf2));
308 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
1a8c5c38 309 type_str = CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) ?
310 "local" : "remote";
311 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
cd233079
CS
312 vty = (struct vty *)ctxt;
313 if (json == NULL) {
314 vty_out(vty, "IP: %s\n",
315 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
1a8c5c38 316 vty_out(vty, " Type: %s\n", type_str);
317 vty_out(vty, " State: %s\n", state_str);
318 vty_out(vty, " MAC: %s\n",
cd233079
CS
319 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
320 } else {
321 json_object_string_add(json, "ip", buf2);
1a8c5c38 322 json_object_string_add(json, "type", type_str);
323 json_object_string_add(json, "state", state_str);
cd233079
CS
324 json_object_string_add(json, "mac", buf1);
325 }
326 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
b6938a74 327 if (json == NULL) {
1a8c5c38 328 vty_out(vty, " Remote VTEP: %s\n",
cd233079 329 inet_ntoa(n->r_vtep_ip));
b6938a74 330 } else
cd233079
CS
331 json_object_string_add(json, "remoteVtep",
332 inet_ntoa(n->r_vtep_ip));
333 }
ead40654 334 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW)) {
1a8c5c38 335 if (!json) {
336 vty_out(vty, " Flags: Default-gateway");
337 flags_present = true;
338 } else
ead40654
MK
339 json_object_boolean_true_add(json, "defaultGateway");
340 }
68e33151 341 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)) {
1a8c5c38 342 if (!json) {
343 vty_out(vty,
344 flags_present ? " ,Router" : " Flags: Router");
345 flags_present = true;
346 }
68e33151 347 }
f07e1c99 348 if (json == NULL) {
1a8c5c38 349 if (flags_present)
350 vty_out(vty, "\n");
351 vty_out(vty, " Local Seq: %u Remote Seq: %u\n",
f07e1c99 352 n->loc_seq, n->rem_seq);
1a8c5c38 353 } else {
354 json_object_int_add(json, "localSequence", n->loc_seq);
355 json_object_int_add(json, "remoteSequence", n->rem_seq);
68e33151 356 }
cec2e17d 357}
358
359/*
360 * Print neighbor hash entry - called for display of all neighbors.
361 */
d62a17ae 362static void zvni_print_neigh_hash(struct hash_backet *backet, void *ctxt)
363{
364 struct vty *vty;
cd233079 365 json_object *json_vni = NULL, *json_row = NULL;
d62a17ae 366 zebra_neigh_t *n;
367 char buf1[ETHER_ADDR_STRLEN];
368 char buf2[INET6_ADDRSTRLEN];
369 struct neigh_walk_ctx *wctx = ctxt;
1a8c5c38 370 const char *state_str;
d62a17ae 371
372 vty = wctx->vty;
cd233079 373 json_vni = wctx->json;
d62a17ae 374 n = (zebra_neigh_t *)backet->data;
d62a17ae 375
cd233079
CS
376 if (json_vni)
377 json_row = json_object_new_object();
378
d62a17ae 379 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
380 ipaddr2str(&n->ip, buf2, sizeof(buf2));
1a8c5c38 381 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
382 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
383 if (wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
384 return;
385
cd233079 386 if (json_vni == NULL) {
1a8c5c38 387 vty_out(vty, "%*s %-6s %-8s %-17s\n",
388 -wctx->addr_width, buf2, "local",
389 state_str, buf1);
cd233079
CS
390 } else {
391 json_object_string_add(json_row, "type", "local");
1a8c5c38 392 json_object_string_add(json_row, "state", state_str);
cd233079 393 json_object_string_add(json_row, "mac", buf1);
1a8c5c38 394 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
395 json_object_boolean_true_add(
396 json_row, "defaultGateway");
397 json_object_int_add(json_row, "localSequence",
398 n->loc_seq);
399 json_object_int_add(json_row, "remoteSequence",
400 n->rem_seq);
cd233079 401 }
d62a17ae 402 wctx->count++;
1a8c5c38 403 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
404 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP) &&
405 !IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))
406 return;
407
408 if (json_vni == NULL) {
409 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP) &&
410 (wctx->count == 0))
411 vty_out(vty,
412 "%*s %-6s %-8s %-17s %-21s\n",
413 -wctx->addr_width, "Neighbor", "Type",
414 "State", "MAC", "Remote VTEP");
415 vty_out(vty, "%*s %-6s %-8s %-17s %-21s\n",
416 -wctx->addr_width, buf2, "remote", state_str,
417 buf1, inet_ntoa(n->r_vtep_ip));
cd233079 418 } else {
1a8c5c38 419 json_object_string_add(json_row, "type", "remote");
420 json_object_string_add(json_row, "state", state_str);
421 json_object_string_add(json_row, "mac", buf1);
422 json_object_string_add(json_row, "remoteVtep",
423 inet_ntoa(n->r_vtep_ip));
424 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
425 json_object_boolean_true_add(json_row,
426 "defaultGateway");
427 json_object_int_add(json_row, "localSequence",
428 n->loc_seq);
429 json_object_int_add(json_row, "remoteSequence",
430 n->rem_seq);
d62a17ae 431 }
1a8c5c38 432 wctx->count++;
d62a17ae 433 }
cd233079
CS
434
435 if (json_vni)
436 json_object_object_add(json_vni, buf2, json_row);
cec2e17d 437}
438
439/*
440 * Print neighbors for all VNI.
441 */
d62a17ae 442static void zvni_print_neigh_hash_all_vni(struct hash_backet *backet,
cd233079 443 void **args)
cec2e17d 444{
d62a17ae 445 struct vty *vty;
cd233079 446 json_object *json = NULL, *json_vni = NULL;
d62a17ae 447 zebra_vni_t *zvni;
d7c0a89a 448 uint32_t num_neigh;
d62a17ae 449 struct neigh_walk_ctx wctx;
cd233079
CS
450 char vni_str[VNI_STR_LEN];
451
452 vty = (struct vty *)args[0];
453 json = (json_object *)args[1];
cec2e17d 454
d62a17ae 455 zvni = (zebra_vni_t *)backet->data;
9ea660be 456
d62a17ae 457 num_neigh = hashcount(zvni->neigh_table);
68e33151 458 if (json == NULL) {
cd233079
CS
459 vty_out(vty,
460 "\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
461 zvni->vni, num_neigh);
68e33151 462 } else {
cd233079
CS
463 json_vni = json_object_new_object();
464 json_object_int_add(json_vni, "numArpNd", num_neigh);
465 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
466 }
467 if (!num_neigh) {
468 if (json)
469 json_object_object_add(json, vni_str, json_vni);
d62a17ae 470 return;
cd233079 471 }
cec2e17d 472
d62a17ae 473 /* Since we have IPv6 addresses to deal with which can vary widely in
474 * size, we try to be a bit more elegant in display by first computing
475 * the maximum width.
476 */
477 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
478 wctx.zvni = zvni;
479 wctx.vty = vty;
480 wctx.addr_width = 15;
cd233079 481 wctx.json = json_vni;
d62a17ae 482 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
cec2e17d 483
68e33151 484 if (json == NULL) {
1a8c5c38 485 vty_out(vty, "%*s %-6s %-8s %-17s %-21s\n",
486 -wctx.addr_width, "IP", "Type",
487 "State", "MAC", "Remote VTEP");
68e33151 488 }
d62a17ae 489 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
cd233079
CS
490
491 if (json)
492 json_object_object_add(json, vni_str, json_vni);
cec2e17d 493}
494
9aa741ea 495/* print a specific next hop for an l3vni */
996c9314 496static void zl3vni_print_nh(zebra_neigh_t *n, struct vty *vty,
c0e519d3 497 json_object *json)
9aa741ea
MK
498{
499 char buf1[ETHER_ADDR_STRLEN];
500 char buf2[INET6_ADDRSTRLEN];
c0e519d3 501 json_object *json_hosts = NULL;
f2a503f0 502 struct host_rb_entry *hle;
9aa741ea 503
c0e519d3
MK
504 if (!json) {
505 vty_out(vty, "Ip: %s\n",
506 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
507 vty_out(vty, " RMAC: %s\n",
996c9314 508 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
f2a503f0
DS
509 vty_out(vty, " Refcount: %d\n",
510 rb_host_count(&n->host_rb));
4cce389e 511 vty_out(vty, " Prefixes:\n");
85442b09 512 RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
c0e519d3 513 vty_out(vty, " %s\n",
f2a503f0 514 prefix2str(&hle->p, buf2, sizeof(buf2)));
c0e519d3
MK
515 } else {
516 json_hosts = json_object_new_array();
996c9314
LB
517 json_object_string_add(
518 json, "ip", ipaddr2str(&(n->ip), buf2, sizeof(buf2)));
519 json_object_string_add(
520 json, "routerMac",
521 prefix_mac2str(&n->emac, buf2, sizeof(buf2)));
f2a503f0
DS
522 json_object_int_add(json, "refCount",
523 rb_host_count(&n->host_rb));
85442b09 524 RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
c0e519d3 525 json_object_array_add(json_hosts,
996c9314 526 json_object_new_string(prefix2str(
f2a503f0 527 &hle->p, buf2, sizeof(buf2))));
4cce389e 528 json_object_object_add(json, "prefixList", json_hosts);
c0e519d3 529 }
9aa741ea
MK
530}
531
532/* Print a specific RMAC entry */
996c9314 533static void zl3vni_print_rmac(zebra_mac_t *zrmac, struct vty *vty,
316f4ca4 534 json_object *json)
9aa741ea
MK
535{
536 char buf1[ETHER_ADDR_STRLEN];
537 char buf2[PREFIX_STRLEN];
316f4ca4 538 json_object *json_hosts = NULL;
5e1b0650 539 struct host_rb_entry *hle;
9aa741ea 540
316f4ca4
MK
541 if (!json) {
542 vty_out(vty, "MAC: %s\n",
543 prefix_mac2str(&zrmac->macaddr, buf1, sizeof(buf1)));
544 vty_out(vty, " Remote VTEP: %s\n",
545 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
41db76c2 546 vty_out(vty, " Refcount: %d\n", rb_host_count(&zrmac->host_rb));
4cce389e 547 vty_out(vty, " Prefixes:\n");
85442b09 548 RB_FOREACH (hle, host_rb_tree_entry, &zrmac->host_rb)
316f4ca4 549 vty_out(vty, " %s\n",
5e1b0650 550 prefix2str(&hle->p, buf2, sizeof(buf2)));
316f4ca4
MK
551 } else {
552 json_hosts = json_object_new_array();
996c9314
LB
553 json_object_string_add(
554 json, "routerMac",
555 prefix_mac2str(&zrmac->macaddr, buf1, sizeof(buf1)));
4cce389e 556 json_object_string_add(json, "vtepIp",
316f4ca4 557 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
41db76c2
DS
558 json_object_int_add(json, "refCount",
559 rb_host_count(&zrmac->host_rb));
1a8c5c38 560 json_object_int_add(json, "localSequence", zrmac->loc_seq);
561 json_object_int_add(json, "remoteSequence", zrmac->rem_seq);
85442b09 562 RB_FOREACH (hle, host_rb_tree_entry, &zrmac->host_rb)
5e1b0650
DS
563 json_object_array_add(
564 json_hosts,
565 json_object_new_string(prefix2str(
566 &hle->p, buf2, sizeof(buf2))));
4cce389e 567 json_object_object_add(json, "prefixList", json_hosts);
316f4ca4 568 }
9aa741ea
MK
569}
570
cec2e17d 571/*
572 * Print a specific MAC entry.
573 */
d62a17ae 574static void zvni_print_mac(zebra_mac_t *mac, void *ctxt)
575{
576 struct vty *vty;
b6938a74
MK
577 zebra_neigh_t *n = NULL;
578 struct listnode *node = NULL;
d62a17ae 579 char buf1[20];
b6938a74 580 char buf2[INET6_ADDRSTRLEN];
d62a17ae 581
582 vty = (struct vty *)ctxt;
583 vty_out(vty, "MAC: %s",
584 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1)));
585 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
586 struct zebra_ns *zns;
587 struct interface *ifp;
588 ifindex_t ifindex;
589
590 ifindex = mac->fwd_info.local.ifindex;
591 zns = zebra_ns_lookup(NS_DEFAULT);
592 ifp = if_lookup_by_index_per_ns(zns, ifindex);
593 if (!ifp) // unexpected
594 return;
595 vty_out(vty, " Intf: %s(%u)", ifp->name, ifindex);
596 if (mac->fwd_info.local.vid)
597 vty_out(vty, " VLAN: %u", mac->fwd_info.local.vid);
b6938a74 598 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
d62a17ae 599 vty_out(vty, " Remote VTEP: %s",
600 inet_ntoa(mac->fwd_info.r_vtep_ip));
b6938a74
MK
601 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
602 vty_out(vty, " Auto Mac ");
603 }
b6938a74 604
ead40654
MK
605 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
606 vty_out(vty, " Sticky Mac ");
607
608 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
609 vty_out(vty, " Default-gateway Mac ");
610
51f4dab4
AK
611 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
612 vty_out(vty, " Remote-gateway Mac ");
613
0a97666d 614 vty_out(vty, "\n");
f07e1c99 615 vty_out(vty, " Local Seq: %u Remote Seq: %u",
616 mac->loc_seq, mac->rem_seq);
617 vty_out(vty, "\n");
618
b6938a74
MK
619 /* print all the associated neigh */
620 vty_out(vty, " Neighbors:\n");
621 if (!listcount(mac->neigh_list))
622 vty_out(vty, " No Neighbors\n");
623 else {
624 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
625 vty_out(vty, " %s %s\n",
626 ipaddr2str(&n->ip, buf2, sizeof(buf2)),
f07e1c99 627 (IS_ZEBRA_NEIGH_ACTIVE(n)
628 ? "Active" : "Inactive"));
b6938a74 629 }
d62a17ae 630 }
b6938a74 631
d62a17ae 632 vty_out(vty, "\n");
cec2e17d 633}
634
635/*
636 * Print MAC hash entry - called for display of all MACs.
637 */
d62a17ae 638static void zvni_print_mac_hash(struct hash_backet *backet, void *ctxt)
639{
640 struct vty *vty;
cd233079 641 json_object *json_mac_hdr = NULL, *json_mac = NULL;
d62a17ae 642 zebra_mac_t *mac;
643 char buf1[20];
644 struct mac_walk_ctx *wctx = ctxt;
645
646 vty = wctx->vty;
cd233079 647 json_mac_hdr = wctx->json;
d62a17ae 648 mac = (zebra_mac_t *)backet->data;
d62a17ae 649
650 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
cd233079
CS
651
652 if (json_mac_hdr)
653 json_mac = json_object_new_object();
654
1a8c5c38 655 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
d62a17ae 656 struct zebra_ns *zns;
657 ifindex_t ifindex;
658 struct interface *ifp;
659 vlanid_t vid;
660
1a8c5c38 661 if (wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP)
662 return;
663
d62a17ae 664 zns = zebra_ns_lookup(NS_DEFAULT);
665 ifindex = mac->fwd_info.local.ifindex;
666 ifp = if_lookup_by_index_per_ns(zns, ifindex);
667 if (!ifp) // unexpected
668 return;
669 vid = mac->fwd_info.local.vid;
cd233079
CS
670 if (json_mac_hdr == NULL)
671 vty_out(vty, "%-17s %-6s %-21s", buf1, "local",
672 ifp->name);
673 else {
674 json_object_string_add(json_mac, "type", "local");
675 json_object_string_add(json_mac, "intf", ifp->name);
676 }
677 if (vid) {
678 if (json_mac_hdr == NULL)
679 vty_out(vty, " %-5u", vid);
680 else
681 json_object_int_add(json_mac, "vlan", vid);
682 }
1a8c5c38 683 if (json_mac_hdr == NULL) {
cd233079 684 vty_out(vty, "\n");
1a8c5c38 685 } else {
686 json_object_int_add(json_mac, "localSequence",
687 mac->loc_seq);
688 json_object_int_add(json_mac, "remoteSequence",
689 mac->rem_seq);
cd233079 690 json_object_object_add(json_mac_hdr, buf1, json_mac);
1a8c5c38 691 }
692
d62a17ae 693 wctx->count++;
1a8c5c38 694
b6938a74 695 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1a8c5c38 696
697 if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) &&
698 !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip,
699 &wctx->r_vtep_ip))
700 return;
701
702 if (json_mac_hdr == NULL) {
703 if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) &&
704 (wctx->count == 0)) {
705 vty_out(vty, "\nVNI %u\n\n", wctx->zvni->vni);
706 vty_out(vty, "%-17s %-6s %-21s %-5s\n", "MAC",
707 "Type", "Intf/Remote VTEP", "VLAN");
d62a17ae 708 }
1a8c5c38 709 vty_out(vty, "%-17s %-6s %-21s\n", buf1, "remote",
710 inet_ntoa(mac->fwd_info.r_vtep_ip));
d62a17ae 711 } else {
1a8c5c38 712 json_object_string_add(json_mac, "type", "remote");
713 json_object_string_add(json_mac, "remoteVtep",
cd233079 714 inet_ntoa(mac->fwd_info.r_vtep_ip));
1a8c5c38 715 json_object_object_add(json_mac_hdr, buf1, json_mac);
716 json_object_int_add(json_mac, "localSequence",
717 mac->loc_seq);
718 json_object_int_add(json_mac, "remoteSequence",
719 mac->rem_seq);
d62a17ae 720 }
1a8c5c38 721
722 wctx->count++;
d62a17ae 723 }
cec2e17d 724}
725
726/*
727 * Print MACs for all VNI.
728 */
d62a17ae 729static void zvni_print_mac_hash_all_vni(struct hash_backet *backet, void *ctxt)
cec2e17d 730{
d62a17ae 731 struct vty *vty;
cd233079
CS
732 json_object *json = NULL, *json_vni = NULL;
733 json_object *json_mac = NULL;
d62a17ae 734 zebra_vni_t *zvni;
d7c0a89a 735 uint32_t num_macs;
d62a17ae 736 struct mac_walk_ctx *wctx = ctxt;
cd233079 737 char vni_str[VNI_STR_LEN];
cec2e17d 738
d62a17ae 739 vty = (struct vty *)wctx->vty;
cd233079 740 json = (struct json_object *)wctx->json;
cec2e17d 741
d62a17ae 742 zvni = (zebra_vni_t *)backet->data;
d62a17ae 743 wctx->zvni = zvni;
cec2e17d 744
d62a17ae 745 /*We are iterating over a new VNI, set the count to 0*/
746 wctx->count = 0;
cec2e17d 747
790f8dc5 748 num_macs = num_valid_macs(zvni);
d62a17ae 749 if (!num_macs)
750 return;
cd233079
CS
751
752 if (json) {
753 json_vni = json_object_new_object();
754 json_mac = json_object_new_object();
755 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
d62a17ae 756 }
cec2e17d 757
cd233079
CS
758 if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
759 if (json == NULL) {
760 vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
761 zvni->vni, num_macs);
762 vty_out(vty, "%-17s %-6s %-21s %-5s\n", "MAC", "Type",
763 "Intf/Remote VTEP", "VLAN");
764 } else
765 json_object_int_add(json_vni, "numMacs", num_macs);
766 }
767 /* assign per-vni to wctx->json object to fill macs
768 * under the vni. Re-assign primary json object to fill
769 * next vni information.
770 */
771 wctx->json = json_mac;
d62a17ae 772 hash_iterate(zvni->mac_table, zvni_print_mac_hash, wctx);
cd233079
CS
773 wctx->json = json;
774 if (json) {
775 if (wctx->count)
776 json_object_object_add(json_vni, "macs", json_mac);
777 json_object_object_add(json, vni_str, json_vni);
778 }
cec2e17d 779}
780
996c9314 781static void zl3vni_print_nh_hash(struct hash_backet *backet, void *ctx)
b7cfce93
MK
782{
783 struct nh_walk_ctx *wctx = NULL;
784 struct vty *vty = NULL;
32798965 785 struct json_object *json_vni = NULL;
b7cfce93
MK
786 struct json_object *json_nh = NULL;
787 zebra_neigh_t *n = NULL;
788 char buf1[ETHER_ADDR_STRLEN];
2dbad57f 789 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
790
791 wctx = (struct nh_walk_ctx *)ctx;
792 vty = wctx->vty;
32798965
MK
793 json_vni = wctx->json;
794 if (json_vni)
b7cfce93
MK
795 json_nh = json_object_new_object();
796 n = (zebra_neigh_t *)backet->data;
b7cfce93 797
32798965 798 if (!json_vni) {
4cce389e 799 vty_out(vty, "%-15s %-17s\n",
2dbad57f 800 ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
4cce389e 801 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
b7cfce93 802 } else {
4cce389e 803 json_object_string_add(json_nh, "nexthopIp",
32798965 804 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
996c9314
LB
805 json_object_string_add(
806 json_nh, "routerMac",
807 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
32798965
MK
808 json_object_object_add(json_vni,
809 ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
810 json_nh);
b7cfce93
MK
811 }
812}
813
2dbad57f 814static void zl3vni_print_nh_hash_all_vni(struct hash_backet *backet,
32798965 815 void **args)
2dbad57f 816{
817 struct vty *vty = NULL;
818 json_object *json = NULL;
819 json_object *json_vni = NULL;
2dbad57f 820 zebra_l3vni_t *zl3vni = NULL;
821 uint32_t num_nh = 0;
32798965 822 struct nh_walk_ctx wctx;
2dbad57f 823 char vni_str[VNI_STR_LEN];
824
32798965
MK
825 vty = (struct vty *)args[0];
826 json = (struct json_object *)args[1];
2dbad57f 827
828 zl3vni = (zebra_l3vni_t *)backet->data;
2dbad57f 829
830 num_nh = hashcount(zl3vni->nh_table);
831 if (!num_nh)
832 return;
833
834 if (json) {
835 json_vni = json_object_new_object();
2dbad57f 836 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
837 }
838
839 if (json == NULL) {
996c9314 840 vty_out(vty, "\nVNI %u #Next-Hops %u\n\n", zl3vni->vni, num_nh);
4cce389e 841 vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
2dbad57f 842 } else
4cce389e 843 json_object_int_add(json_vni, "numNextHops", num_nh);
2dbad57f 844
32798965
MK
845 memset(&wctx, 0, sizeof(struct nh_walk_ctx));
846 wctx.vty = vty;
847 wctx.json = json_vni;
848 hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
849 if (json)
2dbad57f 850 json_object_object_add(json, vni_str, json_vni);
2dbad57f 851}
852
b7cfce93 853static void zl3vni_print_rmac_hash_all_vni(struct hash_backet *backet,
c0b4eaa4 854 void **args)
b7cfce93
MK
855{
856 struct vty *vty = NULL;
857 json_object *json = NULL;
858 json_object *json_vni = NULL;
b7cfce93 859 zebra_l3vni_t *zl3vni = NULL;
d7c0a89a 860 uint32_t num_rmacs;
c0b4eaa4 861 struct rmac_walk_ctx wctx;
b7cfce93
MK
862 char vni_str[VNI_STR_LEN];
863
c0b4eaa4
MK
864 vty = (struct vty *)args[0];
865 json = (struct json_object *)args[1];
b7cfce93
MK
866
867 zl3vni = (zebra_l3vni_t *)backet->data;
b7cfce93
MK
868
869 num_rmacs = hashcount(zl3vni->rmac_table);
870 if (!num_rmacs)
871 return;
872
873 if (json) {
874 json_vni = json_object_new_object();
b7cfce93
MK
875 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
876 }
877
878 if (json == NULL) {
996c9314 879 vty_out(vty, "\nVNI %u #RMACs %u\n\n", zl3vni->vni, num_rmacs);
4cce389e 880 vty_out(vty, "%-17s %-21s\n", "RMAC", "Remote VTEP");
b7cfce93
MK
881 } else
882 json_object_int_add(json_vni, "numRmacs", num_rmacs);
883
884 /* assign per-vni to wctx->json object to fill macs
885 * under the vni. Re-assign primary json object to fill
886 * next vni information.
887 */
c0b4eaa4
MK
888 memset(&wctx, 0, sizeof(struct rmac_walk_ctx));
889 wctx.vty = vty;
890 wctx.json = json_vni;
891 hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
892 if (json)
b7cfce93 893 json_object_object_add(json, vni_str, json_vni);
b7cfce93
MK
894}
895
996c9314 896static void zl3vni_print_rmac_hash(struct hash_backet *backet, void *ctx)
b7cfce93
MK
897{
898 zebra_mac_t *zrmac = NULL;
899 struct rmac_walk_ctx *wctx = NULL;
900 struct vty *vty = NULL;
901 struct json_object *json = NULL;
902 struct json_object *json_rmac = NULL;
903 char buf[ETHER_ADDR_STRLEN];
904
905 wctx = (struct rmac_walk_ctx *)ctx;
906 vty = wctx->vty;
907 json = wctx->json;
908 if (json)
909 json_rmac = json_object_new_object();
910 zrmac = (zebra_mac_t *)backet->data;
b7cfce93
MK
911
912 if (!json) {
4cce389e 913 vty_out(vty, "%-17s %-21s\n",
b7cfce93 914 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
996c9314 915 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
b7cfce93 916 } else {
996c9314
LB
917 json_object_string_add(
918 json_rmac, "routerMac",
919 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)));
4cce389e 920 json_object_string_add(json_rmac, "vtepIp",
b7cfce93 921 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
996c9314
LB
922 json_object_object_add(
923 json, prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
924 json_rmac);
b7cfce93
MK
925 }
926}
927
928/* print a specific L3 VNI entry */
929static void zl3vni_print(zebra_l3vni_t *zl3vni, void **ctx)
930{
931 char buf[ETHER_ADDR_STRLEN];
932 struct vty *vty = NULL;
933 json_object *json = NULL;
934 zebra_vni_t *zvni = NULL;
935 json_object *json_vni_list = NULL;
936 struct listnode *node = NULL, *nnode = NULL;
937
938 vty = ctx[0];
939 json = ctx[1];
940
941 if (!json) {
942 vty_out(vty, "VNI: %u\n", zl3vni->vni);
4cce389e 943 vty_out(vty, " Type: %s\n", "L3");
996c9314 944 vty_out(vty, " Tenant VRF: %s\n", zl3vni_vrf_name(zl3vni));
4cce389e 945 vty_out(vty, " Local Vtep Ip: %s\n",
b67a60d2 946 inet_ntoa(zl3vni->local_vtep_ip));
b7cfce93
MK
947 vty_out(vty, " Vxlan-Intf: %s\n",
948 zl3vni_vxlan_if_name(zl3vni));
996c9314
LB
949 vty_out(vty, " SVI-If: %s\n", zl3vni_svi_if_name(zl3vni));
950 vty_out(vty, " State: %s\n", zl3vni_state2str(zl3vni));
c48d9f5f 951 vty_out(vty, " VNI Filter: %s\n",
996c9314
LB
952 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
953 ? "prefix-routes-only"
954 : "none");
4cce389e 955 vty_out(vty, " Router MAC: %s\n",
b7cfce93 956 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
4cce389e 957 vty_out(vty, " L2 VNIs: ");
b7cfce93
MK
958 for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zvni))
959 vty_out(vty, "%u ", zvni->vni);
960 vty_out(vty, "\n");
961 } else {
962 json_vni_list = json_object_new_array();
963 json_object_int_add(json, "vni", zl3vni->vni);
4cce389e
MK
964 json_object_string_add(json, "type", "L3");
965 json_object_string_add(json, "localVtepIp",
b67a60d2 966 inet_ntoa(zl3vni->local_vtep_ip));
4cce389e 967 json_object_string_add(json, "vxlanIntf",
b7cfce93 968 zl3vni_vxlan_if_name(zl3vni));
4cce389e 969 json_object_string_add(json, "sviIntf",
b7cfce93 970 zl3vni_svi_if_name(zl3vni));
996c9314
LB
971 json_object_string_add(json, "state", zl3vni_state2str(zl3vni));
972 json_object_string_add(json, "vrf", zl3vni_vrf_name(zl3vni));
973 json_object_string_add(
974 json, "routerMac",
975 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
976 json_object_string_add(
977 json, "vniFilter",
978 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
979 ? "prefix-routes-only"
980 : "none");
b7cfce93
MK
981 for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zvni)) {
982 json_object_array_add(json_vni_list,
983 json_object_new_int(zvni->vni));
984 }
4cce389e 985 json_object_object_add(json, "l2Vnis", json_vni_list);
b7cfce93
MK
986 }
987}
988
cec2e17d 989/*
990 * Print a specific VNI entry.
991 */
cd233079 992static void zvni_print(zebra_vni_t *zvni, void **ctxt)
d62a17ae 993{
994 struct vty *vty;
995 zebra_vtep_t *zvtep;
d7c0a89a
QY
996 uint32_t num_macs;
997 uint32_t num_neigh;
cd233079
CS
998 json_object *json = NULL;
999 json_object *json_vtep_list = NULL;
1000 json_object *json_ip_str = NULL;
d62a17ae 1001
cd233079
CS
1002 vty = ctxt[0];
1003 json = ctxt[1];
1004
b7cfce93 1005 if (json == NULL) {
cd233079 1006 vty_out(vty, "VNI: %u\n", zvni->vni);
4cce389e
MK
1007 vty_out(vty, " Type: %s\n", "L2");
1008 vty_out(vty, " Tenant VRF: %s\n", vrf_id_to_name(zvni->vrf_id));
b7cfce93 1009 } else {
cd233079 1010 json_object_int_add(json, "vni", zvni->vni);
4cce389e 1011 json_object_string_add(json, "type", "L2");
b7cfce93
MK
1012 json_object_string_add(json, "vrf",
1013 vrf_id_to_name(zvni->vrf_id));
1014 }
d62a17ae 1015
d62a17ae 1016 if (!zvni->vxlan_if) { // unexpected
cd233079
CS
1017 if (json == NULL)
1018 vty_out(vty, " VxLAN interface: unknown\n");
d62a17ae 1019 return;
1020 }
790f8dc5 1021 num_macs = num_valid_macs(zvni);
cd233079 1022 num_neigh = hashcount(zvni->neigh_table);
4cce389e 1023 if (json == NULL) {
996c9314 1024 vty_out(vty, " VxLAN interface: %s\n", zvni->vxlan_if->name);
4cce389e 1025 vty_out(vty, " VxLAN ifIndex: %u\n", zvni->vxlan_if->ifindex);
996c9314 1026 vty_out(vty, " Local VTEP IP: %s\n",
cd233079 1027 inet_ntoa(zvni->local_vtep_ip));
4cce389e 1028 } else {
cd233079
CS
1029 json_object_string_add(json, "vxlanInterface",
1030 zvni->vxlan_if->name);
1031 json_object_int_add(json, "ifindex", zvni->vxlan_if->ifindex);
1032 json_object_string_add(json, "vtepIp",
1033 inet_ntoa(zvni->local_vtep_ip));
ddd16ed5
MK
1034 json_object_string_add(json, "advertiseGatewayMacip",
1035 zvni->advertise_gw_macip ? "Yes" : "No");
cd233079
CS
1036 json_object_int_add(json, "numMacs", num_macs);
1037 json_object_int_add(json, "numArpNd", num_neigh);
1038 }
d62a17ae 1039 if (!zvni->vteps) {
cd233079
CS
1040 if (json == NULL)
1041 vty_out(vty, " No remote VTEPs known for this VNI\n");
d62a17ae 1042 } else {
cd233079
CS
1043 if (json == NULL)
1044 vty_out(vty, " Remote VTEPs for this VNI:\n");
1045 else
1046 json_vtep_list = json_object_new_array();
1047 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
1048 if (json == NULL)
1049 vty_out(vty, " %s\n",
1050 inet_ntoa(zvtep->vtep_ip));
1051 else {
1052 json_ip_str = json_object_new_string(
1053 inet_ntoa(zvtep->vtep_ip));
1054 json_object_array_add(json_vtep_list,
1055 json_ip_str);
1056 }
1057 }
1058 if (json)
1059 json_object_object_add(json, "numRemoteVteps",
1060 json_vtep_list);
1061 }
1062 if (json == NULL) {
1063 vty_out(vty,
1064 " Number of MACs (local and remote) known for this VNI: %u\n",
1065 num_macs);
1066 vty_out(vty,
1067 " Number of ARPs (IPv4 and IPv6, local and remote) "
1068 "known for this VNI: %u\n",
1069 num_neigh);
ddd16ed5
MK
1070 vty_out(vty, " Advertise-gw-macip: %s\n",
1071 zvni->advertise_gw_macip ? "Yes" : "No");
d62a17ae 1072 }
cec2e17d 1073}
1074
b7cfce93 1075/* print a L3 VNI hash entry */
996c9314 1076static void zl3vni_print_hash(struct hash_backet *backet, void *ctx[])
b7cfce93 1077{
b7cfce93
MK
1078 struct vty *vty = NULL;
1079 json_object *json = NULL;
51d8de8f 1080 json_object *json_vni = NULL;
b7cfce93
MK
1081 zebra_l3vni_t *zl3vni = NULL;
1082
51d8de8f
MK
1083 vty = (struct vty *)ctx[0];
1084 json = (json_object *)ctx[1];
b7cfce93
MK
1085
1086 zl3vni = (zebra_l3vni_t *)backet->data;
b7cfce93
MK
1087
1088 if (!json) {
996c9314
LB
1089 vty_out(vty, "%-10u %-4s %-21s %-8lu %-8lu %-15s %-37s\n",
1090 zl3vni->vni, "L3", zl3vni_vxlan_if_name(zl3vni),
4cce389e 1091 hashcount(zl3vni->rmac_table),
996c9314 1092 hashcount(zl3vni->nh_table), "n/a",
4cce389e 1093 zl3vni_vrf_name(zl3vni));
b7cfce93 1094 } else {
51d8de8f
MK
1095 char vni_str[VNI_STR_LEN];
1096
1097 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
1098 json_vni = json_object_new_object();
1099 json_object_int_add(json_vni, "vni", zl3vni->vni);
4cce389e 1100 json_object_string_add(json_vni, "vxlanIf",
b7cfce93 1101 zl3vni_vxlan_if_name(zl3vni));
4cce389e
MK
1102 json_object_int_add(json_vni, "numMacs",
1103 hashcount(zl3vni->rmac_table));
1104 json_object_int_add(json_vni, "numArpNd",
1105 hashcount(zl3vni->nh_table));
1106 json_object_string_add(json_vni, "numRemoteVteps", "n/a");
1107 json_object_string_add(json_vni, "type", "L3");
1108 json_object_string_add(json_vni, "tenantVrf",
b7cfce93 1109 zl3vni_vrf_name(zl3vni));
51d8de8f 1110 json_object_object_add(json, vni_str, json_vni);
b7cfce93 1111 }
b7cfce93
MK
1112}
1113
cec2e17d 1114/*
1115 * Print a VNI hash entry - called for display of all VNIs.
1116 */
cd233079 1117static void zvni_print_hash(struct hash_backet *backet, void *ctxt[])
cec2e17d 1118{
d62a17ae 1119 struct vty *vty;
1120 zebra_vni_t *zvni;
1121 zebra_vtep_t *zvtep;
d7c0a89a
QY
1122 uint32_t num_vteps = 0;
1123 uint32_t num_macs = 0;
1124 uint32_t num_neigh = 0;
cd233079
CS
1125 json_object *json = NULL;
1126 json_object *json_vni = NULL;
1127 json_object *json_ip_str = NULL;
1128 json_object *json_vtep_list = NULL;
1129
1130 vty = ctxt[0];
1131 json = ctxt[1];
cec2e17d 1132
d62a17ae 1133 zvni = (zebra_vni_t *)backet->data;
cec2e17d 1134
d62a17ae 1135 zvtep = zvni->vteps;
1136 while (zvtep) {
1137 num_vteps++;
1138 zvtep = zvtep->next;
1139 }
cec2e17d 1140
790f8dc5 1141 num_macs = num_valid_macs(zvni);
d62a17ae 1142 num_neigh = hashcount(zvni->neigh_table);
cd233079 1143 if (json == NULL)
996c9314 1144 vty_out(vty, "%-10u %-4s %-21s %-8u %-8u %-15u %-37s\n",
4cce389e 1145 zvni->vni, "L2",
cd233079 1146 zvni->vxlan_if ? zvni->vxlan_if->name : "unknown",
996c9314 1147 num_macs, num_neigh, num_vteps,
b7cfce93 1148 vrf_id_to_name(zvni->vrf_id));
cd233079
CS
1149 else {
1150 char vni_str[VNI_STR_LEN];
1151 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
1152 json_vni = json_object_new_object();
4cce389e
MK
1153 json_object_int_add(json_vni, "vni", zvni->vni);
1154 json_object_string_add(json_vni, "type", "L2");
cd233079
CS
1155 json_object_string_add(json_vni, "vxlanIf",
1156 zvni->vxlan_if ? zvni->vxlan_if->name
1157 : "unknown");
cd233079
CS
1158 json_object_int_add(json_vni, "numMacs", num_macs);
1159 json_object_int_add(json_vni, "numArpNd", num_neigh);
1160 json_object_int_add(json_vni, "numRemoteVteps", num_vteps);
4cce389e
MK
1161 json_object_string_add(json_vni, "tenantVrf",
1162 vrf_id_to_name(zvni->vrf_id));
cd233079
CS
1163 if (num_vteps) {
1164 json_vtep_list = json_object_new_array();
1165 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
1166 json_ip_str = json_object_new_string(
1167 inet_ntoa(zvtep->vtep_ip));
1168 json_object_array_add(json_vtep_list,
1169 json_ip_str);
1170 }
1171 json_object_object_add(json_vni, "remoteVteps",
1172 json_vtep_list);
1173 }
1174 json_object_object_add(json, vni_str, json_vni);
1175 }
cec2e17d 1176}
1177
13d60d35 1178/*
2232a77c 1179 * Inform BGP about local MACIP.
1180 */
996c9314 1181static int zvni_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr,
d7c0a89a 1182 struct ipaddr *ip, uint8_t flags,
f07e1c99 1183 uint32_t seq, uint16_t cmd)
d62a17ae 1184{
d62a17ae 1185 char buf[ETHER_ADDR_STRLEN];
1186 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
1187 int ipa_len;
1188 struct zserv *client = NULL;
1189 struct stream *s = NULL;
d62a17ae 1190
21ccc0cf 1191 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
d62a17ae 1192 /* BGP may not be running. */
1193 if (!client)
1194 return 0;
1195
1002497a 1196 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
d62a17ae 1197
7cf15b25 1198 zclient_create_header(s, cmd, VRF_DEFAULT);
d62a17ae 1199 stream_putl(s, vni);
ff8b7eb8 1200 stream_put(s, macaddr->octet, ETH_ALEN);
d62a17ae 1201 if (ip) {
1202 ipa_len = 0;
1203 if (IS_IPADDR_V4(ip))
1204 ipa_len = IPV4_MAX_BYTELEN;
1205 else if (IS_IPADDR_V6(ip))
1206 ipa_len = IPV6_MAX_BYTELEN;
1207
1208 stream_putl(s, ipa_len); /* IP address length */
1209 if (ipa_len)
1210 stream_put(s, &ip->ip.addr, ipa_len); /* IP address */
1211 } else
1212 stream_putl(s, 0); /* Just MAC. */
1213
f07e1c99 1214 if (cmd == ZEBRA_MACIP_ADD) {
1215 stream_putc(s, flags); /* sticky mac/gateway mac */
1216 stream_putl(s, seq); /* sequence number */
1217 }
d62a17ae 1218
b7cfce93 1219
d62a17ae 1220 /* Write packet size. */
1221 stream_putw_at(s, 0, stream_get_endp(s));
1222
1223 if (IS_ZEBRA_DEBUG_VXLAN)
1a98c087 1224 zlog_debug(
f07e1c99 1225 "Send MACIP %s flags 0x%x MAC %s IP %s seq %u L2-VNI %u to %s",
996c9314
LB
1226 (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
1227 prefix_mac2str(macaddr, buf, sizeof(buf)),
f07e1c99 1228 ipaddr2str(ip, buf2, sizeof(buf2)), seq, vni,
1a98c087 1229 zebra_route_string(client->proto));
d62a17ae 1230
1231 if (cmd == ZEBRA_MACIP_ADD)
1232 client->macipadd_cnt++;
1233 else
1234 client->macipdel_cnt++;
1235
21ccc0cf 1236 return zserv_send_message(client, s);
2232a77c 1237}
1238
1239/*
1240 * Make hash key for neighbors.
13d60d35 1241 */
d62a17ae 1242static unsigned int neigh_hash_keymake(void *p)
13d60d35 1243{
d62a17ae 1244 zebra_neigh_t *n = p;
1245 struct ipaddr *ip = &n->ip;
13d60d35 1246
d62a17ae 1247 if (IS_IPADDR_V4(ip))
1248 return jhash_1word(ip->ipaddr_v4.s_addr, 0);
2232a77c 1249
d62a17ae 1250 return jhash2(ip->ipaddr_v6.s6_addr32,
1251 ZEBRA_NUM_OF(ip->ipaddr_v6.s6_addr32), 0);
13d60d35 1252}
1253
1254/*
2232a77c 1255 * Compare two neighbor hash structures.
13d60d35 1256 */
d62a17ae 1257static int neigh_cmp(const void *p1, const void *p2)
13d60d35 1258{
d62a17ae 1259 const zebra_neigh_t *n1 = p1;
1260 const zebra_neigh_t *n2 = p2;
13d60d35 1261
d62a17ae 1262 if (n1 == NULL && n2 == NULL)
1263 return 1;
2232a77c 1264
d62a17ae 1265 if (n1 == NULL || n2 == NULL)
1266 return 0;
2232a77c 1267
d62a17ae 1268 return (memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr)) == 0);
13d60d35 1269}
1270
1271/*
2232a77c 1272 * Callback to allocate neighbor hash entry.
13d60d35 1273 */
d62a17ae 1274static void *zvni_neigh_alloc(void *p)
13d60d35 1275{
d62a17ae 1276 const zebra_neigh_t *tmp_n = p;
1277 zebra_neigh_t *n;
13d60d35 1278
d62a17ae 1279 n = XCALLOC(MTYPE_NEIGH, sizeof(zebra_neigh_t));
1280 *n = *tmp_n;
2232a77c 1281
d62a17ae 1282 return ((void *)n);
13d60d35 1283}
1284
1285/*
2232a77c 1286 * Add neighbor entry.
13d60d35 1287 */
b6938a74
MK
1288static zebra_neigh_t *zvni_neigh_add(zebra_vni_t *zvni, struct ipaddr *ip,
1289 struct ethaddr *mac)
13d60d35 1290{
d62a17ae 1291 zebra_neigh_t tmp_n;
1292 zebra_neigh_t *n = NULL;
b6938a74 1293 zebra_mac_t *zmac = NULL;
13d60d35 1294
d62a17ae 1295 memset(&tmp_n, 0, sizeof(zebra_neigh_t));
1296 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
1297 n = hash_get(zvni->neigh_table, &tmp_n, zvni_neigh_alloc);
1298 assert(n);
13d60d35 1299
b6938a74
MK
1300 memcpy(&n->emac, mac, ETH_ALEN);
1301 n->state = ZEBRA_NEIGH_INACTIVE;
1302
1303 /* Associate the neigh to mac */
1304 zmac = zvni_mac_lookup(zvni, mac);
1305 if (zmac)
1306 listnode_add_sort(zmac->neigh_list, n);
1307
d62a17ae 1308 return n;
13d60d35 1309}
1310
1311/*
2232a77c 1312 * Delete neighbor entry.
13d60d35 1313 */
d62a17ae 1314static int zvni_neigh_del(zebra_vni_t *zvni, zebra_neigh_t *n)
13d60d35 1315{
d62a17ae 1316 zebra_neigh_t *tmp_n;
b6938a74
MK
1317 zebra_mac_t *zmac = NULL;
1318
1319 zmac = zvni_mac_lookup(zvni, &n->emac);
1320 if (zmac)
1321 listnode_delete(zmac->neigh_list, n);
13d60d35 1322
d62a17ae 1323 /* Free the VNI hash entry and allocated memory. */
1324 tmp_n = hash_release(zvni->neigh_table, n);
1325 if (tmp_n)
1326 XFREE(MTYPE_NEIGH, tmp_n);
13d60d35 1327
d62a17ae 1328 return 0;
13d60d35 1329}
1330
1331/*
2232a77c 1332 * Free neighbor hash entry (callback)
13d60d35 1333 */
b1599bb6 1334static void zvni_neigh_del_hash_entry(struct hash_backet *backet, void *arg)
13d60d35 1335{
d62a17ae 1336 struct neigh_walk_ctx *wctx = arg;
1337 zebra_neigh_t *n = backet->data;
2232a77c 1338
d62a17ae 1339 if (((wctx->flags & DEL_LOCAL_NEIGH) && (n->flags & ZEBRA_NEIGH_LOCAL))
1340 || ((wctx->flags & DEL_REMOTE_NEIGH)
1341 && (n->flags & ZEBRA_NEIGH_REMOTE))
1342 || ((wctx->flags & DEL_REMOTE_NEIGH_FROM_VTEP)
1343 && (n->flags & ZEBRA_NEIGH_REMOTE)
1344 && IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))) {
1345 if (wctx->upd_client && (n->flags & ZEBRA_NEIGH_LOCAL))
2853fed6 1346 zvni_neigh_send_del_to_client(wctx->zvni->vni, &n->ip,
1a98c087 1347 &n->emac, 0);
13d60d35 1348
d62a17ae 1349 if (wctx->uninstall)
1350 zvni_neigh_uninstall(wctx->zvni, n);
13d60d35 1351
b1599bb6 1352 zvni_neigh_del(wctx->zvni, n);
d62a17ae 1353 }
13d60d35 1354
b1599bb6 1355 return;
13d60d35 1356}
1357
1358/*
2232a77c 1359 * Delete all neighbor entries from specific VTEP for a particular VNI.
13d60d35 1360 */
d62a17ae 1361static void zvni_neigh_del_from_vtep(zebra_vni_t *zvni, int uninstall,
1362 struct in_addr *r_vtep_ip)
13d60d35 1363{
d62a17ae 1364 struct neigh_walk_ctx wctx;
13d60d35 1365
d62a17ae 1366 if (!zvni->neigh_table)
1367 return;
13d60d35 1368
d62a17ae 1369 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
1370 wctx.zvni = zvni;
1371 wctx.uninstall = uninstall;
1372 wctx.flags = DEL_REMOTE_NEIGH_FROM_VTEP;
1373 wctx.r_vtep_ip = *r_vtep_ip;
13d60d35 1374
b1599bb6 1375 hash_iterate(zvni->neigh_table, zvni_neigh_del_hash_entry, &wctx);
2232a77c 1376}
13d60d35 1377
2232a77c 1378/*
1379 * Delete all neighbor entries for this VNI.
1380 */
996c9314 1381static void zvni_neigh_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
d7c0a89a 1382 uint32_t flags)
2232a77c 1383{
d62a17ae 1384 struct neigh_walk_ctx wctx;
13d60d35 1385
d62a17ae 1386 if (!zvni->neigh_table)
1387 return;
13d60d35 1388
d62a17ae 1389 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
1390 wctx.zvni = zvni;
d62a17ae 1391 wctx.uninstall = uninstall;
1392 wctx.upd_client = upd_client;
1393 wctx.flags = flags;
2232a77c 1394
b1599bb6 1395 hash_iterate(zvni->neigh_table, zvni_neigh_del_hash_entry, &wctx);
13d60d35 1396}
1397
1398/*
2232a77c 1399 * Look up neighbor hash entry.
1400 */
d62a17ae 1401static zebra_neigh_t *zvni_neigh_lookup(zebra_vni_t *zvni, struct ipaddr *ip)
2232a77c 1402{
d62a17ae 1403 zebra_neigh_t tmp;
1404 zebra_neigh_t *n;
2232a77c 1405
d62a17ae 1406 memset(&tmp, 0, sizeof(tmp));
1407 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
1408 n = hash_lookup(zvni->neigh_table, &tmp);
2232a77c 1409
d62a17ae 1410 return n;
2232a77c 1411}
1412
f07e1c99 1413/*
1414 * Process all neighbors associated with a MAC upon the MAC being learnt
1415 * locally or undergoing any other change (such as sequence number).
1416 */
1417static void zvni_process_neigh_on_local_mac_change(zebra_vni_t *zvni,
1418 zebra_mac_t *zmac,
1419 bool seq_change)
b6938a74
MK
1420{
1421 zebra_neigh_t *n = NULL;
1422 struct listnode *node = NULL;
1423 char buf[ETHER_ADDR_STRLEN];
b6938a74 1424
f07e1c99 1425 if (IS_ZEBRA_DEBUG_VXLAN)
1426 zlog_debug("Processing neighbors on local MAC %s %s, VNI %u",
1427 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
1428 seq_change ? "CHANGE" : "ADD", zvni->vni);
1429
1430 /* Walk all neighbors and mark any inactive local neighbors as
1431 * active and/or update sequence number upon a move, and inform BGP.
1432 * The action for remote neighbors is TBD.
1433 * NOTE: We can't simply uninstall remote neighbors as the kernel may
1434 * accidentally end up deleting a just-learnt local neighbor.
1435 */
b6938a74
MK
1436 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
1437 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
f07e1c99 1438 if (IS_ZEBRA_NEIGH_INACTIVE(n) || seq_change) {
b6938a74 1439 ZEBRA_NEIGH_SET_ACTIVE(n);
f07e1c99 1440 n->loc_seq = zmac->loc_seq;
b6938a74 1441 zvni_neigh_send_add_to_client(
f07e1c99 1442 zvni->vni, &n->ip, &n->emac,
1443 n->flags, n->loc_seq);
b6938a74 1444 }
b6938a74
MK
1445 }
1446 }
1447}
1448
f07e1c99 1449/*
1450 * Process all neighbors associated with a local MAC upon the MAC being
1451 * deleted.
1452 */
2853fed6 1453static void zvni_process_neigh_on_local_mac_del(zebra_vni_t *zvni,
b6938a74
MK
1454 zebra_mac_t *zmac)
1455{
1456 zebra_neigh_t *n = NULL;
1457 struct listnode *node = NULL;
1458 char buf[ETHER_ADDR_STRLEN];
b6938a74 1459
f07e1c99 1460 if (IS_ZEBRA_DEBUG_VXLAN)
1461 zlog_debug("Processing neighbors on local MAC %s DEL, VNI %u",
1462 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
1463 zvni->vni);
1464
1465 /* Walk all local neighbors and mark as inactive and inform
1466 * BGP, if needed.
1467 * TBD: There is currently no handling for remote neighbors. We
1468 * don't expect them to exist, if they do, do we install the MAC
1469 * as a remote MAC and the neighbor as remote?
1470 */
b6938a74
MK
1471 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
1472 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
1473 if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
b6938a74 1474 ZEBRA_NEIGH_SET_INACTIVE(n);
f07e1c99 1475 n->loc_seq = 0;
996c9314
LB
1476 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
1477 &n->emac, 0);
b6938a74 1478 }
b6938a74
MK
1479 }
1480 }
1481}
1482
f07e1c99 1483/*
1484 * Process all neighbors associated with a MAC upon the MAC being remotely
1485 * learnt.
1486 */
2853fed6 1487static void zvni_process_neigh_on_remote_mac_add(zebra_vni_t *zvni,
b6938a74
MK
1488 zebra_mac_t *zmac)
1489{
1490 zebra_neigh_t *n = NULL;
1491 struct listnode *node = NULL;
1492 char buf[ETHER_ADDR_STRLEN];
b6938a74 1493
f07e1c99 1494 if (IS_ZEBRA_DEBUG_VXLAN)
1495 zlog_debug("Processing neighbors on remote MAC %s ADD, VNI %u",
1496 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
1497 zvni->vni);
1498
1499 /* Walk all local neighbors and mark as inactive and inform
1500 * BGP, if needed.
1501 */
b6938a74
MK
1502 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
1503 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
1504 if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
b6938a74 1505 ZEBRA_NEIGH_SET_INACTIVE(n);
f07e1c99 1506 n->loc_seq = 0;
996c9314
LB
1507 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
1508 &n->emac, 0);
b6938a74
MK
1509 }
1510 }
1511 }
1512}
1513
f07e1c99 1514/*
1515 * Process all neighbors associated with a remote MAC upon the MAC being
1516 * deleted.
1517 */
2853fed6 1518static void zvni_process_neigh_on_remote_mac_del(zebra_vni_t *zvni,
b6938a74
MK
1519 zebra_mac_t *zmac)
1520{
f07e1c99 1521 /* NOTE: Currently a NO-OP. */
b6938a74
MK
1522}
1523
2232a77c 1524/*
1525 * Inform BGP about local neighbor addition.
13d60d35 1526 */
996c9314 1527static int zvni_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
ead40654 1528 struct ethaddr *macaddr,
f07e1c99 1529 uint8_t neigh_flags,
1530 uint32_t seq)
13d60d35 1531{
d7c0a89a 1532 uint8_t flags = 0;
ead40654
MK
1533
1534 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_DEF_GW))
1535 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
68e33151
CS
1536 /* Set router flag (R-bit) based on local neigh entry add */
1537 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_ROUTER_FLAG))
1538 SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
ead40654 1539
2853fed6 1540 return zvni_macip_send_msg_to_client(vni, macaddr, ip, flags,
f07e1c99 1541 seq, ZEBRA_MACIP_ADD);
2232a77c 1542}
13d60d35 1543
2232a77c 1544/*
1545 * Inform BGP about local neighbor deletion.
1546 */
996c9314 1547static int zvni_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
d7c0a89a 1548 struct ethaddr *macaddr, uint8_t flags)
2232a77c 1549{
2853fed6 1550 return zvni_macip_send_msg_to_client(vni, macaddr, ip, flags,
f07e1c99 1551 0, ZEBRA_MACIP_DEL);
2232a77c 1552}
1553
1554/*
1555 * Install remote neighbor into the kernel.
1556 */
d62a17ae 1557static int zvni_neigh_install(zebra_vni_t *zvni, zebra_neigh_t *n)
2232a77c 1558{
d62a17ae 1559 struct zebra_if *zif;
1560 struct zebra_l2info_vxlan *vxl;
1561 struct interface *vlan_if;
f7dae312 1562#ifdef GNU_LINUX
68e33151 1563 uint8_t flags;
f7dae312 1564#endif
68e33151 1565 int ret = 0;
2232a77c 1566
d62a17ae 1567 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
1568 return 0;
13d60d35 1569
d62a17ae 1570 zif = zvni->vxlan_if->info;
1571 if (!zif)
1572 return -1;
1573 vxl = &zif->l2info.vxl;
13d60d35 1574
2853fed6 1575 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
d62a17ae 1576 if (!vlan_if)
1577 return -1;
68e33151
CS
1578#ifdef GNU_LINUX
1579 flags = NTF_EXT_LEARNED;
1580 if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
1581 flags |= NTF_ROUTER;
f07e1c99 1582 ZEBRA_NEIGH_SET_ACTIVE(n);
68e33151
CS
1583 ret = kernel_add_neigh(vlan_if, &n->ip, &n->emac, flags);
1584#endif
1585 return ret;
2232a77c 1586}
13d60d35 1587
2232a77c 1588/*
1589 * Uninstall remote neighbor from the kernel.
1590 */
d62a17ae 1591static int zvni_neigh_uninstall(zebra_vni_t *zvni, zebra_neigh_t *n)
2232a77c 1592{
d62a17ae 1593 struct zebra_if *zif;
1594 struct zebra_l2info_vxlan *vxl;
1595 struct interface *vlan_if;
13d60d35 1596
d62a17ae 1597 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
1598 return 0;
2232a77c 1599
d62a17ae 1600 if (!zvni->vxlan_if) {
9df414fe
QY
1601 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
1602 zvni->vni, zvni);
d62a17ae 1603 return -1;
1604 }
2232a77c 1605
d62a17ae 1606 zif = zvni->vxlan_if->info;
1607 if (!zif)
1608 return -1;
1609 vxl = &zif->l2info.vxl;
2853fed6 1610 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
d62a17ae 1611 if (!vlan_if)
1612 return -1;
2232a77c 1613
f07e1c99 1614 ZEBRA_NEIGH_SET_INACTIVE(n);
1615 n->loc_seq = 0;
d62a17ae 1616 return kernel_del_neigh(vlan_if, &n->ip);
13d60d35 1617}
1618
1619/*
2232a77c 1620 * Install neighbor hash entry - called upon access VLAN change.
13d60d35 1621 */
d62a17ae 1622static void zvni_install_neigh_hash(struct hash_backet *backet, void *ctxt)
13d60d35 1623{
d62a17ae 1624 zebra_neigh_t *n;
1625 struct neigh_walk_ctx *wctx = ctxt;
13d60d35 1626
d62a17ae 1627 n = (zebra_neigh_t *)backet->data;
13d60d35 1628
d62a17ae 1629 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
1630 zvni_neigh_install(wctx->zvni, n);
2232a77c 1631}
13d60d35 1632
1a98c087
MK
1633/* Get the VRR interface for SVI if any */
1634struct interface *zebra_get_vrr_intf_for_svi(struct interface *ifp)
1635{
1636 struct zebra_vrf *zvrf = NULL;
1637 struct interface *tmp_if = NULL;
1638 struct zebra_if *zif = NULL;
1a98c087
MK
1639
1640 zvrf = vrf_info_lookup(ifp->vrf_id);
1641 assert(zvrf);
1642
451fda4f 1643 FOR_ALL_INTERFACES (zvrf->vrf, tmp_if) {
1a98c087
MK
1644 zif = tmp_if->info;
1645 if (!zif)
1646 continue;
1647
1648 if (!IS_ZEBRA_IF_MACVLAN(tmp_if))
1649 continue;
1650
1651 if (zif->link == ifp)
1652 return tmp_if;
1653 }
1654
1655 return NULL;
1656}
1657
1658static int zvni_del_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni)
1659{
1a98c087
MK
1660 struct listnode *cnode = NULL, *cnnode = NULL;
1661 struct connected *c = NULL;
1662 struct ethaddr macaddr;
1663
1a98c087
MK
1664 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
1665
1666 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
1667 struct ipaddr ip;
1668
1669 memset(&ip, 0, sizeof(struct ipaddr));
1670 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
1671 continue;
1672
1673 if (c->address->family == AF_INET) {
1674 ip.ipa_type = IPADDR_V4;
1675 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
1676 sizeof(struct in_addr));
1677 } else if (c->address->family == AF_INET6) {
1678 ip.ipa_type = IPADDR_V6;
1679 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
1680 sizeof(struct in6_addr));
1681 } else {
1682 continue;
1683 }
1684
1685 zvni_gw_macip_del(ifp, zvni, &ip);
1686 }
1687
1688 return 0;
1689}
1690
1691static int zvni_add_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni)
1692{
1a98c087
MK
1693 struct listnode *cnode = NULL, *cnnode = NULL;
1694 struct connected *c = NULL;
1695 struct ethaddr macaddr;
1696
1a98c087
MK
1697 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
1698
1699 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
1700 struct ipaddr ip;
1701
1702 memset(&ip, 0, sizeof(struct ipaddr));
1703 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
1704 continue;
1705
1706 if (c->address->family == AF_INET) {
1707 ip.ipa_type = IPADDR_V4;
1708 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
1709 sizeof(struct in_addr));
1710 } else if (c->address->family == AF_INET6) {
1711 ip.ipa_type = IPADDR_V6;
1712 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
1713 sizeof(struct in6_addr));
1714 } else {
1715 continue;
1716 }
1717
1718 zvni_gw_macip_add(ifp, zvni, &macaddr, &ip);
1719 }
31310b25
MK
1720 return 0;
1721}
1722
1723
996c9314 1724static int zvni_advertise_subnet(zebra_vni_t *zvni, struct interface *ifp,
31310b25
MK
1725 int advertise)
1726{
1727 struct listnode *cnode = NULL, *cnnode = NULL;
1728 struct connected *c = NULL;
1729 struct ethaddr macaddr;
1730
1731 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
1732
1733 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
1734 struct prefix p;
1a98c087 1735
31310b25
MK
1736 memcpy(&p, c->address, sizeof(struct prefix));
1737
1738 /* skip link local address */
1739 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
1740 continue;
1741
1742 apply_mask(&p);
1743 if (advertise)
1744 ip_prefix_send_to_client(ifp->vrf_id, &p,
996c9314 1745 ZEBRA_IP_PREFIX_ROUTE_ADD);
31310b25
MK
1746 else
1747 ip_prefix_send_to_client(ifp->vrf_id, &p,
1748 ZEBRA_IP_PREFIX_ROUTE_DEL);
1749 }
1a98c087
MK
1750 return 0;
1751}
1752
1753/*
1754 * zvni_gw_macip_add_to_client
1755 */
1756static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni,
1757 struct ethaddr *macaddr, struct ipaddr *ip)
1758{
1a98c087
MK
1759 char buf[ETHER_ADDR_STRLEN];
1760 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
1761 zebra_neigh_t *n = NULL;
1762 zebra_mac_t *mac = NULL;
1763 struct zebra_if *zif = NULL;
1764 struct zebra_l2info_vxlan *vxl = NULL;
1765
1a98c087
MK
1766 zif = zvni->vxlan_if->info;
1767 if (!zif)
1768 return -1;
1769
1770 vxl = &zif->l2info.vxl;
1771
1772 mac = zvni_mac_lookup(zvni, macaddr);
1773 if (!mac) {
1774 mac = zvni_mac_add(zvni, macaddr);
1775 if (!mac) {
e914ccbe 1776 flog_err(EC_ZEBRA_MAC_ADD_FAILED,
1c50c1c0
QY
1777 "Failed to add MAC %s intf %s(%u) VID %u",
1778 prefix_mac2str(macaddr, buf, sizeof(buf)),
1779 ifp->name, ifp->ifindex, vxl->access_vlan);
1a98c087
MK
1780 return -1;
1781 }
1782 }
1783
1784 /* Set "local" forwarding info. */
1785 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
1786 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
ead40654 1787 SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW);
1a98c087
MK
1788 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
1789 mac->fwd_info.local.ifindex = ifp->ifindex;
1790 mac->fwd_info.local.vid = vxl->access_vlan;
1791
1792 n = zvni_neigh_lookup(zvni, ip);
1793 if (!n) {
b6938a74 1794 n = zvni_neigh_add(zvni, ip, macaddr);
1a98c087 1795 if (!n) {
af4c2728 1796 flog_err(
e914ccbe 1797 EC_ZEBRA_MAC_ADD_FAILED,
2853fed6 1798 "Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
1799 ipaddr2str(ip, buf2, sizeof(buf2)),
0af35d90 1800 prefix_mac2str(macaddr, buf, sizeof(buf)),
1a98c087
MK
1801 ifp->name, ifp->ifindex, zvni->vni);
1802 return -1;
1803 }
1804 }
1805
1806 /* Set "local" forwarding info. */
1807 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
ead40654 1808 SET_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW);
1a8c5c38 1809 ZEBRA_NEIGH_SET_ACTIVE(n);
68e33151
CS
1810 /* Set Router flag (R-bit) */
1811 if (ip->ipa_type == IPADDR_V6)
1812 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
1a98c087
MK
1813 memcpy(&n->emac, macaddr, ETH_ALEN);
1814 n->ifindex = ifp->ifindex;
1815
ead40654
MK
1816 /* Only advertise in BGP if the knob is enabled */
1817 if (!advertise_gw_macip_enabled(zvni))
1818 return 0;
1819
1a98c087
MK
1820 if (IS_ZEBRA_DEBUG_VXLAN)
1821 zlog_debug(
68e33151 1822 "SVI %s(%u) L2-VNI %u, sending GW MAC %s IP %s add to BGP with flags 0x%x",
2853fed6 1823 ifp->name, ifp->ifindex, zvni->vni,
1e9f448f 1824 prefix_mac2str(macaddr, buf, sizeof(buf)),
68e33151 1825 ipaddr2str(ip, buf2, sizeof(buf2)), n->flags);
1a98c087 1826
f07e1c99 1827 zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
1828 n->flags, n->loc_seq);
1a98c087
MK
1829
1830 return 0;
1831}
1832
1833/*
1834 * zvni_gw_macip_del_from_client
1835 */
1836static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni,
1837 struct ipaddr *ip)
1838{
0af35d90 1839 char buf1[ETHER_ADDR_STRLEN];
1a98c087 1840 char buf2[INET6_ADDRSTRLEN];
b7cfce93
MK
1841 zebra_neigh_t *n = NULL;
1842 zebra_mac_t *mac = NULL;
1843
1a98c087
MK
1844 /* If the neigh entry is not present nothing to do*/
1845 n = zvni_neigh_lookup(zvni, ip);
1846 if (!n)
1847 return 0;
1848
1849 /* mac entry should be present */
1850 mac = zvni_mac_lookup(zvni, &n->emac);
0af35d90 1851 if (!mac) {
9df414fe
QY
1852 zlog_debug("MAC %s doesnt exists for neigh %s on VNI %u",
1853 prefix_mac2str(&n->emac, buf1, sizeof(buf1)),
1854 ipaddr2str(ip, buf2, sizeof(buf2)), zvni->vni);
0af35d90
RW
1855 return -1;
1856 }
1a98c087
MK
1857
1858 /* If the entry is not local nothing to do*/
1859 if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL))
1860 return -1;
1861
ead40654 1862 /* only need to delete the entry from bgp if we sent it before */
01a6143b 1863 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
1864 zlog_debug(
1865 "%u:SVI %s(%u) VNI %u, sending GW MAC %s IP %s del to BGP",
1866 ifp->vrf_id, ifp->name, ifp->ifindex, zvni->vni,
ee496c3b 1867 prefix_mac2str(&(n->emac), buf1, sizeof(buf1)),
996c9314 1868 ipaddr2str(ip, buf2, sizeof(buf2)));
01a6143b
MK
1869
1870 /* Remove neighbor from BGP. */
1871 zvni_neigh_send_del_to_client(zvni->vni, &n->ip, &n->emac,
1872 ZEBRA_MACIP_TYPE_GW);
1a98c087
MK
1873
1874 /* Delete this neighbor entry. */
1875 zvni_neigh_del(zvni, n);
1876
1877 /* see if the mac needs to be deleted as well*/
1e9f448f 1878 if (mac)
fe697c6b 1879 zvni_deref_ip2mac(zvni, mac);
1a98c087
MK
1880
1881 return 0;
1882}
1883
1884static void zvni_gw_macip_del_for_vni_hash(struct hash_backet *backet,
2853fed6 1885 void *ctxt)
1a98c087
MK
1886{
1887 zebra_vni_t *zvni = NULL;
1888 struct zebra_if *zif = NULL;
1889 struct zebra_l2info_vxlan zl2_info;
1890 struct interface *vlan_if = NULL;
1891 struct interface *vrr_if = NULL;
b5ebdc9b 1892 struct interface *ifp;
1a98c087
MK
1893
1894 /* Add primary SVI MAC*/
1895 zvni = (zebra_vni_t *)backet->data;
1a98c087 1896
b5ebdc9b 1897 ifp = zvni->vxlan_if;
1898 if (!ifp)
1899 return;
1900 zif = ifp->info;
1901
1902 /* If down or not mapped to a bridge, we're done. */
b682f6de 1903 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
b5ebdc9b 1904 return;
1905
1a98c087
MK
1906 zl2_info = zif->l2info.vxl;
1907
996c9314
LB
1908 vlan_if =
1909 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
1a98c087
MK
1910 if (!vlan_if)
1911 return;
1912
1913 /* Del primary MAC-IP */
1914 zvni_del_macip_for_intf(vlan_if, zvni);
1915
1916 /* Del VRR MAC-IP - if any*/
1917 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
1918 if (vrr_if)
1919 zvni_del_macip_for_intf(vrr_if, zvni);
1920
1921 return;
1922}
1923
1924static void zvni_gw_macip_add_for_vni_hash(struct hash_backet *backet,
2853fed6 1925 void *ctxt)
1a98c087
MK
1926{
1927 zebra_vni_t *zvni = NULL;
1928 struct zebra_if *zif = NULL;
1929 struct zebra_l2info_vxlan zl2_info;
1930 struct interface *vlan_if = NULL;
1931 struct interface *vrr_if = NULL;
b5ebdc9b 1932 struct interface *ifp = NULL;
1a98c087
MK
1933
1934 zvni = (zebra_vni_t *)backet->data;
1a98c087 1935
b5ebdc9b 1936 ifp = zvni->vxlan_if;
1937 if (!ifp)
1938 return;
1939 zif = ifp->info;
1940
1941 /* If down or not mapped to a bridge, we're done. */
b682f6de 1942 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
b5ebdc9b 1943 return;
1a98c087
MK
1944 zl2_info = zif->l2info.vxl;
1945
996c9314
LB
1946 vlan_if =
1947 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
1a98c087
MK
1948 if (!vlan_if)
1949 return;
1950
1a98c087
MK
1951 /* Add primary SVI MAC-IP */
1952 zvni_add_macip_for_intf(vlan_if, zvni);
1953
1954 /* Add VRR MAC-IP - if any*/
1955 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
1956 if (vrr_if)
1957 zvni_add_macip_for_intf(vrr_if, zvni);
1958
1959 return;
1960}
1961
ee69da27
MK
1962static int zvni_local_neigh_update(zebra_vni_t *zvni,
1963 struct interface *ifp,
1964 struct ipaddr *ip,
68e33151 1965 struct ethaddr *macaddr,
a37f4598 1966 bool is_router)
ee69da27
MK
1967{
1968 char buf[ETHER_ADDR_STRLEN];
1969 char buf2[INET6_ADDRSTRLEN];
1970 zebra_neigh_t *n = NULL;
1971 zebra_mac_t *zmac = NULL, *old_zmac = NULL;
f07e1c99 1972 uint32_t old_mac_seq = 0, mac_new_seq = 0;
1973 bool upd_mac_seq = false;
1974 bool neigh_mac_change = false;
29c2ce7c 1975 bool check_rbit = false;
ee69da27 1976
f07e1c99 1977 /* Check if the MAC exists. */
ee69da27
MK
1978 zmac = zvni_mac_lookup(zvni, macaddr);
1979 if (!zmac) {
f07e1c99 1980 /* create a dummy MAC if the MAC is not already present */
ee69da27
MK
1981 if (IS_ZEBRA_DEBUG_VXLAN)
1982 zlog_debug(
1983 "AUTO MAC %s created for neigh %s on VNI %u",
1984 prefix_mac2str(macaddr, buf, sizeof(buf)),
1985 ipaddr2str(ip, buf2, sizeof(buf2)), zvni->vni);
1986
1987 zmac = zvni_mac_add(zvni, macaddr);
1988 if (!zmac) {
9df414fe
QY
1989 zlog_debug("Failed to add MAC %s VNI %u",
1990 prefix_mac2str(macaddr, buf, sizeof(buf)),
1991 zvni->vni);
ee69da27
MK
1992 return -1;
1993 }
1994
1995 memset(&zmac->fwd_info, 0, sizeof(zmac->fwd_info));
1996 memset(&zmac->flags, 0, sizeof(uint32_t));
1997 SET_FLAG(zmac->flags, ZEBRA_MAC_AUTO);
f07e1c99 1998 } else {
1999 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
2000 /*
2001 * We don't change the MAC to local upon a neighbor
2002 * learn event, we wait for the explicit local MAC
2003 * learn. However, we have to compute its sequence
2004 * number in preparation for when it actually turns
2005 * local.
2006 */
2007 upd_mac_seq = true;
2008 }
ee69da27
MK
2009 }
2010
f07e1c99 2011 /* Check if the neighbor exists. */
ee69da27 2012 n = zvni_neigh_lookup(zvni, ip);
f07e1c99 2013 if (!n) {
2014 /* New neighbor - create */
2015 n = zvni_neigh_add(zvni, ip, macaddr);
2016 if (!n) {
2017 flog_err(
e914ccbe 2018 EC_ZEBRA_MAC_ADD_FAILED,
f07e1c99 2019 "Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
2020 ipaddr2str(ip, buf2, sizeof(buf2)),
2021 prefix_mac2str(macaddr, buf, sizeof(buf)),
2022 ifp->name, ifp->ifindex, zvni->vni);
2023 return -1;
2024 }
2025 /* Set "local" forwarding info. */
2026 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
2027 n->ifindex = ifp->ifindex;
f190902f 2028 check_rbit = true;
f07e1c99 2029 } else {
ee69da27 2030 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
66e37987 2031 bool mac_different;
2032 bool cur_is_router;
2033
2034 /* Note any changes and see if of interest to BGP. */
2035 mac_different = (memcmp(n->emac.octet,
2036 macaddr->octet, ETH_ALEN) != 0) ? 1 : 0;
2037 cur_is_router = !!CHECK_FLAG(n->flags,
2038 ZEBRA_NEIGH_ROUTER_FLAG);
2039 if (!mac_different && is_router == cur_is_router) {
ee69da27 2040 n->ifindex = ifp->ifindex;
66e37987 2041 return 0;
2042 }
ee69da27 2043
66e37987 2044 if (!mac_different) {
2045 /* Only the router flag has changed. */
2046 if (is_router)
2047 SET_FLAG(n->flags,
2048 ZEBRA_NEIGH_ROUTER_FLAG);
2049 else
2050 UNSET_FLAG(n->flags,
2051 ZEBRA_NEIGH_ROUTER_FLAG);
ee69da27 2052
66e37987 2053 if (IS_ZEBRA_NEIGH_ACTIVE(n))
2054 return zvni_neigh_send_add_to_client(
2055 zvni->vni, ip, macaddr,
2056 n->flags, n->loc_seq);
2057 return 0;
2058 }
2059
2060 /* The MAC has changed, need to issue a delete
2061 * first as this means a different MACIP route.
2062 * Also, need to do some unlinking/relinking.
2063 * We also need to update the MAC's sequence number
2064 * in different situations.
2065 */
2066 if (IS_ZEBRA_NEIGH_ACTIVE(n))
2067 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
2068 &n->emac, 0);
2069 old_zmac = zvni_mac_lookup(zvni, &n->emac);
2070 if (old_zmac) {
2071 old_mac_seq = CHECK_FLAG(old_zmac->flags,
2072 ZEBRA_MAC_REMOTE) ?
2073 old_zmac->rem_seq : old_zmac->loc_seq;
2074 neigh_mac_change = upd_mac_seq = true;
2075 listnode_delete(old_zmac->neigh_list, n);
2076 zvni_deref_ip2mac(zvni, old_zmac);
29c2ce7c 2077 }
ee69da27 2078
66e37987 2079 /* Update the forwarding info. */
2080 n->ifindex = ifp->ifindex;
2081 memcpy(&n->emac, macaddr, ETH_ALEN);
2082
2083 /* Link to new MAC */
2084 listnode_add_sort(zmac->neigh_list, n);
f07e1c99 2085 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
2086 /*
2087 * Neighbor has moved from remote to local. Its
2088 * MAC could have also changed as part of the move.
2089 */
ee69da27
MK
2090 if (memcmp(n->emac.octet, macaddr->octet,
2091 ETH_ALEN) != 0) {
2092 old_zmac = zvni_mac_lookup(zvni, &n->emac);
2093 if (old_zmac) {
9df2b997 2094 old_mac_seq = CHECK_FLAG(
2095 old_zmac->flags,
2096 ZEBRA_MAC_REMOTE) ?
f07e1c99 2097 old_zmac->rem_seq :
2098 old_zmac->loc_seq;
2099 neigh_mac_change = upd_mac_seq = true;
9fc1522c
DS
2100 listnode_delete(old_zmac->neigh_list,
2101 n);
fe697c6b 2102 zvni_deref_ip2mac(zvni, old_zmac);
ee69da27
MK
2103 }
2104
2105 /* Link to new MAC */
2106 memcpy(&n->emac, macaddr, ETH_ALEN);
2107 listnode_add_sort(zmac->neigh_list, n);
2108 }
2109
2110 /* Mark appropriately */
2111 UNSET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
2112 n->r_vtep_ip.s_addr = 0;
2113 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
2114 n->ifindex = ifp->ifindex;
29c2ce7c 2115 check_rbit = true;
ee69da27 2116 }
f07e1c99 2117 }
2118
2119 /* If MAC was previously remote, or the neighbor had a different
2120 * MAC earlier, recompute the sequence number.
2121 */
2122 if (upd_mac_seq) {
2123 uint32_t seq1, seq2;
2124
2125 seq1 = CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE) ?
2126 zmac->rem_seq + 1 : zmac->loc_seq;
2127 seq2 = neigh_mac_change ? old_mac_seq + 1 : 0;
2128 mac_new_seq = zmac->loc_seq < MAX(seq1, seq2) ?
2129 MAX(seq1, seq2) : zmac->loc_seq;
ee69da27
MK
2130 }
2131
54c17425 2132 /*Mark Router flag (R-bit) */
a37f4598 2133 if (is_router)
54c17425
CS
2134 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2135 else
2136 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2137
ee69da27 2138 /* Before we program this in BGP, we need to check if MAC is locally
f07e1c99 2139 * learnt. If not, force neighbor to be inactive and reset its seq.
ee69da27
MK
2140 */
2141 if (!CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL)) {
8b44d564 2142 ZEBRA_NEIGH_SET_INACTIVE(n);
f07e1c99 2143 n->loc_seq = 0;
2144 zmac->loc_seq = mac_new_seq;
ee69da27
MK
2145 return 0;
2146 }
2147
bf437f90 2148 if (!check_rbit)
f190902f 2149 return 0;
68e33151 2150
f07e1c99 2151 /* If the MAC's sequence number has changed, inform the MAC and all
2152 * neighbors associated with the MAC to BGP, else just inform this
2153 * neighbor.
2154 */
2155 if (upd_mac_seq && zmac->loc_seq != mac_new_seq) {
29c2ce7c 2156 if (IS_ZEBRA_DEBUG_VXLAN)
f07e1c99 2157 zlog_debug("Seq changed for MAC %s VNI %u - old %u new %u",
2158 prefix_mac2str(macaddr, buf, sizeof(buf)),
2159 zvni->vni, zmac->loc_seq, mac_new_seq);
2160 zmac->loc_seq = mac_new_seq;
2161 if (zvni_mac_send_add_to_client(zvni->vni, macaddr,
2162 zmac->flags, zmac->loc_seq))
2163 return -1;
2164 zvni_process_neigh_on_local_mac_change(zvni, zmac, 1);
29c2ce7c
CS
2165 return 0;
2166 }
2167
ee69da27 2168 ZEBRA_NEIGH_SET_ACTIVE(n);
f07e1c99 2169 n->loc_seq = zmac->loc_seq;
ee69da27 2170
f07e1c99 2171 return zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
2172 n->flags, n->loc_seq);
ee69da27
MK
2173}
2174
2175static int zvni_remote_neigh_update(zebra_vni_t *zvni,
2176 struct interface *ifp,
2177 struct ipaddr *ip,
2178 struct ethaddr *macaddr,
2179 uint16_t state)
2180{
2181 char buf[ETHER_ADDR_STRLEN];
2182 char buf2[INET6_ADDRSTRLEN];
2183 zebra_neigh_t *n = NULL;
2184 zebra_mac_t *zmac = NULL;
2185
2186 /* If the neighbor is unknown, there is no further action. */
2187 n = zvni_neigh_lookup(zvni, ip);
2188 if (!n)
2189 return 0;
2190
2191 /* If a remote entry, see if it needs to be refreshed */
2192 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
e9d2cbde 2193#ifdef GNU_LINUX
ee69da27
MK
2194 if (state & NUD_STALE)
2195 zvni_neigh_install(zvni, n);
e9d2cbde 2196#endif
ee69da27
MK
2197 } else {
2198 /* We got a "remote" neighbor notification for an entry
2199 * we think is local. This can happen in a multihoming
2200 * scenario - but only if the MAC is already "remote".
2201 * Just mark our entry as "remote".
2202 */
2203 zmac = zvni_mac_lookup(zvni, macaddr);
2204 if (!zmac || !CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
9df414fe 2205 zlog_debug(
43e52561
QY
2206 "Ignore remote neigh %s (MAC %s) on L2-VNI %u - MAC unknown or local",
2207 ipaddr2str(&n->ip, buf2, sizeof(buf2)),
2208 prefix_mac2str(macaddr, buf, sizeof(buf)),
2209 zvni->vni);
ee69da27
MK
2210 return -1;
2211 }
2212
2213 UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
2214 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
2215 n->r_vtep_ip = zmac->fwd_info.r_vtep_ip;
2216 }
2217
2218 return 0;
2219}
2220
2232a77c 2221/*
2222 * Make hash key for MAC.
2223 */
d62a17ae 2224static unsigned int mac_hash_keymake(void *p)
2232a77c 2225{
d62a17ae 2226 zebra_mac_t *pmac = p;
25331def
DS
2227 const void *pnt = (void *)pmac->macaddr.octet;
2228
ff8b7eb8 2229 return jhash(pnt, ETH_ALEN, 0xa5a5a55a);
2232a77c 2230}
13d60d35 2231
2232a77c 2232/*
2233 * Compare two MAC addresses.
2234 */
d62a17ae 2235static int mac_cmp(const void *p1, const void *p2)
2232a77c 2236{
d62a17ae 2237 const zebra_mac_t *pmac1 = p1;
2238 const zebra_mac_t *pmac2 = p2;
2232a77c 2239
d62a17ae 2240 if (pmac1 == NULL && pmac2 == NULL)
2241 return 1;
2232a77c 2242
d62a17ae 2243 if (pmac1 == NULL || pmac2 == NULL)
2244 return 0;
2232a77c 2245
996c9314 2246 return (memcmp(pmac1->macaddr.octet, pmac2->macaddr.octet, ETH_ALEN)
d62a17ae 2247 == 0);
2232a77c 2248}
2249
2250/*
2251 * Callback to allocate MAC hash entry.
2252 */
d62a17ae 2253static void *zvni_mac_alloc(void *p)
2232a77c 2254{
d62a17ae 2255 const zebra_mac_t *tmp_mac = p;
2256 zebra_mac_t *mac;
2232a77c 2257
d62a17ae 2258 mac = XCALLOC(MTYPE_MAC, sizeof(zebra_mac_t));
2259 *mac = *tmp_mac;
2232a77c 2260
d62a17ae 2261 return ((void *)mac);
2232a77c 2262}
2263
2264/*
2265 * Add MAC entry.
2266 */
d62a17ae 2267static zebra_mac_t *zvni_mac_add(zebra_vni_t *zvni, struct ethaddr *macaddr)
2232a77c 2268{
d62a17ae 2269 zebra_mac_t tmp_mac;
2270 zebra_mac_t *mac = NULL;
2232a77c 2271
d62a17ae 2272 memset(&tmp_mac, 0, sizeof(zebra_mac_t));
ff8b7eb8 2273 memcpy(&tmp_mac.macaddr, macaddr, ETH_ALEN);
d62a17ae 2274 mac = hash_get(zvni->mac_table, &tmp_mac, zvni_mac_alloc);
2275 assert(mac);
2232a77c 2276
b6938a74
MK
2277 mac->neigh_list = list_new();
2278 mac->neigh_list->cmp = (int (*)(void *, void *))neigh_cmp;
2279
d62a17ae 2280 return mac;
2232a77c 2281}
2282
2283/*
2284 * Delete MAC entry.
2285 */
d62a17ae 2286static int zvni_mac_del(zebra_vni_t *zvni, zebra_mac_t *mac)
2232a77c 2287{
d62a17ae 2288 zebra_mac_t *tmp_mac;
2232a77c 2289
affe9e99 2290 list_delete_and_null(&mac->neigh_list);
b6938a74 2291
d62a17ae 2292 /* Free the VNI hash entry and allocated memory. */
2293 tmp_mac = hash_release(zvni->mac_table, mac);
2294 if (tmp_mac)
2295 XFREE(MTYPE_MAC, tmp_mac);
2232a77c 2296
d62a17ae 2297 return 0;
2232a77c 2298}
2299
2300/*
2301 * Free MAC hash entry (callback)
2302 */
b1599bb6 2303static void zvni_mac_del_hash_entry(struct hash_backet *backet, void *arg)
2232a77c 2304{
d62a17ae 2305 struct mac_walk_ctx *wctx = arg;
2306 zebra_mac_t *mac = backet->data;
2232a77c 2307
d62a17ae 2308 if (((wctx->flags & DEL_LOCAL_MAC) && (mac->flags & ZEBRA_MAC_LOCAL))
2309 || ((wctx->flags & DEL_REMOTE_MAC)
2310 && (mac->flags & ZEBRA_MAC_REMOTE))
2311 || ((wctx->flags & DEL_REMOTE_MAC_FROM_VTEP)
2312 && (mac->flags & ZEBRA_MAC_REMOTE)
2313 && IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip,
2314 &wctx->r_vtep_ip))) {
2315 if (wctx->upd_client && (mac->flags & ZEBRA_MAC_LOCAL)) {
996c9314
LB
2316 zvni_mac_send_del_to_client(wctx->zvni->vni,
2317 &mac->macaddr, mac->flags);
d62a17ae 2318 }
2232a77c 2319
d62a17ae 2320 if (wctx->uninstall)
d63c1b18 2321 zvni_mac_uninstall(wctx->zvni, mac);
2232a77c 2322
b1599bb6 2323 zvni_mac_del(wctx->zvni, mac);
d62a17ae 2324 }
2232a77c 2325
b1599bb6 2326 return;
2232a77c 2327}
2328
2329/*
2330 * Delete all MAC entries from specific VTEP for a particular VNI.
2331 */
d62a17ae 2332static void zvni_mac_del_from_vtep(zebra_vni_t *zvni, int uninstall,
2333 struct in_addr *r_vtep_ip)
2232a77c 2334{
d62a17ae 2335 struct mac_walk_ctx wctx;
2232a77c 2336
d62a17ae 2337 if (!zvni->mac_table)
2338 return;
2232a77c 2339
d62a17ae 2340 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
2341 wctx.zvni = zvni;
2342 wctx.uninstall = uninstall;
2343 wctx.flags = DEL_REMOTE_MAC_FROM_VTEP;
2344 wctx.r_vtep_ip = *r_vtep_ip;
2232a77c 2345
b1599bb6 2346 hash_iterate(zvni->mac_table, zvni_mac_del_hash_entry, &wctx);
2232a77c 2347}
2348
2349/*
2350 * Delete all MAC entries for this VNI.
2351 */
996c9314 2352static void zvni_mac_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
d7c0a89a 2353 uint32_t flags)
2232a77c 2354{
d62a17ae 2355 struct mac_walk_ctx wctx;
2232a77c 2356
d62a17ae 2357 if (!zvni->mac_table)
2358 return;
2232a77c 2359
d62a17ae 2360 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
2361 wctx.zvni = zvni;
d62a17ae 2362 wctx.uninstall = uninstall;
2363 wctx.upd_client = upd_client;
2364 wctx.flags = flags;
2232a77c 2365
b1599bb6 2366 hash_iterate(zvni->mac_table, zvni_mac_del_hash_entry, &wctx);
2232a77c 2367}
2368
2369/*
2370 * Look up MAC hash entry.
2371 */
d62a17ae 2372static zebra_mac_t *zvni_mac_lookup(zebra_vni_t *zvni, struct ethaddr *mac)
2232a77c 2373{
d62a17ae 2374 zebra_mac_t tmp;
2375 zebra_mac_t *pmac;
2232a77c 2376
d62a17ae 2377 memset(&tmp, 0, sizeof(tmp));
ff8b7eb8 2378 memcpy(&tmp.macaddr, mac, ETH_ALEN);
d62a17ae 2379 pmac = hash_lookup(zvni->mac_table, &tmp);
2232a77c 2380
d62a17ae 2381 return pmac;
2232a77c 2382}
2383
2384/*
2385 * Inform BGP about local MAC addition.
2386 */
996c9314 2387static int zvni_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr,
f07e1c99 2388 uint8_t mac_flags, uint32_t seq)
2232a77c 2389{
d7c0a89a 2390 uint8_t flags = 0;
ead40654
MK
2391
2392 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_STICKY))
2393 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
2394 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_DEF_GW))
2395 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
2396
2853fed6 2397 return zvni_macip_send_msg_to_client(vni, macaddr, NULL, flags,
f07e1c99 2398 seq, ZEBRA_MACIP_ADD);
2232a77c 2399}
2400
2401/*
2402 * Inform BGP about local MAC deletion.
2403 */
996c9314 2404static int zvni_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr,
d7c0a89a 2405 uint8_t mac_flags)
2232a77c 2406{
d7c0a89a 2407 uint8_t flags = 0;
ead40654
MK
2408
2409 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_STICKY))
2410 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
2411 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_DEF_GW))
2412 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
2413
2853fed6 2414 return zvni_macip_send_msg_to_client(vni, macaddr, NULL, flags,
f07e1c99 2415 0, ZEBRA_MACIP_DEL);
2232a77c 2416}
2417
2418/*
2419 * Map port or (port, VLAN) to a VNI. This is invoked upon getting MAC
2853fed6 2420 * notifications, to see if they are of interest.
2232a77c 2421 */
d62a17ae 2422static zebra_vni_t *zvni_map_vlan(struct interface *ifp,
2423 struct interface *br_if, vlanid_t vid)
2232a77c 2424{
2853fed6 2425 struct zebra_ns *zns;
2426 struct route_node *rn;
2427 struct interface *tmp_if = NULL;
d62a17ae 2428 struct zebra_if *zif;
2429 struct zebra_l2info_bridge *br;
2853fed6 2430 struct zebra_l2info_vxlan *vxl = NULL;
d7c0a89a 2431 uint8_t bridge_vlan_aware;
d62a17ae 2432 zebra_vni_t *zvni;
2853fed6 2433 int found = 0;
2232a77c 2434
d62a17ae 2435 /* Determine if bridge is VLAN-aware or not */
2436 zif = br_if->info;
2437 assert(zif);
2438 br = &zif->l2info.br;
2439 bridge_vlan_aware = br->vlan_aware;
2232a77c 2440
d62a17ae 2441 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
2442 /* TODO: Optimize with a hash. */
2853fed6 2443 zns = zebra_ns_lookup(NS_DEFAULT);
2444 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
2445 tmp_if = (struct interface *)rn->info;
2446 if (!tmp_if)
2447 continue;
d62a17ae 2448 zif = tmp_if->info;
2449 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
2450 continue;
2451 if (!if_is_operative(tmp_if))
2452 continue;
2453 vxl = &zif->l2info.vxl;
2232a77c 2454
d62a17ae 2455 if (zif->brslave_info.br_if != br_if)
2456 continue;
2232a77c 2457
2853fed6 2458 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
2459 found = 1;
d62a17ae 2460 break;
2853fed6 2461 }
d62a17ae 2462 }
2232a77c 2463
2853fed6 2464 if (!found)
d62a17ae 2465 return NULL;
2232a77c 2466
2853fed6 2467 zvni = zvni_lookup(vxl->vni);
d62a17ae 2468 return zvni;
2232a77c 2469}
2470
2471/*
2472 * Map SVI and associated bridge to a VNI. This is invoked upon getting
2473 * neighbor notifications, to see if they are of interest.
2232a77c 2474 */
b7cfce93
MK
2475static zebra_vni_t *zvni_from_svi(struct interface *ifp,
2476 struct interface *br_if)
d62a17ae 2477{
2853fed6 2478 struct zebra_ns *zns;
2479 struct route_node *rn;
2480 struct interface *tmp_if = NULL;
d62a17ae 2481 struct zebra_if *zif;
2482 struct zebra_l2info_bridge *br;
2853fed6 2483 struct zebra_l2info_vxlan *vxl = NULL;
d7c0a89a 2484 uint8_t bridge_vlan_aware;
d62a17ae 2485 vlanid_t vid = 0;
2486 zebra_vni_t *zvni;
2853fed6 2487 int found = 0;
d62a17ae 2488
71349e03
MK
2489 if (!br_if)
2490 return NULL;
2491
d62a17ae 2492 /* Make sure the linked interface is a bridge. */
2493 if (!IS_ZEBRA_IF_BRIDGE(br_if))
2494 return NULL;
2495
d62a17ae 2496 /* Determine if bridge is VLAN-aware or not */
2497 zif = br_if->info;
2498 assert(zif);
2499 br = &zif->l2info.br;
2500 bridge_vlan_aware = br->vlan_aware;
2501 if (bridge_vlan_aware) {
2502 struct zebra_l2info_vlan *vl;
2503
2504 if (!IS_ZEBRA_IF_VLAN(ifp))
2505 return NULL;
2506
2507 zif = ifp->info;
2508 assert(zif);
2509 vl = &zif->l2info.vl;
2510 vid = vl->vid;
2511 }
2512
2513 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
2514 /* TODO: Optimize with a hash. */
2853fed6 2515 zns = zebra_ns_lookup(NS_DEFAULT);
2516 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
2517 tmp_if = (struct interface *)rn->info;
2518 if (!tmp_if)
2519 continue;
d62a17ae 2520 zif = tmp_if->info;
2521 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
2522 continue;
2523 if (!if_is_operative(tmp_if))
2524 continue;
2525 vxl = &zif->l2info.vxl;
2526
2527 if (zif->brslave_info.br_if != br_if)
2528 continue;
2529
2853fed6 2530 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
2531 found = 1;
d62a17ae 2532 break;
2853fed6 2533 }
d62a17ae 2534 }
2535
2853fed6 2536 if (!found)
d62a17ae 2537 return NULL;
2538
2853fed6 2539 zvni = zvni_lookup(vxl->vni);
d62a17ae 2540 return zvni;
2232a77c 2541}
2542
2543/* Map to SVI on bridge corresponding to specified VLAN. This can be one
2544 * of two cases:
2545 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN interface
2546 * linked to the bridge
2547 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge inteface
2548 * itself
2549 */
2853fed6 2550static struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if)
d62a17ae 2551{
2853fed6 2552 struct zebra_ns *zns;
2553 struct route_node *rn;
2554 struct interface *tmp_if = NULL;
d62a17ae 2555 struct zebra_if *zif;
2556 struct zebra_l2info_bridge *br;
2557 struct zebra_l2info_vlan *vl;
d7c0a89a 2558 uint8_t bridge_vlan_aware;
2853fed6 2559 int found = 0;
d62a17ae 2560
b5ebdc9b 2561 /* Defensive check, caller expected to invoke only with valid bridge. */
2562 if (!br_if)
2563 return NULL;
2564
d62a17ae 2565 /* Determine if bridge is VLAN-aware or not */
2566 zif = br_if->info;
2567 assert(zif);
2568 br = &zif->l2info.br;
2569 bridge_vlan_aware = br->vlan_aware;
2570
2571 /* Check oper status of the SVI. */
2572 if (!bridge_vlan_aware)
2573 return if_is_operative(br_if) ? br_if : NULL;
2574
2575 /* Identify corresponding VLAN interface. */
2576 /* TODO: Optimize with a hash. */
2853fed6 2577 zns = zebra_ns_lookup(NS_DEFAULT);
2578 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
2579 tmp_if = (struct interface *)rn->info;
d62a17ae 2580 /* Check oper status of the SVI. */
2853fed6 2581 if (!tmp_if || !if_is_operative(tmp_if))
d62a17ae 2582 continue;
2583 zif = tmp_if->info;
2584 if (!zif || zif->zif_type != ZEBRA_IF_VLAN
2585 || zif->link != br_if)
2586 continue;
2587 vl = (struct zebra_l2info_vlan *)&zif->l2info.vl;
2588
2853fed6 2589 if (vl->vid == vid) {
2590 found = 1;
d62a17ae 2591 break;
2853fed6 2592 }
d62a17ae 2593 }
2594
2853fed6 2595 return found ? tmp_if : NULL;
2232a77c 2596}
2597
2598/*
2599 * Install remote MAC into the kernel.
2600 */
d62a17ae 2601static int zvni_mac_install(zebra_vni_t *zvni, zebra_mac_t *mac)
2232a77c 2602{
d62a17ae 2603 struct zebra_if *zif;
2604 struct zebra_l2info_vxlan *vxl;
a37f4598 2605 bool sticky;
2232a77c 2606
d62a17ae 2607 if (!(mac->flags & ZEBRA_MAC_REMOTE))
2608 return 0;
2232a77c 2609
d62a17ae 2610 zif = zvni->vxlan_if->info;
2611 if (!zif)
2612 return -1;
2613 vxl = &zif->l2info.vxl;
2232a77c 2614
a37f4598 2615 sticky = !!CHECK_FLAG(mac->flags,
2616 (ZEBRA_MAC_STICKY | ZEBRA_MAC_REMOTE_DEF_GW));
c85c03c7 2617
d62a17ae 2618 return kernel_add_mac(zvni->vxlan_if, vxl->access_vlan, &mac->macaddr,
2619 mac->fwd_info.r_vtep_ip, sticky);
2232a77c 2620}
2621
2622/*
d63c1b18 2623 * Uninstall remote MAC from the kernel.
2232a77c 2624 */
d63c1b18 2625static int zvni_mac_uninstall(zebra_vni_t *zvni, zebra_mac_t *mac)
2232a77c 2626{
d62a17ae 2627 struct zebra_if *zif;
2628 struct zebra_l2info_vxlan *vxl;
2629 struct in_addr vtep_ip = {.s_addr = 0};
d62a17ae 2630 struct interface *ifp;
2232a77c 2631
d63c1b18 2632 if (!(mac->flags & ZEBRA_MAC_REMOTE))
d62a17ae 2633 return 0;
2232a77c 2634
d62a17ae 2635 if (!zvni->vxlan_if) {
9df414fe
QY
2636 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
2637 zvni->vni, zvni);
d62a17ae 2638 return -1;
2639 }
2232a77c 2640
d62a17ae 2641 zif = zvni->vxlan_if->info;
2642 if (!zif)
2643 return -1;
2644 vxl = &zif->l2info.vxl;
2232a77c 2645
d63c1b18 2646 ifp = zvni->vxlan_if;
2647 vtep_ip = mac->fwd_info.r_vtep_ip;
2232a77c 2648
d63c1b18 2649 return kernel_del_mac(ifp, vxl->access_vlan, &mac->macaddr, vtep_ip);
2232a77c 2650}
2651
2652/*
2653 * Install MAC hash entry - called upon access VLAN change.
2654 */
d62a17ae 2655static void zvni_install_mac_hash(struct hash_backet *backet, void *ctxt)
2232a77c 2656{
d62a17ae 2657 zebra_mac_t *mac;
2658 struct mac_walk_ctx *wctx = ctxt;
2232a77c 2659
d62a17ae 2660 mac = (zebra_mac_t *)backet->data;
2232a77c 2661
d62a17ae 2662 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
2663 zvni_mac_install(wctx->zvni, mac);
2232a77c 2664}
2665
fe697c6b 2666/*
2667 * Count of remote neighbors referencing this MAC.
2668 */
2669static int remote_neigh_count(zebra_mac_t *zmac)
2670{
2671 zebra_neigh_t *n = NULL;
2672 struct listnode *node = NULL;
2673 int count = 0;
2674
2675 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
2676 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
2677 count++;
2678 }
2679
2680 return count;
2681}
2682
2232a77c 2683/*
2684 * Decrement neighbor refcount of MAC; uninstall and free it if
2685 * appropriate.
2686 */
fe697c6b 2687static void zvni_deref_ip2mac(zebra_vni_t *zvni, zebra_mac_t *mac)
2232a77c 2688{
fe697c6b 2689 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
d62a17ae 2690 return;
2232a77c 2691
fe697c6b 2692 /* If all remote neighbors referencing a remote MAC go away,
2693 * we need to uninstall the MAC.
2694 */
2695 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) &&
2696 remote_neigh_count(mac) == 0) {
d63c1b18 2697 zvni_mac_uninstall(zvni, mac);
fe697c6b 2698 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
2699 }
2232a77c 2700
fe697c6b 2701 /* If no neighbors, delete the MAC. */
2702 if (list_isempty(mac->neigh_list))
2703 zvni_mac_del(zvni, mac);
2232a77c 2704}
2705
2706/*
2707 * Read and populate local MACs and neighbors corresponding to this VNI.
2708 */
996c9314 2709static void zvni_read_mac_neigh(zebra_vni_t *zvni, struct interface *ifp)
2232a77c 2710{
2853fed6 2711 struct zebra_ns *zns;
d62a17ae 2712 struct zebra_if *zif;
2713 struct interface *vlan_if;
2714 struct zebra_l2info_vxlan *vxl;
1a98c087 2715 struct interface *vrr_if;
2232a77c 2716
d62a17ae 2717 zif = ifp->info;
2718 vxl = &zif->l2info.vxl;
2853fed6 2719 zns = zebra_ns_lookup(NS_DEFAULT);
2232a77c 2720
d62a17ae 2721 if (IS_ZEBRA_DEBUG_VXLAN)
2722 zlog_debug(
2853fed6 2723 "Reading MAC FDB and Neighbors for intf %s(%u) VNI %u master %u",
2724 ifp->name, ifp->ifindex, zvni->vni,
d62a17ae 2725 zif->brslave_info.bridge_ifindex);
2232a77c 2726
2853fed6 2727 macfdb_read_for_bridge(zns, ifp, zif->brslave_info.br_if);
2728 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
1a98c087
MK
2729 if (vlan_if) {
2730
ead40654
MK
2731 /* Add SVI MAC-IP */
2732 zvni_add_macip_for_intf(vlan_if, zvni);
1a98c087 2733
ead40654
MK
2734 /* Add VRR MAC-IP - if any*/
2735 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
2736 if (vrr_if)
2737 zvni_add_macip_for_intf(vrr_if, zvni);
1a98c087 2738
2853fed6 2739 neigh_read_for_vlan(zns, vlan_if);
1a98c087 2740 }
2232a77c 2741}
2742
2743/*
2744 * Hash function for VNI.
2745 */
d62a17ae 2746static unsigned int vni_hash_keymake(void *p)
2232a77c 2747{
d62a17ae 2748 const zebra_vni_t *zvni = p;
2232a77c 2749
d62a17ae 2750 return (jhash_1word(zvni->vni, 0));
2232a77c 2751}
2752
2753/*
2754 * Compare 2 VNI hash entries.
2755 */
d62a17ae 2756static int vni_hash_cmp(const void *p1, const void *p2)
2232a77c 2757{
d62a17ae 2758 const zebra_vni_t *zvni1 = p1;
2759 const zebra_vni_t *zvni2 = p2;
2232a77c 2760
d62a17ae 2761 return (zvni1->vni == zvni2->vni);
2232a77c 2762}
2763
2764/*
2765 * Callback to allocate VNI hash entry.
2766 */
d62a17ae 2767static void *zvni_alloc(void *p)
2232a77c 2768{
d62a17ae 2769 const zebra_vni_t *tmp_vni = p;
2770 zebra_vni_t *zvni;
2232a77c 2771
d62a17ae 2772 zvni = XCALLOC(MTYPE_ZVNI, sizeof(zebra_vni_t));
2773 zvni->vni = tmp_vni->vni;
2774 return ((void *)zvni);
2232a77c 2775}
2776
2777/*
2778 * Look up VNI hash entry.
2779 */
2853fed6 2780static zebra_vni_t *zvni_lookup(vni_t vni)
2232a77c 2781{
2853fed6 2782 struct zebra_vrf *zvrf;
d62a17ae 2783 zebra_vni_t tmp_vni;
2784 zebra_vni_t *zvni = NULL;
2232a77c 2785
2853fed6 2786 zvrf = vrf_info_lookup(VRF_DEFAULT);
2787 assert(zvrf);
d62a17ae 2788 memset(&tmp_vni, 0, sizeof(zebra_vni_t));
2789 tmp_vni.vni = vni;
2790 zvni = hash_lookup(zvrf->vni_table, &tmp_vni);
2232a77c 2791
d62a17ae 2792 return zvni;
2232a77c 2793}
2794
2795/*
2796 * Add VNI hash entry.
2797 */
2853fed6 2798static zebra_vni_t *zvni_add(vni_t vni)
2232a77c 2799{
2853fed6 2800 struct zebra_vrf *zvrf;
d62a17ae 2801 zebra_vni_t tmp_zvni;
2802 zebra_vni_t *zvni = NULL;
2232a77c 2803
2853fed6 2804 zvrf = vrf_info_lookup(VRF_DEFAULT);
2805 assert(zvrf);
d62a17ae 2806 memset(&tmp_zvni, 0, sizeof(zebra_vni_t));
2807 tmp_zvni.vni = vni;
2808 zvni = hash_get(zvrf->vni_table, &tmp_zvni, zvni_alloc);
2809 assert(zvni);
2232a77c 2810
d62a17ae 2811 /* Create hash table for MAC */
2812 zvni->mac_table =
2813 hash_create(mac_hash_keymake, mac_cmp, "Zebra VNI MAC Table");
2232a77c 2814
d62a17ae 2815 /* Create hash table for neighbors */
2816 zvni->neigh_table = hash_create(neigh_hash_keymake, neigh_cmp,
2817 "Zebra VNI Neighbor Table");
2232a77c 2818
d62a17ae 2819 return zvni;
2232a77c 2820}
2821
2822/*
2823 * Delete VNI hash entry.
2824 */
2853fed6 2825static int zvni_del(zebra_vni_t *zvni)
2232a77c 2826{
2853fed6 2827 struct zebra_vrf *zvrf;
d62a17ae 2828 zebra_vni_t *tmp_zvni;
2232a77c 2829
2853fed6 2830 zvrf = vrf_info_lookup(VRF_DEFAULT);
2831 assert(zvrf);
2832
d62a17ae 2833 zvni->vxlan_if = NULL;
2232a77c 2834
d62a17ae 2835 /* Free the neighbor hash table. */
2836 hash_free(zvni->neigh_table);
2837 zvni->neigh_table = NULL;
2232a77c 2838
d62a17ae 2839 /* Free the MAC hash table. */
2840 hash_free(zvni->mac_table);
2841 zvni->mac_table = NULL;
2232a77c 2842
d62a17ae 2843 /* Free the VNI hash entry and allocated memory. */
2844 tmp_zvni = hash_release(zvrf->vni_table, zvni);
2845 if (tmp_zvni)
2846 XFREE(MTYPE_ZVNI, tmp_zvni);
2232a77c 2847
d62a17ae 2848 return 0;
2232a77c 2849}
2850
2851/*
2852 * Inform BGP about local VNI addition.
2853 */
2853fed6 2854static int zvni_send_add_to_client(zebra_vni_t *zvni)
2232a77c 2855{
d62a17ae 2856 struct zserv *client;
2857 struct stream *s;
2232a77c 2858
21ccc0cf 2859 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
d62a17ae 2860 /* BGP may not be running. */
2861 if (!client)
2862 return 0;
2232a77c 2863
1002497a 2864 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2232a77c 2865
7cf15b25 2866 zclient_create_header(s, ZEBRA_VNI_ADD, VRF_DEFAULT);
d62a17ae 2867 stream_putl(s, zvni->vni);
2868 stream_put_in_addr(s, &zvni->local_vtep_ip);
b7cfce93 2869 stream_put(s, &zvni->vrf_id, sizeof(vrf_id_t)); /* tenant vrf */
2232a77c 2870
d62a17ae 2871 /* Write packet size. */
2872 stream_putw_at(s, 0, stream_get_endp(s));
2232a77c 2873
d62a17ae 2874 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
2875 zlog_debug("Send VNI_ADD %u %s tenant vrf %s to %s", zvni->vni,
2876 inet_ntoa(zvni->local_vtep_ip),
b7cfce93 2877 vrf_id_to_name(zvni->vrf_id),
d62a17ae 2878 zebra_route_string(client->proto));
2232a77c 2879
d62a17ae 2880 client->vniadd_cnt++;
21ccc0cf 2881 return zserv_send_message(client, s);
2232a77c 2882}
2883
2884/*
2885 * Inform BGP about local VNI deletion.
2886 */
2853fed6 2887static int zvni_send_del_to_client(vni_t vni)
2232a77c 2888{
d62a17ae 2889 struct zserv *client;
2890 struct stream *s;
2232a77c 2891
21ccc0cf 2892 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
d62a17ae 2893 /* BGP may not be running. */
2894 if (!client)
2895 return 0;
2232a77c 2896
1002497a 2897 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
d62a17ae 2898 stream_reset(s);
2232a77c 2899
7cf15b25 2900 zclient_create_header(s, ZEBRA_VNI_DEL, VRF_DEFAULT);
d62a17ae 2901 stream_putl(s, vni);
2232a77c 2902
d62a17ae 2903 /* Write packet size. */
2904 stream_putw_at(s, 0, stream_get_endp(s));
2232a77c 2905
d62a17ae 2906 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 2907 zlog_debug("Send VNI_DEL %u to %s", vni,
d62a17ae 2908 zebra_route_string(client->proto));
2232a77c 2909
d62a17ae 2910 client->vnidel_cnt++;
21ccc0cf 2911 return zserv_send_message(client, s);
2232a77c 2912}
2913
2914/*
2915 * Build the VNI hash table by going over the VxLAN interfaces. This
2916 * is called when EVPN (advertise-all-vni) is enabled.
2917 */
2853fed6 2918static void zvni_build_hash_table()
2232a77c 2919{
2853fed6 2920 struct zebra_ns *zns;
2921 struct route_node *rn;
d62a17ae 2922 struct interface *ifp;
2232a77c 2923
d62a17ae 2924 /* Walk VxLAN interfaces and create VNI hash. */
2853fed6 2925 zns = zebra_ns_lookup(NS_DEFAULT);
2926 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
b7cfce93 2927 vni_t vni;
643215ce 2928 zebra_vni_t *zvni = NULL;
2929 zebra_l3vni_t *zl3vni = NULL;
d62a17ae 2930 struct zebra_if *zif;
2931 struct zebra_l2info_vxlan *vxl;
2232a77c 2932
2853fed6 2933 ifp = (struct interface *)rn->info;
2934 if (!ifp)
2935 continue;
d62a17ae 2936 zif = ifp->info;
2937 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
2938 continue;
2232a77c 2939
b7cfce93 2940 vxl = &zif->l2info.vxl;
d62a17ae 2941 vni = vxl->vni;
2232a77c 2942
643215ce 2943 /* L3-VNI and L2-VNI are handled seperately */
2944 zl3vni = zl3vni_lookup(vni);
2945 if (zl3vni) {
2232a77c 2946
b7cfce93 2947 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
2948 zlog_debug(
2949 "create L3-VNI hash for Intf %s(%u) L3-VNI %u",
2950 ifp->name, ifp->ifindex, vni);
2232a77c 2951
b7cfce93 2952 /* associate with vxlan_if */
b67a60d2 2953 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93 2954 zl3vni->vxlan_if = ifp;
2232a77c 2955
523cafc4 2956 /*
2957 * we need to associate with SVI.
b7cfce93 2958 * we can associate with svi-if only after association
523cafc4 2959 * with vxlan-intf is complete
2960 */
b7cfce93
MK
2961 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
2962
2963 if (is_l3vni_oper_up(zl3vni))
2964 zebra_vxlan_process_l3vni_oper_up(zl3vni);
2965
2966 } else {
b7cfce93
MK
2967 struct interface *vlan_if = NULL;
2968
2969 if (IS_ZEBRA_DEBUG_VXLAN)
2970 zlog_debug(
2971 "Create L2-VNI hash for intf %s(%u) L2-VNI %u local IP %s",
2972 ifp->name, ifp->ifindex, vni,
2973 inet_ntoa(vxl->vtep_ip));
2974
2975 /* VNI hash entry is not expected to exist. */
2976 zvni = zvni_lookup(vni);
2977 if (zvni) {
9df414fe 2978 zlog_debug(
b7cfce93
MK
2979 "VNI hash already present for IF %s(%u) L2-VNI %u",
2980 ifp->name, ifp->ifindex, vni);
2981 continue;
2982 }
2983
2984 zvni = zvni_add(vni);
2985 if (!zvni) {
9df414fe 2986 zlog_debug(
b7cfce93
MK
2987 "Failed to add VNI hash, IF %s(%u) L2-VNI %u",
2988 ifp->name, ifp->ifindex, vni);
2989 return;
2990 }
2991
2992 zvni->local_vtep_ip = vxl->vtep_ip;
2993 zvni->vxlan_if = ifp;
2994 vlan_if = zvni_map_to_svi(vxl->access_vlan,
2995 zif->brslave_info.br_if);
2996 if (vlan_if) {
2997 zvni->vrf_id = vlan_if->vrf_id;
2998 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
2999 if (zl3vni)
3000 listnode_add_sort(zl3vni->l2vnis, zvni);
3001 }
3002
3003
3004 /* Inform BGP if intf is up and mapped to bridge. */
3005 if (if_is_operative(ifp) && zif->brslave_info.br_if)
3006 zvni_send_add_to_client(zvni);
3007 }
d62a17ae 3008 }
2232a77c 3009}
3010
3011/*
3012 * See if remote VTEP matches with prefix.
3013 */
d62a17ae 3014static int zvni_vtep_match(struct in_addr *vtep_ip, zebra_vtep_t *zvtep)
2232a77c 3015{
d62a17ae 3016 return (IPV4_ADDR_SAME(vtep_ip, &zvtep->vtep_ip));
2232a77c 3017}
3018
3019/*
3020 * Locate remote VTEP in VNI hash table.
3021 */
d62a17ae 3022static zebra_vtep_t *zvni_vtep_find(zebra_vni_t *zvni, struct in_addr *vtep_ip)
2232a77c 3023{
d62a17ae 3024 zebra_vtep_t *zvtep;
2232a77c 3025
d62a17ae 3026 if (!zvni)
3027 return NULL;
2232a77c 3028
d62a17ae 3029 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
3030 if (zvni_vtep_match(vtep_ip, zvtep))
3031 break;
3032 }
2232a77c 3033
d62a17ae 3034 return zvtep;
2232a77c 3035}
3036
3037/*
3038 * Add remote VTEP to VNI hash table.
3039 */
d62a17ae 3040static zebra_vtep_t *zvni_vtep_add(zebra_vni_t *zvni, struct in_addr *vtep_ip)
2232a77c 3041{
d62a17ae 3042 zebra_vtep_t *zvtep;
2232a77c 3043
d62a17ae 3044 zvtep = XCALLOC(MTYPE_ZVNI_VTEP, sizeof(zebra_vtep_t));
2232a77c 3045
d62a17ae 3046 zvtep->vtep_ip = *vtep_ip;
2232a77c 3047
d62a17ae 3048 if (zvni->vteps)
3049 zvni->vteps->prev = zvtep;
3050 zvtep->next = zvni->vteps;
3051 zvni->vteps = zvtep;
2232a77c 3052
d62a17ae 3053 return zvtep;
2232a77c 3054}
3055
3056/*
3057 * Remove remote VTEP from VNI hash table.
3058 */
d62a17ae 3059static int zvni_vtep_del(zebra_vni_t *zvni, zebra_vtep_t *zvtep)
2232a77c 3060{
d62a17ae 3061 if (zvtep->next)
3062 zvtep->next->prev = zvtep->prev;
3063 if (zvtep->prev)
3064 zvtep->prev->next = zvtep->next;
3065 else
3066 zvni->vteps = zvtep->next;
2232a77c 3067
d62a17ae 3068 zvtep->prev = zvtep->next = NULL;
3069 XFREE(MTYPE_ZVNI_VTEP, zvtep);
2232a77c 3070
d62a17ae 3071 return 0;
2232a77c 3072}
3073
3074/*
3075 * Delete all remote VTEPs for this VNI (upon VNI delete). Also
3076 * uninstall from kernel if asked to.
3077 */
d62a17ae 3078static int zvni_vtep_del_all(zebra_vni_t *zvni, int uninstall)
2232a77c 3079{
d62a17ae 3080 zebra_vtep_t *zvtep, *zvtep_next;
2232a77c 3081
d62a17ae 3082 if (!zvni)
3083 return -1;
2232a77c 3084
d62a17ae 3085 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep_next) {
3086 zvtep_next = zvtep->next;
3087 if (uninstall)
3088 zvni_vtep_uninstall(zvni, &zvtep->vtep_ip);
3089 zvni_vtep_del(zvni, zvtep);
3090 }
2232a77c 3091
d62a17ae 3092 return 0;
2232a77c 3093}
3094
3095/*
3096 * Install remote VTEP into the kernel.
3097 */
d62a17ae 3098static int zvni_vtep_install(zebra_vni_t *zvni, struct in_addr *vtep_ip)
2232a77c 3099{
d62a17ae 3100 return kernel_add_vtep(zvni->vni, zvni->vxlan_if, vtep_ip);
2232a77c 3101}
3102
3103/*
3104 * Uninstall remote VTEP from the kernel.
3105 */
d62a17ae 3106static int zvni_vtep_uninstall(zebra_vni_t *zvni, struct in_addr *vtep_ip)
2232a77c 3107{
d62a17ae 3108 if (!zvni->vxlan_if) {
9df414fe
QY
3109 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
3110 zvni->vni, zvni);
d62a17ae 3111 return -1;
3112 }
2232a77c 3113
d62a17ae 3114 return kernel_del_vtep(zvni->vni, zvni->vxlan_if, vtep_ip);
2232a77c 3115}
3116
3117/*
3118 * Cleanup VNI/VTEP and update kernel
3119 */
84915b0a 3120static void zvni_cleanup_all(struct hash_backet *backet, void *arg)
2232a77c 3121{
b7cfce93
MK
3122 zebra_vni_t *zvni = NULL;
3123 zebra_l3vni_t *zl3vni = NULL;
84915b0a 3124 struct zebra_vrf *zvrf = (struct zebra_vrf *)arg;
2232a77c 3125
d62a17ae 3126 zvni = (zebra_vni_t *)backet->data;
2232a77c 3127
b7cfce93 3128 /* remove from l3-vni list */
84915b0a 3129 if (zvrf->l3vni)
3130 zl3vni = zl3vni_lookup(zvrf->l3vni);
b7cfce93
MK
3131 if (zl3vni)
3132 listnode_delete(zl3vni->l2vnis, zvni);
3133
d62a17ae 3134 /* Free up all neighbors and MACs, if any. */
2853fed6 3135 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
3136 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
2232a77c 3137
d62a17ae 3138 /* Free up all remote VTEPs, if any. */
3139 zvni_vtep_del_all(zvni, 1);
2232a77c 3140
d62a17ae 3141 /* Delete the hash entry. */
2853fed6 3142 zvni_del(zvni);
2232a77c 3143}
3144
655b04d1 3145/* cleanup L3VNI */
996c9314 3146static void zl3vni_cleanup_all(struct hash_backet *backet, void *args)
655b04d1
MK
3147{
3148 zebra_l3vni_t *zl3vni = NULL;
3149
3150 zl3vni = (zebra_l3vni_t *)backet->data;
655b04d1
MK
3151
3152 zebra_vxlan_process_l3vni_oper_down(zl3vni);
3153}
3154
85442b09 3155static void rb_find_or_add_host(struct host_rb_tree_entry *hrbe,
41db76c2 3156 struct prefix *host)
5e1b0650
DS
3157{
3158 struct host_rb_entry lookup;
3159 struct host_rb_entry *hle;
3160
3161 memset(&lookup, 0, sizeof(lookup));
3162 memcpy(&lookup.p, host, sizeof(*host));
3163
85442b09 3164 hle = RB_FIND(host_rb_tree_entry, hrbe, &lookup);
5e1b0650
DS
3165 if (hle)
3166 return;
3167
3168 hle = XCALLOC(MTYPE_HOST_PREFIX, sizeof(struct host_rb_entry));
3169 memcpy(hle, &lookup, sizeof(lookup));
3170
85442b09 3171 RB_INSERT(host_rb_tree_entry, hrbe, hle);
5e1b0650
DS
3172}
3173
85442b09 3174static void rb_delete_host(struct host_rb_tree_entry *hrbe, struct prefix *host)
5e1b0650
DS
3175{
3176 struct host_rb_entry lookup;
3177 struct host_rb_entry *hle;
3178
3179 memset(&lookup, 0, sizeof(lookup));
3180 memcpy(&lookup.p, host, sizeof(*host));
3181
85442b09 3182 hle = RB_FIND(host_rb_tree_entry, hrbe, &lookup);
10ac2516 3183 if (hle) {
85442b09 3184 RB_REMOVE(host_rb_tree_entry, hrbe, hle);
10ac2516
DS
3185 XFREE(MTYPE_HOST_PREFIX, hle);
3186 }
5e1b0650
DS
3187
3188 return;
3189}
3190
b7cfce93
MK
3191/*
3192 * Look up MAC hash entry.
3193 */
2dbad57f 3194static zebra_mac_t *zl3vni_rmac_lookup(zebra_l3vni_t *zl3vni,
b7cfce93
MK
3195 struct ethaddr *rmac)
3196{
3197 zebra_mac_t tmp;
3198 zebra_mac_t *pmac;
3199
3200 memset(&tmp, 0, sizeof(tmp));
3201 memcpy(&tmp.macaddr, rmac, ETH_ALEN);
3202 pmac = hash_lookup(zl3vni->rmac_table, &tmp);
2232a77c 3203
b7cfce93 3204 return pmac;
2dbad57f 3205}
2232a77c 3206
cec2e17d 3207/*
b7cfce93 3208 * Callback to allocate RMAC hash entry.
cec2e17d 3209 */
2dbad57f 3210static void *zl3vni_rmac_alloc(void *p)
d62a17ae 3211{
b7cfce93
MK
3212 const zebra_mac_t *tmp_rmac = p;
3213 zebra_mac_t *zrmac;
d62a17ae 3214
b7cfce93
MK
3215 zrmac = XCALLOC(MTYPE_MAC, sizeof(zebra_mac_t));
3216 *zrmac = *tmp_rmac;
d62a17ae 3217
b7cfce93 3218 return ((void *)zrmac);
2dbad57f 3219}
cd233079 3220
b7cfce93
MK
3221/*
3222 * Add RMAC entry to l3-vni
3223 */
996c9314 3224static zebra_mac_t *zl3vni_rmac_add(zebra_l3vni_t *zl3vni, struct ethaddr *rmac)
b7cfce93
MK
3225{
3226 zebra_mac_t tmp_rmac;
3227 zebra_mac_t *zrmac = NULL;
d62a17ae 3228
b7cfce93
MK
3229 memset(&tmp_rmac, 0, sizeof(zebra_mac_t));
3230 memcpy(&tmp_rmac.macaddr, rmac, ETH_ALEN);
3231 zrmac = hash_get(zl3vni->rmac_table, &tmp_rmac, zl3vni_rmac_alloc);
3232 assert(zrmac);
d62a17ae 3233
85442b09 3234 RB_INIT(host_rb_tree_entry, &zrmac->host_rb);
b7cfce93 3235
2dbad57f 3236 SET_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE);
3237 SET_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC);
3238
b7cfce93 3239 return zrmac;
2dbad57f 3240}
cec2e17d 3241
3242/*
b7cfce93 3243 * Delete MAC entry.
cec2e17d 3244 */
996c9314 3245static int zl3vni_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
cec2e17d 3246{
b7cfce93 3247 zebra_mac_t *tmp_rmac;
5e1b0650 3248 struct host_rb_entry *hle;
cd233079 3249
85442b09
DS
3250 while (!RB_EMPTY(host_rb_tree_entry, &zrmac->host_rb)) {
3251 hle = RB_ROOT(host_rb_tree_entry, &zrmac->host_rb);
5e1b0650 3252
85442b09 3253 RB_REMOVE(host_rb_tree_entry, &zrmac->host_rb, hle);
5e1b0650
DS
3254 XFREE(MTYPE_HOST_PREFIX, hle);
3255 }
cd233079 3256
b7cfce93
MK
3257 tmp_rmac = hash_release(zl3vni->rmac_table, zrmac);
3258 if (tmp_rmac)
3259 XFREE(MTYPE_MAC, tmp_rmac);
cd233079 3260
b7cfce93 3261 return 0;
2dbad57f 3262}
cec2e17d 3263
3264/*
b7cfce93 3265 * Install remote RMAC into the kernel.
cec2e17d 3266 */
996c9314 3267static int zl3vni_rmac_install(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
cec2e17d 3268{
b7cfce93
MK
3269 struct zebra_if *zif = NULL;
3270 struct zebra_l2info_vxlan *vxl = NULL;
cec2e17d 3271
996c9314
LB
3272 if (!(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE))
3273 || !(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC)))
b7cfce93
MK
3274 return 0;
3275
3276 zif = zl3vni->vxlan_if->info;
3277 if (!zif)
3278 return -1;
3279
3280 vxl = &zif->l2info.vxl;
3281
3282 return kernel_add_mac(zl3vni->vxlan_if, vxl->access_vlan,
996c9314 3283 &zrmac->macaddr, zrmac->fwd_info.r_vtep_ip, 0);
2dbad57f 3284}
b7cfce93
MK
3285
3286/*
3287 * Uninstall remote RMAC from the kernel.
3288 */
996c9314 3289static int zl3vni_rmac_uninstall(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
b7cfce93
MK
3290{
3291 char buf[ETHER_ADDR_STRLEN];
3292 struct zebra_if *zif = NULL;
3293 struct zebra_l2info_vxlan *vxl = NULL;
3294
996c9314
LB
3295 if (!(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE))
3296 || !(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC)))
b7cfce93
MK
3297 return 0;
3298
3299 if (!zl3vni->vxlan_if) {
9df414fe 3300 zlog_debug(
996c9314
LB
3301 "RMAC %s on L3-VNI %u hash %p couldn't be uninstalled - no vxlan_if",
3302 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
3303 zl3vni->vni, zl3vni);
b7cfce93
MK
3304 return -1;
3305 }
3306
3307 zif = zl3vni->vxlan_if->info;
3308 if (!zif)
3309 return -1;
3310
3311 vxl = &zif->l2info.vxl;
3312
3313 return kernel_del_mac(zl3vni->vxlan_if, vxl->access_vlan,
d63c1b18 3314 &zrmac->macaddr, zrmac->fwd_info.r_vtep_ip);
2dbad57f 3315}
3316
3317/* handle rmac add */
996c9314 3318static int zl3vni_remote_rmac_add(zebra_l3vni_t *zl3vni, struct ethaddr *rmac,
6134fd82 3319 struct ipaddr *vtep_ip,
3320 struct prefix *host_prefix)
2dbad57f 3321{
3322 char buf[ETHER_ADDR_STRLEN];
3323 char buf1[INET6_ADDRSTRLEN];
3324 zebra_mac_t *zrmac = NULL;
3325
3326 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
3327 if (!zrmac) {
3328
3329 zrmac = zl3vni_rmac_add(zl3vni, rmac);
3330 if (!zrmac) {
9df414fe 3331 zlog_debug(
2dbad57f 3332 "Failed to add RMAC %s L3VNI %u Remote VTEP %s",
996c9314
LB
3333 prefix_mac2str(rmac, buf, sizeof(buf)),
3334 zl3vni->vni,
3335 ipaddr2str(vtep_ip, buf1, sizeof(buf1)));
2dbad57f 3336 return -1;
3337 }
3338 memset(&zrmac->fwd_info, 0, sizeof(zrmac->fwd_info));
3339 zrmac->fwd_info.r_vtep_ip = vtep_ip->ipaddr_v4;
3340
3341 /* install rmac in kernel */
3342 zl3vni_rmac_install(zl3vni, zrmac);
3343 }
6134fd82 3344
41db76c2 3345 rb_find_or_add_host(&zrmac->host_rb, host_prefix);
5e1b0650 3346
2dbad57f 3347 return 0;
3348}
3349
3350
3351/* handle rmac delete */
22e63104 3352static void zl3vni_remote_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac,
6134fd82 3353 struct prefix *host_prefix)
2dbad57f 3354{
41db76c2 3355 rb_delete_host(&zrmac->host_rb, host_prefix);
2dbad57f 3356
85442b09 3357 if (RB_EMPTY(host_rb_tree_entry, &zrmac->host_rb)) {
2dbad57f 3358 /* uninstall from kernel */
3359 zl3vni_rmac_uninstall(zl3vni, zrmac);
3360
3361 /* del the rmac entry */
3362 zl3vni_rmac_del(zl3vni, zrmac);
3363 }
2dbad57f 3364}
b7cfce93
MK
3365
3366/*
3367 * Look up nh hash entry on a l3-vni.
3368 */
996c9314 3369static zebra_neigh_t *zl3vni_nh_lookup(zebra_l3vni_t *zl3vni, struct ipaddr *ip)
b7cfce93
MK
3370{
3371 zebra_neigh_t tmp;
3372 zebra_neigh_t *n;
3373
3374 memset(&tmp, 0, sizeof(tmp));
3375 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
3376 n = hash_lookup(zl3vni->nh_table, &tmp);
3377
3378 return n;
2dbad57f 3379}
b7cfce93
MK
3380
3381
3382/*
3383 * Callback to allocate NH hash entry on L3-VNI.
3384 */
2dbad57f 3385static void *zl3vni_nh_alloc(void *p)
b7cfce93
MK
3386{
3387 const zebra_neigh_t *tmp_n = p;
3388 zebra_neigh_t *n;
3389
3390 n = XCALLOC(MTYPE_NEIGH, sizeof(zebra_neigh_t));
3391 *n = *tmp_n;
3392
3393 return ((void *)n);
2dbad57f 3394}
b7cfce93
MK
3395
3396/*
3397 * Add neighbor entry.
3398 */
996c9314 3399static zebra_neigh_t *zl3vni_nh_add(zebra_l3vni_t *zl3vni, struct ipaddr *ip,
b7cfce93
MK
3400 struct ethaddr *mac)
3401{
3402 zebra_neigh_t tmp_n;
3403 zebra_neigh_t *n = NULL;
3404
3405 memset(&tmp_n, 0, sizeof(zebra_neigh_t));
3406 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
3407 n = hash_get(zl3vni->nh_table, &tmp_n, zl3vni_nh_alloc);
3408 assert(n);
3409
85442b09 3410 RB_INIT(host_rb_tree_entry, &n->host_rb);
6134fd82 3411
b7cfce93 3412 memcpy(&n->emac, mac, ETH_ALEN);
2dbad57f 3413 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
3414 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE_NH);
b7cfce93
MK
3415
3416 return n;
2dbad57f 3417}
b7cfce93
MK
3418
3419/*
3420 * Delete neighbor entry.
3421 */
996c9314 3422static int zl3vni_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
b7cfce93
MK
3423{
3424 zebra_neigh_t *tmp_n;
f2a503f0 3425 struct host_rb_entry *hle;
b7cfce93 3426
85442b09
DS
3427 while (!RB_EMPTY(host_rb_tree_entry, &n->host_rb)) {
3428 hle = RB_ROOT(host_rb_tree_entry, &n->host_rb);
f2a503f0 3429
85442b09 3430 RB_REMOVE(host_rb_tree_entry, &n->host_rb, hle);
f2a503f0
DS
3431 XFREE(MTYPE_HOST_PREFIX, hle);
3432 }
55aabf6f 3433
b7cfce93
MK
3434 tmp_n = hash_release(zl3vni->nh_table, n);
3435 if (tmp_n)
3436 XFREE(MTYPE_NEIGH, tmp_n);
3437
3438 return 0;
2dbad57f 3439}
b7cfce93
MK
3440
3441/*
3442 * Install remote nh as neigh into the kernel.
3443 */
996c9314 3444static int zl3vni_nh_install(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
b7cfce93 3445{
f7dae312 3446#ifdef GNU_LINUX
68e33151 3447 uint8_t flags;
f7dae312 3448#endif
68e33151
CS
3449 int ret = 0;
3450
b7cfce93
MK
3451 if (!is_l3vni_oper_up(zl3vni))
3452 return -1;
3453
996c9314
LB
3454 if (!(n->flags & ZEBRA_NEIGH_REMOTE)
3455 || !(n->flags & ZEBRA_NEIGH_REMOTE_NH))
b7cfce93 3456 return 0;
68e33151
CS
3457#ifdef GNU_LINUX
3458 flags = NTF_EXT_LEARNED;
3459 if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
3460 flags |= NTF_ROUTER;
3461 ret = kernel_add_neigh(zl3vni->svi_if, &n->ip, &n->emac, flags);
3462#endif
3463 return ret;
2dbad57f 3464}
b7cfce93
MK
3465
3466/*
3467 * Uninstall remote nh from the kernel.
3468 */
996c9314 3469static int zl3vni_nh_uninstall(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
b7cfce93 3470{
996c9314
LB
3471 if (!(n->flags & ZEBRA_NEIGH_REMOTE)
3472 || !(n->flags & ZEBRA_NEIGH_REMOTE_NH))
b7cfce93
MK
3473 return 0;
3474
fa409e1e 3475 if (!zl3vni->svi_if || !if_is_operative(zl3vni->svi_if))
3476 return 0;
3477
b7cfce93 3478 return kernel_del_neigh(zl3vni->svi_if, &n->ip);
2dbad57f 3479}
3480
3481/* add remote vtep as a neigh entry */
996c9314 3482static int zl3vni_remote_nh_add(zebra_l3vni_t *zl3vni, struct ipaddr *vtep_ip,
6134fd82 3483 struct ethaddr *rmac,
3484 struct prefix *host_prefix)
2dbad57f 3485{
3486 char buf[ETHER_ADDR_STRLEN];
3487 char buf1[INET6_ADDRSTRLEN];
3488 zebra_neigh_t *nh = NULL;
3489
3490 nh = zl3vni_nh_lookup(zl3vni, vtep_ip);
3491 if (!nh) {
3492 nh = zl3vni_nh_add(zl3vni, vtep_ip, rmac);
3493 if (!nh) {
3494
9df414fe 3495 zlog_debug(
2dbad57f 3496 "Failed to add NH as Neigh (IP %s MAC %s L3-VNI %u)",
996c9314
LB
3497 ipaddr2str(vtep_ip, buf1, sizeof(buf1)),
3498 prefix_mac2str(rmac, buf, sizeof(buf)),
2dbad57f 3499 zl3vni->vni);
3500 return -1;
3501 }
3502
3503 /* install the nh neigh in kernel */
3504 zl3vni_nh_install(zl3vni, nh);
3505 }
6134fd82 3506
f2a503f0 3507 rb_find_or_add_host(&nh->host_rb, host_prefix);
6134fd82 3508
2dbad57f 3509 return 0;
3510}
3511
3512/* handle nh neigh delete */
22e63104 3513static void zl3vni_remote_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *nh,
3514 struct prefix *host_prefix)
2dbad57f 3515{
f2a503f0 3516 rb_delete_host(&nh->host_rb, host_prefix);
2dbad57f 3517
85442b09 3518 if (RB_EMPTY(host_rb_tree_entry, &nh->host_rb)) {
2dbad57f 3519 /* uninstall from kernel */
3520 zl3vni_nh_uninstall(zl3vni, nh);
3521
3522 /* delete the nh entry */
3523 zl3vni_nh_del(zl3vni, nh);
3524 }
2dbad57f 3525}
b7cfce93 3526
3bcbba10 3527/* handle neigh update from kernel - the only thing of interest is to
3528 * readd stale entries.
3529 */
996c9314 3530static int zl3vni_local_nh_add_update(zebra_l3vni_t *zl3vni, struct ipaddr *ip,
d7c0a89a 3531 uint16_t state)
3bcbba10 3532{
c05a738b 3533#ifdef GNU_LINUX
3bcbba10 3534 zebra_neigh_t *n = NULL;
3535
3536 n = zl3vni_nh_lookup(zl3vni, ip);
3537 if (!n)
3538 return 0;
3539
3540 /* all next hop neigh are remote and installed by frr.
3541 * If the kernel has aged this entry, re-install.
3542 */
3543 if (state & NUD_STALE)
3544 zl3vni_nh_install(zl3vni, n);
c05a738b 3545#endif
3bcbba10 3546 return 0;
3547}
3548
8c9b80b9 3549/* handle neigh delete from kernel */
996c9314 3550static int zl3vni_local_nh_del(zebra_l3vni_t *zl3vni, struct ipaddr *ip)
8c9b80b9
MK
3551{
3552 zebra_neigh_t *n = NULL;
3553
3554 n = zl3vni_nh_lookup(zl3vni, ip);
3555 if (!n)
3556 return 0;
3557
3558 /* all next hop neigh are remote and installed by frr.
3559 * If we get an age out notification for these neigh entries, we have to
523cafc4 3560 * install it back
3561 */
8c9b80b9
MK
3562 zl3vni_nh_install(zl3vni, n);
3563
3564 return 0;
3565}
3566
b7cfce93
MK
3567/*
3568 * Hash function for L3 VNI.
3569 */
3570static unsigned int l3vni_hash_keymake(void *p)
3571{
3572 const zebra_l3vni_t *zl3vni = p;
3573
3574 return jhash_1word(zl3vni->vni, 0);
3575}
3576
3577/*
3578 * Compare 2 L3 VNI hash entries.
3579 */
3580static int l3vni_hash_cmp(const void *p1, const void *p2)
3581{
3582 const zebra_l3vni_t *zl3vni1 = p1;
3583 const zebra_l3vni_t *zl3vni2 = p2;
3584
3585 return (zl3vni1->vni == zl3vni2->vni);
3586}
3587
3588/*
3589 * Callback to allocate L3 VNI hash entry.
3590 */
3591static void *zl3vni_alloc(void *p)
3592{
3593 zebra_l3vni_t *zl3vni = NULL;
3594 const zebra_l3vni_t *tmp_l3vni = p;
3595
3596 zl3vni = XCALLOC(MTYPE_ZL3VNI, sizeof(zebra_l3vni_t));
3597 zl3vni->vni = tmp_l3vni->vni;
3598 return ((void *)zl3vni);
3599}
3600
3601/*
3602 * Look up L3 VNI hash entry.
3603 */
3604static zebra_l3vni_t *zl3vni_lookup(vni_t vni)
3605{
3606 struct zebra_ns *zns;
3607 zebra_l3vni_t tmp_l3vni;
3608 zebra_l3vni_t *zl3vni = NULL;
3609
3610 zns = zebra_ns_lookup(NS_DEFAULT);
3611 assert(zns);
3612 memset(&tmp_l3vni, 0, sizeof(zebra_l3vni_t));
3613 tmp_l3vni.vni = vni;
3614 zl3vni = hash_lookup(zns->l3vni_table, &tmp_l3vni);
3615
3616 return zl3vni;
3617}
3618
3619/*
3620 * Add L3 VNI hash entry.
3621 */
3622static zebra_l3vni_t *zl3vni_add(vni_t vni, vrf_id_t vrf_id)
3623{
3624 zebra_l3vni_t tmp_zl3vni;
3625 struct zebra_ns *zns = NULL;
3626 zebra_l3vni_t *zl3vni = NULL;
3627
3628 zns = zebra_ns_lookup(NS_DEFAULT);
3629 assert(zns);
3630
3631 memset(&tmp_zl3vni, 0, sizeof(zebra_l3vni_t));
3632 tmp_zl3vni.vni = vni;
3633
3634 zl3vni = hash_get(zns->l3vni_table, &tmp_zl3vni, zl3vni_alloc);
3635 assert(zl3vni);
3636
3637 zl3vni->vrf_id = vrf_id;
3638 zl3vni->svi_if = NULL;
3639 zl3vni->vxlan_if = NULL;
3640 zl3vni->l2vnis = list_new();
3641 zl3vni->l2vnis->cmp = (int (*)(void *, void *))vni_hash_cmp;
3642
3643 /* Create hash table for remote RMAC */
996c9314
LB
3644 zl3vni->rmac_table = hash_create(mac_hash_keymake, mac_cmp,
3645 "Zebra L3-VNI RMAC-Table");
b7cfce93
MK
3646
3647 /* Create hash table for neighbors */
3648 zl3vni->nh_table = hash_create(neigh_hash_keymake, neigh_cmp,
996c9314 3649 "Zebra L3-VNI next-hop table");
b7cfce93
MK
3650
3651 return zl3vni;
3652}
3653
3654/*
3655 * Delete L3 VNI hash entry.
3656 */
3657static int zl3vni_del(zebra_l3vni_t *zl3vni)
3658{
3659 struct zebra_ns *zns;
3660 zebra_l3vni_t *tmp_zl3vni;
3661
3662 zns = zebra_ns_lookup(NS_DEFAULT);
3663 assert(zns);
3664
3665 /* free the list of l2vnis */
3666 list_delete_and_null(&zl3vni->l2vnis);
3667 zl3vni->l2vnis = NULL;
3668
3669 /* Free the rmac table */
3670 hash_free(zl3vni->rmac_table);
3671 zl3vni->rmac_table = NULL;
3672
3673 /* Free the nh table */
3674 hash_free(zl3vni->nh_table);
3675 zl3vni->nh_table = NULL;
3676
3677 /* Free the VNI hash entry and allocated memory. */
3678 tmp_zl3vni = hash_release(zns->l3vni_table, zl3vni);
3679 if (tmp_zl3vni)
3680 XFREE(MTYPE_ZL3VNI, tmp_zl3vni);
3681
3682 return 0;
3683}
3684
b7cfce93
MK
3685static struct interface *zl3vni_map_to_vxlan_if(zebra_l3vni_t *zl3vni)
3686{
3687 struct zebra_ns *zns = NULL;
3688 struct route_node *rn = NULL;
3689 struct interface *ifp = NULL;
3690
3691 /* loop through all vxlan-interface */
3692 zns = zebra_ns_lookup(NS_DEFAULT);
3693 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3694
3695 struct zebra_if *zif = NULL;
3696 struct zebra_l2info_vxlan *vxl = NULL;
3697
3698 ifp = (struct interface *)rn->info;
3699 if (!ifp)
3700 continue;
3701
3702 zif = ifp->info;
3703 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
3704 continue;
3705
3706 vxl = &zif->l2info.vxl;
b67a60d2 3707 if (vxl->vni == zl3vni->vni) {
3708 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93 3709 return ifp;
b67a60d2 3710 }
b7cfce93
MK
3711 }
3712
3713 return NULL;
3714}
3715
3716static struct interface *zl3vni_map_to_svi_if(zebra_l3vni_t *zl3vni)
3717{
996c9314 3718 struct zebra_if *zif = NULL; /* zebra_if for vxlan_if */
b7cfce93
MK
3719 struct zebra_l2info_vxlan *vxl = NULL; /* l2 info for vxlan_if */
3720
2aeb403d 3721 if (!zl3vni)
3722 return NULL;
3723
b7cfce93
MK
3724 if (!zl3vni->vxlan_if)
3725 return NULL;
3726
3727 zif = zl3vni->vxlan_if->info;
3728 if (!zif)
3729 return NULL;
3730
3731 vxl = &zif->l2info.vxl;
3732
3733 return zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
3734}
3735
3736static zebra_l3vni_t *zl3vni_from_vrf(vrf_id_t vrf_id)
3737{
3738 struct zebra_vrf *zvrf = NULL;
3739
3740 zvrf = zebra_vrf_lookup_by_id(vrf_id);
3741 if (!zvrf)
3742 return NULL;
3743
3744 return zl3vni_lookup(zvrf->l3vni);
3745}
3746
3747/*
3748 * Map SVI and associated bridge to a VNI. This is invoked upon getting
3749 * neighbor notifications, to see if they are of interest.
3750 */
3751static zebra_l3vni_t *zl3vni_from_svi(struct interface *ifp,
3752 struct interface *br_if)
3753{
3754 int found = 0;
3755 vlanid_t vid = 0;
d7c0a89a 3756 uint8_t bridge_vlan_aware = 0;
b7cfce93
MK
3757 zebra_l3vni_t *zl3vni = NULL;
3758 struct zebra_ns *zns = NULL;
3759 struct route_node *rn = NULL;
3760 struct zebra_if *zif = NULL;
3761 struct interface *tmp_if = NULL;
3762 struct zebra_l2info_bridge *br = NULL;
3763 struct zebra_l2info_vxlan *vxl = NULL;
3764
3765 if (!br_if)
3766 return NULL;
3767
3768 /* Make sure the linked interface is a bridge. */
3769 if (!IS_ZEBRA_IF_BRIDGE(br_if))
3770 return NULL;
3771
3772 /* Determine if bridge is VLAN-aware or not */
3773 zif = br_if->info;
3774 assert(zif);
3775 br = &zif->l2info.br;
3776 bridge_vlan_aware = br->vlan_aware;
3777 if (bridge_vlan_aware) {
3778 struct zebra_l2info_vlan *vl;
3779
3780 if (!IS_ZEBRA_IF_VLAN(ifp))
3781 return NULL;
3782
3783 zif = ifp->info;
3784 assert(zif);
3785 vl = &zif->l2info.vl;
3786 vid = vl->vid;
3787 }
3788
3789 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
3790 /* TODO: Optimize with a hash. */
3791 zns = zebra_ns_lookup(NS_DEFAULT);
3792 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3793 tmp_if = (struct interface *)rn->info;
3794 if (!tmp_if)
3795 continue;
3796 zif = tmp_if->info;
3797 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
3798 continue;
3799 if (!if_is_operative(tmp_if))
3800 continue;
3801 vxl = &zif->l2info.vxl;
3802
3803 if (zif->brslave_info.br_if != br_if)
3804 continue;
3805
3806 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
3807 found = 1;
3808 break;
3809 }
3810 }
3811
3812 if (!found)
3813 return NULL;
3814
3815 zl3vni = zl3vni_lookup(vxl->vni);
3816 return zl3vni;
3817}
3818
3819/*
3820 * Inform BGP about l3-vni.
3821 */
3822static int zl3vni_send_add_to_client(zebra_l3vni_t *zl3vni)
3823{
3824 struct stream *s = NULL;
3825 struct zserv *client = NULL;
3826 struct ethaddr rmac;
3827 char buf[ETHER_ADDR_STRLEN];
3828
21ccc0cf 3829 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
b7cfce93
MK
3830 /* BGP may not be running. */
3831 if (!client)
3832 return 0;
3833
3834 /* get the rmac */
3835 memset(&rmac, 0, sizeof(struct ethaddr));
3836 zl3vni_get_rmac(zl3vni, &rmac);
3837
1002497a 3838 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
b7cfce93 3839
996c9314 3840 zclient_create_header(s, ZEBRA_L3VNI_ADD, zl3vni_vrf_id(zl3vni));
b7cfce93
MK
3841 stream_putl(s, zl3vni->vni);
3842 stream_put(s, &rmac, sizeof(struct ethaddr));
b67a60d2 3843 stream_put_in_addr(s, &zl3vni->local_vtep_ip);
c48d9f5f 3844 stream_put(s, &zl3vni->filter, sizeof(int));
b7cfce93
MK
3845
3846 /* Write packet size. */
3847 stream_putw_at(s, 0, stream_get_endp(s));
3848
3849 if (IS_ZEBRA_DEBUG_VXLAN)
c48d9f5f 3850 zlog_debug(
996c9314
LB
3851 "Send L3_VNI_ADD %u VRF %s RMAC %s local-ip %s filter %s to %s",
3852 zl3vni->vni, vrf_id_to_name(zl3vni_vrf_id(zl3vni)),
3853 prefix_mac2str(&rmac, buf, sizeof(buf)),
3854 inet_ntoa(zl3vni->local_vtep_ip),
3855 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
3856 ? "prefix-routes-only"
3857 : "none",
3858 zebra_route_string(client->proto));
b7cfce93
MK
3859
3860 client->l3vniadd_cnt++;
21ccc0cf 3861 return zserv_send_message(client, s);
b7cfce93
MK
3862}
3863
3864/*
3865 * Inform BGP about local l3-VNI deletion.
3866 */
3867static int zl3vni_send_del_to_client(zebra_l3vni_t *zl3vni)
3868{
3869 struct stream *s = NULL;
3870 struct zserv *client = NULL;
3871
21ccc0cf 3872 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
b7cfce93
MK
3873 /* BGP may not be running. */
3874 if (!client)
3875 return 0;
3876
1002497a 3877 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
b7cfce93 3878
996c9314 3879 zclient_create_header(s, ZEBRA_L3VNI_DEL, zl3vni_vrf_id(zl3vni));
b7cfce93
MK
3880 stream_putl(s, zl3vni->vni);
3881
3882 /* Write packet size. */
3883 stream_putw_at(s, 0, stream_get_endp(s));
3884
3885 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 3886 zlog_debug("Send L3_VNI_DEL %u VRF %s to %s", zl3vni->vni,
b7cfce93
MK
3887 vrf_id_to_name(zl3vni_vrf_id(zl3vni)),
3888 zebra_route_string(client->proto));
3889
3890 client->l3vnidel_cnt++;
21ccc0cf 3891 return zserv_send_message(client, s);
b7cfce93
MK
3892}
3893
3894static void zebra_vxlan_process_l3vni_oper_up(zebra_l3vni_t *zl3vni)
3895{
2aeb403d 3896 if (!zl3vni)
3897 return;
3898
b7cfce93
MK
3899 /* send l3vni add to BGP */
3900 zl3vni_send_add_to_client(zl3vni);
3901}
3902
3903static void zebra_vxlan_process_l3vni_oper_down(zebra_l3vni_t *zl3vni)
3904{
2aeb403d 3905 if (!zl3vni)
3906 return;
3907
b7cfce93
MK
3908 /* send l3-vni del to BGP*/
3909 zl3vni_send_del_to_client(zl3vni);
3910}
3911
996c9314 3912static void zvni_add_to_l3vni_list(struct hash_backet *backet, void *ctxt)
b7cfce93 3913{
996c9314
LB
3914 zebra_vni_t *zvni = (zebra_vni_t *)backet->data;
3915 zebra_l3vni_t *zl3vni = (zebra_l3vni_t *)ctxt;
b7cfce93
MK
3916
3917 if (zvni->vrf_id == zl3vni_vrf_id(zl3vni))
3918 listnode_add_sort(zl3vni->l2vnis, zvni);
3919}
3920
b7cfce93
MK
3921/*
3922 * handle transition of vni from l2 to l3 and vice versa
3923 */
996c9314
LB
3924static int zebra_vxlan_handle_vni_transition(struct zebra_vrf *zvrf, vni_t vni,
3925 int add)
b7cfce93
MK
3926{
3927 zebra_vni_t *zvni = NULL;
3928
3929 /* There is a possibility that VNI notification was already received
3930 * from kernel and we programmed it as L2-VNI
3931 * In such a case we need to delete this L2-VNI first, so
3932 * that it can be reprogrammed as L3-VNI in the system. It is also
3933 * possible that the vrf-vni mapping is removed from FRR while the vxlan
3934 * interface is still present in kernel. In this case to keep it
523cafc4 3935 * symmetric, we will delete the l3-vni and reprogram it as l2-vni
3936 */
b7cfce93
MK
3937 if (add) {
3938 /* Locate hash entry */
3939 zvni = zvni_lookup(vni);
3940 if (!zvni)
3941 return 0;
3942
3943 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 3944 zlog_debug("Del L2-VNI %u - transition to L3-VNI", vni);
b7cfce93
MK
3945
3946 /* Delete VNI from BGP. */
3947 zvni_send_del_to_client(zvni->vni);
3948
3949 /* Free up all neighbors and MAC, if any. */
3950 zvni_neigh_del_all(zvni, 0, 0, DEL_ALL_NEIGH);
3951 zvni_mac_del_all(zvni, 0, 0, DEL_ALL_MAC);
3952
3953 /* Free up all remote VTEPs, if any. */
3954 zvni_vtep_del_all(zvni, 0);
3955
3956 /* Delete the hash entry. */
3957 if (zvni_del(zvni)) {
e914ccbe 3958 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
1c50c1c0
QY
3959 "Failed to del VNI hash %p, VNI %u", zvni,
3960 zvni->vni);
b7cfce93
MK
3961 return -1;
3962 }
3963 } else {
3964 /* TODO_MITESH: This needs to be thought through. We don't have
3965 * enough information at this point to reprogram the vni as
3966 * l2-vni. One way is to store the required info in l3-vni and
523cafc4 3967 * used it solely for this purpose
3968 */
b7cfce93
MK
3969 }
3970
3971 return 0;
3972}
3973
5e06422c 3974/* delete and uninstall rmac hash entry */
996c9314 3975static void zl3vni_del_rmac_hash_entry(struct hash_backet *backet, void *ctx)
5e06422c
MK
3976{
3977 zebra_mac_t *zrmac = NULL;
3978 zebra_l3vni_t *zl3vni = NULL;
3979
3980 zrmac = (zebra_mac_t *)backet->data;
3981 zl3vni = (zebra_l3vni_t *)ctx;
3982 zl3vni_rmac_uninstall(zl3vni, zrmac);
3983 zl3vni_rmac_del(zl3vni, zrmac);
3984}
3985
3986/* delete and uninstall nh hash entry */
996c9314 3987static void zl3vni_del_nh_hash_entry(struct hash_backet *backet, void *ctx)
5e06422c
MK
3988{
3989 zebra_neigh_t *n = NULL;
3990 zebra_l3vni_t *zl3vni = NULL;
3991
3992 n = (zebra_neigh_t *)backet->data;
3993 zl3vni = (zebra_l3vni_t *)ctx;
3994 zl3vni_nh_uninstall(zl3vni, n);
3995 zl3vni_nh_del(zl3vni, n);
3996}
3997
996c9314
LB
3998static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
3999 uint16_t cmd)
31310b25
MK
4000{
4001 struct zserv *client = NULL;
4002 struct stream *s = NULL;
4003 char buf[PREFIX_STRLEN];
4004
21ccc0cf 4005 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
31310b25
MK
4006 /* BGP may not be running. */
4007 if (!client)
4008 return 0;
4009
1002497a 4010 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
31310b25 4011
22bd3e94 4012 zclient_create_header(s, cmd, vrf_id);
31310b25
MK
4013 stream_put(s, p, sizeof(struct prefix));
4014
4015 /* Write packet size. */
4016 stream_putw_at(s, 0, stream_get_endp(s));
4017
4018 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 4019 zlog_debug("Send ip prefix %s %s on vrf %s",
31310b25
MK
4020 prefix2str(p, buf, sizeof(buf)),
4021 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
4022 vrf_id_to_name(vrf_id));
4023
4024 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD)
4025 client->prefixadd_cnt++;
4026 else
4027 client->prefixdel_cnt++;
4028
21ccc0cf 4029 return zserv_send_message(client, s);
31310b25
MK
4030}
4031
a9a76262
MK
4032/* re-add remote rmac if needed */
4033static int zebra_vxlan_readd_remote_rmac(zebra_l3vni_t *zl3vni,
996c9314 4034 struct ethaddr *rmac)
a9a76262
MK
4035{
4036 char buf[ETHER_ADDR_STRLEN];
4037 zebra_mac_t *zrmac = NULL;
4038
4039 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
4040 if (!zrmac)
4041 return 0;
4042
4043 if (IS_ZEBRA_DEBUG_VXLAN)
4044 zlog_debug("Del remote RMAC %s L3VNI %u - readd",
996c9314 4045 prefix_mac2str(rmac, buf, sizeof(buf)), zl3vni->vni);
a9a76262
MK
4046
4047 zl3vni_rmac_install(zl3vni, zrmac);
4048 return 0;
4049}
4050
f07e1c99 4051/* Process a remote MACIP add from BGP. */
4052static void process_remote_macip_add(vni_t vni,
4053 struct ethaddr *macaddr,
9df2b997 4054 uint16_t ipa_len,
f07e1c99 4055 struct ipaddr *ipaddr,
9df2b997 4056 uint8_t flags,
f07e1c99 4057 uint32_t seq,
4058 struct in_addr vtep_ip)
c48d9f5f 4059{
f07e1c99 4060 zebra_vni_t *zvni;
4061 zebra_vtep_t *zvtep;
4062 zebra_mac_t *mac, *old_mac;
4063 zebra_neigh_t *n = NULL;
4064 int update_mac = 0, update_neigh = 0;
4065 char buf[ETHER_ADDR_STRLEN];
4066 char buf1[INET6_ADDRSTRLEN];
4067 struct interface *ifp = NULL;
4068 struct zebra_if *zif = NULL;
4069 uint32_t tmp_seq;
a37f4598 4070 bool sticky;
4071 bool remote_gw;
4072 bool is_router;
c48d9f5f 4073
f07e1c99 4074 /* Locate VNI hash entry - expected to exist. */
4075 zvni = zvni_lookup(vni);
4076 if (!zvni) {
4077 zlog_warn("Unknown VNI %u upon remote MACIP ADD", vni);
4078 return;
4079 }
2dbad57f 4080
f07e1c99 4081 ifp = zvni->vxlan_if;
4082 if (ifp)
4083 zif = ifp->info;
4084 if (!ifp ||
4085 !if_is_operative(ifp) ||
4086 !zif ||
4087 !zif->brslave_info.br_if) {
4088 zlog_warn("Ignoring remote MACIP ADD VNI %u, invalid interface state or info",
4089 vni);
2dbad57f 4090 return;
f07e1c99 4091 }
2dbad57f 4092
f07e1c99 4093 /* The remote VTEP specified should normally exist, but it is
4094 * possible that when peering comes up, peer may advertise MACIP
4095 * routes before advertising type-3 routes.
f50dc5e6 4096 */
f07e1c99 4097 zvtep = zvni_vtep_find(zvni, &vtep_ip);
4098 if (!zvtep) {
4099 if (zvni_vtep_add(zvni, &vtep_ip) == NULL) {
4100 flog_err(
e914ccbe 4101 EC_ZEBRA_VTEP_ADD_FAILED,
f07e1c99 4102 "Failed to add remote VTEP, VNI %u zvni %p upon remote MACIP ADD",
4103 vni, zvni);
4104 return;
4105 }
2dbad57f 4106
f07e1c99 4107 zvni_vtep_install(zvni, &vtep_ip);
4108 }
f50dc5e6 4109
a37f4598 4110 sticky = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
4111 remote_gw = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
4112 is_router = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
2dbad57f 4113
f07e1c99 4114 mac = zvni_mac_lookup(zvni, macaddr);
2dbad57f 4115
f07e1c99 4116 /* Ignore if the mac is already present as a gateway mac */
4117 if (mac &&
4118 CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW) &&
4119 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) {
4120 if (IS_ZEBRA_DEBUG_VXLAN)
4121 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as MAC is already configured as gateway MAC",
4122 vni,
4123 prefix_mac2str(macaddr, buf, sizeof(buf)),
4124 ipa_len ? " IP " : "",
4125 ipa_len ?
4126 ipaddr2str(ipaddr, buf1, sizeof(buf1)) : "");
2dbad57f 4127 return;
f07e1c99 4128 }
2dbad57f 4129
f07e1c99 4130 /* check if the remote MAC is unknown or has a change.
4131 * If so, that needs to be updated first. Note that client could
4132 * install MAC and MACIP separately or just install the latter.
4133 */
4134 if (!mac
4135 || !CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
a37f4598 4136 || sticky != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)
4137 || remote_gw != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW)
f07e1c99 4138 || !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &vtep_ip)
4139 || seq != mac->rem_seq)
4140 update_mac = 1;
4141
4142 if (update_mac) {
4143 if (!mac) {
4144 mac = zvni_mac_add(zvni, macaddr);
4145 if (!mac) {
4146 zlog_warn(
4147 "Failed to add MAC %s VNI %u Remote VTEP %s",
4148 prefix_mac2str(macaddr, buf,
4149 sizeof(buf)),
4150 vni, inet_ntoa(vtep_ip));
4151 return;
4152 }
4153
4154 /* Is this MAC created for a MACIP? */
4155 if (ipa_len)
4156 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
4157 } else {
4158 const char *mac_type;
4159
4160 /* When host moves but changes its (MAC,IP)
4161 * binding, BGP may install a MACIP entry that
4162 * corresponds to "older" location of the host
4163 * in transient situations (because {IP1,M1}
4164 * is a different route from {IP1,M2}). Check
4165 * the sequence number and ignore this update
4166 * if appropriate.
4167 */
4168 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
4169 tmp_seq = mac->loc_seq;
4170 mac_type = "local";
4171 } else {
4172 tmp_seq = mac->rem_seq;
4173 mac_type = "remote";
4174 }
4175 if (seq < tmp_seq) {
4176 if (IS_ZEBRA_DEBUG_VXLAN)
4177 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as existing %s MAC has higher seq %u",
4178 vni,
4179 prefix_mac2str(macaddr,
4180 buf, sizeof(buf)),
4181 ipa_len ? " IP " : "",
4182 ipa_len ?
4183 ipaddr2str(ipaddr,
4184 buf1, sizeof(buf1)) : "",
4185 mac_type,
4186 tmp_seq);
4187 return;
4188 }
4189 }
4190
4191 /* Set "auto" and "remote" forwarding info. */
4192 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
4193 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
4194 SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
4195 mac->fwd_info.r_vtep_ip = vtep_ip;
4196
4197 if (sticky)
4198 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
4199 else
4200 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
4201
4202 if (remote_gw)
4203 SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW);
4204 else
4205 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW);
4206
4207 zvni_process_neigh_on_remote_mac_add(zvni, mac);
4208
4209 /* Install the entry. */
4210 zvni_mac_install(zvni, mac);
4211 }
4212
4213 /* Update seq number. */
4214 mac->rem_seq = seq;
4215
4216 /* If there is no IP, return after clearing AUTO flag of MAC. */
4217 if (!ipa_len) {
4218 UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
4219 return;
4220 }
4221
4222 /* Check if the remote neighbor itself is unknown or has a
4223 * change. If so, create or update and then install the entry.
4224 */
4225 n = zvni_neigh_lookup(zvni, ipaddr);
4226 if (!n
4227 || !CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
a37f4598 4228 || is_router != !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)
f07e1c99 4229 || (memcmp(&n->emac, macaddr, sizeof(*macaddr)) != 0)
4230 || !IPV4_ADDR_SAME(&n->r_vtep_ip, &vtep_ip)
4231 || seq != n->rem_seq)
4232 update_neigh = 1;
4233
4234 if (update_neigh) {
4235 if (!n) {
4236 n = zvni_neigh_add(zvni, ipaddr, macaddr);
4237 if (!n) {
4238 zlog_warn(
4239 "Failed to add Neigh %s MAC %s VNI %u Remote VTEP %s",
4240 ipaddr2str(ipaddr, buf1,
4241 sizeof(buf1)),
4242 prefix_mac2str(macaddr, buf,
4243 sizeof(buf)),
4244 vni, inet_ntoa(vtep_ip));
4245 return;
4246 }
4247
4248 } else {
4249 const char *n_type;
4250
4251 /* When host moves but changes its (MAC,IP)
4252 * binding, BGP may install a MACIP entry that
4253 * corresponds to "older" location of the host
4254 * in transient situations (because {IP1,M1}
4255 * is a different route from {IP1,M2}). Check
4256 * the sequence number and ignore this update
4257 * if appropriate.
4258 */
4259 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
4260 tmp_seq = n->loc_seq;
4261 n_type = "local";
4262 } else {
4263 tmp_seq = n->rem_seq;
4264 n_type = "remote";
4265 }
4266 if (seq < tmp_seq) {
4267 if (IS_ZEBRA_DEBUG_VXLAN)
4268 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as existing %s Neigh has higher seq %u",
4269 vni,
4270 prefix_mac2str(macaddr,
4271 buf, sizeof(buf)),
4272 ipa_len ? " IP " : "",
4273 ipa_len ?
4274 ipaddr2str(ipaddr,
4275 buf1, sizeof(buf1)) : "",
4276 n_type,
4277 tmp_seq);
4278 return;
4279 }
4280 if (memcmp(&n->emac, macaddr, sizeof(*macaddr)) != 0) {
4281 /* MAC change, send a delete for old
4282 * neigh if learnt locally.
4283 */
4284 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) &&
4285 IS_ZEBRA_NEIGH_ACTIVE(n))
4286 zvni_neigh_send_del_to_client(
4287 zvni->vni, &n->ip,
4288 &n->emac, 0);
4289
4290 /* update neigh list for macs */
4291 old_mac = zvni_mac_lookup(zvni, &n->emac);
4292 if (old_mac) {
4293 listnode_delete(old_mac->neigh_list, n);
fe697c6b 4294 zvni_deref_ip2mac(zvni, old_mac);
f07e1c99 4295 }
4296 listnode_add_sort(mac->neigh_list, n);
4297 memcpy(&n->emac, macaddr, ETH_ALEN);
4298 }
4299 }
4300
4301 /* Set "remote" forwarding info. */
4302 UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
4303 n->r_vtep_ip = vtep_ip;
4304 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
4305
4306 /* Set router flag (R-bit) to this Neighbor entry */
4307 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG))
4308 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
f190902f 4309 else
4310 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
f07e1c99 4311
4312 /* Install the entry. */
4313 zvni_neigh_install(zvni, n);
4314 }
4315
4316 /* Update seq number. */
4317 n->rem_seq = seq;
4318}
4319
4320/* Process a remote MACIP delete from BGP. */
4321static void process_remote_macip_del(vni_t vni,
4322 struct ethaddr *macaddr,
9df2b997 4323 uint16_t ipa_len,
f07e1c99 4324 struct ipaddr *ipaddr,
4325 struct in_addr vtep_ip)
4326{
4327 zebra_vni_t *zvni;
4328 zebra_mac_t *mac = NULL;
4329 zebra_neigh_t *n = NULL;
4330 struct interface *ifp = NULL;
4331 struct zebra_if *zif = NULL;
4332 char buf[ETHER_ADDR_STRLEN];
4333 char buf1[INET6_ADDRSTRLEN];
4334
4335 /* Locate VNI hash entry - expected to exist. */
4336 zvni = zvni_lookup(vni);
4337 if (!zvni) {
4338 if (IS_ZEBRA_DEBUG_VXLAN)
4339 zlog_debug("Unknown VNI %u upon remote MACIP DEL", vni);
4340 return;
4341 }
4342
4343 ifp = zvni->vxlan_if;
4344 if (ifp)
4345 zif = ifp->info;
4346 if (!ifp ||
4347 !if_is_operative(ifp) ||
4348 !zif ||
4349 !zif->brslave_info.br_if) {
4350 if (IS_ZEBRA_DEBUG_VXLAN)
4351 zlog_debug("Ignoring remote MACIP DEL VNI %u, invalid interface state or info",
4352 vni);
4353 return;
4354 }
4355
4356 /* The remote VTEP specified is normally expected to exist, but
4357 * it is possible that the peer may delete the VTEP before deleting
4358 * any MACs referring to the VTEP, in which case the handler (see
4359 * remote_vtep_del) would have already deleted the MACs.
4360 */
4361 if (!zvni_vtep_find(zvni, &vtep_ip))
4362 return;
4363
4364 mac = zvni_mac_lookup(zvni, macaddr);
4365 if (ipa_len)
4366 n = zvni_neigh_lookup(zvni, ipaddr);
4367
4368 if (n && !mac) {
4369 zlog_warn("Failed to locate MAC %s for neigh %s VNI %u upon remote MACIP DEL",
4370 prefix_mac2str(macaddr, buf, sizeof(buf)),
4371 ipaddr2str(ipaddr, buf1, sizeof(buf1)), vni);
4372 return;
4373 }
4374
4375 /* If the remote mac or neighbor doesn't exist there is nothing
4376 * more to do. Otherwise, uninstall the entry and then remove it.
4377 */
4378 if (!mac && !n)
4379 return;
4380
4381 /* Ignore the delete if this mac is a gateway mac-ip */
4382 if (mac
4383 && CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
4384 && CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW)) {
4385 zlog_warn(
4386 "Ignore remote MACIP DEL VNI %u MAC %s%s%s as MAC is already configured as gateway MAC",
4387 vni,
4388 prefix_mac2str(macaddr, buf, sizeof(buf)),
4389 ipa_len ? " IP " : "",
4390 ipa_len ?
4391 ipaddr2str(ipaddr, buf1, sizeof(buf1)) : "");
4392 return;
4393 }
4394
4395 /* Uninstall remote neighbor or MAC. */
4396 if (n) {
4397 /* When the MAC changes for an IP, it is possible the
4398 * client may update the new MAC before trying to delete the
4399 * "old" neighbor (as these are two different MACIP routes).
4400 * Do the delete only if the MAC matches.
4401 */
4402 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
4403 && (memcmp(n->emac.octet, macaddr->octet, ETH_ALEN) == 0)) {
4404 zvni_neigh_uninstall(zvni, n);
4405 zvni_neigh_del(zvni, n);
fe697c6b 4406 zvni_deref_ip2mac(zvni, mac);
f07e1c99 4407 }
4408 } else {
4409 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
4410 zvni_process_neigh_on_remote_mac_del(zvni, mac);
4411
fe697c6b 4412 /* If all remote neighbors referencing a remote MAC
4413 * go away, we need to uninstall the MAC.
4414 */
4415 if (remote_neigh_count(mac) == 0) {
d63c1b18 4416 zvni_mac_uninstall(zvni, mac);
fe697c6b 4417 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
4418 }
4419 if (list_isempty(mac->neigh_list))
f07e1c99 4420 zvni_mac_del(zvni, mac);
fe697c6b 4421 else
f07e1c99 4422 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
4423 }
4424 }
4425}
4426
4427
b7cfce93
MK
4428/* Public functions */
4429
c48d9f5f
MK
4430int is_l3vni_for_prefix_routes_only(vni_t vni)
4431{
4432 zebra_l3vni_t *zl3vni = NULL;
4433
4434 zl3vni = zl3vni_lookup(vni);
4435 if (!zl3vni)
4436 return 0;
4437
4438 return CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY) ? 1 : 0;
4439}
4440
2dbad57f 4441/* handle evpn route in vrf table */
996c9314
LB
4442void zebra_vxlan_evpn_vrf_route_add(vrf_id_t vrf_id, struct ethaddr *rmac,
4443 struct ipaddr *vtep_ip,
4444 struct prefix *host_prefix)
2dbad57f 4445{
4446 zebra_l3vni_t *zl3vni = NULL;
f50dc5e6 4447 struct ipaddr ipv4_vtep;
2dbad57f 4448
4449 zl3vni = zl3vni_from_vrf(vrf_id);
4450 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
4451 return;
4452
3518f352 4453 /*
f50dc5e6
MK
4454 * add the next hop neighbor -
4455 * neigh to be installed is the ipv6 nexthop neigh
4456 */
3518f352 4457 zl3vni_remote_nh_add(zl3vni, vtep_ip, rmac, host_prefix);
2dbad57f 4458
f50dc5e6
MK
4459 /*
4460 * if the remote vtep is a ipv4 mapped ipv6 address convert it to ipv4
4461 * address. Rmac is programmed against the ipv4 vtep because we only
4462 * support ipv4 tunnels in the h/w right now
4463 */
4464 memset(&ipv4_vtep, 0, sizeof(struct ipaddr));
4465 ipv4_vtep.ipa_type = IPADDR_V4;
4466 if (vtep_ip->ipa_type == IPADDR_V6)
4467 ipv4_mapped_ipv6_to_ipv4(&vtep_ip->ipaddr_v6,
4468 &(ipv4_vtep.ipaddr_v4));
4469 else
4470 memcpy(&(ipv4_vtep.ipaddr_v4), &vtep_ip->ipaddr_v4,
4471 sizeof(struct in_addr));
4472
3518f352
DS
4473 /*
4474 * add the rmac - remote rmac to be installed is against the ipv4
f50dc5e6
MK
4475 * nexthop address
4476 */
3518f352 4477 zl3vni_remote_rmac_add(zl3vni, rmac, &ipv4_vtep, host_prefix);
2dbad57f 4478}
4479
4480/* handle evpn vrf route delete */
22e63104 4481void zebra_vxlan_evpn_vrf_route_del(vrf_id_t vrf_id,
996c9314
LB
4482 struct ipaddr *vtep_ip,
4483 struct prefix *host_prefix)
2dbad57f 4484{
4485 zebra_l3vni_t *zl3vni = NULL;
22e63104 4486 zebra_neigh_t *nh = NULL;
4487 zebra_mac_t *zrmac = NULL;
2dbad57f 4488
4489 zl3vni = zl3vni_from_vrf(vrf_id);
5e06422c 4490 if (!zl3vni)
2dbad57f 4491 return;
4492
22e63104 4493 /* find the next hop entry and rmac entry */
4494 nh = zl3vni_nh_lookup(zl3vni, vtep_ip);
4495 if (!nh)
4496 return;
4497 zrmac = zl3vni_rmac_lookup(zl3vni, &nh->emac);
4498
2dbad57f 4499 /* delete the next hop entry */
22e63104 4500 zl3vni_remote_nh_del(zl3vni, nh, host_prefix);
2dbad57f 4501
4502 /* delete the rmac entry */
22e63104 4503 if (zrmac)
4504 zl3vni_remote_rmac_del(zl3vni, zrmac, host_prefix);
4505
2dbad57f 4506}
4507
996c9314 4508void zebra_vxlan_print_specific_rmac_l3vni(struct vty *vty, vni_t l3vni,
9f049418 4509 struct ethaddr *rmac, bool use_json)
9aa741ea
MK
4510{
4511 zebra_l3vni_t *zl3vni = NULL;
4512 zebra_mac_t *zrmac = NULL;
316f4ca4 4513 json_object *json = NULL;
9aa741ea 4514
316f4ca4
MK
4515 if (!is_evpn_enabled()) {
4516 if (use_json)
4517 vty_out(vty, "{}\n");
9aa741ea 4518 return;
316f4ca4
MK
4519 }
4520
4521 if (use_json)
4522 json = json_object_new_object();
9aa741ea
MK
4523
4524 zl3vni = zl3vni_lookup(l3vni);
4525 if (!zl3vni) {
316f4ca4
MK
4526 if (use_json)
4527 vty_out(vty, "{}\n");
4528 else
996c9314 4529 vty_out(vty, "%% L3-VNI %u doesnt exist\n", l3vni);
9aa741ea
MK
4530 return;
4531 }
4532
4533 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
4534 if (!zrmac) {
316f4ca4
MK
4535 if (use_json)
4536 vty_out(vty, "{}\n");
4537 else
4538 vty_out(vty,
4539 "%% Requested RMAC doesnt exist in L3-VNI %u",
4540 l3vni);
9aa741ea
MK
4541 return;
4542 }
4543
316f4ca4
MK
4544 zl3vni_print_rmac(zrmac, vty, json);
4545
4546 if (use_json) {
4547 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4548 json, JSON_C_TO_STRING_PRETTY));
4549 json_object_free(json);
4550 }
9aa741ea 4551}
2dbad57f 4552
9f049418 4553void zebra_vxlan_print_rmacs_l3vni(struct vty *vty, vni_t l3vni, bool use_json)
b7cfce93
MK
4554{
4555 zebra_l3vni_t *zl3vni;
d7c0a89a 4556 uint32_t num_rmacs;
b7cfce93
MK
4557 struct rmac_walk_ctx wctx;
4558 json_object *json = NULL;
b7cfce93
MK
4559
4560 if (!is_evpn_enabled())
4561 return;
4562
4563 zl3vni = zl3vni_lookup(l3vni);
4564 if (!zl3vni) {
4565 if (use_json)
4566 vty_out(vty, "{}\n");
4567 else
4568 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
4569 return;
4570 }
4571 num_rmacs = hashcount(zl3vni->rmac_table);
4572 if (!num_rmacs)
4573 return;
4574
75223c9e 4575 if (use_json)
b7cfce93 4576 json = json_object_new_object();
b7cfce93
MK
4577
4578 memset(&wctx, 0, sizeof(struct rmac_walk_ctx));
4579 wctx.vty = vty;
75223c9e 4580 wctx.json = json;
b7cfce93 4581 if (!use_json) {
996c9314 4582 vty_out(vty, "Number of Remote RMACs known for this VNI: %u\n",
b7cfce93 4583 num_rmacs);
4cce389e 4584 vty_out(vty, "%-17s %-21s\n", "MAC", "Remote VTEP");
b7cfce93
MK
4585 } else
4586 json_object_int_add(json, "numRmacs", num_rmacs);
4587
4588 hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
4589
4590 if (use_json) {
b7cfce93
MK
4591 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4592 json, JSON_C_TO_STRING_PRETTY));
4593 json_object_free(json);
4594 }
4595}
4596
9f049418 4597void zebra_vxlan_print_rmacs_all_l3vni(struct vty *vty, bool use_json)
b7cfce93
MK
4598{
4599 struct zebra_ns *zns = NULL;
b7cfce93 4600 json_object *json = NULL;
c0b4eaa4 4601 void *args[2];
b7cfce93
MK
4602
4603 if (!is_evpn_enabled()) {
4604 if (use_json)
4605 vty_out(vty, "{}\n");
4606 return;
4607 }
4608
4609 zns = zebra_ns_lookup(NS_DEFAULT);
c0b4eaa4
MK
4610 if (!zns) {
4611 if (use_json)
4612 vty_out(vty, "{}\n");
b7cfce93 4613 return;
c0b4eaa4 4614 }
b7cfce93
MK
4615
4616 if (use_json)
4617 json = json_object_new_object();
4618
c0b4eaa4
MK
4619 args[0] = vty;
4620 args[1] = json;
4621 hash_iterate(zns->l3vni_table,
4622 (void (*)(struct hash_backet *,
4623 void *))zl3vni_print_rmac_hash_all_vni,
4624 args);
b7cfce93
MK
4625
4626 if (use_json) {
4627 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4628 json, JSON_C_TO_STRING_PRETTY));
4629 json_object_free(json);
4630 }
4631}
4632
996c9314 4633void zebra_vxlan_print_specific_nh_l3vni(struct vty *vty, vni_t l3vni,
9f049418 4634 struct ipaddr *ip, bool use_json)
9aa741ea
MK
4635{
4636 zebra_l3vni_t *zl3vni = NULL;
4637 zebra_neigh_t *n = NULL;
c0e519d3 4638 json_object *json = NULL;
9aa741ea 4639
c0e519d3
MK
4640 if (!is_evpn_enabled()) {
4641 if (use_json)
4642 vty_out(vty, "{}\n");
9aa741ea 4643 return;
c0e519d3
MK
4644 }
4645
4646 if (use_json)
4647 json = json_object_new_object();
9aa741ea
MK
4648
4649 zl3vni = zl3vni_lookup(l3vni);
4650 if (!zl3vni) {
c0e519d3
MK
4651 if (use_json)
4652 vty_out(vty, "{}\n");
4653 else
4654 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
9aa741ea
MK
4655 return;
4656 }
4657
4658 n = zl3vni_nh_lookup(zl3vni, ip);
4659 if (!n) {
c0e519d3
MK
4660 if (use_json)
4661 vty_out(vty, "{}\n");
4662 else
4663 vty_out(vty,
4664 "%% Requested next-hop not present for L3-VNI %u",
4665 l3vni);
9aa741ea
MK
4666 return;
4667 }
4668
c0e519d3
MK
4669 zl3vni_print_nh(n, vty, json);
4670
4671 if (use_json) {
4672 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4673 json, JSON_C_TO_STRING_PRETTY));
4674 json_object_free(json);
4675 }
9aa741ea
MK
4676}
4677
9f049418 4678void zebra_vxlan_print_nh_l3vni(struct vty *vty, vni_t l3vni, bool use_json)
b7cfce93 4679{
d7c0a89a 4680 uint32_t num_nh;
2dbad57f 4681 struct nh_walk_ctx wctx;
b7cfce93 4682 json_object *json = NULL;
b7cfce93
MK
4683 zebra_l3vni_t *zl3vni = NULL;
4684
4685 if (!is_evpn_enabled())
4686 return;
4687
4688 zl3vni = zl3vni_lookup(l3vni);
4689 if (!zl3vni) {
4690 if (use_json)
4691 vty_out(vty, "{}\n");
4692 else
4693 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
4694 return;
4695 }
4696
4697 num_nh = hashcount(zl3vni->nh_table);
4698 if (!num_nh)
4699 return;
4700
9187f600 4701 if (use_json)
b7cfce93 4702 json = json_object_new_object();
b7cfce93 4703
2dbad57f 4704 wctx.vty = vty;
9187f600 4705 wctx.json = json;
b7cfce93 4706 if (!use_json) {
996c9314 4707 vty_out(vty, "Number of NH Neighbors known for this VNI: %u\n",
b7cfce93 4708 num_nh);
4cce389e 4709 vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
b7cfce93 4710 } else
4cce389e 4711 json_object_int_add(json, "numNextHops", num_nh);
b7cfce93
MK
4712
4713 hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
4714
4715 if (use_json) {
b7cfce93
MK
4716 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4717 json, JSON_C_TO_STRING_PRETTY));
4718 json_object_free(json);
4719 }
4720}
4721
9f049418 4722void zebra_vxlan_print_nh_all_l3vni(struct vty *vty, bool use_json)
b7cfce93 4723{
2dbad57f 4724 struct zebra_ns *zns = NULL;
2dbad57f 4725 json_object *json = NULL;
32798965 4726 void *args[2];
2dbad57f 4727
4728 if (!is_evpn_enabled()) {
4729 if (use_json)
4730 vty_out(vty, "{}\n");
4731 return;
4732 }
4733
4734 zns = zebra_ns_lookup(NS_DEFAULT);
4735 if (!zns)
4736 return;
4737
4738 if (use_json)
4739 json = json_object_new_object();
4740
32798965
MK
4741 args[0] = vty;
4742 args[1] = json;
4743 hash_iterate(zns->l3vni_table,
4744 (void (*)(struct hash_backet *,
4745 void *))zl3vni_print_nh_hash_all_vni,
4746 args);
2dbad57f 4747
4748 if (use_json) {
4749 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4750 json, JSON_C_TO_STRING_PRETTY));
4751 json_object_free(json);
4752 }
b7cfce93
MK
4753}
4754
4755/*
4756 * Display L3 VNI information (VTY command handler).
4757 */
9f049418 4758void zebra_vxlan_print_l3vni(struct vty *vty, vni_t vni, bool use_json)
b7cfce93
MK
4759{
4760 void *args[2];
4761 json_object *json = NULL;
4762 zebra_l3vni_t *zl3vni = NULL;
4763
b2ee5a13
MK
4764 if (!is_evpn_enabled()) {
4765 if (use_json)
4766 vty_out(vty, "{}\n");
b7cfce93 4767 return;
b2ee5a13 4768 }
b7cfce93
MK
4769
4770 zl3vni = zl3vni_lookup(vni);
4771 if (!zl3vni) {
4772 if (use_json)
4773 vty_out(vty, "{}\n");
4774 else
4775 vty_out(vty, "%% VNI %u does not exist\n", vni);
4776 return;
4777 }
4778
4779 if (use_json)
4780 json = json_object_new_object();
4781
b2ee5a13
MK
4782 args[0] = vty;
4783 args[1] = json;
b7cfce93
MK
4784 zl3vni_print(zl3vni, (void *)args);
4785
4786 if (use_json) {
4787 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4788 json, JSON_C_TO_STRING_PRETTY));
4789 json_object_free(json);
4790 }
4791}
4792
4cce389e
MK
4793void zebra_vxlan_print_vrf_vni(struct vty *vty, struct zebra_vrf *zvrf,
4794 json_object *json_vrfs)
b7cfce93 4795{
4cce389e
MK
4796 char buf[ETHER_ADDR_STRLEN];
4797 zebra_l3vni_t *zl3vni = NULL;
b7cfce93 4798
4cce389e
MK
4799 zl3vni = zl3vni_lookup(zvrf->l3vni);
4800 if (!zl3vni)
b7cfce93 4801 return;
b7cfce93 4802
4cce389e
MK
4803 if (!json_vrfs) {
4804 vty_out(vty, "%-37s %-10u %-20s %-20s %-5s %-18s\n",
996c9314 4805 zvrf_name(zvrf), zl3vni->vni,
4cce389e 4806 zl3vni_vxlan_if_name(zl3vni),
996c9314 4807 zl3vni_svi_if_name(zl3vni), zl3vni_state2str(zl3vni),
4cce389e 4808 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
b7cfce93 4809 } else {
4cce389e 4810 json_object *json_vrf = NULL;
9df2b997 4811
4cce389e 4812 json_vrf = json_object_new_object();
996c9314 4813 json_object_string_add(json_vrf, "vrf", zvrf_name(zvrf));
4cce389e
MK
4814 json_object_int_add(json_vrf, "vni", zl3vni->vni);
4815 json_object_string_add(json_vrf, "vxlanIntf",
4816 zl3vni_vxlan_if_name(zl3vni));
4817 json_object_string_add(json_vrf, "sviIntf",
4818 zl3vni_svi_if_name(zl3vni));
4819 json_object_string_add(json_vrf, "state",
4820 zl3vni_state2str(zl3vni));
996c9314
LB
4821 json_object_string_add(
4822 json_vrf, "routerMac",
4823 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
4cce389e 4824 json_object_array_add(json_vrfs, json_vrf);
b7cfce93
MK
4825 }
4826}
4827
4828/*
4829 * Display Neighbors for a VNI (VTY command handler).
4830 */
4831void zebra_vxlan_print_neigh_vni(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 4832 vni_t vni, bool use_json)
b7cfce93
MK
4833{
4834 zebra_vni_t *zvni;
d7c0a89a 4835 uint32_t num_neigh;
b7cfce93
MK
4836 struct neigh_walk_ctx wctx;
4837 json_object *json = NULL;
4838
4839 if (!is_evpn_enabled())
4840 return;
4841 zvni = zvni_lookup(vni);
4842 if (!zvni) {
4843 if (use_json)
4844 vty_out(vty, "{}\n");
4845 else
4846 vty_out(vty, "%% VNI %u does not exist\n", vni);
4847 return;
4848 }
4849 num_neigh = hashcount(zvni->neigh_table);
4850 if (!num_neigh)
4851 return;
4852
4853 if (use_json)
4854 json = json_object_new_object();
4855
4856 /* Since we have IPv6 addresses to deal with which can vary widely in
4857 * size, we try to be a bit more elegant in display by first computing
4858 * the maximum width.
4859 */
4860 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
4861 wctx.zvni = zvni;
4862 wctx.vty = vty;
4863 wctx.addr_width = 15;
4864 wctx.json = json;
4865 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
4866
4867 if (!use_json) {
4868 vty_out(vty,
4869 "Number of ARPs (local and remote) known for this VNI: %u\n",
4870 num_neigh);
1a8c5c38 4871 vty_out(vty, "%*s %-6s %-8s %-17s %-21s\n",
4872 -wctx.addr_width, "IP", "Type",
4873 "State", "MAC", "Remote VTEP");
b7cfce93
MK
4874 } else
4875 json_object_int_add(json, "numArpNd", num_neigh);
4876
4877 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
4878 if (use_json) {
4879 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4880 json, JSON_C_TO_STRING_PRETTY));
4881 json_object_free(json);
4882 }
4883}
4884
4885/*
4886 * Display neighbors across all VNIs (VTY command handler).
4887 */
4888void zebra_vxlan_print_neigh_all_vni(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 4889 bool use_json)
b7cfce93
MK
4890{
4891 json_object *json = NULL;
4892 void *args[2];
4893
4894 if (!is_evpn_enabled())
4895 return;
4896
4897 if (use_json)
4898 json = json_object_new_object();
4899
4900 args[0] = vty;
4901 args[1] = json;
4902 hash_iterate(zvrf->vni_table,
4903 (void (*)(struct hash_backet *,
4904 void *))zvni_print_neigh_hash_all_vni,
4905 args);
4906 if (use_json) {
4907 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4908 json, JSON_C_TO_STRING_PRETTY));
4909 json_object_free(json);
4910 }
4911}
4912
4913/*
4914 * Display specific neighbor for a VNI, if present (VTY command handler).
4915 */
4916void zebra_vxlan_print_specific_neigh_vni(struct vty *vty,
4917 struct zebra_vrf *zvrf, vni_t vni,
9f049418 4918 struct ipaddr *ip, bool use_json)
b7cfce93
MK
4919{
4920 zebra_vni_t *zvni;
4921 zebra_neigh_t *n;
4922 json_object *json = NULL;
4923
4924 if (!is_evpn_enabled())
4925 return;
4926 zvni = zvni_lookup(vni);
4927 if (!zvni) {
4928 if (use_json)
cd233079
CS
4929 vty_out(vty, "{}\n");
4930 else
4931 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 4932 return;
4933 }
4934 n = zvni_neigh_lookup(zvni, ip);
4935 if (!n) {
cd233079
CS
4936 if (!use_json)
4937 vty_out(vty,
4938 "%% Requested neighbor does not exist in VNI %u\n",
4939 vni);
d62a17ae 4940 return;
4941 }
cd233079
CS
4942 if (use_json)
4943 json = json_object_new_object();
4944
4945 zvni_print_neigh(n, vty, json);
cec2e17d 4946
cd233079
CS
4947 if (use_json) {
4948 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4949 json, JSON_C_TO_STRING_PRETTY));
4950 json_object_free(json);
4951 }
cec2e17d 4952}
4953
4954/*
4955 * Display neighbors for a VNI from specific VTEP (VTY command handler).
4956 * By definition, these are remote neighbors.
4957 */
d62a17ae 4958void zebra_vxlan_print_neigh_vni_vtep(struct vty *vty, struct zebra_vrf *zvrf,
cd233079 4959 vni_t vni, struct in_addr vtep_ip,
9f049418 4960 bool use_json)
cec2e17d 4961{
d62a17ae 4962 zebra_vni_t *zvni;
d7c0a89a 4963 uint32_t num_neigh;
d62a17ae 4964 struct neigh_walk_ctx wctx;
cd233079 4965 json_object *json = NULL;
cec2e17d 4966
2853fed6 4967 if (!is_evpn_enabled())
d62a17ae 4968 return;
2853fed6 4969 zvni = zvni_lookup(vni);
d62a17ae 4970 if (!zvni) {
cd233079
CS
4971 if (use_json)
4972 vty_out(vty, "{}\n");
4973 else
4974 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 4975 return;
4976 }
4977 num_neigh = hashcount(zvni->neigh_table);
4978 if (!num_neigh)
4979 return;
cec2e17d 4980
d62a17ae 4981 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
4982 wctx.zvni = zvni;
4983 wctx.vty = vty;
68e33151 4984 wctx.addr_width = 15;
d62a17ae 4985 wctx.flags = SHOW_REMOTE_NEIGH_FROM_VTEP;
4986 wctx.r_vtep_ip = vtep_ip;
cd233079 4987 wctx.json = json;
68e33151 4988 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
d62a17ae 4989 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
cd233079
CS
4990
4991 if (use_json) {
4992 vty_out(vty, "%s\n", json_object_to_json_string_ext(
4993 json, JSON_C_TO_STRING_PRETTY));
4994 json_object_free(json);
4995 }
cec2e17d 4996}
4997
4998/*
4999 * Display MACs for a VNI (VTY command handler).
5000 */
d62a17ae 5001void zebra_vxlan_print_macs_vni(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 5002 vni_t vni, bool use_json)
cec2e17d 5003{
d62a17ae 5004 zebra_vni_t *zvni;
d7c0a89a 5005 uint32_t num_macs;
d62a17ae 5006 struct mac_walk_ctx wctx;
cd233079
CS
5007 json_object *json = NULL;
5008 json_object *json_mac = NULL;
cec2e17d 5009
2853fed6 5010 if (!is_evpn_enabled())
d62a17ae 5011 return;
2853fed6 5012 zvni = zvni_lookup(vni);
d62a17ae 5013 if (!zvni) {
cd233079
CS
5014 if (use_json)
5015 vty_out(vty, "{}\n");
5016 else
5017 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 5018 return;
5019 }
790f8dc5 5020 num_macs = num_valid_macs(zvni);
d62a17ae 5021 if (!num_macs)
5022 return;
cec2e17d 5023
cd233079
CS
5024 if (use_json) {
5025 json = json_object_new_object();
5026 json_mac = json_object_new_object();
5027 }
5028
d62a17ae 5029 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
5030 wctx.zvni = zvni;
5031 wctx.vty = vty;
cd233079 5032 wctx.json = json_mac;
cec2e17d 5033
cd233079
CS
5034 if (!use_json) {
5035 vty_out(vty,
5036 "Number of MACs (local and remote) known for this VNI: %u\n",
5037 num_macs);
5038 vty_out(vty, "%-17s %-6s %-21s %-5s\n", "MAC", "Type",
5039 "Intf/Remote VTEP", "VLAN");
5040 } else
5041 json_object_int_add(json, "numMacs", num_macs);
cec2e17d 5042
d62a17ae 5043 hash_iterate(zvni->mac_table, zvni_print_mac_hash, &wctx);
cd233079
CS
5044
5045 if (use_json) {
5046 json_object_object_add(json, "macs", json_mac);
5047 vty_out(vty, "%s\n", json_object_to_json_string_ext(
5048 json, JSON_C_TO_STRING_PRETTY));
5049 json_object_free(json);
5050 }
cec2e17d 5051}
5052
5053/*
5054 * Display MACs for all VNIs (VTY command handler).
5055 */
cd233079 5056void zebra_vxlan_print_macs_all_vni(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 5057 bool use_json)
cec2e17d 5058{
d62a17ae 5059 struct mac_walk_ctx wctx;
cd233079 5060 json_object *json = NULL;
cec2e17d 5061
2853fed6 5062 if (!is_evpn_enabled()) {
cd233079
CS
5063 if (use_json)
5064 vty_out(vty, "{}\n");
d62a17ae 5065 return;
cd233079
CS
5066 }
5067 if (use_json)
5068 json = json_object_new_object();
5069
d62a17ae 5070 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
5071 wctx.vty = vty;
cd233079 5072 wctx.json = json;
d62a17ae 5073 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni, &wctx);
cd233079
CS
5074
5075 if (use_json) {
5076 vty_out(vty, "%s\n", json_object_to_json_string_ext(
5077 json, JSON_C_TO_STRING_PRETTY));
5078 json_object_free(json);
5079 }
cec2e17d 5080}
5081
5082/*
5083 * Display MACs for all VNIs (VTY command handler).
5084 */
d62a17ae 5085void zebra_vxlan_print_macs_all_vni_vtep(struct vty *vty,
5086 struct zebra_vrf *zvrf,
9f049418 5087 struct in_addr vtep_ip, bool use_json)
cec2e17d 5088{
d62a17ae 5089 struct mac_walk_ctx wctx;
cd233079 5090 json_object *json = NULL;
cec2e17d 5091
2853fed6 5092 if (!is_evpn_enabled())
d62a17ae 5093 return;
cd233079
CS
5094
5095 if (use_json)
5096 json = json_object_new_object();
5097
d62a17ae 5098 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
5099 wctx.vty = vty;
5100 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
5101 wctx.r_vtep_ip = vtep_ip;
cd233079 5102 wctx.json = json;
d62a17ae 5103 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni, &wctx);
cd233079
CS
5104
5105 if (use_json) {
5106 vty_out(vty, "%s\n", json_object_to_json_string_ext(
5107 json, JSON_C_TO_STRING_PRETTY));
5108 json_object_free(json);
5109 }
cec2e17d 5110}
5111
5112/*
5113 * Display specific MAC for a VNI, if present (VTY command handler).
5114 */
d62a17ae 5115void zebra_vxlan_print_specific_mac_vni(struct vty *vty, struct zebra_vrf *zvrf,
5116 vni_t vni, struct ethaddr *macaddr)
cec2e17d 5117{
d62a17ae 5118 zebra_vni_t *zvni;
5119 zebra_mac_t *mac;
cec2e17d 5120
2853fed6 5121 if (!is_evpn_enabled())
d62a17ae 5122 return;
2853fed6 5123 zvni = zvni_lookup(vni);
d62a17ae 5124 if (!zvni) {
5125 vty_out(vty, "%% VNI %u does not exist\n", vni);
5126 return;
5127 }
5128 mac = zvni_mac_lookup(zvni, macaddr);
5129 if (!mac) {
5130 vty_out(vty, "%% Requested MAC does not exist in VNI %u\n",
5131 vni);
5132 return;
5133 }
cec2e17d 5134
d62a17ae 5135 zvni_print_mac(mac, vty);
cec2e17d 5136}
5137
5138/*
5139 * Display MACs for a VNI from specific VTEP (VTY command handler).
5140 */
d62a17ae 5141void zebra_vxlan_print_macs_vni_vtep(struct vty *vty, struct zebra_vrf *zvrf,
cd233079 5142 vni_t vni, struct in_addr vtep_ip,
9f049418 5143 bool use_json)
cec2e17d 5144{
d62a17ae 5145 zebra_vni_t *zvni;
d7c0a89a 5146 uint32_t num_macs;
d62a17ae 5147 struct mac_walk_ctx wctx;
cd233079
CS
5148 json_object *json = NULL;
5149 json_object *json_mac = NULL;
cec2e17d 5150
2853fed6 5151 if (!is_evpn_enabled())
d62a17ae 5152 return;
2853fed6 5153 zvni = zvni_lookup(vni);
d62a17ae 5154 if (!zvni) {
cd233079
CS
5155 if (use_json)
5156 vty_out(vty, "{}\n");
5157 else
5158 vty_out(vty, "%% VNI %u does not exist\n", vni);
d62a17ae 5159 return;
5160 }
790f8dc5 5161 num_macs = num_valid_macs(zvni);
d62a17ae 5162 if (!num_macs)
5163 return;
cd233079
CS
5164
5165 if (use_json) {
5166 json = json_object_new_object();
5167 json_mac = json_object_new_object();
5168 }
5169
d62a17ae 5170 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
5171 wctx.zvni = zvni;
5172 wctx.vty = vty;
5173 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
5174 wctx.r_vtep_ip = vtep_ip;
cd233079 5175 wctx.json = json_mac;
d62a17ae 5176 hash_iterate(zvni->mac_table, zvni_print_mac_hash, &wctx);
cd233079
CS
5177
5178 if (use_json) {
5179 json_object_int_add(json, "numMacs", wctx.count);
5180 if (wctx.count)
5181 json_object_object_add(json, "macs", json_mac);
5182 vty_out(vty, "%s\n", json_object_to_json_string_ext(
5183 json, JSON_C_TO_STRING_PRETTY));
5184 json_object_free(json);
5185 }
cec2e17d 5186}
5187
5188
5189/*
5190 * Display VNI information (VTY command handler).
5191 */
cd233079 5192void zebra_vxlan_print_vni(struct vty *vty, struct zebra_vrf *zvrf, vni_t vni,
9f049418 5193 bool use_json)
cec2e17d 5194{
cd233079
CS
5195 json_object *json = NULL;
5196 void *args[2];
1f2129ec 5197 zebra_l3vni_t *zl3vni = NULL;
5198 zebra_vni_t *zvni = NULL;
cec2e17d 5199
2853fed6 5200 if (!is_evpn_enabled())
d62a17ae 5201 return;
4cce389e 5202
cd233079
CS
5203 if (use_json)
5204 json = json_object_new_object();
5205 args[0] = vty;
5206 args[1] = json;
4cce389e 5207
1f2129ec 5208 zl3vni = zl3vni_lookup(vni);
5209 if (zl3vni) {
4cce389e
MK
5210 zl3vni_print(zl3vni, (void *)args);
5211 } else {
4cce389e
MK
5212 zvni = zvni_lookup(vni);
5213 if (!zvni) {
5214 if (use_json)
5215 vty_out(vty, "{}\n");
5216 else
5217 vty_out(vty, "%% VNI %u does not exist\n", vni);
5218 return;
5219 }
5220
5221 zvni_print(zvni, (void *)args);
5222 }
5223
cd233079
CS
5224 if (use_json) {
5225 vty_out(vty, "%s\n", json_object_to_json_string_ext(
5226 json, JSON_C_TO_STRING_PRETTY));
5227 json_object_free(json);
5228 }
cec2e17d 5229}
5230
4cce389e 5231/* Display all global details for EVPN */
088f1098 5232void zebra_vxlan_print_evpn(struct vty *vty, bool uj)
cec2e17d 5233{
4cce389e
MK
5234 int num_l2vnis = 0;
5235 int num_l3vnis = 0;
d4454626 5236 int num_vnis = 0;
cd233079 5237 json_object *json = NULL;
4cce389e
MK
5238 struct zebra_ns *zns = NULL;
5239 struct zebra_vrf *zvrf = NULL;
cec2e17d 5240
2853fed6 5241 if (!is_evpn_enabled())
d62a17ae 5242 return;
4cce389e
MK
5243
5244 zns = zebra_ns_lookup(NS_DEFAULT);
5245 if (!zns)
d62a17ae 5246 return;
4cce389e
MK
5247
5248 zvrf = vrf_info_lookup(VRF_DEFAULT);
5249 if (!zvrf)
5250 return;
5251
5252 num_l3vnis = hashcount(zns->l3vni_table);
5253 num_l2vnis = hashcount(zvrf->vni_table);
d4454626 5254 num_vnis = num_l2vnis + num_l3vnis;
4cce389e
MK
5255
5256 if (uj) {
cd233079 5257 json = json_object_new_object();
ddd16ed5
MK
5258 json_object_string_add(json, "advertiseGatewayMacip",
5259 zvrf->advertise_gw_macip ? "Yes" : "No");
d4454626 5260 json_object_int_add(json, "numVnis", num_vnis);
4cce389e
MK
5261 json_object_int_add(json, "numL2Vnis", num_l2vnis);
5262 json_object_int_add(json, "numL3Vnis", num_l3vnis);
cd233079 5263 } else {
4cce389e
MK
5264 vty_out(vty, "L2 VNIs: %u\n", num_l2vnis);
5265 vty_out(vty, "L3 VNIs: %u\n", num_l3vnis);
ddd16ed5
MK
5266 vty_out(vty, "Advertise gateway mac-ip: %s\n",
5267 zvrf->advertise_gw_macip ? "Yes" : "No");
cd233079 5268 }
4cce389e
MK
5269
5270 if (uj) {
5271 vty_out(vty, "%s\n", json_object_to_json_string_ext(
5272 json, JSON_C_TO_STRING_PRETTY));
5273 json_object_free(json);
5274 }
5275}
5276
5277/*
5278 * Display VNI hash table (VTY command handler).
5279 */
5280void zebra_vxlan_print_vnis(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 5281 bool use_json)
4cce389e
MK
5282{
5283 json_object *json = NULL;
5284 struct zebra_ns *zns = NULL;
5285 void *args[2];
5286
5287 if (!is_evpn_enabled())
5288 return;
5289
5290 zns = zebra_ns_lookup(NS_DEFAULT);
5291 if (!zns)
5292 return;
5293
5294
5295 if (use_json)
5296 json = json_object_new_object();
5297 else
996c9314
LB
5298 vty_out(vty, "%-10s %-4s %-21s %-8s %-8s %-15s %-37s\n", "VNI",
5299 "Type", "VxLAN IF", "# MACs", "# ARPs",
5300 "# Remote VTEPs", "Tenant VRF");
4cce389e 5301
cd233079
CS
5302 args[0] = vty;
5303 args[1] = json;
5304
4cce389e 5305 /* Display all L2-VNIs */
cd233079
CS
5306 hash_iterate(zvrf->vni_table,
5307 (void (*)(struct hash_backet *, void *))zvni_print_hash,
5308 args);
5309
4cce389e
MK
5310 /* Display all L3-VNIs */
5311 hash_iterate(zns->l3vni_table,
5312 (void (*)(struct hash_backet *, void *))zl3vni_print_hash,
5313 args);
5314
cd233079
CS
5315 if (use_json) {
5316 vty_out(vty, "%s\n", json_object_to_json_string_ext(
5317 json, JSON_C_TO_STRING_PRETTY));
5318 json_object_free(json);
5319 }
cec2e17d 5320}
5321
2232a77c 5322/*
ee69da27
MK
5323 * Handle neighbor delete notification from the kernel (on a VLAN device
5324 * / L3 interface). This may result in either the neighbor getting deleted
5325 * from our database or being re-added to the kernel (if it is a valid
2232a77c 5326 * remote neighbor).
5327 */
ee69da27
MK
5328int zebra_vxlan_handle_kernel_neigh_del(struct interface *ifp,
5329 struct interface *link_if,
5330 struct ipaddr *ip)
d62a17ae 5331{
d62a17ae 5332 char buf[INET6_ADDRSTRLEN];
b6938a74 5333 char buf2[ETHER_ADDR_STRLEN];
b7cfce93
MK
5334 zebra_neigh_t *n = NULL;
5335 zebra_vni_t *zvni = NULL;
5336 zebra_mac_t *zmac = NULL;
8c9b80b9 5337 zebra_l3vni_t *zl3vni = NULL;
b7cfce93 5338
8c9b80b9 5339 /* check if this is a remote neigh entry corresponding to remote
523cafc4 5340 * next-hop
5341 */
8c9b80b9
MK
5342 zl3vni = zl3vni_from_svi(ifp, link_if);
5343 if (zl3vni)
5344 return zl3vni_local_nh_del(zl3vni, ip);
d62a17ae 5345
5346 /* We are only interested in neighbors on an SVI that resides on top
5347 * of a VxLAN bridge.
5348 */
b7cfce93 5349 zvni = zvni_from_svi(ifp, link_if);
d62a17ae 5350 if (!zvni)
5351 return 0;
8c9b80b9 5352
d62a17ae 5353 if (!zvni->vxlan_if) {
9df414fe 5354 zlog_debug(
d62a17ae 5355 "VNI %u hash %p doesn't have intf upon local neighbor DEL",
5356 zvni->vni, zvni);
5357 return -1;
5358 }
5359
5360 if (IS_ZEBRA_DEBUG_VXLAN)
8c9b80b9 5361 zlog_debug("Del neighbor %s intf %s(%u) -> L2-VNI %u",
996c9314
LB
5362 ipaddr2str(ip, buf, sizeof(buf)), ifp->name,
5363 ifp->ifindex, zvni->vni);
d62a17ae 5364
5365 /* If entry doesn't exist, nothing to do. */
5366 n = zvni_neigh_lookup(zvni, ip);
5367 if (!n)
5368 return 0;
5369
b6938a74
MK
5370 zmac = zvni_mac_lookup(zvni, &n->emac);
5371 if (!zmac) {
5372 if (IS_ZEBRA_DEBUG_VXLAN)
9df414fe 5373 zlog_debug(
2853fed6 5374 "Trying to del a neigh %s without a mac %s on VNI %u",
5375 ipaddr2str(ip, buf, sizeof(buf)),
b6938a74
MK
5376 prefix_mac2str(&n->emac, buf2, sizeof(buf2)),
5377 zvni->vni);
5378
5379 return 0;
5380 }
5381
d62a17ae 5382 /* If it is a remote entry, the kernel has aged this out or someone has
5383 * deleted it, it needs to be re-installed as Quagga is the owner.
5384 */
5385 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
5386 zvni_neigh_install(zvni, n);
5387 return 0;
5388 }
5389
d62a17ae 5390 /* Remove neighbor from BGP. */
b6938a74 5391 if (IS_ZEBRA_NEIGH_ACTIVE(n))
996c9314 5392 zvni_neigh_send_del_to_client(zvni->vni, &n->ip, &n->emac, 0);
d62a17ae 5393
5394 /* Delete this neighbor entry. */
5395 zvni_neigh_del(zvni, n);
5396
b6938a74
MK
5397 /* see if the AUTO mac needs to be deleted */
5398 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_AUTO)
f51d8a27 5399 && !listcount(zmac->neigh_list))
b6938a74
MK
5400 zvni_mac_del(zvni, zmac);
5401
d62a17ae 5402 return 0;
2232a77c 5403}
5404
5405/*
ee69da27
MK
5406 * Handle neighbor add or update notification from the kernel (on a VLAN
5407 * device / L3 interface). This is typically for a local neighbor but can
5408 * also be for a remote neighbor (e.g., ageout notification). It could
5409 * also be a "move" scenario.
2232a77c 5410 */
ee69da27
MK
5411int zebra_vxlan_handle_kernel_neigh_update(struct interface *ifp,
5412 struct interface *link_if,
5413 struct ipaddr *ip,
5414 struct ethaddr *macaddr,
5415 uint16_t state,
a37f4598 5416 bool is_ext,
5417 bool is_router)
d62a17ae 5418{
d62a17ae 5419 char buf[ETHER_ADDR_STRLEN];
5420 char buf2[INET6_ADDRSTRLEN];
b7cfce93 5421 zebra_vni_t *zvni = NULL;
3bcbba10 5422 zebra_l3vni_t *zl3vni = NULL;
5423
5424 /* check if this is a remote neigh entry corresponding to remote
5425 * next-hop
5426 */
5427 zl3vni = zl3vni_from_svi(ifp, link_if);
5428 if (zl3vni)
5429 return zl3vni_local_nh_add_update(zl3vni, ip, state);
b7cfce93 5430
d62a17ae 5431 /* We are only interested in neighbors on an SVI that resides on top
5432 * of a VxLAN bridge.
5433 */
b7cfce93 5434 zvni = zvni_from_svi(ifp, link_if);
d62a17ae 5435 if (!zvni)
5436 return 0;
5437
d62a17ae 5438 if (IS_ZEBRA_DEBUG_VXLAN)
5439 zlog_debug(
54c17425 5440 "Add/Update neighbor %s MAC %s intf %s(%u) state 0x%x %s %s-> L2-VNI %u",
2853fed6 5441 ipaddr2str(ip, buf2, sizeof(buf2)),
d62a17ae 5442 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
a37f4598 5443 ifp->ifindex, state, is_ext ? "ext-learned " : "",
5444 is_router ? "router " : "",
8c9b80b9 5445 zvni->vni);
d62a17ae 5446
ee69da27 5447 /* Is this about a local neighbor or a remote one? */
a37f4598 5448 if (!is_ext)
68e33151 5449 return zvni_local_neigh_update(zvni, ifp, ip, macaddr,
a37f4598 5450 is_router);
b7cfce93 5451
ee69da27 5452 return zvni_remote_neigh_update(zvni, ifp, ip, macaddr, state);
2232a77c 5453}
5454
b682f6de 5455
2232a77c 5456/*
5457 * Handle message from client to delete a remote MACIP for a VNI.
5458 */
89f4e507 5459void zebra_vxlan_remote_macip_del(ZAPI_HANDLER_ARGS)
d62a17ae 5460{
5461 struct stream *s;
5462 vni_t vni;
5463 struct ethaddr macaddr;
5464 struct ipaddr ip;
5465 struct in_addr vtep_ip;
9df2b997 5466 uint16_t l = 0, ipa_len;
d62a17ae 5467 char buf[ETHER_ADDR_STRLEN];
5468 char buf1[INET6_ADDRSTRLEN];
5469
b7cfce93 5470 memset(&macaddr, 0, sizeof(struct ethaddr));
b7cfce93
MK
5471 memset(&ip, 0, sizeof(struct ipaddr));
5472 memset(&vtep_ip, 0, sizeof(struct in_addr));
5473
1002497a 5474 s = msg;
d62a17ae 5475
89f4e507 5476 while (l < hdr->length) {
d62a17ae 5477 /* Obtain each remote MACIP and process. */
5478 /* Message contains VNI, followed by MAC followed by IP (if any)
5479 * followed by remote VTEP IP.
5480 */
d62a17ae 5481 memset(&ip, 0, sizeof(ip));
ec93aa12
DS
5482 STREAM_GETL(s, vni);
5483 STREAM_GET(&macaddr.octet, s, ETH_ALEN);
5484 STREAM_GETL(s, ipa_len);
d62a17ae 5485 if (ipa_len) {
5486 ip.ipa_type = (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4
5487 : IPADDR_V6;
ec93aa12 5488 STREAM_GET(&ip.ip.addr, s, ipa_len);
d62a17ae 5489 }
ff8b7eb8 5490 l += 4 + ETH_ALEN + 4 + ipa_len;
ec93aa12 5491 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 5492 l += IPV4_MAX_BYTELEN;
5493
5494 if (IS_ZEBRA_DEBUG_VXLAN)
5495 zlog_debug(
f07e1c99 5496 "Recv MACIP DEL VNI %u MAC %s%s%s Remote VTEP %s from %s",
5497 vni,
d62a17ae 5498 prefix_mac2str(&macaddr, buf, sizeof(buf)),
f07e1c99 5499 ipa_len ? " IP " : "",
5500 ipa_len ?
5501 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
d62a17ae 5502 inet_ntoa(vtep_ip),
5503 zebra_route_string(client->proto));
5504
f07e1c99 5505 process_remote_macip_del(vni, &macaddr, ipa_len, &ip, vtep_ip);
d62a17ae 5506 }
5507
ec93aa12 5508stream_failure:
8068a649 5509 return;
2232a77c 5510}
5511
5512/*
5513 * Handle message from client to add a remote MACIP for a VNI. This
5514 * could be just the add of a MAC address or the add of a neighbor
5515 * (IP+MAC).
5516 */
89f4e507 5517void zebra_vxlan_remote_macip_add(ZAPI_HANDLER_ARGS)
d62a17ae 5518{
5519 struct stream *s;
5520 vni_t vni;
5521 struct ethaddr macaddr;
5522 struct ipaddr ip;
5523 struct in_addr vtep_ip;
9df2b997 5524 uint16_t l = 0, ipa_len;
f07e1c99 5525 uint8_t flags = 0;
5526 uint32_t seq;
d62a17ae 5527 char buf[ETHER_ADDR_STRLEN];
5528 char buf1[INET6_ADDRSTRLEN];
d62a17ae 5529
b7cfce93 5530 memset(&macaddr, 0, sizeof(struct ethaddr));
b7cfce93
MK
5531 memset(&ip, 0, sizeof(struct ipaddr));
5532 memset(&vtep_ip, 0, sizeof(struct in_addr));
5533
ec93aa12 5534 if (!EVPN_ENABLED(zvrf)) {
9df414fe 5535 zlog_debug("EVPN not enabled, ignoring remote MACIP ADD");
8068a649 5536 return;
ec93aa12 5537 }
d62a17ae 5538
1002497a 5539 s = msg;
d62a17ae 5540
89f4e507 5541 while (l < hdr->length) {
d62a17ae 5542 /* Obtain each remote MACIP and process. */
5543 /* Message contains VNI, followed by MAC followed by IP (if any)
5544 * followed by remote VTEP IP.
5545 */
d62a17ae 5546 memset(&ip, 0, sizeof(ip));
ec93aa12
DS
5547 STREAM_GETL(s, vni);
5548 STREAM_GET(&macaddr.octet, s, ETH_ALEN);
5549 STREAM_GETL(s, ipa_len);
d62a17ae 5550 if (ipa_len) {
5551 ip.ipa_type = (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4
5552 : IPADDR_V6;
ec93aa12 5553 STREAM_GET(&ip.ip.addr, s, ipa_len);
d62a17ae 5554 }
ff8b7eb8 5555 l += 4 + ETH_ALEN + 4 + ipa_len;
ec93aa12 5556 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 5557 l += IPV4_MAX_BYTELEN;
5558
ead40654 5559 /* Get flags - sticky mac and/or gateway mac */
2017b3ea 5560 STREAM_GETC(s, flags);
d62a17ae 5561 l++;
f07e1c99 5562 STREAM_GETL(s, seq);
5563 l += 4;
d62a17ae 5564
5565 if (IS_ZEBRA_DEBUG_VXLAN)
5566 zlog_debug(
f07e1c99 5567 "Recv MACIP ADD VNI %u MAC %s%s%s flags 0x%x seq %u VTEP %s from %s",
5568 vni,
d62a17ae 5569 prefix_mac2str(&macaddr, buf, sizeof(buf)),
f07e1c99 5570 ipa_len ? " IP " : "",
5571 ipa_len ?
5572 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
5573 flags, seq, inet_ntoa(vtep_ip),
d62a17ae 5574 zebra_route_string(client->proto));
5575
f07e1c99 5576 process_remote_macip_add(vni, &macaddr, ipa_len, &ip,
5577 flags, seq, vtep_ip);
d62a17ae 5578 }
5579
ec93aa12 5580stream_failure:
8068a649 5581 return;
13d60d35 5582}
5583
5584/*
2232a77c 5585 * Handle notification of MAC add/update over VxLAN. If the kernel is notifying
5586 * us, this must involve a multihoming scenario. Treat this as implicit delete
5587 * of any prior local MAC.
13d60d35 5588 */
d62a17ae 5589int zebra_vxlan_check_del_local_mac(struct interface *ifp,
5590 struct interface *br_if,
5591 struct ethaddr *macaddr, vlanid_t vid)
13d60d35 5592{
d62a17ae 5593 struct zebra_if *zif;
d62a17ae 5594 struct zebra_l2info_vxlan *vxl;
5595 vni_t vni;
5596 zebra_vni_t *zvni;
5597 zebra_mac_t *mac;
5598 char buf[ETHER_ADDR_STRLEN];
13d60d35 5599
d62a17ae 5600 zif = ifp->info;
5601 assert(zif);
5602 vxl = &zif->l2info.vxl;
5603 vni = vxl->vni;
13d60d35 5604
2853fed6 5605 /* Check if EVPN is enabled. */
5606 if (!is_evpn_enabled())
d62a17ae 5607 return 0;
13d60d35 5608
d62a17ae 5609 /* Locate hash entry; it is expected to exist. */
2853fed6 5610 zvni = zvni_lookup(vni);
d62a17ae 5611 if (!zvni)
5612 return 0;
13d60d35 5613
d62a17ae 5614 /* If entry doesn't exist, nothing to do. */
5615 mac = zvni_mac_lookup(zvni, macaddr);
5616 if (!mac)
5617 return 0;
13d60d35 5618
d62a17ae 5619 /* Is it a local entry? */
5620 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
5621 return 0;
13d60d35 5622
d62a17ae 5623 if (IS_ZEBRA_DEBUG_VXLAN)
5624 zlog_debug(
2853fed6 5625 "Add/update remote MAC %s intf %s(%u) VNI %u - del local",
996c9314
LB
5626 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
5627 ifp->ifindex, vni);
13d60d35 5628
d62a17ae 5629 /* Remove MAC from BGP. */
ead40654 5630 zvni_mac_send_del_to_client(zvni->vni, macaddr, mac->flags);
13d60d35 5631
b6938a74
MK
5632 /*
5633 * If there are no neigh associated with the mac delete the mac
5634 * else mark it as AUTO for forward reference
5635 */
5636 if (!listcount(mac->neigh_list)) {
5637 zvni_mac_del(zvni, mac);
5638 } else {
5639 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
5640 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5641 }
13d60d35 5642
d62a17ae 5643 return 0;
13d60d35 5644}
5645
5646/*
2232a77c 5647 * Handle remote MAC delete by kernel; readd the remote MAC if we have it.
5648 * This can happen because the remote MAC entries are also added as "dynamic",
5649 * so the kernel can ageout the entry.
13d60d35 5650 */
d62a17ae 5651int zebra_vxlan_check_readd_remote_mac(struct interface *ifp,
5652 struct interface *br_if,
5653 struct ethaddr *macaddr, vlanid_t vid)
13d60d35 5654{
a9a76262
MK
5655 struct zebra_if *zif = NULL;
5656 struct zebra_l2info_vxlan *vxl = NULL;
d62a17ae 5657 vni_t vni;
a9a76262
MK
5658 zebra_vni_t *zvni = NULL;
5659 zebra_l3vni_t *zl3vni = NULL;
5660 zebra_mac_t *mac = NULL;
d62a17ae 5661 char buf[ETHER_ADDR_STRLEN];
2232a77c 5662
d62a17ae 5663 zif = ifp->info;
5664 assert(zif);
5665 vxl = &zif->l2info.vxl;
5666 vni = vxl->vni;
2232a77c 5667
2853fed6 5668 /* Check if EVPN is enabled. */
5669 if (!is_evpn_enabled())
d62a17ae 5670 return 0;
2232a77c 5671
a9a76262
MK
5672 /* check if this is a remote RMAC and readd simillar to remote macs */
5673 zl3vni = zl3vni_lookup(vni);
5674 if (zl3vni)
5675 return zebra_vxlan_readd_remote_rmac(zl3vni, macaddr);
5676
d62a17ae 5677 /* Locate hash entry; it is expected to exist. */
2853fed6 5678 zvni = zvni_lookup(vni);
d62a17ae 5679 if (!zvni)
5680 return 0;
13d60d35 5681
d62a17ae 5682 /* If entry doesn't exist, nothing to do. */
5683 mac = zvni_mac_lookup(zvni, macaddr);
5684 if (!mac)
5685 return 0;
2232a77c 5686
d62a17ae 5687 /* Is it a remote entry? */
5688 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
5689 return 0;
2232a77c 5690
d62a17ae 5691 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 5692 zlog_debug("Del remote MAC %s intf %s(%u) VNI %u - readd",
d62a17ae 5693 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
5694 ifp->ifindex, vni);
13d60d35 5695
d62a17ae 5696 zvni_mac_install(zvni, mac);
5697 return 0;
13d60d35 5698}
5699
5700/*
2232a77c 5701 * Handle local MAC delete (on a port or VLAN corresponding to this VNI).
13d60d35 5702 */
d62a17ae 5703int zebra_vxlan_local_mac_del(struct interface *ifp, struct interface *br_if,
5704 struct ethaddr *macaddr, vlanid_t vid)
13d60d35 5705{
d62a17ae 5706 zebra_vni_t *zvni;
5707 zebra_mac_t *mac;
d62a17ae 5708 char buf[ETHER_ADDR_STRLEN];
13d60d35 5709
d62a17ae 5710 /* We are interested in MACs only on ports or (port, VLAN) that
5711 * map to a VNI.
5712 */
5713 zvni = zvni_map_vlan(ifp, br_if, vid);
5714 if (!zvni)
5715 return 0;
5716 if (!zvni->vxlan_if) {
9df414fe
QY
5717 zlog_debug(
5718 "VNI %u hash %p doesn't have intf upon local MAC DEL",
5719 zvni->vni, zvni);
d62a17ae 5720 return -1;
5721 }
13d60d35 5722
d62a17ae 5723 if (IS_ZEBRA_DEBUG_VXLAN)
f07e1c99 5724 zlog_debug("DEL MAC %s intf %s(%u) VID %u -> VNI %u",
d62a17ae 5725 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
5726 ifp->ifindex, vid, zvni->vni);
2232a77c 5727
d62a17ae 5728 /* If entry doesn't exist, nothing to do. */
5729 mac = zvni_mac_lookup(zvni, macaddr);
5730 if (!mac)
5731 return 0;
2232a77c 5732
d62a17ae 5733 /* Is it a local entry? */
5734 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
5735 return 0;
2232a77c 5736
b6938a74 5737 /* Update all the neigh entries associated with this mac */
2853fed6 5738 zvni_process_neigh_on_local_mac_del(zvni, mac);
b6938a74 5739
f07e1c99 5740 /* Remove MAC from BGP. */
5741 zvni_mac_send_del_to_client(zvni->vni, macaddr, mac->flags);
5742
b6938a74
MK
5743 /*
5744 * If there are no neigh associated with the mac delete the mac
5745 * else mark it as AUTO for forward reference
5746 */
5747 if (!listcount(mac->neigh_list)) {
5748 zvni_mac_del(zvni, mac);
5749 } else {
5750 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
5751 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5752 }
2232a77c 5753
d62a17ae 5754 return 0;
13d60d35 5755}
5756
5757/*
2232a77c 5758 * Handle local MAC add (on a port or VLAN corresponding to this VNI).
13d60d35 5759 */
d62a17ae 5760int zebra_vxlan_local_mac_add_update(struct interface *ifp,
5761 struct interface *br_if,
5762 struct ethaddr *macaddr, vlanid_t vid,
a37f4598 5763 bool sticky)
d62a17ae 5764{
5765 zebra_vni_t *zvni;
5766 zebra_mac_t *mac;
d62a17ae 5767 char buf[ETHER_ADDR_STRLEN];
f07e1c99 5768 bool mac_sticky = false;
5769 bool inform_client = false;
5770 bool upd_neigh = false;
d62a17ae 5771
5772 /* We are interested in MACs only on ports or (port, VLAN) that
5773 * map to a VNI.
5774 */
5775 zvni = zvni_map_vlan(ifp, br_if, vid);
5776 if (!zvni) {
5777 if (IS_ZEBRA_DEBUG_VXLAN)
5778 zlog_debug(
2853fed6 5779 "Add/Update %sMAC %s intf %s(%u) VID %u, could not find VNI",
5780 sticky ? "sticky " : "",
d62a17ae 5781 prefix_mac2str(macaddr, buf, sizeof(buf)),
5782 ifp->name, ifp->ifindex, vid);
5783 return 0;
5784 }
5785
5786 if (!zvni->vxlan_if) {
9df414fe
QY
5787 zlog_debug(
5788 "VNI %u hash %p doesn't have intf upon local MAC ADD",
5789 zvni->vni, zvni);
d62a17ae 5790 return -1;
5791 }
5792
f07e1c99 5793 /* Check if we need to create or update or it is a NO-OP. */
d62a17ae 5794 mac = zvni_mac_lookup(zvni, macaddr);
f07e1c99 5795 if (!mac) {
5796 if (IS_ZEBRA_DEBUG_VXLAN)
5797 zlog_debug(
5798 "ADD %sMAC %s intf %s(%u) VID %u -> VNI %u",
5799 sticky ? "sticky " : "",
5800 prefix_mac2str(macaddr, buf, sizeof(buf)),
5801 ifp->name, ifp->ifindex, vid, zvni->vni);
d62a17ae 5802
f07e1c99 5803 mac = zvni_mac_add(zvni, macaddr);
5804 if (!mac) {
5805 flog_err(
e914ccbe 5806 EC_ZEBRA_MAC_ADD_FAILED,
f07e1c99 5807 "Failed to add MAC %s intf %s(%u) VID %u VNI %u",
5808 prefix_mac2str(macaddr, buf, sizeof(buf)),
5809 ifp->name, ifp->ifindex, vid, zvni->vni);
5810 return -1;
5811 }
5812 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
5813 mac->fwd_info.local.ifindex = ifp->ifindex;
5814 mac->fwd_info.local.vid = vid;
5815 if (sticky)
5816 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
5817 inform_client = true;
5818
5819 } else {
5820 if (IS_ZEBRA_DEBUG_VXLAN)
5821 zlog_debug(
5822 "UPD %sMAC %s intf %s(%u) VID %u -> VNI %u curFlags 0x%x",
5823 sticky ? "sticky " : "",
5824 prefix_mac2str(macaddr, buf, sizeof(buf)),
5825 ifp->name, ifp->ifindex, vid, zvni->vni,
5826 mac->flags);
5827
5828 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
5829 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
5830 mac_sticky = true;
b6938a74 5831
b8ce75a5 5832 /*
f07e1c99 5833 * Update any changes and if changes are relevant to
5834 * BGP, note it.
b8ce75a5 5835 */
d62a17ae 5836 if (mac_sticky == sticky
5837 && mac->fwd_info.local.ifindex == ifp->ifindex
5838 && mac->fwd_info.local.vid == vid) {
5839 if (IS_ZEBRA_DEBUG_VXLAN)
5840 zlog_debug(
2853fed6 5841 "Add/Update %sMAC %s intf %s(%u) VID %u -> VNI %u, "
d62a17ae 5842 "entry exists and has not changed ",
d62a17ae 5843 sticky ? "sticky " : "",
5844 prefix_mac2str(macaddr, buf,
5845 sizeof(buf)),
5846 ifp->name, ifp->ifindex, vid,
5847 zvni->vni);
5848 return 0;
b6938a74 5849 }
f07e1c99 5850 if (mac_sticky != sticky) {
5851 if (sticky)
5852 SET_FLAG(mac->flags,
5853 ZEBRA_MAC_STICKY);
5854 else
5855 UNSET_FLAG(mac->flags,
5856 ZEBRA_MAC_STICKY);
5857 inform_client = true;
5858 }
5859
5860 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
5861 mac->fwd_info.local.ifindex = ifp->ifindex;
5862 mac->fwd_info.local.vid = vid;
5863
5864 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) ||
5865 CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
5866
421bb26a 5867 /*
f07e1c99 5868 * MAC has either moved or was "internally" created due
5869 * to a neighbor learn and is now actually learnt. If
5870 * it was learnt as a remote sticky MAC, this is an
5871 * operator error.
421bb26a 5872 */
8f4b98ee 5873 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)) {
9df414fe 5874 flog_warn(
e914ccbe 5875 EC_ZEBRA_STICKY_MAC_ALREADY_LEARNT,
9df414fe 5876 "MAC %s already learnt as remote sticky MAC behind VTEP %s VNI %u",
421bb26a
MK
5877 prefix_mac2str(macaddr, buf,
5878 sizeof(buf)),
5879 inet_ntoa(mac->fwd_info.r_vtep_ip),
5880 zvni->vni);
8f4b98ee
MK
5881 return 0;
5882 }
d62a17ae 5883
f07e1c99 5884 /* If an actual move, compute MAC's seq number */
5885 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
5886 mac->loc_seq = MAX(mac->rem_seq + 1,
5887 mac->loc_seq);
5888 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
5889 UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5890 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
5891 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
5892 mac->fwd_info.local.ifindex = ifp->ifindex;
5893 mac->fwd_info.local.vid = vid;
5894 if (sticky)
5895 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
5896 else
5897 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
5898 /*
5899 * We have to inform BGP of this MAC as well as process
5900 * all neighbors.
5901 */
5902 inform_client = true;
5903 upd_neigh = true;
d62a17ae 5904 }
5905 }
5906
d62a17ae 5907 /* Inform BGP if required. */
f07e1c99 5908 if (inform_client) {
5909 if (zvni_mac_send_add_to_client(zvni->vni, macaddr,
5910 mac->flags, mac->loc_seq))
5911 return -1;
b6938a74 5912 }
d62a17ae 5913
f07e1c99 5914 /* Process all neighbors associated with this MAC, if required. */
5915 if (upd_neigh)
5916 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
5917
d62a17ae 5918 return 0;
2232a77c 5919}
13d60d35 5920
5921/*
5922 * Handle message from client to delete a remote VTEP for a VNI.
5923 */
89f4e507 5924void zebra_vxlan_remote_vtep_del(ZAPI_HANDLER_ARGS)
d62a17ae 5925{
5926 struct stream *s;
d7c0a89a 5927 unsigned short l = 0;
d62a17ae 5928 vni_t vni;
5929 struct in_addr vtep_ip;
5930 zebra_vni_t *zvni;
5931 zebra_vtep_t *zvtep;
b5ebdc9b 5932 struct interface *ifp;
5933 struct zebra_if *zif;
d62a17ae 5934
ec93aa12 5935 if (!is_evpn_enabled()) {
9df414fe 5936 zlog_debug(
996c9314
LB
5937 "%s: EVPN is not enabled yet we have received a vtep del command",
5938 __PRETTY_FUNCTION__);
8068a649 5939 return;
ec93aa12
DS
5940 }
5941
2853fed6 5942 if (zvrf_id(zvrf) != VRF_DEFAULT) {
9df414fe
QY
5943 zlog_debug("Recv MACIP DEL for non-default VRF %u",
5944 zvrf_id(zvrf));
8068a649 5945 return;
2853fed6 5946 }
5947
1002497a 5948 s = msg;
d62a17ae 5949
89f4e507 5950 while (l < hdr->length) {
d62a17ae 5951 /* Obtain each remote VTEP and process. */
ec93aa12 5952 STREAM_GETL(s, vni);
d62a17ae 5953 l += 4;
ec93aa12 5954 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 5955 l += IPV4_MAX_BYTELEN;
5956
5957 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 5958 zlog_debug("Recv VTEP_DEL %s VNI %u from %s",
5959 inet_ntoa(vtep_ip), vni,
d62a17ae 5960 zebra_route_string(client->proto));
5961
5962 /* Locate VNI hash entry - expected to exist. */
2853fed6 5963 zvni = zvni_lookup(vni);
d62a17ae 5964 if (!zvni) {
5965 if (IS_ZEBRA_DEBUG_VXLAN)
5966 zlog_debug(
5967 "Failed to locate VNI hash upon remote VTEP DEL, "
2853fed6 5968 "VNI %u",
5969 vni);
d62a17ae 5970 continue;
5971 }
5972
b5ebdc9b 5973 ifp = zvni->vxlan_if;
5974 if (!ifp) {
9df414fe 5975 zlog_debug(
60466a63
QY
5976 "VNI %u hash %p doesn't have intf upon remote VTEP DEL",
5977 zvni->vni, zvni);
b682f6de 5978 continue;
b5ebdc9b 5979 }
5980 zif = ifp->info;
5981
5982 /* If down or not mapped to a bridge, we're done. */
b682f6de 5983 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
b5ebdc9b 5984 continue;
5985
d62a17ae 5986 /* If the remote VTEP does not exist, there's nothing more to
5987 * do.
5988 * Otherwise, uninstall any remote MACs pointing to this VTEP
5989 * and
5990 * then, the VTEP entry itself and remove it.
5991 */
5992 zvtep = zvni_vtep_find(zvni, &vtep_ip);
5993 if (!zvtep)
5994 continue;
5995
5996 zvni_neigh_del_from_vtep(zvni, 1, &vtep_ip);
5997 zvni_mac_del_from_vtep(zvni, 1, &vtep_ip);
5998 zvni_vtep_uninstall(zvni, &vtep_ip);
5999 zvni_vtep_del(zvni, zvtep);
6000 }
6001
ec93aa12 6002stream_failure:
8068a649 6003 return;
13d60d35 6004}
6005
6006/*
6007 * Handle message from client to add a remote VTEP for a VNI.
6008 */
89f4e507 6009void zebra_vxlan_remote_vtep_add(ZAPI_HANDLER_ARGS)
d62a17ae 6010{
6011 struct stream *s;
d7c0a89a 6012 unsigned short l = 0;
d62a17ae 6013 vni_t vni;
6014 struct in_addr vtep_ip;
6015 zebra_vni_t *zvni;
b5ebdc9b 6016 struct interface *ifp;
6017 struct zebra_if *zif;
d62a17ae 6018
ec93aa12 6019 if (!is_evpn_enabled()) {
9df414fe 6020 zlog_debug(
996c9314
LB
6021 "%s: EVPN not enabled yet we received a vtep_add zapi call",
6022 __PRETTY_FUNCTION__);
8068a649 6023 return;
ec93aa12
DS
6024 }
6025
2853fed6 6026 if (zvrf_id(zvrf) != VRF_DEFAULT) {
9df414fe
QY
6027 zlog_debug("Recv MACIP ADD for non-default VRF %u",
6028 zvrf_id(zvrf));
8068a649 6029 return;
2853fed6 6030 }
d62a17ae 6031
1002497a 6032 s = msg;
d62a17ae 6033
89f4e507 6034 while (l < hdr->length) {
d62a17ae 6035 /* Obtain each remote VTEP and process. */
ec93aa12 6036 STREAM_GETL(s, vni);
d62a17ae 6037 l += 4;
ec93aa12 6038 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 6039 l += IPV4_MAX_BYTELEN;
6040
6041 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 6042 zlog_debug("Recv VTEP_ADD %s VNI %u from %s",
6043 inet_ntoa(vtep_ip), vni,
d62a17ae 6044 zebra_route_string(client->proto));
6045
6046 /* Locate VNI hash entry - expected to exist. */
2853fed6 6047 zvni = zvni_lookup(vni);
d62a17ae 6048 if (!zvni) {
af4c2728 6049 flog_err(
e914ccbe 6050 EC_ZEBRA_VTEP_ADD_FAILED,
2853fed6 6051 "Failed to locate VNI hash upon remote VTEP ADD, VNI %u",
6052 vni);
d62a17ae 6053 continue;
6054 }
b5ebdc9b 6055
6056 ifp = zvni->vxlan_if;
6057 if (!ifp) {
af4c2728 6058 flog_err(
e914ccbe 6059 EC_ZEBRA_VTEP_ADD_FAILED,
d62a17ae 6060 "VNI %u hash %p doesn't have intf upon remote VTEP ADD",
6061 zvni->vni, zvni);
6062 continue;
6063 }
6064
b5ebdc9b 6065 zif = ifp->info;
d62a17ae 6066
b5ebdc9b 6067 /* If down or not mapped to a bridge, we're done. */
b682f6de 6068 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
d62a17ae 6069 continue;
6070
b682f6de 6071 /* If the remote VTEP already exists,
6072 there's nothing more to do. */
b5ebdc9b 6073 if (zvni_vtep_find(zvni, &vtep_ip))
d62a17ae 6074 continue;
6075
6076 if (zvni_vtep_add(zvni, &vtep_ip) == NULL) {
e914ccbe 6077 flog_err(EC_ZEBRA_VTEP_ADD_FAILED,
1c50c1c0
QY
6078 "Failed to add remote VTEP, VNI %u zvni %p",
6079 vni, zvni);
d62a17ae 6080 continue;
6081 }
6082
6083 zvni_vtep_install(zvni, &vtep_ip);
6084 }
6085
ec93aa12 6086stream_failure:
8068a649 6087 return;
13d60d35 6088}
6089
1a98c087
MK
6090/*
6091 * Add/Del gateway macip to evpn
6092 * g/w can be:
6093 * 1. SVI interface on a vlan aware bridge
6094 * 2. SVI interface on a vlan unaware bridge
6095 * 3. vrr interface (MACVLAN) associated to a SVI
6096 * We advertise macip routes for an interface if it is associated to VxLan vlan
6097 */
6098int zebra_vxlan_add_del_gw_macip(struct interface *ifp, struct prefix *p,
6099 int add)
6100{
6101 struct ipaddr ip;
6102 struct ethaddr macaddr;
6103 zebra_vni_t *zvni = NULL;
1a98c087
MK
6104
6105 memset(&ip, 0, sizeof(struct ipaddr));
6106 memset(&macaddr, 0, sizeof(struct ethaddr));
6107
2853fed6 6108 /* Check if EVPN is enabled. */
6109 if (!is_evpn_enabled())
297a21b6
MK
6110 return 0;
6111
1a98c087
MK
6112 if (IS_ZEBRA_IF_MACVLAN(ifp)) {
6113 struct interface *svi_if =
6114 NULL; /* SVI corresponding to the MACVLAN */
6115 struct zebra_if *ifp_zif =
6116 NULL; /* Zebra daemon specific info for MACVLAN */
6117 struct zebra_if *svi_if_zif =
6118 NULL; /* Zebra daemon specific info for SVI*/
6119
6120 ifp_zif = ifp->info;
6121 if (!ifp_zif)
6122 return -1;
6123
71349e03
MK
6124 /*
6125 * for a MACVLAN interface the link represents the svi_if
6126 */
6127 svi_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
6128 ifp_zif->link_ifindex);
1a98c087 6129 if (!svi_if) {
9df414fe
QY
6130 zlog_debug("MACVLAN %s(%u) without link information",
6131 ifp->name, ifp->ifindex);
1a98c087
MK
6132 return -1;
6133 }
6134
6135 if (IS_ZEBRA_IF_VLAN(svi_if)) {
71349e03
MK
6136 /*
6137 * If it is a vlan aware bridge then the link gives the
6138 * bridge information
6139 */
6140 struct interface *svi_if_link = NULL;
6141
1a98c087 6142 svi_if_zif = svi_if->info;
71349e03
MK
6143 if (svi_if_zif) {
6144 svi_if_link = if_lookup_by_index_per_ns(
60466a63
QY
6145 zebra_ns_lookup(NS_DEFAULT),
6146 svi_if_zif->link_ifindex);
b7cfce93 6147 zvni = zvni_from_svi(svi_if, svi_if_link);
71349e03 6148 }
1a98c087 6149 } else if (IS_ZEBRA_IF_BRIDGE(svi_if)) {
71349e03
MK
6150 /*
6151 * If it is a vlan unaware bridge then svi is the bridge
6152 * itself
6153 */
b7cfce93 6154 zvni = zvni_from_svi(svi_if, svi_if);
1a98c087
MK
6155 }
6156 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
6157 struct zebra_if *svi_if_zif =
71349e03
MK
6158 NULL; /* Zebra daemon specific info for SVI */
6159 struct interface *svi_if_link =
6160 NULL; /* link info for the SVI = bridge info */
1a98c087
MK
6161
6162 svi_if_zif = ifp->info;
e3bb770c
IS
6163 if (svi_if_zif) {
6164 svi_if_link = if_lookup_by_index_per_ns(
cef91a18
QY
6165 zebra_ns_lookup(NS_DEFAULT),
6166 svi_if_zif->link_ifindex);
e3bb770c
IS
6167 if (svi_if_link)
6168 zvni = zvni_from_svi(ifp, svi_if_link);
6169 }
1a98c087 6170 } else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
b7cfce93 6171 zvni = zvni_from_svi(ifp, ifp);
1a98c087
MK
6172 }
6173
6174 if (!zvni)
6175 return 0;
6176
6177 if (!zvni->vxlan_if) {
9df414fe
QY
6178 zlog_debug("VNI %u hash %p doesn't have intf upon MACVLAN up",
6179 zvni->vni, zvni);
1a98c087
MK
6180 return -1;
6181 }
6182
1a98c087 6183
1a98c087
MK
6184 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
6185
6186 if (p->family == AF_INET) {
6187 ip.ipa_type = IPADDR_V4;
6188 memcpy(&(ip.ipaddr_v4), &(p->u.prefix4),
6189 sizeof(struct in_addr));
6190 } else if (p->family == AF_INET6) {
6191 ip.ipa_type = IPADDR_V6;
6192 memcpy(&(ip.ipaddr_v6), &(p->u.prefix6),
6193 sizeof(struct in6_addr));
6194 }
6195
6196
6197 if (add)
6198 zvni_gw_macip_add(ifp, zvni, &macaddr, &ip);
6199 else
6200 zvni_gw_macip_del(ifp, zvni, &ip);
6201
6202 return 0;
6203}
6204
2232a77c 6205/*
b7cfce93
MK
6206 * Handle SVI interface going down.
6207 * SVI can be associated to either L3-VNI or L2-VNI.
6208 * For L2-VNI: At this point, this is a NOP since
6209 * the kernel deletes the neighbor entries on this SVI (if any).
6210 * We only need to update the vrf corresponding to zvni.
6211 * For L3-VNI: L3-VNI is operationally down, update mac-ip routes and delete
6212 * from bgp
2232a77c 6213 */
d62a17ae 6214int zebra_vxlan_svi_down(struct interface *ifp, struct interface *link_if)
2232a77c 6215{
b7cfce93
MK
6216 zebra_l3vni_t *zl3vni = NULL;
6217
6218 zl3vni = zl3vni_from_svi(ifp, link_if);
6219 if (zl3vni) {
6220
6221 /* process l3-vni down */
6222 zebra_vxlan_process_l3vni_oper_down(zl3vni);
6223
6224 /* remove association with svi-if */
6225 zl3vni->svi_if = NULL;
6226 } else {
6227 zebra_vni_t *zvni = NULL;
6228
6229 /* since we dont have svi corresponding to zvni, we associate it
6230 * to default vrf. Note: the corresponding neigh entries on the
6231 * SVI would have already been deleted */
6232 zvni = zvni_from_svi(ifp, link_if);
6233 if (zvni) {
6234 zvni->vrf_id = VRF_DEFAULT;
6235
6236 /* update the tenant vrf in BGP */
6237 zvni_send_add_to_client(zvni);
6238 }
6239 }
d62a17ae 6240 return 0;
2232a77c 6241}
6242
6243/*
b7cfce93
MK
6244 * Handle SVI interface coming up.
6245 * SVI can be associated to L3-VNI (l3vni vxlan interface) or L2-VNI (l2-vni
6246 * vxlan intf).
6247 * For L2-VNI: we need to install any remote neighbors entried (used for
6248 * apr-suppression)
6249 * For L3-VNI: SVI will be used to get the rmac to be used with L3-VNI
2232a77c 6250 */
d62a17ae 6251int zebra_vxlan_svi_up(struct interface *ifp, struct interface *link_if)
2232a77c 6252{
b7cfce93
MK
6253 zebra_vni_t *zvni = NULL;
6254 zebra_l3vni_t *zl3vni = NULL;
2232a77c 6255
b7cfce93
MK
6256 zl3vni = zl3vni_from_svi(ifp, link_if);
6257 if (zl3vni) {
2232a77c 6258
b7cfce93
MK
6259 /* associate with svi */
6260 zl3vni->svi_if = ifp;
2232a77c 6261
b7cfce93
MK
6262 /* process oper-up */
6263 if (is_l3vni_oper_up(zl3vni))
6264 zebra_vxlan_process_l3vni_oper_up(zl3vni);
6265 } else {
6266
6267 /* process SVI up for l2-vni */
6268 struct neigh_walk_ctx n_wctx;
6269
6270 zvni = zvni_from_svi(ifp, link_if);
6271 if (!zvni)
6272 return 0;
6273
6274 if (!zvni->vxlan_if) {
9df414fe 6275 zlog_debug(
43e52561
QY
6276 "VNI %u hash %p doesn't have intf upon SVI up",
6277 zvni->vni, zvni);
b7cfce93
MK
6278 return -1;
6279 }
6280
6281 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
6282 zlog_debug(
6283 "SVI %s(%u) VNI %u VRF %s is UP, installing neighbors",
6284 ifp->name, ifp->ifindex, zvni->vni,
6285 vrf_id_to_name(ifp->vrf_id));
2232a77c 6286
b7cfce93
MK
6287 /* update the vrf information for l2-vni and inform bgp */
6288 zvni->vrf_id = ifp->vrf_id;
6289 zvni_send_add_to_client(zvni);
6290
6291 /* Install any remote neighbors for this VNI. */
6292 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
6293 n_wctx.zvni = zvni;
996c9314 6294 hash_iterate(zvni->neigh_table, zvni_install_neigh_hash,
b7cfce93
MK
6295 &n_wctx);
6296 }
2232a77c 6297
d62a17ae 6298 return 0;
2232a77c 6299}
6300
13d60d35 6301/*
b7cfce93 6302 * Handle VxLAN interface down
13d60d35 6303 */
d62a17ae 6304int zebra_vxlan_if_down(struct interface *ifp)
13d60d35 6305{
d62a17ae 6306 vni_t vni;
b7cfce93
MK
6307 struct zebra_if *zif = NULL;
6308 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 6309 zebra_l3vni_t *zl3vni = NULL;
6310 zebra_vni_t *zvni;
13d60d35 6311
2853fed6 6312 /* Check if EVPN is enabled. */
6313 if (!is_evpn_enabled())
d62a17ae 6314 return 0;
13d60d35 6315
d62a17ae 6316 zif = ifp->info;
6317 assert(zif);
6318 vxl = &zif->l2info.vxl;
6319 vni = vxl->vni;
13d60d35 6320
643215ce 6321 zl3vni = zl3vni_lookup(vni);
6322 if (zl3vni) {
b7cfce93 6323 /* process-if-down for l3-vni */
b7cfce93 6324 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
6325 zlog_debug("Intf %s(%u) L3-VNI %u is DOWN", ifp->name,
6326 ifp->ifindex, vni);
b7cfce93 6327
b7cfce93 6328 zebra_vxlan_process_l3vni_oper_down(zl3vni);
b7cfce93
MK
6329 } else {
6330 /* process if-down for l2-vni */
b7cfce93 6331 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
6332 zlog_debug("Intf %s(%u) L2-VNI %u is DOWN", ifp->name,
6333 ifp->ifindex, vni);
13d60d35 6334
b7cfce93
MK
6335 /* Locate hash entry; it is expected to exist. */
6336 zvni = zvni_lookup(vni);
6337 if (!zvni) {
9df414fe 6338 zlog_debug(
b7cfce93
MK
6339 "Failed to locate VNI hash at DOWN, IF %s(%u) VNI %u",
6340 ifp->name, ifp->ifindex, vni);
6341 return -1;
6342 }
13d60d35 6343
b7cfce93 6344 assert(zvni->vxlan_if == ifp);
13d60d35 6345
b7cfce93
MK
6346 /* Delete this VNI from BGP. */
6347 zvni_send_del_to_client(zvni->vni);
2232a77c 6348
b7cfce93
MK
6349 /* Free up all neighbors and MACs, if any. */
6350 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
6351 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
13d60d35 6352
b7cfce93
MK
6353 /* Free up all remote VTEPs, if any. */
6354 zvni_vtep_del_all(zvni, 1);
6355 }
d62a17ae 6356 return 0;
13d60d35 6357}
6358
6359/*
6360 * Handle VxLAN interface up - update BGP if required.
6361 */
d62a17ae 6362int zebra_vxlan_if_up(struct interface *ifp)
13d60d35 6363{
d62a17ae 6364 vni_t vni;
b7cfce93
MK
6365 struct zebra_if *zif = NULL;
6366 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 6367 zebra_vni_t *zvni = NULL;
6368 zebra_l3vni_t *zl3vni = NULL;
13d60d35 6369
2853fed6 6370 /* Check if EVPN is enabled. */
6371 if (!is_evpn_enabled())
d62a17ae 6372 return 0;
13d60d35 6373
d62a17ae 6374 zif = ifp->info;
6375 assert(zif);
6376 vxl = &zif->l2info.vxl;
6377 vni = vxl->vni;
13d60d35 6378
643215ce 6379 zl3vni = zl3vni_lookup(vni);
6380 if (zl3vni) {
13d60d35 6381
b7cfce93 6382 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
6383 zlog_debug("Intf %s(%u) L3-VNI %u is UP", ifp->name,
6384 ifp->ifindex, vni);
13d60d35 6385
b7cfce93 6386 /* we need to associate with SVI, if any, we can associate with
523cafc4 6387 * svi-if only after association with vxlan-intf is complete
6388 */
b7cfce93
MK
6389 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
6390
6391 if (is_l3vni_oper_up(zl3vni))
6392 zebra_vxlan_process_l3vni_oper_up(zl3vni);
6393 } else {
6394 /* Handle L2-VNI add */
b7cfce93
MK
6395 struct interface *vlan_if = NULL;
6396
6397 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
6398 zlog_debug("Intf %s(%u) L2-VNI %u is UP", ifp->name,
6399 ifp->ifindex, vni);
b7cfce93
MK
6400
6401 /* Locate hash entry; it is expected to exist. */
6402 zvni = zvni_lookup(vni);
6403 if (!zvni) {
9df414fe 6404 zlog_debug(
b7cfce93
MK
6405 "Failed to locate VNI hash at UP, IF %s(%u) VNI %u",
6406 ifp->name, ifp->ifindex, vni);
6407 return -1;
6408 }
6409
6410 assert(zvni->vxlan_if == ifp);
6411 vlan_if = zvni_map_to_svi(vxl->access_vlan,
6412 zif->brslave_info.br_if);
6413 if (vlan_if) {
6414 zvni->vrf_id = vlan_if->vrf_id;
6415 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
6416 if (zl3vni)
6417 listnode_add_sort(zl3vni->l2vnis, zvni);
6418 }
6419
6420 /* If part of a bridge, inform BGP about this VNI. */
6421 /* Also, read and populate local MACs and neighbors. */
6422 if (zif->brslave_info.br_if) {
6423 zvni_send_add_to_client(zvni);
6424 zvni_read_mac_neigh(zvni, ifp);
6425 }
d62a17ae 6426 }
13d60d35 6427
d62a17ae 6428 return 0;
13d60d35 6429}
6430
6431/*
6432 * Handle VxLAN interface delete. Locate and remove entry in hash table
6433 * and update BGP, if required.
6434 */
d62a17ae 6435int zebra_vxlan_if_del(struct interface *ifp)
13d60d35 6436{
d62a17ae 6437 vni_t vni;
b7cfce93
MK
6438 struct zebra_if *zif = NULL;
6439 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 6440 zebra_vni_t *zvni = NULL;
6441 zebra_l3vni_t *zl3vni = NULL;
13d60d35 6442
2853fed6 6443 /* Check if EVPN is enabled. */
6444 if (!is_evpn_enabled())
d62a17ae 6445 return 0;
13d60d35 6446
d62a17ae 6447 zif = ifp->info;
6448 assert(zif);
6449 vxl = &zif->l2info.vxl;
6450 vni = vxl->vni;
13d60d35 6451
643215ce 6452 zl3vni = zl3vni_lookup(vni);
6453 if (zl3vni) {
b7cfce93
MK
6454
6455 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
6456 zlog_debug("Del L3-VNI %u intf %s(%u)", vni, ifp->name,
6457 ifp->ifindex);
13d60d35 6458
b7cfce93
MK
6459 /* process oper-down for l3-vni */
6460 zebra_vxlan_process_l3vni_oper_down(zl3vni);
2232a77c 6461
b7cfce93 6462 /* remove the association with vxlan_if */
b67a60d2 6463 memset(&zl3vni->local_vtep_ip, 0, sizeof(struct in_addr));
b7cfce93
MK
6464 zl3vni->vxlan_if = NULL;
6465 } else {
13d60d35 6466
b7cfce93 6467 /* process if-del for l2-vni*/
b7cfce93 6468 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
6469 zlog_debug("Del L2-VNI %u intf %s(%u)", vni, ifp->name,
6470 ifp->ifindex);
b7cfce93
MK
6471
6472 /* Locate hash entry; it is expected to exist. */
6473 zvni = zvni_lookup(vni);
6474 if (!zvni) {
9df414fe 6475 zlog_debug(
b7cfce93
MK
6476 "Failed to locate VNI hash at del, IF %s(%u) VNI %u",
6477 ifp->name, ifp->ifindex, vni);
6478 return 0;
6479 }
6480
6481 /* remove from l3-vni list */
6482 zl3vni = zl3vni_from_vrf(zvni->vrf_id);
6483 if (zl3vni)
6484 listnode_delete(zl3vni->l2vnis, zvni);
6485
6486 /* Delete VNI from BGP. */
6487 zvni_send_del_to_client(zvni->vni);
6488
6489 /* Free up all neighbors and MAC, if any. */
6490 zvni_neigh_del_all(zvni, 0, 0, DEL_ALL_NEIGH);
6491 zvni_mac_del_all(zvni, 0, 0, DEL_ALL_MAC);
6492
6493 /* Free up all remote VTEPs, if any. */
6494 zvni_vtep_del_all(zvni, 0);
6495
6496 /* Delete the hash entry. */
6497 if (zvni_del(zvni)) {
e914ccbe 6498 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
1c50c1c0
QY
6499 "Failed to del VNI hash %p, IF %s(%u) VNI %u",
6500 zvni, ifp->name, ifp->ifindex, zvni->vni);
b7cfce93
MK
6501 return -1;
6502 }
d62a17ae 6503 }
d62a17ae 6504 return 0;
13d60d35 6505}
6506
6507/*
6508 * Handle VxLAN interface update - change to tunnel IP, master or VLAN.
6509 */
d7c0a89a 6510int zebra_vxlan_if_update(struct interface *ifp, uint16_t chgflags)
d62a17ae 6511{
d62a17ae 6512 vni_t vni;
b7cfce93
MK
6513 struct zebra_if *zif = NULL;
6514 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 6515 zebra_vni_t *zvni = NULL;
6516 zebra_l3vni_t *zl3vni = NULL;
d62a17ae 6517
2853fed6 6518 /* Check if EVPN is enabled. */
6519 if (!is_evpn_enabled())
d62a17ae 6520 return 0;
6521
6522 zif = ifp->info;
6523 assert(zif);
6524 vxl = &zif->l2info.vxl;
6525 vni = vxl->vni;
6526
643215ce 6527 zl3vni = zl3vni_lookup(vni);
6528 if (zl3vni) {
af026ae4 6529
b7cfce93
MK
6530 if (IS_ZEBRA_DEBUG_VXLAN)
6531 zlog_debug(
6532 "Update L3-VNI %u intf %s(%u) VLAN %u local IP %s master %u chg 0x%x",
996c9314
LB
6533 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
6534 inet_ntoa(vxl->vtep_ip),
b7cfce93
MK
6535 zif->brslave_info.bridge_ifindex, chgflags);
6536
6537 /* Removed from bridge? Cleanup and return */
6538 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
6539 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
6540 zebra_vxlan_process_l3vni_oper_down(zl3vni);
6541 return 0;
6542 }
6543
6544 /* access-vlan change - process oper down, associate with new
523cafc4 6545 * svi_if and then process oper up again
6546 */
b7cfce93
MK
6547 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
6548 if (if_is_operative(ifp)) {
6549 zebra_vxlan_process_l3vni_oper_down(zl3vni);
6550 zl3vni->svi_if = NULL;
6551 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
bca63dc8 6552 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93
MK
6553 if (is_l3vni_oper_up(zl3vni))
6554 zebra_vxlan_process_l3vni_oper_up(
996c9314 6555 zl3vni);
b7cfce93
MK
6556 }
6557 }
d62a17ae 6558
12eeac84
MK
6559 /*
6560 * local-ip change - process oper down, associate with new
6561 * local-ip and then process oper up again
6562 */
6563 if (chgflags & ZEBRA_VXLIF_LOCAL_IP_CHANGE) {
6564 if (if_is_operative(ifp)) {
6565 zebra_vxlan_process_l3vni_oper_down(zl3vni);
6566 zl3vni->local_vtep_ip = vxl->vtep_ip;
6567 if (is_l3vni_oper_up(zl3vni))
6568 zebra_vxlan_process_l3vni_oper_up(
996c9314 6569 zl3vni);
12eeac84
MK
6570 }
6571 }
6572
bca63dc8
MK
6573 /* Update local tunnel IP. */
6574 zl3vni->local_vtep_ip = vxl->vtep_ip;
6575
12eeac84
MK
6576 /* if we have a valid new master, process l3-vni oper up */
6577 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE) {
6578 if (if_is_operative(ifp) && is_l3vni_oper_up(zl3vni))
b7cfce93
MK
6579 zebra_vxlan_process_l3vni_oper_up(zl3vni);
6580 }
6581 } else {
d62a17ae 6582
b7cfce93
MK
6583 /* Update VNI hash. */
6584 zvni = zvni_lookup(vni);
6585 if (!zvni) {
9df414fe 6586 zlog_debug(
b7cfce93
MK
6587 "Failed to find L2-VNI hash on update, IF %s(%u) VNI %u",
6588 ifp->name, ifp->ifindex, vni);
6589 return -1;
6590 }
d62a17ae 6591
b7cfce93
MK
6592 if (IS_ZEBRA_DEBUG_VXLAN)
6593 zlog_debug(
6594 "Update L2-VNI %u intf %s(%u) VLAN %u local IP %s master %u chg 0x%x",
996c9314
LB
6595 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
6596 inet_ntoa(vxl->vtep_ip),
b7cfce93
MK
6597 zif->brslave_info.bridge_ifindex, chgflags);
6598
6599 /* Removed from bridge? Cleanup and return */
6600 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
6601 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
6602 /* Delete from client, remove all remote VTEPs */
6603 /* Also, free up all MACs and neighbors. */
6604 zvni_send_del_to_client(zvni->vni);
6605 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
6606 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
6607 zvni_vtep_del_all(zvni, 1);
6608 return 0;
6609 }
d62a17ae 6610
b7cfce93
MK
6611 /* Handle other changes. */
6612 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
6613 /* Remove all existing local neigh and MACs for this VNI
6614 * (including from BGP)
6615 */
6616 zvni_neigh_del_all(zvni, 0, 1, DEL_LOCAL_MAC);
6617 zvni_mac_del_all(zvni, 0, 1, DEL_LOCAL_MAC);
6618 }
d62a17ae 6619
b7cfce93
MK
6620 zvni->local_vtep_ip = vxl->vtep_ip;
6621 zvni->vxlan_if = ifp;
d62a17ae 6622
b7cfce93
MK
6623 /* Take further actions needed.
6624 * Note that if we are here, there is a change of interest.
6625 */
6626 /* If down or not mapped to a bridge, we're done. */
6627 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
6628 return 0;
d62a17ae 6629
b7cfce93
MK
6630 /* Inform BGP, if there is a change of interest. */
6631 if (chgflags
6632 & (ZEBRA_VXLIF_MASTER_CHANGE | ZEBRA_VXLIF_LOCAL_IP_CHANGE))
6633 zvni_send_add_to_client(zvni);
6634
6635 /* If there is a valid new master or a VLAN mapping change,
6636 * read and populate local MACs and neighbors.
6637 * Also, reinstall any remote MACs and neighbors
6638 * for this VNI (based on new VLAN).
6639 */
6640 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
6641 zvni_read_mac_neigh(zvni, ifp);
6642 else if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
6643 struct mac_walk_ctx m_wctx;
6644 struct neigh_walk_ctx n_wctx;
6645
6646 zvni_read_mac_neigh(zvni, ifp);
6647
6648 memset(&m_wctx, 0, sizeof(struct mac_walk_ctx));
6649 m_wctx.zvni = zvni;
996c9314 6650 hash_iterate(zvni->mac_table, zvni_install_mac_hash,
b7cfce93
MK
6651 &m_wctx);
6652
6653 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
6654 n_wctx.zvni = zvni;
6655 hash_iterate(zvni->neigh_table, zvni_install_neigh_hash,
6656 &n_wctx);
6657 }
d62a17ae 6658 }
6659
6660 return 0;
13d60d35 6661}
6662
6663/*
6664 * Handle VxLAN interface add.
6665 */
d62a17ae 6666int zebra_vxlan_if_add(struct interface *ifp)
13d60d35 6667{
d62a17ae 6668 vni_t vni;
b7cfce93
MK
6669 struct zebra_if *zif = NULL;
6670 struct zebra_l2info_vxlan *vxl = NULL;
643215ce 6671 zebra_vni_t *zvni = NULL;
6672 zebra_l3vni_t *zl3vni = NULL;
13d60d35 6673
2853fed6 6674 /* Check if EVPN is enabled. */
6675 if (!is_evpn_enabled())
d62a17ae 6676 return 0;
13d60d35 6677
d62a17ae 6678 zif = ifp->info;
6679 assert(zif);
6680 vxl = &zif->l2info.vxl;
6681 vni = vxl->vni;
13d60d35 6682
643215ce 6683 zl3vni = zl3vni_lookup(vni);
6684 if (zl3vni) {
13d60d35 6685
b7cfce93 6686 /* process if-add for l3-vni*/
b7cfce93
MK
6687 if (IS_ZEBRA_DEBUG_VXLAN)
6688 zlog_debug(
6689 "Add L3-VNI %u intf %s(%u) VLAN %u local IP %s master %u",
996c9314
LB
6690 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
6691 inet_ntoa(vxl->vtep_ip),
b7cfce93
MK
6692 zif->brslave_info.bridge_ifindex);
6693
b7cfce93 6694 /* associate with vxlan_if */
b67a60d2 6695 zl3vni->local_vtep_ip = vxl->vtep_ip;
b7cfce93
MK
6696 zl3vni->vxlan_if = ifp;
6697
6698 /* Associate with SVI, if any. We can associate with svi-if only
6699 * after association with vxlan_if is complete */
6700 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
6701
6702 if (is_l3vni_oper_up(zl3vni))
6703 zebra_vxlan_process_l3vni_oper_up(zl3vni);
6704 } else {
6705
6706 /* process if-add for l2-vni */
b7cfce93
MK
6707 struct interface *vlan_if = NULL;
6708
6709 /* Create or update VNI hash. */
6710 zvni = zvni_lookup(vni);
6711 if (!zvni) {
6712 zvni = zvni_add(vni);
6713 if (!zvni) {
af4c2728 6714 flog_err(
e914ccbe 6715 EC_ZEBRA_VNI_ADD_FAILED,
b7cfce93
MK
6716 "Failed to add VNI hash, IF %s(%u) VNI %u",
6717 ifp->name, ifp->ifindex, vni);
6718 return -1;
6719 }
6720 }
6721
6722 zvni->local_vtep_ip = vxl->vtep_ip;
6723 zvni->vxlan_if = ifp;
6724 vlan_if = zvni_map_to_svi(vxl->access_vlan,
6725 zif->brslave_info.br_if);
6726 if (vlan_if) {
6727 zvni->vrf_id = vlan_if->vrf_id;
6728 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
6729 if (zl3vni)
6730 listnode_add_sort(zl3vni->l2vnis, zvni);
6731 }
6732
6733 if (IS_ZEBRA_DEBUG_VXLAN)
6734 zlog_debug(
6735 "Add L2-VNI %u VRF %s intf %s(%u) VLAN %u local IP %s master %u",
6736 vni,
996c9314
LB
6737 vlan_if ? vrf_id_to_name(vlan_if->vrf_id)
6738 : "Default",
6739 ifp->name, ifp->ifindex, vxl->access_vlan,
6740 inet_ntoa(vxl->vtep_ip),
b7cfce93
MK
6741 zif->brslave_info.bridge_ifindex);
6742
6743 /* If down or not mapped to a bridge, we're done. */
6744 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
6745 return 0;
6746
6747 /* Inform BGP */
6748 zvni_send_add_to_client(zvni);
6749
6750 /* Read and populate local MACs and neighbors */
6751 zvni_read_mac_neigh(zvni, ifp);
6752 }
6753
6754 return 0;
6755}
6756
996c9314
LB
6757int zebra_vxlan_process_vrf_vni_cmd(struct zebra_vrf *zvrf, vni_t vni,
6758 char *err, int err_str_sz, int filter,
6759 int add)
b7cfce93
MK
6760{
6761 zebra_l3vni_t *zl3vni = NULL;
6762 struct zebra_vrf *zvrf_default = NULL;
6763
6764 zvrf_default = zebra_vrf_lookup_by_id(VRF_DEFAULT);
6765 if (!zvrf_default)
6766 return -1;
6767
6768 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314 6769 zlog_debug("vrf %s vni %u %s", zvrf_name(zvrf), vni,
b7cfce93
MK
6770 add ? "ADD" : "DEL");
6771
6772 if (add) {
6773
6774 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
6775
6776 /* check if the vni is already present under zvrf */
6777 if (zvrf->l3vni) {
3f02fbab 6778 snprintf(err, err_str_sz,
b7cfce93
MK
6779 "VNI is already configured under the vrf");
6780 return -1;
6781 }
6782
6783 /* check if this VNI is already present in the system */
6784 zl3vni = zl3vni_lookup(vni);
6785 if (zl3vni) {
3f02fbab 6786 snprintf(err, err_str_sz,
b7cfce93
MK
6787 "VNI is already configured as L3-VNI");
6788 return -1;
6789 }
6790
6791 /* add the L3-VNI to the global table */
6792 zl3vni = zl3vni_add(vni, zvrf_id(zvrf));
6793 if (!zl3vni) {
996c9314 6794 snprintf(err, err_str_sz, "Could not add L3-VNI");
b7cfce93
MK
6795 return -1;
6796 }
6797
6798 /* associate the vrf with vni */
6799 zvrf->l3vni = vni;
6800
c48d9f5f
MK
6801 /* set the filter in l3vni to denote if we are using l3vni only
6802 * for prefix routes
6803 */
6804 if (filter)
6805 SET_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY);
6806
b7cfce93 6807 /* associate with vxlan-intf;
523cafc4 6808 * we need to associate with the vxlan-intf first
6809 */
b7cfce93
MK
6810 zl3vni->vxlan_if = zl3vni_map_to_vxlan_if(zl3vni);
6811
6812 /* associate with corresponding SVI interface, we can associate
6813 * with svi-if only after vxlan interface association is
523cafc4 6814 * complete
6815 */
b7cfce93
MK
6816 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
6817
6818 /* formulate l2vni list */
996c9314
LB
6819 hash_iterate(zvrf_default->vni_table, zvni_add_to_l3vni_list,
6820 zl3vni);
b7cfce93
MK
6821
6822 if (is_l3vni_oper_up(zl3vni))
6823 zebra_vxlan_process_l3vni_oper_up(zl3vni);
6824
6825 } else {
6826 zl3vni = zl3vni_lookup(vni);
6827 if (!zl3vni) {
3f02fbab 6828 snprintf(err, err_str_sz, "VNI doesn't exist");
d62a17ae 6829 return -1;
6830 }
b7cfce93 6831
cf299714
MK
6832 if (filter && !CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)) {
6833 snprintf(err, ERR_STR_SZ,
6834 "prefix-routes-only is not set for the vni");
6835 return -1;
6836 }
6837
b7cfce93
MK
6838 zebra_vxlan_process_l3vni_oper_down(zl3vni);
6839
5e06422c 6840 /* delete and uninstall all rmacs */
996c9314 6841 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
5e06422c
MK
6842 zl3vni);
6843
6844 /* delete and uninstall all next-hops */
996c9314 6845 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
5e06422c
MK
6846 zl3vni);
6847
b7cfce93
MK
6848 zvrf->l3vni = 0;
6849 zl3vni_del(zl3vni);
6850
6851 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
d62a17ae 6852 }
b7cfce93
MK
6853 return 0;
6854}
13d60d35 6855
84915b0a 6856int zebra_vxlan_vrf_enable(struct zebra_vrf *zvrf)
6857{
6858 zebra_l3vni_t *zl3vni = NULL;
6859
6860 if (zvrf->l3vni)
6861 zl3vni = zl3vni_lookup(zvrf->l3vni);
6862 if (!zl3vni)
6863 return 0;
6864
6865 zl3vni->vrf_id = zvrf_id(zvrf);
6866 if (is_l3vni_oper_up(zl3vni))
6867 zebra_vxlan_process_l3vni_oper_up(zl3vni);
6868 return 0;
6869}
6870
6871int zebra_vxlan_vrf_disable(struct zebra_vrf *zvrf)
b7cfce93
MK
6872{
6873 zebra_l3vni_t *zl3vni = NULL;
13d60d35 6874
84915b0a 6875 if (zvrf->l3vni)
6876 zl3vni = zl3vni_lookup(zvrf->l3vni);
b7cfce93 6877 if (!zl3vni)
d62a17ae 6878 return 0;
13d60d35 6879
84915b0a 6880 zl3vni->vrf_id = VRF_UNKNOWN;
b7cfce93 6881 zebra_vxlan_process_l3vni_oper_down(zl3vni);
84915b0a 6882 return 0;
6883}
6884
6885int zebra_vxlan_vrf_delete(struct zebra_vrf *zvrf)
6886{
6887 zebra_l3vni_t *zl3vni = NULL;
6888 vni_t vni;
6889
6890 if (zvrf->l3vni)
6891 zl3vni = zl3vni_lookup(zvrf->l3vni);
6892 if (!zl3vni)
6893 return 0;
6894
6895 vni = zl3vni->vni;
b7cfce93 6896 zl3vni_del(zl3vni);
84915b0a 6897 zebra_vxlan_handle_vni_transition(zvrf, vni, 0);
2232a77c 6898
d62a17ae 6899 return 0;
13d60d35 6900}
6901
31310b25
MK
6902/*
6903 * Handle message from client to enable/disable advertisement of g/w macip
6904 * routes
6905 */
89f4e507 6906void zebra_vxlan_advertise_subnet(ZAPI_HANDLER_ARGS)
31310b25
MK
6907{
6908 struct stream *s;
6909 int advertise;
6910 vni_t vni = 0;
6911 zebra_vni_t *zvni = NULL;
6912 struct interface *ifp = NULL;
6913 struct zebra_if *zif = NULL;
6914 struct zebra_l2info_vxlan zl2_info;
6915 struct interface *vlan_if = NULL;
6916
6917 if (zvrf_id(zvrf) != VRF_DEFAULT) {
9df414fe
QY
6918 zlog_debug("EVPN GW-MACIP Adv for non-default VRF %u",
6919 zvrf_id(zvrf));
8068a649 6920 return;
31310b25
MK
6921 }
6922
1002497a 6923 s = msg;
2017b3ea 6924 STREAM_GETC(s, advertise);
31310b25
MK
6925 vni = stream_get3(s);
6926
6927 zvni = zvni_lookup(vni);
6928 if (!zvni)
8068a649 6929 return;
31310b25
MK
6930
6931 if (zvni->advertise_subnet == advertise)
8068a649 6932 return;
31310b25
MK
6933
6934 if (IS_ZEBRA_DEBUG_VXLAN)
996c9314
LB
6935 zlog_debug("EVPN subnet Adv %s on VNI %d , currently %s",
6936 advertise ? "enabled" : "disabled", vni,
6937 zvni->advertise_subnet ? "enabled" : "disabled");
31310b25
MK
6938
6939
6940 zvni->advertise_subnet = advertise;
6941
6942 ifp = zvni->vxlan_if;
6943 if (!ifp)
8068a649 6944 return;
31310b25
MK
6945
6946 zif = ifp->info;
6947
6948 /* If down or not mapped to a bridge, we're done. */
6949 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8068a649 6950 return;
31310b25
MK
6951
6952 zl2_info = zif->l2info.vxl;
6953
996c9314
LB
6954 vlan_if =
6955 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
31310b25 6956 if (!vlan_if)
8068a649 6957 return;
31310b25
MK
6958
6959 if (zvni->advertise_subnet)
6960 zvni_advertise_subnet(zvni, vlan_if, 1);
6961 else
6962 zvni_advertise_subnet(zvni, vlan_if, 0);
2017b3ea
DS
6963
6964stream_failure:
6965 return;
31310b25
MK
6966}
6967
1a98c087
MK
6968/*
6969 * Handle message from client to enable/disable advertisement of g/w macip
6970 * routes
6971 */
89f4e507 6972void zebra_vxlan_advertise_gw_macip(ZAPI_HANDLER_ARGS)
1a98c087
MK
6973{
6974 struct stream *s;
6975 int advertise;
6976 vni_t vni = 0;
6977 zebra_vni_t *zvni = NULL;
b5ebdc9b 6978 struct interface *ifp = NULL;
1a98c087 6979
2853fed6 6980 if (zvrf_id(zvrf) != VRF_DEFAULT) {
9df414fe
QY
6981 zlog_debug("EVPN GW-MACIP Adv for non-default VRF %u",
6982 zvrf_id(zvrf));
8068a649 6983 return;
2853fed6 6984 }
6985
1002497a 6986 s = msg;
ec93aa12 6987 STREAM_GETC(s, advertise);
cc6d5476 6988 STREAM_GETL(s, vni);
1a98c087
MK
6989
6990 if (!vni) {
6991 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 6992 zlog_debug("EVPN gateway macip Adv %s, currently %s",
1a98c087 6993 advertise ? "enabled" : "disabled",
2853fed6 6994 advertise_gw_macip_enabled(NULL)
1a98c087
MK
6995 ? "enabled"
6996 : "disabled");
6997
6998 if (zvrf->advertise_gw_macip == advertise)
8068a649 6999 return;
1a98c087
MK
7000
7001 zvrf->advertise_gw_macip = advertise;
7002
2853fed6 7003 if (advertise_gw_macip_enabled(zvni))
1a98c087 7004 hash_iterate(zvrf->vni_table,
2853fed6 7005 zvni_gw_macip_add_for_vni_hash, NULL);
1a98c087
MK
7006 else
7007 hash_iterate(zvrf->vni_table,
2853fed6 7008 zvni_gw_macip_del_for_vni_hash, NULL);
1a98c087
MK
7009
7010 } else {
7011 struct zebra_if *zif = NULL;
7012 struct zebra_l2info_vxlan zl2_info;
7013 struct interface *vlan_if = NULL;
7014 struct interface *vrr_if = NULL;
7015
01a6143b
MK
7016 zvni = zvni_lookup(vni);
7017 if (!zvni)
124ead27 7018 return;
01a6143b 7019
1a98c087
MK
7020 if (IS_ZEBRA_DEBUG_VXLAN)
7021 zlog_debug(
2853fed6 7022 "EVPN gateway macip Adv %s on VNI %d , currently %s",
1a98c087 7023 advertise ? "enabled" : "disabled", vni,
996c9314
LB
7024 advertise_gw_macip_enabled(zvni) ? "enabled"
7025 : "disabled");
1a98c087 7026
1a98c087 7027 if (zvni->advertise_gw_macip == advertise)
8068a649 7028 return;
1a98c087
MK
7029
7030 zvni->advertise_gw_macip = advertise;
7031
b5ebdc9b 7032 ifp = zvni->vxlan_if;
7033 if (!ifp)
8068a649 7034 return;
b5ebdc9b 7035
7036 zif = ifp->info;
7037
7038 /* If down or not mapped to a bridge, we're done. */
b682f6de 7039 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8068a649 7040 return;
b5ebdc9b 7041
1a98c087
MK
7042 zl2_info = zif->l2info.vxl;
7043
2853fed6 7044 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
1a98c087
MK
7045 zif->brslave_info.br_if);
7046 if (!vlan_if)
8068a649 7047 return;
1a98c087 7048
2853fed6 7049 if (advertise_gw_macip_enabled(zvni)) {
1a98c087
MK
7050 /* Add primary SVI MAC-IP */
7051 zvni_add_macip_for_intf(vlan_if, zvni);
7052
7053 /* Add VRR MAC-IP - if any*/
7054 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
7055 if (vrr_if)
7056 zvni_add_macip_for_intf(vrr_if, zvni);
7057 } else {
7058 /* Del primary MAC-IP */
7059 zvni_del_macip_for_intf(vlan_if, zvni);
7060
7061 /* Del VRR MAC-IP - if any*/
7062 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
7063 if (vrr_if)
7064 zvni_del_macip_for_intf(vrr_if, zvni);
7065 }
7066 }
7067
ec93aa12 7068stream_failure:
8068a649 7069 return;
1a98c087
MK
7070}
7071
7072
13d60d35 7073/*
7074 * Handle message from client to learn (or stop learning) about VNIs and MACs.
7075 * When enabled, the VNI hash table will be built and MAC FDB table read;
7076 * when disabled, the entries should be deleted and remote VTEPs and MACs
7077 * uninstalled from the kernel.
7078 */
89f4e507 7079void zebra_vxlan_advertise_all_vni(ZAPI_HANDLER_ARGS)
13d60d35 7080{
655b04d1
MK
7081 struct stream *s = NULL;
7082 int advertise = 0;
7083 struct zebra_ns *zns = NULL;
13d60d35 7084
2853fed6 7085 if (zvrf_id(zvrf) != VRF_DEFAULT) {
9df414fe
QY
7086 zlog_debug("EVPN VNI Adv for non-default VRF %u",
7087 zvrf_id(zvrf));
8068a649 7088 return;
2853fed6 7089 }
7090
1002497a 7091 s = msg;
ec93aa12 7092 STREAM_GETC(s, advertise);
13d60d35 7093
d62a17ae 7094 if (IS_ZEBRA_DEBUG_VXLAN)
2853fed6 7095 zlog_debug("EVPN VNI Adv %s, currently %s",
d62a17ae 7096 advertise ? "enabled" : "disabled",
2853fed6 7097 is_evpn_enabled() ? "enabled" : "disabled");
13d60d35 7098
d62a17ae 7099 if (zvrf->advertise_all_vni == advertise)
8068a649 7100 return;
13d60d35 7101
d62a17ae 7102 zvrf->advertise_all_vni = advertise;
2853fed6 7103 if (is_evpn_enabled()) {
d62a17ae 7104 /* Build VNI hash table and inform BGP. */
2853fed6 7105 zvni_build_hash_table();
2232a77c 7106
1a98c087
MK
7107 /* Add all SVI (L3 GW) MACs to BGP*/
7108 hash_iterate(zvrf->vni_table, zvni_gw_macip_add_for_vni_hash,
2853fed6 7109 NULL);
1a98c087 7110
d62a17ae 7111 /* Read the MAC FDB */
7112 macfdb_read(zvrf->zns);
2232a77c 7113
d62a17ae 7114 /* Read neighbors */
7115 neigh_read(zvrf->zns);
7116 } else {
7117 /* Cleanup VTEPs for all VNIs - uninstall from
7118 * kernel and free entries.
7119 */
7120 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
655b04d1
MK
7121
7122 /* cleanup all l3vnis */
7123 zns = zebra_ns_lookup(NS_DEFAULT);
7124 if (!zns)
8068a649 7125 return;
655b04d1
MK
7126
7127 hash_iterate(zns->l3vni_table, zl3vni_cleanup_all, NULL);
d62a17ae 7128 }
13d60d35 7129
ec93aa12 7130stream_failure:
8068a649 7131 return;
13d60d35 7132}
7133
7134/*
7135 * Allocate VNI hash table for this VRF and do other initialization.
7136 * NOTE: Currently supported only for default VRF.
7137 */
d62a17ae 7138void zebra_vxlan_init_tables(struct zebra_vrf *zvrf)
13d60d35 7139{
d62a17ae 7140 if (!zvrf)
7141 return;
7142 zvrf->vni_table = hash_create(vni_hash_keymake, vni_hash_cmp,
7143 "Zebra VRF VNI Table");
13d60d35 7144}
7145
84915b0a 7146/* Cleanup VNI info, but don't free the table. */
7147void zebra_vxlan_cleanup_tables(struct zebra_vrf *zvrf)
7148{
7149 if (!zvrf)
7150 return;
7151 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
7152}
7153
13d60d35 7154/* Close all VNI handling */
d62a17ae 7155void zebra_vxlan_close_tables(struct zebra_vrf *zvrf)
13d60d35 7156{
2853fed6 7157 if (!zvrf)
7158 return;
d62a17ae 7159 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
9b67b514 7160 hash_free(zvrf->vni_table);
13d60d35 7161}
b7cfce93
MK
7162
7163/* init the l3vni table */
7164void zebra_vxlan_ns_init(struct zebra_ns *zns)
7165{
7166 zns->l3vni_table = hash_create(l3vni_hash_keymake, l3vni_hash_cmp,
7167 "Zebra VRF L3 VNI table");
7168}
7169
7170/* free l3vni table */
7171void zebra_vxlan_ns_disable(struct zebra_ns *zns)
7172{
7173 hash_free(zns->l3vni_table);
7174}
d3135ba3 7175
7176/* get the l3vni svi ifindex */
7177ifindex_t get_l3vni_svi_ifindex(vrf_id_t vrf_id)
7178{
7179 zebra_l3vni_t *zl3vni = NULL;
7180
7181 zl3vni = zl3vni_from_vrf(vrf_id);
7182 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
7183 return 0;
7184
7185 return zl3vni->svi_if->ifindex;
7186}