199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson #include <string.h>
599a2dd95SBruce Richardson
699a2dd95SBruce Richardson #include <rte_ip_frag.h>
799a2dd95SBruce Richardson #include <rte_cycles.h>
899a2dd95SBruce Richardson #include <rte_log.h>
999a2dd95SBruce Richardson
1099a2dd95SBruce Richardson #include "rte_port_ras.h"
1199a2dd95SBruce Richardson
12*ae67895bSDavid Marchand #include "port_log.h"
13*ae67895bSDavid Marchand
1499a2dd95SBruce Richardson #ifndef RTE_PORT_RAS_N_BUCKETS
1599a2dd95SBruce Richardson #define RTE_PORT_RAS_N_BUCKETS 4094
1699a2dd95SBruce Richardson #endif
1799a2dd95SBruce Richardson
1899a2dd95SBruce Richardson #ifndef RTE_PORT_RAS_N_ENTRIES_PER_BUCKET
1999a2dd95SBruce Richardson #define RTE_PORT_RAS_N_ENTRIES_PER_BUCKET 8
2099a2dd95SBruce Richardson #endif
2199a2dd95SBruce Richardson
2299a2dd95SBruce Richardson #ifndef RTE_PORT_RAS_N_ENTRIES
2399a2dd95SBruce Richardson #define RTE_PORT_RAS_N_ENTRIES (RTE_PORT_RAS_N_BUCKETS * RTE_PORT_RAS_N_ENTRIES_PER_BUCKET)
2499a2dd95SBruce Richardson #endif
2599a2dd95SBruce Richardson
2699a2dd95SBruce Richardson #ifdef RTE_PORT_STATS_COLLECT
2799a2dd95SBruce Richardson
2899a2dd95SBruce Richardson #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val) \
2999a2dd95SBruce Richardson port->stats.n_pkts_in += val
3099a2dd95SBruce Richardson #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val) \
3199a2dd95SBruce Richardson port->stats.n_pkts_drop += val
3299a2dd95SBruce Richardson
3399a2dd95SBruce Richardson #else
3499a2dd95SBruce Richardson
3599a2dd95SBruce Richardson #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val)
3699a2dd95SBruce Richardson #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val)
3799a2dd95SBruce Richardson
3899a2dd95SBruce Richardson #endif
3999a2dd95SBruce Richardson
4099a2dd95SBruce Richardson struct rte_port_ring_writer_ras;
4199a2dd95SBruce Richardson
4299a2dd95SBruce Richardson typedef void (*ras_op)(
4399a2dd95SBruce Richardson struct rte_port_ring_writer_ras *p,
4499a2dd95SBruce Richardson struct rte_mbuf *pkt);
4599a2dd95SBruce Richardson
4699a2dd95SBruce Richardson static void
4799a2dd95SBruce Richardson process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
4899a2dd95SBruce Richardson static void
4999a2dd95SBruce Richardson process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
5099a2dd95SBruce Richardson
5199a2dd95SBruce Richardson struct rte_port_ring_writer_ras {
5299a2dd95SBruce Richardson struct rte_port_out_stats stats;
5399a2dd95SBruce Richardson
5499a2dd95SBruce Richardson struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
5599a2dd95SBruce Richardson struct rte_ring *ring;
5699a2dd95SBruce Richardson uint32_t tx_burst_sz;
5799a2dd95SBruce Richardson uint32_t tx_buf_count;
5899a2dd95SBruce Richardson struct rte_ip_frag_tbl *frag_tbl;
5999a2dd95SBruce Richardson struct rte_ip_frag_death_row death_row;
6099a2dd95SBruce Richardson
6199a2dd95SBruce Richardson ras_op f_ras;
6299a2dd95SBruce Richardson };
6399a2dd95SBruce Richardson
6499a2dd95SBruce Richardson static void *
rte_port_ring_writer_ras_create(void * params,int socket_id,int is_ipv4)6599a2dd95SBruce Richardson rte_port_ring_writer_ras_create(void *params, int socket_id, int is_ipv4)
6699a2dd95SBruce Richardson {
6799a2dd95SBruce Richardson struct rte_port_ring_writer_ras_params *conf =
6899a2dd95SBruce Richardson params;
6999a2dd95SBruce Richardson struct rte_port_ring_writer_ras *port;
7099a2dd95SBruce Richardson uint64_t frag_cycles;
7199a2dd95SBruce Richardson
7299a2dd95SBruce Richardson /* Check input parameters */
7399a2dd95SBruce Richardson if (conf == NULL) {
74*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Parameter conf is NULL", __func__);
7599a2dd95SBruce Richardson return NULL;
7699a2dd95SBruce Richardson }
7799a2dd95SBruce Richardson if (conf->ring == NULL) {
78*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Parameter ring is NULL", __func__);
7999a2dd95SBruce Richardson return NULL;
8099a2dd95SBruce Richardson }
8199a2dd95SBruce Richardson if ((conf->tx_burst_sz == 0) ||
8299a2dd95SBruce Richardson (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX)) {
83*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Parameter tx_burst_sz is invalid",
8499a2dd95SBruce Richardson __func__);
8599a2dd95SBruce Richardson return NULL;
8699a2dd95SBruce Richardson }
8799a2dd95SBruce Richardson
8899a2dd95SBruce Richardson /* Memory allocation */
8999a2dd95SBruce Richardson port = rte_zmalloc_socket("PORT", sizeof(*port),
9099a2dd95SBruce Richardson RTE_CACHE_LINE_SIZE, socket_id);
9199a2dd95SBruce Richardson if (port == NULL) {
92*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Failed to allocate socket", __func__);
9399a2dd95SBruce Richardson return NULL;
9499a2dd95SBruce Richardson }
9599a2dd95SBruce Richardson
9699a2dd95SBruce Richardson /* Create fragmentation table */
9799a2dd95SBruce Richardson frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * MS_PER_S;
9899a2dd95SBruce Richardson frag_cycles *= 100;
9999a2dd95SBruce Richardson
10099a2dd95SBruce Richardson port->frag_tbl = rte_ip_frag_table_create(
10199a2dd95SBruce Richardson RTE_PORT_RAS_N_BUCKETS,
10299a2dd95SBruce Richardson RTE_PORT_RAS_N_ENTRIES_PER_BUCKET,
10399a2dd95SBruce Richardson RTE_PORT_RAS_N_ENTRIES,
10499a2dd95SBruce Richardson frag_cycles,
10599a2dd95SBruce Richardson socket_id);
10699a2dd95SBruce Richardson
10799a2dd95SBruce Richardson if (port->frag_tbl == NULL) {
108*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: rte_ip_frag_table_create failed",
10999a2dd95SBruce Richardson __func__);
11099a2dd95SBruce Richardson rte_free(port);
11199a2dd95SBruce Richardson return NULL;
11299a2dd95SBruce Richardson }
11399a2dd95SBruce Richardson
11499a2dd95SBruce Richardson /* Initialization */
11599a2dd95SBruce Richardson port->ring = conf->ring;
11699a2dd95SBruce Richardson port->tx_burst_sz = conf->tx_burst_sz;
11799a2dd95SBruce Richardson port->tx_buf_count = 0;
11899a2dd95SBruce Richardson
11999a2dd95SBruce Richardson port->f_ras = (is_ipv4 == 1) ? process_ipv4 : process_ipv6;
12099a2dd95SBruce Richardson
12199a2dd95SBruce Richardson return port;
12299a2dd95SBruce Richardson }
12399a2dd95SBruce Richardson
12499a2dd95SBruce Richardson static void *
rte_port_ring_writer_ipv4_ras_create(void * params,int socket_id)12599a2dd95SBruce Richardson rte_port_ring_writer_ipv4_ras_create(void *params, int socket_id)
12699a2dd95SBruce Richardson {
12799a2dd95SBruce Richardson return rte_port_ring_writer_ras_create(params, socket_id, 1);
12899a2dd95SBruce Richardson }
12999a2dd95SBruce Richardson
13099a2dd95SBruce Richardson static void *
rte_port_ring_writer_ipv6_ras_create(void * params,int socket_id)13199a2dd95SBruce Richardson rte_port_ring_writer_ipv6_ras_create(void *params, int socket_id)
13299a2dd95SBruce Richardson {
13399a2dd95SBruce Richardson return rte_port_ring_writer_ras_create(params, socket_id, 0);
13499a2dd95SBruce Richardson }
13599a2dd95SBruce Richardson
13699a2dd95SBruce Richardson static inline void
send_burst(struct rte_port_ring_writer_ras * p)13799a2dd95SBruce Richardson send_burst(struct rte_port_ring_writer_ras *p)
13899a2dd95SBruce Richardson {
13999a2dd95SBruce Richardson uint32_t nb_tx;
14099a2dd95SBruce Richardson
14199a2dd95SBruce Richardson nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
14299a2dd95SBruce Richardson p->tx_buf_count, NULL);
14399a2dd95SBruce Richardson
14499a2dd95SBruce Richardson RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
14599a2dd95SBruce Richardson for ( ; nb_tx < p->tx_buf_count; nb_tx++)
14699a2dd95SBruce Richardson rte_pktmbuf_free(p->tx_buf[nb_tx]);
14799a2dd95SBruce Richardson
14899a2dd95SBruce Richardson p->tx_buf_count = 0;
14999a2dd95SBruce Richardson }
15099a2dd95SBruce Richardson
15199a2dd95SBruce Richardson static void
process_ipv4(struct rte_port_ring_writer_ras * p,struct rte_mbuf * pkt)15299a2dd95SBruce Richardson process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
15399a2dd95SBruce Richardson {
15499a2dd95SBruce Richardson /* Assume there is no ethernet header */
15599a2dd95SBruce Richardson struct rte_ipv4_hdr *pkt_hdr =
15699a2dd95SBruce Richardson rte_pktmbuf_mtod(pkt, struct rte_ipv4_hdr *);
15799a2dd95SBruce Richardson
15899a2dd95SBruce Richardson /* Get "More fragments" flag and fragment offset */
15999a2dd95SBruce Richardson uint16_t frag_field = rte_be_to_cpu_16(pkt_hdr->fragment_offset);
16099a2dd95SBruce Richardson uint16_t frag_offset = (uint16_t)(frag_field & RTE_IPV4_HDR_OFFSET_MASK);
16199a2dd95SBruce Richardson uint16_t frag_flag = (uint16_t)(frag_field & RTE_IPV4_HDR_MF_FLAG);
16299a2dd95SBruce Richardson
16399a2dd95SBruce Richardson /* If it is a fragmented packet, then try to reassemble */
16499a2dd95SBruce Richardson if ((frag_flag == 0) && (frag_offset == 0))
16599a2dd95SBruce Richardson p->tx_buf[p->tx_buf_count++] = pkt;
16699a2dd95SBruce Richardson else {
16799a2dd95SBruce Richardson struct rte_mbuf *mo;
16899a2dd95SBruce Richardson struct rte_ip_frag_tbl *tbl = p->frag_tbl;
16999a2dd95SBruce Richardson struct rte_ip_frag_death_row *dr = &p->death_row;
17099a2dd95SBruce Richardson
17199a2dd95SBruce Richardson pkt->l3_len = sizeof(*pkt_hdr);
17299a2dd95SBruce Richardson
17399a2dd95SBruce Richardson /* Process this fragment */
17499a2dd95SBruce Richardson mo = rte_ipv4_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(),
17599a2dd95SBruce Richardson pkt_hdr);
17699a2dd95SBruce Richardson if (mo != NULL)
17799a2dd95SBruce Richardson p->tx_buf[p->tx_buf_count++] = mo;
17899a2dd95SBruce Richardson
17999a2dd95SBruce Richardson rte_ip_frag_free_death_row(&p->death_row, 3);
18099a2dd95SBruce Richardson }
18199a2dd95SBruce Richardson }
18299a2dd95SBruce Richardson
18399a2dd95SBruce Richardson static void
process_ipv6(struct rte_port_ring_writer_ras * p,struct rte_mbuf * pkt)18499a2dd95SBruce Richardson process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
18599a2dd95SBruce Richardson {
18699a2dd95SBruce Richardson /* Assume there is no ethernet header */
18799a2dd95SBruce Richardson struct rte_ipv6_hdr *pkt_hdr =
18899a2dd95SBruce Richardson rte_pktmbuf_mtod(pkt, struct rte_ipv6_hdr *);
18999a2dd95SBruce Richardson
190b7fc82ecSKonstantin Ananyev struct rte_ipv6_fragment_ext *frag_hdr;
19199a2dd95SBruce Richardson uint16_t frag_data = 0;
19299a2dd95SBruce Richardson frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(pkt_hdr);
19399a2dd95SBruce Richardson if (frag_hdr != NULL)
19499a2dd95SBruce Richardson frag_data = rte_be_to_cpu_16(frag_hdr->frag_data);
19599a2dd95SBruce Richardson
19699a2dd95SBruce Richardson /* If it is a fragmented packet, then try to reassemble */
19799a2dd95SBruce Richardson if ((frag_data & RTE_IPV6_FRAG_USED_MASK) == 0)
19899a2dd95SBruce Richardson p->tx_buf[p->tx_buf_count++] = pkt;
19999a2dd95SBruce Richardson else {
20099a2dd95SBruce Richardson struct rte_mbuf *mo;
20199a2dd95SBruce Richardson struct rte_ip_frag_tbl *tbl = p->frag_tbl;
20299a2dd95SBruce Richardson struct rte_ip_frag_death_row *dr = &p->death_row;
20399a2dd95SBruce Richardson
20499a2dd95SBruce Richardson pkt->l3_len = sizeof(*pkt_hdr) + sizeof(*frag_hdr);
20599a2dd95SBruce Richardson
20699a2dd95SBruce Richardson /* Process this fragment */
20799a2dd95SBruce Richardson mo = rte_ipv6_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(), pkt_hdr,
20899a2dd95SBruce Richardson frag_hdr);
20999a2dd95SBruce Richardson if (mo != NULL)
21099a2dd95SBruce Richardson p->tx_buf[p->tx_buf_count++] = mo;
21199a2dd95SBruce Richardson
21299a2dd95SBruce Richardson rte_ip_frag_free_death_row(&p->death_row, 3);
21399a2dd95SBruce Richardson }
21499a2dd95SBruce Richardson }
21599a2dd95SBruce Richardson
21699a2dd95SBruce Richardson static int
rte_port_ring_writer_ras_tx(void * port,struct rte_mbuf * pkt)21799a2dd95SBruce Richardson rte_port_ring_writer_ras_tx(void *port, struct rte_mbuf *pkt)
21899a2dd95SBruce Richardson {
21999a2dd95SBruce Richardson struct rte_port_ring_writer_ras *p =
22099a2dd95SBruce Richardson port;
22199a2dd95SBruce Richardson
22299a2dd95SBruce Richardson RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
22399a2dd95SBruce Richardson p->f_ras(p, pkt);
22499a2dd95SBruce Richardson if (p->tx_buf_count >= p->tx_burst_sz)
22599a2dd95SBruce Richardson send_burst(p);
22699a2dd95SBruce Richardson
22799a2dd95SBruce Richardson return 0;
22899a2dd95SBruce Richardson }
22999a2dd95SBruce Richardson
23099a2dd95SBruce Richardson static int
rte_port_ring_writer_ras_tx_bulk(void * port,struct rte_mbuf ** pkts,uint64_t pkts_mask)23199a2dd95SBruce Richardson rte_port_ring_writer_ras_tx_bulk(void *port,
23299a2dd95SBruce Richardson struct rte_mbuf **pkts,
23399a2dd95SBruce Richardson uint64_t pkts_mask)
23499a2dd95SBruce Richardson {
23599a2dd95SBruce Richardson struct rte_port_ring_writer_ras *p =
23699a2dd95SBruce Richardson port;
23799a2dd95SBruce Richardson
23899a2dd95SBruce Richardson if ((pkts_mask & (pkts_mask + 1)) == 0) {
2393d4e27fdSDavid Marchand uint64_t n_pkts = rte_popcount64(pkts_mask);
24099a2dd95SBruce Richardson uint32_t i;
24199a2dd95SBruce Richardson
24299a2dd95SBruce Richardson for (i = 0; i < n_pkts; i++) {
24399a2dd95SBruce Richardson struct rte_mbuf *pkt = pkts[i];
24499a2dd95SBruce Richardson
24599a2dd95SBruce Richardson RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
24699a2dd95SBruce Richardson p->f_ras(p, pkt);
24799a2dd95SBruce Richardson if (p->tx_buf_count >= p->tx_burst_sz)
24899a2dd95SBruce Richardson send_burst(p);
24999a2dd95SBruce Richardson }
25099a2dd95SBruce Richardson } else {
25199a2dd95SBruce Richardson for ( ; pkts_mask; ) {
2523d4e27fdSDavid Marchand uint32_t pkt_index = rte_ctz64(pkts_mask);
25399a2dd95SBruce Richardson uint64_t pkt_mask = 1LLU << pkt_index;
25499a2dd95SBruce Richardson struct rte_mbuf *pkt = pkts[pkt_index];
25599a2dd95SBruce Richardson
25699a2dd95SBruce Richardson RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
25799a2dd95SBruce Richardson p->f_ras(p, pkt);
25899a2dd95SBruce Richardson if (p->tx_buf_count >= p->tx_burst_sz)
25999a2dd95SBruce Richardson send_burst(p);
26099a2dd95SBruce Richardson
26199a2dd95SBruce Richardson pkts_mask &= ~pkt_mask;
26299a2dd95SBruce Richardson }
26399a2dd95SBruce Richardson }
26499a2dd95SBruce Richardson
26599a2dd95SBruce Richardson return 0;
26699a2dd95SBruce Richardson }
26799a2dd95SBruce Richardson
26899a2dd95SBruce Richardson static int
rte_port_ring_writer_ras_flush(void * port)26999a2dd95SBruce Richardson rte_port_ring_writer_ras_flush(void *port)
27099a2dd95SBruce Richardson {
27199a2dd95SBruce Richardson struct rte_port_ring_writer_ras *p =
27299a2dd95SBruce Richardson port;
27399a2dd95SBruce Richardson
27499a2dd95SBruce Richardson if (p->tx_buf_count > 0)
27599a2dd95SBruce Richardson send_burst(p);
27699a2dd95SBruce Richardson
27799a2dd95SBruce Richardson return 0;
27899a2dd95SBruce Richardson }
27999a2dd95SBruce Richardson
28099a2dd95SBruce Richardson static int
rte_port_ring_writer_ras_free(void * port)28199a2dd95SBruce Richardson rte_port_ring_writer_ras_free(void *port)
28299a2dd95SBruce Richardson {
28399a2dd95SBruce Richardson struct rte_port_ring_writer_ras *p =
28499a2dd95SBruce Richardson port;
28599a2dd95SBruce Richardson
28699a2dd95SBruce Richardson if (port == NULL) {
287*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Parameter port is NULL", __func__);
28899a2dd95SBruce Richardson return -1;
28999a2dd95SBruce Richardson }
29099a2dd95SBruce Richardson
29199a2dd95SBruce Richardson rte_port_ring_writer_ras_flush(port);
29299a2dd95SBruce Richardson rte_ip_frag_table_destroy(p->frag_tbl);
29399a2dd95SBruce Richardson rte_free(port);
29499a2dd95SBruce Richardson
29599a2dd95SBruce Richardson return 0;
29699a2dd95SBruce Richardson }
29799a2dd95SBruce Richardson
29899a2dd95SBruce Richardson static int
rte_port_ras_writer_stats_read(void * port,struct rte_port_out_stats * stats,int clear)29999a2dd95SBruce Richardson rte_port_ras_writer_stats_read(void *port,
30099a2dd95SBruce Richardson struct rte_port_out_stats *stats, int clear)
30199a2dd95SBruce Richardson {
30299a2dd95SBruce Richardson struct rte_port_ring_writer_ras *p =
30399a2dd95SBruce Richardson port;
30499a2dd95SBruce Richardson
30599a2dd95SBruce Richardson if (stats != NULL)
30699a2dd95SBruce Richardson memcpy(stats, &p->stats, sizeof(p->stats));
30799a2dd95SBruce Richardson
30899a2dd95SBruce Richardson if (clear)
30999a2dd95SBruce Richardson memset(&p->stats, 0, sizeof(p->stats));
31099a2dd95SBruce Richardson
31199a2dd95SBruce Richardson return 0;
31299a2dd95SBruce Richardson }
31399a2dd95SBruce Richardson
31499a2dd95SBruce Richardson /*
31599a2dd95SBruce Richardson * Summary of port operations
31699a2dd95SBruce Richardson */
31799a2dd95SBruce Richardson struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops = {
31899a2dd95SBruce Richardson .f_create = rte_port_ring_writer_ipv4_ras_create,
31999a2dd95SBruce Richardson .f_free = rte_port_ring_writer_ras_free,
32099a2dd95SBruce Richardson .f_tx = rte_port_ring_writer_ras_tx,
32199a2dd95SBruce Richardson .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
32299a2dd95SBruce Richardson .f_flush = rte_port_ring_writer_ras_flush,
32399a2dd95SBruce Richardson .f_stats = rte_port_ras_writer_stats_read,
32499a2dd95SBruce Richardson };
32599a2dd95SBruce Richardson
32699a2dd95SBruce Richardson struct rte_port_out_ops rte_port_ring_writer_ipv6_ras_ops = {
32799a2dd95SBruce Richardson .f_create = rte_port_ring_writer_ipv6_ras_create,
32899a2dd95SBruce Richardson .f_free = rte_port_ring_writer_ras_free,
32999a2dd95SBruce Richardson .f_tx = rte_port_ring_writer_ras_tx,
33099a2dd95SBruce Richardson .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
33199a2dd95SBruce Richardson .f_flush = rte_port_ring_writer_ras_flush,
33299a2dd95SBruce Richardson .f_stats = rte_port_ras_writer_stats_read,
33399a2dd95SBruce Richardson };
334