1 /* Ethernet-VPN Packet and vty Processing File
2 * Copyright (C) 2016 6WIND
3 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
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
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.
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
35 #include "bgpd/bgp_attr_evpn.h"
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_table.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_mplsvpn.h"
41 #include "bgpd/bgp_label.h"
42 #include "bgpd/bgp_evpn.h"
43 #include "bgpd/bgp_evpn_private.h"
44 #include "bgpd/bgp_ecommunity.h"
45 #include "bgpd/bgp_encap_types.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nexthop.h"
52 * Definitions and external declarations.
54 extern struct zclient
*zclient
;
56 DEFINE_QOBJ_TYPE(bgpevpn
)
60 * Static function declarations
62 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
63 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
64 struct bgp_info
**ri
);
65 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
74 static unsigned int vni_hash_key_make(void *p
)
76 struct bgpevpn
*vpn
= p
;
77 return (jhash_1word(vpn
->vni
, 0));
81 * Comparison function for vni hash
83 static int vni_hash_cmp(const void *p1
, const void *p2
)
85 const struct bgpevpn
*vpn1
= p1
;
86 const struct bgpevpn
*vpn2
= p2
;
92 return (vpn1
->vni
== vpn2
->vni
);
96 * Make vrf import route target hash key.
98 static unsigned int vrf_import_rt_hash_key_make(void *p
)
100 struct vrf_irt_node
*irt
= p
;
101 char *pnt
= irt
->rt
.val
;
103 return jhash(pnt
, 8, 0x5abc1234);
107 * Comparison function for vrf import rt hash
109 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
111 const struct vrf_irt_node
*irt1
= p1
;
112 const struct vrf_irt_node
*irt2
= p2
;
114 if (irt1
== NULL
&& irt2
== NULL
)
117 if (irt1
== NULL
|| irt2
== NULL
)
120 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
124 * Create a new vrf import_rt in default instance
126 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
128 struct bgp
*bgp_def
= NULL
;
129 struct vrf_irt_node
*irt
;
131 bgp_def
= bgp_get_default();
133 zlog_err("vrf import rt new - def instance not created yet");
137 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
138 sizeof(struct vrf_irt_node
));
143 irt
->vrfs
= list_new();
146 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
147 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
155 * Free the vrf import rt node
157 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
159 struct bgp
*bgp_def
= NULL
;
161 bgp_def
= bgp_get_default();
163 zlog_err("vrf import rt free - def instance not created yet");
167 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
168 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
172 * Function to lookup Import RT node - used to map a RT to set of
173 * VNIs importing routes with that RT.
175 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
177 struct bgp
*bgp_def
= NULL
;
178 struct vrf_irt_node
*irt
;
179 struct vrf_irt_node tmp
;
181 bgp_def
= bgp_get_default();
183 zlog_err("vrf import rt lookup - def instance not created yet");
187 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
188 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
189 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
194 * Is specified VRF present on the RT's list of "importing" VRFs?
196 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
,
199 struct listnode
*node
= NULL
, *nnode
= NULL
;
200 struct bgp
*tmp_bgp_vrf
= NULL
;
202 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
203 if (tmp_bgp_vrf
== bgp_vrf
)
210 * Make import route target hash key.
212 static unsigned int import_rt_hash_key_make(void *p
)
214 struct irt_node
*irt
= p
;
215 char *pnt
= irt
->rt
.val
;
217 return jhash(pnt
, 8, 0xdeadbeef);
221 * Comparison function for import rt hash
223 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
225 const struct irt_node
*irt1
= p1
;
226 const struct irt_node
*irt2
= p2
;
228 if (irt1
== NULL
&& irt2
== NULL
)
231 if (irt1
== NULL
|| irt2
== NULL
)
234 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
238 * Create a new import_rt
240 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
241 struct ecommunity_val
*rt
)
243 struct irt_node
*irt
;
248 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
253 irt
->vnis
= list_new();
256 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
257 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
265 * Free the import rt node
267 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
269 hash_release(bgp
->import_rt_hash
, irt
);
270 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
274 * Function to lookup Import RT node - used to map a RT to set of
275 * VNIs importing routes with that RT.
277 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
278 struct ecommunity_val
*rt
)
280 struct irt_node
*irt
;
283 memset(&tmp
, 0, sizeof(struct irt_node
));
284 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
285 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
290 * Is specified VNI present on the RT's list of "importing" VNIs?
292 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
294 struct listnode
*node
, *nnode
;
295 struct bgpevpn
*tmp_vpn
;
297 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
306 * Compare Route Targets.
308 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
309 struct ecommunity
*ecom2
)
317 if (!ecom1
&& !ecom2
)
320 if (ecom1
->str
&& !ecom2
->str
)
323 if (!ecom1
->str
&& ecom2
->str
)
326 if (!ecom1
->str
&& !ecom2
->str
)
329 return strcmp(ecom1
->str
, ecom2
->str
);
333 * Mask off global-admin field of specified extended community (RT),
334 * just retain the local-admin field.
336 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
337 struct ecommunity_val
*src
)
343 if (type
== ECOMMUNITY_ENCODE_AS
) {
344 dst
->val
[2] = dst
->val
[3] = 0;
345 } else if (type
== ECOMMUNITY_ENCODE_AS4
346 || type
== ECOMMUNITY_ENCODE_IP
) {
347 dst
->val
[2] = dst
->val
[3] = 0;
348 dst
->val
[4] = dst
->val
[5] = 0;
353 * Map one RT to specified VRF.
354 * bgp_vrf = BGP vrf instance
356 static void map_vrf_to_rt(struct bgp
*bgp_vrf
,
357 struct ecommunity_val
*eval
)
359 struct vrf_irt_node
*irt
= NULL
;
360 struct ecommunity_val eval_tmp
;
362 /* If using "automatic" RT,
363 * we only care about the local-admin sub-field.
364 * This is to facilitate using L3VNI(VRF-VNI)
365 * as the RT for EBGP peering too.
367 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
368 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
369 BGP_VRF_IMPORT_RT_CFGD
))
370 mask_ecom_global_admin(&eval_tmp
, eval
);
372 irt
= lookup_vrf_import_rt(&eval_tmp
);
373 if (irt
&& irt
->vrfs
)
374 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
375 /* Already mapped. */
379 irt
= vrf_import_rt_new(&eval_tmp
);
383 /* Add VRF to the list for this RT. */
384 listnode_add(irt
->vrfs
, bgp_vrf
);
388 * Unmap specified VRF from specified RT. If there are no other
389 * VRFs for this RT, then the RT hash is deleted.
390 * bgp_vrf: BGP VRF specific instance
392 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
,
393 struct vrf_irt_node
*irt
)
395 /* Delete VRF from list for this RT. */
396 listnode_delete(irt
->vrfs
, bgp_vrf
);
397 if (!listnode_head(irt
->vrfs
)) {
398 list_delete_and_null(&irt
->vrfs
);
399 vrf_import_rt_free(irt
);
404 * Map one RT to specified VNI.
406 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
407 struct ecommunity_val
*eval
)
409 struct irt_node
*irt
;
410 struct ecommunity_val eval_tmp
;
412 /* If using "automatic" RT, we only care about the local-admin
414 * This is to facilitate using VNI as the RT for EBGP peering too.
416 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
417 if (!is_import_rt_configured(vpn
))
418 mask_ecom_global_admin(&eval_tmp
, eval
);
420 irt
= lookup_import_rt(bgp
, &eval_tmp
);
421 if (irt
&& irt
->vnis
)
422 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
423 /* Already mapped. */
427 irt
= import_rt_new(bgp
, &eval_tmp
);
431 /* Add VNI to the hash list for this RT. */
432 listnode_add(irt
->vnis
, vpn
);
436 * Unmap specified VNI from specified RT. If there are no other
437 * VNIs for this RT, then the RT hash is deleted.
439 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
440 struct irt_node
*irt
)
442 /* Delete VNI from hash list for this RT. */
443 listnode_delete(irt
->vnis
, vpn
);
444 if (!listnode_head(irt
->vnis
)) {
445 list_delete_and_null(&irt
->vnis
);
446 import_rt_free(bgp
, irt
);
451 * Create RT extended community automatically from passed information:
452 * of the form AS:VNI.
453 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
454 * the need is to get a RT value that will be unique across different
455 * VNIs but the same across routers (in the same AS) for a particular
458 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
460 struct ecommunity_val eval
;
461 struct ecommunity
*ecomadd
;
463 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
465 ecomadd
= ecommunity_new();
466 ecommunity_add_val(ecomadd
, &eval
);
467 listnode_add_sort(rtl
, ecomadd
);
471 * Derive RD and RT for a VNI automatically. Invoked at the time of
474 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
476 bgp_evpn_derive_auto_rd(bgp
, vpn
);
477 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
478 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
482 * Add (update) or delete MACIP from zebra.
484 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
485 struct prefix_evpn
*p
,
486 struct in_addr remote_vtep_ip
, int add
,
491 char buf1
[ETHER_ADDR_STRLEN
];
492 char buf2
[INET6_ADDRSTRLEN
];
493 char buf3
[INET6_ADDRSTRLEN
];
496 if (!zclient
|| zclient
->sock
< 0)
499 /* Don't try to register if Zebra doesn't know of this instance. */
500 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
506 zclient_create_header(s
, add
? ZEBRA_REMOTE_MACIP_ADD
507 : ZEBRA_REMOTE_MACIP_DEL
,
509 stream_putl(s
, vpn
->vni
);
510 stream_put(s
, &p
->prefix
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
511 /* IP address length and IP address, if any. */
512 if (IS_EVPN_PREFIX_IPADDR_NONE(p
))
515 ipa_len
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? IPV4_MAX_BYTELEN
517 stream_putl(s
, ipa_len
);
518 stream_put(s
, &p
->prefix
.ip
.ip
.addr
, ipa_len
);
520 stream_put_in_addr(s
, &remote_vtep_ip
);
522 /* TX flags - MAC sticky status and/or gateway mac */
524 stream_putc(s
, flags
);
526 stream_putw_at(s
, 0, stream_get_endp(s
));
528 if (bgp_debug_zebra(NULL
))
529 zlog_debug("Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
530 add
? "ADD" : "DEL", vpn
->vni
,
531 prefix_mac2str(&p
->prefix
.mac
, buf1
, sizeof(buf1
)),
532 ipaddr2str(&p
->prefix
.ip
, buf3
, sizeof(buf3
)),
534 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
537 return zclient_send_message(zclient
);
541 * Add (update) or delete remote VTEP from zebra.
543 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
544 struct prefix_evpn
*p
, int add
)
549 if (!zclient
|| zclient
->sock
< 0)
552 /* Don't try to register if Zebra doesn't know of this instance. */
553 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
559 zclient_create_header(s
, add
? ZEBRA_REMOTE_VTEP_ADD
560 : ZEBRA_REMOTE_VTEP_DEL
,
562 stream_putl(s
, vpn
->vni
);
563 if (IS_EVPN_PREFIX_IPADDR_V4(p
))
564 stream_put_in_addr(s
, &p
->prefix
.ip
.ipaddr_v4
);
565 else if (IS_EVPN_PREFIX_IPADDR_V6(p
)) {
567 "Bad remote IP when trying to %s remote VTEP for VNI %u",
568 add
? "ADD" : "DEL", vpn
->vni
);
572 stream_putw_at(s
, 0, stream_get_endp(s
));
574 if (bgp_debug_zebra(NULL
))
575 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
576 add
? "ADD" : "DEL", vpn
->vni
,
577 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
579 return zclient_send_message(zclient
);
583 * Build extended communities for EVPN prefix route.
585 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
588 struct ecommunity ecom_encap
;
589 struct ecommunity ecom_rmac
;
590 struct ecommunity_val eval
;
591 struct ecommunity_val eval_rmac
;
592 bgp_encap_types tnl_type
;
593 struct listnode
*node
, *nnode
;
594 struct ecommunity
*ecom
;
595 struct list
*vrf_export_rtl
= NULL
;
598 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
599 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
600 encode_encap_extcomm(tnl_type
, &eval
);
602 ecom_encap
.val
= (u_int8_t
*)eval
.val
;
605 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
607 /* Add the export RTs for L3VNI/VRF */
608 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
609 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
610 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
611 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
615 /* add the router mac extended community */
616 if (!is_zero_mac(&attr
->rmac
)) {
617 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
618 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
620 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
621 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
625 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
629 * Build extended communities for EVPN route. RT and ENCAP are
630 * applicable to all routes.
631 * TODO: currently kernel doesnt support ipv6 routes with ipv4 nexthops.
632 * This means that we can't do symmetric routing for ipv6 hosts routes
633 * in the same way as ipv4 host routes.
634 * We wont attach l3-vni related RTs for ipv6 routes.
635 * For now, We will only adevrtise ipv4 host routes
636 * with L3-VNI related ext-comm.
638 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
641 struct ecommunity ecom_encap
;
642 struct ecommunity ecom_sticky
;
643 struct ecommunity ecom_default_gw
;
644 struct ecommunity ecom_rmac
;
645 struct ecommunity_val eval
;
646 struct ecommunity_val eval_sticky
;
647 struct ecommunity_val eval_default_gw
;
648 struct ecommunity_val eval_rmac
;
649 bgp_encap_types tnl_type
;
650 struct listnode
*node
, *nnode
;
651 struct ecommunity
*ecom
;
653 struct list
*vrf_export_rtl
= NULL
;
656 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
657 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
658 encode_encap_extcomm(tnl_type
, &eval
);
660 ecom_encap
.val
= (u_int8_t
*)eval
.val
;
663 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
665 /* Add the export RTs for L2VNI */
666 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
667 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
670 * only attach l3-vni export rts for ipv4 address family and if we are
671 * advertising both the labels in type-2 routes
673 if (afi
== AFI_IP
&& CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
674 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
675 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
676 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
679 ecommunity_merge(attr
->ecommunity
,
686 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
687 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
688 ecom_sticky
.size
= 1;
689 ecom_sticky
.val
= (u_int8_t
*)eval_sticky
.val
;
691 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
695 * only attach l3-vni rmac for ipv4 address family and if we are
696 * advertising both the labels in type-2 routes
698 if (afi
== AFI_IP
&& !is_zero_mac(&attr
->rmac
) &&
699 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
700 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
701 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
703 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
704 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
708 if (attr
->default_gw
) {
709 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
710 encode_default_gw_extcomm(&eval_default_gw
);
711 ecom_default_gw
.size
= 1;
712 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
713 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
717 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
721 * Add MAC mobility extended community to attribute.
723 static void add_mac_mobility_to_attr(u_int32_t seq_num
, struct attr
*attr
)
725 struct ecommunity ecom_tmp
;
726 struct ecommunity_val eval
;
727 u_int8_t
*ecom_val_ptr
;
734 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
736 /* Find current MM ecommunity */
739 if (attr
->ecommunity
) {
740 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
741 pnt
= attr
->ecommunity
->val
+ (i
* 8);
745 if (type
== ECOMMUNITY_ENCODE_EVPN
747 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
749 (u_int8_t
*)(attr
->ecommunity
->val
756 /* Update the existing MM ecommunity */
758 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
760 /* Add MM to existing */
762 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
764 ecom_tmp
.val
= (u_int8_t
*)eval
.val
;
767 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
771 /* Install EVPN route into zebra. */
772 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
773 struct prefix_evpn
*p
,
774 struct in_addr remote_vtep_ip
, u_char flags
)
778 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
779 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
782 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
787 /* Uninstall EVPN route from zebra. */
788 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
789 struct prefix_evpn
*p
,
790 struct in_addr remote_vtep_ip
)
794 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
795 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
798 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
804 * Due to MAC mobility, the prior "local" best route has been supplanted
805 * by a "remote" best route. The prior route has to be deleted and withdrawn
808 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
810 struct bgp_info
*old_local
)
812 struct bgp_node
*global_rn
;
814 afi_t afi
= AFI_L2VPN
;
815 safi_t safi
= SAFI_EVPN
;
817 /* Locate route node in the global EVPN routing table. Note that
818 * this table is a 2-level tree (RD-level + Prefix-level) similar to
821 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
822 (struct prefix
*)&rn
->p
, &vpn
->prd
);
824 /* Delete route entry in the global EVPN table. */
825 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
827 /* Schedule for processing - withdraws to peers happen from
831 bgp_process(bgp
, global_rn
, afi
, safi
);
832 bgp_unlock_node(global_rn
);
835 /* Delete route entry in the VNI route table, caller to remove. */
836 bgp_info_delete(rn
, old_local
);
840 * Calculate the best path for an EVPN route. Install/update best path in zebra,
843 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
846 struct bgp_info
*old_select
, *new_select
;
847 struct bgp_info_pair old_and_new
;
848 afi_t afi
= AFI_L2VPN
;
849 safi_t safi
= SAFI_EVPN
;
853 /* Compute the best path. */
854 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
856 old_select
= old_and_new
.old
;
857 new_select
= old_and_new
.new;
859 /* If the best path hasn't changed - see if there is still something to
863 if (old_select
&& old_select
== new_select
864 && old_select
->type
== ZEBRA_ROUTE_BGP
865 && old_select
->sub_type
== BGP_ROUTE_NORMAL
866 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
867 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
868 && !bgp
->addpath_tx_used
[afi
][safi
]) {
869 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
870 if (old_select
->attr
->sticky
)
871 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
872 if (old_select
->attr
->default_gw
)
873 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
874 ret
= evpn_zebra_install(bgp
, vpn
,
875 (struct prefix_evpn
*)&rn
->p
,
876 old_select
->attr
->nexthop
,
879 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
880 bgp_zebra_clear_route_change_flags(rn
);
884 /* If the user did a "clear" this flag will be set */
885 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
887 /* bestpath has changed; update relevant fields and install or uninstall
888 * into the zebra RIB.
890 if (old_select
|| new_select
)
891 bgp_bump_version(rn
);
894 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
896 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
897 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
898 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
901 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
902 && new_select
->sub_type
== BGP_ROUTE_NORMAL
) {
904 if (new_select
->attr
->sticky
)
905 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
906 if (new_select
->attr
->default_gw
)
907 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
908 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
909 new_select
->attr
->nexthop
,
911 /* If an old best existed and it was a "local" route, the only
913 * it would be supplanted is due to MAC mobility procedures. So,
915 * need to do an implicit delete and withdraw that route from
918 if (old_select
&& old_select
->peer
== bgp
->peer_self
919 && old_select
->type
== ZEBRA_ROUTE_BGP
920 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
921 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
923 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
924 && old_select
->sub_type
== BGP_ROUTE_NORMAL
)
925 ret
= evpn_zebra_uninstall(bgp
, vpn
,
926 (struct prefix_evpn
*)&rn
->p
,
927 old_select
->attr
->nexthop
);
930 /* Clear any route change flags. */
931 bgp_zebra_clear_route_change_flags(rn
);
933 /* Reap old select bgp_info, if it has been removed */
934 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
935 bgp_info_reap(rn
, old_select
);
941 * Return true if the local ri for this rn is of type gateway mac
943 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
945 struct bgp_info
*tmp_ri
= NULL
;
946 struct bgp_info
*local_ri
= NULL
;
949 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
950 if (tmp_ri
->peer
== bgp
->peer_self
951 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
952 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
959 return local_ri
->attr
->default_gw
;
964 * Return true if the local ri for this rn has sticky set
966 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
968 struct bgp_info
*tmp_ri
;
969 struct bgp_info
*local_ri
;
972 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
973 if (tmp_ri
->peer
== bgp
->peer_self
974 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
975 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
982 return local_ri
->attr
->sticky
;
985 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
986 struct bgp
*bgp_vrf
, afi_t afi
,
987 safi_t safi
, struct bgp_node
*rn
,
988 struct attr
*attr
, int *route_changed
)
990 struct attr
*attr_new
= NULL
;
991 struct bgp_info
*ri
= NULL
;
992 mpls_label_t label
= MPLS_INVALID_LABEL
;
993 struct bgp_info
*local_ri
= NULL
;
994 struct bgp_info
*tmp_ri
= NULL
;
997 /* locate the local route entry if any */
998 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
999 if (tmp_ri
->peer
== bgp_def
->peer_self
1000 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1001 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1005 /* create a new route entry if one doesnt exist.
1006 Otherwise see if route attr has changed
1010 /* route has changed as this is the first entry */
1013 /* Add (or update) attribute to hash. */
1014 attr_new
= bgp_attr_intern(attr
);
1016 /* create the route info from attribute */
1017 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1018 bgp_def
->peer_self
, attr_new
, rn
);
1019 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1021 /* Type-5 routes advertise the L3-VNI */
1022 bgp_info_extra_get(ri
);
1023 vni2label(bgp_vrf
->l3vni
, &label
);
1024 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1025 ri
->extra
->num_labels
= 1;
1027 /* add the route entry to route node*/
1028 bgp_info_add(rn
, ri
);
1032 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1034 /* attribute changed */
1037 /* The attribute has changed. */
1038 /* Add (or update) attribute to hash. */
1039 attr_new
= bgp_attr_intern(attr
);
1040 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1042 /* Restore route, if needed. */
1043 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1044 bgp_info_restore(rn
, tmp_ri
);
1046 /* Unintern existing, set to new. */
1047 bgp_attr_unintern(&tmp_ri
->attr
);
1048 tmp_ri
->attr
= attr_new
;
1049 tmp_ri
->uptime
= bgp_clock();
1055 /* update evpn type-5 route entry */
1056 static int update_evpn_type5_route(struct bgp
*bgp_vrf
,
1057 struct prefix_evpn
*evp
,
1058 struct attr
* src_attr
)
1060 afi_t afi
= AFI_L2VPN
;
1061 safi_t safi
= SAFI_EVPN
;
1063 struct bgp_node
*rn
= NULL
;
1064 struct bgp
*bgp_def
= NULL
;
1065 int route_changed
= 0;
1067 bgp_def
= bgp_get_default();
1071 /* Build path attribute for this route - use the source attr, if
1072 * present, else treat as locally originated.
1075 bgp_attr_dup(&attr
, src_attr
);
1077 memset(&attr
, 0, sizeof(struct attr
));
1078 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1080 /* Set nexthop to ourselves and fill in the Router MAC. */
1081 attr
.nexthop
= bgp_vrf
->originator_ip
;
1082 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1083 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1084 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1086 /* Setup RT and encap extended community */
1087 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1089 /* get the route node in global table */
1090 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1091 (struct prefix
*)evp
,
1095 /* create or update the route entry within the route node */
1096 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
,
1098 rn
, &attr
, &route_changed
);
1100 /* schedule for processing and unlock node */
1101 if (route_changed
) {
1102 bgp_process(bgp_def
, rn
, afi
, safi
);
1103 bgp_unlock_node(rn
);
1106 /* uninten temporary */
1108 aspath_unintern(&attr
.aspath
);
1113 * Create or update EVPN route entry. This could be in the VNI route table
1114 * or the global route table.
1116 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1117 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1118 struct attr
*attr
, int add
, int vni_table
,
1119 struct bgp_info
**ri
, u_char flags
)
1121 struct bgp_info
*tmp_ri
;
1122 struct bgp_info
*local_ri
, *remote_ri
;
1123 struct attr
*attr_new
;
1124 mpls_label_t label
[BGP_MAX_LABELS
];
1125 u_int32_t num_labels
= 1;
1126 int route_change
= 1;
1128 struct prefix_evpn
*evp
;
1131 evp
= (struct prefix_evpn
*)&rn
->p
;
1132 memset(&label
, 0, sizeof(label
));
1134 /* See if this is an update of an existing route, or a new add. Also,
1135 * identify if already known from remote, and if so, the one with the
1136 * highest sequence number; this is only when adding to the VNI routing
1139 local_ri
= remote_ri
= NULL
;
1140 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1141 if (tmp_ri
->peer
== bgp
->peer_self
1142 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1143 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1146 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1147 && tmp_ri
->sub_type
== BGP_ROUTE_NORMAL
1148 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
1151 else if (mac_mobility_seqnum(tmp_ri
->attr
)
1152 > mac_mobility_seqnum(remote_ri
->attr
))
1158 /* If route doesn't exist already, create a new one, if told to.
1159 * Otherwise act based on whether the attributes of the route have
1162 if (!local_ri
&& !add
)
1166 /* When learnt locally for the first time but already known from
1167 * remote, we have to initiate appropriate MAC mobility steps.
1169 * is applicable when updating the VNI routing table.
1170 * We need to skip mobility steps for g/w macs (local mac on g/w
1171 * SVI) advertised in EVPN.
1172 * This will ensure that local routes are preferred for g/w macs
1174 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
)) {
1175 u_int32_t cur_seqnum
;
1177 /* Add MM extended community to route. */
1178 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
1179 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
1182 /* Add (or update) attribute to hash. */
1183 attr_new
= bgp_attr_intern(attr
);
1185 /* Extract MAC mobility sequence number, if any. */
1186 attr_new
->mm_seqnum
=
1187 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1188 attr_new
->sticky
= sticky
;
1190 /* Create new route with its attribute. */
1191 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1192 bgp
->peer_self
, attr_new
, rn
);
1193 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1194 bgp_info_extra_get(tmp_ri
);
1196 /* The VNI goes into the 'label' field of the route */
1197 vni2label(vpn
->vni
, &label
[0]);
1199 /* Type-2 routes may carry a second VNI - the L3-VNI.
1200 * Only attach second label if we are advertising two labels for
1203 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1204 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1207 l3vni
= bgpevpn_get_l3vni(vpn
);
1209 vni2label(l3vni
, &label
[1]);
1214 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1215 tmp_ri
->extra
->num_labels
= num_labels
;
1216 bgp_info_add(rn
, tmp_ri
);
1219 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1220 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1224 * The attributes have changed, type-2 routes needs to
1225 * be advertised with right labels.
1227 vni2label(vpn
->vni
, &label
[0]);
1228 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1229 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1232 l3vni
= bgpevpn_get_l3vni(vpn
);
1234 vni2label(l3vni
, &label
[1]);
1238 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1239 tmp_ri
->extra
->num_labels
= num_labels
;
1241 /* The attribute has changed. */
1242 /* Add (or update) attribute to hash. */
1243 attr_new
= bgp_attr_intern(attr
);
1244 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1246 /* Restore route, if needed. */
1247 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1248 bgp_info_restore(rn
, tmp_ri
);
1250 /* Unintern existing, set to new. */
1251 bgp_attr_unintern(&tmp_ri
->attr
);
1252 tmp_ri
->attr
= attr_new
;
1253 tmp_ri
->uptime
= bgp_clock();
1257 /* Return back the route entry. */
1259 return route_change
;
1263 * Create or update EVPN route (of type based on prefix) for specified VNI
1264 * and schedule for processing.
1266 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1267 struct prefix_evpn
*p
, u_char flags
)
1269 struct bgp_node
*rn
;
1271 struct attr
*attr_new
;
1272 struct bgp_info
*ri
;
1273 afi_t afi
= AFI_L2VPN
;
1274 safi_t safi
= SAFI_EVPN
;
1277 memset(&attr
, 0, sizeof(struct attr
));
1279 /* Build path-attribute for this route. */
1280 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1281 attr
.nexthop
= vpn
->originator_ip
;
1282 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1283 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1284 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1285 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1287 /* PMSI is only needed for type-3 routes */
1288 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1289 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1291 /* router mac is only needed for type-2 and type-5 routes */
1292 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1293 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1294 vni2label(vpn
->vni
, &(attr
.label
));
1296 /* Set up RT and ENCAP extended community. */
1297 build_evpn_route_extcomm(vpn
, &attr
,
1298 IS_EVPN_PREFIX_IPADDR_V4(p
) ?
1301 /* First, create (or fetch) route node within the VNI. */
1302 /* NOTE: There is no RD here. */
1303 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1305 /* Create or update route entry. */
1306 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1309 attr_new
= ri
->attr
;
1311 /* Perform route selection; this is just to set the flags correctly
1312 * as local route in the VNI always wins.
1314 evpn_route_select_install(bgp
, vpn
, rn
);
1315 bgp_unlock_node(rn
);
1317 /* If this is a new route or some attribute has changed, export the
1318 * route to the global table. The route will be advertised to peers
1319 * from there. Note that this table is a 2-level tree (RD-level +
1320 * Prefix-level) similar to L3VPN routes.
1323 struct bgp_info
*global_ri
;
1325 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1326 (struct prefix
*)p
, &vpn
->prd
);
1327 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1330 /* Schedule for processing and unlock node. */
1331 bgp_process(bgp
, rn
, afi
, safi
);
1332 bgp_unlock_node(rn
);
1335 /* Unintern temporary. */
1336 aspath_unintern(&attr
.aspath
);
1341 /* Delete EVPN type5 route entry from global table */
1342 static void delete_evpn_type5_route_entry(struct bgp
*bgp_def
,
1343 struct bgp
*bgp_vrf
,
1344 afi_t afi
, safi_t safi
,
1345 struct bgp_node
*rn
,
1346 struct bgp_info
**ri
)
1348 struct bgp_info
*tmp_ri
= NULL
;
1352 /* find the matching route entry */
1353 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1354 if (tmp_ri
->peer
== bgp_def
->peer_self
1355 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1356 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1361 /* Mark route for delete. */
1363 bgp_info_delete(rn
, tmp_ri
);
1366 /* Delete EVPN type5 route */
1367 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
,
1368 struct prefix_evpn
*evp
)
1370 afi_t afi
= AFI_L2VPN
;
1371 safi_t safi
= SAFI_EVPN
;
1372 struct bgp_node
*rn
= NULL
;
1373 struct bgp_info
*ri
= NULL
;
1374 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1376 bgp_def
= bgp_get_default();
1380 /* locate the global route entry for this type-5 prefix */
1381 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1382 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1386 delete_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &ri
);
1388 bgp_process(bgp_def
, rn
, afi
, safi
);
1389 bgp_unlock_node(rn
);
1394 * Delete EVPN route entry. This could be in the VNI route table
1395 * or the global route table.
1397 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1398 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1399 struct bgp_info
**ri
)
1401 struct bgp_info
*tmp_ri
;
1405 /* Now, find matching route. */
1406 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1407 if (tmp_ri
->peer
== bgp
->peer_self
1408 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1409 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1414 /* Mark route for delete. */
1416 bgp_info_delete(rn
, tmp_ri
);
1420 * Delete EVPN route (of type based on prefix) for specified VNI and
1421 * schedule for processing.
1423 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1424 struct prefix_evpn
*p
)
1426 struct bgp_node
*rn
, *global_rn
;
1427 struct bgp_info
*ri
;
1428 afi_t afi
= AFI_L2VPN
;
1429 safi_t safi
= SAFI_EVPN
;
1431 /* First, locate the route node within the VNI. If it doesn't exist,
1433 * is nothing further to do.
1435 /* NOTE: There is no RD here. */
1436 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1440 /* Next, locate route node in the global EVPN routing table. Note that
1441 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1444 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1445 (struct prefix
*)p
, &vpn
->prd
);
1447 /* Delete route entry in the global EVPN table. */
1448 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
1450 /* Schedule for processing - withdraws to peers happen from
1454 bgp_process(bgp
, global_rn
, afi
, safi
);
1455 bgp_unlock_node(global_rn
);
1458 /* Delete route entry in the VNI route table. This can just be removed.
1460 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1462 bgp_info_reap(rn
, ri
);
1463 bgp_unlock_node(rn
);
1469 * Update all type-2 (MACIP) local routes for this VNI - these should also
1470 * be scheduled for advertise to peers.
1472 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1476 struct bgp_node
*rn
;
1477 struct bgp_info
*ri
;
1479 struct attr attr_sticky
;
1480 struct attr attr_def_gw
;
1481 struct attr attr_ip6
;
1482 struct attr attr_sticky_ip6
;
1483 struct attr attr_def_gw_ip6
;
1484 struct attr
*attr_new
;
1488 memset(&attr
, 0, sizeof(struct attr
));
1489 memset(&attr_sticky
, 0, sizeof(struct attr
));
1490 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1491 memset(&attr_ip6
, 0, sizeof(struct attr
));
1492 memset(&attr_sticky_ip6
, 0, sizeof(struct attr
));
1493 memset(&attr_def_gw_ip6
, 0, sizeof(struct attr
));
1495 /* Build path-attribute - all type-2 routes for this VNI will share the
1496 * same path attribute.
1498 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1499 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1500 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1501 attr
.nexthop
= vpn
->originator_ip
;
1502 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1503 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1504 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1505 attr_sticky
.nexthop
= vpn
->originator_ip
;
1506 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1507 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1508 attr_sticky
.sticky
= 1;
1509 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1510 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1511 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1512 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1513 attr_def_gw
.default_gw
= 1;
1514 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1515 bgp_attr_default_set(&attr_ip6
, BGP_ORIGIN_IGP
);
1516 bgp_attr_default_set(&attr_sticky_ip6
, BGP_ORIGIN_IGP
);
1517 bgp_attr_default_set(&attr_def_gw_ip6
, BGP_ORIGIN_IGP
);
1518 attr_ip6
.nexthop
= vpn
->originator_ip
;
1519 attr_ip6
.mp_nexthop_global_in
= vpn
->originator_ip
;
1520 attr_ip6
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1521 bgpevpn_get_rmac(vpn
, &attr_ip6
.rmac
);
1522 attr_sticky_ip6
.nexthop
= vpn
->originator_ip
;
1523 attr_sticky_ip6
.mp_nexthop_global_in
= vpn
->originator_ip
;
1524 attr_sticky_ip6
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1525 attr_sticky_ip6
.sticky
= 1;
1526 bgpevpn_get_rmac(vpn
, &attr_sticky_ip6
.rmac
);
1527 attr_def_gw_ip6
.nexthop
= vpn
->originator_ip
;
1528 attr_def_gw_ip6
.mp_nexthop_global_in
= vpn
->originator_ip
;
1529 attr_def_gw_ip6
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1530 attr_def_gw_ip6
.default_gw
= 1;
1531 bgpevpn_get_rmac(vpn
, &attr_def_gw_ip6
.rmac
);
1533 /* Set up RT, ENCAP and sticky MAC extended community. */
1534 build_evpn_route_extcomm(vpn
, &attr
, AFI_IP
);
1535 build_evpn_route_extcomm(vpn
, &attr_sticky
, AFI_IP
);
1536 build_evpn_route_extcomm(vpn
, &attr_def_gw
, AFI_IP
);
1537 build_evpn_route_extcomm(vpn
, &attr_ip6
, AFI_IP6
);
1538 build_evpn_route_extcomm(vpn
, &attr_sticky_ip6
, AFI_IP6
);
1539 build_evpn_route_extcomm(vpn
, &attr_def_gw_ip6
, AFI_IP
);
1541 /* Walk this VNI's route table and update local type-2 routes. For any
1542 * routes updated, update corresponding entry in the global table too.
1544 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1545 rn
= bgp_route_next(rn
)) {
1546 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1547 struct bgp_node
*rd_rn
;
1548 struct bgp_info
*global_ri
;
1550 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1553 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1554 if (evpn_route_is_sticky(bgp
, rn
))
1555 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1558 else if (evpn_route_is_def_gw(bgp
, rn
))
1559 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1563 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1564 &attr
, 0, 1, &ri
, 0);
1566 if (evpn_route_is_sticky(bgp
, rn
))
1567 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1568 &attr_sticky_ip6
, 0, 1,
1570 else if (evpn_route_is_def_gw(bgp
, rn
))
1571 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1572 &attr_def_gw_ip6
, 0, 1,
1575 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1580 /* If a local route exists for this prefix, we need to update
1581 * the global routing table too.
1586 /* Perform route selection; this is just to set the flags
1588 * as local route in the VNI always wins.
1590 evpn_route_select_install(bgp
, vpn
, rn
);
1592 attr_new
= ri
->attr
;
1594 /* Update route in global routing table. */
1595 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1596 (struct prefix
*)evp
, &vpn
->prd
);
1598 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
1601 /* Schedule for processing and unlock node. */
1602 bgp_process(bgp
, rd_rn
, afi
, safi
);
1603 bgp_unlock_node(rd_rn
);
1606 /* Unintern temporary. */
1607 aspath_unintern(&attr
.aspath
);
1608 aspath_unintern(&attr_ip6
.aspath
);
1609 aspath_unintern(&attr_sticky
.aspath
);
1610 aspath_unintern(&attr_sticky_ip6
.aspath
);
1611 aspath_unintern(&attr_def_gw
.aspath
);
1612 aspath_unintern(&attr_def_gw_ip6
.aspath
);
1618 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1619 * global routing table. These are also scheduled for withdraw from peers.
1621 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1625 struct bgp_node
*rdrn
, *rn
;
1626 struct bgp_table
*table
;
1627 struct bgp_info
*ri
;
1632 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
1633 if (rdrn
&& rdrn
->info
) {
1634 table
= (struct bgp_table
*)rdrn
->info
;
1635 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1636 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1638 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1641 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1643 bgp_process(bgp
, rn
, afi
, safi
);
1647 /* Unlock RD node. */
1649 bgp_unlock_node(rdrn
);
1655 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1656 * table as well as the per-VNI route table.
1658 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1662 struct bgp_node
*rn
;
1663 struct bgp_info
*ri
;
1668 /* First, walk the global route table for this VNI's type-2 local
1670 * EVPN routes are a 2-level table, first get the RD table.
1672 delete_global_type2_routes(bgp
, vpn
);
1674 /* Next, walk this VNI's route table and delete local type-2 routes. */
1675 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1676 rn
= bgp_route_next(rn
)) {
1677 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1679 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1682 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1684 /* Route entry in local table gets deleted immediately. */
1686 bgp_info_reap(rn
, ri
);
1693 * Delete all routes in the per-VNI route table.
1695 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1697 struct bgp_node
*rn
;
1698 struct bgp_info
*ri
, *nextri
;
1700 /* Walk this VNI's route table and delete all routes. */
1701 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1702 rn
= bgp_route_next(rn
)) {
1703 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
1705 bgp_info_delete(rn
, ri
);
1706 bgp_info_reap(rn
, ri
);
1714 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1715 * export RT getting modified or change to tunnel IP. Note that these
1716 * situations need the route in the per-VNI table as well as the global
1717 * table to be updated (as attributes change).
1719 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1722 struct prefix_evpn p
;
1724 /* Update and advertise the type-3 route (only one) followed by the
1725 * locally learnt type-2 routes (MACIP) - for this VNI.
1727 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1728 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
1732 return update_all_type2_routes(bgp
, vpn
);
1736 * Delete (and withdraw) local routes for specified VNI from the global
1737 * table and per-VNI table. After this, remove all other routes from
1738 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1739 * (advertise-all-vni) being disabled.
1741 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1744 struct prefix_evpn p
;
1746 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1747 * followed by type-3 routes (only one) - for this VNI.
1749 ret
= delete_all_type2_routes(bgp
, vpn
);
1753 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1754 ret
= delete_evpn_route(bgp
, vpn
, &p
);
1758 /* Delete all routes from the per-VNI table. */
1759 return delete_all_vni_routes(bgp
, vpn
);
1763 * There is a tunnel endpoint IP address change for this VNI,
1764 * need to re-advertise routes with the new nexthop.
1766 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1767 struct in_addr originator_ip
)
1769 struct prefix_evpn p
;
1771 /* If VNI is not live, we only need to update the originator ip */
1772 if (!is_vni_live(vpn
)) {
1773 vpn
->originator_ip
= originator_ip
;
1777 /* Update the tunnel-ip hash */
1778 bgp_tip_del(bgp
, &vpn
->originator_ip
);
1779 bgp_tip_add(bgp
, &originator_ip
);
1781 /* filter routes as martian nexthop db has changed */
1782 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
1784 /* Need to withdraw type-3 route as the originator IP is part
1787 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1788 delete_evpn_route(bgp
, vpn
, &p
);
1790 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1791 vpn
->originator_ip
= originator_ip
;
1792 return update_routes_for_vni(bgp
, vpn
);
1796 * Install route entry into the VRF routing table and invoke route selection.
1798 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1799 struct prefix_evpn
*evp
,
1800 struct bgp_info
*parent_ri
)
1802 struct bgp_node
*rn
;
1803 struct bgp_info
*ri
;
1804 struct attr
*attr_new
;
1807 struct prefix
*pp
= &p
;
1810 char buf
[PREFIX_STRLEN
];
1811 char buf1
[PREFIX_STRLEN
];
1813 memset(pp
, 0, sizeof(struct prefix
));
1814 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1815 ip_prefix_from_type2_prefix(evp
, pp
);
1816 else if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
1817 ip_prefix_from_type5_prefix(evp
, pp
);
1819 if (bgp_debug_zebra(NULL
)) {
1820 zlog_debug("installing evpn prefix %s as ip prefix %s in vrf %s",
1821 prefix2str(evp
, buf
, sizeof(buf
)),
1822 prefix2str(pp
, buf1
, sizeof(buf
)),
1823 vrf_id_to_name(bgp_vrf
->vrf_id
));
1826 /* Create (or fetch) route within the VRF. */
1827 /* NOTE: There is no RD here. */
1828 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1830 safi
= SAFI_UNICAST
;
1831 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1832 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp
)) {
1834 safi
= SAFI_UNICAST
;
1835 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1839 /* Check if route entry is already present. */
1840 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1842 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1846 /* Add (or update) attribute to hash. */
1847 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1849 /* Create new route with its attribute. */
1850 ri
= info_make(parent_ri
->type
, parent_ri
->sub_type
, 0,
1851 parent_ri
->peer
, attr_new
, rn
);
1852 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1853 bgp_info_extra_get(ri
);
1854 ri
->extra
->parent
= parent_ri
;
1855 if (parent_ri
->extra
) {
1856 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1857 sizeof(ri
->extra
->label
));
1858 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1860 bgp_info_add(rn
, ri
);
1862 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1863 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1864 bgp_unlock_node(rn
);
1867 /* The attribute has changed. */
1868 /* Add (or update) attribute to hash. */
1869 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1871 /* Restore route, if needed. */
1872 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1873 bgp_info_restore(rn
, ri
);
1875 /* Mark if nexthop has changed. */
1876 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1877 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1879 /* Unintern existing, set to new. */
1880 bgp_attr_unintern(&ri
->attr
);
1881 ri
->attr
= attr_new
;
1882 ri
->uptime
= bgp_clock();
1885 /* Perform route selection and update zebra, if required. */
1886 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1892 * Install route entry into the VNI routing table and invoke route selection.
1894 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1895 struct prefix_evpn
*p
,
1896 struct bgp_info
*parent_ri
)
1898 struct bgp_node
*rn
;
1899 struct bgp_info
*ri
;
1900 struct attr
*attr_new
;
1903 /* Create (or fetch) route within the VNI. */
1904 /* NOTE: There is no RD here. */
1905 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1907 /* Check if route entry is already present. */
1908 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1910 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1914 /* Add (or update) attribute to hash. */
1915 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1917 /* Create new route with its attribute. */
1918 ri
= info_make(parent_ri
->type
, parent_ri
->sub_type
, 0,
1919 parent_ri
->peer
, attr_new
, rn
);
1920 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1921 bgp_info_extra_get(ri
);
1922 ri
->extra
->parent
= parent_ri
;
1923 if (parent_ri
->extra
) {
1924 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1925 sizeof(ri
->extra
->label
));
1926 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1928 bgp_info_add(rn
, ri
);
1930 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1931 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1932 bgp_unlock_node(rn
);
1935 /* The attribute has changed. */
1936 /* Add (or update) attribute to hash. */
1937 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1939 /* Restore route, if needed. */
1940 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1941 bgp_info_restore(rn
, ri
);
1943 /* Mark if nexthop has changed. */
1944 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1945 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1947 /* Unintern existing, set to new. */
1948 bgp_attr_unintern(&ri
->attr
);
1949 ri
->attr
= attr_new
;
1950 ri
->uptime
= bgp_clock();
1953 /* Perform route selection and update zebra, if required. */
1954 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
1960 * Uninstall route entry from the VRF routing table and send message
1961 * to zebra, if appropriate.
1963 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1964 struct prefix_evpn
*evp
,
1965 struct bgp_info
*parent_ri
)
1967 struct bgp_node
*rn
;
1968 struct bgp_info
*ri
;
1971 struct prefix
*pp
= &p
;
1974 char buf
[PREFIX_STRLEN
];
1975 char buf1
[PREFIX_STRLEN
];
1977 memset(pp
, 0, sizeof(struct prefix
));
1978 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1979 ip_prefix_from_type2_prefix(evp
, pp
);
1980 else if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
1981 ip_prefix_from_type5_prefix(evp
, pp
);
1983 if (bgp_debug_zebra(NULL
)) {
1984 zlog_debug("uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1985 prefix2str(evp
, buf
, sizeof(buf
)),
1986 prefix2str(pp
, buf1
, sizeof(buf
)),
1987 vrf_id_to_name(bgp_vrf
->vrf_id
));
1990 /* Locate route within the VRF. */
1991 /* NOTE: There is no RD here. */
1992 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1994 safi
= SAFI_UNICAST
;
1995 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
1998 safi
= SAFI_UNICAST
;
1999 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2005 /* Find matching route entry. */
2006 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2008 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2014 /* Mark entry for deletion */
2015 bgp_info_delete(rn
, ri
);
2017 /* Perform route selection and update zebra, if required. */
2018 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2020 /* Unlock route node. */
2021 bgp_unlock_node(rn
);
2027 * Uninstall route entry from the VNI routing table and send message
2028 * to zebra, if appropriate.
2030 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2031 struct prefix_evpn
*p
,
2032 struct bgp_info
*parent_ri
)
2034 struct bgp_node
*rn
;
2035 struct bgp_info
*ri
;
2038 /* Locate route within the VNI. */
2039 /* NOTE: There is no RD here. */
2040 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2044 /* Find matching route entry. */
2045 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2047 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2053 /* Mark entry for deletion */
2054 bgp_info_delete(rn
, ri
);
2056 /* Perform route selection and update zebra, if required. */
2057 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2059 /* Unlock route node. */
2060 bgp_unlock_node(rn
);
2066 * Given a route entry and a VRF, see if this route entry should be
2067 * imported into the VRF i.e., RTs match.
2069 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2070 struct bgp_info
*ri
)
2072 struct attr
*attr
= ri
->attr
;
2073 struct ecommunity
*ecom
;
2077 /* Route should have valid RT to be even considered. */
2078 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2081 ecom
= attr
->ecommunity
;
2082 if (!ecom
|| !ecom
->size
)
2085 /* For each extended community RT, see if it matches this VNI. If any RT
2086 * matches, we're done.
2088 for (i
= 0; i
< ecom
->size
; i
++) {
2090 u_char type
, sub_type
;
2091 struct ecommunity_val
*eval
;
2092 struct ecommunity_val eval_tmp
;
2093 struct vrf_irt_node
*irt
;
2095 /* Only deal with RTs */
2096 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2097 eval
= (struct ecommunity_val
*)(ecom
->val
2098 + (i
* ECOMMUNITY_SIZE
));
2101 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2104 /* See if this RT matches specified VNIs import RTs */
2105 irt
= lookup_vrf_import_rt(eval
);
2106 if (irt
&& irt
->vrfs
)
2107 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2110 /* Also check for non-exact match. In this, we mask out the AS
2112 * only check on the local-admin sub-field. This is to
2114 * VNI as the RT for EBGP peering too.
2117 if (type
== ECOMMUNITY_ENCODE_AS
2118 || type
== ECOMMUNITY_ENCODE_AS4
2119 || type
== ECOMMUNITY_ENCODE_IP
) {
2120 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2121 mask_ecom_global_admin(&eval_tmp
, eval
);
2122 irt
= lookup_vrf_import_rt(&eval_tmp
);
2124 if (irt
&& irt
->vrfs
)
2125 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2133 * Given a route entry and a VNI, see if this route entry should be
2134 * imported into the VNI i.e., RTs match.
2136 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2137 struct bgp_info
*ri
)
2139 struct attr
*attr
= ri
->attr
;
2140 struct ecommunity
*ecom
;
2144 /* Route should have valid RT to be even considered. */
2145 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2148 ecom
= attr
->ecommunity
;
2149 if (!ecom
|| !ecom
->size
)
2152 /* For each extended community RT, see if it matches this VNI. If any RT
2153 * matches, we're done.
2155 for (i
= 0; i
< ecom
->size
; i
++) {
2157 u_char type
, sub_type
;
2158 struct ecommunity_val
*eval
;
2159 struct ecommunity_val eval_tmp
;
2160 struct irt_node
*irt
;
2162 /* Only deal with RTs */
2163 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2164 eval
= (struct ecommunity_val
*)(ecom
->val
2165 + (i
* ECOMMUNITY_SIZE
));
2168 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2171 /* See if this RT matches specified VNIs import RTs */
2172 irt
= lookup_import_rt(bgp
, eval
);
2173 if (irt
&& irt
->vnis
)
2174 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2177 /* Also check for non-exact match. In this, we mask out the AS
2179 * only check on the local-admin sub-field. This is to
2181 * VNI as the RT for EBGP peering too.
2184 if (type
== ECOMMUNITY_ENCODE_AS
2185 || type
== ECOMMUNITY_ENCODE_AS4
2186 || type
== ECOMMUNITY_ENCODE_IP
) {
2187 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2188 mask_ecom_global_admin(&eval_tmp
, eval
);
2189 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2191 if (irt
&& irt
->vnis
)
2192 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2200 * Install or uninstall mac-ip routes are appropriate for this
2203 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
,
2208 struct bgp_node
*rd_rn
, *rn
;
2209 struct bgp_table
*table
;
2210 struct bgp_info
*ri
;
2212 char buf
[PREFIX_STRLEN
];
2213 struct bgp
*bgp_def
= NULL
;
2217 bgp_def
= bgp_get_default();
2221 /* Walk entire global routing table and evaluate routes which could be
2222 * imported into this VRF. Note that we need to loop through all global
2223 * routes to determine which route matches the import rt on vrf
2225 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2226 rd_rn
= bgp_route_next(rd_rn
)) {
2227 table
= (struct bgp_table
*)(rd_rn
->info
);
2231 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2232 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2234 /* if not mac-ip route skip this route */
2235 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
2236 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2239 /* if not a mac+ip route skip this route */
2240 if (!(IS_EVPN_PREFIX_IPADDR_V4(evp
) ||
2241 IS_EVPN_PREFIX_IPADDR_V6(evp
)))
2244 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2245 /* Consider "valid" remote routes applicable for
2248 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2249 && ri
->type
== ZEBRA_ROUTE_BGP
2250 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2253 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2256 install_evpn_route_entry_in_vrf(
2260 uninstall_evpn_route_entry_in_vrf(
2265 "Failed to %s EVPN %s route in VRF %s",
2268 prefix2str(evp
, buf
,
2270 vrf_id_to_name(bgp_vrf
->vrf_id
));
2282 * Install or uninstall routes of specified type that are appropriate for this
2285 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2286 struct bgpevpn
*vpn
,
2287 bgp_evpn_route_type rtype
,
2292 struct bgp_node
*rd_rn
, *rn
;
2293 struct bgp_table
*table
;
2294 struct bgp_info
*ri
;
2300 /* Walk entire global routing table and evaluate routes which could be
2301 * imported into this VPN. Note that we cannot just look at the routes
2303 * the VNI's RD - remote routes applicable for this VNI could have any
2306 /* EVPN routes are a 2-level table. */
2307 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2308 rd_rn
= bgp_route_next(rd_rn
)) {
2309 table
= (struct bgp_table
*)(rd_rn
->info
);
2313 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2314 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2316 if (evp
->prefix
.route_type
!= rtype
)
2319 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2320 /* Consider "valid" remote routes applicable for
2322 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2323 && ri
->type
== ZEBRA_ROUTE_BGP
2324 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2327 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
2329 ret
= install_evpn_route_entry(
2332 ret
= uninstall_evpn_route_entry(
2337 "%u: Failed to %s EVPN %s route in VNI %u",
2341 rtype
== BGP_EVPN_MAC_IP_ROUTE
2355 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
2356 * is invoked upon l3vni-add or l3vni import rt change
2358 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
2360 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
2365 * Install any existing remote routes applicable for this VNI into its
2366 * routing table. This is invoked when a VNI becomes "live" or its Import
2369 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2373 /* Install type-3 routes followed by type-2 routes - the ones applicable
2376 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2381 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2385 /* uninstall routes from l3vni vrf. */
2386 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
2388 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
2393 * Uninstall any existing remote routes for this VNI. One scenario in which
2394 * this is invoked is upon an import RT change.
2396 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2400 /* Uninstall type-2 routes followed by type-3 routes - the ones
2404 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2409 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2414 * Install or uninstall route in matching VRFs (list).
2416 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
2417 safi_t safi
, struct prefix_evpn
*evp
,
2418 struct bgp_info
*ri
,
2419 struct list
*vrfs
, int install
)
2421 char buf
[PREFIX2STR_BUFFER
];
2422 struct bgp
*bgp_vrf
;
2423 struct listnode
*node
, *nnode
;
2425 /* Only type-2/type-5 routes go into a VRF */
2426 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
2427 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2430 /* if it is type-2 route and not a mac+ip route skip this route */
2431 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) &&
2432 !(IS_EVPN_PREFIX_IPADDR_V4(evp
) || IS_EVPN_PREFIX_IPADDR_V6(evp
)))
2435 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
2439 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
,
2442 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
,
2446 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2448 install
? "install" : "uninstall",
2449 prefix2str(evp
, buf
, sizeof(buf
)),
2450 vrf_id_to_name(bgp_vrf
->vrf_id
));
2459 * Install or uninstall route in matching VNIs (list).
2461 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
2462 safi_t safi
, struct prefix_evpn
*evp
,
2463 struct bgp_info
*ri
,
2464 struct list
*vnis
, int install
)
2466 struct bgpevpn
*vpn
;
2467 struct listnode
*node
, *nnode
;
2469 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
2472 if (!is_vni_live(vpn
))
2476 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2478 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2481 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2482 bgp
->vrf_id
, install
? "install" : "uninstall",
2483 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2495 * Install or uninstall route for appropriate VNIs.
2497 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2498 struct prefix
*p
, struct bgp_info
*ri
,
2501 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
2502 struct attr
*attr
= ri
->attr
;
2503 struct ecommunity
*ecom
;
2508 /* Only type-2 and type-3 and type-5 are supported currently */
2509 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2510 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
2511 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2514 /* If we don't have Route Target, nothing much to do. */
2515 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2518 ecom
= attr
->ecommunity
;
2519 if (!ecom
|| !ecom
->size
)
2522 /* For each extended community RT, see which VNIs/VRFs match and import
2523 * the route into matching VNIs/VRFs.
2525 for (i
= 0; i
< ecom
->size
; i
++) {
2527 u_char type
, sub_type
;
2528 struct ecommunity_val
*eval
;
2529 struct ecommunity_val eval_tmp
;
2530 struct irt_node
*irt
; /* import rt for l2vni */
2531 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
2533 /* Only deal with RTs */
2534 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2535 eval
= (struct ecommunity_val
*)(ecom
->val
2536 + (i
* ECOMMUNITY_SIZE
));
2539 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2542 /* Import route into matching l2-vnis (type-2/type-3 routes go
2545 irt
= lookup_import_rt(bgp
, eval
);
2546 if (irt
&& irt
->vnis
)
2547 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2550 /* Import route into matching l3-vnis (type-2/type-5 routes go
2551 * into l3vni/vrf table)
2553 vrf_irt
= lookup_vrf_import_rt(eval
);
2554 if (vrf_irt
&& vrf_irt
->vrfs
)
2555 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2556 vrf_irt
->vrfs
, import
);
2558 /* Also check for non-exact match. In this,
2559 * we mask out the AS and
2560 * only check on the local-admin sub-field.
2561 * This is to facilitate using
2562 * VNI as the RT for EBGP peering too.
2566 if (type
== ECOMMUNITY_ENCODE_AS
2567 || type
== ECOMMUNITY_ENCODE_AS4
2568 || type
== ECOMMUNITY_ENCODE_IP
) {
2569 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2570 mask_ecom_global_admin(&eval_tmp
, eval
);
2571 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2572 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
2574 if (irt
&& irt
->vnis
)
2575 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2577 if (vrf_irt
&& vrf_irt
->vrfs
)
2578 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
,
2586 /* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
2588 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
2590 /* delete all ipv4 routes and withdraw from peers */
2591 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2593 /* delete all ipv6 routes and withdraw from peers */
2594 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2597 /* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
2599 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
2601 /* update all ipv4 routes */
2602 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2604 /* update all ipv6 routes */
2605 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2609 * update and advertise local routes for a VRF as type-5 routes.
2610 * This is invoked upon RD change for a VRF. Note taht the processing is only
2611 * done in the global route table using the routes which already exist in the
2614 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
2616 /* skip if the RD is configured */
2617 if (is_vrf_rd_configured(bgp_vrf
))
2620 /* derive the RD for the VRF based on new router-id */
2621 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
2623 /* update advertise ipv4|ipv6 routes as type-5 routes */
2624 update_advertise_vrf_routes(bgp_vrf
);
2628 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2629 * This is invoked upon VRF RD change. The processing is done only from global
2632 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
2634 /* skip if the RD is configured */
2635 if (is_vrf_rd_configured(bgp_vrf
))
2638 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
2639 delete_withdraw_vrf_routes(bgp_vrf
);
2643 * Update and advertise local routes for a VNI. Invoked upon router-id
2644 * change. Note that the processing is done only on the global route table
2645 * using routes that already exist in the per-VNI table.
2647 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2649 struct prefix_evpn p
;
2650 struct bgp_node
*rn
, *global_rn
;
2651 struct bgp_info
*ri
, *global_ri
;
2653 afi_t afi
= AFI_L2VPN
;
2654 safi_t safi
= SAFI_EVPN
;
2656 /* Locate type-3 route for VNI in the per-VNI table and use its
2657 * attributes to create and advertise the type-3 route for this VNI
2658 * in the global table.
2660 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2661 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
2662 if (!rn
) /* unexpected */
2664 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2665 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
2666 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2668 if (!ri
) /* unexpected */
2672 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2673 (struct prefix
*)&p
, &vpn
->prd
);
2674 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
2677 /* Schedule for processing and unlock node. */
2678 bgp_process(bgp
, global_rn
, afi
, safi
);
2679 bgp_unlock_node(global_rn
);
2681 /* Now, walk this VNI's route table and use the route and its attribute
2682 * to create and schedule route in global table.
2684 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2685 rn
= bgp_route_next(rn
)) {
2686 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2688 /* Identify MAC-IP local routes. */
2689 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2692 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2693 if (ri
->peer
== bgp
->peer_self
2694 && ri
->type
== ZEBRA_ROUTE_BGP
2695 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2700 /* Create route in global routing table using this route entry's
2704 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2705 (struct prefix
*)evp
, &vpn
->prd
);
2707 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
2710 /* Schedule for processing and unlock node. */
2711 bgp_process(bgp
, global_rn
, afi
, safi
);
2712 bgp_unlock_node(global_rn
);
2719 * Delete (and withdraw) local routes for a VNI - only from the global
2720 * table. Invoked upon router-id change.
2722 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2725 struct prefix_evpn p
;
2726 struct bgp_node
*global_rn
;
2727 struct bgp_info
*ri
;
2728 afi_t afi
= AFI_L2VPN
;
2729 safi_t safi
= SAFI_EVPN
;
2731 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2732 * for this VNI - from the global table.
2734 ret
= delete_global_type2_routes(bgp
, vpn
);
2738 /* Remove type-3 route for this VNI from global table. */
2739 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2740 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2741 (struct prefix
*)&p
, &vpn
->prd
);
2743 /* Delete route entry in the global EVPN table. */
2744 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
2746 /* Schedule for processing - withdraws to peers happen from
2750 bgp_process(bgp
, global_rn
, afi
, safi
);
2751 bgp_unlock_node(global_rn
);
2758 * Handle router-id change. Update and advertise local routes corresponding
2759 * to this VNI from peers. Note that this is invoked after updating the
2760 * router-id. The routes in the per-VNI table are used to create routes in
2761 * the global table and schedule them.
2763 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2765 struct bgpevpn
*vpn
;
2767 vpn
= (struct bgpevpn
*)backet
->data
;
2770 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2774 /* Skip VNIs with configured RD. */
2775 if (is_rd_configured(vpn
))
2778 bgp_evpn_derive_auto_rd(bgp
, vpn
);
2779 update_advertise_vni_routes(bgp
, vpn
);
2783 * Handle router-id change. Delete and withdraw local routes corresponding
2784 * to this VNI from peers. Note that this is invoked prior to updating
2785 * the router-id and is done only on the global route table, the routes
2786 * are needed in the per-VNI table to re-advertise with new router id.
2788 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2790 struct bgpevpn
*vpn
;
2792 vpn
= (struct bgpevpn
*)backet
->data
;
2795 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2799 /* Skip VNIs with configured RD. */
2800 if (is_rd_configured(vpn
))
2803 delete_withdraw_vni_routes(bgp
, vpn
);
2807 * Process received EVPN type-2 route (advertise or withdraw).
2809 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2810 struct attr
*attr
, u_char
*pfx
, int psize
,
2811 u_int32_t addpath_id
)
2813 struct prefix_rd prd
;
2814 struct prefix_evpn p
;
2817 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
2818 u_int32_t num_labels
= 0;
2821 /* Type-2 route should be either 33, 37 or 49 bytes or an
2822 * additional 3 bytes if there is a second label (VNI):
2823 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2824 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2825 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2827 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
2828 && psize
!= 40 && psize
!= 52) {
2829 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2830 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2834 /* Make prefix_rd */
2835 prd
.family
= AF_UNSPEC
;
2837 memcpy(&prd
.val
, pfx
, 8);
2840 /* Make EVPN prefix. */
2841 memset(&p
, 0, sizeof(struct prefix_evpn
));
2843 p
.prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
2844 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
2846 /* Skip over Ethernet Seg Identifier for now. */
2849 /* Skip over Ethernet Tag for now. */
2852 /* Get the MAC Addr len */
2853 macaddr_len
= *pfx
++;
2855 /* Get the MAC Addr */
2856 if (macaddr_len
== (ETH_ALEN
* 8)) {
2857 memcpy(&p
.prefix
.mac
.octet
, pfx
, ETH_ALEN
);
2861 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2862 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
2868 ipaddr_len
= *pfx
++;
2869 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
2870 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
2872 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2873 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2878 ipaddr_len
/= 8; /* Convert to bytes. */
2879 p
.prefix
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
2882 memcpy(&p
.prefix
.ip
.ip
.addr
, pfx
, ipaddr_len
);
2886 /* Get the VNI(s). Stored as bytes here. */
2888 memset(label
, 0, sizeof(label
));
2889 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
2890 pfx
+= BGP_LABEL_BYTES
;
2891 psize
-= (33 + ipaddr_len
);
2892 /* Do we have a second VNI? */
2895 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
2897 * If in future, we are required to access additional fields,
2898 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next field
2902 /* Process the route. */
2904 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2905 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2906 &prd
, &label
[0], num_labels
, 0, NULL
);
2908 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2909 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2910 &prd
, &label
[0], num_labels
, NULL
);
2915 * Process received EVPN type-3 route (advertise or withdraw).
2917 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2918 struct attr
*attr
, u_char
*pfx
, int psize
,
2919 u_int32_t addpath_id
)
2921 struct prefix_rd prd
;
2922 struct prefix_evpn p
;
2926 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2927 * IP len (1) and IP (4 or 16).
2929 if (psize
!= 17 && psize
!= 29) {
2930 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2931 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2935 /* If PMSI is present, log if it is anything other than IR.
2936 * Note: We just simply ignore the values as it is not clear if
2937 * doing anything else is better.
2940 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
2941 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
2942 zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with "
2943 "unsupported PTA %d",
2944 peer
->bgp
->vrf_id
, peer
->host
,
2945 attr
->pmsi_tnl_type
);
2949 /* Make prefix_rd */
2950 prd
.family
= AF_UNSPEC
;
2952 memcpy(&prd
.val
, pfx
, 8);
2955 /* Make EVPN prefix. */
2956 memset(&p
, 0, sizeof(struct prefix_evpn
));
2958 p
.prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
2959 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
2961 /* Skip over Ethernet Tag for now. */
2965 ipaddr_len
= *pfx
++;
2966 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
2967 p
.prefix
.ip
.ipa_type
= IPADDR_V4
;
2968 memcpy(&p
.prefix
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
2971 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2972 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2976 /* Process the route. */
2978 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2979 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2980 &prd
, NULL
, 0, 0, NULL
);
2982 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2983 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2984 &prd
, NULL
, 0, NULL
);
2989 * Process received EVPN type-5 route (advertise or withdraw).
2991 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2992 struct attr
*attr
, u_char
*pfx
, int psize
,
2993 u_int32_t addpath_id
, int withdraw
)
2995 struct prefix_rd prd
;
2996 struct prefix_evpn p
;
2997 struct bgp_route_evpn evpn
;
3000 mpls_label_t label
; /* holds the VNI as in the packet */
3003 /* Type-5 route should be 34 or 58 bytes:
3004 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3005 * GW (4 or 16) and VNI (3).
3006 * Note that the IP and GW should both be IPv4 or both IPv6.
3008 if (psize
!= 34 && psize
!= 58) {
3009 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3010 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3014 /* Make prefix_rd */
3015 prd
.family
= AF_UNSPEC
;
3017 memcpy(&prd
.val
, pfx
, 8);
3020 /* Make EVPN prefix. */
3021 memset(&p
, 0, sizeof(struct prefix_evpn
));
3023 p
.prefixlen
= EVPN_TYPE_5_ROUTE_PREFIXLEN
;
3024 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3026 /* Additional information outside of prefix - ESI and GW IP */
3027 memset(&evpn
, 0, sizeof(evpn
));
3030 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3033 /* Fetch Ethernet Tag. */
3034 memcpy(ð_tag
, pfx
, 4);
3035 p
.prefix
.eth_tag
= ntohl(eth_tag
);
3038 /* Fetch IP prefix length. */
3040 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3042 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3043 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3046 p
.prefix
.ip_prefix_length
= ippfx_len
;
3048 /* Determine IPv4 or IPv6 prefix */
3049 /* Since the address and GW are from the same family, this just becomes
3050 * a simple check on the total size.
3053 SET_IPADDR_V4(&p
.prefix
.ip
);
3054 memcpy(&p
.prefix
.ip
.ipaddr_v4
, pfx
, 4);
3056 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3059 SET_IPADDR_V6(&p
.prefix
.ip
);
3060 memcpy(&p
.prefix
.ip
.ipaddr_v6
, pfx
, 16);
3062 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3066 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3067 memset(&label
, 0, sizeof(label
));
3068 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3071 * If in future, we are required to access additional fields,
3072 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next field
3075 /* Process the route. */
3077 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3078 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3079 &prd
, &label
, 1, 0, &evpn
);
3081 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3082 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3083 &prd
, &label
, 1, &evpn
);
3088 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3089 struct prefix_rd
*prd
,
3090 mpls_label_t
*label
, u_int32_t num_labels
,
3095 struct evpn_addr
*p_evpn_p
;
3097 memset(&temp
, 0, 16);
3098 if (p
->family
!= AF_EVPN
)
3100 p_evpn_p
= &(p
->u
.prefix_evpn
);
3102 /* len denites the total len of IP and GW-IP in the route
3103 IP and GW-IP have to be both ipv4 or ipv6
3105 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3106 len
= 8; /* IP and GWIP are both ipv4 */
3108 len
= 32; /* IP and GWIP are both ipv6 */
3109 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3110 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3111 stream_put(s
, prd
->val
, 8);
3113 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3115 stream_put(s
, &temp
, 10);
3116 stream_putl(s
, p_evpn_p
->eth_tag
);
3117 stream_putc(s
, p_evpn_p
->ip_prefix_length
);
3118 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3119 stream_put_ipv4(s
, p_evpn_p
->ip
.ipaddr_v4
.s_addr
);
3121 stream_put(s
, &p_evpn_p
->ip
.ipaddr_v6
, 16);
3123 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3125 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3127 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3129 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3130 stream_put_ipv4(s
, 0);
3132 stream_put(s
, &temp
, 16);
3136 stream_put(s
, label
, 3);
3142 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3144 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3146 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3148 /* Remove EVPN routes and schedule for processing. */
3149 delete_routes_for_vni(bgp
, vpn
);
3151 /* Clear "live" flag and see if hash needs to be freed. */
3152 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3153 if (!is_vni_configured(vpn
))
3154 bgp_evpn_free(bgp
, vpn
);
3158 * Free a VNI entry; iterator function called during cleanup.
3160 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3162 struct bgpevpn
*vpn
;
3164 vpn
= (struct bgpevpn
*)backet
->data
;
3165 delete_all_vni_routes(bgp
, vpn
);
3166 bgp_evpn_free(bgp
, vpn
);
3170 * Derive AUTO import RT for BGP VRF - L3VNI
3172 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
3174 struct bgp
*bgp_def
= NULL
;
3176 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3177 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3180 bgp_def
= bgp_get_default();
3183 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3187 * Delete AUTO import RT from BGP VRF - L3VNI
3189 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
3191 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3195 * Derive AUTO export RT for BGP VRF - L3VNI
3197 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
3199 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3200 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3204 * Delete AUTO export RT from BGP VRF - L3VNI
3206 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
3208 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3211 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
3213 struct bgp
*bgp_def
= NULL
;
3214 struct listnode
*node
= NULL
;
3215 struct bgpevpn
*vpn
= NULL
;
3217 bgp_def
= bgp_get_default();
3221 /* update all type-5 routes */
3222 update_advertise_vrf_routes(bgp_vrf
);
3224 /* update all type-2 routes */
3225 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
3226 update_routes_for_vni(bgp_def
, vpn
);
3233 /* withdraw type-5 route corresponding to ip prefix */
3234 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3235 afi_t afi
, safi_t safi
)
3238 struct prefix_evpn evp
;
3239 char buf
[PREFIX_STRLEN
];
3241 /* NOTE: Check needed as this is called per-route also. */
3242 if (!advertise_type5_routes(bgp_vrf
, afi
))
3245 build_type5_prefix_from_ip_prefix(&evp
, p
);
3246 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
3249 "%u failed to delete type-5 route for prefix %s in vrf %s",
3251 prefix2str(p
, buf
, sizeof(buf
)),
3252 vrf_id_to_name(bgp_vrf
->vrf_id
));
3256 /* withdraw all type-5 routes for an address family */
3257 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
,
3258 afi_t afi
, safi_t safi
)
3260 struct bgp_table
*table
= NULL
;
3261 struct bgp_node
*rn
= NULL
;
3263 /* Bail out early if we don't have to advertise type-5 routes. */
3264 if (!advertise_type5_routes(bgp_vrf
, afi
))
3267 table
= bgp_vrf
->rib
[afi
][safi
];
3268 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
3269 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
, afi
, safi
);
3274 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
3275 * to this function correspond to those of the source IP prefix (best
3276 * path in the case of the attr. In the case of a local prefix (when we
3277 * are advertising local subnets), the src_attr will be NULL.
3279 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3280 struct attr
*src_attr
,
3281 afi_t afi
, safi_t safi
)
3284 struct prefix_evpn evp
;
3285 char buf
[PREFIX_STRLEN
];
3287 /* NOTE: Check needed as this is called per-route also. */
3288 if (!advertise_type5_routes(bgp_vrf
, afi
))
3291 /* only advertise subnet routes as type-5 */
3292 if (is_host_route(p
))
3295 build_type5_prefix_from_ip_prefix(&evp
, p
);
3296 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
3299 "%u: Failed to create type-5 route for prefix %s",
3301 prefix2str(p
, buf
, sizeof(buf
)));
3304 /* Inject all prefixes of a particular address-family (currently, IPv4 or
3305 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
3306 * advertisement is enabled.
3308 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
,
3309 afi_t afi
, safi_t safi
)
3311 struct bgp_table
*table
= NULL
;
3312 struct bgp_node
*rn
= NULL
;
3313 struct bgp_info
*ri
;
3315 /* Bail out early if we don't have to advertise type-5 routes. */
3316 if (!advertise_type5_routes(bgp_vrf
, afi
))
3319 table
= bgp_vrf
->rib
[afi
][safi
];
3320 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3321 /* Need to identify the "selected" route entry to use its
3323 * TODO: Support for AddPath for EVPN.
3325 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3326 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)) {
3328 /* apply the route-map */
3329 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
3334 bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
,
3335 &rn
->p
, RMAP_BGP
, ri
);
3336 if (ret
== RMAP_DENYMATCH
)
3340 bgp_evpn_advertise_type5_route(bgp_vrf
, &rn
->p
,
3349 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
,
3352 struct listnode
*node
, *nnode
, *node_to_del
;
3353 struct ecommunity
*ecom
, *ecom_auto
;
3354 struct ecommunity_val eval
;
3356 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
3358 ecom_auto
= ecommunity_new();
3359 ecommunity_add_val(ecom_auto
, &eval
);
3362 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
3363 if (ecommunity_match(ecom
, ecom_auto
)) {
3364 ecommunity_free(&ecom
);
3370 list_delete_node(rtl
, node_to_del
);
3372 ecommunity_free(&ecom_auto
);
3375 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3376 struct ecommunity
*ecomadd
)
3378 /* uninstall routes from vrf */
3379 uninstall_routes_for_vrf(bgp_vrf
);
3381 /* Cleanup the RT to VRF mapping */
3382 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3384 /* Remove auto generated RT */
3385 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
3387 /* Add the newly configured RT to RT list */
3388 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
3389 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3391 /* map VRF to its RTs */
3392 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3394 /* install routes matching the new VRF */
3395 install_routes_for_vrf(bgp_vrf
);
3398 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3399 struct ecommunity
*ecomdel
)
3401 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3402 struct ecommunity
*ecom
= NULL
;
3404 /* uninstall routes from vrf */
3405 uninstall_routes_for_vrf(bgp_vrf
);
3407 /* Cleanup the RT to VRF mapping */
3408 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3410 /* remove the RT from the RT list */
3411 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3412 if (ecommunity_match(ecom
, ecomdel
)) {
3413 ecommunity_free(&ecom
);
3420 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
3422 /* fallback to auto import rt, if this was the last RT */
3423 if (list_isempty(bgp_vrf
->vrf_import_rtl
)) {
3424 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3425 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
3428 /* map VRFs to its RTs */
3429 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3431 /* install routes matching this new RT */
3432 install_routes_for_vrf(bgp_vrf
);
3435 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3436 struct ecommunity
*ecomadd
)
3438 /* remove auto-generated RT */
3439 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
3441 /* Add the new RT to the RT list */
3442 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
3443 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3445 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3449 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3450 struct ecommunity
*ecomdel
)
3452 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3453 struct ecommunity
*ecom
= NULL
;
3455 /* Remove the RT from the RT list */
3456 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
3457 if (ecommunity_match(ecom
, ecomdel
)) {
3458 ecommunity_free(&ecom
);
3465 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
3467 /* fall back to auto-generated RT if this was the last RT */
3468 if (bgp_vrf
->vrf_export_rtl
&& list_isempty(bgp_vrf
->vrf_export_rtl
)) {
3469 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3470 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
3473 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3477 * Handle change to BGP router id. This is invoked twice by the change
3478 * handler, first before the router id has been changed and then after
3479 * the router id has been changed. The first invocation will result in
3480 * local routes for all VNIs/VRF being deleted and withdrawn and the next
3481 * will result in the routes being re-advertised.
3483 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
3487 /* delete and withdraw all the type-5 routes
3488 stored in the global table for this vrf
3490 withdraw_router_id_vrf(bgp
);
3492 /* delete all the VNI routes (type-2/type-3) routes for all the
3495 hash_iterate(bgp
->vnihash
,
3496 (void (*)(struct hash_backet
*,
3497 void *))withdraw_router_id_vni
,
3501 /* advertise all routes in the vrf as type-5 routes with the new
3504 update_router_id_vrf(bgp
);
3506 /* advertise all the VNI routes (type-2/type-3) routes with the
3509 hash_iterate(bgp
->vnihash
,
3510 (void (*)(struct hash_backet
*,
3511 void *))update_router_id_vni
,
3517 * Handle change to export RT - update and advertise local routes.
3519 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3521 return update_routes_for_vni(bgp
, vpn
);
3524 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
,
3528 delete_withdraw_vrf_routes(bgp_vrf
);
3530 update_advertise_vrf_routes(bgp_vrf
);
3534 * Handle change to RD. This is invoked twice by the change handler,
3535 * first before the RD has been changed and then after the RD has
3536 * been changed. The first invocation will result in local routes
3537 * of this VNI being deleted and withdrawn and the next will result
3538 * in the routes being re-advertised.
3540 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3544 delete_withdraw_vni_routes(bgp
, vpn
);
3546 update_advertise_vni_routes(bgp
, vpn
);
3550 * Install routes for this VNI. Invoked upon change to Import RT.
3552 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3554 return install_routes_for_vni(bgp
, vpn
);
3558 * Uninstall all routes installed for this VNI. Invoked upon change
3561 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3563 return uninstall_routes_for_vni(bgp
, vpn
);
3567 * TODO: Hardcoded for a maximum of 2 VNIs right now
3569 char *bgp_evpn_label2str(mpls_label_t
*label
, u_int32_t num_labels
,
3574 vni1
= label2vni(label
);
3575 if (num_labels
== 2) {
3576 vni2
= label2vni(label
+1);
3577 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
3579 snprintf(buf
, len
, "%u", vni1
);
3584 * Function to convert evpn route to json format.
3585 * NOTE: We don't use prefix2str as the output here is a bit different.
3587 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
3589 char buf1
[ETHER_ADDR_STRLEN
];
3590 char buf2
[PREFIX2STR_BUFFER
];
3595 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3596 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
3597 json_object_int_add(json
, "ethTag", 0);
3598 json_object_int_add(json
, "ipLen",
3599 IS_EVPN_PREFIX_IPADDR_V4(p
)
3602 json_object_string_add(json
, "ip",
3603 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
3604 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3605 if (IS_EVPN_PREFIX_IPADDR_NONE(p
)) {
3606 json_object_int_add(json
, "routeType",
3607 p
->prefix
.route_type
);
3608 json_object_int_add(
3610 0); /* TODO: we don't support esi yet */
3611 json_object_int_add(json
, "ethTag", 0);
3612 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3613 json_object_string_add(json
, "mac",
3614 prefix_mac2str(&p
->prefix
.mac
,
3620 family
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? AF_INET
3623 json_object_int_add(json
, "routeType",
3624 p
->prefix
.route_type
);
3625 json_object_int_add(
3627 0); /* TODO: we don't support esi yet */
3628 json_object_int_add(json
, "ethTag", 0);
3629 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3630 json_object_string_add(json
, "mac",
3631 prefix_mac2str(&p
->prefix
.mac
,
3634 json_object_int_add(json
, "ipLen",
3635 IS_EVPN_PREFIX_IPADDR_V4(p
)
3638 json_object_string_add(
3640 inet_ntop(family
, &p
->prefix
.ip
.ip
.addr
, buf2
,
3641 PREFIX2STR_BUFFER
));
3644 /* Currently, this is to cater to other AF_ETHERNET code. */
3649 * Function to convert evpn route to string.
3650 * NOTE: We don't use prefix2str as the output here is a bit different.
3652 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
3654 char buf1
[ETHER_ADDR_STRLEN
];
3655 char buf2
[PREFIX2STR_BUFFER
];
3657 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3658 snprintf(buf
, len
, "[%d]:[0]:[%d]:[%s]", p
->prefix
.route_type
,
3659 IS_EVPN_PREFIX_IPADDR_V4(p
) ? IPV4_MAX_BITLEN
3661 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
3662 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3663 if (IS_EVPN_PREFIX_IPADDR_NONE(p
))
3664 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]",
3665 p
->prefix
.route_type
, 8 * ETH_ALEN
,
3666 prefix_mac2str(&p
->prefix
.mac
, buf1
,
3671 family
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? AF_INET
3673 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
3674 p
->prefix
.route_type
, 8 * ETH_ALEN
,
3675 prefix_mac2str(&p
->prefix
.mac
, buf1
,
3677 family
== AF_INET
? IPV4_MAX_BITLEN
3679 inet_ntop(family
, &p
->prefix
.ip
.ip
.addr
, buf2
,
3680 PREFIX2STR_BUFFER
));
3682 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3683 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]",
3684 p
->prefix
.route_type
,
3685 p
->prefix
.ip_prefix_length
,
3686 IS_EVPN_PREFIX_IPADDR_V4(p
) ?
3687 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
) :
3688 inet6_ntoa(p
->prefix
.ip
.ipaddr_v6
));
3690 /* For EVPN route types not supported yet. */
3691 snprintf(buf
, len
, "(unsupported route type %d)",
3692 p
->prefix
.route_type
);
3699 * Encode EVPN prefix in Update (MP_REACH)
3701 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
3702 struct prefix_rd
*prd
,
3703 mpls_label_t
*label
, u_int32_t num_labels
,
3704 struct attr
*attr
, int addpath_encode
,
3705 u_int32_t addpath_tx_id
)
3707 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3708 int len
, ipa_len
= 0;
3711 stream_putl(s
, addpath_tx_id
);
3714 stream_putc(s
, evp
->prefix
.route_type
);
3716 switch (evp
->prefix
.route_type
) {
3717 case BGP_EVPN_MAC_IP_ROUTE
:
3718 if (IS_EVPN_PREFIX_IPADDR_V4(evp
))
3719 ipa_len
= IPV4_MAX_BYTELEN
;
3720 else if (IS_EVPN_PREFIX_IPADDR_V6(evp
))
3721 ipa_len
= IPV6_MAX_BYTELEN
;
3722 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3723 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
3724 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
3726 stream_putc(s
, len
);
3727 stream_put(s
, prd
->val
, 8); /* RD */
3728 stream_put(s
, 0, 10); /* ESI */
3729 stream_putl(s
, 0); /* Ethernet Tag ID */
3730 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
3731 stream_put(s
, evp
->prefix
.mac
.octet
, 6); /* Mac Addr */
3732 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
3733 if (ipa_len
) /* IP */
3734 stream_put(s
, &evp
->prefix
.ip
.ip
.addr
, ipa_len
);
3735 /* 1st label is the L2 VNI */
3736 stream_put(s
, label
, BGP_LABEL_BYTES
);
3737 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3738 if (ipa_len
&& num_labels
> 1)
3739 stream_put(s
, label
+1, BGP_LABEL_BYTES
);
3742 case BGP_EVPN_IMET_ROUTE
:
3743 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
3744 stream_put(s
, prd
->val
, 8); /* RD */
3745 stream_putl(s
, 0); /* Ethernet Tag ID */
3746 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
3747 /* Originating Router's IP Addr */
3748 stream_put_in_addr(s
, &evp
->prefix
.ip
.ipaddr_v4
);
3751 case BGP_EVPN_IP_PREFIX_ROUTE
:
3752 /* TODO: AddPath support. */
3753 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
3761 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
3762 struct bgp_nlri
*packet
, int withdraw
)
3768 u_int32_t addpath_id
;
3769 int addpath_encoded
;
3775 /* Check peer status. */
3776 if (peer
->status
!= Established
) {
3777 zlog_err("%u:%s - EVPN update received in state %d",
3778 peer
->bgp
->vrf_id
, peer
->host
, peer
->status
);
3782 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3784 lim
= pnt
+ packet
->length
;
3786 safi
= packet
->safi
;
3790 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
3791 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
3792 PEER_CAP_ADDPATH_AF_TX_RCV
));
3794 for (; pnt
< lim
; pnt
+= psize
) {
3795 /* Clear prefix structure. */
3796 memset(&p
, 0, sizeof(struct prefix
));
3798 /* Deal with path-id if AddPath is supported. */
3799 if (addpath_encoded
) {
3800 /* When packet overflow occurs return immediately. */
3801 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
3804 addpath_id
= ntohl(*((uint32_t *)pnt
));
3805 pnt
+= BGP_ADDPATH_ID_LEN
;
3808 /* All EVPN NLRI types start with type and length. */
3813 psize
= rlen
= *pnt
++;
3815 /* When packet overflow occur return immediately. */
3816 if (pnt
+ psize
> lim
)
3820 case BGP_EVPN_MAC_IP_ROUTE
:
3821 if (process_type2_route(peer
, afi
, safi
,
3822 withdraw
? NULL
: attr
, pnt
,
3823 psize
, addpath_id
)) {
3825 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3826 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3831 case BGP_EVPN_IMET_ROUTE
:
3832 if (process_type3_route(peer
, afi
, safi
,
3833 withdraw
? NULL
: attr
, pnt
,
3834 psize
, addpath_id
)) {
3836 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3837 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3842 case BGP_EVPN_IP_PREFIX_ROUTE
:
3843 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
3844 psize
, addpath_id
, withdraw
)) {
3846 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3847 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3857 /* Packet length consistency check. */
3865 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3866 * The mapping will be used during route processing.
3867 * bgp_def: default bgp instance
3868 * bgp_vrf: specific bgp vrf instance on which RT is configured
3870 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
3873 struct ecommunity_val
*eval
= NULL
;
3874 struct listnode
*node
= NULL
, *nnode
= NULL
;
3875 struct ecommunity
*ecom
= NULL
;
3877 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3878 for (i
= 0; i
< ecom
->size
; i
++) {
3879 eval
= (struct ecommunity_val
*)(ecom
->val
3881 * ECOMMUNITY_SIZE
));
3882 map_vrf_to_rt(bgp_vrf
, eval
);
3888 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3890 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
3893 struct ecommunity_val
*eval
;
3894 struct listnode
*node
, *nnode
;
3895 struct ecommunity
*ecom
;
3897 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3898 for (i
= 0; i
< ecom
->size
; i
++) {
3899 struct vrf_irt_node
*irt
;
3900 struct ecommunity_val eval_tmp
;
3902 eval
= (struct ecommunity_val
*)(ecom
->val
3904 * ECOMMUNITY_SIZE
));
3905 /* If using "automatic" RT, we only care about the
3906 * local-admin sub-field.
3907 * This is to facilitate using VNI as the RT for EBGP
3910 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3911 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
3912 BGP_VRF_IMPORT_RT_CFGD
))
3913 mask_ecom_global_admin(&eval_tmp
, eval
);
3915 irt
= lookup_vrf_import_rt(&eval_tmp
);
3917 unmap_vrf_from_rt(bgp_vrf
, irt
);
3925 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3926 * The mapping will be used during route processing.
3928 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3931 struct ecommunity_val
*eval
;
3932 struct listnode
*node
, *nnode
;
3933 struct ecommunity
*ecom
;
3935 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3936 for (i
= 0; i
< ecom
->size
; i
++) {
3937 eval
= (struct ecommunity_val
*)(ecom
->val
3939 * ECOMMUNITY_SIZE
));
3940 map_vni_to_rt(bgp
, vpn
, eval
);
3946 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3948 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3951 struct ecommunity_val
*eval
;
3952 struct listnode
*node
, *nnode
;
3953 struct ecommunity
*ecom
;
3955 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3956 for (i
= 0; i
< ecom
->size
; i
++) {
3957 struct irt_node
*irt
;
3958 struct ecommunity_val eval_tmp
;
3960 eval
= (struct ecommunity_val
*)(ecom
->val
3962 * ECOMMUNITY_SIZE
));
3963 /* If using "automatic" RT, we only care about the
3964 * local-admin sub-field.
3965 * This is to facilitate using VNI as the RT for EBGP
3968 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3969 if (!is_import_rt_configured(vpn
))
3970 mask_ecom_global_admin(&eval_tmp
, eval
);
3972 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3974 unmap_vni_from_rt(bgp
, vpn
, irt
);
3980 * Derive Import RT automatically for VNI and map VNI to RT.
3981 * The mapping will be used during route processing.
3983 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3985 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
3986 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
3989 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
3993 * Derive Export RT automatically for VNI.
3995 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3997 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
3998 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4002 * Derive RD automatically for VNI using passed information - it
4003 * is of the form RouterId:unique-id-for-vni.
4005 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4009 bgp
->vrf_prd
.family
= AF_UNSPEC
;
4010 bgp
->vrf_prd
.prefixlen
= 64;
4011 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), bgp
->vrf_rd_id
);
4012 str2prefix_rd(buf
, &bgp
->vrf_prd
);
4016 * Derive RD automatically for VNI using passed information - it
4017 * is of the form RouterId:unique-id-for-vni.
4019 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4023 vpn
->prd
.family
= AF_UNSPEC
;
4024 vpn
->prd
.prefixlen
= 64;
4025 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4026 (void)str2prefix_rd(buf
, &vpn
->prd
);
4027 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4033 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4035 struct bgpevpn
*vpn
;
4038 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4040 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4045 * Create a new vpn - invoked upon configuration or zebra notification.
4047 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4048 struct in_addr originator_ip
,
4049 vrf_id_t tenant_vrf_id
)
4051 struct bgpevpn
*vpn
;
4056 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4060 /* Set values - RD and RT set to defaults. */
4062 vpn
->originator_ip
= originator_ip
;
4063 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4065 /* Initialize route-target import and export lists */
4066 vpn
->import_rtl
= list_new();
4067 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4068 vpn
->export_rtl
= list_new();
4069 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4070 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4071 derive_rd_rt_for_vni(bgp
, vpn
);
4073 /* Initialize EVPN route table. */
4074 vpn
->route_table
= bgp_table_init(AFI_L2VPN
, SAFI_EVPN
);
4077 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4078 XFREE(MTYPE_BGP_EVPN
, vpn
);
4082 /* add to l2vni list on corresponding vrf */
4083 bgpevpn_link_to_l3vni(vpn
);
4085 QOBJ_REG(vpn
, bgpevpn
);
4090 * Free a given VPN - called in multiple scenarios such as zebra
4091 * notification, configuration being deleted, advertise-all-vni disabled etc.
4092 * This just frees appropriate memory, caller should have taken other
4095 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4097 bgpevpn_unlink_from_l3vni(vpn
);
4098 bgp_table_unlock(vpn
->route_table
);
4099 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4100 list_delete_and_null(&vpn
->import_rtl
);
4101 list_delete_and_null(&vpn
->export_rtl
);
4102 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4103 hash_release(bgp
->vnihash
, vpn
);
4105 XFREE(MTYPE_BGP_EVPN
, vpn
);
4109 * Import route into matching VNI(s).
4111 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4112 struct prefix
*p
, struct bgp_info
*ri
)
4114 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
4118 * Unimport route from matching VNI(s).
4120 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4121 struct prefix
*p
, struct bgp_info
*ri
)
4123 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
4126 /* filter routes which have martian next hops */
4127 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
4131 struct bgp_node
*rd_rn
, *rn
;
4132 struct bgp_table
*table
;
4133 struct bgp_info
*ri
;
4138 /* Walk entire global routing table and evaluate routes which could be
4139 * imported into this VPN. Note that we cannot just look at the routes
4140 * for the VNI's RD -
4141 * remote routes applicable for this VNI could have any RD.
4143 /* EVPN routes are a 2-level table. */
4144 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
4145 rd_rn
= bgp_route_next(rd_rn
)) {
4146 table
= (struct bgp_table
*)(rd_rn
->info
);
4150 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4152 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4154 /* Consider "valid" remote routes applicable for
4156 if (!(ri
->type
== ZEBRA_ROUTE_BGP
4157 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
4160 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
4162 char attr_str
[BUFSIZ
];
4163 char pbuf
[PREFIX_STRLEN
];
4165 bgp_dump_attr(ri
->attr
, attr_str
,
4168 if (bgp_debug_update(ri
->peer
, &rn
->p
,
4171 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
4178 bgp_evpn_unimport_route(bgp
, afi
, safi
,
4181 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
4192 * Handle del of a local MACIP.
4194 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4197 struct bgpevpn
*vpn
;
4198 struct prefix_evpn p
;
4200 if (!bgp
->vnihash
) {
4201 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4205 /* Lookup VNI hash - should exist. */
4206 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4207 if (!vpn
|| !is_vni_live(vpn
)) {
4208 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4209 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4213 /* Remove EVPN type-2 route and schedule for processing. */
4214 build_evpn_type2_prefix(&p
, mac
, ip
);
4215 delete_evpn_route(bgp
, vpn
, &p
);
4221 * Handle add of a local MACIP.
4223 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4224 struct ipaddr
*ip
, u_char flags
)
4226 struct bgpevpn
*vpn
;
4227 struct prefix_evpn p
;
4229 if (!bgp
->vnihash
) {
4230 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4234 /* Lookup VNI hash - should exist. */
4235 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4236 if (!vpn
|| !is_vni_live(vpn
)) {
4237 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4238 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4242 /* Create EVPN type-2 route and schedule for processing. */
4243 build_evpn_type2_prefix(&p
, mac
, ip
);
4244 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
4245 char buf
[ETHER_ADDR_STRLEN
];
4246 char buf2
[INET6_ADDRSTRLEN
];
4249 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
4250 bgp
->vrf_id
, vpn
->vni
,
4251 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? "sticky gateway"
4253 prefix_mac2str(mac
, buf
, sizeof(buf
)),
4254 ipaddr2str(ip
, buf2
, sizeof(buf2
)),
4262 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
4263 struct bgp
*bgp_vrf
)
4265 struct bgpevpn
*vpn
= NULL
;
4266 struct bgp
*bgp_def
= NULL
;
4268 bgp_def
= bgp_get_default();
4271 vpn
= (struct bgpevpn
*)backet
->data
;
4272 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
4273 bgpevpn_link_to_l3vni(vpn
);
4276 int bgp_evpn_local_l3vni_add(vni_t l3vni
,
4278 struct ethaddr
*rmac
,
4279 struct in_addr originator_ip
,
4282 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
4283 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4284 struct listnode
*node
= NULL
;
4285 struct bgpevpn
*vpn
= NULL
;
4288 /* get the default instamce - required to get the AS number for VRF
4291 bgp_def
= bgp_get_default();
4293 zlog_err("Cannot process L3VNI %u ADD - default BGP instance not yet created",
4299 /* if the BGP vrf instance doesnt exist - create one */
4300 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
4305 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
4306 BGP_INSTANCE_TYPE_VRF
);
4308 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
4309 zlog_err("'bgp multiple-instance' not present\n");
4311 case BGP_ERR_AS_MISMATCH
:
4312 zlog_err("BGP is already running; AS is %u\n", as
);
4314 case BGP_ERR_INSTANCE_MISMATCH
:
4315 zlog_err("BGP instance name and AS number mismatch\n");
4319 /* mark as auto created */
4320 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
4323 /* associate with l3vni */
4324 bgp_vrf
->l3vni
= l3vni
;
4326 /* set the router mac - to be used in mac-ip routes for this vrf */
4327 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
4329 /* set the originator ip */
4330 bgp_vrf
->originator_ip
= originator_ip
;
4332 /* set the right filter - are we using l3vni only for prefix routes? */
4334 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
4336 /* auto derive RD/RT */
4337 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4338 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4339 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
4340 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4341 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
4343 /* link all corresponding l2vnis */
4344 hash_iterate(bgp_def
->vnihash
,
4345 (void (*)(struct hash_backet
*, void *))
4346 link_l2vni_hash_to_l3vni
,
4349 /* Only update all corresponding type-2 routes if we are advertising two
4350 * labels along with type-2 routes
4353 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4354 update_routes_for_vni(bgp_def
, vpn
);
4356 /* advertise type-5 routes if needed */
4357 update_advertise_vrf_routes(bgp_vrf
);
4359 /* install all remote routes belonging to this l3vni into correspondng
4361 install_routes_for_vrf(bgp_vrf
);
4366 int bgp_evpn_local_l3vni_del(vni_t l3vni
,
4369 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
4370 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4371 struct listnode
*node
= NULL
;
4372 struct bgpevpn
*vpn
= NULL
;
4374 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
4376 zlog_err("Cannot process L3VNI %u Del - Could not find BGP instance",
4381 bgp_def
= bgp_get_default();
4383 zlog_err("Cannot process L3VNI %u Del - Could not find default BGP instance",
4388 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
4389 * take care of uninstalling the routes from zebra
4391 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4392 uninstall_routes_for_vrf(bgp_vrf
);
4394 /* delete/withdraw all type-5 routes */
4395 delete_withdraw_vrf_routes(bgp_vrf
);
4397 /* remove the l3vni from vrf instance */
4400 /* remove the Rmac from the BGP vrf */
4401 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
4404 if (bgp_vrf
->vrf_import_rtl
&& !list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4405 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4406 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
4408 if (bgp_vrf
->vrf_export_rtl
&& !list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4409 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
4412 /* update all corresponding local mac-ip routes */
4413 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
4414 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
4415 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
4416 update_routes_for_vni(bgp_def
, vpn
);
4420 /* Delete the instance if it was autocreated */
4421 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4422 bgp_delete(bgp_vrf
);
4428 * Handle del of a local VNI.
4430 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
4432 struct bgpevpn
*vpn
;
4434 if (!bgp
->vnihash
) {
4435 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4439 /* Locate VNI hash */
4440 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4442 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
4447 /* Remove all local EVPN routes and schedule for processing (to
4448 * withdraw from peers).
4450 delete_routes_for_vni(bgp
, vpn
);
4453 * tunnel is no longer active, del tunnel ip address from tip_hash
4455 bgp_tip_del(bgp
, &vpn
->originator_ip
);
4457 /* Clear "live" flag and see if hash needs to be freed. */
4458 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4459 if (!is_vni_configured(vpn
))
4460 bgp_evpn_free(bgp
, vpn
);
4466 * Handle add (or update) of a local VNI. The only VNI change we care
4467 * about is change to local-tunnel-ip.
4469 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
4470 struct in_addr originator_ip
,
4471 vrf_id_t tenant_vrf_id
)
4473 struct bgpevpn
*vpn
;
4474 struct prefix_evpn p
;
4476 if (!bgp
->vnihash
) {
4477 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4481 /* Lookup VNI. If present and no change, exit. */
4482 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4485 /* update tenant_vrf_id if required */
4486 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
4487 bgpevpn_unlink_from_l3vni(vpn
);
4488 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4489 bgpevpn_link_to_l3vni(vpn
);
4491 /* update all routes with new export RT for VRFs */
4492 update_routes_for_vni(bgp
, vpn
);
4495 if (is_vni_live(vpn
)
4496 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
4497 /* Probably some other param has changed that we don't
4501 /* Local tunnel endpoint IP address has changed */
4502 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
4505 /* Create or update as appropriate. */
4507 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
4510 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4516 /* if the VNI is live already, there is nothing more to do */
4517 if (is_vni_live(vpn
))
4520 /* Mark as "live" */
4521 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4523 /* tunnel is now active, add tunnel-ip to db */
4524 bgp_tip_add(bgp
, &originator_ip
);
4526 /* filter routes as nexthop database has changed */
4527 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
4529 /* Create EVPN type-3 route and schedule for processing. */
4530 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4531 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
4532 zlog_err("%u: Type3 route creation failure for VNI %u",
4537 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4541 install_routes_for_vni(bgp
, vpn
);
4543 /* If we are advertising gateway mac-ip
4544 It needs to be conveyed again to zebra */
4545 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
4551 * Cleanup EVPN information on disable - Need to delete and withdraw
4552 * EVPN routes from peers.
4554 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
4556 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4557 void *))cleanup_vni_on_disable
,
4562 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4563 * BGP instance (default) is being freed.
4565 void bgp_evpn_cleanup(struct bgp
*bgp
)
4568 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4569 void *))free_vni_entry
,
4571 if (bgp
->import_rt_hash
)
4572 hash_free(bgp
->import_rt_hash
);
4573 bgp
->import_rt_hash
= NULL
;
4574 if (bgp
->vrf_import_rt_hash
)
4575 hash_free(bgp
->vrf_import_rt_hash
);
4576 bgp
->vrf_import_rt_hash
= NULL
;
4578 hash_free(bgp
->vnihash
);
4579 bgp
->vnihash
= NULL
;
4580 if (bgp
->vrf_import_rtl
)
4581 list_delete_and_null(&bgp
->vrf_import_rtl
);
4582 if (bgp
->vrf_export_rtl
)
4583 list_delete_and_null(&bgp
->vrf_export_rtl
);
4585 list_delete_and_null(&bgp
->l2vnis
);
4586 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
4590 * Initialization for EVPN
4593 * hash for RT to VNI
4594 * assign a unique rd id for auto derivation of vrf_prd
4596 void bgp_evpn_init(struct bgp
*bgp
)
4599 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
4600 bgp
->import_rt_hash
=
4601 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
4602 "BGP Import RT Hash");
4603 bgp
->vrf_import_rt_hash
=
4604 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
4605 "BGP VRF Import RT Hash");
4606 bgp
->vrf_import_rtl
= list_new();
4607 bgp
->vrf_import_rtl
->cmp
=
4608 (int (*)(void *, void *))evpn_route_target_cmp
;
4610 bgp
->vrf_export_rtl
= list_new();
4611 bgp
->vrf_export_rtl
->cmp
=
4612 (int (*)(void *, void *))evpn_route_target_cmp
;
4613 bgp
->l2vnis
= list_new();
4615 (int (*)(void *, void *))vni_hash_cmp
;
4616 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
4620 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
4622 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);