1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
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(PHY_PORT, sizeof(struct rte_flow_item_phy_port)),
42 MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)),
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(RSS, sizeof(struct rte_flow_action_rss)),
77 MK_FLOW_ACTION(PF, 0),
78 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
79 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
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 /** Pattern item specification types. */
259 enum item_spec_type {
265 /** Compute storage space needed by item specification and copy it. */
267 flow_item_spec_copy(void *buf, const struct rte_flow_item *item,
268 enum item_spec_type type)
271 const void *item_spec =
272 type == ITEM_SPEC ? item->spec :
273 type == ITEM_LAST ? item->last :
274 type == ITEM_MASK ? item->mask :
279 switch (item->type) {
281 const struct rte_flow_item_raw *raw;
284 struct rte_flow_item_raw *raw;
288 case RTE_FLOW_ITEM_TYPE_RAW:
291 off = RTE_ALIGN_CEIL(sizeof(struct rte_flow_item_raw),
292 sizeof(*src.raw->pattern));
293 size = off + src.raw->length * sizeof(*src.raw->pattern);
295 memcpy(dst.raw, src.raw, sizeof(*src.raw));
296 dst.raw->pattern = memcpy((uint8_t *)dst.raw + off,
302 size = rte_flow_desc_item[item->type].size;
304 memcpy(buf, item_spec, size);
308 return RTE_ALIGN_CEIL(size, sizeof(double));
311 /** Compute storage space needed by action configuration and copy it. */
313 flow_action_conf_copy(void *buf, const struct rte_flow_action *action)
319 switch (action->type) {
321 const struct rte_flow_action_rss *rss;
324 struct rte_flow_action_rss *rss;
328 case RTE_FLOW_ACTION_TYPE_RSS:
329 src.rss = action->conf;
333 *dst.rss = (struct rte_flow_action_rss){
334 .func = src.rss->func,
335 .level = src.rss->level,
336 .types = src.rss->types,
337 .key_len = src.rss->key_len,
338 .queue_num = src.rss->queue_num,
340 off += sizeof(*src.rss);
341 if (src.rss->key_len) {
342 off = RTE_ALIGN_CEIL(off, sizeof(double));
343 size = sizeof(*src.rss->key) * src.rss->key_len;
345 dst.rss->key = memcpy
346 ((void *)((uintptr_t)dst.rss + off),
350 if (src.rss->queue_num) {
351 off = RTE_ALIGN_CEIL(off, sizeof(double));
352 size = sizeof(*src.rss->queue) * src.rss->queue_num;
354 dst.rss->queue = memcpy
355 ((void *)((uintptr_t)dst.rss + off),
356 src.rss->queue, size);
362 size = rte_flow_desc_action[action->type].size;
364 memcpy(buf, action->conf, size);
368 return RTE_ALIGN_CEIL(size, sizeof(double));
371 /** Store a full rte_flow description. */
373 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
374 const struct rte_flow_attr *attr,
375 const struct rte_flow_item *items,
376 const struct rte_flow_action *actions)
378 struct rte_flow_desc *fd = NULL;
386 const struct rte_flow_item *item;
390 fd->items = (void *)&fd->data[off1];
392 struct rte_flow_item *dst = NULL;
394 if ((size_t)item->type >=
395 RTE_DIM(rte_flow_desc_item) ||
396 !rte_flow_desc_item[item->type].name) {
401 dst = memcpy(fd->data + off1, item,
403 off1 += sizeof(*item);
406 dst->spec = fd->data + off2;
407 off2 += flow_item_spec_copy
408 (fd ? fd->data + off2 : NULL, item,
413 dst->last = fd->data + off2;
414 off2 += flow_item_spec_copy
415 (fd ? fd->data + off2 : NULL, item,
420 dst->mask = fd->data + off2;
421 off2 += flow_item_spec_copy
422 (fd ? fd->data + off2 : NULL, item,
425 off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
426 } while ((item++)->type != RTE_FLOW_ITEM_TYPE_END);
427 off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
430 const struct rte_flow_action *action;
434 fd->actions = (void *)&fd->data[off1];
436 struct rte_flow_action *dst = NULL;
438 if ((size_t)action->type >=
439 RTE_DIM(rte_flow_desc_action) ||
440 !rte_flow_desc_action[action->type].name) {
445 dst = memcpy(fd->data + off1, action,
447 off1 += sizeof(*action);
450 dst->conf = fd->data + off2;
451 off2 += flow_action_conf_copy
452 (fd ? fd->data + off2 : NULL, action);
454 off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
455 } while ((action++)->type != RTE_FLOW_ACTION_TYPE_END);
459 off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
460 tmp = RTE_ALIGN_CEIL(offsetof(struct rte_flow_desc, data),
462 size = tmp + off1 + off2;
467 *fd = (const struct rte_flow_desc) {
471 tmp -= offsetof(struct rte_flow_desc, data);