net/pcap: cleanups and test
Subsystems (2)
cover net/pcap: cleanups and test
This is a set of enhancements and tests to the PCAP PMD. It started out when looking at the handling of timestamps then realized lots of other cleanups were needed here.
Stephen Hemminger (12):
net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: support MTU set
net/pcap: use bool for flags
net/pcap: support Tx offloads
net/pcap: support nanosecond timestamp precision net/pcap: remove global variables
net/pcap: avoid use of volatile
net/pcap: optimize calculation of receive timestamp net/pcap: report receive clock
net/pcap: cleanup MAC address handling net/pcap: support MAC address set
test: add test for pcap PMD
app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 1471 +++++++++++++++++++++++++ drivers/net/pcap/pcap_ethdev.c | 373 ++++--- drivers/net/pcap/pcap_osdep.h | 2 + drivers/net/pcap/pcap_osdep_freebsd.c | 60 +- drivers/net/pcap/pcap_osdep_linux.c | 51 +- drivers/net/pcap/pcap_osdep_windows.c | 5 + 7 files changed, 1830 insertions(+), 134 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH 1/12 net/pcap: avoid using rte_malloc and rte_memcpy
No need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_osdep_freebsd.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_osdep_freebsd.c +4 -7
@@ -8,9 +8,6 @@#include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +38,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
Signature · 1 line
2.51.0
PATCH 2/12 net/pcap: support MTU set
In reply to Stephen HemmingerWhen used as single interface useful to support pass through of MTU setting to enable larger frames.
Cleanup the transmit logic so that if packet sent exceeds MTU is an error.
--- drivers/net/pcap/pcap_ethdev.c | 94 +++++++++++++++------------ drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 26 ++++++++ drivers/net/pcap/pcap_osdep_linux.c | 21 ++++++ drivers/net/pcap/pcap_osdep_windows.c | 5 ++ 5 files changed, 104 insertions(+), 43 deletions(-)
drivers/net/pcap/pcap_ethdev.c +51 -43
@@ -22,7 +22,7 @@#include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535 -#define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN +#define RTE_ETH_PCAP_SNAPLEN (RTE_ETHER_MAX_JUMBO_FRAME_LEN - RTE_ETHER_CRC_LEN)#define RTE_ETH_PCAP_PROMISC 1#define RTE_ETH_PCAP_TIMEOUT -1
@@ -377,46 +377,46 @@ 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; +uint16_t mtu; pp = rte_eth_devices[dumper_q->port_id].process_private; dumper = pp->tx_dumper[dumper_q->queue_id]; + mtu = rte_eth_devices[dumper_q->port_id].data->mtu; -if (dumper ==NULL || nb_pkts ==0 ) +if (unlikely (dumper ==NULL || nb_pkts ==0 ))return 0 ;/* 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 =rte_pktmbuf_pkt_len (mbuf); +uint8_t temp_data[RTE_ETH_PCAP_SNAPLEN ]; +struct pcap_pkthdr header; + +if (unlikely (len > mtu)) +continue ;calculate_timestamp (&header.ts); header.len = len; - header.caplen = caplen; + header.caplen = len; +/* rte_pktmbuf_read() returns a pointer to the data directly * in the mbuf (when the mbuf is contiguous) or, otherwise, * a pointer to temp_data after copying into it. */ -pcap_dump ((u_char *)dumper, &header, -rte_pktmbuf_read (mbuf,0 , caplen, temp_data)); +const uint8_t *data =rte_pktmbuf_read (mbuf,0 , len, temp_data); +pcap_dump ((u_char *)dumper, &header, data); num_tx++; - tx_bytes += caplen; -rte_pktmbuf_free (mbuf); + tx_bytes += len; } +rte_pktmbuf_free_bulk (bufs, nb_pkts);/* * Since there's no place to hook a callback when the forwarding
@@ -444,15 +444,15 @@ eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (nb_pkts ==0 ))return 0 ; -for (i =0 ; i < nb_pkts; i++) { +for (i =0 ; i < nb_pkts; i++) tx_bytes += bufs[i]->pkt_len; -rte_pktmbuf_free (bufs[i]); - } + +rte_pktmbuf_free_bulk (bufs, nb_pkts); tx_queue->tx_stat.pkts += nb_pkts; tx_queue->tx_stat.bytes += tx_bytes; -return i; +return nb_pkts; }/*
@@ -462,52 +462,45 @@ 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; +uint16_t mtu; pp = rte_eth_devices[tx_queue->port_id].process_private; pcap = pp->tx_pcap[tx_queue->queue_id]; + mtu = rte_eth_devices[tx_queue->port_id].data->mtu;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); -if (unlikely (!rte_pktmbuf_is_contiguous (mbuf) && - len >sizeof (temp_data))) { -PMD_LOG (ERR , -"Dropping multi segment PCAP packet. Size (%zd) > max size (%zd)." , - len,sizeof (temp_data)); -rte_pktmbuf_free (mbuf); +struct rte_mbuf *mbuf = bufs[i]; +size_t len =rte_pktmbuf_pkt_len (mbuf); +uint8_t temp_data[RTE_ETH_PCAP_SNAPLEN ]; + +if (unlikely (len > mtu))continue ; - }/* rte_pktmbuf_read() returns a pointer to the data directly * in the mbuf (when the mbuf is contiguous) or, otherwise, * a pointer to temp_data after copying into it. */ - ret =pcap_sendpacket (pcap, -rte_pktmbuf_read (mbuf,0 , len, temp_data), len); -if (unlikely (ret !=0 )) -break ; - num_tx++; - tx_bytes += len; -rte_pktmbuf_free (mbuf); +const uint8_t *data =rte_pktmbuf_read (mbuf,0 , len, temp_data); + +if (likely (pcap_sendpacket (pcap, data, len) ==0 )) { + num_tx++; + tx_bytes += len; + } } tx_queue->tx_stat.pkts += num_tx; tx_queue->tx_stat.bytes += tx_bytes; - tx_queue->tx_stat.err_pkts += i - num_tx; + tx_queue->tx_stat.err_pkts += nb_pkts - num_tx; -return i; +return nb_pkts; }/*
@@ -745,6 +738,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; + dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ;return 0 ; }
@@ -1002,6 +997,18 @@ eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)return 0 ; } +static int +eth_mtu_set (struct rte_eth_dev *dev,uint16_t mtu) +{ +struct pmd_internals *internals = dev->data->dev_private; + +if (internals->single_iface) +return osdep_iface_mtu_set (internals->if_index, mtu); + +return 0 ; +} + +static const struct eth_dev_ops ops = { .dev_start = eth_dev_start, .dev_stop = eth_dev_stop,
@@ -1015,6 +1022,7 @@ static const struct eth_dev_ops ops = { .rx_queue_stop = eth_rx_queue_stop, .tx_queue_stop = eth_tx_queue_stop, .link_update = eth_link_update, + .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset, };
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -14,5 +14,6 @@ extern int eth_pcap_logtype;int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +int osdep_iface_mtu_set (int index,uint16_t mtu);#endif
drivers/net/pcap/pcap_osdep_freebsd.c +26 -0
@@ -4,9 +4,12 @@* All rights reserved. */ +#include <unistd.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> +#include <sys/ioctl.h> +#include <sys/sockio.h> #include "pcap_osdep.h"
@@ -54,3 +57,26 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)free (buf);return 0 ; } + +int osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = {0 }; +if (s <0 ) +return -EINVAL ; + +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_mtu = mtu; + +int ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_linux.c +21 -0
@@ -40,3 +40,24 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)close (if_fd);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (PF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = { .ifr_mtu = mtu }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + +int ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_windows.c +5 -0
@@ -116,3 +116,8 @@ osdep_iface_mac_get(const char *device_name, struct rte_ether_addr *mac)free (info);return ret; } + +int osdep_iface_mtu_set(int index __rte_unused, uint16_t mtu __rte_unused) +{ +return -ENOTSUP ; +}
Signature · 1 line
2.51.0
PATCH 3/12 net/pcap: use bool for flags
In reply to Stephen HemmingerSave some space by using bool for flag values.
Use common code to parse value of a boolean flag.
--- drivers/net/pcap/pcap_ethdev.c | 73 +++++++++++++++++----------------- 1 file changed, 36 insertions(+), 37 deletions(-)
drivers/net/pcap/pcap_ethdev.c +36 -37
@@ -5,6 +5,7 @@*/ #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <pcap.h>
@@ -91,9 +92,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -103,25 +104,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -858,7 +859,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1180,31 +1181,29 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} +bool *flag = extra_args; +/* table of possible representation of boolean */ +static const char *const values[] = { +"false" ,"true" , +"0" ,"1" , +"disable" ,"enable" , +"on" ,"off" , + }; -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; +for (unsigned int i =0 ; i <RTE_DIM (values); i++) { +if (strcmp (value, values[i]) ==0 ) { + *flag = !!(i &1 ); -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +PMD_LOG (INFO ,"%s set to %s" , + key, *flag ?"true" :"false" ); +return 0 ; + } } -return 0 ; + +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; } static int
@@ -1479,7 +1478,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1518,9 +1517,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
RE: [PATCH 03/12] net/pcap: use bool for flags
Quoted 9 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Tuesday 6 January 2026 18:27
To: [email protected]
Cc: Stephen Hemminger <[email protected]>
Subject: [PATCH 03/12] net/pcap: use bool for flagsSave some space by using bool for flag values. Use common code to parse value of a boolean flag.
Note that there might technically be some performance cost of this. Said that, since original code did not leave any comments why ints were used, let's think it was not on purpose, while bools are cleaner and less error-prone.
I however think that proper bool conversion should use true or false on assignment/initialization, not 1 or 0.
// snip
drivers/net/pcap/pcap_ethdev.c open_tx_iface(const char *key, const char *value, void *extra_args) · line 1184
Quoted 7 lines
drivers/net/pcap/pcap_ethdev.c +1
-2@@ -1180,31 +1181,29 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args)
This function probably belongs in some common library. There is a similar code in lib/argparse/rte_argparse.c and lib/cmdline/cmdline_parse_bool.c that might benefit. Having some tests could also be useful.
I would also think of naming more, word "process" does not really convey much meaning. How about "parse_bool"?
Quoted 7 lines
drivers/net/pcap/pcap_ethdev.c +0
-6{ -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ;
drivers/net/pcap/pcap_ethdev.c open_tx_iface(const char *key, const char *value, void *extra_args) · line 1192
Quoted 10 lines
drivers/net/pcap/pcap_ethdev.c +7
-3- } -return 0 ; -} +bool *flag = extra_args; +/* table of possible representation of boolean */ +static const char *const values[] = { +"false" ,"true" , +"0" ,"1" , +"disable" ,"enable" , +"on" ,"off" ,
Last pair is swapped.
drivers/net/pcap/pcap_ethdev.c open_tx_iface(const char *key, const char *value, void *extra_args) · line 1196
Quoted 11 lines
drivers/net/pcap/pcap_ethdev.c +3
-7+ }; -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; +for (unsigned int i =0 ; i <RTE_DIM (values); i++) { +if (strcmp (value, values[i]) ==0 ) {
Maybe strcasecmp (just suggesting)?
drivers/net/pcap/pcap_ethdev.c open_tx_iface(const char *key, const char *value, void *extra_args) · line 1200
Quoted 6 lines
drivers/net/pcap/pcap_ethdev.c +3
-2+ *flag = !!(i &1 ); -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +PMD_LOG (INFO ,"%s set to %s" , + key, *flag ?"true" :"false" );
Should we have a common use {"false", "true"} inline function somewhere? Logging of infinite_rx in the same file could use it, as well as a few other places.
Quoted 8 lines
drivers/net/pcap/pcap_ethdev.c +5
-1+return 0 ; + } } -return 0 ; + +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }
Re: [PATCH 03/12] net/pcap: use bool for flags
Quoted 12 lines · On Wed, 7 Jan 2026 10:28:32 +0000 Marat Khalili <[email protected]>…
drivers/net/pcap/pcap_ethdev.c open_tx_iface(const char *key, const char *value, void *extra_args) · line 1184
Quoted 7 lines
drivers/net/pcap/pcap_ethdev.c +1
-2@@ -1180,31 +1181,29 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args)This function probably belongs in some common library. There is a similar code in
lib/argparse/rte_argparse.candlib/cmdline/cmdline_parse_bool.cthat might benefit. Having some tests could also be useful.
Quoted 2 lines
I would also think of naming more, word "process" does not really convey much meaning. How about "
parse_bool"?
I just went off and tried that and it ends up not being that good. The problem is that the place for common code in DPDK is currently EAL.
But if you put bool parsing in EAL it creates a circular dependency since EAL depends on argparse and kvargs. So can't use any new string parsing there. Cmdline does its own validation so common code won't help there either.
Good idea, just won't work.
Going back to just 0/1 for now.
PATCH 4/12 net/pcap: support Tx offloads
In reply to Stephen HemmingerThe driver already handles multi-segment mbufs but did not report that in offload flags. Driver can easily insert vlan tag making testing easier.
--- drivers/net/pcap/pcap_ethdev.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/net/pcap/pcap_ethdev.c +8 -0
@@ -403,6 +403,9 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (len > mtu))continue ; +if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf)) +continue ; +calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -485,6 +488,9 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (len > mtu))continue ; +if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf)) +continue ; +/* rte_pktmbuf_read() returns a pointer to the data directly * in the mbuf (when the mbuf is contiguous) or, otherwise, * a pointer to temp_data after copying into it.
@@ -741,6 +747,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->min_rx_bufsize =0 ; dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ;return 0 ; }
Signature · 1 line
2.51.0
PATCH 5/12 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerConsistently support nanosecond timestamps across all the variations of pcap PMD receive.
--- drivers/net/pcap/pcap_ethdev.c | 103 +++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 24 deletions(-)
drivers/net/pcap/pcap_ethdev.c +79 -24
@@ -19,13 +19,12 @@#include <rte_mbuf_dyn.h> #include <bus_vdev_driver.h> #include <rte_os_shim.h> +#include <rte_time.h> #include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535#define RTE_ETH_PCAP_SNAPLEN (RTE_ETHER_MAX_JUMBO_FRAME_LEN - RTE_ETHER_CRC_LEN) -#define RTE_ETH_PCAP_PROMISC 1 -#define RTE_ETH_PCAP_TIMEOUT -1#define ETH_PCAP_RX_PCAP_ARG "rx_pcap"#define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
@@ -68,6 +67,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool timestamp_offloading;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -95,6 +95,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool timestamp_offloading; };struct pmd_process_private {
@@ -299,7 +300,6 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (ret !=1 ) {if (ret ==PCAP_ERROR ) pcap_q->rx_stat.err_pkts++; -break ; }
@@ -325,10 +325,20 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; -uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; - *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us; - mbuf->ol_flags |= timestamp_rx_dynflag; +if (pcap_q->timestamp_offloading) { +/* +* Although time stamp in struct pcap_pkthdr is defined as struct timeval, +* it really is a timespec with nanosecond resolution. +*/ +const struct timespec *ts = (struct timespec *)&header->ts; + + *RTE_MBUF_DYNFIELD (mbuf, timestamp_dynfield_offset, + rte_mbuf_timestamp_t *) = rte_timespec_to_ns(ts); + + mbuf->ol_flags |= timestamp_rx_dynflag; + } + mbuf->port = pcap_q->port_id; bufs[num_rx] = mbuf; num_rx++;
@@ -514,22 +524,57 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)* pcap_open_live wrapper function */ static inline int -open_iface_live (const char *iface,pcap_t **pcap) { - *pcap =pcap_open_live (iface,RTE_ETH_PCAP_SNAPLEN , -RTE_ETH_PCAP_PROMISC ,RTE_ETH_PCAP_TIMEOUT , errbuf); +open_iface_live (const char *iface,pcap_t **pcap) +{ +pcap_t *pc =pcap_create (iface, errbuf); +if (pc ==NULL ) { +PMD_LOG (ERR ,"Couldn't create %s: %s" , iface, errbuf); + goto error; + } -if (*pcap ==NULL ) { -PMD_LOG (ERR ,"Couldn't open %s: %s" , iface, errbuf); -return -1 ; +int status =pcap_set_tstamp_precision (pc,PCAP_TSTAMP_PRECISION_NANO ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to ns precision: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_immediate_mode (pc,1 ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to immediate mode: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_promisc (pc,1 ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to promiscious: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_snaplen (pc,RTE_ETH_PCAP_SNAPSHOT_LEN ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set snapshot length: %s" , + iface,pcap_statustostr (status)); + + status =pcap_activate (pc); +if (status <0 ) { +char *cp =pcap_geterr (pc); + +if (status ==PCAP_ERROR ) +PMD_LOG (ERR ,"%s: could not activate: %s" , iface, cp); +else +PMD_LOG (ERR ,"%s: %s (%s)" , iface,pcap_statustostr (status), cp); + goto error; } -if (pcap_setnonblock (*pcap,1 , errbuf)) { +if (pcap_setnonblock (pc,1 , errbuf)) {PMD_LOG (ERR ,"Couldn't set non-blocking on %s: %s" , iface, errbuf); -pcap_close (*pcap); -return -1 ; + goto error; } + *pcap = pc;return 0 ; + +error: +if (pc) +pcap_close (pc); +return -1 ; } static int
@@ -576,7 +621,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { - *pcap =pcap_open_offline (pcap_filename, errbuf); + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename, +PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {PMD_LOG (ERR ,"Couldn't open %s: %s" , pcap_filename, errbuf);
@@ -613,6 +659,15 @@ eth_dev_start(struct rte_eth_dev *dev)struct pcap_tx_queue *tx;struct pcap_rx_queue *rx; +if (internals->timestamp_offloading) { +int ret =rte_mbuf_dyn_rx_timestamp_register (×tamp_dynfield_offset, + ×tamp_rx_dynflag); +if (ret !=0 ) { +PMD_LOG (ERR ,"Failed to register Rx timestamp field/flag" ); +return ret; + } + } +/* Special iface case. Single pcap is open and shared between tx/rx. */ if (internals->single_iface) { tx = &internals->tx_queue[0 ];
@@ -728,8 +783,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->timestamp_offloading = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_TIMESTAMP );return 0 ; }
@@ -749,6 +809,7 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS |RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_TIMESTAMP ;return 0 ; }
@@ -896,6 +957,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; dev->data->rx_queues[rx_queue_id] = pcap_q; + pcap_q->timestamp_offloading = internals->timestamp_offloading;if (internals->infinite_rx) {struct pmd_process_private *pp;
@@ -1446,13 +1508,6 @@ pmd_pcap_probe(struct rte_vdev_device *dev) start_cycles =rte_get_timer_cycles (); hz =rte_get_timer_hz (); - ret =rte_mbuf_dyn_rx_timestamp_register (×tamp_dynfield_offset, - ×tamp_rx_dynflag); -if (ret !=0 ) { -PMD_LOG (ERR ,"Failed to register Rx timestamp field/flag" ); -return -1 ; - } -if (rte_eal_process_type () ==RTE_PROC_SECONDARY ) { eth_dev =rte_eth_dev_attach_secondary (name);if (!eth_dev) {
Signature · 1 line
2.51.0
PATCH 6/12 net/pcap: remove global variables
In reply to Stephen HemmingerLocalize variables where possible.
--- drivers/net/pcap/pcap_ethdev.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +6 -2
@@ -39,11 +39,9 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -526,6 +524,8 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; +pcap_t *pc =pcap_create (iface, errbuf);if (pc ==NULL ) {PMD_LOG (ERR ,"Couldn't create %s: %s" , iface, errbuf);
@@ -621,6 +621,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1314,11 +1316,13 @@ pmd_init_internals(struct rte_vdev_device *vdev,* - and point eth_dev structure to new eth_dev_data structure */ *internals = (*eth_dev)->data->dev_private; +/* * Interface MAC = 02:70:63:61:70:<iface_idx> * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
RE: [PATCH 06/12] net/pcap: remove global variables
Quoted 63 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Tuesday 6 January 2026 18:27
To: [email protected]
Cc: Stephen Hemminger <[email protected]>
Subject: [PATCH 06/12] net/pcap: remove global variablesLocalize variables where possible.
Signed-off-by Stephen Hemminger <[email protected]>--- drivers/net/pcap/pcap_ethdev.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/net/pcap/pcap_ethdev.c +6
-3@@ -39,11 +39,9 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;@@ -526,6 +524,8 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; +pcap_t *pc =pcap_create (iface, errbuf);if (pc ==NULL ) {PMD_LOG (ERR ,"Couldn't create %s: %s" , iface, errbuf);@@ -621,6 +621,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {@@ -1314,11 +1316,13 @@ pmd_init_internals(struct rte_vdev_device *vdev,* - and point eth_dev structure to new eth_dev_data structure */ *internals = (*eth_dev)->data->dev_private; +/* * Interface MAC = 02:70:63:61:70:<iface_idx> * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } }; --
Quoted 1 line
2.51.0
PATCH 7/12 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics generates expensive atomic rmw operations when not necessary.
--- drivers/net/pcap/pcap_ethdev.c | 38 ++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -9
@@ -47,10 +47,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -267,6 +267,9 @@ eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) pcap_q->rx_stat.pkts += i; pcap_q->rx_stat.bytes += rx_bytes; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return i; }
@@ -345,6 +348,9 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) pcap_q->rx_stat.pkts += num_rx; pcap_q->rx_stat.bytes += rx_bytes; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return num_rx; }
@@ -440,6 +446,9 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) dumper_q->tx_stat.bytes += tx_bytes; dumper_q->tx_stat.err_pkts += nb_pkts - num_tx; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return nb_pkts; }
@@ -464,6 +473,9 @@ 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; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return nb_pkts; }
@@ -515,6 +527,9 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) tx_queue->tx_stat.bytes += tx_bytes; tx_queue->tx_stat.err_pkts += nb_pkts - num_tx; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return nb_pkts; }
@@ -821,13 +836,16 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; +/* ensure that current statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_acquire); +for (i =0 ; i <RTE_ETHDEV_QUEUE_STAT_CNTRS && i < dev->data->nb_rx_queues; i++) {if (qstats !=NULL ) {
@@ -884,6 +902,8 @@ eth_stats_reset(struct rte_eth_dev *dev) internal->tx_queue[i].tx_stat.err_pkts =0 ; } +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release);return 0 ; }
Signature · 1 line
2.51.0
RE: [PATCH 07/12] net/pcap: avoid use of volatile
Quoted 2 lines
Using volatile for statistics generates expensive atomic rmw operations when not necessary.
I am not sure it is true, AFAIK volatile only affects the compiler. In this case it mostly guards against these variables being completely cached in registers. Your new version is actually more correct, but also potentially slower since general fence affects all reads or writes, not just counters in question. I think it is common among drivers to not guarantee visibility of the most recent counter values for the sake of performance.
Quoted 109 lines
Signed-off-by Stephen Hemminger <[email protected]>--- drivers/net/pcap/pcap_ethdev.c | 38 ++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-)drivers/net/pcap/pcap_ethdev.c +29
-10@@ -47,10 +47,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {@@ -267,6 +267,9 @@ eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) pcap_q->rx_stat.pkts += i; pcap_q->rx_stat.bytes += rx_bytes; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return i; }@@ -345,6 +348,9 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) pcap_q->rx_stat.pkts += num_rx; pcap_q->rx_stat.bytes += rx_bytes; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return num_rx; }@@ -440,6 +446,9 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) dumper_q->tx_stat.bytes += tx_bytes; dumper_q->tx_stat.err_pkts += nb_pkts - num_tx; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return nb_pkts; }@@ -464,6 +473,9 @@ 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; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return nb_pkts; }@@ -515,6 +527,9 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) tx_queue->tx_stat.bytes += tx_bytes; tx_queue->tx_stat.err_pkts += nb_pkts - num_tx; +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release); +return nb_pkts; }@@ -821,13 +836,16 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; +/* ensure that current statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_acquire); +for (i =0 ; i <RTE_ETHDEV_QUEUE_STAT_CNTRS && i < dev->data->nb_rx_queues; i++) {if (qstats !=NULL ) {@@ -884,6 +902,8 @@ eth_stats_reset(struct rte_eth_dev *dev) internal->tx_queue[i].tx_stat.err_pkts =0 ; } +/* ensure store operations of statistics are visible */ +rte_atomic_thread_fence (rte_memory_order_release);return 0 ; } --2.51.0
PATCH 8/12 net/pcap: optimize calculation of receive timestamp
In reply to Stephen HemmingerAvoid doing slow instructions in receive path when calculating timestamp. Give all packets in the same rx burst the same timestamp.
--- drivers/net/pcap/pcap_ethdev.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
drivers/net/pcap/pcap_ethdev.c +13 -10
@@ -20,6 +20,7 @@#include <bus_vdev_driver.h> #include <rte_os_shim.h> #include <rte_time.h> +#include <rte_reciprocal.h> #include "pcap_osdep.h"
@@ -41,7 +42,7 @@static struct timespec start_time;static uint64_t start_cycles; -static uint64_t hz; +static struct rte_reciprocal_u64 hz_inv;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -362,8 +363,6 @@ eth_null_rx(void *queue __rte_unused,return 0 ; } -#define NSEC_PER_SEC 1000000000L -/* * This function stores nanoseconds in `tv_usec` field of `struct timeval`, * because `ts` goes directly to nanosecond-precision dump.
@@ -374,8 +373,10 @@ calculate_timestamp(struct timeval *ts) {struct timespec cur_time; cycles =rte_get_timer_cycles () - start_cycles; - cur_time.tv_sec = cycles / hz; - cur_time.tv_nsec = (cycles % hz) *NSEC_PER_SEC / hz; + cur_time.tv_sec =rte_reciprocal_divide_u64 (cycles, &hz_inv); +/* compute remainder */ + cycles -= cur_time.tv_sec *rte_get_timer_hz (); + cur_time.tv_nsec =rte_reciprocal_divide_u64 (cycles *NS_PER_S , &hz_inv); ts->tv_sec = start_time.tv_sec + cur_time.tv_sec; ts->tv_usec = start_time.tv_nsec + cur_time.tv_nsec;
@@ -394,6 +395,7 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)unsigned int i;struct pmd_process_private *pp;struct pcap_tx_queue *dumper_q = queue; +struct pcap_pkthdr header;uint16_t num_tx =0 ;uint32_t tx_bytes =0 ;pcap_dumper_t *dumper;
@@ -406,13 +408,14 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (dumper ==NULL || nb_pkts ==0 ))return 0 ; -/* writes the nb_pkts packets to the previously opened pcap file -* dumper */ +/* all packets in burst have same timestamp */ +calculate_timestamp (&header.ts); + +/* writes the nb_pkts packets to the previously opened pcap file dumper */ for (i =0 ; i < nb_pkts; i++) {struct rte_mbuf *mbuf = bufs[i];size_t len =rte_pktmbuf_pkt_len (mbuf);uint8_t temp_data[RTE_ETH_PCAP_SNAPLEN ]; -struct pcap_pkthdr header;if (unlikely (len > mtu))continue ;
@@ -420,7 +423,6 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf))continue ; -calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -1530,7 +1532,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)timespec_get (&start_time,TIME_UTC ); start_cycles =rte_get_timer_cycles (); - hz =rte_get_timer_hz (); + + hz_inv =rte_reciprocal_value_u64 (rte_get_timer_hz ());if (rte_eal_process_type () ==RTE_PROC_SECONDARY ) { eth_dev =rte_eth_dev_attach_secondary (name);
Signature · 1 line
2.51.0
RE: [PATCH 08/12] net/pcap: optimize calculation of receive timestamp
Quoted 46 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Tuesday 6 January 2026 18:27
To: [email protected]
Cc: Stephen Hemminger <[email protected]>
Subject: [PATCH 08/12] net/pcap: optimize calculation of receive timestampAvoid doing slow instructions in receive path when calculating timestamp. Give all packets in the same rx burst the same timestamp.
Signed-off-by Stephen Hemminger <[email protected]>--- drivers/net/pcap/pcap_ethdev.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)drivers/net/pcap/pcap_ethdev.c +2
-3@@ -20,6 +20,7 @@#include <bus_vdev_driver.h> #include <rte_os_shim.h> #include <rte_time.h> +#include <rte_reciprocal.h> #include "pcap_osdep.h" @@ -41,7 +42,7 @@static struct timespec start_time;static uint64_t start_cycles; -static uint64_t hz; +static struct rte_reciprocal_u64 hz_inv;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;@@ -362,8 +363,6 @@ eth_null_rx(void *queue __rte_unused,return 0 ; } -#define NSEC_PER_SEC 1000000000L -/* * This function stores nanoseconds in `tv_usec` field of `struct timeval`, * because `ts` goes directly to nanosecond-precision dump.
drivers/net/pcap/pcap_ethdev.c calculate_timestamp(struct timeval *ts) { · line 378
Quoted 8 lines
drivers/net/pcap/pcap_ethdev.c +3
-2@@ -374,8 +373,10 @@ calculate_timestamp(struct timeval *ts) {struct timespec cur_time; cycles =rte_get_timer_cycles () - start_cycles; - cur_time.tv_sec = cycles / hz; - cur_time.tv_nsec = (cycles % hz) *NSEC_PER_SEC / hz; + cur_time.tv_sec =rte_reciprocal_divide_u64 (cycles, &hz_inv); +/* compute remainder */ + cycles -= cur_time.tv_sec *rte_get_timer_hz ();
Can be made faster and safer by caching rte_get_timer_hz() result in current translation unit.
Quoted 49 lines
drivers/net/pcap/pcap_ethdev.c +8
-6+ cur_time.tv_nsec =rte_reciprocal_divide_u64 (cycles *NS_PER_S , &hz_inv); ts->tv_sec = start_time.tv_sec + cur_time.tv_sec; ts->tv_usec = start_time.tv_nsec + cur_time.tv_nsec;@@ -394,6 +395,7 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)unsigned int i;struct pmd_process_private *pp;struct pcap_tx_queue *dumper_q = queue; +struct pcap_pkthdr header;uint16_t num_tx =0 ;uint32_t tx_bytes =0 ;pcap_dumper_t *dumper;@@ -406,13 +408,14 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (dumper ==NULL || nb_pkts ==0 ))return 0 ; -/* writes the nb_pkts packets to the previously opened pcap file -* dumper */ +/* all packets in burst have same timestamp */ +calculate_timestamp (&header.ts); + +/* writes the nb_pkts packets to the previously opened pcap file dumper */ for (i =0 ; i < nb_pkts; i++) {struct rte_mbuf *mbuf = bufs[i];size_t len =rte_pktmbuf_pkt_len (mbuf);uint8_t temp_data[RTE_ETH_PCAP_SNAPLEN ]; -struct pcap_pkthdr header;if (unlikely (len > mtu))continue ;@@ -420,7 +423,6 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf))continue ; -calculate_timestamp (&header.ts); header.len = len; header.caplen = len;@@ -1530,7 +1532,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)timespec_get (&start_time,TIME_UTC ); start_cycles =rte_get_timer_cycles (); - hz =rte_get_timer_hz (); + + hz_inv =rte_reciprocal_value_u64 (rte_get_timer_hz ());if (rte_eal_process_type () ==RTE_PROC_SECONDARY ) { eth_dev =rte_eth_dev_attach_secondary (name); --
Quoted 1 line
2.51.0
Apart from one minor optimization suggestion above,
PATCH 9/12 net/pcap: report receive clock
In reply to Stephen HemmingerSince this drive supports receive timestamping, provide a callback so that the application can determine the clock rate for timestamp.
--- drivers/net/pcap/pcap_ethdev.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/net/pcap/pcap_ethdev.c +11 -0
@@ -1103,6 +1103,16 @@ eth_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)return 0 ; } +/* Timestamp values in receive packets from libpcap are in UTC */ +static int +eth_rx_clock(struct rte_eth_dev *dev __rte_unused, uint64_t *timestamp) +{ +struct timespec cur_time; + +timespec_get (&cur_time,TIME_UTC ); + *timestamp =rte_timespec_to_ns (&cur_time); +return 0 ; +}static const struct eth_dev_ops ops = { .dev_start = eth_dev_start,
@@ -1120,6 +1130,7 @@ static const struct eth_dev_ops ops = { .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset, + .read_clock = eth_rx_clock, }; static int
Signature · 1 line
2.51.0
PATCH 10/12 net/pcap: cleanup MAC address handling
In reply to Stephen HemmingerUse rte_ether_addr structure to avoid memcpy and void *.
--- drivers/net/pcap/pcap_ethdev.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/pcap/pcap_ethdev.c +3 -3
@@ -1383,9 +1383,9 @@ pmd_init_internals(struct rte_vdev_device *vdev,static int eth_pcap_update_mac (const char *if_name,struct rte_eth_dev *eth_dev, -const unsigned int numa_node) +const unsigned int numa_node) { -void *mac_addrs; +struct rte_ether_addr *mac_addrs;struct rte_ether_addr mac;if (osdep_iface_mac_get (if_name, &mac) <0 )
@@ -1396,7 +1396,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,return -1 ;PMD_LOG (INFO ,"Setting phy MAC for %s" , if_name); -rte_memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); +rte_ether_addr_copy (&mac, mac_addrs); eth_dev->data->mac_addrs = mac_addrs;return 0 ; }
Signature · 1 line
2.51.0
PATCH 11/12 net/pcap: support MAC address set
In reply to Stephen HemmingerWhen using pcap on a single interface, it is possible for driver to proxy the mac address set operation.
--- drivers/net/pcap/pcap_ethdev.c | 13 ++++++++++++ drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 23 ++++++++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 30 ++++++++++++++++++++++++++- 4 files changed, 66 insertions(+), 1 deletion(-)
drivers/net/pcap/pcap_ethdev.c +13 -0
@@ -1103,6 +1103,18 @@ eth_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)return 0 ; } +static int +eth_dev_macaddr_set (struct rte_eth_dev *dev,struct rte_ether_addr *addr) +{ +struct pmd_internals *internals = dev->data->dev_private; + +if (internals->single_iface) +return osdep_iface_mac_set (internals->if_index, addr); +else +return -ENOTSUP ; +} + +/* Timestamp values in receive packets from libpcap are in UTC */ static int eth_rx_clock(struct rte_eth_dev *dev __rte_unused, uint64_t *timestamp)
@@ -1127,6 +1139,7 @@ static const struct eth_dev_ops ops = { .rx_queue_stop = eth_rx_queue_stop, .tx_queue_stop = eth_tx_queue_stop, .link_update = eth_link_update, + .mac_addr_set = eth_dev_macaddr_set, .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset,
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -14,6 +14,7 @@ extern int eth_pcap_logtype;int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +int osdep_iface_mac_set (int ifindex,const struct rte_ether_addr *mac);int osdep_iface_mtu_set (int index,uint16_t mtu);#endif
drivers/net/pcap/pcap_osdep_freebsd.c +23 -0
@@ -58,6 +58,29 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return 0 ; } +int +osdep_iface_mac_set (int ifindex,const struct rte_ether_addr *mac) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = {0 }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_addr.sa_family =AF_LINK ; +memcpy (ifr.ifr_addr.sa_data, mac,sizeof (*mac)); + +int ret =ioctl (s,SIOCSIFLLADDR , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +} +int osdep_iface_mtu_set (int ifindex,uint16_t mtu) {char ifname[IFNAMSIZ ];
drivers/net/pcap/pcap_osdep_linux.c +29 -1
@@ -4,13 +4,18 @@* All rights reserved. */ +#include <string.h> +#include <stdint.h> +#include <unistd.h> +#include <errno.h> #include <net/if.h> +#include <net/if_arp.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <unistd.h> #include <rte_memcpy.h> #include <rte_string_fns.h> +#include <rte_ether.h> #include "pcap_osdep.h"
@@ -41,6 +46,29 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return 0 ; } +int +osdep_iface_mac_set (int ifindex,const struct rte_ether_addr *mac) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = {0 }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_hwaddr.sa_family =ARPHRD_ETHER ; +memcpy (ifr.ifr_hwaddr.sa_data, mac,sizeof (*mac)); + +int ret =ioctl (s,SIOCSIFHWADDR , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +} +int osdep_iface_mtu_set (int ifindex,uint16_t mtu) {
Signature · 1 line
2.51.0
PATCH 12/12 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests timestamps and jumbo frame support.
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 1471 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 1473 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['ethdev', 'net', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +1471 -0
1,471 lines not on this page · raw message
Signature · 1 line
2.51.0
v2 cover pcap: cleanup pcap PMD and add test
In reply to Stephen HemmingerThis is a set of enhancements and tests to the PCAP PMD. It started out when looking at the handling of timestamps then realized lots of other cleanups were needed here.
v2 - review feedback
- consolidate patches
Stephen Hemminger (9):
net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: support MTU set
net/pcap: use bool for flags
net/pcap: support Tx offloads
net/pcap: support nanosecond timestamp precision net/pcap: remove global variables net/pcap: avoid use of volatile net/pcap: support MAC address set
test: add test for pcap PMD
app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 1846 +++++++++++++++++++++++++ drivers/net/pcap/pcap_ethdev.c | 353 +++-- drivers/net/pcap/pcap_osdep.h | 2 + drivers/net/pcap/pcap_osdep_freebsd.c | 60 +- drivers/net/pcap/pcap_osdep_linux.c | 51 +- drivers/net/pcap/pcap_osdep_windows.c | 5 + 7 files changed, 2184 insertions(+), 135 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v2 1/9 net/pcap: avoid using rte_malloc and rte_memcpy
No need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_osdep_freebsd.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_osdep_freebsd.c +4 -7
@@ -8,9 +8,6 @@#include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +38,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
Signature · 1 line
2.51.0
PATCH v2 2/9 net/pcap: support MTU set
In reply to Stephen HemmingerWhen used as single interface useful to support pass through of MTU setting to enable larger frames.
Cleanup the transmit logic so that if packet sent exceeds MTU is an error.
--- drivers/net/pcap/pcap_ethdev.c | 94 +++++++++++++++------------ drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 26 ++++++++ drivers/net/pcap/pcap_osdep_linux.c | 21 ++++++ drivers/net/pcap/pcap_osdep_windows.c | 5 ++ 5 files changed, 104 insertions(+), 43 deletions(-)
drivers/net/pcap/pcap_ethdev.c +51 -43
@@ -22,7 +22,7 @@#include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535 -#define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN +#define RTE_ETH_PCAP_SNAPLEN (RTE_ETHER_MAX_JUMBO_FRAME_LEN - RTE_ETHER_CRC_LEN)#define RTE_ETH_PCAP_PROMISC 1#define RTE_ETH_PCAP_TIMEOUT -1
@@ -377,46 +377,46 @@ 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; +uint16_t mtu; pp = rte_eth_devices[dumper_q->port_id].process_private; dumper = pp->tx_dumper[dumper_q->queue_id]; + mtu = rte_eth_devices[dumper_q->port_id].data->mtu; -if (dumper ==NULL || nb_pkts ==0 ) +if (unlikely (dumper ==NULL || nb_pkts ==0 ))return 0 ;/* 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 =rte_pktmbuf_pkt_len (mbuf); +uint8_t temp_data[RTE_ETH_PCAP_SNAPLEN ]; +struct pcap_pkthdr header; + +if (unlikely (len > mtu)) +continue ;calculate_timestamp (&header.ts); header.len = len; - header.caplen = caplen; + header.caplen = len; +/* rte_pktmbuf_read() returns a pointer to the data directly * in the mbuf (when the mbuf is contiguous) or, otherwise, * a pointer to temp_data after copying into it. */ -pcap_dump ((u_char *)dumper, &header, -rte_pktmbuf_read (mbuf,0 , caplen, temp_data)); +const uint8_t *data =rte_pktmbuf_read (mbuf,0 , len, temp_data); +pcap_dump ((u_char *)dumper, &header, data); num_tx++; - tx_bytes += caplen; -rte_pktmbuf_free (mbuf); + tx_bytes += len; } +rte_pktmbuf_free_bulk (bufs, nb_pkts);/* * Since there's no place to hook a callback when the forwarding
@@ -444,15 +444,15 @@ eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (nb_pkts ==0 ))return 0 ; -for (i =0 ; i < nb_pkts; i++) { +for (i =0 ; i < nb_pkts; i++) tx_bytes += bufs[i]->pkt_len; -rte_pktmbuf_free (bufs[i]); - } + +rte_pktmbuf_free_bulk (bufs, nb_pkts); tx_queue->tx_stat.pkts += nb_pkts; tx_queue->tx_stat.bytes += tx_bytes; -return i; +return nb_pkts; }/*
@@ -462,52 +462,45 @@ 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; +uint16_t mtu; pp = rte_eth_devices[tx_queue->port_id].process_private; pcap = pp->tx_pcap[tx_queue->queue_id]; + mtu = rte_eth_devices[tx_queue->port_id].data->mtu;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); -if (unlikely (!rte_pktmbuf_is_contiguous (mbuf) && - len >sizeof (temp_data))) { -PMD_LOG (ERR , -"Dropping multi segment PCAP packet. Size (%zd) > max size (%zd)." , - len,sizeof (temp_data)); -rte_pktmbuf_free (mbuf); +struct rte_mbuf *mbuf = bufs[i]; +size_t len =rte_pktmbuf_pkt_len (mbuf); +uint8_t temp_data[RTE_ETH_PCAP_SNAPLEN ]; + +if (unlikely (len > mtu))continue ; - }/* rte_pktmbuf_read() returns a pointer to the data directly * in the mbuf (when the mbuf is contiguous) or, otherwise, * a pointer to temp_data after copying into it. */ - ret =pcap_sendpacket (pcap, -rte_pktmbuf_read (mbuf,0 , len, temp_data), len); -if (unlikely (ret !=0 )) -break ; - num_tx++; - tx_bytes += len; -rte_pktmbuf_free (mbuf); +const uint8_t *data =rte_pktmbuf_read (mbuf,0 , len, temp_data); + +if (likely (pcap_sendpacket (pcap, data, len) ==0 )) { + num_tx++; + tx_bytes += len; + } } tx_queue->tx_stat.pkts += num_tx; tx_queue->tx_stat.bytes += tx_bytes; - tx_queue->tx_stat.err_pkts += i - num_tx; + tx_queue->tx_stat.err_pkts += nb_pkts - num_tx; -return i; +return nb_pkts; }/*
@@ -745,6 +738,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; + dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ;return 0 ; }
@@ -1002,6 +997,18 @@ eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)return 0 ; } +static int +eth_mtu_set (struct rte_eth_dev *dev,uint16_t mtu) +{ +struct pmd_internals *internals = dev->data->dev_private; + +if (internals->single_iface) +return osdep_iface_mtu_set (internals->if_index, mtu); + +return 0 ; +} + +static const struct eth_dev_ops ops = { .dev_start = eth_dev_start, .dev_stop = eth_dev_stop,
@@ -1015,6 +1022,7 @@ static const struct eth_dev_ops ops = { .rx_queue_stop = eth_rx_queue_stop, .tx_queue_stop = eth_tx_queue_stop, .link_update = eth_link_update, + .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset, };
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -14,5 +14,6 @@ extern int eth_pcap_logtype;int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +int osdep_iface_mtu_set (int index,uint16_t mtu);#endif
drivers/net/pcap/pcap_osdep_freebsd.c +26 -0
@@ -4,9 +4,12 @@* All rights reserved. */ +#include <unistd.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> +#include <sys/ioctl.h> +#include <sys/sockio.h> #include "pcap_osdep.h"
@@ -54,3 +57,26 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)free (buf);return 0 ; } + +int osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = {0 }; +if (s <0 ) +return -EINVAL ; + +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_mtu = mtu; + +int ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_linux.c +21 -0
@@ -40,3 +40,24 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)close (if_fd);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (PF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = { .ifr_mtu = mtu }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + +int ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_windows.c +5 -0
@@ -116,3 +116,8 @@ osdep_iface_mac_get(const char *device_name, struct rte_ether_addr *mac)free (info);return ret; } + +int osdep_iface_mtu_set(int index __rte_unused, uint16_t mtu __rte_unused) +{ +return -ENOTSUP ; +}
Signature · 1 line
2.51.0
PATCH v2 3/9 net/pcap: use bool for flags
In reply to Stephen HemmingerSave some space by using bool for flag values.
--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -38
@@ -5,6 +5,7 @@*/ #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <pcap.h>
@@ -91,9 +92,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -103,25 +104,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -858,7 +859,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1180,29 +1181,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1479,7 +1470,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1518,9 +1509,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
PATCH v2 4/9 net/pcap: support Tx offloads
In reply to Stephen HemmingerThe driver already handles multi-segment mbufs but did not report that in offload flags. Driver can easily insert vlan tag making testing easier.
--- drivers/net/pcap/pcap_ethdev.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/net/pcap/pcap_ethdev.c +8 -0
@@ -403,6 +403,9 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (len > mtu))continue ; +if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf)) +continue ; +calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -485,6 +488,9 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (len > mtu))continue ; +if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf)) +continue ; +/* rte_pktmbuf_read() returns a pointer to the data directly * in the mbuf (when the mbuf is contiguous) or, otherwise, * a pointer to temp_data after copying into it.
@@ -741,6 +747,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->min_rx_bufsize =0 ; dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ;return 0 ; }
Signature · 1 line
2.51.0
PATCH v2 5/9 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerConsistently support nanosecond timestamps across all the variations of pcap PMD receive.
--- drivers/net/pcap/pcap_ethdev.c | 142 +++++++++++++++++++++++++-------- 1 file changed, 109 insertions(+), 33 deletions(-)
drivers/net/pcap/pcap_ethdev.c +109 -33
@@ -19,13 +19,13 @@#include <rte_mbuf_dyn.h> #include <bus_vdev_driver.h> #include <rte_os_shim.h> +#include <rte_time.h> +#include <rte_reciprocal.h> #include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535#define RTE_ETH_PCAP_SNAPLEN (RTE_ETHER_MAX_JUMBO_FRAME_LEN - RTE_ETHER_CRC_LEN) -#define RTE_ETH_PCAP_PROMISC 1 -#define RTE_ETH_PCAP_TIMEOUT -1#define ETH_PCAP_RX_PCAP_ARG "rx_pcap"#define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
@@ -44,6 +44,7 @@ static char errbuf[PCAP_ERRBUF_SIZE];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; +static struct rte_reciprocal_u64 hz_inv;static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;
@@ -68,6 +69,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool timestamp_offloading;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -95,6 +97,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool timestamp_offloading; };struct pmd_process_private {
@@ -325,10 +328,20 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; -uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; - *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us; - mbuf->ol_flags |= timestamp_rx_dynflag; +if (pcap_q->timestamp_offloading) { +/* +* Although time stamp in struct pcap_pkthdr is defined as struct timeval, +* it really is a timespec with nanosecond resolution. +*/ +const struct timespec *ts = (struct timespec *)&header->ts; + + *RTE_MBUF_DYNFIELD (mbuf, timestamp_dynfield_offset, + rte_mbuf_timestamp_t *) = rte_timespec_to_ns(ts); + + mbuf->ol_flags |= timestamp_rx_dynflag; + } + mbuf->port = pcap_q->port_id; bufs[num_rx] = mbuf; num_rx++;
@@ -348,20 +361,21 @@ eth_null_rx(void *queue __rte_unused,return 0 ; } -#define NSEC_PER_SEC 1000000000L -/* * This function stores nanoseconds in `tv_usec` field of `struct timeval`, * because `ts` goes directly to nanosecond-precision dump. */ static inline void -calculate_timestamp (struct timeval *ts) { +calculate_timestamp (struct timeval *ts) +{uint64_t cycles;struct timespec cur_time; cycles =rte_get_timer_cycles () - start_cycles; - cur_time.tv_sec = cycles / hz; - cur_time.tv_nsec = (cycles % hz) *NSEC_PER_SEC / hz; + cur_time.tv_sec =rte_reciprocal_divide_u64 (cycles, &hz_inv); +/* compute remainder */ + cycles -= cur_time.tv_sec * hz; + cur_time.tv_nsec =rte_reciprocal_divide_u64 (cycles *NS_PER_S , &hz_inv); ts->tv_sec = start_time.tv_sec + cur_time.tv_sec; ts->tv_usec = start_time.tv_nsec + cur_time.tv_nsec;
@@ -380,6 +394,7 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)unsigned int i;struct pmd_process_private *pp;struct pcap_tx_queue *dumper_q = queue; +struct pcap_pkthdr header;uint16_t num_tx =0 ;uint32_t tx_bytes =0 ;pcap_dumper_t *dumper;
@@ -392,13 +407,14 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (dumper ==NULL || nb_pkts ==0 ))return 0 ; -/* writes the nb_pkts packets to the previously opened pcap file -* dumper */ +/* all packets in burst have same timestamp */ +calculate_timestamp (&header.ts); + +/* writes the nb_pkts packets to the previously opened pcap file dumper */ for (i =0 ; i < nb_pkts; i++) {struct rte_mbuf *mbuf = bufs[i];size_t len =rte_pktmbuf_pkt_len (mbuf);uint8_t temp_data[RTE_ETH_PCAP_SNAPLEN ]; -struct pcap_pkthdr header;if (unlikely (len > mtu))continue ;
@@ -406,7 +422,6 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf))continue ; -calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -514,22 +529,57 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)* pcap_open_live wrapper function */ static inline int -open_iface_live (const char *iface,pcap_t **pcap) { - *pcap =pcap_open_live (iface,RTE_ETH_PCAP_SNAPLEN , -RTE_ETH_PCAP_PROMISC ,RTE_ETH_PCAP_TIMEOUT , errbuf); +open_iface_live (const char *iface,pcap_t **pcap) +{ +pcap_t *pc =pcap_create (iface, errbuf); +if (pc ==NULL ) { +PMD_LOG (ERR ,"Couldn't create %s: %s" , iface, errbuf); + goto error; + } -if (*pcap ==NULL ) { -PMD_LOG (ERR ,"Couldn't open %s: %s" , iface, errbuf); -return -1 ; +int status =pcap_set_tstamp_precision (pc,PCAP_TSTAMP_PRECISION_NANO ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to ns precision: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_immediate_mode (pc,1 ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to immediate mode: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_promisc (pc,1 ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to promiscious: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_snaplen (pc,RTE_ETH_PCAP_SNAPSHOT_LEN ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set snapshot length: %s" , + iface,pcap_statustostr (status)); + + status =pcap_activate (pc); +if (status <0 ) { +char *cp =pcap_geterr (pc); + +if (status ==PCAP_ERROR ) +PMD_LOG (ERR ,"%s: could not activate: %s" , iface, cp); +else +PMD_LOG (ERR ,"%s: %s (%s)" , iface,pcap_statustostr (status), cp); + goto error; } -if (pcap_setnonblock (*pcap,1 , errbuf)) { +if (pcap_setnonblock (pc,1 , errbuf)) {PMD_LOG (ERR ,"Couldn't set non-blocking on %s: %s" , iface, errbuf); -pcap_close (*pcap); -return -1 ; + goto error; } + *pcap = pc;return 0 ; + +error: +if (pc) +pcap_close (pc); +return -1 ; } static int
@@ -576,7 +626,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { - *pcap =pcap_open_offline (pcap_filename, errbuf); + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename, +PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {PMD_LOG (ERR ,"Couldn't open %s: %s" , pcap_filename, errbuf);
@@ -613,6 +664,15 @@ eth_dev_start(struct rte_eth_dev *dev)struct pcap_tx_queue *tx;struct pcap_rx_queue *rx; +if (internals->timestamp_offloading) { +int ret =rte_mbuf_dyn_rx_timestamp_register (×tamp_dynfield_offset, + ×tamp_rx_dynflag); +if (ret !=0 ) { +PMD_LOG (ERR ,"Failed to register Rx timestamp field/flag" ); +return ret; + } + } +/* Special iface case. Single pcap is open and shared between tx/rx. */ if (internals->single_iface) { tx = &internals->tx_queue[0 ];
@@ -728,8 +788,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->timestamp_offloading = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_TIMESTAMP );return 0 ; }
@@ -749,6 +814,7 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS |RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_TIMESTAMP ;return 0 ; }
@@ -896,6 +962,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; dev->data->rx_queues[rx_queue_id] = pcap_q; + pcap_q->timestamp_offloading = internals->timestamp_offloading;if (internals->infinite_rx) {struct pmd_process_private *pp;
@@ -1017,6 +1084,16 @@ eth_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)return 0 ; } +/* Timestamp values in receive packets from libpcap are in UTC */ +static int +eth_rx_clock(struct rte_eth_dev *dev __rte_unused, uint64_t *timestamp) +{ +struct timespec cur_time; + +timespec_get (&cur_time,TIME_UTC ); + *timestamp =rte_timespec_to_ns (&cur_time); +return 0 ; +}static const struct eth_dev_ops ops = { .dev_start = eth_dev_start,
@@ -1034,6 +1111,7 @@ static const struct eth_dev_ops ops = { .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset, + .read_clock = eth_rx_clock, }; static int
@@ -1434,15 +1512,13 @@ pmd_pcap_probe(struct rte_vdev_device *dev) name =rte_vdev_device_name (dev);PMD_LOG (INFO ,"Initializing pmd_pcap for %s" , name); -timespec_get (&start_time,TIME_UTC ); - start_cycles =rte_get_timer_cycles (); - hz =rte_get_timer_hz (); +/* Record info for timestamps on first probe */ +if (hz ==0 ) { +timespec_get (&start_time,TIME_UTC ); + start_cycles =rte_get_timer_cycles (); - ret =rte_mbuf_dyn_rx_timestamp_register (×tamp_dynfield_offset, - ×tamp_rx_dynflag); -if (ret !=0 ) { -PMD_LOG (ERR ,"Failed to register Rx timestamp field/flag" ); -return -1 ; + hz =rte_get_timer_hz (); + hz_inv =rte_reciprocal_value_u64 (hz); }if (rte_eal_process_type () ==RTE_PROC_SECONDARY ) {
Signature · 1 line
2.51.0
PATCH v2 6/9 net/pcap: remove global variables
In reply to Stephen HemmingerLocalize variables where possible.
--- drivers/net/pcap/pcap_ethdev.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +5 -2
@@ -40,12 +40,10 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz;static struct rte_reciprocal_u64 hz_inv; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -531,6 +529,8 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; +pcap_t *pc =pcap_create (iface, errbuf);if (pc ==NULL ) {PMD_LOG (ERR ,"Couldn't create %s: %s" , iface, errbuf);
@@ -626,6 +626,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1327,6 +1329,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v2 7/9 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -49,10 +49,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -826,11 +826,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0
PATCH v2 8/9 net/pcap: support MAC address set
In reply to Stephen HemmingerUse rte_ether_addr structure to avoid memcpy and void *.
When using pcap on a single interface, it is possible for driver to proxy the mac address set operation.
--- drivers/net/pcap/pcap_ethdev.c | 19 ++++++++++++++--- drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 23 ++++++++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 30 ++++++++++++++++++++++++++- 4 files changed, 69 insertions(+), 4 deletions(-)
drivers/net/pcap/pcap_ethdev.c +16 -3
@@ -1086,6 +1086,18 @@ eth_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)return 0 ; } +static int +eth_dev_macaddr_set (struct rte_eth_dev *dev,struct rte_ether_addr *addr) +{ +struct pmd_internals *internals = dev->data->dev_private; + +if (internals->single_iface) +return osdep_iface_mac_set (internals->if_index, addr); +else +return -ENOTSUP ; +} + +/* Timestamp values in receive packets from libpcap are in UTC */ static int eth_rx_clock(struct rte_eth_dev *dev __rte_unused, uint64_t *timestamp)
@@ -1110,6 +1122,7 @@ static const struct eth_dev_ops ops = { .rx_queue_stop = eth_rx_queue_stop, .tx_queue_stop = eth_tx_queue_stop, .link_update = eth_link_update, + .mac_addr_set = eth_dev_macaddr_set, .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset,
@@ -1357,9 +1370,9 @@ pmd_init_internals(struct rte_vdev_device *vdev,static int eth_pcap_update_mac (const char *if_name,struct rte_eth_dev *eth_dev, -const unsigned int numa_node) +const unsigned int numa_node) { -void *mac_addrs; +struct rte_ether_addr *mac_addrs;struct rte_ether_addr mac;if (osdep_iface_mac_get (if_name, &mac) <0 )
@@ -1370,7 +1383,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,return -1 ;PMD_LOG (INFO ,"Setting phy MAC for %s" , if_name); -rte_memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); +rte_ether_addr_copy (&mac, mac_addrs); eth_dev->data->mac_addrs = mac_addrs;return 0 ; }
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -14,6 +14,7 @@ extern int eth_pcap_logtype;int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +int osdep_iface_mac_set (int ifindex,const struct rte_ether_addr *mac);int osdep_iface_mtu_set (int index,uint16_t mtu);#endif
drivers/net/pcap/pcap_osdep_freebsd.c +23 -0
@@ -58,6 +58,29 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return 0 ; } +int +osdep_iface_mac_set (int ifindex,const struct rte_ether_addr *mac) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = {0 }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_addr.sa_family =AF_LINK ; +memcpy (ifr.ifr_addr.sa_data, mac,sizeof (*mac)); + +int ret =ioctl (s,SIOCSIFLLADDR , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +} +int osdep_iface_mtu_set (int ifindex,uint16_t mtu) {char ifname[IFNAMSIZ ];
drivers/net/pcap/pcap_osdep_linux.c +29 -1
@@ -4,13 +4,18 @@* All rights reserved. */ +#include <string.h> +#include <stdint.h> +#include <unistd.h> +#include <errno.h> #include <net/if.h> +#include <net/if_arp.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <unistd.h> #include <rte_memcpy.h> #include <rte_string_fns.h> +#include <rte_ether.h> #include "pcap_osdep.h"
@@ -41,6 +46,29 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return 0 ; } +int +osdep_iface_mac_set (int ifindex,const struct rte_ether_addr *mac) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = {0 }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_hwaddr.sa_family =ARPHRD_ETHER ; +memcpy (ifr.ifr_hwaddr.sa_data, mac,sizeof (*mac)); + +int ret =ioctl (s,SIOCSIFHWADDR , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +} +int osdep_iface_mtu_set (int ifindex,uint16_t mtu) {
Signature · 1 line
2.51.0
PATCH v2 9/9 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests basic operations, timestamps, jumbo frame, and multiple queues.
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 1846 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 1848 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +1846 -0
1,846 lines not on this page · raw message
Signature · 1 line
2.51.0
Re: [PATCH v2 0/9] pcap: cleanup pcap PMD and add test
In reply to Stephen HemmingerQuoted 18 lines · On Thu, 8 Jan 2026 17:16:17 -0800 Stephen Hemminger…
This is a set of enhancements and tests to the PCAP PMD. It started out when looking at the handling of timestamps then realized lots of other cleanups were needed here.
v2 - review feedback
- consolidate patchesStephen Hemminger (9):
net/pcap: avoid usingrte_mallocandrte_memcpynet/pcap: support MTU set
net/pcap: use bool for flags
net/pcap: support Tx offloads
net/pcap: support nanosecond timestamp precision net/pcap: remove global variables net/pcap: avoid use of volatile net/pcap: support MAC address set
test: add test for pcap PMD
Quoted 10 lines
app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 1846 +++++++++++++++++++++++++ drivers/net/pcap/pcap_ethdev.c | 353 +++-- drivers/net/pcap/pcap_osdep.h | 2 + drivers/net/pcap/pcap_osdep_freebsd.c | 60 +- drivers/net/pcap/pcap_osdep_linux.c | 51 +- drivers/net/pcap/pcap_osdep_windows.c | 5 + 7 files changed, 2184 insertions(+), 135 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Results of AI review with AGENTS.md are:
I'll start by reading the AGENTS.md file to understand the review criteria, then examine the patch bundle.Let me view the middle sections of the patch bundle to see all patches:Now I have a comprehensive view of the patch series. Let me provide the review based on the AGENTS.md criteria:
---
## DPDK Patch Review: net/pcap Driver Changes (v2, 9 patches)
### Patch Series Overview
| # | Subject | Status | |---|---------|--------| | 1/9 | net/pcap: avoid using rte_malloc and rte_memcpy | ✅ Clean | | 2/9 | net/pcap: support MTU set | ⚠️ Minor issues | | 3/9 | net/pcap: use bool for flags | ✅ Clean | | 4/9 | net/pcap: add VLAN insert offload | ✅ Clean | | 5/9 | net/pcap: support nanosecond timestamp precision | ✅ Clean | | 6/9 | net/pcap: localize variables | ✅ Clean | | 7/9 | net/pcap: avoid use of volatile | ✅ Clean | | 8/9 | net/pcap: support MAC address set | ⚠️ Minor issues | | 9/9 | test: add test for pcap PMD | ⚠️ Warnings |
---
### Detailed Review
#### **Patch 1/9: net/pcap: avoid using rte_malloc and rte_memcpy**
**Commit Message**: ✅ Pass
- Subject line: 49 chars (≤60 ✓)
- Correct component prefix net/pcap:
- Imperative mood, lowercase, no trailing period
- Signed-off-by present
**Code Changes**: ✅ Pass
- Simple, correct replacement of rte_malloc/rte_free with standard malloc/free
- Removes unnecessary includes <rte_malloc.h> and <rte_memcpy.h>
- Appropriate for FreeBSD osdep code that doesn't need hugepage allocations
---
#### **Patch 2/9: net/pcap: support MTU set**
**Commit Message**: ✅ Pass
**Code Review**: ⚠️ Warnings
1. **FreeBSD duplicate check** (line 489-491):
```c
int s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -errno;
struct ifreq ifr = { 0 };
if (s < 0) // <-- Dead code: already checked above
return -EINVAL;
``` This is dead code - s < 0 was already handled above. Remove the second check.
2. **Double blank line** (lines 1009-1010): Style issue - extra blank line before static const struct eth_dev_ops ops.
3. **Warning (should fix)**: The FreeBSD osdep_iface_mtu_set function does not close the socket on all error paths before the duplicate check.
---
#### **Patch 3/9: net/pcap: use bool for flags**
**Commit Message**: ✅ Pass
**Code Changes**: ✅ Pass
- Good cleanup converting unsigned int and int flags to bool
- Adds <stdbool.h> header appropriately
- Consolidates select_phy_mac and get_infinite_rx_arg into unified process_bool_flag
- Using bool for flags follows DPDK guidelines
---
#### **Patch 4/9: net/pcap: add VLAN insert offload**
**Commit Message**: ✅ Pass
**Code Changes**: ✅ Pass
- Adds <rte_vlan.h> header
- Correctly adds VLAN insert capability to both TX paths
- Reports capability in dev_info->tx_offload_capa
---
#### **Patch 5/9: net/pcap: support nanosecond timestamp precision**
**Commit Message**: ✅ Pass
**Code Changes**: ✅ Pass
- Good use of rte_reciprocal_u64 for efficient division
- Replaces pcap_open_live with pcap_create/pcap_activate pattern for more control
- Uses pcap_open_offline_with_tstamp_precision for nanosecond precision
- Adds read_clock op for timestamp synchronization
- Makes timestamp offloading configurable via rxmode.offloads
**Info**: Nice optimization using rte_reciprocal_divide_u64 to avoid expensive division operations in the fast path.
---
#### **Patch 6/9: net/pcap: localize variables**
**Commit Message**: ✅ Pass
- Has Acked-by: from reviewer ✓
**Code Changes**: ✅ Pass
- Moves errbuf to function-local scope
- Makes iface_idx static within function scope
- Good practice to reduce global state
---
#### **Patch 7/9: net/pcap: avoid use of volatile**
**Commit Message**: ✅ Pass
**Code Changes**: ✅ Pass
- Removes volatile keyword from statistics structures
- Changes unsigned long to uint64_t for consistency
- Rationale is sound: only one thread operates on a queue at a time in DPDK
---
#### **Patch 8/9: net/pcap: support MAC address set**
**Commit Message**: ✅ Pass
**Code Review**: ⚠️ Warnings
1. **Double blank line** (lines 1709-1711): Extra blank line after eth_dev_macaddr_set function.
2. **Linux code changes** (line 1803-1809): The header reordering is correct:
```c #include <string.h> #include <stdint.h> #include <unistd.h> #include <errno.h> #include <net/if.h> #include <net/if_arp.h> ```
This follows proper include ordering (system headers first).
---
#### **Patch 9/9: test: add test for pcap PMD**
**Commit Message**: ✅ Pass
- Subject test: prefix is correct for app/test
**Code Review**: ⚠️ Warnings
1. **SPDX/Copyright**: ✅ Correct format:
```c
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2026 Stephen Hemminger
*/
```2. **Include order issue** (lines 1977-1997): The "test.h" and "packet_burst_generator.h" should come **after** system and DPDK includes, not before. The correct order per AGENTS.md is:
- System/libc includes
- DPDK EAL includes
- DPDK misc library includes
- Application-specific includes
**Should be:** ```c #include <stdio.h> #include <stdlib.h> ... #include <rte_ethdev.h> ... #include "test.h" #include "packet_burst_generator.h" ```
3. **Meson.build style** (line 1955):
```python 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, ```
Double space after 'ethdev', - should be single space.
4. **Test registration** (line 3818): Uses REGISTER_FAST_TEST which is correct for the test framework.
5. **Line length**: All checked lines are within the 100-char limit ✓
---
### Summary
| Severity | Count | Issues | |----------|-------|--------| | **Error** | 0 | None | | **Warning** | 4 | Dead code in FreeBSD MTU (2/9), double blank lines (2/9, 8/9), include order in test (9/9), meson spacing (9/9) | | **Info** | 1 | Good use of reciprocal divide optimization (5/9) |
### Recommendations
1. **Patch 2/9**: Remove the dead code check if (s < 0) on line 490 of pcap_osdep_freebsd.c
2. **Patch 2/9, 8/9**: Remove extra blank lines to maintain single blank line between functions
3. **Patch 9/9**: Reorder includes in test_pmd_pcap.c to place "test.h" and "packet_burst_generator.h" after DPDK includes
4. **Patch 9/9**: Fix double space in meson.build
Overall, this is a well-structured patch series with good separation of concerns. The issues are minor style/cleanup items that don't affect functionality.
v3 cover net/pcap: improvements and test coverage
In reply to Stephen HemmingerThis series brings several improvements to the PCAP PMD:
- Update documentation to reflect actually supported features
- Code cleanups: remove unnecessary rte_malloc/rte_memcpy usage, convert flags to bool, remove global variables, avoid volatile
- Add MTU set support for single interface mode
- Add Tx offload support
- Support nanosecond timestamp precision
- Add comprehensive test suite
This was done via mix of new code and AI feedback.
Stephen Hemminger (9):
doc: update features for PCAP PMD net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: support MTU set
net/pcap: use bool for flags
net/pcap: support Tx offloads
net/pcap: support nanosecond timestamp precision net/pcap: remove global variables net/pcap: avoid use of volatile
test: add test for pcap PMD
app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 1801 +++++++++++++++++++++++++ doc/guides/nics/features/pcap.ini | 10 + drivers/net/pcap/pcap_ethdev.c | 336 +++-- drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 37 +- drivers/net/pcap/pcap_osdep_linux.c | 21 + drivers/net/pcap/pcap_osdep_windows.c | 6 + 8 files changed, 2083 insertions(+), 131 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v3 1/9 doc: update features for PCAP PMD
The PCAP PMD supports more features that were not flagged in the feature matrix.
--- doc/guides/nics/features/pcap.ini | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/nics/features/pcap.ini +7 -0
@@ -4,8 +4,15 @@ ; Refer to default.ini for the full list of available PMD features. ; [Features] +Link status = Y +Queue start/stop = Y +Scattered Rx = Y +Rx Timestamp = Y Basic stats = Y +Stats per queue = Y Multiprocess aware = Y +FreeBSD = Y +Linux = Y ARMv7 = Y ARMv8 = Y Power8 = Y
Signature · 1 line
2.51.0
PATCH v3 2/9 net/pcap: avoid using rte_malloc and rte_memcpy
In reply to Stephen HemmingerNo need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_osdep_freebsd.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_osdep_freebsd.c +4 -7
@@ -8,9 +8,6 @@#include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +38,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
Signature · 1 line
2.51.0
PATCH v3 3/9 net/pcap: support MTU set
In reply to Stephen HemmingerWhen used as single interface useful to support pass through of MTU setting to enable larger frames.
Cleanup the transmit logic so that if packet sent exceeds MTU it is counted as an error and packet is silently dropped.
--- doc/guides/nics/features/pcap.ini | 1 + drivers/net/pcap/pcap_ethdev.c | 93 ++++++++++++++------------- drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 26 ++++++++ drivers/net/pcap/pcap_osdep_linux.c | 21 ++++++ drivers/net/pcap/pcap_osdep_windows.c | 6 ++ 6 files changed, 105 insertions(+), 43 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -19,3 +19,4 @@ Power8 = Y x86-32 = Y x86-64 = Y Usage doc = Y +MTU update = Y
drivers/net/pcap/pcap_ethdev.c +50 -43
188 lines not on this page · raw message
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -14,5 +14,6 @@ extern int eth_pcap_logtype;int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +int osdep_iface_mtu_set (int index,uint16_t mtu);#endif
drivers/net/pcap/pcap_osdep_freebsd.c +26 -0
@@ -4,9 +4,12 @@* All rights reserved. */ +#include <unistd.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> +#include <sys/ioctl.h> +#include <sys/sockio.h> #include "pcap_osdep.h"
@@ -54,3 +57,26 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)free (buf);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +struct ifreq ifr = {0 }; +char ifname[IFNAMSIZ ]; +int s, ret; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + + s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_mtu = mtu; + + ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_linux.c +21 -0
@@ -40,3 +40,24 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)close (if_fd);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (PF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = { .ifr_mtu = mtu }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + +int ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_windows.c +6 -0
@@ -116,3 +116,9 @@ osdep_iface_mac_get(const char *device_name, struct rte_ether_addr *mac)free (info);return ret; } + +int +osdep_iface_mtu_set(int index __rte_unused, uint16_t mtu __rte_unused) +{ +return -ENOTSUP ; +}
Signature · 1 line
2.51.0
PATCH v3 4/9 net/pcap: use bool for flags
In reply to Stephen HemmingerSave some space by using bool for flag values.
--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -38
@@ -5,6 +5,7 @@*/ #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <pcap.h>
@@ -91,9 +92,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -103,25 +104,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -858,7 +859,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1179,29 +1180,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1478,7 +1469,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1517,9 +1508,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
PATCH v3 5/9 net/pcap: support Tx offloads
In reply to Stephen HemmingerThe driver already handles multi-segment mbufs but did not report that in offload flags. Driver can easily insert vlan tag making testing easier.
--- doc/guides/nics/features/pcap.ini | 1 + drivers/net/pcap/pcap_ethdev.c | 8 ++++++++ 2 files changed, 9 insertions(+)
doc/guides/nics/features/pcap.ini +1 -0
@@ -10,6 +10,7 @@ Scattered Rx = Y Rx Timestamp = Y Basic stats = Y Stats per queue = Y +VLAN offload = P Multiprocess aware = Y FreeBSD = Y Linux = Y
drivers/net/pcap/pcap_ethdev.c +8 -0
@@ -403,6 +403,9 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (len > mtu))continue ; +if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf)) +continue ; +calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -485,6 +488,9 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (len > mtu))continue ; +if ((mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) &&rte_vlan_insert (&mbuf)) +continue ; +/* rte_pktmbuf_read() returns a pointer to the data directly * in the mbuf (when the mbuf is contiguous) or, otherwise, * a pointer to temp_data after copying into it.
@@ -741,6 +747,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->min_rx_bufsize =0 ; dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ;return 0 ; }
Signature · 1 line
2.51.0
PATCH v3 6/9 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerConsistently support nanosecond timestamps across all the variations of pcap PMD receive.
--- doc/guides/nics/features/pcap.ini | 1 + drivers/net/pcap/pcap_ethdev.c | 146 +++++++++++++++++++++++------- 2 files changed, 114 insertions(+), 33 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -11,6 +11,7 @@ Rx Timestamp = Y Basic stats = Y Stats per queue = Y VLAN offload = P +Timestamp offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
drivers/net/pcap/pcap_ethdev.c +113 -33
282 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v3 7/9 net/pcap: remove global variables
In reply to Stephen HemmingerLocalize variables where possible.
--- drivers/net/pcap/pcap_ethdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -2
@@ -40,12 +40,10 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz;static struct rte_reciprocal_u64 hz_inv; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -531,6 +529,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ];pcap_t *pc;int status;
@@ -629,6 +628,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1330,6 +1331,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v3 8/9 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -49,10 +49,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -828,11 +828,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0
PATCH v3 9/9 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests basic operations, timestamps, jumbo frame, and multiple queues.
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 1801 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 1803 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +1801 -0
1,801 lines not on this page · raw message
Signature · 1 line
2.51.0
v4 cover PCAP PMD improvements
In reply to Stephen HemmingerThis series improves the PCAP PMD with new features, better code quality, and a comprehensive test suite.
Changes:
- Update feature matrix to reflect actual capabilities
- Replace unnecessary rte_malloc/rte_memcpy with standard libc
- Fix multi-segment transmit handling (was using 9KB stack buffer)
- Add MTU setting support for single interface mode
- Convert integer flags to bool for type safety
- Add VLAN strip (RX) and insert (TX) offload support
- Support nanosecond timestamp precision
- Localize global variables where possible
- Remove unnecessary volatile from statistics
- Add comprehensive unit test suite (18 test cases)
- Add release notes
The VLAN and timestamp offloads follow the same patterns used by virtio and af_packet PMDs.
The test suite covers basic TX/RX, varied packet sizes, jumbo frames, infinite RX mode, statistics, MTU configuration, timestamps, multi-queue operation, and VLAN offloads.
v4:
- Rebase on current main
- Add VLAN strip
- Add release note
- better multi-segment handling
Stephen Hemminger (11):
doc: update features for PCAP PMD net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: cleanup transmit of multi segment net/pcap: support setting MTU
net/pcap: use bool for flags
net/pcap: support VLAN offloads net/pcap: support nanosecond timestamp precision net/pcap: remove global variables net/pcap: avoid use of volatile
test: add test for pcap PMD
net/pcap: add release note
app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2263 ++++++++++++++++++++++++ doc/guides/nics/features/pcap.ini | 10 + doc/guides/rel_notes/release_26_03.rst | 7 + drivers/net/pcap/pcap_ethdev.c | 376 ++-- drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 37 +- drivers/net/pcap/pcap_osdep_linux.c | 21 + drivers/net/pcap/pcap_osdep_windows.c | 6 + 9 files changed, 2586 insertions(+), 137 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v4 1/11 doc: update features for PCAP PMD
The PCAP PMD supports more features that were not flagged in the feature matrix.
--- doc/guides/nics/features/pcap.ini | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/nics/features/pcap.ini +7 -0
@@ -4,8 +4,15 @@ ; Refer to default.ini for the full list of available PMD features. ; [Features] +Link status = Y +Queue start/stop = Y +Scattered Rx = Y +Rx Timestamp = Y Basic stats = Y +Stats per queue = Y Multiprocess aware = Y +FreeBSD = Y +Linux = Y ARMv7 = Y ARMv8 = Y Power8 = Y
Signature · 1 line
2.51.0
PATCH v4 2/11 net/pcap: avoid using rte_malloc and rte_memcpy
In reply to Stephen HemmingerNo need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_osdep_freebsd.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_osdep_freebsd.c +4 -7
@@ -8,9 +8,6 @@#include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +38,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
Signature · 1 line
2.51.0
PATCH v4 3/11 net/pcap: cleanup transmit of multi segment
In reply to Stephen HemmingerThe driver was not reporting multi segment in offload flags but it can handle it.
The logic for handling transmit would allocate a worst case size buffer on the stack which could be up to 9K. If packet was transmitted larger than that it would get truncated and log would get flooded.
Instead, allocate a buffer on stack only if necessary and gracefully handle errors by incrementing transmit error counter.
--- drivers/net/pcap/pcap_ethdev.c | 110 +++++++++++++++++---------------- 1 file changed, 58 insertions(+), 52 deletions(-)
drivers/net/pcap/pcap_ethdev.c +58 -52
@@ -22,7 +22,7 @@#include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535 -#define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN +#define RTE_ETH_PCAP_SNAPLEN (RTE_ETHER_MAX_JUMBO_FRAME_LEN - RTE_ETHER_CRC_LEN)#define RTE_ETH_PCAP_PROMISC 1#define RTE_ETH_PCAP_TIMEOUT -1
@@ -370,6 +370,22 @@ calculate_timestamp(struct timeval *ts) { } } +/* Like rte_pktmbuf_read() but allocate if needed */ +static inline const void * +pcap_pktmbuf_read (const struct rte_mbuf *m, +uint32_t off,uint32_t len,void **buf) +{ +if (likely (off + len <=rte_pktmbuf_data_len (m))) { + return rte_pktmbuf_mtod_offset(m, char *, off); + }else { + *buf =malloc (len); +if (likely (*buf !=NULL )) +return rte_pktmbuf_read (m, off, len, *buf); +else +return NULL ; + } +} +/* * Callback to handle writing packets to a pcap file. */
@@ -377,46 +393,43 @@ 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; +pcap_dumper_t *dumper;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; pp = rte_eth_devices[dumper_q->port_id].process_private; dumper = pp->tx_dumper[dumper_q->queue_id]; -if (dumper ==NULL || nb_pkts ==0 ) +if (unlikely (dumper ==NULL || nb_pkts ==0 ))return 0 ; -/* writes the nb_pkts packets to the previously opened pcap file -* dumper */ +/* 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]; +uint32_t len =rte_pktmbuf_pkt_len (mbuf); +void *temp =NULL ; +const uint8_t *data;calculate_timestamp (&header.ts); header.len = len; - header.caplen = caplen; -/* rte_pktmbuf_read() returns a pointer to the data directly -* in the mbuf (when the mbuf is contiguous) or, otherwise, -* a pointer to temp_data after copying into it. -*/ -pcap_dump ((u_char *)dumper, &header, -rte_pktmbuf_read (mbuf,0 , caplen, temp_data)); + header.caplen = len; + + data =pcap_pktmbuf_read (mbuf,0 , len, &temp); +if (unlikely (data ==NULL )) { + ++dumper_q->tx_stat.err_pkts; +continue ; + } + +pcap_dump ((u_char *)dumper, &header, data); num_tx++; - tx_bytes += caplen; -rte_pktmbuf_free (mbuf); + tx_bytes += len; +free (temp); } +rte_pktmbuf_free_bulk (bufs, nb_pkts);/* * Since there's no place to hook a callback when the forwarding
@@ -444,15 +457,15 @@ eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (nb_pkts ==0 ))return 0 ; -for (i =0 ; i < nb_pkts; i++) { +for (i =0 ; i < nb_pkts; i++) tx_bytes += bufs[i]->pkt_len; -rte_pktmbuf_free (bufs[i]); - } + +rte_pktmbuf_free_bulk (bufs, nb_pkts); tx_queue->tx_stat.pkts += nb_pkts; tx_queue->tx_stat.bytes += tx_bytes; -return i; +return nb_pkts; }/*
@@ -462,15 +475,11 @@ 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; pp = rte_eth_devices[tx_queue->port_id].process_private; pcap = pp->tx_pcap[tx_queue->queue_id];
@@ -479,35 +488,31 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)return 0 ;for (i =0 ; i < nb_pkts; i++) { - mbuf = bufs[i]; - len =rte_pktmbuf_pkt_len (mbuf); -if (unlikely (!rte_pktmbuf_is_contiguous (mbuf) && - len >sizeof (temp_data))) { -PMD_LOG (ERR , -"Dropping multi segment PCAP packet. Size (%zd) > max size (%zd)." , - len,sizeof (temp_data)); -rte_pktmbuf_free (mbuf); +struct rte_mbuf *mbuf = bufs[i]; +uint32_t len =rte_pktmbuf_pkt_len (mbuf); +void *temp =NULL ; +const uint8_t *data; + + data =pcap_pktmbuf_read (mbuf,0 , len, &temp); +if (unlikely (data ==NULL )) { + ++tx_queue->tx_stat.err_pkts;continue ; } -/* rte_pktmbuf_read() returns a pointer to the data directly -* in the mbuf (when the mbuf is contiguous) or, otherwise, -* a pointer to temp_data after copying into it. -*/ - ret =pcap_sendpacket (pcap, -rte_pktmbuf_read (mbuf,0 , len, temp_data), len); -if (unlikely (ret !=0 )) -break ; - num_tx++; - tx_bytes += len; -rte_pktmbuf_free (mbuf); +if (likely (pcap_sendpacket (pcap, data, len) ==0 )) { + num_tx++; + tx_bytes += len; + }else { + ++tx_queue->tx_stat.err_pkts; + } + } tx_queue->tx_stat.pkts += num_tx; tx_queue->tx_stat.bytes += tx_bytes; - tx_queue->tx_stat.err_pkts += i - num_tx; + tx_queue->tx_stat.err_pkts += nb_pkts - num_tx; -return i; +return nb_pkts; }/*
@@ -745,6 +750,7 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ;return 0 ; }
Signature · 1 line
2.51.0
PATCH v4 4/11 net/pcap: support setting MTU
In reply to Stephen HemmingerIf PCAP PMD is used in single interface mode, it can pass the MTU setup to the device. Only implemented on Linux and FreeBSD.
--- doc/guides/nics/features/pcap.ini | 1 + drivers/net/pcap/pcap_ethdev.c | 14 ++++++++++++++ drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 26 ++++++++++++++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 21 +++++++++++++++++++++ drivers/net/pcap/pcap_osdep_windows.c | 6 ++++++ 6 files changed, 69 insertions(+)
doc/guides/nics/features/pcap.ini +1 -0
@@ -19,3 +19,4 @@ Power8 = Y x86-32 = Y x86-64 = Y Usage doc = Y +MTU update = Y
drivers/net/pcap/pcap_ethdev.c +14 -0
@@ -750,6 +750,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; + dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ;return 0 ;
@@ -1008,6 +1010,17 @@ eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)return 0 ; } +static int +eth_mtu_set (struct rte_eth_dev *dev,uint16_t mtu) +{ +struct pmd_internals *internals = dev->data->dev_private; + +if (internals->single_iface) +return osdep_iface_mtu_set (internals->if_index, mtu); + +return 0 ; +} +static const struct eth_dev_ops ops = { .dev_start = eth_dev_start, .dev_stop = eth_dev_stop,
@@ -1021,6 +1034,7 @@ static const struct eth_dev_ops ops = { .rx_queue_stop = eth_rx_queue_stop, .tx_queue_stop = eth_tx_queue_stop, .link_update = eth_link_update, + .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset, };
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -14,5 +14,6 @@ extern int eth_pcap_logtype;int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +int osdep_iface_mtu_set (int index,uint16_t mtu);#endif
drivers/net/pcap/pcap_osdep_freebsd.c +26 -0
@@ -4,9 +4,12 @@* All rights reserved. */ +#include <unistd.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> +#include <sys/ioctl.h> +#include <sys/sockio.h> #include "pcap_osdep.h"
@@ -54,3 +57,26 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)free (buf);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +struct ifreq ifr = {0 }; +char ifname[IFNAMSIZ ]; +int s, ret; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + + s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_mtu = mtu; + + ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_linux.c +21 -0
@@ -40,3 +40,24 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)close (if_fd);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (PF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = { .ifr_mtu = mtu }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + +int ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_windows.c +6 -0
@@ -116,3 +116,9 @@ osdep_iface_mac_get(const char *device_name, struct rte_ether_addr *mac)free (info);return ret; } + +int +osdep_iface_mtu_set(int index __rte_unused, uint16_t mtu __rte_unused) +{ +return -ENOTSUP ; +}
Signature · 1 line
2.51.0
PATCH v4 5/11 net/pcap: use bool for flags
In reply to Stephen HemmingerSave some space by using bool for flag values.
--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -38
@@ -5,6 +5,7 @@*/ #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <pcap.h>
@@ -91,9 +92,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -103,25 +104,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -871,7 +872,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1192,29 +1193,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1491,7 +1482,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1530,9 +1521,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
RE: [PATCH v4 05/11] net/pcap: use bool for flags
Quoted 125 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Saturday 17 January 2026 21:57
To: [email protected]
Cc: Stephen Hemminger <[email protected]>
Subject: [PATCH v4 05/11] net/pcap: use bool for flagsSave some space by using bool for flag values.
Signed-off-by Stephen Hemminger <[email protected]>--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)drivers/net/pcap/pcap_ethdev.c +25
-34@@ -5,6 +5,7 @@*/ #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <pcap.h> @@ -91,9 +92,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {@@ -103,25 +104,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {@@ -871,7 +872,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;@@ -1192,29 +1193,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
drivers/net/pcap/pcap_ethdev.c pmd_pcap_probe(struct rte_vdev_device *dev) · line 1485
Quoted 5 lines
drivers/net/pcap/pcap_ethdev.c +1
-1@@ -1491,7 +1482,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);
Nit: there seems to be one unnecessary space and unnecessary indentation change in the above and some other lines.
Quoted 17 lines
drivers/net/pcap/pcap_ethdev.c +3
-4if (ret <0 ) goto free_kvlist;@@ -1530,9 +1521,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s", --
Quoted 1 line
2.51.0
Regardless of spacing issues,
PATCH v4 6/11 net/pcap: support VLAN offloads
In reply to Stephen HemmingerDriver can easily insert VLAN tag strip and insertion similar to how it is handled in virtio and af_packet.
--- doc/guides/nics/features/pcap.ini | 1 + drivers/net/pcap/pcap_ethdev.c | 33 ++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -10,6 +10,7 @@ Scattered Rx = Y Rx Timestamp = Y Basic stats = Y Stats per queue = Y +VLAN offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
drivers/net/pcap/pcap_ethdev.c +30 -3
@@ -68,6 +68,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool vlan_strip;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -95,6 +96,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool vlan_strip; };struct pmd_process_private {
@@ -325,6 +327,10 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; + +if (pcap_q->vlan_strip) +rte_vlan_strip (mbuf); +uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us;
@@ -414,6 +420,13 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) { + ++dumper_q->tx_stat.err_pkts; +continue ; + } + } +calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -494,6 +507,13 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) { + ++tx_queue->tx_stat.err_pkts; +continue ; + } + } + data =pcap_pktmbuf_read (mbuf,0 , len, &temp);if (unlikely (data ==NULL )) { ++tx_queue->tx_stat.err_pkts;
@@ -506,7 +526,6 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) }else { ++tx_queue->tx_stat.err_pkts; } - } tx_queue->tx_stat.pkts += num_tx;
@@ -734,8 +753,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->vlan_strip = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_VLAN_STRIP );return 0 ; }
@@ -753,7 +777,9 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->min_rx_bufsize =0 ; dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; - dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP ;return 0 ; }
@@ -900,6 +926,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->mb_pool = mb_pool; pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; + pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q;if (internals->infinite_rx) {
Signature · 1 line
2.51.0
PATCH v4 7/11 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerConsistently support nanosecond timestamps across all the variations of pcap PMD receive.
--- doc/guides/nics/features/pcap.ini | 1 + drivers/net/pcap/pcap_ethdev.c | 136 +++++++++++++++++++++++------- 2 files changed, 107 insertions(+), 30 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -11,6 +11,7 @@ Rx Timestamp = Y Basic stats = Y Stats per queue = Y VLAN offload = Y +Timestamp offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
drivers/net/pcap/pcap_ethdev.c +106 -30
261 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v4 8/11 net/pcap: remove global variables
In reply to Stephen HemmingerLocalize variables where possible.
--- drivers/net/pcap/pcap_ethdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -2
@@ -40,12 +40,10 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz;static struct rte_reciprocal_u64 hz_inv; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -556,6 +554,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ];pcap_t *pc;int status;
@@ -654,6 +653,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1358,6 +1359,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v4 9/11 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -49,10 +49,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -855,11 +855,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0
PATCH v4 10/11 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests basic operations, timestamps, jumbo frame, vlan handling and multiple queues
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2263 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 2265 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +2263 -0
2,263 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v4 11/11 net/pcap: add release note
In reply to Stephen HemmingerAdd a release note describing the new feature
and offload changes.
--- doc/guides/rel_notes/release_26_03.rst | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/rel_notes/release_26_03.rst +7 -0
@@ -55,6 +55,13 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Updated PCAP driver.** + + * Added support for setting MTU in single interface mode. + * Made receive timestamp offload optional. + * Supports VLAN insertion and stripping. + * Added unit test suite. + Removed Items -------------
Signature · 1 line
2.51.0
v5 cover PCAP PMD improvements
In reply to Stephen HemmingerThis series improves the PCAP PMD with new features, better code quality, and a comprehensive test suite.
Changes:
- Update feature matrix to reflect actual capabilities
- Replace unnecessary rte_malloc/rte_memcpy with standard libc
- Fix multi-segment transmit handling (was using 9KB stack buffer)
- Add MTU setting support for single interface mode
- Convert integer flags to bool for type safety
- Add VLAN strip (RX) and insert (TX) offload support
- Support nanosecond timestamp precision
- Localize global variables where possible
- Remove unnecessary volatile from statistics
- Add comprehensive unit test suite (18 test cases)
- Add release notes
The VLAN and timestamp offloads follow the same patterns used by virtio and af_packet PMDs.
The test suite covers basic TX/RX, varied packet sizes, jumbo frames, infinite RX mode, statistics, MTU configuration, timestamps, multi-queue operation, and VLAN offloads.
v5 - fix typo in features table
- resolve minor checkpatch complaint
Stephen Hemminger (11):
doc: update features for PCAP PMD net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: cleanup transmit of multi segment net/pcap: support setting MTU
net/pcap: use bool for flags
net/pcap: support VLAN offloads net/pcap: support nanosecond timestamp precision net/pcap: remove global variables net/pcap: avoid use of volatile
test: add test for pcap PMD
net/pcap: add release note
app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2263 ++++++++++++++++++++++++ doc/guides/nics/features/pcap.ini | 9 + doc/guides/rel_notes/release_26_03.rst | 7 + drivers/net/pcap/pcap_ethdev.c | 375 ++-- drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 37 +- drivers/net/pcap/pcap_osdep_linux.c | 21 + drivers/net/pcap/pcap_osdep_windows.c | 6 + 9 files changed, 2584 insertions(+), 137 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v5 1/11 doc: update features for PCAP PMD
The PCAP PMD supports more features that were not flagged in the feature matrix.
--- doc/guides/nics/features/pcap.ini | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/nics/features/pcap.ini +7 -0
@@ -4,8 +4,15 @@ ; Refer to default.ini for the full list of available PMD features. ; [Features] +Link status = Y +Queue start/stop = Y +Scattered Rx = Y +Timestamp offload = Y Basic stats = Y +Stats per queue = Y Multiprocess aware = Y +FreeBSD = Y +Linux = Y ARMv7 = Y ARMv8 = Y Power8 = Y
Signature · 1 line
2.51.0
PATCH v5 2/11 net/pcap: avoid using rte_malloc and rte_memcpy
In reply to Stephen HemmingerNo need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_osdep_freebsd.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_osdep_freebsd.c +4 -7
@@ -8,9 +8,6 @@#include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +38,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
Signature · 1 line
2.51.0
PATCH v5 3/11 net/pcap: cleanup transmit of multi segment
In reply to Stephen HemmingerThe driver was not reporting multi segment in offload flags but it can handle it.
The logic for handling transmit would allocate a worst case size buffer on the stack which could be up to 9K. If packet was transmitted larger than that it would get truncated and log would get flooded.
Instead, allocate a buffer on stack only if necessary and gracefully handle errors by incrementing transmit error counter.
--- drivers/net/pcap/pcap_ethdev.c | 109 +++++++++++++++++---------------- 1 file changed, 57 insertions(+), 52 deletions(-)
drivers/net/pcap/pcap_ethdev.c +57 -52
@@ -22,7 +22,7 @@#include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535 -#define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN +#define RTE_ETH_PCAP_SNAPLEN (RTE_ETHER_MAX_JUMBO_FRAME_LEN - RTE_ETHER_CRC_LEN)#define RTE_ETH_PCAP_PROMISC 1#define RTE_ETH_PCAP_TIMEOUT -1
@@ -370,6 +370,21 @@ calculate_timestamp(struct timeval *ts) { } } +/* Like rte_pktmbuf_read() but allocate if needed */ +static inline const void * +pcap_pktmbuf_read (const struct rte_mbuf *m, +uint32_t off,uint32_t len,void **buf) +{ +if (likely (off + len <=rte_pktmbuf_data_len (m))) + return rte_pktmbuf_mtod_offset(m, char *, off); + + *buf =malloc (len); +if (likely (*buf !=NULL )) +return rte_pktmbuf_read (m, off, len, *buf); +else +return NULL ; +} +/* * Callback to handle writing packets to a pcap file. */
@@ -377,46 +392,43 @@ 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; +pcap_dumper_t *dumper;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; pp = rte_eth_devices[dumper_q->port_id].process_private; dumper = pp->tx_dumper[dumper_q->queue_id]; -if (dumper ==NULL || nb_pkts ==0 ) +if (unlikely (dumper ==NULL || nb_pkts ==0 ))return 0 ; -/* writes the nb_pkts packets to the previously opened pcap file -* dumper */ +/* 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]; +uint32_t len =rte_pktmbuf_pkt_len (mbuf); +void *temp =NULL ; +const uint8_t *data;calculate_timestamp (&header.ts); header.len = len; - header.caplen = caplen; -/* rte_pktmbuf_read() returns a pointer to the data directly -* in the mbuf (when the mbuf is contiguous) or, otherwise, -* a pointer to temp_data after copying into it. -*/ -pcap_dump ((u_char *)dumper, &header, -rte_pktmbuf_read (mbuf,0 , caplen, temp_data)); + header.caplen = len; + + data =pcap_pktmbuf_read (mbuf,0 , len, &temp); +if (unlikely (data ==NULL )) { + ++dumper_q->tx_stat.err_pkts; +continue ; + } + +pcap_dump ((u_char *)dumper, &header, data); num_tx++; - tx_bytes += caplen; -rte_pktmbuf_free (mbuf); + tx_bytes += len; +free (temp); } +rte_pktmbuf_free_bulk (bufs, nb_pkts);/* * Since there's no place to hook a callback when the forwarding
@@ -444,15 +456,15 @@ eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (nb_pkts ==0 ))return 0 ; -for (i =0 ; i < nb_pkts; i++) { +for (i =0 ; i < nb_pkts; i++) tx_bytes += bufs[i]->pkt_len; -rte_pktmbuf_free (bufs[i]); - } + +rte_pktmbuf_free_bulk (bufs, nb_pkts); tx_queue->tx_stat.pkts += nb_pkts; tx_queue->tx_stat.bytes += tx_bytes; -return i; +return nb_pkts; }/*
@@ -462,15 +474,11 @@ 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; pp = rte_eth_devices[tx_queue->port_id].process_private; pcap = pp->tx_pcap[tx_queue->queue_id];
@@ -479,35 +487,31 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)return 0 ;for (i =0 ; i < nb_pkts; i++) { - mbuf = bufs[i]; - len =rte_pktmbuf_pkt_len (mbuf); -if (unlikely (!rte_pktmbuf_is_contiguous (mbuf) && - len >sizeof (temp_data))) { -PMD_LOG (ERR , -"Dropping multi segment PCAP packet. Size (%zd) > max size (%zd)." , - len,sizeof (temp_data)); -rte_pktmbuf_free (mbuf); +struct rte_mbuf *mbuf = bufs[i]; +uint32_t len =rte_pktmbuf_pkt_len (mbuf); +void *temp =NULL ; +const uint8_t *data; + + data =pcap_pktmbuf_read (mbuf,0 , len, &temp); +if (unlikely (data ==NULL )) { + ++tx_queue->tx_stat.err_pkts;continue ; } -/* rte_pktmbuf_read() returns a pointer to the data directly -* in the mbuf (when the mbuf is contiguous) or, otherwise, -* a pointer to temp_data after copying into it. -*/ - ret =pcap_sendpacket (pcap, -rte_pktmbuf_read (mbuf,0 , len, temp_data), len); -if (unlikely (ret !=0 )) -break ; - num_tx++; - tx_bytes += len; -rte_pktmbuf_free (mbuf); +if (likely (pcap_sendpacket (pcap, data, len) ==0 )) { + num_tx++; + tx_bytes += len; + }else { + ++tx_queue->tx_stat.err_pkts; + } + } tx_queue->tx_stat.pkts += num_tx; tx_queue->tx_stat.bytes += tx_bytes; - tx_queue->tx_stat.err_pkts += i - num_tx; + tx_queue->tx_stat.err_pkts += nb_pkts - num_tx; -return i; +return nb_pkts; }/*
@@ -745,6 +749,7 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ;return 0 ; }
Signature · 1 line
2.51.0
PATCH v5 4/11 net/pcap: support setting MTU
In reply to Stephen HemmingerIf PCAP PMD is used in single interface mode, it can pass the MTU setup to the device. Only implemented on Linux and FreeBSD.
--- doc/guides/nics/features/pcap.ini | 1 + drivers/net/pcap/pcap_ethdev.c | 14 ++++++++++++++ drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 26 ++++++++++++++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 21 +++++++++++++++++++++ drivers/net/pcap/pcap_osdep_windows.c | 6 ++++++ 6 files changed, 69 insertions(+)
doc/guides/nics/features/pcap.ini +1 -0
@@ -19,3 +19,4 @@ Power8 = Y x86-32 = Y x86-64 = Y Usage doc = Y +MTU update = Y
drivers/net/pcap/pcap_ethdev.c +14 -0
@@ -749,6 +749,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; + dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ;return 0 ;
@@ -1007,6 +1009,17 @@ eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)return 0 ; } +static int +eth_mtu_set (struct rte_eth_dev *dev,uint16_t mtu) +{ +struct pmd_internals *internals = dev->data->dev_private; + +if (internals->single_iface) +return osdep_iface_mtu_set (internals->if_index, mtu); + +return 0 ; +} +static const struct eth_dev_ops ops = { .dev_start = eth_dev_start, .dev_stop = eth_dev_stop,
@@ -1020,6 +1033,7 @@ static const struct eth_dev_ops ops = { .rx_queue_stop = eth_rx_queue_stop, .tx_queue_stop = eth_tx_queue_stop, .link_update = eth_link_update, + .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset, };
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -14,5 +14,6 @@ extern int eth_pcap_logtype;int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +int osdep_iface_mtu_set (int index,uint16_t mtu);#endif
drivers/net/pcap/pcap_osdep_freebsd.c +26 -0
@@ -4,9 +4,12 @@* All rights reserved. */ +#include <unistd.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> +#include <sys/ioctl.h> +#include <sys/sockio.h> #include "pcap_osdep.h"
@@ -54,3 +57,26 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)free (buf);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +struct ifreq ifr = {0 }; +char ifname[IFNAMSIZ ]; +int s, ret; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + + s =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + ifr.ifr_mtu = mtu; + + ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_linux.c +21 -0
@@ -40,3 +40,24 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)close (if_fd);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +char ifname[IFNAMSIZ ]; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + +int s =socket (PF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +struct ifreq ifr = { .ifr_mtu = mtu }; +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + +int ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_windows.c +6 -0
@@ -116,3 +116,9 @@ osdep_iface_mac_get(const char *device_name, struct rte_ether_addr *mac)free (info);return ret; } + +int +osdep_iface_mtu_set(int index __rte_unused, uint16_t mtu __rte_unused) +{ +return -ENOTSUP ; +}
Signature · 1 line
2.51.0
PATCH v5 5/11 net/pcap: use bool for flags
In reply to Stephen HemmingerSave some space by using bool for flag values.
--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -38
@@ -5,6 +5,7 @@*/ #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <pcap.h>
@@ -91,9 +92,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -103,25 +104,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -870,7 +871,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1191,29 +1192,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1490,7 +1481,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1529,9 +1520,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
PATCH v5 6/11 net/pcap: support VLAN offloads
In reply to Stephen HemmingerDriver can easily insert VLAN tag strip and insertion similar to how it is handled in virtio and af_packet.
--- doc/guides/nics/features/pcap.ini | 1 + drivers/net/pcap/pcap_ethdev.c | 33 ++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -10,6 +10,7 @@ Scattered Rx = Y Timestamp offload = Y Basic stats = Y Stats per queue = Y +VLAN offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
drivers/net/pcap/pcap_ethdev.c +30 -3
@@ -68,6 +68,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool vlan_strip;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -95,6 +96,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool vlan_strip; };struct pmd_process_private {
@@ -325,6 +327,10 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; + +if (pcap_q->vlan_strip) +rte_vlan_strip (mbuf); +uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us;
@@ -413,6 +419,13 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) { + ++dumper_q->tx_stat.err_pkts; +continue ; + } + } +calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -493,6 +506,13 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) { + ++tx_queue->tx_stat.err_pkts; +continue ; + } + } + data =pcap_pktmbuf_read (mbuf,0 , len, &temp);if (unlikely (data ==NULL )) { ++tx_queue->tx_stat.err_pkts;
@@ -505,7 +525,6 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) }else { ++tx_queue->tx_stat.err_pkts; } - } tx_queue->tx_stat.pkts += num_tx;
@@ -733,8 +752,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->vlan_strip = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_VLAN_STRIP );return 0 ; }
@@ -752,7 +776,9 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->min_rx_bufsize =0 ; dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; - dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP ;return 0 ; }
@@ -899,6 +925,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->mb_pool = mb_pool; pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; + pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q;if (internals->infinite_rx) {
Signature · 1 line
2.51.0
PATCH v5 7/11 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerConsistently support nanosecond timestamps across all the variations of pcap PMD receive.
--- drivers/net/pcap/pcap_ethdev.c | 136 +++++++++++++++++++++++++-------- 1 file changed, 106 insertions(+), 30 deletions(-)
drivers/net/pcap/pcap_ethdev.c +106 -30
@@ -19,13 +19,13 @@#include <rte_mbuf_dyn.h> #include <bus_vdev_driver.h> #include <rte_os_shim.h> +#include <rte_time.h> +#include <rte_reciprocal.h> #include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535#define RTE_ETH_PCAP_SNAPLEN (RTE_ETHER_MAX_JUMBO_FRAME_LEN - RTE_ETHER_CRC_LEN) -#define RTE_ETH_PCAP_PROMISC 1 -#define RTE_ETH_PCAP_TIMEOUT -1#define ETH_PCAP_RX_PCAP_ARG "rx_pcap"#define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
@@ -44,6 +44,7 @@ static char errbuf[PCAP_ERRBUF_SIZE];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; +static struct rte_reciprocal_u64 hz_inv;static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;
@@ -69,6 +70,7 @@ struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id;bool vlan_strip; +bool timestamp_offloading;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -97,6 +99,7 @@ struct pmd_internals {bool phy_mac;bool infinite_rx;bool vlan_strip; +bool timestamp_offloading; };struct pmd_process_private {
@@ -331,10 +334,19 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (pcap_q->vlan_strip)rte_vlan_strip (mbuf); -uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; +if (pcap_q->timestamp_offloading) { +/* +* Although time stamp in struct pcap_pkthdr is defined as struct timeval, +* it really is a timespec with nanosecond resolution. +*/ +const struct timespec *ts = (struct timespec *)&header->ts; + + *RTE_MBUF_DYNFIELD (mbuf, timestamp_dynfield_offset, + rte_mbuf_timestamp_t *) = rte_timespec_to_ns(ts); + + mbuf->ol_flags |= timestamp_rx_dynflag; + } - *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us; - mbuf->ol_flags |= timestamp_rx_dynflag; mbuf->port = pcap_q->port_id; bufs[num_rx] = mbuf; num_rx++;
@@ -354,20 +366,21 @@ eth_null_rx(void *queue __rte_unused,return 0 ; } -#define NSEC_PER_SEC 1000000000L -/* * This function stores nanoseconds in `tv_usec` field of `struct timeval`, * because `ts` goes directly to nanosecond-precision dump. */ static inline void -calculate_timestamp (struct timeval *ts) { +calculate_timestamp (struct timeval *ts) +{uint64_t cycles;struct timespec cur_time; cycles =rte_get_timer_cycles () - start_cycles; - cur_time.tv_sec = cycles / hz; - cur_time.tv_nsec = (cycles % hz) *NSEC_PER_SEC / hz; + cur_time.tv_sec =rte_reciprocal_divide_u64 (cycles, &hz_inv); +/* compute remainder */ + cycles -= cur_time.tv_sec * hz; + cur_time.tv_nsec =rte_reciprocal_divide_u64 (cycles *NS_PER_S , &hz_inv); ts->tv_sec = start_time.tv_sec + cur_time.tv_sec; ts->tv_usec = start_time.tv_nsec + cur_time.tv_nsec;
@@ -412,6 +425,9 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)if (unlikely (dumper ==NULL || nb_pkts ==0 ))return 0 ; +/* all packets in burst have same timestamp */ +calculate_timestamp (&header.ts); +/* writes the nb_pkts packets to the previously opened pcap file dumper */ for (i =0 ; i < nb_pkts; i++) {struct rte_mbuf *mbuf = bufs[i];
@@ -426,7 +442,6 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } } -calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -538,22 +553,60 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)* pcap_open_live wrapper function */ static inline int -open_iface_live (const char *iface,pcap_t **pcap) { - *pcap =pcap_open_live (iface,RTE_ETH_PCAP_SNAPLEN , -RTE_ETH_PCAP_PROMISC ,RTE_ETH_PCAP_TIMEOUT , errbuf); +open_iface_live (const char *iface,pcap_t **pcap) +{ +pcap_t *pc; +int status; -if (*pcap ==NULL ) { -PMD_LOG (ERR ,"Couldn't open %s: %s" , iface, errbuf); -return -1 ; + pc =pcap_create (iface, errbuf); +if (pc ==NULL ) { +PMD_LOG (ERR ,"Couldn't create %s: %s" , iface, errbuf); + goto error; + } + + status =pcap_set_tstamp_precision (pc,PCAP_TSTAMP_PRECISION_NANO ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to ns precision: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_immediate_mode (pc,1 ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to immediate mode: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_promisc (pc,1 ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set to promiscuous: %s" , + iface,pcap_statustostr (status)); + + status =pcap_set_snaplen (pc,RTE_ETH_PCAP_SNAPSHOT_LEN ); +if (status !=0 ) +PMD_LOG (WARNING ,"%s: Could not set snapshot length: %s" , + iface,pcap_statustostr (status)); + + status =pcap_activate (pc); +if (status <0 ) { +char *cp =pcap_geterr (pc); + +if (status ==PCAP_ERROR ) +PMD_LOG (ERR ,"%s: could not activate: %s" , iface, cp); +else +PMD_LOG (ERR ,"%s: %s (%s)" , iface,pcap_statustostr (status), cp); + goto error; } -if (pcap_setnonblock (*pcap,1 , errbuf)) { +if (pcap_setnonblock (pc,1 , errbuf)) {PMD_LOG (ERR ,"Couldn't set non-blocking on %s: %s" , iface, errbuf); -pcap_close (*pcap); -return -1 ; + goto error; } + *pcap = pc;return 0 ; + +error: +if (pc !=NULL ) +pcap_close (pc); +return -1 ; } static int
@@ -600,7 +653,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { - *pcap =pcap_open_offline (pcap_filename, errbuf); + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename, +PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {PMD_LOG (ERR ,"Couldn't open %s: %s" , pcap_filename, errbuf);
@@ -637,6 +691,15 @@ eth_dev_start(struct rte_eth_dev *dev)struct pcap_tx_queue *tx;struct pcap_rx_queue *rx; +if (internals->timestamp_offloading) { +int ret =rte_mbuf_dyn_rx_timestamp_register (×tamp_dynfield_offset, + ×tamp_rx_dynflag); +if (ret !=0 ) { +PMD_LOG (ERR ,"Failed to register Rx timestamp field/flag" ); +return ret; + } + } +/* Special iface case. Single pcap is open and shared between tx/rx. */ if (internals->single_iface) { tx = &internals->tx_queue[0 ];
@@ -759,6 +822,7 @@ eth_dev_configure(struct rte_eth_dev *dev)const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; internals->vlan_strip = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_VLAN_STRIP ); + internals->timestamp_offloading = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_TIMESTAMP );return 0 ; }
@@ -778,7 +842,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS |RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; - dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP | +RTE_ETH_RX_OFFLOAD_TIMESTAMP ;return 0 ; }
@@ -927,6 +992,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->queue_id = rx_queue_id; pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q; + pcap_q->timestamp_offloading = internals->timestamp_offloading;if (internals->infinite_rx) {struct pmd_process_private *pp;
@@ -1048,6 +1114,17 @@ eth_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)return 0 ; } +/* Timestamp values in receive packets from libpcap are in UTC */ +static int +eth_rx_clock(struct rte_eth_dev *dev __rte_unused, uint64_t *timestamp) +{ +struct timespec cur_time; + +timespec_get (&cur_time,TIME_UTC ); + *timestamp =rte_timespec_to_ns (&cur_time); +return 0 ; +} +static const struct eth_dev_ops ops = { .dev_start = eth_dev_start, .dev_stop = eth_dev_stop,
@@ -1064,6 +1141,7 @@ static const struct eth_dev_ops ops = { .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset, + .read_clock = eth_rx_clock, }; static int
@@ -1464,15 +1542,13 @@ pmd_pcap_probe(struct rte_vdev_device *dev) name =rte_vdev_device_name (dev);PMD_LOG (INFO ,"Initializing pmd_pcap for %s" , name); -timespec_get (&start_time,TIME_UTC ); - start_cycles =rte_get_timer_cycles (); - hz =rte_get_timer_hz (); +/* Record info for timestamps on first probe */ +if (hz ==0 ) { +timespec_get (&start_time,TIME_UTC ); + start_cycles =rte_get_timer_cycles (); - ret =rte_mbuf_dyn_rx_timestamp_register (×tamp_dynfield_offset, - ×tamp_rx_dynflag); -if (ret !=0 ) { -PMD_LOG (ERR ,"Failed to register Rx timestamp field/flag" ); -return -1 ; + hz =rte_get_timer_hz (); + hz_inv =rte_reciprocal_value_u64 (hz); }if (rte_eal_process_type () ==RTE_PROC_SECONDARY ) {
Signature · 1 line
2.51.0
PATCH v5 8/11 net/pcap: remove global variables
In reply to Stephen HemmingerLocalize variables where possible.
--- drivers/net/pcap/pcap_ethdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -2
@@ -40,12 +40,10 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz;static struct rte_reciprocal_u64 hz_inv; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -555,6 +553,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ];pcap_t *pc;int status;
@@ -653,6 +652,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1357,6 +1358,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v5 9/11 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -49,10 +49,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -854,11 +854,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0
PATCH v5 10/11 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests basic operations, timestamps, jumbo frame, vlan handling and multiple queues
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2263 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 2265 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +2263 -0
2,263 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v5 11/11 net/pcap: add release note
In reply to Stephen HemmingerAdd a release note describing the new feature
and offload changes.
--- doc/guides/rel_notes/release_26_03.rst | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/rel_notes/release_26_03.rst +7 -0
@@ -55,6 +55,13 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Updated PCAP driver.** + + * Added support for setting MTU in single interface mode. + * Made receive timestamp offload optional. + * Supports VLAN insertion and stripping. + * Added unit test suite. + Removed Items -------------
Signature · 1 line
2.51.0
v6 cover net/pcap: improvements and new features
In reply to Stephen HemmingerThis series modernizes the PCAP PMD with bug fixes, code cleanup, and new features.
**New features:**
- VLAN insert and strip offload support
- Nanosecond timestamp precision (using pcap_create/pcap_activate API)
- MTU configuration in single interface mode
- Advertise multi-segment TX capability
**Bug fixes:**
- Transmit now always consumes all packets, counting failures as errors rather than returning early (matches other PMD behavior)
- Remove silent truncation of oversized multi-segment packets
- Replace large stack buffers with dynamic allocation
**Code cleanup:**
- Add explicit header includes
- Remove unnecessary casts and rte_malloc/rte_memcpy usage
- Consolidate boolean flag parsing with input validation
- Reduce scope of file-level variables
- Replace volatile with proper atomics
**Other:**
- Update feature matrix documentation
- Add comprehensive unit test suite
- Take on maintainership
v6:
- Add maintainers patch
- Add patches for explicit header includes and removing unnecessary casts
- Fold release notes into individual patches
- Improve commit message subjects for clarity
---
Stephen Hemminger (13):
maintainers: update for pcap driver
doc: update features for PCAP PMD
net/pcap: include used headers
net/pcap: remove unnecessary casts
net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: improve multi-segment transmit handling net/pcap: support MTU configuration in single interface mode net/pcap: consolidate boolean flag handling net/pcap: support VLAN insert and strip
net/pcap: support nanosecond timestamp precision net/pcap: reduce scope of file-level variables net/pcap: avoid use of volatile
test: add test for pcap PMD
MAINTAINERS | 1 + app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2263 ++++++++++++++++++++++++ doc/guides/nics/features/pcap.ini | 9 + doc/guides/rel_notes/release_26_03.rst | 10 + drivers/net/pcap/pcap_ethdev.c | 383 ++-- drivers/net/pcap/pcap_osdep.h | 2 + drivers/net/pcap/pcap_osdep_freebsd.c | 36 +- drivers/net/pcap/pcap_osdep_linux.c | 28 +- drivers/net/pcap/pcap_osdep_windows.c | 6 + 10 files changed, 2591 insertions(+), 149 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v6 1/13 maintainers: update for pcap driver
Nominate myself to take care of this since already doing pcapng and pdump code.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -1118,6 +1118,7 @@ F: doc/guides/nics/zxdh.rst F: doc/guides/nics/features/zxdh.ini PCAP PMD +M: Stephen Hemminger <[email protected]> F: drivers/net/pcap/ F: doc/guides/nics/pcap_ring.rst F: doc/guides/nics/features/pcap.ini
Signature · 1 line
2.51.0
PATCH v6 2/13 doc: update features for PCAP PMD
In reply to Stephen HemmingerThe PCAP PMD supports more features that were not flagged in the feature matrix.
--- doc/guides/nics/features/pcap.ini | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/nics/features/pcap.ini +7 -0
@@ -4,8 +4,15 @@ ; Refer to default.ini for the full list of available PMD features. ; [Features] +Link status = Y +Queue start/stop = Y +Scattered Rx = Y +Timestamp offload = Y Basic stats = Y +Stats per queue = Y Multiprocess aware = Y +FreeBSD = Y +Linux = Y ARMv7 = Y ARMv8 = Y Power8 = Y
Signature · 1 line
2.51.0
PATCH v6 3/13 net/pcap: include used headers
In reply to Stephen HemmingerInclude the used headers instead of relying on getting the headers indirectly through other headers.
--- drivers/net/pcap/pcap_ethdev.c | 8 ++++++++ drivers/net/pcap/pcap_osdep.h | 1 + 2 files changed, 9 insertions(+)
drivers/net/pcap/pcap_ethdev.c +8 -0
@@ -4,16 +4,24 @@* All rights reserved. */ +#include <stdio.h> #include <stdlib.h> #include <time.h> +#include <inttypes.h> +#include <errno.h> +#include <sys/time.h> +#include <sys/types.h> #include <pcap.h> #include <rte_cycles.h> +#include <rte_ring.h> +#include <rte_ethdev.h> #include <ethdev_driver.h> #include <ethdev_vdev.h> #include <rte_kvargs.h> #include <rte_malloc.h> +#include <rte_memcpy.h> #include <rte_mbuf.h> #include <rte_mbuf_dyn.h> #include <bus_vdev_driver.h>
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -6,6 +6,7 @@#define _RTE_PCAP_OSDEP_#include <rte_ether.h> +#include <rte_log.h> #define PMD_LOG (level, ...) \ RTE_LOG_LINE_PREFIX(level, ETH_PCAP, "%s(): ", __func__, __VA_ARGS__)
Signature · 1 line
2.51.0
PATCH v6 4/13 net/pcap: remove unnecessary casts
In reply to Stephen HemmingerThe function rte_zmalloc returns void * so cast is unnecessary.
Correct the indentation in that code. Not really worth backporting.
--- drivers/net/pcap/pcap_ethdev.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -7
@@ -1221,9 +1221,8 @@ pmd_init_internals(struct rte_vdev_device *vdev,PMD_LOG (INFO ,"Creating pcap-backed ethdev on numa socket %d" , numa_node); - pp = (struct pmd_process_private *) -rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
@@ -1591,10 +1590,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)unsigned int i; internal = eth_dev->data->dev_private; - pp = (struct pmd_process_private *) -rte_zmalloc (NULL , -sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
Signature · 1 line
2.51.0
PATCH v6 5/13 net/pcap: avoid using rte_malloc and rte_memcpy
In reply to Stephen HemmingerNo need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_ethdev.c | 2 +- drivers/net/pcap/pcap_osdep_freebsd.c | 12 +++++------- drivers/net/pcap/pcap_osdep_linux.c | 6 +++--- 3 files changed, 9 insertions(+), 11 deletions(-)
drivers/net/pcap/pcap_ethdev.c +1 -1
@@ -1289,7 +1289,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,return -1 ;PMD_LOG (INFO ,"Setting phy MAC for %s" , if_name); -rte_memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); +memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); eth_dev->data->mac_addrs = mac_addrs;return 0 ; }
drivers/net/pcap/pcap_osdep_freebsd.c +5 -7
@@ -4,13 +4,11 @@* All rights reserved. */ +#include <string.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +39,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
drivers/net/pcap/pcap_osdep_linux.c +3 -3
@@ -4,12 +4,12 @@* All rights reserved. */ +#include <string.h> +#include <unistd.h> #include <net/if.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <unistd.h> -#include <rte_memcpy.h> #include <rte_string_fns.h> #include "pcap_osdep.h"
@@ -35,7 +35,7 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return -1 ; } -rte_memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN );close (if_fd);return 0 ;
Signature · 1 line
2.51.0
PATCH v6 6/13 net/pcap: improve multi-segment transmit handling
In reply to Stephen HemmingerAdvertise RTE_ETH_TX_OFFLOAD_MULTI_SEGS capability in device info. The driver already handles multi-segment mbufs but was not reporting this.
Replace the fixed-size stack buffer (up to 9K) with dynamic allocation only when the mbuf is non-contiguous. This avoids large stack usage and removes the silent truncation that occurred when packets exceeded the buffer size.
Change transmit functions to always consume and free all packets, returning nb_pkts regardless of send success. The DPDK transmit API interprets a return value less than requested as "queue full, retry later." However, pcap_sendpacket() failures (bad parameters or socket errors) are not transient and retrying would fail again. The correct behavior is to free failed packets and increment the error counter rather than leaving mbufs for the application to retry.
Also use rte_pktmbuf_free_bulk() for more efficient buffer freeing.
--- doc/guides/rel_notes/release_26_03.rst | 5 ++ drivers/net/pcap/pcap_ethdev.c | 106 ++++++++++++------------- 2 files changed, 56 insertions(+), 55 deletions(-)
doc/guides/rel_notes/release_26_03.rst +5 -0
@@ -55,6 +55,11 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Updated PCAP ethernet driver.** + + * Changed transmit burst to always return the number of packets requested. + Failed sends are counted as transmit errors. + Removed Items -------------
drivers/net/pcap/pcap_ethdev.c +51 -55
181 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v6 7/13 net/pcap: support MTU configuration in single interface mode
In reply to Stephen HemmingerImplement the mtu_set dev_op to allow MTU changes when the pcap PMD is attached to a real network interface (single interface mode). The MTU change is passed through to the underlying device via ioctl.
This is supported on Linux and FreeBSD. On Windows, the operation returns -ENOTSUP (for now).
Also report min_mtu and max_mtu in device info.
--- doc/guides/nics/features/pcap.ini | 1 + doc/guides/rel_notes/release_26_03.rst | 1 + drivers/net/pcap/pcap_ethdev.c | 14 ++++++++++++++ drivers/net/pcap/pcap_osdep.h | 1 + drivers/net/pcap/pcap_osdep_freebsd.c | 24 ++++++++++++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 22 ++++++++++++++++++++++ drivers/net/pcap/pcap_osdep_windows.c | 6 ++++++ 7 files changed, 69 insertions(+)
doc/guides/nics/features/pcap.ini +1 -0
@@ -19,3 +19,4 @@ Power8 = Y x86-32 = Y x86-64 = Y Usage doc = Y +MTU update = Y
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -57,6 +57,7 @@ New Features * **Updated PCAP ethernet driver.** + * Added support for setting MTU in single interface mode. * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors.
drivers/net/pcap/pcap_ethdev.c +14 -0
@@ -748,6 +748,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; + dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ;return 0 ;
@@ -1006,6 +1008,17 @@ eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)return 0 ; } +static int +eth_mtu_set (struct rte_eth_dev *dev,uint16_t mtu) +{ +struct pmd_internals *internals = dev->data->dev_private; + +if (internals->single_iface) +return osdep_iface_mtu_set (internals->if_index, mtu); + +return 0 ; +} +static const struct eth_dev_ops ops = { .dev_start = eth_dev_start, .dev_stop = eth_dev_stop,
@@ -1019,6 +1032,7 @@ static const struct eth_dev_ops ops = { .rx_queue_stop = eth_rx_queue_stop, .tx_queue_stop = eth_tx_queue_stop, .link_update = eth_link_update, + .mtu_set = eth_mtu_set, .stats_get = eth_stats_get, .stats_reset = eth_stats_reset, };
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -15,5 +15,6 @@ extern int eth_pcap_logtype;int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +int osdep_iface_mtu_set (int index,uint16_t mtu);#endif
drivers/net/pcap/pcap_osdep_freebsd.c +24 -0
@@ -8,6 +8,8 @@#include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> +#include <sys/ioctl.h> +#include <sys/sockio.h> #include "pcap_osdep.h"
@@ -55,3 +57,25 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)free (buf);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +struct ifreq ifr = { .ifr_mtu = mtu }; +char ifname[IFNAMSIZ ]; +int s, ret; + +if (if_indextoname (ifindex, ifname) ==NULL ) +return -errno; + + s =socket (PF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + +strlcpy (ifr.ifr_name, ifname,IFNAMSIZ ); + + ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_linux.c +22 -0
@@ -40,3 +40,25 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)close (if_fd);return 0 ; } + +int +osdep_iface_mtu_set (int ifindex,uint16_t mtu) +{ +struct ifreq ifr = { .ifr_mtu = mtu }; +char if_name[IFNAMSIZ ]; +int s, ret; + +if (if_indextoname (ifindex, if_name) ==NULL ) +return -errno; + +rte_strscpy (ifr.ifr_name, if_name,sizeof (ifr.ifr_name)); + + s =socket (PF_INET ,SOCK_DGRAM ,0 ); +if (s <0 ) +return -errno; + + ret =ioctl (s,SIOCSIFMTU , &ifr); +close (s); + +return (ret <0 ) ? -errno :0 ; +}
drivers/net/pcap/pcap_osdep_windows.c +6 -0
@@ -116,3 +116,9 @@ osdep_iface_mac_get(const char *device_name, struct rte_ether_addr *mac)free (info);return ret; } + +int +osdep_iface_mtu_set(int index __rte_unused, uint16_t mtu __rte_unused) +{ +return -ENOTSUP ; +}
Signature · 1 line
2.51.0
PATCH v6 8/13 net/pcap: consolidate boolean flag handling
In reply to Stephen HemmingerConvert internal flag fields from int/unsigned int to bool for clarity and reduced structure size.
Merge the separate select_phy_mac() and get_infinite_rx_arg() functions into a single process_bool_flag() handler. The new function also adds proper validation, rejecting values other than "0", "1", or empty (which defaults to true).
Also change num_of_queue from unsigned int to uint16_t since it cannot exceed RTE_PMD_PCAP_MAX_QUEUES.
--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -38
@@ -6,6 +6,7 @@#include <stdio.h> #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <inttypes.h> #include <errno.h>
@@ -99,9 +100,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -111,25 +112,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -869,7 +870,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1190,29 +1191,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1488,7 +1479,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1527,9 +1518,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
PATCH v6 9/13 net/pcap: support VLAN insert and strip
In reply to Stephen HemmingerDriver can easily insert VLAN tag strip and insertion similar to how it is handled in virtio and af_packet.
--- doc/guides/nics/features/pcap.ini | 1 + doc/guides/rel_notes/release_26_03.rst | 1 + drivers/net/pcap/pcap_ethdev.c | 30 ++++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -10,6 +10,7 @@ Scattered Rx = Y Timestamp offload = Y Basic stats = Y Stats per queue = Y +VLAN offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -58,6 +58,7 @@ New Features * **Updated PCAP ethernet driver.** * Added support for setting MTU in single interface mode. + * Added support for VLAN insertion and stripping. * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors.
drivers/net/pcap/pcap_ethdev.c +28 -2
@@ -76,6 +76,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool vlan_strip;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -103,6 +104,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool vlan_strip; };struct pmd_process_private {
@@ -333,6 +335,10 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; + +if (pcap_q->vlan_strip) +rte_vlan_strip (mbuf); +uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us;
@@ -421,6 +427,12 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + } +calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -498,6 +510,12 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + } + data =pcap_pktmbuf_read (mbuf,0 , len, &temp);if (likely (data !=NULL &&pcap_sendpacket (pcap, data, len) ==0 )) {
@@ -732,8 +750,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->vlan_strip = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_VLAN_STRIP );return 0 ; }
@@ -751,7 +774,9 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->min_rx_bufsize =0 ; dev_info->min_mtu =RTE_ETHER_MIN_LEN -RTE_ETHER_HDR_LEN -RTE_ETHER_CRC_LEN ; dev_info->max_mtu =RTE_ETH_PCAP_SNAPLEN ; - dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP ;return 0 ; }
@@ -898,6 +923,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->mb_pool = mb_pool; pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; + pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q;if (internals->infinite_rx) {
Signature · 1 line
2.51.0
PATCH v6 10/13 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerEnable nanosecond-precision timestamps for both live capture and pcap file reading.
Replace pcap_open_live() with the pcap_create()/pcap_activate() API, which allows setting PCAP_TSTAMP_PRECISION_NANO before activation. Similarly, use pcap_open_offline_with_tstamp_precision() for reading pcap files. The pcap_pkthdr timestamp field, despite being declared as struct timeval, actually contains nanoseconds (not microseconds) when nanosecond precision is requested.
Make receive timestamp offloading conditional: timestamps are now only written to the mbuf dynamic field when RTE_ETH_RX_OFFLOAD_TIMESTAMP is enabled. Previously, timestamps were unconditionally added to every received packet.
Other related changes:
* Defer timestamp dynfield registration from probe to device start, and only when timestamp offloading is enabled
* Add read_clock dev_op returning current UTC time
for timestamp correlation
* Move per-burst timestamp calculation outside the packet loop in tx_dumper
* Enable immediate mode and improve error reporting
in live capture setup
--- doc/guides/rel_notes/release_26_03.rst | 2 + drivers/net/pcap/pcap_ethdev.c | 129 +++++++++++++++++++------ 2 files changed, 102 insertions(+), 29 deletions(-)
doc/guides/rel_notes/release_26_03.rst +2 -0
@@ -59,6 +59,8 @@ New Features * Added support for setting MTU in single interface mode. * Added support for VLAN insertion and stripping. + * Receive timestamp offload is only done if offload flag set. + * Receive timestamps support nanosecond precision. * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors.
drivers/net/pcap/pcap_ethdev.c +100 -29
243 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v6 11/13 net/pcap: reduce scope of file-level variables
In reply to Stephen HemmingerMove errbuf from file scope to local variables in the two functions that use it (open_iface_live and open_single_rx_pcap). This avoids potential issues if these functions were called concurrently, since each call now has its own error buffer. Move iface_idx to a static local variable within pmd_init_internals(), the only function that uses it. The variable remains static to preserve the MAC address uniqueness counter across calls.
--- drivers/net/pcap/pcap_ethdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -2
@@ -47,11 +47,9 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -550,6 +548,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ];pcap_t *pc;int status;
@@ -648,6 +647,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1351,6 +1352,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v6 12/13 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -55,10 +55,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -849,11 +849,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0
PATCH v6 13/13 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests basic operations, timestamps, jumbo frame, vlan handling and multiple queues
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2263 ++++++++++++++++++++++++ doc/guides/rel_notes/release_26_03.rst | 1 + 3 files changed, 2266 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +2263 -0
2,263 lines not on this page · raw message
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -63,6 +63,7 @@ New Features * Receive timestamps support nanosecond precision. * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors. + * Added unit test suite. Removed Items
Signature · 1 line
2.51.0
v7 cover net/pcap: improvements and test suite
In reply to Stephen HemmingerThis series contains improvements to the PCAP PMD including new features, bug fixes, code cleanup, and a comprehensive test suite.
New features:
- VLAN tag insertion on Tx and stripping on Rx
- Nanosecond precision timestamps (when hardware/libpcap supports it)
- Accurate link state, speed, and duplex reporting in interface mode
- Advertise RTE_ETH_TX_OFFLOAD_MULTI_SEGS capability
Bug fixes:
- Fix multi-segment transmit to dynamically allocate instead of silently truncating packets larger than 9K stack buffer
- Change Tx burst to always consume all packets; failed sends increment error counter rather than leaving mbufs for retry (pcap_sendpacket failures are not transient)
Code cleanup:
- Convert internal flags from int to bool
- Remove unnecessary casts of void* from rte_zmalloc
- Replace rte_malloc/rte_memcpy with libc equivalents in osdep code
- Include headers explicitly rather than relying on indirect includes
- Remove unnecessary volatile qualifier on statistics
- Reduce scope of file-level variables
Testing:
- Add comprehensive unit test suite covering basic operations, timestamps, jumbo frames, VLAN handling, multi-queue, and more
v7:
- Drop MTU configuration patch; not necessary as underlying OS commands (ip link, ifconfig) can be used directly
- Add patch to remove unnecessary volatile on queue statistics
- Add test_link_status to unit test suite
- Add Acked-by from Marat Khalili on patch 11
v6:
- Rebase to latest main
- Address review comments
Stephen Hemminger (13):
maintainers: update for pcap driver
doc: update features for PCAP PMD
net/pcap: include used headers
net/pcap: remove unnecessary casts
net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: improve multi-segment transmit handling net/pcap: consolidate boolean flag handling net/pcap: support VLAN insert and strip net/pcap: add link state and speed for interface mode net/pcap: support nanosecond timestamp precision net/pcap: reduce scope of file-level variables net/pcap: avoid use of volatile
test: add test for pcap PMD
MAINTAINERS | 1 + app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2334 ++++++++++++++++++++++++ doc/guides/nics/features/pcap.ini | 8 + doc/guides/rel_notes/release_26_03.rst | 9 + drivers/net/pcap/pcap_ethdev.c | 474 +++-- drivers/net/pcap/pcap_osdep.h | 23 + drivers/net/pcap/pcap_osdep_freebsd.c | 288 ++- drivers/net/pcap/pcap_osdep_linux.c | 118 +- drivers/net/pcap/pcap_osdep_windows.c | 95 +- 10 files changed, 3180 insertions(+), 172 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v7 1/13 maintainers: update for pcap driver
Nominate myself to take care of this since already doing pcapng and pdump code.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -1118,6 +1118,7 @@ F: doc/guides/nics/zxdh.rst F: doc/guides/nics/features/zxdh.ini PCAP PMD +M: Stephen Hemminger <[email protected]> F: drivers/net/pcap/ F: doc/guides/nics/pcap_ring.rst F: doc/guides/nics/features/pcap.ini
Signature · 1 line
2.51.0
PATCH v7 2/13 doc: update features for PCAP PMD
In reply to Stephen HemmingerThe PCAP PMD supports more features that were not flagged in the feature matrix.
--- doc/guides/nics/features/pcap.ini | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/nics/features/pcap.ini +7 -0
@@ -4,8 +4,15 @@ ; Refer to default.ini for the full list of available PMD features. ; [Features] +Link status = Y +Queue start/stop = Y +Scattered Rx = Y +Timestamp offload = Y Basic stats = Y +Stats per queue = Y Multiprocess aware = Y +FreeBSD = Y +Linux = Y ARMv7 = Y ARMv8 = Y Power8 = Y
Signature · 1 line
2.51.0
PATCH v7 3/13 net/pcap: include used headers
In reply to Stephen HemmingerInclude the used headers instead of relying on getting the headers indirectly through other headers.
--- drivers/net/pcap/pcap_ethdev.c | 8 ++++++++ drivers/net/pcap/pcap_osdep.h | 1 + 2 files changed, 9 insertions(+)
drivers/net/pcap/pcap_ethdev.c +8 -0
@@ -4,16 +4,24 @@* All rights reserved. */ +#include <stdio.h> #include <stdlib.h> #include <time.h> +#include <inttypes.h> +#include <errno.h> +#include <sys/time.h> +#include <sys/types.h> #include <pcap.h> #include <rte_cycles.h> +#include <rte_ring.h> +#include <rte_ethdev.h> #include <ethdev_driver.h> #include <ethdev_vdev.h> #include <rte_kvargs.h> #include <rte_malloc.h> +#include <rte_memcpy.h> #include <rte_mbuf.h> #include <rte_mbuf_dyn.h> #include <bus_vdev_driver.h>
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -6,6 +6,7 @@#define _RTE_PCAP_OSDEP_#include <rte_ether.h> +#include <rte_log.h> #define PMD_LOG (level, ...) \ RTE_LOG_LINE_PREFIX(level, ETH_PCAP, "%s(): ", __func__, __VA_ARGS__)
Signature · 1 line
2.51.0
PATCH v7 4/13 net/pcap: remove unnecessary casts
In reply to Stephen HemmingerThe function rte_zmalloc returns void * so cast is unnecessary.
Correct the indentation in that code. Not really worth backporting.
--- drivers/net/pcap/pcap_ethdev.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -7
@@ -1221,9 +1221,8 @@ pmd_init_internals(struct rte_vdev_device *vdev,PMD_LOG (INFO ,"Creating pcap-backed ethdev on numa socket %d" , numa_node); - pp = (struct pmd_process_private *) -rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
@@ -1591,10 +1590,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)unsigned int i; internal = eth_dev->data->dev_private; - pp = (struct pmd_process_private *) -rte_zmalloc (NULL , -sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
Signature · 1 line
2.51.0
PATCH v7 5/13 net/pcap: avoid using rte_malloc and rte_memcpy
In reply to Stephen HemmingerNo need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_ethdev.c | 2 +- drivers/net/pcap/pcap_osdep_freebsd.c | 12 +++++------- drivers/net/pcap/pcap_osdep_linux.c | 6 +++--- 3 files changed, 9 insertions(+), 11 deletions(-)
drivers/net/pcap/pcap_ethdev.c +1 -1
@@ -1289,7 +1289,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,return -1 ;PMD_LOG (INFO ,"Setting phy MAC for %s" , if_name); -rte_memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); +memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); eth_dev->data->mac_addrs = mac_addrs;return 0 ; }
drivers/net/pcap/pcap_osdep_freebsd.c +5 -7
@@ -4,13 +4,11 @@* All rights reserved. */ +#include <string.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +39,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
drivers/net/pcap/pcap_osdep_linux.c +3 -3
@@ -4,12 +4,12 @@* All rights reserved. */ +#include <string.h> +#include <unistd.h> #include <net/if.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <unistd.h> -#include <rte_memcpy.h> #include <rte_string_fns.h> #include "pcap_osdep.h"
@@ -35,7 +35,7 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return -1 ; } -rte_memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN );close (if_fd);return 0 ;
Signature · 1 line
2.51.0
PATCH v7 6/13 net/pcap: improve multi-segment transmit handling
In reply to Stephen HemmingerAdvertise RTE_ETH_TX_OFFLOAD_MULTI_SEGS capability in device info. The driver already handles multi-segment mbufs but was not reporting this.
Replace the fixed-size stack buffer (up to 9K) with dynamic allocation only when the mbuf is non-contiguous. This avoids large stack usage and removes the silent truncation that occurred when packets exceeded the buffer size.
Change transmit functions to always consume and free all packets, returning nb_pkts regardless of send success. The DPDK transmit API interprets a return value less than requested as "queue full, retry later." However, pcap_sendpacket() failures (bad parameters or socket errors) are not transient and retrying would fail again. The correct behavior is to free failed packets and increment the error counter rather than leaving mbufs for the application to retry.
Also use rte_pktmbuf_free_bulk() for more efficient buffer freeing.
--- doc/guides/rel_notes/release_26_03.rst | 5 ++ drivers/net/pcap/pcap_ethdev.c | 106 ++++++++++++------------- 2 files changed, 56 insertions(+), 55 deletions(-)
doc/guides/rel_notes/release_26_03.rst +5 -0
@@ -55,6 +55,11 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Updated PCAP ethernet driver.** + + * Changed transmit burst to always return the number of packets requested. + Failed sends are counted as transmit errors. + Removed Items -------------
drivers/net/pcap/pcap_ethdev.c +51 -55
181 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v7 7/13 net/pcap: consolidate boolean flag handling
In reply to Stephen HemmingerConvert internal flag fields from int/unsigned int to bool for clarity and reduced structure size.
Merge the separate select_phy_mac() and get_infinite_rx_arg() functions into a single process_bool_flag() handler. The new function also adds proper validation, rejecting values other than "0", "1", or empty (which defaults to true).
Also change num_of_queue from unsigned int to uint16_t since it cannot exceed RTE_PMD_PCAP_MAX_QUEUES.
--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -38
@@ -6,6 +6,7 @@#include <stdio.h> #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <inttypes.h> #include <errno.h>
@@ -99,9 +100,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -111,25 +112,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -867,7 +868,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1176,29 +1177,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1474,7 +1465,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1513,9 +1504,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
PATCH v7 8/13 net/pcap: support VLAN insert and strip
In reply to Stephen HemmingerDriver can easily insert VLAN tag strip and insertion similar to how it is handled in virtio and af_packet.
--- doc/guides/nics/features/pcap.ini | 1 + doc/guides/rel_notes/release_26_03.rst | 1 + drivers/net/pcap/pcap_ethdev.c | 30 ++++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -10,6 +10,7 @@ Scattered Rx = Y Timestamp offload = Y Basic stats = Y Stats per queue = Y +VLAN offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -59,6 +59,7 @@ New Features * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors. + * Added support for VLAN insertion and stripping. Removed Items
drivers/net/pcap/pcap_ethdev.c +28 -2
@@ -76,6 +76,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool vlan_strip;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -103,6 +104,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool vlan_strip; };struct pmd_process_private {
@@ -333,6 +335,10 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; + +if (pcap_q->vlan_strip) +rte_vlan_strip (mbuf); +uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us;
@@ -421,6 +427,12 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + } +calculate_timestamp (&header.ts); header.len = len; header.caplen = len;
@@ -498,6 +510,12 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + } + data =pcap_pktmbuf_read (mbuf,0 , len, &temp);if (likely (data !=NULL &&pcap_sendpacket (pcap, data, len) ==0 )) {
@@ -732,8 +750,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->vlan_strip = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_VLAN_STRIP );return 0 ; }
@@ -749,7 +772,9 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; - dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP ;return 0 ; }
@@ -896,6 +921,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->mb_pool = mb_pool; pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; + pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q;if (internals->infinite_rx) {
Signature · 1 line
2.51.0
PATCH v7 9/13 net/pcap: add link state and speed for interface mode
In reply to Stephen HemmingerWhen the PCAP PMD is used in pass-through mode with a physical interface (iface=X), the link status was always reported with hardcoded values regardless of the actual interface state.
Add OS-dependent functions to query the real link state, speed, duplex, and autonegotiation settings from the underlying interface. The eth_link_update() callback now returns accurate information when operating in pass-through mode.
Linux uses ETHTOOL_GLINKSETTINGS which supports all speeds up to 800 Gbps. FreeBSD uses SIOCGIFMEDIA, and Windows uses GetAdaptersAddresses().
For pcap file mode or separate rx/tx interface configurations, default values continue to be used since there is no single underlying interface to query.
--- drivers/net/pcap/pcap_ethdev.c | 105 +++++++++- drivers/net/pcap/pcap_osdep.h | 22 ++ drivers/net/pcap/pcap_osdep_freebsd.c | 276 ++++++++++++++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 112 +++++++++++ drivers/net/pcap/pcap_osdep_windows.c | 95 +++++++-- 5 files changed, 587 insertions(+), 23 deletions(-)
drivers/net/pcap/pcap_ethdev.c +94 -11
@@ -147,13 +147,6 @@ static const char *valid_arguments[] = {NULL }; -static struct rte_eth_link pmd_link = { - .link_speed =RTE_ETH_SPEED_NUM_10G , - .link_duplex =RTE_ETH_LINK_FULL_DUPLEX , - .link_status =RTE_ETH_LINK_DOWN , - .link_autoneg =RTE_ETH_LINK_FIXED , -}; -RTE_LOG_REGISTER_DEFAULT (eth_pcap_logtype,NOTICE ); static struct queue_missed_stat*
@@ -900,11 +893,96 @@ eth_dev_close(struct rte_eth_dev *dev)return 0 ; } +/* +* Convert osdep speed (Mbps) to rte_eth_link speed constant. +*/ +static uint32_t +speed_mbps_to_rte (uint32_t speed_mbps) +{ +switch (speed_mbps) { +case 10 : +return RTE_ETH_SPEED_NUM_10M ; +case 100 : +return RTE_ETH_SPEED_NUM_100M ; +case 1000 : +return RTE_ETH_SPEED_NUM_1G ; +case 2500 : +return RTE_ETH_SPEED_NUM_2_5G ; +case 5000 : +return RTE_ETH_SPEED_NUM_5G ; +case 10000 : +return RTE_ETH_SPEED_NUM_10G ; +case 20000 : +return RTE_ETH_SPEED_NUM_20G ; +case 25000 : +return RTE_ETH_SPEED_NUM_25G ; +case 40000 : +return RTE_ETH_SPEED_NUM_40G ; +case 50000 : +return RTE_ETH_SPEED_NUM_50G ; +case 56000 : +return RTE_ETH_SPEED_NUM_56G ; +case 100000 : +return RTE_ETH_SPEED_NUM_100G ; +case 200000 : +return RTE_ETH_SPEED_NUM_200G ; +case 400000 : +return RTE_ETH_SPEED_NUM_400G ; +case 800000 : +return RTE_ETH_SPEED_NUM_800G ; +default : +/* For unknown speeds, return the raw value */ +if (speed_mbps >0 ) +return speed_mbps; +return RTE_ETH_SPEED_NUM_UNKNOWN ; + } +} +static int -eth_link_update(struct rte_eth_dev *dev __rte_unused, - int wait_to_complete __rte_unused) +eth_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused) { -return 0 ; +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_link link; +struct osdep_iface_link osdep_link; +const char *iface_name; + +memset (&link,0 ,sizeof (link)); + +/* +* For pass-through mode (single_iface), query the actual interface. +* Otherwise, use the default static link values. +*/ +if (internals->single_iface) { + iface_name = internals->rx_queue[0 ].name; + +if (osdep_iface_link_get (iface_name, &osdep_link) ==0 ) { + link.link_speed =speed_mbps_to_rte (osdep_link.link_speed); + link.link_status = osdep_link.link_status ? +RTE_ETH_LINK_UP :RTE_ETH_LINK_DOWN ; + link.link_duplex = osdep_link.link_duplex ? +RTE_ETH_LINK_FULL_DUPLEX :RTE_ETH_LINK_HALF_DUPLEX ; + link.link_autoneg = osdep_link.link_autoneg ? +RTE_ETH_LINK_AUTONEG :RTE_ETH_LINK_FIXED ; + }else { +/* Query failed, use defaults */ + link.link_speed =RTE_ETH_SPEED_NUM_10G ; + link.link_duplex =RTE_ETH_LINK_FULL_DUPLEX ; + link.link_status =RTE_ETH_LINK_DOWN ; + link.link_autoneg =RTE_ETH_LINK_FIXED ; + } + }else { +/* +* Not in pass-through mode (using pcap files or separate +* interfaces for rx/tx). Use default values. +*/ + link.link_speed =RTE_ETH_SPEED_NUM_10G ; + link.link_duplex =RTE_ETH_LINK_FULL_DUPLEX ; + link.link_status = dev->data->dev_started ? +RTE_ETH_LINK_UP :RTE_ETH_LINK_DOWN ; + link.link_autoneg =RTE_ETH_LINK_FIXED ; + } + +return rte_eth_linkstatus_set (dev, &link); } static int
@@ -1269,7 +1347,12 @@ pmd_init_internals(struct rte_vdev_device *vdev, data = (*eth_dev)->data; data->nb_rx_queues = (uint16_t )nb_rx_queues; data->nb_tx_queues = (uint16_t )nb_tx_queues; - data->dev_link = pmd_link; + data->dev_link = (struct rte_eth_link ) { + .link_speed =RTE_ETH_SPEED_NUM_NONE , + .link_duplex =RTE_ETH_LINK_FULL_DUPLEX , + .link_status =RTE_ETH_LINK_DOWN , + .link_autoneg =RTE_ETH_LINK_FIXED , + }; data->mac_addrs = &(*internals)->eth_addr; data->promiscuous =1 ; data->all_multicast =1 ;
drivers/net/pcap/pcap_osdep.h +22 -0
@@ -13,7 +13,29 @@extern int eth_pcap_logtype;#define RTE_LOGTYPE_ETH_PCAP eth_pcap_logtype +/** +* Link information returned by osdep_iface_link_get(). +*/ +struct osdep_iface_link { +uint32_t link_speed;/**< Speed in Mbps, 0 if unknown */ +uint8_t link_status;/**< 1 = up, 0 = down */ +uint8_t link_duplex;/**< 1 = full, 0 = half */ +uint8_t link_autoneg;/**< 1 = autoneg enabled, 0 = fixed */ +}; +int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +/** +* Get link state and speed for a network interface. +* +* @param name +* Interface name (e.g., "eth0" on Linux, "{GUID}" on Windows). +* @param link +* Pointer to structure to fill with link information. +* @return +* 0 on success, -1 on failure. +*/ +int osdep_iface_link_get (const char *name,struct osdep_iface_link *link); +#endif
drivers/net/pcap/pcap_osdep_freebsd.c +276 -0
287 lines not on this page · raw message
drivers/net/pcap/pcap_osdep_linux.c +112 -0
121 lines not on this page · raw message
drivers/net/pcap/pcap_osdep_windows.c +83 -12
124 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v7 10/13 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerEnable nanosecond-precision timestamps for both live capture and pcap file reading.
Replace pcap_open_live() with the pcap_create()/pcap_activate() API, which allows setting PCAP_TSTAMP_PRECISION_NANO before activation. Similarly, use pcap_open_offline_with_tstamp_precision() for reading pcap files. The pcap_pkthdr timestamp field, despite being declared as struct timeval, actually contains nanoseconds (not microseconds) when nanosecond precision is requested.
Make receive timestamp offloading conditional: timestamps are now only written to the mbuf dynamic field when RTE_ETH_RX_OFFLOAD_TIMESTAMP is enabled. Previously, timestamps were unconditionally added to every received packet.
Other related changes:
* Defer timestamp dynfield registration from probe to device start, and only when timestamp offloading is enabled
* Add read_clock dev_op returning current UTC time
for timestamp correlation
* Move per-burst timestamp calculation outside the packet loop in tx_dumper
* Enable immediate mode and improve error reporting
in live capture setup
--- doc/guides/rel_notes/release_26_03.rst | 2 + drivers/net/pcap/pcap_ethdev.c | 129 +++++++++++++++++++------ 2 files changed, 102 insertions(+), 29 deletions(-)
doc/guides/rel_notes/release_26_03.rst +2 -0
@@ -60,6 +60,8 @@ New Features * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors. * Added support for VLAN insertion and stripping. + * Receive timestamp offload is only done if offload flag set. + * Receive timestamps support nanosecond precision. Removed Items
drivers/net/pcap/pcap_ethdev.c +100 -29
243 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v7 11/13 net/pcap: reduce scope of file-level variables
In reply to Stephen HemmingerMove errbuf from file scope to local variables in the two functions that use it (open_iface_live and open_single_rx_pcap). This avoids potential issues if these functions were called concurrently, since each call now has its own error buffer. Move iface_idx to a static local variable within pmd_init_internals(), the only function that uses it. The variable remains static to preserve the MAC address uniqueness counter across calls.
--- drivers/net/pcap/pcap_ethdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -2
@@ -47,11 +47,9 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -543,6 +541,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ];pcap_t *pc;int status;
@@ -641,6 +640,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1415,6 +1416,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v7 12/13 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -55,10 +55,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -840,11 +840,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0
PATCH v7 13/13 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests basic operations, timestamps, jumbo frame, vlan handling and multiple queues
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2334 ++++++++++++++++++++++++ doc/guides/rel_notes/release_26_03.rst | 1 + 3 files changed, 2337 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +2334 -0
2,334 lines not on this page · raw message
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -62,6 +62,7 @@ New Features * Added support for VLAN insertion and stripping. * Receive timestamp offload is only done if offload flag set. * Receive timestamps support nanosecond precision. + * Added unit test suite. Removed Items
Signature · 1 line
2.51.0
v8 cover net/pcap: improvements and test suite
In reply to Stephen HemmingerThis series contains improvements to the PCAP PMD including new features, bug fixes, code cleanup, and a comprehensive test suite.
New features:
- VLAN tag insertion on Tx and stripping on Rx
- Nanosecond precision timestamps (when hardware/libpcap supports it)
- Accurate link state, speed, and duplex reporting in interface mode
- Advertise RTE_ETH_TX_OFFLOAD_MULTI_SEGS capability
Bug fixes:
- Fix multi-segment transmit to dynamically allocate instead of silently truncating packets larger than 9K stack buffer
- Change Tx burst to always consume all packets; failed sends increment error counter rather than leaving mbufs for retry (pcap_sendpacket failures are not transient)
Code cleanup:
- Convert internal flags from int to bool
- Remove unnecessary casts of void* from rte_zmalloc
- Replace rte_malloc/rte_memcpy with libc equivalents in osdep code
- Include headers explicitly rather than relying on indirect includes
- Remove unnecessary volatile qualifier on statistics
- Reduce scope of file-level variables
Testing:
- Add comprehensive unit test suite covering basic operations, timestamps, jumbo frames, VLAN handling, multi-queue, and more
v8:
- Fix pcap header length in VLAN transmit
- Fix clang warnings from ethtool link_mode array
v7:
- Drop MTU configuration patch; not necessary as underlying OS commands (ip link, ifconfig) can be used directly
- Add patch to remove unnecessary volatile on queue statistics
- Add test_link_status to unit test suite
- Add Acked-by from Marat Khalili on patch 11
v6:
- Rebase to latest main
Stephen Hemminger (14):
maintainers: update for pcap driver
doc: update features for PCAP PMD
net/pcap: include used headers
net/pcap: remove unnecessary casts
net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: improve multi-segment transmit handling net/pcap: consolidate boolean flag handling net/pcap: support VLAN insert and strip net/pcap: add link state and speed for interface mode net/pcap: support nanosecond timestamp precision net/pcap: reduce scope of file-level variables net/pcap: avoid use of volatile
net/pcap: clarify maximum received packet
test: add test for pcap PMD
MAINTAINERS | 1 + app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2337 ++++++++++++++++++++++++ doc/guides/nics/features/pcap.ini | 8 + doc/guides/rel_notes/release_26_03.rst | 9 + drivers/net/pcap/pcap_ethdev.c | 474 +++-- drivers/net/pcap/pcap_osdep.h | 23 + drivers/net/pcap/pcap_osdep_freebsd.c | 289 ++- drivers/net/pcap/pcap_osdep_linux.c | 115 +- drivers/net/pcap/pcap_osdep_windows.c | 95 +- 10 files changed, 3180 insertions(+), 173 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v8 1/14 maintainers: update for pcap driver
Nominate myself to take care of this since already doing pcapng and pdump code.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -1118,6 +1118,7 @@ F: doc/guides/nics/zxdh.rst F: doc/guides/nics/features/zxdh.ini PCAP PMD +M: Stephen Hemminger <[email protected]> F: drivers/net/pcap/ F: doc/guides/nics/pcap_ring.rst F: doc/guides/nics/features/pcap.ini
Signature · 1 line
2.51.0
PATCH v8 2/14 doc: update features for PCAP PMD
In reply to Stephen HemmingerThe PCAP PMD supports more features that were not flagged in the feature matrix.
--- doc/guides/nics/features/pcap.ini | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/nics/features/pcap.ini +7 -0
@@ -4,8 +4,15 @@ ; Refer to default.ini for the full list of available PMD features. ; [Features] +Link status = Y +Queue start/stop = Y +Scattered Rx = Y +Timestamp offload = Y Basic stats = Y +Stats per queue = Y Multiprocess aware = Y +FreeBSD = Y +Linux = Y ARMv7 = Y ARMv8 = Y Power8 = Y
Signature · 1 line
2.51.0
PATCH v8 3/14 net/pcap: include used headers
In reply to Stephen HemmingerInclude the used headers instead of relying on getting the headers indirectly through other headers.
--- drivers/net/pcap/pcap_ethdev.c | 8 ++++++++ drivers/net/pcap/pcap_osdep.h | 1 + 2 files changed, 9 insertions(+)
drivers/net/pcap/pcap_ethdev.c +8 -0
@@ -4,16 +4,24 @@* All rights reserved. */ +#include <stdio.h> #include <stdlib.h> #include <time.h> +#include <inttypes.h> +#include <errno.h> +#include <sys/time.h> +#include <sys/types.h> #include <pcap.h> #include <rte_cycles.h> +#include <rte_ring.h> +#include <rte_ethdev.h> #include <ethdev_driver.h> #include <ethdev_vdev.h> #include <rte_kvargs.h> #include <rte_malloc.h> +#include <rte_memcpy.h> #include <rte_mbuf.h> #include <rte_mbuf_dyn.h> #include <bus_vdev_driver.h>
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -6,6 +6,7 @@#define _RTE_PCAP_OSDEP_#include <rte_ether.h> +#include <rte_log.h> #define PMD_LOG (level, ...) \ RTE_LOG_LINE_PREFIX(level, ETH_PCAP, "%s(): ", __func__, __VA_ARGS__)
Signature · 1 line
2.51.0
PATCH v8 4/14 net/pcap: remove unnecessary casts
In reply to Stephen HemmingerThe function rte_zmalloc returns void * so cast is unnecessary.
Correct the indentation in that code. Not really worth backporting.
--- drivers/net/pcap/pcap_ethdev.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -7
@@ -1221,9 +1221,8 @@ pmd_init_internals(struct rte_vdev_device *vdev,PMD_LOG (INFO ,"Creating pcap-backed ethdev on numa socket %d" , numa_node); - pp = (struct pmd_process_private *) -rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
@@ -1591,10 +1590,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)unsigned int i; internal = eth_dev->data->dev_private; - pp = (struct pmd_process_private *) -rte_zmalloc (NULL , -sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
Signature · 1 line
2.51.0
PATCH v8 5/14 net/pcap: avoid using rte_malloc and rte_memcpy
In reply to Stephen HemmingerNo need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_ethdev.c | 2 +- drivers/net/pcap/pcap_osdep_freebsd.c | 12 +++++------- drivers/net/pcap/pcap_osdep_linux.c | 6 +++--- 3 files changed, 9 insertions(+), 11 deletions(-)
drivers/net/pcap/pcap_ethdev.c +1 -1
@@ -1289,7 +1289,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,return -1 ;PMD_LOG (INFO ,"Setting phy MAC for %s" , if_name); -rte_memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); +memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); eth_dev->data->mac_addrs = mac_addrs;return 0 ; }
drivers/net/pcap/pcap_osdep_freebsd.c +5 -7
@@ -4,13 +4,11 @@* All rights reserved. */ +#include <string.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +39,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
drivers/net/pcap/pcap_osdep_linux.c +3 -3
@@ -4,12 +4,12 @@* All rights reserved. */ +#include <string.h> +#include <unistd.h> #include <net/if.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <unistd.h> -#include <rte_memcpy.h> #include <rte_string_fns.h> #include "pcap_osdep.h"
@@ -35,7 +35,7 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return -1 ; } -rte_memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN );close (if_fd);return 0 ;
Signature · 1 line
2.51.0
PATCH v8 6/14 net/pcap: improve multi-segment transmit handling
In reply to Stephen HemmingerAdvertise RTE_ETH_TX_OFFLOAD_MULTI_SEGS capability in device info. The driver already handles multi-segment mbufs but was not reporting this.
Replace the fixed-size stack buffer (up to 9K) with dynamic allocation only when the mbuf is non-contiguous. This avoids large stack usage and removes the silent truncation that occurred when packets exceeded the buffer size.
Change transmit functions to always consume and free all packets, returning nb_pkts regardless of send success. The DPDK transmit API interprets a return value less than requested as "queue full, retry later." However, pcap_sendpacket() failures (bad parameters or socket errors) are not transient and retrying would fail again. The correct behavior is to free failed packets and increment the error counter rather than leaving mbufs for the application to retry.
Also use rte_pktmbuf_free_bulk() for more efficient buffer freeing.
--- doc/guides/rel_notes/release_26_03.rst | 5 ++ drivers/net/pcap/pcap_ethdev.c | 105 ++++++++++++------------- 2 files changed, 55 insertions(+), 55 deletions(-)
doc/guides/rel_notes/release_26_03.rst +5 -0
@@ -55,6 +55,11 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Updated PCAP ethernet driver.** + + * Changed transmit burst to always return the number of packets requested. + Failed sends are counted as transmit errors. + Removed Items -------------
drivers/net/pcap/pcap_ethdev.c +50 -55
180 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v8 7/14 net/pcap: consolidate boolean flag handling
In reply to Stephen HemmingerConvert internal flag fields from int/unsigned int to bool for clarity and reduced structure size.
Merge the separate select_phy_mac() and get_infinite_rx_arg() functions into a single process_bool_flag() handler. The new function also adds proper validation, rejecting values other than "0", "1", or empty (which defaults to true).
Also change num_of_queue from unsigned int to uint16_t since it cannot exceed RTE_PMD_PCAP_MAX_QUEUES.
--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -38
@@ -6,6 +6,7 @@#include <stdio.h> #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <inttypes.h> #include <errno.h>
@@ -98,9 +99,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -110,25 +111,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -866,7 +867,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1175,29 +1176,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1473,7 +1464,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1512,9 +1503,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
PATCH v8 8/14 net/pcap: support VLAN insert and strip
In reply to Stephen HemmingerDriver can easily insert VLAN tag strip and insertion similar to how it is handled in virtio and af_packet.
--- doc/guides/nics/features/pcap.ini | 1 + doc/guides/rel_notes/release_26_03.rst | 1 + drivers/net/pcap/pcap_ethdev.c | 38 ++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -10,6 +10,7 @@ Scattered Rx = Y Timestamp offload = Y Basic stats = Y Stats per queue = Y +VLAN offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -59,6 +59,7 @@ New Features * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors. + * Added support for VLAN insertion and stripping. Removed Items
drivers/net/pcap/pcap_ethdev.c +32 -6
@@ -75,6 +75,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool vlan_strip;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -102,6 +103,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool vlan_strip; };struct pmd_process_private {
@@ -332,6 +334,10 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; + +if (pcap_q->vlan_strip) +rte_vlan_strip (mbuf); +uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us;
@@ -416,15 +422,21 @@ 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++) {struct rte_mbuf *mbuf = bufs[i]; -uint32_t len =rte_pktmbuf_pkt_len (mbuf); -void *temp =NULL ; -const uint8_t *data; + +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + }calculate_timestamp (&header.ts); + +uint32_t len =rte_pktmbuf_pkt_len (mbuf); header.len = len; header.caplen = len; - data =pcap_pktmbuf_read (mbuf,0 , len, &temp); +void *temp =NULL ; +const uint8_t *data =pcap_pktmbuf_read (mbuf,0 , len, &temp);if (likely (data !=NULL )) {pcap_dump ((u_char *)dumper, &header, data);
@@ -497,6 +509,12 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + } + data =pcap_pktmbuf_read (mbuf,0 , len, &temp);if (likely (data !=NULL &&pcap_sendpacket (pcap, data, len) ==0 )) {
@@ -731,8 +749,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->vlan_strip = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_VLAN_STRIP );return 0 ; }
@@ -748,7 +771,9 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; - dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP ;return 0 ; }
@@ -895,6 +920,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->mb_pool = mb_pool; pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; + pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q;if (internals->infinite_rx) {
Signature · 1 line
2.51.0
PATCH v8 9/14 net/pcap: add link state and speed for interface mode
In reply to Stephen HemmingerWhen the PCAP PMD is used in pass-through mode with a physical interface (iface=X), the link status was always reported with hardcoded values regardless of the actual interface state.
Add OS-dependent functions to query the real link state, speed, duplex, and autonegotiation settings from the underlying interface. The eth_link_update() callback now returns accurate information when operating in pass-through mode.
Linux uses ETHTOOL_GLINKSETTINGS which supports all speeds up to 800 Gbps. FreeBSD uses SIOCGIFMEDIA, and Windows uses GetAdaptersAddresses().
For pcap file mode or separate rx/tx interface configurations, default values continue to be used since there is no single underlying interface to query.
--- drivers/net/pcap/pcap_ethdev.c | 105 +++++++++- drivers/net/pcap/pcap_osdep.h | 22 ++ drivers/net/pcap/pcap_osdep_freebsd.c | 277 ++++++++++++++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 109 ++++++++++ drivers/net/pcap/pcap_osdep_windows.c | 95 +++++++-- 5 files changed, 585 insertions(+), 23 deletions(-)
drivers/net/pcap/pcap_ethdev.c +94 -11
@@ -146,13 +146,6 @@ static const char *valid_arguments[] = {NULL }; -static struct rte_eth_link pmd_link = { - .link_speed =RTE_ETH_SPEED_NUM_10G , - .link_duplex =RTE_ETH_LINK_FULL_DUPLEX , - .link_status =RTE_ETH_LINK_DOWN , - .link_autoneg =RTE_ETH_LINK_FIXED , -}; -RTE_LOG_REGISTER_DEFAULT (eth_pcap_logtype,NOTICE ); static struct queue_missed_stat*
@@ -899,11 +892,96 @@ eth_dev_close(struct rte_eth_dev *dev)return 0 ; } +/* +* Convert osdep speed (Mbps) to rte_eth_link speed constant. +*/ +static uint32_t +speed_mbps_to_rte (uint32_t speed_mbps) +{ +switch (speed_mbps) { +case 10 : +return RTE_ETH_SPEED_NUM_10M ; +case 100 : +return RTE_ETH_SPEED_NUM_100M ; +case 1000 : +return RTE_ETH_SPEED_NUM_1G ; +case 2500 : +return RTE_ETH_SPEED_NUM_2_5G ; +case 5000 : +return RTE_ETH_SPEED_NUM_5G ; +case 10000 : +return RTE_ETH_SPEED_NUM_10G ; +case 20000 : +return RTE_ETH_SPEED_NUM_20G ; +case 25000 : +return RTE_ETH_SPEED_NUM_25G ; +case 40000 : +return RTE_ETH_SPEED_NUM_40G ; +case 50000 : +return RTE_ETH_SPEED_NUM_50G ; +case 56000 : +return RTE_ETH_SPEED_NUM_56G ; +case 100000 : +return RTE_ETH_SPEED_NUM_100G ; +case 200000 : +return RTE_ETH_SPEED_NUM_200G ; +case 400000 : +return RTE_ETH_SPEED_NUM_400G ; +case 800000 : +return RTE_ETH_SPEED_NUM_800G ; +default : +/* For unknown speeds, return the raw value */ +if (speed_mbps >0 ) +return speed_mbps; +return RTE_ETH_SPEED_NUM_UNKNOWN ; + } +} +static int -eth_link_update(struct rte_eth_dev *dev __rte_unused, - int wait_to_complete __rte_unused) +eth_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused) { -return 0 ; +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_link link; +struct osdep_iface_link osdep_link; +const char *iface_name; + +memset (&link,0 ,sizeof (link)); + +/* +* For pass-through mode (single_iface), query the actual interface. +* Otherwise, use the default static link values. +*/ +if (internals->single_iface) { + iface_name = internals->rx_queue[0 ].name; + +if (osdep_iface_link_get (iface_name, &osdep_link) ==0 ) { + link.link_speed =speed_mbps_to_rte (osdep_link.link_speed); + link.link_status = osdep_link.link_status ? +RTE_ETH_LINK_UP :RTE_ETH_LINK_DOWN ; + link.link_duplex = osdep_link.link_duplex ? +RTE_ETH_LINK_FULL_DUPLEX :RTE_ETH_LINK_HALF_DUPLEX ; + link.link_autoneg = osdep_link.link_autoneg ? +RTE_ETH_LINK_AUTONEG :RTE_ETH_LINK_FIXED ; + }else { +/* Query failed, use defaults */ + link.link_speed =RTE_ETH_SPEED_NUM_10G ; + link.link_duplex =RTE_ETH_LINK_FULL_DUPLEX ; + link.link_status =RTE_ETH_LINK_DOWN ; + link.link_autoneg =RTE_ETH_LINK_FIXED ; + } + }else { +/* +* Not in pass-through mode (using pcap files or separate +* interfaces for rx/tx). Use default values. +*/ + link.link_speed =RTE_ETH_SPEED_NUM_10G ; + link.link_duplex =RTE_ETH_LINK_FULL_DUPLEX ; + link.link_status = dev->data->dev_started ? +RTE_ETH_LINK_UP :RTE_ETH_LINK_DOWN ; + link.link_autoneg =RTE_ETH_LINK_FIXED ; + } + +return rte_eth_linkstatus_set (dev, &link); } static int
@@ -1268,7 +1346,12 @@ pmd_init_internals(struct rte_vdev_device *vdev, data = (*eth_dev)->data; data->nb_rx_queues = (uint16_t )nb_rx_queues; data->nb_tx_queues = (uint16_t )nb_tx_queues; - data->dev_link = pmd_link; + data->dev_link = (struct rte_eth_link ) { + .link_speed =RTE_ETH_SPEED_NUM_NONE , + .link_duplex =RTE_ETH_LINK_FULL_DUPLEX , + .link_status =RTE_ETH_LINK_DOWN , + .link_autoneg =RTE_ETH_LINK_FIXED , + }; data->mac_addrs = &(*internals)->eth_addr; data->promiscuous =1 ; data->all_multicast =1 ;
drivers/net/pcap/pcap_osdep.h +22 -0
@@ -13,7 +13,29 @@extern int eth_pcap_logtype;#define RTE_LOGTYPE_ETH_PCAP eth_pcap_logtype +/** +* Link information returned by osdep_iface_link_get(). +*/ +struct osdep_iface_link { +uint32_t link_speed;/**< Speed in Mbps, 0 if unknown */ +uint8_t link_status;/**< 1 = up, 0 = down */ +uint8_t link_duplex;/**< 1 = full, 0 = half */ +uint8_t link_autoneg;/**< 1 = autoneg enabled, 0 = fixed */ +}; +int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +/** +* Get link state and speed for a network interface. +* +* @param name +* Interface name (e.g., "eth0" on Linux, "{GUID}" on Windows). +* @param link +* Pointer to structure to fill with link information. +* @return +* 0 on success, -1 on failure. +*/ +int osdep_iface_link_get (const char *name,struct osdep_iface_link *link); +#endif
drivers/net/pcap/pcap_osdep_freebsd.c +277 -0
288 lines not on this page · raw message
drivers/net/pcap/pcap_osdep_linux.c +109 -0
118 lines not on this page · raw message
drivers/net/pcap/pcap_osdep_windows.c +83 -12
124 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v8 10/14 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerEnable nanosecond-precision timestamps for both live capture and pcap file reading.
Replace pcap_open_live() with the pcap_create()/pcap_activate() API, which allows setting PCAP_TSTAMP_PRECISION_NANO before activation. Similarly, use pcap_open_offline_with_tstamp_precision() for reading pcap files. The pcap_pkthdr timestamp field, despite being declared as struct timeval, actually contains nanoseconds (not microseconds) when nanosecond precision is requested.
Make receive timestamp offloading conditional: timestamps are now only written to the mbuf dynamic field when RTE_ETH_RX_OFFLOAD_TIMESTAMP is enabled. Previously, timestamps were unconditionally added to every received packet.
Other related changes:
* Defer timestamp dynfield registration from probe to device start, and only when timestamp offloading is enabled
* Add read_clock dev_op returning current UTC time
for timestamp correlation
* Move per-burst timestamp calculation outside the packet loop in tx_dumper
* Enable immediate mode and improve error reporting
in live capture setup
--- doc/guides/rel_notes/release_26_03.rst | 2 + drivers/net/pcap/pcap_ethdev.c | 130 +++++++++++++++++++------ 2 files changed, 102 insertions(+), 30 deletions(-)
doc/guides/rel_notes/release_26_03.rst +2 -0
@@ -60,6 +60,8 @@ New Features * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors. * Added support for VLAN insertion and stripping. + * Receive timestamp offload is only done if offload flag set. + * Receive timestamps support nanosecond precision. Removed Items
drivers/net/pcap/pcap_ethdev.c +100 -30
243 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v8 11/14 net/pcap: reduce scope of file-level variables
In reply to Stephen HemmingerMove errbuf from file scope to local variables in the two functions that use it (open_iface_live and open_single_rx_pcap). This avoids potential issues if these functions were called concurrently, since each call now has its own error buffer. Move iface_idx to a static local variable within pmd_init_internals(), the only function that uses it. The variable remains static to preserve the MAC address uniqueness counter across calls.
--- drivers/net/pcap/pcap_ethdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -2
@@ -46,11 +46,9 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -541,6 +539,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ];pcap_t *pc;int status;
@@ -639,6 +638,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1413,6 +1414,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v8 12/14 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -54,10 +54,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -838,11 +838,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0
PATCH v8 13/14 net/pcap: clarify maximum received packet
In reply to Stephen HemmingerThe driver has constant RTE_ETH_PCAP_SNAPSHOT_LEN with is set to the largest value the pcap library will return, so that should also be the largest receive buffer.
--- drivers/net/pcap/pcap_ethdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/pcap/pcap_ethdev.c +1 -1
@@ -821,7 +821,7 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->if_index = internals->if_index; dev_info->max_mac_addrs =1 ; - dev_info->max_rx_pktlen = (uint32_t )-1 ; + dev_info->max_rx_pktlen =RTE_ETH_PCAP_SNAPSHOT_LEN ; dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ;
Signature · 1 line
2.51.0
PATCH v8 14/14 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests basic operations, timestamps, jumbo frame, vlan handling and multiple queues
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2337 ++++++++++++++++++++++++ doc/guides/rel_notes/release_26_03.rst | 1 + 3 files changed, 2340 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +2337 -0
2,337 lines not on this page · raw message
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -62,6 +62,7 @@ New Features * Added support for VLAN insertion and stripping. * Receive timestamp offload is only done if offload flag set. * Receive timestamps support nanosecond precision. + * Added unit test suite. Removed Items
Signature · 1 line
2.51.0
v9 cover net/pcap: improvements and test suite
In reply to Stephen HemmingerThis series contains improvements to the PCAP PMD including new features, bug fixes, code cleanup, and a comprehensive test suite.
New features:
- VLAN tag insertion on Tx and stripping on Rx
- Nanosecond precision timestamps (when hardware/libpcap supports it)
- Accurate link state, speed, and duplex reporting in interface mode
- Advertise RTE_ETH_TX_OFFLOAD_MULTI_SEGS capability
- Configurable snapshot length via snapshot_len devarg
Bug fixes:
- Fix multi-segment transmit to dynamically allocate instead of silently truncating packets larger than 9K stack buffer
- Change Tx burst to always consume all packets; failed sends increment error counter rather than leaving mbufs for retry (pcap_sendpacket failures are not transient)
Code cleanup:
- Convert internal flags from int to bool
- Remove unnecessary casts of void* from rte_zmalloc
- Replace rte_malloc/rte_memcpy with libc equivalents in osdep code
- Include headers explicitly rather than relying on indirect includes
- Remove unnecessary volatile qualifier on statistics
- Reduce scope of file-level variables
- Defer pcap handle opening until device start
Testing:
- Add comprehensive unit test suite covering basic operations, timestamps, jumbo frames, VLAN handling, multi-queue, and more
v9:
- Add configurable snapshot length parameter (snapshot_len devarg)
- Defer opening of pcap files and interfaces until eth_dev_start() instead of during probe, passing configured snapshot length
v8:
- Fix pcap header length in VLAN transmit
- Fix clang warnings from ethtool link_mode array
v7:
- Drop MTU configuration patch; not necessary as underlying OS commands (ip link, ifconfig) can be used directly
- Add patch to remove unnecessary volatile on queue statistics
- Add test_link_status to unit test suite
- Add Acked-by from Marat Khalili on patch 11
Stephen Hemminger (15):
maintainers: update for pcap driver
doc: update features for PCAP PMD
net/pcap: include used headers
net/pcap: remove unnecessary casts
net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: improve multi-segment transmit handling net/pcap: consolidate boolean flag handling net/pcap: support VLAN insert and strip net/pcap: add link state and speed for interface mode net/pcap: support nanosecond timestamp precision net/pcap: reduce scope of file-level variables net/pcap: avoid use of volatile
net/pcap: clarify maximum received packet
test: add test for pcap PMD
net/pcap: add snapshot length devarg
MAINTAINERS | 1 + app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2525 ++++++++++++++++++++++++ doc/guides/nics/features/pcap.ini | 8 + doc/guides/nics/pcap_ring.rst | 17 + doc/guides/rel_notes/release_26_03.rst | 10 + drivers/net/pcap/pcap_ethdev.c | 608 ++++-- drivers/net/pcap/pcap_osdep.h | 23 + drivers/net/pcap/pcap_osdep_freebsd.c | 289 ++- drivers/net/pcap/pcap_osdep_linux.c | 115 +- drivers/net/pcap/pcap_osdep_windows.c | 95 +- 11 files changed, 3468 insertions(+), 225 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v9 1/15 maintainers: update for pcap driver
Nominate myself to take care of this since already doing pcapng and pdump code.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -1118,6 +1118,7 @@ F: doc/guides/nics/zxdh.rst F: doc/guides/nics/features/zxdh.ini PCAP PMD +M: Stephen Hemminger <[email protected]> F: drivers/net/pcap/ F: doc/guides/nics/pcap_ring.rst F: doc/guides/nics/features/pcap.ini
Signature · 1 line
2.51.0
PATCH v9 2/15 doc: update features for PCAP PMD
In reply to Stephen HemmingerThe PCAP PMD supports more features that were not flagged in the feature matrix.
--- doc/guides/nics/features/pcap.ini | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/nics/features/pcap.ini +7 -0
@@ -4,8 +4,15 @@ ; Refer to default.ini for the full list of available PMD features. ; [Features] +Link status = Y +Queue start/stop = Y +Scattered Rx = Y +Timestamp offload = Y Basic stats = Y +Stats per queue = Y Multiprocess aware = Y +FreeBSD = Y +Linux = Y ARMv7 = Y ARMv8 = Y Power8 = Y
Signature · 1 line
2.51.0
PATCH v9 3/15 net/pcap: include used headers
In reply to Stephen HemmingerInclude the used headers instead of relying on getting the headers indirectly through other headers.
--- drivers/net/pcap/pcap_ethdev.c | 8 ++++++++ drivers/net/pcap/pcap_osdep.h | 1 + 2 files changed, 9 insertions(+)
drivers/net/pcap/pcap_ethdev.c +8 -0
@@ -4,16 +4,24 @@* All rights reserved. */ +#include <stdio.h> #include <stdlib.h> #include <time.h> +#include <inttypes.h> +#include <errno.h> +#include <sys/time.h> +#include <sys/types.h> #include <pcap.h> #include <rte_cycles.h> +#include <rte_ring.h> +#include <rte_ethdev.h> #include <ethdev_driver.h> #include <ethdev_vdev.h> #include <rte_kvargs.h> #include <rte_malloc.h> +#include <rte_memcpy.h> #include <rte_mbuf.h> #include <rte_mbuf_dyn.h> #include <bus_vdev_driver.h>
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -6,6 +6,7 @@#define _RTE_PCAP_OSDEP_#include <rte_ether.h> +#include <rte_log.h> #define PMD_LOG (level, ...) \ RTE_LOG_LINE_PREFIX(level, ETH_PCAP, "%s(): ", __func__, __VA_ARGS__)
Signature · 1 line
2.51.0
PATCH v9 4/15 net/pcap: remove unnecessary casts
In reply to Stephen HemmingerThe function rte_zmalloc returns void * so cast is unnecessary.
Correct the indentation in that code. Not really worth backporting.
--- drivers/net/pcap/pcap_ethdev.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -7
@@ -1221,9 +1221,8 @@ pmd_init_internals(struct rte_vdev_device *vdev,PMD_LOG (INFO ,"Creating pcap-backed ethdev on numa socket %d" , numa_node); - pp = (struct pmd_process_private *) -rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
@@ -1591,10 +1590,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)unsigned int i; internal = eth_dev->data->dev_private; - pp = (struct pmd_process_private *) -rte_zmalloc (NULL , -sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
Signature · 1 line
2.51.0
PATCH v9 5/15 net/pcap: avoid using rte_malloc and rte_memcpy
In reply to Stephen HemmingerNo need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_ethdev.c | 2 +- drivers/net/pcap/pcap_osdep_freebsd.c | 12 +++++------- drivers/net/pcap/pcap_osdep_linux.c | 6 +++--- 3 files changed, 9 insertions(+), 11 deletions(-)
drivers/net/pcap/pcap_ethdev.c +1 -1
@@ -1289,7 +1289,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,return -1 ;PMD_LOG (INFO ,"Setting phy MAC for %s" , if_name); -rte_memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); +memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); eth_dev->data->mac_addrs = mac_addrs;return 0 ; }
drivers/net/pcap/pcap_osdep_freebsd.c +5 -7
@@ -4,13 +4,11 @@* All rights reserved. */ +#include <string.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +39,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
drivers/net/pcap/pcap_osdep_linux.c +3 -3
@@ -4,12 +4,12 @@* All rights reserved. */ +#include <string.h> +#include <unistd.h> #include <net/if.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <unistd.h> -#include <rte_memcpy.h> #include <rte_string_fns.h> #include "pcap_osdep.h"
@@ -35,7 +35,7 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return -1 ; } -rte_memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN );close (if_fd);return 0 ;
Signature · 1 line
2.51.0
PATCH v9 6/15 net/pcap: improve multi-segment transmit handling
In reply to Stephen HemmingerAdvertise RTE_ETH_TX_OFFLOAD_MULTI_SEGS capability in device info. The driver already handles multi-segment mbufs but was not reporting this.
Replace the fixed-size stack buffer (up to 9K) with dynamic allocation only when the mbuf is non-contiguous. This avoids large stack usage and removes the silent truncation that occurred when packets exceeded the buffer size.
Change transmit functions to always consume and free all packets, returning nb_pkts regardless of send success. The DPDK transmit API interprets a return value less than requested as "queue full, retry later." However, pcap_sendpacket() failures (bad parameters or socket errors) are not transient and retrying would fail again. The correct behavior is to free failed packets and increment the error counter rather than leaving mbufs for the application to retry.
Also use rte_pktmbuf_free_bulk() for more efficient buffer freeing.
--- doc/guides/rel_notes/release_26_03.rst | 5 ++ drivers/net/pcap/pcap_ethdev.c | 105 ++++++++++++------------- 2 files changed, 55 insertions(+), 55 deletions(-)
doc/guides/rel_notes/release_26_03.rst +5 -0
@@ -55,6 +55,11 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Updated PCAP ethernet driver.** + + * Changed transmit burst to always return the number of packets requested. + Failed sends are counted as transmit errors. + Removed Items -------------
drivers/net/pcap/pcap_ethdev.c +50 -55
180 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v9 7/15 net/pcap: consolidate boolean flag handling
In reply to Stephen HemmingerConvert internal flag fields from int/unsigned int to bool for clarity and reduced structure size.
Merge the separate select_phy_mac() and get_infinite_rx_arg() functions into a single process_bool_flag() handler. The new function also adds proper validation, rejecting values other than "0", "1", or empty (which defaults to true).
Also change num_of_queue from unsigned int to uint16_t since it cannot exceed RTE_PMD_PCAP_MAX_QUEUES.
--- drivers/net/pcap/pcap_ethdev.c | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-)
drivers/net/pcap/pcap_ethdev.c +29 -38
@@ -6,6 +6,7 @@#include <stdio.h> #include <stdlib.h> +#include <stdbool.h> #include <time.h> #include <inttypes.h> #include <errno.h>
@@ -98,9 +99,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -110,25 +111,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -866,7 +867,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1175,29 +1176,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1473,7 +1464,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1512,9 +1503,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
Signature · 1 line
2.51.0
PATCH v9 8/15 net/pcap: support VLAN insert and strip
In reply to Stephen HemmingerDriver can easily insert VLAN tag strip and insertion similar to how it is handled in virtio and af_packet.
--- doc/guides/nics/features/pcap.ini | 1 + doc/guides/rel_notes/release_26_03.rst | 1 + drivers/net/pcap/pcap_ethdev.c | 38 ++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -10,6 +10,7 @@ Scattered Rx = Y Timestamp offload = Y Basic stats = Y Stats per queue = Y +VLAN offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -59,6 +59,7 @@ New Features * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors. + * Added support for VLAN insertion and stripping. Removed Items
drivers/net/pcap/pcap_ethdev.c +32 -6
@@ -75,6 +75,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool vlan_strip;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -102,6 +103,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool vlan_strip; };struct pmd_process_private {
@@ -332,6 +334,10 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; + +if (pcap_q->vlan_strip) +rte_vlan_strip (mbuf); +uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us;
@@ -416,15 +422,21 @@ 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++) {struct rte_mbuf *mbuf = bufs[i]; -uint32_t len =rte_pktmbuf_pkt_len (mbuf); -void *temp =NULL ; -const uint8_t *data; + +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + }calculate_timestamp (&header.ts); + +uint32_t len =rte_pktmbuf_pkt_len (mbuf); header.len = len; header.caplen = len; - data =pcap_pktmbuf_read (mbuf,0 , len, &temp); +void *temp =NULL ; +const uint8_t *data =pcap_pktmbuf_read (mbuf,0 , len, &temp);if (likely (data !=NULL )) {pcap_dump ((u_char *)dumper, &header, data);
@@ -497,6 +509,12 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)void *temp =NULL ;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + } + data =pcap_pktmbuf_read (mbuf,0 , len, &temp);if (likely (data !=NULL &&pcap_sendpacket (pcap, data, len) ==0 )) {
@@ -731,8 +749,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->vlan_strip = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_VLAN_STRIP );return 0 ; }
@@ -748,7 +771,9 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; - dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP ;return 0 ; }
@@ -895,6 +920,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->mb_pool = mb_pool; pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; + pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q;if (internals->infinite_rx) {
Signature · 1 line
2.51.0
PATCH v9 9/15 net/pcap: add link state and speed for interface mode
In reply to Stephen HemmingerWhen the PCAP PMD is used in pass-through mode with a physical interface (iface=X), the link status was always reported with hardcoded values regardless of the actual interface state.
Add OS-dependent functions to query the real link state, speed, duplex, and autonegotiation settings from the underlying interface. The eth_link_update() callback now returns accurate information when operating in pass-through mode.
Linux uses ETHTOOL_GLINKSETTINGS which supports all speeds up to 800 Gbps. FreeBSD uses SIOCGIFMEDIA, and Windows uses GetAdaptersAddresses().
For pcap file mode or separate rx/tx interface configurations, default values continue to be used since there is no single underlying interface to query.
--- drivers/net/pcap/pcap_ethdev.c | 105 +++++++++- drivers/net/pcap/pcap_osdep.h | 22 ++ drivers/net/pcap/pcap_osdep_freebsd.c | 277 ++++++++++++++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 109 ++++++++++ drivers/net/pcap/pcap_osdep_windows.c | 95 +++++++-- 5 files changed, 585 insertions(+), 23 deletions(-)
drivers/net/pcap/pcap_ethdev.c +94 -11
@@ -146,13 +146,6 @@ static const char *valid_arguments[] = {NULL }; -static struct rte_eth_link pmd_link = { - .link_speed =RTE_ETH_SPEED_NUM_10G , - .link_duplex =RTE_ETH_LINK_FULL_DUPLEX , - .link_status =RTE_ETH_LINK_DOWN , - .link_autoneg =RTE_ETH_LINK_FIXED , -}; -RTE_LOG_REGISTER_DEFAULT (eth_pcap_logtype,NOTICE ); static struct queue_missed_stat*
@@ -899,11 +892,96 @@ eth_dev_close(struct rte_eth_dev *dev)return 0 ; } +/* +* Convert osdep speed (Mbps) to rte_eth_link speed constant. +*/ +static uint32_t +speed_mbps_to_rte (uint32_t speed_mbps) +{ +switch (speed_mbps) { +case 10 : +return RTE_ETH_SPEED_NUM_10M ; +case 100 : +return RTE_ETH_SPEED_NUM_100M ; +case 1000 : +return RTE_ETH_SPEED_NUM_1G ; +case 2500 : +return RTE_ETH_SPEED_NUM_2_5G ; +case 5000 : +return RTE_ETH_SPEED_NUM_5G ; +case 10000 : +return RTE_ETH_SPEED_NUM_10G ; +case 20000 : +return RTE_ETH_SPEED_NUM_20G ; +case 25000 : +return RTE_ETH_SPEED_NUM_25G ; +case 40000 : +return RTE_ETH_SPEED_NUM_40G ; +case 50000 : +return RTE_ETH_SPEED_NUM_50G ; +case 56000 : +return RTE_ETH_SPEED_NUM_56G ; +case 100000 : +return RTE_ETH_SPEED_NUM_100G ; +case 200000 : +return RTE_ETH_SPEED_NUM_200G ; +case 400000 : +return RTE_ETH_SPEED_NUM_400G ; +case 800000 : +return RTE_ETH_SPEED_NUM_800G ; +default : +/* For unknown speeds, return the raw value */ +if (speed_mbps >0 ) +return speed_mbps; +return RTE_ETH_SPEED_NUM_UNKNOWN ; + } +} +static int -eth_link_update(struct rte_eth_dev *dev __rte_unused, - int wait_to_complete __rte_unused) +eth_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused) { -return 0 ; +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_link link; +struct osdep_iface_link osdep_link; +const char *iface_name; + +memset (&link,0 ,sizeof (link)); + +/* +* For pass-through mode (single_iface), query the actual interface. +* Otherwise, use the default static link values. +*/ +if (internals->single_iface) { + iface_name = internals->rx_queue[0 ].name; + +if (osdep_iface_link_get (iface_name, &osdep_link) ==0 ) { + link.link_speed =speed_mbps_to_rte (osdep_link.link_speed); + link.link_status = osdep_link.link_status ? +RTE_ETH_LINK_UP :RTE_ETH_LINK_DOWN ; + link.link_duplex = osdep_link.link_duplex ? +RTE_ETH_LINK_FULL_DUPLEX :RTE_ETH_LINK_HALF_DUPLEX ; + link.link_autoneg = osdep_link.link_autoneg ? +RTE_ETH_LINK_AUTONEG :RTE_ETH_LINK_FIXED ; + }else { +/* Query failed, use defaults */ + link.link_speed =RTE_ETH_SPEED_NUM_10G ; + link.link_duplex =RTE_ETH_LINK_FULL_DUPLEX ; + link.link_status =RTE_ETH_LINK_DOWN ; + link.link_autoneg =RTE_ETH_LINK_FIXED ; + } + }else { +/* +* Not in pass-through mode (using pcap files or separate +* interfaces for rx/tx). Use default values. +*/ + link.link_speed =RTE_ETH_SPEED_NUM_10G ; + link.link_duplex =RTE_ETH_LINK_FULL_DUPLEX ; + link.link_status = dev->data->dev_started ? +RTE_ETH_LINK_UP :RTE_ETH_LINK_DOWN ; + link.link_autoneg =RTE_ETH_LINK_FIXED ; + } + +return rte_eth_linkstatus_set (dev, &link); } static int
@@ -1268,7 +1346,12 @@ pmd_init_internals(struct rte_vdev_device *vdev, data = (*eth_dev)->data; data->nb_rx_queues = (uint16_t )nb_rx_queues; data->nb_tx_queues = (uint16_t )nb_tx_queues; - data->dev_link = pmd_link; + data->dev_link = (struct rte_eth_link ) { + .link_speed =RTE_ETH_SPEED_NUM_NONE , + .link_duplex =RTE_ETH_LINK_FULL_DUPLEX , + .link_status =RTE_ETH_LINK_DOWN , + .link_autoneg =RTE_ETH_LINK_FIXED , + }; data->mac_addrs = &(*internals)->eth_addr; data->promiscuous =1 ; data->all_multicast =1 ;
drivers/net/pcap/pcap_osdep.h +22 -0
@@ -13,7 +13,29 @@extern int eth_pcap_logtype;#define RTE_LOGTYPE_ETH_PCAP eth_pcap_logtype +/** +* Link information returned by osdep_iface_link_get(). +*/ +struct osdep_iface_link { +uint32_t link_speed;/**< Speed in Mbps, 0 if unknown */ +uint8_t link_status;/**< 1 = up, 0 = down */ +uint8_t link_duplex;/**< 1 = full, 0 = half */ +uint8_t link_autoneg;/**< 1 = autoneg enabled, 0 = fixed */ +}; +int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +/** +* Get link state and speed for a network interface. +* +* @param name +* Interface name (e.g., "eth0" on Linux, "{GUID}" on Windows). +* @param link +* Pointer to structure to fill with link information. +* @return +* 0 on success, -1 on failure. +*/ +int osdep_iface_link_get (const char *name,struct osdep_iface_link *link); +#endif
drivers/net/pcap/pcap_osdep_freebsd.c +277 -0
288 lines not on this page · raw message
drivers/net/pcap/pcap_osdep_linux.c +109 -0
118 lines not on this page · raw message
drivers/net/pcap/pcap_osdep_windows.c +83 -12
124 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v9 10/15 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerEnable nanosecond-precision timestamps for both live capture and pcap file reading.
Replace pcap_open_live() with the pcap_create()/pcap_activate() API, which allows setting PCAP_TSTAMP_PRECISION_NANO before activation. Similarly, use pcap_open_offline_with_tstamp_precision() for reading pcap files. The pcap_pkthdr timestamp field, despite being declared as struct timeval, actually contains nanoseconds (not microseconds) when nanosecond precision is requested.
Make receive timestamp offloading conditional: timestamps are now only written to the mbuf dynamic field when RTE_ETH_RX_OFFLOAD_TIMESTAMP is enabled. Previously, timestamps were unconditionally added to every received packet.
Other related changes:
* Defer timestamp dynfield registration from probe to device start, and only when timestamp offloading is enabled
* Add read_clock dev_op returning current UTC time
for timestamp correlation
* Move per-burst timestamp calculation outside the packet loop in tx_dumper
* Enable immediate mode and improve error reporting
in live capture setup
--- doc/guides/rel_notes/release_26_03.rst | 2 + drivers/net/pcap/pcap_ethdev.c | 130 +++++++++++++++++++------ 2 files changed, 102 insertions(+), 30 deletions(-)
doc/guides/rel_notes/release_26_03.rst +2 -0
@@ -60,6 +60,8 @@ New Features * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors. * Added support for VLAN insertion and stripping. + * Receive timestamp offload is only done if offload flag set. + * Receive timestamps support nanosecond precision. Removed Items
drivers/net/pcap/pcap_ethdev.c +100 -30
243 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v9 11/15 net/pcap: reduce scope of file-level variables
In reply to Stephen HemmingerMove errbuf from file scope to local variables in the two functions that use it (open_iface_live and open_single_rx_pcap). This avoids potential issues if these functions were called concurrently, since each call now has its own error buffer. Move iface_idx to a static local variable within pmd_init_internals(), the only function that uses it. The variable remains static to preserve the MAC address uniqueness counter across calls.
--- drivers/net/pcap/pcap_ethdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -2
@@ -46,11 +46,9 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -541,6 +539,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ];pcap_t *pc;int status;
@@ -639,6 +638,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1413,6 +1414,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v9 12/15 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -54,10 +54,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -838,11 +838,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0
PATCH v9 13/15 net/pcap: clarify maximum received packet
In reply to Stephen HemmingerThe driver has constant RTE_ETH_PCAP_SNAPSHOT_LEN with is set to the largest value the pcap library will return, so that should also be the largest receive buffer.
--- drivers/net/pcap/pcap_ethdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/pcap/pcap_ethdev.c +2 -1
@@ -821,10 +821,11 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->if_index = internals->if_index; dev_info->max_mac_addrs =1 ; - dev_info->max_rx_pktlen = (uint32_t )-1 ; + dev_info->max_rx_pktlen =RTE_ETH_PCAP_SNAPSHOT_LEN ; dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->max_mtu =RTE_ETH_PCAP_SNAPSHOT_LEN -RTE_ETHER_HDR_LEN ; dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS |RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP |
Signature · 1 line
2.51.0
PATCH v9 14/15 test: add test for pcap PMD
In reply to Stephen HemmingerThis test was generated by Claude AI with some prompting and pointing at existing ring PMD test. It tests basic operations, timestamps, jumbo frame, vlan handling and multiple queues
--- app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2337 ++++++++++++++++++++++++ doc/guides/rel_notes/release_26_03.rst | 1 + 3 files changed, 2340 insertions(+) create mode 100644 app/test/test_pmd_pcap.c
app/test/meson.build +2 -0
@@ -141,6 +141,7 @@ source_file_deps = { 'test_per_lcore.c': [], 'test_pflock.c': [], 'test_pie.c': ['sched'], + 'test_pmd_pcap.c': ['net_pcap', 'ethdev', 'bus_vdev'] + packet_burst_generator_deps, 'test_pmd_perf.c': ['ethdev', 'net'] + packet_burst_generator_deps, 'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'], 'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
@@ -216,6 +217,7 @@ source_file_deps = { source_file_ext_deps = { 'test_compressdev.c': ['zlib'], 'test_pcapng.c': ['pcap'], + 'test_pmd_pcap.c': ['pcap'], } def_lib = get_option('default_library')
app/test/test_pmd_pcap.c new file +2337 -0
2,337 lines not on this page · raw message
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -62,6 +62,7 @@ New Features * Added support for VLAN insertion and stripping. * Receive timestamp offload is only done if offload flag set. * Receive timestamps support nanosecond precision. + * Added unit test suite. Removed Items
Signature · 1 line
2.51.0
PATCH v9 15/15 net/pcap: add snapshot length devarg
In reply to Stephen HemmingerAdd a new devarg 'snaplen' to configure the pcap snapshot length, which controls the maximum packet size for capture and output.
The snapshot length affects:
- The pcap_set_snaplen() call when capturing from interfaces
- The pcap_open_dead() snapshot parameter for output files
- The reported max_rx_pktlen in device info
- The reported max_mtu in device info (snaplen - ethernet header)
The default value is 65535 bytes, preserving backward compatibility with previous driver behavior.
Example usage:
--vdev 'net_pcap0,iface=eth0,snaplen=1518'
--vdev 'net_pcap0,rx_pcap=in.pcap,tx_pcap=out.pcap,snaplen=9000'
--- app/test/test_pmd_pcap.c | 194 ++++++++++++++++++++++++- doc/guides/nics/pcap_ring.rst | 17 +++ doc/guides/rel_notes/release_26_03.rst | 1 + drivers/net/pcap/pcap_ethdev.c | 165 ++++++++++++--------- 4 files changed, 306 insertions(+), 71 deletions(-)
app/test/test_pmd_pcap.c +191 -3
@@ -447,6 +447,41 @@ get_pcap_packet_sizes(const char *path, uint16_t *sizes, unsigned int max_pkts)return count; } +/* +* Helper: Verify packets in pcap file are truncated correctly +* Returns 0 if all packets have caplen == expected_caplen and len == expected_len +*/ +static int +verify_pcap_truncation (const char *path,uint32_t expected_caplen, +uint32_t expected_len,unsigned int *pkt_count) +{ +pcap_t *pd; +char errbuf[PCAP_ERRBUF_SIZE ]; +struct pcap_pkthdr *hdr; +const u_char *data; +unsigned int count =0 ; + + pd =pcap_open_offline (path, errbuf); +if (pd ==NULL ) +return -1 ; + +while (pcap_next_ex (pd, &hdr, &data) ==1 ) { +if (hdr->caplen != expected_caplen || hdr->len != expected_len) { +printf ("Packet %u: caplen=%u (expected %u), len=%u (expected %u)\n" , + count, hdr->caplen, expected_caplen, + hdr->len, expected_len); +pcap_close (pd); +return -1 ; + } + count++; + } + +pcap_close (pd); +if (pkt_count) + *pkt_count = count; +return 0 ; +} +/* * Helper: Configure and start a pcap ethdev port */
@@ -1973,7 +2008,7 @@ test_multi_rx_queue_same_file(void)* This test verifies that rte_eth_dev_info_get() returns correct values: * 1. max_rx_queues matches the number of rx_pcap files passed * 2. max_tx_queues matches the number of tx_pcap files passed -* 3. min_mtu and max_mtu are set to reasonable values +* 3. max_rx_pktlen and max_mtu are based on default snapshot length */ static int test_dev_info (void )
@@ -1985,6 +2020,9 @@ test_dev_info(void)uint16_t port_id;int ret;unsigned int i; +/* Default snapshot length is 65535 */ +const uint32_t default_snaplen =65535 ; +const uint32_t expected_max_mtu = default_snaplen -RTE_ETHER_HDR_LEN ;printf ("Testing device info reporting\n" );
@@ -2023,8 +2061,8 @@ test_dev_info(void)printf (" driver_name: %s\n" , dev_info.driver_name);printf (" max_rx_queues: %u (expected: 3)\n" , dev_info.max_rx_queues);printf (" max_tx_queues: %u (expected: 2)\n" , dev_info.max_tx_queues); -printf (" min_mtu: %u\n" , dev_info.min_mtu); -printf (" max_mtu: %u\n" , dev_info.max_mtu); +printf (" max_rx_pktlen: %u (expected: %u)\n" , dev_info.max_rx_pktlen, default_snaplen); +printf (" max_mtu: %u (expected: %u)\n" , dev_info.max_mtu, expected_max_mtu);/* Verify queue counts match number of pcap files */ TEST_ASSERT_EQUAL(dev_info.max_rx_queues, 3U,
@@ -2032,6 +2070,16 @@ test_dev_info(void)TEST_ASSERT_EQUAL (dev_info.max_tx_queues,2U ,"max_tx_queues mismatch: expected 2, got %u" , dev_info.max_tx_queues); +/* Verify max_rx_pktlen equals default snapshot length */ +TEST_ASSERT_EQUAL (dev_info.max_rx_pktlen, default_snaplen, +"max_rx_pktlen mismatch: expected %u, got %u" , + default_snaplen, dev_info.max_rx_pktlen); + +/* Verify max_mtu is snapshot_len minus ethernet header */ +TEST_ASSERT_EQUAL (dev_info.max_mtu, expected_max_mtu, +"max_mtu mismatch: expected %u, got %u" , + expected_max_mtu, dev_info.max_mtu); +rte_vdev_uninit ("net_pcap_devinfo" );/* Cleanup temp files */
@@ -2044,6 +2092,144 @@ test_dev_info(void)return TEST_SUCCESS ; } +/* +* Test: Custom snapshot length (snaplen) parameter +* +* This test verifies that the snaplen devarg works correctly: +* 1. max_rx_pktlen reflects the custom snapshot length +* 2. max_mtu is calculated as snaplen - ethernet header +*/ +static int +test_snaplen (void ) +{ +struct rte_eth_dev_info dev_info; +char devargs[512 ]; +char rx_path[PATH_MAX ]; +char tx_path[PATH_MAX ]; +uint16_t port_id; +int ret; +const uint32_t custom_snaplen =9000 ; +const uint32_t expected_max_mtu = custom_snaplen -RTE_ETHER_HDR_LEN ; + +printf ("Testing custom snapshot length parameter\n" ); + +/* Create temp files */ +TEST_ASSERT (create_temp_path (rx_path,sizeof (rx_path),"pcap_snaplen_rx" ) ==0 , +"Failed to create RX temp path" ); +TEST_ASSERT (create_test_pcap (rx_path,1 ) ==0 , +"Failed to create RX pcap" ); +TEST_ASSERT (create_temp_path (tx_path,sizeof (tx_path),"pcap_snaplen_tx" ) ==0 , +"Failed to create TX temp path" ); + +/* Create device with custom snaplen */ +snprintf (devargs,sizeof (devargs),"rx_pcap=%s,tx_pcap=%s,snaplen=%u" , + rx_path, tx_path, custom_snaplen); + + ret =rte_vdev_init ("net_pcap_snaplen" , devargs); +TEST_ASSERT_SUCCESS (ret,"Failed to create pcap PMD: %s" ,rte_strerror (-ret)); + + ret =rte_eth_dev_get_port_by_name ("net_pcap_snaplen" , &port_id); +TEST_ASSERT_SUCCESS (ret,"Cannot find added pcap device" ); + + ret =rte_eth_dev_info_get (port_id, &dev_info); +TEST_ASSERT_SUCCESS (ret,"Failed to get device info: %s" ,rte_strerror (-ret)); + +printf (" Custom snaplen: %u\n" , custom_snaplen); +printf (" max_rx_pktlen: %u (expected: %u)\n" , dev_info.max_rx_pktlen, custom_snaplen); +printf (" max_mtu: %u (expected: %u)\n" , dev_info.max_mtu, expected_max_mtu); + +/* Verify max_rx_pktlen equals custom snapshot length */ +TEST_ASSERT_EQUAL (dev_info.max_rx_pktlen, custom_snaplen, +"max_rx_pktlen mismatch: expected %u, got %u" , + custom_snaplen, dev_info.max_rx_pktlen); + +/* Verify max_mtu is snaplen minus ethernet header */ +TEST_ASSERT_EQUAL (dev_info.max_mtu, expected_max_mtu, +"max_mtu mismatch: expected %u, got %u" , + expected_max_mtu, dev_info.max_mtu); + +rte_vdev_uninit ("net_pcap_snaplen" ); + +/* Cleanup temp files */ +unlink (rx_path); +unlink (tx_path); + +printf ("Snapshot length test PASSED\n" ); +return TEST_SUCCESS ; +} + +/* +* Test: Snapshot length truncation behavior +* +* This test verifies that packets larger than snaplen are properly truncated +* when written to pcap files: +* 1. caplen in pcap header is limited to snaplen +* 2. len in pcap header preserves original packet length +* 3. Only snaplen bytes of data are written +*/ +static int +test_snaplen_truncation (void ) +{ +struct rte_mbuf *mbufs[NUM_PACKETS ]; +char devargs[512 ]; +char tx_path[PATH_MAX ]; +uint16_t port_id; +int ret, nb_tx, nb_gen; +unsigned int pkt_count; +const uint32_t test_snaplen =100 ; +const uint8_t pkt_size =200 ; + +printf ("Testing snaplen truncation behavior\n" ); + +/* Create temp TX file */ +TEST_ASSERT (create_temp_path (tx_path,sizeof (tx_path),"pcap_trunc_tx" ) ==0 , +"Failed to create TX temp path" ); + +/* Create device with small snaplen */ +snprintf (devargs,sizeof (devargs),"tx_pcap=%s,snaplen=%u" , + tx_path, test_snaplen); + + ret =rte_vdev_init ("net_pcap_trunc" , devargs); +TEST_ASSERT_SUCCESS (ret,"Failed to create pcap PMD: %s" ,rte_strerror (-ret)); + + ret =rte_eth_dev_get_port_by_name ("net_pcap_trunc" , &port_id); +TEST_ASSERT_SUCCESS (ret,"Cannot find added pcap device" ); + +TEST_ASSERT (setup_pcap_port (port_id) ==0 ,"Failed to setup port" ); + +/* Generate packets larger than snaplen */ + nb_gen =generate_test_packets (mp, mbufs,NUM_PACKETS , pkt_size); +TEST_ASSERT_EQUAL (nb_gen,NUM_PACKETS , +"Failed to generate packets: got %d, expected %d" , + nb_gen,NUM_PACKETS ); + +printf (" Sending %d packets of size %u with snaplen=%u\n" , +NUM_PACKETS , pkt_size, test_snaplen); + +/* Transmit packets */ + nb_tx =rte_eth_tx_burst (port_id,0 , mbufs,NUM_PACKETS ); +TEST_ASSERT_EQUAL (nb_tx,NUM_PACKETS , +"TX burst failed: sent %d/%d" , nb_tx,NUM_PACKETS ); + +cleanup_pcap_vdev ("net_pcap_trunc" , port_id); + +/* Verify truncation in output file */ + ret =verify_pcap_truncation (tx_path, test_snaplen, pkt_size, &pkt_count); +TEST_ASSERT_SUCCESS (ret,"Truncation verification failed" ); +TEST_ASSERT_EQUAL (pkt_count, (unsigned int )NUM_PACKETS , +"Packet count mismatch: got %u, expected %d" , + pkt_count,NUM_PACKETS ); + +printf (" Verified %u packets: caplen=%u, len=%u\n" , + pkt_count, test_snaplen, pkt_size); + +/* Cleanup */ +unlink (tx_path); + +printf ("Snaplen truncation test PASSED\n" ); +return TEST_SUCCESS ; +} +/* * Test: VLAN Strip on RX *
@@ -2322,6 +2508,8 @@ static struct unit_test_suite test_pmd_pcap_suite = {TEST_CASE (test_vlan_strip_rx),TEST_CASE (test_vlan_insert_tx),TEST_CASE (test_vlan_no_strip_rx), +TEST_CASE (test_snaplen), +TEST_CASE (test_snaplen_truncation), TEST_CASES_END() } };
doc/guides/nics/pcap_ring.rst +17 -0
@@ -94,6 +94,23 @@ The different stream types are: iface=eth0 +* snaplen: Set snapshot length (maximum capture size) + + The snapshot length limits the maximum size of captured packets. This can be + set with the ``snaplen`` devarg, for example:: + + snaplen=1518 + + This sets the snapshot length to 1518 bytes. The value affects the reported + ``max_rx_pktlen`` and ``max_mtu`` in device info. The default value is 65535 + bytes, matching the default pcap snapshot length. + + When capturing from interfaces, this limits the amount of data captured per + packet. For pcap file output, packets larger than the snapshot length are + truncated: only the first ``snaplen`` bytes are written, while the original + packet length is preserved in the pcap packet header. + + Runtime Config Options ^^^^^^^^^^^^^^^^^^^^^^
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -60,6 +60,7 @@ New Features * Changed transmit burst to always return the number of packets requested. Failed sends are counted as transmit errors. * Added support for VLAN insertion and stripping. + * Added ``snaplen`` devarg to configure packet capture snapshot length. * Receive timestamp offload is only done if offload flag set. * Receive timestamps support nanosecond precision. * Added unit test suite.
drivers/net/pcap/pcap_ethdev.c +97 -68
366 lines not on this page · raw message
Signature · 1 line
2.51.0
v10 cover net/pcap: improvements and test suite
In reply to Stephen HemmingerThis series contains improvements to the PCAP PMD including new features, bug fixes, code cleanup, and a comprehensive test suite.
New features:
- VLAN tag insertion on Tx and stripping on Rx
- Nanosecond precision timestamps (when hardware/libpcap supports it)
- Accurate link state, speed, and duplex reporting in interface mode
- Support for Windows interface mode
- Advertise RTE_ETH_TX_OFFLOAD_MULTI_SEGS capability
- Configurable snapshot length via snapshot_len devarg
Bug fixes:
- Fix multi-segment transmit to dynamically allocate instead of silently truncating packets larger than 9K stack buffer
- Change Tx burst to always consume all packets; failed sends increment error counter rather than leaving mbufs for retry (pcap_sendpacket failures are not transient)
- Reject non-Ethernet interfaces to prevent malformed packets and kernel warnings on FreeBSD/macOS loopback
Code cleanup:
- Convert internal flags from int to bool
- Remove unnecessary casts of void* from rte_zmalloc
- Replace rte_malloc/rte_memcpy with libc equivalents in osdep code
- Include headers explicitly rather than relying on indirect includes
- Remove unnecessary volatile qualifier on statistics
- Reduce scope of file-level variables
- Defer pcap handle opening until device start
- Use bulk free for better Tx performance
Testing:
- Add comprehensive unit test suite covering basic operations, timestamps, jumbo frames, VLAN handling, multi-queue, and more
- Test discovers network interfaces using pcap_findalldevs API for portable interface enumeration across Linux, FreeBSD, macOS, and Windows
v10:
- Split transmit handling into separate patches for bulk free, bounce buffer allocation, and cleanup for easier review
- Add patch to reject non-Ethernet interfaces (fixes loopback issues on FreeBSD/macOS where DLT_NULL is used)
- Test uses pcap_findalldevs() for portable interface discovery instead of hardcoded interface names
v9:
- Add configurable snapshot length parameter (snapshot_len devarg)
- Defer opening of pcap files and interfaces until eth_dev_start() instead of during probe, passing configured snapshot length
v8:
- Fix pcap header length in VLAN transmit
- Fix clang warnings from ethtool link_mode array
v7:
- Drop MTU configuration patch; not necessary as underlying OS commands (ip link, ifconfig) can be used directly
- Add patch to remove unnecessary volatile on queue statistics
- Add test_link_status to unit test suite
- Add Acked-by from Marat Khalili on patch 11
Stephen Hemminger (19):
maintainers: update for pcap driver
doc: update features for PCAP PMD
net/pcap: include used headers
net/pcap: remove unnecessary casts
net/pcap: avoid using rte_malloc and rte_memcpy net/pcap: use bulk free
net/pcap: allocate Tx bounce buffer net/pcap: cleanup transmit buffer handling net/pcap: report multi-segment transmit capability net/pcap: consolidate boolean flag handling net/pcap: support VLAN insert and strip net/pcap: add link state and speed for interface mode net/pcap: support nanosecond timestamp precision net/pcap: reject non-Ethernet interfaces net/pcap: reduce scope of file-level variables net/pcap: avoid use of volatile
net/pcap: clarify maximum received packet net/pcap: add snapshot length devarg
test: add test for pcap PMD
MAINTAINERS | 1 + app/test/meson.build | 2 + app/test/test_pmd_pcap.c | 2622 ++++++++++++++++++++++++ doc/guides/nics/features/pcap.ini | 8 + doc/guides/nics/pcap_ring.rst | 27 + doc/guides/rel_notes/release_26_03.rst | 9 + drivers/net/pcap/pcap_ethdev.c | 655 ++++-- drivers/net/pcap/pcap_osdep.h | 23 + drivers/net/pcap/pcap_osdep_freebsd.c | 79 +- drivers/net/pcap/pcap_osdep_linux.c | 115 +- drivers/net/pcap/pcap_osdep_windows.c | 95 +- 11 files changed, 3398 insertions(+), 238 deletions(-) create mode 100644 app/test/test_pmd_pcap.c
Signature · 1 line
2.51.0
PATCH v10 1/19 maintainers: update for pcap driver
Nominate myself to take care of this since already doing pcapng and pdump code.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -1118,6 +1118,7 @@ F: doc/guides/nics/zxdh.rst F: doc/guides/nics/features/zxdh.ini PCAP PMD +M: Stephen Hemminger <[email protected]> F: drivers/net/pcap/ F: doc/guides/nics/pcap_ring.rst F: doc/guides/nics/features/pcap.ini
Signature · 1 line
2.51.0
PATCH v10 2/19 doc: update features for PCAP PMD
In reply to Stephen HemmingerThe PCAP PMD supports more features that were not flagged in the feature matrix.
--- doc/guides/nics/features/pcap.ini | 7 +++++++ 1 file changed, 7 insertions(+)
doc/guides/nics/features/pcap.ini +7 -0
@@ -4,8 +4,15 @@ ; Refer to default.ini for the full list of available PMD features. ; [Features] +Link status = Y +Queue start/stop = Y +Scattered Rx = Y +Timestamp offload = Y Basic stats = Y +Stats per queue = Y Multiprocess aware = Y +FreeBSD = Y +Linux = Y ARMv7 = Y ARMv8 = Y Power8 = Y
Signature · 1 line
2.51.0
PATCH v10 3/19 net/pcap: include used headers
In reply to Stephen HemmingerInclude the used headers instead of relying on getting the headers indirectly through other headers.
--- drivers/net/pcap/pcap_ethdev.c | 9 ++++++++- drivers/net/pcap/pcap_osdep.h | 1 + 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/net/pcap/pcap_ethdev.c +8 -1
@@ -4,16 +4,23 @@* All rights reserved. */ +#include <stdio.h> #include <stdlib.h> #include <time.h> - +#include <inttypes.h> +#include <errno.h> +#include <sys/time.h> +#include <sys/types.h> #include <pcap.h> #include <rte_cycles.h> +#include <rte_ring.h> +#include <rte_ethdev.h> #include <ethdev_driver.h> #include <ethdev_vdev.h> #include <rte_kvargs.h> #include <rte_malloc.h> +#include <rte_memcpy.h> #include <rte_mbuf.h> #include <rte_mbuf_dyn.h> #include <bus_vdev_driver.h>
drivers/net/pcap/pcap_osdep.h +1 -0
@@ -6,6 +6,7 @@#define _RTE_PCAP_OSDEP_#include <rte_ether.h> +#include <rte_log.h> #define PMD_LOG (level, ...) \ RTE_LOG_LINE_PREFIX(level, ETH_PCAP, "%s(): ", __func__, __VA_ARGS__)
Signature · 1 line
2.51.0
PATCH v10 4/19 net/pcap: remove unnecessary casts
In reply to Stephen HemmingerThe function rte_zmalloc returns void * so cast is unnecessary.
Correct the indentation in that code. Not really worth backporting.
--- drivers/net/pcap/pcap_ethdev.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -7
@@ -1220,9 +1220,8 @@ pmd_init_internals(struct rte_vdev_device *vdev,PMD_LOG (INFO ,"Creating pcap-backed ethdev on numa socket %d" , numa_node); - pp = (struct pmd_process_private *) -rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
@@ -1590,10 +1589,8 @@ pmd_pcap_probe(struct rte_vdev_device *dev)unsigned int i; internal = eth_dev->data->dev_private; - pp = (struct pmd_process_private *) -rte_zmalloc (NULL , -sizeof (struct pmd_process_private ), -RTE_CACHE_LINE_SIZE ); + pp =rte_zmalloc (NULL ,sizeof (struct pmd_process_private ), +RTE_CACHE_LINE_SIZE );if (pp ==NULL ) { PMD_LOG(ERR,
Signature · 1 line
2.51.0
PATCH v10 5/19 net/pcap: avoid using rte_malloc and rte_memcpy
In reply to Stephen HemmingerNo need to use rte_malloc or rte_memcpy in the short code to get MAC address.
--- drivers/net/pcap/pcap_ethdev.c | 3 ++- drivers/net/pcap/pcap_osdep_freebsd.c | 12 +++++------- drivers/net/pcap/pcap_osdep_linux.c | 6 +++--- 3 files changed, 10 insertions(+), 11 deletions(-)
drivers/net/pcap/pcap_ethdev.c +2 -1
@@ -6,6 +6,7 @@#include <stdio.h> #include <stdlib.h> +#include <string.h> #include <time.h> #include <inttypes.h> #include <errno.h>
@@ -1288,7 +1289,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,return -1 ;PMD_LOG (INFO ,"Setting phy MAC for %s" , if_name); -rte_memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); +memcpy (mac_addrs, mac.addr_bytes,RTE_ETHER_ADDR_LEN ); eth_dev->data->mac_addrs = mac_addrs;return 0 ; }
drivers/net/pcap/pcap_osdep_freebsd.c +5 -7
@@ -4,13 +4,11 @@* All rights reserved. */ +#include <string.h> #include <net/if.h> #include <net/if_dl.h> #include <sys/sysctl.h> -#include <rte_malloc.h> -#include <rte_memcpy.h> -#include "pcap_osdep.h" int
@@ -41,19 +39,19 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)if (len ==0 )return -1 ; - buf =rte_malloc (NULL , len,0 ); + buf =malloc (len);if (!buf)return -1 ;if (sysctl (mib,6 , buf, &len,NULL ,0 ) <0 ) { -rte_free (buf); +free (buf);return -1 ; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm +1 ); -rte_memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes,LLADDR (sdl),RTE_ETHER_ADDR_LEN ); -rte_free (buf); +free (buf);return 0 ; }
drivers/net/pcap/pcap_osdep_linux.c +3 -3
@@ -4,12 +4,12 @@* All rights reserved. */ +#include <string.h> +#include <unistd.h> #include <net/if.h> #include <sys/ioctl.h> #include <sys/socket.h> -#include <unistd.h> -#include <rte_memcpy.h> #include <rte_string_fns.h> #include "pcap_osdep.h"
@@ -35,7 +35,7 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)return -1 ; } -rte_memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN ); +memcpy (mac->addr_bytes, ifr.ifr_hwaddr.sa_data,RTE_ETHER_ADDR_LEN );close (if_fd);return 0 ;
Signature · 1 line
2.51.0
PATCH v10 6/19 net/pcap: use bulk free
In reply to Stephen HemmingerUsing function rte_pktmbuf_free_bulk is marginally faster here.
--- drivers/net/pcap/pcap_ethdev.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +6 -9
@@ -423,8 +423,8 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) num_tx++; tx_bytes += caplen; -rte_pktmbuf_free (mbuf); } +rte_pktmbuf_free_bulk (bufs, nb_pkts);/* * Since there's no place to hook a callback when the forwarding
@@ -449,13 +449,10 @@ 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++) { +for (i =0 ; i < nb_pkts; i++) tx_bytes += bufs[i]->pkt_len; -rte_pktmbuf_free (bufs[i]); - } + +rte_pktmbuf_free_bulk (bufs, nb_pkts); tx_queue->tx_stat.pkts += nb_pkts; tx_queue->tx_stat.bytes += tx_bytes;
@@ -494,7 +491,6 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)PMD_LOG (ERR ,"Dropping multi segment PCAP packet. Size (%zd) > max size (%zd)." , len,sizeof (temp_data)); -rte_pktmbuf_free (mbuf);continue ; }
@@ -508,9 +504,10 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)break ; num_tx++; tx_bytes += len; -rte_pktmbuf_free (mbuf); } +rte_pktmbuf_free_bulk (bufs, nb_pkts); + tx_queue->tx_stat.pkts += num_tx; tx_queue->tx_stat.bytes += tx_bytes; tx_queue->tx_stat.err_pkts += i - num_tx;
Signature · 1 line
2.51.0
PATCH v10 7/19 net/pcap: allocate Tx bounce buffer
In reply to Stephen HemmingerTo handle possible multi segment mbufs, the driver would allocate a worst case 64k buffer on the stack. Since each Tx queue is single threaded, better to allocate the buffer from hugepage with rte_malloc when queue is setup.
--- drivers/net/pcap/pcap_ethdev.c | 41 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 15 deletions(-)
drivers/net/pcap/pcap_ethdev.c +26 -15
@@ -12,6 +12,7 @@#include <errno.h> #include <sys/time.h> #include <sys/types.h> +#include <unistd.h> #include <pcap.h> #include <rte_cycles.h>
@@ -91,6 +92,9 @@ struct pcap_tx_queue {struct queue_stat tx_stat;char name[PATH_MAX ];char type[ETH_PCAP_ARG_MAXLEN ]; + +/* Temp buffer used to for non-linear packets */ +uint8_t *bounce_buf; };struct pmd_internals {
@@ -392,11 +396,12 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)uint32_t tx_bytes =0 ;struct pcap_pkthdr header;pcap_dumper_t *dumper; -unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN ]; +unsigned char *temp_data;size_t len, caplen; 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 ;
@@ -406,10 +411,6 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)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); - }calculate_timestamp (&header.ts); header.len = len;
@@ -419,7 +420,7 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)* a pointer to temp_data after copying into it. */ pcap_dump ((u_char *)dumper, &header, -rte_pktmbuf_read (mbuf,0 , caplen, temp_data)); +rte_pktmbuf_read (mbuf,0 , caplen, temp_data)); num_tx++; tx_bytes += caplen;
@@ -474,11 +475,12 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)uint16_t num_tx =0 ;uint32_t tx_bytes =0 ;pcap_t *pcap; -unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN ]; +unsigned char *temp_data;size_t len; 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 ;
@@ -486,13 +488,6 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)for (i =0 ; i < nb_pkts; i++) { mbuf = bufs[i]; len =rte_pktmbuf_pkt_len (mbuf); -if (unlikely (!rte_pktmbuf_is_contiguous (mbuf) && - len >sizeof (temp_data))) { -PMD_LOG (ERR , -"Dropping multi segment PCAP packet. Size (%zd) > max size (%zd)." , - len,sizeof (temp_data)); -continue ; - }/* rte_pktmbuf_read() returns a pointer to the data directly * in the mbuf (when the mbuf is contiguous) or, otherwise,
@@ -962,7 +957,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;
@@ -970,11 +965,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) {
@@ -1015,6 +1025,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,
Signature · 1 line
2.51.0
PATCH v10 8/19 net/pcap: cleanup transmit buffer handling
In reply to Stephen HemmingerThe transmit loops should handle the possible failure of rte_pktmbuf_read(). If a pcap_sendpacket fails, then the packet with error should be freed.
--- drivers/net/pcap/pcap_ethdev.c | 50 ++++++++++++++-------------------- 1 file changed, 21 insertions(+), 29 deletions(-)
drivers/net/pcap/pcap_ethdev.c +21 -29
@@ -389,7 +389,6 @@ 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 ;
@@ -397,7 +396,6 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)struct pcap_pkthdr header;pcap_dumper_t *dumper;unsigned char *temp_data; -size_t len, caplen; pp = rte_eth_devices[dumper_q->port_id].process_private; dumper = pp->tx_dumper[dumper_q->queue_id];
@@ -409,21 +407,22 @@ 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]; +struct rte_mbuf *mbuf = bufs[i]; +uint32_t len, caplen; +const uint8_t *data; + len = caplen =rte_pktmbuf_pkt_len (mbuf);calculate_timestamp (&header.ts); header.len = len; header.caplen = caplen; -/* rte_pktmbuf_read() returns a pointer to the data directly -* in the mbuf (when the mbuf is contiguous) or, otherwise, -* a pointer to temp_data after copying into it. -*/ -pcap_dump ((u_char *)dumper, &header, -rte_pktmbuf_read (mbuf,0 , caplen, temp_data)); - num_tx++; - tx_bytes += caplen; + data =rte_pktmbuf_read (mbuf,0 , caplen, temp_data); +if (likely (data !=NULL )) { +pcap_dump ((u_char *)dumper, &header, data); + num_tx++; + tx_bytes += caplen; + } }rte_pktmbuf_free_bulk (bufs, nb_pkts);
@@ -458,7 +457,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; }/*
@@ -468,15 +467,12 @@ 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; -size_t len; pp = rte_eth_devices[tx_queue->port_id].process_private; pcap = pp->tx_pcap[tx_queue->queue_id];
@@ -486,19 +482,15 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)return 0 ;for (i =0 ; i < nb_pkts; i++) { - mbuf = bufs[i]; - len =rte_pktmbuf_pkt_len (mbuf); - -/* rte_pktmbuf_read() returns a pointer to the data directly -* in the mbuf (when the mbuf is contiguous) or, otherwise, -* a pointer to temp_data after copying into it. -*/ - ret =pcap_sendpacket (pcap, -rte_pktmbuf_read (mbuf,0 , len, temp_data), len); -if (unlikely (ret !=0 )) -break ; - num_tx++; - tx_bytes += len; +struct rte_mbuf *mbuf = bufs[i]; +uint32_t len =rte_pktmbuf_pkt_len (mbuf); +const uint8_t *data; + + data =rte_pktmbuf_read (mbuf,0 , len, temp_data); +if (likely (data !=NULL &&pcap_sendpacket (pcap, data, len) ==0 )) { + num_tx++; + tx_bytes += len; + } }rte_pktmbuf_free_bulk (bufs, nb_pkts);
@@ -507,7 +499,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) tx_queue->tx_stat.bytes += tx_bytes; tx_queue->tx_stat.err_pkts += i - num_tx; -return i; +return nb_pkts; }/*
Signature · 1 line
2.51.0
PATCH v10 9/19 net/pcap: report multi-segment transmit capability
In reply to Stephen HemmingerThe driver has always handled multi-segment transmit but the flag was never set in the offload capabilities.
--- drivers/net/pcap/pcap_ethdev.c | 1 + 1 file changed, 1 insertion(+)
drivers/net/pcap/pcap_ethdev.c +1 -0
@@ -737,6 +737,7 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ;return 0 ; }
Signature · 1 line
2.51.0
PATCH v10 10/19 net/pcap: consolidate boolean flag handling
In reply to Stephen HemmingerConvert internal flag fields from int/unsigned int to bool for clarity and reduced structure size.
Merge the separate select_phy_mac() and get_infinite_rx_arg() functions into a single process_bool_flag() handler. The new function also adds proper validation, rejecting values other than "0", "1", or empty (which defaults to true).
Also change num_of_queue from unsigned int to uint16_t since it cannot exceed RTE_PMD_PCAP_MAX_QUEUES.
--- drivers/net/pcap/pcap_ethdev.c | 69 +++++++++++++++------------------- 1 file changed, 30 insertions(+), 39 deletions(-)
drivers/net/pcap/pcap_ethdev.c +30 -39
@@ -7,6 +7,7 @@#include <stdio.h> #include <stdlib.h> #include <string.h> +#include <stdbool.h> #include <time.h> #include <inttypes.h> #include <errno.h>
@@ -103,9 +104,9 @@ struct pmd_internals {char devargs[ETH_PCAP_ARG_MAXLEN ];struct rte_ether_addr eth_addr;int if_index; -int single_iface; -int phy_mac; -unsigned int infinite_rx; +bool single_iface; +bool phy_mac; +bool infinite_rx; };struct pmd_process_private {
@@ -115,25 +116,25 @@ struct pmd_process_private { };struct pmd_devargs { -unsigned int num_of_queue; +uint16_t num_of_queue; +bool phy_mac;struct devargs_queue {pcap_dumper_t *dumper;pcap_t *pcap;const char *name;const char *type; } queue[RTE_PMD_PCAP_MAX_QUEUES ]; -int phy_mac; };struct pmd_devargs_all {struct pmd_devargs rx_queues;struct pmd_devargs tx_queues; -int single_iface; -unsigned int is_tx_pcap; -unsigned int is_tx_iface; -unsigned int is_rx_pcap; -unsigned int is_rx_iface; -unsigned int infinite_rx; +bool single_iface; +bool is_tx_pcap; +bool is_tx_iface; +bool is_rx_pcap; +bool is_rx_iface; +bool infinite_rx; };static const char *valid_arguments[] = {
@@ -856,7 +857,7 @@ eth_dev_close(struct rte_eth_dev *dev) } } -if (internals->phy_mac ==0 ) +if (!internals->phy_mac)/* not dynamically allocated, must not be freed */ dev->data->mac_addrs =NULL ;
@@ -1181,29 +1182,19 @@ open_tx_iface(const char *key, const char *value, void *extra_args) }static int -select_phy_mac(const char *key __rte_unused, const char *value, -void *extra_args) +process_bool_flag (const char *key,const char *value,void *extra_args) { -if (extra_args) { -const int phy_mac =atoi (value); -int *enable_phy_mac = extra_args; - -if (phy_mac) - *enable_phy_mac =1 ; - } -return 0 ; -} - -static int -get_infinite_rx_arg(const char *key __rte_unused, -const char *value,void *extra_args) -{ -if (extra_args) { -const int infinite_rx =atoi (value); -int *enable_infinite_rx = extra_args; - -if (infinite_rx >0 ) - *enable_infinite_rx =1 ; +bool *flag = extra_args; + +if (value ==NULL || *value =='\0' ) { + *flag = true;/* default with no additional argument */ + }else if (strcmp (value,"0" ) ==0 ) { + *flag = false; + }else if (strcmp (value,"1" ) ==0 ) { + *flag = true; + }else { +PMD_LOG (ERR ,"Invalid '%s' value '%s'" , key, value); +return -1 ; }return 0 ; }
@@ -1479,7 +1470,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) dumpers.queue[0 ] = pcaps.queue[0 ]; ret =rte_kvargs_process (kvlist,ETH_PCAP_PHY_MAC_ARG , - &select_phy_mac, &pcaps.phy_mac); + &process_bool_flag, &pcaps.phy_mac);if (ret <0 ) goto free_kvlist;
@@ -1518,9 +1509,9 @@ pmd_pcap_probe(struct rte_vdev_device *dev)if (infinite_rx_arg_cnt ==1 ) { ret =rte_kvargs_process (kvlist, -ETH_PCAP_INFINITE_RX_ARG , - &get_infinite_rx_arg, - &devargs_all.infinite_rx); +ETH_PCAP_INFINITE_RX_ARG , + &process_bool_flag, + &devargs_all.infinite_rx);if (ret <0 ) goto free_kvlist; PMD_LOG(INFO, "infinite_rx has been %s for %s",
@@ -1670,5 +1661,5 @@ RTE_PMD_REGISTER_PARAM_STRING(net_pcap,ETH_PCAP_RX_IFACE_IN_ARG "=<ifc> " ETH_PCAP_TX_IFACE_ARG "=<ifc> " ETH_PCAP_IFACE_ARG "=<ifc> " -ETH_PCAP_PHY_MAC_ARG "=<int>" +ETH_PCAP_PHY_MAC_ARG "=<0|1> " ETH_PCAP_INFINITE_RX_ARG "=<0|1>");
Signature · 1 line
2.51.0
PATCH v10 11/19 net/pcap: support VLAN insert and strip
In reply to Stephen HemmingerDriver can easily insert VLAN tag strip and insertion similar to how it is handled in virtio and af_packet.
--- doc/guides/nics/features/pcap.ini | 1 + doc/guides/nics/pcap_ring.rst | 11 +++++++++ doc/guides/rel_notes/release_26_03.rst | 4 ++++ drivers/net/pcap/pcap_ethdev.c | 31 ++++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 2 deletions(-)
doc/guides/nics/features/pcap.ini +1 -0
@@ -10,6 +10,7 @@ Scattered Rx = Y Timestamp offload = Y Basic stats = Y Stats per queue = Y +VLAN offload = Y Multiprocess aware = Y FreeBSD = Y Linux = Y
doc/guides/nics/pcap_ring.rst +11 -0
@@ -213,6 +213,17 @@ Otherwise, the first 512 packets from the input pcap file will be discarded by t an error if interface is down, and the PMD itself won't change the status of the external network interface. +Features and Limitations +^^^^^^^^^^^^^^^^^^^^^^^^ + +* The PMD will re-insert the VLAN tag transparently to the packet if the kernel + strips it, as long as the ``RTE_ETH_RX_OFFLOAD_VLAN_STRIP`` is not enabled by the + application. + +* The PMD will transparently insert a VLAN tag to transmitted packets if + ``RTE_ETH_TX_OFFLOAD_VLAN_INSERT`` is enabled and the mbuf has ``RTE_MBUF_F_TX_VLAN`` + set. + Rings-based PMD ~~~~~~~~~~~~~~~
doc/guides/rel_notes/release_26_03.rst +4 -0
@@ -55,6 +55,10 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Updated PCAP ethernet driver.** + + * Added support for VLAN insertion and stripping. + Removed Items -------------
drivers/net/pcap/pcap_ethdev.c +29 -2
@@ -77,6 +77,7 @@ struct queue_missed_stat {struct pcap_rx_queue {uint16_t port_id;uint16_t queue_id; +bool vlan_strip;struct rte_mempool *mb_pool;struct queue_stat rx_stat;struct queue_missed_stat missed_stat;
@@ -107,6 +108,7 @@ struct pmd_internals {bool single_iface;bool phy_mac;bool infinite_rx; +bool vlan_strip; };struct pmd_process_private {
@@ -337,6 +339,10 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } mbuf->pkt_len = len; + +if (pcap_q->vlan_strip) +rte_vlan_strip (mbuf); +uint64_t us = (uint64_t )header->ts.tv_sec *US_PER_S + header->ts.tv_usec; *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us;
@@ -412,9 +418,16 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)uint32_t len, caplen;const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + } + len = caplen =rte_pktmbuf_pkt_len (mbuf);calculate_timestamp (&header.ts); + header.len = len; header.caplen = caplen;
@@ -487,6 +500,12 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)uint32_t len =rte_pktmbuf_pkt_len (mbuf);const uint8_t *data; +if (mbuf->ol_flags &RTE_MBUF_F_TX_VLAN ) { +/* if vlan insert fails treat it as error */ +if (unlikely (rte_vlan_insert (&mbuf) !=0 )) +continue ; + } + data =rte_pktmbuf_read (mbuf,0 , len, temp_data);if (likely (data !=NULL &&pcap_sendpacket (pcap, data, len) ==0 )) { num_tx++;
@@ -721,8 +740,13 @@ eth_dev_stop(struct rte_eth_dev *dev) }static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure (struct rte_eth_dev *dev) { +struct pmd_internals *internals = dev->data->dev_private; +struct rte_eth_conf *dev_conf = &dev->data->dev_conf; +const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; + + internals->vlan_strip = !!(rxmode->offloads &RTE_ETH_RX_OFFLOAD_VLAN_STRIP );return 0 ; }
@@ -738,7 +762,9 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->max_rx_queues = dev->data->nb_rx_queues; dev_info->max_tx_queues = dev->data->nb_tx_queues; dev_info->min_rx_bufsize =0 ; - dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS ; + dev_info->tx_offload_capa =RTE_ETH_TX_OFFLOAD_MULTI_SEGS | +RTE_ETH_TX_OFFLOAD_VLAN_INSERT ; + dev_info->rx_offload_capa =RTE_ETH_RX_OFFLOAD_VLAN_STRIP ;return 0 ; }
@@ -885,6 +911,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->mb_pool = mb_pool; pcap_q->port_id = dev->data->port_id; pcap_q->queue_id = rx_queue_id; + pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q;if (internals->infinite_rx) {
Signature · 1 line
2.51.0
PATCH v10 12/19 net/pcap: add link state and speed for interface mode
In reply to Stephen HemmingerWhen the PCAP PMD is used in pass-through mode with a physical interface (iface=X), the link status was always reported with hardcoded values regardless of the actual interface state.
Add OS-dependent functions to query the real link state, speed, duplex, and autonegotiation settings from the underlying interface. The eth_link_update() callback now returns accurate information when operating in pass-through mode.
Linux uses ETHTOOL_GLINKSETTINGS which supports all speeds up to 800 Gbps. FreeBSD uses SIOCGIFMEDIA, and Windows uses GetAdaptersAddresses().
For pcap file mode or separate rx/tx interface configurations, default values continue to be used since there is no single underlying interface to query.
--- doc/guides/rel_notes/release_26_03.rst | 1 + drivers/net/pcap/pcap_ethdev.c | 93 ++++++++++++++++++--- drivers/net/pcap/pcap_osdep.h | 22 +++++ drivers/net/pcap/pcap_osdep_freebsd.c | 67 +++++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 109 +++++++++++++++++++++++++ drivers/net/pcap/pcap_osdep_windows.c | 95 ++++++++++++++++++--- 6 files changed, 364 insertions(+), 23 deletions(-)
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -58,6 +58,7 @@ New Features * **Updated PCAP ethernet driver.** * Added support for VLAN insertion and stripping. + * Added support for reporting link state and speed in ``iface`` mode. Removed Items
drivers/net/pcap/pcap_ethdev.c +82 -11
113 lines not on this page · raw message
drivers/net/pcap/pcap_osdep.h +22 -0
@@ -13,7 +13,29 @@extern int eth_pcap_logtype;#define RTE_LOGTYPE_ETH_PCAP eth_pcap_logtype +/** +* Link information returned by osdep_iface_link_get(). +*/ +struct osdep_iface_link { +uint32_t link_speed;/**< Speed in Mbps, 0 if unknown */ +uint8_t link_status;/**< 1 = up, 0 = down */ +uint8_t link_duplex;/**< 1 = full, 0 = half */ +uint8_t link_autoneg;/**< 1 = autoneg enabled, 0 = fixed */ +}; +int osdep_iface_index_get (const char *name);int osdep_iface_mac_get (const char *name,struct rte_ether_addr *mac); +/** +* Get link state and speed for a network interface. +* +* @param name +* Interface name (e.g., "eth0" on Linux, "{GUID}" on Windows). +* @param link +* Pointer to structure to fill with link information. +* @return +* 0 on success, -1 on failure. +*/ +int osdep_iface_link_get (const char *name,struct osdep_iface_link *link); +#endif
drivers/net/pcap/pcap_osdep_freebsd.c +67 -0
78 lines not on this page · raw message
drivers/net/pcap/pcap_osdep_linux.c +109 -0
118 lines not on this page · raw message
drivers/net/pcap/pcap_osdep_windows.c +83 -12
124 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v10 13/19 net/pcap: support nanosecond timestamp precision
In reply to Stephen HemmingerEnable nanosecond-precision timestamps for both live capture and pcap file reading.
Replace pcap_open_live() with the pcap_create()/pcap_activate() API, which allows setting PCAP_TSTAMP_PRECISION_NANO before activation. Similarly, use pcap_open_offline_with_tstamp_precision() for reading pcap files. The pcap_pkthdr timestamp field, despite being declared as struct timeval, actually contains nanoseconds (not microseconds) when nanosecond precision is requested.
Make receive timestamp offloading conditional: timestamps are now only written to the mbuf dynamic field when RTE_ETH_RX_OFFLOAD_TIMESTAMP is enabled. Previously, timestamps were unconditionally added to every received packet.
Other related changes:
* Defer timestamp dynfield registration from probe to device start, and only when timestamp offloading is enabled
* Add read_clock dev_op returning current UTC time
for timestamp correlation
* Move per-burst timestamp calculation outside the packet loop in tx_dumper
* Enable immediate mode and improve error reporting
in live capture setup
--- doc/guides/nics/pcap_ring.rst | 3 + doc/guides/rel_notes/release_26_03.rst | 2 + drivers/net/pcap/pcap_ethdev.c | 145 +++++++++++++++++++------ 3 files changed, 114 insertions(+), 36 deletions(-)
doc/guides/nics/pcap_ring.rst +3 -0
@@ -224,6 +224,9 @@ Features and Limitations ``RTE_ETH_TX_OFFLOAD_VLAN_INSERT`` is enabled and the mbuf has ``RTE_MBUF_F_TX_VLAN`` set. +* The PMD will insert the pcap header packet timestamp with nanoseconds resolution and + UNIX origin, i.e. time since 1-JAN-1970 UTC, if ``RTE_ETH_RX_OFFLOAD_TIMESTAMP`` is enabled. + Rings-based PMD ~~~~~~~~~~~~~~~
doc/guides/rel_notes/release_26_03.rst +2 -0
@@ -59,6 +59,8 @@ New Features * Added support for VLAN insertion and stripping. * Added support for reporting link state and speed in ``iface`` mode. + * Receive timestamp offload is only done if offload flag set. + * Receive timestamps support nanosecond precision. Removed Items
drivers/net/pcap/pcap_ethdev.c +109 -36
250 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v10 14/19 net/pcap: reject non-Ethernet interfaces
In reply to Stephen HemmingerThe pcap PMD sends and receives raw Ethernet frames. If used with an interface that has a different link type, packets will be malformed.
On FreeBSD and macOS, the loopback interface uses DLT_NULL which expects a 4-byte address family header instead of an Ethernet header. Sending Ethernet frames to such interfaces causes kernel warnings like:
looutput: af=-1 unexpected
Add a check after pcap_activate() to verify the interface uses DLT_EN10MB (Ethernet) link type and reject others with a clear error.
--- drivers/net/pcap/pcap_ethdev.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/net/pcap/pcap_ethdev.c +11 -0
@@ -578,6 +578,17 @@ open_iface_live(const char *iface, pcap_t **pcap) goto error; } +/* +* Verify interface supports Ethernet link type. +* Loopback on FreeBSD/macOS uses DLT_NULL which expects a 4-byte +* address family header instead of Ethernet, causing kernel warnings. +*/ +if (pcap_datalink (pc) !=DLT_EN10MB ) { +PMD_LOG (ERR ,"%s: not Ethernet (link type %d)" , + iface,pcap_datalink (pc)); + goto error; + } +if (pcap_setnonblock (pc,1 , errbuf)) {PMD_LOG (ERR ,"Couldn't set non-blocking on %s: %s" , iface, errbuf); goto error;
Signature · 1 line
2.51.0
PATCH v10 15/19 net/pcap: reduce scope of file-level variables
In reply to Stephen HemmingerMove errbuf from file scope to local variables in the two functions that use it (open_iface_live and open_single_rx_pcap). This avoids potential issues if these functions were called concurrently, since each call now has its own error buffer. Move iface_idx to a static local variable within pmd_init_internals(), the only function that uses it. The variable remains static to preserve the MAC address uniqueness counter across calls.
--- drivers/net/pcap/pcap_ethdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/pcap/pcap_ethdev.c +4 -2
@@ -47,11 +47,9 @@#define RTE_PMD_PCAP_MAX_QUEUES 16 -static char errbuf[PCAP_ERRBUF_SIZE ];static struct timespec start_time;static uint64_t start_cycles;static uint64_t hz; -static uint8_t iface_idx;static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;
@@ -536,6 +534,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)static inline int open_iface_live (const char *iface,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ];pcap_t *pc;int status;
@@ -647,6 +646,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)static int open_single_rx_pcap (const char *pcap_filename,pcap_t **pcap) { +char errbuf[PCAP_ERRBUF_SIZE ]; + *pcap =pcap_open_offline_with_tstamp_precision (pcap_filename,PCAP_TSTAMP_PRECISION_NANO , errbuf);if (*pcap ==NULL ) {
@@ -1416,6 +1417,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,* derived from: 'locally administered':'p':'c':'a':'p':'iface_idx' * where the middle 4 characters are converted to hex. */ +static uint8_t iface_idx; (*internals)->eth_addr = (struct rte_ether_addr ) { .addr_bytes = {0x02 ,0x70 ,0x63 ,0x61 ,0x70 , iface_idx++ } };
Signature · 1 line
2.51.0
PATCH v10 16/19 net/pcap: avoid use of volatile
In reply to Stephen HemmingerUsing volatile for statistics is not necessary since only one thread is allowed to operate on a queue at a time.
--- drivers/net/pcap/pcap_ethdev.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/net/pcap/pcap_ethdev.c +9 -9
@@ -55,10 +55,10 @@ static uint64_t timestamp_rx_dynflag;static int timestamp_dynfield_offset =-1 ;struct queue_stat { -volatile unsigned long pkts; -volatile unsigned long bytes; -volatile unsigned long err_pkts; -volatile unsigned long rx_nombuf; +uint64_t pkts; +uint64_t bytes; +uint64_t err_pkts; +uint64_t rx_nombuf; };struct queue_missed_stat {
@@ -837,11 +837,11 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, struct eth_queue_stats *qstats) {unsigned int i; -unsigned long rx_packets_total =0 , rx_bytes_total =0 ; -unsigned long rx_missed_total =0 ; -unsigned long rx_nombuf_total =0 , rx_err_total =0 ; -unsigned long tx_packets_total =0 , tx_bytes_total =0 ; -unsigned long tx_packets_err_total =0 ; +uint64_t rx_packets_total =0 , rx_bytes_total =0 ; +uint64_t rx_missed_total =0 ; +uint64_t rx_nombuf_total =0 , rx_err_total =0 ; +uint64_t tx_packets_total =0 , tx_bytes_total =0 ; +uint64_t tx_packets_err_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
Signature · 1 line
2.51.0