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