1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2008-2019 Cisco Systems, Inc. All rights reserved.
8 #include <rte_ethdev_driver.h>
9 #include <rte_flow_driver.h>
10 #include <rte_ether.h>
13 #include <rte_memzone.h>
15 #include "enic_compat.h"
20 #define IP_DEFTTL 64 /* from RFC 1340. */
21 #define IP6_VTC_FLOW 0x60000000
23 /* Highest Item type supported by Flowman */
24 #define FM_MAX_ITEM_TYPE RTE_FLOW_ITEM_TYPE_VXLAN
26 /* Up to 1024 TCAM entries */
27 #define FM_MAX_TCAM_TABLE_SIZE 1024
29 /* Up to 4096 entries per exact match table */
30 #define FM_MAX_EXACT_TABLE_SIZE 4096
32 /* Number of counters to increase on for each increment */
33 #define FM_COUNTERS_EXPAND 100
35 #define FM_INVALID_HANDLE 0
38 * Flow exact match tables (FET) in the VIC and rte_flow groups.
39 * Use a simple scheme to map groups to tables.
40 * Group 0 uses the single TCAM tables, one for each direction.
41 * Group 1, 2, ... uses its own exact match table.
43 * The TCAM tables are allocated upfront during init.
45 * Exact match tables are allocated on demand. 3 paths that lead allocations.
47 * 1. Add a flow that jumps from group 0 to group N.
49 * If N does not exist, we allocate an exact match table for it, using
50 * a dummy key. A key is required for the table.
52 * 2. Add a flow that uses group N.
54 * If N does not exist, we allocate an exact match table for it, using
55 * the flow's key. Subsequent flows to the same group all should have
58 * Without a jump flow to N, N is not reachable in hardware. No packets
61 * 3. Add a flow to an empty group N.
63 * N has been created via (1) and the dummy key. We free that table, allocate
64 * a new table using the new flow's key. Also re-do the existing jump flow to
65 * point to the new table.
67 #define FM_TCAM_RTE_GROUP 0
70 TAILQ_ENTRY(enic_fm_fet) list;
71 uint32_t group; /* rte_flow group ID */
72 uint64_t handle; /* Exact match table handle from flowman */
75 int ref; /* Reference count via get/put */
76 struct fm_key_template key; /* Key associated with the table */
79 struct enic_fm_counter {
80 SLIST_ENTRY(enic_fm_counter) next;
87 uint64_t entry_handle;
88 uint64_t action_handle;
89 struct enic_fm_counter *counter;
90 struct enic_fm_fet *fet;
93 struct enic_fm_jump_flow {
94 TAILQ_ENTRY(enic_fm_jump_flow) list;
95 struct rte_flow *flow;
97 struct fm_tcam_match_entry match;
98 struct fm_action action;
102 * Flowman uses host memory for commands. This structure is allocated
103 * in DMA-able memory.
105 union enic_flowman_cmd_mem {
106 struct fm_tcam_match_table fm_tcam_match_table;
107 struct fm_exact_match_table fm_exact_match_table;
108 struct fm_tcam_match_entry fm_tcam_match_entry;
109 struct fm_exact_match_entry fm_exact_match_entry;
110 struct fm_action fm_action;
113 struct enic_flowman {
117 union enic_flowman_cmd_mem *va;
120 /* TCAM tables allocated upfront, used for group 0 */
121 uint64_t ig_tcam_hndl;
122 uint64_t eg_tcam_hndl;
124 SLIST_HEAD(enic_free_counters, enic_fm_counter) counters;
126 uint32_t counters_alloced;
127 /* Exact match tables for groups != 0, dynamically allocated */
128 TAILQ_HEAD(fet_list, enic_fm_fet) fet_list;
130 * Default exact match tables used for jump actions to
131 * non-existent groups.
133 struct enic_fm_fet *default_eg_fet;
134 struct enic_fm_fet *default_ig_fet;
135 /* Flows that jump to the default table above */
136 TAILQ_HEAD(jump_flow_list, enic_fm_jump_flow) jump_list;
138 * Scratch data used during each invocation of flow_create
141 struct enic_fm_fet *fet;
142 struct fm_tcam_match_entry tcam_entry;
143 struct fm_action action;
144 struct fm_action action_tmp; /* enic_fm_reorder_action_op */
148 static int enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle);
151 * Common arguments passed to copy_item functions. Use this structure
152 * so we can easily add new arguments.
153 * item: Item specification.
154 * fm_tcam_entry: Flowman TCAM match entry.
155 * header_level: 0 for outer header, 1 for inner header.
157 struct copy_item_args {
158 const struct rte_flow_item *item;
159 struct fm_tcam_match_entry *fm_tcam_entry;
160 uint8_t header_level;
163 /* functions for copying items into flowman match */
164 typedef int (enic_copy_item_fn)(struct copy_item_args *arg);
166 /* Info about how to copy items into flowman match */
167 struct enic_fm_items {
168 /* Function for copying and validating an item. */
169 enic_copy_item_fn * const copy_item;
170 /* List of valid previous items. */
171 const enum rte_flow_item_type * const prev_items;
173 * True if it's OK for this item to be the first item. For some NIC
174 * versions, it's invalid to start the stack above layer 3.
176 const uint8_t valid_start_item;
179 static enic_copy_item_fn enic_fm_copy_item_eth;
180 static enic_copy_item_fn enic_fm_copy_item_ipv4;
181 static enic_copy_item_fn enic_fm_copy_item_ipv6;
182 static enic_copy_item_fn enic_fm_copy_item_raw;
183 static enic_copy_item_fn enic_fm_copy_item_sctp;
184 static enic_copy_item_fn enic_fm_copy_item_tcp;
185 static enic_copy_item_fn enic_fm_copy_item_udp;
186 static enic_copy_item_fn enic_fm_copy_item_vlan;
187 static enic_copy_item_fn enic_fm_copy_item_vxlan;
189 /* Ingress actions */
190 static const enum rte_flow_action_type enic_fm_supported_ig_actions[] = {
191 RTE_FLOW_ACTION_TYPE_COUNT,
192 RTE_FLOW_ACTION_TYPE_DROP,
193 RTE_FLOW_ACTION_TYPE_FLAG,
194 RTE_FLOW_ACTION_TYPE_JUMP,
195 RTE_FLOW_ACTION_TYPE_MARK,
196 RTE_FLOW_ACTION_TYPE_PORT_ID,
197 RTE_FLOW_ACTION_TYPE_PASSTHRU,
198 RTE_FLOW_ACTION_TYPE_QUEUE,
199 RTE_FLOW_ACTION_TYPE_RSS,
200 RTE_FLOW_ACTION_TYPE_VOID,
201 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
202 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP,
203 RTE_FLOW_ACTION_TYPE_END, /* END must be the last entry */
207 static const enum rte_flow_action_type enic_fm_supported_eg_actions[] = {
208 RTE_FLOW_ACTION_TYPE_COUNT,
209 RTE_FLOW_ACTION_TYPE_DROP,
210 RTE_FLOW_ACTION_TYPE_JUMP,
211 RTE_FLOW_ACTION_TYPE_PASSTHRU,
212 RTE_FLOW_ACTION_TYPE_VOID,
213 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
214 RTE_FLOW_ACTION_TYPE_END,
217 static const struct enic_fm_items enic_fm_items[] = {
218 [RTE_FLOW_ITEM_TYPE_RAW] = {
219 .copy_item = enic_fm_copy_item_raw,
220 .valid_start_item = 0,
221 .prev_items = (const enum rte_flow_item_type[]) {
222 RTE_FLOW_ITEM_TYPE_UDP,
223 RTE_FLOW_ITEM_TYPE_END,
226 [RTE_FLOW_ITEM_TYPE_ETH] = {
227 .copy_item = enic_fm_copy_item_eth,
228 .valid_start_item = 1,
229 .prev_items = (const enum rte_flow_item_type[]) {
230 RTE_FLOW_ITEM_TYPE_END,
233 [RTE_FLOW_ITEM_TYPE_VLAN] = {
234 .copy_item = enic_fm_copy_item_vlan,
235 .valid_start_item = 1,
236 .prev_items = (const enum rte_flow_item_type[]) {
237 RTE_FLOW_ITEM_TYPE_ETH,
238 RTE_FLOW_ITEM_TYPE_END,
241 [RTE_FLOW_ITEM_TYPE_IPV4] = {
242 .copy_item = enic_fm_copy_item_ipv4,
243 .valid_start_item = 1,
244 .prev_items = (const enum rte_flow_item_type[]) {
245 RTE_FLOW_ITEM_TYPE_ETH,
246 RTE_FLOW_ITEM_TYPE_VLAN,
247 RTE_FLOW_ITEM_TYPE_END,
250 [RTE_FLOW_ITEM_TYPE_IPV6] = {
251 .copy_item = enic_fm_copy_item_ipv6,
252 .valid_start_item = 1,
253 .prev_items = (const enum rte_flow_item_type[]) {
254 RTE_FLOW_ITEM_TYPE_ETH,
255 RTE_FLOW_ITEM_TYPE_VLAN,
256 RTE_FLOW_ITEM_TYPE_END,
259 [RTE_FLOW_ITEM_TYPE_UDP] = {
260 .copy_item = enic_fm_copy_item_udp,
261 .valid_start_item = 1,
262 .prev_items = (const enum rte_flow_item_type[]) {
263 RTE_FLOW_ITEM_TYPE_IPV4,
264 RTE_FLOW_ITEM_TYPE_IPV6,
265 RTE_FLOW_ITEM_TYPE_END,
268 [RTE_FLOW_ITEM_TYPE_TCP] = {
269 .copy_item = enic_fm_copy_item_tcp,
270 .valid_start_item = 1,
271 .prev_items = (const enum rte_flow_item_type[]) {
272 RTE_FLOW_ITEM_TYPE_IPV4,
273 RTE_FLOW_ITEM_TYPE_IPV6,
274 RTE_FLOW_ITEM_TYPE_END,
277 [RTE_FLOW_ITEM_TYPE_SCTP] = {
278 .copy_item = enic_fm_copy_item_sctp,
279 .valid_start_item = 0,
280 .prev_items = (const enum rte_flow_item_type[]) {
281 RTE_FLOW_ITEM_TYPE_IPV4,
282 RTE_FLOW_ITEM_TYPE_IPV6,
283 RTE_FLOW_ITEM_TYPE_END,
286 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
287 .copy_item = enic_fm_copy_item_vxlan,
288 .valid_start_item = 1,
289 .prev_items = (const enum rte_flow_item_type[]) {
290 RTE_FLOW_ITEM_TYPE_UDP,
291 RTE_FLOW_ITEM_TYPE_END,
297 enic_fm_copy_item_eth(struct copy_item_args *arg)
299 const struct rte_flow_item *item = arg->item;
300 const struct rte_flow_item_eth *spec = item->spec;
301 const struct rte_flow_item_eth *mask = item->mask;
302 const uint8_t lvl = arg->header_level;
303 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
304 struct fm_header_set *fm_data, *fm_mask;
306 ENICPMD_FUNC_TRACE();
307 /* Match all if no spec */
311 mask = &rte_flow_item_eth_mask;
312 fm_data = &entry->ftm_data.fk_hdrset[lvl];
313 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
314 fm_data->fk_header_select |= FKH_ETHER;
315 fm_mask->fk_header_select |= FKH_ETHER;
316 memcpy(&fm_data->l2.eth, spec, sizeof(*spec));
317 memcpy(&fm_mask->l2.eth, mask, sizeof(*mask));
322 enic_fm_copy_item_vlan(struct copy_item_args *arg)
324 const struct rte_flow_item *item = arg->item;
325 const struct rte_flow_item_vlan *spec = item->spec;
326 const struct rte_flow_item_vlan *mask = item->mask;
327 const uint8_t lvl = arg->header_level;
328 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
329 struct fm_header_set *fm_data, *fm_mask;
330 struct rte_ether_hdr *eth_mask;
331 struct rte_ether_hdr *eth_val;
334 ENICPMD_FUNC_TRACE();
335 fm_data = &entry->ftm_data.fk_hdrset[lvl];
336 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
337 /* Outer and inner packet vlans need different flags */
338 meta = FKM_VLAN_PRES;
341 fm_data->fk_metadata |= meta;
342 fm_mask->fk_metadata |= meta;
344 /* Match all if no spec */
348 mask = &rte_flow_item_vlan_mask;
350 eth_mask = (void *)&fm_mask->l2.eth;
351 eth_val = (void *)&fm_data->l2.eth;
353 /* Outer TPID cannot be matched */
354 if (eth_mask->ether_type)
358 * When packet matching, the VIC always compares vlan-stripped
359 * L2, regardless of vlan stripping settings. So, the inner type
360 * from vlan becomes the ether type of the eth header.
362 eth_mask->ether_type = mask->inner_type;
363 eth_val->ether_type = spec->inner_type;
364 fm_data->fk_header_select |= FKH_ETHER | FKH_QTAG;
365 fm_mask->fk_header_select |= FKH_ETHER | FKH_QTAG;
366 fm_data->fk_vlan = rte_be_to_cpu_16(spec->tci);
367 fm_mask->fk_vlan = rte_be_to_cpu_16(mask->tci);
372 enic_fm_copy_item_ipv4(struct copy_item_args *arg)
374 const struct rte_flow_item *item = arg->item;
375 const struct rte_flow_item_ipv4 *spec = item->spec;
376 const struct rte_flow_item_ipv4 *mask = item->mask;
377 const uint8_t lvl = arg->header_level;
378 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
379 struct fm_header_set *fm_data, *fm_mask;
381 ENICPMD_FUNC_TRACE();
382 fm_data = &entry->ftm_data.fk_hdrset[lvl];
383 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
384 fm_data->fk_metadata |= FKM_IPV4;
385 fm_mask->fk_metadata |= FKM_IPV4;
390 mask = &rte_flow_item_ipv4_mask;
392 fm_data->fk_header_select |= FKH_IPV4;
393 fm_mask->fk_header_select |= FKH_IPV4;
394 memcpy(&fm_data->l3.ip4, spec, sizeof(*spec));
395 memcpy(&fm_mask->l3.ip4, mask, sizeof(*mask));
400 enic_fm_copy_item_ipv6(struct copy_item_args *arg)
402 const struct rte_flow_item *item = arg->item;
403 const struct rte_flow_item_ipv6 *spec = item->spec;
404 const struct rte_flow_item_ipv6 *mask = item->mask;
405 const uint8_t lvl = arg->header_level;
406 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
407 struct fm_header_set *fm_data, *fm_mask;
409 ENICPMD_FUNC_TRACE();
410 fm_data = &entry->ftm_data.fk_hdrset[lvl];
411 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
412 fm_data->fk_metadata |= FKM_IPV6;
413 fm_mask->fk_metadata |= FKM_IPV6;
418 mask = &rte_flow_item_ipv6_mask;
420 fm_data->fk_header_select |= FKH_IPV6;
421 fm_mask->fk_header_select |= FKH_IPV6;
422 memcpy(&fm_data->l3.ip6, spec, sizeof(*spec));
423 memcpy(&fm_mask->l3.ip6, mask, sizeof(*mask));
428 enic_fm_copy_item_udp(struct copy_item_args *arg)
430 const struct rte_flow_item *item = arg->item;
431 const struct rte_flow_item_udp *spec = item->spec;
432 const struct rte_flow_item_udp *mask = item->mask;
433 const uint8_t lvl = arg->header_level;
434 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
435 struct fm_header_set *fm_data, *fm_mask;
437 ENICPMD_FUNC_TRACE();
438 fm_data = &entry->ftm_data.fk_hdrset[lvl];
439 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
440 fm_data->fk_metadata |= FKM_UDP;
441 fm_mask->fk_metadata |= FKM_UDP;
446 mask = &rte_flow_item_udp_mask;
448 fm_data->fk_header_select |= FKH_UDP;
449 fm_mask->fk_header_select |= FKH_UDP;
450 memcpy(&fm_data->l4.udp, spec, sizeof(*spec));
451 memcpy(&fm_mask->l4.udp, mask, sizeof(*mask));
456 enic_fm_copy_item_tcp(struct copy_item_args *arg)
458 const struct rte_flow_item *item = arg->item;
459 const struct rte_flow_item_tcp *spec = item->spec;
460 const struct rte_flow_item_tcp *mask = item->mask;
461 const uint8_t lvl = arg->header_level;
462 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
463 struct fm_header_set *fm_data, *fm_mask;
465 ENICPMD_FUNC_TRACE();
466 fm_data = &entry->ftm_data.fk_hdrset[lvl];
467 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
468 fm_data->fk_metadata |= FKM_TCP;
469 fm_mask->fk_metadata |= FKM_TCP;
474 mask = &rte_flow_item_tcp_mask;
476 fm_data->fk_header_select |= FKH_TCP;
477 fm_mask->fk_header_select |= FKH_TCP;
478 memcpy(&fm_data->l4.tcp, spec, sizeof(*spec));
479 memcpy(&fm_mask->l4.tcp, mask, sizeof(*mask));
484 enic_fm_copy_item_sctp(struct copy_item_args *arg)
486 const struct rte_flow_item *item = arg->item;
487 const struct rte_flow_item_sctp *spec = item->spec;
488 const struct rte_flow_item_sctp *mask = item->mask;
489 const uint8_t lvl = arg->header_level;
490 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
491 struct fm_header_set *fm_data, *fm_mask;
492 uint8_t *ip_proto_mask = NULL;
493 uint8_t *ip_proto = NULL;
496 ENICPMD_FUNC_TRACE();
497 fm_data = &entry->ftm_data.fk_hdrset[lvl];
498 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
500 * The NIC filter API has no flags for "match sctp", so explicitly
501 * set the protocol number in the IP pattern.
503 if (fm_data->fk_metadata & FKM_IPV4) {
504 struct rte_ipv4_hdr *ip;
505 ip = (struct rte_ipv4_hdr *)&fm_mask->l3.ip4;
506 ip_proto_mask = &ip->next_proto_id;
507 ip = (struct rte_ipv4_hdr *)&fm_data->l3.ip4;
508 ip_proto = &ip->next_proto_id;
510 } else if (fm_data->fk_metadata & FKM_IPV6) {
511 struct rte_ipv6_hdr *ip;
512 ip = (struct rte_ipv6_hdr *)&fm_mask->l3.ip6;
513 ip_proto_mask = &ip->proto;
514 ip = (struct rte_ipv6_hdr *)&fm_data->l3.ip6;
515 ip_proto = &ip->proto;
518 /* Need IPv4/IPv6 pattern first */
521 *ip_proto = IPPROTO_SCTP;
522 *ip_proto_mask = 0xff;
523 fm_data->fk_header_select |= l3_fkh;
524 fm_mask->fk_header_select |= l3_fkh;
529 mask = &rte_flow_item_sctp_mask;
531 fm_data->fk_header_select |= FKH_L4RAW;
532 fm_mask->fk_header_select |= FKH_L4RAW;
533 memcpy(fm_data->l4.rawdata, spec, sizeof(*spec));
534 memcpy(fm_mask->l4.rawdata, mask, sizeof(*mask));
539 enic_fm_copy_item_vxlan(struct copy_item_args *arg)
541 const struct rte_flow_item *item = arg->item;
542 const struct rte_flow_item_vxlan *spec = item->spec;
543 const struct rte_flow_item_vxlan *mask = item->mask;
544 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
545 struct fm_header_set *fm_data, *fm_mask;
547 ENICPMD_FUNC_TRACE();
548 /* Only 2 header levels (outer and inner) allowed */
549 if (arg->header_level > 0)
552 fm_data = &entry->ftm_data.fk_hdrset[0];
553 fm_mask = &entry->ftm_mask.fk_hdrset[0];
554 fm_data->fk_metadata |= FKM_VXLAN;
555 fm_mask->fk_metadata |= FKM_VXLAN;
556 /* items from here on out are inner header items */
557 arg->header_level = 1;
559 /* Match all if no spec */
563 mask = &rte_flow_item_vxlan_mask;
565 fm_data->fk_header_select |= FKH_VXLAN;
566 fm_mask->fk_header_select |= FKH_VXLAN;
567 memcpy(&fm_data->vxlan, spec, sizeof(*spec));
568 memcpy(&fm_mask->vxlan, mask, sizeof(*mask));
573 * Currently, raw pattern match is very limited. It is intended for matching
574 * UDP tunnel header (e.g. vxlan or geneve).
577 enic_fm_copy_item_raw(struct copy_item_args *arg)
579 const struct rte_flow_item *item = arg->item;
580 const struct rte_flow_item_raw *spec = item->spec;
581 const struct rte_flow_item_raw *mask = item->mask;
582 const uint8_t lvl = arg->header_level;
583 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
584 struct fm_header_set *fm_data, *fm_mask;
586 ENICPMD_FUNC_TRACE();
587 /* Cannot be used for inner packet */
590 /* Need both spec and mask */
593 /* Only supports relative with offset 0 */
594 if (!spec->relative || spec->offset != 0 || spec->search ||
597 /* Need non-null pattern that fits within the NIC's filter pattern */
598 if (spec->length == 0 ||
599 spec->length + sizeof(struct rte_udp_hdr) > FM_LAYER_SIZE ||
600 !spec->pattern || !mask->pattern)
603 * Mask fields, including length, are often set to zero. Assume that
604 * means "same as spec" to avoid breaking existing apps. If length
605 * is not zero, then it should be >= spec length.
607 * No more pattern follows this, so append to the L4 layer instead of
608 * L5 to work with both recent and older VICs.
610 if (mask->length != 0 && mask->length < spec->length)
613 fm_data = &entry->ftm_data.fk_hdrset[lvl];
614 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
615 fm_data->fk_header_select |= FKH_L4RAW;
616 fm_mask->fk_header_select |= FKH_L4RAW;
617 fm_data->fk_header_select &= ~FKH_UDP;
618 fm_mask->fk_header_select &= ~FKH_UDP;
619 memcpy(fm_data->l4.rawdata + sizeof(struct rte_udp_hdr),
620 spec->pattern, spec->length);
621 memcpy(fm_mask->l4.rawdata + sizeof(struct rte_udp_hdr),
622 mask->pattern, spec->length);
627 enic_fet_alloc(struct enic_flowman *fm, uint8_t ingress,
628 struct fm_key_template *key, int entries,
629 struct enic_fm_fet **fet_out)
631 struct fm_exact_match_table *cmd;
632 struct fm_header_set *hdr;
633 struct enic_fm_fet *fet;
637 ENICPMD_FUNC_TRACE();
638 fet = calloc(1, sizeof(struct enic_fm_fet));
641 cmd = &fm->cmd.va->fm_exact_match_table;
642 memset(cmd, 0, sizeof(*cmd));
643 cmd->fet_direction = ingress ? FM_INGRESS : FM_EGRESS;
644 cmd->fet_stage = FM_STAGE_LAST;
645 cmd->fet_max_entries = entries ? entries : FM_MAX_EXACT_TABLE_SIZE;
647 hdr = &cmd->fet_key.fk_hdrset[0];
648 memset(hdr, 0, sizeof(*hdr));
649 hdr->fk_header_select = FKH_IPV4 | FKH_UDP;
650 hdr->l3.ip4.fk_saddr = 0xFFFFFFFF;
651 hdr->l3.ip4.fk_daddr = 0xFFFFFFFF;
652 hdr->l4.udp.fk_source = 0xFFFF;
653 hdr->l4.udp.fk_dest = 0xFFFF;
654 fet->default_key = 1;
656 memcpy(&cmd->fet_key, key, sizeof(*key));
657 memcpy(&fet->key, key, sizeof(*key));
658 fet->default_key = 0;
660 cmd->fet_key.fk_packet_tag = 1;
662 args[0] = FM_EXACT_TABLE_ALLOC;
663 args[1] = fm->cmd.pa;
664 ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
666 ENICPMD_LOG(ERR, "cannot alloc exact match table: rc=%d", ret);
670 fet->handle = args[0];
671 fet->ingress = ingress;
672 ENICPMD_LOG(DEBUG, "allocated exact match table: handle=0x%" PRIx64,
679 enic_fet_free(struct enic_flowman *fm, struct enic_fm_fet *fet)
681 ENICPMD_FUNC_TRACE();
682 enic_fm_tbl_free(fm, fet->handle);
683 if (!fet->default_key)
684 TAILQ_REMOVE(&fm->fet_list, fet, list);
689 * Get the exact match table for the given combination of
690 * <group, ingress, key>. Allocate one on the fly as necessary.
693 enic_fet_get(struct enic_flowman *fm,
696 struct fm_key_template *key,
697 struct enic_fm_fet **fet_out,
698 struct rte_flow_error *error)
700 struct enic_fm_fet *fet;
702 ENICPMD_FUNC_TRACE();
703 /* See if we already have this table open */
704 TAILQ_FOREACH(fet, &fm->fet_list, list) {
705 if (fet->group == group && fet->ingress == ingress)
709 /* Jumping to a non-existing group? Use the default table */
711 fet = ingress ? fm->default_ig_fet : fm->default_eg_fet;
712 } else if (enic_fet_alloc(fm, ingress, key, 0, &fet)) {
713 return rte_flow_error_set(error, EINVAL,
714 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
715 NULL, "enic: cannot get exact match table");
718 /* Default table is never on the open table list */
719 if (!fet->default_key)
720 TAILQ_INSERT_HEAD(&fm->fet_list, fet, list);
724 ENICPMD_LOG(DEBUG, "fet_get: %s %s group=%u ref=%u",
725 fet->default_key ? "default" : "",
726 fet->ingress ? "ingress" : "egress",
727 fet->group, fet->ref);
732 enic_fet_put(struct enic_flowman *fm, struct enic_fm_fet *fet)
734 ENICPMD_FUNC_TRACE();
735 RTE_ASSERT(fet->ref > 0);
737 ENICPMD_LOG(DEBUG, "fet_put: %s %s group=%u ref=%u",
738 fet->default_key ? "default" : "",
739 fet->ingress ? "ingress" : "egress",
740 fet->group, fet->ref);
742 enic_fet_free(fm, fet);
745 /* Return 1 if current item is valid on top of the previous one. */
747 fm_item_stacking_valid(enum rte_flow_item_type prev_item,
748 const struct enic_fm_items *item_info,
749 uint8_t is_first_item)
751 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
753 ENICPMD_FUNC_TRACE();
754 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
755 if (prev_item == *allowed_items)
759 /* This is the first item in the stack. Check if that's cool */
760 if (is_first_item && item_info->valid_start_item)
766 * Build the flow manager match entry structure from the provided pattern.
767 * The pattern is validated as the items are copied.
770 enic_fm_copy_entry(struct enic_flowman *fm,
771 const struct rte_flow_item pattern[],
772 struct rte_flow_error *error)
774 const struct enic_fm_items *item_info;
775 enum rte_flow_item_type prev_item;
776 const struct rte_flow_item *item;
777 struct copy_item_args args;
778 uint8_t prev_header_level;
779 uint8_t is_first_item;
782 ENICPMD_FUNC_TRACE();
785 prev_item = RTE_FLOW_ITEM_TYPE_END;
787 args.fm_tcam_entry = &fm->tcam_entry;
788 args.header_level = 0;
789 prev_header_level = 0;
790 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
792 * Get info about how to validate and copy the item. If NULL
793 * is returned the nic does not support the item.
795 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
798 item_info = &enic_fm_items[item->type];
800 if (item->type > FM_MAX_ITEM_TYPE ||
801 item_info->copy_item == NULL) {
802 return rte_flow_error_set(error, ENOTSUP,
803 RTE_FLOW_ERROR_TYPE_ITEM,
804 NULL, "enic: unsupported item");
807 /* check to see if item stacking is valid */
808 if (!fm_item_stacking_valid(prev_item, item_info,
813 ret = item_info->copy_item(&args);
815 goto item_not_supported;
816 /* Going from outer to inner? Treat it as a new packet start */
817 if (prev_header_level != args.header_level) {
818 prev_item = RTE_FLOW_ITEM_TYPE_END;
821 prev_item = item->type;
824 prev_header_level = args.header_level;
829 return rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_ITEM,
830 NULL, "enic: unsupported item type");
833 return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
834 item, "enic: unsupported item stack");
838 flow_item_skip_void(const struct rte_flow_item **item)
841 if ((*item)->type != RTE_FLOW_ITEM_TYPE_VOID)
846 append_template(void **template, uint8_t *off, const void *data, int len)
848 memcpy(*template, data, len);
849 *template = (char *)*template + len;
854 enic_fm_append_action_op(struct enic_flowman *fm,
855 struct fm_action_op *fm_op,
856 struct rte_flow_error *error)
860 count = fm->action_op_count;
861 ENICPMD_LOG(DEBUG, "append action op: idx=%d op=%u",
862 count, fm_op->fa_op);
863 if (count == FM_ACTION_OP_MAX) {
864 return rte_flow_error_set(error, EINVAL,
865 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
866 "too many action operations");
868 fm->action.fma_action_ops[count] = *fm_op;
869 fm->action_op_count = count + 1;
873 /* Steer operations need to appear before other ops */
875 enic_fm_reorder_action_op(struct enic_flowman *fm)
877 struct fm_action_op *dst, *dst_head, *src, *src_head;
879 ENICPMD_FUNC_TRACE();
880 /* Move steer ops to the front. */
881 src = fm->action.fma_action_ops;
883 dst = fm->action_tmp.fma_action_ops;
885 /* Copy steer ops to tmp */
886 while (src->fa_op != FMOP_END) {
887 if (src->fa_op == FMOP_RQ_STEER) {
888 ENICPMD_LOG(DEBUG, "move op: %ld -> dst %ld",
889 (long)(src - src_head),
890 (long)(dst - dst_head));
896 /* Then append non-steer ops */
898 while (src->fa_op != FMOP_END) {
899 if (src->fa_op != FMOP_RQ_STEER) {
900 ENICPMD_LOG(DEBUG, "move op: %ld -> dst %ld",
901 (long)(src - src_head),
902 (long)(dst - dst_head));
910 /* Finally replace the original action with the reordered one */
911 memcpy(fm->action.fma_action_ops, fm->action_tmp.fma_action_ops,
912 sizeof(fm->action.fma_action_ops));
915 /* VXLAN decap is done via flowman compound action */
917 enic_fm_copy_vxlan_decap(struct enic_flowman *fm,
918 struct fm_tcam_match_entry *fmt,
919 const struct rte_flow_action *action,
920 struct rte_flow_error *error)
922 struct fm_header_set *fm_data;
923 struct fm_action_op fm_op;
925 ENICPMD_FUNC_TRACE();
926 fm_data = &fmt->ftm_data.fk_hdrset[0];
927 if (!(fm_data->fk_metadata & FKM_VXLAN)) {
928 return rte_flow_error_set(error, EINVAL,
929 RTE_FLOW_ERROR_TYPE_ACTION, action,
930 "vxlan-decap: vxlan must be in pattern");
933 memset(&fm_op, 0, sizeof(fm_op));
934 fm_op.fa_op = FMOP_DECAP_NOSTRIP;
935 return enic_fm_append_action_op(fm, &fm_op, error);
938 /* VXLAN encap is done via flowman compound action */
940 enic_fm_copy_vxlan_encap(struct enic_flowman *fm,
941 const struct rte_flow_item *item,
942 struct rte_flow_error *error)
944 struct fm_action_op fm_op;
945 struct rte_ether_hdr *eth;
950 ENICPMD_FUNC_TRACE();
951 memset(&fm_op, 0, sizeof(fm_op));
952 fm_op.fa_op = FMOP_ENCAP;
953 template = fm->action.fma_data;
956 * Copy flow items to the flowman template starting L2.
957 * L2 must be ethernet.
959 flow_item_skip_void(&item);
960 if (item->type != RTE_FLOW_ITEM_TYPE_ETH)
961 return rte_flow_error_set(error, EINVAL,
962 RTE_FLOW_ERROR_TYPE_ITEM, item,
963 "vxlan-encap: first item should be ethernet");
964 eth = (struct rte_ether_hdr *)template;
965 ethertype = ð->ether_type;
966 append_template(&template, &off, item->spec,
967 sizeof(struct rte_flow_item_eth));
969 flow_item_skip_void(&item);
971 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
972 const struct rte_flow_item_vlan *spec;
974 ENICPMD_LOG(DEBUG, "vxlan-encap: vlan");
976 fm_op.encap.outer_vlan = rte_be_to_cpu_16(spec->tci);
978 flow_item_skip_void(&item);
980 /* L3 must be IPv4, IPv6 */
981 switch (item->type) {
982 case RTE_FLOW_ITEM_TYPE_IPV4:
984 struct rte_ipv4_hdr *ip4;
986 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv4");
987 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
988 ip4 = (struct rte_ipv4_hdr *)template;
990 * Offset of IPv4 length field and its initial value
991 * (IP + UDP + VXLAN) are specified in the action. The NIC
992 * will add inner packet length.
994 fm_op.encap.len1_offset = off +
995 offsetof(struct rte_ipv4_hdr, total_length);
996 fm_op.encap.len1_delta = sizeof(struct rte_ipv4_hdr) +
997 sizeof(struct rte_udp_hdr) +
998 sizeof(struct rte_vxlan_hdr);
999 append_template(&template, &off, item->spec,
1000 sizeof(struct rte_ipv4_hdr));
1001 ip4->version_ihl = RTE_IPV4_VHL_DEF;
1002 if (ip4->time_to_live == 0)
1003 ip4->time_to_live = IP_DEFTTL;
1004 ip4->next_proto_id = IPPROTO_UDP;
1007 case RTE_FLOW_ITEM_TYPE_IPV6:
1009 struct rte_ipv6_hdr *ip6;
1011 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv6");
1012 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
1013 ip6 = (struct rte_ipv6_hdr *)template;
1014 fm_op.encap.len1_offset = off +
1015 offsetof(struct rte_ipv6_hdr, payload_len);
1016 fm_op.encap.len1_delta = sizeof(struct rte_udp_hdr) +
1017 sizeof(struct rte_vxlan_hdr);
1018 append_template(&template, &off, item->spec,
1019 sizeof(struct rte_ipv6_hdr));
1020 ip6->vtc_flow |= rte_cpu_to_be_32(IP6_VTC_FLOW);
1021 if (ip6->hop_limits == 0)
1022 ip6->hop_limits = IP_DEFTTL;
1023 ip6->proto = IPPROTO_UDP;
1027 return rte_flow_error_set(error,
1028 EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1029 "vxlan-encap: L3 must be IPv4/IPv6");
1032 flow_item_skip_void(&item);
1035 if (item->type != RTE_FLOW_ITEM_TYPE_UDP)
1036 return rte_flow_error_set(error, EINVAL,
1037 RTE_FLOW_ERROR_TYPE_ITEM, item,
1038 "vxlan-encap: UDP must follow IPv4/IPv6");
1039 /* UDP length = UDP + VXLAN. NIC will add inner packet length. */
1040 fm_op.encap.len2_offset =
1041 off + offsetof(struct rte_udp_hdr, dgram_len);
1042 fm_op.encap.len2_delta =
1043 sizeof(struct rte_udp_hdr) + sizeof(struct rte_vxlan_hdr);
1044 append_template(&template, &off, item->spec,
1045 sizeof(struct rte_udp_hdr));
1047 flow_item_skip_void(&item);
1050 if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN)
1051 return rte_flow_error_set(error,
1052 EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1053 "vxlan-encap: VXLAN must follow UDP");
1054 append_template(&template, &off, item->spec,
1055 sizeof(struct rte_flow_item_vxlan));
1058 * Fill in the rest of the action structure.
1059 * Indicate that we want to encap with vxlan at packet start.
1061 fm_op.encap.template_offset = 0;
1062 fm_op.encap.template_len = off;
1063 return enic_fm_append_action_op(fm, &fm_op, error);
1067 enic_fm_find_vnic(struct enic *enic, const struct rte_pci_addr *addr,
1074 ENICPMD_FUNC_TRACE();
1075 ENICPMD_LOG(DEBUG, "bdf=%x:%x:%x", addr->bus, addr->devid,
1077 bdf = addr->bus << 8 | addr->devid << 3 | addr->function;
1078 args[0] = FM_VNIC_FIND;
1080 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1082 ENICPMD_LOG(ERR, "allocating counters rc=%d", rc);
1086 ENICPMD_LOG(DEBUG, "found vnic: handle=0x%" PRIx64, *handle);
1090 /* Translate flow actions to flowman TCAM entry actions */
1092 enic_fm_copy_action(struct enic_flowman *fm,
1093 const struct rte_flow_action actions[],
1095 struct rte_flow_error *error)
1104 struct fm_tcam_match_entry *fmt;
1105 struct fm_action_op fm_op;
1112 ENICPMD_FUNC_TRACE();
1113 fmt = &fm->tcam_entry;
1117 vnic_h = 0; /* 0 = current vNIC */
1118 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1119 switch (actions->type) {
1120 case RTE_FLOW_ACTION_TYPE_VOID:
1122 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1123 if (overlap & PASSTHRU)
1125 overlap |= PASSTHRU;
1128 case RTE_FLOW_ACTION_TYPE_JUMP: {
1129 const struct rte_flow_action_jump *jump =
1131 struct enic_fm_fet *fet;
1135 ret = enic_fet_get(fm, jump->group, ingress, NULL,
1140 memset(&fm_op, 0, sizeof(fm_op));
1141 fm_op.fa_op = FMOP_EXACT_MATCH;
1142 fm_op.exact.handle = fet->handle;
1144 ret = enic_fm_append_action_op(fm, &fm_op, error);
1149 case RTE_FLOW_ACTION_TYPE_MARK: {
1150 const struct rte_flow_action_mark *mark =
1156 if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1157 return rte_flow_error_set(error, EINVAL,
1158 RTE_FLOW_ERROR_TYPE_ACTION,
1159 NULL, "invalid mark id");
1160 memset(&fm_op, 0, sizeof(fm_op));
1161 fm_op.fa_op = FMOP_MARK;
1162 fm_op.mark.mark = mark->id + 1;
1163 ret = enic_fm_append_action_op(fm, &fm_op, error);
1168 case RTE_FLOW_ACTION_TYPE_FLAG: {
1172 /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1173 memset(&fm_op, 0, sizeof(fm_op));
1174 fm_op.fa_op = FMOP_MARK;
1175 fm_op.mark.mark = ENIC_MAGIC_FILTER_ID;
1176 ret = enic_fm_append_action_op(fm, &fm_op, error);
1181 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1182 const struct rte_flow_action_queue *queue =
1186 * If other fate kind is set, fail. Multiple
1187 * queue actions are ok.
1189 if ((overlap & FATE) && first_rq)
1193 memset(&fm_op, 0, sizeof(fm_op));
1194 fm_op.fa_op = FMOP_RQ_STEER;
1195 fm_op.rq_steer.rq_index =
1196 enic_rte_rq_idx_to_sop_idx(queue->index);
1197 fm_op.rq_steer.vnic_handle = vnic_h;
1198 ret = enic_fm_append_action_op(fm, &fm_op, error);
1201 ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1202 fm_op.rq_steer.rq_index);
1205 case RTE_FLOW_ACTION_TYPE_DROP: {
1209 memset(&fm_op, 0, sizeof(fm_op));
1210 fm_op.fa_op = FMOP_DROP;
1211 ret = enic_fm_append_action_op(fm, &fm_op, error);
1214 ENICPMD_LOG(DEBUG, "create DROP action");
1217 case RTE_FLOW_ACTION_TYPE_COUNT: {
1218 if (overlap & COUNT)
1221 /* Count is associated with entry not action on VIC. */
1222 fmt->ftm_flags |= FMEF_COUNTER;
1225 case RTE_FLOW_ACTION_TYPE_RSS: {
1226 const struct rte_flow_action_rss *rss = actions->conf;
1231 * Hardware does not support general RSS actions, but
1232 * we can still support the dummy one that is used to
1233 * "receive normally".
1235 allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1238 rss->types == enic->rss_hf) &&
1239 rss->queue_num == enic->rq_count &&
1241 /* Identity queue map is ok */
1242 for (i = 0; i < rss->queue_num; i++)
1243 allow = allow && (i == rss->queue[i]);
1248 /* Need MARK or FLAG */
1249 if (!(overlap & MARK))
1254 case RTE_FLOW_ACTION_TYPE_PORT_ID: {
1255 const struct rte_flow_action_port_id *port;
1256 struct rte_pci_device *pdev;
1257 struct rte_eth_dev *dev;
1259 port = actions->conf;
1260 if (port->original) {
1261 vnic_h = 0; /* This port */
1264 ENICPMD_LOG(DEBUG, "port id %u", port->id);
1265 if (!rte_eth_dev_is_valid_port(port->id)) {
1266 return rte_flow_error_set(error, EINVAL,
1267 RTE_FLOW_ERROR_TYPE_ACTION,
1268 NULL, "invalid port_id");
1270 dev = &rte_eth_devices[port->id];
1271 if (!dev_is_enic(dev)) {
1272 return rte_flow_error_set(error, EINVAL,
1273 RTE_FLOW_ERROR_TYPE_ACTION,
1274 NULL, "port_id is not enic");
1276 pdev = RTE_ETH_DEV_TO_PCI(dev);
1277 if (enic_fm_find_vnic(enic, &pdev->addr, &vnic_h)) {
1278 return rte_flow_error_set(error, EINVAL,
1279 RTE_FLOW_ERROR_TYPE_ACTION,
1280 NULL, "port_id is not vnic");
1284 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP: {
1285 ret = enic_fm_copy_vxlan_decap(fm, fmt, actions,
1291 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP: {
1292 const struct rte_flow_action_vxlan_encap *encap;
1294 encap = actions->conf;
1295 if (overlap & ENCAP)
1298 ret = enic_fm_copy_vxlan_encap(fm, encap->definition,
1309 if (!(overlap & (FATE | PASSTHRU | COUNT)))
1311 memset(&fm_op, 0, sizeof(fm_op));
1312 fm_op.fa_op = FMOP_END;
1313 ret = enic_fm_append_action_op(fm, &fm_op, error);
1316 enic_fm_reorder_action_op(fm);
1320 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1321 NULL, "enic: unsupported action");
1324 /** Check if the action is supported */
1326 enic_fm_match_action(const struct rte_flow_action *action,
1327 const enum rte_flow_action_type *supported_actions)
1329 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1330 supported_actions++) {
1331 if (action->type == *supported_actions)
1337 /* Debug function to dump internal NIC action structure. */
1339 enic_fm_dump_tcam_actions(const struct fm_action *fm_action)
1341 /* Manually keep in sync with FMOP commands */
1342 const char *fmop_str[FMOP_OP_MAX] = {
1344 [FMOP_DROP] = "drop",
1345 [FMOP_RQ_STEER] = "steer",
1346 [FMOP_EXACT_MATCH] = "exmatch",
1347 [FMOP_MARK] = "mark",
1348 [FMOP_EXT_MARK] = "ext_mark",
1350 [FMOP_EG_HAIRPIN] = "eg_hairpin",
1351 [FMOP_IG_HAIRPIN] = "ig_hairpin",
1352 [FMOP_ENCAP_IVLAN] = "encap_ivlan",
1353 [FMOP_ENCAP_NOIVLAN] = "encap_noivlan",
1354 [FMOP_ENCAP] = "encap",
1355 [FMOP_SET_OVLAN] = "set_ovlan",
1356 [FMOP_DECAP_NOSTRIP] = "decap_nostrip",
1358 const struct fm_action_op *op = &fm_action->fma_action_ops[0];
1359 char buf[128], *bp = buf;
1364 buf_len = sizeof(buf);
1365 for (i = 0; i < FM_ACTION_OP_MAX; i++) {
1366 if (op->fa_op == FMOP_END)
1368 if (op->fa_op >= FMOP_OP_MAX)
1371 op_str = fmop_str[op->fa_op];
1372 n = snprintf(bp, buf_len, "%s,", op_str);
1373 if (n > 0 && n < buf_len) {
1379 /* Remove trailing comma */
1382 ENICPMD_LOG(DEBUG, " Acions: %s", buf);
1386 bits_to_str(uint32_t bits, const char *strings[], int max,
1387 char *buf, int buf_len)
1389 int i, n = 0, len = 0;
1391 for (i = 0; i < max; i++) {
1392 if (bits & (1 << i)) {
1393 n = snprintf(buf, buf_len, "%s,", strings[i]);
1394 if (n > 0 && n < buf_len) {
1401 /* Remove trailing comma */
1409 /* Debug function to dump internal NIC filter structure. */
1411 __enic_fm_dump_tcam_match(const struct fm_header_set *fk_hdrset, char *buf,
1414 /* Manually keep in sync with FKM_BITS */
1415 const char *fm_fkm_str[FKM_BIT_COUNT] = {
1416 [FKM_QTAG_BIT] = "qtag",
1417 [FKM_CMD_BIT] = "cmd",
1418 [FKM_IPV4_BIT] = "ip4",
1419 [FKM_IPV6_BIT] = "ip6",
1420 [FKM_ROCE_BIT] = "roce",
1421 [FKM_UDP_BIT] = "udp",
1422 [FKM_TCP_BIT] = "tcp",
1423 [FKM_TCPORUDP_BIT] = "tcpportudp",
1424 [FKM_IPFRAG_BIT] = "ipfrag",
1425 [FKM_NVGRE_BIT] = "nvgre",
1426 [FKM_VXLAN_BIT] = "vxlan",
1427 [FKM_GENEVE_BIT] = "geneve",
1428 [FKM_NSH_BIT] = "nsh",
1429 [FKM_ROCEV2_BIT] = "rocev2",
1430 [FKM_VLAN_PRES_BIT] = "vlan_pres",
1431 [FKM_IPOK_BIT] = "ipok",
1432 [FKM_L4OK_BIT] = "l4ok",
1433 [FKM_ROCEOK_BIT] = "roceok",
1434 [FKM_FCSOK_BIT] = "fcsok",
1435 [FKM_EG_SPAN_BIT] = "eg_span",
1436 [FKM_IG_SPAN_BIT] = "ig_span",
1437 [FKM_EG_HAIRPINNED_BIT] = "eg_hairpinned",
1439 /* Manually keep in sync with FKH_BITS */
1440 const char *fm_fkh_str[FKH_BIT_COUNT] = {
1441 [FKH_ETHER_BIT] = "eth",
1442 [FKH_QTAG_BIT] = "qtag",
1443 [FKH_L2RAW_BIT] = "l2raw",
1444 [FKH_IPV4_BIT] = "ip4",
1445 [FKH_IPV6_BIT] = "ip6",
1446 [FKH_L3RAW_BIT] = "l3raw",
1447 [FKH_UDP_BIT] = "udp",
1448 [FKH_TCP_BIT] = "tcp",
1449 [FKH_ICMP_BIT] = "icmp",
1450 [FKH_VXLAN_BIT] = "vxlan",
1451 [FKH_L4RAW_BIT] = "l4raw",
1453 uint32_t fkh_bits = fk_hdrset->fk_header_select;
1454 uint32_t fkm_bits = fk_hdrset->fk_metadata;
1457 if (!fkm_bits && !fkh_bits)
1459 n = snprintf(buf, buf_len, "metadata(");
1460 if (n > 0 && n < buf_len) {
1464 n = bits_to_str(fkm_bits, fm_fkm_str, FKM_BIT_COUNT, buf, buf_len);
1465 if (n > 0 && n < buf_len) {
1469 n = snprintf(buf, buf_len, ") valid hdr fields(");
1470 if (n > 0 && n < buf_len) {
1474 n = bits_to_str(fkh_bits, fm_fkh_str, FKH_BIT_COUNT, buf, buf_len);
1475 if (n > 0 && n < buf_len) {
1479 snprintf(buf, buf_len, ")");
1483 enic_fm_dump_tcam_match(const struct fm_tcam_match_entry *match,
1488 memset(buf, 0, sizeof(buf));
1489 __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[0],
1491 ENICPMD_LOG(DEBUG, " TCAM %s Outer: %s %scounter",
1492 (ingress) ? "IG" : "EG", buf,
1493 (match->ftm_flags & FMEF_COUNTER) ? "" : "no ");
1494 memset(buf, 0, sizeof(buf));
1495 __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[1],
1498 ENICPMD_LOG(DEBUG, " Inner: %s", buf);
1501 /* Debug function to dump internal NIC flow structures. */
1503 enic_fm_dump_tcam_entry(const struct fm_tcam_match_entry *fm_match,
1504 const struct fm_action *fm_action,
1507 if (!rte_log_can_log(enic_pmd_logtype, RTE_LOG_DEBUG))
1509 enic_fm_dump_tcam_match(fm_match, ingress);
1510 enic_fm_dump_tcam_actions(fm_action);
1514 enic_fm_flow_parse(struct enic_flowman *fm,
1515 const struct rte_flow_attr *attrs,
1516 const struct rte_flow_item pattern[],
1517 const struct rte_flow_action actions[],
1518 struct rte_flow_error *error)
1520 const struct rte_flow_action *action;
1522 static const enum rte_flow_action_type *sa;
1524 ENICPMD_FUNC_TRACE();
1527 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1528 NULL, "no pattern specified");
1533 rte_flow_error_set(error, EINVAL,
1534 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1535 NULL, "no action specified");
1540 if (attrs->priority) {
1541 rte_flow_error_set(error, ENOTSUP,
1542 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1544 "priorities are not supported");
1546 } else if (attrs->transfer) {
1547 rte_flow_error_set(error, ENOTSUP,
1548 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1550 "transfer is not supported");
1552 } else if (attrs->ingress && attrs->egress) {
1553 rte_flow_error_set(error, ENOTSUP,
1554 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1556 "bidirectional rules not supported");
1561 rte_flow_error_set(error, EINVAL,
1562 RTE_FLOW_ERROR_TYPE_ATTR,
1563 NULL, "no attribute specified");
1567 /* Verify Actions. */
1568 sa = (attrs->ingress) ? enic_fm_supported_ig_actions :
1569 enic_fm_supported_eg_actions;
1570 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1572 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1574 else if (!enic_fm_match_action(action, sa))
1577 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1578 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1579 action, "invalid action");
1582 ret = enic_fm_copy_entry(fm, pattern, error);
1585 ret = enic_fm_copy_action(fm, actions, attrs->ingress, error);
1590 enic_fm_counter_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1592 if (!fm_flow->counter_valid)
1594 SLIST_INSERT_HEAD(&fm->counters, fm_flow->counter, next);
1595 fm_flow->counter_valid = false;
1599 enic_fm_more_counters(struct enic_flowman *fm)
1601 struct enic_fm_counter *new_stack;
1602 struct enic_fm_counter *ctrs;
1607 ENICPMD_FUNC_TRACE();
1609 new_stack = rte_realloc(fm->counter_stack, (fm->counters_alloced +
1610 FM_COUNTERS_EXPAND) *
1611 sizeof(struct enic_fm_counter), 0);
1612 if (new_stack == NULL) {
1613 ENICPMD_LOG(ERR, "cannot alloc counter memory");
1616 fm->counter_stack = new_stack;
1618 args[0] = FM_COUNTER_BRK;
1619 args[1] = fm->counters_alloced + FM_COUNTERS_EXPAND;
1620 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1622 ENICPMD_LOG(ERR, "cannot alloc counters rc=%d", rc);
1625 ctrs = (struct enic_fm_counter *)fm->counter_stack +
1626 fm->counters_alloced;
1627 for (i = 0; i < FM_COUNTERS_EXPAND; i++, ctrs++) {
1628 ctrs->handle = fm->counters_alloced + i;
1629 SLIST_INSERT_HEAD(&fm->counters, ctrs, next);
1631 fm->counters_alloced += FM_COUNTERS_EXPAND;
1632 ENICPMD_LOG(DEBUG, "%u counters allocated, total: %u",
1633 FM_COUNTERS_EXPAND, fm->counters_alloced);
1638 enic_fm_counter_zero(struct enic_flowman *fm, struct enic_fm_counter *c)
1644 ENICPMD_FUNC_TRACE();
1646 args[0] = FM_COUNTER_QUERY;
1647 args[1] = c->handle;
1648 args[2] = 1; /* clear */
1649 ret = vnic_dev_flowman_cmd(enic->vdev, args, 3);
1651 ENICPMD_LOG(ERR, "counter init: rc=%d handle=0x%x",
1659 enic_fm_counter_alloc(struct enic_flowman *fm, struct rte_flow_error *error,
1660 struct enic_fm_counter **ctr)
1662 struct enic_fm_counter *c;
1665 ENICPMD_FUNC_TRACE();
1667 if (SLIST_EMPTY(&fm->counters)) {
1668 ret = enic_fm_more_counters(fm);
1670 return rte_flow_error_set(error, -ret,
1671 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1672 NULL, "enic: out of counters");
1674 c = SLIST_FIRST(&fm->counters);
1675 SLIST_REMOVE_HEAD(&fm->counters, next);
1681 enic_fm_action_free(struct enic_flowman *fm, uint64_t handle)
1686 ENICPMD_FUNC_TRACE();
1687 args[0] = FM_ACTION_FREE;
1689 rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1691 ENICPMD_LOG(ERR, "cannot free action: rc=%d handle=0x%" PRIx64,
1697 enic_fm_entry_free(struct enic_flowman *fm, uint64_t handle)
1702 ENICPMD_FUNC_TRACE();
1703 args[0] = FM_MATCH_ENTRY_REMOVE;
1705 rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1707 ENICPMD_LOG(ERR, "cannot free match entry: rc=%d"
1708 " handle=0x%" PRIx64, rc, handle);
1712 static struct enic_fm_jump_flow *
1713 find_jump_flow(struct enic_flowman *fm, uint32_t group)
1715 struct enic_fm_jump_flow *j;
1717 ENICPMD_FUNC_TRACE();
1718 TAILQ_FOREACH(j, &fm->jump_list, list) {
1719 if (j->group == group)
1726 remove_jump_flow(struct enic_flowman *fm, struct rte_flow *flow)
1728 struct enic_fm_jump_flow *j;
1730 ENICPMD_FUNC_TRACE();
1731 TAILQ_FOREACH(j, &fm->jump_list, list) {
1732 if (j->flow == flow) {
1733 TAILQ_REMOVE(&fm->jump_list, j, list);
1741 save_jump_flow(struct enic_flowman *fm,
1742 struct rte_flow *flow,
1744 struct fm_tcam_match_entry *match,
1745 struct fm_action *action)
1747 struct enic_fm_jump_flow *j;
1749 ENICPMD_FUNC_TRACE();
1750 j = calloc(1, sizeof(struct enic_fm_jump_flow));
1756 j->action = *action;
1757 TAILQ_INSERT_HEAD(&fm->jump_list, j, list);
1758 ENICPMD_LOG(DEBUG, "saved jump flow: flow=%p group=%u", flow, group);
1763 __enic_fm_flow_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1765 if (fm_flow->entry_handle != FM_INVALID_HANDLE) {
1766 enic_fm_entry_free(fm, fm_flow->entry_handle);
1767 fm_flow->entry_handle = FM_INVALID_HANDLE;
1769 if (fm_flow->action_handle != FM_INVALID_HANDLE) {
1770 enic_fm_action_free(fm, fm_flow->action_handle);
1771 fm_flow->action_handle = FM_INVALID_HANDLE;
1773 enic_fm_counter_free(fm, fm_flow);
1775 enic_fet_put(fm, fm_flow->fet);
1776 fm_flow->fet = NULL;
1781 enic_fm_flow_free(struct enic_flowman *fm, struct rte_flow *flow)
1783 if (flow->fm->fet && flow->fm->fet->default_key)
1784 remove_jump_flow(fm, flow);
1785 __enic_fm_flow_free(fm, flow->fm);
1791 enic_fm_add_tcam_entry(struct enic_flowman *fm,
1792 struct fm_tcam_match_entry *match_in,
1793 uint64_t *entry_handle,
1795 struct rte_flow_error *error)
1797 struct fm_tcam_match_entry *ftm;
1801 ENICPMD_FUNC_TRACE();
1802 /* Copy entry to the command buffer */
1803 ftm = &fm->cmd.va->fm_tcam_match_entry;
1804 memcpy(ftm, match_in, sizeof(*ftm));
1805 /* Add TCAM entry */
1806 args[0] = FM_TCAM_ENTRY_INSTALL;
1807 args[1] = ingress ? fm->ig_tcam_hndl : fm->eg_tcam_hndl;
1808 args[2] = fm->cmd.pa;
1809 ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 3);
1811 ENICPMD_LOG(ERR, "cannot add %s TCAM entry: rc=%d",
1812 ingress ? "ingress" : "egress", ret);
1813 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1814 NULL, "enic: devcmd(tcam-entry-install)");
1817 ENICPMD_LOG(DEBUG, "installed %s TCAM entry: handle=0x%" PRIx64,
1818 ingress ? "ingress" : "egress", (uint64_t)args[0]);
1819 *entry_handle = args[0];
1824 enic_fm_add_exact_entry(struct enic_flowman *fm,
1825 struct fm_tcam_match_entry *match_in,
1826 uint64_t *entry_handle,
1827 struct enic_fm_fet *fet,
1828 struct rte_flow_error *error)
1830 struct fm_exact_match_entry *fem;
1834 ENICPMD_FUNC_TRACE();
1835 /* The new entry must have the table's key */
1836 if (memcmp(fet->key.fk_hdrset, match_in->ftm_mask.fk_hdrset,
1837 sizeof(struct fm_header_set) * FM_HDRSET_MAX)) {
1838 return rte_flow_error_set(error, EINVAL,
1839 RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1840 "enic: key does not match group's key");
1843 /* Copy entry to the command buffer */
1844 fem = &fm->cmd.va->fm_exact_match_entry;
1846 * Translate TCAM entry to exact entry. As is only need to drop
1847 * position and mask. The mask is part of the exact match table.
1848 * Position (aka priority) is not supported in the exact match table.
1850 fem->fem_data = match_in->ftm_data;
1851 fem->fem_flags = match_in->ftm_flags;
1852 fem->fem_action = match_in->ftm_action;
1853 fem->fem_counter = match_in->ftm_counter;
1855 /* Add exact entry */
1856 args[0] = FM_EXACT_ENTRY_INSTALL;
1857 args[1] = fet->handle;
1858 args[2] = fm->cmd.pa;
1859 ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 3);
1861 ENICPMD_LOG(ERR, "cannot add %s exact entry: group=%u",
1862 fet->ingress ? "ingress" : "egress", fet->group);
1863 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1864 NULL, "enic: devcmd(exact-entry-install)");
1867 ENICPMD_LOG(DEBUG, "installed %s exact entry: group=%u"
1868 " handle=0x%" PRIx64,
1869 fet->ingress ? "ingress" : "egress", fet->group,
1871 *entry_handle = args[0];
1875 /* Push match-action to the NIC. */
1877 __enic_fm_flow_add_entry(struct enic_flowman *fm,
1878 struct enic_fm_flow *fm_flow,
1879 struct fm_tcam_match_entry *match_in,
1880 struct fm_action *action_in,
1883 struct rte_flow_error *error)
1885 struct enic_fm_counter *ctr;
1886 struct fm_action *fma;
1892 ENICPMD_FUNC_TRACE();
1893 /* Allocate action. */
1894 fma = &fm->cmd.va->fm_action;
1895 memcpy(fma, action_in, sizeof(*fma));
1896 args[0] = FM_ACTION_ALLOC;
1897 args[1] = fm->cmd.pa;
1898 ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1900 ENICPMD_LOG(ERR, "allocating TCAM table action rc=%d", ret);
1901 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1902 NULL, "enic: devcmd(action-alloc)");
1906 fm_flow->action_handle = action_h;
1907 match_in->ftm_action = action_h;
1908 ENICPMD_LOG(DEBUG, "action allocated: handle=0x%" PRIx64, action_h);
1910 /* Allocate counter if requested. */
1911 if (match_in->ftm_flags & FMEF_COUNTER) {
1912 ret = enic_fm_counter_alloc(fm, error, &ctr);
1913 if (ret) /* error has been filled in */
1915 fm_flow->counter_valid = true;
1916 fm_flow->counter = ctr;
1917 match_in->ftm_counter = ctr->handle;
1921 * Get the group's table (either TCAM or exact match table) and
1922 * add entry to it. If we use the exact match table, the handler
1923 * will translate the TCAM entry (match_in) to the appropriate
1924 * exact match entry and use that instead.
1926 entry_h = FM_INVALID_HANDLE;
1927 if (group == FM_TCAM_RTE_GROUP) {
1928 ret = enic_fm_add_tcam_entry(fm, match_in, &entry_h, ingress,
1932 /* Jump action might have a ref to fet */
1933 fm_flow->fet = fm->fet;
1936 struct enic_fm_fet *fet = NULL;
1938 ret = enic_fet_get(fm, group, ingress,
1939 &match_in->ftm_mask, &fet, error);
1943 ret = enic_fm_add_exact_entry(fm, match_in, &entry_h, fet,
1948 /* Clear counter after adding entry, as it requires in-use counter */
1949 if (fm_flow->counter_valid) {
1950 ret = enic_fm_counter_zero(fm, fm_flow->counter);
1954 fm_flow->entry_handle = entry_h;
1958 /* Push match-action to the NIC. */
1959 static struct rte_flow *
1960 enic_fm_flow_add_entry(struct enic_flowman *fm,
1961 struct fm_tcam_match_entry *match_in,
1962 struct fm_action *action_in,
1963 const struct rte_flow_attr *attrs,
1964 struct rte_flow_error *error)
1966 struct enic_fm_flow *fm_flow;
1967 struct rte_flow *flow;
1969 ENICPMD_FUNC_TRACE();
1970 enic_fm_dump_tcam_entry(match_in, action_in, attrs->ingress);
1971 flow = calloc(1, sizeof(*flow));
1972 fm_flow = calloc(1, sizeof(*fm_flow));
1973 if (flow == NULL || fm_flow == NULL) {
1974 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1975 NULL, "enic: cannot allocate rte_flow");
1981 fm_flow->action_handle = FM_INVALID_HANDLE;
1982 fm_flow->entry_handle = FM_INVALID_HANDLE;
1983 if (__enic_fm_flow_add_entry(fm, fm_flow, match_in, action_in,
1984 attrs->group, attrs->ingress, error)) {
1985 enic_fm_flow_free(fm, flow);
1992 convert_jump_flows(struct enic_flowman *fm, struct enic_fm_fet *fet,
1993 struct rte_flow_error *error)
1995 struct enic_fm_flow *fm_flow;
1996 struct enic_fm_jump_flow *j;
1997 struct fm_action *fma;
2000 ENICPMD_FUNC_TRACE();
2002 * Find the saved flows that should jump to the new table (fet).
2003 * Then delete the old TCAM entry that jumps to the default table,
2004 * and add a new one that jumps to the new table.
2007 j = find_jump_flow(fm, group);
2009 ENICPMD_LOG(DEBUG, "convert jump flow: flow=%p group=%u",
2011 /* Delete old entry */
2012 fm_flow = j->flow->fm;
2013 __enic_fm_flow_free(fm, fm_flow);
2017 fma->fma_action_ops[0].exact.handle = fet->handle;
2018 if (__enic_fm_flow_add_entry(fm, fm_flow, &j->match, fma,
2019 FM_TCAM_RTE_GROUP, fet->ingress, error)) {
2020 /* Cannot roll back changes at the moment */
2021 ENICPMD_LOG(ERR, "cannot convert jump flow: flow=%p",
2026 ENICPMD_LOG(DEBUG, "convert ok: group=%u ref=%u",
2027 fet->group, fet->ref);
2030 TAILQ_REMOVE(&fm->jump_list, j, list);
2032 j = find_jump_flow(fm, group);
2037 enic_fm_open_scratch(struct enic_flowman *fm)
2039 fm->action_op_count = 0;
2041 memset(&fm->tcam_entry, 0, sizeof(fm->tcam_entry));
2042 memset(&fm->action, 0, sizeof(fm->action));
2046 enic_fm_close_scratch(struct enic_flowman *fm)
2049 enic_fet_put(fm, fm->fet);
2052 fm->action_op_count = 0;
2056 enic_fm_flow_validate(struct rte_eth_dev *dev,
2057 const struct rte_flow_attr *attrs,
2058 const struct rte_flow_item pattern[],
2059 const struct rte_flow_action actions[],
2060 struct rte_flow_error *error)
2062 struct fm_tcam_match_entry *fm_tcam_entry;
2063 struct fm_action *fm_action;
2064 struct enic_flowman *fm;
2067 ENICPMD_FUNC_TRACE();
2068 fm = pmd_priv(dev)->fm;
2071 enic_fm_open_scratch(fm);
2072 ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2074 fm_tcam_entry = &fm->tcam_entry;
2075 fm_action = &fm->action;
2076 enic_fm_dump_tcam_entry(fm_tcam_entry, fm_action,
2079 enic_fm_close_scratch(fm);
2084 enic_fm_flow_query_count(struct rte_eth_dev *dev,
2085 struct rte_flow *flow, void *data,
2086 struct rte_flow_error *error)
2088 struct rte_flow_query_count *query;
2089 struct enic_fm_flow *fm_flow;
2094 ENICPMD_FUNC_TRACE();
2095 enic = pmd_priv(dev);
2098 if (!fm_flow->counter_valid)
2099 return rte_flow_error_set(error, ENOTSUP,
2100 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2101 "enic: flow does not have counter");
2103 args[0] = FM_COUNTER_QUERY;
2104 args[1] = fm_flow->counter->handle;
2105 args[2] = query->reset;
2106 rc = vnic_dev_flowman_cmd(enic->vdev, args, 3);
2108 ENICPMD_LOG(ERR, "cannot query counter: rc=%d handle=0x%x",
2109 rc, fm_flow->counter->handle);
2112 query->hits_set = 1;
2113 query->hits = args[0];
2114 query->bytes_set = 1;
2115 query->bytes = args[1];
2120 enic_fm_flow_query(struct rte_eth_dev *dev,
2121 struct rte_flow *flow,
2122 const struct rte_flow_action *actions,
2124 struct rte_flow_error *error)
2128 ENICPMD_FUNC_TRACE();
2129 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2130 switch (actions->type) {
2131 case RTE_FLOW_ACTION_TYPE_VOID:
2133 case RTE_FLOW_ACTION_TYPE_COUNT:
2134 ret = enic_fm_flow_query_count(dev, flow, data, error);
2137 return rte_flow_error_set(error, ENOTSUP,
2138 RTE_FLOW_ERROR_TYPE_ACTION,
2140 "action not supported");
2148 static struct rte_flow *
2149 enic_fm_flow_create(struct rte_eth_dev *dev,
2150 const struct rte_flow_attr *attrs,
2151 const struct rte_flow_item pattern[],
2152 const struct rte_flow_action actions[],
2153 struct rte_flow_error *error)
2155 struct fm_tcam_match_entry *fm_tcam_entry;
2156 struct fm_action *fm_action;
2157 struct enic_flowman *fm;
2158 struct enic_fm_fet *fet;
2159 struct rte_flow *flow;
2163 ENICPMD_FUNC_TRACE();
2164 enic = pmd_priv(dev);
2167 rte_flow_error_set(error, ENOTSUP,
2168 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2169 "flowman is not initialized");
2172 enic_fm_open_scratch(fm);
2174 ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2176 goto error_with_scratch;
2177 fm_tcam_entry = &fm->tcam_entry;
2178 fm_action = &fm->action;
2179 flow = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2182 LIST_INSERT_HEAD(&enic->flows, flow, next);
2183 fet = flow->fm->fet;
2184 if (fet && fet->default_key) {
2186 * Jump to non-existent group? Save the relevant info
2187 * so we can convert this flow when that group
2190 save_jump_flow(fm, flow, fet->group,
2191 fm_tcam_entry, fm_action);
2192 } else if (fet && fet->ref == 1) {
2194 * A new table is created. Convert the saved flows
2195 * that should jump to this group.
2197 convert_jump_flows(fm, fet, error);
2202 enic_fm_close_scratch(fm);
2207 enic_fm_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
2208 __rte_unused struct rte_flow_error *error)
2210 struct enic *enic = pmd_priv(dev);
2212 ENICPMD_FUNC_TRACE();
2213 if (enic->fm == NULL)
2215 LIST_REMOVE(flow, next);
2216 enic_fm_flow_free(enic->fm, flow);
2221 enic_fm_flow_flush(struct rte_eth_dev *dev,
2222 __rte_unused struct rte_flow_error *error)
2224 struct enic_fm_flow *fm_flow;
2225 struct enic_flowman *fm;
2226 struct rte_flow *flow;
2227 struct enic *enic = pmd_priv(dev);
2229 ENICPMD_FUNC_TRACE();
2230 if (enic->fm == NULL)
2233 while (!LIST_EMPTY(&enic->flows)) {
2234 flow = LIST_FIRST(&enic->flows);
2236 LIST_REMOVE(flow, next);
2238 * If tables are null, then vNIC is closing, and the firmware
2239 * has already cleaned up flowman state. So do not try to free
2240 * resources, as it only causes errors.
2242 if (fm->ig_tcam_hndl == FM_INVALID_HANDLE) {
2243 fm_flow->entry_handle = FM_INVALID_HANDLE;
2244 fm_flow->action_handle = FM_INVALID_HANDLE;
2245 fm_flow->fet = NULL;
2247 enic_fm_flow_free(fm, flow);
2253 enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle)
2258 args[0] = FM_MATCH_TABLE_FREE;
2260 rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
2262 ENICPMD_LOG(ERR, "cannot free table: rc=%d handle=0x%" PRIx64,
2268 enic_fm_tcam_tbl_alloc(struct enic_flowman *fm, uint32_t direction,
2269 uint32_t max_entries, uint64_t *handle)
2271 struct fm_tcam_match_table *tcam_tbl;
2276 ENICPMD_FUNC_TRACE();
2278 tcam_tbl = &fm->cmd.va->fm_tcam_match_table;
2279 tcam_tbl->ftt_direction = direction;
2280 tcam_tbl->ftt_stage = FM_STAGE_LAST;
2281 tcam_tbl->ftt_max_entries = max_entries;
2282 args[0] = FM_TCAM_TABLE_ALLOC;
2283 args[1] = fm->cmd.pa;
2284 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
2286 ENICPMD_LOG(ERR, "cannot alloc %s TCAM table: rc=%d",
2287 (direction == FM_INGRESS) ? "IG" : "EG", rc);
2291 ENICPMD_LOG(DEBUG, "%s TCAM table allocated, handle=0x%" PRIx64,
2292 (direction == FM_INGRESS) ? "IG" : "EG", *handle);
2297 enic_fm_init_counters(struct enic_flowman *fm)
2299 ENICPMD_FUNC_TRACE();
2300 SLIST_INIT(&fm->counters);
2301 return enic_fm_more_counters(fm);
2305 enic_fm_free_all_counters(struct enic_flowman *fm)
2312 args[0] = FM_COUNTER_BRK;
2314 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
2316 ENICPMD_LOG(ERR, "cannot free counters: rc=%d", rc);
2317 rte_free(fm->counter_stack);
2321 enic_fm_alloc_tcam_tables(struct enic_flowman *fm)
2325 ENICPMD_FUNC_TRACE();
2326 rc = enic_fm_tcam_tbl_alloc(fm, FM_INGRESS, FM_MAX_TCAM_TABLE_SIZE,
2330 rc = enic_fm_tcam_tbl_alloc(fm, FM_EGRESS, FM_MAX_TCAM_TABLE_SIZE,
2336 enic_fm_free_tcam_tables(struct enic_flowman *fm)
2338 ENICPMD_FUNC_TRACE();
2339 if (fm->ig_tcam_hndl) {
2340 ENICPMD_LOG(DEBUG, "free IG TCAM table handle=0x%" PRIx64,
2342 enic_fm_tbl_free(fm, fm->ig_tcam_hndl);
2343 fm->ig_tcam_hndl = FM_INVALID_HANDLE;
2345 if (fm->eg_tcam_hndl) {
2346 ENICPMD_LOG(DEBUG, "free EG TCAM table handle=0x%" PRIx64,
2348 enic_fm_tbl_free(fm, fm->eg_tcam_hndl);
2349 fm->eg_tcam_hndl = FM_INVALID_HANDLE;
2354 enic_fm_init(struct enic *enic)
2356 struct enic_flowman *fm;
2357 uint8_t name[RTE_MEMZONE_NAMESIZE];
2360 if (enic->flow_filter_mode != FILTER_FLOWMAN)
2362 ENICPMD_FUNC_TRACE();
2363 fm = calloc(1, sizeof(*fm));
2365 ENICPMD_LOG(ERR, "cannot alloc flowman struct");
2369 TAILQ_INIT(&fm->fet_list);
2370 TAILQ_INIT(&fm->jump_list);
2371 /* Allocate host memory for flowman commands */
2372 snprintf((char *)name, sizeof(name), "fm-cmd-%s", enic->bdf_name);
2373 fm->cmd.va = enic_alloc_consistent(enic,
2374 sizeof(union enic_flowman_cmd_mem), &fm->cmd.pa, name);
2376 ENICPMD_LOG(ERR, "cannot allocate flowman command memory");
2380 /* Allocate TCAM tables upfront as they are the main tables */
2381 rc = enic_fm_alloc_tcam_tables(fm);
2383 ENICPMD_LOG(ERR, "cannot alloc TCAM tables");
2386 /* Then a number of counters */
2387 rc = enic_fm_init_counters(fm);
2389 ENICPMD_LOG(ERR, "cannot alloc counters");
2393 * One default exact match table for each direction. We hold onto
2396 rc = enic_fet_alloc(fm, 1, NULL, 128, &fm->default_ig_fet);
2398 ENICPMD_LOG(ERR, "cannot alloc default IG exact match table");
2399 goto error_counters;
2401 fm->default_ig_fet->ref = 1;
2402 rc = enic_fet_alloc(fm, 0, NULL, 128, &fm->default_eg_fet);
2404 ENICPMD_LOG(ERR, "cannot alloc default EG exact match table");
2407 fm->default_eg_fet->ref = 1;
2412 enic_fet_free(fm, fm->default_ig_fet);
2414 enic_fm_free_all_counters(fm);
2416 enic_fm_free_tcam_tables(fm);
2418 enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2419 fm->cmd.va, fm->cmd.pa);
2426 enic_fm_destroy(struct enic *enic)
2428 struct enic_flowman *fm;
2429 struct enic_fm_fet *fet;
2431 if (enic->fm == NULL)
2433 ENICPMD_FUNC_TRACE();
2435 enic_fet_free(fm, fm->default_eg_fet);
2436 enic_fet_free(fm, fm->default_ig_fet);
2437 /* Free all exact match tables still open */
2438 while (!TAILQ_EMPTY(&fm->fet_list)) {
2439 fet = TAILQ_FIRST(&fm->fet_list);
2440 enic_fet_free(fm, fet);
2442 enic_fm_free_tcam_tables(fm);
2443 enic_fm_free_all_counters(fm);
2444 enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2445 fm->cmd.va, fm->cmd.pa);
2451 const struct rte_flow_ops enic_fm_flow_ops = {
2452 .validate = enic_fm_flow_validate,
2453 .create = enic_fm_flow_create,
2454 .destroy = enic_fm_flow_destroy,
2455 .flush = enic_fm_flow_flush,
2456 .query = enic_fm_flow_query,