[PATCH v24 08/24] net/pcap: replace stack bounce buffer

Stephen Hemminger
2026-04-16T18:48:22Z

Replace the 64K stack-allocated bounce buffer with a per-queue
buffer allocated from hugepages via rte_malloc at queue setup.
This is necessary because the buffer may be used in a secondary
process transmit path where the primary process allocated it.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
---
drivers/net/pcap/pcap_ethdev.c | 60 +++++++++++++++++++++-------------
1 file changed, 37 insertions(+), 23 deletions(-)

diff --git a/drivers/net/pcap/pcap_ethdev.c b/drivers/net/pcap/pcap_ethdev.c
index ebc96fafe5..02d5cb591f 100644
--- a/drivers/net/pcap/pcap_ethdev.c
+++ b/drivers/net/pcap/pcap_ethdev.c
@@ -91,6 +91,9 @@ struct pcap_tx_queue {
struct queue_stat tx_stat;
char name[PATH_MAX];
char type[ETH_PCAP_ARG_MAXLEN];
+
+ /* Temp buffer used for non-linear packets */
+ uint8_t *bounce_buf;
};

struct pmd_internals {
@@ -385,18 +388,17 @@ static uint16_t
eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
{
unsigned int i;
- struct rte_mbuf *mbuf;
struct pmd_process_private *pp;
struct pcap_tx_queue *dumper_q = queue;
uint16_t num_tx = 0;
uint32_t tx_bytes = 0;
struct pcap_pkthdr header;
pcap_dumper_t *dumper;
- unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
- size_t len, caplen;
+ unsigned char *temp_data;

pp = rte_eth_devices[dumper_q->port_id].process_private;
dumper = pp->tx_dumper[dumper_q->queue_id];
+ temp_data = dumper_q->bounce_buf;

if (dumper == NULL || nb_pkts == 0)
return 0;
@@ -404,12 +406,11 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
/* writes the nb_pkts packets to the previously opened pcap file
* dumper */
for (i = 0; i < nb_pkts; i++) {
- mbuf = bufs[i];
- len = caplen = rte_pktmbuf_pkt_len(mbuf);
- if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
- len > sizeof(temp_data))) {
- caplen = sizeof(temp_data);
- }
+ struct rte_mbuf *mbuf = bufs[i];
+ size_t len, caplen;
+
+ len = rte_pktmbuf_pkt_len(mbuf);
+ caplen = RTE_MIN(len, RTE_ETH_PCAP_SNAPSHOT_LEN);

calculate_timestamp(&header.ts);
header.len = len;
@@ -449,9 +450,6 @@ eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
uint32_t tx_bytes = 0;
struct pcap_tx_queue *tx_queue = queue;

- if (unlikely(nb_pkts == 0))
- return 0;
-
for (i = 0; i < nb_pkts; i++) {
tx_bytes += bufs[i]->pkt_len;
rte_pktmbuf_free(bufs[i]);
@@ -460,7 +458,7 @@ eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
tx_queue->tx_stat.pkts += nb_pkts;
tx_queue->tx_stat.bytes += tx_bytes;

- return i;
+ return nb_pkts;
}

/*
@@ -470,30 +468,30 @@ static uint16_t
eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
{
unsigned int i;
- int ret;
- struct rte_mbuf *mbuf;
struct pmd_process_private *pp;
struct pcap_tx_queue *tx_queue = queue;
uint16_t num_tx = 0;
uint32_t tx_bytes = 0;
pcap_t *pcap;
- unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
- size_t len;
+ unsigned char *temp_data;

pp = rte_eth_devices[tx_queue->port_id].process_private;
pcap = pp->tx_pcap[tx_queue->queue_id];
+ temp_data = tx_queue->bounce_buf;

if (unlikely(nb_pkts == 0 || pcap == NULL))
return 0;

for (i = 0; i < nb_pkts; i++) {
- mbuf = bufs[i];
- len = rte_pktmbuf_pkt_len(mbuf);
+ struct rte_mbuf *mbuf = bufs[i];
+ size_t len = rte_pktmbuf_pkt_len(mbuf);
+ int ret;
+
if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
- len > sizeof(temp_data))) {
+ len > RTE_ETH_PCAP_SNAPSHOT_LEN)) {
PMD_LOG(ERR,
- "Dropping multi segment PCAP packet. Size (%zd) > max size (%zd).",
- len, sizeof(temp_data));
+ "Dropping multi segment PCAP packet. Size (%zd) > max size (%u).",
+ len, RTE_ETH_PCAP_SNAPSHOT_LEN);
rte_pktmbuf_free(mbuf);
continue;
}
@@ -966,7 +964,7 @@ static int
eth_tx_queue_setup(struct rte_eth_dev *dev,
uint16_t tx_queue_id,
uint16_t nb_tx_desc __rte_unused,
- unsigned int socket_id __rte_unused,
+ unsigned int socket_id,
const struct rte_eth_txconf *tx_conf __rte_unused)
{
struct pmd_internals *internals = dev->data->dev_private;
@@ -974,11 +972,26 @@ eth_tx_queue_setup(struct rte_eth_dev *dev,

pcap_q->port_id = dev->data->port_id;
pcap_q->queue_id = tx_queue_id;
+ pcap_q->bounce_buf = rte_malloc_socket(NULL, RTE_ETH_PCAP_SNAPSHOT_LEN,
+ RTE_CACHE_LINE_SIZE, socket_id);
+ if (pcap_q->bounce_buf == NULL)
+ return -ENOMEM;
+
dev->data->tx_queues[tx_queue_id] = pcap_q;

return 0;
}

+static void
+eth_tx_queue_release(struct rte_eth_dev *dev, uint16_t tx_queue_id)
+{
+ struct pmd_internals *internals = dev->data->dev_private;
+ struct pcap_tx_queue *pcap_q = &internals->tx_queue[tx_queue_id];
+
+ rte_free(pcap_q->bounce_buf);
+ pcap_q->bounce_buf = NULL;
+}
+
static int
eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
{
@@ -1019,6 +1032,7 @@ static const struct eth_dev_ops ops = {
.dev_infos_get = eth_dev_info,
.rx_queue_setup = eth_rx_queue_setup,
.tx_queue_setup = eth_tx_queue_setup,
+ .tx_queue_release = eth_tx_queue_release,
.rx_queue_start = eth_rx_queue_start,
.tx_queue_start = eth_tx_queue_start,
.rx_queue_stop = eth_rx_queue_stop,
--
2.53.0