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