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