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