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