Skip to contentSkip to conversation

iproute2: Add 'netshaper' command to 'ip link' for netdev shaping

PATCH Superseded Erni Sri Satya Vennela 3 participants 6 messages Jul 28, 2025 – Jul 29, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iproute2: Add 'netshaper' command to 'ip link' for netdev shaping

Add support for the netshaper Generic Netlink
family to iproute2. Introduce a new subcommand to ip link for configuring netshaper parameters directly from userspace.

This interface allows users to set shaping attributes (such as speed) which are passed to the kernel to perform the corresponding netshaper operation.

Example usage:
$ip link netshaper { set | get | delete } dev DEVNAME \
handle scope SCOPE id ID \
[ speed SPEED ]

Internally, this triggers a kernel call to apply the shaping configuration to the specified network device.

Currently, the tool supports the following functionalities:
- Setting speed in Mbps, enabling bandwidth clamping for a network device that support netshaper operations.
- Deleting the current configuration.
- Querying the existing configuration.

Additional netshaper operations will be integrated into the tool as per requirement.

This change enables easy and scriptable configuration of bandwidth shaping for devices that use the netshaper Netlink family.

Corresponding net-next patches:
1) https://lore.kernel.org/all/[email protected]/
2) https://lore.kernel.org/lkml/[email protected]/

---
 include/uapi/linux/netshaper.h |  92 +++++++++++++++++
 ip/Makefile                    |   2 +-
 ip/iplink.c                    |  12 +++
 ip/iplink_netshaper.c          | 180 +++++++++++++++++++++++++++++++++
 4 files changed, 285 insertions(+), 1 deletion(-)
 create mode 100644 include/uapi/linux/netshaper.h
 create mode 100644 ip/iplink_netshaper.c
include/uapi/linux/netshaper.h new file +92 -0
new file mode 100644
index 00000000..1b9c563f
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+
+#ifndef _UAPI_LINUX_NET_SHAPER_H
+#define _UAPI_LINUX_NET_SHAPER_H
+
+#define NET_SHAPER_FAMILY_NAME		"net-shaper"
+#define NET_SHAPER_FAMILY_VERSION	1
+
+/**
+ * enum net_shaper_scope - Defines the shaper @id interpretation.
+ * @NET_SHAPER_SCOPE_UNSPEC: The scope is not specified.
+ * @NET_SHAPER_SCOPE_NETDEV: The main shaper for the given network device.
+ * @NET_SHAPER_SCOPE_QUEUE: The shaper is attached to the given device queue,
+ *   the @id represents the queue number.
+ * @NET_SHAPER_SCOPE_NODE: The shaper allows grouping of queues or other node
+ *   shapers; can be nested in either @netdev shapers or other @node shapers,
+ *   allowing placement in any location of the scheduling tree, except leaves
+ *   and root.
+ */
+enum net_shaper_scope {
+	NET_SHAPER_SCOPE_UNSPEC,
+	NET_SHAPER_SCOPE_NETDEV,
+	NET_SHAPER_SCOPE_QUEUE,
+	NET_SHAPER_SCOPE_NODE,
+
+	/* private: */
+	__NET_SHAPER_SCOPE_MAX,
+	NET_SHAPER_SCOPE_MAX = (__NET_SHAPER_SCOPE_MAX - 1)
+};
+
+/**
+ * enum net_shaper_metric - Different metric supported by the shaper.
+ * @NET_SHAPER_METRIC_BPS: Shaper operates on a bits per second basis.
+ * @NET_SHAPER_METRIC_PPS: Shaper operates on a packets per second basis.
+ */
+enum net_shaper_metric {
+	NET_SHAPER_METRIC_BPS,
+	NET_SHAPER_METRIC_PPS,
+};
+
+enum {
+	NET_SHAPER_A_HANDLE = 1,
+	NET_SHAPER_A_METRIC,
+	NET_SHAPER_A_BW_MIN,
+	NET_SHAPER_A_BW_MAX,
+	NET_SHAPER_A_BURST,
+	NET_SHAPER_A_PRIORITY,
+	NET_SHAPER_A_WEIGHT,
+	NET_SHAPER_A_IFINDEX,
+	NET_SHAPER_A_PARENT,
+	NET_SHAPER_A_LEAVES,
+
+	__NET_SHAPER_A_MAX,
+	NET_SHAPER_A_MAX = (__NET_SHAPER_A_MAX - 1)
+};
+
+enum {
+	NET_SHAPER_A_HANDLE_SCOPE = 1,
+	NET_SHAPER_A_HANDLE_ID,
+
+	__NET_SHAPER_A_HANDLE_MAX,
+	NET_SHAPER_A_HANDLE_MAX = (__NET_SHAPER_A_HANDLE_MAX - 1)
+};
+
+enum {
+	NET_SHAPER_A_CAPS_IFINDEX = 1,
+	NET_SHAPER_A_CAPS_SCOPE,
+	NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS,
+	NET_SHAPER_A_CAPS_SUPPORT_METRIC_PPS,
+	NET_SHAPER_A_CAPS_SUPPORT_NESTING,
+	NET_SHAPER_A_CAPS_SUPPORT_BW_MIN,
+	NET_SHAPER_A_CAPS_SUPPORT_BW_MAX,
+	NET_SHAPER_A_CAPS_SUPPORT_BURST,
+	NET_SHAPER_A_CAPS_SUPPORT_PRIORITY,
+	NET_SHAPER_A_CAPS_SUPPORT_WEIGHT,
+
+	__NET_SHAPER_A_CAPS_MAX,
+	NET_SHAPER_A_CAPS_MAX = (__NET_SHAPER_A_CAPS_MAX - 1)
+};
+
+enum {
+	NET_SHAPER_CMD_GET = 1,
+	NET_SHAPER_CMD_SET,
+	NET_SHAPER_CMD_DELETE,
+	NET_SHAPER_CMD_GROUP,
+	NET_SHAPER_CMD_CAP_GET,
+
+	__NET_SHAPER_CMD_MAX,
+	NET_SHAPER_CMD_MAX = (__NET_SHAPER_CMD_MAX - 1)
+};
+
+#endif /* _UAPI_LINUX_NET_SHAPER_H */
ip/Makefile +1 -1
index 3535ba78..18218c3b 100644
@@ -4,7 +4,7 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \
     ipmaddr.o ipmonitor.o ipmroute.o ipprefix.o iptuntap.o iptoken.o \
     ipxfrm.o xfrm_state.o xfrm_policy.o xfrm_monitor.o iplink_dummy.o \
     iplink_ifb.o iplink_nlmon.o iplink_team.o iplink_vcan.o iplink_vxcan.o \
-    iplink_vlan.o link_veth.o link_gre.o iplink_can.o iplink_xdp.o \
+    iplink_vlan.o iplink_netshaper.o link_veth.o link_gre.o iplink_can.o iplink_xdp.o \
     iplink_macvlan.o ipl2tp.o link_vti.o link_vti6.o link_xfrm.o \
     iplink_vxlan.o tcp_metrics.o iplink_ipoib.o ipnetconf.o link_ip6tnl.o \
     link_iptnl.o link_gre6.o iplink_bond.o iplink_bond_slave.o iplink_hsr.o \
ip/iplink.c +12 -0
index 59e8caf4..9da6e304 100644
@@ -1509,6 +1509,15 @@ static void do_help(int argc, char **argv)
 		usage();
 }
 
+static int iplink_netshaper(int argc, char **argv)
+{
+	struct link_util *lu;
+
+	lu = get_link_kind("netshaper");
+
+	return lu->parse_opt(lu, argc, argv, NULL);
+}
+
 int do_iplink(int argc, char **argv)
 {
 	if (argc < 1)
@@ -1545,6 +1554,9 @@ int do_iplink(int argc, char **argv)
 	if (matches(*argv, "property") == 0)
 		return iplink_prop(argc-1, argv+1);
 
+	if (matches(*argv, "netshaper") == 0)
+		return iplink_netshaper(argc-1, argv+1);
+
 	if (matches(*argv, "help") == 0) {
 		do_help(argc-1, argv+1);
 		return 0;
ip/iplink_netshaper.c new file +180 -0

180 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
Quoted 13 lines · On Mon, 28 Jul 2025 02:14:59 -0700 Erni Sri Satya Vennela…
ip/iplink_netshaper.c new file +13 -0
+
+static void print_netshaper_attrs(struct nlmsghdr *answer)
+{
+	struct genlmsghdr *ghdr = NLMSG_DATA(answer);
+	int len = answer->nlmsg_len - NLMSG_LENGTH(GENL_HDRLEN);
+	struct rtattr *tb[NET_SHAPER_A_MAX + 1] = {};
+	__u32 speed_bps, speed_mbps;
+	int ifindex;
+
+	parse_rtattr(tb, NET_SHAPER_A_MAX, (struct rtattr *)((char *)ghdr + GENL_HDRLEN), len);
+
+	for (int i = 1; i <= NET_SHAPER_A_MAX; ++i) {
+		if (!tb[i])

ip/iplink_netshaper.c line 56

Quoted 10 lines
ip/iplink_netshaper.c new file +10 -0
+			continue;
+		switch (i) {
+		case NET_SHAPER_A_BW_MAX:
+		speed_bps = rta_getattr_u32(tb[i]);
+		speed_mbps = (speed_bps / 1000000);
+		print_uint(PRINT_ANY, "speed", "Current speed (Mbps): %u\n", speed_mbps);
+		break;
+		case NET_SHAPER_A_IFINDEX:
+		ifindex = rta_getattr_u32(tb[i]);
+		print_string(PRINT_ANY, "dev", "Device Name: %s\n", ll_index_to_name(ifindex));

The display in print is supposed to correlate with command line args. Use color for devices if possible.

ip/iplink_netshaper.c line 62

Quoted 6 lines
ip/iplink_netshaper.c new file +6 -0
+		break;
+		default:
+		break;
+		}
+	}
+}

Indentation is a mess.

Iproute2 uses kernel coding style.
Suggest using a tool like clang-format to fix.

#2 lore raw
Quoted 21 lines · Stephen Hemminger
Quoted 18 lines · On Mon, 28 Jul 2025 02:14:59 -0700 Erni Sri Satya Vennela…
ip/iplink_netshaper.c new file +18 -0
+
+static void print_netshaper_attrs(struct nlmsghdr *answer)
+{
+	struct genlmsghdr *ghdr = NLMSG_DATA(answer);
+	int len = answer->nlmsg_len - NLMSG_LENGTH(GENL_HDRLEN);
+	struct rtattr *tb[NET_SHAPER_A_MAX + 1] = {};
+	__u32 speed_bps, speed_mbps;
+	int ifindex;
+
+	parse_rtattr(tb, NET_SHAPER_A_MAX, (struct rtattr *)((char *)ghdr + GENL_HDRLEN), len);
+
+	for (int i = 1; i <= NET_SHAPER_A_MAX; ++i) {
+		if (!tb[i])
+			continue;
+		switch (i) {
+		case NET_SHAPER_A_BW_MAX:
+		speed_bps = rta_getattr_u32(tb[i]);
+		speed_mbps = (speed_bps / 1000000);
Quoted 8 lines

ip/iplink_netshaper.c line 56

Quoted 5 lines
ip/iplink_netshaper.c new file +5 -0
+		print_uint(PRINT_ANY, "speed", "Current speed (Mbps): %u\n", speed_mbps);
+		break;
+		case NET_SHAPER_A_IFINDEX:
+		ifindex = rta_getattr_u32(tb[i]);
+		print_string(PRINT_ANY, "dev", "Device Name: %s\n", ll_index_to_name(ifindex));

The display in print is supposed to correlate with command line args. Use color for devices if possible.

Thankyou for the pointers Stephen. I will make sure to integrate this in the next version.

Quoted 10 lines

ip/iplink_netshaper.c line 62

Quoted 7 lines
ip/iplink_netshaper.c new file +6 -0
+		break;
+		default:
+		break;
+		}
+	}
+}

Indentation is a mess.

Quoted 2 lines

Iproute2 uses kernel coding style.
Suggest using a tool like clang-format to fix.

Sure, I'll use this tool to correct issues with indentation. Thankyou.

#3 lore raw
In reply to Erni Sri Satya Vennela
Quoted 28 lines · Erni Sri Satya Vennela

Add support for the netshaper Generic Netlink
family to iproute2. Introduce a new subcommand to ip link for configuring netshaper parameters directly from userspace.

This interface allows users to set shaping attributes (such as speed) which are passed to the kernel to perform the corresponding netshaper operation.

Example usage:
$ip link netshaper { set | get | delete } dev DEVNAME \
handle scope SCOPE id ID \
[ speed SPEED ]

Internally, this triggers a kernel call to apply the shaping configuration to the specified network device.

Currently, the tool supports the following functionalities:
- Setting speed in Mbps, enabling bandwidth clamping for a network device that support netshaper operations.
- Deleting the current configuration.
- Querying the existing configuration.

Additional netshaper operations will be integrated into the tool as per requirement.

This change enables easy and scriptable configuration of bandwidth shaping for devices that use the netshaper Netlink family.

Quoted 7 lines

Corresponding net-next patches:
1) https://lore.kernel.org/all/[email protected]/
2) https://lore.kernel.org/lkml/[email protected]/

---
 include/uapi/linux/netshaper.h |  92 +++++++++++++++++

the file in the kernel tree is net_shaper.h? drop it from the patch and ask for it to be added to the uapi files when posting the next version.

#4 lore raw
Quoted 33 lines · David Ahern
Quoted 32 lines · Erni Sri Satya Vennela

Add support for the netshaper Generic Netlink
family to iproute2. Introduce a new subcommand to ip link for configuring netshaper parameters directly from userspace.

This interface allows users to set shaping attributes (such as speed) which are passed to the kernel to perform the corresponding netshaper operation.

Example usage:
$ip link netshaper { set | get | delete } dev DEVNAME \
handle scope SCOPE id ID \
[ speed SPEED ]

Internally, this triggers a kernel call to apply the shaping configuration to the specified network device.

Currently, the tool supports the following functionalities:
- Setting speed in Mbps, enabling bandwidth clamping for a network device that support netshaper operations.
- Deleting the current configuration.
- Querying the existing configuration.

Additional netshaper operations will be integrated into the tool as per requirement.

This change enables easy and scriptable configuration of bandwidth shaping for devices that use the netshaper Netlink family.

Corresponding net-next patches:
1) https://lore.kernel.org/all/[email protected]/
2) https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
Quoted 3 lines
---
 include/uapi/linux/netshaper.h |  92 +++++++++++++++++

the file in the kernel tree is net_shaper.h? drop it from the patch and ask for it to be added to the uapi files when posting the next version.

Yes, netshaper.h is same uapi file as net_shaper.h in kernel tree. I'll remove this file in my next version.

Shall I mention about adding this file below the Signed-off-by section in the commit message or create a patch seperately for this file?

Thankyou.

#5 lore raw
Quoted 2 lines · Erni Sri Satya Vennela

Shall I mention about adding this file below the Signed-off-by section in the commit message or create a patch seperately for this file?

either option works