1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
5 #ifndef RTE_PMD_MLX5_FLOW_H_
6 #define RTE_PMD_MLX5_FLOW_H_
8 #include <netinet/in.h>
15 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
17 #pragma GCC diagnostic ignored "-Wpedantic"
19 #include <infiniband/verbs.h>
21 #pragma GCC diagnostic error "-Wpedantic"
24 /* Pattern outer Layer bits. */
25 #define MLX5_FLOW_LAYER_OUTER_L2 (1u << 0)
26 #define MLX5_FLOW_LAYER_OUTER_L3_IPV4 (1u << 1)
27 #define MLX5_FLOW_LAYER_OUTER_L3_IPV6 (1u << 2)
28 #define MLX5_FLOW_LAYER_OUTER_L4_UDP (1u << 3)
29 #define MLX5_FLOW_LAYER_OUTER_L4_TCP (1u << 4)
30 #define MLX5_FLOW_LAYER_OUTER_VLAN (1u << 5)
32 /* Pattern inner Layer bits. */
33 #define MLX5_FLOW_LAYER_INNER_L2 (1u << 6)
34 #define MLX5_FLOW_LAYER_INNER_L3_IPV4 (1u << 7)
35 #define MLX5_FLOW_LAYER_INNER_L3_IPV6 (1u << 8)
36 #define MLX5_FLOW_LAYER_INNER_L4_UDP (1u << 9)
37 #define MLX5_FLOW_LAYER_INNER_L4_TCP (1u << 10)
38 #define MLX5_FLOW_LAYER_INNER_VLAN (1u << 11)
40 /* Pattern tunnel Layer bits. */
41 #define MLX5_FLOW_LAYER_VXLAN (1u << 12)
42 #define MLX5_FLOW_LAYER_VXLAN_GPE (1u << 13)
43 #define MLX5_FLOW_LAYER_GRE (1u << 14)
44 #define MLX5_FLOW_LAYER_MPLS (1u << 15)
47 #define MLX5_FLOW_LAYER_OUTER_L3 \
48 (MLX5_FLOW_LAYER_OUTER_L3_IPV4 | MLX5_FLOW_LAYER_OUTER_L3_IPV6)
49 #define MLX5_FLOW_LAYER_OUTER_L4 \
50 (MLX5_FLOW_LAYER_OUTER_L4_UDP | MLX5_FLOW_LAYER_OUTER_L4_TCP)
51 #define MLX5_FLOW_LAYER_OUTER \
52 (MLX5_FLOW_LAYER_OUTER_L2 | MLX5_FLOW_LAYER_OUTER_L3 | \
53 MLX5_FLOW_LAYER_OUTER_L4)
56 #define MLX5_FLOW_LAYER_TUNNEL \
57 (MLX5_FLOW_LAYER_VXLAN | MLX5_FLOW_LAYER_VXLAN_GPE | \
58 MLX5_FLOW_LAYER_GRE | MLX5_FLOW_LAYER_MPLS)
61 #define MLX5_FLOW_LAYER_INNER_L3 \
62 (MLX5_FLOW_LAYER_INNER_L3_IPV4 | MLX5_FLOW_LAYER_INNER_L3_IPV6)
63 #define MLX5_FLOW_LAYER_INNER_L4 \
64 (MLX5_FLOW_LAYER_INNER_L4_UDP | MLX5_FLOW_LAYER_INNER_L4_TCP)
65 #define MLX5_FLOW_LAYER_INNER \
66 (MLX5_FLOW_LAYER_INNER_L2 | MLX5_FLOW_LAYER_INNER_L3 | \
67 MLX5_FLOW_LAYER_INNER_L4)
70 #define MLX5_FLOW_ACTION_DROP (1u << 0)
71 #define MLX5_FLOW_ACTION_QUEUE (1u << 1)
72 #define MLX5_FLOW_ACTION_RSS (1u << 2)
73 #define MLX5_FLOW_ACTION_FLAG (1u << 3)
74 #define MLX5_FLOW_ACTION_MARK (1u << 4)
75 #define MLX5_FLOW_ACTION_COUNT (1u << 5)
76 #define MLX5_FLOW_ACTION_PORT_ID (1u << 6)
77 #define MLX5_FLOW_ACTION_OF_POP_VLAN (1u << 7)
78 #define MLX5_FLOW_ACTION_OF_PUSH_VLAN (1u << 8)
79 #define MLX5_FLOW_ACTION_OF_SET_VLAN_VID (1u << 9)
80 #define MLX5_FLOW_ACTION_OF_SET_VLAN_PCP (1u << 10)
81 #define MLX5_FLOW_ACTION_SET_IPV4_SRC (1u << 11)
82 #define MLX5_FLOW_ACTION_SET_IPV4_DST (1u << 12)
83 #define MLX5_FLOW_ACTION_SET_IPV6_SRC (1u << 13)
84 #define MLX5_FLOW_ACTION_SET_IPV6_DST (1u << 14)
85 #define MLX5_FLOW_ACTION_SET_TP_SRC (1u << 15)
86 #define MLX5_FLOW_ACTION_SET_TP_DST (1u << 16)
87 #define MLX5_FLOW_ACTION_JUMP (1u << 17)
88 #define MLX5_FLOW_ACTION_SET_TTL (1u << 18)
89 #define MLX5_FLOW_ACTION_DEC_TTL (1u << 19)
90 #define MLX5_FLOW_ACTION_SET_MAC_SRC (1u << 20)
91 #define MLX5_FLOW_ACTION_SET_MAC_DST (1u << 21)
93 #define MLX5_FLOW_FATE_ACTIONS \
94 (MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)
97 #define IPPROTO_MPLS 137
100 /* UDP port numbers for VxLAN. */
101 #define MLX5_UDP_PORT_VXLAN 4789
102 #define MLX5_UDP_PORT_VXLAN_GPE 4790
104 /* Priority reserved for default flows. */
105 #define MLX5_FLOW_PRIO_RSVD ((uint32_t)-1)
108 * Number of sub priorities.
109 * For each kind of pattern matching i.e. L2, L3, L4 to have a correct
110 * matching on the NIC (firmware dependent) L4 most have the higher priority
111 * followed by L3 and ending with L2.
113 #define MLX5_PRIORITY_MAP_L2 2
114 #define MLX5_PRIORITY_MAP_L3 1
115 #define MLX5_PRIORITY_MAP_L4 0
116 #define MLX5_PRIORITY_MAP_MAX 3
118 /* Valid layer type for IPV4 RSS. */
119 #define MLX5_IPV4_LAYER_TYPES \
120 (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 | \
121 ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV4_UDP | \
122 ETH_RSS_NONFRAG_IPV4_OTHER)
124 /* IBV hash source bits for IPV4. */
125 #define MLX5_IPV4_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4)
127 /* Valid layer type for IPV6 RSS. */
128 #define MLX5_IPV6_LAYER_TYPES \
129 (ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 | ETH_RSS_NONFRAG_IPV6_TCP | \
130 ETH_RSS_NONFRAG_IPV6_UDP | ETH_RSS_IPV6_EX | ETH_RSS_IPV6_TCP_EX | \
131 ETH_RSS_IPV6_UDP_EX | ETH_RSS_NONFRAG_IPV6_OTHER)
133 /* IBV hash source bits for IPV6. */
134 #define MLX5_IPV6_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6)
136 /* Max number of actions per DV flow. */
137 #define MLX5_DV_MAX_NUMBER_OF_ACTIONS 8
139 enum mlx5_flow_drv_type {
143 MLX5_FLOW_TYPE_VERBS,
147 /* Matcher PRM representation */
148 struct mlx5_flow_dv_match_params {
150 /**< Size of match value. Do NOT split size and key! */
151 uint32_t buf[MLX5_ST_SZ_DW(fte_match_param)];
152 /**< Matcher value. This value is used as the mask or as a key. */
155 #define MLX5_DV_MAX_NUMBER_OF_ACTIONS 8
157 /* Matcher structure. */
158 struct mlx5_flow_dv_matcher {
159 LIST_ENTRY(mlx5_flow_dv_matcher) next;
160 /* Pointer to the next element. */
161 rte_atomic32_t refcnt; /**< Reference counter. */
162 void *matcher_object; /**< Pointer to DV matcher */
163 uint16_t crc; /**< CRC of key. */
164 uint16_t priority; /**< Priority of matcher. */
165 uint8_t egress; /**< Egress matcher. */
166 struct mlx5_flow_dv_match_params mask; /**< Matcher mask. */
169 /* DV flows structure. */
170 struct mlx5_flow_dv {
171 uint64_t hash_fields; /**< Fields that participate in the hash. */
172 struct mlx5_hrxq *hrxq; /**< Hash Rx queues. */
174 struct mlx5_flow_dv_matcher *matcher; /**< Cache to matcher. */
175 struct mlx5_flow_dv_match_params value;
176 /**< Holds the value that the packet is compared to. */
177 struct ibv_flow *flow; /**< Installed flow. */
178 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
179 struct mlx5dv_flow_action_attr actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS];
182 int actions_n; /**< number of actions. */
185 /** Linux TC flower driver for E-Switch flow. */
186 struct mlx5_flow_tcf {
187 struct nlmsghdr *nlh;
191 /* Verbs specification header. */
192 struct ibv_spec_header {
193 enum ibv_flow_spec_type type;
197 /** Handles information leading to a drop fate. */
198 struct mlx5_flow_verbs {
199 LIST_ENTRY(mlx5_flow_verbs) next;
200 unsigned int size; /**< Size of the attribute. */
202 struct ibv_flow_attr *attr;
203 /**< Pointer to the Specification buffer. */
204 uint8_t *specs; /**< Pointer to the specifications. */
206 struct ibv_flow *flow; /**< Verbs flow pointer. */
207 struct mlx5_hrxq *hrxq; /**< Hash Rx queue object. */
208 uint64_t hash_fields; /**< Verbs hash Rx queue hash fields. */
211 /** Device flow structure. */
213 LIST_ENTRY(mlx5_flow) next;
214 struct rte_flow *flow; /**< Pointer to the main flow. */
215 uint32_t layers; /**< Bit-fields that holds the detected layers. */
217 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
218 struct mlx5_flow_dv dv;
220 struct mlx5_flow_tcf tcf;
221 struct mlx5_flow_verbs verbs;
225 /* Counters information. */
226 struct mlx5_flow_counter {
227 LIST_ENTRY(mlx5_flow_counter) next; /**< Pointer to the next counter. */
228 uint32_t shared:1; /**< Share counter ID with other flow rules. */
229 uint32_t ref_cnt:31; /**< Reference counter. */
230 uint32_t id; /**< Counter ID. */
231 struct ibv_counter_set *cs; /**< Holds the counters for the rule. */
232 uint64_t hits; /**< Number of packets matched by the rule. */
233 uint64_t bytes; /**< Number of bytes matched by the rule. */
236 /* Flow structure. */
238 TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
239 enum mlx5_flow_drv_type drv_type; /**< Drvier type. */
241 /**< Bit-fields of present layers see MLX5_FLOW_LAYER_*. */
242 struct mlx5_flow_counter *counter; /**< Holds flow counter. */
243 struct rte_flow_action_rss rss;/**< RSS context. */
244 uint8_t key[MLX5_RSS_HASH_KEY_LEN]; /**< RSS hash key. */
245 uint16_t (*queue)[]; /**< Destination queues to redirect traffic to. */
246 LIST_HEAD(dev_flows, mlx5_flow) dev_flows;
247 /**< Device flows that are part of the flow. */
248 uint32_t actions; /**< Bit-fields which mark all detected actions. */
250 typedef int (*mlx5_flow_validate_t)(struct rte_eth_dev *dev,
251 const struct rte_flow_attr *attr,
252 const struct rte_flow_item items[],
253 const struct rte_flow_action actions[],
254 struct rte_flow_error *error);
255 typedef struct mlx5_flow *(*mlx5_flow_prepare_t)
256 (const struct rte_flow_attr *attr, const struct rte_flow_item items[],
257 const struct rte_flow_action actions[], uint64_t *item_flags,
258 uint64_t *action_flags, struct rte_flow_error *error);
259 typedef int (*mlx5_flow_translate_t)(struct rte_eth_dev *dev,
260 struct mlx5_flow *dev_flow,
261 const struct rte_flow_attr *attr,
262 const struct rte_flow_item items[],
263 const struct rte_flow_action actions[],
264 struct rte_flow_error *error);
265 typedef int (*mlx5_flow_apply_t)(struct rte_eth_dev *dev, struct rte_flow *flow,
266 struct rte_flow_error *error);
267 typedef void (*mlx5_flow_remove_t)(struct rte_eth_dev *dev,
268 struct rte_flow *flow);
269 typedef void (*mlx5_flow_destroy_t)(struct rte_eth_dev *dev,
270 struct rte_flow *flow);
271 typedef int (*mlx5_flow_query_t)(struct rte_eth_dev *dev,
272 struct rte_flow *flow,
273 const struct rte_flow_action *actions,
275 struct rte_flow_error *error);
276 struct mlx5_flow_driver_ops {
277 mlx5_flow_validate_t validate;
278 mlx5_flow_prepare_t prepare;
279 mlx5_flow_translate_t translate;
280 mlx5_flow_apply_t apply;
281 mlx5_flow_remove_t remove;
282 mlx5_flow_destroy_t destroy;
283 mlx5_flow_query_t query;
288 uint64_t mlx5_flow_hashfields_adjust(struct mlx5_flow *dev_flow, int tunnel,
289 uint32_t layer_types,
290 uint64_t hash_fields);
291 uint32_t mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
292 uint32_t subpriority);
293 int mlx5_flow_validate_action_count(struct rte_eth_dev *dev,
294 const struct rte_flow_attr *attr,
295 struct rte_flow_error *error);
296 int mlx5_flow_validate_action_drop(uint64_t action_flags,
297 const struct rte_flow_attr *attr,
298 struct rte_flow_error *error);
299 int mlx5_flow_validate_action_flag(uint64_t action_flags,
300 const struct rte_flow_attr *attr,
301 struct rte_flow_error *error);
302 int mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
303 uint64_t action_flags,
304 const struct rte_flow_attr *attr,
305 struct rte_flow_error *error);
306 int mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
307 uint64_t action_flags,
308 struct rte_eth_dev *dev,
309 const struct rte_flow_attr *attr,
310 struct rte_flow_error *error);
311 int mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
312 uint64_t action_flags,
313 struct rte_eth_dev *dev,
314 const struct rte_flow_attr *attr,
315 struct rte_flow_error *error);
316 int mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
317 const struct rte_flow_attr *attributes,
318 struct rte_flow_error *error);
319 int mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
321 struct rte_flow_error *error);
322 int mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
324 uint8_t target_protocol,
325 struct rte_flow_error *error);
326 int mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
328 struct rte_flow_error *error);
329 int mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
331 struct rte_flow_error *error);
332 int mlx5_flow_validate_item_mpls(const struct rte_flow_item *item,
334 uint8_t target_protocol,
335 struct rte_flow_error *error);
336 int mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
338 uint8_t target_protocol,
339 const struct rte_flow_item_tcp *flow_mask,
340 struct rte_flow_error *error);
341 int mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
343 uint8_t target_protocol,
344 struct rte_flow_error *error);
345 int mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
347 struct rte_flow_error *error);
348 int mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
350 struct rte_flow_error *error);
351 int mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
353 struct rte_eth_dev *dev,
354 struct rte_flow_error *error);
356 /* mlx5_flow_tcf.c */
358 int mlx5_flow_tcf_init(struct mlx5_flow_tcf_context *ctx,
359 unsigned int ifindex, struct rte_flow_error *error);
360 struct mlx5_flow_tcf_context *mlx5_flow_tcf_context_create(void);
361 void mlx5_flow_tcf_context_destroy(struct mlx5_flow_tcf_context *ctx);
363 #endif /* RTE_PMD_MLX5_FLOW_H_ */