]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vxlan.c
Merge pull request #5810 from donaldsharp/fix_yang_routemap
[mirror_frr.git] / zebra / zebra_vxlan.c
1 /*
2 * Zebra EVPN for VxLAN code
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
4 *
5 * This file is part of FRR.
6 *
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * FRR is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "hash.h"
26 #include "if.h"
27 #include "jhash.h"
28 #include "linklist.h"
29 #include "log.h"
30 #include "memory.h"
31 #include "prefix.h"
32 #include "stream.h"
33 #include "table.h"
34 #include "vlan.h"
35 #include "vxlan.h"
36 #ifdef GNU_LINUX
37 #include <linux/neighbour.h>
38 #endif
39
40 #include "zebra/zebra_router.h"
41 #include "zebra/debug.h"
42 #include "zebra/interface.h"
43 #include "zebra/rib.h"
44 #include "zebra/rt.h"
45 #include "zebra/rt_netlink.h"
46 #include "zebra/zebra_errors.h"
47 #include "zebra/zebra_l2.h"
48 #include "zebra/zebra_memory.h"
49 #include "zebra/zebra_ns.h"
50 #include "zebra/zebra_vrf.h"
51 #include "zebra/zebra_vxlan.h"
52 #include "zebra/zebra_vxlan_private.h"
53 #include "zebra/zebra_router.h"
54
55 DEFINE_MTYPE_STATIC(ZEBRA, HOST_PREFIX, "host prefix");
56 DEFINE_MTYPE_STATIC(ZEBRA, ZVNI, "VNI hash");
57 DEFINE_MTYPE_STATIC(ZEBRA, ZL3VNI, "L3 VNI hash");
58 DEFINE_MTYPE_STATIC(ZEBRA, ZVNI_VTEP, "VNI remote VTEP");
59 DEFINE_MTYPE_STATIC(ZEBRA, MAC, "VNI MAC");
60 DEFINE_MTYPE_STATIC(ZEBRA, NEIGH, "VNI Neighbor");
61 DEFINE_MTYPE_STATIC(ZEBRA, ZVXLAN_SG, "zebra VxLAN multicast group");
62
63 DEFINE_HOOK(zebra_rmac_update, (zebra_mac_t *rmac, zebra_l3vni_t *zl3vni,
64 bool delete, const char *reason), (rmac, zl3vni, delete, reason))
65
66 /* definitions */
67 /* PMSI strings. */
68 #define VXLAN_FLOOD_STR_NO_INFO "-"
69 #define VXLAN_FLOOD_STR_DEFAULT VXLAN_FLOOD_STR_NO_INFO
70 static const struct message zvtep_flood_str[] = {
71 {VXLAN_FLOOD_DISABLED, VXLAN_FLOOD_STR_NO_INFO},
72 {VXLAN_FLOOD_PIM_SM, "PIM-SM"},
73 {VXLAN_FLOOD_HEAD_END_REPL, "HER"},
74 {0}
75 };
76
77
78 /* static function declarations */
79 static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
80 uint16_t cmd);
81 static void zvni_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json);
82 static void zvni_print_neigh_hash(struct hash_bucket *bucket, void *ctxt);
83 static void zvni_print_dad_neigh_hash(struct hash_bucket *bucket, void *ctxt);
84 static void zvni_print_neigh_hash_all_vni(struct hash_bucket *bucket,
85 void **args);
86 static void zl3vni_print_nh(zebra_neigh_t *n, struct vty *vty,
87 json_object *json);
88 static void zl3vni_print_rmac(zebra_mac_t *zrmac, struct vty *vty,
89 json_object *json);
90 static void zvni_print_mac(zebra_mac_t *mac, void *ctxt, json_object *json);
91 static void zvni_print_mac_hash(struct hash_bucket *bucket, void *ctxt);
92 static void zvni_print_mac_hash_all_vni(struct hash_bucket *bucket, void *ctxt);
93 static void zvni_print(zebra_vni_t *zvni, void **ctxt);
94 static void zvni_print_hash(struct hash_bucket *bucket, void *ctxt[]);
95
96 static int zvni_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr,
97 struct ipaddr *ip, uint8_t flags,
98 uint32_t seq, int state, uint16_t cmd);
99 static unsigned int neigh_hash_keymake(const void *p);
100 static void *zvni_neigh_alloc(void *p);
101 static zebra_neigh_t *zvni_neigh_add(zebra_vni_t *zvni, struct ipaddr *ip,
102 struct ethaddr *mac);
103 static int zvni_neigh_del(zebra_vni_t *zvni, zebra_neigh_t *n);
104 static void zvni_neigh_del_from_vtep(zebra_vni_t *zvni, int uninstall,
105 struct in_addr *r_vtep_ip);
106 static void zvni_neigh_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
107 uint32_t flags);
108 static zebra_neigh_t *zvni_neigh_lookup(zebra_vni_t *zvni, struct ipaddr *ip);
109 static int zvni_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
110 struct ethaddr *macaddr,
111 uint8_t flags, uint32_t seq);
112 static int zvni_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
113 struct ethaddr *macaddr,
114 uint8_t flags, int state);
115 static int zvni_neigh_install(zebra_vni_t *zvni, zebra_neigh_t *n);
116 static int zvni_neigh_uninstall(zebra_vni_t *zvni, zebra_neigh_t *n);
117 static int zvni_neigh_probe(zebra_vni_t *zvni, zebra_neigh_t *n);
118 static zebra_vni_t *zvni_from_svi(struct interface *ifp,
119 struct interface *br_if);
120 static struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if);
121 static struct interface *zvni_map_to_macvlan(struct interface *br_if,
122 struct interface *svi_if);
123
124 /* l3-vni next-hop neigh related APIs */
125 static zebra_neigh_t *zl3vni_nh_lookup(zebra_l3vni_t *zl3vni,
126 struct ipaddr *ip);
127 static void *zl3vni_nh_alloc(void *p);
128 static zebra_neigh_t *zl3vni_nh_add(zebra_l3vni_t *zl3vni,
129 struct ipaddr *vtep_ip,
130 struct ethaddr *rmac);
131 static int zl3vni_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
132 static int zl3vni_nh_install(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
133 static int zl3vni_nh_uninstall(zebra_l3vni_t *zl3vni, zebra_neigh_t *n);
134
135 /* l3-vni rmac related APIs */
136 static void zl3vni_print_rmac_hash(struct hash_bucket *, void *);
137 static zebra_mac_t *zl3vni_rmac_lookup(zebra_l3vni_t *zl3vni,
138 struct ethaddr *rmac);
139 static void *zl3vni_rmac_alloc(void *p);
140 static zebra_mac_t *zl3vni_rmac_add(zebra_l3vni_t *zl3vni,
141 struct ethaddr *rmac);
142 static int zl3vni_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
143 static int zl3vni_rmac_install(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
144 static int zl3vni_rmac_uninstall(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac);
145
146 /* l3-vni related APIs*/
147 static zebra_l3vni_t *zl3vni_lookup(vni_t vni);
148 static void *zl3vni_alloc(void *p);
149 static zebra_l3vni_t *zl3vni_add(vni_t vni, vrf_id_t vrf_id);
150 static int zl3vni_del(zebra_l3vni_t *zl3vni);
151 static void zebra_vxlan_process_l3vni_oper_up(zebra_l3vni_t *zl3vni);
152 static void zebra_vxlan_process_l3vni_oper_down(zebra_l3vni_t *zl3vni);
153
154 static unsigned int mac_hash_keymake(const void *p);
155 static bool mac_cmp(const void *p1, const void *p2);
156 static void *zvni_mac_alloc(void *p);
157 static zebra_mac_t *zvni_mac_add(zebra_vni_t *zvni, struct ethaddr *macaddr);
158 static int zvni_mac_del(zebra_vni_t *zvni, zebra_mac_t *mac);
159 static void zvni_mac_del_from_vtep(zebra_vni_t *zvni, int uninstall,
160 struct in_addr *r_vtep_ip);
161 static void zvni_mac_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
162 uint32_t flags);
163 static zebra_mac_t *zvni_mac_lookup(zebra_vni_t *zvni, struct ethaddr *macaddr);
164 static int zvni_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr,
165 uint8_t flags, uint32_t seq);
166 static int zvni_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr);
167 static zebra_vni_t *zvni_map_vlan(struct interface *ifp,
168 struct interface *br_if, vlanid_t vid);
169 static int zvni_mac_install(zebra_vni_t *zvni, zebra_mac_t *mac);
170 static int zvni_mac_uninstall(zebra_vni_t *zvni, zebra_mac_t *mac);
171 static void zvni_install_mac_hash(struct hash_bucket *bucket, void *ctxt);
172
173 static unsigned int vni_hash_keymake(const void *p);
174 static void *zvni_alloc(void *p);
175 static zebra_vni_t *zvni_lookup(vni_t vni);
176 static zebra_vni_t *zvni_add(vni_t vni);
177 static int zvni_del(zebra_vni_t *zvni);
178 static int zvni_send_add_to_client(zebra_vni_t *zvni);
179 static int zvni_send_del_to_client(vni_t vni);
180 static void zvni_build_hash_table(void);
181 static int zvni_vtep_match(struct in_addr *vtep_ip, zebra_vtep_t *zvtep);
182 static zebra_vtep_t *zvni_vtep_find(zebra_vni_t *zvni, struct in_addr *vtep_ip);
183 static zebra_vtep_t *zvni_vtep_add(zebra_vni_t *zvni, struct in_addr *vtep_ip,
184 int flood_control);
185 static int zvni_vtep_del(zebra_vni_t *zvni, zebra_vtep_t *zvtep);
186 static int zvni_vtep_del_all(zebra_vni_t *zvni, int uninstall);
187 static int zvni_vtep_install(zebra_vni_t *zvni, zebra_vtep_t *zvtep);
188 static int zvni_vtep_uninstall(zebra_vni_t *zvni, struct in_addr *vtep_ip);
189 static int zvni_del_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni);
190 static int zvni_add_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni);
191 static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni,
192 struct ethaddr *macaddr, struct ipaddr *ip);
193 static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni,
194 struct ipaddr *ip);
195 struct interface *zebra_get_vrr_intf_for_svi(struct interface *ifp);
196 static int advertise_gw_macip_enabled(zebra_vni_t *zvni);
197 static int advertise_svi_macip_enabled(zebra_vni_t *zvni);
198 static int zebra_vxlan_ip_inherit_dad_from_mac(struct zebra_vrf *zvrf,
199 zebra_mac_t *old_zmac,
200 zebra_mac_t *new_zmac,
201 zebra_neigh_t *nbr);
202 static int remote_neigh_count(zebra_mac_t *zmac);
203 static void zvni_deref_ip2mac(zebra_vni_t *zvni, zebra_mac_t *mac);
204 static int zebra_vxlan_dad_mac_auto_recovery_exp(struct thread *t);
205 static int zebra_vxlan_dad_ip_auto_recovery_exp(struct thread *t);
206 static void zebra_vxlan_dup_addr_detect_for_neigh(struct zebra_vrf *zvrf,
207 zebra_neigh_t *nbr,
208 struct in_addr vtep_ip,
209 bool do_dad,
210 bool *is_dup_detect,
211 bool is_local);
212 static void zebra_vxlan_dup_addr_detect_for_mac(struct zebra_vrf *zvrf,
213 zebra_mac_t *mac,
214 struct in_addr vtep_ip,
215 bool do_dad,
216 bool *is_dup_detect,
217 bool is_local);
218 static unsigned int zebra_vxlan_sg_hash_key_make(const void *p);
219 static bool zebra_vxlan_sg_hash_eq(const void *p1, const void *p2);
220 static void zebra_vxlan_sg_do_deref(struct zebra_vrf *zvrf,
221 struct in_addr sip, struct in_addr mcast_grp);
222 static zebra_vxlan_sg_t *zebra_vxlan_sg_do_ref(struct zebra_vrf *vrf,
223 struct in_addr sip, struct in_addr mcast_grp);
224 static void zebra_vxlan_sg_deref(struct in_addr local_vtep_ip,
225 struct in_addr mcast_grp);
226 static void zebra_vxlan_sg_ref(struct in_addr local_vtep_ip,
227 struct in_addr mcast_grp);
228 static void zebra_vxlan_sg_cleanup(struct hash_backet *backet, void *arg);
229
230 static void zvni_send_mac_to_client(zebra_vni_t *zvn);
231 static void zvni_send_neigh_to_client(zebra_vni_t *zvni);
232
233 /* Private functions */
234 static int host_rb_entry_compare(const struct host_rb_entry *hle1,
235 const struct host_rb_entry *hle2)
236 {
237 if (hle1->p.family < hle2->p.family)
238 return -1;
239
240 if (hle1->p.family > hle2->p.family)
241 return 1;
242
243 if (hle1->p.prefixlen < hle2->p.prefixlen)
244 return -1;
245
246 if (hle1->p.prefixlen > hle2->p.prefixlen)
247 return 1;
248
249 if (hle1->p.family == AF_INET) {
250 if (hle1->p.u.prefix4.s_addr < hle2->p.u.prefix4.s_addr)
251 return -1;
252
253 if (hle1->p.u.prefix4.s_addr > hle2->p.u.prefix4.s_addr)
254 return 1;
255
256 return 0;
257 } else if (hle1->p.family == AF_INET6) {
258 return memcmp(&hle1->p.u.prefix6, &hle2->p.u.prefix6,
259 IPV6_MAX_BYTELEN);
260 } else {
261 zlog_debug("%s: Unexpected family type: %d",
262 __PRETTY_FUNCTION__, hle1->p.family);
263 return 0;
264 }
265 }
266 RB_GENERATE(host_rb_tree_entry, host_rb_entry, hl_entry, host_rb_entry_compare);
267
268 static uint32_t rb_host_count(struct host_rb_tree_entry *hrbe)
269 {
270 struct host_rb_entry *hle;
271 uint32_t count = 0;
272
273 RB_FOREACH (hle, host_rb_tree_entry, hrbe)
274 count++;
275
276 return count;
277 }
278
279 /*
280 * Return number of valid MACs in a VNI's MAC hash table - all
281 * remote MACs and non-internal (auto) local MACs count.
282 */
283 static uint32_t num_valid_macs(zebra_vni_t *zvni)
284 {
285 unsigned int i;
286 uint32_t num_macs = 0;
287 struct hash *hash;
288 struct hash_bucket *hb;
289 zebra_mac_t *mac;
290
291 hash = zvni->mac_table;
292 if (!hash)
293 return num_macs;
294 for (i = 0; i < hash->size; i++) {
295 for (hb = hash->index[i]; hb; hb = hb->next) {
296 mac = (zebra_mac_t *)hb->data;
297 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
298 || CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
299 || !CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
300 num_macs++;
301 }
302 }
303
304 return num_macs;
305 }
306
307 static uint32_t num_dup_detected_macs(zebra_vni_t *zvni)
308 {
309 unsigned int i;
310 uint32_t num_macs = 0;
311 struct hash *hash;
312 struct hash_bucket *hb;
313 zebra_mac_t *mac;
314
315 hash = zvni->mac_table;
316 if (!hash)
317 return num_macs;
318 for (i = 0; i < hash->size; i++) {
319 for (hb = hash->index[i]; hb; hb = hb->next) {
320 mac = (zebra_mac_t *)hb->data;
321 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
322 num_macs++;
323 }
324 }
325
326 return num_macs;
327 }
328
329 static uint32_t num_dup_detected_neighs(zebra_vni_t *zvni)
330 {
331 unsigned int i;
332 uint32_t num_neighs = 0;
333 struct hash *hash;
334 struct hash_bucket *hb;
335 zebra_neigh_t *nbr;
336
337 hash = zvni->neigh_table;
338 if (!hash)
339 return num_neighs;
340 for (i = 0; i < hash->size; i++) {
341 for (hb = hash->index[i]; hb; hb = hb->next) {
342 nbr = (zebra_neigh_t *)hb->data;
343 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
344 num_neighs++;
345 }
346 }
347
348 return num_neighs;
349 }
350
351 static int advertise_gw_macip_enabled(zebra_vni_t *zvni)
352 {
353 struct zebra_vrf *zvrf;
354
355 zvrf = zebra_vrf_get_evpn();
356 if (zvrf && zvrf->advertise_gw_macip)
357 return 1;
358
359 if (zvni && zvni->advertise_gw_macip)
360 return 1;
361
362 return 0;
363 }
364
365 static int advertise_svi_macip_enabled(zebra_vni_t *zvni)
366 {
367 struct zebra_vrf *zvrf;
368
369 zvrf = zebra_vrf_get_evpn();
370 if (zvrf && zvrf->advertise_svi_macip)
371 return 1;
372
373 if (zvni && zvni->advertise_svi_macip)
374 return 1;
375
376 return 0;
377 }
378
379 /* As part Duplicate Address Detection (DAD) for IP mobility
380 * MAC binding changes, ensure to inherit duplicate flag
381 * from MAC.
382 */
383 static int zebra_vxlan_ip_inherit_dad_from_mac(struct zebra_vrf *zvrf,
384 zebra_mac_t *old_zmac,
385 zebra_mac_t *new_zmac,
386 zebra_neigh_t *nbr)
387 {
388 bool is_old_mac_dup = false;
389 bool is_new_mac_dup = false;
390
391 if (!zvrf->dup_addr_detect)
392 return 0;
393 /* Check old or new MAC is detected as duplicate
394 * mark this neigh as duplicate
395 */
396 if (old_zmac)
397 is_old_mac_dup = CHECK_FLAG(old_zmac->flags,
398 ZEBRA_MAC_DUPLICATE);
399 if (new_zmac)
400 is_new_mac_dup = CHECK_FLAG(new_zmac->flags,
401 ZEBRA_MAC_DUPLICATE);
402 /* Old and/or new MAC can be in duplicate state,
403 * based on that IP/Neigh Inherits the flag.
404 * If New MAC is marked duplicate, inherit to the IP.
405 * If old MAC is duplicate but new MAC is not, clear
406 * duplicate flag for IP and reset detection params
407 * and let IP DAD retrigger.
408 */
409 if (is_new_mac_dup && !CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
410 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
411 /* Capture Duplicate detection time */
412 nbr->dad_dup_detect_time = monotime(NULL);
413 /* Mark neigh inactive */
414 ZEBRA_NEIGH_SET_INACTIVE(nbr);
415
416 return 1;
417 } else if (is_old_mac_dup && !is_new_mac_dup) {
418 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
419 nbr->dad_count = 0;
420 nbr->detect_start_time.tv_sec = 0;
421 nbr->detect_start_time.tv_usec = 0;
422 }
423 return 0;
424 }
425
426 static void zebra_vxlan_dup_addr_detect_for_mac(struct zebra_vrf *zvrf,
427 zebra_mac_t *mac,
428 struct in_addr vtep_ip,
429 bool do_dad,
430 bool *is_dup_detect,
431 bool is_local)
432 {
433 zebra_neigh_t *nbr;
434 struct listnode *node = NULL;
435 struct timeval elapsed = {0, 0};
436 char buf[ETHER_ADDR_STRLEN];
437 char buf1[INET6_ADDRSTRLEN];
438 bool reset_params = false;
439
440 if (!(zvrf->dup_addr_detect && do_dad))
441 return;
442
443 /* MAC is detected as duplicate,
444 * Local MAC event -> hold on advertising to BGP.
445 * Remote MAC event -> hold on installing it.
446 */
447 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
448 if (IS_ZEBRA_DEBUG_VXLAN)
449 zlog_debug(
450 "%s: duplicate addr MAC %s flags 0x%x skip update to client, learn count %u recover time %u",
451 __PRETTY_FUNCTION__,
452 prefix_mac2str(&mac->macaddr, buf,
453 sizeof(buf)),
454 mac->flags, mac->dad_count,
455 zvrf->dad_freeze_time);
456
457 /* For duplicate MAC do not update
458 * client but update neigh due to
459 * this MAC update.
460 */
461 if (zvrf->dad_freeze)
462 *is_dup_detect = true;
463
464 return;
465 }
466
467 /* Check if detection time (M-secs) expired.
468 * Reset learn count and detection start time.
469 */
470 monotime_since(&mac->detect_start_time, &elapsed);
471 reset_params = (elapsed.tv_sec > zvrf->dad_time);
472 if (is_local && !reset_params) {
473 /* RFC-7432: A PE/VTEP that detects a MAC mobility
474 * event via LOCAL learning starts an M-second timer.
475 *
476 * NOTE: This is the START of the probe with count is
477 * 0 during LOCAL learn event.
478 * (mac->dad_count == 0 || elapsed.tv_sec >= zvrf->dad_time)
479 */
480 reset_params = !mac->dad_count;
481 }
482
483 if (reset_params) {
484 if (IS_ZEBRA_DEBUG_VXLAN)
485 zlog_debug(
486 "%s: duplicate addr MAC %s flags 0x%x detection time passed, reset learn count %u"
487 , __PRETTY_FUNCTION__,
488 prefix_mac2str(&mac->macaddr, buf,
489 sizeof(buf)),
490 mac->flags, mac->dad_count);
491
492 mac->dad_count = 0;
493 /* Start dup. addr detection (DAD) start time,
494 * ONLY during LOCAL learn.
495 */
496 if (is_local)
497 monotime(&mac->detect_start_time);
498
499 } else if (!is_local) {
500 /* For REMOTE MAC, increment detection count
501 * ONLY while in probe window, once window passed,
502 * next local learn event should trigger DAD.
503 */
504 mac->dad_count++;
505 }
506
507 /* For LOCAL MAC learn event, once count is reset above via either
508 * initial/start detection time or passed the probe time, the count
509 * needs to be incremented.
510 */
511 if (is_local)
512 mac->dad_count++;
513
514 if (mac->dad_count >= zvrf->dad_max_moves) {
515 flog_warn(EC_ZEBRA_DUP_MAC_DETECTED,
516 "VNI %u: MAC %s detected as duplicate during %s VTEP %s",
517 mac->zvni->vni,
518 prefix_mac2str(&mac->macaddr, buf, sizeof(buf)),
519 is_local ? "local update, last" :
520 "remote update, from", inet_ntoa(vtep_ip));
521
522 SET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
523
524 /* Capture Duplicate detection time */
525 mac->dad_dup_detect_time = monotime(NULL);
526
527 /* Mark all IPs/Neighs as duplicate
528 * associcated with this MAC
529 */
530 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
531
532 /* Ony Mark IPs which are Local */
533 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
534 continue;
535
536 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
537
538 nbr->dad_dup_detect_time = monotime(NULL);
539
540 flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
541 "VNI %u: MAC %s IP %s detected as duplicate during %s update, inherit duplicate from MAC",
542 mac->zvni->vni,
543 prefix_mac2str(&mac->macaddr,
544 buf, sizeof(buf)),
545 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
546 is_local ? "local" : "remote");
547 }
548
549 /* Start auto recovery timer for this MAC */
550 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
551 if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
552 if (IS_ZEBRA_DEBUG_VXLAN)
553 zlog_debug(
554 "%s: duplicate addr MAC %s flags 0x%x auto recovery time %u start"
555 , __PRETTY_FUNCTION__,
556 prefix_mac2str(&mac->macaddr, buf,
557 sizeof(buf)),
558 mac->flags, zvrf->dad_freeze_time);
559
560 thread_add_timer(zrouter.master,
561 zebra_vxlan_dad_mac_auto_recovery_exp,
562 mac, zvrf->dad_freeze_time,
563 &mac->dad_mac_auto_recovery_timer);
564 }
565
566 /* In case of local update, do not inform to client (BGPd),
567 * upd_neigh for neigh sequence change.
568 */
569 if (zvrf->dad_freeze)
570 *is_dup_detect = true;
571 }
572 }
573
574 static void zebra_vxlan_dup_addr_detect_for_neigh(struct zebra_vrf *zvrf,
575 zebra_neigh_t *nbr,
576 struct in_addr vtep_ip,
577 bool do_dad,
578 bool *is_dup_detect,
579 bool is_local)
580 {
581
582 struct timeval elapsed = {0, 0};
583 char buf[ETHER_ADDR_STRLEN];
584 char buf1[INET6_ADDRSTRLEN];
585 bool reset_params = false;
586
587 if (!zvrf->dup_addr_detect)
588 return;
589
590 /* IP is detected as duplicate or inherit dup
591 * state, hold on to install as remote entry
592 * only if freeze is enabled.
593 */
594 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
595 if (IS_ZEBRA_DEBUG_VXLAN)
596 zlog_debug(
597 "%s: duplicate addr MAC %s IP %s flags 0x%x skip installing, learn count %u recover time %u",
598 __PRETTY_FUNCTION__,
599 prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
600 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
601 nbr->flags, nbr->dad_count,
602 zvrf->dad_freeze_time);
603
604 if (zvrf->dad_freeze)
605 *is_dup_detect = true;
606
607 /* warn-only action, neigh will be installed.
608 * freeze action, it wil not be installed.
609 */
610 return;
611 }
612
613 if (!do_dad)
614 return;
615
616 /* Check if detection time (M-secs) expired.
617 * Reset learn count and detection start time.
618 * During remote mac add, count should already be 1
619 * via local learning.
620 */
621 monotime_since(&nbr->detect_start_time, &elapsed);
622 reset_params = (elapsed.tv_sec > zvrf->dad_time);
623
624 if (is_local && !reset_params) {
625 /* RFC-7432: A PE/VTEP that detects a MAC mobility
626 * event via LOCAL learning starts an M-second timer.
627 *
628 * NOTE: This is the START of the probe with count is
629 * 0 during LOCAL learn event.
630 */
631 reset_params = !nbr->dad_count;
632 }
633
634 if (reset_params) {
635 if (IS_ZEBRA_DEBUG_VXLAN)
636 zlog_debug(
637 "%s: duplicate addr MAC %s IP %s flags 0x%x detection time passed, reset learn count %u",
638 __PRETTY_FUNCTION__,
639 prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
640 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
641 nbr->flags, nbr->dad_count);
642 /* Reset learn count but do not start detection
643 * during REMOTE learn event.
644 */
645 nbr->dad_count = 0;
646 /* Start dup. addr detection (DAD) start time,
647 * ONLY during LOCAL learn.
648 */
649 if (is_local)
650 monotime(&nbr->detect_start_time);
651
652 } else if (!is_local) {
653 /* For REMOTE IP/Neigh, increment detection count
654 * ONLY while in probe window, once window passed,
655 * next local learn event should trigger DAD.
656 */
657 nbr->dad_count++;
658 }
659
660 /* For LOCAL IP/Neigh learn event, once count is reset above via either
661 * initial/start detection time or passed the probe time, the count
662 * needs to be incremented.
663 */
664 if (is_local)
665 nbr->dad_count++;
666
667 if (nbr->dad_count >= zvrf->dad_max_moves) {
668 flog_warn(EC_ZEBRA_DUP_IP_DETECTED,
669 "VNI %u: MAC %s IP %s detected as duplicate during %s VTEP %s",
670 nbr->zvni->vni,
671 prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
672 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
673 is_local ? "local update, last" :
674 "remote update, from",
675 inet_ntoa(vtep_ip));
676
677 SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
678
679 /* Capture Duplicate detection time */
680 nbr->dad_dup_detect_time = monotime(NULL);
681
682 /* Start auto recovery timer for this IP */
683 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
684 if (zvrf->dad_freeze && zvrf->dad_freeze_time) {
685 if (IS_ZEBRA_DEBUG_VXLAN)
686 zlog_debug(
687 "%s: duplicate addr MAC %s IP %s flags 0x%x auto recovery time %u start",
688 __PRETTY_FUNCTION__,
689 prefix_mac2str(&nbr->emac, buf, sizeof(buf)),
690 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
691 nbr->flags, zvrf->dad_freeze_time);
692
693 thread_add_timer(zrouter.master,
694 zebra_vxlan_dad_ip_auto_recovery_exp,
695 nbr, zvrf->dad_freeze_time,
696 &nbr->dad_ip_auto_recovery_timer);
697 }
698 if (zvrf->dad_freeze)
699 *is_dup_detect = true;
700 }
701 }
702
703 /*
704 * Helper function to determine maximum width of neighbor IP address for
705 * display - just because we're dealing with IPv6 addresses that can
706 * widely vary.
707 */
708 static void zvni_find_neigh_addr_width(struct hash_bucket *bucket, void *ctxt)
709 {
710 zebra_neigh_t *n;
711 char buf[INET6_ADDRSTRLEN];
712 struct neigh_walk_ctx *wctx = ctxt;
713 int width;
714
715 n = (zebra_neigh_t *)bucket->data;
716
717 ipaddr2str(&n->ip, buf, sizeof(buf));
718 width = strlen(buf);
719 if (width > wctx->addr_width)
720 wctx->addr_width = width;
721
722 }
723
724 /*
725 * Print a specific neighbor entry.
726 */
727 static void zvni_print_neigh(zebra_neigh_t *n, void *ctxt, json_object *json)
728 {
729 struct vty *vty;
730 char buf1[ETHER_ADDR_STRLEN];
731 char buf2[INET6_ADDRSTRLEN];
732 const char *type_str;
733 const char *state_str;
734 bool flags_present = false;
735 struct zebra_vrf *zvrf = NULL;
736 struct timeval detect_start_time = {0, 0};
737 char timebuf[MONOTIME_STRLEN];
738
739 zvrf = zebra_vrf_get_evpn();
740 if (!zvrf)
741 return;
742
743 ipaddr2str(&n->ip, buf2, sizeof(buf2));
744 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
745 type_str = CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) ?
746 "local" : "remote";
747 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
748 vty = (struct vty *)ctxt;
749 if (json == NULL) {
750 vty_out(vty, "IP: %s\n",
751 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
752 vty_out(vty, " Type: %s\n", type_str);
753 vty_out(vty, " State: %s\n", state_str);
754 vty_out(vty, " MAC: %s\n",
755 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
756 } else {
757 json_object_string_add(json, "ip", buf2);
758 json_object_string_add(json, "type", type_str);
759 json_object_string_add(json, "state", state_str);
760 json_object_string_add(json, "mac", buf1);
761 }
762 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
763 if (json == NULL) {
764 vty_out(vty, " Remote VTEP: %s\n",
765 inet_ntoa(n->r_vtep_ip));
766 } else
767 json_object_string_add(json, "remoteVtep",
768 inet_ntoa(n->r_vtep_ip));
769 }
770 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW)) {
771 if (!json) {
772 vty_out(vty, " Flags: Default-gateway");
773 flags_present = true;
774 } else
775 json_object_boolean_true_add(json, "defaultGateway");
776 }
777 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)) {
778 if (!json) {
779 vty_out(vty,
780 flags_present ? " ,Router" : " Flags: Router");
781 flags_present = true;
782 }
783 }
784 if (json == NULL) {
785 if (flags_present)
786 vty_out(vty, "\n");
787 vty_out(vty, " Local Seq: %u Remote Seq: %u\n",
788 n->loc_seq, n->rem_seq);
789
790 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE)) {
791 vty_out(vty, " Duplicate, detected at %s",
792 time_to_string(n->dad_dup_detect_time,
793 timebuf));
794 } else if (n->dad_count) {
795 monotime_since(&n->detect_start_time,
796 &detect_start_time);
797 if (detect_start_time.tv_sec <= zvrf->dad_time) {
798 time_to_string(n->detect_start_time.tv_sec,
799 timebuf);
800 vty_out(vty,
801 " Duplicate detection started at %s, detection count %u\n",
802 timebuf, n->dad_count);
803 }
804 }
805 } else {
806 json_object_int_add(json, "localSequence", n->loc_seq);
807 json_object_int_add(json, "remoteSequence", n->rem_seq);
808 json_object_int_add(json, "detectionCount",
809 n->dad_count);
810 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
811 json_object_boolean_true_add(json, "isDuplicate");
812 else
813 json_object_boolean_false_add(json, "isDuplicate");
814
815
816 }
817 }
818
819 /*
820 * Print neighbor hash entry - called for display of all neighbors.
821 */
822 static void zvni_print_neigh_hash(struct hash_bucket *bucket, void *ctxt)
823 {
824 struct vty *vty;
825 json_object *json_vni = NULL, *json_row = NULL;
826 zebra_neigh_t *n;
827 char buf1[ETHER_ADDR_STRLEN];
828 char buf2[INET6_ADDRSTRLEN];
829 struct neigh_walk_ctx *wctx = ctxt;
830 const char *state_str;
831
832 vty = wctx->vty;
833 json_vni = wctx->json;
834 n = (zebra_neigh_t *)bucket->data;
835
836 if (json_vni)
837 json_row = json_object_new_object();
838
839 prefix_mac2str(&n->emac, buf1, sizeof(buf1));
840 ipaddr2str(&n->ip, buf2, sizeof(buf2));
841 state_str = IS_ZEBRA_NEIGH_ACTIVE(n) ? "active" : "inactive";
842 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
843 if (wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP)
844 return;
845
846 if (json_vni == NULL) {
847 vty_out(vty, "%*s %-6s %-8s %-17s %u/%u\n",
848 -wctx->addr_width, buf2, "local",
849 state_str, buf1, n->loc_seq, n->rem_seq);
850 } else {
851 json_object_string_add(json_row, "type", "local");
852 json_object_string_add(json_row, "state", state_str);
853 json_object_string_add(json_row, "mac", buf1);
854 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
855 json_object_boolean_true_add(
856 json_row, "defaultGateway");
857 json_object_int_add(json_row, "localSequence",
858 n->loc_seq);
859 json_object_int_add(json_row, "remoteSequence",
860 n->rem_seq);
861 json_object_int_add(json_row, "detectionCount",
862 n->dad_count);
863 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
864 json_object_boolean_true_add(json_row,
865 "isDuplicate");
866 else
867 json_object_boolean_false_add(json_row,
868 "isDuplicate");
869 }
870 wctx->count++;
871 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
872 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP) &&
873 !IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))
874 return;
875
876 if (json_vni == NULL) {
877 if ((wctx->flags & SHOW_REMOTE_NEIGH_FROM_VTEP) &&
878 (wctx->count == 0))
879 vty_out(vty, "%*s %-6s %-8s %-17s %-21s %s\n",
880 -wctx->addr_width, "Neighbor", "Type",
881 "State", "MAC", "Remote VTEP",
882 "Seq #'s");
883 vty_out(vty, "%*s %-6s %-8s %-17s %-21s %u/%u\n",
884 -wctx->addr_width, buf2, "remote", state_str,
885 buf1, inet_ntoa(n->r_vtep_ip), n->loc_seq, n->rem_seq);
886 } else {
887 json_object_string_add(json_row, "type", "remote");
888 json_object_string_add(json_row, "state", state_str);
889 json_object_string_add(json_row, "mac", buf1);
890 json_object_string_add(json_row, "remoteVtep",
891 inet_ntoa(n->r_vtep_ip));
892 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW))
893 json_object_boolean_true_add(json_row,
894 "defaultGateway");
895 json_object_int_add(json_row, "localSequence",
896 n->loc_seq);
897 json_object_int_add(json_row, "remoteSequence",
898 n->rem_seq);
899 json_object_int_add(json_row, "detectionCount",
900 n->dad_count);
901 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
902 json_object_boolean_true_add(json_row,
903 "isDuplicate");
904 else
905 json_object_boolean_false_add(json_row,
906 "isDuplicate");
907 }
908 wctx->count++;
909 }
910
911 if (json_vni)
912 json_object_object_add(json_vni, buf2, json_row);
913 }
914
915 /*
916 * Print neighbor hash entry in detail - called for display of all neighbors.
917 */
918 static void zvni_print_neigh_hash_detail(struct hash_bucket *bucket, void *ctxt)
919 {
920 struct vty *vty;
921 json_object *json_vni = NULL, *json_row = NULL;
922 zebra_neigh_t *n;
923 char buf[INET6_ADDRSTRLEN];
924 struct neigh_walk_ctx *wctx = ctxt;
925
926 vty = wctx->vty;
927 json_vni = wctx->json;
928 n = (zebra_neigh_t *)bucket->data;
929 if (!n)
930 return;
931
932 ipaddr2str(&n->ip, buf, sizeof(buf));
933 if (json_vni)
934 json_row = json_object_new_object();
935
936 zvni_print_neigh(n, vty, json_row);
937
938 if (json_vni)
939 json_object_object_add(json_vni, buf, json_row);
940 }
941
942 /*
943 * Print neighbors for all VNI.
944 */
945 static void zvni_print_neigh_hash_all_vni(struct hash_bucket *bucket,
946 void **args)
947 {
948 struct vty *vty;
949 json_object *json = NULL, *json_vni = NULL;
950 zebra_vni_t *zvni;
951 uint32_t num_neigh;
952 struct neigh_walk_ctx wctx;
953 char vni_str[VNI_STR_LEN];
954 uint32_t print_dup;
955
956 vty = (struct vty *)args[0];
957 json = (json_object *)args[1];
958 print_dup = (uint32_t)(uintptr_t)args[2];
959
960 zvni = (zebra_vni_t *)bucket->data;
961
962 num_neigh = hashcount(zvni->neigh_table);
963
964 if (print_dup)
965 num_neigh = num_dup_detected_neighs(zvni);
966
967 if (json == NULL) {
968 vty_out(vty,
969 "\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
970 zvni->vni, num_neigh);
971 } else {
972 json_vni = json_object_new_object();
973 json_object_int_add(json_vni, "numArpNd", num_neigh);
974 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
975 }
976
977 if (!num_neigh) {
978 if (json)
979 json_object_object_add(json, vni_str, json_vni);
980 return;
981 }
982
983 /* Since we have IPv6 addresses to deal with which can vary widely in
984 * size, we try to be a bit more elegant in display by first computing
985 * the maximum width.
986 */
987 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
988 wctx.zvni = zvni;
989 wctx.vty = vty;
990 wctx.addr_width = 15;
991 wctx.json = json_vni;
992 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
993
994 if (json == NULL) {
995 vty_out(vty, "%*s %-6s %-8s %-17s %-21s %s\n",
996 -wctx.addr_width, "IP", "Type",
997 "State", "MAC", "Remote VTEP", "Seq #'s");
998 }
999 if (print_dup)
1000 hash_iterate(zvni->neigh_table, zvni_print_dad_neigh_hash,
1001 &wctx);
1002 else
1003 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
1004
1005 if (json)
1006 json_object_object_add(json, vni_str, json_vni);
1007 }
1008
1009 static void zvni_print_dad_neigh_hash(struct hash_bucket *bucket, void *ctxt)
1010 {
1011 zebra_neigh_t *nbr;
1012
1013 nbr = (zebra_neigh_t *)bucket->data;
1014 if (!nbr)
1015 return;
1016
1017 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
1018 zvni_print_neigh_hash(bucket, ctxt);
1019 }
1020
1021 static void zvni_print_dad_neigh_hash_detail(struct hash_bucket *bucket,
1022 void *ctxt)
1023 {
1024 zebra_neigh_t *nbr;
1025
1026 nbr = (zebra_neigh_t *)bucket->data;
1027 if (!nbr)
1028 return;
1029
1030 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
1031 zvni_print_neigh_hash_detail(bucket, ctxt);
1032 }
1033
1034 /*
1035 * Print neighbors for all VNIs in detail.
1036 */
1037 static void zvni_print_neigh_hash_all_vni_detail(struct hash_bucket *bucket,
1038 void **args)
1039 {
1040 struct vty *vty;
1041 json_object *json = NULL, *json_vni = NULL;
1042 zebra_vni_t *zvni;
1043 uint32_t num_neigh;
1044 struct neigh_walk_ctx wctx;
1045 char vni_str[VNI_STR_LEN];
1046 uint32_t print_dup;
1047
1048 vty = (struct vty *)args[0];
1049 json = (json_object *)args[1];
1050 print_dup = (uint32_t)(uintptr_t)args[2];
1051
1052 zvni = (zebra_vni_t *)bucket->data;
1053 if (!zvni) {
1054 if (json)
1055 vty_out(vty, "{}\n");
1056 return;
1057 }
1058 num_neigh = hashcount(zvni->neigh_table);
1059
1060 if (print_dup && num_dup_detected_neighs(zvni) == 0)
1061 return;
1062
1063 if (json == NULL) {
1064 vty_out(vty,
1065 "\nVNI %u #ARP (IPv4 and IPv6, local and remote) %u\n\n",
1066 zvni->vni, num_neigh);
1067 } else {
1068 json_vni = json_object_new_object();
1069 json_object_int_add(json_vni, "numArpNd", num_neigh);
1070 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
1071 }
1072 if (!num_neigh) {
1073 if (json)
1074 json_object_object_add(json, vni_str, json_vni);
1075 return;
1076 }
1077
1078 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
1079 wctx.zvni = zvni;
1080 wctx.vty = vty;
1081 wctx.addr_width = 15;
1082 wctx.json = json_vni;
1083
1084 if (print_dup)
1085 hash_iterate(zvni->neigh_table,
1086 zvni_print_dad_neigh_hash_detail, &wctx);
1087 else
1088 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash_detail,
1089 &wctx);
1090
1091 if (json)
1092 json_object_object_add(json, vni_str, json_vni);
1093 }
1094
1095 /* print a specific next hop for an l3vni */
1096 static void zl3vni_print_nh(zebra_neigh_t *n, struct vty *vty,
1097 json_object *json)
1098 {
1099 char buf1[ETHER_ADDR_STRLEN];
1100 char buf2[INET6_ADDRSTRLEN];
1101 json_object *json_hosts = NULL;
1102 struct host_rb_entry *hle;
1103
1104 if (!json) {
1105 vty_out(vty, "Ip: %s\n",
1106 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
1107 vty_out(vty, " RMAC: %s\n",
1108 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
1109 vty_out(vty, " Refcount: %d\n",
1110 rb_host_count(&n->host_rb));
1111 vty_out(vty, " Prefixes:\n");
1112 RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
1113 vty_out(vty, " %s\n",
1114 prefix2str(&hle->p, buf2, sizeof(buf2)));
1115 } else {
1116 json_hosts = json_object_new_array();
1117 json_object_string_add(
1118 json, "ip", ipaddr2str(&(n->ip), buf2, sizeof(buf2)));
1119 json_object_string_add(
1120 json, "routerMac",
1121 prefix_mac2str(&n->emac, buf2, sizeof(buf2)));
1122 json_object_int_add(json, "refCount",
1123 rb_host_count(&n->host_rb));
1124 RB_FOREACH (hle, host_rb_tree_entry, &n->host_rb)
1125 json_object_array_add(json_hosts,
1126 json_object_new_string(prefix2str(
1127 &hle->p, buf2, sizeof(buf2))));
1128 json_object_object_add(json, "prefixList", json_hosts);
1129 }
1130 }
1131
1132 /* Print a specific RMAC entry */
1133 static void zl3vni_print_rmac(zebra_mac_t *zrmac, struct vty *vty,
1134 json_object *json)
1135 {
1136 char buf1[ETHER_ADDR_STRLEN];
1137 char buf2[PREFIX_STRLEN];
1138 json_object *json_hosts = NULL;
1139 struct host_rb_entry *hle;
1140
1141 if (!json) {
1142 vty_out(vty, "MAC: %s\n",
1143 prefix_mac2str(&zrmac->macaddr, buf1, sizeof(buf1)));
1144 vty_out(vty, " Remote VTEP: %s\n",
1145 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
1146 vty_out(vty, " Refcount: %d\n", rb_host_count(&zrmac->host_rb));
1147 vty_out(vty, " Prefixes:\n");
1148 RB_FOREACH (hle, host_rb_tree_entry, &zrmac->host_rb)
1149 vty_out(vty, " %s\n",
1150 prefix2str(&hle->p, buf2, sizeof(buf2)));
1151 } else {
1152 json_hosts = json_object_new_array();
1153 json_object_string_add(
1154 json, "routerMac",
1155 prefix_mac2str(&zrmac->macaddr, buf1, sizeof(buf1)));
1156 json_object_string_add(json, "vtepIp",
1157 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
1158 json_object_int_add(json, "refCount",
1159 rb_host_count(&zrmac->host_rb));
1160 json_object_int_add(json, "localSequence", zrmac->loc_seq);
1161 json_object_int_add(json, "remoteSequence", zrmac->rem_seq);
1162 RB_FOREACH (hle, host_rb_tree_entry, &zrmac->host_rb)
1163 json_object_array_add(
1164 json_hosts,
1165 json_object_new_string(prefix2str(
1166 &hle->p, buf2, sizeof(buf2))));
1167 json_object_object_add(json, "prefixList", json_hosts);
1168 }
1169 }
1170
1171 /*
1172 * Print a specific MAC entry.
1173 */
1174 static void zvni_print_mac(zebra_mac_t *mac, void *ctxt, json_object *json)
1175 {
1176 struct vty *vty;
1177 zebra_neigh_t *n = NULL;
1178 struct listnode *node = NULL;
1179 char buf1[ETHER_ADDR_STRLEN];
1180 char buf2[INET6_ADDRSTRLEN];
1181 struct zebra_vrf *zvrf;
1182 struct timeval detect_start_time = {0, 0};
1183 char timebuf[MONOTIME_STRLEN];
1184
1185 zvrf = zebra_vrf_get_evpn();
1186 if (!zvrf)
1187 return;
1188
1189 vty = (struct vty *)ctxt;
1190 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
1191
1192 if (json) {
1193 json_object *json_mac = json_object_new_object();
1194
1195 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
1196 struct zebra_ns *zns;
1197 struct interface *ifp;
1198 ifindex_t ifindex;
1199
1200 ifindex = mac->fwd_info.local.ifindex;
1201 zns = zebra_ns_lookup(NS_DEFAULT);
1202 ifp = if_lookup_by_index_per_ns(zns, ifindex);
1203 if (!ifp)
1204 return;
1205 json_object_string_add(json_mac, "type", "local");
1206 json_object_string_add(json_mac, "intf", ifp->name);
1207 json_object_int_add(json_mac, "ifindex", ifindex);
1208 if (mac->fwd_info.local.vid)
1209 json_object_int_add(json_mac, "vlan",
1210 mac->fwd_info.local.vid);
1211 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1212 json_object_string_add(json_mac, "type", "remote");
1213 json_object_string_add(
1214 json_mac, "remoteVtep",
1215 inet_ntoa(mac->fwd_info.r_vtep_ip));
1216 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
1217 json_object_string_add(json_mac, "type", "auto");
1218
1219 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
1220 json_object_boolean_true_add(json_mac, "stickyMac");
1221
1222 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
1223 json_object_boolean_true_add(json_mac,
1224 "defaultGateway");
1225
1226 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
1227 json_object_boolean_true_add(json_mac,
1228 "remoteGatewayMac");
1229
1230 json_object_int_add(json_mac, "localSequence", mac->loc_seq);
1231 json_object_int_add(json_mac, "remoteSequence", mac->rem_seq);
1232
1233 json_object_int_add(json_mac, "detectionCount", mac->dad_count);
1234 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1235 json_object_boolean_true_add(json_mac, "isDuplicate");
1236 else
1237 json_object_boolean_false_add(json_mac, "isDuplicate");
1238
1239 /* print all the associated neigh */
1240 if (!listcount(mac->neigh_list))
1241 json_object_string_add(json_mac, "neighbors", "none");
1242 else {
1243 json_object *json_active_nbrs = json_object_new_array();
1244 json_object *json_inactive_nbrs =
1245 json_object_new_array();
1246 json_object *json_nbrs = json_object_new_object();
1247
1248 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
1249 if (IS_ZEBRA_NEIGH_ACTIVE(n))
1250 json_object_array_add(
1251 json_active_nbrs,
1252 json_object_new_string(
1253 ipaddr2str(
1254 &n->ip, buf2,
1255 sizeof(buf2))));
1256 else
1257 json_object_array_add(
1258 json_inactive_nbrs,
1259 json_object_new_string(
1260 ipaddr2str(
1261 &n->ip, buf2,
1262 sizeof(buf2))));
1263 }
1264
1265 json_object_object_add(json_nbrs, "active",
1266 json_active_nbrs);
1267 json_object_object_add(json_nbrs, "inactive",
1268 json_inactive_nbrs);
1269 json_object_object_add(json_mac, "neighbors",
1270 json_nbrs);
1271 }
1272
1273 json_object_object_add(json, buf1, json_mac);
1274 } else {
1275 vty_out(vty, "MAC: %s\n", buf1);
1276
1277 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
1278 struct zebra_ns *zns;
1279 struct interface *ifp;
1280 ifindex_t ifindex;
1281
1282 ifindex = mac->fwd_info.local.ifindex;
1283 zns = zebra_ns_lookup(NS_DEFAULT);
1284 ifp = if_lookup_by_index_per_ns(zns, ifindex);
1285 if (!ifp)
1286 return;
1287 vty_out(vty, " Intf: %s(%u)", ifp->name, ifindex);
1288 if (mac->fwd_info.local.vid)
1289 vty_out(vty, " VLAN: %u",
1290 mac->fwd_info.local.vid);
1291 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1292 vty_out(vty, " Remote VTEP: %s",
1293 inet_ntoa(mac->fwd_info.r_vtep_ip));
1294 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
1295 vty_out(vty, " Auto Mac ");
1296 }
1297
1298 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
1299 vty_out(vty, " Sticky Mac ");
1300
1301 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW))
1302 vty_out(vty, " Default-gateway Mac ");
1303
1304 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW))
1305 vty_out(vty, " Remote-gateway Mac ");
1306
1307 vty_out(vty, "\n");
1308 vty_out(vty, " Local Seq: %u Remote Seq: %u", mac->loc_seq,
1309 mac->rem_seq);
1310 vty_out(vty, "\n");
1311
1312 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
1313 vty_out(vty, " Duplicate, detected at %s",
1314 time_to_string(mac->dad_dup_detect_time,
1315 timebuf));
1316 } else if (mac->dad_count) {
1317 monotime_since(&mac->detect_start_time,
1318 &detect_start_time);
1319 if (detect_start_time.tv_sec <= zvrf->dad_time) {
1320 time_to_string(mac->detect_start_time.tv_sec,
1321 timebuf);
1322 vty_out(vty,
1323 " Duplicate detection started at %s, detection count %u\n",
1324 timebuf, mac->dad_count);
1325 }
1326 }
1327
1328 /* print all the associated neigh */
1329 vty_out(vty, " Neighbors:\n");
1330 if (!listcount(mac->neigh_list))
1331 vty_out(vty, " No Neighbors\n");
1332 else {
1333 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) {
1334 vty_out(vty, " %s %s\n",
1335 ipaddr2str(&n->ip, buf2, sizeof(buf2)),
1336 (IS_ZEBRA_NEIGH_ACTIVE(n)
1337 ? "Active"
1338 : "Inactive"));
1339 }
1340 }
1341
1342 vty_out(vty, "\n");
1343 }
1344 }
1345
1346 /*
1347 * Print MAC hash entry - called for display of all MACs.
1348 */
1349 static void zvni_print_mac_hash(struct hash_bucket *bucket, void *ctxt)
1350 {
1351 struct vty *vty;
1352 json_object *json_mac_hdr = NULL, *json_mac = NULL;
1353 zebra_mac_t *mac;
1354 char buf1[ETHER_ADDR_STRLEN];
1355 struct mac_walk_ctx *wctx = ctxt;
1356
1357 vty = wctx->vty;
1358 json_mac_hdr = wctx->json;
1359 mac = (zebra_mac_t *)bucket->data;
1360
1361 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
1362
1363 if (json_mac_hdr)
1364 json_mac = json_object_new_object();
1365
1366 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
1367 struct zebra_ns *zns;
1368 ifindex_t ifindex;
1369 struct interface *ifp;
1370 vlanid_t vid;
1371
1372 if (wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP)
1373 return;
1374
1375 zns = zebra_ns_lookup(NS_DEFAULT);
1376 ifindex = mac->fwd_info.local.ifindex;
1377 ifp = if_lookup_by_index_per_ns(zns, ifindex);
1378 if (!ifp) // unexpected
1379 return;
1380 vid = mac->fwd_info.local.vid;
1381 if (json_mac_hdr == NULL)
1382 vty_out(vty, "%-17s %-6s %-21s", buf1, "local",
1383 ifp->name);
1384 else {
1385 json_object_string_add(json_mac, "type", "local");
1386 json_object_string_add(json_mac, "intf", ifp->name);
1387 }
1388 if (vid) {
1389 if (json_mac_hdr == NULL)
1390 vty_out(vty, " %-5u", vid);
1391 else
1392 json_object_int_add(json_mac, "vlan", vid);
1393 } else /* No vid? fill out the space */
1394 if (json_mac_hdr == NULL)
1395 vty_out(vty, " %-5s", "");
1396 if (json_mac_hdr == NULL) {
1397 vty_out(vty, " %u/%u", mac->loc_seq, mac->rem_seq);
1398 vty_out(vty, "\n");
1399 } else {
1400 json_object_int_add(json_mac, "localSequence",
1401 mac->loc_seq);
1402 json_object_int_add(json_mac, "remoteSequence",
1403 mac->rem_seq);
1404 json_object_int_add(json_mac, "detectionCount",
1405 mac->dad_count);
1406 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1407 json_object_boolean_true_add(json_mac,
1408 "isDuplicate");
1409 else
1410 json_object_boolean_false_add(json_mac,
1411 "isDuplicate");
1412 json_object_object_add(json_mac_hdr, buf1, json_mac);
1413 }
1414
1415 wctx->count++;
1416
1417 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1418
1419 if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) &&
1420 !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip,
1421 &wctx->r_vtep_ip))
1422 return;
1423
1424 if (json_mac_hdr == NULL) {
1425 if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) &&
1426 (wctx->count == 0)) {
1427 vty_out(vty, "\nVNI %u\n\n", wctx->zvni->vni);
1428 vty_out(vty, "%-17s %-6s %-21s %-5s %s\n",
1429 "MAC", "Type", "Intf/Remote VTEP",
1430 "VLAN", "Seq #'s");
1431 }
1432 vty_out(vty, "%-17s %-6s %-21s %-5s %u/%u\n", buf1,
1433 "remote", inet_ntoa(mac->fwd_info.r_vtep_ip),
1434 "", mac->loc_seq, mac->rem_seq);
1435 } else {
1436 json_object_string_add(json_mac, "type", "remote");
1437 json_object_string_add(json_mac, "remoteVtep",
1438 inet_ntoa(mac->fwd_info.r_vtep_ip));
1439 json_object_object_add(json_mac_hdr, buf1, json_mac);
1440 json_object_int_add(json_mac, "localSequence",
1441 mac->loc_seq);
1442 json_object_int_add(json_mac, "remoteSequence",
1443 mac->rem_seq);
1444 json_object_int_add(json_mac, "detectionCount",
1445 mac->dad_count);
1446 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1447 json_object_boolean_true_add(json_mac,
1448 "isDuplicate");
1449 else
1450 json_object_boolean_false_add(json_mac,
1451 "isDuplicate");
1452
1453 }
1454
1455 wctx->count++;
1456 }
1457 }
1458
1459 /* Print Duplicate MAC */
1460 static void zvni_print_dad_mac_hash(struct hash_bucket *bucket, void *ctxt)
1461 {
1462 zebra_mac_t *mac;
1463
1464 mac = (zebra_mac_t *)bucket->data;
1465 if (!mac)
1466 return;
1467
1468 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1469 zvni_print_mac_hash(bucket, ctxt);
1470 }
1471
1472 /*
1473 * Print MAC hash entry in detail - called for display of all MACs.
1474 */
1475 static void zvni_print_mac_hash_detail(struct hash_bucket *bucket, void *ctxt)
1476 {
1477 struct vty *vty;
1478 json_object *json_mac_hdr = NULL;
1479 zebra_mac_t *mac;
1480 struct mac_walk_ctx *wctx = ctxt;
1481 char buf1[ETHER_ADDR_STRLEN];
1482
1483 vty = wctx->vty;
1484 json_mac_hdr = wctx->json;
1485 mac = (zebra_mac_t *)bucket->data;
1486 if (!mac)
1487 return;
1488
1489 wctx->count++;
1490 prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1));
1491
1492 zvni_print_mac(mac, vty, json_mac_hdr);
1493 }
1494
1495 /* Print Duplicate MAC in detail */
1496 static void zvni_print_dad_mac_hash_detail(struct hash_bucket *bucket,
1497 void *ctxt)
1498 {
1499 zebra_mac_t *mac;
1500
1501 mac = (zebra_mac_t *)bucket->data;
1502 if (!mac)
1503 return;
1504
1505 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
1506 zvni_print_mac_hash_detail(bucket, ctxt);
1507 }
1508
1509 /*
1510 * Print MACs for all VNI.
1511 */
1512 static void zvni_print_mac_hash_all_vni(struct hash_bucket *bucket, void *ctxt)
1513 {
1514 struct vty *vty;
1515 json_object *json = NULL, *json_vni = NULL;
1516 json_object *json_mac = NULL;
1517 zebra_vni_t *zvni;
1518 uint32_t num_macs;
1519 struct mac_walk_ctx *wctx = ctxt;
1520 char vni_str[VNI_STR_LEN];
1521
1522 vty = (struct vty *)wctx->vty;
1523 json = (struct json_object *)wctx->json;
1524
1525 zvni = (zebra_vni_t *)bucket->data;
1526 wctx->zvni = zvni;
1527
1528 /*We are iterating over a new VNI, set the count to 0*/
1529 wctx->count = 0;
1530
1531 num_macs = num_valid_macs(zvni);
1532 if (!num_macs)
1533 return;
1534
1535 if (wctx->print_dup)
1536 num_macs = num_dup_detected_macs(zvni);
1537
1538 if (json) {
1539 json_vni = json_object_new_object();
1540 json_mac = json_object_new_object();
1541 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
1542 }
1543
1544 if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
1545 if (json == NULL) {
1546 vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
1547 zvni->vni, num_macs);
1548 vty_out(vty, "%-17s %-6s %-21s %-5s %s\n", "MAC",
1549 "Type", "Intf/Remote VTEP", "VLAN", "Seq #'s");
1550 } else
1551 json_object_int_add(json_vni, "numMacs", num_macs);
1552 }
1553
1554 if (!num_macs) {
1555 if (json) {
1556 json_object_int_add(json_vni, "numMacs", num_macs);
1557 json_object_object_add(json, vni_str, json_vni);
1558 }
1559 return;
1560 }
1561
1562 /* assign per-vni to wctx->json object to fill macs
1563 * under the vni. Re-assign primary json object to fill
1564 * next vni information.
1565 */
1566 wctx->json = json_mac;
1567 if (wctx->print_dup)
1568 hash_iterate(zvni->mac_table, zvni_print_dad_mac_hash, wctx);
1569 else
1570 hash_iterate(zvni->mac_table, zvni_print_mac_hash, wctx);
1571 wctx->json = json;
1572 if (json) {
1573 if (wctx->count)
1574 json_object_object_add(json_vni, "macs", json_mac);
1575 json_object_object_add(json, vni_str, json_vni);
1576 }
1577 }
1578
1579 /*
1580 * Print MACs in detail for all VNI.
1581 */
1582 static void zvni_print_mac_hash_all_vni_detail(struct hash_bucket *bucket,
1583 void *ctxt)
1584 {
1585 struct vty *vty;
1586 json_object *json = NULL, *json_vni = NULL;
1587 json_object *json_mac = NULL;
1588 zebra_vni_t *zvni;
1589 uint32_t num_macs;
1590 struct mac_walk_ctx *wctx = ctxt;
1591 char vni_str[VNI_STR_LEN];
1592
1593 vty = (struct vty *)wctx->vty;
1594 json = (struct json_object *)wctx->json;
1595
1596 zvni = (zebra_vni_t *)bucket->data;
1597 if (!zvni) {
1598 if (json)
1599 vty_out(vty, "{}\n");
1600 return;
1601 }
1602 wctx->zvni = zvni;
1603
1604 /*We are iterating over a new VNI, set the count to 0*/
1605 wctx->count = 0;
1606
1607 num_macs = num_valid_macs(zvni);
1608 if (!num_macs)
1609 return;
1610
1611 if (wctx->print_dup && (num_dup_detected_macs(zvni) == 0))
1612 return;
1613
1614 if (json) {
1615 json_vni = json_object_new_object();
1616 json_mac = json_object_new_object();
1617 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
1618 }
1619
1620 if (!CHECK_FLAG(wctx->flags, SHOW_REMOTE_MAC_FROM_VTEP)) {
1621 if (json == NULL) {
1622 vty_out(vty, "\nVNI %u #MACs (local and remote) %u\n\n",
1623 zvni->vni, num_macs);
1624 } else
1625 json_object_int_add(json_vni, "numMacs", num_macs);
1626 }
1627 /* assign per-vni to wctx->json object to fill macs
1628 * under the vni. Re-assign primary json object to fill
1629 * next vni information.
1630 */
1631 wctx->json = json_mac;
1632 if (wctx->print_dup)
1633 hash_iterate(zvni->mac_table, zvni_print_dad_mac_hash_detail,
1634 wctx);
1635 else
1636 hash_iterate(zvni->mac_table, zvni_print_mac_hash_detail, wctx);
1637 wctx->json = json;
1638 if (json) {
1639 if (wctx->count)
1640 json_object_object_add(json_vni, "macs", json_mac);
1641 json_object_object_add(json, vni_str, json_vni);
1642 }
1643 }
1644
1645 static void zl3vni_print_nh_hash(struct hash_bucket *bucket, void *ctx)
1646 {
1647 struct nh_walk_ctx *wctx = NULL;
1648 struct vty *vty = NULL;
1649 struct json_object *json_vni = NULL;
1650 struct json_object *json_nh = NULL;
1651 zebra_neigh_t *n = NULL;
1652 char buf1[ETHER_ADDR_STRLEN];
1653 char buf2[INET6_ADDRSTRLEN];
1654
1655 wctx = (struct nh_walk_ctx *)ctx;
1656 vty = wctx->vty;
1657 json_vni = wctx->json;
1658 if (json_vni)
1659 json_nh = json_object_new_object();
1660 n = (zebra_neigh_t *)bucket->data;
1661
1662 if (!json_vni) {
1663 vty_out(vty, "%-15s %-17s\n",
1664 ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
1665 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
1666 } else {
1667 json_object_string_add(json_nh, "nexthopIp",
1668 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
1669 json_object_string_add(
1670 json_nh, "routerMac",
1671 prefix_mac2str(&n->emac, buf1, sizeof(buf1)));
1672 json_object_object_add(json_vni,
1673 ipaddr2str(&(n->ip), buf2, sizeof(buf2)),
1674 json_nh);
1675 }
1676 }
1677
1678 static void zl3vni_print_nh_hash_all_vni(struct hash_bucket *bucket,
1679 void **args)
1680 {
1681 struct vty *vty = NULL;
1682 json_object *json = NULL;
1683 json_object *json_vni = NULL;
1684 zebra_l3vni_t *zl3vni = NULL;
1685 uint32_t num_nh = 0;
1686 struct nh_walk_ctx wctx;
1687 char vni_str[VNI_STR_LEN];
1688
1689 vty = (struct vty *)args[0];
1690 json = (struct json_object *)args[1];
1691
1692 zl3vni = (zebra_l3vni_t *)bucket->data;
1693
1694 num_nh = hashcount(zl3vni->nh_table);
1695 if (!num_nh)
1696 return;
1697
1698 if (json) {
1699 json_vni = json_object_new_object();
1700 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
1701 }
1702
1703 if (json == NULL) {
1704 vty_out(vty, "\nVNI %u #Next-Hops %u\n\n", zl3vni->vni, num_nh);
1705 vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
1706 } else
1707 json_object_int_add(json_vni, "numNextHops", num_nh);
1708
1709 memset(&wctx, 0, sizeof(struct nh_walk_ctx));
1710 wctx.vty = vty;
1711 wctx.json = json_vni;
1712 hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
1713 if (json)
1714 json_object_object_add(json, vni_str, json_vni);
1715 }
1716
1717 static void zl3vni_print_rmac_hash_all_vni(struct hash_bucket *bucket,
1718 void **args)
1719 {
1720 struct vty *vty = NULL;
1721 json_object *json = NULL;
1722 json_object *json_vni = NULL;
1723 zebra_l3vni_t *zl3vni = NULL;
1724 uint32_t num_rmacs;
1725 struct rmac_walk_ctx wctx;
1726 char vni_str[VNI_STR_LEN];
1727
1728 vty = (struct vty *)args[0];
1729 json = (struct json_object *)args[1];
1730
1731 zl3vni = (zebra_l3vni_t *)bucket->data;
1732
1733 num_rmacs = hashcount(zl3vni->rmac_table);
1734 if (!num_rmacs)
1735 return;
1736
1737 if (json) {
1738 json_vni = json_object_new_object();
1739 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
1740 }
1741
1742 if (json == NULL) {
1743 vty_out(vty, "\nVNI %u #RMACs %u\n\n", zl3vni->vni, num_rmacs);
1744 vty_out(vty, "%-17s %-21s\n", "RMAC", "Remote VTEP");
1745 } else
1746 json_object_int_add(json_vni, "numRmacs", num_rmacs);
1747
1748 /* assign per-vni to wctx->json object to fill macs
1749 * under the vni. Re-assign primary json object to fill
1750 * next vni information.
1751 */
1752 memset(&wctx, 0, sizeof(struct rmac_walk_ctx));
1753 wctx.vty = vty;
1754 wctx.json = json_vni;
1755 hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
1756 if (json)
1757 json_object_object_add(json, vni_str, json_vni);
1758 }
1759
1760 static void zl3vni_print_rmac_hash(struct hash_bucket *bucket, void *ctx)
1761 {
1762 zebra_mac_t *zrmac = NULL;
1763 struct rmac_walk_ctx *wctx = NULL;
1764 struct vty *vty = NULL;
1765 struct json_object *json = NULL;
1766 struct json_object *json_rmac = NULL;
1767 char buf[ETHER_ADDR_STRLEN];
1768
1769 wctx = (struct rmac_walk_ctx *)ctx;
1770 vty = wctx->vty;
1771 json = wctx->json;
1772 if (json)
1773 json_rmac = json_object_new_object();
1774 zrmac = (zebra_mac_t *)bucket->data;
1775
1776 if (!json) {
1777 vty_out(vty, "%-17s %-21s\n",
1778 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
1779 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
1780 } else {
1781 json_object_string_add(
1782 json_rmac, "routerMac",
1783 prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)));
1784 json_object_string_add(json_rmac, "vtepIp",
1785 inet_ntoa(zrmac->fwd_info.r_vtep_ip));
1786 json_object_object_add(
1787 json, prefix_mac2str(&zrmac->macaddr, buf, sizeof(buf)),
1788 json_rmac);
1789 }
1790 }
1791
1792 /* print a specific L3 VNI entry */
1793 static void zl3vni_print(zebra_l3vni_t *zl3vni, void **ctx)
1794 {
1795 char buf[ETHER_ADDR_STRLEN];
1796 struct vty *vty = NULL;
1797 json_object *json = NULL;
1798 zebra_vni_t *zvni = NULL;
1799 json_object *json_vni_list = NULL;
1800 struct listnode *node = NULL, *nnode = NULL;
1801
1802 vty = ctx[0];
1803 json = ctx[1];
1804
1805 if (!json) {
1806 vty_out(vty, "VNI: %u\n", zl3vni->vni);
1807 vty_out(vty, " Type: %s\n", "L3");
1808 vty_out(vty, " Tenant VRF: %s\n", zl3vni_vrf_name(zl3vni));
1809 vty_out(vty, " Local Vtep Ip: %s\n",
1810 inet_ntoa(zl3vni->local_vtep_ip));
1811 vty_out(vty, " Vxlan-Intf: %s\n",
1812 zl3vni_vxlan_if_name(zl3vni));
1813 vty_out(vty, " SVI-If: %s\n", zl3vni_svi_if_name(zl3vni));
1814 vty_out(vty, " State: %s\n", zl3vni_state2str(zl3vni));
1815 vty_out(vty, " VNI Filter: %s\n",
1816 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
1817 ? "prefix-routes-only"
1818 : "none");
1819 vty_out(vty, " System MAC: %s\n",
1820 zl3vni_sysmac2str(zl3vni, buf, sizeof(buf)));
1821 vty_out(vty, " Router MAC: %s\n",
1822 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
1823 vty_out(vty, " L2 VNIs: ");
1824 for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zvni))
1825 vty_out(vty, "%u ", zvni->vni);
1826 vty_out(vty, "\n");
1827 } else {
1828 json_vni_list = json_object_new_array();
1829 json_object_int_add(json, "vni", zl3vni->vni);
1830 json_object_string_add(json, "type", "L3");
1831 json_object_string_add(json, "localVtepIp",
1832 inet_ntoa(zl3vni->local_vtep_ip));
1833 json_object_string_add(json, "vxlanIntf",
1834 zl3vni_vxlan_if_name(zl3vni));
1835 json_object_string_add(json, "sviIntf",
1836 zl3vni_svi_if_name(zl3vni));
1837 json_object_string_add(json, "state", zl3vni_state2str(zl3vni));
1838 json_object_string_add(json, "vrf", zl3vni_vrf_name(zl3vni));
1839 json_object_string_add(
1840 json, "sysMac",
1841 zl3vni_sysmac2str(zl3vni, buf, sizeof(buf)));
1842 json_object_string_add(
1843 json, "routerMac",
1844 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
1845 json_object_string_add(
1846 json, "vniFilter",
1847 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
1848 ? "prefix-routes-only"
1849 : "none");
1850 for (ALL_LIST_ELEMENTS(zl3vni->l2vnis, node, nnode, zvni)) {
1851 json_object_array_add(json_vni_list,
1852 json_object_new_int(zvni->vni));
1853 }
1854 json_object_object_add(json, "l2Vnis", json_vni_list);
1855 }
1856 }
1857
1858 /*
1859 * Print a specific VNI entry.
1860 */
1861 static void zvni_print(zebra_vni_t *zvni, void **ctxt)
1862 {
1863 struct vty *vty;
1864 zebra_vtep_t *zvtep;
1865 uint32_t num_macs;
1866 uint32_t num_neigh;
1867 json_object *json = NULL;
1868 json_object *json_vtep_list = NULL;
1869 json_object *json_ip_str = NULL;
1870
1871 vty = ctxt[0];
1872 json = ctxt[1];
1873
1874 if (json == NULL) {
1875 vty_out(vty, "VNI: %u\n", zvni->vni);
1876 vty_out(vty, " Type: %s\n", "L2");
1877 vty_out(vty, " Tenant VRF: %s\n", vrf_id_to_name(zvni->vrf_id));
1878 } else {
1879 json_object_int_add(json, "vni", zvni->vni);
1880 json_object_string_add(json, "type", "L2");
1881 json_object_string_add(json, "vrf",
1882 vrf_id_to_name(zvni->vrf_id));
1883 }
1884
1885 if (!zvni->vxlan_if) { // unexpected
1886 if (json == NULL)
1887 vty_out(vty, " VxLAN interface: unknown\n");
1888 return;
1889 }
1890 num_macs = num_valid_macs(zvni);
1891 num_neigh = hashcount(zvni->neigh_table);
1892 if (json == NULL) {
1893 vty_out(vty, " VxLAN interface: %s\n", zvni->vxlan_if->name);
1894 vty_out(vty, " VxLAN ifIndex: %u\n", zvni->vxlan_if->ifindex);
1895 vty_out(vty, " Local VTEP IP: %s\n",
1896 inet_ntoa(zvni->local_vtep_ip));
1897 vty_out(vty, " Mcast group: %s\n",
1898 inet_ntoa(zvni->mcast_grp));
1899 } else {
1900 json_object_string_add(json, "vxlanInterface",
1901 zvni->vxlan_if->name);
1902 json_object_int_add(json, "ifindex", zvni->vxlan_if->ifindex);
1903 json_object_string_add(json, "vtepIp",
1904 inet_ntoa(zvni->local_vtep_ip));
1905 json_object_string_add(json, "mcastGroup",
1906 inet_ntoa(zvni->mcast_grp));
1907 json_object_string_add(json, "advertiseGatewayMacip",
1908 zvni->advertise_gw_macip ? "Yes" : "No");
1909 json_object_int_add(json, "numMacs", num_macs);
1910 json_object_int_add(json, "numArpNd", num_neigh);
1911 }
1912 if (!zvni->vteps) {
1913 if (json == NULL)
1914 vty_out(vty, " No remote VTEPs known for this VNI\n");
1915 } else {
1916 if (json == NULL)
1917 vty_out(vty, " Remote VTEPs for this VNI:\n");
1918 else
1919 json_vtep_list = json_object_new_array();
1920 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
1921 const char *flood_str = lookup_msg(zvtep_flood_str,
1922 zvtep->flood_control,
1923 VXLAN_FLOOD_STR_DEFAULT);
1924
1925 if (json == NULL) {
1926 vty_out(vty, " %s flood: %s\n",
1927 inet_ntoa(zvtep->vtep_ip),
1928 flood_str);
1929 } else {
1930 json_ip_str = json_object_new_string(
1931 inet_ntoa(zvtep->vtep_ip));
1932 json_object_array_add(json_vtep_list,
1933 json_ip_str);
1934 }
1935 }
1936 if (json)
1937 json_object_object_add(json, "numRemoteVteps",
1938 json_vtep_list);
1939 }
1940 if (json == NULL) {
1941 vty_out(vty,
1942 " Number of MACs (local and remote) known for this VNI: %u\n",
1943 num_macs);
1944 vty_out(vty,
1945 " Number of ARPs (IPv4 and IPv6, local and remote) "
1946 "known for this VNI: %u\n",
1947 num_neigh);
1948 vty_out(vty, " Advertise-gw-macip: %s\n",
1949 zvni->advertise_gw_macip ? "Yes" : "No");
1950 }
1951 }
1952
1953 /* print a L3 VNI hash entry */
1954 static void zl3vni_print_hash(struct hash_bucket *bucket, void *ctx[])
1955 {
1956 struct vty *vty = NULL;
1957 json_object *json = NULL;
1958 json_object *json_vni = NULL;
1959 zebra_l3vni_t *zl3vni = NULL;
1960
1961 vty = (struct vty *)ctx[0];
1962 json = (json_object *)ctx[1];
1963
1964 zl3vni = (zebra_l3vni_t *)bucket->data;
1965
1966 if (!json) {
1967 vty_out(vty, "%-10u %-4s %-21s %-8lu %-8lu %-15s %-37s\n",
1968 zl3vni->vni, "L3", zl3vni_vxlan_if_name(zl3vni),
1969 hashcount(zl3vni->rmac_table),
1970 hashcount(zl3vni->nh_table), "n/a",
1971 zl3vni_vrf_name(zl3vni));
1972 } else {
1973 char vni_str[VNI_STR_LEN];
1974
1975 snprintf(vni_str, VNI_STR_LEN, "%u", zl3vni->vni);
1976 json_vni = json_object_new_object();
1977 json_object_int_add(json_vni, "vni", zl3vni->vni);
1978 json_object_string_add(json_vni, "vxlanIf",
1979 zl3vni_vxlan_if_name(zl3vni));
1980 json_object_int_add(json_vni, "numMacs",
1981 hashcount(zl3vni->rmac_table));
1982 json_object_int_add(json_vni, "numArpNd",
1983 hashcount(zl3vni->nh_table));
1984 json_object_string_add(json_vni, "numRemoteVteps", "n/a");
1985 json_object_string_add(json_vni, "type", "L3");
1986 json_object_string_add(json_vni, "tenantVrf",
1987 zl3vni_vrf_name(zl3vni));
1988 json_object_object_add(json, vni_str, json_vni);
1989 }
1990 }
1991
1992 /* Private Structure to pass callback data for hash iterator */
1993 struct zvni_evpn_show {
1994 struct vty *vty;
1995 json_object *json;
1996 struct zebra_vrf *zvrf;
1997 bool use_json;
1998 };
1999
2000 /* print a L3 VNI hash entry in detail*/
2001 static void zl3vni_print_hash_detail(struct hash_bucket *bucket, void *data)
2002 {
2003 struct vty *vty = NULL;
2004 zebra_l3vni_t *zl3vni = NULL;
2005 json_object *json_array = NULL;
2006 bool use_json = false;
2007 struct zvni_evpn_show *zes = data;
2008
2009 vty = zes->vty;
2010 json_array = zes->json;
2011 use_json = zes->use_json;
2012
2013 zl3vni = (zebra_l3vni_t *)bucket->data;
2014
2015 zebra_vxlan_print_vni(vty, zes->zvrf, zl3vni->vni,
2016 use_json, json_array);
2017
2018 if (!use_json)
2019 vty_out(vty, "\n");
2020 }
2021
2022
2023 /*
2024 * Print a VNI hash entry - called for display of all VNIs.
2025 */
2026 static void zvni_print_hash(struct hash_bucket *bucket, void *ctxt[])
2027 {
2028 struct vty *vty;
2029 zebra_vni_t *zvni;
2030 zebra_vtep_t *zvtep;
2031 uint32_t num_vteps = 0;
2032 uint32_t num_macs = 0;
2033 uint32_t num_neigh = 0;
2034 json_object *json = NULL;
2035 json_object *json_vni = NULL;
2036 json_object *json_ip_str = NULL;
2037 json_object *json_vtep_list = NULL;
2038
2039 vty = ctxt[0];
2040 json = ctxt[1];
2041
2042 zvni = (zebra_vni_t *)bucket->data;
2043
2044 zvtep = zvni->vteps;
2045 while (zvtep) {
2046 num_vteps++;
2047 zvtep = zvtep->next;
2048 }
2049
2050 num_macs = num_valid_macs(zvni);
2051 num_neigh = hashcount(zvni->neigh_table);
2052 if (json == NULL)
2053 vty_out(vty, "%-10u %-4s %-21s %-8u %-8u %-15u %-37s\n",
2054 zvni->vni, "L2",
2055 zvni->vxlan_if ? zvni->vxlan_if->name : "unknown",
2056 num_macs, num_neigh, num_vteps,
2057 vrf_id_to_name(zvni->vrf_id));
2058 else {
2059 char vni_str[VNI_STR_LEN];
2060 snprintf(vni_str, VNI_STR_LEN, "%u", zvni->vni);
2061 json_vni = json_object_new_object();
2062 json_object_int_add(json_vni, "vni", zvni->vni);
2063 json_object_string_add(json_vni, "type", "L2");
2064 json_object_string_add(json_vni, "vxlanIf",
2065 zvni->vxlan_if ? zvni->vxlan_if->name
2066 : "unknown");
2067 json_object_int_add(json_vni, "numMacs", num_macs);
2068 json_object_int_add(json_vni, "numArpNd", num_neigh);
2069 json_object_int_add(json_vni, "numRemoteVteps", num_vteps);
2070 json_object_string_add(json_vni, "tenantVrf",
2071 vrf_id_to_name(zvni->vrf_id));
2072 if (num_vteps) {
2073 json_vtep_list = json_object_new_array();
2074 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
2075 json_ip_str = json_object_new_string(
2076 inet_ntoa(zvtep->vtep_ip));
2077 json_object_array_add(json_vtep_list,
2078 json_ip_str);
2079 }
2080 json_object_object_add(json_vni, "remoteVteps",
2081 json_vtep_list);
2082 }
2083 json_object_object_add(json, vni_str, json_vni);
2084 }
2085 }
2086
2087 /*
2088 * Print a VNI hash entry in detail - called for display of all VNIs.
2089 */
2090 static void zvni_print_hash_detail(struct hash_bucket *bucket, void *data)
2091 {
2092 struct vty *vty;
2093 zebra_vni_t *zvni;
2094 json_object *json_array = NULL;
2095 bool use_json = false;
2096 struct zvni_evpn_show *zes = data;
2097
2098 vty = zes->vty;
2099 json_array = zes->json;
2100 use_json = zes->use_json;
2101
2102 zvni = (zebra_vni_t *)bucket->data;
2103
2104 zebra_vxlan_print_vni(vty, zes->zvrf, zvni->vni, use_json, json_array);
2105
2106 if (!use_json)
2107 vty_out(vty, "\n");
2108 }
2109
2110 /*
2111 * Inform BGP about local MACIP.
2112 */
2113 static int zvni_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr,
2114 struct ipaddr *ip, uint8_t flags,
2115 uint32_t seq, int state, uint16_t cmd)
2116 {
2117 char buf[ETHER_ADDR_STRLEN];
2118 char buf2[INET6_ADDRSTRLEN];
2119 int ipa_len;
2120 struct zserv *client = NULL;
2121 struct stream *s = NULL;
2122
2123 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
2124 /* BGP may not be running. */
2125 if (!client)
2126 return 0;
2127
2128 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2129
2130 zclient_create_header(s, cmd, zebra_vrf_get_evpn_id());
2131 stream_putl(s, vni);
2132 stream_put(s, macaddr->octet, ETH_ALEN);
2133 if (ip) {
2134 ipa_len = 0;
2135 if (IS_IPADDR_V4(ip))
2136 ipa_len = IPV4_MAX_BYTELEN;
2137 else if (IS_IPADDR_V6(ip))
2138 ipa_len = IPV6_MAX_BYTELEN;
2139
2140 stream_putl(s, ipa_len); /* IP address length */
2141 if (ipa_len)
2142 stream_put(s, &ip->ip.addr, ipa_len); /* IP address */
2143 } else
2144 stream_putl(s, 0); /* Just MAC. */
2145
2146 if (cmd == ZEBRA_MACIP_ADD) {
2147 stream_putc(s, flags); /* sticky mac/gateway mac */
2148 stream_putl(s, seq); /* sequence number */
2149 } else {
2150 stream_putl(s, state); /* state - active/inactive */
2151 }
2152
2153
2154 /* Write packet size. */
2155 stream_putw_at(s, 0, stream_get_endp(s));
2156
2157 if (IS_ZEBRA_DEBUG_VXLAN)
2158 zlog_debug(
2159 "Send MACIP %s flags 0x%x MAC %s IP %s seq %u L2-VNI %u to %s",
2160 (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
2161 prefix_mac2str(macaddr, buf, sizeof(buf)),
2162 ipaddr2str(ip, buf2, sizeof(buf2)), seq, vni,
2163 zebra_route_string(client->proto));
2164
2165 if (cmd == ZEBRA_MACIP_ADD)
2166 client->macipadd_cnt++;
2167 else
2168 client->macipdel_cnt++;
2169
2170 return zserv_send_message(client, s);
2171 }
2172
2173 /*
2174 * Make hash key for neighbors.
2175 */
2176 static unsigned int neigh_hash_keymake(const void *p)
2177 {
2178 const zebra_neigh_t *n = p;
2179 const struct ipaddr *ip = &n->ip;
2180
2181 if (IS_IPADDR_V4(ip))
2182 return jhash_1word(ip->ipaddr_v4.s_addr, 0);
2183
2184 return jhash2(ip->ipaddr_v6.s6_addr32,
2185 array_size(ip->ipaddr_v6.s6_addr32), 0);
2186 }
2187
2188 /*
2189 * Compare two neighbor hash structures.
2190 */
2191 static bool neigh_cmp(const void *p1, const void *p2)
2192 {
2193 const zebra_neigh_t *n1 = p1;
2194 const zebra_neigh_t *n2 = p2;
2195
2196 if (n1 == NULL && n2 == NULL)
2197 return true;
2198
2199 if (n1 == NULL || n2 == NULL)
2200 return false;
2201
2202 return (memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr)) == 0);
2203 }
2204
2205 static int neigh_list_cmp(void *p1, void *p2)
2206 {
2207 const zebra_neigh_t *n1 = p1;
2208 const zebra_neigh_t *n2 = p2;
2209
2210 return memcmp(&n1->ip, &n2->ip, sizeof(struct ipaddr));
2211 }
2212
2213 /*
2214 * Callback to allocate neighbor hash entry.
2215 */
2216 static void *zvni_neigh_alloc(void *p)
2217 {
2218 const zebra_neigh_t *tmp_n = p;
2219 zebra_neigh_t *n;
2220
2221 n = XCALLOC(MTYPE_NEIGH, sizeof(zebra_neigh_t));
2222 *n = *tmp_n;
2223
2224 return ((void *)n);
2225 }
2226
2227 /*
2228 * Add neighbor entry.
2229 */
2230 static zebra_neigh_t *zvni_neigh_add(zebra_vni_t *zvni, struct ipaddr *ip,
2231 struct ethaddr *mac)
2232 {
2233 zebra_neigh_t tmp_n;
2234 zebra_neigh_t *n = NULL;
2235 zebra_mac_t *zmac = NULL;
2236
2237 memset(&tmp_n, 0, sizeof(zebra_neigh_t));
2238 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
2239 n = hash_get(zvni->neigh_table, &tmp_n, zvni_neigh_alloc);
2240 assert(n);
2241
2242 memcpy(&n->emac, mac, ETH_ALEN);
2243 n->state = ZEBRA_NEIGH_INACTIVE;
2244 n->zvni = zvni;
2245 n->dad_ip_auto_recovery_timer = NULL;
2246
2247 /* Associate the neigh to mac */
2248 zmac = zvni_mac_lookup(zvni, mac);
2249 if (zmac)
2250 listnode_add_sort(zmac->neigh_list, n);
2251
2252 return n;
2253 }
2254
2255 /*
2256 * Delete neighbor entry.
2257 */
2258 static int zvni_neigh_del(zebra_vni_t *zvni, zebra_neigh_t *n)
2259 {
2260 zebra_neigh_t *tmp_n;
2261 zebra_mac_t *zmac = NULL;
2262
2263 zmac = zvni_mac_lookup(zvni, &n->emac);
2264 if (zmac)
2265 listnode_delete(zmac->neigh_list, n);
2266
2267 /* Cancel auto recovery */
2268 THREAD_OFF(n->dad_ip_auto_recovery_timer);
2269
2270 /* Free the VNI hash entry and allocated memory. */
2271 tmp_n = hash_release(zvni->neigh_table, n);
2272 XFREE(MTYPE_NEIGH, tmp_n);
2273
2274 return 0;
2275 }
2276
2277 /*
2278 * Free neighbor hash entry (callback)
2279 */
2280 static void zvni_neigh_del_hash_entry(struct hash_bucket *bucket, void *arg)
2281 {
2282 struct neigh_walk_ctx *wctx = arg;
2283 zebra_neigh_t *n = bucket->data;
2284
2285 if (((wctx->flags & DEL_LOCAL_NEIGH) && (n->flags & ZEBRA_NEIGH_LOCAL))
2286 || ((wctx->flags & DEL_REMOTE_NEIGH)
2287 && (n->flags & ZEBRA_NEIGH_REMOTE))
2288 || ((wctx->flags & DEL_REMOTE_NEIGH_FROM_VTEP)
2289 && (n->flags & ZEBRA_NEIGH_REMOTE)
2290 && IPV4_ADDR_SAME(&n->r_vtep_ip, &wctx->r_vtep_ip))) {
2291 if (wctx->upd_client && (n->flags & ZEBRA_NEIGH_LOCAL))
2292 zvni_neigh_send_del_to_client(wctx->zvni->vni, &n->ip,
2293 &n->emac, 0, n->state);
2294
2295 if (wctx->uninstall)
2296 zvni_neigh_uninstall(wctx->zvni, n);
2297
2298 zvni_neigh_del(wctx->zvni, n);
2299 }
2300
2301 return;
2302 }
2303
2304 /*
2305 * Delete all neighbor entries from specific VTEP for a particular VNI.
2306 */
2307 static void zvni_neigh_del_from_vtep(zebra_vni_t *zvni, int uninstall,
2308 struct in_addr *r_vtep_ip)
2309 {
2310 struct neigh_walk_ctx wctx;
2311
2312 if (!zvni->neigh_table)
2313 return;
2314
2315 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
2316 wctx.zvni = zvni;
2317 wctx.uninstall = uninstall;
2318 wctx.flags = DEL_REMOTE_NEIGH_FROM_VTEP;
2319 wctx.r_vtep_ip = *r_vtep_ip;
2320
2321 hash_iterate(zvni->neigh_table, zvni_neigh_del_hash_entry, &wctx);
2322 }
2323
2324 /*
2325 * Delete all neighbor entries for this VNI.
2326 */
2327 static void zvni_neigh_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
2328 uint32_t flags)
2329 {
2330 struct neigh_walk_ctx wctx;
2331
2332 if (!zvni->neigh_table)
2333 return;
2334
2335 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
2336 wctx.zvni = zvni;
2337 wctx.uninstall = uninstall;
2338 wctx.upd_client = upd_client;
2339 wctx.flags = flags;
2340
2341 hash_iterate(zvni->neigh_table, zvni_neigh_del_hash_entry, &wctx);
2342 }
2343
2344 /*
2345 * Look up neighbor hash entry.
2346 */
2347 static zebra_neigh_t *zvni_neigh_lookup(zebra_vni_t *zvni, struct ipaddr *ip)
2348 {
2349 zebra_neigh_t tmp;
2350 zebra_neigh_t *n;
2351
2352 memset(&tmp, 0, sizeof(tmp));
2353 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
2354 n = hash_lookup(zvni->neigh_table, &tmp);
2355
2356 return n;
2357 }
2358
2359 /*
2360 * Process all neighbors associated with a MAC upon the MAC being learnt
2361 * locally or undergoing any other change (such as sequence number).
2362 */
2363 static void zvni_process_neigh_on_local_mac_change(zebra_vni_t *zvni,
2364 zebra_mac_t *zmac,
2365 bool seq_change)
2366 {
2367 zebra_neigh_t *n = NULL;
2368 struct listnode *node = NULL;
2369 struct zebra_vrf *zvrf = NULL;
2370 char buf[ETHER_ADDR_STRLEN];
2371
2372 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
2373
2374 if (IS_ZEBRA_DEBUG_VXLAN)
2375 zlog_debug("Processing neighbors on local MAC %s %s, VNI %u",
2376 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
2377 seq_change ? "CHANGE" : "ADD", zvni->vni);
2378
2379 /* Walk all neighbors and mark any inactive local neighbors as
2380 * active and/or update sequence number upon a move, and inform BGP.
2381 * The action for remote neighbors is TBD.
2382 * NOTE: We can't simply uninstall remote neighbors as the kernel may
2383 * accidentally end up deleting a just-learnt local neighbor.
2384 */
2385 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
2386 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2387 if (IS_ZEBRA_NEIGH_INACTIVE(n) || seq_change) {
2388 ZEBRA_NEIGH_SET_ACTIVE(n);
2389 n->loc_seq = zmac->loc_seq;
2390 if (!(zvrf->dup_addr_detect &&
2391 zvrf->dad_freeze && !!CHECK_FLAG(n->flags,
2392 ZEBRA_NEIGH_DUPLICATE)))
2393 zvni_neigh_send_add_to_client(
2394 zvni->vni, &n->ip, &n->emac,
2395 n->flags, n->loc_seq);
2396 }
2397 }
2398 }
2399 }
2400
2401 /*
2402 * Process all neighbors associated with a local MAC upon the MAC being
2403 * deleted.
2404 */
2405 static void zvni_process_neigh_on_local_mac_del(zebra_vni_t *zvni,
2406 zebra_mac_t *zmac)
2407 {
2408 zebra_neigh_t *n = NULL;
2409 struct listnode *node = NULL;
2410 char buf[ETHER_ADDR_STRLEN];
2411
2412 if (IS_ZEBRA_DEBUG_VXLAN)
2413 zlog_debug("Processing neighbors on local MAC %s DEL, VNI %u",
2414 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
2415 zvni->vni);
2416
2417 /* Walk all local neighbors and mark as inactive and inform
2418 * BGP, if needed.
2419 * TBD: There is currently no handling for remote neighbors. We
2420 * don't expect them to exist, if they do, do we install the MAC
2421 * as a remote MAC and the neighbor as remote?
2422 */
2423 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
2424 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2425 if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
2426 ZEBRA_NEIGH_SET_INACTIVE(n);
2427 n->loc_seq = 0;
2428 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
2429 &n->emac, 0, ZEBRA_NEIGH_ACTIVE);
2430 }
2431 }
2432 }
2433 }
2434
2435 /*
2436 * Process all neighbors associated with a MAC upon the MAC being remotely
2437 * learnt.
2438 */
2439 static void zvni_process_neigh_on_remote_mac_add(zebra_vni_t *zvni,
2440 zebra_mac_t *zmac)
2441 {
2442 zebra_neigh_t *n = NULL;
2443 struct listnode *node = NULL;
2444 char buf[ETHER_ADDR_STRLEN];
2445
2446 if (IS_ZEBRA_DEBUG_VXLAN)
2447 zlog_debug("Processing neighbors on remote MAC %s ADD, VNI %u",
2448 prefix_mac2str(&zmac->macaddr, buf, sizeof(buf)),
2449 zvni->vni);
2450
2451 /* Walk all local neighbors and mark as inactive and inform
2452 * BGP, if needed.
2453 */
2454 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
2455 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
2456 if (IS_ZEBRA_NEIGH_ACTIVE(n)) {
2457 ZEBRA_NEIGH_SET_INACTIVE(n);
2458 n->loc_seq = 0;
2459 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
2460 &n->emac, 0, ZEBRA_NEIGH_ACTIVE);
2461 }
2462 }
2463 }
2464 }
2465
2466 /*
2467 * Process all neighbors associated with a remote MAC upon the MAC being
2468 * deleted.
2469 */
2470 static void zvni_process_neigh_on_remote_mac_del(zebra_vni_t *zvni,
2471 zebra_mac_t *zmac)
2472 {
2473 /* NOTE: Currently a NO-OP. */
2474 }
2475
2476 static void zvni_probe_neigh_on_mac_add(zebra_vni_t *zvni, zebra_mac_t *zmac)
2477 {
2478 zebra_neigh_t *nbr = NULL;
2479 struct listnode *node = NULL;
2480
2481 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, nbr)) {
2482 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL) &&
2483 IS_ZEBRA_NEIGH_INACTIVE(nbr))
2484 zvni_neigh_probe(zvni, nbr);
2485 }
2486 }
2487
2488 /*
2489 * Inform BGP about local neighbor addition.
2490 */
2491 static int zvni_neigh_send_add_to_client(vni_t vni, struct ipaddr *ip,
2492 struct ethaddr *macaddr,
2493 uint8_t neigh_flags,
2494 uint32_t seq)
2495 {
2496 uint8_t flags = 0;
2497
2498 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_DEF_GW))
2499 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
2500 /* Set router flag (R-bit) based on local neigh entry add */
2501 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_ROUTER_FLAG))
2502 SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
2503 if (CHECK_FLAG(neigh_flags, ZEBRA_NEIGH_SVI_IP))
2504 SET_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP);
2505
2506 return zvni_macip_send_msg_to_client(vni, macaddr, ip, flags,
2507 seq, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_ADD);
2508 }
2509
2510 /*
2511 * Inform BGP about local neighbor deletion.
2512 */
2513 static int zvni_neigh_send_del_to_client(vni_t vni, struct ipaddr *ip,
2514 struct ethaddr *macaddr, uint8_t flags,
2515 int state)
2516 {
2517 return zvni_macip_send_msg_to_client(vni, macaddr, ip, flags,
2518 0, state, ZEBRA_MACIP_DEL);
2519 }
2520
2521 /*
2522 * Install remote neighbor into the kernel.
2523 */
2524 static int zvni_neigh_install(zebra_vni_t *zvni, zebra_neigh_t *n)
2525 {
2526 struct zebra_if *zif;
2527 struct zebra_l2info_vxlan *vxl;
2528 struct interface *vlan_if;
2529 int flags;
2530 int ret = 0;
2531
2532 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
2533 return 0;
2534
2535 zif = zvni->vxlan_if->info;
2536 if (!zif)
2537 return -1;
2538 vxl = &zif->l2info.vxl;
2539
2540 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
2541 if (!vlan_if)
2542 return -1;
2543
2544 flags = DPLANE_NTF_EXT_LEARNED;
2545 if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
2546 flags |= DPLANE_NTF_ROUTER;
2547 ZEBRA_NEIGH_SET_ACTIVE(n);
2548
2549 dplane_neigh_add(vlan_if, &n->ip, &n->emac, flags);
2550
2551 return ret;
2552 }
2553
2554 /*
2555 * Uninstall remote neighbor from the kernel.
2556 */
2557 static int zvni_neigh_uninstall(zebra_vni_t *zvni, zebra_neigh_t *n)
2558 {
2559 struct zebra_if *zif;
2560 struct zebra_l2info_vxlan *vxl;
2561 struct interface *vlan_if;
2562
2563 if (!(n->flags & ZEBRA_NEIGH_REMOTE))
2564 return 0;
2565
2566 if (!zvni->vxlan_if) {
2567 if (IS_ZEBRA_DEBUG_VXLAN)
2568 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
2569 zvni->vni, zvni);
2570 return -1;
2571 }
2572
2573 zif = zvni->vxlan_if->info;
2574 if (!zif)
2575 return -1;
2576 vxl = &zif->l2info.vxl;
2577 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
2578 if (!vlan_if)
2579 return -1;
2580
2581 ZEBRA_NEIGH_SET_INACTIVE(n);
2582 n->loc_seq = 0;
2583
2584 dplane_neigh_delete(vlan_if, &n->ip);
2585
2586 return 0;
2587 }
2588
2589 /*
2590 * Probe neighbor from the kernel.
2591 */
2592 static int zvni_neigh_probe(zebra_vni_t *zvni, zebra_neigh_t *n)
2593 {
2594 struct zebra_if *zif;
2595 struct zebra_l2info_vxlan *vxl;
2596 struct interface *vlan_if;
2597
2598 zif = zvni->vxlan_if->info;
2599 if (!zif)
2600 return -1;
2601 vxl = &zif->l2info.vxl;
2602
2603 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
2604 if (!vlan_if)
2605 return -1;
2606
2607 dplane_neigh_update(vlan_if, &n->ip, &n->emac);
2608
2609 return 0;
2610 }
2611
2612 /*
2613 * Install neighbor hash entry - called upon access VLAN change.
2614 */
2615 static void zvni_install_neigh_hash(struct hash_bucket *bucket, void *ctxt)
2616 {
2617 zebra_neigh_t *n;
2618 struct neigh_walk_ctx *wctx = ctxt;
2619
2620 n = (zebra_neigh_t *)bucket->data;
2621
2622 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
2623 zvni_neigh_install(wctx->zvni, n);
2624 }
2625
2626 /* Get the VRR interface for SVI if any */
2627 struct interface *zebra_get_vrr_intf_for_svi(struct interface *ifp)
2628 {
2629 struct zebra_vrf *zvrf = NULL;
2630 struct interface *tmp_if = NULL;
2631 struct zebra_if *zif = NULL;
2632
2633 zvrf = vrf_info_lookup(ifp->vrf_id);
2634 assert(zvrf);
2635
2636 FOR_ALL_INTERFACES (zvrf->vrf, tmp_if) {
2637 zif = tmp_if->info;
2638 if (!zif)
2639 continue;
2640
2641 if (!IS_ZEBRA_IF_MACVLAN(tmp_if))
2642 continue;
2643
2644 if (zif->link == ifp)
2645 return tmp_if;
2646 }
2647
2648 return NULL;
2649 }
2650
2651 static int zvni_del_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni)
2652 {
2653 struct listnode *cnode = NULL, *cnnode = NULL;
2654 struct connected *c = NULL;
2655 struct ethaddr macaddr;
2656
2657 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
2658
2659 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
2660 struct ipaddr ip;
2661
2662 memset(&ip, 0, sizeof(struct ipaddr));
2663 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
2664 continue;
2665
2666 if (c->address->family == AF_INET) {
2667 ip.ipa_type = IPADDR_V4;
2668 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
2669 sizeof(struct in_addr));
2670 } else if (c->address->family == AF_INET6) {
2671 ip.ipa_type = IPADDR_V6;
2672 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
2673 sizeof(struct in6_addr));
2674 } else {
2675 continue;
2676 }
2677
2678 zvni_gw_macip_del(ifp, zvni, &ip);
2679 }
2680
2681 return 0;
2682 }
2683
2684 static int zvni_add_macip_for_intf(struct interface *ifp, zebra_vni_t *zvni)
2685 {
2686 struct listnode *cnode = NULL, *cnnode = NULL;
2687 struct connected *c = NULL;
2688 struct ethaddr macaddr;
2689
2690 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
2691
2692 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
2693 struct ipaddr ip;
2694
2695 memset(&ip, 0, sizeof(struct ipaddr));
2696 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
2697 continue;
2698
2699 if (c->address->family == AF_INET) {
2700 ip.ipa_type = IPADDR_V4;
2701 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
2702 sizeof(struct in_addr));
2703 } else if (c->address->family == AF_INET6) {
2704 ip.ipa_type = IPADDR_V6;
2705 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
2706 sizeof(struct in6_addr));
2707 } else {
2708 continue;
2709 }
2710
2711 zvni_gw_macip_add(ifp, zvni, &macaddr, &ip);
2712 }
2713 return 0;
2714 }
2715
2716
2717 static int zvni_advertise_subnet(zebra_vni_t *zvni, struct interface *ifp,
2718 int advertise)
2719 {
2720 struct listnode *cnode = NULL, *cnnode = NULL;
2721 struct connected *c = NULL;
2722 struct ethaddr macaddr;
2723
2724 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
2725
2726 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
2727 struct prefix p;
2728
2729 memcpy(&p, c->address, sizeof(struct prefix));
2730
2731 /* skip link local address */
2732 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
2733 continue;
2734
2735 apply_mask(&p);
2736 if (advertise)
2737 ip_prefix_send_to_client(ifp->vrf_id, &p,
2738 ZEBRA_IP_PREFIX_ROUTE_ADD);
2739 else
2740 ip_prefix_send_to_client(ifp->vrf_id, &p,
2741 ZEBRA_IP_PREFIX_ROUTE_DEL);
2742 }
2743 return 0;
2744 }
2745
2746 /*
2747 * zvni_gw_macip_add_to_client
2748 */
2749 static int zvni_gw_macip_add(struct interface *ifp, zebra_vni_t *zvni,
2750 struct ethaddr *macaddr, struct ipaddr *ip)
2751 {
2752 char buf[ETHER_ADDR_STRLEN];
2753 char buf2[INET6_ADDRSTRLEN];
2754 zebra_neigh_t *n = NULL;
2755 zebra_mac_t *mac = NULL;
2756 struct zebra_if *zif = NULL;
2757 struct zebra_l2info_vxlan *vxl = NULL;
2758
2759 zif = zvni->vxlan_if->info;
2760 if (!zif)
2761 return -1;
2762
2763 vxl = &zif->l2info.vxl;
2764
2765 mac = zvni_mac_lookup(zvni, macaddr);
2766 if (!mac) {
2767 mac = zvni_mac_add(zvni, macaddr);
2768 if (!mac) {
2769 flog_err(EC_ZEBRA_MAC_ADD_FAILED,
2770 "Failed to add MAC %s intf %s(%u) VID %u",
2771 prefix_mac2str(macaddr, buf, sizeof(buf)),
2772 ifp->name, ifp->ifindex, vxl->access_vlan);
2773 return -1;
2774 }
2775 }
2776
2777 /* Set "local" forwarding info. */
2778 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
2779 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
2780 SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW);
2781 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
2782 mac->fwd_info.local.ifindex = ifp->ifindex;
2783 mac->fwd_info.local.vid = vxl->access_vlan;
2784
2785 n = zvni_neigh_lookup(zvni, ip);
2786 if (!n) {
2787 n = zvni_neigh_add(zvni, ip, macaddr);
2788 if (!n) {
2789 flog_err(
2790 EC_ZEBRA_MAC_ADD_FAILED,
2791 "Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
2792 ipaddr2str(ip, buf2, sizeof(buf2)),
2793 prefix_mac2str(macaddr, buf, sizeof(buf)),
2794 ifp->name, ifp->ifindex, zvni->vni);
2795 return -1;
2796 }
2797 }
2798
2799 /* Set "local" forwarding info. */
2800 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
2801 ZEBRA_NEIGH_SET_ACTIVE(n);
2802 memcpy(&n->emac, macaddr, ETH_ALEN);
2803 n->ifindex = ifp->ifindex;
2804
2805 /* Only advertise in BGP if the knob is enabled */
2806 if (advertise_gw_macip_enabled(zvni)) {
2807
2808 SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW);
2809 SET_FLAG(n->flags, ZEBRA_NEIGH_DEF_GW);
2810 /* Set Router flag (R-bit) */
2811 if (ip->ipa_type == IPADDR_V6)
2812 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
2813
2814 if (IS_ZEBRA_DEBUG_VXLAN)
2815 zlog_debug(
2816 "SVI %s(%u) L2-VNI %u, sending GW MAC %s IP %s add to BGP with flags 0x%x",
2817 ifp->name, ifp->ifindex, zvni->vni,
2818 prefix_mac2str(macaddr, buf, sizeof(buf)),
2819 ipaddr2str(ip, buf2, sizeof(buf2)), n->flags);
2820
2821 zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
2822 n->flags, n->loc_seq);
2823 } else if (advertise_svi_macip_enabled(zvni)) {
2824
2825 SET_FLAG(n->flags, ZEBRA_NEIGH_SVI_IP);
2826 if (IS_ZEBRA_DEBUG_VXLAN)
2827 zlog_debug(
2828 "SVI %s(%u) L2-VNI %u, sending SVI MAC %s IP %s add to BGP with flags 0x%x",
2829 ifp->name, ifp->ifindex, zvni->vni,
2830 prefix_mac2str(macaddr, buf, sizeof(buf)),
2831 ipaddr2str(ip, buf2, sizeof(buf2)), n->flags);
2832
2833 zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
2834 n->flags, n->loc_seq);
2835 }
2836
2837 return 0;
2838 }
2839
2840 /*
2841 * zvni_gw_macip_del_from_client
2842 */
2843 static int zvni_gw_macip_del(struct interface *ifp, zebra_vni_t *zvni,
2844 struct ipaddr *ip)
2845 {
2846 char buf1[ETHER_ADDR_STRLEN];
2847 char buf2[INET6_ADDRSTRLEN];
2848 zebra_neigh_t *n = NULL;
2849 zebra_mac_t *mac = NULL;
2850
2851 /* If the neigh entry is not present nothing to do*/
2852 n = zvni_neigh_lookup(zvni, ip);
2853 if (!n)
2854 return 0;
2855
2856 /* mac entry should be present */
2857 mac = zvni_mac_lookup(zvni, &n->emac);
2858 if (!mac) {
2859 if (IS_ZEBRA_DEBUG_VXLAN)
2860 zlog_debug("MAC %s doesn't exist for neigh %s on VNI %u",
2861 prefix_mac2str(&n->emac,
2862 buf1, sizeof(buf1)),
2863 ipaddr2str(ip, buf2, sizeof(buf2)),
2864 zvni->vni);
2865 return -1;
2866 }
2867
2868 /* If the entry is not local nothing to do*/
2869 if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL))
2870 return -1;
2871
2872 /* only need to delete the entry from bgp if we sent it before */
2873 if (IS_ZEBRA_DEBUG_VXLAN)
2874 zlog_debug(
2875 "%u:SVI %s(%u) VNI %u, sending GW MAC %s IP %s del to BGP",
2876 ifp->vrf_id, ifp->name, ifp->ifindex, zvni->vni,
2877 prefix_mac2str(&(n->emac), buf1, sizeof(buf1)),
2878 ipaddr2str(ip, buf2, sizeof(buf2)));
2879
2880 /* Remove neighbor from BGP. */
2881 zvni_neigh_send_del_to_client(zvni->vni, &n->ip, &n->emac,
2882 ZEBRA_MACIP_TYPE_GW, ZEBRA_NEIGH_ACTIVE);
2883
2884 /* Delete this neighbor entry. */
2885 zvni_neigh_del(zvni, n);
2886
2887 /* see if the mac needs to be deleted as well*/
2888 if (mac)
2889 zvni_deref_ip2mac(zvni, mac);
2890
2891 return 0;
2892 }
2893
2894 static void zvni_gw_macip_del_for_vni_hash(struct hash_bucket *bucket,
2895 void *ctxt)
2896 {
2897 zebra_vni_t *zvni = NULL;
2898 struct zebra_if *zif = NULL;
2899 struct zebra_l2info_vxlan zl2_info;
2900 struct interface *vlan_if = NULL;
2901 struct interface *vrr_if = NULL;
2902 struct interface *ifp;
2903
2904 /* Add primary SVI MAC*/
2905 zvni = (zebra_vni_t *)bucket->data;
2906
2907 /* Global (Zvrf) advertise-default-gw is disabled,
2908 * but zvni advertise-default-gw is enabled
2909 */
2910 if (zvni->advertise_gw_macip) {
2911 if (IS_ZEBRA_DEBUG_VXLAN)
2912 zlog_debug("VNI: %u GW-MACIP enabled, retain gw-macip",
2913 zvni->vni);
2914 return;
2915 }
2916
2917 ifp = zvni->vxlan_if;
2918 if (!ifp)
2919 return;
2920 zif = ifp->info;
2921
2922 /* If down or not mapped to a bridge, we're done. */
2923 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
2924 return;
2925
2926 zl2_info = zif->l2info.vxl;
2927
2928 vlan_if =
2929 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
2930 if (!vlan_if)
2931 return;
2932
2933 /* Del primary MAC-IP */
2934 zvni_del_macip_for_intf(vlan_if, zvni);
2935
2936 /* Del VRR MAC-IP - if any*/
2937 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
2938 if (vrr_if)
2939 zvni_del_macip_for_intf(vrr_if, zvni);
2940
2941 return;
2942 }
2943
2944 static void zvni_gw_macip_add_for_vni_hash(struct hash_bucket *bucket,
2945 void *ctxt)
2946 {
2947 zebra_vni_t *zvni = NULL;
2948 struct zebra_if *zif = NULL;
2949 struct zebra_l2info_vxlan zl2_info;
2950 struct interface *vlan_if = NULL;
2951 struct interface *vrr_if = NULL;
2952 struct interface *ifp = NULL;
2953
2954 zvni = (zebra_vni_t *)bucket->data;
2955
2956 ifp = zvni->vxlan_if;
2957 if (!ifp)
2958 return;
2959 zif = ifp->info;
2960
2961 /* If down or not mapped to a bridge, we're done. */
2962 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
2963 return;
2964 zl2_info = zif->l2info.vxl;
2965
2966 vlan_if =
2967 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
2968 if (!vlan_if)
2969 return;
2970
2971 /* Add primary SVI MAC-IP */
2972 zvni_add_macip_for_intf(vlan_if, zvni);
2973
2974 if (advertise_gw_macip_enabled(zvni)) {
2975 /* Add VRR MAC-IP - if any*/
2976 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
2977 if (vrr_if)
2978 zvni_add_macip_for_intf(vrr_if, zvni);
2979 }
2980
2981 return;
2982 }
2983
2984 static void zvni_svi_macip_del_for_vni_hash(struct hash_bucket *bucket,
2985 void *ctxt)
2986 {
2987 zebra_vni_t *zvni = NULL;
2988 struct zebra_if *zif = NULL;
2989 struct zebra_l2info_vxlan zl2_info;
2990 struct interface *vlan_if = NULL;
2991 struct interface *ifp;
2992
2993 /* Add primary SVI MAC*/
2994 zvni = (zebra_vni_t *)bucket->data;
2995 if (!zvni)
2996 return;
2997
2998 /* Global(vrf) advertise-svi-ip disabled, but zvni advertise-svi-ip
2999 * enabled
3000 */
3001 if (zvni->advertise_svi_macip) {
3002 if (IS_ZEBRA_DEBUG_VXLAN)
3003 zlog_debug("VNI: %u SVI-MACIP enabled, retain svi-macip",
3004 zvni->vni);
3005 return;
3006 }
3007
3008 ifp = zvni->vxlan_if;
3009 if (!ifp)
3010 return;
3011 zif = ifp->info;
3012
3013 /* If down or not mapped to a bridge, we're done. */
3014 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
3015 return;
3016
3017 zl2_info = zif->l2info.vxl;
3018
3019 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
3020 zif->brslave_info.br_if);
3021 if (!vlan_if)
3022 return;
3023
3024 /* Del primary MAC-IP */
3025 zvni_del_macip_for_intf(vlan_if, zvni);
3026
3027 return;
3028 }
3029
3030 static int zvni_local_neigh_update(zebra_vni_t *zvni,
3031 struct interface *ifp,
3032 struct ipaddr *ip,
3033 struct ethaddr *macaddr,
3034 bool is_router)
3035 {
3036 char buf[ETHER_ADDR_STRLEN];
3037 char buf2[INET6_ADDRSTRLEN];
3038 struct zebra_vrf *zvrf;
3039 zebra_neigh_t *n = NULL;
3040 zebra_mac_t *zmac = NULL, *old_zmac = NULL;
3041 uint32_t old_mac_seq = 0, mac_new_seq = 0;
3042 bool upd_mac_seq = false;
3043 bool neigh_mac_change = false;
3044 bool neigh_on_hold = false;
3045 bool neigh_was_remote = false;
3046 bool do_dad = false;
3047 struct in_addr vtep_ip = {.s_addr = 0};
3048
3049 /* Check if the MAC exists. */
3050 zmac = zvni_mac_lookup(zvni, macaddr);
3051 if (!zmac) {
3052 /* create a dummy MAC if the MAC is not already present */
3053 if (IS_ZEBRA_DEBUG_VXLAN)
3054 zlog_debug(
3055 "AUTO MAC %s created for neigh %s on VNI %u",
3056 prefix_mac2str(macaddr, buf, sizeof(buf)),
3057 ipaddr2str(ip, buf2, sizeof(buf2)), zvni->vni);
3058
3059 zmac = zvni_mac_add(zvni, macaddr);
3060 if (!zmac) {
3061 zlog_debug("Failed to add MAC %s VNI %u",
3062 prefix_mac2str(macaddr, buf, sizeof(buf)),
3063 zvni->vni);
3064 return -1;
3065 }
3066
3067 memset(&zmac->fwd_info, 0, sizeof(zmac->fwd_info));
3068 memset(&zmac->flags, 0, sizeof(uint32_t));
3069 SET_FLAG(zmac->flags, ZEBRA_MAC_AUTO);
3070 } else {
3071 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
3072 /*
3073 * We don't change the MAC to local upon a neighbor
3074 * learn event, we wait for the explicit local MAC
3075 * learn. However, we have to compute its sequence
3076 * number in preparation for when it actually turns
3077 * local.
3078 */
3079 upd_mac_seq = true;
3080 }
3081 }
3082
3083 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
3084 if (!zvrf) {
3085 if (IS_ZEBRA_DEBUG_VXLAN)
3086 zlog_debug("\tUnable to find vrf for: %d",
3087 zvni->vxlan_if->vrf_id);
3088 return -1;
3089 }
3090
3091 /* Check if the neighbor exists. */
3092 n = zvni_neigh_lookup(zvni, ip);
3093 if (!n) {
3094 /* New neighbor - create */
3095 n = zvni_neigh_add(zvni, ip, macaddr);
3096 if (!n) {
3097 flog_err(
3098 EC_ZEBRA_MAC_ADD_FAILED,
3099 "Failed to add neighbor %s MAC %s intf %s(%u) -> VNI %u",
3100 ipaddr2str(ip, buf2, sizeof(buf2)),
3101 prefix_mac2str(macaddr, buf, sizeof(buf)),
3102 ifp->name, ifp->ifindex, zvni->vni);
3103 return -1;
3104 }
3105 /* Set "local" forwarding info. */
3106 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
3107 n->ifindex = ifp->ifindex;
3108 } else {
3109 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
3110 bool mac_different;
3111 bool cur_is_router;
3112
3113 /* Note any changes and see if of interest to BGP. */
3114 mac_different = (memcmp(n->emac.octet,
3115 macaddr->octet, ETH_ALEN) != 0) ? 1 : 0;
3116 cur_is_router = !!CHECK_FLAG(n->flags,
3117 ZEBRA_NEIGH_ROUTER_FLAG);
3118 if (!mac_different && is_router == cur_is_router) {
3119 if (IS_ZEBRA_DEBUG_VXLAN)
3120 zlog_debug(
3121 "\tIgnoring entry mac is the same and is_router == cur_is_router");
3122 n->ifindex = ifp->ifindex;
3123 return 0;
3124 }
3125
3126 if (!mac_different) {
3127 bool is_neigh_freezed = false;
3128
3129 /* Only the router flag has changed. */
3130 if (is_router)
3131 SET_FLAG(n->flags,
3132 ZEBRA_NEIGH_ROUTER_FLAG);
3133 else
3134 UNSET_FLAG(n->flags,
3135 ZEBRA_NEIGH_ROUTER_FLAG);
3136
3137 /* Neigh is in freeze state and freeze action
3138 * is enabled, do not send update to client.
3139 */
3140 is_neigh_freezed = (zvrf->dup_addr_detect &&
3141 zvrf->dad_freeze &&
3142 CHECK_FLAG(n->flags,
3143 ZEBRA_NEIGH_DUPLICATE));
3144
3145 if (IS_ZEBRA_NEIGH_ACTIVE(n) &&
3146 !is_neigh_freezed)
3147 return zvni_neigh_send_add_to_client(
3148 zvni->vni, ip, macaddr,
3149 n->flags, n->loc_seq);
3150 else {
3151 if (IS_ZEBRA_DEBUG_VXLAN)
3152 zlog_debug(
3153 "\tNeighbor active and frozen");
3154 }
3155 return 0;
3156 }
3157
3158 /* The MAC has changed, need to issue a delete
3159 * first as this means a different MACIP route.
3160 * Also, need to do some unlinking/relinking.
3161 * We also need to update the MAC's sequence number
3162 * in different situations.
3163 */
3164 if (IS_ZEBRA_NEIGH_ACTIVE(n))
3165 zvni_neigh_send_del_to_client(zvni->vni, &n->ip,
3166 &n->emac, 0, n->state);
3167 old_zmac = zvni_mac_lookup(zvni, &n->emac);
3168 if (old_zmac) {
3169 old_mac_seq = CHECK_FLAG(old_zmac->flags,
3170 ZEBRA_MAC_REMOTE) ?
3171 old_zmac->rem_seq : old_zmac->loc_seq;
3172 neigh_mac_change = upd_mac_seq = true;
3173 listnode_delete(old_zmac->neigh_list, n);
3174 zvni_deref_ip2mac(zvni, old_zmac);
3175 }
3176
3177 /* Update the forwarding info. */
3178 n->ifindex = ifp->ifindex;
3179 memcpy(&n->emac, macaddr, ETH_ALEN);
3180
3181 /* Link to new MAC */
3182 listnode_add_sort(zmac->neigh_list, n);
3183 } else if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
3184 /*
3185 * Neighbor has moved from remote to local. Its
3186 * MAC could have also changed as part of the move.
3187 */
3188 if (memcmp(n->emac.octet, macaddr->octet,
3189 ETH_ALEN) != 0) {
3190 old_zmac = zvni_mac_lookup(zvni, &n->emac);
3191 if (old_zmac) {
3192 old_mac_seq = CHECK_FLAG(
3193 old_zmac->flags,
3194 ZEBRA_MAC_REMOTE) ?
3195 old_zmac->rem_seq :
3196 old_zmac->loc_seq;
3197 neigh_mac_change = upd_mac_seq = true;
3198 listnode_delete(old_zmac->neigh_list,
3199 n);
3200 zvni_deref_ip2mac(zvni, old_zmac);
3201 }
3202
3203 /* Link to new MAC */
3204 memcpy(&n->emac, macaddr, ETH_ALEN);
3205 listnode_add_sort(zmac->neigh_list, n);
3206 }
3207 /* Based on Mobility event Scenario-B from the
3208 * draft, neigh's previous state was remote treat this
3209 * event for DAD.
3210 */
3211 neigh_was_remote = true;
3212 vtep_ip = n->r_vtep_ip;
3213 /* Mark appropriately */
3214 UNSET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
3215 n->r_vtep_ip.s_addr = INADDR_ANY;
3216 SET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
3217 n->ifindex = ifp->ifindex;
3218 }
3219 }
3220
3221 /* If MAC was previously remote, or the neighbor had a different
3222 * MAC earlier, recompute the sequence number.
3223 */
3224 if (upd_mac_seq) {
3225 uint32_t seq1, seq2;
3226
3227 seq1 = CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE) ?
3228 zmac->rem_seq + 1 : zmac->loc_seq;
3229 seq2 = neigh_mac_change ? old_mac_seq + 1 : 0;
3230 mac_new_seq = zmac->loc_seq < MAX(seq1, seq2) ?
3231 MAX(seq1, seq2) : zmac->loc_seq;
3232 }
3233
3234 /* Mark Router flag (R-bit) */
3235 if (is_router)
3236 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
3237 else
3238 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
3239
3240 /* Check old and/or new MAC detected as duplicate mark
3241 * the neigh as duplicate
3242 */
3243 if (zebra_vxlan_ip_inherit_dad_from_mac(zvrf, old_zmac, zmac, n)) {
3244 flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
3245 "VNI %u: MAC %s IP %s detected as duplicate during local update, inherit duplicate from MAC",
3246 zvni->vni,
3247 prefix_mac2str(macaddr, buf, sizeof(buf)),
3248 ipaddr2str(&n->ip, buf2, sizeof(buf2)));
3249 }
3250
3251 /* For IP Duplicate Address Detection (DAD) is trigger,
3252 * when the event is extended mobility based on scenario-B
3253 * from the draft, IP/Neigh's MAC binding changed and
3254 * neigh's previous state was remote.
3255 */
3256 if (neigh_mac_change && neigh_was_remote)
3257 do_dad = true;
3258
3259 zebra_vxlan_dup_addr_detect_for_neigh(zvrf, n, vtep_ip, do_dad,
3260 &neigh_on_hold, true);
3261
3262 /* Before we program this in BGP, we need to check if MAC is locally
3263 * learnt. If not, force neighbor to be inactive and reset its seq.
3264 */
3265 if (!CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL)) {
3266 ZEBRA_NEIGH_SET_INACTIVE(n);
3267 n->loc_seq = 0;
3268 zmac->loc_seq = mac_new_seq;
3269 return 0;
3270 }
3271
3272 /* If the MAC's sequence number has changed, inform the MAC and all
3273 * neighbors associated with the MAC to BGP, else just inform this
3274 * neighbor.
3275 */
3276 if (upd_mac_seq && zmac->loc_seq != mac_new_seq) {
3277 if (IS_ZEBRA_DEBUG_VXLAN)
3278 zlog_debug("Seq changed for MAC %s VNI %u - old %u new %u",
3279 prefix_mac2str(macaddr, buf, sizeof(buf)),
3280 zvni->vni, zmac->loc_seq, mac_new_seq);
3281 zmac->loc_seq = mac_new_seq;
3282 if (zvni_mac_send_add_to_client(zvni->vni, macaddr,
3283 zmac->flags, zmac->loc_seq))
3284 return -1;
3285 zvni_process_neigh_on_local_mac_change(zvni, zmac, 1);
3286 return 0;
3287 }
3288
3289 n->loc_seq = zmac->loc_seq;
3290
3291 if (!neigh_on_hold) {
3292 ZEBRA_NEIGH_SET_ACTIVE(n);
3293
3294 return zvni_neigh_send_add_to_client(zvni->vni, ip, macaddr,
3295 n->flags, n->loc_seq);
3296 } else {
3297 if (IS_ZEBRA_DEBUG_VXLAN)
3298 zlog_debug("\tNeighbor on hold not sending");
3299 }
3300 return 0;
3301 }
3302
3303 static int zvni_remote_neigh_update(zebra_vni_t *zvni,
3304 struct interface *ifp,
3305 struct ipaddr *ip,
3306 struct ethaddr *macaddr,
3307 uint16_t state)
3308 {
3309 char buf[ETHER_ADDR_STRLEN];
3310 char buf2[INET6_ADDRSTRLEN];
3311 zebra_neigh_t *n = NULL;
3312 zebra_mac_t *zmac = NULL;
3313
3314 /* If the neighbor is unknown, there is no further action. */
3315 n = zvni_neigh_lookup(zvni, ip);
3316 if (!n)
3317 return 0;
3318
3319 /* If a remote entry, see if it needs to be refreshed */
3320 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
3321 #ifdef GNU_LINUX
3322 if (state & NUD_STALE)
3323 zvni_neigh_install(zvni, n);
3324 #endif
3325 } else {
3326 /* We got a "remote" neighbor notification for an entry
3327 * we think is local. This can happen in a multihoming
3328 * scenario - but only if the MAC is already "remote".
3329 * Just mark our entry as "remote".
3330 */
3331 zmac = zvni_mac_lookup(zvni, macaddr);
3332 if (!zmac || !CHECK_FLAG(zmac->flags, ZEBRA_MAC_REMOTE)) {
3333 zlog_debug(
3334 "Ignore remote neigh %s (MAC %s) on L2-VNI %u - MAC unknown or local",
3335 ipaddr2str(&n->ip, buf2, sizeof(buf2)),
3336 prefix_mac2str(macaddr, buf, sizeof(buf)),
3337 zvni->vni);
3338 return -1;
3339 }
3340
3341 UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
3342 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
3343 ZEBRA_NEIGH_SET_ACTIVE(n);
3344 n->r_vtep_ip = zmac->fwd_info.r_vtep_ip;
3345 }
3346
3347 return 0;
3348 }
3349
3350 /*
3351 * Make hash key for MAC.
3352 */
3353 static unsigned int mac_hash_keymake(const void *p)
3354 {
3355 const zebra_mac_t *pmac = p;
3356 const void *pnt = (void *)pmac->macaddr.octet;
3357
3358 return jhash(pnt, ETH_ALEN, 0xa5a5a55a);
3359 }
3360
3361 /*
3362 * Compare two MAC addresses.
3363 */
3364 static bool mac_cmp(const void *p1, const void *p2)
3365 {
3366 const zebra_mac_t *pmac1 = p1;
3367 const zebra_mac_t *pmac2 = p2;
3368
3369 if (pmac1 == NULL && pmac2 == NULL)
3370 return true;
3371
3372 if (pmac1 == NULL || pmac2 == NULL)
3373 return false;
3374
3375 return (memcmp(pmac1->macaddr.octet, pmac2->macaddr.octet, ETH_ALEN)
3376 == 0);
3377 }
3378
3379 /*
3380 * Callback to allocate MAC hash entry.
3381 */
3382 static void *zvni_mac_alloc(void *p)
3383 {
3384 const zebra_mac_t *tmp_mac = p;
3385 zebra_mac_t *mac;
3386
3387 mac = XCALLOC(MTYPE_MAC, sizeof(zebra_mac_t));
3388 *mac = *tmp_mac;
3389
3390 return ((void *)mac);
3391 }
3392
3393 /*
3394 * Add MAC entry.
3395 */
3396 static zebra_mac_t *zvni_mac_add(zebra_vni_t *zvni, struct ethaddr *macaddr)
3397 {
3398 zebra_mac_t tmp_mac;
3399 zebra_mac_t *mac = NULL;
3400
3401 memset(&tmp_mac, 0, sizeof(zebra_mac_t));
3402 memcpy(&tmp_mac.macaddr, macaddr, ETH_ALEN);
3403 mac = hash_get(zvni->mac_table, &tmp_mac, zvni_mac_alloc);
3404 assert(mac);
3405
3406 mac->zvni = zvni;
3407 mac->dad_mac_auto_recovery_timer = NULL;
3408
3409 mac->neigh_list = list_new();
3410 mac->neigh_list->cmp = neigh_list_cmp;
3411
3412 return mac;
3413 }
3414
3415 /*
3416 * Delete MAC entry.
3417 */
3418 static int zvni_mac_del(zebra_vni_t *zvni, zebra_mac_t *mac)
3419 {
3420 zebra_mac_t *tmp_mac;
3421
3422 /* Cancel auto recovery */
3423 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
3424
3425 list_delete(&mac->neigh_list);
3426
3427 /* Free the VNI hash entry and allocated memory. */
3428 tmp_mac = hash_release(zvni->mac_table, mac);
3429 XFREE(MTYPE_MAC, tmp_mac);
3430
3431 return 0;
3432 }
3433
3434 static bool zvni_check_mac_del_from_db(struct mac_walk_ctx *wctx,
3435 zebra_mac_t *mac)
3436 {
3437 if ((wctx->flags & DEL_LOCAL_MAC) &&
3438 (mac->flags & ZEBRA_MAC_LOCAL))
3439 return true;
3440 else if ((wctx->flags & DEL_REMOTE_MAC) &&
3441 (mac->flags & ZEBRA_MAC_REMOTE))
3442 return true;
3443 else if ((wctx->flags & DEL_REMOTE_MAC_FROM_VTEP) &&
3444 (mac->flags & ZEBRA_MAC_REMOTE) &&
3445 IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &wctx->r_vtep_ip))
3446 return true;
3447 else if ((wctx->flags & DEL_LOCAL_MAC) &&
3448 (mac->flags & ZEBRA_MAC_AUTO) &&
3449 !listcount(mac->neigh_list)) {
3450 if (IS_ZEBRA_DEBUG_VXLAN) {
3451 char buf[ETHER_ADDR_STRLEN];
3452
3453 zlog_debug("%s: Del MAC %s flags 0x%x",
3454 __PRETTY_FUNCTION__,
3455 prefix_mac2str(&mac->macaddr,
3456 buf, sizeof(buf)),
3457 mac->flags);
3458 }
3459 wctx->uninstall = 0;
3460
3461 return true;
3462 }
3463
3464 return false;
3465 }
3466
3467 /*
3468 * Free MAC hash entry (callback)
3469 */
3470 static void zvni_mac_del_hash_entry(struct hash_bucket *bucket, void *arg)
3471 {
3472 struct mac_walk_ctx *wctx = arg;
3473 zebra_mac_t *mac = bucket->data;
3474
3475 if (zvni_check_mac_del_from_db(wctx, mac)) {
3476 if (wctx->upd_client && (mac->flags & ZEBRA_MAC_LOCAL)) {
3477 zvni_mac_send_del_to_client(wctx->zvni->vni,
3478 &mac->macaddr);
3479 }
3480 if (wctx->uninstall)
3481 zvni_mac_uninstall(wctx->zvni, mac);
3482
3483 zvni_mac_del(wctx->zvni, mac);
3484 }
3485
3486 return;
3487 }
3488
3489 /*
3490 * Delete all MAC entries from specific VTEP for a particular VNI.
3491 */
3492 static void zvni_mac_del_from_vtep(zebra_vni_t *zvni, int uninstall,
3493 struct in_addr *r_vtep_ip)
3494 {
3495 struct mac_walk_ctx wctx;
3496
3497 if (!zvni->mac_table)
3498 return;
3499
3500 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
3501 wctx.zvni = zvni;
3502 wctx.uninstall = uninstall;
3503 wctx.flags = DEL_REMOTE_MAC_FROM_VTEP;
3504 wctx.r_vtep_ip = *r_vtep_ip;
3505
3506 hash_iterate(zvni->mac_table, zvni_mac_del_hash_entry, &wctx);
3507 }
3508
3509 /*
3510 * Delete all MAC entries for this VNI.
3511 */
3512 static void zvni_mac_del_all(zebra_vni_t *zvni, int uninstall, int upd_client,
3513 uint32_t flags)
3514 {
3515 struct mac_walk_ctx wctx;
3516
3517 if (!zvni->mac_table)
3518 return;
3519
3520 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
3521 wctx.zvni = zvni;
3522 wctx.uninstall = uninstall;
3523 wctx.upd_client = upd_client;
3524 wctx.flags = flags;
3525
3526 hash_iterate(zvni->mac_table, zvni_mac_del_hash_entry, &wctx);
3527 }
3528
3529 /*
3530 * Look up MAC hash entry.
3531 */
3532 static zebra_mac_t *zvni_mac_lookup(zebra_vni_t *zvni, struct ethaddr *mac)
3533 {
3534 zebra_mac_t tmp;
3535 zebra_mac_t *pmac;
3536
3537 memset(&tmp, 0, sizeof(tmp));
3538 memcpy(&tmp.macaddr, mac, ETH_ALEN);
3539 pmac = hash_lookup(zvni->mac_table, &tmp);
3540
3541 return pmac;
3542 }
3543
3544 /*
3545 * Inform BGP about local MAC addition.
3546 */
3547 static int zvni_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr,
3548 uint8_t mac_flags, uint32_t seq)
3549 {
3550 uint8_t flags = 0;
3551
3552 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_STICKY))
3553 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
3554 if (CHECK_FLAG(mac_flags, ZEBRA_MAC_DEF_GW))
3555 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
3556
3557 return zvni_macip_send_msg_to_client(vni, macaddr, NULL, flags,
3558 seq, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_ADD);
3559 }
3560
3561 /*
3562 * Inform BGP about local MAC deletion.
3563 */
3564 static int zvni_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr)
3565 {
3566 return zvni_macip_send_msg_to_client(vni, macaddr, NULL, 0 /* flags */,
3567 0 /* seq */, ZEBRA_NEIGH_ACTIVE, ZEBRA_MACIP_DEL);
3568 }
3569
3570 /*
3571 * Map port or (port, VLAN) to a VNI. This is invoked upon getting MAC
3572 * notifications, to see if they are of interest.
3573 */
3574 static zebra_vni_t *zvni_map_vlan(struct interface *ifp,
3575 struct interface *br_if, vlanid_t vid)
3576 {
3577 struct zebra_ns *zns;
3578 struct route_node *rn;
3579 struct interface *tmp_if = NULL;
3580 struct zebra_if *zif;
3581 struct zebra_l2info_bridge *br;
3582 struct zebra_l2info_vxlan *vxl = NULL;
3583 uint8_t bridge_vlan_aware;
3584 zebra_vni_t *zvni;
3585 int found = 0;
3586
3587 /* Determine if bridge is VLAN-aware or not */
3588 zif = br_if->info;
3589 assert(zif);
3590 br = &zif->l2info.br;
3591 bridge_vlan_aware = br->vlan_aware;
3592
3593 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
3594 /* TODO: Optimize with a hash. */
3595 zns = zebra_ns_lookup(NS_DEFAULT);
3596 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3597 tmp_if = (struct interface *)rn->info;
3598 if (!tmp_if)
3599 continue;
3600 zif = tmp_if->info;
3601 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
3602 continue;
3603 if (!if_is_operative(tmp_if))
3604 continue;
3605 vxl = &zif->l2info.vxl;
3606
3607 if (zif->brslave_info.br_if != br_if)
3608 continue;
3609
3610 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
3611 found = 1;
3612 break;
3613 }
3614 }
3615
3616 if (!found)
3617 return NULL;
3618
3619 zvni = zvni_lookup(vxl->vni);
3620 return zvni;
3621 }
3622
3623 /*
3624 * Map SVI and associated bridge to a VNI. This is invoked upon getting
3625 * neighbor notifications, to see if they are of interest.
3626 */
3627 static zebra_vni_t *zvni_from_svi(struct interface *ifp,
3628 struct interface *br_if)
3629 {
3630 struct zebra_ns *zns;
3631 struct route_node *rn;
3632 struct interface *tmp_if = NULL;
3633 struct zebra_if *zif;
3634 struct zebra_l2info_bridge *br;
3635 struct zebra_l2info_vxlan *vxl = NULL;
3636 uint8_t bridge_vlan_aware;
3637 vlanid_t vid = 0;
3638 zebra_vni_t *zvni;
3639 int found = 0;
3640
3641 if (!br_if)
3642 return NULL;
3643
3644 /* Make sure the linked interface is a bridge. */
3645 if (!IS_ZEBRA_IF_BRIDGE(br_if))
3646 return NULL;
3647
3648 /* Determine if bridge is VLAN-aware or not */
3649 zif = br_if->info;
3650 assert(zif);
3651 br = &zif->l2info.br;
3652 bridge_vlan_aware = br->vlan_aware;
3653 if (bridge_vlan_aware) {
3654 struct zebra_l2info_vlan *vl;
3655
3656 if (!IS_ZEBRA_IF_VLAN(ifp))
3657 return NULL;
3658
3659 zif = ifp->info;
3660 assert(zif);
3661 vl = &zif->l2info.vl;
3662 vid = vl->vid;
3663 }
3664
3665 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
3666 /* TODO: Optimize with a hash. */
3667 zns = zebra_ns_lookup(NS_DEFAULT);
3668 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3669 tmp_if = (struct interface *)rn->info;
3670 if (!tmp_if)
3671 continue;
3672 zif = tmp_if->info;
3673 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
3674 continue;
3675 if (!if_is_operative(tmp_if))
3676 continue;
3677 vxl = &zif->l2info.vxl;
3678
3679 if (zif->brslave_info.br_if != br_if)
3680 continue;
3681
3682 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
3683 found = 1;
3684 break;
3685 }
3686 }
3687
3688 if (!found)
3689 return NULL;
3690
3691 zvni = zvni_lookup(vxl->vni);
3692 return zvni;
3693 }
3694
3695 /* Map to SVI on bridge corresponding to specified VLAN. This can be one
3696 * of two cases:
3697 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN interface
3698 * linked to the bridge
3699 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge interface
3700 * itself
3701 */
3702 static struct interface *zvni_map_to_svi(vlanid_t vid, struct interface *br_if)
3703 {
3704 struct zebra_ns *zns;
3705 struct route_node *rn;
3706 struct interface *tmp_if = NULL;
3707 struct zebra_if *zif;
3708 struct zebra_l2info_bridge *br;
3709 struct zebra_l2info_vlan *vl;
3710 uint8_t bridge_vlan_aware;
3711 int found = 0;
3712
3713 /* Defensive check, caller expected to invoke only with valid bridge. */
3714 if (!br_if)
3715 return NULL;
3716
3717 /* Determine if bridge is VLAN-aware or not */
3718 zif = br_if->info;
3719 assert(zif);
3720 br = &zif->l2info.br;
3721 bridge_vlan_aware = br->vlan_aware;
3722
3723 /* Check oper status of the SVI. */
3724 if (!bridge_vlan_aware)
3725 return if_is_operative(br_if) ? br_if : NULL;
3726
3727 /* Identify corresponding VLAN interface. */
3728 /* TODO: Optimize with a hash. */
3729 zns = zebra_ns_lookup(NS_DEFAULT);
3730 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3731 tmp_if = (struct interface *)rn->info;
3732 /* Check oper status of the SVI. */
3733 if (!tmp_if || !if_is_operative(tmp_if))
3734 continue;
3735 zif = tmp_if->info;
3736 if (!zif || zif->zif_type != ZEBRA_IF_VLAN
3737 || zif->link != br_if)
3738 continue;
3739 vl = (struct zebra_l2info_vlan *)&zif->l2info.vl;
3740
3741 if (vl->vid == vid) {
3742 found = 1;
3743 break;
3744 }
3745 }
3746
3747 return found ? tmp_if : NULL;
3748 }
3749
3750 /* Map to MAC-VLAN interface corresponding to specified SVI interface.
3751 */
3752 static struct interface *zvni_map_to_macvlan(struct interface *br_if,
3753 struct interface *svi_if)
3754 {
3755 struct zebra_ns *zns;
3756 struct route_node *rn;
3757 struct interface *tmp_if = NULL;
3758 struct zebra_if *zif;
3759 int found = 0;
3760
3761 /* Defensive check, caller expected to invoke only with valid bridge. */
3762 if (!br_if)
3763 return NULL;
3764
3765 if (!svi_if) {
3766 zlog_debug("svi_if is not passed.");
3767 return NULL;
3768 }
3769
3770 /* Determine if bridge is VLAN-aware or not */
3771 zif = br_if->info;
3772 assert(zif);
3773
3774 /* Identify corresponding VLAN interface. */
3775 zns = zebra_ns_lookup(NS_DEFAULT);
3776 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
3777 tmp_if = (struct interface *)rn->info;
3778 /* Check oper status of the SVI. */
3779 if (!tmp_if || !if_is_operative(tmp_if))
3780 continue;
3781 zif = tmp_if->info;
3782
3783 if (!zif || zif->zif_type != ZEBRA_IF_MACVLAN)
3784 continue;
3785
3786 if (zif->link == svi_if) {
3787 found = 1;
3788 break;
3789 }
3790 }
3791
3792 return found ? tmp_if : NULL;
3793 }
3794
3795
3796 /*
3797 * Install remote MAC into the forwarding plane.
3798 */
3799 static int zvni_mac_install(zebra_vni_t *zvni, zebra_mac_t *mac)
3800 {
3801 const struct zebra_if *zif, *br_zif;
3802 const struct zebra_l2info_vxlan *vxl;
3803 bool sticky;
3804 enum zebra_dplane_result res;
3805 const struct interface *br_ifp;
3806 vlanid_t vid;
3807
3808 if (!(mac->flags & ZEBRA_MAC_REMOTE))
3809 return 0;
3810
3811 zif = zvni->vxlan_if->info;
3812 if (!zif)
3813 return -1;
3814
3815 br_ifp = zif->brslave_info.br_if;
3816 if (br_ifp == NULL)
3817 return -1;
3818
3819 vxl = &zif->l2info.vxl;
3820
3821 sticky = !!CHECK_FLAG(mac->flags,
3822 (ZEBRA_MAC_STICKY | ZEBRA_MAC_REMOTE_DEF_GW));
3823
3824 br_zif = (const struct zebra_if *)(br_ifp->info);
3825
3826 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
3827 vid = vxl->access_vlan;
3828 else
3829 vid = 0;
3830
3831 res = dplane_mac_add(zvni->vxlan_if, br_ifp, vid,
3832 &mac->macaddr, mac->fwd_info.r_vtep_ip, sticky);
3833 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
3834 return 0;
3835 else
3836 return -1;
3837 }
3838
3839 /*
3840 * Uninstall remote MAC from the forwarding plane.
3841 */
3842 static int zvni_mac_uninstall(zebra_vni_t *zvni, zebra_mac_t *mac)
3843 {
3844 const struct zebra_if *zif, *br_zif;
3845 const struct zebra_l2info_vxlan *vxl;
3846 struct in_addr vtep_ip;
3847 const struct interface *ifp, *br_ifp;
3848 vlanid_t vid;
3849 enum zebra_dplane_result res;
3850
3851 if (!(mac->flags & ZEBRA_MAC_REMOTE))
3852 return 0;
3853
3854 if (!zvni->vxlan_if) {
3855 if (IS_ZEBRA_DEBUG_VXLAN)
3856 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
3857 zvni->vni, zvni);
3858 return -1;
3859 }
3860
3861 zif = zvni->vxlan_if->info;
3862 if (!zif)
3863 return -1;
3864
3865 br_ifp = zif->brslave_info.br_if;
3866 if (br_ifp == NULL)
3867 return -1;
3868
3869 vxl = &zif->l2info.vxl;
3870
3871 br_zif = (const struct zebra_if *)br_ifp->info;
3872
3873 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
3874 vid = vxl->access_vlan;
3875 else
3876 vid = 0;
3877
3878 ifp = zvni->vxlan_if;
3879 vtep_ip = mac->fwd_info.r_vtep_ip;
3880
3881 res = dplane_mac_del(ifp, br_ifp, vid, &mac->macaddr, vtep_ip);
3882 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
3883 return 0;
3884 else
3885 return -1;
3886 }
3887
3888 /*
3889 * Install MAC hash entry - called upon access VLAN change.
3890 */
3891 static void zvni_install_mac_hash(struct hash_bucket *bucket, void *ctxt)
3892 {
3893 zebra_mac_t *mac;
3894 struct mac_walk_ctx *wctx = ctxt;
3895
3896 mac = (zebra_mac_t *)bucket->data;
3897
3898 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
3899 zvni_mac_install(wctx->zvni, mac);
3900 }
3901
3902 /*
3903 * Count of remote neighbors referencing this MAC.
3904 */
3905 static int remote_neigh_count(zebra_mac_t *zmac)
3906 {
3907 zebra_neigh_t *n = NULL;
3908 struct listnode *node = NULL;
3909 int count = 0;
3910
3911 for (ALL_LIST_ELEMENTS_RO(zmac->neigh_list, node, n)) {
3912 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
3913 count++;
3914 }
3915
3916 return count;
3917 }
3918
3919 /*
3920 * Decrement neighbor refcount of MAC; uninstall and free it if
3921 * appropriate.
3922 */
3923 static void zvni_deref_ip2mac(zebra_vni_t *zvni, zebra_mac_t *mac)
3924 {
3925 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO))
3926 return;
3927
3928 /* If all remote neighbors referencing a remote MAC go away,
3929 * we need to uninstall the MAC.
3930 */
3931 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) &&
3932 remote_neigh_count(mac) == 0) {
3933 zvni_mac_uninstall(zvni, mac);
3934 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
3935 }
3936
3937 /* If no neighbors, delete the MAC. */
3938 if (list_isempty(mac->neigh_list))
3939 zvni_mac_del(zvni, mac);
3940 }
3941
3942 /*
3943 * Read and populate local MACs and neighbors corresponding to this VNI.
3944 */
3945 static void zvni_read_mac_neigh(zebra_vni_t *zvni, struct interface *ifp)
3946 {
3947 struct zebra_ns *zns;
3948 struct zebra_if *zif;
3949 struct interface *vlan_if;
3950 struct zebra_l2info_vxlan *vxl;
3951 struct interface *vrr_if;
3952
3953 zif = ifp->info;
3954 vxl = &zif->l2info.vxl;
3955 zns = zebra_ns_lookup(NS_DEFAULT);
3956
3957 if (IS_ZEBRA_DEBUG_VXLAN)
3958 zlog_debug(
3959 "Reading MAC FDB and Neighbors for intf %s(%u) VNI %u master %u",
3960 ifp->name, ifp->ifindex, zvni->vni,
3961 zif->brslave_info.bridge_ifindex);
3962
3963 macfdb_read_for_bridge(zns, ifp, zif->brslave_info.br_if);
3964 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
3965 if (vlan_if) {
3966
3967 /* Add SVI MAC-IP */
3968 zvni_add_macip_for_intf(vlan_if, zvni);
3969
3970 /* Add VRR MAC-IP - if any*/
3971 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
3972 if (vrr_if)
3973 zvni_add_macip_for_intf(vrr_if, zvni);
3974
3975 neigh_read_for_vlan(zns, vlan_if);
3976 }
3977 }
3978
3979 /*
3980 * Hash function for VNI.
3981 */
3982 static unsigned int vni_hash_keymake(const void *p)
3983 {
3984 const zebra_vni_t *zvni = p;
3985
3986 return (jhash_1word(zvni->vni, 0));
3987 }
3988
3989 /*
3990 * Compare 2 VNI hash entries.
3991 */
3992 static bool vni_hash_cmp(const void *p1, const void *p2)
3993 {
3994 const zebra_vni_t *zvni1 = p1;
3995 const zebra_vni_t *zvni2 = p2;
3996
3997 return (zvni1->vni == zvni2->vni);
3998 }
3999
4000 static int vni_list_cmp(void *p1, void *p2)
4001 {
4002 const zebra_vni_t *zvni1 = p1;
4003 const zebra_vni_t *zvni2 = p2;
4004
4005 if (zvni1->vni == zvni2->vni)
4006 return 0;
4007 return (zvni1->vni < zvni2->vni) ? -1 : 1;
4008 }
4009
4010 /*
4011 * Callback to allocate VNI hash entry.
4012 */
4013 static void *zvni_alloc(void *p)
4014 {
4015 const zebra_vni_t *tmp_vni = p;
4016 zebra_vni_t *zvni;
4017
4018 zvni = XCALLOC(MTYPE_ZVNI, sizeof(zebra_vni_t));
4019 zvni->vni = tmp_vni->vni;
4020 return ((void *)zvni);
4021 }
4022
4023 /*
4024 * Look up VNI hash entry.
4025 */
4026 static zebra_vni_t *zvni_lookup(vni_t vni)
4027 {
4028 struct zebra_vrf *zvrf;
4029 zebra_vni_t tmp_vni;
4030 zebra_vni_t *zvni = NULL;
4031
4032 zvrf = zebra_vrf_get_evpn();
4033 assert(zvrf);
4034 memset(&tmp_vni, 0, sizeof(zebra_vni_t));
4035 tmp_vni.vni = vni;
4036 zvni = hash_lookup(zvrf->vni_table, &tmp_vni);
4037
4038 return zvni;
4039 }
4040
4041 /*
4042 * Add VNI hash entry.
4043 */
4044 static zebra_vni_t *zvni_add(vni_t vni)
4045 {
4046 struct zebra_vrf *zvrf;
4047 zebra_vni_t tmp_zvni;
4048 zebra_vni_t *zvni = NULL;
4049
4050 zvrf = zebra_vrf_get_evpn();
4051 assert(zvrf);
4052 memset(&tmp_zvni, 0, sizeof(zebra_vni_t));
4053 tmp_zvni.vni = vni;
4054 zvni = hash_get(zvrf->vni_table, &tmp_zvni, zvni_alloc);
4055 assert(zvni);
4056
4057 /* Create hash table for MAC */
4058 zvni->mac_table =
4059 hash_create(mac_hash_keymake, mac_cmp, "Zebra VNI MAC Table");
4060
4061 /* Create hash table for neighbors */
4062 zvni->neigh_table = hash_create(neigh_hash_keymake, neigh_cmp,
4063 "Zebra VNI Neighbor Table");
4064
4065 return zvni;
4066 }
4067
4068 /*
4069 * Delete VNI hash entry.
4070 */
4071 static int zvni_del(zebra_vni_t *zvni)
4072 {
4073 struct zebra_vrf *zvrf;
4074 zebra_vni_t *tmp_zvni;
4075
4076 zvrf = zebra_vrf_get_evpn();
4077 assert(zvrf);
4078
4079 zvni->vxlan_if = NULL;
4080
4081 /* Remove references to the BUM mcast grp */
4082 zebra_vxlan_sg_deref(zvni->local_vtep_ip, zvni->mcast_grp);
4083
4084 /* Free the neighbor hash table. */
4085 hash_free(zvni->neigh_table);
4086 zvni->neigh_table = NULL;
4087
4088 /* Free the MAC hash table. */
4089 hash_free(zvni->mac_table);
4090 zvni->mac_table = NULL;
4091
4092 /* Free the VNI hash entry and allocated memory. */
4093 tmp_zvni = hash_release(zvrf->vni_table, zvni);
4094 XFREE(MTYPE_ZVNI, tmp_zvni);
4095
4096 return 0;
4097 }
4098
4099 /*
4100 * Inform BGP about local VNI addition.
4101 */
4102 static int zvni_send_add_to_client(zebra_vni_t *zvni)
4103 {
4104 struct zserv *client;
4105 struct stream *s;
4106
4107 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
4108 /* BGP may not be running. */
4109 if (!client)
4110 return 0;
4111
4112 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
4113
4114 zclient_create_header(s, ZEBRA_VNI_ADD, zebra_vrf_get_evpn_id());
4115 stream_putl(s, zvni->vni);
4116 stream_put_in_addr(s, &zvni->local_vtep_ip);
4117 stream_put(s, &zvni->vrf_id, sizeof(vrf_id_t)); /* tenant vrf */
4118 stream_put_in_addr(s, &zvni->mcast_grp);
4119
4120 /* Write packet size. */
4121 stream_putw_at(s, 0, stream_get_endp(s));
4122
4123 if (IS_ZEBRA_DEBUG_VXLAN)
4124 zlog_debug("Send VNI_ADD %u %s tenant vrf %s to %s", zvni->vni,
4125 inet_ntoa(zvni->local_vtep_ip),
4126 vrf_id_to_name(zvni->vrf_id),
4127 zebra_route_string(client->proto));
4128
4129 client->vniadd_cnt++;
4130 return zserv_send_message(client, s);
4131 }
4132
4133 /*
4134 * Inform BGP about local VNI deletion.
4135 */
4136 static int zvni_send_del_to_client(vni_t vni)
4137 {
4138 struct zserv *client;
4139 struct stream *s;
4140
4141 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
4142 /* BGP may not be running. */
4143 if (!client)
4144 return 0;
4145
4146 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
4147 stream_reset(s);
4148
4149 zclient_create_header(s, ZEBRA_VNI_DEL, zebra_vrf_get_evpn_id());
4150 stream_putl(s, vni);
4151
4152 /* Write packet size. */
4153 stream_putw_at(s, 0, stream_get_endp(s));
4154
4155 if (IS_ZEBRA_DEBUG_VXLAN)
4156 zlog_debug("Send VNI_DEL %u to %s", vni,
4157 zebra_route_string(client->proto));
4158
4159 client->vnidel_cnt++;
4160 return zserv_send_message(client, s);
4161 }
4162
4163 /*
4164 * Build the VNI hash table by going over the VxLAN interfaces. This
4165 * is called when EVPN (advertise-all-vni) is enabled.
4166 */
4167 static void zvni_build_hash_table(void)
4168 {
4169 struct zebra_ns *zns;
4170 struct route_node *rn;
4171 struct interface *ifp;
4172
4173 /* Walk VxLAN interfaces and create VNI hash. */
4174 zns = zebra_ns_lookup(NS_DEFAULT);
4175 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
4176 vni_t vni;
4177 zebra_vni_t *zvni = NULL;
4178 zebra_l3vni_t *zl3vni = NULL;
4179 struct zebra_if *zif;
4180 struct zebra_l2info_vxlan *vxl;
4181
4182 ifp = (struct interface *)rn->info;
4183 if (!ifp)
4184 continue;
4185 zif = ifp->info;
4186 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
4187 continue;
4188
4189 vxl = &zif->l2info.vxl;
4190 vni = vxl->vni;
4191
4192 /* L3-VNI and L2-VNI are handled seperately */
4193 zl3vni = zl3vni_lookup(vni);
4194 if (zl3vni) {
4195
4196 if (IS_ZEBRA_DEBUG_VXLAN)
4197 zlog_debug(
4198 "create L3-VNI hash for Intf %s(%u) L3-VNI %u",
4199 ifp->name, ifp->ifindex, vni);
4200
4201 /* associate with vxlan_if */
4202 zl3vni->local_vtep_ip = vxl->vtep_ip;
4203 zl3vni->vxlan_if = ifp;
4204
4205 /*
4206 * we need to associate with SVI.
4207 * we can associate with svi-if only after association
4208 * with vxlan-intf is complete
4209 */
4210 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
4211
4212 /* Associate l3vni to mac-vlan and extract VRR MAC */
4213 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
4214
4215 if (IS_ZEBRA_DEBUG_VXLAN)
4216 zlog_debug("create l3vni %u svi_if %s mac_vlan_if %s",
4217 vni, zl3vni->svi_if ? zl3vni->svi_if->name
4218 : "NIL",
4219 zl3vni->mac_vlan_if ?
4220 zl3vni->mac_vlan_if->name : "NIL");
4221
4222 if (is_l3vni_oper_up(zl3vni))
4223 zebra_vxlan_process_l3vni_oper_up(zl3vni);
4224
4225 } else {
4226 struct interface *vlan_if = NULL;
4227
4228 if (IS_ZEBRA_DEBUG_VXLAN)
4229 zlog_debug(
4230 "Create L2-VNI hash for intf %s(%u) L2-VNI %u local IP %s",
4231 ifp->name, ifp->ifindex, vni,
4232 inet_ntoa(vxl->vtep_ip));
4233
4234 /* VNI hash entry is expected to exist, if the BGP process is killed */
4235 zvni = zvni_lookup(vni);
4236 if (zvni) {
4237 zlog_debug(
4238 "VNI hash already present for IF %s(%u) L2-VNI %u",
4239 ifp->name, ifp->ifindex, vni);
4240
4241 /*
4242 * Inform BGP if intf is up and mapped to
4243 * bridge.
4244 */
4245 if (if_is_operative(ifp) &&
4246 zif->brslave_info.br_if)
4247 zvni_send_add_to_client(zvni);
4248
4249 /* Send Local MAC-entries to client */
4250 zvni_send_mac_to_client(zvni);
4251
4252 /* Send Loval Neighbor entries to client */
4253 zvni_send_neigh_to_client(zvni);
4254 } else {
4255 zvni = zvni_add(vni);
4256 if (!zvni) {
4257 zlog_debug(
4258 "Failed to add VNI hash, IF %s(%u) L2-VNI %u",
4259 ifp->name, ifp->ifindex, vni);
4260 return;
4261 }
4262
4263 if (zvni->local_vtep_ip.s_addr !=
4264 vxl->vtep_ip.s_addr ||
4265 zvni->mcast_grp.s_addr !=
4266 vxl->mcast_grp.s_addr) {
4267 zebra_vxlan_sg_deref(
4268 zvni->local_vtep_ip,
4269 zvni->mcast_grp);
4270 zebra_vxlan_sg_ref(vxl->vtep_ip,
4271 vxl->mcast_grp);
4272 zvni->local_vtep_ip = vxl->vtep_ip;
4273 zvni->mcast_grp = vxl->mcast_grp;
4274 }
4275 zvni->vxlan_if = ifp;
4276 vlan_if = zvni_map_to_svi(vxl->access_vlan,
4277 zif->brslave_info.br_if);
4278 if (vlan_if) {
4279 zvni->vrf_id = vlan_if->vrf_id;
4280 zl3vni = zl3vni_from_vrf(
4281 vlan_if->vrf_id);
4282 if (zl3vni)
4283 listnode_add_sort(
4284 zl3vni->l2vnis, zvni);
4285 }
4286
4287 /*
4288 * Inform BGP if intf is up and mapped to
4289 * bridge.
4290 */
4291 if (if_is_operative(ifp) &&
4292 zif->brslave_info.br_if)
4293 zvni_send_add_to_client(zvni);
4294 }
4295 }
4296 }
4297 }
4298
4299 /*
4300 * See if remote VTEP matches with prefix.
4301 */
4302 static int zvni_vtep_match(struct in_addr *vtep_ip, zebra_vtep_t *zvtep)
4303 {
4304 return (IPV4_ADDR_SAME(vtep_ip, &zvtep->vtep_ip));
4305 }
4306
4307 /*
4308 * Locate remote VTEP in VNI hash table.
4309 */
4310 static zebra_vtep_t *zvni_vtep_find(zebra_vni_t *zvni, struct in_addr *vtep_ip)
4311 {
4312 zebra_vtep_t *zvtep;
4313
4314 if (!zvni)
4315 return NULL;
4316
4317 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
4318 if (zvni_vtep_match(vtep_ip, zvtep))
4319 break;
4320 }
4321
4322 return zvtep;
4323 }
4324
4325 /*
4326 * Add remote VTEP to VNI hash table.
4327 */
4328 static zebra_vtep_t *zvni_vtep_add(zebra_vni_t *zvni, struct in_addr *vtep_ip,
4329 int flood_control)
4330
4331 {
4332 zebra_vtep_t *zvtep;
4333
4334 zvtep = XCALLOC(MTYPE_ZVNI_VTEP, sizeof(zebra_vtep_t));
4335
4336 zvtep->vtep_ip = *vtep_ip;
4337 zvtep->flood_control = flood_control;
4338
4339 if (zvni->vteps)
4340 zvni->vteps->prev = zvtep;
4341 zvtep->next = zvni->vteps;
4342 zvni->vteps = zvtep;
4343
4344 return zvtep;
4345 }
4346
4347 /*
4348 * Remove remote VTEP from VNI hash table.
4349 */
4350 static int zvni_vtep_del(zebra_vni_t *zvni, zebra_vtep_t *zvtep)
4351 {
4352 if (zvtep->next)
4353 zvtep->next->prev = zvtep->prev;
4354 if (zvtep->prev)
4355 zvtep->prev->next = zvtep->next;
4356 else
4357 zvni->vteps = zvtep->next;
4358
4359 zvtep->prev = zvtep->next = NULL;
4360 XFREE(MTYPE_ZVNI_VTEP, zvtep);
4361
4362 return 0;
4363 }
4364
4365 /*
4366 * Delete all remote VTEPs for this VNI (upon VNI delete). Also
4367 * uninstall from kernel if asked to.
4368 */
4369 static int zvni_vtep_del_all(zebra_vni_t *zvni, int uninstall)
4370 {
4371 zebra_vtep_t *zvtep, *zvtep_next;
4372
4373 if (!zvni)
4374 return -1;
4375
4376 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep_next) {
4377 zvtep_next = zvtep->next;
4378 if (uninstall)
4379 zvni_vtep_uninstall(zvni, &zvtep->vtep_ip);
4380 zvni_vtep_del(zvni, zvtep);
4381 }
4382
4383 return 0;
4384 }
4385
4386 /*
4387 * Install remote VTEP into the kernel if the remote VTEP has asked
4388 * for head-end-replication.
4389 */
4390 static int zvni_vtep_install(zebra_vni_t *zvni, zebra_vtep_t *zvtep)
4391 {
4392 if (is_vxlan_flooding_head_end() &&
4393 (zvtep->flood_control == VXLAN_FLOOD_HEAD_END_REPL)) {
4394 if (ZEBRA_DPLANE_REQUEST_FAILURE ==
4395 dplane_vtep_add(zvni->vxlan_if,
4396 &zvtep->vtep_ip, zvni->vni))
4397 return -1;
4398 }
4399
4400 return 0;
4401 }
4402
4403 /*
4404 * Uninstall remote VTEP from the kernel.
4405 */
4406 static int zvni_vtep_uninstall(zebra_vni_t *zvni, struct in_addr *vtep_ip)
4407 {
4408 if (!zvni->vxlan_if) {
4409 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
4410 zvni->vni, zvni);
4411 return -1;
4412 }
4413
4414 if (ZEBRA_DPLANE_REQUEST_FAILURE ==
4415 dplane_vtep_delete(zvni->vxlan_if, vtep_ip, zvni->vni))
4416 return -1;
4417
4418 return 0;
4419 }
4420
4421 /*
4422 * Install or uninstall flood entries in the kernel corresponding to
4423 * remote VTEPs. This is invoked upon change to BUM handling.
4424 */
4425 static void zvni_handle_flooding_remote_vteps(struct hash_bucket *bucket,
4426 void *zvrf)
4427 {
4428 zebra_vni_t *zvni;
4429 zebra_vtep_t *zvtep;
4430
4431 zvni = (zebra_vni_t *)bucket->data;
4432 if (!zvni)
4433 return;
4434
4435 for (zvtep = zvni->vteps; zvtep; zvtep = zvtep->next) {
4436 if (is_vxlan_flooding_head_end())
4437 zvni_vtep_install(zvni, zvtep);
4438 else
4439 zvni_vtep_uninstall(zvni, &zvtep->vtep_ip);
4440 }
4441 }
4442
4443 /*
4444 * Cleanup VNI/VTEP and update kernel
4445 */
4446 static void zvni_cleanup_all(struct hash_bucket *bucket, void *arg)
4447 {
4448 zebra_vni_t *zvni = NULL;
4449 zebra_l3vni_t *zl3vni = NULL;
4450 struct zebra_vrf *zvrf = (struct zebra_vrf *)arg;
4451
4452 zvni = (zebra_vni_t *)bucket->data;
4453
4454 /* remove from l3-vni list */
4455 if (zvrf->l3vni)
4456 zl3vni = zl3vni_lookup(zvrf->l3vni);
4457 if (zl3vni)
4458 listnode_delete(zl3vni->l2vnis, zvni);
4459
4460 /* Free up all neighbors and MACs, if any. */
4461 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
4462 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
4463
4464 /* Free up all remote VTEPs, if any. */
4465 zvni_vtep_del_all(zvni, 1);
4466
4467 /* Delete the hash entry. */
4468 zvni_del(zvni);
4469 }
4470
4471 /* cleanup L3VNI */
4472 static void zl3vni_cleanup_all(struct hash_bucket *bucket, void *args)
4473 {
4474 zebra_l3vni_t *zl3vni = NULL;
4475
4476 zl3vni = (zebra_l3vni_t *)bucket->data;
4477
4478 zebra_vxlan_process_l3vni_oper_down(zl3vni);
4479 }
4480
4481 static void rb_find_or_add_host(struct host_rb_tree_entry *hrbe,
4482 struct prefix *host)
4483 {
4484 struct host_rb_entry lookup;
4485 struct host_rb_entry *hle;
4486
4487 memset(&lookup, 0, sizeof(lookup));
4488 memcpy(&lookup.p, host, sizeof(*host));
4489
4490 hle = RB_FIND(host_rb_tree_entry, hrbe, &lookup);
4491 if (hle)
4492 return;
4493
4494 hle = XCALLOC(MTYPE_HOST_PREFIX, sizeof(struct host_rb_entry));
4495 memcpy(hle, &lookup, sizeof(lookup));
4496
4497 RB_INSERT(host_rb_tree_entry, hrbe, hle);
4498 }
4499
4500 static void rb_delete_host(struct host_rb_tree_entry *hrbe, struct prefix *host)
4501 {
4502 struct host_rb_entry lookup;
4503 struct host_rb_entry *hle;
4504
4505 memset(&lookup, 0, sizeof(lookup));
4506 memcpy(&lookup.p, host, sizeof(*host));
4507
4508 hle = RB_FIND(host_rb_tree_entry, hrbe, &lookup);
4509 if (hle) {
4510 RB_REMOVE(host_rb_tree_entry, hrbe, hle);
4511 XFREE(MTYPE_HOST_PREFIX, hle);
4512 }
4513
4514 return;
4515 }
4516
4517 /*
4518 * Look up MAC hash entry.
4519 */
4520 static zebra_mac_t *zl3vni_rmac_lookup(zebra_l3vni_t *zl3vni,
4521 struct ethaddr *rmac)
4522 {
4523 zebra_mac_t tmp;
4524 zebra_mac_t *pmac;
4525
4526 memset(&tmp, 0, sizeof(tmp));
4527 memcpy(&tmp.macaddr, rmac, ETH_ALEN);
4528 pmac = hash_lookup(zl3vni->rmac_table, &tmp);
4529
4530 return pmac;
4531 }
4532
4533 /*
4534 * Callback to allocate RMAC hash entry.
4535 */
4536 static void *zl3vni_rmac_alloc(void *p)
4537 {
4538 const zebra_mac_t *tmp_rmac = p;
4539 zebra_mac_t *zrmac;
4540
4541 zrmac = XCALLOC(MTYPE_MAC, sizeof(zebra_mac_t));
4542 *zrmac = *tmp_rmac;
4543
4544 return ((void *)zrmac);
4545 }
4546
4547 /*
4548 * Add RMAC entry to l3-vni
4549 */
4550 static zebra_mac_t *zl3vni_rmac_add(zebra_l3vni_t *zl3vni, struct ethaddr *rmac)
4551 {
4552 zebra_mac_t tmp_rmac;
4553 zebra_mac_t *zrmac = NULL;
4554
4555 memset(&tmp_rmac, 0, sizeof(zebra_mac_t));
4556 memcpy(&tmp_rmac.macaddr, rmac, ETH_ALEN);
4557 zrmac = hash_get(zl3vni->rmac_table, &tmp_rmac, zl3vni_rmac_alloc);
4558 assert(zrmac);
4559
4560 RB_INIT(host_rb_tree_entry, &zrmac->host_rb);
4561
4562 SET_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE);
4563 SET_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC);
4564
4565 return zrmac;
4566 }
4567
4568 /*
4569 * Delete MAC entry.
4570 */
4571 static int zl3vni_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
4572 {
4573 zebra_mac_t *tmp_rmac;
4574 struct host_rb_entry *hle;
4575
4576 while (!RB_EMPTY(host_rb_tree_entry, &zrmac->host_rb)) {
4577 hle = RB_ROOT(host_rb_tree_entry, &zrmac->host_rb);
4578
4579 RB_REMOVE(host_rb_tree_entry, &zrmac->host_rb, hle);
4580 XFREE(MTYPE_HOST_PREFIX, hle);
4581 }
4582
4583 tmp_rmac = hash_release(zl3vni->rmac_table, zrmac);
4584 XFREE(MTYPE_MAC, tmp_rmac);
4585
4586 return 0;
4587 }
4588
4589 /*
4590 * Install remote RMAC into the forwarding plane.
4591 */
4592 static int zl3vni_rmac_install(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
4593 {
4594 const struct zebra_if *zif = NULL, *br_zif = NULL;
4595 const struct zebra_l2info_vxlan *vxl = NULL;
4596 const struct interface *br_ifp;
4597 enum zebra_dplane_result res;
4598 vlanid_t vid;
4599
4600 if (!(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE))
4601 || !(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC)))
4602 return 0;
4603
4604 zif = zl3vni->vxlan_if->info;
4605 if (!zif)
4606 return -1;
4607
4608 br_ifp = zif->brslave_info.br_if;
4609 if (br_ifp == NULL)
4610 return -1;
4611
4612 vxl = &zif->l2info.vxl;
4613
4614 br_zif = (const struct zebra_if *)br_ifp->info;
4615
4616 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
4617 vid = vxl->access_vlan;
4618 else
4619 vid = 0;
4620
4621 res = dplane_mac_add(zl3vni->vxlan_if, br_ifp, vid,
4622 &zrmac->macaddr, zrmac->fwd_info.r_vtep_ip, 0);
4623 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
4624 return 0;
4625 else
4626 return -1;
4627 }
4628
4629 /*
4630 * Uninstall remote RMAC from the forwarding plane.
4631 */
4632 static int zl3vni_rmac_uninstall(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac)
4633 {
4634 char buf[ETHER_ADDR_STRLEN];
4635 const struct zebra_if *zif = NULL, *br_zif;
4636 const struct zebra_l2info_vxlan *vxl = NULL;
4637 const struct interface *br_ifp;
4638 vlanid_t vid;
4639 enum zebra_dplane_result res;
4640
4641 if (!(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE))
4642 || !(CHECK_FLAG(zrmac->flags, ZEBRA_MAC_REMOTE_RMAC)))
4643 return 0;
4644
4645 if (!zl3vni->vxlan_if) {
4646 if (IS_ZEBRA_DEBUG_VXLAN)
4647 zlog_debug(
4648 "RMAC %s on L3-VNI %u hash %p couldn't be uninstalled - no vxlan_if",
4649 prefix_mac2str(&zrmac->macaddr,
4650 buf, sizeof(buf)),
4651 zl3vni->vni, zl3vni);
4652 return -1;
4653 }
4654
4655 zif = zl3vni->vxlan_if->info;
4656 if (!zif)
4657 return -1;
4658
4659 br_ifp = zif->brslave_info.br_if;
4660 if (br_ifp == NULL)
4661 return -1;
4662
4663 vxl = &zif->l2info.vxl;
4664
4665 br_zif = (const struct zebra_if *)br_ifp->info;
4666 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
4667 vid = vxl->access_vlan;
4668 else
4669 vid = 0;
4670
4671 res = dplane_mac_del(zl3vni->vxlan_if, br_ifp, vid,
4672 &zrmac->macaddr, zrmac->fwd_info.r_vtep_ip);
4673 if (res != ZEBRA_DPLANE_REQUEST_FAILURE)
4674 return 0;
4675 else
4676 return -1;
4677 }
4678
4679 /* handle rmac add */
4680 static int zl3vni_remote_rmac_add(zebra_l3vni_t *zl3vni, struct ethaddr *rmac,
4681 struct ipaddr *vtep_ip,
4682 struct prefix *host_prefix)
4683 {
4684 char buf[ETHER_ADDR_STRLEN];
4685 char buf1[INET6_ADDRSTRLEN];
4686 char buf2[PREFIX_STRLEN];
4687 zebra_mac_t *zrmac = NULL;
4688
4689 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
4690 if (!zrmac) {
4691
4692 /* Create the RMAC entry, or update its vtep, if necessary. */
4693 zrmac = zl3vni_rmac_add(zl3vni, rmac);
4694 if (!zrmac) {
4695 zlog_debug(
4696 "Failed to add RMAC %s L3VNI %u Remote VTEP %s, prefix %s",
4697 prefix_mac2str(rmac, buf, sizeof(buf)),
4698 zl3vni->vni,
4699 ipaddr2str(vtep_ip, buf1, sizeof(buf1)),
4700 prefix2str(host_prefix, buf2, sizeof(buf2)));
4701 return -1;
4702 }
4703 memset(&zrmac->fwd_info, 0, sizeof(zrmac->fwd_info));
4704 zrmac->fwd_info.r_vtep_ip = vtep_ip->ipaddr_v4;
4705
4706 /* Send RMAC for FPM processing */
4707 hook_call(zebra_rmac_update, zrmac, zl3vni, false,
4708 "new RMAC added");
4709
4710 /* install rmac in kernel */
4711 zl3vni_rmac_install(zl3vni, zrmac);
4712 } else if (!IPV4_ADDR_SAME(&zrmac->fwd_info.r_vtep_ip,
4713 &vtep_ip->ipaddr_v4)) {
4714 if (IS_ZEBRA_DEBUG_VXLAN)
4715 zlog_debug(
4716 "L3VNI %u Remote VTEP change(%s -> %s) for RMAC %s, prefix %s",
4717 zl3vni->vni,
4718 inet_ntoa(zrmac->fwd_info.r_vtep_ip),
4719 ipaddr2str(vtep_ip, buf1, sizeof(buf1)),
4720 prefix_mac2str(rmac, buf, sizeof(buf)),
4721 prefix2str(host_prefix, buf2, sizeof(buf2)));
4722
4723 zrmac->fwd_info.r_vtep_ip = vtep_ip->ipaddr_v4;
4724
4725 /* install rmac in kernel */
4726 zl3vni_rmac_install(zl3vni, zrmac);
4727 }
4728
4729 rb_find_or_add_host(&zrmac->host_rb, host_prefix);
4730
4731 return 0;
4732 }
4733
4734
4735 /* handle rmac delete */
4736 static void zl3vni_remote_rmac_del(zebra_l3vni_t *zl3vni, zebra_mac_t *zrmac,
4737 struct prefix *host_prefix)
4738 {
4739 rb_delete_host(&zrmac->host_rb, host_prefix);
4740
4741 if (RB_EMPTY(host_rb_tree_entry, &zrmac->host_rb)) {
4742 /* uninstall from kernel */
4743 zl3vni_rmac_uninstall(zl3vni, zrmac);
4744
4745 /* Send RMAC for FPM processing */
4746 hook_call(zebra_rmac_update, zrmac, zl3vni, true,
4747 "RMAC deleted");
4748
4749 /* del the rmac entry */
4750 zl3vni_rmac_del(zl3vni, zrmac);
4751 }
4752 }
4753
4754 /*
4755 * Look up nh hash entry on a l3-vni.
4756 */
4757 static zebra_neigh_t *zl3vni_nh_lookup(zebra_l3vni_t *zl3vni, struct ipaddr *ip)
4758 {
4759 zebra_neigh_t tmp;
4760 zebra_neigh_t *n;
4761
4762 memset(&tmp, 0, sizeof(tmp));
4763 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
4764 n = hash_lookup(zl3vni->nh_table, &tmp);
4765
4766 return n;
4767 }
4768
4769
4770 /*
4771 * Callback to allocate NH hash entry on L3-VNI.
4772 */
4773 static void *zl3vni_nh_alloc(void *p)
4774 {
4775 const zebra_neigh_t *tmp_n = p;
4776 zebra_neigh_t *n;
4777
4778 n = XCALLOC(MTYPE_NEIGH, sizeof(zebra_neigh_t));
4779 *n = *tmp_n;
4780
4781 return ((void *)n);
4782 }
4783
4784 /*
4785 * Add neighbor entry.
4786 */
4787 static zebra_neigh_t *zl3vni_nh_add(zebra_l3vni_t *zl3vni, struct ipaddr *ip,
4788 struct ethaddr *mac)
4789 {
4790 zebra_neigh_t tmp_n;
4791 zebra_neigh_t *n = NULL;
4792
4793 memset(&tmp_n, 0, sizeof(zebra_neigh_t));
4794 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
4795 n = hash_get(zl3vni->nh_table, &tmp_n, zl3vni_nh_alloc);
4796 assert(n);
4797
4798 RB_INIT(host_rb_tree_entry, &n->host_rb);
4799
4800 memcpy(&n->emac, mac, ETH_ALEN);
4801 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
4802 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE_NH);
4803
4804 return n;
4805 }
4806
4807 /*
4808 * Delete neighbor entry.
4809 */
4810 static int zl3vni_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
4811 {
4812 zebra_neigh_t *tmp_n;
4813 struct host_rb_entry *hle;
4814
4815 while (!RB_EMPTY(host_rb_tree_entry, &n->host_rb)) {
4816 hle = RB_ROOT(host_rb_tree_entry, &n->host_rb);
4817
4818 RB_REMOVE(host_rb_tree_entry, &n->host_rb, hle);
4819 XFREE(MTYPE_HOST_PREFIX, hle);
4820 }
4821
4822 tmp_n = hash_release(zl3vni->nh_table, n);
4823 XFREE(MTYPE_NEIGH, tmp_n);
4824
4825 return 0;
4826 }
4827
4828 /*
4829 * Install remote nh as neigh into the kernel.
4830 */
4831 static int zl3vni_nh_install(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
4832 {
4833 uint8_t flags;
4834 int ret = 0;
4835
4836 if (!is_l3vni_oper_up(zl3vni))
4837 return -1;
4838
4839 if (!(n->flags & ZEBRA_NEIGH_REMOTE)
4840 || !(n->flags & ZEBRA_NEIGH_REMOTE_NH))
4841 return 0;
4842
4843 flags = DPLANE_NTF_EXT_LEARNED;
4844 if (n->flags & ZEBRA_NEIGH_ROUTER_FLAG)
4845 flags |= DPLANE_NTF_ROUTER;
4846
4847 dplane_neigh_add(zl3vni->svi_if, &n->ip, &n->emac, flags);
4848
4849 return ret;
4850 }
4851
4852 /*
4853 * Uninstall remote nh from the kernel.
4854 */
4855 static int zl3vni_nh_uninstall(zebra_l3vni_t *zl3vni, zebra_neigh_t *n)
4856 {
4857 if (!(n->flags & ZEBRA_NEIGH_REMOTE)
4858 || !(n->flags & ZEBRA_NEIGH_REMOTE_NH))
4859 return 0;
4860
4861 if (!zl3vni->svi_if || !if_is_operative(zl3vni->svi_if))
4862 return 0;
4863
4864 dplane_neigh_delete(zl3vni->svi_if, &n->ip);
4865
4866 return 0;
4867 }
4868
4869 /* add remote vtep as a neigh entry */
4870 static int zl3vni_remote_nh_add(zebra_l3vni_t *zl3vni, struct ipaddr *vtep_ip,
4871 struct ethaddr *rmac,
4872 struct prefix *host_prefix)
4873 {
4874 char buf[ETHER_ADDR_STRLEN];
4875 char buf1[ETHER_ADDR_STRLEN];
4876 char buf2[INET6_ADDRSTRLEN];
4877 char buf3[PREFIX_STRLEN];
4878 zebra_neigh_t *nh = NULL;
4879
4880 /* Create the next hop entry, or update its mac, if necessary. */
4881 nh = zl3vni_nh_lookup(zl3vni, vtep_ip);
4882 if (!nh) {
4883 nh = zl3vni_nh_add(zl3vni, vtep_ip, rmac);
4884 if (!nh) {
4885 zlog_debug(
4886 "Failed to add NH %s as Neigh (RMAC %s L3-VNI %u prefix %s)",
4887 ipaddr2str(vtep_ip, buf1, sizeof(buf2)),
4888 prefix_mac2str(rmac, buf, sizeof(buf)),
4889 zl3vni->vni,
4890 prefix2str(host_prefix, buf2, sizeof(buf2)));
4891 return -1;
4892 }
4893
4894 /* install the nh neigh in kernel */
4895 zl3vni_nh_install(zl3vni, nh);
4896 } else if (memcmp(&nh->emac, rmac, ETH_ALEN) != 0) {
4897 if (IS_ZEBRA_DEBUG_VXLAN)
4898 zlog_debug("L3VNI %u RMAC change(%s --> %s) for nexthop %s, prefix %s",
4899 zl3vni->vni,
4900 prefix_mac2str(&nh->emac, buf, sizeof(buf)),
4901 prefix_mac2str(rmac, buf1, sizeof(buf1)),
4902 ipaddr2str(vtep_ip, buf2, sizeof(buf2)),
4903 prefix2str(host_prefix, buf3, sizeof(buf3)));
4904
4905 memcpy(&nh->emac, rmac, ETH_ALEN);
4906 /* install (update) the nh neigh in kernel */
4907 zl3vni_nh_install(zl3vni, nh);
4908 }
4909
4910 rb_find_or_add_host(&nh->host_rb, host_prefix);
4911
4912 return 0;
4913 }
4914
4915 /* handle nh neigh delete */
4916 static void zl3vni_remote_nh_del(zebra_l3vni_t *zl3vni, zebra_neigh_t *nh,
4917 struct prefix *host_prefix)
4918 {
4919 rb_delete_host(&nh->host_rb, host_prefix);
4920
4921 if (RB_EMPTY(host_rb_tree_entry, &nh->host_rb)) {
4922 /* uninstall from kernel */
4923 zl3vni_nh_uninstall(zl3vni, nh);
4924
4925 /* delete the nh entry */
4926 zl3vni_nh_del(zl3vni, nh);
4927 }
4928 }
4929
4930 /* handle neigh update from kernel - the only thing of interest is to
4931 * readd stale entries.
4932 */
4933 static int zl3vni_local_nh_add_update(zebra_l3vni_t *zl3vni, struct ipaddr *ip,
4934 uint16_t state)
4935 {
4936 #ifdef GNU_LINUX
4937 zebra_neigh_t *n = NULL;
4938
4939 n = zl3vni_nh_lookup(zl3vni, ip);
4940 if (!n)
4941 return 0;
4942
4943 /* all next hop neigh are remote and installed by frr.
4944 * If the kernel has aged this entry, re-install.
4945 */
4946 if (state & NUD_STALE)
4947 zl3vni_nh_install(zl3vni, n);
4948 #endif
4949 return 0;
4950 }
4951
4952 /* handle neigh delete from kernel */
4953 static int zl3vni_local_nh_del(zebra_l3vni_t *zl3vni, struct ipaddr *ip)
4954 {
4955 zebra_neigh_t *n = NULL;
4956
4957 n = zl3vni_nh_lookup(zl3vni, ip);
4958 if (!n)
4959 return 0;
4960
4961 /* all next hop neigh are remote and installed by frr.
4962 * If we get an age out notification for these neigh entries, we have to
4963 * install it back
4964 */
4965 zl3vni_nh_install(zl3vni, n);
4966
4967 return 0;
4968 }
4969
4970 /*
4971 * Hash function for L3 VNI.
4972 */
4973 static unsigned int l3vni_hash_keymake(const void *p)
4974 {
4975 const zebra_l3vni_t *zl3vni = p;
4976
4977 return jhash_1word(zl3vni->vni, 0);
4978 }
4979
4980 /*
4981 * Compare 2 L3 VNI hash entries.
4982 */
4983 static bool l3vni_hash_cmp(const void *p1, const void *p2)
4984 {
4985 const zebra_l3vni_t *zl3vni1 = p1;
4986 const zebra_l3vni_t *zl3vni2 = p2;
4987
4988 return (zl3vni1->vni == zl3vni2->vni);
4989 }
4990
4991 /*
4992 * Callback to allocate L3 VNI hash entry.
4993 */
4994 static void *zl3vni_alloc(void *p)
4995 {
4996 zebra_l3vni_t *zl3vni = NULL;
4997 const zebra_l3vni_t *tmp_l3vni = p;
4998
4999 zl3vni = XCALLOC(MTYPE_ZL3VNI, sizeof(zebra_l3vni_t));
5000 zl3vni->vni = tmp_l3vni->vni;
5001 return ((void *)zl3vni);
5002 }
5003
5004 /*
5005 * Look up L3 VNI hash entry.
5006 */
5007 static zebra_l3vni_t *zl3vni_lookup(vni_t vni)
5008 {
5009 zebra_l3vni_t tmp_l3vni;
5010 zebra_l3vni_t *zl3vni = NULL;
5011
5012 memset(&tmp_l3vni, 0, sizeof(zebra_l3vni_t));
5013 tmp_l3vni.vni = vni;
5014 zl3vni = hash_lookup(zrouter.l3vni_table, &tmp_l3vni);
5015
5016 return zl3vni;
5017 }
5018
5019 /*
5020 * Add L3 VNI hash entry.
5021 */
5022 static zebra_l3vni_t *zl3vni_add(vni_t vni, vrf_id_t vrf_id)
5023 {
5024 zebra_l3vni_t tmp_zl3vni;
5025 zebra_l3vni_t *zl3vni = NULL;
5026
5027 memset(&tmp_zl3vni, 0, sizeof(zebra_l3vni_t));
5028 tmp_zl3vni.vni = vni;
5029
5030 zl3vni = hash_get(zrouter.l3vni_table, &tmp_zl3vni, zl3vni_alloc);
5031 assert(zl3vni);
5032
5033 zl3vni->vrf_id = vrf_id;
5034 zl3vni->svi_if = NULL;
5035 zl3vni->vxlan_if = NULL;
5036 zl3vni->l2vnis = list_new();
5037 zl3vni->l2vnis->cmp = vni_list_cmp;
5038
5039 /* Create hash table for remote RMAC */
5040 zl3vni->rmac_table = hash_create(mac_hash_keymake, mac_cmp,
5041 "Zebra L3-VNI RMAC-Table");
5042
5043 /* Create hash table for neighbors */
5044 zl3vni->nh_table = hash_create(neigh_hash_keymake, neigh_cmp,
5045 "Zebra L3-VNI next-hop table");
5046
5047 return zl3vni;
5048 }
5049
5050 /*
5051 * Delete L3 VNI hash entry.
5052 */
5053 static int zl3vni_del(zebra_l3vni_t *zl3vni)
5054 {
5055 zebra_l3vni_t *tmp_zl3vni;
5056
5057 /* free the list of l2vnis */
5058 list_delete(&zl3vni->l2vnis);
5059 zl3vni->l2vnis = NULL;
5060
5061 /* Free the rmac table */
5062 hash_free(zl3vni->rmac_table);
5063 zl3vni->rmac_table = NULL;
5064
5065 /* Free the nh table */
5066 hash_free(zl3vni->nh_table);
5067 zl3vni->nh_table = NULL;
5068
5069 /* Free the VNI hash entry and allocated memory. */
5070 tmp_zl3vni = hash_release(zrouter.l3vni_table, zl3vni);
5071 XFREE(MTYPE_ZL3VNI, tmp_zl3vni);
5072
5073 return 0;
5074 }
5075
5076 struct interface *zl3vni_map_to_vxlan_if(zebra_l3vni_t *zl3vni)
5077 {
5078 struct zebra_ns *zns = NULL;
5079 struct route_node *rn = NULL;
5080 struct interface *ifp = NULL;
5081
5082 /* loop through all vxlan-interface */
5083 zns = zebra_ns_lookup(NS_DEFAULT);
5084 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
5085
5086 struct zebra_if *zif = NULL;
5087 struct zebra_l2info_vxlan *vxl = NULL;
5088
5089 ifp = (struct interface *)rn->info;
5090 if (!ifp)
5091 continue;
5092
5093 zif = ifp->info;
5094 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
5095 continue;
5096
5097 vxl = &zif->l2info.vxl;
5098 if (vxl->vni == zl3vni->vni) {
5099 zl3vni->local_vtep_ip = vxl->vtep_ip;
5100 return ifp;
5101 }
5102 }
5103
5104 return NULL;
5105 }
5106
5107 struct interface *zl3vni_map_to_svi_if(zebra_l3vni_t *zl3vni)
5108 {
5109 struct zebra_if *zif = NULL; /* zebra_if for vxlan_if */
5110 struct zebra_l2info_vxlan *vxl = NULL; /* l2 info for vxlan_if */
5111
5112 if (!zl3vni)
5113 return NULL;
5114
5115 if (!zl3vni->vxlan_if)
5116 return NULL;
5117
5118 zif = zl3vni->vxlan_if->info;
5119 if (!zif)
5120 return NULL;
5121
5122 vxl = &zif->l2info.vxl;
5123
5124 return zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
5125 }
5126
5127 struct interface *zl3vni_map_to_mac_vlan_if(zebra_l3vni_t *zl3vni)
5128 {
5129 struct zebra_if *zif = NULL; /* zebra_if for vxlan_if */
5130
5131 if (!zl3vni)
5132 return NULL;
5133
5134 if (!zl3vni->vxlan_if)
5135 return NULL;
5136
5137 zif = zl3vni->vxlan_if->info;
5138 if (!zif)
5139 return NULL;
5140
5141 return zvni_map_to_macvlan(zif->brslave_info.br_if, zl3vni->svi_if);
5142 }
5143
5144
5145 zebra_l3vni_t *zl3vni_from_vrf(vrf_id_t vrf_id)
5146 {
5147 struct zebra_vrf *zvrf = NULL;
5148
5149 zvrf = zebra_vrf_lookup_by_id(vrf_id);
5150 if (!zvrf)
5151 return NULL;
5152
5153 return zl3vni_lookup(zvrf->l3vni);
5154 }
5155
5156 /*
5157 * Map SVI and associated bridge to a VNI. This is invoked upon getting
5158 * neighbor notifications, to see if they are of interest.
5159 */
5160 static zebra_l3vni_t *zl3vni_from_svi(struct interface *ifp,
5161 struct interface *br_if)
5162 {
5163 int found = 0;
5164 vlanid_t vid = 0;
5165 uint8_t bridge_vlan_aware = 0;
5166 zebra_l3vni_t *zl3vni = NULL;
5167 struct zebra_ns *zns = NULL;
5168 struct route_node *rn = NULL;
5169 struct zebra_if *zif = NULL;
5170 struct interface *tmp_if = NULL;
5171 struct zebra_l2info_bridge *br = NULL;
5172 struct zebra_l2info_vxlan *vxl = NULL;
5173
5174 if (!br_if)
5175 return NULL;
5176
5177 /* Make sure the linked interface is a bridge. */
5178 if (!IS_ZEBRA_IF_BRIDGE(br_if))
5179 return NULL;
5180
5181 /* Determine if bridge is VLAN-aware or not */
5182 zif = br_if->info;
5183 assert(zif);
5184 br = &zif->l2info.br;
5185 bridge_vlan_aware = br->vlan_aware;
5186 if (bridge_vlan_aware) {
5187 struct zebra_l2info_vlan *vl;
5188
5189 if (!IS_ZEBRA_IF_VLAN(ifp))
5190 return NULL;
5191
5192 zif = ifp->info;
5193 assert(zif);
5194 vl = &zif->l2info.vl;
5195 vid = vl->vid;
5196 }
5197
5198 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
5199 /* TODO: Optimize with a hash. */
5200 zns = zebra_ns_lookup(NS_DEFAULT);
5201 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
5202 tmp_if = (struct interface *)rn->info;
5203 if (!tmp_if)
5204 continue;
5205 zif = tmp_if->info;
5206 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
5207 continue;
5208 if (!if_is_operative(tmp_if))
5209 continue;
5210 vxl = &zif->l2info.vxl;
5211
5212 if (zif->brslave_info.br_if != br_if)
5213 continue;
5214
5215 if (!bridge_vlan_aware || vxl->access_vlan == vid) {
5216 found = 1;
5217 break;
5218 }
5219 }
5220
5221 if (!found)
5222 return NULL;
5223
5224 zl3vni = zl3vni_lookup(vxl->vni);
5225 return zl3vni;
5226 }
5227
5228 static inline void zl3vni_get_vrr_rmac(zebra_l3vni_t *zl3vni,
5229 struct ethaddr *rmac)
5230 {
5231 if (!zl3vni)
5232 return;
5233
5234 if (!is_l3vni_oper_up(zl3vni))
5235 return;
5236
5237 if (zl3vni->mac_vlan_if && if_is_operative(zl3vni->mac_vlan_if))
5238 memcpy(rmac->octet, zl3vni->mac_vlan_if->hw_addr, ETH_ALEN);
5239 }
5240
5241 /*
5242 * Inform BGP about l3-vni.
5243 */
5244 static int zl3vni_send_add_to_client(zebra_l3vni_t *zl3vni)
5245 {
5246 struct stream *s = NULL;
5247 struct zserv *client = NULL;
5248 struct ethaddr svi_rmac, vrr_rmac = {.octet = {0} };
5249 struct zebra_vrf *zvrf;
5250 char buf[ETHER_ADDR_STRLEN];
5251 char buf1[ETHER_ADDR_STRLEN];
5252 bool is_anycast_mac = true;
5253
5254 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
5255 /* BGP may not be running. */
5256 if (!client)
5257 return 0;
5258
5259 zvrf = zebra_vrf_lookup_by_id(zl3vni->vrf_id);
5260 assert(zvrf);
5261
5262 /* get the svi and vrr rmac values */
5263 memset(&svi_rmac, 0, sizeof(struct ethaddr));
5264 zl3vni_get_svi_rmac(zl3vni, &svi_rmac);
5265 zl3vni_get_vrr_rmac(zl3vni, &vrr_rmac);
5266
5267 /* In absence of vrr mac use svi mac as anycast MAC value */
5268 if (is_zero_mac(&vrr_rmac)) {
5269 memcpy(&vrr_rmac, &svi_rmac, ETH_ALEN);
5270 is_anycast_mac = false;
5271 }
5272
5273 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
5274
5275 /* The message is used for both vni add and/or update like
5276 * vrr mac is added for l3vni SVI.
5277 */
5278 zclient_create_header(s, ZEBRA_L3VNI_ADD, zl3vni_vrf_id(zl3vni));
5279 stream_putl(s, zl3vni->vni);
5280 stream_put(s, &svi_rmac, sizeof(struct ethaddr));
5281 stream_put_in_addr(s, &zl3vni->local_vtep_ip);
5282 stream_put(s, &zl3vni->filter, sizeof(int));
5283 stream_putl(s, zl3vni->svi_if->ifindex);
5284 stream_put(s, &vrr_rmac, sizeof(struct ethaddr));
5285 stream_putl(s, is_anycast_mac);
5286
5287 /* Write packet size. */
5288 stream_putw_at(s, 0, stream_get_endp(s));
5289
5290 if (IS_ZEBRA_DEBUG_VXLAN)
5291 zlog_debug(
5292 "Send L3_VNI_ADD %u VRF %s RMAC %s VRR %s local-ip %s filter %s to %s",
5293 zl3vni->vni, vrf_id_to_name(zl3vni_vrf_id(zl3vni)),
5294 prefix_mac2str(&svi_rmac, buf, sizeof(buf)),
5295 prefix_mac2str(&vrr_rmac, buf1, sizeof(buf1)),
5296 inet_ntoa(zl3vni->local_vtep_ip),
5297 CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)
5298 ? "prefix-routes-only"
5299 : "none",
5300 zebra_route_string(client->proto));
5301
5302 client->l3vniadd_cnt++;
5303 return zserv_send_message(client, s);
5304 }
5305
5306 /*
5307 * Inform BGP about local l3-VNI deletion.
5308 */
5309 static int zl3vni_send_del_to_client(zebra_l3vni_t *zl3vni)
5310 {
5311 struct stream *s = NULL;
5312 struct zserv *client = NULL;
5313
5314 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
5315 /* BGP may not be running. */
5316 if (!client)
5317 return 0;
5318
5319 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
5320
5321 zclient_create_header(s, ZEBRA_L3VNI_DEL, zl3vni_vrf_id(zl3vni));
5322 stream_putl(s, zl3vni->vni);
5323
5324 /* Write packet size. */
5325 stream_putw_at(s, 0, stream_get_endp(s));
5326
5327 if (IS_ZEBRA_DEBUG_VXLAN)
5328 zlog_debug("Send L3_VNI_DEL %u VRF %s to %s", zl3vni->vni,
5329 vrf_id_to_name(zl3vni_vrf_id(zl3vni)),
5330 zebra_route_string(client->proto));
5331
5332 client->l3vnidel_cnt++;
5333 return zserv_send_message(client, s);
5334 }
5335
5336 static void zebra_vxlan_process_l3vni_oper_up(zebra_l3vni_t *zl3vni)
5337 {
5338 if (!zl3vni)
5339 return;
5340
5341 /* send l3vni add to BGP */
5342 zl3vni_send_add_to_client(zl3vni);
5343 }
5344
5345 static void zebra_vxlan_process_l3vni_oper_down(zebra_l3vni_t *zl3vni)
5346 {
5347 if (!zl3vni)
5348 return;
5349
5350 /* send l3-vni del to BGP*/
5351 zl3vni_send_del_to_client(zl3vni);
5352 }
5353
5354 static void zvni_add_to_l3vni_list(struct hash_bucket *bucket, void *ctxt)
5355 {
5356 zebra_vni_t *zvni = (zebra_vni_t *)bucket->data;
5357 zebra_l3vni_t *zl3vni = (zebra_l3vni_t *)ctxt;
5358
5359 if (zvni->vrf_id == zl3vni_vrf_id(zl3vni))
5360 listnode_add_sort(zl3vni->l2vnis, zvni);
5361 }
5362
5363 /*
5364 * handle transition of vni from l2 to l3 and vice versa
5365 */
5366 static int zebra_vxlan_handle_vni_transition(struct zebra_vrf *zvrf, vni_t vni,
5367 int add)
5368 {
5369 zebra_vni_t *zvni = NULL;
5370
5371 /* There is a possibility that VNI notification was already received
5372 * from kernel and we programmed it as L2-VNI
5373 * In such a case we need to delete this L2-VNI first, so
5374 * that it can be reprogrammed as L3-VNI in the system. It is also
5375 * possible that the vrf-vni mapping is removed from FRR while the vxlan
5376 * interface is still present in kernel. In this case to keep it
5377 * symmetric, we will delete the l3-vni and reprogram it as l2-vni
5378 */
5379 if (add) {
5380 /* Locate hash entry */
5381 zvni = zvni_lookup(vni);
5382 if (!zvni)
5383 return 0;
5384
5385 if (IS_ZEBRA_DEBUG_VXLAN)
5386 zlog_debug("Del L2-VNI %u - transition to L3-VNI", vni);
5387
5388 /* Delete VNI from BGP. */
5389 zvni_send_del_to_client(zvni->vni);
5390
5391 /* Free up all neighbors and MAC, if any. */
5392 zvni_neigh_del_all(zvni, 0, 0, DEL_ALL_NEIGH);
5393 zvni_mac_del_all(zvni, 0, 0, DEL_ALL_MAC);
5394
5395 /* Free up all remote VTEPs, if any. */
5396 zvni_vtep_del_all(zvni, 0);
5397
5398 /* Delete the hash entry. */
5399 if (zvni_del(zvni)) {
5400 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
5401 "Failed to del VNI hash %p, VNI %u", zvni,
5402 zvni->vni);
5403 return -1;
5404 }
5405 } else {
5406 /* TODO_MITESH: This needs to be thought through. We don't have
5407 * enough information at this point to reprogram the vni as
5408 * l2-vni. One way is to store the required info in l3-vni and
5409 * used it solely for this purpose
5410 */
5411 }
5412
5413 return 0;
5414 }
5415
5416 /* delete and uninstall rmac hash entry */
5417 static void zl3vni_del_rmac_hash_entry(struct hash_bucket *bucket, void *ctx)
5418 {
5419 zebra_mac_t *zrmac = NULL;
5420 zebra_l3vni_t *zl3vni = NULL;
5421
5422 zrmac = (zebra_mac_t *)bucket->data;
5423 zl3vni = (zebra_l3vni_t *)ctx;
5424 zl3vni_rmac_uninstall(zl3vni, zrmac);
5425
5426 /* Send RMAC for FPM processing */
5427 hook_call(zebra_rmac_update, zrmac, zl3vni, true, "RMAC deleted");
5428
5429 zl3vni_rmac_del(zl3vni, zrmac);
5430 }
5431
5432 /* delete and uninstall nh hash entry */
5433 static void zl3vni_del_nh_hash_entry(struct hash_bucket *bucket, void *ctx)
5434 {
5435 zebra_neigh_t *n = NULL;
5436 zebra_l3vni_t *zl3vni = NULL;
5437
5438 n = (zebra_neigh_t *)bucket->data;
5439 zl3vni = (zebra_l3vni_t *)ctx;
5440 zl3vni_nh_uninstall(zl3vni, n);
5441 zl3vni_nh_del(zl3vni, n);
5442 }
5443
5444 static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
5445 uint16_t cmd)
5446 {
5447 struct zserv *client = NULL;
5448 struct stream *s = NULL;
5449 char buf[PREFIX_STRLEN];
5450
5451 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
5452 /* BGP may not be running. */
5453 if (!client)
5454 return 0;
5455
5456 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
5457
5458 zclient_create_header(s, cmd, vrf_id);
5459 stream_put(s, p, sizeof(struct prefix));
5460
5461 /* Write packet size. */
5462 stream_putw_at(s, 0, stream_get_endp(s));
5463
5464 if (IS_ZEBRA_DEBUG_VXLAN)
5465 zlog_debug("Send ip prefix %s %s on vrf %s",
5466 prefix2str(p, buf, sizeof(buf)),
5467 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
5468 vrf_id_to_name(vrf_id));
5469
5470 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD)
5471 client->prefixadd_cnt++;
5472 else
5473 client->prefixdel_cnt++;
5474
5475 return zserv_send_message(client, s);
5476 }
5477
5478 /* re-add remote rmac if needed */
5479 static int zebra_vxlan_readd_remote_rmac(zebra_l3vni_t *zl3vni,
5480 struct ethaddr *rmac)
5481 {
5482 char buf[ETHER_ADDR_STRLEN];
5483 zebra_mac_t *zrmac = NULL;
5484
5485 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
5486 if (!zrmac)
5487 return 0;
5488
5489 if (IS_ZEBRA_DEBUG_VXLAN)
5490 zlog_debug("Del remote RMAC %s L3VNI %u - readd",
5491 prefix_mac2str(rmac, buf, sizeof(buf)), zl3vni->vni);
5492
5493 zl3vni_rmac_install(zl3vni, zrmac);
5494 return 0;
5495 }
5496
5497 /* Process a remote MACIP add from BGP. */
5498 static void process_remote_macip_add(vni_t vni,
5499 struct ethaddr *macaddr,
5500 uint16_t ipa_len,
5501 struct ipaddr *ipaddr,
5502 uint8_t flags,
5503 uint32_t seq,
5504 struct in_addr vtep_ip)
5505 {
5506 zebra_vni_t *zvni;
5507 zebra_vtep_t *zvtep;
5508 zebra_mac_t *mac = NULL, *old_mac = NULL;
5509 zebra_neigh_t *n = NULL;
5510 int update_mac = 0, update_neigh = 0;
5511 char buf[ETHER_ADDR_STRLEN];
5512 char buf1[INET6_ADDRSTRLEN];
5513 struct interface *ifp = NULL;
5514 struct zebra_if *zif = NULL;
5515 struct zebra_vrf *zvrf;
5516 uint32_t tmp_seq;
5517 bool sticky;
5518 bool remote_gw;
5519 bool is_router;
5520 bool do_dad = false;
5521 bool is_dup_detect = false;
5522
5523 /* Locate VNI hash entry - expected to exist. */
5524 zvni = zvni_lookup(vni);
5525 if (!zvni) {
5526 zlog_warn("Unknown VNI %u upon remote MACIP ADD", vni);
5527 return;
5528 }
5529
5530 ifp = zvni->vxlan_if;
5531 if (ifp)
5532 zif = ifp->info;
5533 if (!ifp ||
5534 !if_is_operative(ifp) ||
5535 !zif ||
5536 !zif->brslave_info.br_if) {
5537 zlog_warn("Ignoring remote MACIP ADD VNI %u, invalid interface state or info",
5538 vni);
5539 return;
5540 }
5541
5542 /* The remote VTEP specified should normally exist, but it is
5543 * possible that when peering comes up, peer may advertise MACIP
5544 * routes before advertising type-3 routes.
5545 */
5546 zvtep = zvni_vtep_find(zvni, &vtep_ip);
5547 if (!zvtep) {
5548 zvtep = zvni_vtep_add(zvni, &vtep_ip, VXLAN_FLOOD_DISABLED);
5549 if (!zvtep) {
5550 flog_err(
5551 EC_ZEBRA_VTEP_ADD_FAILED,
5552 "Failed to add remote VTEP, VNI %u zvni %p upon remote MACIP ADD",
5553 vni, zvni);
5554 return;
5555 }
5556
5557 zvni_vtep_install(zvni, zvtep);
5558 }
5559
5560 sticky = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
5561 remote_gw = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
5562 is_router = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
5563
5564 mac = zvni_mac_lookup(zvni, macaddr);
5565
5566 /* Ignore if the mac is already present as a gateway mac */
5567 if (mac &&
5568 CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW) &&
5569 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) {
5570 if (IS_ZEBRA_DEBUG_VXLAN)
5571 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as MAC is already configured as gateway MAC",
5572 vni,
5573 prefix_mac2str(macaddr, buf, sizeof(buf)),
5574 ipa_len ? " IP " : "",
5575 ipa_len ?
5576 ipaddr2str(ipaddr, buf1, sizeof(buf1)) : "");
5577 return;
5578 }
5579
5580 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
5581 if (!zvrf)
5582 return;
5583
5584 /* check if the remote MAC is unknown or has a change.
5585 * If so, that needs to be updated first. Note that client could
5586 * install MAC and MACIP separately or just install the latter.
5587 */
5588 if (!mac
5589 || !CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)
5590 || sticky != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)
5591 || remote_gw != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW)
5592 || !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &vtep_ip)
5593 || seq != mac->rem_seq)
5594 update_mac = 1;
5595
5596 if (update_mac) {
5597 if (!mac) {
5598 mac = zvni_mac_add(zvni, macaddr);
5599 if (!mac) {
5600 zlog_warn(
5601 "Failed to add MAC %s VNI %u Remote VTEP %s",
5602 prefix_mac2str(macaddr, buf,
5603 sizeof(buf)),
5604 vni, inet_ntoa(vtep_ip));
5605 return;
5606 }
5607
5608 /* Is this MAC created for a MACIP? */
5609 if (ipa_len)
5610 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5611 } else {
5612 /* When host moves but changes its (MAC,IP)
5613 * binding, BGP may install a MACIP entry that
5614 * corresponds to "older" location of the host
5615 * in transient situations (because {IP1,M1}
5616 * is a different route from {IP1,M2}). Check
5617 * the sequence number and ignore this update
5618 * if appropriate.
5619 */
5620 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
5621 tmp_seq = mac->loc_seq;
5622 else
5623 tmp_seq = mac->rem_seq;
5624
5625 if (seq < tmp_seq) {
5626 if (IS_ZEBRA_DEBUG_VXLAN)
5627 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as existing MAC has higher seq %u flags 0x%x",
5628 vni,
5629 prefix_mac2str(macaddr,
5630 buf, sizeof(buf)),
5631 ipa_len ? " IP " : "",
5632 ipa_len ?
5633 ipaddr2str(ipaddr,
5634 buf1, sizeof(buf1)) : "",
5635 tmp_seq, mac->flags);
5636 return;
5637 }
5638 }
5639
5640 /* Check MAC's curent state is local (this is the case
5641 * where MAC has moved from L->R) and check previous
5642 * detection started via local learning.
5643 * RFC-7432: A PE/VTEP that detects a MAC mobility
5644 * event via local learning starts an M-second timer.
5645 *
5646 * VTEP-IP or seq. change alone is not considered
5647 * for dup. detection.
5648 *
5649 * MAC is already marked duplicate set dad, then
5650 * is_dup_detect will be set to not install the entry.
5651 */
5652 if ((!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) &&
5653 mac->dad_count) ||
5654 CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
5655 do_dad = true;
5656
5657 /* Remove local MAC from BGP. */
5658 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
5659 zvni_mac_send_del_to_client(zvni->vni, macaddr);
5660
5661 /* Set "auto" and "remote" forwarding info. */
5662 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
5663 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
5664 SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
5665 mac->fwd_info.r_vtep_ip = vtep_ip;
5666
5667 if (sticky)
5668 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
5669 else
5670 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
5671
5672 if (remote_gw)
5673 SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW);
5674 else
5675 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW);
5676
5677 zebra_vxlan_dup_addr_detect_for_mac(zvrf, mac,
5678 mac->fwd_info.r_vtep_ip,
5679 do_dad, &is_dup_detect,
5680 false);
5681
5682 if (!is_dup_detect) {
5683 zvni_process_neigh_on_remote_mac_add(zvni, mac);
5684 /* Install the entry. */
5685 zvni_mac_install(zvni, mac);
5686 }
5687 }
5688
5689 /* Update seq number. */
5690 mac->rem_seq = seq;
5691
5692 /* If there is no IP, return after clearing AUTO flag of MAC. */
5693 if (!ipa_len) {
5694 UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5695 return;
5696 }
5697
5698 /* Reset flag */
5699 do_dad = false;
5700
5701 /* Check if the remote neighbor itself is unknown or has a
5702 * change. If so, create or update and then install the entry.
5703 */
5704 n = zvni_neigh_lookup(zvni, ipaddr);
5705 if (!n
5706 || !CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
5707 || is_router != !!CHECK_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG)
5708 || (memcmp(&n->emac, macaddr, sizeof(*macaddr)) != 0)
5709 || !IPV4_ADDR_SAME(&n->r_vtep_ip, &vtep_ip)
5710 || seq != n->rem_seq)
5711 update_neigh = 1;
5712
5713 if (update_neigh) {
5714 if (!n) {
5715 n = zvni_neigh_add(zvni, ipaddr, macaddr);
5716 if (!n) {
5717 zlog_warn(
5718 "Failed to add Neigh %s MAC %s VNI %u Remote VTEP %s",
5719 ipaddr2str(ipaddr, buf1,
5720 sizeof(buf1)),
5721 prefix_mac2str(macaddr, buf,
5722 sizeof(buf)),
5723 vni, inet_ntoa(vtep_ip));
5724 return;
5725 }
5726
5727 } else {
5728 const char *n_type;
5729
5730 /* When host moves but changes its (MAC,IP)
5731 * binding, BGP may install a MACIP entry that
5732 * corresponds to "older" location of the host
5733 * in transient situations (because {IP1,M1}
5734 * is a different route from {IP1,M2}). Check
5735 * the sequence number and ignore this update
5736 * if appropriate.
5737 */
5738 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL)) {
5739 tmp_seq = n->loc_seq;
5740 n_type = "local";
5741 } else {
5742 tmp_seq = n->rem_seq;
5743 n_type = "remote";
5744 }
5745 if (seq < tmp_seq) {
5746 if (IS_ZEBRA_DEBUG_VXLAN)
5747 zlog_debug("Ignore remote MACIP ADD VNI %u MAC %s%s%s as existing %s Neigh has higher seq %u",
5748 vni,
5749 prefix_mac2str(macaddr,
5750 buf, sizeof(buf)),
5751 " IP ",
5752 ipaddr2str(ipaddr, buf1, sizeof(buf1)),
5753 n_type,
5754 tmp_seq);
5755 return;
5756 }
5757 if (memcmp(&n->emac, macaddr, sizeof(*macaddr)) != 0) {
5758 /* MAC change, send a delete for old
5759 * neigh if learnt locally.
5760 */
5761 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL) &&
5762 IS_ZEBRA_NEIGH_ACTIVE(n))
5763 zvni_neigh_send_del_to_client(
5764 zvni->vni, &n->ip,
5765 &n->emac, 0, n->state);
5766
5767 /* update neigh list for macs */
5768 old_mac = zvni_mac_lookup(zvni, &n->emac);
5769 if (old_mac) {
5770 listnode_delete(old_mac->neigh_list, n);
5771 zvni_deref_ip2mac(zvni, old_mac);
5772 }
5773 listnode_add_sort(mac->neigh_list, n);
5774 memcpy(&n->emac, macaddr, ETH_ALEN);
5775
5776 /* Check Neigh's curent state is local
5777 * (this is the case where neigh/host has moved
5778 * from L->R) and check previous detction
5779 * started via local learning.
5780 *
5781 * RFC-7432: A PE/VTEP that detects a MAC
5782 * mobilit event via local learning starts
5783 * an M-second timer.
5784 * VTEP-IP or seq. change along is not
5785 * considered for dup. detection.
5786 *
5787 * Mobilty event scenario-B IP-MAC binding
5788 * changed.
5789 */
5790 if ((!CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE))
5791 && n->dad_count)
5792 do_dad = true;
5793
5794 }
5795 }
5796
5797 /* Set "remote" forwarding info. */
5798 UNSET_FLAG(n->flags, ZEBRA_NEIGH_LOCAL);
5799 n->r_vtep_ip = vtep_ip;
5800 SET_FLAG(n->flags, ZEBRA_NEIGH_REMOTE);
5801
5802 /* Set router flag (R-bit) to this Neighbor entry */
5803 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG))
5804 SET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
5805 else
5806 UNSET_FLAG(n->flags, ZEBRA_NEIGH_ROUTER_FLAG);
5807
5808 /* Check old or new MAC detected as duplicate,
5809 * inherit duplicate flag to this neigh.
5810 */
5811 if (zebra_vxlan_ip_inherit_dad_from_mac(zvrf, old_mac,
5812 mac, n)) {
5813 flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED,
5814 "VNI %u: MAC %s IP %s detected as duplicate during remote update, inherit duplicate from MAC",
5815 zvni->vni,
5816 prefix_mac2str(&mac->macaddr, buf, sizeof(buf)),
5817 ipaddr2str(&n->ip, buf1, sizeof(buf1)));
5818 }
5819
5820 /* Check duplicate address detection for IP */
5821 zebra_vxlan_dup_addr_detect_for_neigh(zvrf, n,
5822 n->r_vtep_ip,
5823 do_dad,
5824 &is_dup_detect,
5825 false);
5826 /* Install the entry. */
5827 if (!is_dup_detect)
5828 zvni_neigh_install(zvni, n);
5829 }
5830
5831 zvni_probe_neigh_on_mac_add(zvni, mac);
5832
5833 /* Update seq number. */
5834 n->rem_seq = seq;
5835 }
5836
5837 /* Process a remote MACIP delete from BGP. */
5838 static void process_remote_macip_del(vni_t vni,
5839 struct ethaddr *macaddr,
5840 uint16_t ipa_len,
5841 struct ipaddr *ipaddr,
5842 struct in_addr vtep_ip)
5843 {
5844 zebra_vni_t *zvni;
5845 zebra_mac_t *mac = NULL;
5846 zebra_neigh_t *n = NULL;
5847 struct interface *ifp = NULL;
5848 struct zebra_if *zif = NULL;
5849 struct zebra_ns *zns;
5850 struct zebra_l2info_vxlan *vxl;
5851 struct zebra_vrf *zvrf;
5852 char buf[ETHER_ADDR_STRLEN];
5853 char buf1[INET6_ADDRSTRLEN];
5854
5855 /* Locate VNI hash entry - expected to exist. */
5856 zvni = zvni_lookup(vni);
5857 if (!zvni) {
5858 if (IS_ZEBRA_DEBUG_VXLAN)
5859 zlog_debug("Unknown VNI %u upon remote MACIP DEL", vni);
5860 return;
5861 }
5862
5863 ifp = zvni->vxlan_if;
5864 if (ifp)
5865 zif = ifp->info;
5866 if (!ifp ||
5867 !if_is_operative(ifp) ||
5868 !zif ||
5869 !zif->brslave_info.br_if) {
5870 if (IS_ZEBRA_DEBUG_VXLAN)
5871 zlog_debug("Ignoring remote MACIP DEL VNI %u, invalid interface state or info",
5872 vni);
5873 return;
5874 }
5875 zns = zebra_ns_lookup(NS_DEFAULT);
5876 vxl = &zif->l2info.vxl;
5877
5878 /* The remote VTEP specified is normally expected to exist, but
5879 * it is possible that the peer may delete the VTEP before deleting
5880 * any MACs referring to the VTEP, in which case the handler (see
5881 * remote_vtep_del) would have already deleted the MACs.
5882 */
5883 if (!zvni_vtep_find(zvni, &vtep_ip))
5884 return;
5885
5886 mac = zvni_mac_lookup(zvni, macaddr);
5887 if (ipa_len)
5888 n = zvni_neigh_lookup(zvni, ipaddr);
5889
5890 if (n && !mac) {
5891 zlog_warn("Failed to locate MAC %s for neigh %s VNI %u upon remote MACIP DEL",
5892 prefix_mac2str(macaddr, buf, sizeof(buf)),
5893 ipaddr2str(ipaddr, buf1, sizeof(buf1)), vni);
5894 return;
5895 }
5896
5897 /* If the remote mac or neighbor doesn't exist there is nothing
5898 * more to do. Otherwise, uninstall the entry and then remove it.
5899 */
5900 if (!mac && !n)
5901 return;
5902
5903 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
5904
5905 /* Ignore the delete if this mac is a gateway mac-ip */
5906 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
5907 && CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW)) {
5908 zlog_warn(
5909 "Ignore remote MACIP DEL VNI %u MAC %s%s%s as MAC is already configured as gateway MAC",
5910 vni,
5911 prefix_mac2str(macaddr, buf, sizeof(buf)),
5912 ipa_len ? " IP " : "",
5913 ipa_len ?
5914 ipaddr2str(ipaddr, buf1, sizeof(buf1)) : "");
5915 return;
5916 }
5917
5918 /* Uninstall remote neighbor or MAC. */
5919 if (n) {
5920 if (zvrf->dad_freeze &&
5921 CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE) &&
5922 CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE) &&
5923 (memcmp(n->emac.octet, macaddr->octet, ETH_ALEN) == 0)) {
5924 struct interface *vlan_if;
5925
5926 vlan_if = zvni_map_to_svi(vxl->access_vlan,
5927 zif->brslave_info.br_if);
5928 if (IS_ZEBRA_DEBUG_VXLAN)
5929 zlog_debug(
5930 "%s: IP %s (flags 0x%x intf %s) is remote and duplicate, read kernel for local entry",
5931 __PRETTY_FUNCTION__,
5932 ipaddr2str(ipaddr, buf1, sizeof(buf1)),
5933 n->flags,
5934 vlan_if ? vlan_if->name : "Unknown");
5935 if (vlan_if)
5936 neigh_read_specific_ip(ipaddr, vlan_if);
5937 }
5938
5939 /* When the MAC changes for an IP, it is possible the
5940 * client may update the new MAC before trying to delete the
5941 * "old" neighbor (as these are two different MACIP routes).
5942 * Do the delete only if the MAC matches.
5943 */
5944 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)
5945 && (memcmp(n->emac.octet, macaddr->octet, ETH_ALEN) == 0)) {
5946 zvni_neigh_uninstall(zvni, n);
5947 zvni_neigh_del(zvni, n);
5948 zvni_deref_ip2mac(zvni, mac);
5949 }
5950 } else {
5951 /* DAD: when MAC is freeze state as remote learn event,
5952 * remote mac-ip delete event is received will result in freeze
5953 * entry removal, first fetch kernel for the same entry present
5954 * as LOCAL and reachable, avoid deleting this entry instead
5955 * use kerenel local entry to update during unfreeze time.
5956 */
5957 if (zvrf->dad_freeze &&
5958 CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE) &&
5959 CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
5960 if (IS_ZEBRA_DEBUG_VXLAN)
5961 zlog_debug("%s: MAC %s (flags 0x%x) is remote and duplicate, read kernel for local entry",
5962 __PRETTY_FUNCTION__,
5963 prefix_mac2str(macaddr, buf,
5964 sizeof(buf)),
5965 mac->flags);
5966 macfdb_read_specific_mac(zns, zif->brslave_info.br_if,
5967 macaddr, vxl->access_vlan);
5968 }
5969
5970 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
5971 zvni_process_neigh_on_remote_mac_del(zvni, mac);
5972 /*
5973 * the remote sequence number in the auto mac entry
5974 * needs to be reset to 0 as the mac entry may have
5975 * been removed on all VTEPs (including
5976 * the originating one)
5977 */
5978 mac->rem_seq = 0;
5979
5980 /* If all remote neighbors referencing a remote MAC
5981 * go away, we need to uninstall the MAC.
5982 */
5983 if (remote_neigh_count(mac) == 0) {
5984 zvni_mac_uninstall(zvni, mac);
5985 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
5986 }
5987 if (list_isempty(mac->neigh_list))
5988 zvni_mac_del(zvni, mac);
5989 else
5990 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
5991 }
5992 }
5993 }
5994
5995
5996 /* Public functions */
5997
5998 int is_l3vni_for_prefix_routes_only(vni_t vni)
5999 {
6000 zebra_l3vni_t *zl3vni = NULL;
6001
6002 zl3vni = zl3vni_lookup(vni);
6003 if (!zl3vni)
6004 return 0;
6005
6006 return CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY) ? 1 : 0;
6007 }
6008
6009 /* handle evpn route in vrf table */
6010 void zebra_vxlan_evpn_vrf_route_add(vrf_id_t vrf_id, struct ethaddr *rmac,
6011 struct ipaddr *vtep_ip,
6012 struct prefix *host_prefix)
6013 {
6014 zebra_l3vni_t *zl3vni = NULL;
6015 struct ipaddr ipv4_vtep;
6016
6017 zl3vni = zl3vni_from_vrf(vrf_id);
6018 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
6019 return;
6020
6021 /*
6022 * add the next hop neighbor -
6023 * neigh to be installed is the ipv6 nexthop neigh
6024 */
6025 zl3vni_remote_nh_add(zl3vni, vtep_ip, rmac, host_prefix);
6026
6027 /*
6028 * if the remote vtep is a ipv4 mapped ipv6 address convert it to ipv4
6029 * address. Rmac is programmed against the ipv4 vtep because we only
6030 * support ipv4 tunnels in the h/w right now
6031 */
6032 memset(&ipv4_vtep, 0, sizeof(struct ipaddr));
6033 ipv4_vtep.ipa_type = IPADDR_V4;
6034 if (vtep_ip->ipa_type == IPADDR_V6)
6035 ipv4_mapped_ipv6_to_ipv4(&vtep_ip->ipaddr_v6,
6036 &(ipv4_vtep.ipaddr_v4));
6037 else
6038 memcpy(&(ipv4_vtep.ipaddr_v4), &vtep_ip->ipaddr_v4,
6039 sizeof(struct in_addr));
6040
6041 /*
6042 * add the rmac - remote rmac to be installed is against the ipv4
6043 * nexthop address
6044 */
6045 zl3vni_remote_rmac_add(zl3vni, rmac, &ipv4_vtep, host_prefix);
6046 }
6047
6048 /* handle evpn vrf route delete */
6049 void zebra_vxlan_evpn_vrf_route_del(vrf_id_t vrf_id,
6050 struct ipaddr *vtep_ip,
6051 struct prefix *host_prefix)
6052 {
6053 zebra_l3vni_t *zl3vni = NULL;
6054 zebra_neigh_t *nh = NULL;
6055 zebra_mac_t *zrmac = NULL;
6056
6057 zl3vni = zl3vni_from_vrf(vrf_id);
6058 if (!zl3vni)
6059 return;
6060
6061 /* find the next hop entry and rmac entry */
6062 nh = zl3vni_nh_lookup(zl3vni, vtep_ip);
6063 if (!nh)
6064 return;
6065 zrmac = zl3vni_rmac_lookup(zl3vni, &nh->emac);
6066
6067 /* delete the next hop entry */
6068 zl3vni_remote_nh_del(zl3vni, nh, host_prefix);
6069
6070 /* delete the rmac entry */
6071 if (zrmac)
6072 zl3vni_remote_rmac_del(zl3vni, zrmac, host_prefix);
6073
6074 }
6075
6076 void zebra_vxlan_print_specific_rmac_l3vni(struct vty *vty, vni_t l3vni,
6077 struct ethaddr *rmac, bool use_json)
6078 {
6079 zebra_l3vni_t *zl3vni = NULL;
6080 zebra_mac_t *zrmac = NULL;
6081 json_object *json = NULL;
6082
6083 if (!is_evpn_enabled()) {
6084 if (use_json)
6085 vty_out(vty, "{}\n");
6086 return;
6087 }
6088
6089 if (use_json)
6090 json = json_object_new_object();
6091
6092 zl3vni = zl3vni_lookup(l3vni);
6093 if (!zl3vni) {
6094 if (use_json)
6095 vty_out(vty, "{}\n");
6096 else
6097 vty_out(vty, "%% L3-VNI %u doesn't exist\n", l3vni);
6098 return;
6099 }
6100
6101 zrmac = zl3vni_rmac_lookup(zl3vni, rmac);
6102 if (!zrmac) {
6103 if (use_json)
6104 vty_out(vty, "{}\n");
6105 else
6106 vty_out(vty,
6107 "%% Requested RMAC doesn't exist in L3-VNI %u",
6108 l3vni);
6109 return;
6110 }
6111
6112 zl3vni_print_rmac(zrmac, vty, json);
6113
6114 if (use_json) {
6115 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6116 json, JSON_C_TO_STRING_PRETTY));
6117 json_object_free(json);
6118 }
6119 }
6120
6121 void zebra_vxlan_print_rmacs_l3vni(struct vty *vty, vni_t l3vni, bool use_json)
6122 {
6123 zebra_l3vni_t *zl3vni;
6124 uint32_t num_rmacs;
6125 struct rmac_walk_ctx wctx;
6126 json_object *json = NULL;
6127
6128 if (!is_evpn_enabled())
6129 return;
6130
6131 zl3vni = zl3vni_lookup(l3vni);
6132 if (!zl3vni) {
6133 if (use_json)
6134 vty_out(vty, "{}\n");
6135 else
6136 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
6137 return;
6138 }
6139 num_rmacs = hashcount(zl3vni->rmac_table);
6140 if (!num_rmacs)
6141 return;
6142
6143 if (use_json)
6144 json = json_object_new_object();
6145
6146 memset(&wctx, 0, sizeof(struct rmac_walk_ctx));
6147 wctx.vty = vty;
6148 wctx.json = json;
6149 if (!use_json) {
6150 vty_out(vty, "Number of Remote RMACs known for this VNI: %u\n",
6151 num_rmacs);
6152 vty_out(vty, "%-17s %-21s\n", "MAC", "Remote VTEP");
6153 } else
6154 json_object_int_add(json, "numRmacs", num_rmacs);
6155
6156 hash_iterate(zl3vni->rmac_table, zl3vni_print_rmac_hash, &wctx);
6157
6158 if (use_json) {
6159 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6160 json, JSON_C_TO_STRING_PRETTY));
6161 json_object_free(json);
6162 }
6163 }
6164
6165 void zebra_vxlan_print_rmacs_all_l3vni(struct vty *vty, bool use_json)
6166 {
6167 json_object *json = NULL;
6168 void *args[2];
6169
6170 if (!is_evpn_enabled()) {
6171 if (use_json)
6172 vty_out(vty, "{}\n");
6173 return;
6174 }
6175
6176 if (use_json)
6177 json = json_object_new_object();
6178
6179 args[0] = vty;
6180 args[1] = json;
6181 hash_iterate(zrouter.l3vni_table,
6182 (void (*)(struct hash_bucket *,
6183 void *))zl3vni_print_rmac_hash_all_vni,
6184 args);
6185
6186 if (use_json) {
6187 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6188 json, JSON_C_TO_STRING_PRETTY));
6189 json_object_free(json);
6190 }
6191 }
6192
6193 void zebra_vxlan_print_specific_nh_l3vni(struct vty *vty, vni_t l3vni,
6194 struct ipaddr *ip, bool use_json)
6195 {
6196 zebra_l3vni_t *zl3vni = NULL;
6197 zebra_neigh_t *n = NULL;
6198 json_object *json = NULL;
6199
6200 if (!is_evpn_enabled()) {
6201 if (use_json)
6202 vty_out(vty, "{}\n");
6203 return;
6204 }
6205
6206 if (use_json)
6207 json = json_object_new_object();
6208
6209 zl3vni = zl3vni_lookup(l3vni);
6210 if (!zl3vni) {
6211 if (use_json)
6212 vty_out(vty, "{}\n");
6213 else
6214 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
6215 return;
6216 }
6217
6218 n = zl3vni_nh_lookup(zl3vni, ip);
6219 if (!n) {
6220 if (use_json)
6221 vty_out(vty, "{}\n");
6222 else
6223 vty_out(vty,
6224 "%% Requested next-hop not present for L3-VNI %u",
6225 l3vni);
6226 return;
6227 }
6228
6229 zl3vni_print_nh(n, vty, json);
6230
6231 if (use_json) {
6232 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6233 json, JSON_C_TO_STRING_PRETTY));
6234 json_object_free(json);
6235 }
6236 }
6237
6238 void zebra_vxlan_print_nh_l3vni(struct vty *vty, vni_t l3vni, bool use_json)
6239 {
6240 uint32_t num_nh;
6241 struct nh_walk_ctx wctx;
6242 json_object *json = NULL;
6243 zebra_l3vni_t *zl3vni = NULL;
6244
6245 if (!is_evpn_enabled())
6246 return;
6247
6248 zl3vni = zl3vni_lookup(l3vni);
6249 if (!zl3vni) {
6250 if (use_json)
6251 vty_out(vty, "{}\n");
6252 else
6253 vty_out(vty, "%% L3-VNI %u does not exist\n", l3vni);
6254 return;
6255 }
6256
6257 num_nh = hashcount(zl3vni->nh_table);
6258 if (!num_nh)
6259 return;
6260
6261 if (use_json)
6262 json = json_object_new_object();
6263
6264 wctx.vty = vty;
6265 wctx.json = json;
6266 if (!use_json) {
6267 vty_out(vty, "Number of NH Neighbors known for this VNI: %u\n",
6268 num_nh);
6269 vty_out(vty, "%-15s %-17s\n", "IP", "RMAC");
6270 } else
6271 json_object_int_add(json, "numNextHops", num_nh);
6272
6273 hash_iterate(zl3vni->nh_table, zl3vni_print_nh_hash, &wctx);
6274
6275 if (use_json) {
6276 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6277 json, JSON_C_TO_STRING_PRETTY));
6278 json_object_free(json);
6279 }
6280 }
6281
6282 void zebra_vxlan_print_nh_all_l3vni(struct vty *vty, bool use_json)
6283 {
6284 json_object *json = NULL;
6285 void *args[2];
6286
6287 if (!is_evpn_enabled()) {
6288 if (use_json)
6289 vty_out(vty, "{}\n");
6290 return;
6291 }
6292
6293 if (use_json)
6294 json = json_object_new_object();
6295
6296 args[0] = vty;
6297 args[1] = json;
6298 hash_iterate(zrouter.l3vni_table,
6299 (void (*)(struct hash_bucket *,
6300 void *))zl3vni_print_nh_hash_all_vni,
6301 args);
6302
6303 if (use_json) {
6304 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6305 json, JSON_C_TO_STRING_PRETTY));
6306 json_object_free(json);
6307 }
6308 }
6309
6310 /*
6311 * Display L3 VNI information (VTY command handler).
6312 */
6313 void zebra_vxlan_print_l3vni(struct vty *vty, vni_t vni, bool use_json)
6314 {
6315 void *args[2];
6316 json_object *json = NULL;
6317 zebra_l3vni_t *zl3vni = NULL;
6318
6319 if (!is_evpn_enabled()) {
6320 if (use_json)
6321 vty_out(vty, "{}\n");
6322 return;
6323 }
6324
6325 zl3vni = zl3vni_lookup(vni);
6326 if (!zl3vni) {
6327 if (use_json)
6328 vty_out(vty, "{}\n");
6329 else
6330 vty_out(vty, "%% VNI %u does not exist\n", vni);
6331 return;
6332 }
6333
6334 if (use_json)
6335 json = json_object_new_object();
6336
6337 args[0] = vty;
6338 args[1] = json;
6339 zl3vni_print(zl3vni, (void *)args);
6340
6341 if (use_json) {
6342 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6343 json, JSON_C_TO_STRING_PRETTY));
6344 json_object_free(json);
6345 }
6346 }
6347
6348 void zebra_vxlan_print_vrf_vni(struct vty *vty, struct zebra_vrf *zvrf,
6349 json_object *json_vrfs)
6350 {
6351 char buf[ETHER_ADDR_STRLEN];
6352 zebra_l3vni_t *zl3vni = NULL;
6353
6354 zl3vni = zl3vni_lookup(zvrf->l3vni);
6355 if (!zl3vni)
6356 return;
6357
6358 if (!json_vrfs) {
6359 vty_out(vty, "%-37s %-10u %-20s %-20s %-5s %-18s\n",
6360 zvrf_name(zvrf), zl3vni->vni,
6361 zl3vni_vxlan_if_name(zl3vni),
6362 zl3vni_svi_if_name(zl3vni), zl3vni_state2str(zl3vni),
6363 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
6364 } else {
6365 json_object *json_vrf = NULL;
6366
6367 json_vrf = json_object_new_object();
6368 json_object_string_add(json_vrf, "vrf", zvrf_name(zvrf));
6369 json_object_int_add(json_vrf, "vni", zl3vni->vni);
6370 json_object_string_add(json_vrf, "vxlanIntf",
6371 zl3vni_vxlan_if_name(zl3vni));
6372 json_object_string_add(json_vrf, "sviIntf",
6373 zl3vni_svi_if_name(zl3vni));
6374 json_object_string_add(json_vrf, "state",
6375 zl3vni_state2str(zl3vni));
6376 json_object_string_add(
6377 json_vrf, "routerMac",
6378 zl3vni_rmac2str(zl3vni, buf, sizeof(buf)));
6379 json_object_array_add(json_vrfs, json_vrf);
6380 }
6381 }
6382
6383 /*
6384 * Display Neighbors for a VNI (VTY command handler).
6385 */
6386 void zebra_vxlan_print_neigh_vni(struct vty *vty, struct zebra_vrf *zvrf,
6387 vni_t vni, bool use_json)
6388 {
6389 zebra_vni_t *zvni;
6390 uint32_t num_neigh;
6391 struct neigh_walk_ctx wctx;
6392 json_object *json = NULL;
6393
6394 if (!is_evpn_enabled())
6395 return;
6396 zvni = zvni_lookup(vni);
6397 if (!zvni) {
6398 if (use_json)
6399 vty_out(vty, "{}\n");
6400 else
6401 vty_out(vty, "%% VNI %u does not exist\n", vni);
6402 return;
6403 }
6404 num_neigh = hashcount(zvni->neigh_table);
6405 if (!num_neigh)
6406 return;
6407
6408 if (use_json)
6409 json = json_object_new_object();
6410
6411 /* Since we have IPv6 addresses to deal with which can vary widely in
6412 * size, we try to be a bit more elegant in display by first computing
6413 * the maximum width.
6414 */
6415 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
6416 wctx.zvni = zvni;
6417 wctx.vty = vty;
6418 wctx.addr_width = 15;
6419 wctx.json = json;
6420 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
6421
6422 if (!use_json) {
6423 vty_out(vty,
6424 "Number of ARPs (local and remote) known for this VNI: %u\n",
6425 num_neigh);
6426 vty_out(vty, "%*s %-6s %-8s %-17s %-21s %s\n", -wctx.addr_width,
6427 "IP", "Type", "State", "MAC", "Remote VTEP", "Seq #'s");
6428 } else
6429 json_object_int_add(json, "numArpNd", num_neigh);
6430
6431 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
6432 if (use_json) {
6433 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6434 json, JSON_C_TO_STRING_PRETTY));
6435 json_object_free(json);
6436 }
6437 }
6438
6439 /*
6440 * Display neighbors across all VNIs (VTY command handler).
6441 */
6442 void zebra_vxlan_print_neigh_all_vni(struct vty *vty, struct zebra_vrf *zvrf,
6443 bool print_dup, bool use_json)
6444 {
6445 json_object *json = NULL;
6446 void *args[3];
6447
6448 if (!is_evpn_enabled())
6449 return;
6450
6451 if (use_json)
6452 json = json_object_new_object();
6453
6454 args[0] = vty;
6455 args[1] = json;
6456 args[2] = (void *)(ptrdiff_t)print_dup;
6457
6458 hash_iterate(zvrf->vni_table,
6459 (void (*)(struct hash_bucket *,
6460 void *))zvni_print_neigh_hash_all_vni,
6461 args);
6462 if (use_json) {
6463 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6464 json, JSON_C_TO_STRING_PRETTY));
6465 json_object_free(json);
6466 }
6467 }
6468
6469 /*
6470 * Display neighbors across all VNIs in detail(VTY command handler).
6471 */
6472 void zebra_vxlan_print_neigh_all_vni_detail(struct vty *vty,
6473 struct zebra_vrf *zvrf,
6474 bool print_dup, bool use_json)
6475 {
6476 json_object *json = NULL;
6477 void *args[3];
6478
6479 if (!is_evpn_enabled())
6480 return;
6481
6482 if (use_json)
6483 json = json_object_new_object();
6484
6485 args[0] = vty;
6486 args[1] = json;
6487 args[2] = (void *)(ptrdiff_t)print_dup;
6488
6489 hash_iterate(zvrf->vni_table,
6490 (void (*)(struct hash_bucket *,
6491 void *))zvni_print_neigh_hash_all_vni_detail,
6492 args);
6493 if (use_json) {
6494 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6495 json, JSON_C_TO_STRING_PRETTY));
6496 json_object_free(json);
6497 }
6498 }
6499
6500 /*
6501 * Display specific neighbor for a VNI, if present (VTY command handler).
6502 */
6503 void zebra_vxlan_print_specific_neigh_vni(struct vty *vty,
6504 struct zebra_vrf *zvrf, vni_t vni,
6505 struct ipaddr *ip, bool use_json)
6506 {
6507 zebra_vni_t *zvni;
6508 zebra_neigh_t *n;
6509 json_object *json = NULL;
6510
6511 if (!is_evpn_enabled())
6512 return;
6513 zvni = zvni_lookup(vni);
6514 if (!zvni) {
6515 if (use_json)
6516 vty_out(vty, "{}\n");
6517 else
6518 vty_out(vty, "%% VNI %u does not exist\n", vni);
6519 return;
6520 }
6521 n = zvni_neigh_lookup(zvni, ip);
6522 if (!n) {
6523 if (!use_json)
6524 vty_out(vty,
6525 "%% Requested neighbor does not exist in VNI %u\n",
6526 vni);
6527 return;
6528 }
6529 if (use_json)
6530 json = json_object_new_object();
6531
6532 zvni_print_neigh(n, vty, json);
6533
6534 if (use_json) {
6535 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6536 json, JSON_C_TO_STRING_PRETTY));
6537 json_object_free(json);
6538 }
6539 }
6540
6541 /*
6542 * Display neighbors for a VNI from specific VTEP (VTY command handler).
6543 * By definition, these are remote neighbors.
6544 */
6545 void zebra_vxlan_print_neigh_vni_vtep(struct vty *vty, struct zebra_vrf *zvrf,
6546 vni_t vni, struct in_addr vtep_ip,
6547 bool use_json)
6548 {
6549 zebra_vni_t *zvni;
6550 uint32_t num_neigh;
6551 struct neigh_walk_ctx wctx;
6552 json_object *json = NULL;
6553
6554 if (!is_evpn_enabled())
6555 return;
6556 zvni = zvni_lookup(vni);
6557 if (!zvni) {
6558 if (use_json)
6559 vty_out(vty, "{}\n");
6560 else
6561 vty_out(vty, "%% VNI %u does not exist\n", vni);
6562 return;
6563 }
6564 num_neigh = hashcount(zvni->neigh_table);
6565 if (!num_neigh)
6566 return;
6567
6568 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
6569 wctx.zvni = zvni;
6570 wctx.vty = vty;
6571 wctx.addr_width = 15;
6572 wctx.flags = SHOW_REMOTE_NEIGH_FROM_VTEP;
6573 wctx.r_vtep_ip = vtep_ip;
6574 wctx.json = json;
6575 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
6576 hash_iterate(zvni->neigh_table, zvni_print_neigh_hash, &wctx);
6577
6578 if (use_json) {
6579 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6580 json, JSON_C_TO_STRING_PRETTY));
6581 json_object_free(json);
6582 }
6583 }
6584
6585 /*
6586 * Display Duplicate detected Neighbors for a VNI
6587 * (VTY command handler).
6588 */
6589 void zebra_vxlan_print_neigh_vni_dad(struct vty *vty,
6590 struct zebra_vrf *zvrf,
6591 vni_t vni,
6592 bool use_json)
6593 {
6594 zebra_vni_t *zvni;
6595 uint32_t num_neigh;
6596 struct neigh_walk_ctx wctx;
6597 json_object *json = NULL;
6598
6599 if (!is_evpn_enabled())
6600 return;
6601
6602 zvni = zvni_lookup(vni);
6603 if (!zvni) {
6604 vty_out(vty, "%% VNI %u does not exist\n", vni);
6605 return;
6606 }
6607
6608 num_neigh = hashcount(zvni->neigh_table);
6609 if (!num_neigh)
6610 return;
6611
6612 num_neigh = num_dup_detected_neighs(zvni);
6613 if (!num_neigh)
6614 return;
6615
6616 if (use_json)
6617 json = json_object_new_object();
6618
6619 /* Since we have IPv6 addresses to deal with which can vary widely in
6620 * size, we try to be a bit more elegant in display by first computing
6621 * the maximum width.
6622 */
6623 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
6624 wctx.zvni = zvni;
6625 wctx.vty = vty;
6626 wctx.addr_width = 15;
6627 wctx.json = json;
6628 hash_iterate(zvni->neigh_table, zvni_find_neigh_addr_width, &wctx);
6629
6630 if (!use_json) {
6631 vty_out(vty,
6632 "Number of ARPs (local and remote) known for this VNI: %u\n",
6633 num_neigh);
6634 vty_out(vty, "%*s %-6s %-8s %-17s %-21s\n",
6635 -wctx.addr_width, "IP", "Type",
6636 "State", "MAC", "Remote VTEP");
6637 } else
6638 json_object_int_add(json, "numArpNd", num_neigh);
6639
6640 hash_iterate(zvni->neigh_table, zvni_print_dad_neigh_hash, &wctx);
6641
6642 if (use_json) {
6643 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6644 json, JSON_C_TO_STRING_PRETTY));
6645 json_object_free(json);
6646 }
6647 }
6648
6649 /*
6650 * Display MACs for a VNI (VTY command handler).
6651 */
6652 void zebra_vxlan_print_macs_vni(struct vty *vty, struct zebra_vrf *zvrf,
6653 vni_t vni, bool use_json)
6654 {
6655 zebra_vni_t *zvni;
6656 uint32_t num_macs;
6657 struct mac_walk_ctx wctx;
6658 json_object *json = NULL;
6659 json_object *json_mac = NULL;
6660
6661 if (!is_evpn_enabled())
6662 return;
6663 zvni = zvni_lookup(vni);
6664 if (!zvni) {
6665 if (use_json)
6666 vty_out(vty, "{}\n");
6667 else
6668 vty_out(vty, "%% VNI %u does not exist\n", vni);
6669 return;
6670 }
6671 num_macs = num_valid_macs(zvni);
6672 if (!num_macs)
6673 return;
6674
6675 if (use_json) {
6676 json = json_object_new_object();
6677 json_mac = json_object_new_object();
6678 }
6679
6680 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6681 wctx.zvni = zvni;
6682 wctx.vty = vty;
6683 wctx.json = json_mac;
6684
6685 if (!use_json) {
6686 vty_out(vty,
6687 "Number of MACs (local and remote) known for this VNI: %u\n",
6688 num_macs);
6689 vty_out(vty, "%-17s %-6s %-21s %-5s %s\n", "MAC", "Type",
6690 "Intf/Remote VTEP", "VLAN", "Seq #'s");
6691 } else
6692 json_object_int_add(json, "numMacs", num_macs);
6693
6694 hash_iterate(zvni->mac_table, zvni_print_mac_hash, &wctx);
6695
6696 if (use_json) {
6697 json_object_object_add(json, "macs", json_mac);
6698 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6699 json, JSON_C_TO_STRING_PRETTY));
6700 json_object_free(json);
6701 }
6702 }
6703
6704 /*
6705 * Display MACs for all VNIs (VTY command handler).
6706 */
6707 void zebra_vxlan_print_macs_all_vni(struct vty *vty, struct zebra_vrf *zvrf,
6708 bool print_dup, bool use_json)
6709 {
6710 struct mac_walk_ctx wctx;
6711 json_object *json = NULL;
6712
6713 if (!is_evpn_enabled()) {
6714 if (use_json)
6715 vty_out(vty, "{}\n");
6716 return;
6717 }
6718 if (use_json)
6719 json = json_object_new_object();
6720
6721 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6722 wctx.vty = vty;
6723 wctx.json = json;
6724 wctx.print_dup = print_dup;
6725 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni, &wctx);
6726
6727 if (use_json) {
6728 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6729 json, JSON_C_TO_STRING_PRETTY));
6730 json_object_free(json);
6731 }
6732 }
6733
6734 /*
6735 * Display MACs in detail for all VNIs (VTY command handler).
6736 */
6737 void zebra_vxlan_print_macs_all_vni_detail(struct vty *vty,
6738 struct zebra_vrf *zvrf,
6739 bool print_dup, bool use_json)
6740 {
6741 struct mac_walk_ctx wctx;
6742 json_object *json = NULL;
6743
6744 if (!is_evpn_enabled()) {
6745 if (use_json)
6746 vty_out(vty, "{}\n");
6747 return;
6748 }
6749 if (use_json)
6750 json = json_object_new_object();
6751
6752 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6753 wctx.vty = vty;
6754 wctx.json = json;
6755 wctx.print_dup = print_dup;
6756 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni_detail,
6757 &wctx);
6758
6759 if (use_json) {
6760 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6761 json, JSON_C_TO_STRING_PRETTY));
6762 json_object_free(json);
6763 }
6764 }
6765
6766 /*
6767 * Display MACs for all VNIs (VTY command handler).
6768 */
6769 void zebra_vxlan_print_macs_all_vni_vtep(struct vty *vty,
6770 struct zebra_vrf *zvrf,
6771 struct in_addr vtep_ip, bool use_json)
6772 {
6773 struct mac_walk_ctx wctx;
6774 json_object *json = NULL;
6775
6776 if (!is_evpn_enabled())
6777 return;
6778
6779 if (use_json)
6780 json = json_object_new_object();
6781
6782 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6783 wctx.vty = vty;
6784 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
6785 wctx.r_vtep_ip = vtep_ip;
6786 wctx.json = json;
6787 hash_iterate(zvrf->vni_table, zvni_print_mac_hash_all_vni, &wctx);
6788
6789 if (use_json) {
6790 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6791 json, JSON_C_TO_STRING_PRETTY));
6792 json_object_free(json);
6793 }
6794 }
6795
6796 /*
6797 * Display specific MAC for a VNI, if present (VTY command handler).
6798 */
6799 void zebra_vxlan_print_specific_mac_vni(struct vty *vty, struct zebra_vrf *zvrf,
6800 vni_t vni, struct ethaddr *macaddr,
6801 bool use_json)
6802 {
6803 zebra_vni_t *zvni;
6804 zebra_mac_t *mac;
6805 json_object *json = NULL;
6806
6807 if (!is_evpn_enabled())
6808 return;
6809
6810 zvni = zvni_lookup(vni);
6811 if (!zvni) {
6812 if (use_json)
6813 vty_out(vty, "{}\n");
6814 else
6815 vty_out(vty, "%% VNI %u does not exist\n", vni);
6816 return;
6817 }
6818 mac = zvni_mac_lookup(zvni, macaddr);
6819 if (!mac) {
6820 if (use_json)
6821 vty_out(vty, "{}\n");
6822 else
6823 vty_out(vty,
6824 "%% Requested MAC does not exist in VNI %u\n",
6825 vni);
6826 return;
6827 }
6828
6829 if (use_json)
6830 json = json_object_new_object();
6831
6832 zvni_print_mac(mac, vty, json);
6833 if (use_json) {
6834 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6835 json, JSON_C_TO_STRING_PRETTY));
6836 json_object_free(json);
6837 }
6838 }
6839
6840 /* Print Duplicate MACs per VNI */
6841 void zebra_vxlan_print_macs_vni_dad(struct vty *vty,
6842 struct zebra_vrf *zvrf,
6843 vni_t vni, bool use_json)
6844 {
6845 zebra_vni_t *zvni;
6846 struct mac_walk_ctx wctx;
6847 uint32_t num_macs;
6848 json_object *json = NULL;
6849 json_object *json_mac = NULL;
6850
6851 if (!is_evpn_enabled())
6852 return;
6853
6854 zvni = zvni_lookup(vni);
6855 if (!zvni) {
6856 vty_out(vty, "%% VNI %u does not exist\n", vni);
6857 return;
6858 }
6859
6860 num_macs = num_valid_macs(zvni);
6861 if (!num_macs)
6862 return;
6863
6864 num_macs = num_dup_detected_macs(zvni);
6865 if (!num_macs)
6866 return;
6867
6868 if (use_json) {
6869 json = json_object_new_object();
6870 json_mac = json_object_new_object();
6871 }
6872
6873 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
6874 wctx.zvni = zvni;
6875 wctx.vty = vty;
6876 wctx.json = json_mac;
6877
6878 if (!use_json) {
6879 vty_out(vty,
6880 "Number of MACs (local and remote) known for this VNI: %u\n",
6881 num_macs);
6882 vty_out(vty, "%-17s %-6s %-21s %-5s\n", "MAC", "Type",
6883 "Intf/Remote VTEP", "VLAN");
6884 } else
6885 json_object_int_add(json, "numMacs", num_macs);
6886
6887 hash_iterate(zvni->mac_table, zvni_print_dad_mac_hash, &wctx);
6888
6889 if (use_json) {
6890 json_object_object_add(json, "macs", json_mac);
6891 vty_out(vty, "%s\n", json_object_to_json_string_ext(
6892 json, JSON_C_TO_STRING_PRETTY));
6893 json_object_free(json);
6894 }
6895
6896 }
6897
6898 int zebra_vxlan_clear_dup_detect_vni_mac(struct vty *vty,
6899 struct zebra_vrf *zvrf,
6900 vni_t vni, struct ethaddr *macaddr)
6901 {
6902 zebra_vni_t *zvni;
6903 zebra_mac_t *mac;
6904 struct listnode *node = NULL;
6905 zebra_neigh_t *nbr = NULL;
6906
6907 if (!is_evpn_enabled())
6908 return CMD_SUCCESS;
6909
6910 zvni = zvni_lookup(vni);
6911 if (!zvni) {
6912 vty_out(vty, "%% VNI %u does not exist\n", vni);
6913 return CMD_WARNING;
6914 }
6915
6916 mac = zvni_mac_lookup(zvni, macaddr);
6917 if (!mac) {
6918 vty_out(vty, "%% Requested MAC does not exist in VNI %u\n",
6919 vni);
6920 return CMD_WARNING;
6921 }
6922
6923 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
6924 vty_out(vty, "%% Requested MAC is not duplicate detected\n");
6925 return CMD_WARNING;
6926 }
6927
6928 /* Remove all IPs as duplicate associcated with this MAC */
6929 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
6930 /* For local neigh mark inactive so MACIP update is generated
6931 * to BGP. This is a scenario where MAC update received
6932 * and detected as duplicate which marked neigh as duplicate.
6933 * Later local neigh update did not get a chance to relay
6934 * to BGP. Similarly remote macip update, neigh needs to be
6935 * installed locally.
6936 */
6937 if (zvrf->dad_freeze &&
6938 CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
6939 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
6940 ZEBRA_NEIGH_SET_INACTIVE(nbr);
6941 else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE))
6942 zvni_neigh_install(zvni, nbr);
6943 }
6944
6945 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
6946 nbr->dad_count = 0;
6947 nbr->detect_start_time.tv_sec = 0;
6948 nbr->dad_dup_detect_time = 0;
6949 }
6950
6951 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
6952 mac->dad_count = 0;
6953 mac->detect_start_time.tv_sec = 0;
6954 mac->detect_start_time.tv_usec = 0;
6955 mac->dad_dup_detect_time = 0;
6956 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
6957
6958 /* warn-only action return */
6959 if (!zvrf->dad_freeze)
6960 return CMD_SUCCESS;
6961
6962 /* Local: Notify Peer VTEPs, Remote: Install the entry */
6963 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
6964 /* Inform to BGP */
6965 if (zvni_mac_send_add_to_client(zvni->vni,
6966 &mac->macaddr,
6967 mac->flags,
6968 mac->loc_seq))
6969 return CMD_SUCCESS;
6970
6971 /* Process all neighbors associated with this MAC. */
6972 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
6973
6974 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
6975 zvni_process_neigh_on_remote_mac_add(zvni, mac);
6976
6977 /* Install the entry. */
6978 zvni_mac_install(zvni, mac);
6979 }
6980
6981 return CMD_SUCCESS;
6982 }
6983
6984 int zebra_vxlan_clear_dup_detect_vni_ip(struct vty *vty,
6985 struct zebra_vrf *zvrf,
6986 vni_t vni, struct ipaddr *ip)
6987 {
6988 zebra_vni_t *zvni;
6989 zebra_neigh_t *nbr;
6990 zebra_mac_t *mac;
6991 char buf[INET6_ADDRSTRLEN];
6992 char buf2[ETHER_ADDR_STRLEN];
6993
6994 if (!is_evpn_enabled())
6995 return CMD_SUCCESS;
6996
6997 zvni = zvni_lookup(vni);
6998 if (!zvni) {
6999 vty_out(vty, "%% VNI %u does not exist\n", vni);
7000 return CMD_WARNING;
7001 }
7002
7003 nbr = zvni_neigh_lookup(zvni, ip);
7004 if (!nbr) {
7005 vty_out(vty,
7006 "%% Requested host IP does not exist in VNI %u\n",
7007 vni);
7008 return CMD_WARNING;
7009 }
7010
7011 ipaddr2str(&nbr->ip, buf, sizeof(buf));
7012
7013 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
7014 vty_out(vty,
7015 "%% Requested host IP %s is not duplicate detected\n",
7016 buf);
7017 return CMD_WARNING;
7018 }
7019
7020 mac = zvni_mac_lookup(zvni, &nbr->emac);
7021
7022 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) {
7023 vty_out(vty,
7024 "%% Requested IP's associated MAC %s is still in duplicate state\n",
7025 prefix_mac2str(&nbr->emac, buf2, sizeof(buf2)));
7026 return CMD_WARNING_CONFIG_FAILED;
7027 }
7028
7029 if (IS_ZEBRA_DEBUG_VXLAN)
7030 zlog_debug("%s: clear neigh %s in dup state, flags 0x%x seq %u",
7031 __PRETTY_FUNCTION__, buf, nbr->flags,
7032 nbr->loc_seq);
7033
7034 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
7035 nbr->dad_count = 0;
7036 nbr->detect_start_time.tv_sec = 0;
7037 nbr->detect_start_time.tv_usec = 0;
7038 nbr->dad_dup_detect_time = 0;
7039 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
7040
7041 if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
7042 zvni_neigh_send_add_to_client(zvni->vni, ip,
7043 &nbr->emac,
7044 nbr->flags, nbr->loc_seq);
7045 } else if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
7046 zvni_neigh_install(zvni, nbr);
7047 }
7048
7049 return CMD_SUCCESS;
7050 }
7051
7052 static void zvni_clear_dup_mac_hash(struct hash_bucket *bucket, void *ctxt)
7053 {
7054 struct mac_walk_ctx *wctx = ctxt;
7055 zebra_mac_t *mac;
7056 zebra_vni_t *zvni;
7057 struct listnode *node = NULL;
7058 zebra_neigh_t *nbr = NULL;
7059
7060 mac = (zebra_mac_t *)bucket->data;
7061 if (!mac)
7062 return;
7063
7064 zvni = wctx->zvni;
7065
7066 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE))
7067 return;
7068
7069 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
7070 mac->dad_count = 0;
7071 mac->detect_start_time.tv_sec = 0;
7072 mac->detect_start_time.tv_usec = 0;
7073 mac->dad_dup_detect_time = 0;
7074 THREAD_OFF(mac->dad_mac_auto_recovery_timer);
7075
7076 /* Remove all IPs as duplicate associcated with this MAC */
7077 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
7078 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)
7079 && nbr->dad_count)
7080 ZEBRA_NEIGH_SET_INACTIVE(nbr);
7081
7082 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
7083 nbr->dad_count = 0;
7084 nbr->detect_start_time.tv_sec = 0;
7085 nbr->dad_dup_detect_time = 0;
7086 }
7087
7088 /* Local: Notify Peer VTEPs, Remote: Install the entry */
7089 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
7090 /* Inform to BGP */
7091 if (zvni_mac_send_add_to_client(zvni->vni,
7092 &mac->macaddr,
7093 mac->flags, mac->loc_seq))
7094 return;
7095
7096 /* Process all neighbors associated with this MAC. */
7097 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
7098
7099 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
7100 zvni_process_neigh_on_remote_mac_add(zvni, mac);
7101
7102 /* Install the entry. */
7103 zvni_mac_install(zvni, mac);
7104 }
7105 }
7106
7107 static void zvni_clear_dup_neigh_hash(struct hash_bucket *bucket, void *ctxt)
7108 {
7109 struct neigh_walk_ctx *wctx = ctxt;
7110 zebra_neigh_t *nbr;
7111 zebra_vni_t *zvni;
7112 char buf[INET6_ADDRSTRLEN];
7113
7114 nbr = (zebra_neigh_t *)bucket->data;
7115 if (!nbr)
7116 return;
7117
7118 zvni = wctx->zvni;
7119
7120 if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE))
7121 return;
7122
7123 if (IS_ZEBRA_DEBUG_VXLAN) {
7124 ipaddr2str(&nbr->ip, buf, sizeof(buf));
7125 zlog_debug(
7126 "%s: clear neigh %s dup state, flags 0x%x seq %u",
7127 __PRETTY_FUNCTION__, buf,
7128 nbr->flags, nbr->loc_seq);
7129 }
7130
7131 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
7132 nbr->dad_count = 0;
7133 nbr->detect_start_time.tv_sec = 0;
7134 nbr->detect_start_time.tv_usec = 0;
7135 nbr->dad_dup_detect_time = 0;
7136 THREAD_OFF(nbr->dad_ip_auto_recovery_timer);
7137
7138 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
7139 zvni_neigh_send_add_to_client(zvni->vni, &nbr->ip,
7140 &nbr->emac,
7141 nbr->flags, nbr->loc_seq);
7142 } else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
7143 zvni_neigh_install(zvni, nbr);
7144 }
7145 }
7146
7147 static void zvni_clear_dup_detect_hash_vni_all(struct hash_bucket *bucket,
7148 void **args)
7149 {
7150 struct vty *vty;
7151 zebra_vni_t *zvni;
7152 struct zebra_vrf *zvrf;
7153 struct mac_walk_ctx m_wctx;
7154 struct neigh_walk_ctx n_wctx;
7155
7156 zvni = (zebra_vni_t *)bucket->data;
7157 if (!zvni)
7158 return;
7159
7160 vty = (struct vty *)args[0];
7161 zvrf = (struct zebra_vrf *)args[1];
7162
7163 if (hashcount(zvni->neigh_table)) {
7164 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
7165 n_wctx.vty = vty;
7166 n_wctx.zvni = zvni;
7167 n_wctx.zvrf = zvrf;
7168 hash_iterate(zvni->neigh_table, zvni_clear_dup_neigh_hash,
7169 &n_wctx);
7170 }
7171
7172 if (num_valid_macs(zvni)) {
7173 memset(&m_wctx, 0, sizeof(struct mac_walk_ctx));
7174 m_wctx.zvni = zvni;
7175 m_wctx.vty = vty;
7176 m_wctx.zvrf = zvrf;
7177 hash_iterate(zvni->mac_table, zvni_clear_dup_mac_hash, &m_wctx);
7178 }
7179
7180 }
7181
7182 int zebra_vxlan_clear_dup_detect_vni_all(struct vty *vty,
7183 struct zebra_vrf *zvrf)
7184 {
7185 void *args[2];
7186
7187 if (!is_evpn_enabled())
7188 return CMD_SUCCESS;
7189
7190 args[0] = vty;
7191 args[1] = zvrf;
7192
7193 hash_iterate(zvrf->vni_table,
7194 (void (*)(struct hash_bucket *, void *))
7195 zvni_clear_dup_detect_hash_vni_all, args);
7196
7197 return CMD_SUCCESS;
7198 }
7199
7200 int zebra_vxlan_clear_dup_detect_vni(struct vty *vty,
7201 struct zebra_vrf *zvrf,
7202 vni_t vni)
7203 {
7204 zebra_vni_t *zvni;
7205 struct mac_walk_ctx m_wctx;
7206 struct neigh_walk_ctx n_wctx;
7207
7208 if (!is_evpn_enabled())
7209 return CMD_SUCCESS;
7210
7211 zvni = zvni_lookup(vni);
7212 if (!zvni) {
7213 vty_out(vty, "%% VNI %u does not exist\n", vni);
7214 return CMD_WARNING;
7215 }
7216
7217 if (hashcount(zvni->neigh_table)) {
7218 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
7219 n_wctx.vty = vty;
7220 n_wctx.zvni = zvni;
7221 n_wctx.zvrf = zvrf;
7222 hash_iterate(zvni->neigh_table, zvni_clear_dup_neigh_hash,
7223 &n_wctx);
7224 }
7225
7226 if (num_valid_macs(zvni)) {
7227 memset(&m_wctx, 0, sizeof(struct mac_walk_ctx));
7228 m_wctx.zvni = zvni;
7229 m_wctx.vty = vty;
7230 m_wctx.zvrf = zvrf;
7231 hash_iterate(zvni->mac_table, zvni_clear_dup_mac_hash, &m_wctx);
7232 }
7233
7234 return CMD_SUCCESS;
7235 }
7236
7237 /*
7238 * Display MACs for a VNI from specific VTEP (VTY command handler).
7239 */
7240 void zebra_vxlan_print_macs_vni_vtep(struct vty *vty, struct zebra_vrf *zvrf,
7241 vni_t vni, struct in_addr vtep_ip,
7242 bool use_json)
7243 {
7244 zebra_vni_t *zvni;
7245 uint32_t num_macs;
7246 struct mac_walk_ctx wctx;
7247 json_object *json = NULL;
7248 json_object *json_mac = NULL;
7249
7250 if (!is_evpn_enabled())
7251 return;
7252 zvni = zvni_lookup(vni);
7253 if (!zvni) {
7254 if (use_json)
7255 vty_out(vty, "{}\n");
7256 else
7257 vty_out(vty, "%% VNI %u does not exist\n", vni);
7258 return;
7259 }
7260 num_macs = num_valid_macs(zvni);
7261 if (!num_macs)
7262 return;
7263
7264 if (use_json) {
7265 json = json_object_new_object();
7266 json_mac = json_object_new_object();
7267 }
7268
7269 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
7270 wctx.zvni = zvni;
7271 wctx.vty = vty;
7272 wctx.flags = SHOW_REMOTE_MAC_FROM_VTEP;
7273 wctx.r_vtep_ip = vtep_ip;
7274 wctx.json = json_mac;
7275 hash_iterate(zvni->mac_table, zvni_print_mac_hash, &wctx);
7276
7277 if (use_json) {
7278 json_object_int_add(json, "numMacs", wctx.count);
7279 if (wctx.count)
7280 json_object_object_add(json, "macs", json_mac);
7281 vty_out(vty, "%s\n", json_object_to_json_string_ext(
7282 json, JSON_C_TO_STRING_PRETTY));
7283 json_object_free(json);
7284 }
7285 }
7286
7287
7288 /*
7289 * Display VNI information (VTY command handler).
7290 *
7291 * use_json flag indicates that output should be in JSON format.
7292 * json_array is non NULL when JSON output needs to be aggregated (by the
7293 * caller) and then printed, otherwise, JSON evpn vni info is printed
7294 * right away.
7295 */
7296 void zebra_vxlan_print_vni(struct vty *vty, struct zebra_vrf *zvrf, vni_t vni,
7297 bool use_json, json_object *json_array)
7298 {
7299 json_object *json = NULL;
7300 void *args[2];
7301 zebra_l3vni_t *zl3vni = NULL;
7302 zebra_vni_t *zvni = NULL;
7303
7304 if (!is_evpn_enabled())
7305 return;
7306
7307 if (use_json)
7308 json = json_object_new_object();
7309
7310 args[0] = vty;
7311 args[1] = json;
7312
7313 zl3vni = zl3vni_lookup(vni);
7314 if (zl3vni) {
7315 zl3vni_print(zl3vni, (void *)args);
7316 } else {
7317 zvni = zvni_lookup(vni);
7318 if (zvni)
7319 zvni_print(zvni, (void *)args);
7320 else if (!json)
7321 vty_out(vty, "%% VNI %u does not exist\n", vni);
7322 }
7323
7324 if (use_json) {
7325 /*
7326 * Each "json" object contains info about 1 VNI.
7327 * When "json_array" is non-null, we aggreggate the json output
7328 * into json_array and print it as a JSON array.
7329 */
7330 if (json_array)
7331 json_object_array_add(json_array, json);
7332 else {
7333 vty_out(vty, "%s\n", json_object_to_json_string_ext(
7334 json, JSON_C_TO_STRING_PRETTY));
7335 json_object_free(json);
7336 }
7337 }
7338 }
7339
7340 /* Display all global details for EVPN */
7341 void zebra_vxlan_print_evpn(struct vty *vty, bool uj)
7342 {
7343 int num_l2vnis = 0;
7344 int num_l3vnis = 0;
7345 int num_vnis = 0;
7346 json_object *json = NULL;
7347 struct zebra_vrf *zvrf = NULL;
7348
7349 if (!is_evpn_enabled())
7350 return;
7351
7352 zvrf = zebra_vrf_get_evpn();
7353 if (!zvrf)
7354 return;
7355
7356 num_l3vnis = hashcount(zrouter.l3vni_table);
7357 num_l2vnis = hashcount(zvrf->vni_table);
7358 num_vnis = num_l2vnis + num_l3vnis;
7359
7360 if (uj) {
7361 json = json_object_new_object();
7362 json_object_string_add(json, "advertiseGatewayMacip",
7363 zvrf->advertise_gw_macip ? "Yes" : "No");
7364 json_object_int_add(json, "numVnis", num_vnis);
7365 json_object_int_add(json, "numL2Vnis", num_l2vnis);
7366 json_object_int_add(json, "numL3Vnis", num_l3vnis);
7367 if (zvrf->dup_addr_detect)
7368 json_object_boolean_true_add(json,
7369 "isDuplicateAddrDetection");
7370 else
7371 json_object_boolean_false_add(json,
7372 "isDuplicateAddrDetection");
7373 json_object_int_add(json, "maxMoves", zvrf->dad_max_moves);
7374 json_object_int_add(json, "detectionTime", zvrf->dad_time);
7375 json_object_int_add(json, "detectionFreezeTime",
7376 zvrf->dad_freeze_time);
7377
7378 } else {
7379 vty_out(vty, "L2 VNIs: %u\n", num_l2vnis);
7380 vty_out(vty, "L3 VNIs: %u\n", num_l3vnis);
7381 vty_out(vty, "Advertise gateway mac-ip: %s\n",
7382 zvrf->advertise_gw_macip ? "Yes" : "No");
7383 vty_out(vty, "Advertise svi mac-ip: %s\n",
7384 zvrf->advertise_svi_macip ? "Yes" : "No");
7385 vty_out(vty, "Duplicate address detection: %s\n",
7386 zvrf->dup_addr_detect ? "Enable" : "Disable");
7387 vty_out(vty, " Detection max-moves %u, time %d\n",
7388 zvrf->dad_max_moves, zvrf->dad_time);
7389 if (zvrf->dad_freeze) {
7390 if (zvrf->dad_freeze_time)
7391 vty_out(vty, " Detection freeze %u\n",
7392 zvrf->dad_freeze_time);
7393 else
7394 vty_out(vty, " Detection freeze %s\n",
7395 "permanent");
7396 }
7397 }
7398
7399 if (uj) {
7400 vty_out(vty, "%s\n", json_object_to_json_string_ext(
7401 json, JSON_C_TO_STRING_PRETTY));
7402 json_object_free(json);
7403 }
7404 }
7405
7406 /*
7407 * Display VNI hash table (VTY command handler).
7408 */
7409 void zebra_vxlan_print_vnis(struct vty *vty, struct zebra_vrf *zvrf,
7410 bool use_json)
7411 {
7412 json_object *json = NULL;
7413 void *args[2];
7414
7415 if (!is_evpn_enabled())
7416 return;
7417
7418 if (use_json)
7419 json = json_object_new_object();
7420 else
7421 vty_out(vty, "%-10s %-4s %-21s %-8s %-8s %-15s %-37s\n", "VNI",
7422 "Type", "VxLAN IF", "# MACs", "# ARPs",
7423 "# Remote VTEPs", "Tenant VRF");
7424
7425 args[0] = vty;
7426 args[1] = json;
7427
7428 /* Display all L2-VNIs */
7429 hash_iterate(zvrf->vni_table,
7430 (void (*)(struct hash_bucket *, void *))zvni_print_hash,
7431 args);
7432
7433 /* Display all L3-VNIs */
7434 hash_iterate(zrouter.l3vni_table,
7435 (void (*)(struct hash_bucket *, void *))zl3vni_print_hash,
7436 args);
7437
7438 if (use_json) {
7439 vty_out(vty, "%s\n", json_object_to_json_string_ext(
7440 json, JSON_C_TO_STRING_PRETTY));
7441 json_object_free(json);
7442 }
7443 }
7444
7445 void zebra_vxlan_dup_addr_detection(ZAPI_HANDLER_ARGS)
7446 {
7447 struct stream *s;
7448 int time = 0;
7449 uint32_t max_moves = 0;
7450 uint32_t freeze_time = 0;
7451 bool dup_addr_detect = false;
7452 bool freeze = false;
7453
7454 s = msg;
7455 STREAM_GETL(s, dup_addr_detect);
7456 STREAM_GETL(s, time);
7457 STREAM_GETL(s, max_moves);
7458 STREAM_GETL(s, freeze);
7459 STREAM_GETL(s, freeze_time);
7460
7461 /* DAD previous state was enabled, and new state is disable,
7462 * clear all duplicate detected addresses.
7463 */
7464 if (zvrf->dup_addr_detect && !dup_addr_detect)
7465 zebra_vxlan_clear_dup_detect_vni_all(NULL, zvrf);
7466
7467 zvrf->dup_addr_detect = dup_addr_detect;
7468 zvrf->dad_time = time;
7469 zvrf->dad_max_moves = max_moves;
7470 zvrf->dad_freeze = freeze;
7471 zvrf->dad_freeze_time = freeze_time;
7472
7473 if (IS_ZEBRA_DEBUG_VXLAN)
7474 zlog_debug(
7475 "VRF %s duplicate detect %s max_moves %u timeout %u freeze %s freeze_time %u",
7476 vrf_id_to_name(zvrf->vrf->vrf_id),
7477 zvrf->dup_addr_detect ? "enable" : "disable",
7478 zvrf->dad_max_moves,
7479 zvrf->dad_time,
7480 zvrf->dad_freeze ? "enable" : "disable",
7481 zvrf->dad_freeze_time);
7482
7483 stream_failure:
7484 return;
7485 }
7486
7487 /*
7488 * Display VNI hash table in detail(VTY command handler).
7489 */
7490 void zebra_vxlan_print_vnis_detail(struct vty *vty, struct zebra_vrf *zvrf,
7491 bool use_json)
7492 {
7493 json_object *json_array = NULL;
7494 struct zebra_ns *zns = NULL;
7495 struct zvni_evpn_show zes;
7496
7497 if (!is_evpn_enabled())
7498 return;
7499
7500 zns = zebra_ns_lookup(NS_DEFAULT);
7501 if (!zns)
7502 return;
7503
7504 if (use_json)
7505 json_array = json_object_new_array();
7506
7507 zes.vty = vty;
7508 zes.json = json_array;
7509 zes.zvrf = zvrf;
7510 zes.use_json = use_json;
7511
7512 /* Display all L2-VNIs */
7513 hash_iterate(
7514 zvrf->vni_table,
7515 (void (*)(struct hash_bucket *, void *))zvni_print_hash_detail,
7516 &zes);
7517
7518 /* Display all L3-VNIs */
7519 hash_iterate(zrouter.l3vni_table,
7520 (void (*)(struct hash_bucket *,
7521 void *))zl3vni_print_hash_detail,
7522 &zes);
7523
7524 if (use_json) {
7525 vty_out(vty, "%s\n",
7526 json_object_to_json_string_ext(
7527 json_array, JSON_C_TO_STRING_PRETTY));
7528 json_object_free(json_array);
7529 }
7530 }
7531
7532 /*
7533 * Handle neighbor delete notification from the kernel (on a VLAN device
7534 * / L3 interface). This may result in either the neighbor getting deleted
7535 * from our database or being re-added to the kernel (if it is a valid
7536 * remote neighbor).
7537 */
7538 int zebra_vxlan_handle_kernel_neigh_del(struct interface *ifp,
7539 struct interface *link_if,
7540 struct ipaddr *ip)
7541 {
7542 char buf[INET6_ADDRSTRLEN];
7543 char buf2[ETHER_ADDR_STRLEN];
7544 zebra_neigh_t *n = NULL;
7545 zebra_vni_t *zvni = NULL;
7546 zebra_mac_t *zmac = NULL;
7547 zebra_l3vni_t *zl3vni = NULL;
7548 struct zebra_vrf *zvrf;
7549
7550 /* check if this is a remote neigh entry corresponding to remote
7551 * next-hop
7552 */
7553 zl3vni = zl3vni_from_svi(ifp, link_if);
7554 if (zl3vni)
7555 return zl3vni_local_nh_del(zl3vni, ip);
7556
7557 /* We are only interested in neighbors on an SVI that resides on top
7558 * of a VxLAN bridge.
7559 */
7560 zvni = zvni_from_svi(ifp, link_if);
7561 if (!zvni) {
7562 if (IS_ZEBRA_DEBUG_VXLAN)
7563 zlog_debug("%s: Del neighbor %s VNI is not present for interface %s",
7564 __PRETTY_FUNCTION__,
7565 ipaddr2str(ip, buf, sizeof(buf)), ifp->name);
7566 return 0;
7567 }
7568
7569 if (!zvni->vxlan_if) {
7570 zlog_debug(
7571 "VNI %u hash %p doesn't have intf upon local neighbor DEL",
7572 zvni->vni, zvni);
7573 return -1;
7574 }
7575
7576 if (IS_ZEBRA_DEBUG_VXLAN)
7577 zlog_debug("Del neighbor %s intf %s(%u) -> L2-VNI %u",
7578 ipaddr2str(ip, buf, sizeof(buf)), ifp->name,
7579 ifp->ifindex, zvni->vni);
7580
7581 /* If entry doesn't exist, nothing to do. */
7582 n = zvni_neigh_lookup(zvni, ip);
7583 if (!n)
7584 return 0;
7585
7586 zmac = zvni_mac_lookup(zvni, &n->emac);
7587 if (!zmac) {
7588 if (IS_ZEBRA_DEBUG_VXLAN)
7589 zlog_debug(
7590 "Trying to del a neigh %s without a mac %s on VNI %u",
7591 ipaddr2str(ip, buf, sizeof(buf)),
7592 prefix_mac2str(&n->emac, buf2, sizeof(buf2)),
7593 zvni->vni);
7594
7595 return 0;
7596 }
7597
7598 /* If it is a remote entry, the kernel has aged this out or someone has
7599 * deleted it, it needs to be re-installed as Quagga is the owner.
7600 */
7601 if (CHECK_FLAG(n->flags, ZEBRA_NEIGH_REMOTE)) {
7602 zvni_neigh_install(zvni, n);
7603 return 0;
7604 }
7605
7606 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
7607 if (!zvrf) {
7608 zlog_debug("%s: VNI %u vrf lookup failed.",
7609 __PRETTY_FUNCTION__, zvni->vni);
7610 return -1;
7611 }
7612
7613 /* In case of feeze action, if local neigh is in duplicate state,
7614 * Mark the Neigh as inactive before sending delete request to BGPd,
7615 * If BGPd has remote entry, it will re-install
7616 */
7617 if (zvrf->dad_freeze &&
7618 CHECK_FLAG(n->flags, ZEBRA_NEIGH_DUPLICATE))
7619 ZEBRA_NEIGH_SET_INACTIVE(n);
7620
7621 /* Remove neighbor from BGP. */
7622 zvni_neigh_send_del_to_client(zvni->vni, &n->ip, &n->emac, 0, n->state);
7623
7624 /* Delete this neighbor entry. */
7625 zvni_neigh_del(zvni, n);
7626
7627 /* see if the AUTO mac needs to be deleted */
7628 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_AUTO)
7629 && !listcount(zmac->neigh_list))
7630 zvni_mac_del(zvni, zmac);
7631
7632 return 0;
7633 }
7634
7635 /*
7636 * Handle neighbor add or update notification from the kernel (on a VLAN
7637 * device / L3 interface). This is typically for a local neighbor but can
7638 * also be for a remote neighbor (e.g., ageout notification). It could
7639 * also be a "move" scenario.
7640 */
7641 int zebra_vxlan_handle_kernel_neigh_update(struct interface *ifp,
7642 struct interface *link_if,
7643 struct ipaddr *ip,
7644 struct ethaddr *macaddr,
7645 uint16_t state,
7646 bool is_ext,
7647 bool is_router)
7648 {
7649 char buf[ETHER_ADDR_STRLEN];
7650 char buf2[INET6_ADDRSTRLEN];
7651 zebra_vni_t *zvni = NULL;
7652 zebra_l3vni_t *zl3vni = NULL;
7653
7654 /* check if this is a remote neigh entry corresponding to remote
7655 * next-hop
7656 */
7657 zl3vni = zl3vni_from_svi(ifp, link_if);
7658 if (zl3vni)
7659 return zl3vni_local_nh_add_update(zl3vni, ip, state);
7660
7661 /* We are only interested in neighbors on an SVI that resides on top
7662 * of a VxLAN bridge.
7663 */
7664 zvni = zvni_from_svi(ifp, link_if);
7665 if (!zvni)
7666 return 0;
7667
7668 if (IS_ZEBRA_DEBUG_VXLAN)
7669 zlog_debug(
7670 "Add/Update neighbor %s MAC %s intf %s(%u) state 0x%x %s %s-> L2-VNI %u",
7671 ipaddr2str(ip, buf2, sizeof(buf2)),
7672 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
7673 ifp->ifindex, state, is_ext ? "ext-learned " : "",
7674 is_router ? "router " : "",
7675 zvni->vni);
7676
7677 /* Is this about a local neighbor or a remote one? */
7678 if (!is_ext)
7679 return zvni_local_neigh_update(zvni, ifp, ip, macaddr,
7680 is_router);
7681
7682 return zvni_remote_neigh_update(zvni, ifp, ip, macaddr, state);
7683 }
7684
7685 static int32_t
7686 zebra_vxlan_remote_macip_helper(bool add, struct stream *s, vni_t *vni,
7687 struct ethaddr *macaddr, uint16_t *ipa_len,
7688 struct ipaddr *ip, struct in_addr *vtep_ip,
7689 uint8_t *flags, uint32_t *seq)
7690 {
7691 uint16_t l = 0;
7692
7693 /*
7694 * Obtain each remote MACIP and process.
7695 * Message contains VNI, followed by MAC followed by IP (if any)
7696 * followed by remote VTEP IP.
7697 */
7698 memset(ip, 0, sizeof(*ip));
7699 STREAM_GETL(s, *vni);
7700 STREAM_GET(macaddr->octet, s, ETH_ALEN);
7701 STREAM_GETL(s, *ipa_len);
7702
7703 if (*ipa_len) {
7704 if (*ipa_len == IPV4_MAX_BYTELEN)
7705 ip->ipa_type = IPADDR_V4;
7706 else if (*ipa_len == IPV6_MAX_BYTELEN)
7707 ip->ipa_type = IPADDR_V6;
7708 else {
7709 if (IS_ZEBRA_DEBUG_VXLAN)
7710 zlog_debug(
7711 "ipa_len *must* be %d or %d bytes in length not %d",
7712 IPV4_MAX_BYTELEN, IPV6_MAX_BYTELEN,
7713 *ipa_len);
7714 goto stream_failure;
7715 }
7716
7717 STREAM_GET(&ip->ip.addr, s, *ipa_len);
7718 }
7719 l += 4 + ETH_ALEN + 4 + *ipa_len;
7720 STREAM_GET(&vtep_ip->s_addr, s, IPV4_MAX_BYTELEN);
7721 l += IPV4_MAX_BYTELEN;
7722
7723 if (add) {
7724 STREAM_GETC(s, *flags);
7725 STREAM_GETL(s, *seq);
7726 l += 5;
7727 }
7728
7729 return l;
7730
7731 stream_failure:
7732 return -1;
7733 }
7734
7735 /*
7736 * Handle message from client to delete a remote MACIP for a VNI.
7737 */
7738 void zebra_vxlan_remote_macip_del(ZAPI_HANDLER_ARGS)
7739 {
7740 struct stream *s;
7741 vni_t vni;
7742 struct ethaddr macaddr;
7743 struct ipaddr ip;
7744 struct in_addr vtep_ip;
7745 uint16_t l = 0, ipa_len;
7746 char buf[ETHER_ADDR_STRLEN];
7747 char buf1[INET6_ADDRSTRLEN];
7748
7749 memset(&macaddr, 0, sizeof(struct ethaddr));
7750 memset(&ip, 0, sizeof(struct ipaddr));
7751 memset(&vtep_ip, 0, sizeof(struct in_addr));
7752
7753 s = msg;
7754
7755 while (l < hdr->length) {
7756 int res_length = zebra_vxlan_remote_macip_helper(
7757 false, s, &vni, &macaddr, &ipa_len, &ip, &vtep_ip, NULL,
7758 NULL);
7759
7760 if (res_length == -1)
7761 goto stream_failure;
7762
7763 l += res_length;
7764 if (IS_ZEBRA_DEBUG_VXLAN)
7765 zlog_debug(
7766 "Recv MACIP DEL VNI %u MAC %s%s%s Remote VTEP %s from %s",
7767 vni,
7768 prefix_mac2str(&macaddr, buf, sizeof(buf)),
7769 ipa_len ? " IP " : "",
7770 ipa_len ?
7771 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
7772 inet_ntoa(vtep_ip),
7773 zebra_route_string(client->proto));
7774
7775 process_remote_macip_del(vni, &macaddr, ipa_len, &ip, vtep_ip);
7776 }
7777
7778 stream_failure:
7779 return;
7780 }
7781
7782 /*
7783 * Handle message from client to add a remote MACIP for a VNI. This
7784 * could be just the add of a MAC address or the add of a neighbor
7785 * (IP+MAC).
7786 */
7787 void zebra_vxlan_remote_macip_add(ZAPI_HANDLER_ARGS)
7788 {
7789 struct stream *s;
7790 vni_t vni;
7791 struct ethaddr macaddr;
7792 struct ipaddr ip;
7793 struct in_addr vtep_ip;
7794 uint16_t l = 0, ipa_len;
7795 uint8_t flags = 0;
7796 uint32_t seq;
7797 char buf[ETHER_ADDR_STRLEN];
7798 char buf1[INET6_ADDRSTRLEN];
7799
7800 memset(&macaddr, 0, sizeof(struct ethaddr));
7801 memset(&ip, 0, sizeof(struct ipaddr));
7802 memset(&vtep_ip, 0, sizeof(struct in_addr));
7803
7804 if (!EVPN_ENABLED(zvrf)) {
7805 zlog_debug("EVPN not enabled, ignoring remote MACIP ADD");
7806 return;
7807 }
7808
7809 s = msg;
7810
7811 while (l < hdr->length) {
7812 int res_length = zebra_vxlan_remote_macip_helper(
7813 true, s, &vni, &macaddr, &ipa_len, &ip, &vtep_ip,
7814 &flags, &seq);
7815
7816 if (res_length == -1)
7817 goto stream_failure;
7818
7819 l += res_length;
7820 if (IS_ZEBRA_DEBUG_VXLAN)
7821 zlog_debug(
7822 "Recv MACIP ADD VNI %u MAC %s%s%s flags 0x%x seq %u VTEP %s from %s",
7823 vni,
7824 prefix_mac2str(&macaddr, buf, sizeof(buf)),
7825 ipa_len ? " IP " : "",
7826 ipa_len ?
7827 ipaddr2str(&ip, buf1, sizeof(buf1)) : "",
7828 flags, seq, inet_ntoa(vtep_ip),
7829 zebra_route_string(client->proto));
7830
7831 process_remote_macip_add(vni, &macaddr, ipa_len, &ip,
7832 flags, seq, vtep_ip);
7833 }
7834
7835 stream_failure:
7836 return;
7837 }
7838
7839 /*
7840 * Handle notification of MAC add/update over VxLAN. If the kernel is notifying
7841 * us, this must involve a multihoming scenario. Treat this as implicit delete
7842 * of any prior local MAC.
7843 */
7844 int zebra_vxlan_check_del_local_mac(struct interface *ifp,
7845 struct interface *br_if,
7846 struct ethaddr *macaddr, vlanid_t vid)
7847 {
7848 struct zebra_if *zif;
7849 struct zebra_l2info_vxlan *vxl;
7850 vni_t vni;
7851 zebra_vni_t *zvni;
7852 zebra_mac_t *mac;
7853 char buf[ETHER_ADDR_STRLEN];
7854
7855 zif = ifp->info;
7856 assert(zif);
7857 vxl = &zif->l2info.vxl;
7858 vni = vxl->vni;
7859
7860 /* Check if EVPN is enabled. */
7861 if (!is_evpn_enabled())
7862 return 0;
7863
7864 /* Locate hash entry; it is expected to exist. */
7865 zvni = zvni_lookup(vni);
7866 if (!zvni)
7867 return 0;
7868
7869 /* If entry doesn't exist, nothing to do. */
7870 mac = zvni_mac_lookup(zvni, macaddr);
7871 if (!mac)
7872 return 0;
7873
7874 /* Is it a local entry? */
7875 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
7876 return 0;
7877
7878 if (IS_ZEBRA_DEBUG_VXLAN)
7879 zlog_debug(
7880 "Add/update remote MAC %s intf %s(%u) VNI %u flags 0x%x - del local",
7881 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
7882 ifp->ifindex, vni, mac->flags);
7883
7884 /* Remove MAC from BGP. */
7885 zvni_mac_send_del_to_client(zvni->vni, macaddr);
7886
7887 /*
7888 * If there are no neigh associated with the mac delete the mac
7889 * else mark it as AUTO for forward reference
7890 */
7891 if (!listcount(mac->neigh_list)) {
7892 zvni_mac_del(zvni, mac);
7893 } else {
7894 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
7895 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
7896 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
7897 }
7898
7899 return 0;
7900 }
7901
7902 /*
7903 * Handle remote MAC delete by kernel; readd the remote MAC if we have it.
7904 * This can happen because the remote MAC entries are also added as "dynamic",
7905 * so the kernel can ageout the entry.
7906 */
7907 int zebra_vxlan_check_readd_remote_mac(struct interface *ifp,
7908 struct interface *br_if,
7909 struct ethaddr *macaddr, vlanid_t vid)
7910 {
7911 struct zebra_if *zif = NULL;
7912 struct zebra_l2info_vxlan *vxl = NULL;
7913 vni_t vni;
7914 zebra_vni_t *zvni = NULL;
7915 zebra_l3vni_t *zl3vni = NULL;
7916 zebra_mac_t *mac = NULL;
7917 char buf[ETHER_ADDR_STRLEN];
7918
7919 zif = ifp->info;
7920 assert(zif);
7921 vxl = &zif->l2info.vxl;
7922 vni = vxl->vni;
7923
7924 /* Check if EVPN is enabled. */
7925 if (!is_evpn_enabled())
7926 return 0;
7927
7928 /* check if this is a remote RMAC and readd simillar to remote macs */
7929 zl3vni = zl3vni_lookup(vni);
7930 if (zl3vni)
7931 return zebra_vxlan_readd_remote_rmac(zl3vni, macaddr);
7932
7933 /* Locate hash entry; it is expected to exist. */
7934 zvni = zvni_lookup(vni);
7935 if (!zvni)
7936 return 0;
7937
7938 /* If entry doesn't exist, nothing to do. */
7939 mac = zvni_mac_lookup(zvni, macaddr);
7940 if (!mac)
7941 return 0;
7942
7943 /* Is it a remote entry? */
7944 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
7945 return 0;
7946
7947 if (IS_ZEBRA_DEBUG_VXLAN)
7948 zlog_debug("Del remote MAC %s intf %s(%u) VNI %u - readd",
7949 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
7950 ifp->ifindex, vni);
7951
7952 zvni_mac_install(zvni, mac);
7953 return 0;
7954 }
7955
7956 /*
7957 * Handle local MAC delete (on a port or VLAN corresponding to this VNI).
7958 */
7959 int zebra_vxlan_local_mac_del(struct interface *ifp, struct interface *br_if,
7960 struct ethaddr *macaddr, vlanid_t vid)
7961 {
7962 zebra_vni_t *zvni;
7963 zebra_mac_t *mac;
7964 char buf[ETHER_ADDR_STRLEN];
7965
7966 /* We are interested in MACs only on ports or (port, VLAN) that
7967 * map to a VNI.
7968 */
7969 zvni = zvni_map_vlan(ifp, br_if, vid);
7970 if (!zvni)
7971 return 0;
7972 if (!zvni->vxlan_if) {
7973 zlog_debug(
7974 "VNI %u hash %p doesn't have intf upon local MAC DEL",
7975 zvni->vni, zvni);
7976 return -1;
7977 }
7978
7979 /* If entry doesn't exist, nothing to do. */
7980 mac = zvni_mac_lookup(zvni, macaddr);
7981 if (!mac)
7982 return 0;
7983
7984 /* Is it a local entry? */
7985 if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
7986 return 0;
7987
7988 if (IS_ZEBRA_DEBUG_VXLAN)
7989 zlog_debug("DEL MAC %s intf %s(%u) VID %u -> VNI %u seq %u flags 0x%x nbr count %u",
7990 prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name,
7991 ifp->ifindex, vid, zvni->vni, mac->loc_seq,
7992 mac->flags, listcount(mac->neigh_list));
7993
7994 /* Update all the neigh entries associated with this mac */
7995 zvni_process_neigh_on_local_mac_del(zvni, mac);
7996
7997 /* Remove MAC from BGP. */
7998 zvni_mac_send_del_to_client(zvni->vni, macaddr);
7999
8000 /*
8001 * If there are no neigh associated with the mac delete the mac
8002 * else mark it as AUTO for forward reference
8003 */
8004 if (!listcount(mac->neigh_list)) {
8005 zvni_mac_del(zvni, mac);
8006 } else {
8007 UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
8008 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
8009 SET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
8010 }
8011
8012 return 0;
8013 }
8014
8015 /*
8016 * Handle local MAC add (on a port or VLAN corresponding to this VNI).
8017 */
8018 int zebra_vxlan_local_mac_add_update(struct interface *ifp,
8019 struct interface *br_if,
8020 struct ethaddr *macaddr, vlanid_t vid,
8021 bool sticky)
8022 {
8023 zebra_vni_t *zvni;
8024 zebra_mac_t *mac;
8025 struct zebra_vrf *zvrf;
8026 char buf[ETHER_ADDR_STRLEN];
8027 bool mac_sticky = false;
8028 bool inform_client = false;
8029 bool upd_neigh = false;
8030 bool is_dup_detect = false;
8031 struct in_addr vtep_ip = {.s_addr = 0};
8032
8033 /* We are interested in MACs only on ports or (port, VLAN) that
8034 * map to a VNI.
8035 */
8036 zvni = zvni_map_vlan(ifp, br_if, vid);
8037 if (!zvni) {
8038 if (IS_ZEBRA_DEBUG_VXLAN)
8039 zlog_debug(
8040 "\tAdd/Update %sMAC %s intf %s(%u) VID %u, could not find VNI",
8041 sticky ? "sticky " : "",
8042 prefix_mac2str(macaddr, buf, sizeof(buf)),
8043 ifp->name, ifp->ifindex, vid);
8044 return 0;
8045 }
8046
8047 if (!zvni->vxlan_if) {
8048 if (IS_ZEBRA_DEBUG_VXLAN)
8049 zlog_debug(
8050 "\tVNI %u hash %p doesn't have intf upon local MAC ADD",
8051 zvni->vni, zvni);
8052 return -1;
8053 }
8054
8055 zvrf = vrf_info_lookup(zvni->vxlan_if->vrf_id);
8056 if (!zvrf) {
8057 if (IS_ZEBRA_DEBUG_VXLAN)
8058 zlog_debug("\tNo Vrf found for vrf_id: %d",
8059 zvni->vxlan_if->vrf_id);
8060 return -1;
8061 }
8062
8063 /* Check if we need to create or update or it is a NO-OP. */
8064 mac = zvni_mac_lookup(zvni, macaddr);
8065 if (!mac) {
8066 if (IS_ZEBRA_DEBUG_VXLAN)
8067 zlog_debug(
8068 "ADD %sMAC %s intf %s(%u) VID %u -> VNI %u",
8069 sticky ? "sticky " : "",
8070 prefix_mac2str(macaddr, buf, sizeof(buf)),
8071 ifp->name, ifp->ifindex, vid, zvni->vni);
8072
8073 mac = zvni_mac_add(zvni, macaddr);
8074 if (!mac) {
8075 flog_err(
8076 EC_ZEBRA_MAC_ADD_FAILED,
8077 "Failed to add MAC %s intf %s(%u) VID %u VNI %u",
8078 prefix_mac2str(macaddr, buf, sizeof(buf)),
8079 ifp->name, ifp->ifindex, vid, zvni->vni);
8080 return -1;
8081 }
8082 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
8083 mac->fwd_info.local.ifindex = ifp->ifindex;
8084 mac->fwd_info.local.vid = vid;
8085 if (sticky)
8086 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
8087 inform_client = true;
8088
8089 } else {
8090 if (IS_ZEBRA_DEBUG_VXLAN)
8091 zlog_debug(
8092 "UPD %sMAC %s intf %s(%u) VID %u -> VNI %u curFlags 0x%x",
8093 sticky ? "sticky " : "",
8094 prefix_mac2str(macaddr, buf, sizeof(buf)),
8095 ifp->name, ifp->ifindex, vid, zvni->vni,
8096 mac->flags);
8097
8098 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
8099 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY))
8100 mac_sticky = true;
8101
8102 /*
8103 * Update any changes and if changes are relevant to
8104 * BGP, note it.
8105 */
8106 if (mac_sticky == sticky
8107 && mac->fwd_info.local.ifindex == ifp->ifindex
8108 && mac->fwd_info.local.vid == vid) {
8109 if (IS_ZEBRA_DEBUG_VXLAN)
8110 zlog_debug(
8111 "\tAdd/Update %sMAC %s intf %s(%u) VID %u -> VNI %u, "
8112 "entry exists and has not changed ",
8113 sticky ? "sticky " : "",
8114 prefix_mac2str(macaddr, buf,
8115 sizeof(buf)),
8116 ifp->name, ifp->ifindex, vid,
8117 zvni->vni);
8118 return 0;
8119 }
8120 if (mac_sticky != sticky) {
8121 if (sticky)
8122 SET_FLAG(mac->flags,
8123 ZEBRA_MAC_STICKY);
8124 else
8125 UNSET_FLAG(mac->flags,
8126 ZEBRA_MAC_STICKY);
8127 inform_client = true;
8128 }
8129
8130 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
8131 mac->fwd_info.local.ifindex = ifp->ifindex;
8132 mac->fwd_info.local.vid = vid;
8133
8134 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) ||
8135 CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) {
8136 bool do_dad = false;
8137
8138 /*
8139 * MAC has either moved or was "internally" created due
8140 * to a neighbor learn and is now actually learnt. If
8141 * it was learnt as a remote sticky MAC, this is an
8142 * operator error.
8143 */
8144 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)) {
8145 flog_warn(
8146 EC_ZEBRA_STICKY_MAC_ALREADY_LEARNT,
8147 "MAC %s already learnt as remote sticky MAC behind VTEP %s VNI %u",
8148 prefix_mac2str(macaddr, buf,
8149 sizeof(buf)),
8150 inet_ntoa(mac->fwd_info.r_vtep_ip),
8151 zvni->vni);
8152 return 0;
8153 }
8154
8155 /* If an actual move, compute MAC's seq number */
8156 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
8157 mac->loc_seq = MAX(mac->rem_seq + 1,
8158 mac->loc_seq);
8159 vtep_ip = mac->fwd_info.r_vtep_ip;
8160 /* Trigger DAD for remote MAC */
8161 do_dad = true;
8162 }
8163
8164 UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE);
8165 UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO);
8166 SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL);
8167 memset(&mac->fwd_info, 0, sizeof(mac->fwd_info));
8168 mac->fwd_info.local.ifindex = ifp->ifindex;
8169 mac->fwd_info.local.vid = vid;
8170 if (sticky)
8171 SET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
8172 else
8173 UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY);
8174 /*
8175 * We have to inform BGP of this MAC as well as process
8176 * all neighbors.
8177 */
8178 inform_client = true;
8179 upd_neigh = true;
8180
8181 zebra_vxlan_dup_addr_detect_for_mac(zvrf, mac, vtep_ip,
8182 do_dad,
8183 &is_dup_detect,
8184 true);
8185 if (is_dup_detect) {
8186 inform_client = false;
8187 upd_neigh = false;
8188 }
8189 }
8190 }
8191
8192 /* Inform BGP if required. */
8193 if (inform_client) {
8194 if (zvni_mac_send_add_to_client(zvni->vni, macaddr,
8195 mac->flags, mac->loc_seq))
8196 return -1;
8197 }
8198
8199 /* Process all neighbors associated with this MAC, if required. */
8200 if (upd_neigh)
8201 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
8202
8203 return 0;
8204 }
8205
8206 /*
8207 * Handle message from client to delete a remote VTEP for a VNI.
8208 */
8209 void zebra_vxlan_remote_vtep_del(ZAPI_HANDLER_ARGS)
8210 {
8211 struct stream *s;
8212 unsigned short l = 0;
8213 vni_t vni;
8214 struct in_addr vtep_ip;
8215 zebra_vni_t *zvni;
8216 zebra_vtep_t *zvtep;
8217 struct interface *ifp;
8218 struct zebra_if *zif;
8219
8220 if (!is_evpn_enabled()) {
8221 zlog_debug(
8222 "%s: EVPN is not enabled yet we have received a vtep del command",
8223 __PRETTY_FUNCTION__);
8224 return;
8225 }
8226
8227 if (!EVPN_ENABLED(zvrf)) {
8228 zlog_debug("Recv MACIP DEL for non-EVPN VRF %u",
8229 zvrf_id(zvrf));
8230 return;
8231 }
8232
8233 s = msg;
8234
8235 while (l < hdr->length) {
8236 int flood_control __attribute__((unused));
8237
8238 /* Obtain each remote VTEP and process. */
8239 STREAM_GETL(s, vni);
8240 l += 4;
8241 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
8242 l += IPV4_MAX_BYTELEN;
8243
8244 /* Flood control is intentionally ignored right now */
8245 STREAM_GETL(s, flood_control);
8246 l += 4;
8247
8248 if (IS_ZEBRA_DEBUG_VXLAN)
8249 zlog_debug("Recv VTEP_DEL %s VNI %u from %s",
8250 inet_ntoa(vtep_ip), vni,
8251 zebra_route_string(client->proto));
8252
8253 /* Locate VNI hash entry - expected to exist. */
8254 zvni = zvni_lookup(vni);
8255 if (!zvni) {
8256 if (IS_ZEBRA_DEBUG_VXLAN)
8257 zlog_debug(
8258 "Failed to locate VNI hash upon remote VTEP DEL, "
8259 "VNI %u",
8260 vni);
8261 continue;
8262 }
8263
8264 ifp = zvni->vxlan_if;
8265 if (!ifp) {
8266 zlog_debug(
8267 "VNI %u hash %p doesn't have intf upon remote VTEP DEL",
8268 zvni->vni, zvni);
8269 continue;
8270 }
8271 zif = ifp->info;
8272
8273 /* If down or not mapped to a bridge, we're done. */
8274 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8275 continue;
8276
8277 /* If the remote VTEP does not exist, there's nothing more to
8278 * do.
8279 * Otherwise, uninstall any remote MACs pointing to this VTEP
8280 * and
8281 * then, the VTEP entry itself and remove it.
8282 */
8283 zvtep = zvni_vtep_find(zvni, &vtep_ip);
8284 if (!zvtep)
8285 continue;
8286
8287 zvni_neigh_del_from_vtep(zvni, 1, &vtep_ip);
8288 zvni_mac_del_from_vtep(zvni, 1, &vtep_ip);
8289 zvni_vtep_uninstall(zvni, &vtep_ip);
8290 zvni_vtep_del(zvni, zvtep);
8291 }
8292
8293 stream_failure:
8294 return;
8295 }
8296
8297 /*
8298 * Handle message from client to add a remote VTEP for a VNI.
8299 */
8300 void zebra_vxlan_remote_vtep_add(ZAPI_HANDLER_ARGS)
8301 {
8302 struct stream *s;
8303 unsigned short l = 0;
8304 vni_t vni;
8305 struct in_addr vtep_ip;
8306 zebra_vni_t *zvni;
8307 struct interface *ifp;
8308 struct zebra_if *zif;
8309 int flood_control;
8310 zebra_vtep_t *zvtep;
8311
8312 if (!is_evpn_enabled()) {
8313 zlog_debug(
8314 "%s: EVPN not enabled yet we received a vtep_add zapi call",
8315 __PRETTY_FUNCTION__);
8316 return;
8317 }
8318
8319 if (!EVPN_ENABLED(zvrf)) {
8320 zlog_debug("Recv MACIP ADD for non-EVPN VRF %u",
8321 zvrf_id(zvrf));
8322 return;
8323 }
8324
8325 s = msg;
8326
8327 while (l < hdr->length) {
8328 /* Obtain each remote VTEP and process. */
8329 STREAM_GETL(s, vni);
8330 l += 4;
8331 STREAM_GET(&vtep_ip.s_addr, s, IPV4_MAX_BYTELEN);
8332 STREAM_GETL(s, flood_control);
8333 l += IPV4_MAX_BYTELEN + 4;
8334
8335 if (IS_ZEBRA_DEBUG_VXLAN)
8336 zlog_debug("Recv VTEP_ADD %s VNI %u flood %d from %s",
8337 inet_ntoa(vtep_ip), vni, flood_control,
8338 zebra_route_string(client->proto));
8339
8340 /* Locate VNI hash entry - expected to exist. */
8341 zvni = zvni_lookup(vni);
8342 if (!zvni) {
8343 flog_err(
8344 EC_ZEBRA_VTEP_ADD_FAILED,
8345 "Failed to locate VNI hash upon remote VTEP ADD, VNI %u",
8346 vni);
8347 continue;
8348 }
8349
8350 ifp = zvni->vxlan_if;
8351 if (!ifp) {
8352 flog_err(
8353 EC_ZEBRA_VTEP_ADD_FAILED,
8354 "VNI %u hash %p doesn't have intf upon remote VTEP ADD",
8355 zvni->vni, zvni);
8356 continue;
8357 }
8358
8359 zif = ifp->info;
8360
8361 /* If down or not mapped to a bridge, we're done. */
8362 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
8363 continue;
8364
8365 zvtep = zvni_vtep_find(zvni, &vtep_ip);
8366 if (zvtep) {
8367 /* If the remote VTEP already exists check if
8368 * the flood mode has changed
8369 */
8370 if (zvtep->flood_control != flood_control) {
8371 if (zvtep->flood_control
8372 == VXLAN_FLOOD_DISABLED)
8373 /* old mode was head-end-replication but
8374 * is no longer; get rid of the HER fdb
8375 * entry installed before
8376 */
8377 zvni_vtep_uninstall(zvni, &vtep_ip);
8378 zvtep->flood_control = flood_control;
8379 zvni_vtep_install(zvni, zvtep);
8380 }
8381 } else {
8382 zvtep = zvni_vtep_add(zvni, &vtep_ip, flood_control);
8383 if (zvtep)
8384 zvni_vtep_install(zvni, zvtep);
8385 else
8386 flog_err(EC_ZEBRA_VTEP_ADD_FAILED,
8387 "Failed to add remote VTEP, VNI %u zvni %p",
8388 vni, zvni);
8389 }
8390 }
8391
8392 stream_failure:
8393 return;
8394 }
8395
8396 /*
8397 * Add/Del gateway macip to evpn
8398 * g/w can be:
8399 * 1. SVI interface on a vlan aware bridge
8400 * 2. SVI interface on a vlan unaware bridge
8401 * 3. vrr interface (MACVLAN) associated to a SVI
8402 * We advertise macip routes for an interface if it is associated to VxLan vlan
8403 */
8404 int zebra_vxlan_add_del_gw_macip(struct interface *ifp, struct prefix *p,
8405 int add)
8406 {
8407 struct ipaddr ip;
8408 struct ethaddr macaddr;
8409 zebra_vni_t *zvni = NULL;
8410
8411 memset(&ip, 0, sizeof(struct ipaddr));
8412 memset(&macaddr, 0, sizeof(struct ethaddr));
8413
8414 /* Check if EVPN is enabled. */
8415 if (!is_evpn_enabled())
8416 return 0;
8417
8418 if (IS_ZEBRA_IF_MACVLAN(ifp)) {
8419 struct interface *svi_if =
8420 NULL; /* SVI corresponding to the MACVLAN */
8421 struct zebra_if *ifp_zif =
8422 NULL; /* Zebra daemon specific info for MACVLAN */
8423 struct zebra_if *svi_if_zif =
8424 NULL; /* Zebra daemon specific info for SVI*/
8425
8426 ifp_zif = ifp->info;
8427 if (!ifp_zif)
8428 return -1;
8429
8430 /*
8431 * for a MACVLAN interface the link represents the svi_if
8432 */
8433 svi_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
8434 ifp_zif->link_ifindex);
8435 if (!svi_if) {
8436 zlog_debug("MACVLAN %s(%u) without link information",
8437 ifp->name, ifp->ifindex);
8438 return -1;
8439 }
8440
8441 if (IS_ZEBRA_IF_VLAN(svi_if)) {
8442 /*
8443 * If it is a vlan aware bridge then the link gives the
8444 * bridge information
8445 */
8446 struct interface *svi_if_link = NULL;
8447
8448 svi_if_zif = svi_if->info;
8449 if (svi_if_zif) {
8450 svi_if_link = if_lookup_by_index_per_ns(
8451 zebra_ns_lookup(NS_DEFAULT),
8452 svi_if_zif->link_ifindex);
8453 zvni = zvni_from_svi(svi_if, svi_if_link);
8454 }
8455 } else if (IS_ZEBRA_IF_BRIDGE(svi_if)) {
8456 /*
8457 * If it is a vlan unaware bridge then svi is the bridge
8458 * itself
8459 */
8460 zvni = zvni_from_svi(svi_if, svi_if);
8461 }
8462 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
8463 struct zebra_if *svi_if_zif =
8464 NULL; /* Zebra daemon specific info for SVI */
8465 struct interface *svi_if_link =
8466 NULL; /* link info for the SVI = bridge info */
8467
8468 svi_if_zif = ifp->info;
8469 if (svi_if_zif) {
8470 svi_if_link = if_lookup_by_index_per_ns(
8471 zebra_ns_lookup(NS_DEFAULT),
8472 svi_if_zif->link_ifindex);
8473 if (svi_if_link)
8474 zvni = zvni_from_svi(ifp, svi_if_link);
8475 }
8476 } else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
8477 zvni = zvni_from_svi(ifp, ifp);
8478 }
8479
8480 if (!zvni)
8481 return 0;
8482
8483 if (!zvni->vxlan_if) {
8484 zlog_debug("VNI %u hash %p doesn't have intf upon MACVLAN up",
8485 zvni->vni, zvni);
8486 return -1;
8487 }
8488
8489
8490 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
8491
8492 if (p->family == AF_INET) {
8493 ip.ipa_type = IPADDR_V4;
8494 memcpy(&(ip.ipaddr_v4), &(p->u.prefix4),
8495 sizeof(struct in_addr));
8496 } else if (p->family == AF_INET6) {
8497 ip.ipa_type = IPADDR_V6;
8498 memcpy(&(ip.ipaddr_v6), &(p->u.prefix6),
8499 sizeof(struct in6_addr));
8500 }
8501
8502
8503 if (add)
8504 zvni_gw_macip_add(ifp, zvni, &macaddr, &ip);
8505 else
8506 zvni_gw_macip_del(ifp, zvni, &ip);
8507
8508 return 0;
8509 }
8510
8511 /*
8512 * Handle SVI interface going down.
8513 * SVI can be associated to either L3-VNI or L2-VNI.
8514 * For L2-VNI: At this point, this is a NOP since
8515 * the kernel deletes the neighbor entries on this SVI (if any).
8516 * We only need to update the vrf corresponding to zvni.
8517 * For L3-VNI: L3-VNI is operationally down, update mac-ip routes and delete
8518 * from bgp
8519 */
8520 int zebra_vxlan_svi_down(struct interface *ifp, struct interface *link_if)
8521 {
8522 zebra_l3vni_t *zl3vni = NULL;
8523
8524 zl3vni = zl3vni_from_svi(ifp, link_if);
8525 if (zl3vni) {
8526
8527 /* process l3-vni down */
8528 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8529
8530 /* remove association with svi-if */
8531 zl3vni->svi_if = NULL;
8532 } else {
8533 zebra_vni_t *zvni = NULL;
8534
8535 /* since we dont have svi corresponding to zvni, we associate it
8536 * to default vrf. Note: the corresponding neigh entries on the
8537 * SVI would have already been deleted */
8538 zvni = zvni_from_svi(ifp, link_if);
8539 if (zvni) {
8540 zvni->vrf_id = VRF_DEFAULT;
8541
8542 /* update the tenant vrf in BGP */
8543 zvni_send_add_to_client(zvni);
8544 }
8545 }
8546 return 0;
8547 }
8548
8549 /*
8550 * Handle SVI interface coming up.
8551 * SVI can be associated to L3-VNI (l3vni vxlan interface) or L2-VNI (l2-vni
8552 * vxlan intf).
8553 * For L2-VNI: we need to install any remote neighbors entried (used for
8554 * apr-suppression)
8555 * For L3-VNI: SVI will be used to get the rmac to be used with L3-VNI
8556 */
8557 int zebra_vxlan_svi_up(struct interface *ifp, struct interface *link_if)
8558 {
8559 zebra_vni_t *zvni = NULL;
8560 zebra_l3vni_t *zl3vni = NULL;
8561
8562 zl3vni = zl3vni_from_svi(ifp, link_if);
8563 if (zl3vni) {
8564
8565 /* associate with svi */
8566 zl3vni->svi_if = ifp;
8567
8568 /* process oper-up */
8569 if (is_l3vni_oper_up(zl3vni))
8570 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8571 } else {
8572
8573 /* process SVI up for l2-vni */
8574 struct neigh_walk_ctx n_wctx;
8575
8576 zvni = zvni_from_svi(ifp, link_if);
8577 if (!zvni)
8578 return 0;
8579
8580 if (!zvni->vxlan_if) {
8581 zlog_debug(
8582 "VNI %u hash %p doesn't have intf upon SVI up",
8583 zvni->vni, zvni);
8584 return -1;
8585 }
8586
8587 if (IS_ZEBRA_DEBUG_VXLAN)
8588 zlog_debug(
8589 "SVI %s(%u) VNI %u VRF %s is UP, installing neighbors",
8590 ifp->name, ifp->ifindex, zvni->vni,
8591 vrf_id_to_name(ifp->vrf_id));
8592
8593 /* update the vrf information for l2-vni and inform bgp */
8594 zvni->vrf_id = ifp->vrf_id;
8595 zvni_send_add_to_client(zvni);
8596
8597 /* Install any remote neighbors for this VNI. */
8598 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
8599 n_wctx.zvni = zvni;
8600 hash_iterate(zvni->neigh_table, zvni_install_neigh_hash,
8601 &n_wctx);
8602 }
8603
8604 return 0;
8605 }
8606
8607 /*
8608 * Handle MAC-VLAN interface going down.
8609 * L3VNI: When MAC-VLAN interface goes down,
8610 * find its associated SVI and update type2/type-5 routes
8611 * with SVI as RMAC
8612 */
8613 void zebra_vxlan_macvlan_down(struct interface *ifp)
8614 {
8615 zebra_l3vni_t *zl3vni = NULL;
8616 struct zebra_if *zif, *link_zif;
8617 struct interface *link_ifp, *link_if;
8618
8619 zif = ifp->info;
8620 assert(zif);
8621 link_ifp = zif->link;
8622 if (!link_ifp) {
8623 if (IS_ZEBRA_DEBUG_VXLAN) {
8624 struct interface *ifp;
8625
8626 ifp = if_lookup_by_index_all_vrf(zif->link_ifindex);
8627 zlog_debug("macvlan parent link is not found. Parent index %d ifp %s",
8628 zif->link_ifindex, ifp ? ifp->name : " ");
8629 }
8630 return;
8631 }
8632 link_zif = link_ifp->info;
8633 assert(link_zif);
8634
8635 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
8636 link_zif->link_ifindex);
8637
8638 zl3vni = zl3vni_from_svi(link_ifp, link_if);
8639 if (zl3vni) {
8640 zl3vni->mac_vlan_if = NULL;
8641 if (is_l3vni_oper_up(zl3vni))
8642 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8643 }
8644 }
8645
8646 /*
8647 * Handle MAC-VLAN interface going up.
8648 * L3VNI: When MAC-VLAN interface comes up,
8649 * find its associated SVI and update type-2 routes
8650 * with MAC-VLAN's MAC as RMAC and for type-5 routes
8651 * use SVI's MAC as RMAC.
8652 */
8653 void zebra_vxlan_macvlan_up(struct interface *ifp)
8654 {
8655 zebra_l3vni_t *zl3vni = NULL;
8656 struct zebra_if *zif, *link_zif;
8657 struct interface *link_ifp, *link_if;
8658
8659 zif = ifp->info;
8660 assert(zif);
8661 link_ifp = zif->link;
8662 link_zif = link_ifp->info;
8663 assert(link_zif);
8664
8665 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
8666 link_zif->link_ifindex);
8667 zl3vni = zl3vni_from_svi(link_ifp, link_if);
8668 if (zl3vni) {
8669 /* associate with macvlan (VRR) interface */
8670 zl3vni->mac_vlan_if = ifp;
8671
8672 /* process oper-up */
8673 if (is_l3vni_oper_up(zl3vni))
8674 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8675 }
8676 }
8677
8678 /*
8679 * Handle VxLAN interface down
8680 */
8681 int zebra_vxlan_if_down(struct interface *ifp)
8682 {
8683 vni_t vni;
8684 struct zebra_if *zif = NULL;
8685 struct zebra_l2info_vxlan *vxl = NULL;
8686 zebra_l3vni_t *zl3vni = NULL;
8687 zebra_vni_t *zvni;
8688
8689 /* Check if EVPN is enabled. */
8690 if (!is_evpn_enabled())
8691 return 0;
8692
8693 zif = ifp->info;
8694 assert(zif);
8695 vxl = &zif->l2info.vxl;
8696 vni = vxl->vni;
8697
8698 zl3vni = zl3vni_lookup(vni);
8699 if (zl3vni) {
8700 /* process-if-down for l3-vni */
8701 if (IS_ZEBRA_DEBUG_VXLAN)
8702 zlog_debug("Intf %s(%u) L3-VNI %u is DOWN", ifp->name,
8703 ifp->ifindex, vni);
8704
8705 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8706 } else {
8707 /* process if-down for l2-vni */
8708 if (IS_ZEBRA_DEBUG_VXLAN)
8709 zlog_debug("Intf %s(%u) L2-VNI %u is DOWN", ifp->name,
8710 ifp->ifindex, vni);
8711
8712 /* Locate hash entry; it is expected to exist. */
8713 zvni = zvni_lookup(vni);
8714 if (!zvni) {
8715 zlog_debug(
8716 "Failed to locate VNI hash at DOWN, IF %s(%u) VNI %u",
8717 ifp->name, ifp->ifindex, vni);
8718 return -1;
8719 }
8720
8721 assert(zvni->vxlan_if == ifp);
8722
8723 /* Delete this VNI from BGP. */
8724 zvni_send_del_to_client(zvni->vni);
8725
8726 /* Free up all neighbors and MACs, if any. */
8727 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
8728 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
8729
8730 /* Free up all remote VTEPs, if any. */
8731 zvni_vtep_del_all(zvni, 1);
8732 }
8733 return 0;
8734 }
8735
8736 /*
8737 * Handle VxLAN interface up - update BGP if required.
8738 */
8739 int zebra_vxlan_if_up(struct interface *ifp)
8740 {
8741 vni_t vni;
8742 struct zebra_if *zif = NULL;
8743 struct zebra_l2info_vxlan *vxl = NULL;
8744 zebra_vni_t *zvni = NULL;
8745 zebra_l3vni_t *zl3vni = NULL;
8746
8747 /* Check if EVPN is enabled. */
8748 if (!is_evpn_enabled())
8749 return 0;
8750
8751 zif = ifp->info;
8752 assert(zif);
8753 vxl = &zif->l2info.vxl;
8754 vni = vxl->vni;
8755
8756 zl3vni = zl3vni_lookup(vni);
8757 if (zl3vni) {
8758 /* we need to associate with SVI, if any, we can associate with
8759 * svi-if only after association with vxlan-intf is complete
8760 */
8761 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
8762 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
8763
8764 if (IS_ZEBRA_DEBUG_VXLAN)
8765 zlog_debug("Intf %s(%u) L3-VNI %u is UP svi_if %s mac_vlan_if %s"
8766 , ifp->name, ifp->ifindex, vni,
8767 zl3vni->svi_if ? zl3vni->svi_if->name : "NIL",
8768 zl3vni->mac_vlan_if ?
8769 zl3vni->mac_vlan_if->name : "NIL");
8770
8771 if (is_l3vni_oper_up(zl3vni))
8772 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8773 } else {
8774 /* Handle L2-VNI add */
8775 struct interface *vlan_if = NULL;
8776
8777 if (IS_ZEBRA_DEBUG_VXLAN)
8778 zlog_debug("Intf %s(%u) L2-VNI %u is UP", ifp->name,
8779 ifp->ifindex, vni);
8780
8781 /* Locate hash entry; it is expected to exist. */
8782 zvni = zvni_lookup(vni);
8783 if (!zvni) {
8784 zlog_debug(
8785 "Failed to locate VNI hash at UP, IF %s(%u) VNI %u",
8786 ifp->name, ifp->ifindex, vni);
8787 return -1;
8788 }
8789
8790 assert(zvni->vxlan_if == ifp);
8791 vlan_if = zvni_map_to_svi(vxl->access_vlan,
8792 zif->brslave_info.br_if);
8793 if (vlan_if) {
8794 zvni->vrf_id = vlan_if->vrf_id;
8795 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
8796 if (zl3vni)
8797 listnode_add_sort(zl3vni->l2vnis, zvni);
8798 }
8799
8800 /* If part of a bridge, inform BGP about this VNI. */
8801 /* Also, read and populate local MACs and neighbors. */
8802 if (zif->brslave_info.br_if) {
8803 zvni_send_add_to_client(zvni);
8804 zvni_read_mac_neigh(zvni, ifp);
8805 }
8806 }
8807
8808 return 0;
8809 }
8810
8811 /*
8812 * Handle VxLAN interface delete. Locate and remove entry in hash table
8813 * and update BGP, if required.
8814 */
8815 int zebra_vxlan_if_del(struct interface *ifp)
8816 {
8817 vni_t vni;
8818 struct zebra_if *zif = NULL;
8819 struct zebra_l2info_vxlan *vxl = NULL;
8820 zebra_vni_t *zvni = NULL;
8821 zebra_l3vni_t *zl3vni = NULL;
8822
8823 /* Check if EVPN is enabled. */
8824 if (!is_evpn_enabled())
8825 return 0;
8826
8827 zif = ifp->info;
8828 assert(zif);
8829 vxl = &zif->l2info.vxl;
8830 vni = vxl->vni;
8831
8832 zl3vni = zl3vni_lookup(vni);
8833 if (zl3vni) {
8834
8835 if (IS_ZEBRA_DEBUG_VXLAN)
8836 zlog_debug("Del L3-VNI %u intf %s(%u)", vni, ifp->name,
8837 ifp->ifindex);
8838
8839 /* process oper-down for l3-vni */
8840 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8841
8842 /* remove the association with vxlan_if */
8843 memset(&zl3vni->local_vtep_ip, 0, sizeof(struct in_addr));
8844 zl3vni->vxlan_if = NULL;
8845 } else {
8846
8847 /* process if-del for l2-vni*/
8848 if (IS_ZEBRA_DEBUG_VXLAN)
8849 zlog_debug("Del L2-VNI %u intf %s(%u)", vni, ifp->name,
8850 ifp->ifindex);
8851
8852 /* Locate hash entry; it is expected to exist. */
8853 zvni = zvni_lookup(vni);
8854 if (!zvni) {
8855 zlog_debug(
8856 "Failed to locate VNI hash at del, IF %s(%u) VNI %u",
8857 ifp->name, ifp->ifindex, vni);
8858 return 0;
8859 }
8860
8861 /* remove from l3-vni list */
8862 zl3vni = zl3vni_from_vrf(zvni->vrf_id);
8863 if (zl3vni)
8864 listnode_delete(zl3vni->l2vnis, zvni);
8865
8866 /* Delete VNI from BGP. */
8867 zvni_send_del_to_client(zvni->vni);
8868
8869 /* Free up all neighbors and MAC, if any. */
8870 zvni_neigh_del_all(zvni, 0, 0, DEL_ALL_NEIGH);
8871 zvni_mac_del_all(zvni, 0, 0, DEL_ALL_MAC);
8872
8873 /* Free up all remote VTEPs, if any. */
8874 zvni_vtep_del_all(zvni, 0);
8875
8876 /* Delete the hash entry. */
8877 if (zvni_del(zvni)) {
8878 flog_err(EC_ZEBRA_VNI_DEL_FAILED,
8879 "Failed to del VNI hash %p, IF %s(%u) VNI %u",
8880 zvni, ifp->name, ifp->ifindex, zvni->vni);
8881 return -1;
8882 }
8883 }
8884 return 0;
8885 }
8886
8887 /*
8888 * Handle VxLAN interface update - change to tunnel IP, master or VLAN.
8889 */
8890 int zebra_vxlan_if_update(struct interface *ifp, uint16_t chgflags)
8891 {
8892 vni_t vni;
8893 struct zebra_if *zif = NULL;
8894 struct zebra_l2info_vxlan *vxl = NULL;
8895 zebra_vni_t *zvni = NULL;
8896 zebra_l3vni_t *zl3vni = NULL;
8897
8898 /* Check if EVPN is enabled. */
8899 if (!is_evpn_enabled())
8900 return 0;
8901
8902 zif = ifp->info;
8903 assert(zif);
8904 vxl = &zif->l2info.vxl;
8905 vni = vxl->vni;
8906
8907 zl3vni = zl3vni_lookup(vni);
8908 if (zl3vni) {
8909
8910 if (IS_ZEBRA_DEBUG_VXLAN)
8911 zlog_debug(
8912 "Update L3-VNI %u intf %s(%u) VLAN %u local IP %s master %u chg 0x%x",
8913 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
8914 inet_ntoa(vxl->vtep_ip),
8915 zif->brslave_info.bridge_ifindex, chgflags);
8916
8917 /* Removed from bridge? Cleanup and return */
8918 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
8919 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
8920 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8921 return 0;
8922 }
8923
8924 /* access-vlan change - process oper down, associate with new
8925 * svi_if and then process oper up again
8926 */
8927 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
8928 if (if_is_operative(ifp)) {
8929 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8930 zl3vni->svi_if = NULL;
8931 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
8932 zl3vni->mac_vlan_if =
8933 zl3vni_map_to_mac_vlan_if(zl3vni);
8934 zl3vni->local_vtep_ip = vxl->vtep_ip;
8935 if (is_l3vni_oper_up(zl3vni))
8936 zebra_vxlan_process_l3vni_oper_up(
8937 zl3vni);
8938 }
8939 }
8940
8941 /*
8942 * local-ip change - process oper down, associate with new
8943 * local-ip and then process oper up again
8944 */
8945 if (chgflags & ZEBRA_VXLIF_LOCAL_IP_CHANGE) {
8946 if (if_is_operative(ifp)) {
8947 zebra_vxlan_process_l3vni_oper_down(zl3vni);
8948 zl3vni->local_vtep_ip = vxl->vtep_ip;
8949 if (is_l3vni_oper_up(zl3vni))
8950 zebra_vxlan_process_l3vni_oper_up(
8951 zl3vni);
8952 }
8953 }
8954
8955 /* Update local tunnel IP. */
8956 zl3vni->local_vtep_ip = vxl->vtep_ip;
8957
8958 /* if we have a valid new master, process l3-vni oper up */
8959 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE) {
8960 if (if_is_operative(ifp) && is_l3vni_oper_up(zl3vni))
8961 zebra_vxlan_process_l3vni_oper_up(zl3vni);
8962 }
8963 } else {
8964
8965 /* Update VNI hash. */
8966 zvni = zvni_lookup(vni);
8967 if (!zvni) {
8968 zlog_debug(
8969 "Failed to find L2-VNI hash on update, IF %s(%u) VNI %u",
8970 ifp->name, ifp->ifindex, vni);
8971 return -1;
8972 }
8973
8974 if (IS_ZEBRA_DEBUG_VXLAN)
8975 zlog_debug(
8976 "Update L2-VNI %u intf %s(%u) VLAN %u local IP %s master %u chg 0x%x",
8977 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
8978 inet_ntoa(vxl->vtep_ip),
8979 zif->brslave_info.bridge_ifindex, chgflags);
8980
8981 /* Removed from bridge? Cleanup and return */
8982 if ((chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
8983 && (zif->brslave_info.bridge_ifindex == IFINDEX_INTERNAL)) {
8984 /* Delete from client, remove all remote VTEPs */
8985 /* Also, free up all MACs and neighbors. */
8986 zvni_send_del_to_client(zvni->vni);
8987 zvni_neigh_del_all(zvni, 1, 0, DEL_ALL_NEIGH);
8988 zvni_mac_del_all(zvni, 1, 0, DEL_ALL_MAC);
8989 zvni_vtep_del_all(zvni, 1);
8990 return 0;
8991 }
8992
8993 /* Handle other changes. */
8994 if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
8995 /* Remove all existing local neigh and MACs for this VNI
8996 * (including from BGP)
8997 */
8998 zvni_neigh_del_all(zvni, 0, 1, DEL_LOCAL_MAC);
8999 zvni_mac_del_all(zvni, 0, 1, DEL_LOCAL_MAC);
9000 }
9001
9002 if (zvni->local_vtep_ip.s_addr != vxl->vtep_ip.s_addr ||
9003 zvni->mcast_grp.s_addr != vxl->mcast_grp.s_addr) {
9004 zebra_vxlan_sg_deref(zvni->local_vtep_ip,
9005 zvni->mcast_grp);
9006 zebra_vxlan_sg_ref(vxl->vtep_ip, vxl->mcast_grp);
9007 zvni->local_vtep_ip = vxl->vtep_ip;
9008 zvni->mcast_grp = vxl->mcast_grp;
9009 }
9010 zvni->vxlan_if = ifp;
9011
9012 /* Take further actions needed.
9013 * Note that if we are here, there is a change of interest.
9014 */
9015 /* If down or not mapped to a bridge, we're done. */
9016 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
9017 return 0;
9018
9019 /* Inform BGP, if there is a change of interest. */
9020 if (chgflags
9021 & (ZEBRA_VXLIF_MASTER_CHANGE |
9022 ZEBRA_VXLIF_LOCAL_IP_CHANGE |
9023 ZEBRA_VXLIF_MCAST_GRP_CHANGE))
9024 zvni_send_add_to_client(zvni);
9025
9026 /* If there is a valid new master or a VLAN mapping change,
9027 * read and populate local MACs and neighbors.
9028 * Also, reinstall any remote MACs and neighbors
9029 * for this VNI (based on new VLAN).
9030 */
9031 if (chgflags & ZEBRA_VXLIF_MASTER_CHANGE)
9032 zvni_read_mac_neigh(zvni, ifp);
9033 else if (chgflags & ZEBRA_VXLIF_VLAN_CHANGE) {
9034 struct mac_walk_ctx m_wctx;
9035 struct neigh_walk_ctx n_wctx;
9036
9037 zvni_read_mac_neigh(zvni, ifp);
9038
9039 memset(&m_wctx, 0, sizeof(struct mac_walk_ctx));
9040 m_wctx.zvni = zvni;
9041 hash_iterate(zvni->mac_table, zvni_install_mac_hash,
9042 &m_wctx);
9043
9044 memset(&n_wctx, 0, sizeof(struct neigh_walk_ctx));
9045 n_wctx.zvni = zvni;
9046 hash_iterate(zvni->neigh_table, zvni_install_neigh_hash,
9047 &n_wctx);
9048 }
9049 }
9050
9051 return 0;
9052 }
9053
9054 /*
9055 * Handle VxLAN interface add.
9056 */
9057 int zebra_vxlan_if_add(struct interface *ifp)
9058 {
9059 vni_t vni;
9060 struct zebra_if *zif = NULL;
9061 struct zebra_l2info_vxlan *vxl = NULL;
9062 zebra_vni_t *zvni = NULL;
9063 zebra_l3vni_t *zl3vni = NULL;
9064
9065 /* Check if EVPN is enabled. */
9066 if (!is_evpn_enabled())
9067 return 0;
9068
9069 zif = ifp->info;
9070 assert(zif);
9071 vxl = &zif->l2info.vxl;
9072 vni = vxl->vni;
9073
9074 zl3vni = zl3vni_lookup(vni);
9075 if (zl3vni) {
9076
9077 /* process if-add for l3-vni*/
9078 if (IS_ZEBRA_DEBUG_VXLAN)
9079 zlog_debug(
9080 "Add L3-VNI %u intf %s(%u) VLAN %u local IP %s master %u",
9081 vni, ifp->name, ifp->ifindex, vxl->access_vlan,
9082 inet_ntoa(vxl->vtep_ip),
9083 zif->brslave_info.bridge_ifindex);
9084
9085 /* associate with vxlan_if */
9086 zl3vni->local_vtep_ip = vxl->vtep_ip;
9087 zl3vni->vxlan_if = ifp;
9088
9089 /* Associate with SVI, if any. We can associate with svi-if only
9090 * after association with vxlan_if is complete */
9091 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
9092
9093 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
9094
9095 if (is_l3vni_oper_up(zl3vni))
9096 zebra_vxlan_process_l3vni_oper_up(zl3vni);
9097 } else {
9098
9099 /* process if-add for l2-vni */
9100 struct interface *vlan_if = NULL;
9101
9102 /* Create or update VNI hash. */
9103 zvni = zvni_lookup(vni);
9104 if (!zvni) {
9105 zvni = zvni_add(vni);
9106 if (!zvni) {
9107 flog_err(
9108 EC_ZEBRA_VNI_ADD_FAILED,
9109 "Failed to add VNI hash, IF %s(%u) VNI %u",
9110 ifp->name, ifp->ifindex, vni);
9111 return -1;
9112 }
9113 }
9114
9115 if (zvni->local_vtep_ip.s_addr != vxl->vtep_ip.s_addr ||
9116 zvni->mcast_grp.s_addr != vxl->mcast_grp.s_addr) {
9117 zebra_vxlan_sg_deref(zvni->local_vtep_ip,
9118 zvni->mcast_grp);
9119 zebra_vxlan_sg_ref(vxl->vtep_ip, vxl->mcast_grp);
9120 zvni->local_vtep_ip = vxl->vtep_ip;
9121 zvni->mcast_grp = vxl->mcast_grp;
9122 }
9123 zvni->vxlan_if = ifp;
9124 vlan_if = zvni_map_to_svi(vxl->access_vlan,
9125 zif->brslave_info.br_if);
9126 if (vlan_if) {
9127 zvni->vrf_id = vlan_if->vrf_id;
9128 zl3vni = zl3vni_from_vrf(vlan_if->vrf_id);
9129 if (zl3vni)
9130 listnode_add_sort(zl3vni->l2vnis, zvni);
9131 }
9132
9133 if (IS_ZEBRA_DEBUG_VXLAN) {
9134 char addr_buf1[INET_ADDRSTRLEN];
9135 char addr_buf2[INET_ADDRSTRLEN];
9136
9137 inet_ntop(AF_INET, &vxl->vtep_ip,
9138 addr_buf1, INET_ADDRSTRLEN);
9139 inet_ntop(AF_INET, &vxl->mcast_grp,
9140 addr_buf2, INET_ADDRSTRLEN);
9141
9142 zlog_debug(
9143 "Add L2-VNI %u VRF %s intf %s(%u) VLAN %u local IP %s mcast_grp %s master %u",
9144 vni,
9145 vlan_if ? vrf_id_to_name(vlan_if->vrf_id)
9146 : VRF_DEFAULT_NAME,
9147 ifp->name, ifp->ifindex, vxl->access_vlan,
9148 addr_buf1, addr_buf2,
9149 zif->brslave_info.bridge_ifindex);
9150 }
9151
9152 /* If down or not mapped to a bridge, we're done. */
9153 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
9154 return 0;
9155
9156 /* Inform BGP */
9157 zvni_send_add_to_client(zvni);
9158
9159 /* Read and populate local MACs and neighbors */
9160 zvni_read_mac_neigh(zvni, ifp);
9161 }
9162
9163 return 0;
9164 }
9165
9166 int zebra_vxlan_process_vrf_vni_cmd(struct zebra_vrf *zvrf, vni_t vni,
9167 char *err, int err_str_sz, int filter,
9168 int add)
9169 {
9170 zebra_l3vni_t *zl3vni = NULL;
9171 struct zebra_vrf *zvrf_evpn = NULL;
9172
9173 zvrf_evpn = zebra_vrf_get_evpn();
9174 if (!zvrf_evpn)
9175 return -1;
9176
9177 if (IS_ZEBRA_DEBUG_VXLAN)
9178 zlog_debug("vrf %s vni %u %s", zvrf_name(zvrf), vni,
9179 add ? "ADD" : "DEL");
9180
9181 if (add) {
9182
9183 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
9184
9185 /* check if the vni is already present under zvrf */
9186 if (zvrf->l3vni) {
9187 snprintf(err, err_str_sz,
9188 "VNI is already configured under the vrf");
9189 return -1;
9190 }
9191
9192 /* check if this VNI is already present in the system */
9193 zl3vni = zl3vni_lookup(vni);
9194 if (zl3vni) {
9195 snprintf(err, err_str_sz,
9196 "VNI is already configured as L3-VNI");
9197 return -1;
9198 }
9199
9200 /* add the L3-VNI to the global table */
9201 zl3vni = zl3vni_add(vni, zvrf_id(zvrf));
9202 if (!zl3vni) {
9203 snprintf(err, err_str_sz, "Could not add L3-VNI");
9204 return -1;
9205 }
9206
9207 /* associate the vrf with vni */
9208 zvrf->l3vni = vni;
9209
9210 /* set the filter in l3vni to denote if we are using l3vni only
9211 * for prefix routes
9212 */
9213 if (filter)
9214 SET_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY);
9215
9216 /* associate with vxlan-intf;
9217 * we need to associate with the vxlan-intf first
9218 */
9219 zl3vni->vxlan_if = zl3vni_map_to_vxlan_if(zl3vni);
9220
9221 /* associate with corresponding SVI interface, we can associate
9222 * with svi-if only after vxlan interface association is
9223 * complete
9224 */
9225 zl3vni->svi_if = zl3vni_map_to_svi_if(zl3vni);
9226
9227 zl3vni->mac_vlan_if = zl3vni_map_to_mac_vlan_if(zl3vni);
9228
9229 if (IS_ZEBRA_DEBUG_VXLAN)
9230 zlog_debug("%s: l3vni %u svi_if %s mac_vlan_if %s",
9231 __PRETTY_FUNCTION__, vni,
9232 zl3vni->svi_if ?
9233 zl3vni->svi_if->name : "NIL",
9234 zl3vni->mac_vlan_if ?
9235 zl3vni->mac_vlan_if->name : "NIL");
9236
9237 /* formulate l2vni list */
9238 hash_iterate(zvrf_evpn->vni_table, zvni_add_to_l3vni_list,
9239 zl3vni);
9240
9241 if (is_l3vni_oper_up(zl3vni))
9242 zebra_vxlan_process_l3vni_oper_up(zl3vni);
9243
9244 } else {
9245 zl3vni = zl3vni_lookup(vni);
9246 if (!zl3vni) {
9247 snprintf(err, err_str_sz, "VNI doesn't exist");
9248 return -1;
9249 }
9250
9251 if (zvrf->l3vni != vni) {
9252 snprintf(err, err_str_sz,
9253 "VNI %d doesn't exist in VRF: %s",
9254 vni, zvrf->vrf->name);
9255 return -1;
9256 }
9257
9258 if (filter && !CHECK_FLAG(zl3vni->filter, PREFIX_ROUTES_ONLY)) {
9259 snprintf(err, ERR_STR_SZ,
9260 "prefix-routes-only is not set for the vni");
9261 return -1;
9262 }
9263
9264 zebra_vxlan_process_l3vni_oper_down(zl3vni);
9265
9266 /* delete and uninstall all rmacs */
9267 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
9268 zl3vni);
9269
9270 /* delete and uninstall all next-hops */
9271 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
9272 zl3vni);
9273
9274 zvrf->l3vni = 0;
9275 zl3vni_del(zl3vni);
9276
9277 zebra_vxlan_handle_vni_transition(zvrf, vni, add);
9278 }
9279 return 0;
9280 }
9281
9282 int zebra_vxlan_vrf_enable(struct zebra_vrf *zvrf)
9283 {
9284 zebra_l3vni_t *zl3vni = NULL;
9285
9286 if (zvrf->l3vni)
9287 zl3vni = zl3vni_lookup(zvrf->l3vni);
9288 if (!zl3vni)
9289 return 0;
9290
9291 zl3vni->vrf_id = zvrf_id(zvrf);
9292 if (is_l3vni_oper_up(zl3vni))
9293 zebra_vxlan_process_l3vni_oper_up(zl3vni);
9294 return 0;
9295 }
9296
9297 int zebra_vxlan_vrf_disable(struct zebra_vrf *zvrf)
9298 {
9299 zebra_l3vni_t *zl3vni = NULL;
9300
9301 if (zvrf->l3vni)
9302 zl3vni = zl3vni_lookup(zvrf->l3vni);
9303 if (!zl3vni)
9304 return 0;
9305
9306 zl3vni->vrf_id = VRF_UNKNOWN;
9307 zebra_vxlan_process_l3vni_oper_down(zl3vni);
9308 return 0;
9309 }
9310
9311 int zebra_vxlan_vrf_delete(struct zebra_vrf *zvrf)
9312 {
9313 zebra_l3vni_t *zl3vni = NULL;
9314 vni_t vni;
9315
9316 if (zvrf->l3vni)
9317 zl3vni = zl3vni_lookup(zvrf->l3vni);
9318 if (!zl3vni)
9319 return 0;
9320
9321 vni = zl3vni->vni;
9322 zl3vni_del(zl3vni);
9323 zebra_vxlan_handle_vni_transition(zvrf, vni, 0);
9324
9325 return 0;
9326 }
9327
9328 /*
9329 * Handle message from client to specify the flooding mechanism for
9330 * BUM packets. The default is to do head-end (ingress) replication
9331 * and the other supported option is to disable it. This applies to
9332 * all BUM traffic and disabling it applies to both the transmit and
9333 * receive direction.
9334 */
9335 void zebra_vxlan_flood_control(ZAPI_HANDLER_ARGS)
9336 {
9337 struct stream *s;
9338 enum vxlan_flood_control flood_ctrl;
9339
9340 if (!EVPN_ENABLED(zvrf)) {
9341 zlog_err("EVPN flood control for non-EVPN VRF %u",
9342 zvrf_id(zvrf));
9343 return;
9344 }
9345
9346 s = msg;
9347 STREAM_GETC(s, flood_ctrl);
9348
9349 if (IS_ZEBRA_DEBUG_VXLAN)
9350 zlog_debug("EVPN flood control %u, currently %u",
9351 flood_ctrl, zvrf->vxlan_flood_ctrl);
9352
9353 if (zvrf->vxlan_flood_ctrl == flood_ctrl)
9354 return;
9355
9356 zvrf->vxlan_flood_ctrl = flood_ctrl;
9357
9358 /* Install or uninstall flood entries corresponding to
9359 * remote VTEPs.
9360 */
9361 hash_iterate(zvrf->vni_table, zvni_handle_flooding_remote_vteps,
9362 zvrf);
9363
9364 stream_failure:
9365 return;
9366 }
9367
9368 /*
9369 * Handle message from client to enable/disable advertisement of svi macip
9370 * routes
9371 */
9372 void zebra_vxlan_advertise_svi_macip(ZAPI_HANDLER_ARGS)
9373 {
9374 struct stream *s;
9375 int advertise;
9376 vni_t vni = 0;
9377 zebra_vni_t *zvni = NULL;
9378 struct interface *ifp = NULL;
9379
9380 if (!EVPN_ENABLED(zvrf)) {
9381 zlog_debug("EVPN SVI-MACIP Adv for non-EVPN VRF %u",
9382 zvrf_id(zvrf));
9383 return;
9384 }
9385
9386 s = msg;
9387 STREAM_GETC(s, advertise);
9388 STREAM_GETL(s, vni);
9389
9390 if (!vni) {
9391 if (IS_ZEBRA_DEBUG_VXLAN)
9392 zlog_debug("EVPN SVI-MACIP Adv %s, currently %s",
9393 advertise ? "enabled" : "disabled",
9394 advertise_svi_macip_enabled(NULL)
9395 ? "enabled"
9396 : "disabled");
9397
9398 if (zvrf->advertise_svi_macip == advertise)
9399 return;
9400
9401
9402 if (advertise) {
9403 zvrf->advertise_svi_macip = advertise;
9404 hash_iterate(zvrf->vni_table,
9405 zvni_gw_macip_add_for_vni_hash, NULL);
9406 } else {
9407 hash_iterate(zvrf->vni_table,
9408 zvni_svi_macip_del_for_vni_hash, NULL);
9409 zvrf->advertise_svi_macip = advertise;
9410 }
9411
9412 } else {
9413 struct zebra_if *zif = NULL;
9414 struct zebra_l2info_vxlan zl2_info;
9415 struct interface *vlan_if = NULL;
9416
9417 zvni = zvni_lookup(vni);
9418 if (!zvni)
9419 return;
9420
9421 if (IS_ZEBRA_DEBUG_VXLAN)
9422 zlog_debug(
9423 "EVPN SVI macip Adv %s on VNI %d , currently %s",
9424 advertise ? "enabled" : "disabled", vni,
9425 advertise_svi_macip_enabled(zvni)
9426 ? "enabled"
9427 : "disabled");
9428
9429 if (zvni->advertise_svi_macip == advertise)
9430 return;
9431
9432 /* Store flag even though SVI is not present.
9433 * Once SVI comes up triggers self MAC-IP route add.
9434 */
9435 zvni->advertise_svi_macip = advertise;
9436
9437 ifp = zvni->vxlan_if;
9438 if (!ifp)
9439 return;
9440
9441 zif = ifp->info;
9442
9443 /* If down or not mapped to a bridge, we're done. */
9444 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
9445 return;
9446
9447 zl2_info = zif->l2info.vxl;
9448 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
9449 zif->brslave_info.br_if);
9450 if (!vlan_if)
9451 return;
9452
9453 if (advertise) {
9454 /* Add primary SVI MAC-IP */
9455 zvni_add_macip_for_intf(vlan_if, zvni);
9456 } else {
9457 /* Del primary SVI MAC-IP */
9458 zvni_del_macip_for_intf(vlan_if, zvni);
9459 }
9460 }
9461
9462 stream_failure:
9463 return;
9464 }
9465
9466 /*
9467 * Handle message from client to enable/disable advertisement of g/w macip
9468 * routes
9469 */
9470 void zebra_vxlan_advertise_subnet(ZAPI_HANDLER_ARGS)
9471 {
9472 struct stream *s;
9473 int advertise;
9474 vni_t vni = 0;
9475 zebra_vni_t *zvni = NULL;
9476 struct interface *ifp = NULL;
9477 struct zebra_if *zif = NULL;
9478 struct zebra_l2info_vxlan zl2_info;
9479 struct interface *vlan_if = NULL;
9480
9481 if (!EVPN_ENABLED(zvrf)) {
9482 zlog_debug("EVPN GW-MACIP Adv for non-EVPN VRF %u",
9483 zvrf_id(zvrf));
9484 return;
9485 }
9486
9487 s = msg;
9488 STREAM_GETC(s, advertise);
9489 STREAM_GET(&vni, s, 3);
9490
9491 zvni = zvni_lookup(vni);
9492 if (!zvni)
9493 return;
9494
9495 if (zvni->advertise_subnet == advertise)
9496 return;
9497
9498 if (IS_ZEBRA_DEBUG_VXLAN)
9499 zlog_debug("EVPN subnet Adv %s on VNI %d , currently %s",
9500 advertise ? "enabled" : "disabled", vni,
9501 zvni->advertise_subnet ? "enabled" : "disabled");
9502
9503
9504 zvni->advertise_subnet = advertise;
9505
9506 ifp = zvni->vxlan_if;
9507 if (!ifp)
9508 return;
9509
9510 zif = ifp->info;
9511
9512 /* If down or not mapped to a bridge, we're done. */
9513 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
9514 return;
9515
9516 zl2_info = zif->l2info.vxl;
9517
9518 vlan_if =
9519 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
9520 if (!vlan_if)
9521 return;
9522
9523 if (zvni->advertise_subnet)
9524 zvni_advertise_subnet(zvni, vlan_if, 1);
9525 else
9526 zvni_advertise_subnet(zvni, vlan_if, 0);
9527
9528 stream_failure:
9529 return;
9530 }
9531
9532 /*
9533 * Handle message from client to enable/disable advertisement of g/w macip
9534 * routes
9535 */
9536 void zebra_vxlan_advertise_gw_macip(ZAPI_HANDLER_ARGS)
9537 {
9538 struct stream *s;
9539 int advertise;
9540 vni_t vni = 0;
9541 zebra_vni_t *zvni = NULL;
9542 struct interface *ifp = NULL;
9543
9544 if (!EVPN_ENABLED(zvrf)) {
9545 zlog_debug("EVPN GW-MACIP Adv for non-EVPN VRF %u",
9546 zvrf_id(zvrf));
9547 return;
9548 }
9549
9550 s = msg;
9551 STREAM_GETC(s, advertise);
9552 STREAM_GETL(s, vni);
9553
9554 if (!vni) {
9555 if (IS_ZEBRA_DEBUG_VXLAN)
9556 zlog_debug("EVPN gateway macip Adv %s, currently %s",
9557 advertise ? "enabled" : "disabled",
9558 advertise_gw_macip_enabled(NULL)
9559 ? "enabled"
9560 : "disabled");
9561
9562 if (zvrf->advertise_gw_macip == advertise)
9563 return;
9564
9565 zvrf->advertise_gw_macip = advertise;
9566
9567 if (advertise_gw_macip_enabled(zvni))
9568 hash_iterate(zvrf->vni_table,
9569 zvni_gw_macip_add_for_vni_hash, NULL);
9570 else
9571 hash_iterate(zvrf->vni_table,
9572 zvni_gw_macip_del_for_vni_hash, NULL);
9573
9574 } else {
9575 struct zebra_if *zif = NULL;
9576 struct zebra_l2info_vxlan zl2_info;
9577 struct interface *vlan_if = NULL;
9578 struct interface *vrr_if = NULL;
9579
9580 zvni = zvni_lookup(vni);
9581 if (!zvni)
9582 return;
9583
9584 if (IS_ZEBRA_DEBUG_VXLAN)
9585 zlog_debug(
9586 "EVPN gateway macip Adv %s on VNI %d , currently %s",
9587 advertise ? "enabled" : "disabled", vni,
9588 advertise_gw_macip_enabled(zvni) ? "enabled"
9589 : "disabled");
9590
9591 if (zvni->advertise_gw_macip == advertise)
9592 return;
9593
9594 zvni->advertise_gw_macip = advertise;
9595
9596 ifp = zvni->vxlan_if;
9597 if (!ifp)
9598 return;
9599
9600 zif = ifp->info;
9601
9602 /* If down or not mapped to a bridge, we're done. */
9603 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
9604 return;
9605
9606 zl2_info = zif->l2info.vxl;
9607
9608 vlan_if = zvni_map_to_svi(zl2_info.access_vlan,
9609 zif->brslave_info.br_if);
9610 if (!vlan_if)
9611 return;
9612
9613 if (advertise_gw_macip_enabled(zvni)) {
9614 /* Add primary SVI MAC-IP */
9615 zvni_add_macip_for_intf(vlan_if, zvni);
9616
9617 /* Add VRR MAC-IP - if any*/
9618 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
9619 if (vrr_if)
9620 zvni_add_macip_for_intf(vrr_if, zvni);
9621 } else {
9622 /* Del primary MAC-IP */
9623 zvni_del_macip_for_intf(vlan_if, zvni);
9624
9625 /* Del VRR MAC-IP - if any*/
9626 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
9627 if (vrr_if)
9628 zvni_del_macip_for_intf(vrr_if, zvni);
9629 }
9630 }
9631
9632 stream_failure:
9633 return;
9634 }
9635
9636
9637 /*
9638 * Handle message from client to learn (or stop learning) about VNIs and MACs.
9639 * When enabled, the VNI hash table will be built and MAC FDB table read;
9640 * when disabled, the entries should be deleted and remote VTEPs and MACs
9641 * uninstalled from the kernel.
9642 * This also informs the setting for BUM handling at the time this change
9643 * occurs; it is relevant only when specifying "learn".
9644 */
9645 void zebra_vxlan_advertise_all_vni(ZAPI_HANDLER_ARGS)
9646 {
9647 struct stream *s = NULL;
9648 int advertise = 0;
9649 enum vxlan_flood_control flood_ctrl;
9650
9651 /* Mismatch between EVPN VRF and current VRF (should be prevented by
9652 * bgpd's cli) */
9653 if (is_evpn_enabled() && !EVPN_ENABLED(zvrf))
9654 return;
9655
9656 s = msg;
9657 STREAM_GETC(s, advertise);
9658 STREAM_GETC(s, flood_ctrl);
9659
9660 if (IS_ZEBRA_DEBUG_VXLAN)
9661 zlog_debug("EVPN VRF %s(%u) VNI Adv %s, currently %s, flood control %u",
9662 zvrf_name(zvrf), zvrf_id(zvrf),
9663 advertise ? "enabled" : "disabled",
9664 is_evpn_enabled() ? "enabled" : "disabled",
9665 flood_ctrl);
9666
9667 if (zvrf->advertise_all_vni == advertise)
9668 return;
9669
9670 zvrf->advertise_all_vni = advertise;
9671 if (EVPN_ENABLED(zvrf)) {
9672 zrouter.evpn_vrf = zvrf;
9673
9674 /* Note BUM handling */
9675 zvrf->vxlan_flood_ctrl = flood_ctrl;
9676
9677 /* Build VNI hash table and inform BGP. */
9678 zvni_build_hash_table();
9679
9680 /* Add all SVI (L3 GW) MACs to BGP*/
9681 hash_iterate(zvrf->vni_table, zvni_gw_macip_add_for_vni_hash,
9682 NULL);
9683
9684 /* Read the MAC FDB */
9685 macfdb_read(zvrf->zns);
9686
9687 /* Read neighbors */
9688 neigh_read(zvrf->zns);
9689 } else {
9690 /* Cleanup VTEPs for all VNIs - uninstall from
9691 * kernel and free entries.
9692 */
9693 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
9694
9695 /* cleanup all l3vnis */
9696 hash_iterate(zrouter.l3vni_table, zl3vni_cleanup_all, NULL);
9697
9698 /* Mark as "no EVPN VRF" */
9699 zrouter.evpn_vrf = NULL;
9700 }
9701
9702 stream_failure:
9703 return;
9704 }
9705
9706 /*
9707 * Allocate VNI hash table for this VRF and do other initialization.
9708 * NOTE: Currently supported only for default VRF.
9709 */
9710 void zebra_vxlan_init_tables(struct zebra_vrf *zvrf)
9711 {
9712 if (!zvrf)
9713 return;
9714 zvrf->vni_table = hash_create(vni_hash_keymake, vni_hash_cmp,
9715 "Zebra VRF VNI Table");
9716 zvrf->vxlan_sg_table = hash_create(zebra_vxlan_sg_hash_key_make,
9717 zebra_vxlan_sg_hash_eq, "Zebra VxLAN SG Table");
9718 }
9719
9720 /* Cleanup VNI info, but don't free the table. */
9721 void zebra_vxlan_cleanup_tables(struct zebra_vrf *zvrf)
9722 {
9723 if (!zvrf)
9724 return;
9725 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
9726 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_sg_cleanup, NULL);
9727 }
9728
9729 /* Close all VNI handling */
9730 void zebra_vxlan_close_tables(struct zebra_vrf *zvrf)
9731 {
9732 if (!zvrf)
9733 return;
9734 hash_iterate(zvrf->vni_table, zvni_cleanup_all, zvrf);
9735 hash_free(zvrf->vni_table);
9736 }
9737
9738 /* init the l3vni table */
9739 void zebra_vxlan_init(void)
9740 {
9741 zrouter.l3vni_table = hash_create(l3vni_hash_keymake, l3vni_hash_cmp,
9742 "Zebra VRF L3 VNI table");
9743 zrouter.evpn_vrf = NULL;
9744 }
9745
9746 /* free l3vni table */
9747 void zebra_vxlan_disable(void)
9748 {
9749 hash_free(zrouter.l3vni_table);
9750 }
9751
9752 /* get the l3vni svi ifindex */
9753 ifindex_t get_l3vni_svi_ifindex(vrf_id_t vrf_id)
9754 {
9755 zebra_l3vni_t *zl3vni = NULL;
9756
9757 zl3vni = zl3vni_from_vrf(vrf_id);
9758 if (!zl3vni || !is_l3vni_oper_up(zl3vni))
9759 return 0;
9760
9761 return zl3vni->svi_if->ifindex;
9762 }
9763
9764 static int zebra_vxlan_dad_ip_auto_recovery_exp(struct thread *t)
9765 {
9766 struct zebra_vrf *zvrf = NULL;
9767 zebra_neigh_t *nbr = NULL;
9768 zebra_vni_t *zvni = NULL;
9769 char buf1[INET6_ADDRSTRLEN];
9770 char buf2[ETHER_ADDR_STRLEN];
9771
9772 nbr = THREAD_ARG(t);
9773
9774 /* since this is asynchronous we need sanity checks*/
9775 zvrf = vrf_info_lookup(nbr->zvni->vrf_id);
9776 if (!zvrf)
9777 return 0;
9778
9779 zvni = zvni_lookup(nbr->zvni->vni);
9780 if (!zvni)
9781 return 0;
9782
9783 nbr = zvni_neigh_lookup(zvni, &nbr->ip);
9784 if (!nbr)
9785 return 0;
9786
9787 if (IS_ZEBRA_DEBUG_VXLAN)
9788 zlog_debug("%s: duplicate addr MAC %s IP %s flags 0x%x learn count %u vni %u auto recovery expired",
9789 __PRETTY_FUNCTION__,
9790 prefix_mac2str(&nbr->emac, buf2, sizeof(buf2)),
9791 ipaddr2str(&nbr->ip, buf1, sizeof(buf1)),
9792 nbr->flags,
9793 nbr->dad_count, zvni->vni);
9794
9795 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
9796 nbr->dad_count = 0;
9797 nbr->detect_start_time.tv_sec = 0;
9798 nbr->detect_start_time.tv_usec = 0;
9799 nbr->dad_dup_detect_time = 0;
9800 nbr->dad_ip_auto_recovery_timer = NULL;
9801 ZEBRA_NEIGH_SET_ACTIVE(nbr);
9802
9803 /* Send to BGP */
9804 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) {
9805 zvni_neigh_send_add_to_client(zvni->vni, &nbr->ip, &nbr->emac,
9806 nbr->flags, nbr->loc_seq);
9807 } else if (!!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) {
9808 zvni_neigh_install(zvni, nbr);
9809 }
9810
9811 return 0;
9812 }
9813
9814 static int zebra_vxlan_dad_mac_auto_recovery_exp(struct thread *t)
9815 {
9816 struct zebra_vrf *zvrf = NULL;
9817 zebra_mac_t *mac = NULL;
9818 zebra_vni_t *zvni = NULL;
9819 struct listnode *node = NULL;
9820 zebra_neigh_t *nbr = NULL;
9821 char buf[ETHER_ADDR_STRLEN];
9822
9823 mac = THREAD_ARG(t);
9824
9825 /* since this is asynchronous we need sanity checks*/
9826 zvrf = vrf_info_lookup(mac->zvni->vrf_id);
9827 if (!zvrf)
9828 return 0;
9829
9830 zvni = zvni_lookup(mac->zvni->vni);
9831 if (!zvni)
9832 return 0;
9833
9834 mac = zvni_mac_lookup(zvni, &mac->macaddr);
9835 if (!mac)
9836 return 0;
9837
9838 if (IS_ZEBRA_DEBUG_VXLAN)
9839 zlog_debug("%s: duplicate addr mac %s flags 0x%x learn count %u host count %u auto recovery expired",
9840 __PRETTY_FUNCTION__,
9841 prefix_mac2str(&mac->macaddr, buf, sizeof(buf)),
9842 mac->flags,
9843 mac->dad_count,
9844 listcount(mac->neigh_list));
9845
9846 /* Remove all IPs as duplicate associcated with this MAC */
9847 for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) {
9848 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) {
9849 if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL))
9850 ZEBRA_NEIGH_SET_INACTIVE(nbr);
9851 else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE))
9852 zvni_neigh_install(zvni, nbr);
9853 }
9854
9855 UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE);
9856 nbr->dad_count = 0;
9857 nbr->detect_start_time.tv_sec = 0;
9858 nbr->dad_dup_detect_time = 0;
9859 }
9860
9861 UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE);
9862 mac->dad_count = 0;
9863 mac->detect_start_time.tv_sec = 0;
9864 mac->detect_start_time.tv_usec = 0;
9865 mac->dad_dup_detect_time = 0;
9866 mac->dad_mac_auto_recovery_timer = NULL;
9867
9868 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
9869 /* Inform to BGP */
9870 if (zvni_mac_send_add_to_client(zvni->vni, &mac->macaddr,
9871 mac->flags, mac->loc_seq))
9872 return -1;
9873
9874 /* Process all neighbors associated with this MAC. */
9875 zvni_process_neigh_on_local_mac_change(zvni, mac, 0);
9876
9877 } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
9878 zvni_process_neigh_on_remote_mac_add(zvni, mac);
9879
9880 /* Install the entry. */
9881 zvni_mac_install(zvni, mac);
9882 }
9883
9884 return 0;
9885 }
9886
9887 /************************** vxlan SG cache management ************************/
9888 /* Inform PIM about the mcast group */
9889 static int zebra_vxlan_sg_send(struct zebra_vrf *zvrf,
9890 struct prefix_sg *sg,
9891 char *sg_str, uint16_t cmd)
9892 {
9893 struct zserv *client = NULL;
9894 struct stream *s = NULL;
9895
9896 client = zserv_find_client(ZEBRA_ROUTE_PIM, 0);
9897 if (!client)
9898 return 0;
9899
9900 if (!CHECK_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG))
9901 return 0;
9902
9903 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
9904
9905 zclient_create_header(s, cmd, VRF_DEFAULT);
9906 stream_putl(s, IPV4_MAX_BYTELEN);
9907 stream_put(s, &sg->src.s_addr, IPV4_MAX_BYTELEN);
9908 stream_put(s, &sg->grp.s_addr, IPV4_MAX_BYTELEN);
9909
9910 /* Write packet size. */
9911 stream_putw_at(s, 0, stream_get_endp(s));
9912
9913 if (IS_ZEBRA_DEBUG_VXLAN)
9914 zlog_debug(
9915 "Send %s %s to %s",
9916 (cmd == ZEBRA_VXLAN_SG_ADD) ? "add" : "del", sg_str,
9917 zebra_route_string(client->proto));
9918
9919 if (cmd == ZEBRA_VXLAN_SG_ADD)
9920 client->vxlan_sg_add_cnt++;
9921 else
9922 client->vxlan_sg_del_cnt++;
9923
9924 return zserv_send_message(client, s);
9925 }
9926
9927 static unsigned int zebra_vxlan_sg_hash_key_make(const void *p)
9928 {
9929 const zebra_vxlan_sg_t *vxlan_sg = p;
9930
9931 return (jhash_2words(vxlan_sg->sg.src.s_addr,
9932 vxlan_sg->sg.grp.s_addr, 0));
9933 }
9934
9935 static bool zebra_vxlan_sg_hash_eq(const void *p1, const void *p2)
9936 {
9937 const zebra_vxlan_sg_t *sg1 = p1;
9938 const zebra_vxlan_sg_t *sg2 = p2;
9939
9940 return ((sg1->sg.src.s_addr == sg2->sg.src.s_addr)
9941 && (sg1->sg.grp.s_addr == sg2->sg.grp.s_addr));
9942 }
9943
9944 static zebra_vxlan_sg_t *zebra_vxlan_sg_new(struct zebra_vrf *zvrf,
9945 struct prefix_sg *sg)
9946 {
9947 zebra_vxlan_sg_t *vxlan_sg;
9948
9949 vxlan_sg = XCALLOC(MTYPE_ZVXLAN_SG, sizeof(*vxlan_sg));
9950
9951 vxlan_sg->zvrf = zvrf;
9952 vxlan_sg->sg = *sg;
9953 prefix_sg2str(sg, vxlan_sg->sg_str);
9954
9955 vxlan_sg = hash_get(zvrf->vxlan_sg_table, vxlan_sg, hash_alloc_intern);
9956
9957 if (IS_ZEBRA_DEBUG_VXLAN)
9958 zlog_debug("vxlan SG %s created", vxlan_sg->sg_str);
9959
9960 return vxlan_sg;
9961 }
9962
9963 static zebra_vxlan_sg_t *zebra_vxlan_sg_find(struct zebra_vrf *zvrf,
9964 struct prefix_sg *sg)
9965 {
9966 zebra_vxlan_sg_t lookup;
9967
9968 lookup.sg = *sg;
9969 return hash_lookup(zvrf->vxlan_sg_table, &lookup);
9970 }
9971
9972 static zebra_vxlan_sg_t *zebra_vxlan_sg_add(struct zebra_vrf *zvrf,
9973 struct prefix_sg *sg)
9974 {
9975 zebra_vxlan_sg_t *vxlan_sg;
9976 zebra_vxlan_sg_t *parent = NULL;
9977 struct in_addr sip;
9978
9979 vxlan_sg = zebra_vxlan_sg_find(zvrf, sg);
9980 if (vxlan_sg)
9981 return vxlan_sg;
9982
9983 /* create a *G entry for every BUM group implicitly -
9984 * 1. The SG entry is used by pimd to setup the vxlan-origination-mroute
9985 * 2. the XG entry is used by pimd to setup the
9986 * vxlan-termination-mroute
9987 */
9988 if (sg->src.s_addr != INADDR_ANY) {
9989 memset(&sip, 0, sizeof(sip));
9990 parent = zebra_vxlan_sg_do_ref(zvrf, sip, sg->grp);
9991 if (!parent)
9992 return NULL;
9993 }
9994
9995 vxlan_sg = zebra_vxlan_sg_new(zvrf, sg);
9996 if (!vxlan_sg) {
9997 if (parent)
9998 zebra_vxlan_sg_do_deref(zvrf, sip, sg->grp);
9999 return vxlan_sg;
10000 }
10001
10002 zebra_vxlan_sg_send(zvrf, sg, vxlan_sg->sg_str,
10003 ZEBRA_VXLAN_SG_ADD);
10004
10005 return vxlan_sg;
10006 }
10007
10008 static void zebra_vxlan_sg_del(zebra_vxlan_sg_t *vxlan_sg)
10009 {
10010 struct in_addr sip;
10011 struct zebra_vrf *zvrf;
10012
10013 zvrf = vrf_info_lookup(VRF_DEFAULT);
10014 if (!zvrf)
10015 return;
10016
10017 /* On SG entry deletion remove the reference to its parent XG
10018 * entry
10019 */
10020 if (vxlan_sg->sg.src.s_addr != INADDR_ANY) {
10021 memset(&sip, 0, sizeof(sip));
10022 zebra_vxlan_sg_do_deref(zvrf, sip, vxlan_sg->sg.grp);
10023 }
10024
10025 zebra_vxlan_sg_send(zvrf, &vxlan_sg->sg,
10026 vxlan_sg->sg_str, ZEBRA_VXLAN_SG_DEL);
10027
10028 hash_release(vxlan_sg->zvrf->vxlan_sg_table, vxlan_sg);
10029
10030 if (IS_ZEBRA_DEBUG_VXLAN)
10031 zlog_debug("VXLAN SG %s deleted", vxlan_sg->sg_str);
10032
10033 XFREE(MTYPE_ZVXLAN_SG, vxlan_sg);
10034 }
10035
10036 static void zebra_vxlan_sg_do_deref(struct zebra_vrf *zvrf,
10037 struct in_addr sip, struct in_addr mcast_grp)
10038 {
10039 zebra_vxlan_sg_t *vxlan_sg;
10040 struct prefix_sg sg;
10041
10042 sg.family = AF_INET;
10043 sg.prefixlen = IPV4_MAX_BYTELEN;
10044 sg.src = sip;
10045 sg.grp = mcast_grp;
10046 vxlan_sg = zebra_vxlan_sg_find(zvrf, &sg);
10047 if (!vxlan_sg)
10048 return;
10049
10050 if (vxlan_sg->ref_cnt)
10051 --vxlan_sg->ref_cnt;
10052
10053 if (!vxlan_sg->ref_cnt)
10054 zebra_vxlan_sg_del(vxlan_sg);
10055 }
10056
10057 static zebra_vxlan_sg_t *zebra_vxlan_sg_do_ref(struct zebra_vrf *zvrf,
10058 struct in_addr sip, struct in_addr mcast_grp)
10059 {
10060 zebra_vxlan_sg_t *vxlan_sg;
10061 struct prefix_sg sg;
10062
10063 sg.family = AF_INET;
10064 sg.prefixlen = IPV4_MAX_BYTELEN;
10065 sg.src = sip;
10066 sg.grp = mcast_grp;
10067 vxlan_sg = zebra_vxlan_sg_add(zvrf, &sg);
10068 if (vxlan_sg)
10069 ++vxlan_sg->ref_cnt;
10070
10071 return vxlan_sg;
10072 }
10073
10074 static void zebra_vxlan_sg_deref(struct in_addr local_vtep_ip,
10075 struct in_addr mcast_grp)
10076 {
10077 struct zebra_vrf *zvrf;
10078
10079 if (local_vtep_ip.s_addr == INADDR_ANY
10080 || mcast_grp.s_addr == INADDR_ANY)
10081 return;
10082
10083 zvrf = vrf_info_lookup(VRF_DEFAULT);
10084 if (!zvrf)
10085 return;
10086
10087 zebra_vxlan_sg_do_deref(zvrf, local_vtep_ip, mcast_grp);
10088 }
10089
10090 static void zebra_vxlan_sg_ref(struct in_addr local_vtep_ip,
10091 struct in_addr mcast_grp)
10092 {
10093 struct zebra_vrf *zvrf;
10094
10095 if (local_vtep_ip.s_addr == INADDR_ANY
10096 || mcast_grp.s_addr == INADDR_ANY)
10097 return;
10098
10099 zvrf = vrf_info_lookup(VRF_DEFAULT);
10100 if (!zvrf)
10101 return;
10102 zebra_vxlan_sg_do_ref(zvrf, local_vtep_ip, mcast_grp);
10103 }
10104
10105 static void zebra_vxlan_sg_cleanup(struct hash_backet *backet, void *arg)
10106 {
10107 zebra_vxlan_sg_t *vxlan_sg = (zebra_vxlan_sg_t *)backet->data;
10108
10109 zebra_vxlan_sg_del(vxlan_sg);
10110 }
10111
10112 static void zebra_vxlan_sg_replay_send(struct hash_backet *backet, void *arg)
10113 {
10114 zebra_vxlan_sg_t *vxlan_sg = (zebra_vxlan_sg_t *)backet->data;
10115
10116 zebra_vxlan_sg_send(vxlan_sg->zvrf, &vxlan_sg->sg,
10117 vxlan_sg->sg_str, ZEBRA_VXLAN_SG_ADD);
10118 }
10119
10120 /* Handle message from client to replay vxlan SG entries */
10121 void zebra_vxlan_sg_replay(ZAPI_HANDLER_ARGS)
10122 {
10123 if (IS_ZEBRA_DEBUG_VXLAN)
10124 zlog_debug("VxLAN SG updates to PIM, start");
10125
10126 SET_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG);
10127
10128 if (!EVPN_ENABLED(zvrf)) {
10129 if (IS_ZEBRA_DEBUG_VXLAN)
10130 zlog_debug("VxLAN SG replay request on unexpected vrf %d",
10131 zvrf->vrf->vrf_id);
10132 return;
10133 }
10134
10135 hash_iterate(zvrf->vxlan_sg_table, zebra_vxlan_sg_replay_send, NULL);
10136 }
10137
10138 /************************** EVPN BGP config management ************************/
10139 /* Notify Local MACs to the clienti, skips GW MAC */
10140 static void zvni_send_mac_hash_entry_to_client(struct hash_bucket *bucket,
10141 void *arg)
10142 {
10143 struct mac_walk_ctx *wctx = arg;
10144 zebra_mac_t *zmac = bucket->data;
10145
10146 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_DEF_GW))
10147 return;
10148
10149 if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL))
10150 zvni_mac_send_add_to_client(wctx->zvni->vni, &zmac->macaddr,
10151 zmac->flags, zmac->loc_seq);
10152 }
10153
10154 /* Iterator to Notify Local MACs of a L2VNI */
10155 static void zvni_send_mac_to_client(zebra_vni_t *zvni)
10156 {
10157 struct mac_walk_ctx wctx;
10158
10159 if (!zvni->mac_table)
10160 return;
10161
10162 memset(&wctx, 0, sizeof(struct mac_walk_ctx));
10163 wctx.zvni = zvni;
10164
10165 hash_iterate(zvni->mac_table, zvni_send_mac_hash_entry_to_client,
10166 &wctx);
10167 }
10168
10169 /* Notify Neighbor entries to the Client, skips the GW entry */
10170 static void zvni_send_neigh_hash_entry_to_client(struct hash_bucket *bucket,
10171 void *arg)
10172 {
10173 struct mac_walk_ctx *wctx = arg;
10174 zebra_neigh_t *zn = bucket->data;
10175 zebra_mac_t *zmac = NULL;
10176
10177 if (CHECK_FLAG(zn->flags, ZEBRA_NEIGH_DEF_GW))
10178 return;
10179
10180 if (CHECK_FLAG(zn->flags, ZEBRA_NEIGH_LOCAL) &&
10181 IS_ZEBRA_NEIGH_ACTIVE(zn)) {
10182 zmac = zvni_mac_lookup(wctx->zvni, &zn->emac);
10183 if (!zmac)
10184 return;
10185
10186 zvni_neigh_send_add_to_client(wctx->zvni->vni, &zn->ip,
10187 &zn->emac, zn->flags,
10188 zn->loc_seq);
10189 }
10190 }
10191
10192 /* Iterator of a specific L2VNI */
10193 static void zvni_send_neigh_to_client(zebra_vni_t *zvni)
10194 {
10195 struct neigh_walk_ctx wctx;
10196
10197 memset(&wctx, 0, sizeof(struct neigh_walk_ctx));
10198 wctx.zvni = zvni;
10199
10200 hash_iterate(zvni->neigh_table, zvni_send_neigh_hash_entry_to_client,
10201 &wctx);
10202 }
10203
10204 static void zvni_evpn_cfg_cleanup(struct hash_bucket *bucket, void *ctxt)
10205 {
10206 zebra_vni_t *zvni = NULL;
10207
10208 zvni = (zebra_vni_t *)bucket->data;
10209 zvni->advertise_gw_macip = 0;
10210 zvni->advertise_svi_macip = 0;
10211 zvni->advertise_subnet = 0;
10212
10213 zvni_neigh_del_all(zvni, 1, 0,
10214 DEL_REMOTE_NEIGH | DEL_REMOTE_NEIGH_FROM_VTEP);
10215 zvni_mac_del_all(zvni, 1, 0,
10216 DEL_REMOTE_MAC | DEL_REMOTE_MAC_FROM_VTEP);
10217 zvni_vtep_del_all(zvni, 1);
10218 }
10219
10220 /* Cleanup EVPN configuration of a specific VRF */
10221 static void zebra_evpn_vrf_cfg_cleanup(struct zebra_vrf *zvrf)
10222 {
10223 zebra_l3vni_t *zl3vni = NULL;
10224
10225 zvrf->advertise_all_vni = 0;
10226 zvrf->advertise_gw_macip = 0;
10227 zvrf->advertise_svi_macip = 0;
10228 zvrf->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
10229
10230 hash_iterate(zvrf->vni_table, zvni_evpn_cfg_cleanup, NULL);
10231
10232 if (zvrf->l3vni)
10233 zl3vni = zl3vni_lookup(zvrf->l3vni);
10234 if (zl3vni) {
10235 /* delete and uninstall all rmacs */
10236 hash_iterate(zl3vni->rmac_table, zl3vni_del_rmac_hash_entry,
10237 zl3vni);
10238 /* delete and uninstall all next-hops */
10239 hash_iterate(zl3vni->nh_table, zl3vni_del_nh_hash_entry,
10240 zl3vni);
10241 }
10242 }
10243
10244 /* Cleanup BGP EVPN configuration upon client disconnect */
10245 static int zebra_evpn_bgp_cfg_clean_up(struct zserv *client)
10246 {
10247 struct vrf *vrf;
10248 struct zebra_vrf *zvrf;
10249
10250 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
10251 zvrf = vrf->info;
10252 if (zvrf)
10253 zebra_evpn_vrf_cfg_cleanup(zvrf);
10254 }
10255
10256 return 0;
10257 }
10258
10259 static int zebra_evpn_pim_cfg_clean_up(struct zserv *client)
10260 {
10261 struct zebra_vrf *zvrf = zebra_vrf_get_evpn();
10262
10263 if (CHECK_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG)) {
10264 if (IS_ZEBRA_DEBUG_VXLAN)
10265 zlog_debug("VxLAN SG updates to PIM, stop");
10266 UNSET_FLAG(zvrf->flags, ZEBRA_PIM_SEND_VXLAN_SG);
10267 }
10268
10269 return 0;
10270 }
10271
10272 static int zebra_evpn_cfg_clean_up(struct zserv *client)
10273 {
10274 if (client->proto == ZEBRA_ROUTE_BGP)
10275 return zebra_evpn_bgp_cfg_clean_up(client);
10276
10277 if (client->proto == ZEBRA_ROUTE_PIM)
10278 return zebra_evpn_pim_cfg_clean_up(client);
10279
10280 return 0;
10281 }
10282
10283 /*
10284 * Handle results for vxlan dataplane operations.
10285 */
10286 extern void zebra_vxlan_handle_result(struct zebra_dplane_ctx *ctx)
10287 {
10288 /* TODO -- anything other than freeing the context? */
10289 dplane_ctx_fini(&ctx);
10290 }
10291
10292 /* Cleanup BGP EVPN configuration upon client disconnect */
10293 extern void zebra_evpn_init(void)
10294 {
10295 hook_register(zserv_client_close, zebra_evpn_cfg_clean_up);
10296 }