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