Skip to contentSkip to conversation

net: report multicast group user count

PATCH Applied Yuyang Huang 5 participants 12 messages Jun 30 – Jul 3

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover net: report multicast group user count

RTM_GETMULTICAST reports IPv4 and IPv6 multicast group membership, but does not include the per-group user count. Userspace therefore still has to parse /proc/net/igmp and /proc/net/igmp6 to obtain the Users column. In particular, this prevents iproute2 from moving "ip maddr show" entirely from procfs to rtnetlink.

Add IFA_MC_USERS to carry the user count in RTM_GETMULTICAST dumps and RTM_NEWMULTICAST / RTM_DELMULTICAST notifications for both address families. Update the rt-addr YNL specification and extend the rtnetlink selftest to verify that two joins increase the reported count by two.

Yuyang Huang (3):
net: ipv4: report multicast group user count
net: ipv6: report multicast group user count
selftests: net: check multicast group user count

 Documentation/netlink/specs/rt-addr.yaml |   4 +
 include/uapi/linux/if_addr.h             |   1 +
 net/ipv4/igmp.c                          |   2 +
 net/ipv6/addrconf.c                      |   1 +
 net/ipv6/mcast.c                         |   1 +
 tools/testing/selftests/net/rtnetlink.py | 101 ++++++++++++++++++++---
 6 files changed, 99 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 net: ipv4: report multicast group user count

RTM_GETMULTICAST has been part of the rtnetlink ABI for a long time and already reports IPv4 multicast group membership through IFA_MULTICAST and IFA_CACHEINFO. It does not report how many consumers hold each membership, so userspace still has to parse /proc/net/igmp to get the Users column.

Add IFA_MC_USERS as a u32 attribute carrying ip_mc_list::users in RTM_GETMULTICAST replies and entry-lifecycle notifications.

This gives iproute2 enough information to migrate the IPv4 part of "ip maddr show" from procfs parsing to rtnetlink.

---
 Documentation/netlink/specs/rt-addr.yaml | 4 ++++
 include/uapi/linux/if_addr.h             | 1 +
 net/ipv4/igmp.c                          | 2 ++
 3 files changed, 7 insertions(+)
Documentation/netlink/specs/rt-addr.yaml +4 -0
index 163a106c41bb..0ecbd24c890c 100644
@@ -123,6 +123,9 @@ attribute-sets:
       -
         name: proto
         type: u8
+      -
+        name: mc-users
+        type: u32
 
 
 operations:
@@ -176,6 +179,7 @@ operations:
           value: 58
           attributes: &mcaddr-attrs
             - multicast
+            - mc-users
             - cacheinfo
       dump:
         request:
include/uapi/linux/if_addr.h +1 -0
index aa7958b4e41d..7fb630b7fe31 100644
@@ -36,6 +36,7 @@ enum {
 	IFA_RT_PRIORITY,	/* u32, priority/metric for prefix route */
 	IFA_TARGET_NETNSID,
 	IFA_PROTO,		/* u8, address protocol */
+	IFA_MC_USERS,		/* u32, multicast group users */
 	__IFA_MAX,
 };
 
net/ipv4/igmp.c +2 -0
index b6337a47c141..116ce7cec80e 100644
@@ -1473,6 +1473,7 @@ int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev,
 	ci.ifa_valid = INFINITY_LIFE_TIME;
 
 	if (nla_put_in_addr(skb, IFA_MULTICAST, im->multiaddr) < 0 ||
+	    nla_put_u32(skb, IFA_MC_USERS, READ_ONCE(im->users)) < 0 ||
 	    nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci) < 0) {
 		nlmsg_cancel(skb, nlh);
 		return -EMSGSIZE;
@@ -1494,6 +1495,7 @@ static void inet_ifmcaddr_notify(struct net_device *dev,
 
 	skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
 			nla_total_size(sizeof(__be32)) +
+			nla_total_size(sizeof(u32)) +
 			nla_total_size(sizeof(struct ifa_cacheinfo)),
 			GFP_KERNEL);
 	if (!skb)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH net-next 1/3] net: ipv4: report multicast group user count

Quoted 12 lines · Yuyang Huang

RTM_GETMULTICAST has been part of the rtnetlink ABI for a long time and already reports IPv4 multicast group membership through IFA_MULTICAST and IFA_CACHEINFO. It does not report how many consumers hold each membership, so userspace still has to parse /proc/net/igmp to get the Users column.

Add IFA_MC_USERS as a u32 attribute carrying ip_mc_list::users in RTM_GETMULTICAST replies and entry-lifecycle notifications.

This gives iproute2 enough information to migrate the IPv4 part of "ip maddr show" from procfs parsing to rtnetlink.

Quoted 7 lines
---
  Documentation/netlink/specs/rt-addr.yaml | 4 ++++
  include/uapi/linux/if_addr.h             | 1 +
  net/ipv4/igmp.c                          | 2 ++
  3 files changed, 7 insertions(+)
maintainer Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH net-next 1/3] net: ipv4: report multicast group user count

In reply to Yuyang Huang
Quoted 6 lines · Yuyang Huang

RTM_GETMULTICAST has been part of the rtnetlink ABI for a long time and already reports IPv4 multicast group membership through IFA_MULTICAST and IFA_CACHEINFO. It does not report how many consumers hold each membership, so userspace still has to parse /proc/net/igmp to get the Users column.

Quoted 7 lines

Add IFA_MC_USERS as a u32 attribute carrying ip_mc_list::users in RTM_GETMULTICAST replies and entry-lifecycle notifications.

This gives iproute2 enough information to migrate the IPv4 part of "ip maddr show" from procfs parsing to rtnetlink.

#4 lore raw

PATCH 2/3 net: ipv6: report multicast group user count

In reply to Yuyang Huang

The previous patch added IFA_MC_USERS and emits it for IPv4 multicast groups. Add the same snapshot attribute to IPv6 RTM_GETMULTICAST replies and entry-lifecycle notifications, carrying ifmcaddr6::mca_users.

This makes the multicast rtnetlink ABI symmetric across IPv4 and IPv6 and gives userspace the same user count that /proc/net/igmp6 exposes.

---
 net/ipv6/addrconf.c | 1 +
 net/ipv6/mcast.c    | 1 +
 2 files changed, 2 insertions(+)
net/ipv6/addrconf.c +1 -0
index cbe681de3818..f1fe9ede1edb 100644
@@ -5264,6 +5264,7 @@ int inet6_fill_ifmcaddr(struct sk_buff *skb,
 
 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
+	    nla_put_u32(skb, IFA_MC_USERS, READ_ONCE(ifmca->mca_users)) < 0 ||
 	    put_cacheinfo(skb, ifmca->mca_cstamp, READ_ONCE(ifmca->mca_tstamp),
 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
 		nlmsg_cancel(skb, nlh);
net/ipv6/mcast.c +1 -0
index 04b811b3be97..774f4c72a6fa 100644
@@ -908,6 +908,7 @@ static void inet6_ifmcaddr_notify(struct net_device *dev,
 
 	skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
 			nla_total_size(sizeof(struct in6_addr)) +
+			nla_total_size(sizeof(u32)) +
 			nla_total_size(sizeof(struct ifa_cacheinfo)),
 			GFP_KERNEL);
 	if (!skb)
Signature · 1 line
2.43.0
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH net-next 2/3] net: ipv6: report multicast group user count

Quoted 9 lines · Yuyang Huang

The previous patch added IFA_MC_USERS and emits it for IPv4 multicast groups. Add the same snapshot attribute to IPv6 RTM_GETMULTICAST replies and entry-lifecycle notifications, carrying ifmcaddr6::mca_users.

This makes the multicast rtnetlink ABI symmetric across IPv4 and IPv6 and gives userspace the same user count that /proc/net/igmp6 exposes.

maintainer Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH net-next 2/3] net: ipv6: report multicast group user count

In reply to Yuyang Huang
Quoted 9 lines · Yuyang Huang

The previous patch added IFA_MC_USERS and emits it for IPv4 multicast groups. Add the same snapshot attribute to IPv6 RTM_GETMULTICAST replies and entry-lifecycle notifications, carrying ifmcaddr6::mca_users.

This makes the multicast rtnetlink ABI symmetric across IPv4 and IPv6 and gives userspace the same user count that /proc/net/igmp6 exposes.

#7 lore raw

PATCH 3/3 selftests: net: check multicast group user count

In reply to Yuyang Huang

Extend the RTM_GETMULTICAST dump test to verify IFA_MC_USERS for both IPv4 and IPv6 multicast groups.

Run each protocol test in a fresh network namespace to avoid changing host-network state or racing with unrelated multicast users. Join a fixed multicast group twice using separate sockets and check that the reported user count increases by two.

---
 tools/testing/selftests/net/rtnetlink.py | 101 ++++++++++++++++++++---
 1 file changed, 90 insertions(+), 11 deletions(-)
tools/testing/selftests/net/rtnetlink.py +90 -11
index 3622413d793d..0c67c7c00d84 100755
@@ -2,27 +2,106 @@
 # SPDX-License-Identifier: GPL-2.0
 
 import socket
+import struct
 import time
-from lib.py import bkg, ip, ksft_exit, ksft_run, ksft_ge, ksft_true, KsftSkipEx
+from lib.py import bkg, ip, ksft_exit, ksft_run, ksft_eq, ksft_ge, ksft_true, KsftSkipEx
 from lib.py import CmdExitFailure, NetNS, NetNSEnter, RtnlAddrFamily
 
 IPV4_ALL_HOSTS_MULTICAST = b'\xe0\x00\x00\x01'
+IPV4_TEST_MULTICAST = b'\xef\x01\x01\x01'
+IPV6_TEST_MULTICAST = bytes.fromhex('ff020000000000000000000000000123')
+
+
+def _users_for(rtnl: RtnlAddrFamily, family: int, grp: bytes, ifindex: int):
+    """Return mc-users for grp on ifindex, or 0 if absent."""
+
+    addrs = rtnl.getmulticast({"ifa-family": family}, dump=True)
+    matches = [addr for addr in addrs
+               if addr['multicast'] == grp and addr['ifa-index'] == ifindex]
+    if not matches:
+        return 0
+    if 'mc-users' not in matches[0]:
+        return None
+
+    return matches[0]['mc-users']
+
 
 def dump_mcaddr_check() -> None:
     """
-    Verify that at least one interface has the IPv4 all-hosts multicast address.
-    At least the loopback interface should have this address.
+    Verify IPv4 multicast addresses and their user counts in RTM_GETMULTICAST.
+    """
+
+    with NetNS() as ns:
+        with NetNSEnter(str(ns)):
+            ip("link set lo up")
+            rtnl = RtnlAddrFamily()
+            lo_idx = socket.if_nametoindex('lo')
+            addresses = rtnl.getmulticast({"ifa-family": socket.AF_INET}, dump=True)
+
+            all_host_multicasts = [
+                addr for addr in addresses
+                if addr['multicast'] == IPV4_ALL_HOSTS_MULTICAST
+            ]
+
+            ksft_ge(len(all_host_multicasts), 1,
+                    "No interface found with the IPv4 all-hosts multicast address")
+
+            mreq = IPV4_TEST_MULTICAST + socket.inet_aton('127.0.0.1')
+            before = _users_for(rtnl, socket.AF_INET, IPV4_TEST_MULTICAST, lo_idx)
+            if before is None:
+                raise KsftSkipEx("kernel does not expose IFA_MC_USERS")
+
+            s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+            s2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+            try:
+                s1.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
+                s2.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
+
+                after_join = _users_for(rtnl, socket.AF_INET,
+                                        IPV4_TEST_MULTICAST, lo_idx)
+                if after_join is None:
+                    raise KsftSkipEx("kernel does not expose IFA_MC_USERS")
+                ksft_eq(after_join - before, 2,
+                        f"users delta != 2 after two joins "
+                        f"(before={before}, after={after_join})")
+            finally:
+                s1.close()
+                s2.close()
+
+
+def dump_mcaddr6_check() -> None:
+    """
+    Verify IPv6 multicast addresses and their user counts in RTM_GETMULTICAST.
     """
 
-    rtnl = RtnlAddrFamily()
-    addresses = rtnl.getmulticast({"ifa-family": socket.AF_INET}, dump=True)
+    with NetNS() as ns:
+        with NetNSEnter(str(ns)):
+            ip("link set lo up")
+            rtnl = RtnlAddrFamily()
+            lo_idx = socket.if_nametoindex('lo')
+            before = _users_for(rtnl, socket.AF_INET6,
+                                IPV6_TEST_MULTICAST, lo_idx)
+            if before is None:
+                raise KsftSkipEx("kernel does not expose IFA_MC_USERS for IPv6")
+
+            mreq = IPV6_TEST_MULTICAST + struct.pack('=I', lo_idx)
+            s1 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
+            s2 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
+            try:
+                s1.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
+                s2.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
 
-    all_host_multicasts = [
-        addr for addr in addresses if addr['multicast'] == IPV4_ALL_HOSTS_MULTICAST
-    ]
+                after_join = _users_for(rtnl, socket.AF_INET6,
+                                        IPV6_TEST_MULTICAST, lo_idx)
+                if after_join is None:
+                    raise KsftSkipEx("kernel does not expose IFA_MC_USERS for IPv6")
+                ksft_eq(after_join - before, 2,
+                        f"IPv6 users delta != 2 after two joins "
+                        f"(before={before}, after={after_join})")
+            finally:
+                s1.close()
+                s2.close()
 
-    ksft_ge(len(all_host_multicasts), 1,
-            "No interface found with the IPv4 all-hosts multicast address")
 
 def ipv4_devconf_notify() -> None:
     """
@@ -56,7 +135,7 @@ def ipv4_devconf_notify() -> None:
               f"No 'forwarding on' notificiation found for interface {ifname}")
 
 def main() -> None:
-    ksft_run([dump_mcaddr_check, ipv4_devconf_notify])
+    ksft_run([dump_mcaddr_check, dump_mcaddr6_check, ipv4_devconf_notify])
     ksft_exit()
 
 if __name__ == "__main__":
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH net-next 3/3] selftests: net: check multicast group user count

Quoted 8 lines · Yuyang Huang

Extend the RTM_GETMULTICAST dump test to verify IFA_MC_USERS for both IPv4 and IPv6 multicast groups.

Run each protocol test in a fresh network namespace to avoid changing host-network state or racing with unrelated multicast users. Join a fixed multicast group twice using separate sockets and check that the reported user count increases by two.

Quoted 5 lines
---
  tools/testing/selftests/net/rtnetlink.py | 101 ++++++++++++++++++++---
  1 file changed, 90 insertions(+), 11 deletions(-)
maintainer
#9 lore raw
In reply to Yuyang Huang
Quoted 10 lines · Yuyang Huang

RTM_GETMULTICAST reports IPv4 and IPv6 multicast group membership, but does not include the per-group user count. Userspace therefore still has to parse /proc/net/igmp and /proc/net/igmp6 to obtain the Users column. In particular, this prevents iproute2 from moving "ip maddr show" entirely from procfs to rtnetlink.

Add IFA_MC_USERS to carry the user count in RTM_GETMULTICAST dumps and RTM_NEWMULTICAST / RTM_DELMULTICAST notifications for both address families. Update the rt-addr YNL specification and extend the rtnetlink selftest to verify that two joins increase the reported count by two.

Will you take care of implementing the iproute2 bits, too?

Thanks,

Paolo

#10 lore raw
Quoted 8 lines · Paolo Abeni
Quoted 6 lines · Yuyang Huang

RTM_GETMULTICAST reports IPv4 and IPv6 multicast group membership, but does not include the per-group user count. Userspace therefore still has to parse /proc/net/igmp and /proc/net/igmp6 to obtain the Users column. In particular, this prevents iproute2 from moving "ip maddr show" entirely from procfs to rtnetlink.

Quoted 6 lines
Quoted 4 lines

Add IFA_MC_USERS to carry the user count in RTM_GETMULTICAST dumps and RTM_NEWMULTICAST / RTM_DELMULTICAST notifications for both address families. Update the rt-addr YNL specification and extend the rtnetlink selftest to verify that two joins increase the reported count by two.

Will you take care of implementing the iproute2 bits, too?

Yes, I actually have something WIP for iproute2 and realized it cannot be done due to the missing fields. That's why I sent out this patch series first.

#11 lore raw
In reply to Yuyang Huang
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 6 lines · On Tue, 30 Jun 2026 20:02:04 +0900 you wrote:

RTM_GETMULTICAST reports IPv4 and IPv6 multicast group membership, but does not include the per-group user count. Userspace therefore still has to parse /proc/net/igmp and /proc/net/igmp6 to obtain the Users column. In particular, this prevents iproute2 from moving "ip maddr show" entirely from procfs to rtnetlink.

Quoted 6 lines

Add IFA_MC_USERS to carry the user count in RTM_GETMULTICAST dumps and RTM_NEWMULTICAST / RTM_DELMULTICAST notifications for both address families. Update the rt-addr YNL specification and extend the rtnetlink selftest to verify that two joins increase the reported count by two.

[...]

Here is the summary with links:
- [net-next,1/3] net: ipv4: report multicast group user count

    https://git.kernel.org/netdev/net-next/c/7cb8198761e6
  - [net-next,2/3] net: ipv6: report multicast group user count
    https://git.kernel.org/netdev/net-next/c/e1d0f3f08391
  - [net-next,3/3] selftests: net: check multicast group user count
    https://git.kernel.org/netdev/net-next/c/3373cb099e66

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html