nfsd: simplify fh_verify access checks
[safe/jmp/linux-2.6] / net / core / pktgen.c
index 33d7247..6eb8d47 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Alexey Kuznetsov  <kuznet@ms2.inr.ac.ru>
  * Ben Greear <greearb@candelatech.com>
- * Jens Låås <jens.laas@data.slu.se>
+ * Jens Låås <jens.laas@data.slu.se>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include <linux/ioport.h>
 #include <linux/interrupt.h>
 #include <linux/capability.h>
+#include <linux/hrtimer.h>
 #include <linux/freezer.h>
 #include <linux/delay.h>
 #include <linux/timer.h>
 #endif
 #include <asm/byteorder.h>
 #include <linux/rcupdate.h>
-#include <asm/bitops.h>
-#include <asm/io.h>
+#include <linux/bitops.h>
+#include <linux/io.h>
+#include <linux/timex.h>
+#include <linux/uaccess.h>
 #include <asm/dma.h>
-#include <asm/uaccess.h>
 #include <asm/div64.h>         /* do_div */
-#include <asm/timex.h>
 
-#define VERSION  "pktgen v2.69: Packet Generator for packet performance testing.\n"
-
-/* The buckets are exponential in 'width' */
-#define LAT_BUCKETS_MAX 32
+#define VERSION        "2.72"
 #define IP_NAME_SZ 32
 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
 #define MPLS_STACK_BOTTOM htonl(0x00000100)
 #define F_FLOW_SEQ    (1<<11)  /* Sequential flows */
 #define F_IPSEC_ON    (1<<12)  /* ipsec on for flows */
 #define F_QUEUE_MAP_RND (1<<13)        /* queue map Random */
+#define F_QUEUE_MAP_CPU (1<<14)        /* queue map mirrors smp_processor_id() */
 
 /* Thread control flag bits */
-#define T_TERMINATE   (1<<0)
-#define T_STOP        (1<<1)   /* Stop run */
-#define T_RUN         (1<<2)   /* Start run */
-#define T_REMDEVALL   (1<<3)   /* Remove all devs */
-#define T_REMDEV      (1<<4)   /* Remove one dev */
+#define T_STOP        (1<<0)   /* Stop run */
+#define T_RUN         (1<<1)   /* Start run */
+#define T_REMDEVALL   (1<<2)   /* Remove all devs */
+#define T_REMDEV      (1<<3)   /* Remove one dev */
 
 /* If lock -- can be removed after some work */
 #define   if_lock(t)           spin_lock(&(t->if_lock));
 #define PKTGEN_MAGIC 0xbe9be955
 #define PG_PROC_DIR "pktgen"
 #define PGCTRL     "pgctrl"
-static struct proc_dir_entry *pg_proc_dir = NULL;
+static struct proc_dir_entry *pg_proc_dir;
 
 #define MAX_CFLOWS  65536
 
@@ -232,9 +230,9 @@ struct pktgen_dev {
         */
        struct proc_dir_entry *entry;   /* proc file */
        struct pktgen_thread *pg_thread;/* the owner */
-       struct list_head list;          /* Used for chaining in the thread's run-queue */
+       struct list_head list;          /* chaining in the thread's run-queue */
 
-       int running;            /* if this changes to false, the test will stop */
+       int running;            /* if false, the test will stop */
 
        /* If min != max, then we will either do a linear iteration, or
         * we will do a random selection from within the range.
@@ -247,33 +245,37 @@ struct pktgen_dev {
        int max_pkt_size;       /* = ETH_ZLEN; */
        int pkt_overhead;       /* overhead for MPLS, VLANs, IPSEC etc */
        int nfrags;
-       __u32 delay_us;         /* Default delay */
-       __u32 delay_ns;
+       u64 delay;              /* nano-seconds */
+
        __u64 count;            /* Default No packets to send */
        __u64 sofar;            /* How many pkts we've sent so far */
        __u64 tx_bytes;         /* How many bytes we've transmitted */
-       __u64 errors;           /* Errors when trying to transmit, pkts will be re-sent */
+       __u64 errors;           /* Errors when trying to transmit,
+                                  pkts will be re-sent */
 
        /* runtime counters relating to clone_skb */
-       __u64 next_tx_us;       /* timestamp of when to tx next */
-       __u32 next_tx_ns;
 
        __u64 allocated_skbs;
        __u32 clone_count;
        int last_ok;            /* Was last skb sent?
-                                * Or a failed transmit of some sort?  This will keep
-                                * sequence numbers in order, for example.
+                                * Or a failed transmit of some sort?
+                                * This will keep sequence numbers in order
                                 */
-       __u64 started_at;       /* micro-seconds */
-       __u64 stopped_at;       /* micro-seconds */
-       __u64 idle_acc;         /* micro-seconds */
+       ktime_t next_tx;
+       ktime_t started_at;
+       ktime_t stopped_at;
+       u64     idle_acc;       /* nano-seconds */
+
        __u32 seq_num;
 
-       int clone_skb;          /* Use multiple SKBs during packet gen.  If this number
-                                * is greater than 1, then that many copies of the same
-                                * packet will be sent before a new packet is allocated.
-                                * For instance, if you want to send 1024 identical packets
-                                * before creating a new packet, set clone_skb to 1024.
+       int clone_skb;          /*
+                                * Use multiple SKBs during packet gen.
+                                * If this number is greater than 1, then
+                                * that many copies of the same packet will be
+                                * sent before a new packet is allocated.
+                                * If you want to send 1024 identical packets
+                                * before creating a new packet,
+                                * set clone_skb to 1024.
                                 */
 
        char dst_min[IP_NAME_SZ];       /* IP, ie 1.2.3.4 */
@@ -305,8 +307,10 @@ struct pktgen_dev {
        __u16 udp_dst_max;      /* exclusive, dest UDP port */
 
        /* DSCP + ECN */
-       __u8 tos;            /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
-       __u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
+       __u8 tos;            /* six MSB of (former) IPv4 TOS
+                               are for dscp codepoint */
+       __u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
+                               (see RFC 3260, sec. 4) */
 
        /* MPLS */
        unsigned nr_labels;     /* Depth of stack, 0 = no MPLS */
@@ -331,6 +335,7 @@ struct pktgen_dev {
        __u32 cur_src_mac_offset;
        __be32 cur_saddr;
        __be32 cur_daddr;
+       __u16 ip_id;
        __u16 cur_udp_dst;
        __u16 cur_udp_src;
        __u16 cur_queue_map;
@@ -347,15 +352,17 @@ struct pktgen_dev {
         */
        __u16 pad;              /* pad out the hh struct to an even 16 bytes */
 
-       struct sk_buff *skb;    /* skb we are to transmit next, mainly used for when we
+       struct sk_buff *skb;    /* skb we are to transmit next, used for when we
                                 * are transmitting the same one multiple times
                                 */
-       struct net_device *odev;        /* The out-going device.  Note that the device should
-                                        * have it's pg_info pointer pointing back to this
-                                        * device.  This will be set when the user specifies
-                                        * the out-going device name (not when the inject is
-                                        * started as it used to do.)
-                                        */
+       struct net_device *odev; /* The out-going device.
+                                 * Note that the device should have it's
+                                 * pg_info pointer pointing back to this
+                                 * device.
+                                 * Set when the user specifies the out-going
+                                 * device name (not when the inject is
+                                 * started as it used to do.)
+                                 */
        struct flow_state *flows;
        unsigned cflows;        /* Concurrent flows (config) */
        unsigned lflow;         /* Flow length  (config) */
@@ -380,135 +387,41 @@ struct pktgen_hdr {
 };
 
 struct pktgen_thread {
-       spinlock_t if_lock;
+       spinlock_t if_lock;             /* for list of devices */
        struct list_head if_list;       /* All device here */
        struct list_head th_list;
        struct task_struct *tsk;
        char result[512];
 
-       /* Field for thread to receive "posted" events terminate, stop ifs etc. */
+       /* Field for thread to receive "posted" events terminate,
+          stop ifs etc. */
 
        u32 control;
        int cpu;
 
        wait_queue_head_t queue;
+       struct completion start_done;
 };
 
 #define REMOVE 1
 #define FIND   0
 
-/*  This code works around the fact that do_div cannot handle two 64-bit
-    numbers, and regular 64-bit division doesn't work on x86 kernels.
-    --Ben
-*/
-
-#define PG_DIV 0
-
-/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
- * Function copied/adapted/optimized from:
- *
- *  nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
- *
- * Copyright 1994, University of Cambridge Computer Laboratory
- * All Rights Reserved.
- *
- */
-static inline s64 divremdi3(s64 x, s64 y, int type)
+static inline ktime_t ktime_now(void)
 {
-       u64 a = (x < 0) ? -x : x;
-       u64 b = (y < 0) ? -y : y;
-       u64 res = 0, d = 1;
-
-       if (b > 0) {
-               while (b < a) {
-                       b <<= 1;
-                       d <<= 1;
-               }
-       }
-
-       do {
-               if (a >= b) {
-                       a -= b;
-                       res += d;
-               }
-               b >>= 1;
-               d >>= 1;
-       }
-       while (d);
+       struct timespec ts;
+       ktime_get_ts(&ts);
 
-       if (PG_DIV == type) {
-               return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
-       } else {
-               return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
-       }
+       return timespec_to_ktime(ts);
 }
 
-/* End of hacks to deal with 64-bit math on x86 */
-
-/** Convert to milliseconds */
-static inline __u64 tv_to_ms(const struct timeval *tv)
+/* This works even if 32 bit because of careful byte order choice */
+static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
 {
-       __u64 ms = tv->tv_usec / 1000;
-       ms += (__u64) tv->tv_sec * (__u64) 1000;
-       return ms;
+       return cmp1.tv64 < cmp2.tv64;
 }
 
-/** Convert to micro-seconds */
-static inline __u64 tv_to_us(const struct timeval *tv)
-{
-       __u64 us = tv->tv_usec;
-       us += (__u64) tv->tv_sec * (__u64) 1000000;
-       return us;
-}
-
-static inline __u64 pg_div(__u64 n, __u32 base)
-{
-       __u64 tmp = n;
-       do_div(tmp, base);
-       /* printk("pktgen: pg_div, n: %llu  base: %d  rv: %llu\n",
-          n, base, tmp); */
-       return tmp;
-}
-
-static inline __u64 pg_div64(__u64 n, __u64 base)
-{
-       __u64 tmp = n;
-/*
- * How do we know if the architecture we are running on
- * supports division with 64 bit base?
- *
- */
-#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
-
-       do_div(tmp, base);
-#else
-       tmp = divremdi3(n, base, PG_DIV);
-#endif
-       return tmp;
-}
-
-static inline __u64 getCurMs(void)
-{
-       struct timeval tv;
-       do_gettimeofday(&tv);
-       return tv_to_ms(&tv);
-}
-
-static inline __u64 getCurUs(void)
-{
-       struct timeval tv;
-       do_gettimeofday(&tv);
-       return tv_to_us(&tv);
-}
-
-static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
-{
-       return tv_to_us(a) - tv_to_us(b);
-}
-
-/* old include end */
-
-static char version[] __initdata = VERSION;
+static const char version[] =
+       "pktgen " VERSION ": Packet Generator for packet performance testing.\n";
 
 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
@@ -516,8 +429,9 @@ static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
                                          const char *ifname);
 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
 static void pktgen_run_all_threads(void);
+static void pktgen_reset_all_threads(void);
 static void pktgen_stop_all_threads_ifs(void);
-static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
+
 static void pktgen_stop(struct pktgen_thread *t);
 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
 
@@ -525,10 +439,10 @@ static unsigned int scan_ip6(const char *s, char ip[16]);
 static unsigned int fmt_ip6(char *s, const char ip[16]);
 
 /* Module parameters, defaults. */
-static int pg_count_d = 1000;  /* 1000 pkts by default */
-static int pg_delay_d;
-static int pg_clone_skb_d;
-static int debug;
+static int pg_count_d __read_mostly = 1000;
+static int pg_delay_d __read_mostly;
+static int pg_clone_skb_d  __read_mostly;
+static int debug  __read_mostly;
 
 static DEFINE_MUTEX(pktgen_thread_lock);
 static LIST_HEAD(pktgen_threads);
@@ -544,12 +458,12 @@ static struct notifier_block pktgen_notifier_block = {
 
 static int pgctrl_show(struct seq_file *seq, void *v)
 {
-       seq_puts(seq, VERSION);
+       seq_puts(seq, version);
        return 0;
 }
 
-static ssize_t pgctrl_write(struct file *file, const char __user * buf,
-                           size_t count, loff_t * ppos)
+static ssize_t pgctrl_write(struct file *file, const char __user *buf,
+                           size_t count, loff_t *ppos)
 {
        int err = 0;
        char data[128];
@@ -574,6 +488,9 @@ static ssize_t pgctrl_write(struct file *file, const char __user * buf,
        else if (!strcmp(data, "start"))
                pktgen_run_all_threads();
 
+       else if (!strcmp(data, "reset"))
+               pktgen_reset_all_threads();
+
        else
                printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
 
@@ -599,11 +516,9 @@ static const struct file_operations pktgen_fops = {
 
 static int pktgen_if_show(struct seq_file *seq, void *v)
 {
-       int i;
-       struct pktgen_dev *pkt_dev = seq->private;
-       __u64 sa;
-       __u64 stopped;
-       __u64 now = getCurUs();
+       const struct pktgen_dev *pkt_dev = seq->private;
+       ktime_t stopped;
+       u64 idle;
 
        seq_printf(seq,
                   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
@@ -611,9 +526,8 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
                   pkt_dev->max_pkt_size);
 
        seq_printf(seq,
-                  "     frags: %d  delay: %u  clone_skb: %d  ifname: %s\n",
-                  pkt_dev->nfrags,
-                  1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
+                  "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
+                  pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
                   pkt_dev->clone_skb, pkt_dev->odev->name);
 
        seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
@@ -640,30 +554,27 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
                           "     daddr: %s  min_daddr: %s  max_daddr: %s\n", b1,
                           b2, b3);
 
-       } else
+       } else {
                seq_printf(seq,
-                          "     dst_min: %s  dst_max: %s\n     src_min: %s  src_max: %s\n",
-                          pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
-                          pkt_dev->src_max);
+                          "     dst_min: %s  dst_max: %s\n",
+                          pkt_dev->dst_min, pkt_dev->dst_max);
+               seq_printf(seq,
+                          "        src_min: %s  src_max: %s\n",
+                          pkt_dev->src_min, pkt_dev->src_max);
+       }
 
        seq_puts(seq, "     src_mac: ");
 
-       if (is_zero_ether_addr(pkt_dev->src_mac))
-               for (i = 0; i < 6; i++)
-                       seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
-                                  i == 5 ? "  " : ":");
-       else
-               for (i = 0; i < 6; i++)
-                       seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
-                                  i == 5 ? "  " : ":");
+       seq_printf(seq, "%pM ",
+                  is_zero_ether_addr(pkt_dev->src_mac) ?
+                            pkt_dev->odev->dev_addr : pkt_dev->src_mac);
 
        seq_printf(seq, "dst_mac: ");
-       for (i = 0; i < 6; i++)
-               seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
-                          i == 5 ? "\n" : ":");
+       seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
 
        seq_printf(seq,
-                  "     udp_src_min: %d  udp_src_max: %d  udp_dst_min: %d  udp_dst_max: %d\n",
+                  "     udp_src_min: %d  udp_src_max: %d"
+                  "  udp_dst_min: %d  udp_dst_max: %d\n",
                   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
                   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
 
@@ -679,23 +590,21 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
                                   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
        }
 
-       if (pkt_dev->vlan_id != 0xffff) {
+       if (pkt_dev->vlan_id != 0xffff)
                seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
-                          pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
-       }
+                          pkt_dev->vlan_id, pkt_dev->vlan_p,
+                          pkt_dev->vlan_cfi);
 
-       if (pkt_dev->svlan_id != 0xffff) {
+       if (pkt_dev->svlan_id != 0xffff)
                seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
-                          pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
-       }
+                          pkt_dev->svlan_id, pkt_dev->svlan_p,
+                          pkt_dev->svlan_cfi);
 
-       if (pkt_dev->tos) {
+       if (pkt_dev->tos)
                seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
-       }
 
-       if (pkt_dev->traffic_class) {
+       if (pkt_dev->traffic_class)
                seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
-       }
 
        seq_printf(seq, "     Flags: ");
 
@@ -723,6 +632,9 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
        if (pkt_dev->flags & F_QUEUE_MAP_RND)
                seq_printf(seq,  "QUEUE_MAP_RND  ");
 
+       if (pkt_dev->flags & F_QUEUE_MAP_CPU)
+               seq_printf(seq,  "QUEUE_MAP_CPU  ");
+
        if (pkt_dev->cflows) {
                if (pkt_dev->flags & F_FLOW_SEQ)
                        seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
@@ -749,17 +661,21 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
 
        seq_puts(seq, "\n");
 
-       sa = pkt_dev->started_at;
-       stopped = pkt_dev->stopped_at;
-       if (pkt_dev->running)
-               stopped = now;  /* not really stopped, more like last-running-at */
+       /* not really stopped, more like last-running-at */
+       stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
+       idle = pkt_dev->idle_acc;
+       do_div(idle, NSEC_PER_USEC);
 
        seq_printf(seq,
-                  "Current:\n     pkts-sofar: %llu  errors: %llu\n     started: %lluus  stopped: %lluus idle: %lluus\n",
+                  "Current:\n     pkts-sofar: %llu  errors: %llu\n",
                   (unsigned long long)pkt_dev->sofar,
-                  (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
-                  (unsigned long long)stopped,
-                  (unsigned long long)pkt_dev->idle_acc);
+                  (unsigned long long)pkt_dev->errors);
+
+       seq_printf(seq,
+                  "     started: %lluus  stopped: %lluus idle: %lluus\n",
+                  (unsigned long long) ktime_to_us(pkt_dev->started_at),
+                  (unsigned long long) ktime_to_us(stopped),
+                  (unsigned long long) idle);
 
        seq_printf(seq,
                   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
@@ -791,7 +707,8 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
 }
 
 
-static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
+static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
+                    __u32 *num)
 {
        int i = 0;
        *num = 0;
@@ -941,9 +858,9 @@ static ssize_t pktgen_if_write(struct file *file,
        /* Read variable name */
 
        len = strn_len(&user_buffer[i], sizeof(name) - 1);
-       if (len < 0) {
+       if (len < 0)
                return len;
-       }
+
        memset(name, 0, sizeof(name));
        if (copy_from_user(name, &user_buffer[i], len))
                return -EFAULT;
@@ -967,9 +884,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "min_pkt_size")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value < 14 + 20 + 8)
                        value = 14 + 20 + 8;
@@ -984,9 +901,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "max_pkt_size")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value < 14 + 20 + 8)
                        value = 14 + 20 + 8;
@@ -1003,9 +920,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "pkt_size")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value < 14 + 20 + 8)
                        value = 14 + 20 + 8;
@@ -1020,9 +937,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "debug")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                debug = value;
                sprintf(pg_result, "OK: debug=%u", debug);
@@ -1031,9 +948,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "frags")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                pkt_dev->nfrags = value;
                sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
@@ -1041,26 +958,24 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "delay")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
-               if (value == 0x7FFFFFFF) {
-                       pkt_dev->delay_us = 0x7FFFFFFF;
-                       pkt_dev->delay_ns = 0;
-               } else {
-                       pkt_dev->delay_us = value / 1000;
-                       pkt_dev->delay_ns = value % 1000;
-               }
-               sprintf(pg_result, "OK: delay=%u",
-                       1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
+               if (value == 0x7FFFFFFF)
+                       pkt_dev->delay = ULLONG_MAX;
+               else
+                       pkt_dev->delay = (u64)value;
+
+               sprintf(pg_result, "OK: delay=%llu",
+                       (unsigned long long) pkt_dev->delay);
                return count;
        }
        if (!strcmp(name, "udp_src_min")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value != pkt_dev->udp_src_min) {
                        pkt_dev->udp_src_min = value;
@@ -1071,9 +986,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "udp_dst_min")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value != pkt_dev->udp_dst_min) {
                        pkt_dev->udp_dst_min = value;
@@ -1084,9 +999,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "udp_src_max")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value != pkt_dev->udp_src_max) {
                        pkt_dev->udp_src_max = value;
@@ -1097,9 +1012,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "udp_dst_max")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value != pkt_dev->udp_dst_max) {
                        pkt_dev->udp_dst_max = value;
@@ -1110,9 +1025,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "clone_skb")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                pkt_dev->clone_skb = value;
 
@@ -1121,9 +1036,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "count")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                pkt_dev->count = value;
                sprintf(pg_result, "OK: count=%llu",
@@ -1132,9 +1047,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "src_mac_count")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (pkt_dev->src_mac_count != value) {
                        pkt_dev->src_mac_count = value;
@@ -1146,9 +1061,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "dst_mac_count")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (pkt_dev->dst_mac_count != value) {
                        pkt_dev->dst_mac_count = value;
@@ -1162,9 +1077,9 @@ static ssize_t pktgen_if_write(struct file *file,
                char f[32];
                memset(f, 0, 32);
                len = strn_len(&user_buffer[i], sizeof(f) - 1);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                if (copy_from_user(f, &user_buffer[i], len))
                        return -EFAULT;
                i += len;
@@ -1236,6 +1151,12 @@ static ssize_t pktgen_if_write(struct file *file,
 
                else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
                        pkt_dev->flags &= ~F_QUEUE_MAP_RND;
+
+               else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
+                       pkt_dev->flags |= F_QUEUE_MAP_CPU;
+
+               else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
+                       pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
 #ifdef CONFIG_XFRM
                else if (strcmp(f, "IPSEC") == 0)
                        pkt_dev->flags |= F_IPSEC_ON;
@@ -1257,9 +1178,8 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
                len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
 
                if (copy_from_user(buf, &user_buffer[i], len))
                        return -EFAULT;
@@ -1279,9 +1199,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "dst_max")) {
                len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
 
                if (copy_from_user(buf, &user_buffer[i], len))
                        return -EFAULT;
@@ -1392,9 +1312,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "src_min")) {
                len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                if (copy_from_user(buf, &user_buffer[i], len))
                        return -EFAULT;
                buf[len] = 0;
@@ -1413,9 +1333,9 @@ static ssize_t pktgen_if_write(struct file *file,
        }
        if (!strcmp(name, "src_max")) {
                len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                if (copy_from_user(buf, &user_buffer[i], len))
                        return -EFAULT;
                buf[len] = 0;
@@ -1439,9 +1359,9 @@ static ssize_t pktgen_if_write(struct file *file,
                memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
 
                len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                memset(valstr, 0, sizeof(valstr));
                if (copy_from_user(valstr, &user_buffer[i], len))
                        return -EFAULT;
@@ -1481,9 +1401,9 @@ static ssize_t pktgen_if_write(struct file *file,
                memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
 
                len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                memset(valstr, 0, sizeof(valstr));
                if (copy_from_user(valstr, &user_buffer[i], len))
                        return -EFAULT;
@@ -1524,9 +1444,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "flows")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value > MAX_CFLOWS)
                        value = MAX_CFLOWS;
@@ -1538,9 +1458,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "flowlen")) {
                len = num_arg(&user_buffer[i], 10, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                pkt_dev->lflow = value;
                sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
@@ -1549,9 +1469,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "queue_map_min")) {
                len = num_arg(&user_buffer[i], 5, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                pkt_dev->queue_map_min = value;
                sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
@@ -1560,9 +1480,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "queue_map_max")) {
                len = num_arg(&user_buffer[i], 5, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                pkt_dev->queue_map_max = value;
                sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
@@ -1570,15 +1490,17 @@ static ssize_t pktgen_if_write(struct file *file,
        }
 
        if (!strcmp(name, "mpls")) {
-               unsigned n, offset;
+               unsigned n, cnt;
+
                len = get_labels(&user_buffer[i], pkt_dev);
-               if (len < 0) { return len; }
+               if (len < 0)
+                       return len;
                i += len;
-               offset = sprintf(pg_result, "OK: mpls=");
+               cnt = sprintf(pg_result, "OK: mpls=");
                for (n = 0; n < pkt_dev->nr_labels; n++)
-                       offset += sprintf(pg_result + offset,
-                                         "%08x%s", ntohl(pkt_dev->labels[n]),
-                                         n == pkt_dev->nr_labels-1 ? "" : ",");
+                       cnt += sprintf(pg_result + cnt,
+                                      "%08x%s", ntohl(pkt_dev->labels[n]),
+                                      n == pkt_dev->nr_labels-1 ? "" : ",");
 
                if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
                        pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
@@ -1592,9 +1514,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "vlan_id")) {
                len = num_arg(&user_buffer[i], 4, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (value <= 4095) {
                        pkt_dev->vlan_id = value;  /* turn on VLAN */
@@ -1619,9 +1541,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "vlan_p")) {
                len = num_arg(&user_buffer[i], 1, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
                        pkt_dev->vlan_p = value;
@@ -1634,9 +1556,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "vlan_cfi")) {
                len = num_arg(&user_buffer[i], 1, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
                        pkt_dev->vlan_cfi = value;
@@ -1649,9 +1571,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "svlan_id")) {
                len = num_arg(&user_buffer[i], 4, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
                        pkt_dev->svlan_id = value;  /* turn on SVLAN */
@@ -1676,9 +1598,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "svlan_p")) {
                len = num_arg(&user_buffer[i], 1, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
                        pkt_dev->svlan_p = value;
@@ -1691,9 +1613,9 @@ static ssize_t pktgen_if_write(struct file *file,
 
        if (!strcmp(name, "svlan_cfi")) {
                len = num_arg(&user_buffer[i], 1, &value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
                        pkt_dev->svlan_cfi = value;
@@ -1707,9 +1629,9 @@ static ssize_t pktgen_if_write(struct file *file,
        if (!strcmp(name, "tos")) {
                __u32 tmp_value = 0;
                len = hex32_arg(&user_buffer[i], 2, &tmp_value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (len == 2) {
                        pkt_dev->tos = tmp_value;
@@ -1723,9 +1645,9 @@ static ssize_t pktgen_if_write(struct file *file,
        if (!strcmp(name, "traffic_class")) {
                __u32 tmp_value = 0;
                len = hex32_arg(&user_buffer[i], 2, &tmp_value);
-               if (len < 0) {
+               if (len < 0)
                        return len;
-               }
+
                i += len;
                if (len == 2) {
                        pkt_dev->traffic_class = tmp_value;
@@ -1757,7 +1679,7 @@ static const struct file_operations pktgen_if_fops = {
 static int pktgen_thread_show(struct seq_file *seq, void *v)
 {
        struct pktgen_thread *t = seq->private;
-       struct pktgen_dev *pkt_dev;
+       const struct pktgen_dev *pkt_dev;
 
        BUG_ON(!t);
 
@@ -1960,8 +1882,10 @@ static void pktgen_change_name(struct net_device *dev)
 
                        remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
 
-                       pkt_dev->entry = create_proc_entry(dev->name, 0600,
-                                                          pg_proc_dir);
+                       pkt_dev->entry = proc_create_data(dev->name, 0600,
+                                                         pg_proc_dir,
+                                                         &pktgen_if_fops,
+                                                         pkt_dev);
                        if (!pkt_dev->entry)
                                printk(KERN_ERR "pktgen: can't move proc "
                                       " entry for '%s'\n", dev->name);
@@ -1975,6 +1899,9 @@ static int pktgen_device_event(struct notifier_block *unused,
 {
        struct net_device *dev = ptr;
 
+       if (!net_eq(dev_net(dev), &init_net))
+               return NOTIFY_DONE;
+
        /* It is OK that we do not hold the group lock right now,
         * as we run under the RTNL lock.
         */
@@ -1992,6 +1919,24 @@ static int pktgen_device_event(struct notifier_block *unused,
        return NOTIFY_DONE;
 }
 
+static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
+                                                const char *ifname)
+{
+       char b[IFNAMSIZ+5];
+       int i = 0;
+
+       for (i = 0; ifname[i] != '@'; i++) {
+               if (i == IFNAMSIZ)
+                       break;
+
+               b[i] = ifname[i];
+       }
+       b[i] = 0;
+
+       return dev_get_by_name(&init_net, b);
+}
+
+
 /* Associate pktgen_dev with a device. */
 
 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
@@ -2005,7 +1950,7 @@ static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
                pkt_dev->odev = NULL;
        }
 
-       odev = dev_get_by_name(ifname);
+       odev = pktgen_dev_get_by_name(pkt_dev, ifname);
        if (!odev) {
                printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
                return -ENODEV;
@@ -2031,6 +1976,8 @@ static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
  */
 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
 {
+       int ntxq;
+
        if (!pkt_dev->odev) {
                printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
                       "setup_inject.\n");
@@ -2039,6 +1986,26 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
                return;
        }
 
+       /* make sure that we don't pick a non-existing transmit queue */
+       ntxq = pkt_dev->odev->real_num_tx_queues;
+
+       if (ntxq <= pkt_dev->queue_map_min) {
+               printk(KERN_WARNING "pktgen: WARNING: Requested "
+                      "queue_map_min (zero-based) (%d) exceeds valid range "
+                      "[0 - %d] for (%d) queues on %s, resetting\n",
+                      pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
+                      pkt_dev->odev->name);
+               pkt_dev->queue_map_min = ntxq - 1;
+       }
+       if (pkt_dev->queue_map_max >= ntxq) {
+               printk(KERN_WARNING "pktgen: WARNING: Requested "
+                      "queue_map_max (zero-based) (%d) exceeds valid range "
+                      "[0 - %d] for (%d) queues on %s, resetting\n",
+                      pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
+                      pkt_dev->odev->name);
+               pkt_dev->queue_map_max = ntxq - 1;
+       }
+
        /* Default to the interface's mac if not explicitly set. */
 
        if (is_zero_ether_addr(pkt_dev->src_mac))
@@ -2075,7 +2042,8 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
                         */
 
                        rcu_read_lock();
-                       if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
+                       idev = __in6_dev_get(pkt_dev->odev);
+                       if (idev) {
                                struct inet6_ifaddr *ifp;
 
                                read_lock_bh(&idev->lock);
@@ -2134,28 +2102,45 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
        pkt_dev->nflows = 0;
 }
 
-static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
+
+static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
 {
-       __u64 start;
-       __u64 now;
+       ktime_t start_time, end_time;
+       s64 remaining;
+       struct hrtimer_sleeper t;
 
-       start = now = getCurUs();
-       printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
-       while (now < spin_until_us) {
-               /* TODO: optimize sleeping behavior */
-               if (spin_until_us - now > jiffies_to_usecs(1) + 1)
-                       schedule_timeout_interruptible(1);
-               else if (spin_until_us - now > 100) {
-                       if (!pkt_dev->running)
-                               return;
-                       if (need_resched())
+       hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
+       hrtimer_set_expires(&t.timer, spin_until);
+
+       remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
+       if (remaining <= 0) {
+               pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
+               return;
+       }
+
+       start_time = ktime_now();
+       if (remaining < 100)
+               udelay(remaining);      /* really small just spin */
+       else {
+               /* see do_nanosleep */
+               hrtimer_init_sleeper(&t, current);
+               do {
+                       set_current_state(TASK_INTERRUPTIBLE);
+                       hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
+                       if (!hrtimer_active(&t.timer))
+                               t.task = NULL;
+
+                       if (likely(t.task))
                                schedule();
-               }
 
-               now = getCurUs();
+                       hrtimer_cancel(&t.timer);
+               } while (t.task && pkt_dev->running && !signal_pending(current));
+               __set_current_state(TASK_RUNNING);
        }
+       end_time = ktime_now();
 
-       pkt_dev->idle_acc += now - start;
+       pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
+       pkt_dev->next_tx = ktime_add_ns(end_time, pkt_dev->delay);
 }
 
 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
@@ -2166,13 +2151,9 @@ static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
        pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
 }
 
-static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
+static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
 {
-
-       if (pkt_dev->flows[flow].flags & F_INIT)
-               return 1;
-       else
-               return 0;
+       return !!(pkt_dev->flows[flow].flags & F_INIT);
 }
 
 static inline int f_pick(struct pktgen_dev *pkt_dev)
@@ -2183,15 +2164,19 @@ static inline int f_pick(struct pktgen_dev *pkt_dev)
                if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
                        /* reset time */
                        pkt_dev->flows[flow].count = 0;
+                       pkt_dev->flows[flow].flags = 0;
                        pkt_dev->curfl += 1;
                        if (pkt_dev->curfl >= pkt_dev->cflows)
                                pkt_dev->curfl = 0; /*reset */
                }
        } else {
                flow = random32() % pkt_dev->cflows;
+               pkt_dev->curfl = flow;
 
-               if (pkt_dev->flows[flow].count > pkt_dev->lflow)
+               if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
                        pkt_dev->flows[flow].count = 0;
+                       pkt_dev->flows[flow].flags = 0;
+               }
        }
 
        return pkt_dev->curfl;
@@ -2207,7 +2192,8 @@ static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
        struct xfrm_state *x = pkt_dev->flows[flow].x;
        if (!x) {
                /*slow path: we dont already have xfrm_state*/
-               x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
+               x = xfrm_stateonly_find(&init_net,
+                                       (xfrm_address_t *)&pkt_dev->cur_daddr,
                                        (xfrm_address_t *)&pkt_dev->cur_saddr,
                                        AF_INET,
                                        pkt_dev->ipsmode,
@@ -2215,12 +2201,35 @@ static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
                if (x) {
                        pkt_dev->flows[flow].x = x;
                        set_pkt_overhead(pkt_dev);
-                       pkt_dev->pkt_overhead+=x->props.header_len;
+                       pkt_dev->pkt_overhead += x->props.header_len;
                }
 
        }
 }
 #endif
+static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
+{
+
+       if (pkt_dev->flags & F_QUEUE_MAP_CPU)
+               pkt_dev->cur_queue_map = smp_processor_id();
+
+       else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
+               __u16 t;
+               if (pkt_dev->flags & F_QUEUE_MAP_RND) {
+                       t = random32() %
+                               (pkt_dev->queue_map_max -
+                                pkt_dev->queue_map_min + 1)
+                               + pkt_dev->queue_map_min;
+               } else {
+                       t = pkt_dev->cur_queue_map + 1;
+                       if (t > pkt_dev->queue_map_max)
+                               t = pkt_dev->queue_map_min;
+               }
+               pkt_dev->cur_queue_map = t;
+       }
+       pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
+}
+
 /* Increment/randomize headers according to flags and current values
  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
  */
@@ -2242,7 +2251,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                        mc = random32() % pkt_dev->src_mac_count;
                else {
                        mc = pkt_dev->cur_src_mac_offset++;
-                       if (pkt_dev->cur_src_mac_offset >
+                       if (pkt_dev->cur_src_mac_offset >=
                            pkt_dev->src_mac_count)
                                pkt_dev->cur_src_mac_offset = 0;
                }
@@ -2269,7 +2278,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
 
                else {
                        mc = pkt_dev->cur_dst_mac_offset++;
-                       if (pkt_dev->cur_dst_mac_offset >
+                       if (pkt_dev->cur_dst_mac_offset >=
                            pkt_dev->dst_mac_count) {
                                pkt_dev->cur_dst_mac_offset = 0;
                        }
@@ -2331,18 +2340,18 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
 
        if (!(pkt_dev->flags & F_IPV6)) {
 
-               if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
-                                                        ntohl(pkt_dev->
-                                                              saddr_max))) {
+               imn = ntohl(pkt_dev->saddr_min);
+               imx = ntohl(pkt_dev->saddr_max);
+               if (imn < imx) {
                        __u32 t;
                        if (pkt_dev->flags & F_IPSRC_RND)
                                t = random32() % (imx - imn) + imn;
                        else {
                                t = ntohl(pkt_dev->cur_saddr);
                                t++;
-                               if (t > imx) {
+                               if (t > imx)
                                        t = imn;
-                               }
+
                        }
                        pkt_dev->cur_saddr = htonl(t);
                }
@@ -2360,9 +2369,11 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                                        t = random32() % (imx - imn) + imn;
                                        s = htonl(t);
 
-                                       while (LOOPBACK(s) || MULTICAST(s)
-                                              || BADCLASS(s) || ZERONET(s)
-                                              || LOCAL_MCAST(s)) {
+                                       while (ipv4_is_loopback(s) ||
+                                              ipv4_is_multicast(s) ||
+                                              ipv4_is_lbcast(s) ||
+                                              ipv4_is_zeronet(s) ||
+                                              ipv4_is_local_multicast(s)) {
                                                t = random32() % (imx - imn) + imn;
                                                s = htonl(t);
                                        }
@@ -2421,19 +2432,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                pkt_dev->cur_pkt_size = t;
        }
 
-       if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
-               __u16 t;
-               if (pkt_dev->flags & F_QUEUE_MAP_RND) {
-                       t = random32() %
-                               (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
-                               + pkt_dev->queue_map_min;
-               } else {
-                       t = pkt_dev->cur_queue_map + 1;
-                       if (t > pkt_dev->queue_map_max)
-                               t = pkt_dev->queue_map_min;
-               }
-               pkt_dev->cur_queue_map = t;
-       }
+       set_cur_queue_map(pkt_dev);
 
        pkt_dev->flows[flow].count++;
 }
@@ -2456,28 +2455,26 @@ static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
        spin_lock(&x->lock);
        iph = ip_hdr(skb);
 
-       err = x->mode->output(x, skb);
+       err = x->outer_mode->output(x, skb);
        if (err)
                goto error;
        err = x->type->output(x, skb);
        if (err)
                goto error;
 
-       x->curlft.bytes +=skb->len;
+       x->curlft.bytes += skb->len;
        x->curlft.packets++;
-       spin_unlock(&x->lock);
-
 error:
        spin_unlock(&x->lock);
        return err;
 }
 
-static inline void free_SAs(struct pktgen_dev *pkt_dev)
+static void free_SAs(struct pktgen_dev *pkt_dev)
 {
        if (pkt_dev->cflows) {
                /* let go of the SAs if we have them */
                int i = 0;
-               for (;  i < pkt_dev->nflows; i++){
+               for (;  i < pkt_dev->cflows; i++) {
                        struct xfrm_state *x = pkt_dev->flows[i].x;
                        if (x) {
                                xfrm_state_put(x);
@@ -2487,7 +2484,7 @@ static inline void free_SAs(struct pktgen_dev *pkt_dev)
        }
 }
 
-static inline int process_ipsec(struct pktgen_dev *pkt_dev,
+static int process_ipsec(struct pktgen_dev *pkt_dev,
                              struct sk_buff *skb, __be16 protocol)
 {
        if (pkt_dev->flags & F_IPSEC_ON) {
@@ -2497,12 +2494,12 @@ static inline int process_ipsec(struct pktgen_dev *pkt_dev,
                        int ret;
                        __u8 *eth;
                        nhead = x->props.header_len - skb_headroom(skb);
-                       if (nhead >0) {
+                       if (nhead > 0) {
                                ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
                                if (ret < 0) {
                                        printk(KERN_ERR "Error expanding "
-                                              "ipsec packet %d\n",ret);
-                                       return 0;
+                                              "ipsec packet %d\n", ret);
+                                       goto err;
                                }
                        }
 
@@ -2511,26 +2508,28 @@ static inline int process_ipsec(struct pktgen_dev *pkt_dev,
                        ret = pktgen_output_ipsec(skb, pkt_dev);
                        if (ret) {
                                printk(KERN_ERR "Error creating ipsec "
-                                      "packet %d\n",ret);
-                               kfree_skb(skb);
-                               return 0;
+                                      "packet %d\n", ret);
+                               goto err;
                        }
                        /* restore ll */
                        eth = (__u8 *) skb_push(skb, ETH_HLEN);
                        memcpy(eth, pkt_dev->hh, 12);
-                       *(u16 *) & eth[12] = protocol;
+                       *(u16 *) &eth[12] = protocol;
                }
        }
        return 1;
+err:
+       kfree_skb(skb);
+       return 0;
 }
 #endif
 
 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
 {
        unsigned i;
-       for (i = 0; i < pkt_dev->nr_labels; i++) {
+       for (i = 0; i < pkt_dev->nr_labels; i++)
                *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
-       }
+
        mpls--;
        *mpls |= MPLS_STACK_BOTTOM;
 }
@@ -2556,7 +2555,7 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
        __be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
        __be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
        __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
-
+       u16 queue_map;
 
        if (pkt_dev->nr_labels)
                protocol = htons(ETH_P_MPLS_UC);
@@ -2567,11 +2566,13 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
        /* Update any of the values, used when we're incrementing various
         * fields.
         */
+       queue_map = pkt_dev->cur_queue_map;
        mod_cur_headers(pkt_dev);
 
        datalen = (odev->hard_header_len + 16) & ~0xf;
-       skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
-                       pkt_dev->pkt_overhead, GFP_ATOMIC);
+       skb = __netdev_alloc_skb(odev,
+                                pkt_dev->cur_pkt_size + 64
+                                + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
        if (!skb) {
                sprintf(pkt_dev->result, "No memory");
                return NULL;
@@ -2605,8 +2606,7 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
        skb->network_header = skb->tail;
        skb->transport_header = skb->network_header + sizeof(struct iphdr);
        skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
-       skb->queue_mapping = pkt_dev->cur_queue_map;
-
+       skb_set_queue_mapping(skb, queue_map);
        iph = ip_hdr(skb);
        udph = udp_hdr(skb);
 
@@ -2631,6 +2631,8 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
        iph->protocol = IPPROTO_UDP;    /* UDP */
        iph->saddr = pkt_dev->cur_saddr;
        iph->daddr = pkt_dev->cur_daddr;
+       iph->id = htons(pkt_dev->ip_id);
+       pkt_dev->ip_id++;
        iph->frag_off = 0;
        iplen = 20 + 8 + datalen;
        iph->tot_len = htons(iplen);
@@ -2642,24 +2644,26 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
        skb->dev = odev;
        skb->pkt_type = PACKET_HOST;
 
-       if (pkt_dev->nfrags <= 0)
+       if (pkt_dev->nfrags <= 0) {
                pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
-       else {
+               memset(pgh + 1, 0, datalen - sizeof(struct pktgen_hdr));
+       } else {
                int frags = pkt_dev->nfrags;
-               int i;
+               int i, len;
 
                pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
 
                if (frags > MAX_SKB_FRAGS)
                        frags = MAX_SKB_FRAGS;
                if (datalen > frags * PAGE_SIZE) {
-                       skb_put(skb, datalen - frags * PAGE_SIZE);
+                       len = datalen - frags * PAGE_SIZE;
+                       memset(skb_put(skb, len), 0, len);
                        datalen = frags * PAGE_SIZE;
                }
 
                i = 0;
                while (datalen > 0) {
-                       struct page *page = alloc_pages(GFP_KERNEL, 0);
+                       struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
                        skb_shinfo(skb)->frags[i].page = page;
                        skb_shinfo(skb)->frags[i].page_offset = 0;
                        skb_shinfo(skb)->frags[i].size =
@@ -2696,8 +2700,9 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
                }
        }
 
-       /* Stamp the time, and sequence number, convert them to network byte order */
-
+       /* Stamp the time, and sequence number,
+        * convert them to network byte order
+        */
        if (pgh) {
                struct timeval timestamp;
 
@@ -2735,6 +2740,7 @@ static unsigned int scan_ip6(const char *s, char ip[16])
        unsigned int prefixlen = 0;
        unsigned int suffixlen = 0;
        __be32 tmp;
+       char *pos;
 
        for (i = 0; i < 16; i++)
                ip[i] = 0;
@@ -2749,12 +2755,9 @@ static unsigned int scan_ip6(const char *s, char ip[16])
                        }
                        s++;
                }
-               {
-                       char *tmp;
-                       u = simple_strtoul(s, &tmp, 16);
-                       i = tmp - s;
-               }
 
+               u = simple_strtoul(s, &pos, 16);
+               i = pos - s;
                if (!i)
                        return 0;
                if (prefixlen == 12 && s[i] == '.') {
@@ -2782,11 +2785,9 @@ static unsigned int scan_ip6(const char *s, char ip[16])
                        len++;
                } else if (suffixlen != 0)
                        break;
-               {
-                       char *tmp;
-                       u = simple_strtol(s, &tmp, 16);
-                       i = tmp - s;
-               }
+
+               u = simple_strtol(s, &pos, 16);
+               i = pos - s;
                if (!i) {
                        if (*s)
                                len--;
@@ -2900,6 +2901,7 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
        __be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
        __be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
        __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
+       u16 queue_map;
 
        if (pkt_dev->nr_labels)
                protocol = htons(ETH_P_MPLS_UC);
@@ -2910,10 +2912,12 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
        /* Update any of the values, used when we're incrementing various
         * fields.
         */
+       queue_map = pkt_dev->cur_queue_map;
        mod_cur_headers(pkt_dev);
 
-       skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
-                       pkt_dev->pkt_overhead, GFP_ATOMIC);
+       skb = __netdev_alloc_skb(odev,
+                                pkt_dev->cur_pkt_size + 64
+                                + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
        if (!skb) {
                sprintf(pkt_dev->result, "No memory");
                return NULL;
@@ -2947,13 +2951,12 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
        skb->network_header = skb->tail;
        skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
        skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
-       skb->queue_mapping = pkt_dev->cur_queue_map;
-
+       skb_set_queue_mapping(skb, queue_map);
        iph = ipv6_hdr(skb);
        udph = udp_hdr(skb);
 
        memcpy(eth, pkt_dev->hh, 12);
-       *(__be16 *) & eth[12] = protocol;
+       *(__be16 *) &eth[12] = protocol;
 
        /* Eth + IPh + UDPh + mpls */
        datalen = pkt_dev->cur_pkt_size - 14 -
@@ -3047,8 +3050,10 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
                }
        }
 
-       /* Stamp the time, and sequence number, convert them to network byte order */
-       /* should we update cloned packets too ? */
+       /* Stamp the time, and sequence number,
+        * convert them to network byte order
+        * should we update cloned packets too ?
+        */
        if (pgh) {
                struct timeval timestamp;
 
@@ -3064,8 +3069,8 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
        return skb;
 }
 
-static inline struct sk_buff *fill_packet(struct net_device *odev,
-                                         struct pktgen_dev *pkt_dev)
+static struct sk_buff *fill_packet(struct net_device *odev,
+                                  struct pktgen_dev *pkt_dev)
 {
        if (pkt_dev->flags & F_IPV6)
                return fill_packet_ipv6(odev, pkt_dev);
@@ -3103,9 +3108,9 @@ static void pktgen_run(struct pktgen_thread *t)
                        pktgen_clear_counters(pkt_dev);
                        pkt_dev->running = 1;   /* Cranke yeself! */
                        pkt_dev->skb = NULL;
-                       pkt_dev->started_at = getCurUs();
-                       pkt_dev->next_tx_us = getCurUs();       /* Transmit immediately */
-                       pkt_dev->next_tx_ns = 0;
+                       pkt_dev->started_at =
+                               pkt_dev->next_tx = ktime_now();
+
                        set_pkt_overhead(pkt_dev);
 
                        strcpy(pkt_dev->result, "Starting");
@@ -3132,17 +3137,14 @@ static void pktgen_stop_all_threads_ifs(void)
        mutex_unlock(&pktgen_thread_lock);
 }
 
-static int thread_is_running(struct pktgen_thread *t)
+static int thread_is_running(const struct pktgen_thread *t)
 {
-       struct pktgen_dev *pkt_dev;
-       int res = 0;
+       const struct pktgen_dev *pkt_dev;
 
        list_for_each_entry(pkt_dev, &t->if_list, list)
-               if (pkt_dev->running) {
-                       res = 1;
-                       break;
-               }
-       return res;
+               if (pkt_dev->running)
+                       return 1;
+       return 0;
 }
 
 static int pktgen_wait_thread_run(struct pktgen_thread *t)
@@ -3199,35 +3201,48 @@ static void pktgen_run_all_threads(void)
 
        mutex_unlock(&pktgen_thread_lock);
 
-       schedule_timeout_interruptible(msecs_to_jiffies(125));  /* Propagate thread->control  */
+       /* Propagate thread->control  */
+       schedule_timeout_interruptible(msecs_to_jiffies(125));
 
        pktgen_wait_all_threads_run();
 }
 
-static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
+static void pktgen_reset_all_threads(void)
 {
-       __u64 total_us, bps, mbps, pps, idle;
-       char *p = pkt_dev->result;
+       struct pktgen_thread *t;
 
-       total_us = pkt_dev->stopped_at - pkt_dev->started_at;
+       pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
 
-       idle = pkt_dev->idle_acc;
+       mutex_lock(&pktgen_thread_lock);
 
-       p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
-                    (unsigned long long)total_us,
-                    (unsigned long long)(total_us - idle),
-                    (unsigned long long)idle,
-                    (unsigned long long)pkt_dev->sofar,
-                    pkt_dev->cur_pkt_size, nr_frags);
+       list_for_each_entry(t, &pktgen_threads, th_list)
+               t->control |= (T_REMDEVALL);
 
-       pps = pkt_dev->sofar * USEC_PER_SEC;
+       mutex_unlock(&pktgen_thread_lock);
 
-       while ((total_us >> 32) != 0) {
-               pps >>= 1;
-               total_us >>= 1;
-       }
+       /* Propagate thread->control  */
+       schedule_timeout_interruptible(msecs_to_jiffies(125));
+
+       pktgen_wait_all_threads_run();
+}
+
+static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
+{
+       __u64 bps, mbps, pps;
+       char *p = pkt_dev->result;
+       ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
+                                   pkt_dev->started_at);
+       ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
+
+       p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
+                    (unsigned long long)ktime_to_us(elapsed),
+                    (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
+                    (unsigned long long)ktime_to_us(idle),
+                    (unsigned long long)pkt_dev->sofar,
+                    pkt_dev->cur_pkt_size, nr_frags);
 
-       do_div(pps, total_us);
+       pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
+                       ktime_to_ns(elapsed));
 
        bps = pps * 8 * pkt_dev->cur_pkt_size;
 
@@ -3241,7 +3256,6 @@ static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
 }
 
 /* Set stopped-at timer, remove from running list, do counters & statistics */
-
 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
 {
        int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
@@ -3252,7 +3266,9 @@ static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
                return -EINVAL;
        }
 
-       pkt_dev->stopped_at = getCurUs();
+       kfree_skb(pkt_dev->skb);
+       pkt_dev->skb = NULL;
+       pkt_dev->stopped_at = ktime_now();
        pkt_dev->running = 0;
 
        show_results(pkt_dev, nr_frags);
@@ -3271,7 +3287,7 @@ static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
                        continue;
                if (best == NULL)
                        best = pkt_dev;
-               else if (pkt_dev->next_tx_us < best->next_tx_us)
+               else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
                        best = pkt_dev;
        }
        if_unlock(t);
@@ -3288,10 +3304,6 @@ static void pktgen_stop(struct pktgen_thread *t)
 
        list_for_each_entry(pkt_dev, &t->if_list, list) {
                pktgen_stop_device(pkt_dev);
-               if (pkt_dev->skb)
-                       kfree_skb(pkt_dev->skb);
-
-               pkt_dev->skb = NULL;
        }
 
        if_unlock(t);
@@ -3316,8 +3328,7 @@ static void pktgen_rem_one_if(struct pktgen_thread *t)
                if (!cur->removal_mark)
                        continue;
 
-               if (cur->skb)
-                       kfree_skb(cur->skb);
+               kfree_skb(cur->skb);
                cur->skb = NULL;
 
                pktgen_remove_device(t, cur);
@@ -3341,8 +3352,7 @@ static void pktgen_rem_all_ifs(struct pktgen_thread *t)
        list_for_each_safe(q, n, &t->if_list) {
                cur = list_entry(q, struct pktgen_dev, list);
 
-               if (cur->skb)
-                       kfree_skb(cur->skb);
+               kfree_skb(cur->skb);
                cur->skb = NULL;
 
                pktgen_remove_device(t, cur);
@@ -3364,145 +3374,114 @@ static void pktgen_rem_thread(struct pktgen_thread *t)
        mutex_unlock(&pktgen_thread_lock);
 }
 
-static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
+static void pktgen_resched(struct pktgen_dev *pkt_dev)
 {
-       struct net_device *odev = NULL;
-       __u64 idle_start = 0;
-       int ret;
-
-       odev = pkt_dev->odev;
-
-       if (pkt_dev->delay_us || pkt_dev->delay_ns) {
-               u64 now;
-
-               now = getCurUs();
-               if (now < pkt_dev->next_tx_us)
-                       spin(pkt_dev, pkt_dev->next_tx_us);
+       ktime_t idle_start = ktime_now();
+       schedule();
+       pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
+}
 
-               /* This is max DELAY, this has special meaning of
-                * "never transmit"
-                */
-               if (pkt_dev->delay_us == 0x7FFFFFFF) {
-                       pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
-                       pkt_dev->next_tx_ns = pkt_dev->delay_ns;
-                       goto out;
-               }
-       }
+static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
+{
+       ktime_t idle_start = ktime_now();
 
-       if ((netif_queue_stopped(odev) ||
-            (pkt_dev->skb &&
-             netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) ||
-           need_resched()) {
-               idle_start = getCurUs();
+       while (atomic_read(&(pkt_dev->skb->users)) != 1) {
+               if (signal_pending(current))
+                       break;
 
-               if (!netif_running(odev)) {
-                       pktgen_stop_device(pkt_dev);
-                       if (pkt_dev->skb)
-                               kfree_skb(pkt_dev->skb);
-                       pkt_dev->skb = NULL;
-                       goto out;
-               }
                if (need_resched())
-                       schedule();
-
-               pkt_dev->idle_acc += getCurUs() - idle_start;
-
-               if (netif_queue_stopped(odev) ||
-                   netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
-                       pkt_dev->next_tx_us = getCurUs();       /* TODO */
-                       pkt_dev->next_tx_ns = 0;
-                       goto out;       /* Try the next interface */
-               }
+                       pktgen_resched(pkt_dev);
+               else
+                       cpu_relax();
        }
+       pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
+}
 
-       if (pkt_dev->last_ok || !pkt_dev->skb) {
-               if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
-                   || (!pkt_dev->skb)) {
-                       /* build a new pkt */
-                       if (pkt_dev->skb)
-                               kfree_skb(pkt_dev->skb);
+static void pktgen_xmit(struct pktgen_dev *pkt_dev)
+{
+       struct net_device *odev = pkt_dev->odev;
+       netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
+               = odev->netdev_ops->ndo_start_xmit;
+       struct netdev_queue *txq;
+       u16 queue_map;
+       int ret;
 
-                       pkt_dev->skb = fill_packet(odev, pkt_dev);
-                       if (pkt_dev->skb == NULL) {
-                               printk(KERN_ERR "pktgen: ERROR: couldn't "
-                                      "allocate skb in fill_packet.\n");
-                               schedule();
-                               pkt_dev->clone_count--; /* back out increment, OOM */
-                               goto out;
-                       }
-                       pkt_dev->allocated_skbs++;
-                       pkt_dev->clone_count = 0;       /* reset counter */
-               }
+       /* If device is offline, then don't send */
+       if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
+               pktgen_stop_device(pkt_dev);
+               return;
        }
 
-       netif_tx_lock_bh(odev);
-       if (!netif_queue_stopped(odev) &&
-           !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
-
-               atomic_inc(&(pkt_dev->skb->users));
-             retry_now:
-               ret = odev->hard_start_xmit(pkt_dev->skb, odev);
-               if (likely(ret == NETDEV_TX_OK)) {
-                       pkt_dev->last_ok = 1;
-                       pkt_dev->sofar++;
-                       pkt_dev->seq_num++;
-                       pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
-
-               } else if (ret == NETDEV_TX_LOCKED
-                          && (odev->features & NETIF_F_LLTX)) {
-                       cpu_relax();
-                       goto retry_now;
-               } else {        /* Retry it next time */
-
-                       atomic_dec(&(pkt_dev->skb->users));
+       /* This is max DELAY, this has special meaning of
+        * "never transmit"
+        */
+       if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
+               pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
+               return;
+       }
 
-                       if (debug && net_ratelimit())
-                               printk(KERN_INFO "pktgen: Hard xmit error\n");
+       /* If no skb or clone count exhausted then get new one */
+       if (!pkt_dev->skb || (pkt_dev->last_ok &&
+                             ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
+               /* build a new pkt */
+               kfree_skb(pkt_dev->skb);
 
-                       pkt_dev->errors++;
-                       pkt_dev->last_ok = 0;
+               pkt_dev->skb = fill_packet(odev, pkt_dev);
+               if (pkt_dev->skb == NULL) {
+                       printk(KERN_ERR "pktgen: ERROR: couldn't "
+                              "allocate skb in fill_packet.\n");
+                       schedule();
+                       pkt_dev->clone_count--; /* back out increment, OOM */
+                       return;
                }
 
-               pkt_dev->next_tx_us = getCurUs();
-               pkt_dev->next_tx_ns = 0;
+               pkt_dev->allocated_skbs++;
+               pkt_dev->clone_count = 0;       /* reset counter */
+       }
+
+       if (pkt_dev->delay && pkt_dev->last_ok)
+               spin(pkt_dev, pkt_dev->next_tx);
 
-               pkt_dev->next_tx_us += pkt_dev->delay_us;
-               pkt_dev->next_tx_ns += pkt_dev->delay_ns;
+       queue_map = skb_get_queue_mapping(pkt_dev->skb);
+       txq = netdev_get_tx_queue(odev, queue_map);
 
-               if (pkt_dev->next_tx_ns > 1000) {
-                       pkt_dev->next_tx_us++;
-                       pkt_dev->next_tx_ns -= 1000;
-               }
-       }
+       __netif_tx_lock_bh(txq);
+       atomic_inc(&(pkt_dev->skb->users));
 
-       else {                  /* Retry it next time */
+       if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)))
+               ret = NETDEV_TX_BUSY;
+       else
+               ret = (*xmit)(pkt_dev->skb, odev);
+
+       switch (ret) {
+       case NETDEV_TX_OK:
+               txq_trans_update(txq);
+               pkt_dev->last_ok = 1;
+               pkt_dev->sofar++;
+               pkt_dev->seq_num++;
+               pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
+               break;
+       default: /* Drivers are not supposed to return other values! */
+               if (net_ratelimit())
+                       pr_info("pktgen: %s xmit error: %d\n",
+                               odev->name, ret);
+               pkt_dev->errors++;
+               /* fallthru */
+       case NETDEV_TX_LOCKED:
+       case NETDEV_TX_BUSY:
+               /* Retry it next time */
+               atomic_dec(&(pkt_dev->skb->users));
                pkt_dev->last_ok = 0;
-               pkt_dev->next_tx_us = getCurUs();       /* TODO */
-               pkt_dev->next_tx_ns = 0;
        }
-
-       netif_tx_unlock_bh(odev);
+       __netif_tx_unlock_bh(txq);
 
        /* If pkt_dev->count is zero, then run forever */
        if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
-               if (atomic_read(&(pkt_dev->skb->users)) != 1) {
-                       idle_start = getCurUs();
-                       while (atomic_read(&(pkt_dev->skb->users)) != 1) {
-                               if (signal_pending(current)) {
-                                       break;
-                               }
-                               schedule();
-                       }
-                       pkt_dev->idle_acc += getCurUs() - idle_start;
-               }
+               pktgen_wait_for_skb(pkt_dev);
 
                /* Done with this */
                pktgen_stop_device(pkt_dev);
-               if (pkt_dev->skb)
-                       kfree_skb(pkt_dev->skb);
-               pkt_dev->skb = NULL;
        }
-out:;
 }
 
 /*
@@ -3519,8 +3498,10 @@ static int pktgen_thread_worker(void *arg)
        BUG_ON(smp_processor_id() != cpu);
 
        init_waitqueue_head(&t->queue);
+       complete(&t->start_done);
 
-       pr_debug("pktgen: starting pktgen/%d:  pid=%d\n", cpu, current->pid);
+       pr_debug("pktgen: starting pktgen/%d:  pid=%d\n",
+                cpu, task_pid_nr(current));
 
        set_current_state(TASK_INTERRUPTIBLE);
 
@@ -3529,20 +3510,24 @@ static int pktgen_thread_worker(void *arg)
        while (!kthread_should_stop()) {
                pkt_dev = next_to_run(t);
 
-               if (!pkt_dev &&
-                   (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
-                   == 0) {
-                       prepare_to_wait(&(t->queue), &wait,
-                                       TASK_INTERRUPTIBLE);
-                       schedule_timeout(HZ / 10);
-                       finish_wait(&(t->queue), &wait);
+               if (unlikely(!pkt_dev && t->control == 0)) {
+                       wait_event_interruptible_timeout(t->queue,
+                                                        t->control != 0,
+                                                        HZ/10);
+                       continue;
                }
 
                __set_current_state(TASK_RUNNING);
 
-               if (pkt_dev)
+               if (likely(pkt_dev)) {
                        pktgen_xmit(pkt_dev);
 
+                       if (need_resched())
+                               pktgen_resched(pkt_dev);
+                       else
+                               cpu_relax();
+               }
+
                if (t->control & T_STOP) {
                        pktgen_stop(t);
                        t->control &= ~(T_STOP);
@@ -3655,8 +3640,7 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
        pkt_dev->max_pkt_size = ETH_ZLEN;
        pkt_dev->nfrags = 0;
        pkt_dev->clone_skb = pg_clone_skb_d;
-       pkt_dev->delay_us = pg_delay_d / 1000;
-       pkt_dev->delay_ns = pg_delay_d % 1000;
+       pkt_dev->delay = pg_delay_d;
        pkt_dev->count = pg_count_d;
        pkt_dev->sofar = 0;
        pkt_dev->udp_src_min = 9;       /* sink port */
@@ -3675,15 +3659,14 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
        if (err)
                goto out1;
 
-       pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
+       pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
+                                         &pktgen_if_fops, pkt_dev);
        if (!pkt_dev->entry) {
                printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
                       PG_PROC_DIR, ifname);
                err = -EINVAL;
                goto out2;
        }
-       pkt_dev->entry->proc_fops = &pktgen_if_fops;
-       pkt_dev->entry->data = pkt_dev;
 #ifdef CONFIG_XFRM
        pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
        pkt_dev->ipsproto = IPPROTO_ESP;
@@ -3696,8 +3679,7 @@ out1:
 #ifdef CONFIG_XFRM
        free_SAs(pkt_dev);
 #endif
-       if (pkt_dev->flows)
-               vfree(pkt_dev->flows);
+       vfree(pkt_dev->flows);
        kfree(pkt_dev);
        return err;
 }
@@ -3721,6 +3703,7 @@ static int __init pktgen_create_thread(int cpu)
        INIT_LIST_HEAD(&t->if_list);
 
        list_add_tail(&t->th_list, &pktgen_threads);
+       init_completion(&t->start_done);
 
        p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
        if (IS_ERR(p)) {
@@ -3733,7 +3716,8 @@ static int __init pktgen_create_thread(int cpu)
        kthread_bind(p, cpu);
        t->tsk = p;
 
-       pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
+       pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
+                             &pktgen_thread_fops, t);
        if (!pe) {
                printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
                       PG_PROC_DIR, t->tsk->comm);
@@ -3743,10 +3727,8 @@ static int __init pktgen_create_thread(int cpu)
                return -EINVAL;
        }
 
-       pe->proc_fops = &pktgen_thread_fops;
-       pe->data = t;
-
        wake_up_process(p);
+       wait_for_completion(&t->start_done);
 
        return 0;
 }
@@ -3796,8 +3778,7 @@ static int pktgen_remove_device(struct pktgen_thread *t,
 #ifdef CONFIG_XFRM
        free_SAs(pkt_dev);
 #endif
-       if (pkt_dev->flows)
-               vfree(pkt_dev->flows);
+       vfree(pkt_dev->flows);
        kfree(pkt_dev);
        return 0;
 }
@@ -3812,9 +3793,8 @@ static int __init pg_init(void)
        pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
        if (!pg_proc_dir)
                return -ENODEV;
-       pg_proc_dir->owner = THIS_MODULE;
 
-       pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
+       pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
        if (pe == NULL) {
                printk(KERN_ERR "pktgen: ERROR: cannot create %s "
                       "procfs entry.\n", PGCTRL);
@@ -3822,9 +3802,6 @@ static int __init pg_init(void)
                return -EINVAL;
        }
 
-       pe->proc_fops = &pktgen_fops;
-       pe->data = NULL;
-
        /* Register us to receive netdevice events */
        register_netdevice_notifier(&pktgen_notifier_block);
 
@@ -3875,10 +3852,15 @@ static void __exit pg_cleanup(void)
 module_init(pg_init);
 module_exit(pg_cleanup);
 
-MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
+MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
 MODULE_DESCRIPTION("Packet Generator tool");
 MODULE_LICENSE("GPL");
+MODULE_VERSION(VERSION);
 module_param(pg_count_d, int, 0);
+MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
 module_param(pg_delay_d, int, 0);
+MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
 module_param(pg_clone_skb_d, int, 0);
+MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
 module_param(debug, int, 0);
+MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");