[PKTGEN]: Centralize packet overhead tracking
[linux-2.6/mini2440.git] / net / core / pktgen.c
blob9f0a780aa916fda9e53637e625ddb12c244935a0
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 pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
232 int nfrags;
233 __u32 delay_us; /* Default delay */
234 __u32 delay_ns;
235 __u64 count; /* Default No packets to send */
236 __u64 sofar; /* How many pkts we've sent so far */
237 __u64 tx_bytes; /* How many bytes we've transmitted */
238 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
240 /* runtime counters relating to clone_skb */
241 __u64 next_tx_us; /* timestamp of when to tx next */
242 __u32 next_tx_ns;
244 __u64 allocated_skbs;
245 __u32 clone_count;
246 int last_ok; /* Was last skb sent?
247 * Or a failed transmit of some sort? This will keep
248 * sequence numbers in order, for example.
250 __u64 started_at; /* micro-seconds */
251 __u64 stopped_at; /* micro-seconds */
252 __u64 idle_acc; /* micro-seconds */
253 __u32 seq_num;
255 int clone_skb; /* Use multiple SKBs during packet gen. If this number
256 * is greater than 1, then that many copies of the same
257 * packet will be sent before a new packet is allocated.
258 * For instance, if you want to send 1024 identical packets
259 * before creating a new packet, set clone_skb to 1024.
262 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
263 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
264 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
265 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
267 struct in6_addr in6_saddr;
268 struct in6_addr in6_daddr;
269 struct in6_addr cur_in6_daddr;
270 struct in6_addr cur_in6_saddr;
271 /* For ranges */
272 struct in6_addr min_in6_daddr;
273 struct in6_addr max_in6_daddr;
274 struct in6_addr min_in6_saddr;
275 struct in6_addr max_in6_saddr;
277 /* If we're doing ranges, random or incremental, then this
278 * defines the min/max for those ranges.
280 __be32 saddr_min; /* inclusive, source IP address */
281 __be32 saddr_max; /* exclusive, source IP address */
282 __be32 daddr_min; /* inclusive, dest IP address */
283 __be32 daddr_max; /* exclusive, dest IP address */
285 __u16 udp_src_min; /* inclusive, source UDP port */
286 __u16 udp_src_max; /* exclusive, source UDP port */
287 __u16 udp_dst_min; /* inclusive, dest UDP port */
288 __u16 udp_dst_max; /* exclusive, dest UDP port */
290 /* DSCP + ECN */
291 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
292 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
294 /* MPLS */
295 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
296 __be32 labels[MAX_MPLS_LABELS];
298 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
299 __u8 vlan_p;
300 __u8 vlan_cfi;
301 __u16 vlan_id; /* 0xffff means no vlan tag */
303 __u8 svlan_p;
304 __u8 svlan_cfi;
305 __u16 svlan_id; /* 0xffff means no svlan tag */
307 __u32 src_mac_count; /* How many MACs to iterate through */
308 __u32 dst_mac_count; /* How many MACs to iterate through */
310 unsigned char dst_mac[ETH_ALEN];
311 unsigned char src_mac[ETH_ALEN];
313 __u32 cur_dst_mac_offset;
314 __u32 cur_src_mac_offset;
315 __be32 cur_saddr;
316 __be32 cur_daddr;
317 __u16 cur_udp_dst;
318 __u16 cur_udp_src;
319 __u32 cur_pkt_size;
321 __u8 hh[14];
322 /* = {
323 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
325 We fill in SRC address later
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
327 0x08, 0x00
330 __u16 pad; /* pad out the hh struct to an even 16 bytes */
332 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
333 * are transmitting the same one multiple times
335 struct net_device *odev; /* The out-going device. Note that the device should
336 * have it's pg_info pointer pointing back to this
337 * device. This will be set when the user specifies
338 * the out-going device name (not when the inject is
339 * started as it used to do.)
341 struct flow_state *flows;
342 unsigned cflows; /* Concurrent flows (config) */
343 unsigned lflow; /* Flow length (config) */
344 unsigned nflows; /* accumulated flows (stats) */
346 char result[512];
349 struct pktgen_hdr {
350 __be32 pgh_magic;
351 __be32 seq_num;
352 __be32 tv_sec;
353 __be32 tv_usec;
356 struct pktgen_thread {
357 spinlock_t if_lock;
358 struct list_head if_list; /* All device here */
359 struct list_head th_list;
360 struct task_struct *tsk;
361 char result[512];
362 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
364 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
366 u32 control;
367 int pid;
368 int cpu;
370 wait_queue_head_t queue;
373 #define REMOVE 1
374 #define FIND 0
376 /* This code works around the fact that do_div cannot handle two 64-bit
377 numbers, and regular 64-bit division doesn't work on x86 kernels.
378 --Ben
381 #define PG_DIV 0
383 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
384 * Function copied/adapted/optimized from:
386 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
388 * Copyright 1994, University of Cambridge Computer Laboratory
389 * All Rights Reserved.
392 static inline s64 divremdi3(s64 x, s64 y, int type)
394 u64 a = (x < 0) ? -x : x;
395 u64 b = (y < 0) ? -y : y;
396 u64 res = 0, d = 1;
398 if (b > 0) {
399 while (b < a) {
400 b <<= 1;
401 d <<= 1;
405 do {
406 if (a >= b) {
407 a -= b;
408 res += d;
410 b >>= 1;
411 d >>= 1;
413 while (d);
415 if (PG_DIV == type) {
416 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
417 } else {
418 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
422 /* End of hacks to deal with 64-bit math on x86 */
424 /** Convert to milliseconds */
425 static inline __u64 tv_to_ms(const struct timeval *tv)
427 __u64 ms = tv->tv_usec / 1000;
428 ms += (__u64) tv->tv_sec * (__u64) 1000;
429 return ms;
432 /** Convert to micro-seconds */
433 static inline __u64 tv_to_us(const struct timeval *tv)
435 __u64 us = tv->tv_usec;
436 us += (__u64) tv->tv_sec * (__u64) 1000000;
437 return us;
440 static inline __u64 pg_div(__u64 n, __u32 base)
442 __u64 tmp = n;
443 do_div(tmp, base);
444 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
445 n, base, tmp); */
446 return tmp;
449 static inline __u64 pg_div64(__u64 n, __u64 base)
451 __u64 tmp = n;
453 * How do we know if the architecture we are running on
454 * supports division with 64 bit base?
457 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
459 do_div(tmp, base);
460 #else
461 tmp = divremdi3(n, base, PG_DIV);
462 #endif
463 return tmp;
466 static inline __u64 getCurMs(void)
468 struct timeval tv;
469 do_gettimeofday(&tv);
470 return tv_to_ms(&tv);
473 static inline __u64 getCurUs(void)
475 struct timeval tv;
476 do_gettimeofday(&tv);
477 return tv_to_us(&tv);
480 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
482 return tv_to_us(a) - tv_to_us(b);
485 /* old include end */
487 static char version[] __initdata = VERSION;
489 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
490 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
491 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
492 const char *ifname);
493 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
494 static void pktgen_run_all_threads(void);
495 static void pktgen_stop_all_threads_ifs(void);
496 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
497 static void pktgen_stop(struct pktgen_thread *t);
498 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
500 static unsigned int scan_ip6(const char *s, char ip[16]);
501 static unsigned int fmt_ip6(char *s, const char ip[16]);
503 /* Module parameters, defaults. */
504 static int pg_count_d = 1000; /* 1000 pkts by default */
505 static int pg_delay_d;
506 static int pg_clone_skb_d;
507 static int debug;
509 static DEFINE_MUTEX(pktgen_thread_lock);
510 static LIST_HEAD(pktgen_threads);
512 static struct notifier_block pktgen_notifier_block = {
513 .notifier_call = pktgen_device_event,
517 * /proc handling functions
521 static int pgctrl_show(struct seq_file *seq, void *v)
523 seq_puts(seq, VERSION);
524 return 0;
527 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
528 size_t count, loff_t * ppos)
530 int err = 0;
531 char data[128];
533 if (!capable(CAP_NET_ADMIN)) {
534 err = -EPERM;
535 goto out;
538 if (count > sizeof(data))
539 count = sizeof(data);
541 if (copy_from_user(data, buf, count)) {
542 err = -EFAULT;
543 goto out;
545 data[count - 1] = 0; /* Make string */
547 if (!strcmp(data, "stop"))
548 pktgen_stop_all_threads_ifs();
550 else if (!strcmp(data, "start"))
551 pktgen_run_all_threads();
553 else
554 printk("pktgen: Unknown command: %s\n", data);
556 err = count;
558 out:
559 return err;
562 static int pgctrl_open(struct inode *inode, struct file *file)
564 return single_open(file, pgctrl_show, PDE(inode)->data);
567 static const struct file_operations pktgen_fops = {
568 .owner = THIS_MODULE,
569 .open = pgctrl_open,
570 .read = seq_read,
571 .llseek = seq_lseek,
572 .write = pgctrl_write,
573 .release = single_release,
576 static int pktgen_if_show(struct seq_file *seq, void *v)
578 int i;
579 struct pktgen_dev *pkt_dev = seq->private;
580 __u64 sa;
581 __u64 stopped;
582 __u64 now = getCurUs();
584 seq_printf(seq,
585 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
586 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
587 pkt_dev->max_pkt_size);
589 seq_printf(seq,
590 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
591 pkt_dev->nfrags,
592 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
593 pkt_dev->clone_skb, pkt_dev->odev->name);
595 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
596 pkt_dev->lflow);
598 if (pkt_dev->flags & F_IPV6) {
599 char b1[128], b2[128], b3[128];
600 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
601 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
602 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
603 seq_printf(seq,
604 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
605 b2, b3);
607 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
608 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
609 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
610 seq_printf(seq,
611 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
612 b2, b3);
614 } else
615 seq_printf(seq,
616 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
617 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
618 pkt_dev->src_max);
620 seq_puts(seq, " src_mac: ");
622 if (is_zero_ether_addr(pkt_dev->src_mac))
623 for (i = 0; i < 6; i++)
624 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
625 i == 5 ? " " : ":");
626 else
627 for (i = 0; i < 6; i++)
628 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
629 i == 5 ? " " : ":");
631 seq_printf(seq, "dst_mac: ");
632 for (i = 0; i < 6; i++)
633 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
634 i == 5 ? "\n" : ":");
636 seq_printf(seq,
637 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
638 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
639 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
641 seq_printf(seq,
642 " src_mac_count: %d dst_mac_count: %d\n",
643 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
645 if (pkt_dev->nr_labels) {
646 unsigned i;
647 seq_printf(seq, " mpls: ");
648 for (i = 0; i < pkt_dev->nr_labels; i++)
649 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
650 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
653 if (pkt_dev->vlan_id != 0xffff) {
654 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
655 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
658 if (pkt_dev->svlan_id != 0xffff) {
659 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
660 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
663 if (pkt_dev->tos) {
664 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
667 if (pkt_dev->traffic_class) {
668 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
671 seq_printf(seq, " Flags: ");
673 if (pkt_dev->flags & F_IPV6)
674 seq_printf(seq, "IPV6 ");
676 if (pkt_dev->flags & F_IPSRC_RND)
677 seq_printf(seq, "IPSRC_RND ");
679 if (pkt_dev->flags & F_IPDST_RND)
680 seq_printf(seq, "IPDST_RND ");
682 if (pkt_dev->flags & F_TXSIZE_RND)
683 seq_printf(seq, "TXSIZE_RND ");
685 if (pkt_dev->flags & F_UDPSRC_RND)
686 seq_printf(seq, "UDPSRC_RND ");
688 if (pkt_dev->flags & F_UDPDST_RND)
689 seq_printf(seq, "UDPDST_RND ");
691 if (pkt_dev->flags & F_MPLS_RND)
692 seq_printf(seq, "MPLS_RND ");
694 if (pkt_dev->flags & F_MACSRC_RND)
695 seq_printf(seq, "MACSRC_RND ");
697 if (pkt_dev->flags & F_MACDST_RND)
698 seq_printf(seq, "MACDST_RND ");
700 if (pkt_dev->flags & F_VID_RND)
701 seq_printf(seq, "VID_RND ");
703 if (pkt_dev->flags & F_SVID_RND)
704 seq_printf(seq, "SVID_RND ");
706 seq_puts(seq, "\n");
708 sa = pkt_dev->started_at;
709 stopped = pkt_dev->stopped_at;
710 if (pkt_dev->running)
711 stopped = now; /* not really stopped, more like last-running-at */
713 seq_printf(seq,
714 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
715 (unsigned long long)pkt_dev->sofar,
716 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
717 (unsigned long long)stopped,
718 (unsigned long long)pkt_dev->idle_acc);
720 seq_printf(seq,
721 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
722 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
723 pkt_dev->cur_src_mac_offset);
725 if (pkt_dev->flags & F_IPV6) {
726 char b1[128], b2[128];
727 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
728 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
729 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
730 } else
731 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
732 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
734 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
735 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
737 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
739 if (pkt_dev->result[0])
740 seq_printf(seq, "Result: %s\n", pkt_dev->result);
741 else
742 seq_printf(seq, "Result: Idle\n");
744 return 0;
748 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
750 int i = 0;
751 *num = 0;
753 for (; i < maxlen; i++) {
754 char c;
755 *num <<= 4;
756 if (get_user(c, &user_buffer[i]))
757 return -EFAULT;
758 if ((c >= '0') && (c <= '9'))
759 *num |= c - '0';
760 else if ((c >= 'a') && (c <= 'f'))
761 *num |= c - 'a' + 10;
762 else if ((c >= 'A') && (c <= 'F'))
763 *num |= c - 'A' + 10;
764 else
765 break;
767 return i;
770 static int count_trail_chars(const char __user * user_buffer,
771 unsigned int maxlen)
773 int i;
775 for (i = 0; i < maxlen; i++) {
776 char c;
777 if (get_user(c, &user_buffer[i]))
778 return -EFAULT;
779 switch (c) {
780 case '\"':
781 case '\n':
782 case '\r':
783 case '\t':
784 case ' ':
785 case '=':
786 break;
787 default:
788 goto done;
791 done:
792 return i;
795 static unsigned long num_arg(const char __user * user_buffer,
796 unsigned long maxlen, unsigned long *num)
798 int i = 0;
799 *num = 0;
801 for (; i < maxlen; i++) {
802 char c;
803 if (get_user(c, &user_buffer[i]))
804 return -EFAULT;
805 if ((c >= '0') && (c <= '9')) {
806 *num *= 10;
807 *num += c - '0';
808 } else
809 break;
811 return i;
814 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
816 int i = 0;
818 for (; i < maxlen; i++) {
819 char c;
820 if (get_user(c, &user_buffer[i]))
821 return -EFAULT;
822 switch (c) {
823 case '\"':
824 case '\n':
825 case '\r':
826 case '\t':
827 case ' ':
828 goto done_str;
829 break;
830 default:
831 break;
834 done_str:
835 return i;
838 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
840 unsigned n = 0;
841 char c;
842 ssize_t i = 0;
843 int len;
845 pkt_dev->nr_labels = 0;
846 do {
847 __u32 tmp;
848 len = hex32_arg(&buffer[i], 8, &tmp);
849 if (len <= 0)
850 return len;
851 pkt_dev->labels[n] = htonl(tmp);
852 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
853 pkt_dev->flags |= F_MPLS_RND;
854 i += len;
855 if (get_user(c, &buffer[i]))
856 return -EFAULT;
857 i++;
858 n++;
859 if (n >= MAX_MPLS_LABELS)
860 return -E2BIG;
861 } while (c == ',');
863 pkt_dev->nr_labels = n;
864 return i;
867 static ssize_t pktgen_if_write(struct file *file,
868 const char __user * user_buffer, size_t count,
869 loff_t * offset)
871 struct seq_file *seq = (struct seq_file *)file->private_data;
872 struct pktgen_dev *pkt_dev = seq->private;
873 int i = 0, max, len;
874 char name[16], valstr[32];
875 unsigned long value = 0;
876 char *pg_result = NULL;
877 int tmp = 0;
878 char buf[128];
880 pg_result = &(pkt_dev->result[0]);
882 if (count < 1) {
883 printk("pktgen: wrong command format\n");
884 return -EINVAL;
887 max = count - i;
888 tmp = count_trail_chars(&user_buffer[i], max);
889 if (tmp < 0) {
890 printk("pktgen: illegal format\n");
891 return tmp;
893 i += tmp;
895 /* Read variable name */
897 len = strn_len(&user_buffer[i], sizeof(name) - 1);
898 if (len < 0) {
899 return len;
901 memset(name, 0, sizeof(name));
902 if (copy_from_user(name, &user_buffer[i], len))
903 return -EFAULT;
904 i += len;
906 max = count - i;
907 len = count_trail_chars(&user_buffer[i], max);
908 if (len < 0)
909 return len;
911 i += len;
913 if (debug) {
914 char tb[count + 1];
915 if (copy_from_user(tb, user_buffer, count))
916 return -EFAULT;
917 tb[count] = 0;
918 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
919 (unsigned long)count, tb);
922 if (!strcmp(name, "min_pkt_size")) {
923 len = num_arg(&user_buffer[i], 10, &value);
924 if (len < 0) {
925 return len;
927 i += len;
928 if (value < 14 + 20 + 8)
929 value = 14 + 20 + 8;
930 if (value != pkt_dev->min_pkt_size) {
931 pkt_dev->min_pkt_size = value;
932 pkt_dev->cur_pkt_size = value;
934 sprintf(pg_result, "OK: min_pkt_size=%u",
935 pkt_dev->min_pkt_size);
936 return count;
939 if (!strcmp(name, "max_pkt_size")) {
940 len = num_arg(&user_buffer[i], 10, &value);
941 if (len < 0) {
942 return len;
944 i += len;
945 if (value < 14 + 20 + 8)
946 value = 14 + 20 + 8;
947 if (value != pkt_dev->max_pkt_size) {
948 pkt_dev->max_pkt_size = value;
949 pkt_dev->cur_pkt_size = value;
951 sprintf(pg_result, "OK: max_pkt_size=%u",
952 pkt_dev->max_pkt_size);
953 return count;
956 /* Shortcut for min = max */
958 if (!strcmp(name, "pkt_size")) {
959 len = num_arg(&user_buffer[i], 10, &value);
960 if (len < 0) {
961 return len;
963 i += len;
964 if (value < 14 + 20 + 8)
965 value = 14 + 20 + 8;
966 if (value != pkt_dev->min_pkt_size) {
967 pkt_dev->min_pkt_size = value;
968 pkt_dev->max_pkt_size = value;
969 pkt_dev->cur_pkt_size = value;
971 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
972 return count;
975 if (!strcmp(name, "debug")) {
976 len = num_arg(&user_buffer[i], 10, &value);
977 if (len < 0) {
978 return len;
980 i += len;
981 debug = value;
982 sprintf(pg_result, "OK: debug=%u", debug);
983 return count;
986 if (!strcmp(name, "frags")) {
987 len = num_arg(&user_buffer[i], 10, &value);
988 if (len < 0) {
989 return len;
991 i += len;
992 pkt_dev->nfrags = value;
993 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
994 return count;
996 if (!strcmp(name, "delay")) {
997 len = num_arg(&user_buffer[i], 10, &value);
998 if (len < 0) {
999 return len;
1001 i += len;
1002 if (value == 0x7FFFFFFF) {
1003 pkt_dev->delay_us = 0x7FFFFFFF;
1004 pkt_dev->delay_ns = 0;
1005 } else {
1006 pkt_dev->delay_us = value / 1000;
1007 pkt_dev->delay_ns = value % 1000;
1009 sprintf(pg_result, "OK: delay=%u",
1010 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1011 return count;
1013 if (!strcmp(name, "udp_src_min")) {
1014 len = num_arg(&user_buffer[i], 10, &value);
1015 if (len < 0) {
1016 return len;
1018 i += len;
1019 if (value != pkt_dev->udp_src_min) {
1020 pkt_dev->udp_src_min = value;
1021 pkt_dev->cur_udp_src = value;
1023 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1024 return count;
1026 if (!strcmp(name, "udp_dst_min")) {
1027 len = num_arg(&user_buffer[i], 10, &value);
1028 if (len < 0) {
1029 return len;
1031 i += len;
1032 if (value != pkt_dev->udp_dst_min) {
1033 pkt_dev->udp_dst_min = value;
1034 pkt_dev->cur_udp_dst = value;
1036 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1037 return count;
1039 if (!strcmp(name, "udp_src_max")) {
1040 len = num_arg(&user_buffer[i], 10, &value);
1041 if (len < 0) {
1042 return len;
1044 i += len;
1045 if (value != pkt_dev->udp_src_max) {
1046 pkt_dev->udp_src_max = value;
1047 pkt_dev->cur_udp_src = value;
1049 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1050 return count;
1052 if (!strcmp(name, "udp_dst_max")) {
1053 len = num_arg(&user_buffer[i], 10, &value);
1054 if (len < 0) {
1055 return len;
1057 i += len;
1058 if (value != pkt_dev->udp_dst_max) {
1059 pkt_dev->udp_dst_max = value;
1060 pkt_dev->cur_udp_dst = value;
1062 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1063 return count;
1065 if (!strcmp(name, "clone_skb")) {
1066 len = num_arg(&user_buffer[i], 10, &value);
1067 if (len < 0) {
1068 return len;
1070 i += len;
1071 pkt_dev->clone_skb = value;
1073 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1074 return count;
1076 if (!strcmp(name, "count")) {
1077 len = num_arg(&user_buffer[i], 10, &value);
1078 if (len < 0) {
1079 return len;
1081 i += len;
1082 pkt_dev->count = value;
1083 sprintf(pg_result, "OK: count=%llu",
1084 (unsigned long long)pkt_dev->count);
1085 return count;
1087 if (!strcmp(name, "src_mac_count")) {
1088 len = num_arg(&user_buffer[i], 10, &value);
1089 if (len < 0) {
1090 return len;
1092 i += len;
1093 if (pkt_dev->src_mac_count != value) {
1094 pkt_dev->src_mac_count = value;
1095 pkt_dev->cur_src_mac_offset = 0;
1097 sprintf(pg_result, "OK: src_mac_count=%d",
1098 pkt_dev->src_mac_count);
1099 return count;
1101 if (!strcmp(name, "dst_mac_count")) {
1102 len = num_arg(&user_buffer[i], 10, &value);
1103 if (len < 0) {
1104 return len;
1106 i += len;
1107 if (pkt_dev->dst_mac_count != value) {
1108 pkt_dev->dst_mac_count = value;
1109 pkt_dev->cur_dst_mac_offset = 0;
1111 sprintf(pg_result, "OK: dst_mac_count=%d",
1112 pkt_dev->dst_mac_count);
1113 return count;
1115 if (!strcmp(name, "flag")) {
1116 char f[32];
1117 memset(f, 0, 32);
1118 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1119 if (len < 0) {
1120 return len;
1122 if (copy_from_user(f, &user_buffer[i], len))
1123 return -EFAULT;
1124 i += len;
1125 if (strcmp(f, "IPSRC_RND") == 0)
1126 pkt_dev->flags |= F_IPSRC_RND;
1128 else if (strcmp(f, "!IPSRC_RND") == 0)
1129 pkt_dev->flags &= ~F_IPSRC_RND;
1131 else if (strcmp(f, "TXSIZE_RND") == 0)
1132 pkt_dev->flags |= F_TXSIZE_RND;
1134 else if (strcmp(f, "!TXSIZE_RND") == 0)
1135 pkt_dev->flags &= ~F_TXSIZE_RND;
1137 else if (strcmp(f, "IPDST_RND") == 0)
1138 pkt_dev->flags |= F_IPDST_RND;
1140 else if (strcmp(f, "!IPDST_RND") == 0)
1141 pkt_dev->flags &= ~F_IPDST_RND;
1143 else if (strcmp(f, "UDPSRC_RND") == 0)
1144 pkt_dev->flags |= F_UDPSRC_RND;
1146 else if (strcmp(f, "!UDPSRC_RND") == 0)
1147 pkt_dev->flags &= ~F_UDPSRC_RND;
1149 else if (strcmp(f, "UDPDST_RND") == 0)
1150 pkt_dev->flags |= F_UDPDST_RND;
1152 else if (strcmp(f, "!UDPDST_RND") == 0)
1153 pkt_dev->flags &= ~F_UDPDST_RND;
1155 else if (strcmp(f, "MACSRC_RND") == 0)
1156 pkt_dev->flags |= F_MACSRC_RND;
1158 else if (strcmp(f, "!MACSRC_RND") == 0)
1159 pkt_dev->flags &= ~F_MACSRC_RND;
1161 else if (strcmp(f, "MACDST_RND") == 0)
1162 pkt_dev->flags |= F_MACDST_RND;
1164 else if (strcmp(f, "!MACDST_RND") == 0)
1165 pkt_dev->flags &= ~F_MACDST_RND;
1167 else if (strcmp(f, "MPLS_RND") == 0)
1168 pkt_dev->flags |= F_MPLS_RND;
1170 else if (strcmp(f, "!MPLS_RND") == 0)
1171 pkt_dev->flags &= ~F_MPLS_RND;
1173 else if (strcmp(f, "VID_RND") == 0)
1174 pkt_dev->flags |= F_VID_RND;
1176 else if (strcmp(f, "!VID_RND") == 0)
1177 pkt_dev->flags &= ~F_VID_RND;
1179 else if (strcmp(f, "SVID_RND") == 0)
1180 pkt_dev->flags |= F_SVID_RND;
1182 else if (strcmp(f, "!SVID_RND") == 0)
1183 pkt_dev->flags &= ~F_SVID_RND;
1185 else if (strcmp(f, "!IPV6") == 0)
1186 pkt_dev->flags &= ~F_IPV6;
1188 else {
1189 sprintf(pg_result,
1190 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1192 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1193 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
1194 return count;
1196 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1197 return count;
1199 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1200 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1201 if (len < 0) {
1202 return len;
1205 if (copy_from_user(buf, &user_buffer[i], len))
1206 return -EFAULT;
1207 buf[len] = 0;
1208 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1209 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1210 strncpy(pkt_dev->dst_min, buf, len);
1211 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1212 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1214 if (debug)
1215 printk("pktgen: dst_min set to: %s\n",
1216 pkt_dev->dst_min);
1217 i += len;
1218 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1219 return count;
1221 if (!strcmp(name, "dst_max")) {
1222 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1223 if (len < 0) {
1224 return len;
1227 if (copy_from_user(buf, &user_buffer[i], len))
1228 return -EFAULT;
1230 buf[len] = 0;
1231 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1232 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1233 strncpy(pkt_dev->dst_max, buf, len);
1234 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1235 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1237 if (debug)
1238 printk("pktgen: dst_max set to: %s\n",
1239 pkt_dev->dst_max);
1240 i += len;
1241 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1242 return count;
1244 if (!strcmp(name, "dst6")) {
1245 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1246 if (len < 0)
1247 return len;
1249 pkt_dev->flags |= F_IPV6;
1251 if (copy_from_user(buf, &user_buffer[i], len))
1252 return -EFAULT;
1253 buf[len] = 0;
1255 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1256 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1258 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1260 if (debug)
1261 printk("pktgen: dst6 set to: %s\n", buf);
1263 i += len;
1264 sprintf(pg_result, "OK: dst6=%s", buf);
1265 return count;
1267 if (!strcmp(name, "dst6_min")) {
1268 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1269 if (len < 0)
1270 return len;
1272 pkt_dev->flags |= F_IPV6;
1274 if (copy_from_user(buf, &user_buffer[i], len))
1275 return -EFAULT;
1276 buf[len] = 0;
1278 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1279 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1281 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1282 &pkt_dev->min_in6_daddr);
1283 if (debug)
1284 printk("pktgen: dst6_min set to: %s\n", buf);
1286 i += len;
1287 sprintf(pg_result, "OK: dst6_min=%s", buf);
1288 return count;
1290 if (!strcmp(name, "dst6_max")) {
1291 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1292 if (len < 0)
1293 return len;
1295 pkt_dev->flags |= F_IPV6;
1297 if (copy_from_user(buf, &user_buffer[i], len))
1298 return -EFAULT;
1299 buf[len] = 0;
1301 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1302 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1304 if (debug)
1305 printk("pktgen: dst6_max set to: %s\n", buf);
1307 i += len;
1308 sprintf(pg_result, "OK: dst6_max=%s", buf);
1309 return count;
1311 if (!strcmp(name, "src6")) {
1312 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1313 if (len < 0)
1314 return len;
1316 pkt_dev->flags |= F_IPV6;
1318 if (copy_from_user(buf, &user_buffer[i], len))
1319 return -EFAULT;
1320 buf[len] = 0;
1322 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1323 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1325 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1327 if (debug)
1328 printk("pktgen: src6 set to: %s\n", buf);
1330 i += len;
1331 sprintf(pg_result, "OK: src6=%s", buf);
1332 return count;
1334 if (!strcmp(name, "src_min")) {
1335 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1336 if (len < 0) {
1337 return len;
1339 if (copy_from_user(buf, &user_buffer[i], len))
1340 return -EFAULT;
1341 buf[len] = 0;
1342 if (strcmp(buf, pkt_dev->src_min) != 0) {
1343 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1344 strncpy(pkt_dev->src_min, buf, len);
1345 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1346 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1348 if (debug)
1349 printk("pktgen: src_min set to: %s\n",
1350 pkt_dev->src_min);
1351 i += len;
1352 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1353 return count;
1355 if (!strcmp(name, "src_max")) {
1356 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1357 if (len < 0) {
1358 return len;
1360 if (copy_from_user(buf, &user_buffer[i], len))
1361 return -EFAULT;
1362 buf[len] = 0;
1363 if (strcmp(buf, pkt_dev->src_max) != 0) {
1364 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1365 strncpy(pkt_dev->src_max, buf, len);
1366 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1367 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1369 if (debug)
1370 printk("pktgen: src_max set to: %s\n",
1371 pkt_dev->src_max);
1372 i += len;
1373 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1374 return count;
1376 if (!strcmp(name, "dst_mac")) {
1377 char *v = valstr;
1378 unsigned char old_dmac[ETH_ALEN];
1379 unsigned char *m = pkt_dev->dst_mac;
1380 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1382 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1383 if (len < 0) {
1384 return len;
1386 memset(valstr, 0, sizeof(valstr));
1387 if (copy_from_user(valstr, &user_buffer[i], len))
1388 return -EFAULT;
1389 i += len;
1391 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1392 if (*v >= '0' && *v <= '9') {
1393 *m *= 16;
1394 *m += *v - '0';
1396 if (*v >= 'A' && *v <= 'F') {
1397 *m *= 16;
1398 *m += *v - 'A' + 10;
1400 if (*v >= 'a' && *v <= 'f') {
1401 *m *= 16;
1402 *m += *v - 'a' + 10;
1404 if (*v == ':') {
1405 m++;
1406 *m = 0;
1410 /* Set up Dest MAC */
1411 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1412 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1414 sprintf(pg_result, "OK: dstmac");
1415 return count;
1417 if (!strcmp(name, "src_mac")) {
1418 char *v = valstr;
1419 unsigned char *m = pkt_dev->src_mac;
1421 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1422 if (len < 0) {
1423 return len;
1425 memset(valstr, 0, sizeof(valstr));
1426 if (copy_from_user(valstr, &user_buffer[i], len))
1427 return -EFAULT;
1428 i += len;
1430 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1431 if (*v >= '0' && *v <= '9') {
1432 *m *= 16;
1433 *m += *v - '0';
1435 if (*v >= 'A' && *v <= 'F') {
1436 *m *= 16;
1437 *m += *v - 'A' + 10;
1439 if (*v >= 'a' && *v <= 'f') {
1440 *m *= 16;
1441 *m += *v - 'a' + 10;
1443 if (*v == ':') {
1444 m++;
1445 *m = 0;
1449 sprintf(pg_result, "OK: srcmac");
1450 return count;
1453 if (!strcmp(name, "clear_counters")) {
1454 pktgen_clear_counters(pkt_dev);
1455 sprintf(pg_result, "OK: Clearing counters.\n");
1456 return count;
1459 if (!strcmp(name, "flows")) {
1460 len = num_arg(&user_buffer[i], 10, &value);
1461 if (len < 0) {
1462 return len;
1464 i += len;
1465 if (value > MAX_CFLOWS)
1466 value = MAX_CFLOWS;
1468 pkt_dev->cflows = value;
1469 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1470 return count;
1473 if (!strcmp(name, "flowlen")) {
1474 len = num_arg(&user_buffer[i], 10, &value);
1475 if (len < 0) {
1476 return len;
1478 i += len;
1479 pkt_dev->lflow = value;
1480 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1481 return count;
1484 if (!strcmp(name, "mpls")) {
1485 unsigned n, offset;
1486 len = get_labels(&user_buffer[i], pkt_dev);
1487 if (len < 0) { return len; }
1488 i += len;
1489 offset = sprintf(pg_result, "OK: mpls=");
1490 for (n = 0; n < pkt_dev->nr_labels; n++)
1491 offset += sprintf(pg_result + offset,
1492 "%08x%s", ntohl(pkt_dev->labels[n]),
1493 n == pkt_dev->nr_labels-1 ? "" : ",");
1495 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1496 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1497 pkt_dev->svlan_id = 0xffff;
1499 if (debug)
1500 printk("pktgen: VLAN/SVLAN auto turned off\n");
1502 return count;
1505 if (!strcmp(name, "vlan_id")) {
1506 len = num_arg(&user_buffer[i], 4, &value);
1507 if (len < 0) {
1508 return len;
1510 i += len;
1511 if (value <= 4095) {
1512 pkt_dev->vlan_id = value; /* turn on VLAN */
1514 if (debug)
1515 printk("pktgen: VLAN turned on\n");
1517 if (debug && pkt_dev->nr_labels)
1518 printk("pktgen: MPLS auto turned off\n");
1520 pkt_dev->nr_labels = 0; /* turn off MPLS */
1521 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1522 } else {
1523 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1524 pkt_dev->svlan_id = 0xffff;
1526 if (debug)
1527 printk("pktgen: VLAN/SVLAN turned off\n");
1529 return count;
1532 if (!strcmp(name, "vlan_p")) {
1533 len = num_arg(&user_buffer[i], 1, &value);
1534 if (len < 0) {
1535 return len;
1537 i += len;
1538 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1539 pkt_dev->vlan_p = value;
1540 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1541 } else {
1542 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1544 return count;
1547 if (!strcmp(name, "vlan_cfi")) {
1548 len = num_arg(&user_buffer[i], 1, &value);
1549 if (len < 0) {
1550 return len;
1552 i += len;
1553 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1554 pkt_dev->vlan_cfi = value;
1555 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1556 } else {
1557 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1559 return count;
1562 if (!strcmp(name, "svlan_id")) {
1563 len = num_arg(&user_buffer[i], 4, &value);
1564 if (len < 0) {
1565 return len;
1567 i += len;
1568 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1569 pkt_dev->svlan_id = value; /* turn on SVLAN */
1571 if (debug)
1572 printk("pktgen: SVLAN turned on\n");
1574 if (debug && pkt_dev->nr_labels)
1575 printk("pktgen: MPLS auto turned off\n");
1577 pkt_dev->nr_labels = 0; /* turn off MPLS */
1578 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1579 } else {
1580 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1581 pkt_dev->svlan_id = 0xffff;
1583 if (debug)
1584 printk("pktgen: VLAN/SVLAN turned off\n");
1586 return count;
1589 if (!strcmp(name, "svlan_p")) {
1590 len = num_arg(&user_buffer[i], 1, &value);
1591 if (len < 0) {
1592 return len;
1594 i += len;
1595 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1596 pkt_dev->svlan_p = value;
1597 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1598 } else {
1599 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1601 return count;
1604 if (!strcmp(name, "svlan_cfi")) {
1605 len = num_arg(&user_buffer[i], 1, &value);
1606 if (len < 0) {
1607 return len;
1609 i += len;
1610 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1611 pkt_dev->svlan_cfi = value;
1612 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1613 } else {
1614 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1616 return count;
1619 if (!strcmp(name, "tos")) {
1620 __u32 tmp_value = 0;
1621 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1622 if (len < 0) {
1623 return len;
1625 i += len;
1626 if (len == 2) {
1627 pkt_dev->tos = tmp_value;
1628 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1629 } else {
1630 sprintf(pg_result, "ERROR: tos must be 00-ff");
1632 return count;
1635 if (!strcmp(name, "traffic_class")) {
1636 __u32 tmp_value = 0;
1637 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1638 if (len < 0) {
1639 return len;
1641 i += len;
1642 if (len == 2) {
1643 pkt_dev->traffic_class = tmp_value;
1644 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1645 } else {
1646 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1648 return count;
1651 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1652 return -EINVAL;
1655 static int pktgen_if_open(struct inode *inode, struct file *file)
1657 return single_open(file, pktgen_if_show, PDE(inode)->data);
1660 static const struct file_operations pktgen_if_fops = {
1661 .owner = THIS_MODULE,
1662 .open = pktgen_if_open,
1663 .read = seq_read,
1664 .llseek = seq_lseek,
1665 .write = pktgen_if_write,
1666 .release = single_release,
1669 static int pktgen_thread_show(struct seq_file *seq, void *v)
1671 struct pktgen_thread *t = seq->private;
1672 struct pktgen_dev *pkt_dev;
1674 BUG_ON(!t);
1676 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1677 t->tsk->comm, t->max_before_softirq);
1679 seq_printf(seq, "Running: ");
1681 if_lock(t);
1682 list_for_each_entry(pkt_dev, &t->if_list, list)
1683 if (pkt_dev->running)
1684 seq_printf(seq, "%s ", pkt_dev->odev->name);
1686 seq_printf(seq, "\nStopped: ");
1688 list_for_each_entry(pkt_dev, &t->if_list, list)
1689 if (!pkt_dev->running)
1690 seq_printf(seq, "%s ", pkt_dev->odev->name);
1692 if (t->result[0])
1693 seq_printf(seq, "\nResult: %s\n", t->result);
1694 else
1695 seq_printf(seq, "\nResult: NA\n");
1697 if_unlock(t);
1699 return 0;
1702 static ssize_t pktgen_thread_write(struct file *file,
1703 const char __user * user_buffer,
1704 size_t count, loff_t * offset)
1706 struct seq_file *seq = (struct seq_file *)file->private_data;
1707 struct pktgen_thread *t = seq->private;
1708 int i = 0, max, len, ret;
1709 char name[40];
1710 char *pg_result;
1711 unsigned long value = 0;
1713 if (count < 1) {
1714 // sprintf(pg_result, "Wrong command format");
1715 return -EINVAL;
1718 max = count - i;
1719 len = count_trail_chars(&user_buffer[i], max);
1720 if (len < 0)
1721 return len;
1723 i += len;
1725 /* Read variable name */
1727 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1728 if (len < 0)
1729 return len;
1731 memset(name, 0, sizeof(name));
1732 if (copy_from_user(name, &user_buffer[i], len))
1733 return -EFAULT;
1734 i += len;
1736 max = count - i;
1737 len = count_trail_chars(&user_buffer[i], max);
1738 if (len < 0)
1739 return len;
1741 i += len;
1743 if (debug)
1744 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
1746 if (!t) {
1747 printk("pktgen: ERROR: No thread\n");
1748 ret = -EINVAL;
1749 goto out;
1752 pg_result = &(t->result[0]);
1754 if (!strcmp(name, "add_device")) {
1755 char f[32];
1756 memset(f, 0, 32);
1757 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1758 if (len < 0) {
1759 ret = len;
1760 goto out;
1762 if (copy_from_user(f, &user_buffer[i], len))
1763 return -EFAULT;
1764 i += len;
1765 mutex_lock(&pktgen_thread_lock);
1766 pktgen_add_device(t, f);
1767 mutex_unlock(&pktgen_thread_lock);
1768 ret = count;
1769 sprintf(pg_result, "OK: add_device=%s", f);
1770 goto out;
1773 if (!strcmp(name, "rem_device_all")) {
1774 mutex_lock(&pktgen_thread_lock);
1775 t->control |= T_REMDEVALL;
1776 mutex_unlock(&pktgen_thread_lock);
1777 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1778 ret = count;
1779 sprintf(pg_result, "OK: rem_device_all");
1780 goto out;
1783 if (!strcmp(name, "max_before_softirq")) {
1784 len = num_arg(&user_buffer[i], 10, &value);
1785 mutex_lock(&pktgen_thread_lock);
1786 t->max_before_softirq = value;
1787 mutex_unlock(&pktgen_thread_lock);
1788 ret = count;
1789 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1790 goto out;
1793 ret = -EINVAL;
1794 out:
1795 return ret;
1798 static int pktgen_thread_open(struct inode *inode, struct file *file)
1800 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1803 static const struct file_operations pktgen_thread_fops = {
1804 .owner = THIS_MODULE,
1805 .open = pktgen_thread_open,
1806 .read = seq_read,
1807 .llseek = seq_lseek,
1808 .write = pktgen_thread_write,
1809 .release = single_release,
1812 /* Think find or remove for NN */
1813 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1815 struct pktgen_thread *t;
1816 struct pktgen_dev *pkt_dev = NULL;
1818 list_for_each_entry(t, &pktgen_threads, th_list) {
1819 pkt_dev = pktgen_find_dev(t, ifname);
1820 if (pkt_dev) {
1821 if (remove) {
1822 if_lock(t);
1823 pkt_dev->removal_mark = 1;
1824 t->control |= T_REMDEV;
1825 if_unlock(t);
1827 break;
1830 return pkt_dev;
1834 * mark a device for removal
1836 static void pktgen_mark_device(const char *ifname)
1838 struct pktgen_dev *pkt_dev = NULL;
1839 const int max_tries = 10, msec_per_try = 125;
1840 int i = 0;
1842 mutex_lock(&pktgen_thread_lock);
1843 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1845 while (1) {
1847 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1848 if (pkt_dev == NULL)
1849 break; /* success */
1851 mutex_unlock(&pktgen_thread_lock);
1852 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1853 "to disappear....\n", ifname);
1854 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1855 mutex_lock(&pktgen_thread_lock);
1857 if (++i >= max_tries) {
1858 printk("pktgen_mark_device: timed out after waiting "
1859 "%d msec for device %s to be removed\n",
1860 msec_per_try * i, ifname);
1861 break;
1866 mutex_unlock(&pktgen_thread_lock);
1869 static void pktgen_change_name(struct net_device *dev)
1871 struct pktgen_thread *t;
1873 list_for_each_entry(t, &pktgen_threads, th_list) {
1874 struct pktgen_dev *pkt_dev;
1876 list_for_each_entry(pkt_dev, &t->if_list, list) {
1877 if (pkt_dev->odev != dev)
1878 continue;
1880 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1882 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1883 pg_proc_dir);
1884 if (!pkt_dev->entry)
1885 printk(KERN_ERR "pktgen: can't move proc "
1886 " entry for '%s'\n", dev->name);
1887 break;
1892 static int pktgen_device_event(struct notifier_block *unused,
1893 unsigned long event, void *ptr)
1895 struct net_device *dev = ptr;
1897 /* It is OK that we do not hold the group lock right now,
1898 * as we run under the RTNL lock.
1901 switch (event) {
1902 case NETDEV_CHANGENAME:
1903 pktgen_change_name(dev);
1904 break;
1906 case NETDEV_UNREGISTER:
1907 pktgen_mark_device(dev->name);
1908 break;
1911 return NOTIFY_DONE;
1914 /* Associate pktgen_dev with a device. */
1916 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1918 struct net_device *odev;
1919 int err;
1921 /* Clean old setups */
1922 if (pkt_dev->odev) {
1923 dev_put(pkt_dev->odev);
1924 pkt_dev->odev = NULL;
1927 odev = dev_get_by_name(ifname);
1928 if (!odev) {
1929 printk("pktgen: no such netdevice: \"%s\"\n", ifname);
1930 return -ENODEV;
1933 if (odev->type != ARPHRD_ETHER) {
1934 printk("pktgen: not an ethernet device: \"%s\"\n", ifname);
1935 err = -EINVAL;
1936 } else if (!netif_running(odev)) {
1937 printk("pktgen: device is down: \"%s\"\n", ifname);
1938 err = -ENETDOWN;
1939 } else {
1940 pkt_dev->odev = odev;
1941 return 0;
1944 dev_put(odev);
1945 return err;
1948 /* Read pkt_dev from the interface and set up internal pktgen_dev
1949 * structure to have the right information to create/send packets
1951 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1953 if (!pkt_dev->odev) {
1954 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1955 sprintf(pkt_dev->result,
1956 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1957 return;
1960 /* Default to the interface's mac if not explicitly set. */
1962 if (is_zero_ether_addr(pkt_dev->src_mac))
1963 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1965 /* Set up Dest MAC */
1966 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1968 /* Set up pkt size */
1969 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1971 if (pkt_dev->flags & F_IPV6) {
1973 * Skip this automatic address setting until locks or functions
1974 * gets exported
1977 #ifdef NOTNOW
1978 int i, set = 0, err = 1;
1979 struct inet6_dev *idev;
1981 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1982 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1983 set = 1;
1984 break;
1987 if (!set) {
1990 * Use linklevel address if unconfigured.
1992 * use ipv6_get_lladdr if/when it's get exported
1995 rcu_read_lock();
1996 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1997 struct inet6_ifaddr *ifp;
1999 read_lock_bh(&idev->lock);
2000 for (ifp = idev->addr_list; ifp;
2001 ifp = ifp->if_next) {
2002 if (ifp->scope == IFA_LINK
2003 && !(ifp->
2004 flags & IFA_F_TENTATIVE)) {
2005 ipv6_addr_copy(&pkt_dev->
2006 cur_in6_saddr,
2007 &ifp->addr);
2008 err = 0;
2009 break;
2012 read_unlock_bh(&idev->lock);
2014 rcu_read_unlock();
2015 if (err)
2016 printk("pktgen: ERROR: IPv6 link address not availble.\n");
2018 #endif
2019 } else {
2020 pkt_dev->saddr_min = 0;
2021 pkt_dev->saddr_max = 0;
2022 if (strlen(pkt_dev->src_min) == 0) {
2024 struct in_device *in_dev;
2026 rcu_read_lock();
2027 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2028 if (in_dev) {
2029 if (in_dev->ifa_list) {
2030 pkt_dev->saddr_min =
2031 in_dev->ifa_list->ifa_address;
2032 pkt_dev->saddr_max = pkt_dev->saddr_min;
2035 rcu_read_unlock();
2036 } else {
2037 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2038 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2041 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2042 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2044 /* Initialize current values. */
2045 pkt_dev->cur_dst_mac_offset = 0;
2046 pkt_dev->cur_src_mac_offset = 0;
2047 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2048 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2049 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2050 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2051 pkt_dev->nflows = 0;
2054 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2056 __u64 start;
2057 __u64 now;
2059 start = now = getCurUs();
2060 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2061 while (now < spin_until_us) {
2062 /* TODO: optimize sleeping behavior */
2063 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2064 schedule_timeout_interruptible(1);
2065 else if (spin_until_us - now > 100) {
2066 do_softirq();
2067 if (!pkt_dev->running)
2068 return;
2069 if (need_resched())
2070 schedule();
2073 now = getCurUs();
2076 pkt_dev->idle_acc += now - start;
2079 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2081 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2082 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2083 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2086 /* Increment/randomize headers according to flags and current values
2087 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2089 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2091 __u32 imn;
2092 __u32 imx;
2093 int flow = 0;
2095 if (pkt_dev->cflows) {
2096 flow = random32() % pkt_dev->cflows;
2098 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2099 pkt_dev->flows[flow].count = 0;
2102 /* Deal with source MAC */
2103 if (pkt_dev->src_mac_count > 1) {
2104 __u32 mc;
2105 __u32 tmp;
2107 if (pkt_dev->flags & F_MACSRC_RND)
2108 mc = random32() % pkt_dev->src_mac_count;
2109 else {
2110 mc = pkt_dev->cur_src_mac_offset++;
2111 if (pkt_dev->cur_src_mac_offset >
2112 pkt_dev->src_mac_count)
2113 pkt_dev->cur_src_mac_offset = 0;
2116 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2117 pkt_dev->hh[11] = tmp;
2118 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2119 pkt_dev->hh[10] = tmp;
2120 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2121 pkt_dev->hh[9] = tmp;
2122 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2123 pkt_dev->hh[8] = tmp;
2124 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2125 pkt_dev->hh[7] = tmp;
2128 /* Deal with Destination MAC */
2129 if (pkt_dev->dst_mac_count > 1) {
2130 __u32 mc;
2131 __u32 tmp;
2133 if (pkt_dev->flags & F_MACDST_RND)
2134 mc = random32() % pkt_dev->dst_mac_count;
2136 else {
2137 mc = pkt_dev->cur_dst_mac_offset++;
2138 if (pkt_dev->cur_dst_mac_offset >
2139 pkt_dev->dst_mac_count) {
2140 pkt_dev->cur_dst_mac_offset = 0;
2144 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2145 pkt_dev->hh[5] = tmp;
2146 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2147 pkt_dev->hh[4] = tmp;
2148 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2149 pkt_dev->hh[3] = tmp;
2150 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2151 pkt_dev->hh[2] = tmp;
2152 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2153 pkt_dev->hh[1] = tmp;
2156 if (pkt_dev->flags & F_MPLS_RND) {
2157 unsigned i;
2158 for (i = 0; i < pkt_dev->nr_labels; i++)
2159 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2160 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2161 ((__force __be32)random32() &
2162 htonl(0x000fffff));
2165 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2166 pkt_dev->vlan_id = random32() & (4096-1);
2169 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2170 pkt_dev->svlan_id = random32() & (4096 - 1);
2173 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2174 if (pkt_dev->flags & F_UDPSRC_RND)
2175 pkt_dev->cur_udp_src = random32() %
2176 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2177 + pkt_dev->udp_src_min;
2179 else {
2180 pkt_dev->cur_udp_src++;
2181 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2182 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2186 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2187 if (pkt_dev->flags & F_UDPDST_RND) {
2188 pkt_dev->cur_udp_dst = random32() %
2189 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2190 + pkt_dev->udp_dst_min;
2191 } else {
2192 pkt_dev->cur_udp_dst++;
2193 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2194 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2198 if (!(pkt_dev->flags & F_IPV6)) {
2200 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2201 ntohl(pkt_dev->
2202 saddr_max))) {
2203 __u32 t;
2204 if (pkt_dev->flags & F_IPSRC_RND)
2205 t = random32() % (imx - imn) + imn;
2206 else {
2207 t = ntohl(pkt_dev->cur_saddr);
2208 t++;
2209 if (t > imx) {
2210 t = imn;
2213 pkt_dev->cur_saddr = htonl(t);
2216 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2217 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2218 } else {
2219 imn = ntohl(pkt_dev->daddr_min);
2220 imx = ntohl(pkt_dev->daddr_max);
2221 if (imn < imx) {
2222 __u32 t;
2223 __be32 s;
2224 if (pkt_dev->flags & F_IPDST_RND) {
2226 t = random32() % (imx - imn) + imn;
2227 s = htonl(t);
2229 while (LOOPBACK(s) || MULTICAST(s)
2230 || BADCLASS(s) || ZERONET(s)
2231 || LOCAL_MCAST(s)) {
2232 t = random32() % (imx - imn) + imn;
2233 s = htonl(t);
2235 pkt_dev->cur_daddr = s;
2236 } else {
2237 t = ntohl(pkt_dev->cur_daddr);
2238 t++;
2239 if (t > imx) {
2240 t = imn;
2242 pkt_dev->cur_daddr = htonl(t);
2245 if (pkt_dev->cflows) {
2246 pkt_dev->flows[flow].cur_daddr =
2247 pkt_dev->cur_daddr;
2248 pkt_dev->nflows++;
2251 } else { /* IPV6 * */
2253 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2254 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2255 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2256 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2257 else {
2258 int i;
2260 /* Only random destinations yet */
2262 for (i = 0; i < 4; i++) {
2263 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2264 (((__force __be32)random32() |
2265 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2266 pkt_dev->max_in6_daddr.s6_addr32[i]);
2271 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2272 __u32 t;
2273 if (pkt_dev->flags & F_TXSIZE_RND) {
2274 t = random32() %
2275 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2276 + pkt_dev->min_pkt_size;
2277 } else {
2278 t = pkt_dev->cur_pkt_size + 1;
2279 if (t > pkt_dev->max_pkt_size)
2280 t = pkt_dev->min_pkt_size;
2282 pkt_dev->cur_pkt_size = t;
2285 pkt_dev->flows[flow].count++;
2288 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2290 unsigned i;
2291 for (i = 0; i < pkt_dev->nr_labels; i++) {
2292 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2294 mpls--;
2295 *mpls |= MPLS_STACK_BOTTOM;
2298 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2299 unsigned int prio)
2301 return htons(id | (cfi << 12) | (prio << 13));
2304 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2305 struct pktgen_dev *pkt_dev)
2307 struct sk_buff *skb = NULL;
2308 __u8 *eth;
2309 struct udphdr *udph;
2310 int datalen, iplen;
2311 struct iphdr *iph;
2312 struct pktgen_hdr *pgh = NULL;
2313 __be16 protocol = htons(ETH_P_IP);
2314 __be32 *mpls;
2315 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2316 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2317 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2318 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2321 if (pkt_dev->nr_labels)
2322 protocol = htons(ETH_P_MPLS_UC);
2324 if (pkt_dev->vlan_id != 0xffff)
2325 protocol = htons(ETH_P_8021Q);
2327 /* Update any of the values, used when we're incrementing various
2328 * fields.
2330 mod_cur_headers(pkt_dev);
2332 datalen = (odev->hard_header_len + 16) & ~0xf;
2333 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2334 pkt_dev->pkt_overhead, GFP_ATOMIC);
2335 if (!skb) {
2336 sprintf(pkt_dev->result, "No memory");
2337 return NULL;
2340 skb_reserve(skb, datalen);
2342 /* Reserve for ethernet and IP header */
2343 eth = (__u8 *) skb_push(skb, 14);
2344 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2345 if (pkt_dev->nr_labels)
2346 mpls_push(mpls, pkt_dev);
2348 if (pkt_dev->vlan_id != 0xffff) {
2349 if (pkt_dev->svlan_id != 0xffff) {
2350 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2351 *svlan_tci = build_tci(pkt_dev->svlan_id,
2352 pkt_dev->svlan_cfi,
2353 pkt_dev->svlan_p);
2354 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2355 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2357 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2358 *vlan_tci = build_tci(pkt_dev->vlan_id,
2359 pkt_dev->vlan_cfi,
2360 pkt_dev->vlan_p);
2361 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2362 *vlan_encapsulated_proto = htons(ETH_P_IP);
2365 skb->network_header = skb->tail;
2366 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2367 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2369 iph = ip_hdr(skb);
2370 udph = udp_hdr(skb);
2372 memcpy(eth, pkt_dev->hh, 12);
2373 *(__be16 *) & eth[12] = protocol;
2375 /* Eth + IPh + UDPh + mpls */
2376 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2377 pkt_dev->pkt_overhead;
2378 if (datalen < sizeof(struct pktgen_hdr))
2379 datalen = sizeof(struct pktgen_hdr);
2381 udph->source = htons(pkt_dev->cur_udp_src);
2382 udph->dest = htons(pkt_dev->cur_udp_dst);
2383 udph->len = htons(datalen + 8); /* DATA + udphdr */
2384 udph->check = 0; /* No checksum */
2386 iph->ihl = 5;
2387 iph->version = 4;
2388 iph->ttl = 32;
2389 iph->tos = pkt_dev->tos;
2390 iph->protocol = IPPROTO_UDP; /* UDP */
2391 iph->saddr = pkt_dev->cur_saddr;
2392 iph->daddr = pkt_dev->cur_daddr;
2393 iph->frag_off = 0;
2394 iplen = 20 + 8 + datalen;
2395 iph->tot_len = htons(iplen);
2396 iph->check = 0;
2397 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2398 skb->protocol = protocol;
2399 skb->mac_header = (skb->network_header - ETH_HLEN -
2400 pkt_dev->pkt_overhead);
2401 skb->dev = odev;
2402 skb->pkt_type = PACKET_HOST;
2404 if (pkt_dev->nfrags <= 0)
2405 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2406 else {
2407 int frags = pkt_dev->nfrags;
2408 int i;
2410 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2412 if (frags > MAX_SKB_FRAGS)
2413 frags = MAX_SKB_FRAGS;
2414 if (datalen > frags * PAGE_SIZE) {
2415 skb_put(skb, datalen - frags * PAGE_SIZE);
2416 datalen = frags * PAGE_SIZE;
2419 i = 0;
2420 while (datalen > 0) {
2421 struct page *page = alloc_pages(GFP_KERNEL, 0);
2422 skb_shinfo(skb)->frags[i].page = page;
2423 skb_shinfo(skb)->frags[i].page_offset = 0;
2424 skb_shinfo(skb)->frags[i].size =
2425 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2426 datalen -= skb_shinfo(skb)->frags[i].size;
2427 skb->len += skb_shinfo(skb)->frags[i].size;
2428 skb->data_len += skb_shinfo(skb)->frags[i].size;
2429 i++;
2430 skb_shinfo(skb)->nr_frags = i;
2433 while (i < frags) {
2434 int rem;
2436 if (i == 0)
2437 break;
2439 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2440 if (rem == 0)
2441 break;
2443 skb_shinfo(skb)->frags[i - 1].size -= rem;
2445 skb_shinfo(skb)->frags[i] =
2446 skb_shinfo(skb)->frags[i - 1];
2447 get_page(skb_shinfo(skb)->frags[i].page);
2448 skb_shinfo(skb)->frags[i].page =
2449 skb_shinfo(skb)->frags[i - 1].page;
2450 skb_shinfo(skb)->frags[i].page_offset +=
2451 skb_shinfo(skb)->frags[i - 1].size;
2452 skb_shinfo(skb)->frags[i].size = rem;
2453 i++;
2454 skb_shinfo(skb)->nr_frags = i;
2458 /* Stamp the time, and sequence number, convert them to network byte order */
2460 if (pgh) {
2461 struct timeval timestamp;
2463 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2464 pgh->seq_num = htonl(pkt_dev->seq_num);
2466 do_gettimeofday(&timestamp);
2467 pgh->tv_sec = htonl(timestamp.tv_sec);
2468 pgh->tv_usec = htonl(timestamp.tv_usec);
2471 return skb;
2475 * scan_ip6, fmt_ip taken from dietlibc-0.21
2476 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2478 * Slightly modified for kernel.
2479 * Should be candidate for net/ipv4/utils.c
2480 * --ro
2483 static unsigned int scan_ip6(const char *s, char ip[16])
2485 unsigned int i;
2486 unsigned int len = 0;
2487 unsigned long u;
2488 char suffix[16];
2489 unsigned int prefixlen = 0;
2490 unsigned int suffixlen = 0;
2491 __be32 tmp;
2493 for (i = 0; i < 16; i++)
2494 ip[i] = 0;
2496 for (;;) {
2497 if (*s == ':') {
2498 len++;
2499 if (s[1] == ':') { /* Found "::", skip to part 2 */
2500 s += 2;
2501 len++;
2502 break;
2504 s++;
2507 char *tmp;
2508 u = simple_strtoul(s, &tmp, 16);
2509 i = tmp - s;
2512 if (!i)
2513 return 0;
2514 if (prefixlen == 12 && s[i] == '.') {
2516 /* the last 4 bytes may be written as IPv4 address */
2518 tmp = in_aton(s);
2519 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2520 return i + len;
2522 ip[prefixlen++] = (u >> 8);
2523 ip[prefixlen++] = (u & 255);
2524 s += i;
2525 len += i;
2526 if (prefixlen == 16)
2527 return len;
2530 /* part 2, after "::" */
2531 for (;;) {
2532 if (*s == ':') {
2533 if (suffixlen == 0)
2534 break;
2535 s++;
2536 len++;
2537 } else if (suffixlen != 0)
2538 break;
2540 char *tmp;
2541 u = simple_strtol(s, &tmp, 16);
2542 i = tmp - s;
2544 if (!i) {
2545 if (*s)
2546 len--;
2547 break;
2549 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2550 tmp = in_aton(s);
2551 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2552 sizeof(tmp));
2553 suffixlen += 4;
2554 len += strlen(s);
2555 break;
2557 suffix[suffixlen++] = (u >> 8);
2558 suffix[suffixlen++] = (u & 255);
2559 s += i;
2560 len += i;
2561 if (prefixlen + suffixlen == 16)
2562 break;
2564 for (i = 0; i < suffixlen; i++)
2565 ip[16 - suffixlen + i] = suffix[i];
2566 return len;
2569 static char tohex(char hexdigit)
2571 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2574 static int fmt_xlong(char *s, unsigned int i)
2576 char *bak = s;
2577 *s = tohex((i >> 12) & 0xf);
2578 if (s != bak || *s != '0')
2579 ++s;
2580 *s = tohex((i >> 8) & 0xf);
2581 if (s != bak || *s != '0')
2582 ++s;
2583 *s = tohex((i >> 4) & 0xf);
2584 if (s != bak || *s != '0')
2585 ++s;
2586 *s = tohex(i & 0xf);
2587 return s - bak + 1;
2590 static unsigned int fmt_ip6(char *s, const char ip[16])
2592 unsigned int len;
2593 unsigned int i;
2594 unsigned int temp;
2595 unsigned int compressing;
2596 int j;
2598 len = 0;
2599 compressing = 0;
2600 for (j = 0; j < 16; j += 2) {
2602 #ifdef V4MAPPEDPREFIX
2603 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2604 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2605 temp = strlen(s);
2606 return len + temp;
2608 #endif
2609 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2610 (unsigned long)(unsigned char)ip[j + 1];
2611 if (temp == 0) {
2612 if (!compressing) {
2613 compressing = 1;
2614 if (j == 0) {
2615 *s++ = ':';
2616 ++len;
2619 } else {
2620 if (compressing) {
2621 compressing = 0;
2622 *s++ = ':';
2623 ++len;
2625 i = fmt_xlong(s, temp);
2626 len += i;
2627 s += i;
2628 if (j < 14) {
2629 *s++ = ':';
2630 ++len;
2634 if (compressing) {
2635 *s++ = ':';
2636 ++len;
2638 *s = 0;
2639 return len;
2642 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2643 struct pktgen_dev *pkt_dev)
2645 struct sk_buff *skb = NULL;
2646 __u8 *eth;
2647 struct udphdr *udph;
2648 int datalen;
2649 struct ipv6hdr *iph;
2650 struct pktgen_hdr *pgh = NULL;
2651 __be16 protocol = htons(ETH_P_IPV6);
2652 __be32 *mpls;
2653 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2654 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2655 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2656 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2658 if (pkt_dev->nr_labels)
2659 protocol = htons(ETH_P_MPLS_UC);
2661 if (pkt_dev->vlan_id != 0xffff)
2662 protocol = htons(ETH_P_8021Q);
2664 /* Update any of the values, used when we're incrementing various
2665 * fields.
2667 mod_cur_headers(pkt_dev);
2669 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2670 pkt_dev->pkt_overhead, GFP_ATOMIC);
2671 if (!skb) {
2672 sprintf(pkt_dev->result, "No memory");
2673 return NULL;
2676 skb_reserve(skb, 16);
2678 /* Reserve for ethernet and IP header */
2679 eth = (__u8 *) skb_push(skb, 14);
2680 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2681 if (pkt_dev->nr_labels)
2682 mpls_push(mpls, pkt_dev);
2684 if (pkt_dev->vlan_id != 0xffff) {
2685 if (pkt_dev->svlan_id != 0xffff) {
2686 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2687 *svlan_tci = build_tci(pkt_dev->svlan_id,
2688 pkt_dev->svlan_cfi,
2689 pkt_dev->svlan_p);
2690 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2691 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2693 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2694 *vlan_tci = build_tci(pkt_dev->vlan_id,
2695 pkt_dev->vlan_cfi,
2696 pkt_dev->vlan_p);
2697 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2698 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2701 skb->network_header = skb->tail;
2702 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2703 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2705 iph = ipv6_hdr(skb);
2706 udph = udp_hdr(skb);
2708 memcpy(eth, pkt_dev->hh, 12);
2709 *(__be16 *) & eth[12] = protocol;
2711 /* Eth + IPh + UDPh + mpls */
2712 datalen = pkt_dev->cur_pkt_size - 14 -
2713 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2714 pkt_dev->pkt_overhead;
2716 if (datalen < sizeof(struct pktgen_hdr)) {
2717 datalen = sizeof(struct pktgen_hdr);
2718 if (net_ratelimit())
2719 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2720 datalen);
2723 udph->source = htons(pkt_dev->cur_udp_src);
2724 udph->dest = htons(pkt_dev->cur_udp_dst);
2725 udph->len = htons(datalen + sizeof(struct udphdr));
2726 udph->check = 0; /* No checksum */
2728 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2730 if (pkt_dev->traffic_class) {
2731 /* Version + traffic class + flow (0) */
2732 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2735 iph->hop_limit = 32;
2737 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2738 iph->nexthdr = IPPROTO_UDP;
2740 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2741 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2743 skb->mac_header = (skb->network_header - ETH_HLEN -
2744 pkt_dev->pkt_overhead);
2745 skb->protocol = protocol;
2746 skb->dev = odev;
2747 skb->pkt_type = PACKET_HOST;
2749 if (pkt_dev->nfrags <= 0)
2750 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2751 else {
2752 int frags = pkt_dev->nfrags;
2753 int i;
2755 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2757 if (frags > MAX_SKB_FRAGS)
2758 frags = MAX_SKB_FRAGS;
2759 if (datalen > frags * PAGE_SIZE) {
2760 skb_put(skb, datalen - frags * PAGE_SIZE);
2761 datalen = frags * PAGE_SIZE;
2764 i = 0;
2765 while (datalen > 0) {
2766 struct page *page = alloc_pages(GFP_KERNEL, 0);
2767 skb_shinfo(skb)->frags[i].page = page;
2768 skb_shinfo(skb)->frags[i].page_offset = 0;
2769 skb_shinfo(skb)->frags[i].size =
2770 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2771 datalen -= skb_shinfo(skb)->frags[i].size;
2772 skb->len += skb_shinfo(skb)->frags[i].size;
2773 skb->data_len += skb_shinfo(skb)->frags[i].size;
2774 i++;
2775 skb_shinfo(skb)->nr_frags = i;
2778 while (i < frags) {
2779 int rem;
2781 if (i == 0)
2782 break;
2784 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2785 if (rem == 0)
2786 break;
2788 skb_shinfo(skb)->frags[i - 1].size -= rem;
2790 skb_shinfo(skb)->frags[i] =
2791 skb_shinfo(skb)->frags[i - 1];
2792 get_page(skb_shinfo(skb)->frags[i].page);
2793 skb_shinfo(skb)->frags[i].page =
2794 skb_shinfo(skb)->frags[i - 1].page;
2795 skb_shinfo(skb)->frags[i].page_offset +=
2796 skb_shinfo(skb)->frags[i - 1].size;
2797 skb_shinfo(skb)->frags[i].size = rem;
2798 i++;
2799 skb_shinfo(skb)->nr_frags = i;
2803 /* Stamp the time, and sequence number, convert them to network byte order */
2804 /* should we update cloned packets too ? */
2805 if (pgh) {
2806 struct timeval timestamp;
2808 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2809 pgh->seq_num = htonl(pkt_dev->seq_num);
2811 do_gettimeofday(&timestamp);
2812 pgh->tv_sec = htonl(timestamp.tv_sec);
2813 pgh->tv_usec = htonl(timestamp.tv_usec);
2815 /* pkt_dev->seq_num++; FF: you really mean this? */
2817 return skb;
2820 static inline struct sk_buff *fill_packet(struct net_device *odev,
2821 struct pktgen_dev *pkt_dev)
2823 if (pkt_dev->flags & F_IPV6)
2824 return fill_packet_ipv6(odev, pkt_dev);
2825 else
2826 return fill_packet_ipv4(odev, pkt_dev);
2829 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2831 pkt_dev->seq_num = 1;
2832 pkt_dev->idle_acc = 0;
2833 pkt_dev->sofar = 0;
2834 pkt_dev->tx_bytes = 0;
2835 pkt_dev->errors = 0;
2838 /* Set up structure for sending pkts, clear counters */
2840 static void pktgen_run(struct pktgen_thread *t)
2842 struct pktgen_dev *pkt_dev;
2843 int started = 0;
2845 pr_debug("pktgen: entering pktgen_run. %p\n", t);
2847 if_lock(t);
2848 list_for_each_entry(pkt_dev, &t->if_list, list) {
2851 * setup odev and create initial packet.
2853 pktgen_setup_inject(pkt_dev);
2855 if (pkt_dev->odev) {
2856 pktgen_clear_counters(pkt_dev);
2857 pkt_dev->running = 1; /* Cranke yeself! */
2858 pkt_dev->skb = NULL;
2859 pkt_dev->started_at = getCurUs();
2860 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2861 pkt_dev->next_tx_ns = 0;
2862 set_pkt_overhead(pkt_dev);
2864 strcpy(pkt_dev->result, "Starting");
2865 started++;
2866 } else
2867 strcpy(pkt_dev->result, "Error starting");
2869 if_unlock(t);
2870 if (started)
2871 t->control &= ~(T_STOP);
2874 static void pktgen_stop_all_threads_ifs(void)
2876 struct pktgen_thread *t;
2878 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
2880 mutex_lock(&pktgen_thread_lock);
2882 list_for_each_entry(t, &pktgen_threads, th_list)
2883 t->control |= T_STOP;
2885 mutex_unlock(&pktgen_thread_lock);
2888 static int thread_is_running(struct pktgen_thread *t)
2890 struct pktgen_dev *pkt_dev;
2891 int res = 0;
2893 list_for_each_entry(pkt_dev, &t->if_list, list)
2894 if (pkt_dev->running) {
2895 res = 1;
2896 break;
2898 return res;
2901 static int pktgen_wait_thread_run(struct pktgen_thread *t)
2903 if_lock(t);
2905 while (thread_is_running(t)) {
2907 if_unlock(t);
2909 msleep_interruptible(100);
2911 if (signal_pending(current))
2912 goto signal;
2913 if_lock(t);
2915 if_unlock(t);
2916 return 1;
2917 signal:
2918 return 0;
2921 static int pktgen_wait_all_threads_run(void)
2923 struct pktgen_thread *t;
2924 int sig = 1;
2926 mutex_lock(&pktgen_thread_lock);
2928 list_for_each_entry(t, &pktgen_threads, th_list) {
2929 sig = pktgen_wait_thread_run(t);
2930 if (sig == 0)
2931 break;
2934 if (sig == 0)
2935 list_for_each_entry(t, &pktgen_threads, th_list)
2936 t->control |= (T_STOP);
2938 mutex_unlock(&pktgen_thread_lock);
2939 return sig;
2942 static void pktgen_run_all_threads(void)
2944 struct pktgen_thread *t;
2946 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
2948 mutex_lock(&pktgen_thread_lock);
2950 list_for_each_entry(t, &pktgen_threads, th_list)
2951 t->control |= (T_RUN);
2953 mutex_unlock(&pktgen_thread_lock);
2955 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2957 pktgen_wait_all_threads_run();
2960 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2962 __u64 total_us, bps, mbps, pps, idle;
2963 char *p = pkt_dev->result;
2965 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
2967 idle = pkt_dev->idle_acc;
2969 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2970 (unsigned long long)total_us,
2971 (unsigned long long)(total_us - idle),
2972 (unsigned long long)idle,
2973 (unsigned long long)pkt_dev->sofar,
2974 pkt_dev->cur_pkt_size, nr_frags);
2976 pps = pkt_dev->sofar * USEC_PER_SEC;
2978 while ((total_us >> 32) != 0) {
2979 pps >>= 1;
2980 total_us >>= 1;
2983 do_div(pps, total_us);
2985 bps = pps * 8 * pkt_dev->cur_pkt_size;
2987 mbps = bps;
2988 do_div(mbps, 1000000);
2989 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2990 (unsigned long long)pps,
2991 (unsigned long long)mbps,
2992 (unsigned long long)bps,
2993 (unsigned long long)pkt_dev->errors);
2996 /* Set stopped-at timer, remove from running list, do counters & statistics */
2998 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3000 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3002 if (!pkt_dev->running) {
3003 printk("pktgen: interface: %s is already stopped\n",
3004 pkt_dev->odev->name);
3005 return -EINVAL;
3008 pkt_dev->stopped_at = getCurUs();
3009 pkt_dev->running = 0;
3011 show_results(pkt_dev, nr_frags);
3013 return 0;
3016 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3018 struct pktgen_dev *pkt_dev, *best = NULL;
3020 if_lock(t);
3022 list_for_each_entry(pkt_dev, &t->if_list, list) {
3023 if (!pkt_dev->running)
3024 continue;
3025 if (best == NULL)
3026 best = pkt_dev;
3027 else if (pkt_dev->next_tx_us < best->next_tx_us)
3028 best = pkt_dev;
3030 if_unlock(t);
3031 return best;
3034 static void pktgen_stop(struct pktgen_thread *t)
3036 struct pktgen_dev *pkt_dev;
3038 pr_debug("pktgen: entering pktgen_stop\n");
3040 if_lock(t);
3042 list_for_each_entry(pkt_dev, &t->if_list, list) {
3043 pktgen_stop_device(pkt_dev);
3044 if (pkt_dev->skb)
3045 kfree_skb(pkt_dev->skb);
3047 pkt_dev->skb = NULL;
3050 if_unlock(t);
3054 * one of our devices needs to be removed - find it
3055 * and remove it
3057 static void pktgen_rem_one_if(struct pktgen_thread *t)
3059 struct list_head *q, *n;
3060 struct pktgen_dev *cur;
3062 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3064 if_lock(t);
3066 list_for_each_safe(q, n, &t->if_list) {
3067 cur = list_entry(q, struct pktgen_dev, list);
3069 if (!cur->removal_mark)
3070 continue;
3072 if (cur->skb)
3073 kfree_skb(cur->skb);
3074 cur->skb = NULL;
3076 pktgen_remove_device(t, cur);
3078 break;
3081 if_unlock(t);
3084 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3086 struct list_head *q, *n;
3087 struct pktgen_dev *cur;
3089 /* Remove all devices, free mem */
3091 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3092 if_lock(t);
3094 list_for_each_safe(q, n, &t->if_list) {
3095 cur = list_entry(q, struct pktgen_dev, list);
3097 if (cur->skb)
3098 kfree_skb(cur->skb);
3099 cur->skb = NULL;
3101 pktgen_remove_device(t, cur);
3104 if_unlock(t);
3107 static void pktgen_rem_thread(struct pktgen_thread *t)
3109 /* Remove from the thread list */
3111 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3113 mutex_lock(&pktgen_thread_lock);
3115 list_del(&t->th_list);
3117 mutex_unlock(&pktgen_thread_lock);
3120 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3122 struct net_device *odev = NULL;
3123 __u64 idle_start = 0;
3124 int ret;
3126 odev = pkt_dev->odev;
3128 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3129 u64 now;
3131 now = getCurUs();
3132 if (now < pkt_dev->next_tx_us)
3133 spin(pkt_dev, pkt_dev->next_tx_us);
3135 /* This is max DELAY, this has special meaning of
3136 * "never transmit"
3138 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3139 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3140 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3141 goto out;
3145 if ((netif_queue_stopped(odev) ||
3146 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) ||
3147 need_resched()) {
3148 idle_start = getCurUs();
3150 if (!netif_running(odev)) {
3151 pktgen_stop_device(pkt_dev);
3152 if (pkt_dev->skb)
3153 kfree_skb(pkt_dev->skb);
3154 pkt_dev->skb = NULL;
3155 goto out;
3157 if (need_resched())
3158 schedule();
3160 pkt_dev->idle_acc += getCurUs() - idle_start;
3162 if (netif_queue_stopped(odev) ||
3163 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3164 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3165 pkt_dev->next_tx_ns = 0;
3166 goto out; /* Try the next interface */
3170 if (pkt_dev->last_ok || !pkt_dev->skb) {
3171 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3172 || (!pkt_dev->skb)) {
3173 /* build a new pkt */
3174 if (pkt_dev->skb)
3175 kfree_skb(pkt_dev->skb);
3177 pkt_dev->skb = fill_packet(odev, pkt_dev);
3178 if (pkt_dev->skb == NULL) {
3179 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3180 schedule();
3181 pkt_dev->clone_count--; /* back out increment, OOM */
3182 goto out;
3184 pkt_dev->allocated_skbs++;
3185 pkt_dev->clone_count = 0; /* reset counter */
3189 netif_tx_lock_bh(odev);
3190 if (!netif_queue_stopped(odev) &&
3191 !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3193 atomic_inc(&(pkt_dev->skb->users));
3194 retry_now:
3195 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3196 if (likely(ret == NETDEV_TX_OK)) {
3197 pkt_dev->last_ok = 1;
3198 pkt_dev->sofar++;
3199 pkt_dev->seq_num++;
3200 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3202 } else if (ret == NETDEV_TX_LOCKED
3203 && (odev->features & NETIF_F_LLTX)) {
3204 cpu_relax();
3205 goto retry_now;
3206 } else { /* Retry it next time */
3208 atomic_dec(&(pkt_dev->skb->users));
3210 if (debug && net_ratelimit())
3211 printk(KERN_INFO "pktgen: Hard xmit error\n");
3213 pkt_dev->errors++;
3214 pkt_dev->last_ok = 0;
3217 pkt_dev->next_tx_us = getCurUs();
3218 pkt_dev->next_tx_ns = 0;
3220 pkt_dev->next_tx_us += pkt_dev->delay_us;
3221 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3223 if (pkt_dev->next_tx_ns > 1000) {
3224 pkt_dev->next_tx_us++;
3225 pkt_dev->next_tx_ns -= 1000;
3229 else { /* Retry it next time */
3230 pkt_dev->last_ok = 0;
3231 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3232 pkt_dev->next_tx_ns = 0;
3235 netif_tx_unlock_bh(odev);
3237 /* If pkt_dev->count is zero, then run forever */
3238 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3239 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3240 idle_start = getCurUs();
3241 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3242 if (signal_pending(current)) {
3243 break;
3245 schedule();
3247 pkt_dev->idle_acc += getCurUs() - idle_start;
3250 /* Done with this */
3251 pktgen_stop_device(pkt_dev);
3252 if (pkt_dev->skb)
3253 kfree_skb(pkt_dev->skb);
3254 pkt_dev->skb = NULL;
3256 out:;
3260 * Main loop of the thread goes here
3263 static int pktgen_thread_worker(void *arg)
3265 DEFINE_WAIT(wait);
3266 struct pktgen_thread *t = arg;
3267 struct pktgen_dev *pkt_dev = NULL;
3268 int cpu = t->cpu;
3269 u32 max_before_softirq;
3270 u32 tx_since_softirq = 0;
3272 BUG_ON(smp_processor_id() != cpu);
3274 init_waitqueue_head(&t->queue);
3276 t->pid = current->pid;
3278 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
3280 max_before_softirq = t->max_before_softirq;
3282 set_current_state(TASK_INTERRUPTIBLE);
3284 while (!kthread_should_stop()) {
3285 pkt_dev = next_to_run(t);
3287 if (!pkt_dev &&
3288 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3289 == 0) {
3290 prepare_to_wait(&(t->queue), &wait,
3291 TASK_INTERRUPTIBLE);
3292 schedule_timeout(HZ / 10);
3293 finish_wait(&(t->queue), &wait);
3296 __set_current_state(TASK_RUNNING);
3298 if (pkt_dev) {
3300 pktgen_xmit(pkt_dev);
3303 * We like to stay RUNNING but must also give
3304 * others fair share.
3307 tx_since_softirq += pkt_dev->last_ok;
3309 if (tx_since_softirq > max_before_softirq) {
3310 if (local_softirq_pending())
3311 do_softirq();
3312 tx_since_softirq = 0;
3316 if (t->control & T_STOP) {
3317 pktgen_stop(t);
3318 t->control &= ~(T_STOP);
3321 if (t->control & T_RUN) {
3322 pktgen_run(t);
3323 t->control &= ~(T_RUN);
3326 if (t->control & T_REMDEVALL) {
3327 pktgen_rem_all_ifs(t);
3328 t->control &= ~(T_REMDEVALL);
3331 if (t->control & T_REMDEV) {
3332 pktgen_rem_one_if(t);
3333 t->control &= ~(T_REMDEV);
3336 try_to_freeze();
3338 set_current_state(TASK_INTERRUPTIBLE);
3341 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3342 pktgen_stop(t);
3344 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3345 pktgen_rem_all_ifs(t);
3347 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3348 pktgen_rem_thread(t);
3350 return 0;
3353 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3354 const char *ifname)
3356 struct pktgen_dev *p, *pkt_dev = NULL;
3357 if_lock(t);
3359 list_for_each_entry(p, &t->if_list, list)
3360 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3361 pkt_dev = p;
3362 break;
3365 if_unlock(t);
3366 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3367 return pkt_dev;
3371 * Adds a dev at front of if_list.
3374 static int add_dev_to_thread(struct pktgen_thread *t,
3375 struct pktgen_dev *pkt_dev)
3377 int rv = 0;
3379 if_lock(t);
3381 if (pkt_dev->pg_thread) {
3382 printk("pktgen: ERROR: already assigned to a thread.\n");
3383 rv = -EBUSY;
3384 goto out;
3387 list_add(&pkt_dev->list, &t->if_list);
3388 pkt_dev->pg_thread = t;
3389 pkt_dev->running = 0;
3391 out:
3392 if_unlock(t);
3393 return rv;
3396 /* Called under thread lock */
3398 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3400 struct pktgen_dev *pkt_dev;
3401 int err;
3403 /* We don't allow a device to be on several threads */
3405 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3406 if (pkt_dev) {
3407 printk("pktgen: ERROR: interface already used.\n");
3408 return -EBUSY;
3411 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3412 if (!pkt_dev)
3413 return -ENOMEM;
3415 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3416 if (pkt_dev->flows == NULL) {
3417 kfree(pkt_dev);
3418 return -ENOMEM;
3420 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3422 pkt_dev->removal_mark = 0;
3423 pkt_dev->min_pkt_size = ETH_ZLEN;
3424 pkt_dev->max_pkt_size = ETH_ZLEN;
3425 pkt_dev->nfrags = 0;
3426 pkt_dev->clone_skb = pg_clone_skb_d;
3427 pkt_dev->delay_us = pg_delay_d / 1000;
3428 pkt_dev->delay_ns = pg_delay_d % 1000;
3429 pkt_dev->count = pg_count_d;
3430 pkt_dev->sofar = 0;
3431 pkt_dev->udp_src_min = 9; /* sink port */
3432 pkt_dev->udp_src_max = 9;
3433 pkt_dev->udp_dst_min = 9;
3434 pkt_dev->udp_dst_max = 9;
3436 pkt_dev->vlan_p = 0;
3437 pkt_dev->vlan_cfi = 0;
3438 pkt_dev->vlan_id = 0xffff;
3439 pkt_dev->svlan_p = 0;
3440 pkt_dev->svlan_cfi = 0;
3441 pkt_dev->svlan_id = 0xffff;
3443 err = pktgen_setup_dev(pkt_dev, ifname);
3444 if (err)
3445 goto out1;
3447 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3448 if (!pkt_dev->entry) {
3449 printk("pktgen: cannot create %s/%s procfs entry.\n",
3450 PG_PROC_DIR, ifname);
3451 err = -EINVAL;
3452 goto out2;
3454 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3455 pkt_dev->entry->data = pkt_dev;
3457 return add_dev_to_thread(t, pkt_dev);
3458 out2:
3459 dev_put(pkt_dev->odev);
3460 out1:
3461 if (pkt_dev->flows)
3462 vfree(pkt_dev->flows);
3463 kfree(pkt_dev);
3464 return err;
3467 static int __init pktgen_create_thread(int cpu)
3469 struct pktgen_thread *t;
3470 struct proc_dir_entry *pe;
3471 struct task_struct *p;
3473 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3474 if (!t) {
3475 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3476 return -ENOMEM;
3479 spin_lock_init(&t->if_lock);
3480 t->cpu = cpu;
3482 INIT_LIST_HEAD(&t->if_list);
3484 list_add_tail(&t->th_list, &pktgen_threads);
3486 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3487 if (IS_ERR(p)) {
3488 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3489 list_del(&t->th_list);
3490 kfree(t);
3491 return PTR_ERR(p);
3493 kthread_bind(p, cpu);
3494 t->tsk = p;
3496 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3497 if (!pe) {
3498 printk("pktgen: cannot create %s/%s procfs entry.\n",
3499 PG_PROC_DIR, t->tsk->comm);
3500 kthread_stop(p);
3501 list_del(&t->th_list);
3502 kfree(t);
3503 return -EINVAL;
3506 pe->proc_fops = &pktgen_thread_fops;
3507 pe->data = t;
3509 wake_up_process(p);
3511 return 0;
3515 * Removes a device from the thread if_list.
3517 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3518 struct pktgen_dev *pkt_dev)
3520 struct list_head *q, *n;
3521 struct pktgen_dev *p;
3523 list_for_each_safe(q, n, &t->if_list) {
3524 p = list_entry(q, struct pktgen_dev, list);
3525 if (p == pkt_dev)
3526 list_del(&p->list);
3530 static int pktgen_remove_device(struct pktgen_thread *t,
3531 struct pktgen_dev *pkt_dev)
3534 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3536 if (pkt_dev->running) {
3537 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3538 pktgen_stop_device(pkt_dev);
3541 /* Dis-associate from the interface */
3543 if (pkt_dev->odev) {
3544 dev_put(pkt_dev->odev);
3545 pkt_dev->odev = NULL;
3548 /* And update the thread if_list */
3550 _rem_dev_from_if_list(t, pkt_dev);
3552 if (pkt_dev->entry)
3553 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3555 if (pkt_dev->flows)
3556 vfree(pkt_dev->flows);
3557 kfree(pkt_dev);
3558 return 0;
3561 static int __init pg_init(void)
3563 int cpu;
3564 struct proc_dir_entry *pe;
3566 printk(version);
3568 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3569 if (!pg_proc_dir)
3570 return -ENODEV;
3571 pg_proc_dir->owner = THIS_MODULE;
3573 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3574 if (pe == NULL) {
3575 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3576 PGCTRL);
3577 proc_net_remove(PG_PROC_DIR);
3578 return -EINVAL;
3581 pe->proc_fops = &pktgen_fops;
3582 pe->data = NULL;
3584 /* Register us to receive netdevice events */
3585 register_netdevice_notifier(&pktgen_notifier_block);
3587 for_each_online_cpu(cpu) {
3588 int err;
3590 err = pktgen_create_thread(cpu);
3591 if (err)
3592 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3593 cpu, err);
3596 if (list_empty(&pktgen_threads)) {
3597 printk("pktgen: ERROR: Initialization failed for all threads\n");
3598 unregister_netdevice_notifier(&pktgen_notifier_block);
3599 remove_proc_entry(PGCTRL, pg_proc_dir);
3600 proc_net_remove(PG_PROC_DIR);
3601 return -ENODEV;
3604 return 0;
3607 static void __exit pg_cleanup(void)
3609 struct pktgen_thread *t;
3610 struct list_head *q, *n;
3611 wait_queue_head_t queue;
3612 init_waitqueue_head(&queue);
3614 /* Stop all interfaces & threads */
3616 list_for_each_safe(q, n, &pktgen_threads) {
3617 t = list_entry(q, struct pktgen_thread, th_list);
3618 kthread_stop(t->tsk);
3619 kfree(t);
3622 /* Un-register us from receiving netdevice events */
3623 unregister_netdevice_notifier(&pktgen_notifier_block);
3625 /* Clean up proc file system */
3626 remove_proc_entry(PGCTRL, pg_proc_dir);
3627 proc_net_remove(PG_PROC_DIR);
3630 module_init(pg_init);
3631 module_exit(pg_cleanup);
3633 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3634 MODULE_DESCRIPTION("Packet Generator tool");
3635 MODULE_LICENSE("GPL");
3636 module_param(pg_count_d, int, 0);
3637 module_param(pg_delay_d, int, 0);
3638 module_param(pg_clone_skb_d, int, 0);
3639 module_param(debug, int, 0);