* [PATCH v2 1/4] monitor: add --time-format,-t option
@ 2024-12-02 14:54 James Prestwood
2024-12-02 14:54 ` [PATCH v2 2/4] monitor: track current PCAP size James Prestwood
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: James Prestwood @ 2024-12-02 14:54 UTC (permalink / raw)
To: iwd; +Cc: James Prestwood
For syncing iwmon captures with other logging its useful to
timestamp in some absolute format like UTC. This adds an
option which allows the user to specify what time format to
show. For now support:
delta - (default) The time delta between the first packet
and the current packet.
utc - The packet time in UTC
---
monitor/main.c | 56 ++++++++++++++++++++++++++++++-------------------
monitor/nlmon.c | 45 +++++++++++++++++++++++++++++++++------
monitor/nlmon.h | 6 ++++++
3 files changed, 80 insertions(+), 27 deletions(-)
v2:
* Fix compiler warning on musl. The switch statement checking the
time format had no default case which would cause 'n' to be
uninitialized. Its an impossible condition least with the
current code base, but it doesn't hurt to add a default to
future proof.
diff --git a/monitor/main.c b/monitor/main.c
index fc22c777..fe40e301 100644
--- a/monitor/main.c
+++ b/monitor/main.c
@@ -718,29 +718,32 @@ static void usage(void)
"Usage:\n");
printf("\tiwmon [options]\n");
printf("Options:\n"
- "\t-r, --read <file> Read netlink PCAP trace file\n"
- "\t-w, --write <file> Write netlink PCAP trace file\n"
- "\t-a, --analyze <file> Analyze netlink PCAP trace file\n"
- "\t-i, --interface <dev> Use specified netlink monitor\n"
- "\t-n, --nortnl Don't show RTNL output\n"
- "\t-y, --nowiphy Don't show 'New Wiphy' output\n"
- "\t-s, --noscan Don't show scan result output\n"
- "\t-e, --noies Don't show IEs except SSID\n"
- "\t-h, --help Show help options\n");
+ "\t-r, --read <file> Read netlink PCAP trace file\n"
+ "\t-w, --write <file> Write netlink PCAP trace file\n"
+ "\t-a, --analyze <file> Analyze netlink PCAP trace file\n"
+ "\t-i, --interface <dev> Use specified netlink monitor\n"
+ "\t-n, --nortnl Don't show RTNL output\n"
+ "\t-y, --nowiphy Don't show 'New Wiphy' output\n"
+ "\t-s, --noscan Don't show scan result output\n"
+ "\t-e, --noies Don't show IEs except SSID\n"
+ "\t-t, --time-format <format> Time format to display. Either\n"
+ "\t\t\t\t 'delta' or 'utc'.\n"
+ "\t-h, --help Show help options\n");
}
static const struct option main_options[] = {
- { "read", required_argument, NULL, 'r' },
- { "write", required_argument, NULL, 'w' },
- { "analyze", required_argument, NULL, 'a' },
- { "nl80211", required_argument, NULL, 'F' },
- { "interface", required_argument, NULL, 'i' },
- { "nortnl", no_argument, NULL, 'n' },
- { "nowiphy", no_argument, NULL, 'y' },
- { "noscan", no_argument, NULL, 's' },
- { "noies", no_argument, NULL, 'e' },
- { "version", no_argument, NULL, 'v' },
- { "help", no_argument, NULL, 'h' },
+ { "read", required_argument, NULL, 'r' },
+ { "write", required_argument, NULL, 'w' },
+ { "analyze", required_argument, NULL, 'a' },
+ { "nl80211", required_argument, NULL, 'F' },
+ { "interface", required_argument, NULL, 'i' },
+ { "nortnl", no_argument, NULL, 'n' },
+ { "nowiphy", no_argument, NULL, 'y' },
+ { "noscan", no_argument, NULL, 's' },
+ { "noies", no_argument, NULL, 'e' },
+ { "time-format", required_argument, NULL, 't' },
+ { "version", no_argument, NULL, 'v' },
+ { "help", no_argument, NULL, 'h' },
{ }
};
@@ -754,7 +757,7 @@ int main(int argc, char *argv[])
for (;;) {
int opt;
- opt = getopt_long(argc, argv, "r:w:a:i:nvhyse",
+ opt = getopt_long(argc, argv, "r:w:a:i:t:nvhyse",
main_options, NULL);
if (opt < 0)
break;
@@ -784,6 +787,17 @@ int main(int argc, char *argv[])
break;
case 'e':
config.noies = true;
+ break;
+ case 't':
+ if (!strcmp(optarg, "delta"))
+ config.time_format = TIME_FORMAT_DELTA;
+ else if (!strcmp(optarg, "utc"))
+ config.time_format = TIME_FORMAT_UTC;
+ else {
+ printf("Invalid time format '%s'", optarg);
+ return EXIT_FAILURE;
+ }
+
break;
case 'v':
printf("%s\n", VERSION);
diff --git a/monitor/nlmon.c b/monitor/nlmon.c
index 60adddc5..b30b1add 100644
--- a/monitor/nlmon.c
+++ b/monitor/nlmon.c
@@ -29,6 +29,7 @@
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
+#include <time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
@@ -113,6 +114,7 @@ struct nlmon {
bool noscan;
bool noies;
bool read;
+ enum time_format time_format;
};
struct nlmon_req {
@@ -185,11 +187,15 @@ static void nlmon_req_free(void *data)
}
static time_t time_offset = ((time_t) -1);
+static enum time_format time_format;
-static inline void update_time_offset(const struct timeval *tv)
+static inline void update_time_offset(const struct timeval *tv,
+ enum time_format tf)
{
- if (tv && time_offset == ((time_t) -1))
+ if (tv && time_offset == ((time_t) -1)) {
time_offset = tv->tv_sec;
+ time_format = tf;
+ }
}
#define print_indent(indent, color1, prefix, title, color2, fmt, args...) \
@@ -225,15 +231,38 @@ static void print_packet(const struct timeval *tv, char ident,
int n, ts_len = 0, ts_pos = 0, len = 0, pos = 0;
if (tv) {
+ struct tm *tm;
+
if (use_color()) {
n = sprintf(ts_str + ts_pos, "%s", COLOR_TIMESTAMP);
if (n > 0)
ts_pos += n;
}
- n = sprintf(ts_str + ts_pos, " %" PRId64 ".%06" PRId64,
+ switch (time_format) {
+ case TIME_FORMAT_DELTA:
+ n = sprintf(ts_str + ts_pos, " %" PRId64 ".%06" PRId64,
(int64_t)tv->tv_sec - time_offset,
(int64_t)tv->tv_usec);
+ break;
+ case TIME_FORMAT_UTC:
+ tm = gmtime(&tv->tv_sec);
+ if (!tm) {
+ n = sprintf(ts_str + ts_pos, "%s",
+ "Time error");
+ break;
+ }
+
+ n = strftime(ts_str + ts_pos, sizeof(ts_str) - ts_pos,
+ "%b %d %H:%M:%S", tm);
+ break;
+ default:
+ /* Should never happen */
+ printf("Unknown time format");
+ l_main_quit();
+ return;
+ }
+
if (n > 0) {
ts_pos += n;
ts_len += n;
@@ -7497,6 +7526,7 @@ struct nlmon *nlmon_create(uint16_t id, const struct nlmon_config *config)
nlmon->noscan = config->noscan;
nlmon->noies = config->noies;
nlmon->read = config->read_only;
+ nlmon->time_format = config->time_format;
return nlmon;
}
@@ -8333,7 +8363,10 @@ void nlmon_print_rtnl(struct nlmon *nlmon, const struct timeval *tv,
int64_t aligned_size = NLMSG_ALIGN(size);
const struct nlmsghdr *nlmsg;
- update_time_offset(tv);
+ if (nlmon->nortnl)
+ return;
+
+ update_time_offset(tv, nlmon->time_format);
for (nlmsg = data; NLMSG_OK(nlmsg, aligned_size);
nlmsg = NLMSG_NEXT(nlmsg, aligned_size)) {
@@ -8371,7 +8404,7 @@ void nlmon_print_genl(struct nlmon *nlmon, const struct timeval *tv,
{
const struct nlmsghdr *nlmsg;
- update_time_offset(tv);
+ update_time_offset(tv, nlmon->time_format);
for (nlmsg = data; NLMSG_OK(nlmsg, size);
nlmsg = NLMSG_NEXT(nlmsg, size)) {
@@ -8394,7 +8427,7 @@ void nlmon_print_pae(struct nlmon *nlmon, const struct timeval *tv,
{
char extra_str[16];
- update_time_offset(tv);
+ update_time_offset(tv, nlmon->time_format);
sprintf(extra_str, "len %u", size);
diff --git a/monitor/nlmon.h b/monitor/nlmon.h
index bb1a7c58..bbc5d250 100644
--- a/monitor/nlmon.h
+++ b/monitor/nlmon.h
@@ -25,12 +25,18 @@
struct nlmon;
+enum time_format {
+ TIME_FORMAT_DELTA,
+ TIME_FORMAT_UTC,
+};
+
struct nlmon_config {
bool nortnl;
bool nowiphy;
bool noscan;
bool noies;
bool read_only;
+ enum time_format time_format;
};
struct nlmon *nlmon_open(uint16_t id, const char *pathname,
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 2/4] monitor: track current PCAP size
2024-12-02 14:54 [PATCH v2 1/4] monitor: add --time-format,-t option James Prestwood
@ 2024-12-02 14:54 ` James Prestwood
2024-12-02 14:54 ` [PATCH v2 3/4] monitor: add support for limiting PCAP size/count James Prestwood
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: James Prestwood @ 2024-12-02 14:54 UTC (permalink / raw)
To: iwd; +Cc: James Prestwood
This will come into play when support for rolling captures is
added to iwmon.
---
monitor/pcap.c | 10 ++++++++++
monitor/pcap.h | 1 +
2 files changed, 11 insertions(+)
diff --git a/monitor/pcap.c b/monitor/pcap.c
index fb29eea8..b13a29f3 100644
--- a/monitor/pcap.c
+++ b/monitor/pcap.c
@@ -60,6 +60,7 @@ struct pcap {
bool closed;
uint32_t type;
uint32_t snaplen;
+ size_t size;
};
struct pcap *pcap_open(const char *pathname)
@@ -152,6 +153,8 @@ struct pcap *pcap_create(const char *pathname)
goto failed;
}
+ pcap->size += len;
+
return pcap;
failed:
@@ -188,6 +191,11 @@ uint32_t pcap_get_snaplen(struct pcap *pcap)
return pcap->snaplen;
}
+size_t pcap_get_size(struct pcap *pcap)
+{
+ return pcap->size;
+}
+
bool pcap_read(struct pcap *pcap, struct timeval *tv,
void *data, uint32_t size, uint32_t *len, uint32_t *real_len)
{
@@ -279,5 +287,7 @@ bool pcap_write(struct pcap *pcap, const struct timeval *tv,
return false;
}
+ pcap->size += written;
+
return true;
}
diff --git a/monitor/pcap.h b/monitor/pcap.h
index 1705b33d..5b797cf3 100644
--- a/monitor/pcap.h
+++ b/monitor/pcap.h
@@ -36,6 +36,7 @@ void pcap_close(struct pcap *pcap);
uint32_t pcap_get_type(struct pcap *pcap);
uint32_t pcap_get_snaplen(struct pcap *pcap);
+size_t pcap_get_size(struct pcap *pcap);
bool pcap_read(struct pcap *pcap, struct timeval *tv,
void *data, uint32_t size, uint32_t *len, uint32_t *real_len);
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 3/4] monitor: add support for limiting PCAP size/count
2024-12-02 14:54 [PATCH v2 1/4] monitor: add --time-format,-t option James Prestwood
2024-12-02 14:54 ` [PATCH v2 2/4] monitor: track current PCAP size James Prestwood
@ 2024-12-02 14:54 ` James Prestwood
2024-12-02 14:54 ` [PATCH v2 4/4] monitor: add --pcap-size,--pcap-count James Prestwood
2024-12-17 17:29 ` [PATCH v2 1/4] monitor: add --time-format,-t option Denis Kenzior
3 siblings, 0 replies; 5+ messages in thread
From: James Prestwood @ 2024-12-02 14:54 UTC (permalink / raw)
To: iwd; +Cc: James Prestwood
This implements support for "rolling captures" by allowing iwmon to
limit the PCAP file size and number of PCAP's that are created.
This is a useful feature when long term monitoring is needed. If
there is some rare behavior requiring iwmon to run for days, months,
or longer the resulting PCAP file would become quite large and fill
up disk space.
When enabled (command line arguments in subsequent patch) the PCAP
file size is checked on each write. If it exceeds the limit a new
PCAP file will be created. Once the number of old PCAP files reaches
the set limit the oldest PCAP will be removed from disk.
---
monitor/nlmon.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++--
monitor/nlmon.h | 4 +++
2 files changed, 85 insertions(+), 2 deletions(-)
diff --git a/monitor/nlmon.c b/monitor/nlmon.c
index b30b1add..76eb2db6 100644
--- a/monitor/nlmon.c
+++ b/monitor/nlmon.c
@@ -43,6 +43,7 @@
#include <linux/genetlink.h>
#include <linux/rtnetlink.h>
#include <linux/filter.h>
+#include <linux/limits.h>
#include <ell/ell.h>
#ifndef ARPHRD_NETLINK
@@ -94,6 +95,8 @@
#define BSS_CAPABILITY_APSD (1<<11)
#define BSS_CAPABILITY_DSSS_OFDM (1<<13)
+#define BYTES_PER_MB 1000000
+
struct nlmon *cur_nlmon;
enum msg_type {
@@ -115,6 +118,11 @@ struct nlmon {
bool noies;
bool read;
enum time_format time_format;
+
+ char *file_prefix;
+ unsigned int file_idx;
+ unsigned int max_files;
+ unsigned int max_size;
};
struct nlmon_req {
@@ -7393,6 +7401,64 @@ static bool nlmon_req_match(const void *a, const void *b)
return (req->seq == match->seq && req->pid == match->pid);
}
+/*
+ * Ensures that PCAP names are zero padded when needed. This makes the files
+ * sort correctly.
+ */
+static void next_pcap_name(char *buf, size_t size, const char *prefix,
+ unsigned int idx, unsigned int max)
+{
+ unsigned int ndigits = 1;
+
+ while (max > 9) {
+ max /= 10;
+ ndigits++;
+ }
+
+ snprintf(buf, size, "%s%.*u", prefix, ndigits, idx);
+}
+
+static bool check_pcap(struct nlmon *nlmon, size_t next_size)
+{
+ char path[PATH_MAX];
+
+ if (!nlmon->pcap)
+ return false;
+
+ if (!nlmon->max_size)
+ return true;
+
+ if (pcap_get_size(nlmon->pcap) + next_size <= nlmon->max_size)
+ return true;
+
+ pcap_close(nlmon->pcap);
+
+ /* Exausted the single PCAP file */
+ if (nlmon->max_files < 2) {
+ printf("Reached maximum size of PCAP, exiting\n");
+ nlmon->pcap = NULL;
+ l_main_quit();
+ return false;
+ }
+
+ next_pcap_name(path, sizeof(path), nlmon->file_prefix,
+ ++nlmon->file_idx, nlmon->max_files);
+
+ nlmon->pcap = pcap_create(path);
+
+ if (nlmon->max_files > nlmon->file_idx)
+ return true;
+
+ /* Remove oldest PCAP file */
+ next_pcap_name(path, sizeof(path), nlmon->file_prefix,
+ nlmon->file_idx - nlmon->max_files, nlmon->max_files);
+
+ if (remove(path) < 0)
+ printf("Failed to remove old PCAP file %s\n", path);
+
+ return true;
+}
+
static void store_packet(struct nlmon *nlmon, const struct timeval *tv,
uint16_t pkt_type,
uint16_t arphrd_type,
@@ -7401,7 +7467,7 @@ static void store_packet(struct nlmon *nlmon, const struct timeval *tv,
{
uint8_t sll_hdr[16], *buf = sll_hdr;
- if (!nlmon->pcap)
+ if (!check_pcap(nlmon, sizeof(sll_hdr) + size))
return;
memset(sll_hdr, 0, sizeof(sll_hdr));
@@ -7527,6 +7593,9 @@ struct nlmon *nlmon_create(uint16_t id, const struct nlmon_config *config)
nlmon->noies = config->noies;
nlmon->read = config->read_only;
nlmon->time_format = config->time_format;
+ nlmon->max_files = config->pcap_file_count;
+ /* Command line expects MB, but use bytes internally */
+ nlmon->max_size = config->pcap_file_size * BYTES_PER_MB;
return nlmon;
}
@@ -8554,13 +8623,20 @@ struct nlmon *nlmon_open(uint16_t id, const char *pathname,
struct nlmon *nlmon;
struct l_io *pae_io;
struct pcap *pcap;
+ char path[PATH_MAX];
pae_io = open_pae();
if (!pae_io)
return NULL;
if (pathname) {
- pcap = pcap_create(pathname);
+ if (config->pcap_file_count > 1)
+ next_pcap_name(path, sizeof(path), pathname,
+ 0, config->pcap_file_count);
+ else
+ snprintf(path, sizeof(path), "%s", pathname);
+
+ pcap = pcap_create(path);
if (!pcap) {
l_io_destroy(pae_io);
return NULL;
@@ -8573,6 +8649,7 @@ struct nlmon *nlmon_open(uint16_t id, const char *pathname,
nlmon->pae_io = pae_io;
nlmon->pcap = pcap;
+ nlmon->file_prefix = l_strdup(pathname);
l_io_set_read_handler(nlmon->pae_io, pae_receive, nlmon, NULL);
@@ -8595,5 +8672,7 @@ void nlmon_close(struct nlmon *nlmon)
if (nlmon->pcap)
pcap_close(nlmon->pcap);
+ l_free(nlmon->file_prefix);
+
l_free(nlmon);
}
diff --git a/monitor/nlmon.h b/monitor/nlmon.h
index bbc5d250..fa027021 100644
--- a/monitor/nlmon.h
+++ b/monitor/nlmon.h
@@ -37,6 +37,10 @@ struct nlmon_config {
bool noies;
bool read_only;
enum time_format time_format;
+
+ /* File size in MB */
+ uint32_t pcap_file_size;
+ uint32_t pcap_file_count;
};
struct nlmon *nlmon_open(uint16_t id, const char *pathname,
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 4/4] monitor: add --pcap-size,--pcap-count
2024-12-02 14:54 [PATCH v2 1/4] monitor: add --time-format,-t option James Prestwood
2024-12-02 14:54 ` [PATCH v2 2/4] monitor: track current PCAP size James Prestwood
2024-12-02 14:54 ` [PATCH v2 3/4] monitor: add support for limiting PCAP size/count James Prestwood
@ 2024-12-02 14:54 ` James Prestwood
2024-12-17 17:29 ` [PATCH v2 1/4] monitor: add --time-format,-t option Denis Kenzior
3 siblings, 0 replies; 5+ messages in thread
From: James Prestwood @ 2024-12-02 14:54 UTC (permalink / raw)
To: iwd; +Cc: James Prestwood
The user can now limit the size and count of PCAP files iwmon will
create. This allows iwmon to run for long periods of time without
filling up disk space.
---
monitor/main.c | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/monitor/main.c b/monitor/main.c
index fe40e301..11077cee 100644
--- a/monitor/main.c
+++ b/monitor/main.c
@@ -728,6 +728,8 @@ static void usage(void)
"\t-e, --noies Don't show IEs except SSID\n"
"\t-t, --time-format <format> Time format to display. Either\n"
"\t\t\t\t 'delta' or 'utc'.\n"
+ "\t-W,--pcap-count Maximum number of PCAP files\n"
+ "\t-C,--pcap-size Maximum size (MB) of PCAP files\n"
"\t-h, --help Show help options\n");
}
@@ -742,6 +744,8 @@ static const struct option main_options[] = {
{ "noscan", no_argument, NULL, 's' },
{ "noies", no_argument, NULL, 'e' },
{ "time-format", required_argument, NULL, 't' },
+ { "pcap-count", required_argument, NULL, 'W' },
+ { "pcap-size", required_argument, NULL, 'C' },
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ }
@@ -757,7 +761,7 @@ int main(int argc, char *argv[])
for (;;) {
int opt;
- opt = getopt_long(argc, argv, "r:w:a:i:t:nvhyse",
+ opt = getopt_long(argc, argv, "r:w:a:i:t:W:C:nvhyse",
main_options, NULL);
if (opt < 0)
break;
@@ -798,6 +802,24 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
+ break;
+ case 'W':
+ if (l_safe_atou32(optarg,
+ &config.pcap_file_count) < 0 ||
+ config.pcap_file_count == 0) {
+ printf("Invalid file count '%s'\n", optarg);
+ return EXIT_FAILURE;
+ }
+
+ break;
+ case 'C':
+ if (l_safe_atou32(optarg,
+ &config.pcap_file_size) < 0 ||
+ config.pcap_file_size == 0) {
+ printf("Invalid file size '%s'\n", optarg);
+ return EXIT_FAILURE;
+ }
+
break;
case 'v':
printf("%s\n", VERSION);
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2 1/4] monitor: add --time-format,-t option
2024-12-02 14:54 [PATCH v2 1/4] monitor: add --time-format,-t option James Prestwood
` (2 preceding siblings ...)
2024-12-02 14:54 ` [PATCH v2 4/4] monitor: add --pcap-size,--pcap-count James Prestwood
@ 2024-12-17 17:29 ` Denis Kenzior
3 siblings, 0 replies; 5+ messages in thread
From: Denis Kenzior @ 2024-12-17 17:29 UTC (permalink / raw)
To: James Prestwood, iwd
Hi James,
On 12/2/24 8:54 AM, James Prestwood wrote:
> For syncing iwmon captures with other logging its useful to
> timestamp in some absolute format like UTC. This adds an
> option which allows the user to specify what time format to
> show. For now support:
>
> delta - (default) The time delta between the first packet
> and the current packet.
> utc - The packet time in UTC
> ---
> monitor/main.c | 56 ++++++++++++++++++++++++++++++-------------------
> monitor/nlmon.c | 45 +++++++++++++++++++++++++++++++++------
> monitor/nlmon.h | 6 ++++++
> 3 files changed, 80 insertions(+), 27 deletions(-)
>
> v2:
> * Fix compiler warning on musl. The switch statement checking the
> time format had no default case which would cause 'n' to be
> uninitialized. Its an impossible condition least with the
> current code base, but it doesn't hurt to add a default to
> future proof.
>
All applied, thanks.
Regards,
-Denis
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-12-17 17:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-02 14:54 [PATCH v2 1/4] monitor: add --time-format,-t option James Prestwood
2024-12-02 14:54 ` [PATCH v2 2/4] monitor: track current PCAP size James Prestwood
2024-12-02 14:54 ` [PATCH v2 3/4] monitor: add support for limiting PCAP size/count James Prestwood
2024-12-02 14:54 ` [PATCH v2 4/4] monitor: add --pcap-size,--pcap-count James Prestwood
2024-12-17 17:29 ` [PATCH v2 1/4] monitor: add --time-format,-t option Denis Kenzior
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox