1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox.
11 #include <rte_common.h>
12 #include <rte_errno.h>
13 #include <rte_branch_prediction.h>
14 #include "rte_ethdev.h"
15 #include "rte_flow_driver.h"
19 * Flow elements description tables.
21 struct rte_flow_desc_data {
26 /** Generate flow_item[] entry. */
27 #define MK_FLOW_ITEM(t, s) \
28 [RTE_FLOW_ITEM_TYPE_ ## t] = { \
33 /** Information about known flow pattern items. */
34 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
36 MK_FLOW_ITEM(VOID, 0),
37 MK_FLOW_ITEM(INVERT, 0),
38 MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
40 MK_FLOW_ITEM(VF, sizeof(struct rte_flow_item_vf)),
41 MK_FLOW_ITEM(PORT, sizeof(struct rte_flow_item_port)),
42 MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)), /* +pattern[] */
43 MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
44 MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
45 MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
46 MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
47 MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
48 MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
49 MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
50 MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
51 MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
52 MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
53 MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
54 MK_FLOW_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
55 MK_FLOW_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
56 MK_FLOW_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
59 /** Generate flow_action[] entry. */
60 #define MK_FLOW_ACTION(t, s) \
61 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
66 /** Information about known flow actions. */
67 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
68 MK_FLOW_ACTION(END, 0),
69 MK_FLOW_ACTION(VOID, 0),
70 MK_FLOW_ACTION(PASSTHRU, 0),
71 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
72 MK_FLOW_ACTION(FLAG, 0),
73 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
74 MK_FLOW_ACTION(DROP, 0),
75 MK_FLOW_ACTION(COUNT, 0),
76 MK_FLOW_ACTION(DUP, sizeof(struct rte_flow_action_dup)),
77 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)), /* +queue[] */
78 MK_FLOW_ACTION(PF, 0),
79 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
83 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
87 if (rte_eth_dev_is_removed(port_id))
88 return rte_flow_error_set(error, EIO,
89 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
90 NULL, rte_strerror(EIO));
94 /* Get generic flow operations structure from a port. */
95 const struct rte_flow_ops *
96 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
98 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
99 const struct rte_flow_ops *ops;
102 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
104 else if (unlikely(!dev->dev_ops->filter_ctrl ||
105 dev->dev_ops->filter_ctrl(dev,
106 RTE_ETH_FILTER_GENERIC,
113 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
114 NULL, rte_strerror(code));
118 /* Check whether a flow rule can be created on a given port. */
120 rte_flow_validate(uint16_t port_id,
121 const struct rte_flow_attr *attr,
122 const struct rte_flow_item pattern[],
123 const struct rte_flow_action actions[],
124 struct rte_flow_error *error)
126 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
127 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
131 if (likely(!!ops->validate))
132 return flow_err(port_id, ops->validate(dev, attr, pattern,
133 actions, error), error);
134 return rte_flow_error_set(error, ENOSYS,
135 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
136 NULL, rte_strerror(ENOSYS));
139 /* Create a flow rule on a given port. */
141 rte_flow_create(uint16_t port_id,
142 const struct rte_flow_attr *attr,
143 const struct rte_flow_item pattern[],
144 const struct rte_flow_action actions[],
145 struct rte_flow_error *error)
147 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
148 struct rte_flow *flow;
149 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
153 if (likely(!!ops->create)) {
154 flow = ops->create(dev, attr, pattern, actions, error);
156 flow_err(port_id, -rte_errno, error);
159 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
160 NULL, rte_strerror(ENOSYS));
164 /* Destroy a flow rule on a given port. */
166 rte_flow_destroy(uint16_t port_id,
167 struct rte_flow *flow,
168 struct rte_flow_error *error)
170 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
171 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
175 if (likely(!!ops->destroy))
176 return flow_err(port_id, ops->destroy(dev, flow, error),
178 return rte_flow_error_set(error, ENOSYS,
179 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
180 NULL, rte_strerror(ENOSYS));
183 /* Destroy all flow rules associated with a port. */
185 rte_flow_flush(uint16_t port_id,
186 struct rte_flow_error *error)
188 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
189 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
193 if (likely(!!ops->flush))
194 return flow_err(port_id, ops->flush(dev, error), error);
195 return rte_flow_error_set(error, ENOSYS,
196 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
197 NULL, rte_strerror(ENOSYS));
200 /* Query an existing flow rule. */
202 rte_flow_query(uint16_t port_id,
203 struct rte_flow *flow,
204 enum rte_flow_action_type action,
206 struct rte_flow_error *error)
208 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
209 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
213 if (likely(!!ops->query))
214 return flow_err(port_id, ops->query(dev, flow, action, data,
216 return rte_flow_error_set(error, ENOSYS,
217 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
218 NULL, rte_strerror(ENOSYS));
221 /* Restrict ingress traffic to the defined flow rules. */
223 rte_flow_isolate(uint16_t port_id,
225 struct rte_flow_error *error)
227 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
228 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
232 if (likely(!!ops->isolate))
233 return flow_err(port_id, ops->isolate(dev, set, error), error);
234 return rte_flow_error_set(error, ENOSYS,
235 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
236 NULL, rte_strerror(ENOSYS));
239 /* Initialize flow error structure. */
241 rte_flow_error_set(struct rte_flow_error *error,
243 enum rte_flow_error_type type,
248 *error = (struct rte_flow_error){
258 /** Compute storage space needed by item specification. */
260 flow_item_spec_size(const struct rte_flow_item *item,
261 size_t *size, size_t *pad)
267 switch (item->type) {
269 const struct rte_flow_item_raw *raw;
272 /* Not a fall-through */
273 case RTE_FLOW_ITEM_TYPE_RAW:
274 spec.raw = item->spec;
275 *size = offsetof(struct rte_flow_item_raw, pattern) +
276 spec.raw->length * sizeof(*spec.raw->pattern);
279 *size = rte_flow_desc_item[item->type].size;
283 *pad = RTE_ALIGN_CEIL(*size, sizeof(double)) - *size;
286 /** Compute storage space needed by action configuration. */
288 flow_action_conf_size(const struct rte_flow_action *action,
289 size_t *size, size_t *pad)
295 switch (action->type) {
297 const struct rte_flow_action_rss *rss;
300 /* Not a fall-through. */
301 case RTE_FLOW_ACTION_TYPE_RSS:
302 conf.rss = action->conf;
303 *size = offsetof(struct rte_flow_action_rss, queue) +
304 conf.rss->num * sizeof(*conf.rss->queue);
307 *size = rte_flow_desc_action[action->type].size;
311 *pad = RTE_ALIGN_CEIL(*size, sizeof(double)) - *size;
314 /** Store a full rte_flow description. */
316 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
317 const struct rte_flow_attr *attr,
318 const struct rte_flow_item *items,
319 const struct rte_flow_action *actions)
321 struct rte_flow_desc *fd = NULL;
330 const struct rte_flow_item *item;
334 fd->items = (void *)&fd->data[off1];
336 struct rte_flow_item *dst = NULL;
338 if ((size_t)item->type >=
339 RTE_DIM(rte_flow_desc_item) ||
340 !rte_flow_desc_item[item->type].name) {
345 dst = memcpy(fd->data + off1, item,
347 off1 += sizeof(*item);
348 flow_item_spec_size(item, &tmp, &pad);
351 dst->spec = memcpy(fd->data + off2,
357 dst->last = memcpy(fd->data + off2,
363 dst->mask = memcpy(fd->data + off2,
367 off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
368 } while ((item++)->type != RTE_FLOW_ITEM_TYPE_END);
369 off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
372 const struct rte_flow_action *action;
376 fd->actions = (void *)&fd->data[off1];
378 struct rte_flow_action *dst = NULL;
380 if ((size_t)action->type >=
381 RTE_DIM(rte_flow_desc_action) ||
382 !rte_flow_desc_action[action->type].name) {
387 dst = memcpy(fd->data + off1, action,
389 off1 += sizeof(*action);
390 flow_action_conf_size(action, &tmp, &pad);
393 dst->conf = memcpy(fd->data + off2,
397 off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
398 } while ((action++)->type != RTE_FLOW_ACTION_TYPE_END);
402 off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
403 tmp = RTE_ALIGN_CEIL(offsetof(struct rte_flow_desc, data),
405 size = tmp + off1 + off2;
410 *fd = (const struct rte_flow_desc) {
414 tmp -= offsetof(struct rte_flow_desc, data);