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