public inbox for dev@dpdk.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH v3 5/5] test: add pcapng test for copy before open
Date: Sat, 28 Feb 2026 09:35:04 -0800	[thread overview]
Message-ID: <20260228175601.585102-6-stephen@networkplumber.org> (raw)
In-Reply-To: <20260228175601.585102-1-stephen@networkplumber.org>

If packets are copied before open is called, the timestamps
will be before the start of epoch. Add a test for this which
makes sure pcapng file is still valid.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_pcapng.c | 82 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c
index 53b806833b..298bcbd31f 100644
--- a/app/test/test_pcapng.c
+++ b/app/test/test_pcapng.c
@@ -591,6 +591,87 @@ test_write_packets(void)
 	return -1;
 }
 
+static int
+test_write_before_open(void)
+{
+	char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
+	struct dummy_mbuf mbfs;
+	struct rte_mbuf *clones[MAX_BURST];
+	rte_pcapng_t *pcapng = NULL;
+	int ret, tmp_fd, i;
+	unsigned int count = 8;
+	uint64_t now;
+	ssize_t len;
+
+	mbuf1_prepare(&mbfs);
+	mbuf1_resize(&mbfs, rte_rand_max(MAX_DATA_SIZE));
+
+	/* Copy packets BEFORE opening the pcapng file.
+	 * This exercises the negative TSC delta path in tsc_to_ns_epoch().
+	 */
+	for (i = 0; i < (int)count; i++) {
+		clones[i] = rte_pcapng_copy(port_id, 0, &mbfs.mb[0], mp,
+					    rte_pktmbuf_pkt_len(&mbfs.mb[0]),
+					    RTE_PCAPNG_DIRECTION_IN, NULL);
+		if (clones[i] == NULL) {
+			fprintf(stderr, "Cannot copy packet before open\n");
+			rte_pktmbuf_free_bulk(clones, i);
+			return -1;
+		}
+	}
+
+	/* Small delay so fdopen's tsc_base is measurably after the copies */
+	rte_delay_us_block(100);
+
+	now = current_timestamp();
+
+	tmp_fd = mkstemps(file_name, strlen(".pcapng"));
+	if (tmp_fd == -1) {
+		perror("mkstemps() failure");
+		rte_pktmbuf_free_bulk(clones, count);
+		return -1;
+	}
+
+	pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_preopen", NULL);
+	if (pcapng == NULL) {
+		fprintf(stderr, "rte_pcapng_fdopen failed\n");
+		close(tmp_fd);
+		rte_pktmbuf_free_bulk(clones, count);
+		return -1;
+	}
+
+	ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB,
+				       NULL, NULL, NULL);
+	if (ret < 0) {
+		fprintf(stderr, "can not add port %u\n", port_id);
+		goto fail;
+	}
+
+	/* Write the pre-captured packets — timestamps precede tsc_base */
+	len = rte_pcapng_write_packets(pcapng, clones, count);
+	rte_pktmbuf_free_bulk(clones, count);
+	if (len <= 0) {
+		fprintf(stderr, "Write of pre-open packets failed: %s\n",
+			rte_strerror(rte_errno));
+		goto fail;
+	}
+
+	rte_pcapng_close(pcapng);
+
+	/* Validate the file is parseable — timestamps should be
+	 * slightly before 'now' but still reasonable.
+	 */
+	ret = valid_pcapng_file(file_name, now - NS_PER_S, count);
+	if (ret == 0)
+		remove(file_name);
+
+	return ret;
+
+fail:
+	rte_pcapng_close(pcapng);
+	return -1;
+}
+
 static void
 test_cleanup(void)
 {
@@ -606,6 +687,7 @@ unit_test_suite test_pcapng_suite  = {
 	.unit_test_cases = {
 		TEST_CASE(test_add_interface),
 		TEST_CASE(test_write_packets),
+		TEST_CASE(test_write_before_open),
 		TEST_CASES_END()
 	}
 };
-- 
2.51.0


  parent reply	other threads:[~2026-02-28 17:56 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-20 18:41 [PATCH 0/2] pcapng: bug fixes Stephen Hemminger
2026-02-20 18:41 ` [PATCH 1/2] test: fix pcapng test to work on Windows Stephen Hemminger
2026-02-20 18:41 ` [PATCH 2/2] pcapng: handle packets copied before file open Stephen Hemminger
2026-02-24 15:25 ` [PATCH v2 0/4] pcapng: fix test and copy before open Stephen Hemminger
2026-02-24 15:25   ` [PATCH v2 1/4] test: fix pcapng test to work on Windows Stephen Hemminger
2026-02-24 15:25   ` [PATCH v2 2/4] pcapng: handle packets copied before file open Stephen Hemminger
2026-02-24 15:25   ` [PATCH v2 3/4] test: add pcapng test for copy before open Stephen Hemminger
2026-02-24 15:25   ` [PATCH v2 4/4] test: use fixed time length for write packet test Stephen Hemminger
2026-02-25 22:57 ` [PATCH v3 0/4] pcapng: enhancements and test fix Stephen Hemminger
2026-02-25 22:57   ` [PATCH v3 1/4] test: fix pcapng test to work on Windows Stephen Hemminger
2026-02-25 22:57   ` [PATCH v3 2/4] test: use fixed time length for write packet test Stephen Hemminger
2026-02-25 22:57   ` [PATCH v3 3/4] pcapng: handle packets copied before file open Stephen Hemminger
2026-02-25 22:57   ` [PATCH v3 4/4] test: add pcapng test for copy before open Stephen Hemminger
2026-02-28 17:34 ` [PATCH v3 0/5] pcapng: fixes for Windows and timestamps Stephen Hemminger
2026-02-28 17:35   ` [PATCH v3 1/5] test: fix pcapng test to work on Windows Stephen Hemminger
2026-02-28 17:35   ` [PATCH v3 2/5] test: use fixed time length for write packet test Stephen Hemminger
2026-02-28 17:35   ` [PATCH v3 3/5] test: better logging for pcapng test Stephen Hemminger
2026-02-28 17:35   ` [PATCH v3 4/5] pcapng: handle packets copied before file open Stephen Hemminger
2026-03-19 10:34     ` Kevin Traynor
2026-03-31 18:07       ` Stephen Hemminger
2026-02-28 17:35   ` Stephen Hemminger [this message]
2026-03-17 10:38   ` [PATCH v3 0/5] pcapng: fixes for Windows and timestamps Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260228175601.585102-6-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox