netns xfrm: ipcomp support
[safe/jmp/linux-2.6] / net / ipv4 / tcp_probe.c
index 87dd5bf..9bc805d 100644 (file)
@@ -39,9 +39,9 @@ static int port __read_mostly = 0;
 MODULE_PARM_DESC(port, "Port to match (0=all)");
 module_param(port, int, 0);
 
-static int bufsize __read_mostly = 4096;
+static unsigned int bufsize __read_mostly = 4096;
 MODULE_PARM_DESC(bufsize, "Log buffer size in packets (4096)");
-module_param(bufsize, int, 0);
+module_param(bufsize, uint, 0);
 
 static int full __read_mostly;
 MODULE_PARM_DESC(full, "Full log (1=every ack packet received,  0=only cwnd changes)");
@@ -75,12 +75,12 @@ static struct {
 
 static inline int tcp_probe_used(void)
 {
-       return (tcp_probe.head - tcp_probe.tail) % bufsize;
+       return (tcp_probe.head - tcp_probe.tail) & (bufsize - 1);
 }
 
 static inline int tcp_probe_avail(void)
 {
-       return bufsize - tcp_probe_used();
+       return bufsize - tcp_probe_used() - 1;
 }
 
 /*
@@ -94,8 +94,9 @@ static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
        const struct inet_sock *inet = inet_sk(sk);
 
        /* Only update if port matches */
-       if ((port == 0 || ntohs(inet->dport) == port || ntohs(inet->sport) == port)
-           && (full || tp->snd_cwnd != tcp_probe.lastcwnd)) {
+       if ((port == 0 || ntohs(inet->inet_dport) == port ||
+            ntohs(inet->inet_sport) == port) &&
+           (full || tp->snd_cwnd != tcp_probe.lastcwnd)) {
 
                spin_lock(&tcp_probe.lock);
                /* If log fills, just silently drop */
@@ -103,10 +104,10 @@ static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
                        struct tcp_log *p = tcp_probe.log + tcp_probe.head;
 
                        p->tstamp = ktime_get();
-                       p->saddr = inet->saddr;
-                       p->sport = inet->sport;
-                       p->daddr = inet->daddr;
-                       p->dport = inet->dport;
+                       p->saddr = inet->inet_saddr;
+                       p->sport = inet->inet_sport;
+                       p->daddr = inet->inet_daddr;
+                       p->dport = inet->inet_dport;
                        p->length = skb->len;
                        p->snd_nxt = tp->snd_nxt;
                        p->snd_una = tp->snd_una;
@@ -115,7 +116,7 @@ static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
                        p->ssthresh = tcp_current_ssthresh(sk);
                        p->srtt = tp->srtt >> 3;
 
-                       tcp_probe.head = (tcp_probe.head + 1) % bufsize;
+                       tcp_probe.head = (tcp_probe.head + 1) & (bufsize - 1);
                }
                tcp_probe.lastcwnd = tp->snd_cwnd;
                spin_unlock(&tcp_probe.lock);
@@ -148,17 +149,16 @@ static int tcpprobe_open(struct inode * inode, struct file * file)
 static int tcpprobe_sprint(char *tbuf, int n)
 {
        const struct tcp_log *p
-               = tcp_probe.log + tcp_probe.tail % bufsize;
+               = tcp_probe.log + tcp_probe.tail;
        struct timespec tv
                = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
 
        return snprintf(tbuf, n,
-                       "%lu.%09lu %d.%d.%d.%d:%u %d.%d.%d.%d:%u"
-                       " %d %#x %#x %u %u %u %u\n",
+                       "%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n",
                        (unsigned long) tv.tv_sec,
                        (unsigned long) tv.tv_nsec,
-                       NIPQUAD(p->saddr), ntohs(p->sport),
-                       NIPQUAD(p->daddr), ntohs(p->dport),
+                       &p->saddr, ntohs(p->sport),
+                       &p->daddr, ntohs(p->dport),
                        p->length, p->snd_nxt, p->snd_una,
                        p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt);
 }
@@ -166,9 +166,10 @@ static int tcpprobe_sprint(char *tbuf, int n)
 static ssize_t tcpprobe_read(struct file *file, char __user *buf,
                             size_t len, loff_t *ppos)
 {
-       int error = 0, cnt = 0;
+       int error = 0;
+       size_t cnt = 0;
 
-       if (!buf || len < 0)
+       if (!buf)
                return -EINVAL;
 
        while (cnt < len) {
@@ -190,19 +191,18 @@ static ssize_t tcpprobe_read(struct file *file, char __user *buf,
 
                width = tcpprobe_sprint(tbuf, sizeof(tbuf));
 
-               if (width < len)
-                       tcp_probe.tail = (tcp_probe.tail + 1) % bufsize;
+               if (cnt + width < len)
+                       tcp_probe.tail = (tcp_probe.tail + 1) & (bufsize - 1);
 
                spin_unlock_bh(&tcp_probe.lock);
 
                /* if record greater than space available
                   return partial buffer (so far) */
-               if (width >= len)
+               if (cnt + width >= len)
                        break;
 
-               error = copy_to_user(buf + cnt, tbuf, width);
-               if (error)
-                       break;
+               if (copy_to_user(buf + cnt, tbuf, width))
+                       return -EFAULT;
                cnt += width;
        }
 
@@ -222,10 +222,11 @@ static __init int tcpprobe_init(void)
        init_waitqueue_head(&tcp_probe.wait);
        spin_lock_init(&tcp_probe.lock);
 
-       if (bufsize < 0)
+       if (bufsize == 0)
                return -EINVAL;
 
-       tcp_probe.log = kcalloc(sizeof(struct tcp_log), bufsize, GFP_KERNEL);
+       bufsize = roundup_pow_of_two(bufsize);
+       tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL);
        if (!tcp_probe.log)
                goto err0;
 
@@ -236,7 +237,7 @@ static __init int tcpprobe_init(void)
        if (ret)
                goto err1;
 
-       pr_info("TCP probe registered (port=%d)\n", port);
+       pr_info("TCP probe registered (port=%d) bufsize=%u\n", port, bufsize);
        return 0;
  err1:
        proc_net_remove(&init_net, procname);