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