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