net/mlx5: add flow translate function
[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 /* Priority reserved for default flows. */
95 #define MLX5_FLOW_PRIO_RSVD ((uint32_t)-1)
96
97 /*
98  * Number of sub priorities.
99  * For each kind of pattern matching i.e. L2, L3, L4 to have a correct
100  * matching on the NIC (firmware dependent) L4 most have the higher priority
101  * followed by L3 and ending with L2.
102  */
103 #define MLX5_PRIORITY_MAP_L2 2
104 #define MLX5_PRIORITY_MAP_L3 1
105 #define MLX5_PRIORITY_MAP_L4 0
106 #define MLX5_PRIORITY_MAP_MAX 3
107
108 /* Verbs specification header. */
109 struct ibv_spec_header {
110         enum ibv_flow_spec_type type;
111         uint16_t size;
112 };
113
114 /** Handles information leading to a drop fate. */
115 struct mlx5_flow_verbs {
116         LIST_ENTRY(mlx5_flow_verbs) next;
117         unsigned int size; /**< Size of the attribute. */
118         struct {
119                 struct ibv_flow_attr *attr;
120                 /**< Pointer to the Specification buffer. */
121                 uint8_t *specs; /**< Pointer to the specifications. */
122         };
123         struct ibv_flow *flow; /**< Verbs flow pointer. */
124         struct mlx5_hrxq *hrxq; /**< Hash Rx queue object. */
125         uint64_t hash_fields; /**< Verbs hash Rx queue hash fields. */
126 };
127
128 /** Device flow structure. */
129 struct mlx5_flow {
130         LIST_ENTRY(mlx5_flow) next;
131         struct rte_flow *flow; /**< Pointer to the main flow. */
132         uint32_t layers; /**< Bit-fields that holds the detected layers. */
133         union {
134                 struct mlx5_flow_verbs verbs; /**< Holds the verbs dev-flow. */
135         };
136 };
137
138 /* Counters information. */
139 struct mlx5_flow_counter {
140         LIST_ENTRY(mlx5_flow_counter) next; /**< Pointer to the next counter. */
141         uint32_t shared:1; /**< Share counter ID with other flow rules. */
142         uint32_t ref_cnt:31; /**< Reference counter. */
143         uint32_t id; /**< Counter ID. */
144         struct ibv_counter_set *cs; /**< Holds the counters for the rule. */
145         uint64_t hits; /**< Number of packets matched by the rule. */
146         uint64_t bytes; /**< Number of bytes matched by the rule. */
147 };
148
149 /* Flow structure. */
150 struct rte_flow {
151         TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
152         struct rte_flow_attr attributes; /**< User flow attribute. */
153         uint32_t layers;
154         /**< Bit-fields of present layers see MLX5_FLOW_LAYER_*. */
155         struct mlx5_flow_counter *counter; /**< Holds flow counter. */
156         struct rte_flow_action_rss rss;/**< RSS context. */
157         uint8_t key[MLX5_RSS_HASH_KEY_LEN]; /**< RSS hash key. */
158         uint16_t (*queue)[]; /**< Destination queues to redirect traffic to. */
159         void *nl_flow; /**< Netlink flow buffer if relevant. */
160         LIST_HEAD(dev_flows, mlx5_flow) dev_flows;
161         /**< Device flows that are part of the flow. */
162         uint32_t actions; /**< Bit-fields which mark all detected actions. */
163 };
164 typedef int (*mlx5_flow_validate_t)(struct rte_eth_dev *dev,
165                                     const struct rte_flow_attr *attr,
166                                     const struct rte_flow_item items[],
167                                     const struct rte_flow_action actions[],
168                                     struct rte_flow_error *error);
169 typedef struct mlx5_flow *(*mlx5_flow_prepare_t)
170         (const struct rte_flow_attr *attr, const struct rte_flow_item items[],
171          const struct rte_flow_action actions[], uint64_t *item_flags,
172          uint64_t *action_flags, struct rte_flow_error *error);
173 typedef int (*mlx5_flow_translate_t)(struct rte_eth_dev *dev,
174                                      struct mlx5_flow *dev_flow,
175                                      const struct rte_flow_attr *attr,
176                                      const struct rte_flow_item items[],
177                                      const struct rte_flow_action actions[],
178                                      struct rte_flow_error *error);
179 typedef int (*mlx5_flow_apply_t)(struct rte_eth_dev *dev, struct rte_flow *flow,
180                                  struct rte_flow_error *error);
181 typedef void (*mlx5_flow_remove_t)(struct rte_eth_dev *dev,
182                                    struct rte_flow *flow);
183 typedef void (*mlx5_flow_destroy_t)(struct rte_eth_dev *dev,
184                                     struct rte_flow *flow);
185 struct mlx5_flow_driver_ops {
186         mlx5_flow_validate_t validate;
187         mlx5_flow_prepare_t prepare;
188         mlx5_flow_translate_t translate;
189         mlx5_flow_apply_t apply;
190         mlx5_flow_remove_t remove;
191         mlx5_flow_destroy_t destroy;
192 };
193
194 /* mlx5_flow.c */
195
196 uint32_t mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
197                                    uint32_t subpriority);
198 int mlx5_flow_validate_action_count(struct rte_eth_dev *dev,
199                                     struct rte_flow_error *error);
200 int mlx5_flow_validate_action_drop(uint64_t action_flags,
201                                    struct rte_flow_error *error);
202 int mlx5_flow_validate_action_flag(uint64_t action_flags,
203                                    struct rte_flow_error *error);
204 int mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
205                                    uint64_t action_flags,
206                                    struct rte_flow_error *error);
207 int mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
208                                     uint64_t action_flags,
209                                     struct rte_eth_dev *dev,
210                                     struct rte_flow_error *error);
211 int mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
212                                   uint64_t action_flags,
213                                   struct rte_eth_dev *dev,
214                                   struct rte_flow_error *error);
215 int mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
216                                   const struct rte_flow_attr *attributes,
217                                   struct rte_flow_error *error);
218 int mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
219                                 uint64_t item_flags,
220                                 struct rte_flow_error *error);
221 int mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
222                                 uint64_t item_flags,
223                                 uint8_t target_protocol,
224                                 struct rte_flow_error *error);
225 int mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
226                                  int64_t item_flags,
227                                  struct rte_flow_error *error);
228 int mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
229                                  uint64_t item_flags,
230                                  struct rte_flow_error *error);
231 int mlx5_flow_validate_item_mpls(const struct rte_flow_item *item,
232                                  uint64_t item_flags,
233                                  uint8_t target_protocol,
234                                  struct rte_flow_error *error);
235 int mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
236                                 uint64_t item_flags,
237                                 uint8_t target_protocol,
238                                 struct rte_flow_error *error);
239 int mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
240                                 uint64_t item_flags,
241                                 uint8_t target_protocol,
242                                 struct rte_flow_error *error);
243 int mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
244                                  int64_t item_flags,
245                                  struct rte_flow_error *error);
246 int mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
247                                   uint64_t item_flags,
248                                   struct rte_flow_error *error);
249 int mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
250                                       uint64_t item_flags,
251                                       struct rte_eth_dev *dev,
252                                       struct rte_flow_error *error);
253 void mlx5_flow_init_driver_ops(struct rte_eth_dev *dev);
254
255 /* mlx5_flow_verbs.c */
256
257 void mlx5_flow_verbs_get_driver_ops(struct mlx5_flow_driver_ops *flow_ops);
258
259 #endif /* RTE_PMD_MLX5_FLOW_H_ */