]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_evpn.c
Merge pull request #8643 from icosahedral/master
[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"
6006414d
PR
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"
8b5fdf2e 55#include "zebra/zebra_evpn_vxlan.h"
6006414d
PR
56#include "zebra/zebra_router.h"
57
8b5fdf2e
PR
58DEFINE_MTYPE_STATIC(ZEBRA, ZEVPN, "VNI hash");
59DEFINE_MTYPE_STATIC(ZEBRA, ZEVPN_VTEP, "VNI remote VTEP");
6006414d 60
6006414d
PR
61/* PMSI strings. */
62#define VXLAN_FLOOD_STR_NO_INFO "-"
63#define VXLAN_FLOOD_STR_DEFAULT VXLAN_FLOOD_STR_NO_INFO
64static 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
6006414d
PR
71int advertise_gw_macip_enabled(zebra_evpn_t *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
85int advertise_svi_macip_enabled(zebra_evpn_t *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
6006414d
PR
99/*
100 * Print a specific EVPN entry.
101 */
8b5fdf2e 102void zebra_evpn_print(zebra_evpn_t *zevpn, void **ctxt)
6006414d
PR
103{
104 struct vty *vty;
105 zebra_vtep_t *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;
9bcef951 111 char buf[PREFIX_STRLEN];
6006414d
PR
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);
9bcef951
MS
137 vty_out(vty, " Local VTEP IP: %pI4\n",
138 &zevpn->local_vtep_ip);
139 vty_out(vty, " Mcast group: %pI4\n",
140 &zevpn->mcast_grp);
6006414d
PR
141 } else {
142 json_object_string_add(json, "vxlanInterface",
143 zevpn->vxlan_if->name);
144 json_object_int_add(json, "ifindex", zevpn->vxlan_if->ifindex);
145 json_object_string_add(json, "vtepIp",
9bcef951
MS
146 inet_ntop(AF_INET, &zevpn->local_vtep_ip,
147 buf, sizeof(buf)));
6006414d 148 json_object_string_add(json, "mcastGroup",
9bcef951
MS
149 inet_ntop(AF_INET, &zevpn->mcast_grp,
150 buf, sizeof(buf)));
6006414d
PR
151 json_object_string_add(json, "advertiseGatewayMacip",
152 zevpn->advertise_gw_macip ? "Yes" : "No");
c0c7707d
AK
153 json_object_string_add(json, "advertiseSviMacip",
154 zevpn->advertise_svi_macip ? "Yes"
155 : "No");
6006414d
PR
156 json_object_int_add(json, "numMacs", num_macs);
157 json_object_int_add(json, "numArpNd", num_neigh);
158 }
159 if (!zevpn->vteps) {
160 if (json == NULL)
161 vty_out(vty, " No remote VTEPs known for this VNI\n");
162 } else {
163 if (json == NULL)
164 vty_out(vty, " Remote VTEPs for this VNI:\n");
165 else
166 json_vtep_list = json_object_new_array();
167 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep->next) {
168 const char *flood_str = lookup_msg(zvtep_flood_str,
169 zvtep->flood_control,
170 VXLAN_FLOOD_STR_DEFAULT);
171
172 if (json == NULL) {
9bcef951
MS
173 vty_out(vty, " %pI4 flood: %s\n",
174 &zvtep->vtep_ip,
6006414d
PR
175 flood_str);
176 } else {
177 json_ip_str = json_object_new_string(
9bcef951
MS
178 inet_ntop(AF_INET,
179 &zvtep->vtep_ip, buf,
180 sizeof(buf)));
6006414d
PR
181 json_object_array_add(json_vtep_list,
182 json_ip_str);
183 }
184 }
185 if (json)
186 json_object_object_add(json, "numRemoteVteps",
187 json_vtep_list);
188 }
189 if (json == NULL) {
190 vty_out(vty,
191 " Number of MACs (local and remote) known for this VNI: %u\n",
192 num_macs);
193 vty_out(vty,
194 " Number of ARPs (IPv4 and IPv6, local and remote) "
195 "known for this VNI: %u\n",
196 num_neigh);
197 vty_out(vty, " Advertise-gw-macip: %s\n",
198 zevpn->advertise_gw_macip ? "Yes" : "No");
c0c7707d
AK
199 vty_out(vty, " Advertise-svi-macip: %s\n",
200 zevpn->advertise_svi_macip ? "Yes" : "No");
6006414d
PR
201 }
202}
203
8b5fdf2e
PR
204/*
205 * Print an EVPN hash entry - called for display of all VNIs.
206 */
207void zebra_evpn_print_hash(struct hash_bucket *bucket, void *ctxt[])
6006414d 208{
8b5fdf2e
PR
209 struct vty *vty;
210 zebra_evpn_t *zevpn;
211 zebra_vtep_t *zvtep;
212 uint32_t num_vteps = 0;
213 uint32_t num_macs = 0;
214 uint32_t num_neigh = 0;
6006414d
PR
215 json_object *json = NULL;
216 json_object *json_evpn = NULL;
8b5fdf2e
PR
217 json_object *json_ip_str = NULL;
218 json_object *json_vtep_list = NULL;
9bcef951 219 char buf[PREFIX_STRLEN];
6006414d 220
8b5fdf2e
PR
221 vty = ctxt[0];
222 json = ctxt[1];
6006414d 223
8b5fdf2e 224 zevpn = (zebra_evpn_t *)bucket->data;
6006414d 225
8b5fdf2e
PR
226 zvtep = zevpn->vteps;
227 while (zvtep) {
228 num_vteps++;
229 zvtep = zvtep->next;
6006414d
PR
230 }
231
232 num_macs = num_valid_macs(zevpn);
233 num_neigh = hashcount(zevpn->neigh_table);
234 if (json == NULL)
235 vty_out(vty, "%-10u %-4s %-21s %-8u %-8u %-15u %-37s\n",
236 zevpn->vni, "L2",
237 zevpn->vxlan_if ? zevpn->vxlan_if->name : "unknown",
238 num_macs, num_neigh, num_vteps,
239 vrf_id_to_name(zevpn->vrf_id));
240 else {
241 char vni_str[VNI_STR_LEN];
242 snprintf(vni_str, VNI_STR_LEN, "%u", zevpn->vni);
243 json_evpn = json_object_new_object();
244 json_object_int_add(json_evpn, "vni", zevpn->vni);
245 json_object_string_add(json_evpn, "type", "L2");
246 json_object_string_add(json_evpn, "vxlanIf",
247 zevpn->vxlan_if ? zevpn->vxlan_if->name
248 : "unknown");
249 json_object_int_add(json_evpn, "numMacs", num_macs);
250 json_object_int_add(json_evpn, "numArpNd", num_neigh);
251 json_object_int_add(json_evpn, "numRemoteVteps", num_vteps);
252 json_object_string_add(json_evpn, "tenantVrf",
253 vrf_id_to_name(zevpn->vrf_id));
254 if (num_vteps) {
255 json_vtep_list = json_object_new_array();
256 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep->next) {
257 json_ip_str = json_object_new_string(
9bcef951
MS
258 inet_ntop(AF_INET, &zvtep->vtep_ip, buf,
259 sizeof(buf)));
6006414d
PR
260 json_object_array_add(json_vtep_list,
261 json_ip_str);
262 }
263 json_object_object_add(json_evpn, "remoteVteps",
264 json_vtep_list);
265 }
266 json_object_object_add(json, vni_str, json_evpn);
267 }
268}
269
270/*
271 * Print an EVPN hash entry in detail - called for display of all EVPNs.
272 */
8b5fdf2e 273void zebra_evpn_print_hash_detail(struct hash_bucket *bucket, void *data)
6006414d
PR
274{
275 struct vty *vty;
276 zebra_evpn_t *zevpn;
277 json_object *json_array = NULL;
278 bool use_json = false;
8b5fdf2e 279 struct zebra_evpn_show *zes = data;
6006414d
PR
280
281 vty = zes->vty;
282 json_array = zes->json;
283 use_json = zes->use_json;
284
285 zevpn = (zebra_evpn_t *)bucket->data;
286
287 zebra_vxlan_print_vni(vty, zes->zvrf, zevpn->vni, use_json, json_array);
288
289 if (!use_json)
290 vty_out(vty, "\n");
291}
292
8b5fdf2e 293int zebra_evpn_del_macip_for_intf(struct interface *ifp, zebra_evpn_t *zevpn)
6006414d
PR
294{
295 struct listnode *cnode = NULL, *cnnode = NULL;
296 struct connected *c = NULL;
297 struct ethaddr macaddr;
298
299 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
300
301 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
302 struct ipaddr ip;
303
304 memset(&ip, 0, sizeof(struct ipaddr));
305 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
306 continue;
307
308 if (c->address->family == AF_INET) {
309 ip.ipa_type = IPADDR_V4;
310 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
311 sizeof(struct in_addr));
312 } else if (c->address->family == AF_INET6) {
313 ip.ipa_type = IPADDR_V6;
314 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
315 sizeof(struct in6_addr));
316 } else {
317 continue;
318 }
319
8b5fdf2e 320 zebra_evpn_gw_macip_del(ifp, zevpn, &ip);
6006414d
PR
321 }
322
323 return 0;
324}
325
8b5fdf2e 326int zebra_evpn_add_macip_for_intf(struct interface *ifp, zebra_evpn_t *zevpn)
6006414d
PR
327{
328 struct listnode *cnode = NULL, *cnnode = NULL;
329 struct connected *c = NULL;
330 struct ethaddr macaddr;
331
332 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
333
334 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
335 struct ipaddr ip;
336
337 memset(&ip, 0, sizeof(struct ipaddr));
338 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
339 continue;
340
341 if (c->address->family == AF_INET) {
342 ip.ipa_type = IPADDR_V4;
343 memcpy(&(ip.ipaddr_v4), &(c->address->u.prefix4),
344 sizeof(struct in_addr));
345 } else if (c->address->family == AF_INET6) {
346 ip.ipa_type = IPADDR_V6;
347 memcpy(&(ip.ipaddr_v6), &(c->address->u.prefix6),
348 sizeof(struct in6_addr));
349 } else {
350 continue;
351 }
352
8b5fdf2e 353 zebra_evpn_gw_macip_add(ifp, zevpn, &macaddr, &ip);
6006414d
PR
354 }
355 return 0;
356}
357
8b5fdf2e
PR
358static int ip_prefix_send_to_client(vrf_id_t vrf_id, struct prefix *p,
359 uint16_t cmd)
360{
361 struct zserv *client = NULL;
362 struct stream *s = NULL;
8b5fdf2e
PR
363
364 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
365 /* BGP may not be running. */
366 if (!client)
367 return 0;
368
369 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
370
371 zclient_create_header(s, cmd, vrf_id);
372 stream_put(s, p, sizeof(struct prefix));
373
374 /* Write packet size. */
375 stream_putw_at(s, 0, stream_get_endp(s));
376
377 if (IS_ZEBRA_DEBUG_VXLAN)
2dbe669b 378 zlog_debug("Send ip prefix %pFX %s on vrf %s", p,
8b5fdf2e
PR
379 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
380 vrf_id_to_name(vrf_id));
381
382 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD)
383 client->prefixadd_cnt++;
384 else
385 client->prefixdel_cnt++;
386
387 return zserv_send_message(client, s);
388}
6006414d 389
8b5fdf2e
PR
390int zebra_evpn_advertise_subnet(zebra_evpn_t *zevpn, struct interface *ifp,
391 int advertise)
6006414d
PR
392{
393 struct listnode *cnode = NULL, *cnnode = NULL;
394 struct connected *c = NULL;
395 struct ethaddr macaddr;
396
397 memcpy(&macaddr.octet, ifp->hw_addr, ETH_ALEN);
398
399 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
400 struct prefix p;
401
402 memcpy(&p, c->address, sizeof(struct prefix));
403
404 /* skip link local address */
405 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
406 continue;
407
408 apply_mask(&p);
409 if (advertise)
410 ip_prefix_send_to_client(ifp->vrf_id, &p,
411 ZEBRA_IP_PREFIX_ROUTE_ADD);
412 else
413 ip_prefix_send_to_client(ifp->vrf_id, &p,
414 ZEBRA_IP_PREFIX_ROUTE_DEL);
415 }
416 return 0;
417}
418
419/*
8b5fdf2e 420 * zebra_evpn_gw_macip_add_to_client
6006414d 421 */
8b5fdf2e
PR
422int zebra_evpn_gw_macip_add(struct interface *ifp, zebra_evpn_t *zevpn,
423 struct ethaddr *macaddr, struct ipaddr *ip)
6006414d
PR
424{
425 zebra_mac_t *mac = NULL;
426 struct zebra_if *zif = NULL;
427 struct zebra_l2info_vxlan *vxl = NULL;
428
429 zif = zevpn->vxlan_if->info;
430 if (!zif)
431 return -1;
432
433 vxl = &zif->l2info.vxl;
434
435 if (zebra_evpn_mac_gw_macip_add(ifp, zevpn, ip, &mac, macaddr,
e4c3ece6 436 vxl->access_vlan, true)
6006414d
PR
437 != 0)
438 return -1;
439
440 return zebra_evpn_neigh_gw_macip_add(ifp, zevpn, ip, mac);
441}
442
443/*
8b5fdf2e 444 * zebra_evpn_gw_macip_del_from_client
6006414d 445 */
8b5fdf2e
PR
446int zebra_evpn_gw_macip_del(struct interface *ifp, zebra_evpn_t *zevpn,
447 struct ipaddr *ip)
6006414d 448{
6006414d
PR
449 zebra_neigh_t *n = NULL;
450 zebra_mac_t *mac = NULL;
451
452 /* If the neigh entry is not present nothing to do*/
453 n = zebra_evpn_neigh_lookup(zevpn, ip);
454 if (!n)
455 return 0;
456
457 /* mac entry should be present */
458 mac = zebra_evpn_mac_lookup(zevpn, &n->emac);
459 if (!mac) {
460 if (IS_ZEBRA_DEBUG_VXLAN)
ef7b8be4
DL
461 zlog_debug("MAC %pEA doesn't exist for neigh %pIA on VNI %u",
462 &n->emac, ip, zevpn->vni);
6006414d
PR
463 return -1;
464 }
465
466 /* If the entry is not local nothing to do*/
467 if (!CHECK_FLAG(n->flags, ZEBRA_NEIGH_LOCAL))
468 return -1;
469
470 /* only need to delete the entry from bgp if we sent it before */
471 if (IS_ZEBRA_DEBUG_VXLAN)
472 zlog_debug(
ef7b8be4 473 "%u:SVI %s(%u) VNI %u, sending GW MAC %pEA IP %pIA del to BGP",
6006414d 474 ifp->vrf_id, ifp->name, ifp->ifindex, zevpn->vni,
ef7b8be4 475 &n->emac, ip);
6006414d
PR
476
477 /* Remove neighbor from BGP. */
478 zebra_evpn_neigh_send_del_to_client(zevpn->vni, &n->ip, &n->emac,
479 n->flags, ZEBRA_NEIGH_ACTIVE,
480 false /*force*/);
481
482 /* Delete this neighbor entry. */
483 zebra_evpn_neigh_del(zevpn, n);
484
485 /* see if the mac needs to be deleted as well*/
486 if (mac)
487 zebra_evpn_deref_ip2mac(zevpn, mac);
488
489 return 0;
490}
491
8b5fdf2e 492void zebra_evpn_gw_macip_del_for_evpn_hash(struct hash_bucket *bucket,
6006414d
PR
493 void *ctxt)
494{
495 zebra_evpn_t *zevpn = NULL;
496 struct zebra_if *zif = NULL;
497 struct zebra_l2info_vxlan zl2_info;
498 struct interface *vlan_if = NULL;
499 struct interface *vrr_if = NULL;
500 struct interface *ifp;
501
502 /* Add primary SVI MAC*/
503 zevpn = (zebra_evpn_t *)bucket->data;
504
505 /* Global (Zvrf) advertise-default-gw is disabled,
506 * but zevpn advertise-default-gw is enabled
507 */
508 if (zevpn->advertise_gw_macip) {
509 if (IS_ZEBRA_DEBUG_VXLAN)
510 zlog_debug("VNI: %u GW-MACIP enabled, retain gw-macip",
511 zevpn->vni);
512 return;
513 }
514
515 ifp = zevpn->vxlan_if;
516 if (!ifp)
517 return;
518 zif = ifp->info;
519
520 /* If down or not mapped to a bridge, we're done. */
521 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
522 return;
523
524 zl2_info = zif->l2info.vxl;
525
526 vlan_if =
527 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
528 if (!vlan_if)
529 return;
530
531 /* Del primary MAC-IP */
8b5fdf2e 532 zebra_evpn_del_macip_for_intf(vlan_if, zevpn);
6006414d
PR
533
534 /* Del VRR MAC-IP - if any*/
535 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
536 if (vrr_if)
8b5fdf2e 537 zebra_evpn_del_macip_for_intf(vrr_if, zevpn);
6006414d
PR
538
539 return;
540}
541
8b5fdf2e 542void zebra_evpn_gw_macip_add_for_evpn_hash(struct hash_bucket *bucket,
6006414d
PR
543 void *ctxt)
544{
545 zebra_evpn_t *zevpn = NULL;
546 struct zebra_if *zif = NULL;
547 struct zebra_l2info_vxlan zl2_info;
548 struct interface *vlan_if = NULL;
549 struct interface *vrr_if = NULL;
550 struct interface *ifp = NULL;
551
552 zevpn = (zebra_evpn_t *)bucket->data;
553
554 ifp = zevpn->vxlan_if;
555 if (!ifp)
556 return;
557 zif = ifp->info;
558
559 /* If down or not mapped to a bridge, we're done. */
560 if (!if_is_operative(ifp) || !zif->brslave_info.br_if)
561 return;
562 zl2_info = zif->l2info.vxl;
563
564 vlan_if =
565 zvni_map_to_svi(zl2_info.access_vlan, zif->brslave_info.br_if);
566 if (!vlan_if)
567 return;
568
569 /* Add primary SVI MAC-IP */
c0c7707d
AK
570 if (advertise_svi_macip_enabled(zevpn)
571 || advertise_gw_macip_enabled(zevpn))
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) {
243b74ed
AK
928 /* Add SVI MAC */
929 zebra_evpn_acc_bd_svi_mac_add(vlan_if);
6006414d
PR
930
931 /* Add SVI MAC-IP */
c0c7707d
AK
932 if (advertise_svi_macip_enabled(zevpn)
933 || advertise_gw_macip_enabled(zevpn))
934 zebra_evpn_add_macip_for_intf(vlan_if, zevpn);
6006414d
PR
935
936 /* Add VRR MAC-IP - if any*/
c0c7707d
AK
937 if (advertise_gw_macip_enabled(zevpn)) {
938 vrr_if = zebra_get_vrr_intf_for_svi(vlan_if);
939 if (vrr_if)
940 zebra_evpn_add_macip_for_intf(vrr_if, zevpn);
941 }
6006414d
PR
942
943 neigh_read_for_vlan(zns, vlan_if);
944 }
945}
946
947/*
8b5fdf2e 948 * Hash function for EVPN.
6006414d 949 */
8b5fdf2e 950unsigned int zebra_evpn_hash_keymake(const void *p)
6006414d
PR
951{
952 const zebra_evpn_t *zevpn = p;
953
954 return (jhash_1word(zevpn->vni, 0));
955}
956
957/*
8b5fdf2e 958 * Compare 2 evpn hash entries.
6006414d 959 */
8b5fdf2e 960bool zebra_evpn_hash_cmp(const void *p1, const void *p2)
6006414d
PR
961{
962 const zebra_evpn_t *zevpn1 = p1;
963 const zebra_evpn_t *zevpn2 = p2;
964
965 return (zevpn1->vni == zevpn2->vni);
966}
967
8b5fdf2e 968int zebra_evpn_list_cmp(void *p1, void *p2)
6006414d
PR
969{
970 const zebra_evpn_t *zevpn1 = p1;
971 const zebra_evpn_t *zevpn2 = p2;
972
973 if (zevpn1->vni == zevpn2->vni)
974 return 0;
975 return (zevpn1->vni < zevpn2->vni) ? -1 : 1;
976}
977
978/*
979 * Callback to allocate VNI hash entry.
980 */
8b5fdf2e 981void *zebra_evpn_alloc(void *p)
6006414d
PR
982{
983 const zebra_evpn_t *tmp_vni = p;
984 zebra_evpn_t *zevpn;
985
986 zevpn = XCALLOC(MTYPE_ZEVPN, sizeof(zebra_evpn_t));
987 zevpn->vni = tmp_vni->vni;
988 return ((void *)zevpn);
989}
990
991/*
992 * Look up EVPN hash entry.
993 */
8b5fdf2e 994zebra_evpn_t *zebra_evpn_lookup(vni_t vni)
6006414d
PR
995{
996 struct zebra_vrf *zvrf;
997 zebra_evpn_t tmp_vni;
998 zebra_evpn_t *zevpn = NULL;
999
1000 zvrf = zebra_vrf_get_evpn();
1001 assert(zvrf);
1002 memset(&tmp_vni, 0, sizeof(zebra_evpn_t));
1003 tmp_vni.vni = vni;
1004 zevpn = hash_lookup(zvrf->evpn_table, &tmp_vni);
1005
1006 return zevpn;
1007}
1008
1009/*
1010 * Add EVPN hash entry.
1011 */
8b5fdf2e 1012zebra_evpn_t *zebra_evpn_add(vni_t vni)
6006414d 1013{
38078b1d 1014 char buffer[80];
6006414d
PR
1015 struct zebra_vrf *zvrf;
1016 zebra_evpn_t tmp_zevpn;
1017 zebra_evpn_t *zevpn = NULL;
1018
1019 zvrf = zebra_vrf_get_evpn();
1020 assert(zvrf);
1021 memset(&tmp_zevpn, 0, sizeof(zebra_evpn_t));
1022 tmp_zevpn.vni = vni;
8b5fdf2e 1023 zevpn = hash_get(zvrf->evpn_table, &tmp_zevpn, zebra_evpn_alloc);
6006414d
PR
1024 assert(zevpn);
1025
945ee7b2 1026 zebra_evpn_es_evi_init(zevpn);
6006414d 1027
38078b1d 1028 snprintf(buffer, sizeof(buffer), "Zebra EVPN MAC Table vni: %u", vni);
6006414d 1029 /* Create hash table for MAC */
38078b1d 1030 zevpn->mac_table = zebra_mac_db_create(buffer);
6006414d 1031
e2071325
DL
1032 snprintf(buffer, sizeof(buffer), "Zebra EVPN Neighbor Table vni: %u",
1033 vni);
6006414d 1034 /* Create hash table for neighbors */
38078b1d 1035 zevpn->neigh_table = zebra_neigh_db_create(buffer);
6006414d
PR
1036
1037 return zevpn;
1038}
1039
6006414d
PR
1040/*
1041 * Delete EVPN hash entry.
1042 */
8b5fdf2e 1043int zebra_evpn_del(zebra_evpn_t *zevpn)
6006414d
PR
1044{
1045 struct zebra_vrf *zvrf;
1046 zebra_evpn_t *tmp_zevpn;
1047
1048 zvrf = zebra_vrf_get_evpn();
1049 assert(zvrf);
1050
6006414d
PR
1051 /* Free the neighbor hash table. */
1052 hash_free(zevpn->neigh_table);
1053 zevpn->neigh_table = NULL;
1054
1055 /* Free the MAC hash table. */
1056 hash_free(zevpn->mac_table);
1057 zevpn->mac_table = NULL;
1058
963b0c55
AK
1059 /* Remove references to the zevpn in the MH databases */
1060 if (zevpn->vxlan_if)
1061 zebra_evpn_vxl_evpn_set(zevpn->vxlan_if->info, zevpn, false);
945ee7b2 1062 zebra_evpn_es_evi_cleanup(zevpn);
6006414d
PR
1063
1064 /* Free the EVPN hash entry and allocated memory. */
1065 tmp_zevpn = hash_release(zvrf->evpn_table, zevpn);
1066 XFREE(MTYPE_ZEVPN, tmp_zevpn);
1067
1068 return 0;
1069}
1070
1071/*
1072 * Inform BGP about local EVPN addition.
1073 */
8b5fdf2e 1074int zebra_evpn_send_add_to_client(zebra_evpn_t *zevpn)
6006414d
PR
1075{
1076 struct zserv *client;
1077 struct stream *s;
1078 int rc;
1079
1080 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
1081 /* BGP may not be running. */
1082 if (!client)
1083 return 0;
1084
1085 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1086
1087 zclient_create_header(s, ZEBRA_VNI_ADD, zebra_vrf_get_evpn_id());
1088 stream_putl(s, zevpn->vni);
1089 stream_put_in_addr(s, &zevpn->local_vtep_ip);
1090 stream_put(s, &zevpn->vrf_id, sizeof(vrf_id_t)); /* tenant vrf */
1091 stream_put_in_addr(s, &zevpn->mcast_grp);
1092
1093 /* Write packet size. */
1094 stream_putw_at(s, 0, stream_get_endp(s));
1095
1096 if (IS_ZEBRA_DEBUG_VXLAN)
9bcef951
MS
1097 zlog_debug("Send EVPN_ADD %u %pI4 tenant vrf %s to %s", zevpn->vni,
1098 &zevpn->local_vtep_ip,
6006414d
PR
1099 vrf_id_to_name(zevpn->vrf_id),
1100 zebra_route_string(client->proto));
1101
1102 client->vniadd_cnt++;
1103 rc = zserv_send_message(client, s);
1104
1105 if (!(zevpn->flags & ZEVPN_READY_FOR_BGP)) {
1106 zevpn->flags |= ZEVPN_READY_FOR_BGP;
1107 /* once the EVPN is sent the ES-EVIs can also be replayed
1108 * to BGP
1109 */
1110 zebra_evpn_update_all_es(zevpn);
1111 }
1112 return rc;
1113}
1114
1115/*
1116 * Inform BGP about local EVPN deletion.
1117 */
8b5fdf2e 1118int zebra_evpn_send_del_to_client(zebra_evpn_t *zevpn)
6006414d
PR
1119{
1120 struct zserv *client;
1121 struct stream *s;
1122
1123 client = zserv_find_client(ZEBRA_ROUTE_BGP, 0);
1124 /* BGP may not be running. */
1125 if (!client)
1126 return 0;
1127
1128 if (zevpn->flags & ZEVPN_READY_FOR_BGP) {
1129 zevpn->flags &= ~ZEVPN_READY_FOR_BGP;
1130 /* the ES-EVIs must be removed from BGP before the EVPN is */
1131 zebra_evpn_update_all_es(zevpn);
1132 }
1133
1134 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1135 stream_reset(s);
1136
1137 zclient_create_header(s, ZEBRA_VNI_DEL, zebra_vrf_get_evpn_id());
1138 stream_putl(s, zevpn->vni);
1139
1140 /* Write packet size. */
1141 stream_putw_at(s, 0, stream_get_endp(s));
1142
1143 if (IS_ZEBRA_DEBUG_VXLAN)
1144 zlog_debug("Send EVPN_DEL %u to %s", zevpn->vni,
1145 zebra_route_string(client->proto));
1146
1147 client->vnidel_cnt++;
1148 return zserv_send_message(client, s);
1149}
1150
6006414d
PR
1151/*
1152 * See if remote VTEP matches with prefix.
1153 */
8b5fdf2e 1154static int zebra_evpn_vtep_match(struct in_addr *vtep_ip, zebra_vtep_t *zvtep)
6006414d
PR
1155{
1156 return (IPV4_ADDR_SAME(vtep_ip, &zvtep->vtep_ip));
1157}
1158
1159/*
1160 * Locate remote VTEP in EVPN hash table.
1161 */
8b5fdf2e 1162zebra_vtep_t *zebra_evpn_vtep_find(zebra_evpn_t *zevpn, struct in_addr *vtep_ip)
6006414d
PR
1163{
1164 zebra_vtep_t *zvtep;
1165
1166 if (!zevpn)
1167 return NULL;
1168
1169 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep->next) {
8b5fdf2e 1170 if (zebra_evpn_vtep_match(vtep_ip, zvtep))
6006414d
PR
1171 break;
1172 }
1173
1174 return zvtep;
1175}
1176
1177/*
1178 * Add remote VTEP to EVPN hash table.
1179 */
8b5fdf2e
PR
1180zebra_vtep_t *zebra_evpn_vtep_add(zebra_evpn_t *zevpn, struct in_addr *vtep_ip,
1181 int flood_control)
6006414d
PR
1182
1183{
1184 zebra_vtep_t *zvtep;
1185
1186 zvtep = XCALLOC(MTYPE_ZEVPN_VTEP, sizeof(zebra_vtep_t));
1187
1188 zvtep->vtep_ip = *vtep_ip;
1189 zvtep->flood_control = flood_control;
1190
1191 if (zevpn->vteps)
1192 zevpn->vteps->prev = zvtep;
1193 zvtep->next = zevpn->vteps;
1194 zevpn->vteps = zvtep;
1195
1196 return zvtep;
1197}
1198
1199/*
1200 * Remove remote VTEP from EVPN hash table.
1201 */
8b5fdf2e 1202int zebra_evpn_vtep_del(zebra_evpn_t *zevpn, zebra_vtep_t *zvtep)
6006414d
PR
1203{
1204 if (zvtep->next)
1205 zvtep->next->prev = zvtep->prev;
1206 if (zvtep->prev)
1207 zvtep->prev->next = zvtep->next;
1208 else
1209 zevpn->vteps = zvtep->next;
1210
1211 zvtep->prev = zvtep->next = NULL;
1212 XFREE(MTYPE_ZEVPN_VTEP, zvtep);
1213
1214 return 0;
1215}
1216
1217/*
1218 * Delete all remote VTEPs for this EVPN (upon VNI delete). Also
1219 * uninstall from kernel if asked to.
1220 */
8b5fdf2e 1221int zebra_evpn_vtep_del_all(zebra_evpn_t *zevpn, int uninstall)
6006414d
PR
1222{
1223 zebra_vtep_t *zvtep, *zvtep_next;
1224
1225 if (!zevpn)
1226 return -1;
1227
1228 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep_next) {
1229 zvtep_next = zvtep->next;
1230 if (uninstall)
8b5fdf2e
PR
1231 zebra_evpn_vtep_uninstall(zevpn, &zvtep->vtep_ip);
1232 zebra_evpn_vtep_del(zevpn, zvtep);
6006414d
PR
1233 }
1234
1235 return 0;
1236}
1237
1238/*
1239 * Install remote VTEP into the kernel if the remote VTEP has asked
1240 * for head-end-replication.
1241 */
8b5fdf2e 1242int zebra_evpn_vtep_install(zebra_evpn_t *zevpn, zebra_vtep_t *zvtep)
6006414d
PR
1243{
1244 if (is_vxlan_flooding_head_end() &&
1245 (zvtep->flood_control == VXLAN_FLOOD_HEAD_END_REPL)) {
1246 if (ZEBRA_DPLANE_REQUEST_FAILURE ==
1247 dplane_vtep_add(zevpn->vxlan_if,
1248 &zvtep->vtep_ip, zevpn->vni))
1249 return -1;
1250 }
1251
1252 return 0;
1253}
1254
1255/*
1256 * Uninstall remote VTEP from the kernel.
1257 */
8b5fdf2e 1258int zebra_evpn_vtep_uninstall(zebra_evpn_t *zevpn, struct in_addr *vtep_ip)
6006414d
PR
1259{
1260 if (!zevpn->vxlan_if) {
1261 zlog_debug("VNI %u hash %p couldn't be uninstalled - no intf",
1262 zevpn->vni, zevpn);
1263 return -1;
1264 }
1265
1266 if (ZEBRA_DPLANE_REQUEST_FAILURE ==
1267 dplane_vtep_delete(zevpn->vxlan_if, vtep_ip, zevpn->vni))
1268 return -1;
1269
1270 return 0;
1271}
1272
1273/*
1274 * Install or uninstall flood entries in the kernel corresponding to
1275 * remote VTEPs. This is invoked upon change to BUM handling.
1276 */
8b5fdf2e
PR
1277void zebra_evpn_handle_flooding_remote_vteps(struct hash_bucket *bucket,
1278 void *zvrf)
6006414d
PR
1279{
1280 zebra_evpn_t *zevpn;
1281 zebra_vtep_t *zvtep;
1282
1283 zevpn = (zebra_evpn_t *)bucket->data;
1284 if (!zevpn)
1285 return;
1286
1287 for (zvtep = zevpn->vteps; zvtep; zvtep = zvtep->next) {
1288 if (is_vxlan_flooding_head_end())
8b5fdf2e 1289 zebra_evpn_vtep_install(zevpn, zvtep);
6006414d 1290 else
8b5fdf2e 1291 zebra_evpn_vtep_uninstall(zevpn, &zvtep->vtep_ip);
6006414d
PR
1292 }
1293}
1294
1295/*
1296 * Cleanup EVPN/VTEP and update kernel
1297 */
8b5fdf2e 1298void zebra_evpn_cleanup_all(struct hash_bucket *bucket, void *arg)
6006414d
PR
1299{
1300 zebra_evpn_t *zevpn = NULL;
6006414d
PR
1301
1302 zevpn = (zebra_evpn_t *)bucket->data;
1303
6006414d
PR
1304 /* Free up all neighbors and MACs, if any. */
1305 zebra_evpn_neigh_del_all(zevpn, 1, 0, DEL_ALL_NEIGH);
1306 zebra_evpn_mac_del_all(zevpn, 1, 0, DEL_ALL_MAC);
1307
1308 /* Free up all remote VTEPs, if any. */
8b5fdf2e 1309 zebra_evpn_vtep_del_all(zevpn, 1);
6006414d
PR
1310
1311 /* Delete the hash entry. */
8b5fdf2e 1312 zebra_evpn_del(zevpn);
6006414d
PR
1313}
1314
8b5fdf2e
PR
1315static void
1316zebra_evpn_process_sync_macip_add(zebra_evpn_t *zevpn, struct ethaddr *macaddr,
1317 uint16_t ipa_len, struct ipaddr *ipaddr,
1318 uint8_t flags, uint32_t seq, esi_t *esi)
6006414d 1319{
8b5fdf2e 1320 struct sync_mac_ip_ctx ctx;
8b5fdf2e
PR
1321 char ipbuf[INET6_ADDRSTRLEN];
1322 bool sticky;
1323 bool remote_gw;
1324 zebra_neigh_t *n = NULL;
6006414d 1325
8b5fdf2e
PR
1326 sticky = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
1327 remote_gw = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
1328 /* if sticky or remote-gw ignore updates from the peer */
1329 if (sticky || remote_gw) {
1330 if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_NEIGH
1331 || IS_ZEBRA_DEBUG_EVPN_MH_MAC)
1332 zlog_debug(
ef7b8be4 1333 "Ignore sync-macip vni %u mac %pEA%s%s%s%s",
8b5fdf2e 1334 zevpn->vni,
ef7b8be4 1335 macaddr,
8b5fdf2e
PR
1336 ipa_len ? " IP " : "",
1337 ipa_len ? ipaddr2str(ipaddr, ipbuf,
1338 sizeof(ipbuf))
1339 : "",
1340 sticky ? " sticky" : "",
1341 remote_gw ? " remote_gw" : "");
1342 return;
1343 }
6006414d 1344
8b5fdf2e
PR
1345 if (ipa_len) {
1346 n = zebra_evpn_neigh_lookup(zevpn, ipaddr);
1347 if (n
16de1338
AK
1348 && !zebra_evpn_neigh_is_bgp_seq_ok(zevpn, n, macaddr, seq,
1349 true))
8b5fdf2e
PR
1350 return;
1351 }
6006414d 1352
8b5fdf2e
PR
1353 memset(&ctx, 0, sizeof(ctx));
1354 ctx.mac = zebra_evpn_proc_sync_mac_update(
1355 zevpn, macaddr, ipa_len, ipaddr, flags, seq, esi, &ctx);
1356 if (ctx.ignore_macip || !ctx.mac || !ipa_len)
6006414d
PR
1357 return;
1358
8b5fdf2e
PR
1359 zebra_evpn_proc_sync_neigh_update(zevpn, n, ipa_len, ipaddr, flags, seq,
1360 esi, &ctx);
6006414d
PR
1361}
1362
8b5fdf2e
PR
1363/************************** remote mac-ip handling **************************/
1364/* Process a remote MACIP add from BGP. */
1365void process_remote_macip_add(vni_t vni, struct ethaddr *macaddr,
1366 uint16_t ipa_len, struct ipaddr *ipaddr,
1367 uint8_t flags, uint32_t seq,
1368 struct in_addr vtep_ip, esi_t *esi)
6006414d 1369{
8b5fdf2e
PR
1370 zebra_evpn_t *zevpn;
1371 zebra_vtep_t *zvtep;
1372 zebra_mac_t *mac = NULL;
1373 struct interface *ifp = NULL;
1374 struct zebra_if *zif = NULL;
1375 struct zebra_vrf *zvrf;
6006414d 1376
8b5fdf2e
PR
1377 /* Locate EVPN hash entry - expected to exist. */
1378 zevpn = zebra_evpn_lookup(vni);
1379 if (!zevpn) {
34c9b28b
DS
1380 if (IS_ZEBRA_DEBUG_VXLAN)
1381 zlog_debug("Unknown VNI %u upon remote MACIP ADD", vni);
8b5fdf2e 1382 return;
6006414d
PR
1383 }
1384
8b5fdf2e
PR
1385 ifp = zevpn->vxlan_if;
1386 if (ifp)
1387 zif = ifp->info;
1388 if (!ifp || !if_is_operative(ifp) || !zif || !zif->brslave_info.br_if) {
1389 zlog_warn(
1390 "Ignoring remote MACIP ADD VNI %u, invalid interface state or info",
1391 vni);
1392 return;
1393 }
6006414d 1394
8b5fdf2e
PR
1395 /* Type-2 routes from another PE can be interpreted as remote or
1396 * SYNC based on the destination ES -
1397 * SYNC - if ES is local
1398 * REMOTE - if ES is not local
1399 */
1400 if (flags & ZEBRA_MACIP_TYPE_SYNC_PATH) {
1401 zebra_evpn_process_sync_macip_add(zevpn, macaddr, ipa_len,
1402 ipaddr, flags, seq, esi);
1403 return;
1404 }
6006414d 1405
8b5fdf2e
PR
1406 /* The remote VTEP specified should normally exist, but it is
1407 * possible that when peering comes up, peer may advertise MACIP
1408 * routes before advertising type-3 routes.
1409 */
1410 if (vtep_ip.s_addr) {
1411 zvtep = zebra_evpn_vtep_find(zevpn, &vtep_ip);
1412 if (!zvtep) {
1413 zvtep = zebra_evpn_vtep_add(zevpn, &vtep_ip,
1414 VXLAN_FLOOD_DISABLED);
1415 if (!zvtep) {
1416 flog_err(
1417 EC_ZEBRA_VTEP_ADD_FAILED,
1418 "Failed to add remote VTEP, VNI %u zevpn %p upon remote MACIP ADD",
1419 vni, zevpn);
1420 return;
1421 }
6006414d 1422
8b5fdf2e
PR
1423 zebra_evpn_vtep_install(zevpn, zvtep);
1424 }
6006414d
PR
1425 }
1426
d6bf8f13 1427 zvrf = zebra_vrf_get_evpn();
6006414d
PR
1428 if (!zvrf)
1429 return;
6006414d 1430
6006414d 1431
8b5fdf2e
PR
1432 if (process_mac_remote_macip_add(zevpn, zvrf, macaddr, ipa_len, ipaddr,
1433 &mac, vtep_ip, flags, seq, esi)
1434 != 0)
6006414d 1435 return;
6006414d 1436
8b5fdf2e
PR
1437 process_neigh_remote_macip_add(zevpn, zvrf, ipaddr, mac, vtep_ip, flags,
1438 seq);
6006414d
PR
1439}
1440
8b5fdf2e
PR
1441/* Process a remote MACIP delete from BGP. */
1442void process_remote_macip_del(vni_t vni, struct ethaddr *macaddr,
1443 uint16_t ipa_len, struct ipaddr *ipaddr,
1444 struct in_addr vtep_ip)
6006414d 1445{
8b5fdf2e
PR
1446 zebra_evpn_t *zevpn;
1447 zebra_mac_t *mac = NULL;
1448 zebra_neigh_t *n = NULL;
1449 struct interface *ifp = NULL;
1450 struct zebra_if *zif = NULL;
1451 struct zebra_ns *zns;
1452 struct zebra_l2info_vxlan *vxl;
6006414d 1453 struct zebra_vrf *zvrf;
8b5fdf2e 1454 char buf1[INET6_ADDRSTRLEN];
6006414d 1455
8b5fdf2e
PR
1456 /* Locate EVPN hash entry - expected to exist. */
1457 zevpn = zebra_evpn_lookup(vni);
1458 if (!zevpn) {
1459 if (IS_ZEBRA_DEBUG_VXLAN)
1460 zlog_debug("Unknown VNI %u upon remote MACIP DEL", vni);
6006414d 1461 return;
6006414d
PR
1462 }
1463
8b5fdf2e
PR
1464 ifp = zevpn->vxlan_if;
1465 if (ifp)
1466 zif = ifp->info;
1467 if (!ifp || !if_is_operative(ifp) || !zif || !zif->brslave_info.br_if) {
1468 if (IS_ZEBRA_DEBUG_VXLAN)
1469 zlog_debug(
1470 "Ignoring remote MACIP DEL VNI %u, invalid interface state or info",
1471 vni);
6006414d 1472 return;
8b5fdf2e
PR
1473 }
1474 zns = zebra_ns_lookup(NS_DEFAULT);
1475 vxl = &zif->l2info.vxl;
6006414d 1476
8b5fdf2e
PR
1477 mac = zebra_evpn_mac_lookup(zevpn, macaddr);
1478 if (ipa_len)
1479 n = zebra_evpn_neigh_lookup(zevpn, ipaddr);
6006414d 1480
8b5fdf2e
PR
1481 if (n && !mac) {
1482 zlog_warn(
ef7b8be4
DL
1483 "Failed to locate MAC %pEA for neigh %pIA VNI %u upon remote MACIP DEL",
1484 macaddr, ipaddr, vni);
6006414d 1485 return;
8b5fdf2e 1486 }
6006414d 1487
8b5fdf2e
PR
1488 /* If the remote mac or neighbor doesn't exist there is nothing
1489 * more to do. Otherwise, uninstall the entry and then remove it.
1490 */
1491 if (!mac && !n)
6006414d
PR
1492 return;
1493
8b5fdf2e 1494 zvrf = vrf_info_lookup(zevpn->vxlan_if->vrf_id);
6006414d 1495
8b5fdf2e
PR
1496 /* Ignore the delete if this mac is a gateway mac-ip */
1497 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)
1498 && CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW)) {
1499 zlog_warn(
ef7b8be4
DL
1500 "Ignore remote MACIP DEL VNI %u MAC %pEA%s%s as MAC is already configured as gateway MAC",
1501 vni, macaddr,
8b5fdf2e
PR
1502 ipa_len ? " IP " : "",
1503 ipa_len ? ipaddr2str(ipaddr, buf1, sizeof(buf1)) : "");
6006414d 1504 return;
8b5fdf2e 1505 }
6006414d 1506
8b5fdf2e
PR
1507 /* Uninstall remote neighbor or MAC. */
1508 if (n)
1509 zebra_evpn_neigh_remote_uninstall(zevpn, zvrf, n, mac, ipaddr);
1510 else {
1511 /* DAD: when MAC is freeze state as remote learn event,
1512 * remote mac-ip delete event is received will result in freeze
1513 * entry removal, first fetch kernel for the same entry present
1514 * as LOCAL and reachable, avoid deleting this entry instead
1515 * use kerenel local entry to update during unfreeze time.
1516 */
1517 if (zvrf->dad_freeze
1518 && CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)
1519 && CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) {
1520 if (IS_ZEBRA_DEBUG_VXLAN)
1521 zlog_debug(
ef7b8be4
DL
1522 "%s: MAC %pEA (flags 0x%x) is remote and duplicate, read kernel for local entry",
1523 __func__, macaddr, mac->flags);
8b5fdf2e
PR
1524 macfdb_read_specific_mac(zns, zif->brslave_info.br_if,
1525 macaddr, vxl->access_vlan);
1526 }
6006414d 1527
8b5fdf2e
PR
1528 if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) {
1529 if (!ipa_len)
1530 zebra_evpn_sync_mac_del(mac);
1531 } else if (CHECK_FLAG(mac->flags, ZEBRA_NEIGH_REMOTE)) {
1532 zebra_evpn_rem_mac_del(zevpn, mac);
1533 }
6006414d 1534 }
6006414d
PR
1535}
1536
1537/************************** EVPN BGP config management ************************/
8b5fdf2e 1538void zebra_evpn_cfg_cleanup(struct hash_bucket *bucket, void *ctxt)
6006414d
PR
1539{
1540 zebra_evpn_t *zevpn = NULL;
1541
1542 zevpn = (zebra_evpn_t *)bucket->data;
1543 zevpn->advertise_gw_macip = 0;
1544 zevpn->advertise_svi_macip = 0;
1545 zevpn->advertise_subnet = 0;
1546
1547 zebra_evpn_neigh_del_all(zevpn, 1, 0,
1548 DEL_REMOTE_NEIGH | DEL_REMOTE_NEIGH_FROM_VTEP);
1549 zebra_evpn_mac_del_all(zevpn, 1, 0,
1550 DEL_REMOTE_MAC | DEL_REMOTE_MAC_FROM_VTEP);
8b5fdf2e 1551 zebra_evpn_vtep_del_all(zevpn, 1);
6006414d 1552}