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