New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_flow_classify / rte_flow_classify.h
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 #ifndef _RTE_FLOW_CLASSIFY_H_
35 #define _RTE_FLOW_CLASSIFY_H_
36
37 /**
38  * @file
39  *
40  * RTE Flow Classify Library
41  *
42  * @b EXPERIMENTAL: this API may change without prior notice
43  *
44  * This library provides flow record information with some measured properties.
45  *
46  * Application should define the flow and measurement criteria (action) for it.
47  *
48  * The Library doesn't maintain any flow records itself, instead flow
49  * information is returned to upper layer only for given packets.
50  *
51  * It is application's responsibility to call rte_flow_classifier_query()
52  * for a burst of packets, just after receiving them or before transmitting
53  * them.
54  * Application should provide the flow type interested in, measurement to apply
55  * to that flow in rte_flow_classify_table_entry_add() API, and should provide
56  * the rte_flow_classifier object and storage to put results in for the
57  * rte_flow_classifier_query() API.
58  *
59  *  Usage:
60  *  - application calls rte_flow_classifier_create() to create an
61  *    rte_flow_classifier object.
62  *  - application calls rte_flow_classify_table_create() to create a table
63  *    in the rte_flow_classifier object.
64  *  - application calls rte_flow_classify_table_entry_add() to add a rule to
65  *    the table in the rte_flow_classifier object.
66  *  - application calls rte_flow_classifier_query() in a polling manner,
67  *    preferably after rte_eth_rx_burst(). This will cause the library to
68  *    match packet information to flow information with some measurements.
69  *  - rte_flow_classifier object can be destroyed when it is no longer needed
70  *    with rte_flow_classifier_free()
71  */
72
73 #include <rte_ethdev.h>
74 #include <rte_ether.h>
75 #include <rte_flow.h>
76 #include <rte_acl.h>
77 #include <rte_table_acl.h>
78
79 #ifdef __cplusplus
80 extern "C" {
81 #endif
82
83 extern int librte_flow_classify_logtype;
84
85 #define RTE_FLOW_CLASSIFY_LOG(level, fmt, args...) \
86 rte_log(RTE_LOG_ ## level, librte_flow_classify_logtype, "%s(): " fmt, \
87         __func__, ## args)
88
89 /** Opaque data type for flow classifier */
90 struct rte_flow_classifier;
91
92 /** Opaque data type for flow classify rule */
93 struct rte_flow_classify_rule;
94
95 /** Flow classify rule type */
96 enum rte_flow_classify_rule_type {
97         /** no type */
98         RTE_FLOW_CLASSIFY_RULE_TYPE_NONE,
99         /** IPv4 5tuple type */
100         RTE_FLOW_CLASSIFY_RULE_TYPE_IPV4_5TUPLE,
101 };
102
103 /** Flow classify table type */
104 enum rte_flow_classify_table_type {
105         /** no type */
106         RTE_FLOW_CLASSIFY_TABLE_TYPE_NONE,
107         /** ACL type */
108         RTE_FLOW_CLASSIFY_TABLE_TYPE_ACL,
109 };
110
111 /**
112  * Maximum number of tables allowed for any Flow Classifier instance.
113  * The value of this parameter cannot be changed.
114  */
115 #define RTE_FLOW_CLASSIFY_TABLE_MAX  64
116
117 /** Parameters for flow classifier creation */
118 struct rte_flow_classifier_params {
119         /** flow classifier name */
120         const char *name;
121
122         /** CPU socket ID where memory for the flow classifier and its */
123         /** elements (tables) should be allocated */
124         int socket_id;
125
126         /** Table type */
127         enum rte_flow_classify_table_type type;
128 };
129
130 /** Parameters for table creation */
131 struct rte_flow_classify_table_params {
132         /** Table operations (specific to each table type) */
133         struct rte_table_ops *ops;
134
135         /** Opaque param to be passed to the table create operation */
136         void *arg_create;
137 };
138
139 /** IPv4 5-tuple data */
140 struct rte_flow_classify_ipv4_5tuple {
141         uint32_t dst_ip;         /**< Destination IP address in big endian. */
142         uint32_t dst_ip_mask;    /**< Mask of destination IP address. */
143         uint32_t src_ip;         /**< Source IP address in big endian. */
144         uint32_t src_ip_mask;    /**< Mask of destination IP address. */
145         uint16_t dst_port;       /**< Destination port in big endian. */
146         uint16_t dst_port_mask;  /**< Mask of destination port. */
147         uint16_t src_port;       /**< Source Port in big endian. */
148         uint16_t src_port_mask;  /**< Mask of source port. */
149         uint8_t proto;           /**< L4 protocol. */
150         uint8_t proto_mask;      /**< Mask of L4 protocol. */
151 };
152
153 /**
154  * Flow stats
155  *
156  * For the count action, stats can be returned by the query API.
157  *
158  * Storage for stats is provided by application.
159  */
160 struct rte_flow_classify_stats {
161         void *stats;
162 };
163
164 struct rte_flow_classify_ipv4_5tuple_stats {
165         /** count of packets that match IPv4 5tuple pattern */
166         uint64_t counter1;
167         /** IPv4 5tuple data */
168         struct rte_flow_classify_ipv4_5tuple ipv4_5tuple;
169 };
170
171 /**
172  * Flow classifier create
173  *
174  * @param params
175  *   Parameters for flow classifier creation
176  * @return
177  *   Handle to flow classifier instance on success or NULL otherwise
178  */
179 struct rte_flow_classifier *
180 rte_flow_classifier_create(struct rte_flow_classifier_params *params);
181
182 /**
183  * Flow classifier free
184  *
185  * @param cls
186  *   Handle to flow classifier instance
187  * @return
188  *   0 on success, error code otherwise
189  */
190 int
191 rte_flow_classifier_free(struct rte_flow_classifier *cls);
192
193 /**
194  * Flow classify table create
195  *
196  * @param cls
197  *   Handle to flow classifier instance
198  * @param params
199  *   Parameters for flow_classify table creation
200  * @param table_id
201  *   Table ID. Valid only within the scope of table IDs of the current
202  *   classifier. Only returned after a successful invocation.
203  * @return
204  *   0 on success, error code otherwise
205  */
206 int
207 rte_flow_classify_table_create(struct rte_flow_classifier *cls,
208                 struct rte_flow_classify_table_params *params,
209                 uint32_t *table_id);
210
211 /**
212  * Add a flow classify rule to the flow_classifer table.
213  *
214  * @param[in] cls
215  *   Flow classifier handle
216  * @param[in] table_id
217  *   id of table
218  * @param[out] key_found
219  *  returns 1 if key present already, 0 otherwise.
220  * @param[in] attr
221  *   Flow rule attributes
222  * @param[in] pattern
223  *   Pattern specification (list terminated by the END pattern item).
224  * @param[in] actions
225  *   Associated actions (list terminated by the END pattern item).
226  * @param[out] error
227  *   Perform verbose error reporting if not NULL. Structure
228  *   initialised in case of error only.
229  * @return
230  *   A valid handle in case of success, NULL otherwise.
231  */
232 struct rte_flow_classify_rule *
233 rte_flow_classify_table_entry_add(struct rte_flow_classifier *cls,
234                 uint32_t table_id,
235                 int *key_found,
236                 const struct rte_flow_attr *attr,
237                 const struct rte_flow_item pattern[],
238                 const struct rte_flow_action actions[],
239                 struct rte_flow_error *error);
240
241 /**
242  * Delete a flow classify rule from the flow_classifer table.
243  *
244  * @param[in] cls
245  *   Flow classifier handle
246  * @param[in] table_id
247  *   id of table
248  * @param[in] rule
249  *   Flow classify rule
250  * @return
251  *   0 on success, error code otherwise.
252  */
253 int
254 rte_flow_classify_table_entry_delete(struct rte_flow_classifier *cls,
255                 uint32_t table_id,
256                 struct rte_flow_classify_rule *rule);
257
258 /**
259  * Query flow classifier for given rule.
260  *
261  * @param[in] cls
262  *   Flow classifier handle
263  * @param[in] table_id
264  *   id of table
265  * @param[in] pkts
266  *   Pointer to packets to process
267  * @param[in] nb_pkts
268  *   Number of packets to process
269  * @param[in] rule
270  *   Flow classify rule
271  * @param[in] stats
272  *   Flow classify stats
273  *
274  * @return
275  *   0 on success, error code otherwise.
276  */
277 int
278 rte_flow_classifier_query(struct rte_flow_classifier *cls,
279                 uint32_t table_id,
280                 struct rte_mbuf **pkts,
281                 const uint16_t nb_pkts,
282                 struct rte_flow_classify_rule *rule,
283                 struct rte_flow_classify_stats *stats);
284
285 #ifdef __cplusplus
286 }
287 #endif
288
289 #endif /* _RTE_FLOW_CLASSIFY_H_ */