1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <rte_ip_frag.h>
8 #include <rte_cycles.h>
11 #include "rte_port_ras.h"
13 #ifndef RTE_PORT_RAS_N_BUCKETS
14 #define RTE_PORT_RAS_N_BUCKETS 4094
17 #ifndef RTE_PORT_RAS_N_ENTRIES_PER_BUCKET
18 #define RTE_PORT_RAS_N_ENTRIES_PER_BUCKET 8
21 #ifndef RTE_PORT_RAS_N_ENTRIES
22 #define RTE_PORT_RAS_N_ENTRIES (RTE_PORT_RAS_N_BUCKETS * RTE_PORT_RAS_N_ENTRIES_PER_BUCKET)
25 #ifdef RTE_PORT_STATS_COLLECT
27 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val) \
28 port->stats.n_pkts_in += val
29 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val) \
30 port->stats.n_pkts_drop += val
34 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val)
35 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val)
39 struct rte_port_ring_writer_ras;
41 typedef void (*ras_op)(
42 struct rte_port_ring_writer_ras *p,
43 struct rte_mbuf *pkt);
46 process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
48 process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
50 struct rte_port_ring_writer_ras {
51 struct rte_port_out_stats stats;
53 struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
54 struct rte_ring *ring;
56 uint32_t tx_buf_count;
57 struct rte_ip_frag_tbl *frag_tbl;
58 struct rte_ip_frag_death_row death_row;
64 rte_port_ring_writer_ras_create(void *params, int socket_id, int is_ipv4)
66 struct rte_port_ring_writer_ras_params *conf =
68 struct rte_port_ring_writer_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(
100 RTE_PORT_RAS_N_BUCKETS,
101 RTE_PORT_RAS_N_ENTRIES_PER_BUCKET,
102 RTE_PORT_RAS_N_ENTRIES,
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;
118 port->f_ras = (is_ipv4 == 1) ? process_ipv4 : process_ipv6;
124 rte_port_ring_writer_ipv4_ras_create(void *params, int socket_id)
126 return rte_port_ring_writer_ras_create(params, socket_id, 1);
130 rte_port_ring_writer_ipv6_ras_create(void *params, int socket_id)
132 return rte_port_ring_writer_ras_create(params, socket_id, 0);
136 send_burst(struct rte_port_ring_writer_ras *p)
140 nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
141 p->tx_buf_count, NULL);
143 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
144 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
145 rte_pktmbuf_free(p->tx_buf[nb_tx]);
151 process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
153 /* Assume there is no ethernet header */
154 struct rte_ipv4_hdr *pkt_hdr =
155 rte_pktmbuf_mtod(pkt, struct rte_ipv4_hdr *);
157 /* Get "More fragments" flag and fragment offset */
158 uint16_t frag_field = rte_be_to_cpu_16(pkt_hdr->fragment_offset);
159 uint16_t frag_offset = (uint16_t)(frag_field & RTE_IPV4_HDR_OFFSET_MASK);
160 uint16_t frag_flag = (uint16_t)(frag_field & RTE_IPV4_HDR_MF_FLAG);
162 /* If it is a fragmented packet, then try to reassemble */
163 if ((frag_flag == 0) && (frag_offset == 0))
164 p->tx_buf[p->tx_buf_count++] = pkt;
167 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
168 struct rte_ip_frag_death_row *dr = &p->death_row;
170 pkt->l3_len = sizeof(*pkt_hdr);
172 /* Process this fragment */
173 mo = rte_ipv4_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(),
176 p->tx_buf[p->tx_buf_count++] = mo;
178 rte_ip_frag_free_death_row(&p->death_row, 3);
183 process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
185 /* Assume there is no ethernet header */
186 struct rte_ipv6_hdr *pkt_hdr =
187 rte_pktmbuf_mtod(pkt, struct rte_ipv6_hdr *);
189 struct ipv6_extension_fragment *frag_hdr;
190 uint16_t frag_data = 0;
191 frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(pkt_hdr);
192 if (frag_hdr != NULL)
193 frag_data = rte_be_to_cpu_16(frag_hdr->frag_data);
195 /* If it is a fragmented packet, then try to reassemble */
196 if ((frag_data & RTE_IPV6_FRAG_USED_MASK) == 0)
197 p->tx_buf[p->tx_buf_count++] = pkt;
200 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
201 struct rte_ip_frag_death_row *dr = &p->death_row;
203 pkt->l3_len = sizeof(*pkt_hdr) + sizeof(*frag_hdr);
205 /* Process this fragment */
206 mo = rte_ipv6_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(), pkt_hdr,
209 p->tx_buf[p->tx_buf_count++] = mo;
211 rte_ip_frag_free_death_row(&p->death_row, 3);
216 rte_port_ring_writer_ras_tx(void *port, struct rte_mbuf *pkt)
218 struct rte_port_ring_writer_ras *p =
221 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
223 if (p->tx_buf_count >= p->tx_burst_sz)
230 rte_port_ring_writer_ras_tx_bulk(void *port,
231 struct rte_mbuf **pkts,
234 struct rte_port_ring_writer_ras *p =
237 if ((pkts_mask & (pkts_mask + 1)) == 0) {
238 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
241 for (i = 0; i < n_pkts; i++) {
242 struct rte_mbuf *pkt = pkts[i];
244 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
246 if (p->tx_buf_count >= p->tx_burst_sz)
250 for ( ; pkts_mask; ) {
251 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
252 uint64_t pkt_mask = 1LLU << pkt_index;
253 struct rte_mbuf *pkt = pkts[pkt_index];
255 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
257 if (p->tx_buf_count >= p->tx_burst_sz)
260 pkts_mask &= ~pkt_mask;
268 rte_port_ring_writer_ras_flush(void *port)
270 struct rte_port_ring_writer_ras *p =
273 if (p->tx_buf_count > 0)
280 rte_port_ring_writer_ras_free(void *port)
282 struct rte_port_ring_writer_ras *p =
286 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
290 rte_port_ring_writer_ras_flush(port);
291 rte_ip_frag_table_destroy(p->frag_tbl);
298 rte_port_ras_writer_stats_read(void *port,
299 struct rte_port_out_stats *stats, int clear)
301 struct rte_port_ring_writer_ras *p =
305 memcpy(stats, &p->stats, sizeof(p->stats));
308 memset(&p->stats, 0, sizeof(p->stats));
314 * Summary of port operations
316 struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops = {
317 .f_create = rte_port_ring_writer_ipv4_ras_create,
318 .f_free = rte_port_ring_writer_ras_free,
319 .f_tx = rte_port_ring_writer_ras_tx,
320 .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
321 .f_flush = rte_port_ring_writer_ras_flush,
322 .f_stats = rte_port_ras_writer_stats_read,
325 struct rte_port_out_ops rte_port_ring_writer_ipv6_ras_ops = {
326 .f_create = rte_port_ring_writer_ipv6_ras_create,
327 .f_free = rte_port_ring_writer_ras_free,
328 .f_tx = rte_port_ring_writer_ras_tx,
329 .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
330 .f_flush = rte_port_ring_writer_ras_flush,
331 .f_stats = rte_port_ras_writer_stats_read,