]> git.proxmox.com Git - mirror_frr.git/blobdiff - bgpd/bgp_evpn_private.h
bgpd: support evpn nd ext community
[mirror_frr.git] / bgpd / bgp_evpn_private.h
index cc0ec82344b3f2c03cad7cc7a4fd1ca1806d82c5..d89716ca399a01c3cf9e5f9e84bd3b6cddcf875d 100644 (file)
@@ -31,9 +31,7 @@
 #define RT_ADDRSTRLEN 28
 
 /* EVPN prefix lengths. This reprsent the sizeof struct prefix_evpn  */
-#define EVPN_TYPE_2_ROUTE_PREFIXLEN      224
-#define EVPN_TYPE_3_ROUTE_PREFIXLEN      224
-#define EVPN_TYPE_5_ROUTE_PREFIXLEN      224
+#define EVPN_ROUTE_PREFIXLEN     224
 
 /* EVPN route types. */
 typedef enum {
@@ -55,21 +53,29 @@ typedef enum {
 struct bgpevpn {
        vni_t vni;
        vrf_id_t tenant_vrf_id;
-       u_int32_t flags;
+       uint32_t flags;
 #define VNI_FLAG_CFGD              0x1  /* VNI is user configured */
 #define VNI_FLAG_LIVE              0x2  /* VNI is "live" */
 #define VNI_FLAG_RD_CFGD           0x4  /* RD is user configured. */
 #define VNI_FLAG_IMPRT_CFGD        0x8  /* Import RT is user configured */
 #define VNI_FLAG_EXPRT_CFGD        0x10 /* Export RT is user configured */
+#define VNI_FLAG_USE_TWO_LABELS    0x20 /* Attach both L2-VNI and L3-VNI if
+                                          needed for this VPN */
 
-       /* Flag to indicate if we are advertising the g/w mac ip for this VNI*/
-       u_int8_t advertise_gw_macip;
+       struct bgp *bgp_vrf; /* back pointer to the vrf instance */
 
-       /* Flag to indicate if we are advertising subnet for this VNI */
-       u_int8_t advertise_subnet;
+                                          /* Flag to indicate if we are
+                                           * advertising the g/w mac ip for
+                                           * this VNI*/
+       uint8_t advertise_gw_macip;
 
-       /* Id for deriving the RD automatically for this VNI */
-       u_int16_t rd_id;
+       /* Flag to indicate if we are
+        * advertising subnet for this VNI */
+       uint8_t advertise_subnet;
+
+       /* Id for deriving the RD
+        * automatically for this VNI */
+       uint16_t rd_id;
 
        /* RD for this VNI. */
        struct prefix_rd prd;
@@ -81,7 +87,8 @@ struct bgpevpn {
        struct list *import_rtl;
        struct list *export_rtl;
 
-       /* Route table for EVPN routes for this VNI. */
+       /* Route table for EVPN routes for
+        * this VNI. */
        struct bgp_table *route_table;
 
        QOBJ_FIELDS
@@ -89,6 +96,42 @@ struct bgpevpn {
 
 DECLARE_QOBJ_TYPE(bgpevpn)
 
+struct evpnes {
+
+       /* Ethernet Segment Identifier */
+       esi_t esi;
+
+       /* es flags */
+       uint16_t flags;
+#define EVPNES_LOCAL   0x01
+#define EVPNES_REMOTE  0x02
+
+       /*
+        * Id for deriving the RD
+        * automatically for this ESI
+        */
+       uint16_t rd_id;
+
+       /* RD for this VNI. */
+       struct prefix_rd prd;
+
+       /* originator ip address  */
+       struct ipaddr originator_ip;
+
+       /* list of VTEPs in the same site */
+       struct list *vtep_list;
+
+       /*
+        * Route table for EVPN routes for
+        * this ESI. - type4 routes
+        */
+       struct bgp_table *route_table;
+
+       QOBJ_FIELDS
+};
+
+DECLARE_QOBJ_TYPE(evpnes)
+
 /* Mapping of Import RT to VNIs.
  * The Import RTs of all VNIs are maintained in a hash table with each
  * RT linking to all VNIs that will import routes matching this RT.
@@ -120,8 +163,7 @@ struct vrf_irt_node {
 
 static inline int is_vrf_rd_configured(struct bgp *bgp_vrf)
 {
-       return (CHECK_FLAG(bgp_vrf->vrf_flags,
-                          BGP_VRF_RD_CFGD));
+       return (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_RD_CFGD));
 }
 
 static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp *bgp_vrf,
@@ -132,66 +174,65 @@ static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp *bgp_vrf,
 
 static inline vni_t bgpevpn_get_l3vni(struct bgpevpn *vpn)
 {
-       struct bgp *bgp_vrf = NULL;
-
-       bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
-       if (!bgp_vrf)
-               return 0;
-
-       return bgp_vrf->l3vni;
+       return vpn->bgp_vrf ? vpn->bgp_vrf->l3vni : 0;
 }
 
 static inline void bgpevpn_get_rmac(struct bgpevpn *vpn, struct ethaddr *rmac)
 {
-       struct bgp *bgp_vrf = NULL;
-
        memset(rmac, 0, sizeof(struct ethaddr));
-       bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
-       if (!bgp_vrf)
+       if (!vpn->bgp_vrf)
                return;
-       memcpy(rmac, &bgp_vrf->rmac, sizeof(struct ethaddr));
+       memcpy(rmac, &vpn->bgp_vrf->rmac, sizeof(struct ethaddr));
 }
 
 static inline struct list *bgpevpn_get_vrf_export_rtl(struct bgpevpn *vpn)
 {
-       struct bgp *bgp_vrf = NULL;
-
-       bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
-       if (!bgp_vrf)
+       if (!vpn->bgp_vrf)
                return NULL;
 
-       return bgp_vrf->vrf_export_rtl;
+       return vpn->bgp_vrf->vrf_export_rtl;
 }
 
 static inline struct list *bgpevpn_get_vrf_import_rtl(struct bgpevpn *vpn)
 {
-       struct bgp *bgp_vrf = NULL;
-
-       bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
-       if (!bgp_vrf)
+       if (!vpn->bgp_vrf)
                return NULL;
 
-       return bgp_vrf->vrf_import_rtl;
+       return vpn->bgp_vrf->vrf_import_rtl;
 }
 
 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn *vpn)
 {
-       struct bgp *bgp_vrf = NULL;
-
-       bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
-       if (!bgp_vrf || !bgp_vrf->l2vnis)
+       /* bail if vpn is not associated to bgp_vrf */
+       if (!vpn->bgp_vrf)
                return;
-       listnode_delete(bgp_vrf->l2vnis, vpn);
+
+       UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
+       listnode_delete(vpn->bgp_vrf->l2vnis, vpn);
+
+       /* remove the backpointer to the vrf instance */
+       vpn->bgp_vrf = NULL;
 }
 
 static inline void bgpevpn_link_to_l3vni(struct bgpevpn *vpn)
 {
        struct bgp *bgp_vrf = NULL;
 
+       /* bail if vpn is already associated to vrf */
+       if (vpn->bgp_vrf)
+               return;
+
        bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
-       if (!bgp_vrf || !bgp_vrf->l2vnis)
+       if (!bgp_vrf)
                return;
+
+       /* associate the vpn to the bgp_vrf instance */
+       vpn->bgp_vrf = bgp_vrf;
        listnode_add_sort(bgp_vrf->l2vnis, vpn);
+
+       /* check if we are advertising two labels for this vpn */
+       if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY))
+               SET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
 }
 
 static inline int is_vni_configured(struct bgpevpn *vpn)
@@ -231,6 +272,15 @@ static inline int is_vni_param_configured(struct bgpevpn *vpn)
                || is_export_rt_configured(vpn));
 }
 
+static inline void encode_es_rt_extcomm(struct ecommunity_val *eval,
+                                       struct ethaddr *mac)
+{
+       memset(eval, 0, sizeof(struct ecommunity_val));
+       eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
+       eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_ES_IMPORT_RT;
+       memcpy(&eval->val[2], mac, ETH_ALEN);
+}
+
 static inline void encode_rmac_extcomm(struct ecommunity_val *eval,
                                       struct ethaddr *rmac)
 {
@@ -247,7 +297,7 @@ static inline void encode_default_gw_extcomm(struct ecommunity_val *eval)
        eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_DEF_GW;
 }
 
-static inline void encode_mac_mobility_extcomm(int static_mac, u_int32_t seq,
+static inline void encode_mac_mobility_extcomm(int static_mac, uint32_t seq,
                                               struct ecommunity_val *eval)
 {
        memset(eval, 0, sizeof(*eval));
@@ -261,56 +311,80 @@ static inline void encode_mac_mobility_extcomm(int static_mac, u_int32_t seq,
        eval->val[7] = seq & 0xff;
 }
 
+static inline void encode_na_flag_extcomm(struct ecommunity_val *eval,
+                                         uint8_t na_flag)
+{
+       memset(eval, 0, sizeof(*eval));
+       eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
+       eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_ND;
+       if (na_flag)
+               eval->val[2] |= ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG;
+}
+
 static inline void ip_prefix_from_type5_prefix(struct prefix_evpn *evp,
                                               struct prefix *ip)
 {
        memset(ip, 0, sizeof(struct prefix));
-       if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
+       if (is_evpn_prefix_ipaddr_v4(evp)) {
                ip->family = AF_INET;
-               ip->prefixlen = evp->prefix.ip_prefix_length;
-               memcpy(&(ip->u.prefix4),
-                      &(evp->prefix.ip.ip),
+               ip->prefixlen = evp->prefix.prefix_addr.ip_prefix_length;
+               memcpy(&(ip->u.prefix4), &(evp->prefix.prefix_addr.ip.ip),
                       IPV4_MAX_BYTELEN);
-       } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
+       } else if (is_evpn_prefix_ipaddr_v6(evp)) {
                ip->family = AF_INET6;
-               ip->prefixlen = evp->prefix.ip_prefix_length;
-               memcpy(&(ip->u.prefix6),
-                      &(evp->prefix.ip.ip),
+               ip->prefixlen = evp->prefix.prefix_addr.ip_prefix_length;
+               memcpy(&(ip->u.prefix6), &(evp->prefix.prefix_addr.ip.ip),
                       IPV6_MAX_BYTELEN);
        }
 }
 
+static inline int is_evpn_prefix_default(struct prefix *evp)
+{
+       if (evp->family != AF_EVPN)
+               return 0;
+
+       return ((evp->u.prefix_evpn.prefix_addr.ip_prefix_length  == 0) ?
+               1 : 0);
+}
+
 static inline void ip_prefix_from_type2_prefix(struct prefix_evpn *evp,
                                               struct prefix *ip)
 {
        memset(ip, 0, sizeof(struct prefix));
-       if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
+       if (is_evpn_prefix_ipaddr_v4(evp)) {
                ip->family = AF_INET;
                ip->prefixlen = IPV4_MAX_BITLEN;
-               memcpy(&(ip->u.prefix4),
-                      &(evp->prefix.ip.ip),
+               memcpy(&(ip->u.prefix4), &(evp->prefix.macip_addr.ip.ip),
                       IPV4_MAX_BYTELEN);
-       } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
+       } else if (is_evpn_prefix_ipaddr_v6(evp)) {
                ip->family = AF_INET6;
                ip->prefixlen = IPV6_MAX_BITLEN;
-               memcpy(&(ip->u.prefix6),
-                      &(evp->prefix.ip.ip),
+               memcpy(&(ip->u.prefix6), &(evp->prefix.macip_addr.ip.ip),
                       IPV6_MAX_BYTELEN);
        }
 }
 
+static inline void ip_prefix_from_evpn_prefix(struct prefix_evpn *evp,
+                                             struct prefix *ip)
+{
+       if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
+               ip_prefix_from_type2_prefix(evp, ip);
+       else if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
+               ip_prefix_from_type5_prefix(evp, ip);
+}
+
 static inline void build_evpn_type2_prefix(struct prefix_evpn *p,
                                           struct ethaddr *mac,
                                           struct ipaddr *ip)
 {
        memset(p, 0, sizeof(struct prefix_evpn));
        p->family = AF_EVPN;
-       p->prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
+       p->prefixlen = EVPN_ROUTE_PREFIXLEN;
        p->prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
-       memcpy(&p->prefix.mac.octet, mac->octet, ETH_ALEN);
-       p->prefix.ip.ipa_type = IPADDR_NONE;
+       memcpy(&p->prefix.macip_addr.mac.octet, mac->octet, ETH_ALEN);
+       p->prefix.macip_addr.ip.ipa_type = IPADDR_NONE;
        if (ip)
-               memcpy(&p->prefix.ip, ip, sizeof(*ip));
+               memcpy(&p->prefix.macip_addr.ip, ip, sizeof(*ip));
 }
 
 static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn *evp,
@@ -331,11 +405,11 @@ static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn *evp,
 
        memset(evp, 0, sizeof(struct prefix_evpn));
        evp->family = AF_EVPN;
-       evp->prefixlen = EVPN_TYPE_5_ROUTE_PREFIXLEN;
-       evp->prefix.ip_prefix_length = ip_prefix->prefixlen;
+       evp->prefixlen = EVPN_ROUTE_PREFIXLEN;
        evp->prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
-       evp->prefix.ip.ipa_type = ip.ipa_type;
-       memcpy(&evp->prefix.ip, &ip, sizeof(struct ipaddr));
+       evp->prefix.prefix_addr.ip_prefix_length = ip_prefix->prefixlen;
+       evp->prefix.prefix_addr.ip.ipa_type = ip.ipa_type;
+       memcpy(&evp->prefix.prefix_addr.ip, &ip, sizeof(struct ipaddr));
 }
 
 static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
@@ -343,30 +417,56 @@ static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
 {
        memset(p, 0, sizeof(struct prefix_evpn));
        p->family = AF_EVPN;
-       p->prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
+       p->prefixlen = EVPN_ROUTE_PREFIXLEN;
        p->prefix.route_type = BGP_EVPN_IMET_ROUTE;
-       p->prefix.ip.ipa_type = IPADDR_V4;
-       p->prefix.ip.ipaddr_v4 = originator_ip;
+       p->prefix.imet_addr.ip.ipa_type = IPADDR_V4;
+       p->prefix.imet_addr.ip.ipaddr_v4 = originator_ip;
 }
 
-static inline int advertise_type5_routes(struct bgp *bgp_vrf,
-                                        afi_t afi)
+static inline void build_evpn_type4_prefix(struct prefix_evpn *p,
+                                          esi_t *esi,
+                                          struct in_addr originator_ip)
 {
-       if (!bgp_vrf->l3vni)
-               return 0;
+       memset(p, 0, sizeof(struct prefix_evpn));
+       p->family = AF_EVPN;
+       p->prefixlen = EVPN_ROUTE_PREFIXLEN;
+       p->prefix.route_type = BGP_EVPN_ES_ROUTE;
+       p->prefix.es_addr.ip_prefix_length = IPV4_MAX_BITLEN;
+       p->prefix.es_addr.ip.ipa_type = IPADDR_V4;
+       p->prefix.es_addr.ip.ipaddr_v4 = originator_ip;
+       memcpy(&p->prefix.es_addr.esi, esi, sizeof(esi_t));
+}
 
+static inline int evpn_default_originate_set(struct bgp *bgp, afi_t afi,
+                                            safi_t safi)
+{
        if (afi == AFI_IP &&
-           CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_IPV4_IN_EVPN))
+           CHECK_FLAG(bgp->af_flags[AFI_L2VPN][SAFI_EVPN],
+                      BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4))
                return 1;
-
-       if (afi == AFI_IP6 &&
-           CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_IPV6_IN_EVPN))
+       else if (afi == AFI_IP6 &&
+                CHECK_FLAG(bgp->af_flags[AFI_L2VPN][SAFI_EVPN],
+                           BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6))
                return 1;
-
        return 0;
 }
 
-extern void evpn_rt_delete_auto(struct bgp*, vni_t, struct list*);
+static inline void es_get_system_mac(esi_t *esi,
+                                    struct ethaddr *mac)
+{
+       /*
+        * for type-1 and type-3 ESIs,
+        * the system mac starts at val[1]
+        */
+       memcpy(mac, &esi->val[1], ETH_ALEN);
+}
+
+static inline int is_es_local(struct evpnes *es)
+{
+       return CHECK_FLAG(es->flags, EVPNES_LOCAL) ? 1 : 0;
+}
+
+extern void evpn_rt_delete_auto(struct bgp *, vni_t, struct list *);
 extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
                                                 struct ecommunity *ecomadd);
 extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
@@ -377,6 +477,7 @@ extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
                                                   struct ecommunity *ecomdel);
 extern int bgp_evpn_handle_export_rt_change(struct bgp *bgp,
                                            struct bgpevpn *vpn);
+extern void bgp_evpn_handle_autort_change(struct bgp *bgp);
 extern void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw);
 extern void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
                                      int withdraw);
@@ -398,4 +499,8 @@ extern struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
                                    struct in_addr originator_ip,
                                    vrf_id_t tenant_vrf_id);
 extern void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn);
+extern struct evpnes *bgp_evpn_lookup_es(struct bgp *bgp, esi_t *esi);
+extern struct evpnes *bgp_evpn_es_new(struct bgp *bgp, esi_t *esi,
+                                     struct ipaddr *originator_ip);
+extern void bgp_evpn_es_free(struct bgp *bgp, struct evpnes *es);
 #endif /* _BGP_EVPN_PRIVATE_H */