port: add reassembly stats
[dpdk.git] / lib / librte_port / rte_port_ras.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <string.h>
34
35 #include <rte_ether.h>
36 #include <rte_ip_frag.h>
37 #include <rte_cycles.h>
38 #include <rte_log.h>
39
40 #include "rte_port_ras.h"
41
42 #ifndef RTE_PORT_RAS_N_BUCKETS
43 #define RTE_PORT_RAS_N_BUCKETS                                 4094
44 #endif
45
46 #ifndef RTE_PORT_RAS_N_ENTRIES_PER_BUCKET
47 #define RTE_PORT_RAS_N_ENTRIES_PER_BUCKET                      8
48 #endif
49
50 #ifndef RTE_PORT_RAS_N_ENTRIES
51 #define RTE_PORT_RAS_N_ENTRIES (RTE_PORT_RAS_N_BUCKETS * RTE_PORT_RAS_N_ENTRIES_PER_BUCKET)
52 #endif
53
54 #ifdef RTE_PORT_STATS_COLLECT
55
56 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val) \
57         port->stats.n_pkts_in += val
58 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val) \
59         port->stats.n_pkts_drop += val
60
61 #else
62
63 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val)
64 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val)
65
66 #endif
67
68 struct rte_port_ring_writer_ras;
69
70 typedef void (*ras_op)(
71                 struct rte_port_ring_writer_ras *p,
72                 struct rte_mbuf *pkt);
73
74 static void
75 process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
76 static void
77 process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
78
79 struct rte_port_ring_writer_ras {
80         struct rte_port_out_stats stats;
81
82         struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
83         struct rte_ring *ring;
84         uint32_t tx_burst_sz;
85         uint32_t tx_buf_count;
86         struct rte_ip_frag_tbl *frag_tbl;
87         struct rte_ip_frag_death_row death_row;
88
89         ras_op f_ras;
90 };
91
92 static void *
93 rte_port_ring_writer_ras_create(void *params, int socket_id, int is_ipv4)
94 {
95         struct rte_port_ring_writer_ras_params *conf =
96                         (struct rte_port_ring_writer_ras_params *) params;
97         struct rte_port_ring_writer_ras *port;
98         uint64_t frag_cycles;
99
100         /* Check input parameters */
101         if (conf == NULL) {
102                 RTE_LOG(ERR, PORT, "%s: Parameter conf is NULL\n", __func__);
103                 return NULL;
104         }
105         if (conf->ring == NULL) {
106                 RTE_LOG(ERR, PORT, "%s: Parameter ring is NULL\n", __func__);
107                 return NULL;
108         }
109         if ((conf->tx_burst_sz == 0) ||
110             (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX)) {
111                 RTE_LOG(ERR, PORT, "%s: Parameter tx_burst_sz is invalid\n",
112                         __func__);
113                 return NULL;
114         }
115
116         /* Memory allocation */
117         port = rte_zmalloc_socket("PORT", sizeof(*port),
118                         RTE_CACHE_LINE_SIZE, socket_id);
119         if (port == NULL) {
120                 RTE_LOG(ERR, PORT, "%s: Failed to allocate socket\n", __func__);
121                 return NULL;
122         }
123
124         /* Create fragmentation table */
125         frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * MS_PER_S;
126         frag_cycles *= 100;
127
128         port->frag_tbl = rte_ip_frag_table_create(
129                 RTE_PORT_RAS_N_BUCKETS,
130                 RTE_PORT_RAS_N_ENTRIES_PER_BUCKET,
131                 RTE_PORT_RAS_N_ENTRIES,
132                 frag_cycles,
133                 socket_id);
134
135         if (port->frag_tbl == NULL) {
136                 RTE_LOG(ERR, PORT, "%s: rte_ip_frag_table_create failed\n",
137                         __func__);
138                 rte_free(port);
139                 return NULL;
140         }
141
142         /* Initialization */
143         port->ring = conf->ring;
144         port->tx_burst_sz = conf->tx_burst_sz;
145         port->tx_buf_count = 0;
146
147         port->f_ras = (is_ipv4 == 0) ? process_ipv4 : process_ipv6;
148
149         return port;
150 }
151
152 static void *
153 rte_port_ring_writer_ipv4_ras_create(void *params, int socket_id)
154 {
155         return rte_port_ring_writer_ras_create(params, socket_id, 1);
156 }
157
158 static void *
159 rte_port_ring_writer_ipv6_ras_create(void *params, int socket_id)
160 {
161         return rte_port_ring_writer_ras_create(params, socket_id, 0);
162 }
163
164 static inline void
165 send_burst(struct rte_port_ring_writer_ras *p)
166 {
167         uint32_t nb_tx;
168
169         nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
170                         p->tx_buf_count);
171
172         RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
173         for ( ; nb_tx < p->tx_buf_count; nb_tx++)
174                 rte_pktmbuf_free(p->tx_buf[nb_tx]);
175
176         p->tx_buf_count = 0;
177 }
178
179 static void
180 process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
181 {
182         /* Assume there is no ethernet header */
183         struct ipv4_hdr *pkt_hdr = (struct ipv4_hdr *)
184                         (rte_pktmbuf_mtod(pkt, unsigned char *));
185
186         /* Get "Do not fragment" flag and fragment offset */
187         uint16_t frag_field = rte_be_to_cpu_16(pkt_hdr->fragment_offset);
188         uint16_t frag_offset = (uint16_t)(frag_field & IPV4_HDR_OFFSET_MASK);
189         uint16_t frag_flag = (uint16_t)(frag_field & IPV4_HDR_MF_FLAG);
190
191         /* If it is a fragmented packet, then try to reassemble */
192         if ((frag_flag == 0) && (frag_offset == 0))
193                 p->tx_buf[p->tx_buf_count++] = pkt;
194         else {
195                 struct rte_mbuf *mo;
196                 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
197                 struct rte_ip_frag_death_row *dr = &p->death_row;
198
199                 /* Process this fragment */
200                 mo = rte_ipv4_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(),
201                                 pkt_hdr);
202                 if (mo != NULL)
203                         p->tx_buf[p->tx_buf_count++] = mo;
204
205                 rte_ip_frag_free_death_row(&p->death_row, 3);
206         }
207 }
208
209 static void
210 process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
211 {
212         /* Assume there is no ethernet header */
213         struct ipv6_hdr *pkt_hdr = (struct ipv6_hdr *)
214                         (rte_pktmbuf_mtod(pkt, unsigned char *));
215
216         struct ipv6_extension_fragment *frag_hdr;
217         frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(pkt_hdr);
218         uint16_t frag_offset = frag_hdr->frag_offset;
219         uint16_t frag_flag = frag_hdr->more_frags;
220
221         /* If it is a fragmented packet, then try to reassemble */
222         if ((frag_flag == 0) && (frag_offset == 0))
223                 p->tx_buf[p->tx_buf_count++] = pkt;
224         else {
225                 struct rte_mbuf *mo;
226                 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
227                 struct rte_ip_frag_death_row *dr = &p->death_row;
228
229                 /* Process this fragment */
230                 mo = rte_ipv6_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(), pkt_hdr,
231                                 frag_hdr);
232                 if (mo != NULL)
233                         p->tx_buf[p->tx_buf_count++] = mo;
234
235                 rte_ip_frag_free_death_row(&p->death_row, 3);
236         }
237 }
238
239 static int
240 rte_port_ring_writer_ras_tx(void *port, struct rte_mbuf *pkt)
241 {
242         struct rte_port_ring_writer_ras *p =
243                         (struct rte_port_ring_writer_ras *) port;
244
245         RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
246         p->f_ras(p, pkt);
247         if (p->tx_buf_count >= p->tx_burst_sz)
248                 send_burst(p);
249
250         return 0;
251 }
252
253 static int
254 rte_port_ring_writer_ras_tx_bulk(void *port,
255                 struct rte_mbuf **pkts,
256                 uint64_t pkts_mask)
257 {
258         struct rte_port_ring_writer_ras *p =
259                         (struct rte_port_ring_writer_ras *) port;
260
261         if ((pkts_mask & (pkts_mask + 1)) == 0) {
262                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
263                 uint32_t i;
264
265                 for (i = 0; i < n_pkts; i++) {
266                         struct rte_mbuf *pkt = pkts[i];
267
268                         RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
269                         p->f_ras(p, pkt);
270                         if (p->tx_buf_count >= p->tx_burst_sz)
271                                 send_burst(p);
272                 }
273         } else {
274                 for ( ; pkts_mask; ) {
275                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
276                         uint64_t pkt_mask = 1LLU << pkt_index;
277                         struct rte_mbuf *pkt = pkts[pkt_index];
278
279                         RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
280                         p->f_ras(p, pkt);
281                         if (p->tx_buf_count >= p->tx_burst_sz)
282                                 send_burst(p);
283
284                         pkts_mask &= ~pkt_mask;
285                 }
286         }
287
288         return 0;
289 }
290
291 static int
292 rte_port_ring_writer_ras_flush(void *port)
293 {
294         struct rte_port_ring_writer_ras *p =
295                         (struct rte_port_ring_writer_ras *) port;
296
297         if (p->tx_buf_count > 0)
298                 send_burst(p);
299
300         return 0;
301 }
302
303 static int
304 rte_port_ring_writer_ras_free(void *port)
305 {
306         struct rte_port_ring_writer_ras *p =
307                         (struct rte_port_ring_writer_ras *) port;
308
309         if (port == NULL) {
310                 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
311                 return -1;
312         }
313
314         rte_port_ring_writer_ras_flush(port);
315         rte_ip_frag_table_destroy(p->frag_tbl);
316         rte_free(port);
317
318         return 0;
319 }
320
321 static int
322 rte_port_ras_writer_stats_read(void *port,
323                 struct rte_port_out_stats *stats, int clear)
324 {
325         struct rte_port_ring_writer_ras *p =
326                 (struct rte_port_ring_writer_ras *) port;
327
328         if (stats != NULL)
329                 memcpy(stats, &p->stats, sizeof(p->stats));
330
331         if (clear)
332                 memset(&p->stats, 0, sizeof(p->stats));
333
334         return 0;
335 }
336
337 /*
338  * Summary of port operations
339  */
340 struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops = {
341         .f_create = rte_port_ring_writer_ipv4_ras_create,
342         .f_free = rte_port_ring_writer_ras_free,
343         .f_tx = rte_port_ring_writer_ras_tx,
344         .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
345         .f_flush = rte_port_ring_writer_ras_flush,
346         .f_stats = rte_port_ras_writer_stats_read,
347 };
348
349 struct rte_port_out_ops rte_port_ring_writer_ipv6_ras_ops = {
350         .f_create = rte_port_ring_writer_ipv6_ras_create,
351         .f_free = rte_port_ring_writer_ras_free,
352         .f_tx = rte_port_ring_writer_ras_tx,
353         .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
354         .f_flush = rte_port_ring_writer_ras_flush,
355         .f_stats = rte_port_ras_writer_stats_read,
356 };