Skip to contentSkip to conversation

ip link: Show devices by link type

PATCH Superseded Vadim Kochan 2 participants 8 messages Nov 30, 2014 – Dec 3, 2014

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ip link: Show devices by link type

Added new option 'type' to 'ip link show'
command which allows to filter devices by
link type name:

ip link show type bridge
ip link show type vlan

---
 ip/ipaddress.c        | 24 ++++++++++++++++++++++++
 ip/iplink.c           |  2 +-
 man/man8/ip-link.8.in | 20 +++++++++++++++++++-
 3 files changed, 44 insertions(+), 2 deletions(-)
ip/ipaddress.c +24 -0
index 4d99324..a2f7a83 100644
@@ -57,6 +57,7 @@ static struct
 	int flushe;
 	int group;
 	int master;
+	char *link_kind;
 } filter;
 
 static int do_link;
@@ -189,6 +190,18 @@ static void print_linkmode(FILE *f, struct rtattr *tb)
 		fprintf(f, "mode %s ", link_modes[mode]);
 }
 
+static char *parse_link_kind(struct rtattr *tb)
+{
+	struct rtattr *linkinfo[IFLA_INFO_MAX+1];
+
+	parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
+
+	if (linkinfo[IFLA_INFO_KIND])
+		return RTA_DATA(linkinfo[IFLA_INFO_KIND]);
+
+	return "";
+}
+
 static void print_linktype(FILE *fp, struct rtattr *tb)
 {
 	struct rtattr *linkinfo[IFLA_INFO_MAX+1];
@@ -551,6 +564,14 @@ int print_linkinfo(const struct sockaddr_nl *who,
 	else if (filter.master > 0)
 		return -1;
 
+	if (filter.link_kind && tb[IFLA_LINKINFO]) {
+		char *link_kind = parse_link_kind(tb[IFLA_LINKINFO]);
+		if (strcmp(link_kind, filter.link_kind)) {
+			return -1;
+		}
+	} else if (filter.link_kind)
+		return -1;
+
 	if (n->nlmsg_type == RTM_DELLINK)
 		fprintf(fp, "Deleted ");
 
@@ -1293,6 +1314,9 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
 			if (!ifindex)
 				invarg("Device does not exist\n", *argv);
 			filter.master = ifindex;
+		} else if (do_link && strcmp(*argv, "type") == 0) {
+			NEXT_ARG();
+			filter.link_kind = *argv;
 		} else {
 			if (strcmp(*argv, "dev") == 0) {
 				NEXT_ARG();
ip/iplink.c +1 -1
index ce6eb3e..f9a75d5 100644
@@ -82,7 +82,7 @@ void iplink_usage(void)
 	fprintf(stderr, "			  [ master DEVICE ]\n");
 	fprintf(stderr, "			  [ nomaster ]\n");
 	fprintf(stderr, "			  [ addrgenmode { eui64 | none } ]\n");
-	fprintf(stderr, "       ip link show [ DEVICE | group GROUP ] [up] [master DEV]\n");
+	fprintf(stderr, "       ip link show [ DEVICE | group GROUP ] [up] [master DEV] [type TYPE]\n");
 
 	if (iplink_have_newlink()) {
 		fprintf(stderr, "       ip link help [ TYPE ]\n");
man/man8/ip-link.8.in +19 -1
index 9d4e3da..4233291 100644
@@ -148,7 +148,9 @@ ip-link \- network device configuration
 .IR GROUP " | "
 .BR up " | "
 .B master
-.IR DEVICE " ]"
+.IR DEVICE " | "
+.B type
+.IR TYPE " ]"
 
 .ti -8
 .B ip link help
@@ -688,6 +690,12 @@ only display running interfaces.
 .I DEVICE
 specifies the master device which enslaves devices to show.
 
+.TP
+.BI type " TYPE "
+.I TYPE
+specifies the link type of devices to show.
+
+.TP
 The show command has additional formatting options:
 
 .TP
@@ -719,6 +727,16 @@ ip link show
 Shows the state of all network interfaces on the system.
 .RE
 .PP
+ip link show type bridge
+.RS 4
+Shows the bridge devices.
+.RE
+.PP
+ip link show type vlan
+.RS 4
+Shows the vlan devices.
+.RE
+.PP
 ip link set dev ppp0 mtu 1400
 .RS 4
 Change the MTU the ppp0 device.
Signature · 1 line
2.1.3
#1 lore raw

Hi,

The few things which were confused me a little:

#1 I think, I was not correct by calling it 'link type' in descriptions, because it is rather the interface/device type than link type ... so I can resend updated subject + man page in v2.

#2 Another think is that may be it would be better to consider the ll_type_n2a too by the same 'ip link show type' or it is better to add another option like 'ip link show link ether' and use ll_type_n2a to filter devices by link type.

Quoted 131 lines · Vadim Kochan

Added new option 'type' to 'ip link show'
command which allows to filter devices by
link type name:

ip link show type bridge
ip link show type vlan

---
 ip/ipaddress.c        | 24 ++++++++++++++++++++++++
 ip/iplink.c           |  2 +-
 man/man8/ip-link.8.in | 20 +++++++++++++++++++-
 3 files changed, 44 insertions(+), 2 deletions(-)
ip/ipaddress.c +24 -0
index 4d99324..a2f7a83 100644
@@ -57,6 +57,7 @@ static struct
        int flushe;
        int group;
        int master;
+       char *link_kind;
 } filter;
 
 static int do_link;
@@ -189,6 +190,18 @@ static void print_linkmode(FILE *f, struct rtattr *tb)
                fprintf(f, "mode %s ", link_modes[mode]);
 }
 
+static char *parse_link_kind(struct rtattr *tb)
+{
+       struct rtattr *linkinfo[IFLA_INFO_MAX+1];
+
+       parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
+
+       if (linkinfo[IFLA_INFO_KIND])
+               return RTA_DATA(linkinfo[IFLA_INFO_KIND]);
+
+       return "";
+}
+
 static void print_linktype(FILE *fp, struct rtattr *tb)
 {
        struct rtattr *linkinfo[IFLA_INFO_MAX+1];
@@ -551,6 +564,14 @@ int print_linkinfo(const struct sockaddr_nl *who,
        else if (filter.master > 0)
                return -1;
 
+       if (filter.link_kind && tb[IFLA_LINKINFO]) {
+               char *link_kind = parse_link_kind(tb[IFLA_LINKINFO]);
+               if (strcmp(link_kind, filter.link_kind)) {
+                       return -1;
+               }
+       } else if (filter.link_kind)
+               return -1;
+
        if (n->nlmsg_type == RTM_DELLINK)
                fprintf(fp, "Deleted ");
 
@@ -1293,6 +1314,9 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
                        if (!ifindex)
                                invarg("Device does not exist\n", *argv);
                        filter.master = ifindex;
+               } else if (do_link && strcmp(*argv, "type") == 0) {
+                       NEXT_ARG();
+                       filter.link_kind = *argv;
                } else {
                        if (strcmp(*argv, "dev") == 0) {
                                NEXT_ARG();
ip/iplink.c +1 -1
index ce6eb3e..f9a75d5 100644
@@ -82,7 +82,7 @@ void iplink_usage(void)
        fprintf(stderr, "                         [ master DEVICE ]\n");
        fprintf(stderr, "                         [ nomaster ]\n");
        fprintf(stderr, "                         [ addrgenmode { eui64 | none } ]\n");
-       fprintf(stderr, "       ip link show [ DEVICE | group GROUP ] [up] [master DEV]\n");
+       fprintf(stderr, "       ip link show [ DEVICE | group GROUP ] [up] [master DEV] [type TYPE]\n");
 
        if (iplink_have_newlink()) {
                fprintf(stderr, "       ip link help [ TYPE ]\n");
man/man8/ip-link.8.in +19 -2
index 9d4e3da..4233291 100644
@@ -148,7 +148,9 @@ ip-link \- network device configuration
 .IR GROUP " | "
 .BR up " | "
 .B master
-.IR DEVICE " ]"
+.IR DEVICE " | "
+.B type
+.IR TYPE " ]"
 
 .ti -8
 .B ip link help
@@ -688,6 +690,12 @@ only display running interfaces.
 .I DEVICE
 specifies the master device which enslaves devices to show.
 
+.TP
+.BI type " TYPE "
+.I TYPE
+specifies the link type of devices to show.
+
+.TP
 The show command has additional formatting options:
 
 .TP
@@ -719,6 +727,16 @@ ip link show
 Shows the state of all network interfaces on the system.
 .RE
 .PP
+ip link show type bridge
+.RS 4
+Shows the bridge devices.
+.RE
+.PP
+ip link show type vlan
+.RS 4
+Shows the vlan devices.
+.RE
+.PP
 ip link set dev ppp0 mtu 1400
 .RS 4
 Change the MTU the ppp0 device.
--

2.1.3

#2 lore raw
In reply to Vadim Kochan
Quoted 19 lines · Vadim Kochan

Added new option 'type' to 'ip link show'
command which allows to filter devices by
link type name:

ip link show type bridge
ip link show type vlan

---
  ip/ipaddress.c        | 24 ++++++++++++++++++++++++
  ip/iplink.c           |  2 +-
  man/man8/ip-link.8.in | 20 +++++++++++++++++++-
  3 files changed, 44 insertions(+), 2 deletions(-)
ip/ipaddress.c +0 -0
index 4d99324..a2f7a83 100644

ip/ipaddress.c static struct · line 60

Quoted 4 lines
ip/ipaddress.c +1 -0
@@ -57,6 +57,7 @@ static struct
  	int flushe;
  	int group;
  	int master;
+	char *link_kind;

The name can be just "kind", given all the others dont use the link prefix

Quoted 23 lines
ip/ipaddress.c +12 -0
  } filter;
 
  static int do_link;
@@ -189,6 +190,18 @@ static void print_linkmode(FILE *f, struct rtattr *tb)
  		fprintf(f, "mode %s ", link_modes[mode]);
  }
 
+static char *parse_link_kind(struct rtattr *tb)
+{
+	struct rtattr *linkinfo[IFLA_INFO_MAX+1];
+
+	parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
+
+	if (linkinfo[IFLA_INFO_KIND])
+		return RTA_DATA(linkinfo[IFLA_INFO_KIND]);
+
+	return "";
+}
+
  static void print_linktype(FILE *fp, struct rtattr *tb)
  {
  	struct rtattr *linkinfo[IFLA_INFO_MAX+1];

ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 571

Quoted 8 lines
ip/ipaddress.c +5 -0
@@ -551,6 +564,14 @@ int print_linkinfo(const struct sockaddr_nl *who,
  	else if (filter.master > 0)
  		return -1;
 
+	if (filter.link_kind && tb[IFLA_LINKINFO]) {
+		char *link_kind = parse_link_kind(tb[IFLA_LINKINFO]);
+		if (strcmp(link_kind, filter.link_kind)) {
+			return -1;
+		}

you can skip the braces

ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 572

Quoted 1 line
ip/ipaddress.c +1 -0
+	} else if (filter.link_kind)

you have if (filter.link_kind) twice, you can use a single if without the else.

Quoted 73 lines
ip/ipaddress.c +5 -0
+		return -1;
+
  	if (n->nlmsg_type == RTM_DELLINK)
  		fprintf(fp, "Deleted ");
 
@@ -1293,6 +1314,9 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
  			if (!ifindex)
  				invarg("Device does not exist\n", *argv);
  			filter.master = ifindex;
+		} else if (do_link && strcmp(*argv, "type") == 0) {
+			NEXT_ARG();
+			filter.link_kind = *argv;
  		} else {
  			if (strcmp(*argv, "dev") == 0) {
  				NEXT_ARG();
ip/iplink.c +1 -1
index ce6eb3e..f9a75d5 100644
@@ -82,7 +82,7 @@ void iplink_usage(void)
  	fprintf(stderr, "			  [ master DEVICE ]\n");
  	fprintf(stderr, "			  [ nomaster ]\n");
  	fprintf(stderr, "			  [ addrgenmode { eui64 | none } ]\n");
-	fprintf(stderr, "       ip link show [ DEVICE | group GROUP ] [up] [master DEV]\n");
+	fprintf(stderr, "       ip link show [ DEVICE | group GROUP ] [up] [master DEV] [type TYPE]\n");
 
  	if (iplink_have_newlink()) {
  		fprintf(stderr, "       ip link help [ TYPE ]\n");
man/man8/ip-link.8.in +19 -1
index 9d4e3da..4233291 100644
@@ -148,7 +148,9 @@ ip-link \- network device configuration
  .IR GROUP " | "
  .BR up " | "
  .B master
-.IR DEVICE " ]"
+.IR DEVICE " | "
+.B type
+.IR TYPE " ]"
 
  .ti -8
  .B ip link help
@@ -688,6 +690,12 @@ only display running interfaces.
  .I DEVICE
  specifies the master device which enslaves devices to show.
 
+.TP
+.BI type " TYPE "
+.I TYPE
+specifies the link type of devices to show.
+
+.TP
  The show command has additional formatting options:
 
  .TP
@@ -719,6 +727,16 @@ ip link show
  Shows the state of all network interfaces on the system.
  .RE
  .PP
+ip link show type bridge
+.RS 4
+Shows the bridge devices.
+.RE
+.PP
+ip link show type vlan
+.RS 4
+Shows the vlan devices.
+.RE
+.PP
  ip link set dev ppp0 mtu 1400
  .RS 4
  Change the MTU the ppp0 device.
#3 lore raw
Quoted 4 lines · Roopa Prabhu

ip/ipaddress.c static struct · line 60

Quoted 2 lines
ip/ipaddress.c +1 -0
 	int master;
+	char *link_kind;

The name can be just "kind", given all the others dont use the link prefix

Quoted 1 line

} filter;

OK

Quoted 6 lines

ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 571

Quoted 5 lines
ip/ipaddress.c +5 -0
+	if (filter.link_kind && tb[IFLA_LINKINFO]) {
+		char *link_kind = parse_link_kind(tb[IFLA_LINKINFO]);
+		if (strcmp(link_kind, filter.link_kind)) {
+			return -1;
+		}

you can skip the braces

Quoted 6 lines

ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 572

Quoted 1 line
ip/ipaddress.c +1 -0
+	} else if (filter.link_kind)

you have if (filter.link_kind) twice, you can use a single if without the else.

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return -1;
+

I need to skip interfaces which has not IFLA_LINKINFO attribute, w/o "else if(...)" I get bridges with normal ether devices:

bash# ip link show type bridge

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether XX:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
5: br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default
link/ether 6e:02:f9:17:7f:10 brd ff:ff:ff:ff:ff:ff

but expected result should be only bridges.

Regards,
Vadim

#4 lore raw

Will re-send v2 with changed man page + link_kind filter variable as suggested by Roopa.

Regards,
Vadim

#5 lore raw
In reply to [email protected]
Quoted 23 lines · On 12/2/14, 5:13 PM, [email protected] wrote:
Quoted 4 lines · Roopa Prabhu

ip/ipaddress.c static struct · line 60

Quoted 2 lines

int master;

ip/ipaddress.c +1 -0
+	char *link_kind;

The name can be just "kind", given all the others dont use the link prefix

Quoted 1 line
  } filter;

OK

Quoted 11 lines

ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 571

Quoted 5 lines
ip/ipaddress.c +5 -0
+	if (filter.link_kind && tb[IFLA_LINKINFO]) {
+		char *link_kind = parse_link_kind(tb[IFLA_LINKINFO]);
+		if (strcmp(link_kind, filter.link_kind)) {
+			return -1;
+		}

you can skip the braces

ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 572

Quoted 1 line
ip/ipaddress.c +1 -0
+	} else if (filter.link_kind)

you have if (filter.link_kind) twice, you can use a single if without the else.

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return -1;
+

I need to skip interfaces which has not IFLA_LINKINFO attribute, w/o "else if(...)" I get bridges with normal ether devices:

bash# ip link show type bridge

Quoted 8 lines

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether XX:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
5: br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default
link/ether 6e:02:f9:17:7f:10 brd ff:ff:ff:ff:ff:ff

but expected result should be only bridges.

I was just saying, it could be:

if (filter.link_kind) {
    if (tb[IFLA_LINKINFO]) {
        char *kind = parse_link_kind(tb[IFLA_LINKINFO]);
        if (strcmp(kind, filter.kind))
            return -1;
    } else {
      return -1;
    }
}
#6 lore raw

OK, I can use it)

Thanks,

Quoted 58 lines · Roopa Prabhu
Quoted 37 lines · On 12/2/14, 5:13 PM, [email protected] wrote:
Quoted 8 lines · Roopa Prabhu
Quoted 3 lines
        int master;

+ char *link_kind;

The name can be just "kind", given all the others dont use the link prefix

Quoted 2 lines
  } filter;

OK

Quoted 6 lines

ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 571

Quoted 5 lines
ip/ipaddress.c +5 -0
+       if (filter.link_kind && tb[IFLA_LINKINFO]) {
+               char *link_kind = parse_link_kind(tb[IFLA_LINKINFO]);
+               if (strcmp(link_kind, filter.link_kind)) {
+                       return -1;
+               }
Quoted 9 lines

you can skip the braces

Quoted 2 lines

+ } else if (filter.link_kind)

you have if (filter.link_kind) twice, you can use a single if without the else.

Quoted 3 lines
(quoted diff) +2 -0
+               return -1;
+

I need to skip interfaces which has not IFLA_LINKINFO attribute, w/o "else if(...)" I get bridges with normal ether devices:
bash# ip link show type bridge

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
mode DEFAULT group default

         link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc
Quoted 7 lines

pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether XX:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
5: br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode
DEFAULT group default

         link/ether 6e:02:f9:17:7f:10 brd ff:ff:ff:ff:ff:ff

but expected result should be only bridges.

I was just saying, it could be:

if (filter.link_kind) {
    if (tb[IFLA_LINKINFO]) {
        char *kind = parse_link_kind(tb[IFLA_LINKINFO]);
        if (strcmp(kind, filter.kind))
            return -1;
    } else {
      return -1;
    }
}
#7 lore raw

I have sent v2 with subject "ip link: Show devices by type"

Regards,
Vadim

Quoted 63 lines · Vadim Kochan

OK, I can use it)

Thanks,

Quoted 58 lines · Roopa Prabhu
Quoted 37 lines · On 12/2/14, 5:13 PM, [email protected] wrote:
Quoted 8 lines · Roopa Prabhu
Quoted 3 lines
        int master;

+ char *link_kind;

The name can be just "kind", given all the others dont use the link prefix

Quoted 2 lines
  } filter;

OK

Quoted 6 lines

ip/ipaddress.c int print_linkinfo(const struct sockaddr_nl *who, · line 571

Quoted 5 lines
ip/ipaddress.c +5 -0
+       if (filter.link_kind && tb[IFLA_LINKINFO]) {
+               char *link_kind = parse_link_kind(tb[IFLA_LINKINFO]);
+               if (strcmp(link_kind, filter.link_kind)) {
+                       return -1;
+               }
Quoted 9 lines

you can skip the braces

Quoted 2 lines

+ } else if (filter.link_kind)

you have if (filter.link_kind) twice, you can use a single if without the else.

Quoted 3 lines
(quoted diff) +2 -0
+               return -1;
+

I need to skip interfaces which has not IFLA_LINKINFO attribute, w/o "else if(...)" I get bridges with normal ether devices:
bash# ip link show type bridge

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
mode DEFAULT group default

         link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc
Quoted 7 lines

pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether XX:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
5: br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode
DEFAULT group default

         link/ether 6e:02:f9:17:7f:10 brd ff:ff:ff:ff:ff:ff

but expected result should be only bridges.

I was just saying, it could be:

if (filter.link_kind) {
    if (tb[IFLA_LINKINFO]) {
        char *kind = parse_link_kind(tb[IFLA_LINKINFO]);
        if (strcmp(kind, filter.kind))
            return -1;
    } else {
      return -1;
    }
}