The pcapng test bounded the packet timestamps it read back using
clock_gettime(CLOCK_REALTIME) sampled around the capture, but the
timestamps in the file come from the TSC converted with the reference
point EAL records at init. Comparing the two measures the drift
between the calibrated TSC rate and NTP's slewing of the wall clock,
which grows with process uptime. Packets were reported a few tens of
microseconds before the start of the window.

Bound the window with rte_tsc_to_ns() so both sides of the comparison
come from the same reference point.

What matters for analysis is the time between packets rather than the
absolute offset, so also check that the timestamps never go backwards.
A reference point that moved during the capture would show up as a
negative delta, which no test covered before.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 app/test/test_pcapng.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c
index 7e19aa4096..0cf5d469e5 100644
--- a/app/test/test_pcapng.c
+++ b/app/test/test_pcapng.c
@@ -350,6 +350,7 @@ struct pkt_print_ctx {
        unsigned int count;
        uint64_t start_ns;
        uint64_t end_ns;
+       uint64_t prev_ns;
 };
 
 static void
@@ -398,6 +399,19 @@ parse_pcap_packet(u_char *user, const struct pcap_pkthdr 
*h,
                goto error;
        }
 
+       /*
+        * Packets are stamped from the TSC and written in order, so the
+        * time between them must never go backwards. A reference point
+        * that moved mid-capture, for example by following a wall clock
+        * step, would show up here as a negative delta.
+        */
+       if (ns < ctx->prev_ns) {
+               printf("Timestamp went backwards by %"PRIu64" ns\n",
+                      ctx->prev_ns - ns);
+               goto error;
+       }
+       ctx->prev_ns = ns;
+
        if (!rte_is_broadcast_ether_addr(&eh->dst_addr)) {
                printf("Destination is not broadcast\n");
                goto error;
@@ -441,8 +455,16 @@ valid_pcapng_file(const char *file_name, uint64_t started, 
unsigned int expected
        struct pkt_print_ctx ctx = { };
        int ret;
 
+       /*
+        * Packet timestamps come from the TSC converted with the reference
+        * point EAL took at init. Comparing them against CLOCK_REALTIME
+        * read now would measure the drift between the calibrated TSC rate
+        * and NTP's slewing of the wall clock, so bound the window using
+        * the same conversion the library uses.
+        */
        ctx.start_ns = started;
-       ctx.end_ns = current_timestamp() + NS_PER_S;
+       ctx.end_ns = rte_tsc_to_ns(rte_get_tsc_cycles()) + NS_PER_S;
+       ctx.prev_ns = started;
 
        ctx.pcap = pcap_open_offline_with_tstamp_precision(file_name,
                                                           
PCAP_TSTAMP_PRECISION_NANO,
@@ -474,7 +496,7 @@ test_add_interface(void)
        char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
        static rte_pcapng_t *pcapng;
        int ret, tmp_fd;
-       uint64_t now = current_timestamp();
+       uint64_t now = rte_tsc_to_ns(rte_get_tsc_cycles());
 
        tmp_fd = mkstemps(file_name, strlen(".pcapng"));
        if (tmp_fd == -1) {
@@ -535,7 +557,7 @@ test_write_packets(void)
        char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
        rte_pcapng_t *pcapng = NULL;
        int ret, tmp_fd, count;
-       uint64_t now = current_timestamp();
+       uint64_t now = rte_tsc_to_ns(rte_get_tsc_cycles());
 
        tmp_fd = mkstemps(file_name, strlen(".pcapng"));
        if (tmp_fd == -1) {
@@ -625,7 +647,7 @@ test_write_before_open(void)
        /* Small delay so the capture start is measurably after the copies */
        rte_delay_us_block(100);
 
-       now = current_timestamp();
+       now = rte_tsc_to_ns(rte_get_tsc_cycles());
 
        tmp_fd = mkstemps(file_name, strlen(".pcapng"));
        if (tmp_fd == -1) {
-- 
2.53.0

Reply via email to