Skip to contentSkip to conversation

Convert /proc/net/ipv6_route to seq_file interface

PATCH Applied Alexey Dobriyan 5 participants 6 messages Oct 30, 2007

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

PATCH 1/2 Convert /proc/net/ipv6_route to seq_file interface

One proc_net_create() user less.

---

 net/ipv6/route.c |   70 +++++++++++++++++++------------------------------------
 1 file changed, 25 insertions(+), 45 deletions(-)
net/ipv6/route.c +25 -45
@@ -2288,71 +2288,49 @@ struct rt6_proc_arg
 
 static int rt6_info_route(struct rt6_info *rt, void *p_arg)
 {
-	struct rt6_proc_arg *arg = (struct rt6_proc_arg *) p_arg;
+	struct seq_file *m = p_arg;
 
-	if (arg->skip < arg->offset / RT6_INFO_LEN) {
-		arg->skip++;
-		return 0;
-	}
-
-	if (arg->len >= arg->length)
-		return 0;
-
-	arg->len += sprintf(arg->buffer + arg->len,
-			    NIP6_SEQFMT " %02x ",
-			    NIP6(rt->rt6i_dst.addr),
+	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_dst.addr),
 			    rt->rt6i_dst.plen);
 
 #ifdef CONFIG_IPV6_SUBTREES
-	arg->len += sprintf(arg->buffer + arg->len,
-			    NIP6_SEQFMT " %02x ",
-			    NIP6(rt->rt6i_src.addr),
+	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_src.addr),
 			    rt->rt6i_src.plen);
 #else
-	arg->len += sprintf(arg->buffer + arg->len,
-			    "00000000000000000000000000000000 00 ");
+	seq_puts(m, "00000000000000000000000000000000 00 ");
 #endif
 
 	if (rt->rt6i_nexthop) {
-		arg->len += sprintf(arg->buffer + arg->len,
-				    NIP6_SEQFMT,
+		seq_printf(m, NIP6_SEQFMT,
 				    NIP6(*((struct in6_addr *)rt->rt6i_nexthop->primary_key)));
 	} else {
-		arg->len += sprintf(arg->buffer + arg->len,
-				    "00000000000000000000000000000000");
+		seq_puts(m, "00000000000000000000000000000000");
 	}
-	arg->len += sprintf(arg->buffer + arg->len,
-			    " %08x %08x %08x %08x %8s\n",
+	seq_printf(m, " %08x %08x %08x %08x %8s\n",
 			    rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
 			    rt->u.dst.__use, rt->rt6i_flags,
 			    rt->rt6i_dev ? rt->rt6i_dev->name : "");
 	return 0;
 }
 
-static int rt6_proc_info(char *buffer, char **start, off_t offset, int length)
+static int ipv6_route_show(struct seq_file *m, void *v)
 {
-	struct rt6_proc_arg arg = {
-		.buffer = buffer,
-		.offset = offset,
-		.length = length,
-	};
-
-	fib6_clean_all(rt6_info_route, 0, &arg);
-
-	*start = buffer;
-	if (offset)
-		*start += offset % RT6_INFO_LEN;
-
-	arg.len -= offset % RT6_INFO_LEN;
-
-	if (arg.len > length)
-		arg.len = length;
-	if (arg.len < 0)
-		arg.len = 0;
+	fib6_clean_all(rt6_info_route, 0, m);
+	return 0;
+}
 
-	return arg.len;
+static int ipv6_route_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, ipv6_route_show, NULL);
 }
 
+static const struct file_operations ipv6_route_proc_fops = {
+	.open		= ipv6_route_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+
 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
 {
 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
@@ -2499,9 +2477,11 @@ void __init ip6_route_init(void)
 
 	fib6_init();
 #ifdef 	CONFIG_PROC_FS
-	p = proc_net_create(&init_net, "ipv6_route", 0, rt6_proc_info);
-	if (p)
+	p = create_proc_entry("ipv6_route", 0, init_net.proc_net);
+	if (p) {
 		p->owner = THIS_MODULE;
+		p->proc_fops = &ipv6_route_proc_fops;
+	}
 
 	proc_net_fops_create(&init_net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
 #endif
#1 lore raw
Quoted 1 line · Alexey Dobriyan

One proc_net_create() user less.

Funny, I was working on a similar patch.

See comment below.

Quoted 103 lines

---

 net/ipv6/route.c |   70 +++++++++++++++++++------------------------------------
 1 file changed, 25 insertions(+), 45 deletions(-)
net/ipv6/route.c +21 -43
@@ -2288,71 +2288,49 @@ struct rt6_proc_arg
 
 static int rt6_info_route(struct rt6_info *rt, void *p_arg)
 {
-	struct rt6_proc_arg *arg = (struct rt6_proc_arg *) p_arg;
+	struct seq_file *m = p_arg;
 
-	if (arg->skip < arg->offset / RT6_INFO_LEN) {
-		arg->skip++;
-		return 0;
-	}
-
-	if (arg->len >= arg->length)
-		return 0;
-
-	arg->len += sprintf(arg->buffer + arg->len,
-			    NIP6_SEQFMT " %02x ",
-			    NIP6(rt->rt6i_dst.addr),
+	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_dst.addr),
 			    rt->rt6i_dst.plen);
 
 #ifdef CONFIG_IPV6_SUBTREES
-	arg->len += sprintf(arg->buffer + arg->len,
-			    NIP6_SEQFMT " %02x ",
-			    NIP6(rt->rt6i_src.addr),
+	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_src.addr),
 			    rt->rt6i_src.plen);
 #else
-	arg->len += sprintf(arg->buffer + arg->len,
-			    "00000000000000000000000000000000 00 ");
+	seq_puts(m, "00000000000000000000000000000000 00 ");
 #endif
 
 	if (rt->rt6i_nexthop) {
-		arg->len += sprintf(arg->buffer + arg->len,
-				    NIP6_SEQFMT,
+		seq_printf(m, NIP6_SEQFMT,
 				    NIP6(*((struct in6_addr *)rt->rt6i_nexthop->primary_key)));
 	} else {
-		arg->len += sprintf(arg->buffer + arg->len,
-				    "00000000000000000000000000000000");
+		seq_puts(m, "00000000000000000000000000000000");
 	}
-	arg->len += sprintf(arg->buffer + arg->len,
-			    " %08x %08x %08x %08x %8s\n",
+	seq_printf(m, " %08x %08x %08x %08x %8s\n",
 			    rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
 			    rt->u.dst.__use, rt->rt6i_flags,
 			    rt->rt6i_dev ? rt->rt6i_dev->name : "");
 	return 0;
 }
 
-static int rt6_proc_info(char *buffer, char **start, off_t offset, int length)
+static int ipv6_route_show(struct seq_file *m, void *v)
 {
-	struct rt6_proc_arg arg = {
-		.buffer = buffer,
-		.offset = offset,
-		.length = length,
-	};
-
-	fib6_clean_all(rt6_info_route, 0, &arg);
-
-	*start = buffer;
-	if (offset)
-		*start += offset % RT6_INFO_LEN;
-
-	arg.len -= offset % RT6_INFO_LEN;
-
-	if (arg.len > length)
-		arg.len = length;
-	if (arg.len < 0)
-		arg.len = 0;
+	fib6_clean_all(rt6_info_route, 0, m);
+	return 0;
+}
 
-	return arg.len;
+static int ipv6_route_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, ipv6_route_show, NULL);
 }
 
+static const struct file_operations ipv6_route_proc_fops = {
+	.open		= ipv6_route_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+
 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
 {
 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
@@ -2499,9 +2477,11 @@ void __init ip6_route_init(void)
 

net/ipv6/route.c void __init ip6_route_init(void) · line 2485

Quoted 10 lines
net/ipv6/route.c +4 -2
 	fib6_init();
 #ifdef 	CONFIG_PROC_FS
-	p = proc_net_create(&init_net, "ipv6_route", 0, rt6_proc_info);
-	if (p)
 
+	p = create_proc_entry("ipv6_route", 0, init_net.proc_net);
+	if (p) {
 		p->owner = THIS_MODULE;
+		p->proc_fops = &ipv6_route_proc_fops;
+	}

You should use proc_net_fops_create() instead of the above code. It does the same thing.

Otherwise the patch looks fine to me.
Tested on i386.

Benjamin

Quoted 8 lines
net/ipv6/route.c +0 -1
 	proc_net_fops_create(&init_net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
 #endif
 
-

To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
B e n j a m i n   T h e r y  - BULL/DT/Open Software R&D

   http://www.bull.com
#2 lore raw

Cosmetic comment:

I forgot to say there are a few indentation "errors" when I apply your patch. See below.

Quoted 37 lines · Benjamin Thery
Quoted 1 line · Alexey Dobriyan

One proc_net_create() user less.

Funny, I was working on a similar patch.

See comment below.

Quoted 29 lines

---

 net/ipv6/route.c |   70 +++++++++++++++++++------------------------------------
 1 file changed, 25 insertions(+), 45 deletions(-)
net/ipv6/route.c +2 -12
@@ -2288,71 +2288,49 @@ struct rt6_proc_arg
 
 static int rt6_info_route(struct rt6_info *rt, void *p_arg)
 {
-	struct rt6_proc_arg *arg = (struct rt6_proc_arg *) p_arg;
+	struct seq_file *m = p_arg;
 
-	if (arg->skip < arg->offset / RT6_INFO_LEN) {
-		arg->skip++;
-		return 0;
-	}
-
-	if (arg->len >= arg->length)
-		return 0;
-
-	arg->len += sprintf(arg->buffer + arg->len,
-			    NIP6_SEQFMT " %02x ",
-			    NIP6(rt->rt6i_dst.addr),
+	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_dst.addr),
 			    rt->rt6i_dst.plen);
 
Quoted 6 lines

net/ipv6/route.c struct rt6_proc_arg · line 2298

Quoted 6 lines
net/ipv6/route.c +1 -3
 #ifdef CONFIG_IPV6_SUBTREES
-	arg->len += sprintf(arg->buffer + arg->len,
-			    NIP6_SEQFMT " %02x ",
-			    NIP6(rt->rt6i_src.addr),
+	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_src.addr),
 			    rt->rt6i_src.plen);

Indent is wrong for the above line.

Quoted 11 lines

net/ipv6/route.c struct rt6_proc_arg · line 2305

Quoted 11 lines
net/ipv6/route.c +2 -4
 #else
-	arg->len += sprintf(arg->buffer + arg->len,
-			    "00000000000000000000000000000000 00 ");
+	seq_puts(m, "00000000000000000000000000000000 00 ");
 #endif
 
 	if (rt->rt6i_nexthop) {
-		arg->len += sprintf(arg->buffer + arg->len,
-				    NIP6_SEQFMT,
+		seq_printf(m, NIP6_SEQFMT,
 				    NIP6(*((struct in6_addr *)rt->rt6i_nexthop->primary_key)));

Idem.

Quoted 11 lines

net/ipv6/route.c struct rt6_proc_arg · line 2312

Quoted 11 lines
net/ipv6/route.c +2 -4
 	} else {
-		arg->len += sprintf(arg->buffer + arg->len,
-				    "00000000000000000000000000000000");
+		seq_puts(m, "00000000000000000000000000000000");
 	}
-	arg->len += sprintf(arg->buffer + arg->len,
-			    " %08x %08x %08x %08x %8s\n",
+	seq_printf(m, " %08x %08x %08x %08x %8s\n",
 			    rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
 			    rt->u.dst.__use, rt->rt6i_flags,
 			    rt->rt6i_dev ? rt->rt6i_dev->name : "");

Indent of the 3 above lines.

Quoted 75 lines
Quoted 46 lines
net/ipv6/route.c +14 -20
 	return 0;
 }
 
-static int rt6_proc_info(char *buffer, char **start, off_t offset, int length)
+static int ipv6_route_show(struct seq_file *m, void *v)
 {
-	struct rt6_proc_arg arg = {
-		.buffer = buffer,
-		.offset = offset,
-		.length = length,
-	};
-
-	fib6_clean_all(rt6_info_route, 0, &arg);
-
-	*start = buffer;
-	if (offset)
-		*start += offset % RT6_INFO_LEN;
-
-	arg.len -= offset % RT6_INFO_LEN;
-
-	if (arg.len > length)
-		arg.len = length;
-	if (arg.len < 0)
-		arg.len = 0;
+	fib6_clean_all(rt6_info_route, 0, m);
+	return 0;
+}
 
-	return arg.len;
+static int ipv6_route_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, ipv6_route_show, NULL);
 }
 
+static const struct file_operations ipv6_route_proc_fops = {
+	.open		= ipv6_route_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+
 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
 {
 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
@@ -2499,9 +2477,11 @@ void __init ip6_route_init(void)
 

net/ipv6/route.c void __init ip6_route_init(void) · line 2485

Quoted 10 lines
net/ipv6/route.c +4 -2
 	fib6_init();
 #ifdef 	CONFIG_PROC_FS
-	p = proc_net_create(&init_net, "ipv6_route", 0, rt6_proc_info);
-	if (p)
 
+	p = create_proc_entry("ipv6_route", 0, init_net.proc_net);
+	if (p) {
 		p->owner = THIS_MODULE;
+		p->proc_fops = &ipv6_route_proc_fops;
+	}

You should use proc_net_fops_create() instead of the above code. It does the same thing.

Otherwise the patch looks fine to me.
Tested on i386.

Benjamin

Quoted 8 lines
net/ipv6/route.c +0 -1
 	proc_net_fops_create(&init_net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
 #endif
 
-

To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
B e n j a m i n   T h e r y  - BULL/DT/Open Software R&D

   http://www.bull.com
#3 lore raw
In reply to Alexey Dobriyan

net/ipv6/route.c struct rt6_proc_arg · line 2333

Quoted 8 lines · On Tue, 30 Oct 2007 16:11:47 +0300 Alexey Dobriyan <[email protected]> wrote:
net/ipv6/route.c +7 -0
+static const struct file_operations ipv6_route_proc_fops = {
+	.open		= ipv6_route_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+

This needs

	.owner		= THIS_MODULE,
Quoted 9 lines

static int rt6_stats_seq_show(struct seq_file *seq, void *v) {

net/ipv6/route.c +0 -0
 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
@@ -2499,9 +2477,11 @@ void __init ip6_route_init(void)
 
 	fib6_init();
 #ifdef 	CONFIG_PROC_FS

net/ipv6/route.c void __init ip6_route_init(void) · line 2487

Quoted 10 lines
net/ipv6/route.c +4 -2
-	p = proc_net_create(&init_net, "ipv6_route", 0, rt6_proc_info);
-	if (p)
+	p = create_proc_entry("ipv6_route", 0, init_net.proc_net);
+	if (p) {
 		p->owner = THIS_MODULE;
+		p->proc_fops = &ipv6_route_proc_fops;
+	}
 
 	proc_net_fops_create(&init_net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
 #endif

Use proc_net_fops_create()

	proc_net_fops_create(&init_net, "ipv6_route", S_IRUGO, &ipv6_route_proc_fops)

You can get rid of #ifdef since proc_net_fops_create stub does correct thing if PROC_FS is not configured.

Signature · 1 line
Stephen Hemminger <[email protected]>
#4 lore raw
Quoted 12 lines · Stephen Hemminger

net/ipv6/route.c struct rt6_proc_arg · line 2333

Quoted 8 lines · On Tue, 30 Oct 2007 16:11:47 +0300 Alexey Dobriyan <[email protected]> wrote:
net/ipv6/route.c +7 -0
+static const struct file_operations ipv6_route_proc_fops = {
+	.open		= ipv6_route_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+

This needs

Quoted 1 line
	.owner		= THIS_MODULE,

Your ip_queue conversion patch was also missing this, I've fixed it up.