From patchwork Wed Dec 18 15:08:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yuyang Huang X-Patchwork-Id: 13913845 X-Patchwork-Delegate: dsahern@gmail.com Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6EDB1B424B for ; Wed, 18 Dec 2024 15:08:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734534540; cv=none; b=oXiFxSQMrs+80SxGiwRs2+Rt0PGtQOczSeeKP+mFEWARcVKBuaoBcXo+A+8plmDR7ksY0Fa1iFNQgawk0HR5KTQr1spneYmIMtzfEwMehNHa9IoQ0OSEcmbM2R4z5r0KG7++xUSMFCORVtNaV1JOzqi54aI+ceFRDJft8IpgMlc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734534540; c=relaxed/simple; bh=004sZ/nL4UsVxtzRRlA6x7e/NDoNXUL90a+G92aqfAI=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=UjR8jagoD41YgHsL1sz6Qnw3F5+FgTIbkagQytr8HsyEh/m9CsoBn0zR2vafOv+eDTeyNn4mnrc8Nr0qzKX2S3V0k/pD3pIZuTTBH4DhjvFSaKFwv65zJgM7+ZZEK2lRu8JqFOvVU/UMrnrRS3q+zaLonFTyinPeLBBRxpKxj3g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuyanghuang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NhZFvbQy; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuyanghuang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NhZFvbQy" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-725cf06e7bcso5444282b3a.3 for ; Wed, 18 Dec 2024 07:08:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734534538; x=1735139338; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=pV3+lbnIIz3aTESekv7s2IKWuw4bQblntABf+tmBGQk=; b=NhZFvbQyaEWm/sz+MoJ74lqv/+sfPVf/tdASvGIx3pE5DOH/PEcovE+mSrq3m5xFSg U/yXeXIEHqudoDOxCyg0d7IHa8Ubq3vELWcQYIKaxwLy00zehtRLmIPu56+8w0wjsQQ4 ThNanVZtSF2doIEUh019OP0fk1oSEq6rmAhWNuv2MhcpUX3QgzqP4/0iAn+okNhw/uLv Dlu3Emfbg1ojhX60mChM+0Cnn3TYH6mflsAKwUhfF+k9L8hNtjOmNAExiBFLDChlMglQ gvc2NV5uBAjKjhS+LgsS2hg7YGhFAF4Z7ov6ewf0s9VzPdDbig6mLFCHN/1CnSdrokfR KEFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734534538; x=1735139338; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pV3+lbnIIz3aTESekv7s2IKWuw4bQblntABf+tmBGQk=; b=BUFfYbgbwKx1zMfZtwd81XwA3j3yfJWxXnj6UcbvQIuauZW1vL+mEmU5h6AZwlldRE /0lpT3dj1Uq0Zs/yMjKV29SaNcHNnyIMEv606/aKzcnEZBdFOdrYt+OmJdQeYrj197io +6YKiH1s2+LLj8FsmycrWeJQIggQUKsh+euWni6k4QAY3cyD1dk5p8skZZGgx0SBvfqm lACbXCx6lOhxBlh9YCcpHXx1fd2iA+KPLEsSjNAUB8tunkHDxvDspdlUi/XWlEFxJppT 7o7/GkqzFsLI0u4hhpXiI9ktdSd3JAkjVuGK2m09vyUgtGBLhsg/UyVgiXObO5n0WRZz lQAw== X-Forwarded-Encrypted: i=1; AJvYcCXraxNpl1l3GkZJNbs5E5TmbKgsDj+4GbuZIbFQ3vd1qGCtRC5sNPE2xbfrPs3ZeeHnoyW9R+A=@vger.kernel.org X-Gm-Message-State: AOJu0YyULnNn5+4Xavx8wMTTZsH37ZQrseiZVaBHQLYFthSW6nkKFFjn 4+vxnqGSBExLIZYa+76Ik4XMWCi0mvG+P73x4qVMOeSLKJ3bIFTGgiNRaVArWvTCUrIKl0ttvH6 VsSD8z8rKrVOGA7NpHuy4iA== X-Google-Smtp-Source: AGHT+IEtlCKKVpTNL75q2WaGyiZfuoDa7Vvh3LDu8UOB8DoULqa3iC12lLRW2Uo/bxtxH8jnEpHWkC+bfKqjebwjsA== X-Received: from pfbna2.prod.google.com ([2002:a05:6a00:3e02:b0:728:9b0a:2ddf]) (user=yuyanghuang job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:8017:b0:725:db34:6a7d with SMTP id d2e1a72fcca58-72a8d2dc8afmr5228867b3a.23.1734534538194; Wed, 18 Dec 2024 07:08:58 -0800 (PST) Date: Thu, 19 Dec 2024 00:08:52 +0900 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241218150852.185489-1-yuyanghuang@google.com> Subject: [PATCH iproute2-next, v7] iproute2: add 'ip monitor maddress' support From: Yuyang Huang To: Yuyang Huang Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , David Ahern , roopa@cumulusnetworks.com, jiri@resnulli.us, stephen@networkplumber.org, jimictw@google.com, prohr@google.com, liuhangbin@gmail.com, nicolas.dichtel@6wind.com, andrew@lunn.ch, pruddy@vyatta.att-mail.com, netdev@vger.kernel.org, " =?utf-8?q?Maciej_=C5=BBenczykowski?= " , Lorenzo Colitti X-Patchwork-Delegate: dsahern@gmail.com 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. Here is an example usage: root@uml-x86-64:/# ip monitor maddress 9: nettest123 inet6 mcast ff01::1 scope global valid_lft forever preferred_lft forever 9: nettest123 inet6 mcast ff02::1 scope global valid_lft forever preferred_lft forever 9: nettest123 inet mcast 224.0.0.1 scope global valid_lft forever preferred_lft forever 9: nettest123 inet6 mcast ff02::1:ff00:7b01 scope global valid_lft forever preferred_lft forever Deleted 9: nettest123 inet mcast 224.0.0.1 scope global valid_lft forever preferred_lft forever Deleted 9: nettest123 inet6 mcast ff02::1:ff00:7b01 scope global valid_lft forever preferred_lft forever Deleted 9: nettest123 inet6 mcast ff02::1 scope global valid_lft forever preferred_lft forever Cc: Maciej Żenczykowski Cc: Lorenzo Colitti Signed-off-by: Yuyang Huang --- Changelog since v6: - Remove unnecessary commit messages given that the kernel patch is already merged. Changelog since v5: - Revise the commit message example to align with the recent kernel notification patch updates. Changelog since v4: - To match the existing code style, move the boolean operator to the end of the line. - To match the existing naming pattern, use 'maddress' instead of 'maddr'. Changelog since v3: - Update man/man8/ip-monitor.8 page. - Use 'ip monitor maddr' for naming consistency with 'ip maddr' command. Changelog since v1: - Move the UAPI constants to a separate patch. - Update the commit message. - Fix the indentation format. ip/ipaddress.c | 17 +++++++++++++++-- ip/ipmonitor.c | 25 ++++++++++++++++++++++++- man/man8/ip-monitor.8 | 2 +- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index d90ba94d..679b4c00 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..b28faa20 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 | maddress | 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_LMADDR BIT(11) #define IPMON_L_ALL (~0) @@ -202,6 +208,8 @@ int do_ipmonitor(int argc, char **argv) lmask |= IPMON_LLINK; } else if (matches(*argv, "address") == 0) { lmask |= IPMON_LADDR; + } else if (matches(*argv, "maddress") == 0) { + lmask |= IPMON_LMADDR; } else if (matches(*argv, "route") == 0) { lmask |= IPMON_LROUTE; } else if (matches(*argv, "mroute") == 0) { @@ -326,6 +334,21 @@ int do_ipmonitor(int argc, char **argv) exit(1); } + if (lmask & IPMON_LMADDR) { + 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); diff --git a/man/man8/ip-monitor.8 b/man/man8/ip-monitor.8 index ec033c69..a3c099ae 100644 --- a/man/man8/ip-monitor.8 +++ b/man/man8/ip-monitor.8 @@ -54,7 +54,7 @@ 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 ", " prefix ", " +.BR link ", " address ", " route ", " mroute ", " maddress ", " prefix ", " .BR neigh ", " netconf ", " rule ", " stats ", " nsid " and " nexthop "." If no .B file