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