netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andi Kleen <ak@suse.de>
To: davem@redhat.com
Cc: netdev@oss.sgi.com
Subject: [PATCH] Add /proc/net/tcp_listen
Date: Fri, 18 Jun 2004 04:31:51 +0200	[thread overview]
Message-ID: <20040618043151.0483c158.ak@suse.de> (raw)


This patch adds a /proc/net/tcp_listen. This is a performance optimization
for slpd from openslp implementing the SLP protocol. A slpd regularly
needs to check the LISTEN sockets on the local system to announce the 
services provided by them on the network. Problem is that reading
/proc/net/tcp can be extremly slow, because it requires to lock/unlock
hundred thousands of rwlocks. On some architectures (especially IA64 and
PPC64) read_unlock seems to be quite slow and a simple cat /proc/net/tcp
on a box with only a few sockets open can take over a second of systime.
Doing this will also disturb network traffic severly (it makes
actually a noticeable difference in SpecWeb)

For slpd this problem can be avoided completely because it only cares
about LISTEN sockets and the LISTEN hash table is much smaller and
can be read much cheaper. For that this patch adds a new /proc/net/tcp_listen
file that only lists LISTEN sockets. 

There were some proposals to make /proc/net/tcp faster too
(like checking the hash bucket head first before getting the lock),
but they are still all far slower than this.

The patch reuses the existing infrastructure and doesn't add much
bloat.

Please consider merging.

-Andi


diff -u linux-2.6.7-work/include/net/tcp.h-TCPPROC linux-2.6.7-work/include/net/tcp.h
--- linux-2.6.7-work/include/net/tcp.h-TCPPROC	2004-06-16 12:23:41.000000000 +0200
+++ linux-2.6.7-work/include/net/tcp.h	2004-06-18 04:23:40.000000000 +0200
@@ -2096,7 +2096,10 @@
 	struct module		*owner;
 	char			*name;
 	sa_family_t		family;
+	void *			(*seq_start)(struct seq_file *seq, loff_t *pos);
 	int			(*seq_show) (struct seq_file *m, void *v);
+	void *			(*seq_next)(struct seq_file *seq, void *v, loff_t *pos);
+
 	struct file_operations	*seq_fops;
 };
 
@@ -2111,6 +2114,11 @@
 extern int tcp_proc_register(struct tcp_seq_afinfo *afinfo);
 extern void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo);
 
+extern void *tcp_seq_start(struct seq_file *seq, loff_t *pos);
+extern void *tcp_listen_seq_start(struct seq_file *seq, loff_t *pos);
+extern void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
+extern void *tcp_listen_seq_next(struct seq_file *seq, void *v, loff_t *pos);
+
 /* TCP Westwood functions and constants */
 
 #define TCP_WESTWOOD_INIT_RTT  (20*HZ)           /* maybe too conservative?! */
diff -u linux-2.6.7-work/net/ipv4/tcp_ipv4.c-TCPPROC linux-2.6.7-work/net/ipv4/tcp_ipv4.c
--- linux-2.6.7-work/net/ipv4/tcp_ipv4.c-TCPPROC	2004-06-16 12:23:45.000000000 +0200
+++ linux-2.6.7-work/net/ipv4/tcp_ipv4.c	2004-06-18 04:20:13.000000000 +0200
@@ -2137,7 +2137,7 @@
 		hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
 }
 
-static void *listening_get_next(struct seq_file *seq, void *cur)
+static void *listening_get_next(struct seq_file *seq, void *cur, int noopenreq)
 {
 	struct tcp_opt *tp;
 	struct hlist_node *node;
@@ -2183,7 +2183,7 @@
 		}
 	       	tp = tcp_sk(sk);
 		read_lock_bh(&tp->syn_wait_lock);
-		if (tp->listen_opt && tp->listen_opt->qlen) {
+		if (!noopenreq && tp->listen_opt && tp->listen_opt->qlen) {
 			st->uid		= sock_i_uid(sk);
 			st->syn_wait_sk = sk;
 			st->state	= TCP_SEQ_STATE_OPENREQ;
@@ -2201,12 +2201,12 @@
 	return cur;
 }
 
-static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
+static void *listening_get_idx(struct seq_file *seq, loff_t *pos, int noopenreq)
 {
-	void *rc = listening_get_next(seq, NULL);
+	void *rc = listening_get_next(seq, NULL, noopenreq);
 
 	while (rc && *pos) {
-		rc = listening_get_next(seq, rc);
+		rc = listening_get_next(seq, rc, noopenreq);
 		--*pos;
 	}
 	return rc;
@@ -2303,40 +2303,54 @@
 	return rc;
 }
 
-static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
+static void *tcp_get_idx(struct seq_file *seq, loff_t pos, int listenonly)
 {
 	void *rc;
 	struct tcp_iter_state* st = seq->private;
 
 	tcp_listen_lock();
 	st->state = TCP_SEQ_STATE_LISTENING;
-	rc	  = listening_get_idx(seq, &pos);
+	rc	  = listening_get_idx(seq, &pos, listenonly);
 
 	if (!rc) {
-		tcp_listen_unlock();
-		local_bh_disable();
-		st->state = TCP_SEQ_STATE_ESTABLISHED;
-		rc	  = established_get_idx(seq, pos);
+		if (!listenonly) { 
+			tcp_listen_unlock();
+			local_bh_disable();
+			st->state = TCP_SEQ_STATE_ESTABLISHED;
+			rc	  = established_get_idx(seq, pos);
+		}
 	}
 
 	return rc;
 }
 
-static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
+void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
+{
+	struct tcp_iter_state* st = seq->private;
+	st->state = TCP_SEQ_STATE_LISTENING;
+	st->num = 0;
+	return *pos ? tcp_get_idx(seq, *pos - 1, 0) : SEQ_START_TOKEN;
+}
+
+EXPORT_SYMBOL(tcp_seq_start);
+
+void *tcp_listen_seq_start(struct seq_file *seq, loff_t *pos)
 {
 	struct tcp_iter_state* st = seq->private;
 	st->state = TCP_SEQ_STATE_LISTENING;
 	st->num = 0;
-	return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
+	return *pos ? tcp_get_idx(seq, *pos - 1, 1) : SEQ_START_TOKEN;
 }
 
-static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+EXPORT_SYMBOL(tcp_listen_seq_start);
+
+void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
 	void *rc = NULL;
 	struct tcp_iter_state* st;
 
 	if (v == SEQ_START_TOKEN) {
-		rc = tcp_get_idx(seq, 0);
+		rc = tcp_get_idx(seq, 0, 0);
 		goto out;
 	}
 	st = seq->private;
@@ -2344,7 +2358,7 @@
 	switch (st->state) {
 	case TCP_SEQ_STATE_OPENREQ:
 	case TCP_SEQ_STATE_LISTENING:
-		rc = listening_get_next(seq, v);
+		rc = listening_get_next(seq, v, 0);
 		if (!rc) {
 			tcp_listen_unlock();
 			local_bh_disable();
@@ -2362,6 +2376,27 @@
 	return rc;
 }
 
+EXPORT_SYMBOL(tcp_seq_next);
+
+void *tcp_listen_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	void *rc = NULL;
+	struct tcp_iter_state* st;
+
+	if (v == SEQ_START_TOKEN) {
+		rc = tcp_get_idx(seq, 0, 1);
+		goto out;
+	}
+	st = seq->private;
+	rc = listening_get_next(seq, v, 1);
+
+out:
+	++*pos;
+	return rc;
+}
+
+EXPORT_SYMBOL(tcp_listen_seq_next);
+
 static void tcp_seq_stop(struct seq_file *seq, void *v)
 {
 	struct tcp_iter_state* st = seq->private;
@@ -2400,8 +2435,8 @@
 		return -ENOMEM;
 	memset(s, 0, sizeof(*s));
 	s->family		= afinfo->family;
-	s->seq_ops.start	= tcp_seq_start;
-	s->seq_ops.next		= tcp_seq_next;
+	s->seq_ops.start	= afinfo->seq_start;
+	s->seq_ops.next		= afinfo->seq_next;
 	s->seq_ops.show		= afinfo->seq_show;
 	s->seq_ops.stop		= tcp_seq_stop;
 
@@ -2570,18 +2605,36 @@
 	.owner		= THIS_MODULE,
 	.name		= "tcp",
 	.family		= AF_INET,
+	.seq_start	= tcp_seq_start, 
 	.seq_show	= tcp4_seq_show,
+	.seq_next	= tcp_seq_next,
 	.seq_fops	= &tcp4_seq_fops,
 };
 
+static struct file_operations tcp4_listen_seq_fops;
+static struct tcp_seq_afinfo tcp4_listen_seq_afinfo = {
+	.owner		= THIS_MODULE,
+	.name		= "tcp_listen",
+	.family		= AF_INET,
+	.seq_start	= tcp_listen_seq_start, 
+	.seq_show	= tcp4_seq_show,
+	.seq_next	= tcp_listen_seq_next,
+	.seq_fops	= &tcp4_listen_seq_fops,
+};
+
+
 int __init tcp4_proc_init(void)
 {
-	return tcp_proc_register(&tcp4_seq_afinfo);
+	int err = tcp_proc_register(&tcp4_seq_afinfo);
+	if (err) 
+		return err;
+	return tcp_proc_register(&tcp4_listen_seq_afinfo);
 }
 
 void tcp4_proc_exit(void)
 {
 	tcp_proc_unregister(&tcp4_seq_afinfo);
+	tcp_proc_unregister(&tcp4_listen_seq_afinfo);
 }
 #endif /* CONFIG_PROC_FS */
 
diff -u linux-2.6.7-work/net/ipv6/tcp_ipv6.c-TCPPROC linux-2.6.7-work/net/ipv6/tcp_ipv6.c
--- linux-2.6.7-work/net/ipv6/tcp_ipv6.c-TCPPROC	2004-06-16 12:23:45.000000000 +0200
+++ linux-2.6.7-work/net/ipv6/tcp_ipv6.c	2004-06-18 04:23:35.000000000 +0200
@@ -2066,18 +2066,35 @@
 	.owner		= THIS_MODULE,
 	.name		= "tcp6",
 	.family		= AF_INET6,
+	.seq_start	= tcp_seq_start,
+	.seq_next	= tcp_seq_next,
 	.seq_show	= tcp6_seq_show,
 	.seq_fops	= &tcp6_seq_fops,
 };
 
+static struct file_operations tcp6_listen_seq_fops;
+static struct tcp_seq_afinfo tcp6_listen_seq_afinfo = {
+	.owner		= THIS_MODULE,
+	.name		= "tcp6_listen",
+	.family		= AF_INET6,
+	.seq_start	= tcp_listen_seq_start,
+	.seq_next	= tcp_listen_seq_next,
+	.seq_show	= tcp6_seq_show,
+	.seq_fops	= &tcp6_listen_seq_fops,
+};
+
 int __init tcp6_proc_init(void)
 {
-	return tcp_proc_register(&tcp6_seq_afinfo);
+	int err = tcp_proc_register(&tcp6_seq_afinfo);
+	if (err)
+		return err;
+	return tcp_proc_register(&tcp6_listen_seq_afinfo);
 }
 
 void tcp6_proc_exit(void)
 {
 	tcp_proc_unregister(&tcp6_seq_afinfo);
+	tcp_proc_unregister(&tcp6_listen_seq_afinfo);
 }
 #endif
 

             reply	other threads:[~2004-06-18  2:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-18  2:31 Andi Kleen [this message]
2004-06-18  3:01 ` [PATCH] Add /proc/net/tcp_listen YOSHIFUJI Hideaki / 吉藤英明
2004-06-18  3:39 ` David S. Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040618043151.0483c158.ak@suse.de \
    --to=ak@suse.de \
    --cc=davem@redhat.com \
    --cc=netdev@oss.sgi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).