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