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 ipv4_hdr *pkt_hdr = rte_pktmbuf_mtod(pkt, struct ipv4_hdr *);
156 /* Get "More fragments" flag and fragment offset */
157 uint16_t frag_field = rte_be_to_cpu_16(pkt_hdr->fragment_offset);
158 uint16_t frag_offset = (uint16_t)(frag_field & IPV4_HDR_OFFSET_MASK);
159 uint16_t frag_flag = (uint16_t)(frag_field & IPV4_HDR_MF_FLAG);
161 /* If it is a fragmented packet, then try to reassemble */
162 if ((frag_flag == 0) && (frag_offset == 0))
163 p->tx_buf[p->tx_buf_count++] = pkt;
166 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
167 struct rte_ip_frag_death_row *dr = &p->death_row;
169 pkt->l3_len = sizeof(*pkt_hdr);
171 /* Process this fragment */
172 mo = rte_ipv4_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(),
175 p->tx_buf[p->tx_buf_count++] = mo;
177 rte_ip_frag_free_death_row(&p->death_row, 3);
182 process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
184 /* Assume there is no ethernet header */
185 struct ipv6_hdr *pkt_hdr = rte_pktmbuf_mtod(pkt, struct ipv6_hdr *);
187 struct ipv6_extension_fragment *frag_hdr;
188 uint16_t frag_data = 0;
189 frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(pkt_hdr);
190 if (frag_hdr != NULL)
191 frag_data = rte_be_to_cpu_16(frag_hdr->frag_data);
193 /* If it is a fragmented packet, then try to reassemble */
194 if ((frag_data & RTE_IPV6_FRAG_USED_MASK) == 0)
195 p->tx_buf[p->tx_buf_count++] = pkt;
198 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
199 struct rte_ip_frag_death_row *dr = &p->death_row;
201 pkt->l3_len = sizeof(*pkt_hdr) + sizeof(*frag_hdr);
203 /* Process this fragment */
204 mo = rte_ipv6_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(), pkt_hdr,
207 p->tx_buf[p->tx_buf_count++] = mo;
209 rte_ip_frag_free_death_row(&p->death_row, 3);
214 rte_port_ring_writer_ras_tx(void *port, struct rte_mbuf *pkt)
216 struct rte_port_ring_writer_ras *p =
219 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
221 if (p->tx_buf_count >= p->tx_burst_sz)
228 rte_port_ring_writer_ras_tx_bulk(void *port,
229 struct rte_mbuf **pkts,
232 struct rte_port_ring_writer_ras *p =
235 if ((pkts_mask & (pkts_mask + 1)) == 0) {
236 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
239 for (i = 0; i < n_pkts; i++) {
240 struct rte_mbuf *pkt = pkts[i];
242 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
244 if (p->tx_buf_count >= p->tx_burst_sz)
248 for ( ; pkts_mask; ) {
249 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
250 uint64_t pkt_mask = 1LLU << pkt_index;
251 struct rte_mbuf *pkt = pkts[pkt_index];
253 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
255 if (p->tx_buf_count >= p->tx_burst_sz)
258 pkts_mask &= ~pkt_mask;
266 rte_port_ring_writer_ras_flush(void *port)
268 struct rte_port_ring_writer_ras *p =
271 if (p->tx_buf_count > 0)
278 rte_port_ring_writer_ras_free(void *port)
280 struct rte_port_ring_writer_ras *p =
284 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
288 rte_port_ring_writer_ras_flush(port);
289 rte_ip_frag_table_destroy(p->frag_tbl);
296 rte_port_ras_writer_stats_read(void *port,
297 struct rte_port_out_stats *stats, int clear)
299 struct rte_port_ring_writer_ras *p =
303 memcpy(stats, &p->stats, sizeof(p->stats));
306 memset(&p->stats, 0, sizeof(p->stats));
312 * Summary of port operations
314 struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops = {
315 .f_create = rte_port_ring_writer_ipv4_ras_create,
316 .f_free = rte_port_ring_writer_ras_free,
317 .f_tx = rte_port_ring_writer_ras_tx,
318 .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
319 .f_flush = rte_port_ring_writer_ras_flush,
320 .f_stats = rte_port_ras_writer_stats_read,
323 struct rte_port_out_ops rte_port_ring_writer_ipv6_ras_ops = {
324 .f_create = rte_port_ring_writer_ipv6_ras_create,
325 .f_free = rte_port_ring_writer_ras_free,
326 .f_tx = rte_port_ring_writer_ras_tx,
327 .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
328 .f_flush = rte_port_ring_writer_ras_flush,
329 .f_stats = rte_port_ras_writer_stats_read,