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