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