net/mlx5: add raw data encap/decap to Direct Verbs
[dpdk.git] / drivers / net / mlx5 / mlx5_flow.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 Mellanox Technologies, Ltd
3  */
4
5 #ifndef RTE_PMD_MLX5_FLOW_H_
6 #define RTE_PMD_MLX5_FLOW_H_
7
8 #include <netinet/in.h>
9 #include <sys/queue.h>
10 #include <stdalign.h>
11 #include <stdint.h>
12 #include <string.h>
13
14 /* Verbs header. */
15 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
16 #ifdef PEDANTIC
17 #pragma GCC diagnostic ignored "-Wpedantic"
18 #endif
19 #include <infiniband/verbs.h>
20 #ifdef PEDANTIC
21 #pragma GCC diagnostic error "-Wpedantic"
22 #endif
23
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)
31
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)
39
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)
45
46 /* General pattern items bits. */
47 #define MLX5_FLOW_ITEM_METADATA (1u << 16)
48
49 /* Outer Masks. */
50 #define MLX5_FLOW_LAYER_OUTER_L3 \
51         (MLX5_FLOW_LAYER_OUTER_L3_IPV4 | MLX5_FLOW_LAYER_OUTER_L3_IPV6)
52 #define MLX5_FLOW_LAYER_OUTER_L4 \
53         (MLX5_FLOW_LAYER_OUTER_L4_UDP | MLX5_FLOW_LAYER_OUTER_L4_TCP)
54 #define MLX5_FLOW_LAYER_OUTER \
55         (MLX5_FLOW_LAYER_OUTER_L2 | MLX5_FLOW_LAYER_OUTER_L3 | \
56          MLX5_FLOW_LAYER_OUTER_L4)
57
58 /* Tunnel Masks. */
59 #define MLX5_FLOW_LAYER_TUNNEL \
60         (MLX5_FLOW_LAYER_VXLAN | MLX5_FLOW_LAYER_VXLAN_GPE | \
61          MLX5_FLOW_LAYER_GRE | MLX5_FLOW_LAYER_MPLS)
62
63 /* Inner Masks. */
64 #define MLX5_FLOW_LAYER_INNER_L3 \
65         (MLX5_FLOW_LAYER_INNER_L3_IPV4 | MLX5_FLOW_LAYER_INNER_L3_IPV6)
66 #define MLX5_FLOW_LAYER_INNER_L4 \
67         (MLX5_FLOW_LAYER_INNER_L4_UDP | MLX5_FLOW_LAYER_INNER_L4_TCP)
68 #define MLX5_FLOW_LAYER_INNER \
69         (MLX5_FLOW_LAYER_INNER_L2 | MLX5_FLOW_LAYER_INNER_L3 | \
70          MLX5_FLOW_LAYER_INNER_L4)
71
72 /* Actions */
73 #define MLX5_FLOW_ACTION_DROP (1u << 0)
74 #define MLX5_FLOW_ACTION_QUEUE (1u << 1)
75 #define MLX5_FLOW_ACTION_RSS (1u << 2)
76 #define MLX5_FLOW_ACTION_FLAG (1u << 3)
77 #define MLX5_FLOW_ACTION_MARK (1u << 4)
78 #define MLX5_FLOW_ACTION_COUNT (1u << 5)
79 #define MLX5_FLOW_ACTION_PORT_ID (1u << 6)
80 #define MLX5_FLOW_ACTION_OF_POP_VLAN (1u << 7)
81 #define MLX5_FLOW_ACTION_OF_PUSH_VLAN (1u << 8)
82 #define MLX5_FLOW_ACTION_OF_SET_VLAN_VID (1u << 9)
83 #define MLX5_FLOW_ACTION_OF_SET_VLAN_PCP (1u << 10)
84 #define MLX5_FLOW_ACTION_SET_IPV4_SRC (1u << 11)
85 #define MLX5_FLOW_ACTION_SET_IPV4_DST (1u << 12)
86 #define MLX5_FLOW_ACTION_SET_IPV6_SRC (1u << 13)
87 #define MLX5_FLOW_ACTION_SET_IPV6_DST (1u << 14)
88 #define MLX5_FLOW_ACTION_SET_TP_SRC (1u << 15)
89 #define MLX5_FLOW_ACTION_SET_TP_DST (1u << 16)
90 #define MLX5_FLOW_ACTION_JUMP (1u << 17)
91 #define MLX5_FLOW_ACTION_SET_TTL (1u << 18)
92 #define MLX5_FLOW_ACTION_DEC_TTL (1u << 19)
93 #define MLX5_FLOW_ACTION_SET_MAC_SRC (1u << 20)
94 #define MLX5_FLOW_ACTION_SET_MAC_DST (1u << 21)
95 #define MLX5_FLOW_ACTION_VXLAN_ENCAP (1u << 22)
96 #define MLX5_FLOW_ACTION_VXLAN_DECAP (1u << 23)
97 #define MLX5_FLOW_ACTION_NVGRE_ENCAP (1u << 24)
98 #define MLX5_FLOW_ACTION_NVGRE_DECAP (1u << 25)
99 #define MLX5_FLOW_ACTION_RAW_ENCAP (1u << 26)
100 #define MLX5_FLOW_ACTION_RAW_DECAP (1u << 27)
101
102 #define MLX5_FLOW_FATE_ACTIONS \
103         (MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)
104
105 #define MLX5_FLOW_ENCAP_ACTIONS (MLX5_FLOW_ACTION_VXLAN_ENCAP | \
106                                  MLX5_FLOW_ACTION_NVGRE_ENCAP | \
107                                  MLX5_FLOW_ACTION_RAW_ENCAP)
108
109 #define MLX5_FLOW_DECAP_ACTIONS (MLX5_FLOW_ACTION_VXLAN_DECAP | \
110                                  MLX5_FLOW_ACTION_NVGRE_DECAP | \
111                                  MLX5_FLOW_ACTION_RAW_DECAP)
112
113 #ifndef IPPROTO_MPLS
114 #define IPPROTO_MPLS 137
115 #endif
116
117 /* UDP port numbers for VxLAN. */
118 #define MLX5_UDP_PORT_VXLAN 4789
119 #define MLX5_UDP_PORT_VXLAN_GPE 4790
120
121 /* Priority reserved for default flows. */
122 #define MLX5_FLOW_PRIO_RSVD ((uint32_t)-1)
123
124 /*
125  * Number of sub priorities.
126  * For each kind of pattern matching i.e. L2, L3, L4 to have a correct
127  * matching on the NIC (firmware dependent) L4 most have the higher priority
128  * followed by L3 and ending with L2.
129  */
130 #define MLX5_PRIORITY_MAP_L2 2
131 #define MLX5_PRIORITY_MAP_L3 1
132 #define MLX5_PRIORITY_MAP_L4 0
133 #define MLX5_PRIORITY_MAP_MAX 3
134
135 /* Valid layer type for IPV4 RSS. */
136 #define MLX5_IPV4_LAYER_TYPES \
137         (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 | \
138          ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV4_UDP | \
139          ETH_RSS_NONFRAG_IPV4_OTHER)
140
141 /* IBV hash source bits  for IPV4. */
142 #define MLX5_IPV4_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4)
143
144 /* Valid layer type for IPV6 RSS. */
145 #define MLX5_IPV6_LAYER_TYPES \
146         (ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 | ETH_RSS_NONFRAG_IPV6_TCP | \
147          ETH_RSS_NONFRAG_IPV6_UDP | ETH_RSS_IPV6_EX  | ETH_RSS_IPV6_TCP_EX | \
148          ETH_RSS_IPV6_UDP_EX | ETH_RSS_NONFRAG_IPV6_OTHER)
149
150 /* IBV hash source bits  for IPV6. */
151 #define MLX5_IPV6_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6)
152
153 /* Max number of actions per DV flow. */
154 #define MLX5_DV_MAX_NUMBER_OF_ACTIONS 8
155
156 enum mlx5_flow_drv_type {
157         MLX5_FLOW_TYPE_MIN,
158         MLX5_FLOW_TYPE_DV,
159         MLX5_FLOW_TYPE_TCF,
160         MLX5_FLOW_TYPE_VERBS,
161         MLX5_FLOW_TYPE_MAX,
162 };
163
164 /* Matcher PRM representation */
165 struct mlx5_flow_dv_match_params {
166         size_t size;
167         /**< Size of match value. Do NOT split size and key! */
168         uint32_t buf[MLX5_ST_SZ_DW(fte_match_param)];
169         /**< Matcher value. This value is used as the mask or as a key. */
170 };
171
172 #define MLX5_DV_MAX_NUMBER_OF_ACTIONS 8
173
174 /* Matcher structure. */
175 struct mlx5_flow_dv_matcher {
176         LIST_ENTRY(mlx5_flow_dv_matcher) next;
177         /* Pointer to the next element. */
178         rte_atomic32_t refcnt; /**< Reference counter. */
179         void *matcher_object; /**< Pointer to DV matcher */
180         uint16_t crc; /**< CRC of key. */
181         uint16_t priority; /**< Priority of matcher. */
182         uint8_t egress; /**< Egress matcher. */
183         struct mlx5_flow_dv_match_params mask; /**< Matcher mask. */
184 };
185
186 /* DV flows structure. */
187 struct mlx5_flow_dv {
188         uint64_t hash_fields; /**< Fields that participate in the hash. */
189         struct mlx5_hrxq *hrxq; /**< Hash Rx queues. */
190         /* Flow DV api: */
191         struct mlx5_flow_dv_matcher *matcher; /**< Cache to matcher. */
192         struct mlx5_flow_dv_match_params value;
193         /**< Holds the value that the packet is compared to. */
194         struct ibv_flow *flow; /**< Installed flow. */
195 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
196         struct mlx5dv_flow_action_attr actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS];
197         /**< Action list. */
198         struct ibv_flow_action *encap_decap_verbs_action;
199         /**< Verbs encap/decap object. */
200 #endif
201         int actions_n; /**< number of actions. */
202 };
203
204 /** Linux TC flower driver for E-Switch flow. */
205 struct mlx5_flow_tcf {
206         struct nlmsghdr *nlh;
207         struct tcmsg *tcm;
208 };
209
210 /* Verbs specification header. */
211 struct ibv_spec_header {
212         enum ibv_flow_spec_type type;
213         uint16_t size;
214 };
215
216 /** Handles information leading to a drop fate. */
217 struct mlx5_flow_verbs {
218         LIST_ENTRY(mlx5_flow_verbs) next;
219         unsigned int size; /**< Size of the attribute. */
220         struct {
221                 struct ibv_flow_attr *attr;
222                 /**< Pointer to the Specification buffer. */
223                 uint8_t *specs; /**< Pointer to the specifications. */
224         };
225         struct ibv_flow *flow; /**< Verbs flow pointer. */
226         struct mlx5_hrxq *hrxq; /**< Hash Rx queue object. */
227         uint64_t hash_fields; /**< Verbs hash Rx queue hash fields. */
228 };
229
230 /** Device flow structure. */
231 struct mlx5_flow {
232         LIST_ENTRY(mlx5_flow) next;
233         struct rte_flow *flow; /**< Pointer to the main flow. */
234         uint64_t layers;
235         /**< Bit-fields of present layers, see MLX5_FLOW_LAYER_*. */
236         union {
237 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
238                 struct mlx5_flow_dv dv;
239 #endif
240                 struct mlx5_flow_tcf tcf;
241                 struct mlx5_flow_verbs verbs;
242         };
243 };
244
245 /* Counters information. */
246 struct mlx5_flow_counter {
247         LIST_ENTRY(mlx5_flow_counter) next; /**< Pointer to the next counter. */
248         uint32_t shared:1; /**< Share counter ID with other flow rules. */
249         uint32_t ref_cnt:31; /**< Reference counter. */
250         uint32_t id; /**< Counter ID. */
251 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
252         struct ibv_counter_set *cs; /**< Holds the counters for the rule. */
253 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
254         struct ibv_counters *cs; /**< Holds the counters for the rule. */
255 #endif
256         uint64_t hits; /**< Number of packets matched by the rule. */
257         uint64_t bytes; /**< Number of bytes matched by the rule. */
258 };
259
260 /* Flow structure. */
261 struct rte_flow {
262         TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
263         enum mlx5_flow_drv_type drv_type; /**< Drvier type. */
264         struct mlx5_flow_counter *counter; /**< Holds flow counter. */
265         struct rte_flow_action_rss rss;/**< RSS context. */
266         uint8_t key[MLX5_RSS_HASH_KEY_LEN]; /**< RSS hash key. */
267         uint16_t (*queue)[]; /**< Destination queues to redirect traffic to. */
268         LIST_HEAD(dev_flows, mlx5_flow) dev_flows;
269         /**< Device flows that are part of the flow. */
270         uint64_t actions;
271         /**< Bit-fields of detected actions, see MLX5_FLOW_ACTION_*. */
272         struct mlx5_fdir *fdir; /**< Pointer to associated FDIR if any. */
273 };
274
275 typedef int (*mlx5_flow_validate_t)(struct rte_eth_dev *dev,
276                                     const struct rte_flow_attr *attr,
277                                     const struct rte_flow_item items[],
278                                     const struct rte_flow_action actions[],
279                                     struct rte_flow_error *error);
280 typedef struct mlx5_flow *(*mlx5_flow_prepare_t)
281         (const struct rte_flow_attr *attr, const struct rte_flow_item items[],
282          const struct rte_flow_action actions[], uint64_t *item_flags,
283          uint64_t *action_flags, struct rte_flow_error *error);
284 typedef int (*mlx5_flow_translate_t)(struct rte_eth_dev *dev,
285                                      struct mlx5_flow *dev_flow,
286                                      const struct rte_flow_attr *attr,
287                                      const struct rte_flow_item items[],
288                                      const struct rte_flow_action actions[],
289                                      struct rte_flow_error *error);
290 typedef int (*mlx5_flow_apply_t)(struct rte_eth_dev *dev, struct rte_flow *flow,
291                                  struct rte_flow_error *error);
292 typedef void (*mlx5_flow_remove_t)(struct rte_eth_dev *dev,
293                                    struct rte_flow *flow);
294 typedef void (*mlx5_flow_destroy_t)(struct rte_eth_dev *dev,
295                                     struct rte_flow *flow);
296 typedef int (*mlx5_flow_query_t)(struct rte_eth_dev *dev,
297                                  struct rte_flow *flow,
298                                  const struct rte_flow_action *actions,
299                                  void *data,
300                                  struct rte_flow_error *error);
301 struct mlx5_flow_driver_ops {
302         mlx5_flow_validate_t validate;
303         mlx5_flow_prepare_t prepare;
304         mlx5_flow_translate_t translate;
305         mlx5_flow_apply_t apply;
306         mlx5_flow_remove_t remove;
307         mlx5_flow_destroy_t destroy;
308         mlx5_flow_query_t query;
309 };
310
311 /* mlx5_flow.c */
312
313 uint64_t mlx5_flow_hashfields_adjust(struct mlx5_flow *dev_flow, int tunnel,
314                                      uint64_t layer_types,
315                                      uint64_t hash_fields);
316 uint32_t mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
317                                    uint32_t subpriority);
318 int mlx5_flow_validate_action_count(struct rte_eth_dev *dev,
319                                     const struct rte_flow_attr *attr,
320                                     struct rte_flow_error *error);
321 int mlx5_flow_validate_action_drop(uint64_t action_flags,
322                                    const struct rte_flow_attr *attr,
323                                    struct rte_flow_error *error);
324 int mlx5_flow_validate_action_flag(uint64_t action_flags,
325                                    const struct rte_flow_attr *attr,
326                                    struct rte_flow_error *error);
327 int mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
328                                    uint64_t action_flags,
329                                    const struct rte_flow_attr *attr,
330                                    struct rte_flow_error *error);
331 int mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
332                                     uint64_t action_flags,
333                                     struct rte_eth_dev *dev,
334                                     const struct rte_flow_attr *attr,
335                                     struct rte_flow_error *error);
336 int mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
337                                   uint64_t action_flags,
338                                   struct rte_eth_dev *dev,
339                                   const struct rte_flow_attr *attr,
340                                   struct rte_flow_error *error);
341 int mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
342                                   const struct rte_flow_attr *attributes,
343                                   struct rte_flow_error *error);
344 int mlx5_flow_item_acceptable(const struct rte_flow_item *item,
345                               const uint8_t *mask,
346                               const uint8_t *nic_mask,
347                               unsigned int size,
348                               struct rte_flow_error *error);
349 int mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
350                                 uint64_t item_flags,
351                                 struct rte_flow_error *error);
352 int mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
353                                 uint64_t item_flags,
354                                 uint8_t target_protocol,
355                                 struct rte_flow_error *error);
356 int mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
357                                  uint64_t item_flags,
358                                  struct rte_flow_error *error);
359 int mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
360                                  uint64_t item_flags,
361                                  struct rte_flow_error *error);
362 int mlx5_flow_validate_item_mpls(const struct rte_flow_item *item,
363                                  uint64_t item_flags,
364                                  uint8_t target_protocol,
365                                  struct rte_flow_error *error);
366 int mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
367                                 uint64_t item_flags,
368                                 uint8_t target_protocol,
369                                 const struct rte_flow_item_tcp *flow_mask,
370                                 struct rte_flow_error *error);
371 int mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
372                                 uint64_t item_flags,
373                                 uint8_t target_protocol,
374                                 struct rte_flow_error *error);
375 int mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
376                                  uint64_t item_flags,
377                                  struct rte_flow_error *error);
378 int mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
379                                   uint64_t item_flags,
380                                   struct rte_flow_error *error);
381 int mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
382                                       uint64_t item_flags,
383                                       struct rte_eth_dev *dev,
384                                       struct rte_flow_error *error);
385
386 /* mlx5_flow_tcf.c */
387
388 int mlx5_flow_tcf_init(struct mlx5_flow_tcf_context *ctx,
389                        unsigned int ifindex, struct rte_flow_error *error);
390 struct mlx5_flow_tcf_context *mlx5_flow_tcf_context_create(void);
391 void mlx5_flow_tcf_context_destroy(struct mlx5_flow_tcf_context *ctx);
392
393 #endif /* RTE_PMD_MLX5_FLOW_H_ */