1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <rte_ip_frag.h>
8 #include <rte_memory.h>
10 #include "rte_port_frag.h"
12 /* Max number of fragments per packet allowed */
13 #define RTE_PORT_FRAG_MAX_FRAGS_PER_PACKET 0x80
15 #ifdef RTE_PORT_STATS_COLLECT
17 #define RTE_PORT_RING_READER_FRAG_STATS_PKTS_IN_ADD(port, val) \
18 port->stats.n_pkts_in += val
19 #define RTE_PORT_RING_READER_FRAG_STATS_PKTS_DROP_ADD(port, val) \
20 port->stats.n_pkts_drop += val
24 #define RTE_PORT_RING_READER_FRAG_STATS_PKTS_IN_ADD(port, val)
25 #define RTE_PORT_RING_READER_FRAG_STATS_PKTS_DROP_ADD(port, val)
30 (*frag_op)(struct rte_mbuf *pkt_in,
31 struct rte_mbuf **pkts_out,
34 struct rte_mempool *pool_direct,
35 struct rte_mempool *pool_indirect);
37 struct rte_port_ring_reader_frag {
38 struct rte_port_in_stats stats;
40 /* Input parameters */
41 struct rte_ring *ring;
43 uint32_t metadata_size;
44 struct rte_mempool *pool_direct;
45 struct rte_mempool *pool_indirect;
47 /* Internal buffers */
48 struct rte_mbuf *pkts[RTE_PORT_IN_BURST_SIZE_MAX];
49 struct rte_mbuf *frags[RTE_PORT_FRAG_MAX_FRAGS_PER_PACKET];
56 } __rte_cache_aligned;
59 rte_port_ring_reader_frag_create(void *params, int socket_id, int is_ipv4)
61 struct rte_port_ring_reader_frag_params *conf =
63 struct rte_port_ring_reader_frag *port;
65 /* Check input parameters */
67 RTE_LOG(ERR, PORT, "%s: Parameter conf is NULL\n", __func__);
70 if (conf->ring == NULL) {
71 RTE_LOG(ERR, PORT, "%s: Parameter ring is NULL\n", __func__);
75 RTE_LOG(ERR, PORT, "%s: Parameter mtu is invalid\n", __func__);
78 if (conf->pool_direct == NULL) {
79 RTE_LOG(ERR, PORT, "%s: Parameter pool_direct is NULL\n",
83 if (conf->pool_indirect == NULL) {
84 RTE_LOG(ERR, PORT, "%s: Parameter pool_indirect is NULL\n",
89 /* Memory allocation */
90 port = rte_zmalloc_socket("PORT", sizeof(*port), RTE_CACHE_LINE_SIZE,
93 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
98 port->ring = conf->ring;
99 port->mtu = conf->mtu;
100 port->metadata_size = conf->metadata_size;
101 port->pool_direct = conf->pool_direct;
102 port->pool_indirect = conf->pool_indirect;
109 port->f_frag = (is_ipv4) ?
110 rte_ipv4_fragment_packet : rte_ipv6_fragment_packet;
116 rte_port_ring_reader_ipv4_frag_create(void *params, int socket_id)
118 return rte_port_ring_reader_frag_create(params, socket_id, 1);
122 rte_port_ring_reader_ipv6_frag_create(void *params, int socket_id)
124 return rte_port_ring_reader_frag_create(params, socket_id, 0);
128 rte_port_ring_reader_frag_rx(void *port,
129 struct rte_mbuf **pkts,
132 struct rte_port_ring_reader_frag *p =
138 /* Get packets from the "frag" buffer */
139 if (p->n_frags >= n_pkts) {
140 memcpy(pkts, &p->frags[p->pos_frags], n_pkts * sizeof(void *));
141 p->pos_frags += n_pkts;
142 p->n_frags -= n_pkts;
147 memcpy(pkts, &p->frags[p->pos_frags], p->n_frags * sizeof(void *));
148 n_pkts_out = p->n_frags;
151 /* Look to "pkts" buffer to get more packets */
153 struct rte_mbuf *pkt;
154 uint32_t n_pkts_to_provide, i;
157 /* If "pkts" buffer is empty, read packet burst from ring */
158 if (p->n_pkts == 0) {
159 p->n_pkts = rte_ring_sc_dequeue_burst(p->ring,
160 (void **) p->pkts, RTE_PORT_IN_BURST_SIZE_MAX,
162 RTE_PORT_RING_READER_FRAG_STATS_PKTS_IN_ADD(p, p->n_pkts);
168 /* Read next packet from "pkts" buffer */
169 pkt = p->pkts[p->pos_pkts++];
172 /* If not jumbo, pass current packet to output */
173 if (pkt->pkt_len <= p->mtu) {
174 pkts[n_pkts_out++] = pkt;
176 n_pkts_to_provide = n_pkts - n_pkts_out;
177 if (n_pkts_to_provide == 0)
183 /* Fragment current packet into the "frags" buffer */
187 RTE_PORT_FRAG_MAX_FRAGS_PER_PACKET,
194 rte_pktmbuf_free(pkt);
195 RTE_PORT_RING_READER_FRAG_STATS_PKTS_DROP_ADD(p, 1);
199 p->n_frags = (uint32_t) status;
202 /* Copy meta-data from input jumbo packet to its fragments */
203 for (i = 0; i < p->n_frags; i++) {
205 RTE_MBUF_METADATA_UINT8_PTR(pkt, sizeof(struct rte_mbuf));
207 RTE_MBUF_METADATA_UINT8_PTR(p->frags[i], sizeof(struct rte_mbuf));
209 memcpy(dst, src, p->metadata_size);
212 /* Free input jumbo packet */
213 rte_pktmbuf_free(pkt);
215 /* Get packets from "frag" buffer */
216 n_pkts_to_provide = n_pkts - n_pkts_out;
217 if (p->n_frags >= n_pkts_to_provide) {
218 memcpy(&pkts[n_pkts_out], p->frags,
219 n_pkts_to_provide * sizeof(void *));
220 p->n_frags -= n_pkts_to_provide;
221 p->pos_frags += n_pkts_to_provide;
226 memcpy(&pkts[n_pkts_out], p->frags,
227 p->n_frags * sizeof(void *));
228 n_pkts_out += p->n_frags;
234 rte_port_ring_reader_frag_free(void *port)
237 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
247 rte_port_frag_reader_stats_read(void *port,
248 struct rte_port_in_stats *stats, int clear)
250 struct rte_port_ring_reader_frag *p =
254 memcpy(stats, &p->stats, sizeof(p->stats));
257 memset(&p->stats, 0, sizeof(p->stats));
263 * Summary of port operations
265 struct rte_port_in_ops rte_port_ring_reader_ipv4_frag_ops = {
266 .f_create = rte_port_ring_reader_ipv4_frag_create,
267 .f_free = rte_port_ring_reader_frag_free,
268 .f_rx = rte_port_ring_reader_frag_rx,
269 .f_stats = rte_port_frag_reader_stats_read,
272 struct rte_port_in_ops rte_port_ring_reader_ipv6_frag_ops = {
273 .f_create = rte_port_ring_reader_ipv6_frag_create,
274 .f_free = rte_port_ring_reader_frag_free,
275 .f_rx = rte_port_ring_reader_frag_rx,
276 .f_stats = rte_port_frag_reader_stats_read,