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