ip-address: Support filtering by slave type, too

This patch allows to query all interfaces enslaved to a bridge or bond
using the following syntax:

| ip addr show type bridge_slave

Filtering has to be done in userspace since the kernel does not support
filtering on IFLA_INFO_SLAVE_KIND.

Functionality introduced in this patch is not fully complete since it
does not allow to match on type and slave type at the same time, but it
doesn't prevent implementing a dedicated slave_type match, either.

Signed-off-by: Phil Sutter <phil@nwl.cc>
This commit is contained in:
Phil Sutter 2016-06-28 15:07:16 +02:00 committed by Stephen Hemminger
parent 62000e51e0
commit e0513807f6

View File

@ -61,6 +61,7 @@ static struct
int group; int group;
int master; int master;
char *kind; char *kind;
char *slave_kind;
} filter; } filter;
static int do_link; static int do_link;
@ -206,18 +207,27 @@ static void print_linkmode(FILE *f, struct rtattr *tb)
fprintf(f, "mode %s ", link_modes[mode]); fprintf(f, "mode %s ", link_modes[mode]);
} }
static char *parse_link_kind(struct rtattr *tb) static char *parse_link_kind(struct rtattr *tb, bool slave)
{ {
struct rtattr *linkinfo[IFLA_INFO_MAX+1]; struct rtattr *linkinfo[IFLA_INFO_MAX+1];
int attr = slave ? IFLA_INFO_SLAVE_KIND : IFLA_INFO_KIND;
parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
if (linkinfo[IFLA_INFO_KIND]) if (linkinfo[attr])
return RTA_DATA(linkinfo[IFLA_INFO_KIND]); return RTA_DATA(linkinfo[attr]);
return ""; return "";
} }
static int match_link_kind(struct rtattr **tb, char *kind, bool slave)
{
if (!tb[IFLA_LINKINFO])
return -1;
return strcmp(parse_link_kind(tb[IFLA_LINKINFO], slave), kind);
}
static void print_linktype(FILE *fp, struct rtattr *tb) static void print_linktype(FILE *fp, struct rtattr *tb)
{ {
struct rtattr *linkinfo[IFLA_INFO_MAX+1]; struct rtattr *linkinfo[IFLA_INFO_MAX+1];
@ -680,16 +690,11 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,
} else if (filter.master > 0) } else if (filter.master > 0)
return -1; return -1;
if (filter.kind) { if (filter.kind && match_link_kind(tb, filter.kind, 0))
if (tb[IFLA_LINKINFO]) { return -1;
char *kind = parse_link_kind(tb[IFLA_LINKINFO]);
if (strcmp(kind, filter.kind)) if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
return -1; return -1;
} else {
return -1;
}
}
if (n->nlmsg_type == RTM_DELLINK) if (n->nlmsg_type == RTM_DELLINK)
fprintf(fp, "Deleted "); fprintf(fp, "Deleted ");
@ -781,16 +786,11 @@ int print_linkinfo(const struct sockaddr_nl *who,
} else if (filter.master > 0) } else if (filter.master > 0)
return -1; return -1;
if (filter.kind) { if (filter.kind && match_link_kind(tb, filter.kind, 0))
if (tb[IFLA_LINKINFO]) { return -1;
char *kind = parse_link_kind(tb[IFLA_LINKINFO]);
if (strcmp(kind, filter.kind)) if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
return -1; return -1;
} else {
return -1;
}
}
if (n->nlmsg_type == RTM_DELLINK) if (n->nlmsg_type == RTM_DELLINK)
fprintf(fp, "Deleted "); fprintf(fp, "Deleted ");
@ -1621,8 +1621,16 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
invarg("Device does not exist\n", *argv); invarg("Device does not exist\n", *argv);
filter.master = ifindex; filter.master = ifindex;
} else if (strcmp(*argv, "type") == 0) { } else if (strcmp(*argv, "type") == 0) {
int soff;
NEXT_ARG(); NEXT_ARG();
filter.kind = *argv; soff = strlen(*argv) - strlen("_slave");
if (!strcmp(*argv + soff, "_slave")) {
(*argv)[soff] = '\0';
filter.slave_kind = *argv;
} else {
filter.kind = *argv;
}
} else { } else {
if (strcmp(*argv, "dev") == 0) { if (strcmp(*argv, "dev") == 0) {
NEXT_ARG(); NEXT_ARG();