]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn.c
Merge pull request #5066 from ak503/libfrr_crash
[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 }
1546 /* Set nexthop to ourselves and fill in the Router MAC. */
342dd0c6 1547 attr.nexthop = bgp_vrf->originator_ip;
1548 attr.mp_nexthop_global_in = bgp_vrf->originator_ip;
1549 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1550 memcpy(&attr.rmac, &bgp_vrf->rmac, sizeof(struct ethaddr));
1551
1552 /* Setup RT and encap extended community */
1553 build_evpn_type5_route_extcomm(bgp_vrf, &attr);
1554
1555 /* get the route node in global table */
5e53dce3 1556 rn = bgp_afi_node_get(bgp_evpn->rib[afi][safi], afi, safi,
996c9314 1557 (struct prefix *)evp, &bgp_vrf->vrf_prd);
342dd0c6 1558 assert(rn);
1559
1560 /* create or update the route entry within the route node */
5e53dce3 1561 update_evpn_type5_route_entry(bgp_evpn, bgp_vrf, afi, safi, rn, &attr,
996c9314 1562 &route_changed);
342dd0c6 1563
1564 /* schedule for processing and unlock node */
5424b7ba 1565 if (route_changed) {
5e53dce3 1566 bgp_process(bgp_evpn, rn, afi, safi);
5424b7ba
MK
1567 bgp_unlock_node(rn);
1568 }
342dd0c6 1569
1570 /* uninten temporary */
5ee65f6f 1571 if (!src_attr)
1572 aspath_unintern(&attr.aspath);
342dd0c6 1573 return 0;
1574}
1575
128ea8ab 1576/*
1577 * Create or update EVPN route entry. This could be in the VNI route table
1578 * or the global route table.
1579 */
d62a17ae 1580static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1581 afi_t afi, safi_t safi, struct bgp_node *rn,
f07e1c99 1582 struct attr *attr, int add,
40381db7 1583 struct bgp_path_info **pi, uint8_t flags,
f07e1c99 1584 uint32_t seq)
d62a17ae 1585{
40381db7
DS
1586 struct bgp_path_info *tmp_pi;
1587 struct bgp_path_info *local_pi;
d62a17ae 1588 struct attr *attr_new;
b57ba6d2 1589 mpls_label_t label[BGP_MAX_LABELS];
d7c0a89a 1590 uint32_t num_labels = 1;
d62a17ae 1591 int route_change = 1;
d7c0a89a 1592 uint8_t sticky = 0;
b57ba6d2 1593 struct prefix_evpn *evp;
d62a17ae 1594
40381db7 1595 *pi = NULL;
b57ba6d2
MK
1596 evp = (struct prefix_evpn *)&rn->p;
1597 memset(&label, 0, sizeof(label));
d62a17ae 1598
f07e1c99 1599 /* See if this is an update of an existing route, or a new add. */
40381db7 1600 local_pi = NULL;
6f94b685
DS
1601 for (tmp_pi = bgp_node_get_bgp_path_info(rn); tmp_pi;
1602 tmp_pi = tmp_pi->next) {
40381db7
DS
1603 if (tmp_pi->peer == bgp->peer_self
1604 && tmp_pi->type == ZEBRA_ROUTE_BGP
1605 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
1606 local_pi = tmp_pi;
d62a17ae 1607 }
1608
1609 /* If route doesn't exist already, create a new one, if told to.
1610 * Otherwise act based on whether the attributes of the route have
1611 * changed or not.
1612 */
40381db7 1613 if (!local_pi && !add)
d62a17ae 1614 return 0;
1615
f07e1c99 1616 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1617 if (seq && !CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW))
1618 add_mac_mobility_to_attr(seq, attr);
d62a17ae 1619
40381db7 1620 if (!local_pi) {
d62a17ae 1621 /* Add (or update) attribute to hash. */
1622 attr_new = bgp_attr_intern(attr);
1623
1624 /* Extract MAC mobility sequence number, if any. */
1625 attr_new->mm_seqnum =
1626 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
1627 attr_new->sticky = sticky;
1628
1629 /* Create new route with its attribute. */
40381db7 1630 tmp_pi = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
d62a17ae 1631 bgp->peer_self, attr_new, rn);
40381db7
DS
1632 SET_FLAG(tmp_pi->flags, BGP_PATH_VALID);
1633 bgp_path_info_extra_get(tmp_pi);
d62a17ae 1634
1635 /* The VNI goes into the 'label' field of the route */
b57ba6d2 1636 vni2label(vpn->vni, &label[0]);
c48d9f5f
MK
1637
1638 /* Type-2 routes may carry a second VNI - the L3-VNI.
1639 * Only attach second label if we are advertising two labels for
1640 * type-2 routes.
1641 */
996c9314
LB
1642 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1643 && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
b57ba6d2
MK
1644 vni_t l3vni;
1645
1646 l3vni = bgpevpn_get_l3vni(vpn);
1647 if (l3vni) {
1648 vni2label(l3vni, &label[1]);
1649 num_labels++;
1650 }
1651 }
d62a17ae 1652
40381db7
DS
1653 memcpy(&tmp_pi->extra->label, label, sizeof(label));
1654 tmp_pi->extra->num_labels = num_labels;
1655 bgp_path_info_add(rn, tmp_pi);
d62a17ae 1656 } else {
40381db7
DS
1657 tmp_pi = local_pi;
1658 if (attrhash_cmp(tmp_pi->attr, attr)
1659 && !CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
d62a17ae 1660 route_change = 0;
1661 else {
c48d9f5f
MK
1662 /*
1663 * The attributes have changed, type-2 routes needs to
1664 * be advertised with right labels.
1665 */
1666 vni2label(vpn->vni, &label[0]);
996c9314
LB
1667 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1668 && CHECK_FLAG(vpn->flags,
1669 VNI_FLAG_USE_TWO_LABELS)) {
c48d9f5f
MK
1670 vni_t l3vni;
1671
1672 l3vni = bgpevpn_get_l3vni(vpn);
1673 if (l3vni) {
1674 vni2label(l3vni, &label[1]);
1675 num_labels++;
1676 }
1677 }
40381db7
DS
1678 memcpy(&tmp_pi->extra->label, label, sizeof(label));
1679 tmp_pi->extra->num_labels = num_labels;
c48d9f5f 1680
d62a17ae 1681 /* The attribute has changed. */
1682 /* Add (or update) attribute to hash. */
1683 attr_new = bgp_attr_intern(attr);
40381db7 1684 bgp_path_info_set_flag(rn, tmp_pi,
18ee8310 1685 BGP_PATH_ATTR_CHANGED);
d62a17ae 1686
f07e1c99 1687 /* Extract MAC mobility sequence number, if any. */
1688 attr_new->mm_seqnum =
1689 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
1690 attr_new->sticky = sticky;
1691
d62a17ae 1692 /* Restore route, if needed. */
40381db7
DS
1693 if (CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
1694 bgp_path_info_restore(rn, tmp_pi);
d62a17ae 1695
1696 /* Unintern existing, set to new. */
40381db7
DS
1697 bgp_attr_unintern(&tmp_pi->attr);
1698 tmp_pi->attr = attr_new;
1699 tmp_pi->uptime = bgp_clock();
d62a17ae 1700 }
1701 }
1702
1703 /* Return back the route entry. */
40381db7 1704 *pi = tmp_pi;
d62a17ae 1705 return route_change;
128ea8ab 1706}
1707
ec0ab544
AK
1708static void evpn_zebra_reinstall_best_route(struct bgp *bgp,
1709 struct bgpevpn *vpn, struct bgp_node *rn)
1710{
1711 struct bgp_path_info *tmp_ri;
1712 struct bgp_path_info *curr_select = NULL;
1713
1714 for (tmp_ri = bgp_node_get_bgp_path_info(rn);
1715 tmp_ri; tmp_ri = tmp_ri->next) {
1716 if (CHECK_FLAG(tmp_ri->flags, BGP_PATH_SELECTED)) {
1717 curr_select = tmp_ri;
1718 break;
1719 }
1720 }
1721
1722 if (curr_select && curr_select->type == ZEBRA_ROUTE_BGP
1723 && curr_select->sub_type == BGP_ROUTE_IMPORTED)
1724 evpn_zebra_install(bgp, vpn,
1725 (struct prefix_evpn *)&rn->p,
1726 curr_select);
1727}
1728
6d8c603a
AK
1729/*
1730 * If the local route was not selected evict it and tell zebra to re-add
1731 * the best remote dest.
1732 *
1733 * Typically a local path added by zebra is expected to be selected as
1734 * best. In which case when a remote path wins as best (later)
1735 * evpn_route_select_install itself evicts the older-local-best path.
1736 *
1737 * However if bgp's add and zebra's add cross paths (race condition) it
1738 * is possible that the local path is no longer the "older" best path.
1739 * It is a path that was never designated as best and hence requires
1740 * additional handling to prevent bgp from injecting and holding on to a
1741 * non-best local path.
1742 */
1743static void evpn_cleanup_local_non_best_route(struct bgp *bgp,
1744 struct bgpevpn *vpn,
1745 struct bgp_node *rn,
9a8897aa 1746 struct bgp_path_info *local_pi)
6d8c603a 1747{
6d8c603a
AK
1748 char buf[PREFIX_STRLEN];
1749
6d8c603a
AK
1750 /* local path was not picked as the winner; kick it out */
1751 if (bgp_debug_zebra(NULL)) {
1752 zlog_debug("evicting local evpn prefix %s as remote won",
1753 prefix2str(&rn->p, buf, sizeof(buf)));
1754 }
6d8c603a
AK
1755 evpn_delete_old_local_route(bgp, vpn, rn, local_pi);
1756 bgp_path_info_reap(rn, local_pi);
1757
1758 /* tell zebra to re-add the best remote path */
ec0ab544 1759 evpn_zebra_reinstall_best_route(bgp, vpn, rn);
6d8c603a
AK
1760}
1761
128ea8ab 1762/*
1763 * Create or update EVPN route (of type based on prefix) for specified VNI
1764 * and schedule for processing.
1765 */
d62a17ae 1766static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
f07e1c99 1767 struct prefix_evpn *p, uint8_t flags,
1768 uint32_t seq)
128ea8ab 1769{
d62a17ae 1770 struct bgp_node *rn;
1771 struct attr attr;
1772 struct attr *attr_new;
1ec31309 1773 int add_l3_ecomm = 0;
40381db7 1774 struct bgp_path_info *pi;
d62a17ae 1775 afi_t afi = AFI_L2VPN;
1776 safi_t safi = SAFI_EVPN;
1777 int route_change;
128ea8ab 1778
d62a17ae 1779 memset(&attr, 0, sizeof(struct attr));
128ea8ab 1780
d62a17ae 1781 /* Build path-attribute for this route. */
1782 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1783 attr.nexthop = vpn->originator_ip;
1784 attr.mp_nexthop_global_in = vpn->originator_ip;
1785 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
317f1fe0 1786 attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0;
ead40654 1787 attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0;
68e33151
CS
1788 attr.router_flag = CHECK_FLAG(flags,
1789 ZEBRA_MACIP_TYPE_ROUTER_FLAG) ? 1 : 0;
be41eb68
MK
1790
1791 /* PMSI is only needed for type-3 routes */
d03239d0 1792 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
be41eb68 1793 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL);
d03239d0
AK
1794 attr.pmsi_tnl_type = PMSI_TNLTYPE_INGR_REPL;
1795 }
be41eb68 1796
1ec31309 1797 /* router mac is only needed for type-2 routes here. */
be41eb68
MK
1798 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
1799 bgpevpn_get_rmac(vpn, &attr.rmac);
a21bd7a3 1800 vni2label(vpn->vni, &(attr.label));
128ea8ab 1801
1ec31309 1802 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1803 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1804 * these routes.
1805 */
1806 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
3714a385 1807 (is_evpn_prefix_ipaddr_v4(p) ||
1808 !IN6_IS_ADDR_LINKLOCAL(&p->prefix.macip_addr.ip.ipaddr_v6)) &&
148b548c 1809 CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS) &&
1810 bgpevpn_get_l3vni(vpn))
1ec31309 1811 add_l3_ecomm = 1;
1812
1813 /* Set up extended community. */
1814 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
128ea8ab 1815
d62a17ae 1816 /* First, create (or fetch) route node within the VNI. */
1817 /* NOTE: There is no RD here. */
1818 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
128ea8ab 1819
d62a17ae 1820 /* Create or update route entry. */
1821 route_change = update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
40381db7
DS
1822 1, &pi, flags, seq);
1823 assert(pi);
1824 attr_new = pi->attr;
128ea8ab 1825
6d8c603a
AK
1826 /* lock ri to prevent freeing in evpn_route_select_install */
1827 bgp_path_info_lock(pi);
d62a17ae 1828 /* Perform route selection; this is just to set the flags correctly
1829 * as local route in the VNI always wins.
1830 */
1831 evpn_route_select_install(bgp, vpn, rn);
6d8c603a 1832 /*
9a8897aa
AK
1833 * If the new local route was not selected evict it and tell zebra
1834 * to re-add the best remote dest. BGP doesn't retain non-best local
1835 * routes.
6d8c603a 1836 */
9a8897aa
AK
1837 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
1838 route_change = 0;
1839 evpn_cleanup_local_non_best_route(bgp, vpn, rn, pi);
1840 }
6d8c603a
AK
1841 bgp_path_info_unlock(pi);
1842
d62a17ae 1843 bgp_unlock_node(rn);
128ea8ab 1844
d62a17ae 1845 /* If this is a new route or some attribute has changed, export the
1846 * route to the global table. The route will be advertised to peers
1847 * from there. Note that this table is a 2-level tree (RD-level +
1848 * Prefix-level) similar to L3VPN routes.
1849 */
1850 if (route_change) {
40381db7 1851 struct bgp_path_info *global_pi;
128ea8ab 1852
d62a17ae 1853 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1854 (struct prefix *)p, &vpn->prd);
f07e1c99 1855 update_evpn_route_entry(bgp, vpn, afi, safi, rn, attr_new, 1,
40381db7 1856 &global_pi, flags, seq);
128ea8ab 1857
d62a17ae 1858 /* Schedule for processing and unlock node. */
1859 bgp_process(bgp, rn, afi, safi);
1860 bgp_unlock_node(rn);
1861 }
128ea8ab 1862
d62a17ae 1863 /* Unintern temporary. */
1864 aspath_unintern(&attr.aspath);
128ea8ab 1865
d62a17ae 1866 return 0;
128ea8ab 1867}
1868
50f74cf1 1869/*
1870 * Delete EVPN route entry.
1871 * The entry can be in ESI/VNI table or the global table.
1872 */
4b7e6066 1873static void delete_evpn_route_entry(struct bgp *bgp, afi_t afi, safi_t safi,
50f74cf1 1874 struct bgp_node *rn,
40381db7 1875 struct bgp_path_info **pi)
342dd0c6 1876{
40381db7 1877 struct bgp_path_info *tmp_pi;
342dd0c6 1878
40381db7 1879 *pi = NULL;
342dd0c6 1880
50f74cf1 1881 /* Now, find matching route. */
6f94b685
DS
1882 for (tmp_pi = bgp_node_get_bgp_path_info(rn); tmp_pi;
1883 tmp_pi = tmp_pi->next)
40381db7
DS
1884 if (tmp_pi->peer == bgp->peer_self
1885 && tmp_pi->type == ZEBRA_ROUTE_BGP
1886 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
342dd0c6 1887 break;
1888
40381db7 1889 *pi = tmp_pi;
342dd0c6 1890
1891 /* Mark route for delete. */
40381db7
DS
1892 if (tmp_pi)
1893 bgp_path_info_delete(rn, tmp_pi);
342dd0c6 1894}
1895
50f74cf1 1896
1897
1898/* Delete EVPN ES (type-4) route */
1899static int delete_evpn_type4_route(struct bgp *bgp,
1900 struct evpnes *es,
1901 struct prefix_evpn *p)
1902{
1903 afi_t afi = AFI_L2VPN;
1904 safi_t safi = SAFI_EVPN;
40381db7 1905 struct bgp_path_info *pi;
50f74cf1 1906 struct bgp_node *rn = NULL; /* rn in esi table */
1907 struct bgp_node *global_rn = NULL; /* rn in global table */
1908
1909 /* First, locate the route node within the ESI.
1910 * If it doesn't exist, ther is nothing to do.
1911 * Note: there is no RD here.
1912 */
1913 rn = bgp_node_lookup(es->route_table, (struct prefix *)p);
2bb9eff4 1914 if (!rn)
50f74cf1 1915 return 0;
1916
1917 /* Next, locate route node in the global EVPN routing table.
1918 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1919 */
1920 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
1921 (struct prefix *)p, &es->prd);
1922 if (global_rn) {
1923
1924 /* Delete route entry in the global EVPN table. */
40381db7 1925 delete_evpn_route_entry(bgp, afi, safi, global_rn, &pi);
50f74cf1 1926
1927 /* Schedule for processing - withdraws to peers happen from
1928 * this table.
1929 */
40381db7 1930 if (pi)
50f74cf1 1931 bgp_process(bgp, global_rn, afi, safi);
1932 bgp_unlock_node(global_rn);
1933 }
1934
1935 /*
1936 * Delete route entry in the ESI route table.
1937 * This can just be removed.
1938 */
40381db7
DS
1939 delete_evpn_route_entry(bgp, afi, safi, rn, &pi);
1940 if (pi)
1941 bgp_path_info_reap(rn, pi);
50f74cf1 1942 bgp_unlock_node(rn);
1943 return 0;
1944}
1945
342dd0c6 1946/* Delete EVPN type5 route */
996c9314 1947static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp)
342dd0c6 1948{
1949 afi_t afi = AFI_L2VPN;
1950 safi_t safi = SAFI_EVPN;
1951 struct bgp_node *rn = NULL;
40381db7 1952 struct bgp_path_info *pi = NULL;
5e53dce3 1953 struct bgp *bgp_evpn = NULL; /* evpn bgp instance */
342dd0c6 1954
5e53dce3
T
1955 bgp_evpn = bgp_get_evpn();
1956 if (!bgp_evpn)
faafdfa8 1957 return 0;
342dd0c6 1958
1959 /* locate the global route entry for this type-5 prefix */
5e53dce3 1960 rn = bgp_afi_node_lookup(bgp_evpn->rib[afi][safi], afi, safi,
342dd0c6 1961 (struct prefix *)evp, &bgp_vrf->vrf_prd);
1962 if (!rn)
1963 return 0;
1964
5e53dce3 1965 delete_evpn_route_entry(bgp_evpn, afi, safi, rn, &pi);
40381db7 1966 if (pi)
5e53dce3 1967 bgp_process(bgp_evpn, rn, afi, safi);
342dd0c6 1968 bgp_unlock_node(rn);
1969 return 0;
1970}
1971
128ea8ab 1972/*
1973 * Delete EVPN route (of type based on prefix) for specified VNI and
1974 * schedule for processing.
1975 */
d62a17ae 1976static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1977 struct prefix_evpn *p)
1978{
1979 struct bgp_node *rn, *global_rn;
40381db7 1980 struct bgp_path_info *pi;
d62a17ae 1981 afi_t afi = AFI_L2VPN;
1982 safi_t safi = SAFI_EVPN;
1983
1984 /* First, locate the route node within the VNI. If it doesn't exist,
1985 * there
1986 * is nothing further to do.
1987 */
1988 /* NOTE: There is no RD here. */
1989 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1990 if (!rn)
1991 return 0;
1992
1993 /* Next, locate route node in the global EVPN routing table. Note that
1994 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1995 * L3VPN routes.
1996 */
1997 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
1998 (struct prefix *)p, &vpn->prd);
1999 if (global_rn) {
2000 /* Delete route entry in the global EVPN table. */
40381db7 2001 delete_evpn_route_entry(bgp, afi, safi, global_rn, &pi);
d62a17ae 2002
2003 /* Schedule for processing - withdraws to peers happen from
2004 * this table.
2005 */
40381db7 2006 if (pi)
d62a17ae 2007 bgp_process(bgp, global_rn, afi, safi);
2008 bgp_unlock_node(global_rn);
2009 }
2010
2011 /* Delete route entry in the VNI route table. This can just be removed.
2012 */
40381db7 2013 delete_evpn_route_entry(bgp, afi, safi, rn, &pi);
3e3aa88e 2014 if (pi) {
40381db7 2015 bgp_path_info_reap(rn, pi);
3e3aa88e
AK
2016 evpn_route_select_install(bgp, vpn, rn);
2017 }
d62a17ae 2018 bgp_unlock_node(rn);
2019
2020 return 0;
128ea8ab 2021}
2022
2023/*
2024 * Update all type-2 (MACIP) local routes for this VNI - these should also
2025 * be scheduled for advertise to peers.
2026 */
d62a17ae 2027static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2028{
2029 afi_t afi;
2030 safi_t safi;
2031 struct bgp_node *rn;
40381db7 2032 struct bgp_path_info *pi, *tmp_pi;
d62a17ae 2033 struct attr attr;
d62a17ae 2034 struct attr *attr_new;
f07e1c99 2035 uint32_t seq;
1ec31309 2036 int add_l3_ecomm = 0;
d62a17ae 2037
2038 afi = AFI_L2VPN;
2039 safi = SAFI_EVPN;
d62a17ae 2040
2041 /* Walk this VNI's route table and update local type-2 routes. For any
2042 * routes updated, update corresponding entry in the global table too.
2043 */
2044 for (rn = bgp_table_top(vpn->route_table); rn;
2045 rn = bgp_route_next(rn)) {
2046 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2047 struct bgp_node *rd_rn;
40381db7 2048 struct bgp_path_info *global_pi;
d62a17ae 2049
2050 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2051 continue;
2052
f07e1c99 2053 /* Identify local route. */
6f94b685
DS
2054 for (tmp_pi = bgp_node_get_bgp_path_info(rn); tmp_pi;
2055 tmp_pi = tmp_pi->next) {
40381db7
DS
2056 if (tmp_pi->peer == bgp->peer_self
2057 && tmp_pi->type == ZEBRA_ROUTE_BGP
2058 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
f07e1c99 2059 break;
7ec156a9 2060 }
d62a17ae 2061
40381db7 2062 if (!tmp_pi)
d62a17ae 2063 continue;
2064
f07e1c99 2065 /*
2066 * Build attribute per local route as the MAC mobility and
2067 * some other values could differ for different routes. The
2068 * attributes will be shared in the hash table.
2069 */
2070 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
2071 attr.nexthop = vpn->originator_ip;
2072 attr.mp_nexthop_global_in = vpn->originator_ip;
2073 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
2074 bgpevpn_get_rmac(vpn, &attr.rmac);
2075
2076 if (evpn_route_is_sticky(bgp, rn))
2077 attr.sticky = 1;
2078 else if (evpn_route_is_def_gw(bgp, rn)) {
2079 attr.default_gw = 1;
2080 if (is_evpn_prefix_ipaddr_v6(evp))
2081 attr.router_flag = 1;
2082 }
2083
2084 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2085 * using L3 VNI for type-2 routes also.
2086 */
2087 if ((is_evpn_prefix_ipaddr_v4(evp) ||
9df2b997 2088 !IN6_IS_ADDR_LINKLOCAL(
2089 &evp->prefix.macip_addr.ip.ipaddr_v6)) &&
f07e1c99 2090 CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS) &&
2091 bgpevpn_get_l3vni(vpn))
2092 add_l3_ecomm = 1;
2093
2094 /* Set up extended community. */
2095 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
2096
40381db7 2097 seq = mac_mobility_seqnum(tmp_pi->attr);
f07e1c99 2098
2099 /* Update the route entry. */
40381db7
DS
2100 update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr, 0, &pi,
2101 0, seq);
f07e1c99 2102
d62a17ae 2103 /* Perform route selection; this is just to set the flags
f07e1c99 2104 * correctly as local route in the VNI always wins.
d62a17ae 2105 */
2106 evpn_route_select_install(bgp, vpn, rn);
2107
40381db7 2108 attr_new = pi->attr;
d62a17ae 2109
2110 /* Update route in global routing table. */
2111 rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
2112 (struct prefix *)evp, &vpn->prd);
2113 assert(rd_rn);
2114 update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0,
40381db7 2115 &global_pi, 0,
f07e1c99 2116 mac_mobility_seqnum(attr_new));
d62a17ae 2117
2118 /* Schedule for processing and unlock node. */
2119 bgp_process(bgp, rd_rn, afi, safi);
2120 bgp_unlock_node(rd_rn);
d62a17ae 2121
f07e1c99 2122 /* Unintern temporary. */
2123 aspath_unintern(&attr.aspath);
2124
2125 }
d62a17ae 2126
2127 return 0;
128ea8ab 2128}
2129
2130/*
2131 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2132 * global routing table. These are also scheduled for withdraw from peers.
2133 */
d62a17ae 2134static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2135{
d62a17ae 2136 afi_t afi;
2137 safi_t safi;
2138 struct bgp_node *rdrn, *rn;
2139 struct bgp_table *table;
40381db7 2140 struct bgp_path_info *pi;
128ea8ab 2141
d62a17ae 2142 afi = AFI_L2VPN;
2143 safi = SAFI_EVPN;
128ea8ab 2144
d62a17ae 2145 rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
6f94b685 2146 if (rdrn && bgp_node_has_bgp_path_info_data(rdrn)) {
67009e22 2147 table = bgp_node_get_bgp_table_info(rdrn);
d62a17ae 2148 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2149 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 2150
d62a17ae 2151 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2152 continue;
128ea8ab 2153
40381db7
DS
2154 delete_evpn_route_entry(bgp, afi, safi, rn, &pi);
2155 if (pi)
d62a17ae 2156 bgp_process(bgp, rn, afi, safi);
2157 }
2158 }
128ea8ab 2159
d62a17ae 2160 /* Unlock RD node. */
2161 if (rdrn)
2162 bgp_unlock_node(rdrn);
128ea8ab 2163
d62a17ae 2164 return 0;
128ea8ab 2165}
2166
2167/*
2168 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2169 * table as well as the per-VNI route table.
2170 */
d62a17ae 2171static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2172{
d62a17ae 2173 afi_t afi;
2174 safi_t safi;
2175 struct bgp_node *rn;
40381db7 2176 struct bgp_path_info *pi;
128ea8ab 2177
d62a17ae 2178 afi = AFI_L2VPN;
2179 safi = SAFI_EVPN;
128ea8ab 2180
d62a17ae 2181 /* First, walk the global route table for this VNI's type-2 local
2182 * routes.
2183 * EVPN routes are a 2-level table, first get the RD table.
2184 */
2185 delete_global_type2_routes(bgp, vpn);
128ea8ab 2186
d62a17ae 2187 /* Next, walk this VNI's route table and delete local type-2 routes. */
2188 for (rn = bgp_table_top(vpn->route_table); rn;
2189 rn = bgp_route_next(rn)) {
2190 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 2191
d62a17ae 2192 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2193 continue;
128ea8ab 2194
40381db7 2195 delete_evpn_route_entry(bgp, afi, safi, rn, &pi);
128ea8ab 2196
d62a17ae 2197 /* Route entry in local table gets deleted immediately. */
40381db7
DS
2198 if (pi)
2199 bgp_path_info_reap(rn, pi);
d62a17ae 2200 }
128ea8ab 2201
d62a17ae 2202 return 0;
128ea8ab 2203}
2204
50f74cf1 2205/*
2206 * Delete all routes in per ES route-table
2207 */
2208static int delete_all_es_routes(struct bgp *bgp, struct evpnes *es)
2209{
2210 struct bgp_node *rn;
40381db7 2211 struct bgp_path_info *pi, *nextpi;
50f74cf1 2212
2213 /* Walk this ES's route table and delete all routes. */
2214 for (rn = bgp_table_top(es->route_table); rn;
2215 rn = bgp_route_next(rn)) {
6f94b685
DS
2216 for (pi = bgp_node_get_bgp_path_info(rn);
2217 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
40381db7
DS
2218 bgp_path_info_delete(rn, pi);
2219 bgp_path_info_reap(rn, pi);
50f74cf1 2220 }
2221 }
2222
2223 return 0;
2224}
2225
128ea8ab 2226/*
2227 * Delete all routes in the per-VNI route table.
2228 */
d62a17ae 2229static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2230{
d62a17ae 2231 struct bgp_node *rn;
40381db7 2232 struct bgp_path_info *pi, *nextpi;
128ea8ab 2233
d62a17ae 2234 /* Walk this VNI's route table and delete all routes. */
2235 for (rn = bgp_table_top(vpn->route_table); rn;
2236 rn = bgp_route_next(rn)) {
6f94b685
DS
2237 for (pi = bgp_node_get_bgp_path_info(rn);
2238 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
40381db7
DS
2239 bgp_path_info_delete(rn, pi);
2240 bgp_path_info_reap(rn, pi);
d62a17ae 2241 }
2242 }
128ea8ab 2243
d62a17ae 2244 return 0;
128ea8ab 2245}
2246
833b8a50
AK
2247/* BUM traffic flood mode per-l2-vni */
2248static int bgp_evpn_vni_flood_mode_get(struct bgp *bgp,
2249 struct bgpevpn *vpn)
2250{
2251 /* if flooding has been globally disabled per-vni mode is
2252 * not relevant
2253 */
2254 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
2255 return VXLAN_FLOOD_DISABLED;
2256
2257 /* if mcast group ip has been specified we use a PIM-SM MDT */
2258 if (vpn->mcast_grp.s_addr != INADDR_ANY)
2259 return VXLAN_FLOOD_PIM_SM;
2260
2261 /* default is ingress replication */
2262 return VXLAN_FLOOD_HEAD_END_REPL;
2263}
2264
128ea8ab 2265/*
2266 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2267 * export RT getting modified or change to tunnel IP. Note that these
2268 * situations need the route in the per-VNI table as well as the global
2269 * table to be updated (as attributes change).
2270 */
d62a17ae 2271static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2272{
d62a17ae 2273 int ret;
2274 struct prefix_evpn p;
128ea8ab 2275
d62a17ae 2276 /* Update and advertise the type-3 route (only one) followed by the
2277 * locally learnt type-2 routes (MACIP) - for this VNI.
fd069644
DS
2278 *
2279 * RT-3 only if doing head-end replication
d62a17ae 2280 */
833b8a50
AK
2281 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
2282 == VXLAN_FLOOD_HEAD_END_REPL) {
fd069644
DS
2283 build_evpn_type3_prefix(&p, vpn->originator_ip);
2284 ret = update_evpn_route(bgp, vpn, &p, 0, 0);
2285 if (ret)
2286 return ret;
2287 }
128ea8ab 2288
d62a17ae 2289 return update_all_type2_routes(bgp, vpn);
128ea8ab 2290}
2291
50f74cf1 2292/* Delete (and withdraw) local routes for specified ES from global and ES table.
2293 * Also remove all other routes from the per ES table.
2294 * Invoked when ES is deleted.
2295 */
2296static int delete_routes_for_es(struct bgp *bgp, struct evpnes *es)
2297{
2298 int ret;
2299 char buf[ESI_STR_LEN];
2300 struct prefix_evpn p;
2301
2302 /* Delete and withdraw locally learnt ES route */
2303 build_evpn_type4_prefix(&p, &es->esi, es->originator_ip.ipaddr_v4);
2304 ret = delete_evpn_type4_route(bgp, es, &p);
2305 if (ret) {
e50f7cfd 2306 flog_err(EC_BGP_EVPN_ROUTE_DELETE,
1c50c1c0
QY
2307 "%u failed to delete type-4 route for ESI %s",
2308 bgp->vrf_id, esi_to_str(&es->esi, buf, sizeof(buf)));
50f74cf1 2309 }
2310
2311 /* Delete all routes from per ES table */
2312 return delete_all_es_routes(bgp, es);
2313}
2314
128ea8ab 2315/*
2316 * Delete (and withdraw) local routes for specified VNI from the global
2317 * table and per-VNI table. After this, remove all other routes from
2318 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2319 * (advertise-all-vni) being disabled.
2320 */
d62a17ae 2321static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2322{
d62a17ae 2323 int ret;
2324 struct prefix_evpn p;
128ea8ab 2325
d62a17ae 2326 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2327 * followed by type-3 routes (only one) - for this VNI.
2328 */
2329 ret = delete_all_type2_routes(bgp, vpn);
2330 if (ret)
2331 return ret;
128ea8ab 2332
d62a17ae 2333 build_evpn_type3_prefix(&p, vpn->originator_ip);
2334 ret = delete_evpn_route(bgp, vpn, &p);
2335 if (ret)
2336 return ret;
128ea8ab 2337
d62a17ae 2338 /* Delete all routes from the per-VNI table. */
2339 return delete_all_vni_routes(bgp, vpn);
128ea8ab 2340}
2341
76d07c7a
AK
2342/*
2343 * There is a flood mcast IP address change. Update the mcast-grp and
2344 * remove the type-3 route if any. A new type-3 route will be generated
2345 * post tunnel_ip update if the new flood mode is head-end-replication.
2346 */
2347static int bgp_evpn_mcast_grp_change(struct bgp *bgp, struct bgpevpn *vpn,
2348 struct in_addr mcast_grp)
2349{
2350 struct prefix_evpn p;
2351
2352 vpn->mcast_grp = mcast_grp;
2353
2354 if (is_vni_live(vpn)) {
2355 build_evpn_type3_prefix(&p, vpn->originator_ip);
2356 delete_evpn_route(bgp, vpn, &p);
2357 }
2358
2359 return 0;
2360}
2361
128ea8ab 2362/*
d1911c26 2363 * There is a tunnel endpoint IP address change for this VNI, delete
2364 * prior type-3 route (if needed) and update.
2365 * Note: Route re-advertisement happens elsewhere after other processing
2366 * other changes.
128ea8ab 2367 */
d62a17ae 2368static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
2369 struct in_addr originator_ip)
128ea8ab 2370{
d62a17ae 2371 struct prefix_evpn p;
128ea8ab 2372
ddd16ed5
MK
2373 /* If VNI is not live, we only need to update the originator ip */
2374 if (!is_vni_live(vpn)) {
2375 vpn->originator_ip = originator_ip;
2376 return 0;
2377 }
2378
db0e1937
MK
2379 /* Update the tunnel-ip hash */
2380 bgp_tip_del(bgp, &vpn->originator_ip);
2381 bgp_tip_add(bgp, &originator_ip);
2382
2383 /* filter routes as martian nexthop db has changed */
2384 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
2385
d62a17ae 2386 /* Need to withdraw type-3 route as the originator IP is part
2387 * of the key.
2388 */
2389 build_evpn_type3_prefix(&p, vpn->originator_ip);
2390 delete_evpn_route(bgp, vpn, &p);
128ea8ab 2391
d62a17ae 2392 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2393 vpn->originator_ip = originator_ip;
d1911c26 2394 return 0;
128ea8ab 2395}
2396
50f74cf1 2397/* Install EVPN route entry in ES */
4b7e6066 2398static int install_evpn_route_entry_in_es(struct bgp *bgp, struct evpnes *es,
50f74cf1 2399 struct prefix_evpn *p,
40381db7 2400 struct bgp_path_info *parent_pi)
50f74cf1 2401{
2402 int ret = 0;
2403 struct bgp_node *rn = NULL;
40381db7 2404 struct bgp_path_info *pi = NULL;
50f74cf1 2405 struct attr *attr_new = NULL;
2406
2407 /* Create (or fetch) route within the VNI.
2408 * NOTE: There is no RD here.
2409 */
2410 rn = bgp_node_get(es->route_table, (struct prefix *)p);
2411
2412 /* Check if route entry is already present. */
6f94b685 2413 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next)
40381db7
DS
2414 if (pi->extra
2415 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
50f74cf1 2416 break;
2417
40381db7 2418 if (!pi) {
50f74cf1 2419 /* Add (or update) attribute to hash. */
40381db7 2420 attr_new = bgp_attr_intern(parent_pi->attr);
50f74cf1 2421
2422 /* Create new route with its attribute. */
40381db7
DS
2423 pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0,
2424 parent_pi->peer, attr_new, rn);
2425 SET_FLAG(pi->flags, BGP_PATH_VALID);
2426 bgp_path_info_extra_get(pi);
2427 pi->extra->parent = parent_pi;
2428 bgp_path_info_add(rn, pi);
50f74cf1 2429 } else {
40381db7
DS
2430 if (attrhash_cmp(pi->attr, parent_pi->attr)
2431 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
50f74cf1 2432 bgp_unlock_node(rn);
2433 return 0;
2434 }
2435 /* The attribute has changed. */
2436 /* Add (or update) attribute to hash. */
40381db7 2437 attr_new = bgp_attr_intern(parent_pi->attr);
50f74cf1 2438
2439 /* Restore route, if needed. */
40381db7
DS
2440 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
2441 bgp_path_info_restore(rn, pi);
50f74cf1 2442
2443 /* Mark if nexthop has changed. */
40381db7
DS
2444 if (!IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
2445 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
50f74cf1 2446
2447 /* Unintern existing, set to new. */
40381db7
DS
2448 bgp_attr_unintern(&pi->attr);
2449 pi->attr = attr_new;
2450 pi->uptime = bgp_clock();
50f74cf1 2451 }
2452
2453 /* Perform route selection and update zebra, if required. */
2454 ret = evpn_es_route_select_install(bgp, es, rn);
2455 return ret;
2456}
2457
d3135ba3 2458/*
2459 * Install route entry into the VRF routing table and invoke route selection.
2460 */
2461static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2462 struct prefix_evpn *evp,
40381db7 2463 struct bgp_path_info *parent_pi)
d3135ba3 2464{
2465 struct bgp_node *rn;
40381db7 2466 struct bgp_path_info *pi;
1ec31309 2467 struct attr attr;
d3135ba3 2468 struct attr *attr_new;
c4edf708 2469 int ret = 0;
d3135ba3 2470 struct prefix p;
2471 struct prefix *pp = &p;
2472 afi_t afi = 0;
2473 safi_t safi = 0;
1eb88002
MK
2474 char buf[PREFIX_STRLEN];
2475 char buf1[PREFIX_STRLEN];
d3135ba3 2476
2477 memset(pp, 0, sizeof(struct prefix));
3714a385 2478 ip_prefix_from_evpn_prefix(evp, pp);
d3135ba3 2479
1eb88002 2480 if (bgp_debug_zebra(NULL)) {
996c9314 2481 zlog_debug(
1ee069db 2482 "import evpn prefix %s as ip prefix %s in vrf %s",
996c9314
LB
2483 prefix2str(evp, buf, sizeof(buf)),
2484 prefix2str(pp, buf1, sizeof(buf)),
2485 vrf_id_to_name(bgp_vrf->vrf_id));
1eb88002
MK
2486 }
2487
d3135ba3 2488 /* Create (or fetch) route within the VRF. */
2489 /* NOTE: There is no RD here. */
3714a385 2490 if (is_evpn_prefix_ipaddr_v4(evp)) {
d3135ba3 2491 afi = AFI_IP;
2492 safi = SAFI_UNICAST;
2493 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
3714a385 2494 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
d3135ba3 2495 afi = AFI_IP6;
2496 safi = SAFI_UNICAST;
2497 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
2498 } else
2499 return 0;
2500
1ec31309 2501 /* EVPN routes currently only support a IPv4 next hop which corresponds
2502 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
450e362d 2503 * or prefix route, we have to convert the next hop to an IPv4-mapped
2504 * address for the rest of the code to flow through. In the case of IPv4,
2505 * make sure to set the flag for next hop attribute.
1ec31309 2506 */
40381db7 2507 bgp_attr_dup(&attr, parent_pi->attr);
1ec31309 2508 if (afi == AFI_IP6)
2509 evpn_convert_nexthop_to_ipv6(&attr);
450e362d 2510 else
2511 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
1ec31309 2512
d3135ba3 2513 /* Check if route entry is already present. */
6f94b685 2514 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next)
40381db7
DS
2515 if (pi->extra
2516 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
d3135ba3 2517 break;
2518
40381db7 2519 if (!pi) {
d3135ba3 2520 /* Add (or update) attribute to hash. */
1ec31309 2521 attr_new = bgp_attr_intern(&attr);
d3135ba3 2522
2523 /* Create new route with its attribute. */
40381db7
DS
2524 pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0,
2525 parent_pi->peer, attr_new, rn);
2526 SET_FLAG(pi->flags, BGP_PATH_VALID);
2527 bgp_path_info_extra_get(pi);
2528 pi->extra->parent = bgp_path_info_lock(parent_pi);
2529 bgp_lock_node((struct bgp_node *)parent_pi->net);
2530 if (parent_pi->extra) {
2531 memcpy(&pi->extra->label, &parent_pi->extra->label,
2532 sizeof(pi->extra->label));
2533 pi->extra->num_labels = parent_pi->extra->num_labels;
b57ba6d2 2534 }
40381db7 2535 bgp_path_info_add(rn, pi);
d3135ba3 2536 } else {
40381db7
DS
2537 if (attrhash_cmp(pi->attr, &attr)
2538 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
d3135ba3 2539 bgp_unlock_node(rn);
2540 return 0;
2541 }
2542 /* The attribute has changed. */
2543 /* Add (or update) attribute to hash. */
1ec31309 2544 attr_new = bgp_attr_intern(&attr);
d3135ba3 2545
2546 /* Restore route, if needed. */
40381db7
DS
2547 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
2548 bgp_path_info_restore(rn, pi);
d3135ba3 2549
2550 /* Mark if nexthop has changed. */
40381db7
DS
2551 if ((afi == AFI_IP
2552 && !IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
2553 || (afi == AFI_IP6
2554 && !IPV6_ADDR_SAME(&pi->attr->mp_nexthop_global,
2555 &attr_new->mp_nexthop_global)))
2556 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
d3135ba3 2557
a9f8ad9f 2558 bgp_path_info_set_flag(rn, pi, BGP_PATH_ATTR_CHANGED);
d3135ba3 2559 /* Unintern existing, set to new. */
40381db7
DS
2560 bgp_attr_unintern(&pi->attr);
2561 pi->attr = attr_new;
2562 pi->uptime = bgp_clock();
d3135ba3 2563 }
2564
40381db7 2565 bgp_aggregate_increment(bgp_vrf, &rn->p, pi, afi, safi);
b49cdf4c 2566
d3135ba3 2567 /* Perform route selection and update zebra, if required. */
1eb88002 2568 bgp_process(bgp_vrf, rn, afi, safi);
d3135ba3 2569
7452e879 2570 /* Process for route leaking. */
2571 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf, pi);
2572
d3135ba3 2573 return ret;
2574}
2575
128ea8ab 2576/*
2577 * Install route entry into the VNI routing table and invoke route selection.
2578 */
d62a17ae 2579static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2580 struct prefix_evpn *p,
40381db7 2581 struct bgp_path_info *parent_pi)
d62a17ae 2582{
2583 struct bgp_node *rn;
40381db7 2584 struct bgp_path_info *pi;
d62a17ae 2585 struct attr *attr_new;
2586 int ret;
2587
2588 /* Create (or fetch) route within the VNI. */
2589 /* NOTE: There is no RD here. */
2590 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
2591
2592 /* Check if route entry is already present. */
6f94b685 2593 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next)
40381db7
DS
2594 if (pi->extra
2595 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
d62a17ae 2596 break;
2597
40381db7 2598 if (!pi) {
d62a17ae 2599 /* Add (or update) attribute to hash. */
40381db7 2600 attr_new = bgp_attr_intern(parent_pi->attr);
d62a17ae 2601
2602 /* Create new route with its attribute. */
40381db7
DS
2603 pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0,
2604 parent_pi->peer, attr_new, rn);
2605 SET_FLAG(pi->flags, BGP_PATH_VALID);
2606 bgp_path_info_extra_get(pi);
2607 pi->extra->parent = bgp_path_info_lock(parent_pi);
2608 bgp_lock_node((struct bgp_node *)parent_pi->net);
2609 if (parent_pi->extra) {
2610 memcpy(&pi->extra->label, &parent_pi->extra->label,
2611 sizeof(pi->extra->label));
2612 pi->extra->num_labels = parent_pi->extra->num_labels;
b57ba6d2 2613 }
40381db7 2614 bgp_path_info_add(rn, pi);
d62a17ae 2615 } else {
40381db7
DS
2616 if (attrhash_cmp(pi->attr, parent_pi->attr)
2617 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
d62a17ae 2618 bgp_unlock_node(rn);
2619 return 0;
2620 }
2621 /* The attribute has changed. */
2622 /* Add (or update) attribute to hash. */
40381db7 2623 attr_new = bgp_attr_intern(parent_pi->attr);
d62a17ae 2624
50f74cf1 2625 /* Restore route, if needed. */
40381db7
DS
2626 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
2627 bgp_path_info_restore(rn, pi);
50f74cf1 2628
2629 /* Mark if nexthop has changed. */
40381db7
DS
2630 if (!IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
2631 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
50f74cf1 2632
2633 /* Unintern existing, set to new. */
40381db7
DS
2634 bgp_attr_unintern(&pi->attr);
2635 pi->attr = attr_new;
2636 pi->uptime = bgp_clock();
50f74cf1 2637 }
2638
2639 /* Perform route selection and update zebra, if required. */
2640 ret = evpn_route_select_install(bgp, vpn, rn);
2641
2642 return ret;
2643}
2644
2645/* Uninstall EVPN route entry from ES route table */
4b7e6066 2646static int uninstall_evpn_route_entry_in_es(struct bgp *bgp, struct evpnes *es,
50f74cf1 2647 struct prefix_evpn *p,
40381db7 2648 struct bgp_path_info *parent_pi)
50f74cf1 2649{
2650 int ret;
2651 struct bgp_node *rn;
40381db7 2652 struct bgp_path_info *pi;
50f74cf1 2653
2654 if (!es->route_table)
2655 return 0;
2656
2657 /* Locate route within the ESI.
2658 * NOTE: There is no RD here.
2659 */
2660 rn = bgp_node_lookup(es->route_table, (struct prefix *)p);
2661 if (!rn)
2662 return 0;
2663
2664 /* Find matching route entry. */
6f94b685 2665 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next)
40381db7
DS
2666 if (pi->extra
2667 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
50f74cf1 2668 break;
d62a17ae 2669
40381db7 2670 if (!pi)
50f74cf1 2671 return 0;
d62a17ae 2672
50f74cf1 2673 /* Mark entry for deletion */
40381db7 2674 bgp_path_info_delete(rn, pi);
d62a17ae 2675
2676 /* Perform route selection and update zebra, if required. */
50f74cf1 2677 ret = evpn_es_route_select_install(bgp, es, rn);
2678
2679 /* Unlock route node. */
2680 bgp_unlock_node(rn);
d62a17ae 2681
2682 return ret;
128ea8ab 2683}
2684
d3135ba3 2685/*
2686 * Uninstall route entry from the VRF routing table and send message
2687 * to zebra, if appropriate.
2688 */
2689static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2690 struct prefix_evpn *evp,
40381db7 2691 struct bgp_path_info *parent_pi)
d3135ba3 2692{
2693 struct bgp_node *rn;
40381db7 2694 struct bgp_path_info *pi;
c4edf708 2695 int ret = 0;
d3135ba3 2696 struct prefix p;
2697 struct prefix *pp = &p;
2698 afi_t afi = 0;
2699 safi_t safi = 0;
1eb88002
MK
2700 char buf[PREFIX_STRLEN];
2701 char buf1[PREFIX_STRLEN];
d3135ba3 2702
2703 memset(pp, 0, sizeof(struct prefix));
3714a385 2704 ip_prefix_from_evpn_prefix(evp, pp);
d3135ba3 2705
1eb88002 2706 if (bgp_debug_zebra(NULL)) {
996c9314
LB
2707 zlog_debug(
2708 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2709 prefix2str(evp, buf, sizeof(buf)),
2710 prefix2str(pp, buf1, sizeof(buf)),
2711 vrf_id_to_name(bgp_vrf->vrf_id));
1eb88002
MK
2712 }
2713
d3135ba3 2714 /* Locate route within the VRF. */
2715 /* NOTE: There is no RD here. */
3714a385 2716 if (is_evpn_prefix_ipaddr_v4(evp)) {
d3135ba3 2717 afi = AFI_IP;
2718 safi = SAFI_UNICAST;
2719 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2720 } else {
2721 afi = AFI_IP6;
2722 safi = SAFI_UNICAST;
2723 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2724 }
2725
2726 if (!rn)
2727 return 0;
2728
2729 /* Find matching route entry. */
6f94b685 2730 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next)
40381db7
DS
2731 if (pi->extra
2732 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
d3135ba3 2733 break;
2734
40381db7 2735 if (!pi)
d3135ba3 2736 return 0;
2737
7452e879 2738 /* Process for route leaking. */
2739 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf, pi);
2740
40381db7 2741 bgp_aggregate_decrement(bgp_vrf, &rn->p, pi, afi, safi);
4c7a11d5 2742
d3135ba3 2743 /* Mark entry for deletion */
40381db7 2744 bgp_path_info_delete(rn, pi);
d3135ba3 2745
2746 /* Perform route selection and update zebra, if required. */
1eb88002 2747 bgp_process(bgp_vrf, rn, afi, safi);
d3135ba3 2748
2749 /* Unlock route node. */
2750 bgp_unlock_node(rn);
2751
2752 return ret;
2753}
2754
128ea8ab 2755/*
2756 * Uninstall route entry from the VNI routing table and send message
2757 * to zebra, if appropriate.
2758 */
d62a17ae 2759static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2760 struct prefix_evpn *p,
40381db7 2761 struct bgp_path_info *parent_pi)
128ea8ab 2762{
d62a17ae 2763 struct bgp_node *rn;
40381db7 2764 struct bgp_path_info *pi;
d62a17ae 2765 int ret;
128ea8ab 2766
d62a17ae 2767 /* Locate route within the VNI. */
2768 /* NOTE: There is no RD here. */
2769 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
2770 if (!rn)
2771 return 0;
128ea8ab 2772
d62a17ae 2773 /* Find matching route entry. */
6f94b685 2774 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next)
40381db7
DS
2775 if (pi->extra
2776 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
d62a17ae 2777 break;
128ea8ab 2778
40381db7 2779 if (!pi)
d62a17ae 2780 return 0;
128ea8ab 2781
d62a17ae 2782 /* Mark entry for deletion */
40381db7 2783 bgp_path_info_delete(rn, pi);
128ea8ab 2784
d62a17ae 2785 /* Perform route selection and update zebra, if required. */
2786 ret = evpn_route_select_install(bgp, vpn, rn);
128ea8ab 2787
d62a17ae 2788 /* Unlock route node. */
2789 bgp_unlock_node(rn);
128ea8ab 2790
d62a17ae 2791 return ret;
128ea8ab 2792}
2793
50f74cf1 2794/*
2795 * Given a prefix, see if it belongs to ES.
2796 */
2797static int is_prefix_matching_for_es(struct prefix_evpn *p,
2798 struct evpnes *es)
2799{
2800 /* if not an ES route return false */
2801 if (p->prefix.route_type != BGP_EVPN_ES_ROUTE)
2802 return 0;
2803
2804 if (memcmp(&p->prefix.es_addr.esi, &es->esi, sizeof(esi_t)) == 0)
2805 return 1;
2806
2807 return 0;
2808}
2809
5ba238b7
MK
2810/*
2811 * Given a route entry and a VRF, see if this route entry should be
2812 * imported into the VRF i.e., RTs match.
2813 */
4b7e6066 2814static int is_route_matching_for_vrf(struct bgp *bgp_vrf,
40381db7 2815 struct bgp_path_info *pi)
5ba238b7 2816{
40381db7 2817 struct attr *attr = pi->attr;
5ba238b7
MK
2818 struct ecommunity *ecom;
2819 int i;
2820
2821 assert(attr);
2822 /* Route should have valid RT to be even considered. */
2823 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2824 return 0;
2825
2826 ecom = attr->ecommunity;
2827 if (!ecom || !ecom->size)
2828 return 0;
2829
2830 /* For each extended community RT, see if it matches this VNI. If any RT
2831 * matches, we're done.
2832 */
2833 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
2834 uint8_t *pnt;
2835 uint8_t type, sub_type;
5ba238b7
MK
2836 struct ecommunity_val *eval;
2837 struct ecommunity_val eval_tmp;
2838 struct vrf_irt_node *irt;
2839
2840 /* Only deal with RTs */
2841 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2842 eval = (struct ecommunity_val *)(ecom->val
2843 + (i * ECOMMUNITY_SIZE));
2844 type = *pnt++;
2845 sub_type = *pnt++;
2846 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2847 continue;
2848
2849 /* See if this RT matches specified VNIs import RTs */
2850 irt = lookup_vrf_import_rt(eval);
5d9cbca2 2851 if (irt)
5ba238b7
MK
2852 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2853 return 1;
2854
2855 /* Also check for non-exact match. In this, we mask out the AS
2856 * and
2857 * only check on the local-admin sub-field. This is to
2858 * facilitate using
2859 * VNI as the RT for EBGP peering too.
2860 */
2861 irt = NULL;
2862 if (type == ECOMMUNITY_ENCODE_AS
2863 || type == ECOMMUNITY_ENCODE_AS4
2864 || type == ECOMMUNITY_ENCODE_IP) {
2865 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2866 mask_ecom_global_admin(&eval_tmp, eval);
2867 irt = lookup_vrf_import_rt(&eval_tmp);
2868 }
5d9cbca2 2869 if (irt)
5ba238b7
MK
2870 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2871 return 1;
2872 }
2873
2874 return 0;
2875}
2876
128ea8ab 2877/*
2878 * Given a route entry and a VNI, see if this route entry should be
2879 * imported into the VNI i.e., RTs match.
2880 */
d62a17ae 2881static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
40381db7 2882 struct bgp_path_info *pi)
d62a17ae 2883{
40381db7 2884 struct attr *attr = pi->attr;
d62a17ae 2885 struct ecommunity *ecom;
2886 int i;
2887
2888 assert(attr);
2889 /* Route should have valid RT to be even considered. */
2890 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2891 return 0;
2892
2893 ecom = attr->ecommunity;
2894 if (!ecom || !ecom->size)
2895 return 0;
2896
2897 /* For each extended community RT, see if it matches this VNI. If any RT
2898 * matches, we're done.
2899 */
2900 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
2901 uint8_t *pnt;
2902 uint8_t type, sub_type;
d62a17ae 2903 struct ecommunity_val *eval;
2904 struct ecommunity_val eval_tmp;
2905 struct irt_node *irt;
2906
2907 /* Only deal with RTs */
2908 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2909 eval = (struct ecommunity_val *)(ecom->val
2910 + (i * ECOMMUNITY_SIZE));
2911 type = *pnt++;
2912 sub_type = *pnt++;
2913 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2914 continue;
2915
2916 /* See if this RT matches specified VNIs import RTs */
2917 irt = lookup_import_rt(bgp, eval);
b1ab0dfe 2918 if (irt)
d62a17ae 2919 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2920 return 1;
2921
2922 /* Also check for non-exact match. In this, we mask out the AS
2923 * and
2924 * only check on the local-admin sub-field. This is to
2925 * facilitate using
2926 * VNI as the RT for EBGP peering too.
2927 */
2928 irt = NULL;
2929 if (type == ECOMMUNITY_ENCODE_AS
2930 || type == ECOMMUNITY_ENCODE_AS4
2931 || type == ECOMMUNITY_ENCODE_IP) {
2932 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2933 mask_ecom_global_admin(&eval_tmp, eval);
2934 irt = lookup_import_rt(bgp, &eval_tmp);
2935 }
b1ab0dfe 2936 if (irt)
d62a17ae 2937 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2938 return 1;
2939 }
2940
2941 return 0;
128ea8ab 2942}
2943
50f74cf1 2944static int install_uninstall_routes_for_es(struct bgp *bgp,
2945 struct evpnes *es,
2946 int install)
2947{
2948 int ret;
2949 afi_t afi;
2950 safi_t safi;
2951 char buf[PREFIX_STRLEN];
2952 char buf1[ESI_STR_LEN];
2953 struct bgp_node *rd_rn, *rn;
2954 struct bgp_table *table;
40381db7 2955 struct bgp_path_info *pi;
50f74cf1 2956
2957 afi = AFI_L2VPN;
2958 safi = SAFI_EVPN;
2959
2bb9eff4
DS
2960 /*
2961 * Walk entire global routing table and evaluate routes which could be
50f74cf1 2962 * imported into this VRF. Note that we need to loop through all global
2963 * routes to determine which route matches the import rt on vrf
2964 */
2965 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
2966 rd_rn = bgp_route_next(rd_rn)) {
67009e22 2967 table = bgp_node_get_bgp_table_info(rd_rn);
50f74cf1 2968 if (!table)
2969 continue;
2970
2971 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2972 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2973
6f94b685
DS
2974 for (pi = bgp_node_get_bgp_path_info(rn); pi;
2975 pi = pi->next) {
2bb9eff4
DS
2976 /*
2977 * Consider "valid" remote routes applicable for
50f74cf1 2978 * this ES.
2979 */
40381db7
DS
2980 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
2981 && pi->type == ZEBRA_ROUTE_BGP
2982 && pi->sub_type == BGP_ROUTE_NORMAL))
50f74cf1 2983 continue;
2984
2bb9eff4
DS
2985 if (!is_prefix_matching_for_es(evp, es))
2986 continue;
50f74cf1 2987
2bb9eff4
DS
2988 if (install)
2989 ret = install_evpn_route_entry_in_es(
40381db7 2990 bgp, es, evp, pi);
2bb9eff4
DS
2991 else
2992 ret = uninstall_evpn_route_entry_in_es(
40381db7 2993 bgp, es, evp, pi);
2bb9eff4
DS
2994
2995 if (ret) {
af4c2728 2996 flog_err(
e50f7cfd 2997 EC_BGP_EVPN_FAIL,
2bb9eff4
DS
2998 "Failed to %s EVPN %s route in ESI %s",
2999 install ? "install"
3000 : "uninstall",
3001 prefix2str(evp, buf,
3002 sizeof(buf)),
3003 esi_to_str(&es->esi, buf1,
3004 sizeof(buf1)));
3005 return ret;
50f74cf1 3006 }
3007 }
3008 }
3009 }
3010 return 0;
3011}
3012
47bf0432
CS
3013/* This API will scan evpn routes for checking attribute's rmac
3014 * macthes with bgp instance router mac. It avoid installing
3015 * route into bgp vrf table and remote rmac in bridge table.
3016 */
3017static int bgp_evpn_route_rmac_self_check(struct bgp *bgp_vrf,
3018 struct prefix_evpn *evp,
3019 struct bgp_path_info *pi)
3020{
3021 /* evpn route could have learnt prior to L3vni has come up,
3022 * perform rmac check before installing route and
3023 * remote router mac.
3024 * The route will be removed from global bgp table once
3025 * SVI comes up with MAC and stored in hash, triggers
3026 * bgp_mac_rescan_all_evpn_tables.
3027 */
3028 if (pi->attr &&
3029 memcmp(&bgp_vrf->rmac, &pi->attr->rmac, ETH_ALEN) == 0) {
3030 if (bgp_debug_update(pi->peer, NULL, NULL, 1)) {
3031 char buf1[PREFIX_STRLEN];
3032 char attr_str[BUFSIZ] = {0};
3033
3034 bgp_dump_attr(pi->attr, attr_str, BUFSIZ);
3035
3036 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
3037 __func__, bgp_vrf->vrf_id,
3038 prefix2str(evp, buf1, sizeof(buf1)),
3039 attr_str);
3040 }
3041
3042 return 1;
3043 }
3044
3045 return 0;
3046}
3047
5ba238b7
MK
3048/*
3049 * Install or uninstall mac-ip routes are appropriate for this
3050 * particular VRF.
3051 */
996c9314 3052static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
5ba238b7
MK
3053{
3054 afi_t afi;
3055 safi_t safi;
3056 struct bgp_node *rd_rn, *rn;
3057 struct bgp_table *table;
40381db7 3058 struct bgp_path_info *pi;
5ba238b7
MK
3059 int ret;
3060 char buf[PREFIX_STRLEN];
5e53dce3 3061 struct bgp *bgp_evpn = NULL;
5ba238b7
MK
3062
3063 afi = AFI_L2VPN;
3064 safi = SAFI_EVPN;
5e53dce3
T
3065 bgp_evpn = bgp_get_evpn();
3066 if (!bgp_evpn)
5ba238b7
MK
3067 return -1;
3068
3069 /* Walk entire global routing table and evaluate routes which could be
3070 * imported into this VRF. Note that we need to loop through all global
3071 * routes to determine which route matches the import rt on vrf
3072 */
5e53dce3 3073 for (rd_rn = bgp_table_top(bgp_evpn->rib[afi][safi]); rd_rn;
5ba238b7 3074 rd_rn = bgp_route_next(rd_rn)) {
67009e22 3075 table = bgp_node_get_bgp_table_info(rd_rn);
5ba238b7
MK
3076 if (!table)
3077 continue;
3078
3079 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
3080 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
3081
1eb88002 3082 /* if not mac-ip route skip this route */
996c9314
LB
3083 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3084 || evp->prefix.route_type
3085 == BGP_EVPN_IP_PREFIX_ROUTE))
5ba238b7
MK
3086 continue;
3087
1eb88002 3088 /* if not a mac+ip route skip this route */
3714a385 3089 if (!(is_evpn_prefix_ipaddr_v4(evp)
3090 || is_evpn_prefix_ipaddr_v6(evp)))
1eb88002
MK
3091 continue;
3092
6f94b685
DS
3093 for (pi = bgp_node_get_bgp_path_info(rn); pi;
3094 pi = pi->next) {
5ba238b7 3095 /* Consider "valid" remote routes applicable for
523cafc4 3096 * this VRF.
3097 */
40381db7
DS
3098 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3099 && pi->type == ZEBRA_ROUTE_BGP
3100 && pi->sub_type == BGP_ROUTE_NORMAL))
5ba238b7
MK
3101 continue;
3102
40381db7 3103 if (is_route_matching_for_vrf(bgp_vrf, pi)) {
47bf0432
CS
3104 if (bgp_evpn_route_rmac_self_check(
3105 bgp_vrf, evp, pi))
3106 continue;
3107
5ba238b7 3108 if (install)
996c9314 3109 ret = install_evpn_route_entry_in_vrf(
40381db7 3110 bgp_vrf, evp, pi);
5ba238b7 3111 else
996c9314 3112 ret = uninstall_evpn_route_entry_in_vrf(
40381db7 3113 bgp_vrf, evp, pi);
5ba238b7
MK
3114
3115 if (ret) {
af4c2728 3116 flog_err(
e50f7cfd 3117 EC_BGP_EVPN_FAIL,
5ba238b7
MK
3118 "Failed to %s EVPN %s route in VRF %s",
3119 install ? "install"
3120 : "uninstall",
3121 prefix2str(evp, buf,
3122 sizeof(buf)),
996c9314
LB
3123 vrf_id_to_name(
3124 bgp_vrf->vrf_id));
5ba238b7
MK
3125 return ret;
3126 }
3127 }
3128 }
3129 }
3130 }
3131
3132 return 0;
3133}
3134
128ea8ab 3135/*
3136 * Install or uninstall routes of specified type that are appropriate for this
3137 * particular VNI.
3138 */
d62a17ae 3139static int install_uninstall_routes_for_vni(struct bgp *bgp,
3140 struct bgpevpn *vpn,
3141 bgp_evpn_route_type rtype,
3142 int install)
3143{
0291c246
MK
3144 afi_t afi;
3145 safi_t safi;
3146 struct bgp_node *rd_rn, *rn;
3147 struct bgp_table *table;
40381db7 3148 struct bgp_path_info *pi;
0291c246 3149 int ret;
d62a17ae 3150
3151 afi = AFI_L2VPN;
3152 safi = SAFI_EVPN;
3153
3154 /* Walk entire global routing table and evaluate routes which could be
3155 * imported into this VPN. Note that we cannot just look at the routes
3156 * for
3157 * the VNI's RD - remote routes applicable for this VNI could have any
3158 * RD.
3159 */
3160 /* EVPN routes are a 2-level table. */
3161 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
3162 rd_rn = bgp_route_next(rd_rn)) {
67009e22 3163 table = bgp_node_get_bgp_table_info(rd_rn);
d62a17ae 3164 if (!table)
3165 continue;
3166
3167 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
3168 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
3169
3170 if (evp->prefix.route_type != rtype)
3171 continue;
3172
6f94b685
DS
3173 for (pi = bgp_node_get_bgp_path_info(rn); pi;
3174 pi = pi->next) {
d62a17ae 3175 /* Consider "valid" remote routes applicable for
3176 * this VNI. */
40381db7
DS
3177 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3178 && pi->type == ZEBRA_ROUTE_BGP
3179 && pi->sub_type == BGP_ROUTE_NORMAL))
d62a17ae 3180 continue;
3181
40381db7 3182 if (is_route_matching_for_vni(bgp, vpn, pi)) {
d62a17ae 3183 if (install)
3184 ret = install_evpn_route_entry(
40381db7 3185 bgp, vpn, evp, pi);
d62a17ae 3186 else
3187 ret = uninstall_evpn_route_entry(
40381db7 3188 bgp, vpn, evp, pi);
d62a17ae 3189
3190 if (ret) {
af4c2728 3191 flog_err(
e50f7cfd 3192 EC_BGP_EVPN_FAIL,
d62a17ae 3193 "%u: Failed to %s EVPN %s route in VNI %u",
3194 bgp->vrf_id,
3195 install ? "install"
3196 : "uninstall",
3197 rtype == BGP_EVPN_MAC_IP_ROUTE
3198 ? "MACIP"
3199 : "IMET",
3200 vpn->vni);
3201 return ret;
3202 }
3203 }
3204 }
3205 }
3206 }
3207
3208 return 0;
128ea8ab 3209}
3210
50f74cf1 3211/* Install any existing remote ES routes applicable for this ES into its routing
3212 * table. This is invoked when ES comes up.
3213 */
3214static int install_routes_for_es(struct bgp *bgp, struct evpnes *es)
3215{
3216 return install_uninstall_routes_for_es(bgp, es, 1);
3217}
3218
3219
5ba238b7 3220/* Install any existing remote routes applicable for this VRF into VRF RIB. This
523cafc4 3221 * is invoked upon l3vni-add or l3vni import rt change
3222 */
5ba238b7
MK
3223static int install_routes_for_vrf(struct bgp *bgp_vrf)
3224{
3225 install_uninstall_routes_for_vrf(bgp_vrf, 1);
3226 return 0;
3227}
3228
128ea8ab 3229/*
3230 * Install any existing remote routes applicable for this VNI into its
3231 * routing table. This is invoked when a VNI becomes "live" or its Import
3232 * RT is changed.
3233 */
d62a17ae 3234static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3235{
d62a17ae 3236 int ret;
128ea8ab 3237
d62a17ae 3238 /* Install type-3 routes followed by type-2 routes - the ones applicable
3239 * for this VNI.
3240 */
3241 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3242 1);
3243 if (ret)
3244 return ret;
128ea8ab 3245
d62a17ae 3246 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3247 1);
128ea8ab 3248}
3249
5ba238b7
MK
3250/* uninstall routes from l3vni vrf. */
3251static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
3252{
3253 install_uninstall_routes_for_vrf(bgp_vrf, 0);
3254 return 0;
3255}
3256
90e60aa7 3257/*
3258 * Uninstall any existing remote routes for this VNI. One scenario in which
3259 * this is invoked is upon an import RT change.
3260 */
d62a17ae 3261static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3262{
d62a17ae 3263 int ret;
90e60aa7 3264
d62a17ae 3265 /* Uninstall type-2 routes followed by type-3 routes - the ones
3266 * applicable
3267 * for this VNI.
3268 */
3269 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3270 0);
3271 if (ret)
3272 return ret;
90e60aa7 3273
d62a17ae 3274 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3275 0);
90e60aa7 3276}
3277
50f74cf1 3278/* Install or unistall route in ES */
3279static int install_uninstall_route_in_es(struct bgp *bgp, struct evpnes *es,
3280 afi_t afi, safi_t safi,
3281 struct prefix_evpn *evp,
40381db7 3282 struct bgp_path_info *pi, int install)
50f74cf1 3283{
3284 int ret = 0;
3285 char buf[ESI_STR_LEN];
3286
3287 if (install)
40381db7 3288 ret = install_evpn_route_entry_in_es(bgp, es, evp, pi);
50f74cf1 3289 else
40381db7 3290 ret = uninstall_evpn_route_entry_in_es(bgp, es, evp, pi);
50f74cf1 3291
3292 if (ret) {
af4c2728 3293 flog_err(
e50f7cfd 3294 EC_BGP_EVPN_FAIL,
14454c9f
DS
3295 "%u: Failed to %s EVPN %s route in ESI %s", bgp->vrf_id,
3296 install ? "install" : "uninstall", "ES",
3297 esi_to_str(&evp->prefix.es_addr.esi, buf, sizeof(buf)));
50f74cf1 3298 return ret;
3299 }
3300 return 0;
3301}
3302
d3135ba3 3303/*
3304 * Install or uninstall route in matching VRFs (list).
3305 */
3306static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
3307 safi_t safi, struct prefix_evpn *evp,
40381db7 3308 struct bgp_path_info *pi,
d3135ba3 3309 struct list *vrfs, int install)
3310{
3311 char buf[PREFIX2STR_BUFFER];
3312 struct bgp *bgp_vrf;
3313 struct listnode *node, *nnode;
3314
90264d64 3315 /* Only type-2/type-5 routes go into a VRF */
996c9314
LB
3316 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3317 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
d3135ba3 3318 return 0;
3319
90264d64 3320 /* if it is type-2 route and not a mac+ip route skip this route */
996c9314 3321 if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 3322 && !(is_evpn_prefix_ipaddr_v4(evp)
3323 || is_evpn_prefix_ipaddr_v6(evp)))
30a30f57
MK
3324 return 0;
3325
d3135ba3 3326 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
3327 int ret;
3328
3329 if (install)
40381db7 3330 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, pi);
d3135ba3 3331 else
996c9314 3332 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
40381db7 3333 pi);
d3135ba3 3334
3335 if (ret) {
e50f7cfd 3336 flog_err(EC_BGP_EVPN_FAIL,
1c50c1c0
QY
3337 "%u: Failed to %s prefix %s in VRF %s",
3338 bgp_def->vrf_id,
3339 install ? "install" : "uninstall",
3340 prefix2str(evp, buf, sizeof(buf)),
3341 vrf_id_to_name(bgp_vrf->vrf_id));
d3135ba3 3342 return ret;
3343 }
3344 }
3345
3346 return 0;
3347}
3348
128ea8ab 3349/*
3350 * Install or uninstall route in matching VNIs (list).
3351 */
d62a17ae 3352static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
3353 safi_t safi, struct prefix_evpn *evp,
40381db7 3354 struct bgp_path_info *pi,
d62a17ae 3355 struct list *vnis, int install)
128ea8ab 3356{
d62a17ae 3357 struct bgpevpn *vpn;
3358 struct listnode *node, *nnode;
128ea8ab 3359
d62a17ae 3360 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
3361 int ret;
128ea8ab 3362
d62a17ae 3363 if (!is_vni_live(vpn))
3364 continue;
128ea8ab 3365
d62a17ae 3366 if (install)
40381db7 3367 ret = install_evpn_route_entry(bgp, vpn, evp, pi);
d62a17ae 3368 else
40381db7 3369 ret = uninstall_evpn_route_entry(bgp, vpn, evp, pi);
128ea8ab 3370
d62a17ae 3371 if (ret) {
1c50c1c0
QY
3372 flog_err(EC_BGP_EVPN_FAIL,
3373 "%u: Failed to %s EVPN %s route in VNI %u",
3374 bgp->vrf_id, install ? "install" : "uninstall",
3375 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3376 ? "MACIP"
3377 : "IMET",
3378 vpn->vni);
d62a17ae 3379 return ret;
3380 }
3381 }
128ea8ab 3382
d62a17ae 3383 return 0;
128ea8ab 3384}
3385
3386/*
50f74cf1 3387 * Install or uninstall route for appropriate VNIs/ESIs.
128ea8ab 3388 */
d62a17ae 3389static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
4b7e6066 3390 struct prefix *p,
40381db7 3391 struct bgp_path_info *pi, int import)
d62a17ae 3392{
3393 struct prefix_evpn *evp = (struct prefix_evpn *)p;
40381db7 3394 struct attr *attr = pi->attr;
d62a17ae 3395 struct ecommunity *ecom;
3396 int i;
3397
3398 assert(attr);
3399
50f74cf1 3400 /* Only type-2, type-3, type-4 and type-5 are supported currently */
d62a17ae 3401 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
90264d64 3402 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
50f74cf1 3403 || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
90264d64 3404 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
d62a17ae 3405 return 0;
3406
3407 /* If we don't have Route Target, nothing much to do. */
3408 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3409 return 0;
3410
3411 ecom = attr->ecommunity;
3412 if (!ecom || !ecom->size)
3413 return -1;
3414
50f74cf1 3415 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3416 * attached to the route */
d62a17ae 3417 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
3418 uint8_t *pnt;
3419 uint8_t type, sub_type;
d62a17ae 3420 struct ecommunity_val *eval;
3421 struct ecommunity_val eval_tmp;
996c9314 3422 struct irt_node *irt; /* import rt for l2vni */
d3135ba3 3423 struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
50f74cf1 3424 struct evpnes *es;
d62a17ae 3425
3426 /* Only deal with RTs */
3427 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
3428 eval = (struct ecommunity_val *)(ecom->val
3429 + (i * ECOMMUNITY_SIZE));
3430 type = *pnt++;
3431 sub_type = *pnt++;
3432 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3433 continue;
3434
50f74cf1 3435 /*
3436 * macip routes (type-2) are imported into VNI and VRF tables.
3437 * IMET route is imported into VNI table.
3438 * prefix routes are imported into VRF table.
523cafc4 3439 */
50f74cf1 3440 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE ||
3441 evp->prefix.route_type == BGP_EVPN_IMET_ROUTE ||
3442 evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
d62a17ae 3443
50f74cf1 3444 irt = lookup_import_rt(bgp, eval);
3445 if (irt)
40381db7
DS
3446 install_uninstall_route_in_vnis(
3447 bgp, afi, safi, evp, pi, irt->vnis,
3448 import);
50f74cf1 3449
3450 vrf_irt = lookup_vrf_import_rt(eval);
3451 if (vrf_irt)
40381db7
DS
3452 install_uninstall_route_in_vrfs(
3453 bgp, afi, safi, evp, pi, vrf_irt->vrfs,
3454 import);
50f74cf1 3455
3456 /* Also check for non-exact match.
3457 * In this, we mask out the AS and
3458 * only check on the local-admin sub-field.
3459 * This is to facilitate using
3460 * VNI as the RT for EBGP peering too.
3461 */
3462 irt = NULL;
3463 vrf_irt = NULL;
3464 if (type == ECOMMUNITY_ENCODE_AS
3465 || type == ECOMMUNITY_ENCODE_AS4
3466 || type == ECOMMUNITY_ENCODE_IP) {
3467 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3468 mask_ecom_global_admin(&eval_tmp, eval);
3469 irt = lookup_import_rt(bgp, &eval_tmp);
3470 vrf_irt = lookup_vrf_import_rt(&eval_tmp);
3471 }
3472
3473 if (irt)
40381db7
DS
3474 install_uninstall_route_in_vnis(
3475 bgp, afi, safi, evp, pi, irt->vnis,
3476 import);
50f74cf1 3477 if (vrf_irt)
40381db7
DS
3478 install_uninstall_route_in_vrfs(
3479 bgp, afi, safi, evp, pi, vrf_irt->vrfs,
3480 import);
50f74cf1 3481 }
3482
3483 /* es route is imported into the es table */
3484 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE) {
3485
3486 /* we will match based on the entire esi to avoid
3487 * imoort of an es route for esi2 into esi1
3488 */
3489 es = bgp_evpn_lookup_es(bgp, &evp->prefix.es_addr.esi);
3490 if (es && is_es_local(es))
40381db7
DS
3491 install_uninstall_route_in_es(
3492 bgp, es, afi, safi, evp, pi, import);
d62a17ae 3493 }
d62a17ae 3494 }
3495
3496 return 0;
128ea8ab 3497}
3498
2bb9eff4
DS
3499/*
3500 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3501 * routes
3502 */
80b140af
MK
3503static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
3504{
5fd9c12b
KA
3505 /* Delete ipv4 default route and withdraw from peers */
3506 if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
3507 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
3508 SAFI_UNICAST, false);
3509
80b140af 3510 /* delete all ipv4 routes and withdraw from peers */
fdf19f06
MK
3511 if (advertise_type5_routes(bgp_vrf, AFI_IP))
3512 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
80b140af 3513
5fd9c12b
KA
3514 /* Delete ipv6 default route and withdraw from peers */
3515 if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
3516 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
3517 SAFI_UNICAST, false);
3518
80b140af 3519 /* delete all ipv6 routes and withdraw from peers */
fdf19f06
MK
3520 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
3521 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
80b140af
MK
3522}
3523
2bb9eff4
DS
3524/*
3525 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3526 * routes
3527 */
80b140af
MK
3528static void update_advertise_vrf_routes(struct bgp *bgp_vrf)
3529{
3530 /* update all ipv4 routes */
fdf19f06
MK
3531 if (advertise_type5_routes(bgp_vrf, AFI_IP))
3532 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
80b140af 3533
5fd9c12b
KA
3534 /* update ipv4 default route and withdraw from peers */
3535 if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
3536 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
3537 SAFI_UNICAST, true);
3538
80b140af 3539 /* update all ipv6 routes */
fdf19f06
MK
3540 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
3541 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
5fd9c12b
KA
3542
3543 /* update ipv6 default route and withdraw from peers */
3544 if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
3545 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
3546 SAFI_UNICAST, true);
3547
80b140af
MK
3548}
3549
676f83b9 3550/*
3551 * update and advertise local routes for a VRF as type-5 routes.
3552 * This is invoked upon RD change for a VRF. Note taht the processing is only
3553 * done in the global route table using the routes which already exist in the
3554 * VRF routing table
3555 */
80b140af 3556static void update_router_id_vrf(struct bgp *bgp_vrf)
676f83b9 3557{
80b140af
MK
3558 /* skip if the RD is configured */
3559 if (is_vrf_rd_configured(bgp_vrf))
3560 return;
3561
3562 /* derive the RD for the VRF based on new router-id */
3563 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
3564
3565 /* update advertise ipv4|ipv6 routes as type-5 routes */
3566 update_advertise_vrf_routes(bgp_vrf);
676f83b9 3567}
3568
3569/*
3570 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3571 * This is invoked upon VRF RD change. The processing is done only from global
3572 * table.
3573 */
80b140af 3574static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
676f83b9 3575{
80b140af
MK
3576 /* skip if the RD is configured */
3577 if (is_vrf_rd_configured(bgp_vrf))
3578 return;
3579
3580 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3581 delete_withdraw_vrf_routes(bgp_vrf);
676f83b9 3582}
3583
90e60aa7 3584/*
3585 * Update and advertise local routes for a VNI. Invoked upon router-id
3586 * change. Note that the processing is done only on the global route table
3587 * using routes that already exist in the per-VNI table.
3588 */
d62a17ae 3589static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
3590{
3591 struct prefix_evpn p;
3592 struct bgp_node *rn, *global_rn;
40381db7 3593 struct bgp_path_info *pi, *global_pi;
d62a17ae 3594 struct attr *attr;
3595 afi_t afi = AFI_L2VPN;
3596 safi_t safi = SAFI_EVPN;
3597
3598 /* Locate type-3 route for VNI in the per-VNI table and use its
3599 * attributes to create and advertise the type-3 route for this VNI
3600 * in the global table.
fd069644
DS
3601 *
3602 * RT-3 only if doing head-end replication
d62a17ae 3603 */
833b8a50
AK
3604 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
3605 == VXLAN_FLOOD_HEAD_END_REPL) {
fd069644
DS
3606 build_evpn_type3_prefix(&p, vpn->originator_ip);
3607 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
3608 if (!rn) /* unexpected */
3609 return 0;
6f94b685 3610 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next)
fd069644
DS
3611 if (pi->peer == bgp->peer_self &&
3612 pi->type == ZEBRA_ROUTE_BGP
3613 && pi->sub_type == BGP_ROUTE_STATIC)
3614 break;
3615 if (!pi) /* unexpected */
3616 return 0;
3617 attr = pi->attr;
d62a17ae 3618
fd069644
DS
3619 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
3620 (struct prefix *)&p, &vpn->prd);
3621 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr,
3622 1, &pi, 0, mac_mobility_seqnum(attr));
d62a17ae 3623
fd069644
DS
3624 /* Schedule for processing and unlock node. */
3625 bgp_process(bgp, global_rn, afi, safi);
3626 bgp_unlock_node(global_rn);
3627 }
d62a17ae 3628
3629 /* Now, walk this VNI's route table and use the route and its attribute
3630 * to create and schedule route in global table.
3631 */
3632 for (rn = bgp_table_top(vpn->route_table); rn;
3633 rn = bgp_route_next(rn)) {
3634 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
3635
3636 /* Identify MAC-IP local routes. */
3637 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
3638 continue;
3639
6f94b685 3640 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next)
40381db7
DS
3641 if (pi->peer == bgp->peer_self
3642 && pi->type == ZEBRA_ROUTE_BGP
3643 && pi->sub_type == BGP_ROUTE_STATIC)
d62a17ae 3644 break;
40381db7 3645 if (!pi)
d62a17ae 3646 continue;
3647
3648 /* Create route in global routing table using this route entry's
3649 * attribute.
3650 */
40381db7 3651 attr = pi->attr;
d62a17ae 3652 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
3653 (struct prefix *)evp, &vpn->prd);
3654 assert(global_rn);
3655 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1,
40381db7 3656 &global_pi, 0,
f07e1c99 3657 mac_mobility_seqnum(attr));
d62a17ae 3658
3659 /* Schedule for processing and unlock node. */
3660 bgp_process(bgp, global_rn, afi, safi);
3661 bgp_unlock_node(global_rn);
3662 }
3663
3664 return 0;
90e60aa7 3665}
3666
3667/*
3668 * Delete (and withdraw) local routes for a VNI - only from the global
3669 * table. Invoked upon router-id change.
3670 */
d62a17ae 3671static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3672{
d62a17ae 3673 int ret;
3674 struct prefix_evpn p;
3675 struct bgp_node *global_rn;
40381db7 3676 struct bgp_path_info *pi;
d62a17ae 3677 afi_t afi = AFI_L2VPN;
3678 safi_t safi = SAFI_EVPN;
90e60aa7 3679
d62a17ae 3680 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3681 * for this VNI - from the global table.
3682 */
3683 ret = delete_global_type2_routes(bgp, vpn);
3684 if (ret)
3685 return ret;
90e60aa7 3686
d62a17ae 3687 /* Remove type-3 route for this VNI from global table. */
3688 build_evpn_type3_prefix(&p, vpn->originator_ip);
3689 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
3690 (struct prefix *)&p, &vpn->prd);
3691 if (global_rn) {
3692 /* Delete route entry in the global EVPN table. */
40381db7 3693 delete_evpn_route_entry(bgp, afi, safi, global_rn, &pi);
90e60aa7 3694
d62a17ae 3695 /* Schedule for processing - withdraws to peers happen from
3696 * this table.
3697 */
40381db7 3698 if (pi)
d62a17ae 3699 bgp_process(bgp, global_rn, afi, safi);
3700 bgp_unlock_node(global_rn);
3701 }
90e60aa7 3702
d62a17ae 3703 return 0;
90e60aa7 3704}
3705
2d48ee25 3706/*
3707 * Handle router-id change. Update and advertise local routes corresponding
3708 * to this VNI from peers. Note that this is invoked after updating the
3709 * router-id. The routes in the per-VNI table are used to create routes in
3710 * the global table and schedule them.
3711 */
e3b78da8 3712static void update_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
2d48ee25 3713{
e3b78da8 3714 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
2d48ee25 3715
d62a17ae 3716 /* Skip VNIs with configured RD. */
3717 if (is_rd_configured(vpn))
3718 return;
2d48ee25 3719
d62a17ae 3720 bgp_evpn_derive_auto_rd(bgp, vpn);
3721 update_advertise_vni_routes(bgp, vpn);
2d48ee25 3722}
3723
3724/*
3725 * Handle router-id change. Delete and withdraw local routes corresponding
3726 * to this VNI from peers. Note that this is invoked prior to updating
3727 * the router-id and is done only on the global route table, the routes
3728 * are needed in the per-VNI table to re-advertise with new router id.
3729 */
e3b78da8 3730static void withdraw_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
2d48ee25 3731{
e3b78da8 3732 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
2d48ee25 3733
d62a17ae 3734 /* Skip VNIs with configured RD. */
3735 if (is_rd_configured(vpn))
3736 return;
2d48ee25 3737
d62a17ae 3738 delete_withdraw_vni_routes(bgp, vpn);
2d48ee25 3739}
3740
fd069644
DS
3741/*
3742 * Create RT-3 for a VNI and schedule for processing and advertisement.
3743 * This is invoked upon flooding mode changing to head-end replication.
3744 */
e3b78da8 3745static void create_advertise_type3(struct hash_bucket *bucket, void *data)
fd069644 3746{
e3b78da8 3747 struct bgpevpn *vpn = bucket->data;
fd069644
DS
3748 struct bgp *bgp = data;
3749 struct prefix_evpn p;
3750
833b8a50
AK
3751 if (!vpn || !is_vni_live(vpn) ||
3752 bgp_evpn_vni_flood_mode_get(bgp, vpn)
3753 != VXLAN_FLOOD_HEAD_END_REPL)
fd069644
DS
3754 return;
3755
3756 build_evpn_type3_prefix(&p, vpn->originator_ip);
3757 if (update_evpn_route(bgp, vpn, &p, 0, 0))
3758 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
3759 "Type3 route creation failure for VNI %u", vpn->vni);
3760}
3761
3762/*
3763 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3764 * This is invoked upon flooding mode changing to drop BUM packets.
3765 */
e3b78da8 3766static void delete_withdraw_type3(struct hash_bucket *bucket, void *data)
fd069644 3767{
e3b78da8 3768 struct bgpevpn *vpn = bucket->data;
fd069644
DS
3769 struct bgp *bgp = data;
3770 struct prefix_evpn p;
3771
3772 if (!vpn || !is_vni_live(vpn))
3773 return;
3774
3775 build_evpn_type3_prefix(&p, vpn->originator_ip);
3776 delete_evpn_route(bgp, vpn, &p);
3777}
3778
128ea8ab 3779/*
3780 * Process received EVPN type-2 route (advertise or withdraw).
3781 */
d62a17ae 3782static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a
QY
3783 struct attr *attr, uint8_t *pfx, int psize,
3784 uint32_t addpath_id)
d62a17ae 3785{
3786 struct prefix_rd prd;
3787 struct prefix_evpn p;
554cd77a 3788 struct bgp_route_evpn evpn;
d7c0a89a
QY
3789 uint8_t ipaddr_len;
3790 uint8_t macaddr_len;
b57ba6d2 3791 mpls_label_t label[BGP_MAX_LABELS]; /* holds the VNI(s) as in packet */
d7c0a89a 3792 uint32_t num_labels = 0;
554cd77a 3793 uint32_t eth_tag;
d62a17ae 3794 int ret;
3795
3796 /* Type-2 route should be either 33, 37 or 49 bytes or an
3797 * additional 3 bytes if there is a second label (VNI):
3798 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3799 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3800 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3801 */
3802 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
3803 && psize != 40 && psize != 52) {
e50f7cfd 3804 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
1c50c1c0
QY
3805 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3806 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 3807 return -1;
3808 }
3809
554cd77a
VB
3810 memset(&evpn, 0, sizeof(evpn));
3811
d62a17ae 3812 /* Make prefix_rd */
3813 prd.family = AF_UNSPEC;
3814 prd.prefixlen = 64;
3815 memcpy(&prd.val, pfx, 8);
3816 pfx += 8;
3817
3818 /* Make EVPN prefix. */
3819 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 3820 p.family = AF_EVPN;
50f74cf1 3821 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 3822 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
3823
554cd77a
VB
3824 /* Copy Ethernet Seg Identifier */
3825 memcpy(&evpn.eth_s_id.val, pfx, ESI_LEN);
3826 pfx += ESI_LEN;
d62a17ae 3827
554cd77a
VB
3828 /* Copy Ethernet Tag */
3829 memcpy(&eth_tag, pfx, 4);
3714a385 3830 p.prefix.macip_addr.eth_tag = ntohl(eth_tag);
d62a17ae 3831 pfx += 4;
3832
3833 /* Get the MAC Addr len */
3834 macaddr_len = *pfx++;
3835
3836 /* Get the MAC Addr */
28328ea9 3837 if (macaddr_len == (ETH_ALEN * 8)) {
3714a385 3838 memcpy(&p.prefix.macip_addr.mac.octet, pfx, ETH_ALEN);
28328ea9 3839 pfx += ETH_ALEN;
d62a17ae 3840 } else {
af4c2728 3841 flog_err(
e50f7cfd 3842 EC_BGP_EVPN_ROUTE_INVALID,
d62a17ae 3843 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3844 peer->bgp->vrf_id, peer->host, macaddr_len);
3845 return -1;
3846 }
3847
3848
3849 /* Get the IP. */
3850 ipaddr_len = *pfx++;
3851 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
3852 && ipaddr_len != IPV6_MAX_BITLEN) {
af4c2728 3853 flog_err(
e50f7cfd 3854 EC_BGP_EVPN_ROUTE_INVALID,
d62a17ae 3855 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3856 peer->bgp->vrf_id, peer->host, ipaddr_len);
3857 return -1;
3858 }
3859
3860 if (ipaddr_len) {
3861 ipaddr_len /= 8; /* Convert to bytes. */
3714a385 3862 p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
d62a17ae 3863 ? IPADDR_V4
3864 : IPADDR_V6;
3714a385 3865 memcpy(&p.prefix.macip_addr.ip.ip.addr, pfx, ipaddr_len);
d62a17ae 3866 }
3867 pfx += ipaddr_len;
3868
b57ba6d2
MK
3869 /* Get the VNI(s). Stored as bytes here. */
3870 num_labels++;
3871 memset(label, 0, sizeof(label));
3872 memcpy(&label[0], pfx, BGP_LABEL_BYTES);
1b817c78 3873 pfx += BGP_LABEL_BYTES;
b57ba6d2
MK
3874 psize -= (33 + ipaddr_len);
3875 /* Do we have a second VNI? */
3876 if (psize) {
3877 num_labels++;
3878 memcpy(&label[1], pfx, BGP_LABEL_BYTES);
6b11bd8d 3879 /*
3880 * If in future, we are required to access additional fields,
996c9314
LB
3881 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3882 * the next field
6b11bd8d 3883 */
b57ba6d2 3884 }
d62a17ae 3885
3886 /* Process the route. */
3887 if (attr)
3888 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3889 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
554cd77a 3890 &prd, &label[0], num_labels, 0, &evpn);
d62a17ae 3891 else
3892 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3893 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
554cd77a 3894 &prd, &label[0], num_labels, &evpn);
d62a17ae 3895 return ret;
128ea8ab 3896}
3897
3898/*
3899 * Process received EVPN type-3 route (advertise or withdraw).
3900 */
d62a17ae 3901static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a
QY
3902 struct attr *attr, uint8_t *pfx, int psize,
3903 uint32_t addpath_id)
d62a17ae 3904{
3905 struct prefix_rd prd;
3906 struct prefix_evpn p;
d7c0a89a 3907 uint8_t ipaddr_len;
554cd77a 3908 uint32_t eth_tag;
d62a17ae 3909 int ret;
3910
3911 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3912 * IP len (1) and IP (4 or 16).
3913 */
3914 if (psize != 17 && psize != 29) {
e50f7cfd 3915 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
1c50c1c0
QY
3916 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3917 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 3918 return -1;
3919 }
3920
7fd077aa 3921 /* If PMSI is present, log if it is anything other than IR.
3922 * Note: We just simply ignore the values as it is not clear if
3923 * doing anything else is better.
3924 */
3925 if (attr &&
3926 (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
b16dd019
AK
3927 if (attr->pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL &&
3928 attr->pmsi_tnl_type != PMSI_TNLTYPE_PIM_SM) {
3929 flog_warn(EC_BGP_EVPN_PMSI_PRESENT,
3930 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3931 peer->bgp->vrf_id, peer->host,
3932 attr->pmsi_tnl_type);
7fd077aa 3933 }
3934 }
3935
d62a17ae 3936 /* Make prefix_rd */
3937 prd.family = AF_UNSPEC;
3938 prd.prefixlen = 64;
3939 memcpy(&prd.val, pfx, 8);
3940 pfx += 8;
3941
3942 /* Make EVPN prefix. */
3943 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 3944 p.family = AF_EVPN;
50f74cf1 3945 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 3946 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
3947
554cd77a
VB
3948 /* Copy Ethernet Tag */
3949 memcpy(&eth_tag, pfx, 4);
3714a385 3950 p.prefix.imet_addr.eth_tag = ntohl(eth_tag);
d62a17ae 3951 pfx += 4;
3952
3953 /* Get the IP. */
3954 ipaddr_len = *pfx++;
3955 if (ipaddr_len == IPV4_MAX_BITLEN) {
3714a385 3956 p.prefix.imet_addr.ip.ipa_type = IPADDR_V4;
3957 memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
d62a17ae 3958 } else {
af4c2728 3959 flog_err(
e50f7cfd 3960 EC_BGP_EVPN_ROUTE_INVALID,
d62a17ae 3961 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3962 peer->bgp->vrf_id, peer->host, ipaddr_len);
3963 return -1;
3964 }
3965
3966 /* Process the route. */
3967 if (attr)
3968 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3969 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 3970 &prd, NULL, 0, 0, NULL);
d62a17ae 3971 else
3972 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3973 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 3974 &prd, NULL, 0, NULL);
d62a17ae 3975 return ret;
128ea8ab 3976}
3977
50f74cf1 3978/*
3979 * Process received EVPN type-4 route (advertise or withdraw).
3980 */
3981static int process_type4_route(struct peer *peer, afi_t afi, safi_t safi,
3982 struct attr *attr, uint8_t *pfx, int psize,
3983 uint32_t addpath_id)
3984{
3985 int ret;
3986 esi_t esi;
3987 uint8_t ipaddr_len;
3988 struct in_addr vtep_ip;
3989 struct prefix_rd prd;
3990 struct prefix_evpn p;
3991
3992 /* Type-4 route should be either 23 or 35 bytes
3993 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3994 */
3995 if (psize != 23 && psize != 35) {
e50f7cfd 3996 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
1c50c1c0
QY
3997 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3998 peer->bgp->vrf_id, peer->host, psize);
50f74cf1 3999 return -1;
4000 }
4001
4002 /* Make prefix_rd */
4003 prd.family = AF_UNSPEC;
4004 prd.prefixlen = 64;
4005 memcpy(&prd.val, pfx, 8);
4006 pfx += 8;
4007
4008 /* get the ESI */
4009 memcpy(&esi, pfx, ESI_BYTES);
4010 pfx += ESI_BYTES;
4011
4012
4013 /* Get the IP. */
4014 ipaddr_len = *pfx++;
4015 if (ipaddr_len == IPV4_MAX_BITLEN) {
4016 memcpy(&vtep_ip, pfx, IPV4_MAX_BYTELEN);
4017 } else {
af4c2728 4018 flog_err(
e50f7cfd 4019 EC_BGP_EVPN_ROUTE_INVALID,
50f74cf1 4020 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
4021 peer->bgp->vrf_id, peer->host, ipaddr_len);
4022 return -1;
4023 }
4024
4025 build_evpn_type4_prefix(&p, &esi, vtep_ip);
4026 /* Process the route. */
4027 if (attr) {
4028 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4029 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4030 &prd, NULL, 0, 0, NULL);
2bb9eff4 4031 } else {
50f74cf1 4032 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4033 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4034 &prd, NULL, 0, NULL);
4035 }
4036 return ret;
4037}
4038
4039
128ea8ab 4040/*
4041 * Process received EVPN type-5 route (advertise or withdraw).
4042 */
d62a17ae 4043static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a 4044 struct attr *attr, uint8_t *pfx, int psize,
f007bdce 4045 uint32_t addpath_id)
d62a17ae 4046{
4047 struct prefix_rd prd;
4048 struct prefix_evpn p;
4049 struct bgp_route_evpn evpn;
d7c0a89a
QY
4050 uint8_t ippfx_len;
4051 uint32_t eth_tag;
b57ba6d2 4052 mpls_label_t label; /* holds the VNI as in the packet */
d62a17ae 4053 int ret;
4054
4055 /* Type-5 route should be 34 or 58 bytes:
4056 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4057 * GW (4 or 16) and VNI (3).
4058 * Note that the IP and GW should both be IPv4 or both IPv6.
4059 */
4060 if (psize != 34 && psize != 58) {
e50f7cfd 4061 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
1c50c1c0
QY
4062 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4063 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 4064 return -1;
4065 }
4066
4067 /* Make prefix_rd */
4068 prd.family = AF_UNSPEC;
4069 prd.prefixlen = 64;
4070 memcpy(&prd.val, pfx, 8);
4071 pfx += 8;
4072
4073 /* Make EVPN prefix. */
4074 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 4075 p.family = AF_EVPN;
50f74cf1 4076 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 4077 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
4078
4079 /* Additional information outside of prefix - ESI and GW IP */
4080 memset(&evpn, 0, sizeof(evpn));
4081
4082 /* Fetch ESI */
4083 memcpy(&evpn.eth_s_id.val, pfx, 10);
4084 pfx += 10;
4085
4086 /* Fetch Ethernet Tag. */
4087 memcpy(&eth_tag, pfx, 4);
3714a385 4088 p.prefix.prefix_addr.eth_tag = ntohl(eth_tag);
d62a17ae 4089 pfx += 4;
4090
4091 /* Fetch IP prefix length. */
4092 ippfx_len = *pfx++;
4093 if (ippfx_len > IPV6_MAX_BITLEN) {
af4c2728 4094 flog_err(
e50f7cfd 4095 EC_BGP_EVPN_ROUTE_INVALID,
d62a17ae 4096 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4097 peer->bgp->vrf_id, peer->host, ippfx_len);
4098 return -1;
4099 }
3714a385 4100 p.prefix.prefix_addr.ip_prefix_length = ippfx_len;
d62a17ae 4101
4102 /* Determine IPv4 or IPv6 prefix */
4103 /* Since the address and GW are from the same family, this just becomes
4104 * a simple check on the total size.
4105 */
4106 if (psize == 34) {
3714a385 4107 SET_IPADDR_V4(&p.prefix.prefix_addr.ip);
4108 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4);
d62a17ae 4109 pfx += 4;
4110 memcpy(&evpn.gw_ip.ipv4, pfx, 4);
4111 pfx += 4;
d62a17ae 4112 } else {
3714a385 4113 SET_IPADDR_V6(&p.prefix.prefix_addr.ip);
4114 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx, 16);
d62a17ae 4115 pfx += 16;
4116 memcpy(&evpn.gw_ip.ipv6, pfx, 16);
4117 pfx += 16;
d62a17ae 4118 }
4119
b57ba6d2
MK
4120 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4121 memset(&label, 0, sizeof(label));
4122 memcpy(&label, pfx, BGP_LABEL_BYTES);
6b11bd8d 4123
4124 /*
4125 * If in future, we are required to access additional fields,
996c9314
LB
4126 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4127 * field
6b11bd8d 4128 */
d62a17ae 4129
4130 /* Process the route. */
f007bdce 4131 if (attr)
d62a17ae 4132 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4133 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 4134 &prd, &label, 1, 0, &evpn);
d62a17ae 4135 else
4136 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4137 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 4138 &prd, &label, 1, &evpn);
d62a17ae 4139
4140 return ret;
4141}
4142
4143static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p,
996c9314 4144 struct prefix_rd *prd, mpls_label_t *label,
d7c0a89a 4145 uint32_t num_labels, struct attr *attr)
d62a17ae 4146{
4147 int len;
4148 char temp[16];
4149 struct evpn_addr *p_evpn_p;
4150
4151 memset(&temp, 0, 16);
b03b8898 4152 if (p->family != AF_EVPN)
d62a17ae 4153 return;
4154 p_evpn_p = &(p->u.prefix_evpn);
4155
e9fc2840 4156 /* len denites the total len of IP and GW-IP in the route
523cafc4 4157 IP and GW-IP have to be both ipv4 or ipv6
4158 */
3714a385 4159 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
e9fc2840 4160 len = 8; /* IP and GWIP are both ipv4 */
d62a17ae 4161 else
e9fc2840 4162 len = 32; /* IP and GWIP are both ipv6 */
d62a17ae 4163 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4164 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
4165 stream_put(s, prd->val, 8);
0af35d90 4166 if (attr)
d62a17ae 4167 stream_put(s, &(attr->evpn_overlay.eth_s_id), 10);
4168 else
4169 stream_put(s, &temp, 10);
3714a385 4170 stream_putl(s, p_evpn_p->prefix_addr.eth_tag);
4171 stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length);
4172 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4173 stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr);
d62a17ae 4174 else
3714a385 4175 stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16);
0af35d90 4176 if (attr) {
3714a385 4177 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
d62a17ae 4178 stream_put_ipv4(s,
4179 attr->evpn_overlay.gw_ip.ipv4.s_addr);
4180 else
4181 stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
4182 } else {
3714a385 4183 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
d62a17ae 4184 stream_put_ipv4(s, 0);
4185 else
4186 stream_put(s, &temp, 16);
4187 }
4188
b57ba6d2 4189 if (num_labels)
d62a17ae 4190 stream_put(s, label, 3);
4191 else
4192 stream_put3(s, 0);
128ea8ab 4193}
4194
4195/*
4196 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4197 */
e3b78da8 4198static void cleanup_vni_on_disable(struct hash_bucket *bucket, struct bgp *bgp)
128ea8ab 4199{
e3b78da8 4200 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
128ea8ab 4201
d62a17ae 4202 /* Remove EVPN routes and schedule for processing. */
4203 delete_routes_for_vni(bgp, vpn);
128ea8ab 4204
d62a17ae 4205 /* Clear "live" flag and see if hash needs to be freed. */
4206 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4207 if (!is_vni_configured(vpn))
4208 bgp_evpn_free(bgp, vpn);
128ea8ab 4209}
4210
4211/*
4212 * Free a VNI entry; iterator function called during cleanup.
4213 */
e3b78da8 4214static void free_vni_entry(struct hash_bucket *bucket, struct bgp *bgp)
128ea8ab 4215{
e3b78da8 4216 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
128ea8ab 4217
d62a17ae 4218 delete_all_vni_routes(bgp, vpn);
4219 bgp_evpn_free(bgp, vpn);
128ea8ab 4220}
4221
c581d8b0
MK
4222/*
4223 * Derive AUTO import RT for BGP VRF - L3VNI
4224 */
4225static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
4226{
5e53dce3 4227 struct bgp *bgp_evpn = NULL;
10ebe1ab 4228
c581d8b0 4229 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
10ebe1ab
MK
4230 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4231
4232 /* Map RT to VRF */
5e53dce3
T
4233 bgp_evpn = bgp_get_evpn();
4234 if (!bgp_evpn)
10ebe1ab
MK
4235 return;
4236 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
c581d8b0
MK
4237}
4238
4239/*
4240 * Delete AUTO import RT from BGP VRF - L3VNI
4241 */
4242static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
4243{
4244 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
4245}
4246
4247/*
4248 * Derive AUTO export RT for BGP VRF - L3VNI
4249 */
4250static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
4251{
4252 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4253 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4254}
4255
4256/*
4257 * Delete AUTO export RT from BGP VRF - L3VNI
4258 */
4259static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
4260{
4261 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4262}
128ea8ab 4263
f1f8b53c
MK
4264static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
4265{
5e53dce3 4266 struct bgp *bgp_evpn = NULL;
f1f8b53c
MK
4267 struct listnode *node = NULL;
4268 struct bgpevpn *vpn = NULL;
4269
5e53dce3
T
4270 bgp_evpn = bgp_get_evpn();
4271 if (!bgp_evpn)
f1f8b53c
MK
4272 return;
4273
4992b4ae
MK
4274 /* update all type-5 routes */
4275 update_advertise_vrf_routes(bgp_vrf);
4276
4277 /* update all type-2 routes */
f1f8b53c 4278 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
5e53dce3 4279 update_routes_for_vni(bgp_evpn, vpn);
f1f8b53c
MK
4280}
4281
bf1061d8
VB
4282/*
4283 * Handle autort change for a given VNI.
4284 */
e3b78da8 4285static void update_autort_vni(struct hash_bucket *bucket, struct bgp *bgp)
bf1061d8 4286{
e3b78da8 4287 struct bgpevpn *vpn = bucket->data;
bf1061d8 4288
bf1061d8
VB
4289 if (!is_import_rt_configured(vpn)) {
4290 if (is_vni_live(vpn))
4291 bgp_evpn_uninstall_routes(bgp, vpn);
4292 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
bf1061d8
VB
4293 list_delete_all_node(vpn->import_rtl);
4294 bgp_evpn_derive_auto_rt_import(bgp, vpn);
4295 if (is_vni_live(vpn))
4296 bgp_evpn_install_routes(bgp, vpn);
4297 }
4298 if (!is_export_rt_configured(vpn)) {
bf1061d8
VB
4299 list_delete_all_node(vpn->export_rtl);
4300 bgp_evpn_derive_auto_rt_export(bgp, vpn);
4301 if (is_vni_live(vpn))
4302 bgp_evpn_handle_export_rt_change(bgp, vpn);
4303 }
4304}
4305
128ea8ab 4306/*
4307 * Public functions.
4308 */
4309
5424b7ba 4310/* withdraw type-5 route corresponding to ip prefix */
31310b25 4311void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, struct prefix *p,
5424b7ba
MK
4312 afi_t afi, safi_t safi)
4313{
4314 int ret = 0;
4315 struct prefix_evpn evp;
4316 char buf[PREFIX_STRLEN];
4317
31310b25 4318 build_type5_prefix_from_ip_prefix(&evp, p);
5424b7ba
MK
4319 ret = delete_evpn_type5_route(bgp_vrf, &evp);
4320 if (ret) {
af4c2728 4321 flog_err(
e50f7cfd 4322 EC_BGP_EVPN_ROUTE_DELETE,
996c9314
LB
4323 "%u failed to delete type-5 route for prefix %s in vrf %s",
4324 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)),
4325 vrf_id_to_name(bgp_vrf->vrf_id));
5424b7ba
MK
4326 }
4327}
4328
342dd0c6 4329/* withdraw all type-5 routes for an address family */
996c9314 4330void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
342dd0c6 4331{
4332 struct bgp_table *table = NULL;
4333 struct bgp_node *rn = NULL;
40381db7 4334 struct bgp_path_info *pi;
342dd0c6 4335
053905d2 4336 table = bgp_vrf->rib[afi][safi];
25f2ca53 4337 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
f106e3a7 4338 /* Only care about "selected" routes. Also ensure that
4339 * these are routes that are injectable into EVPN.
4340 */
25f2ca53 4341 /* TODO: Support for AddPath for EVPN. */
6f94b685 4342 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next) {
40381db7 4343 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
f106e3a7 4344 && is_route_injectable_into_evpn(pi)) {
25f2ca53 4345 bgp_evpn_withdraw_type5_route(bgp_vrf, &rn->p,
4346 afi, safi);
4347 break;
4348 }
4349 }
4350 }
5424b7ba 4351}
342dd0c6 4352
5fd9c12b
KA
4353/*
4354 * evpn - enable advertisement of default g/w
4355 */
4356void bgp_evpn_install_uninstall_default_route(struct bgp *bgp_vrf, afi_t afi,
4357 safi_t safi, bool add)
4358{
4359 struct prefix ip_prefix;
4360
4361 /* form the default prefix 0.0.0.0/0 */
4362 memset(&ip_prefix, 0, sizeof(struct prefix));
4363 ip_prefix.family = afi2family(afi);
4364
4365 if (add) {
4366 bgp_evpn_advertise_type5_route(bgp_vrf, &ip_prefix,
4367 NULL, afi, safi);
4368 } else {
4369 bgp_evpn_withdraw_type5_route(bgp_vrf, &ip_prefix,
4370 afi, safi);
4371 }
4372}
4373
4374
2f69f6d3 4375/*
4376 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4377 * to this function correspond to those of the source IP prefix (best
4378 * path in the case of the attr. In the case of a local prefix (when we
4379 * are advertising local subnets), the src_attr will be NULL.
4380 */
31310b25 4381void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, struct prefix *p,
996c9314
LB
4382 struct attr *src_attr, afi_t afi,
4383 safi_t safi)
5424b7ba
MK
4384{
4385 int ret = 0;
4386 struct prefix_evpn evp;
4387 char buf[PREFIX_STRLEN];
7c82b312 4388
31310b25 4389 build_type5_prefix_from_ip_prefix(&evp, p);
2f69f6d3 4390 ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr);
4391 if (ret)
e50f7cfd 4392 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
1c50c1c0
QY
4393 "%u: Failed to create type-5 route for prefix %s",
4394 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)));
342dd0c6 4395}
4396
2f69f6d3 4397/* Inject all prefixes of a particular address-family (currently, IPv4 or
4398 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4399 * advertisement is enabled.
4400 */
996c9314
LB
4401void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
4402 safi_t safi)
342dd0c6 4403{
4404 struct bgp_table *table = NULL;
4405 struct bgp_node *rn = NULL;
40381db7 4406 struct bgp_path_info *pi;
342dd0c6 4407
053905d2 4408 table = bgp_vrf->rib[afi][safi];
31310b25 4409 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2f69f6d3 4410 /* Need to identify the "selected" route entry to use its
f106e3a7 4411 * attribute. Also, ensure that the route is injectable
4412 * into EVPN.
2f69f6d3 4413 * TODO: Support for AddPath for EVPN.
4414 */
6f94b685 4415 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next) {
40381db7 4416 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
f106e3a7 4417 && is_route_injectable_into_evpn(pi)) {
53c84f78
MK
4418
4419 /* apply the route-map */
4420 if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
b68885f9 4421 route_map_result_t ret;
53c84f78 4422
996c9314
LB
4423 ret = route_map_apply(
4424 bgp_vrf->adv_cmd_rmap[afi][safi]
4425 .map,
40381db7 4426 &rn->p, RMAP_BGP, pi);
53c84f78
MK
4427 if (ret == RMAP_DENYMATCH)
4428 continue;
4429 }
996c9314 4430 bgp_evpn_advertise_type5_route(
40381db7 4431 bgp_vrf, &rn->p, pi->attr, afi, safi);
2f69f6d3 4432 break;
4433 }
4434 }
31310b25 4435 }
342dd0c6 4436}
4437
996c9314 4438void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl)
c581d8b0
MK
4439{
4440 struct listnode *node, *nnode, *node_to_del;
4441 struct ecommunity *ecom, *ecom_auto;
4442 struct ecommunity_val eval;
4443
bf1061d8
VB
4444 if (bgp->advertise_autort_rfc8365)
4445 vni |= EVPN_AUTORT_VXLAN;
c581d8b0
MK
4446 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
4447
4448 ecom_auto = ecommunity_new();
4449 ecommunity_add_val(ecom_auto, &eval);
4450 node_to_del = NULL;
4451
4452 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
4453 if (ecommunity_match(ecom, ecom_auto)) {
4454 ecommunity_free(&ecom);
4455 node_to_del = node;
4456 }
4457 }
4458
4459 if (node_to_del)
4460 list_delete_node(rtl, node_to_del);
4461
4462 ecommunity_free(&ecom_auto);
4463}
4464
4465void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
10ebe1ab 4466 struct ecommunity *ecomadd)
c581d8b0 4467{
5ba238b7 4468 /* uninstall routes from vrf */
3d0b43d7 4469 if (is_l3vni_live(bgp_vrf))
4470 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
4471
4472 /* Cleanup the RT to VRF mapping */
4473 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4474
c581d8b0
MK
4475 /* Remove auto generated RT */
4476 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
4477
4478 /* Add the newly configured RT to RT list */
4479 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
4480 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4481
3d0b43d7 4482 /* map VRF to its RTs and install routes matching the new RTs */
4483 if (is_l3vni_live(bgp_vrf)) {
4484 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4485 install_routes_for_vrf(bgp_vrf);
4486 }
c581d8b0
MK
4487}
4488
4489void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
4490 struct ecommunity *ecomdel)
4491{
4492 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4493 struct ecommunity *ecom = NULL;
4494
5ba238b7 4495 /* uninstall routes from vrf */
3d0b43d7 4496 if (is_l3vni_live(bgp_vrf))
4497 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
4498
4499 /* Cleanup the RT to VRF mapping */
4500 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4501
c581d8b0
MK
4502 /* remove the RT from the RT list */
4503 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4504 if (ecommunity_match(ecom, ecomdel)) {
4505 ecommunity_free(&ecom);
4506 node_to_del = node;
4507 break;
4508 }
4509 }
4510
4511 if (node_to_del)
4512 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
4513
b3a4db3d 4514 assert(bgp_vrf->vrf_import_rtl);
c581d8b0 4515 /* fallback to auto import rt, if this was the last RT */
1230a82d 4516 if (bgp_vrf->vrf_import_rtl && list_isempty(bgp_vrf->vrf_import_rtl)) {
c581d8b0
MK
4517 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4518 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
4519 }
4520
3d0b43d7 4521 /* map VRFs to its RTs and install routes matching this new RT */
4522 if (is_l3vni_live(bgp_vrf)) {
4523 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4524 install_routes_for_vrf(bgp_vrf);
4525 }
c581d8b0
MK
4526}
4527
4528void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
4529 struct ecommunity *ecomadd)
4530{
4531 /* remove auto-generated RT */
4532 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
4533
4534 /* Add the new RT to the RT list */
4535 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
4536 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4537
f1f8b53c 4538 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
4539}
4540
4541void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
4542 struct ecommunity *ecomdel)
4543{
4544 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4545 struct ecommunity *ecom = NULL;
4546
4547 /* Remove the RT from the RT list */
4548 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
4549 if (ecommunity_match(ecom, ecomdel)) {
4550 ecommunity_free(&ecom);
4551 node_to_del = node;
4552 break;
4553 }
4554 }
4555
4556 if (node_to_del)
4557 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
4558
1525e99f
DS
4559 /*
4560 * Temporary assert to make SA happy.
4561 * The ALL_LIST_ELEMENTS macro above has a NULL check
4562 * which means that SA is going to complain about
4563 * the list_isempty call, which doesn't NULL check.
4564 * So until we get this situation cleaned up, here
4565 * we are.
4566 */
4567 assert(bgp_vrf->vrf_export_rtl);
4568
c581d8b0 4569 /* fall back to auto-generated RT if this was the last RT */
1525e99f 4570 if (list_isempty(bgp_vrf->vrf_export_rtl)) {
c581d8b0
MK
4571 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4572 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
4573 }
4574
f1f8b53c 4575 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
4576}
4577
2d48ee25 4578/*
4579 * Handle change to BGP router id. This is invoked twice by the change
4580 * handler, first before the router id has been changed and then after
4581 * the router id has been changed. The first invocation will result in
676f83b9 4582 * local routes for all VNIs/VRF being deleted and withdrawn and the next
2d48ee25 4583 * will result in the routes being re-advertised.
4584 */
d62a17ae 4585void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
2d48ee25 4586{
676f83b9 4587 if (withdraw) {
4588
4589 /* delete and withdraw all the type-5 routes
523cafc4 4590 stored in the global table for this vrf
4591 */
80b140af 4592 withdraw_router_id_vrf(bgp);
676f83b9 4593
4594 /* delete all the VNI routes (type-2/type-3) routes for all the
523cafc4 4595 * L2-VNIs
4596 */
d62a17ae 4597 hash_iterate(bgp->vnihash,
e3b78da8 4598 (void (*)(struct hash_bucket *,
d62a17ae 4599 void *))withdraw_router_id_vni,
4600 bgp);
676f83b9 4601 } else {
4602
4603 /* advertise all routes in the vrf as type-5 routes with the new
523cafc4 4604 * RD
4605 */
80b140af 4606 update_router_id_vrf(bgp);
676f83b9 4607
4608 /* advertise all the VNI routes (type-2/type-3) routes with the
523cafc4 4609 * new RD
4610 */
d62a17ae 4611 hash_iterate(bgp->vnihash,
e3b78da8 4612 (void (*)(struct hash_bucket *,
d62a17ae 4613 void *))update_router_id_vni,
4614 bgp);
676f83b9 4615 }
2d48ee25 4616}
4617
bf1061d8
VB
4618/*
4619 * Handle change to auto-RT algorithm - update and advertise local routes.
4620 */
4621void bgp_evpn_handle_autort_change(struct bgp *bgp)
4622{
4623 hash_iterate(bgp->vnihash,
e3b78da8 4624 (void (*)(struct hash_bucket *,
bf1061d8
VB
4625 void*))update_autort_vni,
4626 bgp);
4627}
4628
90e60aa7 4629/*
4630 * Handle change to export RT - update and advertise local routes.
4631 */
d62a17ae 4632int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 4633{
d62a17ae 4634 return update_routes_for_vni(bgp, vpn);
90e60aa7 4635}
4636
996c9314 4637void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
676f83b9 4638{
4639 if (withdraw)
4640 delete_withdraw_vrf_routes(bgp_vrf);
4641 else
4642 update_advertise_vrf_routes(bgp_vrf);
4643}
4644
90e60aa7 4645/*
4646 * Handle change to RD. This is invoked twice by the change handler,
4647 * first before the RD has been changed and then after the RD has
4648 * been changed. The first invocation will result in local routes
4649 * of this VNI being deleted and withdrawn and the next will result
4650 * in the routes being re-advertised.
4651 */
d62a17ae 4652void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
4653 int withdraw)
90e60aa7 4654{
d62a17ae 4655 if (withdraw)
4656 delete_withdraw_vni_routes(bgp, vpn);
4657 else
4658 update_advertise_vni_routes(bgp, vpn);
90e60aa7 4659}
4660
4661/*
4662 * Install routes for this VNI. Invoked upon change to Import RT.
4663 */
d62a17ae 4664int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 4665{
d62a17ae 4666 return install_routes_for_vni(bgp, vpn);
90e60aa7 4667}
4668
4669/*
4670 * Uninstall all routes installed for this VNI. Invoked upon change
4671 * to Import RT.
4672 */
d62a17ae 4673int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 4674{
d62a17ae 4675 return uninstall_routes_for_vni(bgp, vpn);
90e60aa7 4676}
4677
b16031a2 4678/*
b57ba6d2 4679 * TODO: Hardcoded for a maximum of 2 VNIs right now
b16031a2 4680 */
d7c0a89a 4681char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf,
996c9314 4682 int len)
b16031a2 4683{
b57ba6d2 4684 vni_t vni1, vni2;
b16031a2 4685
b57ba6d2
MK
4686 vni1 = label2vni(label);
4687 if (num_labels == 2) {
996c9314 4688 vni2 = label2vni(label + 1);
b57ba6d2
MK
4689 snprintf(buf, len, "%u/%u", vni1, vni2);
4690 } else
4691 snprintf(buf, len, "%u", vni1);
d62a17ae 4692 return buf;
b16031a2 4693}
4694
9c92b5f7
MK
4695/*
4696 * Function to convert evpn route to json format.
4697 * NOTE: We don't use prefix2str as the output here is a bit different.
4698 */
57f7feb6 4699void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json)
9c92b5f7 4700{
b682f6de 4701 char buf1[ETHER_ADDR_STRLEN];
4702 char buf2[PREFIX2STR_BUFFER];
9c92b5f7 4703
b682f6de 4704 if (!json)
4705 return;
9c92b5f7 4706
dff8f48d 4707 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
b682f6de 4708 json_object_int_add(json, "routeType", p->prefix.route_type);
3714a385 4709 json_object_int_add(json, "ethTag",
4710 p->prefix.imet_addr.eth_tag);
57f7feb6 4711 json_object_int_add(json, "ipLen",
3714a385 4712 is_evpn_prefix_ipaddr_v4(p)
57f7feb6
MK
4713 ? IPV4_MAX_BITLEN
4714 : IPV6_MAX_BITLEN);
b682f6de 4715 json_object_string_add(json, "ip",
3714a385 4716 inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4));
57f7feb6 4717 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3714a385 4718 if (is_evpn_prefix_ipaddr_none(p)) {
57f7feb6
MK
4719 json_object_int_add(json, "routeType",
4720 p->prefix.route_type);
3714a385 4721 json_object_int_add(json, "ethTag",
4722 p->prefix.macip_addr.eth_tag);
57f7feb6
MK
4723 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
4724 json_object_string_add(json, "mac",
3714a385 4725 prefix_mac2str(&p->prefix.macip_addr.mac,
57f7feb6
MK
4726 buf1,
4727 sizeof(buf1)));
dff8f48d 4728 } else {
d7c0a89a 4729 uint8_t family;
dff8f48d 4730
3714a385 4731 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET
57f7feb6 4732 : AF_INET6;
dff8f48d 4733
b682f6de 4734 json_object_int_add(json, "routeType",
57f7feb6 4735 p->prefix.route_type);
3714a385 4736 json_object_int_add(json, "ethTag",
4737 p->prefix.macip_addr.eth_tag);
57f7feb6 4738 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
b682f6de 4739 json_object_string_add(json, "mac",
3714a385 4740 prefix_mac2str(&p->prefix.macip_addr.mac,
57f7feb6
MK
4741 buf1,
4742 sizeof(buf1)));
b682f6de 4743 json_object_int_add(json, "ipLen",
3714a385 4744 is_evpn_prefix_ipaddr_v4(p)
57f7feb6
MK
4745 ? IPV4_MAX_BITLEN
4746 : IPV6_MAX_BITLEN);
4747 json_object_string_add(
4748 json, "ip",
3714a385 4749 inet_ntop(family,
4750 &p->prefix.macip_addr.ip.ip.addr,
4751 buf2,
57f7feb6 4752 PREFIX2STR_BUFFER));
dff8f48d
MK
4753 }
4754 } else {
4755 /* Currently, this is to cater to other AF_ETHERNET code. */
4756 }
9c92b5f7
MK
4757}
4758
520d5d76 4759/*
4760 * Function to convert evpn route to string.
4761 * NOTE: We don't use prefix2str as the output here is a bit different.
4762 */
d62a17ae 4763char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
4764{
4765 char buf1[ETHER_ADDR_STRLEN];
4766 char buf2[PREFIX2STR_BUFFER];
50f74cf1 4767 char buf3[ESI_STR_LEN];
d62a17ae 4768
4769 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
554cd77a 4770 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type,
3714a385 4771 p->prefix.imet_addr.eth_tag,
4772 is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BITLEN
d62a17ae 4773 : IPV6_MAX_BITLEN,
3714a385 4774 inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4));
d62a17ae 4775 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3714a385 4776 if (is_evpn_prefix_ipaddr_none(p))
554cd77a
VB
4777 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]",
4778 p->prefix.route_type,
3714a385 4779 p->prefix.macip_addr.eth_tag,
554cd77a 4780 8 * ETH_ALEN,
3714a385 4781 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
d62a17ae 4782 sizeof(buf1)));
4783 else {
d7c0a89a 4784 uint8_t family;
d62a17ae 4785
3714a385 4786 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET
d62a17ae 4787 : AF_INET6;
554cd77a
VB
4788 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4789 p->prefix.route_type,
3714a385 4790 p->prefix.macip_addr.eth_tag,
554cd77a 4791 8 * ETH_ALEN,
3714a385 4792 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
d62a17ae 4793 sizeof(buf1)),
4794 family == AF_INET ? IPV4_MAX_BITLEN
4795 : IPV6_MAX_BITLEN,
3714a385 4796 inet_ntop(family,
4797 &p->prefix.macip_addr.ip.ip.addr,
4798 buf2,
d62a17ae 4799 PREFIX2STR_BUFFER));
4800 }
342dd0c6 4801 } else if (p->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
554cd77a
VB
4802 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]",
4803 p->prefix.route_type,
3714a385 4804 p->prefix.prefix_addr.eth_tag,
4805 p->prefix.prefix_addr.ip_prefix_length,
4806 is_evpn_prefix_ipaddr_v4(p)
4807 ? inet_ntoa(p->prefix.prefix_addr.ip.ipaddr_v4)
4808 : inet6_ntoa(p->prefix.prefix_addr.ip.ipaddr_v6));
50f74cf1 4809 } else if (p->prefix.route_type == BGP_EVPN_ES_ROUTE) {
4810 snprintf(buf, len, "[%d]:[%s]:[%d]:[%s]",
4811 p->prefix.route_type,
4812 esi_to_str(&p->prefix.es_addr.esi, buf3, sizeof(buf3)),
4813 is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BITLEN
4814 : IPV6_MAX_BITLEN,
4815 inet_ntoa(p->prefix.es_addr.ip.ipaddr_v4));
d62a17ae 4816 } else {
b03b8898 4817 /* For EVPN route types not supported yet. */
f9aa3e55
QY
4818 snprintf(buf, len, "(unsupported route type %d)",
4819 p->prefix.route_type);
d62a17ae 4820 }
4821
4822 return (buf);
520d5d76 4823}
4824
128ea8ab 4825/*
4826 * Encode EVPN prefix in Update (MP_REACH)
4827 */
d62a17ae 4828void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
996c9314 4829 struct prefix_rd *prd, mpls_label_t *label,
d7c0a89a
QY
4830 uint32_t num_labels, struct attr *attr,
4831 int addpath_encode, uint32_t addpath_tx_id)
d62a17ae 4832{
4833 struct prefix_evpn *evp = (struct prefix_evpn *)p;
b57ba6d2 4834 int len, ipa_len = 0;
d62a17ae 4835
4836 if (addpath_encode)
4837 stream_putl(s, addpath_tx_id);
4838
4839 /* Route type */
4840 stream_putc(s, evp->prefix.route_type);
4841
4842 switch (evp->prefix.route_type) {
4843 case BGP_EVPN_MAC_IP_ROUTE:
3714a385 4844 if (is_evpn_prefix_ipaddr_v4(evp))
d62a17ae 4845 ipa_len = IPV4_MAX_BYTELEN;
3714a385 4846 else if (is_evpn_prefix_ipaddr_v6(evp))
d62a17ae 4847 ipa_len = IPV6_MAX_BYTELEN;
b57ba6d2
MK
4848 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4849 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
4850 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
4851 len += 3;
4852 stream_putc(s, len);
996c9314 4853 stream_put(s, prd->val, 8); /* RD */
554cd77a
VB
4854 if (attr)
4855 stream_put(s, &attr->evpn_overlay.eth_s_id, ESI_LEN);
4856 else
4857 stream_put(s, 0, 10);
3714a385 4858 stream_putl(s, evp->prefix.macip_addr.eth_tag); /* Ethernet Tag ID */
28328ea9 4859 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
3714a385 4860 stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */
4861 stream_putc(s, 8 * ipa_len); /* IP address Length */
4862 if (ipa_len) /* IP */
4863 stream_put(s, &evp->prefix.macip_addr.ip.ip.addr,
4864 ipa_len);
b57ba6d2
MK
4865 /* 1st label is the L2 VNI */
4866 stream_put(s, label, BGP_LABEL_BYTES);
4867 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4868 if (ipa_len && num_labels > 1)
996c9314 4869 stream_put(s, label + 1, BGP_LABEL_BYTES);
d62a17ae 4870 break;
4871
4872 case BGP_EVPN_IMET_ROUTE:
4873 stream_putc(s, 17); // TODO: length - assumes IPv4 address
4874 stream_put(s, prd->val, 8); /* RD */
3714a385 4875 stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */
d62a17ae 4876 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
4877 /* Originating Router's IP Addr */
3714a385 4878 stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4);
d62a17ae 4879 break;
4880
50f74cf1 4881 case BGP_EVPN_ES_ROUTE:
4882 stream_putc(s, 23); /* TODO: length: assumes ipv4 VTEP */
4883 stream_put(s, prd->val, 8); /* RD */
4884 stream_put(s, evp->prefix.es_addr.esi.val, 10); /* ESI */
4885 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
2bb9eff4
DS
4886 /* VTEP IP */
4887 stream_put_in_addr(s, &evp->prefix.es_addr.ip.ipaddr_v4);
50f74cf1 4888 break;
4889
d62a17ae 4890 case BGP_EVPN_IP_PREFIX_ROUTE:
4891 /* TODO: AddPath support. */
b57ba6d2 4892 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
d62a17ae 4893 break;
4894
4895 default:
4896 break;
4897 }
4898}
4899
4900int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
4901 struct bgp_nlri *packet, int withdraw)
4902{
d7c0a89a
QY
4903 uint8_t *pnt;
4904 uint8_t *lim;
d62a17ae 4905 afi_t afi;
4906 safi_t safi;
d7c0a89a 4907 uint32_t addpath_id;
d62a17ae 4908 int addpath_encoded;
4909 int psize = 0;
d7c0a89a 4910 uint8_t rtype;
d62a17ae 4911 struct prefix p;
4912
d62a17ae 4913 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4914 pnt = packet->nlri;
4915 lim = pnt + packet->length;
4916 afi = packet->afi;
4917 safi = packet->safi;
4918 addpath_id = 0;
4919
4920 addpath_encoded =
4921 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
4922 && CHECK_FLAG(peer->af_cap[afi][safi],
4923 PEER_CAP_ADDPATH_AF_TX_RCV));
4924
4925 for (; pnt < lim; pnt += psize) {
4926 /* Clear prefix structure. */
4927 memset(&p, 0, sizeof(struct prefix));
4928
4929 /* Deal with path-id if AddPath is supported. */
4930 if (addpath_encoded) {
4931 /* When packet overflow occurs return immediately. */
4932 if (pnt + BGP_ADDPATH_ID_LEN > lim)
513386b5 4933 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
d62a17ae 4934
4935 addpath_id = ntohl(*((uint32_t *)pnt));
4936 pnt += BGP_ADDPATH_ID_LEN;
4937 }
4938
4939 /* All EVPN NLRI types start with type and length. */
4940 if (pnt + 2 > lim)
513386b5 4941 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE;
d62a17ae 4942
4943 rtype = *pnt++;
3f54c705 4944 psize = *pnt++;
d62a17ae 4945
4946 /* When packet overflow occur return immediately. */
4947 if (pnt + psize > lim)
513386b5 4948 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
d62a17ae 4949
4950 switch (rtype) {
4951 case BGP_EVPN_MAC_IP_ROUTE:
4952 if (process_type2_route(peer, afi, safi,
4953 withdraw ? NULL : attr, pnt,
4954 psize, addpath_id)) {
af4c2728 4955 flog_err(
e50f7cfd 4956 EC_BGP_EVPN_FAIL,
d62a17ae 4957 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4958 peer->bgp->vrf_id, peer->host, psize);
513386b5 4959 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE;
d62a17ae 4960 }
4961 break;
4962
4963 case BGP_EVPN_IMET_ROUTE:
4964 if (process_type3_route(peer, afi, safi,
4965 withdraw ? NULL : attr, pnt,
4966 psize, addpath_id)) {
af4c2728 4967 flog_err(
e50f7cfd 4968 EC_BGP_PKT_PROCESS,
d62a17ae 4969 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4970 peer->bgp->vrf_id, peer->host, psize);
513386b5 4971 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE;
d62a17ae 4972 }
4973 break;
4974
50f74cf1 4975 case BGP_EVPN_ES_ROUTE:
4976 if (process_type4_route(peer, afi, safi,
4977 withdraw ? NULL : attr, pnt,
4978 psize, addpath_id)) {
af4c2728 4979 flog_err(
e50f7cfd 4980 EC_BGP_PKT_PROCESS,
50f74cf1 4981 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4982 peer->bgp->vrf_id, peer->host, psize);
513386b5 4983 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE;
50f74cf1 4984 }
4985 break;
4986
d62a17ae 4987 case BGP_EVPN_IP_PREFIX_ROUTE:
f007bdce
CS
4988 if (process_type5_route(peer, afi, safi,
4989 withdraw ? NULL : attr, pnt,
4990 psize, addpath_id)) {
af4c2728 4991 flog_err(
e50f7cfd 4992 EC_BGP_PKT_PROCESS,
d62a17ae 4993 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4994 peer->bgp->vrf_id, peer->host, psize);
513386b5 4995 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE;
d62a17ae 4996 }
4997 break;
4998
4999 default:
5000 break;
5001 }
5002 }
5003
5004 /* Packet length consistency check. */
5005 if (pnt != lim)
513386b5 5006 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
d62a17ae 5007
513386b5 5008 return BGP_NLRI_PARSE_OK;
128ea8ab 5009}
5010
10ebe1ab
MK
5011/*
5012 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5013 * The mapping will be used during route processing.
5014 * bgp_def: default bgp instance
5015 * bgp_vrf: specific bgp vrf instance on which RT is configured
5016 */
5017void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
5018{
5019 int i = 0;
5020 struct ecommunity_val *eval = NULL;
5021 struct listnode *node = NULL, *nnode = NULL;
5022 struct ecommunity *ecom = NULL;
5023
5024 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
5025 for (i = 0; i < ecom->size; i++) {
5026 eval = (struct ecommunity_val *)(ecom->val
5027 + (i
5028 * ECOMMUNITY_SIZE));
5029 map_vrf_to_rt(bgp_vrf, eval);
5030 }
5031 }
5032}
5033
5034/*
5035 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5036 */
5037void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
5038{
5039 int i;
5040 struct ecommunity_val *eval;
5041 struct listnode *node, *nnode;
5042 struct ecommunity *ecom;
5043
5044 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
5045 for (i = 0; i < ecom->size; i++) {
5046 struct vrf_irt_node *irt;
5047 struct ecommunity_val eval_tmp;
5048
5049 eval = (struct ecommunity_val *)(ecom->val
5050 + (i
5051 * ECOMMUNITY_SIZE));
5052 /* If using "automatic" RT, we only care about the
5053 * local-admin sub-field.
5054 * This is to facilitate using VNI as the RT for EBGP
5055 * peering too.
5056 */
5057 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
5058 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
5059 BGP_VRF_IMPORT_RT_CFGD))
5060 mask_ecom_global_admin(&eval_tmp, eval);
5061
5062 irt = lookup_vrf_import_rt(&eval_tmp);
5063 if (irt)
5064 unmap_vrf_from_rt(bgp_vrf, irt);
5065 }
5066 }
5067}
5068
5069
128ea8ab 5070/*
5071 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5072 * The mapping will be used during route processing.
5073 */
d62a17ae 5074void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5075{
d62a17ae 5076 int i;
5077 struct ecommunity_val *eval;
5078 struct listnode *node, *nnode;
5079 struct ecommunity *ecom;
128ea8ab 5080
d62a17ae 5081 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5082 for (i = 0; i < ecom->size; i++) {
5083 eval = (struct ecommunity_val *)(ecom->val
5084 + (i
5085 * ECOMMUNITY_SIZE));
5086 map_vni_to_rt(bgp, vpn, eval);
5087 }
5088 }
128ea8ab 5089}
5090
5091/*
5092 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5093 */
d62a17ae 5094void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5095{
d62a17ae 5096 int i;
5097 struct ecommunity_val *eval;
5098 struct listnode *node, *nnode;
5099 struct ecommunity *ecom;
128ea8ab 5100
d62a17ae 5101 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5102 for (i = 0; i < ecom->size; i++) {
5103 struct irt_node *irt;
5104 struct ecommunity_val eval_tmp;
128ea8ab 5105
d62a17ae 5106 eval = (struct ecommunity_val *)(ecom->val
5107 + (i
5108 * ECOMMUNITY_SIZE));
5109 /* If using "automatic" RT, we only care about the
5110 * local-admin sub-field.
5111 * This is to facilitate using VNI as the RT for EBGP
5112 * peering too.
5113 */
5114 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
5115 if (!is_import_rt_configured(vpn))
5116 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 5117
d62a17ae 5118 irt = lookup_import_rt(bgp, &eval_tmp);
5119 if (irt)
5120 unmap_vni_from_rt(bgp, vpn, irt);
5121 }
5122 }
128ea8ab 5123}
5124
5125/*
5126 * Derive Import RT automatically for VNI and map VNI to RT.
5127 * The mapping will be used during route processing.
5128 */
d62a17ae 5129void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5130{
c581d8b0 5131 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
d62a17ae 5132 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
128ea8ab 5133
d62a17ae 5134 /* Map RT to VNI */
5135 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
128ea8ab 5136}
5137
5138/*
5139 * Derive Export RT automatically for VNI.
5140 */
d62a17ae 5141void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5142{
c581d8b0 5143 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
d62a17ae 5144 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
128ea8ab 5145}
5146
676f83b9 5147/*
5148 * Derive RD automatically for VNI using passed information - it
5149 * is of the form RouterId:unique-id-for-vni.
5150 */
5151void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
5152{
9e97ff03
CS
5153 if (is_vrf_rd_configured(bgp))
5154 return;
5155
92708db6 5156 form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd);
676f83b9 5157}
5158
128ea8ab 5159/*
5160 * Derive RD automatically for VNI using passed information - it
5161 * is of the form RouterId:unique-id-for-vni.
5162 */
d62a17ae 5163void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5164{
d62a17ae 5165 char buf[100];
128ea8ab 5166
d62a17ae 5167 vpn->prd.family = AF_UNSPEC;
5168 vpn->prd.prefixlen = 64;
5169 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
cbb65f5e 5170 (void)str2prefix_rd(buf, &vpn->prd);
d62a17ae 5171 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
128ea8ab 5172}
5173
7df407ed
CS
5174/*
5175 * Lookup L3-VNI
5176 */
5177bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni)
5178{
5179 struct list *inst = bm->bgp;
5180 struct listnode *node;
5181 struct bgp *bgp_vrf;
5182
5183 for (ALL_LIST_ELEMENTS_RO(inst, node, bgp_vrf)) {
5184 if (bgp_vrf->l3vni == vni)
5185 return true;
5186 }
5187
5188 return false;
5189}
5190
128ea8ab 5191/*
5192 * Lookup VNI.
5193 */
d62a17ae 5194struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
128ea8ab 5195{
d62a17ae 5196 struct bgpevpn *vpn;
5197 struct bgpevpn tmp;
128ea8ab 5198
d62a17ae 5199 memset(&tmp, 0, sizeof(struct bgpevpn));
5200 tmp.vni = vni;
5201 vpn = hash_lookup(bgp->vnihash, &tmp);
5202 return vpn;
128ea8ab 5203}
5204
5205/*
5206 * Create a new vpn - invoked upon configuration or zebra notification.
5207 */
d62a17ae 5208struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
76d07c7a
AK
5209 struct in_addr originator_ip,
5210 vrf_id_t tenant_vrf_id,
5211 struct in_addr mcast_grp)
128ea8ab 5212{
d62a17ae 5213 struct bgpevpn *vpn;
128ea8ab 5214
d62a17ae 5215 if (!bgp)
5216 return NULL;
128ea8ab 5217
d62a17ae 5218 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
128ea8ab 5219
d62a17ae 5220 /* Set values - RD and RT set to defaults. */
5221 vpn->vni = vni;
5222 vpn->originator_ip = originator_ip;
29c53922 5223 vpn->tenant_vrf_id = tenant_vrf_id;
76d07c7a 5224 vpn->mcast_grp = mcast_grp;
128ea8ab 5225
d62a17ae 5226 /* Initialize route-target import and export lists */
5227 vpn->import_rtl = list_new();
5228 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
987d8198 5229 vpn->import_rtl->del = evpn_xxport_delete_ecomm;
d62a17ae 5230 vpn->export_rtl = list_new();
5231 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
987d8198 5232 vpn->export_rtl->del = evpn_xxport_delete_ecomm;
e9eb5f63 5233 bf_assign_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 5234 derive_rd_rt_for_vni(bgp, vpn);
128ea8ab 5235
d62a17ae 5236 /* Initialize EVPN route table. */
960035b2 5237 vpn->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
128ea8ab 5238
d62a17ae 5239 /* Add to hash */
5240 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
5241 XFREE(MTYPE_BGP_EVPN, vpn);
5242 return NULL;
5243 }
6a8657d0
MK
5244
5245 /* add to l2vni list on corresponding vrf */
5246 bgpevpn_link_to_l3vni(vpn);
5247
d62a17ae 5248 QOBJ_REG(vpn, bgpevpn);
5249 return vpn;
128ea8ab 5250}
5251
5252/*
5253 * Free a given VPN - called in multiple scenarios such as zebra
5254 * notification, configuration being deleted, advertise-all-vni disabled etc.
5255 * This just frees appropriate memory, caller should have taken other
5256 * needed actions.
5257 */
d62a17ae 5258void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 5259{
6a8657d0 5260 bgpevpn_unlink_from_l3vni(vpn);
d62a17ae 5261 bgp_table_unlock(vpn->route_table);
5262 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
6a154c88
DL
5263 list_delete(&vpn->import_rtl);
5264 list_delete(&vpn->export_rtl);
e9eb5f63 5265 bf_release_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 5266 hash_release(bgp->vnihash, vpn);
5267 QOBJ_UNREG(vpn);
5268 XFREE(MTYPE_BGP_EVPN, vpn);
128ea8ab 5269}
5270
5271/*
50f74cf1 5272 * Lookup local ES.
5273 */
5274struct evpnes *bgp_evpn_lookup_es(struct bgp *bgp, esi_t *esi)
5275{
5276 struct evpnes *es;
5277 struct evpnes tmp;
5278
5279 memset(&tmp, 0, sizeof(struct evpnes));
5280 memcpy(&tmp.esi, esi, sizeof(esi_t));
5281 es = hash_lookup(bgp->esihash, &tmp);
5282 return es;
5283}
5284
5285/*
5286 * Create a new local es - invoked upon zebra notification.
5287 */
5288struct evpnes *bgp_evpn_es_new(struct bgp *bgp,
5289 esi_t *esi,
5290 struct ipaddr *originator_ip)
5291{
5292 char buf[100];
5293 struct evpnes *es;
5294
5295 if (!bgp)
5296 return NULL;
5297
5298 es = XCALLOC(MTYPE_BGP_EVPN_ES, sizeof(struct evpnes));
50f74cf1 5299
5300 /* set the ESI and originator_ip */
5301 memcpy(&es->esi, esi, sizeof(esi_t));
5302 memcpy(&es->originator_ip, originator_ip, sizeof(struct ipaddr));
5303
5304 /* Initialise the VTEP list */
5305 es->vtep_list = list_new();
ce167790 5306 es->vtep_list->cmp = evpn_vtep_ip_cmp;
50f74cf1 5307
5308 /* auto derive RD for this es */
5309 bf_assign_index(bm->rd_idspace, es->rd_id);
5310 es->prd.family = AF_UNSPEC;
5311 es->prd.prefixlen = 64;
5312 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), es->rd_id);
5313 (void)str2prefix_rd(buf, &es->prd);
5314
5315 /* Initialize the ES route table */
5316 es->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
5317
5318 /* Add to hash */
5319 if (!hash_get(bgp->esihash, es, hash_alloc_intern)) {
5320 XFREE(MTYPE_BGP_EVPN_ES, es);
5321 return NULL;
5322 }
5323
5324 QOBJ_REG(es, evpnes);
5325 return es;
5326}
5327
5328/*
5329 * Free a given ES -
5330 * This just frees appropriate memory, caller should have taken other
5331 * needed actions.
5332 */
5333void bgp_evpn_es_free(struct bgp *bgp, struct evpnes *es)
5334{
6a154c88 5335 list_delete(&es->vtep_list);
50f74cf1 5336 bgp_table_unlock(es->route_table);
5337 bf_release_index(bm->rd_idspace, es->rd_id);
5338 hash_release(bgp->esihash, es);
5339 QOBJ_UNREG(es);
5340 XFREE(MTYPE_BGP_EVPN_ES, es);
5341}
5342
5343/*
5344 * Import evpn route from global table to VNI/VRF/ESI.
128ea8ab 5345 */
d62a17ae 5346int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
40381db7 5347 struct prefix *p, struct bgp_path_info *pi)
128ea8ab 5348{
40381db7 5349 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 1);
128ea8ab 5350}
5351
5352/*
50f74cf1 5353 * Unimport evpn route from VNI/VRF/ESI.
128ea8ab 5354 */
d62a17ae 5355int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
40381db7 5356 struct prefix *p, struct bgp_path_info *pi)
128ea8ab 5357{
40381db7 5358 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 0);
128ea8ab 5359}
5360
db0e1937
MK
5361/* filter routes which have martian next hops */
5362int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
5363{
0291c246
MK
5364 afi_t afi;
5365 safi_t safi;
5366 struct bgp_node *rd_rn, *rn;
5367 struct bgp_table *table;
40381db7 5368 struct bgp_path_info *pi;
db0e1937
MK
5369
5370 afi = AFI_L2VPN;
5371 safi = SAFI_EVPN;
5372
5373 /* Walk entire global routing table and evaluate routes which could be
5374 * imported into this VPN. Note that we cannot just look at the routes
5375 * for the VNI's RD -
5376 * remote routes applicable for this VNI could have any RD.
5377 */
5378 /* EVPN routes are a 2-level table. */
5379 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
5380 rd_rn = bgp_route_next(rd_rn)) {
67009e22 5381 table = bgp_node_get_bgp_table_info(rd_rn);
db0e1937
MK
5382 if (!table)
5383 continue;
5384
5385 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
5386
6f94b685
DS
5387 for (pi = bgp_node_get_bgp_path_info(rn); pi;
5388 pi = pi->next) {
db0e1937
MK
5389
5390 /* Consider "valid" remote routes applicable for
5391 * this VNI. */
40381db7
DS
5392 if (!(pi->type == ZEBRA_ROUTE_BGP
5393 && pi->sub_type == BGP_ROUTE_NORMAL))
db0e1937
MK
5394 continue;
5395
40381db7 5396 if (bgp_nexthop_self(bgp, pi->attr->nexthop)) {
db0e1937 5397
8ba71050 5398 char attr_str[BUFSIZ] = {0};
db0e1937
MK
5399 char pbuf[PREFIX_STRLEN];
5400
40381db7 5401 bgp_dump_attr(pi->attr, attr_str,
db0e1937
MK
5402 BUFSIZ);
5403
40381db7 5404 if (bgp_debug_update(pi->peer, &rn->p,
db0e1937
MK
5405 NULL, 1))
5406 zlog_debug(
b682f6de 5407 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
db0e1937
MK
5408 bgp->vrf_id,
5409 prefix2str(
60466a63 5410 &rn->p, pbuf,
db0e1937
MK
5411 sizeof(pbuf)),
5412 attr_str);
5413
5414 bgp_evpn_unimport_route(bgp, afi, safi,
40381db7 5415 &rn->p, pi);
db0e1937 5416
40381db7 5417 bgp_rib_remove(rn, pi, pi->peer, afi,
60466a63 5418 safi);
db0e1937 5419 }
db0e1937
MK
5420 }
5421 }
5422 }
5423
5424 return 0;
5425}
5426
128ea8ab 5427/*
5428 * Handle del of a local MACIP.
5429 */
d62a17ae 5430int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
ec0ab544 5431 struct ipaddr *ip, int state)
128ea8ab 5432{
d62a17ae 5433 struct bgpevpn *vpn;
5434 struct prefix_evpn p;
ec0ab544 5435 struct bgp_node *rn;
128ea8ab 5436
d62a17ae 5437 /* Lookup VNI hash - should exist. */
5438 vpn = bgp_evpn_lookup_vni(bgp, vni);
5439 if (!vpn || !is_vni_live(vpn)) {
e50f7cfd 5440 flog_warn(EC_BGP_EVPN_VPN_VNI,
28642513 5441 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
d62a17ae 5442 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5443 return -1;
5444 }
128ea8ab 5445
d62a17ae 5446 build_evpn_type2_prefix(&p, mac, ip);
ec0ab544
AK
5447 if (state == ZEBRA_NEIGH_ACTIVE) {
5448 /* Remove EVPN type-2 route and schedule for processing. */
5449 delete_evpn_route(bgp, vpn, &p);
5450 } else {
5451 /* Re-instate the current remote best path if any */
5452 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
5453 if (rn)
5454 evpn_zebra_reinstall_best_route(bgp, vpn, rn);
5455 }
128ea8ab 5456
d62a17ae 5457 return 0;
128ea8ab 5458}
5459
5460/*
5461 * Handle add of a local MACIP.
5462 */
d62a17ae 5463int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
f07e1c99 5464 struct ipaddr *ip, uint8_t flags, uint32_t seq)
128ea8ab 5465{
d62a17ae 5466 struct bgpevpn *vpn;
5467 struct prefix_evpn p;
128ea8ab 5468
d62a17ae 5469 /* Lookup VNI hash - should exist. */
5470 vpn = bgp_evpn_lookup_vni(bgp, vni);
5471 if (!vpn || !is_vni_live(vpn)) {
e50f7cfd 5472 flog_warn(EC_BGP_EVPN_VPN_VNI,
28642513 5473 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
d62a17ae 5474 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5475 return -1;
5476 }
128ea8ab 5477
d62a17ae 5478 /* Create EVPN type-2 route and schedule for processing. */
5479 build_evpn_type2_prefix(&p, mac, ip);
f07e1c99 5480 if (update_evpn_route(bgp, vpn, &p, flags, seq)) {
d62a17ae 5481 char buf[ETHER_ADDR_STRLEN];
5482 char buf2[INET6_ADDRSTRLEN];
128ea8ab 5483
af4c2728 5484 flog_err(
e50f7cfd 5485 EC_BGP_EVPN_ROUTE_CREATE,
ead40654 5486 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
1a98c087 5487 bgp->vrf_id, vpn->vni,
996c9314
LB
5488 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
5489 ? "sticky gateway"
5490 : "",
d62a17ae 5491 prefix_mac2str(mac, buf, sizeof(buf)),
996c9314 5492 ipaddr2str(ip, buf2, sizeof(buf2)), flags);
d62a17ae 5493 return -1;
5494 }
128ea8ab 5495
d62a17ae 5496 return 0;
128ea8ab 5497}
5498
e3b78da8 5499static void link_l2vni_hash_to_l3vni(struct hash_bucket *bucket,
6a8657d0
MK
5500 struct bgp *bgp_vrf)
5501{
e3b78da8 5502 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
5e53dce3 5503 struct bgp *bgp_evpn = NULL;
6a8657d0 5504
5e53dce3
T
5505 bgp_evpn = bgp_get_evpn();
5506 assert(bgp_evpn);
6a8657d0 5507
6a8657d0
MK
5508 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
5509 bgpevpn_link_to_l3vni(vpn);
5510}
5511
996c9314 5512int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id, struct ethaddr *rmac,
0483af6e 5513 struct in_addr originator_ip, int filter,
5514 ifindex_t svi_ifindex)
fe1dc5a3
MK
5515{
5516 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
5e53dce3 5517 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
f1f8b53c
MK
5518 struct listnode *node = NULL;
5519 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
5520 as_t as = 0;
5521
3621ebc5 5522 /* get the EVPN instance - required to get the AS number for VRF
523cafc4 5523 * auto-creatio
5524 */
5e53dce3
T
5525 bgp_evpn = bgp_get_evpn();
5526 if (!bgp_evpn) {
af4c2728 5527 flog_err(
e50f7cfd 5528 EC_BGP_NO_DFLT,
3621ebc5 5529 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
996c9314 5530 l3vni);
fe1dc5a3
MK
5531 return -1;
5532 }
5e53dce3 5533 as = bgp_evpn->as;
fe1dc5a3 5534
0437e105 5535 /* if the BGP vrf instance doesn't exist - create one */
3621ebc5 5536 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
fe1dc5a3
MK
5537 if (!bgp_vrf) {
5538
5539 int ret = 0;
5540
5541 ret = bgp_get(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
3621ebc5
T
5542 vrf_id == VRF_DEFAULT ? BGP_INSTANCE_TYPE_DEFAULT
5543 : BGP_INSTANCE_TYPE_VRF);
fe1dc5a3 5544 switch (ret) {
fe1dc5a3 5545 case BGP_ERR_AS_MISMATCH:
e50f7cfd 5546 flog_err(EC_BGP_EVPN_AS_MISMATCH,
1c50c1c0 5547 "BGP is already running; AS is %u\n", as);
fe1dc5a3
MK
5548 return -1;
5549 case BGP_ERR_INSTANCE_MISMATCH:
e50f7cfd 5550 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH,
1c50c1c0 5551 "BGP instance name and AS number mismatch\n");
fe1dc5a3
MK
5552 return -1;
5553 }
5554
5555 /* mark as auto created */
5556 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
5557 }
5558
0483af6e 5559 /* associate the vrf with l3vni and related parameters */
fe1dc5a3 5560 bgp_vrf->l3vni = l3vni;
fe1dc5a3 5561 memcpy(&bgp_vrf->rmac, rmac, sizeof(struct ethaddr));
b67a60d2 5562 bgp_vrf->originator_ip = originator_ip;
0483af6e 5563 bgp_vrf->l3vni_svi_ifindex = svi_ifindex;
b67a60d2 5564
c48d9f5f
MK
5565 /* set the right filter - are we using l3vni only for prefix routes? */
5566 if (filter)
5567 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5568
530e8a6e 5569 /* Map auto derive or configured RTs */
c581d8b0
MK
5570 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5571 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
530e8a6e
CS
5572 else
5573 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
5574
c581d8b0
MK
5575 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
5576 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
530e8a6e
CS
5577
5578 /* auto derive RD */
676f83b9 5579 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
fe1dc5a3 5580
6a8657d0 5581 /* link all corresponding l2vnis */
5e53dce3 5582 hash_iterate(bgp_evpn->vnihash,
e3b78da8 5583 (void (*)(struct hash_bucket *,
996c9314 5584 void *))link_l2vni_hash_to_l3vni,
6a8657d0
MK
5585 bgp_vrf);
5586
c48d9f5f
MK
5587 /* Only update all corresponding type-2 routes if we are advertising two
5588 * labels along with type-2 routes
5589 */
5590 if (!filter)
5591 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
5e53dce3 5592 update_routes_for_vni(bgp_evpn, vpn);
fe1dc5a3 5593
06d2e8f3
MK
5594 /* advertise type-5 routes if needed */
5595 update_advertise_vrf_routes(bgp_vrf);
5596
5ba238b7
MK
5597 /* install all remote routes belonging to this l3vni into correspondng
5598 * vrf */
5599 install_routes_for_vrf(bgp_vrf);
fe1dc5a3
MK
5600
5601 return 0;
5602}
5603
996c9314 5604int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
fe1dc5a3
MK
5605{
5606 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
5e53dce3 5607 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
f1f8b53c 5608 struct listnode *node = NULL;
18abc1eb 5609 struct listnode *next = NULL;
f1f8b53c 5610 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
5611
5612 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
5613 if (!bgp_vrf) {
af4c2728 5614 flog_err(
e50f7cfd 5615 EC_BGP_NO_DFLT,
996c9314
LB
5616 "Cannot process L3VNI %u Del - Could not find BGP instance",
5617 l3vni);
fe1dc5a3
MK
5618 return -1;
5619 }
5620
5e53dce3
T
5621 bgp_evpn = bgp_get_evpn();
5622 if (!bgp_evpn) {
af4c2728 5623 flog_err(
e50f7cfd 5624 EC_BGP_NO_DFLT,
3621ebc5 5625 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
996c9314 5626 l3vni);
f1f8b53c
MK
5627 return -1;
5628 }
5629
d846168d 5630 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
18ee8310 5631 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
d846168d
CS
5632 * routes. This will uninstalling the routes from zebra and decremnt the
5633 * bgp info count.
523cafc4 5634 */
d846168d 5635 uninstall_routes_for_vrf(bgp_vrf);
5ba238b7 5636
06d2e8f3
MK
5637 /* delete/withdraw all type-5 routes */
5638 delete_withdraw_vrf_routes(bgp_vrf);
5639
fe1dc5a3
MK
5640 /* remove the l3vni from vrf instance */
5641 bgp_vrf->l3vni = 0;
5642
5643 /* remove the Rmac from the BGP vrf */
5644 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
5645
530e8a6e 5646 /* remove default import RT or Unmap non-default import RT */
1525e99f 5647 if (!list_isempty(bgp_vrf->vrf_import_rtl)) {
10ebe1ab 5648 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
530e8a6e
CS
5649 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5650 list_delete_all_node(bgp_vrf->vrf_import_rtl);
23a06e11 5651 }
530e8a6e
CS
5652
5653 /* remove default export RT */
5654 if (!list_isempty(bgp_vrf->vrf_export_rtl) &&
5655 !CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
5ba238b7 5656 list_delete_all_node(bgp_vrf->vrf_export_rtl);
23a06e11 5657 }
fe1dc5a3 5658
f1f8b53c 5659 /* update all corresponding local mac-ip routes */
c48d9f5f
MK
5660 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
5661 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
5662 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5e53dce3 5663 update_routes_for_vni(bgp_evpn, vpn);
c48d9f5f
MK
5664 }
5665 }
fe1dc5a3 5666
18abc1eb 5667 /* If any L2VNIs point to this instance, unlink them. */
5668 for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn))
5669 bgpevpn_unlink_from_l3vni(vpn);
5670
ff9d54fb
CS
5671 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5672
fe1dc5a3
MK
5673 /* Delete the instance if it was autocreated */
5674 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
5675 bgp_delete(bgp_vrf);
5676
5677 return 0;
5678}
5679
128ea8ab 5680/*
5681 * Handle del of a local VNI.
5682 */
d62a17ae 5683int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
128ea8ab 5684{
d62a17ae 5685 struct bgpevpn *vpn;
128ea8ab 5686
d62a17ae 5687 /* Locate VNI hash */
5688 vpn = bgp_evpn_lookup_vni(bgp, vni);
5689 if (!vpn) {
1e00627b 5690 if (bgp_debug_zebra(NULL))
ade6974d 5691 flog_warn(
e50f7cfd 5692 EC_BGP_EVPN_VPN_VNI,
ade6974d
QY
5693 "%u: VNI hash entry for VNI %u not found at DEL",
5694 bgp->vrf_id, vni);
d62a17ae 5695 return 0;
5696 }
128ea8ab 5697
d62a17ae 5698 /* Remove all local EVPN routes and schedule for processing (to
5699 * withdraw from peers).
5700 */
5701 delete_routes_for_vni(bgp, vpn);
128ea8ab 5702
db0e1937
MK
5703 /*
5704 * tunnel is no longer active, del tunnel ip address from tip_hash
5705 */
5706 bgp_tip_del(bgp, &vpn->originator_ip);
5707
d62a17ae 5708 /* Clear "live" flag and see if hash needs to be freed. */
5709 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5710 if (!is_vni_configured(vpn))
5711 bgp_evpn_free(bgp, vpn);
128ea8ab 5712
d62a17ae 5713 return 0;
128ea8ab 5714}
5715
5716/*
d1911c26 5717 * Handle add (or update) of a local VNI. The VNI changes we care
5718 * about are for the local-tunnel-ip and the (tenant) VRF.
128ea8ab 5719 */
d62a17ae 5720int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
76d07c7a
AK
5721 struct in_addr originator_ip,
5722 vrf_id_t tenant_vrf_id,
5723 struct in_addr mcast_grp)
5724
d62a17ae 5725{
5726 struct bgpevpn *vpn;
5727 struct prefix_evpn p;
5728
d62a17ae 5729 /* Lookup VNI. If present and no change, exit. */
5730 vpn = bgp_evpn_lookup_vni(bgp, vni);
ddd16ed5 5731 if (vpn) {
29c53922 5732
d1911c26 5733 if (is_vni_live(vpn)
5734 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
76d07c7a 5735 && IPV4_ADDR_SAME(&vpn->mcast_grp, &mcast_grp)
d1911c26 5736 && vpn->tenant_vrf_id == tenant_vrf_id)
5737 /* Probably some other param has changed that we don't
5738 * care about. */
5739 return 0;
5740
76d07c7a
AK
5741 bgp_evpn_mcast_grp_change(bgp, vpn, mcast_grp);
5742
d1911c26 5743 /* Update tenant_vrf_id if it has changed. */
6a8657d0
MK
5744 if (vpn->tenant_vrf_id != tenant_vrf_id) {
5745 bgpevpn_unlink_from_l3vni(vpn);
29c53922 5746 vpn->tenant_vrf_id = tenant_vrf_id;
6a8657d0
MK
5747 bgpevpn_link_to_l3vni(vpn);
5748 }
29c53922 5749
d1911c26 5750 /* If tunnel endpoint IP has changed, update (and delete prior
5751 * type-3 route, if needed.)
5752 */
5753 if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
5754 handle_tunnel_ip_change(bgp, vpn, originator_ip);
d62a17ae 5755
d1911c26 5756 /* Update all routes with new endpoint IP and/or export RT
5757 * for VRFs
5758 */
5759 if (is_vni_live(vpn))
5760 update_routes_for_vni(bgp, vpn);
d62a17ae 5761 }
5762
5763 /* Create or update as appropriate. */
5764 if (!vpn) {
76d07c7a
AK
5765 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id,
5766 mcast_grp);
d62a17ae 5767 if (!vpn) {
af4c2728 5768 flog_err(
e50f7cfd 5769 EC_BGP_VNI,
d62a17ae 5770 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5771 bgp->vrf_id, vni);
5772 return -1;
5773 }
5774 }
5775
db0e1937 5776 /* if the VNI is live already, there is nothing more to do */
ddd16ed5
MK
5777 if (is_vni_live(vpn))
5778 return 0;
5779
d62a17ae 5780 /* Mark as "live" */
5781 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5782
db0e1937
MK
5783 /* tunnel is now active, add tunnel-ip to db */
5784 bgp_tip_add(bgp, &originator_ip);
5785
5786 /* filter routes as nexthop database has changed */
5787 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
5788
fd069644
DS
5789 /*
5790 * Create EVPN type-3 route and schedule for processing.
5791 *
5792 * RT-3 only if doing head-end replication
5793 */
833b8a50
AK
5794 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
5795 == VXLAN_FLOOD_HEAD_END_REPL) {
fd069644
DS
5796 build_evpn_type3_prefix(&p, vpn->originator_ip);
5797 if (update_evpn_route(bgp, vpn, &p, 0, 0)) {
5798 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
5799 "%u: Type3 route creation failure for VNI %u",
5800 bgp->vrf_id, vni);
5801 return -1;
5802 }
d62a17ae 5803 }
5804
5805 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5806 * VNI,
5807 * install them.
5808 */
5809 install_routes_for_vni(bgp, vpn);
5810
d7d97010
MK
5811 /* If we are advertising gateway mac-ip
5812 It needs to be conveyed again to zebra */
5813 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
5814
df070e6f
CS
5815 /* advertise svi mac-ip knob to zebra */
5816 bgp_zebra_advertise_svi_macip(bgp, vpn->advertise_svi_macip, vpn->vni);
5817
d62a17ae 5818 return 0;
b18825eb 5819}
14c1a7bf 5820
50f74cf1 5821/*
5822 * bgp_evpn_local_es_del
5823 */
5824int bgp_evpn_local_es_del(struct bgp *bgp,
5825 esi_t *esi,
5826 struct ipaddr *originator_ip)
5827{
5828 char buf[ESI_STR_LEN];
5829 struct evpnes *es = NULL;
5830
5831 if (!bgp->esihash) {
e50f7cfd 5832 flog_err(EC_BGP_ES_CREATE, "%u: ESI hash not yet created",
1c50c1c0 5833 bgp->vrf_id);
50f74cf1 5834 return -1;
5835 }
5836
5837 /* Lookup ESI hash - should exist. */
5838 es = bgp_evpn_lookup_es(bgp, esi);
5839 if (!es) {
e50f7cfd 5840 flog_warn(EC_BGP_EVPN_ESI,
28642513
DS
5841 "%u: ESI hash entry for ESI %s at Local ES DEL",
5842 bgp->vrf_id, esi_to_str(esi, buf, sizeof(buf)));
50f74cf1 5843 return -1;
5844 }
5845
5846 /* Delete all local EVPN ES routes from ESI table
2bb9eff4 5847 * and schedule for processing (to withdraw from peers))
50f74cf1 5848 */
5849 delete_routes_for_es(bgp, es);
5850
5851 /* free the hash entry */
5852 bgp_evpn_es_free(bgp, es);
5853
5854 return 0;
5855}
5856
5857/*
5858 * bgp_evpn_local_es_add
5859 */
5860int bgp_evpn_local_es_add(struct bgp *bgp,
5861 esi_t *esi,
5862 struct ipaddr *originator_ip)
5863{
5864 char buf[ESI_STR_LEN];
5865 struct evpnes *es = NULL;
5866 struct prefix_evpn p;
5867
5868 if (!bgp->esihash) {
e50f7cfd 5869 flog_err(EC_BGP_ES_CREATE, "%u: ESI hash not yet created",
1c50c1c0 5870 bgp->vrf_id);
50f74cf1 5871 return -1;
5872 }
5873
5874 /* create the new es */
2bb9eff4 5875 es = bgp_evpn_lookup_es(bgp, esi);
50f74cf1 5876 if (!es) {
5877 es = bgp_evpn_es_new(bgp, esi, originator_ip);
5878 if (!es) {
af4c2728 5879 flog_err(
e50f7cfd 5880 EC_BGP_ES_CREATE,
50f74cf1 5881 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5882 bgp->vrf_id, esi_to_str(esi, buf, sizeof(buf)));
5883 return -1;
5884 }
5885 }
5886 UNSET_FLAG(es->flags, EVPNES_REMOTE);
5887 SET_FLAG(es->flags, EVPNES_LOCAL);
5888
5889 build_evpn_type4_prefix(&p, esi, originator_ip->ipaddr_v4);
5890 if (update_evpn_type4_route(bgp, es, &p)) {
e50f7cfd 5891 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
1c50c1c0
QY
5892 "%u: Type4 route creation failure for ESI %s",
5893 bgp->vrf_id, esi_to_str(esi, buf, sizeof(buf)));
50f74cf1 5894 return -1;
5895 }
5896
5897 /* import all remote ES routes in th ES table */
5898 install_routes_for_es(bgp, es);
5899
5900 return 0;
5901}
5902
fd069644
DS
5903/*
5904 * Handle change in setting for BUM handling. The supported values
5905 * are head-end replication and dropping all BUM packets. Any change
5906 * should be registered with zebra. Also, if doing head-end replication,
5907 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5908 * to be dropped, the RT-3s must be withdrawn.
5909 */
5910void bgp_evpn_flood_control_change(struct bgp *bgp)
5911{
5912 zlog_info("L2VPN EVPN BUM handling is %s",
5913 bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL ?
5914 "Flooding" : "Flooding Disabled");
5915
5916 bgp_zebra_vxlan_flood_control(bgp, bgp->vxlan_flood_ctrl);
5917 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL)
5918 hash_iterate(bgp->vnihash, create_advertise_type3, bgp);
5919 else if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
5920 hash_iterate(bgp->vnihash, delete_withdraw_type3, bgp);
5921}
5922
7724c0a1 5923/*
5924 * Cleanup EVPN information on disable - Need to delete and withdraw
5925 * EVPN routes from peers.
5926 */
d62a17ae 5927void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
7724c0a1 5928{
e3b78da8 5929 hash_iterate(bgp->vnihash, (void (*)(struct hash_bucket *,
9d303b37
DL
5930 void *))cleanup_vni_on_disable,
5931 bgp);
7724c0a1 5932}
5933
14c1a7bf 5934/*
5935 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5936 * BGP instance (default) is being freed.
5937 */
d62a17ae 5938void bgp_evpn_cleanup(struct bgp *bgp)
14c1a7bf 5939{
1525e99f 5940 hash_iterate(bgp->vnihash,
e3b78da8 5941 (void (*)(struct hash_bucket *, void *))free_vni_entry,
1525e99f
DS
5942 bgp);
5943
5944 hash_free(bgp->import_rt_hash);
d62a17ae 5945 bgp->import_rt_hash = NULL;
1525e99f
DS
5946
5947 hash_free(bgp->vrf_import_rt_hash);
10ebe1ab 5948 bgp->vrf_import_rt_hash = NULL;
1525e99f
DS
5949
5950 hash_free(bgp->vnihash);
d62a17ae 5951 bgp->vnihash = NULL;
50f74cf1 5952 if (bgp->esihash)
5953 hash_free(bgp->esihash);
5954 bgp->esihash = NULL;
1525e99f 5955
6a154c88
DL
5956 list_delete(&bgp->vrf_import_rtl);
5957 list_delete(&bgp->vrf_export_rtl);
5958 list_delete(&bgp->l2vnis);
14c1a7bf 5959}
5960
5961/*
5962 * Initialization for EVPN
5963 * Create
5964 * VNI hash table
5965 * hash for RT to VNI
14c1a7bf 5966 */
d62a17ae 5967void bgp_evpn_init(struct bgp *bgp)
5968{
5969 bgp->vnihash =
5970 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
50f74cf1 5971 bgp->esihash =
5972 hash_create(esi_hash_keymake, esi_cmp,
5973 "BGP EVPN Local ESI Hash");
d62a17ae 5974 bgp->import_rt_hash =
5975 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
5976 "BGP Import RT Hash");
10ebe1ab
MK
5977 bgp->vrf_import_rt_hash =
5978 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
5979 "BGP VRF Import RT Hash");
c581d8b0
MK
5980 bgp->vrf_import_rtl = list_new();
5981 bgp->vrf_import_rtl->cmp =
5982 (int (*)(void *, void *))evpn_route_target_cmp;
987d8198 5983 bgp->vrf_import_rtl->del = evpn_xxport_delete_ecomm;
c581d8b0
MK
5984 bgp->vrf_export_rtl = list_new();
5985 bgp->vrf_export_rtl->cmp =
5986 (int (*)(void *, void *))evpn_route_target_cmp;
987d8198 5987 bgp->vrf_export_rtl->del = evpn_xxport_delete_ecomm;
6a8657d0 5988 bgp->l2vnis = list_new();
64465785 5989 bgp->l2vnis->cmp = vni_list_cmp;
85c8d83b
CS
5990 /* By default Duplicate Address Dection is enabled.
5991 * Max-moves (N) 5, detection time (M) 180
5992 * default action is warning-only
5993 * freeze action permanently freezes address,
5994 * and freeze time (auto-recovery) is disabled.
5995 */
5996 if (bgp->evpn_info) {
5997 bgp->evpn_info->dup_addr_detect = true;
5998 bgp->evpn_info->dad_time = EVPN_DAD_DEFAULT_TIME;
5999 bgp->evpn_info->dad_max_moves = EVPN_DAD_DEFAULT_MAX_MOVES;
6000 bgp->evpn_info->dad_freeze = false;
6001 bgp->evpn_info->dad_freeze_time = 0;
0b9d9cd0
CS
6002 /* Initialize zebra vxlan */
6003 bgp_zebra_dup_addr_detection(bgp);
85c8d83b 6004 }
fd069644
DS
6005
6006 /* Default BUM handling is to do head-end replication. */
6007 bgp->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
14c1a7bf 6008}
10ebe1ab
MK
6009
6010void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
6011{
6012 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
6013}