Add support for igmp and multicast
[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 #include "packet_utils.h"
29
30 // runtime_flags 16 bits only
31 #define TASK_MPLS_TAGGING              0x0001
32 #define TASK_ROUTING                   0x0002
33 #define TASK_CLASSIFY                  0x0004
34 #define TASK_CTRL_HANDLE_ARP           0x0008
35 #define TASK_MARK                      0x0020
36 #define TASK_FP_HANDLE_ARP             0x0040
37 #define TASK_TX_CRC                    0x0080
38 #define TASK_L3                        0x0100
39
40 // flag_features 64 bits
41 #define TASK_FEATURE_ROUTING           0x0001
42 #define TASK_FEATURE_CLASSIFY          0x0002
43 #define TASK_FEATURE_MULTI_RX                  0x0004
44 #define TASK_FEATURE_NEVER_DISCARDS            0x0008
45 #define TASK_FEATURE_NO_RX                     0x0010
46 #define TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS      0x0020
47 #define TASK_FEATURE_TXQ_FLAGS_MULTSEGS        0x0040
48 #define TASK_FEATURE_ZERO_RX                   0x0080
49 #define TASK_FEATURE_TXQ_FLAGS_REFCOUNT        0x0100
50 #define TASK_FEATURE_TSC_RX                    0x0200
51 #define TASK_FEATURE_THROUGHPUT_OPT            0x0400
52 #define TASK_FEATURE_GRE_ID                    0x1000
53 #define TASK_FEATURE_LUT_QINQ_RSS              0x2000
54 #define TASK_FEATURE_LUT_QINQ_HASH             0x4000
55 #define TASK_FEATURE_RX_ALL                    0x8000
56 #define TASK_FEATURE_TXQ_FLAGS_MULTIPLE_MEMPOOL 0x20000
57
58 #define FLAG_TX_FLUSH                  0x01
59 #define FLAG_NEVER_FLUSH               0x02
60 // Task specific flags
61 #define BASE_FLAG_LUT_QINQ_HASH         0x08
62 #define BASE_FLAG_LUT_QINQ_RSS          0x10
63
64 #define OUT_DISCARD 0xFF
65 #define OUT_HANDLED 0xFE
66
67 #define WS_MBUF_MASK (2 * MAX_PKT_BURST - 1)
68
69 /* struct ws_mbuf stores the working set of mbufs. It starts with a
70    prod/cons index to keep track of the number of elemenets. */
71 struct ws_mbuf {
72         struct {
73                 uint16_t        prod;
74                 uint16_t        cons;
75                 uint16_t        nb_rx;
76                 uint16_t        pad; /* reserved */
77         } idx[MAX_RINGS_PER_TASK];
78         struct rte_mbuf *mbuf[][MAX_RING_BURST * 3]  __rte_cache_aligned;
79 };
80
81 struct port_queue {
82         uint8_t port;
83         uint8_t queue;
84 } __attribute__((packed));
85
86 struct rx_params_hw {
87         union {
88                 uint8_t           nb_rxports;
89                 uint8_t           rxport_mask;
90         };
91         uint8_t           last_read_portid;
92         struct port_queue *rx_pq;
93 } __attribute__((packed));
94
95 struct rx_params_hw1 {
96         struct port_queue rx_pq;
97 } __attribute__((packed));
98
99 struct rx_params_sw {
100         union {
101                 uint8_t         nb_rxrings;
102                 uint8_t         rxrings_mask; /* Used if rte_is_power_of_2(nb_rxrings)*/
103         };
104         uint8_t         last_read_ring;
105         struct rte_ring **rx_rings;
106 } __attribute__((packed));
107
108 /* If there is only one input ring, the pointer to it can be stored
109    directly into the task_base instead of having to use a pointer to a
110    set of rings which would require two dereferences. */
111 struct rx_params_sw1 {
112         struct rte_ring *rx_ring;
113 } __attribute__((packed));
114
115 struct tx_params_hw {
116         uint16_t          nb_txports;
117         struct port_queue *tx_port_queue;
118 } __attribute__((packed));
119
120 struct tx_params_sw {
121         uint16_t         nb_txrings;
122         struct rte_ring **tx_rings;
123 } __attribute__((packed));
124
125 struct tx_params_hw_sw {        /* Only one port supported in this mode */
126         uint16_t         nb_txrings;
127         struct rte_ring **tx_rings;
128         struct port_queue tx_port_queue;
129 } __attribute__((packed));
130
131 struct task_rt_dump {
132         uint32_t n_print_rx;
133         uint32_t n_print_tx;
134         struct input *input;
135         uint32_t n_trace;
136         uint32_t cur_trace;
137         void     *pkt_mbuf_addr[MAX_RING_BURST]; /* To track reordering */
138         uint8_t  pkt_cpy[MAX_RING_BURST][DUMP_PKT_LEN];
139         uint16_t pkt_cpy_len[MAX_RING_BURST];
140 };
141
142 struct task_base;
143
144 #define MAX_RX_PKT_ALL 16384
145
146 #define RX_BUCKET_SIZE (2 * MAX_RING_BURST + 1) /* Limit RX bucket size */
147 #define TX_BUCKET_SIZE  (MAX_RING_BURST +1)
148
149 #define MAX_STACKED_RX_FUCTIONS 16
150
151 typedef uint16_t (*rx_pkt_func) (struct task_base *tbase, struct rte_mbuf ***mbufs);
152
153 struct task_base_aux {
154         /* Not used when PROX_STATS is not defined */
155         struct task_rt_stats stats;
156
157         /* Used if TASK_TSC_RX is enabled*/
158         struct {
159                 uint64_t before;
160                 uint64_t after;
161         } tsc_rx;
162
163         struct  rte_mbuf **all_mbufs;
164
165         uint16_t      rx_prev_count;
166         uint16_t      rx_prev_idx;
167         uint16_t (*rx_pkt_prev[MAX_STACKED_RX_FUCTIONS])(struct task_base *tbase, struct rte_mbuf ***mbufs);
168
169         uint32_t rx_bucket[RX_BUCKET_SIZE];
170         uint32_t tx_bucket[TX_BUCKET_SIZE];
171         int (*tx_pkt_l2)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts, uint8_t *out);
172         int (*tx_pkt_orig)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts, uint8_t *out);
173         int (*tx_pkt_hw)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts, uint8_t *out);
174         uint16_t (*tx_pkt_try)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts);
175         void (*stop)(struct task_base *tbase);
176         int (*tx_ctrlplane_pkt)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts, uint8_t *out);
177         void (*start)(struct task_base *tbase);
178         void (*stop_last)(struct task_base *tbase);
179         void (*start_first)(struct task_base *tbase);
180         struct task_rt_dump task_rt_dump;
181 };
182
183 /* The task_base is accessed for _all_ task types. In case
184    no debugging or l3 is needed, it has been optimized to fit
185    into a single cache line to minimize cache pollution */
186 struct task_base {
187         int (*handle_bulk)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts);
188         int (*tx_pkt)(struct task_base *tbase, struct rte_mbuf **mbufs, const uint16_t n_pkts, uint8_t *out);
189         uint16_t (*rx_pkt)(struct task_base *tbase, struct rte_mbuf ***mbufs);
190
191         struct task_base_aux* aux;
192         /* The working set of mbufs contains mbufs that are currently
193            being handled. */
194         struct ws_mbuf *ws_mbuf;
195
196         uint16_t flags;
197
198         union {
199                 struct rx_params_hw rx_params_hw;
200                 struct rx_params_hw1 rx_params_hw1;
201                 struct rx_params_sw rx_params_sw;
202                 struct rx_params_sw1 rx_params_sw1;
203         };
204
205         union {
206                 struct tx_params_hw tx_params_hw;
207                 struct tx_params_sw tx_params_sw;
208                 struct tx_params_hw_sw tx_params_hw_sw;
209         };
210         struct l3_base l3;
211         uint32_t local_ipv4;
212 } __attribute__((packed)) __rte_cache_aligned;
213
214 static void task_base_add_rx_pkt_function(struct task_base *tbase, rx_pkt_func to_add)
215 {
216         if (tbase->aux->rx_prev_count == MAX_STACKED_RX_FUCTIONS) {
217                 return;
218         }
219
220         for (int16_t i = tbase->aux->rx_prev_count; i > 0; --i) {
221                 tbase->aux->rx_pkt_prev[i] = tbase->aux->rx_pkt_prev[i - 1];
222         }
223         tbase->aux->rx_pkt_prev[0] = tbase->rx_pkt;
224         tbase->rx_pkt = to_add;
225         tbase->aux->rx_prev_count++;
226 }
227
228 static void task_base_del_rx_pkt_function(struct task_base *tbase, rx_pkt_func to_del)
229 {
230         int cur = 0;
231         int found = 0;
232
233         if (unlikely(tbase->aux->rx_prev_count == 0)) {
234                 return;
235         } else if (tbase->rx_pkt == to_del) {
236                 tbase->rx_pkt = tbase->aux->rx_pkt_prev[0];
237                 for (int16_t i = 0; i < tbase->aux->rx_prev_count - 1; ++i) {
238                         tbase->aux->rx_pkt_prev[i] = tbase->aux->rx_pkt_prev[i + 1];
239                 }
240                 found = 1;
241         } else {
242                 for (int16_t i = 0; i < tbase->aux->rx_prev_count; ++i) {
243                         if (found || tbase->aux->rx_pkt_prev[i] != to_del)
244                                 tbase->aux->rx_pkt_prev[cur++] = tbase->aux->rx_pkt_prev[i];
245                         else
246                                 found = 1;
247                 }
248         }
249         if (found)
250                 tbase->aux->rx_prev_count--;
251 }
252
253 static rx_pkt_func task_base_get_original_rx_pkt_function(struct task_base *tbase)
254 {
255         if (tbase->aux->rx_prev_count == 0)
256                 return tbase->rx_pkt;
257         else
258                 return tbase->aux->rx_pkt_prev[tbase->aux->rx_prev_count - 1];
259 }
260
261 #endif /* _TASK_BASE_H_ */