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
;
102 unsigned int key
= 0;
118 * Comparison function for vrf import rt hash
120 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
122 const struct vrf_irt_node
*irt1
= p1
;
123 const struct vrf_irt_node
*irt2
= p2
;
125 if (irt1
== NULL
&& irt2
== NULL
)
128 if (irt1
== NULL
|| irt2
== NULL
)
131 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
135 * Create a new vrf import_rt in default instance
137 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
139 struct bgp
*bgp_def
= NULL
;
140 struct vrf_irt_node
*irt
;
142 bgp_def
= bgp_get_default();
144 zlog_err("vrf import rt new - def instance not created yet");
148 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
149 sizeof(struct vrf_irt_node
));
154 irt
->vrfs
= list_new();
157 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
158 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
166 * Free the vrf import rt node
168 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
170 struct bgp
*bgp_def
= NULL
;
172 bgp_def
= bgp_get_default();
174 zlog_err("vrf import rt free - def instance not created yet");
178 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
179 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
183 * Function to lookup Import RT node - used to map a RT to set of
184 * VNIs importing routes with that RT.
186 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
188 struct bgp
*bgp_def
= NULL
;
189 struct vrf_irt_node
*irt
;
190 struct vrf_irt_node tmp
;
192 bgp_def
= bgp_get_default();
194 zlog_err("vrf import rt lookup - def instance not created yet");
198 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
199 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
200 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
205 * Is specified VRF present on the RT's list of "importing" VRFs?
207 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
,
210 struct listnode
*node
= NULL
, *nnode
= NULL
;
211 struct bgp
*tmp_bgp_vrf
= NULL
;
213 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
214 if (tmp_bgp_vrf
== bgp_vrf
)
221 * Make import route target hash key.
223 static unsigned int import_rt_hash_key_make(void *p
)
225 struct irt_node
*irt
= p
;
226 char *pnt
= irt
->rt
.val
;
227 unsigned int key
= 0;
243 * Comparison function for import rt hash
245 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
247 const struct irt_node
*irt1
= p1
;
248 const struct irt_node
*irt2
= p2
;
250 if (irt1
== NULL
&& irt2
== NULL
)
253 if (irt1
== NULL
|| irt2
== NULL
)
256 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
260 * Create a new import_rt
262 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
263 struct ecommunity_val
*rt
)
265 struct irt_node
*irt
;
270 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
275 irt
->vnis
= list_new();
278 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
279 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
287 * Free the import rt node
289 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
291 hash_release(bgp
->import_rt_hash
, irt
);
292 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
296 * Function to lookup Import RT node - used to map a RT to set of
297 * VNIs importing routes with that RT.
299 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
300 struct ecommunity_val
*rt
)
302 struct irt_node
*irt
;
305 memset(&tmp
, 0, sizeof(struct irt_node
));
306 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
307 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
312 * Is specified VNI present on the RT's list of "importing" VNIs?
314 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
316 struct listnode
*node
, *nnode
;
317 struct bgpevpn
*tmp_vpn
;
319 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
328 * Compare Route Targets.
330 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
331 struct ecommunity
*ecom2
)
339 if (!ecom1
&& !ecom2
)
342 if (ecom1
->str
&& !ecom2
->str
)
345 if (!ecom1
->str
&& ecom2
->str
)
348 if (!ecom1
->str
&& !ecom2
->str
)
351 return strcmp(ecom1
->str
, ecom2
->str
);
355 * Mask off global-admin field of specified extended community (RT),
356 * just retain the local-admin field.
358 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
359 struct ecommunity_val
*src
)
365 if (type
== ECOMMUNITY_ENCODE_AS
) {
366 dst
->val
[2] = dst
->val
[3] = 0;
367 } else if (type
== ECOMMUNITY_ENCODE_AS4
368 || type
== ECOMMUNITY_ENCODE_IP
) {
369 dst
->val
[2] = dst
->val
[3] = 0;
370 dst
->val
[4] = dst
->val
[5] = 0;
375 * Map one RT to specified VRF.
376 * bgp_vrf = BGP vrf instance
378 static void map_vrf_to_rt(struct bgp
*bgp_vrf
,
379 struct ecommunity_val
*eval
)
381 struct vrf_irt_node
*irt
= NULL
;
382 struct ecommunity_val eval_tmp
;
384 /* If using "automatic" RT,
385 * we only care about the local-admin sub-field.
386 * This is to facilitate using L3VNI(VRF-VNI)
387 * as the RT for EBGP peering too.
389 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
390 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
391 BGP_VRF_IMPORT_RT_CFGD
))
392 mask_ecom_global_admin(&eval_tmp
, eval
);
394 irt
= lookup_vrf_import_rt(&eval_tmp
);
395 if (irt
&& irt
->vrfs
)
396 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
397 /* Already mapped. */
401 irt
= vrf_import_rt_new(&eval_tmp
);
405 /* Add VRF to the list for this RT. */
406 listnode_add(irt
->vrfs
, bgp_vrf
);
410 * Unmap specified VRF from specified RT. If there are no other
411 * VRFs for this RT, then the RT hash is deleted.
412 * bgp_vrf: BGP VRF specific instance
414 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
,
415 struct vrf_irt_node
*irt
)
417 /* Delete VRF from list for this RT. */
418 listnode_delete(irt
->vrfs
, bgp_vrf
);
419 if (!listnode_head(irt
->vrfs
)) {
420 list_delete_and_null(&irt
->vrfs
);
421 vrf_import_rt_free(irt
);
426 * Map one RT to specified VNI.
428 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
429 struct ecommunity_val
*eval
)
431 struct irt_node
*irt
;
432 struct ecommunity_val eval_tmp
;
434 /* If using "automatic" RT, we only care about the local-admin
436 * This is to facilitate using VNI as the RT for EBGP peering too.
438 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
439 if (!is_import_rt_configured(vpn
))
440 mask_ecom_global_admin(&eval_tmp
, eval
);
442 irt
= lookup_import_rt(bgp
, &eval_tmp
);
443 if (irt
&& irt
->vnis
)
444 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
445 /* Already mapped. */
449 irt
= import_rt_new(bgp
, &eval_tmp
);
453 /* Add VNI to the hash list for this RT. */
454 listnode_add(irt
->vnis
, vpn
);
458 * Unmap specified VNI from specified RT. If there are no other
459 * VNIs for this RT, then the RT hash is deleted.
461 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
462 struct irt_node
*irt
)
464 /* Delete VNI from hash list for this RT. */
465 listnode_delete(irt
->vnis
, vpn
);
466 if (!listnode_head(irt
->vnis
)) {
467 list_delete_and_null(&irt
->vnis
);
468 import_rt_free(bgp
, irt
);
473 * Create RT extended community automatically from passed information:
474 * of the form AS:VNI.
475 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
476 * the need is to get a RT value that will be unique across different
477 * VNIs but the same across routers (in the same AS) for a particular
480 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
482 struct ecommunity_val eval
;
483 struct ecommunity
*ecomadd
;
485 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
487 ecomadd
= ecommunity_new();
488 ecommunity_add_val(ecomadd
, &eval
);
489 listnode_add_sort(rtl
, ecomadd
);
493 * Derive RD and RT for a VNI automatically. Invoked at the time of
496 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
498 bgp_evpn_derive_auto_rd(bgp
, vpn
);
499 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
500 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
504 * Add (update) or delete MACIP from zebra.
506 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
507 struct prefix_evpn
*p
,
508 struct in_addr remote_vtep_ip
, int add
,
513 char buf1
[ETHER_ADDR_STRLEN
];
514 char buf2
[INET6_ADDRSTRLEN
];
515 char buf3
[INET6_ADDRSTRLEN
];
518 if (!zclient
|| zclient
->sock
< 0)
521 /* Don't try to register if Zebra doesn't know of this instance. */
522 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
528 zclient_create_header(s
, add
? ZEBRA_REMOTE_MACIP_ADD
529 : ZEBRA_REMOTE_MACIP_DEL
,
531 stream_putl(s
, vpn
->vni
);
532 stream_put(s
, &p
->prefix
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
533 /* IP address length and IP address, if any. */
534 if (IS_EVPN_PREFIX_IPADDR_NONE(p
))
537 ipa_len
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? IPV4_MAX_BYTELEN
539 stream_putl(s
, ipa_len
);
540 stream_put(s
, &p
->prefix
.ip
.ip
.addr
, ipa_len
);
542 stream_put_in_addr(s
, &remote_vtep_ip
);
544 /* TX MAC sticky status */
546 stream_putc(s
, sticky
);
548 stream_putw_at(s
, 0, stream_get_endp(s
));
550 if (bgp_debug_zebra(NULL
))
551 zlog_debug("Tx %s MACIP, VNI %u %sMAC %s IP %s remote VTEP %s",
552 add
? "ADD" : "DEL", vpn
->vni
,
553 sticky
? "sticky " : "",
554 prefix_mac2str(&p
->prefix
.mac
, buf1
, sizeof(buf1
)),
555 ipaddr2str(&p
->prefix
.ip
, buf3
, sizeof(buf3
)),
556 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
559 return zclient_send_message(zclient
);
563 * Add (update) or delete remote VTEP from zebra.
565 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
566 struct prefix_evpn
*p
, int add
)
571 if (!zclient
|| zclient
->sock
< 0)
574 /* Don't try to register if Zebra doesn't know of this instance. */
575 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
581 zclient_create_header(s
, add
? ZEBRA_REMOTE_VTEP_ADD
582 : ZEBRA_REMOTE_VTEP_DEL
,
584 stream_putl(s
, vpn
->vni
);
585 if (IS_EVPN_PREFIX_IPADDR_V4(p
))
586 stream_put_in_addr(s
, &p
->prefix
.ip
.ipaddr_v4
);
587 else if (IS_EVPN_PREFIX_IPADDR_V6(p
)) {
589 "Bad remote IP when trying to %s remote VTEP for VNI %u",
590 add
? "ADD" : "DEL", vpn
->vni
);
594 stream_putw_at(s
, 0, stream_get_endp(s
));
596 if (bgp_debug_zebra(NULL
))
597 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
598 add
? "ADD" : "DEL", vpn
->vni
,
599 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
601 return zclient_send_message(zclient
);
605 * Build extended communities for EVPN route. RT and ENCAP are
606 * applicable to all routes.
608 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
)
610 struct ecommunity ecom_encap
;
611 struct ecommunity ecom_sticky
;
612 struct ecommunity ecom_rmac
;
613 struct ecommunity_val eval
;
614 struct ecommunity_val eval_sticky
;
615 struct ecommunity_val eval_rmac
;
616 bgp_encap_types tnl_type
;
617 struct listnode
*node
, *nnode
;
618 struct ecommunity
*ecom
;
620 struct list
*vrf_export_rtl
= NULL
;
623 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
624 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
625 encode_encap_extcomm(tnl_type
, &eval
);
627 ecom_encap
.val
= (u_int8_t
*)eval
.val
;
630 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
632 /* Add the export RTs for L2VNI */
633 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
634 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
636 /* Add the export RTs for L3VNI */
637 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
638 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
639 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
640 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
646 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
647 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
648 ecom_sticky
.size
= 1;
649 ecom_sticky
.val
= (u_int8_t
*)eval_sticky
.val
;
651 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
654 if (!is_zero_mac(&attr
->rmac
)) {
655 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
656 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
658 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
659 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
663 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
667 * Add MAC mobility extended community to attribute.
669 static void add_mac_mobility_to_attr(u_int32_t seq_num
, struct attr
*attr
)
671 struct ecommunity ecom_tmp
;
672 struct ecommunity_val eval
;
673 u_int8_t
*ecom_val_ptr
;
680 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
682 /* Find current MM ecommunity */
685 if (attr
->ecommunity
) {
686 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
687 pnt
= attr
->ecommunity
->val
+ (i
* 8);
691 if (type
== ECOMMUNITY_ENCODE_EVPN
693 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
695 (u_int8_t
*)(attr
->ecommunity
->val
702 /* Update the existing MM ecommunity */
704 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
706 /* Add MM to existing */
708 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
710 ecom_tmp
.val
= (u_int8_t
*)eval
.val
;
713 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
717 /* Install EVPN route into zebra. */
718 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
719 struct prefix_evpn
*p
,
720 struct in_addr remote_vtep_ip
, u_char sticky
)
724 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
725 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
728 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
733 /* Uninstall EVPN route from zebra. */
734 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
735 struct prefix_evpn
*p
,
736 struct in_addr remote_vtep_ip
)
740 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
741 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
744 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
750 * Due to MAC mobility, the prior "local" best route has been supplanted
751 * by a "remote" best route. The prior route has to be deleted and withdrawn
754 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
756 struct bgp_info
*old_local
)
758 struct bgp_node
*global_rn
;
760 afi_t afi
= AFI_L2VPN
;
761 safi_t safi
= SAFI_EVPN
;
763 /* Locate route node in the global EVPN routing table. Note that
764 * this table is a 2-level tree (RD-level + Prefix-level) similar to
767 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
768 (struct prefix
*)&rn
->p
, &vpn
->prd
);
770 /* Delete route entry in the global EVPN table. */
771 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
773 /* Schedule for processing - withdraws to peers happen from
777 bgp_process(bgp
, global_rn
, afi
, safi
);
778 bgp_unlock_node(global_rn
);
781 /* Delete route entry in the VNI route table, caller to remove. */
782 bgp_info_delete(rn
, old_local
);
786 * Calculate the best path for an EVPN route. Install/update best path in zebra,
789 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
792 struct bgp_info
*old_select
, *new_select
;
793 struct bgp_info_pair old_and_new
;
794 afi_t afi
= AFI_L2VPN
;
795 safi_t safi
= SAFI_EVPN
;
798 /* Compute the best path. */
799 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
801 old_select
= old_and_new
.old
;
802 new_select
= old_and_new
.new;
804 /* If the best path hasn't changed - see if there is still something to
808 if (old_select
&& old_select
== new_select
809 && old_select
->type
== ZEBRA_ROUTE_BGP
810 && old_select
->sub_type
== BGP_ROUTE_NORMAL
811 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
812 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
813 && !bgp
->addpath_tx_used
[afi
][safi
]) {
814 if (bgp_zebra_has_route_changed(rn
, old_select
))
815 ret
= evpn_zebra_install(bgp
, vpn
,
816 (struct prefix_evpn
*)&rn
->p
,
817 old_select
->attr
->nexthop
,
818 old_select
->attr
->sticky
);
819 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
820 bgp_zebra_clear_route_change_flags(rn
);
824 /* If the user did a "clear" this flag will be set */
825 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
827 /* bestpath has changed; update relevant fields and install or uninstall
828 * into the zebra RIB.
830 if (old_select
|| new_select
)
831 bgp_bump_version(rn
);
834 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
836 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
837 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
838 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
841 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
842 && new_select
->sub_type
== BGP_ROUTE_NORMAL
) {
843 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
844 new_select
->attr
->nexthop
,
845 new_select
->attr
->sticky
);
846 /* If an old best existed and it was a "local" route, the only
848 * it would be supplanted is due to MAC mobility procedures. So,
850 * need to do an implicit delete and withdraw that route from
853 if (old_select
&& old_select
->peer
== bgp
->peer_self
854 && old_select
->type
== ZEBRA_ROUTE_BGP
855 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
856 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
858 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
859 && old_select
->sub_type
== BGP_ROUTE_NORMAL
)
860 ret
= evpn_zebra_uninstall(bgp
, vpn
,
861 (struct prefix_evpn
*)&rn
->p
,
862 old_select
->attr
->nexthop
);
865 /* Clear any route change flags. */
866 bgp_zebra_clear_route_change_flags(rn
);
868 /* Reap old select bgp_info, if it has been removed */
869 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
870 bgp_info_reap(rn
, old_select
);
877 * Return true if the local ri for this rn has sticky set
879 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
881 struct bgp_info
*tmp_ri
;
882 struct bgp_info
*local_ri
;
885 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
886 if (tmp_ri
->peer
== bgp
->peer_self
887 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
888 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
895 return local_ri
->attr
->sticky
;
899 * Create or update EVPN route entry. This could be in the VNI route table
900 * or the global route table.
902 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
903 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
904 struct attr
*attr
, int add
, int vni_table
,
905 struct bgp_info
**ri
, u_char flags
)
907 struct bgp_info
*tmp_ri
;
908 struct bgp_info
*local_ri
, *remote_ri
;
909 struct attr
*attr_new
;
910 mpls_label_t label
= MPLS_INVALID_LABEL
;
911 int route_change
= 1;
916 /* See if this is an update of an existing route, or a new add. Also,
917 * identify if already known from remote, and if so, the one with the
918 * highest sequence number; this is only when adding to the VNI routing
921 local_ri
= remote_ri
= NULL
;
922 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
923 if (tmp_ri
->peer
== bgp
->peer_self
924 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
925 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
928 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
929 && tmp_ri
->sub_type
== BGP_ROUTE_NORMAL
930 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
933 else if (mac_mobility_seqnum(tmp_ri
->attr
)
934 > mac_mobility_seqnum(remote_ri
->attr
))
940 /* If route doesn't exist already, create a new one, if told to.
941 * Otherwise act based on whether the attributes of the route have
944 if (!local_ri
&& !add
)
948 /* When learnt locally for the first time but already known from
949 * remote, we have to initiate appropriate MAC mobility steps.
951 * is applicable when updating the VNI routing table.
952 * We need to skip mobility steps for g/w macs (local mac on g/w
953 * SVI) advertised in EVPN.
954 * This will ensure that local routes are preferred for g/w macs
956 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MAC_TYPE_GW
)) {
957 u_int32_t cur_seqnum
;
959 /* Add MM extended community to route. */
960 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
961 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
964 /* Add (or update) attribute to hash. */
965 attr_new
= bgp_attr_intern(attr
);
967 /* Extract MAC mobility sequence number, if any. */
968 attr_new
->mm_seqnum
=
969 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
970 attr_new
->sticky
= sticky
;
972 /* Create new route with its attribute. */
973 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
974 bgp
->peer_self
, attr_new
, rn
);
975 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
976 bgp_info_extra_get(tmp_ri
);
978 /* The VNI goes into the 'label' field of the route */
979 vni2label(vpn
->vni
, &label
);
981 memcpy(&tmp_ri
->extra
->label
, &label
, BGP_LABEL_BYTES
);
982 bgp_info_add(rn
, tmp_ri
);
985 if (attrhash_cmp(tmp_ri
->attr
, attr
)
986 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
989 /* The attribute has changed. */
990 /* Add (or update) attribute to hash. */
991 attr_new
= bgp_attr_intern(attr
);
992 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
994 /* Restore route, if needed. */
995 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
996 bgp_info_restore(rn
, tmp_ri
);
998 /* Unintern existing, set to new. */
999 bgp_attr_unintern(&tmp_ri
->attr
);
1000 tmp_ri
->attr
= attr_new
;
1001 tmp_ri
->uptime
= bgp_clock();
1005 /* Return back the route entry. */
1007 return route_change
;
1011 * Create or update EVPN route (of type based on prefix) for specified VNI
1012 * and schedule for processing.
1014 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1015 struct prefix_evpn
*p
, u_char flags
)
1017 struct bgp_node
*rn
;
1019 struct attr
*attr_new
;
1020 struct bgp_info
*ri
;
1021 afi_t afi
= AFI_L2VPN
;
1022 safi_t safi
= SAFI_EVPN
;
1025 memset(&attr
, 0, sizeof(struct attr
));
1027 /* Build path-attribute for this route. */
1028 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1029 attr
.nexthop
= vpn
->originator_ip
;
1030 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1031 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1032 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MAC_TYPE_STICKY
) ? 1 : 0;
1033 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1035 /* Set up RT and ENCAP extended community. */
1036 build_evpn_route_extcomm(vpn
, &attr
);
1038 /* First, create (or fetch) route node within the VNI. */
1039 /* NOTE: There is no RD here. */
1040 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1042 /* Create or update route entry. */
1043 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1046 attr_new
= ri
->attr
;
1048 /* Perform route selection; this is just to set the flags correctly
1049 * as local route in the VNI always wins.
1051 evpn_route_select_install(bgp
, vpn
, rn
);
1052 bgp_unlock_node(rn
);
1054 /* If this is a new route or some attribute has changed, export the
1055 * route to the global table. The route will be advertised to peers
1056 * from there. Note that this table is a 2-level tree (RD-level +
1057 * Prefix-level) similar to L3VPN routes.
1060 struct bgp_info
*global_ri
;
1062 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1063 (struct prefix
*)p
, &vpn
->prd
);
1064 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1067 /* Schedule for processing and unlock node. */
1068 bgp_process(bgp
, rn
, afi
, safi
);
1069 bgp_unlock_node(rn
);
1072 /* Unintern temporary. */
1073 aspath_unintern(&attr
.aspath
);
1079 * Delete EVPN route entry. This could be in the VNI route table
1080 * or the global route table.
1082 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1083 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1084 struct bgp_info
**ri
)
1086 struct bgp_info
*tmp_ri
;
1090 /* Now, find matching route. */
1091 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1092 if (tmp_ri
->peer
== bgp
->peer_self
1093 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1094 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1099 /* Mark route for delete. */
1101 bgp_info_delete(rn
, tmp_ri
);
1105 * Delete EVPN route (of type based on prefix) for specified VNI and
1106 * schedule for processing.
1108 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1109 struct prefix_evpn
*p
)
1111 struct bgp_node
*rn
, *global_rn
;
1112 struct bgp_info
*ri
;
1113 afi_t afi
= AFI_L2VPN
;
1114 safi_t safi
= SAFI_EVPN
;
1116 /* First, locate the route node within the VNI. If it doesn't exist,
1118 * is nothing further to do.
1120 /* NOTE: There is no RD here. */
1121 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1125 /* Next, locate route node in the global EVPN routing table. Note that
1126 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1129 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1130 (struct prefix
*)p
, &vpn
->prd
);
1132 /* Delete route entry in the global EVPN table. */
1133 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
1135 /* Schedule for processing - withdraws to peers happen from
1139 bgp_process(bgp
, global_rn
, afi
, safi
);
1140 bgp_unlock_node(global_rn
);
1143 /* Delete route entry in the VNI route table. This can just be removed.
1145 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1147 bgp_info_reap(rn
, ri
);
1148 bgp_unlock_node(rn
);
1154 * Update all type-2 (MACIP) local routes for this VNI - these should also
1155 * be scheduled for advertise to peers.
1157 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1161 struct bgp_node
*rn
;
1162 struct bgp_info
*ri
;
1164 struct attr attr_sticky
;
1165 struct attr
*attr_new
;
1169 memset(&attr
, 0, sizeof(struct attr
));
1170 memset(&attr_sticky
, 0, sizeof(struct attr
));
1172 /* Build path-attribute - all type-2 routes for this VNI will share the
1173 * same path attribute.
1175 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1176 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1177 attr
.nexthop
= vpn
->originator_ip
;
1178 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1179 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1180 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1181 attr_sticky
.nexthop
= vpn
->originator_ip
;
1182 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1183 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1184 attr_sticky
.sticky
= 1;
1185 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1187 /* Set up RT, ENCAP and sticky MAC extended community. */
1188 build_evpn_route_extcomm(vpn
, &attr
);
1189 build_evpn_route_extcomm(vpn
, &attr_sticky
);
1191 /* Walk this VNI's route table and update local type-2 routes. For any
1192 * routes updated, update corresponding entry in the global table too.
1194 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1195 rn
= bgp_route_next(rn
)) {
1196 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1197 struct bgp_node
*rd_rn
;
1198 struct bgp_info
*global_ri
;
1200 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1203 if (evpn_route_is_sticky(bgp
, rn
))
1204 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1205 &attr_sticky
, 0, 1, &ri
, 0);
1207 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1210 /* If a local route exists for this prefix, we need to update
1211 * the global routing table too.
1216 /* Perform route selection; this is just to set the flags
1218 * as local route in the VNI always wins.
1220 evpn_route_select_install(bgp
, vpn
, rn
);
1222 attr_new
= ri
->attr
;
1224 /* Update route in global routing table. */
1225 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1226 (struct prefix
*)evp
, &vpn
->prd
);
1228 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
1231 /* Schedule for processing and unlock node. */
1232 bgp_process(bgp
, rd_rn
, afi
, safi
);
1233 bgp_unlock_node(rd_rn
);
1236 /* Unintern temporary. */
1237 aspath_unintern(&attr
.aspath
);
1238 aspath_unintern(&attr_sticky
.aspath
);
1244 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1245 * global routing table. These are also scheduled for withdraw from peers.
1247 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1251 struct bgp_node
*rdrn
, *rn
;
1252 struct bgp_table
*table
;
1253 struct bgp_info
*ri
;
1258 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
1259 if (rdrn
&& rdrn
->info
) {
1260 table
= (struct bgp_table
*)rdrn
->info
;
1261 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1262 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1264 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1267 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1269 bgp_process(bgp
, rn
, afi
, safi
);
1273 /* Unlock RD node. */
1275 bgp_unlock_node(rdrn
);
1281 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1282 * table as well as the per-VNI route table.
1284 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1288 struct bgp_node
*rn
;
1289 struct bgp_info
*ri
;
1294 /* First, walk the global route table for this VNI's type-2 local
1296 * EVPN routes are a 2-level table, first get the RD table.
1298 delete_global_type2_routes(bgp
, vpn
);
1300 /* Next, walk this VNI's route table and delete local type-2 routes. */
1301 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1302 rn
= bgp_route_next(rn
)) {
1303 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1305 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1308 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1310 /* Route entry in local table gets deleted immediately. */
1312 bgp_info_reap(rn
, ri
);
1319 * Delete all routes in the per-VNI route table.
1321 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1323 struct bgp_node
*rn
;
1324 struct bgp_info
*ri
, *nextri
;
1326 /* Walk this VNI's route table and delete all routes. */
1327 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1328 rn
= bgp_route_next(rn
)) {
1329 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
1331 bgp_info_delete(rn
, ri
);
1332 bgp_info_reap(rn
, ri
);
1340 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1341 * export RT getting modified or change to tunnel IP. Note that these
1342 * situations need the route in the per-VNI table as well as the global
1343 * table to be updated (as attributes change).
1345 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1348 struct prefix_evpn p
;
1350 /* Update and advertise the type-3 route (only one) followed by the
1351 * locally learnt type-2 routes (MACIP) - for this VNI.
1353 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1354 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
1358 return update_all_type2_routes(bgp
, vpn
);
1362 * Delete (and withdraw) local routes for specified VNI from the global
1363 * table and per-VNI table. After this, remove all other routes from
1364 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1365 * (advertise-all-vni) being disabled.
1367 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1370 struct prefix_evpn p
;
1372 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1373 * followed by type-3 routes (only one) - for this VNI.
1375 ret
= delete_all_type2_routes(bgp
, vpn
);
1379 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1380 ret
= delete_evpn_route(bgp
, vpn
, &p
);
1384 /* Delete all routes from the per-VNI table. */
1385 return delete_all_vni_routes(bgp
, vpn
);
1389 * There is a tunnel endpoint IP address change for this VNI,
1390 * need to re-advertise routes with the new nexthop.
1392 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1393 struct in_addr originator_ip
)
1395 struct prefix_evpn p
;
1397 /* If VNI is not live, we only need to update the originator ip */
1398 if (!is_vni_live(vpn
)) {
1399 vpn
->originator_ip
= originator_ip
;
1403 /* Update the tunnel-ip hash */
1404 bgp_tip_del(bgp
, &vpn
->originator_ip
);
1405 bgp_tip_add(bgp
, &originator_ip
);
1407 /* filter routes as martian nexthop db has changed */
1408 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
1410 /* Need to withdraw type-3 route as the originator IP is part
1413 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1414 delete_evpn_route(bgp
, vpn
, &p
);
1416 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1417 vpn
->originator_ip
= originator_ip
;
1418 return update_routes_for_vni(bgp
, vpn
);
1422 * Install route entry into the VRF routing table and invoke route selection.
1424 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1425 struct prefix_evpn
*evp
,
1426 struct bgp_info
*parent_ri
)
1428 struct bgp_node
*rn
;
1429 struct bgp_info
*ri
;
1430 struct attr
*attr_new
;
1433 struct prefix
*pp
= &p
;
1436 char buf
[PREFIX_STRLEN
];
1437 char buf1
[PREFIX_STRLEN
];
1439 memset(pp
, 0, sizeof(struct prefix
));
1440 ip_prefix_from_type2_prefix(evp
, pp
);
1442 if (bgp_debug_zebra(NULL
)) {
1443 zlog_debug("installing evpn prefix %s as ip prefix %s in vrf %s",
1444 prefix2str(evp
, buf
, sizeof(buf
)),
1445 prefix2str(pp
, buf1
, sizeof(buf
)),
1446 vrf_id_to_name(bgp_vrf
->vrf_id
));
1449 /* Create (or fetch) route within the VRF. */
1450 /* NOTE: There is no RD here. */
1451 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1453 safi
= SAFI_UNICAST
;
1454 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1455 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp
)) {
1457 safi
= SAFI_UNICAST
;
1458 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1462 /* Check if route entry is already present. */
1463 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1465 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1469 /* Add (or update) attribute to hash. */
1470 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1472 /* Create new route with its attribute. */
1473 ri
= info_make(parent_ri
->type
, parent_ri
->sub_type
, 0,
1474 parent_ri
->peer
, attr_new
, rn
);
1475 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1476 bgp_info_extra_get(ri
);
1477 ri
->extra
->parent
= parent_ri
;
1478 if (parent_ri
->extra
)
1479 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1481 bgp_info_add(rn
, ri
);
1483 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1484 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1485 bgp_unlock_node(rn
);
1488 /* The attribute has changed. */
1489 /* Add (or update) attribute to hash. */
1490 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1492 /* Restore route, if needed. */
1493 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1494 bgp_info_restore(rn
, ri
);
1496 /* Mark if nexthop has changed. */
1497 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1498 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1500 /* Unintern existing, set to new. */
1501 bgp_attr_unintern(&ri
->attr
);
1502 ri
->attr
= attr_new
;
1503 ri
->uptime
= bgp_clock();
1506 /* Perform route selection and update zebra, if required. */
1507 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1513 * Install route entry into the VNI routing table and invoke route selection.
1515 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1516 struct prefix_evpn
*p
,
1517 struct bgp_info
*parent_ri
)
1519 struct bgp_node
*rn
;
1520 struct bgp_info
*ri
;
1521 struct attr
*attr_new
;
1524 /* Create (or fetch) route within the VNI. */
1525 /* NOTE: There is no RD here. */
1526 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1528 /* Check if route entry is already present. */
1529 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1531 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1535 /* Add (or update) attribute to hash. */
1536 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1538 /* Create new route with its attribute. */
1539 ri
= info_make(parent_ri
->type
, parent_ri
->sub_type
, 0,
1540 parent_ri
->peer
, attr_new
, rn
);
1541 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1542 bgp_info_extra_get(ri
);
1543 ri
->extra
->parent
= parent_ri
;
1544 if (parent_ri
->extra
)
1545 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1547 bgp_info_add(rn
, ri
);
1549 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1550 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1551 bgp_unlock_node(rn
);
1554 /* The attribute has changed. */
1555 /* Add (or update) attribute to hash. */
1556 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1558 /* Restore route, if needed. */
1559 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1560 bgp_info_restore(rn
, ri
);
1562 /* Mark if nexthop has changed. */
1563 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1564 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1566 /* Unintern existing, set to new. */
1567 bgp_attr_unintern(&ri
->attr
);
1568 ri
->attr
= attr_new
;
1569 ri
->uptime
= bgp_clock();
1572 /* Perform route selection and update zebra, if required. */
1573 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
1579 * Uninstall route entry from the VRF routing table and send message
1580 * to zebra, if appropriate.
1582 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1583 struct prefix_evpn
*evp
,
1584 struct bgp_info
*parent_ri
)
1586 struct bgp_node
*rn
;
1587 struct bgp_info
*ri
;
1590 struct prefix
*pp
= &p
;
1593 char buf
[PREFIX_STRLEN
];
1594 char buf1
[PREFIX_STRLEN
];
1596 memset(pp
, 0, sizeof(struct prefix
));
1597 ip_prefix_from_type2_prefix(evp
, pp
);
1599 if (bgp_debug_zebra(NULL
)) {
1600 zlog_debug("uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1601 prefix2str(evp
, buf
, sizeof(buf
)),
1602 prefix2str(pp
, buf1
, sizeof(buf
)),
1603 vrf_id_to_name(bgp_vrf
->vrf_id
));
1606 /* Locate route within the VRF. */
1607 /* NOTE: There is no RD here. */
1608 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1610 safi
= SAFI_UNICAST
;
1611 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
1614 safi
= SAFI_UNICAST
;
1615 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
1621 /* Find matching route entry. */
1622 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1624 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1630 /* Mark entry for deletion */
1631 bgp_info_delete(rn
, ri
);
1633 /* Perform route selection and update zebra, if required. */
1634 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1636 /* Unlock route node. */
1637 bgp_unlock_node(rn
);
1643 * Uninstall route entry from the VNI routing table and send message
1644 * to zebra, if appropriate.
1646 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1647 struct prefix_evpn
*p
,
1648 struct bgp_info
*parent_ri
)
1650 struct bgp_node
*rn
;
1651 struct bgp_info
*ri
;
1654 /* Locate route within the VNI. */
1655 /* NOTE: There is no RD here. */
1656 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1660 /* Find matching route entry. */
1661 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1663 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1669 /* Mark entry for deletion */
1670 bgp_info_delete(rn
, ri
);
1672 /* Perform route selection and update zebra, if required. */
1673 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
1675 /* Unlock route node. */
1676 bgp_unlock_node(rn
);
1682 * Given a route entry and a VRF, see if this route entry should be
1683 * imported into the VRF i.e., RTs match.
1685 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
1686 struct bgp_info
*ri
)
1688 struct attr
*attr
= ri
->attr
;
1689 struct ecommunity
*ecom
;
1693 /* Route should have valid RT to be even considered. */
1694 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
1697 ecom
= attr
->ecommunity
;
1698 if (!ecom
|| !ecom
->size
)
1701 /* For each extended community RT, see if it matches this VNI. If any RT
1702 * matches, we're done.
1704 for (i
= 0; i
< ecom
->size
; i
++) {
1706 u_char type
, sub_type
;
1707 struct ecommunity_val
*eval
;
1708 struct ecommunity_val eval_tmp
;
1709 struct vrf_irt_node
*irt
;
1711 /* Only deal with RTs */
1712 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
1713 eval
= (struct ecommunity_val
*)(ecom
->val
1714 + (i
* ECOMMUNITY_SIZE
));
1717 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
1720 /* See if this RT matches specified VNIs import RTs */
1721 irt
= lookup_vrf_import_rt(eval
);
1722 if (irt
&& irt
->vrfs
)
1723 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
1726 /* Also check for non-exact match. In this, we mask out the AS
1728 * only check on the local-admin sub-field. This is to
1730 * VNI as the RT for EBGP peering too.
1733 if (type
== ECOMMUNITY_ENCODE_AS
1734 || type
== ECOMMUNITY_ENCODE_AS4
1735 || type
== ECOMMUNITY_ENCODE_IP
) {
1736 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
1737 mask_ecom_global_admin(&eval_tmp
, eval
);
1738 irt
= lookup_vrf_import_rt(&eval_tmp
);
1740 if (irt
&& irt
->vrfs
)
1741 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
1749 * Given a route entry and a VNI, see if this route entry should be
1750 * imported into the VNI i.e., RTs match.
1752 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1753 struct bgp_info
*ri
)
1755 struct attr
*attr
= ri
->attr
;
1756 struct ecommunity
*ecom
;
1760 /* Route should have valid RT to be even considered. */
1761 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
1764 ecom
= attr
->ecommunity
;
1765 if (!ecom
|| !ecom
->size
)
1768 /* For each extended community RT, see if it matches this VNI. If any RT
1769 * matches, we're done.
1771 for (i
= 0; i
< ecom
->size
; i
++) {
1773 u_char type
, sub_type
;
1774 struct ecommunity_val
*eval
;
1775 struct ecommunity_val eval_tmp
;
1776 struct irt_node
*irt
;
1778 /* Only deal with RTs */
1779 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
1780 eval
= (struct ecommunity_val
*)(ecom
->val
1781 + (i
* ECOMMUNITY_SIZE
));
1784 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
1787 /* See if this RT matches specified VNIs import RTs */
1788 irt
= lookup_import_rt(bgp
, eval
);
1789 if (irt
&& irt
->vnis
)
1790 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
1793 /* Also check for non-exact match. In this, we mask out the AS
1795 * only check on the local-admin sub-field. This is to
1797 * VNI as the RT for EBGP peering too.
1800 if (type
== ECOMMUNITY_ENCODE_AS
1801 || type
== ECOMMUNITY_ENCODE_AS4
1802 || type
== ECOMMUNITY_ENCODE_IP
) {
1803 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
1804 mask_ecom_global_admin(&eval_tmp
, eval
);
1805 irt
= lookup_import_rt(bgp
, &eval_tmp
);
1807 if (irt
&& irt
->vnis
)
1808 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
1816 * Install or uninstall mac-ip routes are appropriate for this
1819 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
,
1824 struct bgp_node
*rd_rn
, *rn
;
1825 struct bgp_table
*table
;
1826 struct bgp_info
*ri
;
1828 char buf
[PREFIX_STRLEN
];
1829 struct bgp
*bgp_def
= NULL
;
1833 bgp_def
= bgp_get_default();
1837 /* Walk entire global routing table and evaluate routes which could be
1838 * imported into this VRF. Note that we need to loop through all global
1839 * routes to determine which route matches the import rt on vrf
1841 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
1842 rd_rn
= bgp_route_next(rd_rn
)) {
1843 table
= (struct bgp_table
*)(rd_rn
->info
);
1847 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1848 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1850 /* if not mac-ip route skip this route */
1851 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1854 /* if not a mac+ip route skip this route */
1855 if (!(IS_EVPN_PREFIX_IPADDR_V4(evp
) ||
1856 IS_EVPN_PREFIX_IPADDR_V6(evp
)))
1859 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1860 /* Consider "valid" remote routes applicable for
1862 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
1863 && ri
->type
== ZEBRA_ROUTE_BGP
1864 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
1867 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
1870 install_evpn_route_entry_in_vrf(
1874 uninstall_evpn_route_entry_in_vrf(
1879 "Failed to %s EVPN %s route in VRF %s",
1882 prefix2str(evp
, buf
,
1884 vrf_id_to_name(bgp_vrf
->vrf_id
));
1896 * Install or uninstall routes of specified type that are appropriate for this
1899 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
1900 struct bgpevpn
*vpn
,
1901 bgp_evpn_route_type rtype
,
1906 struct bgp_node
*rd_rn
, *rn
;
1907 struct bgp_table
*table
;
1908 struct bgp_info
*ri
;
1914 /* Walk entire global routing table and evaluate routes which could be
1915 * imported into this VPN. Note that we cannot just look at the routes
1917 * the VNI's RD - remote routes applicable for this VNI could have any
1920 /* EVPN routes are a 2-level table. */
1921 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
1922 rd_rn
= bgp_route_next(rd_rn
)) {
1923 table
= (struct bgp_table
*)(rd_rn
->info
);
1927 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1928 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1930 if (evp
->prefix
.route_type
!= rtype
)
1933 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1934 /* Consider "valid" remote routes applicable for
1936 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
1937 && ri
->type
== ZEBRA_ROUTE_BGP
1938 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
1941 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
1943 ret
= install_evpn_route_entry(
1946 ret
= uninstall_evpn_route_entry(
1951 "%u: Failed to %s EVPN %s route in VNI %u",
1955 rtype
== BGP_EVPN_MAC_IP_ROUTE
1969 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
1970 * is invoked upon l3vni-add or l3vni import rt change */
1971 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
1973 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
1978 * Install any existing remote routes applicable for this VNI into its
1979 * routing table. This is invoked when a VNI becomes "live" or its Import
1982 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1986 /* Install type-3 routes followed by type-2 routes - the ones applicable
1989 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
1994 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
1998 /* uninstall routes from l3vni vrf. */
1999 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
2001 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
2006 * Uninstall any existing remote routes for this VNI. One scenario in which
2007 * this is invoked is upon an import RT change.
2009 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2013 /* Uninstall type-2 routes followed by type-3 routes - the ones
2017 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2022 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2027 * Install or uninstall route in matching VRFs (list).
2029 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
2030 safi_t safi
, struct prefix_evpn
*evp
,
2031 struct bgp_info
*ri
,
2032 struct list
*vrfs
, int install
)
2034 char buf
[PREFIX2STR_BUFFER
];
2035 struct bgp
*bgp_vrf
;
2036 struct listnode
*node
, *nnode
;
2038 /* Only type-2 routes go into a VRF */
2039 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
))
2042 /* if not a mac+ip route skip this route */
2043 if (!(IS_EVPN_PREFIX_IPADDR_V4(evp
) ||
2044 IS_EVPN_PREFIX_IPADDR_V6(evp
)))
2047 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
2051 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
,
2054 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
,
2058 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2060 install
? "install" : "uninstall",
2061 prefix2str(evp
, buf
, sizeof(buf
)),
2062 vrf_id_to_name(bgp_vrf
->vrf_id
));
2071 * Install or uninstall route in matching VNIs (list).
2073 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
2074 safi_t safi
, struct prefix_evpn
*evp
,
2075 struct bgp_info
*ri
,
2076 struct list
*vnis
, int install
)
2078 struct bgpevpn
*vpn
;
2079 struct listnode
*node
, *nnode
;
2081 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
2084 if (!is_vni_live(vpn
))
2088 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2090 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2093 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2094 bgp
->vrf_id
, install
? "install" : "uninstall",
2095 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2107 * Install or uninstall route for appropriate VNIs.
2109 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2110 struct prefix
*p
, struct bgp_info
*ri
,
2113 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
2114 struct attr
*attr
= ri
->attr
;
2115 struct ecommunity
*ecom
;
2120 /* Only type-2 and type-3 routes go into a L2 VNI. */
2121 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2122 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
))
2125 /* If we don't have Route Target, nothing much to do. */
2126 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2129 ecom
= attr
->ecommunity
;
2130 if (!ecom
|| !ecom
->size
)
2133 /* For each extended community RT, see which VNIs match and import
2134 * the route into matching VNIs.
2136 for (i
= 0; i
< ecom
->size
; i
++) {
2138 u_char type
, sub_type
;
2139 struct ecommunity_val
*eval
;
2140 struct ecommunity_val eval_tmp
;
2141 struct irt_node
*irt
; /* import rt for l2vni */
2142 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
2144 /* Only deal with RTs */
2145 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2146 eval
= (struct ecommunity_val
*)(ecom
->val
2147 + (i
* ECOMMUNITY_SIZE
));
2150 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2153 /* Import route into matching l2-vnis */
2154 irt
= lookup_import_rt(bgp
, eval
);
2155 if (irt
&& irt
->vnis
)
2156 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2159 /* Import route into matching l3-vnis (vrfs) */
2160 vrf_irt
= lookup_vrf_import_rt(eval
);
2161 if (vrf_irt
&& vrf_irt
->vrfs
)
2162 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2163 vrf_irt
->vrfs
, import
);
2165 /* Also check for non-exact match. In this, we mask out the AS
2167 * only check on the local-admin sub-field. This is to
2169 * VNI as the RT for EBGP peering too.
2173 if (type
== ECOMMUNITY_ENCODE_AS
2174 || type
== ECOMMUNITY_ENCODE_AS4
2175 || type
== ECOMMUNITY_ENCODE_IP
) {
2176 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2177 mask_ecom_global_admin(&eval_tmp
, eval
);
2178 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2179 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
2181 if (irt
&& irt
->vnis
)
2182 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2184 if (vrf_irt
&& vrf_irt
->vrfs
)
2185 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
,
2194 * Update and advertise local routes for a VNI. Invoked upon router-id
2195 * change. Note that the processing is done only on the global route table
2196 * using routes that already exist in the per-VNI table.
2198 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2200 struct prefix_evpn p
;
2201 struct bgp_node
*rn
, *global_rn
;
2202 struct bgp_info
*ri
, *global_ri
;
2204 afi_t afi
= AFI_L2VPN
;
2205 safi_t safi
= SAFI_EVPN
;
2207 /* Locate type-3 route for VNI in the per-VNI table and use its
2208 * attributes to create and advertise the type-3 route for this VNI
2209 * in the global table.
2211 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2212 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
2213 if (!rn
) /* unexpected */
2215 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2216 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
2217 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2219 if (!ri
) /* unexpected */
2223 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2224 (struct prefix
*)&p
, &vpn
->prd
);
2225 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
2228 /* Schedule for processing and unlock node. */
2229 bgp_process(bgp
, global_rn
, afi
, safi
);
2230 bgp_unlock_node(global_rn
);
2232 /* Now, walk this VNI's route table and use the route and its attribute
2233 * to create and schedule route in global table.
2235 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2236 rn
= bgp_route_next(rn
)) {
2237 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2239 /* Identify MAC-IP local routes. */
2240 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2243 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2244 if (ri
->peer
== bgp
->peer_self
2245 && ri
->type
== ZEBRA_ROUTE_BGP
2246 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2251 /* Create route in global routing table using this route entry's
2255 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2256 (struct prefix
*)evp
, &vpn
->prd
);
2258 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
2261 /* Schedule for processing and unlock node. */
2262 bgp_process(bgp
, global_rn
, afi
, safi
);
2263 bgp_unlock_node(global_rn
);
2270 * Delete (and withdraw) local routes for a VNI - only from the global
2271 * table. Invoked upon router-id change.
2273 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2276 struct prefix_evpn p
;
2277 struct bgp_node
*global_rn
;
2278 struct bgp_info
*ri
;
2279 afi_t afi
= AFI_L2VPN
;
2280 safi_t safi
= SAFI_EVPN
;
2282 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2283 * for this VNI - from the global table.
2285 ret
= delete_global_type2_routes(bgp
, vpn
);
2289 /* Remove type-3 route for this VNI from global table. */
2290 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2291 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2292 (struct prefix
*)&p
, &vpn
->prd
);
2294 /* Delete route entry in the global EVPN table. */
2295 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
2297 /* Schedule for processing - withdraws to peers happen from
2301 bgp_process(bgp
, global_rn
, afi
, safi
);
2302 bgp_unlock_node(global_rn
);
2309 * Handle router-id change. Update and advertise local routes corresponding
2310 * to this VNI from peers. Note that this is invoked after updating the
2311 * router-id. The routes in the per-VNI table are used to create routes in
2312 * the global table and schedule them.
2314 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2316 struct bgpevpn
*vpn
;
2318 vpn
= (struct bgpevpn
*)backet
->data
;
2321 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2325 /* Skip VNIs with configured RD. */
2326 if (is_rd_configured(vpn
))
2329 bgp_evpn_derive_auto_rd(bgp
, vpn
);
2330 update_advertise_vni_routes(bgp
, vpn
);
2334 * Handle router-id change. Delete and withdraw local routes corresponding
2335 * to this VNI from peers. Note that this is invoked prior to updating
2336 * the router-id and is done only on the global route table, the routes
2337 * are needed in the per-VNI table to re-advertise with new router id.
2339 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2341 struct bgpevpn
*vpn
;
2343 vpn
= (struct bgpevpn
*)backet
->data
;
2346 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2350 /* Skip VNIs with configured RD. */
2351 if (is_rd_configured(vpn
))
2354 delete_withdraw_vni_routes(bgp
, vpn
);
2358 * Process received EVPN type-2 route (advertise or withdraw).
2360 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2361 struct attr
*attr
, u_char
*pfx
, int psize
,
2362 u_int32_t addpath_id
)
2364 struct prefix_rd prd
;
2365 struct prefix_evpn p
;
2368 mpls_label_t
*label_pnt
;
2371 /* Type-2 route should be either 33, 37 or 49 bytes or an
2372 * additional 3 bytes if there is a second label (VNI):
2373 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2374 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2375 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2377 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
2378 && psize
!= 40 && psize
!= 52) {
2379 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2380 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2384 /* Make prefix_rd */
2385 prd
.family
= AF_UNSPEC
;
2387 memcpy(&prd
.val
, pfx
, 8);
2390 /* Make EVPN prefix. */
2391 memset(&p
, 0, sizeof(struct prefix_evpn
));
2393 p
.prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
2394 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
2396 /* Skip over Ethernet Seg Identifier for now. */
2399 /* Skip over Ethernet Tag for now. */
2402 /* Get the MAC Addr len */
2403 macaddr_len
= *pfx
++;
2405 /* Get the MAC Addr */
2406 if (macaddr_len
== (ETH_ALEN
* 8)) {
2407 memcpy(&p
.prefix
.mac
.octet
, pfx
, ETH_ALEN
);
2411 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2412 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
2418 ipaddr_len
= *pfx
++;
2419 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
2420 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
2422 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2423 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2428 ipaddr_len
/= 8; /* Convert to bytes. */
2429 p
.prefix
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
2432 memcpy(&p
.prefix
.ip
.ip
.addr
, pfx
, ipaddr_len
);
2436 /* Get the VNI (in MPLS label field). */
2437 /* Note: We ignore the second VNI, if any. */
2438 label_pnt
= (mpls_label_t
*)pfx
;
2440 /* Process the route. */
2442 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2443 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2444 &prd
, label_pnt
, 0, NULL
);
2446 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2447 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2448 &prd
, label_pnt
, NULL
);
2453 * Process received EVPN type-3 route (advertise or withdraw).
2455 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2456 struct attr
*attr
, u_char
*pfx
, int psize
,
2457 u_int32_t addpath_id
)
2459 struct prefix_rd prd
;
2460 struct prefix_evpn p
;
2464 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2465 * IP len (1) and IP (4 or 16).
2467 if (psize
!= 17 && psize
!= 29) {
2468 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2469 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2473 /* Make prefix_rd */
2474 prd
.family
= AF_UNSPEC
;
2476 memcpy(&prd
.val
, pfx
, 8);
2479 /* Make EVPN prefix. */
2480 memset(&p
, 0, sizeof(struct prefix_evpn
));
2482 p
.prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
2483 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
2485 /* Skip over Ethernet Tag for now. */
2489 ipaddr_len
= *pfx
++;
2490 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
2491 p
.prefix
.ip
.ipa_type
= IPADDR_V4
;
2492 memcpy(&p
.prefix
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
2495 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2496 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2500 /* Process the route. */
2502 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2503 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2504 &prd
, NULL
, 0, NULL
);
2506 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2507 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2513 * Process received EVPN type-5 route (advertise or withdraw).
2515 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2516 struct attr
*attr
, u_char
*pfx
, int psize
,
2517 u_int32_t addpath_id
, int withdraw
)
2519 struct prefix_rd prd
;
2520 struct prefix_evpn p
;
2521 struct bgp_route_evpn evpn
;
2524 mpls_label_t
*label_pnt
;
2527 /* Type-5 route should be 34 or 58 bytes:
2528 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
2529 * GW (4 or 16) and VNI (3).
2530 * Note that the IP and GW should both be IPv4 or both IPv6.
2532 if (psize
!= 34 && psize
!= 58) {
2533 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
2534 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2538 /* Make prefix_rd */
2539 prd
.family
= AF_UNSPEC
;
2541 memcpy(&prd
.val
, pfx
, 8);
2544 /* Make EVPN prefix. */
2545 memset(&p
, 0, sizeof(struct prefix_evpn
));
2547 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
2549 /* Additional information outside of prefix - ESI and GW IP */
2550 memset(&evpn
, 0, sizeof(evpn
));
2553 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
2556 /* Fetch Ethernet Tag. */
2557 memcpy(ð_tag
, pfx
, 4);
2558 p
.prefix
.eth_tag
= ntohl(eth_tag
);
2561 /* Fetch IP prefix length. */
2563 if (ippfx_len
> IPV6_MAX_BITLEN
) {
2565 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
2566 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
2569 p
.prefix
.ip_prefix_length
= ippfx_len
;
2571 /* Determine IPv4 or IPv6 prefix */
2572 /* Since the address and GW are from the same family, this just becomes
2573 * a simple check on the total size.
2576 SET_IPADDR_V4(&p
.prefix
.ip
);
2577 memcpy(&p
.prefix
.ip
.ipaddr_v4
, pfx
, 4);
2579 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
2581 p
.prefixlen
= PREFIX_LEN_ROUTE_TYPE_5_IPV4
;
2583 SET_IPADDR_V6(&p
.prefix
.ip
);
2584 memcpy(&p
.prefix
.ip
.ipaddr_v6
, pfx
, 16);
2586 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
2588 p
.prefixlen
= PREFIX_LEN_ROUTE_TYPE_5_IPV6
;
2591 label_pnt
= (mpls_label_t
*)pfx
;
2593 /* Process the route. */
2595 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2596 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2597 &prd
, label_pnt
, 0, &evpn
);
2599 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2600 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2601 &prd
, label_pnt
, &evpn
);
2606 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
2607 struct prefix_rd
*prd
, mpls_label_t
*label
,
2612 struct evpn_addr
*p_evpn_p
;
2614 memset(&temp
, 0, 16);
2615 if (p
->family
!= AF_EVPN
)
2617 p_evpn_p
= &(p
->u
.prefix_evpn
);
2619 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
2622 len
= 32; /* ipv6 */
2623 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
2624 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
2625 stream_put(s
, prd
->val
, 8);
2627 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
2629 stream_put(s
, &temp
, 10);
2630 stream_putl(s
, p_evpn_p
->eth_tag
);
2631 stream_putc(s
, p_evpn_p
->ip_prefix_length
);
2632 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
2633 stream_put_ipv4(s
, p_evpn_p
->ip
.ipaddr_v4
.s_addr
);
2635 stream_put(s
, &p_evpn_p
->ip
.ipaddr_v6
, 16);
2637 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
2639 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
2641 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
2643 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
2644 stream_put_ipv4(s
, 0);
2646 stream_put(s
, &temp
, 16);
2650 stream_put(s
, label
, 3);
2656 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
2658 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
2660 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
2662 /* Remove EVPN routes and schedule for processing. */
2663 delete_routes_for_vni(bgp
, vpn
);
2665 /* Clear "live" flag and see if hash needs to be freed. */
2666 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
2667 if (!is_vni_configured(vpn
))
2668 bgp_evpn_free(bgp
, vpn
);
2672 * Free a VNI entry; iterator function called during cleanup.
2674 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
2676 struct bgpevpn
*vpn
;
2678 vpn
= (struct bgpevpn
*)backet
->data
;
2679 delete_all_vni_routes(bgp
, vpn
);
2680 bgp_evpn_free(bgp
, vpn
);
2684 * Derive AUTO import RT for BGP VRF - L3VNI
2686 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
2688 struct bgp
*bgp_def
= NULL
;
2690 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
2691 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
2694 bgp_def
= bgp_get_default();
2697 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
2701 * Delete AUTO import RT from BGP VRF - L3VNI
2703 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
2705 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
2709 * Derive AUTO export RT for BGP VRF - L3VNI
2711 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
2713 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
2714 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
2718 * Delete AUTO export RT from BGP VRF - L3VNI
2720 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
2722 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
2725 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
2727 struct bgp
*bgp_def
= NULL
;
2728 struct listnode
*node
= NULL
;
2729 struct bgpevpn
*vpn
= NULL
;
2731 bgp_def
= bgp_get_default();
2735 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
2736 update_routes_for_vni(bgp_def
, vpn
);
2743 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
,
2746 struct listnode
*node
, *nnode
, *node_to_del
;
2747 struct ecommunity
*ecom
, *ecom_auto
;
2748 struct ecommunity_val eval
;
2750 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
2752 ecom_auto
= ecommunity_new();
2753 ecommunity_add_val(ecom_auto
, &eval
);
2756 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
2757 if (ecommunity_match(ecom
, ecom_auto
)) {
2758 ecommunity_free(&ecom
);
2764 list_delete_node(rtl
, node_to_del
);
2766 ecommunity_free(&ecom_auto
);
2769 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
2770 struct ecommunity
*ecomadd
)
2772 /* uninstall routes from vrf */
2773 uninstall_routes_for_vrf(bgp_vrf
);
2775 /* Cleanup the RT to VRF mapping */
2776 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
2778 /* Remove auto generated RT */
2779 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
2781 /* Add the newly configured RT to RT list */
2782 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
2783 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
2785 /* map VRF to its RTs */
2786 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
2788 /* install routes matching the new VRF */
2789 install_routes_for_vrf(bgp_vrf
);
2792 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
2793 struct ecommunity
*ecomdel
)
2795 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
2796 struct ecommunity
*ecom
= NULL
;
2798 /* uninstall routes from vrf */
2799 uninstall_routes_for_vrf(bgp_vrf
);
2801 /* Cleanup the RT to VRF mapping */
2802 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
2804 /* remove the RT from the RT list */
2805 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
2806 if (ecommunity_match(ecom
, ecomdel
)) {
2807 ecommunity_free(&ecom
);
2814 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
2816 /* fallback to auto import rt, if this was the last RT */
2817 if (list_isempty(bgp_vrf
->vrf_import_rtl
)) {
2818 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
2819 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
2822 /* map VRFs to its RTs */
2823 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
2825 /* install routes matching this new RT */
2826 install_routes_for_vrf(bgp_vrf
);
2829 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
2830 struct ecommunity
*ecomadd
)
2832 /* remove auto-generated RT */
2833 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
2835 /* Add the new RT to the RT list */
2836 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
2837 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
2839 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
2843 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
2844 struct ecommunity
*ecomdel
)
2846 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
2847 struct ecommunity
*ecom
= NULL
;
2849 /* Remove the RT from the RT list */
2850 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
2851 if (ecommunity_match(ecom
, ecomdel
)) {
2852 ecommunity_free(&ecom
);
2859 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
2861 /* fall back to auto-generated RT if this was the last RT */
2862 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
2863 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
2864 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
2867 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
2871 * Handle change to BGP router id. This is invoked twice by the change
2872 * handler, first before the router id has been changed and then after
2873 * the router id has been changed. The first invocation will result in
2874 * local routes for all VNIs being deleted and withdrawn and the next
2875 * will result in the routes being re-advertised.
2877 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
2880 hash_iterate(bgp
->vnihash
,
2881 (void (*)(struct hash_backet
*,
2882 void *))withdraw_router_id_vni
,
2885 hash_iterate(bgp
->vnihash
,
2886 (void (*)(struct hash_backet
*,
2887 void *))update_router_id_vni
,
2892 * Handle change to export RT - update and advertise local routes.
2894 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2896 return update_routes_for_vni(bgp
, vpn
);
2900 * Handle change to RD. This is invoked twice by the change handler,
2901 * first before the RD has been changed and then after the RD has
2902 * been changed. The first invocation will result in local routes
2903 * of this VNI being deleted and withdrawn and the next will result
2904 * in the routes being re-advertised.
2906 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2910 delete_withdraw_vni_routes(bgp
, vpn
);
2912 update_advertise_vni_routes(bgp
, vpn
);
2916 * Install routes for this VNI. Invoked upon change to Import RT.
2918 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2920 return install_routes_for_vni(bgp
, vpn
);
2924 * Uninstall all routes installed for this VNI. Invoked upon change
2927 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2929 return uninstall_routes_for_vni(bgp
, vpn
);
2933 * Function to display "tag" in route as a VNI.
2935 char *bgp_evpn_label2str(mpls_label_t
*label
, char *buf
, int len
)
2939 vni
= label2vni(label
);
2940 snprintf(buf
, len
, "%u", vni
);
2945 * Function to convert evpn route to json format.
2946 * NOTE: We don't use prefix2str as the output here is a bit different.
2948 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
2950 char buf1
[ETHER_ADDR_STRLEN
];
2951 char buf2
[PREFIX2STR_BUFFER
];
2956 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
2957 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
2958 json_object_int_add(json
, "ethTag", 0);
2959 json_object_int_add(json
, "ipLen",
2960 IS_EVPN_PREFIX_IPADDR_V4(p
)
2963 json_object_string_add(json
, "ip",
2964 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
2965 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
2966 if (IS_EVPN_PREFIX_IPADDR_NONE(p
)) {
2967 json_object_int_add(json
, "routeType",
2968 p
->prefix
.route_type
);
2969 json_object_int_add(
2971 0); /* TODO: we don't support esi yet */
2972 json_object_int_add(json
, "ethTag", 0);
2973 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
2974 json_object_string_add(json
, "mac",
2975 prefix_mac2str(&p
->prefix
.mac
,
2981 family
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? AF_INET
2984 json_object_int_add(json
, "routeType",
2985 p
->prefix
.route_type
);
2986 json_object_int_add(
2988 0); /* TODO: we don't support esi yet */
2989 json_object_int_add(json
, "ethTag", 0);
2990 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
2991 json_object_string_add(json
, "mac",
2992 prefix_mac2str(&p
->prefix
.mac
,
2995 json_object_int_add(json
, "ipLen",
2996 IS_EVPN_PREFIX_IPADDR_V4(p
)
2999 json_object_string_add(
3001 inet_ntop(family
, &p
->prefix
.ip
.ip
.addr
, buf2
,
3002 PREFIX2STR_BUFFER
));
3005 /* Currently, this is to cater to other AF_ETHERNET code. */
3010 * Function to convert evpn route to string.
3011 * NOTE: We don't use prefix2str as the output here is a bit different.
3013 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
3015 char buf1
[ETHER_ADDR_STRLEN
];
3016 char buf2
[PREFIX2STR_BUFFER
];
3018 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3019 snprintf(buf
, len
, "[%d]:[0]:[%d]:[%s]", p
->prefix
.route_type
,
3020 IS_EVPN_PREFIX_IPADDR_V4(p
) ? IPV4_MAX_BITLEN
3022 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
3023 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3024 if (IS_EVPN_PREFIX_IPADDR_NONE(p
))
3025 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]",
3026 p
->prefix
.route_type
, 8 * ETH_ALEN
,
3027 prefix_mac2str(&p
->prefix
.mac
, buf1
,
3032 family
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? AF_INET
3034 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
3035 p
->prefix
.route_type
, 8 * ETH_ALEN
,
3036 prefix_mac2str(&p
->prefix
.mac
, buf1
,
3038 family
== AF_INET
? IPV4_MAX_BITLEN
3040 inet_ntop(family
, &p
->prefix
.ip
.ip
.addr
, buf2
,
3041 PREFIX2STR_BUFFER
));
3044 /* For EVPN route types not supported yet. */
3045 snprintf(buf
, len
, "(unsupported route type %d)",
3046 p
->prefix
.route_type
);
3053 * Encode EVPN prefix in Update (MP_REACH)
3055 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
3056 struct prefix_rd
*prd
, mpls_label_t
*label
,
3057 struct attr
*attr
, int addpath_encode
,
3058 u_int32_t addpath_tx_id
)
3060 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3064 stream_putl(s
, addpath_tx_id
);
3067 stream_putc(s
, evp
->prefix
.route_type
);
3069 switch (evp
->prefix
.route_type
) {
3070 case BGP_EVPN_MAC_IP_ROUTE
:
3071 if (IS_EVPN_PREFIX_IPADDR_V4(evp
))
3072 ipa_len
= IPV4_MAX_BYTELEN
;
3073 else if (IS_EVPN_PREFIX_IPADDR_V6(evp
))
3074 ipa_len
= IPV6_MAX_BYTELEN
;
3075 stream_putc(s
, 33 + ipa_len
); // 1 VNI
3076 stream_put(s
, prd
->val
, 8); /* RD */
3077 stream_put(s
, 0, 10); /* ESI */
3078 stream_putl(s
, 0); /* Ethernet Tag ID */
3079 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
3080 stream_put(s
, evp
->prefix
.mac
.octet
, 6); /* Mac Addr */
3081 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
3083 stream_put(s
, &evp
->prefix
.ip
.ip
.addr
,
3085 stream_put(s
, label
,
3086 BGP_LABEL_BYTES
); /* VNI is contained in 'tag' */
3089 case BGP_EVPN_IMET_ROUTE
:
3090 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
3091 stream_put(s
, prd
->val
, 8); /* RD */
3092 stream_putl(s
, 0); /* Ethernet Tag ID */
3093 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
3094 /* Originating Router's IP Addr */
3095 stream_put_in_addr(s
, &evp
->prefix
.ip
.ipaddr_v4
);
3098 case BGP_EVPN_IP_PREFIX_ROUTE
:
3099 /* TODO: AddPath support. */
3100 evpn_mpattr_encode_type5(s
, p
, prd
, label
, attr
);
3108 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
3109 struct bgp_nlri
*packet
, int withdraw
)
3115 u_int32_t addpath_id
;
3116 int addpath_encoded
;
3122 /* Check peer status. */
3123 if (peer
->status
!= Established
) {
3124 zlog_err("%u:%s - EVPN update received in state %d",
3125 peer
->bgp
->vrf_id
, peer
->host
, peer
->status
);
3129 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3131 lim
= pnt
+ packet
->length
;
3133 safi
= packet
->safi
;
3137 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
3138 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
3139 PEER_CAP_ADDPATH_AF_TX_RCV
));
3141 for (; pnt
< lim
; pnt
+= psize
) {
3142 /* Clear prefix structure. */
3143 memset(&p
, 0, sizeof(struct prefix
));
3145 /* Deal with path-id if AddPath is supported. */
3146 if (addpath_encoded
) {
3147 /* When packet overflow occurs return immediately. */
3148 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
3151 addpath_id
= ntohl(*((uint32_t *)pnt
));
3152 pnt
+= BGP_ADDPATH_ID_LEN
;
3155 /* All EVPN NLRI types start with type and length. */
3160 psize
= rlen
= *pnt
++;
3162 /* When packet overflow occur return immediately. */
3163 if (pnt
+ psize
> lim
)
3167 case BGP_EVPN_MAC_IP_ROUTE
:
3168 if (process_type2_route(peer
, afi
, safi
,
3169 withdraw
? NULL
: attr
, pnt
,
3170 psize
, addpath_id
)) {
3172 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3173 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3178 case BGP_EVPN_IMET_ROUTE
:
3179 if (process_type3_route(peer
, afi
, safi
,
3180 withdraw
? NULL
: attr
, pnt
,
3181 psize
, addpath_id
)) {
3183 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3184 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3189 case BGP_EVPN_IP_PREFIX_ROUTE
:
3190 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
3191 psize
, addpath_id
, withdraw
)) {
3193 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3194 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3204 /* Packet length consistency check. */
3212 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3213 * The mapping will be used during route processing.
3214 * bgp_def: default bgp instance
3215 * bgp_vrf: specific bgp vrf instance on which RT is configured
3217 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
3220 struct ecommunity_val
*eval
= NULL
;
3221 struct listnode
*node
= NULL
, *nnode
= NULL
;
3222 struct ecommunity
*ecom
= NULL
;
3224 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3225 for (i
= 0; i
< ecom
->size
; i
++) {
3226 eval
= (struct ecommunity_val
*)(ecom
->val
3228 * ECOMMUNITY_SIZE
));
3229 map_vrf_to_rt(bgp_vrf
, eval
);
3235 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3237 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
3240 struct ecommunity_val
*eval
;
3241 struct listnode
*node
, *nnode
;
3242 struct ecommunity
*ecom
;
3244 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3245 for (i
= 0; i
< ecom
->size
; i
++) {
3246 struct vrf_irt_node
*irt
;
3247 struct ecommunity_val eval_tmp
;
3249 eval
= (struct ecommunity_val
*)(ecom
->val
3251 * ECOMMUNITY_SIZE
));
3252 /* If using "automatic" RT, we only care about the
3253 * local-admin sub-field.
3254 * This is to facilitate using VNI as the RT for EBGP
3257 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3258 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
3259 BGP_VRF_IMPORT_RT_CFGD
))
3260 mask_ecom_global_admin(&eval_tmp
, eval
);
3262 irt
= lookup_vrf_import_rt(&eval_tmp
);
3264 unmap_vrf_from_rt(bgp_vrf
, irt
);
3272 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3273 * The mapping will be used during route processing.
3275 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3278 struct ecommunity_val
*eval
;
3279 struct listnode
*node
, *nnode
;
3280 struct ecommunity
*ecom
;
3282 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3283 for (i
= 0; i
< ecom
->size
; i
++) {
3284 eval
= (struct ecommunity_val
*)(ecom
->val
3286 * ECOMMUNITY_SIZE
));
3287 map_vni_to_rt(bgp
, vpn
, eval
);
3293 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3295 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3298 struct ecommunity_val
*eval
;
3299 struct listnode
*node
, *nnode
;
3300 struct ecommunity
*ecom
;
3302 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3303 for (i
= 0; i
< ecom
->size
; i
++) {
3304 struct irt_node
*irt
;
3305 struct ecommunity_val eval_tmp
;
3307 eval
= (struct ecommunity_val
*)(ecom
->val
3309 * ECOMMUNITY_SIZE
));
3310 /* If using "automatic" RT, we only care about the
3311 * local-admin sub-field.
3312 * This is to facilitate using VNI as the RT for EBGP
3315 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3316 if (!is_import_rt_configured(vpn
))
3317 mask_ecom_global_admin(&eval_tmp
, eval
);
3319 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3321 unmap_vni_from_rt(bgp
, vpn
, irt
);
3327 * Derive Import RT automatically for VNI and map VNI to RT.
3328 * The mapping will be used during route processing.
3330 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3332 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
3333 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
3336 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
3340 * Derive Export RT automatically for VNI.
3342 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3344 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
3345 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
3349 * Derive RD automatically for VNI using passed information - it
3350 * is of the form RouterId:unique-id-for-vni.
3352 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3356 vpn
->prd
.family
= AF_UNSPEC
;
3357 vpn
->prd
.prefixlen
= 64;
3358 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
3359 (void)str2prefix_rd(buf
, &vpn
->prd
);
3360 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
3366 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
3368 struct bgpevpn
*vpn
;
3371 memset(&tmp
, 0, sizeof(struct bgpevpn
));
3373 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
3378 * Create a new vpn - invoked upon configuration or zebra notification.
3380 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
3381 struct in_addr originator_ip
,
3382 vrf_id_t tenant_vrf_id
)
3384 struct bgpevpn
*vpn
;
3389 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
3393 /* Set values - RD and RT set to defaults. */
3395 vpn
->originator_ip
= originator_ip
;
3396 vpn
->tenant_vrf_id
= tenant_vrf_id
;
3398 /* Initialize route-target import and export lists */
3399 vpn
->import_rtl
= list_new();
3400 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
3401 vpn
->export_rtl
= list_new();
3402 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
3403 bf_assign_index(bgp
->rd_idspace
, vpn
->rd_id
);
3404 derive_rd_rt_for_vni(bgp
, vpn
);
3406 /* Initialize EVPN route table. */
3407 vpn
->route_table
= bgp_table_init(AFI_L2VPN
, SAFI_EVPN
);
3410 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
3411 XFREE(MTYPE_BGP_EVPN
, vpn
);
3415 /* add to l2vni list on corresponding vrf */
3416 bgpevpn_link_to_l3vni(vpn
);
3418 QOBJ_REG(vpn
, bgpevpn
);
3423 * Free a given VPN - called in multiple scenarios such as zebra
3424 * notification, configuration being deleted, advertise-all-vni disabled etc.
3425 * This just frees appropriate memory, caller should have taken other
3428 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3430 bgpevpn_unlink_from_l3vni(vpn
);
3431 bgp_table_unlock(vpn
->route_table
);
3432 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
3433 list_delete_and_null(&vpn
->import_rtl
);
3434 list_delete_and_null(&vpn
->export_rtl
);
3435 bf_release_index(bgp
->rd_idspace
, vpn
->rd_id
);
3436 hash_release(bgp
->vnihash
, vpn
);
3438 XFREE(MTYPE_BGP_EVPN
, vpn
);
3442 * Import route into matching VNI(s).
3444 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3445 struct prefix
*p
, struct bgp_info
*ri
)
3447 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
3451 * Unimport route from matching VNI(s).
3453 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3454 struct prefix
*p
, struct bgp_info
*ri
)
3456 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
3459 /* filter routes which have martian next hops */
3460 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
3464 struct bgp_node
*rd_rn
, *rn
;
3465 struct bgp_table
*table
;
3466 struct bgp_info
*ri
;
3471 /* Walk entire global routing table and evaluate routes which could be
3472 * imported into this VPN. Note that we cannot just look at the routes
3473 * for the VNI's RD -
3474 * remote routes applicable for this VNI could have any RD.
3476 /* EVPN routes are a 2-level table. */
3477 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3478 rd_rn
= bgp_route_next(rd_rn
)) {
3479 table
= (struct bgp_table
*)(rd_rn
->info
);
3483 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3485 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3487 /* Consider "valid" remote routes applicable for
3489 if (!(ri
->type
== ZEBRA_ROUTE_BGP
3490 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
3493 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
3495 char attr_str
[BUFSIZ
];
3496 char pbuf
[PREFIX_STRLEN
];
3498 bgp_dump_attr(ri
->attr
, attr_str
,
3501 if (bgp_debug_update(ri
->peer
, &rn
->p
,
3504 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
3511 bgp_evpn_unimport_route(bgp
, afi
, safi
,
3514 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
3525 * Handle del of a local MACIP.
3527 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
3530 struct bgpevpn
*vpn
;
3531 struct prefix_evpn p
;
3533 if (!bgp
->vnihash
) {
3534 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
3538 /* Lookup VNI hash - should exist. */
3539 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
3540 if (!vpn
|| !is_vni_live(vpn
)) {
3541 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
3542 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
3546 /* Remove EVPN type-2 route and schedule for processing. */
3547 build_evpn_type2_prefix(&p
, mac
, ip
);
3548 delete_evpn_route(bgp
, vpn
, &p
);
3554 * Handle add of a local MACIP.
3556 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
3557 struct ipaddr
*ip
, u_char flags
)
3559 struct bgpevpn
*vpn
;
3560 struct prefix_evpn p
;
3562 if (!bgp
->vnihash
) {
3563 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
3567 /* Lookup VNI hash - should exist. */
3568 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
3569 if (!vpn
|| !is_vni_live(vpn
)) {
3570 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
3571 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
3575 /* Create EVPN type-2 route and schedule for processing. */
3576 build_evpn_type2_prefix(&p
, mac
, ip
);
3577 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
3578 char buf
[ETHER_ADDR_STRLEN
];
3579 char buf2
[INET6_ADDRSTRLEN
];
3582 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s",
3583 bgp
->vrf_id
, vpn
->vni
,
3584 CHECK_FLAG(flags
, ZEBRA_MAC_TYPE_STICKY
) ? "sticky gateway"
3586 prefix_mac2str(mac
, buf
, sizeof(buf
)),
3587 ipaddr2str(ip
, buf2
, sizeof(buf2
)));
3594 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
3595 struct bgp
*bgp_vrf
)
3597 struct bgpevpn
*vpn
= NULL
;
3598 struct bgp
*bgp_def
= NULL
;
3600 bgp_def
= bgp_get_default();
3603 vpn
= (struct bgpevpn
*)backet
->data
;
3604 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
3605 bgpevpn_link_to_l3vni(vpn
);
3608 int bgp_evpn_local_l3vni_add(vni_t l3vni
,
3610 struct ethaddr
*rmac
)
3612 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
3613 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
3614 struct listnode
*node
= NULL
;
3615 struct bgpevpn
*vpn
= NULL
;
3618 /* get the default instamce - required to get the AS number for VRF
3620 bgp_def
= bgp_get_default();
3622 zlog_err("Cannot process L3VNI %u ADD - default BGP instance not yet created",
3628 /* if the BGP vrf instance doesnt exist - create one */
3629 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
3634 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
3635 BGP_INSTANCE_TYPE_VRF
);
3637 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
3638 zlog_err("'bgp multiple-instance' not present\n");
3640 case BGP_ERR_AS_MISMATCH
:
3641 zlog_err("BGP is already running; AS is %u\n", as
);
3643 case BGP_ERR_INSTANCE_MISMATCH
:
3644 zlog_err("BGP instance name and AS number mismatch\n");
3648 /* mark as auto created */
3649 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
3652 /* associate with l3vni */
3653 bgp_vrf
->l3vni
= l3vni
;
3655 /* set the router mac - to be used in mac-ip routes for this vrf */
3656 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
3658 /* auto derive RD/RT */
3659 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
3660 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
3661 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
3662 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
3664 /* link all corresponding l2vnis */
3665 hash_iterate(bgp_def
->vnihash
,
3666 (void (*)(struct hash_backet
*, void *))
3667 link_l2vni_hash_to_l3vni
,
3670 /* updates all corresponding local mac-ip routes */
3671 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
3672 update_routes_for_vni(bgp_def
, vpn
);
3674 /* install all remote routes belonging to this l3vni into correspondng
3676 install_routes_for_vrf(bgp_vrf
);
3681 int bgp_evpn_local_l3vni_del(vni_t l3vni
,
3684 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
3685 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
3686 struct listnode
*node
= NULL
;
3687 struct bgpevpn
*vpn
= NULL
;
3689 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
3691 zlog_err("Cannot process L3VNI %u Del - Could not find BGP instance",
3696 bgp_def
= bgp_get_default();
3698 zlog_err("Cannot process L3VNI %u Del - Could not find default BGP instance",
3703 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
3704 * take care of uninstalling the routes from zebra */
3705 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
3706 uninstall_routes_for_vrf(bgp_vrf
);
3708 /* remove the l3vni from vrf instance */
3711 /* remove the Rmac from the BGP vrf */
3712 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
3715 if (bgp_vrf
->vrf_import_rtl
&& !list_isempty(bgp_vrf
->vrf_import_rtl
)) {
3716 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3717 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
3719 if (bgp_vrf
->vrf_export_rtl
&& !list_isempty(bgp_vrf
->vrf_export_rtl
)) {
3720 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
3723 /* update all corresponding local mac-ip routes */
3724 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
3725 update_routes_for_vni(bgp_def
, vpn
);
3728 /* Delete the instance if it was autocreated */
3729 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
3730 bgp_delete(bgp_vrf
);
3736 * Handle del of a local VNI.
3738 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
3740 struct bgpevpn
*vpn
;
3742 if (!bgp
->vnihash
) {
3743 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
3747 /* Locate VNI hash */
3748 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
3750 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
3755 /* Remove all local EVPN routes and schedule for processing (to
3756 * withdraw from peers).
3758 delete_routes_for_vni(bgp
, vpn
);
3761 * tunnel is no longer active, del tunnel ip address from tip_hash
3763 bgp_tip_del(bgp
, &vpn
->originator_ip
);
3765 /* Clear "live" flag and see if hash needs to be freed. */
3766 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3767 if (!is_vni_configured(vpn
))
3768 bgp_evpn_free(bgp
, vpn
);
3774 * Handle add (or update) of a local VNI. The only VNI change we care
3775 * about is change to local-tunnel-ip.
3777 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
3778 struct in_addr originator_ip
,
3779 vrf_id_t tenant_vrf_id
)
3781 struct bgpevpn
*vpn
;
3782 struct prefix_evpn p
;
3784 if (!bgp
->vnihash
) {
3785 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
3789 /* Lookup VNI. If present and no change, exit. */
3790 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
3793 /* update tenant_vrf_id if required */
3794 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
3795 bgpevpn_unlink_from_l3vni(vpn
);
3796 vpn
->tenant_vrf_id
= tenant_vrf_id
;
3797 bgpevpn_link_to_l3vni(vpn
);
3799 /* update all routes with new export RT for VRFs */
3800 update_routes_for_vni(bgp
, vpn
);
3803 if (is_vni_live(vpn
)
3804 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
3805 /* Probably some other param has changed that we don't
3809 /* Local tunnel endpoint IP address has changed */
3810 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
3813 /* Create or update as appropriate. */
3815 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
3818 "%u: Failed to allocate VNI entry for VNI %u - at Add",
3824 /* if the VNI is live already, there is nothing more to do */
3825 if (is_vni_live(vpn
))
3828 /* Mark as "live" */
3829 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3831 /* tunnel is now active, add tunnel-ip to db */
3832 bgp_tip_add(bgp
, &originator_ip
);
3834 /* filter routes as nexthop database has changed */
3835 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
3837 /* Create EVPN type-3 route and schedule for processing. */
3838 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3839 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
3840 zlog_err("%u: Type3 route creation failure for VNI %u",
3845 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
3849 install_routes_for_vni(bgp
, vpn
);
3851 /* If we are advertising gateway mac-ip
3852 It needs to be conveyed again to zebra */
3853 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
3859 * Cleanup EVPN information on disable - Need to delete and withdraw
3860 * EVPN routes from peers.
3862 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
3864 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
3865 void *))cleanup_vni_on_disable
,
3870 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
3871 * BGP instance (default) is being freed.
3873 void bgp_evpn_cleanup(struct bgp
*bgp
)
3876 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
3877 void *))free_vni_entry
,
3879 if (bgp
->import_rt_hash
)
3880 hash_free(bgp
->import_rt_hash
);
3881 bgp
->import_rt_hash
= NULL
;
3882 if (bgp
->vrf_import_rt_hash
)
3883 hash_free(bgp
->vrf_import_rt_hash
);
3884 bgp
->vrf_import_rt_hash
= NULL
;
3886 hash_free(bgp
->vnihash
);
3887 bgp
->vnihash
= NULL
;
3888 if (bgp
->vrf_import_rtl
)
3889 list_delete_and_null(&bgp
->vrf_import_rtl
);
3890 bgp
->vrf_import_rtl
= NULL
;
3891 if (bgp
->vrf_export_rtl
)
3892 list_delete_and_null(&bgp
->vrf_export_rtl
);
3893 bgp
->vrf_export_rtl
= NULL
;
3895 list_delete_and_null(&bgp
->l2vnis
);
3897 bf_free(bgp
->rd_idspace
);
3901 * Initialization for EVPN
3904 * hash for RT to VNI
3905 * unique rd id space for auto derivation of RD for VNIs
3907 void bgp_evpn_init(struct bgp
*bgp
)
3910 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
3911 bgp
->import_rt_hash
=
3912 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
3913 "BGP Import RT Hash");
3914 bgp
->vrf_import_rt_hash
=
3915 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
3916 "BGP VRF Import RT Hash");
3917 bgp
->vrf_import_rtl
= list_new();
3918 bgp
->vrf_import_rtl
->cmp
=
3919 (int (*)(void *, void *))evpn_route_target_cmp
;
3921 bgp
->vrf_export_rtl
= list_new();
3922 bgp
->vrf_export_rtl
->cmp
=
3923 (int (*)(void *, void *))evpn_route_target_cmp
;
3924 bgp
->l2vnis
= list_new();
3926 (int (*)(void *, void *))vni_hash_cmp
;
3927 bf_init(bgp
->rd_idspace
, UINT16_MAX
);
3928 /*assign 0th index in the bitfield, so that we start with id 1*/
3929 bf_assign_zero_index(bgp
->rd_idspace
);
3932 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
3934 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);