aaa70d68cb9d945f1d4d40b65cbd24be4d7f8510
[deb_dpdk.git] / lib / librte_ether / rte_flow.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 6WIND S.A.
5  *   Copyright 2016 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35
36 #include <rte_errno.h>
37 #include <rte_branch_prediction.h>
38 #include "rte_ethdev.h"
39 #include "rte_flow_driver.h"
40 #include "rte_flow.h"
41
42 /* Get generic flow operations structure from a port. */
43 const struct rte_flow_ops *
44 rte_flow_ops_get(uint8_t port_id, struct rte_flow_error *error)
45 {
46         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
47         const struct rte_flow_ops *ops;
48         int code;
49
50         if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
51                 code = ENODEV;
52         else if (unlikely(!dev->dev_ops->filter_ctrl ||
53                           dev->dev_ops->filter_ctrl(dev,
54                                                     RTE_ETH_FILTER_GENERIC,
55                                                     RTE_ETH_FILTER_GET,
56                                                     &ops) ||
57                           !ops))
58                 code = ENOSYS;
59         else
60                 return ops;
61         rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
62                            NULL, rte_strerror(code));
63         return NULL;
64 }
65
66 /* Check whether a flow rule can be created on a given port. */
67 int
68 rte_flow_validate(uint8_t port_id,
69                   const struct rte_flow_attr *attr,
70                   const struct rte_flow_item pattern[],
71                   const struct rte_flow_action actions[],
72                   struct rte_flow_error *error)
73 {
74         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
75         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
76
77         if (unlikely(!ops))
78                 return -rte_errno;
79         if (likely(!!ops->validate))
80                 return ops->validate(dev, attr, pattern, actions, error);
81         return -rte_flow_error_set(error, ENOSYS,
82                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
83                                    NULL, rte_strerror(ENOSYS));
84 }
85
86 /* Create a flow rule on a given port. */
87 struct rte_flow *
88 rte_flow_create(uint8_t port_id,
89                 const struct rte_flow_attr *attr,
90                 const struct rte_flow_item pattern[],
91                 const struct rte_flow_action actions[],
92                 struct rte_flow_error *error)
93 {
94         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
95         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
96
97         if (unlikely(!ops))
98                 return NULL;
99         if (likely(!!ops->create))
100                 return ops->create(dev, attr, pattern, actions, error);
101         rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
102                            NULL, rte_strerror(ENOSYS));
103         return NULL;
104 }
105
106 /* Destroy a flow rule on a given port. */
107 int
108 rte_flow_destroy(uint8_t port_id,
109                  struct rte_flow *flow,
110                  struct rte_flow_error *error)
111 {
112         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
113         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
114
115         if (unlikely(!ops))
116                 return -rte_errno;
117         if (likely(!!ops->destroy))
118                 return ops->destroy(dev, flow, error);
119         return -rte_flow_error_set(error, ENOSYS,
120                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
121                                    NULL, rte_strerror(ENOSYS));
122 }
123
124 /* Destroy all flow rules associated with a port. */
125 int
126 rte_flow_flush(uint8_t port_id,
127                struct rte_flow_error *error)
128 {
129         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
130         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
131
132         if (unlikely(!ops))
133                 return -rte_errno;
134         if (likely(!!ops->flush))
135                 return ops->flush(dev, error);
136         return -rte_flow_error_set(error, ENOSYS,
137                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
138                                    NULL, rte_strerror(ENOSYS));
139 }
140
141 /* Query an existing flow rule. */
142 int
143 rte_flow_query(uint8_t port_id,
144                struct rte_flow *flow,
145                enum rte_flow_action_type action,
146                void *data,
147                struct rte_flow_error *error)
148 {
149         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
150         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
151
152         if (!ops)
153                 return -rte_errno;
154         if (likely(!!ops->query))
155                 return ops->query(dev, flow, action, data, error);
156         return -rte_flow_error_set(error, ENOSYS,
157                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
158                                    NULL, rte_strerror(ENOSYS));
159 }