Merge "docs: Update install and release docs for DPDK migration support"
[samplevnf.git] / VNFs / DPPD-PROX / task_base.h
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef _TASK_BASE_H_
18 #define _TASK_BASE_H_
19
20 #include <rte_common.h>
21 #ifndef __rte_cache_aligned
22 #include <rte_memory.h>
23 #endif
24
25 #include "defaults.h"
26 #include "prox_globals.h"
27 #include "stats_task.h"
28
29 // runtime_flags 8 bits only
30 #define TASK_MPLS_TAGGING              0x0001
31 #define TASK_ROUTING                   0x0002
32 #define TASK_CLASSIFY                  0x0004
33 #define TASK_CTRL_HANDLE_ARP           0x0008
34 #define TASK_MARK                      0x0020
35 #define TASK_FP_HANDLE_ARP             0x0040
36 #define TASK_TX_CRC                    0x0080
37
38 // flag_features 64 bits
39 #define TASK_FEATURE_ROUTING           0x0001
40 #define TASK_FEATURE_CLASSIFY          0x0002
41 #define TASK_FEATURE_MULTI_RX                  0x0004
42 #define TASK_FEATURE_NEVER_DISCARDS            0x0008
43 #define TASK_FEATURE_NO_RX                     0x0010
44 #define TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS      0x0020
45 #define TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS      0x0040
46 #define TASK_FEATURE_ZERO_RX                   0x0080
47 #define TASK_FEATURE_TXQ_FLAGS_REFCOUNT        0x0100
48 #define TASK_FEATURE_TSC_RX                    0x0200
49 #define TASK_FEATURE_THROUGHPUT_OPT            0x0400
50 #define TASK_FEATURE_GRE_ID                    0x1000
51 #define TASK_FEATURE_LUT_QINQ_RSS              0x2000
52 #define TASK_FEATURE_LUT_QINQ_HASH             0x4000
53 #define TASK_FEATURE_RX_ALL                    0x8000
54 #define TASK_MULTIPLE_MAC                      0x10000
55
56 #define FLAG_TX_FLUSH                  0x01
57 #define FLAG_NEVER_FLUSH               0x02
58 // Task specific flags
59 #define BASE_FLAG_LUT_QINQ_HASH         0x08
60 #define BASE_FLAG_LUT_QINQ_RSS          0x10
61
62 #define OUT_DISCARD 0xFF
63 #define OUT_HANDLED 0xFE
64
65 #define WS_MBUF_MASK (2 * MAX_PKT_BURST - 1)
66
67 /* struct ws_mbuf stores the working set of mbufs. It starts with a
68    prod/cons index to keep track of the number of elemenets. */
69 struct ws_mbuf {
70         struct {
71                 uint16_t        prod;
72                 uint16_t        cons;
73                 uint16_t        nb_rx;
74                 uint16_t        pad; /* reserved */
75         } idx[MAX_RINGS_PER_TASK];
76         struct rte_mbuf *mbuf[][MAX_RING_BURST * 3]  __rte_cache_aligned;
77 };
78
79 struct port_queue {
80         uint8_t port;
81         uint8_t queue;
82 } __attribute__((packed));
83
84 struct rx_params_hw {
85         union {
86                 uint8_t           nb_rxports;
87                 uint8_t           rxport_mask;
88         };
89         uint8_t           last_read_portid;
90         struct port_queue *rx_pq;
91 } __attribute__((packed));
92
93 struct rx_params_hw1 {
94         struct port_queue rx_pq;
95 } __attribute__((packed));
96
97 struct rx_params_sw {
98         union {
99                 uint8_t         nb_rxrings;
100                 uint8_t         rxrings_mask; /* Used if rte_is_power_of_2(nb_rxrings)*/
101         };
102         uint8_t         last_read_ring;
103         struct rte_ring **rx_rings;
104 } __attribute__((packed));
105
106 /* If there is only one input ring, the pointer to it can be stored
107    directly into the task_base instead of having to use a pointer to a
108    set of rings which would require two dereferences. */
109 struct rx_params_sw1 {
110         struct rte_ring *rx_ring;
111 } __attribute__((packed));
112
113 struct tx_params_hw {
114         uint16_t          nb_txports;
115         struct port_queue *tx_port_queue;
116 } __attribute__((packed));
117
118 struct tx_params_sw {
119         uint16_t         nb_txrings;
120         struct rte_ring **tx_rings;
121 } __attribute__((packed));
122
123 struct tx_params_hw_sw {        /* Only one port supported in this mode */
124         uint16_t         nb_txrings;
125         struct rte_ring **tx_rings;
126         struct port_queue tx_port_queue;
127 } __attribute__((packed));
128
129 struct task_rt_dump {
130         uint32_t n_print_rx;
131         uint32_t n_print_tx;
132         struct input *input;
133         uint32_t n_trace;
134         uint32_t cur_trace;
135         void     *pkt_mbuf_addr[MAX_RING_BURST]; /* To track reordering */
136         uint8_t  pkt_cpy[MAX_RING_BURST][128];
137         uint16_t pkt_cpy_len[MAX_RING_BURST];
138 };
139
140 struct task_base;
141
142 #define MAX_RX_PKT_ALL 16384
143
144 #define MAX_STACKED_RX_FUCTIONS 16
145
146 typedef uint16_t (*rx_pkt_func) (struct task_base *tbase, struct rte_mbuf ***mbufs);
147
148 struct task_base_aux {
149         /* Not used when PROX_STATS is not defined */
150         struct task_rt_stats stats;
151         struct task_rt_dump task_rt_dump;
152
153         /* Used if TASK_TSC_RX is enabled*/
154         struct {
155                 uint64_t before;
156                 uint64_t after;
157         } tsc_rx;
158
159         struct  rte_mbuf **all_mbufs;
160
161         int      rx_prev_count;
162         int      rx_prev_idx;
163         uint16_t (*rx_pkt_prev[MAX_STACKED_RX_FUCTIONS])(struct task_base *tbase, struct rte_mbuf ***mbufs);
164
165         uint32_t rx_bucket[MAX_RING_BURST + 1];
166         uint32_t tx_bucket[MAX_RING_BURST + 1];
167         int (*tx_pkt_orig)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts, uint8_t *out);
168         int (*tx_pkt_hw)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts, uint8_t *out);
169         uint16_t (*tx_pkt_try)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts);
170         void (*stop)(struct task_base *tbase);
171         void (*start)(struct task_base *tbase);
172         void (*stop_last)(struct task_base *tbase);
173         void (*start_first)(struct task_base *tbase);
174 };
175
176 /* The task_base is accessed for _all_ task types. In case
177    no debugging is needed, it has been optimized to fit
178    into a single cache line to minimize cache pollution */
179 struct task_base {
180         int (*handle_bulk)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts);
181         int (*tx_pkt)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts, uint8_t *out);
182         uint16_t (*rx_pkt)(struct task_base *tbase, struct rte_mbuf ***mbufs);
183
184         struct task_base_aux* aux;
185         /* The working set of mbufs contains mbufs that are currently
186            being handled. */
187         struct ws_mbuf *ws_mbuf;
188
189         uint16_t flags;
190
191         union {
192                 struct rx_params_hw rx_params_hw;
193                 struct rx_params_hw1 rx_params_hw1;
194                 struct rx_params_sw rx_params_sw;
195                 struct rx_params_sw1 rx_params_sw1;
196         };
197
198         union {
199                 struct tx_params_hw tx_params_hw;
200                 struct tx_params_sw tx_params_sw;
201                 struct tx_params_hw_sw tx_params_hw_sw;
202         };
203 } __attribute__((packed)) __rte_cache_aligned;
204
205 static void task_base_add_rx_pkt_function(struct task_base *tbase, rx_pkt_func to_add)
206 {
207         if (tbase->aux->rx_prev_count == MAX_STACKED_RX_FUCTIONS) {
208                 return;
209         }
210
211         for (int16_t i = tbase->aux->rx_prev_count; i >= 0; --i) {
212                 tbase->aux->rx_pkt_prev[i + 1] = tbase->aux->rx_pkt_prev[i];
213         }
214         tbase->aux->rx_pkt_prev[0] = tbase->rx_pkt;
215         tbase->rx_pkt = to_add;
216         tbase->aux->rx_prev_count++;
217 }
218
219 static void task_base_del_rx_pkt_function(struct task_base *tbase, rx_pkt_func to_del)
220 {
221         int cur = 0;
222         int found = 0;
223
224         if (tbase->aux->rx_prev_count == 1) {
225                 tbase->rx_pkt = tbase->aux->rx_pkt_prev[0];
226                 found = 1;
227         } else {
228                 for (int16_t i = 0; i < tbase->aux->rx_prev_count; ++i) {
229                         if (found || tbase->aux->rx_pkt_prev[i] != to_del)
230                                 tbase->aux->rx_pkt_prev[cur++] = tbase->aux->rx_pkt_prev[i];
231                         else
232                                 found = 1;
233                 }
234         }
235         if (found)
236                 tbase->aux->rx_prev_count--;
237 }
238
239 static rx_pkt_func task_base_get_original_rx_pkt_function(struct task_base *tbase)
240 {
241         if (tbase->aux->rx_prev_count == 0)
242                 return tbase->rx_pkt;
243         else
244                 return tbase->aux->rx_pkt_prev[tbase->aux->rx_prev_count - 1];
245 }
246
247 #endif /* _TASK_BASE_H_ */