Skip to contentSkip to conversation

app/dumpcap: assorted fixes

PATCH New Stephen Hemminger 1 participant 5 messages Nov 17

lore
Share & tools
lore
#0 lore raw

cover app/dumpcap: assorted fixes

Som patchs for 27.03.
These came out of benchmarking dumpcap: capture for 10 seconds, count how many packets landed in the file. The numbers would not line up, and the reasons turned out to be in the stop logic.

The last patch is unrelated cleanup from reading that code. The error handling could call rte_exit before rte_eal_init was run.

All four are candidates for stable.

Stephen Hemminger (4):
app/dumpcap: fix packet count overshoot app/dumpcap: fix capture duration app/dumpcap: fix pcap timestamps app/dumpcap: do not use rte_exit before EAL init

 app/dumpcap/main.c | 87 +++++++++++++++++++++++++++++++++---------------------
 1 file changed, 54 insertions(+), 33 deletions(-)
Signature · 13 lines
2.53.0

Stephen Hemminger (4):
  app/dumpcap: fix packet count overshoot
  app/dumpcap: fix capture duration
  app/dumpcap: fix pcap timestamps
  app/dumpcap: do not use rte_exit before EAL init

 app/dumpcap/main.c | 87 ++++++++++++++++++++++++++++------------------
 1 file changed, 54 insertions(+), 33 deletions(-)

-- 
2.53.0
#1 lore raw

PATCH 1/4 app/dumpcap: fix packet count overshoot

When a packet count was given with -c or -a packets:NUM, the capture could write up to a whole burst (32) of extra packets because the count was only checked after each burst. Limit the burst to the number of packets still wanted.

Fixes cbb44143be74 ("app/dumpcap: add new packet capture application")
---
 app/dumpcap/main.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
app/dumpcap/main.c +6 -3
index 46a6cb251e..c4790988e5 100644
@@ -945,12 +945,15 @@ pcap_write_packets(pcap_dumper_t *dumper,
 static int process_ring(dumpcap_out_t out, struct rte_ring *r)
 {
 	struct rte_mbuf *pkts[BURST_SIZE];
-	unsigned int avail, n;
+	unsigned int avail, n, burst = BURST_SIZE;
 	static unsigned int empty_count;
 	ssize_t written;
 
-	n = rte_ring_sc_dequeue_burst(r, (void **) pkts, BURST_SIZE,
-				      &avail);
+	/* don't capture more than the requested number of packets */
+	if (stop.packets != 0 && stop.packets - packets_received < burst)
+		burst = stop.packets - packets_received;
+
+	n = rte_ring_sc_dequeue_burst(r, (void **) pkts, burst, &avail);
 	if (n == 0) {
 		/* don't consume endless amounts of cpu if idle */
 		if (empty_count < SLEEP_THRESHOLD)
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/4 app/dumpcap: fix capture duration

In reply to Stephen Hemminger

The capture duration was measured using time() which has one second resolution and was compared with greater than, so the capture could run up to a second longer than requested. Also, since the duration was converted to whole seconds, fractional durations were lost; a duration of less than one second would be treated as no limit.

Use the TSC to measure the capture interval, as is done in the rest of DPDK, and keep the fractional part of the duration.

Fixes 166591931b72 ("pcapng: modify timestamp calculation")
---
 app/dumpcap/main.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
app/dumpcap/main.c +6 -5
index c4790988e5..2808f77add 100644
@@ -69,13 +69,13 @@ static bool print_stats;
 
 /* capture limit options */
 static struct {
-	time_t  duration;	/* seconds */
+	double  duration;	/* seconds */
 	unsigned long packets;  /* number of packets in file */
 	size_t size;		/* file size (bytes) */
 } stop;
 
 /* Running state */
-static time_t start_time;
+static uint64_t stop_tsc;	/* TSC when duration expires, 0 if none */
 static uint64_t packets_received;
 static size_t file_size;
 
@@ -1036,7 +1036,9 @@ int main(int argc, char **argv)
 	mp = create_mempool();
 	out = create_output();
 
-	start_time = time(NULL);
+	if (stop.duration > 0)
+		stop_tsc = rte_get_tsc_cycles() +
+			(uint64_t)(stop.duration * rte_get_tsc_hz());
 	enable_pdump(r, mp);
 
 	if (!quiet) {
@@ -1057,8 +1059,7 @@ int main(int argc, char **argv)
 		if (stop.packets && packets_received >= stop.packets)
 			break;
 
-		if (stop.duration != 0 &&
-		    time(NULL) - start_time > stop.duration)
+		if (stop_tsc != 0 && rte_get_tsc_cycles() >= stop_tsc)
 			break;
 	}
 
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/4 app/dumpcap: fix pcap timestamps

In reply to Stephen Hemminger

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")
---
 app/dumpcap/main.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
app/dumpcap/main.c +9 -1
index 2808f77add..1f9f03c7a3 100644
@@ -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];
Signature · 1 line
2.53.0
#4 lore raw

PATCH 4/4 app/dumpcap: do not use rte_exit before EAL init

In reply to Stephen Hemminger

Errors in command line options and in setting up the EAL arguments were reported with rte_exit() or rte_panic(), which call EAL cleanup or dump EAL state before EAL has been initialized.

Use a simple helper which prints the message and exits instead.

Fixes cbb44143be74 ("app/dumpcap: add new packet capture application")
---
 app/dumpcap/main.c | 57 +++++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 24 deletions(-)
app/dumpcap/main.c +33 -24
index 1f9f03c7a3..49979fbf37 100644
@@ -12,6 +12,7 @@
 #include <inttypes.h>
 #include <limits.h>
 #include <signal.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -163,6 +164,22 @@ static const char *version(void)
 	return str;
 }
 
+/*
+ * Exit with error message, for errors before EAL is initialized
+ * where rte_exit() can not be used.
+ */
+static __rte_noreturn __rte_format_printf(1, 2)
+void early_exit(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+
+	exit(EXIT_FAILURE);
+}
+
 /* Parse numeric argument from command line */
 static unsigned long get_uint(const char *arg, const char *name,
 			     unsigned int limit)
@@ -172,13 +189,11 @@ static unsigned long get_uint(const char *arg, const char *name,
 
 	u = strtoul(arg, &endp, 0);
 	if (*arg == '\0' || *endp != '\0')
-		rte_exit(EXIT_FAILURE,
-			 "Specified %s \"%s\" is not a valid number\n",
-			 name, arg);
+		early_exit("Specified %s \"%s\" is not a valid number\n",
+			   name, arg);
 	if (limit && u > limit)
-		rte_exit(EXIT_FAILURE,
-			 "Specified %s \"%s\" is too large (greater than %u)\n",
-			 name, arg, limit);
+		early_exit("Specified %s \"%s\" is too large (greater than %u)\n",
+			   name, arg, limit);
 
 	return u;
 }
@@ -190,24 +205,21 @@ static void auto_stop(char *opt)
 
 	value = strchr(opt, ':');
 	if (value == NULL)
-		rte_exit(EXIT_FAILURE,
-			 "Missing colon in auto stop parameter\n");
+		early_exit("Missing colon in auto stop parameter\n");
 
 	*value++ = '\0';
 	if (strcmp(opt, "duration") == 0) {
 		double interval = strtod(value, &endp);
 
 		if (*value == '\0' || *endp != '\0' || interval <= 0)
-			rte_exit(EXIT_FAILURE,
-				 "Invalid duration \"%s\"\n", value);
+			early_exit("Invalid duration \"%s\"\n", value);
 		stop.duration = interval;
 	} else if (strcmp(opt, "filesize") == 0) {
 		stop.size = get_uint(value, "filesize", 0) * 1024;
 	} else if (strcmp(opt, "packets") == 0) {
 		stop.packets = get_uint(value, "packets", 0);
 	} else {
-		rte_exit(EXIT_FAILURE,
-			 "Unknown autostop parameter \"%s\"\n", opt);
+		early_exit("Unknown autostop parameter \"%s\"\n", opt);
 	}
 }
 
@@ -217,11 +229,11 @@ static struct interface *add_interface(const char *name)
 	struct interface *intf;
 
 	if (strlen(name) >= RTE_ETH_NAME_MAX_LEN)
-		rte_exit(EXIT_FAILURE, "invalid name for interface: '%s'\n", name);
+		early_exit("invalid name for interface: '%s'\n", name);
 
 	intf = malloc(sizeof(*intf));
 	if (!intf)
-		rte_exit(EXIT_FAILURE, "no memory for interface\n");
+		early_exit("no memory for interface\n");
 
 	memset(intf, 0, sizeof(*intf));
 	rte_strscpy(intf->name, name, sizeof(intf->name));
@@ -381,13 +393,11 @@ static void parse_opts(int argc, char **argv)
 				tmp_dir = optarg;
 			} else if (!strcmp(longopt, "ifdescr")) {
 				if (last_intf == NULL)
-					rte_exit(EXIT_FAILURE,
-						 "--ifdescr must be specified after a -i option\n");
+					early_exit("--ifdescr must be specified after a -i option\n");
 				last_intf->ifdescr = optarg;
 			} else if (!strcmp(longopt, "ifname")) {
 				if (last_intf == NULL)
-					rte_exit(EXIT_FAILURE,
-						 "--ifname must be specified after a -i option\n");
+					early_exit("--ifname must be specified after a -i option\n");
 				last_intf->ifname = optarg;
 			} else {
 				usage();
@@ -399,8 +409,7 @@ static void parse_opts(int argc, char **argv)
 			auto_stop(optarg);
 			break;
 		case 'b':
-			rte_exit(EXIT_FAILURE,
-				 "multiple files not implemented\n");
+			early_exit("multiple files not implemented\n");
 			break;
 		case 'c':
 			stop.packets = get_uint(optarg, "packet_count", 0);
@@ -629,7 +638,7 @@ static void dpdk_init(void)
 	/* DPDK API requires mutable versions of command line arguments. */
 	eal_argv = calloc(eal_argc + 1, sizeof(char *));
 	if (eal_argv == NULL)
-		rte_panic("No memory\n");
+		early_exit("No memory\n");
 
 	eal_argv[0] = strdup(progname);
 	for (i = 1; i < RTE_DIM(args); i++)
@@ -647,7 +656,7 @@ static void dpdk_init(void)
 
 	for (i = 0; i < (unsigned int)eal_argc; i++) {
 		if (eal_argv[i] == NULL)
-			rte_panic("No memory\n");
+			early_exit("No memory\n");
 	}
 
 	/*
@@ -656,10 +665,10 @@ static void dpdk_init(void)
 	 */
 	if (lcore_arg == NULL &&
 	    rte_thread_get_affinity_by_id(rte_thread_self(), &cpuset) != 0)
-		rte_panic("rte_thread_getaffinity failed\n");
+		early_exit("rte_thread_getaffinity failed\n");
 
 	if (rte_eal_init(eal_argc, eal_argv) < 0)
-		rte_exit(EXIT_FAILURE, "EAL init failed: is primary process running?\n");
+		early_exit("EAL init failed: is primary process running?\n");
 
 	/*
 	 * Register pdump callback handler.
Signature · 1 line
2.53.0