1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
6 #include <rte_ip_frag.h>
7 #include <rte_cycles.h>
10 #include "rte_port_ras.h"
12 #ifndef RTE_PORT_RAS_N_BUCKETS
13 #define RTE_PORT_RAS_N_BUCKETS 4094
16 #ifndef RTE_PORT_RAS_N_ENTRIES_PER_BUCKET
17 #define RTE_PORT_RAS_N_ENTRIES_PER_BUCKET 8
20 #ifndef RTE_PORT_RAS_N_ENTRIES
21 #define RTE_PORT_RAS_N_ENTRIES (RTE_PORT_RAS_N_BUCKETS * RTE_PORT_RAS_N_ENTRIES_PER_BUCKET)
24 #ifdef RTE_PORT_STATS_COLLECT
26 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val) \
27 port->stats.n_pkts_in += val
28 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val) \
29 port->stats.n_pkts_drop += val
33 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val)
34 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val)
38 struct rte_port_ring_writer_ras;
40 typedef void (*ras_op)(
41 struct rte_port_ring_writer_ras *p,
42 struct rte_mbuf *pkt);
45 process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
47 process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
49 struct rte_port_ring_writer_ras {
50 struct rte_port_out_stats stats;
52 struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
53 struct rte_ring *ring;
55 uint32_t tx_buf_count;
56 struct rte_ip_frag_tbl *frag_tbl;
57 struct rte_ip_frag_death_row death_row;
63 rte_port_ring_writer_ras_create(void *params, int socket_id, int is_ipv4)
65 struct rte_port_ring_writer_ras_params *conf =
67 struct rte_port_ring_writer_ras *port;
70 /* Check input parameters */
72 RTE_LOG(ERR, PORT, "%s: Parameter conf is NULL\n", __func__);
75 if (conf->ring == NULL) {
76 RTE_LOG(ERR, PORT, "%s: Parameter ring is NULL\n", __func__);
79 if ((conf->tx_burst_sz == 0) ||
80 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX)) {
81 RTE_LOG(ERR, PORT, "%s: Parameter tx_burst_sz is invalid\n",
86 /* Memory allocation */
87 port = rte_zmalloc_socket("PORT", sizeof(*port),
88 RTE_CACHE_LINE_SIZE, socket_id);
90 RTE_LOG(ERR, PORT, "%s: Failed to allocate socket\n", __func__);
94 /* Create fragmentation table */
95 frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * MS_PER_S;
98 port->frag_tbl = rte_ip_frag_table_create(
99 RTE_PORT_RAS_N_BUCKETS,
100 RTE_PORT_RAS_N_ENTRIES_PER_BUCKET,
101 RTE_PORT_RAS_N_ENTRIES,
105 if (port->frag_tbl == NULL) {
106 RTE_LOG(ERR, PORT, "%s: rte_ip_frag_table_create failed\n",
113 port->ring = conf->ring;
114 port->tx_burst_sz = conf->tx_burst_sz;
115 port->tx_buf_count = 0;
117 port->f_ras = (is_ipv4 == 1) ? process_ipv4 : process_ipv6;
123 rte_port_ring_writer_ipv4_ras_create(void *params, int socket_id)
125 return rte_port_ring_writer_ras_create(params, socket_id, 1);
129 rte_port_ring_writer_ipv6_ras_create(void *params, int socket_id)
131 return rte_port_ring_writer_ras_create(params, socket_id, 0);
135 send_burst(struct rte_port_ring_writer_ras *p)
139 nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
140 p->tx_buf_count, NULL);
142 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
143 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
144 rte_pktmbuf_free(p->tx_buf[nb_tx]);
150 process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
152 /* Assume there is no ethernet header */
153 struct rte_ipv4_hdr *pkt_hdr =
154 rte_pktmbuf_mtod(pkt, struct rte_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 & RTE_IPV4_HDR_OFFSET_MASK);
159 uint16_t frag_flag = (uint16_t)(frag_field & RTE_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 rte_ipv6_hdr *pkt_hdr =
186 rte_pktmbuf_mtod(pkt, struct rte_ipv6_hdr *);
188 struct rte_ipv6_fragment_ext *frag_hdr;
189 uint16_t frag_data = 0;
190 frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(pkt_hdr);
191 if (frag_hdr != NULL)
192 frag_data = rte_be_to_cpu_16(frag_hdr->frag_data);
194 /* If it is a fragmented packet, then try to reassemble */
195 if ((frag_data & RTE_IPV6_FRAG_USED_MASK) == 0)
196 p->tx_buf[p->tx_buf_count++] = pkt;
199 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
200 struct rte_ip_frag_death_row *dr = &p->death_row;
202 pkt->l3_len = sizeof(*pkt_hdr) + sizeof(*frag_hdr);
204 /* Process this fragment */
205 mo = rte_ipv6_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(), pkt_hdr,
208 p->tx_buf[p->tx_buf_count++] = mo;
210 rte_ip_frag_free_death_row(&p->death_row, 3);
215 rte_port_ring_writer_ras_tx(void *port, struct rte_mbuf *pkt)
217 struct rte_port_ring_writer_ras *p =
220 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
222 if (p->tx_buf_count >= p->tx_burst_sz)
229 rte_port_ring_writer_ras_tx_bulk(void *port,
230 struct rte_mbuf **pkts,
233 struct rte_port_ring_writer_ras *p =
236 if ((pkts_mask & (pkts_mask + 1)) == 0) {
237 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
240 for (i = 0; i < n_pkts; i++) {
241 struct rte_mbuf *pkt = pkts[i];
243 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
245 if (p->tx_buf_count >= p->tx_burst_sz)
249 for ( ; pkts_mask; ) {
250 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
251 uint64_t pkt_mask = 1LLU << pkt_index;
252 struct rte_mbuf *pkt = pkts[pkt_index];
254 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
256 if (p->tx_buf_count >= p->tx_burst_sz)
259 pkts_mask &= ~pkt_mask;
267 rte_port_ring_writer_ras_flush(void *port)
269 struct rte_port_ring_writer_ras *p =
272 if (p->tx_buf_count > 0)
279 rte_port_ring_writer_ras_free(void *port)
281 struct rte_port_ring_writer_ras *p =
285 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
289 rte_port_ring_writer_ras_flush(port);
290 rte_ip_frag_table_destroy(p->frag_tbl);
297 rte_port_ras_writer_stats_read(void *port,
298 struct rte_port_out_stats *stats, int clear)
300 struct rte_port_ring_writer_ras *p =
304 memcpy(stats, &p->stats, sizeof(p->stats));
307 memset(&p->stats, 0, sizeof(p->stats));
313 * Summary of port operations
315 struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops = {
316 .f_create = rte_port_ring_writer_ipv4_ras_create,
317 .f_free = rte_port_ring_writer_ras_free,
318 .f_tx = rte_port_ring_writer_ras_tx,
319 .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
320 .f_flush = rte_port_ring_writer_ras_flush,
321 .f_stats = rte_port_ras_writer_stats_read,
324 struct rte_port_out_ops rte_port_ring_writer_ipv6_ras_ops = {
325 .f_create = rte_port_ring_writer_ipv6_ras_create,
326 .f_free = rte_port_ring_writer_ras_free,
327 .f_tx = rte_port_ring_writer_ras_tx,
328 .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
329 .f_flush = rte_port_ring_writer_ras_flush,
330 .f_stats = rte_port_ras_writer_stats_read,