[PATCH iproute2-next 3/5] netshaper: Extend show output with parent, bw-min and weight

Mohsin Bashir
2026-05-01T01:16:24Z

Extend print_netshaper_attrs() to display parent scope/id, bw-min,
and weight fields in the show output. Replace the switch-based
iteration with direct attribute checks for cleaner output formatting.
Verify the support by setting the rate for a queue and attempt to read it
back.

Before:
./netshaper set dev eth2 handle scope queue id 0 bw-min 10mbit weight 5
./netshaper show dev eth2 handle scope queue id 0
scope: queue
id: 0
dev: eth2

After:
./netshaper set dev eth2 handle scope queue id 0 bw-min 10mbit weight 5
./netshaper show dev eth2 handle scope queue id 0
dev: eth2 scope queue id 1 parent scope netdev bw-min 10 mbps weight 5

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Mohsin Bashir <mohsin.bashr@gmail.com>
---
netshaper/netshaper.c | 89 ++++++++++++++++++++++++++-----------------
1 file changed, 55 insertions(+), 34 deletions(-)

diff --git a/netshaper/netshaper.c b/netshaper/netshaper.c
index 4ee30525..53a5eae3 100644
--- a/netshaper/netshaper.c
+++ b/netshaper/netshaper.c
@@ -72,53 +72,74 @@ static int parse_rate(const char *str, __u64 *rate_bps)

static void print_netshaper_attrs(struct nlmsghdr *answer)
{
+ struct rtattr *parent_tb[NET_SHAPER_A_HANDLE_MAX + 1] = {};
struct genlmsghdr *ghdr = NLMSG_DATA(answer);
int len = answer->nlmsg_len - NLMSG_LENGTH(GENL_HDRLEN);
struct rtattr *tb[NET_SHAPER_A_MAX + 1] = {};
struct rtattr *handle_tb[NET_SHAPER_A_HANDLE_MAX + 1] = {};
- __u32 bw_max_mbps, scope, id;
- __u64 bw_max_bps;
+ __u32 scope, id;
int ifindex;

parse_rtattr_flags(tb, NET_SHAPER_A_MAX,
(struct rtattr *)((char *)ghdr + GENL_HDRLEN),
len, NLA_F_NESTED);

- for (int i = 1; i <= NET_SHAPER_A_MAX; ++i) {
- if (!tb[i])
- continue;
- switch (i) {
- case NET_SHAPER_A_BW_MAX:
- bw_max_bps = rta_getattr_uint(tb[i]);
- bw_max_mbps = (bw_max_bps / 1000000);
+ if (tb[NET_SHAPER_A_IFINDEX]) {
+ ifindex = rta_getattr_u32(tb[NET_SHAPER_A_IFINDEX]);
+ print_color_string(PRINT_ANY, COLOR_IFNAME, "dev",
+ "dev: %s ", ll_index_to_name(ifindex));
+ }

- print_uint(PRINT_ANY, "bw-max", "bw-max: %u mbps\n",
- bw_max_mbps);
- break;
- case NET_SHAPER_A_IFINDEX:
- ifindex = rta_getattr_u32(tb[i]);
- print_color_string(PRINT_ANY, COLOR_IFNAME, "dev",
- "dev: %s\n",
- ll_index_to_name(ifindex));
- break;
- case NET_SHAPER_A_HANDLE:
- parse_rtattr_nested(handle_tb, NET_SHAPER_A_HANDLE_MAX,
- tb[NET_SHAPER_A_HANDLE]);
- if (handle_tb[NET_SHAPER_A_HANDLE_SCOPE]) {
- scope = rta_getattr_u32(handle_tb[NET_SHAPER_A_HANDLE_SCOPE]);
- print_string(PRINT_ANY, "scope",
- "scope: %s\n",
- net_shaper_scope_names[scope]);
- }
- if (handle_tb[NET_SHAPER_A_HANDLE_ID]) {
- id = rta_getattr_u32(handle_tb[NET_SHAPER_A_HANDLE_ID]);
- print_uint(PRINT_ANY, "id", "id: %u\n", id);
- }
- break;
- default:
- break;
+ if (tb[NET_SHAPER_A_HANDLE]) {
+ parse_rtattr_nested(handle_tb, NET_SHAPER_A_HANDLE_MAX,
+ tb[NET_SHAPER_A_HANDLE]);
+ if (handle_tb[NET_SHAPER_A_HANDLE_SCOPE]) {
+ scope = rta_getattr_u32(handle_tb[NET_SHAPER_A_HANDLE_SCOPE]);
+ print_string(PRINT_ANY, "scope", "scope %s ",
+ net_shaper_scope_names[scope]);
+ }
+ if (handle_tb[NET_SHAPER_A_HANDLE_ID]) {
+ id = rta_getattr_u32(handle_tb[NET_SHAPER_A_HANDLE_ID]);
+ print_uint(PRINT_ANY, "id", "id %u ", id);
+ }
+ }
+
+ if (tb[NET_SHAPER_A_PARENT]) {
+ parse_rtattr_nested(parent_tb, NET_SHAPER_A_HANDLE_MAX,
+ tb[NET_SHAPER_A_PARENT]);
+ if (parent_tb[NET_SHAPER_A_HANDLE_SCOPE]) {
+ scope = rta_getattr_u32(parent_tb[NET_SHAPER_A_HANDLE_SCOPE]);
+ print_string(PRINT_ANY, "parent-scope",
+ "parent scope %s ",
+ net_shaper_scope_names[scope]);
}
+ if (parent_tb[NET_SHAPER_A_HANDLE_ID]) {
+ id = rta_getattr_u32(parent_tb[NET_SHAPER_A_HANDLE_ID]);
+ print_uint(PRINT_ANY, "parent-id", "id %u ", id);
+ }
+ }
+
+ if (tb[NET_SHAPER_A_BW_MAX]) {
+ __u64 bw = rta_getattr_uint(tb[NET_SHAPER_A_BW_MAX]);
+
+ print_uint(PRINT_ANY, "bw-max", "bw-max %u mbps ",
+ (__u32)(bw / 1000000));
}
+
+ if (tb[NET_SHAPER_A_BW_MIN]) {
+ __u64 bw = rta_getattr_uint(tb[NET_SHAPER_A_BW_MIN]);
+
+ print_uint(PRINT_ANY, "bw-min", "bw-min %u mbps ",
+ (__u32)(bw / 1000000));
+ }
+
+ if (tb[NET_SHAPER_A_WEIGHT]) {
+ __u32 weight = rta_getattr_u32(tb[NET_SHAPER_A_WEIGHT]);
+
+ print_uint(PRINT_ANY, "weight", "weight %u ", weight);
+ }
+
+ printf("\n");
}

static int do_cmd(int argc, char **argv, int cmd)
--
2.52.0