net/bnxt: handle cleanup if flow creation fails
[dpdk.git] / drivers / net / bnxt / bnxt_flow.c
index 33f4b07..1ed4c47 100644 (file)
@@ -177,6 +177,14 @@ bnxt_validate_and_parse_flow_type(struct bnxt *bp,
                        return -rte_errno;
                }
 
+               if (!item->spec || !item->mask) {
+                       rte_flow_error_set(error, EINVAL,
+                                          RTE_FLOW_ERROR_TYPE_ITEM,
+                                          item,
+                                          "spec/mask is NULL");
+                       return -rte_errno;
+               }
+
                switch (item->type) {
                case RTE_FLOW_ITEM_TYPE_ANY:
                        inner =
@@ -219,6 +227,14 @@ bnxt_validate_and_parse_flow_type(struct bnxt *bp,
                        }
 
                        if (rte_is_broadcast_ether_addr(&eth_mask->dst)) {
+                               if (!rte_is_unicast_ether_addr(&eth_spec->dst)) {
+                                       rte_flow_error_set(error,
+                                                          EINVAL,
+                                                          RTE_FLOW_ERROR_TYPE_ITEM,
+                                                          item,
+                                                          "DMAC is invalid");
+                                       return -rte_errno;
+                               }
                                rte_memcpy(filter->dst_macaddr,
                                           &eth_spec->dst, RTE_ETHER_ADDR_LEN);
                                en |= use_ntuple ?
@@ -233,6 +249,14 @@ bnxt_validate_and_parse_flow_type(struct bnxt *bp,
                        }
 
                        if (rte_is_broadcast_ether_addr(&eth_mask->src)) {
+                               if (!rte_is_unicast_ether_addr(&eth_spec->src)) {
+                                       rte_flow_error_set(error,
+                                                          EINVAL,
+                                                          RTE_FLOW_ERROR_TYPE_ITEM,
+                                                          item,
+                                                          "SMAC is invalid");
+                                       return -rte_errno;
+                               }
                                rte_memcpy(filter->src_macaddr,
                                           &eth_spec->src, RTE_ETHER_ADDR_LEN);
                                en |= use_ntuple ?
@@ -956,11 +980,11 @@ bnxt_validate_and_parse_flow(struct rte_eth_dev *dev,
                bnxt_flow_non_void_action(actions);
        struct bnxt *bp = dev->data->dev_private;
        struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
+       struct bnxt_vnic_info *vnic = NULL, *vnic0 = NULL;
        const struct rte_flow_action_queue *act_q;
        const struct rte_flow_action_vf *act_vf;
        struct bnxt_filter_info *filter1 = NULL;
        const struct rte_flow_action_rss *rss;
-       struct bnxt_vnic_info *vnic, *vnic0;
        struct bnxt_rx_queue *rxq = NULL;
        int dflt_vnic, vnic_id;
        unsigned int rss_idx;
@@ -1061,8 +1085,15 @@ bnxt_validate_and_parse_flow(struct rte_eth_dev *dev,
                PMD_DRV_LOG(DEBUG, "VNIC found\n");
 
                rc = bnxt_vnic_prep(bp, vnic);
-               if (rc)
+               if (rc)  {
+                       rte_flow_error_set(error,
+                                          EINVAL,
+                                          RTE_FLOW_ERROR_TYPE_ACTION,
+                                          act,
+                                          "VNIC prep fail");
+                       rc = -rte_errno;
                        goto ret;
+               }
 
                PMD_DRV_LOG(DEBUG,
                            "vnic[%d] = %p vnic->fw_grp_ids = %p\n",
@@ -1075,7 +1106,12 @@ use_vnic:
                filter->dst_id = vnic->fw_vnic_id;
                filter1 = bnxt_get_l2_filter(bp, filter, vnic);
                if (filter1 == NULL) {
-                       rc = -ENOSPC;
+                       rte_flow_error_set(error,
+                                          ENOSPC,
+                                          RTE_FLOW_ERROR_TYPE_ACTION,
+                                          act,
+                                          "Filter not available");
+                       rc = -rte_errno;
                        goto ret;
                }
 
@@ -1103,7 +1139,12 @@ use_vnic:
                vnic0 = &bp->vnic_info[0];
                filter1 = bnxt_get_l2_filter(bp, filter, vnic0);
                if (filter1 == NULL) {
-                       rc = -ENOSPC;
+                       rte_flow_error_set(error,
+                                          ENOSPC,
+                                          RTE_FLOW_ERROR_TYPE_ACTION,
+                                          act,
+                                          "New filter not available");
+                       rc = -rte_errno;
                        goto ret;
                }
 
@@ -1166,6 +1207,11 @@ use_vnic:
                vnic0 = &bp->vnic_info[0];
                filter1 = bnxt_get_l2_filter(bp, filter, vnic0);
                if (filter1 == NULL) {
+                       rte_flow_error_set(error,
+                                          ENOSPC,
+                                          RTE_FLOW_ERROR_TYPE_ACTION,
+                                          act,
+                                          "New filter not available");
                        rc = -ENOSPC;
                        goto ret;
                }
@@ -1262,8 +1308,15 @@ use_vnic:
                vnic->func_default = 0; //This is not a default VNIC.
 
                rc = bnxt_vnic_prep(bp, vnic);
-               if (rc)
+               if (rc) {
+                       rte_flow_error_set(error,
+                                          EINVAL,
+                                          RTE_FLOW_ERROR_TYPE_ACTION,
+                                          act,
+                                          "VNIC prep fail");
+                       rc = -rte_errno;
                        goto ret;
+               }
 
                PMD_DRV_LOG(DEBUG,
                            "vnic[%d] = %p vnic->fw_grp_ids = %p\n",
@@ -1318,6 +1371,11 @@ vnic_found:
                filter->dst_id = vnic->fw_vnic_id;
                filter1 = bnxt_get_l2_filter(bp, filter, vnic);
                if (filter1 == NULL) {
+                       rte_flow_error_set(error,
+                                          ENOSPC,
+                                          RTE_FLOW_ERROR_TYPE_ACTION,
+                                          act,
+                                          "New filter not available");
                        rc = -ENOSPC;
                        goto ret;
                }
@@ -1351,10 +1409,40 @@ done:
                rc = -rte_errno;
                goto ret;
        }
+
+       return rc;
 ret:
+
+       //TODO: Cleanup according to ACTION TYPE.
+       if (rte_errno)  {
+               if (vnic && STAILQ_EMPTY(&vnic->filter))
+                       vnic->rx_queue_cnt = 0;
+
+               if (rxq && !vnic->rx_queue_cnt)
+                       rxq->vnic = &bp->vnic_info[0];
+       }
        return rc;
 }
 
+static
+struct bnxt_vnic_info *find_matching_vnic(struct bnxt *bp,
+                                         struct bnxt_filter_info *filter)
+{
+       struct bnxt_vnic_info *vnic = NULL;
+       unsigned int i;
+
+       for (i = 0; i < bp->max_vnics; i++) {
+               vnic = &bp->vnic_info[i];
+               if (vnic->fw_vnic_id != INVALID_VNIC_ID &&
+                   filter->dst_id == vnic->fw_vnic_id) {
+                       PMD_DRV_LOG(DEBUG, "Found matching VNIC Id %d\n",
+                                   vnic->ff_pool_idx);
+                       return vnic;
+               }
+       }
+       return NULL;
+}
+
 static int
 bnxt_flow_validate(struct rte_eth_dev *dev,
                   const struct rte_flow_attr *attr,
@@ -1363,6 +1451,7 @@ bnxt_flow_validate(struct rte_eth_dev *dev,
                   struct rte_flow_error *error)
 {
        struct bnxt *bp = dev->data->dev_private;
+       struct bnxt_vnic_info *vnic = NULL;
        struct bnxt_filter_info *filter;
        int ret = 0;
 
@@ -1378,6 +1467,29 @@ bnxt_flow_validate(struct rte_eth_dev *dev,
 
        ret = bnxt_validate_and_parse_flow(dev, pattern, actions, attr,
                                           error, filter);
+       if (ret)
+               goto exit;
+
+       vnic = find_matching_vnic(bp, filter);
+       if (vnic) {
+               if (STAILQ_EMPTY(&vnic->filter)) {
+                       rte_free(vnic->fw_grp_ids);
+                       bnxt_hwrm_vnic_ctx_free(bp, vnic);
+                       bnxt_hwrm_vnic_free(bp, vnic);
+                       vnic->rx_queue_cnt = 0;
+                       bp->nr_vnics--;
+                       PMD_DRV_LOG(DEBUG, "Free VNIC\n");
+               }
+       }
+
+       if (filter->filter_type == HWRM_CFA_EM_FILTER)
+               bnxt_hwrm_clear_em_filter(bp, filter);
+       else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
+               bnxt_hwrm_clear_ntuple_filter(bp, filter);
+       else
+               bnxt_hwrm_clear_l2_filter(bp, filter);
+
+exit:
        /* No need to hold on to this filter if we are just validating flow */
        filter->fw_l2_filter_id = UINT64_MAX;
        bnxt_free_filter(bp, filter);
@@ -1414,7 +1526,7 @@ bnxt_match_filter(struct bnxt *bp, struct bnxt_filter_info *nf)
        struct rte_flow *flow;
        int i;
 
-       for (i = bp->max_vnics; i >= 0; i--) {
+       for (i = bp->max_vnics - 1; i >= 0; i--) {
                struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
 
                if (vnic->fw_vnic_id == INVALID_VNIC_ID)
@@ -1484,7 +1596,6 @@ bnxt_flow_create(struct rte_eth_dev *dev,
        struct bnxt_filter_info *filter;
        bool update_flow = false;
        struct rte_flow *flow;
-       unsigned int i;
        int ret = 0;
        uint32_t tun_type;
 
@@ -1495,6 +1606,15 @@ bnxt_flow_create(struct rte_eth_dev *dev,
                return NULL;
        }
 
+       if (!dev->data->dev_started) {
+               rte_flow_error_set(error,
+                                  EINVAL,
+                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
+                                  NULL,
+                                  "Device must be started");
+               return NULL;
+       }
+
        flow = rte_zmalloc("bnxt_flow", sizeof(struct rte_flow), 0);
        if (!flow) {
                rte_flow_error_set(error, ENOMEM,
@@ -1585,15 +1705,7 @@ bnxt_flow_create(struct rte_eth_dev *dev,
                ret = bnxt_hwrm_set_ntuple_filter(bp, filter->dst_id, filter);
        }
 
-       for (i = 0; i < bp->max_vnics; i++) {
-               vnic = &bp->vnic_info[i];
-               if (vnic->fw_vnic_id != INVALID_VNIC_ID &&
-                   filter->dst_id == vnic->fw_vnic_id) {
-                       PMD_DRV_LOG(ERR, "Found matching VNIC Id %d\n",
-                                   vnic->ff_pool_idx);
-                       break;
-               }
-       }
+       vnic = find_matching_vnic(bp, filter);
 done:
        if (!ret || update_flow) {
                flow->filter = filter;
@@ -1734,26 +1846,9 @@ done:
                 */
                if (vnic && STAILQ_EMPTY(&vnic->flow_list)) {
                        rte_free(vnic->fw_grp_ids);
-                       if (vnic->rx_queue_cnt > 1) {
-                               if (BNXT_CHIP_THOR(bp)) {
-                                       int j;
-
-                                       for (j = 0; j < vnic->num_lb_ctxts;
-                                            j++) {
-                                               bnxt_hwrm_vnic_ctx_free(bp,
-                                                                        vnic,
-                                                                        vnic->fw_grp_ids[j]);
-                                               vnic->fw_grp_ids[j] =
-                                                       INVALID_HW_RING_ID;
-                                       }
-                                       vnic->num_lb_ctxts = 0;
-                               } else {
-                                       bnxt_hwrm_vnic_ctx_free(bp,
-                                                               vnic,
-                                                               vnic->rss_rule);
-                                       vnic->rss_rule = INVALID_HW_RING_ID;
-                               }
-                       }
+                       if (vnic->rx_queue_cnt > 1)
+                               bnxt_hwrm_vnic_ctx_free(bp, vnic);
+
                        bnxt_hwrm_vnic_free(bp, vnic);
                        vnic->rx_queue_cnt = 0;
                        bp->nr_vnics--;