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