ethdev: add pre-defined meter policy API
[dpdk.git] / drivers / net / mlx5 / mlx5_flow.c
index 7a6d6f8..05d86b5 100644 (file)
@@ -4342,6 +4342,9 @@ flow_create_split_inner(struct rte_eth_dev *dev,
                dev_flow->handle->mark = 1;
        if (sub_flow)
                *sub_flow = dev_flow;
+#ifdef HAVE_IBV_FLOW_DV_SUPPORT
+       dev_flow->dv.table_id = flow_split_info->table_id;
+#endif
        return flow_drv_translate(dev, dev_flow, attr, items, actions, error);
 }
 
@@ -4363,6 +4366,8 @@ flow_create_split_inner(struct rte_eth_dev *dev,
  *   Parent flow structure pointer.
  * @param[in] fm
  *   Pointer to flow meter structure.
+ * @param[in] attr
+ *   Flow rule attributes.
  * @param[in] items
  *   Pattern specification (list terminated by the END pattern item).
  * @param[out] sfx_items
@@ -4383,6 +4388,7 @@ static uint32_t
 flow_meter_split_prep(struct rte_eth_dev *dev,
                      struct rte_flow *flow,
                      struct mlx5_flow_meter_info *fm,
+                     const struct rte_flow_attr *attr,
                      const struct rte_flow_item items[],
                      struct rte_flow_item sfx_items[],
                      const struct rte_flow_action actions[],
@@ -4400,6 +4406,12 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
        struct mlx5_rte_flow_item_tag *tag_item_mask;
        uint32_t tag_id = 0;
        bool copy_vlan = false;
+       struct rte_flow_action *hw_mtr_action;
+       struct rte_flow_action_jump *jump_data;
+       struct rte_flow_action *action_pre_head = NULL;
+       bool mtr_first = priv->sh->meter_aso_en &&
+                       (attr->egress ||
+                       (attr->transfer && priv->representor_id != UINT16_MAX));
        uint8_t mtr_id_offset = priv->mtr_reg_share ? MLX5_MTR_COLOR_BITS : 0;
        uint8_t mtr_reg_bits = priv->mtr_reg_share ?
                                MLX5_MTR_IDLE_BITS_IN_COLOR_REG : MLX5_REG_BITS;
@@ -4408,29 +4420,39 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
        uint8_t flow_id_bits = 0;
        int shift;
 
+       /* For ASO meter, meter must be before tag in TX direction. */
+       if (mtr_first) {
+               action_pre_head = actions_pre++;
+               /* Leave space for tag action. */
+               tag_action = actions_pre++;
+       }
        /* Prepare the actions for prefix and suffix flow. */
        for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
-               struct rte_flow_action **action_cur = NULL;
+               struct rte_flow_action *action_cur = NULL;
 
                switch (actions->type) {
                case RTE_FLOW_ACTION_TYPE_METER:
-                       /* Add the extra tag action first. */
-                       tag_action = actions_pre++;
-                       action_cur = &actions_pre;
+                       if (mtr_first) {
+                               action_cur = action_pre_head;
+                       } else {
+                               /* Leave space for tag action. */
+                               tag_action = actions_pre++;
+                               action_cur = actions_pre++;
+                       }
                        break;
                case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
                case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
-                       action_cur = &actions_pre;
+                       action_cur = actions_pre++;
                        break;
                case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
                        raw_encap = actions->conf;
                        if (raw_encap->size < MLX5_ENCAPSULATION_DECISION_SIZE)
-                               action_cur = &actions_pre;
+                               action_cur = actions_pre++;
                        break;
                case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
                        raw_decap = actions->conf;
                        if (raw_decap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
-                               action_cur = &actions_pre;
+                               action_cur = actions_pre++;
                        break;
                case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
                case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
@@ -4440,14 +4462,32 @@ flow_meter_split_prep(struct rte_eth_dev *dev,
                        break;
                }
                if (!action_cur)
-                       action_cur = &actions_sfx;
-               memcpy(*action_cur, actions, sizeof(struct rte_flow_action));
-               (*action_cur)++;
+                       action_cur = actions_sfx++;
+               memcpy(action_cur, actions, sizeof(struct rte_flow_action));
        }
        /* Add end action to the actions. */
        actions_sfx->type = RTE_FLOW_ACTION_TYPE_END;
-       actions_pre->type = RTE_FLOW_ACTION_TYPE_END;
-       actions_pre++;
+       if (priv->sh->meter_aso_en) {
+               /**
+                * For ASO meter, need to add an extra jump action explicitly,
+                * to jump from meter to policer table.
+                */
+               hw_mtr_action = actions_pre;
+               hw_mtr_action->type = RTE_FLOW_ACTION_TYPE_JUMP;
+               actions_pre++;
+               actions_pre->type = RTE_FLOW_ACTION_TYPE_END;
+               actions_pre++;
+               jump_data = (struct rte_flow_action_jump *)actions_pre;
+               jump_data->group = attr->transfer ?
+                               (MLX5_FLOW_TABLE_LEVEL_METER - 1) :
+                                MLX5_FLOW_TABLE_LEVEL_METER;
+               hw_mtr_action->conf = jump_data;
+               actions_pre = (struct rte_flow_action *)(jump_data + 1);
+       } else {
+               actions_pre->type = RTE_FLOW_ACTION_TYPE_END;
+               actions_pre++;
+       }
+       /* Generate meter flow_id only if support multiple flows per meter. */
        mlx5_ipool_malloc(fm->flow_ipool, &tag_id);
        if (!tag_id)
                return rte_flow_error_set(error, ENOMEM,
@@ -5272,10 +5312,11 @@ flow_create_split_meter(struct rte_eth_dev *dev,
                        flow->meter = mtr_idx;
                }
                wks->fm = fm;
-               /* The prefix actions: meter, decap, encap, tag, end. */
-               act_size = sizeof(struct rte_flow_action) * (actions_n + 5) +
-                          sizeof(struct mlx5_rte_flow_action_set_tag);
-               /* The suffix items: tag, vlan, port id, end. */
+               /* Prefix actions: meter, decap, encap, tag, jump, end. */
+               act_size = sizeof(struct rte_flow_action) * (actions_n + 6) +
+                          sizeof(struct mlx5_rte_flow_action_set_tag) +
+                          sizeof(struct rte_flow_action_jump);
+               /* Suffix items: tag, vlan, port id, end. */
 #define METER_SUFFIX_ITEM 4
                item_size = sizeof(struct rte_flow_item) * METER_SUFFIX_ITEM +
                            sizeof(struct mlx5_rte_flow_item_tag) * 2;
@@ -5289,8 +5330,8 @@ flow_create_split_meter(struct rte_eth_dev *dev,
                sfx_items = (struct rte_flow_item *)((char *)sfx_actions +
                             act_size);
                pre_actions = sfx_actions + actions_n;
-               mtr_tag_id = flow_meter_split_prep(dev, flow, fm, items,
-                                                  sfx_items, actions,
+               mtr_tag_id = flow_meter_split_prep(dev, flow, fm, &sfx_attr,
+                                                  items, sfx_items, actions,
                                                   sfx_actions, pre_actions,
                                                   error);
                if (!mtr_tag_id) {
@@ -5459,8 +5500,7 @@ flow_create_split_sample(struct rte_eth_dev *dev,
                                                struct mlx5_flow_tbl_data_entry,
                                                tbl);
                        sfx_attr.group = sfx_attr.transfer ?
-                                               (sfx_tbl_data->table_id - 1) :
-                                               sfx_tbl_data->table_id;
+                       (sfx_tbl_data->level - 1) : sfx_tbl_data->level;
                } else {
                        MLX5_ASSERT(attr->transfer);
                        sfx_attr.group = jump_table;
@@ -5660,7 +5700,8 @@ flow_list_create(struct rte_eth_dev *dev, uint32_t *list,
                .skip_scale = 0,
                .flow_idx = 0,
                .prefix_mark = 0,
-               .prefix_layers = 0
+               .prefix_layers = 0,
+               .table_id = 0
        };
        int ret;
 
@@ -6614,52 +6655,6 @@ mlx5_flow_destroy_mtr_tbls(struct rte_eth_dev *dev,
        return fops->destroy_mtr_tbls(dev, tbls);
 }
 
-/**
- * Prepare policer rules.
- *
- * @param[in] dev
- *   Pointer to Ethernet device.
- * @param[in] fm
- *   Pointer to flow meter structure.
- * @param[in] attr
- *   Pointer to flow attributes.
- *
- * @return
- *   0 on success, -1 otherwise.
- */
-int
-mlx5_flow_prepare_policer_rules(struct rte_eth_dev *dev,
-                              struct mlx5_flow_meter_info *fm,
-                              const struct rte_flow_attr *attr)
-{
-       const struct mlx5_flow_driver_ops *fops;
-
-       fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
-       return fops->prepare_policer_rules(dev, fm, attr);
-}
-
-/**
- * Destroy policer rules.
- *
- * @param[in] fm
- *   Pointer to flow meter structure.
- * @param[in] attr
- *   Pointer to flow attributes.
- *
- * @return
- *   0 on success, -1 otherwise.
- */
-int
-mlx5_flow_destroy_policer_rules(struct rte_eth_dev *dev,
-                               struct mlx5_flow_meter_info *fm,
-                               const struct rte_flow_attr *attr)
-{
-       const struct mlx5_flow_driver_ops *fops;
-
-       fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
-       return fops->destroy_policer_rules(dev, fm, attr);
-}
-
 /**
  * Allocate the needed aso flow meter id.
  *
@@ -7746,10 +7741,12 @@ tunnel_mark_decode(struct rte_eth_dev *dev, uint32_t mark)
        union tunnel_offload_mark mbits = { .val = mark };
        union mlx5_flow_tbl_key table_key = {
                {
-                       .table_id = tunnel_id_to_flow_tbl(mbits.table_id),
+                       .level = tunnel_id_to_flow_tbl(mbits.table_id),
+                       .id = 0,
+                       .reserved = 0,
                        .dummy = 0,
-                       .domain = !!mbits.transfer,
-                       .direction = 0,
+                       .is_fdb = !!mbits.transfer,
+                       .is_egress = 0,
                }
        };
        he = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64, NULL);