]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn.c
bgpd: move tunnel-ip comparison into handler
[mirror_frr.git] / bgpd / bgp_evpn.c
CommitLineData
7ef5a232 1/* Ethernet-VPN Packet and vty Processing File
896014f4 2 * Copyright (C) 2016 6WIND
128ea8ab 3 * Copyright (C) 2017 Cumulus Networks, Inc.
896014f4 4 *
128ea8ab 5 * This file is part of FRR.
896014f4
DL
6 *
7 * FRRouting 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 * FRRouting 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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
7ef5a232 21
7ef5a232
PG
22#include <zebra.h>
23
24#include "command.h"
25#include "filter.h"
26#include "prefix.h"
27#include "log.h"
28#include "memory.h"
29#include "stream.h"
14c1a7bf 30#include "hash.h"
31#include "jhash.h"
128ea8ab 32#include "zclient.h"
7ef5a232 33
23d0a753
DA
34#include "lib/printfrr.h"
35
7ef5a232
PG
36#include "bgpd/bgp_attr_evpn.h"
37#include "bgpd/bgpd.h"
38#include "bgpd/bgp_table.h"
39#include "bgpd/bgp_route.h"
40#include "bgpd/bgp_attr.h"
41#include "bgpd/bgp_mplsvpn.h"
9bedbb1e 42#include "bgpd/bgp_label.h"
7ef5a232 43#include "bgpd/bgp_evpn.h"
14c1a7bf 44#include "bgpd/bgp_evpn_private.h"
185fb14a 45#include "bgpd/bgp_evpn_mh.h"
14c1a7bf 46#include "bgpd/bgp_ecommunity.h"
128ea8ab 47#include "bgpd/bgp_encap_types.h"
48#include "bgpd/bgp_debug.h"
14454c9f 49#include "bgpd/bgp_errors.h"
128ea8ab 50#include "bgpd/bgp_aspath.h"
d7d97010 51#include "bgpd/bgp_zebra.h"
db0e1937 52#include "bgpd/bgp_nexthop.h"
dcc68b5e 53#include "bgpd/bgp_addpath.h"
47bf0432 54#include "bgpd/bgp_mac.h"
5d5393b9 55#include "bgpd/bgp_vty.h"
a2299aba 56#include "bgpd/bgp_nht.h"
23aa35ad 57#include "bgpd/bgp_trace.h"
128ea8ab 58
59/*
60 * Definitions and external declarations.
61 */
96244aca
DL
62DEFINE_QOBJ_TYPE(bgpevpn);
63DEFINE_QOBJ_TYPE(bgp_evpn_es);
128ea8ab 64
ca337b46 65DEFINE_MTYPE_STATIC(BGPD, VRF_ROUTE_TARGET, "L3 Route Target");
128ea8ab 66
67/*
68 * Static function declarations
69 */
021b6596
AD
70static void bgp_evpn_remote_ip_hash_init(struct bgpevpn *evpn);
71static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn *evpn);
72static void bgp_evpn_remote_ip_hash_add(struct bgpevpn *vpn,
73 struct bgp_path_info *pi);
74static void bgp_evpn_remote_ip_hash_del(struct bgpevpn *vpn,
75 struct bgp_path_info *pi);
dc6cef73
AD
76static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn *vpn,
77 void (*func)(struct hash_bucket *,
78 void *),
79 void *arg);
021b6596
AD
80static void bgp_evpn_link_to_vni_svi_hash(struct bgp *bgp, struct bgpevpn *vpn);
81static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp *bgp,
82 struct bgpevpn *vpn);
83static unsigned int vni_svi_hash_key_make(const void *p);
84static bool vni_svi_hash_cmp(const void *p1, const void *p2);
85static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn *vpn,
86 struct ipaddr *addr,
87 bool resolve);
88static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket *bucket,
89 void *args);
90static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket *bucket,
91 void *args);
9c7edc03 92static struct in_addr zero_vtep_ip;
14c1a7bf 93
94/*
95 * Private functions.
96 */
97
98/*
99 * Make vni hash key.
100 */
d8b87afe 101static unsigned int vni_hash_key_make(const void *p)
14c1a7bf 102{
d8b87afe 103 const struct bgpevpn *vpn = p;
d62a17ae 104 return (jhash_1word(vpn->vni, 0));
14c1a7bf 105}
106
107/*
108 * Comparison function for vni hash
109 */
74df8d6d 110static bool vni_hash_cmp(const void *p1, const void *p2)
14c1a7bf 111{
d62a17ae 112 const struct bgpevpn *vpn1 = p1;
113 const struct bgpevpn *vpn2 = p2;
14c1a7bf 114
ce40c627 115 return vpn1->vni == vpn2->vni;
14c1a7bf 116}
117
c44ab6f1 118int vni_list_cmp(void *p1, void *p2)
64465785
DS
119{
120 const struct bgpevpn *vpn1 = p1;
121 const struct bgpevpn *vpn2 = p2;
122
123 return vpn1->vni - vpn2->vni;
124}
125
10ebe1ab
MK
126/*
127 * Make vrf import route target hash key.
128 */
d8b87afe 129static unsigned int vrf_import_rt_hash_key_make(const void *p)
10ebe1ab 130{
d8b87afe
QY
131 const struct vrf_irt_node *irt = p;
132 const char *pnt = irt->rt.val;
5a1b3fb5
DS
133
134 return jhash(pnt, 8, 0x5abc1234);
10ebe1ab
MK
135}
136
137/*
138 * Comparison function for vrf import rt hash
139 */
74df8d6d 140static bool vrf_import_rt_hash_cmp(const void *p1, const void *p2)
10ebe1ab
MK
141{
142 const struct vrf_irt_node *irt1 = p1;
143 const struct vrf_irt_node *irt2 = p2;
144
10ebe1ab
MK
145 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
146}
147
148/*
f9b8094e 149 * Create a new vrf import_rt in evpn instance
10ebe1ab
MK
150 */
151static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt)
152{
5e53dce3 153 struct bgp *bgp_evpn = NULL;
10ebe1ab
MK
154 struct vrf_irt_node *irt;
155
5e53dce3
T
156 bgp_evpn = bgp_get_evpn();
157 if (!bgp_evpn) {
e50f7cfd 158 flog_err(EC_BGP_NO_DFLT,
f9b8094e 159 "vrf import rt new - evpn instance not created yet");
10ebe1ab
MK
160 return NULL;
161 }
162
163 irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT,
164 sizeof(struct vrf_irt_node));
10ebe1ab
MK
165
166 irt->rt = *rt;
167 irt->vrfs = list_new();
168
169 /* Add to hash */
ac390ef8 170 (void)hash_get(bgp_evpn->vrf_import_rt_hash, irt, hash_alloc_intern);
10ebe1ab
MK
171
172 return irt;
173}
174
175/*
176 * Free the vrf import rt node
177 */
178static void vrf_import_rt_free(struct vrf_irt_node *irt)
179{
5e53dce3 180 struct bgp *bgp_evpn = NULL;
10ebe1ab 181
5e53dce3
T
182 bgp_evpn = bgp_get_evpn();
183 if (!bgp_evpn) {
e50f7cfd 184 flog_err(EC_BGP_NO_DFLT,
f9b8094e 185 "vrf import rt free - evpn instance not created yet");
10ebe1ab
MK
186 return;
187 }
188
5e53dce3 189 hash_release(bgp_evpn->vrf_import_rt_hash, irt);
6a154c88 190 list_delete(&irt->vrfs);
10ebe1ab
MK
191 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
192}
193
3727e359
DA
194static void hash_vrf_import_rt_free(struct vrf_irt_node *irt)
195{
196 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
197}
198
10ebe1ab
MK
199/*
200 * Function to lookup Import RT node - used to map a RT to set of
201 * VNIs importing routes with that RT.
202 */
203static struct vrf_irt_node *lookup_vrf_import_rt(struct ecommunity_val *rt)
204{
5e53dce3 205 struct bgp *bgp_evpn = NULL;
10ebe1ab
MK
206 struct vrf_irt_node *irt;
207 struct vrf_irt_node tmp;
208
5e53dce3
T
209 bgp_evpn = bgp_get_evpn();
210 if (!bgp_evpn) {
f9b8094e
T
211 flog_err(
212 EC_BGP_NO_DFLT,
213 "vrf import rt lookup - evpn instance not created yet");
10ebe1ab
MK
214 return NULL;
215 }
216
6006b807 217 memset(&tmp, 0, sizeof(tmp));
10ebe1ab 218 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
5e53dce3 219 irt = hash_lookup(bgp_evpn->vrf_import_rt_hash, &tmp);
10ebe1ab
MK
220 return irt;
221}
222
223/*
224 * Is specified VRF present on the RT's list of "importing" VRFs?
225 */
996c9314 226static int is_vrf_present_in_irt_vrfs(struct list *vrfs, struct bgp *bgp_vrf)
10ebe1ab
MK
227{
228 struct listnode *node = NULL, *nnode = NULL;
229 struct bgp *tmp_bgp_vrf = NULL;
230
231 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, tmp_bgp_vrf)) {
232 if (tmp_bgp_vrf == bgp_vrf)
233 return 1;
234 }
235 return 0;
236}
237
14c1a7bf 238/*
239 * Make import route target hash key.
240 */
d8b87afe 241static unsigned int import_rt_hash_key_make(const void *p)
14c1a7bf 242{
d8b87afe
QY
243 const struct irt_node *irt = p;
244 const char *pnt = irt->rt.val;
5a1b3fb5
DS
245
246 return jhash(pnt, 8, 0xdeadbeef);
14c1a7bf 247}
248
249/*
250 * Comparison function for import rt hash
251 */
74df8d6d 252static bool import_rt_hash_cmp(const void *p1, const void *p2)
14c1a7bf 253{
d62a17ae 254 const struct irt_node *irt1 = p1;
255 const struct irt_node *irt2 = p2;
14c1a7bf 256
d62a17ae 257 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
14c1a7bf 258}
259
7724c0a1 260/*
128ea8ab 261 * Create a new import_rt
262 */
d62a17ae 263static struct irt_node *import_rt_new(struct bgp *bgp,
264 struct ecommunity_val *rt)
128ea8ab 265{
d62a17ae 266 struct irt_node *irt;
128ea8ab 267
d62a17ae 268 irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
128ea8ab 269
d62a17ae 270 irt->rt = *rt;
271 irt->vnis = list_new();
128ea8ab 272
d62a17ae 273 /* Add to hash */
ac390ef8 274 (void)hash_get(bgp->import_rt_hash, irt, hash_alloc_intern);
128ea8ab 275
d62a17ae 276 return irt;
128ea8ab 277}
278
279/*
280 * Free the import rt node
7724c0a1 281 */
d62a17ae 282static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
7724c0a1 283{
d62a17ae 284 hash_release(bgp->import_rt_hash, irt);
6a154c88 285 list_delete(&irt->vnis);
d62a17ae 286 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
7724c0a1 287}
288
3727e359
DA
289static void hash_import_rt_free(struct irt_node *irt)
290{
291 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
292}
293
14c1a7bf 294/*
128ea8ab 295 * Function to lookup Import RT node - used to map a RT to set of
296 * VNIs importing routes with that RT.
297 */
d62a17ae 298static struct irt_node *lookup_import_rt(struct bgp *bgp,
299 struct ecommunity_val *rt)
128ea8ab 300{
d62a17ae 301 struct irt_node *irt;
302 struct irt_node tmp;
128ea8ab 303
6006b807 304 memset(&tmp, 0, sizeof(tmp));
d62a17ae 305 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
306 irt = hash_lookup(bgp->import_rt_hash, &tmp);
307 return irt;
128ea8ab 308}
309
310/*
311 * Is specified VNI present on the RT's list of "importing" VNIs?
312 */
d62a17ae 313static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
128ea8ab 314{
d62a17ae 315 struct listnode *node, *nnode;
316 struct bgpevpn *tmp_vpn;
128ea8ab 317
d62a17ae 318 for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
319 if (tmp_vpn == vpn)
320 return 1;
321 }
128ea8ab 322
d62a17ae 323 return 0;
128ea8ab 324}
325
326/*
327 * Compare Route Targets.
328 */
f4a5218d
AK
329int bgp_evpn_route_target_cmp(struct ecommunity *ecom1,
330 struct ecommunity *ecom2)
128ea8ab 331{
d62a17ae 332 if (ecom1 && !ecom2)
333 return -1;
128ea8ab 334
d62a17ae 335 if (!ecom1 && ecom2)
336 return 1;
128ea8ab 337
d62a17ae 338 if (!ecom1 && !ecom2)
339 return 0;
128ea8ab 340
d62a17ae 341 if (ecom1->str && !ecom2->str)
342 return -1;
128ea8ab 343
d62a17ae 344 if (!ecom1->str && ecom2->str)
345 return 1;
128ea8ab 346
d62a17ae 347 if (!ecom1->str && !ecom2->str)
348 return 0;
128ea8ab 349
d62a17ae 350 return strcmp(ecom1->str, ecom2->str);
128ea8ab 351}
352
ca337b46
SW
353/*
354 * Compare L3 Route Targets.
355 */
356static int evpn_vrf_route_target_cmp(struct vrf_route_target *rt1,
357 struct vrf_route_target *rt2)
358{
359 return bgp_evpn_route_target_cmp(rt1->ecom, rt2->ecom);
360}
361
f4a5218d 362void bgp_evpn_xxport_delete_ecomm(void *val)
987d8198
DS
363{
364 struct ecommunity *ecomm = val;
365 ecommunity_free(&ecomm);
366}
367
ca337b46
SW
368/*
369 * Delete l3 Route Target.
370 */
371static void evpn_vrf_rt_del(void *val)
372{
373 struct vrf_route_target *l3rt = val;
374
375 ecommunity_free(&l3rt->ecom);
376
377 XFREE(MTYPE_VRF_ROUTE_TARGET, l3rt);
378}
379
380/*
381 * Allocate a new l3 Route Target.
382 */
383static struct vrf_route_target *evpn_vrf_rt_new(struct ecommunity *ecom)
384{
385 struct vrf_route_target *l3rt;
386
387 l3rt = XCALLOC(MTYPE_VRF_ROUTE_TARGET, sizeof(struct vrf_route_target));
388
389 l3rt->ecom = ecom;
390
391 return l3rt;
392}
393
128ea8ab 394/*
395 * Mask off global-admin field of specified extended community (RT),
396 * just retain the local-admin field.
397 */
d62a17ae 398static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
58d8948c 399 const struct ecommunity_val *src)
128ea8ab 400{
d7c0a89a 401 uint8_t type;
128ea8ab 402
d62a17ae 403 type = src->val[0];
404 dst->val[0] = 0;
405 if (type == ECOMMUNITY_ENCODE_AS) {
406 dst->val[2] = dst->val[3] = 0;
407 } else if (type == ECOMMUNITY_ENCODE_AS4
408 || type == ECOMMUNITY_ENCODE_IP) {
409 dst->val[2] = dst->val[3] = 0;
410 dst->val[4] = dst->val[5] = 0;
411 }
128ea8ab 412}
413
10ebe1ab 414/*
58d8948c
SW
415 * Converts the RT to Ecommunity Value and adjusts masking based
416 * on flags set for RT.
10ebe1ab 417 */
58d8948c
SW
418static void vrf_rt2ecom_val(struct ecommunity_val *to_eval,
419 const struct vrf_route_target *l3rt, int iter)
10ebe1ab 420{
58d8948c 421 const struct ecommunity_val *eval;
10ebe1ab 422
58d8948c
SW
423 eval = (const struct ecommunity_val *)(l3rt->ecom->val +
424 (iter * ECOMMUNITY_SIZE));
425 /* If using "automatic" or "wildcard *" RT,
10ebe1ab
MK
426 * we only care about the local-admin sub-field.
427 * This is to facilitate using L3VNI(VRF-VNI)
58d8948c
SW
428 * as the RT for EBGP peering too and simplify
429 * configurations by allowing any ASN via '*'.
10ebe1ab 430 */
58d8948c 431 memcpy(to_eval, eval, ECOMMUNITY_SIZE);
10ebe1ab 432
58d8948c
SW
433 if (CHECK_FLAG(l3rt->flags, BGP_VRF_RT_AUTO) ||
434 CHECK_FLAG(l3rt->flags, BGP_VRF_RT_WILD))
435 mask_ecom_global_admin(to_eval, eval);
436}
10ebe1ab 437
58d8948c
SW
438/*
439 * Map one RT to specified VRF.
440 * bgp_vrf = BGP vrf instance
441 */
442static void map_vrf_to_rt(struct bgp *bgp_vrf, struct vrf_route_target *l3rt)
443{
444 uint32_t i = 0;
445
446 for (i = 0; i < l3rt->ecom->size; i++) {
447 struct vrf_irt_node *irt = NULL;
448 struct ecommunity_val eval_tmp;
449
450 /* Adjust masking for value */
451 vrf_rt2ecom_val(&eval_tmp, l3rt, i);
10ebe1ab 452
58d8948c 453 irt = lookup_vrf_import_rt(&eval_tmp);
10ebe1ab 454
58d8948c
SW
455 if (irt && is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
456 return; /* Already mapped. */
457
458 if (!irt)
459 irt = vrf_import_rt_new(&eval_tmp);
460
461 /* Add VRF to the list for this RT. */
462 listnode_add(irt->vrfs, bgp_vrf);
463 }
10ebe1ab
MK
464}
465
466/*
467 * Unmap specified VRF from specified RT. If there are no other
468 * VRFs for this RT, then the RT hash is deleted.
469 * bgp_vrf: BGP VRF specific instance
470 */
58d8948c
SW
471static void unmap_vrf_from_rt(struct bgp *bgp_vrf,
472 struct vrf_route_target *l3rt)
10ebe1ab 473{
58d8948c
SW
474 uint32_t i;
475
476 for (i = 0; i < l3rt->ecom->size; i++) {
477 struct vrf_irt_node *irt;
478 struct ecommunity_val eval_tmp;
479
480 /* Adjust masking for value */
481 vrf_rt2ecom_val(&eval_tmp, l3rt, i);
482
483 irt = lookup_vrf_import_rt(&eval_tmp);
484
485 if (!irt)
486 return; /* Not mapped */
487
488 /* Delete VRF from list for this RT. */
489 listnode_delete(irt->vrfs, bgp_vrf);
490
491 if (!listnode_head(irt->vrfs))
492 vrf_import_rt_free(irt);
10ebe1ab
MK
493 }
494}
495
128ea8ab 496/*
497 * Map one RT to specified VNI.
14c1a7bf 498 */
d62a17ae 499static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
500 struct ecommunity_val *eval)
128ea8ab 501{
d62a17ae 502 struct irt_node *irt;
503 struct ecommunity_val eval_tmp;
128ea8ab 504
d62a17ae 505 /* If using "automatic" RT, we only care about the local-admin
506 * sub-field.
507 * This is to facilitate using VNI as the RT for EBGP peering too.
508 */
509 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
510 if (!is_import_rt_configured(vpn))
511 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 512
d62a17ae 513 irt = lookup_import_rt(bgp, &eval_tmp);
b1ab0dfe 514 if (irt)
d62a17ae 515 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
516 /* Already mapped. */
517 return;
128ea8ab 518
879e43a5 519 if (!irt)
d62a17ae 520 irt = import_rt_new(bgp, &eval_tmp);
128ea8ab 521
d62a17ae 522 /* Add VNI to the hash list for this RT. */
523 listnode_add(irt->vnis, vpn);
128ea8ab 524}
525
526/*
527 * Unmap specified VNI from specified RT. If there are no other
528 * VNIs for this RT, then the RT hash is deleted.
529 */
d62a17ae 530static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
531 struct irt_node *irt)
14c1a7bf 532{
d62a17ae 533 /* Delete VNI from hash list for this RT. */
534 listnode_delete(irt->vnis, vpn);
535 if (!listnode_head(irt->vnis)) {
d62a17ae 536 import_rt_free(bgp, irt);
537 }
14c1a7bf 538}
539
0ca10580 540static void bgp_evpn_get_rmac_nexthop(struct bgpevpn *vpn,
bd494ec5 541 const struct prefix_evpn *p,
0ca10580
CS
542 struct attr *attr, uint8_t flags)
543{
544 struct bgp *bgp_vrf = vpn->bgp_vrf;
545
546 memset(&attr->rmac, 0, sizeof(struct ethaddr));
547 if (!bgp_vrf)
548 return;
549
b96cafa3
CS
550 if (p->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
551 return;
552
553 /* Copy sys (pip) RMAC and PIP IP as nexthop
554 * in case of route is self MAC-IP,
555 * advertise-pip and advertise-svi-ip features
556 * are enabled.
557 * Otherwise, for all host MAC-IP route's
27727001 558 * copy anycast RMAC.
b96cafa3
CS
559 */
560 if (CHECK_FLAG(flags, BGP_EVPN_MACIP_TYPE_SVI_IP)
561 && bgp_vrf->evpn_info->advertise_pip &&
562 bgp_vrf->evpn_info->is_anycast_mac) {
563 /* copy sys rmac */
564 memcpy(&attr->rmac, &bgp_vrf->evpn_info->pip_rmac,
565 ETH_ALEN);
566 attr->nexthop = bgp_vrf->evpn_info->pip_ip;
567 attr->mp_nexthop_global_in =
568 bgp_vrf->evpn_info->pip_ip;
569 } else
570 memcpy(&attr->rmac, &bgp_vrf->rmac, ETH_ALEN);
0ca10580 571}
b96cafa3 572
128ea8ab 573/*
574 * Create RT extended community automatically from passed information:
575 * of the form AS:VNI.
576 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
577 * the need is to get a RT value that will be unique across different
578 * VNIs but the same across routers (in the same AS) for a particular
579 * VNI.
580 */
58d8948c
SW
581static void form_auto_rt(struct bgp *bgp, vni_t vni, struct list *rtl,
582 bool is_l3)
128ea8ab 583{
d62a17ae 584 struct ecommunity_val eval;
ca337b46 585 struct ecommunity *ecomadd;
58d8948c 586 struct ecommunity *ecom;
ca337b46
SW
587 struct vrf_route_target *l3rt;
588 struct vrf_route_target *newrt;
b90d4580
CS
589 bool ecom_found = false;
590 struct listnode *node;
128ea8ab 591
bf1061d8
VB
592 if (bgp->advertise_autort_rfc8365)
593 vni |= EVPN_AUTORT_VXLAN;
c581d8b0 594 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
128ea8ab 595
d62a17ae 596 ecomadd = ecommunity_new();
1207a5bc 597 ecommunity_add_val(ecomadd, &eval, false, false);
b90d4580 598
58d8948c
SW
599 if (is_l3) {
600 for (ALL_LIST_ELEMENTS_RO(rtl, node, l3rt))
601 if (ecommunity_cmp(ecomadd, l3rt->ecom)) {
602 ecom_found = true;
603 break;
604 }
605 } else {
606 for (ALL_LIST_ELEMENTS_RO(rtl, node, ecom))
607 if (ecommunity_cmp(ecomadd, ecom)) {
608 ecom_found = true;
609 break;
610 }
611 }
b90d4580 612
ca337b46 613 if (!ecom_found) {
58d8948c
SW
614 if (is_l3) {
615 newrt = evpn_vrf_rt_new(ecomadd);
616 /* Label it as autoderived */
617 SET_FLAG(newrt->flags, BGP_VRF_RT_AUTO);
618 listnode_add_sort(rtl, newrt);
619 } else
620 listnode_add_sort(rtl, ecomadd);
ca337b46 621 } else
84f22ecc 622 ecommunity_free(&ecomadd);
128ea8ab 623}
14c1a7bf 624
625/*
128ea8ab 626 * Derive RD and RT for a VNI automatically. Invoked at the time of
627 * creation of a VNI.
628 */
d62a17ae 629static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 630{
d62a17ae 631 bgp_evpn_derive_auto_rd(bgp, vpn);
632 bgp_evpn_derive_auto_rt_import(bgp, vpn);
633 bgp_evpn_derive_auto_rt_export(bgp, vpn);
128ea8ab 634}
635
1ec31309 636/*
637 * Convert nexthop (remote VTEP IP) into an IPv6 address.
638 */
639static void evpn_convert_nexthop_to_ipv6(struct attr *attr)
640{
641 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr))
642 return;
643 ipv4_to_ipv4_mapped_ipv6(&attr->mp_nexthop_global, attr->nexthop);
644 attr->mp_nexthop_len = IPV6_MAX_BYTELEN;
645}
646
34c7f35f
SW
647/*
648 * Wrapper for node get in global table.
649 */
650struct bgp_dest *bgp_evpn_global_node_get(struct bgp_table *table, afi_t afi,
09319b4e
DS
651 safi_t safi,
652 const struct prefix_evpn *evp,
34c7f35f
SW
653 struct prefix_rd *prd,
654 const struct bgp_path_info *local_pi)
c44ab6f1
AK
655{
656 struct prefix_evpn global_p;
657
658 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE) {
659 /* prefix in the global table doesn't include the VTEP-IP so
660 * we need to create a different copy of the prefix
661 */
662 evpn_type1_prefix_global_copy(&global_p, evp);
663 evp = &global_p;
34c7f35f
SW
664 } else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
665 local_pi) {
666 /*
852d9f97
SW
667 * prefix in the global table needs MAC/IP, ensure they are
668 * present, using one's from local table's path_info.
34c7f35f 669 */
852d9f97
SW
670 if (is_evpn_prefix_ipaddr_none(evp)) {
671 /* VNI MAC -> Global */
672 evpn_type2_prefix_global_copy(
673 &global_p, evp, NULL /* mac */,
674 evpn_type2_path_info_get_ip(local_pi));
675 } else {
676 /* VNI IP -> Global */
677 evpn_type2_prefix_global_copy(
678 &global_p, evp,
679 evpn_type2_path_info_get_mac(local_pi),
680 NULL /* ip */);
681 }
682
c44ab6f1
AK
683 evp = &global_p;
684 }
685 return bgp_afi_node_get(table, afi, safi, (struct prefix *)evp, prd);
686}
687
34c7f35f
SW
688/*
689 * Wrapper for node lookup in global table.
690 */
691struct bgp_dest *
692bgp_evpn_global_node_lookup(struct bgp_table *table, afi_t afi, safi_t safi,
693 const struct prefix_evpn *evp,
694 struct prefix_rd *prd,
695 const struct bgp_path_info *local_pi)
c44ab6f1
AK
696{
697 struct prefix_evpn global_p;
698
699 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE) {
700 /* prefix in the global table doesn't include the VTEP-IP so
701 * we need to create a different copy of the prefix
702 */
703 evpn_type1_prefix_global_copy(&global_p, evp);
704 evp = &global_p;
34c7f35f
SW
705 } else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
706 local_pi) {
707 /*
852d9f97
SW
708 * prefix in the global table needs MAC/IP, ensure they are
709 * present, using one's from local table's path_info.
34c7f35f 710 */
852d9f97
SW
711 if (is_evpn_prefix_ipaddr_none(evp)) {
712 /* VNI MAC -> Global */
713 evpn_type2_prefix_global_copy(
714 &global_p, evp, NULL /* mac */,
715 evpn_type2_path_info_get_ip(local_pi));
716 } else {
717 /* VNI IP -> Global */
718 evpn_type2_prefix_global_copy(
719 &global_p, evp,
720 evpn_type2_path_info_get_mac(local_pi),
721 NULL /* ip */);
722 }
723
c44ab6f1
AK
724 evp = &global_p;
725 }
726 return bgp_afi_node_lookup(table, afi, safi, (struct prefix *)evp, prd);
727}
728
34c7f35f 729/*
852d9f97 730 * Wrapper for node get in VNI IP table.
34c7f35f 731 */
852d9f97
SW
732struct bgp_dest *bgp_evpn_vni_ip_node_get(struct bgp_table *const table,
733 const struct prefix_evpn *evp,
734 const struct bgp_path_info *parent_pi)
34c7f35f
SW
735{
736 struct prefix_evpn vni_p;
737
738 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE && parent_pi) {
739 /* prefix in the global table doesn't include the VTEP-IP so
740 * we need to create a different copy for the VNI
741 */
852d9f97
SW
742 evpn_type1_prefix_vni_ip_copy(&vni_p, evp,
743 parent_pi->attr->nexthop);
34c7f35f 744 evp = &vni_p;
852d9f97
SW
745 } else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
746 /* Only MAC-IP should go into this table, not mac-only */
747 assert(is_evpn_prefix_ipaddr_none(evp) == false);
748
34c7f35f 749 /*
852d9f97 750 * prefix in the vni IP table doesn't include MAC so
34c7f35f 751 * we need to create a different copy of the prefix.
34c7f35f 752 */
852d9f97 753 evpn_type2_prefix_vni_ip_copy(&vni_p, evp);
34c7f35f
SW
754 evp = &vni_p;
755 }
756 return bgp_node_get(table, (struct prefix *)evp);
757}
758
759/*
852d9f97 760 * Wrapper for node lookup in VNI IP table.
34c7f35f 761 */
852d9f97
SW
762struct bgp_dest *
763bgp_evpn_vni_ip_node_lookup(const struct bgp_table *const table,
764 const struct prefix_evpn *evp,
765 const struct bgp_path_info *parent_pi)
34c7f35f
SW
766{
767 struct prefix_evpn vni_p;
768
769 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE && parent_pi) {
770 /* prefix in the global table doesn't include the VTEP-IP so
771 * we need to create a different copy for the VNI
772 */
852d9f97
SW
773 evpn_type1_prefix_vni_ip_copy(&vni_p, evp,
774 parent_pi->attr->nexthop);
34c7f35f 775 evp = &vni_p;
852d9f97
SW
776 } else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
777 /* Only MAC-IP should go into this table, not mac-only */
778 assert(is_evpn_prefix_ipaddr_none(evp) == false);
779
34c7f35f 780 /*
852d9f97 781 * prefix in the vni IP table doesn't include MAC so
34c7f35f 782 * we need to create a different copy of the prefix.
34c7f35f 783 */
852d9f97 784 evpn_type2_prefix_vni_ip_copy(&vni_p, evp);
34c7f35f
SW
785 evp = &vni_p;
786 }
787 return bgp_node_lookup(table, (struct prefix *)evp);
788}
789
852d9f97
SW
790/*
791 * Wrapper for node get in VNI MAC table.
792 */
793struct bgp_dest *
794bgp_evpn_vni_mac_node_get(struct bgp_table *const table,
795 const struct prefix_evpn *evp,
796 const struct bgp_path_info *parent_pi)
797{
798 struct prefix_evpn vni_p;
799
800 /* Only type-2 should ever go into this table */
801 assert(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE);
802
803 /*
804 * prefix in the vni MAC table doesn't include IP so
805 * we need to create a different copy of the prefix.
806 */
807 evpn_type2_prefix_vni_mac_copy(&vni_p, evp);
808 evp = &vni_p;
809 return bgp_node_get(table, (struct prefix *)evp);
810}
811
812/*
813 * Wrapper for node lookup in VNI MAC table.
814 */
815struct bgp_dest *
816bgp_evpn_vni_mac_node_lookup(const struct bgp_table *const table,
817 const struct prefix_evpn *evp,
818 const struct bgp_path_info *parent_pi)
819{
820 struct prefix_evpn vni_p;
821
822 /* Only type-2 should ever go into this table */
823 assert(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE);
824
825 /*
826 * prefix in the vni MAC table doesn't include IP so
827 * we need to create a different copy of the prefix.
828 */
829 evpn_type2_prefix_vni_mac_copy(&vni_p, evp);
830 evp = &vni_p;
831 return bgp_node_lookup(table, (struct prefix *)evp);
832}
833
834/*
835 * Wrapper for node get in both VNI tables.
836 */
837struct bgp_dest *bgp_evpn_vni_node_get(struct bgpevpn *vpn,
838 const struct prefix_evpn *p,
839 const struct bgp_path_info *parent_pi)
840{
841 if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
842 (is_evpn_prefix_ipaddr_none(p) == true))
843 return bgp_evpn_vni_mac_node_get(vpn->mac_table, p, parent_pi);
844
845 return bgp_evpn_vni_ip_node_get(vpn->ip_table, p, parent_pi);
846}
847
848/*
849 * Wrapper for node lookup in both VNI tables.
850 */
851struct bgp_dest *bgp_evpn_vni_node_lookup(const struct bgpevpn *vpn,
852 const struct prefix_evpn *p,
853 const struct bgp_path_info *parent_pi)
854{
855 if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
856 (is_evpn_prefix_ipaddr_none(p) == true))
857 return bgp_evpn_vni_mac_node_lookup(vpn->mac_table, p,
858 parent_pi);
859
860 return bgp_evpn_vni_ip_node_lookup(vpn->ip_table, p, parent_pi);
861}
862
128ea8ab 863/*
864 * Add (update) or delete MACIP from zebra.
14c1a7bf 865 */
d62a17ae 866static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
bd494ec5 867 const struct prefix_evpn *p,
34c7f35f 868 const struct ethaddr *mac,
d62a17ae 869 struct in_addr remote_vtep_ip, int add,
c44ab6f1 870 uint8_t flags, uint32_t seq, esi_t *esi)
d62a17ae 871{
872 struct stream *s;
0ffd0fb5 873 uint16_t ipa_len;
c44ab6f1 874 static struct in_addr zero_remote_vtep_ip;
90f30caa 875 bool esi_valid;
d62a17ae 876
877 /* Check socket. */
878 if (!zclient || zclient->sock < 0)
879 return 0;
880
881 /* Don't try to register if Zebra doesn't know of this instance. */
bb4ef1ae
DS
882 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
883 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
884 zlog_debug(
885 "%s: No zebra instance to talk to, not installing remote macip",
886 __func__);
d62a17ae 887 return 0;
bb4ef1ae 888 }
c44ab6f1
AK
889
890 if (!esi)
891 esi = zero_esi;
d62a17ae 892 s = zclient->obuf;
893 stream_reset(s);
894
996c9314
LB
895 zclient_create_header(
896 s, add ? ZEBRA_REMOTE_MACIP_ADD : ZEBRA_REMOTE_MACIP_DEL,
897 bgp->vrf_id);
d62a17ae 898 stream_putl(s, vpn->vni);
34c7f35f
SW
899
900 if (mac) /* Mac Addr */
901 stream_put(s, &mac->octet, ETH_ALEN);
902 else
903 stream_put(s, &p->prefix.macip_addr.mac.octet, ETH_ALEN);
904
d62a17ae 905 /* IP address length and IP address, if any. */
3714a385 906 if (is_evpn_prefix_ipaddr_none(p))
0ffd0fb5 907 stream_putw(s, 0);
d62a17ae 908 else {
3714a385 909 ipa_len = is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BYTELEN
d62a17ae 910 : IPV6_MAX_BYTELEN;
0ffd0fb5 911 stream_putw(s, ipa_len);
3714a385 912 stream_put(s, &p->prefix.macip_addr.ip.ip.addr, ipa_len);
d62a17ae 913 }
9c7edc03
AK
914 /* If the ESI is valid that becomes the nexthop; tape out the
915 * VTEP-IP for that case
c44ab6f1 916 */
90f30caa
AK
917 if (bgp_evpn_is_esi_valid(esi)) {
918 esi_valid = true;
c44ab6f1 919 stream_put_in_addr(s, &zero_remote_vtep_ip);
90f30caa
AK
920 } else {
921 esi_valid = false;
9c7edc03 922 stream_put_in_addr(s, &remote_vtep_ip);
90f30caa 923 }
d62a17ae 924
ead40654 925 /* TX flags - MAC sticky status and/or gateway mac */
f07e1c99 926 /* Also TX the sequence number of the best route. */
927 if (add) {
ead40654 928 stream_putc(s, flags);
f07e1c99 929 stream_putl(s, seq);
c44ab6f1 930 stream_put(s, esi, sizeof(esi_t));
f07e1c99 931 }
d62a17ae 932
933 stream_putw_at(s, 0, stream_get_endp(s));
934
90f30caa
AK
935 if (bgp_debug_zebra(NULL)) {
936 char esi_buf[ESI_STR_LEN];
937
938 if (esi_valid)
939 esi_to_str(esi, esi_buf, sizeof(esi_buf));
940 else
941 snprintf(esi_buf, sizeof(esi_buf), "-");
996c9314 942 zlog_debug(
90f30caa 943 "Tx %s MACIP, VNI %u MAC %pEA IP %pIA flags 0x%x seq %u remote VTEP %pI4 esi %s",
996c9314 944 add ? "ADD" : "DEL", vpn->vni,
852d9f97
SW
945 (mac ? mac : &p->prefix.macip_addr.mac),
946 &p->prefix.macip_addr.ip, flags, seq, &remote_vtep_ip,
947 esi_buf);
90f30caa 948 }
d62a17ae 949
23aa35ad
AK
950 frrtrace(5, frr_bgp, evpn_mac_ip_zsend, add, vpn, p, remote_vtep_ip,
951 esi);
952
d62a17ae 953 return zclient_send_message(zclient);
7ef5a232 954}
b18825eb 955
128ea8ab 956/*
957 * Add (update) or delete remote VTEP from zebra.
958 */
d62a17ae 959static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
bd494ec5
DS
960 const struct prefix_evpn *p,
961 int flood_control, int add)
128ea8ab 962{
d62a17ae 963 struct stream *s;
128ea8ab 964
d62a17ae 965 /* Check socket. */
966 if (!zclient || zclient->sock < 0)
967 return 0;
128ea8ab 968
d62a17ae 969 /* Don't try to register if Zebra doesn't know of this instance. */
bb4ef1ae
DS
970 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
971 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
972 zlog_debug(
973 "%s: No zebra instance to talk to, not installing remote vtep",
974 __func__);
d62a17ae 975 return 0;
bb4ef1ae 976 }
128ea8ab 977
d62a17ae 978 s = zclient->obuf;
979 stream_reset(s);
128ea8ab 980
996c9314
LB
981 zclient_create_header(
982 s, add ? ZEBRA_REMOTE_VTEP_ADD : ZEBRA_REMOTE_VTEP_DEL,
983 bgp->vrf_id);
d62a17ae 984 stream_putl(s, vpn->vni);
3714a385 985 if (is_evpn_prefix_ipaddr_v4(p))
986 stream_put_in_addr(s, &p->prefix.imet_addr.ip.ipaddr_v4);
987 else if (is_evpn_prefix_ipaddr_v6(p)) {
af4c2728 988 flog_err(
e50f7cfd 989 EC_BGP_VTEP_INVALID,
d62a17ae 990 "Bad remote IP when trying to %s remote VTEP for VNI %u",
991 add ? "ADD" : "DEL", vpn->vni);
992 return -1;
993 }
b16dd019 994 stream_putl(s, flood_control);
128ea8ab 995
d62a17ae 996 stream_putw_at(s, 0, stream_get_endp(s));
128ea8ab 997
d62a17ae 998 if (bgp_debug_zebra(NULL))
23d0a753 999 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %pI4",
d62a17ae 1000 add ? "ADD" : "DEL", vpn->vni,
23d0a753 1001 &p->prefix.imet_addr.ip.ipaddr_v4);
128ea8ab 1002
23aa35ad
AK
1003 frrtrace(3, frr_bgp, evpn_bum_vtep_zsend, add, vpn, p);
1004
d62a17ae 1005 return zclient_send_message(zclient);
128ea8ab 1006}
1007
342dd0c6 1008/*
1009 * Build extended communities for EVPN prefix route.
1010 */
1011static void build_evpn_type5_route_extcomm(struct bgp *bgp_vrf,
1012 struct attr *attr)
1013{
1014 struct ecommunity ecom_encap;
342dd0c6 1015 struct ecommunity_val eval;
1016 struct ecommunity_val eval_rmac;
1017 bgp_encap_types tnl_type;
1018 struct listnode *node, *nnode;
ca337b46 1019 struct vrf_route_target *l3rt;
fab92da7 1020 struct ecommunity *old_ecom;
ca337b46 1021 struct ecommunity *ecom;
342dd0c6 1022 struct list *vrf_export_rtl = NULL;
1023
1024 /* Encap */
1025 tnl_type = BGP_ENCAP_TYPE_VXLAN;
1026 memset(&ecom_encap, 0, sizeof(ecom_encap));
1027 encode_encap_extcomm(tnl_type, &eval);
1028 ecom_encap.size = 1;
34540b0d 1029 ecom_encap.unit_size = ECOMMUNITY_SIZE;
d7c0a89a 1030 ecom_encap.val = (uint8_t *)eval.val;
342dd0c6 1031
1032 /* Add Encap */
b53e67a3
DA
1033 if (bgp_attr_get_ecommunity(attr)) {
1034 old_ecom = bgp_attr_get_ecommunity(attr);
fab92da7 1035 ecom = ecommunity_merge(ecommunity_dup(old_ecom), &ecom_encap);
1036 if (!old_ecom->refcnt)
1037 ecommunity_free(&old_ecom);
1038 } else
1039 ecom = ecommunity_dup(&ecom_encap);
b53e67a3 1040 bgp_attr_set_ecommunity(attr, ecom);
5bbd2cc1 1041 attr->encap_tunneltype = tnl_type;
342dd0c6 1042
1043 /* Add the export RTs for L3VNI/VRF */
1044 vrf_export_rtl = bgp_vrf->vrf_export_rtl;
ca337b46 1045 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, l3rt))
b53e67a3 1046 bgp_attr_set_ecommunity(
ca337b46
SW
1047 attr, ecommunity_merge(bgp_attr_get_ecommunity(attr),
1048 l3rt->ecom));
342dd0c6 1049
1050 /* add the router mac extended community */
1051 if (!is_zero_mac(&attr->rmac)) {
342dd0c6 1052 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
b53e67a3
DA
1053 ecommunity_add_val(bgp_attr_get_ecommunity(attr), &eval_rmac,
1054 true, true);
342dd0c6 1055 }
342dd0c6 1056}
1057
128ea8ab 1058/*
1ec31309 1059 * Build extended communities for EVPN route.
1060 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
1061 * and ENCAP extended communities are applicable for all routes.
1062 * The default gateway extended community and MAC mobility (sticky) extended
1063 * community are added as needed based on passed settings - only for type-2
1064 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
1065 * added, if present, based on passed settings - only for non-link-local
1066 * type-2 routes.
128ea8ab 1067 */
7ec156a9 1068static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
1ec31309 1069 int add_l3_ecomm)
128ea8ab 1070{
d62a17ae 1071 struct ecommunity ecom_encap;
1072 struct ecommunity ecom_sticky;
ead40654 1073 struct ecommunity ecom_default_gw;
68e33151 1074 struct ecommunity ecom_na;
d62a17ae 1075 struct ecommunity_val eval;
1076 struct ecommunity_val eval_sticky;
ead40654 1077 struct ecommunity_val eval_default_gw;
bc59a672 1078 struct ecommunity_val eval_rmac;
68e33151 1079 struct ecommunity_val eval_na;
9c7edc03 1080 bool proxy;
68e33151 1081
d62a17ae 1082 bgp_encap_types tnl_type;
1083 struct listnode *node, *nnode;
1084 struct ecommunity *ecom;
ca337b46 1085 struct vrf_route_target *l3rt;
d7c0a89a 1086 uint32_t seqnum;
7a3e76f1 1087 struct list *vrf_export_rtl = NULL;
128ea8ab 1088
d62a17ae 1089 /* Encap */
1090 tnl_type = BGP_ENCAP_TYPE_VXLAN;
1091 memset(&ecom_encap, 0, sizeof(ecom_encap));
1092 encode_encap_extcomm(tnl_type, &eval);
1093 ecom_encap.size = 1;
34540b0d 1094 ecom_encap.unit_size = ECOMMUNITY_SIZE;
d7c0a89a 1095 ecom_encap.val = (uint8_t *)eval.val;
128ea8ab 1096
d62a17ae 1097 /* Add Encap */
b53e67a3 1098 bgp_attr_set_ecommunity(attr, ecommunity_dup(&ecom_encap));
5bbd2cc1 1099 attr->encap_tunneltype = tnl_type;
128ea8ab 1100
7a3e76f1 1101 /* Add the export RTs for L2VNI */
d62a17ae 1102 for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
b53e67a3
DA
1103 bgp_attr_set_ecommunity(
1104 attr,
1105 ecommunity_merge(bgp_attr_get_ecommunity(attr), ecom));
128ea8ab 1106
1ec31309 1107 /* Add the export RTs for L3VNI if told to - caller determines
1108 * when this should be done.
523cafc4 1109 */
1ec31309 1110 if (add_l3_ecomm) {
7ec156a9
MK
1111 vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn);
1112 if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
1113 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode,
ca337b46 1114 l3rt))
b53e67a3
DA
1115 bgp_attr_set_ecommunity(
1116 attr,
1117 ecommunity_merge(
1118 bgp_attr_get_ecommunity(attr),
ca337b46 1119 l3rt->ecom));
7ec156a9 1120 }
f1f8b53c 1121 }
7a3e76f1 1122
1ec31309 1123 /* Add MAC mobility (sticky) if needed. */
d62a17ae 1124 if (attr->sticky) {
1125 seqnum = 0;
1126 memset(&ecom_sticky, 0, sizeof(ecom_sticky));
1127 encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
1128 ecom_sticky.size = 1;
34540b0d 1129 ecom_sticky.unit_size = ECOMMUNITY_SIZE;
d7c0a89a 1130 ecom_sticky.val = (uint8_t *)eval_sticky.val;
b53e67a3
DA
1131 bgp_attr_set_ecommunity(
1132 attr, ecommunity_merge(bgp_attr_get_ecommunity(attr),
1133 &ecom_sticky));
d62a17ae 1134 }
c85c03c7 1135
1ec31309 1136 /* Add RMAC, if told to. */
1137 if (add_l3_ecomm) {
bc59a672 1138 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
b53e67a3
DA
1139 ecommunity_add_val(bgp_attr_get_ecommunity(attr), &eval_rmac,
1140 true, true);
bc59a672
MK
1141 }
1142
1ec31309 1143 /* Add default gateway, if needed. */
ead40654
MK
1144 if (attr->default_gw) {
1145 memset(&ecom_default_gw, 0, sizeof(ecom_default_gw));
1146 encode_default_gw_extcomm(&eval_default_gw);
1147 ecom_default_gw.size = 1;
34540b0d 1148 ecom_default_gw.unit_size = ECOMMUNITY_SIZE;
ead40654 1149 ecom_default_gw.val = (uint8_t *)eval_default_gw.val;
b53e67a3
DA
1150 bgp_attr_set_ecommunity(
1151 attr, ecommunity_merge(bgp_attr_get_ecommunity(attr),
1152 &ecom_default_gw));
ead40654
MK
1153 }
1154
9c7edc03
AK
1155 proxy = !!(attr->es_flags & ATTR_ES_PROXY_ADVERT);
1156 if (attr->router_flag || proxy) {
68e33151 1157 memset(&ecom_na, 0, sizeof(ecom_na));
9c7edc03 1158 encode_na_flag_extcomm(&eval_na, attr->router_flag, proxy);
68e33151 1159 ecom_na.size = 1;
34540b0d 1160 ecom_na.unit_size = ECOMMUNITY_SIZE;
68e33151 1161 ecom_na.val = (uint8_t *)eval_na.val;
b53e67a3
DA
1162 bgp_attr_set_ecommunity(
1163 attr, ecommunity_merge(bgp_attr_get_ecommunity(attr),
1164 &ecom_na));
68e33151 1165 }
128ea8ab 1166}
1167
1168/*
1169 * Add MAC mobility extended community to attribute.
1170 */
d7c0a89a 1171static void add_mac_mobility_to_attr(uint32_t seq_num, struct attr *attr)
d62a17ae 1172{
1173 struct ecommunity ecom_tmp;
1174 struct ecommunity_val eval;
d7c0a89a 1175 uint8_t *ecom_val_ptr;
f6e07e1b 1176 uint32_t i;
d7c0a89a 1177 uint8_t *pnt;
d62a17ae 1178 int type = 0;
1179 int sub_type = 0;
b53e67a3 1180 struct ecommunity *ecomm = bgp_attr_get_ecommunity(attr);
d62a17ae 1181
1182 /* Build MM */
1183 encode_mac_mobility_extcomm(0, seq_num, &eval);
1184
1185 /* Find current MM ecommunity */
421bb26a 1186 ecom_val_ptr = NULL;
d62a17ae 1187
b53e67a3
DA
1188 if (ecomm) {
1189 for (i = 0; i < ecomm->size; i++) {
1190 pnt = ecomm->val + (i * ecomm->unit_size);
d62a17ae 1191 type = *pnt++;
1192 sub_type = *pnt++;
1193
1194 if (type == ECOMMUNITY_ENCODE_EVPN
1195 && sub_type
1196 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
c4efd0f4 1197 ecom_val_ptr =
b53e67a3 1198 (ecomm->val + (i * ecomm->unit_size));
d62a17ae 1199 break;
1200 }
1201 }
1202 }
1203
1204 /* Update the existing MM ecommunity */
421bb26a 1205 if (ecom_val_ptr) {
b53e67a3 1206 memcpy(ecom_val_ptr, eval.val, sizeof(char) * ecomm->unit_size);
d62a17ae 1207 }
1208 /* Add MM to existing */
1209 else {
1210 memset(&ecom_tmp, 0, sizeof(ecom_tmp));
1211 ecom_tmp.size = 1;
34540b0d 1212 ecom_tmp.unit_size = ECOMMUNITY_SIZE;
d7c0a89a 1213 ecom_tmp.val = (uint8_t *)eval.val;
d62a17ae 1214
b53e67a3
DA
1215 if (ecomm)
1216 bgp_attr_set_ecommunity(
1217 attr, ecommunity_merge(ecomm, &ecom_tmp));
f9a78910 1218 else
b53e67a3
DA
1219 bgp_attr_set_ecommunity(attr,
1220 ecommunity_dup(&ecom_tmp));
d62a17ae 1221 }
128ea8ab 1222}
1223
1224/* Install EVPN route into zebra. */
d62a17ae 1225static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
bd494ec5
DS
1226 const struct prefix_evpn *p,
1227 struct bgp_path_info *pi)
128ea8ab 1228{
d62a17ae 1229 int ret;
d594a14c 1230 uint8_t flags;
b16dd019 1231 int flood_control;
9c7edc03 1232 uint32_t seq;
128ea8ab 1233
d594a14c
AK
1234 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
1235 flags = 0;
9c7edc03
AK
1236
1237 if (pi->sub_type == BGP_ROUTE_IMPORTED) {
1238 if (pi->attr->sticky)
1239 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
1240 if (pi->attr->default_gw)
1241 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
1242 if (is_evpn_prefix_ipaddr_v6(p) &&
1243 pi->attr->router_flag)
1244 SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
1245
1246 seq = mac_mobility_seqnum(pi->attr);
1247 /* if local ES notify zebra that this is a sync path */
1248 if (bgp_evpn_attr_is_local_es(pi->attr)) {
1249 SET_FLAG(flags, ZEBRA_MACIP_TYPE_SYNC_PATH);
1250 if (bgp_evpn_attr_is_proxy(pi->attr))
1251 SET_FLAG(flags,
1252 ZEBRA_MACIP_TYPE_PROXY_ADVERT);
1253 }
1254 } else {
1255 if (!bgp_evpn_attr_is_sync(pi->attr))
1256 return 0;
1257
1258 /* if a local path is being turned around and sent
1259 * to zebra it is because it is a sync path on
1260 * a local ES
1261 */
1262 SET_FLAG(flags, ZEBRA_MACIP_TYPE_SYNC_PATH);
1263 /* supply the highest peer seq number to zebra
1264 * for MM seq syncing
1265 */
1266 seq = bgp_evpn_attr_get_sync_seq(pi->attr);
1267 /* if any of the paths from the peer have the ROUTER
1268 * flag set install the local entry as a router entry
1269 */
1270 if (is_evpn_prefix_ipaddr_v6(p) &&
1271 (pi->attr->es_flags &
1272 ATTR_ES_PEER_ROUTER))
1273 SET_FLAG(flags,
1274 ZEBRA_MACIP_TYPE_ROUTER_FLAG);
1275
1276 if (!(pi->attr->es_flags & ATTR_ES_PEER_ACTIVE))
1277 SET_FLAG(flags,
1278 ZEBRA_MACIP_TYPE_PROXY_ADVERT);
1279 }
1280
d594a14c 1281 ret = bgp_zebra_send_remote_macip(
852d9f97
SW
1282 bgp, vpn, p,
1283 (is_evpn_prefix_ipaddr_none(p)
1284 ? NULL /* MAC update */
1285 : evpn_type2_path_info_get_mac(
1286 pi) /* MAC-IP update */),
34c7f35f
SW
1287 pi->attr->nexthop, 1, flags, seq,
1288 bgp_evpn_attr_get_esi(pi->attr));
c44ab6f1
AK
1289 } else if (p->prefix.route_type == BGP_EVPN_AD_ROUTE) {
1290 ret = bgp_evpn_remote_es_evi_add(bgp, vpn, p);
d594a14c 1291 } else {
2a3f51cf 1292 switch (bgp_attr_get_pmsi_tnl_type(pi->attr)) {
b16dd019
AK
1293 case PMSI_TNLTYPE_INGR_REPL:
1294 flood_control = VXLAN_FLOOD_HEAD_END_REPL;
1295 break;
1296
1297 case PMSI_TNLTYPE_PIM_SM:
1298 flood_control = VXLAN_FLOOD_PIM_SM;
1299 break;
1300
1301 default:
1302 flood_control = VXLAN_FLOOD_DISABLED;
1303 break;
1304 }
1305 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, flood_control, 1);
d594a14c 1306 }
128ea8ab 1307
d62a17ae 1308 return ret;
128ea8ab 1309}
1310
1311/* Uninstall EVPN route from zebra. */
d62a17ae 1312static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
bd494ec5 1313 const struct prefix_evpn *p,
34c7f35f 1314 struct bgp_path_info *pi, bool is_sync)
128ea8ab 1315{
d62a17ae 1316 int ret;
128ea8ab 1317
d62a17ae 1318 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
34c7f35f 1319 ret = bgp_zebra_send_remote_macip(
852d9f97
SW
1320 bgp, vpn, p,
1321 (is_evpn_prefix_ipaddr_none(p)
1322 ? NULL /* MAC update */
1323 : evpn_type2_path_info_get_mac(
1324 pi) /* MAC-IP update */),
34c7f35f
SW
1325 (is_sync ? zero_vtep_ip : pi->attr->nexthop), 0, 0, 0,
1326 NULL);
c44ab6f1
AK
1327 else if (p->prefix.route_type == BGP_EVPN_AD_ROUTE)
1328 ret = bgp_evpn_remote_es_evi_del(bgp, vpn, p);
d62a17ae 1329 else
b16dd019
AK
1330 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p,
1331 VXLAN_FLOOD_DISABLED, 0);
128ea8ab 1332
d62a17ae 1333 return ret;
128ea8ab 1334}
1335
1336/*
1337 * Due to MAC mobility, the prior "local" best route has been supplanted
1338 * by a "remote" best route. The prior route has to be deleted and withdrawn
1339 * from peers.
1340 */
d62a17ae 1341static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
9bcb3eef 1342 struct bgp_dest *dest,
9c7edc03
AK
1343 struct bgp_path_info *old_local,
1344 struct bgp_path_info *new_select)
128ea8ab 1345{
9bcb3eef 1346 struct bgp_dest *global_dest;
40381db7 1347 struct bgp_path_info *pi;
d62a17ae 1348 afi_t afi = AFI_L2VPN;
1349 safi_t safi = SAFI_EVPN;
128ea8ab 1350
9c7edc03 1351 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
9c7edc03
AK
1352 char esi_buf[ESI_STR_LEN];
1353 char esi_buf2[ESI_STR_LEN];
752eed47
DS
1354 struct prefix_evpn *evp =
1355 (struct prefix_evpn *)bgp_dest_get_prefix(dest);
9c7edc03 1356
2dbe669b
DA
1357 zlog_debug("local path deleted %pFX es %s; new-path-es %s", evp,
1358 esi_to_str(&old_local->attr->esi, esi_buf,
1359 sizeof(esi_buf)),
1360 new_select ? esi_to_str(&new_select->attr->esi,
1361 esi_buf2, sizeof(esi_buf2))
1362 : "");
9c7edc03
AK
1363 }
1364
d62a17ae 1365 /* Locate route node in the global EVPN routing table. Note that
1366 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1367 * L3VPN routes.
1368 */
34c7f35f
SW
1369 global_dest = bgp_evpn_global_node_lookup(
1370 bgp->rib[afi][safi], afi, safi,
1371 (const struct prefix_evpn *)bgp_dest_get_prefix(dest),
1372 &vpn->prd, old_local);
9bcb3eef 1373 if (global_dest) {
d62a17ae 1374 /* Delete route entry in the global EVPN table. */
9bcb3eef 1375 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
128ea8ab 1376
d62a17ae 1377 /* Schedule for processing - withdraws to peers happen from
1378 * this table.
1379 */
40381db7 1380 if (pi)
9bcb3eef
DS
1381 bgp_process(bgp, global_dest, afi, safi);
1382 bgp_dest_unlock_node(global_dest);
d62a17ae 1383 }
128ea8ab 1384
d62a17ae 1385 /* Delete route entry in the VNI route table, caller to remove. */
9bcb3eef 1386 bgp_path_info_delete(dest, old_local);
128ea8ab 1387}
1388
1389/*
1390 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1391 * if appropriate.
c44ab6f1 1392 * Note: vpn is NULL for local EAD-ES routes.
128ea8ab 1393 */
c44ab6f1 1394int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn,
9bcb3eef 1395 struct bgp_dest *dest)
d62a17ae 1396{
4b7e6066
DS
1397 struct bgp_path_info *old_select, *new_select;
1398 struct bgp_path_info_pair old_and_new;
d62a17ae 1399 afi_t afi = AFI_L2VPN;
1400 safi_t safi = SAFI_EVPN;
1401 int ret = 0;
1402
1403 /* Compute the best path. */
9bcb3eef 1404 bgp_best_selection(bgp, dest, &bgp->maxpaths[afi][safi], &old_and_new,
d62a17ae 1405 afi, safi);
1406 old_select = old_and_new.old;
1407 new_select = old_and_new.new;
1408
1409 /* If the best path hasn't changed - see if there is still something to
9c7edc03
AK
1410 * update to zebra RIB.
1411 * Remote routes and SYNC route (i.e. local routes with
1412 * SYNCED_FROM_PEER flag) need to updated to zebra on any attr
1413 * change.
d62a17ae 1414 */
1415 if (old_select && old_select == new_select
1416 && old_select->type == ZEBRA_ROUTE_BGP
9c7edc03
AK
1417 && (old_select->sub_type == BGP_ROUTE_IMPORTED ||
1418 bgp_evpn_attr_is_sync(old_select->attr))
9bcb3eef 1419 && !CHECK_FLAG(dest->flags, BGP_NODE_USER_CLEAR)
1defdda8 1420 && !CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
dcc68b5e 1421 && !bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
9bcb3eef 1422 if (bgp_zebra_has_route_changed(old_select))
996c9314 1423 ret = evpn_zebra_install(
9bcb3eef
DS
1424 bgp, vpn,
1425 (const struct prefix_evpn *)bgp_dest_get_prefix(
1426 dest),
d594a14c 1427 old_select);
1defdda8 1428 UNSET_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG);
b1875e65 1429 UNSET_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG);
9bcb3eef 1430 bgp_zebra_clear_route_change_flags(dest);
d62a17ae 1431 return ret;
1432 }
1433
1434 /* If the user did a "clear" this flag will be set */
9bcb3eef 1435 UNSET_FLAG(dest->flags, BGP_NODE_USER_CLEAR);
d62a17ae 1436
1437 /* bestpath has changed; update relevant fields and install or uninstall
1438 * into the zebra RIB.
1439 */
1440 if (old_select || new_select)
9bcb3eef 1441 bgp_bump_version(dest);
d62a17ae 1442
1443 if (old_select)
9bcb3eef 1444 bgp_path_info_unset_flag(dest, old_select, BGP_PATH_SELECTED);
d62a17ae 1445 if (new_select) {
9bcb3eef
DS
1446 bgp_path_info_set_flag(dest, new_select, BGP_PATH_SELECTED);
1447 bgp_path_info_unset_flag(dest, new_select,
1448 BGP_PATH_ATTR_CHANGED);
1defdda8 1449 UNSET_FLAG(new_select->flags, BGP_PATH_MULTIPATH_CHG);
b1875e65 1450 UNSET_FLAG(new_select->flags, BGP_PATH_LINK_BW_CHG);
d62a17ae 1451 }
1452
9c7edc03
AK
1453 /* a local entry with the SYNC flag also results in a MAC-IP update
1454 * to zebra
1455 */
d62a17ae 1456 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
9c7edc03
AK
1457 && (new_select->sub_type == BGP_ROUTE_IMPORTED ||
1458 bgp_evpn_attr_is_sync(new_select->attr))) {
b54892e0 1459 ret = evpn_zebra_install(
9bcb3eef
DS
1460 bgp, vpn,
1461 (struct prefix_evpn *)bgp_dest_get_prefix(dest),
b54892e0 1462 new_select);
d594a14c 1463
d62a17ae 1464 /* If an old best existed and it was a "local" route, the only
1465 * reason
1466 * it would be supplanted is due to MAC mobility procedures. So,
1467 * we
1468 * need to do an implicit delete and withdraw that route from
1469 * peers.
1470 */
9c7edc03
AK
1471 if (new_select->sub_type == BGP_ROUTE_IMPORTED &&
1472 old_select && old_select->peer == bgp->peer_self
1473 && old_select->type == ZEBRA_ROUTE_BGP
1474 && old_select->sub_type == BGP_ROUTE_STATIC
1475 && vpn)
1476 evpn_delete_old_local_route(bgp, vpn, dest,
1477 old_select, new_select);
d62a17ae 1478 } else {
1479 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
90f4f482 1480 && old_select->sub_type == BGP_ROUTE_IMPORTED)
b54892e0
DS
1481 ret = evpn_zebra_uninstall(
1482 bgp, vpn,
9bcb3eef
DS
1483 (const struct prefix_evpn *)bgp_dest_get_prefix(
1484 dest),
34c7f35f 1485 old_select, false);
d62a17ae 1486 }
1487
1488 /* Clear any route change flags. */
9bcb3eef 1489 bgp_zebra_clear_route_change_flags(dest);
d62a17ae 1490
18ee8310 1491 /* Reap old select bgp_path_info, if it has been removed */
1defdda8 1492 if (old_select && CHECK_FLAG(old_select->flags, BGP_PATH_REMOVED))
9bcb3eef 1493 bgp_path_info_reap(dest, old_select);
d62a17ae 1494
1495 return ret;
128ea8ab 1496}
1497
9c7edc03
AK
1498static struct bgp_path_info *bgp_evpn_route_get_local_path(
1499 struct bgp *bgp, struct bgp_dest *dest)
c85c03c7 1500{
40381db7 1501 struct bgp_path_info *tmp_pi;
9c7edc03 1502 struct bgp_path_info *local_pi = NULL;
c85c03c7 1503
9bcb3eef 1504 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
9c7edc03
AK
1505 tmp_pi = tmp_pi->next) {
1506 if (bgp_evpn_is_path_local(bgp, tmp_pi)) {
40381db7 1507 local_pi = tmp_pi;
9c7edc03
AK
1508 break;
1509 }
d62a17ae 1510 }
c85c03c7 1511
9c7edc03 1512 return local_pi;
c85c03c7 1513}
1514
5e53dce3 1515static int update_evpn_type5_route_entry(struct bgp *bgp_evpn,
342dd0c6 1516 struct bgp *bgp_vrf, afi_t afi,
9bcb3eef 1517 safi_t safi, struct bgp_dest *dest,
5424b7ba 1518 struct attr *attr, int *route_changed)
342dd0c6 1519{
1520 struct attr *attr_new = NULL;
40381db7 1521 struct bgp_path_info *pi = NULL;
342dd0c6 1522 mpls_label_t label = MPLS_INVALID_LABEL;
40381db7
DS
1523 struct bgp_path_info *local_pi = NULL;
1524 struct bgp_path_info *tmp_pi = NULL;
342dd0c6 1525
5424b7ba 1526 *route_changed = 0;
342dd0c6 1527 /* locate the local route entry if any */
9bcb3eef 1528 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
6f94b685 1529 tmp_pi = tmp_pi->next) {
5e53dce3 1530 if (tmp_pi->peer == bgp_evpn->peer_self
40381db7
DS
1531 && tmp_pi->type == ZEBRA_ROUTE_BGP
1532 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
1533 local_pi = tmp_pi;
342dd0c6 1534 }
1535
2bb9eff4 1536 /*
0437e105 1537 * create a new route entry if one doesn't exist.
2bb9eff4 1538 * Otherwise see if route attr has changed
523cafc4 1539 */
40381db7 1540 if (!local_pi) {
342dd0c6 1541
5424b7ba
MK
1542 /* route has changed as this is the first entry */
1543 *route_changed = 1;
1544
342dd0c6 1545 /* Add (or update) attribute to hash. */
1546 attr_new = bgp_attr_intern(attr);
1547
1548 /* create the route info from attribute */
40381db7 1549 pi = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
9bcb3eef 1550 bgp_evpn->peer_self, attr_new, dest);
40381db7 1551 SET_FLAG(pi->flags, BGP_PATH_VALID);
342dd0c6 1552
b57ba6d2 1553 /* Type-5 routes advertise the L3-VNI */
40381db7 1554 bgp_path_info_extra_get(pi);
342dd0c6 1555 vni2label(bgp_vrf->l3vni, &label);
40381db7
DS
1556 memcpy(&pi->extra->label, &label, sizeof(label));
1557 pi->extra->num_labels = 1;
342dd0c6 1558
1559 /* add the route entry to route node*/
9bcb3eef 1560 bgp_path_info_add(dest, pi);
342dd0c6 1561 } else {
1562
40381db7
DS
1563 tmp_pi = local_pi;
1564 if (!attrhash_cmp(tmp_pi->attr, attr)) {
5424b7ba
MK
1565
1566 /* attribute changed */
1567 *route_changed = 1;
1568
342dd0c6 1569 /* The attribute has changed. */
1570 /* Add (or update) attribute to hash. */
1571 attr_new = bgp_attr_intern(attr);
9bcb3eef 1572 bgp_path_info_set_flag(dest, tmp_pi,
18ee8310 1573 BGP_PATH_ATTR_CHANGED);
342dd0c6 1574
1575 /* Restore route, if needed. */
40381db7 1576 if (CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
9bcb3eef 1577 bgp_path_info_restore(dest, tmp_pi);
342dd0c6 1578
1579 /* Unintern existing, set to new. */
40381db7
DS
1580 bgp_attr_unintern(&tmp_pi->attr);
1581 tmp_pi->attr = attr_new;
083ec940 1582 tmp_pi->uptime = monotime(NULL);
342dd0c6 1583 }
1584 }
1585 return 0;
1586}
1587
1588/* update evpn type-5 route entry */
996c9314 1589static int update_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp,
6c995628
AD
1590 struct attr *src_attr, afi_t src_afi,
1591 safi_t src_safi)
342dd0c6 1592{
1593 afi_t afi = AFI_L2VPN;
1594 safi_t safi = SAFI_EVPN;
1595 struct attr attr;
9bcb3eef 1596 struct bgp_dest *dest = NULL;
5e53dce3 1597 struct bgp *bgp_evpn = NULL;
5424b7ba 1598 int route_changed = 0;
342dd0c6 1599
5e53dce3
T
1600 bgp_evpn = bgp_get_evpn();
1601 if (!bgp_evpn)
faafdfa8 1602 return 0;
342dd0c6 1603
2f69f6d3 1604 /* Build path attribute for this route - use the source attr, if
1605 * present, else treat as locally originated.
1606 */
1607 if (src_attr)
6f4f49b2 1608 attr = *src_attr;
2f69f6d3 1609 else {
6006b807 1610 memset(&attr, 0, sizeof(attr));
0f05ea43 1611 bgp_attr_default_set(&attr, bgp_vrf, BGP_ORIGIN_IGP);
2f69f6d3 1612 }
5394a276 1613
5394a276
CS
1614 /* Advertise Primary IP (PIP) is enabled, send individual
1615 * IP (default instance router-id) as nexthop.
1616 * PIP is disabled or vrr interface is not present
27727001 1617 * use anycast-IP as nexthop and anycast RMAC.
5394a276
CS
1618 */
1619 if (!bgp_vrf->evpn_info->advertise_pip ||
1620 (!bgp_vrf->evpn_info->is_anycast_mac)) {
1621 attr.nexthop = bgp_vrf->originator_ip;
1622 attr.mp_nexthop_global_in = bgp_vrf->originator_ip;
27727001 1623 memcpy(&attr.rmac, &bgp_vrf->rmac, ETH_ALEN);
5394a276 1624 } else {
27727001
CS
1625 /* copy sys rmac */
1626 memcpy(&attr.rmac, &bgp_vrf->evpn_info->pip_rmac, ETH_ALEN);
5394a276
CS
1627 if (bgp_vrf->evpn_info->pip_ip.s_addr != INADDR_ANY) {
1628 attr.nexthop = bgp_vrf->evpn_info->pip_ip;
1629 attr.mp_nexthop_global_in = bgp_vrf->evpn_info->pip_ip;
1630 } else if (bgp_vrf->evpn_info->pip_ip.s_addr == INADDR_ANY)
2dbe669b
DA
1631 if (bgp_debug_zebra(NULL))
1632 zlog_debug(
1633 "VRF %s evp %pFX advertise-pip primary ip is not configured",
1634 vrf_id_to_name(bgp_vrf->vrf_id), evp);
5394a276
CS
1635 }
1636
c0d72166
DS
1637 if (bgp_debug_zebra(NULL))
1638 zlog_debug(
1639 "VRF %s type-5 route evp %pFX RMAC %pEA nexthop %pI4",
1640 vrf_id_to_name(bgp_vrf->vrf_id), evp, &attr.rmac,
1641 &attr.nexthop);
5394a276 1642
342dd0c6 1643 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
342dd0c6 1644
6c995628
AD
1645 if (src_afi == AFI_IP6 &&
1646 CHECK_FLAG(bgp_vrf->af_flags[AFI_L2VPN][SAFI_EVPN],
1647 BGP_L2VPN_EVPN_ADV_IPV6_UNICAST_GW_IP)) {
1648 if (src_attr &&
1649 !IN6_IS_ADDR_UNSPECIFIED(&src_attr->mp_nexthop_global)) {
1650 attr.evpn_overlay.type = OVERLAY_INDEX_GATEWAY_IP;
860e740b
IR
1651 SET_IPADDR_V6(&attr.evpn_overlay.gw_ip);
1652 memcpy(&attr.evpn_overlay.gw_ip.ipaddr_v6,
6c995628
AD
1653 &src_attr->mp_nexthop_global,
1654 sizeof(struct in6_addr));
1655 }
1656 } else if (src_afi == AFI_IP &&
1657 CHECK_FLAG(bgp_vrf->af_flags[AFI_L2VPN][SAFI_EVPN],
1658 BGP_L2VPN_EVPN_ADV_IPV4_UNICAST_GW_IP)) {
1659 if (src_attr && src_attr->nexthop.s_addr != 0) {
1660 attr.evpn_overlay.type = OVERLAY_INDEX_GATEWAY_IP;
860e740b
IR
1661 SET_IPADDR_V4(&attr.evpn_overlay.gw_ip);
1662 memcpy(&attr.evpn_overlay.gw_ip.ipaddr_v4,
6c995628
AD
1663 &src_attr->nexthop, sizeof(struct in_addr));
1664 }
1665 }
1666
342dd0c6 1667 /* Setup RT and encap extended community */
1668 build_evpn_type5_route_extcomm(bgp_vrf, &attr);
1669
1670 /* get the route node in global table */
34c7f35f
SW
1671 dest = bgp_evpn_global_node_get(bgp_evpn->rib[afi][safi], afi, safi,
1672 evp, &bgp_vrf->vrf_prd, NULL);
9bcb3eef 1673 assert(dest);
342dd0c6 1674
1675 /* create or update the route entry within the route node */
9bcb3eef 1676 update_evpn_type5_route_entry(bgp_evpn, bgp_vrf, afi, safi, dest, &attr,
996c9314 1677 &route_changed);
342dd0c6 1678
1679 /* schedule for processing and unlock node */
5424b7ba 1680 if (route_changed) {
9bcb3eef
DS
1681 bgp_process(bgp_evpn, dest, afi, safi);
1682 bgp_dest_unlock_node(dest);
5424b7ba 1683 }
342dd0c6 1684
1685 /* uninten temporary */
5ee65f6f 1686 if (!src_attr)
1687 aspath_unintern(&attr.aspath);
342dd0c6 1688 return 0;
1689}
1690
9c7edc03 1691static void bgp_evpn_get_sync_info(struct bgp *bgp, esi_t *esi,
09319b4e
DS
1692 struct bgp_dest *dest, uint32_t loc_seq,
1693 uint32_t *max_sync_seq, bool *active_on_peer,
36bac85c
AK
1694 bool *peer_router, bool *proxy_from_peer,
1695 const struct ethaddr *mac)
9c7edc03
AK
1696{
1697 struct bgp_path_info *tmp_pi;
1698 struct bgp_path_info *second_best_path = NULL;
1699 uint32_t tmp_mm_seq = 0;
1700 esi_t *tmp_esi;
1701 int paths_eq;
36bac85c
AK
1702 struct ethaddr *tmp_mac;
1703 bool mac_cmp = false;
1704 struct prefix_evpn *evp = (struct prefix_evpn *)&dest->p;
1705
1706
1707 /* mac comparison is not needed for MAC-only routes */
1708 if (mac && !is_evpn_prefix_ipaddr_none(evp))
1709 mac_cmp = true;
9c7edc03
AK
1710
1711 /* find the best non-local path. a local path can only be present
1712 * as best path
1713 */
09319b4e
DS
1714 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
1715 tmp_pi = tmp_pi->next) {
9c7edc03
AK
1716 if (tmp_pi->sub_type != BGP_ROUTE_IMPORTED ||
1717 !CHECK_FLAG(tmp_pi->flags, BGP_PATH_VALID))
1718 continue;
1719
36bac85c
AK
1720 /* ignore paths that have a different mac */
1721 if (mac_cmp) {
1722 tmp_mac = evpn_type2_path_info_get_mac(tmp_pi);
1723 if (memcmp(mac, tmp_mac, sizeof(*mac)))
1724 continue;
1725 }
1726
9c7edc03
AK
1727 if (bgp_evpn_path_info_cmp(bgp, tmp_pi,
1728 second_best_path, &paths_eq))
1729 second_best_path = tmp_pi;
1730 }
1731
1732 if (!second_best_path)
1733 return;
1734
1735 tmp_esi = bgp_evpn_attr_get_esi(second_best_path->attr);
1736 /* if this has the same ES desination as the local path
1737 * it is a sync path
1738 */
1739 if (!memcmp(esi, tmp_esi, sizeof(esi_t))) {
1740 tmp_mm_seq = mac_mobility_seqnum(second_best_path->attr);
1741 if (tmp_mm_seq < loc_seq)
1742 return;
1743
1744 /* we have a non-proxy path from the ES peer. */
1745 if (second_best_path->attr->es_flags &
1746 ATTR_ES_PROXY_ADVERT) {
1747 *proxy_from_peer = true;
1748 } else {
1749 *active_on_peer = true;
1750 }
1751
1752 if (second_best_path->attr->router_flag)
1753 *peer_router = true;
1754
1755 /* we use both proxy and non-proxy imports to
1756 * determine the max sync sequence
1757 */
1758 if (tmp_mm_seq > *max_sync_seq)
1759 *max_sync_seq = tmp_mm_seq;
1760 }
1761}
1762
1763/* Bubble up sync-info from all paths (non-best) to the local-path.
1764 * This is need for MM sequence number syncing and proxy advertisement.
1765 * Note: The local path can only exist as a best path in the
1766 * VPN route table. It will take precedence over all sync paths.
1767 */
1768static void update_evpn_route_entry_sync_info(struct bgp *bgp,
09319b4e
DS
1769 struct bgp_dest *dest,
1770 struct attr *attr,
36bac85c
AK
1771 uint32_t loc_seq, bool setup_sync,
1772 const struct ethaddr *mac)
9c7edc03
AK
1773{
1774 esi_t *esi;
752eed47
DS
1775 struct prefix_evpn *evp =
1776 (struct prefix_evpn *)bgp_dest_get_prefix(dest);
9c7edc03
AK
1777
1778 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1779 return;
1780
1781 esi = bgp_evpn_attr_get_esi(attr);
1782 if (bgp_evpn_is_esi_valid(esi)) {
1783 if (setup_sync) {
1784 uint32_t max_sync_seq = 0;
1785 bool active_on_peer = false;
1786 bool peer_router = false;
1787 bool proxy_from_peer = false;
1788
09319b4e
DS
1789 bgp_evpn_get_sync_info(bgp, esi, dest, loc_seq,
1790 &max_sync_seq, &active_on_peer,
36bac85c
AK
1791 &peer_router, &proxy_from_peer,
1792 mac);
9c7edc03
AK
1793 attr->mm_sync_seqnum = max_sync_seq;
1794 if (active_on_peer)
1795 attr->es_flags |= ATTR_ES_PEER_ACTIVE;
1796 else
1797 attr->es_flags &= ~ATTR_ES_PEER_ACTIVE;
1798 if (proxy_from_peer)
1799 attr->es_flags |= ATTR_ES_PEER_PROXY;
1800 else
1801 attr->es_flags &= ~ATTR_ES_PEER_PROXY;
1802 if (peer_router)
1803 attr->es_flags |= ATTR_ES_PEER_ROUTER;
1804 else
1805 attr->es_flags &= ~ATTR_ES_PEER_ROUTER;
1806
1807 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
9c7edc03
AK
1808 char esi_buf[ESI_STR_LEN];
1809
2dbe669b
DA
1810 zlog_debug(
1811 "setup sync info for %pFX es %s max_seq %d %s%s%s",
1812 evp,
9c7edc03 1813 esi_to_str(esi, esi_buf,
2dbe669b 1814 sizeof(esi_buf)),
9c7edc03 1815 max_sync_seq,
2dbe669b
DA
1816 (attr->es_flags & ATTR_ES_PEER_ACTIVE)
1817 ? "peer-active "
1818 : "",
1819 (attr->es_flags & ATTR_ES_PEER_PROXY)
1820 ? "peer-proxy "
1821 : "",
1822 (attr->es_flags & ATTR_ES_PEER_ROUTER)
1823 ? "peer-router "
1824 : "");
9c7edc03
AK
1825 }
1826 }
1827 } else {
1828 attr->mm_sync_seqnum = 0;
1829 attr->es_flags &= ~ATTR_ES_PEER_ACTIVE;
1830 attr->es_flags &= ~ATTR_ES_PEER_PROXY;
1831 }
1832}
1833
128ea8ab 1834/*
852d9f97 1835 * Create or update EVPN route entry. This could be in the VNI route tables
128ea8ab 1836 * or the global route table.
1837 */
d62a17ae 1838static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
26c03e43
AK
1839 afi_t afi, safi_t safi,
1840 struct bgp_dest *dest, struct attr *attr,
852d9f97
SW
1841 const struct ethaddr *mac,
1842 const struct ipaddr *ip, int add,
34c7f35f
SW
1843 struct bgp_path_info **pi, uint8_t flags,
1844 uint32_t seq, bool vpn_rt, bool *old_is_sync)
d62a17ae 1845{
40381db7
DS
1846 struct bgp_path_info *tmp_pi;
1847 struct bgp_path_info *local_pi;
d62a17ae 1848 struct attr *attr_new;
b57ba6d2 1849 mpls_label_t label[BGP_MAX_LABELS];
d7c0a89a 1850 uint32_t num_labels = 1;
d62a17ae 1851 int route_change = 1;
d7c0a89a 1852 uint8_t sticky = 0;
b54892e0 1853 const struct prefix_evpn *evp;
d62a17ae 1854
40381db7 1855 *pi = NULL;
9bcb3eef 1856 evp = (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
b57ba6d2 1857 memset(&label, 0, sizeof(label));
d62a17ae 1858
f07e1c99 1859 /* See if this is an update of an existing route, or a new add. */
9c7edc03 1860 local_pi = bgp_evpn_route_get_local_path(bgp, dest);
d62a17ae 1861
1862 /* If route doesn't exist already, create a new one, if told to.
1863 * Otherwise act based on whether the attributes of the route have
1864 * changed or not.
1865 */
40381db7 1866 if (!local_pi && !add)
d62a17ae 1867 return 0;
1868
9c7edc03
AK
1869 if (old_is_sync && local_pi)
1870 *old_is_sync = bgp_evpn_attr_is_sync(local_pi->attr);
1871
1872 /* if a local path is being added with a non-zero esi look
1873 * for SYNC paths from ES peers and bubble up the sync-info
1874 */
36bac85c 1875 update_evpn_route_entry_sync_info(bgp, dest, attr, seq, vpn_rt, mac);
9c7edc03 1876
f07e1c99 1877 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1878 if (seq && !CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW))
1879 add_mac_mobility_to_attr(seq, attr);
d62a17ae 1880
40381db7 1881 if (!local_pi) {
d62a17ae 1882 /* Add (or update) attribute to hash. */
1883 attr_new = bgp_attr_intern(attr);
1884
1885 /* Extract MAC mobility sequence number, if any. */
1886 attr_new->mm_seqnum =
1887 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
1888 attr_new->sticky = sticky;
1889
1890 /* Create new route with its attribute. */
40381db7 1891 tmp_pi = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
9bcb3eef 1892 bgp->peer_self, attr_new, dest);
40381db7
DS
1893 SET_FLAG(tmp_pi->flags, BGP_PATH_VALID);
1894 bgp_path_info_extra_get(tmp_pi);
d62a17ae 1895
1896 /* The VNI goes into the 'label' field of the route */
b57ba6d2 1897 vni2label(vpn->vni, &label[0]);
c48d9f5f
MK
1898
1899 /* Type-2 routes may carry a second VNI - the L3-VNI.
1900 * Only attach second label if we are advertising two labels for
1901 * type-2 routes.
1902 */
996c9314
LB
1903 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1904 && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
b57ba6d2
MK
1905 vni_t l3vni;
1906
1907 l3vni = bgpevpn_get_l3vni(vpn);
1908 if (l3vni) {
1909 vni2label(l3vni, &label[1]);
1910 num_labels++;
1911 }
1912 }
d62a17ae 1913
40381db7
DS
1914 memcpy(&tmp_pi->extra->label, label, sizeof(label));
1915 tmp_pi->extra->num_labels = num_labels;
34c7f35f 1916
852d9f97
SW
1917 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
1918 if (mac)
1919 evpn_type2_path_info_set_mac(tmp_pi, *mac);
1920 else if (ip)
1921 evpn_type2_path_info_set_ip(tmp_pi, *ip);
1922 }
34c7f35f 1923
0ca10580
CS
1924 /* Mark route as self type-2 route */
1925 if (flags && CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP))
1926 tmp_pi->extra->af_flags = BGP_EVPN_MACIP_TYPE_SVI_IP;
9bcb3eef 1927 bgp_path_info_add(dest, tmp_pi);
d62a17ae 1928 } else {
40381db7
DS
1929 tmp_pi = local_pi;
1930 if (attrhash_cmp(tmp_pi->attr, attr)
1931 && !CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
d62a17ae 1932 route_change = 0;
1933 else {
c48d9f5f
MK
1934 /*
1935 * The attributes have changed, type-2 routes needs to
1936 * be advertised with right labels.
1937 */
1938 vni2label(vpn->vni, &label[0]);
996c9314
LB
1939 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1940 && CHECK_FLAG(vpn->flags,
1941 VNI_FLAG_USE_TWO_LABELS)) {
c48d9f5f
MK
1942 vni_t l3vni;
1943
1944 l3vni = bgpevpn_get_l3vni(vpn);
1945 if (l3vni) {
1946 vni2label(l3vni, &label[1]);
1947 num_labels++;
1948 }
1949 }
40381db7
DS
1950 memcpy(&tmp_pi->extra->label, label, sizeof(label));
1951 tmp_pi->extra->num_labels = num_labels;
c48d9f5f 1952
852d9f97
SW
1953 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
1954 if (mac)
1955 evpn_type2_path_info_set_mac(tmp_pi,
1956 *mac);
1957 else if (ip)
1958 evpn_type2_path_info_set_ip(tmp_pi,
1959 *ip);
1960 }
34c7f35f 1961
d62a17ae 1962 /* The attribute has changed. */
1963 /* Add (or update) attribute to hash. */
1964 attr_new = bgp_attr_intern(attr);
9bcb3eef 1965 bgp_path_info_set_flag(dest, tmp_pi,
18ee8310 1966 BGP_PATH_ATTR_CHANGED);
d62a17ae 1967
f07e1c99 1968 /* Extract MAC mobility sequence number, if any. */
1969 attr_new->mm_seqnum =
1970 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
1971 attr_new->sticky = sticky;
1972
d62a17ae 1973 /* Restore route, if needed. */
40381db7 1974 if (CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
9bcb3eef 1975 bgp_path_info_restore(dest, tmp_pi);
d62a17ae 1976
1977 /* Unintern existing, set to new. */
40381db7
DS
1978 bgp_attr_unintern(&tmp_pi->attr);
1979 tmp_pi->attr = attr_new;
083ec940 1980 tmp_pi->uptime = monotime(NULL);
d62a17ae 1981 }
1982 }
1983
58bff4d1
AK
1984 /* local MAC-IP routes in the VNI table are linked to
1985 * the destination ES
26c03e43
AK
1986 */
1987 if (route_change && vpn_rt
1988 && (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE))
1989 bgp_evpn_path_es_link(tmp_pi, vpn->vni,
1990 bgp_evpn_attr_get_esi(tmp_pi->attr));
1991
d62a17ae 1992 /* Return back the route entry. */
40381db7 1993 *pi = tmp_pi;
d62a17ae 1994 return route_change;
128ea8ab 1995}
1996
ec0ab544 1997static void evpn_zebra_reinstall_best_route(struct bgp *bgp,
9bcb3eef
DS
1998 struct bgpevpn *vpn,
1999 struct bgp_dest *dest)
ec0ab544
AK
2000{
2001 struct bgp_path_info *tmp_ri;
2002 struct bgp_path_info *curr_select = NULL;
2003
9bcb3eef
DS
2004 for (tmp_ri = bgp_dest_get_bgp_path_info(dest); tmp_ri;
2005 tmp_ri = tmp_ri->next) {
ec0ab544
AK
2006 if (CHECK_FLAG(tmp_ri->flags, BGP_PATH_SELECTED)) {
2007 curr_select = tmp_ri;
2008 break;
2009 }
2010 }
2011
2012 if (curr_select && curr_select->type == ZEBRA_ROUTE_BGP
9c7edc03
AK
2013 && (curr_select->sub_type == BGP_ROUTE_IMPORTED ||
2014 bgp_evpn_attr_is_sync(curr_select->attr)))
2015 evpn_zebra_install(bgp, vpn,
2016 (const struct prefix_evpn *)bgp_dest_get_prefix(dest),
2017 curr_select);
ec0ab544
AK
2018}
2019
6d8c603a
AK
2020/*
2021 * If the local route was not selected evict it and tell zebra to re-add
2022 * the best remote dest.
2023 *
2024 * Typically a local path added by zebra is expected to be selected as
2025 * best. In which case when a remote path wins as best (later)
2026 * evpn_route_select_install itself evicts the older-local-best path.
2027 *
2028 * However if bgp's add and zebra's add cross paths (race condition) it
2029 * is possible that the local path is no longer the "older" best path.
2030 * It is a path that was never designated as best and hence requires
2031 * additional handling to prevent bgp from injecting and holding on to a
2032 * non-best local path.
2033 */
2034static void evpn_cleanup_local_non_best_route(struct bgp *bgp,
2035 struct bgpevpn *vpn,
9bcb3eef 2036 struct bgp_dest *dest,
9a8897aa 2037 struct bgp_path_info *local_pi)
6d8c603a 2038{
6d8c603a 2039 /* local path was not picked as the winner; kick it out */
b54892e0 2040 if (bgp_debug_zebra(NULL))
56ca3b5b 2041 zlog_debug("evicting local evpn prefix %pBD as remote won",
9bcb3eef 2042 dest);
b54892e0 2043
9c7edc03 2044 evpn_delete_old_local_route(bgp, vpn, dest, local_pi, NULL);
9bcb3eef 2045 bgp_path_info_reap(dest, local_pi);
6d8c603a
AK
2046
2047 /* tell zebra to re-add the best remote path */
9bcb3eef 2048 evpn_zebra_reinstall_best_route(bgp, vpn, dest);
6d8c603a
AK
2049}
2050
70524092
AK
2051static inline bool bgp_evpn_route_add_l3_ecomm_ok(struct bgpevpn *vpn,
2052 const struct prefix_evpn *p,
2053 esi_t *esi)
2054{
2055 return p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2056 && (is_evpn_prefix_ipaddr_v4(p)
a2b19693
TA
2057 || (is_evpn_prefix_ipaddr_v6(p)
2058 && !IN6_IS_ADDR_LINKLOCAL(
2059 &p->prefix.macip_addr.ip.ipaddr_v6)))
70524092
AK
2060 && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)
2061 && bgpevpn_get_l3vni(vpn) && bgp_evpn_es_add_l3_ecomm_ok(esi);
2062}
2063
128ea8ab 2064/*
2065 * Create or update EVPN route (of type based on prefix) for specified VNI
2066 * and schedule for processing.
2067 */
d62a17ae 2068static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
f07e1c99 2069 struct prefix_evpn *p, uint8_t flags,
c44ab6f1 2070 uint32_t seq, esi_t *esi)
128ea8ab 2071{
9bcb3eef 2072 struct bgp_dest *dest;
d62a17ae 2073 struct attr attr;
2074 struct attr *attr_new;
1ec31309 2075 int add_l3_ecomm = 0;
40381db7 2076 struct bgp_path_info *pi;
d62a17ae 2077 afi_t afi = AFI_L2VPN;
2078 safi_t safi = SAFI_EVPN;
2079 int route_change;
9c7edc03 2080 bool old_is_sync = false;
852d9f97 2081 bool mac_only = false;
128ea8ab 2082
6006b807 2083 memset(&attr, 0, sizeof(attr));
128ea8ab 2084
d62a17ae 2085 /* Build path-attribute for this route. */
0f05ea43 2086 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
d62a17ae 2087 attr.nexthop = vpn->originator_ip;
2088 attr.mp_nexthop_global_in = vpn->originator_ip;
2089 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
317f1fe0 2090 attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0;
ead40654 2091 attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0;
68e33151
CS
2092 attr.router_flag = CHECK_FLAG(flags,
2093 ZEBRA_MACIP_TYPE_ROUTER_FLAG) ? 1 : 0;
9c7edc03
AK
2094 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
2095 attr.es_flags |= ATTR_ES_PROXY_ADVERT;
2096
2097 if (esi && bgp_evpn_is_esi_valid(esi)) {
c44ab6f1 2098 memcpy(&attr.esi, esi, sizeof(esi_t));
9c7edc03
AK
2099 attr.es_flags |= ATTR_ES_IS_LOCAL;
2100 }
2101
be41eb68 2102 /* PMSI is only needed for type-3 routes */
d03239d0 2103 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
be41eb68 2104 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL);
2a3f51cf 2105 bgp_attr_set_pmsi_tnl_type(&attr, PMSI_TNLTYPE_INGR_REPL);
d03239d0 2106 }
be41eb68 2107
2304139a 2108 /* router mac is only needed for type-2 routes here. */
2109 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
2110 uint8_t af_flags = 0;
2111
2112 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP))
2113 SET_FLAG(af_flags, BGP_EVPN_MACIP_TYPE_SVI_IP);
2114
2115 bgp_evpn_get_rmac_nexthop(vpn, p, &attr, af_flags);
2116 }
2117
9c7edc03 2118 if (bgp_debug_zebra(NULL)) {
9c7edc03
AK
2119 char buf3[ESI_STR_LEN];
2120
23d0a753 2121 zlog_debug(
47f5eb74 2122 "VRF %s vni %u type-%u route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
23d0a753 2123 vpn->bgp_vrf ? vrf_id_to_name(vpn->bgp_vrf->vrf_id)
47f5eb74 2124 : "None",
2125 vpn->vni, p->prefix.route_type, p, &attr.rmac,
2126 &attr.mp_nexthop_global_in,
23d0a753 2127 esi_to_str(esi, buf3, sizeof(buf3)));
9c7edc03 2128 }
0ca10580 2129
a21bd7a3 2130 vni2label(vpn->vni, &(attr.label));
128ea8ab 2131
1ec31309 2132 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
2133 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
2134 * these routes.
2135 */
70524092
AK
2136 add_l3_ecomm = bgp_evpn_route_add_l3_ecomm_ok(
2137 vpn, p, (attr.es_flags & ATTR_ES_IS_LOCAL) ? &attr.esi : NULL);
1ec31309 2138
2139 /* Set up extended community. */
2140 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
128ea8ab 2141
852d9f97
SW
2142 /* First, create (or fetch) route node within the VNI.
2143 * NOTE: There is no RD here.
2144 */
2145 dest = bgp_evpn_vni_node_get(vpn, p, NULL);
2146
2147 if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
2148 (is_evpn_prefix_ipaddr_none(p) == true))
2149 mac_only = true;
128ea8ab 2150
d62a17ae 2151 /* Create or update route entry. */
34c7f35f 2152 route_change = update_evpn_route_entry(
852d9f97
SW
2153 bgp, vpn, afi, safi, dest, &attr,
2154 (mac_only ? NULL : &p->prefix.macip_addr.mac), NULL /* ip */, 1,
34c7f35f 2155 &pi, flags, seq, true /* setup_sync */, &old_is_sync);
40381db7
DS
2156 assert(pi);
2157 attr_new = pi->attr;
128ea8ab 2158
6d8c603a
AK
2159 /* lock ri to prevent freeing in evpn_route_select_install */
2160 bgp_path_info_lock(pi);
7ab604ab
CS
2161
2162 /* Perform route selection. Normally, the local route in the
2163 * VNI is expected to win and be the best route. However, if
2164 * there is a race condition where a host moved from local to
2165 * remote and the remote route was received in BGP just prior
2166 * to the local MACIP notification from zebra, the remote
2167 * route would win, and we should evict the defunct local route
2168 * and (re)install the remote route into zebra.
2169 */
9bcb3eef 2170 evpn_route_select_install(bgp, vpn, dest);
6d8c603a 2171 /*
9a8897aa
AK
2172 * If the new local route was not selected evict it and tell zebra
2173 * to re-add the best remote dest. BGP doesn't retain non-best local
2174 * routes.
6d8c603a 2175 */
9c7edc03 2176 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
9a8897aa 2177 route_change = 0;
9c7edc03
AK
2178 } else {
2179 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
2180 route_change = 0;
2181 evpn_cleanup_local_non_best_route(bgp, vpn, dest, pi);
2182 } else {
2183 bool new_is_sync;
2184
2185 /* If the local path already existed and is still the
2186 * best path we need to also check if it transitioned
2187 * from being a sync path to a non-sync path. If it
2188 * it did we need to notify zebra that the sync-path
2189 * has been removed.
2190 */
2191 new_is_sync = bgp_evpn_attr_is_sync(pi->attr);
2192 if (!new_is_sync && old_is_sync)
34c7f35f 2193 evpn_zebra_uninstall(bgp, vpn, p, pi, true);
9c7edc03 2194 }
9a8897aa 2195 }
6d8c603a
AK
2196 bgp_path_info_unlock(pi);
2197
9bcb3eef 2198 bgp_dest_unlock_node(dest);
128ea8ab 2199
d62a17ae 2200 /* If this is a new route or some attribute has changed, export the
2201 * route to the global table. The route will be advertised to peers
2202 * from there. Note that this table is a 2-level tree (RD-level +
2203 * Prefix-level) similar to L3VPN routes.
2204 */
2205 if (route_change) {
40381db7 2206 struct bgp_path_info *global_pi;
128ea8ab 2207
34c7f35f
SW
2208 dest = bgp_evpn_global_node_get(bgp->rib[afi][safi], afi, safi,
2209 p, &vpn->prd, NULL);
852d9f97
SW
2210 update_evpn_route_entry(
2211 bgp, vpn, afi, safi, dest, attr_new, NULL /* mac */,
2212 NULL /* ip */, 1, &global_pi, flags, seq,
2213 false /* setup_sync */, NULL /* old_is_sync */);
128ea8ab 2214
d62a17ae 2215 /* Schedule for processing and unlock node. */
9bcb3eef
DS
2216 bgp_process(bgp, dest, afi, safi);
2217 bgp_dest_unlock_node(dest);
d62a17ae 2218 }
128ea8ab 2219
d62a17ae 2220 /* Unintern temporary. */
2221 aspath_unintern(&attr.aspath);
128ea8ab 2222
d62a17ae 2223 return 0;
128ea8ab 2224}
2225
50f74cf1 2226/*
2227 * Delete EVPN route entry.
2228 * The entry can be in ESI/VNI table or the global table.
2229 */
185fb14a 2230void delete_evpn_route_entry(struct bgp *bgp, afi_t afi, safi_t safi,
9bcb3eef 2231 struct bgp_dest *dest,
40381db7 2232 struct bgp_path_info **pi)
342dd0c6 2233{
40381db7 2234 struct bgp_path_info *tmp_pi;
342dd0c6 2235
40381db7 2236 *pi = NULL;
342dd0c6 2237
50f74cf1 2238 /* Now, find matching route. */
9bcb3eef 2239 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
6f94b685 2240 tmp_pi = tmp_pi->next)
40381db7
DS
2241 if (tmp_pi->peer == bgp->peer_self
2242 && tmp_pi->type == ZEBRA_ROUTE_BGP
2243 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
342dd0c6 2244 break;
2245
40381db7 2246 *pi = tmp_pi;
342dd0c6 2247
2248 /* Mark route for delete. */
40381db7 2249 if (tmp_pi)
9bcb3eef 2250 bgp_path_info_delete(dest, tmp_pi);
342dd0c6 2251}
2252
2253/* Delete EVPN type5 route */
996c9314 2254static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp)
342dd0c6 2255{
2256 afi_t afi = AFI_L2VPN;
2257 safi_t safi = SAFI_EVPN;
9bcb3eef 2258 struct bgp_dest *dest = NULL;
40381db7 2259 struct bgp_path_info *pi = NULL;
5e53dce3 2260 struct bgp *bgp_evpn = NULL; /* evpn bgp instance */
342dd0c6 2261
5e53dce3
T
2262 bgp_evpn = bgp_get_evpn();
2263 if (!bgp_evpn)
faafdfa8 2264 return 0;
342dd0c6 2265
2266 /* locate the global route entry for this type-5 prefix */
34c7f35f
SW
2267 dest = bgp_evpn_global_node_lookup(bgp_evpn->rib[afi][safi], afi, safi,
2268 evp, &bgp_vrf->vrf_prd, NULL);
9bcb3eef 2269 if (!dest)
342dd0c6 2270 return 0;
2271
9bcb3eef 2272 delete_evpn_route_entry(bgp_evpn, afi, safi, dest, &pi);
40381db7 2273 if (pi)
9bcb3eef
DS
2274 bgp_process(bgp_evpn, dest, afi, safi);
2275 bgp_dest_unlock_node(dest);
342dd0c6 2276 return 0;
2277}
2278
128ea8ab 2279/*
2280 * Delete EVPN route (of type based on prefix) for specified VNI and
2281 * schedule for processing.
2282 */
d62a17ae 2283static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
2284 struct prefix_evpn *p)
2285{
9bcb3eef 2286 struct bgp_dest *dest, *global_dest;
40381db7 2287 struct bgp_path_info *pi;
d62a17ae 2288 afi_t afi = AFI_L2VPN;
2289 safi_t safi = SAFI_EVPN;
2290
2291 /* First, locate the route node within the VNI. If it doesn't exist,
2292 * there
2293 * is nothing further to do.
852d9f97 2294 * NOTE: There is no RD here.
d62a17ae 2295 */
852d9f97 2296 dest = bgp_evpn_vni_node_lookup(vpn, p, NULL);
9bcb3eef 2297 if (!dest)
d62a17ae 2298 return 0;
2299
2300 /* Next, locate route node in the global EVPN routing table. Note that
2301 * this table is a 2-level tree (RD-level + Prefix-level) similar to
2302 * L3VPN routes.
2303 */
34c7f35f
SW
2304 global_dest = bgp_evpn_global_node_lookup(bgp->rib[afi][safi], afi,
2305 safi, p, &vpn->prd, NULL);
9bcb3eef 2306 if (global_dest) {
d62a17ae 2307 /* Delete route entry in the global EVPN table. */
9bcb3eef 2308 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
d62a17ae 2309
2310 /* Schedule for processing - withdraws to peers happen from
2311 * this table.
2312 */
40381db7 2313 if (pi)
9bcb3eef
DS
2314 bgp_process(bgp, global_dest, afi, safi);
2315 bgp_dest_unlock_node(global_dest);
d62a17ae 2316 }
2317
2318 /* Delete route entry in the VNI route table. This can just be removed.
2319 */
9bcb3eef 2320 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
3e3aa88e 2321 if (pi) {
9bcb3eef
DS
2322 bgp_path_info_reap(dest, pi);
2323 evpn_route_select_install(bgp, vpn, dest);
3e3aa88e 2324 }
9bcb3eef 2325 bgp_dest_unlock_node(dest);
d62a17ae 2326
2327 return 0;
128ea8ab 2328}
2329
70524092
AK
2330void bgp_evpn_update_type2_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2331 struct bgp_dest *dest,
2332 struct bgp_path_info *local_pi,
2333 const char *caller)
9c7edc03
AK
2334{
2335 afi_t afi = AFI_L2VPN;
2336 safi_t safi = SAFI_EVPN;
2337 struct bgp_path_info *pi;
2338 struct attr attr;
2339 struct attr *attr_new;
2340 uint32_t seq;
2341 int add_l3_ecomm = 0;
09319b4e 2342 struct bgp_dest *global_dest;
9c7edc03 2343 struct bgp_path_info *global_pi;
34c7f35f 2344 struct prefix_evpn evp;
9c7edc03
AK
2345 int route_change;
2346 bool old_is_sync = false;
2347
2348 if (CHECK_FLAG(local_pi->flags, BGP_PATH_REMOVED))
2349 return;
2350
34c7f35f
SW
2351 /*
2352 * VNI table MAC-IP prefixes don't have MAC so make sure it's set from
2353 * path info here.
2354 */
852d9f97
SW
2355 if (is_evpn_prefix_ipaddr_none((struct prefix_evpn *)&dest->p)) {
2356 /* VNI MAC -> Global */
2357 evpn_type2_prefix_global_copy(
2358 &evp, (struct prefix_evpn *)&dest->p, NULL /* mac */,
2359 evpn_type2_path_info_get_ip(local_pi));
2360 } else {
2361 /* VNI IP -> Global */
2362 evpn_type2_prefix_global_copy(
2363 &evp, (struct prefix_evpn *)&dest->p,
2364 evpn_type2_path_info_get_mac(local_pi), NULL /* ip */);
2365 }
34c7f35f 2366
9c7edc03
AK
2367 /*
2368 * Build attribute per local route as the MAC mobility and
2369 * some other values could differ for different routes. The
2370 * attributes will be shared in the hash table.
2371 */
0f05ea43 2372 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
9c7edc03
AK
2373 attr.nexthop = vpn->originator_ip;
2374 attr.mp_nexthop_global_in = vpn->originator_ip;
2375 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
2376 attr.sticky = (local_pi->attr->sticky) ? 1 : 0;
2377 attr.router_flag = (local_pi->attr->router_flag) ? 1 : 0;
2378 attr.es_flags = local_pi->attr->es_flags;
2379 if (local_pi->attr->default_gw) {
2380 attr.default_gw = 1;
34c7f35f 2381 if (is_evpn_prefix_ipaddr_v6(&evp))
9c7edc03
AK
2382 attr.router_flag = 1;
2383 }
2384 memcpy(&attr.esi, &local_pi->attr->esi, sizeof(esi_t));
34c7f35f 2385 bgp_evpn_get_rmac_nexthop(vpn, &evp, &attr, local_pi->extra->af_flags);
9c7edc03
AK
2386 vni2label(vpn->vni, &(attr.label));
2387 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2388 * using L3 VNI for type-2 routes also.
2389 */
70524092 2390 add_l3_ecomm = bgp_evpn_route_add_l3_ecomm_ok(
34c7f35f 2391 vpn, &evp,
70524092 2392 (attr.es_flags & ATTR_ES_IS_LOCAL) ? &attr.esi : NULL);
9c7edc03
AK
2393
2394 /* Set up extended community. */
2395 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
2396 seq = mac_mobility_seqnum(local_pi->attr);
2397
2398 if (bgp_debug_zebra(NULL)) {
9c7edc03
AK
2399 char buf3[ESI_STR_LEN];
2400
23d0a753 2401 zlog_debug(
c0d72166 2402 "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
23d0a753
DA
2403 vpn->bgp_vrf ? vrf_id_to_name(vpn->bgp_vrf->vrf_id)
2404 : " ",
34c7f35f 2405 vpn->vni, &evp, &attr.rmac, &attr.mp_nexthop_global_in,
23d0a753
DA
2406 esi_to_str(&attr.esi, buf3, sizeof(buf3)),
2407 attr.es_flags, caller);
9c7edc03
AK
2408 }
2409
2410 /* Update the route entry. */
09319b4e 2411 route_change = update_evpn_route_entry(
852d9f97
SW
2412 bgp, vpn, afi, safi, dest, &attr, NULL /* mac */, NULL /* ip */,
2413 0, &pi, 0, seq, true /* setup_sync */, &old_is_sync);
9c7edc03
AK
2414
2415 assert(pi);
2416 attr_new = pi->attr;
2417 /* lock ri to prevent freeing in evpn_route_select_install */
2418 bgp_path_info_lock(pi);
2419
2420 /* Perform route selection. Normally, the local route in the
2421 * VNI is expected to win and be the best route. However,
2422 * under peculiar situations (e.g., tunnel (next hop) IP change
2423 * that causes best selection to be based on next hop), a
2424 * remote route could win. If the local route is the best,
2425 * ensure it is updated in the global EVPN route table and
2426 * advertised to peers; otherwise, ensure it is evicted and
2427 * (re)install the remote route into zebra.
2428 */
09319b4e 2429 evpn_route_select_install(bgp, vpn, dest);
9c7edc03
AK
2430
2431 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2432 route_change = 0;
2433 } else {
2434 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
2435 route_change = 0;
09319b4e 2436 evpn_cleanup_local_non_best_route(bgp, vpn, dest, pi);
9c7edc03
AK
2437 } else {
2438 bool new_is_sync;
2439
2440 /* If the local path already existed and is still the
2441 * best path we need to also check if it transitioned
2442 * from being a sync path to a non-sync path. If it
2443 * it did we need to notify zebra that the sync-path
2444 * has been removed.
2445 */
2446 new_is_sync = bgp_evpn_attr_is_sync(pi->attr);
2447 if (!new_is_sync && old_is_sync)
34c7f35f 2448 evpn_zebra_uninstall(bgp, vpn, &evp, pi, true);
9c7edc03
AK
2449 }
2450 }
2451
2452
2453 /* unlock pi */
2454 bgp_path_info_unlock(pi);
2455
2456 if (route_change) {
2457 /* Update route in global routing table. */
34c7f35f
SW
2458 global_dest = bgp_evpn_global_node_get(
2459 bgp->rib[afi][safi], afi, safi, &evp, &vpn->prd, NULL);
09319b4e
DS
2460 assert(global_dest);
2461 update_evpn_route_entry(
852d9f97
SW
2462 bgp, vpn, afi, safi, global_dest, attr_new,
2463 NULL /* mac */, NULL /* ip */, 0, &global_pi, 0,
2464 mac_mobility_seqnum(attr_new), false /* setup_sync */,
2465 NULL /* old_is_sync */);
9c7edc03
AK
2466
2467 /* Schedule for processing and unlock node. */
09319b4e
DS
2468 bgp_process(bgp, global_dest, afi, safi);
2469 bgp_dest_unlock_node(global_dest);
9c7edc03
AK
2470 }
2471
2472 /* Unintern temporary. */
2473 aspath_unintern(&attr.aspath);
2474}
2475
852d9f97
SW
2476static void update_type2_route(struct bgp *bgp, struct bgpevpn *vpn,
2477 struct bgp_dest *dest)
2478{
2479 struct bgp_path_info *tmp_pi;
2480
2481 const struct prefix_evpn *evp =
2482 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
2483
2484 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2485 return;
2486
2487 /* Identify local route. */
2488 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
2489 tmp_pi = tmp_pi->next) {
2490 if (tmp_pi->peer == bgp->peer_self &&
2491 tmp_pi->type == ZEBRA_ROUTE_BGP &&
2492 tmp_pi->sub_type == BGP_ROUTE_STATIC)
2493 break;
2494 }
2495
2496 if (!tmp_pi)
2497 return;
2498
2499 bgp_evpn_update_type2_route_entry(bgp, vpn, dest, tmp_pi, __func__);
2500}
2501
128ea8ab 2502/*
2503 * Update all type-2 (MACIP) local routes for this VNI - these should also
2504 * be scheduled for advertise to peers.
2505 */
852d9f97 2506static void update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
d62a17ae 2507{
9bcb3eef 2508 struct bgp_dest *dest;
d62a17ae 2509
852d9f97
SW
2510 /* Walk this VNI's route MAC & IP table and update local type-2
2511 * routes. For any routes updated, update corresponding entry in the
2512 * global table too.
d62a17ae 2513 */
852d9f97
SW
2514 for (dest = bgp_table_top(vpn->mac_table); dest;
2515 dest = bgp_route_next(dest))
2516 update_type2_route(bgp, vpn, dest);
d62a17ae 2517
852d9f97
SW
2518 for (dest = bgp_table_top(vpn->ip_table); dest;
2519 dest = bgp_route_next(dest))
2520 update_type2_route(bgp, vpn, dest);
128ea8ab 2521}
2522
2523/*
2524 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2525 * global routing table. These are also scheduled for withdraw from peers.
2526 */
17151ae9 2527static void delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2528{
d62a17ae 2529 afi_t afi;
2530 safi_t safi;
9bcb3eef 2531 struct bgp_dest *rddest, *dest;
d62a17ae 2532 struct bgp_table *table;
40381db7 2533 struct bgp_path_info *pi;
128ea8ab 2534
d62a17ae 2535 afi = AFI_L2VPN;
2536 safi = SAFI_EVPN;
128ea8ab 2537
9bcb3eef
DS
2538 rddest = bgp_node_lookup(bgp->rib[afi][safi],
2539 (struct prefix *)&vpn->prd);
17151ae9 2540 if (rddest) {
9bcb3eef
DS
2541 table = bgp_dest_get_bgp_table_info(rddest);
2542 for (dest = bgp_table_top(table); dest;
2543 dest = bgp_route_next(dest)) {
2544 const struct prefix_evpn *evp =
2545 (const struct prefix_evpn *)bgp_dest_get_prefix(
2546 dest);
128ea8ab 2547
d62a17ae 2548 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2549 continue;
128ea8ab 2550
9bcb3eef 2551 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
40381db7 2552 if (pi)
9bcb3eef 2553 bgp_process(bgp, dest, afi, safi);
d62a17ae 2554 }
128ea8ab 2555
17151ae9 2556 /* Unlock RD node. */
9bcb3eef 2557 bgp_dest_unlock_node(rddest);
17151ae9 2558 }
128ea8ab 2559}
2560
852d9f97
SW
2561static void delete_vni_type2_route(struct bgp *bgp, struct bgp_dest *dest)
2562{
2563 struct bgp_path_info *pi;
2564 afi_t afi = AFI_L2VPN;
2565 safi_t safi = SAFI_EVPN;
2566
2567 const struct prefix_evpn *evp =
2568 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
2569
2570 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2571 return;
2572
2573 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
2574
2575 /* Route entry in local table gets deleted immediately. */
2576 if (pi)
2577 bgp_path_info_reap(dest, pi);
2578}
2579
2580static void delete_vni_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2581{
2582 struct bgp_dest *dest;
2583
2584 /* Next, walk this VNI's MAC & IP route table and delete local type-2
2585 * routes.
2586 */
2587 for (dest = bgp_table_top(vpn->mac_table); dest;
2588 dest = bgp_route_next(dest))
2589 delete_vni_type2_route(bgp, dest);
2590
2591 for (dest = bgp_table_top(vpn->ip_table); dest;
2592 dest = bgp_route_next(dest))
2593 delete_vni_type2_route(bgp, dest);
2594}
2595
128ea8ab 2596/*
2597 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2598 * table as well as the per-VNI route table.
2599 */
852d9f97 2600static void delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2601{
d62a17ae 2602 /* First, walk the global route table for this VNI's type-2 local
2603 * routes.
2604 * EVPN routes are a 2-level table, first get the RD table.
2605 */
2606 delete_global_type2_routes(bgp, vpn);
852d9f97 2607 delete_vni_type2_routes(bgp, vpn);
128ea8ab 2608}
2609
2610/*
2611 * Delete all routes in the per-VNI route table.
2612 */
852d9f97 2613static void delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2614{
9bcb3eef 2615 struct bgp_dest *dest;
40381db7 2616 struct bgp_path_info *pi, *nextpi;
128ea8ab 2617
852d9f97
SW
2618 /* Walk this VNI's MAC & IP route table and delete all routes. */
2619 for (dest = bgp_table_top(vpn->mac_table); dest;
9bcb3eef
DS
2620 dest = bgp_route_next(dest)) {
2621 for (pi = bgp_dest_get_bgp_path_info(dest);
6f94b685 2622 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
021b6596 2623 bgp_evpn_remote_ip_hash_del(vpn, pi);
9bcb3eef
DS
2624 bgp_path_info_delete(dest, pi);
2625 bgp_path_info_reap(dest, pi);
d62a17ae 2626 }
2627 }
128ea8ab 2628
852d9f97
SW
2629 for (dest = bgp_table_top(vpn->ip_table); dest;
2630 dest = bgp_route_next(dest)) {
2631 for (pi = bgp_dest_get_bgp_path_info(dest);
2632 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2633 bgp_path_info_delete(dest, pi);
2634 bgp_path_info_reap(dest, pi);
2635 }
2636 }
128ea8ab 2637}
2638
833b8a50
AK
2639/* BUM traffic flood mode per-l2-vni */
2640static int bgp_evpn_vni_flood_mode_get(struct bgp *bgp,
2641 struct bgpevpn *vpn)
2642{
2643 /* if flooding has been globally disabled per-vni mode is
2644 * not relevant
2645 */
2646 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
2647 return VXLAN_FLOOD_DISABLED;
2648
2649 /* if mcast group ip has been specified we use a PIM-SM MDT */
2650 if (vpn->mcast_grp.s_addr != INADDR_ANY)
2651 return VXLAN_FLOOD_PIM_SM;
2652
2653 /* default is ingress replication */
2654 return VXLAN_FLOOD_HEAD_END_REPL;
2655}
2656
128ea8ab 2657/*
2658 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2659 * export RT getting modified or change to tunnel IP. Note that these
2660 * situations need the route in the per-VNI table as well as the global
2661 * table to be updated (as attributes change).
2662 */
0ca10580 2663int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2664{
d62a17ae 2665 int ret;
2666 struct prefix_evpn p;
128ea8ab 2667
9c49ac74
AD
2668 update_type1_routes_for_evi(bgp, vpn);
2669
d62a17ae 2670 /* Update and advertise the type-3 route (only one) followed by the
2671 * locally learnt type-2 routes (MACIP) - for this VNI.
fd069644
DS
2672 *
2673 * RT-3 only if doing head-end replication
d62a17ae 2674 */
833b8a50
AK
2675 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
2676 == VXLAN_FLOOD_HEAD_END_REPL) {
fd069644 2677 build_evpn_type3_prefix(&p, vpn->originator_ip);
c44ab6f1 2678 ret = update_evpn_route(bgp, vpn, &p, 0, 0, NULL);
fd069644
DS
2679 if (ret)
2680 return ret;
2681 }
128ea8ab 2682
852d9f97
SW
2683 update_all_type2_routes(bgp, vpn);
2684 return 0;
128ea8ab 2685}
2686
2687/*
2688 * Delete (and withdraw) local routes for specified VNI from the global
2689 * table and per-VNI table. After this, remove all other routes from
2690 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2691 * (advertise-all-vni) being disabled.
2692 */
d62a17ae 2693static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2694{
d62a17ae 2695 int ret;
2696 struct prefix_evpn p;
128ea8ab 2697
d62a17ae 2698 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2699 * followed by type-3 routes (only one) - for this VNI.
2700 */
852d9f97 2701 delete_all_type2_routes(bgp, vpn);
128ea8ab 2702
d62a17ae 2703 build_evpn_type3_prefix(&p, vpn->originator_ip);
2704 ret = delete_evpn_route(bgp, vpn, &p);
2705 if (ret)
2706 return ret;
128ea8ab 2707
d62a17ae 2708 /* Delete all routes from the per-VNI table. */
852d9f97
SW
2709 delete_all_vni_routes(bgp, vpn);
2710 return 0;
128ea8ab 2711}
2712
76d07c7a
AK
2713/*
2714 * There is a flood mcast IP address change. Update the mcast-grp and
2715 * remove the type-3 route if any. A new type-3 route will be generated
2716 * post tunnel_ip update if the new flood mode is head-end-replication.
2717 */
2718static int bgp_evpn_mcast_grp_change(struct bgp *bgp, struct bgpevpn *vpn,
2719 struct in_addr mcast_grp)
2720{
2721 struct prefix_evpn p;
2722
2723 vpn->mcast_grp = mcast_grp;
2724
2725 if (is_vni_live(vpn)) {
2726 build_evpn_type3_prefix(&p, vpn->originator_ip);
2727 delete_evpn_route(bgp, vpn, &p);
2728 }
2729
2730 return 0;
2731}
2732
128ea8ab 2733/*
d1911c26 2734 * There is a tunnel endpoint IP address change for this VNI, delete
2735 * prior type-3 route (if needed) and update.
2736 * Note: Route re-advertisement happens elsewhere after other processing
2737 * other changes.
128ea8ab 2738 */
4dabdde3
TA
2739static void handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
2740 struct in_addr originator_ip)
128ea8ab 2741{
d62a17ae 2742 struct prefix_evpn p;
128ea8ab 2743
4dabdde3
TA
2744 if (IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
2745 return;
2746
ddd16ed5
MK
2747 /* If VNI is not live, we only need to update the originator ip */
2748 if (!is_vni_live(vpn)) {
2749 vpn->originator_ip = originator_ip;
4dabdde3 2750 return;
ddd16ed5
MK
2751 }
2752
db0e1937
MK
2753 /* Update the tunnel-ip hash */
2754 bgp_tip_del(bgp, &vpn->originator_ip);
826c3f6d
TA
2755 if (bgp_tip_add(bgp, &originator_ip))
2756 /* The originator_ip was not already present in the
2757 * bgp martian next-hop table as a tunnel-ip, so we
2758 * need to go back and filter routes matching the new
2759 * martian next-hop.
2760 */
2761 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
db0e1937 2762
d62a17ae 2763 /* Need to withdraw type-3 route as the originator IP is part
2764 * of the key.
2765 */
2766 build_evpn_type3_prefix(&p, vpn->originator_ip);
2767 delete_evpn_route(bgp, vpn, &p);
128ea8ab 2768
d62a17ae 2769 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2770 vpn->originator_ip = originator_ip;
4dabdde3 2771 return;
185fb14a 2772}
50f74cf1 2773
185fb14a
AK
2774static struct bgp_path_info *
2775bgp_create_evpn_bgp_path_info(struct bgp_path_info *parent_pi,
2776 struct bgp_dest *dest, struct attr *attr)
2777{
2778 struct attr *attr_new;
2779 struct bgp_path_info *pi;
50f74cf1 2780
185fb14a
AK
2781 /* Add (or update) attribute to hash. */
2782 attr_new = bgp_attr_intern(attr);
50f74cf1 2783
185fb14a
AK
2784 /* Create new route with its attribute. */
2785 pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0, parent_pi->peer,
2786 attr_new, dest);
2787 SET_FLAG(pi->flags, BGP_PATH_VALID);
2788 bgp_path_info_extra_get(pi);
2789 pi->extra->parent = bgp_path_info_lock(parent_pi);
2790 bgp_dest_lock_node((struct bgp_dest *)parent_pi->net);
2791 if (parent_pi->extra) {
2792 memcpy(&pi->extra->label, &parent_pi->extra->label,
2793 sizeof(pi->extra->label));
2794 pi->extra->num_labels = parent_pi->extra->num_labels;
56c70d87 2795 pi->extra->igpmetric = parent_pi->extra->igpmetric;
50f74cf1 2796 }
34c7f35f 2797
185fb14a 2798 bgp_path_info_add(dest, pi);
50f74cf1 2799
185fb14a 2800 return pi;
50f74cf1 2801}
2802
d3135ba3 2803/*
2804 * Install route entry into the VRF routing table and invoke route selection.
2805 */
2806static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
bd494ec5 2807 const struct prefix_evpn *evp,
40381db7 2808 struct bgp_path_info *parent_pi)
d3135ba3 2809{
9bcb3eef 2810 struct bgp_dest *dest;
40381db7 2811 struct bgp_path_info *pi;
1ec31309 2812 struct attr attr;
d3135ba3 2813 struct attr *attr_new;
c4edf708 2814 int ret = 0;
d3135ba3 2815 struct prefix p;
2816 struct prefix *pp = &p;
2817 afi_t afi = 0;
2818 safi_t safi = 0;
5f0c5ec8 2819 bool new_pi = false;
58bff4d1
AK
2820 bool use_l3nhg = false;
2821 bool is_l3nhg_active = false;
a2299aba 2822 char buf1[INET6_ADDRSTRLEN];
d3135ba3 2823
2824 memset(pp, 0, sizeof(struct prefix));
3714a385 2825 ip_prefix_from_evpn_prefix(evp, pp);
d3135ba3 2826
2dbe669b 2827 if (bgp_debug_zebra(NULL))
996c9314 2828 zlog_debug(
2dbe669b
DA
2829 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2830 vrf_id_to_name(bgp_vrf->vrf_id), evp, parent_pi,
2831 parent_pi->flags);
1eb88002 2832
d3135ba3 2833 /* Create (or fetch) route within the VRF. */
2834 /* NOTE: There is no RD here. */
3714a385 2835 if (is_evpn_prefix_ipaddr_v4(evp)) {
d3135ba3 2836 afi = AFI_IP;
2837 safi = SAFI_UNICAST;
9bcb3eef 2838 dest = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
3714a385 2839 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
d3135ba3 2840 afi = AFI_IP6;
2841 safi = SAFI_UNICAST;
9bcb3eef 2842 dest = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
d3135ba3 2843 } else
2844 return 0;
2845
1ec31309 2846 /* EVPN routes currently only support a IPv4 next hop which corresponds
2847 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
450e362d 2848 * or prefix route, we have to convert the next hop to an IPv4-mapped
2849 * address for the rest of the code to flow through. In the case of IPv4,
2850 * make sure to set the flag for next hop attribute.
1ec31309 2851 */
6f4f49b2 2852 attr = *parent_pi->attr;
a2299aba
AD
2853 if (attr.evpn_overlay.type != OVERLAY_INDEX_GATEWAY_IP) {
2854 if (afi == AFI_IP6)
2855 evpn_convert_nexthop_to_ipv6(&attr);
7226bc40
TA
2856 else {
2857 attr.nexthop = attr.mp_nexthop_global_in;
a2299aba 2858 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
7226bc40 2859 }
a2299aba
AD
2860 } else {
2861
2862 /*
2863 * If gateway IP overlay index is specified in the NLRI of
2864 * EVPN RT-5, this gateway IP should be used as the nexthop
2865 * for the prefix in the VRF
2866 */
2867 if (bgp_debug_zebra(NULL)) {
2868 zlog_debug(
2869 "Install gateway IP %s as nexthop for prefix %pFX in vrf %s",
2870 inet_ntop(pp->family, &attr.evpn_overlay.gw_ip,
2871 buf1, sizeof(buf1)), pp,
2872 vrf_id_to_name(bgp_vrf->vrf_id));
2873 }
2874
2875 if (afi == AFI_IP6) {
2876 memcpy(&attr.mp_nexthop_global,
860e740b 2877 &attr.evpn_overlay.gw_ip.ipaddr_v6,
a2299aba
AD
2878 sizeof(struct in6_addr));
2879 attr.mp_nexthop_len = IPV6_MAX_BYTELEN;
2880 } else {
860e740b 2881 attr.nexthop = attr.evpn_overlay.gw_ip.ipaddr_v4;
a2299aba
AD
2882 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
2883 }
2884 }
1ec31309 2885
58bff4d1
AK
2886 bgp_evpn_es_vrf_use_nhg(bgp_vrf, &parent_pi->attr->esi, &use_l3nhg,
2887 &is_l3nhg_active, NULL);
2888 if (use_l3nhg)
2889 attr.es_flags |= ATTR_ES_L3_NHG_USE;
2890 if (is_l3nhg_active)
2891 attr.es_flags |= ATTR_ES_L3_NHG_ACTIVE;
2892
d3135ba3 2893 /* Check if route entry is already present. */
9bcb3eef 2894 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
40381db7
DS
2895 if (pi->extra
2896 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
d3135ba3 2897 break;
2898
5f0c5ec8 2899 if (!pi) {
9bcb3eef 2900 pi = bgp_create_evpn_bgp_path_info(parent_pi, dest, &attr);
5f0c5ec8 2901 new_pi = true;
2902 } else {
40381db7
DS
2903 if (attrhash_cmp(pi->attr, &attr)
2904 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
9bcb3eef 2905 bgp_dest_unlock_node(dest);
d3135ba3 2906 return 0;
2907 }
2908 /* The attribute has changed. */
2909 /* Add (or update) attribute to hash. */
1ec31309 2910 attr_new = bgp_attr_intern(&attr);
d3135ba3 2911
2912 /* Restore route, if needed. */
40381db7 2913 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
9bcb3eef 2914 bgp_path_info_restore(dest, pi);
d3135ba3 2915
2916 /* Mark if nexthop has changed. */
40381db7
DS
2917 if ((afi == AFI_IP
2918 && !IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
2919 || (afi == AFI_IP6
2920 && !IPV6_ADDR_SAME(&pi->attr->mp_nexthop_global,
2921 &attr_new->mp_nexthop_global)))
2922 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
d3135ba3 2923
9bcb3eef 2924 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
d3135ba3 2925 /* Unintern existing, set to new. */
40381db7
DS
2926 bgp_attr_unintern(&pi->attr);
2927 pi->attr = attr_new;
083ec940 2928 pi->uptime = monotime(NULL);
d3135ba3 2929 }
a2299aba
AD
2930
2931 /* Gateway IP nexthop should be resolved */
2932 if (attr.evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
2933 if (bgp_find_or_add_nexthop(bgp_vrf, bgp_vrf, afi, safi, pi,
654a5978 2934 NULL, 0, NULL))
a2299aba
AD
2935 bgp_path_info_set_flag(dest, pi, BGP_PATH_VALID);
2936 else {
2937 if (BGP_DEBUG(nht, NHT)) {
2938 inet_ntop(pp->family,
2939 &attr.evpn_overlay.gw_ip,
2940 buf1, sizeof(buf1));
2941 zlog_debug("%s: gateway IP NH unresolved",
2942 buf1);
2943 }
2944 bgp_path_info_unset_flag(dest, pi, BGP_PATH_VALID);
2945 }
2946 } else {
2947
2948 /* as it is an importation, change nexthop */
2949 bgp_path_info_set_flag(dest, pi, BGP_PATH_ANNC_NH_SELF);
2950 }
d3135ba3 2951
090efa2f
AK
2952 /* Link path to evpn nexthop */
2953 bgp_evpn_path_nh_add(bgp_vrf, pi);
2954
9bcb3eef 2955 bgp_aggregate_increment(bgp_vrf, bgp_dest_get_prefix(dest), pi, afi,
b54892e0 2956 safi);
b49cdf4c 2957
d3135ba3 2958 /* Perform route selection and update zebra, if required. */
9bcb3eef 2959 bgp_process(bgp_vrf, dest, afi, safi);
d3135ba3 2960
7452e879 2961 /* Process for route leaking. */
2962 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf, pi);
2963
9bcb3eef 2964 bgp_dest_unlock_node(dest);
3c11d70a 2965
5f0c5ec8 2966 if (bgp_debug_zebra(NULL))
c10e14e9
DS
2967 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2968 new_pi ? "new" : "update", dest,
2969 bgp_dest_get_lock_count(dest), pi, pi->lock,
2970 pi->flags);
5f0c5ec8 2971
d3135ba3 2972 return ret;
2973}
2974
128ea8ab 2975/*
852d9f97 2976 * Common handling for vni route tables install/selection.
128ea8ab 2977 */
852d9f97
SW
2978static int install_evpn_route_entry_in_vni_common(
2979 struct bgp *bgp, struct bgpevpn *vpn, const struct prefix_evpn *p,
2980 struct bgp_dest *dest, struct bgp_path_info *parent_pi)
d62a17ae 2981{
40381db7 2982 struct bgp_path_info *pi;
9c7edc03 2983 struct bgp_path_info *local_pi;
d62a17ae 2984 struct attr *attr_new;
2985 int ret;
74efb822
AK
2986 bool old_local_es = false;
2987 bool new_local_es;
c44ab6f1 2988
d62a17ae 2989 /* Check if route entry is already present. */
9bcb3eef 2990 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
40381db7
DS
2991 if (pi->extra
2992 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
d62a17ae 2993 break;
2994
bd0254af
MS
2995 if (!pi) {
2996 /* Create an info */
26c03e43 2997 pi = bgp_create_evpn_bgp_path_info(parent_pi, dest,
4e72ff72 2998 parent_pi->attr);
34c7f35f 2999
852d9f97
SW
3000 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3001 if (is_evpn_type2_dest_ipaddr_none(dest))
3002 evpn_type2_path_info_set_ip(
3003 pi, p->prefix.macip_addr.ip);
3004 else
3005 evpn_type2_path_info_set_mac(
3006 pi, p->prefix.macip_addr.mac);
3007 }
34c7f35f 3008
74efb822 3009 new_local_es = bgp_evpn_attr_is_local_es(pi->attr);
bd0254af 3010 } else {
b5118501
TA
3011 /* Return early if attributes haven't changed
3012 * and dest isn't flagged for removal.
3013 * dest will be unlocked by either
3014 * install_evpn_route_entry_in_vni_mac() or
3015 * install_evpn_route_entry_in_vni_ip()
3016 */
3017 if (attrhash_cmp(pi->attr, parent_pi->attr) &&
3018 !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
d62a17ae 3019 return 0;
d62a17ae 3020 /* The attribute has changed. */
3021 /* Add (or update) attribute to hash. */
40381db7 3022 attr_new = bgp_attr_intern(parent_pi->attr);
d62a17ae 3023
50f74cf1 3024 /* Restore route, if needed. */
40381db7 3025 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
9bcb3eef 3026 bgp_path_info_restore(dest, pi);
50f74cf1 3027
3028 /* Mark if nexthop has changed. */
40381db7
DS
3029 if (!IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
3030 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
50f74cf1 3031
74efb822
AK
3032 old_local_es = bgp_evpn_attr_is_local_es(pi->attr);
3033 new_local_es = bgp_evpn_attr_is_local_es(attr_new);
3034 /* If ESI is different or if its type has changed we
3035 * need to reinstall the path in zebra
3036 */
3037 if ((old_local_es != new_local_es)
3038 || memcmp(&pi->attr->esi, &attr_new->esi,
3039 sizeof(attr_new->esi))) {
3040
3041 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
3042 zlog_debug("VNI %d path %pFX chg to %s es",
3043 vpn->vni, &pi->net->p,
3044 new_local_es ? "local"
3045 : "non-local");
3046 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
3047 }
3048
50f74cf1 3049 /* Unintern existing, set to new. */
40381db7
DS
3050 bgp_attr_unintern(&pi->attr);
3051 pi->attr = attr_new;
083ec940 3052 pi->uptime = monotime(NULL);
50f74cf1 3053 }
3054
021b6596
AD
3055 /* Add this route to remote IP hashtable */
3056 bgp_evpn_remote_ip_hash_add(vpn, pi);
3057
50f74cf1 3058 /* Perform route selection and update zebra, if required. */
9bcb3eef 3059 ret = evpn_route_select_install(bgp, vpn, dest);
50f74cf1 3060
9c7edc03
AK
3061 /* if the best path is a local path with a non-zero ES
3062 * sync info against the local path may need to be updated
3063 * when a remote path is added/updated (including changes
3064 * from sync-path to remote-path)
3065 */
3066 local_pi = bgp_evpn_route_get_local_path(bgp, dest);
74efb822 3067 if (local_pi && (old_local_es || new_local_es))
9c7edc03 3068 bgp_evpn_update_type2_route_entry(bgp, vpn, dest, local_pi,
74efb822 3069 __func__);
852d9f97
SW
3070
3071 return ret;
3072}
3073
3074/*
3075 * Common handling for vni route tables uninstall/selection.
3076 */
3077static int uninstall_evpn_route_entry_in_vni_common(
3078 struct bgp *bgp, struct bgpevpn *vpn, const struct prefix_evpn *p,
3079 struct bgp_dest *dest, struct bgp_path_info *parent_pi)
3080{
3081 struct bgp_path_info *pi;
3082 struct bgp_path_info *local_pi;
3083 int ret;
3084
3085 /* Find matching route entry. */
3086 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
3087 if (pi->extra &&
3088 (struct bgp_path_info *)pi->extra->parent == parent_pi)
3089 break;
3090
3091 if (!pi)
3092 return 0;
3093
3094 bgp_evpn_remote_ip_hash_del(vpn, pi);
3095
3096 /* Mark entry for deletion */
3097 bgp_path_info_delete(dest, pi);
3098
3099 /* Perform route selection and update zebra, if required. */
3100 ret = evpn_route_select_install(bgp, vpn, dest);
3101
3102 /* if the best path is a local path with a non-zero ES
3103 * sync info against the local path may need to be updated
3104 * when a remote path is deleted
3105 */
3106 local_pi = bgp_evpn_route_get_local_path(bgp, dest);
3107 if (local_pi && bgp_evpn_attr_is_local_es(local_pi->attr))
3108 bgp_evpn_update_type2_route_entry(bgp, vpn, dest, local_pi,
3109 __func__);
3110
3111 return ret;
3112}
3113
3114/*
3115 * Install route entry into VNI IP table and invoke route selection.
3116 */
3117static int install_evpn_route_entry_in_vni_ip(struct bgp *bgp,
3118 struct bgpevpn *vpn,
3119 const struct prefix_evpn *p,
3120 struct bgp_path_info *parent_pi)
3121{
3122 int ret;
3123 struct bgp_dest *dest;
3124
3125 /* Ignore MAC Only Type-2 */
3126 if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
3127 (is_evpn_prefix_ipaddr_none(p) == true))
3128 return 0;
3129
3130 /* Create (or fetch) route within the VNI IP table. */
3131 dest = bgp_evpn_vni_ip_node_get(vpn->ip_table, p, parent_pi);
3132
3133 ret = install_evpn_route_entry_in_vni_common(bgp, vpn, p, dest,
3134 parent_pi);
3135
3136 bgp_dest_unlock_node(dest);
3137
3138 return ret;
3139}
3140
3141/*
3142 * Install route entry into VNI MAC table and invoke route selection.
3143 */
3144static int install_evpn_route_entry_in_vni_mac(struct bgp *bgp,
3145 struct bgpevpn *vpn,
3146 const struct prefix_evpn *p,
3147 struct bgp_path_info *parent_pi)
3148{
3149 int ret;
3150 struct bgp_dest *dest;
3151
3152 /* Only type-2 routes go into this table */
3153 if (p->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
3154 return 0;
3155
3156 /* Create (or fetch) route within the VNI MAC table. */
3157 dest = bgp_evpn_vni_mac_node_get(vpn->mac_table, p, parent_pi);
3158
3159 ret = install_evpn_route_entry_in_vni_common(bgp, vpn, p, dest,
3160 parent_pi);
3161
3162 bgp_dest_unlock_node(dest);
3163
3164 return ret;
3165}
3166
3167/*
3168 * Uninstall route entry from VNI IP table and invoke route selection.
3169 */
3170static int uninstall_evpn_route_entry_in_vni_ip(struct bgp *bgp,
3171 struct bgpevpn *vpn,
3172 const struct prefix_evpn *p,
3173 struct bgp_path_info *parent_pi)
3174{
3175 int ret;
3176 struct bgp_dest *dest;
3177
3178 /* Ignore MAC Only Type-2 */
3179 if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
3180 (is_evpn_prefix_ipaddr_none(p) == true))
3181 return 0;
3182
3183 /* Locate route within the VNI IP table. */
3184 dest = bgp_evpn_vni_ip_node_lookup(vpn->ip_table, p, parent_pi);
3185 if (!dest)
3186 return 0;
3187
3188 ret = uninstall_evpn_route_entry_in_vni_common(bgp, vpn, p, dest,
3189 parent_pi);
3190
9bcb3eef 3191 bgp_dest_unlock_node(dest);
a97a1e11 3192
50f74cf1 3193 return ret;
3194}
3195
852d9f97
SW
3196/*
3197 * Uninstall route entry from VNI IP table and invoke route selection.
3198 */
3199static int
3200uninstall_evpn_route_entry_in_vni_mac(struct bgp *bgp, struct bgpevpn *vpn,
3201 const struct prefix_evpn *p,
3202 struct bgp_path_info *parent_pi)
3203{
3204 int ret;
3205 struct bgp_dest *dest;
3206
3207 /* Only type-2 routes go into this table */
3208 if (p->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
3209 return 0;
3210
3211 /* Locate route within the VNI MAC table. */
3212 dest = bgp_evpn_vni_mac_node_lookup(vpn->mac_table, p, parent_pi);
3213 if (!dest)
3214 return 0;
3215
3216 ret = uninstall_evpn_route_entry_in_vni_common(bgp, vpn, p, dest,
3217 parent_pi);
3218
3219 bgp_dest_unlock_node(dest);
3220
3221 return ret;
3222}
d3135ba3 3223/*
3224 * Uninstall route entry from the VRF routing table and send message
3225 * to zebra, if appropriate.
3226 */
3227static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
bd494ec5 3228 const struct prefix_evpn *evp,
40381db7 3229 struct bgp_path_info *parent_pi)
d3135ba3 3230{
9bcb3eef 3231 struct bgp_dest *dest;
40381db7 3232 struct bgp_path_info *pi;
c4edf708 3233 int ret = 0;
d3135ba3 3234 struct prefix p;
3235 struct prefix *pp = &p;
3236 afi_t afi = 0;
3237 safi_t safi = 0;
3238
3239 memset(pp, 0, sizeof(struct prefix));
3714a385 3240 ip_prefix_from_evpn_prefix(evp, pp);
d3135ba3 3241
2dbe669b 3242 if (bgp_debug_zebra(NULL))
996c9314 3243 zlog_debug(
2dbe669b
DA
3244 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
3245 vrf_id_to_name(bgp_vrf->vrf_id), evp, parent_pi,
3246 parent_pi->flags);
1eb88002 3247
d3135ba3 3248 /* Locate route within the VRF. */
3249 /* NOTE: There is no RD here. */
3714a385 3250 if (is_evpn_prefix_ipaddr_v4(evp)) {
d3135ba3 3251 afi = AFI_IP;
3252 safi = SAFI_UNICAST;
9bcb3eef 3253 dest = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
d3135ba3 3254 } else {
3255 afi = AFI_IP6;
3256 safi = SAFI_UNICAST;
9bcb3eef 3257 dest = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
d3135ba3 3258 }
3259
9bcb3eef 3260 if (!dest)
d3135ba3 3261 return 0;
3262
3263 /* Find matching route entry. */
9bcb3eef 3264 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
40381db7
DS
3265 if (pi->extra
3266 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
d3135ba3 3267 break;
3268
e71ad4b6
DA
3269 if (!pi) {
3270 bgp_dest_unlock_node(dest);
d3135ba3 3271 return 0;
e71ad4b6 3272 }
d3135ba3 3273
5f0c5ec8 3274 if (bgp_debug_zebra(NULL))
c10e14e9
DS
3275 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
3276 dest, bgp_dest_get_lock_count(dest), pi, pi->lock,
3277 pi->flags);
5f0c5ec8 3278
7452e879 3279 /* Process for route leaking. */
3280 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf, pi);
3281
9bcb3eef 3282 bgp_aggregate_decrement(bgp_vrf, bgp_dest_get_prefix(dest), pi, afi,
b54892e0 3283 safi);
4c7a11d5 3284
d3135ba3 3285 /* Mark entry for deletion */
9bcb3eef 3286 bgp_path_info_delete(dest, pi);
d3135ba3 3287
090efa2f
AK
3288 /* Unlink path to evpn nexthop */
3289 bgp_evpn_path_nh_del(bgp_vrf, pi);
3290
d3135ba3 3291 /* Perform route selection and update zebra, if required. */
9bcb3eef 3292 bgp_process(bgp_vrf, dest, afi, safi);
d3135ba3 3293
3294 /* Unlock route node. */
9bcb3eef 3295 bgp_dest_unlock_node(dest);
d3135ba3 3296
3297 return ret;
3298}
3299
128ea8ab 3300/*
852d9f97
SW
3301 * Install route entry into the VNI routing tables.
3302 */
3303static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
3304 const struct prefix_evpn *p,
3305 struct bgp_path_info *parent_pi)
3306{
3307 int ret = 0;
3308
3309 if (bgp_debug_update(parent_pi->peer, NULL, NULL, 1))
3310 zlog_debug(
3311 "%s (%u): Installing EVPN %pFX route in VNI %u IP/MAC table",
3312 vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3313
3314 ret = install_evpn_route_entry_in_vni_mac(bgp, vpn, p, parent_pi);
3315
3316 if (ret) {
3317 flog_err(
3318 EC_BGP_EVPN_FAIL,
3319 "%s (%u): Failed to install EVPN %pFX route in VNI %u MAC table",
3320 vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3321
3322 return ret;
3323 }
3324
3325 ret = install_evpn_route_entry_in_vni_ip(bgp, vpn, p, parent_pi);
3326
3327 if (ret) {
3328 flog_err(
3329 EC_BGP_EVPN_FAIL,
3330 "%s (%u): Failed to install EVPN %pFX route in VNI %u IP table",
3331 vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3332
3333 return ret;
3334 }
3335
3336 return ret;
3337}
3338
3339/*
3340 * Uninstall route entry from the VNI routing tables.
128ea8ab 3341 */
d62a17ae 3342static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
bd494ec5 3343 const struct prefix_evpn *p,
40381db7 3344 struct bgp_path_info *parent_pi)
128ea8ab 3345{
852d9f97 3346 int ret = 0;
c44ab6f1 3347
852d9f97
SW
3348 if (bgp_debug_update(parent_pi->peer, NULL, NULL, 1))
3349 zlog_debug(
3350 "%s (%u): Uninstalling EVPN %pFX route from VNI %u IP/MAC table",
3351 vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
128ea8ab 3352
852d9f97 3353 ret = uninstall_evpn_route_entry_in_vni_ip(bgp, vpn, p, parent_pi);
128ea8ab 3354
852d9f97
SW
3355 if (ret) {
3356 flog_err(
3357 EC_BGP_EVPN_FAIL,
3358 "%s (%u): Failed to uninstall EVPN %pFX route from VNI %u IP table",
3359 vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
128ea8ab 3360
852d9f97 3361 return ret;
e71ad4b6 3362 }
128ea8ab 3363
852d9f97 3364 ret = uninstall_evpn_route_entry_in_vni_mac(bgp, vpn, p, parent_pi);
128ea8ab 3365
852d9f97
SW
3366 if (ret) {
3367 flog_err(
3368 EC_BGP_EVPN_FAIL,
3369 "%s (%u): Failed to uninstall EVPN %pFX route from VNI %u MAC table",
3370 vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
9c7edc03 3371
852d9f97
SW
3372 return ret;
3373 }
128ea8ab 3374
d62a17ae 3375 return ret;
128ea8ab 3376}
3377
5ba238b7
MK
3378/*
3379 * Given a route entry and a VRF, see if this route entry should be
3380 * imported into the VRF i.e., RTs match.
3381 */
4b7e6066 3382static int is_route_matching_for_vrf(struct bgp *bgp_vrf,
40381db7 3383 struct bgp_path_info *pi)
5ba238b7 3384{
40381db7 3385 struct attr *attr = pi->attr;
5ba238b7 3386 struct ecommunity *ecom;
f6e07e1b 3387 uint32_t i;
5ba238b7
MK
3388
3389 assert(attr);
3390 /* Route should have valid RT to be even considered. */
3391 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3392 return 0;
3393
b53e67a3 3394 ecom = bgp_attr_get_ecommunity(attr);
5ba238b7
MK
3395 if (!ecom || !ecom->size)
3396 return 0;
3397
3398 /* For each extended community RT, see if it matches this VNI. If any RT
3399 * matches, we're done.
3400 */
3401 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
3402 uint8_t *pnt;
3403 uint8_t type, sub_type;
5ba238b7
MK
3404 struct ecommunity_val *eval;
3405 struct ecommunity_val eval_tmp;
3406 struct vrf_irt_node *irt;
3407
3408 /* Only deal with RTs */
34540b0d 3409 pnt = (ecom->val + (i * ecom->unit_size));
5ba238b7 3410 eval = (struct ecommunity_val *)(ecom->val
34540b0d 3411 + (i * ecom->unit_size));
5ba238b7
MK
3412 type = *pnt++;
3413 sub_type = *pnt++;
3414 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3415 continue;
3416
3417 /* See if this RT matches specified VNIs import RTs */
3418 irt = lookup_vrf_import_rt(eval);
5d9cbca2 3419 if (irt)
5ba238b7
MK
3420 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
3421 return 1;
3422
3423 /* Also check for non-exact match. In this, we mask out the AS
3424 * and
3425 * only check on the local-admin sub-field. This is to
3426 * facilitate using
3427 * VNI as the RT for EBGP peering too.
3428 */
3429 irt = NULL;
3430 if (type == ECOMMUNITY_ENCODE_AS
3431 || type == ECOMMUNITY_ENCODE_AS4
3432 || type == ECOMMUNITY_ENCODE_IP) {
34540b0d 3433 memcpy(&eval_tmp, eval, ecom->unit_size);
5ba238b7
MK
3434 mask_ecom_global_admin(&eval_tmp, eval);
3435 irt = lookup_vrf_import_rt(&eval_tmp);
3436 }
5d9cbca2 3437 if (irt)
5ba238b7
MK
3438 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
3439 return 1;
3440 }
3441
3442 return 0;
3443}
3444
128ea8ab 3445/*
3446 * Given a route entry and a VNI, see if this route entry should be
3447 * imported into the VNI i.e., RTs match.
3448 */
d62a17ae 3449static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
40381db7 3450 struct bgp_path_info *pi)
d62a17ae 3451{
40381db7 3452 struct attr *attr = pi->attr;
d62a17ae 3453 struct ecommunity *ecom;
f6e07e1b 3454 uint32_t i;
d62a17ae 3455
3456 assert(attr);
3457 /* Route should have valid RT to be even considered. */
3458 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3459 return 0;
3460
b53e67a3 3461 ecom = bgp_attr_get_ecommunity(attr);
d62a17ae 3462 if (!ecom || !ecom->size)
3463 return 0;
3464
3465 /* For each extended community RT, see if it matches this VNI. If any RT
3466 * matches, we're done.
3467 */
3468 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
3469 uint8_t *pnt;
3470 uint8_t type, sub_type;
d62a17ae 3471 struct ecommunity_val *eval;
3472 struct ecommunity_val eval_tmp;
3473 struct irt_node *irt;
3474
3475 /* Only deal with RTs */
34540b0d 3476 pnt = (ecom->val + (i * ecom->unit_size));
d62a17ae 3477 eval = (struct ecommunity_val *)(ecom->val
34540b0d 3478 + (i * ecom->unit_size));
d62a17ae 3479 type = *pnt++;
3480 sub_type = *pnt++;
3481 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3482 continue;
3483
3484 /* See if this RT matches specified VNIs import RTs */
3485 irt = lookup_import_rt(bgp, eval);
b1ab0dfe 3486 if (irt)
d62a17ae 3487 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
3488 return 1;
3489
3490 /* Also check for non-exact match. In this, we mask out the AS
3491 * and
3492 * only check on the local-admin sub-field. This is to
3493 * facilitate using
3494 * VNI as the RT for EBGP peering too.
3495 */
3496 irt = NULL;
3497 if (type == ECOMMUNITY_ENCODE_AS
3498 || type == ECOMMUNITY_ENCODE_AS4
3499 || type == ECOMMUNITY_ENCODE_IP) {
34540b0d 3500 memcpy(&eval_tmp, eval, ecom->unit_size);
d62a17ae 3501 mask_ecom_global_admin(&eval_tmp, eval);
3502 irt = lookup_import_rt(bgp, &eval_tmp);
3503 }
b1ab0dfe 3504 if (irt)
d62a17ae 3505 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
3506 return 1;
3507 }
3508
3509 return 0;
128ea8ab 3510}
3511
47bf0432
CS
3512/* This API will scan evpn routes for checking attribute's rmac
3513 * macthes with bgp instance router mac. It avoid installing
3514 * route into bgp vrf table and remote rmac in bridge table.
3515 */
3516static int bgp_evpn_route_rmac_self_check(struct bgp *bgp_vrf,
bd494ec5 3517 const struct prefix_evpn *evp,
47bf0432
CS
3518 struct bgp_path_info *pi)
3519{
3520 /* evpn route could have learnt prior to L3vni has come up,
3521 * perform rmac check before installing route and
3522 * remote router mac.
3523 * The route will be removed from global bgp table once
3524 * SVI comes up with MAC and stored in hash, triggers
3525 * bgp_mac_rescan_all_evpn_tables.
3526 */
05864da7 3527 if (memcmp(&bgp_vrf->rmac, &pi->attr->rmac, ETH_ALEN) == 0) {
47bf0432 3528 if (bgp_debug_update(pi->peer, NULL, NULL, 1)) {
47bf0432
CS
3529 char attr_str[BUFSIZ] = {0};
3530
5022c833 3531 bgp_dump_attr(pi->attr, attr_str, sizeof(attr_str));
47bf0432 3532
2dbe669b
DA
3533 zlog_debug(
3534 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
3535 __func__, bgp_vrf->vrf_id, evp, attr_str);
47bf0432
CS
3536 }
3537
3538 return 1;
3539 }
3540
3541 return 0;
3542}
3543
bbc57c6c
AK
3544/* don't import hosts that are locally attached */
3545static inline bool
36dd4574
AK
3546bgp_evpn_skip_vrf_import_of_local_es(struct bgp *bgp_vrf,
3547 const struct prefix_evpn *evp,
bbc57c6c
AK
3548 struct bgp_path_info *pi, int install)
3549{
26c03e43 3550 esi_t *esi;
bbc57c6c 3551
26c03e43
AK
3552 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3553 esi = bgp_evpn_attr_get_esi(pi->attr);
3554
3555 /* Don't import routes that point to a local destination */
3556 if (bgp_evpn_attr_is_local_es(pi->attr)) {
3557 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
3558 char esi_buf[ESI_STR_LEN];
3559
3560 zlog_debug(
3561 "vrf %s of evpn prefix %pFX skipped, local es %s",
3562 install ? "import" : "unimport", evp,
3563 esi_to_str(esi, esi_buf,
3564 sizeof(esi_buf)));
3565 }
3566 return true;
3567 }
58bff4d1
AK
3568 }
3569 return false;
3570}
26c03e43 3571
58bff4d1
AK
3572/*
3573 * Install or uninstall a mac-ip route in the provided vrf if
3574 * there is a rt match
3575 */
3576int bgp_evpn_route_entry_install_if_vrf_match(struct bgp *bgp_vrf,
3577 struct bgp_path_info *pi,
3578 int install)
3579{
3580 int ret = 0;
3581 const struct prefix_evpn *evp =
3582 (const struct prefix_evpn *)bgp_dest_get_prefix(pi->net);
3583
3584 /* Consider "valid" remote routes applicable for
3585 * this VRF.
3586 */
3587 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3588 && pi->type == ZEBRA_ROUTE_BGP
3589 && pi->sub_type == BGP_ROUTE_NORMAL))
3590 return 0;
3591
58bff4d1
AK
3592 if (is_route_matching_for_vrf(bgp_vrf, pi)) {
3593 if (bgp_evpn_route_rmac_self_check(bgp_vrf, evp, pi))
3594 return 0;
3595
74efb822 3596 /* don't import hosts that are locally attached */
e57e63eb 3597 if (install && bgp_evpn_skip_vrf_import_of_local_es(
3598 bgp_vrf, evp, pi, install))
3599 return 0;
3600
3601 if (install)
58bff4d1 3602 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, pi);
26c03e43 3603 else
58bff4d1
AK
3604 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
3605 pi);
26c03e43 3606
58bff4d1
AK
3607 if (ret)
3608 flog_err(EC_BGP_EVPN_FAIL,
3609 "Failed to %s EVPN %pFX route in VRF %s",
3610 install ? "install" : "uninstall", evp,
3611 vrf_id_to_name(bgp_vrf->vrf_id));
bbc57c6c 3612 }
58bff4d1
AK
3613
3614 return ret;
bbc57c6c
AK
3615}
3616
5ba238b7
MK
3617/*
3618 * Install or uninstall mac-ip routes are appropriate for this
3619 * particular VRF.
3620 */
996c9314 3621static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
5ba238b7
MK
3622{
3623 afi_t afi;
3624 safi_t safi;
9bcb3eef 3625 struct bgp_dest *rd_dest, *dest;
5ba238b7 3626 struct bgp_table *table;
40381db7 3627 struct bgp_path_info *pi;
5ba238b7 3628 int ret;
5e53dce3 3629 struct bgp *bgp_evpn = NULL;
5ba238b7
MK
3630
3631 afi = AFI_L2VPN;
3632 safi = SAFI_EVPN;
5e53dce3
T
3633 bgp_evpn = bgp_get_evpn();
3634 if (!bgp_evpn)
5ba238b7
MK
3635 return -1;
3636
3637 /* Walk entire global routing table and evaluate routes which could be
3638 * imported into this VRF. Note that we need to loop through all global
3639 * routes to determine which route matches the import rt on vrf
3640 */
9bcb3eef
DS
3641 for (rd_dest = bgp_table_top(bgp_evpn->rib[afi][safi]); rd_dest;
3642 rd_dest = bgp_route_next(rd_dest)) {
3643 table = bgp_dest_get_bgp_table_info(rd_dest);
5ba238b7
MK
3644 if (!table)
3645 continue;
3646
9bcb3eef
DS
3647 for (dest = bgp_table_top(table); dest;
3648 dest = bgp_route_next(dest)) {
3649 const struct prefix_evpn *evp =
3650 (const struct prefix_evpn *)bgp_dest_get_prefix(
3651 dest);
5ba238b7 3652
1eb88002 3653 /* if not mac-ip route skip this route */
996c9314
LB
3654 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3655 || evp->prefix.route_type
3656 == BGP_EVPN_IP_PREFIX_ROUTE))
5ba238b7
MK
3657 continue;
3658
1eb88002 3659 /* if not a mac+ip route skip this route */
3714a385 3660 if (!(is_evpn_prefix_ipaddr_v4(evp)
3661 || is_evpn_prefix_ipaddr_v6(evp)))
1eb88002
MK
3662 continue;
3663
9bcb3eef 3664 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
6f94b685 3665 pi = pi->next) {
58bff4d1
AK
3666 ret = bgp_evpn_route_entry_install_if_vrf_match(
3667 bgp_vrf, pi, install);
3668 if (ret)
3669 return ret;
5ba238b7
MK
3670 }
3671 }
3672 }
3673
3674 return 0;
3675}
3676
128ea8ab 3677/*
3678 * Install or uninstall routes of specified type that are appropriate for this
3679 * particular VNI.
3680 */
d62a17ae 3681static int install_uninstall_routes_for_vni(struct bgp *bgp,
3682 struct bgpevpn *vpn,
3683 bgp_evpn_route_type rtype,
3684 int install)
3685{
0291c246
MK
3686 afi_t afi;
3687 safi_t safi;
9bcb3eef 3688 struct bgp_dest *rd_dest, *dest;
0291c246 3689 struct bgp_table *table;
40381db7 3690 struct bgp_path_info *pi;
0291c246 3691 int ret;
d62a17ae 3692
3693 afi = AFI_L2VPN;
3694 safi = SAFI_EVPN;
3695
3696 /* Walk entire global routing table and evaluate routes which could be
3697 * imported into this VPN. Note that we cannot just look at the routes
3698 * for
3699 * the VNI's RD - remote routes applicable for this VNI could have any
3700 * RD.
3701 */
3702 /* EVPN routes are a 2-level table. */
9bcb3eef
DS
3703 for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
3704 rd_dest = bgp_route_next(rd_dest)) {
3705 table = bgp_dest_get_bgp_table_info(rd_dest);
d62a17ae 3706 if (!table)
3707 continue;
3708
9bcb3eef
DS
3709 for (dest = bgp_table_top(table); dest;
3710 dest = bgp_route_next(dest)) {
b54892e0 3711 const struct prefix_evpn *evp =
9bcb3eef
DS
3712 (const struct prefix_evpn *)bgp_dest_get_prefix(
3713 dest);
d62a17ae 3714
3715 if (evp->prefix.route_type != rtype)
3716 continue;
3717
9bcb3eef 3718 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
6f94b685 3719 pi = pi->next) {
d62a17ae 3720 /* Consider "valid" remote routes applicable for
3721 * this VNI. */
40381db7
DS
3722 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3723 && pi->type == ZEBRA_ROUTE_BGP
3724 && pi->sub_type == BGP_ROUTE_NORMAL))
d62a17ae 3725 continue;
3726
40381db7 3727 if (is_route_matching_for_vni(bgp, vpn, pi)) {
d62a17ae 3728 if (install)
3729 ret = install_evpn_route_entry(
40381db7 3730 bgp, vpn, evp, pi);
d62a17ae 3731 else
3732 ret = uninstall_evpn_route_entry(
40381db7 3733 bgp, vpn, evp, pi);
d62a17ae 3734
3735 if (ret) {
af4c2728 3736 flog_err(
e50f7cfd 3737 EC_BGP_EVPN_FAIL,
d62a17ae 3738 "%u: Failed to %s EVPN %s route in VNI %u",
3739 bgp->vrf_id,
3740 install ? "install"
3741 : "uninstall",
3742 rtype == BGP_EVPN_MAC_IP_ROUTE
3743 ? "MACIP"
3744 : "IMET",
3745 vpn->vni);
dc52bece
DS
3746
3747 bgp_dest_unlock_node(rd_dest);
3748 bgp_dest_unlock_node(dest);
d62a17ae 3749 return ret;
3750 }
3751 }
3752 }
3753 }
3754 }
3755
3756 return 0;
128ea8ab 3757}
3758
5ba238b7 3759/* Install any existing remote routes applicable for this VRF into VRF RIB. This
523cafc4 3760 * is invoked upon l3vni-add or l3vni import rt change
3761 */
5ba238b7
MK
3762static int install_routes_for_vrf(struct bgp *bgp_vrf)
3763{
3764 install_uninstall_routes_for_vrf(bgp_vrf, 1);
3765 return 0;
3766}
3767
128ea8ab 3768/*
3769 * Install any existing remote routes applicable for this VNI into its
3770 * routing table. This is invoked when a VNI becomes "live" or its Import
3771 * RT is changed.
3772 */
d62a17ae 3773static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3774{
d62a17ae 3775 int ret;
128ea8ab 3776
d62a17ae 3777 /* Install type-3 routes followed by type-2 routes - the ones applicable
3778 * for this VNI.
3779 */
3780 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3781 1);
3782 if (ret)
3783 return ret;
128ea8ab 3784
c44ab6f1
AK
3785 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_AD_ROUTE,
3786 1);
3787 if (ret)
3788 return ret;
3789
d62a17ae 3790 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3791 1);
128ea8ab 3792}
3793
5ba238b7
MK
3794/* uninstall routes from l3vni vrf. */
3795static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
3796{
3797 install_uninstall_routes_for_vrf(bgp_vrf, 0);
3798 return 0;
3799}
3800
90e60aa7 3801/*
3802 * Uninstall any existing remote routes for this VNI. One scenario in which
3803 * this is invoked is upon an import RT change.
3804 */
d62a17ae 3805static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3806{
d62a17ae 3807 int ret;
90e60aa7 3808
d62a17ae 3809 /* Uninstall type-2 routes followed by type-3 routes - the ones
3810 * applicable
3811 * for this VNI.
3812 */
3813 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3814 0);
3815 if (ret)
3816 return ret;
90e60aa7 3817
c44ab6f1 3818 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_AD_ROUTE,
9c49ac74 3819 0);
c44ab6f1
AK
3820 if (ret)
3821 return ret;
3822
3823
d62a17ae 3824 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3825 0);
90e60aa7 3826}
3827
d3135ba3 3828/*
3829 * Install or uninstall route in matching VRFs (list).
3830 */
3831static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
3832 safi_t safi, struct prefix_evpn *evp,
40381db7 3833 struct bgp_path_info *pi,
d3135ba3 3834 struct list *vrfs, int install)
3835{
d3135ba3 3836 struct bgp *bgp_vrf;
3837 struct listnode *node, *nnode;
3838
90264d64 3839 /* Only type-2/type-5 routes go into a VRF */
996c9314
LB
3840 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3841 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
d3135ba3 3842 return 0;
3843
90264d64 3844 /* if it is type-2 route and not a mac+ip route skip this route */
996c9314 3845 if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 3846 && !(is_evpn_prefix_ipaddr_v4(evp)
3847 || is_evpn_prefix_ipaddr_v6(evp)))
30a30f57
MK
3848 return 0;
3849
d3135ba3 3850 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
3851 int ret;
3852
36dd4574 3853 /* don't import hosts that are locally attached */
e57e63eb 3854 if (install && bgp_evpn_skip_vrf_import_of_local_es(
3855 bgp_vrf, evp, pi, install))
3856 return 0;
3857
3858 if (install)
40381db7 3859 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, pi);
d3135ba3 3860 else
996c9314 3861 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
40381db7 3862 pi);
d3135ba3 3863
3864 if (ret) {
e50f7cfd 3865 flog_err(EC_BGP_EVPN_FAIL,
2dbe669b 3866 "%u: Failed to %s prefix %pFX in VRF %s",
1c50c1c0 3867 bgp_def->vrf_id,
2dbe669b 3868 install ? "install" : "uninstall", evp,
1c50c1c0 3869 vrf_id_to_name(bgp_vrf->vrf_id));
d3135ba3 3870 return ret;
3871 }
3872 }
3873
3874 return 0;
3875}
3876
128ea8ab 3877/*
3878 * Install or uninstall route in matching VNIs (list).
3879 */
d62a17ae 3880static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
3881 safi_t safi, struct prefix_evpn *evp,
40381db7 3882 struct bgp_path_info *pi,
d62a17ae 3883 struct list *vnis, int install)
128ea8ab 3884{
d62a17ae 3885 struct bgpevpn *vpn;
3886 struct listnode *node, *nnode;
128ea8ab 3887
d62a17ae 3888 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
3889 int ret;
128ea8ab 3890
d62a17ae 3891 if (!is_vni_live(vpn))
3892 continue;
128ea8ab 3893
d62a17ae 3894 if (install)
40381db7 3895 ret = install_evpn_route_entry(bgp, vpn, evp, pi);
d62a17ae 3896 else
40381db7 3897 ret = uninstall_evpn_route_entry(bgp, vpn, evp, pi);
128ea8ab 3898
d62a17ae 3899 if (ret) {
1c50c1c0
QY
3900 flog_err(EC_BGP_EVPN_FAIL,
3901 "%u: Failed to %s EVPN %s route in VNI %u",
3902 bgp->vrf_id, install ? "install" : "uninstall",
3903 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3904 ? "MACIP"
3905 : "IMET",
3906 vpn->vni);
d62a17ae 3907 return ret;
3908 }
3909 }
128ea8ab 3910
d62a17ae 3911 return 0;
128ea8ab 3912}
3913
3914/*
50f74cf1 3915 * Install or uninstall route for appropriate VNIs/ESIs.
128ea8ab 3916 */
26c03e43
AK
3917static int bgp_evpn_install_uninstall_table(struct bgp *bgp, afi_t afi,
3918 safi_t safi, const struct prefix *p,
3919 struct bgp_path_info *pi,
3920 int import, bool in_vni_rt,
3921 bool in_vrf_rt)
d62a17ae 3922{
3923 struct prefix_evpn *evp = (struct prefix_evpn *)p;
40381db7 3924 struct attr *attr = pi->attr;
d62a17ae 3925 struct ecommunity *ecom;
f6e07e1b 3926 uint32_t i;
c44ab6f1 3927 struct prefix_evpn ad_evp;
d62a17ae 3928
3929 assert(attr);
3930
fff7545a 3931 /* Only type-1, type-2, type-3, type-4 and type-5
3932 * are supported currently
3933 */
d62a17ae 3934 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
90264d64 3935 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
50f74cf1 3936 || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
c44ab6f1 3937 || evp->prefix.route_type == BGP_EVPN_AD_ROUTE
90264d64 3938 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
d62a17ae 3939 return 0;
3940
3941 /* If we don't have Route Target, nothing much to do. */
3942 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3943 return 0;
3944
c44ab6f1
AK
3945 /* EAD prefix in the global table doesn't include the VTEP-IP so
3946 * we need to create a different copy for the VNI
3947 */
3948 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
852d9f97
SW
3949 evp = evpn_type1_prefix_vni_ip_copy(&ad_evp, evp,
3950 attr->nexthop);
c44ab6f1 3951
b53e67a3 3952 ecom = bgp_attr_get_ecommunity(attr);
d62a17ae 3953 if (!ecom || !ecom->size)
3954 return -1;
3955
50f74cf1 3956 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3957 * attached to the route */
d62a17ae 3958 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
3959 uint8_t *pnt;
3960 uint8_t type, sub_type;
d62a17ae 3961 struct ecommunity_val *eval;
3962 struct ecommunity_val eval_tmp;
996c9314 3963 struct irt_node *irt; /* import rt for l2vni */
d3135ba3 3964 struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
c44ab6f1 3965 struct bgp_evpn_es *es;
d62a17ae 3966
3967 /* Only deal with RTs */
34540b0d 3968 pnt = (ecom->val + (i * ecom->unit_size));
d62a17ae 3969 eval = (struct ecommunity_val *)(ecom->val
34540b0d 3970 + (i * ecom->unit_size));
d62a17ae 3971 type = *pnt++;
3972 sub_type = *pnt++;
3973 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3974 continue;
3975
58bff4d1
AK
3976 /* non-local MAC-IP routes in the global route table are linked
3977 * to the destination ES
3978 */
3979 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3980 bgp_evpn_path_es_link(pi, 0,
3981 bgp_evpn_attr_get_esi(pi->attr));
3982
50f74cf1 3983 /*
3984 * macip routes (type-2) are imported into VNI and VRF tables.
3985 * IMET route is imported into VNI table.
3986 * prefix routes are imported into VRF table.
523cafc4 3987 */
50f74cf1 3988 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE ||
3989 evp->prefix.route_type == BGP_EVPN_IMET_ROUTE ||
c44ab6f1 3990 evp->prefix.route_type == BGP_EVPN_AD_ROUTE ||
50f74cf1 3991 evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
d62a17ae 3992
26c03e43 3993 irt = in_vni_rt ? lookup_import_rt(bgp, eval) : NULL;
50f74cf1 3994 if (irt)
40381db7
DS
3995 install_uninstall_route_in_vnis(
3996 bgp, afi, safi, evp, pi, irt->vnis,
3997 import);
50f74cf1 3998
26c03e43 3999 vrf_irt = in_vrf_rt ? lookup_vrf_import_rt(eval) : NULL;
50f74cf1 4000 if (vrf_irt)
40381db7
DS
4001 install_uninstall_route_in_vrfs(
4002 bgp, afi, safi, evp, pi, vrf_irt->vrfs,
4003 import);
50f74cf1 4004
4005 /* Also check for non-exact match.
4006 * In this, we mask out the AS and
4007 * only check on the local-admin sub-field.
4008 * This is to facilitate using
4009 * VNI as the RT for EBGP peering too.
4010 */
4011 irt = NULL;
4012 vrf_irt = NULL;
4013 if (type == ECOMMUNITY_ENCODE_AS
4014 || type == ECOMMUNITY_ENCODE_AS4
4015 || type == ECOMMUNITY_ENCODE_IP) {
34540b0d 4016 memcpy(&eval_tmp, eval, ecom->unit_size);
50f74cf1 4017 mask_ecom_global_admin(&eval_tmp, eval);
26c03e43
AK
4018 if (in_vni_rt)
4019 irt = lookup_import_rt(bgp, &eval_tmp);
4020 if (in_vrf_rt)
4021 vrf_irt =
4022 lookup_vrf_import_rt(&eval_tmp);
50f74cf1 4023 }
4024
4025 if (irt)
40381db7
DS
4026 install_uninstall_route_in_vnis(
4027 bgp, afi, safi, evp, pi, irt->vnis,
4028 import);
50f74cf1 4029 if (vrf_irt)
40381db7
DS
4030 install_uninstall_route_in_vrfs(
4031 bgp, afi, safi, evp, pi, vrf_irt->vrfs,
4032 import);
50f74cf1 4033 }
4034
4035 /* es route is imported into the es table */
4036 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE) {
4037
4038 /* we will match based on the entire esi to avoid
fff7545a 4039 * import of an es route for esi2 into esi1
50f74cf1 4040 */
c44ab6f1 4041 es = bgp_evpn_es_find(&evp->prefix.es_addr.esi);
9c7edc03 4042 if (es && bgp_evpn_is_es_local(es))
c44ab6f1 4043 bgp_evpn_es_route_install_uninstall(
40381db7 4044 bgp, es, afi, safi, evp, pi, import);
d62a17ae 4045 }
d62a17ae 4046 }
4047
4048 return 0;
128ea8ab 4049}
4050
26c03e43
AK
4051/*
4052 * Install or uninstall route for appropriate VNIs/ESIs.
4053 */
4054static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
4055 const struct prefix *p,
4056 struct bgp_path_info *pi, int import)
4057{
4058 return bgp_evpn_install_uninstall_table(bgp, afi, safi, p, pi, import,
4059 true, true);
4060}
4061
74efb822
AK
4062void bgp_evpn_import_type2_route(struct bgp_path_info *pi, int import)
4063{
4064 struct bgp *bgp_evpn;
4065
4066 bgp_evpn = bgp_get_evpn();
4067 if (!bgp_evpn)
4068 return;
4069
4070 install_uninstall_evpn_route(bgp_evpn, AFI_L2VPN, SAFI_EVPN,
4071 &pi->net->p, pi, import);
4072}
4073
2bb9eff4
DS
4074/*
4075 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
4076 * routes
4077 */
80b140af
MK
4078static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
4079{
5fd9c12b
KA
4080 /* Delete ipv4 default route and withdraw from peers */
4081 if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
4082 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
4083 SAFI_UNICAST, false);
4084
80b140af 4085 /* delete all ipv4 routes and withdraw from peers */
fdf19f06
MK
4086 if (advertise_type5_routes(bgp_vrf, AFI_IP))
4087 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
80b140af 4088
5fd9c12b
KA
4089 /* Delete ipv6 default route and withdraw from peers */
4090 if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
4091 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
4092 SAFI_UNICAST, false);
4093
80b140af 4094 /* delete all ipv6 routes and withdraw from peers */
fdf19f06
MK
4095 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
4096 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
80b140af
MK
4097}
4098
2bb9eff4
DS
4099/*
4100 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
4101 * routes
4102 */
5394a276 4103void update_advertise_vrf_routes(struct bgp *bgp_vrf)
80b140af 4104{
5394a276
CS
4105 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
4106
4107 bgp_evpn = bgp_get_evpn();
4108 if (!bgp_evpn)
4109 return;
4110
80b140af 4111 /* update all ipv4 routes */
fdf19f06
MK
4112 if (advertise_type5_routes(bgp_vrf, AFI_IP))
4113 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
80b140af 4114
5fd9c12b
KA
4115 /* update ipv4 default route and withdraw from peers */
4116 if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
4117 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
4118 SAFI_UNICAST, true);
4119
80b140af 4120 /* update all ipv6 routes */
fdf19f06
MK
4121 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
4122 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
5fd9c12b
KA
4123
4124 /* update ipv6 default route and withdraw from peers */
4125 if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
4126 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
4127 SAFI_UNICAST, true);
4128
80b140af
MK
4129}
4130
676f83b9 4131/*
4132 * update and advertise local routes for a VRF as type-5 routes.
4133 * This is invoked upon RD change for a VRF. Note taht the processing is only
4134 * done in the global route table using the routes which already exist in the
4135 * VRF routing table
4136 */
80b140af 4137static void update_router_id_vrf(struct bgp *bgp_vrf)
676f83b9 4138{
80b140af
MK
4139 /* skip if the RD is configured */
4140 if (is_vrf_rd_configured(bgp_vrf))
4141 return;
4142
4143 /* derive the RD for the VRF based on new router-id */
4144 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
4145
4146 /* update advertise ipv4|ipv6 routes as type-5 routes */
4147 update_advertise_vrf_routes(bgp_vrf);
676f83b9 4148}
4149
4150/*
4151 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
4152 * This is invoked upon VRF RD change. The processing is done only from global
4153 * table.
4154 */
80b140af 4155static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
676f83b9 4156{
80b140af
MK
4157 /* skip if the RD is configured */
4158 if (is_vrf_rd_configured(bgp_vrf))
4159 return;
4160
4161 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
4162 delete_withdraw_vrf_routes(bgp_vrf);
676f83b9 4163}
4164
852d9f97
SW
4165static void update_advertise_vni_route(struct bgp *bgp, struct bgpevpn *vpn,
4166 struct bgp_dest *dest)
4167{
4168 struct bgp_dest *global_dest;
4169 struct bgp_path_info *pi, *global_pi;
4170 struct attr *attr;
4171 afi_t afi = AFI_L2VPN;
4172 safi_t safi = SAFI_EVPN;
4173
4174 struct prefix_evpn tmp_evp;
4175 const struct prefix_evpn *evp =
4176 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
4177
4178 /*
4179 * We have already processed type-3 routes.
4180 * Process only type-1 and type-2 routes here.
4181 */
4182 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE &&
4183 evp->prefix.route_type != BGP_EVPN_AD_ROUTE)
4184 return;
4185
4186 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
4187 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP &&
4188 pi->sub_type == BGP_ROUTE_STATIC)
4189 break;
4190 if (!pi)
4191 return;
4192
4193 /*
4194 * VNI table MAC-IP prefixes don't have MAC so make sure it's
4195 * set from path info here.
4196 */
4197 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
4198 if (is_evpn_prefix_ipaddr_none(evp)) {
4199 /* VNI MAC -> Global */
4200 evpn_type2_prefix_global_copy(
4201 &tmp_evp, evp, NULL /* mac */,
4202 evpn_type2_path_info_get_ip(pi));
4203 } else {
4204 /* VNI IP -> Global */
4205 evpn_type2_prefix_global_copy(
4206 &tmp_evp, evp, evpn_type2_path_info_get_mac(pi),
4207 NULL /* ip */);
4208 }
4209 } else {
4210 memcpy(&tmp_evp, evp, sizeof(tmp_evp));
4211 }
4212
4213 /* Create route in global routing table using this route entry's
4214 * attribute.
4215 */
4216 attr = pi->attr;
4217 global_dest = bgp_evpn_global_node_get(bgp->rib[afi][safi], afi, safi,
4218 &tmp_evp, &vpn->prd, NULL);
4219 assert(global_dest);
4220
4221 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
4222 /* Type-2 route */
4223 update_evpn_route_entry(
4224 bgp, vpn, afi, safi, global_dest, attr, NULL /* mac */,
4225 NULL /* ip */, 1, &global_pi, 0,
4226 mac_mobility_seqnum(attr), false /* setup_sync */,
4227 NULL /* old_is_sync */);
4228 } else {
4229 /* Type-1 route */
4230 struct bgp_evpn_es *es;
4231 int route_changed = 0;
4232
4233 es = bgp_evpn_es_find(&evp->prefix.ead_addr.esi);
4234 bgp_evpn_mh_route_update(bgp, es, vpn, afi, safi, global_dest,
4235 attr, &global_pi, &route_changed);
4236 }
4237
4238 /* Schedule for processing and unlock node. */
4239 bgp_process(bgp, global_dest, afi, safi);
4240 bgp_dest_unlock_node(global_dest);
4241}
4242
90e60aa7 4243/*
4244 * Update and advertise local routes for a VNI. Invoked upon router-id
4245 * change. Note that the processing is done only on the global route table
4246 * using routes that already exist in the per-VNI table.
4247 */
852d9f97 4248static void update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
d62a17ae 4249{
4250 struct prefix_evpn p;
9bcb3eef 4251 struct bgp_dest *dest, *global_dest;
852d9f97 4252 struct bgp_path_info *pi;
d62a17ae 4253 struct attr *attr;
4254 afi_t afi = AFI_L2VPN;
4255 safi_t safi = SAFI_EVPN;
4256
4257 /* Locate type-3 route for VNI in the per-VNI table and use its
4258 * attributes to create and advertise the type-3 route for this VNI
4259 * in the global table.
fd069644
DS
4260 *
4261 * RT-3 only if doing head-end replication
d62a17ae 4262 */
833b8a50
AK
4263 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
4264 == VXLAN_FLOOD_HEAD_END_REPL) {
fd069644 4265 build_evpn_type3_prefix(&p, vpn->originator_ip);
852d9f97 4266 dest = bgp_evpn_vni_node_lookup(vpn, &p, NULL);
9bcb3eef 4267 if (!dest) /* unexpected */
852d9f97 4268 return;
9bcb3eef 4269 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
fd069644
DS
4270 if (pi->peer == bgp->peer_self &&
4271 pi->type == ZEBRA_ROUTE_BGP
4272 && pi->sub_type == BGP_ROUTE_STATIC)
4273 break;
fd43ffd9
DA
4274 if (!pi) {
4275 bgp_dest_unlock_node(dest);
852d9f97 4276 return;
fd43ffd9 4277 }
852d9f97 4278
fd069644 4279 attr = pi->attr;
d62a17ae 4280
34c7f35f
SW
4281 global_dest = bgp_evpn_global_node_get(
4282 bgp->rib[afi][safi], afi, safi, &p, &vpn->prd, NULL);
4283 update_evpn_route_entry(
4284 bgp, vpn, afi, safi, global_dest, attr, NULL /* mac */,
852d9f97 4285 NULL /* ip */, 1, &pi, 0, mac_mobility_seqnum(attr),
34c7f35f 4286 false /* setup_sync */, NULL /* old_is_sync */);
d62a17ae 4287
fd069644 4288 /* Schedule for processing and unlock node. */
9bcb3eef
DS
4289 bgp_process(bgp, global_dest, afi, safi);
4290 bgp_dest_unlock_node(global_dest);
fd069644 4291 }
d62a17ae 4292
852d9f97
SW
4293 /* Now, walk this VNI's MAC & IP route table and use the route and its
4294 * attribute to create and schedule route in global table.
d62a17ae 4295 */
852d9f97
SW
4296 for (dest = bgp_table_top(vpn->mac_table); dest;
4297 dest = bgp_route_next(dest))
4298 update_advertise_vni_route(bgp, vpn, dest);
d62a17ae 4299
852d9f97
SW
4300 for (dest = bgp_table_top(vpn->ip_table); dest;
4301 dest = bgp_route_next(dest))
4302 update_advertise_vni_route(bgp, vpn, dest);
90e60aa7 4303}
4304
4305/*
4306 * Delete (and withdraw) local routes for a VNI - only from the global
4307 * table. Invoked upon router-id change.
4308 */
d62a17ae 4309static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 4310{
d62a17ae 4311 struct prefix_evpn p;
9bcb3eef 4312 struct bgp_dest *global_dest;
40381db7 4313 struct bgp_path_info *pi;
d62a17ae 4314 afi_t afi = AFI_L2VPN;
4315 safi_t safi = SAFI_EVPN;
90e60aa7 4316
d62a17ae 4317 /* Delete and withdraw locally learnt type-2 routes (MACIP)
4318 * for this VNI - from the global table.
4319 */
17151ae9 4320 delete_global_type2_routes(bgp, vpn);
90e60aa7 4321
d62a17ae 4322 /* Remove type-3 route for this VNI from global table. */
4323 build_evpn_type3_prefix(&p, vpn->originator_ip);
34c7f35f
SW
4324 global_dest = bgp_evpn_global_node_lookup(bgp->rib[afi][safi], afi,
4325 safi, &p, &vpn->prd, NULL);
9bcb3eef 4326 if (global_dest) {
d62a17ae 4327 /* Delete route entry in the global EVPN table. */
9bcb3eef 4328 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
90e60aa7 4329
d62a17ae 4330 /* Schedule for processing - withdraws to peers happen from
4331 * this table.
4332 */
40381db7 4333 if (pi)
9bcb3eef
DS
4334 bgp_process(bgp, global_dest, afi, safi);
4335 bgp_dest_unlock_node(global_dest);
d62a17ae 4336 }
90e60aa7 4337
d60f63f0
AD
4338
4339 delete_global_ead_evi_routes(bgp, vpn);
d62a17ae 4340 return 0;
90e60aa7 4341}
4342
2d48ee25 4343/*
4344 * Handle router-id change. Update and advertise local routes corresponding
4345 * to this VNI from peers. Note that this is invoked after updating the
4346 * router-id. The routes in the per-VNI table are used to create routes in
4347 * the global table and schedule them.
4348 */
e3b78da8 4349static void update_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
2d48ee25 4350{
e3b78da8 4351 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
2d48ee25 4352
d62a17ae 4353 /* Skip VNIs with configured RD. */
4354 if (is_rd_configured(vpn))
4355 return;
2d48ee25 4356
d62a17ae 4357 bgp_evpn_derive_auto_rd(bgp, vpn);
4358 update_advertise_vni_routes(bgp, vpn);
2d48ee25 4359}
4360
4361/*
4362 * Handle router-id change. Delete and withdraw local routes corresponding
4363 * to this VNI from peers. Note that this is invoked prior to updating
4364 * the router-id and is done only on the global route table, the routes
4365 * are needed in the per-VNI table to re-advertise with new router id.
4366 */
e3b78da8 4367static void withdraw_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
2d48ee25 4368{
e3b78da8 4369 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
2d48ee25 4370
d62a17ae 4371 /* Skip VNIs with configured RD. */
4372 if (is_rd_configured(vpn))
4373 return;
2d48ee25 4374
d62a17ae 4375 delete_withdraw_vni_routes(bgp, vpn);
2d48ee25 4376}
4377
fd069644
DS
4378/*
4379 * Create RT-3 for a VNI and schedule for processing and advertisement.
4380 * This is invoked upon flooding mode changing to head-end replication.
4381 */
e3b78da8 4382static void create_advertise_type3(struct hash_bucket *bucket, void *data)
fd069644 4383{
e3b78da8 4384 struct bgpevpn *vpn = bucket->data;
fd069644
DS
4385 struct bgp *bgp = data;
4386 struct prefix_evpn p;
4387
833b8a50
AK
4388 if (!vpn || !is_vni_live(vpn) ||
4389 bgp_evpn_vni_flood_mode_get(bgp, vpn)
4390 != VXLAN_FLOOD_HEAD_END_REPL)
fd069644
DS
4391 return;
4392
4393 build_evpn_type3_prefix(&p, vpn->originator_ip);
c44ab6f1 4394 if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL))
fd069644
DS
4395 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
4396 "Type3 route creation failure for VNI %u", vpn->vni);
4397}
4398
4399/*
4400 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
4401 * This is invoked upon flooding mode changing to drop BUM packets.
4402 */
e3b78da8 4403static void delete_withdraw_type3(struct hash_bucket *bucket, void *data)
fd069644 4404{
e3b78da8 4405 struct bgpevpn *vpn = bucket->data;
fd069644
DS
4406 struct bgp *bgp = data;
4407 struct prefix_evpn p;
4408
4409 if (!vpn || !is_vni_live(vpn))
4410 return;
4411
4412 build_evpn_type3_prefix(&p, vpn->originator_ip);
4413 delete_evpn_route(bgp, vpn, &p);
4414}
4415
128ea8ab 4416/*
4417 * Process received EVPN type-2 route (advertise or withdraw).
4418 */
d62a17ae 4419static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a
QY
4420 struct attr *attr, uint8_t *pfx, int psize,
4421 uint32_t addpath_id)
d62a17ae 4422{
4423 struct prefix_rd prd;
e121d831
QY
4424 struct prefix_evpn p = {};
4425 struct bgp_route_evpn evpn = {};
d7c0a89a
QY
4426 uint8_t ipaddr_len;
4427 uint8_t macaddr_len;
e121d831
QY
4428 /* holds the VNI(s) as in packet */
4429 mpls_label_t label[BGP_MAX_LABELS] = {};
d7c0a89a 4430 uint32_t num_labels = 0;
554cd77a 4431 uint32_t eth_tag;
d62a17ae 4432 int ret;
4433
4434 /* Type-2 route should be either 33, 37 or 49 bytes or an
4435 * additional 3 bytes if there is a second label (VNI):
4436 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
4437 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
4438 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
4439 */
4440 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
4441 && psize != 40 && psize != 52) {
e50f7cfd 4442 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
1c50c1c0
QY
4443 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
4444 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 4445 return -1;
4446 }
4447
e121d831
QY
4448 struct stream *pkt = stream_new(psize);
4449 stream_put(pkt, pfx, psize);
554cd77a 4450
d62a17ae 4451 /* Make prefix_rd */
4452 prd.family = AF_UNSPEC;
4453 prd.prefixlen = 64;
e121d831
QY
4454
4455 STREAM_GET(&prd.val, pkt, 8);
d62a17ae 4456
4457 /* Make EVPN prefix. */
b03b8898 4458 p.family = AF_EVPN;
50f74cf1 4459 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 4460 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
4461
554cd77a 4462 /* Copy Ethernet Seg Identifier */
9c7edc03 4463 if (attr) {
e121d831
QY
4464 STREAM_GET(&attr->esi, pkt, sizeof(esi_t));
4465
74efb822 4466 if (bgp_evpn_is_esi_local_and_non_bypass(&attr->esi))
9c7edc03
AK
4467 attr->es_flags |= ATTR_ES_IS_LOCAL;
4468 else
4469 attr->es_flags &= ~ATTR_ES_IS_LOCAL;
e121d831
QY
4470 } else {
4471 STREAM_FORWARD_GETP(pkt, sizeof(esi_t));
9c7edc03 4472 }
d62a17ae 4473
554cd77a 4474 /* Copy Ethernet Tag */
e121d831 4475 STREAM_GET(&eth_tag, pkt, 4);
3714a385 4476 p.prefix.macip_addr.eth_tag = ntohl(eth_tag);
d62a17ae 4477
4478 /* Get the MAC Addr len */
e121d831 4479 STREAM_GETC(pkt, macaddr_len);
d62a17ae 4480
4481 /* Get the MAC Addr */
28328ea9 4482 if (macaddr_len == (ETH_ALEN * 8)) {
e121d831 4483 STREAM_GET(&p.prefix.macip_addr.mac.octet, pkt, ETH_ALEN);
d62a17ae 4484 } else {
af4c2728 4485 flog_err(
e50f7cfd 4486 EC_BGP_EVPN_ROUTE_INVALID,
d62a17ae 4487 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
4488 peer->bgp->vrf_id, peer->host, macaddr_len);
e121d831 4489 goto fail;
d62a17ae 4490 }
4491
4492
4493 /* Get the IP. */
e121d831
QY
4494 STREAM_GETC(pkt, ipaddr_len);
4495
d62a17ae 4496 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
4497 && ipaddr_len != IPV6_MAX_BITLEN) {
af4c2728 4498 flog_err(
e50f7cfd 4499 EC_BGP_EVPN_ROUTE_INVALID,
d62a17ae 4500 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
4501 peer->bgp->vrf_id, peer->host, ipaddr_len);
e121d831 4502 goto fail;
d62a17ae 4503 }
4504
4505 if (ipaddr_len) {
4506 ipaddr_len /= 8; /* Convert to bytes. */
3714a385 4507 p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
d62a17ae 4508 ? IPADDR_V4
4509 : IPADDR_V6;
e121d831 4510 STREAM_GET(&p.prefix.macip_addr.ip.ip.addr, pkt, ipaddr_len);
d62a17ae 4511 }
d62a17ae 4512
b57ba6d2 4513 /* Get the VNI(s). Stored as bytes here. */
e121d831 4514 STREAM_GET(&label[0], pkt, BGP_LABEL_BYTES);
b57ba6d2 4515 num_labels++;
e121d831 4516
b57ba6d2 4517 /* Do we have a second VNI? */
e121d831 4518 if (STREAM_READABLE(pkt)) {
b57ba6d2 4519 num_labels++;
e121d831 4520 STREAM_GET(&label[1], pkt, BGP_LABEL_BYTES);
b57ba6d2 4521 }
d62a17ae 4522
4523 /* Process the route. */
4524 if (attr)
4525 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4526 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
554cd77a 4527 &prd, &label[0], num_labels, 0, &evpn);
d62a17ae 4528 else
4529 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4530 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
554cd77a 4531 &prd, &label[0], num_labels, &evpn);
e121d831
QY
4532 goto done;
4533
4534fail:
4535stream_failure:
4536 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4537 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
4538 peer->bgp->vrf_id, peer->host);
4539 ret = -1;
4540done:
4541 stream_free(pkt);
d62a17ae 4542 return ret;
128ea8ab 4543}
4544
4545/*
4546 * Process received EVPN type-3 route (advertise or withdraw).
4547 */
d62a17ae 4548static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a
QY
4549 struct attr *attr, uint8_t *pfx, int psize,
4550 uint32_t addpath_id)
d62a17ae 4551{
4552 struct prefix_rd prd;
4553 struct prefix_evpn p;
d7c0a89a 4554 uint8_t ipaddr_len;
554cd77a 4555 uint32_t eth_tag;
d62a17ae 4556 int ret;
4557
4558 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4559 * IP len (1) and IP (4 or 16).
4560 */
4561 if (psize != 17 && psize != 29) {
e50f7cfd 4562 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
1c50c1c0
QY
4563 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4564 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 4565 return -1;
4566 }
4567
7fd077aa 4568 /* If PMSI is present, log if it is anything other than IR.
4569 * Note: We just simply ignore the values as it is not clear if
4570 * doing anything else is better.
4571 */
4572 if (attr &&
4573 (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
2a3f51cf
DS
4574 enum pta_type pmsi_tnl_type = bgp_attr_get_pmsi_tnl_type(attr);
4575
4576 if (pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL
4577 && pmsi_tnl_type != PMSI_TNLTYPE_PIM_SM) {
4578 flog_warn(
4579 EC_BGP_EVPN_PMSI_PRESENT,
4580 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4581 peer->bgp->vrf_id, peer->host, pmsi_tnl_type);
7fd077aa 4582 }
4583 }
4584
d62a17ae 4585 /* Make prefix_rd */
4586 prd.family = AF_UNSPEC;
4587 prd.prefixlen = 64;
4588 memcpy(&prd.val, pfx, 8);
4589 pfx += 8;
4590
4591 /* Make EVPN prefix. */
6006b807 4592 memset(&p, 0, sizeof(p));
b03b8898 4593 p.family = AF_EVPN;
50f74cf1 4594 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 4595 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
4596
554cd77a
VB
4597 /* Copy Ethernet Tag */
4598 memcpy(&eth_tag, pfx, 4);
3714a385 4599 p.prefix.imet_addr.eth_tag = ntohl(eth_tag);
d62a17ae 4600 pfx += 4;
4601
4602 /* Get the IP. */
4603 ipaddr_len = *pfx++;
4604 if (ipaddr_len == IPV4_MAX_BITLEN) {
3714a385 4605 p.prefix.imet_addr.ip.ipa_type = IPADDR_V4;
4606 memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
d62a17ae 4607 } else {
af4c2728 4608 flog_err(
e50f7cfd 4609 EC_BGP_EVPN_ROUTE_INVALID,
d62a17ae 4610 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4611 peer->bgp->vrf_id, peer->host, ipaddr_len);
4612 return -1;
4613 }
4614
4615 /* Process the route. */
4616 if (attr)
4617 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4618 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 4619 &prd, NULL, 0, 0, NULL);
d62a17ae 4620 else
4621 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4622 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 4623 &prd, NULL, 0, NULL);
d62a17ae 4624 return ret;
128ea8ab 4625}
4626
4627/*
4628 * Process received EVPN type-5 route (advertise or withdraw).
4629 */
d62a17ae 4630static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a 4631 struct attr *attr, uint8_t *pfx, int psize,
f007bdce 4632 uint32_t addpath_id)
d62a17ae 4633{
4634 struct prefix_rd prd;
4635 struct prefix_evpn p;
4636 struct bgp_route_evpn evpn;
d7c0a89a
QY
4637 uint8_t ippfx_len;
4638 uint32_t eth_tag;
b57ba6d2 4639 mpls_label_t label; /* holds the VNI as in the packet */
d62a17ae 4640 int ret;
66ff6089 4641 bool is_valid_update = true;
d62a17ae 4642
4643 /* Type-5 route should be 34 or 58 bytes:
4644 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4645 * GW (4 or 16) and VNI (3).
4646 * Note that the IP and GW should both be IPv4 or both IPv6.
4647 */
4648 if (psize != 34 && psize != 58) {
e50f7cfd 4649 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
1c50c1c0
QY
4650 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4651 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 4652 return -1;
4653 }
4654
4655 /* Make prefix_rd */
4656 prd.family = AF_UNSPEC;
4657 prd.prefixlen = 64;
4658 memcpy(&prd.val, pfx, 8);
4659 pfx += 8;
4660
4661 /* Make EVPN prefix. */
6006b807 4662 memset(&p, 0, sizeof(p));
b03b8898 4663 p.family = AF_EVPN;
50f74cf1 4664 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 4665 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
4666
4667 /* Additional information outside of prefix - ESI and GW IP */
4668 memset(&evpn, 0, sizeof(evpn));
4669
66ff6089 4670 /* Fetch ESI overlay index */
c44ab6f1 4671 if (attr)
66ff6089 4672 memcpy(&evpn.eth_s_id, pfx, sizeof(esi_t));
c44ab6f1 4673 pfx += ESI_BYTES;
d62a17ae 4674
4675 /* Fetch Ethernet Tag. */
4676 memcpy(&eth_tag, pfx, 4);
3714a385 4677 p.prefix.prefix_addr.eth_tag = ntohl(eth_tag);
d62a17ae 4678 pfx += 4;
4679
4680 /* Fetch IP prefix length. */
4681 ippfx_len = *pfx++;
4682 if (ippfx_len > IPV6_MAX_BITLEN) {
af4c2728 4683 flog_err(
e50f7cfd 4684 EC_BGP_EVPN_ROUTE_INVALID,
d62a17ae 4685 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4686 peer->bgp->vrf_id, peer->host, ippfx_len);
4687 return -1;
4688 }
3714a385 4689 p.prefix.prefix_addr.ip_prefix_length = ippfx_len;
d62a17ae 4690
4691 /* Determine IPv4 or IPv6 prefix */
4692 /* Since the address and GW are from the same family, this just becomes
4693 * a simple check on the total size.
4694 */
4695 if (psize == 34) {
3714a385 4696 SET_IPADDR_V4(&p.prefix.prefix_addr.ip);
4697 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4);
d62a17ae 4698 pfx += 4;
860e740b
IR
4699 SET_IPADDR_V4(&evpn.gw_ip);
4700 memcpy(&evpn.gw_ip.ipaddr_v4, pfx, 4);
d62a17ae 4701 pfx += 4;
d62a17ae 4702 } else {
3714a385 4703 SET_IPADDR_V6(&p.prefix.prefix_addr.ip);
8643c2e5
DA
4704 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx,
4705 IPV6_MAX_BYTELEN);
4706 pfx += IPV6_MAX_BYTELEN;
860e740b
IR
4707 SET_IPADDR_V6(&evpn.gw_ip);
4708 memcpy(&evpn.gw_ip.ipaddr_v6, pfx, IPV6_MAX_BYTELEN);
8643c2e5 4709 pfx += IPV6_MAX_BYTELEN;
d62a17ae 4710 }
4711
b57ba6d2
MK
4712 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4713 memset(&label, 0, sizeof(label));
4714 memcpy(&label, pfx, BGP_LABEL_BYTES);
6b11bd8d 4715
4716 /*
4717 * If in future, we are required to access additional fields,
996c9314
LB
4718 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4719 * field
6b11bd8d 4720 */
d62a17ae 4721
66ff6089
AD
4722 /*
4723 * An update containing a non-zero gateway IP and a non-zero ESI
4724 * at the same time is should be treated as withdraw
4725 */
860e740b
IR
4726 if (bgp_evpn_is_esi_valid(&evpn.eth_s_id) &&
4727 !ipaddr_is_zero(&evpn.gw_ip)) {
66ff6089
AD
4728 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4729 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4730 peer->host);
4731 is_valid_update = false;
4732 } else if (bgp_evpn_is_esi_valid(&evpn.eth_s_id))
4733 evpn.type = OVERLAY_INDEX_ESI;
860e740b 4734 else if (!ipaddr_is_zero(&evpn.gw_ip))
66ff6089 4735 evpn.type = OVERLAY_INDEX_GATEWAY_IP;
c6ec0c74 4736 if (attr) {
860e740b
IR
4737 if (is_zero_mac(&attr->rmac) &&
4738 !bgp_evpn_is_esi_valid(&evpn.eth_s_id) &&
4739 ipaddr_is_zero(&evpn.gw_ip) && label == 0) {
66ff6089
AD
4740 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4741 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4742 peer->host);
c6ec0c74 4743 is_valid_update = false;
66ff6089 4744 }
c6ec0c74
KA
4745
4746 if (is_mcast_mac(&attr->rmac) || is_bcast_mac(&attr->rmac))
4747 is_valid_update = false;
4748 }
4749
d62a17ae 4750 /* Process the route. */
66ff6089 4751 if (attr && is_valid_update)
d62a17ae 4752 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4753 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 4754 &prd, &label, 1, 0, &evpn);
66ff6089
AD
4755 else {
4756 if (!is_valid_update) {
4757 char attr_str[BUFSIZ] = {0};
4758
4759 bgp_dump_attr(attr, attr_str, BUFSIZ);
4760 zlog_warn(
4761 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4762 peer->hostname, peer->bgp->vrf_id, &p,
4763 attr_str);
4764 }
d62a17ae 4765 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4766 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 4767 &prd, &label, 1, &evpn);
66ff6089 4768 }
d62a17ae 4769
4770 return ret;
4771}
4772
5f040085
DS
4773static void evpn_mpattr_encode_type5(struct stream *s, const struct prefix *p,
4774 const struct prefix_rd *prd,
4775 mpls_label_t *label, uint32_t num_labels,
4776 struct attr *attr)
d62a17ae 4777{
4778 int len;
4779 char temp[16];
5f040085 4780 const struct evpn_addr *p_evpn_p;
d62a17ae 4781
b5605493 4782 memset(&temp, 0, sizeof(temp));
b03b8898 4783 if (p->family != AF_EVPN)
d62a17ae 4784 return;
4785 p_evpn_p = &(p->u.prefix_evpn);
4786
e9fc2840 4787 /* len denites the total len of IP and GW-IP in the route
523cafc4 4788 IP and GW-IP have to be both ipv4 or ipv6
4789 */
3714a385 4790 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
e9fc2840 4791 len = 8; /* IP and GWIP are both ipv4 */
d62a17ae 4792 else
e9fc2840 4793 len = 32; /* IP and GWIP are both ipv6 */
d62a17ae 4794 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4795 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
4796 stream_put(s, prd->val, 8);
6c995628 4797 if (attr && attr->evpn_overlay.type == OVERLAY_INDEX_ESI)
c44ab6f1 4798 stream_put(s, &attr->esi, sizeof(esi_t));
d62a17ae 4799 else
c44ab6f1 4800 stream_put(s, 0, sizeof(esi_t));
3714a385 4801 stream_putl(s, p_evpn_p->prefix_addr.eth_tag);
4802 stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length);
4803 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4804 stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr);
d62a17ae 4805 else
3714a385 4806 stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16);
6c995628 4807 if (attr && attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
6c924775
DS
4808 const struct bgp_route_evpn *evpn_overlay =
4809 bgp_attr_get_evpn_overlay(attr);
4810
3714a385 4811 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
860e740b
IR
4812 stream_put_ipv4(s,
4813 evpn_overlay->gw_ip.ipaddr_v4.s_addr);
d62a17ae 4814 else
860e740b 4815 stream_put(s, &(evpn_overlay->gw_ip.ipaddr_v6), 16);
d62a17ae 4816 } else {
3714a385 4817 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
d62a17ae 4818 stream_put_ipv4(s, 0);
4819 else
4820 stream_put(s, &temp, 16);
4821 }
4822
b57ba6d2 4823 if (num_labels)
d62a17ae 4824 stream_put(s, label, 3);
4825 else
4826 stream_put3(s, 0);
128ea8ab 4827}
4828
4829/*
4830 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4831 */
e3b78da8 4832static void cleanup_vni_on_disable(struct hash_bucket *bucket, struct bgp *bgp)
128ea8ab 4833{
e3b78da8 4834 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
128ea8ab 4835
d62a17ae 4836 /* Remove EVPN routes and schedule for processing. */
4837 delete_routes_for_vni(bgp, vpn);
128ea8ab 4838
d62a17ae 4839 /* Clear "live" flag and see if hash needs to be freed. */
4840 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4841 if (!is_vni_configured(vpn))
4842 bgp_evpn_free(bgp, vpn);
128ea8ab 4843}
4844
4845/*
4846 * Free a VNI entry; iterator function called during cleanup.
4847 */
e3b78da8 4848static void free_vni_entry(struct hash_bucket *bucket, struct bgp *bgp)
128ea8ab 4849{
e3b78da8 4850 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
128ea8ab 4851
d62a17ae 4852 delete_all_vni_routes(bgp, vpn);
4853 bgp_evpn_free(bgp, vpn);
128ea8ab 4854}
4855
c581d8b0
MK
4856/*
4857 * Derive AUTO import RT for BGP VRF - L3VNI
4858 */
4859static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
4860{
5e53dce3 4861 struct bgp *bgp_evpn = NULL;
10ebe1ab 4862
58d8948c 4863 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl, true);
10ebe1ab
MK
4864
4865 /* Map RT to VRF */
5e53dce3 4866 bgp_evpn = bgp_get_evpn();
58d8948c 4867
5e53dce3 4868 if (!bgp_evpn)
10ebe1ab 4869 return;
58d8948c 4870
10ebe1ab 4871 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
c581d8b0
MK
4872}
4873
4874/*
4875 * Delete AUTO import RT from BGP VRF - L3VNI
4876 */
4877static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
4878{
ca337b46
SW
4879 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl,
4880 true);
c581d8b0
MK
4881}
4882
4883/*
4884 * Derive AUTO export RT for BGP VRF - L3VNI
4885 */
4886static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
4887{
58d8948c 4888 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl, true);
c581d8b0
MK
4889}
4890
4891/*
4892 * Delete AUTO export RT from BGP VRF - L3VNI
4893 */
4894static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
4895{
ca337b46
SW
4896 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl,
4897 true);
c581d8b0 4898}
128ea8ab 4899
f1f8b53c
MK
4900static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
4901{
5e53dce3 4902 struct bgp *bgp_evpn = NULL;
f1f8b53c
MK
4903 struct listnode *node = NULL;
4904 struct bgpevpn *vpn = NULL;
4905
5e53dce3
T
4906 bgp_evpn = bgp_get_evpn();
4907 if (!bgp_evpn)
f1f8b53c
MK
4908 return;
4909
4992b4ae
MK
4910 /* update all type-5 routes */
4911 update_advertise_vrf_routes(bgp_vrf);
4912
4913 /* update all type-2 routes */
f1f8b53c 4914 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
5e53dce3 4915 update_routes_for_vni(bgp_evpn, vpn);
f1f8b53c
MK
4916}
4917
bf1061d8
VB
4918/*
4919 * Handle autort change for a given VNI.
4920 */
e3b78da8 4921static void update_autort_vni(struct hash_bucket *bucket, struct bgp *bgp)
bf1061d8 4922{
e3b78da8 4923 struct bgpevpn *vpn = bucket->data;
bf1061d8 4924
bf1061d8
VB
4925 if (!is_import_rt_configured(vpn)) {
4926 if (is_vni_live(vpn))
4927 bgp_evpn_uninstall_routes(bgp, vpn);
4928 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
bf1061d8
VB
4929 list_delete_all_node(vpn->import_rtl);
4930 bgp_evpn_derive_auto_rt_import(bgp, vpn);
4931 if (is_vni_live(vpn))
4932 bgp_evpn_install_routes(bgp, vpn);
4933 }
4934 if (!is_export_rt_configured(vpn)) {
bf1061d8
VB
4935 list_delete_all_node(vpn->export_rtl);
4936 bgp_evpn_derive_auto_rt_export(bgp, vpn);
4937 if (is_vni_live(vpn))
4938 bgp_evpn_handle_export_rt_change(bgp, vpn);
4939 }
4940}
4941
4204021e
PG
4942/*
4943 * Handle autort change for L3VNI.
4944 */
4945static void update_autort_l3vni(struct bgp *bgp)
4946{
4947 if ((CHECK_FLAG(bgp->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
4948 && (CHECK_FLAG(bgp->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)))
4949 return;
4950
4951 if (!CHECK_FLAG(bgp->vrf_flags, BGP_VRF_IMPORT_RT_CFGD)) {
4952 if (is_l3vni_live(bgp))
4953 uninstall_routes_for_vrf(bgp);
4954
4955 /* Cleanup the RT to VRF mapping */
4956 bgp_evpn_unmap_vrf_from_its_rts(bgp);
4957
4958 /* Remove auto generated RT */
4959 evpn_auto_rt_import_delete_for_vrf(bgp);
4960
4961 list_delete_all_node(bgp->vrf_import_rtl);
4962
4963 /* Map auto derive or configured RTs */
4964 evpn_auto_rt_import_add_for_vrf(bgp);
4965 }
4966
4967 if (!CHECK_FLAG(bgp->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
4968 list_delete_all_node(bgp->vrf_export_rtl);
4969
4970 evpn_auto_rt_export_delete_for_vrf(bgp);
4971
4972 evpn_auto_rt_export_add_for_vrf(bgp);
4973
4974 if (is_l3vni_live(bgp))
4975 bgp_evpn_map_vrf_to_its_rts(bgp);
4976 }
4977
4978 if (!is_l3vni_live(bgp))
4979 return;
4980
4981 /* advertise type-5 routes if needed */
4982 update_advertise_vrf_routes(bgp);
4983
4984 /* install all remote routes belonging to this l3vni
4985 * into corresponding vrf
4986 */
4987 install_routes_for_vrf(bgp);
4988}
4989
128ea8ab 4990/*
4991 * Public functions.
4992 */
4993
5424b7ba 4994/* withdraw type-5 route corresponding to ip prefix */
bd494ec5 4995void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
5424b7ba
MK
4996 afi_t afi, safi_t safi)
4997{
4998 int ret = 0;
4999 struct prefix_evpn evp;
5424b7ba 5000
31310b25 5001 build_type5_prefix_from_ip_prefix(&evp, p);
5424b7ba 5002 ret = delete_evpn_type5_route(bgp_vrf, &evp);
2dbe669b 5003 if (ret)
af4c2728 5004 flog_err(
e50f7cfd 5005 EC_BGP_EVPN_ROUTE_DELETE,
2dbe669b
DA
5006 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
5007 bgp_vrf->vrf_id, p, vrf_id_to_name(bgp_vrf->vrf_id));
5424b7ba
MK
5008}
5009
342dd0c6 5010/* withdraw all type-5 routes for an address family */
996c9314 5011void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
342dd0c6 5012{
5013 struct bgp_table *table = NULL;
9bcb3eef 5014 struct bgp_dest *dest = NULL;
40381db7 5015 struct bgp_path_info *pi;
342dd0c6 5016
053905d2 5017 table = bgp_vrf->rib[afi][safi];
9bcb3eef 5018 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
f106e3a7 5019 /* Only care about "selected" routes. Also ensure that
5020 * these are routes that are injectable into EVPN.
5021 */
25f2ca53 5022 /* TODO: Support for AddPath for EVPN. */
9bcb3eef 5023 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
40381db7 5024 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
f106e3a7 5025 && is_route_injectable_into_evpn(pi)) {
b54892e0 5026 bgp_evpn_withdraw_type5_route(
9bcb3eef 5027 bgp_vrf, bgp_dest_get_prefix(dest), afi,
b54892e0 5028 safi);
25f2ca53 5029 break;
5030 }
5031 }
5032 }
5424b7ba 5033}
342dd0c6 5034
5fd9c12b
KA
5035/*
5036 * evpn - enable advertisement of default g/w
5037 */
5038void bgp_evpn_install_uninstall_default_route(struct bgp *bgp_vrf, afi_t afi,
5039 safi_t safi, bool add)
5040{
5041 struct prefix ip_prefix;
5042
5043 /* form the default prefix 0.0.0.0/0 */
6006b807 5044 memset(&ip_prefix, 0, sizeof(ip_prefix));
5fd9c12b
KA
5045 ip_prefix.family = afi2family(afi);
5046
5047 if (add) {
5048 bgp_evpn_advertise_type5_route(bgp_vrf, &ip_prefix,
5049 NULL, afi, safi);
5050 } else {
5051 bgp_evpn_withdraw_type5_route(bgp_vrf, &ip_prefix,
5052 afi, safi);
5053 }
5054}
5055
5056
2f69f6d3 5057/*
5058 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
5059 * to this function correspond to those of the source IP prefix (best
5060 * path in the case of the attr. In the case of a local prefix (when we
5061 * are advertising local subnets), the src_attr will be NULL.
5062 */
bd494ec5 5063void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
996c9314
LB
5064 struct attr *src_attr, afi_t afi,
5065 safi_t safi)
5424b7ba
MK
5066{
5067 int ret = 0;
5068 struct prefix_evpn evp;
7c82b312 5069
31310b25 5070 build_type5_prefix_from_ip_prefix(&evp, p);
6c995628 5071 ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr, afi, safi);
2f69f6d3 5072 if (ret)
e50f7cfd 5073 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
2dbe669b
DA
5074 "%u: Failed to create type-5 route for prefix %pFX",
5075 bgp_vrf->vrf_id, p);
342dd0c6 5076}
5077
2f69f6d3 5078/* Inject all prefixes of a particular address-family (currently, IPv4 or
5079 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
5080 * advertisement is enabled.
5081 */
996c9314
LB
5082void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
5083 safi_t safi)
342dd0c6 5084{
5085 struct bgp_table *table = NULL;
9bcb3eef 5086 struct bgp_dest *dest = NULL;
40381db7 5087 struct bgp_path_info *pi;
342dd0c6 5088
053905d2 5089 table = bgp_vrf->rib[afi][safi];
9bcb3eef 5090 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
2f69f6d3 5091 /* Need to identify the "selected" route entry to use its
f106e3a7 5092 * attribute. Also, ensure that the route is injectable
5093 * into EVPN.
2f69f6d3 5094 * TODO: Support for AddPath for EVPN.
5095 */
9bcb3eef 5096 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
40381db7 5097 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
f106e3a7 5098 && is_route_injectable_into_evpn(pi)) {
53c84f78
MK
5099
5100 /* apply the route-map */
5101 if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
b68885f9 5102 route_map_result_t ret;
e34291b8 5103 struct bgp_path_info tmp_pi;
5104 struct bgp_path_info_extra tmp_pie;
5105 struct attr tmp_attr;
5106
5107 tmp_attr = *pi->attr;
5108
5109 /* Fill temp path_info */
9bcb3eef
DS
5110 prep_for_rmap_apply(&tmp_pi, &tmp_pie,
5111 dest, pi, pi->peer,
5112 &tmp_attr);
e34291b8 5113
5114 RESET_FLAG(tmp_attr.rmap_change_flags);
53c84f78 5115
996c9314
LB
5116 ret = route_map_apply(
5117 bgp_vrf->adv_cmd_rmap[afi][safi]
5118 .map,
9bcb3eef 5119 bgp_dest_get_prefix(dest),
1782514f 5120 &tmp_pi);
e34291b8 5121 if (ret == RMAP_DENYMATCH) {
5122 bgp_attr_flush(&tmp_attr);
53c84f78 5123 continue;
e34291b8 5124 }
5125 bgp_evpn_advertise_type5_route(
b54892e0 5126 bgp_vrf,
9bcb3eef 5127 bgp_dest_get_prefix(dest),
b54892e0 5128 &tmp_attr, afi, safi);
e34291b8 5129 } else
5130 bgp_evpn_advertise_type5_route(
b54892e0 5131 bgp_vrf,
9bcb3eef 5132 bgp_dest_get_prefix(dest),
b54892e0 5133 pi->attr, afi, safi);
2f69f6d3 5134 break;
5135 }
5136 }
31310b25 5137 }
342dd0c6 5138}
5139
ca337b46
SW
5140static void rt_list_remove_node(struct list *rt_list,
5141 struct ecommunity *ecomdel, bool is_l3)
c581d8b0 5142{
ca337b46
SW
5143 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
5144 struct vrf_route_target *l3rt = NULL;
5145 struct ecommunity *ecom = NULL;
5146
ca337b46
SW
5147 if (is_l3) {
5148 for (ALL_LIST_ELEMENTS(rt_list, node, nnode, l3rt)) {
5149 if (ecommunity_match(l3rt->ecom, ecomdel)) {
5150 evpn_vrf_rt_del(l3rt);
5151 node_to_del = node;
5152 break;
5153 }
5154 }
5155 } else {
5156 for (ALL_LIST_ELEMENTS(rt_list, node, nnode, ecom)) {
5157 if (ecommunity_match(ecom, ecomdel)) {
5158 ecommunity_free(&ecom);
5159 node_to_del = node;
5160 break;
5161 }
5162 }
5163 }
5164
58d8948c 5165
ca337b46
SW
5166 if (node_to_del)
5167 list_delete_node(rt_list, node_to_del);
5168}
5169
5170void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl,
5171 bool is_l3)
5172{
5173 struct ecommunity *ecom_auto;
c581d8b0
MK
5174 struct ecommunity_val eval;
5175
bf1061d8
VB
5176 if (bgp->advertise_autort_rfc8365)
5177 vni |= EVPN_AUTORT_VXLAN;
58d8948c 5178
c581d8b0
MK
5179 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
5180
5181 ecom_auto = ecommunity_new();
1207a5bc 5182 ecommunity_add_val(ecom_auto, &eval, false, false);
c581d8b0 5183
ca337b46 5184 rt_list_remove_node(rtl, ecom_auto, is_l3);
c581d8b0
MK
5185
5186 ecommunity_free(&ecom_auto);
5187}
5188
58d8948c 5189static void evpn_vrf_rt_routes_map(struct bgp *bgp_vrf)
c581d8b0 5190{
58d8948c
SW
5191 /* map VRFs to its RTs and install routes matching this new RT */
5192 if (is_l3vni_live(bgp_vrf)) {
5193 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
5194 install_routes_for_vrf(bgp_vrf);
5195 }
5196}
ca337b46 5197
58d8948c 5198static void evpn_vrf_rt_routes_unmap(struct bgp *bgp_vrf)
c581d8b0 5199{
5ba238b7 5200 /* uninstall routes from vrf */
3d0b43d7 5201 if (is_l3vni_live(bgp_vrf))
5202 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
5203
5204 /* Cleanup the RT to VRF mapping */
5205 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
58d8948c 5206}
10ebe1ab 5207
58d8948c
SW
5208static bool rt_list_has_cfgd_rt(struct list *rt_list)
5209{
5210 struct listnode *node = NULL, *nnode = NULL;
5211 struct vrf_route_target *l3rt = NULL;
5212
5213 for (ALL_LIST_ELEMENTS(rt_list, node, nnode, l3rt)) {
5214 if (!CHECK_FLAG(l3rt->flags, BGP_VRF_RT_AUTO))
5215 return true;
5216 }
5217
5218 return false;
5219}
5220
5221static void unconfigure_import_rt_for_vrf_fini(struct bgp *bgp_vrf)
5222{
5223 if (!bgp_vrf->vrf_import_rtl)
5224 return; /* this should never fail */
5225
5226 if (!is_l3vni_live(bgp_vrf))
5227 return; /* Nothing to do if no vni */
5228
5229 /* fall back to auto-generated RT if this was the last RT */
5230 if (list_isempty(bgp_vrf->vrf_import_rtl))
5231 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
5232}
5233
5234static void unconfigure_export_rt_for_vrf_fini(struct bgp *bgp_vrf)
5235{
5236
5237 if (!bgp_vrf->vrf_export_rtl)
5238 return; /* this should never fail */
5239
5240 if (!is_l3vni_live(bgp_vrf))
5241 return; /* Nothing to do if no vni */
5242
5243 /* fall back to auto-generated RT if this was the last RT */
5244 if (list_isempty(bgp_vrf->vrf_export_rtl))
5245 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
5246
5247 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
5248}
5249
5250void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
5251 struct ecommunity *ecomadd,
5252 bool is_wildcard)
5253{
5254 struct vrf_route_target *newrt;
5255
5256 newrt = evpn_vrf_rt_new(ecomadd);
5257
5258 if (is_wildcard)
5259 SET_FLAG(newrt->flags, BGP_VRF_RT_WILD);
5260
5261 evpn_vrf_rt_routes_unmap(bgp_vrf);
5262
5263 /* Remove auto generated RT if not configured */
5264 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD))
5265 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
c581d8b0
MK
5266
5267 /* Add the newly configured RT to RT list */
ca337b46 5268 listnode_add_sort(bgp_vrf->vrf_import_rtl, newrt);
58d8948c 5269
c581d8b0
MK
5270 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
5271
58d8948c
SW
5272 evpn_vrf_rt_routes_map(bgp_vrf);
5273}
5274
5275void bgp_evpn_configure_import_auto_rt_for_vrf(struct bgp *bgp_vrf)
5276{
5277 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD))
5278 return; /* Already configured */
5279
5280 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD);
5281
5282 if (!is_l3vni_live(bgp_vrf))
5283 return; /* Wait for VNI before adding rts */
5284
5285 evpn_vrf_rt_routes_unmap(bgp_vrf);
5286
5287 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
5288
5289 evpn_vrf_rt_routes_map(bgp_vrf);
c581d8b0
MK
5290}
5291
5292void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
5293 struct ecommunity *ecomdel)
5294{
58d8948c
SW
5295 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5296 return; /* Already un-configured */
c581d8b0 5297
58d8948c 5298 evpn_vrf_rt_routes_unmap(bgp_vrf);
10ebe1ab 5299
ca337b46
SW
5300 /* Remove rt */
5301 rt_list_remove_node(bgp_vrf->vrf_import_rtl, ecomdel, true);
10ebe1ab 5302
58d8948c 5303 if (!rt_list_has_cfgd_rt(bgp_vrf->vrf_import_rtl))
c581d8b0 5304 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
c581d8b0 5305
58d8948c 5306 unconfigure_import_rt_for_vrf_fini(bgp_vrf);
c581d8b0 5307
58d8948c
SW
5308 evpn_vrf_rt_routes_map(bgp_vrf);
5309}
c581d8b0 5310
58d8948c
SW
5311void bgp_evpn_unconfigure_import_auto_rt_for_vrf(struct bgp *bgp_vrf)
5312{
5313 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD))
5314 return; /* Already un-configured */
5315
5316 evpn_vrf_rt_routes_unmap(bgp_vrf);
5317
5318 /* remove auto-generated RT */
5319 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
5320
5321 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD);
5322
5323 unconfigure_import_rt_for_vrf_fini(bgp_vrf);
5324
5325 evpn_vrf_rt_routes_map(bgp_vrf);
c581d8b0
MK
5326}
5327
5328void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
5329 struct ecommunity *ecomadd)
5330{
ca337b46
SW
5331 struct vrf_route_target *newrt;
5332
5333 newrt = evpn_vrf_rt_new(ecomadd);
5334
58d8948c
SW
5335 /* Remove auto generated RT if not configured */
5336 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD))
5337 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
c581d8b0
MK
5338
5339 /* Add the new RT to the RT list */
ca337b46 5340 listnode_add_sort(bgp_vrf->vrf_export_rtl, newrt);
58d8948c 5341
c581d8b0
MK
5342 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
5343
ebdc9e64
AD
5344 if (is_l3vni_live(bgp_vrf))
5345 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
5346}
5347
58d8948c
SW
5348void bgp_evpn_configure_export_auto_rt_for_vrf(struct bgp *bgp_vrf)
5349{
5350 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD))
5351 return; /* Already configured */
5352
5353 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD);
5354
5355 if (!is_l3vni_live(bgp_vrf))
5356 return; /* Wait for VNI before adding rts */
5357
5358 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
5359
5360 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
5361}
5362
c581d8b0
MK
5363void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
5364 struct ecommunity *ecomdel)
5365{
58d8948c
SW
5366 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
5367 return; /* Already un-configured */
c581d8b0 5368
ca337b46
SW
5369 /* Remove rt */
5370 rt_list_remove_node(bgp_vrf->vrf_export_rtl, ecomdel, true);
c581d8b0 5371
58d8948c 5372 if (!rt_list_has_cfgd_rt(bgp_vrf->vrf_export_rtl))
c581d8b0 5373 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
c581d8b0 5374
58d8948c
SW
5375 unconfigure_export_rt_for_vrf_fini(bgp_vrf);
5376}
1525e99f 5377
58d8948c
SW
5378void bgp_evpn_unconfigure_export_auto_rt_for_vrf(struct bgp *bgp_vrf)
5379{
5380 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD))
5381 return; /* Already un-configured */
c581d8b0 5382
58d8948c
SW
5383 /* remove auto-generated RT */
5384 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
5385
5386 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD);
5387
5388 unconfigure_export_rt_for_vrf_fini(bgp_vrf);
c581d8b0
MK
5389}
5390
2d48ee25 5391/*
5392 * Handle change to BGP router id. This is invoked twice by the change
5393 * handler, first before the router id has been changed and then after
5394 * the router id has been changed. The first invocation will result in
676f83b9 5395 * local routes for all VNIs/VRF being deleted and withdrawn and the next
2d48ee25 5396 * will result in the routes being re-advertised.
5397 */
d62a17ae 5398void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
2d48ee25 5399{
5394a276
CS
5400 struct listnode *node;
5401 struct bgp *bgp_vrf;
5402
676f83b9 5403 if (withdraw) {
5404
5405 /* delete and withdraw all the type-5 routes
523cafc4 5406 stored in the global table for this vrf
5407 */
80b140af 5408 withdraw_router_id_vrf(bgp);
676f83b9 5409
5410 /* delete all the VNI routes (type-2/type-3) routes for all the
523cafc4 5411 * L2-VNIs
5412 */
d62a17ae 5413 hash_iterate(bgp->vnihash,
e3b78da8 5414 (void (*)(struct hash_bucket *,
d62a17ae 5415 void *))withdraw_router_id_vni,
5416 bgp);
5394a276
CS
5417
5418 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
5419 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
5420 if (bgp_vrf->evpn_info->advertise_pip &&
5421 (bgp_vrf->evpn_info->pip_ip_static.s_addr
5422 == INADDR_ANY))
5423 bgp_vrf->evpn_info->pip_ip.s_addr
5424 = INADDR_ANY;
5425 }
5426 }
676f83b9 5427 } else {
5428
5394a276
CS
5429 /* Assign new default instance router-id */
5430 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
5431 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
5432 if (bgp_vrf->evpn_info->advertise_pip &&
5433 (bgp_vrf->evpn_info->pip_ip_static.s_addr
5434 == INADDR_ANY)) {
5435 bgp_vrf->evpn_info->pip_ip =
5436 bgp->router_id;
5437 /* advertise type-5 routes with
5438 * new nexthop
5439 */
5440 update_advertise_vrf_routes(bgp_vrf);
5441 }
5442 }
5443 }
5444
676f83b9 5445 /* advertise all routes in the vrf as type-5 routes with the new
523cafc4 5446 * RD
5447 */
80b140af 5448 update_router_id_vrf(bgp);
676f83b9 5449
5450 /* advertise all the VNI routes (type-2/type-3) routes with the
523cafc4 5451 * new RD
5452 */
d62a17ae 5453 hash_iterate(bgp->vnihash,
e3b78da8 5454 (void (*)(struct hash_bucket *,
d62a17ae 5455 void *))update_router_id_vni,
5456 bgp);
676f83b9 5457 }
2d48ee25 5458}
5459
bf1061d8
VB
5460/*
5461 * Handle change to auto-RT algorithm - update and advertise local routes.
5462 */
5463void bgp_evpn_handle_autort_change(struct bgp *bgp)
5464{
5465 hash_iterate(bgp->vnihash,
e3b78da8 5466 (void (*)(struct hash_bucket *,
bf1061d8
VB
5467 void*))update_autort_vni,
5468 bgp);
4204021e
PG
5469 if (bgp->l3vni)
5470 update_autort_l3vni(bgp);
bf1061d8
VB
5471}
5472
90e60aa7 5473/*
5474 * Handle change to export RT - update and advertise local routes.
5475 */
d62a17ae 5476int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 5477{
d62a17ae 5478 return update_routes_for_vni(bgp, vpn);
90e60aa7 5479}
5480
996c9314 5481void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
676f83b9 5482{
5483 if (withdraw)
5484 delete_withdraw_vrf_routes(bgp_vrf);
5485 else
5486 update_advertise_vrf_routes(bgp_vrf);
5487}
5488
90e60aa7 5489/*
5490 * Handle change to RD. This is invoked twice by the change handler,
5491 * first before the RD has been changed and then after the RD has
5492 * been changed. The first invocation will result in local routes
5493 * of this VNI being deleted and withdrawn and the next will result
5494 * in the routes being re-advertised.
5495 */
d62a17ae 5496void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
5497 int withdraw)
90e60aa7 5498{
d62a17ae 5499 if (withdraw)
5500 delete_withdraw_vni_routes(bgp, vpn);
5501 else
5502 update_advertise_vni_routes(bgp, vpn);
90e60aa7 5503}
5504
5505/*
5506 * Install routes for this VNI. Invoked upon change to Import RT.
5507 */
d62a17ae 5508int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 5509{
d62a17ae 5510 return install_routes_for_vni(bgp, vpn);
90e60aa7 5511}
5512
5513/*
5514 * Uninstall all routes installed for this VNI. Invoked upon change
5515 * to Import RT.
5516 */
d62a17ae 5517int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 5518{
d62a17ae 5519 return uninstall_routes_for_vni(bgp, vpn);
90e60aa7 5520}
5521
b16031a2 5522/*
b57ba6d2 5523 * TODO: Hardcoded for a maximum of 2 VNIs right now
b16031a2 5524 */
d7c0a89a 5525char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf,
996c9314 5526 int len)
b16031a2 5527{
b57ba6d2 5528 vni_t vni1, vni2;
b16031a2 5529
b57ba6d2
MK
5530 vni1 = label2vni(label);
5531 if (num_labels == 2) {
996c9314 5532 vni2 = label2vni(label + 1);
b57ba6d2
MK
5533 snprintf(buf, len, "%u/%u", vni1, vni2);
5534 } else
5535 snprintf(buf, len, "%u", vni1);
d62a17ae 5536 return buf;
b16031a2 5537}
5538
9c92b5f7
MK
5539/*
5540 * Function to convert evpn route to json format.
5541 * NOTE: We don't use prefix2str as the output here is a bit different.
5542 */
bd494ec5 5543void bgp_evpn_route2json(const struct prefix_evpn *p, json_object *json)
9c92b5f7 5544{
b682f6de 5545 char buf1[ETHER_ADDR_STRLEN];
5546 char buf2[PREFIX2STR_BUFFER];
44c69747
LK
5547 uint8_t family;
5548 uint8_t prefixlen;
9c92b5f7 5549
b682f6de 5550 if (!json)
5551 return;
9c92b5f7 5552
44c69747
LK
5553 json_object_int_add(json, "routeType", p->prefix.route_type);
5554
5555 switch (p->prefix.route_type) {
5556 case BGP_EVPN_MAC_IP_ROUTE:
3714a385 5557 json_object_int_add(json, "ethTag",
44c69747
LK
5558 p->prefix.macip_addr.eth_tag);
5559 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
5560 json_object_string_add(json, "mac",
5561 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
5562 sizeof(buf1)));
5563
5564 if (!is_evpn_prefix_ipaddr_none(p)) {
5565 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET :
5566 AF_INET6;
5567 prefixlen = (family == AF_INET) ?
5568 IPV4_MAX_BITLEN : IPV6_MAX_BITLEN;
5569 inet_ntop(family, &p->prefix.macip_addr.ip.ip.addr,
5570 buf2, PREFIX2STR_BUFFER);
5571 json_object_int_add(json, "ipLen", prefixlen);
5572 json_object_string_add(json, "ip", buf2);
5573 }
5574 break;
dff8f48d 5575
44c69747
LK
5576 case BGP_EVPN_IMET_ROUTE:
5577 json_object_int_add(json, "ethTag",
5578 p->prefix.imet_addr.eth_tag);
5579 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
5580 prefixlen = (family == AF_INET) ? IPV4_MAX_BITLEN :
5581 IPV6_MAX_BITLEN;
5582 inet_ntop(family, &p->prefix.imet_addr.ip.ip.addr, buf2,
5583 PREFIX2STR_BUFFER);
5584 json_object_int_add(json, "ipLen", prefixlen);
5585 json_object_string_add(json, "ip", buf2);
5586 break;
dff8f48d 5587
44c69747
LK
5588 case BGP_EVPN_IP_PREFIX_ROUTE:
5589 json_object_int_add(json, "ethTag",
5590 p->prefix.prefix_addr.eth_tag);
5591 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
5592 inet_ntop(family, &p->prefix.prefix_addr.ip.ip.addr,
5593 buf2, sizeof(buf2));
5594 json_object_int_add(json, "ipLen",
5595 p->prefix.prefix_addr.ip_prefix_length);
5596 json_object_string_add(json, "ip", buf2);
5597 break;
5598
5599 default:
5600 break;
dff8f48d 5601 }
9c92b5f7
MK
5602}
5603
128ea8ab 5604/*
5605 * Encode EVPN prefix in Update (MP_REACH)
5606 */
5f040085
DS
5607void bgp_evpn_encode_prefix(struct stream *s, const struct prefix *p,
5608 const struct prefix_rd *prd, mpls_label_t *label,
d7c0a89a 5609 uint32_t num_labels, struct attr *attr,
be92fc9f 5610 bool addpath_capable, uint32_t addpath_tx_id)
d62a17ae 5611{
5612 struct prefix_evpn *evp = (struct prefix_evpn *)p;
b57ba6d2 5613 int len, ipa_len = 0;
d62a17ae 5614
be92fc9f 5615 if (addpath_capable)
d62a17ae 5616 stream_putl(s, addpath_tx_id);
5617
5618 /* Route type */
5619 stream_putc(s, evp->prefix.route_type);
5620
5621 switch (evp->prefix.route_type) {
5622 case BGP_EVPN_MAC_IP_ROUTE:
3714a385 5623 if (is_evpn_prefix_ipaddr_v4(evp))
d62a17ae 5624 ipa_len = IPV4_MAX_BYTELEN;
3714a385 5625 else if (is_evpn_prefix_ipaddr_v6(evp))
d62a17ae 5626 ipa_len = IPV6_MAX_BYTELEN;
b57ba6d2
MK
5627 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
5628 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
5629 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
5630 len += 3;
5631 stream_putc(s, len);
996c9314 5632 stream_put(s, prd->val, 8); /* RD */
554cd77a 5633 if (attr)
c44ab6f1 5634 stream_put(s, &attr->esi, ESI_BYTES);
554cd77a
VB
5635 else
5636 stream_put(s, 0, 10);
3714a385 5637 stream_putl(s, evp->prefix.macip_addr.eth_tag); /* Ethernet Tag ID */
28328ea9 5638 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
3714a385 5639 stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */
5640 stream_putc(s, 8 * ipa_len); /* IP address Length */
5641 if (ipa_len) /* IP */
5642 stream_put(s, &evp->prefix.macip_addr.ip.ip.addr,
5643 ipa_len);
b57ba6d2
MK
5644 /* 1st label is the L2 VNI */
5645 stream_put(s, label, BGP_LABEL_BYTES);
5646 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5647 if (ipa_len && num_labels > 1)
996c9314 5648 stream_put(s, label + 1, BGP_LABEL_BYTES);
d62a17ae 5649 break;
5650
5651 case BGP_EVPN_IMET_ROUTE:
5652 stream_putc(s, 17); // TODO: length - assumes IPv4 address
5653 stream_put(s, prd->val, 8); /* RD */
3714a385 5654 stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */
d62a17ae 5655 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
5656 /* Originating Router's IP Addr */
3714a385 5657 stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4);
d62a17ae 5658 break;
5659
50f74cf1 5660 case BGP_EVPN_ES_ROUTE:
5661 stream_putc(s, 23); /* TODO: length: assumes ipv4 VTEP */
5662 stream_put(s, prd->val, 8); /* RD */
5663 stream_put(s, evp->prefix.es_addr.esi.val, 10); /* ESI */
5664 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
2bb9eff4
DS
5665 /* VTEP IP */
5666 stream_put_in_addr(s, &evp->prefix.es_addr.ip.ipaddr_v4);
50f74cf1 5667 break;
5668
c44ab6f1
AK
5669 case BGP_EVPN_AD_ROUTE:
5670 /* RD, ESI, EthTag, 1 VNI */
5671 len = RD_BYTES + ESI_BYTES + EVPN_ETH_TAG_BYTES + BGP_LABEL_BYTES;
5672 stream_putc(s, len);
5673 stream_put(s, prd->val, RD_BYTES); /* RD */
5674 stream_put(s, evp->prefix.ead_addr.esi.val, ESI_BYTES); /* ESI */
5675 stream_putl(s, evp->prefix.ead_addr.eth_tag); /* Ethernet Tag */
5676 stream_put(s, label, BGP_LABEL_BYTES);
5677 break;
5678
d62a17ae 5679 case BGP_EVPN_IP_PREFIX_ROUTE:
5680 /* TODO: AddPath support. */
b57ba6d2 5681 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
d62a17ae 5682 break;
5683
5684 default:
5685 break;
5686 }
5687}
5688
5689int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
5690 struct bgp_nlri *packet, int withdraw)
5691{
d7c0a89a
QY
5692 uint8_t *pnt;
5693 uint8_t *lim;
d62a17ae 5694 afi_t afi;
5695 safi_t safi;
d7c0a89a 5696 uint32_t addpath_id;
be92fc9f 5697 bool addpath_capable;
d62a17ae 5698 int psize = 0;
d7c0a89a 5699 uint8_t rtype;
d62a17ae 5700 struct prefix p;
5701
d62a17ae 5702 /* Start processing the NLRI - there may be multiple in the MP_REACH */
5703 pnt = packet->nlri;
5704 lim = pnt + packet->length;
5705 afi = packet->afi;
5706 safi = packet->safi;
5707 addpath_id = 0;
5708
be92fc9f 5709 addpath_capable = bgp_addpath_encode_rx(peer, afi, safi);
d62a17ae 5710
5711 for (; pnt < lim; pnt += psize) {
5712 /* Clear prefix structure. */
6006b807 5713 memset(&p, 0, sizeof(p));
d62a17ae 5714
5715 /* Deal with path-id if AddPath is supported. */
be92fc9f 5716 if (addpath_capable) {
d62a17ae 5717 /* When packet overflow occurs return immediately. */
5718 if (pnt + BGP_ADDPATH_ID_LEN > lim)
513386b5 5719 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
d62a17ae 5720
a3a850a1
S
5721 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
5722 addpath_id = ntohl(addpath_id);
d62a17ae 5723 pnt += BGP_ADDPATH_ID_LEN;
5724 }
5725
5726 /* All EVPN NLRI types start with type and length. */
5727 if (pnt + 2 > lim)
513386b5 5728 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE;
d62a17ae 5729
5730 rtype = *pnt++;
3f54c705 5731 psize = *pnt++;
d62a17ae 5732
5733 /* When packet overflow occur return immediately. */
5734 if (pnt + psize > lim)
513386b5 5735 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
d62a17ae 5736
5737 switch (rtype) {
5738 case BGP_EVPN_MAC_IP_ROUTE:
5739 if (process_type2_route(peer, afi, safi,
5740 withdraw ? NULL : attr, pnt,
5741 psize, addpath_id)) {
af4c2728 5742 flog_err(
e50f7cfd 5743 EC_BGP_EVPN_FAIL,
d62a17ae 5744 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5745 peer->bgp->vrf_id, peer->host, psize);
513386b5 5746 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE;
d62a17ae 5747 }
5748 break;
5749
5750 case BGP_EVPN_IMET_ROUTE:
5751 if (process_type3_route(peer, afi, safi,
5752 withdraw ? NULL : attr, pnt,
5753 psize, addpath_id)) {
af4c2728 5754 flog_err(
e50f7cfd 5755 EC_BGP_PKT_PROCESS,
d62a17ae 5756 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5757 peer->bgp->vrf_id, peer->host, psize);
513386b5 5758 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE;
d62a17ae 5759 }
5760 break;
5761
50f74cf1 5762 case BGP_EVPN_ES_ROUTE:
c44ab6f1 5763 if (bgp_evpn_type4_route_process(peer, afi, safi,
50f74cf1 5764 withdraw ? NULL : attr, pnt,
5765 psize, addpath_id)) {
af4c2728 5766 flog_err(
e50f7cfd 5767 EC_BGP_PKT_PROCESS,
50f74cf1 5768 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5769 peer->bgp->vrf_id, peer->host, psize);
513386b5 5770 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE;
50f74cf1 5771 }
5772 break;
5773
c44ab6f1
AK
5774 case BGP_EVPN_AD_ROUTE:
5775 if (bgp_evpn_type1_route_process(peer, afi, safi,
5776 withdraw ? NULL : attr, pnt,
5777 psize, addpath_id)) {
5778 flog_err(
5779 EC_BGP_PKT_PROCESS,
5780 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5781 peer->bgp->vrf_id, peer->host, psize);
5782 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE;
5783 }
5784 break;
5785
d62a17ae 5786 case BGP_EVPN_IP_PREFIX_ROUTE:
f007bdce
CS
5787 if (process_type5_route(peer, afi, safi,
5788 withdraw ? NULL : attr, pnt,
5789 psize, addpath_id)) {
af4c2728 5790 flog_err(
e50f7cfd 5791 EC_BGP_PKT_PROCESS,
d62a17ae 5792 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5793 peer->bgp->vrf_id, peer->host, psize);
513386b5 5794 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE;
d62a17ae 5795 }
5796 break;
5797
5798 default:
5799 break;
5800 }
5801 }
5802
5803 /* Packet length consistency check. */
5804 if (pnt != lim)
513386b5 5805 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
d62a17ae 5806
513386b5 5807 return BGP_NLRI_PARSE_OK;
128ea8ab 5808}
5809
10ebe1ab
MK
5810/*
5811 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5812 * The mapping will be used during route processing.
10ebe1ab
MK
5813 * bgp_vrf: specific bgp vrf instance on which RT is configured
5814 */
5815void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
5816{
58d8948c 5817 struct listnode *node, *nnode;
ca337b46 5818 struct vrf_route_target *l3rt;
10ebe1ab 5819
58d8948c
SW
5820 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, l3rt))
5821 map_vrf_to_rt(bgp_vrf, l3rt);
10ebe1ab
MK
5822}
5823
5824/*
5825 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5826 */
5827void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
5828{
10ebe1ab 5829 struct listnode *node, *nnode;
ca337b46 5830 struct vrf_route_target *l3rt;
10ebe1ab 5831
58d8948c
SW
5832 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, l3rt))
5833 unmap_vrf_from_rt(bgp_vrf, l3rt);
10ebe1ab
MK
5834}
5835
128ea8ab 5836/*
5837 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5838 * The mapping will be used during route processing.
5839 */
d62a17ae 5840void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5841{
f6e07e1b 5842 uint32_t i;
d62a17ae 5843 struct ecommunity_val *eval;
5844 struct listnode *node, *nnode;
5845 struct ecommunity *ecom;
128ea8ab 5846
d62a17ae 5847 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5848 for (i = 0; i < ecom->size; i++) {
5849 eval = (struct ecommunity_val *)(ecom->val
5850 + (i
5851 * ECOMMUNITY_SIZE));
5852 map_vni_to_rt(bgp, vpn, eval);
5853 }
5854 }
128ea8ab 5855}
5856
5857/*
5858 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5859 */
d62a17ae 5860void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5861{
f6e07e1b 5862 uint32_t i;
d62a17ae 5863 struct ecommunity_val *eval;
5864 struct listnode *node, *nnode;
5865 struct ecommunity *ecom;
128ea8ab 5866
d62a17ae 5867 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5868 for (i = 0; i < ecom->size; i++) {
5869 struct irt_node *irt;
5870 struct ecommunity_val eval_tmp;
128ea8ab 5871
d62a17ae 5872 eval = (struct ecommunity_val *)(ecom->val
5873 + (i
5874 * ECOMMUNITY_SIZE));
5875 /* If using "automatic" RT, we only care about the
5876 * local-admin sub-field.
5877 * This is to facilitate using VNI as the RT for EBGP
5878 * peering too.
5879 */
5880 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
5881 if (!is_import_rt_configured(vpn))
5882 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 5883
d62a17ae 5884 irt = lookup_import_rt(bgp, &eval_tmp);
5885 if (irt)
5886 unmap_vni_from_rt(bgp, vpn, irt);
5887 }
5888 }
128ea8ab 5889}
5890
5891/*
5892 * Derive Import RT automatically for VNI and map VNI to RT.
5893 * The mapping will be used during route processing.
5894 */
d62a17ae 5895void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5896{
58d8948c 5897 form_auto_rt(bgp, vpn->vni, vpn->import_rtl, false);
d62a17ae 5898 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
128ea8ab 5899
d62a17ae 5900 /* Map RT to VNI */
5901 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
128ea8ab 5902}
5903
5904/*
5905 * Derive Export RT automatically for VNI.
5906 */
d62a17ae 5907void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5908{
58d8948c 5909 form_auto_rt(bgp, vpn->vni, vpn->export_rtl, false);
d62a17ae 5910 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
128ea8ab 5911}
5912
676f83b9 5913/*
5914 * Derive RD automatically for VNI using passed information - it
5915 * is of the form RouterId:unique-id-for-vni.
5916 */
5917void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
5918{
9e97ff03
CS
5919 if (is_vrf_rd_configured(bgp))
5920 return;
5921
92708db6 5922 form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd);
676f83b9 5923}
5924
128ea8ab 5925/*
5926 * Derive RD automatically for VNI using passed information - it
5927 * is of the form RouterId:unique-id-for-vni.
5928 */
d62a17ae 5929void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5930{
c44ab6f1 5931 char buf[BGP_EVPN_PREFIX_RD_LEN];
128ea8ab 5932
d62a17ae 5933 vpn->prd.family = AF_UNSPEC;
5934 vpn->prd.prefixlen = 64;
23d0a753 5935 snprintfrr(buf, sizeof(buf), "%pI4:%hu", &bgp->router_id, vpn->rd_id);
cbb65f5e 5936 (void)str2prefix_rd(buf, &vpn->prd);
d62a17ae 5937 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
128ea8ab 5938}
5939
7df407ed
CS
5940/*
5941 * Lookup L3-VNI
5942 */
5943bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni)
5944{
5945 struct list *inst = bm->bgp;
5946 struct listnode *node;
5947 struct bgp *bgp_vrf;
5948
5949 for (ALL_LIST_ELEMENTS_RO(inst, node, bgp_vrf)) {
5950 if (bgp_vrf->l3vni == vni)
5951 return true;
5952 }
5953
5954 return false;
5955}
5956
128ea8ab 5957/*
5958 * Lookup VNI.
5959 */
d62a17ae 5960struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
128ea8ab 5961{
d62a17ae 5962 struct bgpevpn *vpn;
5963 struct bgpevpn tmp;
128ea8ab 5964
6006b807 5965 memset(&tmp, 0, sizeof(tmp));
d62a17ae 5966 tmp.vni = vni;
5967 vpn = hash_lookup(bgp->vnihash, &tmp);
5968 return vpn;
128ea8ab 5969}
5970
5971/*
5972 * Create a new vpn - invoked upon configuration or zebra notification.
5973 */
d62a17ae 5974struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
76d07c7a
AK
5975 struct in_addr originator_ip,
5976 vrf_id_t tenant_vrf_id,
9daa5d47
AD
5977 struct in_addr mcast_grp,
5978 ifindex_t svi_ifindex)
128ea8ab 5979{
d62a17ae 5980 struct bgpevpn *vpn;
128ea8ab 5981
d62a17ae 5982 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
128ea8ab 5983
d62a17ae 5984 /* Set values - RD and RT set to defaults. */
5985 vpn->vni = vni;
5986 vpn->originator_ip = originator_ip;
29c53922 5987 vpn->tenant_vrf_id = tenant_vrf_id;
76d07c7a 5988 vpn->mcast_grp = mcast_grp;
9daa5d47 5989 vpn->svi_ifindex = svi_ifindex;
128ea8ab 5990
d62a17ae 5991 /* Initialize route-target import and export lists */
5992 vpn->import_rtl = list_new();
f4a5218d
AK
5993 vpn->import_rtl->cmp =
5994 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
5995 vpn->import_rtl->del = bgp_evpn_xxport_delete_ecomm;
d62a17ae 5996 vpn->export_rtl = list_new();
f4a5218d
AK
5997 vpn->export_rtl->cmp =
5998 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
5999 vpn->export_rtl->del = bgp_evpn_xxport_delete_ecomm;
e9eb5f63 6000 bf_assign_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 6001 derive_rd_rt_for_vni(bgp, vpn);
128ea8ab 6002
852d9f97
SW
6003 /* Initialize EVPN route tables. */
6004 vpn->ip_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
6005 vpn->mac_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
128ea8ab 6006
d62a17ae 6007 /* Add to hash */
ac390ef8 6008 (void)hash_get(bgp->vnihash, vpn, hash_alloc_intern);
6a8657d0 6009
021b6596
AD
6010 bgp_evpn_remote_ip_hash_init(vpn);
6011 bgp_evpn_link_to_vni_svi_hash(bgp, vpn);
6012
6a8657d0
MK
6013 /* add to l2vni list on corresponding vrf */
6014 bgpevpn_link_to_l3vni(vpn);
6015
c44ab6f1
AK
6016 bgp_evpn_vni_es_init(vpn);
6017
d62a17ae 6018 QOBJ_REG(vpn, bgpevpn);
6019 return vpn;
128ea8ab 6020}
6021
6022/*
6023 * Free a given VPN - called in multiple scenarios such as zebra
6024 * notification, configuration being deleted, advertise-all-vni disabled etc.
6025 * This just frees appropriate memory, caller should have taken other
6026 * needed actions.
6027 */
d62a17ae 6028void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 6029{
021b6596 6030 bgp_evpn_remote_ip_hash_destroy(vpn);
c44ab6f1 6031 bgp_evpn_vni_es_cleanup(vpn);
6a8657d0 6032 bgpevpn_unlink_from_l3vni(vpn);
852d9f97
SW
6033 bgp_table_unlock(vpn->ip_table);
6034 bgp_table_unlock(vpn->mac_table);
d62a17ae 6035 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
6a154c88
DL
6036 list_delete(&vpn->import_rtl);
6037 list_delete(&vpn->export_rtl);
e9eb5f63 6038 bf_release_index(bm->rd_idspace, vpn->rd_id);
021b6596 6039 hash_release(bgp->vni_svi_hash, vpn);
d62a17ae 6040 hash_release(bgp->vnihash, vpn);
6041 QOBJ_UNREG(vpn);
6042 XFREE(MTYPE_BGP_EVPN, vpn);
128ea8ab 6043}
6044
3727e359
DA
6045static void hash_evpn_free(struct bgpevpn *vpn)
6046{
6047 XFREE(MTYPE_BGP_EVPN, vpn);
6048}
6049
50f74cf1 6050/*
6051 * Import evpn route from global table to VNI/VRF/ESI.
128ea8ab 6052 */
d62a17ae 6053int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
5a1ae2c2 6054 const struct prefix *p, struct bgp_path_info *pi)
128ea8ab 6055{
40381db7 6056 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 1);
128ea8ab 6057}
6058
6059/*
50f74cf1 6060 * Unimport evpn route from VNI/VRF/ESI.
128ea8ab 6061 */
d62a17ae 6062int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
5a1ae2c2 6063 const struct prefix *p, struct bgp_path_info *pi)
128ea8ab 6064{
40381db7 6065 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 0);
128ea8ab 6066}
6067
db0e1937
MK
6068/* filter routes which have martian next hops */
6069int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
6070{
0291c246
MK
6071 afi_t afi;
6072 safi_t safi;
9bcb3eef 6073 struct bgp_dest *rd_dest, *dest;
0291c246 6074 struct bgp_table *table;
40381db7 6075 struct bgp_path_info *pi;
db0e1937
MK
6076
6077 afi = AFI_L2VPN;
6078 safi = SAFI_EVPN;
6079
6080 /* Walk entire global routing table and evaluate routes which could be
6081 * imported into this VPN. Note that we cannot just look at the routes
6082 * for the VNI's RD -
6083 * remote routes applicable for this VNI could have any RD.
6084 */
6085 /* EVPN routes are a 2-level table. */
9bcb3eef
DS
6086 for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
6087 rd_dest = bgp_route_next(rd_dest)) {
6088 table = bgp_dest_get_bgp_table_info(rd_dest);
db0e1937
MK
6089 if (!table)
6090 continue;
6091
9bcb3eef
DS
6092 for (dest = bgp_table_top(table); dest;
6093 dest = bgp_route_next(dest)) {
db0e1937 6094
9bcb3eef 6095 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
6f94b685 6096 pi = pi->next) {
db0e1937
MK
6097
6098 /* Consider "valid" remote routes applicable for
6099 * this VNI. */
40381db7
DS
6100 if (!(pi->type == ZEBRA_ROUTE_BGP
6101 && pi->sub_type == BGP_ROUTE_NORMAL))
db0e1937 6102 continue;
9bcb3eef
DS
6103 if (bgp_nexthop_self(bgp, afi, pi->type,
6104 pi->sub_type, pi->attr,
6105 dest)) {
b54892e0 6106 const struct prefix *p =
9bcb3eef 6107 bgp_dest_get_prefix(dest);
db0e1937 6108
b54892e0
DS
6109 if (bgp_debug_update(pi->peer, p, NULL,
6110 1)) {
6111 char attr_str[BUFSIZ] = {0};
db0e1937 6112
b54892e0 6113 bgp_dump_attr(pi->attr,
5022c833
DA
6114 attr_str,
6115 sizeof(attr_str));
db0e1937 6116
db0e1937 6117 zlog_debug(
56ca3b5b 6118 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
9bcb3eef 6119 bgp->vrf_id, dest,
db0e1937 6120 attr_str);
b54892e0 6121 }
db0e1937 6122 bgp_evpn_unimport_route(bgp, afi, safi,
b54892e0 6123 p, pi);
db0e1937 6124
9bcb3eef 6125 bgp_rib_remove(dest, pi, pi->peer, afi,
60466a63 6126 safi);
db0e1937 6127 }
db0e1937
MK
6128 }
6129 }
6130 }
6131
6132 return 0;
6133}
6134
128ea8ab 6135/*
6136 * Handle del of a local MACIP.
6137 */
d62a17ae 6138int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
ec0ab544 6139 struct ipaddr *ip, int state)
128ea8ab 6140{
d62a17ae 6141 struct bgpevpn *vpn;
6142 struct prefix_evpn p;
9bcb3eef 6143 struct bgp_dest *dest;
128ea8ab 6144
d62a17ae 6145 /* Lookup VNI hash - should exist. */
6146 vpn = bgp_evpn_lookup_vni(bgp, vni);
6147 if (!vpn || !is_vni_live(vpn)) {
e50f7cfd 6148 flog_warn(EC_BGP_EVPN_VPN_VNI,
28642513 6149 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
d62a17ae 6150 bgp->vrf_id, vni, vpn ? "not live" : "not found");
6151 return -1;
6152 }
128ea8ab 6153
d62a17ae 6154 build_evpn_type2_prefix(&p, mac, ip);
ec0ab544
AK
6155 if (state == ZEBRA_NEIGH_ACTIVE) {
6156 /* Remove EVPN type-2 route and schedule for processing. */
6157 delete_evpn_route(bgp, vpn, &p);
6158 } else {
6159 /* Re-instate the current remote best path if any */
852d9f97 6160 dest = bgp_evpn_vni_node_lookup(vpn, &p, NULL);
e71ad4b6 6161 if (dest) {
9bcb3eef 6162 evpn_zebra_reinstall_best_route(bgp, vpn, dest);
e71ad4b6
DA
6163 bgp_dest_unlock_node(dest);
6164 }
ec0ab544 6165 }
128ea8ab 6166
d62a17ae 6167 return 0;
128ea8ab 6168}
6169
6170/*
6171 * Handle add of a local MACIP.
6172 */
d62a17ae 6173int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
c44ab6f1 6174 struct ipaddr *ip, uint8_t flags, uint32_t seq, esi_t *esi)
128ea8ab 6175{
d62a17ae 6176 struct bgpevpn *vpn;
6177 struct prefix_evpn p;
128ea8ab 6178
d62a17ae 6179 /* Lookup VNI hash - should exist. */
6180 vpn = bgp_evpn_lookup_vni(bgp, vni);
6181 if (!vpn || !is_vni_live(vpn)) {
e50f7cfd 6182 flog_warn(EC_BGP_EVPN_VPN_VNI,
28642513 6183 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
d62a17ae 6184 bgp->vrf_id, vni, vpn ? "not live" : "not found");
6185 return -1;
6186 }
128ea8ab 6187
d62a17ae 6188 /* Create EVPN type-2 route and schedule for processing. */
6189 build_evpn_type2_prefix(&p, mac, ip);
c44ab6f1 6190 if (update_evpn_route(bgp, vpn, &p, flags, seq, esi)) {
af4c2728 6191 flog_err(
e50f7cfd 6192 EC_BGP_EVPN_ROUTE_CREATE,
c0d72166 6193 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
1a98c087 6194 bgp->vrf_id, vpn->vni,
996c9314
LB
6195 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
6196 ? "sticky gateway"
6197 : "",
c0d72166 6198 mac, ip, flags);
d62a17ae 6199 return -1;
6200 }
128ea8ab 6201
d62a17ae 6202 return 0;
128ea8ab 6203}
6204
e3b78da8 6205static void link_l2vni_hash_to_l3vni(struct hash_bucket *bucket,
6a8657d0
MK
6206 struct bgp *bgp_vrf)
6207{
e3b78da8 6208 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
5e53dce3 6209 struct bgp *bgp_evpn = NULL;
6a8657d0 6210
5e53dce3
T
6211 bgp_evpn = bgp_get_evpn();
6212 assert(bgp_evpn);
6a8657d0 6213
6a8657d0
MK
6214 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
6215 bgpevpn_link_to_l3vni(vpn);
6216}
6217
14e814ea
CS
6218int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id,
6219 struct ethaddr *svi_rmac,
6220 struct ethaddr *vrr_rmac,
0483af6e 6221 struct in_addr originator_ip, int filter,
14e814ea
CS
6222 ifindex_t svi_ifindex,
6223 bool is_anycast_mac)
fe1dc5a3
MK
6224{
6225 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
5e53dce3 6226 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
f1f8b53c
MK
6227 struct listnode *node = NULL;
6228 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
6229 as_t as = 0;
6230
3621ebc5 6231 /* get the EVPN instance - required to get the AS number for VRF
523cafc4 6232 * auto-creatio
6233 */
5e53dce3
T
6234 bgp_evpn = bgp_get_evpn();
6235 if (!bgp_evpn) {
af4c2728 6236 flog_err(
e50f7cfd 6237 EC_BGP_NO_DFLT,
3621ebc5 6238 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
996c9314 6239 l3vni);
fe1dc5a3
MK
6240 return -1;
6241 }
5e53dce3 6242 as = bgp_evpn->as;
fe1dc5a3 6243
0437e105 6244 /* if the BGP vrf instance doesn't exist - create one */
3621ebc5 6245 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
fe1dc5a3
MK
6246 if (!bgp_vrf) {
6247
6248 int ret = 0;
6249
5d5393b9
DL
6250 ret = bgp_get_vty(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
6251 vrf_id == VRF_DEFAULT
6252 ? BGP_INSTANCE_TYPE_DEFAULT
6253 : BGP_INSTANCE_TYPE_VRF);
fe1dc5a3 6254 switch (ret) {
fe1dc5a3 6255 case BGP_ERR_AS_MISMATCH:
e50f7cfd 6256 flog_err(EC_BGP_EVPN_AS_MISMATCH,
d09328e5
IR
6257 "BGP instance is already running; AS is %u",
6258 as);
fe1dc5a3
MK
6259 return -1;
6260 case BGP_ERR_INSTANCE_MISMATCH:
e50f7cfd 6261 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH,
d09328e5 6262 "BGP instance type mismatch");
fe1dc5a3
MK
6263 return -1;
6264 }
6265
6266 /* mark as auto created */
6267 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
6268 }
6269
0483af6e 6270 /* associate the vrf with l3vni and related parameters */
fe1dc5a3 6271 bgp_vrf->l3vni = l3vni;
b67a60d2 6272 bgp_vrf->originator_ip = originator_ip;
0483af6e 6273 bgp_vrf->l3vni_svi_ifindex = svi_ifindex;
14e814ea 6274 bgp_vrf->evpn_info->is_anycast_mac = is_anycast_mac;
b67a60d2 6275
14e814ea
CS
6276 /* copy anycast MAC from VRR MAC */
6277 memcpy(&bgp_vrf->rmac, vrr_rmac, ETH_ALEN);
6278 /* copy sys RMAC from SVI MAC */
6279 memcpy(&bgp_vrf->evpn_info->pip_rmac_zebra, svi_rmac, ETH_ALEN);
6280 /* PIP user configured mac is not present use svi mac as sys mac */
6281 if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static))
6282 memcpy(&bgp_vrf->evpn_info->pip_rmac, svi_rmac, ETH_ALEN);
6283
c0d72166
DS
6284 if (bgp_debug_zebra(NULL))
6285 zlog_debug(
6286 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
6287 vrf_id_to_name(bgp_vrf->vrf_id), bgp_vrf->l3vni,
6288 bgp_vrf->evpn_info->advertise_pip ? "enable"
6289 : "disable",
6290 &bgp_vrf->rmac, &bgp_vrf->evpn_info->pip_rmac,
6291 &bgp_vrf->evpn_info->pip_rmac_static,
6292 is_anycast_mac ? "Enable" : "Disable");
6293
c48d9f5f 6294 /* set the right filter - are we using l3vni only for prefix routes? */
10f70510 6295 if (filter) {
c48d9f5f
MK
6296 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
6297
10f70510
AD
6298 /*
6299 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
6300 * set before linking vrf to L3VNI. Thus, no need to clear
6301 * that explicitly.
6302 */
6303 } else {
6304 UNSET_FLAG(bgp_vrf->vrf_flags,
6305 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
6306
6307 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
6308 if (!CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
6309
6310 /*
6311 * If we are flapping VNI_FLAG_USE_TWO_LABELS
6312 * flag, update all MACIP routes in this VNI
6313 */
6314 SET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
6315 update_all_type2_routes(bgp_evpn, vpn);
6316 }
6317 }
6318 }
6319
530e8a6e 6320 /* Map auto derive or configured RTs */
58d8948c
SW
6321 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD) ||
6322 CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD))
c581d8b0 6323 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
530e8a6e
CS
6324 else
6325 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
6326
58d8948c
SW
6327 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD) ||
6328 CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD))
c581d8b0 6329 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
530e8a6e
CS
6330
6331 /* auto derive RD */
676f83b9 6332 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
fe1dc5a3 6333
6a8657d0 6334 /* link all corresponding l2vnis */
5e53dce3 6335 hash_iterate(bgp_evpn->vnihash,
e3b78da8 6336 (void (*)(struct hash_bucket *,
996c9314 6337 void *))link_l2vni_hash_to_l3vni,
6a8657d0
MK
6338 bgp_vrf);
6339
c48d9f5f
MK
6340 /* Only update all corresponding type-2 routes if we are advertising two
6341 * labels along with type-2 routes
6342 */
6343 if (!filter)
6344 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
5e53dce3 6345 update_routes_for_vni(bgp_evpn, vpn);
fe1dc5a3 6346
06d2e8f3
MK
6347 /* advertise type-5 routes if needed */
6348 update_advertise_vrf_routes(bgp_vrf);
6349
5ba238b7
MK
6350 /* install all remote routes belonging to this l3vni into correspondng
6351 * vrf */
6352 install_routes_for_vrf(bgp_vrf);
fe1dc5a3
MK
6353
6354 return 0;
6355}
6356
996c9314 6357int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
fe1dc5a3
MK
6358{
6359 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
5e53dce3 6360 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
f1f8b53c 6361 struct listnode *node = NULL;
18abc1eb 6362 struct listnode *next = NULL;
f1f8b53c 6363 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
6364
6365 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
6366 if (!bgp_vrf) {
af4c2728 6367 flog_err(
e50f7cfd 6368 EC_BGP_NO_DFLT,
996c9314
LB
6369 "Cannot process L3VNI %u Del - Could not find BGP instance",
6370 l3vni);
fe1dc5a3
MK
6371 return -1;
6372 }
6373
5e53dce3
T
6374 bgp_evpn = bgp_get_evpn();
6375 if (!bgp_evpn) {
af4c2728 6376 flog_err(
e50f7cfd 6377 EC_BGP_NO_DFLT,
3621ebc5 6378 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
996c9314 6379 l3vni);
f1f8b53c
MK
6380 return -1;
6381 }
6382
d846168d 6383 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
18ee8310 6384 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
d846168d
CS
6385 * routes. This will uninstalling the routes from zebra and decremnt the
6386 * bgp info count.
523cafc4 6387 */
d846168d 6388 uninstall_routes_for_vrf(bgp_vrf);
5ba238b7 6389
06d2e8f3
MK
6390 /* delete/withdraw all type-5 routes */
6391 delete_withdraw_vrf_routes(bgp_vrf);
6392
fe1dc5a3
MK
6393 /* remove the l3vni from vrf instance */
6394 bgp_vrf->l3vni = 0;
6395
6396 /* remove the Rmac from the BGP vrf */
6397 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
14e814ea
CS
6398 memset(&bgp_vrf->evpn_info->pip_rmac_zebra, 0, ETH_ALEN);
6399 if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static) &&
6400 !is_zero_mac(&bgp_vrf->evpn_info->pip_rmac))
6401 memset(&bgp_vrf->evpn_info->pip_rmac, 0, ETH_ALEN);
fe1dc5a3 6402
530e8a6e 6403 /* remove default import RT or Unmap non-default import RT */
1525e99f 6404 if (!list_isempty(bgp_vrf->vrf_import_rtl)) {
10ebe1ab 6405 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
530e8a6e
CS
6406 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
6407 list_delete_all_node(bgp_vrf->vrf_import_rtl);
23a06e11 6408 }
530e8a6e
CS
6409
6410 /* remove default export RT */
6411 if (!list_isempty(bgp_vrf->vrf_export_rtl) &&
6412 !CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
5ba238b7 6413 list_delete_all_node(bgp_vrf->vrf_export_rtl);
23a06e11 6414 }
fe1dc5a3 6415
f1f8b53c 6416 /* update all corresponding local mac-ip routes */
c48d9f5f
MK
6417 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
6418 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
6419 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5e53dce3 6420 update_routes_for_vni(bgp_evpn, vpn);
c48d9f5f
MK
6421 }
6422 }
fe1dc5a3 6423
18abc1eb 6424 /* If any L2VNIs point to this instance, unlink them. */
6425 for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn))
6426 bgpevpn_unlink_from_l3vni(vpn);
6427
ff9d54fb
CS
6428 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
6429
fe1dc5a3
MK
6430 /* Delete the instance if it was autocreated */
6431 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
6432 bgp_delete(bgp_vrf);
6433
6434 return 0;
6435}
6436
128ea8ab 6437/*
6438 * Handle del of a local VNI.
6439 */
d62a17ae 6440int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
128ea8ab 6441{
d62a17ae 6442 struct bgpevpn *vpn;
128ea8ab 6443
d62a17ae 6444 /* Locate VNI hash */
6445 vpn = bgp_evpn_lookup_vni(bgp, vni);
07b9f44d 6446 if (!vpn)
d62a17ae 6447 return 0;
128ea8ab 6448
d62a17ae 6449 /* Remove all local EVPN routes and schedule for processing (to
6450 * withdraw from peers).
6451 */
6452 delete_routes_for_vni(bgp, vpn);
128ea8ab 6453
021b6596
AD
6454 bgp_evpn_unlink_from_vni_svi_hash(bgp, vpn);
6455
9daa5d47 6456 vpn->svi_ifindex = 0;
db0e1937
MK
6457 /*
6458 * tunnel is no longer active, del tunnel ip address from tip_hash
6459 */
6460 bgp_tip_del(bgp, &vpn->originator_ip);
6461
d62a17ae 6462 /* Clear "live" flag and see if hash needs to be freed. */
6463 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
6464 if (!is_vni_configured(vpn))
6465 bgp_evpn_free(bgp, vpn);
128ea8ab 6466
d62a17ae 6467 return 0;
128ea8ab 6468}
6469
6470/*
d1911c26 6471 * Handle add (or update) of a local VNI. The VNI changes we care
6472 * about are for the local-tunnel-ip and the (tenant) VRF.
128ea8ab 6473 */
d62a17ae 6474int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
76d07c7a
AK
6475 struct in_addr originator_ip,
6476 vrf_id_t tenant_vrf_id,
9daa5d47
AD
6477 struct in_addr mcast_grp,
6478 ifindex_t svi_ifindex)
d62a17ae 6479{
6480 struct bgpevpn *vpn;
6481 struct prefix_evpn p;
6482
d62a17ae 6483 /* Lookup VNI. If present and no change, exit. */
6484 vpn = bgp_evpn_lookup_vni(bgp, vni);
ddd16ed5 6485 if (vpn) {
29c53922 6486
d1911c26 6487 if (is_vni_live(vpn)
6488 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
76d07c7a 6489 && IPV4_ADDR_SAME(&vpn->mcast_grp, &mcast_grp)
9daa5d47
AD
6490 && vpn->tenant_vrf_id == tenant_vrf_id
6491 && vpn->svi_ifindex == svi_ifindex)
d1911c26 6492 /* Probably some other param has changed that we don't
6493 * care about. */
6494 return 0;
6495
76d07c7a
AK
6496 bgp_evpn_mcast_grp_change(bgp, vpn, mcast_grp);
6497
021b6596
AD
6498 if (vpn->svi_ifindex != svi_ifindex) {
6499
6500 /*
6501 * Unresolve all the gateway IP nexthops for this VNI
6502 * for old SVI
6503 */
dc6cef73
AD
6504 bgp_evpn_remote_ip_hash_iterate(
6505 vpn,
6506 (void (*)(struct hash_bucket *, void *))
6507 bgp_evpn_remote_ip_hash_unlink_nexthop,
021b6596
AD
6508 vpn);
6509 bgp_evpn_unlink_from_vni_svi_hash(bgp, vpn);
6510 vpn->svi_ifindex = svi_ifindex;
6511 bgp_evpn_link_to_vni_svi_hash(bgp, vpn);
6512
6513 /*
6514 * Resolve all the gateway IP nexthops for this VNI
6515 * for new SVI
6516 */
dc6cef73
AD
6517 bgp_evpn_remote_ip_hash_iterate(
6518 vpn,
6519 (void (*)(struct hash_bucket *, void *))
6520 bgp_evpn_remote_ip_hash_link_nexthop,
021b6596
AD
6521 vpn);
6522 }
9daa5d47 6523
d1911c26 6524 /* Update tenant_vrf_id if it has changed. */
6a8657d0 6525 if (vpn->tenant_vrf_id != tenant_vrf_id) {
021b6596
AD
6526
6527 /*
6528 * Unresolve all the gateway IP nexthops for this VNI
6529 * in old tenant vrf
6530 */
dc6cef73
AD
6531 bgp_evpn_remote_ip_hash_iterate(
6532 vpn,
6533 (void (*)(struct hash_bucket *, void *))
6534 bgp_evpn_remote_ip_hash_unlink_nexthop,
021b6596 6535 vpn);
6a8657d0 6536 bgpevpn_unlink_from_l3vni(vpn);
29c53922 6537 vpn->tenant_vrf_id = tenant_vrf_id;
6a8657d0 6538 bgpevpn_link_to_l3vni(vpn);
021b6596
AD
6539
6540 /*
6541 * Resolve all the gateway IP nexthops for this VNI
6542 * in new tenant vrf
6543 */
dc6cef73
AD
6544 bgp_evpn_remote_ip_hash_iterate(
6545 vpn,
6546 (void (*)(struct hash_bucket *, void *))
6547 bgp_evpn_remote_ip_hash_link_nexthop,
021b6596 6548 vpn);
6a8657d0 6549 }
29c53922 6550
d1911c26 6551 /* If tunnel endpoint IP has changed, update (and delete prior
6552 * type-3 route, if needed.)
6553 */
4dabdde3 6554 handle_tunnel_ip_change(bgp, vpn, originator_ip);
d62a17ae 6555
d1911c26 6556 /* Update all routes with new endpoint IP and/or export RT
6557 * for VRFs
6558 */
6559 if (is_vni_live(vpn))
6560 update_routes_for_vni(bgp, vpn);
879e43a5 6561 } else {
6562 /* Create or update as appropriate. */
76d07c7a 6563 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id,
879e43a5 6564 mcast_grp, svi_ifindex);
d62a17ae 6565 }
6566
db0e1937 6567 /* if the VNI is live already, there is nothing more to do */
ddd16ed5
MK
6568 if (is_vni_live(vpn))
6569 return 0;
6570
d62a17ae 6571 /* Mark as "live" */
6572 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
6573
db0e1937 6574 /* tunnel is now active, add tunnel-ip to db */
826c3f6d
TA
6575 if (bgp_tip_add(bgp, &originator_ip))
6576 /* The originator_ip was not already present in the
6577 * bgp martian next-hop table as a tunnel-ip, so we
6578 * need to go back and filter routes matching the new
6579 * martian next-hop.
6580 */
6581 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
db0e1937 6582
fd069644
DS
6583 /*
6584 * Create EVPN type-3 route and schedule for processing.
6585 *
6586 * RT-3 only if doing head-end replication
6587 */
833b8a50
AK
6588 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
6589 == VXLAN_FLOOD_HEAD_END_REPL) {
fd069644 6590 build_evpn_type3_prefix(&p, vpn->originator_ip);
c44ab6f1 6591 if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL)) {
fd069644
DS
6592 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
6593 "%u: Type3 route creation failure for VNI %u",
6594 bgp->vrf_id, vni);
6595 return -1;
6596 }
d62a17ae 6597 }
6598
6599 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
6600 * VNI,
6601 * install them.
6602 */
6603 install_routes_for_vni(bgp, vpn);
6604
d7d97010
MK
6605 /* If we are advertising gateway mac-ip
6606 It needs to be conveyed again to zebra */
6607 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
6608
df070e6f
CS
6609 /* advertise svi mac-ip knob to zebra */
6610 bgp_zebra_advertise_svi_macip(bgp, vpn->advertise_svi_macip, vpn->vni);
6611
d62a17ae 6612 return 0;
b18825eb 6613}
14c1a7bf 6614
fd069644
DS
6615/*
6616 * Handle change in setting for BUM handling. The supported values
6617 * are head-end replication and dropping all BUM packets. Any change
6618 * should be registered with zebra. Also, if doing head-end replication,
6619 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6620 * to be dropped, the RT-3s must be withdrawn.
6621 */
6622void bgp_evpn_flood_control_change(struct bgp *bgp)
6623{
6624 zlog_info("L2VPN EVPN BUM handling is %s",
6625 bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL ?
6626 "Flooding" : "Flooding Disabled");
6627
6628 bgp_zebra_vxlan_flood_control(bgp, bgp->vxlan_flood_ctrl);
6629 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL)
6630 hash_iterate(bgp->vnihash, create_advertise_type3, bgp);
6631 else if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
6632 hash_iterate(bgp->vnihash, delete_withdraw_type3, bgp);
6633}
6634
7724c0a1 6635/*
6636 * Cleanup EVPN information on disable - Need to delete and withdraw
6637 * EVPN routes from peers.
6638 */
d62a17ae 6639void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
7724c0a1 6640{
e3b78da8 6641 hash_iterate(bgp->vnihash, (void (*)(struct hash_bucket *,
9d303b37
DL
6642 void *))cleanup_vni_on_disable,
6643 bgp);
7724c0a1 6644}
6645
14c1a7bf 6646/*
6647 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6648 * BGP instance (default) is being freed.
6649 */
d62a17ae 6650void bgp_evpn_cleanup(struct bgp *bgp)
14c1a7bf 6651{
1525e99f 6652 hash_iterate(bgp->vnihash,
e3b78da8 6653 (void (*)(struct hash_bucket *, void *))free_vni_entry,
1525e99f
DS
6654 bgp);
6655
3727e359 6656 hash_clean(bgp->import_rt_hash, (void (*)(void *))hash_import_rt_free);
1525e99f 6657 hash_free(bgp->import_rt_hash);
d62a17ae 6658 bgp->import_rt_hash = NULL;
1525e99f 6659
3727e359
DA
6660 hash_clean(bgp->vrf_import_rt_hash,
6661 (void (*)(void *))hash_vrf_import_rt_free);
1525e99f 6662 hash_free(bgp->vrf_import_rt_hash);
10ebe1ab 6663 bgp->vrf_import_rt_hash = NULL;
1525e99f 6664
3727e359 6665 hash_clean(bgp->vni_svi_hash, (void (*)(void *))hash_evpn_free);
021b6596
AD
6666 hash_free(bgp->vni_svi_hash);
6667 bgp->vni_svi_hash = NULL;
1525e99f 6668 hash_free(bgp->vnihash);
d62a17ae 6669 bgp->vnihash = NULL;
1525e99f 6670
6a154c88
DL
6671 list_delete(&bgp->vrf_import_rtl);
6672 list_delete(&bgp->vrf_export_rtl);
6673 list_delete(&bgp->l2vnis);
14c1a7bf 6674}
6675
6676/*
6677 * Initialization for EVPN
6678 * Create
6679 * VNI hash table
6680 * hash for RT to VNI
14c1a7bf 6681 */
d62a17ae 6682void bgp_evpn_init(struct bgp *bgp)
6683{
6684 bgp->vnihash =
6685 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
021b6596
AD
6686 bgp->vni_svi_hash =
6687 hash_create(vni_svi_hash_key_make, vni_svi_hash_cmp,
6688 "BGP VNI hash based on SVI ifindex");
d62a17ae 6689 bgp->import_rt_hash =
6690 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
6691 "BGP Import RT Hash");
10ebe1ab
MK
6692 bgp->vrf_import_rt_hash =
6693 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
6694 "BGP VRF Import RT Hash");
c581d8b0
MK
6695 bgp->vrf_import_rtl = list_new();
6696 bgp->vrf_import_rtl->cmp =
ca337b46
SW
6697 (int (*)(void *, void *))evpn_vrf_route_target_cmp;
6698 bgp->vrf_import_rtl->del = evpn_vrf_rt_del;
c581d8b0
MK
6699 bgp->vrf_export_rtl = list_new();
6700 bgp->vrf_export_rtl->cmp =
ca337b46
SW
6701 (int (*)(void *, void *))evpn_vrf_route_target_cmp;
6702 bgp->vrf_export_rtl->del = evpn_vrf_rt_del;
6a8657d0 6703 bgp->l2vnis = list_new();
64465785 6704 bgp->l2vnis->cmp = vni_list_cmp;
85c8d83b
CS
6705 /* By default Duplicate Address Dection is enabled.
6706 * Max-moves (N) 5, detection time (M) 180
6707 * default action is warning-only
6708 * freeze action permanently freezes address,
6709 * and freeze time (auto-recovery) is disabled.
6710 */
6711 if (bgp->evpn_info) {
6712 bgp->evpn_info->dup_addr_detect = true;
6713 bgp->evpn_info->dad_time = EVPN_DAD_DEFAULT_TIME;
6714 bgp->evpn_info->dad_max_moves = EVPN_DAD_DEFAULT_MAX_MOVES;
6715 bgp->evpn_info->dad_freeze = false;
6716 bgp->evpn_info->dad_freeze_time = 0;
0b9d9cd0
CS
6717 /* Initialize zebra vxlan */
6718 bgp_zebra_dup_addr_detection(bgp);
5394a276
CS
6719 /* Enable PIP feature by default for bgp vrf instance */
6720 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
6721 struct bgp *bgp_default;
6722
6723 bgp->evpn_info->advertise_pip = true;
6724 bgp_default = bgp_get_default();
6725 if (bgp_default)
6726 bgp->evpn_info->pip_ip = bgp_default->router_id;
6727 }
85c8d83b 6728 }
fd069644
DS
6729
6730 /* Default BUM handling is to do head-end replication. */
6731 bgp->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
090efa2f
AK
6732
6733 bgp_evpn_nh_init(bgp);
14c1a7bf 6734}
10ebe1ab
MK
6735
6736void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
6737{
6738 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
090efa2f 6739 bgp_evpn_nh_finish(bgp_vrf);
10ebe1ab 6740}
44c69747
LK
6741
6742/*
6743 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6744 */
5f040085 6745int bgp_evpn_get_type5_prefixlen(const struct prefix *pfx)
44c69747
LK
6746{
6747 struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
6748
6749 if (!pfx || pfx->family != AF_EVPN)
6750 return 0;
6751
6752 if (evp->prefix.route_type != BGP_EVPN_IP_PREFIX_ROUTE)
6753 return 0;
6754
6755 return evp->prefix.prefix_addr.ip_prefix_length;
6756}
7c312383
AD
6757
6758/*
6759 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6760 */
5a1ae2c2 6761bool bgp_evpn_is_prefix_nht_supported(const struct prefix *pfx)
7c312383
AD
6762{
6763 struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
6764
6765 /*
e11329ca 6766 * EVPN routes should be marked as valid only if the nexthop is
6767 * reachable. Only if this happens, the route should be imported
6768 * (into VNI or VRF routing tables) and/or advertised.
fff7545a 6769 * Note: This is currently applied for EVPN type-1, type-2,
6770 * type-3, type-4 and type-5 routes.
6771 * It may be tweaked later on for other routes, or
e11329ca 6772 * even removed completely when all routes are handled.
7c312383 6773 */
fa46a5cd
AK
6774 if (pfx && pfx->family == AF_EVPN
6775 && (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
6776 || evp->prefix.route_type == BGP_EVPN_AD_ROUTE
6777 || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
6778 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
6779 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
7c312383
AD
6780 return true;
6781
6782 return false;
6783}
021b6596
AD
6784
6785static void *bgp_evpn_remote_ip_hash_alloc(void *p)
6786{
6787 const struct evpn_remote_ip *key = (const struct evpn_remote_ip *)p;
6788 struct evpn_remote_ip *ip;
6789
6790 ip = XMALLOC(MTYPE_EVPN_REMOTE_IP, sizeof(struct evpn_remote_ip));
6791 *ip = *key;
6792 ip->macip_path_list = list_new();
6793
6794 return ip;
6795}
6796
6797static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p)
6798{
6799 const struct evpn_remote_ip *ip = p;
6800 const struct ipaddr *addr = &ip->addr;
6801
6802 if (IS_IPADDR_V4(addr))
6803 return jhash_1word(addr->ipaddr_v4.s_addr, 0);
6804
6805 return jhash2(addr->ipaddr_v6.s6_addr32,
6806 array_size(addr->ipaddr_v6.s6_addr32), 0);
6807}
6808
6809static bool bgp_evpn_remote_ip_hash_cmp(const void *p1, const void *p2)
6810{
6811 const struct evpn_remote_ip *ip1 = p1;
6812 const struct evpn_remote_ip *ip2 = p2;
6813
761cc919 6814 return !ipaddr_cmp(&ip1->addr, &ip2->addr);
021b6596
AD
6815}
6816
6817static void bgp_evpn_remote_ip_hash_init(struct bgpevpn *vpn)
6818{
dc6cef73
AD
6819 if (!evpn_resolve_overlay_index())
6820 return;
6821
021b6596
AD
6822 vpn->remote_ip_hash = hash_create(bgp_evpn_remote_ip_hash_key_make,
6823 bgp_evpn_remote_ip_hash_cmp,
6824 "BGP EVPN remote IP hash");
6825}
6826
6827static void bgp_evpn_remote_ip_hash_free(struct hash_bucket *bucket, void *args)
6828{
6829 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6830 struct bgpevpn *vpn = (struct bgpevpn *)args;
6831
6832 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6833
6834 list_delete(&ip->macip_path_list);
6835
6836 hash_release(vpn->remote_ip_hash, ip);
6837 XFREE(MTYPE_EVPN_REMOTE_IP, ip);
6838}
6839
6840static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn *vpn)
6841{
dc6cef73 6842 if (!evpn_resolve_overlay_index() || vpn->remote_ip_hash == NULL)
021b6596
AD
6843 return;
6844
6845 hash_iterate(vpn->remote_ip_hash,
6846 (void (*)(struct hash_bucket *, void *))bgp_evpn_remote_ip_hash_free,
6847 vpn);
6848
6849 hash_free(vpn->remote_ip_hash);
6850 vpn->remote_ip_hash = NULL;
6851}
6852
6853/* Add a remote MAC/IP route to hash table */
6854static void bgp_evpn_remote_ip_hash_add(struct bgpevpn *vpn,
6855 struct bgp_path_info *pi)
6856{
6857 struct evpn_remote_ip tmp;
6858 struct evpn_remote_ip *ip;
6859 struct prefix_evpn *evp;
6860
dc6cef73
AD
6861 if (!evpn_resolve_overlay_index())
6862 return;
6863
6864 if (pi->type != ZEBRA_ROUTE_BGP || pi->sub_type != BGP_ROUTE_IMPORTED
6865 || !CHECK_FLAG(pi->flags, BGP_PATH_VALID))
6866 return;
6867
021b6596
AD
6868 evp = (struct prefix_evpn *)&pi->net->p;
6869
6870 if (evp->family != AF_EVPN
6871 || evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
6872 || is_evpn_prefix_ipaddr_none(evp))
6873 return;
6874
6875 tmp.addr = evp->prefix.macip_addr.ip;
6876 ip = hash_lookup(vpn->remote_ip_hash, &tmp);
6877 if (ip) {
6878 if (listnode_lookup(ip->macip_path_list, pi) != NULL)
6879 return;
6880 (void)listnode_add(ip->macip_path_list, pi);
6881 return;
6882 }
6883
6884 ip = hash_get(vpn->remote_ip_hash, &tmp, bgp_evpn_remote_ip_hash_alloc);
021b6596
AD
6885 (void)listnode_add(ip->macip_path_list, pi);
6886
6887 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, true);
6888}
6889
6890/* Delete a remote MAC/IP route from hash table */
6891static void bgp_evpn_remote_ip_hash_del(struct bgpevpn *vpn,
6892 struct bgp_path_info *pi)
6893{
6894 struct evpn_remote_ip tmp;
6895 struct evpn_remote_ip *ip;
6896 struct prefix_evpn *evp;
6897
dc6cef73
AD
6898 if (!evpn_resolve_overlay_index())
6899 return;
6900
021b6596
AD
6901 evp = (struct prefix_evpn *)&pi->net->p;
6902
6903 if (evp->family != AF_EVPN
6904 || evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
6905 || is_evpn_prefix_ipaddr_none(evp))
6906 return;
6907
6908 tmp.addr = evp->prefix.macip_addr.ip;
6909 ip = hash_lookup(vpn->remote_ip_hash, &tmp);
6910 if (ip == NULL)
6911 return;
6912
6913 listnode_delete(ip->macip_path_list, pi);
6914
6915 if (ip->macip_path_list->count == 0) {
6916 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6917 hash_release(vpn->remote_ip_hash, ip);
889360dc 6918 list_delete(&ip->macip_path_list);
021b6596
AD
6919 XFREE(MTYPE_EVPN_REMOTE_IP, ip);
6920 }
6921}
6922
dc6cef73
AD
6923static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn *vpn,
6924 void (*func)(struct hash_bucket *,
6925 void *),
6926 void *arg)
6927{
6928 if (!evpn_resolve_overlay_index())
6929 return;
6930
6931 hash_iterate(vpn->remote_ip_hash, func, arg);
6932}
6933
021b6596
AD
6934static void show_remote_ip_entry(struct hash_bucket *bucket, void *args)
6935{
6936 char buf[INET6_ADDRSTRLEN];
021b6596
AD
6937 struct listnode *node = NULL;
6938 struct bgp_path_info *pi = NULL;
6939 struct vty *vty = (struct vty *)args;
6940 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6941
6942 vty_out(vty, " Remote IP: %s\n",
6943 ipaddr2str(&ip->addr, buf, sizeof(buf)));
6944 vty_out(vty, " Linked MAC/IP routes:\n");
511211bf
DA
6945 for (ALL_LIST_ELEMENTS_RO(ip->macip_path_list, node, pi))
6946 vty_out(vty, " %pFX\n", &pi->net->p);
021b6596
AD
6947}
6948
6949void bgp_evpn_show_remote_ip_hash(struct hash_bucket *bucket, void *args)
6950{
6951 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6952 struct vty *vty = (struct vty *)args;
6953
6954 vty_out(vty, "VNI: %u\n", vpn->vni);
dc6cef73
AD
6955 bgp_evpn_remote_ip_hash_iterate(
6956 vpn,
021b6596
AD
6957 (void (*)(struct hash_bucket *, void *))show_remote_ip_entry,
6958 vty);
6959 vty_out(vty, "\n");
6960}
6961
6962static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket *bucket,
6963 void *args)
6964{
6965 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6966 struct bgpevpn *vpn = (struct bgpevpn *)args;
6967
6968 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, true);
6969}
6970
6971static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket *bucket,
6972 void *args)
6973{
6974 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6975 struct bgpevpn *vpn = (struct bgpevpn *)args;
6976
6977 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6978}
6979
6980static unsigned int vni_svi_hash_key_make(const void *p)
6981{
6982 const struct bgpevpn *vpn = p;
6983
6984 return jhash_1word(vpn->svi_ifindex, 0);
6985}
6986
6987static bool vni_svi_hash_cmp(const void *p1, const void *p2)
6988{
6989 const struct bgpevpn *vpn1 = p1;
6990 const struct bgpevpn *vpn2 = p2;
6991
6992 return (vpn1->svi_ifindex == vpn2->svi_ifindex);
6993}
6994
6995static struct bgpevpn *bgp_evpn_vni_svi_hash_lookup(struct bgp *bgp,
6996 ifindex_t svi)
6997{
6998 struct bgpevpn *vpn;
6999 struct bgpevpn tmp;
7000
6006b807 7001 memset(&tmp, 0, sizeof(tmp));
021b6596
AD
7002 tmp.svi_ifindex = svi;
7003 vpn = hash_lookup(bgp->vni_svi_hash, &tmp);
7004 return vpn;
7005}
7006
7007static void bgp_evpn_link_to_vni_svi_hash(struct bgp *bgp, struct bgpevpn *vpn)
7008{
7009 if (vpn->svi_ifindex == 0)
7010 return;
7011
8e3aae66 7012 (void)hash_get(bgp->vni_svi_hash, vpn, hash_alloc_intern);
021b6596
AD
7013}
7014
7015static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp *bgp,
7016 struct bgpevpn *vpn)
7017{
7018 if (vpn->svi_ifindex == 0)
7019 return;
7020
7021 hash_release(bgp->vni_svi_hash, vpn);
7022}
7023
7024void bgp_evpn_show_vni_svi_hash(struct hash_bucket *bucket, void *args)
7025{
7026 struct bgpevpn *evpn = (struct bgpevpn *)bucket->data;
7027 struct vty *vty = (struct vty *)args;
7028
7029 vty_out(vty, "SVI: %u VNI: %u\n", evpn->svi_ifindex, evpn->vni);
7030}
7031
7032/*
7033 * This function is called for a bgp_nexthop_cache entry when the nexthop is
7034 * gateway IP overlay index.
7035 * This function returns true if there is a remote MAC/IP route for the gateway
7036 * IP in the EVI of the nexthop SVI.
7037 */
7038bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache *bnc)
7039{
7040 struct bgp *bgp_evpn = NULL;
7041 struct bgpevpn *vpn = NULL;
7042 struct evpn_remote_ip tmp;
7043 struct prefix *p;
7044
dc6cef73
AD
7045 if (!evpn_resolve_overlay_index())
7046 return false;
7047
021b6596
AD
7048 if (!bnc->nexthop || bnc->nexthop->ifindex == 0)
7049 return false;
7050
7051 bgp_evpn = bgp_get_evpn();
7052 if (!bgp_evpn)
7053 return false;
7054
7055 /*
7056 * Gateway IP is resolved by nht over SVI interface.
7057 * Use this SVI to find corresponding EVI(L2 context)
7058 */
7059 vpn = bgp_evpn_vni_svi_hash_lookup(bgp_evpn, bnc->nexthop->ifindex);
7060 if (!vpn)
7061 return false;
7062
7063 if (vpn->bgp_vrf != bnc->bgp)
7064 return false;
7065
7066 /*
7067 * Check if the gateway IP is present in the EVI remote_ip_hash table
7068 * which stores all the remote IP addresses received via MAC/IP routes
7069 * in this EVI
7070 */
6006b807 7071 memset(&tmp, 0, sizeof(tmp));
021b6596
AD
7072
7073 p = &bnc->prefix;
7074 if (p->family == AF_INET) {
7075 tmp.addr.ipa_type = IPADDR_V4;
7076 memcpy(&(tmp.addr.ipaddr_v4), &(p->u.prefix4),
7077 sizeof(struct in_addr));
7078 } else if (p->family == AF_INET6) {
7079 tmp.addr.ipa_type = IPADDR_V6;
7080 memcpy(&(tmp.addr.ipaddr_v6), &(p->u.prefix6),
7081 sizeof(struct in6_addr));
7082 } else
7083 return false;
7084
7085 if (hash_lookup(vpn->remote_ip_hash, &tmp) == NULL)
7086 return false;
7087
7088 return true;
7089}
7090
7091/* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
7092static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn *vpn,
7093 struct ipaddr *addr,
7094 bool resolve)
7095{
7096 afi_t afi;
7097 struct prefix p;
7098 struct bgp_nexthop_cache *bnc;
7099 struct bgp_nexthop_cache_head *tree = NULL;
7100
7101 if (!vpn->bgp_vrf || vpn->svi_ifindex == 0)
7102 return;
7103
6006b807 7104 memset(&p, 0, sizeof(p));
021b6596
AD
7105
7106 if (addr->ipa_type == IPADDR_V4) {
7107 afi = AFI_IP;
7108 p.family = AF_INET;
7109 memcpy(&(p.u.prefix4), &(addr->ipaddr_v4),
7110 sizeof(struct in_addr));
7111 p.prefixlen = IPV4_MAX_BITLEN;
7112 } else if (addr->ipa_type == IPADDR_V6) {
7113 afi = AFI_IP6;
7114 p.family = AF_INET6;
7115 memcpy(&(p.u.prefix6), &(addr->ipaddr_v6),
7116 sizeof(struct in6_addr));
7117 p.prefixlen = IPV6_MAX_BITLEN;
7118 } else
7119 return;
7120
7121 tree = &vpn->bgp_vrf->nexthop_cache_table[afi];
35aae5c9 7122 bnc = bnc_find(tree, &p, 0, 0);
021b6596
AD
7123
7124 if (!bnc || !bnc->is_evpn_gwip_nexthop)
7125 return;
7126
7127 if (!bnc->nexthop || bnc->nexthop->ifindex != vpn->svi_ifindex)
7128 return;
7129
511211bf
DA
7130 if (BGP_DEBUG(nht, NHT))
7131 zlog_debug("%s(%u): vni %u mac/ip %s for NH %pFX",
021b6596 7132 vpn->bgp_vrf->name_pretty, vpn->tenant_vrf_id,
511211bf
DA
7133 vpn->vni, (resolve ? "add" : "delete"),
7134 &bnc->prefix);
021b6596
AD
7135
7136 /*
7137 * MAC/IP route or SVI or tenant vrf being added to EVI.
7138 * Set nexthop as valid only if it is already L3 reachable
7139 */
7140 if (resolve && bnc->flags & BGP_NEXTHOP_EVPN_INCOMPLETE) {
7141 bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
7142 bnc->flags |= BGP_NEXTHOP_VALID;
7143 bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
7144 evaluate_paths(bnc);
7145 }
7146
7147 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
7148 if (!resolve && bnc->flags & BGP_NEXTHOP_VALID) {
7149 bnc->flags &= ~BGP_NEXTHOP_VALID;
7150 bnc->flags |= BGP_NEXTHOP_EVPN_INCOMPLETE;
7151 bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
7152 evaluate_paths(bnc);
7153 }
7154}
7155
dc6cef73
AD
7156void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket *bucket,
7157 void *arg)
7158{
7159 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
7160 struct bgp_dest *dest;
7161 struct bgp_path_info *pi;
7162
7163 bgp_evpn_remote_ip_hash_init(vpn);
7164
852d9f97 7165 for (dest = bgp_table_top(vpn->ip_table); dest;
dc6cef73
AD
7166 dest = bgp_route_next(dest))
7167 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
7168 bgp_evpn_remote_ip_hash_add(vpn, pi);
7169}
7170
7171void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket *bucket,
7172 void *arg)
7173{
7174 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
7175
7176 bgp_evpn_remote_ip_hash_destroy(vpn);
7177}