The pcap output file is opened with nanosecond timestamp
precision, but the timestamp was set with gettimeofday()
which gives microseconds. This caused the fractional part
of the timestamps to be 1000 times too small.

Use clock_gettime() to get the timestamp in nanoseconds.

Fixes: cbb44143be74 ("app/dumpcap: add new packet capture application")
Cc: [email protected]

Signed-off-by: Stephen Hemminger <[email protected]>
---
 app/dumpcap/main.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
index 2808f77add..1f9f03c7a3 100644
--- a/app/dumpcap/main.c
+++ b/app/dumpcap/main.c
@@ -916,10 +916,18 @@ pcap_write_packets(pcap_dumper_t *dumper,
 {
        uint8_t temp_data[RTE_ETHER_MAX_JUMBO_FRAME_LEN];
        struct pcap_pkthdr header;
+       struct timespec ts;
        uint16_t i;
        size_t total = 0;
 
-       gettimeofday(&header.ts, NULL);
+       /*
+        * The use of tv_usec as nanoseconds is not a bug here.
+        * Output is opened with PCAP_TSTAMP_PRECISION_NANO in create_output(),
+        * and that is how the pcap API bodged in nanosecond support.
+        */
+       clock_gettime(CLOCK_REALTIME, &ts);
+       header.ts.tv_sec = ts.tv_sec;
+       header.ts.tv_usec = ts.tv_nsec;
 
        for (i = 0; i < n; i++) {
                struct rte_mbuf *m = pkts[i];
-- 
2.53.0

Reply via email to