git: 65768d03cdf6 - stable/13 - routing: improve multiline debug

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Fri, 13 Jan 2023 21:25:50 UTC
The branch stable/13 has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=65768d03cdf65a48129c35c1dbb851d480c7aeac

commit 65768d03cdf65a48129c35c1dbb851d480c7aeac
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2022-08-29 15:14:49 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-01-13 21:24:12 +0000

    routing: improve multiline debug
    
    Add IF_DEBUG_LEVEL() macro to ensure all debug output preparation
     is run only if the current debug level is sufficient. Consistently
     use it within routing subsystem.
    
    MFC after:      2 weeks
    
    (cherry picked from commit 578a99c939b6f7a774f399fc7b64ee956c4ae200)
---
 sys/net/route/nhgrp.c         | 25 +++++++++++--------------
 sys/net/route/nhgrp_ctl.c     | 12 ++++++------
 sys/net/route/nhop_ctl.c      | 23 +++++++++++------------
 sys/net/route/route_ctl.c     | 19 +++++++++----------
 sys/net/route/route_debug.h   |  3 +++
 sys/net/route/route_helpers.c | 34 +++++++++++++++++++---------------
 6 files changed, 59 insertions(+), 57 deletions(-)

diff --git a/sys/net/route/nhgrp.c b/sys/net/route/nhgrp.c
index 358e5d1eaace..de07eb76113f 100644
--- a/sys/net/route/nhgrp.c
+++ b/sys/net/route/nhgrp.c
@@ -179,13 +179,11 @@ link_nhgrp(struct nh_control *ctl, struct nhgrp_priv *grp_priv)
 
 	NHOPS_WUNLOCK(ctl);
 
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG2
-	{
-		char nhgrp_buf[NHOP_PRINT_BUFSIZE];
-		nhgrp_print_buf(grp_priv->nhg, nhgrp_buf, sizeof(nhgrp_buf));
-		FIB_RH_LOG(LOG_DEBUG2, ctl->ctl_rh, "linked %s", nhgrp_buf);
+	IF_DEBUG_LEVEL(LOG_DEBUG2) {
+		char nhgrp_buf[NHOP_PRINT_BUFSIZE] __unused;
+		FIB_RH_LOG(LOG_DEBUG2, ctl->ctl_rh, "linked %s",
+		    nhgrp_print_buf(grp_priv->nhg, nhgrp_buf, sizeof(nhgrp_buf)));
 	}
-#endif
 	consider_resize(ctl, new_num_buckets, new_num_items);
 
 	return (1);
@@ -214,14 +212,13 @@ unlink_nhgrp(struct nh_control *ctl, struct nhgrp_priv *key)
 
 	NHOPS_WUNLOCK(ctl);
 
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG2
-	{
+	IF_DEBUG_LEVEL(LOG_DEBUG2) {
 		char nhgrp_buf[NHOP_PRINT_BUFSIZE];
 		nhgrp_print_buf(nhg_priv_ret->nhg, nhgrp_buf, sizeof(nhgrp_buf));
 		FIB_RH_LOG(LOG_DEBUG2, ctl->ctl_rh, "unlinked idx#%d %s", idx,
 		    nhgrp_buf);
 	}
-#endif
+
 	return (nhg_priv_ret);
 }
 
@@ -340,11 +337,11 @@ nhgrp_ctl_unlink_all(struct nh_control *ctl)
 	NHOPS_WLOCK_ASSERT(ctl);
 
 	CHT_SLIST_FOREACH(&ctl->gr_head, mpath, nhg_priv) {
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG
-		char nhgbuf[NHOP_PRINT_BUFSIZE];
-		FIB_RH_LOG(LOG_DEBUG, ctl->ctl_rh, "marking %s unlinked",
-		    nhgrp_print_buf(nhg_priv->nhg, nhgbuf, sizeof(nhgbuf)));
-#endif
+		IF_DEBUG_LEVEL(LOG_DEBUG2) {
+			char nhgbuf[NHOP_PRINT_BUFSIZE] __unused;
+			FIB_RH_LOG(LOG_DEBUG2, ctl->ctl_rh, "marking %s unlinked",
+			    nhgrp_print_buf(nhg_priv->nhg, nhgbuf, sizeof(nhgbuf)));
+		}
 		refcount_release(&nhg_priv->nhg_linked);
 	} CHT_SLIST_FOREACH_END;
 }
diff --git a/sys/net/route/nhgrp_ctl.c b/sys/net/route/nhgrp_ctl.c
index 90cd5b1e45a0..ca9e01ed0077 100644
--- a/sys/net/route/nhgrp_ctl.c
+++ b/sys/net/route/nhgrp_ctl.c
@@ -406,12 +406,12 @@ destroy_nhgrp(struct nhgrp_priv *nhg_priv)
 	KASSERT((nhg_priv->nhg_refcount == 0), ("nhg_refcount != 0"));
 	KASSERT((nhg_priv->nhg_idx == 0), ("gr_idx != 0"));
 
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG
-	char nhgbuf[NHOP_PRINT_BUFSIZE];
-	FIB_NH_LOG(LOG_DEBUG, nhg_priv->nhg_nh_weights[0].nh,
-	    "destroying %s", nhgrp_print_buf(nhg_priv->nhg,
-	    nhgbuf, sizeof(nhgbuf)));
-#endif
+	IF_DEBUG_LEVEL(LOG_DEBUG2) {
+		char nhgbuf[NHOP_PRINT_BUFSIZE] __unused;
+		FIB_NH_LOG(LOG_DEBUG2, nhg_priv->nhg_nh_weights[0].nh,
+		    "destroying %s", nhgrp_print_buf(nhg_priv->nhg,
+		    nhgbuf, sizeof(nhgbuf)));
+	}
 
 	free_nhgrp_nhops(nhg_priv);
 	destroy_nhgrp_int(nhg_priv);
diff --git a/sys/net/route/nhop_ctl.c b/sys/net/route/nhop_ctl.c
index 263a71c1322e..e4195d106d01 100644
--- a/sys/net/route/nhop_ctl.c
+++ b/sys/net/route/nhop_ctl.c
@@ -534,10 +534,11 @@ finalize_nhop(struct nh_control *ctl, struct nhop_object *nh, bool link)
 		return (ENOBUFS);
 	}
 
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG
-	char nhbuf[NHOP_PRINT_BUFSIZE];
-	FIB_NH_LOG(LOG_DEBUG, nh, "finalized: %s", nhop_print_buf(nh, nhbuf, sizeof(nhbuf)));
-#endif
+	IF_DEBUG_LEVEL(LOG_DEBUG) {
+		char nhbuf[NHOP_PRINT_BUFSIZE] __unused;
+		FIB_NH_LOG(LOG_DEBUG, nh, "finalized: %s",
+		    nhop_print_buf(nh, nhbuf, sizeof(nhbuf)));
+	}
 
 	return (0);
 }
@@ -598,10 +599,11 @@ nhop_free(struct nhop_object *nh)
 		return;
 	}
 
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG
-	char nhbuf[NHOP_PRINT_BUFSIZE];
-	FIB_NH_LOG(LOG_DEBUG, nh, "deleting %s", nhop_print_buf(nh, nhbuf, sizeof(nhbuf)));
-#endif
+	IF_DEBUG_LEVEL(LOG_DEBUG) {
+		char nhbuf[NHOP_PRINT_BUFSIZE] __unused;
+		FIB_NH_LOG(LOG_DEBUG, nh, "deleting %s",
+		    nhop_print_buf(nh, nhbuf, sizeof(nhbuf)));
+	}
 
 	/*
 	 * There are only 2 places, where nh_linked can be decreased:
@@ -1189,10 +1191,7 @@ nhops_dump_sysctl(struct rib_head *rh, struct sysctl_req *w)
 	ctl = rh->nh_control;
 
 	NHOPS_RLOCK(ctl);
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG
-	FIB_LOG(LOG_DEBUG, rh->rib_fibnum, rh->rib_family, "dump %u items",
-	    ctl->nh_head.items_count);
-#endif
+	FIB_RH_LOG(LOG_DEBUG, rh, "dump %u items", ctl->nh_head.items_count);
 	CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) {
 		error = dump_nhop_entry(rh, nh_priv->nh, w);
 		if (error != 0) {
diff --git a/sys/net/route/route_ctl.c b/sys/net/route/route_ctl.c
index 4b7572ce7980..a9ea2ad49103 100644
--- a/sys/net/route/route_ctl.c
+++ b/sys/net/route/route_ctl.c
@@ -609,13 +609,13 @@ rib_copy_route(struct rtentry *rt, const struct route_nhop_data *rnd_src,
 
 	MPASS((nh_src->nh_flags & NHF_MULTIPATH) == 0);
 
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG2
+	IF_DEBUG_LEVEL(LOG_DEBUG2) {
 		char nhbuf[NHOP_PRINT_BUFSIZE], rtbuf[NHOP_PRINT_BUFSIZE];
 		nhop_print_buf_any(nh_src, nhbuf, sizeof(nhbuf));
 		rt_print_buf(rt, rtbuf, sizeof(rtbuf));
 		FIB_RH_LOG(LOG_DEBUG2, rh_dst, "copying %s -> %s from fib %u",
 		    rtbuf, nhbuf, nhop_get_fibnum(nh_src));
-#endif
+	}
 	struct nhop_object *nh = nhop_alloc(rh_dst->rib_fibnum, rh_dst->rib_family);
 	if (nh == NULL) {
 		FIB_RH_LOG(LOG_INFO, rh_dst, "unable to allocate new nexthop");
@@ -645,11 +645,12 @@ rib_copy_route(struct rtentry *rt, const struct route_nhop_data *rnd_src,
 	error = add_route_flags(rh_dst, rt_new, &rnd, op_flags, rc);
 
 	if (error != 0) {
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG
-		char buf[NHOP_PRINT_BUFSIZE];
-		rt_print_buf(rt_new, buf, sizeof(buf));
-		FIB_RH_LOG(LOG_DEBUG, rh_dst, "Unable to add route %s: error %d", buf, error);
-#endif
+		IF_DEBUG_LEVEL(LOG_DEBUG2) {
+			char buf[NHOP_PRINT_BUFSIZE];
+			rt_print_buf(rt_new, buf, sizeof(buf));
+			FIB_RH_LOG(LOG_DEBUG, rh_dst,
+			    "Unable to add route %s: error %d", buf, error);
+		}
 		nhop_free(nh);
 		rt_free_immediate(rt_new);
 	}
@@ -1295,15 +1296,13 @@ change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
 	struct rtentry *rt_new;
 	int error = 0;
 
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG2
-	{
+	IF_DEBUG_LEVEL(LOG_DEBUG2) {
 		char buf_old[NHOP_PRINT_BUFSIZE], buf_new[NHOP_PRINT_BUFSIZE];
 		nhop_print_buf_any(rnd_orig->rnd_nhop, buf_old, NHOP_PRINT_BUFSIZE);
 		nhop_print_buf_any(rnd_new->rnd_nhop, buf_new, NHOP_PRINT_BUFSIZE);
 		FIB_LOG(LOG_DEBUG2, rnh->rib_fibnum, rnh->rib_family,
 		    "trying change %s -> %s", buf_old, buf_new);
 	}
-#endif
 	RIB_WLOCK(rnh);
 
 	struct route_nhop_data rnd;
diff --git a/sys/net/route/route_debug.h b/sys/net/route/route_debug.h
index de80bd1ea78b..b7c8c79f7ffa 100644
--- a/sys/net/route/route_debug.h
+++ b/sys/net/route/route_debug.h
@@ -83,6 +83,8 @@
 #define _output			printf
 #define	_DEBUG_PASS_MSG(_l)	(DEBUG_VAR_NAME >= (_l))
 
+#define	IF_DEBUG_LEVEL(_l)	if ((DEBUG_MAX_LEVEL >= (_l)) && (__predict_false(DEBUG_VAR_NAME >= (_l))))
+
 /*
  * Logging for events specific for particular family and fib
  * Example: [nhop_neigh] inet.0 find_lle: nhop nh#4/inet/vtnet0/10.0.0.1: mapped to lle NULL
@@ -155,6 +157,7 @@ struct nhgrp_object;
 struct llentry;
 struct nhop_neigh;
 struct rtentry;
+struct ifnet;
 
 #define	NHOP_PRINT_BUFSIZE	48
 char *nhop_print_buf(const struct nhop_object *nh, char *buf, size_t bufsize);
diff --git a/sys/net/route/route_helpers.c b/sys/net/route/route_helpers.c
index a9d21ebfb507..fe503db368ce 100644
--- a/sys/net/route/route_helpers.c
+++ b/sys/net/route/route_helpers.c
@@ -261,27 +261,33 @@ rib_lookup(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags,
 #ifdef ROUTE_MPATH
 static void
 notify_add(struct rib_cmd_info *rc, const struct weightened_nhop *wn_src,
-    route_notification_t *cb, void *cbdata) {
+    route_notification_t *cb, void *cbdata)
+{
 	rc->rc_nh_new = wn_src->nh;
 	rc->rc_nh_weight = wn_src->weight;
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG2
-	char nhbuf[NHOP_PRINT_BUFSIZE];
-	FIB_NH_LOG(LOG_DEBUG2, wn_src->nh, "RTM_ADD for %s @ w=%u",
-	    nhop_print_buf(wn_src->nh, nhbuf, sizeof(nhbuf)), wn_src->weight);
-#endif
+
+	IF_DEBUG_LEVEL(LOG_DEBUG2) {
+		char nhbuf[NHOP_PRINT_BUFSIZE] __unused;
+		FIB_NH_LOG(LOG_DEBUG2, wn_src->nh, "RTM_ADD for %s @ w=%u",
+		    nhop_print_buf(wn_src->nh, nhbuf, sizeof(nhbuf)),
+		    wn_src->weight);
+	}
 	cb(rc, cbdata);
 }
 
 static void
 notify_del(struct rib_cmd_info *rc, const struct weightened_nhop *wn_src,
-    route_notification_t *cb, void *cbdata) {
+    route_notification_t *cb, void *cbdata)
+{
 	rc->rc_nh_old = wn_src->nh;
 	rc->rc_nh_weight = wn_src->weight;
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG2
-	char nhbuf[NHOP_PRINT_BUFSIZE];
-	FIB_NH_LOG(LOG_DEBUG2, wn_src->nh, "RTM_DEL for %s @ w=%u",
-	    nhop_print_buf(wn_src->nh, nhbuf, sizeof(nhbuf)), wn_src->weight);
-#endif
+
+	IF_DEBUG_LEVEL(LOG_DEBUG2) {
+		char nhbuf[NHOP_PRINT_BUFSIZE] __unused;
+		FIB_NH_LOG(LOG_DEBUG2, wn_src->nh, "RTM_DEL for %s @ w=%u",
+		    nhop_print_buf(wn_src->nh, nhbuf, sizeof(nhbuf)),
+		    wn_src->weight);
+	}
 	cb(rc, cbdata);
 }
 
@@ -313,14 +319,12 @@ decompose_change_notification(struct rib_cmd_info *rc, route_notification_t *cb,
 		wn_new = &tmp;
 		num_new = 1;
 	}
-#if DEBUG_MAX_LEVEL >= LOG_DEBUG
-	{
+	IF_DEBUG_LEVEL(LOG_DEBUG) {
 		char buf_old[NHOP_PRINT_BUFSIZE], buf_new[NHOP_PRINT_BUFSIZE];
 		nhop_print_buf_any(rc->rc_nh_old, buf_old, NHOP_PRINT_BUFSIZE);
 		nhop_print_buf_any(rc->rc_nh_new, buf_new, NHOP_PRINT_BUFSIZE);
 		FIB_NH_LOG(LOG_DEBUG, wn_old[0].nh, "change %s -> %s", buf_old, buf_new);
 	}
-#endif
 
 	/* Use the fact that each @wn array is sorted */
 	/*