057f7e71767c463f09ce03ec5fb49f15c822cd1b
[dpdk.git] / lib / librte_ether / rte_flow.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 6WIND S.A.
5  *   Copyright 2016 Mellanox.
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 6WIND S.A. 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_H_
35 #define RTE_FLOW_H_
36
37 /**
38  * @file
39  * RTE generic flow API
40  *
41  * This interface provides the ability to program packet matching and
42  * associated actions in hardware through flow rules.
43  */
44
45 #include <rte_arp.h>
46 #include <rte_ether.h>
47 #include <rte_icmp.h>
48 #include <rte_ip.h>
49 #include <rte_sctp.h>
50 #include <rte_tcp.h>
51 #include <rte_udp.h>
52 #include <rte_byteorder.h>
53
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 /**
59  * Flow rule attributes.
60  *
61  * Priorities are set on two levels: per group and per rule within groups.
62  *
63  * Lower values denote higher priority, the highest priority for both levels
64  * is 0, so that a rule with priority 0 in group 8 is always matched after a
65  * rule with priority 8 in group 0.
66  *
67  * Although optional, applications are encouraged to group similar rules as
68  * much as possible to fully take advantage of hardware capabilities
69  * (e.g. optimized matching) and work around limitations (e.g. a single
70  * pattern type possibly allowed in a given group).
71  *
72  * Group and priority levels are arbitrary and up to the application, they
73  * do not need to be contiguous nor start from 0, however the maximum number
74  * varies between devices and may be affected by existing flow rules.
75  *
76  * If a packet is matched by several rules of a given group for a given
77  * priority level, the outcome is undefined. It can take any path, may be
78  * duplicated or even cause unrecoverable errors.
79  *
80  * Note that support for more than a single group and priority level is not
81  * guaranteed.
82  *
83  * Flow rules can apply to inbound and/or outbound traffic (ingress/egress).
84  *
85  * Several pattern items and actions are valid and can be used in both
86  * directions. Those valid for only one direction are described as such.
87  *
88  * At least one direction must be specified.
89  *
90  * Specifying both directions at once for a given rule is not recommended
91  * but may be valid in a few cases (e.g. shared counter).
92  */
93 struct rte_flow_attr {
94         uint32_t group; /**< Priority group. */
95         uint32_t priority; /**< Priority level within group. */
96         uint32_t ingress:1; /**< Rule applies to ingress traffic. */
97         uint32_t egress:1; /**< Rule applies to egress traffic. */
98         uint32_t reserved:30; /**< Reserved, must be zero. */
99 };
100
101 /**
102  * Matching pattern item types.
103  *
104  * Pattern items fall in two categories:
105  *
106  * - Matching protocol headers and packet data (ANY, RAW, ETH, VLAN, IPV4,
107  *   IPV6, ICMP, UDP, TCP, SCTP, VXLAN and so on), usually associated with a
108  *   specification structure. These must be stacked in the same order as the
109  *   protocol layers to match, starting from the lowest.
110  *
111  * - Matching meta-data or affecting pattern processing (END, VOID, INVERT,
112  *   PF, VF, PORT and so on), often without a specification structure. Since
113  *   they do not match packet contents, these can be specified anywhere
114  *   within item lists without affecting others.
115  *
116  * See the description of individual types for more information. Those
117  * marked with [META] fall into the second category.
118  */
119 enum rte_flow_item_type {
120         /**
121          * [META]
122          *
123          * End marker for item lists. Prevents further processing of items,
124          * thereby ending the pattern.
125          *
126          * No associated specification structure.
127          */
128         RTE_FLOW_ITEM_TYPE_END,
129
130         /**
131          * [META]
132          *
133          * Used as a placeholder for convenience. It is ignored and simply
134          * discarded by PMDs.
135          *
136          * No associated specification structure.
137          */
138         RTE_FLOW_ITEM_TYPE_VOID,
139
140         /**
141          * [META]
142          *
143          * Inverted matching, i.e. process packets that do not match the
144          * pattern.
145          *
146          * No associated specification structure.
147          */
148         RTE_FLOW_ITEM_TYPE_INVERT,
149
150         /**
151          * Matches any protocol in place of the current layer, a single ANY
152          * may also stand for several protocol layers.
153          *
154          * See struct rte_flow_item_any.
155          */
156         RTE_FLOW_ITEM_TYPE_ANY,
157
158         /**
159          * [META]
160          *
161          * Matches packets addressed to the physical function of the device.
162          *
163          * If the underlying device function differs from the one that would
164          * normally receive the matched traffic, specifying this item
165          * prevents it from reaching that device unless the flow rule
166          * contains a PF action. Packets are not duplicated between device
167          * instances by default.
168          *
169          * No associated specification structure.
170          */
171         RTE_FLOW_ITEM_TYPE_PF,
172
173         /**
174          * [META]
175          *
176          * Matches packets addressed to a virtual function ID of the device.
177          *
178          * If the underlying device function differs from the one that would
179          * normally receive the matched traffic, specifying this item
180          * prevents it from reaching that device unless the flow rule
181          * contains a VF action. Packets are not duplicated between device
182          * instances by default.
183          *
184          * See struct rte_flow_item_vf.
185          */
186         RTE_FLOW_ITEM_TYPE_VF,
187
188         /**
189          * [META]
190          *
191          * Matches packets coming from the specified physical port of the
192          * underlying device.
193          *
194          * The first PORT item overrides the physical port normally
195          * associated with the specified DPDK input port (port_id). This
196          * item can be provided several times to match additional physical
197          * ports.
198          *
199          * See struct rte_flow_item_port.
200          */
201         RTE_FLOW_ITEM_TYPE_PORT,
202
203         /**
204          * Matches a byte string of a given length at a given offset.
205          *
206          * See struct rte_flow_item_raw.
207          */
208         RTE_FLOW_ITEM_TYPE_RAW,
209
210         /**
211          * Matches an Ethernet header.
212          *
213          * See struct rte_flow_item_eth.
214          */
215         RTE_FLOW_ITEM_TYPE_ETH,
216
217         /**
218          * Matches an 802.1Q/ad VLAN tag.
219          *
220          * See struct rte_flow_item_vlan.
221          */
222         RTE_FLOW_ITEM_TYPE_VLAN,
223
224         /**
225          * Matches an IPv4 header.
226          *
227          * See struct rte_flow_item_ipv4.
228          */
229         RTE_FLOW_ITEM_TYPE_IPV4,
230
231         /**
232          * Matches an IPv6 header.
233          *
234          * See struct rte_flow_item_ipv6.
235          */
236         RTE_FLOW_ITEM_TYPE_IPV6,
237
238         /**
239          * Matches an ICMP header.
240          *
241          * See struct rte_flow_item_icmp.
242          */
243         RTE_FLOW_ITEM_TYPE_ICMP,
244
245         /**
246          * Matches a UDP header.
247          *
248          * See struct rte_flow_item_udp.
249          */
250         RTE_FLOW_ITEM_TYPE_UDP,
251
252         /**
253          * Matches a TCP header.
254          *
255          * See struct rte_flow_item_tcp.
256          */
257         RTE_FLOW_ITEM_TYPE_TCP,
258
259         /**
260          * Matches a SCTP header.
261          *
262          * See struct rte_flow_item_sctp.
263          */
264         RTE_FLOW_ITEM_TYPE_SCTP,
265
266         /**
267          * Matches a VXLAN header.
268          *
269          * See struct rte_flow_item_vxlan.
270          */
271         RTE_FLOW_ITEM_TYPE_VXLAN,
272
273         /**
274          * Matches a E_TAG header.
275          *
276          * See struct rte_flow_item_e_tag.
277          */
278         RTE_FLOW_ITEM_TYPE_E_TAG,
279
280         /**
281          * Matches a NVGRE header.
282          *
283          * See struct rte_flow_item_nvgre.
284          */
285         RTE_FLOW_ITEM_TYPE_NVGRE,
286
287         /**
288          * Matches a MPLS header.
289          *
290          * See struct rte_flow_item_mpls.
291          */
292         RTE_FLOW_ITEM_TYPE_MPLS,
293
294         /**
295          * Matches a GRE header.
296          *
297          * See struct rte_flow_item_gre.
298          */
299         RTE_FLOW_ITEM_TYPE_GRE,
300 };
301
302 /**
303  * RTE_FLOW_ITEM_TYPE_ANY
304  *
305  * Matches any protocol in place of the current layer, a single ANY may also
306  * stand for several protocol layers.
307  *
308  * This is usually specified as the first pattern item when looking for a
309  * protocol anywhere in a packet.
310  *
311  * A zeroed mask stands for any number of layers.
312  */
313 struct rte_flow_item_any {
314         uint32_t num; /**< Number of layers covered. */
315 };
316
317 /** Default mask for RTE_FLOW_ITEM_TYPE_ANY. */
318 #ifndef __cplusplus
319 static const struct rte_flow_item_any rte_flow_item_any_mask = {
320         .num = 0x00000000,
321 };
322 #endif
323
324 /**
325  * RTE_FLOW_ITEM_TYPE_VF
326  *
327  * Matches packets addressed to a virtual function ID of the device.
328  *
329  * If the underlying device function differs from the one that would
330  * normally receive the matched traffic, specifying this item prevents it
331  * from reaching that device unless the flow rule contains a VF
332  * action. Packets are not duplicated between device instances by default.
333  *
334  * - Likely to return an error or never match any traffic if this causes a
335  *   VF device to match traffic addressed to a different VF.
336  * - Can be specified multiple times to match traffic addressed to several
337  *   VF IDs.
338  * - Can be combined with a PF item to match both PF and VF traffic.
339  *
340  * A zeroed mask can be used to match any VF ID.
341  */
342 struct rte_flow_item_vf {
343         uint32_t id; /**< Destination VF ID. */
344 };
345
346 /** Default mask for RTE_FLOW_ITEM_TYPE_VF. */
347 #ifndef __cplusplus
348 static const struct rte_flow_item_vf rte_flow_item_vf_mask = {
349         .id = 0x00000000,
350 };
351 #endif
352
353 /**
354  * RTE_FLOW_ITEM_TYPE_PORT
355  *
356  * Matches packets coming from the specified physical port of the underlying
357  * device.
358  *
359  * The first PORT item overrides the physical port normally associated with
360  * the specified DPDK input port (port_id). This item can be provided
361  * several times to match additional physical ports.
362  *
363  * Note that physical ports are not necessarily tied to DPDK input ports
364  * (port_id) when those are not under DPDK control. Possible values are
365  * specific to each device, they are not necessarily indexed from zero and
366  * may not be contiguous.
367  *
368  * As a device property, the list of allowed values as well as the value
369  * associated with a port_id should be retrieved by other means.
370  *
371  * A zeroed mask can be used to match any port index.
372  */
373 struct rte_flow_item_port {
374         uint32_t index; /**< Physical port index. */
375 };
376
377 /** Default mask for RTE_FLOW_ITEM_TYPE_PORT. */
378 #ifndef __cplusplus
379 static const struct rte_flow_item_port rte_flow_item_port_mask = {
380         .index = 0x00000000,
381 };
382 #endif
383
384 /**
385  * RTE_FLOW_ITEM_TYPE_RAW
386  *
387  * Matches a byte string of a given length at a given offset.
388  *
389  * Offset is either absolute (using the start of the packet) or relative to
390  * the end of the previous matched item in the stack, in which case negative
391  * values are allowed.
392  *
393  * If search is enabled, offset is used as the starting point. The search
394  * area can be delimited by setting limit to a nonzero value, which is the
395  * maximum number of bytes after offset where the pattern may start.
396  *
397  * Matching a zero-length pattern is allowed, doing so resets the relative
398  * offset for subsequent items.
399  *
400  * This type does not support ranges (struct rte_flow_item.last).
401  */
402 struct rte_flow_item_raw {
403         uint32_t relative:1; /**< Look for pattern after the previous item. */
404         uint32_t search:1; /**< Search pattern from offset (see also limit). */
405         uint32_t reserved:30; /**< Reserved, must be set to zero. */
406         int32_t offset; /**< Absolute or relative offset for pattern. */
407         uint16_t limit; /**< Search area limit for start of pattern. */
408         uint16_t length; /**< Pattern length. */
409         uint8_t pattern[]; /**< Byte string to look for. */
410 };
411
412 /** Default mask for RTE_FLOW_ITEM_TYPE_RAW. */
413 #ifndef __cplusplus
414 static const struct rte_flow_item_raw rte_flow_item_raw_mask = {
415         .relative = 1,
416         .search = 1,
417         .reserved = 0x3fffffff,
418         .offset = 0xffffffff,
419         .limit = 0xffff,
420         .length = 0xffff,
421 };
422 #endif
423
424 /**
425  * RTE_FLOW_ITEM_TYPE_ETH
426  *
427  * Matches an Ethernet header.
428  */
429 struct rte_flow_item_eth {
430         struct ether_addr dst; /**< Destination MAC. */
431         struct ether_addr src; /**< Source MAC. */
432         rte_be16_t type; /**< EtherType. */
433 };
434
435 /** Default mask for RTE_FLOW_ITEM_TYPE_ETH. */
436 #ifndef __cplusplus
437 static const struct rte_flow_item_eth rte_flow_item_eth_mask = {
438         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
439         .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
440         .type = RTE_BE16(0x0000),
441 };
442 #endif
443
444 /**
445  * RTE_FLOW_ITEM_TYPE_VLAN
446  *
447  * Matches an 802.1Q/ad VLAN tag.
448  *
449  * This type normally follows either RTE_FLOW_ITEM_TYPE_ETH or
450  * RTE_FLOW_ITEM_TYPE_VLAN.
451  */
452 struct rte_flow_item_vlan {
453         rte_be16_t tpid; /**< Tag protocol identifier. */
454         rte_be16_t tci; /**< Tag control information. */
455 };
456
457 /** Default mask for RTE_FLOW_ITEM_TYPE_VLAN. */
458 #ifndef __cplusplus
459 static const struct rte_flow_item_vlan rte_flow_item_vlan_mask = {
460         .tpid = RTE_BE16(0x0000),
461         .tci = RTE_BE16(0xffff),
462 };
463 #endif
464
465 /**
466  * RTE_FLOW_ITEM_TYPE_IPV4
467  *
468  * Matches an IPv4 header.
469  *
470  * Note: IPv4 options are handled by dedicated pattern items.
471  */
472 struct rte_flow_item_ipv4 {
473         struct ipv4_hdr hdr; /**< IPv4 header definition. */
474 };
475
476 /** Default mask for RTE_FLOW_ITEM_TYPE_IPV4. */
477 #ifndef __cplusplus
478 static const struct rte_flow_item_ipv4 rte_flow_item_ipv4_mask = {
479         .hdr = {
480                 .src_addr = RTE_BE32(0xffffffff),
481                 .dst_addr = RTE_BE32(0xffffffff),
482         },
483 };
484 #endif
485
486 /**
487  * RTE_FLOW_ITEM_TYPE_IPV6.
488  *
489  * Matches an IPv6 header.
490  *
491  * Note: IPv6 options are handled by dedicated pattern items.
492  */
493 struct rte_flow_item_ipv6 {
494         struct ipv6_hdr hdr; /**< IPv6 header definition. */
495 };
496
497 /** Default mask for RTE_FLOW_ITEM_TYPE_IPV6. */
498 #ifndef __cplusplus
499 static const struct rte_flow_item_ipv6 rte_flow_item_ipv6_mask = {
500         .hdr = {
501                 .src_addr =
502                         "\xff\xff\xff\xff\xff\xff\xff\xff"
503                         "\xff\xff\xff\xff\xff\xff\xff\xff",
504                 .dst_addr =
505                         "\xff\xff\xff\xff\xff\xff\xff\xff"
506                         "\xff\xff\xff\xff\xff\xff\xff\xff",
507         },
508 };
509 #endif
510
511 /**
512  * RTE_FLOW_ITEM_TYPE_ICMP.
513  *
514  * Matches an ICMP header.
515  */
516 struct rte_flow_item_icmp {
517         struct icmp_hdr hdr; /**< ICMP header definition. */
518 };
519
520 /** Default mask for RTE_FLOW_ITEM_TYPE_ICMP. */
521 #ifndef __cplusplus
522 static const struct rte_flow_item_icmp rte_flow_item_icmp_mask = {
523         .hdr = {
524                 .icmp_type = 0xff,
525                 .icmp_code = 0xff,
526         },
527 };
528 #endif
529
530 /**
531  * RTE_FLOW_ITEM_TYPE_UDP.
532  *
533  * Matches a UDP header.
534  */
535 struct rte_flow_item_udp {
536         struct udp_hdr hdr; /**< UDP header definition. */
537 };
538
539 /** Default mask for RTE_FLOW_ITEM_TYPE_UDP. */
540 #ifndef __cplusplus
541 static const struct rte_flow_item_udp rte_flow_item_udp_mask = {
542         .hdr = {
543                 .src_port = RTE_BE16(0xffff),
544                 .dst_port = RTE_BE16(0xffff),
545         },
546 };
547 #endif
548
549 /**
550  * RTE_FLOW_ITEM_TYPE_TCP.
551  *
552  * Matches a TCP header.
553  */
554 struct rte_flow_item_tcp {
555         struct tcp_hdr hdr; /**< TCP header definition. */
556 };
557
558 /** Default mask for RTE_FLOW_ITEM_TYPE_TCP. */
559 #ifndef __cplusplus
560 static const struct rte_flow_item_tcp rte_flow_item_tcp_mask = {
561         .hdr = {
562                 .src_port = RTE_BE16(0xffff),
563                 .dst_port = RTE_BE16(0xffff),
564         },
565 };
566 #endif
567
568 /**
569  * RTE_FLOW_ITEM_TYPE_SCTP.
570  *
571  * Matches a SCTP header.
572  */
573 struct rte_flow_item_sctp {
574         struct sctp_hdr hdr; /**< SCTP header definition. */
575 };
576
577 /** Default mask for RTE_FLOW_ITEM_TYPE_SCTP. */
578 #ifndef __cplusplus
579 static const struct rte_flow_item_sctp rte_flow_item_sctp_mask = {
580         .hdr = {
581                 .src_port = RTE_BE16(0xffff),
582                 .dst_port = RTE_BE16(0xffff),
583         },
584 };
585 #endif
586
587 /**
588  * RTE_FLOW_ITEM_TYPE_VXLAN.
589  *
590  * Matches a VXLAN header (RFC 7348).
591  */
592 struct rte_flow_item_vxlan {
593         uint8_t flags; /**< Normally 0x08 (I flag). */
594         uint8_t rsvd0[3]; /**< Reserved, normally 0x000000. */
595         uint8_t vni[3]; /**< VXLAN identifier. */
596         uint8_t rsvd1; /**< Reserved, normally 0x00. */
597 };
598
599 /** Default mask for RTE_FLOW_ITEM_TYPE_VXLAN. */
600 #ifndef __cplusplus
601 static const struct rte_flow_item_vxlan rte_flow_item_vxlan_mask = {
602         .vni = "\xff\xff\xff",
603 };
604 #endif
605
606 /**
607  * RTE_FLOW_ITEM_TYPE_E_TAG.
608  *
609  * Matches a E-tag header.
610  */
611 struct rte_flow_item_e_tag {
612         rte_be16_t tpid; /**< Tag protocol identifier (0x893F). */
613         /**
614          * E-Tag control information (E-TCI).
615          * E-PCP (3b), E-DEI (1b), ingress E-CID base (12b).
616          */
617         rte_be16_t epcp_edei_in_ecid_b;
618         /** Reserved (2b), GRP (2b), E-CID base (12b). */
619         rte_be16_t rsvd_grp_ecid_b;
620         uint8_t in_ecid_e; /**< Ingress E-CID ext. */
621         uint8_t ecid_e; /**< E-CID ext. */
622 };
623
624 /** Default mask for RTE_FLOW_ITEM_TYPE_E_TAG. */
625 #ifndef __cplusplus
626 static const struct rte_flow_item_e_tag rte_flow_item_e_tag_mask = {
627         .rsvd_grp_ecid_b = RTE_BE16(0x3fff),
628 };
629 #endif
630
631 /**
632  * RTE_FLOW_ITEM_TYPE_NVGRE.
633  *
634  * Matches a NVGRE header.
635  */
636 struct rte_flow_item_nvgre {
637         /**
638          * Checksum (1b), undefined (1b), key bit (1b), sequence number (1b),
639          * reserved 0 (9b), version (3b).
640          *
641          * c_k_s_rsvd0_ver must have value 0x2000 according to RFC 7637.
642          */
643         rte_be16_t c_k_s_rsvd0_ver;
644         rte_be16_t protocol; /**< Protocol type (0x6558). */
645         uint8_t tni[3]; /**< Virtual subnet ID. */
646         uint8_t flow_id; /**< Flow ID. */
647 };
648
649 /** Default mask for RTE_FLOW_ITEM_TYPE_NVGRE. */
650 #ifndef __cplusplus
651 static const struct rte_flow_item_nvgre rte_flow_item_nvgre_mask = {
652         .tni = "\xff\xff\xff",
653 };
654 #endif
655
656 /**
657  * RTE_FLOW_ITEM_TYPE_MPLS.
658  *
659  * Matches a MPLS header.
660  */
661 struct rte_flow_item_mpls {
662         /**
663          * Label (20b), TC (3b), Bottom of Stack (1b).
664          */
665         uint8_t label_tc_s[3];
666         uint8_t ttl; /** Time-to-Live. */
667 };
668
669 /** Default mask for RTE_FLOW_ITEM_TYPE_MPLS. */
670 #ifndef __cplusplus
671 static const struct rte_flow_item_mpls rte_flow_item_mpls_mask = {
672         .label_tc_s = "\xff\xff\xf0",
673 };
674 #endif
675
676 /**
677  * RTE_FLOW_ITEM_TYPE_GRE.
678  *
679  * Matches a GRE header.
680  */
681 struct rte_flow_item_gre {
682         /**
683          * Checksum (1b), reserved 0 (12b), version (3b).
684          * Refer to RFC 2784.
685          */
686         rte_be16_t c_rsvd0_ver;
687         rte_be16_t protocol; /**< Protocol type. */
688 };
689
690 /** Default mask for RTE_FLOW_ITEM_TYPE_GRE. */
691 #ifndef __cplusplus
692 static const struct rte_flow_item_gre rte_flow_item_gre_mask = {
693         .protocol = RTE_BE16(0xffff),
694 };
695 #endif
696
697 /**
698  * Matching pattern item definition.
699  *
700  * A pattern is formed by stacking items starting from the lowest protocol
701  * layer to match. This stacking restriction does not apply to meta items
702  * which can be placed anywhere in the stack without affecting the meaning
703  * of the resulting pattern.
704  *
705  * Patterns are terminated by END items.
706  *
707  * The spec field should be a valid pointer to a structure of the related
708  * item type. It may remain unspecified (NULL) in many cases to request
709  * broad (nonspecific) matching. In such cases, last and mask must also be
710  * set to NULL.
711  *
712  * Optionally, last can point to a structure of the same type to define an
713  * inclusive range. This is mostly supported by integer and address fields,
714  * may cause errors otherwise. Fields that do not support ranges must be set
715  * to 0 or to the same value as the corresponding fields in spec.
716  *
717  * Only the fields defined to nonzero values in the default masks (see
718  * rte_flow_item_{name}_mask constants) are considered relevant by
719  * default. This can be overridden by providing a mask structure of the
720  * same type with applicable bits set to one. It can also be used to
721  * partially filter out specific fields (e.g. as an alternate mean to match
722  * ranges of IP addresses).
723  *
724  * Mask is a simple bit-mask applied before interpreting the contents of
725  * spec and last, which may yield unexpected results if not used
726  * carefully. For example, if for an IPv4 address field, spec provides
727  * 10.1.2.3, last provides 10.3.4.5 and mask provides 255.255.0.0, the
728  * effective range becomes 10.1.0.0 to 10.3.255.255.
729  */
730 struct rte_flow_item {
731         enum rte_flow_item_type type; /**< Item type. */
732         const void *spec; /**< Pointer to item specification structure. */
733         const void *last; /**< Defines an inclusive range (spec to last). */
734         const void *mask; /**< Bit-mask applied to spec and last. */
735 };
736
737 /**
738  * Action types.
739  *
740  * Each possible action is represented by a type. Some have associated
741  * configuration structures. Several actions combined in a list can be
742  * affected to a flow rule. That list is not ordered.
743  *
744  * They fall in three categories:
745  *
746  * - Terminating actions (such as QUEUE, DROP, RSS, PF, VF) that prevent
747  *   processing matched packets by subsequent flow rules, unless overridden
748  *   with PASSTHRU.
749  *
750  * - Non terminating actions (PASSTHRU, DUP) that leave matched packets up
751  *   for additional processing by subsequent flow rules.
752  *
753  * - Other non terminating meta actions that do not affect the fate of
754  *   packets (END, VOID, MARK, FLAG, COUNT).
755  *
756  * When several actions are combined in a flow rule, they should all have
757  * different types (e.g. dropping a packet twice is not possible).
758  *
759  * Only the last action of a given type is taken into account. PMDs still
760  * perform error checking on the entire list.
761  *
762  * Note that PASSTHRU is the only action able to override a terminating
763  * rule.
764  */
765 enum rte_flow_action_type {
766         /**
767          * [META]
768          *
769          * End marker for action lists. Prevents further processing of
770          * actions, thereby ending the list.
771          *
772          * No associated configuration structure.
773          */
774         RTE_FLOW_ACTION_TYPE_END,
775
776         /**
777          * [META]
778          *
779          * Used as a placeholder for convenience. It is ignored and simply
780          * discarded by PMDs.
781          *
782          * No associated configuration structure.
783          */
784         RTE_FLOW_ACTION_TYPE_VOID,
785
786         /**
787          * Leaves packets up for additional processing by subsequent flow
788          * rules. This is the default when a rule does not contain a
789          * terminating action, but can be specified to force a rule to
790          * become non-terminating.
791          *
792          * No associated configuration structure.
793          */
794         RTE_FLOW_ACTION_TYPE_PASSTHRU,
795
796         /**
797          * [META]
798          *
799          * Attaches an integer value to packets and sets PKT_RX_FDIR and
800          * PKT_RX_FDIR_ID mbuf flags.
801          *
802          * See struct rte_flow_action_mark.
803          */
804         RTE_FLOW_ACTION_TYPE_MARK,
805
806         /**
807          * [META]
808          *
809          * Flags packets. Similar to MARK without a specific value; only
810          * sets the PKT_RX_FDIR mbuf flag.
811          *
812          * No associated configuration structure.
813          */
814         RTE_FLOW_ACTION_TYPE_FLAG,
815
816         /**
817          * Assigns packets to a given queue index.
818          *
819          * See struct rte_flow_action_queue.
820          */
821         RTE_FLOW_ACTION_TYPE_QUEUE,
822
823         /**
824          * Drops packets.
825          *
826          * PASSTHRU overrides this action if both are specified.
827          *
828          * No associated configuration structure.
829          */
830         RTE_FLOW_ACTION_TYPE_DROP,
831
832         /**
833          * [META]
834          *
835          * Enables counters for this rule.
836          *
837          * These counters can be retrieved and reset through rte_flow_query(),
838          * see struct rte_flow_query_count.
839          *
840          * No associated configuration structure.
841          */
842         RTE_FLOW_ACTION_TYPE_COUNT,
843
844         /**
845          * Duplicates packets to a given queue index.
846          *
847          * This is normally combined with QUEUE, however when used alone, it
848          * is actually similar to QUEUE + PASSTHRU.
849          *
850          * See struct rte_flow_action_dup.
851          */
852         RTE_FLOW_ACTION_TYPE_DUP,
853
854         /**
855          * Similar to QUEUE, except RSS is additionally performed on packets
856          * to spread them among several queues according to the provided
857          * parameters.
858          *
859          * See struct rte_flow_action_rss.
860          */
861         RTE_FLOW_ACTION_TYPE_RSS,
862
863         /**
864          * Redirects packets to the physical function (PF) of the current
865          * device.
866          *
867          * No associated configuration structure.
868          */
869         RTE_FLOW_ACTION_TYPE_PF,
870
871         /**
872          * Redirects packets to the virtual function (VF) of the current
873          * device with the specified ID.
874          *
875          * See struct rte_flow_action_vf.
876          */
877         RTE_FLOW_ACTION_TYPE_VF,
878 };
879
880 /**
881  * RTE_FLOW_ACTION_TYPE_MARK
882  *
883  * Attaches an integer value to packets and sets PKT_RX_FDIR and
884  * PKT_RX_FDIR_ID mbuf flags.
885  *
886  * This value is arbitrary and application-defined. Maximum allowed value
887  * depends on the underlying implementation. It is returned in the
888  * hash.fdir.hi mbuf field.
889  */
890 struct rte_flow_action_mark {
891         uint32_t id; /**< Integer value to return with packets. */
892 };
893
894 /**
895  * RTE_FLOW_ACTION_TYPE_QUEUE
896  *
897  * Assign packets to a given queue index.
898  *
899  * Terminating by default.
900  */
901 struct rte_flow_action_queue {
902         uint16_t index; /**< Queue index to use. */
903 };
904
905 /**
906  * RTE_FLOW_ACTION_TYPE_COUNT (query)
907  *
908  * Query structure to retrieve and reset flow rule counters.
909  */
910 struct rte_flow_query_count {
911         uint32_t reset:1; /**< Reset counters after query [in]. */
912         uint32_t hits_set:1; /**< hits field is set [out]. */
913         uint32_t bytes_set:1; /**< bytes field is set [out]. */
914         uint32_t reserved:29; /**< Reserved, must be zero [in, out]. */
915         uint64_t hits; /**< Number of hits for this rule [out]. */
916         uint64_t bytes; /**< Number of bytes through this rule [out]. */
917 };
918
919 /**
920  * RTE_FLOW_ACTION_TYPE_DUP
921  *
922  * Duplicates packets to a given queue index.
923  *
924  * This is normally combined with QUEUE, however when used alone, it is
925  * actually similar to QUEUE + PASSTHRU.
926  *
927  * Non-terminating by default.
928  */
929 struct rte_flow_action_dup {
930         uint16_t index; /**< Queue index to duplicate packets to. */
931 };
932
933 /**
934  * RTE_FLOW_ACTION_TYPE_RSS
935  *
936  * Similar to QUEUE, except RSS is additionally performed on packets to
937  * spread them among several queues according to the provided parameters.
938  *
939  * Note: RSS hash result is stored in the hash.rss mbuf field which overlaps
940  * hash.fdir.lo. Since the MARK action sets the hash.fdir.hi field only,
941  * both can be requested simultaneously.
942  *
943  * Terminating by default.
944  */
945 struct rte_flow_action_rss {
946         const struct rte_eth_rss_conf *rss_conf; /**< RSS parameters. */
947         uint16_t num; /**< Number of entries in queue[]. */
948         uint16_t queue[]; /**< Queues indices to use. */
949 };
950
951 /**
952  * RTE_FLOW_ACTION_TYPE_VF
953  *
954  * Redirects packets to a virtual function (VF) of the current device.
955  *
956  * Packets matched by a VF pattern item can be redirected to their original
957  * VF ID instead of the specified one. This parameter may not be available
958  * and is not guaranteed to work properly if the VF part is matched by a
959  * prior flow rule or if packets are not addressed to a VF in the first
960  * place.
961  *
962  * Terminating by default.
963  */
964 struct rte_flow_action_vf {
965         uint32_t original:1; /**< Use original VF ID if possible. */
966         uint32_t reserved:31; /**< Reserved, must be zero. */
967         uint32_t id; /**< VF ID to redirect packets to. */
968 };
969
970 /**
971  * Definition of a single action.
972  *
973  * A list of actions is terminated by a END action.
974  *
975  * For simple actions without a configuration structure, conf remains NULL.
976  */
977 struct rte_flow_action {
978         enum rte_flow_action_type type; /**< Action type. */
979         const void *conf; /**< Pointer to action configuration structure. */
980 };
981
982 /**
983  * Opaque type returned after successfully creating a flow.
984  *
985  * This handle can be used to manage and query the related flow (e.g. to
986  * destroy it or retrieve counters).
987  */
988 struct rte_flow;
989
990 /**
991  * Verbose error types.
992  *
993  * Most of them provide the type of the object referenced by struct
994  * rte_flow_error.cause.
995  */
996 enum rte_flow_error_type {
997         RTE_FLOW_ERROR_TYPE_NONE, /**< No error. */
998         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, /**< Cause unspecified. */
999         RTE_FLOW_ERROR_TYPE_HANDLE, /**< Flow rule (handle). */
1000         RTE_FLOW_ERROR_TYPE_ATTR_GROUP, /**< Group field. */
1001         RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY, /**< Priority field. */
1002         RTE_FLOW_ERROR_TYPE_ATTR_INGRESS, /**< Ingress field. */
1003         RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, /**< Egress field. */
1004         RTE_FLOW_ERROR_TYPE_ATTR, /**< Attributes structure. */
1005         RTE_FLOW_ERROR_TYPE_ITEM_NUM, /**< Pattern length. */
1006         RTE_FLOW_ERROR_TYPE_ITEM, /**< Specific pattern item. */
1007         RTE_FLOW_ERROR_TYPE_ACTION_NUM, /**< Number of actions. */
1008         RTE_FLOW_ERROR_TYPE_ACTION, /**< Specific action. */
1009 };
1010
1011 /**
1012  * Verbose error structure definition.
1013  *
1014  * This object is normally allocated by applications and set by PMDs, the
1015  * message points to a constant string which does not need to be freed by
1016  * the application, however its pointer can be considered valid only as long
1017  * as its associated DPDK port remains configured. Closing the underlying
1018  * device or unloading the PMD invalidates it.
1019  *
1020  * Both cause and message may be NULL regardless of the error type.
1021  */
1022 struct rte_flow_error {
1023         enum rte_flow_error_type type; /**< Cause field and error types. */
1024         const void *cause; /**< Object responsible for the error. */
1025         const char *message; /**< Human-readable error message. */
1026 };
1027
1028 /**
1029  * Check whether a flow rule can be created on a given port.
1030  *
1031  * The flow rule is validated for correctness and whether it could be accepted
1032  * by the device given sufficient resources. The rule is checked against the
1033  * current device mode and queue configuration. The flow rule may also
1034  * optionally be validated against existing flow rules and device resources.
1035  * This function has no effect on the target device.
1036  *
1037  * The returned value is guaranteed to remain valid only as long as no
1038  * successful calls to rte_flow_create() or rte_flow_destroy() are made in
1039  * the meantime and no device parameter affecting flow rules in any way are
1040  * modified, due to possible collisions or resource limitations (although in
1041  * such cases EINVAL should not be returned).
1042  *
1043  * @param port_id
1044  *   Port identifier of Ethernet device.
1045  * @param[in] attr
1046  *   Flow rule attributes.
1047  * @param[in] pattern
1048  *   Pattern specification (list terminated by the END pattern item).
1049  * @param[in] actions
1050  *   Associated actions (list terminated by the END action).
1051  * @param[out] error
1052  *   Perform verbose error reporting if not NULL. PMDs initialize this
1053  *   structure in case of error only.
1054  *
1055  * @return
1056  *   0 if flow rule is valid and can be created. A negative errno value
1057  *   otherwise (rte_errno is also set), the following errors are defined:
1058  *
1059  *   -ENOSYS: underlying device does not support this functionality.
1060  *
1061  *   -EINVAL: unknown or invalid rule specification.
1062  *
1063  *   -ENOTSUP: valid but unsupported rule specification (e.g. partial
1064  *   bit-masks are unsupported).
1065  *
1066  *   -EEXIST: collision with an existing rule. Only returned if device
1067  *   supports flow rule collision checking and there was a flow rule
1068  *   collision. Not receiving this return code is no guarantee that creating
1069  *   the rule will not fail due to a collision.
1070  *
1071  *   -ENOMEM: not enough memory to execute the function, or if the device
1072  *   supports resource validation, resource limitation on the device.
1073  *
1074  *   -EBUSY: action cannot be performed due to busy device resources, may
1075  *   succeed if the affected queues or even the entire port are in a stopped
1076  *   state (see rte_eth_dev_rx_queue_stop() and rte_eth_dev_stop()).
1077  */
1078 int
1079 rte_flow_validate(uint8_t port_id,
1080                   const struct rte_flow_attr *attr,
1081                   const struct rte_flow_item pattern[],
1082                   const struct rte_flow_action actions[],
1083                   struct rte_flow_error *error);
1084
1085 /**
1086  * Create a flow rule on a given port.
1087  *
1088  * @param port_id
1089  *   Port identifier of Ethernet device.
1090  * @param[in] attr
1091  *   Flow rule attributes.
1092  * @param[in] pattern
1093  *   Pattern specification (list terminated by the END pattern item).
1094  * @param[in] actions
1095  *   Associated actions (list terminated by the END action).
1096  * @param[out] error
1097  *   Perform verbose error reporting if not NULL. PMDs initialize this
1098  *   structure in case of error only.
1099  *
1100  * @return
1101  *   A valid handle in case of success, NULL otherwise and rte_errno is set
1102  *   to the positive version of one of the error codes defined for
1103  *   rte_flow_validate().
1104  */
1105 struct rte_flow *
1106 rte_flow_create(uint8_t port_id,
1107                 const struct rte_flow_attr *attr,
1108                 const struct rte_flow_item pattern[],
1109                 const struct rte_flow_action actions[],
1110                 struct rte_flow_error *error);
1111
1112 /**
1113  * Destroy a flow rule on a given port.
1114  *
1115  * Failure to destroy a flow rule handle may occur when other flow rules
1116  * depend on it, and destroying it would result in an inconsistent state.
1117  *
1118  * This function is only guaranteed to succeed if handles are destroyed in
1119  * reverse order of their creation.
1120  *
1121  * @param port_id
1122  *   Port identifier of Ethernet device.
1123  * @param flow
1124  *   Flow rule handle to destroy.
1125  * @param[out] error
1126  *   Perform verbose error reporting if not NULL. PMDs initialize this
1127  *   structure in case of error only.
1128  *
1129  * @return
1130  *   0 on success, a negative errno value otherwise and rte_errno is set.
1131  */
1132 int
1133 rte_flow_destroy(uint8_t port_id,
1134                  struct rte_flow *flow,
1135                  struct rte_flow_error *error);
1136
1137 /**
1138  * Destroy all flow rules associated with a port.
1139  *
1140  * In the unlikely event of failure, handles are still considered destroyed
1141  * and no longer valid but the port must be assumed to be in an inconsistent
1142  * state.
1143  *
1144  * @param port_id
1145  *   Port identifier of Ethernet device.
1146  * @param[out] error
1147  *   Perform verbose error reporting if not NULL. PMDs initialize this
1148  *   structure in case of error only.
1149  *
1150  * @return
1151  *   0 on success, a negative errno value otherwise and rte_errno is set.
1152  */
1153 int
1154 rte_flow_flush(uint8_t port_id,
1155                struct rte_flow_error *error);
1156
1157 /**
1158  * Query an existing flow rule.
1159  *
1160  * This function allows retrieving flow-specific data such as counters.
1161  * Data is gathered by special actions which must be present in the flow
1162  * rule definition.
1163  *
1164  * \see RTE_FLOW_ACTION_TYPE_COUNT
1165  *
1166  * @param port_id
1167  *   Port identifier of Ethernet device.
1168  * @param flow
1169  *   Flow rule handle to query.
1170  * @param action
1171  *   Action type to query.
1172  * @param[in, out] data
1173  *   Pointer to storage for the associated query data type.
1174  * @param[out] error
1175  *   Perform verbose error reporting if not NULL. PMDs initialize this
1176  *   structure in case of error only.
1177  *
1178  * @return
1179  *   0 on success, a negative errno value otherwise and rte_errno is set.
1180  */
1181 int
1182 rte_flow_query(uint8_t port_id,
1183                struct rte_flow *flow,
1184                enum rte_flow_action_type action,
1185                void *data,
1186                struct rte_flow_error *error);
1187
1188 /**
1189  * Restrict ingress traffic to the defined flow rules.
1190  *
1191  * Isolated mode guarantees that all ingress traffic comes from defined flow
1192  * rules only (current and future).
1193  *
1194  * Besides making ingress more deterministic, it allows PMDs to safely reuse
1195  * resources otherwise assigned to handle the remaining traffic, such as
1196  * global RSS configuration settings, VLAN filters, MAC address entries,
1197  * legacy filter API rules and so on in order to expand the set of possible
1198  * flow rule types.
1199  *
1200  * Calling this function as soon as possible after device initialization,
1201  * ideally before the first call to rte_eth_dev_configure(), is recommended
1202  * to avoid possible failures due to conflicting settings.
1203  *
1204  * Once effective, leaving isolated mode may not be possible depending on
1205  * PMD implementation.
1206  *
1207  * Additionally, the following functionality has no effect on the underlying
1208  * port and may return errors such as ENOTSUP ("not supported"):
1209  *
1210  * - Toggling promiscuous mode.
1211  * - Toggling allmulticast mode.
1212  * - Configuring MAC addresses.
1213  * - Configuring multicast addresses.
1214  * - Configuring VLAN filters.
1215  * - Configuring Rx filters through the legacy API (e.g. FDIR).
1216  * - Configuring global RSS settings.
1217  *
1218  * @param port_id
1219  *   Port identifier of Ethernet device.
1220  * @param set
1221  *   Nonzero to enter isolated mode, attempt to leave it otherwise.
1222  * @param[out] error
1223  *   Perform verbose error reporting if not NULL. PMDs initialize this
1224  *   structure in case of error only.
1225  *
1226  * @return
1227  *   0 on success, a negative errno value otherwise and rte_errno is set.
1228  */
1229 int
1230 rte_flow_isolate(uint8_t port_id, int set, struct rte_flow_error *error);
1231
1232 #ifdef __cplusplus
1233 }
1234 #endif
1235
1236 #endif /* RTE_FLOW_H_ */