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_cycles.h>
40 #include "rte_port_ras.h"
42 #ifndef IPV4_RAS_N_BUCKETS
43 #define IPV4_RAS_N_BUCKETS 4094
46 #ifndef IPV4_RAS_N_ENTRIES_PER_BUCKET
47 #define IPV4_RAS_N_ENTRIES_PER_BUCKET 8
50 #ifndef IPV4_RAS_N_ENTRIES
51 #define IPV4_RAS_N_ENTRIES (IPV4_RAS_N_BUCKETS * IPV4_RAS_N_ENTRIES_PER_BUCKET)
54 struct rte_port_ring_writer_ipv4_ras {
55 struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
56 struct rte_ring *ring;
58 uint32_t tx_buf_count;
59 struct rte_ip_frag_tbl *frag_tbl;
60 struct rte_ip_frag_death_row death_row;
64 rte_port_ring_writer_ipv4_ras_create(void *params, int socket_id)
66 struct rte_port_ring_writer_ipv4_ras_params *conf =
67 (struct rte_port_ring_writer_ipv4_ras_params *) params;
68 struct rte_port_ring_writer_ipv4_ras *port;
71 /* Check input parameters */
73 RTE_LOG(ERR, PORT, "%s: Parameter conf is NULL\n", __func__);
76 if (conf->ring == NULL) {
77 RTE_LOG(ERR, PORT, "%s: Parameter ring is NULL\n", __func__);
80 if ((conf->tx_burst_sz == 0) ||
81 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX)) {
82 RTE_LOG(ERR, PORT, "%s: Parameter tx_burst_sz is invalid\n",
87 /* Memory allocation */
88 port = rte_zmalloc_socket("PORT", sizeof(*port),
89 RTE_CACHE_LINE_SIZE, socket_id);
91 RTE_LOG(ERR, PORT, "%s: Failed to allocate socket\n", __func__);
95 /* Create fragmentation table */
96 frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * MS_PER_S;
99 port->frag_tbl = rte_ip_frag_table_create(
101 IPV4_RAS_N_ENTRIES_PER_BUCKET,
106 if (port->frag_tbl == NULL) {
107 RTE_LOG(ERR, PORT, "%s: rte_ip_frag_table_create failed\n",
114 port->ring = conf->ring;
115 port->tx_burst_sz = conf->tx_burst_sz;
116 port->tx_buf_count = 0;
122 send_burst(struct rte_port_ring_writer_ipv4_ras *p)
126 nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
129 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
130 rte_pktmbuf_free(p->tx_buf[nb_tx]);
136 process_one(struct rte_port_ring_writer_ipv4_ras *p, struct rte_mbuf *pkt)
138 /* Assume there is no ethernet header */
139 struct ipv4_hdr *pkt_hdr = (struct ipv4_hdr *)
140 (rte_pktmbuf_mtod(pkt, unsigned char *));
142 /* Get "Do not fragment" flag and fragment offset */
143 uint16_t frag_field = rte_be_to_cpu_16(pkt_hdr->fragment_offset);
144 uint16_t frag_offset = (uint16_t)(frag_field & IPV4_HDR_OFFSET_MASK);
145 uint16_t frag_flag = (uint16_t)(frag_field & IPV4_HDR_MF_FLAG);
147 /* If it is a fragmented packet, then try to reassemble */
148 if ((frag_flag == 0) && (frag_offset == 0))
149 p->tx_buf[p->tx_buf_count++] = pkt;
152 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
153 struct rte_ip_frag_death_row *dr = &p->death_row;
155 /* Process this fragment */
156 mo = rte_ipv4_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(), pkt_hdr);
158 p->tx_buf[p->tx_buf_count++] = mo;
160 rte_ip_frag_free_death_row(&p->death_row, 3);
165 rte_port_ring_writer_ipv4_ras_tx(void *port, struct rte_mbuf *pkt)
167 struct rte_port_ring_writer_ipv4_ras *p =
168 (struct rte_port_ring_writer_ipv4_ras *) port;
171 if (p->tx_buf_count >= p->tx_burst_sz)
178 rte_port_ring_writer_ipv4_ras_tx_bulk(void *port,
179 struct rte_mbuf **pkts,
182 struct rte_port_ring_writer_ipv4_ras *p =
183 (struct rte_port_ring_writer_ipv4_ras *) port;
185 if ((pkts_mask & (pkts_mask + 1)) == 0) {
186 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
189 for (i = 0; i < n_pkts; i++) {
190 struct rte_mbuf *pkt = pkts[i];
193 if (p->tx_buf_count >= p->tx_burst_sz)
197 for ( ; pkts_mask; ) {
198 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
199 uint64_t pkt_mask = 1LLU << pkt_index;
200 struct rte_mbuf *pkt = pkts[pkt_index];
203 if (p->tx_buf_count >= p->tx_burst_sz)
206 pkts_mask &= ~pkt_mask;
214 rte_port_ring_writer_ipv4_ras_flush(void *port)
216 struct rte_port_ring_writer_ipv4_ras *p =
217 (struct rte_port_ring_writer_ipv4_ras *) port;
219 if (p->tx_buf_count > 0)
226 rte_port_ring_writer_ipv4_ras_free(void *port)
228 struct rte_port_ring_writer_ipv4_ras *p =
229 (struct rte_port_ring_writer_ipv4_ras *) port;
232 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
236 rte_port_ring_writer_ipv4_ras_flush(port);
237 rte_ip_frag_table_destroy(p->frag_tbl);
244 * Summary of port operations
246 struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops = {
247 .f_create = rte_port_ring_writer_ipv4_ras_create,
248 .f_free = rte_port_ring_writer_ipv4_ras_free,
249 .f_tx = rte_port_ring_writer_ipv4_ras_tx,
250 .f_tx_bulk = rte_port_ring_writer_ipv4_ras_tx_bulk,
251 .f_flush = rte_port_ring_writer_ipv4_ras_flush,