New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_ether / rte_mtr.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
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 Intel Corporation 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_ethdev.h"
38 #include "rte_mtr_driver.h"
39 #include "rte_mtr.h"
40
41 /* Get generic traffic metering & policing operations structure from a port. */
42 const struct rte_mtr_ops *
43 rte_mtr_ops_get(uint16_t port_id, struct rte_mtr_error *error)
44 {
45         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
46         const struct rte_mtr_ops *ops;
47
48         if (!rte_eth_dev_is_valid_port(port_id)) {
49                 rte_mtr_error_set(error,
50                         ENODEV,
51                         RTE_MTR_ERROR_TYPE_UNSPECIFIED,
52                         NULL,
53                         rte_strerror(ENODEV));
54                 return NULL;
55         }
56
57         if ((dev->dev_ops->mtr_ops_get == NULL) ||
58                 (dev->dev_ops->mtr_ops_get(dev, &ops) != 0) ||
59                 (ops == NULL)) {
60                 rte_mtr_error_set(error,
61                         ENOSYS,
62                         RTE_MTR_ERROR_TYPE_UNSPECIFIED,
63                         NULL,
64                         rte_strerror(ENOSYS));
65                 return NULL;
66         }
67
68         return ops;
69 }
70
71 #define RTE_MTR_FUNC(port_id, func)                     \
72 ({                                                      \
73         const struct rte_mtr_ops *ops =                 \
74                 rte_mtr_ops_get(port_id, error);                \
75         if (ops == NULL)                                        \
76                 return -rte_errno;                      \
77                                                         \
78         if (ops->func == NULL)                          \
79                 return -rte_mtr_error_set(error,                \
80                         ENOSYS,                         \
81                         RTE_MTR_ERROR_TYPE_UNSPECIFIED, \
82                         NULL,                           \
83                         rte_strerror(ENOSYS));          \
84                                                         \
85         ops->func;                                      \
86 })
87
88 /* MTR capabilities get */
89 int
90 rte_mtr_capabilities_get(uint16_t port_id,
91         struct rte_mtr_capabilities *cap,
92         struct rte_mtr_error *error)
93 {
94         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
95         return RTE_MTR_FUNC(port_id, capabilities_get)(dev,
96                 cap, error);
97 }
98
99 /* MTR meter profile add */
100 int
101 rte_mtr_meter_profile_add(uint16_t port_id,
102         uint32_t meter_profile_id,
103         struct rte_mtr_meter_profile *profile,
104         struct rte_mtr_error *error)
105 {
106         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
107         return RTE_MTR_FUNC(port_id, meter_profile_add)(dev,
108                 meter_profile_id, profile, error);
109 }
110
111 /** MTR meter profile delete */
112 int
113 rte_mtr_meter_profile_delete(uint16_t port_id,
114         uint32_t meter_profile_id,
115         struct rte_mtr_error *error)
116 {
117         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
118         return RTE_MTR_FUNC(port_id, meter_profile_delete)(dev,
119                 meter_profile_id, error);
120 }
121
122 /** MTR object create */
123 int
124 rte_mtr_create(uint16_t port_id,
125         uint32_t mtr_id,
126         struct rte_mtr_params *params,
127         int shared,
128         struct rte_mtr_error *error)
129 {
130         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
131         return RTE_MTR_FUNC(port_id, create)(dev,
132                 mtr_id, params, shared, error);
133 }
134
135 /** MTR object destroy */
136 int
137 rte_mtr_destroy(uint16_t port_id,
138         uint32_t mtr_id,
139         struct rte_mtr_error *error)
140 {
141         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
142         return RTE_MTR_FUNC(port_id, destroy)(dev,
143                 mtr_id, error);
144 }
145
146 /** MTR object meter enable */
147 int
148 rte_mtr_meter_enable(uint16_t port_id,
149         uint32_t mtr_id,
150         struct rte_mtr_error *error)
151 {
152         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
153         return RTE_MTR_FUNC(port_id, meter_enable)(dev,
154                 mtr_id, error);
155 }
156
157 /** MTR object meter disable */
158 int
159 rte_mtr_meter_disable(uint16_t port_id,
160         uint32_t mtr_id,
161         struct rte_mtr_error *error)
162 {
163         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
164         return RTE_MTR_FUNC(port_id, meter_disable)(dev,
165                 mtr_id, error);
166 }
167
168 /** MTR object meter profile update */
169 int
170 rte_mtr_meter_profile_update(uint16_t port_id,
171         uint32_t mtr_id,
172         uint32_t meter_profile_id,
173         struct rte_mtr_error *error)
174 {
175         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
176         return RTE_MTR_FUNC(port_id, meter_profile_update)(dev,
177                 mtr_id, meter_profile_id, error);
178 }
179
180 /** MTR object meter DSCP table update */
181 int
182 rte_mtr_meter_dscp_table_update(uint16_t port_id,
183         uint32_t mtr_id,
184         enum rte_mtr_color *dscp_table,
185         struct rte_mtr_error *error)
186 {
187         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
188         return RTE_MTR_FUNC(port_id, meter_dscp_table_update)(dev,
189                 mtr_id, dscp_table, error);
190 }
191
192 /** MTR object policer action update */
193 int
194 rte_mtr_policer_actions_update(uint16_t port_id,
195         uint32_t mtr_id,
196         uint32_t action_mask,
197         enum rte_mtr_policer_action *actions,
198         struct rte_mtr_error *error)
199 {
200         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
201         return RTE_MTR_FUNC(port_id, policer_actions_update)(dev,
202                 mtr_id, action_mask, actions, error);
203 }
204
205 /** MTR object enabled stats update */
206 int
207 rte_mtr_stats_update(uint16_t port_id,
208         uint32_t mtr_id,
209         uint64_t stats_mask,
210         struct rte_mtr_error *error)
211 {
212         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
213         return RTE_MTR_FUNC(port_id, stats_update)(dev,
214                 mtr_id, stats_mask, error);
215 }
216
217 /** MTR object stats read */
218 int
219 rte_mtr_stats_read(uint16_t port_id,
220         uint32_t mtr_id,
221         struct rte_mtr_stats *stats,
222         uint64_t *stats_mask,
223         int clear,
224         struct rte_mtr_error *error)
225 {
226         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
227         return RTE_MTR_FUNC(port_id, stats_read)(dev,
228                 mtr_id, stats, stats_mask, clear, error);
229 }