ACPI: thinkpad-acpi: rename pci HID constant
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / core / pktgen.c
blob9cd3a1cb60ef33b485075cb4ce49183d13f9c67b
1 /*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
23 * Additional hacking by:
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
52 * clones.
54 * Also moved to /proc/net/pktgen/
55 * --ro
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
67 * The new operation:
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * to /proc gives result code thats should be read be the "writer".
78 * For practical use this should be no problem.
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
82 * --ro
84 * Fix refcount off by one if first packet fails, potential null deref,
85 * memleak 030710- KJP
87 * First "ranges" functionality for ipv6 030726 --ro
89 * Included flow support. 030802 ANK.
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
99 * Randy Dunlap fixed u64 printk compiler waring
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
108 * 050103
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
115 #include <linux/sys.h>
116 #include <linux/types.h>
117 #include <linux/module.h>
118 #include <linux/moduleparam.h>
119 #include <linux/kernel.h>
120 #include <linux/mutex.h>
121 #include <linux/sched.h>
122 #include <linux/slab.h>
123 #include <linux/vmalloc.h>
124 #include <linux/unistd.h>
125 #include <linux/string.h>
126 #include <linux/ptrace.h>
127 #include <linux/errno.h>
128 #include <linux/ioport.h>
129 #include <linux/interrupt.h>
130 #include <linux/capability.h>
131 #include <linux/freezer.h>
132 #include <linux/delay.h>
133 #include <linux/timer.h>
134 #include <linux/list.h>
135 #include <linux/init.h>
136 #include <linux/skbuff.h>
137 #include <linux/netdevice.h>
138 #include <linux/inet.h>
139 #include <linux/inetdevice.h>
140 #include <linux/rtnetlink.h>
141 #include <linux/if_arp.h>
142 #include <linux/if_vlan.h>
143 #include <linux/in.h>
144 #include <linux/ip.h>
145 #include <linux/ipv6.h>
146 #include <linux/udp.h>
147 #include <linux/proc_fs.h>
148 #include <linux/seq_file.h>
149 #include <linux/wait.h>
150 #include <linux/etherdevice.h>
151 #include <linux/kthread.h>
152 #include <net/checksum.h>
153 #include <net/ipv6.h>
154 #include <net/addrconf.h>
155 #include <asm/byteorder.h>
156 #include <linux/rcupdate.h>
157 #include <asm/bitops.h>
158 #include <asm/io.h>
159 #include <asm/dma.h>
160 #include <asm/uaccess.h>
161 #include <asm/div64.h> /* do_div */
162 #include <asm/timex.h>
164 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
166 /* The buckets are exponential in 'width' */
167 #define LAT_BUCKETS_MAX 32
168 #define IP_NAME_SZ 32
169 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
170 #define MPLS_STACK_BOTTOM htonl(0x00000100)
172 /* Device flag bits */
173 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
174 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
175 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
176 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
177 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
178 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
179 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
180 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
181 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
182 #define F_VID_RND (1<<9) /* Random VLAN ID */
183 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
185 /* Thread control flag bits */
186 #define T_TERMINATE (1<<0)
187 #define T_STOP (1<<1) /* Stop run */
188 #define T_RUN (1<<2) /* Start run */
189 #define T_REMDEVALL (1<<3) /* Remove all devs */
190 #define T_REMDEV (1<<4) /* Remove one dev */
192 /* If lock -- can be removed after some work */
193 #define if_lock(t) spin_lock(&(t->if_lock));
194 #define if_unlock(t) spin_unlock(&(t->if_lock));
196 /* Used to help with determining the pkts on receive */
197 #define PKTGEN_MAGIC 0xbe9be955
198 #define PG_PROC_DIR "pktgen"
199 #define PGCTRL "pgctrl"
200 static struct proc_dir_entry *pg_proc_dir = NULL;
202 #define MAX_CFLOWS 65536
204 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
205 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
207 struct flow_state {
208 __be32 cur_daddr;
209 int count;
212 struct pktgen_dev {
214 * Try to keep frequent/infrequent used vars. separated.
216 struct proc_dir_entry *entry; /* proc file */
217 struct pktgen_thread *pg_thread;/* the owner */
218 struct list_head list; /* Used for chaining in the thread's run-queue */
220 int running; /* if this changes to false, the test will stop */
222 /* If min != max, then we will either do a linear iteration, or
223 * we will do a random selection from within the range.
225 __u32 flags;
226 int removal_mark; /* non-zero => the device is marked for
227 * removal by worker thread */
229 int min_pkt_size; /* = ETH_ZLEN; */
230 int max_pkt_size; /* = ETH_ZLEN; */
231 int nfrags;
232 __u32 delay_us; /* Default delay */
233 __u32 delay_ns;
234 __u64 count; /* Default No packets to send */
235 __u64 sofar; /* How many pkts we've sent so far */
236 __u64 tx_bytes; /* How many bytes we've transmitted */
237 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
239 /* runtime counters relating to clone_skb */
240 __u64 next_tx_us; /* timestamp of when to tx next */
241 __u32 next_tx_ns;
243 __u64 allocated_skbs;
244 __u32 clone_count;
245 int last_ok; /* Was last skb sent?
246 * Or a failed transmit of some sort? This will keep
247 * sequence numbers in order, for example.
249 __u64 started_at; /* micro-seconds */
250 __u64 stopped_at; /* micro-seconds */
251 __u64 idle_acc; /* micro-seconds */
252 __u32 seq_num;
254 int clone_skb; /* Use multiple SKBs during packet gen. If this number
255 * is greater than 1, then that many copies of the same
256 * packet will be sent before a new packet is allocated.
257 * For instance, if you want to send 1024 identical packets
258 * before creating a new packet, set clone_skb to 1024.
261 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
262 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
263 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
264 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
266 struct in6_addr in6_saddr;
267 struct in6_addr in6_daddr;
268 struct in6_addr cur_in6_daddr;
269 struct in6_addr cur_in6_saddr;
270 /* For ranges */
271 struct in6_addr min_in6_daddr;
272 struct in6_addr max_in6_daddr;
273 struct in6_addr min_in6_saddr;
274 struct in6_addr max_in6_saddr;
276 /* If we're doing ranges, random or incremental, then this
277 * defines the min/max for those ranges.
279 __be32 saddr_min; /* inclusive, source IP address */
280 __be32 saddr_max; /* exclusive, source IP address */
281 __be32 daddr_min; /* inclusive, dest IP address */
282 __be32 daddr_max; /* exclusive, dest IP address */
284 __u16 udp_src_min; /* inclusive, source UDP port */
285 __u16 udp_src_max; /* exclusive, source UDP port */
286 __u16 udp_dst_min; /* inclusive, dest UDP port */
287 __u16 udp_dst_max; /* exclusive, dest UDP port */
289 /* DSCP + ECN */
290 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
291 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
293 /* MPLS */
294 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
295 __be32 labels[MAX_MPLS_LABELS];
297 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
298 __u8 vlan_p;
299 __u8 vlan_cfi;
300 __u16 vlan_id; /* 0xffff means no vlan tag */
302 __u8 svlan_p;
303 __u8 svlan_cfi;
304 __u16 svlan_id; /* 0xffff means no svlan tag */
306 __u32 src_mac_count; /* How many MACs to iterate through */
307 __u32 dst_mac_count; /* How many MACs to iterate through */
309 unsigned char dst_mac[ETH_ALEN];
310 unsigned char src_mac[ETH_ALEN];
312 __u32 cur_dst_mac_offset;
313 __u32 cur_src_mac_offset;
314 __be32 cur_saddr;
315 __be32 cur_daddr;
316 __u16 cur_udp_dst;
317 __u16 cur_udp_src;
318 __u32 cur_pkt_size;
320 __u8 hh[14];
321 /* = {
322 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
324 We fill in SRC address later
325 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326 0x08, 0x00
329 __u16 pad; /* pad out the hh struct to an even 16 bytes */
331 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
332 * are transmitting the same one multiple times
334 struct net_device *odev; /* The out-going device. Note that the device should
335 * have it's pg_info pointer pointing back to this
336 * device. This will be set when the user specifies
337 * the out-going device name (not when the inject is
338 * started as it used to do.)
340 struct flow_state *flows;
341 unsigned cflows; /* Concurrent flows (config) */
342 unsigned lflow; /* Flow length (config) */
343 unsigned nflows; /* accumulated flows (stats) */
345 char result[512];
348 struct pktgen_hdr {
349 __be32 pgh_magic;
350 __be32 seq_num;
351 __be32 tv_sec;
352 __be32 tv_usec;
355 struct pktgen_thread {
356 spinlock_t if_lock;
357 struct list_head if_list; /* All device here */
358 struct list_head th_list;
359 struct task_struct *tsk;
360 char result[512];
361 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
363 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
365 u32 control;
366 int pid;
367 int cpu;
369 wait_queue_head_t queue;
372 #define REMOVE 1
373 #define FIND 0
375 /* This code works around the fact that do_div cannot handle two 64-bit
376 numbers, and regular 64-bit division doesn't work on x86 kernels.
377 --Ben
380 #define PG_DIV 0
382 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
383 * Function copied/adapted/optimized from:
385 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
387 * Copyright 1994, University of Cambridge Computer Laboratory
388 * All Rights Reserved.
391 static inline s64 divremdi3(s64 x, s64 y, int type)
393 u64 a = (x < 0) ? -x : x;
394 u64 b = (y < 0) ? -y : y;
395 u64 res = 0, d = 1;
397 if (b > 0) {
398 while (b < a) {
399 b <<= 1;
400 d <<= 1;
404 do {
405 if (a >= b) {
406 a -= b;
407 res += d;
409 b >>= 1;
410 d >>= 1;
412 while (d);
414 if (PG_DIV == type) {
415 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
416 } else {
417 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
421 /* End of hacks to deal with 64-bit math on x86 */
423 /** Convert to milliseconds */
424 static inline __u64 tv_to_ms(const struct timeval *tv)
426 __u64 ms = tv->tv_usec / 1000;
427 ms += (__u64) tv->tv_sec * (__u64) 1000;
428 return ms;
431 /** Convert to micro-seconds */
432 static inline __u64 tv_to_us(const struct timeval *tv)
434 __u64 us = tv->tv_usec;
435 us += (__u64) tv->tv_sec * (__u64) 1000000;
436 return us;
439 static inline __u64 pg_div(__u64 n, __u32 base)
441 __u64 tmp = n;
442 do_div(tmp, base);
443 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
444 n, base, tmp); */
445 return tmp;
448 static inline __u64 pg_div64(__u64 n, __u64 base)
450 __u64 tmp = n;
452 * How do we know if the architecture we are running on
453 * supports division with 64 bit base?
456 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
458 do_div(tmp, base);
459 #else
460 tmp = divremdi3(n, base, PG_DIV);
461 #endif
462 return tmp;
465 static inline __u64 getCurMs(void)
467 struct timeval tv;
468 do_gettimeofday(&tv);
469 return tv_to_ms(&tv);
472 static inline __u64 getCurUs(void)
474 struct timeval tv;
475 do_gettimeofday(&tv);
476 return tv_to_us(&tv);
479 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
481 return tv_to_us(a) - tv_to_us(b);
484 /* old include end */
486 static char version[] __initdata = VERSION;
488 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
489 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
490 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
491 const char *ifname);
492 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
493 static void pktgen_run_all_threads(void);
494 static void pktgen_stop_all_threads_ifs(void);
495 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
496 static void pktgen_stop(struct pktgen_thread *t);
497 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
499 static unsigned int scan_ip6(const char *s, char ip[16]);
500 static unsigned int fmt_ip6(char *s, const char ip[16]);
502 /* Module parameters, defaults. */
503 static int pg_count_d = 1000; /* 1000 pkts by default */
504 static int pg_delay_d;
505 static int pg_clone_skb_d;
506 static int debug;
508 static DEFINE_MUTEX(pktgen_thread_lock);
509 static LIST_HEAD(pktgen_threads);
511 static struct notifier_block pktgen_notifier_block = {
512 .notifier_call = pktgen_device_event,
516 * /proc handling functions
520 static int pgctrl_show(struct seq_file *seq, void *v)
522 seq_puts(seq, VERSION);
523 return 0;
526 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
527 size_t count, loff_t * ppos)
529 int err = 0;
530 char data[128];
532 if (!capable(CAP_NET_ADMIN)) {
533 err = -EPERM;
534 goto out;
537 if (count > sizeof(data))
538 count = sizeof(data);
540 if (copy_from_user(data, buf, count)) {
541 err = -EFAULT;
542 goto out;
544 data[count - 1] = 0; /* Make string */
546 if (!strcmp(data, "stop"))
547 pktgen_stop_all_threads_ifs();
549 else if (!strcmp(data, "start"))
550 pktgen_run_all_threads();
552 else
553 printk("pktgen: Unknown command: %s\n", data);
555 err = count;
557 out:
558 return err;
561 static int pgctrl_open(struct inode *inode, struct file *file)
563 return single_open(file, pgctrl_show, PDE(inode)->data);
566 static const struct file_operations pktgen_fops = {
567 .owner = THIS_MODULE,
568 .open = pgctrl_open,
569 .read = seq_read,
570 .llseek = seq_lseek,
571 .write = pgctrl_write,
572 .release = single_release,
575 static int pktgen_if_show(struct seq_file *seq, void *v)
577 int i;
578 struct pktgen_dev *pkt_dev = seq->private;
579 __u64 sa;
580 __u64 stopped;
581 __u64 now = getCurUs();
583 seq_printf(seq,
584 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
585 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
586 pkt_dev->max_pkt_size);
588 seq_printf(seq,
589 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
590 pkt_dev->nfrags,
591 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
592 pkt_dev->clone_skb, pkt_dev->odev->name);
594 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
595 pkt_dev->lflow);
597 if (pkt_dev->flags & F_IPV6) {
598 char b1[128], b2[128], b3[128];
599 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
600 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
601 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
602 seq_printf(seq,
603 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
604 b2, b3);
606 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
607 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
608 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
609 seq_printf(seq,
610 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
611 b2, b3);
613 } else
614 seq_printf(seq,
615 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
616 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
617 pkt_dev->src_max);
619 seq_puts(seq, " src_mac: ");
621 if (is_zero_ether_addr(pkt_dev->src_mac))
622 for (i = 0; i < 6; i++)
623 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
624 i == 5 ? " " : ":");
625 else
626 for (i = 0; i < 6; i++)
627 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
628 i == 5 ? " " : ":");
630 seq_printf(seq, "dst_mac: ");
631 for (i = 0; i < 6; i++)
632 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
633 i == 5 ? "\n" : ":");
635 seq_printf(seq,
636 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
637 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
638 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
640 seq_printf(seq,
641 " src_mac_count: %d dst_mac_count: %d\n",
642 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
644 if (pkt_dev->nr_labels) {
645 unsigned i;
646 seq_printf(seq, " mpls: ");
647 for (i = 0; i < pkt_dev->nr_labels; i++)
648 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
649 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
652 if (pkt_dev->vlan_id != 0xffff) {
653 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
654 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
657 if (pkt_dev->svlan_id != 0xffff) {
658 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
659 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
662 if (pkt_dev->tos) {
663 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
666 if (pkt_dev->traffic_class) {
667 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
670 seq_printf(seq, " Flags: ");
672 if (pkt_dev->flags & F_IPV6)
673 seq_printf(seq, "IPV6 ");
675 if (pkt_dev->flags & F_IPSRC_RND)
676 seq_printf(seq, "IPSRC_RND ");
678 if (pkt_dev->flags & F_IPDST_RND)
679 seq_printf(seq, "IPDST_RND ");
681 if (pkt_dev->flags & F_TXSIZE_RND)
682 seq_printf(seq, "TXSIZE_RND ");
684 if (pkt_dev->flags & F_UDPSRC_RND)
685 seq_printf(seq, "UDPSRC_RND ");
687 if (pkt_dev->flags & F_UDPDST_RND)
688 seq_printf(seq, "UDPDST_RND ");
690 if (pkt_dev->flags & F_MPLS_RND)
691 seq_printf(seq, "MPLS_RND ");
693 if (pkt_dev->flags & F_MACSRC_RND)
694 seq_printf(seq, "MACSRC_RND ");
696 if (pkt_dev->flags & F_MACDST_RND)
697 seq_printf(seq, "MACDST_RND ");
699 if (pkt_dev->flags & F_VID_RND)
700 seq_printf(seq, "VID_RND ");
702 if (pkt_dev->flags & F_SVID_RND)
703 seq_printf(seq, "SVID_RND ");
705 seq_puts(seq, "\n");
707 sa = pkt_dev->started_at;
708 stopped = pkt_dev->stopped_at;
709 if (pkt_dev->running)
710 stopped = now; /* not really stopped, more like last-running-at */
712 seq_printf(seq,
713 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
714 (unsigned long long)pkt_dev->sofar,
715 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
716 (unsigned long long)stopped,
717 (unsigned long long)pkt_dev->idle_acc);
719 seq_printf(seq,
720 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
721 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
722 pkt_dev->cur_src_mac_offset);
724 if (pkt_dev->flags & F_IPV6) {
725 char b1[128], b2[128];
726 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
727 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
728 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
729 } else
730 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
731 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
733 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
734 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
736 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
738 if (pkt_dev->result[0])
739 seq_printf(seq, "Result: %s\n", pkt_dev->result);
740 else
741 seq_printf(seq, "Result: Idle\n");
743 return 0;
747 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
749 int i = 0;
750 *num = 0;
752 for (; i < maxlen; i++) {
753 char c;
754 *num <<= 4;
755 if (get_user(c, &user_buffer[i]))
756 return -EFAULT;
757 if ((c >= '0') && (c <= '9'))
758 *num |= c - '0';
759 else if ((c >= 'a') && (c <= 'f'))
760 *num |= c - 'a' + 10;
761 else if ((c >= 'A') && (c <= 'F'))
762 *num |= c - 'A' + 10;
763 else
764 break;
766 return i;
769 static int count_trail_chars(const char __user * user_buffer,
770 unsigned int maxlen)
772 int i;
774 for (i = 0; i < maxlen; i++) {
775 char c;
776 if (get_user(c, &user_buffer[i]))
777 return -EFAULT;
778 switch (c) {
779 case '\"':
780 case '\n':
781 case '\r':
782 case '\t':
783 case ' ':
784 case '=':
785 break;
786 default:
787 goto done;
790 done:
791 return i;
794 static unsigned long num_arg(const char __user * user_buffer,
795 unsigned long maxlen, unsigned long *num)
797 int i = 0;
798 *num = 0;
800 for (; i < maxlen; i++) {
801 char c;
802 if (get_user(c, &user_buffer[i]))
803 return -EFAULT;
804 if ((c >= '0') && (c <= '9')) {
805 *num *= 10;
806 *num += c - '0';
807 } else
808 break;
810 return i;
813 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
815 int i = 0;
817 for (; i < maxlen; i++) {
818 char c;
819 if (get_user(c, &user_buffer[i]))
820 return -EFAULT;
821 switch (c) {
822 case '\"':
823 case '\n':
824 case '\r':
825 case '\t':
826 case ' ':
827 goto done_str;
828 break;
829 default:
830 break;
833 done_str:
834 return i;
837 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
839 unsigned n = 0;
840 char c;
841 ssize_t i = 0;
842 int len;
844 pkt_dev->nr_labels = 0;
845 do {
846 __u32 tmp;
847 len = hex32_arg(&buffer[i], 8, &tmp);
848 if (len <= 0)
849 return len;
850 pkt_dev->labels[n] = htonl(tmp);
851 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
852 pkt_dev->flags |= F_MPLS_RND;
853 i += len;
854 if (get_user(c, &buffer[i]))
855 return -EFAULT;
856 i++;
857 n++;
858 if (n >= MAX_MPLS_LABELS)
859 return -E2BIG;
860 } while (c == ',');
862 pkt_dev->nr_labels = n;
863 return i;
866 static ssize_t pktgen_if_write(struct file *file,
867 const char __user * user_buffer, size_t count,
868 loff_t * offset)
870 struct seq_file *seq = (struct seq_file *)file->private_data;
871 struct pktgen_dev *pkt_dev = seq->private;
872 int i = 0, max, len;
873 char name[16], valstr[32];
874 unsigned long value = 0;
875 char *pg_result = NULL;
876 int tmp = 0;
877 char buf[128];
879 pg_result = &(pkt_dev->result[0]);
881 if (count < 1) {
882 printk("pktgen: wrong command format\n");
883 return -EINVAL;
886 max = count - i;
887 tmp = count_trail_chars(&user_buffer[i], max);
888 if (tmp < 0) {
889 printk("pktgen: illegal format\n");
890 return tmp;
892 i += tmp;
894 /* Read variable name */
896 len = strn_len(&user_buffer[i], sizeof(name) - 1);
897 if (len < 0) {
898 return len;
900 memset(name, 0, sizeof(name));
901 if (copy_from_user(name, &user_buffer[i], len))
902 return -EFAULT;
903 i += len;
905 max = count - i;
906 len = count_trail_chars(&user_buffer[i], max);
907 if (len < 0)
908 return len;
910 i += len;
912 if (debug) {
913 char tb[count + 1];
914 if (copy_from_user(tb, user_buffer, count))
915 return -EFAULT;
916 tb[count] = 0;
917 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
918 (unsigned long)count, tb);
921 if (!strcmp(name, "min_pkt_size")) {
922 len = num_arg(&user_buffer[i], 10, &value);
923 if (len < 0) {
924 return len;
926 i += len;
927 if (value < 14 + 20 + 8)
928 value = 14 + 20 + 8;
929 if (value != pkt_dev->min_pkt_size) {
930 pkt_dev->min_pkt_size = value;
931 pkt_dev->cur_pkt_size = value;
933 sprintf(pg_result, "OK: min_pkt_size=%u",
934 pkt_dev->min_pkt_size);
935 return count;
938 if (!strcmp(name, "max_pkt_size")) {
939 len = num_arg(&user_buffer[i], 10, &value);
940 if (len < 0) {
941 return len;
943 i += len;
944 if (value < 14 + 20 + 8)
945 value = 14 + 20 + 8;
946 if (value != pkt_dev->max_pkt_size) {
947 pkt_dev->max_pkt_size = value;
948 pkt_dev->cur_pkt_size = value;
950 sprintf(pg_result, "OK: max_pkt_size=%u",
951 pkt_dev->max_pkt_size);
952 return count;
955 /* Shortcut for min = max */
957 if (!strcmp(name, "pkt_size")) {
958 len = num_arg(&user_buffer[i], 10, &value);
959 if (len < 0) {
960 return len;
962 i += len;
963 if (value < 14 + 20 + 8)
964 value = 14 + 20 + 8;
965 if (value != pkt_dev->min_pkt_size) {
966 pkt_dev->min_pkt_size = value;
967 pkt_dev->max_pkt_size = value;
968 pkt_dev->cur_pkt_size = value;
970 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
971 return count;
974 if (!strcmp(name, "debug")) {
975 len = num_arg(&user_buffer[i], 10, &value);
976 if (len < 0) {
977 return len;
979 i += len;
980 debug = value;
981 sprintf(pg_result, "OK: debug=%u", debug);
982 return count;
985 if (!strcmp(name, "frags")) {
986 len = num_arg(&user_buffer[i], 10, &value);
987 if (len < 0) {
988 return len;
990 i += len;
991 pkt_dev->nfrags = value;
992 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
993 return count;
995 if (!strcmp(name, "delay")) {
996 len = num_arg(&user_buffer[i], 10, &value);
997 if (len < 0) {
998 return len;
1000 i += len;
1001 if (value == 0x7FFFFFFF) {
1002 pkt_dev->delay_us = 0x7FFFFFFF;
1003 pkt_dev->delay_ns = 0;
1004 } else {
1005 pkt_dev->delay_us = value / 1000;
1006 pkt_dev->delay_ns = value % 1000;
1008 sprintf(pg_result, "OK: delay=%u",
1009 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1010 return count;
1012 if (!strcmp(name, "udp_src_min")) {
1013 len = num_arg(&user_buffer[i], 10, &value);
1014 if (len < 0) {
1015 return len;
1017 i += len;
1018 if (value != pkt_dev->udp_src_min) {
1019 pkt_dev->udp_src_min = value;
1020 pkt_dev->cur_udp_src = value;
1022 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1023 return count;
1025 if (!strcmp(name, "udp_dst_min")) {
1026 len = num_arg(&user_buffer[i], 10, &value);
1027 if (len < 0) {
1028 return len;
1030 i += len;
1031 if (value != pkt_dev->udp_dst_min) {
1032 pkt_dev->udp_dst_min = value;
1033 pkt_dev->cur_udp_dst = value;
1035 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1036 return count;
1038 if (!strcmp(name, "udp_src_max")) {
1039 len = num_arg(&user_buffer[i], 10, &value);
1040 if (len < 0) {
1041 return len;
1043 i += len;
1044 if (value != pkt_dev->udp_src_max) {
1045 pkt_dev->udp_src_max = value;
1046 pkt_dev->cur_udp_src = value;
1048 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1049 return count;
1051 if (!strcmp(name, "udp_dst_max")) {
1052 len = num_arg(&user_buffer[i], 10, &value);
1053 if (len < 0) {
1054 return len;
1056 i += len;
1057 if (value != pkt_dev->udp_dst_max) {
1058 pkt_dev->udp_dst_max = value;
1059 pkt_dev->cur_udp_dst = value;
1061 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1062 return count;
1064 if (!strcmp(name, "clone_skb")) {
1065 len = num_arg(&user_buffer[i], 10, &value);
1066 if (len < 0) {
1067 return len;
1069 i += len;
1070 pkt_dev->clone_skb = value;
1072 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1073 return count;
1075 if (!strcmp(name, "count")) {
1076 len = num_arg(&user_buffer[i], 10, &value);
1077 if (len < 0) {
1078 return len;
1080 i += len;
1081 pkt_dev->count = value;
1082 sprintf(pg_result, "OK: count=%llu",
1083 (unsigned long long)pkt_dev->count);
1084 return count;
1086 if (!strcmp(name, "src_mac_count")) {
1087 len = num_arg(&user_buffer[i], 10, &value);
1088 if (len < 0) {
1089 return len;
1091 i += len;
1092 if (pkt_dev->src_mac_count != value) {
1093 pkt_dev->src_mac_count = value;
1094 pkt_dev->cur_src_mac_offset = 0;
1096 sprintf(pg_result, "OK: src_mac_count=%d",
1097 pkt_dev->src_mac_count);
1098 return count;
1100 if (!strcmp(name, "dst_mac_count")) {
1101 len = num_arg(&user_buffer[i], 10, &value);
1102 if (len < 0) {
1103 return len;
1105 i += len;
1106 if (pkt_dev->dst_mac_count != value) {
1107 pkt_dev->dst_mac_count = value;
1108 pkt_dev->cur_dst_mac_offset = 0;
1110 sprintf(pg_result, "OK: dst_mac_count=%d",
1111 pkt_dev->dst_mac_count);
1112 return count;
1114 if (!strcmp(name, "flag")) {
1115 char f[32];
1116 memset(f, 0, 32);
1117 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1118 if (len < 0) {
1119 return len;
1121 if (copy_from_user(f, &user_buffer[i], len))
1122 return -EFAULT;
1123 i += len;
1124 if (strcmp(f, "IPSRC_RND") == 0)
1125 pkt_dev->flags |= F_IPSRC_RND;
1127 else if (strcmp(f, "!IPSRC_RND") == 0)
1128 pkt_dev->flags &= ~F_IPSRC_RND;
1130 else if (strcmp(f, "TXSIZE_RND") == 0)
1131 pkt_dev->flags |= F_TXSIZE_RND;
1133 else if (strcmp(f, "!TXSIZE_RND") == 0)
1134 pkt_dev->flags &= ~F_TXSIZE_RND;
1136 else if (strcmp(f, "IPDST_RND") == 0)
1137 pkt_dev->flags |= F_IPDST_RND;
1139 else if (strcmp(f, "!IPDST_RND") == 0)
1140 pkt_dev->flags &= ~F_IPDST_RND;
1142 else if (strcmp(f, "UDPSRC_RND") == 0)
1143 pkt_dev->flags |= F_UDPSRC_RND;
1145 else if (strcmp(f, "!UDPSRC_RND") == 0)
1146 pkt_dev->flags &= ~F_UDPSRC_RND;
1148 else if (strcmp(f, "UDPDST_RND") == 0)
1149 pkt_dev->flags |= F_UDPDST_RND;
1151 else if (strcmp(f, "!UDPDST_RND") == 0)
1152 pkt_dev->flags &= ~F_UDPDST_RND;
1154 else if (strcmp(f, "MACSRC_RND") == 0)
1155 pkt_dev->flags |= F_MACSRC_RND;
1157 else if (strcmp(f, "!MACSRC_RND") == 0)
1158 pkt_dev->flags &= ~F_MACSRC_RND;
1160 else if (strcmp(f, "MACDST_RND") == 0)
1161 pkt_dev->flags |= F_MACDST_RND;
1163 else if (strcmp(f, "!MACDST_RND") == 0)
1164 pkt_dev->flags &= ~F_MACDST_RND;
1166 else if (strcmp(f, "MPLS_RND") == 0)
1167 pkt_dev->flags |= F_MPLS_RND;
1169 else if (strcmp(f, "!MPLS_RND") == 0)
1170 pkt_dev->flags &= ~F_MPLS_RND;
1172 else if (strcmp(f, "VID_RND") == 0)
1173 pkt_dev->flags |= F_VID_RND;
1175 else if (strcmp(f, "!VID_RND") == 0)
1176 pkt_dev->flags &= ~F_VID_RND;
1178 else if (strcmp(f, "SVID_RND") == 0)
1179 pkt_dev->flags |= F_SVID_RND;
1181 else if (strcmp(f, "!SVID_RND") == 0)
1182 pkt_dev->flags &= ~F_SVID_RND;
1184 else if (strcmp(f, "!IPV6") == 0)
1185 pkt_dev->flags &= ~F_IPV6;
1187 else {
1188 sprintf(pg_result,
1189 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1191 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1192 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
1193 return count;
1195 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1196 return count;
1198 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1199 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1200 if (len < 0) {
1201 return len;
1204 if (copy_from_user(buf, &user_buffer[i], len))
1205 return -EFAULT;
1206 buf[len] = 0;
1207 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1208 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1209 strncpy(pkt_dev->dst_min, buf, len);
1210 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1211 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1213 if (debug)
1214 printk("pktgen: dst_min set to: %s\n",
1215 pkt_dev->dst_min);
1216 i += len;
1217 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1218 return count;
1220 if (!strcmp(name, "dst_max")) {
1221 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1222 if (len < 0) {
1223 return len;
1226 if (copy_from_user(buf, &user_buffer[i], len))
1227 return -EFAULT;
1229 buf[len] = 0;
1230 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1231 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1232 strncpy(pkt_dev->dst_max, buf, len);
1233 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1234 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1236 if (debug)
1237 printk("pktgen: dst_max set to: %s\n",
1238 pkt_dev->dst_max);
1239 i += len;
1240 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1241 return count;
1243 if (!strcmp(name, "dst6")) {
1244 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1245 if (len < 0)
1246 return len;
1248 pkt_dev->flags |= F_IPV6;
1250 if (copy_from_user(buf, &user_buffer[i], len))
1251 return -EFAULT;
1252 buf[len] = 0;
1254 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1255 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1257 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1259 if (debug)
1260 printk("pktgen: dst6 set to: %s\n", buf);
1262 i += len;
1263 sprintf(pg_result, "OK: dst6=%s", buf);
1264 return count;
1266 if (!strcmp(name, "dst6_min")) {
1267 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1268 if (len < 0)
1269 return len;
1271 pkt_dev->flags |= F_IPV6;
1273 if (copy_from_user(buf, &user_buffer[i], len))
1274 return -EFAULT;
1275 buf[len] = 0;
1277 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1278 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1280 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1281 &pkt_dev->min_in6_daddr);
1282 if (debug)
1283 printk("pktgen: dst6_min set to: %s\n", buf);
1285 i += len;
1286 sprintf(pg_result, "OK: dst6_min=%s", buf);
1287 return count;
1289 if (!strcmp(name, "dst6_max")) {
1290 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1291 if (len < 0)
1292 return len;
1294 pkt_dev->flags |= F_IPV6;
1296 if (copy_from_user(buf, &user_buffer[i], len))
1297 return -EFAULT;
1298 buf[len] = 0;
1300 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1301 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1303 if (debug)
1304 printk("pktgen: dst6_max set to: %s\n", buf);
1306 i += len;
1307 sprintf(pg_result, "OK: dst6_max=%s", buf);
1308 return count;
1310 if (!strcmp(name, "src6")) {
1311 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1312 if (len < 0)
1313 return len;
1315 pkt_dev->flags |= F_IPV6;
1317 if (copy_from_user(buf, &user_buffer[i], len))
1318 return -EFAULT;
1319 buf[len] = 0;
1321 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1322 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1324 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1326 if (debug)
1327 printk("pktgen: src6 set to: %s\n", buf);
1329 i += len;
1330 sprintf(pg_result, "OK: src6=%s", buf);
1331 return count;
1333 if (!strcmp(name, "src_min")) {
1334 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1335 if (len < 0) {
1336 return len;
1338 if (copy_from_user(buf, &user_buffer[i], len))
1339 return -EFAULT;
1340 buf[len] = 0;
1341 if (strcmp(buf, pkt_dev->src_min) != 0) {
1342 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1343 strncpy(pkt_dev->src_min, buf, len);
1344 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1345 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1347 if (debug)
1348 printk("pktgen: src_min set to: %s\n",
1349 pkt_dev->src_min);
1350 i += len;
1351 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1352 return count;
1354 if (!strcmp(name, "src_max")) {
1355 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1356 if (len < 0) {
1357 return len;
1359 if (copy_from_user(buf, &user_buffer[i], len))
1360 return -EFAULT;
1361 buf[len] = 0;
1362 if (strcmp(buf, pkt_dev->src_max) != 0) {
1363 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1364 strncpy(pkt_dev->src_max, buf, len);
1365 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1366 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1368 if (debug)
1369 printk("pktgen: src_max set to: %s\n",
1370 pkt_dev->src_max);
1371 i += len;
1372 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1373 return count;
1375 if (!strcmp(name, "dst_mac")) {
1376 char *v = valstr;
1377 unsigned char old_dmac[ETH_ALEN];
1378 unsigned char *m = pkt_dev->dst_mac;
1379 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1381 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1382 if (len < 0) {
1383 return len;
1385 memset(valstr, 0, sizeof(valstr));
1386 if (copy_from_user(valstr, &user_buffer[i], len))
1387 return -EFAULT;
1388 i += len;
1390 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1391 if (*v >= '0' && *v <= '9') {
1392 *m *= 16;
1393 *m += *v - '0';
1395 if (*v >= 'A' && *v <= 'F') {
1396 *m *= 16;
1397 *m += *v - 'A' + 10;
1399 if (*v >= 'a' && *v <= 'f') {
1400 *m *= 16;
1401 *m += *v - 'a' + 10;
1403 if (*v == ':') {
1404 m++;
1405 *m = 0;
1409 /* Set up Dest MAC */
1410 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1411 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1413 sprintf(pg_result, "OK: dstmac");
1414 return count;
1416 if (!strcmp(name, "src_mac")) {
1417 char *v = valstr;
1418 unsigned char *m = pkt_dev->src_mac;
1420 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1421 if (len < 0) {
1422 return len;
1424 memset(valstr, 0, sizeof(valstr));
1425 if (copy_from_user(valstr, &user_buffer[i], len))
1426 return -EFAULT;
1427 i += len;
1429 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1430 if (*v >= '0' && *v <= '9') {
1431 *m *= 16;
1432 *m += *v - '0';
1434 if (*v >= 'A' && *v <= 'F') {
1435 *m *= 16;
1436 *m += *v - 'A' + 10;
1438 if (*v >= 'a' && *v <= 'f') {
1439 *m *= 16;
1440 *m += *v - 'a' + 10;
1442 if (*v == ':') {
1443 m++;
1444 *m = 0;
1448 sprintf(pg_result, "OK: srcmac");
1449 return count;
1452 if (!strcmp(name, "clear_counters")) {
1453 pktgen_clear_counters(pkt_dev);
1454 sprintf(pg_result, "OK: Clearing counters.\n");
1455 return count;
1458 if (!strcmp(name, "flows")) {
1459 len = num_arg(&user_buffer[i], 10, &value);
1460 if (len < 0) {
1461 return len;
1463 i += len;
1464 if (value > MAX_CFLOWS)
1465 value = MAX_CFLOWS;
1467 pkt_dev->cflows = value;
1468 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1469 return count;
1472 if (!strcmp(name, "flowlen")) {
1473 len = num_arg(&user_buffer[i], 10, &value);
1474 if (len < 0) {
1475 return len;
1477 i += len;
1478 pkt_dev->lflow = value;
1479 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1480 return count;
1483 if (!strcmp(name, "mpls")) {
1484 unsigned n, offset;
1485 len = get_labels(&user_buffer[i], pkt_dev);
1486 if (len < 0) { return len; }
1487 i += len;
1488 offset = sprintf(pg_result, "OK: mpls=");
1489 for (n = 0; n < pkt_dev->nr_labels; n++)
1490 offset += sprintf(pg_result + offset,
1491 "%08x%s", ntohl(pkt_dev->labels[n]),
1492 n == pkt_dev->nr_labels-1 ? "" : ",");
1494 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1495 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1496 pkt_dev->svlan_id = 0xffff;
1498 if (debug)
1499 printk("pktgen: VLAN/SVLAN auto turned off\n");
1501 return count;
1504 if (!strcmp(name, "vlan_id")) {
1505 len = num_arg(&user_buffer[i], 4, &value);
1506 if (len < 0) {
1507 return len;
1509 i += len;
1510 if (value <= 4095) {
1511 pkt_dev->vlan_id = value; /* turn on VLAN */
1513 if (debug)
1514 printk("pktgen: VLAN turned on\n");
1516 if (debug && pkt_dev->nr_labels)
1517 printk("pktgen: MPLS auto turned off\n");
1519 pkt_dev->nr_labels = 0; /* turn off MPLS */
1520 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1521 } else {
1522 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1523 pkt_dev->svlan_id = 0xffff;
1525 if (debug)
1526 printk("pktgen: VLAN/SVLAN turned off\n");
1528 return count;
1531 if (!strcmp(name, "vlan_p")) {
1532 len = num_arg(&user_buffer[i], 1, &value);
1533 if (len < 0) {
1534 return len;
1536 i += len;
1537 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1538 pkt_dev->vlan_p = value;
1539 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1540 } else {
1541 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1543 return count;
1546 if (!strcmp(name, "vlan_cfi")) {
1547 len = num_arg(&user_buffer[i], 1, &value);
1548 if (len < 0) {
1549 return len;
1551 i += len;
1552 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1553 pkt_dev->vlan_cfi = value;
1554 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1555 } else {
1556 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1558 return count;
1561 if (!strcmp(name, "svlan_id")) {
1562 len = num_arg(&user_buffer[i], 4, &value);
1563 if (len < 0) {
1564 return len;
1566 i += len;
1567 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1568 pkt_dev->svlan_id = value; /* turn on SVLAN */
1570 if (debug)
1571 printk("pktgen: SVLAN turned on\n");
1573 if (debug && pkt_dev->nr_labels)
1574 printk("pktgen: MPLS auto turned off\n");
1576 pkt_dev->nr_labels = 0; /* turn off MPLS */
1577 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1578 } else {
1579 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1580 pkt_dev->svlan_id = 0xffff;
1582 if (debug)
1583 printk("pktgen: VLAN/SVLAN turned off\n");
1585 return count;
1588 if (!strcmp(name, "svlan_p")) {
1589 len = num_arg(&user_buffer[i], 1, &value);
1590 if (len < 0) {
1591 return len;
1593 i += len;
1594 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1595 pkt_dev->svlan_p = value;
1596 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1597 } else {
1598 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1600 return count;
1603 if (!strcmp(name, "svlan_cfi")) {
1604 len = num_arg(&user_buffer[i], 1, &value);
1605 if (len < 0) {
1606 return len;
1608 i += len;
1609 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1610 pkt_dev->svlan_cfi = value;
1611 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1612 } else {
1613 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1615 return count;
1618 if (!strcmp(name, "tos")) {
1619 __u32 tmp_value = 0;
1620 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1621 if (len < 0) {
1622 return len;
1624 i += len;
1625 if (len == 2) {
1626 pkt_dev->tos = tmp_value;
1627 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1628 } else {
1629 sprintf(pg_result, "ERROR: tos must be 00-ff");
1631 return count;
1634 if (!strcmp(name, "traffic_class")) {
1635 __u32 tmp_value = 0;
1636 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1637 if (len < 0) {
1638 return len;
1640 i += len;
1641 if (len == 2) {
1642 pkt_dev->traffic_class = tmp_value;
1643 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1644 } else {
1645 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1647 return count;
1650 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1651 return -EINVAL;
1654 static int pktgen_if_open(struct inode *inode, struct file *file)
1656 return single_open(file, pktgen_if_show, PDE(inode)->data);
1659 static const struct file_operations pktgen_if_fops = {
1660 .owner = THIS_MODULE,
1661 .open = pktgen_if_open,
1662 .read = seq_read,
1663 .llseek = seq_lseek,
1664 .write = pktgen_if_write,
1665 .release = single_release,
1668 static int pktgen_thread_show(struct seq_file *seq, void *v)
1670 struct pktgen_thread *t = seq->private;
1671 struct pktgen_dev *pkt_dev;
1673 BUG_ON(!t);
1675 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1676 t->tsk->comm, t->max_before_softirq);
1678 seq_printf(seq, "Running: ");
1680 if_lock(t);
1681 list_for_each_entry(pkt_dev, &t->if_list, list)
1682 if (pkt_dev->running)
1683 seq_printf(seq, "%s ", pkt_dev->odev->name);
1685 seq_printf(seq, "\nStopped: ");
1687 list_for_each_entry(pkt_dev, &t->if_list, list)
1688 if (!pkt_dev->running)
1689 seq_printf(seq, "%s ", pkt_dev->odev->name);
1691 if (t->result[0])
1692 seq_printf(seq, "\nResult: %s\n", t->result);
1693 else
1694 seq_printf(seq, "\nResult: NA\n");
1696 if_unlock(t);
1698 return 0;
1701 static ssize_t pktgen_thread_write(struct file *file,
1702 const char __user * user_buffer,
1703 size_t count, loff_t * offset)
1705 struct seq_file *seq = (struct seq_file *)file->private_data;
1706 struct pktgen_thread *t = seq->private;
1707 int i = 0, max, len, ret;
1708 char name[40];
1709 char *pg_result;
1710 unsigned long value = 0;
1712 if (count < 1) {
1713 // sprintf(pg_result, "Wrong command format");
1714 return -EINVAL;
1717 max = count - i;
1718 len = count_trail_chars(&user_buffer[i], max);
1719 if (len < 0)
1720 return len;
1722 i += len;
1724 /* Read variable name */
1726 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1727 if (len < 0)
1728 return len;
1730 memset(name, 0, sizeof(name));
1731 if (copy_from_user(name, &user_buffer[i], len))
1732 return -EFAULT;
1733 i += len;
1735 max = count - i;
1736 len = count_trail_chars(&user_buffer[i], max);
1737 if (len < 0)
1738 return len;
1740 i += len;
1742 if (debug)
1743 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
1745 if (!t) {
1746 printk("pktgen: ERROR: No thread\n");
1747 ret = -EINVAL;
1748 goto out;
1751 pg_result = &(t->result[0]);
1753 if (!strcmp(name, "add_device")) {
1754 char f[32];
1755 memset(f, 0, 32);
1756 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1757 if (len < 0) {
1758 ret = len;
1759 goto out;
1761 if (copy_from_user(f, &user_buffer[i], len))
1762 return -EFAULT;
1763 i += len;
1764 mutex_lock(&pktgen_thread_lock);
1765 pktgen_add_device(t, f);
1766 mutex_unlock(&pktgen_thread_lock);
1767 ret = count;
1768 sprintf(pg_result, "OK: add_device=%s", f);
1769 goto out;
1772 if (!strcmp(name, "rem_device_all")) {
1773 mutex_lock(&pktgen_thread_lock);
1774 t->control |= T_REMDEVALL;
1775 mutex_unlock(&pktgen_thread_lock);
1776 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1777 ret = count;
1778 sprintf(pg_result, "OK: rem_device_all");
1779 goto out;
1782 if (!strcmp(name, "max_before_softirq")) {
1783 len = num_arg(&user_buffer[i], 10, &value);
1784 mutex_lock(&pktgen_thread_lock);
1785 t->max_before_softirq = value;
1786 mutex_unlock(&pktgen_thread_lock);
1787 ret = count;
1788 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1789 goto out;
1792 ret = -EINVAL;
1793 out:
1794 return ret;
1797 static int pktgen_thread_open(struct inode *inode, struct file *file)
1799 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1802 static const struct file_operations pktgen_thread_fops = {
1803 .owner = THIS_MODULE,
1804 .open = pktgen_thread_open,
1805 .read = seq_read,
1806 .llseek = seq_lseek,
1807 .write = pktgen_thread_write,
1808 .release = single_release,
1811 /* Think find or remove for NN */
1812 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1814 struct pktgen_thread *t;
1815 struct pktgen_dev *pkt_dev = NULL;
1817 list_for_each_entry(t, &pktgen_threads, th_list) {
1818 pkt_dev = pktgen_find_dev(t, ifname);
1819 if (pkt_dev) {
1820 if (remove) {
1821 if_lock(t);
1822 pkt_dev->removal_mark = 1;
1823 t->control |= T_REMDEV;
1824 if_unlock(t);
1826 break;
1829 return pkt_dev;
1833 * mark a device for removal
1835 static void pktgen_mark_device(const char *ifname)
1837 struct pktgen_dev *pkt_dev = NULL;
1838 const int max_tries = 10, msec_per_try = 125;
1839 int i = 0;
1841 mutex_lock(&pktgen_thread_lock);
1842 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1844 while (1) {
1846 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1847 if (pkt_dev == NULL)
1848 break; /* success */
1850 mutex_unlock(&pktgen_thread_lock);
1851 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1852 "to disappear....\n", ifname);
1853 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1854 mutex_lock(&pktgen_thread_lock);
1856 if (++i >= max_tries) {
1857 printk("pktgen_mark_device: timed out after waiting "
1858 "%d msec for device %s to be removed\n",
1859 msec_per_try * i, ifname);
1860 break;
1865 mutex_unlock(&pktgen_thread_lock);
1868 static void pktgen_change_name(struct net_device *dev)
1870 struct pktgen_thread *t;
1872 list_for_each_entry(t, &pktgen_threads, th_list) {
1873 struct pktgen_dev *pkt_dev;
1875 list_for_each_entry(pkt_dev, &t->if_list, list) {
1876 if (pkt_dev->odev != dev)
1877 continue;
1879 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1881 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1882 pg_proc_dir);
1883 if (!pkt_dev->entry)
1884 printk(KERN_ERR "pktgen: can't move proc "
1885 " entry for '%s'\n", dev->name);
1886 break;
1891 static int pktgen_device_event(struct notifier_block *unused,
1892 unsigned long event, void *ptr)
1894 struct net_device *dev = ptr;
1896 /* It is OK that we do not hold the group lock right now,
1897 * as we run under the RTNL lock.
1900 switch (event) {
1901 case NETDEV_CHANGENAME:
1902 pktgen_change_name(dev);
1903 break;
1905 case NETDEV_UNREGISTER:
1906 pktgen_mark_device(dev->name);
1907 break;
1910 return NOTIFY_DONE;
1913 /* Associate pktgen_dev with a device. */
1915 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1917 struct net_device *odev;
1918 int err;
1920 /* Clean old setups */
1921 if (pkt_dev->odev) {
1922 dev_put(pkt_dev->odev);
1923 pkt_dev->odev = NULL;
1926 odev = dev_get_by_name(ifname);
1927 if (!odev) {
1928 printk("pktgen: no such netdevice: \"%s\"\n", ifname);
1929 return -ENODEV;
1932 if (odev->type != ARPHRD_ETHER) {
1933 printk("pktgen: not an ethernet device: \"%s\"\n", ifname);
1934 err = -EINVAL;
1935 } else if (!netif_running(odev)) {
1936 printk("pktgen: device is down: \"%s\"\n", ifname);
1937 err = -ENETDOWN;
1938 } else {
1939 pkt_dev->odev = odev;
1940 return 0;
1943 dev_put(odev);
1944 return err;
1947 /* Read pkt_dev from the interface and set up internal pktgen_dev
1948 * structure to have the right information to create/send packets
1950 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1952 if (!pkt_dev->odev) {
1953 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1954 sprintf(pkt_dev->result,
1955 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1956 return;
1959 /* Default to the interface's mac if not explicitly set. */
1961 if (is_zero_ether_addr(pkt_dev->src_mac))
1962 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1964 /* Set up Dest MAC */
1965 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1967 /* Set up pkt size */
1968 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1970 if (pkt_dev->flags & F_IPV6) {
1972 * Skip this automatic address setting until locks or functions
1973 * gets exported
1976 #ifdef NOTNOW
1977 int i, set = 0, err = 1;
1978 struct inet6_dev *idev;
1980 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1981 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1982 set = 1;
1983 break;
1986 if (!set) {
1989 * Use linklevel address if unconfigured.
1991 * use ipv6_get_lladdr if/when it's get exported
1994 rcu_read_lock();
1995 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1996 struct inet6_ifaddr *ifp;
1998 read_lock_bh(&idev->lock);
1999 for (ifp = idev->addr_list; ifp;
2000 ifp = ifp->if_next) {
2001 if (ifp->scope == IFA_LINK
2002 && !(ifp->
2003 flags & IFA_F_TENTATIVE)) {
2004 ipv6_addr_copy(&pkt_dev->
2005 cur_in6_saddr,
2006 &ifp->addr);
2007 err = 0;
2008 break;
2011 read_unlock_bh(&idev->lock);
2013 rcu_read_unlock();
2014 if (err)
2015 printk("pktgen: ERROR: IPv6 link address not availble.\n");
2017 #endif
2018 } else {
2019 pkt_dev->saddr_min = 0;
2020 pkt_dev->saddr_max = 0;
2021 if (strlen(pkt_dev->src_min) == 0) {
2023 struct in_device *in_dev;
2025 rcu_read_lock();
2026 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2027 if (in_dev) {
2028 if (in_dev->ifa_list) {
2029 pkt_dev->saddr_min =
2030 in_dev->ifa_list->ifa_address;
2031 pkt_dev->saddr_max = pkt_dev->saddr_min;
2034 rcu_read_unlock();
2035 } else {
2036 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2037 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2040 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2041 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2043 /* Initialize current values. */
2044 pkt_dev->cur_dst_mac_offset = 0;
2045 pkt_dev->cur_src_mac_offset = 0;
2046 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2047 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2048 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2049 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2050 pkt_dev->nflows = 0;
2053 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2055 __u64 start;
2056 __u64 now;
2058 start = now = getCurUs();
2059 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2060 while (now < spin_until_us) {
2061 /* TODO: optimize sleeping behavior */
2062 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2063 schedule_timeout_interruptible(1);
2064 else if (spin_until_us - now > 100) {
2065 do_softirq();
2066 if (!pkt_dev->running)
2067 return;
2068 if (need_resched())
2069 schedule();
2072 now = getCurUs();
2075 pkt_dev->idle_acc += now - start;
2078 /* Increment/randomize headers according to flags and current values
2079 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2081 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2083 __u32 imn;
2084 __u32 imx;
2085 int flow = 0;
2087 if (pkt_dev->cflows) {
2088 flow = random32() % pkt_dev->cflows;
2090 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2091 pkt_dev->flows[flow].count = 0;
2094 /* Deal with source MAC */
2095 if (pkt_dev->src_mac_count > 1) {
2096 __u32 mc;
2097 __u32 tmp;
2099 if (pkt_dev->flags & F_MACSRC_RND)
2100 mc = random32() % pkt_dev->src_mac_count;
2101 else {
2102 mc = pkt_dev->cur_src_mac_offset++;
2103 if (pkt_dev->cur_src_mac_offset >
2104 pkt_dev->src_mac_count)
2105 pkt_dev->cur_src_mac_offset = 0;
2108 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2109 pkt_dev->hh[11] = tmp;
2110 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2111 pkt_dev->hh[10] = tmp;
2112 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2113 pkt_dev->hh[9] = tmp;
2114 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2115 pkt_dev->hh[8] = tmp;
2116 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2117 pkt_dev->hh[7] = tmp;
2120 /* Deal with Destination MAC */
2121 if (pkt_dev->dst_mac_count > 1) {
2122 __u32 mc;
2123 __u32 tmp;
2125 if (pkt_dev->flags & F_MACDST_RND)
2126 mc = random32() % pkt_dev->dst_mac_count;
2128 else {
2129 mc = pkt_dev->cur_dst_mac_offset++;
2130 if (pkt_dev->cur_dst_mac_offset >
2131 pkt_dev->dst_mac_count) {
2132 pkt_dev->cur_dst_mac_offset = 0;
2136 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2137 pkt_dev->hh[5] = tmp;
2138 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2139 pkt_dev->hh[4] = tmp;
2140 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2141 pkt_dev->hh[3] = tmp;
2142 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2143 pkt_dev->hh[2] = tmp;
2144 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2145 pkt_dev->hh[1] = tmp;
2148 if (pkt_dev->flags & F_MPLS_RND) {
2149 unsigned i;
2150 for (i = 0; i < pkt_dev->nr_labels; i++)
2151 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2152 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2153 ((__force __be32)random32() &
2154 htonl(0x000fffff));
2157 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2158 pkt_dev->vlan_id = random32() & (4096-1);
2161 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2162 pkt_dev->svlan_id = random32() & (4096 - 1);
2165 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2166 if (pkt_dev->flags & F_UDPSRC_RND)
2167 pkt_dev->cur_udp_src = random32() %
2168 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2169 + pkt_dev->udp_src_min;
2171 else {
2172 pkt_dev->cur_udp_src++;
2173 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2174 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2178 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2179 if (pkt_dev->flags & F_UDPDST_RND) {
2180 pkt_dev->cur_udp_dst = random32() %
2181 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2182 + pkt_dev->udp_dst_min;
2183 } else {
2184 pkt_dev->cur_udp_dst++;
2185 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2186 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2190 if (!(pkt_dev->flags & F_IPV6)) {
2192 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2193 ntohl(pkt_dev->
2194 saddr_max))) {
2195 __u32 t;
2196 if (pkt_dev->flags & F_IPSRC_RND)
2197 t = random32() % (imx - imn) + imn;
2198 else {
2199 t = ntohl(pkt_dev->cur_saddr);
2200 t++;
2201 if (t > imx) {
2202 t = imn;
2205 pkt_dev->cur_saddr = htonl(t);
2208 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2209 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2210 } else {
2211 imn = ntohl(pkt_dev->daddr_min);
2212 imx = ntohl(pkt_dev->daddr_max);
2213 if (imn < imx) {
2214 __u32 t;
2215 __be32 s;
2216 if (pkt_dev->flags & F_IPDST_RND) {
2218 t = random32() % (imx - imn) + imn;
2219 s = htonl(t);
2221 while (LOOPBACK(s) || MULTICAST(s)
2222 || BADCLASS(s) || ZERONET(s)
2223 || LOCAL_MCAST(s)) {
2224 t = random32() % (imx - imn) + imn;
2225 s = htonl(t);
2227 pkt_dev->cur_daddr = s;
2228 } else {
2229 t = ntohl(pkt_dev->cur_daddr);
2230 t++;
2231 if (t > imx) {
2232 t = imn;
2234 pkt_dev->cur_daddr = htonl(t);
2237 if (pkt_dev->cflows) {
2238 pkt_dev->flows[flow].cur_daddr =
2239 pkt_dev->cur_daddr;
2240 pkt_dev->nflows++;
2243 } else { /* IPV6 * */
2245 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2246 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2247 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2248 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2249 else {
2250 int i;
2252 /* Only random destinations yet */
2254 for (i = 0; i < 4; i++) {
2255 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2256 (((__force __be32)random32() |
2257 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2258 pkt_dev->max_in6_daddr.s6_addr32[i]);
2263 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2264 __u32 t;
2265 if (pkt_dev->flags & F_TXSIZE_RND) {
2266 t = random32() %
2267 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2268 + pkt_dev->min_pkt_size;
2269 } else {
2270 t = pkt_dev->cur_pkt_size + 1;
2271 if (t > pkt_dev->max_pkt_size)
2272 t = pkt_dev->min_pkt_size;
2274 pkt_dev->cur_pkt_size = t;
2277 pkt_dev->flows[flow].count++;
2280 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2282 unsigned i;
2283 for (i = 0; i < pkt_dev->nr_labels; i++) {
2284 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2286 mpls--;
2287 *mpls |= MPLS_STACK_BOTTOM;
2290 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2291 unsigned int prio)
2293 return htons(id | (cfi << 12) | (prio << 13));
2296 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2297 struct pktgen_dev *pkt_dev)
2299 struct sk_buff *skb = NULL;
2300 __u8 *eth;
2301 struct udphdr *udph;
2302 int datalen, iplen;
2303 struct iphdr *iph;
2304 struct pktgen_hdr *pgh = NULL;
2305 __be16 protocol = htons(ETH_P_IP);
2306 __be32 *mpls;
2307 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2308 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2309 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2310 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2313 if (pkt_dev->nr_labels)
2314 protocol = htons(ETH_P_MPLS_UC);
2316 if (pkt_dev->vlan_id != 0xffff)
2317 protocol = htons(ETH_P_8021Q);
2319 /* Update any of the values, used when we're incrementing various
2320 * fields.
2322 mod_cur_headers(pkt_dev);
2324 datalen = (odev->hard_header_len + 16) & ~0xf;
2325 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2326 pkt_dev->nr_labels*sizeof(u32) +
2327 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2328 GFP_ATOMIC);
2329 if (!skb) {
2330 sprintf(pkt_dev->result, "No memory");
2331 return NULL;
2334 skb_reserve(skb, datalen);
2336 /* Reserve for ethernet and IP header */
2337 eth = (__u8 *) skb_push(skb, 14);
2338 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2339 if (pkt_dev->nr_labels)
2340 mpls_push(mpls, pkt_dev);
2342 if (pkt_dev->vlan_id != 0xffff) {
2343 if (pkt_dev->svlan_id != 0xffff) {
2344 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2345 *svlan_tci = build_tci(pkt_dev->svlan_id,
2346 pkt_dev->svlan_cfi,
2347 pkt_dev->svlan_p);
2348 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2349 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2351 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2352 *vlan_tci = build_tci(pkt_dev->vlan_id,
2353 pkt_dev->vlan_cfi,
2354 pkt_dev->vlan_p);
2355 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2356 *vlan_encapsulated_proto = htons(ETH_P_IP);
2359 skb->network_header = skb->tail;
2360 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2361 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2363 iph = ip_hdr(skb);
2364 udph = udp_hdr(skb);
2366 memcpy(eth, pkt_dev->hh, 12);
2367 *(__be16 *) & eth[12] = protocol;
2369 /* Eth + IPh + UDPh + mpls */
2370 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2371 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2372 if (datalen < sizeof(struct pktgen_hdr))
2373 datalen = sizeof(struct pktgen_hdr);
2375 udph->source = htons(pkt_dev->cur_udp_src);
2376 udph->dest = htons(pkt_dev->cur_udp_dst);
2377 udph->len = htons(datalen + 8); /* DATA + udphdr */
2378 udph->check = 0; /* No checksum */
2380 iph->ihl = 5;
2381 iph->version = 4;
2382 iph->ttl = 32;
2383 iph->tos = pkt_dev->tos;
2384 iph->protocol = IPPROTO_UDP; /* UDP */
2385 iph->saddr = pkt_dev->cur_saddr;
2386 iph->daddr = pkt_dev->cur_daddr;
2387 iph->frag_off = 0;
2388 iplen = 20 + 8 + datalen;
2389 iph->tot_len = htons(iplen);
2390 iph->check = 0;
2391 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2392 skb->protocol = protocol;
2393 skb->mac_header = (skb->network_header - ETH_HLEN -
2394 pkt_dev->nr_labels * sizeof(u32) -
2395 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev));
2396 skb->dev = odev;
2397 skb->pkt_type = PACKET_HOST;
2399 if (pkt_dev->nfrags <= 0)
2400 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2401 else {
2402 int frags = pkt_dev->nfrags;
2403 int i;
2405 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2407 if (frags > MAX_SKB_FRAGS)
2408 frags = MAX_SKB_FRAGS;
2409 if (datalen > frags * PAGE_SIZE) {
2410 skb_put(skb, datalen - frags * PAGE_SIZE);
2411 datalen = frags * PAGE_SIZE;
2414 i = 0;
2415 while (datalen > 0) {
2416 struct page *page = alloc_pages(GFP_KERNEL, 0);
2417 skb_shinfo(skb)->frags[i].page = page;
2418 skb_shinfo(skb)->frags[i].page_offset = 0;
2419 skb_shinfo(skb)->frags[i].size =
2420 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2421 datalen -= skb_shinfo(skb)->frags[i].size;
2422 skb->len += skb_shinfo(skb)->frags[i].size;
2423 skb->data_len += skb_shinfo(skb)->frags[i].size;
2424 i++;
2425 skb_shinfo(skb)->nr_frags = i;
2428 while (i < frags) {
2429 int rem;
2431 if (i == 0)
2432 break;
2434 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2435 if (rem == 0)
2436 break;
2438 skb_shinfo(skb)->frags[i - 1].size -= rem;
2440 skb_shinfo(skb)->frags[i] =
2441 skb_shinfo(skb)->frags[i - 1];
2442 get_page(skb_shinfo(skb)->frags[i].page);
2443 skb_shinfo(skb)->frags[i].page =
2444 skb_shinfo(skb)->frags[i - 1].page;
2445 skb_shinfo(skb)->frags[i].page_offset +=
2446 skb_shinfo(skb)->frags[i - 1].size;
2447 skb_shinfo(skb)->frags[i].size = rem;
2448 i++;
2449 skb_shinfo(skb)->nr_frags = i;
2453 /* Stamp the time, and sequence number, convert them to network byte order */
2455 if (pgh) {
2456 struct timeval timestamp;
2458 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2459 pgh->seq_num = htonl(pkt_dev->seq_num);
2461 do_gettimeofday(&timestamp);
2462 pgh->tv_sec = htonl(timestamp.tv_sec);
2463 pgh->tv_usec = htonl(timestamp.tv_usec);
2466 return skb;
2470 * scan_ip6, fmt_ip taken from dietlibc-0.21
2471 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2473 * Slightly modified for kernel.
2474 * Should be candidate for net/ipv4/utils.c
2475 * --ro
2478 static unsigned int scan_ip6(const char *s, char ip[16])
2480 unsigned int i;
2481 unsigned int len = 0;
2482 unsigned long u;
2483 char suffix[16];
2484 unsigned int prefixlen = 0;
2485 unsigned int suffixlen = 0;
2486 __be32 tmp;
2488 for (i = 0; i < 16; i++)
2489 ip[i] = 0;
2491 for (;;) {
2492 if (*s == ':') {
2493 len++;
2494 if (s[1] == ':') { /* Found "::", skip to part 2 */
2495 s += 2;
2496 len++;
2497 break;
2499 s++;
2502 char *tmp;
2503 u = simple_strtoul(s, &tmp, 16);
2504 i = tmp - s;
2507 if (!i)
2508 return 0;
2509 if (prefixlen == 12 && s[i] == '.') {
2511 /* the last 4 bytes may be written as IPv4 address */
2513 tmp = in_aton(s);
2514 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2515 return i + len;
2517 ip[prefixlen++] = (u >> 8);
2518 ip[prefixlen++] = (u & 255);
2519 s += i;
2520 len += i;
2521 if (prefixlen == 16)
2522 return len;
2525 /* part 2, after "::" */
2526 for (;;) {
2527 if (*s == ':') {
2528 if (suffixlen == 0)
2529 break;
2530 s++;
2531 len++;
2532 } else if (suffixlen != 0)
2533 break;
2535 char *tmp;
2536 u = simple_strtol(s, &tmp, 16);
2537 i = tmp - s;
2539 if (!i) {
2540 if (*s)
2541 len--;
2542 break;
2544 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2545 tmp = in_aton(s);
2546 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2547 sizeof(tmp));
2548 suffixlen += 4;
2549 len += strlen(s);
2550 break;
2552 suffix[suffixlen++] = (u >> 8);
2553 suffix[suffixlen++] = (u & 255);
2554 s += i;
2555 len += i;
2556 if (prefixlen + suffixlen == 16)
2557 break;
2559 for (i = 0; i < suffixlen; i++)
2560 ip[16 - suffixlen + i] = suffix[i];
2561 return len;
2564 static char tohex(char hexdigit)
2566 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2569 static int fmt_xlong(char *s, unsigned int i)
2571 char *bak = s;
2572 *s = tohex((i >> 12) & 0xf);
2573 if (s != bak || *s != '0')
2574 ++s;
2575 *s = tohex((i >> 8) & 0xf);
2576 if (s != bak || *s != '0')
2577 ++s;
2578 *s = tohex((i >> 4) & 0xf);
2579 if (s != bak || *s != '0')
2580 ++s;
2581 *s = tohex(i & 0xf);
2582 return s - bak + 1;
2585 static unsigned int fmt_ip6(char *s, const char ip[16])
2587 unsigned int len;
2588 unsigned int i;
2589 unsigned int temp;
2590 unsigned int compressing;
2591 int j;
2593 len = 0;
2594 compressing = 0;
2595 for (j = 0; j < 16; j += 2) {
2597 #ifdef V4MAPPEDPREFIX
2598 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2599 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2600 temp = strlen(s);
2601 return len + temp;
2603 #endif
2604 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2605 (unsigned long)(unsigned char)ip[j + 1];
2606 if (temp == 0) {
2607 if (!compressing) {
2608 compressing = 1;
2609 if (j == 0) {
2610 *s++ = ':';
2611 ++len;
2614 } else {
2615 if (compressing) {
2616 compressing = 0;
2617 *s++ = ':';
2618 ++len;
2620 i = fmt_xlong(s, temp);
2621 len += i;
2622 s += i;
2623 if (j < 14) {
2624 *s++ = ':';
2625 ++len;
2629 if (compressing) {
2630 *s++ = ':';
2631 ++len;
2633 *s = 0;
2634 return len;
2637 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2638 struct pktgen_dev *pkt_dev)
2640 struct sk_buff *skb = NULL;
2641 __u8 *eth;
2642 struct udphdr *udph;
2643 int datalen;
2644 struct ipv6hdr *iph;
2645 struct pktgen_hdr *pgh = NULL;
2646 __be16 protocol = htons(ETH_P_IPV6);
2647 __be32 *mpls;
2648 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2649 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2650 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2651 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2653 if (pkt_dev->nr_labels)
2654 protocol = htons(ETH_P_MPLS_UC);
2656 if (pkt_dev->vlan_id != 0xffff)
2657 protocol = htons(ETH_P_8021Q);
2659 /* Update any of the values, used when we're incrementing various
2660 * fields.
2662 mod_cur_headers(pkt_dev);
2664 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2665 pkt_dev->nr_labels*sizeof(u32) +
2666 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2667 GFP_ATOMIC);
2668 if (!skb) {
2669 sprintf(pkt_dev->result, "No memory");
2670 return NULL;
2673 skb_reserve(skb, 16);
2675 /* Reserve for ethernet and IP header */
2676 eth = (__u8 *) skb_push(skb, 14);
2677 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2678 if (pkt_dev->nr_labels)
2679 mpls_push(mpls, pkt_dev);
2681 if (pkt_dev->vlan_id != 0xffff) {
2682 if (pkt_dev->svlan_id != 0xffff) {
2683 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2684 *svlan_tci = build_tci(pkt_dev->svlan_id,
2685 pkt_dev->svlan_cfi,
2686 pkt_dev->svlan_p);
2687 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2688 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2690 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2691 *vlan_tci = build_tci(pkt_dev->vlan_id,
2692 pkt_dev->vlan_cfi,
2693 pkt_dev->vlan_p);
2694 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2695 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2698 skb->network_header = skb->tail;
2699 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2700 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2702 iph = ipv6_hdr(skb);
2703 udph = udp_hdr(skb);
2705 memcpy(eth, pkt_dev->hh, 12);
2706 *(__be16 *) & eth[12] = protocol;
2708 /* Eth + IPh + UDPh + mpls */
2709 datalen = pkt_dev->cur_pkt_size - 14 -
2710 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2711 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2713 if (datalen < sizeof(struct pktgen_hdr)) {
2714 datalen = sizeof(struct pktgen_hdr);
2715 if (net_ratelimit())
2716 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2717 datalen);
2720 udph->source = htons(pkt_dev->cur_udp_src);
2721 udph->dest = htons(pkt_dev->cur_udp_dst);
2722 udph->len = htons(datalen + sizeof(struct udphdr));
2723 udph->check = 0; /* No checksum */
2725 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2727 if (pkt_dev->traffic_class) {
2728 /* Version + traffic class + flow (0) */
2729 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2732 iph->hop_limit = 32;
2734 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2735 iph->nexthdr = IPPROTO_UDP;
2737 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2738 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2740 skb->mac_header = (skb->network_header - ETH_HLEN -
2741 pkt_dev->nr_labels * sizeof(u32) -
2742 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev));
2743 skb->protocol = protocol;
2744 skb->dev = odev;
2745 skb->pkt_type = PACKET_HOST;
2747 if (pkt_dev->nfrags <= 0)
2748 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2749 else {
2750 int frags = pkt_dev->nfrags;
2751 int i;
2753 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2755 if (frags > MAX_SKB_FRAGS)
2756 frags = MAX_SKB_FRAGS;
2757 if (datalen > frags * PAGE_SIZE) {
2758 skb_put(skb, datalen - frags * PAGE_SIZE);
2759 datalen = frags * PAGE_SIZE;
2762 i = 0;
2763 while (datalen > 0) {
2764 struct page *page = alloc_pages(GFP_KERNEL, 0);
2765 skb_shinfo(skb)->frags[i].page = page;
2766 skb_shinfo(skb)->frags[i].page_offset = 0;
2767 skb_shinfo(skb)->frags[i].size =
2768 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2769 datalen -= skb_shinfo(skb)->frags[i].size;
2770 skb->len += skb_shinfo(skb)->frags[i].size;
2771 skb->data_len += skb_shinfo(skb)->frags[i].size;
2772 i++;
2773 skb_shinfo(skb)->nr_frags = i;
2776 while (i < frags) {
2777 int rem;
2779 if (i == 0)
2780 break;
2782 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2783 if (rem == 0)
2784 break;
2786 skb_shinfo(skb)->frags[i - 1].size -= rem;
2788 skb_shinfo(skb)->frags[i] =
2789 skb_shinfo(skb)->frags[i - 1];
2790 get_page(skb_shinfo(skb)->frags[i].page);
2791 skb_shinfo(skb)->frags[i].page =
2792 skb_shinfo(skb)->frags[i - 1].page;
2793 skb_shinfo(skb)->frags[i].page_offset +=
2794 skb_shinfo(skb)->frags[i - 1].size;
2795 skb_shinfo(skb)->frags[i].size = rem;
2796 i++;
2797 skb_shinfo(skb)->nr_frags = i;
2801 /* Stamp the time, and sequence number, convert them to network byte order */
2802 /* should we update cloned packets too ? */
2803 if (pgh) {
2804 struct timeval timestamp;
2806 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2807 pgh->seq_num = htonl(pkt_dev->seq_num);
2809 do_gettimeofday(&timestamp);
2810 pgh->tv_sec = htonl(timestamp.tv_sec);
2811 pgh->tv_usec = htonl(timestamp.tv_usec);
2813 /* pkt_dev->seq_num++; FF: you really mean this? */
2815 return skb;
2818 static inline struct sk_buff *fill_packet(struct net_device *odev,
2819 struct pktgen_dev *pkt_dev)
2821 if (pkt_dev->flags & F_IPV6)
2822 return fill_packet_ipv6(odev, pkt_dev);
2823 else
2824 return fill_packet_ipv4(odev, pkt_dev);
2827 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2829 pkt_dev->seq_num = 1;
2830 pkt_dev->idle_acc = 0;
2831 pkt_dev->sofar = 0;
2832 pkt_dev->tx_bytes = 0;
2833 pkt_dev->errors = 0;
2836 /* Set up structure for sending pkts, clear counters */
2838 static void pktgen_run(struct pktgen_thread *t)
2840 struct pktgen_dev *pkt_dev;
2841 int started = 0;
2843 pr_debug("pktgen: entering pktgen_run. %p\n", t);
2845 if_lock(t);
2846 list_for_each_entry(pkt_dev, &t->if_list, list) {
2849 * setup odev and create initial packet.
2851 pktgen_setup_inject(pkt_dev);
2853 if (pkt_dev->odev) {
2854 pktgen_clear_counters(pkt_dev);
2855 pkt_dev->running = 1; /* Cranke yeself! */
2856 pkt_dev->skb = NULL;
2857 pkt_dev->started_at = getCurUs();
2858 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2859 pkt_dev->next_tx_ns = 0;
2861 strcpy(pkt_dev->result, "Starting");
2862 started++;
2863 } else
2864 strcpy(pkt_dev->result, "Error starting");
2866 if_unlock(t);
2867 if (started)
2868 t->control &= ~(T_STOP);
2871 static void pktgen_stop_all_threads_ifs(void)
2873 struct pktgen_thread *t;
2875 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
2877 mutex_lock(&pktgen_thread_lock);
2879 list_for_each_entry(t, &pktgen_threads, th_list)
2880 t->control |= T_STOP;
2882 mutex_unlock(&pktgen_thread_lock);
2885 static int thread_is_running(struct pktgen_thread *t)
2887 struct pktgen_dev *pkt_dev;
2888 int res = 0;
2890 list_for_each_entry(pkt_dev, &t->if_list, list)
2891 if (pkt_dev->running) {
2892 res = 1;
2893 break;
2895 return res;
2898 static int pktgen_wait_thread_run(struct pktgen_thread *t)
2900 if_lock(t);
2902 while (thread_is_running(t)) {
2904 if_unlock(t);
2906 msleep_interruptible(100);
2908 if (signal_pending(current))
2909 goto signal;
2910 if_lock(t);
2912 if_unlock(t);
2913 return 1;
2914 signal:
2915 return 0;
2918 static int pktgen_wait_all_threads_run(void)
2920 struct pktgen_thread *t;
2921 int sig = 1;
2923 mutex_lock(&pktgen_thread_lock);
2925 list_for_each_entry(t, &pktgen_threads, th_list) {
2926 sig = pktgen_wait_thread_run(t);
2927 if (sig == 0)
2928 break;
2931 if (sig == 0)
2932 list_for_each_entry(t, &pktgen_threads, th_list)
2933 t->control |= (T_STOP);
2935 mutex_unlock(&pktgen_thread_lock);
2936 return sig;
2939 static void pktgen_run_all_threads(void)
2941 struct pktgen_thread *t;
2943 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
2945 mutex_lock(&pktgen_thread_lock);
2947 list_for_each_entry(t, &pktgen_threads, th_list)
2948 t->control |= (T_RUN);
2950 mutex_unlock(&pktgen_thread_lock);
2952 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2954 pktgen_wait_all_threads_run();
2957 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2959 __u64 total_us, bps, mbps, pps, idle;
2960 char *p = pkt_dev->result;
2962 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
2964 idle = pkt_dev->idle_acc;
2966 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2967 (unsigned long long)total_us,
2968 (unsigned long long)(total_us - idle),
2969 (unsigned long long)idle,
2970 (unsigned long long)pkt_dev->sofar,
2971 pkt_dev->cur_pkt_size, nr_frags);
2973 pps = pkt_dev->sofar * USEC_PER_SEC;
2975 while ((total_us >> 32) != 0) {
2976 pps >>= 1;
2977 total_us >>= 1;
2980 do_div(pps, total_us);
2982 bps = pps * 8 * pkt_dev->cur_pkt_size;
2984 mbps = bps;
2985 do_div(mbps, 1000000);
2986 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2987 (unsigned long long)pps,
2988 (unsigned long long)mbps,
2989 (unsigned long long)bps,
2990 (unsigned long long)pkt_dev->errors);
2993 /* Set stopped-at timer, remove from running list, do counters & statistics */
2995 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
2997 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
2999 if (!pkt_dev->running) {
3000 printk("pktgen: interface: %s is already stopped\n",
3001 pkt_dev->odev->name);
3002 return -EINVAL;
3005 pkt_dev->stopped_at = getCurUs();
3006 pkt_dev->running = 0;
3008 show_results(pkt_dev, nr_frags);
3010 return 0;
3013 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3015 struct pktgen_dev *pkt_dev, *best = NULL;
3017 if_lock(t);
3019 list_for_each_entry(pkt_dev, &t->if_list, list) {
3020 if (!pkt_dev->running)
3021 continue;
3022 if (best == NULL)
3023 best = pkt_dev;
3024 else if (pkt_dev->next_tx_us < best->next_tx_us)
3025 best = pkt_dev;
3027 if_unlock(t);
3028 return best;
3031 static void pktgen_stop(struct pktgen_thread *t)
3033 struct pktgen_dev *pkt_dev;
3035 pr_debug("pktgen: entering pktgen_stop\n");
3037 if_lock(t);
3039 list_for_each_entry(pkt_dev, &t->if_list, list) {
3040 pktgen_stop_device(pkt_dev);
3041 if (pkt_dev->skb)
3042 kfree_skb(pkt_dev->skb);
3044 pkt_dev->skb = NULL;
3047 if_unlock(t);
3051 * one of our devices needs to be removed - find it
3052 * and remove it
3054 static void pktgen_rem_one_if(struct pktgen_thread *t)
3056 struct list_head *q, *n;
3057 struct pktgen_dev *cur;
3059 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3061 if_lock(t);
3063 list_for_each_safe(q, n, &t->if_list) {
3064 cur = list_entry(q, struct pktgen_dev, list);
3066 if (!cur->removal_mark)
3067 continue;
3069 if (cur->skb)
3070 kfree_skb(cur->skb);
3071 cur->skb = NULL;
3073 pktgen_remove_device(t, cur);
3075 break;
3078 if_unlock(t);
3081 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3083 struct list_head *q, *n;
3084 struct pktgen_dev *cur;
3086 /* Remove all devices, free mem */
3088 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3089 if_lock(t);
3091 list_for_each_safe(q, n, &t->if_list) {
3092 cur = list_entry(q, struct pktgen_dev, list);
3094 if (cur->skb)
3095 kfree_skb(cur->skb);
3096 cur->skb = NULL;
3098 pktgen_remove_device(t, cur);
3101 if_unlock(t);
3104 static void pktgen_rem_thread(struct pktgen_thread *t)
3106 /* Remove from the thread list */
3108 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3110 mutex_lock(&pktgen_thread_lock);
3112 list_del(&t->th_list);
3114 mutex_unlock(&pktgen_thread_lock);
3117 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3119 struct net_device *odev = NULL;
3120 __u64 idle_start = 0;
3121 int ret;
3123 odev = pkt_dev->odev;
3125 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3126 u64 now;
3128 now = getCurUs();
3129 if (now < pkt_dev->next_tx_us)
3130 spin(pkt_dev, pkt_dev->next_tx_us);
3132 /* This is max DELAY, this has special meaning of
3133 * "never transmit"
3135 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3136 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3137 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3138 goto out;
3142 if (netif_queue_stopped(odev) || need_resched()) {
3143 idle_start = getCurUs();
3145 if (!netif_running(odev)) {
3146 pktgen_stop_device(pkt_dev);
3147 if (pkt_dev->skb)
3148 kfree_skb(pkt_dev->skb);
3149 pkt_dev->skb = NULL;
3150 goto out;
3152 if (need_resched())
3153 schedule();
3155 pkt_dev->idle_acc += getCurUs() - idle_start;
3157 if (netif_queue_stopped(odev)) {
3158 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3159 pkt_dev->next_tx_ns = 0;
3160 goto out; /* Try the next interface */
3164 if (pkt_dev->last_ok || !pkt_dev->skb) {
3165 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3166 || (!pkt_dev->skb)) {
3167 /* build a new pkt */
3168 if (pkt_dev->skb)
3169 kfree_skb(pkt_dev->skb);
3171 pkt_dev->skb = fill_packet(odev, pkt_dev);
3172 if (pkt_dev->skb == NULL) {
3173 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3174 schedule();
3175 pkt_dev->clone_count--; /* back out increment, OOM */
3176 goto out;
3178 pkt_dev->allocated_skbs++;
3179 pkt_dev->clone_count = 0; /* reset counter */
3183 netif_tx_lock_bh(odev);
3184 if (!netif_queue_stopped(odev)) {
3186 atomic_inc(&(pkt_dev->skb->users));
3187 retry_now:
3188 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3189 if (likely(ret == NETDEV_TX_OK)) {
3190 pkt_dev->last_ok = 1;
3191 pkt_dev->sofar++;
3192 pkt_dev->seq_num++;
3193 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3195 } else if (ret == NETDEV_TX_LOCKED
3196 && (odev->features & NETIF_F_LLTX)) {
3197 cpu_relax();
3198 goto retry_now;
3199 } else { /* Retry it next time */
3201 atomic_dec(&(pkt_dev->skb->users));
3203 if (debug && net_ratelimit())
3204 printk(KERN_INFO "pktgen: Hard xmit error\n");
3206 pkt_dev->errors++;
3207 pkt_dev->last_ok = 0;
3210 pkt_dev->next_tx_us = getCurUs();
3211 pkt_dev->next_tx_ns = 0;
3213 pkt_dev->next_tx_us += pkt_dev->delay_us;
3214 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3216 if (pkt_dev->next_tx_ns > 1000) {
3217 pkt_dev->next_tx_us++;
3218 pkt_dev->next_tx_ns -= 1000;
3222 else { /* Retry it next time */
3223 pkt_dev->last_ok = 0;
3224 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3225 pkt_dev->next_tx_ns = 0;
3228 netif_tx_unlock_bh(odev);
3230 /* If pkt_dev->count is zero, then run forever */
3231 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3232 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3233 idle_start = getCurUs();
3234 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3235 if (signal_pending(current)) {
3236 break;
3238 schedule();
3240 pkt_dev->idle_acc += getCurUs() - idle_start;
3243 /* Done with this */
3244 pktgen_stop_device(pkt_dev);
3245 if (pkt_dev->skb)
3246 kfree_skb(pkt_dev->skb);
3247 pkt_dev->skb = NULL;
3249 out:;
3253 * Main loop of the thread goes here
3256 static int pktgen_thread_worker(void *arg)
3258 DEFINE_WAIT(wait);
3259 struct pktgen_thread *t = arg;
3260 struct pktgen_dev *pkt_dev = NULL;
3261 int cpu = t->cpu;
3262 u32 max_before_softirq;
3263 u32 tx_since_softirq = 0;
3265 BUG_ON(smp_processor_id() != cpu);
3267 init_waitqueue_head(&t->queue);
3269 t->pid = current->pid;
3271 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
3273 max_before_softirq = t->max_before_softirq;
3275 set_current_state(TASK_INTERRUPTIBLE);
3277 while (!kthread_should_stop()) {
3278 pkt_dev = next_to_run(t);
3280 if (!pkt_dev &&
3281 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3282 == 0) {
3283 prepare_to_wait(&(t->queue), &wait,
3284 TASK_INTERRUPTIBLE);
3285 schedule_timeout(HZ / 10);
3286 finish_wait(&(t->queue), &wait);
3289 __set_current_state(TASK_RUNNING);
3291 if (pkt_dev) {
3293 pktgen_xmit(pkt_dev);
3296 * We like to stay RUNNING but must also give
3297 * others fair share.
3300 tx_since_softirq += pkt_dev->last_ok;
3302 if (tx_since_softirq > max_before_softirq) {
3303 if (local_softirq_pending())
3304 do_softirq();
3305 tx_since_softirq = 0;
3309 if (t->control & T_STOP) {
3310 pktgen_stop(t);
3311 t->control &= ~(T_STOP);
3314 if (t->control & T_RUN) {
3315 pktgen_run(t);
3316 t->control &= ~(T_RUN);
3319 if (t->control & T_REMDEVALL) {
3320 pktgen_rem_all_ifs(t);
3321 t->control &= ~(T_REMDEVALL);
3324 if (t->control & T_REMDEV) {
3325 pktgen_rem_one_if(t);
3326 t->control &= ~(T_REMDEV);
3329 try_to_freeze();
3331 set_current_state(TASK_INTERRUPTIBLE);
3334 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3335 pktgen_stop(t);
3337 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3338 pktgen_rem_all_ifs(t);
3340 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3341 pktgen_rem_thread(t);
3343 return 0;
3346 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3347 const char *ifname)
3349 struct pktgen_dev *p, *pkt_dev = NULL;
3350 if_lock(t);
3352 list_for_each_entry(p, &t->if_list, list)
3353 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3354 pkt_dev = p;
3355 break;
3358 if_unlock(t);
3359 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3360 return pkt_dev;
3364 * Adds a dev at front of if_list.
3367 static int add_dev_to_thread(struct pktgen_thread *t,
3368 struct pktgen_dev *pkt_dev)
3370 int rv = 0;
3372 if_lock(t);
3374 if (pkt_dev->pg_thread) {
3375 printk("pktgen: ERROR: already assigned to a thread.\n");
3376 rv = -EBUSY;
3377 goto out;
3380 list_add(&pkt_dev->list, &t->if_list);
3381 pkt_dev->pg_thread = t;
3382 pkt_dev->running = 0;
3384 out:
3385 if_unlock(t);
3386 return rv;
3389 /* Called under thread lock */
3391 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3393 struct pktgen_dev *pkt_dev;
3394 int err;
3396 /* We don't allow a device to be on several threads */
3398 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3399 if (pkt_dev) {
3400 printk("pktgen: ERROR: interface already used.\n");
3401 return -EBUSY;
3404 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3405 if (!pkt_dev)
3406 return -ENOMEM;
3408 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3409 if (pkt_dev->flows == NULL) {
3410 kfree(pkt_dev);
3411 return -ENOMEM;
3413 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3415 pkt_dev->removal_mark = 0;
3416 pkt_dev->min_pkt_size = ETH_ZLEN;
3417 pkt_dev->max_pkt_size = ETH_ZLEN;
3418 pkt_dev->nfrags = 0;
3419 pkt_dev->clone_skb = pg_clone_skb_d;
3420 pkt_dev->delay_us = pg_delay_d / 1000;
3421 pkt_dev->delay_ns = pg_delay_d % 1000;
3422 pkt_dev->count = pg_count_d;
3423 pkt_dev->sofar = 0;
3424 pkt_dev->udp_src_min = 9; /* sink port */
3425 pkt_dev->udp_src_max = 9;
3426 pkt_dev->udp_dst_min = 9;
3427 pkt_dev->udp_dst_max = 9;
3429 pkt_dev->vlan_p = 0;
3430 pkt_dev->vlan_cfi = 0;
3431 pkt_dev->vlan_id = 0xffff;
3432 pkt_dev->svlan_p = 0;
3433 pkt_dev->svlan_cfi = 0;
3434 pkt_dev->svlan_id = 0xffff;
3436 err = pktgen_setup_dev(pkt_dev, ifname);
3437 if (err)
3438 goto out1;
3440 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3441 if (!pkt_dev->entry) {
3442 printk("pktgen: cannot create %s/%s procfs entry.\n",
3443 PG_PROC_DIR, ifname);
3444 err = -EINVAL;
3445 goto out2;
3447 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3448 pkt_dev->entry->data = pkt_dev;
3450 return add_dev_to_thread(t, pkt_dev);
3451 out2:
3452 dev_put(pkt_dev->odev);
3453 out1:
3454 if (pkt_dev->flows)
3455 vfree(pkt_dev->flows);
3456 kfree(pkt_dev);
3457 return err;
3460 static int __init pktgen_create_thread(int cpu)
3462 struct pktgen_thread *t;
3463 struct proc_dir_entry *pe;
3464 struct task_struct *p;
3466 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3467 if (!t) {
3468 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3469 return -ENOMEM;
3472 spin_lock_init(&t->if_lock);
3473 t->cpu = cpu;
3475 INIT_LIST_HEAD(&t->if_list);
3477 list_add_tail(&t->th_list, &pktgen_threads);
3479 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3480 if (IS_ERR(p)) {
3481 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3482 list_del(&t->th_list);
3483 kfree(t);
3484 return PTR_ERR(p);
3486 kthread_bind(p, cpu);
3487 t->tsk = p;
3489 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3490 if (!pe) {
3491 printk("pktgen: cannot create %s/%s procfs entry.\n",
3492 PG_PROC_DIR, t->tsk->comm);
3493 kthread_stop(p);
3494 list_del(&t->th_list);
3495 kfree(t);
3496 return -EINVAL;
3499 pe->proc_fops = &pktgen_thread_fops;
3500 pe->data = t;
3502 wake_up_process(p);
3504 return 0;
3508 * Removes a device from the thread if_list.
3510 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3511 struct pktgen_dev *pkt_dev)
3513 struct list_head *q, *n;
3514 struct pktgen_dev *p;
3516 list_for_each_safe(q, n, &t->if_list) {
3517 p = list_entry(q, struct pktgen_dev, list);
3518 if (p == pkt_dev)
3519 list_del(&p->list);
3523 static int pktgen_remove_device(struct pktgen_thread *t,
3524 struct pktgen_dev *pkt_dev)
3527 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3529 if (pkt_dev->running) {
3530 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3531 pktgen_stop_device(pkt_dev);
3534 /* Dis-associate from the interface */
3536 if (pkt_dev->odev) {
3537 dev_put(pkt_dev->odev);
3538 pkt_dev->odev = NULL;
3541 /* And update the thread if_list */
3543 _rem_dev_from_if_list(t, pkt_dev);
3545 if (pkt_dev->entry)
3546 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3548 if (pkt_dev->flows)
3549 vfree(pkt_dev->flows);
3550 kfree(pkt_dev);
3551 return 0;
3554 static int __init pg_init(void)
3556 int cpu;
3557 struct proc_dir_entry *pe;
3559 printk(version);
3561 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3562 if (!pg_proc_dir)
3563 return -ENODEV;
3564 pg_proc_dir->owner = THIS_MODULE;
3566 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3567 if (pe == NULL) {
3568 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3569 PGCTRL);
3570 proc_net_remove(PG_PROC_DIR);
3571 return -EINVAL;
3574 pe->proc_fops = &pktgen_fops;
3575 pe->data = NULL;
3577 /* Register us to receive netdevice events */
3578 register_netdevice_notifier(&pktgen_notifier_block);
3580 for_each_online_cpu(cpu) {
3581 int err;
3583 err = pktgen_create_thread(cpu);
3584 if (err)
3585 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3586 cpu, err);
3589 if (list_empty(&pktgen_threads)) {
3590 printk("pktgen: ERROR: Initialization failed for all threads\n");
3591 unregister_netdevice_notifier(&pktgen_notifier_block);
3592 remove_proc_entry(PGCTRL, pg_proc_dir);
3593 proc_net_remove(PG_PROC_DIR);
3594 return -ENODEV;
3597 return 0;
3600 static void __exit pg_cleanup(void)
3602 struct pktgen_thread *t;
3603 struct list_head *q, *n;
3604 wait_queue_head_t queue;
3605 init_waitqueue_head(&queue);
3607 /* Stop all interfaces & threads */
3609 list_for_each_safe(q, n, &pktgen_threads) {
3610 t = list_entry(q, struct pktgen_thread, th_list);
3611 kthread_stop(t->tsk);
3612 kfree(t);
3615 /* Un-register us from receiving netdevice events */
3616 unregister_netdevice_notifier(&pktgen_notifier_block);
3618 /* Clean up proc file system */
3619 remove_proc_entry(PGCTRL, pg_proc_dir);
3620 proc_net_remove(PG_PROC_DIR);
3623 module_init(pg_init);
3624 module_exit(pg_cleanup);
3626 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3627 MODULE_DESCRIPTION("Packet Generator tool");
3628 MODULE_LICENSE("GPL");
3629 module_param(pg_count_d, int, 0);
3630 module_param(pg_delay_d, int, 0);
3631 module_param(pg_clone_skb_d, int, 0);
3632 module_param(debug, int, 0);