public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Anshu Kumari <anskuma@redhat.com>
To: passt-dev@passt.top
Cc: Anshu Kumari <anskuma@redhat.com>
Subject: [PATCH v2] Bug 134: message rate limiting
Date: Tue, 24 Mar 2026 22:23:01 +0530	[thread overview]
Message-ID: <20260324165300.86066-2-anskuma@redhat.com> (raw)

Signed-off-by: Anshu Kumari <anskuma@redhat.com>
---
 log.h | 40 ++++++++++++++++++++++++++++++++++++++++
 tap.c | 19 ++++++-------------
 2 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/log.h b/log.h
index 6ceb686..2e9286e 100644
--- a/log.h
+++ b/log.h
@@ -48,6 +48,46 @@ void logmsg_perror(int pri, const char *format, ...)
 		passt_exit(EXIT_FAILURE);				\
 	} while (0)
 
+#define LOG_RATELIMIT_BURST	5  /* Max messages per window per call site */
+#define LOG_RATELIMIT_INTERVAL	1  /* Default rate limit window in seconds */
+
+/**
+ * logmsg_ratelimit() - Rate-limited log message
+ * @fn:		Logging function
+ * @now:	current timestamp
+ */
+#define logmsg_ratelimit(fn, now, ...)				\
+	do {								\
+		static time_t rl_last_;					\
+		static unsigned int rl_printed_;			\
+		static unsigned int rl_suppressed_;			\
+									\
+		if ((now)->tv_sec - rl_last_ > LOG_RATELIMIT_INTERVAL) {		\
+			if (rl_suppressed_)				\
+				fn("(suppressed %u similar messages)",	\
+				   rl_suppressed_);			\
+			rl_last_ = (now)->tv_sec;			\
+			rl_printed_ = 0;				\
+			rl_suppressed_ = 0;				\
+		}							\
+									\
+		if (rl_printed_ < LOG_RATELIMIT_BURST) {		\
+			fn(__VA_ARGS__);				\
+			rl_printed_++;					\
+		} else {						\
+			rl_suppressed_++;				\
+		}							\
+	} while (0)
+
+#define err_ratelimit(now, ...)					\
+	logmsg_ratelimit(err, now, __VA_ARGS__)
+#define warn_ratelimit(now, ...)					\
+	logmsg_ratelimit(warn, now, __VA_ARGS__)
+#define info_ratelimit(now, ...)					\
+	logmsg_ratelimit(info, now, __VA_ARGS__)
+#define debug_ratelimit(now, ...)					\
+	logmsg_ratelimit(debug, now, __VA_ARGS__)
+
 extern int log_file;
 extern int log_trace;
 extern bool log_conf_parsed;
diff --git a/tap.c b/tap.c
index 1049e02..656b6e9 100644
--- a/tap.c
+++ b/tap.c
@@ -686,17 +686,8 @@ static bool tap4_is_fragment(const struct iphdr *iph,
 			     const struct timespec *now)
 {
 	if (ntohs(iph->frag_off) & ~IP_DF) {
-		/* Ratelimit messages */
-		static time_t last_message;
-		static unsigned num_dropped;
-
-		num_dropped++;
-		if (now->tv_sec - last_message > FRAGMENT_MSG_RATE) {
-			warn("Can't process IPv4 fragments (%u dropped)",
-			     num_dropped);
-			last_message = now->tv_sec;
-			num_dropped = 0;
-		}
+		warn_ratelimit(now,
+			"Can't process IPv4 fragment");
 		return true;
 	}
 	return false;
@@ -1115,8 +1106,10 @@ void tap_add_packet(struct ctx *c, struct iov_tail *data,
 		char bufmac[ETH_ADDRSTRLEN];
 
 		memcpy(c->guest_mac, eh->h_source, ETH_ALEN);
-		debug("New guest MAC address observed: %s",
-		      eth_ntop(c->guest_mac, bufmac, sizeof(bufmac)));
+		info_ratelimit(now,
+			       "New guest MAC address observed: %s",
+			       eth_ntop(c->guest_mac, bufmac,
+					sizeof(bufmac)));
 		proto_update_l2_buf(c->guest_mac);
 	}
 
-- 
2.53.0


             reply	other threads:[~2026-03-24 16:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-24 16:53 Anshu Kumari [this message]
2026-03-24 20:01 ` Stefano Brivio

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=20260324165300.86066-2-anskuma@redhat.com \
    --to=anskuma@redhat.com \
    --cc=passt-dev@passt.top \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).