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