diff mbox series

[iproute2-next,2/2] iproute2: add 'ip monitor acaddress' support

Message ID 20241222010548.2304540-2-yuyanghuang@google.com (mailing list archive)
State New
Delegated to: David Ahern
Headers show
Series [iproute2-next,1/2] iproute2: expose anycast netlink constants in UAPI | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Yuyang Huang Dec. 22, 2024, 1:05 a.m. UTC
Enhanced the 'ip monitor' command to track changes in IPv6
anycast addresses. This update allows the command to listen for
events related to anycast address additions and deletions by
registering to the newly introduced RTNLGRP_IPV6_ACADDR netlink group.

This patch depends on the kernel patch that adds RTNLGRP_IPV6_ACADDR
being merged first.

Here is an example usage:

root@uml-x86-64:/# ip monitor acaddress
2: if2    inet6 any 2001:db8:7b:0:528e:a53a:9224:c9c5 scope global
       valid_lft forever preferred_lft forever
Deleted 2: if2    inet6 any 2001:db8:7b:0:528e:a53a:9224:c9c5 scope global
       valid_lft forever preferred_lft forever

Cc: Maciej Żenczykowski <maze@google.com>
Cc: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Yuyang Huang <yuyanghuang@google.com>
---
 ip/ipaddress.c        |  8 ++++++--
 ip/ipmonitor.c        | 18 ++++++++++++++++--
 man/man8/ip-monitor.8 |  5 +++--
 3 files changed, 25 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index 679b4c00..70b3d513 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -1507,7 +1507,9 @@  int print_addrinfo(struct nlmsghdr *n, void *arg)
 	if (n->nlmsg_type != RTM_NEWADDR &&
 	    n->nlmsg_type != RTM_DELADDR &&
 	    n->nlmsg_type != RTM_NEWMULTICAST &&
-	    n->nlmsg_type != RTM_DELMULTICAST)
+	    n->nlmsg_type != RTM_DELMULTICAST &&
+	    n->nlmsg_type != RTM_NEWANYCAST &&
+	    n->nlmsg_type != RTM_DELANYCAST)
 		return 0;
 	len -= NLMSG_LENGTH(sizeof(*ifa));
 	if (len < 0) {
@@ -1567,7 +1569,9 @@  int print_addrinfo(struct nlmsghdr *n, void *arg)
 
 	print_headers(fp, "[ADDR]");
 
-	if (n->nlmsg_type == RTM_DELADDR || n->nlmsg_type == RTM_DELMULTICAST)
+	if (n->nlmsg_type == RTM_DELADDR ||
+	    n->nlmsg_type == RTM_DELMULTICAST ||
+	    n->nlmsg_type == RTM_DELANYCAST)
 		print_bool(PRINT_ANY, "deleted", "Deleted ", true);
 
 	if (!brief) {
diff --git a/ip/ipmonitor.c b/ip/ipmonitor.c
index b28faa20..eec48d83 100644
--- a/ip/ipmonitor.c
+++ b/ip/ipmonitor.c
@@ -30,8 +30,8 @@  static void usage(void)
 	fprintf(stderr,
 		"Usage: ip monitor [ all | OBJECTS ] [ FILE ] [ label ] [ all-nsid ]\n"
 		"                  [ dev DEVICE ]\n"
-		"OBJECTS :=  address | link | mroute | maddress | neigh | netconf |\n"
-		"            nexthop | nsid | prefix | route | rule | stats\n"
+		"OBJECTS :=  address | link | mroute | maddress | acaddress | neigh |\n"
+		"            netconf | nexthop | nsid | prefix | route | rule | stats\n"
 		"FILE := file FILENAME\n");
 	exit(-1);
 }
@@ -154,6 +154,8 @@  static int accept_msg(struct rtnl_ctrl_data *ctrl,
 
 	case RTM_DELMULTICAST:
 	case RTM_NEWMULTICAST:
+	case RTM_DELANYCAST:
+	case RTM_NEWANYCAST:
 		print_addrinfo(n, arg);
 		return 0;
 
@@ -184,6 +186,7 @@  static int accept_msg(struct rtnl_ctrl_data *ctrl,
 #define IPMON_LNSID		BIT(9)
 #define IPMON_LNEXTHOP		BIT(10)
 #define IPMON_LMADDR		BIT(11)
+#define IPMON_LACADDR		BIT(12)
 
 #define IPMON_L_ALL		(~0)
 
@@ -210,6 +213,8 @@  int do_ipmonitor(int argc, char **argv)
 			lmask |= IPMON_LADDR;
 		} else if (matches(*argv, "maddress") == 0) {
 			lmask |= IPMON_LMADDR;
+		} else if (matches(*argv, "acaddress") == 0) {
+			lmask |= IPMON_LACADDR;
 		} else if (matches(*argv, "route") == 0) {
 			lmask |= IPMON_LROUTE;
 		} else if (matches(*argv, "mroute") == 0) {
@@ -349,6 +354,15 @@  int do_ipmonitor(int argc, char **argv)
 		}
 	}
 
+	if (lmask & IPMON_LACADDR) {
+		if ((!preferred_family || preferred_family == AF_INET6) &&
+		    rtnl_add_nl_group(&rth, RTNLGRP_IPV6_ACADDR) < 0) {
+			fprintf(stderr,
+				"Failed to add ipv6 acaddr group to list\n");
+			exit(1);
+		}
+	}
+
 	if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
 		exit(1);
 
diff --git a/man/man8/ip-monitor.8 b/man/man8/ip-monitor.8
index a3c099ae..6cb585ba 100644
--- a/man/man8/ip-monitor.8
+++ b/man/man8/ip-monitor.8
@@ -54,8 +54,9 @@  command is the first in the command line and then the object list follows:
 .I OBJECT-LIST
 is the list of object types that we want to monitor.
 It may contain
-.BR link ", " address ", " route ", " mroute ", " maddress ", " prefix ", "
-.BR neigh ", " netconf ", "  rule ", " stats ", " nsid " and " nexthop "."
+.BR link ", " address ", " route ", " mroute ", " maddress ", " acaddress ", "
+.BR prefix ", "neigh ", " netconf ", "  rule ", " stats ", " nsid " and "
+.BR nexthop "."
 If no
 .B file
 argument is given,