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_OF_POP_VLAN,
197 RTE_FLOW_ACTION_TYPE_PORT_ID,
198 RTE_FLOW_ACTION_TYPE_PASSTHRU,
199 RTE_FLOW_ACTION_TYPE_QUEUE,
200 RTE_FLOW_ACTION_TYPE_RSS,
201 RTE_FLOW_ACTION_TYPE_VOID,
202 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
203 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP,
204 RTE_FLOW_ACTION_TYPE_END, /* END must be the last entry */
208 static const enum rte_flow_action_type enic_fm_supported_eg_actions[] = {
209 RTE_FLOW_ACTION_TYPE_COUNT,
210 RTE_FLOW_ACTION_TYPE_DROP,
211 RTE_FLOW_ACTION_TYPE_JUMP,
212 RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN,
213 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP,
214 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID,
215 RTE_FLOW_ACTION_TYPE_PASSTHRU,
216 RTE_FLOW_ACTION_TYPE_VOID,
217 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
218 RTE_FLOW_ACTION_TYPE_END,
221 static const struct enic_fm_items enic_fm_items[] = {
222 [RTE_FLOW_ITEM_TYPE_RAW] = {
223 .copy_item = enic_fm_copy_item_raw,
224 .valid_start_item = 0,
225 .prev_items = (const enum rte_flow_item_type[]) {
226 RTE_FLOW_ITEM_TYPE_UDP,
227 RTE_FLOW_ITEM_TYPE_END,
230 [RTE_FLOW_ITEM_TYPE_ETH] = {
231 .copy_item = enic_fm_copy_item_eth,
232 .valid_start_item = 1,
233 .prev_items = (const enum rte_flow_item_type[]) {
234 RTE_FLOW_ITEM_TYPE_END,
237 [RTE_FLOW_ITEM_TYPE_VLAN] = {
238 .copy_item = enic_fm_copy_item_vlan,
239 .valid_start_item = 1,
240 .prev_items = (const enum rte_flow_item_type[]) {
241 RTE_FLOW_ITEM_TYPE_ETH,
242 RTE_FLOW_ITEM_TYPE_END,
245 [RTE_FLOW_ITEM_TYPE_IPV4] = {
246 .copy_item = enic_fm_copy_item_ipv4,
247 .valid_start_item = 1,
248 .prev_items = (const enum rte_flow_item_type[]) {
249 RTE_FLOW_ITEM_TYPE_ETH,
250 RTE_FLOW_ITEM_TYPE_VLAN,
251 RTE_FLOW_ITEM_TYPE_END,
254 [RTE_FLOW_ITEM_TYPE_IPV6] = {
255 .copy_item = enic_fm_copy_item_ipv6,
256 .valid_start_item = 1,
257 .prev_items = (const enum rte_flow_item_type[]) {
258 RTE_FLOW_ITEM_TYPE_ETH,
259 RTE_FLOW_ITEM_TYPE_VLAN,
260 RTE_FLOW_ITEM_TYPE_END,
263 [RTE_FLOW_ITEM_TYPE_UDP] = {
264 .copy_item = enic_fm_copy_item_udp,
265 .valid_start_item = 1,
266 .prev_items = (const enum rte_flow_item_type[]) {
267 RTE_FLOW_ITEM_TYPE_IPV4,
268 RTE_FLOW_ITEM_TYPE_IPV6,
269 RTE_FLOW_ITEM_TYPE_END,
272 [RTE_FLOW_ITEM_TYPE_TCP] = {
273 .copy_item = enic_fm_copy_item_tcp,
274 .valid_start_item = 1,
275 .prev_items = (const enum rte_flow_item_type[]) {
276 RTE_FLOW_ITEM_TYPE_IPV4,
277 RTE_FLOW_ITEM_TYPE_IPV6,
278 RTE_FLOW_ITEM_TYPE_END,
281 [RTE_FLOW_ITEM_TYPE_SCTP] = {
282 .copy_item = enic_fm_copy_item_sctp,
283 .valid_start_item = 0,
284 .prev_items = (const enum rte_flow_item_type[]) {
285 RTE_FLOW_ITEM_TYPE_IPV4,
286 RTE_FLOW_ITEM_TYPE_IPV6,
287 RTE_FLOW_ITEM_TYPE_END,
290 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
291 .copy_item = enic_fm_copy_item_vxlan,
292 .valid_start_item = 1,
293 .prev_items = (const enum rte_flow_item_type[]) {
294 RTE_FLOW_ITEM_TYPE_UDP,
295 RTE_FLOW_ITEM_TYPE_END,
301 enic_fm_copy_item_eth(struct copy_item_args *arg)
303 const struct rte_flow_item *item = arg->item;
304 const struct rte_flow_item_eth *spec = item->spec;
305 const struct rte_flow_item_eth *mask = item->mask;
306 const uint8_t lvl = arg->header_level;
307 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
308 struct fm_header_set *fm_data, *fm_mask;
310 ENICPMD_FUNC_TRACE();
311 /* Match all if no spec */
315 mask = &rte_flow_item_eth_mask;
316 fm_data = &entry->ftm_data.fk_hdrset[lvl];
317 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
318 fm_data->fk_header_select |= FKH_ETHER;
319 fm_mask->fk_header_select |= FKH_ETHER;
320 memcpy(&fm_data->l2.eth, spec, sizeof(*spec));
321 memcpy(&fm_mask->l2.eth, mask, sizeof(*mask));
326 enic_fm_copy_item_vlan(struct copy_item_args *arg)
328 const struct rte_flow_item *item = arg->item;
329 const struct rte_flow_item_vlan *spec = item->spec;
330 const struct rte_flow_item_vlan *mask = item->mask;
331 const uint8_t lvl = arg->header_level;
332 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
333 struct fm_header_set *fm_data, *fm_mask;
334 struct rte_ether_hdr *eth_mask;
335 struct rte_ether_hdr *eth_val;
338 ENICPMD_FUNC_TRACE();
339 fm_data = &entry->ftm_data.fk_hdrset[lvl];
340 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
341 /* Outer and inner packet vlans need different flags */
342 meta = FKM_VLAN_PRES;
345 fm_data->fk_metadata |= meta;
346 fm_mask->fk_metadata |= meta;
348 /* Match all if no spec */
352 mask = &rte_flow_item_vlan_mask;
354 eth_mask = (void *)&fm_mask->l2.eth;
355 eth_val = (void *)&fm_data->l2.eth;
357 /* Outer TPID cannot be matched */
358 if (eth_mask->ether_type)
362 * When packet matching, the VIC always compares vlan-stripped
363 * L2, regardless of vlan stripping settings. So, the inner type
364 * from vlan becomes the ether type of the eth header.
366 eth_mask->ether_type = mask->inner_type;
367 eth_val->ether_type = spec->inner_type;
368 fm_data->fk_header_select |= FKH_ETHER | FKH_QTAG;
369 fm_mask->fk_header_select |= FKH_ETHER | FKH_QTAG;
370 fm_data->fk_vlan = rte_be_to_cpu_16(spec->tci);
371 fm_mask->fk_vlan = rte_be_to_cpu_16(mask->tci);
376 enic_fm_copy_item_ipv4(struct copy_item_args *arg)
378 const struct rte_flow_item *item = arg->item;
379 const struct rte_flow_item_ipv4 *spec = item->spec;
380 const struct rte_flow_item_ipv4 *mask = item->mask;
381 const uint8_t lvl = arg->header_level;
382 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
383 struct fm_header_set *fm_data, *fm_mask;
385 ENICPMD_FUNC_TRACE();
386 fm_data = &entry->ftm_data.fk_hdrset[lvl];
387 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
388 fm_data->fk_metadata |= FKM_IPV4;
389 fm_mask->fk_metadata |= FKM_IPV4;
394 mask = &rte_flow_item_ipv4_mask;
396 fm_data->fk_header_select |= FKH_IPV4;
397 fm_mask->fk_header_select |= FKH_IPV4;
398 memcpy(&fm_data->l3.ip4, spec, sizeof(*spec));
399 memcpy(&fm_mask->l3.ip4, mask, sizeof(*mask));
404 enic_fm_copy_item_ipv6(struct copy_item_args *arg)
406 const struct rte_flow_item *item = arg->item;
407 const struct rte_flow_item_ipv6 *spec = item->spec;
408 const struct rte_flow_item_ipv6 *mask = item->mask;
409 const uint8_t lvl = arg->header_level;
410 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
411 struct fm_header_set *fm_data, *fm_mask;
413 ENICPMD_FUNC_TRACE();
414 fm_data = &entry->ftm_data.fk_hdrset[lvl];
415 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
416 fm_data->fk_metadata |= FKM_IPV6;
417 fm_mask->fk_metadata |= FKM_IPV6;
422 mask = &rte_flow_item_ipv6_mask;
424 fm_data->fk_header_select |= FKH_IPV6;
425 fm_mask->fk_header_select |= FKH_IPV6;
426 memcpy(&fm_data->l3.ip6, spec, sizeof(*spec));
427 memcpy(&fm_mask->l3.ip6, mask, sizeof(*mask));
432 enic_fm_copy_item_udp(struct copy_item_args *arg)
434 const struct rte_flow_item *item = arg->item;
435 const struct rte_flow_item_udp *spec = item->spec;
436 const struct rte_flow_item_udp *mask = item->mask;
437 const uint8_t lvl = arg->header_level;
438 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
439 struct fm_header_set *fm_data, *fm_mask;
441 ENICPMD_FUNC_TRACE();
442 fm_data = &entry->ftm_data.fk_hdrset[lvl];
443 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
444 fm_data->fk_metadata |= FKM_UDP;
445 fm_mask->fk_metadata |= FKM_UDP;
450 mask = &rte_flow_item_udp_mask;
452 fm_data->fk_header_select |= FKH_UDP;
453 fm_mask->fk_header_select |= FKH_UDP;
454 memcpy(&fm_data->l4.udp, spec, sizeof(*spec));
455 memcpy(&fm_mask->l4.udp, mask, sizeof(*mask));
460 enic_fm_copy_item_tcp(struct copy_item_args *arg)
462 const struct rte_flow_item *item = arg->item;
463 const struct rte_flow_item_tcp *spec = item->spec;
464 const struct rte_flow_item_tcp *mask = item->mask;
465 const uint8_t lvl = arg->header_level;
466 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
467 struct fm_header_set *fm_data, *fm_mask;
469 ENICPMD_FUNC_TRACE();
470 fm_data = &entry->ftm_data.fk_hdrset[lvl];
471 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
472 fm_data->fk_metadata |= FKM_TCP;
473 fm_mask->fk_metadata |= FKM_TCP;
478 mask = &rte_flow_item_tcp_mask;
480 fm_data->fk_header_select |= FKH_TCP;
481 fm_mask->fk_header_select |= FKH_TCP;
482 memcpy(&fm_data->l4.tcp, spec, sizeof(*spec));
483 memcpy(&fm_mask->l4.tcp, mask, sizeof(*mask));
488 enic_fm_copy_item_sctp(struct copy_item_args *arg)
490 const struct rte_flow_item *item = arg->item;
491 const struct rte_flow_item_sctp *spec = item->spec;
492 const struct rte_flow_item_sctp *mask = item->mask;
493 const uint8_t lvl = arg->header_level;
494 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
495 struct fm_header_set *fm_data, *fm_mask;
496 uint8_t *ip_proto_mask = NULL;
497 uint8_t *ip_proto = NULL;
500 ENICPMD_FUNC_TRACE();
501 fm_data = &entry->ftm_data.fk_hdrset[lvl];
502 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
504 * The NIC filter API has no flags for "match sctp", so explicitly
505 * set the protocol number in the IP pattern.
507 if (fm_data->fk_metadata & FKM_IPV4) {
508 struct rte_ipv4_hdr *ip;
509 ip = (struct rte_ipv4_hdr *)&fm_mask->l3.ip4;
510 ip_proto_mask = &ip->next_proto_id;
511 ip = (struct rte_ipv4_hdr *)&fm_data->l3.ip4;
512 ip_proto = &ip->next_proto_id;
514 } else if (fm_data->fk_metadata & FKM_IPV6) {
515 struct rte_ipv6_hdr *ip;
516 ip = (struct rte_ipv6_hdr *)&fm_mask->l3.ip6;
517 ip_proto_mask = &ip->proto;
518 ip = (struct rte_ipv6_hdr *)&fm_data->l3.ip6;
519 ip_proto = &ip->proto;
522 /* Need IPv4/IPv6 pattern first */
525 *ip_proto = IPPROTO_SCTP;
526 *ip_proto_mask = 0xff;
527 fm_data->fk_header_select |= l3_fkh;
528 fm_mask->fk_header_select |= l3_fkh;
533 mask = &rte_flow_item_sctp_mask;
535 fm_data->fk_header_select |= FKH_L4RAW;
536 fm_mask->fk_header_select |= FKH_L4RAW;
537 memcpy(fm_data->l4.rawdata, spec, sizeof(*spec));
538 memcpy(fm_mask->l4.rawdata, mask, sizeof(*mask));
543 enic_fm_copy_item_vxlan(struct copy_item_args *arg)
545 const struct rte_flow_item *item = arg->item;
546 const struct rte_flow_item_vxlan *spec = item->spec;
547 const struct rte_flow_item_vxlan *mask = item->mask;
548 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
549 struct fm_header_set *fm_data, *fm_mask;
551 ENICPMD_FUNC_TRACE();
552 /* Only 2 header levels (outer and inner) allowed */
553 if (arg->header_level > 0)
556 fm_data = &entry->ftm_data.fk_hdrset[0];
557 fm_mask = &entry->ftm_mask.fk_hdrset[0];
558 fm_data->fk_metadata |= FKM_VXLAN;
559 fm_mask->fk_metadata |= FKM_VXLAN;
560 /* items from here on out are inner header items */
561 arg->header_level = 1;
563 /* Match all if no spec */
567 mask = &rte_flow_item_vxlan_mask;
569 fm_data->fk_header_select |= FKH_VXLAN;
570 fm_mask->fk_header_select |= FKH_VXLAN;
571 memcpy(&fm_data->vxlan, spec, sizeof(*spec));
572 memcpy(&fm_mask->vxlan, mask, sizeof(*mask));
577 * Currently, raw pattern match is very limited. It is intended for matching
578 * UDP tunnel header (e.g. vxlan or geneve).
581 enic_fm_copy_item_raw(struct copy_item_args *arg)
583 const struct rte_flow_item *item = arg->item;
584 const struct rte_flow_item_raw *spec = item->spec;
585 const struct rte_flow_item_raw *mask = item->mask;
586 const uint8_t lvl = arg->header_level;
587 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
588 struct fm_header_set *fm_data, *fm_mask;
590 ENICPMD_FUNC_TRACE();
591 /* Cannot be used for inner packet */
594 /* Need both spec and mask */
597 /* Only supports relative with offset 0 */
598 if (!spec->relative || spec->offset != 0 || spec->search ||
601 /* Need non-null pattern that fits within the NIC's filter pattern */
602 if (spec->length == 0 ||
603 spec->length + sizeof(struct rte_udp_hdr) > FM_LAYER_SIZE ||
604 !spec->pattern || !mask->pattern)
607 * Mask fields, including length, are often set to zero. Assume that
608 * means "same as spec" to avoid breaking existing apps. If length
609 * is not zero, then it should be >= spec length.
611 * No more pattern follows this, so append to the L4 layer instead of
612 * L5 to work with both recent and older VICs.
614 if (mask->length != 0 && mask->length < spec->length)
617 fm_data = &entry->ftm_data.fk_hdrset[lvl];
618 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
619 fm_data->fk_header_select |= FKH_L4RAW;
620 fm_mask->fk_header_select |= FKH_L4RAW;
621 fm_data->fk_header_select &= ~FKH_UDP;
622 fm_mask->fk_header_select &= ~FKH_UDP;
623 memcpy(fm_data->l4.rawdata + sizeof(struct rte_udp_hdr),
624 spec->pattern, spec->length);
625 memcpy(fm_mask->l4.rawdata + sizeof(struct rte_udp_hdr),
626 mask->pattern, spec->length);
631 enic_fet_alloc(struct enic_flowman *fm, uint8_t ingress,
632 struct fm_key_template *key, int entries,
633 struct enic_fm_fet **fet_out)
635 struct fm_exact_match_table *cmd;
636 struct fm_header_set *hdr;
637 struct enic_fm_fet *fet;
641 ENICPMD_FUNC_TRACE();
642 fet = calloc(1, sizeof(struct enic_fm_fet));
645 cmd = &fm->cmd.va->fm_exact_match_table;
646 memset(cmd, 0, sizeof(*cmd));
647 cmd->fet_direction = ingress ? FM_INGRESS : FM_EGRESS;
648 cmd->fet_stage = FM_STAGE_LAST;
649 cmd->fet_max_entries = entries ? entries : FM_MAX_EXACT_TABLE_SIZE;
651 hdr = &cmd->fet_key.fk_hdrset[0];
652 memset(hdr, 0, sizeof(*hdr));
653 hdr->fk_header_select = FKH_IPV4 | FKH_UDP;
654 hdr->l3.ip4.fk_saddr = 0xFFFFFFFF;
655 hdr->l3.ip4.fk_daddr = 0xFFFFFFFF;
656 hdr->l4.udp.fk_source = 0xFFFF;
657 hdr->l4.udp.fk_dest = 0xFFFF;
658 fet->default_key = 1;
660 memcpy(&cmd->fet_key, key, sizeof(*key));
661 memcpy(&fet->key, key, sizeof(*key));
662 fet->default_key = 0;
664 cmd->fet_key.fk_packet_tag = 1;
666 args[0] = FM_EXACT_TABLE_ALLOC;
667 args[1] = fm->cmd.pa;
668 ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
670 ENICPMD_LOG(ERR, "cannot alloc exact match table: rc=%d", ret);
674 fet->handle = args[0];
675 fet->ingress = ingress;
676 ENICPMD_LOG(DEBUG, "allocated exact match table: handle=0x%" PRIx64,
683 enic_fet_free(struct enic_flowman *fm, struct enic_fm_fet *fet)
685 ENICPMD_FUNC_TRACE();
686 enic_fm_tbl_free(fm, fet->handle);
687 if (!fet->default_key)
688 TAILQ_REMOVE(&fm->fet_list, fet, list);
693 * Get the exact match table for the given combination of
694 * <group, ingress, key>. Allocate one on the fly as necessary.
697 enic_fet_get(struct enic_flowman *fm,
700 struct fm_key_template *key,
701 struct enic_fm_fet **fet_out,
702 struct rte_flow_error *error)
704 struct enic_fm_fet *fet;
706 ENICPMD_FUNC_TRACE();
707 /* See if we already have this table open */
708 TAILQ_FOREACH(fet, &fm->fet_list, list) {
709 if (fet->group == group && fet->ingress == ingress)
713 /* Jumping to a non-existing group? Use the default table */
715 fet = ingress ? fm->default_ig_fet : fm->default_eg_fet;
716 } else if (enic_fet_alloc(fm, ingress, key, 0, &fet)) {
717 return rte_flow_error_set(error, EINVAL,
718 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
719 NULL, "enic: cannot get exact match table");
722 /* Default table is never on the open table list */
723 if (!fet->default_key)
724 TAILQ_INSERT_HEAD(&fm->fet_list, fet, list);
728 ENICPMD_LOG(DEBUG, "fet_get: %s %s group=%u ref=%u",
729 fet->default_key ? "default" : "",
730 fet->ingress ? "ingress" : "egress",
731 fet->group, fet->ref);
736 enic_fet_put(struct enic_flowman *fm, struct enic_fm_fet *fet)
738 ENICPMD_FUNC_TRACE();
739 RTE_ASSERT(fet->ref > 0);
741 ENICPMD_LOG(DEBUG, "fet_put: %s %s group=%u ref=%u",
742 fet->default_key ? "default" : "",
743 fet->ingress ? "ingress" : "egress",
744 fet->group, fet->ref);
746 enic_fet_free(fm, fet);
749 /* Return 1 if current item is valid on top of the previous one. */
751 fm_item_stacking_valid(enum rte_flow_item_type prev_item,
752 const struct enic_fm_items *item_info,
753 uint8_t is_first_item)
755 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
757 ENICPMD_FUNC_TRACE();
758 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
759 if (prev_item == *allowed_items)
763 /* This is the first item in the stack. Check if that's cool */
764 if (is_first_item && item_info->valid_start_item)
770 * Build the flow manager match entry structure from the provided pattern.
771 * The pattern is validated as the items are copied.
774 enic_fm_copy_entry(struct enic_flowman *fm,
775 const struct rte_flow_item pattern[],
776 struct rte_flow_error *error)
778 const struct enic_fm_items *item_info;
779 enum rte_flow_item_type prev_item;
780 const struct rte_flow_item *item;
781 struct copy_item_args args;
782 uint8_t prev_header_level;
783 uint8_t is_first_item;
786 ENICPMD_FUNC_TRACE();
789 prev_item = RTE_FLOW_ITEM_TYPE_END;
791 args.fm_tcam_entry = &fm->tcam_entry;
792 args.header_level = 0;
793 prev_header_level = 0;
794 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
796 * Get info about how to validate and copy the item. If NULL
797 * is returned the nic does not support the item.
799 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
802 item_info = &enic_fm_items[item->type];
804 if (item->type > FM_MAX_ITEM_TYPE ||
805 item_info->copy_item == NULL) {
806 return rte_flow_error_set(error, ENOTSUP,
807 RTE_FLOW_ERROR_TYPE_ITEM,
808 NULL, "enic: unsupported item");
811 /* check to see if item stacking is valid */
812 if (!fm_item_stacking_valid(prev_item, item_info,
817 ret = item_info->copy_item(&args);
819 goto item_not_supported;
820 /* Going from outer to inner? Treat it as a new packet start */
821 if (prev_header_level != args.header_level) {
822 prev_item = RTE_FLOW_ITEM_TYPE_END;
825 prev_item = item->type;
828 prev_header_level = args.header_level;
833 return rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_ITEM,
834 NULL, "enic: unsupported item type");
837 return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
838 item, "enic: unsupported item stack");
842 flow_item_skip_void(const struct rte_flow_item **item)
845 if ((*item)->type != RTE_FLOW_ITEM_TYPE_VOID)
850 append_template(void **template, uint8_t *off, const void *data, int len)
852 memcpy(*template, data, len);
853 *template = (char *)*template + len;
858 enic_fm_append_action_op(struct enic_flowman *fm,
859 struct fm_action_op *fm_op,
860 struct rte_flow_error *error)
864 count = fm->action_op_count;
865 ENICPMD_LOG(DEBUG, "append action op: idx=%d op=%u",
866 count, fm_op->fa_op);
867 if (count == FM_ACTION_OP_MAX) {
868 return rte_flow_error_set(error, EINVAL,
869 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
870 "too many action operations");
872 fm->action.fma_action_ops[count] = *fm_op;
873 fm->action_op_count = count + 1;
877 /* NIC requires that 1st steer appear before decap.
878 * Correct example: steer, decap, steer, steer, ...
881 enic_fm_reorder_action_op(struct enic_flowman *fm)
883 struct fm_action_op *op, *steer, *decap;
884 struct fm_action_op tmp_op;
886 ENICPMD_FUNC_TRACE();
887 /* Find 1st steer and decap */
888 op = fm->action.fma_action_ops;
891 while (op->fa_op != FMOP_END) {
892 if (!decap && op->fa_op == FMOP_DECAP_NOSTRIP)
894 else if (!steer && op->fa_op == FMOP_RQ_STEER)
898 /* If decap is before steer, swap */
899 if (steer && decap && decap < steer) {
900 op = fm->action.fma_action_ops;
901 ENICPMD_LOG(DEBUG, "swap decap %ld <-> steer %ld",
902 (long)(decap - op), (long)(steer - op));
909 /* VXLAN decap is done via flowman compound action */
911 enic_fm_copy_vxlan_decap(struct enic_flowman *fm,
912 struct fm_tcam_match_entry *fmt,
913 const struct rte_flow_action *action,
914 struct rte_flow_error *error)
916 struct fm_header_set *fm_data;
917 struct fm_action_op fm_op;
919 ENICPMD_FUNC_TRACE();
920 fm_data = &fmt->ftm_data.fk_hdrset[0];
921 if (!(fm_data->fk_metadata & FKM_VXLAN)) {
922 return rte_flow_error_set(error, EINVAL,
923 RTE_FLOW_ERROR_TYPE_ACTION, action,
924 "vxlan-decap: vxlan must be in pattern");
927 memset(&fm_op, 0, sizeof(fm_op));
928 fm_op.fa_op = FMOP_DECAP_NOSTRIP;
929 return enic_fm_append_action_op(fm, &fm_op, error);
932 /* VXLAN encap is done via flowman compound action */
934 enic_fm_copy_vxlan_encap(struct enic_flowman *fm,
935 const struct rte_flow_item *item,
936 struct rte_flow_error *error)
938 struct fm_action_op fm_op;
939 struct rte_ether_hdr *eth;
944 ENICPMD_FUNC_TRACE();
945 memset(&fm_op, 0, sizeof(fm_op));
946 fm_op.fa_op = FMOP_ENCAP;
947 template = fm->action.fma_data;
950 * Copy flow items to the flowman template starting L2.
951 * L2 must be ethernet.
953 flow_item_skip_void(&item);
954 if (item->type != RTE_FLOW_ITEM_TYPE_ETH)
955 return rte_flow_error_set(error, EINVAL,
956 RTE_FLOW_ERROR_TYPE_ITEM, item,
957 "vxlan-encap: first item should be ethernet");
958 eth = (struct rte_ether_hdr *)template;
959 ethertype = ð->ether_type;
960 append_template(&template, &off, item->spec,
961 sizeof(struct rte_flow_item_eth));
963 flow_item_skip_void(&item);
965 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
966 const struct rte_flow_item_vlan *spec;
968 ENICPMD_LOG(DEBUG, "vxlan-encap: vlan");
970 fm_op.encap.outer_vlan = rte_be_to_cpu_16(spec->tci);
972 flow_item_skip_void(&item);
974 /* L3 must be IPv4, IPv6 */
975 switch (item->type) {
976 case RTE_FLOW_ITEM_TYPE_IPV4:
978 struct rte_ipv4_hdr *ip4;
980 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv4");
981 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
982 ip4 = (struct rte_ipv4_hdr *)template;
984 * Offset of IPv4 length field and its initial value
985 * (IP + UDP + VXLAN) are specified in the action. The NIC
986 * will add inner packet length.
988 fm_op.encap.len1_offset = off +
989 offsetof(struct rte_ipv4_hdr, total_length);
990 fm_op.encap.len1_delta = sizeof(struct rte_ipv4_hdr) +
991 sizeof(struct rte_udp_hdr) +
992 sizeof(struct rte_vxlan_hdr);
993 append_template(&template, &off, item->spec,
994 sizeof(struct rte_ipv4_hdr));
995 ip4->version_ihl = RTE_IPV4_VHL_DEF;
996 if (ip4->time_to_live == 0)
997 ip4->time_to_live = IP_DEFTTL;
998 ip4->next_proto_id = IPPROTO_UDP;
1001 case RTE_FLOW_ITEM_TYPE_IPV6:
1003 struct rte_ipv6_hdr *ip6;
1005 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv6");
1006 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
1007 ip6 = (struct rte_ipv6_hdr *)template;
1008 fm_op.encap.len1_offset = off +
1009 offsetof(struct rte_ipv6_hdr, payload_len);
1010 fm_op.encap.len1_delta = sizeof(struct rte_udp_hdr) +
1011 sizeof(struct rte_vxlan_hdr);
1012 append_template(&template, &off, item->spec,
1013 sizeof(struct rte_ipv6_hdr));
1014 ip6->vtc_flow |= rte_cpu_to_be_32(IP6_VTC_FLOW);
1015 if (ip6->hop_limits == 0)
1016 ip6->hop_limits = IP_DEFTTL;
1017 ip6->proto = IPPROTO_UDP;
1021 return rte_flow_error_set(error,
1022 EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1023 "vxlan-encap: L3 must be IPv4/IPv6");
1026 flow_item_skip_void(&item);
1029 if (item->type != RTE_FLOW_ITEM_TYPE_UDP)
1030 return rte_flow_error_set(error, EINVAL,
1031 RTE_FLOW_ERROR_TYPE_ITEM, item,
1032 "vxlan-encap: UDP must follow IPv4/IPv6");
1033 /* UDP length = UDP + VXLAN. NIC will add inner packet length. */
1034 fm_op.encap.len2_offset =
1035 off + offsetof(struct rte_udp_hdr, dgram_len);
1036 fm_op.encap.len2_delta =
1037 sizeof(struct rte_udp_hdr) + sizeof(struct rte_vxlan_hdr);
1038 append_template(&template, &off, item->spec,
1039 sizeof(struct rte_udp_hdr));
1041 flow_item_skip_void(&item);
1044 if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN)
1045 return rte_flow_error_set(error,
1046 EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1047 "vxlan-encap: VXLAN must follow UDP");
1048 append_template(&template, &off, item->spec,
1049 sizeof(struct rte_flow_item_vxlan));
1052 * Fill in the rest of the action structure.
1053 * Indicate that we want to encap with vxlan at packet start.
1055 fm_op.encap.template_offset = 0;
1056 fm_op.encap.template_len = off;
1057 return enic_fm_append_action_op(fm, &fm_op, error);
1061 enic_fm_find_vnic(struct enic *enic, const struct rte_pci_addr *addr,
1068 ENICPMD_FUNC_TRACE();
1069 ENICPMD_LOG(DEBUG, "bdf=%x:%x:%x", addr->bus, addr->devid,
1071 bdf = addr->bus << 8 | addr->devid << 3 | addr->function;
1072 args[0] = FM_VNIC_FIND;
1074 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1076 /* Expected to fail if BDF is not on the adapter */
1077 ENICPMD_LOG(DEBUG, "cannot find vnic handle: rc=%d", rc);
1081 ENICPMD_LOG(DEBUG, "found vnic: handle=0x%" PRIx64, *handle);
1085 /* Translate flow actions to flowman TCAM entry actions */
1087 enic_fm_copy_action(struct enic_flowman *fm,
1088 const struct rte_flow_action actions[],
1090 struct rte_flow_error *error)
1100 struct fm_tcam_match_entry *fmt;
1101 struct fm_action_op fm_op;
1102 bool need_ovlan_action;
1110 ENICPMD_FUNC_TRACE();
1111 fmt = &fm->tcam_entry;
1112 need_ovlan_action = false;
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 =
1153 if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1154 return rte_flow_error_set(error, EINVAL,
1155 RTE_FLOW_ERROR_TYPE_ACTION,
1156 NULL, "invalid mark id");
1157 memset(&fm_op, 0, sizeof(fm_op));
1158 fm_op.fa_op = FMOP_MARK;
1159 fm_op.mark.mark = mark->id + 1;
1160 ret = enic_fm_append_action_op(fm, &fm_op, error);
1165 case RTE_FLOW_ACTION_TYPE_FLAG: {
1166 /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1167 memset(&fm_op, 0, sizeof(fm_op));
1168 fm_op.fa_op = FMOP_MARK;
1169 fm_op.mark.mark = ENIC_MAGIC_FILTER_ID;
1170 ret = enic_fm_append_action_op(fm, &fm_op, error);
1175 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1176 const struct rte_flow_action_queue *queue =
1180 * If fate other than QUEUE or RSS, fail. Multiple
1181 * rss and queue actions are ok.
1183 if ((overlap & FATE) && first_rq)
1187 memset(&fm_op, 0, sizeof(fm_op));
1188 fm_op.fa_op = FMOP_RQ_STEER;
1189 fm_op.rq_steer.rq_index =
1190 enic_rte_rq_idx_to_sop_idx(queue->index);
1191 fm_op.rq_steer.rq_count = 1;
1192 fm_op.rq_steer.vnic_handle = vnic_h;
1193 ret = enic_fm_append_action_op(fm, &fm_op, error);
1196 ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1197 fm_op.rq_steer.rq_index);
1200 case RTE_FLOW_ACTION_TYPE_DROP: {
1204 memset(&fm_op, 0, sizeof(fm_op));
1205 fm_op.fa_op = FMOP_DROP;
1206 ret = enic_fm_append_action_op(fm, &fm_op, error);
1209 ENICPMD_LOG(DEBUG, "create DROP action");
1212 case RTE_FLOW_ACTION_TYPE_COUNT: {
1213 if (overlap & COUNT)
1216 /* Count is associated with entry not action on VIC. */
1217 fmt->ftm_flags |= FMEF_COUNTER;
1220 case RTE_FLOW_ACTION_TYPE_RSS: {
1221 const struct rte_flow_action_rss *rss = actions->conf;
1226 * If fate other than QUEUE or RSS, fail. Multiple
1227 * rss and queue actions are ok.
1229 if ((overlap & FATE) && first_rq)
1235 * Hardware only supports RSS actions on outer level
1236 * with default type and function. Queues must be
1239 allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1240 rss->level == 0 && (rss->types == 0 ||
1241 rss->types == enic->rss_hf) &&
1242 rss->queue_num <= enic->rq_count &&
1243 rss->queue[rss->queue_num - 1] < enic->rq_count;
1246 /* Identity queue map needs to be sequential */
1247 for (i = 1; i < rss->queue_num; i++)
1248 allow = allow && (rss->queue[i] ==
1249 rss->queue[i - 1] + 1);
1253 memset(&fm_op, 0, sizeof(fm_op));
1254 fm_op.fa_op = FMOP_RQ_STEER;
1255 fm_op.rq_steer.rq_index =
1256 enic_rte_rq_idx_to_sop_idx(rss->queue[0]);
1257 fm_op.rq_steer.rq_count = rss->queue_num;
1258 fm_op.rq_steer.vnic_handle = vnic_h;
1259 ret = enic_fm_append_action_op(fm, &fm_op, error);
1262 ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1263 fm_op.rq_steer.rq_index);
1266 case RTE_FLOW_ACTION_TYPE_PORT_ID: {
1267 const struct rte_flow_action_port_id *port;
1268 struct rte_pci_device *pdev;
1269 struct rte_eth_dev *dev;
1271 port = actions->conf;
1272 if (port->original) {
1273 vnic_h = 0; /* This port */
1276 ENICPMD_LOG(DEBUG, "port id %u", port->id);
1277 if (!rte_eth_dev_is_valid_port(port->id)) {
1278 return rte_flow_error_set(error, EINVAL,
1279 RTE_FLOW_ERROR_TYPE_ACTION,
1280 NULL, "invalid port_id");
1282 dev = &rte_eth_devices[port->id];
1283 if (!dev_is_enic(dev)) {
1284 return rte_flow_error_set(error, EINVAL,
1285 RTE_FLOW_ERROR_TYPE_ACTION,
1286 NULL, "port_id is not enic");
1288 pdev = RTE_ETH_DEV_TO_PCI(dev);
1289 if (enic_fm_find_vnic(enic, &pdev->addr, &vnic_h)) {
1290 return rte_flow_error_set(error, EINVAL,
1291 RTE_FLOW_ERROR_TYPE_ACTION,
1292 NULL, "port_id is not vnic");
1296 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP: {
1297 if (overlap & DECAP)
1301 ret = enic_fm_copy_vxlan_decap(fm, fmt, actions,
1307 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP: {
1308 const struct rte_flow_action_vxlan_encap *encap;
1310 encap = actions->conf;
1311 if (overlap & ENCAP)
1314 ret = enic_fm_copy_vxlan_encap(fm, encap->definition,
1320 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN: {
1321 memset(&fm_op, 0, sizeof(fm_op));
1322 fm_op.fa_op = FMOP_POP_VLAN;
1323 ret = enic_fm_append_action_op(fm, &fm_op, error);
1328 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN: {
1329 const struct rte_flow_action_of_push_vlan *vlan;
1331 if (overlap & PASSTHRU)
1333 vlan = actions->conf;
1334 if (vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN)) {
1335 return rte_flow_error_set(error, EINVAL,
1336 RTE_FLOW_ERROR_TYPE_ACTION,
1337 NULL, "unexpected push_vlan ethertype");
1339 overlap |= PUSH_VLAN;
1340 need_ovlan_action = true;
1343 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP: {
1344 const struct rte_flow_action_of_set_vlan_pcp *pcp;
1346 pcp = actions->conf;
1347 if (pcp->vlan_pcp > 7) {
1348 return rte_flow_error_set(error, EINVAL,
1349 RTE_FLOW_ERROR_TYPE_ACTION,
1350 NULL, "invalid vlan_pcp");
1352 need_ovlan_action = true;
1353 ovlan |= ((uint16_t)pcp->vlan_pcp) << 13;
1356 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID: {
1357 const struct rte_flow_action_of_set_vlan_vid *vid;
1359 vid = actions->conf;
1360 need_ovlan_action = true;
1361 ovlan |= rte_be_to_cpu_16(vid->vlan_vid);
1369 if (!(overlap & (FATE | PASSTHRU | COUNT)))
1371 if (need_ovlan_action) {
1372 memset(&fm_op, 0, sizeof(fm_op));
1373 fm_op.fa_op = FMOP_SET_OVLAN;
1374 fm_op.ovlan.vlan = ovlan;
1375 ret = enic_fm_append_action_op(fm, &fm_op, error);
1379 memset(&fm_op, 0, sizeof(fm_op));
1380 fm_op.fa_op = FMOP_END;
1381 ret = enic_fm_append_action_op(fm, &fm_op, error);
1384 enic_fm_reorder_action_op(fm);
1388 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1389 NULL, "enic: unsupported action");
1392 /** Check if the action is supported */
1394 enic_fm_match_action(const struct rte_flow_action *action,
1395 const enum rte_flow_action_type *supported_actions)
1397 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1398 supported_actions++) {
1399 if (action->type == *supported_actions)
1405 /* Debug function to dump internal NIC action structure. */
1407 enic_fm_dump_tcam_actions(const struct fm_action *fm_action)
1409 /* Manually keep in sync with FMOP commands */
1410 const char *fmop_str[FMOP_OP_MAX] = {
1412 [FMOP_DROP] = "drop",
1413 [FMOP_RQ_STEER] = "steer",
1414 [FMOP_EXACT_MATCH] = "exmatch",
1415 [FMOP_MARK] = "mark",
1416 [FMOP_EXT_MARK] = "ext_mark",
1418 [FMOP_EG_HAIRPIN] = "eg_hairpin",
1419 [FMOP_IG_HAIRPIN] = "ig_hairpin",
1420 [FMOP_ENCAP_IVLAN] = "encap_ivlan",
1421 [FMOP_ENCAP_NOIVLAN] = "encap_noivlan",
1422 [FMOP_ENCAP] = "encap",
1423 [FMOP_SET_OVLAN] = "set_ovlan",
1424 [FMOP_DECAP_NOSTRIP] = "decap_nostrip",
1425 [FMOP_DECAP_STRIP] = "decap_strip",
1426 [FMOP_POP_VLAN] = "pop_vlan",
1427 [FMOP_SET_EGPORT] = "set_egport",
1428 [FMOP_RQ_STEER_ONLY] = "rq_steer_only",
1429 [FMOP_SET_ENCAP_VLAN] = "set_encap_vlan",
1430 [FMOP_EMIT] = "emit",
1431 [FMOP_MODIFY] = "modify",
1433 const struct fm_action_op *op = &fm_action->fma_action_ops[0];
1434 char buf[128], *bp = buf;
1439 buf_len = sizeof(buf);
1440 for (i = 0; i < FM_ACTION_OP_MAX; i++) {
1441 if (op->fa_op == FMOP_END)
1443 if (op->fa_op >= FMOP_OP_MAX)
1446 op_str = fmop_str[op->fa_op];
1447 n = snprintf(bp, buf_len, "%s,", op_str);
1448 if (n > 0 && n < buf_len) {
1454 /* Remove trailing comma */
1457 ENICPMD_LOG(DEBUG, " Acions: %s", buf);
1461 bits_to_str(uint32_t bits, const char *strings[], int max,
1462 char *buf, int buf_len)
1464 int i, n = 0, len = 0;
1466 for (i = 0; i < max; i++) {
1467 if (bits & (1 << i)) {
1468 n = snprintf(buf, buf_len, "%s,", strings[i]);
1469 if (n > 0 && n < buf_len) {
1476 /* Remove trailing comma */
1484 /* Debug function to dump internal NIC filter structure. */
1486 __enic_fm_dump_tcam_match(const struct fm_header_set *fk_hdrset, char *buf,
1489 /* Manually keep in sync with FKM_BITS */
1490 const char *fm_fkm_str[FKM_BIT_COUNT] = {
1491 [FKM_QTAG_BIT] = "qtag",
1492 [FKM_CMD_BIT] = "cmd",
1493 [FKM_IPV4_BIT] = "ip4",
1494 [FKM_IPV6_BIT] = "ip6",
1495 [FKM_ROCE_BIT] = "roce",
1496 [FKM_UDP_BIT] = "udp",
1497 [FKM_TCP_BIT] = "tcp",
1498 [FKM_TCPORUDP_BIT] = "tcpportudp",
1499 [FKM_IPFRAG_BIT] = "ipfrag",
1500 [FKM_NVGRE_BIT] = "nvgre",
1501 [FKM_VXLAN_BIT] = "vxlan",
1502 [FKM_GENEVE_BIT] = "geneve",
1503 [FKM_NSH_BIT] = "nsh",
1504 [FKM_ROCEV2_BIT] = "rocev2",
1505 [FKM_VLAN_PRES_BIT] = "vlan_pres",
1506 [FKM_IPOK_BIT] = "ipok",
1507 [FKM_L4OK_BIT] = "l4ok",
1508 [FKM_ROCEOK_BIT] = "roceok",
1509 [FKM_FCSOK_BIT] = "fcsok",
1510 [FKM_EG_SPAN_BIT] = "eg_span",
1511 [FKM_IG_SPAN_BIT] = "ig_span",
1512 [FKM_EG_HAIRPINNED_BIT] = "eg_hairpinned",
1514 /* Manually keep in sync with FKH_BITS */
1515 const char *fm_fkh_str[FKH_BIT_COUNT] = {
1516 [FKH_ETHER_BIT] = "eth",
1517 [FKH_QTAG_BIT] = "qtag",
1518 [FKH_L2RAW_BIT] = "l2raw",
1519 [FKH_IPV4_BIT] = "ip4",
1520 [FKH_IPV6_BIT] = "ip6",
1521 [FKH_L3RAW_BIT] = "l3raw",
1522 [FKH_UDP_BIT] = "udp",
1523 [FKH_TCP_BIT] = "tcp",
1524 [FKH_ICMP_BIT] = "icmp",
1525 [FKH_VXLAN_BIT] = "vxlan",
1526 [FKH_L4RAW_BIT] = "l4raw",
1528 uint32_t fkh_bits = fk_hdrset->fk_header_select;
1529 uint32_t fkm_bits = fk_hdrset->fk_metadata;
1532 if (!fkm_bits && !fkh_bits)
1534 n = snprintf(buf, buf_len, "metadata(");
1535 if (n > 0 && n < buf_len) {
1539 n = bits_to_str(fkm_bits, fm_fkm_str, FKM_BIT_COUNT, buf, buf_len);
1540 if (n > 0 && n < buf_len) {
1544 n = snprintf(buf, buf_len, ") valid hdr fields(");
1545 if (n > 0 && n < buf_len) {
1549 n = bits_to_str(fkh_bits, fm_fkh_str, FKH_BIT_COUNT, buf, buf_len);
1550 if (n > 0 && n < buf_len) {
1554 snprintf(buf, buf_len, ")");
1558 enic_fm_dump_tcam_match(const struct fm_tcam_match_entry *match,
1563 memset(buf, 0, sizeof(buf));
1564 __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[0],
1566 ENICPMD_LOG(DEBUG, " TCAM %s Outer: %s %scounter",
1567 (ingress) ? "IG" : "EG", buf,
1568 (match->ftm_flags & FMEF_COUNTER) ? "" : "no ");
1569 memset(buf, 0, sizeof(buf));
1570 __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[1],
1573 ENICPMD_LOG(DEBUG, " Inner: %s", buf);
1576 /* Debug function to dump internal NIC flow structures. */
1578 enic_fm_dump_tcam_entry(const struct fm_tcam_match_entry *fm_match,
1579 const struct fm_action *fm_action,
1582 if (!rte_log_can_log(enic_pmd_logtype, RTE_LOG_DEBUG))
1584 enic_fm_dump_tcam_match(fm_match, ingress);
1585 enic_fm_dump_tcam_actions(fm_action);
1589 enic_fm_flow_parse(struct enic_flowman *fm,
1590 const struct rte_flow_attr *attrs,
1591 const struct rte_flow_item pattern[],
1592 const struct rte_flow_action actions[],
1593 struct rte_flow_error *error)
1595 const struct rte_flow_action *action;
1597 static const enum rte_flow_action_type *sa;
1599 ENICPMD_FUNC_TRACE();
1602 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1603 NULL, "no pattern specified");
1608 rte_flow_error_set(error, EINVAL,
1609 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1610 NULL, "no action specified");
1615 if (attrs->priority) {
1616 rte_flow_error_set(error, ENOTSUP,
1617 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1619 "priorities are not supported");
1621 } else if (attrs->transfer) {
1622 rte_flow_error_set(error, ENOTSUP,
1623 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1625 "transfer is not supported");
1627 } else if (attrs->ingress && attrs->egress) {
1628 rte_flow_error_set(error, ENOTSUP,
1629 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1631 "bidirectional rules not supported");
1636 rte_flow_error_set(error, EINVAL,
1637 RTE_FLOW_ERROR_TYPE_ATTR,
1638 NULL, "no attribute specified");
1642 /* Verify Actions. */
1643 sa = (attrs->ingress) ? enic_fm_supported_ig_actions :
1644 enic_fm_supported_eg_actions;
1645 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1647 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1649 else if (!enic_fm_match_action(action, sa))
1652 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1653 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1654 action, "invalid action");
1657 ret = enic_fm_copy_entry(fm, pattern, error);
1660 ret = enic_fm_copy_action(fm, actions, attrs->ingress, error);
1665 enic_fm_counter_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1667 if (!fm_flow->counter_valid)
1669 SLIST_INSERT_HEAD(&fm->counters, fm_flow->counter, next);
1670 fm_flow->counter_valid = false;
1674 enic_fm_more_counters(struct enic_flowman *fm)
1676 struct enic_fm_counter *new_stack;
1677 struct enic_fm_counter *ctrs;
1682 ENICPMD_FUNC_TRACE();
1684 new_stack = rte_realloc(fm->counter_stack, (fm->counters_alloced +
1685 FM_COUNTERS_EXPAND) *
1686 sizeof(struct enic_fm_counter), 0);
1687 if (new_stack == NULL) {
1688 ENICPMD_LOG(ERR, "cannot alloc counter memory");
1691 fm->counter_stack = new_stack;
1693 args[0] = FM_COUNTER_BRK;
1694 args[1] = fm->counters_alloced + FM_COUNTERS_EXPAND;
1695 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1697 ENICPMD_LOG(ERR, "cannot alloc counters rc=%d", rc);
1700 ctrs = (struct enic_fm_counter *)fm->counter_stack +
1701 fm->counters_alloced;
1702 for (i = 0; i < FM_COUNTERS_EXPAND; i++, ctrs++) {
1703 ctrs->handle = fm->counters_alloced + i;
1704 SLIST_INSERT_HEAD(&fm->counters, ctrs, next);
1706 fm->counters_alloced += FM_COUNTERS_EXPAND;
1707 ENICPMD_LOG(DEBUG, "%u counters allocated, total: %u",
1708 FM_COUNTERS_EXPAND, fm->counters_alloced);
1713 enic_fm_counter_zero(struct enic_flowman *fm, struct enic_fm_counter *c)
1719 ENICPMD_FUNC_TRACE();
1721 args[0] = FM_COUNTER_QUERY;
1722 args[1] = c->handle;
1723 args[2] = 1; /* clear */
1724 ret = vnic_dev_flowman_cmd(enic->vdev, args, 3);
1726 ENICPMD_LOG(ERR, "counter init: rc=%d handle=0x%x",
1734 enic_fm_counter_alloc(struct enic_flowman *fm, struct rte_flow_error *error,
1735 struct enic_fm_counter **ctr)
1737 struct enic_fm_counter *c;
1740 ENICPMD_FUNC_TRACE();
1742 if (SLIST_EMPTY(&fm->counters)) {
1743 ret = enic_fm_more_counters(fm);
1745 return rte_flow_error_set(error, -ret,
1746 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1747 NULL, "enic: out of counters");
1749 c = SLIST_FIRST(&fm->counters);
1750 SLIST_REMOVE_HEAD(&fm->counters, next);
1756 enic_fm_action_free(struct enic_flowman *fm, uint64_t handle)
1761 ENICPMD_FUNC_TRACE();
1762 args[0] = FM_ACTION_FREE;
1764 rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1766 ENICPMD_LOG(ERR, "cannot free action: rc=%d handle=0x%" PRIx64,
1772 enic_fm_entry_free(struct enic_flowman *fm, uint64_t handle)
1777 ENICPMD_FUNC_TRACE();
1778 args[0] = FM_MATCH_ENTRY_REMOVE;
1780 rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1782 ENICPMD_LOG(ERR, "cannot free match entry: rc=%d"
1783 " handle=0x%" PRIx64, rc, handle);
1787 static struct enic_fm_jump_flow *
1788 find_jump_flow(struct enic_flowman *fm, uint32_t group)
1790 struct enic_fm_jump_flow *j;
1792 ENICPMD_FUNC_TRACE();
1793 TAILQ_FOREACH(j, &fm->jump_list, list) {
1794 if (j->group == group)
1801 remove_jump_flow(struct enic_flowman *fm, struct rte_flow *flow)
1803 struct enic_fm_jump_flow *j;
1805 ENICPMD_FUNC_TRACE();
1806 TAILQ_FOREACH(j, &fm->jump_list, list) {
1807 if (j->flow == flow) {
1808 TAILQ_REMOVE(&fm->jump_list, j, list);
1816 save_jump_flow(struct enic_flowman *fm,
1817 struct rte_flow *flow,
1819 struct fm_tcam_match_entry *match,
1820 struct fm_action *action)
1822 struct enic_fm_jump_flow *j;
1824 ENICPMD_FUNC_TRACE();
1825 j = calloc(1, sizeof(struct enic_fm_jump_flow));
1831 j->action = *action;
1832 TAILQ_INSERT_HEAD(&fm->jump_list, j, list);
1833 ENICPMD_LOG(DEBUG, "saved jump flow: flow=%p group=%u", flow, group);
1838 __enic_fm_flow_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1840 if (fm_flow->entry_handle != FM_INVALID_HANDLE) {
1841 enic_fm_entry_free(fm, fm_flow->entry_handle);
1842 fm_flow->entry_handle = FM_INVALID_HANDLE;
1844 if (fm_flow->action_handle != FM_INVALID_HANDLE) {
1845 enic_fm_action_free(fm, fm_flow->action_handle);
1846 fm_flow->action_handle = FM_INVALID_HANDLE;
1848 enic_fm_counter_free(fm, fm_flow);
1850 enic_fet_put(fm, fm_flow->fet);
1851 fm_flow->fet = NULL;
1856 enic_fm_flow_free(struct enic_flowman *fm, struct rte_flow *flow)
1858 if (flow->fm->fet && flow->fm->fet->default_key)
1859 remove_jump_flow(fm, flow);
1860 __enic_fm_flow_free(fm, flow->fm);
1866 enic_fm_add_tcam_entry(struct enic_flowman *fm,
1867 struct fm_tcam_match_entry *match_in,
1868 uint64_t *entry_handle,
1870 struct rte_flow_error *error)
1872 struct fm_tcam_match_entry *ftm;
1876 ENICPMD_FUNC_TRACE();
1877 /* Copy entry to the command buffer */
1878 ftm = &fm->cmd.va->fm_tcam_match_entry;
1879 memcpy(ftm, match_in, sizeof(*ftm));
1880 /* Add TCAM entry */
1881 args[0] = FM_TCAM_ENTRY_INSTALL;
1882 args[1] = ingress ? fm->ig_tcam_hndl : fm->eg_tcam_hndl;
1883 args[2] = fm->cmd.pa;
1884 ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 3);
1886 ENICPMD_LOG(ERR, "cannot add %s TCAM entry: rc=%d",
1887 ingress ? "ingress" : "egress", ret);
1888 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1889 NULL, "enic: devcmd(tcam-entry-install)");
1892 ENICPMD_LOG(DEBUG, "installed %s TCAM entry: handle=0x%" PRIx64,
1893 ingress ? "ingress" : "egress", (uint64_t)args[0]);
1894 *entry_handle = args[0];
1899 enic_fm_add_exact_entry(struct enic_flowman *fm,
1900 struct fm_tcam_match_entry *match_in,
1901 uint64_t *entry_handle,
1902 struct enic_fm_fet *fet,
1903 struct rte_flow_error *error)
1905 struct fm_exact_match_entry *fem;
1909 ENICPMD_FUNC_TRACE();
1910 /* The new entry must have the table's key */
1911 if (memcmp(fet->key.fk_hdrset, match_in->ftm_mask.fk_hdrset,
1912 sizeof(struct fm_header_set) * FM_HDRSET_MAX)) {
1913 return rte_flow_error_set(error, EINVAL,
1914 RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1915 "enic: key does not match group's key");
1918 /* Copy entry to the command buffer */
1919 fem = &fm->cmd.va->fm_exact_match_entry;
1921 * Translate TCAM entry to exact entry. As is only need to drop
1922 * position and mask. The mask is part of the exact match table.
1923 * Position (aka priority) is not supported in the exact match table.
1925 fem->fem_data = match_in->ftm_data;
1926 fem->fem_flags = match_in->ftm_flags;
1927 fem->fem_action = match_in->ftm_action;
1928 fem->fem_counter = match_in->ftm_counter;
1930 /* Add exact entry */
1931 args[0] = FM_EXACT_ENTRY_INSTALL;
1932 args[1] = fet->handle;
1933 args[2] = fm->cmd.pa;
1934 ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 3);
1936 ENICPMD_LOG(ERR, "cannot add %s exact entry: group=%u",
1937 fet->ingress ? "ingress" : "egress", fet->group);
1938 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1939 NULL, "enic: devcmd(exact-entry-install)");
1942 ENICPMD_LOG(DEBUG, "installed %s exact entry: group=%u"
1943 " handle=0x%" PRIx64,
1944 fet->ingress ? "ingress" : "egress", fet->group,
1946 *entry_handle = args[0];
1950 /* Push match-action to the NIC. */
1952 __enic_fm_flow_add_entry(struct enic_flowman *fm,
1953 struct enic_fm_flow *fm_flow,
1954 struct fm_tcam_match_entry *match_in,
1955 struct fm_action *action_in,
1958 struct rte_flow_error *error)
1960 struct enic_fm_counter *ctr;
1961 struct fm_action *fma;
1967 ENICPMD_FUNC_TRACE();
1968 /* Allocate action. */
1969 fma = &fm->cmd.va->fm_action;
1970 memcpy(fma, action_in, sizeof(*fma));
1971 args[0] = FM_ACTION_ALLOC;
1972 args[1] = fm->cmd.pa;
1973 ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1975 ENICPMD_LOG(ERR, "allocating TCAM table action rc=%d", ret);
1976 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1977 NULL, "enic: devcmd(action-alloc)");
1981 fm_flow->action_handle = action_h;
1982 match_in->ftm_action = action_h;
1983 ENICPMD_LOG(DEBUG, "action allocated: handle=0x%" PRIx64, action_h);
1985 /* Allocate counter if requested. */
1986 if (match_in->ftm_flags & FMEF_COUNTER) {
1987 ret = enic_fm_counter_alloc(fm, error, &ctr);
1988 if (ret) /* error has been filled in */
1990 fm_flow->counter_valid = true;
1991 fm_flow->counter = ctr;
1992 match_in->ftm_counter = ctr->handle;
1996 * Get the group's table (either TCAM or exact match table) and
1997 * add entry to it. If we use the exact match table, the handler
1998 * will translate the TCAM entry (match_in) to the appropriate
1999 * exact match entry and use that instead.
2001 entry_h = FM_INVALID_HANDLE;
2002 if (group == FM_TCAM_RTE_GROUP) {
2003 ret = enic_fm_add_tcam_entry(fm, match_in, &entry_h, ingress,
2007 /* Jump action might have a ref to fet */
2008 fm_flow->fet = fm->fet;
2011 struct enic_fm_fet *fet = NULL;
2013 ret = enic_fet_get(fm, group, ingress,
2014 &match_in->ftm_mask, &fet, error);
2018 ret = enic_fm_add_exact_entry(fm, match_in, &entry_h, fet,
2023 /* Clear counter after adding entry, as it requires in-use counter */
2024 if (fm_flow->counter_valid) {
2025 ret = enic_fm_counter_zero(fm, fm_flow->counter);
2029 fm_flow->entry_handle = entry_h;
2033 /* Push match-action to the NIC. */
2034 static struct rte_flow *
2035 enic_fm_flow_add_entry(struct enic_flowman *fm,
2036 struct fm_tcam_match_entry *match_in,
2037 struct fm_action *action_in,
2038 const struct rte_flow_attr *attrs,
2039 struct rte_flow_error *error)
2041 struct enic_fm_flow *fm_flow;
2042 struct rte_flow *flow;
2044 ENICPMD_FUNC_TRACE();
2045 enic_fm_dump_tcam_entry(match_in, action_in, attrs->ingress);
2046 flow = calloc(1, sizeof(*flow));
2047 fm_flow = calloc(1, sizeof(*fm_flow));
2048 if (flow == NULL || fm_flow == NULL) {
2049 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2050 NULL, "enic: cannot allocate rte_flow");
2056 fm_flow->action_handle = FM_INVALID_HANDLE;
2057 fm_flow->entry_handle = FM_INVALID_HANDLE;
2058 if (__enic_fm_flow_add_entry(fm, fm_flow, match_in, action_in,
2059 attrs->group, attrs->ingress, error)) {
2060 enic_fm_flow_free(fm, flow);
2067 convert_jump_flows(struct enic_flowman *fm, struct enic_fm_fet *fet,
2068 struct rte_flow_error *error)
2070 struct enic_fm_flow *fm_flow;
2071 struct enic_fm_jump_flow *j;
2072 struct fm_action *fma;
2075 ENICPMD_FUNC_TRACE();
2077 * Find the saved flows that should jump to the new table (fet).
2078 * Then delete the old TCAM entry that jumps to the default table,
2079 * and add a new one that jumps to the new table.
2082 j = find_jump_flow(fm, group);
2084 ENICPMD_LOG(DEBUG, "convert jump flow: flow=%p group=%u",
2086 /* Delete old entry */
2087 fm_flow = j->flow->fm;
2088 __enic_fm_flow_free(fm, fm_flow);
2092 fma->fma_action_ops[0].exact.handle = fet->handle;
2093 if (__enic_fm_flow_add_entry(fm, fm_flow, &j->match, fma,
2094 FM_TCAM_RTE_GROUP, fet->ingress, error)) {
2095 /* Cannot roll back changes at the moment */
2096 ENICPMD_LOG(ERR, "cannot convert jump flow: flow=%p",
2101 ENICPMD_LOG(DEBUG, "convert ok: group=%u ref=%u",
2102 fet->group, fet->ref);
2105 TAILQ_REMOVE(&fm->jump_list, j, list);
2107 j = find_jump_flow(fm, group);
2112 enic_fm_open_scratch(struct enic_flowman *fm)
2114 fm->action_op_count = 0;
2116 memset(&fm->tcam_entry, 0, sizeof(fm->tcam_entry));
2117 memset(&fm->action, 0, sizeof(fm->action));
2121 enic_fm_close_scratch(struct enic_flowman *fm)
2124 enic_fet_put(fm, fm->fet);
2127 fm->action_op_count = 0;
2131 enic_fm_flow_validate(struct rte_eth_dev *dev,
2132 const struct rte_flow_attr *attrs,
2133 const struct rte_flow_item pattern[],
2134 const struct rte_flow_action actions[],
2135 struct rte_flow_error *error)
2137 struct fm_tcam_match_entry *fm_tcam_entry;
2138 struct fm_action *fm_action;
2139 struct enic_flowman *fm;
2142 ENICPMD_FUNC_TRACE();
2143 fm = pmd_priv(dev)->fm;
2146 enic_fm_open_scratch(fm);
2147 ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2149 fm_tcam_entry = &fm->tcam_entry;
2150 fm_action = &fm->action;
2151 enic_fm_dump_tcam_entry(fm_tcam_entry, fm_action,
2154 enic_fm_close_scratch(fm);
2159 enic_fm_flow_query_count(struct rte_eth_dev *dev,
2160 struct rte_flow *flow, void *data,
2161 struct rte_flow_error *error)
2163 struct rte_flow_query_count *query;
2164 struct enic_fm_flow *fm_flow;
2169 ENICPMD_FUNC_TRACE();
2170 enic = pmd_priv(dev);
2173 if (!fm_flow->counter_valid)
2174 return rte_flow_error_set(error, ENOTSUP,
2175 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2176 "enic: flow does not have counter");
2178 args[0] = FM_COUNTER_QUERY;
2179 args[1] = fm_flow->counter->handle;
2180 args[2] = query->reset;
2181 rc = vnic_dev_flowman_cmd(enic->vdev, args, 3);
2183 ENICPMD_LOG(ERR, "cannot query counter: rc=%d handle=0x%x",
2184 rc, fm_flow->counter->handle);
2187 query->hits_set = 1;
2188 query->hits = args[0];
2189 query->bytes_set = 1;
2190 query->bytes = args[1];
2195 enic_fm_flow_query(struct rte_eth_dev *dev,
2196 struct rte_flow *flow,
2197 const struct rte_flow_action *actions,
2199 struct rte_flow_error *error)
2203 ENICPMD_FUNC_TRACE();
2204 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2205 switch (actions->type) {
2206 case RTE_FLOW_ACTION_TYPE_VOID:
2208 case RTE_FLOW_ACTION_TYPE_COUNT:
2209 ret = enic_fm_flow_query_count(dev, flow, data, error);
2212 return rte_flow_error_set(error, ENOTSUP,
2213 RTE_FLOW_ERROR_TYPE_ACTION,
2215 "action not supported");
2223 static struct rte_flow *
2224 enic_fm_flow_create(struct rte_eth_dev *dev,
2225 const struct rte_flow_attr *attrs,
2226 const struct rte_flow_item pattern[],
2227 const struct rte_flow_action actions[],
2228 struct rte_flow_error *error)
2230 struct fm_tcam_match_entry *fm_tcam_entry;
2231 struct fm_action *fm_action;
2232 struct enic_flowman *fm;
2233 struct enic_fm_fet *fet;
2234 struct rte_flow *flow;
2238 ENICPMD_FUNC_TRACE();
2239 enic = pmd_priv(dev);
2242 rte_flow_error_set(error, ENOTSUP,
2243 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2244 "flowman is not initialized");
2247 enic_fm_open_scratch(fm);
2249 ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2251 goto error_with_scratch;
2252 fm_tcam_entry = &fm->tcam_entry;
2253 fm_action = &fm->action;
2254 flow = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2257 LIST_INSERT_HEAD(&enic->flows, flow, next);
2258 fet = flow->fm->fet;
2259 if (fet && fet->default_key) {
2261 * Jump to non-existent group? Save the relevant info
2262 * so we can convert this flow when that group
2265 save_jump_flow(fm, flow, fet->group,
2266 fm_tcam_entry, fm_action);
2267 } else if (fet && fet->ref == 1) {
2269 * A new table is created. Convert the saved flows
2270 * that should jump to this group.
2272 convert_jump_flows(fm, fet, error);
2277 enic_fm_close_scratch(fm);
2282 enic_fm_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
2283 __rte_unused struct rte_flow_error *error)
2285 struct enic *enic = pmd_priv(dev);
2287 ENICPMD_FUNC_TRACE();
2288 if (enic->fm == NULL)
2290 LIST_REMOVE(flow, next);
2291 enic_fm_flow_free(enic->fm, flow);
2296 enic_fm_flow_flush(struct rte_eth_dev *dev,
2297 __rte_unused struct rte_flow_error *error)
2299 struct enic_fm_flow *fm_flow;
2300 struct enic_flowman *fm;
2301 struct rte_flow *flow;
2302 struct enic *enic = pmd_priv(dev);
2304 ENICPMD_FUNC_TRACE();
2305 if (enic->fm == NULL)
2308 while (!LIST_EMPTY(&enic->flows)) {
2309 flow = LIST_FIRST(&enic->flows);
2311 LIST_REMOVE(flow, next);
2313 * If tables are null, then vNIC is closing, and the firmware
2314 * has already cleaned up flowman state. So do not try to free
2315 * resources, as it only causes errors.
2317 if (fm->ig_tcam_hndl == FM_INVALID_HANDLE) {
2318 fm_flow->entry_handle = FM_INVALID_HANDLE;
2319 fm_flow->action_handle = FM_INVALID_HANDLE;
2320 fm_flow->fet = NULL;
2322 enic_fm_flow_free(fm, flow);
2328 enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle)
2333 args[0] = FM_MATCH_TABLE_FREE;
2335 rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
2337 ENICPMD_LOG(ERR, "cannot free table: rc=%d handle=0x%" PRIx64,
2343 enic_fm_tcam_tbl_alloc(struct enic_flowman *fm, uint32_t direction,
2344 uint32_t max_entries, uint64_t *handle)
2346 struct fm_tcam_match_table *tcam_tbl;
2351 ENICPMD_FUNC_TRACE();
2353 tcam_tbl = &fm->cmd.va->fm_tcam_match_table;
2354 tcam_tbl->ftt_direction = direction;
2355 tcam_tbl->ftt_stage = FM_STAGE_LAST;
2356 tcam_tbl->ftt_max_entries = max_entries;
2357 args[0] = FM_TCAM_TABLE_ALLOC;
2358 args[1] = fm->cmd.pa;
2359 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
2361 ENICPMD_LOG(ERR, "cannot alloc %s TCAM table: rc=%d",
2362 (direction == FM_INGRESS) ? "IG" : "EG", rc);
2366 ENICPMD_LOG(DEBUG, "%s TCAM table allocated, handle=0x%" PRIx64,
2367 (direction == FM_INGRESS) ? "IG" : "EG", *handle);
2372 enic_fm_init_counters(struct enic_flowman *fm)
2374 ENICPMD_FUNC_TRACE();
2375 SLIST_INIT(&fm->counters);
2376 return enic_fm_more_counters(fm);
2380 enic_fm_free_all_counters(struct enic_flowman *fm)
2387 args[0] = FM_COUNTER_BRK;
2389 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
2391 ENICPMD_LOG(ERR, "cannot free counters: rc=%d", rc);
2392 rte_free(fm->counter_stack);
2396 enic_fm_alloc_tcam_tables(struct enic_flowman *fm)
2400 ENICPMD_FUNC_TRACE();
2401 rc = enic_fm_tcam_tbl_alloc(fm, FM_INGRESS, FM_MAX_TCAM_TABLE_SIZE,
2405 rc = enic_fm_tcam_tbl_alloc(fm, FM_EGRESS, FM_MAX_TCAM_TABLE_SIZE,
2411 enic_fm_free_tcam_tables(struct enic_flowman *fm)
2413 ENICPMD_FUNC_TRACE();
2414 if (fm->ig_tcam_hndl) {
2415 ENICPMD_LOG(DEBUG, "free IG TCAM table handle=0x%" PRIx64,
2417 enic_fm_tbl_free(fm, fm->ig_tcam_hndl);
2418 fm->ig_tcam_hndl = FM_INVALID_HANDLE;
2420 if (fm->eg_tcam_hndl) {
2421 ENICPMD_LOG(DEBUG, "free EG TCAM table handle=0x%" PRIx64,
2423 enic_fm_tbl_free(fm, fm->eg_tcam_hndl);
2424 fm->eg_tcam_hndl = FM_INVALID_HANDLE;
2429 enic_fm_init(struct enic *enic)
2431 struct enic_flowman *fm;
2432 uint8_t name[RTE_MEMZONE_NAMESIZE];
2435 if (enic->flow_filter_mode != FILTER_FLOWMAN)
2437 ENICPMD_FUNC_TRACE();
2438 fm = calloc(1, sizeof(*fm));
2440 ENICPMD_LOG(ERR, "cannot alloc flowman struct");
2444 TAILQ_INIT(&fm->fet_list);
2445 TAILQ_INIT(&fm->jump_list);
2446 /* Allocate host memory for flowman commands */
2447 snprintf((char *)name, sizeof(name), "fm-cmd-%s", enic->bdf_name);
2448 fm->cmd.va = enic_alloc_consistent(enic,
2449 sizeof(union enic_flowman_cmd_mem), &fm->cmd.pa, name);
2451 ENICPMD_LOG(ERR, "cannot allocate flowman command memory");
2455 /* Allocate TCAM tables upfront as they are the main tables */
2456 rc = enic_fm_alloc_tcam_tables(fm);
2458 ENICPMD_LOG(ERR, "cannot alloc TCAM tables");
2461 /* Then a number of counters */
2462 rc = enic_fm_init_counters(fm);
2464 ENICPMD_LOG(ERR, "cannot alloc counters");
2468 * One default exact match table for each direction. We hold onto
2471 rc = enic_fet_alloc(fm, 1, NULL, 128, &fm->default_ig_fet);
2473 ENICPMD_LOG(ERR, "cannot alloc default IG exact match table");
2474 goto error_counters;
2476 fm->default_ig_fet->ref = 1;
2477 rc = enic_fet_alloc(fm, 0, NULL, 128, &fm->default_eg_fet);
2479 ENICPMD_LOG(ERR, "cannot alloc default EG exact match table");
2482 fm->default_eg_fet->ref = 1;
2487 enic_fet_free(fm, fm->default_ig_fet);
2489 enic_fm_free_all_counters(fm);
2491 enic_fm_free_tcam_tables(fm);
2493 enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2494 fm->cmd.va, fm->cmd.pa);
2501 enic_fm_destroy(struct enic *enic)
2503 struct enic_flowman *fm;
2504 struct enic_fm_fet *fet;
2506 if (enic->fm == NULL)
2508 ENICPMD_FUNC_TRACE();
2510 enic_fet_free(fm, fm->default_eg_fet);
2511 enic_fet_free(fm, fm->default_ig_fet);
2512 /* Free all exact match tables still open */
2513 while (!TAILQ_EMPTY(&fm->fet_list)) {
2514 fet = TAILQ_FIRST(&fm->fet_list);
2515 enic_fet_free(fm, fet);
2517 enic_fm_free_tcam_tables(fm);
2518 enic_fm_free_all_counters(fm);
2519 enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2520 fm->cmd.va, fm->cmd.pa);
2527 enic_fm_allocate_switch_domain(struct enic *pf)
2529 const struct rte_pci_addr *cur_a, *prev_a;
2530 struct rte_eth_dev *dev;
2531 struct enic *cur, *prev;
2537 ENICPMD_FUNC_TRACE();
2538 if (enic_is_vf_rep(pf))
2541 cur_a = &RTE_ETH_DEV_TO_PCI(cur->rte_dev)->addr;
2542 /* Go through ports and find another PF that is on the same adapter */
2543 RTE_ETH_FOREACH_DEV(pid) {
2544 dev = &rte_eth_devices[pid];
2545 if (!dev_is_enic(dev))
2547 if (dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)
2549 if (dev == cur->rte_dev)
2551 /* dev is another PF. Is it on the same adapter? */
2552 prev = pmd_priv(dev);
2553 prev_a = &RTE_ETH_DEV_TO_PCI(dev)->addr;
2554 if (!enic_fm_find_vnic(cur, prev_a, &vnic_h)) {
2555 ENICPMD_LOG(DEBUG, "Port %u (PF BDF %x:%x:%x) and port %u (PF BDF %x:%x:%x domain %u) are on the same VIC",
2556 cur->rte_dev->data->port_id,
2557 cur_a->bus, cur_a->devid, cur_a->function,
2559 prev_a->bus, prev_a->devid, prev_a->function,
2560 prev->switch_domain_id);
2561 cur->switch_domain_id = prev->switch_domain_id;
2565 ret = rte_eth_switch_domain_alloc(&domain_id);
2567 ENICPMD_LOG(WARNING, "failed to allocate switch domain for device %d",
2570 cur->switch_domain_id = domain_id;
2571 ENICPMD_LOG(DEBUG, "Port %u (PF BDF %x:%x:%x) is the 1st PF on the VIC. Allocated switch domain id %u",
2572 cur->rte_dev->data->port_id,
2573 cur_a->bus, cur_a->devid, cur_a->function,
2578 const struct rte_flow_ops enic_fm_flow_ops = {
2579 .validate = enic_fm_flow_validate,
2580 .create = enic_fm_flow_create,
2581 .destroy = enic_fm_flow_destroy,
2582 .flush = enic_fm_flow_flush,
2583 .query = enic_fm_flow_query,