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
37 /* Low priority used for implicit VF -> representor flow */
38 #define FM_LOWEST_PRIORITY 100000
40 /* High priority used for implicit representor -> VF flow */
41 #define FM_HIGHEST_PRIORITY 0
43 /* Tag used for implicit VF <-> representor flows */
44 #define FM_VF_REP_TAG 1
47 * Flow exact match tables (FET) in the VIC and rte_flow groups.
48 * Use a simple scheme to map groups to tables.
49 * Group 0 uses the single TCAM tables, one for each direction.
50 * Group 1, 2, ... uses its own exact match table.
52 * The TCAM tables are allocated upfront during init.
54 * Exact match tables are allocated on demand. 3 paths that lead allocations.
56 * 1. Add a flow that jumps from group 0 to group N.
58 * If N does not exist, we allocate an exact match table for it, using
59 * a dummy key. A key is required for the table.
61 * 2. Add a flow that uses group N.
63 * If N does not exist, we allocate an exact match table for it, using
64 * the flow's key. Subsequent flows to the same group all should have
67 * Without a jump flow to N, N is not reachable in hardware. No packets
70 * 3. Add a flow to an empty group N.
72 * N has been created via (1) and the dummy key. We free that table, allocate
73 * a new table using the new flow's key. Also re-do the existing jump flow to
74 * point to the new table.
76 #define FM_TCAM_RTE_GROUP 0
79 TAILQ_ENTRY(enic_fm_fet) list;
80 uint32_t group; /* rte_flow group ID */
81 uint64_t handle; /* Exact match table handle from flowman */
84 int ref; /* Reference count via get/put */
85 struct fm_key_template key; /* Key associated with the table */
88 struct enic_fm_counter {
89 SLIST_ENTRY(enic_fm_counter) next;
96 uint64_t entry_handle;
97 uint64_t action_handle;
98 struct enic_fm_counter *counter;
99 struct enic_fm_fet *fet;
100 /* Auto-added steer action for hairpin flows (e.g. vnic->vnic) */
101 struct enic_fm_flow *hairpin_steer_flow;
104 struct enic_fm_jump_flow {
105 TAILQ_ENTRY(enic_fm_jump_flow) list;
106 struct rte_flow *flow;
108 struct fm_tcam_match_entry match;
109 struct fm_action action;
113 * Flowman uses host memory for commands. This structure is allocated
114 * in DMA-able memory.
116 union enic_flowman_cmd_mem {
117 struct fm_tcam_match_table fm_tcam_match_table;
118 struct fm_exact_match_table fm_exact_match_table;
119 struct fm_tcam_match_entry fm_tcam_match_entry;
120 struct fm_exact_match_entry fm_exact_match_entry;
121 struct fm_action fm_action;
125 * PF has a flowman instance, and VF representors share it with PF.
126 * PF allocates this structure and owns it. VF representors borrow
127 * the PF's structure during API calls (e.g. create, query).
129 struct enic_flowman {
130 struct enic *owner_enic; /* PF */
131 struct enic *user_enic; /* API caller (PF or representor) */
133 * Representors and PF share the same underlying flowman.
134 * Lock API calls to serialize accesses from them. Only used
135 * when VF representors are present.
140 union enic_flowman_cmd_mem *va;
143 /* TCAM tables allocated upfront, used for group 0 */
144 uint64_t ig_tcam_hndl;
145 uint64_t eg_tcam_hndl;
147 SLIST_HEAD(enic_free_counters, enic_fm_counter) counters;
149 uint32_t counters_alloced;
150 /* Exact match tables for groups != 0, dynamically allocated */
151 TAILQ_HEAD(fet_list, enic_fm_fet) fet_list;
153 * Default exact match tables used for jump actions to
154 * non-existent groups.
156 struct enic_fm_fet *default_eg_fet;
157 struct enic_fm_fet *default_ig_fet;
158 /* Flows that jump to the default table above */
159 TAILQ_HEAD(jump_flow_list, enic_fm_jump_flow) jump_list;
161 * Scratch data used during each invocation of flow_create
164 struct enic_fm_fet *fet;
165 struct fm_tcam_match_entry tcam_entry;
166 struct fm_action action;
167 struct fm_action action_tmp; /* enic_fm_reorder_action_op */
169 /* Tags used for representor flows */
171 /* For auto-added steer action for hairpin */
172 int need_hairpin_steer;
173 uint64_t hairpin_steer_vnic_h;
176 static int enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle);
178 * API functions (create, destroy, validate, flush) call begin_fm()
179 * upon entering to save the caller enic (PF or VF representor) and
180 * lock. Upon exit, they call end_fm() to unlock.
182 static struct enic_flowman *begin_fm(struct enic *enic);
183 static void end_fm(struct enic_flowman *fm);
184 /* Delete internal flows created for representor paths */
185 static void delete_rep_flows(struct enic *enic);
188 * Common arguments passed to copy_item functions. Use this structure
189 * so we can easily add new arguments.
190 * item: Item specification.
191 * fm_tcam_entry: Flowman TCAM match entry.
192 * header_level: 0 for outer header, 1 for inner header.
194 struct copy_item_args {
195 const struct rte_flow_item *item;
196 struct fm_tcam_match_entry *fm_tcam_entry;
197 uint8_t header_level;
200 /* functions for copying items into flowman match */
201 typedef int (enic_copy_item_fn)(struct copy_item_args *arg);
203 /* Info about how to copy items into flowman match */
204 struct enic_fm_items {
205 /* Function for copying and validating an item. */
206 enic_copy_item_fn * const copy_item;
207 /* List of valid previous items. */
208 const enum rte_flow_item_type * const prev_items;
210 * True if it's OK for this item to be the first item. For some NIC
211 * versions, it's invalid to start the stack above layer 3.
213 const uint8_t valid_start_item;
216 static enic_copy_item_fn enic_fm_copy_item_eth;
217 static enic_copy_item_fn enic_fm_copy_item_ipv4;
218 static enic_copy_item_fn enic_fm_copy_item_ipv6;
219 static enic_copy_item_fn enic_fm_copy_item_raw;
220 static enic_copy_item_fn enic_fm_copy_item_sctp;
221 static enic_copy_item_fn enic_fm_copy_item_tcp;
222 static enic_copy_item_fn enic_fm_copy_item_udp;
223 static enic_copy_item_fn enic_fm_copy_item_vlan;
224 static enic_copy_item_fn enic_fm_copy_item_vxlan;
226 /* Ingress actions */
227 static const enum rte_flow_action_type enic_fm_supported_ig_actions[] = {
228 RTE_FLOW_ACTION_TYPE_COUNT,
229 RTE_FLOW_ACTION_TYPE_DROP,
230 RTE_FLOW_ACTION_TYPE_FLAG,
231 RTE_FLOW_ACTION_TYPE_JUMP,
232 RTE_FLOW_ACTION_TYPE_MARK,
233 RTE_FLOW_ACTION_TYPE_OF_POP_VLAN,
234 RTE_FLOW_ACTION_TYPE_PORT_ID,
235 RTE_FLOW_ACTION_TYPE_PASSTHRU,
236 RTE_FLOW_ACTION_TYPE_QUEUE,
237 RTE_FLOW_ACTION_TYPE_RSS,
238 RTE_FLOW_ACTION_TYPE_VOID,
239 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
240 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP,
241 RTE_FLOW_ACTION_TYPE_END, /* END must be the last entry */
245 static const enum rte_flow_action_type enic_fm_supported_eg_actions[] = {
246 RTE_FLOW_ACTION_TYPE_COUNT,
247 RTE_FLOW_ACTION_TYPE_DROP,
248 RTE_FLOW_ACTION_TYPE_JUMP,
249 RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN,
250 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP,
251 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID,
252 RTE_FLOW_ACTION_TYPE_PORT_ID,
253 RTE_FLOW_ACTION_TYPE_PASSTHRU,
254 RTE_FLOW_ACTION_TYPE_VOID,
255 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
256 RTE_FLOW_ACTION_TYPE_END,
259 static const struct enic_fm_items enic_fm_items[] = {
260 [RTE_FLOW_ITEM_TYPE_RAW] = {
261 .copy_item = enic_fm_copy_item_raw,
262 .valid_start_item = 0,
263 .prev_items = (const enum rte_flow_item_type[]) {
264 RTE_FLOW_ITEM_TYPE_UDP,
265 RTE_FLOW_ITEM_TYPE_END,
268 [RTE_FLOW_ITEM_TYPE_ETH] = {
269 .copy_item = enic_fm_copy_item_eth,
270 .valid_start_item = 1,
271 .prev_items = (const enum rte_flow_item_type[]) {
272 RTE_FLOW_ITEM_TYPE_END,
275 [RTE_FLOW_ITEM_TYPE_VLAN] = {
276 .copy_item = enic_fm_copy_item_vlan,
277 .valid_start_item = 1,
278 .prev_items = (const enum rte_flow_item_type[]) {
279 RTE_FLOW_ITEM_TYPE_ETH,
280 RTE_FLOW_ITEM_TYPE_END,
283 [RTE_FLOW_ITEM_TYPE_IPV4] = {
284 .copy_item = enic_fm_copy_item_ipv4,
285 .valid_start_item = 1,
286 .prev_items = (const enum rte_flow_item_type[]) {
287 RTE_FLOW_ITEM_TYPE_ETH,
288 RTE_FLOW_ITEM_TYPE_VLAN,
289 RTE_FLOW_ITEM_TYPE_END,
292 [RTE_FLOW_ITEM_TYPE_IPV6] = {
293 .copy_item = enic_fm_copy_item_ipv6,
294 .valid_start_item = 1,
295 .prev_items = (const enum rte_flow_item_type[]) {
296 RTE_FLOW_ITEM_TYPE_ETH,
297 RTE_FLOW_ITEM_TYPE_VLAN,
298 RTE_FLOW_ITEM_TYPE_END,
301 [RTE_FLOW_ITEM_TYPE_UDP] = {
302 .copy_item = enic_fm_copy_item_udp,
303 .valid_start_item = 1,
304 .prev_items = (const enum rte_flow_item_type[]) {
305 RTE_FLOW_ITEM_TYPE_IPV4,
306 RTE_FLOW_ITEM_TYPE_IPV6,
307 RTE_FLOW_ITEM_TYPE_END,
310 [RTE_FLOW_ITEM_TYPE_TCP] = {
311 .copy_item = enic_fm_copy_item_tcp,
312 .valid_start_item = 1,
313 .prev_items = (const enum rte_flow_item_type[]) {
314 RTE_FLOW_ITEM_TYPE_IPV4,
315 RTE_FLOW_ITEM_TYPE_IPV6,
316 RTE_FLOW_ITEM_TYPE_END,
319 [RTE_FLOW_ITEM_TYPE_SCTP] = {
320 .copy_item = enic_fm_copy_item_sctp,
321 .valid_start_item = 0,
322 .prev_items = (const enum rte_flow_item_type[]) {
323 RTE_FLOW_ITEM_TYPE_IPV4,
324 RTE_FLOW_ITEM_TYPE_IPV6,
325 RTE_FLOW_ITEM_TYPE_END,
328 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
329 .copy_item = enic_fm_copy_item_vxlan,
330 .valid_start_item = 1,
331 .prev_items = (const enum rte_flow_item_type[]) {
332 RTE_FLOW_ITEM_TYPE_UDP,
333 RTE_FLOW_ITEM_TYPE_END,
339 enic_fm_copy_item_eth(struct copy_item_args *arg)
341 const struct rte_flow_item *item = arg->item;
342 const struct rte_flow_item_eth *spec = item->spec;
343 const struct rte_flow_item_eth *mask = item->mask;
344 const uint8_t lvl = arg->header_level;
345 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
346 struct fm_header_set *fm_data, *fm_mask;
348 ENICPMD_FUNC_TRACE();
349 /* Match all if no spec */
353 mask = &rte_flow_item_eth_mask;
354 fm_data = &entry->ftm_data.fk_hdrset[lvl];
355 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
356 fm_data->fk_header_select |= FKH_ETHER;
357 fm_mask->fk_header_select |= FKH_ETHER;
358 memcpy(&fm_data->l2.eth, spec, sizeof(*spec));
359 memcpy(&fm_mask->l2.eth, mask, sizeof(*mask));
364 enic_fm_copy_item_vlan(struct copy_item_args *arg)
366 const struct rte_flow_item *item = arg->item;
367 const struct rte_flow_item_vlan *spec = item->spec;
368 const struct rte_flow_item_vlan *mask = item->mask;
369 const uint8_t lvl = arg->header_level;
370 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
371 struct fm_header_set *fm_data, *fm_mask;
372 struct rte_ether_hdr *eth_mask;
373 struct rte_ether_hdr *eth_val;
376 ENICPMD_FUNC_TRACE();
377 fm_data = &entry->ftm_data.fk_hdrset[lvl];
378 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
379 /* Outer and inner packet vlans need different flags */
380 meta = FKM_VLAN_PRES;
383 fm_data->fk_metadata |= meta;
384 fm_mask->fk_metadata |= meta;
386 /* Match all if no spec */
390 mask = &rte_flow_item_vlan_mask;
392 eth_mask = (void *)&fm_mask->l2.eth;
393 eth_val = (void *)&fm_data->l2.eth;
396 * Outer TPID cannot be matched. If inner_type is 0, use what is
399 if (eth_mask->ether_type && mask->inner_type)
403 * When packet matching, the VIC always compares vlan-stripped
404 * L2, regardless of vlan stripping settings. So, the inner type
405 * from vlan becomes the ether type of the eth header.
407 if (mask->inner_type) {
408 eth_mask->ether_type = mask->inner_type;
409 eth_val->ether_type = spec->inner_type;
411 fm_data->fk_header_select |= FKH_ETHER | FKH_QTAG;
412 fm_mask->fk_header_select |= FKH_ETHER | FKH_QTAG;
413 fm_data->fk_vlan = rte_be_to_cpu_16(spec->tci);
414 fm_mask->fk_vlan = rte_be_to_cpu_16(mask->tci);
419 enic_fm_copy_item_ipv4(struct copy_item_args *arg)
421 const struct rte_flow_item *item = arg->item;
422 const struct rte_flow_item_ipv4 *spec = item->spec;
423 const struct rte_flow_item_ipv4 *mask = item->mask;
424 const uint8_t lvl = arg->header_level;
425 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
426 struct fm_header_set *fm_data, *fm_mask;
428 ENICPMD_FUNC_TRACE();
429 fm_data = &entry->ftm_data.fk_hdrset[lvl];
430 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
431 fm_data->fk_metadata |= FKM_IPV4;
432 fm_mask->fk_metadata |= FKM_IPV4;
437 mask = &rte_flow_item_ipv4_mask;
439 fm_data->fk_header_select |= FKH_IPV4;
440 fm_mask->fk_header_select |= FKH_IPV4;
441 memcpy(&fm_data->l3.ip4, spec, sizeof(*spec));
442 memcpy(&fm_mask->l3.ip4, mask, sizeof(*mask));
447 enic_fm_copy_item_ipv6(struct copy_item_args *arg)
449 const struct rte_flow_item *item = arg->item;
450 const struct rte_flow_item_ipv6 *spec = item->spec;
451 const struct rte_flow_item_ipv6 *mask = item->mask;
452 const uint8_t lvl = arg->header_level;
453 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
454 struct fm_header_set *fm_data, *fm_mask;
456 ENICPMD_FUNC_TRACE();
457 fm_data = &entry->ftm_data.fk_hdrset[lvl];
458 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
459 fm_data->fk_metadata |= FKM_IPV6;
460 fm_mask->fk_metadata |= FKM_IPV6;
465 mask = &rte_flow_item_ipv6_mask;
467 fm_data->fk_header_select |= FKH_IPV6;
468 fm_mask->fk_header_select |= FKH_IPV6;
469 memcpy(&fm_data->l3.ip6, spec, sizeof(*spec));
470 memcpy(&fm_mask->l3.ip6, mask, sizeof(*mask));
475 enic_fm_copy_item_udp(struct copy_item_args *arg)
477 const struct rte_flow_item *item = arg->item;
478 const struct rte_flow_item_udp *spec = item->spec;
479 const struct rte_flow_item_udp *mask = item->mask;
480 const uint8_t lvl = arg->header_level;
481 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
482 struct fm_header_set *fm_data, *fm_mask;
484 ENICPMD_FUNC_TRACE();
485 fm_data = &entry->ftm_data.fk_hdrset[lvl];
486 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
487 fm_data->fk_metadata |= FKM_UDP;
488 fm_mask->fk_metadata |= FKM_UDP;
493 mask = &rte_flow_item_udp_mask;
495 fm_data->fk_header_select |= FKH_UDP;
496 fm_mask->fk_header_select |= FKH_UDP;
497 memcpy(&fm_data->l4.udp, spec, sizeof(*spec));
498 memcpy(&fm_mask->l4.udp, mask, sizeof(*mask));
503 enic_fm_copy_item_tcp(struct copy_item_args *arg)
505 const struct rte_flow_item *item = arg->item;
506 const struct rte_flow_item_tcp *spec = item->spec;
507 const struct rte_flow_item_tcp *mask = item->mask;
508 const uint8_t lvl = arg->header_level;
509 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
510 struct fm_header_set *fm_data, *fm_mask;
512 ENICPMD_FUNC_TRACE();
513 fm_data = &entry->ftm_data.fk_hdrset[lvl];
514 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
515 fm_data->fk_metadata |= FKM_TCP;
516 fm_mask->fk_metadata |= FKM_TCP;
521 mask = &rte_flow_item_tcp_mask;
523 fm_data->fk_header_select |= FKH_TCP;
524 fm_mask->fk_header_select |= FKH_TCP;
525 memcpy(&fm_data->l4.tcp, spec, sizeof(*spec));
526 memcpy(&fm_mask->l4.tcp, mask, sizeof(*mask));
531 enic_fm_copy_item_sctp(struct copy_item_args *arg)
533 const struct rte_flow_item *item = arg->item;
534 const struct rte_flow_item_sctp *spec = item->spec;
535 const struct rte_flow_item_sctp *mask = item->mask;
536 const uint8_t lvl = arg->header_level;
537 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
538 struct fm_header_set *fm_data, *fm_mask;
539 uint8_t *ip_proto_mask = NULL;
540 uint8_t *ip_proto = NULL;
543 ENICPMD_FUNC_TRACE();
544 fm_data = &entry->ftm_data.fk_hdrset[lvl];
545 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
547 * The NIC filter API has no flags for "match sctp", so explicitly
548 * set the protocol number in the IP pattern.
550 if (fm_data->fk_metadata & FKM_IPV4) {
551 struct rte_ipv4_hdr *ip;
552 ip = (struct rte_ipv4_hdr *)&fm_mask->l3.ip4;
553 ip_proto_mask = &ip->next_proto_id;
554 ip = (struct rte_ipv4_hdr *)&fm_data->l3.ip4;
555 ip_proto = &ip->next_proto_id;
557 } else if (fm_data->fk_metadata & FKM_IPV6) {
558 struct rte_ipv6_hdr *ip;
559 ip = (struct rte_ipv6_hdr *)&fm_mask->l3.ip6;
560 ip_proto_mask = &ip->proto;
561 ip = (struct rte_ipv6_hdr *)&fm_data->l3.ip6;
562 ip_proto = &ip->proto;
565 /* Need IPv4/IPv6 pattern first */
568 *ip_proto = IPPROTO_SCTP;
569 *ip_proto_mask = 0xff;
570 fm_data->fk_header_select |= l3_fkh;
571 fm_mask->fk_header_select |= l3_fkh;
576 mask = &rte_flow_item_sctp_mask;
578 fm_data->fk_header_select |= FKH_L4RAW;
579 fm_mask->fk_header_select |= FKH_L4RAW;
580 memcpy(fm_data->l4.rawdata, spec, sizeof(*spec));
581 memcpy(fm_mask->l4.rawdata, mask, sizeof(*mask));
586 enic_fm_copy_item_vxlan(struct copy_item_args *arg)
588 const struct rte_flow_item *item = arg->item;
589 const struct rte_flow_item_vxlan *spec = item->spec;
590 const struct rte_flow_item_vxlan *mask = item->mask;
591 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
592 struct fm_header_set *fm_data, *fm_mask;
594 ENICPMD_FUNC_TRACE();
595 /* Only 2 header levels (outer and inner) allowed */
596 if (arg->header_level > 0)
599 fm_data = &entry->ftm_data.fk_hdrset[0];
600 fm_mask = &entry->ftm_mask.fk_hdrset[0];
601 fm_data->fk_metadata |= FKM_VXLAN;
602 fm_mask->fk_metadata |= FKM_VXLAN;
603 /* items from here on out are inner header items */
604 arg->header_level = 1;
606 /* Match all if no spec */
610 mask = &rte_flow_item_vxlan_mask;
612 fm_data->fk_header_select |= FKH_VXLAN;
613 fm_mask->fk_header_select |= FKH_VXLAN;
614 memcpy(&fm_data->vxlan, spec, sizeof(*spec));
615 memcpy(&fm_mask->vxlan, mask, sizeof(*mask));
620 * Currently, raw pattern match is very limited. It is intended for matching
621 * UDP tunnel header (e.g. vxlan or geneve).
624 enic_fm_copy_item_raw(struct copy_item_args *arg)
626 const struct rte_flow_item *item = arg->item;
627 const struct rte_flow_item_raw *spec = item->spec;
628 const struct rte_flow_item_raw *mask = item->mask;
629 const uint8_t lvl = arg->header_level;
630 struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
631 struct fm_header_set *fm_data, *fm_mask;
633 ENICPMD_FUNC_TRACE();
634 /* Cannot be used for inner packet */
637 /* Need both spec and mask */
640 /* Only supports relative with offset 0 */
641 if (!spec->relative || spec->offset != 0 || spec->search ||
644 /* Need non-null pattern that fits within the NIC's filter pattern */
645 if (spec->length == 0 ||
646 spec->length + sizeof(struct rte_udp_hdr) > FM_LAYER_SIZE ||
647 !spec->pattern || !mask->pattern)
650 * Mask fields, including length, are often set to zero. Assume that
651 * means "same as spec" to avoid breaking existing apps. If length
652 * is not zero, then it should be >= spec length.
654 * No more pattern follows this, so append to the L4 layer instead of
655 * L5 to work with both recent and older VICs.
657 if (mask->length != 0 && mask->length < spec->length)
660 fm_data = &entry->ftm_data.fk_hdrset[lvl];
661 fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
662 fm_data->fk_header_select |= FKH_L4RAW;
663 fm_mask->fk_header_select |= FKH_L4RAW;
664 fm_data->fk_header_select &= ~FKH_UDP;
665 fm_mask->fk_header_select &= ~FKH_UDP;
666 memcpy(fm_data->l4.rawdata + sizeof(struct rte_udp_hdr),
667 spec->pattern, spec->length);
668 memcpy(fm_mask->l4.rawdata + sizeof(struct rte_udp_hdr),
669 mask->pattern, spec->length);
674 flowman_cmd(struct enic_flowman *fm, uint64_t *args, int nargs)
676 return vnic_dev_flowman_cmd(fm->owner_enic->vdev, args, nargs);
680 enic_fet_alloc(struct enic_flowman *fm, uint8_t ingress,
681 struct fm_key_template *key, int entries,
682 struct enic_fm_fet **fet_out)
684 struct fm_exact_match_table *cmd;
685 struct fm_header_set *hdr;
686 struct enic_fm_fet *fet;
690 ENICPMD_FUNC_TRACE();
691 fet = calloc(1, sizeof(struct enic_fm_fet));
694 cmd = &fm->cmd.va->fm_exact_match_table;
695 memset(cmd, 0, sizeof(*cmd));
696 cmd->fet_direction = ingress ? FM_INGRESS : FM_EGRESS;
697 cmd->fet_stage = FM_STAGE_LAST;
698 cmd->fet_max_entries = entries ? entries : FM_MAX_EXACT_TABLE_SIZE;
700 hdr = &cmd->fet_key.fk_hdrset[0];
701 memset(hdr, 0, sizeof(*hdr));
702 hdr->fk_header_select = FKH_IPV4 | FKH_UDP;
703 hdr->l3.ip4.fk_saddr = 0xFFFFFFFF;
704 hdr->l3.ip4.fk_daddr = 0xFFFFFFFF;
705 hdr->l4.udp.fk_source = 0xFFFF;
706 hdr->l4.udp.fk_dest = 0xFFFF;
707 fet->default_key = 1;
709 memcpy(&cmd->fet_key, key, sizeof(*key));
710 memcpy(&fet->key, key, sizeof(*key));
711 fet->default_key = 0;
713 cmd->fet_key.fk_packet_tag = 1;
715 args[0] = FM_EXACT_TABLE_ALLOC;
716 args[1] = fm->cmd.pa;
717 ret = flowman_cmd(fm, args, 2);
719 ENICPMD_LOG(ERR, "cannot alloc exact match table: rc=%d", ret);
723 fet->handle = args[0];
724 fet->ingress = ingress;
725 ENICPMD_LOG(DEBUG, "allocated exact match table: handle=0x%" PRIx64,
732 enic_fet_free(struct enic_flowman *fm, struct enic_fm_fet *fet)
734 ENICPMD_FUNC_TRACE();
735 enic_fm_tbl_free(fm, fet->handle);
736 if (!fet->default_key)
737 TAILQ_REMOVE(&fm->fet_list, fet, list);
742 * Get the exact match table for the given combination of
743 * <group, ingress, key>. Allocate one on the fly as necessary.
746 enic_fet_get(struct enic_flowman *fm,
749 struct fm_key_template *key,
750 struct enic_fm_fet **fet_out,
751 struct rte_flow_error *error)
753 struct enic_fm_fet *fet;
755 ENICPMD_FUNC_TRACE();
756 /* See if we already have this table open */
757 TAILQ_FOREACH(fet, &fm->fet_list, list) {
758 if (fet->group == group && fet->ingress == ingress)
762 /* Jumping to a non-existing group? Use the default table */
764 fet = ingress ? fm->default_ig_fet : fm->default_eg_fet;
765 } else if (enic_fet_alloc(fm, ingress, key, 0, &fet)) {
766 return rte_flow_error_set(error, EINVAL,
767 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
768 NULL, "enic: cannot get exact match table");
771 /* Default table is never on the open table list */
772 if (!fet->default_key)
773 TAILQ_INSERT_HEAD(&fm->fet_list, fet, list);
777 ENICPMD_LOG(DEBUG, "fet_get: %s %s group=%u ref=%u",
778 fet->default_key ? "default" : "",
779 fet->ingress ? "ingress" : "egress",
780 fet->group, fet->ref);
785 enic_fet_put(struct enic_flowman *fm, struct enic_fm_fet *fet)
787 ENICPMD_FUNC_TRACE();
788 RTE_ASSERT(fet->ref > 0);
790 ENICPMD_LOG(DEBUG, "fet_put: %s %s group=%u ref=%u",
791 fet->default_key ? "default" : "",
792 fet->ingress ? "ingress" : "egress",
793 fet->group, fet->ref);
795 enic_fet_free(fm, fet);
798 /* Return 1 if current item is valid on top of the previous one. */
800 fm_item_stacking_valid(enum rte_flow_item_type prev_item,
801 const struct enic_fm_items *item_info,
802 uint8_t is_first_item)
804 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
806 ENICPMD_FUNC_TRACE();
807 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
808 if (prev_item == *allowed_items)
812 /* This is the first item in the stack. Check if that's cool */
813 if (is_first_item && item_info->valid_start_item)
819 * Build the flow manager match entry structure from the provided pattern.
820 * The pattern is validated as the items are copied.
823 enic_fm_copy_entry(struct enic_flowman *fm,
824 const struct rte_flow_item pattern[],
825 struct rte_flow_error *error)
827 const struct enic_fm_items *item_info;
828 enum rte_flow_item_type prev_item;
829 const struct rte_flow_item *item;
830 struct copy_item_args args;
831 uint8_t prev_header_level;
832 uint8_t is_first_item;
835 ENICPMD_FUNC_TRACE();
838 prev_item = RTE_FLOW_ITEM_TYPE_END;
840 args.fm_tcam_entry = &fm->tcam_entry;
841 args.header_level = 0;
842 prev_header_level = 0;
843 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
845 * Get info about how to validate and copy the item. If NULL
846 * is returned the nic does not support the item.
848 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
851 item_info = &enic_fm_items[item->type];
853 if (item->type > FM_MAX_ITEM_TYPE ||
854 item_info->copy_item == NULL) {
855 return rte_flow_error_set(error, ENOTSUP,
856 RTE_FLOW_ERROR_TYPE_ITEM,
857 NULL, "enic: unsupported item");
860 /* check to see if item stacking is valid */
861 if (!fm_item_stacking_valid(prev_item, item_info,
866 ret = item_info->copy_item(&args);
868 goto item_not_supported;
869 /* Going from outer to inner? Treat it as a new packet start */
870 if (prev_header_level != args.header_level) {
871 prev_item = RTE_FLOW_ITEM_TYPE_END;
874 prev_item = item->type;
877 prev_header_level = args.header_level;
882 return rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_ITEM,
883 NULL, "enic: unsupported item type");
886 return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
887 item, "enic: unsupported item stack");
891 flow_item_skip_void(const struct rte_flow_item **item)
894 if ((*item)->type != RTE_FLOW_ITEM_TYPE_VOID)
899 append_template(void **template, uint8_t *off, const void *data, int len)
901 memcpy(*template, data, len);
902 *template = (char *)*template + len;
907 enic_fm_append_action_op(struct enic_flowman *fm,
908 struct fm_action_op *fm_op,
909 struct rte_flow_error *error)
913 count = fm->action_op_count;
914 ENICPMD_LOG(DEBUG, "append action op: idx=%d op=%u",
915 count, fm_op->fa_op);
916 if (count == FM_ACTION_OP_MAX) {
917 return rte_flow_error_set(error, EINVAL,
918 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
919 "too many action operations");
921 fm->action.fma_action_ops[count] = *fm_op;
922 fm->action_op_count = count + 1;
926 /* NIC requires that 1st steer appear before decap.
927 * Correct example: steer, decap, steer, steer, ...
930 enic_fm_reorder_action_op(struct enic_flowman *fm)
932 struct fm_action_op *op, *steer, *decap;
933 struct fm_action_op tmp_op;
935 ENICPMD_FUNC_TRACE();
936 /* Find 1st steer and decap */
937 op = fm->action.fma_action_ops;
940 while (op->fa_op != FMOP_END) {
941 if (!decap && op->fa_op == FMOP_DECAP_NOSTRIP)
943 else if (!steer && op->fa_op == FMOP_RQ_STEER)
947 /* If decap is before steer, swap */
948 if (steer && decap && decap < steer) {
949 op = fm->action.fma_action_ops;
950 ENICPMD_LOG(DEBUG, "swap decap %ld <-> steer %ld",
951 (long)(decap - op), (long)(steer - op));
958 /* VXLAN decap is done via flowman compound action */
960 enic_fm_copy_vxlan_decap(struct enic_flowman *fm,
961 struct fm_tcam_match_entry *fmt,
962 const struct rte_flow_action *action,
963 struct rte_flow_error *error)
965 struct fm_header_set *fm_data;
966 struct fm_action_op fm_op;
968 ENICPMD_FUNC_TRACE();
969 fm_data = &fmt->ftm_data.fk_hdrset[0];
970 if (!(fm_data->fk_metadata & FKM_VXLAN)) {
971 return rte_flow_error_set(error, EINVAL,
972 RTE_FLOW_ERROR_TYPE_ACTION, action,
973 "vxlan-decap: vxlan must be in pattern");
976 memset(&fm_op, 0, sizeof(fm_op));
977 fm_op.fa_op = FMOP_DECAP_NOSTRIP;
978 return enic_fm_append_action_op(fm, &fm_op, error);
981 /* Generate a reasonable source port number */
985 /* Min/max below are the default values in OVS-DPDK and Linux */
986 uint16_t p = rte_rand();
987 p = RTE_MAX(p, 32768);
988 p = RTE_MIN(p, 61000);
989 return rte_cpu_to_be_16(p);
992 /* VXLAN encap is done via flowman compound action */
994 enic_fm_copy_vxlan_encap(struct enic_flowman *fm,
995 const struct rte_flow_item *item,
996 struct rte_flow_error *error)
998 struct fm_action_op fm_op;
999 struct rte_ether_hdr *eth;
1000 struct rte_udp_hdr *udp;
1001 uint16_t *ethertype;
1005 ENICPMD_FUNC_TRACE();
1006 memset(&fm_op, 0, sizeof(fm_op));
1007 fm_op.fa_op = FMOP_ENCAP;
1008 template = fm->action.fma_data;
1011 * Copy flow items to the flowman template starting L2.
1012 * L2 must be ethernet.
1014 flow_item_skip_void(&item);
1015 if (item->type != RTE_FLOW_ITEM_TYPE_ETH)
1016 return rte_flow_error_set(error, EINVAL,
1017 RTE_FLOW_ERROR_TYPE_ITEM, item,
1018 "vxlan-encap: first item should be ethernet");
1019 eth = (struct rte_ether_hdr *)template;
1020 ethertype = ð->ether_type;
1021 append_template(&template, &off, item->spec,
1022 sizeof(struct rte_flow_item_eth));
1024 flow_item_skip_void(&item);
1026 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
1027 const struct rte_flow_item_vlan *spec;
1029 ENICPMD_LOG(DEBUG, "vxlan-encap: vlan");
1031 fm_op.encap.outer_vlan = rte_be_to_cpu_16(spec->tci);
1033 flow_item_skip_void(&item);
1035 /* L3 must be IPv4, IPv6 */
1036 switch (item->type) {
1037 case RTE_FLOW_ITEM_TYPE_IPV4:
1039 struct rte_ipv4_hdr *ip4;
1041 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv4");
1042 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
1043 ip4 = (struct rte_ipv4_hdr *)template;
1045 * Offset of IPv4 length field and its initial value
1046 * (IP + UDP + VXLAN) are specified in the action. The NIC
1047 * will add inner packet length.
1049 fm_op.encap.len1_offset = off +
1050 offsetof(struct rte_ipv4_hdr, total_length);
1051 fm_op.encap.len1_delta = sizeof(struct rte_ipv4_hdr) +
1052 sizeof(struct rte_udp_hdr) +
1053 sizeof(struct rte_vxlan_hdr);
1054 append_template(&template, &off, item->spec,
1055 sizeof(struct rte_ipv4_hdr));
1056 ip4->version_ihl = RTE_IPV4_VHL_DEF;
1057 if (ip4->time_to_live == 0)
1058 ip4->time_to_live = IP_DEFTTL;
1059 ip4->next_proto_id = IPPROTO_UDP;
1062 case RTE_FLOW_ITEM_TYPE_IPV6:
1064 struct rte_ipv6_hdr *ip6;
1066 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv6");
1067 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
1068 ip6 = (struct rte_ipv6_hdr *)template;
1069 fm_op.encap.len1_offset = off +
1070 offsetof(struct rte_ipv6_hdr, payload_len);
1071 fm_op.encap.len1_delta = sizeof(struct rte_udp_hdr) +
1072 sizeof(struct rte_vxlan_hdr);
1073 append_template(&template, &off, item->spec,
1074 sizeof(struct rte_ipv6_hdr));
1075 ip6->vtc_flow |= rte_cpu_to_be_32(IP6_VTC_FLOW);
1076 if (ip6->hop_limits == 0)
1077 ip6->hop_limits = IP_DEFTTL;
1078 ip6->proto = IPPROTO_UDP;
1082 return rte_flow_error_set(error,
1083 EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1084 "vxlan-encap: L3 must be IPv4/IPv6");
1087 flow_item_skip_void(&item);
1090 if (item->type != RTE_FLOW_ITEM_TYPE_UDP)
1091 return rte_flow_error_set(error, EINVAL,
1092 RTE_FLOW_ERROR_TYPE_ITEM, item,
1093 "vxlan-encap: UDP must follow IPv4/IPv6");
1094 /* UDP length = UDP + VXLAN. NIC will add inner packet length. */
1095 fm_op.encap.len2_offset =
1096 off + offsetof(struct rte_udp_hdr, dgram_len);
1097 fm_op.encap.len2_delta =
1098 sizeof(struct rte_udp_hdr) + sizeof(struct rte_vxlan_hdr);
1099 udp = (struct rte_udp_hdr *)template;
1100 append_template(&template, &off, item->spec,
1101 sizeof(struct rte_udp_hdr));
1103 * Firmware does not hash/fill source port yet. Generate a
1104 * random port, as there is *usually* one rte_flow for the
1105 * given inner packet stream (i.e. a single stream has one
1108 if (udp->src_port == 0)
1109 udp->src_port = gen_src_port();
1111 flow_item_skip_void(&item);
1114 if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN)
1115 return rte_flow_error_set(error,
1116 EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1117 "vxlan-encap: VXLAN must follow UDP");
1118 append_template(&template, &off, item->spec,
1119 sizeof(struct rte_flow_item_vxlan));
1122 * Fill in the rest of the action structure.
1123 * Indicate that we want to encap with vxlan at packet start.
1125 fm_op.encap.template_offset = 0;
1126 fm_op.encap.template_len = off;
1127 return enic_fm_append_action_op(fm, &fm_op, error);
1131 enic_fm_find_vnic(struct enic *enic, const struct rte_pci_addr *addr,
1138 ENICPMD_FUNC_TRACE();
1139 ENICPMD_LOG(DEBUG, "bdf=%x:%x:%x", addr->bus, addr->devid,
1141 bdf = addr->bus << 8 | addr->devid << 3 | addr->function;
1142 args[0] = FM_VNIC_FIND;
1144 rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1146 /* Expected to fail if BDF is not on the adapter */
1147 ENICPMD_LOG(DEBUG, "cannot find vnic handle: rc=%d", rc);
1151 ENICPMD_LOG(DEBUG, "found vnic: handle=0x%" PRIx64, *handle);
1156 * Egress: target port should be either PF uplink or VF.
1158 * 1. VF egress -> PF uplink
1159 * PF may be this VF's PF, or another PF, as long as they are on the same VIC.
1160 * 2. VF egress -> VF
1163 * 1. PF egress -> VF
1164 * App should be using representor to pass packets to VF
1167 vf_egress_port_id_action(struct enic_flowman *fm,
1168 struct rte_eth_dev *dst_dev,
1169 uint64_t dst_vnic_h,
1170 struct fm_action_op *fm_op,
1171 struct rte_flow_error *error)
1173 struct enic *src_enic, *dst_enic;
1174 struct enic_vf_representor *vf;
1178 ENICPMD_FUNC_TRACE();
1179 src_enic = fm->user_enic;
1180 dst_enic = pmd_priv(dst_dev);
1181 if (!(src_enic->rte_dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)) {
1182 return rte_flow_error_set(error, EINVAL,
1183 RTE_FLOW_ERROR_TYPE_ACTION,
1184 NULL, "source port is not VF representor");
1187 /* VF -> PF uplink. dst is not VF representor */
1188 if (!(dst_dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)) {
1189 /* PF is the VF's PF? Then nothing to do */
1190 vf = VF_ENIC_TO_VF_REP(src_enic);
1191 if (vf->pf == dst_enic) {
1192 ENICPMD_LOG(DEBUG, "destination port is VF's PF");
1195 /* If not, steer to the remote PF's uplink */
1196 uif = dst_enic->fm_vnic_uif;
1197 ENICPMD_LOG(DEBUG, "steer to uplink %u", uif);
1198 memset(fm_op, 0, sizeof(*fm_op));
1199 fm_op->fa_op = FMOP_SET_EGPORT;
1200 fm_op->set_egport.egport = uif;
1201 ret = enic_fm_append_action_op(fm, fm_op, error);
1205 /* VF -> VF loopback. Hairpin and steer to vnic */
1206 memset(fm_op, 0, sizeof(*fm_op));
1207 fm_op->fa_op = FMOP_EG_HAIRPIN;
1208 ret = enic_fm_append_action_op(fm, fm_op, error);
1211 ENICPMD_LOG(DEBUG, "egress hairpin");
1212 fm->hairpin_steer_vnic_h = dst_vnic_h;
1213 fm->need_hairpin_steer = 1;
1217 /* Translate flow actions to flowman TCAM entry actions */
1219 enic_fm_copy_action(struct enic_flowman *fm,
1220 const struct rte_flow_action actions[],
1222 struct rte_flow_error *error)
1233 struct fm_tcam_match_entry *fmt;
1234 struct fm_action_op fm_op;
1235 bool need_ovlan_action;
1244 ENICPMD_FUNC_TRACE();
1245 fmt = &fm->tcam_entry;
1246 need_ovlan_action = false;
1250 enic = fm->user_enic;
1252 vnic_h = enic->fm_vnic_handle;
1254 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1255 switch (actions->type) {
1256 case RTE_FLOW_ACTION_TYPE_VOID:
1258 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1259 if (overlap & PASSTHRU)
1261 overlap |= PASSTHRU;
1264 case RTE_FLOW_ACTION_TYPE_JUMP: {
1265 const struct rte_flow_action_jump *jump =
1267 struct enic_fm_fet *fet;
1271 ret = enic_fet_get(fm, jump->group, ingress, NULL,
1276 memset(&fm_op, 0, sizeof(fm_op));
1277 fm_op.fa_op = FMOP_EXACT_MATCH;
1278 fm_op.exact.handle = fet->handle;
1280 ret = enic_fm_append_action_op(fm, &fm_op, error);
1285 case RTE_FLOW_ACTION_TYPE_MARK: {
1286 const struct rte_flow_action_mark *mark =
1289 if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1290 return rte_flow_error_set(error, EINVAL,
1291 RTE_FLOW_ERROR_TYPE_ACTION,
1292 NULL, "invalid mark id");
1293 memset(&fm_op, 0, sizeof(fm_op));
1294 fm_op.fa_op = FMOP_MARK;
1295 fm_op.mark.mark = mark->id + 1;
1296 ret = enic_fm_append_action_op(fm, &fm_op, error);
1301 case RTE_FLOW_ACTION_TYPE_FLAG: {
1302 /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1303 memset(&fm_op, 0, sizeof(fm_op));
1304 fm_op.fa_op = FMOP_MARK;
1305 fm_op.mark.mark = ENIC_MAGIC_FILTER_ID;
1306 ret = enic_fm_append_action_op(fm, &fm_op, error);
1311 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1312 const struct rte_flow_action_queue *queue =
1316 * If fate other than QUEUE or RSS, fail. Multiple
1317 * rss and queue actions are ok.
1319 if ((overlap & FATE) && first_rq)
1323 memset(&fm_op, 0, sizeof(fm_op));
1324 fm_op.fa_op = FMOP_RQ_STEER;
1325 fm_op.rq_steer.rq_index =
1326 enic_rte_rq_idx_to_sop_idx(queue->index);
1327 fm_op.rq_steer.rq_count = 1;
1328 fm_op.rq_steer.vnic_handle = vnic_h;
1329 ret = enic_fm_append_action_op(fm, &fm_op, error);
1332 ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1333 fm_op.rq_steer.rq_index);
1337 case RTE_FLOW_ACTION_TYPE_DROP: {
1341 memset(&fm_op, 0, sizeof(fm_op));
1342 fm_op.fa_op = FMOP_DROP;
1343 ret = enic_fm_append_action_op(fm, &fm_op, error);
1346 ENICPMD_LOG(DEBUG, "create DROP action");
1349 case RTE_FLOW_ACTION_TYPE_COUNT: {
1350 if (overlap & COUNT)
1353 /* Count is associated with entry not action on VIC. */
1354 fmt->ftm_flags |= FMEF_COUNTER;
1357 case RTE_FLOW_ACTION_TYPE_RSS: {
1358 const struct rte_flow_action_rss *rss = actions->conf;
1363 * If fate other than QUEUE or RSS, fail. Multiple
1364 * rss and queue actions are ok.
1366 if ((overlap & FATE) && first_rq)
1372 * Hardware only supports RSS actions on outer level
1373 * with default type and function. Queues must be
1376 allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1377 rss->level == 0 && (rss->types == 0 ||
1378 rss->types == enic->rss_hf) &&
1379 rss->queue_num <= enic->rq_count &&
1380 rss->queue[rss->queue_num - 1] < enic->rq_count;
1383 /* Identity queue map needs to be sequential */
1384 for (i = 1; i < rss->queue_num; i++)
1385 allow = allow && (rss->queue[i] ==
1386 rss->queue[i - 1] + 1);
1390 memset(&fm_op, 0, sizeof(fm_op));
1391 fm_op.fa_op = FMOP_RQ_STEER;
1392 fm_op.rq_steer.rq_index =
1393 enic_rte_rq_idx_to_sop_idx(rss->queue[0]);
1394 fm_op.rq_steer.rq_count = rss->queue_num;
1395 fm_op.rq_steer.vnic_handle = vnic_h;
1396 ret = enic_fm_append_action_op(fm, &fm_op, error);
1399 ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1400 fm_op.rq_steer.rq_index);
1404 case RTE_FLOW_ACTION_TYPE_PORT_ID: {
1405 const struct rte_flow_action_port_id *port;
1406 struct rte_eth_dev *dev;
1408 if (!ingress && (overlap & PORT_ID)) {
1409 ENICPMD_LOG(DEBUG, "cannot have multiple egress PORT_ID actions");
1412 port = actions->conf;
1413 if (port->original) {
1414 vnic_h = enic->fm_vnic_handle; /* This port */
1417 ENICPMD_LOG(DEBUG, "port id %u", port->id);
1418 if (!rte_eth_dev_is_valid_port(port->id)) {
1419 return rte_flow_error_set(error, EINVAL,
1420 RTE_FLOW_ERROR_TYPE_ACTION,
1421 NULL, "invalid port_id");
1423 dev = &rte_eth_devices[port->id];
1424 if (!dev_is_enic(dev)) {
1425 return rte_flow_error_set(error, EINVAL,
1426 RTE_FLOW_ERROR_TYPE_ACTION,
1427 NULL, "port_id is not enic");
1429 if (enic->switch_domain_id !=
1430 pmd_priv(dev)->switch_domain_id) {
1431 return rte_flow_error_set(error, EINVAL,
1432 RTE_FLOW_ERROR_TYPE_ACTION,
1433 NULL, "destination and source ports are not in the same switch domain");
1435 vnic_h = pmd_priv(dev)->fm_vnic_handle;
1438 * Ingress. Nothing more to do. We add an implicit
1439 * steer at the end if needed.
1444 ret = vf_egress_port_id_action(fm, dev, vnic_h, &fm_op,
1450 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP: {
1451 if (overlap & DECAP)
1455 ret = enic_fm_copy_vxlan_decap(fm, fmt, actions,
1461 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP: {
1462 const struct rte_flow_action_vxlan_encap *encap;
1464 encap = actions->conf;
1465 if (overlap & ENCAP)
1468 ret = enic_fm_copy_vxlan_encap(fm, encap->definition,
1474 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN: {
1475 memset(&fm_op, 0, sizeof(fm_op));
1476 fm_op.fa_op = FMOP_POP_VLAN;
1477 ret = enic_fm_append_action_op(fm, &fm_op, error);
1482 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN: {
1483 const struct rte_flow_action_of_push_vlan *vlan;
1485 if (overlap & PASSTHRU)
1487 vlan = actions->conf;
1488 if (vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN)) {
1489 return rte_flow_error_set(error, EINVAL,
1490 RTE_FLOW_ERROR_TYPE_ACTION,
1491 NULL, "unexpected push_vlan ethertype");
1493 overlap |= PUSH_VLAN;
1494 need_ovlan_action = true;
1497 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP: {
1498 const struct rte_flow_action_of_set_vlan_pcp *pcp;
1500 pcp = actions->conf;
1501 if (pcp->vlan_pcp > 7) {
1502 return rte_flow_error_set(error, EINVAL,
1503 RTE_FLOW_ERROR_TYPE_ACTION,
1504 NULL, "invalid vlan_pcp");
1506 need_ovlan_action = true;
1507 ovlan |= ((uint16_t)pcp->vlan_pcp) << 13;
1510 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID: {
1511 const struct rte_flow_action_of_set_vlan_vid *vid;
1513 vid = actions->conf;
1514 need_ovlan_action = true;
1515 ovlan |= rte_be_to_cpu_16(vid->vlan_vid);
1523 if (!(overlap & (FATE | PASSTHRU | COUNT | PORT_ID)))
1525 /* Egress from VF: need implicit WQ match */
1526 if (enic_is_vf_rep(enic) && !ingress) {
1527 fmt->ftm_data.fk_wq_id = 0;
1528 fmt->ftm_mask.fk_wq_id = 0xffff;
1529 fmt->ftm_data.fk_wq_vnic = enic->fm_vnic_handle;
1530 ENICPMD_LOG(DEBUG, "add implicit wq id match for vf %d",
1531 VF_ENIC_TO_VF_REP(enic)->vf_id);
1533 if (need_ovlan_action) {
1534 memset(&fm_op, 0, sizeof(fm_op));
1535 fm_op.fa_op = FMOP_SET_OVLAN;
1536 fm_op.ovlan.vlan = ovlan;
1537 ret = enic_fm_append_action_op(fm, &fm_op, error);
1541 /* Add steer op for PORT_ID without QUEUE */
1542 if ((overlap & PORT_ID) && !steer && ingress) {
1543 memset(&fm_op, 0, sizeof(fm_op));
1544 /* Always to queue 0 for now as generic RSS is not available */
1545 fm_op.fa_op = FMOP_RQ_STEER;
1546 fm_op.rq_steer.rq_index = 0;
1547 fm_op.rq_steer.vnic_handle = vnic_h;
1548 ret = enic_fm_append_action_op(fm, &fm_op, error);
1551 ENICPMD_LOG(DEBUG, "add implicit steer op");
1553 /* Add required END */
1554 memset(&fm_op, 0, sizeof(fm_op));
1555 fm_op.fa_op = FMOP_END;
1556 ret = enic_fm_append_action_op(fm, &fm_op, error);
1559 enic_fm_reorder_action_op(fm);
1563 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1564 NULL, "enic: unsupported action");
1567 /** Check if the action is supported */
1569 enic_fm_match_action(const struct rte_flow_action *action,
1570 const enum rte_flow_action_type *supported_actions)
1572 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1573 supported_actions++) {
1574 if (action->type == *supported_actions)
1580 /* Debug function to dump internal NIC action structure. */
1582 enic_fm_dump_tcam_actions(const struct fm_action *fm_action)
1584 /* Manually keep in sync with FMOP commands */
1585 const char *fmop_str[FMOP_OP_MAX] = {
1587 [FMOP_DROP] = "drop",
1588 [FMOP_RQ_STEER] = "steer",
1589 [FMOP_EXACT_MATCH] = "exmatch",
1590 [FMOP_MARK] = "mark",
1591 [FMOP_EXT_MARK] = "ext_mark",
1593 [FMOP_EG_HAIRPIN] = "eg_hairpin",
1594 [FMOP_IG_HAIRPIN] = "ig_hairpin",
1595 [FMOP_ENCAP_IVLAN] = "encap_ivlan",
1596 [FMOP_ENCAP_NOIVLAN] = "encap_noivlan",
1597 [FMOP_ENCAP] = "encap",
1598 [FMOP_SET_OVLAN] = "set_ovlan",
1599 [FMOP_DECAP_NOSTRIP] = "decap_nostrip",
1600 [FMOP_DECAP_STRIP] = "decap_strip",
1601 [FMOP_POP_VLAN] = "pop_vlan",
1602 [FMOP_SET_EGPORT] = "set_egport",
1603 [FMOP_RQ_STEER_ONLY] = "rq_steer_only",
1604 [FMOP_SET_ENCAP_VLAN] = "set_encap_vlan",
1605 [FMOP_EMIT] = "emit",
1606 [FMOP_MODIFY] = "modify",
1608 const struct fm_action_op *op = &fm_action->fma_action_ops[0];
1609 char buf[128], *bp = buf;
1614 buf_len = sizeof(buf);
1615 for (i = 0; i < FM_ACTION_OP_MAX; i++) {
1616 if (op->fa_op == FMOP_END)
1618 if (op->fa_op >= FMOP_OP_MAX)
1621 op_str = fmop_str[op->fa_op];
1622 n = snprintf(bp, buf_len, "%s,", op_str);
1623 if (n > 0 && n < buf_len) {
1629 /* Remove trailing comma */
1632 ENICPMD_LOG(DEBUG, " Acions: %s", buf);
1636 bits_to_str(uint32_t bits, const char *strings[], int max,
1637 char *buf, int buf_len)
1639 int i, n = 0, len = 0;
1641 for (i = 0; i < max; i++) {
1642 if (bits & (1 << i)) {
1643 n = snprintf(buf, buf_len, "%s,", strings[i]);
1644 if (n > 0 && n < buf_len) {
1651 /* Remove trailing comma */
1659 /* Debug function to dump internal NIC filter structure. */
1661 __enic_fm_dump_tcam_match(const struct fm_header_set *fk_hdrset, char *buf,
1664 /* Manually keep in sync with FKM_BITS */
1665 const char *fm_fkm_str[FKM_BIT_COUNT] = {
1666 [FKM_QTAG_BIT] = "qtag",
1667 [FKM_CMD_BIT] = "cmd",
1668 [FKM_IPV4_BIT] = "ip4",
1669 [FKM_IPV6_BIT] = "ip6",
1670 [FKM_ROCE_BIT] = "roce",
1671 [FKM_UDP_BIT] = "udp",
1672 [FKM_TCP_BIT] = "tcp",
1673 [FKM_TCPORUDP_BIT] = "tcpportudp",
1674 [FKM_IPFRAG_BIT] = "ipfrag",
1675 [FKM_NVGRE_BIT] = "nvgre",
1676 [FKM_VXLAN_BIT] = "vxlan",
1677 [FKM_GENEVE_BIT] = "geneve",
1678 [FKM_NSH_BIT] = "nsh",
1679 [FKM_ROCEV2_BIT] = "rocev2",
1680 [FKM_VLAN_PRES_BIT] = "vlan_pres",
1681 [FKM_IPOK_BIT] = "ipok",
1682 [FKM_L4OK_BIT] = "l4ok",
1683 [FKM_ROCEOK_BIT] = "roceok",
1684 [FKM_FCSOK_BIT] = "fcsok",
1685 [FKM_EG_SPAN_BIT] = "eg_span",
1686 [FKM_IG_SPAN_BIT] = "ig_span",
1687 [FKM_EG_HAIRPINNED_BIT] = "eg_hairpinned",
1689 /* Manually keep in sync with FKH_BITS */
1690 const char *fm_fkh_str[FKH_BIT_COUNT] = {
1691 [FKH_ETHER_BIT] = "eth",
1692 [FKH_QTAG_BIT] = "qtag",
1693 [FKH_L2RAW_BIT] = "l2raw",
1694 [FKH_IPV4_BIT] = "ip4",
1695 [FKH_IPV6_BIT] = "ip6",
1696 [FKH_L3RAW_BIT] = "l3raw",
1697 [FKH_UDP_BIT] = "udp",
1698 [FKH_TCP_BIT] = "tcp",
1699 [FKH_ICMP_BIT] = "icmp",
1700 [FKH_VXLAN_BIT] = "vxlan",
1701 [FKH_L4RAW_BIT] = "l4raw",
1703 uint32_t fkh_bits = fk_hdrset->fk_header_select;
1704 uint32_t fkm_bits = fk_hdrset->fk_metadata;
1707 if (!fkm_bits && !fkh_bits)
1709 n = snprintf(buf, buf_len, "metadata(");
1710 if (n > 0 && n < buf_len) {
1714 n = bits_to_str(fkm_bits, fm_fkm_str, FKM_BIT_COUNT, buf, buf_len);
1715 if (n > 0 && n < buf_len) {
1719 n = snprintf(buf, buf_len, ") valid hdr fields(");
1720 if (n > 0 && n < buf_len) {
1724 n = bits_to_str(fkh_bits, fm_fkh_str, FKH_BIT_COUNT, buf, buf_len);
1725 if (n > 0 && n < buf_len) {
1729 snprintf(buf, buf_len, ")");
1733 enic_fm_dump_tcam_match(const struct fm_tcam_match_entry *match,
1738 memset(buf, 0, sizeof(buf));
1739 __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[0],
1741 ENICPMD_LOG(DEBUG, " TCAM %s Outer: %s %scounter position %u",
1742 (ingress) ? "IG" : "EG", buf,
1743 (match->ftm_flags & FMEF_COUNTER) ? "" : "no ",
1744 match->ftm_position);
1745 memset(buf, 0, sizeof(buf));
1746 __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[1],
1749 ENICPMD_LOG(DEBUG, " Inner: %s", buf);
1752 /* Debug function to dump internal NIC flow structures. */
1754 enic_fm_dump_tcam_entry(const struct fm_tcam_match_entry *fm_match,
1755 const struct fm_action *fm_action,
1758 if (!rte_log_can_log(enic_pmd_logtype, RTE_LOG_DEBUG))
1760 enic_fm_dump_tcam_match(fm_match, ingress);
1761 enic_fm_dump_tcam_actions(fm_action);
1765 enic_fm_flow_parse(struct enic_flowman *fm,
1766 const struct rte_flow_attr *attrs,
1767 const struct rte_flow_item pattern[],
1768 const struct rte_flow_action actions[],
1769 struct rte_flow_error *error)
1771 const struct rte_flow_action *action;
1773 static const enum rte_flow_action_type *sa;
1775 ENICPMD_FUNC_TRACE();
1778 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1779 NULL, "no pattern specified");
1784 rte_flow_error_set(error, EINVAL,
1785 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1786 NULL, "no action specified");
1791 if (attrs->group != FM_TCAM_RTE_GROUP && attrs->priority) {
1792 rte_flow_error_set(error, ENOTSUP,
1793 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1795 "priorities are not supported for non-default (0) groups");
1797 } else if (!fm->owner_enic->switchdev_mode && attrs->transfer) {
1798 rte_flow_error_set(error, ENOTSUP,
1799 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1801 "transfer is not supported");
1803 } else if (attrs->ingress && attrs->egress) {
1804 rte_flow_error_set(error, ENOTSUP,
1805 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1807 "bidirectional rules not supported");
1812 rte_flow_error_set(error, EINVAL,
1813 RTE_FLOW_ERROR_TYPE_ATTR,
1814 NULL, "no attribute specified");
1818 /* Verify Actions. */
1819 sa = (attrs->ingress) ? enic_fm_supported_ig_actions :
1820 enic_fm_supported_eg_actions;
1821 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1823 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1825 else if (!enic_fm_match_action(action, sa))
1828 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1829 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1830 action, "invalid action");
1833 ret = enic_fm_copy_entry(fm, pattern, error);
1836 ret = enic_fm_copy_action(fm, actions, attrs->ingress, error);
1841 enic_fm_counter_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1843 if (!fm_flow->counter_valid)
1845 SLIST_INSERT_HEAD(&fm->counters, fm_flow->counter, next);
1846 fm_flow->counter_valid = false;
1850 enic_fm_more_counters(struct enic_flowman *fm)
1852 struct enic_fm_counter *new_stack;
1853 struct enic_fm_counter *ctrs;
1857 ENICPMD_FUNC_TRACE();
1858 new_stack = rte_realloc(fm->counter_stack, (fm->counters_alloced +
1859 FM_COUNTERS_EXPAND) *
1860 sizeof(struct enic_fm_counter), 0);
1861 if (new_stack == NULL) {
1862 ENICPMD_LOG(ERR, "cannot alloc counter memory");
1865 fm->counter_stack = new_stack;
1867 args[0] = FM_COUNTER_BRK;
1868 args[1] = fm->counters_alloced + FM_COUNTERS_EXPAND;
1869 rc = flowman_cmd(fm, args, 2);
1871 ENICPMD_LOG(ERR, "cannot alloc counters rc=%d", rc);
1874 ctrs = (struct enic_fm_counter *)fm->counter_stack +
1875 fm->counters_alloced;
1876 for (i = 0; i < FM_COUNTERS_EXPAND; i++, ctrs++) {
1877 ctrs->handle = fm->counters_alloced + i;
1878 SLIST_INSERT_HEAD(&fm->counters, ctrs, next);
1880 fm->counters_alloced += FM_COUNTERS_EXPAND;
1881 ENICPMD_LOG(DEBUG, "%u counters allocated, total: %u",
1882 FM_COUNTERS_EXPAND, fm->counters_alloced);
1887 enic_fm_counter_zero(struct enic_flowman *fm, struct enic_fm_counter *c)
1892 ENICPMD_FUNC_TRACE();
1893 args[0] = FM_COUNTER_QUERY;
1894 args[1] = c->handle;
1895 args[2] = 1; /* clear */
1896 ret = flowman_cmd(fm, args, 3);
1898 ENICPMD_LOG(ERR, "counter init: rc=%d handle=0x%x",
1906 enic_fm_counter_alloc(struct enic_flowman *fm, struct rte_flow_error *error,
1907 struct enic_fm_counter **ctr)
1909 struct enic_fm_counter *c;
1912 ENICPMD_FUNC_TRACE();
1914 if (SLIST_EMPTY(&fm->counters)) {
1915 ret = enic_fm_more_counters(fm);
1917 return rte_flow_error_set(error, -ret,
1918 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1919 NULL, "enic: out of counters");
1921 c = SLIST_FIRST(&fm->counters);
1922 SLIST_REMOVE_HEAD(&fm->counters, next);
1928 enic_fm_action_free(struct enic_flowman *fm, uint64_t handle)
1933 ENICPMD_FUNC_TRACE();
1934 args[0] = FM_ACTION_FREE;
1936 rc = flowman_cmd(fm, args, 2);
1938 ENICPMD_LOG(ERR, "cannot free action: rc=%d handle=0x%" PRIx64,
1944 enic_fm_entry_free(struct enic_flowman *fm, uint64_t handle)
1949 ENICPMD_FUNC_TRACE();
1950 args[0] = FM_MATCH_ENTRY_REMOVE;
1952 rc = flowman_cmd(fm, args, 2);
1954 ENICPMD_LOG(ERR, "cannot free match entry: rc=%d"
1955 " handle=0x%" PRIx64, rc, handle);
1959 static struct enic_fm_jump_flow *
1960 find_jump_flow(struct enic_flowman *fm, uint32_t group)
1962 struct enic_fm_jump_flow *j;
1964 ENICPMD_FUNC_TRACE();
1965 TAILQ_FOREACH(j, &fm->jump_list, list) {
1966 if (j->group == group)
1973 remove_jump_flow(struct enic_flowman *fm, struct rte_flow *flow)
1975 struct enic_fm_jump_flow *j;
1977 ENICPMD_FUNC_TRACE();
1978 TAILQ_FOREACH(j, &fm->jump_list, list) {
1979 if (j->flow == flow) {
1980 TAILQ_REMOVE(&fm->jump_list, j, list);
1988 save_jump_flow(struct enic_flowman *fm,
1989 struct rte_flow *flow,
1991 struct fm_tcam_match_entry *match,
1992 struct fm_action *action)
1994 struct enic_fm_jump_flow *j;
1996 ENICPMD_FUNC_TRACE();
1997 j = calloc(1, sizeof(struct enic_fm_jump_flow));
2003 j->action = *action;
2004 TAILQ_INSERT_HEAD(&fm->jump_list, j, list);
2005 ENICPMD_LOG(DEBUG, "saved jump flow: flow=%p group=%u", flow, group);
2010 __enic_fm_flow_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
2012 if (fm_flow->entry_handle != FM_INVALID_HANDLE) {
2013 enic_fm_entry_free(fm, fm_flow->entry_handle);
2014 fm_flow->entry_handle = FM_INVALID_HANDLE;
2016 if (fm_flow->action_handle != FM_INVALID_HANDLE) {
2017 enic_fm_action_free(fm, fm_flow->action_handle);
2018 fm_flow->action_handle = FM_INVALID_HANDLE;
2020 enic_fm_counter_free(fm, fm_flow);
2022 enic_fet_put(fm, fm_flow->fet);
2023 fm_flow->fet = NULL;
2028 enic_fm_flow_free(struct enic_flowman *fm, struct rte_flow *flow)
2030 struct enic_fm_flow *steer = flow->fm->hairpin_steer_flow;
2032 if (flow->fm->fet && flow->fm->fet->default_key)
2033 remove_jump_flow(fm, flow);
2034 __enic_fm_flow_free(fm, flow->fm);
2036 __enic_fm_flow_free(fm, steer);
2044 enic_fm_add_tcam_entry(struct enic_flowman *fm,
2045 struct fm_tcam_match_entry *match_in,
2046 uint64_t *entry_handle,
2048 struct rte_flow_error *error)
2050 struct fm_tcam_match_entry *ftm;
2054 ENICPMD_FUNC_TRACE();
2055 /* Copy entry to the command buffer */
2056 ftm = &fm->cmd.va->fm_tcam_match_entry;
2057 memcpy(ftm, match_in, sizeof(*ftm));
2058 /* Add TCAM entry */
2059 args[0] = FM_TCAM_ENTRY_INSTALL;
2060 args[1] = ingress ? fm->ig_tcam_hndl : fm->eg_tcam_hndl;
2061 args[2] = fm->cmd.pa;
2062 ret = flowman_cmd(fm, args, 3);
2064 ENICPMD_LOG(ERR, "cannot add %s TCAM entry: rc=%d",
2065 ingress ? "ingress" : "egress", ret);
2066 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2067 NULL, "enic: devcmd(tcam-entry-install)");
2070 ENICPMD_LOG(DEBUG, "installed %s TCAM entry: handle=0x%" PRIx64,
2071 ingress ? "ingress" : "egress", (uint64_t)args[0]);
2072 *entry_handle = args[0];
2077 enic_fm_add_exact_entry(struct enic_flowman *fm,
2078 struct fm_tcam_match_entry *match_in,
2079 uint64_t *entry_handle,
2080 struct enic_fm_fet *fet,
2081 struct rte_flow_error *error)
2083 struct fm_exact_match_entry *fem;
2087 ENICPMD_FUNC_TRACE();
2088 /* The new entry must have the table's key */
2089 if (memcmp(fet->key.fk_hdrset, match_in->ftm_mask.fk_hdrset,
2090 sizeof(struct fm_header_set) * FM_HDRSET_MAX)) {
2091 return rte_flow_error_set(error, EINVAL,
2092 RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2093 "enic: key does not match group's key");
2096 /* Copy entry to the command buffer */
2097 fem = &fm->cmd.va->fm_exact_match_entry;
2099 * Translate TCAM entry to exact entry. As is only need to drop
2100 * position and mask. The mask is part of the exact match table.
2101 * Position (aka priority) is not supported in the exact match table.
2103 fem->fem_data = match_in->ftm_data;
2104 fem->fem_flags = match_in->ftm_flags;
2105 fem->fem_action = match_in->ftm_action;
2106 fem->fem_counter = match_in->ftm_counter;
2108 /* Add exact entry */
2109 args[0] = FM_EXACT_ENTRY_INSTALL;
2110 args[1] = fet->handle;
2111 args[2] = fm->cmd.pa;
2112 ret = flowman_cmd(fm, args, 3);
2114 ENICPMD_LOG(ERR, "cannot add %s exact entry: group=%u",
2115 fet->ingress ? "ingress" : "egress", fet->group);
2116 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2117 NULL, "enic: devcmd(exact-entry-install)");
2120 ENICPMD_LOG(DEBUG, "installed %s exact entry: group=%u"
2121 " handle=0x%" PRIx64,
2122 fet->ingress ? "ingress" : "egress", fet->group,
2124 *entry_handle = args[0];
2128 /* Push match-action to the NIC. */
2130 __enic_fm_flow_add_entry(struct enic_flowman *fm,
2131 struct enic_fm_flow *fm_flow,
2132 struct fm_tcam_match_entry *match_in,
2133 struct fm_action *action_in,
2136 struct rte_flow_error *error)
2138 struct enic_fm_counter *ctr;
2139 struct fm_action *fma;
2145 ENICPMD_FUNC_TRACE();
2146 /* Allocate action. */
2147 fma = &fm->cmd.va->fm_action;
2148 memcpy(fma, action_in, sizeof(*fma));
2149 args[0] = FM_ACTION_ALLOC;
2150 args[1] = fm->cmd.pa;
2151 ret = flowman_cmd(fm, args, 2);
2153 ENICPMD_LOG(ERR, "allocating TCAM table action rc=%d", ret);
2154 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2155 NULL, "enic: devcmd(action-alloc)");
2159 fm_flow->action_handle = action_h;
2160 match_in->ftm_action = action_h;
2161 ENICPMD_LOG(DEBUG, "action allocated: handle=0x%" PRIx64, action_h);
2163 /* Allocate counter if requested. */
2164 if (match_in->ftm_flags & FMEF_COUNTER) {
2165 ret = enic_fm_counter_alloc(fm, error, &ctr);
2166 if (ret) /* error has been filled in */
2168 fm_flow->counter_valid = true;
2169 fm_flow->counter = ctr;
2170 match_in->ftm_counter = ctr->handle;
2174 * Get the group's table (either TCAM or exact match table) and
2175 * add entry to it. If we use the exact match table, the handler
2176 * will translate the TCAM entry (match_in) to the appropriate
2177 * exact match entry and use that instead.
2179 entry_h = FM_INVALID_HANDLE;
2180 if (group == FM_TCAM_RTE_GROUP) {
2181 ret = enic_fm_add_tcam_entry(fm, match_in, &entry_h, ingress,
2185 /* Jump action might have a ref to fet */
2186 fm_flow->fet = fm->fet;
2189 struct enic_fm_fet *fet = NULL;
2191 ret = enic_fet_get(fm, group, ingress,
2192 &match_in->ftm_mask, &fet, error);
2196 ret = enic_fm_add_exact_entry(fm, match_in, &entry_h, fet,
2201 /* Clear counter after adding entry, as it requires in-use counter */
2202 if (fm_flow->counter_valid) {
2203 ret = enic_fm_counter_zero(fm, fm_flow->counter);
2207 fm_flow->entry_handle = entry_h;
2211 /* Push match-action to the NIC. */
2212 static struct rte_flow *
2213 enic_fm_flow_add_entry(struct enic_flowman *fm,
2214 struct fm_tcam_match_entry *match_in,
2215 struct fm_action *action_in,
2216 const struct rte_flow_attr *attrs,
2217 struct rte_flow_error *error)
2219 struct enic_fm_flow *fm_flow;
2220 struct rte_flow *flow;
2222 ENICPMD_FUNC_TRACE();
2223 match_in->ftm_position = attrs->priority;
2224 enic_fm_dump_tcam_entry(match_in, action_in, attrs->ingress);
2225 flow = calloc(1, sizeof(*flow));
2226 fm_flow = calloc(1, sizeof(*fm_flow));
2227 if (flow == NULL || fm_flow == NULL) {
2228 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2229 NULL, "enic: cannot allocate rte_flow");
2235 fm_flow->action_handle = FM_INVALID_HANDLE;
2236 fm_flow->entry_handle = FM_INVALID_HANDLE;
2237 if (__enic_fm_flow_add_entry(fm, fm_flow, match_in, action_in,
2238 attrs->group, attrs->ingress, error)) {
2239 enic_fm_flow_free(fm, flow);
2246 convert_jump_flows(struct enic_flowman *fm, struct enic_fm_fet *fet,
2247 struct rte_flow_error *error)
2249 struct enic_fm_flow *fm_flow;
2250 struct enic_fm_jump_flow *j;
2251 struct fm_action *fma;
2254 ENICPMD_FUNC_TRACE();
2256 * Find the saved flows that should jump to the new table (fet).
2257 * Then delete the old TCAM entry that jumps to the default table,
2258 * and add a new one that jumps to the new table.
2261 j = find_jump_flow(fm, group);
2263 ENICPMD_LOG(DEBUG, "convert jump flow: flow=%p group=%u",
2265 /* Delete old entry */
2266 fm_flow = j->flow->fm;
2267 __enic_fm_flow_free(fm, fm_flow);
2271 fma->fma_action_ops[0].exact.handle = fet->handle;
2272 if (__enic_fm_flow_add_entry(fm, fm_flow, &j->match, fma,
2273 FM_TCAM_RTE_GROUP, fet->ingress, error)) {
2274 /* Cannot roll back changes at the moment */
2275 ENICPMD_LOG(ERR, "cannot convert jump flow: flow=%p",
2280 ENICPMD_LOG(DEBUG, "convert ok: group=%u ref=%u",
2281 fet->group, fet->ref);
2284 TAILQ_REMOVE(&fm->jump_list, j, list);
2286 j = find_jump_flow(fm, group);
2291 add_hairpin_steer(struct enic_flowman *fm, struct rte_flow *flow,
2292 struct rte_flow_error *error)
2294 struct fm_tcam_match_entry *fm_tcam_entry;
2295 struct enic_fm_flow *fm_flow;
2296 struct fm_action *fm_action;
2297 struct fm_action_op fm_op;
2300 ENICPMD_FUNC_TRACE();
2301 fm_flow = calloc(1, sizeof(*fm_flow));
2302 if (fm_flow == NULL) {
2303 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2304 NULL, "enic: cannot allocate rte_flow");
2307 /* Original egress hairpin flow */
2308 fm_tcam_entry = &fm->tcam_entry;
2309 fm_action = &fm->action;
2310 /* Use the match pattern of the egress flow as is, without counters */
2311 fm_tcam_entry->ftm_flags &= ~FMEF_COUNTER;
2312 /* The only action is steer to vnic */
2313 fm->action_op_count = 0;
2314 memset(fm_action, 0, sizeof(*fm_action));
2315 memset(&fm_op, 0, sizeof(fm_op));
2316 /* Always to queue 0 for now */
2317 fm_op.fa_op = FMOP_RQ_STEER;
2318 fm_op.rq_steer.rq_index = 0;
2319 fm_op.rq_steer.vnic_handle = fm->hairpin_steer_vnic_h;
2320 ret = enic_fm_append_action_op(fm, &fm_op, error);
2322 goto error_with_flow;
2323 ENICPMD_LOG(DEBUG, "add steer op");
2324 /* Add required END */
2325 memset(&fm_op, 0, sizeof(fm_op));
2326 fm_op.fa_op = FMOP_END;
2327 ret = enic_fm_append_action_op(fm, &fm_op, error);
2329 goto error_with_flow;
2330 /* Add the ingress flow */
2331 fm_flow->action_handle = FM_INVALID_HANDLE;
2332 fm_flow->entry_handle = FM_INVALID_HANDLE;
2333 ret = __enic_fm_flow_add_entry(fm, fm_flow, fm_tcam_entry, fm_action,
2334 FM_TCAM_RTE_GROUP, 1 /* ingress */, error);
2336 ENICPMD_LOG(ERR, "cannot add hairpin-steer flow");
2337 goto error_with_flow;
2339 /* The new flow is now the egress flow's paired flow */
2340 flow->fm->hairpin_steer_flow = fm_flow;
2349 enic_fm_open_scratch(struct enic_flowman *fm)
2351 fm->action_op_count = 0;
2353 fm->need_hairpin_steer = 0;
2354 fm->hairpin_steer_vnic_h = 0;
2355 memset(&fm->tcam_entry, 0, sizeof(fm->tcam_entry));
2356 memset(&fm->action, 0, sizeof(fm->action));
2360 enic_fm_close_scratch(struct enic_flowman *fm)
2363 enic_fet_put(fm, fm->fet);
2366 fm->action_op_count = 0;
2370 enic_fm_flow_validate(struct rte_eth_dev *dev,
2371 const struct rte_flow_attr *attrs,
2372 const struct rte_flow_item pattern[],
2373 const struct rte_flow_action actions[],
2374 struct rte_flow_error *error)
2376 struct fm_tcam_match_entry *fm_tcam_entry;
2377 struct fm_action *fm_action;
2378 struct enic_flowman *fm;
2381 ENICPMD_FUNC_TRACE();
2382 fm = begin_fm(pmd_priv(dev));
2385 enic_fm_open_scratch(fm);
2386 ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2388 fm_tcam_entry = &fm->tcam_entry;
2389 fm_action = &fm->action;
2390 enic_fm_dump_tcam_entry(fm_tcam_entry, fm_action,
2393 enic_fm_close_scratch(fm);
2399 enic_fm_flow_query_count(struct rte_eth_dev *dev,
2400 struct rte_flow *flow, void *data,
2401 struct rte_flow_error *error)
2403 struct rte_flow_query_count *query;
2404 struct enic_fm_flow *fm_flow;
2405 struct enic_flowman *fm;
2409 ENICPMD_FUNC_TRACE();
2410 fm = begin_fm(pmd_priv(dev));
2413 if (!fm_flow->counter_valid) {
2414 rc = rte_flow_error_set(error, ENOTSUP,
2415 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2416 "enic: flow does not have counter");
2420 args[0] = FM_COUNTER_QUERY;
2421 args[1] = fm_flow->counter->handle;
2422 args[2] = query->reset;
2423 rc = flowman_cmd(fm, args, 3);
2425 ENICPMD_LOG(ERR, "cannot query counter: rc=%d handle=0x%x",
2426 rc, fm_flow->counter->handle);
2429 query->hits_set = 1;
2430 query->hits = args[0];
2431 query->bytes_set = 1;
2432 query->bytes = args[1];
2440 enic_fm_flow_query(struct rte_eth_dev *dev,
2441 struct rte_flow *flow,
2442 const struct rte_flow_action *actions,
2444 struct rte_flow_error *error)
2448 ENICPMD_FUNC_TRACE();
2449 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2450 switch (actions->type) {
2451 case RTE_FLOW_ACTION_TYPE_VOID:
2453 case RTE_FLOW_ACTION_TYPE_COUNT:
2454 ret = enic_fm_flow_query_count(dev, flow, data, error);
2457 return rte_flow_error_set(error, ENOTSUP,
2458 RTE_FLOW_ERROR_TYPE_ACTION,
2460 "action not supported");
2468 static struct rte_flow *
2469 enic_fm_flow_create(struct rte_eth_dev *dev,
2470 const struct rte_flow_attr *attrs,
2471 const struct rte_flow_item pattern[],
2472 const struct rte_flow_action actions[],
2473 struct rte_flow_error *error)
2475 struct fm_tcam_match_entry *fm_tcam_entry;
2476 struct fm_action *fm_action;
2477 struct enic_flowman *fm;
2478 struct enic_fm_fet *fet;
2479 struct rte_flow *flow;
2483 ENICPMD_FUNC_TRACE();
2484 enic = pmd_priv(dev);
2485 fm = begin_fm(enic);
2487 rte_flow_error_set(error, ENOTSUP,
2488 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2489 "flowman is not initialized");
2492 enic_fm_open_scratch(fm);
2494 ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2496 goto error_with_scratch;
2497 fm_tcam_entry = &fm->tcam_entry;
2498 fm_action = &fm->action;
2499 flow = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2502 /* Add ingress rule that pairs with hairpin rule */
2503 if (fm->need_hairpin_steer) {
2504 ret = add_hairpin_steer(fm, flow, error);
2506 enic_fm_flow_free(fm, flow);
2508 goto error_with_scratch;
2511 LIST_INSERT_HEAD(&enic->flows, flow, next);
2512 fet = flow->fm->fet;
2513 if (fet && fet->default_key) {
2515 * Jump to non-existent group? Save the relevant info
2516 * so we can convert this flow when that group
2519 save_jump_flow(fm, flow, fet->group,
2520 fm_tcam_entry, fm_action);
2521 } else if (fet && fet->ref == 1) {
2523 * A new table is created. Convert the saved flows
2524 * that should jump to this group.
2526 convert_jump_flows(fm, fet, error);
2531 enic_fm_close_scratch(fm);
2537 enic_fm_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
2538 __rte_unused struct rte_flow_error *error)
2540 struct enic *enic = pmd_priv(dev);
2541 struct enic_flowman *fm;
2543 ENICPMD_FUNC_TRACE();
2544 fm = begin_fm(enic);
2547 LIST_REMOVE(flow, next);
2548 enic_fm_flow_free(fm, flow);
2554 enic_fm_flow_flush(struct rte_eth_dev *dev,
2555 __rte_unused struct rte_flow_error *error)
2557 LIST_HEAD(enic_flows, rte_flow) internal;
2558 struct enic_fm_flow *fm_flow;
2559 struct enic_flowman *fm;
2560 struct rte_flow *flow;
2561 struct enic *enic = pmd_priv(dev);
2563 ENICPMD_FUNC_TRACE();
2565 fm = begin_fm(enic);
2568 /* Destroy all non-internal flows */
2569 LIST_INIT(&internal);
2570 while (!LIST_EMPTY(&enic->flows)) {
2571 flow = LIST_FIRST(&enic->flows);
2573 LIST_REMOVE(flow, next);
2574 if (flow->internal) {
2575 LIST_INSERT_HEAD(&internal, flow, next);
2579 * If tables are null, then vNIC is closing, and the firmware
2580 * has already cleaned up flowman state. So do not try to free
2581 * resources, as it only causes errors.
2583 if (fm->ig_tcam_hndl == FM_INVALID_HANDLE) {
2584 fm_flow->entry_handle = FM_INVALID_HANDLE;
2585 fm_flow->action_handle = FM_INVALID_HANDLE;
2586 fm_flow->fet = NULL;
2588 enic_fm_flow_free(fm, flow);
2590 while (!LIST_EMPTY(&internal)) {
2591 flow = LIST_FIRST(&internal);
2592 LIST_REMOVE(flow, next);
2593 LIST_INSERT_HEAD(&enic->flows, flow, next);
2600 enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle)
2605 args[0] = FM_MATCH_TABLE_FREE;
2607 rc = flowman_cmd(fm, args, 2);
2609 ENICPMD_LOG(ERR, "cannot free table: rc=%d handle=0x%" PRIx64,
2615 enic_fm_tcam_tbl_alloc(struct enic_flowman *fm, uint32_t direction,
2616 uint32_t max_entries, uint64_t *handle)
2618 struct fm_tcam_match_table *tcam_tbl;
2622 ENICPMD_FUNC_TRACE();
2623 tcam_tbl = &fm->cmd.va->fm_tcam_match_table;
2624 tcam_tbl->ftt_direction = direction;
2625 tcam_tbl->ftt_stage = FM_STAGE_LAST;
2626 tcam_tbl->ftt_max_entries = max_entries;
2627 args[0] = FM_TCAM_TABLE_ALLOC;
2628 args[1] = fm->cmd.pa;
2629 rc = flowman_cmd(fm, args, 2);
2631 ENICPMD_LOG(ERR, "cannot alloc %s TCAM table: rc=%d",
2632 (direction == FM_INGRESS) ? "IG" : "EG", rc);
2636 ENICPMD_LOG(DEBUG, "%s TCAM table allocated, handle=0x%" PRIx64,
2637 (direction == FM_INGRESS) ? "IG" : "EG", *handle);
2642 enic_fm_init_counters(struct enic_flowman *fm)
2644 ENICPMD_FUNC_TRACE();
2645 SLIST_INIT(&fm->counters);
2646 return enic_fm_more_counters(fm);
2650 enic_fm_free_all_counters(struct enic_flowman *fm)
2655 args[0] = FM_COUNTER_BRK;
2657 rc = flowman_cmd(fm, args, 2);
2659 ENICPMD_LOG(ERR, "cannot free counters: rc=%d", rc);
2660 rte_free(fm->counter_stack);
2664 enic_fm_alloc_tcam_tables(struct enic_flowman *fm)
2668 ENICPMD_FUNC_TRACE();
2669 rc = enic_fm_tcam_tbl_alloc(fm, FM_INGRESS, FM_MAX_TCAM_TABLE_SIZE,
2673 rc = enic_fm_tcam_tbl_alloc(fm, FM_EGRESS, FM_MAX_TCAM_TABLE_SIZE,
2679 enic_fm_free_tcam_tables(struct enic_flowman *fm)
2681 ENICPMD_FUNC_TRACE();
2682 if (fm->ig_tcam_hndl) {
2683 ENICPMD_LOG(DEBUG, "free IG TCAM table handle=0x%" PRIx64,
2685 enic_fm_tbl_free(fm, fm->ig_tcam_hndl);
2686 fm->ig_tcam_hndl = FM_INVALID_HANDLE;
2688 if (fm->eg_tcam_hndl) {
2689 ENICPMD_LOG(DEBUG, "free EG TCAM table handle=0x%" PRIx64,
2691 enic_fm_tbl_free(fm, fm->eg_tcam_hndl);
2692 fm->eg_tcam_hndl = FM_INVALID_HANDLE;
2697 enic_fm_init(struct enic *enic)
2699 const struct rte_pci_addr *addr;
2700 struct enic_flowman *fm;
2701 uint8_t name[RTE_MEMZONE_NAMESIZE];
2704 if (enic->flow_filter_mode != FILTER_FLOWMAN)
2706 ENICPMD_FUNC_TRACE();
2707 /* Get vnic handle and save for port-id action */
2708 if (enic_is_vf_rep(enic))
2709 addr = &VF_ENIC_TO_VF_REP(enic)->bdf;
2711 addr = &RTE_ETH_DEV_TO_PCI(enic->rte_dev)->addr;
2712 rc = enic_fm_find_vnic(enic, addr, &enic->fm_vnic_handle);
2714 ENICPMD_LOG(ERR, "cannot find vnic handle for %x:%x:%x",
2715 addr->bus, addr->devid, addr->function);
2718 /* Save UIF for egport action */
2719 enic->fm_vnic_uif = vnic_dev_uif(enic->vdev);
2720 ENICPMD_LOG(DEBUG, "uif %u", enic->fm_vnic_uif);
2721 /* Nothing else to do for representor. It will share the PF flowman */
2722 if (enic_is_vf_rep(enic))
2724 fm = calloc(1, sizeof(*fm));
2726 ENICPMD_LOG(ERR, "cannot alloc flowman struct");
2729 fm->owner_enic = enic;
2730 rte_spinlock_init(&fm->lock);
2731 TAILQ_INIT(&fm->fet_list);
2732 TAILQ_INIT(&fm->jump_list);
2733 /* Allocate host memory for flowman commands */
2734 snprintf((char *)name, sizeof(name), "fm-cmd-%s", enic->bdf_name);
2735 fm->cmd.va = enic_alloc_consistent(enic,
2736 sizeof(union enic_flowman_cmd_mem), &fm->cmd.pa, name);
2738 ENICPMD_LOG(ERR, "cannot allocate flowman command memory");
2742 /* Allocate TCAM tables upfront as they are the main tables */
2743 rc = enic_fm_alloc_tcam_tables(fm);
2745 ENICPMD_LOG(ERR, "cannot alloc TCAM tables");
2748 /* Then a number of counters */
2749 rc = enic_fm_init_counters(fm);
2751 ENICPMD_LOG(ERR, "cannot alloc counters");
2755 * One default exact match table for each direction. We hold onto
2758 rc = enic_fet_alloc(fm, 1, NULL, 128, &fm->default_ig_fet);
2760 ENICPMD_LOG(ERR, "cannot alloc default IG exact match table");
2761 goto error_counters;
2763 fm->default_ig_fet->ref = 1;
2764 rc = enic_fet_alloc(fm, 0, NULL, 128, &fm->default_eg_fet);
2766 ENICPMD_LOG(ERR, "cannot alloc default EG exact match table");
2769 fm->default_eg_fet->ref = 1;
2770 fm->vf_rep_tag = FM_VF_REP_TAG;
2775 enic_fet_free(fm, fm->default_ig_fet);
2777 enic_fm_free_all_counters(fm);
2779 enic_fm_free_tcam_tables(fm);
2781 enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2782 fm->cmd.va, fm->cmd.pa);
2789 enic_fm_destroy(struct enic *enic)
2791 struct enic_flowman *fm;
2792 struct enic_fm_fet *fet;
2794 ENICPMD_FUNC_TRACE();
2795 if (enic_is_vf_rep(enic)) {
2796 delete_rep_flows(enic);
2799 if (enic->fm == NULL)
2802 enic_fet_free(fm, fm->default_eg_fet);
2803 enic_fet_free(fm, fm->default_ig_fet);
2804 /* Free all exact match tables still open */
2805 while (!TAILQ_EMPTY(&fm->fet_list)) {
2806 fet = TAILQ_FIRST(&fm->fet_list);
2807 enic_fet_free(fm, fet);
2809 enic_fm_free_tcam_tables(fm);
2810 enic_fm_free_all_counters(fm);
2811 enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2812 fm->cmd.va, fm->cmd.pa);
2819 enic_fm_allocate_switch_domain(struct enic *pf)
2821 const struct rte_pci_addr *cur_a, *prev_a;
2822 struct rte_eth_dev *dev;
2823 struct enic *cur, *prev;
2829 ENICPMD_FUNC_TRACE();
2830 if (enic_is_vf_rep(pf))
2833 cur_a = &RTE_ETH_DEV_TO_PCI(cur->rte_dev)->addr;
2834 /* Go through ports and find another PF that is on the same adapter */
2835 RTE_ETH_FOREACH_DEV(pid) {
2836 dev = &rte_eth_devices[pid];
2837 if (!dev_is_enic(dev))
2839 if (dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)
2841 if (dev == cur->rte_dev)
2843 /* dev is another PF. Is it on the same adapter? */
2844 prev = pmd_priv(dev);
2845 prev_a = &RTE_ETH_DEV_TO_PCI(dev)->addr;
2846 if (!enic_fm_find_vnic(cur, prev_a, &vnic_h)) {
2847 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",
2848 cur->rte_dev->data->port_id,
2849 cur_a->bus, cur_a->devid, cur_a->function,
2851 prev_a->bus, prev_a->devid, prev_a->function,
2852 prev->switch_domain_id);
2853 cur->switch_domain_id = prev->switch_domain_id;
2857 ret = rte_eth_switch_domain_alloc(&domain_id);
2859 ENICPMD_LOG(WARNING, "failed to allocate switch domain for device %d",
2862 cur->switch_domain_id = domain_id;
2863 ENICPMD_LOG(DEBUG, "Port %u (PF BDF %x:%x:%x) is the 1st PF on the VIC. Allocated switch domain id %u",
2864 cur->rte_dev->data->port_id,
2865 cur_a->bus, cur_a->devid, cur_a->function,
2870 const struct rte_flow_ops enic_fm_flow_ops = {
2871 .validate = enic_fm_flow_validate,
2872 .create = enic_fm_flow_create,
2873 .destroy = enic_fm_flow_destroy,
2874 .flush = enic_fm_flow_flush,
2875 .query = enic_fm_flow_query,
2878 /* Add a high priority flow that loops representor packets to VF */
2880 enic_fm_add_rep2vf_flow(struct enic_vf_representor *vf)
2882 struct fm_tcam_match_entry *fm_tcam_entry;
2883 struct rte_flow *flow0, *flow1;
2884 struct fm_action *fm_action;
2885 struct rte_flow_error error;
2886 struct rte_flow_attr attrs;
2887 struct fm_action_op fm_op;
2888 struct enic_flowman *fm;
2894 tag = fm->vf_rep_tag;
2895 enic_fm_open_scratch(fm);
2896 fm_tcam_entry = &fm->tcam_entry;
2897 fm_action = &fm->action;
2898 /* Egress rule: match WQ ID and tag+hairpin */
2899 fm_tcam_entry->ftm_data.fk_wq_id = vf->pf_wq_idx;
2900 fm_tcam_entry->ftm_mask.fk_wq_id = 0xffff;
2901 fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
2902 memset(&fm_op, 0, sizeof(fm_op));
2903 fm_op.fa_op = FMOP_TAG;
2904 fm_op.tag.tag = tag;
2905 enic_fm_append_action_op(fm, &fm_op, &error);
2906 memset(&fm_op, 0, sizeof(fm_op));
2907 fm_op.fa_op = FMOP_EG_HAIRPIN;
2908 enic_fm_append_action_op(fm, &fm_op, &error);
2909 memset(&fm_op, 0, sizeof(fm_op));
2910 fm_op.fa_op = FMOP_END;
2911 enic_fm_append_action_op(fm, &fm_op, &error);
2915 attrs.priority = FM_HIGHEST_PRIORITY;
2916 flow0 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2918 enic_fm_close_scratch(fm);
2919 if (flow0 == NULL) {
2920 ENICPMD_LOG(ERR, "Cannot create flow 0 for representor->VF");
2923 LIST_INSERT_HEAD(&pf->flows, flow0, next);
2924 /* Make this flow internal, so the user app cannot delete it */
2925 flow0->internal = 1;
2926 ENICPMD_LOG(DEBUG, "representor->VF %d flow created: wq %d -> tag %d hairpin",
2927 vf->vf_id, vf->pf_wq_idx, tag);
2929 /* Ingress: steer hairpinned to VF RQ 0 */
2930 enic_fm_open_scratch(fm);
2931 fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
2932 fm_tcam_entry->ftm_data.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
2933 fm_tcam_entry->ftm_mask.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
2934 fm_tcam_entry->ftm_data.fk_packet_tag = tag;
2935 fm_tcam_entry->ftm_mask.fk_packet_tag = 0xff;
2936 memset(&fm_op, 0, sizeof(fm_op));
2937 fm_op.fa_op = FMOP_RQ_STEER;
2938 fm_op.rq_steer.rq_index = 0;
2939 fm_op.rq_steer.vnic_handle = vf->enic.fm_vnic_handle;
2940 enic_fm_append_action_op(fm, &fm_op, &error);
2941 memset(&fm_op, 0, sizeof(fm_op));
2942 fm_op.fa_op = FMOP_END;
2943 enic_fm_append_action_op(fm, &fm_op, &error);
2947 attrs.priority = FM_HIGHEST_PRIORITY;
2948 flow1 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2950 enic_fm_close_scratch(fm);
2951 if (flow1 == NULL) {
2952 ENICPMD_LOG(ERR, "Cannot create flow 1 for representor->VF");
2953 enic_fm_flow_destroy(pf->rte_dev, flow0, &error);
2956 LIST_INSERT_HEAD(&pf->flows, flow1, next);
2957 flow1->internal = 1;
2958 ENICPMD_LOG(DEBUG, "representor->VF %d flow created: tag %d hairpinned -> VF RQ %d",
2959 vf->vf_id, tag, fm_op.rq_steer.rq_index);
2960 vf->rep2vf_flow[0] = flow0;
2961 vf->rep2vf_flow[1] = flow1;
2962 /* Done with this tag, use a different one next time */
2968 * Add a low priority flow that matches all packets from VF and loops them
2969 * back to the representor.
2972 enic_fm_add_vf2rep_flow(struct enic_vf_representor *vf)
2974 struct fm_tcam_match_entry *fm_tcam_entry;
2975 struct rte_flow *flow0, *flow1;
2976 struct fm_action *fm_action;
2977 struct rte_flow_error error;
2978 struct rte_flow_attr attrs;
2979 struct fm_action_op fm_op;
2980 struct enic_flowman *fm;
2986 tag = fm->vf_rep_tag;
2987 enic_fm_open_scratch(fm);
2988 fm_tcam_entry = &fm->tcam_entry;
2989 fm_action = &fm->action;
2990 /* Egress rule: match-any and tag+hairpin */
2991 fm_tcam_entry->ftm_data.fk_wq_id = 0;
2992 fm_tcam_entry->ftm_mask.fk_wq_id = 0xffff;
2993 fm_tcam_entry->ftm_data.fk_wq_vnic = vf->enic.fm_vnic_handle;
2994 fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
2995 memset(&fm_op, 0, sizeof(fm_op));
2996 fm_op.fa_op = FMOP_TAG;
2997 fm_op.tag.tag = tag;
2998 enic_fm_append_action_op(fm, &fm_op, &error);
2999 memset(&fm_op, 0, sizeof(fm_op));
3000 fm_op.fa_op = FMOP_EG_HAIRPIN;
3001 enic_fm_append_action_op(fm, &fm_op, &error);
3002 memset(&fm_op, 0, sizeof(fm_op));
3003 fm_op.fa_op = FMOP_END;
3004 enic_fm_append_action_op(fm, &fm_op, &error);
3008 attrs.priority = FM_LOWEST_PRIORITY;
3009 flow0 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
3011 enic_fm_close_scratch(fm);
3012 if (flow0 == NULL) {
3013 ENICPMD_LOG(ERR, "Cannot create flow 0 for VF->representor");
3016 LIST_INSERT_HEAD(&pf->flows, flow0, next);
3017 /* Make this flow internal, so the user app cannot delete it */
3018 flow0->internal = 1;
3019 ENICPMD_LOG(DEBUG, "VF %d->representor flow created: wq %d (low prio) -> tag %d hairpin",
3020 vf->vf_id, fm_tcam_entry->ftm_data.fk_wq_id, tag);
3022 /* Ingress: steer hairpinned to VF rep RQ */
3023 enic_fm_open_scratch(fm);
3024 fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
3025 fm_tcam_entry->ftm_data.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
3026 fm_tcam_entry->ftm_mask.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
3027 fm_tcam_entry->ftm_data.fk_packet_tag = tag;
3028 fm_tcam_entry->ftm_mask.fk_packet_tag = 0xff;
3029 memset(&fm_op, 0, sizeof(fm_op));
3030 fm_op.fa_op = FMOP_RQ_STEER;
3031 fm_op.rq_steer.rq_index = vf->pf_rq_sop_idx;
3032 fm_op.rq_steer.vnic_handle = pf->fm_vnic_handle;
3033 enic_fm_append_action_op(fm, &fm_op, &error);
3034 memset(&fm_op, 0, sizeof(fm_op));
3035 fm_op.fa_op = FMOP_END;
3036 enic_fm_append_action_op(fm, &fm_op, &error);
3040 attrs.priority = FM_HIGHEST_PRIORITY;
3041 flow1 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
3043 enic_fm_close_scratch(fm);
3044 if (flow1 == NULL) {
3045 ENICPMD_LOG(ERR, "Cannot create flow 1 for VF->representor");
3046 enic_fm_flow_destroy(pf->rte_dev, flow0, &error);
3049 LIST_INSERT_HEAD(&pf->flows, flow1, next);
3050 flow1->internal = 1;
3051 ENICPMD_LOG(DEBUG, "VF %d->representor flow created: tag %d hairpinned -> PF RQ %d",
3052 vf->vf_id, tag, vf->pf_rq_sop_idx);
3053 vf->vf2rep_flow[0] = flow0;
3054 vf->vf2rep_flow[1] = flow1;
3055 /* Done with this tag, use a different one next time */
3060 /* Destroy representor flows created by enic_fm_add_{rep2vf,vf2rep}_flow */
3062 delete_rep_flows(struct enic *enic)
3064 struct enic_vf_representor *vf;
3065 struct rte_flow_error error;
3066 struct rte_eth_dev *dev;
3069 RTE_ASSERT(enic_is_vf_rep(enic));
3070 vf = VF_ENIC_TO_VF_REP(enic);
3071 dev = vf->pf->rte_dev;
3072 for (i = 0; i < ARRAY_SIZE(vf->vf2rep_flow); i++) {
3073 if (vf->vf2rep_flow[i])
3074 enic_fm_flow_destroy(dev, vf->vf2rep_flow[i], &error);
3076 for (i = 0; i < ARRAY_SIZE(vf->rep2vf_flow); i++) {
3077 if (vf->rep2vf_flow[i])
3078 enic_fm_flow_destroy(dev, vf->rep2vf_flow[i], &error);
3082 static struct enic_flowman *
3083 begin_fm(struct enic *enic)
3085 struct enic_vf_representor *vf;
3086 struct enic_flowman *fm;
3088 /* Representor uses PF flowman */
3089 if (enic_is_vf_rep(enic)) {
3090 vf = VF_ENIC_TO_VF_REP(enic);
3095 /* Save the API caller and lock if representors exist */
3097 if (fm->owner_enic->switchdev_mode)
3098 rte_spinlock_lock(&fm->lock);
3099 fm->user_enic = enic;
3105 end_fm(struct enic_flowman *fm)
3107 fm->user_enic = NULL;
3108 if (fm->owner_enic->switchdev_mode)
3109 rte_spinlock_unlock(&fm->lock);