1 /* BGP EVPN internal definitions
2 * Copyright (C) 2017 Cumulus Networks, Inc.
4 * This file is part of FRR.
6 * FRR is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * FRR is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with FRR; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #ifndef _BGP_EVPN_PRIVATE_H
23 #define _BGP_EVPN_PRIVATE_H
28 #include "bgpd/bgpd.h"
29 #include "bgpd/bgp_ecommunity.h"
31 #define RT_ADDRSTRLEN 28
33 /* EVPN prefix lengths. */
34 #define EVPN_TYPE_2_ROUTE_PREFIXLEN 224
35 #define EVPN_TYPE_3_ROUTE_PREFIXLEN 224
37 /* EVPN route types. */
39 BGP_EVPN_AD_ROUTE
= 1, /* Ethernet Auto-Discovery (A-D) route */
40 BGP_EVPN_MAC_IP_ROUTE
, /* MAC/IP Advertisement route */
41 BGP_EVPN_IMET_ROUTE
, /* Inclusive Multicast Ethernet Tag route */
42 BGP_EVPN_ES_ROUTE
, /* Ethernet Segment route */
43 BGP_EVPN_IP_PREFIX_ROUTE
, /* IP Prefix route */
44 } bgp_evpn_route_type
;
47 * Hash table of EVIs. Right now, the only type of EVI supported is with
48 * VxLAN encapsulation, hence each EVI corresponds to a L2 VNI.
49 * The VNIs are not "created" through BGP but through some other interface
50 * on the system. This table stores VNIs that BGP comes to know as present
51 * on the system (through interaction with zebra) as well as pre-configured
52 * VNIs (which need to be defined in the system to become "live").
56 vrf_id_t tenant_vrf_id
;
58 #define VNI_FLAG_CFGD 0x1 /* VNI is user configured */
59 #define VNI_FLAG_LIVE 0x2 /* VNI is "live" */
60 #define VNI_FLAG_RD_CFGD 0x4 /* RD is user configured. */
61 #define VNI_FLAG_IMPRT_CFGD 0x8 /* Import RT is user configured */
62 #define VNI_FLAG_EXPRT_CFGD 0x10 /* Export RT is user configured */
64 /* Flag to indicate if we are advertising the g/w mac ip for this VNI*/
65 u_int8_t advertise_gw_macip
;
67 /* Id for deriving the RD automatically for this VNI */
70 /* RD for this VNI. */
73 /* Route type 3 field */
74 struct in_addr originator_ip
;
76 /* Import and Export RTs. */
77 struct list
*import_rtl
;
78 struct list
*export_rtl
;
80 /* Route table for EVPN routes for this VNI. */
81 struct bgp_table
*route_table
;
86 DECLARE_QOBJ_TYPE(bgpevpn
)
88 /* Mapping of Import RT to VNIs.
89 * The Import RTs of all VNIs are maintained in a hash table with each
90 * RT linking to all VNIs that will import routes matching this RT.
94 struct ecommunity_val rt
;
96 /* List of VNIs importing routes matching this RT. */
100 #define RT_TYPE_IMPORT 1
101 #define RT_TYPE_EXPORT 2
102 #define RT_TYPE_BOTH 3
104 static inline vni_t
bgpevpn_get_l3vni(struct bgpevpn
*vpn
)
106 struct bgp
*bgp_vrf
= NULL
;
108 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
112 return bgp_vrf
->l3vni
;
115 static inline void bgpevpn_get_rmac(struct bgpevpn
*vpn
, struct ethaddr
*rmac
)
117 struct bgp
*bgp_vrf
= NULL
;
119 memset(rmac
, 0, sizeof(struct ethaddr
));
120 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
123 memcpy(rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
126 static inline struct list
*bgpevpn_get_vrf_export_rtl(struct bgpevpn
*vpn
)
128 struct bgp
*bgp_vrf
= NULL
;
130 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
134 return bgp_vrf
->vrf_export_rtl
;
137 static inline struct list
*bgpevpn_get_vrf_import_rtl(struct bgpevpn
*vpn
)
139 struct bgp
*bgp_vrf
= NULL
;
141 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
145 return bgp_vrf
->vrf_import_rtl
;
148 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn
*vpn
)
150 struct bgp
*bgp_vrf
= NULL
;
152 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
153 if (!bgp_vrf
|| !bgp_vrf
->l2vnis
)
155 listnode_delete(bgp_vrf
->l2vnis
, vpn
);
158 static inline void bgpevpn_link_to_l3vni(struct bgpevpn
*vpn
)
160 struct bgp
*bgp_vrf
= NULL
;
162 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
163 if (!bgp_vrf
|| !bgp_vrf
->l2vnis
)
165 listnode_add_sort(bgp_vrf
->l2vnis
, vpn
);
168 static inline int is_vni_configured(struct bgpevpn
*vpn
)
170 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_CFGD
));
173 static inline int is_vni_live(struct bgpevpn
*vpn
)
175 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_LIVE
));
178 static inline int is_rd_configured(struct bgpevpn
*vpn
)
180 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
));
183 static inline int bgp_evpn_rd_matches_existing(struct bgpevpn
*vpn
,
184 struct prefix_rd
*prd
)
186 return (memcmp(&vpn
->prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
189 static inline int is_import_rt_configured(struct bgpevpn
*vpn
)
191 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
));
194 static inline int is_export_rt_configured(struct bgpevpn
*vpn
)
196 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
));
199 static inline int is_vni_param_configured(struct bgpevpn
*vpn
)
201 return (is_rd_configured(vpn
) || is_import_rt_configured(vpn
)
202 || is_export_rt_configured(vpn
));
205 static inline void vni2label(vni_t vni
, mpls_label_t
*label
)
207 u_char
*tag
= (u_char
*)label
;
208 tag
[0] = (vni
>> 16) & 0xFF;
209 tag
[1] = (vni
>> 8) & 0xFF;
213 static inline vni_t
label2vni(mpls_label_t
*label
)
215 u_char
*tag
= (u_char
*)label
;
218 vni
= ((u_int32_t
)*tag
++ << 16);
219 vni
|= (u_int32_t
)*tag
++ << 8;
220 vni
|= (u_int32_t
)(*tag
& 0xFF);
225 static inline void encode_rmac_extcomm(struct ecommunity_val
*eval
,
226 struct ethaddr
*rmac
)
228 memset(eval
, 0, sizeof(*eval
));
229 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
230 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC
;
231 memcpy(&eval
->val
[2], rmac
, ETH_ALEN
);
234 static inline void encode_mac_mobility_extcomm(int static_mac
, u_int32_t seq
,
235 struct ecommunity_val
*eval
)
237 memset(eval
, 0, sizeof(*eval
));
238 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
239 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
;
241 eval
->val
[2] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY
;
242 eval
->val
[4] = (seq
>> 24) & 0xff;
243 eval
->val
[5] = (seq
>> 16) & 0xff;
244 eval
->val
[6] = (seq
>> 8) & 0xff;
245 eval
->val
[7] = seq
& 0xff;
248 static inline void build_evpn_type2_prefix(struct prefix_evpn
*p
,
252 memset(p
, 0, sizeof(struct prefix_evpn
));
254 p
->prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
255 p
->prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
256 memcpy(&p
->prefix
.mac
.octet
, mac
->octet
, ETH_ALEN
);
257 p
->prefix
.ip
.ipa_type
= IPADDR_NONE
;
259 memcpy(&p
->prefix
.ip
, ip
, sizeof(*ip
));
262 static inline void build_evpn_type3_prefix(struct prefix_evpn
*p
,
263 struct in_addr originator_ip
)
265 memset(p
, 0, sizeof(struct prefix_evpn
));
267 p
->prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
268 p
->prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
269 p
->prefix
.ip
.ipa_type
= IPADDR_V4
;
270 p
->prefix
.ip
.ipaddr_v4
= originator_ip
;
273 extern void evpn_rt_delete_auto(struct bgp
*, vni_t
, struct list
*);
274 extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*,
276 extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*,
278 extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*,
280 extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*,
282 extern int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
,
283 struct bgpevpn
*vpn
);
284 extern void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
286 extern int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
287 extern int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
288 extern void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
);
289 extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
,
290 struct bgpevpn
*vpn
);
291 extern void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
,
292 struct bgpevpn
*vpn
);
293 extern void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
,
294 struct bgpevpn
*vpn
);
295 extern void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
);
296 extern struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
);
297 extern struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
298 struct in_addr originator_ip
,
299 vrf_id_t tenant_vrf_id
);
300 extern void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
);
301 #endif /* _BGP_EVPN_PRIVATE_H */