Imported Upstream version 16.04
[deb_dpdk.git] / examples / ip_pipeline / pipeline / pipeline_actions_common.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
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 Intel Corporation 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 #ifndef __INCLUDE_PIPELINE_ACTIONS_COMMON_H__
34 #define __INCLUDE_PIPELINE_ACTIONS_COMMON_H__
35
36 #include <stdint.h>
37
38 #include <rte_common.h>
39 #include <rte_cycles.h>
40 #include <rte_mbuf.h>
41 #include <rte_pipeline.h>
42
43 #define PIPELINE_PORT_IN_AH(f_ah, f_pkt_work, f_pkt4_work)              \
44 static int                                                              \
45 f_ah(                                                                   \
46         __rte_unused struct rte_pipeline *p,                            \
47         struct rte_mbuf **pkts,                                         \
48         uint32_t n_pkts,                                                \
49         void *arg)                                                      \
50 {                                                                       \
51         uint32_t i;                                                     \
52                                                                         \
53         for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4)                   \
54                 f_pkt4_work(&pkts[i], arg);                             \
55                                                                         \
56         for ( ; i < n_pkts; i++)                                        \
57                 f_pkt_work(pkts[i], arg);                               \
58                                                                         \
59         return 0;                                                       \
60 }
61
62 #define PIPELINE_PORT_IN_AH_HIJACK_ALL(f_ah, f_pkt_work, f_pkt4_work) \
63 static int                                                              \
64 f_ah(                                                                   \
65         struct rte_pipeline *p,                         \
66         struct rte_mbuf **pkts,                                 \
67         uint32_t n_pkts,                                                \
68         void *arg)                                              \
69 {                                                                       \
70         uint64_t pkt_mask = RTE_LEN2MASK(n_pkts, uint64_t);     \
71         uint32_t i;                                                     \
72                                                                         \
73         rte_pipeline_ah_packet_hijack(p, pkt_mask);     \
74                                                                         \
75         for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4)   \
76                 f_pkt4_work(&pkts[i], arg);                             \
77                                                                         \
78         for ( ; i < n_pkts; i++)                                \
79                 f_pkt_work(pkts[i], arg);                       \
80                                                                         \
81         return 0;                                                       \
82 }
83
84 #define PIPELINE_TABLE_AH_HIT(f_ah, f_pkt_work, f_pkt4_work)            \
85 static int                                                              \
86 f_ah(                                                                   \
87         __rte_unused struct rte_pipeline *p,                            \
88         struct rte_mbuf **pkts,                                         \
89         uint64_t pkts_in_mask,                                          \
90         struct rte_pipeline_table_entry **entries,                      \
91         void *arg)                                                      \
92 {                                                                       \
93         if ((pkts_in_mask & (pkts_in_mask + 1)) == 0) {                 \
94                 uint64_t n_pkts = __builtin_popcountll(pkts_in_mask);   \
95                 uint32_t i;                                             \
96                                                                         \
97                 for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4)           \
98                         f_pkt4_work(&pkts[i], &entries[i], arg);        \
99                                                                         \
100                 for ( ; i < n_pkts; i++)                                \
101                         f_pkt_work(pkts[i], entries[i], arg);           \
102         } else                                                          \
103                 for ( ; pkts_in_mask; ) {                               \
104                         uint32_t pos = __builtin_ctzll(pkts_in_mask);   \
105                         uint64_t pkt_mask = 1LLU << pos;                \
106                                                                         \
107                         pkts_in_mask &= ~pkt_mask;                      \
108                         f_pkt_work(pkts[pos], entries[pos], arg);       \
109                 }                                                       \
110                                                                         \
111         return 0;                                                       \
112 }
113
114 #define PIPELINE_TABLE_AH_MISS(f_ah, f_pkt_work, f_pkt4_work)           \
115 static int                                                              \
116 f_ah(                                                                   \
117         __rte_unused struct rte_pipeline *p,                            \
118         struct rte_mbuf **pkts,                                         \
119         uint64_t pkts_in_mask,                                          \
120         struct rte_pipeline_table_entry *entry,                         \
121         void *arg)                                                      \
122 {                                                                       \
123         if ((pkts_in_mask & (pkts_in_mask + 1)) == 0) {                 \
124                 uint64_t n_pkts = __builtin_popcountll(pkts_in_mask);   \
125                 uint32_t i;                                             \
126                                                                         \
127                 for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4)           \
128                         f_pkt4_work(&pkts[i], entry, arg);              \
129                                                                         \
130                 for ( ; i < n_pkts; i++)                                \
131                         f_pkt_work(pkts[i], entry, arg);                \
132         } else                                                          \
133                 for ( ; pkts_in_mask; ) {                               \
134                         uint32_t pos = __builtin_ctzll(pkts_in_mask);   \
135                         uint64_t pkt_mask = 1LLU << pos;                \
136                                                                         \
137                         pkts_in_mask &= ~pkt_mask;                      \
138                         f_pkt_work(pkts[pos], entry, arg);              \
139                 }                                                       \
140                                                                         \
141         return 0;                                                       \
142 }
143
144 #define PIPELINE_TABLE_AH_HIT_DROP_TIME(f_ah, f_pkt_work, f_pkt4_work)  \
145 static int                                                              \
146 f_ah(                                                                   \
147         struct rte_pipeline *p,                                         \
148         struct rte_mbuf **pkts,                                         \
149         uint64_t pkts_mask,                                             \
150         struct rte_pipeline_table_entry **entries,                      \
151         void *arg)                                                      \
152 {                                                                       \
153         uint64_t pkts_in_mask = pkts_mask;                              \
154         uint64_t pkts_out_mask = pkts_mask;                             \
155         uint64_t time = rte_rdtsc();                                    \
156                                                                         \
157         if ((pkts_in_mask & (pkts_in_mask + 1)) == 0) {                 \
158                 uint64_t n_pkts = __builtin_popcountll(pkts_in_mask);   \
159                 uint32_t i;                                             \
160                                                                         \
161                 for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {         \
162                         uint64_t mask = f_pkt4_work(&pkts[i],           \
163                                 &entries[i], arg, time);                \
164                         pkts_out_mask ^= mask << i;                     \
165                 }                                                       \
166                                                                         \
167                 for ( ; i < n_pkts; i++) {                              \
168                         uint64_t mask = f_pkt_work(pkts[i],             \
169                                 entries[i], arg, time);                 \
170                         pkts_out_mask ^= mask << i;                     \
171                 }                                                       \
172         } else                                                          \
173                 for ( ; pkts_in_mask; ) {                               \
174                         uint32_t pos = __builtin_ctzll(pkts_in_mask);   \
175                         uint64_t pkt_mask = 1LLU << pos;                \
176                         uint64_t mask = f_pkt_work(pkts[pos],           \
177                                 entries[pos], arg, time);               \
178                                                                         \
179                         pkts_in_mask &= ~pkt_mask;                      \
180                         pkts_out_mask ^= mask << pos;                   \
181                 }                                                       \
182                                                                         \
183         rte_pipeline_ah_packet_drop(p, pkts_out_mask ^ pkts_mask);      \
184                                                                         \
185         return 0;                                                       \
186 }
187
188 #define PIPELINE_TABLE_AH_MISS_DROP_TIME(f_ah, f_pkt_work, f_pkt4_work) \
189 static int                                                              \
190 f_ah(                                                                   \
191         struct rte_pipeline *p,                                         \
192         struct rte_mbuf **pkts,                                         \
193         uint64_t pkts_mask,                                             \
194         struct rte_pipeline_table_entry *entry,                         \
195         void *arg)                                                      \
196 {                                                                       \
197         uint64_t pkts_in_mask = pkts_mask;                              \
198         uint64_t pkts_out_mask = pkts_mask;                             \
199         uint64_t time = rte_rdtsc();                                    \
200                                                                         \
201         if ((pkts_in_mask & (pkts_in_mask + 1)) == 0) {                 \
202                 uint64_t n_pkts = __builtin_popcountll(pkts_in_mask);   \
203                 uint32_t i;                                             \
204                                                                         \
205                 for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {         \
206                         uint64_t mask = f_pkt4_work(&pkts[i],           \
207                                 entry, arg, time);                      \
208                         pkts_out_mask ^= mask << i;                     \
209                 }                                                       \
210                                                                         \
211                 for ( ; i < n_pkts; i++) {                              \
212                         uint64_t mask = f_pkt_work(pkts[i], entry, arg, time);\
213                         pkts_out_mask ^= mask << i;                     \
214                 }                                                       \
215         } else                                                          \
216                 for ( ; pkts_in_mask; ) {                               \
217                         uint32_t pos = __builtin_ctzll(pkts_in_mask);   \
218                         uint64_t pkt_mask = 1LLU << pos;                \
219                         uint64_t mask = f_pkt_work(pkts[pos],           \
220                                 entry, arg, time);              \
221                                                                         \
222                         pkts_in_mask &= ~pkt_mask;                      \
223                         pkts_out_mask ^= mask << pos;                   \
224                 }                                                       \
225                                                                         \
226         rte_pipeline_ah_packet_drop(p, pkts_out_mask ^ pkts_mask);      \
227                                                                         \
228         return 0;                                                       \
229 }
230
231 #endif