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