ip-monitor: Do not error out when RTNLGRP_STATS is not available

Following commit 4e8a9914c4 ("ip-monitor: Include stats events in default
and "all" cases"), `ip monitor` fails to start on kernels which do not
contain linux.git commit 5fd0b838efac ("net: rtnetlink: Add UAPI toggle for
IFLA_OFFLOAD_XSTATS_L3_STATS") because the netlink group RTNLGRP_STATS
doesn't exist:

 $ ip monitor
 Failed to add stats group to list

When "stats" is not explicitly requested, ignore the error so that `ip
monitor` and `ip monitor all` continue to work on older kernels.

Note that the same change is not done for RTNLGRP_NEXTHOP because its value
is 32 and group numbers <= 32 are always supported; see the comment above
netlink_change_ngroups() in the kernel source. Therefore
NETLINK_ADD_MEMBERSHIP 32 does not error out even on kernels which do not
support RTNLGRP_NEXTHOP.

v2:
* Silently ignore a failure to implicitly add the stats group, instead of
  printing a warning.

Reported-by: Stephen Hemminger <stephen@networkplumber.org>
Fixes: 4e8a9914c4 ("ip-monitor: Include stats events in default and "all" cases")
Signed-off-by: Benjamin Poirier <bpoirier@nvidia.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Benjamin Poirier 2022-10-26 15:49:07 +09:00 committed by Stephen Hemminger
parent dc2a4c024f
commit 683a8b8d64

View File

@ -195,6 +195,8 @@ static int accept_msg(struct rtnl_ctrl_data *ctrl,
int do_ipmonitor(int argc, char **argv)
{
unsigned int groups = 0, lmask = 0;
/* "needed" mask, failure to enable is an error */
unsigned int nmask;
char *file = NULL;
int ifindex = 0;
@ -253,6 +255,7 @@ int do_ipmonitor(int argc, char **argv)
ipneigh_reset_filter(ifindex);
ipnetconf_reset_filter(ifindex);
nmask = lmask;
if (!lmask)
lmask = IPMON_L_ALL;
@ -327,7 +330,8 @@ int do_ipmonitor(int argc, char **argv)
}
if (lmask & IPMON_LSTATS &&
rtnl_add_nl_group(&rth, RTNLGRP_STATS) < 0) {
rtnl_add_nl_group(&rth, RTNLGRP_STATS) < 0 &&
nmask & IPMON_LSTATS) {
fprintf(stderr, "Failed to add stats group to list\n");
exit(1);
}