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