199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2018 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson #include <string.h>
599a2dd95SBruce Richardson
699a2dd95SBruce Richardson #include <rte_common.h>
799a2dd95SBruce Richardson #include <rte_malloc.h>
899a2dd95SBruce Richardson
999a2dd95SBruce Richardson #include "rte_port_sym_crypto.h"
1099a2dd95SBruce Richardson
11*ae67895bSDavid Marchand #include "port_log.h"
12*ae67895bSDavid Marchand
1399a2dd95SBruce Richardson /*
1499a2dd95SBruce Richardson * Port Crypto Reader
1599a2dd95SBruce Richardson */
1699a2dd95SBruce Richardson #ifdef RTE_PORT_STATS_COLLECT
1799a2dd95SBruce Richardson
1899a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_READER_STATS_PKTS_IN_ADD(port, val) \
1999a2dd95SBruce Richardson (port)->stats.n_pkts_in += (val)
2099a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_READER_STATS_PKTS_DROP_ADD(port, val) \
2199a2dd95SBruce Richardson (port)->stats.n_pkts_drop += (val)
2299a2dd95SBruce Richardson
2399a2dd95SBruce Richardson #else
2499a2dd95SBruce Richardson
2599a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_READER_STATS_PKTS_IN_ADD(port, val)
2699a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_READER_STATS_PKTS_DROP_ADD(port, val)
2799a2dd95SBruce Richardson
2899a2dd95SBruce Richardson #endif
2999a2dd95SBruce Richardson
3099a2dd95SBruce Richardson struct rte_port_sym_crypto_reader {
3199a2dd95SBruce Richardson struct rte_port_in_stats stats;
3299a2dd95SBruce Richardson
3399a2dd95SBruce Richardson uint8_t cryptodev_id;
3499a2dd95SBruce Richardson uint16_t queue_id;
3599a2dd95SBruce Richardson struct rte_crypto_op *ops[RTE_PORT_IN_BURST_SIZE_MAX];
3699a2dd95SBruce Richardson rte_port_sym_crypto_reader_callback_fn f_callback;
3799a2dd95SBruce Richardson void *arg_callback;
3899a2dd95SBruce Richardson };
3999a2dd95SBruce Richardson
4099a2dd95SBruce Richardson static void *
rte_port_sym_crypto_reader_create(void * params,int socket_id)4199a2dd95SBruce Richardson rte_port_sym_crypto_reader_create(void *params, int socket_id)
4299a2dd95SBruce Richardson {
4399a2dd95SBruce Richardson struct rte_port_sym_crypto_reader_params *conf =
4499a2dd95SBruce Richardson params;
4599a2dd95SBruce Richardson struct rte_port_sym_crypto_reader *port;
4699a2dd95SBruce Richardson
4799a2dd95SBruce Richardson /* Check input parameters */
4899a2dd95SBruce Richardson if (conf == NULL) {
49*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: params is NULL", __func__);
5099a2dd95SBruce Richardson return NULL;
5199a2dd95SBruce Richardson }
5299a2dd95SBruce Richardson
5399a2dd95SBruce Richardson /* Memory allocation */
5499a2dd95SBruce Richardson port = rte_zmalloc_socket("PORT", sizeof(*port),
5599a2dd95SBruce Richardson RTE_CACHE_LINE_SIZE, socket_id);
5699a2dd95SBruce Richardson if (port == NULL) {
57*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Failed to allocate port", __func__);
5899a2dd95SBruce Richardson return NULL;
5999a2dd95SBruce Richardson }
6099a2dd95SBruce Richardson
6199a2dd95SBruce Richardson /* Initialization */
6299a2dd95SBruce Richardson port->cryptodev_id = conf->cryptodev_id;
6399a2dd95SBruce Richardson port->queue_id = conf->queue_id;
6499a2dd95SBruce Richardson port->f_callback = conf->f_callback;
6599a2dd95SBruce Richardson port->arg_callback = conf->arg_callback;
6699a2dd95SBruce Richardson
6799a2dd95SBruce Richardson return port;
6899a2dd95SBruce Richardson }
6999a2dd95SBruce Richardson
7099a2dd95SBruce Richardson static int
rte_port_sym_crypto_reader_rx(void * port,struct rte_mbuf ** pkts,uint32_t n_pkts)7199a2dd95SBruce Richardson rte_port_sym_crypto_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
7299a2dd95SBruce Richardson {
7399a2dd95SBruce Richardson struct rte_port_sym_crypto_reader *p =
7499a2dd95SBruce Richardson port;
7599a2dd95SBruce Richardson uint16_t rx_ops_cnt, i, n = 0;
7699a2dd95SBruce Richardson
7799a2dd95SBruce Richardson rx_ops_cnt = rte_cryptodev_dequeue_burst(p->cryptodev_id, p->queue_id,
7899a2dd95SBruce Richardson p->ops, n_pkts);
7999a2dd95SBruce Richardson
8099a2dd95SBruce Richardson for (i = 0; i < rx_ops_cnt; i++) {
8199a2dd95SBruce Richardson struct rte_crypto_op *op = p->ops[i];
8299a2dd95SBruce Richardson
8399a2dd95SBruce Richardson /** Drop failed pkts */
8499a2dd95SBruce Richardson if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)) {
8599a2dd95SBruce Richardson rte_pktmbuf_free(op->sym->m_src);
8699a2dd95SBruce Richardson continue;
8799a2dd95SBruce Richardson }
8899a2dd95SBruce Richardson
8999a2dd95SBruce Richardson pkts[n++] = op->sym->m_src;
9099a2dd95SBruce Richardson }
9199a2dd95SBruce Richardson
9299a2dd95SBruce Richardson if (p->f_callback)
9399a2dd95SBruce Richardson (*p->f_callback)(pkts, n, p->arg_callback);
9499a2dd95SBruce Richardson
9599a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_READER_STATS_PKTS_IN_ADD(p, n);
9699a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_READER_STATS_PKTS_DROP_ADD(p, rx_ops_cnt - n);
9799a2dd95SBruce Richardson
9899a2dd95SBruce Richardson return n;
9999a2dd95SBruce Richardson }
10099a2dd95SBruce Richardson
10199a2dd95SBruce Richardson static int
rte_port_sym_crypto_reader_free(void * port)10299a2dd95SBruce Richardson rte_port_sym_crypto_reader_free(void *port)
10399a2dd95SBruce Richardson {
10499a2dd95SBruce Richardson if (port == NULL) {
105*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: port is NULL", __func__);
10699a2dd95SBruce Richardson return -EINVAL;
10799a2dd95SBruce Richardson }
10899a2dd95SBruce Richardson
10999a2dd95SBruce Richardson rte_free(port);
11099a2dd95SBruce Richardson
11199a2dd95SBruce Richardson return 0;
11299a2dd95SBruce Richardson }
11399a2dd95SBruce Richardson
rte_port_sym_crypto_reader_stats_read(void * port,struct rte_port_in_stats * stats,int clear)11499a2dd95SBruce Richardson static int rte_port_sym_crypto_reader_stats_read(void *port,
11599a2dd95SBruce Richardson struct rte_port_in_stats *stats, int clear)
11699a2dd95SBruce Richardson {
11799a2dd95SBruce Richardson struct rte_port_sym_crypto_reader *p =
11899a2dd95SBruce Richardson port;
11999a2dd95SBruce Richardson
12099a2dd95SBruce Richardson if (stats != NULL)
12199a2dd95SBruce Richardson memcpy(stats, &p->stats, sizeof(p->stats));
12299a2dd95SBruce Richardson
12399a2dd95SBruce Richardson if (clear)
12499a2dd95SBruce Richardson memset(&p->stats, 0, sizeof(p->stats));
12599a2dd95SBruce Richardson
12699a2dd95SBruce Richardson return 0;
12799a2dd95SBruce Richardson }
12899a2dd95SBruce Richardson
12999a2dd95SBruce Richardson /*
13099a2dd95SBruce Richardson * Port crypto Writer
13199a2dd95SBruce Richardson */
13299a2dd95SBruce Richardson #ifdef RTE_PORT_STATS_COLLECT
13399a2dd95SBruce Richardson
13499a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_IN_ADD(port, val) \
13599a2dd95SBruce Richardson (port)->stats.n_pkts_in += (val)
13699a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_DROP_ADD(port, val) \
13799a2dd95SBruce Richardson (port)->stats.n_pkts_drop += (val)
13899a2dd95SBruce Richardson
13999a2dd95SBruce Richardson #else
14099a2dd95SBruce Richardson
14199a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_IN_ADD(port, val)
14299a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_DROP_ADD(port, val)
14399a2dd95SBruce Richardson
14499a2dd95SBruce Richardson #endif
14599a2dd95SBruce Richardson
14699a2dd95SBruce Richardson struct rte_port_sym_crypto_writer {
14799a2dd95SBruce Richardson struct rte_port_out_stats stats;
14899a2dd95SBruce Richardson
14999a2dd95SBruce Richardson struct rte_crypto_op *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
15099a2dd95SBruce Richardson
15199a2dd95SBruce Richardson uint32_t tx_burst_sz;
15299a2dd95SBruce Richardson uint32_t tx_buf_count;
15399a2dd95SBruce Richardson uint64_t bsz_mask;
15499a2dd95SBruce Richardson
15599a2dd95SBruce Richardson uint8_t cryptodev_id;
15699a2dd95SBruce Richardson uint16_t queue_id;
15799a2dd95SBruce Richardson uint16_t crypto_op_offset;
15899a2dd95SBruce Richardson };
15999a2dd95SBruce Richardson
16099a2dd95SBruce Richardson static void *
rte_port_sym_crypto_writer_create(void * params,int socket_id)16199a2dd95SBruce Richardson rte_port_sym_crypto_writer_create(void *params, int socket_id)
16299a2dd95SBruce Richardson {
16399a2dd95SBruce Richardson struct rte_port_sym_crypto_writer_params *conf =
16499a2dd95SBruce Richardson params;
16599a2dd95SBruce Richardson struct rte_port_sym_crypto_writer *port;
16699a2dd95SBruce Richardson
16799a2dd95SBruce Richardson /* Check input parameters */
16899a2dd95SBruce Richardson if ((conf == NULL) ||
16999a2dd95SBruce Richardson (conf->tx_burst_sz == 0) ||
17099a2dd95SBruce Richardson (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
17199a2dd95SBruce Richardson (!rte_is_power_of_2(conf->tx_burst_sz))) {
172*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Invalid input parameters", __func__);
17399a2dd95SBruce Richardson return NULL;
17499a2dd95SBruce Richardson }
17599a2dd95SBruce Richardson
17699a2dd95SBruce Richardson /* Memory allocation */
17799a2dd95SBruce Richardson port = rte_zmalloc_socket("PORT", sizeof(*port),
17899a2dd95SBruce Richardson RTE_CACHE_LINE_SIZE, socket_id);
17999a2dd95SBruce Richardson if (port == NULL) {
180*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Failed to allocate port", __func__);
18199a2dd95SBruce Richardson return NULL;
18299a2dd95SBruce Richardson }
18399a2dd95SBruce Richardson
18499a2dd95SBruce Richardson /* Initialization */
18599a2dd95SBruce Richardson port->tx_burst_sz = conf->tx_burst_sz;
18699a2dd95SBruce Richardson port->tx_buf_count = 0;
18799a2dd95SBruce Richardson port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
18899a2dd95SBruce Richardson
18999a2dd95SBruce Richardson port->cryptodev_id = conf->cryptodev_id;
19099a2dd95SBruce Richardson port->queue_id = conf->queue_id;
19199a2dd95SBruce Richardson port->crypto_op_offset = conf->crypto_op_offset;
19299a2dd95SBruce Richardson
19399a2dd95SBruce Richardson return port;
19499a2dd95SBruce Richardson }
19599a2dd95SBruce Richardson
19699a2dd95SBruce Richardson static inline void
send_burst(struct rte_port_sym_crypto_writer * p)19799a2dd95SBruce Richardson send_burst(struct rte_port_sym_crypto_writer *p)
19899a2dd95SBruce Richardson {
19999a2dd95SBruce Richardson uint32_t nb_tx;
20099a2dd95SBruce Richardson
20199a2dd95SBruce Richardson nb_tx = rte_cryptodev_enqueue_burst(p->cryptodev_id, p->queue_id,
20299a2dd95SBruce Richardson p->tx_buf, p->tx_buf_count);
20399a2dd95SBruce Richardson
20499a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count -
20599a2dd95SBruce Richardson nb_tx);
20699a2dd95SBruce Richardson for (; nb_tx < p->tx_buf_count; nb_tx++)
20799a2dd95SBruce Richardson rte_pktmbuf_free(p->tx_buf[nb_tx]->sym->m_src);
20899a2dd95SBruce Richardson
20999a2dd95SBruce Richardson p->tx_buf_count = 0;
21099a2dd95SBruce Richardson }
21199a2dd95SBruce Richardson
21299a2dd95SBruce Richardson static int
rte_port_sym_crypto_writer_tx(void * port,struct rte_mbuf * pkt)21399a2dd95SBruce Richardson rte_port_sym_crypto_writer_tx(void *port, struct rte_mbuf *pkt)
21499a2dd95SBruce Richardson {
21599a2dd95SBruce Richardson struct rte_port_sym_crypto_writer *p =
21699a2dd95SBruce Richardson port;
21799a2dd95SBruce Richardson
21899a2dd95SBruce Richardson p->tx_buf[p->tx_buf_count++] = (struct rte_crypto_op *)
21999a2dd95SBruce Richardson RTE_MBUF_METADATA_UINT8_PTR(pkt, p->crypto_op_offset);
22099a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_IN_ADD(p, 1);
22199a2dd95SBruce Richardson if (p->tx_buf_count >= p->tx_burst_sz)
22299a2dd95SBruce Richardson send_burst(p);
22399a2dd95SBruce Richardson
22499a2dd95SBruce Richardson return 0;
22599a2dd95SBruce Richardson }
22699a2dd95SBruce Richardson
22799a2dd95SBruce Richardson static int
rte_port_sym_crypto_writer_tx_bulk(void * port,struct rte_mbuf ** pkts,uint64_t pkts_mask)22899a2dd95SBruce Richardson rte_port_sym_crypto_writer_tx_bulk(void *port,
22999a2dd95SBruce Richardson struct rte_mbuf **pkts,
23099a2dd95SBruce Richardson uint64_t pkts_mask)
23199a2dd95SBruce Richardson {
23299a2dd95SBruce Richardson struct rte_port_sym_crypto_writer *p =
23399a2dd95SBruce Richardson port;
23499a2dd95SBruce Richardson uint64_t bsz_mask = p->bsz_mask;
23599a2dd95SBruce Richardson uint32_t tx_buf_count = p->tx_buf_count;
23699a2dd95SBruce Richardson uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
23799a2dd95SBruce Richardson ((pkts_mask & bsz_mask) ^ bsz_mask);
23899a2dd95SBruce Richardson
23999a2dd95SBruce Richardson if (expr == 0) {
2403d4e27fdSDavid Marchand uint64_t n_pkts = rte_popcount64(pkts_mask);
24199a2dd95SBruce Richardson uint32_t i;
24299a2dd95SBruce Richardson
24399a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_IN_ADD(p, n_pkts);
24499a2dd95SBruce Richardson
24599a2dd95SBruce Richardson for (i = 0; i < n_pkts; i++)
24699a2dd95SBruce Richardson p->tx_buf[p->tx_buf_count++] = (struct rte_crypto_op *)
24799a2dd95SBruce Richardson RTE_MBUF_METADATA_UINT8_PTR(pkts[i],
24899a2dd95SBruce Richardson p->crypto_op_offset);
24999a2dd95SBruce Richardson
25099a2dd95SBruce Richardson if (p->tx_buf_count >= p->tx_burst_sz)
25199a2dd95SBruce Richardson send_burst(p);
25299a2dd95SBruce Richardson } else {
25399a2dd95SBruce Richardson for (; pkts_mask;) {
2543d4e27fdSDavid Marchand uint32_t pkt_index = rte_ctz64(pkts_mask);
25599a2dd95SBruce Richardson uint64_t pkt_mask = 1LLU << pkt_index;
25699a2dd95SBruce Richardson struct rte_mbuf *pkt = pkts[pkt_index];
25799a2dd95SBruce Richardson
25899a2dd95SBruce Richardson p->tx_buf[tx_buf_count++] = (struct rte_crypto_op *)
25999a2dd95SBruce Richardson RTE_MBUF_METADATA_UINT8_PTR(pkt,
26099a2dd95SBruce Richardson p->crypto_op_offset);
26199a2dd95SBruce Richardson
26299a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_IN_ADD(p, 1);
26399a2dd95SBruce Richardson pkts_mask &= ~pkt_mask;
26499a2dd95SBruce Richardson }
26599a2dd95SBruce Richardson
26699a2dd95SBruce Richardson p->tx_buf_count = tx_buf_count;
26799a2dd95SBruce Richardson if (tx_buf_count >= p->tx_burst_sz)
26899a2dd95SBruce Richardson send_burst(p);
26999a2dd95SBruce Richardson }
27099a2dd95SBruce Richardson
27199a2dd95SBruce Richardson return 0;
27299a2dd95SBruce Richardson }
27399a2dd95SBruce Richardson
27499a2dd95SBruce Richardson static int
rte_port_sym_crypto_writer_flush(void * port)27599a2dd95SBruce Richardson rte_port_sym_crypto_writer_flush(void *port)
27699a2dd95SBruce Richardson {
27799a2dd95SBruce Richardson struct rte_port_sym_crypto_writer *p =
27899a2dd95SBruce Richardson port;
27999a2dd95SBruce Richardson
28099a2dd95SBruce Richardson if (p->tx_buf_count > 0)
28199a2dd95SBruce Richardson send_burst(p);
28299a2dd95SBruce Richardson
28399a2dd95SBruce Richardson return 0;
28499a2dd95SBruce Richardson }
28599a2dd95SBruce Richardson
28699a2dd95SBruce Richardson static int
rte_port_sym_crypto_writer_free(void * port)28799a2dd95SBruce Richardson rte_port_sym_crypto_writer_free(void *port)
28899a2dd95SBruce Richardson {
28999a2dd95SBruce Richardson if (port == NULL) {
290*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Port is NULL", __func__);
29199a2dd95SBruce Richardson return -EINVAL;
29299a2dd95SBruce Richardson }
29399a2dd95SBruce Richardson
29499a2dd95SBruce Richardson rte_port_sym_crypto_writer_flush(port);
29599a2dd95SBruce Richardson rte_free(port);
29699a2dd95SBruce Richardson
29799a2dd95SBruce Richardson return 0;
29899a2dd95SBruce Richardson }
29999a2dd95SBruce Richardson
rte_port_sym_crypto_writer_stats_read(void * port,struct rte_port_out_stats * stats,int clear)30099a2dd95SBruce Richardson static int rte_port_sym_crypto_writer_stats_read(void *port,
30199a2dd95SBruce Richardson struct rte_port_out_stats *stats, int clear)
30299a2dd95SBruce Richardson {
30399a2dd95SBruce Richardson struct rte_port_sym_crypto_writer *p =
30499a2dd95SBruce Richardson port;
30599a2dd95SBruce Richardson
30699a2dd95SBruce Richardson if (stats != NULL)
30799a2dd95SBruce Richardson memcpy(stats, &p->stats, sizeof(p->stats));
30899a2dd95SBruce Richardson
30999a2dd95SBruce Richardson if (clear)
31099a2dd95SBruce Richardson memset(&p->stats, 0, sizeof(p->stats));
31199a2dd95SBruce Richardson
31299a2dd95SBruce Richardson return 0;
31399a2dd95SBruce Richardson }
31499a2dd95SBruce Richardson
31599a2dd95SBruce Richardson /*
31699a2dd95SBruce Richardson * Port crypto Writer Nodrop
31799a2dd95SBruce Richardson */
31899a2dd95SBruce Richardson #ifdef RTE_PORT_STATS_COLLECT
31999a2dd95SBruce Richardson
32099a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val) \
32199a2dd95SBruce Richardson (port)->stats.n_pkts_in += (val)
32299a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val) \
32399a2dd95SBruce Richardson (port)->stats.n_pkts_drop += (val)
32499a2dd95SBruce Richardson
32599a2dd95SBruce Richardson #else
32699a2dd95SBruce Richardson
32799a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val)
32899a2dd95SBruce Richardson #define RTE_PORT_SYM_CRYPTO_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val)
32999a2dd95SBruce Richardson
33099a2dd95SBruce Richardson #endif
33199a2dd95SBruce Richardson
33299a2dd95SBruce Richardson struct rte_port_sym_crypto_writer_nodrop {
33399a2dd95SBruce Richardson struct rte_port_out_stats stats;
33499a2dd95SBruce Richardson
33599a2dd95SBruce Richardson struct rte_crypto_op *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
33699a2dd95SBruce Richardson uint32_t tx_burst_sz;
33799a2dd95SBruce Richardson uint32_t tx_buf_count;
33899a2dd95SBruce Richardson uint64_t bsz_mask;
33999a2dd95SBruce Richardson uint64_t n_retries;
34099a2dd95SBruce Richardson
34199a2dd95SBruce Richardson uint8_t cryptodev_id;
34299a2dd95SBruce Richardson uint16_t queue_id;
34399a2dd95SBruce Richardson uint16_t crypto_op_offset;
34499a2dd95SBruce Richardson };
34599a2dd95SBruce Richardson
34699a2dd95SBruce Richardson static void *
rte_port_sym_crypto_writer_nodrop_create(void * params,int socket_id)34799a2dd95SBruce Richardson rte_port_sym_crypto_writer_nodrop_create(void *params, int socket_id)
34899a2dd95SBruce Richardson {
34999a2dd95SBruce Richardson struct rte_port_sym_crypto_writer_nodrop_params *conf =
35099a2dd95SBruce Richardson params;
35199a2dd95SBruce Richardson struct rte_port_sym_crypto_writer_nodrop *port;
35299a2dd95SBruce Richardson
35399a2dd95SBruce Richardson /* Check input parameters */
35499a2dd95SBruce Richardson if ((conf == NULL) ||
35599a2dd95SBruce Richardson (conf->tx_burst_sz == 0) ||
35699a2dd95SBruce Richardson (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
35799a2dd95SBruce Richardson (!rte_is_power_of_2(conf->tx_burst_sz))) {
358*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Invalid input parameters", __func__);
35999a2dd95SBruce Richardson return NULL;
36099a2dd95SBruce Richardson }
36199a2dd95SBruce Richardson
36299a2dd95SBruce Richardson /* Memory allocation */
36399a2dd95SBruce Richardson port = rte_zmalloc_socket("PORT", sizeof(*port),
36499a2dd95SBruce Richardson RTE_CACHE_LINE_SIZE, socket_id);
36599a2dd95SBruce Richardson if (port == NULL) {
366*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Failed to allocate port", __func__);
36799a2dd95SBruce Richardson return NULL;
36899a2dd95SBruce Richardson }
36999a2dd95SBruce Richardson
37099a2dd95SBruce Richardson /* Initialization */
37199a2dd95SBruce Richardson port->cryptodev_id = conf->cryptodev_id;
37299a2dd95SBruce Richardson port->queue_id = conf->queue_id;
37399a2dd95SBruce Richardson port->crypto_op_offset = conf->crypto_op_offset;
37499a2dd95SBruce Richardson port->tx_burst_sz = conf->tx_burst_sz;
37599a2dd95SBruce Richardson port->tx_buf_count = 0;
37699a2dd95SBruce Richardson port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
37799a2dd95SBruce Richardson
37899a2dd95SBruce Richardson /*
37999a2dd95SBruce Richardson * When n_retries is 0 it means that we should wait for every packet to
38099a2dd95SBruce Richardson * send no matter how many retries should it take. To limit number of
38199a2dd95SBruce Richardson * branches in fast path, we use UINT64_MAX instead of branching.
38299a2dd95SBruce Richardson */
38399a2dd95SBruce Richardson port->n_retries = (conf->n_retries == 0) ? UINT64_MAX : conf->n_retries;
38499a2dd95SBruce Richardson
38599a2dd95SBruce Richardson return port;
38699a2dd95SBruce Richardson }
38799a2dd95SBruce Richardson
38899a2dd95SBruce Richardson static inline void
send_burst_nodrop(struct rte_port_sym_crypto_writer_nodrop * p)38999a2dd95SBruce Richardson send_burst_nodrop(struct rte_port_sym_crypto_writer_nodrop *p)
39099a2dd95SBruce Richardson {
39199a2dd95SBruce Richardson uint32_t nb_tx = 0, i;
39299a2dd95SBruce Richardson
39399a2dd95SBruce Richardson nb_tx = rte_cryptodev_enqueue_burst(p->cryptodev_id, p->queue_id,
39499a2dd95SBruce Richardson p->tx_buf, p->tx_buf_count);
39599a2dd95SBruce Richardson
39699a2dd95SBruce Richardson /* We sent all the packets in a first try */
39799a2dd95SBruce Richardson if (nb_tx >= p->tx_buf_count) {
39899a2dd95SBruce Richardson p->tx_buf_count = 0;
39999a2dd95SBruce Richardson return;
40099a2dd95SBruce Richardson }
40199a2dd95SBruce Richardson
40299a2dd95SBruce Richardson for (i = 0; i < p->n_retries; i++) {
40399a2dd95SBruce Richardson nb_tx += rte_cryptodev_enqueue_burst(p->cryptodev_id,
40499a2dd95SBruce Richardson p->queue_id, p->tx_buf + nb_tx,
40599a2dd95SBruce Richardson p->tx_buf_count - nb_tx);
40699a2dd95SBruce Richardson
40799a2dd95SBruce Richardson /* We sent all the packets in more than one try */
40899a2dd95SBruce Richardson if (nb_tx >= p->tx_buf_count) {
40999a2dd95SBruce Richardson p->tx_buf_count = 0;
41099a2dd95SBruce Richardson return;
41199a2dd95SBruce Richardson }
41299a2dd95SBruce Richardson }
41399a2dd95SBruce Richardson
41499a2dd95SBruce Richardson /* We didn't send the packets in maximum allowed attempts */
41599a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_WRITER_NODROP_STATS_PKTS_DROP_ADD(p,
41699a2dd95SBruce Richardson p->tx_buf_count - nb_tx);
41799a2dd95SBruce Richardson for ( ; nb_tx < p->tx_buf_count; nb_tx++)
41899a2dd95SBruce Richardson rte_pktmbuf_free(p->tx_buf[nb_tx]->sym->m_src);
41999a2dd95SBruce Richardson
42099a2dd95SBruce Richardson p->tx_buf_count = 0;
42199a2dd95SBruce Richardson }
42299a2dd95SBruce Richardson
42399a2dd95SBruce Richardson static int
rte_port_sym_crypto_writer_nodrop_tx(void * port,struct rte_mbuf * pkt)42499a2dd95SBruce Richardson rte_port_sym_crypto_writer_nodrop_tx(void *port, struct rte_mbuf *pkt)
42599a2dd95SBruce Richardson {
42699a2dd95SBruce Richardson struct rte_port_sym_crypto_writer_nodrop *p =
42799a2dd95SBruce Richardson port;
42899a2dd95SBruce Richardson
42999a2dd95SBruce Richardson p->tx_buf[p->tx_buf_count++] = (struct rte_crypto_op *)
43099a2dd95SBruce Richardson RTE_MBUF_METADATA_UINT8_PTR(pkt, p->crypto_op_offset);
43199a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_WRITER_STATS_PKTS_IN_ADD(p, 1);
43299a2dd95SBruce Richardson if (p->tx_buf_count >= p->tx_burst_sz)
43399a2dd95SBruce Richardson send_burst_nodrop(p);
43499a2dd95SBruce Richardson
43599a2dd95SBruce Richardson return 0;
43699a2dd95SBruce Richardson }
43799a2dd95SBruce Richardson
43899a2dd95SBruce Richardson static int
rte_port_sym_crypto_writer_nodrop_tx_bulk(void * port,struct rte_mbuf ** pkts,uint64_t pkts_mask)43999a2dd95SBruce Richardson rte_port_sym_crypto_writer_nodrop_tx_bulk(void *port,
44099a2dd95SBruce Richardson struct rte_mbuf **pkts,
44199a2dd95SBruce Richardson uint64_t pkts_mask)
44299a2dd95SBruce Richardson {
44399a2dd95SBruce Richardson struct rte_port_sym_crypto_writer_nodrop *p =
44499a2dd95SBruce Richardson port;
44599a2dd95SBruce Richardson
44699a2dd95SBruce Richardson uint64_t bsz_mask = p->bsz_mask;
44799a2dd95SBruce Richardson uint32_t tx_buf_count = p->tx_buf_count;
44899a2dd95SBruce Richardson uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
44999a2dd95SBruce Richardson ((pkts_mask & bsz_mask) ^ bsz_mask);
45099a2dd95SBruce Richardson
45199a2dd95SBruce Richardson if (expr == 0) {
4523d4e27fdSDavid Marchand uint64_t n_pkts = rte_popcount64(pkts_mask);
45399a2dd95SBruce Richardson uint32_t i;
45499a2dd95SBruce Richardson
45599a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_WRITER_NODROP_STATS_PKTS_IN_ADD(p, n_pkts);
45699a2dd95SBruce Richardson
45799a2dd95SBruce Richardson for (i = 0; i < n_pkts; i++)
45899a2dd95SBruce Richardson p->tx_buf[p->tx_buf_count++] = (struct rte_crypto_op *)
45999a2dd95SBruce Richardson RTE_MBUF_METADATA_UINT8_PTR(pkts[i],
46099a2dd95SBruce Richardson p->crypto_op_offset);
46199a2dd95SBruce Richardson
46299a2dd95SBruce Richardson if (p->tx_buf_count >= p->tx_burst_sz)
46399a2dd95SBruce Richardson send_burst_nodrop(p);
46499a2dd95SBruce Richardson } else {
46599a2dd95SBruce Richardson for ( ; pkts_mask; ) {
4663d4e27fdSDavid Marchand uint32_t pkt_index = rte_ctz64(pkts_mask);
46799a2dd95SBruce Richardson uint64_t pkt_mask = 1LLU << pkt_index;
46899a2dd95SBruce Richardson struct rte_mbuf *pkt = pkts[pkt_index];
46999a2dd95SBruce Richardson
47099a2dd95SBruce Richardson p->tx_buf[tx_buf_count++] = (struct rte_crypto_op *)
47199a2dd95SBruce Richardson RTE_MBUF_METADATA_UINT8_PTR(pkt,
47299a2dd95SBruce Richardson p->crypto_op_offset);
47399a2dd95SBruce Richardson RTE_PORT_SYM_CRYPTO_WRITER_NODROP_STATS_PKTS_IN_ADD(p,
47499a2dd95SBruce Richardson 1);
47599a2dd95SBruce Richardson pkts_mask &= ~pkt_mask;
47699a2dd95SBruce Richardson }
47799a2dd95SBruce Richardson
47899a2dd95SBruce Richardson p->tx_buf_count = tx_buf_count;
47999a2dd95SBruce Richardson if (tx_buf_count >= p->tx_burst_sz)
48099a2dd95SBruce Richardson send_burst_nodrop(p);
48199a2dd95SBruce Richardson }
48299a2dd95SBruce Richardson
48399a2dd95SBruce Richardson return 0;
48499a2dd95SBruce Richardson }
48599a2dd95SBruce Richardson
48699a2dd95SBruce Richardson static int
rte_port_sym_crypto_writer_nodrop_flush(void * port)48799a2dd95SBruce Richardson rte_port_sym_crypto_writer_nodrop_flush(void *port)
48899a2dd95SBruce Richardson {
48999a2dd95SBruce Richardson struct rte_port_sym_crypto_writer_nodrop *p =
49099a2dd95SBruce Richardson port;
49199a2dd95SBruce Richardson
49299a2dd95SBruce Richardson if (p->tx_buf_count > 0)
49399a2dd95SBruce Richardson send_burst_nodrop(p);
49499a2dd95SBruce Richardson
49599a2dd95SBruce Richardson return 0;
49699a2dd95SBruce Richardson }
49799a2dd95SBruce Richardson
49899a2dd95SBruce Richardson static int
rte_port_sym_crypto_writer_nodrop_free(void * port)49999a2dd95SBruce Richardson rte_port_sym_crypto_writer_nodrop_free(void *port)
50099a2dd95SBruce Richardson {
50199a2dd95SBruce Richardson if (port == NULL) {
502*ae67895bSDavid Marchand PORT_LOG(ERR, "%s: Port is NULL", __func__);
50399a2dd95SBruce Richardson return -EINVAL;
50499a2dd95SBruce Richardson }
50599a2dd95SBruce Richardson
50699a2dd95SBruce Richardson rte_port_sym_crypto_writer_nodrop_flush(port);
50799a2dd95SBruce Richardson rte_free(port);
50899a2dd95SBruce Richardson
50999a2dd95SBruce Richardson return 0;
51099a2dd95SBruce Richardson }
51199a2dd95SBruce Richardson
rte_port_sym_crypto_writer_nodrop_stats_read(void * port,struct rte_port_out_stats * stats,int clear)51299a2dd95SBruce Richardson static int rte_port_sym_crypto_writer_nodrop_stats_read(void *port,
51399a2dd95SBruce Richardson struct rte_port_out_stats *stats, int clear)
51499a2dd95SBruce Richardson {
51599a2dd95SBruce Richardson struct rte_port_sym_crypto_writer_nodrop *p =
51699a2dd95SBruce Richardson port;
51799a2dd95SBruce Richardson
51899a2dd95SBruce Richardson if (stats != NULL)
51999a2dd95SBruce Richardson memcpy(stats, &p->stats, sizeof(p->stats));
52099a2dd95SBruce Richardson
52199a2dd95SBruce Richardson if (clear)
52299a2dd95SBruce Richardson memset(&p->stats, 0, sizeof(p->stats));
52399a2dd95SBruce Richardson
52499a2dd95SBruce Richardson return 0;
52599a2dd95SBruce Richardson }
52699a2dd95SBruce Richardson
52799a2dd95SBruce Richardson
52899a2dd95SBruce Richardson /*
52999a2dd95SBruce Richardson * Summary of port operations
53099a2dd95SBruce Richardson */
53199a2dd95SBruce Richardson struct rte_port_in_ops rte_port_sym_crypto_reader_ops = {
53299a2dd95SBruce Richardson .f_create = rte_port_sym_crypto_reader_create,
53399a2dd95SBruce Richardson .f_free = rte_port_sym_crypto_reader_free,
53499a2dd95SBruce Richardson .f_rx = rte_port_sym_crypto_reader_rx,
53599a2dd95SBruce Richardson .f_stats = rte_port_sym_crypto_reader_stats_read,
53699a2dd95SBruce Richardson };
53799a2dd95SBruce Richardson
53899a2dd95SBruce Richardson struct rte_port_out_ops rte_port_sym_crypto_writer_ops = {
53999a2dd95SBruce Richardson .f_create = rte_port_sym_crypto_writer_create,
54099a2dd95SBruce Richardson .f_free = rte_port_sym_crypto_writer_free,
54199a2dd95SBruce Richardson .f_tx = rte_port_sym_crypto_writer_tx,
54299a2dd95SBruce Richardson .f_tx_bulk = rte_port_sym_crypto_writer_tx_bulk,
54399a2dd95SBruce Richardson .f_flush = rte_port_sym_crypto_writer_flush,
54499a2dd95SBruce Richardson .f_stats = rte_port_sym_crypto_writer_stats_read,
54599a2dd95SBruce Richardson };
54699a2dd95SBruce Richardson
54799a2dd95SBruce Richardson struct rte_port_out_ops rte_port_sym_crypto_writer_nodrop_ops = {
54899a2dd95SBruce Richardson .f_create = rte_port_sym_crypto_writer_nodrop_create,
54999a2dd95SBruce Richardson .f_free = rte_port_sym_crypto_writer_nodrop_free,
55099a2dd95SBruce Richardson .f_tx = rte_port_sym_crypto_writer_nodrop_tx,
55199a2dd95SBruce Richardson .f_tx_bulk = rte_port_sym_crypto_writer_nodrop_tx_bulk,
55299a2dd95SBruce Richardson .f_flush = rte_port_sym_crypto_writer_nodrop_flush,
55399a2dd95SBruce Richardson .f_stats = rte_port_sym_crypto_writer_nodrop_stats_read,
55499a2dd95SBruce Richardson };
555