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