+ for (i = 0; rss_type_table[i].str; i++) {
+ if ((rss_conf->types &
+ rss_type_table[i].rss_type) ==
+ rss_type_table[i].rss_type &&
+ rss_type_table[i].rss_type != 0)
+ printf(" %s\n", rss_type_table[i].str);
+ }
+}
+
+static struct port_indirect_action *
+action_get_by_id(portid_t port_id, uint32_t id)
+{
+ struct rte_port *port;
+ struct port_indirect_action **ppia;
+ struct port_indirect_action *pia = NULL;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return NULL;
+ port = &ports[port_id];
+ ppia = &port->actions_list;
+ while (*ppia) {
+ if ((*ppia)->id == id) {
+ pia = *ppia;
+ break;
+ }
+ ppia = &(*ppia)->next;
+ }
+ if (!pia)
+ fprintf(stderr,
+ "Failed to find indirect action #%u on port %u\n",
+ id, port_id);
+ return pia;
+}
+
+static int
+action_alloc(portid_t port_id, uint32_t id,
+ struct port_indirect_action **action)
+{
+ struct rte_port *port;
+ struct port_indirect_action **ppia;
+ struct port_indirect_action *pia = NULL;
+
+ *action = NULL;
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ if (id == UINT32_MAX) {
+ /* taking first available ID */
+ if (port->actions_list) {
+ if (port->actions_list->id == UINT32_MAX - 1) {
+ fprintf(stderr,
+ "Highest indirect action ID is already assigned, delete it first\n");
+ return -ENOMEM;
+ }
+ id = port->actions_list->id + 1;
+ } else {
+ id = 0;
+ }
+ }
+ pia = calloc(1, sizeof(*pia));
+ if (!pia) {
+ fprintf(stderr,
+ "Allocation of port %u indirect action failed\n",
+ port_id);
+ return -ENOMEM;
+ }
+ ppia = &port->actions_list;
+ while (*ppia && (*ppia)->id > id)
+ ppia = &(*ppia)->next;
+ if (*ppia && (*ppia)->id == id) {
+ fprintf(stderr,
+ "Indirect action #%u is already assigned, delete it first\n",
+ id);
+ free(pia);
+ return -EINVAL;
+ }
+ pia->next = *ppia;
+ pia->id = id;
+ *ppia = pia;
+ *action = pia;
+ return 0;
+}
+
+static int
+template_alloc(uint32_t id, struct port_template **template,
+ struct port_template **list)
+{
+ struct port_template *lst = *list;
+ struct port_template **ppt;
+ struct port_template *pt = NULL;
+
+ *template = NULL;
+ if (id == UINT32_MAX) {
+ /* taking first available ID */
+ if (lst) {
+ if (lst->id == UINT32_MAX - 1) {
+ printf("Highest template ID is already"
+ " assigned, delete it first\n");
+ return -ENOMEM;
+ }
+ id = lst->id + 1;
+ } else {
+ id = 0;
+ }
+ }
+ pt = calloc(1, sizeof(*pt));
+ if (!pt) {
+ printf("Allocation of port template failed\n");
+ return -ENOMEM;
+ }
+ ppt = list;
+ while (*ppt && (*ppt)->id > id)
+ ppt = &(*ppt)->next;
+ if (*ppt && (*ppt)->id == id) {
+ printf("Template #%u is already assigned,"
+ " delete it first\n", id);
+ free(pt);
+ return -EINVAL;
+ }
+ pt->next = *ppt;
+ pt->id = id;
+ *ppt = pt;
+ *template = pt;
+ return 0;
+}
+
+static int
+table_alloc(uint32_t id, struct port_table **table,
+ struct port_table **list)
+{
+ struct port_table *lst = *list;
+ struct port_table **ppt;
+ struct port_table *pt = NULL;
+
+ *table = NULL;
+ if (id == UINT32_MAX) {
+ /* taking first available ID */
+ if (lst) {
+ if (lst->id == UINT32_MAX - 1) {
+ printf("Highest table ID is already"
+ " assigned, delete it first\n");
+ return -ENOMEM;
+ }
+ id = lst->id + 1;
+ } else {
+ id = 0;
+ }
+ }
+ pt = calloc(1, sizeof(*pt));
+ if (!pt) {
+ printf("Allocation of table failed\n");
+ return -ENOMEM;
+ }
+ ppt = list;
+ while (*ppt && (*ppt)->id > id)
+ ppt = &(*ppt)->next;
+ if (*ppt && (*ppt)->id == id) {
+ printf("Table #%u is already assigned,"
+ " delete it first\n", id);
+ free(pt);
+ return -EINVAL;
+ }
+ pt->next = *ppt;
+ pt->id = id;
+ *ppt = pt;
+ *table = pt;
+ return 0;
+}
+
+/** Get info about flow management resources. */
+int
+port_flow_get_info(portid_t port_id)
+{
+ struct rte_flow_port_info port_info;
+ struct rte_flow_queue_info queue_info;
+ struct rte_flow_error error;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x99, sizeof(error));
+ memset(&port_info, 0, sizeof(port_info));
+ memset(&queue_info, 0, sizeof(queue_info));
+ if (rte_flow_info_get(port_id, &port_info, &queue_info, &error))
+ return port_flow_complain(&error);
+ printf("Flow engine resources on port %u:\n"
+ "Number of queues: %d\n"
+ "Size of queues: %d\n"
+ "Number of counters: %d\n"
+ "Number of aging objects: %d\n"
+ "Number of meter actions: %d\n",
+ port_id, port_info.max_nb_queues,
+ queue_info.max_size,
+ port_info.max_nb_counters,
+ port_info.max_nb_aging_objects,
+ port_info.max_nb_meters);
+ return 0;
+}
+
+/** Configure flow management resources. */
+int
+port_flow_configure(portid_t port_id,
+ const struct rte_flow_port_attr *port_attr,
+ uint16_t nb_queue,
+ const struct rte_flow_queue_attr *queue_attr)
+{
+ struct rte_port *port;
+ struct rte_flow_error error;
+ const struct rte_flow_queue_attr *attr_list[nb_queue];
+ int std_queue;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ port->queue_nb = nb_queue;
+ port->queue_sz = queue_attr->size;
+ for (std_queue = 0; std_queue < nb_queue; std_queue++)
+ attr_list[std_queue] = queue_attr;
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x66, sizeof(error));
+ if (rte_flow_configure(port_id, port_attr, nb_queue, attr_list, &error))
+ return port_flow_complain(&error);
+ printf("Configure flows on port %u: "
+ "number of queues %d with %d elements\n",
+ port_id, nb_queue, queue_attr->size);
+ return 0;
+}
+
+/** Create indirect action */
+int
+port_action_handle_create(portid_t port_id, uint32_t id,
+ const struct rte_flow_indir_action_conf *conf,
+ const struct rte_flow_action *action)
+{
+ struct port_indirect_action *pia;
+ int ret;
+ struct rte_flow_error error;
+
+ ret = action_alloc(port_id, id, &pia);
+ if (ret)
+ return ret;
+ if (action->type == RTE_FLOW_ACTION_TYPE_AGE) {
+ struct rte_flow_action_age *age =
+ (struct rte_flow_action_age *)(uintptr_t)(action->conf);
+
+ pia->age_type = ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION;
+ age->context = &pia->age_type;
+ } else if (action->type == RTE_FLOW_ACTION_TYPE_CONNTRACK) {
+ struct rte_flow_action_conntrack *ct =
+ (struct rte_flow_action_conntrack *)(uintptr_t)(action->conf);
+
+ memcpy(ct, &conntrack_context, sizeof(*ct));
+ }
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x22, sizeof(error));
+ pia->handle = rte_flow_action_handle_create(port_id, conf, action,
+ &error);
+ if (!pia->handle) {
+ uint32_t destroy_id = pia->id;
+ port_action_handle_destroy(port_id, 1, &destroy_id);
+ return port_flow_complain(&error);
+ }
+ pia->type = action->type;
+ printf("Indirect action #%u created\n", pia->id);
+ return 0;
+}
+
+/** Destroy indirect action */
+int
+port_action_handle_destroy(portid_t port_id,
+ uint32_t n,
+ const uint32_t *actions)
+{
+ struct rte_port *port;
+ struct port_indirect_action **tmp;
+ uint32_t c = 0;
+ int ret = 0;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ tmp = &port->actions_list;
+ while (*tmp) {
+ uint32_t i;
+
+ for (i = 0; i != n; ++i) {
+ struct rte_flow_error error;
+ struct port_indirect_action *pia = *tmp;
+
+ if (actions[i] != pia->id)
+ continue;
+ /*
+ * Poisoning to make sure PMDs update it in case
+ * of error.
+ */
+ memset(&error, 0x33, sizeof(error));
+
+ if (pia->handle && rte_flow_action_handle_destroy(
+ port_id, pia->handle, &error)) {
+ ret = port_flow_complain(&error);
+ continue;
+ }
+ *tmp = pia->next;
+ printf("Indirect action #%u destroyed\n", pia->id);
+ free(pia);
+ break;
+ }
+ if (i == n)
+ tmp = &(*tmp)->next;
+ ++c;
+ }
+ return ret;
+}
+
+
+/** Get indirect action by port + id */
+struct rte_flow_action_handle *
+port_action_handle_get_by_id(portid_t port_id, uint32_t id)
+{
+
+ struct port_indirect_action *pia = action_get_by_id(port_id, id);
+
+ return (pia) ? pia->handle : NULL;
+}
+
+/** Update indirect action */
+int
+port_action_handle_update(portid_t port_id, uint32_t id,
+ const struct rte_flow_action *action)
+{
+ struct rte_flow_error error;
+ struct rte_flow_action_handle *action_handle;
+ struct port_indirect_action *pia;
+ const void *update;
+
+ action_handle = port_action_handle_get_by_id(port_id, id);
+ if (!action_handle)
+ return -EINVAL;
+ pia = action_get_by_id(port_id, id);
+ if (!pia)
+ return -EINVAL;
+ switch (pia->type) {
+ case RTE_FLOW_ACTION_TYPE_CONNTRACK:
+ update = action->conf;
+ break;
+ default:
+ update = action;
+ break;
+ }
+ if (rte_flow_action_handle_update(port_id, action_handle, update,
+ &error)) {
+ return port_flow_complain(&error);
+ }
+ printf("Indirect action #%u updated\n", id);
+ return 0;
+}
+
+int
+port_action_handle_query(portid_t port_id, uint32_t id)
+{
+ struct rte_flow_error error;
+ struct port_indirect_action *pia;
+ union {
+ struct rte_flow_query_count count;
+ struct rte_flow_query_age age;
+ struct rte_flow_action_conntrack ct;
+ } query;
+
+ pia = action_get_by_id(port_id, id);
+ if (!pia)
+ return -EINVAL;
+ switch (pia->type) {
+ case RTE_FLOW_ACTION_TYPE_AGE:
+ case RTE_FLOW_ACTION_TYPE_COUNT:
+ break;
+ default:
+ fprintf(stderr,
+ "Indirect action %u (type: %d) on port %u doesn't support query\n",
+ id, pia->type, port_id);
+ return -ENOTSUP;
+ }
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x55, sizeof(error));
+ memset(&query, 0, sizeof(query));
+ if (rte_flow_action_handle_query(port_id, pia->handle, &query, &error))
+ return port_flow_complain(&error);
+ switch (pia->type) {
+ case RTE_FLOW_ACTION_TYPE_AGE:
+ printf("Indirect AGE action:\n"
+ " aged: %u\n"
+ " sec_since_last_hit_valid: %u\n"
+ " sec_since_last_hit: %" PRIu32 "\n",
+ query.age.aged,
+ query.age.sec_since_last_hit_valid,
+ query.age.sec_since_last_hit);
+ break;
+ case RTE_FLOW_ACTION_TYPE_COUNT:
+ printf("Indirect COUNT action:\n"
+ " hits_set: %u\n"
+ " bytes_set: %u\n"
+ " hits: %" PRIu64 "\n"
+ " bytes: %" PRIu64 "\n",
+ query.count.hits_set,
+ query.count.bytes_set,
+ query.count.hits,
+ query.count.bytes);
+ break;
+ case RTE_FLOW_ACTION_TYPE_CONNTRACK:
+ printf("Conntrack Context:\n"
+ " Peer: %u, Flow dir: %s, Enable: %u\n"
+ " Live: %u, SACK: %u, CACK: %u\n"
+ " Packet dir: %s, Liberal: %u, State: %u\n"
+ " Factor: %u, Retrans: %u, TCP flags: %u\n"
+ " Last Seq: %u, Last ACK: %u\n"
+ " Last Win: %u, Last End: %u\n",
+ query.ct.peer_port,
+ query.ct.is_original_dir ? "Original" : "Reply",
+ query.ct.enable, query.ct.live_connection,
+ query.ct.selective_ack, query.ct.challenge_ack_passed,
+ query.ct.last_direction ? "Original" : "Reply",
+ query.ct.liberal_mode, query.ct.state,
+ query.ct.max_ack_window, query.ct.retransmission_limit,
+ query.ct.last_index, query.ct.last_seq,
+ query.ct.last_ack, query.ct.last_window,
+ query.ct.last_end);
+ printf(" Original Dir:\n"
+ " scale: %u, fin: %u, ack seen: %u\n"
+ " unacked data: %u\n Sent end: %u,"
+ " Reply end: %u, Max win: %u, Max ACK: %u\n",
+ query.ct.original_dir.scale,
+ query.ct.original_dir.close_initiated,
+ query.ct.original_dir.last_ack_seen,
+ query.ct.original_dir.data_unacked,
+ query.ct.original_dir.sent_end,
+ query.ct.original_dir.reply_end,
+ query.ct.original_dir.max_win,
+ query.ct.original_dir.max_ack);
+ printf(" Reply Dir:\n"
+ " scale: %u, fin: %u, ack seen: %u\n"
+ " unacked data: %u\n Sent end: %u,"
+ " Reply end: %u, Max win: %u, Max ACK: %u\n",
+ query.ct.reply_dir.scale,
+ query.ct.reply_dir.close_initiated,
+ query.ct.reply_dir.last_ack_seen,
+ query.ct.reply_dir.data_unacked,
+ query.ct.reply_dir.sent_end,
+ query.ct.reply_dir.reply_end,
+ query.ct.reply_dir.max_win,
+ query.ct.reply_dir.max_ack);
+ break;
+ default:
+ fprintf(stderr,
+ "Indirect action %u (type: %d) on port %u doesn't support query\n",
+ id, pia->type, port_id);
+ break;
+ }
+ return 0;
+}
+
+static struct port_flow_tunnel *
+port_flow_tunnel_offload_cmd_prep(portid_t port_id,
+ const struct rte_flow_item *pattern,
+ const struct rte_flow_action *actions,
+ const struct tunnel_ops *tunnel_ops)
+{
+ int ret;
+ struct rte_port *port;
+ struct port_flow_tunnel *pft;
+ struct rte_flow_error error;
+
+ port = &ports[port_id];
+ pft = port_flow_locate_tunnel_id(port, tunnel_ops->id);
+ if (!pft) {
+ fprintf(stderr, "failed to locate port flow tunnel #%u\n",
+ tunnel_ops->id);
+ return NULL;
+ }
+ if (tunnel_ops->actions) {
+ uint32_t num_actions;
+ const struct rte_flow_action *aptr;
+
+ ret = rte_flow_tunnel_decap_set(port_id, &pft->tunnel,
+ &pft->pmd_actions,
+ &pft->num_pmd_actions,
+ &error);
+ if (ret) {
+ port_flow_complain(&error);
+ return NULL;
+ }
+ for (aptr = actions, num_actions = 1;
+ aptr->type != RTE_FLOW_ACTION_TYPE_END;
+ aptr++, num_actions++);
+ pft->actions = malloc(
+ (num_actions + pft->num_pmd_actions) *
+ sizeof(actions[0]));
+ if (!pft->actions) {
+ rte_flow_tunnel_action_decap_release(
+ port_id, pft->actions,
+ pft->num_pmd_actions, &error);
+ return NULL;
+ }
+ rte_memcpy(pft->actions, pft->pmd_actions,
+ pft->num_pmd_actions * sizeof(actions[0]));
+ rte_memcpy(pft->actions + pft->num_pmd_actions, actions,
+ num_actions * sizeof(actions[0]));
+ }
+ if (tunnel_ops->items) {
+ uint32_t num_items;
+ const struct rte_flow_item *iptr;
+
+ ret = rte_flow_tunnel_match(port_id, &pft->tunnel,
+ &pft->pmd_items,
+ &pft->num_pmd_items,
+ &error);
+ if (ret) {
+ port_flow_complain(&error);
+ return NULL;
+ }
+ for (iptr = pattern, num_items = 1;
+ iptr->type != RTE_FLOW_ITEM_TYPE_END;
+ iptr++, num_items++);
+ pft->items = malloc((num_items + pft->num_pmd_items) *
+ sizeof(pattern[0]));
+ if (!pft->items) {
+ rte_flow_tunnel_item_release(
+ port_id, pft->pmd_items,
+ pft->num_pmd_items, &error);
+ return NULL;
+ }
+ rte_memcpy(pft->items, pft->pmd_items,
+ pft->num_pmd_items * sizeof(pattern[0]));
+ rte_memcpy(pft->items + pft->num_pmd_items, pattern,
+ num_items * sizeof(pattern[0]));
+ }
+
+ return pft;
+}
+
+static void
+port_flow_tunnel_offload_cmd_release(portid_t port_id,
+ const struct tunnel_ops *tunnel_ops,
+ struct port_flow_tunnel *pft)
+{
+ struct rte_flow_error error;
+
+ if (tunnel_ops->actions) {
+ free(pft->actions);
+ rte_flow_tunnel_action_decap_release(
+ port_id, pft->pmd_actions,
+ pft->num_pmd_actions, &error);
+ pft->actions = NULL;
+ pft->pmd_actions = NULL;
+ }
+ if (tunnel_ops->items) {
+ free(pft->items);
+ rte_flow_tunnel_item_release(port_id, pft->pmd_items,
+ pft->num_pmd_items,
+ &error);
+ pft->items = NULL;
+ pft->pmd_items = NULL;
+ }
+}
+
+/** Add port meter policy */
+int
+port_meter_policy_add(portid_t port_id, uint32_t policy_id,
+ const struct rte_flow_action *actions)
+{
+ struct rte_mtr_error error;
+ const struct rte_flow_action *act = actions;
+ const struct rte_flow_action *start;
+ struct rte_mtr_meter_policy_params policy;
+ uint32_t i = 0, act_n;
+ int ret;
+
+ for (i = 0; i < RTE_COLORS; i++) {
+ for (act_n = 0, start = act;
+ act->type != RTE_FLOW_ACTION_TYPE_END; act++)
+ act_n++;
+ if (act_n && act->type == RTE_FLOW_ACTION_TYPE_END)
+ policy.actions[i] = start;
+ else
+ policy.actions[i] = NULL;
+ act++;
+ }
+ ret = rte_mtr_meter_policy_add(port_id,
+ policy_id,
+ &policy, &error);
+ if (ret)
+ print_mtr_err_msg(&error);
+ return ret;
+}
+
+/** Validate flow rule. */
+int
+port_flow_validate(portid_t port_id,
+ const struct rte_flow_attr *attr,
+ const struct rte_flow_item *pattern,
+ const struct rte_flow_action *actions,
+ const struct tunnel_ops *tunnel_ops)
+{
+ struct rte_flow_error error;
+ struct port_flow_tunnel *pft = NULL;
+ int ret;
+
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x11, sizeof(error));
+ if (tunnel_ops->enabled) {
+ pft = port_flow_tunnel_offload_cmd_prep(port_id, pattern,
+ actions, tunnel_ops);
+ if (!pft)
+ return -ENOENT;
+ if (pft->items)
+ pattern = pft->items;
+ if (pft->actions)
+ actions = pft->actions;
+ }
+ ret = rte_flow_validate(port_id, attr, pattern, actions, &error);
+ if (tunnel_ops->enabled)
+ port_flow_tunnel_offload_cmd_release(port_id, tunnel_ops, pft);
+ if (ret)
+ return port_flow_complain(&error);
+ printf("Flow rule validated\n");
+ return 0;
+}
+
+/** Return age action structure if exists, otherwise NULL. */
+static struct rte_flow_action_age *
+age_action_get(const struct rte_flow_action *actions)
+{
+ for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
+ switch (actions->type) {
+ case RTE_FLOW_ACTION_TYPE_AGE:
+ return (struct rte_flow_action_age *)
+ (uintptr_t)actions->conf;
+ default:
+ break;
+ }
+ }
+ return NULL;
+}
+
+/** Create pattern template */
+int
+port_flow_pattern_template_create(portid_t port_id, uint32_t id,
+ const struct rte_flow_pattern_template_attr *attr,
+ const struct rte_flow_item *pattern)
+{
+ struct rte_port *port;
+ struct port_template *pit;
+ int ret;
+ struct rte_flow_error error;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ ret = template_alloc(id, &pit, &port->pattern_templ_list);
+ if (ret)
+ return ret;
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x22, sizeof(error));
+ pit->template.pattern_template = rte_flow_pattern_template_create(port_id,
+ attr, pattern, &error);
+ if (!pit->template.pattern_template) {
+ uint32_t destroy_id = pit->id;
+ port_flow_pattern_template_destroy(port_id, 1, &destroy_id);
+ return port_flow_complain(&error);
+ }
+ printf("Pattern template #%u created\n", pit->id);
+ return 0;
+}
+
+/** Destroy pattern template */
+int
+port_flow_pattern_template_destroy(portid_t port_id, uint32_t n,
+ const uint32_t *template)
+{
+ struct rte_port *port;
+ struct port_template **tmp;
+ uint32_t c = 0;
+ int ret = 0;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ tmp = &port->pattern_templ_list;
+ while (*tmp) {
+ uint32_t i;
+
+ for (i = 0; i != n; ++i) {
+ struct rte_flow_error error;
+ struct port_template *pit = *tmp;
+
+ if (template[i] != pit->id)
+ continue;
+ /*
+ * Poisoning to make sure PMDs update it in case
+ * of error.
+ */
+ memset(&error, 0x33, sizeof(error));
+
+ if (pit->template.pattern_template &&
+ rte_flow_pattern_template_destroy(port_id,
+ pit->template.pattern_template,
+ &error)) {
+ ret = port_flow_complain(&error);
+ continue;
+ }
+ *tmp = pit->next;
+ printf("Pattern template #%u destroyed\n", pit->id);
+ free(pit);
+ break;
+ }
+ if (i == n)
+ tmp = &(*tmp)->next;
+ ++c;
+ }
+ return ret;
+}
+
+/** Create actions template */
+int
+port_flow_actions_template_create(portid_t port_id, uint32_t id,
+ const struct rte_flow_actions_template_attr *attr,
+ const struct rte_flow_action *actions,
+ const struct rte_flow_action *masks)
+{
+ struct rte_port *port;
+ struct port_template *pat;
+ int ret;
+ struct rte_flow_error error;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ ret = template_alloc(id, &pat, &port->actions_templ_list);
+ if (ret)
+ return ret;
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x22, sizeof(error));
+ pat->template.actions_template = rte_flow_actions_template_create(port_id,
+ attr, actions, masks, &error);
+ if (!pat->template.actions_template) {
+ uint32_t destroy_id = pat->id;
+ port_flow_actions_template_destroy(port_id, 1, &destroy_id);
+ return port_flow_complain(&error);
+ }
+ printf("Actions template #%u created\n", pat->id);
+ return 0;
+}
+
+/** Destroy actions template */
+int
+port_flow_actions_template_destroy(portid_t port_id, uint32_t n,
+ const uint32_t *template)
+{
+ struct rte_port *port;
+ struct port_template **tmp;
+ uint32_t c = 0;
+ int ret = 0;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ tmp = &port->actions_templ_list;
+ while (*tmp) {
+ uint32_t i;
+
+ for (i = 0; i != n; ++i) {
+ struct rte_flow_error error;
+ struct port_template *pat = *tmp;
+
+ if (template[i] != pat->id)
+ continue;
+ /*
+ * Poisoning to make sure PMDs update it in case
+ * of error.
+ */
+ memset(&error, 0x33, sizeof(error));
+
+ if (pat->template.actions_template &&
+ rte_flow_actions_template_destroy(port_id,
+ pat->template.actions_template, &error)) {
+ ret = port_flow_complain(&error);
+ continue;
+ }
+ *tmp = pat->next;
+ printf("Actions template #%u destroyed\n", pat->id);
+ free(pat);
+ break;
+ }
+ if (i == n)
+ tmp = &(*tmp)->next;
+ ++c;
+ }
+ return ret;
+}
+
+/** Create table */
+int
+port_flow_template_table_create(portid_t port_id, uint32_t id,
+ const struct rte_flow_template_table_attr *table_attr,
+ uint32_t nb_pattern_templates, uint32_t *pattern_templates,
+ uint32_t nb_actions_templates, uint32_t *actions_templates)
+{
+ struct rte_port *port;
+ struct port_table *pt;
+ struct port_template *temp = NULL;
+ int ret;
+ uint32_t i;
+ struct rte_flow_error error;
+ struct rte_flow_pattern_template
+ *flow_pattern_templates[nb_pattern_templates];
+ struct rte_flow_actions_template
+ *flow_actions_templates[nb_actions_templates];
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ for (i = 0; i < nb_pattern_templates; ++i) {
+ bool found = false;
+ temp = port->pattern_templ_list;
+ while (temp) {
+ if (pattern_templates[i] == temp->id) {
+ flow_pattern_templates[i] =
+ temp->template.pattern_template;
+ found = true;
+ break;
+ }
+ temp = temp->next;
+ }
+ if (!found) {
+ printf("Pattern template #%u is invalid\n",
+ pattern_templates[i]);
+ return -EINVAL;
+ }
+ }
+ for (i = 0; i < nb_actions_templates; ++i) {
+ bool found = false;
+ temp = port->actions_templ_list;
+ while (temp) {
+ if (actions_templates[i] == temp->id) {
+ flow_actions_templates[i] =
+ temp->template.actions_template;
+ found = true;
+ break;
+ }
+ temp = temp->next;
+ }
+ if (!found) {
+ printf("Actions template #%u is invalid\n",
+ actions_templates[i]);
+ return -EINVAL;
+ }
+ }
+ ret = table_alloc(id, &pt, &port->table_list);
+ if (ret)
+ return ret;
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x22, sizeof(error));
+ pt->table = rte_flow_template_table_create(port_id, table_attr,
+ flow_pattern_templates, nb_pattern_templates,
+ flow_actions_templates, nb_actions_templates,
+ &error);
+
+ if (!pt->table) {
+ uint32_t destroy_id = pt->id;
+ port_flow_template_table_destroy(port_id, 1, &destroy_id);
+ return port_flow_complain(&error);
+ }
+ pt->nb_pattern_templates = nb_pattern_templates;
+ pt->nb_actions_templates = nb_actions_templates;
+ printf("Template table #%u created\n", pt->id);
+ return 0;
+}
+
+/** Destroy table */
+int
+port_flow_template_table_destroy(portid_t port_id,
+ uint32_t n, const uint32_t *table)
+{
+ struct rte_port *port;
+ struct port_table **tmp;
+ uint32_t c = 0;
+ int ret = 0;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+ tmp = &port->table_list;
+ while (*tmp) {
+ uint32_t i;
+
+ for (i = 0; i != n; ++i) {
+ struct rte_flow_error error;
+ struct port_table *pt = *tmp;
+
+ if (table[i] != pt->id)
+ continue;
+ /*
+ * Poisoning to make sure PMDs update it in case
+ * of error.
+ */
+ memset(&error, 0x33, sizeof(error));
+
+ if (pt->table &&
+ rte_flow_template_table_destroy(port_id,
+ pt->table,
+ &error)) {
+ ret = port_flow_complain(&error);
+ continue;
+ }
+ *tmp = pt->next;
+ printf("Template table #%u destroyed\n", pt->id);
+ free(pt);
+ break;
+ }
+ if (i == n)
+ tmp = &(*tmp)->next;
+ ++c;
+ }
+ return ret;
+}
+
+/** Enqueue create flow rule operation. */
+int
+port_queue_flow_create(portid_t port_id, queueid_t queue_id,
+ bool postpone, uint32_t table_id,
+ uint32_t pattern_idx, uint32_t actions_idx,
+ const struct rte_flow_item *pattern,
+ const struct rte_flow_action *actions)
+{
+ struct rte_flow_op_attr op_attr = { .postpone = postpone };
+ struct rte_flow *flow;
+ struct rte_port *port;
+ struct port_flow *pf;
+ struct port_table *pt;
+ uint32_t id = 0;
+ bool found;
+ struct rte_flow_error error = { RTE_FLOW_ERROR_TYPE_NONE, NULL, NULL };
+ struct rte_flow_action_age *age = age_action_get(actions);
+
+ port = &ports[port_id];
+ if (port->flow_list) {
+ if (port->flow_list->id == UINT32_MAX) {
+ printf("Highest rule ID is already assigned,"
+ " delete it first");
+ return -ENOMEM;
+ }
+ id = port->flow_list->id + 1;
+ }
+
+ if (queue_id >= port->queue_nb) {
+ printf("Queue #%u is invalid\n", queue_id);
+ return -EINVAL;
+ }
+
+ found = false;
+ pt = port->table_list;
+ while (pt) {
+ if (table_id == pt->id) {
+ found = true;
+ break;
+ }
+ pt = pt->next;
+ }
+ if (!found) {
+ printf("Table #%u is invalid\n", table_id);
+ return -EINVAL;
+ }
+
+ if (pattern_idx >= pt->nb_pattern_templates) {
+ printf("Pattern template index #%u is invalid,"
+ " %u templates present in the table\n",
+ pattern_idx, pt->nb_pattern_templates);
+ return -EINVAL;
+ }
+ if (actions_idx >= pt->nb_actions_templates) {
+ printf("Actions template index #%u is invalid,"
+ " %u templates present in the table\n",
+ actions_idx, pt->nb_actions_templates);
+ return -EINVAL;
+ }
+
+ pf = port_flow_new(NULL, pattern, actions, &error);
+ if (!pf)
+ return port_flow_complain(&error);
+ if (age) {
+ pf->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW;
+ age->context = &pf->age_type;
+ }
+ /* Poisoning to make sure PMDs update it in case of error. */
+ memset(&error, 0x11, sizeof(error));
+ flow = rte_flow_async_create(port_id, queue_id, &op_attr, pt->table,
+ pattern, pattern_idx, actions, actions_idx, NULL, &error);
+ if (!flow) {
+ uint32_t flow_id = pf->id;
+ port_queue_flow_destroy(port_id, queue_id, true, 1, &flow_id);
+ return port_flow_complain(&error);
+ }
+
+ pf->next = port->flow_list;
+ pf->id = id;
+ pf->flow = flow;
+ port->flow_list = pf;
+ printf("Flow rule #%u creation enqueued\n", pf->id);
+ return 0;
+}
+
+/** Enqueue number of destroy flow rules operations. */
+int
+port_queue_flow_destroy(portid_t port_id, queueid_t queue_id,
+ bool postpone, uint32_t n, const uint32_t *rule)
+{
+ struct rte_flow_op_attr op_attr = { .postpone = postpone };
+ struct rte_port *port;
+ struct port_flow **tmp;
+ uint32_t c = 0;
+ int ret = 0;
+
+ if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+ port_id == (portid_t)RTE_PORT_ALL)
+ return -EINVAL;
+ port = &ports[port_id];
+
+ if (queue_id >= port->queue_nb) {
+ printf("Queue #%u is invalid\n", queue_id);
+ return -EINVAL;
+ }
+
+ tmp = &port->flow_list;
+ while (*tmp) {
+ uint32_t i;
+
+ for (i = 0; i != n; ++i) {
+ struct rte_flow_error error;
+ struct port_flow *pf = *tmp;
+
+ if (rule[i] != pf->id)
+ continue;
+ /*
+ * Poisoning to make sure PMD
+ * update it in case of error.
+ */
+ memset(&error, 0x33, sizeof(error));
+ if (rte_flow_async_destroy(port_id, queue_id, &op_attr,
+ pf->flow, NULL, &error)) {
+ ret = port_flow_complain(&error);
+ continue;
+ }
+ printf("Flow rule #%u destruction enqueued\n", pf->id);
+ *tmp = pf->next;
+ free(pf);
+ break;
+ }
+ if (i == n)
+ tmp = &(*tmp)->next;
+ ++c;
+ }
+ return ret;
+}