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