Subject: [PATCH iproute2-next 4/5] netshaper: Make handle id optional for node scope

From: Mohsin Bashir <[email-masked]>
Date: Thu, 30 Apr 2026 18:16:10 -0700

Make handle id optional for node scope so that omitting it tells
the kernel to create a new node and assign an id automatically,
while providing it updates an existing node. Queue scope still
requires an explicit id since it maps to a specific TX queue.
Only send the handle id in the netlink message when the user
explicitly provides one.

Signed-off-by: Jakub Kicinski <[email-masked]>
Signed-off-by: Mohsin Bashir <[email-masked]>
---
 netshaper/netshaper.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/netshaper/netshaper.c b/netshaper/netshaper.c
index 53a5eae3..fc68e735 100644
--- a/netshaper/netshaper.c
+++ b/netshaper/netshaper.c
@@ -152,6 +152,7 @@ static int do_cmd(int argc, char **argv, int cmd)
 	__u64 bw_max_bps = 0, bw_min_bps = 0;
 	__u32 handle_id = 0, weight = 0;
 	bool handle_present = false;
+	bool has_handle_id = false;
 	struct nlmsghdr *answer;
 	int err, ifindex = -1;
 
@@ -193,18 +194,8 @@ static int do_cmd(int argc, char **argv, int cmd)
 				return -1;
 			}
 
-			if (handle_scope == NET_SHAPER_SCOPE_NETDEV) {
-				/* For netdev scope, id is optional */
-				if (argc > 1 && strcmp(argv[1], "id") == 0) {
-					NEXT_ARG();
-					NEXT_ARG();
-					if (get_unsigned(&handle_id, *argv, 10)) {
-						fprintf(stderr, "Invalid handle id\n");
-						return -1;
-					}
-				}
-			} else {
-				/* For queue/node scope, id is required */
+			if (handle_scope == NET_SHAPER_SCOPE_QUEUE) {
+				/* For queue scope, id is required */
 				NEXT_ARG();
 				if (strcmp(*argv, "id") != 0) {
 					fprintf(stderr, "What is \"%s\"\n", *argv);
@@ -216,6 +207,16 @@ static int do_cmd(int argc, char **argv, int cmd)
 					fprintf(stderr, "Invalid handle id\n");
 					return -1;
 				}
+				has_handle_id = true;
+			} else if (argc > 1 && strcmp(argv[1], "id") == 0) {
+				/* For netdev and node scope, id is optional */
+				NEXT_ARG();
+				NEXT_ARG();
+				if (get_unsigned(&handle_id, *argv, 10)) {
+					fprintf(stderr, "Invalid handle id\n");
+					return -1;
+				}
+				has_handle_id = true;
 			}
 		} else {
 			fprintf(stderr, "What is \"%s\"\n", *argv);
@@ -240,7 +241,8 @@ static int do_cmd(int argc, char **argv, int cmd)
 	struct rtattr *handle = addattr_nest(&req.n, sizeof(req),
 					     NET_SHAPER_A_HANDLE | NLA_F_NESTED);
 	addattr32(&req.n, sizeof(req), NET_SHAPER_A_HANDLE_SCOPE, handle_scope);
-	addattr32(&req.n, sizeof(req), NET_SHAPER_A_HANDLE_ID, handle_id);
+	if (has_handle_id)
+		addattr32(&req.n, sizeof(req), NET_SHAPER_A_HANDLE_ID, handle_id);
 	addattr_nest_end(&req.n, handle);
 
 	if (has_bw_max)
-- 
2.52.0