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