]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vxlan.c
Merge remote-tracking branch 'origin/master' into evpn_plus_struct_attr
[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
50 DEFINE_MTYPE_STATIC(ZEBRA, ZVNI, "VNI hash");
51 DEFINE_MTYPE_STATIC(ZEBRA, ZVNI_VTEP, "VNI remote VTEP");
52 DEFINE_MTYPE_STATIC(ZEBRA, MAC, "VNI MAC");
53 DEFINE_MTYPE_STATIC(ZEBRA, NEIGH, "VNI Neighbor");
54
55 /* definitions */
56
57
58 /* static function declarations */
59 static void
60 zvni_print_neigh (zebra_neigh_t *n, void *ctxt);
61 static void
62 zvni_print_neigh_hash (struct hash_backet *backet, void *ctxt);
63 static void
64 zvni_print_neigh_hash_all_vni (struct hash_backet *backet, void *ctxt);
65 static void
66 zvni_print_mac (zebra_mac_t *mac, void *ctxt);
67 static void
68 zvni_print_mac_hash (struct hash_backet *backet, void *ctxt);
69 static void
70 zvni_print_mac_hash_all_vni (struct hash_backet *backet, void *ctxt);
71 static void
72 zvni_print (zebra_vni_t *zvni, void *ctxt);
73 static void
74 zvni_print_hash (struct hash_backet *backet, void *ctxt);
75
76 static int
77 zvni_macip_send_msg_to_client (struct zebra_vrf *zvrf, vni_t vni,
78 struct ethaddr *macaddr,
79 struct ipaddr *ip, u_char sticky,
80 u_int16_t cmd);
81 static unsigned int
82 neigh_hash_keymake (void *p);
83 static int
84 neigh_cmp (const void *p1, const void *p2);
85 static void *
86 zvni_neigh_alloc (void *p);
87 static zebra_neigh_t *
88 zvni_neigh_add (zebra_vni_t *zvni, struct ipaddr *ip);
89 static int
90 zvni_neigh_del (zebra_vni_t *zvni, zebra_neigh_t *n);
91 static int
92 zvni_neigh_del_hash_entry (struct hash_backet *backet, void *arg);
93 static void
94 zvni_neigh_del_from_vtep (zebra_vni_t *zvni, int uninstall,
95 struct in_addr *r_vtep_ip);
96 static void
97 zvni_neigh_del_all (struct zebra_vrf *zvrf, zebra_vni_t *zvni,
98 int uninstall, int upd_client, u_int32_t flags);
99 static zebra_neigh_t *
100 zvni_neigh_lookup (zebra_vni_t *zvni, struct ipaddr *ip);
101 static int
102 zvni_neigh_send_add_to_client (struct zebra_vrf *zvrf, vni_t vni,
103 struct ipaddr *ip, struct ethaddr *macaddr);
104 static int
105 zvni_neigh_send_del_to_client (struct zebra_vrf *zvrf, vni_t vni,
106 struct ipaddr *ip, struct ethaddr *macaddr);
107 static int
108 zvni_neigh_install (zebra_vni_t *zvni, zebra_neigh_t *n);
109 static int
110 zvni_neigh_uninstall (zebra_vni_t *zvni, zebra_neigh_t *n);
111 static zebra_vni_t *
112 zvni_map_svi (struct interface *ifp, struct interface *br_if);
113 static struct interface *
114 zvni_map_to_svi (struct zebra_vrf *zvrf, vlanid_t vid,
115 struct interface *br_if);
116
117 static unsigned int
118 mac_hash_keymake (void *p);
119 static int
120 mac_cmp (const void *p1, const void *p2);
121 static void *
122 zvni_mac_alloc (void *p);
123 static zebra_mac_t *
124 zvni_mac_add (zebra_vni_t *zvni, struct ethaddr *macaddr);
125 static int
126 zvni_mac_del (zebra_vni_t *zvni, zebra_mac_t *mac);
127 static int
128 zvni_mac_del_hash_entry (struct hash_backet *backet, void *arg);
129 static void
130 zvni_mac_del_from_vtep (zebra_vni_t *zvni, int uninstall,
131 struct in_addr *r_vtep_ip);
132 static void
133 zvni_mac_del_all (struct zebra_vrf *zvrf, zebra_vni_t *zvni,
134 int uninstall, int upd_client, u_int32_t flags);
135 static zebra_mac_t *
136 zvni_mac_lookup (zebra_vni_t *zvni, struct ethaddr *macaddr);
137 static int
138 zvni_mac_send_add_to_client (struct zebra_vrf *zvrf, vni_t vni,
139 struct ethaddr *macaddr, u_char sticky);
140 static int
141 zvni_mac_send_del_to_client (struct zebra_vrf *zvrf, vni_t vni,
142 struct ethaddr *macaddr, u_char sticky);
143 static zebra_vni_t *
144 zvni_map_vlan (struct interface *ifp, struct interface *br_if, vlanid_t vid);
145 static int
146 zvni_mac_install (zebra_vni_t *zvni, zebra_mac_t *mac);
147 static int
148 zvni_mac_uninstall (zebra_vni_t *zvni, zebra_mac_t *mac, int local);
149 static void
150 zvni_install_mac_hash (struct hash_backet *backet, void *ctxt);
151
152 static unsigned int
153 vni_hash_keymake (void *p);
154 static int
155 vni_hash_cmp (const void *p1, const void *p2);
156 static void *
157 zvni_alloc (void *p);
158 static zebra_vni_t *
159 zvni_lookup (struct zebra_vrf *zvrf, vni_t vni);
160 static zebra_vni_t *
161 zvni_add (struct zebra_vrf *zvrf, vni_t vni);
162 static int
163 zvni_del (struct zebra_vrf *zvrf, zebra_vni_t *zvni);
164 static int
165 zvni_send_add_to_client (struct zebra_vrf *zvrf, zebra_vni_t *zvni);
166 static int
167 zvni_send_del_to_client (struct zebra_vrf *zvrf, vni_t vni);
168 static void
169 zvni_build_hash_table (struct zebra_vrf *zvrf);
170 static int
171 zvni_vtep_match (struct in_addr *vtep_ip, zebra_vtep_t *zvtep);
172 static zebra_vtep_t *
173 zvni_vtep_find (zebra_vni_t *zvni, struct in_addr *vtep_ip);
174 static zebra_vtep_t *
175 zvni_vtep_add (zebra_vni_t *zvni, struct in_addr *vtep_ip);
176 static int
177 zvni_vtep_del (zebra_vni_t *zvni, zebra_vtep_t *zvtep);
178 static int
179 zvni_vtep_del_all (zebra_vni_t *zvni, int uninstall);
180 static int
181 zvni_vtep_install (zebra_vni_t *zvni, struct in_addr *vtep_ip);
182 static int
183 zvni_vtep_uninstall (zebra_vni_t *zvni, struct in_addr *vtep_ip);
184
185
186 /* Private functions */
187
188 /*
189 * Helper function to determine maximum width of neighbor IP address for
190 * display - just because we're dealing with IPv6 addresses that can
191 * widely vary.
192 */
193 static void
194 zvni_find_neigh_addr_width (struct hash_backet *backet, void *ctxt)
195 {
196 zebra_neigh_t *n;
197 char buf[INET6_ADDRSTRLEN];
198 struct neigh_walk_ctx *wctx = ctxt;
199 int width;
200
201 n = (zebra_neigh_t *) backet->data;
202 if (!n)
203 return;
204
205 ipaddr2str (&n->ip, buf, sizeof(buf)),
206 width = strlen (buf);
207 if (width > wctx->addr_width)
208 wctx->addr_width = width;
209 }
210
211 /*
212 * Print a specific neighbor entry.
213 */
214 static void
215 zvni_print_neigh (zebra_neigh_t *n, void *ctxt)
216 {
217 struct vty *vty;
218 char buf1[ETHER_ADDR_STRLEN];
219 char buf2[INET6_ADDRSTRLEN];
220
221 ipaddr2str (&n->ip, buf2, sizeof(buf2)),
222 vty = (struct vty *) ctxt;
223 vty_out(vty, "IP: %s%s",
224 ipaddr2str (&n->ip, buf2, sizeof(buf2)), VTY_NEWLINE);
225 vty_out(vty, " MAC: %s", prefix_mac2str (&n->emac, buf1, sizeof (buf1)));
226 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
227 vty_out(vty, " Remote VTEP: %s", inet_ntoa (n->r_vtep_ip));
228 vty_out(vty, "%s", VTY_NEWLINE);
229 }
230
231 /*
232 * Print neighbor hash entry - called for display of all neighbors.
233 */
234 static void
235 zvni_print_neigh_hash (struct hash_backet *backet, void *ctxt)
236 {
237 struct vty *vty;
238 zebra_neigh_t *n;
239 char buf1[ETHER_ADDR_STRLEN];
240 char buf2[INET6_ADDRSTRLEN];
241 struct neigh_walk_ctx *wctx = ctxt;
242
243 vty = wctx->vty;
244 n = (zebra_neigh_t *) backet->data;
245 if (!n)
246 return;
247
248 prefix_mac2str (&n->emac, buf1, sizeof (buf1));
249 ipaddr2str (&n->ip, buf2, sizeof(buf2));
250 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) &&
251 !(wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP))
252 {
253 vty_out(vty, "%*s %-6s %-17s %s",
254 -wctx->addr_width, buf2, "local", buf1, VTY_NEWLINE);
255 wctx->count++;
256 }
257 else
258 {
259 if (wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
260 {
261 if (IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))
262 {
263 if (wctx->count == 0)
264 vty_out(vty, "%*s %-6s %-17s %-21s%s",
265 -wctx->addr_width, "Neighbor", "Type", "MAC",
266 "Remote VTEP", VTY_NEWLINE);
267 vty_out(vty, "%*s %-6s %-17s %-21s%s",
268 -wctx->addr_width, buf2, "remote", buf1,
269 inet_ntoa (n->r_vtep_ip), VTY_NEWLINE);
270 wctx->count++;
271 }
272 }
273 else
274 {
275 vty_out(vty, "%*s %-6s %-17s %-21s%s",
276 -wctx->addr_width, buf2, "remote", buf1,
277 inet_ntoa (n->r_vtep_ip), VTY_NEWLINE);
278 wctx->count++;
279 }
280 }
281 }
282
283 /*
284 * Print neighbors for all VNI.
285 */
286 static void
287 zvni_print_neigh_hash_all_vni (struct hash_backet *backet, void *ctxt)
288 {
289 struct vty *vty;
290 zebra_vni_t *zvni;
291 u_int32_t num_neigh;
292 struct neigh_walk_ctx wctx;
293
294 vty = (struct vty *) ctxt;
295 zvni = (zebra_vni_t *) backet->data;
296 if (!zvni)
297 return;
298
299 num_neigh = hashcount(zvni->neigh_table);
300 vty_out(vty, "%sVNI %u #ARP (IPv4 and IPv6, local and remote) %u%s%s",
301 VTY_NEWLINE, zvni->vni, num_neigh, VTY_NEWLINE, VTY_NEWLINE);
302 if (!num_neigh)
303 return;
304
305 /* Since we have IPv6 addresses to deal with which can vary widely in
306 * size, we try to be a bit more elegant in display by first computing
307 * the maximum width.
308 */
309 memset (&wctx, 0, sizeof (struct neigh_walk_ctx));
310 wctx.zvni = zvni;
311 wctx.vty = vty;
312 wctx.addr_width = 15;
313 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
314
315 vty_out(vty, "%*s %-6s %-17s %-21s%s",
316 -wctx.addr_width, "IP", "Type", "MAC",
317 "Remote VTEP", VTY_NEWLINE);
318 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
319 }
320
321 /*
322 * Print a specific MAC entry.
323 */
324 static void
325 zvni_print_mac (zebra_mac_t *mac, void *ctxt)
326 {
327 struct vty *vty;
328 char buf1[20];
329
330 vty = (struct vty *) ctxt;
331 vty_out(vty, "MAC: %s%s",
332 prefix_mac2str (&mac->macaddr, buf1, sizeof (buf1)), VTY_NEWLINE);
333 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
334 {
335 struct zebra_ns *zns;
336 struct interface *ifp;
337 ifindex_t ifindex;
338
339 ifindex = mac->fwd_info.local.ifindex;
340 zns = zebra_ns_lookup (NS_DEFAULT);
341 ifp = if_lookup_by_index_per_ns (zns, ifindex);
342 if (!ifp) // unexpected
343 return;
344 vty_out(vty, " Intf: %s(%u)", ifp->name, ifindex);
345 if (mac->fwd_info.local.vid)
346 vty_out(vty, " VLAN: %u", mac->fwd_info.local.vid);
347 }
348 else
349 {
350 vty_out(vty, " Remote VTEP: %s",
351 inet_ntoa (mac->fwd_info.r_vtep_ip));
352 }
353 vty_out(vty, " ARP ref: %u", mac->neigh_refcnt);
354 vty_out(vty, "%s", VTY_NEWLINE);
355 }
356
357 /*
358 * Print MAC hash entry - called for display of all MACs.
359 */
360 static void
361 zvni_print_mac_hash (struct hash_backet *backet, void *ctxt)
362 {
363 struct vty *vty;
364 zebra_mac_t *mac;
365 char buf1[20];
366 struct mac_walk_ctx *wctx = ctxt;
367
368 vty = wctx->vty;
369 mac = (zebra_mac_t *) backet->data;
370 if (!mac)
371 return;
372
373 prefix_mac2str (&mac->macaddr, buf1, sizeof (buf1));
374 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL) &&
375 !(wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP))
376 {
377 struct zebra_ns *zns;
378 ifindex_t ifindex;
379 struct interface *ifp;
380 vlanid_t vid;
381
382 zns = zebra_ns_lookup (NS_DEFAULT);
383 ifindex = mac->fwd_info.local.ifindex;
384 ifp = if_lookup_by_index_per_ns (zns, ifindex);
385 if (!ifp) // unexpected
386 return;
387 vid = mac->fwd_info.local.vid;
388 vty_out(vty, "%-17s %-6s %-21s",
389 buf1, "local", ifp->name);
390 if (vid)
391 vty_out(vty, " %-5u", vid);
392 vty_out(vty, "%s", VTY_NEWLINE);
393 wctx->count++;
394 }
395 else
396 {
397 if (wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP)
398 {
399 if (IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip,
400 &wctx->r_vtep_ip))
401 {
402 if (wctx->count == 0)
403 {
404 vty_out(vty, "%sVNI %u%s%s",
405 VTY_NEWLINE, wctx->zvni->vni,VTY_NEWLINE, VTY_NEWLINE);
406 vty_out(vty, "%-17s %-6s %-21s %-5s%s",
407 "MAC", "Type", "Intf/Remote VTEP",
408 "VLAN", VTY_NEWLINE);
409 }
410 vty_out(vty, "%-17s %-6s %-21s%s",
411 buf1, "remote",
412 inet_ntoa (mac->fwd_info.r_vtep_ip),
413 VTY_NEWLINE);
414 wctx->count++;
415 }
416 }
417 else
418 {
419 vty_out(vty, "%-17s %-6s %-21s%s",
420 buf1, "remote",
421 inet_ntoa (mac->fwd_info.r_vtep_ip), VTY_NEWLINE);
422 wctx->count++;
423 }
424 }
425
426 }
427
428 /*
429 * Print MACs for all VNI.
430 */
431 static void
432 zvni_print_mac_hash_all_vni (struct hash_backet *backet, void *ctxt)
433 {
434 struct vty *vty;
435 zebra_vni_t *zvni;
436 u_int32_t num_macs;
437 struct mac_walk_ctx *wctx = ctxt;
438
439 vty = (struct vty *) wctx->vty;
440
441 zvni = (zebra_vni_t *) backet->data;
442 if (!zvni)
443 return;
444 wctx->zvni = zvni;
445
446 /*We are iterating over a new VNI, set the count to 0*/
447 wctx->count = 0;
448
449 num_macs = hashcount(zvni->mac_table);
450 if (!num_macs)
451 return;
452 if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP))
453 {
454 vty_out(vty, "%sVNI %u #MACs (local and remote) %u%s%s",
455 VTY_NEWLINE, zvni->vni, num_macs, VTY_NEWLINE, VTY_NEWLINE);
456 vty_out(vty, "%-17s %-6s %-21s %-5s%s",
457 "MAC", "Type", "Intf/Remote VTEP", "VLAN", VTY_NEWLINE);
458 }
459
460 hash_iterate(zvni->mac_table, zvni_print_mac_hash, wctx);
461 }
462
463 /*
464 * Print a specific VNI entry.
465 */
466 static void
467 zvni_print (zebra_vni_t *zvni, void *ctxt)
468 {
469 struct vty *vty;
470 zebra_vtep_t *zvtep;
471 u_int32_t num_macs;
472 u_int32_t num_neigh;
473
474 vty = (struct vty *) ctxt;
475
476 vty_out(vty, "VNI: %u%s", zvni->vni, VTY_NEWLINE);
477 if (!zvni->vxlan_if)
478 { // unexpected
479 vty_out(vty, " VxLAN interface: unknown%s", VTY_NEWLINE);
480 return;
481 }
482 vty_out(vty, " VxLAN interface: %s ifIndex: %u VTEP IP: %s%s",
483 zvni->vxlan_if->name, zvni->vxlan_if->ifindex,
484 inet_ntoa(zvni->local_vtep_ip), VTY_NEWLINE);
485
486 if (!zvni->vteps)
487 {
488 vty_out(vty, " No remote VTEPs known for this VNI%s", VTY_NEWLINE);
489 }
490 else
491 {
492 vty_out(vty, " Remote VTEPs for this VNI:%s", VTY_NEWLINE);
493 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next)
494 vty_out(vty, " %s%s",
495 inet_ntoa (zvtep->vtep_ip), VTY_NEWLINE);
496 }
497 num_macs = hashcount(zvni->mac_table);
498 vty_out(vty, " Number of MACs (local and remote) known for this VNI: %u%s",
499 num_macs, VTY_NEWLINE);
500 num_neigh = hashcount(zvni->neigh_table);
501 vty_out(vty, " Number of ARPs (IPv4 and IPv6, local and remote) "
502 "known for this VNI: %u%s", num_neigh, VTY_NEWLINE);
503 }
504
505 /*
506 * Print a VNI hash entry - called for display of all VNIs.
507 */
508 static void
509 zvni_print_hash (struct hash_backet *backet, void *ctxt)
510 {
511 struct vty *vty;
512 zebra_vni_t *zvni;
513 zebra_vtep_t *zvtep;
514 u_int32_t num_vteps = 0;
515 u_int32_t num_macs = 0;
516 u_int32_t num_neigh = 0;
517
518 vty = (struct vty *) ctxt;
519 zvni = (zebra_vni_t *) backet->data;
520 if (!zvni)
521 return;
522
523 zvtep = zvni->vteps;
524 while (zvtep)
525 {
526 num_vteps++;
527 zvtep = zvtep->next;
528 }
529
530 num_macs = hashcount(zvni->mac_table);
531 num_neigh = hashcount(zvni->neigh_table);
532 vty_out(vty, "%-10u %-21s %-15s %-8u %-8u %-15u%s",
533 zvni->vni,
534 zvni->vxlan_if ? zvni->vxlan_if->name : "unknown",
535 inet_ntoa(zvni->local_vtep_ip),
536 num_macs, num_neigh, num_vteps, VTY_NEWLINE);
537 }
538
539 /*
540 * Inform BGP about local MACIP.
541 */
542 static int
543 zvni_macip_send_msg_to_client (struct zebra_vrf *zvrf, vni_t vni,
544 struct ethaddr *macaddr,
545 struct ipaddr *ip, u_char sticky,
546 u_int16_t cmd)
547 {
548 struct zserv *client;
549 struct stream *s;
550 int ipa_len;
551 char buf[ETHER_ADDR_STRLEN];
552 char buf2[INET6_ADDRSTRLEN];
553
554 client = zebra_find_client (ZEBRA_ROUTE_BGP);
555 /* BGP may not be running. */
556 if (!client)
557 return 0;
558
559 s = client->obuf;
560 stream_reset (s);
561
562 zserv_create_header (s, cmd, zvrf_id (zvrf));
563 stream_putl (s, vni);
564 stream_put (s, macaddr->octet, ETHER_ADDR_LEN);
565 if (ip)
566 {
567 ipa_len = 0;
568 if (IS_IPADDR_V4(ip))
569 ipa_len = IPV4_MAX_BYTELEN;
570 else if (IS_IPADDR_V6(ip))
571 ipa_len = IPV6_MAX_BYTELEN;
572
573 stream_putl (s, ipa_len); /* IP address length */
574 if (ipa_len)
575 stream_put (s, &ip->ip.addr, ipa_len); /* IP address */
576 }
577 else
578 stream_putl (s, 0); /* Just MAC. */
579
580 stream_putc (s, sticky); /* Sticky MAC? */
581
582 /* Write packet size. */
583 stream_putw_at (s, 0, stream_get_endp (s));
584
585 if (IS_ZEBRA_DEBUG_VXLAN)
586 zlog_debug ("%u:Send MACIP %s %sMAC %s IP %s VNI %u to %s",
587 zvrf_id (zvrf), (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del",
588 sticky ? "sticky " : "",
589 prefix_mac2str (macaddr, buf, sizeof (buf)),
590 ipaddr2str (ip, buf2, sizeof(buf2)), vni,
591 zebra_route_string (client->proto));
592
593 if (cmd == ZEBRA_MACIP_ADD)
594 client->macipadd_cnt++;
595 else
596 client->macipdel_cnt++;
597
598 return zebra_server_send_message(client);
599 }
600
601 /*
602 * Make hash key for neighbors.
603 */
604 static unsigned int
605 neigh_hash_keymake (void *p)
606 {
607 zebra_neigh_t *n = p;
608 struct ipaddr *ip = &n->ip;
609
610 if (IS_IPADDR_V4(ip))
611 return jhash_1word (ip->ipaddr_v4.s_addr, 0);
612
613 return jhash2 (ip->ipaddr_v6.s6_addr32,
614 ZEBRA_NUM_OF(ip->ipaddr_v6.s6_addr32), 0);
615 }
616
617 /*
618 * Compare two neighbor hash structures.
619 */
620 static int
621 neigh_cmp (const void *p1, const void *p2)
622 {
623 const zebra_neigh_t *n1 = p1;
624 const zebra_neigh_t *n2 = p2;
625
626 if (n1 == NULL && n2 == NULL)
627 return 1;
628
629 if (n1 == NULL || n2 == NULL)
630 return 0;
631
632 return (memcmp(&n1->ip, &n2->ip, sizeof (struct ipaddr)) == 0);
633 }
634
635 /*
636 * Callback to allocate neighbor hash entry.
637 */
638 static void *
639 zvni_neigh_alloc (void *p)
640 {
641 const zebra_neigh_t *tmp_n = p;
642 zebra_neigh_t *n;
643
644 n = XCALLOC (MTYPE_NEIGH, sizeof(zebra_neigh_t));
645 *n = *tmp_n;
646
647 return ((void *)n);
648 }
649
650 /*
651 * Add neighbor entry.
652 */
653 static zebra_neigh_t *
654 zvni_neigh_add (zebra_vni_t *zvni, struct ipaddr *ip)
655 {
656 zebra_neigh_t tmp_n;
657 zebra_neigh_t *n = NULL;
658
659 memset (&tmp_n, 0, sizeof (zebra_neigh_t));
660 memcpy (&tmp_n.ip, ip, sizeof (struct ipaddr));
661 n = hash_get (zvni->neigh_table, &tmp_n, zvni_neigh_alloc);
662 assert (n);
663
664 return n;
665 }
666
667 /*
668 * Delete neighbor entry.
669 */
670 static int
671 zvni_neigh_del (zebra_vni_t *zvni, zebra_neigh_t *n)
672 {
673 zebra_neigh_t *tmp_n;
674
675 /* Free the VNI hash entry and allocated memory. */
676 tmp_n = hash_release (zvni->neigh_table, n);
677 if (tmp_n)
678 XFREE(MTYPE_NEIGH, tmp_n);
679
680 return 0;
681 }
682
683 /*
684 * Free neighbor hash entry (callback)
685 */
686 static int
687 zvni_neigh_del_hash_entry (struct hash_backet *backet, void *arg)
688 {
689 struct neigh_walk_ctx *wctx = arg;
690 zebra_neigh_t *n = backet->data;
691
692 if (((wctx->flags & DEL_LOCAL_NEIGH) && (n->flags & ZEBRA_NEIGH_LOCAL)) ||
693 ((wctx->flags & DEL_REMOTE_NEIGH) && (n->flags & ZEBRA_NEIGH_REMOTE)) ||
694 ((wctx->flags & DEL_REMOTE_NEIGH_FROM_VTEP) &&
695 (n->flags & ZEBRA_NEIGH_REMOTE) &&
696 IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip)
697 ))
698 {
699 if (wctx->upd_client && (n->flags & ZEBRA_NEIGH_LOCAL))
700 zvni_neigh_send_del_to_client (wctx->zvrf, wctx->zvni->vni, &n->ip,
701 &n->emac);
702
703 if (wctx->uninstall)
704 zvni_neigh_uninstall (wctx->zvni, n);
705
706 return zvni_neigh_del (wctx->zvni, n);
707 }
708
709 return 0;
710 }
711
712 /*
713 * Delete all neighbor entries from specific VTEP for a particular VNI.
714 */
715 static void
716 zvni_neigh_del_from_vtep (zebra_vni_t *zvni, int uninstall,
717 struct in_addr *r_vtep_ip)
718 {
719 struct neigh_walk_ctx wctx;
720
721 if (!zvni->neigh_table)
722 return;
723
724 memset (&wctx, 0, sizeof (struct neigh_walk_ctx));
725 wctx.zvni = zvni;
726 wctx.uninstall = uninstall;
727 wctx.flags = DEL_REMOTE_NEIGH_FROM_VTEP;
728 wctx.r_vtep_ip = *r_vtep_ip;
729
730 hash_iterate (zvni->neigh_table,
731 (void (*) (struct hash_backet *, void *))
732 zvni_neigh_del_hash_entry, &wctx);
733 }
734
735 /*
736 * Delete all neighbor entries for this VNI.
737 */
738 static void
739 zvni_neigh_del_all (struct zebra_vrf *zvrf, zebra_vni_t *zvni,
740 int uninstall, int upd_client, u_int32_t flags)
741 {
742 struct neigh_walk_ctx wctx;
743
744 if (!zvni->neigh_table)
745 return;
746
747 memset (&wctx, 0, sizeof (struct neigh_walk_ctx));
748 wctx.zvni = zvni;
749 wctx.zvrf = zvrf;
750 wctx.uninstall = uninstall;
751 wctx.upd_client = upd_client;
752 wctx.flags = flags;
753
754 hash_iterate (zvni->neigh_table,
755 (void (*) (struct hash_backet *, void *))
756 zvni_neigh_del_hash_entry, &wctx);
757 }
758
759 /*
760 * Look up neighbor hash entry.
761 */
762 static zebra_neigh_t *
763 zvni_neigh_lookup (zebra_vni_t *zvni, struct ipaddr *ip)
764 {
765 zebra_neigh_t tmp;
766 zebra_neigh_t *n;
767
768 memset (&tmp, 0, sizeof(tmp));
769 memcpy (&tmp.ip, ip, sizeof (struct ipaddr));
770 n = hash_lookup (zvni->neigh_table, &tmp);
771
772 return n;
773 }
774
775 /*
776 * Inform BGP about local neighbor addition.
777 */
778 static int
779 zvni_neigh_send_add_to_client (struct zebra_vrf *zvrf, vni_t vni,
780 struct ipaddr *ip, struct ethaddr *macaddr)
781 {
782 return zvni_macip_send_msg_to_client (zvrf, vni, macaddr, ip, 0,
783 ZEBRA_MACIP_ADD);
784 }
785
786 /*
787 * Inform BGP about local neighbor deletion.
788 */
789 static int
790 zvni_neigh_send_del_to_client (struct zebra_vrf *zvrf, vni_t vni,
791 struct ipaddr *ip, struct ethaddr *macaddr)
792 {
793 return zvni_macip_send_msg_to_client (zvrf, vni, macaddr, ip, 0,
794 ZEBRA_MACIP_DEL);
795 }
796
797 /*
798 * Install remote neighbor into the kernel.
799 */
800 static int
801 zvni_neigh_install (zebra_vni_t *zvni, zebra_neigh_t *n)
802 {
803 struct zebra_vrf *zvrf;
804 struct zebra_if *zif;
805 struct zebra_l2info_vxlan *vxl;
806 struct interface *vlan_if;
807
808 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
809 return 0;
810
811 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
812 assert(zvrf);
813 zif = zvni->vxlan_if->info;
814 if (!zif)
815 return -1;
816 vxl = &zif->l2info.vxl;
817
818 vlan_if = zvni_map_to_svi (zvrf, vxl->access_vlan,
819 zif->brslave_info.br_if);
820 if (!vlan_if)
821 return -1;
822
823 return kernel_add_neigh (vlan_if, &n->ip, &n->emac);
824 }
825
826 /*
827 * Uninstall remote neighbor from the kernel.
828 */
829 static int
830 zvni_neigh_uninstall (zebra_vni_t *zvni, zebra_neigh_t *n)
831 {
832 struct zebra_vrf *zvrf;
833 struct zebra_if *zif;
834 struct zebra_l2info_vxlan *vxl;
835 struct interface *vlan_if;
836
837 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
838 return 0;
839
840 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
841 assert(zvrf);
842 if (!zvni->vxlan_if)
843 {
844 zlog_err ("VNI %u hash %p couldn't be uninstalled - no intf",
845 zvni->vni, zvni);
846 return -1;
847 }
848
849 zif = zvni->vxlan_if->info;
850 if (!zif)
851 return -1;
852 vxl = &zif->l2info.vxl;
853 vlan_if = zvni_map_to_svi (zvrf, vxl->access_vlan,
854 zif->brslave_info.br_if);
855 if (!vlan_if)
856 return -1;
857
858 return kernel_del_neigh (vlan_if, &n->ip);
859 }
860
861 /*
862 * Install neighbor hash entry - called upon access VLAN change.
863 */
864 static void
865 zvni_install_neigh_hash (struct hash_backet *backet, void *ctxt)
866 {
867 zebra_neigh_t *n;
868 struct neigh_walk_ctx *wctx = ctxt;
869
870 n = (zebra_neigh_t *) backet->data;
871 if (!n)
872 return;
873
874 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
875 zvni_neigh_install (wctx->zvni, n);
876 }
877
878 /*
879 * Make hash key for MAC.
880 */
881 static unsigned int
882 mac_hash_keymake (void *p)
883 {
884 zebra_mac_t *pmac = p;
885 char *pnt = (char *) pmac->macaddr.octet;
886 unsigned int key = 0;
887 int c = 0;
888
889 key += pnt[c];
890 key += pnt[c + 1];
891 key += pnt[c + 2];
892 key += pnt[c + 3];
893 key += pnt[c + 4];
894 key += pnt[c + 5];
895
896 return (key);
897 }
898
899 /*
900 * Compare two MAC addresses.
901 */
902 static int
903 mac_cmp (const void *p1, const void *p2)
904 {
905 const zebra_mac_t *pmac1 = p1;
906 const zebra_mac_t *pmac2 = p2;
907
908 if (pmac1 == NULL && pmac2 == NULL)
909 return 1;
910
911 if (pmac1 == NULL || pmac2 == NULL)
912 return 0;
913
914 return(memcmp(pmac1->macaddr.octet, pmac2->macaddr.octet, ETHER_ADDR_LEN) == 0);
915 }
916
917 /*
918 * Callback to allocate MAC hash entry.
919 */
920 static void *
921 zvni_mac_alloc (void *p)
922 {
923 const zebra_mac_t *tmp_mac = p;
924 zebra_mac_t *mac;
925
926 mac = XCALLOC (MTYPE_MAC, sizeof(zebra_mac_t));
927 *mac = *tmp_mac;
928
929 return ((void *)mac);
930 }
931
932 /*
933 * Add MAC entry.
934 */
935 static zebra_mac_t *
936 zvni_mac_add (zebra_vni_t *zvni, struct ethaddr *macaddr)
937 {
938 zebra_mac_t tmp_mac;
939 zebra_mac_t *mac = NULL;
940
941 memset (&tmp_mac, 0, sizeof (zebra_mac_t));
942 memcpy(&tmp_mac.macaddr, macaddr, ETHER_ADDR_LEN);
943 mac = hash_get (zvni->mac_table, &tmp_mac, zvni_mac_alloc);
944 assert (mac);
945
946 return mac;
947 }
948
949 /*
950 * Delete MAC entry.
951 */
952 static int
953 zvni_mac_del (zebra_vni_t *zvni, zebra_mac_t *mac)
954 {
955 zebra_mac_t *tmp_mac;
956
957 /* Free the VNI hash entry and allocated memory. */
958 tmp_mac = hash_release (zvni->mac_table, mac);
959 if (tmp_mac)
960 XFREE(MTYPE_MAC, tmp_mac);
961
962 return 0;
963 }
964
965 /*
966 * Free MAC hash entry (callback)
967 */
968 static int
969 zvni_mac_del_hash_entry (struct hash_backet *backet, void *arg)
970 {
971 struct mac_walk_ctx *wctx = arg;
972 zebra_mac_t *mac = backet->data;
973 u_char sticky = 0;
974
975 if (((wctx->flags & DEL_LOCAL_MAC) && (mac->flags & ZEBRA_MAC_LOCAL)) ||
976 ((wctx->flags & DEL_REMOTE_MAC) && (mac->flags & ZEBRA_MAC_REMOTE)) ||
977 ((wctx->flags & DEL_REMOTE_MAC_FROM_VTEP) &&
978 (mac->flags & ZEBRA_MAC_REMOTE) &&
979 IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &wctx->r_vtep_ip)
980 ))
981 {
982 if (wctx->upd_client && (mac->flags & ZEBRA_MAC_LOCAL))
983 {
984 sticky = CHECK_FLAG (mac->flags, ZEBRA_MAC_STICKY) ? 1: 0;
985 zvni_mac_send_del_to_client (wctx->zvrf, wctx->zvni->vni,
986 &mac->macaddr, sticky);
987 }
988
989 if (wctx->uninstall)
990 zvni_mac_uninstall (wctx->zvni, mac, 0);
991
992 return zvni_mac_del (wctx->zvni, mac);
993 }
994
995 return 0;
996 }
997
998 /*
999 * Delete all MAC entries from specific VTEP for a particular VNI.
1000 */
1001 static void
1002 zvni_mac_del_from_vtep (zebra_vni_t *zvni, int uninstall,
1003 struct in_addr *r_vtep_ip)
1004 {
1005 struct mac_walk_ctx wctx;
1006
1007 if (!zvni->mac_table)
1008 return;
1009
1010 memset (&wctx, 0, sizeof (struct mac_walk_ctx));
1011 wctx.zvni = zvni;
1012 wctx.uninstall = uninstall;
1013 wctx.flags = DEL_REMOTE_MAC_FROM_VTEP;
1014 wctx.r_vtep_ip = *r_vtep_ip;
1015
1016 hash_iterate (zvni->mac_table,
1017 (void (*) (struct hash_backet *, void *))
1018 zvni_mac_del_hash_entry, &wctx);
1019 }
1020
1021 /*
1022 * Delete all MAC entries for this VNI.
1023 */
1024 static void
1025 zvni_mac_del_all (struct zebra_vrf *zvrf, zebra_vni_t *zvni,
1026 int uninstall, int upd_client, u_int32_t flags)
1027 {
1028 struct mac_walk_ctx wctx;
1029
1030 if (!zvni->mac_table)
1031 return;
1032
1033 memset (&wctx, 0, sizeof (struct mac_walk_ctx));
1034 wctx.zvni = zvni;
1035 wctx.zvrf = zvrf;
1036 wctx.uninstall = uninstall;
1037 wctx.upd_client = upd_client;
1038 wctx.flags = flags;
1039
1040 hash_iterate (zvni->mac_table,
1041 (void (*) (struct hash_backet *, void *))
1042 zvni_mac_del_hash_entry, &wctx);
1043 }
1044
1045 /*
1046 * Look up MAC hash entry.
1047 */
1048 static zebra_mac_t *
1049 zvni_mac_lookup (zebra_vni_t *zvni, struct ethaddr *mac)
1050 {
1051 zebra_mac_t tmp;
1052 zebra_mac_t *pmac;
1053
1054 memset(&tmp, 0, sizeof(tmp));
1055 memcpy(&tmp.macaddr, mac, ETHER_ADDR_LEN);
1056 pmac = hash_lookup (zvni->mac_table, &tmp);
1057
1058 return pmac;
1059 }
1060
1061 /*
1062 * Inform BGP about local MAC addition.
1063 */
1064 static int
1065 zvni_mac_send_add_to_client (struct zebra_vrf *zvrf, vni_t vni,
1066 struct ethaddr *macaddr, u_char sticky)
1067 {
1068 return zvni_macip_send_msg_to_client (zvrf, vni, macaddr, NULL,
1069 sticky, ZEBRA_MACIP_ADD);
1070 }
1071
1072 /*
1073 * Inform BGP about local MAC deletion.
1074 */
1075 static int
1076 zvni_mac_send_del_to_client (struct zebra_vrf *zvrf, vni_t vni,
1077 struct ethaddr *macaddr, u_char sticky)
1078 {
1079 return zvni_macip_send_msg_to_client (zvrf, vni, macaddr, NULL,
1080 sticky, ZEBRA_MACIP_DEL);
1081 }
1082
1083 /*
1084 * Map port or (port, VLAN) to a VNI. This is invoked upon getting MAC
1085 * notifications, to see if there are of interest.
1086 * TODO: Need to make this as a hash table.
1087 */
1088 static zebra_vni_t *
1089 zvni_map_vlan (struct interface *ifp, struct interface *br_if, vlanid_t vid)
1090 {
1091 struct zebra_vrf *zvrf;
1092 struct listnode *node;
1093 struct interface *tmp_if;
1094 struct zebra_if *zif;
1095 struct zebra_l2info_bridge *br;
1096 struct zebra_l2info_vxlan *vxl;
1097 u_char bridge_vlan_aware;
1098 zebra_vni_t *zvni;
1099
1100 /* Locate VRF corresponding to interface. */
1101 zvrf = vrf_info_lookup(ifp->vrf_id);
1102 assert(zvrf);
1103
1104 /* Determine if bridge is VLAN-aware or not */
1105 zif = br_if->info;
1106 assert (zif);
1107 br = &zif->l2info.br;
1108 bridge_vlan_aware = br->vlan_aware;
1109
1110 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
1111 /* TODO: Optimize with a hash. */
1112 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (zvrf_id (zvrf)), node, tmp_if))
1113 {
1114 zif = tmp_if->info;
1115 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
1116 continue;
1117 if (!if_is_operative (tmp_if))
1118 continue;
1119 vxl = &zif->l2info.vxl;
1120
1121 if (zif->brslave_info.br_if != br_if)
1122 continue;
1123
1124 if (!bridge_vlan_aware)
1125 break;
1126
1127 if (vxl->access_vlan == vid)
1128 break;
1129 }
1130
1131 if (!tmp_if)
1132 return NULL;
1133
1134 zvni = zvni_lookup (zvrf, vxl->vni);
1135 return zvni;
1136 }
1137
1138 /*
1139 * Map SVI and associated bridge to a VNI. This is invoked upon getting
1140 * neighbor notifications, to see if they are of interest.
1141 * TODO: Need to make this as a hash table.
1142 */
1143 static zebra_vni_t *
1144 zvni_map_svi (struct interface *ifp, struct interface *br_if)
1145 {
1146 struct zebra_vrf *zvrf;
1147 struct listnode *node;
1148 struct interface *tmp_if;
1149 struct zebra_if *zif;
1150 struct zebra_l2info_bridge *br;
1151 struct zebra_l2info_vxlan *vxl;
1152 u_char bridge_vlan_aware;
1153 vlanid_t vid = 0;
1154 zebra_vni_t *zvni;
1155
1156 /* Make sure the linked interface is a bridge. */
1157 if (!IS_ZEBRA_IF_BRIDGE (br_if))
1158 return NULL;
1159
1160 /* Locate VRF corresponding to interface. */
1161 zvrf = vrf_info_lookup(ifp->vrf_id);
1162 assert(zvrf);
1163
1164 /* Determine if bridge is VLAN-aware or not */
1165 zif = br_if->info;
1166 assert (zif);
1167 br = &zif->l2info.br;
1168 bridge_vlan_aware = br->vlan_aware;
1169 if (bridge_vlan_aware)
1170 {
1171 struct zebra_l2info_vlan *vl;
1172
1173 if (!IS_ZEBRA_IF_VLAN(ifp))
1174 return NULL;
1175
1176 zif = ifp->info;
1177 assert (zif);
1178 vl = &zif->l2info.vl;
1179 vid = vl->vid;
1180 }
1181
1182 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
1183 /* TODO: Optimize with a hash. */
1184 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (zvrf_id (zvrf)), node, tmp_if))
1185 {
1186 zif = tmp_if->info;
1187 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
1188 continue;
1189 if (!if_is_operative (tmp_if))
1190 continue;
1191 vxl = &zif->l2info.vxl;
1192
1193 if (zif->brslave_info.br_if != br_if)
1194 continue;
1195
1196 if (!bridge_vlan_aware)
1197 break;
1198
1199 if (vxl->access_vlan == vid)
1200 break;
1201 }
1202
1203 if (!tmp_if)
1204 return NULL;
1205
1206 zvni = zvni_lookup (zvrf, vxl->vni);
1207 return zvni;
1208 }
1209
1210 /* Map to SVI on bridge corresponding to specified VLAN. This can be one
1211 * of two cases:
1212 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN interface
1213 * linked to the bridge
1214 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge inteface
1215 * itself
1216 */
1217 static struct interface *
1218 zvni_map_to_svi (struct zebra_vrf *zvrf, vlanid_t vid,
1219 struct interface *br_if)
1220 {
1221 struct listnode *node;
1222 struct interface *tmp_if;
1223 struct zebra_if *zif;
1224 struct zebra_l2info_bridge *br;
1225 struct zebra_l2info_vlan *vl;
1226 u_char bridge_vlan_aware;
1227
1228 /* Determine if bridge is VLAN-aware or not */
1229 zif = br_if->info;
1230 assert (zif);
1231 br = &zif->l2info.br;
1232 bridge_vlan_aware = br->vlan_aware;
1233
1234 /* Check oper status of the SVI. */
1235 if (!bridge_vlan_aware)
1236 return if_is_operative (br_if) ? br_if : NULL;
1237
1238 /* Identify corresponding VLAN interface. */
1239 /* TODO: Optimize with a hash. */
1240 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (zvrf_id (zvrf)), node, tmp_if))
1241 {
1242 /* Check oper status of the SVI. */
1243 if (!if_is_operative (tmp_if))
1244 continue;
1245 zif = tmp_if->info;
1246 if (!zif ||
1247 zif->zif_type != ZEBRA_IF_VLAN ||
1248 zif->link != br_if)
1249 continue;
1250 vl = (struct zebra_l2info_vlan *)&zif->l2info.vl;
1251
1252 if (vl->vid == vid)
1253 break;
1254 }
1255
1256 return tmp_if;
1257 }
1258
1259 /*
1260 * Install remote MAC into the kernel.
1261 */
1262 static int
1263 zvni_mac_install (zebra_vni_t *zvni, zebra_mac_t *mac)
1264 {
1265 struct zebra_if *zif;
1266 struct zebra_l2info_vxlan *vxl;
1267 u_char sticky;
1268
1269 if (!(mac->flags & ZEBRA_MAC_REMOTE))
1270 return 0;
1271
1272 zif = zvni->vxlan_if->info;
1273 if (!zif)
1274 return -1;
1275 vxl = &zif->l2info.vxl;
1276
1277 sticky = CHECK_FLAG (mac->flags, ZEBRA_MAC_STICKY) ? 1: 0;
1278
1279 return kernel_add_mac (zvni->vxlan_if, vxl->access_vlan,
1280 &mac->macaddr, mac->fwd_info.r_vtep_ip, sticky);
1281 }
1282
1283 /*
1284 * Uninstall remote MAC from the kernel. In the scenario where the MAC
1285 * moves to remote, we have to uninstall any existing local entry first.
1286 */
1287 static int
1288 zvni_mac_uninstall (zebra_vni_t *zvni, zebra_mac_t *mac, int local)
1289 {
1290 struct zebra_if *zif;
1291 struct zebra_l2info_vxlan *vxl;
1292 struct in_addr vtep_ip = { .s_addr = 0 };
1293 struct zebra_ns *zns;
1294 struct interface *ifp;
1295
1296 if (!local && !(mac->flags & ZEBRA_MAC_REMOTE))
1297 return 0;
1298
1299 if (!zvni->vxlan_if)
1300 {
1301 zlog_err ("VNI %u hash %p couldn't be uninstalled - no intf",
1302 zvni->vni, zvni);
1303 return -1;
1304 }
1305
1306 zif = zvni->vxlan_if->info;
1307 if (!zif)
1308 return -1;
1309 vxl = &zif->l2info.vxl;
1310
1311 if (local)
1312 {
1313 zns = zebra_ns_lookup (NS_DEFAULT);
1314 ifp = if_lookup_by_index_per_ns (zns, mac->fwd_info.local.ifindex);
1315 if (!ifp) // unexpected
1316 return -1;
1317 }
1318 else
1319 {
1320 ifp = zvni->vxlan_if;
1321 vtep_ip = mac->fwd_info.r_vtep_ip;
1322 }
1323
1324 return kernel_del_mac (ifp, vxl->access_vlan,
1325 &mac->macaddr, vtep_ip, local);
1326 }
1327
1328 /*
1329 * Install MAC hash entry - called upon access VLAN change.
1330 */
1331 static void
1332 zvni_install_mac_hash (struct hash_backet *backet, void *ctxt)
1333 {
1334 zebra_mac_t *mac;
1335 struct mac_walk_ctx *wctx = ctxt;
1336
1337 mac = (zebra_mac_t *) backet->data;
1338 if (!mac)
1339 return;
1340
1341 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
1342 zvni_mac_install (wctx->zvni, mac);
1343 }
1344
1345 /*
1346 * Decrement neighbor refcount of MAC; uninstall and free it if
1347 * appropriate.
1348 */
1349 static void
1350 zvni_deref_ip2mac (zebra_vni_t *zvni, zebra_mac_t *mac, int uninstall)
1351 {
1352 if (mac->neigh_refcnt)
1353 mac->neigh_refcnt--;
1354
1355 if (!CHECK_FLAG (mac->flags, ZEBRA_MAC_AUTO) ||
1356 mac->neigh_refcnt > 0)
1357 return;
1358
1359 if (uninstall)
1360 zvni_mac_uninstall (zvni, mac, 0);
1361
1362 zvni_mac_del (zvni, mac);
1363 }
1364
1365 /*
1366 * Read and populate local MACs and neighbors corresponding to this VNI.
1367 */
1368 static void
1369 zvni_read_mac_neigh (struct zebra_vrf *zvrf, zebra_vni_t *zvni,
1370 struct interface *ifp)
1371 {
1372 struct zebra_if *zif;
1373 struct interface *vlan_if;
1374 struct zebra_l2info_vxlan *vxl;
1375
1376 zif = ifp->info;
1377 vxl = &zif->l2info.vxl;
1378
1379 if (IS_ZEBRA_DEBUG_VXLAN)
1380 zlog_debug ("%u:Reading MAC FDB and Neighbors for intf %s(%u) VNI %u master %u",
1381 ifp->vrf_id, ifp->name, ifp->ifindex, zvni->vni,
1382 zif->brslave_info.bridge_ifindex);
1383
1384 macfdb_read_for_bridge (zvrf->zns, ifp, zif->brslave_info.br_if);
1385 vlan_if = zvni_map_to_svi (zvrf, vxl->access_vlan,
1386 zif->brslave_info.br_if);
1387 if (vlan_if)
1388 neigh_read_for_vlan (zvrf->zns, vlan_if);
1389 }
1390
1391 /*
1392 * Hash function for VNI.
1393 */
1394 static unsigned int
1395 vni_hash_keymake (void *p)
1396 {
1397 const zebra_vni_t *zvni = p;
1398
1399 return (jhash_1word(zvni->vni, 0));
1400 }
1401
1402 /*
1403 * Compare 2 VNI hash entries.
1404 */
1405 static int
1406 vni_hash_cmp (const void *p1, const void *p2)
1407 {
1408 const zebra_vni_t *zvni1 = p1;
1409 const zebra_vni_t *zvni2 = p2;
1410
1411 return (zvni1->vni == zvni2->vni);
1412 }
1413
1414 /*
1415 * Callback to allocate VNI hash entry.
1416 */
1417 static void *
1418 zvni_alloc (void *p)
1419 {
1420 const zebra_vni_t *tmp_vni = p;
1421 zebra_vni_t *zvni;
1422
1423 zvni = XCALLOC (MTYPE_ZVNI, sizeof(zebra_vni_t));
1424 zvni->vni = tmp_vni->vni;
1425 return ((void *)zvni);
1426 }
1427
1428 /*
1429 * Look up VNI hash entry.
1430 */
1431 static zebra_vni_t *
1432 zvni_lookup (struct zebra_vrf *zvrf, vni_t vni)
1433 {
1434 zebra_vni_t tmp_vni;
1435 zebra_vni_t *zvni = NULL;
1436
1437 memset (&tmp_vni, 0, sizeof (zebra_vni_t));
1438 tmp_vni.vni = vni;
1439 zvni = hash_lookup (zvrf->vni_table, &tmp_vni);
1440
1441 return zvni;
1442 }
1443
1444 /*
1445 * Add VNI hash entry.
1446 */
1447 static zebra_vni_t *
1448 zvni_add (struct zebra_vrf *zvrf, vni_t vni)
1449 {
1450 zebra_vni_t tmp_zvni;
1451 zebra_vni_t *zvni = NULL;
1452
1453 memset (&tmp_zvni, 0, sizeof (zebra_vni_t));
1454 tmp_zvni.vni = vni;
1455 zvni = hash_get (zvrf->vni_table, &tmp_zvni, zvni_alloc);
1456 assert (zvni);
1457
1458 /* Create hash table for MAC */
1459 zvni->mac_table = hash_create(mac_hash_keymake,
1460 mac_cmp,
1461 "Zebra VNI MAC Table");
1462
1463 /* Create hash table for neighbors */
1464 zvni->neigh_table = hash_create(neigh_hash_keymake,
1465 neigh_cmp,
1466 "Zebra VNI Neighbor Table");
1467
1468 return zvni;
1469 }
1470
1471 /*
1472 * Delete VNI hash entry.
1473 */
1474 static int
1475 zvni_del (struct zebra_vrf *zvrf, zebra_vni_t *zvni)
1476 {
1477 zebra_vni_t *tmp_zvni;
1478
1479 zvni->vxlan_if = NULL;
1480
1481 /* Free the neighbor hash table. */
1482 hash_free(zvni->neigh_table);
1483 zvni->neigh_table = NULL;
1484
1485 /* Free the MAC hash table. */
1486 hash_free(zvni->mac_table);
1487 zvni->mac_table = NULL;
1488
1489 /* Free the VNI hash entry and allocated memory. */
1490 tmp_zvni = hash_release (zvrf->vni_table, zvni);
1491 if (tmp_zvni)
1492 XFREE(MTYPE_ZVNI, tmp_zvni);
1493
1494 return 0;
1495 }
1496
1497 /*
1498 * Inform BGP about local VNI addition.
1499 */
1500 static int
1501 zvni_send_add_to_client (struct zebra_vrf *zvrf,
1502 zebra_vni_t *zvni)
1503 {
1504 struct zserv *client;
1505 struct stream *s;
1506
1507 client = zebra_find_client (ZEBRA_ROUTE_BGP);
1508 /* BGP may not be running. */
1509 if (!client)
1510 return 0;
1511
1512 s = client->obuf;
1513 stream_reset (s);
1514
1515 zserv_create_header (s, ZEBRA_VNI_ADD, zvrf_id (zvrf));
1516 stream_putl (s, zvni->vni);
1517 stream_put_in_addr (s, &zvni->local_vtep_ip);
1518
1519 /* Write packet size. */
1520 stream_putw_at (s, 0, stream_get_endp (s));
1521
1522 if (IS_ZEBRA_DEBUG_VXLAN)
1523 zlog_debug ("%u:Send VNI_ADD %u %s to %s",
1524 zvrf_id (zvrf), zvni->vni,
1525 inet_ntoa(zvni->local_vtep_ip),
1526 zebra_route_string (client->proto));
1527
1528 client->vniadd_cnt++;
1529 return zebra_server_send_message(client);
1530 }
1531
1532 /*
1533 * Inform BGP about local VNI deletion.
1534 */
1535 static int
1536 zvni_send_del_to_client (struct zebra_vrf *zvrf, vni_t vni)
1537 {
1538 struct zserv *client;
1539 struct stream *s;
1540
1541 client = zebra_find_client (ZEBRA_ROUTE_BGP);
1542 /* BGP may not be running. */
1543 if (!client)
1544 return 0;
1545
1546 s = client->obuf;
1547 stream_reset (s);
1548
1549 zserv_create_header (s, ZEBRA_VNI_DEL, zvrf_id (zvrf));
1550 stream_putl (s, vni);
1551
1552 /* Write packet size. */
1553 stream_putw_at (s, 0, stream_get_endp (s));
1554
1555 if (IS_ZEBRA_DEBUG_VXLAN)
1556 zlog_debug ("%u:Send VNI_DEL %u to %s", zvrf_id (zvrf), vni,
1557 zebra_route_string (client->proto));
1558
1559 client->vnidel_cnt++;
1560 return zebra_server_send_message(client);
1561 }
1562
1563 /*
1564 * Build the VNI hash table by going over the VxLAN interfaces. This
1565 * is called when EVPN (advertise-all-vni) is enabled.
1566 */
1567 static void
1568 zvni_build_hash_table (struct zebra_vrf *zvrf)
1569 {
1570 struct listnode *node;
1571 struct interface *ifp;
1572
1573 /* Walk VxLAN interfaces and create VNI hash. */
1574 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (zvrf_id (zvrf)), node, ifp))
1575 {
1576 struct zebra_if *zif;
1577 struct zebra_l2info_vxlan *vxl;
1578 zebra_vni_t *zvni;
1579 vni_t vni;
1580
1581 zif = ifp->info;
1582 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
1583 continue;
1584 vxl = &zif->l2info.vxl;
1585
1586 vni = vxl->vni;
1587
1588 if (IS_ZEBRA_DEBUG_VXLAN)
1589 zlog_debug ("%u:Create VNI hash for intf %s(%u) VNI %u local IP %s",
1590 zvrf_id (zvrf), ifp->name, ifp->ifindex, vni,
1591 inet_ntoa (vxl->vtep_ip));
1592
1593 /* VNI hash entry is not expected to exist. */
1594 zvni = zvni_lookup (zvrf, vni);
1595 if (zvni)
1596 {
1597 zlog_err ("VNI hash already present for VRF %d IF %s(%u) VNI %u",
1598 zvrf_id (zvrf), ifp->name, ifp->ifindex, vni);
1599 continue;
1600 }
1601
1602 zvni = zvni_add (zvrf, vni);
1603 if (!zvni)
1604 {
1605 zlog_err ("Failed to add VNI hash, VRF %d IF %s(%u) VNI %u",
1606 zvrf_id (zvrf), ifp->name, ifp->ifindex, vni);
1607 return;
1608 }
1609
1610 zvni->local_vtep_ip = vxl->vtep_ip;
1611 zvni->vxlan_if = ifp;
1612
1613 /* Inform BGP if interface is up and mapped to bridge. */
1614 if (if_is_operative (ifp) &&
1615 zif->brslave_info.br_if)
1616 zvni_send_add_to_client (zvrf, zvni);
1617 }
1618 }
1619
1620 /*
1621 * See if remote VTEP matches with prefix.
1622 */
1623 static int
1624 zvni_vtep_match (struct in_addr *vtep_ip, zebra_vtep_t *zvtep)
1625 {
1626 return (IPV4_ADDR_SAME (vtep_ip, &zvtep->vtep_ip));
1627 }
1628
1629 /*
1630 * Locate remote VTEP in VNI hash table.
1631 */
1632 static zebra_vtep_t *
1633 zvni_vtep_find (zebra_vni_t *zvni, struct in_addr *vtep_ip)
1634 {
1635 zebra_vtep_t *zvtep;
1636
1637 if (!zvni)
1638 return NULL;
1639
1640 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next)
1641 {
1642 if (zvni_vtep_match (vtep_ip, zvtep))
1643 break;
1644 }
1645
1646 return zvtep;
1647 }
1648
1649 /*
1650 * Add remote VTEP to VNI hash table.
1651 */
1652 static zebra_vtep_t *
1653 zvni_vtep_add (zebra_vni_t *zvni, struct in_addr *vtep_ip)
1654 {
1655 zebra_vtep_t *zvtep;
1656
1657 zvtep = XCALLOC (MTYPE_ZVNI_VTEP, sizeof(zebra_vtep_t));
1658 if (!zvtep)
1659 {
1660 zlog_err ("Failed to alloc VTEP entry, VNI %u", zvni->vni);
1661 return NULL;
1662 }
1663
1664 zvtep->vtep_ip = *vtep_ip;
1665
1666 if (zvni->vteps)
1667 zvni->vteps->prev = zvtep;
1668 zvtep->next = zvni->vteps;
1669 zvni->vteps = zvtep;
1670
1671 return zvtep;
1672 }
1673
1674 /*
1675 * Remove remote VTEP from VNI hash table.
1676 */
1677 static int
1678 zvni_vtep_del (zebra_vni_t *zvni, zebra_vtep_t *zvtep)
1679 {
1680 if (zvtep->next)
1681 zvtep->next->prev = zvtep->prev;
1682 if (zvtep->prev)
1683 zvtep->prev->next = zvtep->next;
1684 else
1685 zvni->vteps = zvtep->next;
1686
1687 zvtep->prev = zvtep->next = NULL;
1688 XFREE (MTYPE_ZVNI_VTEP, zvtep);
1689
1690 return 0;
1691 }
1692
1693 /*
1694 * Delete all remote VTEPs for this VNI (upon VNI delete). Also
1695 * uninstall from kernel if asked to.
1696 */
1697 static int
1698 zvni_vtep_del_all (zebra_vni_t *zvni, int uninstall)
1699 {
1700 zebra_vtep_t *zvtep, *zvtep_next;
1701
1702 if (!zvni)
1703 return -1;
1704
1705 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep_next)
1706 {
1707 zvtep_next = zvtep->next;
1708 if (uninstall)
1709 zvni_vtep_uninstall (zvni, &zvtep->vtep_ip);
1710 zvni_vtep_del (zvni, zvtep);
1711 }
1712
1713 return 0;
1714 }
1715
1716 /*
1717 * Install remote VTEP into the kernel.
1718 */
1719 static int
1720 zvni_vtep_install (zebra_vni_t *zvni, struct in_addr *vtep_ip)
1721 {
1722 return kernel_add_vtep (zvni->vni, zvni->vxlan_if, vtep_ip);
1723 }
1724
1725 /*
1726 * Uninstall remote VTEP from the kernel.
1727 */
1728 static int
1729 zvni_vtep_uninstall (zebra_vni_t *zvni, struct in_addr *vtep_ip)
1730 {
1731 if (!zvni->vxlan_if)
1732 {
1733 zlog_err ("VNI %u hash %p couldn't be uninstalled - no intf",
1734 zvni->vni, zvni);
1735 return -1;
1736 }
1737
1738 return kernel_del_vtep (zvni->vni, zvni->vxlan_if, vtep_ip);
1739 }
1740
1741 /*
1742 * Cleanup VNI/VTEP and update kernel
1743 */
1744 static void
1745 zvni_cleanup_all (struct hash_backet *backet, void *zvrf)
1746 {
1747 zebra_vni_t *zvni;
1748
1749 zvni = (zebra_vni_t *) backet->data;
1750 if (!zvni)
1751 return;
1752
1753 /* Free up all neighbors and MACs, if any. */
1754 zvni_neigh_del_all (zvrf, zvni, 1, 0, DEL_ALL_NEIGH);
1755 zvni_mac_del_all (zvrf, zvni, 1, 0, DEL_ALL_MAC);
1756
1757 /* Free up all remote VTEPs, if any. */
1758 zvni_vtep_del_all (zvni, 1);
1759
1760 /* Delete the hash entry. */
1761 zvni_del (zvrf, zvni);
1762 }
1763
1764
1765 /* Public functions */
1766
1767 /*
1768 * Display Neighbors for a VNI (VTY command handler).
1769 */
1770 void
1771 zebra_vxlan_print_neigh_vni (struct vty *vty, struct zebra_vrf *zvrf, vni_t vni)
1772 {
1773 zebra_vni_t *zvni;
1774 u_int32_t num_neigh;
1775 struct neigh_walk_ctx wctx;
1776
1777 if (!EVPN_ENABLED(zvrf))
1778 return;
1779 zvni = zvni_lookup (zvrf, vni);
1780 if (!zvni)
1781 {
1782 vty_out (vty, "%% VNI %u does not exist%s", vni, VTY_NEWLINE);
1783 return;
1784 }
1785 num_neigh = hashcount(zvni->neigh_table);
1786 if (!num_neigh)
1787 return;
1788
1789 /* Since we have IPv6 addresses to deal with which can vary widely in
1790 * size, we try to be a bit more elegant in display by first computing
1791 * the maximum width.
1792 */
1793 memset (&wctx, 0, sizeof (struct neigh_walk_ctx));
1794 wctx.zvni = zvni;
1795 wctx.vty = vty;
1796 wctx.addr_width = 15;
1797 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
1798
1799 vty_out(vty, "Number of ARPs (local and remote) known for this VNI: %u%s",
1800 num_neigh, VTY_NEWLINE);
1801 vty_out(vty, "%*s %-6s %-17s %-21s%s",
1802 -wctx.addr_width, "IP", "Type", "MAC",
1803 "Remote VTEP", VTY_NEWLINE);
1804
1805 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
1806 }
1807
1808 /*
1809 * Display neighbors across all VNIs (VTY command handler).
1810 */
1811 void
1812 zebra_vxlan_print_neigh_all_vni (struct vty *vty, struct zebra_vrf *zvrf)
1813 {
1814 if (!EVPN_ENABLED(zvrf))
1815 return;
1816 hash_iterate(zvrf->vni_table, zvni_print_neigh_hash_all_vni, vty);
1817 }
1818
1819 /*
1820 * Display specific neighbor for a VNI, if present (VTY command handler).
1821 */
1822 void
1823 zebra_vxlan_print_specific_neigh_vni (struct vty *vty, struct zebra_vrf *zvrf,
1824 vni_t vni, struct ipaddr *ip)
1825 {
1826 zebra_vni_t *zvni;
1827 zebra_neigh_t *n;
1828
1829 if (!EVPN_ENABLED(zvrf))
1830 return;
1831 zvni = zvni_lookup (zvrf, vni);
1832 if (!zvni)
1833 {
1834 vty_out (vty, "%% VNI %u does not exist%s", vni, VTY_NEWLINE);
1835 return;
1836 }
1837 n = zvni_neigh_lookup (zvni, ip);
1838 if (!n)
1839 {
1840 vty_out (vty, "%% Requested neighbor does not exist in VNI %u%s",
1841 vni, VTY_NEWLINE);
1842 return;
1843 }
1844
1845 zvni_print_neigh (n, vty);
1846 }
1847
1848 /*
1849 * Display neighbors for a VNI from specific VTEP (VTY command handler).
1850 * By definition, these are remote neighbors.
1851 */
1852 void
1853 zebra_vxlan_print_neigh_vni_vtep (struct vty *vty, struct zebra_vrf *zvrf,
1854 vni_t vni, struct in_addr vtep_ip)
1855 {
1856 zebra_vni_t *zvni;
1857 u_int32_t num_neigh;
1858 struct neigh_walk_ctx wctx;
1859
1860 if (!EVPN_ENABLED(zvrf))
1861 return;
1862 zvni = zvni_lookup (zvrf, vni);
1863 if (!zvni)
1864 {
1865 vty_out (vty, "%% VNI %u does not exist%s", vni, VTY_NEWLINE);
1866 return;
1867 }
1868 num_neigh = hashcount(zvni->neigh_table);
1869 if (!num_neigh)
1870 return;
1871
1872 memset (&wctx, 0, sizeof (struct neigh_walk_ctx));
1873 wctx.zvni = zvni;
1874 wctx.vty = vty;
1875 wctx.flags = SHOW_REMOTE_NEIGH_FROM_VTEP;
1876 wctx.r_vtep_ip = vtep_ip;
1877
1878 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
1879 }
1880
1881 /*
1882 * Display MACs for a VNI (VTY command handler).
1883 */
1884 void
1885 zebra_vxlan_print_macs_vni (struct vty *vty, struct zebra_vrf *zvrf, vni_t vni)
1886 {
1887 zebra_vni_t *zvni;
1888 u_int32_t num_macs;
1889 struct mac_walk_ctx wctx;
1890
1891 if (!EVPN_ENABLED(zvrf))
1892 return;
1893 zvni = zvni_lookup (zvrf, vni);
1894 if (!zvni)
1895 {
1896 vty_out (vty, "%% VNI %u does not exist%s", vni, VTY_NEWLINE);
1897 return;
1898 }
1899 num_macs = hashcount(zvni->mac_table);
1900 if (!num_macs)
1901 return;
1902
1903 memset (&wctx, 0, sizeof (struct mac_walk_ctx));
1904 wctx.zvni = zvni;
1905 wctx.vty = vty;
1906
1907 vty_out(vty, "Number of MACs (local and remote) known for this VNI: %u%s",
1908 num_macs, VTY_NEWLINE);
1909 vty_out(vty, "%-17s %-6s %-21s %-5s%s",
1910 "MAC", "Type", "Intf/Remote VTEP", "VLAN", VTY_NEWLINE);
1911
1912 hash_iterate(zvni->mac_table, zvni_print_mac_hash, &wctx);
1913 }
1914
1915 /*
1916 * Display MACs for all VNIs (VTY command handler).
1917 */
1918 void
1919 zebra_vxlan_print_macs_all_vni (struct vty *vty, struct zebra_vrf *zvrf)
1920 {
1921 struct mac_walk_ctx wctx;
1922
1923 if (!EVPN_ENABLED(zvrf))
1924 return;
1925 memset (&wctx, 0, sizeof (struct mac_walk_ctx));
1926 wctx.vty = vty;
1927 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni, &wctx);
1928 }
1929
1930 /*
1931 * Display MACs for all VNIs (VTY command handler).
1932 */
1933 void
1934 zebra_vxlan_print_macs_all_vni_vtep (struct vty *vty, struct zebra_vrf *zvrf,
1935 struct in_addr vtep_ip)
1936 {
1937 struct mac_walk_ctx wctx;
1938
1939 if (!EVPN_ENABLED(zvrf))
1940 return;
1941 memset (&wctx, 0, sizeof (struct mac_walk_ctx));
1942 wctx.vty = vty;
1943 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
1944 wctx.r_vtep_ip = vtep_ip;
1945 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni, &wctx);
1946 }
1947
1948 /*
1949 * Display specific MAC for a VNI, if present (VTY command handler).
1950 */
1951 void
1952 zebra_vxlan_print_specific_mac_vni (struct vty *vty, struct zebra_vrf *zvrf,
1953 vni_t vni, struct ethaddr *macaddr)
1954 {
1955 zebra_vni_t *zvni;
1956 zebra_mac_t *mac;
1957
1958 if (!EVPN_ENABLED(zvrf))
1959 return;
1960 zvni = zvni_lookup (zvrf, vni);
1961 if (!zvni)
1962 {
1963 vty_out (vty, "%% VNI %u does not exist%s", vni, VTY_NEWLINE);
1964 return;
1965 }
1966 mac = zvni_mac_lookup (zvni, macaddr);
1967 if (!mac)
1968 {
1969 vty_out (vty, "%% Requested MAC does not exist in VNI %u%s",
1970 vni, VTY_NEWLINE);
1971 return;
1972 }
1973
1974 zvni_print_mac (mac, vty);
1975 }
1976
1977 /*
1978 * Display MACs for a VNI from specific VTEP (VTY command handler).
1979 */
1980 void
1981 zebra_vxlan_print_macs_vni_vtep (struct vty *vty, struct zebra_vrf *zvrf,
1982 vni_t vni, struct in_addr vtep_ip)
1983 {
1984 zebra_vni_t *zvni;
1985 u_int32_t num_macs;
1986 struct mac_walk_ctx wctx;
1987
1988 if (!EVPN_ENABLED(zvrf))
1989 return;
1990 zvni = zvni_lookup (zvrf, vni);
1991 if (!zvni)
1992 {
1993 vty_out (vty, "%% VNI %u does not exist%s", vni, VTY_NEWLINE);
1994 return;
1995 }
1996 num_macs = hashcount(zvni->mac_table);
1997 if (!num_macs)
1998 return;
1999 memset (&wctx, 0, sizeof (struct mac_walk_ctx));
2000 wctx.zvni = zvni;
2001 wctx.vty = vty;
2002 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
2003 wctx.r_vtep_ip = vtep_ip;
2004 hash_iterate(zvni->mac_table, zvni_print_mac_hash, &wctx);
2005 }
2006
2007
2008 /*
2009 * Display VNI information (VTY command handler).
2010 */
2011 void
2012 zebra_vxlan_print_vni (struct vty *vty, struct zebra_vrf *zvrf, vni_t vni)
2013 {
2014 zebra_vni_t *zvni;
2015
2016 if (!EVPN_ENABLED(zvrf))
2017 return;
2018 zvni = zvni_lookup (zvrf, vni);
2019 if (!zvni)
2020 {
2021 vty_out (vty, "%% VNI %u does not exist%s", vni, VTY_NEWLINE);
2022 return;
2023 }
2024 zvni_print (zvni, (void *)vty);
2025 }
2026
2027 /*
2028 * Display VNI hash table (VTY command handler).
2029 */
2030 void
2031 zebra_vxlan_print_vnis (struct vty *vty, struct zebra_vrf *zvrf)
2032 {
2033 u_int32_t num_vnis;
2034
2035 if (!EVPN_ENABLED(zvrf))
2036 return;
2037 num_vnis = hashcount(zvrf->vni_table);
2038 if (!num_vnis)
2039 return;
2040 vty_out(vty, "Number of VNIs: %u%s", num_vnis, VTY_NEWLINE);
2041 vty_out(vty, "%-10s %-21s %-15s %-8s %-8s %-15s%s",
2042 "VNI", "VxLAN IF", "VTEP IP", "# MACs", "# ARPs",
2043 "# Remote VTEPs", VTY_NEWLINE);
2044 hash_iterate(zvrf->vni_table, zvni_print_hash, vty);
2045 }
2046
2047 /*
2048 * Handle neighbor delete (on a VLAN device / L3 interface) from the
2049 * kernel. This may result in either the neighbor getting deleted from
2050 * our database or being re-added to the kernel (if it is a valid
2051 * remote neighbor).
2052 */
2053 int
2054 zebra_vxlan_local_neigh_del (struct interface *ifp,
2055 struct interface *link_if,
2056 struct ipaddr *ip)
2057 {
2058 zebra_vni_t *zvni;
2059 zebra_neigh_t *n;
2060 struct zebra_vrf *zvrf;
2061 char buf[INET6_ADDRSTRLEN];
2062
2063 /* We are only interested in neighbors on an SVI that resides on top
2064 * of a VxLAN bridge.
2065 */
2066 zvni = zvni_map_svi (ifp, link_if);
2067 if (!zvni)
2068 return 0;
2069 if (!zvni->vxlan_if)
2070 {
2071 zlog_err ("VNI %u hash %p doesn't have intf upon local neighbor DEL",
2072 zvni->vni, zvni);
2073 return -1;
2074 }
2075
2076 if (IS_ZEBRA_DEBUG_VXLAN)
2077 zlog_debug ("%u:Del neighbor %s intf %s(%u) -> VNI %u",
2078 ifp->vrf_id, ipaddr2str (ip, buf, sizeof(buf)),
2079 ifp->name, ifp->ifindex, zvni->vni);
2080
2081 /* If entry doesn't exist, nothing to do. */
2082 n = zvni_neigh_lookup (zvni, ip);
2083 if (!n)
2084 return 0;
2085
2086 /* If it is a remote entry, the kernel has aged this out or someone has
2087 * deleted it, it needs to be re-installed as Quagga is the owner.
2088 */
2089 if (CHECK_FLAG (n->flags, ZEBRA_NEIGH_REMOTE))
2090 {
2091 zvni_neigh_install (zvni, n);
2092 return 0;
2093 }
2094
2095 /* Locate VRF corresponding to interface. */
2096 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
2097 assert(zvrf);
2098
2099 /* Remove neighbor from BGP. */
2100 zvni_neigh_send_del_to_client (zvrf, zvni->vni, &n->ip, &n->emac);
2101
2102 /* Delete this neighbor entry. */
2103 zvni_neigh_del (zvni, n);
2104
2105 return 0;
2106 }
2107
2108 /*
2109 * Handle neighbor add or update (on a VLAN device / L3 interface)
2110 * from the kernel.
2111 */
2112 int
2113 zebra_vxlan_local_neigh_add_update (struct interface *ifp,
2114 struct interface *link_if,
2115 struct ipaddr *ip,
2116 struct ethaddr *macaddr,
2117 u_int16_t state,
2118 u_char ext_learned)
2119 {
2120 zebra_vni_t *zvni;
2121 zebra_neigh_t *n;
2122 struct zebra_vrf *zvrf;
2123 char buf[ETHER_ADDR_STRLEN];
2124 char buf2[INET6_ADDRSTRLEN];
2125 int send_upd = 1, send_del = 0;
2126
2127 /* We are only interested in neighbors on an SVI that resides on top
2128 * of a VxLAN bridge.
2129 */
2130 zvni = zvni_map_svi (ifp, link_if);
2131 if (!zvni)
2132 return 0;
2133
2134 /* Locate VRF corresponding to interface. */
2135 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
2136 assert(zvrf);
2137
2138 if (IS_ZEBRA_DEBUG_VXLAN)
2139 zlog_debug ("%u:Add/Update neighbor %s MAC %s intf %s(%u) state 0x%x "
2140 "%s-> VNI %u",
2141 ifp->vrf_id, ipaddr2str (ip, buf2, sizeof(buf2)),
2142 prefix_mac2str (macaddr, buf, sizeof (buf)),
2143 ifp->name, ifp->ifindex, state,
2144 ext_learned ? "ext-learned " : "", zvni->vni);
2145
2146 /* If same entry already exists, it might be a change or it might be a
2147 * move from remote to local.
2148 */
2149 n = zvni_neigh_lookup (zvni, ip);
2150 if (n)
2151 {
2152 if (CHECK_FLAG (n->flags, ZEBRA_NEIGH_LOCAL))
2153 {
2154 if (memcmp (n->emac.octet, macaddr->octet, ETHER_ADDR_LEN) == 0)
2155 {
2156 if (n->ifindex == ifp->ifindex)
2157 /* we're not interested in whatever has changed. */
2158 return 0;
2159 /* client doesn't care about a purely local change. */
2160 send_upd = 0;
2161 }
2162 else
2163 /* If the MAC has changed, issue a delete first as this means a
2164 * different MACIP route.
2165 */
2166 send_del = 1;
2167 }
2168 else if (ext_learned)
2169 /* The neighbor is remote and that is the notification we got. */
2170 {
2171 /* TODO: Evaluate if we need to do anything here. */
2172 return 0;
2173 }
2174 else
2175 /* Neighbor has moved from remote to local. */
2176 {
2177 UNSET_FLAG (n->flags, ZEBRA_NEIGH_REMOTE);
2178 n->r_vtep_ip.s_addr = 0;
2179 }
2180 }
2181 else
2182 {
2183 n = zvni_neigh_add (zvni, ip);
2184 if (!n)
2185 {
2186 zlog_err ("%u:Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
2187 ifp->vrf_id, ipaddr2str (ip, buf2, sizeof(buf2)),
2188 prefix_mac2str (macaddr, buf, sizeof (buf)),
2189 ifp->name, ifp->ifindex, zvni->vni);
2190 return -1;
2191 }
2192 }
2193
2194 /* Issue delete for older info, if needed. */
2195 if (send_del)
2196 zvni_neigh_send_del_to_client (zvrf, zvni->vni, &n->ip, &n->emac);
2197
2198 /* Set "local" forwarding info. */
2199 SET_FLAG (n->flags, ZEBRA_NEIGH_LOCAL);
2200 memcpy (&n->emac, macaddr, ETHER_ADDR_LEN);
2201 n->ifindex = ifp->ifindex;
2202
2203 /* Inform BGP if required. */
2204 if (send_upd)
2205 return zvni_neigh_send_add_to_client (zvrf, zvni->vni, ip, macaddr);
2206
2207 return 0;
2208 }
2209
2210 /*
2211 * Handle message from client to delete a remote MACIP for a VNI.
2212 */
2213 int zebra_vxlan_remote_macip_del (struct zserv *client, int sock,
2214 u_short length, struct zebra_vrf *zvrf)
2215 {
2216 struct stream *s;
2217 vni_t vni;
2218 struct ethaddr macaddr;
2219 struct ipaddr ip;
2220 struct in_addr vtep_ip;
2221 zebra_vni_t *zvni;
2222 zebra_mac_t *mac;
2223 zebra_neigh_t *n;
2224 u_short l = 0, ipa_len;
2225 char buf[ETHER_ADDR_STRLEN];
2226 char buf1[INET6_ADDRSTRLEN];
2227
2228 s = client->ibuf;
2229
2230 while (l < length)
2231 {
2232 /* Obtain each remote MACIP and process. */
2233 /* Message contains VNI, followed by MAC followed by IP (if any)
2234 * followed by remote VTEP IP.
2235 */
2236 mac = NULL;
2237 n = NULL;
2238 memset (&ip, 0, sizeof (ip));
2239 vni = (vni_t) stream_getl (s);
2240 stream_get (&macaddr.octet, s, ETHER_ADDR_LEN);
2241 ipa_len = stream_getl (s);
2242 if (ipa_len)
2243 {
2244 ip.ipa_type = (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4: IPADDR_V6;
2245 stream_get (&ip.ip.addr, s, ipa_len);
2246 }
2247 l += 4 + ETHER_ADDR_LEN + 4 + ipa_len;
2248 vtep_ip.s_addr = stream_get_ipv4(s);
2249 l += IPV4_MAX_BYTELEN;
2250
2251 if (IS_ZEBRA_DEBUG_VXLAN)
2252 zlog_debug ("%u:Recv MACIP Del MAC %s IP %s VNI %u Remote VTEP %s from %s",
2253 zvrf_id (zvrf),
2254 prefix_mac2str (&macaddr, buf, sizeof (buf)),
2255 ipaddr2str (&ip, buf1, sizeof (buf1)),
2256 vni, inet_ntoa (vtep_ip),
2257 zebra_route_string (client->proto));
2258
2259 /* Locate VNI hash entry - expected to exist. */
2260 zvni = zvni_lookup (zvrf, vni);
2261 if (!zvni)
2262 {
2263 if (IS_ZEBRA_DEBUG_VXLAN)
2264 zlog_debug ("Failed to locate VNI hash upon remote MACIP DEL, "
2265 "VRF %d VNI %u", zvrf_id (zvrf), vni);
2266 continue;
2267 }
2268 if (!zvni->vxlan_if)
2269 {
2270 zlog_err ("VNI %u hash %p doesn't have intf upon remote MACIP DEL",
2271 vni, zvni);
2272 continue;
2273 }
2274
2275 /* The remote VTEP specified is normally expected to exist, but it is
2276 * possible that the peer may delete the VTEP before deleting any MACs
2277 * referring to the VTEP, in which case the handler (see remote_vtep_del)
2278 * would have already deleted the MACs.
2279 */
2280 if (!zvni_vtep_find (zvni, &vtep_ip))
2281 continue;
2282
2283 /* If the local VxLAN interface is not up (should be a transient
2284 * event), there's nothing more to do.
2285 */
2286 if (!if_is_operative (zvni->vxlan_if))
2287 continue;
2288
2289 mac = zvni_mac_lookup (zvni, &macaddr);
2290 if (ipa_len)
2291 n = zvni_neigh_lookup (zvni, &ip);
2292
2293 if (n && !mac)
2294 {
2295 zlog_err ("failed to locate MAC %s for neigh %s in VRF %u VNI %u",
2296 prefix_mac2str (&macaddr, buf, sizeof (buf)),
2297 ipaddr2str (&ip, buf1, sizeof (buf1)),
2298 zvrf_id (zvrf), vni);
2299 continue;
2300 }
2301
2302 /* If the remote mac or neighbor doesn't exist there is nothing more
2303 * to do. Otherwise, uninstall the entry and then remove it.
2304 */
2305 if (!mac && !n)
2306 continue;
2307
2308 /* Uninstall remote neighbor or MAC. */
2309 if (n)
2310 {
2311 /* When the MAC changes for an IP, it is possible the client may
2312 * update the new MAC before trying to delete the "old" neighbor
2313 * (as these are two different MACIP routes). Do the delete only
2314 * if the MAC matches.
2315 */
2316 if (CHECK_FLAG (n->flags, ZEBRA_NEIGH_REMOTE) &&
2317 (memcmp (n->emac.octet, macaddr.octet, ETHER_ADDR_LEN) == 0))
2318 {
2319 zvni_neigh_uninstall (zvni, n);
2320 zvni_neigh_del (zvni, n);
2321 zvni_deref_ip2mac (zvni, mac, 1);
2322 }
2323 }
2324 else
2325 {
2326 if (CHECK_FLAG (mac->flags, ZEBRA_MAC_REMOTE))
2327 {
2328 if (!mac->neigh_refcnt)
2329 {
2330 zvni_mac_uninstall (zvni, mac, 0);
2331 zvni_mac_del (zvni, mac);
2332 }
2333 else
2334 SET_FLAG (mac->flags, ZEBRA_MAC_AUTO);
2335 }
2336 }
2337 }
2338
2339 return 0;
2340 }
2341
2342 /*
2343 * Handle message from client to add a remote MACIP for a VNI. This
2344 * could be just the add of a MAC address or the add of a neighbor
2345 * (IP+MAC).
2346 */
2347 int
2348 zebra_vxlan_remote_macip_add (struct zserv *client, int sock,
2349 u_short length, struct zebra_vrf *zvrf)
2350 {
2351 struct stream *s;
2352 vni_t vni;
2353 struct ethaddr macaddr;
2354 struct ipaddr ip;
2355 struct in_addr vtep_ip;
2356 zebra_vni_t *zvni;
2357 zebra_vtep_t *zvtep;
2358 zebra_mac_t *mac, *old_mac;
2359 zebra_neigh_t *n;
2360 u_short l = 0, ipa_len;
2361 int update_mac = 0, update_neigh = 0;
2362 char buf[ETHER_ADDR_STRLEN];
2363 char buf1[INET6_ADDRSTRLEN];
2364 u_char sticky;
2365
2366 assert (EVPN_ENABLED (zvrf));
2367
2368 s = client->ibuf;
2369
2370 while (l < length)
2371 {
2372 /* Obtain each remote MACIP and process. */
2373 /* Message contains VNI, followed by MAC followed by IP (if any)
2374 * followed by remote VTEP IP.
2375 */
2376 update_mac = update_neigh = 0;
2377 mac = NULL;
2378 n = NULL;
2379 memset (&ip, 0, sizeof (ip));
2380 vni = (vni_t) stream_getl (s);
2381 stream_get (&macaddr.octet, s, ETHER_ADDR_LEN);
2382 ipa_len = stream_getl (s);
2383 if (ipa_len)
2384 {
2385 ip.ipa_type = (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4: IPADDR_V6;
2386 stream_get (&ip.ip.addr, s, ipa_len);
2387 }
2388 l += 4 + ETHER_ADDR_LEN + 4 + ipa_len;
2389 vtep_ip.s_addr = stream_get_ipv4 (s);
2390 l += IPV4_MAX_BYTELEN;
2391
2392 /* Get 'sticky' flag. */
2393 sticky = stream_getc(s);
2394 l++;
2395
2396 if (IS_ZEBRA_DEBUG_VXLAN)
2397 zlog_debug ("%u:Recv MACIP Add %sMAC %s IP %s VNI %u Remote VTEP %s from %s",
2398 zvrf_id (zvrf),
2399 sticky ? "sticky " : "",
2400 prefix_mac2str (&macaddr, buf, sizeof (buf)),
2401 ipaddr2str (&ip, buf1, sizeof (buf1)),
2402 vni, inet_ntoa (vtep_ip),
2403 zebra_route_string (client->proto));
2404
2405 /* Locate VNI hash entry - expected to exist. */
2406 zvni = zvni_lookup (zvrf, vni);
2407 if (!zvni)
2408 {
2409 zlog_err ("Failed to locate VNI hash upon remote MACIP ADD, VRF %d VNI %u",
2410 zvrf_id (zvrf), vni);
2411 continue;
2412 }
2413 if (!zvni->vxlan_if)
2414 {
2415 zlog_err ("VNI %u hash %p doesn't have intf upon remote MACIP add",
2416 vni, zvni);
2417 continue;
2418 }
2419 /* If the local VxLAN interface is not up (should be a transient
2420 * event), there's nothing more to do.
2421 */
2422 if (!if_is_operative (zvni->vxlan_if))
2423 continue;
2424
2425 /* The remote VTEP specified should normally exist, but it is possible
2426 * that when peering comes up, peer may advertise MACIP routes before
2427 * advertising type-3 routes.
2428 */
2429 zvtep = zvni_vtep_find (zvni, &vtep_ip);
2430 if (!zvtep)
2431 {
2432 if (zvni_vtep_add (zvni, &vtep_ip) == NULL)
2433 {
2434 zlog_err ("Failed to add remote VTEP, VRF %d VNI %u zvni %p",
2435 zvrf_id (zvrf), vni, zvni);
2436 continue;
2437 }
2438
2439 zvni_vtep_install (zvni, &vtep_ip);
2440 }
2441
2442 /* First, check if the remote MAC is unknown or has a change. If so,
2443 * that needs to be updated first. Note that client could install
2444 * MAC and MACIP separately or just install the latter.
2445 */
2446 mac = zvni_mac_lookup (zvni, &macaddr);
2447 if (!mac || !CHECK_FLAG (mac->flags, ZEBRA_MAC_REMOTE) ||
2448 (CHECK_FLAG (mac->flags, ZEBRA_MAC_STICKY) ? 1: 0) != sticky ||
2449 !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &vtep_ip))
2450 update_mac = 1;
2451
2452 if (update_mac)
2453 {
2454 if (!mac)
2455 {
2456 mac = zvni_mac_add (zvni, &macaddr);
2457 if (!mac)
2458 {
2459 zlog_warn ("%u:Failed to add MAC %s VNI %u Remote VTEP %s",
2460 zvrf_id (zvrf),
2461 prefix_mac2str (&macaddr, buf, sizeof (buf)),
2462 vni, inet_ntoa (vtep_ip));
2463 return -1;
2464 }
2465
2466 /* Is this MAC created for a MACIP? */
2467 if (ipa_len)
2468 SET_FLAG (mac->flags, ZEBRA_MAC_AUTO);
2469 }
2470 else if (CHECK_FLAG (mac->flags, ZEBRA_MAC_LOCAL))
2471 {
2472 /* Moving from local to remote, issue delete. */
2473 zvni_mac_uninstall (zvni, mac, 1);
2474 }
2475
2476 /* Set "auto" and "remote" forwarding info. */
2477 UNSET_FLAG (mac->flags, ZEBRA_MAC_LOCAL);
2478 memset (&mac->fwd_info, 0, sizeof (mac->fwd_info));
2479 SET_FLAG (mac->flags, ZEBRA_MAC_REMOTE);
2480 mac->fwd_info.r_vtep_ip = vtep_ip;
2481
2482 if (sticky)
2483 SET_FLAG (mac->flags, ZEBRA_MAC_STICKY);
2484 else
2485 UNSET_FLAG (mac->flags, ZEBRA_MAC_STICKY);
2486
2487 /* Install the entry. */
2488 zvni_mac_install (zvni, mac);
2489 }
2490
2491 /* If there is no IP, continue - after clearing AUTO flag of MAC. */
2492 if (!ipa_len)
2493 {
2494 UNSET_FLAG (mac->flags, ZEBRA_MAC_AUTO);
2495 continue;
2496 }
2497
2498 /* Check if the remote neighbor itself is unknown or has a change.
2499 * If so, create or update and then install the entry.
2500 */
2501 n = zvni_neigh_lookup (zvni, &ip);
2502 if (!n || !CHECK_FLAG (n->flags, ZEBRA_NEIGH_REMOTE) ||
2503 (memcmp(&n->emac, &macaddr, sizeof (macaddr)) != 0) ||
2504 !IPV4_ADDR_SAME(&n->r_vtep_ip, &vtep_ip))
2505 update_neigh = 1;
2506
2507 if (update_neigh)
2508 {
2509 if (!n)
2510 {
2511 n = zvni_neigh_add (zvni, &ip);
2512 if (!n)
2513 {
2514 zlog_warn ("%u:Failed to add Neigh %s MAC %s VNI %u Remote VTEP %s",
2515 zvrf_id (zvrf), ipaddr2str (&ip, buf1, sizeof (buf1)),
2516 prefix_mac2str (&macaddr, buf, sizeof (buf)),
2517 vni, inet_ntoa (vtep_ip));
2518 return -1;
2519 }
2520
2521 /* New neighbor referring to this MAC. */
2522 mac->neigh_refcnt++;
2523 }
2524 else if (memcmp(&n->emac, &macaddr, sizeof (macaddr)) != 0)
2525 {
2526 /* MAC change, update ref counts for old and new MAC. */
2527 old_mac = zvni_mac_lookup (zvni, &n->emac);
2528 if (old_mac)
2529 zvni_deref_ip2mac (zvni, old_mac, 1);
2530 mac->neigh_refcnt++;
2531 }
2532
2533 /* Set "remote" forwarding info. */
2534 UNSET_FLAG (n->flags, ZEBRA_NEIGH_LOCAL);
2535 /* TODO: Handle MAC change. */
2536 memcpy (&n->emac, &macaddr, ETHER_ADDR_LEN);
2537 n->r_vtep_ip = vtep_ip;
2538 SET_FLAG (n->flags, ZEBRA_NEIGH_REMOTE);
2539
2540 /* Install the entry. */
2541 zvni_neigh_install (zvni, n);
2542 }
2543 }
2544
2545 return 0;
2546 }
2547
2548 /*
2549 * Handle notification of MAC add/update over VxLAN. If the kernel is notifying
2550 * us, this must involve a multihoming scenario. Treat this as implicit delete
2551 * of any prior local MAC.
2552 */
2553 int
2554 zebra_vxlan_check_del_local_mac (struct interface *ifp,
2555 struct interface *br_if,
2556 struct ethaddr *macaddr,
2557 vlanid_t vid)
2558 {
2559 struct zebra_if *zif;
2560 struct zebra_vrf *zvrf;
2561 struct zebra_l2info_vxlan *vxl;
2562 vni_t vni;
2563 zebra_vni_t *zvni;
2564 zebra_mac_t *mac;
2565 char buf[ETHER_ADDR_STRLEN];
2566 u_char sticky;
2567
2568 zif = ifp->info;
2569 assert(zif);
2570 vxl = &zif->l2info.vxl;
2571 vni = vxl->vni;
2572
2573 /* Locate VRF corresponding to interface. */
2574 zvrf = vrf_info_lookup(ifp->vrf_id);
2575 assert(zvrf);
2576
2577 /* If EVPN is not enabled, nothing to do. */
2578 if (!EVPN_ENABLED(zvrf))
2579 return 0;
2580
2581 /* Locate hash entry; it is expected to exist. */
2582 zvni = zvni_lookup (zvrf, vni);
2583 if (!zvni)
2584 return 0;
2585
2586 /* If entry doesn't exist, nothing to do. */
2587 mac = zvni_mac_lookup (zvni, macaddr);
2588 if (!mac)
2589 return 0;
2590
2591 /* Is it a local entry? */
2592 if (!CHECK_FLAG (mac->flags, ZEBRA_MAC_LOCAL))
2593 return 0;
2594
2595 if (IS_ZEBRA_DEBUG_VXLAN)
2596 zlog_debug ("%u:Add/update remote MAC %s intf %s(%u) VNI %u - del local",
2597 ifp->vrf_id, prefix_mac2str (macaddr, buf, sizeof (buf)),
2598 ifp->name, ifp->ifindex, vni);
2599
2600 /* Remove MAC from BGP. */
2601 sticky = CHECK_FLAG (mac->flags, ZEBRA_MAC_STICKY) ? 1: 0;
2602 zvni_mac_send_del_to_client (zvrf, zvni->vni, macaddr, sticky);
2603
2604 /* Delete this MAC entry. */
2605 zvni_mac_del (zvni, mac);
2606
2607 return 0;
2608 }
2609
2610 /*
2611 * Handle remote MAC delete by kernel; readd the remote MAC if we have it.
2612 * This can happen because the remote MAC entries are also added as "dynamic",
2613 * so the kernel can ageout the entry.
2614 */
2615 int
2616 zebra_vxlan_check_readd_remote_mac (struct interface *ifp,
2617 struct interface *br_if,
2618 struct ethaddr *macaddr,
2619 vlanid_t vid)
2620 {
2621 struct zebra_if *zif;
2622 struct zebra_vrf *zvrf;
2623 struct zebra_l2info_vxlan *vxl;
2624 vni_t vni;
2625 zebra_vni_t *zvni;
2626 zebra_mac_t *mac;
2627 char buf[ETHER_ADDR_STRLEN];
2628
2629 zif = ifp->info;
2630 assert(zif);
2631 vxl = &zif->l2info.vxl;
2632 vni = vxl->vni;
2633
2634 /* Locate VRF corresponding to interface. */
2635 zvrf = vrf_info_lookup(ifp->vrf_id);
2636 assert(zvrf);
2637
2638 /* If EVPN is not enabled, nothing to do. */
2639 if (!EVPN_ENABLED(zvrf))
2640 return 0;
2641
2642 /* Locate hash entry; it is expected to exist. */
2643 zvni = zvni_lookup (zvrf, vni);
2644 if (!zvni)
2645 return 0;
2646
2647 /* If entry doesn't exist, nothing to do. */
2648 mac = zvni_mac_lookup (zvni, macaddr);
2649 if (!mac)
2650 return 0;
2651
2652 /* Is it a remote entry? */
2653 if (!CHECK_FLAG (mac->flags, ZEBRA_MAC_REMOTE))
2654 return 0;
2655
2656 if (IS_ZEBRA_DEBUG_VXLAN)
2657 zlog_debug ("%u:Del remote MAC %s intf %s(%u) VNI %u - readd",
2658 ifp->vrf_id, prefix_mac2str (macaddr, buf, sizeof (buf)),
2659 ifp->name, ifp->ifindex, vni);
2660
2661 zvni_mac_install (zvni, mac);
2662 return 0;
2663 }
2664
2665 /*
2666 * Handle local MAC delete (on a port or VLAN corresponding to this VNI).
2667 */
2668 int
2669 zebra_vxlan_local_mac_del (struct interface *ifp, struct interface *br_if,
2670 struct ethaddr *macaddr, vlanid_t vid)
2671 {
2672 zebra_vni_t *zvni;
2673 zebra_mac_t *mac;
2674 struct zebra_vrf *zvrf;
2675 char buf[ETHER_ADDR_STRLEN];
2676 u_char sticky;
2677
2678 /* We are interested in MACs only on ports or (port, VLAN) that
2679 * map to a VNI.
2680 */
2681 zvni = zvni_map_vlan (ifp, br_if, vid);
2682 if (!zvni)
2683 return 0;
2684 if (!zvni->vxlan_if)
2685 {
2686 zlog_err ("VNI %u hash %p doesn't have intf upon local MAC DEL",
2687 zvni->vni, zvni);
2688 return -1;
2689 }
2690
2691 if (IS_ZEBRA_DEBUG_VXLAN)
2692 zlog_debug ("%u:Del MAC %s intf %s(%u) VID %u -> VNI %u",
2693 ifp->vrf_id, prefix_mac2str (macaddr, buf, sizeof (buf)),
2694 ifp->name, ifp->ifindex, vid, zvni->vni);
2695
2696 /* If entry doesn't exist, nothing to do. */
2697 mac = zvni_mac_lookup (zvni, macaddr);
2698 if (!mac)
2699 return 0;
2700
2701 /* Is it a local entry? */
2702 if (!CHECK_FLAG (mac->flags, ZEBRA_MAC_LOCAL))
2703 return 0;
2704
2705 /* Locate VRF corresponding to interface. */
2706 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
2707 assert(zvrf);
2708
2709 /* Remove MAC from BGP. */
2710 sticky = CHECK_FLAG (mac->flags, ZEBRA_MAC_STICKY) ? 1: 0;
2711 zvni_mac_send_del_to_client (zvrf, zvni->vni, macaddr, sticky);
2712
2713 /* Delete this MAC entry. */
2714 zvni_mac_del (zvni, mac);
2715
2716 return 0;
2717 }
2718
2719 /*
2720 * Handle local MAC add (on a port or VLAN corresponding to this VNI).
2721 */
2722 int
2723 zebra_vxlan_local_mac_add_update (struct interface *ifp, struct interface *br_if,
2724 struct ethaddr *macaddr, vlanid_t vid,
2725 u_char sticky)
2726 {
2727 zebra_vni_t *zvni;
2728 zebra_mac_t *mac;
2729 struct zebra_vrf *zvrf;
2730 char buf[ETHER_ADDR_STRLEN];
2731 int add = 1;
2732 u_char mac_sticky;
2733
2734 /* We are interested in MACs only on ports or (port, VLAN) that
2735 * map to a VNI.
2736 */
2737 zvni = zvni_map_vlan (ifp, br_if, vid);
2738 if (!zvni)
2739 {
2740 if (IS_ZEBRA_DEBUG_VXLAN)
2741 zlog_debug ("%u:Add/Update %sMAC %s intf %s(%u) VID %u, could not find VNI",
2742 ifp->vrf_id,
2743 sticky ? "sticky " : "",
2744 prefix_mac2str (macaddr, buf, sizeof (buf)),
2745 ifp->name, ifp->ifindex, vid);
2746 return 0;
2747 }
2748
2749 if (!zvni->vxlan_if)
2750 {
2751 zlog_err ("VNI %u hash %p doesn't have intf upon local MAC ADD",
2752 zvni->vni, zvni);
2753 return -1;
2754 }
2755
2756 if (IS_ZEBRA_DEBUG_VXLAN)
2757 zlog_debug ("%u:Add/Update %sMAC %s intf %s(%u) VID %u -> VNI %u",
2758 ifp->vrf_id,
2759 sticky ? "sticky " : "",
2760 prefix_mac2str (macaddr, buf, sizeof (buf)),
2761 ifp->name, ifp->ifindex, vid, zvni->vni);
2762
2763 /* If same entry already exists, nothing to do. */
2764 mac = zvni_mac_lookup (zvni, macaddr);
2765 if (mac)
2766 {
2767 if (CHECK_FLAG (mac->flags, ZEBRA_MAC_LOCAL))
2768 {
2769 mac_sticky = CHECK_FLAG (mac->flags, ZEBRA_MAC_STICKY) ? 1: 0;
2770
2771 if (mac_sticky == sticky &&
2772 mac->fwd_info.local.ifindex == ifp->ifindex &&
2773 mac->fwd_info.local.vid == vid)
2774 {
2775 if (IS_ZEBRA_DEBUG_VXLAN)
2776 zlog_debug ("%u:Add/Update %sMAC %s intf %s(%u) VID %u -> VNI %u, "
2777 "entry exists and has not changed ",
2778 ifp->vrf_id,
2779 sticky ? "sticky " : "",
2780 prefix_mac2str (macaddr, buf, sizeof (buf)),
2781 ifp->name, ifp->ifindex, vid, zvni->vni);
2782 return 0;
2783 }
2784
2785 add = 0; /* This is an update of local interface. */
2786 }
2787 }
2788
2789 /* Locate VRF corresponding to interface. */
2790 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
2791 assert(zvrf);
2792
2793 if (!mac)
2794 {
2795 mac = zvni_mac_add (zvni, macaddr);
2796 if (!mac)
2797 {
2798 zlog_err ("%u:Failed to add MAC %s intf %s(%u) VID %u",
2799 ifp->vrf_id, prefix_mac2str (macaddr, buf, sizeof (buf)),
2800 ifp->name, ifp->ifindex, vid);
2801 return -1;
2802 }
2803 }
2804
2805 /* Set "local" forwarding info. */
2806 UNSET_FLAG (mac->flags, ZEBRA_MAC_REMOTE);
2807 memset (&mac->fwd_info, 0, sizeof (mac->fwd_info));
2808 SET_FLAG (mac->flags, ZEBRA_MAC_LOCAL);
2809 mac->fwd_info.local.ifindex = ifp->ifindex;
2810 mac->fwd_info.local.vid = vid;
2811
2812 if (sticky)
2813 SET_FLAG (mac->flags, ZEBRA_MAC_STICKY);
2814 else
2815 UNSET_FLAG (mac->flags, ZEBRA_MAC_STICKY);
2816
2817 /* Inform BGP if required. */
2818 if (add)
2819 return zvni_mac_send_add_to_client (zvrf, zvni->vni, macaddr, sticky);
2820
2821 return 0;
2822 }
2823
2824 /*
2825 * Handle message from client to delete a remote VTEP for a VNI.
2826 */
2827 int zebra_vxlan_remote_vtep_del (struct zserv *client, int sock,
2828 u_short length, struct zebra_vrf *zvrf)
2829 {
2830 struct stream *s;
2831 u_short l = 0;
2832 vni_t vni;
2833 struct in_addr vtep_ip;
2834 zebra_vni_t *zvni;
2835 zebra_vtep_t *zvtep;
2836
2837 s = client->ibuf;
2838
2839 while (l < length)
2840 {
2841 /* Obtain each remote VTEP and process. */
2842 vni = (vni_t) stream_getl (s);
2843 l += 4;
2844 vtep_ip.s_addr = stream_get_ipv4 (s);
2845 l += IPV4_MAX_BYTELEN;
2846
2847 if (IS_ZEBRA_DEBUG_VXLAN)
2848 zlog_debug ("%u:Recv VTEP_DEL %s VNI %u from %s",
2849 zvrf_id (zvrf), inet_ntoa (vtep_ip),
2850 vni, zebra_route_string (client->proto));
2851
2852 /* Locate VNI hash entry - expected to exist. */
2853 zvni = zvni_lookup (zvrf, vni);
2854 if (!zvni)
2855 {
2856 if (IS_ZEBRA_DEBUG_VXLAN)
2857 zlog_debug ("Failed to locate VNI hash upon remote VTEP DEL, "
2858 "VRF %d VNI %u", zvrf_id (zvrf), vni);
2859 continue;
2860 }
2861
2862 /* If the remote VTEP does not exist, there's nothing more to do.
2863 * Otherwise, uninstall any remote MACs pointing to this VTEP and
2864 * then, the VTEP entry itself and remove it.
2865 */
2866 zvtep = zvni_vtep_find (zvni, &vtep_ip);
2867 if (!zvtep)
2868 continue;
2869
2870 zvni_neigh_del_from_vtep (zvni, 1, &vtep_ip);
2871 zvni_mac_del_from_vtep (zvni, 1, &vtep_ip);
2872 zvni_vtep_uninstall (zvni, &vtep_ip);
2873 zvni_vtep_del (zvni, zvtep);
2874 }
2875
2876 return 0;
2877 }
2878
2879 /*
2880 * Handle message from client to add a remote VTEP for a VNI.
2881 */
2882 int zebra_vxlan_remote_vtep_add (struct zserv *client, int sock,
2883 u_short length, struct zebra_vrf *zvrf)
2884 {
2885 struct stream *s;
2886 u_short l = 0;
2887 vni_t vni;
2888 struct in_addr vtep_ip;
2889 zebra_vni_t *zvni;
2890
2891 assert (EVPN_ENABLED (zvrf));
2892
2893 s = client->ibuf;
2894
2895 while (l < length)
2896 {
2897 /* Obtain each remote VTEP and process. */
2898 vni = (vni_t) stream_getl (s);
2899 l += 4;
2900 vtep_ip.s_addr = stream_get_ipv4 (s);
2901 l += IPV4_MAX_BYTELEN;
2902
2903 if (IS_ZEBRA_DEBUG_VXLAN)
2904 zlog_debug ("%u:Recv VTEP_ADD %s VNI %u from %s",
2905 zvrf_id (zvrf), inet_ntoa (vtep_ip),
2906 vni, zebra_route_string (client->proto));
2907
2908 /* Locate VNI hash entry - expected to exist. */
2909 zvni = zvni_lookup (zvrf, vni);
2910 if (!zvni)
2911 {
2912 zlog_err ("Failed to locate VNI hash upon remote VTEP ADD, VRF %d VNI %u",
2913 zvrf_id (zvrf), vni);
2914 continue;
2915 }
2916 if (!zvni->vxlan_if)
2917 {
2918 zlog_err ("VNI %u hash %p doesn't have intf upon remote VTEP ADD",
2919 zvni->vni, zvni);
2920 continue;
2921 }
2922
2923
2924 /* If the remote VTEP already exists, or the local VxLAN interface is
2925 * not up (should be a transient event), there's nothing more to do.
2926 * Otherwise, add and install the entry.
2927 */
2928 if (zvni_vtep_find (zvni, &vtep_ip))
2929 continue;
2930
2931 if (!if_is_operative (zvni->vxlan_if))
2932 continue;
2933
2934 if (zvni_vtep_add (zvni, &vtep_ip) == NULL)
2935 {
2936 zlog_err ("Failed to add remote VTEP, VRF %d VNI %u zvni %p",
2937 zvrf_id (zvrf), vni, zvni);
2938 continue;
2939 }
2940
2941 zvni_vtep_install (zvni, &vtep_ip);
2942 }
2943
2944 return 0;
2945 }
2946
2947 /*
2948 * Handle SVI interface going down. At this point, this is a NOP since
2949 * the kernel deletes the neighbor entries on this SVI (if any).
2950 */
2951 int
2952 zebra_vxlan_svi_down (struct interface *ifp, struct interface *link_if)
2953 {
2954 return 0;
2955 }
2956
2957 /*
2958 * Handle SVI interface coming up. This may or may not be of interest,
2959 * but if this is a SVI on a VxLAN bridge, we need to install any remote
2960 * neighbor entries (which will be used for EVPN ARP suppression).
2961 */
2962 int
2963 zebra_vxlan_svi_up (struct interface *ifp, struct interface *link_if)
2964 {
2965 zebra_vni_t *zvni;
2966 struct neigh_walk_ctx n_wctx;
2967
2968 zvni = zvni_map_svi (ifp, link_if);
2969 if (!zvni)
2970 return 0;
2971
2972 if (!zvni->vxlan_if)
2973 {
2974 zlog_err ("VNI %u hash %p doesn't have intf upon SVI up",
2975 zvni->vni, zvni);
2976 return -1;
2977 }
2978
2979 if (IS_ZEBRA_DEBUG_VXLAN)
2980 zlog_debug ("%u:SVI %s(%u) VNI %u is UP, installing neighbors",
2981 ifp->vrf_id, ifp->name, ifp->ifindex, zvni->vni);
2982
2983 /* Install any remote neighbors for this VNI. */
2984 memset (&n_wctx, 0, sizeof (struct neigh_walk_ctx));
2985 n_wctx.zvni = zvni;
2986 hash_iterate(zvni->neigh_table, zvni_install_neigh_hash, &n_wctx);
2987
2988 return 0;
2989 }
2990
2991 /*
2992 * Handle VxLAN interface down - update BGP if required, and do
2993 * internal cleanup.
2994 */
2995 int
2996 zebra_vxlan_if_down (struct interface *ifp)
2997 {
2998 struct zebra_if *zif;
2999 struct zebra_vrf *zvrf;
3000 zebra_vni_t *zvni;
3001 struct zebra_l2info_vxlan *vxl;
3002 vni_t vni;
3003
3004 /* Locate VRF corresponding to interface. */
3005 zvrf = vrf_info_lookup(ifp->vrf_id);
3006 assert(zvrf);
3007
3008 /* If EVPN is not enabled, nothing further to be done. */
3009 if (!EVPN_ENABLED(zvrf))
3010 return 0;
3011
3012 zif = ifp->info;
3013 assert(zif);
3014 vxl = &zif->l2info.vxl;
3015 vni = vxl->vni;
3016
3017 if (IS_ZEBRA_DEBUG_VXLAN)
3018 zlog_debug ("%u:Intf %s(%u) VNI %u is DOWN",
3019 ifp->vrf_id, ifp->name, ifp->ifindex, vni);
3020
3021 /* Locate hash entry; it is expected to exist. */
3022 zvni = zvni_lookup (zvrf, vni);
3023 if (!zvni)
3024 {
3025 zlog_err ("Failed to locate VNI hash at DOWN, VRF %d IF %s(%u) VNI %u",
3026 ifp->vrf_id, ifp->name, ifp->ifindex, vni);
3027 return -1;
3028 }
3029
3030 assert (zvni->vxlan_if == ifp);
3031
3032 /* Delete this VNI from BGP. */
3033 zvni_send_del_to_client (zvrf, zvni->vni);
3034
3035 /* Free up all neighbors and MACs, if any. */
3036 zvni_neigh_del_all (zvrf, zvni, 1, 0, DEL_ALL_NEIGH);
3037 zvni_mac_del_all (zvrf, zvni, 1, 0, DEL_ALL_MAC);
3038
3039 /* Free up all remote VTEPs, if any. */
3040 zvni_vtep_del_all (zvni, 1);
3041
3042 return 0;
3043 }
3044
3045 /*
3046 * Handle VxLAN interface up - update BGP if required.
3047 */
3048 int
3049 zebra_vxlan_if_up (struct interface *ifp)
3050 {
3051 struct zebra_if *zif;
3052 struct zebra_vrf *zvrf;
3053 zebra_vni_t *zvni;
3054 struct zebra_l2info_vxlan *vxl;
3055 vni_t vni;
3056
3057 /* Locate VRF corresponding to interface. */
3058 zvrf = vrf_info_lookup(ifp->vrf_id);
3059 assert(zvrf);
3060
3061 /* If EVPN is not enabled, nothing further to be done. */
3062 if (!EVPN_ENABLED(zvrf))
3063 return 0;
3064
3065 zif = ifp->info;
3066 assert(zif);
3067 vxl = &zif->l2info.vxl;
3068 vni = vxl->vni;
3069
3070 if (IS_ZEBRA_DEBUG_VXLAN)
3071 zlog_debug ("%u:Intf %s(%u) VNI %u is UP",
3072 ifp->vrf_id, ifp->name, ifp->ifindex, vni);
3073
3074 /* Locate hash entry; it is expected to exist. */
3075 zvni = zvni_lookup (zvrf, vni);
3076 if (!zvni)
3077 {
3078 zlog_err ("Failed to locate VNI hash at UP, VRF %d IF %s(%u) VNI %u",
3079 ifp->vrf_id, ifp->name, ifp->ifindex, vni);
3080 return -1;
3081 }
3082
3083 assert (zvni->vxlan_if == ifp);
3084
3085 /* If part of a bridge, inform BGP about this VNI. */
3086 /* Also, read and populate local MACs and neighbors. */
3087 if (zif->brslave_info.br_if)
3088 {
3089 zvni_send_add_to_client (zvrf, zvni);
3090 zvni_read_mac_neigh (zvrf, zvni, ifp);
3091 }
3092
3093 return 0;
3094 }
3095
3096 /*
3097 * Handle VxLAN interface delete. Locate and remove entry in hash table
3098 * and update BGP, if required.
3099 */
3100 int
3101 zebra_vxlan_if_del (struct interface *ifp)
3102 {
3103 struct zebra_if *zif;
3104 struct zebra_vrf *zvrf;
3105 zebra_vni_t *zvni;
3106 struct zebra_l2info_vxlan *vxl;
3107 vni_t vni;
3108
3109 /* Locate VRF corresponding to interface. */
3110 zvrf = vrf_info_lookup(ifp->vrf_id);
3111 assert(zvrf);
3112
3113 /* If EVPN is not enabled, nothing further to be done. */
3114 if (!EVPN_ENABLED(zvrf))
3115 return 0;
3116
3117 zif = ifp->info;
3118 assert(zif);
3119 vxl = &zif->l2info.vxl;
3120 vni = vxl->vni;
3121
3122 if (IS_ZEBRA_DEBUG_VXLAN)
3123 zlog_debug ("%u:Del VNI %u intf %s(%u)",
3124 ifp->vrf_id, vni, ifp->name, ifp->ifindex);
3125
3126 /* Locate hash entry; it is expected to exist. */
3127 zvni = zvni_lookup (zvrf, vni);
3128 if (!zvni)
3129 {
3130 zlog_err ("Failed to locate VNI hash at del, VRF %d IF %s(%u) VNI %u",
3131 ifp->vrf_id, ifp->name, ifp->ifindex, vni);
3132 return 0;
3133 }
3134
3135 /* Delete VNI from BGP. */
3136 zvni_send_del_to_client (zvrf, zvni->vni);
3137
3138 /* Free up all neighbors and MAC, if any. */
3139 zvni_neigh_del_all (zvrf, zvni, 0, 0, DEL_ALL_NEIGH);
3140 zvni_mac_del_all (zvrf, zvni, 0, 0, DEL_ALL_MAC);
3141
3142 /* Free up all remote VTEPs, if any. */
3143 zvni_vtep_del_all (zvni, 0);
3144
3145 /* Delete the hash entry. */
3146 if (zvni_del (zvrf, zvni))
3147 {
3148 zlog_err ("Failed to del VNI hash %p, VRF %d IF %s(%u) VNI %u",
3149 zvni, ifp->vrf_id, ifp->name, ifp->ifindex, zvni->vni);
3150 return -1;
3151 }
3152
3153 return 0;
3154 }
3155
3156 /*
3157 * Handle VxLAN interface update - change to tunnel IP, master or VLAN.
3158 */
3159 int
3160 zebra_vxlan_if_update (struct interface *ifp, u_int16_t chgflags)
3161 {
3162 struct zebra_if *zif;
3163 struct zebra_vrf *zvrf;
3164 zebra_vni_t *zvni;
3165 struct zebra_l2info_vxlan *vxl;
3166 vni_t vni;
3167
3168 /* Locate VRF corresponding to interface. */
3169 zvrf = vrf_info_lookup(ifp->vrf_id);
3170 assert(zvrf);
3171
3172 /* If EVPN is not enabled, nothing further to be done. */
3173 if (!EVPN_ENABLED(zvrf))
3174 return 0;
3175
3176 zif = ifp->info;
3177 assert(zif);
3178 vxl = &zif->l2info.vxl;
3179 vni = vxl->vni;
3180
3181 /* Update VNI hash. */
3182 zvni = zvni_lookup (zvrf, vni);
3183 if (!zvni)
3184 {
3185 zlog_err ("Failed to find VNI hash on update, VRF %d IF %s(%u) VNI %u",
3186 ifp->vrf_id, ifp->name, ifp->ifindex, vni);
3187 return -1;
3188 }
3189
3190 if (IS_ZEBRA_DEBUG_VXLAN)
3191 zlog_debug ("%u:Update VNI %u intf %s(%u) VLAN %u local IP %s "
3192 "master %u chg 0x%x",
3193 ifp->vrf_id, vni, ifp->name, ifp->ifindex,
3194 vxl->access_vlan, inet_ntoa (vxl->vtep_ip),
3195 zif->brslave_info.bridge_ifindex, chgflags);
3196
3197 /* Removed from bridge? */
3198 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE) &&
3199 (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL))
3200 {
3201 /* Delete from client, remove all remote VTEPs */
3202 /* Also, free up all MACs and neighbors. */
3203 zvni_send_del_to_client (zvrf, zvni->vni);
3204 zvni_neigh_del_all (zvrf, zvni, 1, 0, DEL_ALL_NEIGH);
3205 zvni_mac_del_all (zvrf, zvni, 1, 0, DEL_ALL_MAC);
3206 zvni_vtep_del_all (zvni, 1);
3207 }
3208 else if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE)
3209 {
3210 /* Remove all existing local neighbors and MACs for this VNI
3211 * (including from BGP)
3212 */
3213 zvni_neigh_del_all (zvrf, zvni, 0, 1, DEL_LOCAL_MAC);
3214 zvni_mac_del_all (zvrf, zvni, 0, 1, DEL_LOCAL_MAC);
3215 }
3216
3217 zvni->local_vtep_ip = vxl->vtep_ip;
3218 zvni->vxlan_if = ifp;
3219
3220 /* Take further actions needed. Note that if we are here, there is a
3221 * change of interest.
3222 */
3223 /* If down or not mapped to a bridge, we're done. */
3224 if (!if_is_operative (ifp) || !zif->brslave_info.br_if)
3225 return 0;
3226
3227 /* Inform BGP, if there is a change of interest. */
3228 if (chgflags & (ZEBRA_VXLIF_MASTER_CHANGE | ZEBRA_VXLIF_LOCAL_IP_CHANGE))
3229 zvni_send_add_to_client (zvrf, zvni);
3230
3231 /* If there is a valid new master or a VLAN mapping change, read and
3232 * populate local MACs and neighbors. Also, reinstall any remote MACs
3233 * and neighbors for this VNI (based on new VLAN).
3234 */
3235 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
3236 zvni_read_mac_neigh (zvrf, zvni, ifp);
3237 else if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE)
3238 {
3239 struct mac_walk_ctx m_wctx;
3240 struct neigh_walk_ctx n_wctx;
3241
3242 zvni_read_mac_neigh (zvrf, zvni, ifp);
3243
3244 memset (&m_wctx, 0, sizeof (struct mac_walk_ctx));
3245 m_wctx.zvni = zvni;
3246 hash_iterate(zvni->mac_table, zvni_install_mac_hash, &m_wctx);
3247
3248 memset (&n_wctx, 0, sizeof (struct neigh_walk_ctx));
3249 n_wctx.zvni = zvni;
3250 hash_iterate(zvni->neigh_table, zvni_install_neigh_hash, &n_wctx);
3251 }
3252
3253 return 0;
3254 }
3255
3256 /*
3257 * Handle VxLAN interface add.
3258 */
3259 int
3260 zebra_vxlan_if_add (struct interface *ifp)
3261 {
3262 struct zebra_if *zif;
3263 struct zebra_vrf *zvrf;
3264 zebra_vni_t *zvni;
3265 struct zebra_l2info_vxlan *vxl;
3266 vni_t vni;
3267
3268 /* Locate VRF corresponding to interface. */
3269 zvrf = vrf_info_lookup(ifp->vrf_id);
3270 assert(zvrf);
3271
3272 /* If EVPN is not enabled, nothing further to be done. */
3273 if (!EVPN_ENABLED(zvrf))
3274 return 0;
3275
3276 zif = ifp->info;
3277 assert(zif);
3278 vxl = &zif->l2info.vxl;
3279 vni = vxl->vni;
3280
3281 if (IS_ZEBRA_DEBUG_VXLAN)
3282 zlog_debug ("%u:Add VNI %u intf %s(%u) VLAN %u local IP %s master %u",
3283 ifp->vrf_id, vni, ifp->name, ifp->ifindex,
3284 vxl->access_vlan, inet_ntoa (vxl->vtep_ip),
3285 zif->brslave_info.bridge_ifindex);
3286
3287 /* Create or update VNI hash. */
3288 zvni = zvni_lookup (zvrf, vni);
3289 if (!zvni)
3290 {
3291 zvni = zvni_add (zvrf, vni);
3292 if (!zvni)
3293 {
3294 zlog_err ("Failed to add VNI hash, VRF %d IF %s(%u) VNI %u",
3295 ifp->vrf_id, ifp->name, ifp->ifindex, vni);
3296 return -1;
3297 }
3298 }
3299
3300 zvni->local_vtep_ip = vxl->vtep_ip;
3301 zvni->vxlan_if = ifp;
3302
3303 /* If down or not mapped to a bridge, we're done. */
3304 if (!if_is_operative (ifp) || !zif->brslave_info.br_if)
3305 return 0;
3306
3307 /* Inform BGP */
3308 zvni_send_add_to_client (zvrf, zvni);
3309
3310 /* Read and populate local MACs and neighbors */
3311 zvni_read_mac_neigh (zvrf, zvni, ifp);
3312
3313 return 0;
3314 }
3315
3316 /*
3317 * Handle message from client to learn (or stop learning) about VNIs and MACs.
3318 * When enabled, the VNI hash table will be built and MAC FDB table read;
3319 * when disabled, the entries should be deleted and remote VTEPs and MACs
3320 * uninstalled from the kernel.
3321 */
3322 int zebra_vxlan_advertise_all_vni (struct zserv *client, int sock,
3323 u_short length, struct zebra_vrf *zvrf)
3324 {
3325 struct stream *s;
3326 int advertise;
3327
3328 s = client->ibuf;
3329 advertise = stream_getc (s);
3330
3331 if (IS_ZEBRA_DEBUG_VXLAN)
3332 zlog_debug ("%u:EVPN VNI Adv %s, currently %s",
3333 zvrf_id (zvrf), advertise ? "enabled" : "disabled",
3334 EVPN_ENABLED(zvrf) ? "enabled" : "disabled");
3335
3336 if (zvrf->advertise_all_vni == advertise)
3337 return 0;
3338
3339 zvrf->advertise_all_vni = advertise;
3340 if (EVPN_ENABLED(zvrf))
3341 {
3342 /* Build VNI hash table and inform BGP. */
3343 zvni_build_hash_table (zvrf);
3344
3345 /* Read the MAC FDB */
3346 macfdb_read (zvrf->zns);
3347
3348 /* Read neighbors */
3349 neigh_read (zvrf->zns);
3350 }
3351 else
3352 {
3353 /* Cleanup VTEPs for all VNIs - uninstall from
3354 * kernel and free entries.
3355 */
3356 hash_iterate (zvrf->vni_table, zvni_cleanup_all, zvrf);
3357 }
3358
3359 return 0;
3360 }
3361
3362 /*
3363 * Allocate VNI hash table for this VRF and do other initialization.
3364 * NOTE: Currently supported only for default VRF.
3365 */
3366 void
3367 zebra_vxlan_init_tables (struct zebra_vrf *zvrf)
3368 {
3369 if (!zvrf)
3370 return;
3371 zvrf->vni_table = hash_create(vni_hash_keymake,
3372 vni_hash_cmp,
3373 "Zebra VRF VNI Table");
3374 }
3375
3376 /* Close all VNI handling */
3377 void
3378 zebra_vxlan_close_tables (struct zebra_vrf *zvrf)
3379 {
3380 hash_iterate (zvrf->vni_table, zvni_cleanup_all, zvrf);
3381 }