+struct softnic_mtr_meter_policy *
+softnic_mtr_meter_policy_find(struct pmd_internals *p,
+ uint32_t meter_policy_id)
+{
+ struct softnic_mtr_meter_policy_list *mpl = &p->mtr.meter_policies;
+ struct softnic_mtr_meter_policy *mp;
+
+ TAILQ_FOREACH(mp, mpl, node)
+ if (meter_policy_id == mp->meter_policy_id)
+ return mp;
+
+ return NULL;
+}
+
+/* MTR meter policy add */
+static int
+pmd_mtr_meter_policy_add(struct rte_eth_dev *dev,
+ uint32_t meter_policy_id,
+ struct rte_mtr_meter_policy_params *policy,
+ struct rte_mtr_error *error)
+{
+ struct pmd_internals *p = dev->data->dev_private;
+ struct softnic_mtr_meter_policy_list *mpl = &p->mtr.meter_policies;
+ struct softnic_mtr_meter_policy *mp;
+ const struct rte_flow_action *act;
+ const struct rte_flow_action_meter_color *recolor;
+ uint32_t i;
+ bool valid_act_found;
+
+ if (policy == NULL)
+ return -rte_mtr_error_set(error,
+ EINVAL,
+ RTE_MTR_ERROR_TYPE_METER_POLICY,
+ NULL,
+ "Null meter policy invalid");
+
+ /* Meter policy must not exist. */
+ mp = softnic_mtr_meter_policy_find(p, meter_policy_id);
+ if (mp != NULL)
+ return -rte_mtr_error_set(error,
+ EINVAL,
+ RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
+ NULL,
+ "Meter policy already exists");
+
+ for (i = 0; i < RTE_COLORS; i++) {
+ if (policy->actions[i] == NULL)
+ return -rte_mtr_error_set(error,
+ EINVAL,
+ RTE_MTR_ERROR_TYPE_METER_POLICY,
+ NULL,
+ "Null action list");
+ for (act = policy->actions[i], valid_act_found = false;
+ act->type != RTE_FLOW_ACTION_TYPE_END; act++) {
+ if (act->type == RTE_FLOW_ACTION_TYPE_VOID)
+ continue;
+ /*
+ * Support one (and one only) of
+ * METER_COLOR or DROP action.
+ */
+ if ((act->type != RTE_FLOW_ACTION_TYPE_METER_COLOR &&
+ act->type != RTE_FLOW_ACTION_TYPE_DROP) ||
+ valid_act_found)
+ return -rte_mtr_error_set(error,
+ EINVAL,
+ RTE_MTR_ERROR_TYPE_METER_POLICY,
+ NULL,
+ "Action invalid");
+ valid_act_found = true;
+ }
+ if (!valid_act_found)
+ return -rte_mtr_error_set(error,
+ EINVAL,
+ RTE_MTR_ERROR_TYPE_METER_POLICY,
+ NULL,
+ "No valid action found");
+ }
+
+ /* Memory allocation */
+ mp = calloc(1, sizeof(struct softnic_mtr_meter_policy));
+ if (mp == NULL)
+ return -rte_mtr_error_set(error,
+ ENOMEM,
+ RTE_MTR_ERROR_TYPE_UNSPECIFIED,
+ NULL,
+ "Memory alloc failed");
+
+ /* Fill in */
+ mp->meter_policy_id = meter_policy_id;
+ for (i = 0; i < RTE_COLORS; i++) {
+ mp->policer[i] = RTE_TABLE_ACTION_POLICER_DROP;
+ act = policy->actions[i];
+ if (!act)
+ continue;
+ if (act->type == RTE_FLOW_ACTION_TYPE_METER_COLOR) {
+ recolor = act->conf;
+ switch (recolor->color) {
+ case RTE_COLOR_GREEN:
+ mp->policer[i] =
+ RTE_TABLE_ACTION_POLICER_COLOR_GREEN;
+ break;
+ case RTE_COLOR_YELLOW:
+ mp->policer[i] =
+ RTE_TABLE_ACTION_POLICER_COLOR_YELLOW;
+ break;
+ case RTE_COLOR_RED:
+ mp->policer[i] =
+ RTE_TABLE_ACTION_POLICER_COLOR_RED;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ /* Add to list */
+ TAILQ_INSERT_TAIL(mpl, mp, node);
+
+ return 0;
+}
+
+/* MTR meter policy delete */
+static int
+pmd_mtr_meter_policy_delete(struct rte_eth_dev *dev,
+ uint32_t meter_policy_id,
+ struct rte_mtr_error *error)
+{
+ struct pmd_internals *p = dev->data->dev_private;
+ struct softnic_mtr_meter_policy *mp;
+
+ /* Meter policy must exist */
+ mp = softnic_mtr_meter_policy_find(p, meter_policy_id);
+ if (mp == NULL)
+ return -rte_mtr_error_set(error,
+ EINVAL,
+ RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
+ NULL,
+ "Meter policy id invalid");
+
+ /* Check unused */
+ if (mp->n_users)
+ return -rte_mtr_error_set(error,
+ EBUSY,
+ RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
+ NULL,
+ "Meter policy in use");
+
+ /* Remove from list */
+ TAILQ_REMOVE(&p->mtr.meter_policies, mp, node);
+ free(mp);
+
+ return 0;
+}
+