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