lib: fix cache alignment of structures
[dpdk.git] / lib / librte_port / rte_port_frag.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_memory.h>
38
39 #include "rte_port_frag.h"
40
41 /* Default byte size for the IPv4 Maximum Transfer Unit (MTU).
42  * This value includes the size of IPv4 header. */
43 #define IPV4_MTU_DEFAULT ETHER_MTU
44
45 /* Max number of fragments per packet allowed */
46 #define IPV4_MAX_FRAGS_PER_PACKET 0x80
47
48 struct rte_port_ring_reader_ipv4_frag {
49         /* Input parameters */
50         struct rte_ring *ring;
51         uint32_t mtu;
52         uint32_t metadata_size;
53         struct rte_mempool *pool_direct;
54         struct rte_mempool *pool_indirect;
55
56         /* Internal buffers */
57         struct rte_mbuf *pkts[RTE_PORT_IN_BURST_SIZE_MAX];
58         struct rte_mbuf *frags[IPV4_MAX_FRAGS_PER_PACKET];
59         uint32_t n_pkts;
60         uint32_t pos_pkts;
61         uint32_t n_frags;
62         uint32_t pos_frags;
63 } __rte_cache_aligned;
64
65 static void *
66 rte_port_ring_reader_ipv4_frag_create(void *params, int socket_id)
67 {
68         struct rte_port_ring_reader_ipv4_frag_params *conf =
69                         (struct rte_port_ring_reader_ipv4_frag_params *) params;
70         struct rte_port_ring_reader_ipv4_frag *port;
71
72         /* Check input parameters */
73         if (conf == NULL) {
74                 RTE_LOG(ERR, PORT, "%s: Parameter conf is NULL\n", __func__);
75                 return NULL;
76         }
77         if (conf->ring == NULL) {
78                 RTE_LOG(ERR, PORT, "%s: Parameter ring is NULL\n", __func__);
79                 return NULL;
80         }
81         if (conf->mtu == 0) {
82                 RTE_LOG(ERR, PORT, "%s: Parameter mtu is invalid\n", __func__);
83                 return NULL;
84         }
85         if (conf->pool_direct == NULL) {
86                 RTE_LOG(ERR, PORT, "%s: Parameter pool_direct is NULL\n",
87                         __func__);
88                 return NULL;
89         }
90         if (conf->pool_indirect == NULL) {
91                 RTE_LOG(ERR, PORT, "%s: Parameter pool_indirect is NULL\n",
92                         __func__);
93                 return NULL;
94         }
95
96         /* Memory allocation */
97         port = rte_zmalloc_socket("PORT", sizeof(*port), RTE_CACHE_LINE_SIZE,
98                 socket_id);
99         if (port == NULL) {
100                 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
101                 return NULL;
102         }
103
104         /* Initialization */
105         port->ring = conf->ring;
106         port->mtu = conf->mtu;
107         port->metadata_size = conf->metadata_size;
108         port->pool_direct = conf->pool_direct;
109         port->pool_indirect = conf->pool_indirect;
110
111         port->n_pkts = 0;
112         port->pos_pkts = 0;
113         port->n_frags = 0;
114         port->pos_frags = 0;
115
116         return port;
117 }
118
119 static int
120 rte_port_ring_reader_ipv4_frag_rx(void *port,
121                 struct rte_mbuf **pkts,
122                 uint32_t n_pkts)
123 {
124         struct rte_port_ring_reader_ipv4_frag *p =
125                         (struct rte_port_ring_reader_ipv4_frag *) port;
126         uint32_t n_pkts_out;
127
128         n_pkts_out = 0;
129
130         /* Get packets from the "frag" buffer */
131         if (p->n_frags >= n_pkts) {
132                 memcpy(pkts, &p->frags[p->pos_frags], n_pkts * sizeof(void *));
133                 p->pos_frags += n_pkts;
134                 p->n_frags -= n_pkts;
135
136                 return n_pkts;
137         }
138
139         memcpy(pkts, &p->frags[p->pos_frags], p->n_frags * sizeof(void *));
140         n_pkts_out = p->n_frags;
141         p->n_frags = 0;
142
143         /* Look to "pkts" buffer to get more packets */
144         for ( ; ; ) {
145                 struct rte_mbuf *pkt;
146                 uint32_t n_pkts_to_provide, i;
147                 int status;
148
149                 /* If "pkts" buffer is empty, read packet burst from ring */
150                 if (p->n_pkts == 0) {
151                         p->n_pkts = rte_ring_sc_dequeue_burst(p->ring,
152                                 (void **) p->pkts, RTE_PORT_IN_BURST_SIZE_MAX);
153                         if (p->n_pkts == 0)
154                                 return n_pkts_out;
155                         p->pos_pkts = 0;
156                 }
157
158                 /* Read next packet from "pkts" buffer */
159                 pkt = p->pkts[p->pos_pkts++];
160                 p->n_pkts--;
161
162                 /* If not jumbo, pass current packet to output */
163                 if (pkt->pkt_len <= IPV4_MTU_DEFAULT) {
164                         pkts[n_pkts_out++] = pkt;
165
166                         n_pkts_to_provide = n_pkts - n_pkts_out;
167                         if (n_pkts_to_provide == 0)
168                                 return n_pkts;
169
170                         continue;
171                 }
172
173                 /* Fragment current packet into the "frags" buffer */
174                 status = rte_ipv4_fragment_packet(
175                         pkt,
176                         p->frags,
177                         IPV4_MAX_FRAGS_PER_PACKET,
178                         p->mtu,
179                         p->pool_direct,
180                         p->pool_indirect
181                 );
182
183                 if (status < 0) {
184                         rte_pktmbuf_free(pkt);
185                         continue;
186                 }
187
188                 p->n_frags = (uint32_t) status;
189                 p->pos_frags = 0;
190
191                 /* Copy meta-data from input jumbo packet to its fragments */
192                 for (i = 0; i < p->n_frags; i++) {
193                         uint8_t *src = RTE_MBUF_METADATA_UINT8_PTR(pkt, 0);
194                         uint8_t *dst =
195                                 RTE_MBUF_METADATA_UINT8_PTR(p->frags[i], 0);
196
197                         memcpy(dst, src, p->metadata_size);
198                 }
199
200                 /* Free input jumbo packet */
201                 rte_pktmbuf_free(pkt);
202
203                 /* Get packets from "frag" buffer */
204                 n_pkts_to_provide = n_pkts - n_pkts_out;
205                 if (p->n_frags >= n_pkts_to_provide) {
206                         memcpy(&pkts[n_pkts_out], p->frags,
207                                 n_pkts_to_provide * sizeof(void *));
208                         p->n_frags -= n_pkts_to_provide;
209                         p->pos_frags += n_pkts_to_provide;
210
211                         return n_pkts;
212                 }
213
214                 memcpy(&pkts[n_pkts_out], p->frags,
215                         p->n_frags * sizeof(void *));
216                 n_pkts_out += p->n_frags;
217                 p->n_frags = 0;
218         }
219 }
220
221 static int
222 rte_port_ring_reader_ipv4_frag_free(void *port)
223 {
224         if (port == NULL) {
225                 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
226                 return -1;
227         }
228
229         rte_free(port);
230
231         return 0;
232 }
233
234 /*
235  * Summary of port operations
236  */
237 struct rte_port_in_ops rte_port_ring_reader_ipv4_frag_ops = {
238         .f_create = rte_port_ring_reader_ipv4_frag_create,
239         .f_free = rte_port_ring_reader_ipv4_frag_free,
240         .f_rx = rte_port_ring_reader_ipv4_frag_rx,
241 };