New upstream version 17.11-rc3
[deb_dpdk.git] / doc / guides / prog_guide / flow_classify_lib.rst
1 ..  BSD LICENSE
2     Copyright(c) 2017 Intel Corporation. All rights reserved.
3     All rights reserved.
4
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8
9     * Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in
13     the documentation and/or other materials provided with the
14     distribution.
15     * Neither the name of Intel Corporation nor the names of its
16     contributors may be used to endorse or promote products derived
17     from this software without specific prior written permission.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23     OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 Flow Classification Library
32 ===========================
33
34 DPDK provides a Flow Classification library that provides the ability
35 to classify an input packet by matching it against a set of Flow rules.
36
37 The initial implementation supports counting of IPv4 5-tuple packets which match
38 a particular Flow rule only.
39
40 Please refer to the
41 :doc:`./rte_flow`
42 for more information.
43
44 The Flow Classification library uses the ``librte_table`` API for managing Flow
45 rules and matching packets against the Flow rules.
46 The library is table agnostic and can use the following tables:
47 ``Access Control List``, ``Hash`` and ``Longest Prefix Match(LPM)``.
48 The ``Access Control List`` table is used in the initial implementation.
49
50 Please refer to the
51 :doc:`./packet_framework`
52 for more information.on ``librte_table``.
53
54 DPDK provides an Access Control List library that provides the ability to
55 classify an input packet based on a set of classification rules.
56
57 Please refer to the
58 :doc:`./packet_classif_access_ctrl`
59 library for more information on ``librte_acl``.
60
61 There is also a Flow Classify sample application which demonstrates the use of
62 the Flow Classification Library API's.
63
64 Please refer to the
65 :doc:`../sample_app_ug/flow_classify`
66 for more information on the ``flow_classify`` sample application.
67
68 Overview
69 --------
70
71 The library has the following API's
72
73 .. code-block:: c
74
75     /**
76      * Flow classifier create
77      *
78      * @param params
79      *   Parameters for flow classifier creation
80      * @return
81      *   Handle to flow classifier instance on success or NULL otherwise
82      */
83     struct rte_flow_classifier *
84     rte_flow_classifier_create(struct rte_flow_classifier_params *params);
85
86     /**
87      * Flow classifier free
88      *
89      * @param cls
90      *   Handle to flow classifier instance
91      * @return
92      *   0 on success, error code otherwise
93      */
94     int
95     rte_flow_classifier_free(struct rte_flow_classifier *cls);
96
97     /**
98      * Flow classify table create
99      *
100      * @param cls
101      *   Handle to flow classifier instance
102      * @param params
103      *   Parameters for flow_classify table creation
104      * @param table_id
105      *   Table ID. Valid only within the scope of table IDs of the current
106      *   classifier. Only returned after a successful invocation.
107      * @return
108      *   0 on success, error code otherwise
109      */
110     int
111     rte_flow_classify_table_create(struct rte_flow_classifier *cls,
112            struct rte_flow_classify_table_params *params,
113            uint32_t *table_id);
114
115     /**
116      * Add a flow classify rule to the flow_classifier table.
117      *
118      * @param[in] cls
119      *   Flow classifier handle
120      * @param[in] table_id
121      *   id of table
122      * @param[in] attr
123      *   Flow rule attributes
124      * @param[in] pattern
125      *   Pattern specification (list terminated by the END pattern item).
126      * @param[in] actions
127      *   Associated actions (list terminated by the END pattern item).
128      * @param[out] error
129      *   Perform verbose error reporting if not NULL. Structure
130      *   initialised in case of error only.
131      * @return
132      *   A valid handle in case of success, NULL otherwise.
133      */
134     struct rte_flow_classify_rule *
135     rte_flow_classify_table_entry_add(struct rte_flow_classifier *cls,
136             uint32_t table_id,
137             const struct rte_flow_attr *attr,
138             const struct rte_flow_item pattern[],
139             const struct rte_flow_action actions[],
140             struct rte_flow_error *error);
141
142     /**
143      * Delete a flow classify rule from the flow_classifier table.
144      *
145      * @param[in] cls
146      *   Flow classifier handle
147      * @param[in] table_id
148      *   id of table
149      * @param[in] rule
150      *   Flow classify rule
151      * @return
152      *   0 on success, error code otherwise.
153      */
154     int
155     rte_flow_classify_table_entry_delete(struct rte_flow_classifier *cls,
156             uint32_t table_id,
157             struct rte_flow_classify_rule *rule);
158
159     /**
160      * Query flow classifier for given rule.
161      *
162      * @param[in] cls
163      *   Flow classifier handle
164      * @param[in] table_id
165      *   id of table
166      * @param[in] pkts
167      *   Pointer to packets to process
168      * @param[in] nb_pkts
169      *   Number of packets to process
170      * @param[in] rule
171      *   Flow classify rule
172      * @param[in] stats
173      *   Flow classify stats
174      *
175      * @return
176      *   0 on success, error code otherwise.
177      */
178     int
179     rte_flow_classifier_query(struct rte_flow_classifier *cls,
180             uint32_t table_id,
181             struct rte_mbuf **pkts,
182             const uint16_t nb_pkts,
183             struct rte_flow_classify_rule *rule,
184             struct rte_flow_classify_stats *stats);
185
186 Classifier creation
187 ~~~~~~~~~~~~~~~~~~~
188
189 The application creates the ``Classifier`` using the
190 ``rte_flow_classifier_create`` API.
191 The ``rte_flow_classify_params`` structure must be initialised by the
192 application before calling the API.
193
194 .. code-block:: c
195
196     struct rte_flow_classifier_params {
197         /** flow classifier name */
198         const char *name;
199
200         /** CPU socket ID where memory for the flow classifier and its */
201         /** elements (tables) should be allocated */
202         int socket_id;
203
204         /** Table type */
205         enum rte_flow_classify_table_type type;
206     };
207
208 The ``Classifier`` has the following internal structures:
209
210 .. code-block:: c
211
212     struct rte_table {
213         /* Input parameters */
214         struct rte_table_ops ops;
215         uint32_t entry_size;
216         enum rte_flow_classify_table_type type;
217
218         /* Handle to the low-level table object */
219         void *h_table;
220     };
221
222     #define RTE_FLOW_CLASSIFIER_MAX_NAME_SZ 256
223
224     struct rte_flow_classifier {
225         /* Input parameters */
226         char name[RTE_FLOW_CLASSIFIER_MAX_NAME_SZ];
227         int socket_id;
228         enum rte_flow_classify_table_type type;
229
230         /* Internal tables */
231         struct rte_table tables[RTE_FLOW_CLASSIFY_TABLE_MAX];
232         uint32_t num_tables;
233         uint16_t nb_pkts;
234         struct rte_flow_classify_table_entry
235             *entries[RTE_PORT_IN_BURST_SIZE_MAX];
236     } __rte_cache_aligned;
237
238 Adding a table to the Classifier
239 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
240
241 The application adds a table to the ``Classifier`` using the
242 ``rte_flow_classify_table_create`` API.
243 The ``rte_flow_classify_table_params`` structure must be initialised by the
244 application before calling the API.
245
246 .. code-block:: c
247
248     struct rte_flow_classify_table_params {
249         /** Table operations (specific to each table type) */
250         struct rte_table_ops *ops;
251
252         /** Opaque param to be passed to the table create operation */
253         void *arg_create;
254
255         /** Memory size to be reserved per classifier object entry for */
256         /** storing meta data */
257         uint32_t table_metadata_size;
258      };
259
260 To create an ACL table the ``rte_table_acl_params`` structure must be
261 initialised and assigned to ``arg_create`` in the
262 ``rte_flow_classify_table_params`` structure.
263
264 .. code-block:: c
265
266     struct rte_table_acl_params {
267         /** Name */
268         const char *name;
269
270         /** Maximum number of ACL rules in the table */
271         uint32_t n_rules;
272
273         /** Number of fields in the ACL rule specification */
274         uint32_t n_rule_fields;
275
276         /** Format specification of the fields of the ACL rule */
277         struct rte_acl_field_def field_format[RTE_ACL_MAX_FIELDS];
278     };
279
280 The fields for the ACL rule must also be initialised by the application.
281
282 An ACL table can be added to the ``Classifier`` for each ACL rule, for example
283 another table could be added for the IPv6 5-tuple rule.
284
285 Flow Parsing
286 ~~~~~~~~~~~~
287
288 The library currently supports three IPv4 5-tuple flow patterns, for UDP, TCP
289 and SCTP.
290
291 .. code-block:: c
292
293     /* Pattern for IPv4 5-tuple UDP filter */
294     static enum rte_flow_item_type pattern_ntuple_1[] = {
295         RTE_FLOW_ITEM_TYPE_ETH,
296         RTE_FLOW_ITEM_TYPE_IPV4,
297         RTE_FLOW_ITEM_TYPE_UDP,
298         RTE_FLOW_ITEM_TYPE_END,
299     };
300
301     /* Pattern for IPv4 5-tuple TCP filter */
302     static enum rte_flow_item_type pattern_ntuple_2[] = {
303         RTE_FLOW_ITEM_TYPE_ETH,
304         RTE_FLOW_ITEM_TYPE_IPV4,
305         RTE_FLOW_ITEM_TYPE_TCP,
306         RTE_FLOW_ITEM_TYPE_END,
307     };
308
309     /* Pattern for IPv4 5-tuple SCTP filter */
310     static enum rte_flow_item_type pattern_ntuple_3[] = {
311         RTE_FLOW_ITEM_TYPE_ETH,
312         RTE_FLOW_ITEM_TYPE_IPV4,
313         RTE_FLOW_ITEM_TYPE_SCTP,
314         RTE_FLOW_ITEM_TYPE_END,
315     };
316
317 The internal function ``flow_classify_parse_flow`` parses the
318 IPv4 5-tuple pattern, attributes and actions and returns the 5-tuple data in the
319 ``rte_eth_ntuple_filter`` structure.
320
321 .. code-block:: c
322
323     static int
324     flow_classify_parse_flow(
325                    const struct rte_flow_attr *attr,
326                    const struct rte_flow_item pattern[],
327                    const struct rte_flow_action actions[],
328                    struct rte_flow_error *error)
329
330 Adding Flow Rules
331 ~~~~~~~~~~~~~~~~~
332
333 The ``rte_flow_classify_table_entry_add`` API creates an
334 ``rte_flow_classify`` object which contains the flow_classify id and type, the
335 action, a union of add and delete keys and a union of rules.
336 It uses the ``flow_classify_parse_flow`` internal function for parsing the
337 flow parameters.
338 The 5-tuple ACL key data is obtained from the ``rte_eth_ntuple_filter``
339 structure populated by the ``classify_parse_ntuple_filter`` function which
340 parses the Flow rule.
341
342 .. code-block:: c
343
344     struct acl_keys {
345         struct rte_table_acl_rule_add_params key_add; /* add key */
346         struct rte_table_acl_rule_delete_params key_del; /* delete key */
347     };
348
349     struct classify_rules {
350         enum rte_flow_classify_rule_type type;
351         union {
352             struct rte_flow_classify_ipv4_5tuple ipv4_5tuple;
353         } u;
354     };
355
356     struct rte_flow_classify {
357         uint32_t id;  /* unique ID of classify object */
358         struct rte_flow_action action; /* action when match found */
359         struct classify_rules rules; /* union of rules */
360         union {
361             struct acl_keys key;
362         } u;
363         int key_found; /* rule key found in table */
364         void *entry; /* pointer to buffer to hold rule meta data */
365         void *entry_ptr; /* handle to the table entry for rule meta data */
366     };
367
368 It then calls the ``table[table_id].ops.f_add`` API to add the rule to the ACL
369 table.
370
371 Deleting Flow Rules
372 ~~~~~~~~~~~~~~~~~~~
373
374 The ``rte_flow_classify_table_entry_delete`` API calls the
375 ``table[table_id].ops.f_delete`` API to delete a rule from the ACL table.
376
377 Packet Matching
378 ~~~~~~~~~~~~~~~
379
380 The ``rte_flow_classifier_query`` API is used to find packets which match a
381 given flow Flow rule in the table.
382 This API calls the flow_classify_run internal function which calls the
383 ``table[table_id].ops.f_lookup`` API to see if any packets in a burst match any
384 of the Flow rules in the table.
385 The meta data for the highest priority rule matched for each packet is returned
386 in the entries array in the ``rte_flow_classify`` object.
387 The internal function ``action_apply`` implements the ``Count`` action which is
388 used to return data which matches a particular Flow rule.
389
390 The rte_flow_classifier_query API uses the following structures to return data
391 to the application.
392
393 .. code-block:: c
394
395     /** IPv4 5-tuple data */
396     struct rte_flow_classify_ipv4_5tuple {
397         uint32_t dst_ip;         /**< Destination IP address in big endian. */
398         uint32_t dst_ip_mask;    /**< Mask of destination IP address. */
399         uint32_t src_ip;         /**< Source IP address in big endian. */
400         uint32_t src_ip_mask;    /**< Mask of destination IP address. */
401         uint16_t dst_port;       /**< Destination port in big endian. */
402         uint16_t dst_port_mask;  /**< Mask of destination port. */
403         uint16_t src_port;       /**< Source Port in big endian. */
404         uint16_t src_port_mask;  /**< Mask of source port. */
405         uint8_t proto;           /**< L4 protocol. */
406         uint8_t proto_mask;      /**< Mask of L4 protocol. */
407     };
408
409     /**
410      * Flow stats
411      *
412      * For the count action, stats can be returned by the query API.
413      *
414      * Storage for stats is provided by the application.
415      *
416      *
417      */
418     struct rte_flow_classify_stats {
419         void *stats;
420     };
421
422     struct rte_flow_classify_5tuple_stats {
423         /** count of packets that match IPv4 5tuple pattern */
424         uint64_t counter1;
425         /** IPv4 5tuple data */
426         struct rte_flow_classify_ipv4_5tuple ipv4_5tuple;
427     };