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