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