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