diff mbox series

[iproute2-next] iproute2: add 'ip monitor mcaddr' support

Message ID 20241117141655.2078777-1-yuyanghuang@google.com (mailing list archive)
State Superseded
Delegated to: David Ahern
Headers show
Series [iproute2-next] iproute2: add 'ip monitor mcaddr' support | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Yuyang Huang Nov. 17, 2024, 2:16 p.m. UTC
Enhanced the 'ip monitor' command to track changes in IPv4 and IPv6
multicast addresses. This update allows the command to listen for
events related to multicast address additions and deletions by
registering to the newly introduced RTNLGRP_IPV4_MCADDR and
RTNLGRP_IPV6_MCADDR netlink groups.

This patch depends on the kernel patch that adds RTNLGRP_IPV4_MCADDR
and RTNLGRP_IPV6_MCADDR being merged first.

Here is an example usage:

root@uml-x86-64:/# ip monitor mcaddr
8: nettest123    inet6 mcast ff01::1 scope global
8: nettest123    inet6 mcast ff02::1 scope global
8: nettest123    inet mcast 224.0.0.1 scope link
8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
Deleted 8: nettest123    inet mcast 224.0.0.1 scope link
Deleted 8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
Deleted 8: nettest123    inet6 mcast ff02::1 scope global

Cc: Maciej Żenczykowski <maze@google.com>
Cc: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Yuyang Huang <yuyanghuang@google.com>
---
 include/uapi/linux/rtnetlink.h |  8 ++++++++
 ip/ipaddress.c                 | 17 +++++++++++++++--
 ip/ipmonitor.c                 | 25 ++++++++++++++++++++++++-
 3 files changed, 47 insertions(+), 3 deletions(-)

Comments

Hangbin Liu Nov. 18, 2024, 12:36 p.m. UTC | #1
On Sun, Nov 17, 2024 at 11:16:55PM +0900, Yuyang Huang wrote:
> Enhanced the 'ip monitor' command to track changes in IPv4 and IPv6
> multicast addresses. This update allows the command to listen for
> events related to multicast address additions and deletions by
> registering to the newly introduced RTNLGRP_IPV4_MCADDR and
> RTNLGRP_IPV6_MCADDR netlink groups.
> 
> This patch depends on the kernel patch that adds RTNLGRP_IPV4_MCADDR
> and RTNLGRP_IPV6_MCADDR being merged first.
> 
> Here is an example usage:
> 
> root@uml-x86-64:/# ip monitor mcaddr
> 8: nettest123    inet6 mcast ff01::1 scope global
> 8: nettest123    inet6 mcast ff02::1 scope global
> 8: nettest123    inet mcast 224.0.0.1 scope link
> 8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
> Deleted 8: nettest123    inet mcast 224.0.0.1 scope link
> Deleted 8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
> Deleted 8: nettest123    inet6 mcast ff02::1 scope global
> 
> Cc: Maciej Żenczykowski <maze@google.com>
> Cc: Lorenzo Colitti <lorenzo@google.com>
> Signed-off-by: Yuyang Huang <yuyanghuang@google.com>
> ---
>  include/uapi/linux/rtnetlink.h |  8 ++++++++
>  ip/ipaddress.c                 | 17 +++++++++++++++--
>  ip/ipmonitor.c                 | 25 ++++++++++++++++++++++++-
>  3 files changed, 47 insertions(+), 3 deletions(-)
> 
> diff --git a/include/uapi/linux/rtnetlink.h b/include/uapi/linux/rtnetlink.h
> index 4e6c8e14..ccf26bf1 100644
> --- a/include/uapi/linux/rtnetlink.h
> +++ b/include/uapi/linux/rtnetlink.h
> @@ -93,6 +93,10 @@ enum {
>  	RTM_NEWPREFIX	= 52,
>  #define RTM_NEWPREFIX	RTM_NEWPREFIX
>  
> +	RTM_NEWMULTICAST,
> +#define RTM_NEWMULTICAST RTM_NEWMULTICAST
> +	RTM_DELMULTICAST,
> +#define RTM_DELMULTICAST RTM_DELMULTICAST
>  	RTM_GETMULTICAST = 58,
>  #define RTM_GETMULTICAST RTM_GETMULTICAST
>  
> @@ -772,6 +776,10 @@ enum rtnetlink_groups {
>  #define RTNLGRP_TUNNEL		RTNLGRP_TUNNEL
>  	RTNLGRP_STATS,
>  #define RTNLGRP_STATS		RTNLGRP_STATS
> +	RTNLGRP_IPV4_MCADDR,
> +#define RTNLGRP_IPV4_MCADDR	RTNLGRP_IPV4_MCADDR
> +	RTNLGRP_IPV6_MCADDR,
> +#define RTNLGRP_IPV6_MCADDR    RTNLGRP_IPV6_MCADDR
>  	__RTNLGRP_MAX
>  };
>  #define RTNLGRP_MAX	(__RTNLGRP_MAX - 1)

No need changes for headers. Stephen will sync the headers.

> @@ -220,6 +226,8 @@ int do_ipmonitor(int argc, char **argv)
>  			lmask |= IPMON_LNEXTHOP;
>  		} else if (strcmp(*argv, "stats") == 0) {
>  			lmask |= IPMON_LSTATS;
> +		} else if (strcmp(*argv, "mcaddr") == 0) {
> +			lmask |= IPMON_LMCADDR;
>  		} else if (strcmp(*argv, "all") == 0) {
>  			prefix_banner = 1;
>  		} else if (matches(*argv, "all-nsid") == 0) {
> @@ -326,6 +334,21 @@ int do_ipmonitor(int argc, char **argv)
>  		exit(1);
>  	}
>  
> +	if (lmask & IPMON_LMCADDR) {
> +		if ((!preferred_family || preferred_family == AF_INET) &&
> +			rtnl_add_nl_group(&rth, RTNLGRP_IPV4_MCADDR) < 0) {

The rtnl_add_nl_group() should be aligned with the upper bracket. e.g.

		if ((!preferred_family || preferred_family == AF_INET) &&
		    rtnl_add_nl_group(&rth, RTNLGRP_IPV4_MCADDR) < 0) {

> +			fprintf(stderr,
> +				"Failed to add ipv4 mcaddr group to list\n");
> +			exit(1);
> +		}
> +		if ((!preferred_family || preferred_family == AF_INET6) &&
> +			rtnl_add_nl_group(&rth, RTNLGRP_IPV6_MCADDR) < 0) {

Same with this one.

Thanks
Hangbin
Yuyang Huang Nov. 18, 2024, 1:19 p.m. UTC | #2
Thanks for the prompt review feedback.

>No need changes for headers. Stephen will sync the headers.

The patch will not compile without the header changes. I guess that
means I should put the patch on hold until the kernel change is merged
and the header changes get synced up to iproute2?

>The rtnl_add_nl_group() should be aligned with the upper bracket. e.g.

> Same with this one.

Acked, will fix the alignment in version 2.

Thanks,
Yuyang

Thanks,
Yuyang


On Mon, Nov 18, 2024 at 9:36 PM Hangbin Liu <liuhangbin@gmail.com> wrote:
>
> On Sun, Nov 17, 2024 at 11:16:55PM +0900, Yuyang Huang wrote:
> > Enhanced the 'ip monitor' command to track changes in IPv4 and IPv6
> > multicast addresses. This update allows the command to listen for
> > events related to multicast address additions and deletions by
> > registering to the newly introduced RTNLGRP_IPV4_MCADDR and
> > RTNLGRP_IPV6_MCADDR netlink groups.
> >
> > This patch depends on the kernel patch that adds RTNLGRP_IPV4_MCADDR
> > and RTNLGRP_IPV6_MCADDR being merged first.
> >
> > Here is an example usage:
> >
> > root@uml-x86-64:/# ip monitor mcaddr
> > 8: nettest123    inet6 mcast ff01::1 scope global
> > 8: nettest123    inet6 mcast ff02::1 scope global
> > 8: nettest123    inet mcast 224.0.0.1 scope link
> > 8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
> > Deleted 8: nettest123    inet mcast 224.0.0.1 scope link
> > Deleted 8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
> > Deleted 8: nettest123    inet6 mcast ff02::1 scope global
> >
> > Cc: Maciej Żenczykowski <maze@google.com>
> > Cc: Lorenzo Colitti <lorenzo@google.com>
> > Signed-off-by: Yuyang Huang <yuyanghuang@google.com>
> > ---
> >  include/uapi/linux/rtnetlink.h |  8 ++++++++
> >  ip/ipaddress.c                 | 17 +++++++++++++++--
> >  ip/ipmonitor.c                 | 25 ++++++++++++++++++++++++-
> >  3 files changed, 47 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/uapi/linux/rtnetlink.h b/include/uapi/linux/rtnetlink.h
> > index 4e6c8e14..ccf26bf1 100644
> > --- a/include/uapi/linux/rtnetlink.h
> > +++ b/include/uapi/linux/rtnetlink.h
> > @@ -93,6 +93,10 @@ enum {
> >       RTM_NEWPREFIX   = 52,
> >  #define RTM_NEWPREFIX        RTM_NEWPREFIX
> >
> > +     RTM_NEWMULTICAST,
> > +#define RTM_NEWMULTICAST RTM_NEWMULTICAST
> > +     RTM_DELMULTICAST,
> > +#define RTM_DELMULTICAST RTM_DELMULTICAST
> >       RTM_GETMULTICAST = 58,
> >  #define RTM_GETMULTICAST RTM_GETMULTICAST
> >
> > @@ -772,6 +776,10 @@ enum rtnetlink_groups {
> >  #define RTNLGRP_TUNNEL               RTNLGRP_TUNNEL
> >       RTNLGRP_STATS,
> >  #define RTNLGRP_STATS                RTNLGRP_STATS
> > +     RTNLGRP_IPV4_MCADDR,
> > +#define RTNLGRP_IPV4_MCADDR  RTNLGRP_IPV4_MCADDR
> > +     RTNLGRP_IPV6_MCADDR,
> > +#define RTNLGRP_IPV6_MCADDR    RTNLGRP_IPV6_MCADDR
> >       __RTNLGRP_MAX
> >  };
> >  #define RTNLGRP_MAX  (__RTNLGRP_MAX - 1)
>
> No need changes for headers. Stephen will sync the headers.
>
> > @@ -220,6 +226,8 @@ int do_ipmonitor(int argc, char **argv)
> >                       lmask |= IPMON_LNEXTHOP;
> >               } else if (strcmp(*argv, "stats") == 0) {
> >                       lmask |= IPMON_LSTATS;
> > +             } else if (strcmp(*argv, "mcaddr") == 0) {
> > +                     lmask |= IPMON_LMCADDR;
> >               } else if (strcmp(*argv, "all") == 0) {
> >                       prefix_banner = 1;
> >               } else if (matches(*argv, "all-nsid") == 0) {
> > @@ -326,6 +334,21 @@ int do_ipmonitor(int argc, char **argv)
> >               exit(1);
> >       }
> >
> > +     if (lmask & IPMON_LMCADDR) {
> > +             if ((!preferred_family || preferred_family == AF_INET) &&
> > +                     rtnl_add_nl_group(&rth, RTNLGRP_IPV4_MCADDR) < 0) {
>
> The rtnl_add_nl_group() should be aligned with the upper bracket. e.g.
>
>                 if ((!preferred_family || preferred_family == AF_INET) &&
>                     rtnl_add_nl_group(&rth, RTNLGRP_IPV4_MCADDR) < 0) {
>
> > +                     fprintf(stderr,
> > +                             "Failed to add ipv4 mcaddr group to list\n");
> > +                     exit(1);
> > +             }
> > +             if ((!preferred_family || preferred_family == AF_INET6) &&
> > +                     rtnl_add_nl_group(&rth, RTNLGRP_IPV6_MCADDR) < 0) {
>
> Same with this one.
>
> Thanks
> Hangbin
David Ahern Nov. 18, 2024, 6:45 p.m. UTC | #3
On 11/18/24 6:19 AM, Yuyang Huang wrote:
> Thanks for the prompt review feedback.
> 
>> No need changes for headers. Stephen will sync the headers.
> 
> The patch will not compile without the header changes. I guess that
> means I should put the patch on hold until the kernel change is merged
> and the header changes get synced up to iproute2?

headers in 1 patch; remaining change in a second patch. That allows the
set to be usable and then I can drop the uapi patch after a header sync
and only apply the second one.
Andrew Lunn Nov. 19, 2024, 1:06 a.m. UTC | #4
On Mon, Nov 18, 2024 at 10:19:59PM +0900, Yuyang Huang wrote:
> Thanks for the prompt review feedback.
> 
> >No need changes for headers. Stephen will sync the headers.
> 
> The patch will not compile without the header changes. I guess that
> means I should put the patch on hold until the kernel change is merged
> and the header changes get synced up to iproute2?

What you can do is have two patches. The first updates the headers,
the second adds the new functionality. What then happens is that the
headers get syncd once, and then all patches queues up for merging get
applied, dropping the first patch of each series.

	Andrew
Yuyang Huang Nov. 19, 2024, 4:25 a.m. UTC | #5
Thanks for the suggestion! I will split the patch into two parts in v2.

Thanks,
Yuyang

Thanks,
Yuyang


On Tue, Nov 19, 2024 at 10:06 AM Andrew Lunn <andrew@lunn.ch> wrote:
>
> On Mon, Nov 18, 2024 at 10:19:59PM +0900, Yuyang Huang wrote:
> > Thanks for the prompt review feedback.
> >
> > >No need changes for headers. Stephen will sync the headers.
> >
> > The patch will not compile without the header changes. I guess that
> > means I should put the patch on hold until the kernel change is merged
> > and the header changes get synced up to iproute2?
>
> What you can do is have two patches. The first updates the headers,
> the second adds the new functionality. What then happens is that the
> headers get syncd once, and then all patches queues up for merging get
> applied, dropping the first patch of each series.
>
>         Andrew
diff mbox series

Patch

diff --git a/include/uapi/linux/rtnetlink.h b/include/uapi/linux/rtnetlink.h
index 4e6c8e14..ccf26bf1 100644
--- a/include/uapi/linux/rtnetlink.h
+++ b/include/uapi/linux/rtnetlink.h
@@ -93,6 +93,10 @@  enum {
 	RTM_NEWPREFIX	= 52,
 #define RTM_NEWPREFIX	RTM_NEWPREFIX
 
+	RTM_NEWMULTICAST,
+#define RTM_NEWMULTICAST RTM_NEWMULTICAST
+	RTM_DELMULTICAST,
+#define RTM_DELMULTICAST RTM_DELMULTICAST
 	RTM_GETMULTICAST = 58,
 #define RTM_GETMULTICAST RTM_GETMULTICAST
 
@@ -772,6 +776,10 @@  enum rtnetlink_groups {
 #define RTNLGRP_TUNNEL		RTNLGRP_TUNNEL
 	RTNLGRP_STATS,
 #define RTNLGRP_STATS		RTNLGRP_STATS
+	RTNLGRP_IPV4_MCADDR,
+#define RTNLGRP_IPV4_MCADDR	RTNLGRP_IPV4_MCADDR
+	RTNLGRP_IPV6_MCADDR,
+#define RTNLGRP_IPV6_MCADDR    RTNLGRP_IPV6_MCADDR
 	__RTNLGRP_MAX
 };
 #define RTNLGRP_MAX	(__RTNLGRP_MAX - 1)
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index d90ba94d..373f613f 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -1504,7 +1504,10 @@  int print_addrinfo(struct nlmsghdr *n, void *arg)
 
 	SPRINT_BUF(b1);
 
-	if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
+	if (n->nlmsg_type != RTM_NEWADDR
+	    && n->nlmsg_type != RTM_DELADDR
+	    && n->nlmsg_type != RTM_NEWMULTICAST
+	    && n->nlmsg_type != RTM_DELMULTICAST)
 		return 0;
 	len -= NLMSG_LENGTH(sizeof(*ifa));
 	if (len < 0) {
@@ -1564,7 +1567,7 @@  int print_addrinfo(struct nlmsghdr *n, void *arg)
 
 	print_headers(fp, "[ADDR]");
 
-	if (n->nlmsg_type == RTM_DELADDR)
+	if (n->nlmsg_type == RTM_DELADDR || n->nlmsg_type == RTM_DELMULTICAST)
 		print_bool(PRINT_ANY, "deleted", "Deleted ", true);
 
 	if (!brief) {
@@ -1639,6 +1642,16 @@  int print_addrinfo(struct nlmsghdr *n, void *arg)
 						   rta_tb[IFA_ANYCAST]));
 	}
 
+	if (rta_tb[IFA_MULTICAST]) {
+		print_string(PRINT_FP, NULL, "%s ", "mcast");
+		print_color_string(PRINT_ANY,
+				   ifa_family_color(ifa->ifa_family),
+				   "multicast",
+				   "%s ",
+				   format_host_rta(ifa->ifa_family,
+						   rta_tb[IFA_MULTICAST]));
+	}
+
 	print_string(PRINT_ANY,
 		     "scope",
 		     "scope %s ",
diff --git a/ip/ipmonitor.c b/ip/ipmonitor.c
index de67f2c9..3690515d 100644
--- a/ip/ipmonitor.c
+++ b/ip/ipmonitor.c
@@ -30,7 +30,7 @@  static void usage(void)
 	fprintf(stderr,
 		"Usage: ip monitor [ all | OBJECTS ] [ FILE ] [ label ] [ all-nsid ]\n"
 		"                  [ dev DEVICE ]\n"
-		"OBJECTS :=  address | link | mroute | neigh | netconf |\n"
+		"OBJECTS :=  address | link | mroute | mcaddr | neigh | netconf |\n"
 		"            nexthop | nsid | prefix | route | rule | stats\n"
 		"FILE := file FILENAME\n");
 	exit(-1);
@@ -152,6 +152,11 @@  static int accept_msg(struct rtnl_ctrl_data *ctrl,
 		ipstats_print(n, arg);
 		return 0;
 
+	case RTM_DELMULTICAST:
+	case RTM_NEWMULTICAST:
+		print_addrinfo(n, arg);
+		return 0;
+
 	case NLMSG_ERROR:
 	case NLMSG_NOOP:
 	case NLMSG_DONE:
@@ -178,6 +183,7 @@  static int accept_msg(struct rtnl_ctrl_data *ctrl,
 #define IPMON_LRULE		BIT(8)
 #define IPMON_LNSID		BIT(9)
 #define IPMON_LNEXTHOP		BIT(10)
+#define IPMON_LMCADDR		BIT(11)
 
 #define IPMON_L_ALL		(~0)
 
@@ -220,6 +226,8 @@  int do_ipmonitor(int argc, char **argv)
 			lmask |= IPMON_LNEXTHOP;
 		} else if (strcmp(*argv, "stats") == 0) {
 			lmask |= IPMON_LSTATS;
+		} else if (strcmp(*argv, "mcaddr") == 0) {
+			lmask |= IPMON_LMCADDR;
 		} else if (strcmp(*argv, "all") == 0) {
 			prefix_banner = 1;
 		} else if (matches(*argv, "all-nsid") == 0) {
@@ -326,6 +334,21 @@  int do_ipmonitor(int argc, char **argv)
 		exit(1);
 	}
 
+	if (lmask & IPMON_LMCADDR) {
+		if ((!preferred_family || preferred_family == AF_INET) &&
+			rtnl_add_nl_group(&rth, RTNLGRP_IPV4_MCADDR) < 0) {
+			fprintf(stderr,
+				"Failed to add ipv4 mcaddr group to list\n");
+			exit(1);
+		}
+		if ((!preferred_family || preferred_family == AF_INET6) &&
+			rtnl_add_nl_group(&rth, RTNLGRP_IPV6_MCADDR) < 0) {
+			fprintf(stderr,
+				"Failed to add ipv6 mcaddr group to list\n");
+			exit(1);
+		}
+	}
+
 	if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
 		exit(1);