mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Octavian Purdila <opurdila@ixiacom.com>
To: Cong Wang <amwang@redhat.com>
Cc: David Miller <davem@davemloft.net>,
	linux-kernel@vger.kernel.org, eric.dumazet@gmail.com,
	linux-rdma@vger.kernel.org, netdev@vger.kernel.org,
	nhorman@tuxdriver.com, linux-sctp@vger.kernel.org
Subject: Re: [RFC Patch] net: reserve ports for applications using fixed port numbers
Date: Mon, 8 Feb 2010 18:51:19 +0200	[thread overview]
Message-ID: <201002081851.19999.opurdila@ixiacom.com> (raw)
In-Reply-To: <4B6F834E.4010801@redhat.com>

On Monday 08 February 2010 05:21:50 you wrote:
> Octavian Purdila wrote:
> > On Friday 05 February 2010 06:45:38 you wrote:
> >> Again, using bitmap algorithm is not a problem and it's better, the
> >> problem is sysctl interface, how would you plan to interact with users
> >> via sysctl/proc if you use bitmap to handle this? I would like to hear
> >> more details about this.
> >
> > We could use something like positive values for setting and negative for
> > reset (e.g. 3 would set the port in the bitmap and -3 would reset it).
> 
> Hmm, then how do you output the info of those ports? Arrays of bitmaps?
> 

See the patch bellow (work in progress).

BTW, while working on it I added some helpers, which we can use to rewrite the proc_doint/long stuff. I think it will help with readability and eliminates some code duplication as well. What do you guys think about that?

--- linux_2.6.32/main/src/kernel/sysctl.c
+++ linux_2.6.32/main/src/kernel/sysctl.c
@@ -250,6 +250,11 @@
 static int max_wakeup_granularity_ns = NSEC_PER_SEC;	/* 1 second */
 #endif
 
+static unsigned long test_bitmap[65535/sizeof(long)];
+static int proc_dobitmap(struct ctl_table *table, int write,
+			 void __user *buf, size_t *lenp, loff_t *ppos);
+
+
 static struct ctl_table kern_table[] = {
 	{
 		.ctl_name	= CTL_UNNUMBERED,
@@ -1032,6 +1037,15 @@
 		.proc_handler	= &proc_dointvec,
 	},
 #endif
+	{
+		.ctl_name	= CTL_UNNUMBERED,
+		.procname	= "bitmap_test",
+		.data		= &test_bitmap,
+		.maxlen		= 65535,
+		.mode		= 0644,
+		.proc_handler	= &proc_dobitmap,
+	},
+	
 /*
  * NOTE: do not add new entries to this table unless you have read
  * Documentation/sysctl/ctl_unnumbered.txt
@@ -2902,6 +2916,194 @@
 	return 0;
 }
 
+static int proc_skip_wspace(char __user **buf, size_t *size)
+{
+	char c;
+
+	while (*size) {
+		if (get_user(c, *buf))
+			return -EFAULT;
+		if (!isspace(c))
+			break;
+		*size--; *buf++;
+	}
+
+	return 0;
+}
+
+static inline int _proc_get_ulong(char __user **buf, size_t *size, 
+				  unsigned long *val, bool *neg)
+{
+#define TMPBUFLEN 21
+	int len = *size;
+	char *p, tmp[TMPBUFLEN];
+
+	if (len > TMPBUFLEN-1)
+		len = TMPBUFLEN-1;
+
+	if (copy_from_user(tmp, *buf, len))
+		return -EFAULT;
+
+	tmp[len] = 0;
+	p = tmp;
+	if (*p == '-' && *size > 1) {
+		*neg = 1;
+		p++;
+	}
+	if (*p < '0' || *p > '9')
+		return -EINVAL;
+
+	*val = simple_strtoul(p, &p, 0);
+
+	len = p - tmp;
+	if ((len < *size) && *p && !isspace(*p))
+		return -EINVAL;
+
+	*buf += len; *size -= len;
+
+	return 0;
+#undef TMPBUFLEN
+}
+
+static int proc_get_long(char __user **buf, size_t *size, long *val)
+{
+	int err;
+	bool neg;
+	unsigned long uval;
+
+	err = _proc_get_ulong(buf, size, &uval, &neg);
+	if (err)
+		return err;
+
+	if (neg)
+		*val = -uval;
+	else
+		*val = uval;
+
+	return 0;
+}
+
+static int proc_get_ulong(char __user **buf, size_t *size, unsigned long *val)
+{
+	int err;
+	bool neg;
+
+	err = _proc_get_ulong(buf, size, val, &neg);
+	if (err)
+		return err;
+	if (neg)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int proc_put_ulong(char __user **buf, size_t *size, unsigned long val,
+			  bool first)
+{
+#define TMPBUFLEN 21
+	int len;
+	char tmp[TMPBUFLEN], *p = tmp;
+
+	if (!first)
+		*p++ = '\t';
+	sprintf(p, "%lu", val);
+	len = strlen(tmp);
+	if (len > *size)
+		len = *size;
+	if (copy_to_user(*buf, tmp, len))
+		return -EFAULT;
+	*size -= len;
+	*buf += len;
+	return 0;
+#undef TMPBUFLEN
+}
+
+static int proc_put_newline(char __user **buf, size_t *size)
+{
+	if (*size) {
+		if (put_user('\n', *buf))
+			return -EFAULT;
+		*size--, *buf++;
+	}
+	return 0;
+}
+
+static int proc_dobitmap(struct ctl_table *table, int write,
+			 void __user *buf, size_t *lenp, loff_t *ppos)
+{
+	bool first = 1;
+	unsigned long *bitmap = (unsigned long *) table->data;
+	unsigned long bitmap_len = table->maxlen;
+	int left = *lenp, err = 0;
+	char __user *buffer = (char __user *) buf;
+
+	if (!bitmap_len || !left || (*ppos && !write)) {
+		*lenp = 0;
+		return 0;
+	}
+
+	if (write) {
+		while (left) {
+			long val;
+			
+			err = proc_skip_wspace(&buffer, &left);
+			if (err)
+				break;
+			if (!left) {
+				err = -EINVAL;
+				break;
+			}
+			err = proc_get_long(&buffer, &left, &val);
+			if (err)
+				break;
+			if (abs(val) > bitmap_len) {
+				err = -EINVAL;
+				break;
+			}
+
+			if (val < 0)
+				clear_bit(-val, bitmap);
+			else
+				set_bit(val, bitmap);
+
+			first = 0;
+		}
+		if (!err)
+			err = proc_skip_wspace(&buffer, &left);
+	} else {
+		unsigned long bit = 0;
+
+		while (left) {
+			bit = find_next_bit(bitmap, bitmap_len, bit);
+			printk("%s:%d %lu\n", __func__, __LINE__, bit);
+			if (bit >= bitmap_len)
+				break;
+			err = proc_put_ulong(&buffer, &left, bit, first);
+			printk("%s:%d %d\n", __func__, __LINE__, err);
+			if (err)
+				break;
+			first = 0; bit++;
+		}
+		if (!err)
+			err = proc_put_newline(&buffer, &left);
+	}
+
+	if (first) {
+		if (write && !err)
+			err = -EINVAL;
+		if (err)
+			return err;
+	}
+
+	if (err == -EFAULT)
+		return err;
+
+	printk("%s:%d %d %d\n", __func__, __LINE__, *lenp, left);
+	*lenp -= left;
+	*ppos += *lenp;
+	return 0;
+}
+
 #else /* CONFIG_PROC_FS */
 
 int proc_dostring(struct ctl_table *table, int write,

  reply	other threads:[~2010-02-08 16:54 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-03  4:30 Amerigo Wang
2010-02-03  4:39 ` Eric Dumazet
2010-02-03  5:15   ` Cong Wang
2010-02-03 11:12 ` Octavian Purdila
2010-02-04  3:23   ` Cong Wang
2010-02-04 12:44     ` Octavian Purdila
2010-02-04 17:41       ` David Miller
2010-02-04 18:15         ` Octavian Purdila
2010-02-04 18:21           ` David Miller
2010-02-04 21:45           ` Tetsuo Handa
2010-02-04 21:56             ` David Miller
2010-02-05  0:41               ` Tetsuo Handa
2010-02-05  1:05                 ` Octavian Purdila
2010-02-05  6:01                   ` Cong Wang
2010-02-05 12:28                     ` Octavian Purdila
2010-02-05  4:45           ` Cong Wang
2010-02-05 12:05             ` Octavian Purdila
2010-02-08  3:21               ` Cong Wang
2010-02-08 16:51                 ` Octavian Purdila [this message]
2010-02-05  7:11 ` Bart Van Assche
2010-02-05  7:25   ` Cong Wang
2010-02-05  9:08     ` [RFC Patch] net: reserve ports for applications using fixed portnumbers Tetsuo Handa

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=201002081851.19999.opurdila@ixiacom.com \
    --to=opurdila@ixiacom.com \
    --cc=amwang@redhat.com \
    --cc=davem@davemloft.net \
    --cc=eric.dumazet@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-sctp@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=nhorman@tuxdriver.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

all inboxes | Powered by JetHome®