allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / core / pktgen.c
blob33190c3c7410fe183bfff5b5aea2a5127382fb4e
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>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/freezer.h>
135 #include <linux/delay.h>
136 #include <linux/timer.h>
137 #include <linux/list.h>
138 #include <linux/init.h>
139 #include <linux/skbuff.h>
140 #include <linux/netdevice.h>
141 #include <linux/inet.h>
142 #include <linux/inetdevice.h>
143 #include <linux/rtnetlink.h>
144 #include <linux/if_arp.h>
145 #include <linux/if_vlan.h>
146 #include <linux/in.h>
147 #include <linux/ip.h>
148 #include <linux/ipv6.h>
149 #include <linux/udp.h>
150 #include <linux/proc_fs.h>
151 #include <linux/seq_file.h>
152 #include <linux/wait.h>
153 #include <linux/etherdevice.h>
154 #include <linux/kthread.h>
155 #include <net/checksum.h>
156 #include <net/ipv6.h>
157 #include <net/addrconf.h>
158 #include <asm/byteorder.h>
159 #include <linux/rcupdate.h>
160 #include <asm/bitops.h>
161 #include <asm/io.h>
162 #include <asm/dma.h>
163 #include <asm/uaccess.h>
164 #include <asm/div64.h> /* do_div */
165 #include <asm/timex.h>
167 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
169 /* The buckets are exponential in 'width' */
170 #define LAT_BUCKETS_MAX 32
171 #define IP_NAME_SZ 32
172 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
173 #define MPLS_STACK_BOTTOM htonl(0x00000100)
175 /* Device flag bits */
176 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
177 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
178 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
179 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
180 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
181 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
182 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
183 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
184 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
185 #define F_VID_RND (1<<9) /* Random VLAN ID */
186 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
188 /* Thread control flag bits */
189 #define T_TERMINATE (1<<0)
190 #define T_STOP (1<<1) /* Stop run */
191 #define T_RUN (1<<2) /* Start run */
192 #define T_REMDEVALL (1<<3) /* Remove all devs */
193 #define T_REMDEV (1<<4) /* Remove one dev */
195 /* If lock -- can be removed after some work */
196 #define if_lock(t) spin_lock(&(t->if_lock));
197 #define if_unlock(t) spin_unlock(&(t->if_lock));
199 /* Used to help with determining the pkts on receive */
200 #define PKTGEN_MAGIC 0xbe9be955
201 #define PG_PROC_DIR "pktgen"
202 #define PGCTRL "pgctrl"
203 static struct proc_dir_entry *pg_proc_dir = NULL;
205 #define MAX_CFLOWS 65536
207 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
208 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
210 struct flow_state {
211 __be32 cur_daddr;
212 int count;
215 struct pktgen_dev {
217 * Try to keep frequent/infrequent used vars. separated.
219 struct proc_dir_entry *entry; /* proc file */
220 struct pktgen_thread *pg_thread;/* the owner */
221 struct list_head list; /* Used for chaining in the thread's run-queue */
223 int running; /* if this changes to false, the test will stop */
225 /* If min != max, then we will either do a linear iteration, or
226 * we will do a random selection from within the range.
228 __u32 flags;
229 int removal_mark; /* non-zero => the device is marked for
230 * removal by worker thread */
232 int min_pkt_size; /* = ETH_ZLEN; */
233 int max_pkt_size; /* = ETH_ZLEN; */
234 int nfrags;
235 __u32 delay_us; /* Default delay */
236 __u32 delay_ns;
237 __u64 count; /* Default No packets to send */
238 __u64 sofar; /* How many pkts we've sent so far */
239 __u64 tx_bytes; /* How many bytes we've transmitted */
240 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
242 /* runtime counters relating to clone_skb */
243 __u64 next_tx_us; /* timestamp of when to tx next */
244 __u32 next_tx_ns;
246 __u64 allocated_skbs;
247 __u32 clone_count;
248 int last_ok; /* Was last skb sent?
249 * Or a failed transmit of some sort? This will keep
250 * sequence numbers in order, for example.
252 __u64 started_at; /* micro-seconds */
253 __u64 stopped_at; /* micro-seconds */
254 __u64 idle_acc; /* micro-seconds */
255 __u32 seq_num;
257 int clone_skb; /* Use multiple SKBs during packet gen. If this number
258 * is greater than 1, then that many copies of the same
259 * packet will be sent before a new packet is allocated.
260 * For instance, if you want to send 1024 identical packets
261 * before creating a new packet, set clone_skb to 1024.
264 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
265 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
266 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
267 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
269 struct in6_addr in6_saddr;
270 struct in6_addr in6_daddr;
271 struct in6_addr cur_in6_daddr;
272 struct in6_addr cur_in6_saddr;
273 /* For ranges */
274 struct in6_addr min_in6_daddr;
275 struct in6_addr max_in6_daddr;
276 struct in6_addr min_in6_saddr;
277 struct in6_addr max_in6_saddr;
279 /* If we're doing ranges, random or incremental, then this
280 * defines the min/max for those ranges.
282 __be32 saddr_min; /* inclusive, source IP address */
283 __be32 saddr_max; /* exclusive, source IP address */
284 __be32 daddr_min; /* inclusive, dest IP address */
285 __be32 daddr_max; /* exclusive, dest IP address */
287 __u16 udp_src_min; /* inclusive, source UDP port */
288 __u16 udp_src_max; /* exclusive, source UDP port */
289 __u16 udp_dst_min; /* inclusive, dest UDP port */
290 __u16 udp_dst_max; /* exclusive, dest UDP port */
292 /* DSCP + ECN */
293 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
294 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
296 /* MPLS */
297 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
298 __be32 labels[MAX_MPLS_LABELS];
300 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
301 __u8 vlan_p;
302 __u8 vlan_cfi;
303 __u16 vlan_id; /* 0xffff means no vlan tag */
305 __u8 svlan_p;
306 __u8 svlan_cfi;
307 __u16 svlan_id; /* 0xffff means no svlan tag */
309 __u32 src_mac_count; /* How many MACs to iterate through */
310 __u32 dst_mac_count; /* How many MACs to iterate through */
312 unsigned char dst_mac[ETH_ALEN];
313 unsigned char src_mac[ETH_ALEN];
315 __u32 cur_dst_mac_offset;
316 __u32 cur_src_mac_offset;
317 __be32 cur_saddr;
318 __be32 cur_daddr;
319 __u16 cur_udp_dst;
320 __u16 cur_udp_src;
321 __u32 cur_pkt_size;
323 __u8 hh[14];
324 /* = {
325 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
327 We fill in SRC address later
328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
329 0x08, 0x00
332 __u16 pad; /* pad out the hh struct to an even 16 bytes */
334 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
335 * are transmitting the same one multiple times
337 struct net_device *odev; /* The out-going device. Note that the device should
338 * have it's pg_info pointer pointing back to this
339 * device. This will be set when the user specifies
340 * the out-going device name (not when the inject is
341 * started as it used to do.)
343 struct flow_state *flows;
344 unsigned cflows; /* Concurrent flows (config) */
345 unsigned lflow; /* Flow length (config) */
346 unsigned nflows; /* accumulated flows (stats) */
348 char result[512];
351 struct pktgen_hdr {
352 __be32 pgh_magic;
353 __be32 seq_num;
354 __be32 tv_sec;
355 __be32 tv_usec;
358 struct pktgen_thread {
359 spinlock_t if_lock;
360 struct list_head if_list; /* All device here */
361 struct list_head th_list;
362 struct task_struct *tsk;
363 char result[512];
364 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
366 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
368 u32 control;
369 int pid;
370 int cpu;
372 wait_queue_head_t queue;
375 #define REMOVE 1
376 #define FIND 0
378 /* This code works around the fact that do_div cannot handle two 64-bit
379 numbers, and regular 64-bit division doesn't work on x86 kernels.
380 --Ben
383 #define PG_DIV 0
385 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
386 * Function copied/adapted/optimized from:
388 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
390 * Copyright 1994, University of Cambridge Computer Laboratory
391 * All Rights Reserved.
394 static inline s64 divremdi3(s64 x, s64 y, int type)
396 u64 a = (x < 0) ? -x : x;
397 u64 b = (y < 0) ? -y : y;
398 u64 res = 0, d = 1;
400 if (b > 0) {
401 while (b < a) {
402 b <<= 1;
403 d <<= 1;
407 do {
408 if (a >= b) {
409 a -= b;
410 res += d;
412 b >>= 1;
413 d >>= 1;
415 while (d);
417 if (PG_DIV == type) {
418 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
419 } else {
420 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
424 /* End of hacks to deal with 64-bit math on x86 */
426 /** Convert to milliseconds */
427 static inline __u64 tv_to_ms(const struct timeval *tv)
429 __u64 ms = tv->tv_usec / 1000;
430 ms += (__u64) tv->tv_sec * (__u64) 1000;
431 return ms;
434 /** Convert to micro-seconds */
435 static inline __u64 tv_to_us(const struct timeval *tv)
437 __u64 us = tv->tv_usec;
438 us += (__u64) tv->tv_sec * (__u64) 1000000;
439 return us;
442 static inline __u64 pg_div(__u64 n, __u32 base)
444 __u64 tmp = n;
445 do_div(tmp, base);
446 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
447 n, base, tmp); */
448 return tmp;
451 static inline __u64 pg_div64(__u64 n, __u64 base)
453 __u64 tmp = n;
455 * How do we know if the architecture we are running on
456 * supports division with 64 bit base?
459 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
461 do_div(tmp, base);
462 #else
463 tmp = divremdi3(n, base, PG_DIV);
464 #endif
465 return tmp;
468 static inline __u64 getCurMs(void)
470 struct timeval tv;
471 do_gettimeofday(&tv);
472 return tv_to_ms(&tv);
475 static inline __u64 getCurUs(void)
477 struct timeval tv;
478 do_gettimeofday(&tv);
479 return tv_to_us(&tv);
482 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
484 return tv_to_us(a) - tv_to_us(b);
487 /* old include end */
489 static char version[] __initdata = VERSION;
491 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
492 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
493 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
494 const char *ifname);
495 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
496 static void pktgen_run_all_threads(void);
497 static void pktgen_stop_all_threads_ifs(void);
498 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
499 static void pktgen_stop(struct pktgen_thread *t);
500 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
502 static unsigned int scan_ip6(const char *s, char ip[16]);
503 static unsigned int fmt_ip6(char *s, const char ip[16]);
505 /* Module parameters, defaults. */
506 static int pg_count_d = 1000; /* 1000 pkts by default */
507 static int pg_delay_d;
508 static int pg_clone_skb_d;
509 static int debug;
511 static DEFINE_MUTEX(pktgen_thread_lock);
512 static LIST_HEAD(pktgen_threads);
514 static struct notifier_block pktgen_notifier_block = {
515 .notifier_call = pktgen_device_event,
519 * /proc handling functions
523 static int pgctrl_show(struct seq_file *seq, void *v)
525 seq_puts(seq, VERSION);
526 return 0;
529 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
530 size_t count, loff_t * ppos)
532 int err = 0;
533 char data[128];
535 if (!capable(CAP_NET_ADMIN)) {
536 err = -EPERM;
537 goto out;
540 if (count > sizeof(data))
541 count = sizeof(data);
543 if (copy_from_user(data, buf, count)) {
544 err = -EFAULT;
545 goto out;
547 data[count - 1] = 0; /* Make string */
549 if (!strcmp(data, "stop"))
550 pktgen_stop_all_threads_ifs();
552 else if (!strcmp(data, "start"))
553 pktgen_run_all_threads();
555 else
556 printk("pktgen: Unknown command: %s\n", data);
558 err = count;
560 out:
561 return err;
564 static int pgctrl_open(struct inode *inode, struct file *file)
566 return single_open(file, pgctrl_show, PDE(inode)->data);
569 static const struct file_operations pktgen_fops = {
570 .owner = THIS_MODULE,
571 .open = pgctrl_open,
572 .read = seq_read,
573 .llseek = seq_lseek,
574 .write = pgctrl_write,
575 .release = single_release,
578 static int pktgen_if_show(struct seq_file *seq, void *v)
580 int i;
581 struct pktgen_dev *pkt_dev = seq->private;
582 __u64 sa;
583 __u64 stopped;
584 __u64 now = getCurUs();
586 seq_printf(seq,
587 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
588 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
589 pkt_dev->max_pkt_size);
591 seq_printf(seq,
592 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
593 pkt_dev->nfrags,
594 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
595 pkt_dev->clone_skb, pkt_dev->odev->name);
597 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
598 pkt_dev->lflow);
600 if (pkt_dev->flags & F_IPV6) {
601 char b1[128], b2[128], b3[128];
602 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
603 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
604 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
605 seq_printf(seq,
606 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
607 b2, b3);
609 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
610 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
611 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
612 seq_printf(seq,
613 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
614 b2, b3);
616 } else
617 seq_printf(seq,
618 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
619 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
620 pkt_dev->src_max);
622 seq_puts(seq, " src_mac: ");
624 if (is_zero_ether_addr(pkt_dev->src_mac))
625 for (i = 0; i < 6; i++)
626 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
627 i == 5 ? " " : ":");
628 else
629 for (i = 0; i < 6; i++)
630 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
631 i == 5 ? " " : ":");
633 seq_printf(seq, "dst_mac: ");
634 for (i = 0; i < 6; i++)
635 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
636 i == 5 ? "\n" : ":");
638 seq_printf(seq,
639 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
640 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
641 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
643 seq_printf(seq,
644 " src_mac_count: %d dst_mac_count: %d\n",
645 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
647 if (pkt_dev->nr_labels) {
648 unsigned i;
649 seq_printf(seq, " mpls: ");
650 for (i = 0; i < pkt_dev->nr_labels; i++)
651 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
652 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
655 if (pkt_dev->vlan_id != 0xffff) {
656 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
657 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
660 if (pkt_dev->svlan_id != 0xffff) {
661 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
662 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
665 if (pkt_dev->tos) {
666 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
669 if (pkt_dev->traffic_class) {
670 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
673 seq_printf(seq, " Flags: ");
675 if (pkt_dev->flags & F_IPV6)
676 seq_printf(seq, "IPV6 ");
678 if (pkt_dev->flags & F_IPSRC_RND)
679 seq_printf(seq, "IPSRC_RND ");
681 if (pkt_dev->flags & F_IPDST_RND)
682 seq_printf(seq, "IPDST_RND ");
684 if (pkt_dev->flags & F_TXSIZE_RND)
685 seq_printf(seq, "TXSIZE_RND ");
687 if (pkt_dev->flags & F_UDPSRC_RND)
688 seq_printf(seq, "UDPSRC_RND ");
690 if (pkt_dev->flags & F_UDPDST_RND)
691 seq_printf(seq, "UDPDST_RND ");
693 if (pkt_dev->flags & F_MPLS_RND)
694 seq_printf(seq, "MPLS_RND ");
696 if (pkt_dev->flags & F_MACSRC_RND)
697 seq_printf(seq, "MACSRC_RND ");
699 if (pkt_dev->flags & F_MACDST_RND)
700 seq_printf(seq, "MACDST_RND ");
702 if (pkt_dev->flags & F_VID_RND)
703 seq_printf(seq, "VID_RND ");
705 if (pkt_dev->flags & F_SVID_RND)
706 seq_printf(seq, "SVID_RND ");
708 seq_puts(seq, "\n");
710 sa = pkt_dev->started_at;
711 stopped = pkt_dev->stopped_at;
712 if (pkt_dev->running)
713 stopped = now; /* not really stopped, more like last-running-at */
715 seq_printf(seq,
716 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
717 (unsigned long long)pkt_dev->sofar,
718 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
719 (unsigned long long)stopped,
720 (unsigned long long)pkt_dev->idle_acc);
722 seq_printf(seq,
723 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
724 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
725 pkt_dev->cur_src_mac_offset);
727 if (pkt_dev->flags & F_IPV6) {
728 char b1[128], b2[128];
729 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
730 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
731 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
732 } else
733 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
734 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
736 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
737 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
739 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
741 if (pkt_dev->result[0])
742 seq_printf(seq, "Result: %s\n", pkt_dev->result);
743 else
744 seq_printf(seq, "Result: Idle\n");
746 return 0;
750 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
752 int i = 0;
753 *num = 0;
755 for (; i < maxlen; i++) {
756 char c;
757 *num <<= 4;
758 if (get_user(c, &user_buffer[i]))
759 return -EFAULT;
760 if ((c >= '0') && (c <= '9'))
761 *num |= c - '0';
762 else if ((c >= 'a') && (c <= 'f'))
763 *num |= c - 'a' + 10;
764 else if ((c >= 'A') && (c <= 'F'))
765 *num |= c - 'A' + 10;
766 else
767 break;
769 return i;
772 static int count_trail_chars(const char __user * user_buffer,
773 unsigned int maxlen)
775 int i;
777 for (i = 0; i < maxlen; i++) {
778 char c;
779 if (get_user(c, &user_buffer[i]))
780 return -EFAULT;
781 switch (c) {
782 case '\"':
783 case '\n':
784 case '\r':
785 case '\t':
786 case ' ':
787 case '=':
788 break;
789 default:
790 goto done;
793 done:
794 return i;
797 static unsigned long num_arg(const char __user * user_buffer,
798 unsigned long maxlen, unsigned long *num)
800 int i = 0;
801 *num = 0;
803 for (; i < maxlen; i++) {
804 char c;
805 if (get_user(c, &user_buffer[i]))
806 return -EFAULT;
807 if ((c >= '0') && (c <= '9')) {
808 *num *= 10;
809 *num += c - '0';
810 } else
811 break;
813 return i;
816 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
818 int i = 0;
820 for (; i < maxlen; i++) {
821 char c;
822 if (get_user(c, &user_buffer[i]))
823 return -EFAULT;
824 switch (c) {
825 case '\"':
826 case '\n':
827 case '\r':
828 case '\t':
829 case ' ':
830 goto done_str;
831 break;
832 default:
833 break;
836 done_str:
837 return i;
840 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
842 unsigned n = 0;
843 char c;
844 ssize_t i = 0;
845 int len;
847 pkt_dev->nr_labels = 0;
848 do {
849 __u32 tmp;
850 len = hex32_arg(&buffer[i], 8, &tmp);
851 if (len <= 0)
852 return len;
853 pkt_dev->labels[n] = htonl(tmp);
854 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
855 pkt_dev->flags |= F_MPLS_RND;
856 i += len;
857 if (get_user(c, &buffer[i]))
858 return -EFAULT;
859 i++;
860 n++;
861 if (n >= MAX_MPLS_LABELS)
862 return -E2BIG;
863 } while (c == ',');
865 pkt_dev->nr_labels = n;
866 return i;
869 static ssize_t pktgen_if_write(struct file *file,
870 const char __user * user_buffer, size_t count,
871 loff_t * offset)
873 struct seq_file *seq = (struct seq_file *)file->private_data;
874 struct pktgen_dev *pkt_dev = seq->private;
875 int i = 0, max, len;
876 char name[16], valstr[32];
877 unsigned long value = 0;
878 char *pg_result = NULL;
879 int tmp = 0;
880 char buf[128];
882 pg_result = &(pkt_dev->result[0]);
884 if (count < 1) {
885 printk("pktgen: wrong command format\n");
886 return -EINVAL;
889 max = count - i;
890 tmp = count_trail_chars(&user_buffer[i], max);
891 if (tmp < 0) {
892 printk("pktgen: illegal format\n");
893 return tmp;
895 i += tmp;
897 /* Read variable name */
899 len = strn_len(&user_buffer[i], sizeof(name) - 1);
900 if (len < 0) {
901 return len;
903 memset(name, 0, sizeof(name));
904 if (copy_from_user(name, &user_buffer[i], len))
905 return -EFAULT;
906 i += len;
908 max = count - i;
909 len = count_trail_chars(&user_buffer[i], max);
910 if (len < 0)
911 return len;
913 i += len;
915 if (debug) {
916 char tb[count + 1];
917 if (copy_from_user(tb, user_buffer, count))
918 return -EFAULT;
919 tb[count] = 0;
920 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
921 (unsigned long)count, tb);
924 if (!strcmp(name, "min_pkt_size")) {
925 len = num_arg(&user_buffer[i], 10, &value);
926 if (len < 0) {
927 return len;
929 i += len;
930 if (value < 14 + 20 + 8)
931 value = 14 + 20 + 8;
932 if (value != pkt_dev->min_pkt_size) {
933 pkt_dev->min_pkt_size = value;
934 pkt_dev->cur_pkt_size = value;
936 sprintf(pg_result, "OK: min_pkt_size=%u",
937 pkt_dev->min_pkt_size);
938 return count;
941 if (!strcmp(name, "max_pkt_size")) {
942 len = num_arg(&user_buffer[i], 10, &value);
943 if (len < 0) {
944 return len;
946 i += len;
947 if (value < 14 + 20 + 8)
948 value = 14 + 20 + 8;
949 if (value != pkt_dev->max_pkt_size) {
950 pkt_dev->max_pkt_size = value;
951 pkt_dev->cur_pkt_size = value;
953 sprintf(pg_result, "OK: max_pkt_size=%u",
954 pkt_dev->max_pkt_size);
955 return count;
958 /* Shortcut for min = max */
960 if (!strcmp(name, "pkt_size")) {
961 len = num_arg(&user_buffer[i], 10, &value);
962 if (len < 0) {
963 return len;
965 i += len;
966 if (value < 14 + 20 + 8)
967 value = 14 + 20 + 8;
968 if (value != pkt_dev->min_pkt_size) {
969 pkt_dev->min_pkt_size = value;
970 pkt_dev->max_pkt_size = value;
971 pkt_dev->cur_pkt_size = value;
973 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
974 return count;
977 if (!strcmp(name, "debug")) {
978 len = num_arg(&user_buffer[i], 10, &value);
979 if (len < 0) {
980 return len;
982 i += len;
983 debug = value;
984 sprintf(pg_result, "OK: debug=%u", debug);
985 return count;
988 if (!strcmp(name, "frags")) {
989 len = num_arg(&user_buffer[i], 10, &value);
990 if (len < 0) {
991 return len;
993 i += len;
994 pkt_dev->nfrags = value;
995 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
996 return count;
998 if (!strcmp(name, "delay")) {
999 len = num_arg(&user_buffer[i], 10, &value);
1000 if (len < 0) {
1001 return len;
1003 i += len;
1004 if (value == 0x7FFFFFFF) {
1005 pkt_dev->delay_us = 0x7FFFFFFF;
1006 pkt_dev->delay_ns = 0;
1007 } else {
1008 pkt_dev->delay_us = value / 1000;
1009 pkt_dev->delay_ns = value % 1000;
1011 sprintf(pg_result, "OK: delay=%u",
1012 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1013 return count;
1015 if (!strcmp(name, "udp_src_min")) {
1016 len = num_arg(&user_buffer[i], 10, &value);
1017 if (len < 0) {
1018 return len;
1020 i += len;
1021 if (value != pkt_dev->udp_src_min) {
1022 pkt_dev->udp_src_min = value;
1023 pkt_dev->cur_udp_src = value;
1025 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1026 return count;
1028 if (!strcmp(name, "udp_dst_min")) {
1029 len = num_arg(&user_buffer[i], 10, &value);
1030 if (len < 0) {
1031 return len;
1033 i += len;
1034 if (value != pkt_dev->udp_dst_min) {
1035 pkt_dev->udp_dst_min = value;
1036 pkt_dev->cur_udp_dst = value;
1038 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1039 return count;
1041 if (!strcmp(name, "udp_src_max")) {
1042 len = num_arg(&user_buffer[i], 10, &value);
1043 if (len < 0) {
1044 return len;
1046 i += len;
1047 if (value != pkt_dev->udp_src_max) {
1048 pkt_dev->udp_src_max = value;
1049 pkt_dev->cur_udp_src = value;
1051 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1052 return count;
1054 if (!strcmp(name, "udp_dst_max")) {
1055 len = num_arg(&user_buffer[i], 10, &value);
1056 if (len < 0) {
1057 return len;
1059 i += len;
1060 if (value != pkt_dev->udp_dst_max) {
1061 pkt_dev->udp_dst_max = value;
1062 pkt_dev->cur_udp_dst = value;
1064 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1065 return count;
1067 if (!strcmp(name, "clone_skb")) {
1068 len = num_arg(&user_buffer[i], 10, &value);
1069 if (len < 0) {
1070 return len;
1072 i += len;
1073 pkt_dev->clone_skb = value;
1075 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1076 return count;
1078 if (!strcmp(name, "count")) {
1079 len = num_arg(&user_buffer[i], 10, &value);
1080 if (len < 0) {
1081 return len;
1083 i += len;
1084 pkt_dev->count = value;
1085 sprintf(pg_result, "OK: count=%llu",
1086 (unsigned long long)pkt_dev->count);
1087 return count;
1089 if (!strcmp(name, "src_mac_count")) {
1090 len = num_arg(&user_buffer[i], 10, &value);
1091 if (len < 0) {
1092 return len;
1094 i += len;
1095 if (pkt_dev->src_mac_count != value) {
1096 pkt_dev->src_mac_count = value;
1097 pkt_dev->cur_src_mac_offset = 0;
1099 sprintf(pg_result, "OK: src_mac_count=%d",
1100 pkt_dev->src_mac_count);
1101 return count;
1103 if (!strcmp(name, "dst_mac_count")) {
1104 len = num_arg(&user_buffer[i], 10, &value);
1105 if (len < 0) {
1106 return len;
1108 i += len;
1109 if (pkt_dev->dst_mac_count != value) {
1110 pkt_dev->dst_mac_count = value;
1111 pkt_dev->cur_dst_mac_offset = 0;
1113 sprintf(pg_result, "OK: dst_mac_count=%d",
1114 pkt_dev->dst_mac_count);
1115 return count;
1117 if (!strcmp(name, "flag")) {
1118 char f[32];
1119 memset(f, 0, 32);
1120 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1121 if (len < 0) {
1122 return len;
1124 if (copy_from_user(f, &user_buffer[i], len))
1125 return -EFAULT;
1126 i += len;
1127 if (strcmp(f, "IPSRC_RND") == 0)
1128 pkt_dev->flags |= F_IPSRC_RND;
1130 else if (strcmp(f, "!IPSRC_RND") == 0)
1131 pkt_dev->flags &= ~F_IPSRC_RND;
1133 else if (strcmp(f, "TXSIZE_RND") == 0)
1134 pkt_dev->flags |= F_TXSIZE_RND;
1136 else if (strcmp(f, "!TXSIZE_RND") == 0)
1137 pkt_dev->flags &= ~F_TXSIZE_RND;
1139 else if (strcmp(f, "IPDST_RND") == 0)
1140 pkt_dev->flags |= F_IPDST_RND;
1142 else if (strcmp(f, "!IPDST_RND") == 0)
1143 pkt_dev->flags &= ~F_IPDST_RND;
1145 else if (strcmp(f, "UDPSRC_RND") == 0)
1146 pkt_dev->flags |= F_UDPSRC_RND;
1148 else if (strcmp(f, "!UDPSRC_RND") == 0)
1149 pkt_dev->flags &= ~F_UDPSRC_RND;
1151 else if (strcmp(f, "UDPDST_RND") == 0)
1152 pkt_dev->flags |= F_UDPDST_RND;
1154 else if (strcmp(f, "!UDPDST_RND") == 0)
1155 pkt_dev->flags &= ~F_UDPDST_RND;
1157 else if (strcmp(f, "MACSRC_RND") == 0)
1158 pkt_dev->flags |= F_MACSRC_RND;
1160 else if (strcmp(f, "!MACSRC_RND") == 0)
1161 pkt_dev->flags &= ~F_MACSRC_RND;
1163 else if (strcmp(f, "MACDST_RND") == 0)
1164 pkt_dev->flags |= F_MACDST_RND;
1166 else if (strcmp(f, "!MACDST_RND") == 0)
1167 pkt_dev->flags &= ~F_MACDST_RND;
1169 else if (strcmp(f, "MPLS_RND") == 0)
1170 pkt_dev->flags |= F_MPLS_RND;
1172 else if (strcmp(f, "!MPLS_RND") == 0)
1173 pkt_dev->flags &= ~F_MPLS_RND;
1175 else if (strcmp(f, "VID_RND") == 0)
1176 pkt_dev->flags |= F_VID_RND;
1178 else if (strcmp(f, "!VID_RND") == 0)
1179 pkt_dev->flags &= ~F_VID_RND;
1181 else if (strcmp(f, "SVID_RND") == 0)
1182 pkt_dev->flags |= F_SVID_RND;
1184 else if (strcmp(f, "!SVID_RND") == 0)
1185 pkt_dev->flags &= ~F_SVID_RND;
1187 else if (strcmp(f, "!IPV6") == 0)
1188 pkt_dev->flags &= ~F_IPV6;
1190 else {
1191 sprintf(pg_result,
1192 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1194 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1195 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
1196 return count;
1198 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1199 return count;
1201 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1202 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1203 if (len < 0) {
1204 return len;
1207 if (copy_from_user(buf, &user_buffer[i], len))
1208 return -EFAULT;
1209 buf[len] = 0;
1210 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1211 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1212 strncpy(pkt_dev->dst_min, buf, len);
1213 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1214 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1216 if (debug)
1217 printk("pktgen: dst_min set to: %s\n",
1218 pkt_dev->dst_min);
1219 i += len;
1220 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1221 return count;
1223 if (!strcmp(name, "dst_max")) {
1224 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1225 if (len < 0) {
1226 return len;
1229 if (copy_from_user(buf, &user_buffer[i], len))
1230 return -EFAULT;
1232 buf[len] = 0;
1233 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1234 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1235 strncpy(pkt_dev->dst_max, buf, len);
1236 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1237 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1239 if (debug)
1240 printk("pktgen: dst_max set to: %s\n",
1241 pkt_dev->dst_max);
1242 i += len;
1243 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1244 return count;
1246 if (!strcmp(name, "dst6")) {
1247 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1248 if (len < 0)
1249 return len;
1251 pkt_dev->flags |= F_IPV6;
1253 if (copy_from_user(buf, &user_buffer[i], len))
1254 return -EFAULT;
1255 buf[len] = 0;
1257 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1258 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1260 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1262 if (debug)
1263 printk("pktgen: dst6 set to: %s\n", buf);
1265 i += len;
1266 sprintf(pg_result, "OK: dst6=%s", buf);
1267 return count;
1269 if (!strcmp(name, "dst6_min")) {
1270 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1271 if (len < 0)
1272 return len;
1274 pkt_dev->flags |= F_IPV6;
1276 if (copy_from_user(buf, &user_buffer[i], len))
1277 return -EFAULT;
1278 buf[len] = 0;
1280 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1281 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1283 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1284 &pkt_dev->min_in6_daddr);
1285 if (debug)
1286 printk("pktgen: dst6_min set to: %s\n", buf);
1288 i += len;
1289 sprintf(pg_result, "OK: dst6_min=%s", buf);
1290 return count;
1292 if (!strcmp(name, "dst6_max")) {
1293 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1294 if (len < 0)
1295 return len;
1297 pkt_dev->flags |= F_IPV6;
1299 if (copy_from_user(buf, &user_buffer[i], len))
1300 return -EFAULT;
1301 buf[len] = 0;
1303 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1304 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1306 if (debug)
1307 printk("pktgen: dst6_max set to: %s\n", buf);
1309 i += len;
1310 sprintf(pg_result, "OK: dst6_max=%s", buf);
1311 return count;
1313 if (!strcmp(name, "src6")) {
1314 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1315 if (len < 0)
1316 return len;
1318 pkt_dev->flags |= F_IPV6;
1320 if (copy_from_user(buf, &user_buffer[i], len))
1321 return -EFAULT;
1322 buf[len] = 0;
1324 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1325 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1327 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1329 if (debug)
1330 printk("pktgen: src6 set to: %s\n", buf);
1332 i += len;
1333 sprintf(pg_result, "OK: src6=%s", buf);
1334 return count;
1336 if (!strcmp(name, "src_min")) {
1337 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1338 if (len < 0) {
1339 return len;
1341 if (copy_from_user(buf, &user_buffer[i], len))
1342 return -EFAULT;
1343 buf[len] = 0;
1344 if (strcmp(buf, pkt_dev->src_min) != 0) {
1345 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1346 strncpy(pkt_dev->src_min, buf, len);
1347 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1348 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1350 if (debug)
1351 printk("pktgen: src_min set to: %s\n",
1352 pkt_dev->src_min);
1353 i += len;
1354 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1355 return count;
1357 if (!strcmp(name, "src_max")) {
1358 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1359 if (len < 0) {
1360 return len;
1362 if (copy_from_user(buf, &user_buffer[i], len))
1363 return -EFAULT;
1364 buf[len] = 0;
1365 if (strcmp(buf, pkt_dev->src_max) != 0) {
1366 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1367 strncpy(pkt_dev->src_max, buf, len);
1368 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1369 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1371 if (debug)
1372 printk("pktgen: src_max set to: %s\n",
1373 pkt_dev->src_max);
1374 i += len;
1375 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1376 return count;
1378 if (!strcmp(name, "dst_mac")) {
1379 char *v = valstr;
1380 unsigned char old_dmac[ETH_ALEN];
1381 unsigned char *m = pkt_dev->dst_mac;
1382 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1384 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1385 if (len < 0) {
1386 return len;
1388 memset(valstr, 0, sizeof(valstr));
1389 if (copy_from_user(valstr, &user_buffer[i], len))
1390 return -EFAULT;
1391 i += len;
1393 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1394 if (*v >= '0' && *v <= '9') {
1395 *m *= 16;
1396 *m += *v - '0';
1398 if (*v >= 'A' && *v <= 'F') {
1399 *m *= 16;
1400 *m += *v - 'A' + 10;
1402 if (*v >= 'a' && *v <= 'f') {
1403 *m *= 16;
1404 *m += *v - 'a' + 10;
1406 if (*v == ':') {
1407 m++;
1408 *m = 0;
1412 /* Set up Dest MAC */
1413 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1414 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1416 sprintf(pg_result, "OK: dstmac");
1417 return count;
1419 if (!strcmp(name, "src_mac")) {
1420 char *v = valstr;
1421 unsigned char old_smac[ETH_ALEN];
1422 unsigned char *m = pkt_dev->src_mac;
1424 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1426 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1427 if (len < 0) {
1428 return len;
1430 memset(valstr, 0, sizeof(valstr));
1431 if (copy_from_user(valstr, &user_buffer[i], len))
1432 return -EFAULT;
1433 i += len;
1435 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1436 if (*v >= '0' && *v <= '9') {
1437 *m *= 16;
1438 *m += *v - '0';
1440 if (*v >= 'A' && *v <= 'F') {
1441 *m *= 16;
1442 *m += *v - 'A' + 10;
1444 if (*v >= 'a' && *v <= 'f') {
1445 *m *= 16;
1446 *m += *v - 'a' + 10;
1448 if (*v == ':') {
1449 m++;
1450 *m = 0;
1454 /* Set up Src MAC */
1455 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1456 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1458 sprintf(pg_result, "OK: srcmac");
1459 return count;
1462 if (!strcmp(name, "clear_counters")) {
1463 pktgen_clear_counters(pkt_dev);
1464 sprintf(pg_result, "OK: Clearing counters.\n");
1465 return count;
1468 if (!strcmp(name, "flows")) {
1469 len = num_arg(&user_buffer[i], 10, &value);
1470 if (len < 0) {
1471 return len;
1473 i += len;
1474 if (value > MAX_CFLOWS)
1475 value = MAX_CFLOWS;
1477 pkt_dev->cflows = value;
1478 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1479 return count;
1482 if (!strcmp(name, "flowlen")) {
1483 len = num_arg(&user_buffer[i], 10, &value);
1484 if (len < 0) {
1485 return len;
1487 i += len;
1488 pkt_dev->lflow = value;
1489 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1490 return count;
1493 if (!strcmp(name, "mpls")) {
1494 unsigned n, offset;
1495 len = get_labels(&user_buffer[i], pkt_dev);
1496 if (len < 0) { return len; }
1497 i += len;
1498 offset = sprintf(pg_result, "OK: mpls=");
1499 for (n = 0; n < pkt_dev->nr_labels; n++)
1500 offset += sprintf(pg_result + offset,
1501 "%08x%s", ntohl(pkt_dev->labels[n]),
1502 n == pkt_dev->nr_labels-1 ? "" : ",");
1504 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1505 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1506 pkt_dev->svlan_id = 0xffff;
1508 if (debug)
1509 printk("pktgen: VLAN/SVLAN auto turned off\n");
1511 return count;
1514 if (!strcmp(name, "vlan_id")) {
1515 len = num_arg(&user_buffer[i], 4, &value);
1516 if (len < 0) {
1517 return len;
1519 i += len;
1520 if (value <= 4095) {
1521 pkt_dev->vlan_id = value; /* turn on VLAN */
1523 if (debug)
1524 printk("pktgen: VLAN turned on\n");
1526 if (debug && pkt_dev->nr_labels)
1527 printk("pktgen: MPLS auto turned off\n");
1529 pkt_dev->nr_labels = 0; /* turn off MPLS */
1530 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1531 } else {
1532 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1533 pkt_dev->svlan_id = 0xffff;
1535 if (debug)
1536 printk("pktgen: VLAN/SVLAN turned off\n");
1538 return count;
1541 if (!strcmp(name, "vlan_p")) {
1542 len = num_arg(&user_buffer[i], 1, &value);
1543 if (len < 0) {
1544 return len;
1546 i += len;
1547 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1548 pkt_dev->vlan_p = value;
1549 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1550 } else {
1551 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1553 return count;
1556 if (!strcmp(name, "vlan_cfi")) {
1557 len = num_arg(&user_buffer[i], 1, &value);
1558 if (len < 0) {
1559 return len;
1561 i += len;
1562 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1563 pkt_dev->vlan_cfi = value;
1564 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1565 } else {
1566 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1568 return count;
1571 if (!strcmp(name, "svlan_id")) {
1572 len = num_arg(&user_buffer[i], 4, &value);
1573 if (len < 0) {
1574 return len;
1576 i += len;
1577 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1578 pkt_dev->svlan_id = value; /* turn on SVLAN */
1580 if (debug)
1581 printk("pktgen: SVLAN turned on\n");
1583 if (debug && pkt_dev->nr_labels)
1584 printk("pktgen: MPLS auto turned off\n");
1586 pkt_dev->nr_labels = 0; /* turn off MPLS */
1587 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1588 } else {
1589 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1590 pkt_dev->svlan_id = 0xffff;
1592 if (debug)
1593 printk("pktgen: VLAN/SVLAN turned off\n");
1595 return count;
1598 if (!strcmp(name, "svlan_p")) {
1599 len = num_arg(&user_buffer[i], 1, &value);
1600 if (len < 0) {
1601 return len;
1603 i += len;
1604 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1605 pkt_dev->svlan_p = value;
1606 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1607 } else {
1608 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1610 return count;
1613 if (!strcmp(name, "svlan_cfi")) {
1614 len = num_arg(&user_buffer[i], 1, &value);
1615 if (len < 0) {
1616 return len;
1618 i += len;
1619 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1620 pkt_dev->svlan_cfi = value;
1621 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1622 } else {
1623 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1625 return count;
1628 if (!strcmp(name, "tos")) {
1629 __u32 tmp_value = 0;
1630 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1631 if (len < 0) {
1632 return len;
1634 i += len;
1635 if (len == 2) {
1636 pkt_dev->tos = tmp_value;
1637 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1638 } else {
1639 sprintf(pg_result, "ERROR: tos must be 00-ff");
1641 return count;
1644 if (!strcmp(name, "traffic_class")) {
1645 __u32 tmp_value = 0;
1646 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1647 if (len < 0) {
1648 return len;
1650 i += len;
1651 if (len == 2) {
1652 pkt_dev->traffic_class = tmp_value;
1653 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1654 } else {
1655 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1657 return count;
1660 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1661 return -EINVAL;
1664 static int pktgen_if_open(struct inode *inode, struct file *file)
1666 return single_open(file, pktgen_if_show, PDE(inode)->data);
1669 static const struct file_operations pktgen_if_fops = {
1670 .owner = THIS_MODULE,
1671 .open = pktgen_if_open,
1672 .read = seq_read,
1673 .llseek = seq_lseek,
1674 .write = pktgen_if_write,
1675 .release = single_release,
1678 static int pktgen_thread_show(struct seq_file *seq, void *v)
1680 struct pktgen_thread *t = seq->private;
1681 struct pktgen_dev *pkt_dev;
1683 BUG_ON(!t);
1685 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1686 t->tsk->comm, t->max_before_softirq);
1688 seq_printf(seq, "Running: ");
1690 if_lock(t);
1691 list_for_each_entry(pkt_dev, &t->if_list, list)
1692 if (pkt_dev->running)
1693 seq_printf(seq, "%s ", pkt_dev->odev->name);
1695 seq_printf(seq, "\nStopped: ");
1697 list_for_each_entry(pkt_dev, &t->if_list, list)
1698 if (!pkt_dev->running)
1699 seq_printf(seq, "%s ", pkt_dev->odev->name);
1701 if (t->result[0])
1702 seq_printf(seq, "\nResult: %s\n", t->result);
1703 else
1704 seq_printf(seq, "\nResult: NA\n");
1706 if_unlock(t);
1708 return 0;
1711 static ssize_t pktgen_thread_write(struct file *file,
1712 const char __user * user_buffer,
1713 size_t count, loff_t * offset)
1715 struct seq_file *seq = (struct seq_file *)file->private_data;
1716 struct pktgen_thread *t = seq->private;
1717 int i = 0, max, len, ret;
1718 char name[40];
1719 char *pg_result;
1720 unsigned long value = 0;
1722 if (count < 1) {
1723 // sprintf(pg_result, "Wrong command format");
1724 return -EINVAL;
1727 max = count - i;
1728 len = count_trail_chars(&user_buffer[i], max);
1729 if (len < 0)
1730 return len;
1732 i += len;
1734 /* Read variable name */
1736 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1737 if (len < 0)
1738 return len;
1740 memset(name, 0, sizeof(name));
1741 if (copy_from_user(name, &user_buffer[i], len))
1742 return -EFAULT;
1743 i += len;
1745 max = count - i;
1746 len = count_trail_chars(&user_buffer[i], max);
1747 if (len < 0)
1748 return len;
1750 i += len;
1752 if (debug)
1753 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
1755 if (!t) {
1756 printk("pktgen: ERROR: No thread\n");
1757 ret = -EINVAL;
1758 goto out;
1761 pg_result = &(t->result[0]);
1763 if (!strcmp(name, "add_device")) {
1764 char f[32];
1765 memset(f, 0, 32);
1766 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1767 if (len < 0) {
1768 ret = len;
1769 goto out;
1771 if (copy_from_user(f, &user_buffer[i], len))
1772 return -EFAULT;
1773 i += len;
1774 mutex_lock(&pktgen_thread_lock);
1775 pktgen_add_device(t, f);
1776 mutex_unlock(&pktgen_thread_lock);
1777 ret = count;
1778 sprintf(pg_result, "OK: add_device=%s", f);
1779 goto out;
1782 if (!strcmp(name, "rem_device_all")) {
1783 mutex_lock(&pktgen_thread_lock);
1784 t->control |= T_REMDEVALL;
1785 mutex_unlock(&pktgen_thread_lock);
1786 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1787 ret = count;
1788 sprintf(pg_result, "OK: rem_device_all");
1789 goto out;
1792 if (!strcmp(name, "max_before_softirq")) {
1793 len = num_arg(&user_buffer[i], 10, &value);
1794 mutex_lock(&pktgen_thread_lock);
1795 t->max_before_softirq = value;
1796 mutex_unlock(&pktgen_thread_lock);
1797 ret = count;
1798 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1799 goto out;
1802 ret = -EINVAL;
1803 out:
1804 return ret;
1807 static int pktgen_thread_open(struct inode *inode, struct file *file)
1809 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1812 static const struct file_operations pktgen_thread_fops = {
1813 .owner = THIS_MODULE,
1814 .open = pktgen_thread_open,
1815 .read = seq_read,
1816 .llseek = seq_lseek,
1817 .write = pktgen_thread_write,
1818 .release = single_release,
1821 /* Think find or remove for NN */
1822 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1824 struct pktgen_thread *t;
1825 struct pktgen_dev *pkt_dev = NULL;
1827 list_for_each_entry(t, &pktgen_threads, th_list) {
1828 pkt_dev = pktgen_find_dev(t, ifname);
1829 if (pkt_dev) {
1830 if (remove) {
1831 if_lock(t);
1832 pkt_dev->removal_mark = 1;
1833 t->control |= T_REMDEV;
1834 if_unlock(t);
1836 break;
1839 return pkt_dev;
1843 * mark a device for removal
1845 static void pktgen_mark_device(const char *ifname)
1847 struct pktgen_dev *pkt_dev = NULL;
1848 const int max_tries = 10, msec_per_try = 125;
1849 int i = 0;
1851 mutex_lock(&pktgen_thread_lock);
1852 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1854 while (1) {
1856 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1857 if (pkt_dev == NULL)
1858 break; /* success */
1860 mutex_unlock(&pktgen_thread_lock);
1861 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1862 "to disappear....\n", ifname);
1863 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1864 mutex_lock(&pktgen_thread_lock);
1866 if (++i >= max_tries) {
1867 printk("pktgen_mark_device: timed out after waiting "
1868 "%d msec for device %s to be removed\n",
1869 msec_per_try * i, ifname);
1870 break;
1875 mutex_unlock(&pktgen_thread_lock);
1878 static void pktgen_change_name(struct net_device *dev)
1880 struct pktgen_thread *t;
1882 list_for_each_entry(t, &pktgen_threads, th_list) {
1883 struct pktgen_dev *pkt_dev;
1885 list_for_each_entry(pkt_dev, &t->if_list, list) {
1886 if (pkt_dev->odev != dev)
1887 continue;
1889 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1891 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1892 pg_proc_dir);
1893 if (!pkt_dev->entry)
1894 printk(KERN_ERR "pktgen: can't move proc "
1895 " entry for '%s'\n", dev->name);
1896 break;
1901 static int pktgen_device_event(struct notifier_block *unused,
1902 unsigned long event, void *ptr)
1904 struct net_device *dev = ptr;
1906 /* It is OK that we do not hold the group lock right now,
1907 * as we run under the RTNL lock.
1910 switch (event) {
1911 case NETDEV_CHANGENAME:
1912 pktgen_change_name(dev);
1913 break;
1915 case NETDEV_UNREGISTER:
1916 pktgen_mark_device(dev->name);
1917 break;
1920 return NOTIFY_DONE;
1923 /* Associate pktgen_dev with a device. */
1925 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1927 struct net_device *odev;
1928 int err;
1930 /* Clean old setups */
1931 if (pkt_dev->odev) {
1932 dev_put(pkt_dev->odev);
1933 pkt_dev->odev = NULL;
1936 odev = dev_get_by_name(ifname);
1937 if (!odev) {
1938 printk("pktgen: no such netdevice: \"%s\"\n", ifname);
1939 return -ENODEV;
1942 if (odev->type != ARPHRD_ETHER) {
1943 printk("pktgen: not an ethernet device: \"%s\"\n", ifname);
1944 err = -EINVAL;
1945 } else if (!netif_running(odev)) {
1946 printk("pktgen: device is down: \"%s\"\n", ifname);
1947 err = -ENETDOWN;
1948 } else {
1949 pkt_dev->odev = odev;
1950 return 0;
1953 dev_put(odev);
1954 return err;
1957 /* Read pkt_dev from the interface and set up internal pktgen_dev
1958 * structure to have the right information to create/send packets
1960 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1962 if (!pkt_dev->odev) {
1963 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1964 sprintf(pkt_dev->result,
1965 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1966 return;
1969 /* Default to the interface's mac if not explicitly set. */
1971 if (is_zero_ether_addr(pkt_dev->src_mac))
1972 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1974 /* Set up Dest MAC */
1975 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1977 /* Set up pkt size */
1978 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1980 if (pkt_dev->flags & F_IPV6) {
1982 * Skip this automatic address setting until locks or functions
1983 * gets exported
1986 #ifdef NOTNOW
1987 int i, set = 0, err = 1;
1988 struct inet6_dev *idev;
1990 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1991 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1992 set = 1;
1993 break;
1996 if (!set) {
1999 * Use linklevel address if unconfigured.
2001 * use ipv6_get_lladdr if/when it's get exported
2004 rcu_read_lock();
2005 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2006 struct inet6_ifaddr *ifp;
2008 read_lock_bh(&idev->lock);
2009 for (ifp = idev->addr_list; ifp;
2010 ifp = ifp->if_next) {
2011 if (ifp->scope == IFA_LINK
2012 && !(ifp->
2013 flags & IFA_F_TENTATIVE)) {
2014 ipv6_addr_copy(&pkt_dev->
2015 cur_in6_saddr,
2016 &ifp->addr);
2017 err = 0;
2018 break;
2021 read_unlock_bh(&idev->lock);
2023 rcu_read_unlock();
2024 if (err)
2025 printk("pktgen: ERROR: IPv6 link address not availble.\n");
2027 #endif
2028 } else {
2029 pkt_dev->saddr_min = 0;
2030 pkt_dev->saddr_max = 0;
2031 if (strlen(pkt_dev->src_min) == 0) {
2033 struct in_device *in_dev;
2035 rcu_read_lock();
2036 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2037 if (in_dev) {
2038 if (in_dev->ifa_list) {
2039 pkt_dev->saddr_min =
2040 in_dev->ifa_list->ifa_address;
2041 pkt_dev->saddr_max = pkt_dev->saddr_min;
2044 rcu_read_unlock();
2045 } else {
2046 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2047 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2050 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2051 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2053 /* Initialize current values. */
2054 pkt_dev->cur_dst_mac_offset = 0;
2055 pkt_dev->cur_src_mac_offset = 0;
2056 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2057 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2058 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2059 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2060 pkt_dev->nflows = 0;
2063 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2065 __u64 start;
2066 __u64 now;
2068 start = now = getCurUs();
2069 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2070 while (now < spin_until_us) {
2071 /* TODO: optimize sleeping behavior */
2072 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2073 schedule_timeout_interruptible(1);
2074 else if (spin_until_us - now > 100) {
2075 do_softirq();
2076 if (!pkt_dev->running)
2077 return;
2078 if (need_resched())
2079 schedule();
2082 now = getCurUs();
2085 pkt_dev->idle_acc += now - start;
2088 /* Increment/randomize headers according to flags and current values
2089 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2091 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2093 __u32 imn;
2094 __u32 imx;
2095 int flow = 0;
2097 if (pkt_dev->cflows) {
2098 flow = random32() % pkt_dev->cflows;
2100 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2101 pkt_dev->flows[flow].count = 0;
2104 /* Deal with source MAC */
2105 if (pkt_dev->src_mac_count > 1) {
2106 __u32 mc;
2107 __u32 tmp;
2109 if (pkt_dev->flags & F_MACSRC_RND)
2110 mc = random32() % pkt_dev->src_mac_count;
2111 else {
2112 mc = pkt_dev->cur_src_mac_offset++;
2113 if (pkt_dev->cur_src_mac_offset >
2114 pkt_dev->src_mac_count)
2115 pkt_dev->cur_src_mac_offset = 0;
2118 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2119 pkt_dev->hh[11] = tmp;
2120 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2121 pkt_dev->hh[10] = tmp;
2122 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2123 pkt_dev->hh[9] = tmp;
2124 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2125 pkt_dev->hh[8] = tmp;
2126 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2127 pkt_dev->hh[7] = tmp;
2130 /* Deal with Destination MAC */
2131 if (pkt_dev->dst_mac_count > 1) {
2132 __u32 mc;
2133 __u32 tmp;
2135 if (pkt_dev->flags & F_MACDST_RND)
2136 mc = random32() % pkt_dev->dst_mac_count;
2138 else {
2139 mc = pkt_dev->cur_dst_mac_offset++;
2140 if (pkt_dev->cur_dst_mac_offset >
2141 pkt_dev->dst_mac_count) {
2142 pkt_dev->cur_dst_mac_offset = 0;
2146 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2147 pkt_dev->hh[5] = tmp;
2148 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2149 pkt_dev->hh[4] = tmp;
2150 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2151 pkt_dev->hh[3] = tmp;
2152 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2153 pkt_dev->hh[2] = tmp;
2154 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2155 pkt_dev->hh[1] = tmp;
2158 if (pkt_dev->flags & F_MPLS_RND) {
2159 unsigned i;
2160 for (i = 0; i < pkt_dev->nr_labels; i++)
2161 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2162 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2163 ((__force __be32)random32() &
2164 htonl(0x000fffff));
2167 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2168 pkt_dev->vlan_id = random32() & (4096-1);
2171 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2172 pkt_dev->svlan_id = random32() & (4096 - 1);
2175 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2176 if (pkt_dev->flags & F_UDPSRC_RND)
2177 pkt_dev->cur_udp_src = random32() %
2178 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2179 + pkt_dev->udp_src_min;
2181 else {
2182 pkt_dev->cur_udp_src++;
2183 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2184 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2188 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2189 if (pkt_dev->flags & F_UDPDST_RND) {
2190 pkt_dev->cur_udp_dst = random32() %
2191 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2192 + pkt_dev->udp_dst_min;
2193 } else {
2194 pkt_dev->cur_udp_dst++;
2195 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2196 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2200 if (!(pkt_dev->flags & F_IPV6)) {
2202 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2203 ntohl(pkt_dev->
2204 saddr_max))) {
2205 __u32 t;
2206 if (pkt_dev->flags & F_IPSRC_RND)
2207 t = random32() % (imx - imn) + imn;
2208 else {
2209 t = ntohl(pkt_dev->cur_saddr);
2210 t++;
2211 if (t > imx) {
2212 t = imn;
2215 pkt_dev->cur_saddr = htonl(t);
2218 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2219 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2220 } else {
2221 imn = ntohl(pkt_dev->daddr_min);
2222 imx = ntohl(pkt_dev->daddr_max);
2223 if (imn < imx) {
2224 __u32 t;
2225 __be32 s;
2226 if (pkt_dev->flags & F_IPDST_RND) {
2228 t = random32() % (imx - imn) + imn;
2229 s = htonl(t);
2231 while (LOOPBACK(s) || MULTICAST(s)
2232 || BADCLASS(s) || ZERONET(s)
2233 || LOCAL_MCAST(s)) {
2234 t = random32() % (imx - imn) + imn;
2235 s = htonl(t);
2237 pkt_dev->cur_daddr = s;
2238 } else {
2239 t = ntohl(pkt_dev->cur_daddr);
2240 t++;
2241 if (t > imx) {
2242 t = imn;
2244 pkt_dev->cur_daddr = htonl(t);
2247 if (pkt_dev->cflows) {
2248 pkt_dev->flows[flow].cur_daddr =
2249 pkt_dev->cur_daddr;
2250 pkt_dev->nflows++;
2253 } else { /* IPV6 * */
2255 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2256 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2257 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2258 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2259 else {
2260 int i;
2262 /* Only random destinations yet */
2264 for (i = 0; i < 4; i++) {
2265 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2266 (((__force __be32)random32() |
2267 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2268 pkt_dev->max_in6_daddr.s6_addr32[i]);
2273 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2274 __u32 t;
2275 if (pkt_dev->flags & F_TXSIZE_RND) {
2276 t = random32() %
2277 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2278 + pkt_dev->min_pkt_size;
2279 } else {
2280 t = pkt_dev->cur_pkt_size + 1;
2281 if (t > pkt_dev->max_pkt_size)
2282 t = pkt_dev->min_pkt_size;
2284 pkt_dev->cur_pkt_size = t;
2287 pkt_dev->flows[flow].count++;
2290 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2292 unsigned i;
2293 for (i = 0; i < pkt_dev->nr_labels; i++) {
2294 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2296 mpls--;
2297 *mpls |= MPLS_STACK_BOTTOM;
2300 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2301 unsigned int prio)
2303 return htons(id | (cfi << 12) | (prio << 13));
2306 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2307 struct pktgen_dev *pkt_dev)
2309 struct sk_buff *skb = NULL;
2310 __u8 *eth;
2311 struct udphdr *udph;
2312 int datalen, iplen;
2313 struct iphdr *iph;
2314 struct pktgen_hdr *pgh = NULL;
2315 __be16 protocol = htons(ETH_P_IP);
2316 __be32 *mpls;
2317 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2318 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2319 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2320 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2323 if (pkt_dev->nr_labels)
2324 protocol = htons(ETH_P_MPLS_UC);
2326 if (pkt_dev->vlan_id != 0xffff)
2327 protocol = htons(ETH_P_8021Q);
2329 /* Update any of the values, used when we're incrementing various
2330 * fields.
2332 mod_cur_headers(pkt_dev);
2334 datalen = (odev->hard_header_len + 16) & ~0xf;
2335 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2336 pkt_dev->nr_labels*sizeof(u32) +
2337 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2338 GFP_ATOMIC);
2339 if (!skb) {
2340 sprintf(pkt_dev->result, "No memory");
2341 return NULL;
2344 skb_reserve(skb, datalen);
2346 /* Reserve for ethernet and IP header */
2347 eth = (__u8 *) skb_push(skb, 14);
2348 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2349 if (pkt_dev->nr_labels)
2350 mpls_push(mpls, pkt_dev);
2352 if (pkt_dev->vlan_id != 0xffff) {
2353 if (pkt_dev->svlan_id != 0xffff) {
2354 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2355 *svlan_tci = build_tci(pkt_dev->svlan_id,
2356 pkt_dev->svlan_cfi,
2357 pkt_dev->svlan_p);
2358 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2359 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2361 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2362 *vlan_tci = build_tci(pkt_dev->vlan_id,
2363 pkt_dev->vlan_cfi,
2364 pkt_dev->vlan_p);
2365 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2366 *vlan_encapsulated_proto = htons(ETH_P_IP);
2369 skb->network_header = skb->tail;
2370 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2371 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2373 iph = ip_hdr(skb);
2374 udph = udp_hdr(skb);
2376 memcpy(eth, pkt_dev->hh, 12);
2377 *(__be16 *) & eth[12] = protocol;
2379 /* Eth + IPh + UDPh + mpls */
2380 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2381 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2382 if (datalen < sizeof(struct pktgen_hdr))
2383 datalen = sizeof(struct pktgen_hdr);
2385 udph->source = htons(pkt_dev->cur_udp_src);
2386 udph->dest = htons(pkt_dev->cur_udp_dst);
2387 udph->len = htons(datalen + 8); /* DATA + udphdr */
2388 udph->check = 0; /* No checksum */
2390 iph->ihl = 5;
2391 iph->version = 4;
2392 iph->ttl = 32;
2393 iph->tos = pkt_dev->tos;
2394 iph->protocol = IPPROTO_UDP; /* UDP */
2395 iph->saddr = pkt_dev->cur_saddr;
2396 iph->daddr = pkt_dev->cur_daddr;
2397 iph->frag_off = 0;
2398 iplen = 20 + 8 + datalen;
2399 iph->tot_len = htons(iplen);
2400 iph->check = 0;
2401 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2402 skb->protocol = protocol;
2403 skb->mac_header = (skb->network_header - ETH_HLEN -
2404 pkt_dev->nr_labels * sizeof(u32) -
2405 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev));
2406 skb->dev = odev;
2407 skb->pkt_type = PACKET_HOST;
2409 if (pkt_dev->nfrags <= 0)
2410 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2411 else {
2412 int frags = pkt_dev->nfrags;
2413 int i;
2415 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2417 if (frags > MAX_SKB_FRAGS)
2418 frags = MAX_SKB_FRAGS;
2419 if (datalen > frags * PAGE_SIZE) {
2420 skb_put(skb, datalen - frags * PAGE_SIZE);
2421 datalen = frags * PAGE_SIZE;
2424 i = 0;
2425 while (datalen > 0) {
2426 struct page *page = alloc_pages(GFP_KERNEL, 0);
2427 skb_shinfo(skb)->frags[i].page = page;
2428 skb_shinfo(skb)->frags[i].page_offset = 0;
2429 skb_shinfo(skb)->frags[i].size =
2430 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2431 datalen -= skb_shinfo(skb)->frags[i].size;
2432 skb->len += skb_shinfo(skb)->frags[i].size;
2433 skb->data_len += skb_shinfo(skb)->frags[i].size;
2434 i++;
2435 skb_shinfo(skb)->nr_frags = i;
2438 while (i < frags) {
2439 int rem;
2441 if (i == 0)
2442 break;
2444 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2445 if (rem == 0)
2446 break;
2448 skb_shinfo(skb)->frags[i - 1].size -= rem;
2450 skb_shinfo(skb)->frags[i] =
2451 skb_shinfo(skb)->frags[i - 1];
2452 get_page(skb_shinfo(skb)->frags[i].page);
2453 skb_shinfo(skb)->frags[i].page =
2454 skb_shinfo(skb)->frags[i - 1].page;
2455 skb_shinfo(skb)->frags[i].page_offset +=
2456 skb_shinfo(skb)->frags[i - 1].size;
2457 skb_shinfo(skb)->frags[i].size = rem;
2458 i++;
2459 skb_shinfo(skb)->nr_frags = i;
2463 /* Stamp the time, and sequence number, convert them to network byte order */
2465 if (pgh) {
2466 struct timeval timestamp;
2468 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2469 pgh->seq_num = htonl(pkt_dev->seq_num);
2471 do_gettimeofday(&timestamp);
2472 pgh->tv_sec = htonl(timestamp.tv_sec);
2473 pgh->tv_usec = htonl(timestamp.tv_usec);
2476 return skb;
2480 * scan_ip6, fmt_ip taken from dietlibc-0.21
2481 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2483 * Slightly modified for kernel.
2484 * Should be candidate for net/ipv4/utils.c
2485 * --ro
2488 static unsigned int scan_ip6(const char *s, char ip[16])
2490 unsigned int i;
2491 unsigned int len = 0;
2492 unsigned long u;
2493 char suffix[16];
2494 unsigned int prefixlen = 0;
2495 unsigned int suffixlen = 0;
2496 __be32 tmp;
2498 for (i = 0; i < 16; i++)
2499 ip[i] = 0;
2501 for (;;) {
2502 if (*s == ':') {
2503 len++;
2504 if (s[1] == ':') { /* Found "::", skip to part 2 */
2505 s += 2;
2506 len++;
2507 break;
2509 s++;
2512 char *tmp;
2513 u = simple_strtoul(s, &tmp, 16);
2514 i = tmp - s;
2517 if (!i)
2518 return 0;
2519 if (prefixlen == 12 && s[i] == '.') {
2521 /* the last 4 bytes may be written as IPv4 address */
2523 tmp = in_aton(s);
2524 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2525 return i + len;
2527 ip[prefixlen++] = (u >> 8);
2528 ip[prefixlen++] = (u & 255);
2529 s += i;
2530 len += i;
2531 if (prefixlen == 16)
2532 return len;
2535 /* part 2, after "::" */
2536 for (;;) {
2537 if (*s == ':') {
2538 if (suffixlen == 0)
2539 break;
2540 s++;
2541 len++;
2542 } else if (suffixlen != 0)
2543 break;
2545 char *tmp;
2546 u = simple_strtol(s, &tmp, 16);
2547 i = tmp - s;
2549 if (!i) {
2550 if (*s)
2551 len--;
2552 break;
2554 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2555 tmp = in_aton(s);
2556 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2557 sizeof(tmp));
2558 suffixlen += 4;
2559 len += strlen(s);
2560 break;
2562 suffix[suffixlen++] = (u >> 8);
2563 suffix[suffixlen++] = (u & 255);
2564 s += i;
2565 len += i;
2566 if (prefixlen + suffixlen == 16)
2567 break;
2569 for (i = 0; i < suffixlen; i++)
2570 ip[16 - suffixlen + i] = suffix[i];
2571 return len;
2574 static char tohex(char hexdigit)
2576 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2579 static int fmt_xlong(char *s, unsigned int i)
2581 char *bak = s;
2582 *s = tohex((i >> 12) & 0xf);
2583 if (s != bak || *s != '0')
2584 ++s;
2585 *s = tohex((i >> 8) & 0xf);
2586 if (s != bak || *s != '0')
2587 ++s;
2588 *s = tohex((i >> 4) & 0xf);
2589 if (s != bak || *s != '0')
2590 ++s;
2591 *s = tohex(i & 0xf);
2592 return s - bak + 1;
2595 static unsigned int fmt_ip6(char *s, const char ip[16])
2597 unsigned int len;
2598 unsigned int i;
2599 unsigned int temp;
2600 unsigned int compressing;
2601 int j;
2603 len = 0;
2604 compressing = 0;
2605 for (j = 0; j < 16; j += 2) {
2607 #ifdef V4MAPPEDPREFIX
2608 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2609 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2610 temp = strlen(s);
2611 return len + temp;
2613 #endif
2614 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2615 (unsigned long)(unsigned char)ip[j + 1];
2616 if (temp == 0) {
2617 if (!compressing) {
2618 compressing = 1;
2619 if (j == 0) {
2620 *s++ = ':';
2621 ++len;
2624 } else {
2625 if (compressing) {
2626 compressing = 0;
2627 *s++ = ':';
2628 ++len;
2630 i = fmt_xlong(s, temp);
2631 len += i;
2632 s += i;
2633 if (j < 14) {
2634 *s++ = ':';
2635 ++len;
2639 if (compressing) {
2640 *s++ = ':';
2641 ++len;
2643 *s = 0;
2644 return len;
2647 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2648 struct pktgen_dev *pkt_dev)
2650 struct sk_buff *skb = NULL;
2651 __u8 *eth;
2652 struct udphdr *udph;
2653 int datalen;
2654 struct ipv6hdr *iph;
2655 struct pktgen_hdr *pgh = NULL;
2656 __be16 protocol = htons(ETH_P_IPV6);
2657 __be32 *mpls;
2658 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2659 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2660 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2661 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2663 if (pkt_dev->nr_labels)
2664 protocol = htons(ETH_P_MPLS_UC);
2666 if (pkt_dev->vlan_id != 0xffff)
2667 protocol = htons(ETH_P_8021Q);
2669 /* Update any of the values, used when we're incrementing various
2670 * fields.
2672 mod_cur_headers(pkt_dev);
2674 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2675 pkt_dev->nr_labels*sizeof(u32) +
2676 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2677 GFP_ATOMIC);
2678 if (!skb) {
2679 sprintf(pkt_dev->result, "No memory");
2680 return NULL;
2683 skb_reserve(skb, 16);
2685 /* Reserve for ethernet and IP header */
2686 eth = (__u8 *) skb_push(skb, 14);
2687 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2688 if (pkt_dev->nr_labels)
2689 mpls_push(mpls, pkt_dev);
2691 if (pkt_dev->vlan_id != 0xffff) {
2692 if (pkt_dev->svlan_id != 0xffff) {
2693 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2694 *svlan_tci = build_tci(pkt_dev->svlan_id,
2695 pkt_dev->svlan_cfi,
2696 pkt_dev->svlan_p);
2697 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2698 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2700 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2701 *vlan_tci = build_tci(pkt_dev->vlan_id,
2702 pkt_dev->vlan_cfi,
2703 pkt_dev->vlan_p);
2704 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2705 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2708 skb->network_header = skb->tail;
2709 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2710 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2712 iph = ipv6_hdr(skb);
2713 udph = udp_hdr(skb);
2715 memcpy(eth, pkt_dev->hh, 12);
2716 *(__be16 *) & eth[12] = protocol;
2718 /* Eth + IPh + UDPh + mpls */
2719 datalen = pkt_dev->cur_pkt_size - 14 -
2720 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2721 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2723 if (datalen < sizeof(struct pktgen_hdr)) {
2724 datalen = sizeof(struct pktgen_hdr);
2725 if (net_ratelimit())
2726 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2727 datalen);
2730 udph->source = htons(pkt_dev->cur_udp_src);
2731 udph->dest = htons(pkt_dev->cur_udp_dst);
2732 udph->len = htons(datalen + sizeof(struct udphdr));
2733 udph->check = 0; /* No checksum */
2735 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2737 if (pkt_dev->traffic_class) {
2738 /* Version + traffic class + flow (0) */
2739 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2742 iph->hop_limit = 32;
2744 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2745 iph->nexthdr = IPPROTO_UDP;
2747 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2748 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2750 skb->mac_header = (skb->network_header - ETH_HLEN -
2751 pkt_dev->nr_labels * sizeof(u32) -
2752 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev));
2753 skb->protocol = protocol;
2754 skb->dev = odev;
2755 skb->pkt_type = PACKET_HOST;
2757 if (pkt_dev->nfrags <= 0)
2758 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2759 else {
2760 int frags = pkt_dev->nfrags;
2761 int i;
2763 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2765 if (frags > MAX_SKB_FRAGS)
2766 frags = MAX_SKB_FRAGS;
2767 if (datalen > frags * PAGE_SIZE) {
2768 skb_put(skb, datalen - frags * PAGE_SIZE);
2769 datalen = frags * PAGE_SIZE;
2772 i = 0;
2773 while (datalen > 0) {
2774 struct page *page = alloc_pages(GFP_KERNEL, 0);
2775 skb_shinfo(skb)->frags[i].page = page;
2776 skb_shinfo(skb)->frags[i].page_offset = 0;
2777 skb_shinfo(skb)->frags[i].size =
2778 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2779 datalen -= skb_shinfo(skb)->frags[i].size;
2780 skb->len += skb_shinfo(skb)->frags[i].size;
2781 skb->data_len += skb_shinfo(skb)->frags[i].size;
2782 i++;
2783 skb_shinfo(skb)->nr_frags = i;
2786 while (i < frags) {
2787 int rem;
2789 if (i == 0)
2790 break;
2792 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2793 if (rem == 0)
2794 break;
2796 skb_shinfo(skb)->frags[i - 1].size -= rem;
2798 skb_shinfo(skb)->frags[i] =
2799 skb_shinfo(skb)->frags[i - 1];
2800 get_page(skb_shinfo(skb)->frags[i].page);
2801 skb_shinfo(skb)->frags[i].page =
2802 skb_shinfo(skb)->frags[i - 1].page;
2803 skb_shinfo(skb)->frags[i].page_offset +=
2804 skb_shinfo(skb)->frags[i - 1].size;
2805 skb_shinfo(skb)->frags[i].size = rem;
2806 i++;
2807 skb_shinfo(skb)->nr_frags = i;
2811 /* Stamp the time, and sequence number, convert them to network byte order */
2812 /* should we update cloned packets too ? */
2813 if (pgh) {
2814 struct timeval timestamp;
2816 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2817 pgh->seq_num = htonl(pkt_dev->seq_num);
2819 do_gettimeofday(&timestamp);
2820 pgh->tv_sec = htonl(timestamp.tv_sec);
2821 pgh->tv_usec = htonl(timestamp.tv_usec);
2823 /* pkt_dev->seq_num++; FF: you really mean this? */
2825 return skb;
2828 static inline struct sk_buff *fill_packet(struct net_device *odev,
2829 struct pktgen_dev *pkt_dev)
2831 if (pkt_dev->flags & F_IPV6)
2832 return fill_packet_ipv6(odev, pkt_dev);
2833 else
2834 return fill_packet_ipv4(odev, pkt_dev);
2837 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2839 pkt_dev->seq_num = 1;
2840 pkt_dev->idle_acc = 0;
2841 pkt_dev->sofar = 0;
2842 pkt_dev->tx_bytes = 0;
2843 pkt_dev->errors = 0;
2846 /* Set up structure for sending pkts, clear counters */
2848 static void pktgen_run(struct pktgen_thread *t)
2850 struct pktgen_dev *pkt_dev;
2851 int started = 0;
2853 pr_debug("pktgen: entering pktgen_run. %p\n", t);
2855 if_lock(t);
2856 list_for_each_entry(pkt_dev, &t->if_list, list) {
2859 * setup odev and create initial packet.
2861 pktgen_setup_inject(pkt_dev);
2863 if (pkt_dev->odev) {
2864 pktgen_clear_counters(pkt_dev);
2865 pkt_dev->running = 1; /* Cranke yeself! */
2866 pkt_dev->skb = NULL;
2867 pkt_dev->started_at = getCurUs();
2868 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2869 pkt_dev->next_tx_ns = 0;
2871 strcpy(pkt_dev->result, "Starting");
2872 started++;
2873 } else
2874 strcpy(pkt_dev->result, "Error starting");
2876 if_unlock(t);
2877 if (started)
2878 t->control &= ~(T_STOP);
2881 static void pktgen_stop_all_threads_ifs(void)
2883 struct pktgen_thread *t;
2885 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
2887 mutex_lock(&pktgen_thread_lock);
2889 list_for_each_entry(t, &pktgen_threads, th_list)
2890 t->control |= T_STOP;
2892 mutex_unlock(&pktgen_thread_lock);
2895 static int thread_is_running(struct pktgen_thread *t)
2897 struct pktgen_dev *pkt_dev;
2898 int res = 0;
2900 list_for_each_entry(pkt_dev, &t->if_list, list)
2901 if (pkt_dev->running) {
2902 res = 1;
2903 break;
2905 return res;
2908 static int pktgen_wait_thread_run(struct pktgen_thread *t)
2910 if_lock(t);
2912 while (thread_is_running(t)) {
2914 if_unlock(t);
2916 msleep_interruptible(100);
2918 if (signal_pending(current))
2919 goto signal;
2920 if_lock(t);
2922 if_unlock(t);
2923 return 1;
2924 signal:
2925 return 0;
2928 static int pktgen_wait_all_threads_run(void)
2930 struct pktgen_thread *t;
2931 int sig = 1;
2933 mutex_lock(&pktgen_thread_lock);
2935 list_for_each_entry(t, &pktgen_threads, th_list) {
2936 sig = pktgen_wait_thread_run(t);
2937 if (sig == 0)
2938 break;
2941 if (sig == 0)
2942 list_for_each_entry(t, &pktgen_threads, th_list)
2943 t->control |= (T_STOP);
2945 mutex_unlock(&pktgen_thread_lock);
2946 return sig;
2949 static void pktgen_run_all_threads(void)
2951 struct pktgen_thread *t;
2953 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
2955 mutex_lock(&pktgen_thread_lock);
2957 list_for_each_entry(t, &pktgen_threads, th_list)
2958 t->control |= (T_RUN);
2960 mutex_unlock(&pktgen_thread_lock);
2962 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2964 pktgen_wait_all_threads_run();
2967 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2969 __u64 total_us, bps, mbps, pps, idle;
2970 char *p = pkt_dev->result;
2972 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
2974 idle = pkt_dev->idle_acc;
2976 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2977 (unsigned long long)total_us,
2978 (unsigned long long)(total_us - idle),
2979 (unsigned long long)idle,
2980 (unsigned long long)pkt_dev->sofar,
2981 pkt_dev->cur_pkt_size, nr_frags);
2983 pps = pkt_dev->sofar * USEC_PER_SEC;
2985 while ((total_us >> 32) != 0) {
2986 pps >>= 1;
2987 total_us >>= 1;
2990 do_div(pps, total_us);
2992 bps = pps * 8 * pkt_dev->cur_pkt_size;
2994 mbps = bps;
2995 do_div(mbps, 1000000);
2996 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2997 (unsigned long long)pps,
2998 (unsigned long long)mbps,
2999 (unsigned long long)bps,
3000 (unsigned long long)pkt_dev->errors);
3003 /* Set stopped-at timer, remove from running list, do counters & statistics */
3005 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3007 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3009 if (!pkt_dev->running) {
3010 printk("pktgen: interface: %s is already stopped\n",
3011 pkt_dev->odev->name);
3012 return -EINVAL;
3015 pkt_dev->stopped_at = getCurUs();
3016 pkt_dev->running = 0;
3018 show_results(pkt_dev, nr_frags);
3020 return 0;
3023 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3025 struct pktgen_dev *pkt_dev, *best = NULL;
3027 if_lock(t);
3029 list_for_each_entry(pkt_dev, &t->if_list, list) {
3030 if (!pkt_dev->running)
3031 continue;
3032 if (best == NULL)
3033 best = pkt_dev;
3034 else if (pkt_dev->next_tx_us < best->next_tx_us)
3035 best = pkt_dev;
3037 if_unlock(t);
3038 return best;
3041 static void pktgen_stop(struct pktgen_thread *t)
3043 struct pktgen_dev *pkt_dev;
3045 pr_debug("pktgen: entering pktgen_stop\n");
3047 if_lock(t);
3049 list_for_each_entry(pkt_dev, &t->if_list, list) {
3050 pktgen_stop_device(pkt_dev);
3051 if (pkt_dev->skb)
3052 kfree_skb(pkt_dev->skb);
3054 pkt_dev->skb = NULL;
3057 if_unlock(t);
3061 * one of our devices needs to be removed - find it
3062 * and remove it
3064 static void pktgen_rem_one_if(struct pktgen_thread *t)
3066 struct list_head *q, *n;
3067 struct pktgen_dev *cur;
3069 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3071 if_lock(t);
3073 list_for_each_safe(q, n, &t->if_list) {
3074 cur = list_entry(q, struct pktgen_dev, list);
3076 if (!cur->removal_mark)
3077 continue;
3079 if (cur->skb)
3080 kfree_skb(cur->skb);
3081 cur->skb = NULL;
3083 pktgen_remove_device(t, cur);
3085 break;
3088 if_unlock(t);
3091 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3093 struct list_head *q, *n;
3094 struct pktgen_dev *cur;
3096 /* Remove all devices, free mem */
3098 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3099 if_lock(t);
3101 list_for_each_safe(q, n, &t->if_list) {
3102 cur = list_entry(q, struct pktgen_dev, list);
3104 if (cur->skb)
3105 kfree_skb(cur->skb);
3106 cur->skb = NULL;
3108 pktgen_remove_device(t, cur);
3111 if_unlock(t);
3114 static void pktgen_rem_thread(struct pktgen_thread *t)
3116 /* Remove from the thread list */
3118 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3120 mutex_lock(&pktgen_thread_lock);
3122 list_del(&t->th_list);
3124 mutex_unlock(&pktgen_thread_lock);
3127 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3129 struct net_device *odev = NULL;
3130 __u64 idle_start = 0;
3131 int ret;
3133 odev = pkt_dev->odev;
3135 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3136 u64 now;
3138 now = getCurUs();
3139 if (now < pkt_dev->next_tx_us)
3140 spin(pkt_dev, pkt_dev->next_tx_us);
3142 /* This is max DELAY, this has special meaning of
3143 * "never transmit"
3145 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3146 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3147 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3148 goto out;
3152 if (netif_queue_stopped(odev) || need_resched()) {
3153 idle_start = getCurUs();
3155 if (!netif_running(odev)) {
3156 pktgen_stop_device(pkt_dev);
3157 if (pkt_dev->skb)
3158 kfree_skb(pkt_dev->skb);
3159 pkt_dev->skb = NULL;
3160 goto out;
3162 if (need_resched())
3163 schedule();
3165 pkt_dev->idle_acc += getCurUs() - idle_start;
3167 if (netif_queue_stopped(odev)) {
3168 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3169 pkt_dev->next_tx_ns = 0;
3170 goto out; /* Try the next interface */
3174 if (pkt_dev->last_ok || !pkt_dev->skb) {
3175 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3176 || (!pkt_dev->skb)) {
3177 /* build a new pkt */
3178 if (pkt_dev->skb)
3179 kfree_skb(pkt_dev->skb);
3181 pkt_dev->skb = fill_packet(odev, pkt_dev);
3182 if (pkt_dev->skb == NULL) {
3183 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3184 schedule();
3185 pkt_dev->clone_count--; /* back out increment, OOM */
3186 goto out;
3188 pkt_dev->allocated_skbs++;
3189 pkt_dev->clone_count = 0; /* reset counter */
3193 netif_tx_lock_bh(odev);
3194 if (!netif_queue_stopped(odev)) {
3196 atomic_inc(&(pkt_dev->skb->users));
3197 retry_now:
3198 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3199 if (likely(ret == NETDEV_TX_OK)) {
3200 pkt_dev->last_ok = 1;
3201 pkt_dev->sofar++;
3202 pkt_dev->seq_num++;
3203 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3205 } else if (ret == NETDEV_TX_LOCKED
3206 && (odev->features & NETIF_F_LLTX)) {
3207 cpu_relax();
3208 goto retry_now;
3209 } else { /* Retry it next time */
3211 atomic_dec(&(pkt_dev->skb->users));
3213 if (debug && net_ratelimit())
3214 printk(KERN_INFO "pktgen: Hard xmit error\n");
3216 pkt_dev->errors++;
3217 pkt_dev->last_ok = 0;
3220 pkt_dev->next_tx_us = getCurUs();
3221 pkt_dev->next_tx_ns = 0;
3223 pkt_dev->next_tx_us += pkt_dev->delay_us;
3224 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3226 if (pkt_dev->next_tx_ns > 1000) {
3227 pkt_dev->next_tx_us++;
3228 pkt_dev->next_tx_ns -= 1000;
3232 else { /* Retry it next time */
3233 pkt_dev->last_ok = 0;
3234 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3235 pkt_dev->next_tx_ns = 0;
3238 netif_tx_unlock_bh(odev);
3240 /* If pkt_dev->count is zero, then run forever */
3241 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3242 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3243 idle_start = getCurUs();
3244 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3245 if (signal_pending(current)) {
3246 break;
3248 schedule();
3250 pkt_dev->idle_acc += getCurUs() - idle_start;
3253 /* Done with this */
3254 pktgen_stop_device(pkt_dev);
3255 if (pkt_dev->skb)
3256 kfree_skb(pkt_dev->skb);
3257 pkt_dev->skb = NULL;
3259 out:;
3263 * Main loop of the thread goes here
3266 static int pktgen_thread_worker(void *arg)
3268 DEFINE_WAIT(wait);
3269 struct pktgen_thread *t = arg;
3270 struct pktgen_dev *pkt_dev = NULL;
3271 int cpu = t->cpu;
3272 u32 max_before_softirq;
3273 u32 tx_since_softirq = 0;
3275 BUG_ON(smp_processor_id() != cpu);
3277 init_waitqueue_head(&t->queue);
3279 t->pid = current->pid;
3281 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
3283 max_before_softirq = t->max_before_softirq;
3285 set_current_state(TASK_INTERRUPTIBLE);
3287 while (!kthread_should_stop()) {
3288 pkt_dev = next_to_run(t);
3290 if (!pkt_dev &&
3291 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3292 == 0) {
3293 prepare_to_wait(&(t->queue), &wait,
3294 TASK_INTERRUPTIBLE);
3295 schedule_timeout(HZ / 10);
3296 finish_wait(&(t->queue), &wait);
3299 __set_current_state(TASK_RUNNING);
3301 if (pkt_dev) {
3303 pktgen_xmit(pkt_dev);
3306 * We like to stay RUNNING but must also give
3307 * others fair share.
3310 tx_since_softirq += pkt_dev->last_ok;
3312 if (tx_since_softirq > max_before_softirq) {
3313 if (local_softirq_pending())
3314 do_softirq();
3315 tx_since_softirq = 0;
3319 if (t->control & T_STOP) {
3320 pktgen_stop(t);
3321 t->control &= ~(T_STOP);
3324 if (t->control & T_RUN) {
3325 pktgen_run(t);
3326 t->control &= ~(T_RUN);
3329 if (t->control & T_REMDEVALL) {
3330 pktgen_rem_all_ifs(t);
3331 t->control &= ~(T_REMDEVALL);
3334 if (t->control & T_REMDEV) {
3335 pktgen_rem_one_if(t);
3336 t->control &= ~(T_REMDEV);
3339 try_to_freeze();
3341 set_current_state(TASK_INTERRUPTIBLE);
3344 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3345 pktgen_stop(t);
3347 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3348 pktgen_rem_all_ifs(t);
3350 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3351 pktgen_rem_thread(t);
3353 return 0;
3356 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3357 const char *ifname)
3359 struct pktgen_dev *p, *pkt_dev = NULL;
3360 if_lock(t);
3362 list_for_each_entry(p, &t->if_list, list)
3363 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3364 pkt_dev = p;
3365 break;
3368 if_unlock(t);
3369 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3370 return pkt_dev;
3374 * Adds a dev at front of if_list.
3377 static int add_dev_to_thread(struct pktgen_thread *t,
3378 struct pktgen_dev *pkt_dev)
3380 int rv = 0;
3382 if_lock(t);
3384 if (pkt_dev->pg_thread) {
3385 printk("pktgen: ERROR: already assigned to a thread.\n");
3386 rv = -EBUSY;
3387 goto out;
3390 list_add(&pkt_dev->list, &t->if_list);
3391 pkt_dev->pg_thread = t;
3392 pkt_dev->running = 0;
3394 out:
3395 if_unlock(t);
3396 return rv;
3399 /* Called under thread lock */
3401 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3403 struct pktgen_dev *pkt_dev;
3404 int err;
3406 /* We don't allow a device to be on several threads */
3408 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3409 if (pkt_dev) {
3410 printk("pktgen: ERROR: interface already used.\n");
3411 return -EBUSY;
3414 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3415 if (!pkt_dev)
3416 return -ENOMEM;
3418 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3419 if (pkt_dev->flows == NULL) {
3420 kfree(pkt_dev);
3421 return -ENOMEM;
3423 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3425 pkt_dev->removal_mark = 0;
3426 pkt_dev->min_pkt_size = ETH_ZLEN;
3427 pkt_dev->max_pkt_size = ETH_ZLEN;
3428 pkt_dev->nfrags = 0;
3429 pkt_dev->clone_skb = pg_clone_skb_d;
3430 pkt_dev->delay_us = pg_delay_d / 1000;
3431 pkt_dev->delay_ns = pg_delay_d % 1000;
3432 pkt_dev->count = pg_count_d;
3433 pkt_dev->sofar = 0;
3434 pkt_dev->udp_src_min = 9; /* sink port */
3435 pkt_dev->udp_src_max = 9;
3436 pkt_dev->udp_dst_min = 9;
3437 pkt_dev->udp_dst_max = 9;
3439 pkt_dev->vlan_p = 0;
3440 pkt_dev->vlan_cfi = 0;
3441 pkt_dev->vlan_id = 0xffff;
3442 pkt_dev->svlan_p = 0;
3443 pkt_dev->svlan_cfi = 0;
3444 pkt_dev->svlan_id = 0xffff;
3446 err = pktgen_setup_dev(pkt_dev, ifname);
3447 if (err)
3448 goto out1;
3450 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3451 if (!pkt_dev->entry) {
3452 printk("pktgen: cannot create %s/%s procfs entry.\n",
3453 PG_PROC_DIR, ifname);
3454 err = -EINVAL;
3455 goto out2;
3457 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3458 pkt_dev->entry->data = pkt_dev;
3460 return add_dev_to_thread(t, pkt_dev);
3461 out2:
3462 dev_put(pkt_dev->odev);
3463 out1:
3464 if (pkt_dev->flows)
3465 vfree(pkt_dev->flows);
3466 kfree(pkt_dev);
3467 return err;
3470 static int __init pktgen_create_thread(int cpu)
3472 struct pktgen_thread *t;
3473 struct proc_dir_entry *pe;
3474 struct task_struct *p;
3476 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3477 if (!t) {
3478 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3479 return -ENOMEM;
3482 spin_lock_init(&t->if_lock);
3483 t->cpu = cpu;
3485 INIT_LIST_HEAD(&t->if_list);
3487 list_add_tail(&t->th_list, &pktgen_threads);
3489 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3490 if (IS_ERR(p)) {
3491 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3492 list_del(&t->th_list);
3493 kfree(t);
3494 return PTR_ERR(p);
3496 kthread_bind(p, cpu);
3497 t->tsk = p;
3499 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3500 if (!pe) {
3501 printk("pktgen: cannot create %s/%s procfs entry.\n",
3502 PG_PROC_DIR, t->tsk->comm);
3503 kthread_stop(p);
3504 list_del(&t->th_list);
3505 kfree(t);
3506 return -EINVAL;
3509 pe->proc_fops = &pktgen_thread_fops;
3510 pe->data = t;
3512 wake_up_process(p);
3514 return 0;
3518 * Removes a device from the thread if_list.
3520 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3521 struct pktgen_dev *pkt_dev)
3523 struct list_head *q, *n;
3524 struct pktgen_dev *p;
3526 list_for_each_safe(q, n, &t->if_list) {
3527 p = list_entry(q, struct pktgen_dev, list);
3528 if (p == pkt_dev)
3529 list_del(&p->list);
3533 static int pktgen_remove_device(struct pktgen_thread *t,
3534 struct pktgen_dev *pkt_dev)
3537 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3539 if (pkt_dev->running) {
3540 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3541 pktgen_stop_device(pkt_dev);
3544 /* Dis-associate from the interface */
3546 if (pkt_dev->odev) {
3547 dev_put(pkt_dev->odev);
3548 pkt_dev->odev = NULL;
3551 /* And update the thread if_list */
3553 _rem_dev_from_if_list(t, pkt_dev);
3555 if (pkt_dev->entry)
3556 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3558 if (pkt_dev->flows)
3559 vfree(pkt_dev->flows);
3560 kfree(pkt_dev);
3561 return 0;
3564 static int __init pg_init(void)
3566 int cpu;
3567 struct proc_dir_entry *pe;
3569 printk(version);
3571 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3572 if (!pg_proc_dir)
3573 return -ENODEV;
3574 pg_proc_dir->owner = THIS_MODULE;
3576 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3577 if (pe == NULL) {
3578 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3579 PGCTRL);
3580 proc_net_remove(PG_PROC_DIR);
3581 return -EINVAL;
3584 pe->proc_fops = &pktgen_fops;
3585 pe->data = NULL;
3587 /* Register us to receive netdevice events */
3588 register_netdevice_notifier(&pktgen_notifier_block);
3590 for_each_online_cpu(cpu) {
3591 int err;
3593 err = pktgen_create_thread(cpu);
3594 if (err)
3595 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3596 cpu, err);
3599 if (list_empty(&pktgen_threads)) {
3600 printk("pktgen: ERROR: Initialization failed for all threads\n");
3601 unregister_netdevice_notifier(&pktgen_notifier_block);
3602 remove_proc_entry(PGCTRL, pg_proc_dir);
3603 proc_net_remove(PG_PROC_DIR);
3604 return -ENODEV;
3607 return 0;
3610 static void __exit pg_cleanup(void)
3612 struct pktgen_thread *t;
3613 struct list_head *q, *n;
3614 wait_queue_head_t queue;
3615 init_waitqueue_head(&queue);
3617 /* Stop all interfaces & threads */
3619 list_for_each_safe(q, n, &pktgen_threads) {
3620 t = list_entry(q, struct pktgen_thread, th_list);
3621 kthread_stop(t->tsk);
3622 kfree(t);
3625 /* Un-register us from receiving netdevice events */
3626 unregister_netdevice_notifier(&pktgen_notifier_block);
3628 /* Clean up proc file system */
3629 remove_proc_entry(PGCTRL, pg_proc_dir);
3630 proc_net_remove(PG_PROC_DIR);
3633 module_init(pg_init);
3634 module_exit(pg_cleanup);
3636 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3637 MODULE_DESCRIPTION("Packet Generator tool");
3638 MODULE_LICENSE("GPL");
3639 module_param(pg_count_d, int, 0);
3640 module_param(pg_delay_d, int, 0);
3641 module_param(pg_clone_skb_d, int, 0);
3642 module_param(debug, int, 0);