net/mlx5: add Direct Verbs translate items
[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 /* Outer Masks. */
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)
54
55 /* Tunnel Masks. */
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)
59
60 /* Inner Masks. */
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)
68
69 /* Actions that modify the fate of matching traffic. */
70 #define MLX5_FLOW_FATE_DROP (1u << 0)
71 #define MLX5_FLOW_FATE_QUEUE (1u << 1)
72 #define MLX5_FLOW_FATE_RSS (1u << 2)
73
74 /* Modify a packet. */
75 #define MLX5_FLOW_MOD_FLAG (1u << 0)
76 #define MLX5_FLOW_MOD_MARK (1u << 1)
77 #define MLX5_FLOW_MOD_COUNT (1u << 2)
78
79 /* Actions */
80 #define MLX5_FLOW_ACTION_DROP (1u << 0)
81 #define MLX5_FLOW_ACTION_QUEUE (1u << 1)
82 #define MLX5_FLOW_ACTION_RSS (1u << 2)
83 #define MLX5_FLOW_ACTION_FLAG (1u << 3)
84 #define MLX5_FLOW_ACTION_MARK (1u << 4)
85 #define MLX5_FLOW_ACTION_COUNT (1u << 5)
86
87 #define MLX5_FLOW_FATE_ACTIONS \
88         (MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)
89
90 #ifndef IPPROTO_MPLS
91 #define IPPROTO_MPLS 137
92 #endif
93
94 /* UDP port numbers for VxLAN. */
95 #define MLX5_UDP_PORT_VXLAN 4789
96 #define MLX5_UDP_PORT_VXLAN_GPE 4790
97
98 /* Priority reserved for default flows. */
99 #define MLX5_FLOW_PRIO_RSVD ((uint32_t)-1)
100
101 /*
102  * Number of sub priorities.
103  * For each kind of pattern matching i.e. L2, L3, L4 to have a correct
104  * matching on the NIC (firmware dependent) L4 most have the higher priority
105  * followed by L3 and ending with L2.
106  */
107 #define MLX5_PRIORITY_MAP_L2 2
108 #define MLX5_PRIORITY_MAP_L3 1
109 #define MLX5_PRIORITY_MAP_L4 0
110 #define MLX5_PRIORITY_MAP_MAX 3
111
112 /* Valid layer type for IPV4 RSS. */
113 #define MLX5_IPV4_LAYER_TYPES \
114         (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 | \
115          ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV4_UDP | \
116          ETH_RSS_NONFRAG_IPV4_OTHER)
117
118 /* IBV hash source bits  for IPV4. */
119 #define MLX5_IPV4_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4)
120
121 /* Valid layer type for IPV6 RSS. */
122 #define MLX5_IPV6_LAYER_TYPES \
123         (ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 | ETH_RSS_NONFRAG_IPV6_TCP | \
124          ETH_RSS_NONFRAG_IPV6_UDP | ETH_RSS_IPV6_EX  | ETH_RSS_IPV6_TCP_EX | \
125          ETH_RSS_IPV6_UDP_EX | ETH_RSS_NONFRAG_IPV6_OTHER)
126
127 /* IBV hash source bits  for IPV6. */
128 #define MLX5_IPV6_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6)
129
130 /* Max number of actions per DV flow. */
131 #define MLX5_DV_MAX_NUMBER_OF_ACTIONS 8
132
133 /* Matcher PRM representation */
134 struct mlx5_flow_dv_match_params {
135         size_t size;
136         /**< Size of match value. Do NOT split size and key! */
137         uint32_t buf[MLX5_ST_SZ_DW(fte_match_param)];
138         /**< Matcher value. This value is used as the mask or as a key. */
139 };
140
141 /* Matcher structure. */
142 struct mlx5_flow_dv_matcher {
143         LIST_ENTRY(mlx5_flow_dv_matcher) next;
144         /* Pointer to the next element. */
145         rte_atomic32_t refcnt; /**< Reference counter. */
146         void *matcher_object; /**< Pointer to DV matcher */
147         uint16_t crc; /**< CRC of key. */
148         uint16_t priority; /**< Priority of matcher. */
149         uint8_t egress; /**< Egress matcher. */
150         struct mlx5_flow_dv_match_params mask; /**< Matcher mask. */
151 };
152
153 /* DV flows structure. */
154 struct mlx5_flow_dv {
155         uint64_t hash_fields; /**< Fields that participate in the hash. */
156         struct mlx5_hrxq *hrxq; /**< Hash Rx queues. */
157         /* Flow DV api: */
158         struct mlx5_flow_dv_matcher *matcher; /**< Cache to matcher. */
159         struct mlx5_flow_dv_match_params value;
160         /**< Holds the value that the packet is compared to. */
161         struct ibv_flow *flow; /**< Installed flow. */
162 };
163
164 /* Verbs specification header. */
165 struct ibv_spec_header {
166         enum ibv_flow_spec_type type;
167         uint16_t size;
168 };
169
170 /** Handles information leading to a drop fate. */
171 struct mlx5_flow_verbs {
172         LIST_ENTRY(mlx5_flow_verbs) next;
173         unsigned int size; /**< Size of the attribute. */
174         struct {
175                 struct ibv_flow_attr *attr;
176                 /**< Pointer to the Specification buffer. */
177                 uint8_t *specs; /**< Pointer to the specifications. */
178         };
179         struct ibv_flow *flow; /**< Verbs flow pointer. */
180         struct mlx5_hrxq *hrxq; /**< Hash Rx queue object. */
181         uint64_t hash_fields; /**< Verbs hash Rx queue hash fields. */
182 };
183
184 /** Device flow structure. */
185 struct mlx5_flow {
186         LIST_ENTRY(mlx5_flow) next;
187         struct rte_flow *flow; /**< Pointer to the main flow. */
188         uint32_t layers; /**< Bit-fields that holds the detected layers. */
189         union {
190                 struct mlx5_flow_dv dv;
191                 struct mlx5_flow_verbs verbs;
192         };
193 };
194
195 /* Counters information. */
196 struct mlx5_flow_counter {
197         LIST_ENTRY(mlx5_flow_counter) next; /**< Pointer to the next counter. */
198         uint32_t shared:1; /**< Share counter ID with other flow rules. */
199         uint32_t ref_cnt:31; /**< Reference counter. */
200         uint32_t id; /**< Counter ID. */
201         struct ibv_counter_set *cs; /**< Holds the counters for the rule. */
202         uint64_t hits; /**< Number of packets matched by the rule. */
203         uint64_t bytes; /**< Number of bytes matched by the rule. */
204 };
205
206 /* Flow structure. */
207 struct rte_flow {
208         TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
209         struct rte_flow_attr attributes; /**< User flow attribute. */
210         uint32_t layers;
211         /**< Bit-fields of present layers see MLX5_FLOW_LAYER_*. */
212         struct mlx5_flow_counter *counter; /**< Holds flow counter. */
213         struct rte_flow_action_rss rss;/**< RSS context. */
214         uint8_t key[MLX5_RSS_HASH_KEY_LEN]; /**< RSS hash key. */
215         uint16_t (*queue)[]; /**< Destination queues to redirect traffic to. */
216         void *nl_flow; /**< Netlink flow buffer if relevant. */
217         LIST_HEAD(dev_flows, mlx5_flow) dev_flows;
218         /**< Device flows that are part of the flow. */
219         uint32_t actions; /**< Bit-fields which mark all detected actions. */
220 };
221 typedef int (*mlx5_flow_validate_t)(struct rte_eth_dev *dev,
222                                     const struct rte_flow_attr *attr,
223                                     const struct rte_flow_item items[],
224                                     const struct rte_flow_action actions[],
225                                     struct rte_flow_error *error);
226 typedef struct mlx5_flow *(*mlx5_flow_prepare_t)
227         (const struct rte_flow_attr *attr, const struct rte_flow_item items[],
228          const struct rte_flow_action actions[], uint64_t *item_flags,
229          uint64_t *action_flags, struct rte_flow_error *error);
230 typedef int (*mlx5_flow_translate_t)(struct rte_eth_dev *dev,
231                                      struct mlx5_flow *dev_flow,
232                                      const struct rte_flow_attr *attr,
233                                      const struct rte_flow_item items[],
234                                      const struct rte_flow_action actions[],
235                                      struct rte_flow_error *error);
236 typedef int (*mlx5_flow_apply_t)(struct rte_eth_dev *dev, struct rte_flow *flow,
237                                  struct rte_flow_error *error);
238 typedef void (*mlx5_flow_remove_t)(struct rte_eth_dev *dev,
239                                    struct rte_flow *flow);
240 typedef void (*mlx5_flow_destroy_t)(struct rte_eth_dev *dev,
241                                     struct rte_flow *flow);
242 struct mlx5_flow_driver_ops {
243         mlx5_flow_validate_t validate;
244         mlx5_flow_prepare_t prepare;
245         mlx5_flow_translate_t translate;
246         mlx5_flow_apply_t apply;
247         mlx5_flow_remove_t remove;
248         mlx5_flow_destroy_t destroy;
249 };
250
251 /* mlx5_flow.c */
252
253 uint64_t mlx5_flow_hashfields_adjust(struct mlx5_flow *dev_flow, int tunnel,
254                                      uint32_t layer_types,
255                                      uint64_t hash_fields);
256 uint32_t mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
257                                    uint32_t subpriority);
258 int mlx5_flow_validate_action_count(struct rte_eth_dev *dev,
259                                     struct rte_flow_error *error);
260 int mlx5_flow_validate_action_drop(uint64_t action_flags,
261                                    struct rte_flow_error *error);
262 int mlx5_flow_validate_action_flag(uint64_t action_flags,
263                                    struct rte_flow_error *error);
264 int mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
265                                    uint64_t action_flags,
266                                    struct rte_flow_error *error);
267 int mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
268                                     uint64_t action_flags,
269                                     struct rte_eth_dev *dev,
270                                     struct rte_flow_error *error);
271 int mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
272                                   uint64_t action_flags,
273                                   struct rte_eth_dev *dev,
274                                   struct rte_flow_error *error);
275 int mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
276                                   const struct rte_flow_attr *attributes,
277                                   struct rte_flow_error *error);
278 int mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
279                                 uint64_t item_flags,
280                                 struct rte_flow_error *error);
281 int mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
282                                 uint64_t item_flags,
283                                 uint8_t target_protocol,
284                                 struct rte_flow_error *error);
285 int mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
286                                  int64_t item_flags,
287                                  struct rte_flow_error *error);
288 int mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
289                                  uint64_t item_flags,
290                                  struct rte_flow_error *error);
291 int mlx5_flow_validate_item_mpls(const struct rte_flow_item *item,
292                                  uint64_t item_flags,
293                                  uint8_t target_protocol,
294                                  struct rte_flow_error *error);
295 int mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
296                                 uint64_t item_flags,
297                                 uint8_t target_protocol,
298                                 struct rte_flow_error *error);
299 int mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
300                                 uint64_t item_flags,
301                                 uint8_t target_protocol,
302                                 struct rte_flow_error *error);
303 int mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
304                                  int64_t item_flags,
305                                  struct rte_flow_error *error);
306 int mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
307                                   uint64_t item_flags,
308                                   struct rte_flow_error *error);
309 int mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
310                                       uint64_t item_flags,
311                                       struct rte_eth_dev *dev,
312                                       struct rte_flow_error *error);
313 void mlx5_flow_init_driver_ops(struct rte_eth_dev *dev);
314
315 /* mlx5_flow_dv.c */
316 void mlx5_flow_dv_get_driver_ops(struct mlx5_flow_driver_ops *flow_ops);
317
318 /* mlx5_flow_verbs.c */
319
320 void mlx5_flow_verbs_get_driver_ops(struct mlx5_flow_driver_ops *flow_ops);
321
322 #endif /* RTE_PMD_MLX5_FLOW_H_ */