xref: /f-stack/dpdk/drivers/net/enic/enic_fm_flow.c (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2008-2019 Cisco Systems, Inc.  All rights reserved.
3  */
4 
5 #include <errno.h>
6 #include <stdint.h>
7 #include <rte_log.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_flow_driver.h>
10 #include <rte_ether.h>
11 #include <rte_hash.h>
12 #include <rte_jhash.h>
13 #include <rte_ip.h>
14 #include <rte_udp.h>
15 #include <rte_memzone.h>
16 
17 #include "enic_compat.h"
18 #include "enic.h"
19 #include "vnic_dev.h"
20 #include "vnic_nic.h"
21 
22 #define IP_DEFTTL  64   /* from RFC 1340. */
23 #define IP6_VTC_FLOW 0x60000000
24 
25 /* Highest Item type supported by Flowman */
26 #define FM_MAX_ITEM_TYPE RTE_FLOW_ITEM_TYPE_VXLAN
27 
28 /* Up to 1024 TCAM entries */
29 #define FM_MAX_TCAM_TABLE_SIZE 1024
30 
31 /* Up to 4096 entries per exact match table */
32 #define FM_MAX_EXACT_TABLE_SIZE 4096
33 
34 /* Number of counters to increase on for each increment */
35 #define FM_COUNTERS_EXPAND  100
36 
37 #define FM_INVALID_HANDLE 0
38 
39 /* Low priority used for implicit VF -> representor flow */
40 #define FM_LOWEST_PRIORITY 100000
41 
42 /* High priority used for implicit representor -> VF flow */
43 #define FM_HIGHEST_PRIORITY 0
44 
45 /* Tag used for implicit VF <-> representor flows */
46 #define FM_VF_REP_TAG 1
47 
48 /* Max number of actions supported by VIC is 2K. Make hash table double that. */
49 #define FM_MAX_ACTION_TABLE_SIZE 4096
50 
51 /*
52  * Flow exact match tables (FET) in the VIC and rte_flow groups.
53  * Use a simple scheme to map groups to tables.
54  * Group 0 uses the single TCAM tables, one for each direction.
55  * Group 1, 2, ... uses its own exact match table.
56  *
57  * The TCAM tables are allocated upfront during init.
58  *
59  * Exact match tables are allocated on demand. 3 paths that lead allocations.
60  *
61  * 1. Add a flow that jumps from group 0 to group N.
62  *
63  * If N does not exist, we allocate an exact match table for it, using
64  * a dummy key. A key is required for the table.
65  *
66  * 2. Add a flow that uses group N.
67  *
68  * If N does not exist, we allocate an exact match table for it, using
69  * the flow's key. Subsequent flows to the same group all should have
70  * the same key.
71  *
72  * Without a jump flow to N, N is not reachable in hardware. No packets
73  * reach N and match.
74  *
75  * 3. Add a flow to an empty group N.
76  *
77  * N has been created via (1) and the dummy key. We free that table, allocate
78  * a new table using the new flow's key. Also re-do the existing jump flow to
79  * point to the new table.
80  */
81 #define FM_TCAM_RTE_GROUP 0
82 
83 struct enic_fm_fet {
84 	TAILQ_ENTRY(enic_fm_fet) list;
85 	uint32_t group; /* rte_flow group ID */
86 	uint64_t handle; /* Exact match table handle from flowman */
87 	uint8_t ingress;
88 	uint8_t default_key;
89 	int ref; /* Reference count via get/put */
90 	struct fm_key_template key; /* Key associated with the table */
91 };
92 
93 struct enic_fm_counter {
94 	SLIST_ENTRY(enic_fm_counter) next;
95 	uint32_t handle;
96 };
97 
98 struct enic_fm_action {
99 	int ref;
100 	uint64_t handle;
101 	struct fm_action key;
102 };
103 
104 /* rte_flow.fm */
105 struct enic_fm_flow {
106 	bool counter_valid;
107 	uint64_t entry_handle;
108 	struct enic_fm_action  *action;
109 	struct enic_fm_counter *counter;
110 	struct enic_fm_fet *fet;
111 	/* Auto-added steer action for hairpin flows (e.g. vnic->vnic) */
112 	struct enic_fm_flow *hairpin_steer_flow;
113 };
114 
115 struct enic_fm_jump_flow {
116 	TAILQ_ENTRY(enic_fm_jump_flow) list;
117 	struct rte_flow *flow;
118 	uint32_t group;
119 	struct fm_tcam_match_entry match;
120 	struct fm_action action;
121 };
122 
123 /*
124  * Flowman uses host memory for commands. This structure is allocated
125  * in DMA-able memory.
126  */
127 union enic_flowman_cmd_mem {
128 	struct fm_tcam_match_table fm_tcam_match_table;
129 	struct fm_exact_match_table fm_exact_match_table;
130 	struct fm_tcam_match_entry fm_tcam_match_entry;
131 	struct fm_exact_match_entry fm_exact_match_entry;
132 	struct fm_action fm_action;
133 };
134 
135 /*
136  * PF has a flowman instance, and VF representors share it with PF.
137  * PF allocates this structure and owns it. VF representors borrow
138  * the PF's structure during API calls (e.g. create, query).
139  */
140 struct enic_flowman {
141 	struct enic *owner_enic; /* PF */
142 	struct enic *user_enic;  /* API caller (PF or representor) */
143 	/*
144 	 * Representors and PF share the same underlying flowman.
145 	 * Lock API calls to serialize accesses from them. Only used
146 	 * when VF representors are present.
147 	 */
148 	rte_spinlock_t lock;
149 	/* Command buffer */
150 	struct {
151 		union enic_flowman_cmd_mem *va;
152 		dma_addr_t pa;
153 	} cmd;
154 	/* TCAM tables allocated upfront, used for group 0 */
155 	uint64_t ig_tcam_hndl;
156 	uint64_t eg_tcam_hndl;
157 	/* Counters */
158 	SLIST_HEAD(enic_free_counters, enic_fm_counter) counters;
159 	void *counter_stack;
160 	uint32_t counters_alloced;
161 	/* Exact match tables for groups != 0, dynamically allocated */
162 	TAILQ_HEAD(fet_list, enic_fm_fet) fet_list;
163 	/*
164 	 * Default exact match tables used for jump actions to
165 	 * non-existent groups.
166 	 */
167 	struct enic_fm_fet *default_eg_fet;
168 	struct enic_fm_fet *default_ig_fet;
169 	/* hash table for Action reuse */
170 	struct rte_hash *action_hash;
171 	/* Flows that jump to the default table above */
172 	TAILQ_HEAD(jump_flow_list, enic_fm_jump_flow) jump_list;
173 	/*
174 	 * Scratch data used during each invocation of flow_create
175 	 * and flow_validate.
176 	 */
177 	struct enic_fm_fet *fet;
178 	struct fm_tcam_match_entry tcam_entry;
179 	struct fm_action action;
180 	struct fm_action action_tmp; /* enic_fm_reorder_action_op */
181 	int action_op_count;
182 	/* Tags used for representor flows */
183 	uint8_t vf_rep_tag;
184 	/* For auto-added steer action for hairpin */
185 	int need_hairpin_steer;
186 	uint64_t hairpin_steer_vnic_h;
187 };
188 
189 static int enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle);
190 /*
191  * API functions (create, destroy, validate, flush) call begin_fm()
192  * upon entering to save the caller enic (PF or VF representor) and
193  * lock. Upon exit, they call end_fm() to unlock.
194  */
195 static struct enic_flowman *begin_fm(struct enic *enic);
196 static void end_fm(struct enic_flowman *fm);
197 /* Delete internal flows created for representor paths */
198 static void delete_rep_flows(struct enic *enic);
199 
200 /*
201  * Common arguments passed to copy_item functions. Use this structure
202  * so we can easily add new arguments.
203  * item: Item specification.
204  * fm_tcam_entry: Flowman TCAM match entry.
205  * header_level: 0 for outer header, 1 for inner header.
206  */
207 struct copy_item_args {
208 	const struct rte_flow_item *item;
209 	struct fm_tcam_match_entry *fm_tcam_entry;
210 	uint8_t header_level;
211 };
212 
213 /* functions for copying items into flowman match */
214 typedef int (enic_copy_item_fn)(struct copy_item_args *arg);
215 
216 /* Info about how to copy items into flowman match */
217 struct enic_fm_items {
218 	/* Function for copying and validating an item. */
219 	enic_copy_item_fn * const copy_item;
220 	/* List of valid previous items. */
221 	const enum rte_flow_item_type * const prev_items;
222 	/*
223 	 * True if it's OK for this item to be the first item. For some NIC
224 	 * versions, it's invalid to start the stack above layer 3.
225 	 */
226 	const uint8_t valid_start_item;
227 };
228 
229 static enic_copy_item_fn enic_fm_copy_item_eth;
230 static enic_copy_item_fn enic_fm_copy_item_ipv4;
231 static enic_copy_item_fn enic_fm_copy_item_ipv6;
232 static enic_copy_item_fn enic_fm_copy_item_raw;
233 static enic_copy_item_fn enic_fm_copy_item_sctp;
234 static enic_copy_item_fn enic_fm_copy_item_tcp;
235 static enic_copy_item_fn enic_fm_copy_item_udp;
236 static enic_copy_item_fn enic_fm_copy_item_vlan;
237 static enic_copy_item_fn enic_fm_copy_item_vxlan;
238 
239 /* Ingress actions */
240 static const enum rte_flow_action_type enic_fm_supported_ig_actions[] = {
241 	RTE_FLOW_ACTION_TYPE_COUNT,
242 	RTE_FLOW_ACTION_TYPE_DROP,
243 	RTE_FLOW_ACTION_TYPE_FLAG,
244 	RTE_FLOW_ACTION_TYPE_JUMP,
245 	RTE_FLOW_ACTION_TYPE_MARK,
246 	RTE_FLOW_ACTION_TYPE_OF_POP_VLAN,
247 	RTE_FLOW_ACTION_TYPE_PORT_ID,
248 	RTE_FLOW_ACTION_TYPE_PASSTHRU,
249 	RTE_FLOW_ACTION_TYPE_QUEUE,
250 	RTE_FLOW_ACTION_TYPE_RSS,
251 	RTE_FLOW_ACTION_TYPE_VOID,
252 	RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
253 	RTE_FLOW_ACTION_TYPE_VXLAN_DECAP,
254 	RTE_FLOW_ACTION_TYPE_END, /* END must be the last entry */
255 };
256 
257 /* Egress actions */
258 static const enum rte_flow_action_type enic_fm_supported_eg_actions[] = {
259 	RTE_FLOW_ACTION_TYPE_COUNT,
260 	RTE_FLOW_ACTION_TYPE_DROP,
261 	RTE_FLOW_ACTION_TYPE_JUMP,
262 	RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN,
263 	RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP,
264 	RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID,
265 	RTE_FLOW_ACTION_TYPE_PORT_ID,
266 	RTE_FLOW_ACTION_TYPE_PASSTHRU,
267 	RTE_FLOW_ACTION_TYPE_VOID,
268 	RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
269 	RTE_FLOW_ACTION_TYPE_END,
270 };
271 
272 static const struct enic_fm_items enic_fm_items[] = {
273 	[RTE_FLOW_ITEM_TYPE_RAW] = {
274 		.copy_item = enic_fm_copy_item_raw,
275 		.valid_start_item = 0,
276 		.prev_items = (const enum rte_flow_item_type[]) {
277 			       RTE_FLOW_ITEM_TYPE_UDP,
278 			       RTE_FLOW_ITEM_TYPE_END,
279 		},
280 	},
281 	[RTE_FLOW_ITEM_TYPE_ETH] = {
282 		.copy_item = enic_fm_copy_item_eth,
283 		.valid_start_item = 1,
284 		.prev_items = (const enum rte_flow_item_type[]) {
285 			       RTE_FLOW_ITEM_TYPE_END,
286 		},
287 	},
288 	[RTE_FLOW_ITEM_TYPE_VLAN] = {
289 		.copy_item = enic_fm_copy_item_vlan,
290 		.valid_start_item = 1,
291 		.prev_items = (const enum rte_flow_item_type[]) {
292 			       RTE_FLOW_ITEM_TYPE_ETH,
293 			       RTE_FLOW_ITEM_TYPE_END,
294 		},
295 	},
296 	[RTE_FLOW_ITEM_TYPE_IPV4] = {
297 		.copy_item = enic_fm_copy_item_ipv4,
298 		.valid_start_item = 1,
299 		.prev_items = (const enum rte_flow_item_type[]) {
300 			       RTE_FLOW_ITEM_TYPE_ETH,
301 			       RTE_FLOW_ITEM_TYPE_VLAN,
302 			       RTE_FLOW_ITEM_TYPE_END,
303 		},
304 	},
305 	[RTE_FLOW_ITEM_TYPE_IPV6] = {
306 		.copy_item = enic_fm_copy_item_ipv6,
307 		.valid_start_item = 1,
308 		.prev_items = (const enum rte_flow_item_type[]) {
309 			       RTE_FLOW_ITEM_TYPE_ETH,
310 			       RTE_FLOW_ITEM_TYPE_VLAN,
311 			       RTE_FLOW_ITEM_TYPE_END,
312 		},
313 	},
314 	[RTE_FLOW_ITEM_TYPE_UDP] = {
315 		.copy_item = enic_fm_copy_item_udp,
316 		.valid_start_item = 1,
317 		.prev_items = (const enum rte_flow_item_type[]) {
318 			       RTE_FLOW_ITEM_TYPE_IPV4,
319 			       RTE_FLOW_ITEM_TYPE_IPV6,
320 			       RTE_FLOW_ITEM_TYPE_END,
321 		},
322 	},
323 	[RTE_FLOW_ITEM_TYPE_TCP] = {
324 		.copy_item = enic_fm_copy_item_tcp,
325 		.valid_start_item = 1,
326 		.prev_items = (const enum rte_flow_item_type[]) {
327 			       RTE_FLOW_ITEM_TYPE_IPV4,
328 			       RTE_FLOW_ITEM_TYPE_IPV6,
329 			       RTE_FLOW_ITEM_TYPE_END,
330 		},
331 	},
332 	[RTE_FLOW_ITEM_TYPE_SCTP] = {
333 		.copy_item = enic_fm_copy_item_sctp,
334 		.valid_start_item = 0,
335 		.prev_items = (const enum rte_flow_item_type[]) {
336 			       RTE_FLOW_ITEM_TYPE_IPV4,
337 			       RTE_FLOW_ITEM_TYPE_IPV6,
338 			       RTE_FLOW_ITEM_TYPE_END,
339 		},
340 	},
341 	[RTE_FLOW_ITEM_TYPE_VXLAN] = {
342 		.copy_item = enic_fm_copy_item_vxlan,
343 		.valid_start_item = 1,
344 		.prev_items = (const enum rte_flow_item_type[]) {
345 			       RTE_FLOW_ITEM_TYPE_UDP,
346 			       RTE_FLOW_ITEM_TYPE_END,
347 		},
348 	},
349 };
350 
351 static int
enic_fm_copy_item_eth(struct copy_item_args * arg)352 enic_fm_copy_item_eth(struct copy_item_args *arg)
353 {
354 	const struct rte_flow_item *item = arg->item;
355 	const struct rte_flow_item_eth *spec = item->spec;
356 	const struct rte_flow_item_eth *mask = item->mask;
357 	const uint8_t lvl = arg->header_level;
358 	struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
359 	struct fm_header_set *fm_data, *fm_mask;
360 
361 	ENICPMD_FUNC_TRACE();
362 	/* Match all if no spec */
363 	if (!spec)
364 		return 0;
365 	if (!mask)
366 		mask = &rte_flow_item_eth_mask;
367 	fm_data = &entry->ftm_data.fk_hdrset[lvl];
368 	fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
369 	fm_data->fk_header_select |= FKH_ETHER;
370 	fm_mask->fk_header_select |= FKH_ETHER;
371 	memcpy(&fm_data->l2.eth, spec, sizeof(struct rte_ether_hdr));
372 	memcpy(&fm_mask->l2.eth, mask, sizeof(struct rte_ether_hdr));
373 	return 0;
374 }
375 
376 static int
enic_fm_copy_item_vlan(struct copy_item_args * arg)377 enic_fm_copy_item_vlan(struct copy_item_args *arg)
378 {
379 	const struct rte_flow_item *item = arg->item;
380 	const struct rte_flow_item_vlan *spec = item->spec;
381 	const struct rte_flow_item_vlan *mask = item->mask;
382 	const uint8_t lvl = arg->header_level;
383 	struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
384 	struct fm_header_set *fm_data, *fm_mask;
385 	struct rte_ether_hdr *eth_mask;
386 	struct rte_ether_hdr *eth_val;
387 	uint32_t meta;
388 
389 	ENICPMD_FUNC_TRACE();
390 	fm_data = &entry->ftm_data.fk_hdrset[lvl];
391 	fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
392 	/* Outer and inner packet vlans need different flags */
393 	meta = FKM_VLAN_PRES;
394 	if (lvl > 0)
395 		meta = FKM_QTAG;
396 	fm_data->fk_metadata |= meta;
397 	fm_mask->fk_metadata |= meta;
398 
399 	/* Match all if no spec */
400 	if (!spec)
401 		return 0;
402 	if (!mask)
403 		mask = &rte_flow_item_vlan_mask;
404 
405 	eth_mask = (void *)&fm_mask->l2.eth;
406 	eth_val = (void *)&fm_data->l2.eth;
407 
408 	/*
409 	 * Outer TPID cannot be matched. If inner_type is 0, use what is
410 	 * in the eth header.
411 	 */
412 	if (eth_mask->ether_type && mask->inner_type)
413 		return -ENOTSUP;
414 
415 	/*
416 	 * When packet matching, the VIC always compares vlan-stripped
417 	 * L2, regardless of vlan stripping settings. So, the inner type
418 	 * from vlan becomes the ether type of the eth header.
419 	 */
420 	if (mask->inner_type) {
421 		eth_mask->ether_type = mask->inner_type;
422 		eth_val->ether_type = spec->inner_type;
423 	}
424 	fm_data->fk_header_select |= FKH_ETHER | FKH_QTAG;
425 	fm_mask->fk_header_select |= FKH_ETHER | FKH_QTAG;
426 	fm_data->fk_vlan = rte_be_to_cpu_16(spec->tci);
427 	fm_mask->fk_vlan = rte_be_to_cpu_16(mask->tci);
428 	return 0;
429 }
430 
431 static int
enic_fm_copy_item_ipv4(struct copy_item_args * arg)432 enic_fm_copy_item_ipv4(struct copy_item_args *arg)
433 {
434 	const struct rte_flow_item *item = arg->item;
435 	const struct rte_flow_item_ipv4 *spec = item->spec;
436 	const struct rte_flow_item_ipv4 *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;
440 
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_IPV4;
445 	fm_mask->fk_metadata |= FKM_IPV4;
446 
447 	if (!spec)
448 		return 0;
449 	if (!mask)
450 		mask = &rte_flow_item_ipv4_mask;
451 
452 	fm_data->fk_header_select |= FKH_IPV4;
453 	fm_mask->fk_header_select |= FKH_IPV4;
454 	memcpy(&fm_data->l3.ip4, spec, sizeof(*spec));
455 	memcpy(&fm_mask->l3.ip4, mask, sizeof(*mask));
456 	return 0;
457 }
458 
459 static int
enic_fm_copy_item_ipv6(struct copy_item_args * arg)460 enic_fm_copy_item_ipv6(struct copy_item_args *arg)
461 {
462 	const struct rte_flow_item *item = arg->item;
463 	const struct rte_flow_item_ipv6 *spec = item->spec;
464 	const struct rte_flow_item_ipv6 *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;
468 
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_IPV6;
473 	fm_mask->fk_metadata |= FKM_IPV6;
474 
475 	if (!spec)
476 		return 0;
477 	if (!mask)
478 		mask = &rte_flow_item_ipv6_mask;
479 
480 	fm_data->fk_header_select |= FKH_IPV6;
481 	fm_mask->fk_header_select |= FKH_IPV6;
482 	memcpy(&fm_data->l3.ip6, spec, sizeof(struct rte_ipv6_hdr));
483 	memcpy(&fm_mask->l3.ip6, mask, sizeof(struct rte_ipv6_hdr));
484 	return 0;
485 }
486 
487 static int
enic_fm_copy_item_udp(struct copy_item_args * arg)488 enic_fm_copy_item_udp(struct copy_item_args *arg)
489 {
490 	const struct rte_flow_item *item = arg->item;
491 	const struct rte_flow_item_udp *spec = item->spec;
492 	const struct rte_flow_item_udp *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 
497 	ENICPMD_FUNC_TRACE();
498 	fm_data = &entry->ftm_data.fk_hdrset[lvl];
499 	fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
500 	fm_data->fk_metadata |= FKM_UDP;
501 	fm_mask->fk_metadata |= FKM_UDP;
502 
503 	if (!spec)
504 		return 0;
505 	if (!mask)
506 		mask = &rte_flow_item_udp_mask;
507 
508 	fm_data->fk_header_select |= FKH_UDP;
509 	fm_mask->fk_header_select |= FKH_UDP;
510 	memcpy(&fm_data->l4.udp, spec, sizeof(*spec));
511 	memcpy(&fm_mask->l4.udp, mask, sizeof(*mask));
512 	return 0;
513 }
514 
515 static int
enic_fm_copy_item_tcp(struct copy_item_args * arg)516 enic_fm_copy_item_tcp(struct copy_item_args *arg)
517 {
518 	const struct rte_flow_item *item = arg->item;
519 	const struct rte_flow_item_tcp *spec = item->spec;
520 	const struct rte_flow_item_tcp *mask = item->mask;
521 	const uint8_t lvl = arg->header_level;
522 	struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
523 	struct fm_header_set *fm_data, *fm_mask;
524 
525 	ENICPMD_FUNC_TRACE();
526 	fm_data = &entry->ftm_data.fk_hdrset[lvl];
527 	fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
528 	fm_data->fk_metadata |= FKM_TCP;
529 	fm_mask->fk_metadata |= FKM_TCP;
530 
531 	if (!spec)
532 		return 0;
533 	if (!mask)
534 		mask = &rte_flow_item_tcp_mask;
535 
536 	fm_data->fk_header_select |= FKH_TCP;
537 	fm_mask->fk_header_select |= FKH_TCP;
538 	memcpy(&fm_data->l4.tcp, spec, sizeof(*spec));
539 	memcpy(&fm_mask->l4.tcp, mask, sizeof(*mask));
540 	return 0;
541 }
542 
543 static int
enic_fm_copy_item_sctp(struct copy_item_args * arg)544 enic_fm_copy_item_sctp(struct copy_item_args *arg)
545 {
546 	const struct rte_flow_item *item = arg->item;
547 	const struct rte_flow_item_sctp *spec = item->spec;
548 	const struct rte_flow_item_sctp *mask = item->mask;
549 	const uint8_t lvl = arg->header_level;
550 	struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
551 	struct fm_header_set *fm_data, *fm_mask;
552 	uint8_t *ip_proto_mask = NULL;
553 	uint8_t *ip_proto = NULL;
554 	uint32_t l3_fkh;
555 
556 	ENICPMD_FUNC_TRACE();
557 	fm_data = &entry->ftm_data.fk_hdrset[lvl];
558 	fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
559 	/*
560 	 * The NIC filter API has no flags for "match sctp", so explicitly
561 	 * set the protocol number in the IP pattern.
562 	 */
563 	if (fm_data->fk_metadata & FKM_IPV4) {
564 		struct rte_ipv4_hdr *ip;
565 		ip = (struct rte_ipv4_hdr *)&fm_mask->l3.ip4;
566 		ip_proto_mask = &ip->next_proto_id;
567 		ip = (struct rte_ipv4_hdr *)&fm_data->l3.ip4;
568 		ip_proto = &ip->next_proto_id;
569 		l3_fkh = FKH_IPV4;
570 	} else if (fm_data->fk_metadata & FKM_IPV6) {
571 		struct rte_ipv6_hdr *ip;
572 		ip = (struct rte_ipv6_hdr *)&fm_mask->l3.ip6;
573 		ip_proto_mask = &ip->proto;
574 		ip = (struct rte_ipv6_hdr *)&fm_data->l3.ip6;
575 		ip_proto = &ip->proto;
576 		l3_fkh = FKH_IPV6;
577 	} else {
578 		/* Need IPv4/IPv6 pattern first */
579 		return -EINVAL;
580 	}
581 	*ip_proto = IPPROTO_SCTP;
582 	*ip_proto_mask = 0xff;
583 	fm_data->fk_header_select |= l3_fkh;
584 	fm_mask->fk_header_select |= l3_fkh;
585 
586 	if (!spec)
587 		return 0;
588 	if (!mask)
589 		mask = &rte_flow_item_sctp_mask;
590 
591 	fm_data->fk_header_select |= FKH_L4RAW;
592 	fm_mask->fk_header_select |= FKH_L4RAW;
593 	memcpy(fm_data->l4.rawdata, spec, sizeof(*spec));
594 	memcpy(fm_mask->l4.rawdata, mask, sizeof(*mask));
595 	return 0;
596 }
597 
598 static int
enic_fm_copy_item_vxlan(struct copy_item_args * arg)599 enic_fm_copy_item_vxlan(struct copy_item_args *arg)
600 {
601 	const struct rte_flow_item *item = arg->item;
602 	const struct rte_flow_item_vxlan *spec = item->spec;
603 	const struct rte_flow_item_vxlan *mask = item->mask;
604 	struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
605 	struct fm_header_set *fm_data, *fm_mask;
606 
607 	ENICPMD_FUNC_TRACE();
608 	/* Only 2 header levels (outer and inner) allowed */
609 	if (arg->header_level > 0)
610 		return -EINVAL;
611 
612 	fm_data = &entry->ftm_data.fk_hdrset[0];
613 	fm_mask = &entry->ftm_mask.fk_hdrset[0];
614 	fm_data->fk_metadata |= FKM_VXLAN;
615 	fm_mask->fk_metadata |= FKM_VXLAN;
616 	/* items from here on out are inner header items */
617 	arg->header_level = 1;
618 
619 	/* Match all if no spec */
620 	if (!spec)
621 		return 0;
622 	if (!mask)
623 		mask = &rte_flow_item_vxlan_mask;
624 
625 	fm_data->fk_header_select |= FKH_VXLAN;
626 	fm_mask->fk_header_select |= FKH_VXLAN;
627 	memcpy(&fm_data->vxlan, spec, sizeof(*spec));
628 	memcpy(&fm_mask->vxlan, mask, sizeof(*mask));
629 	return 0;
630 }
631 
632 /*
633  * Currently, raw pattern match is very limited. It is intended for matching
634  * UDP tunnel header (e.g. vxlan or geneve).
635  */
636 static int
enic_fm_copy_item_raw(struct copy_item_args * arg)637 enic_fm_copy_item_raw(struct copy_item_args *arg)
638 {
639 	const struct rte_flow_item *item = arg->item;
640 	const struct rte_flow_item_raw *spec = item->spec;
641 	const struct rte_flow_item_raw *mask = item->mask;
642 	const uint8_t lvl = arg->header_level;
643 	struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
644 	struct fm_header_set *fm_data, *fm_mask;
645 
646 	ENICPMD_FUNC_TRACE();
647 	/* Cannot be used for inner packet */
648 	if (lvl > 0)
649 		return -EINVAL;
650 	/* Need both spec and mask */
651 	if (!spec || !mask)
652 		return -EINVAL;
653 	/* Only supports relative with offset 0 */
654 	if (!spec->relative || spec->offset != 0 || spec->search ||
655 	    spec->limit)
656 		return -EINVAL;
657 	/* Need non-null pattern that fits within the NIC's filter pattern */
658 	if (spec->length == 0 ||
659 	    spec->length + sizeof(struct rte_udp_hdr) > FM_LAYER_SIZE ||
660 	    !spec->pattern || !mask->pattern)
661 		return -EINVAL;
662 	/*
663 	 * Mask fields, including length, are often set to zero. Assume that
664 	 * means "same as spec" to avoid breaking existing apps. If length
665 	 * is not zero, then it should be >= spec length.
666 	 *
667 	 * No more pattern follows this, so append to the L4 layer instead of
668 	 * L5 to work with both recent and older VICs.
669 	 */
670 	if (mask->length != 0 && mask->length < spec->length)
671 		return -EINVAL;
672 
673 	fm_data = &entry->ftm_data.fk_hdrset[lvl];
674 	fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
675 	fm_data->fk_header_select |= FKH_L4RAW;
676 	fm_mask->fk_header_select |= FKH_L4RAW;
677 	fm_data->fk_header_select &= ~FKH_UDP;
678 	fm_mask->fk_header_select &= ~FKH_UDP;
679 	memcpy(fm_data->l4.rawdata + sizeof(struct rte_udp_hdr),
680 	       spec->pattern, spec->length);
681 	memcpy(fm_mask->l4.rawdata + sizeof(struct rte_udp_hdr),
682 	       mask->pattern, spec->length);
683 	return 0;
684 }
685 
686 static int
flowman_cmd(struct enic_flowman * fm,uint64_t * args,int nargs)687 flowman_cmd(struct enic_flowman *fm, uint64_t *args, int nargs)
688 {
689 	return vnic_dev_flowman_cmd(fm->owner_enic->vdev, args, nargs);
690 }
691 
692 static int
enic_fet_alloc(struct enic_flowman * fm,uint8_t ingress,struct fm_key_template * key,int entries,struct enic_fm_fet ** fet_out)693 enic_fet_alloc(struct enic_flowman *fm, uint8_t ingress,
694 	       struct fm_key_template *key, int entries,
695 	       struct enic_fm_fet **fet_out)
696 {
697 	struct fm_exact_match_table *cmd;
698 	struct fm_header_set *hdr;
699 	struct enic_fm_fet *fet;
700 	uint64_t args[3];
701 	int ret;
702 
703 	ENICPMD_FUNC_TRACE();
704 	fet = calloc(1, sizeof(struct enic_fm_fet));
705 	if (fet == NULL)
706 		return -ENOMEM;
707 	cmd = &fm->cmd.va->fm_exact_match_table;
708 	memset(cmd, 0, sizeof(*cmd));
709 	cmd->fet_direction = ingress ? FM_INGRESS : FM_EGRESS;
710 	cmd->fet_stage = FM_STAGE_LAST;
711 	cmd->fet_max_entries = entries ? entries : FM_MAX_EXACT_TABLE_SIZE;
712 	if (key == NULL) {
713 		hdr = &cmd->fet_key.fk_hdrset[0];
714 		memset(hdr, 0, sizeof(*hdr));
715 		hdr->fk_header_select = FKH_IPV4 | FKH_UDP;
716 		hdr->l3.ip4.fk_saddr = 0xFFFFFFFF;
717 		hdr->l3.ip4.fk_daddr = 0xFFFFFFFF;
718 		hdr->l4.udp.fk_source = 0xFFFF;
719 		hdr->l4.udp.fk_dest = 0xFFFF;
720 		fet->default_key = 1;
721 	} else {
722 		memcpy(&cmd->fet_key, key, sizeof(*key));
723 		memcpy(&fet->key, key, sizeof(*key));
724 		fet->default_key = 0;
725 	}
726 	cmd->fet_key.fk_packet_tag = 1;
727 
728 	args[0] = FM_EXACT_TABLE_ALLOC;
729 	args[1] = fm->cmd.pa;
730 	ret = flowman_cmd(fm, args, 2);
731 	if (ret) {
732 		ENICPMD_LOG(ERR, "cannot alloc exact match table: rc=%d", ret);
733 		free(fet);
734 		return ret;
735 	}
736 	fet->handle = args[0];
737 	fet->ingress = ingress;
738 	ENICPMD_LOG(DEBUG, "allocated exact match table: handle=0x%" PRIx64,
739 		    fet->handle);
740 	*fet_out = fet;
741 	return 0;
742 }
743 
744 static void
enic_fet_free(struct enic_flowman * fm,struct enic_fm_fet * fet)745 enic_fet_free(struct enic_flowman *fm, struct enic_fm_fet *fet)
746 {
747 	ENICPMD_FUNC_TRACE();
748 	enic_fm_tbl_free(fm, fet->handle);
749 	if (!fet->default_key)
750 		TAILQ_REMOVE(&fm->fet_list, fet, list);
751 	free(fet);
752 }
753 
754 /*
755  * Get the exact match table for the given combination of
756  * <group, ingress, key>. Allocate one on the fly as necessary.
757  */
758 static int
enic_fet_get(struct enic_flowman * fm,uint32_t group,uint8_t ingress,struct fm_key_template * key,struct enic_fm_fet ** fet_out,struct rte_flow_error * error)759 enic_fet_get(struct enic_flowman *fm,
760 	     uint32_t group,
761 	     uint8_t ingress,
762 	     struct fm_key_template *key,
763 	     struct enic_fm_fet **fet_out,
764 	     struct rte_flow_error *error)
765 {
766 	struct enic_fm_fet *fet;
767 
768 	ENICPMD_FUNC_TRACE();
769 	/* See if we already have this table open */
770 	TAILQ_FOREACH(fet, &fm->fet_list, list) {
771 		if (fet->group == group && fet->ingress == ingress)
772 			break;
773 	}
774 	if (fet == NULL) {
775 		/* Jumping to a non-existing group? Use the default table */
776 		if (key == NULL) {
777 			fet = ingress ? fm->default_ig_fet : fm->default_eg_fet;
778 		} else if (enic_fet_alloc(fm, ingress, key, 0, &fet)) {
779 			return rte_flow_error_set(error, EINVAL,
780 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
781 				NULL, "enic: cannot get exact match table");
782 		}
783 		fet->group = group;
784 		/* Default table is never on the open table list */
785 		if (!fet->default_key)
786 			TAILQ_INSERT_HEAD(&fm->fet_list, fet, list);
787 	}
788 	fet->ref++;
789 	*fet_out = fet;
790 	ENICPMD_LOG(DEBUG, "fet_get: %s %s group=%u ref=%u",
791 		    fet->default_key ? "default" : "",
792 		    fet->ingress ? "ingress" : "egress",
793 		    fet->group, fet->ref);
794 	return 0;
795 }
796 
797 static void
enic_fet_put(struct enic_flowman * fm,struct enic_fm_fet * fet)798 enic_fet_put(struct enic_flowman *fm, struct enic_fm_fet *fet)
799 {
800 	ENICPMD_FUNC_TRACE();
801 	RTE_ASSERT(fet->ref > 0);
802 	fet->ref--;
803 	ENICPMD_LOG(DEBUG, "fet_put: %s %s group=%u ref=%u",
804 		    fet->default_key ? "default" : "",
805 		    fet->ingress ? "ingress" : "egress",
806 		    fet->group, fet->ref);
807 	if (fet->ref == 0)
808 		enic_fet_free(fm, fet);
809 }
810 
811 /* Return 1 if current item is valid on top of the previous one. */
812 static int
fm_item_stacking_valid(enum rte_flow_item_type prev_item,const struct enic_fm_items * item_info,uint8_t is_first_item)813 fm_item_stacking_valid(enum rte_flow_item_type prev_item,
814 		       const struct enic_fm_items *item_info,
815 		       uint8_t is_first_item)
816 {
817 	enum rte_flow_item_type const *allowed_items = item_info->prev_items;
818 
819 	ENICPMD_FUNC_TRACE();
820 	for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
821 		if (prev_item == *allowed_items)
822 			return 1;
823 	}
824 
825 	/* This is the first item in the stack. Check if that's cool */
826 	if (is_first_item && item_info->valid_start_item)
827 		return 1;
828 	return 0;
829 }
830 
831 /*
832  * Build the flow manager match entry structure from the provided pattern.
833  * The pattern is validated as the items are copied.
834  */
835 static int
enic_fm_copy_entry(struct enic_flowman * fm,const struct rte_flow_item pattern[],struct rte_flow_error * error)836 enic_fm_copy_entry(struct enic_flowman *fm,
837 		   const struct rte_flow_item pattern[],
838 		   struct rte_flow_error *error)
839 {
840 	const struct enic_fm_items *item_info;
841 	enum rte_flow_item_type prev_item;
842 	const struct rte_flow_item *item;
843 	struct copy_item_args args;
844 	uint8_t prev_header_level;
845 	uint8_t is_first_item;
846 	int ret;
847 
848 	ENICPMD_FUNC_TRACE();
849 	item = pattern;
850 	is_first_item = 1;
851 	prev_item = RTE_FLOW_ITEM_TYPE_END;
852 
853 	args.fm_tcam_entry = &fm->tcam_entry;
854 	args.header_level = 0;
855 	prev_header_level = 0;
856 	for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
857 		/*
858 		 * Get info about how to validate and copy the item. If NULL
859 		 * is returned the nic does not support the item.
860 		 */
861 		if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
862 			continue;
863 
864 		item_info = &enic_fm_items[item->type];
865 
866 		if (item->type > FM_MAX_ITEM_TYPE ||
867 		    item_info->copy_item == NULL) {
868 			return rte_flow_error_set(error, ENOTSUP,
869 				RTE_FLOW_ERROR_TYPE_ITEM,
870 				NULL, "enic: unsupported item");
871 		}
872 
873 		/* check to see if item stacking is valid */
874 		if (!fm_item_stacking_valid(prev_item, item_info,
875 					    is_first_item))
876 			goto stacking_error;
877 
878 		args.item = item;
879 		ret = item_info->copy_item(&args);
880 		if (ret)
881 			goto item_not_supported;
882 		/* Going from outer to inner? Treat it as a new packet start */
883 		if (prev_header_level != args.header_level) {
884 			prev_item = RTE_FLOW_ITEM_TYPE_END;
885 			is_first_item = 1;
886 		} else {
887 			prev_item = item->type;
888 			is_first_item = 0;
889 		}
890 		prev_header_level = args.header_level;
891 	}
892 	return 0;
893 
894 item_not_supported:
895 	return rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_ITEM,
896 				  NULL, "enic: unsupported item type");
897 
898 stacking_error:
899 	return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
900 				  item, "enic: unsupported item stack");
901 }
902 
903 static void
flow_item_skip_void(const struct rte_flow_item ** item)904 flow_item_skip_void(const struct rte_flow_item **item)
905 {
906 	for ( ; ; (*item)++)
907 		if ((*item)->type != RTE_FLOW_ITEM_TYPE_VOID)
908 			return;
909 }
910 
911 static void
append_template(void ** template,uint8_t * off,const void * data,int len)912 append_template(void **template, uint8_t *off, const void *data, int len)
913 {
914 	memcpy(*template, data, len);
915 	*template = (char *)*template + len;
916 	*off = *off + len;
917 }
918 
919 static int
enic_fm_append_action_op(struct enic_flowman * fm,struct fm_action_op * fm_op,struct rte_flow_error * error)920 enic_fm_append_action_op(struct enic_flowman *fm,
921 			 struct fm_action_op *fm_op,
922 			 struct rte_flow_error *error)
923 {
924 	int count;
925 
926 	count = fm->action_op_count;
927 	ENICPMD_LOG(DEBUG, "append action op: idx=%d op=%u",
928 		    count, fm_op->fa_op);
929 	if (count == FM_ACTION_OP_MAX) {
930 		return rte_flow_error_set(error, EINVAL,
931 			RTE_FLOW_ERROR_TYPE_ACTION, NULL,
932 			"too many action operations");
933 	}
934 	fm->action.fma_action_ops[count] = *fm_op;
935 	fm->action_op_count = count + 1;
936 	return 0;
937 }
938 
939 static struct fm_action_op *
find_prev_action_op(struct enic_flowman * fm,uint32_t opcode)940 find_prev_action_op(struct enic_flowman *fm, uint32_t opcode)
941 {
942 	struct fm_action_op *op;
943 	int i;
944 
945 	for (i = 0; i < fm->action_op_count; i++) {
946 		op = &fm->action.fma_action_ops[i];
947 		if (op->fa_op == opcode)
948 			return op;
949 	}
950 	return NULL;
951 }
952 
953 /* NIC requires that 1st steer appear before decap.
954  * Correct example: steer, decap, steer, steer, ...
955  */
956 static void
enic_fm_reorder_action_op(struct enic_flowman * fm)957 enic_fm_reorder_action_op(struct enic_flowman *fm)
958 {
959 	struct fm_action_op *op, *steer, *decap;
960 	struct fm_action_op tmp_op;
961 
962 	ENICPMD_FUNC_TRACE();
963 	/* Find 1st steer and decap */
964 	op = fm->action.fma_action_ops;
965 	steer = NULL;
966 	decap = NULL;
967 	while (op->fa_op != FMOP_END) {
968 		if (!decap && (op->fa_op == FMOP_DECAP_NOSTRIP ||
969 			       op->fa_op == FMOP_DECAP_STRIP))
970 			decap = op;
971 		else if (!steer && op->fa_op == FMOP_RQ_STEER)
972 			steer = op;
973 		op++;
974 	}
975 	/* If decap is before steer, swap */
976 	if (steer && decap && decap < steer) {
977 		op = fm->action.fma_action_ops;
978 		ENICPMD_LOG(DEBUG, "swap decap %ld <-> steer %ld",
979 			    (long)(decap - op), (long)(steer - op));
980 		tmp_op = *decap;
981 		*decap = *steer;
982 		*steer = tmp_op;
983 	}
984 }
985 
986 /* VXLAN decap is done via flowman compound action */
987 static int
enic_fm_copy_vxlan_decap(struct enic_flowman * fm,struct fm_tcam_match_entry * fmt,const struct rte_flow_action * action,struct rte_flow_error * error)988 enic_fm_copy_vxlan_decap(struct enic_flowman *fm,
989 			 struct fm_tcam_match_entry *fmt,
990 			 const struct rte_flow_action *action,
991 			 struct rte_flow_error *error)
992 {
993 	struct fm_header_set *fm_data;
994 	struct fm_action_op fm_op;
995 
996 	ENICPMD_FUNC_TRACE();
997 	fm_data = &fmt->ftm_data.fk_hdrset[0];
998 	if (!(fm_data->fk_metadata & FKM_VXLAN)) {
999 		return rte_flow_error_set(error, EINVAL,
1000 			RTE_FLOW_ERROR_TYPE_ACTION, action,
1001 			"vxlan-decap: vxlan must be in pattern");
1002 	}
1003 
1004 	memset(&fm_op, 0, sizeof(fm_op));
1005 	fm_op.fa_op = FMOP_DECAP_NOSTRIP;
1006 	return enic_fm_append_action_op(fm, &fm_op, error);
1007 }
1008 
1009 /* Generate a reasonable source port number */
1010 static uint16_t
gen_src_port(void)1011 gen_src_port(void)
1012 {
1013 	/* Min/max below are the default values in OVS-DPDK and Linux */
1014 	uint16_t p = rte_rand();
1015 	p = RTE_MAX(p, 32768);
1016 	p = RTE_MIN(p, 61000);
1017 	return rte_cpu_to_be_16(p);
1018 }
1019 
1020 /* VXLAN encap is done via flowman compound action */
1021 static int
enic_fm_copy_vxlan_encap(struct enic_flowman * fm,const struct rte_flow_item * item,struct rte_flow_error * error)1022 enic_fm_copy_vxlan_encap(struct enic_flowman *fm,
1023 			 const struct rte_flow_item *item,
1024 			 struct rte_flow_error *error)
1025 {
1026 	struct fm_action_op fm_op;
1027 	struct rte_ether_hdr *eth;
1028 	struct rte_udp_hdr *udp;
1029 	uint16_t *ethertype;
1030 	void *template;
1031 	uint8_t off;
1032 
1033 	ENICPMD_FUNC_TRACE();
1034 	memset(&fm_op, 0, sizeof(fm_op));
1035 	fm_op.fa_op = FMOP_ENCAP;
1036 	template = fm->action.fma_data;
1037 	off = 0;
1038 	/*
1039 	 * Copy flow items to the flowman template starting L2.
1040 	 * L2 must be ethernet.
1041 	 */
1042 	flow_item_skip_void(&item);
1043 	if (item->type != RTE_FLOW_ITEM_TYPE_ETH)
1044 		return rte_flow_error_set(error, EINVAL,
1045 			RTE_FLOW_ERROR_TYPE_ITEM, item,
1046 			"vxlan-encap: first item should be ethernet");
1047 	eth = (struct rte_ether_hdr *)template;
1048 	ethertype = &eth->ether_type;
1049 	append_template(&template, &off, item->spec,
1050 			sizeof(struct rte_ether_hdr));
1051 	item++;
1052 	flow_item_skip_void(&item);
1053 	/* Optional VLAN */
1054 	if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
1055 		const struct rte_flow_item_vlan *spec;
1056 
1057 		ENICPMD_LOG(DEBUG, "vxlan-encap: vlan");
1058 		spec = item->spec;
1059 		fm_op.encap.outer_vlan = rte_be_to_cpu_16(spec->tci);
1060 		item++;
1061 		flow_item_skip_void(&item);
1062 	}
1063 	/* L3 must be IPv4, IPv6 */
1064 	switch (item->type) {
1065 	case RTE_FLOW_ITEM_TYPE_IPV4:
1066 	{
1067 		struct rte_ipv4_hdr *ip4;
1068 
1069 		ENICPMD_LOG(DEBUG, "vxlan-encap: ipv4");
1070 		*ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
1071 		ip4 = (struct rte_ipv4_hdr *)template;
1072 		/*
1073 		 * Offset of IPv4 length field and its initial value
1074 		 * (IP + UDP + VXLAN) are specified in the action. The NIC
1075 		 * will add inner packet length.
1076 		 */
1077 		fm_op.encap.len1_offset = off +
1078 			offsetof(struct rte_ipv4_hdr, total_length);
1079 		fm_op.encap.len1_delta = sizeof(struct rte_ipv4_hdr) +
1080 			sizeof(struct rte_udp_hdr) +
1081 			sizeof(struct rte_vxlan_hdr);
1082 		append_template(&template, &off, item->spec,
1083 				sizeof(struct rte_ipv4_hdr));
1084 		ip4->version_ihl = RTE_IPV4_VHL_DEF;
1085 		if (ip4->time_to_live == 0)
1086 			ip4->time_to_live = IP_DEFTTL;
1087 		ip4->next_proto_id = IPPROTO_UDP;
1088 		break;
1089 	}
1090 	case RTE_FLOW_ITEM_TYPE_IPV6:
1091 	{
1092 		struct rte_ipv6_hdr *ip6;
1093 
1094 		ENICPMD_LOG(DEBUG, "vxlan-encap: ipv6");
1095 		*ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
1096 		ip6 = (struct rte_ipv6_hdr *)template;
1097 		fm_op.encap.len1_offset = off +
1098 			offsetof(struct rte_ipv6_hdr, payload_len);
1099 		fm_op.encap.len1_delta = sizeof(struct rte_udp_hdr) +
1100 			sizeof(struct rte_vxlan_hdr);
1101 		append_template(&template, &off, item->spec,
1102 				sizeof(struct rte_ipv6_hdr));
1103 		ip6->vtc_flow |= rte_cpu_to_be_32(IP6_VTC_FLOW);
1104 		if (ip6->hop_limits == 0)
1105 			ip6->hop_limits = IP_DEFTTL;
1106 		ip6->proto = IPPROTO_UDP;
1107 		break;
1108 	}
1109 	default:
1110 		return rte_flow_error_set(error,
1111 			EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1112 			"vxlan-encap: L3 must be IPv4/IPv6");
1113 	}
1114 	item++;
1115 	flow_item_skip_void(&item);
1116 
1117 	/* L4 is UDP */
1118 	if (item->type != RTE_FLOW_ITEM_TYPE_UDP)
1119 		return rte_flow_error_set(error, EINVAL,
1120 			RTE_FLOW_ERROR_TYPE_ITEM, item,
1121 			"vxlan-encap: UDP must follow IPv4/IPv6");
1122 	/* UDP length = UDP + VXLAN. NIC will add inner packet length. */
1123 	fm_op.encap.len2_offset =
1124 		off + offsetof(struct rte_udp_hdr, dgram_len);
1125 	fm_op.encap.len2_delta =
1126 		sizeof(struct rte_udp_hdr) + sizeof(struct rte_vxlan_hdr);
1127 	udp = (struct rte_udp_hdr *)template;
1128 	append_template(&template, &off, item->spec,
1129 			sizeof(struct rte_udp_hdr));
1130 	/*
1131 	 * Firmware does not hash/fill source port yet. Generate a
1132 	 * random port, as there is *usually* one rte_flow for the
1133 	 * given inner packet stream (i.e. a single stream has one
1134 	 * random port).
1135 	 */
1136 	if (udp->src_port == 0)
1137 		udp->src_port = gen_src_port();
1138 	item++;
1139 	flow_item_skip_void(&item);
1140 
1141 	/* Finally VXLAN */
1142 	if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN)
1143 		return rte_flow_error_set(error,
1144 			EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1145 			"vxlan-encap: VXLAN must follow UDP");
1146 	append_template(&template, &off, item->spec,
1147 			sizeof(struct rte_flow_item_vxlan));
1148 
1149 	/*
1150 	 * Fill in the rest of the action structure.
1151 	 * Indicate that we want to encap with vxlan at packet start.
1152 	 */
1153 	fm_op.encap.template_offset = 0;
1154 	fm_op.encap.template_len = off;
1155 	return enic_fm_append_action_op(fm, &fm_op, error);
1156 }
1157 
1158 static int
enic_fm_find_vnic(struct enic * enic,const struct rte_pci_addr * addr,uint64_t * handle)1159 enic_fm_find_vnic(struct enic *enic, const struct rte_pci_addr *addr,
1160 		  uint64_t *handle)
1161 {
1162 	uint32_t bdf;
1163 	uint64_t args[2];
1164 	int rc;
1165 
1166 	ENICPMD_FUNC_TRACE();
1167 	ENICPMD_LOG(DEBUG, "bdf=%x:%x:%x", addr->bus, addr->devid,
1168 		    addr->function);
1169 	bdf = addr->bus << 8 | addr->devid << 3 | addr->function;
1170 	args[0] = FM_VNIC_FIND;
1171 	args[1] = bdf;
1172 	rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1173 	if (rc != 0) {
1174 		/* Expected to fail if BDF is not on the adapter */
1175 		ENICPMD_LOG(DEBUG, "cannot find vnic handle: rc=%d", rc);
1176 		return rc;
1177 	}
1178 	*handle = args[0];
1179 	ENICPMD_LOG(DEBUG, "found vnic: handle=0x%" PRIx64, *handle);
1180 	return 0;
1181 }
1182 
1183 /*
1184  * Egress: target port should be either PF uplink or VF.
1185  * Supported cases
1186  * 1. VF egress -> PF uplink
1187  *   PF may be this VF's PF, or another PF, as long as they are on the same VIC.
1188  * 2. VF egress -> VF
1189  *
1190  * Unsupported cases
1191  * 1. PF egress -> VF
1192  *   App should be using representor to pass packets to VF
1193  */
1194 static int
vf_egress_port_id_action(struct enic_flowman * fm,struct rte_eth_dev * dst_dev,uint64_t dst_vnic_h,struct fm_action_op * fm_op,struct rte_flow_error * error)1195 vf_egress_port_id_action(struct enic_flowman *fm,
1196 			 struct rte_eth_dev *dst_dev,
1197 			 uint64_t dst_vnic_h,
1198 			 struct fm_action_op *fm_op,
1199 			 struct rte_flow_error *error)
1200 {
1201 	struct enic *src_enic, *dst_enic;
1202 	struct enic_vf_representor *vf;
1203 	uint8_t uif;
1204 	int ret;
1205 
1206 	ENICPMD_FUNC_TRACE();
1207 	src_enic = fm->user_enic;
1208 	dst_enic = pmd_priv(dst_dev);
1209 	if (!(src_enic->rte_dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)) {
1210 		return rte_flow_error_set(error, EINVAL,
1211 			RTE_FLOW_ERROR_TYPE_ACTION,
1212 			NULL, "source port is not VF representor");
1213 	}
1214 
1215 	/* VF -> PF uplink. dst is not VF representor */
1216 	if (!(dst_dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)) {
1217 		/* PF is the VF's PF? Then nothing to do */
1218 		vf = VF_ENIC_TO_VF_REP(src_enic);
1219 		if (vf->pf == dst_enic) {
1220 			ENICPMD_LOG(DEBUG, "destination port is VF's PF");
1221 			return 0;
1222 		}
1223 		/* If not, steer to the remote PF's uplink */
1224 		uif = dst_enic->fm_vnic_uif;
1225 		ENICPMD_LOG(DEBUG, "steer to uplink %u", uif);
1226 		memset(fm_op, 0, sizeof(*fm_op));
1227 		fm_op->fa_op = FMOP_SET_EGPORT;
1228 		fm_op->set_egport.egport = uif;
1229 		ret = enic_fm_append_action_op(fm, fm_op, error);
1230 		return ret;
1231 	}
1232 
1233 	/* VF -> VF loopback. Hairpin and steer to vnic */
1234 	memset(fm_op, 0, sizeof(*fm_op));
1235 	fm_op->fa_op = FMOP_EG_HAIRPIN;
1236 	ret = enic_fm_append_action_op(fm, fm_op, error);
1237 	if (ret)
1238 		return ret;
1239 	ENICPMD_LOG(DEBUG, "egress hairpin");
1240 	fm->hairpin_steer_vnic_h = dst_vnic_h;
1241 	fm->need_hairpin_steer = 1;
1242 	return 0;
1243 }
1244 
1245 /* Translate flow actions to flowman TCAM entry actions */
1246 static int
enic_fm_copy_action(struct enic_flowman * fm,const struct rte_flow_action actions[],uint8_t ingress,struct rte_flow_error * error)1247 enic_fm_copy_action(struct enic_flowman *fm,
1248 		    const struct rte_flow_action actions[],
1249 		    uint8_t ingress,
1250 		    struct rte_flow_error *error)
1251 {
1252 	enum {
1253 		FATE = 1 << 0,
1254 		DECAP = 1 << 1,
1255 		PASSTHRU = 1 << 2,
1256 		COUNT = 1 << 3,
1257 		ENCAP = 1 << 4,
1258 		PUSH_VLAN = 1 << 5,
1259 		PORT_ID = 1 << 6,
1260 	};
1261 	struct fm_tcam_match_entry *fmt;
1262 	struct fm_action_op fm_op;
1263 	bool need_ovlan_action;
1264 	struct enic *enic;
1265 	uint32_t overlap;
1266 	uint64_t vnic_h;
1267 	uint16_t ovlan;
1268 	bool first_rq;
1269 	bool steer;
1270 	int ret;
1271 
1272 	ENICPMD_FUNC_TRACE();
1273 	fmt = &fm->tcam_entry;
1274 	need_ovlan_action = false;
1275 	ovlan = 0;
1276 	first_rq = true;
1277 	steer = false;
1278 	enic = fm->user_enic;
1279 	overlap = 0;
1280 	vnic_h = enic->fm_vnic_handle;
1281 
1282 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1283 		switch (actions->type) {
1284 		case RTE_FLOW_ACTION_TYPE_VOID:
1285 			continue;
1286 		case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1287 			if (overlap & PASSTHRU)
1288 				goto unsupported;
1289 			overlap |= PASSTHRU;
1290 			break;
1291 		}
1292 		case RTE_FLOW_ACTION_TYPE_JUMP: {
1293 			const struct rte_flow_action_jump *jump =
1294 				actions->conf;
1295 			struct enic_fm_fet *fet;
1296 
1297 			if (overlap & FATE)
1298 				goto unsupported;
1299 			ret = enic_fet_get(fm, jump->group, ingress, NULL,
1300 					   &fet, error);
1301 			if (ret)
1302 				return ret;
1303 			overlap |= FATE;
1304 			memset(&fm_op, 0, sizeof(fm_op));
1305 			fm_op.fa_op = FMOP_EXACT_MATCH;
1306 			fm_op.exact.handle = fet->handle;
1307 			fm->fet = fet;
1308 			ret = enic_fm_append_action_op(fm, &fm_op, error);
1309 			if (ret)
1310 				return ret;
1311 			break;
1312 		}
1313 		case RTE_FLOW_ACTION_TYPE_MARK: {
1314 			const struct rte_flow_action_mark *mark =
1315 				actions->conf;
1316 
1317 			if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1318 				return rte_flow_error_set(error, EINVAL,
1319 					RTE_FLOW_ERROR_TYPE_ACTION,
1320 					NULL, "invalid mark id");
1321 			memset(&fm_op, 0, sizeof(fm_op));
1322 			fm_op.fa_op = FMOP_MARK;
1323 			fm_op.mark.mark = mark->id + 1;
1324 			ret = enic_fm_append_action_op(fm, &fm_op, error);
1325 			if (ret)
1326 				return ret;
1327 			break;
1328 		}
1329 		case RTE_FLOW_ACTION_TYPE_FLAG: {
1330 			/* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1331 			memset(&fm_op, 0, sizeof(fm_op));
1332 			fm_op.fa_op = FMOP_MARK;
1333 			fm_op.mark.mark = ENIC_MAGIC_FILTER_ID;
1334 			ret = enic_fm_append_action_op(fm, &fm_op, error);
1335 			if (ret)
1336 				return ret;
1337 			break;
1338 		}
1339 		case RTE_FLOW_ACTION_TYPE_QUEUE: {
1340 			const struct rte_flow_action_queue *queue =
1341 				actions->conf;
1342 
1343 			/*
1344 			 * If fate other than QUEUE or RSS, fail. Multiple
1345 			 * rss and queue actions are ok.
1346 			 */
1347 			if ((overlap & FATE) && first_rq)
1348 				goto unsupported;
1349 			first_rq = false;
1350 			overlap |= FATE;
1351 			memset(&fm_op, 0, sizeof(fm_op));
1352 			fm_op.fa_op = FMOP_RQ_STEER;
1353 			fm_op.rq_steer.rq_index =
1354 				enic_rte_rq_idx_to_sop_idx(queue->index);
1355 			fm_op.rq_steer.rq_count = 1;
1356 			fm_op.rq_steer.vnic_handle = vnic_h;
1357 			ret = enic_fm_append_action_op(fm, &fm_op, error);
1358 			if (ret)
1359 				return ret;
1360 			ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1361 				    fm_op.rq_steer.rq_index);
1362 			steer = true;
1363 			break;
1364 		}
1365 		case RTE_FLOW_ACTION_TYPE_DROP: {
1366 			if (overlap & FATE)
1367 				goto unsupported;
1368 			overlap |= FATE;
1369 			memset(&fm_op, 0, sizeof(fm_op));
1370 			fm_op.fa_op = FMOP_DROP;
1371 			ret = enic_fm_append_action_op(fm, &fm_op, error);
1372 			if (ret)
1373 				return ret;
1374 			ENICPMD_LOG(DEBUG, "create DROP action");
1375 			break;
1376 		}
1377 		case RTE_FLOW_ACTION_TYPE_COUNT: {
1378 			if (overlap & COUNT)
1379 				goto unsupported;
1380 			overlap |= COUNT;
1381 			/* Count is associated with entry not action on VIC. */
1382 			fmt->ftm_flags |= FMEF_COUNTER;
1383 			break;
1384 		}
1385 		case RTE_FLOW_ACTION_TYPE_RSS: {
1386 			const struct rte_flow_action_rss *rss = actions->conf;
1387 			bool allow;
1388 			uint16_t i;
1389 
1390 			/*
1391 			 * If fate other than QUEUE or RSS, fail. Multiple
1392 			 * rss and queue actions are ok.
1393 			 */
1394 			if ((overlap & FATE) && first_rq)
1395 				goto unsupported;
1396 			first_rq = false;
1397 			overlap |= FATE;
1398 
1399 			/*
1400 			 * Hardware only supports RSS actions on outer level
1401 			 * with default type and function. Queues must be
1402 			 * sequential.
1403 			 */
1404 			allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1405 				rss->level == 0 && (rss->types == 0 ||
1406 				rss->types == enic->rss_hf) &&
1407 				rss->queue_num <= enic->rq_count &&
1408 				rss->queue[rss->queue_num - 1] < enic->rq_count;
1409 
1410 
1411 			/* Identity queue map needs to be sequential */
1412 			for (i = 1; i < rss->queue_num; i++)
1413 				allow = allow && (rss->queue[i] ==
1414 					rss->queue[i - 1] + 1);
1415 			if (!allow)
1416 				goto unsupported;
1417 
1418 			memset(&fm_op, 0, sizeof(fm_op));
1419 			fm_op.fa_op = FMOP_RQ_STEER;
1420 			fm_op.rq_steer.rq_index =
1421 				enic_rte_rq_idx_to_sop_idx(rss->queue[0]);
1422 			fm_op.rq_steer.rq_count = rss->queue_num;
1423 			fm_op.rq_steer.vnic_handle = vnic_h;
1424 			ret = enic_fm_append_action_op(fm, &fm_op, error);
1425 			if (ret)
1426 				return ret;
1427 			ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1428 				    fm_op.rq_steer.rq_index);
1429 			steer = true;
1430 			break;
1431 		}
1432 		case RTE_FLOW_ACTION_TYPE_PORT_ID: {
1433 			const struct rte_flow_action_port_id *port;
1434 			struct rte_eth_dev *dev;
1435 
1436 			if (!ingress && (overlap & PORT_ID)) {
1437 				ENICPMD_LOG(DEBUG, "cannot have multiple egress PORT_ID actions");
1438 				goto unsupported;
1439 			}
1440 			port = actions->conf;
1441 			if (port->original) {
1442 				vnic_h = enic->fm_vnic_handle; /* This port */
1443 				break;
1444 			}
1445 			ENICPMD_LOG(DEBUG, "port id %u", port->id);
1446 			if (!rte_eth_dev_is_valid_port(port->id)) {
1447 				return rte_flow_error_set(error, EINVAL,
1448 					RTE_FLOW_ERROR_TYPE_ACTION,
1449 					NULL, "invalid port_id");
1450 			}
1451 			dev = &rte_eth_devices[port->id];
1452 			if (!dev_is_enic(dev)) {
1453 				return rte_flow_error_set(error, EINVAL,
1454 					RTE_FLOW_ERROR_TYPE_ACTION,
1455 					NULL, "port_id is not enic");
1456 			}
1457 			if (enic->switch_domain_id !=
1458 			    pmd_priv(dev)->switch_domain_id) {
1459 				return rte_flow_error_set(error, EINVAL,
1460 					RTE_FLOW_ERROR_TYPE_ACTION,
1461 					NULL, "destination and source ports are not in the same switch domain");
1462 			}
1463 			vnic_h = pmd_priv(dev)->fm_vnic_handle;
1464 			overlap |= PORT_ID;
1465 			/*
1466 			 * Ingress. Nothing more to do. We add an implicit
1467 			 * steer at the end if needed.
1468 			 */
1469 			if (ingress)
1470 				break;
1471 			/* Egress */
1472 			ret = vf_egress_port_id_action(fm, dev, vnic_h, &fm_op,
1473 				error);
1474 			if (ret)
1475 				return ret;
1476 			break;
1477 		}
1478 		case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP: {
1479 			if (overlap & DECAP)
1480 				goto unsupported;
1481 			overlap |= DECAP;
1482 
1483 			ret = enic_fm_copy_vxlan_decap(fm, fmt, actions,
1484 				error);
1485 			if (ret != 0)
1486 				return ret;
1487 			break;
1488 		}
1489 		case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP: {
1490 			const struct rte_flow_action_vxlan_encap *encap;
1491 
1492 			encap = actions->conf;
1493 			if (overlap & ENCAP)
1494 				goto unsupported;
1495 			overlap |= ENCAP;
1496 			ret = enic_fm_copy_vxlan_encap(fm, encap->definition,
1497 				error);
1498 			if (ret != 0)
1499 				return ret;
1500 			break;
1501 		}
1502 		case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN: {
1503 			struct fm_action_op *decap;
1504 
1505 			/*
1506 			 * If decap-nostrip appears before pop vlan, this pop
1507 			 * applies to the inner packet vlan. Turn it into
1508 			 * decap-strip.
1509 			 */
1510 			decap = find_prev_action_op(fm, FMOP_DECAP_NOSTRIP);
1511 			if (decap) {
1512 				ENICPMD_LOG(DEBUG, "pop-vlan inner: decap-nostrip => decap-strip");
1513 				decap->fa_op = FMOP_DECAP_STRIP;
1514 				break;
1515 			}
1516 			memset(&fm_op, 0, sizeof(fm_op));
1517 			fm_op.fa_op = FMOP_POP_VLAN;
1518 			ret = enic_fm_append_action_op(fm, &fm_op, error);
1519 			if (ret)
1520 				return ret;
1521 			break;
1522 		}
1523 		case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN: {
1524 			const struct rte_flow_action_of_push_vlan *vlan;
1525 
1526 			if (overlap & PASSTHRU)
1527 				goto unsupported;
1528 			vlan = actions->conf;
1529 			if (vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN)) {
1530 				return rte_flow_error_set(error, EINVAL,
1531 					RTE_FLOW_ERROR_TYPE_ACTION,
1532 					NULL, "unexpected push_vlan ethertype");
1533 			}
1534 			overlap |= PUSH_VLAN;
1535 			need_ovlan_action = true;
1536 			break;
1537 		}
1538 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP: {
1539 			const struct rte_flow_action_of_set_vlan_pcp *pcp;
1540 
1541 			pcp = actions->conf;
1542 			if (pcp->vlan_pcp > 7) {
1543 				return rte_flow_error_set(error, EINVAL,
1544 					RTE_FLOW_ERROR_TYPE_ACTION,
1545 					NULL, "invalid vlan_pcp");
1546 			}
1547 			need_ovlan_action = true;
1548 			ovlan |= ((uint16_t)pcp->vlan_pcp) << 13;
1549 			break;
1550 		}
1551 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID: {
1552 			const struct rte_flow_action_of_set_vlan_vid *vid;
1553 
1554 			vid = actions->conf;
1555 			need_ovlan_action = true;
1556 			ovlan |= rte_be_to_cpu_16(vid->vlan_vid);
1557 			break;
1558 		}
1559 		default:
1560 			goto unsupported;
1561 		}
1562 	}
1563 
1564 	if (!(overlap & (FATE | PASSTHRU | COUNT | PORT_ID)))
1565 		goto unsupported;
1566 	/* Egress from VF: need implicit WQ match */
1567 	if (enic_is_vf_rep(enic) && !ingress) {
1568 		fmt->ftm_data.fk_wq_id = 0;
1569 		fmt->ftm_mask.fk_wq_id = 0xffff;
1570 		fmt->ftm_data.fk_wq_vnic = enic->fm_vnic_handle;
1571 		ENICPMD_LOG(DEBUG, "add implicit wq id match for vf %d",
1572 			    VF_ENIC_TO_VF_REP(enic)->vf_id);
1573 	}
1574 	if (need_ovlan_action) {
1575 		memset(&fm_op, 0, sizeof(fm_op));
1576 		fm_op.fa_op = FMOP_SET_OVLAN;
1577 		fm_op.ovlan.vlan = ovlan;
1578 		ret = enic_fm_append_action_op(fm, &fm_op, error);
1579 		if (ret)
1580 			return ret;
1581 	}
1582 	/* Add steer op for PORT_ID without QUEUE */
1583 	if ((overlap & PORT_ID) && !steer && ingress) {
1584 		memset(&fm_op, 0, sizeof(fm_op));
1585 		/* Always to queue 0 for now as generic RSS is not available */
1586 		fm_op.fa_op = FMOP_RQ_STEER;
1587 		fm_op.rq_steer.rq_index = 0;
1588 		fm_op.rq_steer.vnic_handle = vnic_h;
1589 		ret = enic_fm_append_action_op(fm, &fm_op, error);
1590 		if (ret)
1591 			return ret;
1592 		ENICPMD_LOG(DEBUG, "add implicit steer op");
1593 	}
1594 	/* Add required END */
1595 	memset(&fm_op, 0, sizeof(fm_op));
1596 	fm_op.fa_op = FMOP_END;
1597 	ret = enic_fm_append_action_op(fm, &fm_op, error);
1598 	if (ret)
1599 		return ret;
1600 	enic_fm_reorder_action_op(fm);
1601 	return 0;
1602 
1603 unsupported:
1604 	return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1605 				  NULL, "enic: unsupported action");
1606 }
1607 
1608 /** Check if the action is supported */
1609 static int
enic_fm_match_action(const struct rte_flow_action * action,const enum rte_flow_action_type * supported_actions)1610 enic_fm_match_action(const struct rte_flow_action *action,
1611 		     const enum rte_flow_action_type *supported_actions)
1612 {
1613 	for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1614 	     supported_actions++) {
1615 		if (action->type == *supported_actions)
1616 			return 1;
1617 	}
1618 	return 0;
1619 }
1620 
1621 /* Debug function to dump internal NIC action structure. */
1622 static void
enic_fm_dump_tcam_actions(const struct fm_action * fm_action)1623 enic_fm_dump_tcam_actions(const struct fm_action *fm_action)
1624 {
1625 	/* Manually keep in sync with FMOP commands */
1626 	const char *fmop_str[FMOP_OP_MAX] = {
1627 		[FMOP_END] = "end",
1628 		[FMOP_DROP] = "drop",
1629 		[FMOP_RQ_STEER] = "steer",
1630 		[FMOP_EXACT_MATCH] = "exmatch",
1631 		[FMOP_MARK] = "mark",
1632 		[FMOP_EXT_MARK] = "ext_mark",
1633 		[FMOP_TAG] = "tag",
1634 		[FMOP_EG_HAIRPIN] = "eg_hairpin",
1635 		[FMOP_IG_HAIRPIN] = "ig_hairpin",
1636 		[FMOP_ENCAP_IVLAN] = "encap_ivlan",
1637 		[FMOP_ENCAP_NOIVLAN] = "encap_noivlan",
1638 		[FMOP_ENCAP] = "encap",
1639 		[FMOP_SET_OVLAN] = "set_ovlan",
1640 		[FMOP_DECAP_NOSTRIP] = "decap_nostrip",
1641 		[FMOP_DECAP_STRIP] = "decap_strip",
1642 		[FMOP_POP_VLAN] = "pop_vlan",
1643 		[FMOP_SET_EGPORT] = "set_egport",
1644 		[FMOP_RQ_STEER_ONLY] = "rq_steer_only",
1645 		[FMOP_SET_ENCAP_VLAN] = "set_encap_vlan",
1646 		[FMOP_EMIT] = "emit",
1647 		[FMOP_MODIFY] = "modify",
1648 	};
1649 	const struct fm_action_op *op = &fm_action->fma_action_ops[0];
1650 	char buf[128], *bp = buf;
1651 	const char *op_str;
1652 	int i, n, buf_len;
1653 
1654 	buf[0] = '\0';
1655 	buf_len = sizeof(buf);
1656 	for (i = 0; i < FM_ACTION_OP_MAX; i++) {
1657 		if (op->fa_op == FMOP_END)
1658 			break;
1659 		if (op->fa_op >= FMOP_OP_MAX)
1660 			op_str = "unknown";
1661 		else
1662 			op_str = fmop_str[op->fa_op];
1663 		n = snprintf(bp, buf_len, "%s,", op_str);
1664 		if (n > 0 && n < buf_len) {
1665 			bp += n;
1666 			buf_len -= n;
1667 		}
1668 		op++;
1669 	}
1670 	/* Remove trailing comma */
1671 	if (buf[0])
1672 		*(bp - 1) = '\0';
1673 	ENICPMD_LOG(DEBUG, "       Acions: %s", buf);
1674 }
1675 
1676 static int
bits_to_str(uint32_t bits,const char * strings[],int max,char * buf,int buf_len)1677 bits_to_str(uint32_t bits, const char *strings[], int max,
1678 	    char *buf, int buf_len)
1679 {
1680 	int i, n = 0, len = 0;
1681 
1682 	for (i = 0; i < max; i++) {
1683 		if (bits & (1 << i)) {
1684 			n = snprintf(buf, buf_len, "%s,", strings[i]);
1685 			if (n > 0 && n < buf_len) {
1686 				buf += n;
1687 				buf_len -= n;
1688 				len += n;
1689 			}
1690 		}
1691 	}
1692 	/* Remove trailing comma */
1693 	if (len) {
1694 		*(buf - 1) = '\0';
1695 		len--;
1696 	}
1697 	return len;
1698 }
1699 
1700 /* Debug function to dump internal NIC filter structure. */
1701 static void
__enic_fm_dump_tcam_match(const struct fm_header_set * fk_hdrset,char * buf,int buf_len)1702 __enic_fm_dump_tcam_match(const struct fm_header_set *fk_hdrset, char *buf,
1703 			  int buf_len)
1704 {
1705 	/* Manually keep in sync with FKM_BITS */
1706 	const char *fm_fkm_str[FKM_BIT_COUNT] = {
1707 		[FKM_QTAG_BIT] = "qtag",
1708 		[FKM_CMD_BIT] = "cmd",
1709 		[FKM_IPV4_BIT] = "ip4",
1710 		[FKM_IPV6_BIT] = "ip6",
1711 		[FKM_ROCE_BIT] = "roce",
1712 		[FKM_UDP_BIT] = "udp",
1713 		[FKM_TCP_BIT] = "tcp",
1714 		[FKM_TCPORUDP_BIT] = "tcpportudp",
1715 		[FKM_IPFRAG_BIT] = "ipfrag",
1716 		[FKM_NVGRE_BIT] = "nvgre",
1717 		[FKM_VXLAN_BIT] = "vxlan",
1718 		[FKM_GENEVE_BIT] = "geneve",
1719 		[FKM_NSH_BIT] = "nsh",
1720 		[FKM_ROCEV2_BIT] = "rocev2",
1721 		[FKM_VLAN_PRES_BIT] = "vlan_pres",
1722 		[FKM_IPOK_BIT] = "ipok",
1723 		[FKM_L4OK_BIT] = "l4ok",
1724 		[FKM_ROCEOK_BIT] = "roceok",
1725 		[FKM_FCSOK_BIT] = "fcsok",
1726 		[FKM_EG_SPAN_BIT] = "eg_span",
1727 		[FKM_IG_SPAN_BIT] = "ig_span",
1728 		[FKM_EG_HAIRPINNED_BIT] = "eg_hairpinned",
1729 	};
1730 	/* Manually keep in sync with FKH_BITS */
1731 	const char *fm_fkh_str[FKH_BIT_COUNT] = {
1732 		[FKH_ETHER_BIT] = "eth",
1733 		[FKH_QTAG_BIT] = "qtag",
1734 		[FKH_L2RAW_BIT] = "l2raw",
1735 		[FKH_IPV4_BIT] = "ip4",
1736 		[FKH_IPV6_BIT] = "ip6",
1737 		[FKH_L3RAW_BIT] = "l3raw",
1738 		[FKH_UDP_BIT] = "udp",
1739 		[FKH_TCP_BIT] = "tcp",
1740 		[FKH_ICMP_BIT] = "icmp",
1741 		[FKH_VXLAN_BIT] = "vxlan",
1742 		[FKH_L4RAW_BIT] = "l4raw",
1743 	};
1744 	uint32_t fkh_bits = fk_hdrset->fk_header_select;
1745 	uint32_t fkm_bits = fk_hdrset->fk_metadata;
1746 	int n;
1747 
1748 	if (!fkm_bits && !fkh_bits)
1749 		return;
1750 	n = snprintf(buf, buf_len, "metadata(");
1751 	if (n > 0 && n < buf_len) {
1752 		buf += n;
1753 		buf_len -= n;
1754 	}
1755 	n = bits_to_str(fkm_bits, fm_fkm_str, FKM_BIT_COUNT, buf, buf_len);
1756 	if (n > 0 && n < buf_len) {
1757 		buf += n;
1758 		buf_len -= n;
1759 	}
1760 	n = snprintf(buf, buf_len, ") valid hdr fields(");
1761 	if (n > 0 && n < buf_len) {
1762 		buf += n;
1763 		buf_len -= n;
1764 	}
1765 	n = bits_to_str(fkh_bits, fm_fkh_str, FKH_BIT_COUNT, buf, buf_len);
1766 	if (n > 0 && n < buf_len) {
1767 		buf += n;
1768 		buf_len -= n;
1769 	}
1770 	snprintf(buf, buf_len, ")");
1771 }
1772 
1773 static void
enic_fm_dump_tcam_match(const struct fm_tcam_match_entry * match,uint8_t ingress)1774 enic_fm_dump_tcam_match(const struct fm_tcam_match_entry *match,
1775 			uint8_t ingress)
1776 {
1777 	char buf[256];
1778 
1779 	memset(buf, 0, sizeof(buf));
1780 	__enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[0],
1781 				  buf, sizeof(buf));
1782 	ENICPMD_LOG(DEBUG, " TCAM %s Outer: %s %scounter position %u",
1783 		    (ingress) ? "IG" : "EG", buf,
1784 		    (match->ftm_flags & FMEF_COUNTER) ? "" : "no ",
1785 		    match->ftm_position);
1786 	memset(buf, 0, sizeof(buf));
1787 	__enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[1],
1788 				  buf, sizeof(buf));
1789 	if (buf[0])
1790 		ENICPMD_LOG(DEBUG, "         Inner: %s", buf);
1791 }
1792 
1793 /* Debug function to dump internal NIC flow structures. */
1794 static void
enic_fm_dump_tcam_entry(const struct fm_tcam_match_entry * fm_match,const struct fm_action * fm_action,uint8_t ingress)1795 enic_fm_dump_tcam_entry(const struct fm_tcam_match_entry *fm_match,
1796 			const struct fm_action *fm_action,
1797 			uint8_t ingress)
1798 {
1799 	if (!rte_log_can_log(enic_pmd_logtype, RTE_LOG_DEBUG))
1800 		return;
1801 	enic_fm_dump_tcam_match(fm_match, ingress);
1802 	enic_fm_dump_tcam_actions(fm_action);
1803 }
1804 
1805 static int
enic_fm_flow_parse(struct enic_flowman * fm,const struct rte_flow_attr * attrs,const struct rte_flow_item pattern[],const struct rte_flow_action actions[],struct rte_flow_error * error)1806 enic_fm_flow_parse(struct enic_flowman *fm,
1807 		   const struct rte_flow_attr *attrs,
1808 		   const struct rte_flow_item pattern[],
1809 		   const struct rte_flow_action actions[],
1810 		   struct rte_flow_error *error)
1811 {
1812 	const struct rte_flow_action *action;
1813 	unsigned int ret;
1814 	static const enum rte_flow_action_type *sa;
1815 
1816 	ENICPMD_FUNC_TRACE();
1817 	ret = 0;
1818 	if (!pattern) {
1819 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1820 				   NULL, "no pattern specified");
1821 		return -rte_errno;
1822 	}
1823 
1824 	if (!actions) {
1825 		rte_flow_error_set(error, EINVAL,
1826 				   RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1827 				   NULL, "no action specified");
1828 		return -rte_errno;
1829 	}
1830 
1831 	if (attrs) {
1832 		if (attrs->group != FM_TCAM_RTE_GROUP && attrs->priority) {
1833 			rte_flow_error_set(error, ENOTSUP,
1834 					   RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1835 					   NULL,
1836 					   "priorities are not supported for non-default (0) groups");
1837 			return -rte_errno;
1838 		} else if (!fm->owner_enic->switchdev_mode && attrs->transfer) {
1839 			rte_flow_error_set(error, ENOTSUP,
1840 					   RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1841 					   NULL,
1842 					   "transfer is not supported");
1843 			return -rte_errno;
1844 		} else if (attrs->ingress && attrs->egress) {
1845 			rte_flow_error_set(error, ENOTSUP,
1846 					   RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1847 					   NULL,
1848 					   "bidirectional rules not supported");
1849 			return -rte_errno;
1850 		}
1851 
1852 	} else {
1853 		rte_flow_error_set(error, EINVAL,
1854 				   RTE_FLOW_ERROR_TYPE_ATTR,
1855 				   NULL, "no attribute specified");
1856 		return -rte_errno;
1857 	}
1858 
1859 	/* Verify Actions. */
1860 	sa = (attrs->ingress) ? enic_fm_supported_ig_actions :
1861 	     enic_fm_supported_eg_actions;
1862 	for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1863 	     action++) {
1864 		if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1865 			continue;
1866 		else if (!enic_fm_match_action(action, sa))
1867 			break;
1868 	}
1869 	if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1870 		rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1871 				   action, "invalid action");
1872 		return -rte_errno;
1873 	}
1874 	ret = enic_fm_copy_entry(fm, pattern, error);
1875 	if (ret)
1876 		return ret;
1877 	ret = enic_fm_copy_action(fm, actions, attrs->ingress, error);
1878 	return ret;
1879 }
1880 
1881 static void
enic_fm_counter_free(struct enic_flowman * fm,struct enic_fm_flow * fm_flow)1882 enic_fm_counter_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1883 {
1884 	if (!fm_flow->counter_valid)
1885 		return;
1886 	SLIST_INSERT_HEAD(&fm->counters, fm_flow->counter, next);
1887 	fm_flow->counter_valid = false;
1888 }
1889 
1890 static int
enic_fm_more_counters(struct enic_flowman * fm)1891 enic_fm_more_counters(struct enic_flowman *fm)
1892 {
1893 	struct enic_fm_counter *new_stack;
1894 	struct enic_fm_counter *ctrs;
1895 	int i, rc;
1896 	uint64_t args[2];
1897 
1898 	ENICPMD_FUNC_TRACE();
1899 	new_stack = rte_realloc(fm->counter_stack, (fm->counters_alloced +
1900 				FM_COUNTERS_EXPAND) *
1901 				sizeof(struct enic_fm_counter), 0);
1902 	if (new_stack == NULL) {
1903 		ENICPMD_LOG(ERR, "cannot alloc counter memory");
1904 		return -ENOMEM;
1905 	}
1906 	fm->counter_stack = new_stack;
1907 
1908 	args[0] = FM_COUNTER_BRK;
1909 	args[1] = fm->counters_alloced + FM_COUNTERS_EXPAND;
1910 	rc = flowman_cmd(fm, args, 2);
1911 	if (rc != 0) {
1912 		ENICPMD_LOG(ERR, "cannot alloc counters rc=%d", rc);
1913 		return rc;
1914 	}
1915 	ctrs = (struct enic_fm_counter *)fm->counter_stack +
1916 		fm->counters_alloced;
1917 	for (i = 0; i < FM_COUNTERS_EXPAND; i++, ctrs++) {
1918 		ctrs->handle = fm->counters_alloced + i;
1919 		SLIST_INSERT_HEAD(&fm->counters, ctrs, next);
1920 	}
1921 	fm->counters_alloced += FM_COUNTERS_EXPAND;
1922 	ENICPMD_LOG(DEBUG, "%u counters allocated, total: %u",
1923 		    FM_COUNTERS_EXPAND, fm->counters_alloced);
1924 	return 0;
1925 }
1926 
1927 static int
enic_fm_counter_zero(struct enic_flowman * fm,struct enic_fm_counter * c)1928 enic_fm_counter_zero(struct enic_flowman *fm, struct enic_fm_counter *c)
1929 {
1930 	uint64_t args[3];
1931 	int ret;
1932 
1933 	ENICPMD_FUNC_TRACE();
1934 	args[0] = FM_COUNTER_QUERY;
1935 	args[1] = c->handle;
1936 	args[2] = 1; /* clear */
1937 	ret = flowman_cmd(fm, args, 3);
1938 	if (ret) {
1939 		ENICPMD_LOG(ERR, "counter init: rc=%d handle=0x%x",
1940 			    ret, c->handle);
1941 		return ret;
1942 	}
1943 	return 0;
1944 }
1945 
1946 static int
enic_fm_counter_alloc(struct enic_flowman * fm,struct rte_flow_error * error,struct enic_fm_counter ** ctr)1947 enic_fm_counter_alloc(struct enic_flowman *fm, struct rte_flow_error *error,
1948 		      struct enic_fm_counter **ctr)
1949 {
1950 	struct enic_fm_counter *c;
1951 	int ret;
1952 
1953 	ENICPMD_FUNC_TRACE();
1954 	*ctr = NULL;
1955 	if (SLIST_EMPTY(&fm->counters)) {
1956 		ret = enic_fm_more_counters(fm);
1957 		if (ret)
1958 			return rte_flow_error_set(error, -ret,
1959 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1960 				NULL, "enic: out of counters");
1961 	}
1962 	c = SLIST_FIRST(&fm->counters);
1963 	SLIST_REMOVE_HEAD(&fm->counters, next);
1964 	*ctr = c;
1965 	return 0;
1966 }
1967 
1968 static int
enic_fm_action_free(struct enic_flowman * fm,struct enic_fm_action * ah)1969 enic_fm_action_free(struct enic_flowman *fm, struct enic_fm_action *ah)
1970 {
1971 	uint64_t args[2];
1972 	int ret = 0;
1973 
1974 	ENICPMD_FUNC_TRACE();
1975 	RTE_ASSERT(ah->ref > 0);
1976 	ah->ref--;
1977 	if (ah->ref == 0) {
1978 		args[0] = FM_ACTION_FREE;
1979 		args[1] = ah->handle;
1980 		ret = flowman_cmd(fm, args, 2);
1981 		if (ret)
1982 			/* This is a "should never happen" error. */
1983 			ENICPMD_LOG(ERR, "freeing action rc=%d handle=0x%"
1984 				    PRIx64, ret, ah->handle);
1985 		rte_hash_del_key(fm->action_hash, (const void *)&ah->key);
1986 		free(ah);
1987 	}
1988 	return ret;
1989 }
1990 
1991 static int
enic_fm_entry_free(struct enic_flowman * fm,uint64_t handle)1992 enic_fm_entry_free(struct enic_flowman *fm, uint64_t handle)
1993 {
1994 	uint64_t args[2];
1995 	int rc;
1996 
1997 	ENICPMD_FUNC_TRACE();
1998 	args[0] = FM_MATCH_ENTRY_REMOVE;
1999 	args[1] = handle;
2000 	rc = flowman_cmd(fm, args, 2);
2001 	if (rc)
2002 		ENICPMD_LOG(ERR, "cannot free match entry: rc=%d"
2003 			    " handle=0x%" PRIx64, rc, handle);
2004 	return rc;
2005 }
2006 
2007 static struct enic_fm_jump_flow *
find_jump_flow(struct enic_flowman * fm,uint32_t group)2008 find_jump_flow(struct enic_flowman *fm, uint32_t group)
2009 {
2010 	struct enic_fm_jump_flow *j;
2011 
2012 	ENICPMD_FUNC_TRACE();
2013 	TAILQ_FOREACH(j, &fm->jump_list, list) {
2014 		if (j->group == group)
2015 			return j;
2016 	}
2017 	return NULL;
2018 }
2019 
2020 static void
remove_jump_flow(struct enic_flowman * fm,struct rte_flow * flow)2021 remove_jump_flow(struct enic_flowman *fm, struct rte_flow *flow)
2022 {
2023 	struct enic_fm_jump_flow *j;
2024 
2025 	ENICPMD_FUNC_TRACE();
2026 	TAILQ_FOREACH(j, &fm->jump_list, list) {
2027 		if (j->flow == flow) {
2028 			TAILQ_REMOVE(&fm->jump_list, j, list);
2029 			free(j);
2030 			return;
2031 		}
2032 	}
2033 }
2034 
2035 static int
save_jump_flow(struct enic_flowman * fm,struct rte_flow * flow,uint32_t group,struct fm_tcam_match_entry * match,struct fm_action * action)2036 save_jump_flow(struct enic_flowman *fm,
2037 	       struct rte_flow *flow,
2038 	       uint32_t group,
2039 	       struct fm_tcam_match_entry *match,
2040 	       struct fm_action *action)
2041 {
2042 	struct enic_fm_jump_flow *j;
2043 
2044 	ENICPMD_FUNC_TRACE();
2045 	j = calloc(1, sizeof(struct enic_fm_jump_flow));
2046 	if (j == NULL)
2047 		return -ENOMEM;
2048 	j->flow = flow;
2049 	j->group = group;
2050 	j->match = *match;
2051 	j->action = *action;
2052 	TAILQ_INSERT_HEAD(&fm->jump_list, j, list);
2053 	ENICPMD_LOG(DEBUG, "saved jump flow: flow=%p group=%u", flow, group);
2054 	return 0;
2055 }
2056 
2057 static void
__enic_fm_flow_free(struct enic_flowman * fm,struct enic_fm_flow * fm_flow)2058 __enic_fm_flow_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
2059 {
2060 	if (fm_flow->entry_handle != FM_INVALID_HANDLE) {
2061 		enic_fm_entry_free(fm, fm_flow->entry_handle);
2062 		fm_flow->entry_handle = FM_INVALID_HANDLE;
2063 	}
2064 	if (fm_flow->action != NULL) {
2065 		enic_fm_action_free(fm, fm_flow->action);
2066 		fm_flow->action = NULL;
2067 	}
2068 	enic_fm_counter_free(fm, fm_flow);
2069 	if (fm_flow->fet) {
2070 		enic_fet_put(fm, fm_flow->fet);
2071 		fm_flow->fet = NULL;
2072 	}
2073 }
2074 
2075 static void
enic_fm_flow_free(struct enic_flowman * fm,struct rte_flow * flow)2076 enic_fm_flow_free(struct enic_flowman *fm, struct rte_flow *flow)
2077 {
2078 	struct enic_fm_flow *steer = flow->fm->hairpin_steer_flow;
2079 
2080 	if (flow->fm->fet && flow->fm->fet->default_key)
2081 		remove_jump_flow(fm, flow);
2082 	__enic_fm_flow_free(fm, flow->fm);
2083 	if (steer) {
2084 		__enic_fm_flow_free(fm, steer);
2085 		free(steer);
2086 	}
2087 	free(flow->fm);
2088 	free(flow);
2089 }
2090 
2091 static int
enic_fm_add_tcam_entry(struct enic_flowman * fm,struct fm_tcam_match_entry * match_in,uint64_t * entry_handle,uint8_t ingress,struct rte_flow_error * error)2092 enic_fm_add_tcam_entry(struct enic_flowman *fm,
2093 		       struct fm_tcam_match_entry *match_in,
2094 		       uint64_t *entry_handle,
2095 		       uint8_t ingress,
2096 		       struct rte_flow_error *error)
2097 {
2098 	struct fm_tcam_match_entry *ftm;
2099 	uint64_t args[3];
2100 	int ret;
2101 
2102 	ENICPMD_FUNC_TRACE();
2103 	/* Copy entry to the command buffer */
2104 	ftm = &fm->cmd.va->fm_tcam_match_entry;
2105 	memcpy(ftm, match_in, sizeof(*ftm));
2106 	/* Add TCAM entry */
2107 	args[0] = FM_TCAM_ENTRY_INSTALL;
2108 	args[1] = ingress ? fm->ig_tcam_hndl : fm->eg_tcam_hndl;
2109 	args[2] = fm->cmd.pa;
2110 	ret = flowman_cmd(fm, args, 3);
2111 	if (ret != 0) {
2112 		ENICPMD_LOG(ERR, "cannot add %s TCAM entry: rc=%d",
2113 			    ingress ? "ingress" : "egress", ret);
2114 		rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2115 			NULL, "enic: devcmd(tcam-entry-install)");
2116 		return ret;
2117 	}
2118 	ENICPMD_LOG(DEBUG, "installed %s TCAM entry: handle=0x%" PRIx64,
2119 		    ingress ? "ingress" : "egress", (uint64_t)args[0]);
2120 	*entry_handle = args[0];
2121 	return 0;
2122 }
2123 
2124 static int
enic_fm_add_exact_entry(struct enic_flowman * fm,struct fm_tcam_match_entry * match_in,uint64_t * entry_handle,struct enic_fm_fet * fet,struct rte_flow_error * error)2125 enic_fm_add_exact_entry(struct enic_flowman *fm,
2126 			struct fm_tcam_match_entry *match_in,
2127 			uint64_t *entry_handle,
2128 			struct enic_fm_fet *fet,
2129 			struct rte_flow_error *error)
2130 {
2131 	struct fm_exact_match_entry *fem;
2132 	uint64_t args[3];
2133 	int ret;
2134 
2135 	ENICPMD_FUNC_TRACE();
2136 	/* The new entry must have the table's key */
2137 	if (memcmp(fet->key.fk_hdrset, match_in->ftm_mask.fk_hdrset,
2138 		   sizeof(struct fm_header_set) * FM_HDRSET_MAX)) {
2139 		return rte_flow_error_set(error, EINVAL,
2140 			RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2141 			"enic: key does not match group's key");
2142 	}
2143 
2144 	/* Copy entry to the command buffer */
2145 	fem = &fm->cmd.va->fm_exact_match_entry;
2146 	/*
2147 	 * Translate TCAM entry to exact entry. As is only need to drop
2148 	 * position and mask. The mask is part of the exact match table.
2149 	 * Position (aka priority) is not supported in the exact match table.
2150 	 */
2151 	fem->fem_data = match_in->ftm_data;
2152 	fem->fem_flags = match_in->ftm_flags;
2153 	fem->fem_action = match_in->ftm_action;
2154 	fem->fem_counter = match_in->ftm_counter;
2155 
2156 	/* Add exact entry */
2157 	args[0] = FM_EXACT_ENTRY_INSTALL;
2158 	args[1] = fet->handle;
2159 	args[2] = fm->cmd.pa;
2160 	ret = flowman_cmd(fm, args, 3);
2161 	if (ret != 0) {
2162 		ENICPMD_LOG(ERR, "cannot add %s exact entry: group=%u",
2163 			    fet->ingress ? "ingress" : "egress", fet->group);
2164 		rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2165 			NULL, "enic: devcmd(exact-entry-install)");
2166 		return ret;
2167 	}
2168 	ENICPMD_LOG(DEBUG, "installed %s exact entry: group=%u"
2169 		    " handle=0x%" PRIx64,
2170 		    fet->ingress ? "ingress" : "egress", fet->group,
2171 		    (uint64_t)args[0]);
2172 	*entry_handle = args[0];
2173 	return 0;
2174 }
2175 
2176 static int
enic_action_handle_get(struct enic_flowman * fm,struct fm_action * action_in,struct rte_flow_error * error,struct enic_fm_action ** ah_o)2177 enic_action_handle_get(struct enic_flowman *fm, struct fm_action *action_in,
2178 		       struct rte_flow_error *error,
2179 		       struct enic_fm_action **ah_o)
2180 {
2181 	struct enic_fm_action *ah;
2182 	struct fm_action *fma;
2183 	uint64_t args[2];
2184 	int ret = 0;
2185 
2186 	ret = rte_hash_lookup_data(fm->action_hash, action_in,
2187 				   (void **)&ah);
2188 	if (ret < 0 && ret != -ENOENT)
2189 		return rte_flow_error_set(error, -ret,
2190 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2191 				   NULL, "enic: rte_hash_lookup(aciton)");
2192 
2193 	if (ret == -ENOENT) {
2194 		/* Allocate a new action on the NIC. */
2195 		fma = &fm->cmd.va->fm_action;
2196 		memcpy(fma, action_in, sizeof(*fma));
2197 
2198 		ah = calloc(1, sizeof(*ah));
2199 		memcpy(&ah->key, action_in, sizeof(struct fm_action));
2200 		if (ah == NULL)
2201 			return rte_flow_error_set(error, ENOMEM,
2202 					   RTE_FLOW_ERROR_TYPE_HANDLE,
2203 					   NULL, "enic: calloc(fm-action)");
2204 		args[0] = FM_ACTION_ALLOC;
2205 		args[1] = fm->cmd.pa;
2206 		ret = flowman_cmd(fm, args, 2);
2207 		if (ret != 0) {
2208 			rte_flow_error_set(error, -ret,
2209 					   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2210 					   NULL, "enic: devcmd(action-alloc)");
2211 			goto error_with_ah;
2212 		}
2213 		ah->handle = args[0];
2214 		ret = rte_hash_add_key_data(fm->action_hash,
2215 					    (const void *)action_in,
2216 					    (void *)ah);
2217 		if (ret != 0) {
2218 			rte_flow_error_set(error, -ret,
2219 					   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2220 					   NULL,
2221 					   "enic: rte_hash_add_key_data(actn)");
2222 			goto error_with_action_handle;
2223 		}
2224 		ENICPMD_LOG(DEBUG, "action allocated: handle=0x%" PRIx64,
2225 			    ah->handle);
2226 	}
2227 
2228 	/* Action handle struct is valid, increment reference count. */
2229 	ah->ref++;
2230 	*ah_o = ah;
2231 	return 0;
2232 error_with_action_handle:
2233 	args[0] = FM_ACTION_FREE;
2234 	args[1] = ah->handle;
2235 	ret = flowman_cmd(fm, args, 2);
2236 	if (ret != 0)
2237 		rte_flow_error_set(error, -ret,
2238 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2239 				   NULL, "enic: devcmd(action-free)");
2240 error_with_ah:
2241 	free(ah);
2242 	return ret;
2243 }
2244 
2245 /* Push match-action to the NIC. */
2246 static int
__enic_fm_flow_add_entry(struct enic_flowman * fm,struct enic_fm_flow * fm_flow,struct fm_tcam_match_entry * match_in,struct fm_action * action_in,uint32_t group,uint8_t ingress,struct rte_flow_error * error)2247 __enic_fm_flow_add_entry(struct enic_flowman *fm,
2248 			 struct enic_fm_flow *fm_flow,
2249 			 struct fm_tcam_match_entry *match_in,
2250 			 struct fm_action *action_in,
2251 			 uint32_t group,
2252 			 uint8_t ingress,
2253 			 struct rte_flow_error *error)
2254 {
2255 	struct enic_fm_counter *ctr;
2256 	struct enic_fm_action *ah = NULL;
2257 	uint64_t entry_h;
2258 	int ret;
2259 
2260 	ENICPMD_FUNC_TRACE();
2261 
2262 	/* Get or create an aciton handle. */
2263 	ret = enic_action_handle_get(fm, action_in, error, &ah);
2264 	if (ret)
2265 		return ret;
2266 	match_in->ftm_action = ah->handle;
2267 	fm_flow->action = ah;
2268 
2269 	/* Allocate counter if requested. */
2270 	if (match_in->ftm_flags & FMEF_COUNTER) {
2271 		ret = enic_fm_counter_alloc(fm, error, &ctr);
2272 		if (ret) /* error has been filled in */
2273 			return ret;
2274 		fm_flow->counter_valid = true;
2275 		fm_flow->counter = ctr;
2276 		match_in->ftm_counter = ctr->handle;
2277 	}
2278 
2279 	/*
2280 	 * Get the group's table (either TCAM or exact match table) and
2281 	 * add entry to it. If we use the exact match table, the handler
2282 	 * will translate the TCAM entry (match_in) to the appropriate
2283 	 * exact match entry and use that instead.
2284 	 */
2285 	entry_h = FM_INVALID_HANDLE;
2286 	if (group == FM_TCAM_RTE_GROUP) {
2287 		ret = enic_fm_add_tcam_entry(fm, match_in, &entry_h, ingress,
2288 					     error);
2289 		if (ret)
2290 			return ret;
2291 		/* Jump action might have a ref to fet */
2292 		fm_flow->fet = fm->fet;
2293 		fm->fet = NULL;
2294 	} else {
2295 		struct enic_fm_fet *fet = NULL;
2296 
2297 		ret = enic_fet_get(fm, group, ingress,
2298 				   &match_in->ftm_mask, &fet, error);
2299 		if (ret)
2300 			return ret;
2301 		fm_flow->fet = fet;
2302 		ret = enic_fm_add_exact_entry(fm, match_in, &entry_h, fet,
2303 					      error);
2304 		if (ret)
2305 			return ret;
2306 	}
2307 	/* Clear counter after adding entry, as it requires in-use counter */
2308 	if (fm_flow->counter_valid) {
2309 		ret = enic_fm_counter_zero(fm, fm_flow->counter);
2310 		if (ret)
2311 			return ret;
2312 	}
2313 	fm_flow->entry_handle = entry_h;
2314 	return 0;
2315 }
2316 
2317 /* Push match-action to the NIC. */
2318 static struct rte_flow *
enic_fm_flow_add_entry(struct enic_flowman * fm,struct fm_tcam_match_entry * match_in,struct fm_action * action_in,const struct rte_flow_attr * attrs,struct rte_flow_error * error)2319 enic_fm_flow_add_entry(struct enic_flowman *fm,
2320 		       struct fm_tcam_match_entry *match_in,
2321 		       struct fm_action *action_in,
2322 		       const struct rte_flow_attr *attrs,
2323 		       struct rte_flow_error *error)
2324 {
2325 	struct enic_fm_flow *fm_flow;
2326 	struct rte_flow *flow;
2327 
2328 	ENICPMD_FUNC_TRACE();
2329 	match_in->ftm_position = attrs->priority;
2330 	enic_fm_dump_tcam_entry(match_in, action_in, attrs->ingress);
2331 	flow = calloc(1, sizeof(*flow));
2332 	fm_flow = calloc(1, sizeof(*fm_flow));
2333 	if (flow == NULL || fm_flow == NULL) {
2334 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2335 			NULL, "enic: cannot allocate rte_flow");
2336 		free(flow);
2337 		free(fm_flow);
2338 		return NULL;
2339 	}
2340 	flow->fm = fm_flow;
2341 	fm_flow->action = NULL;
2342 	fm_flow->entry_handle = FM_INVALID_HANDLE;
2343 	if (__enic_fm_flow_add_entry(fm, fm_flow, match_in, action_in,
2344 				     attrs->group, attrs->ingress, error)) {
2345 		enic_fm_flow_free(fm, flow);
2346 		return NULL;
2347 	}
2348 	return flow;
2349 }
2350 
2351 static void
convert_jump_flows(struct enic_flowman * fm,struct enic_fm_fet * fet,struct rte_flow_error * error)2352 convert_jump_flows(struct enic_flowman *fm, struct enic_fm_fet *fet,
2353 		   struct rte_flow_error *error)
2354 {
2355 	struct enic_fm_flow *fm_flow;
2356 	struct enic_fm_jump_flow *j;
2357 	struct fm_action *fma;
2358 	uint32_t group;
2359 
2360 	ENICPMD_FUNC_TRACE();
2361 	/*
2362 	 * Find the saved flows that should jump to the new table (fet).
2363 	 * Then delete the old TCAM entry that jumps to the default table,
2364 	 * and add a new one that jumps to the new table.
2365 	 */
2366 	group = fet->group;
2367 	j = find_jump_flow(fm, group);
2368 	while (j) {
2369 		ENICPMD_LOG(DEBUG, "convert jump flow: flow=%p group=%u",
2370 			    j->flow, group);
2371 		/* Delete old entry */
2372 		fm_flow = j->flow->fm;
2373 		__enic_fm_flow_free(fm, fm_flow);
2374 
2375 		/* Add new entry */
2376 		fma = &j->action;
2377 		fma->fma_action_ops[0].exact.handle = fet->handle;
2378 		if (__enic_fm_flow_add_entry(fm, fm_flow, &j->match, fma,
2379 			FM_TCAM_RTE_GROUP, fet->ingress, error)) {
2380 			/* Cannot roll back changes at the moment */
2381 			ENICPMD_LOG(ERR, "cannot convert jump flow: flow=%p",
2382 				    j->flow);
2383 		} else {
2384 			fm_flow->fet = fet;
2385 			fet->ref++;
2386 			ENICPMD_LOG(DEBUG, "convert ok: group=%u ref=%u",
2387 				    fet->group, fet->ref);
2388 		}
2389 
2390 		TAILQ_REMOVE(&fm->jump_list, j, list);
2391 		free(j);
2392 		j = find_jump_flow(fm, group);
2393 	}
2394 }
2395 
2396 static int
add_hairpin_steer(struct enic_flowman * fm,struct rte_flow * flow,struct rte_flow_error * error)2397 add_hairpin_steer(struct enic_flowman *fm, struct rte_flow *flow,
2398 		  struct rte_flow_error *error)
2399 {
2400 	struct fm_tcam_match_entry *fm_tcam_entry;
2401 	struct enic_fm_flow *fm_flow;
2402 	struct fm_action *fm_action;
2403 	struct fm_action_op fm_op;
2404 	int ret;
2405 
2406 	ENICPMD_FUNC_TRACE();
2407 	fm_flow = calloc(1, sizeof(*fm_flow));
2408 	if (fm_flow == NULL) {
2409 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2410 			NULL, "enic: cannot allocate rte_flow");
2411 		return -ENOMEM;
2412 	}
2413 	/* Original egress hairpin flow */
2414 	fm_tcam_entry = &fm->tcam_entry;
2415 	fm_action = &fm->action;
2416 	/* Use the match pattern of the egress flow as is, without counters */
2417 	fm_tcam_entry->ftm_flags &= ~FMEF_COUNTER;
2418 	/* The only action is steer to vnic */
2419 	fm->action_op_count = 0;
2420 	memset(fm_action, 0, sizeof(*fm_action));
2421 	memset(&fm_op, 0, sizeof(fm_op));
2422 	/* Always to queue 0 for now */
2423 	fm_op.fa_op = FMOP_RQ_STEER;
2424 	fm_op.rq_steer.rq_index = 0;
2425 	fm_op.rq_steer.vnic_handle = fm->hairpin_steer_vnic_h;
2426 	ret = enic_fm_append_action_op(fm, &fm_op, error);
2427 	if (ret)
2428 		goto error_with_flow;
2429 	ENICPMD_LOG(DEBUG, "add steer op");
2430 	/* Add required END */
2431 	memset(&fm_op, 0, sizeof(fm_op));
2432 	fm_op.fa_op = FMOP_END;
2433 	ret = enic_fm_append_action_op(fm, &fm_op, error);
2434 	if (ret)
2435 		goto error_with_flow;
2436 	/* Add the ingress flow */
2437 	fm_flow->action = NULL;
2438 	fm_flow->entry_handle = FM_INVALID_HANDLE;
2439 	ret = __enic_fm_flow_add_entry(fm, fm_flow, fm_tcam_entry, fm_action,
2440 				       FM_TCAM_RTE_GROUP, 1 /* ingress */, error);
2441 	if (ret) {
2442 		ENICPMD_LOG(ERR, "cannot add hairpin-steer flow");
2443 		goto error_with_flow;
2444 	}
2445 	/* The new flow is now the egress flow's paired flow */
2446 	flow->fm->hairpin_steer_flow = fm_flow;
2447 	return 0;
2448 
2449 error_with_flow:
2450 	free(fm_flow);
2451 	return ret;
2452 }
2453 
2454 static void
enic_fm_open_scratch(struct enic_flowman * fm)2455 enic_fm_open_scratch(struct enic_flowman *fm)
2456 {
2457 	fm->action_op_count = 0;
2458 	fm->fet = NULL;
2459 	fm->need_hairpin_steer = 0;
2460 	fm->hairpin_steer_vnic_h = 0;
2461 	memset(&fm->tcam_entry, 0, sizeof(fm->tcam_entry));
2462 	memset(&fm->action, 0, sizeof(fm->action));
2463 }
2464 
2465 static void
enic_fm_close_scratch(struct enic_flowman * fm)2466 enic_fm_close_scratch(struct enic_flowman *fm)
2467 {
2468 	if (fm->fet) {
2469 		enic_fet_put(fm, fm->fet);
2470 		fm->fet = NULL;
2471 	}
2472 	fm->action_op_count = 0;
2473 }
2474 
2475 static int
enic_fm_flow_validate(struct rte_eth_dev * dev,const struct rte_flow_attr * attrs,const struct rte_flow_item pattern[],const struct rte_flow_action actions[],struct rte_flow_error * error)2476 enic_fm_flow_validate(struct rte_eth_dev *dev,
2477 		      const struct rte_flow_attr *attrs,
2478 		      const struct rte_flow_item pattern[],
2479 		      const struct rte_flow_action actions[],
2480 		      struct rte_flow_error *error)
2481 {
2482 	struct fm_tcam_match_entry *fm_tcam_entry;
2483 	struct fm_action *fm_action;
2484 	struct enic_flowman *fm;
2485 	int ret;
2486 
2487 	ENICPMD_FUNC_TRACE();
2488 	fm = begin_fm(pmd_priv(dev));
2489 	if (fm == NULL)
2490 		return -ENOTSUP;
2491 	enic_fm_open_scratch(fm);
2492 	ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2493 	if (!ret) {
2494 		fm_tcam_entry = &fm->tcam_entry;
2495 		fm_action = &fm->action;
2496 		enic_fm_dump_tcam_entry(fm_tcam_entry, fm_action,
2497 					attrs->ingress);
2498 	}
2499 	enic_fm_close_scratch(fm);
2500 	end_fm(fm);
2501 	return ret;
2502 }
2503 
2504 static int
enic_fm_flow_query_count(struct rte_eth_dev * dev,struct rte_flow * flow,void * data,struct rte_flow_error * error)2505 enic_fm_flow_query_count(struct rte_eth_dev *dev,
2506 			 struct rte_flow *flow, void *data,
2507 			 struct rte_flow_error *error)
2508 {
2509 	struct rte_flow_query_count *query;
2510 	struct enic_fm_flow *fm_flow;
2511 	struct enic_flowman *fm;
2512 	uint64_t args[3];
2513 	int rc;
2514 
2515 	ENICPMD_FUNC_TRACE();
2516 	fm = begin_fm(pmd_priv(dev));
2517 	query = data;
2518 	fm_flow = flow->fm;
2519 	if (!fm_flow->counter_valid) {
2520 		rc = rte_flow_error_set(error, ENOTSUP,
2521 			RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2522 			"enic: flow does not have counter");
2523 		goto exit;
2524 	}
2525 
2526 	args[0] = FM_COUNTER_QUERY;
2527 	args[1] = fm_flow->counter->handle;
2528 	args[2] = query->reset;
2529 	rc = flowman_cmd(fm, args, 3);
2530 	if (rc) {
2531 		ENICPMD_LOG(ERR, "cannot query counter: rc=%d handle=0x%x",
2532 			    rc, fm_flow->counter->handle);
2533 		goto exit;
2534 	}
2535 	query->hits_set = 1;
2536 	query->hits = args[0];
2537 	query->bytes_set = 1;
2538 	query->bytes = args[1];
2539 	rc = 0;
2540 exit:
2541 	end_fm(fm);
2542 	return rc;
2543 }
2544 
2545 static int
enic_fm_flow_query(struct rte_eth_dev * dev,struct rte_flow * flow,const struct rte_flow_action * actions,void * data,struct rte_flow_error * error)2546 enic_fm_flow_query(struct rte_eth_dev *dev,
2547 		   struct rte_flow *flow,
2548 		   const struct rte_flow_action *actions,
2549 		   void *data,
2550 		   struct rte_flow_error *error)
2551 {
2552 	int ret = 0;
2553 
2554 	ENICPMD_FUNC_TRACE();
2555 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2556 		switch (actions->type) {
2557 		case RTE_FLOW_ACTION_TYPE_VOID:
2558 			break;
2559 		case RTE_FLOW_ACTION_TYPE_COUNT:
2560 			ret = enic_fm_flow_query_count(dev, flow, data, error);
2561 			break;
2562 		default:
2563 			return rte_flow_error_set(error, ENOTSUP,
2564 						  RTE_FLOW_ERROR_TYPE_ACTION,
2565 						  actions,
2566 						  "action not supported");
2567 		}
2568 		if (ret < 0)
2569 			return ret;
2570 	}
2571 	return 0;
2572 }
2573 
2574 static struct rte_flow *
enic_fm_flow_create(struct rte_eth_dev * dev,const struct rte_flow_attr * attrs,const struct rte_flow_item pattern[],const struct rte_flow_action actions[],struct rte_flow_error * error)2575 enic_fm_flow_create(struct rte_eth_dev *dev,
2576 		    const struct rte_flow_attr *attrs,
2577 		    const struct rte_flow_item pattern[],
2578 		    const struct rte_flow_action actions[],
2579 		    struct rte_flow_error *error)
2580 {
2581 	struct fm_tcam_match_entry *fm_tcam_entry;
2582 	struct fm_action *fm_action;
2583 	struct enic_flowman *fm;
2584 	struct enic_fm_fet *fet;
2585 	struct rte_flow *flow;
2586 	struct enic *enic;
2587 	int ret;
2588 
2589 	ENICPMD_FUNC_TRACE();
2590 	enic = pmd_priv(dev);
2591 	fm = begin_fm(enic);
2592 	if (fm == NULL) {
2593 		rte_flow_error_set(error, ENOTSUP,
2594 			RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2595 			"flowman is not initialized");
2596 		return NULL;
2597 	}
2598 	enic_fm_open_scratch(fm);
2599 	flow = NULL;
2600 	ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2601 	if (ret < 0)
2602 		goto error_with_scratch;
2603 	fm_tcam_entry = &fm->tcam_entry;
2604 	fm_action = &fm->action;
2605 	flow = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2606 				      attrs, error);
2607 	if (flow) {
2608 		/* Add ingress rule that pairs with hairpin rule */
2609 		if (fm->need_hairpin_steer) {
2610 			ret = add_hairpin_steer(fm, flow, error);
2611 			if (ret) {
2612 				enic_fm_flow_free(fm, flow);
2613 				flow = NULL;
2614 				goto error_with_scratch;
2615 			}
2616 		}
2617 		LIST_INSERT_HEAD(&enic->flows, flow, next);
2618 		fet = flow->fm->fet;
2619 		if (fet && fet->default_key) {
2620 			/*
2621 			 * Jump to non-existent group? Save the relevant info
2622 			 * so we can convert this flow when that group
2623 			 * materializes.
2624 			 */
2625 			save_jump_flow(fm, flow, fet->group,
2626 				       fm_tcam_entry, fm_action);
2627 		} else if (fet && fet->ref == 1) {
2628 			/*
2629 			 * A new table is created. Convert the saved flows
2630 			 * that should jump to this group.
2631 			 */
2632 			convert_jump_flows(fm, fet, error);
2633 		}
2634 	}
2635 
2636 error_with_scratch:
2637 	enic_fm_close_scratch(fm);
2638 	end_fm(fm);
2639 	return flow;
2640 }
2641 
2642 static int
enic_fm_flow_destroy(struct rte_eth_dev * dev,struct rte_flow * flow,__rte_unused struct rte_flow_error * error)2643 enic_fm_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
2644 		     __rte_unused struct rte_flow_error *error)
2645 {
2646 	struct enic *enic = pmd_priv(dev);
2647 	struct enic_flowman *fm;
2648 
2649 	ENICPMD_FUNC_TRACE();
2650 	fm = begin_fm(enic);
2651 	if (fm == NULL)
2652 		return 0;
2653 	LIST_REMOVE(flow, next);
2654 	enic_fm_flow_free(fm, flow);
2655 	end_fm(fm);
2656 	return 0;
2657 }
2658 
2659 static int
enic_fm_flow_flush(struct rte_eth_dev * dev,__rte_unused struct rte_flow_error * error)2660 enic_fm_flow_flush(struct rte_eth_dev *dev,
2661 		   __rte_unused struct rte_flow_error *error)
2662 {
2663 	LIST_HEAD(enic_flows, rte_flow) internal;
2664 	struct enic_fm_flow *fm_flow;
2665 	struct enic_flowman *fm;
2666 	struct rte_flow *flow;
2667 	struct enic *enic = pmd_priv(dev);
2668 
2669 	ENICPMD_FUNC_TRACE();
2670 
2671 	fm = begin_fm(enic);
2672 	if (fm == NULL)
2673 		return 0;
2674 	/* Destroy all non-internal flows */
2675 	LIST_INIT(&internal);
2676 	while (!LIST_EMPTY(&enic->flows)) {
2677 		flow = LIST_FIRST(&enic->flows);
2678 		fm_flow = flow->fm;
2679 		LIST_REMOVE(flow, next);
2680 		if (flow->internal) {
2681 			LIST_INSERT_HEAD(&internal, flow, next);
2682 			continue;
2683 		}
2684 		/*
2685 		 * If tables are null, then vNIC is closing, and the firmware
2686 		 * has already cleaned up flowman state. So do not try to free
2687 		 * resources, as it only causes errors.
2688 		 */
2689 		if (fm->ig_tcam_hndl == FM_INVALID_HANDLE) {
2690 			fm_flow->entry_handle = FM_INVALID_HANDLE;
2691 			fm_flow->action = NULL;
2692 			fm_flow->fet = NULL;
2693 		}
2694 		enic_fm_flow_free(fm, flow);
2695 	}
2696 	while (!LIST_EMPTY(&internal)) {
2697 		flow = LIST_FIRST(&internal);
2698 		LIST_REMOVE(flow, next);
2699 		LIST_INSERT_HEAD(&enic->flows, flow, next);
2700 	}
2701 	end_fm(fm);
2702 	return 0;
2703 }
2704 
2705 static int
enic_fm_tbl_free(struct enic_flowman * fm,uint64_t handle)2706 enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle)
2707 {
2708 	uint64_t args[2];
2709 	int rc;
2710 
2711 	args[0] = FM_MATCH_TABLE_FREE;
2712 	args[1] = handle;
2713 	rc = flowman_cmd(fm, args, 2);
2714 	if (rc)
2715 		ENICPMD_LOG(ERR, "cannot free table: rc=%d handle=0x%" PRIx64,
2716 			    rc, handle);
2717 	return rc;
2718 }
2719 
2720 static int
enic_fm_tcam_tbl_alloc(struct enic_flowman * fm,uint32_t direction,uint32_t max_entries,uint64_t * handle)2721 enic_fm_tcam_tbl_alloc(struct enic_flowman *fm, uint32_t direction,
2722 			uint32_t max_entries, uint64_t *handle)
2723 {
2724 	struct fm_tcam_match_table *tcam_tbl;
2725 	uint64_t args[2];
2726 	int rc;
2727 
2728 	ENICPMD_FUNC_TRACE();
2729 	tcam_tbl = &fm->cmd.va->fm_tcam_match_table;
2730 	tcam_tbl->ftt_direction = direction;
2731 	tcam_tbl->ftt_stage = FM_STAGE_LAST;
2732 	tcam_tbl->ftt_max_entries = max_entries;
2733 	args[0] = FM_TCAM_TABLE_ALLOC;
2734 	args[1] = fm->cmd.pa;
2735 	rc = flowman_cmd(fm, args, 2);
2736 	if (rc) {
2737 		ENICPMD_LOG(ERR, "cannot alloc %s TCAM table: rc=%d",
2738 			    (direction == FM_INGRESS) ? "IG" : "EG", rc);
2739 		return rc;
2740 	}
2741 	*handle = args[0];
2742 	ENICPMD_LOG(DEBUG, "%s TCAM table allocated, handle=0x%" PRIx64,
2743 		    (direction == FM_INGRESS) ? "IG" : "EG", *handle);
2744 	return 0;
2745 }
2746 
2747 static int
enic_fm_init_actions(struct enic_flowman * fm)2748 enic_fm_init_actions(struct enic_flowman *fm)
2749 {
2750 	struct rte_hash *a_hash;
2751 	char name[RTE_HASH_NAMESIZE];
2752 	struct rte_hash_parameters params = {
2753 		.entries = FM_MAX_ACTION_TABLE_SIZE,
2754 		.key_len = sizeof(struct fm_action),
2755 		.hash_func = rte_jhash,
2756 		.hash_func_init_val = 0,
2757 		.socket_id = rte_socket_id(),
2758 	};
2759 
2760 	ENICPMD_FUNC_TRACE();
2761 	snprintf((char *)name, sizeof(name), "fm-ah-%s",
2762 		 fm->owner_enic->bdf_name);
2763 	params.name = name;
2764 
2765 	a_hash = rte_hash_create(&params);
2766 	if (a_hash == NULL)
2767 		return -rte_errno;
2768 	fm->action_hash = a_hash;
2769 	return 0;
2770 }
2771 
2772 static int
enic_fm_init_counters(struct enic_flowman * fm)2773 enic_fm_init_counters(struct enic_flowman *fm)
2774 {
2775 	ENICPMD_FUNC_TRACE();
2776 	SLIST_INIT(&fm->counters);
2777 	return enic_fm_more_counters(fm);
2778 }
2779 
2780 static void
enic_fm_free_all_counters(struct enic_flowman * fm)2781 enic_fm_free_all_counters(struct enic_flowman *fm)
2782 {
2783 	uint64_t args[2];
2784 	int rc;
2785 
2786 	args[0] = FM_COUNTER_BRK;
2787 	args[1] = 0;
2788 	rc = flowman_cmd(fm, args, 2);
2789 	if (rc != 0)
2790 		ENICPMD_LOG(ERR, "cannot free counters: rc=%d", rc);
2791 	rte_free(fm->counter_stack);
2792 }
2793 
2794 static int
enic_fm_alloc_tcam_tables(struct enic_flowman * fm)2795 enic_fm_alloc_tcam_tables(struct enic_flowman *fm)
2796 {
2797 	int rc;
2798 
2799 	ENICPMD_FUNC_TRACE();
2800 	rc = enic_fm_tcam_tbl_alloc(fm, FM_INGRESS, FM_MAX_TCAM_TABLE_SIZE,
2801 				    &fm->ig_tcam_hndl);
2802 	if (rc)
2803 		return rc;
2804 	rc = enic_fm_tcam_tbl_alloc(fm, FM_EGRESS, FM_MAX_TCAM_TABLE_SIZE,
2805 				    &fm->eg_tcam_hndl);
2806 	return rc;
2807 }
2808 
2809 static void
enic_fm_free_tcam_tables(struct enic_flowman * fm)2810 enic_fm_free_tcam_tables(struct enic_flowman *fm)
2811 {
2812 	ENICPMD_FUNC_TRACE();
2813 	if (fm->ig_tcam_hndl) {
2814 		ENICPMD_LOG(DEBUG, "free IG TCAM table handle=0x%" PRIx64,
2815 			    fm->ig_tcam_hndl);
2816 		enic_fm_tbl_free(fm, fm->ig_tcam_hndl);
2817 		fm->ig_tcam_hndl = FM_INVALID_HANDLE;
2818 	}
2819 	if (fm->eg_tcam_hndl) {
2820 		ENICPMD_LOG(DEBUG, "free EG TCAM table handle=0x%" PRIx64,
2821 			    fm->eg_tcam_hndl);
2822 		enic_fm_tbl_free(fm, fm->eg_tcam_hndl);
2823 		fm->eg_tcam_hndl = FM_INVALID_HANDLE;
2824 	}
2825 }
2826 
2827 int
enic_fm_init(struct enic * enic)2828 enic_fm_init(struct enic *enic)
2829 {
2830 	const struct rte_pci_addr *addr;
2831 	struct enic_flowman *fm;
2832 	uint8_t name[RTE_MEMZONE_NAMESIZE];
2833 	int rc;
2834 
2835 	if (enic->flow_filter_mode != FILTER_FLOWMAN)
2836 		return 0;
2837 	ENICPMD_FUNC_TRACE();
2838 	/* Get vnic handle and save for port-id action */
2839 	if (enic_is_vf_rep(enic))
2840 		addr = &VF_ENIC_TO_VF_REP(enic)->bdf;
2841 	else
2842 		addr = &RTE_ETH_DEV_TO_PCI(enic->rte_dev)->addr;
2843 	rc = enic_fm_find_vnic(enic, addr, &enic->fm_vnic_handle);
2844 	if (rc) {
2845 		ENICPMD_LOG(ERR, "cannot find vnic handle for %x:%x:%x",
2846 			    addr->bus, addr->devid, addr->function);
2847 		return rc;
2848 	}
2849 	/* Save UIF for egport action */
2850 	enic->fm_vnic_uif = vnic_dev_uif(enic->vdev);
2851 	ENICPMD_LOG(DEBUG, "uif %u", enic->fm_vnic_uif);
2852 	/* Nothing else to do for representor. It will share the PF flowman */
2853 	if (enic_is_vf_rep(enic))
2854 		return 0;
2855 	fm = calloc(1, sizeof(*fm));
2856 	if (fm == NULL) {
2857 		ENICPMD_LOG(ERR, "cannot alloc flowman struct");
2858 		return -ENOMEM;
2859 	}
2860 	fm->owner_enic = enic;
2861 	rte_spinlock_init(&fm->lock);
2862 	TAILQ_INIT(&fm->fet_list);
2863 	TAILQ_INIT(&fm->jump_list);
2864 	/* Allocate host memory for flowman commands */
2865 	snprintf((char *)name, sizeof(name), "fm-cmd-%s", enic->bdf_name);
2866 	fm->cmd.va = enic_alloc_consistent(enic,
2867 		sizeof(union enic_flowman_cmd_mem), &fm->cmd.pa, name);
2868 	if (!fm->cmd.va) {
2869 		ENICPMD_LOG(ERR, "cannot allocate flowman command memory");
2870 		rc = -ENOMEM;
2871 		goto error_fm;
2872 	}
2873 	/* Allocate TCAM tables upfront as they are the main tables */
2874 	rc = enic_fm_alloc_tcam_tables(fm);
2875 	if (rc) {
2876 		ENICPMD_LOG(ERR, "cannot alloc TCAM tables");
2877 		goto error_cmd;
2878 	}
2879 	/* Then a number of counters */
2880 	rc = enic_fm_init_counters(fm);
2881 	if (rc) {
2882 		ENICPMD_LOG(ERR, "cannot alloc counters");
2883 		goto error_tables;
2884 	}
2885 	/* set up action handle hash */
2886 	rc = enic_fm_init_actions(fm);
2887 	if (rc) {
2888 		ENICPMD_LOG(ERR, "cannot create action hash, error:%d", rc);
2889 		goto error_tables;
2890 	}
2891 	/*
2892 	 * One default exact match table for each direction. We hold onto
2893 	 * it until close.
2894 	 */
2895 	rc = enic_fet_alloc(fm, 1, NULL, 128, &fm->default_ig_fet);
2896 	if (rc) {
2897 		ENICPMD_LOG(ERR, "cannot alloc default IG exact match table");
2898 		goto error_counters;
2899 	}
2900 	fm->default_ig_fet->ref = 1;
2901 	rc = enic_fet_alloc(fm, 0, NULL, 128, &fm->default_eg_fet);
2902 	if (rc) {
2903 		ENICPMD_LOG(ERR, "cannot alloc default EG exact match table");
2904 		goto error_ig_fet;
2905 	}
2906 	fm->default_eg_fet->ref = 1;
2907 	fm->vf_rep_tag = FM_VF_REP_TAG;
2908 	enic->fm = fm;
2909 	return 0;
2910 
2911 error_ig_fet:
2912 	enic_fet_free(fm, fm->default_ig_fet);
2913 error_counters:
2914 	enic_fm_free_all_counters(fm);
2915 error_tables:
2916 	enic_fm_free_tcam_tables(fm);
2917 error_cmd:
2918 	enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2919 		fm->cmd.va, fm->cmd.pa);
2920 error_fm:
2921 	free(fm);
2922 	return rc;
2923 }
2924 
2925 void
enic_fm_destroy(struct enic * enic)2926 enic_fm_destroy(struct enic *enic)
2927 {
2928 	struct enic_flowman *fm;
2929 	struct enic_fm_fet *fet;
2930 
2931 	ENICPMD_FUNC_TRACE();
2932 	if (enic_is_vf_rep(enic)) {
2933 		delete_rep_flows(enic);
2934 		return;
2935 	}
2936 	if (enic->fm == NULL)
2937 		return;
2938 	fm = enic->fm;
2939 	enic_fm_flow_flush(enic->rte_dev, NULL);
2940 	enic_fet_free(fm, fm->default_eg_fet);
2941 	enic_fet_free(fm, fm->default_ig_fet);
2942 	/* Free all exact match tables still open */
2943 	while (!TAILQ_EMPTY(&fm->fet_list)) {
2944 		fet = TAILQ_FIRST(&fm->fet_list);
2945 		enic_fet_free(fm, fet);
2946 	}
2947 	enic_fm_free_tcam_tables(fm);
2948 	enic_fm_free_all_counters(fm);
2949 	rte_hash_free(fm->action_hash);
2950 	enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2951 		fm->cmd.va, fm->cmd.pa);
2952 	fm->cmd.va = NULL;
2953 	free(fm);
2954 	enic->fm = NULL;
2955 }
2956 
2957 int
enic_fm_allocate_switch_domain(struct enic * pf)2958 enic_fm_allocate_switch_domain(struct enic *pf)
2959 {
2960 	const struct rte_pci_addr *cur_a, *prev_a;
2961 	struct rte_eth_dev *dev;
2962 	struct enic *cur, *prev;
2963 	uint16_t domain_id;
2964 	uint64_t vnic_h;
2965 	uint16_t pid;
2966 	int ret;
2967 
2968 	ENICPMD_FUNC_TRACE();
2969 	if (enic_is_vf_rep(pf))
2970 		return -EINVAL;
2971 	cur = pf;
2972 	cur_a = &RTE_ETH_DEV_TO_PCI(cur->rte_dev)->addr;
2973 	/* Go through ports and find another PF that is on the same adapter */
2974 	RTE_ETH_FOREACH_DEV(pid) {
2975 		dev = &rte_eth_devices[pid];
2976 		if (!dev_is_enic(dev))
2977 			continue;
2978 		if (dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)
2979 			continue;
2980 		if (dev == cur->rte_dev)
2981 			continue;
2982 		/* dev is another PF. Is it on the same adapter? */
2983 		prev = pmd_priv(dev);
2984 		prev_a = &RTE_ETH_DEV_TO_PCI(dev)->addr;
2985 		if (!enic_fm_find_vnic(cur, prev_a, &vnic_h)) {
2986 			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",
2987 				cur->rte_dev->data->port_id,
2988 				cur_a->bus, cur_a->devid, cur_a->function,
2989 				dev->data->port_id,
2990 				prev_a->bus, prev_a->devid, prev_a->function,
2991 				prev->switch_domain_id);
2992 			cur->switch_domain_id = prev->switch_domain_id;
2993 			return 0;
2994 		}
2995 	}
2996 	ret = rte_eth_switch_domain_alloc(&domain_id);
2997 	if (ret) {
2998 		ENICPMD_LOG(WARNING, "failed to allocate switch domain for device %d",
2999 			    ret);
3000 	}
3001 	cur->switch_domain_id = domain_id;
3002 	ENICPMD_LOG(DEBUG, "Port %u (PF BDF %x:%x:%x) is the 1st PF on the VIC. Allocated switch domain id %u",
3003 		    cur->rte_dev->data->port_id,
3004 		    cur_a->bus, cur_a->devid, cur_a->function,
3005 		    domain_id);
3006 	return ret;
3007 }
3008 
3009 const struct rte_flow_ops enic_fm_flow_ops = {
3010 	.validate = enic_fm_flow_validate,
3011 	.create = enic_fm_flow_create,
3012 	.destroy = enic_fm_flow_destroy,
3013 	.flush = enic_fm_flow_flush,
3014 	.query = enic_fm_flow_query,
3015 };
3016 
3017 /* Add a high priority flow that loops representor packets to VF */
3018 int
enic_fm_add_rep2vf_flow(struct enic_vf_representor * vf)3019 enic_fm_add_rep2vf_flow(struct enic_vf_representor *vf)
3020 {
3021 	struct fm_tcam_match_entry *fm_tcam_entry;
3022 	struct rte_flow *flow0, *flow1;
3023 	struct fm_action *fm_action;
3024 	struct rte_flow_error error;
3025 	struct rte_flow_attr attrs;
3026 	struct fm_action_op fm_op;
3027 	struct enic_flowman *fm;
3028 	struct enic *pf;
3029 	uint8_t tag;
3030 
3031 	pf = vf->pf;
3032 	fm = pf->fm;
3033 	tag = fm->vf_rep_tag;
3034 	enic_fm_open_scratch(fm);
3035 	fm_tcam_entry = &fm->tcam_entry;
3036 	fm_action = &fm->action;
3037 	/* Egress rule: match WQ ID and tag+hairpin */
3038 	fm_tcam_entry->ftm_data.fk_wq_id = vf->pf_wq_idx;
3039 	fm_tcam_entry->ftm_mask.fk_wq_id = 0xffff;
3040 	fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
3041 	memset(&fm_op, 0, sizeof(fm_op));
3042 	fm_op.fa_op = FMOP_TAG;
3043 	fm_op.tag.tag = tag;
3044 	enic_fm_append_action_op(fm, &fm_op, &error);
3045 	memset(&fm_op, 0, sizeof(fm_op));
3046 	fm_op.fa_op = FMOP_EG_HAIRPIN;
3047 	enic_fm_append_action_op(fm, &fm_op, &error);
3048 	memset(&fm_op, 0, sizeof(fm_op));
3049 	fm_op.fa_op = FMOP_END;
3050 	enic_fm_append_action_op(fm, &fm_op, &error);
3051 	attrs.group = 0;
3052 	attrs.ingress = 0;
3053 	attrs.egress = 1;
3054 	attrs.priority = FM_HIGHEST_PRIORITY;
3055 	flow0 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
3056 				       &attrs, &error);
3057 	enic_fm_close_scratch(fm);
3058 	if (flow0 == NULL) {
3059 		ENICPMD_LOG(ERR, "Cannot create flow 0 for representor->VF");
3060 		return -EINVAL;
3061 	}
3062 	LIST_INSERT_HEAD(&pf->flows, flow0, next);
3063 	/* Make this flow internal, so the user app cannot delete it */
3064 	flow0->internal = 1;
3065 	ENICPMD_LOG(DEBUG, "representor->VF %d flow created: wq %d -> tag %d hairpin",
3066 		    vf->vf_id, vf->pf_wq_idx, tag);
3067 
3068 	/* Ingress: steer hairpinned to VF RQ 0 */
3069 	enic_fm_open_scratch(fm);
3070 	fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
3071 	fm_tcam_entry->ftm_data.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
3072 	fm_tcam_entry->ftm_mask.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
3073 	fm_tcam_entry->ftm_data.fk_packet_tag = tag;
3074 	fm_tcam_entry->ftm_mask.fk_packet_tag = 0xff;
3075 	memset(&fm_op, 0, sizeof(fm_op));
3076 	fm_op.fa_op = FMOP_RQ_STEER;
3077 	fm_op.rq_steer.rq_index = 0;
3078 	fm_op.rq_steer.vnic_handle = vf->enic.fm_vnic_handle;
3079 	enic_fm_append_action_op(fm, &fm_op, &error);
3080 	memset(&fm_op, 0, sizeof(fm_op));
3081 	fm_op.fa_op = FMOP_END;
3082 	enic_fm_append_action_op(fm, &fm_op, &error);
3083 	attrs.group = 0;
3084 	attrs.ingress = 1;
3085 	attrs.egress = 0;
3086 	attrs.priority = FM_HIGHEST_PRIORITY;
3087 	flow1 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
3088 				       &attrs, &error);
3089 	enic_fm_close_scratch(fm);
3090 	if (flow1 == NULL) {
3091 		ENICPMD_LOG(ERR, "Cannot create flow 1 for representor->VF");
3092 		enic_fm_flow_destroy(pf->rte_dev, flow0, &error);
3093 		return -EINVAL;
3094 	}
3095 	LIST_INSERT_HEAD(&pf->flows, flow1, next);
3096 	flow1->internal = 1;
3097 	ENICPMD_LOG(DEBUG, "representor->VF %d flow created: tag %d hairpinned -> VF RQ %d",
3098 		    vf->vf_id, tag, fm_op.rq_steer.rq_index);
3099 	vf->rep2vf_flow[0] = flow0;
3100 	vf->rep2vf_flow[1] = flow1;
3101 	/* Done with this tag, use a different one next time */
3102 	fm->vf_rep_tag++;
3103 	return 0;
3104 }
3105 
3106 /*
3107  * Add a low priority flow that matches all packets from VF and loops them
3108  * back to the representor.
3109  */
3110 int
enic_fm_add_vf2rep_flow(struct enic_vf_representor * vf)3111 enic_fm_add_vf2rep_flow(struct enic_vf_representor *vf)
3112 {
3113 	struct fm_tcam_match_entry *fm_tcam_entry;
3114 	struct rte_flow *flow0, *flow1;
3115 	struct fm_action *fm_action;
3116 	struct rte_flow_error error;
3117 	struct rte_flow_attr attrs;
3118 	struct fm_action_op fm_op;
3119 	struct enic_flowman *fm;
3120 	struct enic *pf;
3121 	uint8_t tag;
3122 
3123 	pf = vf->pf;
3124 	fm = pf->fm;
3125 	tag = fm->vf_rep_tag;
3126 	enic_fm_open_scratch(fm);
3127 	fm_tcam_entry = &fm->tcam_entry;
3128 	fm_action = &fm->action;
3129 	/* Egress rule: match-any and tag+hairpin */
3130 	fm_tcam_entry->ftm_data.fk_wq_id = 0;
3131 	fm_tcam_entry->ftm_mask.fk_wq_id = 0xffff;
3132 	fm_tcam_entry->ftm_data.fk_wq_vnic = vf->enic.fm_vnic_handle;
3133 	fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
3134 	memset(&fm_op, 0, sizeof(fm_op));
3135 	fm_op.fa_op = FMOP_TAG;
3136 	fm_op.tag.tag = tag;
3137 	enic_fm_append_action_op(fm, &fm_op, &error);
3138 	memset(&fm_op, 0, sizeof(fm_op));
3139 	fm_op.fa_op = FMOP_EG_HAIRPIN;
3140 	enic_fm_append_action_op(fm, &fm_op, &error);
3141 	memset(&fm_op, 0, sizeof(fm_op));
3142 	fm_op.fa_op = FMOP_END;
3143 	enic_fm_append_action_op(fm, &fm_op, &error);
3144 	attrs.group = 0;
3145 	attrs.ingress = 0;
3146 	attrs.egress = 1;
3147 	attrs.priority = FM_LOWEST_PRIORITY;
3148 	flow0 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
3149 				       &attrs, &error);
3150 	enic_fm_close_scratch(fm);
3151 	if (flow0 == NULL) {
3152 		ENICPMD_LOG(ERR, "Cannot create flow 0 for VF->representor");
3153 		return -EINVAL;
3154 	}
3155 	LIST_INSERT_HEAD(&pf->flows, flow0, next);
3156 	/* Make this flow internal, so the user app cannot delete it */
3157 	flow0->internal = 1;
3158 	ENICPMD_LOG(DEBUG, "VF %d->representor flow created: wq %d (low prio) -> tag %d hairpin",
3159 		    vf->vf_id, fm_tcam_entry->ftm_data.fk_wq_id, tag);
3160 
3161 	/* Ingress: steer hairpinned to VF rep RQ */
3162 	enic_fm_open_scratch(fm);
3163 	fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
3164 	fm_tcam_entry->ftm_data.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
3165 	fm_tcam_entry->ftm_mask.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
3166 	fm_tcam_entry->ftm_data.fk_packet_tag = tag;
3167 	fm_tcam_entry->ftm_mask.fk_packet_tag = 0xff;
3168 	memset(&fm_op, 0, sizeof(fm_op));
3169 	fm_op.fa_op = FMOP_RQ_STEER;
3170 	fm_op.rq_steer.rq_index = vf->pf_rq_sop_idx;
3171 	fm_op.rq_steer.vnic_handle = pf->fm_vnic_handle;
3172 	enic_fm_append_action_op(fm, &fm_op, &error);
3173 	memset(&fm_op, 0, sizeof(fm_op));
3174 	fm_op.fa_op = FMOP_END;
3175 	enic_fm_append_action_op(fm, &fm_op, &error);
3176 	attrs.group = 0;
3177 	attrs.ingress = 1;
3178 	attrs.egress = 0;
3179 	attrs.priority = FM_HIGHEST_PRIORITY;
3180 	flow1 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
3181 				       &attrs, &error);
3182 	enic_fm_close_scratch(fm);
3183 	if (flow1 == NULL) {
3184 		ENICPMD_LOG(ERR, "Cannot create flow 1 for VF->representor");
3185 		enic_fm_flow_destroy(pf->rte_dev, flow0, &error);
3186 		return -EINVAL;
3187 	}
3188 	LIST_INSERT_HEAD(&pf->flows, flow1, next);
3189 	flow1->internal = 1;
3190 	ENICPMD_LOG(DEBUG, "VF %d->representor flow created: tag %d hairpinned -> PF RQ %d",
3191 		    vf->vf_id, tag, vf->pf_rq_sop_idx);
3192 	vf->vf2rep_flow[0] = flow0;
3193 	vf->vf2rep_flow[1] = flow1;
3194 	/* Done with this tag, use a different one next time */
3195 	fm->vf_rep_tag++;
3196 	return 0;
3197 }
3198 
3199 /* Destroy representor flows created by enic_fm_add_{rep2vf,vf2rep}_flow */
3200 static void
delete_rep_flows(struct enic * enic)3201 delete_rep_flows(struct enic *enic)
3202 {
3203 	struct enic_vf_representor *vf;
3204 	struct rte_flow_error error;
3205 	struct rte_eth_dev *dev;
3206 	uint32_t i;
3207 
3208 	RTE_ASSERT(enic_is_vf_rep(enic));
3209 	vf = VF_ENIC_TO_VF_REP(enic);
3210 	dev = vf->pf->rte_dev;
3211 	for (i = 0; i < ARRAY_SIZE(vf->vf2rep_flow); i++) {
3212 		if (vf->vf2rep_flow[i])
3213 			enic_fm_flow_destroy(dev, vf->vf2rep_flow[i], &error);
3214 	}
3215 	for (i = 0; i < ARRAY_SIZE(vf->rep2vf_flow); i++) {
3216 		if (vf->rep2vf_flow[i])
3217 			enic_fm_flow_destroy(dev, vf->rep2vf_flow[i], &error);
3218 	}
3219 }
3220 
3221 static struct enic_flowman *
begin_fm(struct enic * enic)3222 begin_fm(struct enic *enic)
3223 {
3224 	struct enic_vf_representor *vf;
3225 	struct enic_flowman *fm;
3226 
3227 	/* Representor uses PF flowman */
3228 	if (enic_is_vf_rep(enic)) {
3229 		vf = VF_ENIC_TO_VF_REP(enic);
3230 		fm = vf->pf->fm;
3231 	} else {
3232 		fm = enic->fm;
3233 	}
3234 	/* Save the API caller and lock if representors exist */
3235 	if (fm) {
3236 		if (fm->owner_enic->switchdev_mode)
3237 			rte_spinlock_lock(&fm->lock);
3238 		fm->user_enic = enic;
3239 	}
3240 	return fm;
3241 }
3242 
3243 static void
end_fm(struct enic_flowman * fm)3244 end_fm(struct enic_flowman *fm)
3245 {
3246 	fm->user_enic = NULL;
3247 	if (fm->owner_enic->switchdev_mode)
3248 		rte_spinlock_unlock(&fm->lock);
3249 }
3250