]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_evpn.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / zebra / zebra_evpn.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Zebra EVPN for VxLAN code
4 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
5 */
6 #include <zebra.h>
7
8 #include "hash.h"
9 #include "if.h"
10 #include "jhash.h"
11 #include "linklist.h"
12 #include "log.h"
13 #include "memory.h"
14 #include "prefix.h"
15 #include "stream.h"
16 #include "table.h"
17 #include "vlan.h"
18 #include "vxlan.h"
19 #ifdef GNU_LINUX
20 #include <linux/neighbour.h>
21 #endif
22
23 #include "zebra/zebra_router.h"
24 #include "zebra/debug.h"
25 #include "zebra/interface.h"
26 #include "zebra/rib.h"
27 #include "zebra/rt.h"
28 #include "zebra/rt_netlink.h"
29 #include "zebra/zebra_errors.h"
30 #include "zebra/zebra_l2.h"
31 #include "zebra/zebra_ns.h"
32 #include "zebra/zebra_vrf.h"
33 #include "zebra/zebra_vxlan.h"
34 #include "zebra/zebra_evpn.h"
35 #include "zebra/zebra_evpn_mac.h"
36 #include "zebra/zebra_evpn_neigh.h"
37 #include "zebra/zebra_vxlan_private.h"
38 #include "zebra/zebra_evpn_mh.h"
39 #include "zebra/zebra_evpn_vxlan.h"
40 #include "zebra/zebra_router.h"
41
42 DEFINE_MTYPE_STATIC(ZEBRA, ZEVPN, "VNI hash");
43 DEFINE_MTYPE_STATIC(ZEBRA, ZEVPN_VTEP, "VNI remote VTEP");
44
45 /* PMSI strings. */
46 #define VXLAN_FLOOD_STR_NO_INFO "-"
47 #define VXLAN_FLOOD_STR_DEFAULT VXLAN_FLOOD_STR_NO_INFO
48 static const struct message zvtep_flood_str[] = {
49 {VXLAN_FLOOD_DISABLED, VXLAN_FLOOD_STR_NO_INFO},
50 {VXLAN_FLOOD_PIM_SM, "PIM-SM"},
51 {VXLAN_FLOOD_HEAD_END_REPL, "HER"},
52 {0}
53 };
54
55 int advertise_gw_macip_enabled(struct zebra_evpn *zevpn)
56 {
57 struct zebra_vrf *zvrf;
58
59 zvrf = zebra_vrf_get_evpn();
60 if (zvrf->advertise_gw_macip)
61 return 1;
62
63 if (zevpn && zevpn->advertise_gw_macip)
64 return 1;
65
66 return 0;
67 }
68
69 int advertise_svi_macip_enabled(struct zebra_evpn *zevpn)
70 {
71 struct zebra_vrf *zvrf;
72
73 zvrf = zebra_vrf_get_evpn();
74 if (zvrf->advertise_svi_macip)
75 return 1;
76
77 if (zevpn && zevpn->advertise_svi_macip)
78 return 1;
79
80 return 0;
81 }
82
83 /*
84 * Print a specific EVPN entry.
85 */
86 void zebra_evpn_print(struct zebra_evpn *zevpn, void **ctxt)
87 {
88
89 struct vty *vty = NULL;
90 struct zebra_vtep *zvtep = NULL;
91 uint32_t num_macs = 0;
92 uint32_t num_neigh = 0;
93 uint32_t num_vteps = 0;
94 json_object *json = NULL;
95 json_object *json_vtep_list = NULL;
96 json_object *json_vtep = NULL;
97
98 vty = ctxt[0];
99 json = ctxt[1];
100
101 if (json == NULL) {
102 vty_out(vty, "VNI: %u\n", zevpn->vni);
103 vty_out(vty, " Type: %s\n", "L2");
104 vty_out(vty, " Tenant VRF: %s\n", vrf_id_to_name(zevpn->vrf_id));
105 } else {
106 json_object_int_add(json, "vni", zevpn->vni);
107 json_object_string_add(json, "type", "L2");
108 #if CONFDATE > 20240210
109 CPP_NOTICE("Drop `vrf` from JSON output")
110 #endif
111 json_object_string_add(json, "vrf",
112 vrf_id_to_name(zevpn->vrf_id));
113 json_object_string_add(json, "tenantVrf",
114 vrf_id_to_name(zevpn->vrf_id));
115 }
116
117 if (!zevpn->vxlan_if) { // unexpected
118 if (json == NULL)
119 vty_out(vty, " VxLAN interface: unknown\n");
120 else
121 json_object_string_add(json, "vxlanInterface",
122 "unknown");
123 return;
124 }
125 num_macs = num_valid_macs(zevpn);
126 num_neigh = hashcount(zevpn->neigh_table);
127 if (json == NULL) {
128 vty_out(vty, " VxLAN interface: %s\n", zevpn->vxlan_if->name);
129 vty_out(vty, " VxLAN ifIndex: %u\n", zevpn->vxlan_if->ifindex);
130 vty_out(vty, " SVI interface: %s\n",
131 (zevpn->svi_if ? zevpn->svi_if->name : ""));
132 vty_out(vty, " SVI ifIndex: %u\n",
133 (zevpn->svi_if ? zevpn->svi_if->ifindex : 0));
134 vty_out(vty, " Local VTEP IP: %pI4\n",
135 &zevpn->local_vtep_ip);
136 vty_out(vty, " Mcast group: %pI4\n",
137 &zevpn->mcast_grp);
138 } else {
139 json_object_string_add(json, "vxlanInterface",
140 zevpn->vxlan_if->name);
141 #if CONFDATE > 20240210
142 CPP_NOTICE("Drop `ifindex` from JSON output")
143 #endif
144 json_object_int_add(json, "ifindex", zevpn->vxlan_if->ifindex);
145 json_object_int_add(json, "vxlanIfindex",
146 zevpn->vxlan_if->ifindex);
147 if (zevpn->svi_if) {
148 json_object_string_add(json, "sviInterface",
149 zevpn->svi_if->name);
150 json_object_int_add(json, "sviIfindex",
151 zevpn->svi_if->ifindex);
152 }
153 json_object_string_addf(json, "vtepIp", "%pI4",
154 &zevpn->local_vtep_ip);
155 json_object_string_addf(json, "mcastGroup", "%pI4",
156 &zevpn->mcast_grp);
157 json_object_string_add(json, "advertiseGatewayMacip",
158 zevpn->advertise_gw_macip ? "Yes"
159 : "No");
160 json_object_string_add(json, "advertiseSviMacip",
161 zevpn->advertise_svi_macip ? "Yes"
162 : "No");
163 json_object_int_add(json, "numMacs", num_macs);
164 json_object_int_add(json, "numArpNd", num_neigh);
165 }
166 if (!zevpn->vteps) {
167 if (json == NULL)
168 vty_out(vty, " No remote VTEPs known for this VNI\n");
169 else
170 json_object_int_add(json, "numRemoteVteps", num_vteps);
171 } else {
172 if (json == NULL)
173 vty_out(vty, " Remote VTEPs for this VNI:\n");
174 else
175 json_vtep_list = json_object_new_array();
176 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep->next) {
177 const char *flood_str = lookup_msg(
178 zvtep_flood_str, zvtep->flood_control,
179 VXLAN_FLOOD_STR_DEFAULT);
180
181 if (json == NULL) {
182 vty_out(vty, " %pI4 flood: %s\n",
183 &zvtep->vtep_ip,
184 flood_str);
185 } else {
186 json_vtep = json_object_new_object();
187 json_object_string_addf(json_vtep, "ip", "%pI4",
188 &zvtep->vtep_ip);
189 json_object_string_add(json_vtep, "flood",
190 flood_str);
191 json_object_array_add(json_vtep_list,
192 json_vtep);
193 }
194 num_vteps++;
195 }
196 if (json) {
197 json_object_int_add(json, "numRemoteVteps", num_vteps);
198 json_object_object_add(json, "remoteVteps",
199 json_vtep_list);
200 }
201 }
202 if (json == NULL) {
203 vty_out(vty,
204 " Number of MACs (local and remote) known for this VNI: %u\n",
205 num_macs);
206 vty_out(vty,
207 " Number of ARPs (IPv4 and IPv6, local and remote) "
208 "known for this VNI: %u\n",
209 num_neigh);
210 vty_out(vty, " Advertise-gw-macip: %s\n",
211 zevpn->advertise_gw_macip ? "Yes" : "No");
212 vty_out(vty, " Advertise-svi-macip: %s\n",
213 zevpn->advertise_svi_macip ? "Yes" : "No");
214 }
215 }
216
217 /*
218 * Print an EVPN hash entry - called for display of all VNIs.
219 */
220 void zebra_evpn_print_hash(struct hash_bucket *bucket, void *ctxt[])
221 {
222 struct vty *vty;
223 struct zebra_evpn *zevpn;
224 struct zebra_vtep *zvtep;
225 uint32_t num_vteps = 0;
226 uint32_t num_macs = 0;
227 uint32_t num_neigh = 0;
228 json_object *json = NULL;
229 json_object *json_evpn = NULL;
230 json_object *json_ip_str = NULL;
231 json_object *json_vtep_list = NULL;
232 char buf[PREFIX_STRLEN];
233
234 vty = ctxt[0];
235 json = ctxt[1];
236
237 zevpn = (struct zebra_evpn *)bucket->data;
238
239 zvtep = zevpn->vteps;
240 while (zvtep) {
241 num_vteps++;
242 zvtep = zvtep->next;
243 }
244
245 num_macs = num_valid_macs(zevpn);
246 num_neigh = hashcount(zevpn->neigh_table);
247 if (json == NULL)
248 vty_out(vty, "%-10u %-4s %-21s %-8u %-8u %-15u %-37s\n",
249 zevpn->vni, "L2",
250 zevpn->vxlan_if ? zevpn->vxlan_if->name : "unknown",
251 num_macs, num_neigh, num_vteps,
252 vrf_id_to_name(zevpn->vrf_id));
253 else {
254 char vni_str[VNI_STR_LEN];
255 snprintf(vni_str, VNI_STR_LEN, "%u", zevpn->vni);
256 json_evpn = json_object_new_object();
257 json_object_int_add(json_evpn, "vni", zevpn->vni);
258 json_object_string_add(json_evpn, "type", "L2");
259 json_object_string_add(json_evpn, "vxlanIf",
260 zevpn->vxlan_if ? zevpn->vxlan_if->name
261 : "unknown");
262 json_object_int_add(json_evpn, "numMacs", num_macs);
263 json_object_int_add(json_evpn, "numArpNd", num_neigh);
264 json_object_int_add(json_evpn, "numRemoteVteps", num_vteps);
265 json_object_string_add(json_evpn, "tenantVrf",
266 vrf_id_to_name(zevpn->vrf_id));
267 if (num_vteps) {
268 json_vtep_list = json_object_new_array();
269 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep->next) {
270 json_ip_str = json_object_new_string(
271 inet_ntop(AF_INET, &zvtep->vtep_ip, buf,
272 sizeof(buf)));
273 json_object_array_add(json_vtep_list,
274 json_ip_str);
275 }
276 json_object_object_add(json_evpn, "remoteVteps",
277 json_vtep_list);
278 }
279 json_object_object_add(json, vni_str, json_evpn);
280 }
281 }
282
283 /*
284 * Print an EVPN hash entry in detail - called for display of all EVPNs.
285 */
286 void zebra_evpn_print_hash_detail(struct hash_bucket *bucket, void *data)
287 {
288 struct vty *vty;
289 struct zebra_evpn *zevpn;
290 json_object *json_array = NULL;
291 bool use_json = false;
292 struct zebra_evpn_show *zes = data;
293
294 vty = zes->vty;
295 json_array = zes->json;
296 use_json = zes->use_json;
297
298 zevpn = (struct zebra_evpn *)bucket->data;
299
300 zebra_vxlan_print_vni(vty, zes->zvrf, zevpn->vni, use_json, json_array);
301
302 if (!use_json)
303 vty_out(vty, "\n");
304 }
305
306 int zebra_evpn_del_macip_for_intf(struct interface *ifp,
307 struct zebra_evpn *zevpn)
308 {
309 struct listnode *cnode = NULL, *cnnode = NULL;
310 struct connected *c = NULL;
311 struct ethaddr macaddr;
312
313 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
314
315 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
316 struct ipaddr ip;
317
318 memset(&ip, 0, sizeof(struct ipaddr));
319 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
320 continue;
321
322 if (c->address->family == AF_INET) {
323 ip.ipa_type = IPADDR_V4;
324 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
325 sizeof(struct in_addr));
326 } else if (c->address->family == AF_INET6) {
327 ip.ipa_type = IPADDR_V6;
328 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
329 sizeof(struct in6_addr));
330 } else {
331 continue;
332 }
333
334 zebra_evpn_gw_macip_del(ifp, zevpn, &ip);
335 }
336
337 return 0;
338 }
339
340 int zebra_evpn_add_macip_for_intf(struct interface *ifp,
341 struct zebra_evpn *zevpn)
342 {
343 struct listnode *cnode = NULL, *cnnode = NULL;
344 struct connected *c = NULL;
345 struct ethaddr macaddr;
346
347 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
348
349 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
350 struct ipaddr ip;
351
352 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
353 continue;
354
355 memset(&ip, 0, sizeof(struct ipaddr));
356 if (c->address->family == AF_INET) {
357 ip.ipa_type = IPADDR_V4;
358 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
359 sizeof(struct in_addr));
360 } else if (c->address->family == AF_INET6) {
361 ip.ipa_type = IPADDR_V6;
362 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
363 sizeof(struct in6_addr));
364 } else {
365 continue;
366 }
367
368 zebra_evpn_gw_macip_add(ifp, zevpn, &macaddr, &ip);
369 }
370 return 0;
371 }
372
373 static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
374 uint16_t cmd)
375 {
376 struct zserv *client = NULL;
377 struct stream *s = NULL;
378
379 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
380 /* BGP may not be running. */
381 if (!client)
382 return 0;
383
384 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
385
386 zclient_create_header(s, cmd, vrf_id);
387 stream_put(s, p, sizeof(struct prefix));
388
389 /* Write packet size. */
390 stream_putw_at(s, 0, stream_get_endp(s));
391
392 if (IS_ZEBRA_DEBUG_VXLAN)
393 zlog_debug("Send ip prefix %pFX %s on vrf %s", p,
394 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
395 vrf_id_to_name(vrf_id));
396
397 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD)
398 client->prefixadd_cnt++;
399 else
400 client->prefixdel_cnt++;
401
402 return zserv_send_message(client, s);
403 }
404
405 int zebra_evpn_advertise_subnet(struct zebra_evpn *zevpn, struct interface *ifp,
406 int advertise)
407 {
408 struct listnode *cnode = NULL, *cnnode = NULL;
409 struct connected *c = NULL;
410 struct ethaddr macaddr;
411
412 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
413
414 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
415 struct prefix p;
416
417 memcpy(&p, c->address, sizeof(struct prefix));
418
419 /* skip link local address */
420 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
421 continue;
422
423 apply_mask(&p);
424 if (advertise)
425 ip_prefix_send_to_client(ifp->vrf->vrf_id, &p,
426 ZEBRA_IP_PREFIX_ROUTE_ADD);
427 else
428 ip_prefix_send_to_client(ifp->vrf->vrf_id, &p,
429 ZEBRA_IP_PREFIX_ROUTE_DEL);
430 }
431 return 0;
432 }
433
434 /*
435 * zebra_evpn_gw_macip_add_to_client
436 */
437 int zebra_evpn_gw_macip_add(struct interface *ifp, struct zebra_evpn *zevpn,
438 struct ethaddr *macaddr, struct ipaddr *ip)
439 {
440 struct zebra_mac *mac = NULL;
441 struct zebra_if *zif = NULL;
442 struct zebra_l2info_vxlan *vxl = NULL;
443
444 zif = zevpn->vxlan_if->info;
445 if (!zif)
446 return -1;
447
448 vxl = &zif->l2info.vxl;
449
450 zebra_evpn_mac_gw_macip_add(ifp, zevpn, ip, &mac, macaddr,
451 vxl->access_vlan, true);
452
453 return zebra_evpn_neigh_gw_macip_add(ifp, zevpn, ip, mac);
454 }
455
456 /*
457 * zebra_evpn_gw_macip_del_from_client
458 */
459 int zebra_evpn_gw_macip_del(struct interface *ifp, struct zebra_evpn *zevpn,
460 struct ipaddr *ip)
461 {
462 struct zebra_neigh *n = NULL;
463 struct zebra_mac *mac = NULL;
464
465 /* If the neigh entry is not present nothing to do*/
466 n = zebra_evpn_neigh_lookup(zevpn, ip);
467 if (!n)
468 return 0;
469
470 /* mac entry should be present */
471 mac = zebra_evpn_mac_lookup(zevpn, &n->emac);
472 if (!mac) {
473 if (IS_ZEBRA_DEBUG_VXLAN)
474 zlog_debug("MAC %pEA doesn't exist for neigh %pIA on VNI %u",
475 &n->emac, ip, zevpn->vni);
476 return -1;
477 }
478
479 /* If the entry is not local nothing to do*/
480 if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL))
481 return -1;
482
483 /* only need to delete the entry from bgp if we sent it before */
484 if (IS_ZEBRA_DEBUG_VXLAN)
485 zlog_debug(
486 "%u:SVI %s(%u) VNI %u, sending GW MAC %pEA IP %pIA del to BGP",
487 ifp->vrf->vrf_id, ifp->name, ifp->ifindex, zevpn->vni,
488 &n->emac, ip);
489
490 /* Remove neighbor from BGP. */
491 zebra_evpn_neigh_send_del_to_client(zevpn->vni, &n->ip, &n->emac,
492 n->flags, ZEBRA_NEIGH_ACTIVE,
493 false /*force*/);
494
495 /* Delete this neighbor entry. */
496 zebra_evpn_neigh_del(zevpn, n);
497
498 /* see if the mac needs to be deleted as well*/
499 if (mac)
500 zebra_evpn_deref_ip2mac(zevpn, mac);
501
502 return 0;
503 }
504
505 void zebra_evpn_gw_macip_del_for_evpn_hash(struct hash_bucket *bucket,
506 void *ctxt)
507 {
508 struct zebra_evpn *zevpn = NULL;
509 struct zebra_if *zif = NULL;
510 struct zebra_l2info_vxlan zl2_info;
511 struct interface *vlan_if = NULL;
512 struct interface *vrr_if = NULL;
513 struct interface *ifp;
514
515 /* Add primary SVI MAC*/
516 zevpn = (struct zebra_evpn *)bucket->data;
517
518 /* Global (Zvrf) advertise-default-gw is disabled,
519 * but zevpn advertise-default-gw is enabled
520 */
521 if (zevpn->advertise_gw_macip) {
522 if (IS_ZEBRA_DEBUG_VXLAN)
523 zlog_debug("VNI: %u GW-MACIP enabled, retain gw-macip",
524 zevpn->vni);
525 return;
526 }
527
528 ifp = zevpn->vxlan_if;
529 if (!ifp)
530 return;
531 zif = ifp->info;
532
533 /* If down or not mapped to a bridge, we're done. */
534 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
535 return;
536
537 zl2_info = zif->l2info.vxl;
538
539 vlan_if =
540 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
541 if (!vlan_if)
542 return;
543
544 /* Del primary MAC-IP */
545 zebra_evpn_del_macip_for_intf(vlan_if, zevpn);
546
547 /* Del VRR MAC-IP - if any*/
548 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
549 if (vrr_if)
550 zebra_evpn_del_macip_for_intf(vrr_if, zevpn);
551
552 return;
553 }
554
555 void zebra_evpn_gw_macip_add_for_evpn_hash(struct hash_bucket *bucket,
556 void *ctxt)
557 {
558 struct zebra_evpn *zevpn = NULL;
559 struct zebra_if *zif = NULL;
560 struct zebra_l2info_vxlan zl2_info;
561 struct interface *vlan_if = NULL;
562 struct interface *vrr_if = NULL;
563 struct interface *ifp = NULL;
564
565 zevpn = (struct zebra_evpn *)bucket->data;
566
567 ifp = zevpn->vxlan_if;
568 if (!ifp)
569 return;
570 zif = ifp->info;
571
572 /* If down or not mapped to a bridge, we're done. */
573 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
574 return;
575 zl2_info = zif->l2info.vxl;
576
577 vlan_if =
578 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
579 if (!vlan_if)
580 return;
581
582 /* Add primary SVI MAC-IP */
583 if (advertise_svi_macip_enabled(zevpn)
584 || advertise_gw_macip_enabled(zevpn))
585 zebra_evpn_add_macip_for_intf(vlan_if, zevpn);
586
587 if (advertise_gw_macip_enabled(zevpn)) {
588 /* Add VRR MAC-IP - if any*/
589 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
590 if (vrr_if)
591 zebra_evpn_add_macip_for_intf(vrr_if, zevpn);
592 }
593
594 return;
595 }
596
597 void zebra_evpn_svi_macip_del_for_evpn_hash(struct hash_bucket *bucket,
598 void *ctxt)
599 {
600 struct zebra_evpn *zevpn = NULL;
601 struct zebra_if *zif = NULL;
602 struct zebra_l2info_vxlan zl2_info;
603 struct interface *vlan_if = NULL;
604 struct interface *ifp;
605
606 /* Add primary SVI MAC*/
607 zevpn = (struct zebra_evpn *)bucket->data;
608 if (!zevpn)
609 return;
610
611 /* Global(vrf) advertise-svi-ip disabled, but zevpn advertise-svi-ip
612 * enabled
613 */
614 if (zevpn->advertise_svi_macip) {
615 if (IS_ZEBRA_DEBUG_VXLAN)
616 zlog_debug("VNI: %u SVI-MACIP enabled, retain svi-macip",
617 zevpn->vni);
618 return;
619 }
620
621 ifp = zevpn->vxlan_if;
622 if (!ifp)
623 return;
624 zif = ifp->info;
625
626 /* If down or not mapped to a bridge, we're done. */
627 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
628 return;
629
630 zl2_info = zif->l2info.vxl;
631
632 vlan_if =
633 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
634 if (!vlan_if)
635 return;
636
637 /* Del primary MAC-IP */
638 zebra_evpn_del_macip_for_intf(vlan_if, zevpn);
639
640 return;
641 }
642
643 static int zebra_evpn_map_vlan_ns(struct ns *ns,
644 void *_in_param,
645 void **_p_zevpn)
646 {
647 struct zebra_ns *zns = ns->info;
648 struct route_node *rn;
649 struct interface *br_if;
650 struct zebra_evpn **p_zevpn = (struct zebra_evpn **)_p_zevpn;
651 struct zebra_evpn *zevpn;
652 struct interface *tmp_if = NULL;
653 struct zebra_if *zif;
654 struct zebra_l2info_vxlan *vxl = NULL;
655 struct zebra_from_svi_param *in_param =
656 (struct zebra_from_svi_param *)_in_param;
657
658 assert(p_zevpn && in_param);
659
660 br_if = in_param->br_if;
661 zif = in_param->zif;
662 assert(zif);
663 assert(br_if);
664
665 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
666 /* TODO: Optimize with a hash. */
667 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
668 tmp_if = (struct interface *)rn->info;
669 if (!tmp_if)
670 continue;
671 zif = tmp_if->info;
672 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
673 continue;
674 if (!if_is_operative(tmp_if))
675 continue;
676 vxl = &zif->l2info.vxl;
677
678 if (zif->brslave_info.br_if != br_if)
679 continue;
680
681 if (!in_param->bridge_vlan_aware
682 || vxl->access_vlan == in_param->vid) {
683 zevpn = zebra_evpn_lookup(vxl->vni);
684 *p_zevpn = zevpn;
685 return NS_WALK_STOP;
686 }
687 }
688
689 return NS_WALK_CONTINUE;
690 }
691
692 /*
693 * Map port or (port, VLAN) to an EVPN. This is invoked upon getting MAC
694 * notifications, to see if they are of interest.
695 */
696 struct zebra_evpn *zebra_evpn_map_vlan(struct interface *ifp,
697 struct interface *br_if, vlanid_t vid)
698 {
699 struct zebra_if *zif;
700 struct zebra_l2info_bridge *br;
701 struct zebra_evpn **p_zevpn;
702 struct zebra_evpn *zevpn = NULL;
703 struct zebra_from_svi_param in_param;
704
705 /* Determine if bridge is VLAN-aware or not */
706 zif = br_if->info;
707 assert(zif);
708 br = &zif->l2info.br;
709 in_param.bridge_vlan_aware = br->vlan_aware;
710 in_param.vid = vid;
711 in_param.br_if = br_if;
712 in_param.zif = zif;
713 p_zevpn = &zevpn;
714
715 ns_walk_func(zebra_evpn_map_vlan_ns,
716 (void *)&in_param,
717 (void **)p_zevpn);
718 return zevpn;
719 }
720
721 static int zebra_evpn_from_svi_ns(struct ns *ns,
722 void *_in_param,
723 void **_p_zevpn)
724 {
725 struct zebra_ns *zns = ns->info;
726 struct route_node *rn;
727 struct interface *br_if;
728 struct zebra_evpn **p_zevpn = (struct zebra_evpn **)_p_zevpn;
729 struct zebra_evpn *zevpn;
730 struct interface *tmp_if = NULL;
731 struct zebra_if *zif;
732 struct zebra_l2info_vxlan *vxl = NULL;
733 struct zebra_from_svi_param *in_param =
734 (struct zebra_from_svi_param *)_in_param;
735 int found = 0;
736
737 if (!in_param)
738 return NS_WALK_STOP;
739 br_if = in_param->br_if;
740 zif = in_param->zif;
741 assert(zif);
742
743 /* TODO: Optimize with a hash. */
744 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
745 tmp_if = (struct interface *)rn->info;
746 if (!tmp_if)
747 continue;
748 zif = tmp_if->info;
749 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
750 continue;
751 if (!if_is_operative(tmp_if))
752 continue;
753 vxl = &zif->l2info.vxl;
754
755 if (zif->brslave_info.br_if != br_if)
756 continue;
757
758 if (!in_param->bridge_vlan_aware
759 || vxl->access_vlan == in_param->vid) {
760 found = 1;
761 break;
762 }
763 }
764
765 if (!found)
766 return NS_WALK_CONTINUE;
767
768 zevpn = zebra_evpn_lookup(vxl->vni);
769 if (p_zevpn)
770 *p_zevpn = zevpn;
771 return NS_WALK_STOP;
772 }
773
774 /*
775 * Map SVI and associated bridge to an EVPN. This is invoked upon getting
776 * neighbor notifications, to see if they are of interest.
777 */
778 struct zebra_evpn *zebra_evpn_from_svi(struct interface *ifp,
779 struct interface *br_if)
780 {
781 struct zebra_l2info_bridge *br;
782 struct zebra_evpn *zevpn = NULL;
783 struct zebra_evpn **p_zevpn;
784 struct zebra_if *zif;
785 struct zebra_from_svi_param in_param;
786
787 if (!br_if)
788 return NULL;
789
790 /* Make sure the linked interface is a bridge. */
791 if (!IS_ZEBRA_IF_BRIDGE(br_if))
792 return NULL;
793
794 /* Determine if bridge is VLAN-aware or not */
795 zif = br_if->info;
796 assert(zif);
797 br = &zif->l2info.br;
798 in_param.bridge_vlan_aware = br->vlan_aware;
799 in_param.vid = 0;
800
801 if (in_param.bridge_vlan_aware) {
802 struct zebra_l2info_vlan *vl;
803
804 if (!IS_ZEBRA_IF_VLAN(ifp))
805 return NULL;
806
807 zif = ifp->info;
808 assert(zif);
809 vl = &zif->l2info.vl;
810 in_param.vid = vl->vid;
811 }
812
813 in_param.br_if = br_if;
814 in_param.zif = zif;
815 p_zevpn = &zevpn;
816 /* See if this interface (or interface plus VLAN Id) maps to a VxLAN */
817 ns_walk_func(zebra_evpn_from_svi_ns, (void *)&in_param,
818 (void **)p_zevpn);
819 return zevpn;
820 }
821
822 static int zvni_map_to_macvlan_ns(struct ns *ns,
823 void *_in_param,
824 void **_p_ifp)
825 {
826 struct zebra_ns *zns = ns->info;
827 struct zebra_from_svi_param *in_param =
828 (struct zebra_from_svi_param *)_in_param;
829 struct interface **p_ifp = (struct interface **)_p_ifp;
830 struct route_node *rn;
831 struct interface *tmp_if = NULL;
832 struct zebra_if *zif;
833
834 assert(in_param && p_ifp);
835
836 /* Identify corresponding VLAN interface. */
837 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
838 tmp_if = (struct interface *)rn->info;
839 /* Check oper status of the SVI. */
840 if (!tmp_if || !if_is_operative(tmp_if))
841 continue;
842 zif = tmp_if->info;
843
844 if (!zif || zif->zif_type != ZEBRA_IF_MACVLAN)
845 continue;
846
847 if (zif->link == in_param->svi_if) {
848 *p_ifp = tmp_if;
849 return NS_WALK_STOP;
850 }
851 }
852
853 return NS_WALK_CONTINUE;
854 }
855
856 /* Map to MAC-VLAN interface corresponding to specified SVI interface.
857 */
858 struct interface *zebra_evpn_map_to_macvlan(struct interface *br_if,
859 struct interface *svi_if)
860 {
861 struct interface *tmp_if = NULL;
862 struct zebra_if *zif;
863 struct interface **p_ifp;
864 struct zebra_from_svi_param in_param;
865
866 /* Defensive check, caller expected to invoke only with valid bridge. */
867 if (!br_if)
868 return NULL;
869
870 if (!svi_if) {
871 zlog_debug("svi_if is not passed.");
872 return NULL;
873 }
874
875 /* Determine if bridge is VLAN-aware or not */
876 zif = br_if->info;
877 assert(zif);
878
879 in_param.vid = 0;
880 in_param.br_if = br_if;
881 in_param.zif = NULL;
882 in_param.svi_if = svi_if;
883 p_ifp = &tmp_if;
884
885 /* Identify corresponding VLAN interface. */
886 ns_walk_func(zvni_map_to_macvlan_ns,
887 (void *)&in_param,
888 (void **)p_ifp);
889 return tmp_if;
890 }
891
892 /*
893 * Install MAC hash entry - called upon access VLAN change.
894 */
895 void zebra_evpn_install_mac_hash(struct hash_bucket *bucket, void *ctxt)
896 {
897 struct zebra_mac *mac;
898 struct mac_walk_ctx *wctx = ctxt;
899
900 mac = (struct zebra_mac *)bucket->data;
901
902 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE))
903 zebra_evpn_rem_mac_install(wctx->zevpn, mac, false);
904 }
905
906 /*
907 * Read and populate local MACs and neighbors corresponding to this EVPN.
908 */
909 void zebra_evpn_read_mac_neigh(struct zebra_evpn *zevpn, struct interface *ifp)
910 {
911 struct zebra_ns *zns;
912 struct zebra_vrf *zvrf;
913 struct zebra_if *zif;
914 struct interface *vlan_if;
915 struct zebra_l2info_vxlan *vxl;
916 struct interface *vrr_if;
917
918 zif = ifp->info;
919 vxl = &zif->l2info.vxl;
920 zvrf = zebra_vrf_lookup_by_id(zevpn->vrf_id);
921 if (!zvrf || !zvrf->zns)
922 return;
923 zns = zvrf->zns;
924
925 if (IS_ZEBRA_DEBUG_VXLAN)
926 zlog_debug(
927 "Reading MAC FDB and Neighbors for intf %s(%u) VNI %u master %u",
928 ifp->name, ifp->ifindex, zevpn->vni,
929 zif->brslave_info.bridge_ifindex);
930
931 macfdb_read_for_bridge(zns, ifp, zif->brslave_info.br_if);
932 vlan_if = zvni_map_to_svi(vxl->access_vlan, zif->brslave_info.br_if);
933 if (vlan_if) {
934 /* Add SVI MAC */
935 zebra_evpn_acc_bd_svi_mac_add(vlan_if);
936
937 /* Add SVI MAC-IP */
938 if (advertise_svi_macip_enabled(zevpn)
939 || advertise_gw_macip_enabled(zevpn))
940 zebra_evpn_add_macip_for_intf(vlan_if, zevpn);
941
942 /* Add VRR MAC-IP - if any*/
943 if (advertise_gw_macip_enabled(zevpn)) {
944 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
945 if (vrr_if)
946 zebra_evpn_add_macip_for_intf(vrr_if, zevpn);
947 }
948
949 neigh_read_for_vlan(zns, vlan_if);
950 }
951 }
952
953 /*
954 * Hash function for EVPN.
955 */
956 unsigned int zebra_evpn_hash_keymake(const void *p)
957 {
958 const struct zebra_evpn *zevpn = p;
959
960 return (jhash_1word(zevpn->vni, 0));
961 }
962
963 /*
964 * Compare 2 evpn hash entries.
965 */
966 bool zebra_evpn_hash_cmp(const void *p1, const void *p2)
967 {
968 const struct zebra_evpn *zevpn1 = p1;
969 const struct zebra_evpn *zevpn2 = p2;
970
971 return (zevpn1->vni == zevpn2->vni);
972 }
973
974 int zebra_evpn_list_cmp(void *p1, void *p2)
975 {
976 const struct zebra_evpn *zevpn1 = p1;
977 const struct zebra_evpn *zevpn2 = p2;
978
979 if (zevpn1->vni == zevpn2->vni)
980 return 0;
981 return (zevpn1->vni < zevpn2->vni) ? -1 : 1;
982 }
983
984 /*
985 * Callback to allocate VNI hash entry.
986 */
987 void *zebra_evpn_alloc(void *p)
988 {
989 const struct zebra_evpn *tmp_vni = p;
990 struct zebra_evpn *zevpn;
991
992 zevpn = XCALLOC(MTYPE_ZEVPN, sizeof(struct zebra_evpn));
993 zevpn->vni = tmp_vni->vni;
994 return ((void *)zevpn);
995 }
996
997 /*
998 * Look up EVPN hash entry.
999 */
1000 struct zebra_evpn *zebra_evpn_lookup(vni_t vni)
1001 {
1002 struct zebra_vrf *zvrf;
1003 struct zebra_evpn tmp_vni;
1004 struct zebra_evpn *zevpn = NULL;
1005
1006 zvrf = zebra_vrf_get_evpn();
1007 memset(&tmp_vni, 0, sizeof(tmp_vni));
1008 tmp_vni.vni = vni;
1009 zevpn = hash_lookup(zvrf->evpn_table, &tmp_vni);
1010
1011 return zevpn;
1012 }
1013
1014 /*
1015 * Add EVPN hash entry.
1016 */
1017 struct zebra_evpn *zebra_evpn_add(vni_t vni)
1018 {
1019 char buffer[80];
1020 struct zebra_vrf *zvrf;
1021 struct zebra_evpn tmp_zevpn;
1022 struct zebra_evpn *zevpn = NULL;
1023
1024 zvrf = zebra_vrf_get_evpn();
1025 memset(&tmp_zevpn, 0, sizeof(tmp_zevpn));
1026 tmp_zevpn.vni = vni;
1027 zevpn = hash_get(zvrf->evpn_table, &tmp_zevpn, zebra_evpn_alloc);
1028
1029 zebra_evpn_es_evi_init(zevpn);
1030
1031 snprintf(buffer, sizeof(buffer), "Zebra EVPN MAC Table vni: %u", vni);
1032 /* Create hash table for MAC */
1033 zevpn->mac_table = zebra_mac_db_create(buffer);
1034
1035 snprintf(buffer, sizeof(buffer), "Zebra EVPN Neighbor Table vni: %u",
1036 vni);
1037 /* Create hash table for neighbors */
1038 zevpn->neigh_table = zebra_neigh_db_create(buffer);
1039
1040 return zevpn;
1041 }
1042
1043 /*
1044 * Delete EVPN hash entry.
1045 */
1046 int zebra_evpn_del(struct zebra_evpn *zevpn)
1047 {
1048 struct zebra_vrf *zvrf;
1049 struct zebra_evpn *tmp_zevpn;
1050
1051 zvrf = zebra_vrf_get_evpn();
1052
1053 zevpn->svi_if = NULL;
1054
1055 /* Free the neighbor hash table. */
1056 hash_free(zevpn->neigh_table);
1057 zevpn->neigh_table = NULL;
1058
1059 /* Free the MAC hash table. */
1060 hash_free(zevpn->mac_table);
1061 zevpn->mac_table = NULL;
1062
1063 /* Remove references to the zevpn in the MH databases */
1064 if (zevpn->vxlan_if)
1065 zebra_evpn_vxl_evpn_set(zevpn->vxlan_if->info, zevpn, false);
1066 zebra_evpn_es_evi_cleanup(zevpn);
1067
1068 /* Free the EVPN hash entry and allocated memory. */
1069 tmp_zevpn = hash_release(zvrf->evpn_table, zevpn);
1070 XFREE(MTYPE_ZEVPN, tmp_zevpn);
1071
1072 return 0;
1073 }
1074
1075 /*
1076 * Inform BGP about local EVPN addition.
1077 */
1078 int zebra_evpn_send_add_to_client(struct zebra_evpn *zevpn)
1079 {
1080 struct zserv *client;
1081 struct stream *s;
1082 ifindex_t svi_index;
1083 int rc;
1084
1085 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
1086 /* BGP may not be running. */
1087 if (!client)
1088 return 0;
1089
1090 svi_index = zevpn->svi_if ? zevpn->svi_if->ifindex : 0;
1091
1092 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1093
1094 zclient_create_header(s, ZEBRA_VNI_ADD, zebra_vrf_get_evpn_id());
1095 stream_putl(s, zevpn->vni);
1096 stream_put_in_addr(s, &zevpn->local_vtep_ip);
1097 stream_put(s, &zevpn->vrf_id, sizeof(vrf_id_t)); /* tenant vrf */
1098 stream_put_in_addr(s, &zevpn->mcast_grp);
1099 stream_put(s, &svi_index, sizeof(ifindex_t));
1100
1101 /* Write packet size. */
1102 stream_putw_at(s, 0, stream_get_endp(s));
1103
1104 if (IS_ZEBRA_DEBUG_VXLAN)
1105 zlog_debug(
1106 "Send EVPN_ADD %u %pI4 tenant vrf %s(%u) SVI index %u to %s",
1107 zevpn->vni, &zevpn->local_vtep_ip,
1108 vrf_id_to_name(zevpn->vrf_id), zevpn->vrf_id,
1109 (zevpn->svi_if ? zevpn->svi_if->ifindex : 0),
1110 zebra_route_string(client->proto));
1111
1112 client->vniadd_cnt++;
1113 rc = zserv_send_message(client, s);
1114
1115 if (!(zevpn->flags & ZEVPN_READY_FOR_BGP)) {
1116 zevpn->flags |= ZEVPN_READY_FOR_BGP;
1117 /* once the EVPN is sent the ES-EVIs can also be replayed
1118 * to BGP
1119 */
1120 zebra_evpn_update_all_es(zevpn);
1121 }
1122 return rc;
1123 }
1124
1125 /*
1126 * Inform BGP about local EVPN deletion.
1127 */
1128 int zebra_evpn_send_del_to_client(struct zebra_evpn *zevpn)
1129 {
1130 struct zserv *client;
1131 struct stream *s;
1132
1133 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
1134 /* BGP may not be running. */
1135 if (!client)
1136 return 0;
1137
1138 if (zevpn->flags & ZEVPN_READY_FOR_BGP) {
1139 zevpn->flags &= ~ZEVPN_READY_FOR_BGP;
1140 /* the ES-EVIs must be removed from BGP before the EVPN is */
1141 zebra_evpn_update_all_es(zevpn);
1142 }
1143
1144 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1145 stream_reset(s);
1146
1147 zclient_create_header(s, ZEBRA_VNI_DEL, zebra_vrf_get_evpn_id());
1148 stream_putl(s, zevpn->vni);
1149
1150 /* Write packet size. */
1151 stream_putw_at(s, 0, stream_get_endp(s));
1152
1153 if (IS_ZEBRA_DEBUG_VXLAN)
1154 zlog_debug("Send EVPN_DEL %u to %s", zevpn->vni,
1155 zebra_route_string(client->proto));
1156
1157 client->vnidel_cnt++;
1158 return zserv_send_message(client, s);
1159 }
1160
1161 /*
1162 * See if remote VTEP matches with prefix.
1163 */
1164 static int zebra_evpn_vtep_match(struct in_addr *vtep_ip,
1165 struct zebra_vtep *zvtep)
1166 {
1167 return (IPV4_ADDR_SAME(vtep_ip, &zvtep->vtep_ip));
1168 }
1169
1170 /*
1171 * Locate remote VTEP in EVPN hash table.
1172 */
1173 struct zebra_vtep *zebra_evpn_vtep_find(struct zebra_evpn *zevpn,
1174 struct in_addr *vtep_ip)
1175 {
1176 struct zebra_vtep *zvtep;
1177
1178 if (!zevpn)
1179 return NULL;
1180
1181 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep->next) {
1182 if (zebra_evpn_vtep_match(vtep_ip, zvtep))
1183 break;
1184 }
1185
1186 return zvtep;
1187 }
1188
1189 /*
1190 * Add remote VTEP to EVPN hash table.
1191 */
1192 struct zebra_vtep *zebra_evpn_vtep_add(struct zebra_evpn *zevpn,
1193 struct in_addr *vtep_ip,
1194 int flood_control)
1195
1196 {
1197 struct zebra_vtep *zvtep;
1198
1199 zvtep = XCALLOC(MTYPE_ZEVPN_VTEP, sizeof(struct zebra_vtep));
1200
1201 zvtep->vtep_ip = *vtep_ip;
1202 zvtep->flood_control = flood_control;
1203
1204 if (zevpn->vteps)
1205 zevpn->vteps->prev = zvtep;
1206 zvtep->next = zevpn->vteps;
1207 zevpn->vteps = zvtep;
1208
1209 return zvtep;
1210 }
1211
1212 /*
1213 * Remove remote VTEP from EVPN hash table.
1214 */
1215 int zebra_evpn_vtep_del(struct zebra_evpn *zevpn, struct zebra_vtep *zvtep)
1216 {
1217 if (zvtep->next)
1218 zvtep->next->prev = zvtep->prev;
1219 if (zvtep->prev)
1220 zvtep->prev->next = zvtep->next;
1221 else
1222 zevpn->vteps = zvtep->next;
1223
1224 zvtep->prev = zvtep->next = NULL;
1225 XFREE(MTYPE_ZEVPN_VTEP, zvtep);
1226
1227 return 0;
1228 }
1229
1230 /*
1231 * Delete all remote VTEPs for this EVPN (upon VNI delete). Also
1232 * uninstall from kernel if asked to.
1233 */
1234 int zebra_evpn_vtep_del_all(struct zebra_evpn *zevpn, int uninstall)
1235 {
1236 struct zebra_vtep *zvtep, *zvtep_next;
1237
1238 if (!zevpn)
1239 return -1;
1240
1241 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep_next) {
1242 zvtep_next = zvtep->next;
1243 if (uninstall)
1244 zebra_evpn_vtep_uninstall(zevpn, &zvtep->vtep_ip);
1245 zebra_evpn_vtep_del(zevpn, zvtep);
1246 }
1247
1248 return 0;
1249 }
1250
1251 /*
1252 * Install remote VTEP into the kernel if the remote VTEP has asked
1253 * for head-end-replication.
1254 */
1255 int zebra_evpn_vtep_install(struct zebra_evpn *zevpn, struct zebra_vtep *zvtep)
1256 {
1257 if (is_vxlan_flooding_head_end() &&
1258 (zvtep->flood_control == VXLAN_FLOOD_HEAD_END_REPL)) {
1259 if (ZEBRA_DPLANE_REQUEST_FAILURE ==
1260 dplane_vtep_add(zevpn->vxlan_if,
1261 &zvtep->vtep_ip, zevpn->vni))
1262 return -1;
1263 }
1264
1265 return 0;
1266 }
1267
1268 /*
1269 * Uninstall remote VTEP from the kernel.
1270 */
1271 int zebra_evpn_vtep_uninstall(struct zebra_evpn *zevpn, struct in_addr *vtep_ip)
1272 {
1273 if (!zevpn->vxlan_if) {
1274 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
1275 zevpn->vni, zevpn);
1276 return -1;
1277 }
1278
1279 if (ZEBRA_DPLANE_REQUEST_FAILURE ==
1280 dplane_vtep_delete(zevpn->vxlan_if, vtep_ip, zevpn->vni))
1281 return -1;
1282
1283 return 0;
1284 }
1285
1286 /*
1287 * Install or uninstall flood entries in the kernel corresponding to
1288 * remote VTEPs. This is invoked upon change to BUM handling.
1289 */
1290 void zebra_evpn_handle_flooding_remote_vteps(struct hash_bucket *bucket,
1291 void *zvrf)
1292 {
1293 struct zebra_evpn *zevpn;
1294 struct zebra_vtep *zvtep;
1295
1296 zevpn = (struct zebra_evpn *)bucket->data;
1297 if (!zevpn)
1298 return;
1299
1300 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep->next) {
1301 if (is_vxlan_flooding_head_end())
1302 zebra_evpn_vtep_install(zevpn, zvtep);
1303 else
1304 zebra_evpn_vtep_uninstall(zevpn, &zvtep->vtep_ip);
1305 }
1306 }
1307
1308 /*
1309 * Cleanup EVPN/VTEP and update kernel
1310 */
1311 void zebra_evpn_cleanup_all(struct hash_bucket *bucket, void *arg)
1312 {
1313 struct zebra_evpn *zevpn = NULL;
1314
1315 zevpn = (struct zebra_evpn *)bucket->data;
1316
1317 /* Free up all neighbors and MACs, if any. */
1318 zebra_evpn_neigh_del_all(zevpn, 1, 0, DEL_ALL_NEIGH);
1319 zebra_evpn_mac_del_all(zevpn, 1, 0, DEL_ALL_MAC);
1320
1321 /* Free up all remote VTEPs, if any. */
1322 zebra_evpn_vtep_del_all(zevpn, 1);
1323
1324 /* Delete the hash entry. */
1325 zebra_evpn_del(zevpn);
1326 }
1327
1328 static void zebra_evpn_process_sync_macip_add(struct zebra_evpn *zevpn,
1329 const struct ethaddr *macaddr,
1330 uint16_t ipa_len,
1331 const struct ipaddr *ipaddr,
1332 uint8_t flags, uint32_t seq,
1333 const esi_t *esi)
1334 {
1335 char ipbuf[INET6_ADDRSTRLEN];
1336 bool sticky;
1337 bool remote_gw;
1338 struct zebra_neigh *n = NULL;
1339 struct zebra_mac *mac = NULL;
1340
1341 sticky = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
1342 remote_gw = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
1343 /* if sticky or remote-gw ignore updates from the peer */
1344 if (sticky || remote_gw) {
1345 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_NEIGH
1346 || IS_ZEBRA_DEBUG_EVPN_MH_MAC)
1347 zlog_debug(
1348 "Ignore sync-macip vni %u mac %pEA%s%s%s%s",
1349 zevpn->vni,
1350 macaddr,
1351 ipa_len ? " IP " : "",
1352 ipa_len ? ipaddr2str(ipaddr, ipbuf,
1353 sizeof(ipbuf))
1354 : "",
1355 sticky ? " sticky" : "",
1356 remote_gw ? " remote_gw" : "");
1357 return;
1358 }
1359
1360 if (!ipa_len) {
1361 /* MAC update */
1362 (void)zebra_evpn_proc_sync_mac_update(zevpn, macaddr, ipa_len,
1363 ipaddr, flags, seq, esi);
1364 } else {
1365 /* MAC-IP update */
1366 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
1367 if (!mac) {
1368 mac = zebra_evpn_proc_sync_mac_update(zevpn, macaddr,
1369 ipa_len, ipaddr,
1370 flags, seq, esi);
1371 }
1372 if (!mac)
1373 return;
1374
1375 n = zebra_evpn_neigh_lookup(zevpn, ipaddr);
1376 if (n
1377 && !zebra_evpn_neigh_is_bgp_seq_ok(zevpn, n, macaddr, seq,
1378 true))
1379 return;
1380
1381 zebra_evpn_proc_sync_neigh_update(zevpn, n, ipa_len, ipaddr,
1382 flags, seq, esi, mac);
1383 }
1384 }
1385
1386 /************************** remote mac-ip handling **************************/
1387 /* Process a remote MACIP add from BGP. */
1388 void zebra_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr,
1389 uint16_t ipa_len, const struct ipaddr *ipaddr,
1390 uint8_t flags, uint32_t seq,
1391 struct in_addr vtep_ip, const esi_t *esi)
1392 {
1393 struct zebra_evpn *zevpn;
1394 struct zebra_vtep *zvtep;
1395 struct zebra_mac *mac = NULL;
1396 struct interface *ifp = NULL;
1397 struct zebra_if *zif = NULL;
1398 struct zebra_vrf *zvrf;
1399
1400 /* Locate EVPN hash entry - expected to exist. */
1401 zevpn = zebra_evpn_lookup(vni);
1402 if (!zevpn) {
1403 if (IS_ZEBRA_DEBUG_VXLAN)
1404 zlog_debug("Unknown VNI %u upon remote MACIP ADD", vni);
1405 return;
1406 }
1407
1408 ifp = zevpn->vxlan_if;
1409 if (ifp)
1410 zif = ifp->info;
1411 if (!ifp || !if_is_operative(ifp) || !zif || !zif->brslave_info.br_if) {
1412 if (IS_ZEBRA_DEBUG_VXLAN)
1413 zlog_debug(
1414 "Ignoring remote MACIP ADD VNI %u, invalid interface state or info",
1415 vni);
1416 return;
1417 }
1418
1419 /* Type-2 routes from another PE can be interpreted as remote or
1420 * SYNC based on the destination ES -
1421 * SYNC - if ES is local
1422 * REMOTE - if ES is not local
1423 */
1424 if (flags & ZEBRA_MACIP_TYPE_SYNC_PATH) {
1425 struct zebra_evpn_es *es;
1426
1427 es = zebra_evpn_es_find(esi);
1428 if (es && (es->flags & ZEBRA_EVPNES_READY_FOR_BGP)) {
1429 zebra_evpn_process_sync_macip_add(zevpn, macaddr,
1430 ipa_len, ipaddr,
1431 flags, seq, esi);
1432 } else {
1433 if (IS_ZEBRA_DEBUG_EVPN_MH_ES) {
1434 char esi_str[ESI_STR_LEN];
1435
1436 esi_to_str(esi, esi_str, sizeof(esi_str));
1437 zlog_debug(
1438 "Ignore sync-macip add; ES %s is not ready",
1439 esi_str);
1440 }
1441 }
1442
1443 return;
1444 }
1445
1446 /* The remote VTEP specified should normally exist, but it is
1447 * possible that when peering comes up, peer may advertise MACIP
1448 * routes before advertising type-3 routes.
1449 */
1450 if (vtep_ip.s_addr) {
1451 zvtep = zebra_evpn_vtep_find(zevpn, &vtep_ip);
1452 if (!zvtep) {
1453 zvtep = zebra_evpn_vtep_add(zevpn, &vtep_ip,
1454 VXLAN_FLOOD_DISABLED);
1455 if (!zvtep) {
1456 flog_err(
1457 EC_ZEBRA_VTEP_ADD_FAILED,
1458 "Failed to add remote VTEP, VNI %u zevpn %p upon remote MACIP ADD",
1459 vni, zevpn);
1460 return;
1461 }
1462
1463 zebra_evpn_vtep_install(zevpn, zvtep);
1464 }
1465 }
1466
1467 zvrf = zebra_vrf_get_evpn();
1468 if (!zvrf)
1469 return;
1470
1471 if (!ipa_len) {
1472 /* MAC update */
1473 zebra_evpn_mac_remote_macip_add(zevpn, zvrf, macaddr, vtep_ip,
1474 flags, seq, esi);
1475 } else {
1476 /* MAC-IP update
1477 * Add auto MAC if it doesn't exist.
1478 */
1479 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
1480 if (!mac) {
1481 mac = zebra_evpn_mac_add_auto(zevpn, macaddr);
1482
1483 if (IS_ZEBRA_DEBUG_VXLAN)
1484 zlog_debug(
1485 "Neigh %pIA: MAC %pEA not found, Auto MAC created",
1486 ipaddr, macaddr);
1487 }
1488
1489 zebra_evpn_neigh_remote_macip_add(zevpn, zvrf, ipaddr, mac,
1490 vtep_ip, flags, seq);
1491 }
1492 }
1493
1494 /* Process a remote MACIP delete from BGP. */
1495 void zebra_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr,
1496 uint16_t ipa_len, const struct ipaddr *ipaddr,
1497 struct in_addr vtep_ip)
1498 {
1499 struct zebra_evpn *zevpn;
1500 struct zebra_mac *mac = NULL;
1501 struct zebra_neigh *n = NULL;
1502 struct interface *ifp = NULL;
1503 struct zebra_if *zif = NULL;
1504 struct zebra_ns *zns;
1505 struct zebra_l2info_vxlan *vxl;
1506 struct zebra_vrf *zvrf;
1507 char buf1[INET6_ADDRSTRLEN];
1508
1509 /* Locate EVPN hash entry - expected to exist. */
1510 zevpn = zebra_evpn_lookup(vni);
1511 if (!zevpn) {
1512 if (IS_ZEBRA_DEBUG_VXLAN)
1513 zlog_debug("Unknown VNI %u upon remote MACIP DEL", vni);
1514 return;
1515 }
1516
1517 ifp = zevpn->vxlan_if;
1518 if (ifp)
1519 zif = ifp->info;
1520 if (!ifp || !if_is_operative(ifp) || !zif || !zif->brslave_info.br_if) {
1521 if (IS_ZEBRA_DEBUG_VXLAN)
1522 zlog_debug(
1523 "Ignoring remote MACIP DEL VNI %u, invalid interface state or info",
1524 vni);
1525 return;
1526 }
1527 zns = zebra_ns_lookup(NS_DEFAULT);
1528 vxl = &zif->l2info.vxl;
1529
1530 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
1531 if (ipa_len)
1532 n = zebra_evpn_neigh_lookup(zevpn, ipaddr);
1533
1534 if (n && !mac) {
1535 zlog_warn(
1536 "Failed to locate MAC %pEA for Neigh %pIA VNI %u upon remote MACIP DEL",
1537 macaddr, ipaddr, vni);
1538 return;
1539 }
1540
1541 /* If the remote mac or neighbor doesn't exist there is nothing
1542 * more to do. Otherwise, uninstall the entry and then remove it.
1543 */
1544 if (!mac && !n) {
1545 if (IS_ZEBRA_DEBUG_VXLAN)
1546 zlog_debug(
1547 "Failed to locate MAC %pEA & Neigh %pIA VNI %u upon remote MACIP DEL",
1548 macaddr, ipaddr, vni);
1549 return;
1550 }
1551
1552 zvrf = zevpn->vxlan_if->vrf->info;
1553
1554 /* Ignore the delete if this mac is a gateway mac-ip */
1555 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
1556 && CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW)) {
1557 zlog_warn(
1558 "Ignore remote MACIP DEL VNI %u MAC %pEA%s%s as MAC is already configured as gateway MAC",
1559 vni, macaddr,
1560 ipa_len ? " IP " : "",
1561 ipa_len ? ipaddr2str(ipaddr, buf1, sizeof(buf1)) : "");
1562 return;
1563 }
1564
1565 /* Uninstall remote neighbor or MAC. */
1566 if (n)
1567 zebra_evpn_neigh_remote_uninstall(zevpn, zvrf, n, mac, ipaddr);
1568 else {
1569 /* DAD: when MAC is freeze state as remote learn event,
1570 * remote mac-ip delete event is received will result in freeze
1571 * entry removal, first fetch kernel for the same entry present
1572 * as LOCAL and reachable, avoid deleting this entry instead
1573 * use kerenel local entry to update during unfreeze time.
1574 */
1575 if (zvrf->dad_freeze
1576 && CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)
1577 && CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1578 if (IS_ZEBRA_DEBUG_VXLAN)
1579 zlog_debug(
1580 "%s: MAC %pEA (flags 0x%x) is remote and duplicate, read kernel for local entry",
1581 __func__, macaddr, mac->flags);
1582 macfdb_read_specific_mac(zns, zif->brslave_info.br_if,
1583 macaddr, vxl->access_vlan);
1584 }
1585
1586 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
1587 if (!ipa_len)
1588 zebra_evpn_sync_mac_del(mac);
1589 } else if (CHECK_FLAG(mac->flags, ZEBRA_NEIGH_REMOTE)) {
1590 zebra_evpn_rem_mac_del(zevpn, mac);
1591 }
1592 }
1593 }
1594
1595 /************************** EVPN BGP config management ************************/
1596 void zebra_evpn_cfg_cleanup(struct hash_bucket *bucket, void *ctxt)
1597 {
1598 struct zebra_evpn *zevpn = NULL;
1599
1600 zevpn = (struct zebra_evpn *)bucket->data;
1601 zevpn->advertise_gw_macip = 0;
1602 zevpn->advertise_svi_macip = 0;
1603 zevpn->advertise_subnet = 0;
1604
1605 zebra_evpn_neigh_del_all(zevpn, 1, 0,
1606 DEL_REMOTE_NEIGH | DEL_REMOTE_NEIGH_FROM_VTEP);
1607 zebra_evpn_mac_del_all(zevpn, 1, 0,
1608 DEL_REMOTE_MAC | DEL_REMOTE_MAC_FROM_VTEP);
1609 zebra_evpn_vtep_del_all(zevpn, 1);
1610 }