pcapng fixes
cover pcapng fixes
There were a couple of reported bugs in dumpcap around timestamps and multiple invocations. This patchset does some refactoring to fix them in a simpler way.
Stephen Hemminger (4):
pdump: fix setting rte_errno on mp error
dumpcap: allow multiple invocations
pcapng: change timestamp argument to write_stats
pcapng: move timestamp calculation into pdump
app/dumpcap/main.c | 31 ++++++++-------- app/test/test_pcapng.c | 4 +-- lib/pcapng/rte_pcapng.c | 79 ++++------------------------------------- lib/pcapng/rte_pcapng.h | 7 ++-- lib/pdump/rte_pdump.c | 61 +++++++++++++++++++++++++++---- 5 files changed, 83 insertions(+), 99 deletions(-)
Signature · 1 line
2.39.2
PATCH 1/4 pdump: fix setting rte_errno on mp error
The response from MP server sets err_value to negative on error. The convention for rte_errno is to use a positive value on error. This makes errors like duplicate registration show up with the correct error value.
--- lib/pdump/rte_pdump.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/pdump/rte_pdump.c +3 -2
@@ -564,9 +564,10 @@ pdump_prepare_client_request(const char *device, uint16_t queue,if (rte_mp_request_sync (&mp_req, &mp_reply, &ts) ==0 ) { mp_rep = &mp_reply.msgs[0 ]; resp = (struct pdump_response *)mp_rep->param; - rte_errno = resp->err_value; -if (!resp->err_value) +if (resp->err_value ==0 ) ret =0 ; +else + rte_errno = -resp->err_value;free (mp_reply.msgs); }
Signature · 1 line
2.39.2
PATCH 2/4 dumpcap: allow multiple invocations
In reply to Stephen HemmingerIf dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
app/dumpcap/main.c +14 -14
@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32
@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */
@@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; } - ring =rte_ring_lookup (RING_NAME ); -if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%u" ,getpid ()); + + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf; -static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%u" ,getpid ());/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {
@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno)); }if (intf->opts.promisc_mode) {
Signature · 1 line
2.39.2
RE: [PATCH 2/4] dumpcap: allow multiple invocations
Quoted 39 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 21 September 2023 06.24If dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
Fixes cbb44143be74 ("app/dumpcap: add new packet capture application")Reported-by Isaac Boukris <[email protected]>Signed-off-by Stephen Hemminger <[email protected]>--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)app/dumpcap/main.c +1
-2@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */ @@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; } - ring =rte_ring_lookup (RING_NAME );
app/dumpcap/main.c static struct rte_ring *create_ring(void) · line 665
Quoted 10 lines
app/dumpcap/main.c +3
-7-if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%u" ,getpid ());
I'm not sure getpid() is available on Windows. How about:
#ifdef _WIN32
#include <processthreadsapi.h> // With the headers, not here. "dumpcap-%lu", GetCurrentProcessId());
#else
"dumpcap-%u", getpid());
#endif
Quoted 13 lines
app/dumpcap/main.c +7
-0+ + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf;
app/dumpcap/main.c static struct rte_ring *create_ring(void) · line 684
Quoted 10 lines
app/dumpcap/main.c +2
-4-static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%u" ,getpid ());
Same regarding getpid().
Quoted 15 lines
app/dumpcap/main.c +1
-1/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp) rte_exit(EXIT_FAILURE,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, - rte_strerror(-ret)); + rte_strerror(rte_errno)); }if (
intf->opts.promisc_mode) {
--
2.39.2
Re: [PATCH 2/4] dumpcap: allow multiple invocations
Quoted 39 lines · Morten Brørup
Quoted 39 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 21 September 2023 06.24If dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
Fixes cbb44143be74 ("app/dumpcap: add new packet capture application")Reported-by Isaac Boukris <[email protected]>Signed-off-by Stephen Hemminger <[email protected]>--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)app/dumpcap/main.c +1
-1@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */ @@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; }
Quoted 13 lines
app/dumpcap/main.c static struct rte_ring *create_ring(void) · line 665
Quoted 11 lines
app/dumpcap/main.c +3
-8- ring =rte_ring_lookup (RING_NAME ); -if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%u" ,getpid ());I'm not sure
getpid()is available on Windows. How about:
I think the 'app/dumpcap/meson.build' file indicates no support for Windows.
Regards
Quoted 49 lines
#ifdef
_WIN32
#include <processthreadsapi.h> // With the headers, not here. "dumpcap-%lu",GetCurrentProcessId());
#else
"dumpcap-%u",getpid());
#endifQuoted 13 lines
app/dumpcap/main.c +7
-0+ + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf;app/dumpcap/main.c static struct rte_ring *create_ring(void) · line 684
Quoted 10 lines
app/dumpcap/main.c +2
-4-static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%u" ,getpid ());Same regarding
getpid().Quoted 15 lines
app/dumpcap/main.c +1
-1/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp) rte_exit(EXIT_FAILURE,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, - rte_strerror(-ret)); + rte_strerror(rte_errno)); }if (intf->opts.promisc_mode) {--
2.39.2
Re: [PATCH 2/4] dumpcap: allow multiple invocations
In reply to Morten BrørupQuoted 8 lines · On Thu, 21 Sep 2023 08:22:12 +0200 Morten Brørup <[email protected]>…
I'm not sure
getpid()is available on Windows. How about:#ifdef
_WIN32
#include <processthreadsapi.h> // With the headers, not here. "dumpcap-%lu",GetCurrentProcessId());
#else
"dumpcap-%u",getpid());
#endif
Dumpcap doesn't support windows because there are lots of things about pdump library that won't work on Windows.
PATCH 3/4 pcapng: change timestamp argument to write_stats
In reply to Stephen HemmingerIn order to cleanup the management of time base calculation, later patch will move the calculation from pcapng to the pdump library. One of the changes necessary is to move the timestamp calculation in the write_stats call from the pcapng library into the caller. Since dumpcap already does this for other timestamps the change is rather small.
--- app/dumpcap/main.c | 3 ++- app/test/test_pcapng.c | 4 ++-- lib/pcapng/rte_pcapng.c | 8 +++----- lib/pcapng/rte_pcapng.h | 5 ++++- 4 files changed, 11 insertions(+), 9 deletions(-)
app/dumpcap/main.c +2 -1
@@ -577,6 +577,7 @@ report_packet_stats(dumpcap_out_t out)struct rte_pdump_stats pdump_stats;struct interface *intf;uint64_t ifrecv, ifdrop; +uint64_t timestamp =create_timestamp ();double percent;fputc ('\n' , stderr);
@@ -590,7 +591,7 @@ report_packet_stats(dumpcap_out_t out)if (use_pcapng)rte_pcapng_write_stats (out.pcapng, intf->port,NULL , - start_time, end_time, + timestamp, start_time, end_time, ifrecv, ifdrop);if (ifrecv ==0 )
app/test/test_pcapng.c +2 -2
@@ -173,8 +173,8 @@ test_write_stats(void)ssize_t len;/* write a statistics block */ - len =rte_pcapng_write_stats (pcapng, port_id, -NULL ,0 ,0 , + len =rte_pcapng_write_stats (pcapng, port_id,NULL , +0 ,0 ,0 ,NUM_PACKETS ,0 );if (len <=0 ) {fprintf (stderr,"Write of statistics failed\n" );
lib/pcapng/rte_pcapng.c +3 -5
@@ -368,7 +368,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port,*/ ssize_t rte_pcapng_write_stats (rte_pcapng_t *self,uint16_t port_id, -const char *comment, +const char *comment,uint64_t sample_time,uint64_t start_time,uint64_t end_time,uint64_t ifrecv,uint64_t ifdrop) {
@@ -376,7 +376,6 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,struct pcapng_option *opt;uint32_t optlen, len;uint8_t *buf; -uint64_t ns;RTE_ETH_VALID_PORTID_OR_ERR_RET (port_id, -EINVAL );
@@ -425,9 +424,8 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, hdr->block_length = len; hdr->interface_id = self->port_index[port_id]; - ns =pcapng_tsc_to_ns (rte_get_tsc_cycles ()); - hdr->timestamp_hi = ns >>32 ; - hdr->timestamp_lo = (uint32_t )ns; + hdr->timestamp_hi = sample_time >>32 ; + hdr->timestamp_lo = (uint32_t )sample_time;/* clone block_length after option */ memcpy (opt, &len,sizeof (uint32_t ));
lib/pcapng/rte_pcapng.h +4 -1
@@ -189,7 +189,9 @@ rte_pcapng_write_packets(rte_pcapng_t *self,* @param port * The Ethernet port to report stats on. * @param comment -* Optional comment to add to statistics. +* Optional comment to add to statistics. +* @param timestamp +* Time this statistic sample refers to in nanoseconds. * @param start_time * The time when packet capture was started in nanoseconds. * Optional: can be zero if not known.
@@ -209,6 +211,7 @@ __rte_experimentalssize_t rte_pcapng_write_stats (rte_pcapng_t *self,uint16_t port,const char *comment, +uint64_t timestamp,uint64_t start_time,uint64_t end_time,uint64_t ifrecv,uint64_t ifdrop);
Signature · 1 line
2.39.2
PATCH 4/4 pcapng: move timestamp calculation into pdump
In reply to Stephen HemmingerThe computation of timestamp is more easily done in pdump than pcapng. The initialization is easier and makes the pcapng library have no global state.
It also makes it easier to add HW timestamp support later.
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
--- lib/pcapng/rte_pcapng.c | 71 ++--------------------------------------- lib/pcapng/rte_pcapng.h | 2 +- lib/pdump/rte_pdump.c | 56 +++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 74 deletions(-)
lib/pcapng/rte_pcapng.c +3 -68
@@ -25,7 +25,6 @@#include <rte_mbuf.h> #include <rte_os_shim.h> #include <rte_pcapng.h> -#include <rte_reciprocal.h> #include <rte_time.h> #include "pcapng_proto.h"
@@ -43,15 +42,6 @@ struct rte_pcapng {uint32_t port_index[RTE_MAX_ETHPORTS ]; }; -/* For converting TSC cycles to PCAPNG ns format */ -static struct pcapng_time { -uint64_t ns; -uint64_t cycles; -uint64_t tsc_hz; -struct rte_reciprocal_u64 tsc_hz_inverse; -} pcapng_time; - -#ifdef RTE_EXEC_ENV_WINDOWS /* * Windows does not have writev() call.
@@ -102,58 +92,6 @@ static ssize_t writev(int fd, const struct iovec *iov, int iovcnt)#define if_indextoname (ifindex, ifname) NULL#endif -static inline void -pcapng_init (void ) -{ -struct timespec ts; - - pcapng_time.cycles =rte_get_tsc_cycles (); -clock_gettime (CLOCK_REALTIME , &ts); - pcapng_time.cycles = (pcapng_time.cycles +rte_get_tsc_cycles ()) /2 ; - pcapng_time.ns =rte_timespec_to_ns (&ts); - - pcapng_time.tsc_hz =rte_get_tsc_hz (); - pcapng_time.tsc_hz_inverse =rte_reciprocal_value_u64 (pcapng_time.tsc_hz); -} - -/* PCAPNG timestamps are in nanoseconds */ -static uint64_t pcapng_tsc_to_ns (uint64_t cycles) -{ -uint64_t delta, secs; - -if (!pcapng_time.tsc_hz) -pcapng_init (); - -/* In essence the calculation is: -* delta = (cycles - pcapng_time.cycles) * NSEC_PRE_SEC / rte_get_tsc_hz() -* but this overflows within 4 to 8 seconds depending on TSC frequency. -* Instead, if delta >= pcapng_time.tsc_hz: -* Increase pcapng_time.ns and pcapng_time.cycles by the number of -* whole seconds in delta and reduce delta accordingly. -* delta will therefore always lie in the interval [0, pcapng_time.tsc_hz), -* which will not overflow when multiplied by NSEC_PER_SEC provided the -* TSC frequency < approx 18.4GHz. -* -* Currently all TSCs operate below 5GHz. -*/ - delta = cycles - pcapng_time.cycles; -if (unlikely (delta >= pcapng_time.tsc_hz)) { -if (likely (delta < pcapng_time.tsc_hz *2 )) { - delta -= pcapng_time.tsc_hz; - pcapng_time.cycles += pcapng_time.tsc_hz; - pcapng_time.ns +=NSEC_PER_SEC ; - }else { - secs =rte_reciprocal_divide_u64 (delta, &pcapng_time.tsc_hz_inverse); - delta -= secs * pcapng_time.tsc_hz; - pcapng_time.cycles += secs * pcapng_time.tsc_hz; - pcapng_time.ns += secs *NSEC_PER_SEC ; - } - } - -return pcapng_time.ns +rte_reciprocal_divide_u64 (delta *NSEC_PER_SEC , - &pcapng_time.tsc_hz_inverse); -} -/* length of option including padding */ static uint16_t pcapng_optlen (uint16_t len) {
@@ -518,7 +456,7 @@ struct rte_mbuf * rte_pcapng_copy(uint16_t port_id, uint32_t queue, const struct rte_mbuf *md, struct rte_mempool *mp, - uint32_t length, uint64_t cycles, + uint32_t length, uint64_t timestamp, enum rte_pcapng_direction direction, const char *comment) {
@@ -527,14 +465,11 @@ rte_pcapng_copy(uint16_t port_id, uint32_t queue,struct pcapng_option *opt;uint16_t optlen;struct rte_mbuf *mc; -uint64_t ns;bool rss_hash;#ifdef RTE_LIBRTE_ETHDEV_DEBUG RTE_ETH_VALID_PORTID_OR_ERR_RET (port_id,NULL );#endif - ns =pcapng_tsc_to_ns (cycles); - orig_len =rte_pktmbuf_pkt_len (md);/* Take snapshot of the data */
@@ -639,8 +574,8 @@ rte_pcapng_copy(uint16_t port_id, uint32_t queue,/* Interface index is filled in later during write */ mc->port = port_id; - epb->timestamp_hi = ns >>32 ; - epb->timestamp_lo = (uint32_t )ns; + epb->timestamp_hi = timestamp >>32 ; + epb->timestamp_lo = (uint32_t )timestamp; epb->capture_length = data_len; epb->original_length = orig_len;
lib/pcapng/rte_pcapng.h +1 -1
@@ -122,7 +122,7 @@ enum rte_pcapng_direction {* The upper limit on bytes to copy. Passing UINT32_MAX * means all data (after offset). * @param timestamp -* The timestamp in TSC cycles. +* The timestamp in nanoseconds since 1/1/1970. * @param direction * The direction of the packer: receive, transmit or unknown. * @param comment
lib/pdump/rte_pdump.c +51 -5
98 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH 4/4] pcapng: move timestamp calculation into pdump
Hello Stephen,
Quoted 11 lines · Stephen Hemminger
The computation of timestamp is more easily done in pdump than pcapng. The initialization is easier and makes the pcapng library have no global state.
It also makes it easier to add HW timestamp support later.
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
Bugzilla ID: 1291 ?
This patch (and patch 3) updates some pcapng API, is it worth a RN update?
Quoted 1 line
Fixes c882eb544842 ("pcapng: fix timestamp wrapping in output files")
Is it worth backporting?
I would say no, as some API update was needed to fix the issue. But on the other hand, this is an experimental API, so I prefer to ask.
Quoted 1 line
Signed-off-by Stephen Hemminger <[email protected]>
Signature · 1 line
David Marchand
Re: [PATCH 4/4] pcapng: move timestamp calculation into pdump
Quoted 4 lines · On Mon, 2 Oct 2023 10:15:25 +0200 David Marchand <[email protected]>…
Quoted 1 line
Bugzilla ID: 1291 ?
Quoted 10 lines
This patch (and patch 3) updates some pcapng API, is it worth a RN update?
Quoted 1 line
Fixes c882eb544842 ("pcapng: fix timestamp wrapping in output files")Is it worth backporting?
I would say no, as some API update was needed to fix the issue. But on the other hand, this is an experimental API, so I prefer to ask.Quoted 1 line
Signed-off-by Stephen Hemminger <[email protected]>
Good question.
Is experimental API allowed to change in a stable release?
Re: [PATCH 4/4] pcapng: move timestamp calculation into pdump
Quoted 17 lines · Stephen Hemminger
Quoted 13 lines · On Mon, 2 Oct 2023 10:15:25 +0200 David Marchand <[email protected]>…
Quoted 1 line
Bugzilla ID: 1291 ?
This patch (and patch 3) updates some pcapng API, is it worth a RN update?
Quoted 1 line
Fixes c882eb544842 ("pcapng: fix timestamp wrapping in output files")Is it worth backporting?
I would say no, as some API update was needed to fix the issue. But on the other hand, this is an experimental API, so I prefer to ask.
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Stephen Hemminger <[email protected]>Good question.
Is experimental API allowed to change in a stable release?
I don't think this is cleary described in our ABI policy. An experimental API may be changed at any time, but nothing is said wrt backports.
Breaking an API is always a pain, and for a LTS release it would probably be badly accepted by users.
We may need a clarification on this topic in the doc.
Signature · 1 line
David Marchand
Re: [PATCH 4/4] pcapng: move timestamp calculation into pdump
Quoted 24 lines · David Marchand
Quoted 17 lines · Stephen Hemminger
Quoted 13 lines · On Mon, 2 Oct 2023 10:15:25 +0200 David Marchand <[email protected]>…
Quoted 1 line
Bugzilla ID: 1291 ?
This patch (and patch 3) updates some pcapng API, is it worth a RN update?
Quoted 1 line
Fixes c882eb544842 ("pcapng: fix timestamp wrapping in output files")Is it worth backporting?
I would say no, as some API update was needed to fix the issue. But on the other hand, this is an experimental API, so I prefer to ask.Quoted 4 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Stephen Hemminger <[email protected]>Good question.
Is experimental API allowed to change in a stable release?
Quoted 7 lines
I don't think this is cleary described in our ABI policy. An experimental API may be changed at any time, but nothing is said wrt backports.
Breaking an API is always a pain, and for a LTS release it would probably be badly accepted by users.
yes, I agree. IIRC, this arose sometime in the past with a branch that Luca was maintaining and I think the consensus among LTS maintainers was not to change experimental API on stable branches.
Quoted 5 lines
Cc Kevin for his opinion.We may need a clarification on this topic in the doc.
Perhaps it's not a "rule" since experimental API comes with no guarantee, but I can add something to the docs that it is a guideline not to break experimental API on stable branch.
v2 cover dumpcap and pcapng fixes
In reply to Stephen HemmingerThis version slightly modifies the pcapng API to fix issues related to timestamping. The design choices are to maximize performance in the primary process; and do all the time adjustment in the secondary (dumpcap) since the dumpcap needs to system calls anyway to write the result.
This patches set changes where the adjustment is calculated into the pcapng portion that opens the output file. All details of the format of timestamp are contained inside pcapng (data hiding).
Stephen Hemminger (4):
pdump: fix setting rte_errno on mp error
dumpcap: allow multiple invocations
pcapng: modify timestamp calculation
test: cleanups to pcapng test
app/dumpcap/main.c | 53 +++--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 378 +++++++++++++++++++++++++--------------- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 119 +++++-------- lib/pcapng/rte_pcapng.h | 19 +- lib/pdump/rte_pdump.c | 9 +- 7 files changed, 318 insertions(+), 264 deletions(-)
Signature · 1 line
2.39.2
PATCH v2 1/4 pdump: fix setting rte_errno on mp error
The response from MP server sets err_value to negative on error. The convention for rte_errno is to use a positive value on error. This makes errors like duplicate registration show up with the correct error value.
--- lib/pdump/rte_pdump.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/pdump/rte_pdump.c +3 -2
@@ -564,9 +564,10 @@ pdump_prepare_client_request(const char *device, uint16_t queue,if (rte_mp_request_sync (&mp_req, &mp_reply, &ts) ==0 ) { mp_rep = &mp_reply.msgs[0 ]; resp = (struct pdump_response *)mp_rep->param; - rte_errno = resp->err_value; -if (!resp->err_value) +if (resp->err_value ==0 ) ret =0 ; +else + rte_errno = -resp->err_value;free (mp_reply.msgs); }
Signature · 1 line
2.39.2
PATCH v2 2/4 dumpcap: allow multiple invocations
In reply to Stephen HemmingerIf dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
app/dumpcap/main.c +14 -14
@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32
@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */
@@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; } - ring =rte_ring_lookup (RING_NAME ); -if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%u" ,getpid ()); + + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf; -static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%u" ,getpid ());/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {
@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno)); }if (intf->opts.promisc_mode) {
Signature · 1 line
2.39.2
PATCH v2 3/4 pcapng: modify timestamp calculation
In reply to Stephen HemmingerThe computation of timestamp is best done in the part of pcapng library that is in secondary process. The secondary process is already doing a bunch of system calls which makes it not performance sensitive.
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
--- app/dumpcap/main.c | 25 +++------ app/test/test_pcapng.c | 4 +- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 119 +++++++++++++++------------------------- lib/pcapng/rte_pcapng.h | 19 ++----- lib/pdump/rte_pdump.c | 4 +- 6 files changed, 61 insertions(+), 112 deletions(-)
app/dumpcap/main.c +7 -18
@@ -66,13 +66,13 @@ static bool print_stats;/* capture limit options */ static struct { -uint64_t duration;/* nanoseconds */ +time_t duration;/* seconds */ unsigned long packets;/* number of packets in file */ size_t size;/* file size (bytes) */ } stop;/* Running state */ -static uint64_t start_time, end_time; +static time_t start_time;static uint64_t packets_received;static size_t file_size;
@@ -197,7 +197,7 @@ static void auto_stop(char *opt)if (*value =='\0' || *endp !='\0' || interval <=0 )rte_exit (EXIT_FAILURE ,"Invalid duration \"%s\"\n" , value); - stop.duration =NSEC_PER_SEC * interval; + stop.duration = interval; }else if (strcmp (opt,"filesize" ) ==0 ) { stop.size =get_uint (value,"filesize" ,0 ) *1024 ; }else if (strcmp (opt,"packets" ) ==0 ) {
@@ -511,15 +511,6 @@ static void statistics_loop(void) } } -/* Return the time since 1/1/1970 in nanoseconds */ -static uint64_t create_timestamp (void ) -{ -struct timespec now; - -clock_gettime (CLOCK_MONOTONIC , &now); -return rte_timespec_to_ns (&now); -} -static void cleanup_pdump_resources (void ) {
@@ -589,9 +580,8 @@ report_packet_stats(dumpcap_out_t out) ifdrop = pdump_stats.nombuf + pdump_stats.ringfull;if (use_pcapng) -rte_pcapng_write_stats (out.pcapng, intf->port,NULL , - start_time, end_time, - ifrecv, ifdrop); +rte_pcapng_write_stats (out.pcapng, intf->port, + ifrecv, ifdrop,NULL );if (ifrecv ==0 ) percent =0 ;
@@ -983,7 +973,7 @@ int main(int argc, char **argv) mp =create_mempool (); out =create_output (); - start_time =create_timestamp (); + start_time =time (NULL );enable_pdump (r, mp);if (!quiet) {
@@ -1005,11 +995,10 @@ int main(int argc, char **argv)break ;if (stop.duration !=0 && -create_timestamp () - start_time > stop.duration) +time (NULL ) - start_time > stop.duration)break ; } - end_time =create_timestamp ();disable_primary_monitor ();if (rte_eal_primary_proc_alive (NULL ))
app/test/test_pcapng.c +2 -2
@@ -173,8 +173,8 @@ test_write_stats(void)ssize_t len;/* write a statistics block */ - len =rte_pcapng_write_stats (pcapng, port_id, -NULL ,0 ,0 , + len =rte_pcapng_write_stats (pcapng, port_id,NULL , +0 ,0 ,0 ,NUM_PACKETS ,0 );if (len <=0 ) {fprintf (stderr,"Write of statistics failed\n" );
lib/graph/graph_pcap.c +1 -1
@@ -214,7 +214,7 @@ graph_pcap_dispatch(struct rte_graph *graph, mbuf = (struct rte_mbuf *)objs[i]; mc =rte_pcapng_copy (mbuf->port,0 , mbuf, pkt_mp, mbuf->pkt_len, -rte_get_tsc_cycles (),0 , buffer); +0 , buffer);if (mc ==NULL )break ;
lib/pcapng/rte_pcapng.c +45 -74
221 lines not on this page · raw message
lib/pcapng/rte_pcapng.h +5 -14
@@ -121,8 +121,6 @@ enum rte_pcapng_direction {* @param length * The upper limit on bytes to copy. Passing UINT32_MAX * means all data (after offset). -* @param timestamp -* The timestamp in TSC cycles. * @param direction * The direction of the packer: receive, transmit or unknown. * @param comment
@@ -136,7 +134,7 @@ __rte_experimentalstruct rte_mbuf *rte_pcapng_copy (uint16_t port_id,uint32_t queue,const struct rte_mbuf *m,struct rte_mempool *mp, -uint32_t length,uint64_t timestamp, +uint32_t length,enum rte_pcapng_direction direction,const char *comment);
@@ -188,29 +186,22 @@ rte_pcapng_write_packets(rte_pcapng_t *self,* The handle to the packet capture file * @param port * The Ethernet port to report stats on. -* @param comment -* Optional comment to add to statistics. -* @param start_time -* The time when packet capture was started in nanoseconds. -* Optional: can be zero if not known. -* @param end_time -* The time when packet capture was stopped in nanoseconds. -* Optional: can be zero if not finished; * @param ifrecv * The number of packets received by capture. * Optional: use UINT64_MAX if not known. * @param ifdrop * The number of packets missed by the capture process. * Optional: use UINT64_MAX if not known. +* @param comment +* Optional comment to add to statistics. * @return * number of bytes written to file, -1 on failure to write file */ __rte_experimental ssize_trte_pcapng_write_stats (rte_pcapng_t *self,uint16_t port, -const char *comment, -uint64_t start_time,uint64_t end_time, -uint64_t ifrecv,uint64_t ifdrop); +uint64_t ifrecv,uint64_t ifdrop, +const char *comment); #ifdef __cplusplus }
lib/pdump/rte_pdump.c +1 -3
@@ -90,7 +90,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,int ring_enq;uint16_t d_pkts =0 ;struct rte_mbuf *dup_bufs[nb_pkts]; -uint64_t ts;struct rte_ring *ring;struct rte_mempool *mp;struct rte_mbuf *p;
@@ -99,7 +98,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->filter)rte_bpf_exec_burst (cbs->filter, (void **)pkts, rcs, nb_pkts); - ts =rte_get_tsc_cycles (); ring = cbs->ring; mp = cbs->mp;for (i =0 ; i < nb_pkts; i++) {
@@ -122,7 +120,7 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->ver ==V2 ) p =rte_pcapng_copy (port_id, queue, pkts[i], mp, cbs->snaplen, - ts, direction,NULL ); + direction,NULL );else p =rte_pktmbuf_copy (pkts[i], mp,0 , cbs->snaplen);
Signature · 1 line
2.39.2
PATCH v2 4/4 test: cleanups to pcapng test
In reply to Stephen HemmingerOverhaul of the pcapng test:
- promote it to be a fast test so it gets regularly run.
- create null device and use i.
- use UDP discard packets that are valid so that for debugging the resulting pcapng file can be looked at with wireshark.
- do basic checks on resulting pcap file that lengths and timestamps are in range.
--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 378 ++++++++++++++++++++++++++--------------- 2 files changed, 242 insertions(+), 138 deletions(-)
app/test/meson.build +1 -1
@@ -124,7 +124,7 @@ source_file_deps = { 'test_meter.c': ['meter'], 'test_metrics.c': ['metrics'], 'test_mp_secondary.c': ['hash', 'lpm'], - 'test_pcapng.c': ['ethdev', 'net', 'pcapng'], + 'test_pcapng.c': ['ethdev', 'net', 'pcapng', 'bus_vdev'], 'test_pdcp.c': ['eventdev', 'pdcp', 'net', 'timer', 'security'], 'test_pdump.c': ['pdump'] + sample_packet_forward_deps, 'test_per_lcore.c': [],
app/test/test_pcapng.c +241 -137
512 lines not on this page · raw message
Signature · 1 line
2.39.2
v3 cover dumpcap and pcapng fixes
In reply to Stephen HemmingerThis has bugfixes and tests for dumpcap and pcapng. It should be in 23.11 but seems to have been ignored.
It fixes issues related to timestamping. The design choices are to maximize performance in the primary process; and do all the time adjustment in the secondary (dumpcap) since the dumpcap needs to system calls anyway to write the result.
This patches set changes where the adjustment is calculated into the pcapng portion that opens the output file. All details of the format of timestamp are contained inside pcapng (data hiding).
v3 - don't use alloca() since can have VLA type issues
Stephen Hemminger (5):
pdump: fix setting rte_errno on mp error
dumpcap: allow multiple invocations
pcapng: modify timestamp calculation
pcapng: avoid using alloca()
test: cleanups to pcapng test
app/dumpcap/main.c | 53 ++--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 418 +++++++++++++++++++++++++++------------- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 153 ++++++--------- lib/pcapng/rte_pcapng.h | 19 +- lib/pdump/rte_pdump.c | 9 +- 7 files changed, 371 insertions(+), 285 deletions(-)
Signature · 1 line
2.39.2
PATCH v3 1/5 pdump: fix setting rte_errno on mp error
The response from MP server sets err_value to negative on error. The convention for rte_errno is to use a positive value on error. This makes errors like duplicate registration show up with the correct error value.
--- lib/pdump/rte_pdump.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/pdump/rte_pdump.c +3 -2
@@ -564,9 +564,10 @@ pdump_prepare_client_request(const char *device, uint16_t queue,if (rte_mp_request_sync (&mp_req, &mp_reply, &ts) ==0 ) { mp_rep = &mp_reply.msgs[0 ]; resp = (struct pdump_response *)mp_rep->param; - rte_errno = resp->err_value; -if (!resp->err_value) +if (resp->err_value ==0 ) ret =0 ; +else + rte_errno = -resp->err_value;free (mp_reply.msgs); }
Signature · 1 line
2.39.2
RE: [PATCH v3 1/5] pdump: fix setting rte_errno on mp error
Quoted 11 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 8 November 2023 19.36The response from MP server sets
err_valueto negative on error. The convention forrte_errnois to use a positive value on error. This makes errors like duplicate registration show up with the correct error value.Fixes 660098d61f57 ("pdump: use generic multi-process channel")Signed-off-by Stephen Hemminger <[email protected]>---
PATCH v3 2/5 dumpcap: allow multiple invocations
In reply to Stephen HemmingerIf dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
app/dumpcap/main.c +14 -14
@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32
@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */
@@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; } - ring =rte_ring_lookup (RING_NAME ); -if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%u" ,getpid ()); + + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf; -static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%u" ,getpid ());/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {
@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno)); }if (intf->opts.promisc_mode) {
Signature · 1 line
2.39.2
RE: [PATCH v3 2/5] dumpcap: allow multiple invocations
Quoted 11 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 8 November 2023 19.36If dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
Fixes cbb44143be74 ("app/dumpcap: add new packet capture application")Reported-by Isaac Boukris <[email protected]>Signed-off-by Stephen Hemminger <[email protected]>---
Minor detail: getpid() returns int, so prefer %d over %u.
[...]
app/dumpcap/main.c static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp) · line 829
Quoted 5 lines
app/dumpcap/main.c +1
-1rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno));
This bugfix (the line above, not the patch itself) supports Tyler's proposal to standardize on returning -1 with rte_errno set on failure, instead of some functions returning -errno. Our dual convention for function return values will cause many bugs like this.
With %u or %d,
Re: [PATCH v3 2/5] dumpcap: allow multiple invocations
Quoted 4 lines · On Thu, 9 Nov 2023 08:50:10 +0100 Morten Brørup <[email protected]>…
Quoted 2 lines
Signed-off-by Stephen Hemminger <[email protected]>---
Minor detail:
getpid()returns int, so prefer %d over %u.
Let me check, per man page. getpid() returns pid_t. The typedef chain leads to:
pid_t -> __pid_t -> __PID_T_TYPE -> __S32_TYPE -> int32 -> int
RE: [PATCH v3 2/5] dumpcap: allow multiple invocations
Quoted 6 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 9 November 2023 16.40On Thu, 9 Nov 2023 08:50:10 +0100
Morten Brørup <[email protected]> wrote:
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Signed-off-by Stephen Hemminger <[email protected]>---
Minor detail:
getpid()returns int, so prefer %d over %u.Let me check, per man page.
getpid()returnspid_t. The typedef chain leads to:pid_t -> __pid_t -> __PID_T_TYPE -> __S32_TYPE -> int32 -> int
Thank you for confirming. So %d is preferred over %u for getpid(). :-)
Re: [PATCH v3 2/5] dumpcap: allow multiple invocations
In reply to Morten BrørupQuoted 7 lines · On Thu, 9 Nov 2023 08:50:10 +0100 Morten Brørup <[email protected]>…
app/dumpcap/main.c static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp) · line 829
Quoted 5 lines
app/dumpcap/main.c +1
-1rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno));This bugfix (the line above, not the patch itself) supports Tyler's proposal to standardize on returning -1 with
rte_errnoset on failure, instead of some functions returning -errno. Our dual convention for function return values will cause many bugs like this.
The error case here is when rte_pdump_enable_bpf() fails.
This is return from pdump_enable in pdump library.
The library does follow the rte_errno convention correctly.
But the error message wasn't reporting correctly which would lead to confusing error in case where multiple invocations failed.
It is not possible to do multiple captures on same interface. And not worth modifying the library (would require multiple copies and ref counts) to handle this case.
RE: [PATCH v3 2/5] dumpcap: allow multiple invocations
Quoted 13 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 9 November 2023 18.16Quoted 7 lines · On Thu, 9 Nov 2023 08:50:10 +0100 Morten Brørup <[email protected]>…
app/dumpcap/main.c static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp) · line 829
Quoted 5 lines
app/dumpcap/main.c +1
-1rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno));This bugfix (the line above, not the patch itself) supports Tyler's
Quoted 7 lines
proposal to standardize on returning -1 with
rte_errnoset on failure, instead of some functions returning -errno. Our dual convention for function return values will cause many bugs like this.The error case here is when
rte_pdump_enable_bpf()fails. This is return frompdump_enablein pdump library. The library does follow therte_errnoconvention correctly.
I'm sorry about being unclear in my comment about rte_errno conventions; it was not targeted at this library.
My comment was meant as general support for Tyler's suggestion, using this as an example of a bug that would not have been there if the return convention was always -1 with rte_errno.
With the dual return convention, it's amazing that you caught this bug.
Quoted 8 lines
But the error message wasn't reporting correctly which would lead to confusing error in case where
multiple invocations failed.It is not possible to do multiple captures on same interface. And not worth modifying the
library (would require multiple copies and ref counts) to handle this case.
PATCH v3 3/5 pcapng: modify timestamp calculation
In reply to Stephen HemmingerThe computation of timestamp is best done in the part of pcapng library that is in secondary process. The secondary process is already doing a bunch of system calls which makes it not performance sensitive.
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
--- app/dumpcap/main.c | 25 +++------ app/test/test_pcapng.c | 4 +- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 119 +++++++++++++++------------------------- lib/pcapng/rte_pcapng.h | 19 ++----- lib/pdump/rte_pdump.c | 4 +- 6 files changed, 61 insertions(+), 112 deletions(-)
app/dumpcap/main.c +7 -18
@@ -66,13 +66,13 @@ static bool print_stats;/* capture limit options */ static struct { -uint64_t duration;/* nanoseconds */ +time_t duration;/* seconds */ unsigned long packets;/* number of packets in file */ size_t size;/* file size (bytes) */ } stop;/* Running state */ -static uint64_t start_time, end_time; +static time_t start_time;static uint64_t packets_received;static size_t file_size;
@@ -197,7 +197,7 @@ static void auto_stop(char *opt)if (*value =='\0' || *endp !='\0' || interval <=0 )rte_exit (EXIT_FAILURE ,"Invalid duration \"%s\"\n" , value); - stop.duration =NSEC_PER_SEC * interval; + stop.duration = interval; }else if (strcmp (opt,"filesize" ) ==0 ) { stop.size =get_uint (value,"filesize" ,0 ) *1024 ; }else if (strcmp (opt,"packets" ) ==0 ) {
@@ -511,15 +511,6 @@ static void statistics_loop(void) } } -/* Return the time since 1/1/1970 in nanoseconds */ -static uint64_t create_timestamp (void ) -{ -struct timespec now; - -clock_gettime (CLOCK_MONOTONIC , &now); -return rte_timespec_to_ns (&now); -} -static void cleanup_pdump_resources (void ) {
@@ -589,9 +580,8 @@ report_packet_stats(dumpcap_out_t out) ifdrop = pdump_stats.nombuf + pdump_stats.ringfull;if (use_pcapng) -rte_pcapng_write_stats (out.pcapng, intf->port,NULL , - start_time, end_time, - ifrecv, ifdrop); +rte_pcapng_write_stats (out.pcapng, intf->port, + ifrecv, ifdrop,NULL );if (ifrecv ==0 ) percent =0 ;
@@ -983,7 +973,7 @@ int main(int argc, char **argv) mp =create_mempool (); out =create_output (); - start_time =create_timestamp (); + start_time =time (NULL );enable_pdump (r, mp);if (!quiet) {
@@ -1005,11 +995,10 @@ int main(int argc, char **argv)break ;if (stop.duration !=0 && -create_timestamp () - start_time > stop.duration) +time (NULL ) - start_time > stop.duration)break ; } - end_time =create_timestamp ();disable_primary_monitor ();if (rte_eal_primary_proc_alive (NULL ))
app/test/test_pcapng.c +2 -2
@@ -173,8 +173,8 @@ test_write_stats(void)ssize_t len;/* write a statistics block */ - len =rte_pcapng_write_stats (pcapng, port_id, -NULL ,0 ,0 , + len =rte_pcapng_write_stats (pcapng, port_id,NULL , +0 ,0 ,0 ,NUM_PACKETS ,0 );if (len <=0 ) {fprintf (stderr,"Write of statistics failed\n" );
lib/graph/graph_pcap.c +1 -1
@@ -214,7 +214,7 @@ graph_pcap_dispatch(struct rte_graph *graph, mbuf = (struct rte_mbuf *)objs[i]; mc =rte_pcapng_copy (mbuf->port,0 , mbuf, pkt_mp, mbuf->pkt_len, -rte_get_tsc_cycles (),0 , buffer); +0 , buffer);if (mc ==NULL )break ;
lib/pcapng/rte_pcapng.c +45 -74
221 lines not on this page · raw message
lib/pcapng/rte_pcapng.h +5 -14
@@ -121,8 +121,6 @@ enum rte_pcapng_direction {* @param length * The upper limit on bytes to copy. Passing UINT32_MAX * means all data (after offset). -* @param timestamp -* The timestamp in TSC cycles. * @param direction * The direction of the packer: receive, transmit or unknown. * @param comment
@@ -136,7 +134,7 @@ __rte_experimentalstruct rte_mbuf *rte_pcapng_copy (uint16_t port_id,uint32_t queue,const struct rte_mbuf *m,struct rte_mempool *mp, -uint32_t length,uint64_t timestamp, +uint32_t length,enum rte_pcapng_direction direction,const char *comment);
@@ -188,29 +186,22 @@ rte_pcapng_write_packets(rte_pcapng_t *self,* The handle to the packet capture file * @param port * The Ethernet port to report stats on. -* @param comment -* Optional comment to add to statistics. -* @param start_time -* The time when packet capture was started in nanoseconds. -* Optional: can be zero if not known. -* @param end_time -* The time when packet capture was stopped in nanoseconds. -* Optional: can be zero if not finished; * @param ifrecv * The number of packets received by capture. * Optional: use UINT64_MAX if not known. * @param ifdrop * The number of packets missed by the capture process. * Optional: use UINT64_MAX if not known. +* @param comment +* Optional comment to add to statistics. * @return * number of bytes written to file, -1 on failure to write file */ __rte_experimental ssize_trte_pcapng_write_stats (rte_pcapng_t *self,uint16_t port, -const char *comment, -uint64_t start_time,uint64_t end_time, -uint64_t ifrecv,uint64_t ifdrop); +uint64_t ifrecv,uint64_t ifdrop, +const char *comment); #ifdef __cplusplus }
lib/pdump/rte_pdump.c +1 -3
@@ -90,7 +90,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,int ring_enq;uint16_t d_pkts =0 ;struct rte_mbuf *dup_bufs[nb_pkts]; -uint64_t ts;struct rte_ring *ring;struct rte_mempool *mp;struct rte_mbuf *p;
@@ -99,7 +98,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->filter)rte_bpf_exec_burst (cbs->filter, (void **)pkts, rcs, nb_pkts); - ts =rte_get_tsc_cycles (); ring = cbs->ring; mp = cbs->mp;for (i =0 ; i < nb_pkts; i++) {
@@ -122,7 +120,7 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->ver ==V2 ) p =rte_pcapng_copy (port_id, queue, pkts[i], mp, cbs->snaplen, - ts, direction,NULL ); + direction,NULL );else p =rte_pktmbuf_copy (pkts[i], mp,0 , cbs->snaplen);
Signature · 1 line
2.39.2
RE: [PATCH v3 3/5] pcapng: modify timestamp calculation
Quoted 2 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 8 November 2023 19.36
] pcapng: modify timestamp calculation
Quoted 6 lines
The computation of timestamp is best done in the part of pcapng library that is in secondary process. The secondary process is already doing a bunch of system calls which makes it not performance sensitive.
Quoted 7 lines
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
Fixes c882eb544842 ("pcapng: fix timestamp wrapping in output files")Signed-off-by Stephen Hemminger <[email protected]>---
This changes the rte_pcapng lib API, but it is marked experimental, so should be allowed.
PATCH v3 4/5 pcapng: avoid using alloca()
In reply to Stephen HemmingerThe function alloca() like VLA's has problems if the caller passes a large value. Instead use a fixed size buffer (4K) which will be more than sufficient for the info related blocks in the file. Add bounds checks as well.
--- lib/pcapng/rte_pcapng.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-)
lib/pcapng/rte_pcapng.c +13 -21
@@ -140,9 +140,8 @@ pcapng_section_block(rte_pcapng_t *self, {struct pcapng_section_header *hdr;struct pcapng_option *opt; -void *buf; +uint8_t buf[BUFSIZ ];uint32_t len; -ssize_t cc; len =sizeof (*hdr);if (hw)
@@ -158,8 +157,7 @@ pcapng_section_block(rte_pcapng_t *self, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =calloc (1 , len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_section_header *)buf;
@@ -193,10 +191,7 @@ pcapng_section_block(rte_pcapng_t *self,/* clone block_length after option */ memcpy (opt, &hdr->block_length,sizeof (uint32_t )); - cc =write (self->outfd, buf, len); -free (buf); - -return cc; +return write (self->outfd, buf, len); }/* Write an interface block for a DPDK port */
@@ -213,7 +208,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port,struct pcapng_option *opt;const uint8_t tsresol =9 ;/* nanosecond resolution */ uint32_t len; -void *buf; +uint8_t buf[BUFSIZ ];char ifname_buf[IF_NAMESIZE ];char ifhw[256 ];uint64_t speed =0 ;
@@ -267,8 +262,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =alloca (len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_interface_block *)buf;
@@ -296,17 +290,16 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, opt =pcapng_add_option (opt,PCAPNG_IFB_HARDWARE , ifhw,strlen (ifhw));if (filter) { -/* Encoding is that the first octet indicates string vs BPF */ size_t len; -char *buf; len =strlen (filter) +1 ; - buf =alloca (len); - *buf ='\0' ; -memcpy (buf +1 , filter, len); + opt->code =PCAPNG_IFB_FILTER ; + opt->length = len; +/* Encoding is that the first octet indicates string vs BPF */ + opt->data[0 ] =0 ; +memcpy (opt->data +1 , filter,strlen (filter)); - opt =pcapng_add_option (opt,PCAPNG_IFB_FILTER , - buf, len); + opt = (struct pcapng_option *)((uint8_t *)opt +pcapng_optlen (len)); } opt =pcapng_add_option (opt,PCAPNG_OPT_END ,NULL ,0 );
@@ -333,7 +326,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,uint64_t start_time = self->offset_ns;uint64_t sample_time;uint32_t optlen, len; -uint8_t *buf; +uint8_t buf[BUFSIZ ];RTE_ETH_VALID_PORTID_OR_ERR_RET (port_id, -EINVAL );
@@ -353,8 +346,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, optlen +=pcapng_optlen (0 ); len =sizeof (*hdr) + optlen +sizeof (uint32_t ); - buf =alloca (len); -if (buf ==NULL ) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_statistics *)buf;
Signature · 1 line
2.39.2
RE: [PATCH v3 4/5] pcapng: avoid using alloca()
Quoted 10 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 8 November 2023 19.36The function
alloca()like VLA's has problems if the caller passes a large value. Instead use a fixed size buffer (4K) which will be more than sufficient for the info related blocks in the file. Add bounds checks as well.Signed-off-by Stephen Hemminger <[email protected]>---
I can't find the definition of BUFSIZ. Please make sure to add a comment to the definition of BUFSIZ mentioning - like in your patch description - that it will be more than sufficient for the info related blocks in the file.
More comments inline below, regarding existing bugs found while reviewing.
Assuming BUFSIZ has a comment describing the reason for its value,
Quoted 19 lines
lib/pcapng/rte_pcapng.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-)lib/pcapng/rte_pcapng.c +1
-2@@ -140,9 +140,8 @@ pcapng_section_block(rte_pcapng_t *self, {struct pcapng_section_header *hdr;struct pcapng_option *opt; -void *buf; +uint8_t buf[BUFSIZ ];uint32_t len; -ssize_t cc; len =sizeof (*hdr);if (hw)
lib/pcapng/rte_pcapng.c pcapng_section_block(rte_pcapng_t *self, · line 161
Quoted 7 lines
lib/pcapng/rte_pcapng.c +1
-2@@ -158,8 +157,7 @@ pcapng_section_block(rte_pcapng_t *self, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =calloc (1 , len); -if (!buf) +if (len >sizeof (buf))return -1 ;
Existing bug: rte_errno must be set before returning -1. This bug occurs multiple times in rte_pcapng.c, probably also in code you're not updating in this patch.
lib/pcapng/rte_pcapng.c pcapng_section_block(rte_pcapng_t *self, · line 194
Quoted 11 lines
lib/pcapng/rte_pcapng.c +1
-4hdr = (struct pcapng_section_header *)buf;@@ -193,10 +191,7 @@ pcapng_section_block(rte_pcapng_t *self,/* clone block_length after option */ memcpy (opt, &hdr->block_length,sizeof (uint32_t )); - cc =write (self->outfd, buf, len); -free (buf); - -return cc; +return write (self->outfd, buf, len);
Existing bug: if write() returns -1, errno must be stored in rte_errno before returning -1. This bug might also occur multiple times in rte_pcapng.c.
Re: [PATCH v3 4/5] pcapng: avoid using alloca()
Quoted 8 lines · On Thu, 9 Nov 2023 09:21:22 +0100 Morten Brørup <[email protected]>…
I can't find the definition of BUFSIZ. Please make sure to add a comment to the definition of BUFSIZ mentioning - like in your patch description - that it will be more than sufficient for the info related blocks in the file.
More comments inline below, regarding existing bugs found while reviewing.
Assuming BUFSIZ has a comment describing the reason for its value,
Acked-by Morten Brørup <[email protected]>
The constant BUFSIZ comes from stdio.h and used lots of places in libraries. It is 8192 in current glibc and unlikely to be a problem.
Chose it because this a on stack buffer used before writing to a file which is similar to what stdio does.
The library does not use stdio because most of the I/O is writing packets which needs to be fast and overhead of extra stdio buffer is harmful. Looking into using io_uring in a future version.
RE: [PATCH v3 4/5] pcapng: avoid using alloca()
Quoted 15 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 9 November 2023 16.45Quoted 1 line · On Thu, 9 Nov 2023 09:21:22 +0100 Morten Brørup <[email protected]>…
I can't find the definition of BUFSIZ. Please make sure to add a
comment to the definition of BUFSIZ mentioning - like in your patch description - that it will be more than sufficient for the info related blocks in the file.
Quoted 2 lines
More comments inline below, regarding existing bugs found while
reviewing.
Quoted 2 lines
Quoted 7 lines
Quoted 3 lines
Assuming BUFSIZ has a comment describing the reason for its value,
Acked-by Morten Brørup <[email protected]>The constant BUFSIZ comes from
stdio.hand used lots of places in libraries.
It is 8192 in current glibc and unlikely to be a problem.
OK, didn't know that. So I looked it up, trying to learn more about it.
I found two sources [1], [2] mentioning that BUFSIZ is guaranteed to be at least 256.
[1]: https://www.gnu.org/software/libc/manual/html_node/Controlling-Buffering.html#BUFSIZ
[2]: Page 234 in "The C Standard Library" by P.J. Plauger, ISBN: 0-13-131509-9, from 1992
If 256 suffices, then I am OK with using BUFSIZ.
I hope the authors of the other libraries using BUFSIZ don't assume more than the C standard promises about it.
Quoted 8 lines
Chose it because this a on stack buffer used before writing to a file which
is similar to what stdio does.The library does not use stdio because most of the I/O is writing packets
which needs to be fast and overhead of extra stdio buffer is harmful. Looking into usingio_uringin a future version.
PATCH v3 5/5 test: cleanups to pcapng test
In reply to Stephen HemmingerOverhaul of the pcapng test:
- promote it to be a fast test so it gets regularly run.
- create null device and use i.
- use UDP discard packets that are valid so that for debugging the resulting pcapng file can be looked at with wireshark.
- do basic checks on resulting pcap file that lengths and timestamps are in range.
- add test for interface options
--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 418 +++++++++++++++++++++++++++-------------- 2 files changed, 282 insertions(+), 138 deletions(-)
app/test/meson.build +1 -1
@@ -128,7 +128,7 @@ source_file_deps = { 'test_metrics.c': ['metrics'], 'test_mp_secondary.c': ['hash', 'lpm'], 'test_net_ether.c': ['net'], - 'test_pcapng.c': ['ethdev', 'net', 'pcapng'], + 'test_pcapng.c': ['ethdev', 'net', 'pcapng', 'bus_vdev'], 'test_pdcp.c': ['eventdev', 'pdcp', 'net', 'timer', 'security'], 'test_pdump.c': ['pdump'] + sample_packet_forward_deps, 'test_per_lcore.c': [],
app/test/test_pcapng.c +281 -137
553 lines not on this page · raw message
Signature · 1 line
2.39.2
v4 cover dumpcap and pcapng fixes
In reply to Stephen HemmingerThis series has bugfixes and tests for dumpcap and pcapng. It should be in 23.11!
It fixes issues related to timestamping. The design choices are to maximize performance in the primary process; and do all the time adjustment in the secondary (dumpcap) since the dumpcap needs to system calls anyway to write the result.
This patches set changes where the adjustment is calculated into the pcapng portion that opens the output file. All details of the format of timestamp are contained inside pcapng (data hiding).
v4 - incorporate review feedback
v3 - don't use alloca() since can have VLA type issues
Stephen Hemminger (5):
pdump: fix setting rte_errno on mp error
dumpcap: allow multiple invocations
pcapng: modify timestamp calculation
pcapng: avoid using alloca()
test: cleanups to pcapng test
app/dumpcap/main.c | 53 ++--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 418 +++++++++++++++++++++++++++------------- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 156 ++++++--------- lib/pcapng/rte_pcapng.h | 19 +- lib/pdump/rte_pdump.c | 9 +- 7 files changed, 374 insertions(+), 285 deletions(-)
Signature · 1 line
2.39.2
PATCH v4 1/5 pdump: fix setting rte_errno on mp error
The response from MP server sets err_value to negative on error. The convention for rte_errno is to use a positive value on error. This makes errors like duplicate registration show up with the correct error value.
--- lib/pdump/rte_pdump.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/pdump/rte_pdump.c +3 -2
@@ -564,9 +564,10 @@ pdump_prepare_client_request(const char *device, uint16_t queue,if (rte_mp_request_sync (&mp_req, &mp_reply, &ts) ==0 ) { mp_rep = &mp_reply.msgs[0 ]; resp = (struct pdump_response *)mp_rep->param; - rte_errno = resp->err_value; -if (!resp->err_value) +if (resp->err_value ==0 ) ret =0 ; +else + rte_errno = -resp->err_value;free (mp_reply.msgs); }
Signature · 1 line
2.39.2
PATCH v4 2/5 dumpcap: allow multiple invocations
In reply to Stephen HemmingerIf dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
It is still not allowed to do multiple invocations on the same interface because only one callback is allowed and only one copy of mbuf is done. Dumpcap will fail with error in this case:
pdump_prepare_client_request(): client request for pdump enable/disable failed
EAL: Error - exiting with code: 1
Cause: Packet dump enable on 0:net_null0 failed File exists
--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
app/dumpcap/main.c +14 -14
@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32
@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */
@@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; } - ring =rte_ring_lookup (RING_NAME ); -if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%d" ,getpid ()); + + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf; -static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%u" ,getpid ());/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {
@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno)); }if (intf->opts.promisc_mode) {
Signature · 1 line
2.39.2
RE: [PATCH v4 2/5] dumpcap: allow multiple invocations
Quoted 20 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 9 November 2023 18.34If dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
It is still not allowed to do multiple invocations on the same interface because only one callback is allowed and only one copy of mbuf is done. Dumpcap will fail with error in this case:
pdump_prepare_client_request(): client request for pdump
enable/disable failed
EAL: Error - exiting with code: 1
Cause: Packet dump enable on 0:net_null0 failed File existsFixes cbb44143be74 ("app/dumpcap: add new packet capture application")Reported-by Isaac Boukris <[email protected]>Signed-off-by Stephen Hemminger <[email protected]>---
[...]
app/dumpcap/main.c static struct rte_ring *create_ring(void) · line 665
Quoted 2 lines
app/dumpcap/main.c +2
-0+snprintf (ring_name,sizeof (ring_name), +"dumpcap-%d" ,getpid ());
Fixed - thank you.
[...]
app/dumpcap/main.c static struct rte_ring *create_ring(void) · line 684
Quoted 1 line
app/dumpcap/main.c +1
-0+snprintf (pool_name,sizeof (pool_name),"capture_%u" ,getpid ());
Should change from %u to %d here too. ;-)
Either way,
PATCH v4 3/5 pcapng: modify timestamp calculation
In reply to Stephen HemmingerThe computation of timestamp is best done in the part of pcapng library that is in secondary process. The secondary process is already doing a bunch of system calls which makes it not performance sensitive. This does change the rte_pcapng_copy()
and rte_pcapng_write_stats() experimental API's.
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
--- app/dumpcap/main.c | 25 +++------ app/test/test_pcapng.c | 4 +- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 119 +++++++++++++++------------------------- lib/pcapng/rte_pcapng.h | 19 ++----- lib/pdump/rte_pdump.c | 4 +- 6 files changed, 61 insertions(+), 112 deletions(-)
app/dumpcap/main.c +7 -18
@@ -66,13 +66,13 @@ static bool print_stats;/* capture limit options */ static struct { -uint64_t duration;/* nanoseconds */ +time_t duration;/* seconds */ unsigned long packets;/* number of packets in file */ size_t size;/* file size (bytes) */ } stop;/* Running state */ -static uint64_t start_time, end_time; +static time_t start_time;static uint64_t packets_received;static size_t file_size;
@@ -197,7 +197,7 @@ static void auto_stop(char *opt)if (*value =='\0' || *endp !='\0' || interval <=0 )rte_exit (EXIT_FAILURE ,"Invalid duration \"%s\"\n" , value); - stop.duration =NSEC_PER_SEC * interval; + stop.duration = interval; }else if (strcmp (opt,"filesize" ) ==0 ) { stop.size =get_uint (value,"filesize" ,0 ) *1024 ; }else if (strcmp (opt,"packets" ) ==0 ) {
@@ -511,15 +511,6 @@ static void statistics_loop(void) } } -/* Return the time since 1/1/1970 in nanoseconds */ -static uint64_t create_timestamp (void ) -{ -struct timespec now; - -clock_gettime (CLOCK_MONOTONIC , &now); -return rte_timespec_to_ns (&now); -} -static void cleanup_pdump_resources (void ) {
@@ -589,9 +580,8 @@ report_packet_stats(dumpcap_out_t out) ifdrop = pdump_stats.nombuf + pdump_stats.ringfull;if (use_pcapng) -rte_pcapng_write_stats (out.pcapng, intf->port,NULL , - start_time, end_time, - ifrecv, ifdrop); +rte_pcapng_write_stats (out.pcapng, intf->port, + ifrecv, ifdrop,NULL );if (ifrecv ==0 ) percent =0 ;
@@ -983,7 +973,7 @@ int main(int argc, char **argv) mp =create_mempool (); out =create_output (); - start_time =create_timestamp (); + start_time =time (NULL );enable_pdump (r, mp);if (!quiet) {
@@ -1005,11 +995,10 @@ int main(int argc, char **argv)break ;if (stop.duration !=0 && -create_timestamp () - start_time > stop.duration) +time (NULL ) - start_time > stop.duration)break ; } - end_time =create_timestamp ();disable_primary_monitor ();if (rte_eal_primary_proc_alive (NULL ))
app/test/test_pcapng.c +2 -2
@@ -173,8 +173,8 @@ test_write_stats(void)ssize_t len;/* write a statistics block */ - len =rte_pcapng_write_stats (pcapng, port_id, -NULL ,0 ,0 , + len =rte_pcapng_write_stats (pcapng, port_id,NULL , +0 ,0 ,0 ,NUM_PACKETS ,0 );if (len <=0 ) {fprintf (stderr,"Write of statistics failed\n" );
lib/graph/graph_pcap.c +1 -1
@@ -214,7 +214,7 @@ graph_pcap_dispatch(struct rte_graph *graph, mbuf = (struct rte_mbuf *)objs[i]; mc =rte_pcapng_copy (mbuf->port,0 , mbuf, pkt_mp, mbuf->pkt_len, -rte_get_tsc_cycles (),0 , buffer); +0 , buffer);if (mc ==NULL )break ;
lib/pcapng/rte_pcapng.c +45 -74
221 lines not on this page · raw message
lib/pcapng/rte_pcapng.h +5 -14
@@ -121,8 +121,6 @@ enum rte_pcapng_direction {* @param length * The upper limit on bytes to copy. Passing UINT32_MAX * means all data (after offset). -* @param timestamp -* The timestamp in TSC cycles. * @param direction * The direction of the packer: receive, transmit or unknown. * @param comment
@@ -136,7 +134,7 @@ __rte_experimentalstruct rte_mbuf *rte_pcapng_copy (uint16_t port_id,uint32_t queue,const struct rte_mbuf *m,struct rte_mempool *mp, -uint32_t length,uint64_t timestamp, +uint32_t length,enum rte_pcapng_direction direction,const char *comment);
@@ -188,29 +186,22 @@ rte_pcapng_write_packets(rte_pcapng_t *self,* The handle to the packet capture file * @param port * The Ethernet port to report stats on. -* @param comment -* Optional comment to add to statistics. -* @param start_time -* The time when packet capture was started in nanoseconds. -* Optional: can be zero if not known. -* @param end_time -* The time when packet capture was stopped in nanoseconds. -* Optional: can be zero if not finished; * @param ifrecv * The number of packets received by capture. * Optional: use UINT64_MAX if not known. * @param ifdrop * The number of packets missed by the capture process. * Optional: use UINT64_MAX if not known. +* @param comment +* Optional comment to add to statistics. * @return * number of bytes written to file, -1 on failure to write file */ __rte_experimental ssize_trte_pcapng_write_stats (rte_pcapng_t *self,uint16_t port, -const char *comment, -uint64_t start_time,uint64_t end_time, -uint64_t ifrecv,uint64_t ifdrop); +uint64_t ifrecv,uint64_t ifdrop, +const char *comment); #ifdef __cplusplus }
lib/pdump/rte_pdump.c +1 -3
@@ -90,7 +90,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,int ring_enq;uint16_t d_pkts =0 ;struct rte_mbuf *dup_bufs[nb_pkts]; -uint64_t ts;struct rte_ring *ring;struct rte_mempool *mp;struct rte_mbuf *p;
@@ -99,7 +98,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->filter)rte_bpf_exec_burst (cbs->filter, (void **)pkts, rcs, nb_pkts); - ts =rte_get_tsc_cycles (); ring = cbs->ring; mp = cbs->mp;for (i =0 ; i < nb_pkts; i++) {
@@ -122,7 +120,7 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->ver ==V2 ) p =rte_pcapng_copy (port_id, queue, pkts[i], mp, cbs->snaplen, - ts, direction,NULL ); + direction,NULL );else p =rte_pktmbuf_copy (pkts[i], mp,0 , cbs->snaplen);
Signature · 1 line
2.39.2
PATCH v4 4/5 pcapng: avoid using alloca()
In reply to Stephen HemmingerThe function alloca() like VLA's has problems if the caller passes a large value. Instead use a fixed size buffer (2K) which will be more than sufficient for the info related blocks in the file. Add bounds checks as well.
--- lib/pcapng/rte_pcapng.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-)
lib/pcapng/rte_pcapng.c +16 -21
@@ -33,6 +33,9 @@/* conversion from DPDK speed to PCAPNG */ #define PCAPNG_MBPS_SPEED 1000000ull +/* upper bound for section, stats and interface blocks */ +#define PCAPNG_BLKSIZ 2048 +/* Format of the capture file handle */ struct rte_pcapng {int outfd;/* output file */
@@ -140,9 +143,8 @@ pcapng_section_block(rte_pcapng_t *self, {struct pcapng_section_header *hdr;struct pcapng_option *opt; -void *buf; +uint8_t buf[PCAPNG_BLKSIZ ];uint32_t len; -ssize_t cc; len =sizeof (*hdr);if (hw)
@@ -158,8 +160,7 @@ pcapng_section_block(rte_pcapng_t *self, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =calloc (1 , len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_section_header *)buf;
@@ -193,10 +194,7 @@ pcapng_section_block(rte_pcapng_t *self,/* clone block_length after option */ memcpy (opt, &hdr->block_length,sizeof (uint32_t )); - cc =write (self->outfd, buf, len); -free (buf); - -return cc; +return write (self->outfd, buf, len); }/* Write an interface block for a DPDK port */
@@ -213,7 +211,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port,struct pcapng_option *opt;const uint8_t tsresol =9 ;/* nanosecond resolution */ uint32_t len; -void *buf; +uint8_t buf[PCAPNG_BLKSIZ ];char ifname_buf[IF_NAMESIZE ];char ifhw[256 ];uint64_t speed =0 ;
@@ -267,8 +265,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =alloca (len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_interface_block *)buf;
@@ -296,17 +293,16 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, opt =pcapng_add_option (opt,PCAPNG_IFB_HARDWARE , ifhw,strlen (ifhw));if (filter) { -/* Encoding is that the first octet indicates string vs BPF */ size_t len; -char *buf; len =strlen (filter) +1 ; - buf =alloca (len); - *buf ='\0' ; -memcpy (buf +1 , filter, len); + opt->code =PCAPNG_IFB_FILTER ; + opt->length = len; +/* Encoding is that the first octet indicates string vs BPF */ + opt->data[0 ] =0 ; +memcpy (opt->data +1 , filter,strlen (filter)); - opt =pcapng_add_option (opt,PCAPNG_IFB_FILTER , - buf, len); + opt = (struct pcapng_option *)((uint8_t *)opt +pcapng_optlen (len)); } opt =pcapng_add_option (opt,PCAPNG_OPT_END ,NULL ,0 );
@@ -333,7 +329,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,uint64_t start_time = self->offset_ns;uint64_t sample_time;uint32_t optlen, len; -uint8_t *buf; +uint8_t buf[PCAPNG_BLKSIZ ];RTE_ETH_VALID_PORTID_OR_ERR_RET (port_id, -EINVAL );
@@ -353,8 +349,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, optlen +=pcapng_optlen (0 ); len =sizeof (*hdr) + optlen +sizeof (uint32_t ); - buf =alloca (len); -if (buf ==NULL ) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_statistics *)buf;
Signature · 1 line
2.39.2
PATCH v4 5/5 test: cleanups to pcapng test
In reply to Stephen HemmingerOverhaul of the pcapng test:
- promote it to be a fast test so it gets regularly run.
- create null device and use i.
- use UDP discard packets that are valid so that for debugging the resulting pcapng file can be looked at with wireshark.
- do basic checks on resulting pcap file that lengths and timestamps are in range.
- add test for interface options
--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 418 +++++++++++++++++++++++++++-------------- 2 files changed, 282 insertions(+), 138 deletions(-)
app/test/meson.build +1 -1
@@ -128,7 +128,7 @@ source_file_deps = { 'test_metrics.c': ['metrics'], 'test_mp_secondary.c': ['hash', 'lpm'], 'test_net_ether.c': ['net'], - 'test_pcapng.c': ['ethdev', 'net', 'pcapng'], + 'test_pcapng.c': ['ethdev', 'net', 'pcapng', 'bus_vdev'], 'test_pdcp.c': ['eventdev', 'pdcp', 'net', 'timer', 'security'], 'test_pdump.c': ['pdump'] + sample_packet_forward_deps, 'test_per_lcore.c': [],
app/test/test_pcapng.c +281 -137
553 lines not on this page · raw message
Signature · 1 line
2.39.2
v5 cover dumpcap and pcapng fixes
In reply to Stephen HemmingerThis series has bugfixes and tests for dumpcap and pcapng. It should be in 23.11!
It fixes issues related to timestamping. The design choices are to maximize performance in the primary process; and do all the time adjustment in the secondary (dumpcap) since the dumpcap needs to system calls anyway to write the result.
This patches set changes where the adjustment is calculated into the pcapng portion that opens the output file. All details of the format of timestamp are contained inside pcapng (data hiding).
v5 - fix format of getpid in capture name
v4 - incorporate review feedback
v3 - don't use alloca() since can have VLA type issues
Stephen Hemminger (5):
pdump: fix setting rte_errno on mp error
dumpcap: allow multiple invocations
pcapng: modify timestamp calculation
pcapng: avoid using alloca()
test: cleanups to pcapng test
app/dumpcap/main.c | 53 ++--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 418 +++++++++++++++++++++++++++------------- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 156 ++++++--------- lib/pcapng/rte_pcapng.h | 19 +- lib/pdump/rte_pdump.c | 9 +- 7 files changed, 374 insertions(+), 285 deletions(-)
Signature · 1 line
2.39.2
PATCH v5 1/5 pdump: fix setting rte_errno on mp error
The response from MP server sets err_value to negative on error. The convention for rte_errno is to use a positive value on error. This makes errors like duplicate registration show up with the correct error value.
--- lib/pdump/rte_pdump.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/pdump/rte_pdump.c +3 -2
@@ -564,9 +564,10 @@ pdump_prepare_client_request(const char *device, uint16_t queue,if (rte_mp_request_sync (&mp_req, &mp_reply, &ts) ==0 ) { mp_rep = &mp_reply.msgs[0 ]; resp = (struct pdump_response *)mp_rep->param; - rte_errno = resp->err_value; -if (!resp->err_value) +if (resp->err_value ==0 ) ret =0 ; +else + rte_errno = -resp->err_value;free (mp_reply.msgs); }
Signature · 1 line
2.39.2
PATCH v5 2/5 dumpcap: allow multiple invocations
In reply to Stephen HemmingerIf dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
It is still not allowed to do multiple invocations on the same interface because only one callback is allowed and only one copy of mbuf is done. Dumpcap will fail with error in this case:
pdump_prepare_client_request(): client request for pdump enable/disable failed
EAL: Error - exiting with code: 1
Cause: Packet dump enable on 0:net_null0 failed File exists
--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
app/dumpcap/main.c +14 -14
@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32
@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */
@@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; } - ring =rte_ring_lookup (RING_NAME ); -if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%d" ,getpid ()); + + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf; -static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%d" ,getpid ());/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {
@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno)); }if (intf->opts.promisc_mode) {
Signature · 1 line
2.39.2
RE: [PATCH v5 2/5] dumpcap: allow multiple invocations
Quoted 16 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 9 November 2023 20.46If dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
It is still not allowed to do multiple invocations on the same interface because only one callback is allowed and only one copy of mbuf is done. Dumpcap will fail with error in this case:
pdump_prepare_client_request(): client request for pdump
enable/disable failed
EAL: Error - exiting with code: 1
Cause: Packet dump enable on 0:net_null0 failed File exists
Quoted 4 lines
Fixes cbb44143be74 ("app/dumpcap: add new packet capture application")Reported-by Isaac Boukris <[email protected]>Signed-off-by Stephen Hemminger <[email protected]>---
PATCH v5 3/5 pcapng: modify timestamp calculation
In reply to Stephen HemmingerThe computation of timestamp is best done in the part of pcapng library that is in secondary process. The secondary process is already doing a bunch of system calls which makes it not performance sensitive. This does change the rte_pcapng_copy()
and rte_pcapng_write_stats() experimental API's.
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
--- app/dumpcap/main.c | 25 +++------ app/test/test_pcapng.c | 4 +- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 119 +++++++++++++++------------------------- lib/pcapng/rte_pcapng.h | 19 ++----- lib/pdump/rte_pdump.c | 4 +- 6 files changed, 61 insertions(+), 112 deletions(-)
app/dumpcap/main.c +7 -18
@@ -66,13 +66,13 @@ static bool print_stats;/* capture limit options */ static struct { -uint64_t duration;/* nanoseconds */ +time_t duration;/* seconds */ unsigned long packets;/* number of packets in file */ size_t size;/* file size (bytes) */ } stop;/* Running state */ -static uint64_t start_time, end_time; +static time_t start_time;static uint64_t packets_received;static size_t file_size;
@@ -197,7 +197,7 @@ static void auto_stop(char *opt)if (*value =='\0' || *endp !='\0' || interval <=0 )rte_exit (EXIT_FAILURE ,"Invalid duration \"%s\"\n" , value); - stop.duration =NSEC_PER_SEC * interval; + stop.duration = interval; }else if (strcmp (opt,"filesize" ) ==0 ) { stop.size =get_uint (value,"filesize" ,0 ) *1024 ; }else if (strcmp (opt,"packets" ) ==0 ) {
@@ -511,15 +511,6 @@ static void statistics_loop(void) } } -/* Return the time since 1/1/1970 in nanoseconds */ -static uint64_t create_timestamp (void ) -{ -struct timespec now; - -clock_gettime (CLOCK_MONOTONIC , &now); -return rte_timespec_to_ns (&now); -} -static void cleanup_pdump_resources (void ) {
@@ -589,9 +580,8 @@ report_packet_stats(dumpcap_out_t out) ifdrop = pdump_stats.nombuf + pdump_stats.ringfull;if (use_pcapng) -rte_pcapng_write_stats (out.pcapng, intf->port,NULL , - start_time, end_time, - ifrecv, ifdrop); +rte_pcapng_write_stats (out.pcapng, intf->port, + ifrecv, ifdrop,NULL );if (ifrecv ==0 ) percent =0 ;
@@ -983,7 +973,7 @@ int main(int argc, char **argv) mp =create_mempool (); out =create_output (); - start_time =create_timestamp (); + start_time =time (NULL );enable_pdump (r, mp);if (!quiet) {
@@ -1005,11 +995,10 @@ int main(int argc, char **argv)break ;if (stop.duration !=0 && -create_timestamp () - start_time > stop.duration) +time (NULL ) - start_time > stop.duration)break ; } - end_time =create_timestamp ();disable_primary_monitor ();if (rte_eal_primary_proc_alive (NULL ))
app/test/test_pcapng.c +2 -2
@@ -173,8 +173,8 @@ test_write_stats(void)ssize_t len;/* write a statistics block */ - len =rte_pcapng_write_stats (pcapng, port_id, -NULL ,0 ,0 , + len =rte_pcapng_write_stats (pcapng, port_id,NULL , +0 ,0 ,0 ,NUM_PACKETS ,0 );if (len <=0 ) {fprintf (stderr,"Write of statistics failed\n" );
lib/graph/graph_pcap.c +1 -1
@@ -214,7 +214,7 @@ graph_pcap_dispatch(struct rte_graph *graph, mbuf = (struct rte_mbuf *)objs[i]; mc =rte_pcapng_copy (mbuf->port,0 , mbuf, pkt_mp, mbuf->pkt_len, -rte_get_tsc_cycles (),0 , buffer); +0 , buffer);if (mc ==NULL )break ;
lib/pcapng/rte_pcapng.c +45 -74
221 lines not on this page · raw message
lib/pcapng/rte_pcapng.h +5 -14
@@ -121,8 +121,6 @@ enum rte_pcapng_direction {* @param length * The upper limit on bytes to copy. Passing UINT32_MAX * means all data (after offset). -* @param timestamp -* The timestamp in TSC cycles. * @param direction * The direction of the packer: receive, transmit or unknown. * @param comment
@@ -136,7 +134,7 @@ __rte_experimentalstruct rte_mbuf *rte_pcapng_copy (uint16_t port_id,uint32_t queue,const struct rte_mbuf *m,struct rte_mempool *mp, -uint32_t length,uint64_t timestamp, +uint32_t length,enum rte_pcapng_direction direction,const char *comment);
@@ -188,29 +186,22 @@ rte_pcapng_write_packets(rte_pcapng_t *self,* The handle to the packet capture file * @param port * The Ethernet port to report stats on. -* @param comment -* Optional comment to add to statistics. -* @param start_time -* The time when packet capture was started in nanoseconds. -* Optional: can be zero if not known. -* @param end_time -* The time when packet capture was stopped in nanoseconds. -* Optional: can be zero if not finished; * @param ifrecv * The number of packets received by capture. * Optional: use UINT64_MAX if not known. * @param ifdrop * The number of packets missed by the capture process. * Optional: use UINT64_MAX if not known. +* @param comment +* Optional comment to add to statistics. * @return * number of bytes written to file, -1 on failure to write file */ __rte_experimental ssize_trte_pcapng_write_stats (rte_pcapng_t *self,uint16_t port, -const char *comment, -uint64_t start_time,uint64_t end_time, -uint64_t ifrecv,uint64_t ifdrop); +uint64_t ifrecv,uint64_t ifdrop, +const char *comment); #ifdef __cplusplus }
lib/pdump/rte_pdump.c +1 -3
@@ -90,7 +90,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,int ring_enq;uint16_t d_pkts =0 ;struct rte_mbuf *dup_bufs[nb_pkts]; -uint64_t ts;struct rte_ring *ring;struct rte_mempool *mp;struct rte_mbuf *p;
@@ -99,7 +98,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->filter)rte_bpf_exec_burst (cbs->filter, (void **)pkts, rcs, nb_pkts); - ts =rte_get_tsc_cycles (); ring = cbs->ring; mp = cbs->mp;for (i =0 ; i < nb_pkts; i++) {
@@ -122,7 +120,7 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->ver ==V2 ) p =rte_pcapng_copy (port_id, queue, pkts[i], mp, cbs->snaplen, - ts, direction,NULL ); + direction,NULL );else p =rte_pktmbuf_copy (pkts[i], mp,0 , cbs->snaplen);
Signature · 1 line
2.39.2
Re: [PATCH v5 3/5] pcapng: modify timestamp calculation
09/11/2023 20:45, Stephen Hemminger:
Quoted 7 lines
The computation of timestamp is best done in the part of pcapng library that is in secondary process. The secondary process is already doing a bunch of system calls which makes it not performance sensitive. This does change the
rte_pcapng_copy()
andrte_pcapng_write_stats()experimental API's.
Quoted 7 lines
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
Fixes c882eb544842 ("pcapng: fix timestamp wrapping in output files")Signed-off-by Stephen Hemminger <[email protected]>Acked-by Morten Brørup <[email protected]>
It does not compile:
app/test/test_pcapng.c:148:22: error:
too many arguments to function 'rte_pcapng_copy'
Please make sure it compiles after each patch.
Thank you
PATCH v5 4/5 pcapng: avoid using alloca()
In reply to Stephen HemmingerThe function alloca() like VLA's has problems if the caller passes a large value. Instead use a fixed size buffer (2K) which will be more than sufficient for the info related blocks in the file. Add bounds checks as well.
--- lib/pcapng/rte_pcapng.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-)
lib/pcapng/rte_pcapng.c +16 -21
@@ -33,6 +33,9 @@/* conversion from DPDK speed to PCAPNG */ #define PCAPNG_MBPS_SPEED 1000000ull +/* upper bound for section, stats and interface blocks */ +#define PCAPNG_BLKSIZ 2048 +/* Format of the capture file handle */ struct rte_pcapng {int outfd;/* output file */
@@ -140,9 +143,8 @@ pcapng_section_block(rte_pcapng_t *self, {struct pcapng_section_header *hdr;struct pcapng_option *opt; -void *buf; +uint8_t buf[PCAPNG_BLKSIZ ];uint32_t len; -ssize_t cc; len =sizeof (*hdr);if (hw)
@@ -158,8 +160,7 @@ pcapng_section_block(rte_pcapng_t *self, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =calloc (1 , len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_section_header *)buf;
@@ -193,10 +194,7 @@ pcapng_section_block(rte_pcapng_t *self,/* clone block_length after option */ memcpy (opt, &hdr->block_length,sizeof (uint32_t )); - cc =write (self->outfd, buf, len); -free (buf); - -return cc; +return write (self->outfd, buf, len); }/* Write an interface block for a DPDK port */
@@ -213,7 +211,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port,struct pcapng_option *opt;const uint8_t tsresol =9 ;/* nanosecond resolution */ uint32_t len; -void *buf; +uint8_t buf[PCAPNG_BLKSIZ ];char ifname_buf[IF_NAMESIZE ];char ifhw[256 ];uint64_t speed =0 ;
@@ -267,8 +265,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =alloca (len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_interface_block *)buf;
@@ -296,17 +293,16 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, opt =pcapng_add_option (opt,PCAPNG_IFB_HARDWARE , ifhw,strlen (ifhw));if (filter) { -/* Encoding is that the first octet indicates string vs BPF */ size_t len; -char *buf; len =strlen (filter) +1 ; - buf =alloca (len); - *buf ='\0' ; -memcpy (buf +1 , filter, len); + opt->code =PCAPNG_IFB_FILTER ; + opt->length = len; +/* Encoding is that the first octet indicates string vs BPF */ + opt->data[0 ] =0 ; +memcpy (opt->data +1 , filter,strlen (filter)); - opt =pcapng_add_option (opt,PCAPNG_IFB_FILTER , - buf, len); + opt = (struct pcapng_option *)((uint8_t *)opt +pcapng_optlen (len)); } opt =pcapng_add_option (opt,PCAPNG_OPT_END ,NULL ,0 );
@@ -333,7 +329,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,uint64_t start_time = self->offset_ns;uint64_t sample_time;uint32_t optlen, len; -uint8_t *buf; +uint8_t buf[PCAPNG_BLKSIZ ];RTE_ETH_VALID_PORTID_OR_ERR_RET (port_id, -EINVAL );
@@ -353,8 +349,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, optlen +=pcapng_optlen (0 ); len =sizeof (*hdr) + optlen +sizeof (uint32_t ); - buf =alloca (len); -if (buf ==NULL ) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_statistics *)buf;
Signature · 1 line
2.39.2
PATCH v5 5/5 test: cleanups to pcapng test
In reply to Stephen HemmingerOverhaul of the pcapng test:
- promote it to be a fast test so it gets regularly run.
- create null device and use i.
- use UDP discard packets that are valid so that for debugging the resulting pcapng file can be looked at with wireshark.
- do basic checks on resulting pcap file that lengths and timestamps are in range.
- add test for interface options
--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 418 +++++++++++++++++++++++++++-------------- 2 files changed, 282 insertions(+), 138 deletions(-)
app/test/meson.build +1 -1
@@ -128,7 +128,7 @@ source_file_deps = { 'test_metrics.c': ['metrics'], 'test_mp_secondary.c': ['hash', 'lpm'], 'test_net_ether.c': ['net'], - 'test_pcapng.c': ['ethdev', 'net', 'pcapng'], + 'test_pcapng.c': ['ethdev', 'net', 'pcapng', 'bus_vdev'], 'test_pdcp.c': ['eventdev', 'pdcp', 'net', 'timer', 'security'], 'test_pdump.c': ['pdump'] + sample_packet_forward_deps, 'test_per_lcore.c': [],
app/test/test_pcapng.c +281 -137
553 lines not on this page · raw message
Signature · 1 line
2.39.2
v6 cover dumpcap and pcapng fixes
In reply to Stephen HemmingerIt fixes issues related to timestamping. The design choices are to maximize performance in the primary process; and do all the time adjustment in the secondary (dumpcap) since the dumpcap needs to system calls anyway to write the result.
This patches set changes where the adjustment is calculated into the pcapng portion that opens the output file. All details of the format of timestamp are contained inside pcapng (data hiding).
v6 - make sure all steps compile
v5 - fix format of getpid in capture name
v4 - incorporate review feedback
v3 - don't use alloca() since can have VLA type issues
Stephen Hemminger (5):
pdump: fix setting rte_errno on mp error
dumpcap: allow multiple invocations
pcapng: modify timestamp calculation
pcapng: avoid using alloca()
test: cleanups to pcapng test
app/dumpcap/main.c | 53 ++--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 417 +++++++++++++++++++++++++++------------- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 156 ++++++--------- lib/pcapng/rte_pcapng.h | 19 +- lib/pdump/rte_pdump.c | 9 +- 7 files changed, 373 insertions(+), 285 deletions(-)
Signature · 1 line
2.39.2
PATCH v6 1/5 pdump: fix setting rte_errno on mp error
The response from MP server sets err_value to negative on error. The convention for rte_errno is to use a positive value on error. This makes errors like duplicate registration show up with the correct error value.
--- lib/pdump/rte_pdump.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/pdump/rte_pdump.c +3 -2
@@ -564,9 +564,10 @@ pdump_prepare_client_request(const char *device, uint16_t queue,if (rte_mp_request_sync (&mp_req, &mp_reply, &ts) ==0 ) { mp_rep = &mp_reply.msgs[0 ]; resp = (struct pdump_response *)mp_rep->param; - rte_errno = resp->err_value; -if (!resp->err_value) +if (resp->err_value ==0 ) ret =0 ; +else + rte_errno = -resp->err_value;free (mp_reply.msgs); }
Signature · 1 line
2.39.2
PATCH v6 2/5 dumpcap: allow multiple invocations
In reply to Stephen HemmingerIf dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
It is still not allowed to do multiple invocations on the same interface because only one callback is allowed and only one copy of mbuf is done. Dumpcap will fail with error in this case:
pdump_prepare_client_request(): client request for pdump enable/disable failed
EAL: Error - exiting with code: 1
Cause: Packet dump enable on 0:net_null0 failed File exists
--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
app/dumpcap/main.c +14 -14
@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32
@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */
@@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; } - ring =rte_ring_lookup (RING_NAME ); -if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%d" ,getpid ()); + + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf; -static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%d" ,getpid ());/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {
@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno)); }if (intf->opts.promisc_mode) {
Signature · 1 line
2.39.2
PATCH v6 3/5 pcapng: modify timestamp calculation
In reply to Stephen HemmingerThe computation of timestamp is best done in the part of pcapng library that is in secondary process. The secondary process is already doing a bunch of system calls which makes it not performance sensitive. This does change the rte_pcapng_copy()
and rte_pcapng_write_stats() experimental API's.
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
--- app/dumpcap/main.c | 25 +++------ app/test/test_pcapng.c | 7 +-- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 119 +++++++++++++++------------------------- lib/pcapng/rte_pcapng.h | 19 ++----- lib/pdump/rte_pdump.c | 4 +- 6 files changed, 62 insertions(+), 114 deletions(-)
app/dumpcap/main.c +7 -18
@@ -66,13 +66,13 @@ static bool print_stats;/* capture limit options */ static struct { -uint64_t duration;/* nanoseconds */ +time_t duration;/* seconds */ unsigned long packets;/* number of packets in file */ size_t size;/* file size (bytes) */ } stop;/* Running state */ -static uint64_t start_time, end_time; +static time_t start_time;static uint64_t packets_received;static size_t file_size;
@@ -197,7 +197,7 @@ static void auto_stop(char *opt)if (*value =='\0' || *endp !='\0' || interval <=0 )rte_exit (EXIT_FAILURE ,"Invalid duration \"%s\"\n" , value); - stop.duration =NSEC_PER_SEC * interval; + stop.duration = interval; }else if (strcmp (opt,"filesize" ) ==0 ) { stop.size =get_uint (value,"filesize" ,0 ) *1024 ; }else if (strcmp (opt,"packets" ) ==0 ) {
@@ -511,15 +511,6 @@ static void statistics_loop(void) } } -/* Return the time since 1/1/1970 in nanoseconds */ -static uint64_t create_timestamp (void ) -{ -struct timespec now; - -clock_gettime (CLOCK_MONOTONIC , &now); -return rte_timespec_to_ns (&now); -} -static void cleanup_pdump_resources (void ) {
@@ -589,9 +580,8 @@ report_packet_stats(dumpcap_out_t out) ifdrop = pdump_stats.nombuf + pdump_stats.ringfull;if (use_pcapng) -rte_pcapng_write_stats (out.pcapng, intf->port,NULL , - start_time, end_time, - ifrecv, ifdrop); +rte_pcapng_write_stats (out.pcapng, intf->port, + ifrecv, ifdrop,NULL );if (ifrecv ==0 ) percent =0 ;
@@ -983,7 +973,7 @@ int main(int argc, char **argv) mp =create_mempool (); out =create_output (); - start_time =create_timestamp (); + start_time =time (NULL );enable_pdump (r, mp);if (!quiet) {
@@ -1005,11 +995,10 @@ int main(int argc, char **argv)break ;if (stop.duration !=0 && -create_timestamp () - start_time > stop.duration) +time (NULL ) - start_time > stop.duration)break ; } - end_time =create_timestamp ();disable_primary_monitor ();if (rte_eal_primary_proc_alive (NULL ))
app/test/test_pcapng.c +3 -4
@@ -146,7 +146,7 @@ test_write_packets(void)struct rte_mbuf *mc; mc =rte_pcapng_copy (port_id,0 , orig, mp, pkt_len, -rte_get_tsc_cycles (),0 ,NULL ); +RTE_PCAPNG_DIRECTION_UNKNOWN ,NULL );if (mc ==NULL ) {fprintf (stderr,"Cannot copy packet\n" );return -1 ;
@@ -174,8 +174,7 @@ test_write_stats(void)/* write a statistics block */ len =rte_pcapng_write_stats (pcapng, port_id, -NULL ,0 ,0 , -NUM_PACKETS ,0 ); +UINT64_MAX ,UINT64_MAX ,NULL );if (len <=0 ) {fprintf (stderr,"Write of statistics failed\n" );return -1 ;
@@ -262,7 +261,7 @@ test_write_over_limit_iov_max(void)struct rte_mbuf *mc; mc =rte_pcapng_copy (port_id,0 , orig, mp, pkt_len, -rte_get_tsc_cycles (),0 ,NULL ); +RTE_PCAPNG_DIRECTION_UNKNOWN ,NULL );if (mc ==NULL ) {fprintf (stderr,"Cannot copy packet\n" );return -1 ;
lib/graph/graph_pcap.c +1 -1
@@ -214,7 +214,7 @@ graph_pcap_dispatch(struct rte_graph *graph, mbuf = (struct rte_mbuf *)objs[i]; mc =rte_pcapng_copy (mbuf->port,0 , mbuf, pkt_mp, mbuf->pkt_len, -rte_get_tsc_cycles (),0 , buffer); +0 , buffer);if (mc ==NULL )break ;
lib/pcapng/rte_pcapng.c +45 -74
221 lines not on this page · raw message
lib/pcapng/rte_pcapng.h +5 -14
@@ -121,8 +121,6 @@ enum rte_pcapng_direction {* @param length * The upper limit on bytes to copy. Passing UINT32_MAX * means all data (after offset). -* @param timestamp -* The timestamp in TSC cycles. * @param direction * The direction of the packer: receive, transmit or unknown. * @param comment
@@ -136,7 +134,7 @@ __rte_experimentalstruct rte_mbuf *rte_pcapng_copy (uint16_t port_id,uint32_t queue,const struct rte_mbuf *m,struct rte_mempool *mp, -uint32_t length,uint64_t timestamp, +uint32_t length,enum rte_pcapng_direction direction,const char *comment);
@@ -188,29 +186,22 @@ rte_pcapng_write_packets(rte_pcapng_t *self,* The handle to the packet capture file * @param port * The Ethernet port to report stats on. -* @param comment -* Optional comment to add to statistics. -* @param start_time -* The time when packet capture was started in nanoseconds. -* Optional: can be zero if not known. -* @param end_time -* The time when packet capture was stopped in nanoseconds. -* Optional: can be zero if not finished; * @param ifrecv * The number of packets received by capture. * Optional: use UINT64_MAX if not known. * @param ifdrop * The number of packets missed by the capture process. * Optional: use UINT64_MAX if not known. +* @param comment +* Optional comment to add to statistics. * @return * number of bytes written to file, -1 on failure to write file */ __rte_experimental ssize_trte_pcapng_write_stats (rte_pcapng_t *self,uint16_t port, -const char *comment, -uint64_t start_time,uint64_t end_time, -uint64_t ifrecv,uint64_t ifdrop); +uint64_t ifrecv,uint64_t ifdrop, +const char *comment); #ifdef __cplusplus }
lib/pdump/rte_pdump.c +1 -3
@@ -90,7 +90,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,int ring_enq;uint16_t d_pkts =0 ;struct rte_mbuf *dup_bufs[nb_pkts]; -uint64_t ts;struct rte_ring *ring;struct rte_mempool *mp;struct rte_mbuf *p;
@@ -99,7 +98,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->filter)rte_bpf_exec_burst (cbs->filter, (void **)pkts, rcs, nb_pkts); - ts =rte_get_tsc_cycles (); ring = cbs->ring; mp = cbs->mp;for (i =0 ; i < nb_pkts; i++) {
@@ -122,7 +120,7 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->ver ==V2 ) p =rte_pcapng_copy (port_id, queue, pkts[i], mp, cbs->snaplen, - ts, direction,NULL ); + direction,NULL );else p =rte_pktmbuf_copy (pkts[i], mp,0 , cbs->snaplen);
Signature · 1 line
2.39.2
PATCH v6 4/5 pcapng: avoid using alloca()
In reply to Stephen HemmingerThe function alloca() like VLA's has problems if the caller passes a large value. Instead use a fixed size buffer (2K) which will be more than sufficient for the info related blocks in the file. Add bounds checks as well.
--- lib/pcapng/rte_pcapng.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-)
lib/pcapng/rte_pcapng.c +16 -21
@@ -33,6 +33,9 @@/* conversion from DPDK speed to PCAPNG */ #define PCAPNG_MBPS_SPEED 1000000ull +/* upper bound for section, stats and interface blocks */ +#define PCAPNG_BLKSIZ 2048 +/* Format of the capture file handle */ struct rte_pcapng {int outfd;/* output file */
@@ -140,9 +143,8 @@ pcapng_section_block(rte_pcapng_t *self, {struct pcapng_section_header *hdr;struct pcapng_option *opt; -void *buf; +uint8_t buf[PCAPNG_BLKSIZ ];uint32_t len; -ssize_t cc; len =sizeof (*hdr);if (hw)
@@ -158,8 +160,7 @@ pcapng_section_block(rte_pcapng_t *self, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =calloc (1 , len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_section_header *)buf;
@@ -193,10 +194,7 @@ pcapng_section_block(rte_pcapng_t *self,/* clone block_length after option */ memcpy (opt, &hdr->block_length,sizeof (uint32_t )); - cc =write (self->outfd, buf, len); -free (buf); - -return cc; +return write (self->outfd, buf, len); }/* Write an interface block for a DPDK port */
@@ -213,7 +211,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port,struct pcapng_option *opt;const uint8_t tsresol =9 ;/* nanosecond resolution */ uint32_t len; -void *buf; +uint8_t buf[PCAPNG_BLKSIZ ];char ifname_buf[IF_NAMESIZE ];char ifhw[256 ];uint64_t speed =0 ;
@@ -267,8 +265,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =alloca (len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_interface_block *)buf;
@@ -296,17 +293,16 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, opt =pcapng_add_option (opt,PCAPNG_IFB_HARDWARE , ifhw,strlen (ifhw));if (filter) { -/* Encoding is that the first octet indicates string vs BPF */ size_t len; -char *buf; len =strlen (filter) +1 ; - buf =alloca (len); - *buf ='\0' ; -memcpy (buf +1 , filter, len); + opt->code =PCAPNG_IFB_FILTER ; + opt->length = len; +/* Encoding is that the first octet indicates string vs BPF */ + opt->data[0 ] =0 ; +memcpy (opt->data +1 , filter,strlen (filter)); - opt =pcapng_add_option (opt,PCAPNG_IFB_FILTER , - buf, len); + opt = (struct pcapng_option *)((uint8_t *)opt +pcapng_optlen (len)); } opt =pcapng_add_option (opt,PCAPNG_OPT_END ,NULL ,0 );
@@ -333,7 +329,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,uint64_t start_time = self->offset_ns;uint64_t sample_time;uint32_t optlen, len; -uint8_t *buf; +uint8_t buf[PCAPNG_BLKSIZ ];RTE_ETH_VALID_PORTID_OR_ERR_RET (port_id, -EINVAL );
@@ -353,8 +349,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, optlen +=pcapng_optlen (0 ); len =sizeof (*hdr) + optlen +sizeof (uint32_t ); - buf =alloca (len); -if (buf ==NULL ) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_statistics *)buf;
Signature · 1 line
2.39.2
PATCH v6 5/5 test: cleanups to pcapng test
In reply to Stephen HemmingerOverhaul of the pcapng test:
- promote it to be a fast test so it gets regularly run.
- create null device and use i.
- use UDP discard packets that are valid so that for debugging the resulting pcapng file can be looked at with wireshark.
- do basic checks on resulting pcap file that lengths and timestamps are in range.
- add test for interface options
--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 416 +++++++++++++++++++++++++++-------------- 2 files changed, 281 insertions(+), 137 deletions(-)
app/test/meson.build +1 -1
@@ -128,7 +128,7 @@ source_file_deps = { 'test_metrics.c': ['metrics'], 'test_mp_secondary.c': ['hash', 'lpm'], 'test_net_ether.c': ['net'], - 'test_pcapng.c': ['ethdev', 'net', 'pcapng'], + 'test_pcapng.c': ['ethdev', 'net', 'pcapng', 'bus_vdev'], 'test_pdcp.c': ['eventdev', 'pdcp', 'net', 'timer', 'security'], 'test_pdump.c': ['pdump'] + sample_packet_forward_deps, 'test_per_lcore.c': [],
app/test/test_pcapng.c +280 -136
551 lines not on this page · raw message
Signature · 1 line
2.39.2
v7 cover dumpcap and pcapng fixes
In reply to Stephen HemmingerIt fixes issues related to timestamping. The design choices are to maximize performance in the primary process; and do all the time adjustment in the secondary (dumpcap) since the dumpcap needs to system calls anyway to write the result.
This patches set changes where the adjustment is calculated into the pcapng portion that opens the output file. All details of the format of timestamp are contained inside pcapng (data hiding).
v7 - no change, rebase there were some apply failures by CI v6 - make sure all steps compile
v5 - fix format of getpid in capture name
v4 - incorporate review feedback
v3 - don't use alloca() since can have VLA type issues
Stephen Hemminger (5):
pdump: fix setting rte_errno on mp error
dumpcap: allow multiple invocations
pcapng: modify timestamp calculation
pcapng: avoid using alloca()
test: cleanups to pcapng test
app/dumpcap/main.c | 53 ++--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 417 +++++++++++++++++++++++++++------------- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 156 ++++++--------- lib/pcapng/rte_pcapng.h | 19 +- lib/pdump/rte_pdump.c | 9 +- 7 files changed, 373 insertions(+), 285 deletions(-)
Signature · 1 line
2.42.0
PATCH v7 1/5 pdump: fix setting rte_errno on mp error
The response from MP server sets err_value to negative on error. The convention for rte_errno is to use a positive value on error. This makes errors like duplicate registration show up with the correct error value.
--- lib/pdump/rte_pdump.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/pdump/rte_pdump.c +3 -2
@@ -564,9 +564,10 @@ pdump_prepare_client_request(const char *device, uint16_t queue,if (rte_mp_request_sync (&mp_req, &mp_reply, &ts) ==0 ) { mp_rep = &mp_reply.msgs[0 ]; resp = (struct pdump_response *)mp_rep->param; - rte_errno = resp->err_value; -if (!resp->err_value) +if (resp->err_value ==0 ) ret =0 ; +else + rte_errno = -resp->err_value;free (mp_reply.msgs); }
Signature · 1 line
2.42.0
PATCH v7 2/5 dumpcap: allow multiple invocations
In reply to Stephen HemmingerIf dumpcap is run twice with each instance pointing a different interface, it would fail because of overlap in ring a pool names. Fix by putting process id in the name.
It is still not allowed to do multiple invocations on the same interface because only one callback is allowed and only one copy of mbuf is done. Dumpcap will fail with error in this case:
pdump_prepare_client_request(): client request for pdump enable/disable failed
EAL: Error - exiting with code: 1
Cause: Packet dump enable on 0:net_null0 failed File exists
--- app/dumpcap/main.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
app/dumpcap/main.c +14 -14
@@ -44,7 +44,6 @@#include <pcap/pcap.h> #include <pcap/bpf.h> -#define RING_NAME "capture-ring"#define MONITOR_INTERVAL (500 * 1000)#define MBUF_POOL_CACHE_SIZE 32#define BURST_SIZE 32
@@ -647,6 +646,7 @@ static void dpdk_init(void)static struct rte_ring *create_ring (void ) {struct rte_ring *ring; +char ring_name[RTE_RING_NAMESIZE ];size_t size, log2;/* Find next power of 2 >= size. */
@@ -660,28 +660,28 @@ static struct rte_ring *create_ring(void) ring_size = size; } - ring =rte_ring_lookup (RING_NAME ); -if (ring ==NULL ) { - ring =rte_ring_create (RING_NAME , ring_size, -rte_socket_id (),0 ); -if (ring ==NULL ) -rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , -rte_strerror (rte_errno)); - } +/* Want one ring per invocation of program */ +snprintf (ring_name,sizeof (ring_name), +"dumpcap-%d" ,getpid ()); + + ring =rte_ring_create (ring_name, ring_size, +rte_socket_id (),0 ); +if (ring ==NULL ) +rte_exit (EXIT_FAILURE ,"Could not create ring :%s\n" , +rte_strerror (rte_errno)); +return ring; }static struct rte_mempool *create_mempool (void ) {const struct interface *intf; -static const char pool_name[] ="capture_mbufs" ; +char pool_name[RTE_MEMPOOL_NAMESIZE ];size_t num_mbufs =2 * ring_size;struct rte_mempool *mp;uint32_t data_size =128 ; - mp =rte_mempool_lookup (pool_name); -if (mp) -return mp; +snprintf (pool_name,sizeof (pool_name),"capture_%d" ,getpid ());/* Common pool so size mbuf for biggest snap length */ TAILQ_FOREACH(intf, &interfaces, next) {
@@ -826,7 +826,7 @@ static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)rte_exit (EXIT_FAILURE ,"Packet dump enable on %u:%s failed %s\n" , intf->port, intf->name, -rte_strerror (-ret)); +rte_strerror (rte_errno)); }if (intf->opts.promisc_mode) {
Signature · 1 line
2.42.0
PATCH v7 3/5 pcapng: modify timestamp calculation
In reply to Stephen HemmingerThe computation of timestamp is best done in the part of pcapng library that is in secondary process. The secondary process is already doing a bunch of system calls which makes it not performance sensitive. This does change the rte_pcapng_copy()
and rte_pcapng_write_stats() experimental API's.
Simplify the computation of nanoseconds from TSC to a two step process which avoids numeric overflow issues. The previous code was not thread safe as well.
--- app/dumpcap/main.c | 25 +++------ app/test/test_pcapng.c | 7 +-- lib/graph/graph_pcap.c | 2 +- lib/pcapng/rte_pcapng.c | 119 +++++++++++++++------------------------- lib/pcapng/rte_pcapng.h | 19 ++----- lib/pdump/rte_pdump.c | 4 +- 6 files changed, 62 insertions(+), 114 deletions(-)
app/dumpcap/main.c +7 -18
@@ -66,13 +66,13 @@ static bool print_stats;/* capture limit options */ static struct { -uint64_t duration;/* nanoseconds */ +time_t duration;/* seconds */ unsigned long packets;/* number of packets in file */ size_t size;/* file size (bytes) */ } stop;/* Running state */ -static uint64_t start_time, end_time; +static time_t start_time;static uint64_t packets_received;static size_t file_size;
@@ -197,7 +197,7 @@ static void auto_stop(char *opt)if (*value =='\0' || *endp !='\0' || interval <=0 )rte_exit (EXIT_FAILURE ,"Invalid duration \"%s\"\n" , value); - stop.duration =NSEC_PER_SEC * interval; + stop.duration = interval; }else if (strcmp (opt,"filesize" ) ==0 ) { stop.size =get_uint (value,"filesize" ,0 ) *1024 ; }else if (strcmp (opt,"packets" ) ==0 ) {
@@ -511,15 +511,6 @@ static void statistics_loop(void) } } -/* Return the time since 1/1/1970 in nanoseconds */ -static uint64_t create_timestamp (void ) -{ -struct timespec now; - -clock_gettime (CLOCK_MONOTONIC , &now); -return rte_timespec_to_ns (&now); -} -static void cleanup_pdump_resources (void ) {
@@ -589,9 +580,8 @@ report_packet_stats(dumpcap_out_t out) ifdrop = pdump_stats.nombuf + pdump_stats.ringfull;if (use_pcapng) -rte_pcapng_write_stats (out.pcapng, intf->port,NULL , - start_time, end_time, - ifrecv, ifdrop); +rte_pcapng_write_stats (out.pcapng, intf->port, + ifrecv, ifdrop,NULL );if (ifrecv ==0 ) percent =0 ;
@@ -983,7 +973,7 @@ int main(int argc, char **argv) mp =create_mempool (); out =create_output (); - start_time =create_timestamp (); + start_time =time (NULL );enable_pdump (r, mp);if (!quiet) {
@@ -1005,11 +995,10 @@ int main(int argc, char **argv)break ;if (stop.duration !=0 && -create_timestamp () - start_time > stop.duration) +time (NULL ) - start_time > stop.duration)break ; } - end_time =create_timestamp ();disable_primary_monitor ();if (rte_eal_primary_proc_alive (NULL ))
app/test/test_pcapng.c +3 -4
@@ -146,7 +146,7 @@ test_write_packets(void)struct rte_mbuf *mc; mc =rte_pcapng_copy (port_id,0 , orig, mp, pkt_len, -rte_get_tsc_cycles (),0 ,NULL ); +RTE_PCAPNG_DIRECTION_UNKNOWN ,NULL );if (mc ==NULL ) {fprintf (stderr,"Cannot copy packet\n" );return -1 ;
@@ -174,8 +174,7 @@ test_write_stats(void)/* write a statistics block */ len =rte_pcapng_write_stats (pcapng, port_id, -NULL ,0 ,0 , -NUM_PACKETS ,0 ); +UINT64_MAX ,UINT64_MAX ,NULL );if (len <=0 ) {fprintf (stderr,"Write of statistics failed\n" );return -1 ;
@@ -262,7 +261,7 @@ test_write_over_limit_iov_max(void)struct rte_mbuf *mc; mc =rte_pcapng_copy (port_id,0 , orig, mp, pkt_len, -rte_get_tsc_cycles (),0 ,NULL ); +RTE_PCAPNG_DIRECTION_UNKNOWN ,NULL );if (mc ==NULL ) {fprintf (stderr,"Cannot copy packet\n" );return -1 ;
lib/graph/graph_pcap.c +1 -1
@@ -214,7 +214,7 @@ graph_pcap_dispatch(struct rte_graph *graph, mbuf = (struct rte_mbuf *)objs[i]; mc =rte_pcapng_copy (mbuf->port,0 , mbuf, pkt_mp, mbuf->pkt_len, -rte_get_tsc_cycles (),0 , buffer); +0 , buffer);if (mc ==NULL )break ;
lib/pcapng/rte_pcapng.c +45 -74
221 lines not on this page · raw message
lib/pcapng/rte_pcapng.h +5 -14
@@ -114,8 +114,6 @@ enum rte_pcapng_direction {* @param length * The upper limit on bytes to copy. Passing UINT32_MAX * means all data (after offset). -* @param timestamp -* The timestamp in TSC cycles. * @param direction * The direction of the packer: receive, transmit or unknown. * @param comment
@@ -128,7 +126,7 @@ enum rte_pcapng_direction {struct rte_mbuf *rte_pcapng_copy (uint16_t port_id,uint32_t queue,const struct rte_mbuf *m,struct rte_mempool *mp, -uint32_t length,uint64_t timestamp, +uint32_t length,enum rte_pcapng_direction direction,const char *comment);
@@ -178,28 +176,21 @@ rte_pcapng_write_packets(rte_pcapng_t *self,* The handle to the packet capture file * @param port * The Ethernet port to report stats on. -* @param comment -* Optional comment to add to statistics. -* @param start_time -* The time when packet capture was started in nanoseconds. -* Optional: can be zero if not known. -* @param end_time -* The time when packet capture was stopped in nanoseconds. -* Optional: can be zero if not finished; * @param ifrecv * The number of packets received by capture. * Optional: use UINT64_MAX if not known. * @param ifdrop * The number of packets missed by the capture process. * Optional: use UINT64_MAX if not known. +* @param comment +* Optional comment to add to statistics. * @return * number of bytes written to file, -1 on failure to write file */ ssize_t rte_pcapng_write_stats (rte_pcapng_t *self,uint16_t port, -const char *comment, -uint64_t start_time,uint64_t end_time, -uint64_t ifrecv,uint64_t ifdrop); +uint64_t ifrecv,uint64_t ifdrop, +const char *comment); #ifdef __cplusplus }
lib/pdump/rte_pdump.c +1 -3
@@ -90,7 +90,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,int ring_enq;uint16_t d_pkts =0 ;struct rte_mbuf *dup_bufs[nb_pkts]; -uint64_t ts;struct rte_ring *ring;struct rte_mempool *mp;struct rte_mbuf *p;
@@ -99,7 +98,6 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->filter)rte_bpf_exec_burst (cbs->filter, (void **)pkts, rcs, nb_pkts); - ts =rte_get_tsc_cycles (); ring = cbs->ring; mp = cbs->mp;for (i =0 ; i < nb_pkts; i++) {
@@ -122,7 +120,7 @@ pdump_copy(uint16_t port_id, uint16_t queue,if (cbs->ver ==V2 ) p =rte_pcapng_copy (port_id, queue, pkts[i], mp, cbs->snaplen, - ts, direction,NULL ); + direction,NULL );else p =rte_pktmbuf_copy (pkts[i], mp,0 , cbs->snaplen);
Signature · 1 line
2.42.0
PATCH v7 4/5 pcapng: avoid using alloca()
In reply to Stephen HemmingerThe function alloca() like VLA's has problems if the caller passes a large value. Instead use a fixed size buffer (2K) which will be more than sufficient for the info related blocks in the file. Add bounds checks as well.
--- lib/pcapng/rte_pcapng.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-)
lib/pcapng/rte_pcapng.c +16 -21
@@ -33,6 +33,9 @@/* conversion from DPDK speed to PCAPNG */ #define PCAPNG_MBPS_SPEED 1000000ull +/* upper bound for section, stats and interface blocks */ +#define PCAPNG_BLKSIZ 2048 +/* Format of the capture file handle */ struct rte_pcapng {int outfd;/* output file */
@@ -140,9 +143,8 @@ pcapng_section_block(rte_pcapng_t *self, {struct pcapng_section_header *hdr;struct pcapng_option *opt; -void *buf; +uint8_t buf[PCAPNG_BLKSIZ ];uint32_t len; -ssize_t cc; len =sizeof (*hdr);if (hw)
@@ -158,8 +160,7 @@ pcapng_section_block(rte_pcapng_t *self, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =calloc (1 , len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_section_header *)buf;
@@ -193,10 +194,7 @@ pcapng_section_block(rte_pcapng_t *self,/* clone block_length after option */ memcpy (opt, &hdr->block_length,sizeof (uint32_t )); - cc =write (self->outfd, buf, len); -free (buf); - -return cc; +return write (self->outfd, buf, len); }/* Write an interface block for a DPDK port */
@@ -213,7 +211,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port,struct pcapng_option *opt;const uint8_t tsresol =9 ;/* nanosecond resolution */ uint32_t len; -void *buf; +uint8_t buf[PCAPNG_BLKSIZ ];char ifname_buf[IF_NAMESIZE ];char ifhw[256 ];uint64_t speed =0 ;
@@ -267,8 +265,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, len +=pcapng_optlen (0 ); len +=sizeof (uint32_t ); - buf =alloca (len); -if (!buf) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_interface_block *)buf;
@@ -296,17 +293,16 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, opt =pcapng_add_option (opt,PCAPNG_IFB_HARDWARE , ifhw,strlen (ifhw));if (filter) { -/* Encoding is that the first octet indicates string vs BPF */ size_t len; -char *buf; len =strlen (filter) +1 ; - buf =alloca (len); - *buf ='\0' ; -memcpy (buf +1 , filter, len); + opt->code =PCAPNG_IFB_FILTER ; + opt->length = len; +/* Encoding is that the first octet indicates string vs BPF */ + opt->data[0 ] =0 ; +memcpy (opt->data +1 , filter,strlen (filter)); - opt =pcapng_add_option (opt,PCAPNG_IFB_FILTER , - buf, len); + opt = (struct pcapng_option *)((uint8_t *)opt +pcapng_optlen (len)); } opt =pcapng_add_option (opt,PCAPNG_OPT_END ,NULL ,0 );
@@ -333,7 +329,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,uint64_t start_time = self->offset_ns;uint64_t sample_time;uint32_t optlen, len; -uint8_t *buf; +uint8_t buf[PCAPNG_BLKSIZ ];RTE_ETH_VALID_PORTID_OR_ERR_RET (port_id, -EINVAL );
@@ -353,8 +349,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, optlen +=pcapng_optlen (0 ); len =sizeof (*hdr) + optlen +sizeof (uint32_t ); - buf =alloca (len); -if (buf ==NULL ) +if (len >sizeof (buf))return -1 ; hdr = (struct pcapng_statistics *)buf;
Signature · 1 line
2.42.0
PATCH v7 5/5 test: cleanups to pcapng test
In reply to Stephen HemmingerOverhaul of the pcapng test:
- promote it to be a fast test so it gets regularly run.
- create null device and use i.
- use UDP discard packets that are valid so that for debugging the resulting pcapng file can be looked at with wireshark.
- do basic checks on resulting pcap file that lengths and timestamps are in range.
- add test for interface options
--- app/test/meson.build | 2 +- app/test/test_pcapng.c | 416 +++++++++++++++++++++++++++-------------- 2 files changed, 281 insertions(+), 137 deletions(-)
app/test/meson.build +1 -1
@@ -128,7 +128,7 @@ source_file_deps = { 'test_metrics.c': ['metrics'], 'test_mp_secondary.c': ['hash', 'lpm'], 'test_net_ether.c': ['net'], - 'test_pcapng.c': ['ethdev', 'net', 'pcapng'], + 'test_pcapng.c': ['ethdev', 'net', 'pcapng', 'bus_vdev'], 'test_pdcp.c': ['eventdev', 'pdcp', 'net', 'timer', 'security'], 'test_pdump.c': ['pdump'] + sample_packet_forward_deps, 'test_per_lcore.c': [],
app/test/test_pcapng.c +280 -136
551 lines not on this page · raw message
Signature · 1 line
2.42.0
Re: [PATCH v7 0/5] dumpcap and pcapng fixes
In reply to Stephen HemmingerQuoted 6 lines
Stephen Hemminger (5):
pdump: fix settingrte_errnoon mp error
dumpcap: allow multiple invocations
pcapng: modify timestamp calculation
pcapng: avoid usingalloca()
test: cleanups to pcapng test
Applied with a note about the API change in release notes.