From patchwork Mon Dec 2 14:54:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Prestwood X-Patchwork-Id: 13890862 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15D2720B1FD for ; Mon, 2 Dec 2024 14:54:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733151286; cv=none; b=OLFItuWZ+c4Hi2AivEmwfqeLpz+LZ35l4xkdcomyOyExrHJH1f3F5pzZs0y+gR51vi/QypZpUU4TxSi50FzK4uOJwo0RScQVwkbysDFE+cFW/RKFELVATYYkQoYFSrUStg4S4V13a7irc9n7eNacgDWfWIUJeSxedDLVkqDwhVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733151286; c=relaxed/simple; bh=BOOAaps/puV+PtSYg6BqLwyBzXyvgbr9zhUwlGQPdvE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=FYQxj/p68PHt995hU8b9Pic7e3NQK7NWY+GIbo+gcMALCpJACMNFuU/O/UJHIjVnu7TWkVktXAEop4qRIFIPejrYmrCaw+NoOz5ImV1Qnh8ALJpaiXH6qHFkEUz70dbmt5RmLxv1efWjRNOd1NOj2W9riHgptfIcoG9tqActIeo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GKQTiWhp; arc=none smtp.client-ip=209.85.215.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GKQTiWhp" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7fc22a88bcbso2997779a12.2 for ; Mon, 02 Dec 2024 06:54:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733151284; x=1733756084; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Is8hRQW7URqqlM0PBgArwZvRqVe4FOkBlMh6zAFt5a0=; b=GKQTiWhpspg4tQ4HaOaM6qXIBntCLgZ1tdU5XN8Do4DoV2ZP6yjzd1CE84tY+GCZ6Q +GyeLew6qbO5CXiLfmhjydF8YPTsrzMf5srnxLnKQ7mdyyjzmkR8DbXtfEv56vHBSPHj T29iGXDfSKvFZeFnP9R0TzPcvOIxLLSQRMvIhtBZcbli6Jh8VPdf3DYM/M4UWOPq60T9 cVr4mjfAADZmmeMG7PhZ1V7ibkt9+MpkEIWY4GOrGhfHy+Js3QCM1FqJEVuswj3aEGmi gq2U8FWSdCxfNjHRjpBOqMhjaF0Z4iCMgjvSYQh4nlrc82dXwglD406gv7Mq77AHGWMs u2jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733151284; x=1733756084; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Is8hRQW7URqqlM0PBgArwZvRqVe4FOkBlMh6zAFt5a0=; b=tFXEzliZqUGwp0RAP/83ICevFmbUDxPIXFy/LAqjZDsBJm64mib9v2ux4QOVE8Pu/P pwM3NYI8mGVIWBO7xg8nJnsLT/X3gXaEdJg2S0nyWOCGjYfUtlt0UirZx0F6ZECPN14s WYxrtZzdgAuVXjBF2/nNDaChbdYjBjy1vDwNNpSufAvf42wMFf8vOL7Q+T43lanZBU+Y howlNfk9Uqn5UliTAf0epPnvDcaujUZdy3IPlBr8R2MEcRCjB62ppeB7ewBA/rUoTRry XDJRxvisqzBJcNYvnk2omiEo2sRIOrFWz3rRdX6z/QEZpnUnWhYEFqjr7w/fcvBTyYKb XLDA== X-Gm-Message-State: AOJu0YweDltRYRxeolOThd9W19EVOqZtyA/t+urcC/h9/iscqmed/Dyl MNdImGTnyWImZTFFeaGxqstcXU/AxDzra/iA98f7k0uLPgOfb7ofkixTeg== X-Gm-Gg: ASbGncvSG9kVPLZEqF40HHCYW6IIAbQuEo34iKhnZXyywgYst/IrIictQRcOIaHcprx aczNFrZ9DxmSnXLjrYoNtH7j1laM33a9HZ78vR5+8YU0hrB/pRfHm3odIEATLzD4+DxZ4EJ64cp +2Lpg9MpmW012ajzNwB9fQS/VVyd6l8z4NAaaO0Xxtm2k1ZKOXkS0nN9r4DlxIzOh4JJJUDRyNq REMOM0NOUXqwj1/9bX5yUNLCk/8+mvhAAi4Lb/oLuI9lWry4UQjko9gN2KFDoQ8NlGn6YSK4e20 99kTN04H X-Google-Smtp-Source: AGHT+IGfBdAl4jAZZkeAUShIeRCVxW79XgvJI+hr+/Op75OAnJO1ewBlGhxte+aUx63pVuKsK2cJFw== X-Received: by 2002:a17:90b:28cd:b0:2ee:db8a:29f0 with SMTP id 98e67ed59e1d1-2eedb8a322amr3137464a91.27.1733151283996; Mon, 02 Dec 2024 06:54:43 -0800 (PST) Received: from LOCLAP699.locus-rst-dev-locuspark.locus ([152.193.78.90]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ee6cec695csm5592474a91.2.2024.12.02.06.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Dec 2024 06:54:43 -0800 (PST) From: James Prestwood To: iwd@lists.linux.dev Cc: James Prestwood Subject: [PATCH v2 1/4] monitor: add --time-format,-t option Date: Mon, 2 Dec 2024 06:54:35 -0800 Message-Id: <20241202145438.282998-1-prestwoj@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Read netlink PCAP trace file\n" - "\t-w, --write Write netlink PCAP trace file\n" - "\t-a, --analyze Analyze netlink PCAP trace file\n" - "\t-i, --interface 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 Read netlink PCAP trace file\n" + "\t-w, --write Write netlink PCAP trace file\n" + "\t-a, --analyze Analyze netlink PCAP trace file\n" + "\t-i, --interface 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 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 #include #include +#include #include #include #include @@ -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, From patchwork Mon Dec 2 14:54:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Prestwood X-Patchwork-Id: 13890863 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9EA2520B202 for ; Mon, 2 Dec 2024 14:54:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733151288; cv=none; b=s1u0Ssm5ohF2d4U1xXJRNRMqiygI8G9fUP7MjamxpJHz6Lk93r3OgRza62HHtOr/4waPvHg/xUBeAuuGL5Hf2pJvxdBCaYh7n+Rwz8Lc7WRl52ZzQZhkGBau2pwzwRTe7yyBsBtNTqrtJfKAPCg3wsEUPQ1ymsfmsU2ADvPtKQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733151288; c=relaxed/simple; bh=2Zi4/vqFtxByKs/3VxDoJnY99JyExLX1mp2WF7lSBr8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TbPVWbtZSzZuj5X0tw+k/ClcN2iG59tKP7pXDcO1fNghBrsQGJfap2kP9KJ7WL5Nh+plhWzueboLzBPg2v1eP4gDdsriaerhKjUY8ZFZcvC+7iwBhwWTpNLOfI0rG29Gp9xiYMkF5p2qh69vUgxI7NuSAIyHorwtLuRTv16ybD4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=N0KsbNKE; arc=none smtp.client-ip=209.85.215.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="N0KsbNKE" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7ee11ff7210so3072996a12.1 for ; Mon, 02 Dec 2024 06:54:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733151285; x=1733756085; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NTf38Av7Y42HfeDckzjvAA0AbIrAstHrD8Hg1EQv/xY=; b=N0KsbNKEMreg4BcVWh2vjVVITsqPkrb7y/GEoUUe/MCrlnumoMt40/jiwrR9mkZRg9 +3uwbD0fEVCLXZKTLnGTBKfCxrfNKQAR1Ftx7uLRP3CTcwfuOMYqW9iOqMLgtd14WYGz CURtGFVbccJfSw8udtI2/xmvNApQ/hqDBugb54yjJvbnW+xq1XzGDkoVguPKz88928Gv 8yTYjwoHfSGPdwe/cieigj1XDVNj6hWyPpKpcHpzaplm6FbcJG3Y/+9JbT0mXVbwWGoo fxwIUm2mWmdHvLX3tOlSJOSoN87vmH4ZLRvjxl3JxghLjZxazUQbR5eNS3kThm6FLGdJ 54Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733151285; x=1733756085; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NTf38Av7Y42HfeDckzjvAA0AbIrAstHrD8Hg1EQv/xY=; b=aGM0Ay6PURiKliTrrvrDa1foVzPF2gjIBjnvTYvTL/eo2YaAEd21i1e6jpKpa2xc01 JI/Na1igHOnqiYvkbhNwQOsJuU+QGm1WnZNHMfcvFJP+hob3kMfo6wMzGl79gyvAiTeR CNkRkB0swi961nTPDdDSsLmPiCxMvfXD//q7obKP4aqSU74+jcy7vFH0FGnAQLqu164x G7ICEUEI10nYKjrjbae3ViiTgd3KHgamaLq5YhKWSW325yUw0ZBWurp0xI25LF4Za1go izrBJIZuZ9dAacsoovdlmoIBbtx2Hfd66qL/TaRP+A3ptAiFJiC2+gqpbjPcjPGXq+6D jXtQ== X-Gm-Message-State: AOJu0YwNwXwm8shtWPdlXLaYZePIErSSDPtOxGJ3MpaktRXhj+b9TF3B AE7xlytG5JbJvxSYTGY9pIlDZYhoqLsxoaQ7MNv6jkT/cy+TI/QPGzBqmA== X-Gm-Gg: ASbGnctU9y8EQOFC1bhqYQGnyDd18oxI/++SXiLAxP+kNwH1+aRPaQfKLwYn2bkf4fk exy0cjOEVoY6G1LiSfzqypHwhVsNmciEMnhl42zGpQ8yQWFPGyUGcwKpGk2i5wHdM4M6B66SsIs k6NdlyQO93gqJUWX8NKbiu/k/Zn89BYSVhB+V5ZONci9+BTPSFJqm4/hi2SKLbknacZVJ+vvVFB SnVDv+U6DMNgFcwXAVguEP1DG0ydRpeWkjX125RZc7F/K4dG5s7Tzek9iGiGXy1jbjdFA/TBU3K 2NOfPchg X-Google-Smtp-Source: AGHT+IF/xdUxoejCZZnUiPqJ8UOE+m/5QJ+22Yr06T8Vm0fEfQE4XRtDymdDmgIAlZSG2CkzKlwy2A== X-Received: by 2002:a17:90b:5289:b0:2ee:a127:ba8b with SMTP id 98e67ed59e1d1-2eea127bc24mr9125998a91.36.1733151285528; Mon, 02 Dec 2024 06:54:45 -0800 (PST) Received: from LOCLAP699.locus-rst-dev-locuspark.locus ([152.193.78.90]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ee6cec695csm5592474a91.2.2024.12.02.06.54.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Dec 2024 06:54:45 -0800 (PST) From: James Prestwood To: iwd@lists.linux.dev Cc: James Prestwood Subject: [PATCH v2 2/4] monitor: track current PCAP size Date: Mon, 2 Dec 2024 06:54:36 -0800 Message-Id: <20241202145438.282998-2-prestwoj@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241202145438.282998-1-prestwoj@gmail.com> References: <20241202145438.282998-1-prestwoj@gmail.com> Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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); From patchwork Mon Dec 2 14:54:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Prestwood X-Patchwork-Id: 13890864 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4202220B217 for ; Mon, 2 Dec 2024 14:54:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733151289; cv=none; b=Sv2hncQbHWEbqNRoVhg0EhJalCKavIsaEjjftcbAKpsnq9wm1iytL7ISXFu3Yj8DANw1/w11xb+aQ20VfnchyL4X1GD2WA2MxcUnY2HEdKy2gtmf+KAkr+ieVX0nMoEwJiTcuoQJiRRpi7aTblBvj5ZKVpAlHD1rYpu4J88aQrs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733151289; c=relaxed/simple; bh=VTrCOc1WSZfqyBtSpeXTozqMuQ2tDAiKFWDQR2WKKNM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QK794sxRW+IKih9962tXvCE0DNUKoznecWh98Xbe+UftqqHlGm91Qn1dFg/O2fzC4V7rTPBdXrGUjSXpzklHXTzPunyBgN4hrHKhDuPcyMxU1fKTywN6IAnQh2mgpE99HGAWzDtP7uE0MCO8Uq5dbUWY7/gC7cQIDtGugavMi+A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=g5V/XiUi; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="g5V/XiUi" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-724e6c53fe2so3194212b3a.3 for ; Mon, 02 Dec 2024 06:54:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733151287; x=1733756087; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fX188QrblHZasHUg7g7D9sJnCDY8rhoQ1PHYBCHf8eY=; b=g5V/XiUiKjY6QZZLKCukFf0MI6ooCjw4S3iWBfdOdRK3OKtMDQFHyoN9K8eEV/hWXp DCzq5XSdwrJT7YzZMcblRO9xMO5JhTdPz/KkghhRz4gW//0QWjve6FvCyF4CkwYIrYEU rDgjSATnYREmm653TBSZdyuwVZ/M0vC0r8P49pOEK5fE6VOwIPAXqxSa/WTA1wSrKw+X kj2XV5j+i9ODVyk3AM37AZcxX8FWWzrUqMoLsC2mDHgZ42FukG9Es0Ms2H0GZgfDkIbY vrkSG519sgtQNzVqllppQUaJdt0A9Fyj9s7LQ6FDcgQfMj4h4/AsxwSVVEnQS+FSR/4J gzSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733151287; x=1733756087; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fX188QrblHZasHUg7g7D9sJnCDY8rhoQ1PHYBCHf8eY=; b=fsJqUhL5GD8hDlo8Ob7byEY5EY2zW1FBJCzcmTerXiOXbFhvmgnvWrUw6BKMP8pcwV FAZoL+hk2KGe94+XZ4gEKyeP5qt+Jt+oI4oKTC+86E1Rq5o16eHKrZaHSVoC4HcpWUVG 1Fs1aC4nt6NjSBQQiTPXCQXPze4EiNhRmgi7G8lxjr65SJjP5aMUtg/n6Crv6XbYwLIy SfXPigZupogKLvuNfw+vBflcLbB/vFH7GoIysNDkQ8P1OeF5d3XPUUh8+rVfj9AY39D3 dycYRzGYS5sfZ7F0HjdIsEvmUqK8hczwyW5FGDkniAwjj9IV6DQgI/7JESKiaMN1UT3y mUOA== X-Gm-Message-State: AOJu0YzFVPL0iaF2BMMITcolUYhM2GUmkeI+WueIUf2N+U0iS7bk4CWZ Hw0bX/MG56bzW8HxOIZWHKaSjj3M2hs7PvYqDpRQ+YG+oqU9Se+EedLfrw== X-Gm-Gg: ASbGnctnIov3rkL8Np2+B8LiY4mUU24SgCs34xgYzyQX/tJ/Tav3YeNoNkxR/I5sb6l tQxOsvx2OSNoyLzmGiygoEyxdcdOVsRuIOK8XDwOIDuFrhvgZYmNt+7wzJ2F2TO30yNm+Zr58J3 ycDRaQRyulkyZXqthLAdvIC9kI+PvPv/iyGFrZkuidTmKTpu//1AvF9YEyE7lJmV5UUowCkFdfc PL/9mPNPkAnwgqL4/IXOXXYlKRiZRjgrXrd78wNfPREBCBO8wvkhGLMY/ccBHnPamOZ3kK5ZC8E QYXghBMg X-Google-Smtp-Source: AGHT+IEqFnYlvVkaYiBGD99DSLcTbf1hgV4f3lmDhQNt4fWE41nSGJGy9Nn707+Kt0D7OZ7OGHStoA== X-Received: by 2002:a17:90b:2f08:b0:2ee:dd79:e046 with SMTP id 98e67ed59e1d1-2eedd79eb01mr3414237a91.13.1733151287159; Mon, 02 Dec 2024 06:54:47 -0800 (PST) Received: from LOCLAP699.locus-rst-dev-locuspark.locus ([152.193.78.90]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ee6cec695csm5592474a91.2.2024.12.02.06.54.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Dec 2024 06:54:46 -0800 (PST) From: James Prestwood To: iwd@lists.linux.dev Cc: James Prestwood Subject: [PATCH v2 3/4] monitor: add support for limiting PCAP size/count Date: Mon, 2 Dec 2024 06:54:37 -0800 Message-Id: <20241202145438.282998-3-prestwoj@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241202145438.282998-1-prestwoj@gmail.com> References: <20241202145438.282998-1-prestwoj@gmail.com> Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 #include #include +#include #include #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, From patchwork Mon Dec 2 14:54:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Prestwood X-Patchwork-Id: 13890865 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F9CF20B7E4 for ; Mon, 2 Dec 2024 14:54:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733151291; cv=none; b=OBv5nWdJKw8iiGh9BNrSJziNKLGc04F+B3EvXB8VzNVDy6vauCRAqVVA1nSOKsPGBnfV2mWeUj+fkg2Jrkewy+xBjgQj80DsoS21USY/l7OUvPTJ1N3Fix3AngbzmOmXdJsS4iaGA4l1/Jlfwv9SaWaySOm907HMEwi83dIg0bE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733151291; c=relaxed/simple; bh=G63noPF0CmFLEGQEvGhXroPWXXsDVAPupvvbH1Aslh0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M13tcbrkWPdYgzt6m0b7Uc3MeA0IOxGWnsYsToyoV1KVCnfrRWYUXGOAl2Dny3BITdhU1HIDNsWCzksFO+olcWqEOxKQqQLqR7ZLyYv1HliYuL1tXLhbmX+G2HQCO2tiQzp1kWJt3KMuYR4sxJCYcq9pH/GqKd1iKWruR8/Kd14= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eqed2TmY; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eqed2TmY" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-7251331e756so4136456b3a.3 for ; Mon, 02 Dec 2024 06:54:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733151289; x=1733756089; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sqQbg3z767lkylc4fof21EsyCBDqgw3MsPqrXDPxyBU=; b=eqed2TmYdeBjXgM0BL3d+GBHvmWhV0MFidck8gn+fPAC7H2q7wYeC7Rh2m+YANLLbk ypPzEeKAR/qFb1LhGYov2aeA7InquYtNeA862R6UKkcz0O4/F8Hq4xGujrWlSRTzcljF 2LgepkkLas+95JVRvSNpUGNEjLRSGCCkn97/1e0uuKCMIcJRcav5KUZgdVjf2ekzvrdu A/gnVu+1JxWZgqcMN9+albfsyvDU9kQHqr8+Nb339iHZa/hfbtrtD5syg2LivugjufqF 48EJf/0TTlzhF6+GzAeKN7A5PasC/NBN4lteDufFZ0dF23AGx9bbbxOWKy8KKNkdZHhf rYuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733151289; x=1733756089; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sqQbg3z767lkylc4fof21EsyCBDqgw3MsPqrXDPxyBU=; b=OX1aCP6Uxft3SRSguzp7E4MYI4Fl0SXGH3VPG+bkokd6+6oJddSYP3uRMddW8TdPxE bNqcSoo6s7oFOxWJkU1NKUkMt/eGUDEWYuz0SelKD0oZe4LKytK8URrqRkN7Y0NhGiVW V7yDyOBeG8bxWx/DnVK03q6dGgsk1YWDIMg1oj+efTIm+He1Ujs1KajELf4U+A9ydLkc pQLgrqC5IAHhFYWtRbO07UU1CTtBjU508f/2Gt38PY6Q/k8xavvtQ0hmKDwYfeMUc657 vsa5DnNhwi4018KlIqrsjNuqRQhXJUARTe3OrEYe1SkXGGdCBui1Myfzsch/quRYsqgh RAWQ== X-Gm-Message-State: AOJu0YwOKXTkNKuBwATMt7jmNfK9Q++mSIQWjI+WKxMVnveVmbE57h1R ERzNeptjAObOvNOAe6R3PzXLvz2MX+H6uE6cdX111o1tkWD+3lw3SEtM7g== X-Gm-Gg: ASbGnctiehPcuK2UNGAfiefJa1PmyjEBvGGSJE8Tw5ilUcdY/GYKIXqcsO+DQ1yve/j 4DqjcpyTdm6TpXsJs6Ks93+nsNFiIAkCR4WO8KLX/NN4d+Q/Kk2wY2BtFhKxQ6LnTcpdS14dfj4 b6J5DSKqMAOKnQe1Xf6h5ClDAMh0N9xTKV78/oDkC5eCmbCjugigNc+HGfFdR3RUmGbp3FCslGF pcMh7789GInnHe34GtJPuW/kRXaoe84Si000mi11P4bgcfDxK7Wmpe1d2cn8M7KB7i7f/glqas8 isD6SQJJ X-Google-Smtp-Source: AGHT+IFOuGrzM5vTaSSSuufEgyK3XJFeXkuq3jdFTLrbdVqtQJESSh7SOMKDU9ebLQmTMza/XZ+g+g== X-Received: by 2002:a17:90b:35d0:b0:2ee:c059:7de3 with SMTP id 98e67ed59e1d1-2eec0598100mr6408553a91.18.1733151288638; Mon, 02 Dec 2024 06:54:48 -0800 (PST) Received: from LOCLAP699.locus-rst-dev-locuspark.locus ([152.193.78.90]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ee6cec695csm5592474a91.2.2024.12.02.06.54.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Dec 2024 06:54:48 -0800 (PST) From: James Prestwood To: iwd@lists.linux.dev Cc: James Prestwood Subject: [PATCH v2 4/4] monitor: add --pcap-size,--pcap-count Date: Mon, 2 Dec 2024 06:54:38 -0800 Message-Id: <20241202145438.282998-4-prestwoj@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241202145438.282998-1-prestwoj@gmail.com> References: <20241202145438.282998-1-prestwoj@gmail.com> Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 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);