Skip to contentSkip to conversation

iproute2: ip: add wilcard support for device matching

PATCH In review Octavian Purdila 6 participants 10 messages Dec 10, 2010 – Jan 8, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iproute2: ip: add wilcard support for device matching

Allow the users to specify a wildcard when selecting a device:

$ ip set link dev dummy* up

We do this by expanding the original command line in multiple lines which we then feed via a pipe to a forked ip processed run in batch mode.

---
 ip/ip.c |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 70 insertions(+), 0 deletions(-)
ip/ip.c +70 -0
index b127d57..2e26488 100644
@@ -18,6 +18,8 @@
 #include <netinet/in.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include "SNAPSHOT.h"
 #include "utils.h"
@@ -139,10 +141,72 @@ static int batch(const char *name)
 	return ret;
 }
 
+int main(int argc, char **argv);
+
+int expand_dev_pattern(int argc, char **argv, int pos)
+{
+	FILE *proc;
+	size_t n, dev_no;
+	char scanf_pattern[64], *line = NULL, *dev_base = argv[pos];
+	int p[2], i;
+	pid_t pid;
+
+	*strchr(dev_base, '*') = 0;
+	snprintf(scanf_pattern, sizeof(scanf_pattern), " %s%%d:", dev_base);
+
+	if (pipe(p) < 0) {
+		fprintf(stderr, "pipe() failed: %s\n", strerror(errno));
+		return -1;
+	}
+
+	pid = fork();
+	switch (pid) {
+	case -1:
+		fprintf(stderr, "fork failed: %s\n", strerror(errno));
+		return -1;
+	case 0:
+	{
+		char *nargv[] = { argv[0], "-b", "-" };
+		int ret;
+
+		dup2(p[0], 0); close(p[0]); close(p[1]);
+		ret = main(3, nargv);
+		exit(ret);
+	}
+	default:
+		dup2(p[1], 1); close(p[0]); close(p[1]);
+	}
+
+	proc = fopen("/proc/net/dev", "r");
+	if (!proc) {
+		fprintf(stderr, "can't open /proc/net/dev\n");
+		return -1;
+	}
+
+	while (getline(&line, &n, proc) > 0) {
+		if (sscanf(line, scanf_pattern, &dev_no) == 1) {
+			for (i = 1; i < argc; i++)
+				if (i != pos)
+					printf("%s ", argv[i]);
+				else
+					printf("%s%d ", dev_base, dev_no);
+			printf("\n");
+		}
+	}
+	free(line);
+
+	fflush(stdout); close(1);
+
+	waitpid(pid, NULL, 0);
+
+	return 0;
+}
 
 int main(int argc, char **argv)
 {
 	char *basename;
+	int i = 0;
+
 
 	basename = strrchr(argv[0], '/');
 	if (basename == NULL)
@@ -150,6 +214,12 @@ int main(int argc, char **argv)
 	else
 		basename++;
 
+	for (i = 1; i < argc - 1; i++) {
+		if (matches(argv[i], "dev") == 0 && strchr(argv[i+1], '*')) {
+			return expand_dev_pattern(argc, argv, i+1);
+		}
+	}
+
 	while (argc > 1) {
 		char *opt = argv[1];
 		if (strcmp(opt,"--") == 0) {
Signature · 1 line
1.7.1
#1 lore raw
Quoted 8 lines · On Fri, 10 Dec 2010 16:58:12 +0200 Octavian Purdila <[email protected]>…

Allow the users to specify a wildcard when selecting a device:

$ ip set link dev dummy* up

We do this by expanding the original command line in multiple lines which we then feed via a pipe to a forked ip processed run in batch mode.

Seems like feature creep. Can't you do this with bash completion script instead.

#2 lore raw
Quoted 13 lines · Stephen Hemminger
Quoted 8 lines · On Fri, 10 Dec 2010 16:58:12 +0200 Octavian Purdila <[email protected]>…

Allow the users to specify a wildcard when selecting a device:

$ ip set link dev dummy* up

We do this by expanding the original command line in multiple lines which we then feed via a pipe to a forked ip processed run in batch mode.

Quoted 3 lines

Seems like feature creep. Can't you do this with bash completion script instead.
-

furthermore, "*" is allowed in a device name

ip link add link bond0 "vlan*" txqueuelen 100 type vlan id 999

$ ifconfig "vlan*"
vlan*     Link encap:Ethernet  HWaddr 00:1E:0B:EC:D3:D2  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
#3 lore raw

From: Eric Dumazet <[email protected]>
Date: Friday 10 December 2010, 18:32:20

Quoted 6 lines · Stephen Hemminger
Quoted 5 lines

On Fri, 10 Dec 2010 16:58:12 +0200

Quoted 2 lines · Octavian Purdila

Allow the users to specify a wildcard when selecting a device:

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
$ ip set link dev dummy* up

We do this by expanding the original command line in multiple lines which we then feed via a pipe to a forked ip processed run in batch mode.

Seems like feature creep. Can't you do this with bash completion script instead.

This feature would make my life easier so to me its just a nice feature :) Sure I can do it as a bash completion script but:

- bash does not run everywhere

- having it in iproute would make it available to everyone

- its userspace so the price to pay for a few more lines of code for usability seems reasonable

- I don't know how scalable you can make the bash completion script

Quoted 4 lines

furthermore, "*" is allowed in a device name

ip link add link bond0 "vlan*" txqueuelen 100 type vlan id 999

Would allowing escaping it fix the issue? Like:

ip link add link bond0 "vlan\*" txqueuelen 100 type vlan id 999

#4 lore raw
Quoted 4 lines · Octavian Purdila

Would allowing escaping it fix the issue? Like:

ip link add link bond0 "vlan\*" txqueuelen 100 type vlan id 999

Well, same compat problem ;)

$ ip link add link bond0 "vlan\*199" type vlan id 199
$ ifconfig "vlan\*199"
vlan\199  Link encap:Ethernet  HWaddr 00:1E:0B:EC:D3:D2  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
#5 lore raw

From: Eric Dumazet <[email protected]>
Date: Friday 10 December 2010, 19:42:39

Quoted 9 lines · Octavian Purdila
Quoted 3 lines

Would allowing escaping it fix the issue? Like:

ip link add link bond0 "vlan\*" txqueuelen 100 type vlan id 999

Well, same compat problem ;)

$ ip link add link bond0 "vlan\*199" type vlan id 199
$ ifconfig "vlan\*199"

:) Then use a special dev keyword like dev* ?

$ ip link set dev* dummy set

Or use a new flag to allow expansion?

$ ip -e link set dev dummy* set

Thanks,
tavi

#6 lore raw
Quoted 10 lines · Octavian Purdila
Quoted 2 lines
$ ip link add link bond0 "vlan\*199" type vlan id 199
$ ifconfig "vlan\*199"

:) Then use a special dev keyword like dev* ?

$ ip link set dev* dummy set

Or use a new flag to allow expansion?

$ ip -e link set dev dummy* set

There was something ive always wanted to do but havent had time. It will cut time in a big way the user-kernel interaction in precisely your situation.

Add a new general purpose netdev 32 bit tag. You can use this feature to "group" netdevs. The group "all netdevs" is 0 - which is the default.
I can group individual dummy interfaces into group 1. ip link dev dummy0 set group 1
..
..
ip link dev dummy99 set group 1

Then i can send a query to only ifup and ignore
the 1000 vlans that exist.

ip link dev ls group 1

or if i didnt list the group, then group 0 is assumed.

As a warning - this would be a general purpose tag. So i can use it in conjunction with skb->mark to enable filtering for example on ingress side with some action.

cheers,
jamal

#7 lore raw
Quoted 26 lines · jamal
Quoted 10 lines · Octavian Purdila
Quoted 2 lines
$ ip link add link bond0 "vlan\*199" type vlan id 199
$ ifconfig "vlan\*199"

:) Then use a special dev keyword like dev* ?

$ ip link set dev* dummy set

Or use a new flag to allow expansion?

$ ip -e link set dev dummy* set

There was something ive always wanted to do but havent had time. It will cut time in a big way the user-kernel interaction in precisely your situation.

Add a new general purpose netdev 32 bit tag. You can use this feature to "group" netdevs. The group "all netdevs" is 0 - which is the default.
I can group individual dummy interfaces into group 1. ip link dev dummy0 set group 1
..
..
ip link dev dummy99 set group 1

Quoted 6 lines

Then i can send a query to only ifup and ignore
the 1000 vlans that exist.

ip link dev ls group 1

or if i didnt list the group, then group 0 is assumed.

I'll try to implement this approach in the next few days.

Vlad

#8 lore raw
Quoted 1 line · Vlad Dogaru

I'll try to implement this approach in the next few days.

Excellent ;-> Remember, this is general purpose tag, sort like socket/route/skb->mark. It is upto the administrator to define its use via policy.

cheers,
jamal

#9 lore raw
Quoted 9 lines · jamal
Quoted 1 line · Vlad Dogaru

I'll try to implement this approach in the next few days.

Excellent ;-> Remember, this is general purpose tag, sort like socket/route/skb->mark. It is upto the administrator to define its use via policy.

cheers,
jamal

Did that end up going anywhere?

There are a lot of cases where matching on "ppp*" would be handy.