bba6169f001a4a309996d1dad3589bff7f5f2d01
[deb_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          * [META]
303          *
304          * Fuzzy pattern match, expect faster than default.
305          *
306          * This is for device that support fuzzy matching option.
307          * Usually a fuzzy matching is fast but the cost is accuracy.
308          *
309          * See struct rte_flow_item_fuzzy.
310          */
311         RTE_FLOW_ITEM_TYPE_FUZZY,
312 };
313
314 /**
315  * RTE_FLOW_ITEM_TYPE_ANY
316  *
317  * Matches any protocol in place of the current layer, a single ANY may also
318  * stand for several protocol layers.
319  *
320  * This is usually specified as the first pattern item when looking for a
321  * protocol anywhere in a packet.
322  *
323  * A zeroed mask stands for any number of layers.
324  */
325 struct rte_flow_item_any {
326         uint32_t num; /**< Number of layers covered. */
327 };
328
329 /** Default mask for RTE_FLOW_ITEM_TYPE_ANY. */
330 #ifndef __cplusplus
331 static const struct rte_flow_item_any rte_flow_item_any_mask = {
332         .num = 0x00000000,
333 };
334 #endif
335
336 /**
337  * RTE_FLOW_ITEM_TYPE_VF
338  *
339  * Matches packets addressed to a virtual function ID of the device.
340  *
341  * If the underlying device function differs from the one that would
342  * normally receive the matched traffic, specifying this item prevents it
343  * from reaching that device unless the flow rule contains a VF
344  * action. Packets are not duplicated between device instances by default.
345  *
346  * - Likely to return an error or never match any traffic if this causes a
347  *   VF device to match traffic addressed to a different VF.
348  * - Can be specified multiple times to match traffic addressed to several
349  *   VF IDs.
350  * - Can be combined with a PF item to match both PF and VF traffic.
351  *
352  * A zeroed mask can be used to match any VF ID.
353  */
354 struct rte_flow_item_vf {
355         uint32_t id; /**< Destination VF ID. */
356 };
357
358 /** Default mask for RTE_FLOW_ITEM_TYPE_VF. */
359 #ifndef __cplusplus
360 static const struct rte_flow_item_vf rte_flow_item_vf_mask = {
361         .id = 0x00000000,
362 };
363 #endif
364
365 /**
366  * RTE_FLOW_ITEM_TYPE_PORT
367  *
368  * Matches packets coming from the specified physical port of the underlying
369  * device.
370  *
371  * The first PORT item overrides the physical port normally associated with
372  * the specified DPDK input port (port_id). This item can be provided
373  * several times to match additional physical ports.
374  *
375  * Note that physical ports are not necessarily tied to DPDK input ports
376  * (port_id) when those are not under DPDK control. Possible values are
377  * specific to each device, they are not necessarily indexed from zero and
378  * may not be contiguous.
379  *
380  * As a device property, the list of allowed values as well as the value
381  * associated with a port_id should be retrieved by other means.
382  *
383  * A zeroed mask can be used to match any port index.
384  */
385 struct rte_flow_item_port {
386         uint32_t index; /**< Physical port index. */
387 };
388
389 /** Default mask for RTE_FLOW_ITEM_TYPE_PORT. */
390 #ifndef __cplusplus
391 static const struct rte_flow_item_port rte_flow_item_port_mask = {
392         .index = 0x00000000,
393 };
394 #endif
395
396 /**
397  * RTE_FLOW_ITEM_TYPE_RAW
398  *
399  * Matches a byte string of a given length at a given offset.
400  *
401  * Offset is either absolute (using the start of the packet) or relative to
402  * the end of the previous matched item in the stack, in which case negative
403  * values are allowed.
404  *
405  * If search is enabled, offset is used as the starting point. The search
406  * area can be delimited by setting limit to a nonzero value, which is the
407  * maximum number of bytes after offset where the pattern may start.
408  *
409  * Matching a zero-length pattern is allowed, doing so resets the relative
410  * offset for subsequent items.
411  *
412  * This type does not support ranges (struct rte_flow_item.last).
413  */
414 struct rte_flow_item_raw {
415         uint32_t relative:1; /**< Look for pattern after the previous item. */
416         uint32_t search:1; /**< Search pattern from offset (see also limit). */
417         uint32_t reserved:30; /**< Reserved, must be set to zero. */
418         int32_t offset; /**< Absolute or relative offset for pattern. */
419         uint16_t limit; /**< Search area limit for start of pattern. */
420         uint16_t length; /**< Pattern length. */
421         uint8_t pattern[]; /**< Byte string to look for. */
422 };
423
424 /** Default mask for RTE_FLOW_ITEM_TYPE_RAW. */
425 #ifndef __cplusplus
426 static const struct rte_flow_item_raw rte_flow_item_raw_mask = {
427         .relative = 1,
428         .search = 1,
429         .reserved = 0x3fffffff,
430         .offset = 0xffffffff,
431         .limit = 0xffff,
432         .length = 0xffff,
433 };
434 #endif
435
436 /**
437  * RTE_FLOW_ITEM_TYPE_ETH
438  *
439  * Matches an Ethernet header.
440  */
441 struct rte_flow_item_eth {
442         struct ether_addr dst; /**< Destination MAC. */
443         struct ether_addr src; /**< Source MAC. */
444         rte_be16_t type; /**< EtherType. */
445 };
446
447 /** Default mask for RTE_FLOW_ITEM_TYPE_ETH. */
448 #ifndef __cplusplus
449 static const struct rte_flow_item_eth rte_flow_item_eth_mask = {
450         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
451         .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
452         .type = RTE_BE16(0x0000),
453 };
454 #endif
455
456 /**
457  * RTE_FLOW_ITEM_TYPE_VLAN
458  *
459  * Matches an 802.1Q/ad VLAN tag.
460  *
461  * This type normally follows either RTE_FLOW_ITEM_TYPE_ETH or
462  * RTE_FLOW_ITEM_TYPE_VLAN.
463  */
464 struct rte_flow_item_vlan {
465         rte_be16_t tpid; /**< Tag protocol identifier. */
466         rte_be16_t tci; /**< Tag control information. */
467 };
468
469 /** Default mask for RTE_FLOW_ITEM_TYPE_VLAN. */
470 #ifndef __cplusplus
471 static const struct rte_flow_item_vlan rte_flow_item_vlan_mask = {
472         .tpid = RTE_BE16(0x0000),
473         .tci = RTE_BE16(0xffff),
474 };
475 #endif
476
477 /**
478  * RTE_FLOW_ITEM_TYPE_IPV4
479  *
480  * Matches an IPv4 header.
481  *
482  * Note: IPv4 options are handled by dedicated pattern items.
483  */
484 struct rte_flow_item_ipv4 {
485         struct ipv4_hdr hdr; /**< IPv4 header definition. */
486 };
487
488 /** Default mask for RTE_FLOW_ITEM_TYPE_IPV4. */
489 #ifndef __cplusplus
490 static const struct rte_flow_item_ipv4 rte_flow_item_ipv4_mask = {
491         .hdr = {
492                 .src_addr = RTE_BE32(0xffffffff),
493                 .dst_addr = RTE_BE32(0xffffffff),
494         },
495 };
496 #endif
497
498 /**
499  * RTE_FLOW_ITEM_TYPE_IPV6.
500  *
501  * Matches an IPv6 header.
502  *
503  * Note: IPv6 options are handled by dedicated pattern items.
504  */
505 struct rte_flow_item_ipv6 {
506         struct ipv6_hdr hdr; /**< IPv6 header definition. */
507 };
508
509 /** Default mask for RTE_FLOW_ITEM_TYPE_IPV6. */
510 #ifndef __cplusplus
511 static const struct rte_flow_item_ipv6 rte_flow_item_ipv6_mask = {
512         .hdr = {
513                 .src_addr =
514                         "\xff\xff\xff\xff\xff\xff\xff\xff"
515                         "\xff\xff\xff\xff\xff\xff\xff\xff",
516                 .dst_addr =
517                         "\xff\xff\xff\xff\xff\xff\xff\xff"
518                         "\xff\xff\xff\xff\xff\xff\xff\xff",
519         },
520 };
521 #endif
522
523 /**
524  * RTE_FLOW_ITEM_TYPE_ICMP.
525  *
526  * Matches an ICMP header.
527  */
528 struct rte_flow_item_icmp {
529         struct icmp_hdr hdr; /**< ICMP header definition. */
530 };
531
532 /** Default mask for RTE_FLOW_ITEM_TYPE_ICMP. */
533 #ifndef __cplusplus
534 static const struct rte_flow_item_icmp rte_flow_item_icmp_mask = {
535         .hdr = {
536                 .icmp_type = 0xff,
537                 .icmp_code = 0xff,
538         },
539 };
540 #endif
541
542 /**
543  * RTE_FLOW_ITEM_TYPE_UDP.
544  *
545  * Matches a UDP header.
546  */
547 struct rte_flow_item_udp {
548         struct udp_hdr hdr; /**< UDP header definition. */
549 };
550
551 /** Default mask for RTE_FLOW_ITEM_TYPE_UDP. */
552 #ifndef __cplusplus
553 static const struct rte_flow_item_udp rte_flow_item_udp_mask = {
554         .hdr = {
555                 .src_port = RTE_BE16(0xffff),
556                 .dst_port = RTE_BE16(0xffff),
557         },
558 };
559 #endif
560
561 /**
562  * RTE_FLOW_ITEM_TYPE_TCP.
563  *
564  * Matches a TCP header.
565  */
566 struct rte_flow_item_tcp {
567         struct tcp_hdr hdr; /**< TCP header definition. */
568 };
569
570 /** Default mask for RTE_FLOW_ITEM_TYPE_TCP. */
571 #ifndef __cplusplus
572 static const struct rte_flow_item_tcp rte_flow_item_tcp_mask = {
573         .hdr = {
574                 .src_port = RTE_BE16(0xffff),
575                 .dst_port = RTE_BE16(0xffff),
576         },
577 };
578 #endif
579
580 /**
581  * RTE_FLOW_ITEM_TYPE_SCTP.
582  *
583  * Matches a SCTP header.
584  */
585 struct rte_flow_item_sctp {
586         struct sctp_hdr hdr; /**< SCTP header definition. */
587 };
588
589 /** Default mask for RTE_FLOW_ITEM_TYPE_SCTP. */
590 #ifndef __cplusplus
591 static const struct rte_flow_item_sctp rte_flow_item_sctp_mask = {
592         .hdr = {
593                 .src_port = RTE_BE16(0xffff),
594                 .dst_port = RTE_BE16(0xffff),
595         },
596 };
597 #endif
598
599 /**
600  * RTE_FLOW_ITEM_TYPE_VXLAN.
601  *
602  * Matches a VXLAN header (RFC 7348).
603  */
604 struct rte_flow_item_vxlan {
605         uint8_t flags; /**< Normally 0x08 (I flag). */
606         uint8_t rsvd0[3]; /**< Reserved, normally 0x000000. */
607         uint8_t vni[3]; /**< VXLAN identifier. */
608         uint8_t rsvd1; /**< Reserved, normally 0x00. */
609 };
610
611 /** Default mask for RTE_FLOW_ITEM_TYPE_VXLAN. */
612 #ifndef __cplusplus
613 static const struct rte_flow_item_vxlan rte_flow_item_vxlan_mask = {
614         .vni = "\xff\xff\xff",
615 };
616 #endif
617
618 /**
619  * RTE_FLOW_ITEM_TYPE_E_TAG.
620  *
621  * Matches a E-tag header.
622  */
623 struct rte_flow_item_e_tag {
624         rte_be16_t tpid; /**< Tag protocol identifier (0x893F). */
625         /**
626          * E-Tag control information (E-TCI).
627          * E-PCP (3b), E-DEI (1b), ingress E-CID base (12b).
628          */
629         rte_be16_t epcp_edei_in_ecid_b;
630         /** Reserved (2b), GRP (2b), E-CID base (12b). */
631         rte_be16_t rsvd_grp_ecid_b;
632         uint8_t in_ecid_e; /**< Ingress E-CID ext. */
633         uint8_t ecid_e; /**< E-CID ext. */
634 };
635
636 /** Default mask for RTE_FLOW_ITEM_TYPE_E_TAG. */
637 #ifndef __cplusplus
638 static const struct rte_flow_item_e_tag rte_flow_item_e_tag_mask = {
639         .rsvd_grp_ecid_b = RTE_BE16(0x3fff),
640 };
641 #endif
642
643 /**
644  * RTE_FLOW_ITEM_TYPE_NVGRE.
645  *
646  * Matches a NVGRE header.
647  */
648 struct rte_flow_item_nvgre {
649         /**
650          * Checksum (1b), undefined (1b), key bit (1b), sequence number (1b),
651          * reserved 0 (9b), version (3b).
652          *
653          * c_k_s_rsvd0_ver must have value 0x2000 according to RFC 7637.
654          */
655         rte_be16_t c_k_s_rsvd0_ver;
656         rte_be16_t protocol; /**< Protocol type (0x6558). */
657         uint8_t tni[3]; /**< Virtual subnet ID. */
658         uint8_t flow_id; /**< Flow ID. */
659 };
660
661 /** Default mask for RTE_FLOW_ITEM_TYPE_NVGRE. */
662 #ifndef __cplusplus
663 static const struct rte_flow_item_nvgre rte_flow_item_nvgre_mask = {
664         .tni = "\xff\xff\xff",
665 };
666 #endif
667
668 /**
669  * RTE_FLOW_ITEM_TYPE_MPLS.
670  *
671  * Matches a MPLS header.
672  */
673 struct rte_flow_item_mpls {
674         /**
675          * Label (20b), TC (3b), Bottom of Stack (1b).
676          */
677         uint8_t label_tc_s[3];
678         uint8_t ttl; /** Time-to-Live. */
679 };
680
681 /** Default mask for RTE_FLOW_ITEM_TYPE_MPLS. */
682 #ifndef __cplusplus
683 static const struct rte_flow_item_mpls rte_flow_item_mpls_mask = {
684         .label_tc_s = "\xff\xff\xf0",
685 };
686 #endif
687
688 /**
689  * RTE_FLOW_ITEM_TYPE_GRE.
690  *
691  * Matches a GRE header.
692  */
693 struct rte_flow_item_gre {
694         /**
695          * Checksum (1b), reserved 0 (12b), version (3b).
696          * Refer to RFC 2784.
697          */
698         rte_be16_t c_rsvd0_ver;
699         rte_be16_t protocol; /**< Protocol type. */
700 };
701
702 /** Default mask for RTE_FLOW_ITEM_TYPE_GRE. */
703 #ifndef __cplusplus
704 static const struct rte_flow_item_gre rte_flow_item_gre_mask = {
705         .protocol = RTE_BE16(0xffff),
706 };
707 #endif
708
709 /**
710  * RTE_FLOW_ITEM_TYPE_FUZZY
711  *
712  * Fuzzy pattern match, expect faster than default.
713  *
714  * This is for device that support fuzzy match option.
715  * Usually a fuzzy match is fast but the cost is accuracy.
716  * i.e. Signature Match only match pattern's hash value, but it is
717  * possible two different patterns have the same hash value.
718  *
719  * Matching accuracy level can be configure by threshold.
720  * Driver can divide the range of threshold and map to different
721  * accuracy levels that device support.
722  *
723  * Threshold 0 means perfect match (no fuzziness), while threshold
724  * 0xffffffff means fuzziest match.
725  */
726 struct rte_flow_item_fuzzy {
727         uint32_t thresh; /**< Accuracy threshold. */
728 };
729
730 /** Default mask for RTE_FLOW_ITEM_TYPE_FUZZY. */
731 #ifndef __cplusplus
732 static const struct rte_flow_item_fuzzy rte_flow_item_fuzzy_mask = {
733         .thresh = 0xffffffff,
734 };
735 #endif
736
737 /**
738  * Matching pattern item definition.
739  *
740  * A pattern is formed by stacking items starting from the lowest protocol
741  * layer to match. This stacking restriction does not apply to meta items
742  * which can be placed anywhere in the stack without affecting the meaning
743  * of the resulting pattern.
744  *
745  * Patterns are terminated by END items.
746  *
747  * The spec field should be a valid pointer to a structure of the related
748  * item type. It may remain unspecified (NULL) in many cases to request
749  * broad (nonspecific) matching. In such cases, last and mask must also be
750  * set to NULL.
751  *
752  * Optionally, last can point to a structure of the same type to define an
753  * inclusive range. This is mostly supported by integer and address fields,
754  * may cause errors otherwise. Fields that do not support ranges must be set
755  * to 0 or to the same value as the corresponding fields in spec.
756  *
757  * Only the fields defined to nonzero values in the default masks (see
758  * rte_flow_item_{name}_mask constants) are considered relevant by
759  * default. This can be overridden by providing a mask structure of the
760  * same type with applicable bits set to one. It can also be used to
761  * partially filter out specific fields (e.g. as an alternate mean to match
762  * ranges of IP addresses).
763  *
764  * Mask is a simple bit-mask applied before interpreting the contents of
765  * spec and last, which may yield unexpected results if not used
766  * carefully. For example, if for an IPv4 address field, spec provides
767  * 10.1.2.3, last provides 10.3.4.5 and mask provides 255.255.0.0, the
768  * effective range becomes 10.1.0.0 to 10.3.255.255.
769  */
770 struct rte_flow_item {
771         enum rte_flow_item_type type; /**< Item type. */
772         const void *spec; /**< Pointer to item specification structure. */
773         const void *last; /**< Defines an inclusive range (spec to last). */
774         const void *mask; /**< Bit-mask applied to spec and last. */
775 };
776
777 /**
778  * Action types.
779  *
780  * Each possible action is represented by a type. Some have associated
781  * configuration structures. Several actions combined in a list can be
782  * affected to a flow rule. That list is not ordered.
783  *
784  * They fall in three categories:
785  *
786  * - Terminating actions (such as QUEUE, DROP, RSS, PF, VF) that prevent
787  *   processing matched packets by subsequent flow rules, unless overridden
788  *   with PASSTHRU.
789  *
790  * - Non terminating actions (PASSTHRU, DUP) that leave matched packets up
791  *   for additional processing by subsequent flow rules.
792  *
793  * - Other non terminating meta actions that do not affect the fate of
794  *   packets (END, VOID, MARK, FLAG, COUNT).
795  *
796  * When several actions are combined in a flow rule, they should all have
797  * different types (e.g. dropping a packet twice is not possible).
798  *
799  * Only the last action of a given type is taken into account. PMDs still
800  * perform error checking on the entire list.
801  *
802  * Note that PASSTHRU is the only action able to override a terminating
803  * rule.
804  */
805 enum rte_flow_action_type {
806         /**
807          * [META]
808          *
809          * End marker for action lists. Prevents further processing of
810          * actions, thereby ending the list.
811          *
812          * No associated configuration structure.
813          */
814         RTE_FLOW_ACTION_TYPE_END,
815
816         /**
817          * [META]
818          *
819          * Used as a placeholder for convenience. It is ignored and simply
820          * discarded by PMDs.
821          *
822          * No associated configuration structure.
823          */
824         RTE_FLOW_ACTION_TYPE_VOID,
825
826         /**
827          * Leaves packets up for additional processing by subsequent flow
828          * rules. This is the default when a rule does not contain a
829          * terminating action, but can be specified to force a rule to
830          * become non-terminating.
831          *
832          * No associated configuration structure.
833          */
834         RTE_FLOW_ACTION_TYPE_PASSTHRU,
835
836         /**
837          * [META]
838          *
839          * Attaches an integer value to packets and sets PKT_RX_FDIR and
840          * PKT_RX_FDIR_ID mbuf flags.
841          *
842          * See struct rte_flow_action_mark.
843          */
844         RTE_FLOW_ACTION_TYPE_MARK,
845
846         /**
847          * [META]
848          *
849          * Flags packets. Similar to MARK without a specific value; only
850          * sets the PKT_RX_FDIR mbuf flag.
851          *
852          * No associated configuration structure.
853          */
854         RTE_FLOW_ACTION_TYPE_FLAG,
855
856         /**
857          * Assigns packets to a given queue index.
858          *
859          * See struct rte_flow_action_queue.
860          */
861         RTE_FLOW_ACTION_TYPE_QUEUE,
862
863         /**
864          * Drops packets.
865          *
866          * PASSTHRU overrides this action if both are specified.
867          *
868          * No associated configuration structure.
869          */
870         RTE_FLOW_ACTION_TYPE_DROP,
871
872         /**
873          * [META]
874          *
875          * Enables counters for this rule.
876          *
877          * These counters can be retrieved and reset through rte_flow_query(),
878          * see struct rte_flow_query_count.
879          *
880          * No associated configuration structure.
881          */
882         RTE_FLOW_ACTION_TYPE_COUNT,
883
884         /**
885          * Duplicates packets to a given queue index.
886          *
887          * This is normally combined with QUEUE, however when used alone, it
888          * is actually similar to QUEUE + PASSTHRU.
889          *
890          * See struct rte_flow_action_dup.
891          */
892         RTE_FLOW_ACTION_TYPE_DUP,
893
894         /**
895          * Similar to QUEUE, except RSS is additionally performed on packets
896          * to spread them among several queues according to the provided
897          * parameters.
898          *
899          * See struct rte_flow_action_rss.
900          */
901         RTE_FLOW_ACTION_TYPE_RSS,
902
903         /**
904          * Redirects packets to the physical function (PF) of the current
905          * device.
906          *
907          * No associated configuration structure.
908          */
909         RTE_FLOW_ACTION_TYPE_PF,
910
911         /**
912          * Redirects packets to the virtual function (VF) of the current
913          * device with the specified ID.
914          *
915          * See struct rte_flow_action_vf.
916          */
917         RTE_FLOW_ACTION_TYPE_VF,
918 };
919
920 /**
921  * RTE_FLOW_ACTION_TYPE_MARK
922  *
923  * Attaches an integer value to packets and sets PKT_RX_FDIR and
924  * PKT_RX_FDIR_ID mbuf flags.
925  *
926  * This value is arbitrary and application-defined. Maximum allowed value
927  * depends on the underlying implementation. It is returned in the
928  * hash.fdir.hi mbuf field.
929  */
930 struct rte_flow_action_mark {
931         uint32_t id; /**< Integer value to return with packets. */
932 };
933
934 /**
935  * RTE_FLOW_ACTION_TYPE_QUEUE
936  *
937  * Assign packets to a given queue index.
938  *
939  * Terminating by default.
940  */
941 struct rte_flow_action_queue {
942         uint16_t index; /**< Queue index to use. */
943 };
944
945 /**
946  * RTE_FLOW_ACTION_TYPE_COUNT (query)
947  *
948  * Query structure to retrieve and reset flow rule counters.
949  */
950 struct rte_flow_query_count {
951         uint32_t reset:1; /**< Reset counters after query [in]. */
952         uint32_t hits_set:1; /**< hits field is set [out]. */
953         uint32_t bytes_set:1; /**< bytes field is set [out]. */
954         uint32_t reserved:29; /**< Reserved, must be zero [in, out]. */
955         uint64_t hits; /**< Number of hits for this rule [out]. */
956         uint64_t bytes; /**< Number of bytes through this rule [out]. */
957 };
958
959 /**
960  * RTE_FLOW_ACTION_TYPE_DUP
961  *
962  * Duplicates packets to a given queue index.
963  *
964  * This is normally combined with QUEUE, however when used alone, it is
965  * actually similar to QUEUE + PASSTHRU.
966  *
967  * Non-terminating by default.
968  */
969 struct rte_flow_action_dup {
970         uint16_t index; /**< Queue index to duplicate packets to. */
971 };
972
973 /**
974  * RTE_FLOW_ACTION_TYPE_RSS
975  *
976  * Similar to QUEUE, except RSS is additionally performed on packets to
977  * spread them among several queues according to the provided parameters.
978  *
979  * Note: RSS hash result is stored in the hash.rss mbuf field which overlaps
980  * hash.fdir.lo. Since the MARK action sets the hash.fdir.hi field only,
981  * both can be requested simultaneously.
982  *
983  * Terminating by default.
984  */
985 struct rte_flow_action_rss {
986         const struct rte_eth_rss_conf *rss_conf; /**< RSS parameters. */
987         uint16_t num; /**< Number of entries in queue[]. */
988         uint16_t queue[]; /**< Queues indices to use. */
989 };
990
991 /**
992  * RTE_FLOW_ACTION_TYPE_VF
993  *
994  * Redirects packets to a virtual function (VF) of the current device.
995  *
996  * Packets matched by a VF pattern item can be redirected to their original
997  * VF ID instead of the specified one. This parameter may not be available
998  * and is not guaranteed to work properly if the VF part is matched by a
999  * prior flow rule or if packets are not addressed to a VF in the first
1000  * place.
1001  *
1002  * Terminating by default.
1003  */
1004 struct rte_flow_action_vf {
1005         uint32_t original:1; /**< Use original VF ID if possible. */
1006         uint32_t reserved:31; /**< Reserved, must be zero. */
1007         uint32_t id; /**< VF ID to redirect packets to. */
1008 };
1009
1010 /**
1011  * Definition of a single action.
1012  *
1013  * A list of actions is terminated by a END action.
1014  *
1015  * For simple actions without a configuration structure, conf remains NULL.
1016  */
1017 struct rte_flow_action {
1018         enum rte_flow_action_type type; /**< Action type. */
1019         const void *conf; /**< Pointer to action configuration structure. */
1020 };
1021
1022 /**
1023  * Opaque type returned after successfully creating a flow.
1024  *
1025  * This handle can be used to manage and query the related flow (e.g. to
1026  * destroy it or retrieve counters).
1027  */
1028 struct rte_flow;
1029
1030 /**
1031  * Verbose error types.
1032  *
1033  * Most of them provide the type of the object referenced by struct
1034  * rte_flow_error.cause.
1035  */
1036 enum rte_flow_error_type {
1037         RTE_FLOW_ERROR_TYPE_NONE, /**< No error. */
1038         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, /**< Cause unspecified. */
1039         RTE_FLOW_ERROR_TYPE_HANDLE, /**< Flow rule (handle). */
1040         RTE_FLOW_ERROR_TYPE_ATTR_GROUP, /**< Group field. */
1041         RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY, /**< Priority field. */
1042         RTE_FLOW_ERROR_TYPE_ATTR_INGRESS, /**< Ingress field. */
1043         RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, /**< Egress field. */
1044         RTE_FLOW_ERROR_TYPE_ATTR, /**< Attributes structure. */
1045         RTE_FLOW_ERROR_TYPE_ITEM_NUM, /**< Pattern length. */
1046         RTE_FLOW_ERROR_TYPE_ITEM, /**< Specific pattern item. */
1047         RTE_FLOW_ERROR_TYPE_ACTION_NUM, /**< Number of actions. */
1048         RTE_FLOW_ERROR_TYPE_ACTION, /**< Specific action. */
1049 };
1050
1051 /**
1052  * Verbose error structure definition.
1053  *
1054  * This object is normally allocated by applications and set by PMDs, the
1055  * message points to a constant string which does not need to be freed by
1056  * the application, however its pointer can be considered valid only as long
1057  * as its associated DPDK port remains configured. Closing the underlying
1058  * device or unloading the PMD invalidates it.
1059  *
1060  * Both cause and message may be NULL regardless of the error type.
1061  */
1062 struct rte_flow_error {
1063         enum rte_flow_error_type type; /**< Cause field and error types. */
1064         const void *cause; /**< Object responsible for the error. */
1065         const char *message; /**< Human-readable error message. */
1066 };
1067
1068 /**
1069  * Check whether a flow rule can be created on a given port.
1070  *
1071  * The flow rule is validated for correctness and whether it could be accepted
1072  * by the device given sufficient resources. The rule is checked against the
1073  * current device mode and queue configuration. The flow rule may also
1074  * optionally be validated against existing flow rules and device resources.
1075  * This function has no effect on the target device.
1076  *
1077  * The returned value is guaranteed to remain valid only as long as no
1078  * successful calls to rte_flow_create() or rte_flow_destroy() are made in
1079  * the meantime and no device parameter affecting flow rules in any way are
1080  * modified, due to possible collisions or resource limitations (although in
1081  * such cases EINVAL should not be returned).
1082  *
1083  * @param port_id
1084  *   Port identifier of Ethernet device.
1085  * @param[in] attr
1086  *   Flow rule attributes.
1087  * @param[in] pattern
1088  *   Pattern specification (list terminated by the END pattern item).
1089  * @param[in] actions
1090  *   Associated actions (list terminated by the END action).
1091  * @param[out] error
1092  *   Perform verbose error reporting if not NULL. PMDs initialize this
1093  *   structure in case of error only.
1094  *
1095  * @return
1096  *   0 if flow rule is valid and can be created. A negative errno value
1097  *   otherwise (rte_errno is also set), the following errors are defined:
1098  *
1099  *   -ENOSYS: underlying device does not support this functionality.
1100  *
1101  *   -EINVAL: unknown or invalid rule specification.
1102  *
1103  *   -ENOTSUP: valid but unsupported rule specification (e.g. partial
1104  *   bit-masks are unsupported).
1105  *
1106  *   -EEXIST: collision with an existing rule. Only returned if device
1107  *   supports flow rule collision checking and there was a flow rule
1108  *   collision. Not receiving this return code is no guarantee that creating
1109  *   the rule will not fail due to a collision.
1110  *
1111  *   -ENOMEM: not enough memory to execute the function, or if the device
1112  *   supports resource validation, resource limitation on the device.
1113  *
1114  *   -EBUSY: action cannot be performed due to busy device resources, may
1115  *   succeed if the affected queues or even the entire port are in a stopped
1116  *   state (see rte_eth_dev_rx_queue_stop() and rte_eth_dev_stop()).
1117  */
1118 int
1119 rte_flow_validate(uint8_t port_id,
1120                   const struct rte_flow_attr *attr,
1121                   const struct rte_flow_item pattern[],
1122                   const struct rte_flow_action actions[],
1123                   struct rte_flow_error *error);
1124
1125 /**
1126  * Create a flow rule on a given port.
1127  *
1128  * @param port_id
1129  *   Port identifier of Ethernet device.
1130  * @param[in] attr
1131  *   Flow rule attributes.
1132  * @param[in] pattern
1133  *   Pattern specification (list terminated by the END pattern item).
1134  * @param[in] actions
1135  *   Associated actions (list terminated by the END action).
1136  * @param[out] error
1137  *   Perform verbose error reporting if not NULL. PMDs initialize this
1138  *   structure in case of error only.
1139  *
1140  * @return
1141  *   A valid handle in case of success, NULL otherwise and rte_errno is set
1142  *   to the positive version of one of the error codes defined for
1143  *   rte_flow_validate().
1144  */
1145 struct rte_flow *
1146 rte_flow_create(uint8_t port_id,
1147                 const struct rte_flow_attr *attr,
1148                 const struct rte_flow_item pattern[],
1149                 const struct rte_flow_action actions[],
1150                 struct rte_flow_error *error);
1151
1152 /**
1153  * Destroy a flow rule on a given port.
1154  *
1155  * Failure to destroy a flow rule handle may occur when other flow rules
1156  * depend on it, and destroying it would result in an inconsistent state.
1157  *
1158  * This function is only guaranteed to succeed if handles are destroyed in
1159  * reverse order of their creation.
1160  *
1161  * @param port_id
1162  *   Port identifier of Ethernet device.
1163  * @param flow
1164  *   Flow rule handle to destroy.
1165  * @param[out] error
1166  *   Perform verbose error reporting if not NULL. PMDs initialize this
1167  *   structure in case of error only.
1168  *
1169  * @return
1170  *   0 on success, a negative errno value otherwise and rte_errno is set.
1171  */
1172 int
1173 rte_flow_destroy(uint8_t port_id,
1174                  struct rte_flow *flow,
1175                  struct rte_flow_error *error);
1176
1177 /**
1178  * Destroy all flow rules associated with a port.
1179  *
1180  * In the unlikely event of failure, handles are still considered destroyed
1181  * and no longer valid but the port must be assumed to be in an inconsistent
1182  * state.
1183  *
1184  * @param port_id
1185  *   Port identifier of Ethernet device.
1186  * @param[out] error
1187  *   Perform verbose error reporting if not NULL. PMDs initialize this
1188  *   structure in case of error only.
1189  *
1190  * @return
1191  *   0 on success, a negative errno value otherwise and rte_errno is set.
1192  */
1193 int
1194 rte_flow_flush(uint8_t port_id,
1195                struct rte_flow_error *error);
1196
1197 /**
1198  * Query an existing flow rule.
1199  *
1200  * This function allows retrieving flow-specific data such as counters.
1201  * Data is gathered by special actions which must be present in the flow
1202  * rule definition.
1203  *
1204  * \see RTE_FLOW_ACTION_TYPE_COUNT
1205  *
1206  * @param port_id
1207  *   Port identifier of Ethernet device.
1208  * @param flow
1209  *   Flow rule handle to query.
1210  * @param action
1211  *   Action type to query.
1212  * @param[in, out] data
1213  *   Pointer to storage for the associated query data type.
1214  * @param[out] error
1215  *   Perform verbose error reporting if not NULL. PMDs initialize this
1216  *   structure in case of error only.
1217  *
1218  * @return
1219  *   0 on success, a negative errno value otherwise and rte_errno is set.
1220  */
1221 int
1222 rte_flow_query(uint8_t port_id,
1223                struct rte_flow *flow,
1224                enum rte_flow_action_type action,
1225                void *data,
1226                struct rte_flow_error *error);
1227
1228 /**
1229  * Restrict ingress traffic to the defined flow rules.
1230  *
1231  * Isolated mode guarantees that all ingress traffic comes from defined flow
1232  * rules only (current and future).
1233  *
1234  * Besides making ingress more deterministic, it allows PMDs to safely reuse
1235  * resources otherwise assigned to handle the remaining traffic, such as
1236  * global RSS configuration settings, VLAN filters, MAC address entries,
1237  * legacy filter API rules and so on in order to expand the set of possible
1238  * flow rule types.
1239  *
1240  * Calling this function as soon as possible after device initialization,
1241  * ideally before the first call to rte_eth_dev_configure(), is recommended
1242  * to avoid possible failures due to conflicting settings.
1243  *
1244  * Once effective, leaving isolated mode may not be possible depending on
1245  * PMD implementation.
1246  *
1247  * Additionally, the following functionality has no effect on the underlying
1248  * port and may return errors such as ENOTSUP ("not supported"):
1249  *
1250  * - Toggling promiscuous mode.
1251  * - Toggling allmulticast mode.
1252  * - Configuring MAC addresses.
1253  * - Configuring multicast addresses.
1254  * - Configuring VLAN filters.
1255  * - Configuring Rx filters through the legacy API (e.g. FDIR).
1256  * - Configuring global RSS settings.
1257  *
1258  * @param port_id
1259  *   Port identifier of Ethernet device.
1260  * @param set
1261  *   Nonzero to enter isolated mode, attempt to leave it otherwise.
1262  * @param[out] error
1263  *   Perform verbose error reporting if not NULL. PMDs initialize this
1264  *   structure in case of error only.
1265  *
1266  * @return
1267  *   0 on success, a negative errno value otherwise and rte_errno is set.
1268  */
1269 int
1270 rte_flow_isolate(uint8_t port_id, int set, struct rte_flow_error *error);
1271
1272 /**
1273  * Generic flow representation.
1274  *
1275  * This form is sufficient to describe an rte_flow independently from any
1276  * PMD implementation and allows for replayability and identification.
1277  */
1278 struct rte_flow_desc {
1279         size_t size; /**< Allocated space including data[]. */
1280         struct rte_flow_attr attr; /**< Attributes. */
1281         struct rte_flow_item *items; /**< Items. */
1282         struct rte_flow_action *actions; /**< Actions. */
1283         uint8_t data[]; /**< Storage for items/actions. */
1284 };
1285
1286 /**
1287  * Copy an rte_flow rule description.
1288  *
1289  * @param[in] fd
1290  *   Flow rule description.
1291  * @param[in] len
1292  *   Total size of allocated data for the flow description.
1293  * @param[in] attr
1294  *   Flow rule attributes.
1295  * @param[in] items
1296  *   Pattern specification (list terminated by the END pattern item).
1297  * @param[in] actions
1298  *   Associated actions (list terminated by the END action).
1299  *
1300  * @return
1301  *   If len is greater or equal to the size of the flow, the total size of the
1302  *   flow description and its data.
1303  *   If len is lower than the size of the flow, the number of bytes that would
1304  *   have been written to desc had it been sufficient. Nothing is written.
1305  */
1306 size_t
1307 rte_flow_copy(struct rte_flow_desc *fd, size_t len,
1308               const struct rte_flow_attr *attr,
1309               const struct rte_flow_item *items,
1310               const struct rte_flow_action *actions);
1311
1312 #ifdef __cplusplus
1313 }
1314 #endif
1315
1316 #endif /* RTE_FLOW_H_ */