4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <rte_ether.h>
36 #include <rte_ip_frag.h>
37 #include <rte_memory.h>
39 #include "rte_port_frag.h"
41 /* Max number of fragments per packet allowed */
42 #define RTE_PORT_FRAG_MAX_FRAGS_PER_PACKET 0x80
45 (*frag_op)(struct rte_mbuf *pkt_in,
46 struct rte_mbuf **pkts_out,
49 struct rte_mempool *pool_direct,
50 struct rte_mempool *pool_indirect);
52 struct rte_port_ring_reader_frag {
53 /* Input parameters */
54 struct rte_ring *ring;
56 uint32_t metadata_size;
57 struct rte_mempool *pool_direct;
58 struct rte_mempool *pool_indirect;
60 /* Internal buffers */
61 struct rte_mbuf *pkts[RTE_PORT_IN_BURST_SIZE_MAX];
62 struct rte_mbuf *frags[RTE_PORT_FRAG_MAX_FRAGS_PER_PACKET];
69 } __rte_cache_aligned;
72 rte_port_ring_reader_frag_create(void *params, int socket_id, int is_ipv4)
74 struct rte_port_ring_reader_frag_params *conf =
75 (struct rte_port_ring_reader_frag_params *) params;
76 struct rte_port_ring_reader_frag *port;
78 /* Check input parameters */
80 RTE_LOG(ERR, PORT, "%s: Parameter conf is NULL\n", __func__);
83 if (conf->ring == NULL) {
84 RTE_LOG(ERR, PORT, "%s: Parameter ring is NULL\n", __func__);
88 RTE_LOG(ERR, PORT, "%s: Parameter mtu is invalid\n", __func__);
91 if (conf->pool_direct == NULL) {
92 RTE_LOG(ERR, PORT, "%s: Parameter pool_direct is NULL\n",
96 if (conf->pool_indirect == NULL) {
97 RTE_LOG(ERR, PORT, "%s: Parameter pool_indirect is NULL\n",
102 /* Memory allocation */
103 port = rte_zmalloc_socket("PORT", sizeof(*port), RTE_CACHE_LINE_SIZE,
106 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
111 port->ring = conf->ring;
112 port->mtu = conf->mtu;
113 port->metadata_size = conf->metadata_size;
114 port->pool_direct = conf->pool_direct;
115 port->pool_indirect = conf->pool_indirect;
122 port->f_frag = (is_ipv4) ?
123 rte_ipv4_fragment_packet : rte_ipv6_fragment_packet;
129 rte_port_ring_reader_ipv4_frag_create(void *params, int socket_id)
131 return rte_port_ring_reader_frag_create(params, socket_id, 1);
135 rte_port_ring_reader_ipv6_frag_create(void *params, int socket_id)
137 return rte_port_ring_reader_frag_create(params, socket_id, 0);
141 rte_port_ring_reader_frag_rx(void *port,
142 struct rte_mbuf **pkts,
145 struct rte_port_ring_reader_frag *p =
146 (struct rte_port_ring_reader_frag *) port;
151 /* Get packets from the "frag" buffer */
152 if (p->n_frags >= n_pkts) {
153 memcpy(pkts, &p->frags[p->pos_frags], n_pkts * sizeof(void *));
154 p->pos_frags += n_pkts;
155 p->n_frags -= n_pkts;
160 memcpy(pkts, &p->frags[p->pos_frags], p->n_frags * sizeof(void *));
161 n_pkts_out = p->n_frags;
164 /* Look to "pkts" buffer to get more packets */
166 struct rte_mbuf *pkt;
167 uint32_t n_pkts_to_provide, i;
170 /* If "pkts" buffer is empty, read packet burst from ring */
171 if (p->n_pkts == 0) {
172 p->n_pkts = rte_ring_sc_dequeue_burst(p->ring,
173 (void **) p->pkts, RTE_PORT_IN_BURST_SIZE_MAX);
179 /* Read next packet from "pkts" buffer */
180 pkt = p->pkts[p->pos_pkts++];
183 /* If not jumbo, pass current packet to output */
184 if (pkt->pkt_len <= p->mtu) {
185 pkts[n_pkts_out++] = pkt;
187 n_pkts_to_provide = n_pkts - n_pkts_out;
188 if (n_pkts_to_provide == 0)
194 /* Fragment current packet into the "frags" buffer */
198 RTE_PORT_FRAG_MAX_FRAGS_PER_PACKET,
205 rte_pktmbuf_free(pkt);
209 p->n_frags = (uint32_t) status;
212 /* Copy meta-data from input jumbo packet to its fragments */
213 for (i = 0; i < p->n_frags; i++) {
214 uint8_t *src = RTE_MBUF_METADATA_UINT8_PTR(pkt, 0);
216 RTE_MBUF_METADATA_UINT8_PTR(p->frags[i], 0);
218 memcpy(dst, src, p->metadata_size);
221 /* Free input jumbo packet */
222 rte_pktmbuf_free(pkt);
224 /* Get packets from "frag" buffer */
225 n_pkts_to_provide = n_pkts - n_pkts_out;
226 if (p->n_frags >= n_pkts_to_provide) {
227 memcpy(&pkts[n_pkts_out], p->frags,
228 n_pkts_to_provide * sizeof(void *));
229 p->n_frags -= n_pkts_to_provide;
230 p->pos_frags += n_pkts_to_provide;
235 memcpy(&pkts[n_pkts_out], p->frags,
236 p->n_frags * sizeof(void *));
237 n_pkts_out += p->n_frags;
243 rte_port_ring_reader_frag_free(void *port)
246 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
256 * Summary of port operations
258 struct rte_port_in_ops rte_port_ring_reader_ipv4_frag_ops = {
259 .f_create = rte_port_ring_reader_ipv4_frag_create,
260 .f_free = rte_port_ring_reader_frag_free,
261 .f_rx = rte_port_ring_reader_frag_rx,
264 struct rte_port_in_ops rte_port_ring_reader_ipv6_frag_ops = {
265 .f_create = rte_port_ring_reader_ipv6_frag_create,
266 .f_free = rte_port_ring_reader_frag_free,
267 .f_rx = rte_port_ring_reader_frag_rx,