net/sfc: support inner frame ETH in flow API filters
[dpdk.git] / drivers / net / sfc / sfc_flow.c
index 11ab892..0da6383 100644 (file)
@@ -9,7 +9,7 @@
 
 #include <rte_tailq.h>
 #include <rte_common.h>
-#include <rte_ethdev.h>
+#include <rte_ethdev_driver.h>
 #include <rte_eth_ctrl.h>
 #include <rte_ether.h>
 #include <rte_flow.h>
@@ -57,6 +57,9 @@ static sfc_flow_item_parse sfc_flow_parse_ipv4;
 static sfc_flow_item_parse sfc_flow_parse_ipv6;
 static sfc_flow_item_parse sfc_flow_parse_tcp;
 static sfc_flow_item_parse sfc_flow_parse_udp;
+static sfc_flow_item_parse sfc_flow_parse_vxlan;
+static sfc_flow_item_parse sfc_flow_parse_geneve;
+static sfc_flow_item_parse sfc_flow_parse_nvgre;
 
 static boolean_t
 sfc_flow_is_zero(const uint8_t *buf, unsigned int size)
@@ -115,13 +118,13 @@ sfc_flow_parse_init(const struct rte_flow_item *item,
                        return -rte_errno;
                }
 
-               mask = (const uint8_t *)def_mask;
+               mask = def_mask;
        } else {
-               mask = (const uint8_t *)item->mask;
+               mask = item->mask;
        }
 
-       spec = (const uint8_t *)item->spec;
-       last = (const uint8_t *)item->last;
+       spec = item->spec;
+       last = item->last;
 
        if (spec == NULL)
                goto exit;
@@ -184,11 +187,11 @@ sfc_flow_parse_void(__rte_unused const struct rte_flow_item *item,
  * Convert Ethernet item to EFX filter specification.
  *
  * @param item[in]
- *   Item specification. Only source and destination addresses and
- *   Ethernet type fields are supported. In addition to full and
- *   empty masks of destination address, individual/group mask is
- *   also supported. If the mask is NULL, default mask will be used.
- *   Ranging is not supported.
+ *   Item specification. Outer frame specification may only comprise
+ *   source/destination addresses and Ethertype field.
+ *   Inner frame specification may contain destination address only.
+ *   There is support for individual/group mask as well as for empty and full.
+ *   If the mask is NULL, default mask will be used. Ranging is not supported.
  * @param efx_spec[in, out]
  *   EFX filter specification to update.
  * @param[out] error
@@ -207,40 +210,75 @@ sfc_flow_parse_eth(const struct rte_flow_item *item,
                .src.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
                .type = 0xffff,
        };
+       const struct rte_flow_item_eth ifrm_supp_mask = {
+               .dst.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
+       };
        const uint8_t ig_mask[EFX_MAC_ADDR_LEN] = {
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00
        };
+       const struct rte_flow_item_eth *supp_mask_p;
+       const struct rte_flow_item_eth *def_mask_p;
+       uint8_t *loc_mac = NULL;
+       boolean_t is_ifrm = (efx_spec->efs_encap_type !=
+               EFX_TUNNEL_PROTOCOL_NONE);
+
+       if (is_ifrm) {
+               supp_mask_p = &ifrm_supp_mask;
+               def_mask_p = &ifrm_supp_mask;
+               loc_mac = efx_spec->efs_ifrm_loc_mac;
+       } else {
+               supp_mask_p = &supp_mask;
+               def_mask_p = &rte_flow_item_eth_mask;
+               loc_mac = efx_spec->efs_loc_mac;
+       }
 
        rc = sfc_flow_parse_init(item,
                                 (const void **)&spec,
                                 (const void **)&mask,
-                                &supp_mask,
-                                &rte_flow_item_eth_mask,
+                                supp_mask_p, def_mask_p,
                                 sizeof(struct rte_flow_item_eth),
                                 error);
        if (rc != 0)
                return rc;
 
-       /* If "spec" is not set, could be any Ethernet */
-       if (spec == NULL)
-               return 0;
+       /*
+        * If "spec" is not set, could be any Ethernet, but for the inner frame
+        * type of destination MAC must be set
+        */
+       if (spec == NULL) {
+               if (is_ifrm)
+                       goto fail_bad_ifrm_dst_mac;
+               else
+                       return 0;
+       }
 
        if (is_same_ether_addr(&mask->dst, &supp_mask.dst)) {
-               efx_spec->efs_match_flags |= EFX_FILTER_MATCH_LOC_MAC;
-               rte_memcpy(efx_spec->efs_loc_mac, spec->dst.addr_bytes,
+               efx_spec->efs_match_flags |= is_ifrm ?
+                       EFX_FILTER_MATCH_IFRM_LOC_MAC :
+                       EFX_FILTER_MATCH_LOC_MAC;
+               rte_memcpy(loc_mac, spec->dst.addr_bytes,
                           EFX_MAC_ADDR_LEN);
        } else if (memcmp(mask->dst.addr_bytes, ig_mask,
                          EFX_MAC_ADDR_LEN) == 0) {
                if (is_unicast_ether_addr(&spec->dst))
-                       efx_spec->efs_match_flags |=
+                       efx_spec->efs_match_flags |= is_ifrm ?
+                               EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST :
                                EFX_FILTER_MATCH_UNKNOWN_UCAST_DST;
                else
-                       efx_spec->efs_match_flags |=
+                       efx_spec->efs_match_flags |= is_ifrm ?
+                               EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST :
                                EFX_FILTER_MATCH_UNKNOWN_MCAST_DST;
        } else if (!is_zero_ether_addr(&mask->dst)) {
                goto fail_bad_mask;
+       } else if (is_ifrm) {
+               goto fail_bad_ifrm_dst_mac;
        }
 
+       /*
+        * ifrm_supp_mask ensures that the source address and
+        * ethertype masks are equal to zero in inner frame,
+        * so these fields are filled in only for the outer frame
+        */
        if (is_same_ether_addr(&mask->src, &supp_mask.src)) {
                efx_spec->efs_match_flags |= EFX_FILTER_MATCH_REM_MAC;
                rte_memcpy(efx_spec->efs_rem_mac, spec->src.addr_bytes,
@@ -267,6 +305,13 @@ fail_bad_mask:
                           RTE_FLOW_ERROR_TYPE_ITEM, item,
                           "Bad mask in the ETH pattern item");
        return -rte_errno;
+
+fail_bad_ifrm_dst_mac:
+       rte_flow_error_set(error, EINVAL,
+                          RTE_FLOW_ERROR_TYPE_ITEM, item,
+                          "Type of destination MAC address in inner frame "
+                          "must be set");
+       return -rte_errno;
 }
 
 /**
@@ -696,6 +741,258 @@ fail_bad_mask:
        return -rte_errno;
 }
 
+/*
+ * Filters for encapsulated packets match based on the EtherType and IP
+ * protocol in the outer frame.
+ */
+static int
+sfc_flow_set_match_flags_for_encap_pkts(const struct rte_flow_item *item,
+                                       efx_filter_spec_t *efx_spec,
+                                       uint8_t ip_proto,
+                                       struct rte_flow_error *error)
+{
+       if (!(efx_spec->efs_match_flags & EFX_FILTER_MATCH_IP_PROTO)) {
+               efx_spec->efs_match_flags |= EFX_FILTER_MATCH_IP_PROTO;
+               efx_spec->efs_ip_proto = ip_proto;
+       } else if (efx_spec->efs_ip_proto != ip_proto) {
+               switch (ip_proto) {
+               case EFX_IPPROTO_UDP:
+                       rte_flow_error_set(error, EINVAL,
+                               RTE_FLOW_ERROR_TYPE_ITEM, item,
+                               "Outer IP header protocol must be UDP "
+                               "in VxLAN/GENEVE pattern");
+                       return -rte_errno;
+
+               case EFX_IPPROTO_GRE:
+                       rte_flow_error_set(error, EINVAL,
+                               RTE_FLOW_ERROR_TYPE_ITEM, item,
+                               "Outer IP header protocol must be GRE "
+                               "in NVGRE pattern");
+                       return -rte_errno;
+
+               default:
+                       rte_flow_error_set(error, EINVAL,
+                               RTE_FLOW_ERROR_TYPE_ITEM, item,
+                               "Only VxLAN/GENEVE/NVGRE tunneling patterns "
+                               "are supported");
+                       return -rte_errno;
+               }
+       }
+
+       if (!(efx_spec->efs_match_flags & EFX_FILTER_MATCH_ETHER_TYPE)) {
+               rte_flow_error_set(error, EINVAL,
+                       RTE_FLOW_ERROR_TYPE_ITEM, item,
+                       "Outer frame EtherType in pattern with tunneling "
+                       "must be set");
+               return -rte_errno;
+       } else if (efx_spec->efs_ether_type != EFX_ETHER_TYPE_IPV4 &&
+                  efx_spec->efs_ether_type != EFX_ETHER_TYPE_IPV6) {
+               rte_flow_error_set(error, EINVAL,
+                       RTE_FLOW_ERROR_TYPE_ITEM, item,
+                       "Outer frame EtherType in pattern with tunneling "
+                       "must be IPv4 or IPv6");
+               return -rte_errno;
+       }
+
+       return 0;
+}
+
+static int
+sfc_flow_set_efx_spec_vni_or_vsid(efx_filter_spec_t *efx_spec,
+                                 const uint8_t *vni_or_vsid_val,
+                                 const uint8_t *vni_or_vsid_mask,
+                                 const struct rte_flow_item *item,
+                                 struct rte_flow_error *error)
+{
+       const uint8_t vni_or_vsid_full_mask[EFX_VNI_OR_VSID_LEN] = {
+               0xff, 0xff, 0xff
+       };
+
+       if (memcmp(vni_or_vsid_mask, vni_or_vsid_full_mask,
+                  EFX_VNI_OR_VSID_LEN) == 0) {
+               efx_spec->efs_match_flags |= EFX_FILTER_MATCH_VNI_OR_VSID;
+               rte_memcpy(efx_spec->efs_vni_or_vsid, vni_or_vsid_val,
+                          EFX_VNI_OR_VSID_LEN);
+       } else if (!sfc_flow_is_zero(vni_or_vsid_mask, EFX_VNI_OR_VSID_LEN)) {
+               rte_flow_error_set(error, EINVAL,
+                                  RTE_FLOW_ERROR_TYPE_ITEM, item,
+                                  "Unsupported VNI/VSID mask");
+               return -rte_errno;
+       }
+
+       return 0;
+}
+
+/**
+ * Convert VXLAN item to EFX filter specification.
+ *
+ * @param item[in]
+ *   Item specification. Only VXLAN network identifier field is supported.
+ *   If the mask is NULL, default mask will be used.
+ *   Ranging is not supported.
+ * @param efx_spec[in, out]
+ *   EFX filter specification to update.
+ * @param[out] error
+ *   Perform verbose error reporting if not NULL.
+ */
+static int
+sfc_flow_parse_vxlan(const struct rte_flow_item *item,
+                    efx_filter_spec_t *efx_spec,
+                    struct rte_flow_error *error)
+{
+       int rc;
+       const struct rte_flow_item_vxlan *spec = NULL;
+       const struct rte_flow_item_vxlan *mask = NULL;
+       const struct rte_flow_item_vxlan supp_mask = {
+               .vni = { 0xff, 0xff, 0xff }
+       };
+
+       rc = sfc_flow_parse_init(item,
+                                (const void **)&spec,
+                                (const void **)&mask,
+                                &supp_mask,
+                                &rte_flow_item_vxlan_mask,
+                                sizeof(struct rte_flow_item_vxlan),
+                                error);
+       if (rc != 0)
+               return rc;
+
+       rc = sfc_flow_set_match_flags_for_encap_pkts(item, efx_spec,
+                                                    EFX_IPPROTO_UDP, error);
+       if (rc != 0)
+               return rc;
+
+       efx_spec->efs_encap_type = EFX_TUNNEL_PROTOCOL_VXLAN;
+       efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ENCAP_TYPE;
+
+       if (spec == NULL)
+               return 0;
+
+       rc = sfc_flow_set_efx_spec_vni_or_vsid(efx_spec, spec->vni,
+                                              mask->vni, item, error);
+
+       return rc;
+}
+
+/**
+ * Convert GENEVE item to EFX filter specification.
+ *
+ * @param item[in]
+ *   Item specification. Only Virtual Network Identifier and protocol type
+ *   fields are supported. But protocol type can be only Ethernet (0x6558).
+ *   If the mask is NULL, default mask will be used.
+ *   Ranging is not supported.
+ * @param efx_spec[in, out]
+ *   EFX filter specification to update.
+ * @param[out] error
+ *   Perform verbose error reporting if not NULL.
+ */
+static int
+sfc_flow_parse_geneve(const struct rte_flow_item *item,
+                     efx_filter_spec_t *efx_spec,
+                     struct rte_flow_error *error)
+{
+       int rc;
+       const struct rte_flow_item_geneve *spec = NULL;
+       const struct rte_flow_item_geneve *mask = NULL;
+       const struct rte_flow_item_geneve supp_mask = {
+               .protocol = RTE_BE16(0xffff),
+               .vni = { 0xff, 0xff, 0xff }
+       };
+
+       rc = sfc_flow_parse_init(item,
+                                (const void **)&spec,
+                                (const void **)&mask,
+                                &supp_mask,
+                                &rte_flow_item_geneve_mask,
+                                sizeof(struct rte_flow_item_geneve),
+                                error);
+       if (rc != 0)
+               return rc;
+
+       rc = sfc_flow_set_match_flags_for_encap_pkts(item, efx_spec,
+                                                    EFX_IPPROTO_UDP, error);
+       if (rc != 0)
+               return rc;
+
+       efx_spec->efs_encap_type = EFX_TUNNEL_PROTOCOL_GENEVE;
+       efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ENCAP_TYPE;
+
+       if (spec == NULL)
+               return 0;
+
+       if (mask->protocol == supp_mask.protocol) {
+               if (spec->protocol != rte_cpu_to_be_16(ETHER_TYPE_TEB)) {
+                       rte_flow_error_set(error, EINVAL,
+                               RTE_FLOW_ERROR_TYPE_ITEM, item,
+                               "GENEVE encap. protocol must be Ethernet "
+                               "(0x6558) in the GENEVE pattern item");
+                       return -rte_errno;
+               }
+       } else if (mask->protocol != 0) {
+               rte_flow_error_set(error, EINVAL,
+                       RTE_FLOW_ERROR_TYPE_ITEM, item,
+                       "Unsupported mask for GENEVE encap. protocol");
+               return -rte_errno;
+       }
+
+       rc = sfc_flow_set_efx_spec_vni_or_vsid(efx_spec, spec->vni,
+                                              mask->vni, item, error);
+
+       return rc;
+}
+
+/**
+ * Convert NVGRE item to EFX filter specification.
+ *
+ * @param item[in]
+ *   Item specification. Only virtual subnet ID field is supported.
+ *   If the mask is NULL, default mask will be used.
+ *   Ranging is not supported.
+ * @param efx_spec[in, out]
+ *   EFX filter specification to update.
+ * @param[out] error
+ *   Perform verbose error reporting if not NULL.
+ */
+static int
+sfc_flow_parse_nvgre(const struct rte_flow_item *item,
+                    efx_filter_spec_t *efx_spec,
+                    struct rte_flow_error *error)
+{
+       int rc;
+       const struct rte_flow_item_nvgre *spec = NULL;
+       const struct rte_flow_item_nvgre *mask = NULL;
+       const struct rte_flow_item_nvgre supp_mask = {
+               .tni = { 0xff, 0xff, 0xff }
+       };
+
+       rc = sfc_flow_parse_init(item,
+                                (const void **)&spec,
+                                (const void **)&mask,
+                                &supp_mask,
+                                &rte_flow_item_nvgre_mask,
+                                sizeof(struct rte_flow_item_nvgre),
+                                error);
+       if (rc != 0)
+               return rc;
+
+       rc = sfc_flow_set_match_flags_for_encap_pkts(item, efx_spec,
+                                                    EFX_IPPROTO_GRE, error);
+       if (rc != 0)
+               return rc;
+
+       efx_spec->efs_encap_type = EFX_TUNNEL_PROTOCOL_NVGRE;
+       efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ENCAP_TYPE;
+
+       if (spec == NULL)
+               return 0;
+
+       rc = sfc_flow_set_efx_spec_vni_or_vsid(efx_spec, spec->tni,
+                                              mask->tni, item, error);
+
+       return rc;
+}
+
 static const struct sfc_flow_item sfc_flow_items[] = {
        {
                .type = RTE_FLOW_ITEM_TYPE_VOID,
@@ -739,6 +1036,24 @@ static const struct sfc_flow_item sfc_flow_items[] = {
                .layer = SFC_FLOW_ITEM_L4,
                .parse = sfc_flow_parse_udp,
        },
+       {
+               .type = RTE_FLOW_ITEM_TYPE_VXLAN,
+               .prev_layer = SFC_FLOW_ITEM_L4,
+               .layer = SFC_FLOW_ITEM_START_LAYER,
+               .parse = sfc_flow_parse_vxlan,
+       },
+       {
+               .type = RTE_FLOW_ITEM_TYPE_GENEVE,
+               .prev_layer = SFC_FLOW_ITEM_L4,
+               .layer = SFC_FLOW_ITEM_START_LAYER,
+               .parse = sfc_flow_parse_geneve,
+       },
+       {
+               .type = RTE_FLOW_ITEM_TYPE_NVGRE,
+               .prev_layer = SFC_FLOW_ITEM_L3,
+               .layer = SFC_FLOW_ITEM_START_LAYER,
+               .parse = sfc_flow_parse_nvgre,
+       },
 };
 
 /*
@@ -806,6 +1121,7 @@ sfc_flow_parse_pattern(const struct rte_flow_item pattern[],
 {
        int rc;
        unsigned int prev_layer = SFC_FLOW_ITEM_ANY_LAYER;
+       boolean_t is_ifrm = B_FALSE;
        const struct sfc_flow_item *item;
 
        if (pattern == NULL) {
@@ -837,6 +1153,40 @@ sfc_flow_parse_pattern(const struct rte_flow_item pattern[],
                        return -rte_errno;
                }
 
+               /*
+                * Allow only VOID and ETH pattern items in the inner frame.
+                * Also check that there is only one tunneling protocol.
+                */
+               switch (item->type) {
+               case RTE_FLOW_ITEM_TYPE_VOID:
+               case RTE_FLOW_ITEM_TYPE_ETH:
+                       break;
+
+               case RTE_FLOW_ITEM_TYPE_VXLAN:
+               case RTE_FLOW_ITEM_TYPE_GENEVE:
+               case RTE_FLOW_ITEM_TYPE_NVGRE:
+                       if (is_ifrm) {
+                               rte_flow_error_set(error, EINVAL,
+                                       RTE_FLOW_ERROR_TYPE_ITEM,
+                                       pattern,
+                                       "More than one tunneling protocol");
+                               return -rte_errno;
+                       }
+                       is_ifrm = B_TRUE;
+                       break;
+
+               default:
+                       if (is_ifrm) {
+                               rte_flow_error_set(error, EINVAL,
+                                       RTE_FLOW_ERROR_TYPE_ITEM,
+                                       pattern,
+                                       "There is an unsupported pattern item "
+                                       "in the inner frame");
+                               return -rte_errno;
+                       }
+                       break;
+               }
+
                rc = item->parse(pattern, &flow->spec, error);
                if (rc != 0)
                        return rc;