pktgen: cleanup checkpatch warnings
[linux-2.6/x86.git] / net / core / pktgen.c
blob51b1e61d3029e9d0b5898dfe5a1993cde2fb1fa8
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/hrtimer.h>
135 #include <linux/freezer.h>
136 #include <linux/delay.h>
137 #include <linux/timer.h>
138 #include <linux/list.h>
139 #include <linux/init.h>
140 #include <linux/skbuff.h>
141 #include <linux/netdevice.h>
142 #include <linux/inet.h>
143 #include <linux/inetdevice.h>
144 #include <linux/rtnetlink.h>
145 #include <linux/if_arp.h>
146 #include <linux/if_vlan.h>
147 #include <linux/in.h>
148 #include <linux/ip.h>
149 #include <linux/ipv6.h>
150 #include <linux/udp.h>
151 #include <linux/proc_fs.h>
152 #include <linux/seq_file.h>
153 #include <linux/wait.h>
154 #include <linux/etherdevice.h>
155 #include <linux/kthread.h>
156 #include <net/net_namespace.h>
157 #include <net/checksum.h>
158 #include <net/ipv6.h>
159 #include <net/addrconf.h>
160 #ifdef CONFIG_XFRM
161 #include <net/xfrm.h>
162 #endif
163 #include <asm/byteorder.h>
164 #include <linux/rcupdate.h>
165 #include <linux/bitops.h>
166 #include <linux/io.h>
167 #include <linux/timex.h>
168 #include <linux/uaccess.h>
169 #include <asm/dma.h>
170 #include <asm/div64.h> /* do_div */
172 #define VERSION \
173 "pktgen v2.70: Packet Generator for packet performance testing.\n"
175 #define IP_NAME_SZ 32
176 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
177 #define MPLS_STACK_BOTTOM htonl(0x00000100)
179 /* Device flag bits */
180 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
181 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
182 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
183 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
184 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
185 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
186 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
187 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
188 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
189 #define F_VID_RND (1<<9) /* Random VLAN ID */
190 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
191 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
192 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
193 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
194 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
196 /* Thread control flag bits */
197 #define T_TERMINATE (1<<0)
198 #define T_STOP (1<<1) /* Stop run */
199 #define T_RUN (1<<2) /* Start run */
200 #define T_REMDEVALL (1<<3) /* Remove all devs */
201 #define T_REMDEV (1<<4) /* Remove one dev */
203 /* If lock -- can be removed after some work */
204 #define if_lock(t) spin_lock(&(t->if_lock));
205 #define if_unlock(t) spin_unlock(&(t->if_lock));
207 /* Used to help with determining the pkts on receive */
208 #define PKTGEN_MAGIC 0xbe9be955
209 #define PG_PROC_DIR "pktgen"
210 #define PGCTRL "pgctrl"
211 static struct proc_dir_entry *pg_proc_dir;
213 #define MAX_CFLOWS 65536
215 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
216 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
218 struct flow_state {
219 __be32 cur_daddr;
220 int count;
221 #ifdef CONFIG_XFRM
222 struct xfrm_state *x;
223 #endif
224 __u32 flags;
227 /* flow flag bits */
228 #define F_INIT (1<<0) /* flow has been initialized */
230 struct pktgen_dev {
232 * Try to keep frequent/infrequent used vars. separated.
234 struct proc_dir_entry *entry; /* proc file */
235 struct pktgen_thread *pg_thread;/* the owner */
236 struct list_head list; /* chaining in the thread's run-queue */
238 int running; /* if false, the test will stop */
240 /* If min != max, then we will either do a linear iteration, or
241 * we will do a random selection from within the range.
243 __u32 flags;
244 int removal_mark; /* non-zero => the device is marked for
245 * removal by worker thread */
247 int min_pkt_size; /* = ETH_ZLEN; */
248 int max_pkt_size; /* = ETH_ZLEN; */
249 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
250 int nfrags;
251 u64 delay; /* nano-seconds */
253 __u64 count; /* Default No packets to send */
254 __u64 sofar; /* How many pkts we've sent so far */
255 __u64 tx_bytes; /* How many bytes we've transmitted */
256 __u64 errors; /* Errors when trying to transmit,
257 pkts will be re-sent */
259 /* runtime counters relating to clone_skb */
261 __u64 allocated_skbs;
262 __u32 clone_count;
263 int last_ok; /* Was last skb sent?
264 * Or a failed transmit of some sort?
265 * This will keep sequence numbers in order
267 ktime_t next_tx;
268 ktime_t started_at;
269 ktime_t stopped_at;
270 u64 idle_acc; /* nano-seconds */
272 __u32 seq_num;
274 int clone_skb; /*
275 * Use multiple SKBs during packet gen.
276 * If this number is greater than 1, then
277 * that many copies of the same packet will be
278 * sent before a new packet is allocated.
279 * If you want to send 1024 identical packets
280 * before creating a new packet,
281 * set clone_skb to 1024.
284 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
285 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
286 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
287 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 struct in6_addr in6_saddr;
290 struct in6_addr in6_daddr;
291 struct in6_addr cur_in6_daddr;
292 struct in6_addr cur_in6_saddr;
293 /* For ranges */
294 struct in6_addr min_in6_daddr;
295 struct in6_addr max_in6_daddr;
296 struct in6_addr min_in6_saddr;
297 struct in6_addr max_in6_saddr;
299 /* If we're doing ranges, random or incremental, then this
300 * defines the min/max for those ranges.
302 __be32 saddr_min; /* inclusive, source IP address */
303 __be32 saddr_max; /* exclusive, source IP address */
304 __be32 daddr_min; /* inclusive, dest IP address */
305 __be32 daddr_max; /* exclusive, dest IP address */
307 __u16 udp_src_min; /* inclusive, source UDP port */
308 __u16 udp_src_max; /* exclusive, source UDP port */
309 __u16 udp_dst_min; /* inclusive, dest UDP port */
310 __u16 udp_dst_max; /* exclusive, dest UDP port */
312 /* DSCP + ECN */
313 __u8 tos; /* six MSB of (former) IPv4 TOS
314 are for dscp codepoint */
315 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
316 (see RFC 3260, sec. 4) */
318 /* MPLS */
319 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
320 __be32 labels[MAX_MPLS_LABELS];
322 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
323 __u8 vlan_p;
324 __u8 vlan_cfi;
325 __u16 vlan_id; /* 0xffff means no vlan tag */
327 __u8 svlan_p;
328 __u8 svlan_cfi;
329 __u16 svlan_id; /* 0xffff means no svlan tag */
331 __u32 src_mac_count; /* How many MACs to iterate through */
332 __u32 dst_mac_count; /* How many MACs to iterate through */
334 unsigned char dst_mac[ETH_ALEN];
335 unsigned char src_mac[ETH_ALEN];
337 __u32 cur_dst_mac_offset;
338 __u32 cur_src_mac_offset;
339 __be32 cur_saddr;
340 __be32 cur_daddr;
341 __u16 cur_udp_dst;
342 __u16 cur_udp_src;
343 __u16 cur_queue_map;
344 __u32 cur_pkt_size;
346 __u8 hh[14];
347 /* = {
348 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
350 We fill in SRC address later
351 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
352 0x08, 0x00
355 __u16 pad; /* pad out the hh struct to an even 16 bytes */
357 struct sk_buff *skb; /* skb we are to transmit next, used for when we
358 * are transmitting the same one multiple times
360 struct net_device *odev; /* The out-going device.
361 * Note that the device should have it's
362 * pg_info pointer pointing back to this
363 * device.
364 * Set when the user specifies the out-going
365 * device name (not when the inject is
366 * started as it used to do.)
368 struct flow_state *flows;
369 unsigned cflows; /* Concurrent flows (config) */
370 unsigned lflow; /* Flow length (config) */
371 unsigned nflows; /* accumulated flows (stats) */
372 unsigned curfl; /* current sequenced flow (state)*/
374 u16 queue_map_min;
375 u16 queue_map_max;
377 #ifdef CONFIG_XFRM
378 __u8 ipsmode; /* IPSEC mode (config) */
379 __u8 ipsproto; /* IPSEC type (config) */
380 #endif
381 char result[512];
384 struct pktgen_hdr {
385 __be32 pgh_magic;
386 __be32 seq_num;
387 __be32 tv_sec;
388 __be32 tv_usec;
391 struct pktgen_thread {
392 spinlock_t if_lock; /* for list of devices */
393 struct list_head if_list; /* All device here */
394 struct list_head th_list;
395 struct task_struct *tsk;
396 char result[512];
398 /* Field for thread to receive "posted" events terminate,
399 stop ifs etc. */
401 u32 control;
402 int cpu;
404 wait_queue_head_t queue;
405 struct completion start_done;
408 #define REMOVE 1
409 #define FIND 0
411 static inline ktime_t ktime_now(void)
413 struct timespec ts;
414 ktime_get_ts(&ts);
416 return timespec_to_ktime(ts);
419 /* This works even if 32 bit because of careful byte order choice */
420 static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
422 return cmp1.tv64 < cmp2.tv64;
425 static const char version[] __initconst = VERSION;
427 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
428 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
429 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
430 const char *ifname);
431 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
432 static void pktgen_run_all_threads(void);
433 static void pktgen_reset_all_threads(void);
434 static void pktgen_stop_all_threads_ifs(void);
436 static void pktgen_stop(struct pktgen_thread *t);
437 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
439 static unsigned int scan_ip6(const char *s, char ip[16]);
440 static unsigned int fmt_ip6(char *s, const char ip[16]);
442 /* Module parameters, defaults. */
443 static int pg_count_d __read_mostly = 1000;
444 static int pg_delay_d __read_mostly;
445 static int pg_clone_skb_d __read_mostly;
446 static int debug __read_mostly;
448 static DEFINE_MUTEX(pktgen_thread_lock);
449 static LIST_HEAD(pktgen_threads);
451 static struct notifier_block pktgen_notifier_block = {
452 .notifier_call = pktgen_device_event,
456 * /proc handling functions
460 static int pgctrl_show(struct seq_file *seq, void *v)
462 seq_puts(seq, VERSION);
463 return 0;
466 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
467 size_t count, loff_t *ppos)
469 int err = 0;
470 char data[128];
472 if (!capable(CAP_NET_ADMIN)) {
473 err = -EPERM;
474 goto out;
477 if (count > sizeof(data))
478 count = sizeof(data);
480 if (copy_from_user(data, buf, count)) {
481 err = -EFAULT;
482 goto out;
484 data[count - 1] = 0; /* Make string */
486 if (!strcmp(data, "stop"))
487 pktgen_stop_all_threads_ifs();
489 else if (!strcmp(data, "start"))
490 pktgen_run_all_threads();
492 else if (!strcmp(data, "reset"))
493 pktgen_reset_all_threads();
495 else
496 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
498 err = count;
500 out:
501 return err;
504 static int pgctrl_open(struct inode *inode, struct file *file)
506 return single_open(file, pgctrl_show, PDE(inode)->data);
509 static const struct file_operations pktgen_fops = {
510 .owner = THIS_MODULE,
511 .open = pgctrl_open,
512 .read = seq_read,
513 .llseek = seq_lseek,
514 .write = pgctrl_write,
515 .release = single_release,
518 static int pktgen_if_show(struct seq_file *seq, void *v)
520 const struct pktgen_dev *pkt_dev = seq->private;
521 ktime_t stopped;
522 u64 idle;
524 seq_printf(seq,
525 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
526 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
527 pkt_dev->max_pkt_size);
529 seq_printf(seq,
530 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
531 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
532 pkt_dev->clone_skb, pkt_dev->odev->name);
534 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
535 pkt_dev->lflow);
537 seq_printf(seq,
538 " queue_map_min: %u queue_map_max: %u\n",
539 pkt_dev->queue_map_min,
540 pkt_dev->queue_map_max);
542 if (pkt_dev->flags & F_IPV6) {
543 char b1[128], b2[128], b3[128];
544 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
545 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
546 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
547 seq_printf(seq,
548 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
549 b2, b3);
551 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
552 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
553 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
554 seq_printf(seq,
555 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
556 b2, b3);
558 } else {
559 seq_printf(seq,
560 " dst_min: %s dst_max: %s\n",
561 pkt_dev->dst_min, pkt_dev->dst_max);
562 seq_printf(seq,
563 " src_min: %s src_max: %s\n",
564 pkt_dev->src_min, pkt_dev->src_max);
567 seq_puts(seq, " src_mac: ");
569 seq_printf(seq, "%pM ",
570 is_zero_ether_addr(pkt_dev->src_mac) ?
571 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
573 seq_printf(seq, "dst_mac: ");
574 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
576 seq_printf(seq,
577 " udp_src_min: %d udp_src_max: %d"
578 " udp_dst_min: %d udp_dst_max: %d\n",
579 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
580 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
582 seq_printf(seq,
583 " src_mac_count: %d dst_mac_count: %d\n",
584 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
586 if (pkt_dev->nr_labels) {
587 unsigned i;
588 seq_printf(seq, " mpls: ");
589 for (i = 0; i < pkt_dev->nr_labels; i++)
590 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
591 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
594 if (pkt_dev->vlan_id != 0xffff)
595 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
596 pkt_dev->vlan_id, pkt_dev->vlan_p,
597 pkt_dev->vlan_cfi);
599 if (pkt_dev->svlan_id != 0xffff)
600 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
601 pkt_dev->svlan_id, pkt_dev->svlan_p,
602 pkt_dev->svlan_cfi);
604 if (pkt_dev->tos)
605 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
607 if (pkt_dev->traffic_class)
608 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
610 seq_printf(seq, " Flags: ");
612 if (pkt_dev->flags & F_IPV6)
613 seq_printf(seq, "IPV6 ");
615 if (pkt_dev->flags & F_IPSRC_RND)
616 seq_printf(seq, "IPSRC_RND ");
618 if (pkt_dev->flags & F_IPDST_RND)
619 seq_printf(seq, "IPDST_RND ");
621 if (pkt_dev->flags & F_TXSIZE_RND)
622 seq_printf(seq, "TXSIZE_RND ");
624 if (pkt_dev->flags & F_UDPSRC_RND)
625 seq_printf(seq, "UDPSRC_RND ");
627 if (pkt_dev->flags & F_UDPDST_RND)
628 seq_printf(seq, "UDPDST_RND ");
630 if (pkt_dev->flags & F_MPLS_RND)
631 seq_printf(seq, "MPLS_RND ");
633 if (pkt_dev->flags & F_QUEUE_MAP_RND)
634 seq_printf(seq, "QUEUE_MAP_RND ");
636 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
637 seq_printf(seq, "QUEUE_MAP_CPU ");
639 if (pkt_dev->cflows) {
640 if (pkt_dev->flags & F_FLOW_SEQ)
641 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
642 else
643 seq_printf(seq, "FLOW_RND ");
646 #ifdef CONFIG_XFRM
647 if (pkt_dev->flags & F_IPSEC_ON)
648 seq_printf(seq, "IPSEC ");
649 #endif
651 if (pkt_dev->flags & F_MACSRC_RND)
652 seq_printf(seq, "MACSRC_RND ");
654 if (pkt_dev->flags & F_MACDST_RND)
655 seq_printf(seq, "MACDST_RND ");
657 if (pkt_dev->flags & F_VID_RND)
658 seq_printf(seq, "VID_RND ");
660 if (pkt_dev->flags & F_SVID_RND)
661 seq_printf(seq, "SVID_RND ");
663 seq_puts(seq, "\n");
665 /* not really stopped, more like last-running-at */
666 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
667 idle = pkt_dev->idle_acc;
668 do_div(idle, NSEC_PER_USEC);
670 seq_printf(seq,
671 "Current:\n pkts-sofar: %llu errors: %llu\n",
672 (unsigned long long)pkt_dev->sofar,
673 (unsigned long long)pkt_dev->errors);
675 seq_printf(seq,
676 " started: %lluus stopped: %lluus idle: %lluus\n",
677 (unsigned long long) ktime_to_us(pkt_dev->started_at),
678 (unsigned long long) ktime_to_us(stopped),
679 (unsigned long long) idle);
681 seq_printf(seq,
682 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
683 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
684 pkt_dev->cur_src_mac_offset);
686 if (pkt_dev->flags & F_IPV6) {
687 char b1[128], b2[128];
688 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
689 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
690 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
691 } else
692 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
693 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
695 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
696 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
698 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
700 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
702 if (pkt_dev->result[0])
703 seq_printf(seq, "Result: %s\n", pkt_dev->result);
704 else
705 seq_printf(seq, "Result: Idle\n");
707 return 0;
711 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
712 __u32 *num)
714 int i = 0;
715 *num = 0;
717 for (; i < maxlen; i++) {
718 char c;
719 *num <<= 4;
720 if (get_user(c, &user_buffer[i]))
721 return -EFAULT;
722 if ((c >= '0') && (c <= '9'))
723 *num |= c - '0';
724 else if ((c >= 'a') && (c <= 'f'))
725 *num |= c - 'a' + 10;
726 else if ((c >= 'A') && (c <= 'F'))
727 *num |= c - 'A' + 10;
728 else
729 break;
731 return i;
734 static int count_trail_chars(const char __user * user_buffer,
735 unsigned int maxlen)
737 int i;
739 for (i = 0; i < maxlen; i++) {
740 char c;
741 if (get_user(c, &user_buffer[i]))
742 return -EFAULT;
743 switch (c) {
744 case '\"':
745 case '\n':
746 case '\r':
747 case '\t':
748 case ' ':
749 case '=':
750 break;
751 default:
752 goto done;
755 done:
756 return i;
759 static unsigned long num_arg(const char __user * user_buffer,
760 unsigned long maxlen, unsigned long *num)
762 int i = 0;
763 *num = 0;
765 for (; i < maxlen; i++) {
766 char c;
767 if (get_user(c, &user_buffer[i]))
768 return -EFAULT;
769 if ((c >= '0') && (c <= '9')) {
770 *num *= 10;
771 *num += c - '0';
772 } else
773 break;
775 return i;
778 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
780 int i = 0;
782 for (; i < maxlen; i++) {
783 char c;
784 if (get_user(c, &user_buffer[i]))
785 return -EFAULT;
786 switch (c) {
787 case '\"':
788 case '\n':
789 case '\r':
790 case '\t':
791 case ' ':
792 goto done_str;
793 break;
794 default:
795 break;
798 done_str:
799 return i;
802 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
804 unsigned n = 0;
805 char c;
806 ssize_t i = 0;
807 int len;
809 pkt_dev->nr_labels = 0;
810 do {
811 __u32 tmp;
812 len = hex32_arg(&buffer[i], 8, &tmp);
813 if (len <= 0)
814 return len;
815 pkt_dev->labels[n] = htonl(tmp);
816 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
817 pkt_dev->flags |= F_MPLS_RND;
818 i += len;
819 if (get_user(c, &buffer[i]))
820 return -EFAULT;
821 i++;
822 n++;
823 if (n >= MAX_MPLS_LABELS)
824 return -E2BIG;
825 } while (c == ',');
827 pkt_dev->nr_labels = n;
828 return i;
831 static ssize_t pktgen_if_write(struct file *file,
832 const char __user * user_buffer, size_t count,
833 loff_t * offset)
835 struct seq_file *seq = (struct seq_file *)file->private_data;
836 struct pktgen_dev *pkt_dev = seq->private;
837 int i = 0, max, len;
838 char name[16], valstr[32];
839 unsigned long value = 0;
840 char *pg_result = NULL;
841 int tmp = 0;
842 char buf[128];
844 pg_result = &(pkt_dev->result[0]);
846 if (count < 1) {
847 printk(KERN_WARNING "pktgen: wrong command format\n");
848 return -EINVAL;
851 max = count - i;
852 tmp = count_trail_chars(&user_buffer[i], max);
853 if (tmp < 0) {
854 printk(KERN_WARNING "pktgen: illegal format\n");
855 return tmp;
857 i += tmp;
859 /* Read variable name */
861 len = strn_len(&user_buffer[i], sizeof(name) - 1);
862 if (len < 0)
863 return len;
865 memset(name, 0, sizeof(name));
866 if (copy_from_user(name, &user_buffer[i], len))
867 return -EFAULT;
868 i += len;
870 max = count - i;
871 len = count_trail_chars(&user_buffer[i], max);
872 if (len < 0)
873 return len;
875 i += len;
877 if (debug) {
878 char tb[count + 1];
879 if (copy_from_user(tb, user_buffer, count))
880 return -EFAULT;
881 tb[count] = 0;
882 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
883 (unsigned long)count, tb);
886 if (!strcmp(name, "min_pkt_size")) {
887 len = num_arg(&user_buffer[i], 10, &value);
888 if (len < 0)
889 return len;
891 i += len;
892 if (value < 14 + 20 + 8)
893 value = 14 + 20 + 8;
894 if (value != pkt_dev->min_pkt_size) {
895 pkt_dev->min_pkt_size = value;
896 pkt_dev->cur_pkt_size = value;
898 sprintf(pg_result, "OK: min_pkt_size=%u",
899 pkt_dev->min_pkt_size);
900 return count;
903 if (!strcmp(name, "max_pkt_size")) {
904 len = num_arg(&user_buffer[i], 10, &value);
905 if (len < 0)
906 return len;
908 i += len;
909 if (value < 14 + 20 + 8)
910 value = 14 + 20 + 8;
911 if (value != pkt_dev->max_pkt_size) {
912 pkt_dev->max_pkt_size = value;
913 pkt_dev->cur_pkt_size = value;
915 sprintf(pg_result, "OK: max_pkt_size=%u",
916 pkt_dev->max_pkt_size);
917 return count;
920 /* Shortcut for min = max */
922 if (!strcmp(name, "pkt_size")) {
923 len = num_arg(&user_buffer[i], 10, &value);
924 if (len < 0)
925 return len;
927 i += len;
928 if (value < 14 + 20 + 8)
929 value = 14 + 20 + 8;
930 if (value != pkt_dev->min_pkt_size) {
931 pkt_dev->min_pkt_size = value;
932 pkt_dev->max_pkt_size = value;
933 pkt_dev->cur_pkt_size = value;
935 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
936 return count;
939 if (!strcmp(name, "debug")) {
940 len = num_arg(&user_buffer[i], 10, &value);
941 if (len < 0)
942 return len;
944 i += len;
945 debug = value;
946 sprintf(pg_result, "OK: debug=%u", debug);
947 return count;
950 if (!strcmp(name, "frags")) {
951 len = num_arg(&user_buffer[i], 10, &value);
952 if (len < 0)
953 return len;
955 i += len;
956 pkt_dev->nfrags = value;
957 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
958 return count;
960 if (!strcmp(name, "delay")) {
961 len = num_arg(&user_buffer[i], 10, &value);
962 if (len < 0)
963 return len;
965 i += len;
966 if (value == 0x7FFFFFFF)
967 pkt_dev->delay = ULLONG_MAX;
968 else
969 pkt_dev->delay = (u64)value * NSEC_PER_USEC;
971 sprintf(pg_result, "OK: delay=%llu",
972 (unsigned long long) pkt_dev->delay);
973 return count;
975 if (!strcmp(name, "udp_src_min")) {
976 len = num_arg(&user_buffer[i], 10, &value);
977 if (len < 0)
978 return len;
980 i += len;
981 if (value != pkt_dev->udp_src_min) {
982 pkt_dev->udp_src_min = value;
983 pkt_dev->cur_udp_src = value;
985 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
986 return count;
988 if (!strcmp(name, "udp_dst_min")) {
989 len = num_arg(&user_buffer[i], 10, &value);
990 if (len < 0)
991 return len;
993 i += len;
994 if (value != pkt_dev->udp_dst_min) {
995 pkt_dev->udp_dst_min = value;
996 pkt_dev->cur_udp_dst = value;
998 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
999 return count;
1001 if (!strcmp(name, "udp_src_max")) {
1002 len = num_arg(&user_buffer[i], 10, &value);
1003 if (len < 0)
1004 return len;
1006 i += len;
1007 if (value != pkt_dev->udp_src_max) {
1008 pkt_dev->udp_src_max = value;
1009 pkt_dev->cur_udp_src = value;
1011 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1012 return count;
1014 if (!strcmp(name, "udp_dst_max")) {
1015 len = num_arg(&user_buffer[i], 10, &value);
1016 if (len < 0)
1017 return len;
1019 i += len;
1020 if (value != pkt_dev->udp_dst_max) {
1021 pkt_dev->udp_dst_max = value;
1022 pkt_dev->cur_udp_dst = value;
1024 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1025 return count;
1027 if (!strcmp(name, "clone_skb")) {
1028 len = num_arg(&user_buffer[i], 10, &value);
1029 if (len < 0)
1030 return len;
1032 i += len;
1033 pkt_dev->clone_skb = value;
1035 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1036 return count;
1038 if (!strcmp(name, "count")) {
1039 len = num_arg(&user_buffer[i], 10, &value);
1040 if (len < 0)
1041 return len;
1043 i += len;
1044 pkt_dev->count = value;
1045 sprintf(pg_result, "OK: count=%llu",
1046 (unsigned long long)pkt_dev->count);
1047 return count;
1049 if (!strcmp(name, "src_mac_count")) {
1050 len = num_arg(&user_buffer[i], 10, &value);
1051 if (len < 0)
1052 return len;
1054 i += len;
1055 if (pkt_dev->src_mac_count != value) {
1056 pkt_dev->src_mac_count = value;
1057 pkt_dev->cur_src_mac_offset = 0;
1059 sprintf(pg_result, "OK: src_mac_count=%d",
1060 pkt_dev->src_mac_count);
1061 return count;
1063 if (!strcmp(name, "dst_mac_count")) {
1064 len = num_arg(&user_buffer[i], 10, &value);
1065 if (len < 0)
1066 return len;
1068 i += len;
1069 if (pkt_dev->dst_mac_count != value) {
1070 pkt_dev->dst_mac_count = value;
1071 pkt_dev->cur_dst_mac_offset = 0;
1073 sprintf(pg_result, "OK: dst_mac_count=%d",
1074 pkt_dev->dst_mac_count);
1075 return count;
1077 if (!strcmp(name, "flag")) {
1078 char f[32];
1079 memset(f, 0, 32);
1080 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1081 if (len < 0)
1082 return len;
1084 if (copy_from_user(f, &user_buffer[i], len))
1085 return -EFAULT;
1086 i += len;
1087 if (strcmp(f, "IPSRC_RND") == 0)
1088 pkt_dev->flags |= F_IPSRC_RND;
1090 else if (strcmp(f, "!IPSRC_RND") == 0)
1091 pkt_dev->flags &= ~F_IPSRC_RND;
1093 else if (strcmp(f, "TXSIZE_RND") == 0)
1094 pkt_dev->flags |= F_TXSIZE_RND;
1096 else if (strcmp(f, "!TXSIZE_RND") == 0)
1097 pkt_dev->flags &= ~F_TXSIZE_RND;
1099 else if (strcmp(f, "IPDST_RND") == 0)
1100 pkt_dev->flags |= F_IPDST_RND;
1102 else if (strcmp(f, "!IPDST_RND") == 0)
1103 pkt_dev->flags &= ~F_IPDST_RND;
1105 else if (strcmp(f, "UDPSRC_RND") == 0)
1106 pkt_dev->flags |= F_UDPSRC_RND;
1108 else if (strcmp(f, "!UDPSRC_RND") == 0)
1109 pkt_dev->flags &= ~F_UDPSRC_RND;
1111 else if (strcmp(f, "UDPDST_RND") == 0)
1112 pkt_dev->flags |= F_UDPDST_RND;
1114 else if (strcmp(f, "!UDPDST_RND") == 0)
1115 pkt_dev->flags &= ~F_UDPDST_RND;
1117 else if (strcmp(f, "MACSRC_RND") == 0)
1118 pkt_dev->flags |= F_MACSRC_RND;
1120 else if (strcmp(f, "!MACSRC_RND") == 0)
1121 pkt_dev->flags &= ~F_MACSRC_RND;
1123 else if (strcmp(f, "MACDST_RND") == 0)
1124 pkt_dev->flags |= F_MACDST_RND;
1126 else if (strcmp(f, "!MACDST_RND") == 0)
1127 pkt_dev->flags &= ~F_MACDST_RND;
1129 else if (strcmp(f, "MPLS_RND") == 0)
1130 pkt_dev->flags |= F_MPLS_RND;
1132 else if (strcmp(f, "!MPLS_RND") == 0)
1133 pkt_dev->flags &= ~F_MPLS_RND;
1135 else if (strcmp(f, "VID_RND") == 0)
1136 pkt_dev->flags |= F_VID_RND;
1138 else if (strcmp(f, "!VID_RND") == 0)
1139 pkt_dev->flags &= ~F_VID_RND;
1141 else if (strcmp(f, "SVID_RND") == 0)
1142 pkt_dev->flags |= F_SVID_RND;
1144 else if (strcmp(f, "!SVID_RND") == 0)
1145 pkt_dev->flags &= ~F_SVID_RND;
1147 else if (strcmp(f, "FLOW_SEQ") == 0)
1148 pkt_dev->flags |= F_FLOW_SEQ;
1150 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1151 pkt_dev->flags |= F_QUEUE_MAP_RND;
1153 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1154 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1156 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1157 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1159 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1160 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1161 #ifdef CONFIG_XFRM
1162 else if (strcmp(f, "IPSEC") == 0)
1163 pkt_dev->flags |= F_IPSEC_ON;
1164 #endif
1166 else if (strcmp(f, "!IPV6") == 0)
1167 pkt_dev->flags &= ~F_IPV6;
1169 else {
1170 sprintf(pg_result,
1171 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1173 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1174 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1175 return count;
1177 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1178 return count;
1180 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1181 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1182 if (len < 0)
1183 return len;
1185 if (copy_from_user(buf, &user_buffer[i], len))
1186 return -EFAULT;
1187 buf[len] = 0;
1188 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1189 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1190 strncpy(pkt_dev->dst_min, buf, len);
1191 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1192 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1194 if (debug)
1195 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1196 pkt_dev->dst_min);
1197 i += len;
1198 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1199 return count;
1201 if (!strcmp(name, "dst_max")) {
1202 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1203 if (len < 0)
1204 return len;
1207 if (copy_from_user(buf, &user_buffer[i], len))
1208 return -EFAULT;
1210 buf[len] = 0;
1211 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1212 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1213 strncpy(pkt_dev->dst_max, buf, len);
1214 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1215 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1217 if (debug)
1218 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1219 pkt_dev->dst_max);
1220 i += len;
1221 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1222 return count;
1224 if (!strcmp(name, "dst6")) {
1225 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1226 if (len < 0)
1227 return len;
1229 pkt_dev->flags |= F_IPV6;
1231 if (copy_from_user(buf, &user_buffer[i], len))
1232 return -EFAULT;
1233 buf[len] = 0;
1235 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1236 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1238 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1240 if (debug)
1241 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1243 i += len;
1244 sprintf(pg_result, "OK: dst6=%s", buf);
1245 return count;
1247 if (!strcmp(name, "dst6_min")) {
1248 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1249 if (len < 0)
1250 return len;
1252 pkt_dev->flags |= F_IPV6;
1254 if (copy_from_user(buf, &user_buffer[i], len))
1255 return -EFAULT;
1256 buf[len] = 0;
1258 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1259 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1261 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1262 &pkt_dev->min_in6_daddr);
1263 if (debug)
1264 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1266 i += len;
1267 sprintf(pg_result, "OK: dst6_min=%s", buf);
1268 return count;
1270 if (!strcmp(name, "dst6_max")) {
1271 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1272 if (len < 0)
1273 return len;
1275 pkt_dev->flags |= F_IPV6;
1277 if (copy_from_user(buf, &user_buffer[i], len))
1278 return -EFAULT;
1279 buf[len] = 0;
1281 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1282 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1284 if (debug)
1285 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1287 i += len;
1288 sprintf(pg_result, "OK: dst6_max=%s", buf);
1289 return count;
1291 if (!strcmp(name, "src6")) {
1292 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1293 if (len < 0)
1294 return len;
1296 pkt_dev->flags |= F_IPV6;
1298 if (copy_from_user(buf, &user_buffer[i], len))
1299 return -EFAULT;
1300 buf[len] = 0;
1302 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1303 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1305 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1307 if (debug)
1308 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1310 i += len;
1311 sprintf(pg_result, "OK: src6=%s", buf);
1312 return count;
1314 if (!strcmp(name, "src_min")) {
1315 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1316 if (len < 0)
1317 return len;
1319 if (copy_from_user(buf, &user_buffer[i], len))
1320 return -EFAULT;
1321 buf[len] = 0;
1322 if (strcmp(buf, pkt_dev->src_min) != 0) {
1323 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1324 strncpy(pkt_dev->src_min, buf, len);
1325 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1326 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1328 if (debug)
1329 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1330 pkt_dev->src_min);
1331 i += len;
1332 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1333 return count;
1335 if (!strcmp(name, "src_max")) {
1336 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1337 if (len < 0)
1338 return len;
1340 if (copy_from_user(buf, &user_buffer[i], len))
1341 return -EFAULT;
1342 buf[len] = 0;
1343 if (strcmp(buf, pkt_dev->src_max) != 0) {
1344 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1345 strncpy(pkt_dev->src_max, buf, len);
1346 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1347 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1349 if (debug)
1350 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1351 pkt_dev->src_max);
1352 i += len;
1353 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1354 return count;
1356 if (!strcmp(name, "dst_mac")) {
1357 char *v = valstr;
1358 unsigned char old_dmac[ETH_ALEN];
1359 unsigned char *m = pkt_dev->dst_mac;
1360 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1362 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1363 if (len < 0)
1364 return len;
1366 memset(valstr, 0, sizeof(valstr));
1367 if (copy_from_user(valstr, &user_buffer[i], len))
1368 return -EFAULT;
1369 i += len;
1371 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1372 if (*v >= '0' && *v <= '9') {
1373 *m *= 16;
1374 *m += *v - '0';
1376 if (*v >= 'A' && *v <= 'F') {
1377 *m *= 16;
1378 *m += *v - 'A' + 10;
1380 if (*v >= 'a' && *v <= 'f') {
1381 *m *= 16;
1382 *m += *v - 'a' + 10;
1384 if (*v == ':') {
1385 m++;
1386 *m = 0;
1390 /* Set up Dest MAC */
1391 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1392 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1394 sprintf(pg_result, "OK: dstmac");
1395 return count;
1397 if (!strcmp(name, "src_mac")) {
1398 char *v = valstr;
1399 unsigned char old_smac[ETH_ALEN];
1400 unsigned char *m = pkt_dev->src_mac;
1402 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1404 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1405 if (len < 0)
1406 return len;
1408 memset(valstr, 0, sizeof(valstr));
1409 if (copy_from_user(valstr, &user_buffer[i], len))
1410 return -EFAULT;
1411 i += len;
1413 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1414 if (*v >= '0' && *v <= '9') {
1415 *m *= 16;
1416 *m += *v - '0';
1418 if (*v >= 'A' && *v <= 'F') {
1419 *m *= 16;
1420 *m += *v - 'A' + 10;
1422 if (*v >= 'a' && *v <= 'f') {
1423 *m *= 16;
1424 *m += *v - 'a' + 10;
1426 if (*v == ':') {
1427 m++;
1428 *m = 0;
1432 /* Set up Src MAC */
1433 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1434 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1436 sprintf(pg_result, "OK: srcmac");
1437 return count;
1440 if (!strcmp(name, "clear_counters")) {
1441 pktgen_clear_counters(pkt_dev);
1442 sprintf(pg_result, "OK: Clearing counters.\n");
1443 return count;
1446 if (!strcmp(name, "flows")) {
1447 len = num_arg(&user_buffer[i], 10, &value);
1448 if (len < 0)
1449 return len;
1451 i += len;
1452 if (value > MAX_CFLOWS)
1453 value = MAX_CFLOWS;
1455 pkt_dev->cflows = value;
1456 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1457 return count;
1460 if (!strcmp(name, "flowlen")) {
1461 len = num_arg(&user_buffer[i], 10, &value);
1462 if (len < 0)
1463 return len;
1465 i += len;
1466 pkt_dev->lflow = value;
1467 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1468 return count;
1471 if (!strcmp(name, "queue_map_min")) {
1472 len = num_arg(&user_buffer[i], 5, &value);
1473 if (len < 0)
1474 return len;
1476 i += len;
1477 pkt_dev->queue_map_min = value;
1478 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1479 return count;
1482 if (!strcmp(name, "queue_map_max")) {
1483 len = num_arg(&user_buffer[i], 5, &value);
1484 if (len < 0)
1485 return len;
1487 i += len;
1488 pkt_dev->queue_map_max = value;
1489 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1490 return count;
1493 if (!strcmp(name, "mpls")) {
1494 unsigned n, cnt;
1496 len = get_labels(&user_buffer[i], pkt_dev);
1497 if (len < 0)
1498 return len;
1499 i += len;
1500 cnt = sprintf(pg_result, "OK: mpls=");
1501 for (n = 0; n < pkt_dev->nr_labels; n++)
1502 cnt += sprintf(pg_result + cnt,
1503 "%08x%s", ntohl(pkt_dev->labels[n]),
1504 n == pkt_dev->nr_labels-1 ? "" : ",");
1506 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1507 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1508 pkt_dev->svlan_id = 0xffff;
1510 if (debug)
1511 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1513 return count;
1516 if (!strcmp(name, "vlan_id")) {
1517 len = num_arg(&user_buffer[i], 4, &value);
1518 if (len < 0)
1519 return len;
1521 i += len;
1522 if (value <= 4095) {
1523 pkt_dev->vlan_id = value; /* turn on VLAN */
1525 if (debug)
1526 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1528 if (debug && pkt_dev->nr_labels)
1529 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1531 pkt_dev->nr_labels = 0; /* turn off MPLS */
1532 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1533 } else {
1534 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1535 pkt_dev->svlan_id = 0xffff;
1537 if (debug)
1538 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1540 return count;
1543 if (!strcmp(name, "vlan_p")) {
1544 len = num_arg(&user_buffer[i], 1, &value);
1545 if (len < 0)
1546 return len;
1548 i += len;
1549 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1550 pkt_dev->vlan_p = value;
1551 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1552 } else {
1553 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1555 return count;
1558 if (!strcmp(name, "vlan_cfi")) {
1559 len = num_arg(&user_buffer[i], 1, &value);
1560 if (len < 0)
1561 return len;
1563 i += len;
1564 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1565 pkt_dev->vlan_cfi = value;
1566 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1567 } else {
1568 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1570 return count;
1573 if (!strcmp(name, "svlan_id")) {
1574 len = num_arg(&user_buffer[i], 4, &value);
1575 if (len < 0)
1576 return len;
1578 i += len;
1579 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1580 pkt_dev->svlan_id = value; /* turn on SVLAN */
1582 if (debug)
1583 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1585 if (debug && pkt_dev->nr_labels)
1586 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1588 pkt_dev->nr_labels = 0; /* turn off MPLS */
1589 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1590 } else {
1591 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1592 pkt_dev->svlan_id = 0xffff;
1594 if (debug)
1595 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1597 return count;
1600 if (!strcmp(name, "svlan_p")) {
1601 len = num_arg(&user_buffer[i], 1, &value);
1602 if (len < 0)
1603 return len;
1605 i += len;
1606 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1607 pkt_dev->svlan_p = value;
1608 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1609 } else {
1610 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1612 return count;
1615 if (!strcmp(name, "svlan_cfi")) {
1616 len = num_arg(&user_buffer[i], 1, &value);
1617 if (len < 0)
1618 return len;
1620 i += len;
1621 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1622 pkt_dev->svlan_cfi = value;
1623 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1624 } else {
1625 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1627 return count;
1630 if (!strcmp(name, "tos")) {
1631 __u32 tmp_value = 0;
1632 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1633 if (len < 0)
1634 return len;
1636 i += len;
1637 if (len == 2) {
1638 pkt_dev->tos = tmp_value;
1639 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1640 } else {
1641 sprintf(pg_result, "ERROR: tos must be 00-ff");
1643 return count;
1646 if (!strcmp(name, "traffic_class")) {
1647 __u32 tmp_value = 0;
1648 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1649 if (len < 0)
1650 return len;
1652 i += len;
1653 if (len == 2) {
1654 pkt_dev->traffic_class = tmp_value;
1655 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1656 } else {
1657 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1659 return count;
1662 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1663 return -EINVAL;
1666 static int pktgen_if_open(struct inode *inode, struct file *file)
1668 return single_open(file, pktgen_if_show, PDE(inode)->data);
1671 static const struct file_operations pktgen_if_fops = {
1672 .owner = THIS_MODULE,
1673 .open = pktgen_if_open,
1674 .read = seq_read,
1675 .llseek = seq_lseek,
1676 .write = pktgen_if_write,
1677 .release = single_release,
1680 static int pktgen_thread_show(struct seq_file *seq, void *v)
1682 struct pktgen_thread *t = seq->private;
1683 const struct pktgen_dev *pkt_dev;
1685 BUG_ON(!t);
1687 seq_printf(seq, "Running: ");
1689 if_lock(t);
1690 list_for_each_entry(pkt_dev, &t->if_list, list)
1691 if (pkt_dev->running)
1692 seq_printf(seq, "%s ", pkt_dev->odev->name);
1694 seq_printf(seq, "\nStopped: ");
1696 list_for_each_entry(pkt_dev, &t->if_list, list)
1697 if (!pkt_dev->running)
1698 seq_printf(seq, "%s ", pkt_dev->odev->name);
1700 if (t->result[0])
1701 seq_printf(seq, "\nResult: %s\n", t->result);
1702 else
1703 seq_printf(seq, "\nResult: NA\n");
1705 if_unlock(t);
1707 return 0;
1710 static ssize_t pktgen_thread_write(struct file *file,
1711 const char __user * user_buffer,
1712 size_t count, loff_t * offset)
1714 struct seq_file *seq = (struct seq_file *)file->private_data;
1715 struct pktgen_thread *t = seq->private;
1716 int i = 0, max, len, ret;
1717 char name[40];
1718 char *pg_result;
1720 if (count < 1) {
1721 // sprintf(pg_result, "Wrong command format");
1722 return -EINVAL;
1725 max = count - i;
1726 len = count_trail_chars(&user_buffer[i], max);
1727 if (len < 0)
1728 return len;
1730 i += len;
1732 /* Read variable name */
1734 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1735 if (len < 0)
1736 return len;
1738 memset(name, 0, sizeof(name));
1739 if (copy_from_user(name, &user_buffer[i], len))
1740 return -EFAULT;
1741 i += len;
1743 max = count - i;
1744 len = count_trail_chars(&user_buffer[i], max);
1745 if (len < 0)
1746 return len;
1748 i += len;
1750 if (debug)
1751 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1752 name, (unsigned long)count);
1754 if (!t) {
1755 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1756 ret = -EINVAL;
1757 goto out;
1760 pg_result = &(t->result[0]);
1762 if (!strcmp(name, "add_device")) {
1763 char f[32];
1764 memset(f, 0, 32);
1765 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1766 if (len < 0) {
1767 ret = len;
1768 goto out;
1770 if (copy_from_user(f, &user_buffer[i], len))
1771 return -EFAULT;
1772 i += len;
1773 mutex_lock(&pktgen_thread_lock);
1774 pktgen_add_device(t, f);
1775 mutex_unlock(&pktgen_thread_lock);
1776 ret = count;
1777 sprintf(pg_result, "OK: add_device=%s", f);
1778 goto out;
1781 if (!strcmp(name, "rem_device_all")) {
1782 mutex_lock(&pktgen_thread_lock);
1783 t->control |= T_REMDEVALL;
1784 mutex_unlock(&pktgen_thread_lock);
1785 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1786 ret = count;
1787 sprintf(pg_result, "OK: rem_device_all");
1788 goto out;
1791 if (!strcmp(name, "max_before_softirq")) {
1792 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1793 ret = count;
1794 goto out;
1797 ret = -EINVAL;
1798 out:
1799 return ret;
1802 static int pktgen_thread_open(struct inode *inode, struct file *file)
1804 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1807 static const struct file_operations pktgen_thread_fops = {
1808 .owner = THIS_MODULE,
1809 .open = pktgen_thread_open,
1810 .read = seq_read,
1811 .llseek = seq_lseek,
1812 .write = pktgen_thread_write,
1813 .release = single_release,
1816 /* Think find or remove for NN */
1817 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1819 struct pktgen_thread *t;
1820 struct pktgen_dev *pkt_dev = NULL;
1822 list_for_each_entry(t, &pktgen_threads, th_list) {
1823 pkt_dev = pktgen_find_dev(t, ifname);
1824 if (pkt_dev) {
1825 if (remove) {
1826 if_lock(t);
1827 pkt_dev->removal_mark = 1;
1828 t->control |= T_REMDEV;
1829 if_unlock(t);
1831 break;
1834 return pkt_dev;
1838 * mark a device for removal
1840 static void pktgen_mark_device(const char *ifname)
1842 struct pktgen_dev *pkt_dev = NULL;
1843 const int max_tries = 10, msec_per_try = 125;
1844 int i = 0;
1846 mutex_lock(&pktgen_thread_lock);
1847 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1849 while (1) {
1851 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1852 if (pkt_dev == NULL)
1853 break; /* success */
1855 mutex_unlock(&pktgen_thread_lock);
1856 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1857 "to disappear....\n", ifname);
1858 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1859 mutex_lock(&pktgen_thread_lock);
1861 if (++i >= max_tries) {
1862 printk(KERN_ERR "pktgen_mark_device: timed out after "
1863 "waiting %d msec for device %s to be removed\n",
1864 msec_per_try * i, ifname);
1865 break;
1870 mutex_unlock(&pktgen_thread_lock);
1873 static void pktgen_change_name(struct net_device *dev)
1875 struct pktgen_thread *t;
1877 list_for_each_entry(t, &pktgen_threads, th_list) {
1878 struct pktgen_dev *pkt_dev;
1880 list_for_each_entry(pkt_dev, &t->if_list, list) {
1881 if (pkt_dev->odev != dev)
1882 continue;
1884 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1886 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1887 pg_proc_dir);
1888 if (!pkt_dev->entry)
1889 printk(KERN_ERR "pktgen: can't move proc "
1890 " entry for '%s'\n", dev->name);
1891 break;
1896 static int pktgen_device_event(struct notifier_block *unused,
1897 unsigned long event, void *ptr)
1899 struct net_device *dev = ptr;
1901 if (!net_eq(dev_net(dev), &init_net))
1902 return NOTIFY_DONE;
1904 /* It is OK that we do not hold the group lock right now,
1905 * as we run under the RTNL lock.
1908 switch (event) {
1909 case NETDEV_CHANGENAME:
1910 pktgen_change_name(dev);
1911 break;
1913 case NETDEV_UNREGISTER:
1914 pktgen_mark_device(dev->name);
1915 break;
1918 return NOTIFY_DONE;
1921 static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1922 const char *ifname)
1924 char b[IFNAMSIZ+5];
1925 int i = 0;
1927 for (i = 0; ifname[i] != '@'; i++) {
1928 if (i == IFNAMSIZ)
1929 break;
1931 b[i] = ifname[i];
1933 b[i] = 0;
1935 return dev_get_by_name(&init_net, b);
1939 /* Associate pktgen_dev with a device. */
1941 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1943 struct net_device *odev;
1944 int err;
1946 /* Clean old setups */
1947 if (pkt_dev->odev) {
1948 dev_put(pkt_dev->odev);
1949 pkt_dev->odev = NULL;
1952 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
1953 if (!odev) {
1954 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1955 return -ENODEV;
1958 if (odev->type != ARPHRD_ETHER) {
1959 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1960 err = -EINVAL;
1961 } else if (!netif_running(odev)) {
1962 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1963 err = -ENETDOWN;
1964 } else {
1965 pkt_dev->odev = odev;
1966 return 0;
1969 dev_put(odev);
1970 return err;
1973 /* Read pkt_dev from the interface and set up internal pktgen_dev
1974 * structure to have the right information to create/send packets
1976 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1978 int ntxq;
1980 if (!pkt_dev->odev) {
1981 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1982 "setup_inject.\n");
1983 sprintf(pkt_dev->result,
1984 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1985 return;
1988 /* make sure that we don't pick a non-existing transmit queue */
1989 ntxq = pkt_dev->odev->real_num_tx_queues;
1991 if (ntxq <= pkt_dev->queue_map_min) {
1992 printk(KERN_WARNING "pktgen: WARNING: Requested "
1993 "queue_map_min (zero-based) (%d) exceeds valid range "
1994 "[0 - %d] for (%d) queues on %s, resetting\n",
1995 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
1996 pkt_dev->odev->name);
1997 pkt_dev->queue_map_min = ntxq - 1;
1999 if (pkt_dev->queue_map_max >= ntxq) {
2000 printk(KERN_WARNING "pktgen: WARNING: Requested "
2001 "queue_map_max (zero-based) (%d) exceeds valid range "
2002 "[0 - %d] for (%d) queues on %s, resetting\n",
2003 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2004 pkt_dev->odev->name);
2005 pkt_dev->queue_map_max = ntxq - 1;
2008 /* Default to the interface's mac if not explicitly set. */
2010 if (is_zero_ether_addr(pkt_dev->src_mac))
2011 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2013 /* Set up Dest MAC */
2014 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2016 /* Set up pkt size */
2017 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2019 if (pkt_dev->flags & F_IPV6) {
2021 * Skip this automatic address setting until locks or functions
2022 * gets exported
2025 #ifdef NOTNOW
2026 int i, set = 0, err = 1;
2027 struct inet6_dev *idev;
2029 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2030 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2031 set = 1;
2032 break;
2035 if (!set) {
2038 * Use linklevel address if unconfigured.
2040 * use ipv6_get_lladdr if/when it's get exported
2043 rcu_read_lock();
2044 idev = __in6_dev_get(pkt_dev->odev);
2045 if (idev) {
2046 struct inet6_ifaddr *ifp;
2048 read_lock_bh(&idev->lock);
2049 for (ifp = idev->addr_list; ifp;
2050 ifp = ifp->if_next) {
2051 if (ifp->scope == IFA_LINK
2052 && !(ifp->
2053 flags & IFA_F_TENTATIVE)) {
2054 ipv6_addr_copy(&pkt_dev->
2055 cur_in6_saddr,
2056 &ifp->addr);
2057 err = 0;
2058 break;
2061 read_unlock_bh(&idev->lock);
2063 rcu_read_unlock();
2064 if (err)
2065 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2066 "address not availble.\n");
2068 #endif
2069 } else {
2070 pkt_dev->saddr_min = 0;
2071 pkt_dev->saddr_max = 0;
2072 if (strlen(pkt_dev->src_min) == 0) {
2074 struct in_device *in_dev;
2076 rcu_read_lock();
2077 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2078 if (in_dev) {
2079 if (in_dev->ifa_list) {
2080 pkt_dev->saddr_min =
2081 in_dev->ifa_list->ifa_address;
2082 pkt_dev->saddr_max = pkt_dev->saddr_min;
2085 rcu_read_unlock();
2086 } else {
2087 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2088 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2091 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2092 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2094 /* Initialize current values. */
2095 pkt_dev->cur_dst_mac_offset = 0;
2096 pkt_dev->cur_src_mac_offset = 0;
2097 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2098 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2099 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2100 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2101 pkt_dev->nflows = 0;
2105 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2107 ktime_t start;
2108 s32 remaining;
2109 struct hrtimer_sleeper t;
2111 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2112 hrtimer_set_expires(&t.timer, spin_until);
2114 remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
2115 if (remaining <= 0)
2116 return;
2118 start = ktime_now();
2119 if (remaining < 100)
2120 udelay(remaining); /* really small just spin */
2121 else {
2122 /* see do_nanosleep */
2123 hrtimer_init_sleeper(&t, current);
2124 do {
2125 set_current_state(TASK_INTERRUPTIBLE);
2126 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2127 if (!hrtimer_active(&t.timer))
2128 t.task = NULL;
2130 if (likely(t.task))
2131 schedule();
2133 hrtimer_cancel(&t.timer);
2134 } while (t.task && pkt_dev->running && !signal_pending(current));
2135 __set_current_state(TASK_RUNNING);
2137 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), start));
2140 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2142 pkt_dev->pkt_overhead = 0;
2143 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2144 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2145 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2148 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2150 return !!(pkt_dev->flows[flow].flags & F_INIT);
2153 static inline int f_pick(struct pktgen_dev *pkt_dev)
2155 int flow = pkt_dev->curfl;
2157 if (pkt_dev->flags & F_FLOW_SEQ) {
2158 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2159 /* reset time */
2160 pkt_dev->flows[flow].count = 0;
2161 pkt_dev->flows[flow].flags = 0;
2162 pkt_dev->curfl += 1;
2163 if (pkt_dev->curfl >= pkt_dev->cflows)
2164 pkt_dev->curfl = 0; /*reset */
2166 } else {
2167 flow = random32() % pkt_dev->cflows;
2168 pkt_dev->curfl = flow;
2170 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2171 pkt_dev->flows[flow].count = 0;
2172 pkt_dev->flows[flow].flags = 0;
2176 return pkt_dev->curfl;
2180 #ifdef CONFIG_XFRM
2181 /* If there was already an IPSEC SA, we keep it as is, else
2182 * we go look for it ...
2184 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2186 struct xfrm_state *x = pkt_dev->flows[flow].x;
2187 if (!x) {
2188 /*slow path: we dont already have xfrm_state*/
2189 x = xfrm_stateonly_find(&init_net,
2190 (xfrm_address_t *)&pkt_dev->cur_daddr,
2191 (xfrm_address_t *)&pkt_dev->cur_saddr,
2192 AF_INET,
2193 pkt_dev->ipsmode,
2194 pkt_dev->ipsproto, 0);
2195 if (x) {
2196 pkt_dev->flows[flow].x = x;
2197 set_pkt_overhead(pkt_dev);
2198 pkt_dev->pkt_overhead += x->props.header_len;
2203 #endif
2204 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2207 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2208 pkt_dev->cur_queue_map = smp_processor_id();
2210 else if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2211 __u16 t;
2212 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2213 t = random32() %
2214 (pkt_dev->queue_map_max -
2215 pkt_dev->queue_map_min + 1)
2216 + pkt_dev->queue_map_min;
2217 } else {
2218 t = pkt_dev->cur_queue_map + 1;
2219 if (t > pkt_dev->queue_map_max)
2220 t = pkt_dev->queue_map_min;
2222 pkt_dev->cur_queue_map = t;
2224 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2227 /* Increment/randomize headers according to flags and current values
2228 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2230 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2232 __u32 imn;
2233 __u32 imx;
2234 int flow = 0;
2236 if (pkt_dev->cflows)
2237 flow = f_pick(pkt_dev);
2239 /* Deal with source MAC */
2240 if (pkt_dev->src_mac_count > 1) {
2241 __u32 mc;
2242 __u32 tmp;
2244 if (pkt_dev->flags & F_MACSRC_RND)
2245 mc = random32() % pkt_dev->src_mac_count;
2246 else {
2247 mc = pkt_dev->cur_src_mac_offset++;
2248 if (pkt_dev->cur_src_mac_offset >=
2249 pkt_dev->src_mac_count)
2250 pkt_dev->cur_src_mac_offset = 0;
2253 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2254 pkt_dev->hh[11] = tmp;
2255 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2256 pkt_dev->hh[10] = tmp;
2257 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2258 pkt_dev->hh[9] = tmp;
2259 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2260 pkt_dev->hh[8] = tmp;
2261 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2262 pkt_dev->hh[7] = tmp;
2265 /* Deal with Destination MAC */
2266 if (pkt_dev->dst_mac_count > 1) {
2267 __u32 mc;
2268 __u32 tmp;
2270 if (pkt_dev->flags & F_MACDST_RND)
2271 mc = random32() % pkt_dev->dst_mac_count;
2273 else {
2274 mc = pkt_dev->cur_dst_mac_offset++;
2275 if (pkt_dev->cur_dst_mac_offset >=
2276 pkt_dev->dst_mac_count) {
2277 pkt_dev->cur_dst_mac_offset = 0;
2281 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2282 pkt_dev->hh[5] = tmp;
2283 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2284 pkt_dev->hh[4] = tmp;
2285 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2286 pkt_dev->hh[3] = tmp;
2287 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2288 pkt_dev->hh[2] = tmp;
2289 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2290 pkt_dev->hh[1] = tmp;
2293 if (pkt_dev->flags & F_MPLS_RND) {
2294 unsigned i;
2295 for (i = 0; i < pkt_dev->nr_labels; i++)
2296 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2297 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2298 ((__force __be32)random32() &
2299 htonl(0x000fffff));
2302 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2303 pkt_dev->vlan_id = random32() & (4096-1);
2306 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2307 pkt_dev->svlan_id = random32() & (4096 - 1);
2310 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2311 if (pkt_dev->flags & F_UDPSRC_RND)
2312 pkt_dev->cur_udp_src = random32() %
2313 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2314 + pkt_dev->udp_src_min;
2316 else {
2317 pkt_dev->cur_udp_src++;
2318 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2319 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2323 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2324 if (pkt_dev->flags & F_UDPDST_RND) {
2325 pkt_dev->cur_udp_dst = random32() %
2326 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2327 + pkt_dev->udp_dst_min;
2328 } else {
2329 pkt_dev->cur_udp_dst++;
2330 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2331 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2335 if (!(pkt_dev->flags & F_IPV6)) {
2337 imn = ntohl(pkt_dev->saddr_min);
2338 imx = ntohl(pkt_dev->saddr_max);
2339 if (imn < imx) {
2340 __u32 t;
2341 if (pkt_dev->flags & F_IPSRC_RND)
2342 t = random32() % (imx - imn) + imn;
2343 else {
2344 t = ntohl(pkt_dev->cur_saddr);
2345 t++;
2346 if (t > imx)
2347 t = imn;
2350 pkt_dev->cur_saddr = htonl(t);
2353 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2354 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2355 } else {
2356 imn = ntohl(pkt_dev->daddr_min);
2357 imx = ntohl(pkt_dev->daddr_max);
2358 if (imn < imx) {
2359 __u32 t;
2360 __be32 s;
2361 if (pkt_dev->flags & F_IPDST_RND) {
2363 t = random32() % (imx - imn) + imn;
2364 s = htonl(t);
2366 while (ipv4_is_loopback(s) ||
2367 ipv4_is_multicast(s) ||
2368 ipv4_is_lbcast(s) ||
2369 ipv4_is_zeronet(s) ||
2370 ipv4_is_local_multicast(s)) {
2371 t = random32() % (imx - imn) + imn;
2372 s = htonl(t);
2374 pkt_dev->cur_daddr = s;
2375 } else {
2376 t = ntohl(pkt_dev->cur_daddr);
2377 t++;
2378 if (t > imx) {
2379 t = imn;
2381 pkt_dev->cur_daddr = htonl(t);
2384 if (pkt_dev->cflows) {
2385 pkt_dev->flows[flow].flags |= F_INIT;
2386 pkt_dev->flows[flow].cur_daddr =
2387 pkt_dev->cur_daddr;
2388 #ifdef CONFIG_XFRM
2389 if (pkt_dev->flags & F_IPSEC_ON)
2390 get_ipsec_sa(pkt_dev, flow);
2391 #endif
2392 pkt_dev->nflows++;
2395 } else { /* IPV6 * */
2397 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2398 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2399 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2400 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2401 else {
2402 int i;
2404 /* Only random destinations yet */
2406 for (i = 0; i < 4; i++) {
2407 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2408 (((__force __be32)random32() |
2409 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2410 pkt_dev->max_in6_daddr.s6_addr32[i]);
2415 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2416 __u32 t;
2417 if (pkt_dev->flags & F_TXSIZE_RND) {
2418 t = random32() %
2419 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2420 + pkt_dev->min_pkt_size;
2421 } else {
2422 t = pkt_dev->cur_pkt_size + 1;
2423 if (t > pkt_dev->max_pkt_size)
2424 t = pkt_dev->min_pkt_size;
2426 pkt_dev->cur_pkt_size = t;
2429 set_cur_queue_map(pkt_dev);
2431 pkt_dev->flows[flow].count++;
2435 #ifdef CONFIG_XFRM
2436 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2438 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2439 int err = 0;
2440 struct iphdr *iph;
2442 if (!x)
2443 return 0;
2444 /* XXX: we dont support tunnel mode for now until
2445 * we resolve the dst issue */
2446 if (x->props.mode != XFRM_MODE_TRANSPORT)
2447 return 0;
2449 spin_lock(&x->lock);
2450 iph = ip_hdr(skb);
2452 err = x->outer_mode->output(x, skb);
2453 if (err)
2454 goto error;
2455 err = x->type->output(x, skb);
2456 if (err)
2457 goto error;
2459 x->curlft.bytes += skb->len;
2460 x->curlft.packets++;
2461 error:
2462 spin_unlock(&x->lock);
2463 return err;
2466 static void free_SAs(struct pktgen_dev *pkt_dev)
2468 if (pkt_dev->cflows) {
2469 /* let go of the SAs if we have them */
2470 int i = 0;
2471 for (; i < pkt_dev->cflows; i++) {
2472 struct xfrm_state *x = pkt_dev->flows[i].x;
2473 if (x) {
2474 xfrm_state_put(x);
2475 pkt_dev->flows[i].x = NULL;
2481 static int process_ipsec(struct pktgen_dev *pkt_dev,
2482 struct sk_buff *skb, __be16 protocol)
2484 if (pkt_dev->flags & F_IPSEC_ON) {
2485 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2486 int nhead = 0;
2487 if (x) {
2488 int ret;
2489 __u8 *eth;
2490 nhead = x->props.header_len - skb_headroom(skb);
2491 if (nhead > 0) {
2492 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2493 if (ret < 0) {
2494 printk(KERN_ERR "Error expanding "
2495 "ipsec packet %d\n", ret);
2496 goto err;
2500 /* ipsec is not expecting ll header */
2501 skb_pull(skb, ETH_HLEN);
2502 ret = pktgen_output_ipsec(skb, pkt_dev);
2503 if (ret) {
2504 printk(KERN_ERR "Error creating ipsec "
2505 "packet %d\n", ret);
2506 goto err;
2508 /* restore ll */
2509 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2510 memcpy(eth, pkt_dev->hh, 12);
2511 *(u16 *) &eth[12] = protocol;
2514 return 1;
2515 err:
2516 kfree_skb(skb);
2517 return 0;
2519 #endif
2521 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2523 unsigned i;
2524 for (i = 0; i < pkt_dev->nr_labels; i++)
2525 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2527 mpls--;
2528 *mpls |= MPLS_STACK_BOTTOM;
2531 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2532 unsigned int prio)
2534 return htons(id | (cfi << 12) | (prio << 13));
2537 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2538 struct pktgen_dev *pkt_dev)
2540 struct sk_buff *skb = NULL;
2541 __u8 *eth;
2542 struct udphdr *udph;
2543 int datalen, iplen;
2544 struct iphdr *iph;
2545 struct pktgen_hdr *pgh = NULL;
2546 __be16 protocol = htons(ETH_P_IP);
2547 __be32 *mpls;
2548 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2549 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2550 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2551 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2552 u16 queue_map;
2554 if (pkt_dev->nr_labels)
2555 protocol = htons(ETH_P_MPLS_UC);
2557 if (pkt_dev->vlan_id != 0xffff)
2558 protocol = htons(ETH_P_8021Q);
2560 /* Update any of the values, used when we're incrementing various
2561 * fields.
2563 queue_map = pkt_dev->cur_queue_map;
2564 mod_cur_headers(pkt_dev);
2566 datalen = (odev->hard_header_len + 16) & ~0xf;
2567 skb = __netdev_alloc_skb(odev,
2568 pkt_dev->cur_pkt_size + 64
2569 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2570 if (!skb) {
2571 sprintf(pkt_dev->result, "No memory");
2572 return NULL;
2575 skb_reserve(skb, datalen);
2577 /* Reserve for ethernet and IP header */
2578 eth = (__u8 *) skb_push(skb, 14);
2579 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2580 if (pkt_dev->nr_labels)
2581 mpls_push(mpls, pkt_dev);
2583 if (pkt_dev->vlan_id != 0xffff) {
2584 if (pkt_dev->svlan_id != 0xffff) {
2585 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2586 *svlan_tci = build_tci(pkt_dev->svlan_id,
2587 pkt_dev->svlan_cfi,
2588 pkt_dev->svlan_p);
2589 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2590 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2592 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2593 *vlan_tci = build_tci(pkt_dev->vlan_id,
2594 pkt_dev->vlan_cfi,
2595 pkt_dev->vlan_p);
2596 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2597 *vlan_encapsulated_proto = htons(ETH_P_IP);
2600 skb->network_header = skb->tail;
2601 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2602 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2603 skb_set_queue_mapping(skb, queue_map);
2604 iph = ip_hdr(skb);
2605 udph = udp_hdr(skb);
2607 memcpy(eth, pkt_dev->hh, 12);
2608 *(__be16 *) & eth[12] = protocol;
2610 /* Eth + IPh + UDPh + mpls */
2611 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2612 pkt_dev->pkt_overhead;
2613 if (datalen < sizeof(struct pktgen_hdr))
2614 datalen = sizeof(struct pktgen_hdr);
2616 udph->source = htons(pkt_dev->cur_udp_src);
2617 udph->dest = htons(pkt_dev->cur_udp_dst);
2618 udph->len = htons(datalen + 8); /* DATA + udphdr */
2619 udph->check = 0; /* No checksum */
2621 iph->ihl = 5;
2622 iph->version = 4;
2623 iph->ttl = 32;
2624 iph->tos = pkt_dev->tos;
2625 iph->protocol = IPPROTO_UDP; /* UDP */
2626 iph->saddr = pkt_dev->cur_saddr;
2627 iph->daddr = pkt_dev->cur_daddr;
2628 iph->frag_off = 0;
2629 iplen = 20 + 8 + datalen;
2630 iph->tot_len = htons(iplen);
2631 iph->check = 0;
2632 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2633 skb->protocol = protocol;
2634 skb->mac_header = (skb->network_header - ETH_HLEN -
2635 pkt_dev->pkt_overhead);
2636 skb->dev = odev;
2637 skb->pkt_type = PACKET_HOST;
2639 if (pkt_dev->nfrags <= 0)
2640 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2641 else {
2642 int frags = pkt_dev->nfrags;
2643 int i;
2645 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2647 if (frags > MAX_SKB_FRAGS)
2648 frags = MAX_SKB_FRAGS;
2649 if (datalen > frags * PAGE_SIZE) {
2650 skb_put(skb, datalen - frags * PAGE_SIZE);
2651 datalen = frags * PAGE_SIZE;
2654 i = 0;
2655 while (datalen > 0) {
2656 struct page *page = alloc_pages(GFP_KERNEL, 0);
2657 skb_shinfo(skb)->frags[i].page = page;
2658 skb_shinfo(skb)->frags[i].page_offset = 0;
2659 skb_shinfo(skb)->frags[i].size =
2660 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2661 datalen -= skb_shinfo(skb)->frags[i].size;
2662 skb->len += skb_shinfo(skb)->frags[i].size;
2663 skb->data_len += skb_shinfo(skb)->frags[i].size;
2664 i++;
2665 skb_shinfo(skb)->nr_frags = i;
2668 while (i < frags) {
2669 int rem;
2671 if (i == 0)
2672 break;
2674 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2675 if (rem == 0)
2676 break;
2678 skb_shinfo(skb)->frags[i - 1].size -= rem;
2680 skb_shinfo(skb)->frags[i] =
2681 skb_shinfo(skb)->frags[i - 1];
2682 get_page(skb_shinfo(skb)->frags[i].page);
2683 skb_shinfo(skb)->frags[i].page =
2684 skb_shinfo(skb)->frags[i - 1].page;
2685 skb_shinfo(skb)->frags[i].page_offset +=
2686 skb_shinfo(skb)->frags[i - 1].size;
2687 skb_shinfo(skb)->frags[i].size = rem;
2688 i++;
2689 skb_shinfo(skb)->nr_frags = i;
2693 /* Stamp the time, and sequence number,
2694 * convert them to network byte order
2696 if (pgh) {
2697 struct timeval timestamp;
2699 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2700 pgh->seq_num = htonl(pkt_dev->seq_num);
2702 do_gettimeofday(&timestamp);
2703 pgh->tv_sec = htonl(timestamp.tv_sec);
2704 pgh->tv_usec = htonl(timestamp.tv_usec);
2707 #ifdef CONFIG_XFRM
2708 if (!process_ipsec(pkt_dev, skb, protocol))
2709 return NULL;
2710 #endif
2712 return skb;
2716 * scan_ip6, fmt_ip taken from dietlibc-0.21
2717 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2719 * Slightly modified for kernel.
2720 * Should be candidate for net/ipv4/utils.c
2721 * --ro
2724 static unsigned int scan_ip6(const char *s, char ip[16])
2726 unsigned int i;
2727 unsigned int len = 0;
2728 unsigned long u;
2729 char suffix[16];
2730 unsigned int prefixlen = 0;
2731 unsigned int suffixlen = 0;
2732 __be32 tmp;
2733 char *pos;
2735 for (i = 0; i < 16; i++)
2736 ip[i] = 0;
2738 for (;;) {
2739 if (*s == ':') {
2740 len++;
2741 if (s[1] == ':') { /* Found "::", skip to part 2 */
2742 s += 2;
2743 len++;
2744 break;
2746 s++;
2749 u = simple_strtoul(s, &pos, 16);
2750 i = pos - s;
2751 if (!i)
2752 return 0;
2753 if (prefixlen == 12 && s[i] == '.') {
2755 /* the last 4 bytes may be written as IPv4 address */
2757 tmp = in_aton(s);
2758 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2759 return i + len;
2761 ip[prefixlen++] = (u >> 8);
2762 ip[prefixlen++] = (u & 255);
2763 s += i;
2764 len += i;
2765 if (prefixlen == 16)
2766 return len;
2769 /* part 2, after "::" */
2770 for (;;) {
2771 if (*s == ':') {
2772 if (suffixlen == 0)
2773 break;
2774 s++;
2775 len++;
2776 } else if (suffixlen != 0)
2777 break;
2779 u = simple_strtol(s, &pos, 16);
2780 i = pos - s;
2781 if (!i) {
2782 if (*s)
2783 len--;
2784 break;
2786 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2787 tmp = in_aton(s);
2788 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2789 sizeof(tmp));
2790 suffixlen += 4;
2791 len += strlen(s);
2792 break;
2794 suffix[suffixlen++] = (u >> 8);
2795 suffix[suffixlen++] = (u & 255);
2796 s += i;
2797 len += i;
2798 if (prefixlen + suffixlen == 16)
2799 break;
2801 for (i = 0; i < suffixlen; i++)
2802 ip[16 - suffixlen + i] = suffix[i];
2803 return len;
2806 static char tohex(char hexdigit)
2808 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2811 static int fmt_xlong(char *s, unsigned int i)
2813 char *bak = s;
2814 *s = tohex((i >> 12) & 0xf);
2815 if (s != bak || *s != '0')
2816 ++s;
2817 *s = tohex((i >> 8) & 0xf);
2818 if (s != bak || *s != '0')
2819 ++s;
2820 *s = tohex((i >> 4) & 0xf);
2821 if (s != bak || *s != '0')
2822 ++s;
2823 *s = tohex(i & 0xf);
2824 return s - bak + 1;
2827 static unsigned int fmt_ip6(char *s, const char ip[16])
2829 unsigned int len;
2830 unsigned int i;
2831 unsigned int temp;
2832 unsigned int compressing;
2833 int j;
2835 len = 0;
2836 compressing = 0;
2837 for (j = 0; j < 16; j += 2) {
2839 #ifdef V4MAPPEDPREFIX
2840 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2841 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2842 temp = strlen(s);
2843 return len + temp;
2845 #endif
2846 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2847 (unsigned long)(unsigned char)ip[j + 1];
2848 if (temp == 0) {
2849 if (!compressing) {
2850 compressing = 1;
2851 if (j == 0) {
2852 *s++ = ':';
2853 ++len;
2856 } else {
2857 if (compressing) {
2858 compressing = 0;
2859 *s++ = ':';
2860 ++len;
2862 i = fmt_xlong(s, temp);
2863 len += i;
2864 s += i;
2865 if (j < 14) {
2866 *s++ = ':';
2867 ++len;
2871 if (compressing) {
2872 *s++ = ':';
2873 ++len;
2875 *s = 0;
2876 return len;
2879 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2880 struct pktgen_dev *pkt_dev)
2882 struct sk_buff *skb = NULL;
2883 __u8 *eth;
2884 struct udphdr *udph;
2885 int datalen;
2886 struct ipv6hdr *iph;
2887 struct pktgen_hdr *pgh = NULL;
2888 __be16 protocol = htons(ETH_P_IPV6);
2889 __be32 *mpls;
2890 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2891 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2892 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2893 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2894 u16 queue_map;
2896 if (pkt_dev->nr_labels)
2897 protocol = htons(ETH_P_MPLS_UC);
2899 if (pkt_dev->vlan_id != 0xffff)
2900 protocol = htons(ETH_P_8021Q);
2902 /* Update any of the values, used when we're incrementing various
2903 * fields.
2905 queue_map = pkt_dev->cur_queue_map;
2906 mod_cur_headers(pkt_dev);
2908 skb = __netdev_alloc_skb(odev,
2909 pkt_dev->cur_pkt_size + 64
2910 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
2911 if (!skb) {
2912 sprintf(pkt_dev->result, "No memory");
2913 return NULL;
2916 skb_reserve(skb, 16);
2918 /* Reserve for ethernet and IP header */
2919 eth = (__u8 *) skb_push(skb, 14);
2920 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2921 if (pkt_dev->nr_labels)
2922 mpls_push(mpls, pkt_dev);
2924 if (pkt_dev->vlan_id != 0xffff) {
2925 if (pkt_dev->svlan_id != 0xffff) {
2926 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2927 *svlan_tci = build_tci(pkt_dev->svlan_id,
2928 pkt_dev->svlan_cfi,
2929 pkt_dev->svlan_p);
2930 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2931 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2933 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2934 *vlan_tci = build_tci(pkt_dev->vlan_id,
2935 pkt_dev->vlan_cfi,
2936 pkt_dev->vlan_p);
2937 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2938 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2941 skb->network_header = skb->tail;
2942 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2943 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2944 skb_set_queue_mapping(skb, queue_map);
2945 iph = ipv6_hdr(skb);
2946 udph = udp_hdr(skb);
2948 memcpy(eth, pkt_dev->hh, 12);
2949 *(__be16 *) &eth[12] = protocol;
2951 /* Eth + IPh + UDPh + mpls */
2952 datalen = pkt_dev->cur_pkt_size - 14 -
2953 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2954 pkt_dev->pkt_overhead;
2956 if (datalen < sizeof(struct pktgen_hdr)) {
2957 datalen = sizeof(struct pktgen_hdr);
2958 if (net_ratelimit())
2959 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2960 datalen);
2963 udph->source = htons(pkt_dev->cur_udp_src);
2964 udph->dest = htons(pkt_dev->cur_udp_dst);
2965 udph->len = htons(datalen + sizeof(struct udphdr));
2966 udph->check = 0; /* No checksum */
2968 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2970 if (pkt_dev->traffic_class) {
2971 /* Version + traffic class + flow (0) */
2972 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2975 iph->hop_limit = 32;
2977 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2978 iph->nexthdr = IPPROTO_UDP;
2980 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2981 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2983 skb->mac_header = (skb->network_header - ETH_HLEN -
2984 pkt_dev->pkt_overhead);
2985 skb->protocol = protocol;
2986 skb->dev = odev;
2987 skb->pkt_type = PACKET_HOST;
2989 if (pkt_dev->nfrags <= 0)
2990 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2991 else {
2992 int frags = pkt_dev->nfrags;
2993 int i;
2995 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2997 if (frags > MAX_SKB_FRAGS)
2998 frags = MAX_SKB_FRAGS;
2999 if (datalen > frags * PAGE_SIZE) {
3000 skb_put(skb, datalen - frags * PAGE_SIZE);
3001 datalen = frags * PAGE_SIZE;
3004 i = 0;
3005 while (datalen > 0) {
3006 struct page *page = alloc_pages(GFP_KERNEL, 0);
3007 skb_shinfo(skb)->frags[i].page = page;
3008 skb_shinfo(skb)->frags[i].page_offset = 0;
3009 skb_shinfo(skb)->frags[i].size =
3010 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
3011 datalen -= skb_shinfo(skb)->frags[i].size;
3012 skb->len += skb_shinfo(skb)->frags[i].size;
3013 skb->data_len += skb_shinfo(skb)->frags[i].size;
3014 i++;
3015 skb_shinfo(skb)->nr_frags = i;
3018 while (i < frags) {
3019 int rem;
3021 if (i == 0)
3022 break;
3024 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3025 if (rem == 0)
3026 break;
3028 skb_shinfo(skb)->frags[i - 1].size -= rem;
3030 skb_shinfo(skb)->frags[i] =
3031 skb_shinfo(skb)->frags[i - 1];
3032 get_page(skb_shinfo(skb)->frags[i].page);
3033 skb_shinfo(skb)->frags[i].page =
3034 skb_shinfo(skb)->frags[i - 1].page;
3035 skb_shinfo(skb)->frags[i].page_offset +=
3036 skb_shinfo(skb)->frags[i - 1].size;
3037 skb_shinfo(skb)->frags[i].size = rem;
3038 i++;
3039 skb_shinfo(skb)->nr_frags = i;
3043 /* Stamp the time, and sequence number,
3044 * convert them to network byte order
3045 * should we update cloned packets too ?
3047 if (pgh) {
3048 struct timeval timestamp;
3050 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3051 pgh->seq_num = htonl(pkt_dev->seq_num);
3053 do_gettimeofday(&timestamp);
3054 pgh->tv_sec = htonl(timestamp.tv_sec);
3055 pgh->tv_usec = htonl(timestamp.tv_usec);
3057 /* pkt_dev->seq_num++; FF: you really mean this? */
3059 return skb;
3062 static struct sk_buff *fill_packet(struct net_device *odev,
3063 struct pktgen_dev *pkt_dev)
3065 if (pkt_dev->flags & F_IPV6)
3066 return fill_packet_ipv6(odev, pkt_dev);
3067 else
3068 return fill_packet_ipv4(odev, pkt_dev);
3071 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3073 pkt_dev->seq_num = 1;
3074 pkt_dev->idle_acc = 0;
3075 pkt_dev->sofar = 0;
3076 pkt_dev->tx_bytes = 0;
3077 pkt_dev->errors = 0;
3080 /* Set up structure for sending pkts, clear counters */
3082 static void pktgen_run(struct pktgen_thread *t)
3084 struct pktgen_dev *pkt_dev;
3085 int started = 0;
3087 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3089 if_lock(t);
3090 list_for_each_entry(pkt_dev, &t->if_list, list) {
3093 * setup odev and create initial packet.
3095 pktgen_setup_inject(pkt_dev);
3097 if (pkt_dev->odev) {
3098 pktgen_clear_counters(pkt_dev);
3099 pkt_dev->running = 1; /* Cranke yeself! */
3100 pkt_dev->skb = NULL;
3101 pkt_dev->started_at =
3102 pkt_dev->next_tx = ktime_now();
3104 set_pkt_overhead(pkt_dev);
3106 strcpy(pkt_dev->result, "Starting");
3107 started++;
3108 } else
3109 strcpy(pkt_dev->result, "Error starting");
3111 if_unlock(t);
3112 if (started)
3113 t->control &= ~(T_STOP);
3116 static void pktgen_stop_all_threads_ifs(void)
3118 struct pktgen_thread *t;
3120 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3122 mutex_lock(&pktgen_thread_lock);
3124 list_for_each_entry(t, &pktgen_threads, th_list)
3125 t->control |= T_STOP;
3127 mutex_unlock(&pktgen_thread_lock);
3130 static int thread_is_running(const struct pktgen_thread *t)
3132 const struct pktgen_dev *pkt_dev;
3134 list_for_each_entry(pkt_dev, &t->if_list, list)
3135 if (pkt_dev->running)
3136 return 1;
3137 return 0;
3140 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3142 if_lock(t);
3144 while (thread_is_running(t)) {
3146 if_unlock(t);
3148 msleep_interruptible(100);
3150 if (signal_pending(current))
3151 goto signal;
3152 if_lock(t);
3154 if_unlock(t);
3155 return 1;
3156 signal:
3157 return 0;
3160 static int pktgen_wait_all_threads_run(void)
3162 struct pktgen_thread *t;
3163 int sig = 1;
3165 mutex_lock(&pktgen_thread_lock);
3167 list_for_each_entry(t, &pktgen_threads, th_list) {
3168 sig = pktgen_wait_thread_run(t);
3169 if (sig == 0)
3170 break;
3173 if (sig == 0)
3174 list_for_each_entry(t, &pktgen_threads, th_list)
3175 t->control |= (T_STOP);
3177 mutex_unlock(&pktgen_thread_lock);
3178 return sig;
3181 static void pktgen_run_all_threads(void)
3183 struct pktgen_thread *t;
3185 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3187 mutex_lock(&pktgen_thread_lock);
3189 list_for_each_entry(t, &pktgen_threads, th_list)
3190 t->control |= (T_RUN);
3192 mutex_unlock(&pktgen_thread_lock);
3194 /* Propagate thread->control */
3195 schedule_timeout_interruptible(msecs_to_jiffies(125));
3197 pktgen_wait_all_threads_run();
3200 static void pktgen_reset_all_threads(void)
3202 struct pktgen_thread *t;
3204 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3206 mutex_lock(&pktgen_thread_lock);
3208 list_for_each_entry(t, &pktgen_threads, th_list)
3209 t->control |= (T_REMDEVALL);
3211 mutex_unlock(&pktgen_thread_lock);
3213 /* Propagate thread->control */
3214 schedule_timeout_interruptible(msecs_to_jiffies(125));
3216 pktgen_wait_all_threads_run();
3219 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3221 __u64 bps, mbps, pps;
3222 char *p = pkt_dev->result;
3223 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3224 pkt_dev->started_at);
3225 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3227 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3228 (unsigned long long)ktime_to_us(elapsed),
3229 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3230 (unsigned long long)ktime_to_us(idle),
3231 (unsigned long long)pkt_dev->sofar,
3232 pkt_dev->cur_pkt_size, nr_frags);
3234 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3235 ktime_to_ns(elapsed));
3237 bps = pps * 8 * pkt_dev->cur_pkt_size;
3239 mbps = bps;
3240 do_div(mbps, 1000000);
3241 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3242 (unsigned long long)pps,
3243 (unsigned long long)mbps,
3244 (unsigned long long)bps,
3245 (unsigned long long)pkt_dev->errors);
3248 /* Set stopped-at timer, remove from running list, do counters & statistics */
3249 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3251 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3253 if (!pkt_dev->running) {
3254 printk(KERN_WARNING "pktgen: interface: %s is already "
3255 "stopped\n", pkt_dev->odev->name);
3256 return -EINVAL;
3259 kfree_skb(pkt_dev->skb);
3260 pkt_dev->skb = NULL;
3261 pkt_dev->stopped_at = ktime_now();
3262 pkt_dev->running = 0;
3264 show_results(pkt_dev, nr_frags);
3266 return 0;
3269 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3271 struct pktgen_dev *pkt_dev, *best = NULL;
3273 if_lock(t);
3275 list_for_each_entry(pkt_dev, &t->if_list, list) {
3276 if (!pkt_dev->running)
3277 continue;
3278 if (best == NULL)
3279 best = pkt_dev;
3280 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3281 best = pkt_dev;
3283 if_unlock(t);
3284 return best;
3287 static void pktgen_stop(struct pktgen_thread *t)
3289 struct pktgen_dev *pkt_dev;
3291 pr_debug("pktgen: entering pktgen_stop\n");
3293 if_lock(t);
3295 list_for_each_entry(pkt_dev, &t->if_list, list) {
3296 pktgen_stop_device(pkt_dev);
3299 if_unlock(t);
3303 * one of our devices needs to be removed - find it
3304 * and remove it
3306 static void pktgen_rem_one_if(struct pktgen_thread *t)
3308 struct list_head *q, *n;
3309 struct pktgen_dev *cur;
3311 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3313 if_lock(t);
3315 list_for_each_safe(q, n, &t->if_list) {
3316 cur = list_entry(q, struct pktgen_dev, list);
3318 if (!cur->removal_mark)
3319 continue;
3321 kfree_skb(cur->skb);
3322 cur->skb = NULL;
3324 pktgen_remove_device(t, cur);
3326 break;
3329 if_unlock(t);
3332 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3334 struct list_head *q, *n;
3335 struct pktgen_dev *cur;
3337 /* Remove all devices, free mem */
3339 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3340 if_lock(t);
3342 list_for_each_safe(q, n, &t->if_list) {
3343 cur = list_entry(q, struct pktgen_dev, list);
3345 kfree_skb(cur->skb);
3346 cur->skb = NULL;
3348 pktgen_remove_device(t, cur);
3351 if_unlock(t);
3354 static void pktgen_rem_thread(struct pktgen_thread *t)
3356 /* Remove from the thread list */
3358 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3360 mutex_lock(&pktgen_thread_lock);
3362 list_del(&t->th_list);
3364 mutex_unlock(&pktgen_thread_lock);
3367 static void idle(struct pktgen_dev *pkt_dev)
3369 ktime_t idle_start = ktime_now();
3371 if (need_resched())
3372 schedule();
3373 else
3374 cpu_relax();
3376 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3380 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3382 struct net_device *odev = pkt_dev->odev;
3383 int (*xmit)(struct sk_buff *, struct net_device *)
3384 = odev->netdev_ops->ndo_start_xmit;
3385 struct netdev_queue *txq;
3386 u16 queue_map;
3387 int ret;
3389 if (pkt_dev->delay) {
3390 spin(pkt_dev, pkt_dev->next_tx);
3392 /* This is max DELAY, this has special meaning of
3393 * "never transmit"
3395 if (pkt_dev->delay == ULLONG_MAX) {
3396 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3397 return;
3401 if (!pkt_dev->skb) {
3402 set_cur_queue_map(pkt_dev);
3403 queue_map = pkt_dev->cur_queue_map;
3404 } else {
3405 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3408 txq = netdev_get_tx_queue(odev, queue_map);
3409 /* Did we saturate the queue already? */
3410 if (netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)) {
3411 /* If device is down, then all queues are permnantly frozen */
3412 if (netif_running(odev))
3413 idle(pkt_dev);
3414 else
3415 pktgen_stop_device(pkt_dev);
3416 return;
3419 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3420 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3421 /* build a new pkt */
3422 kfree_skb(pkt_dev->skb);
3424 pkt_dev->skb = fill_packet(odev, pkt_dev);
3425 if (pkt_dev->skb == NULL) {
3426 printk(KERN_ERR "pktgen: ERROR: couldn't "
3427 "allocate skb in fill_packet.\n");
3428 schedule();
3429 pkt_dev->clone_count--; /* back out increment, OOM */
3430 return;
3433 pkt_dev->allocated_skbs++;
3434 pkt_dev->clone_count = 0; /* reset counter */
3437 /* fill_packet() might have changed the queue */
3438 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3439 txq = netdev_get_tx_queue(odev, queue_map);
3441 __netif_tx_lock_bh(txq);
3442 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)))
3443 pkt_dev->last_ok = 0;
3444 else {
3445 atomic_inc(&(pkt_dev->skb->users));
3447 retry_now:
3448 ret = (*xmit)(pkt_dev->skb, odev);
3449 switch (ret) {
3450 case NETDEV_TX_OK:
3451 txq_trans_update(txq);
3452 pkt_dev->last_ok = 1;
3453 pkt_dev->sofar++;
3454 pkt_dev->seq_num++;
3455 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3456 break;
3457 case NETDEV_TX_LOCKED:
3458 cpu_relax();
3459 goto retry_now;
3460 default: /* Drivers are not supposed to return other values! */
3461 if (net_ratelimit())
3462 pr_info("pktgen: %s xmit error: %d\n",
3463 odev->name, ret);
3464 pkt_dev->errors++;
3465 /* fallthru */
3466 case NETDEV_TX_BUSY:
3467 /* Retry it next time */
3468 atomic_dec(&(pkt_dev->skb->users));
3469 pkt_dev->last_ok = 0;
3472 if (pkt_dev->delay)
3473 pkt_dev->next_tx = ktime_add_ns(ktime_now(),
3474 pkt_dev->delay);
3476 __netif_tx_unlock_bh(txq);
3478 /* If pkt_dev->count is zero, then run forever */
3479 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3480 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3481 if (signal_pending(current))
3482 break;
3483 idle(pkt_dev);
3486 /* Done with this */
3487 pktgen_stop_device(pkt_dev);
3492 * Main loop of the thread goes here
3495 static int pktgen_thread_worker(void *arg)
3497 DEFINE_WAIT(wait);
3498 struct pktgen_thread *t = arg;
3499 struct pktgen_dev *pkt_dev = NULL;
3500 int cpu = t->cpu;
3502 BUG_ON(smp_processor_id() != cpu);
3504 init_waitqueue_head(&t->queue);
3505 complete(&t->start_done);
3507 pr_debug("pktgen: starting pktgen/%d: pid=%d\n",
3508 cpu, task_pid_nr(current));
3510 set_current_state(TASK_INTERRUPTIBLE);
3512 set_freezable();
3514 while (!kthread_should_stop()) {
3515 pkt_dev = next_to_run(t);
3517 if (!pkt_dev &&
3518 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3519 == 0) {
3520 prepare_to_wait(&(t->queue), &wait,
3521 TASK_INTERRUPTIBLE);
3522 schedule_timeout(HZ / 10);
3523 finish_wait(&(t->queue), &wait);
3526 __set_current_state(TASK_RUNNING);
3528 if (pkt_dev)
3529 pktgen_xmit(pkt_dev);
3531 if (t->control & T_STOP) {
3532 pktgen_stop(t);
3533 t->control &= ~(T_STOP);
3536 if (t->control & T_RUN) {
3537 pktgen_run(t);
3538 t->control &= ~(T_RUN);
3541 if (t->control & T_REMDEVALL) {
3542 pktgen_rem_all_ifs(t);
3543 t->control &= ~(T_REMDEVALL);
3546 if (t->control & T_REMDEV) {
3547 pktgen_rem_one_if(t);
3548 t->control &= ~(T_REMDEV);
3551 try_to_freeze();
3553 set_current_state(TASK_INTERRUPTIBLE);
3556 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3557 pktgen_stop(t);
3559 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3560 pktgen_rem_all_ifs(t);
3562 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3563 pktgen_rem_thread(t);
3565 return 0;
3568 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3569 const char *ifname)
3571 struct pktgen_dev *p, *pkt_dev = NULL;
3572 if_lock(t);
3574 list_for_each_entry(p, &t->if_list, list)
3575 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3576 pkt_dev = p;
3577 break;
3580 if_unlock(t);
3581 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3582 return pkt_dev;
3586 * Adds a dev at front of if_list.
3589 static int add_dev_to_thread(struct pktgen_thread *t,
3590 struct pktgen_dev *pkt_dev)
3592 int rv = 0;
3594 if_lock(t);
3596 if (pkt_dev->pg_thread) {
3597 printk(KERN_ERR "pktgen: ERROR: already assigned "
3598 "to a thread.\n");
3599 rv = -EBUSY;
3600 goto out;
3603 list_add(&pkt_dev->list, &t->if_list);
3604 pkt_dev->pg_thread = t;
3605 pkt_dev->running = 0;
3607 out:
3608 if_unlock(t);
3609 return rv;
3612 /* Called under thread lock */
3614 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3616 struct pktgen_dev *pkt_dev;
3617 int err;
3619 /* We don't allow a device to be on several threads */
3621 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3622 if (pkt_dev) {
3623 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3624 return -EBUSY;
3627 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3628 if (!pkt_dev)
3629 return -ENOMEM;
3631 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3632 if (pkt_dev->flows == NULL) {
3633 kfree(pkt_dev);
3634 return -ENOMEM;
3636 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3638 pkt_dev->removal_mark = 0;
3639 pkt_dev->min_pkt_size = ETH_ZLEN;
3640 pkt_dev->max_pkt_size = ETH_ZLEN;
3641 pkt_dev->nfrags = 0;
3642 pkt_dev->clone_skb = pg_clone_skb_d;
3643 pkt_dev->delay = pg_delay_d;
3644 pkt_dev->count = pg_count_d;
3645 pkt_dev->sofar = 0;
3646 pkt_dev->udp_src_min = 9; /* sink port */
3647 pkt_dev->udp_src_max = 9;
3648 pkt_dev->udp_dst_min = 9;
3649 pkt_dev->udp_dst_max = 9;
3651 pkt_dev->vlan_p = 0;
3652 pkt_dev->vlan_cfi = 0;
3653 pkt_dev->vlan_id = 0xffff;
3654 pkt_dev->svlan_p = 0;
3655 pkt_dev->svlan_cfi = 0;
3656 pkt_dev->svlan_id = 0xffff;
3658 err = pktgen_setup_dev(pkt_dev, ifname);
3659 if (err)
3660 goto out1;
3662 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3663 &pktgen_if_fops, pkt_dev);
3664 if (!pkt_dev->entry) {
3665 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3666 PG_PROC_DIR, ifname);
3667 err = -EINVAL;
3668 goto out2;
3670 #ifdef CONFIG_XFRM
3671 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3672 pkt_dev->ipsproto = IPPROTO_ESP;
3673 #endif
3675 return add_dev_to_thread(t, pkt_dev);
3676 out2:
3677 dev_put(pkt_dev->odev);
3678 out1:
3679 #ifdef CONFIG_XFRM
3680 free_SAs(pkt_dev);
3681 #endif
3682 vfree(pkt_dev->flows);
3683 kfree(pkt_dev);
3684 return err;
3687 static int __init pktgen_create_thread(int cpu)
3689 struct pktgen_thread *t;
3690 struct proc_dir_entry *pe;
3691 struct task_struct *p;
3693 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3694 if (!t) {
3695 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3696 "create new thread.\n");
3697 return -ENOMEM;
3700 spin_lock_init(&t->if_lock);
3701 t->cpu = cpu;
3703 INIT_LIST_HEAD(&t->if_list);
3705 list_add_tail(&t->th_list, &pktgen_threads);
3706 init_completion(&t->start_done);
3708 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3709 if (IS_ERR(p)) {
3710 printk(KERN_ERR "pktgen: kernel_thread() failed "
3711 "for cpu %d\n", t->cpu);
3712 list_del(&t->th_list);
3713 kfree(t);
3714 return PTR_ERR(p);
3716 kthread_bind(p, cpu);
3717 t->tsk = p;
3719 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3720 &pktgen_thread_fops, t);
3721 if (!pe) {
3722 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3723 PG_PROC_DIR, t->tsk->comm);
3724 kthread_stop(p);
3725 list_del(&t->th_list);
3726 kfree(t);
3727 return -EINVAL;
3730 wake_up_process(p);
3731 wait_for_completion(&t->start_done);
3733 return 0;
3737 * Removes a device from the thread if_list.
3739 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3740 struct pktgen_dev *pkt_dev)
3742 struct list_head *q, *n;
3743 struct pktgen_dev *p;
3745 list_for_each_safe(q, n, &t->if_list) {
3746 p = list_entry(q, struct pktgen_dev, list);
3747 if (p == pkt_dev)
3748 list_del(&p->list);
3752 static int pktgen_remove_device(struct pktgen_thread *t,
3753 struct pktgen_dev *pkt_dev)
3756 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3758 if (pkt_dev->running) {
3759 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3760 "running interface, stopping it now.\n");
3761 pktgen_stop_device(pkt_dev);
3764 /* Dis-associate from the interface */
3766 if (pkt_dev->odev) {
3767 dev_put(pkt_dev->odev);
3768 pkt_dev->odev = NULL;
3771 /* And update the thread if_list */
3773 _rem_dev_from_if_list(t, pkt_dev);
3775 if (pkt_dev->entry)
3776 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3778 #ifdef CONFIG_XFRM
3779 free_SAs(pkt_dev);
3780 #endif
3781 vfree(pkt_dev->flows);
3782 kfree(pkt_dev);
3783 return 0;
3786 static int __init pg_init(void)
3788 int cpu;
3789 struct proc_dir_entry *pe;
3791 printk(KERN_INFO "%s", version);
3793 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3794 if (!pg_proc_dir)
3795 return -ENODEV;
3797 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3798 if (pe == NULL) {
3799 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3800 "procfs entry.\n", PGCTRL);
3801 proc_net_remove(&init_net, PG_PROC_DIR);
3802 return -EINVAL;
3805 /* Register us to receive netdevice events */
3806 register_netdevice_notifier(&pktgen_notifier_block);
3808 for_each_online_cpu(cpu) {
3809 int err;
3811 err = pktgen_create_thread(cpu);
3812 if (err)
3813 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3814 "thread for cpu %d (%d)\n", cpu, err);
3817 if (list_empty(&pktgen_threads)) {
3818 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3819 "all threads\n");
3820 unregister_netdevice_notifier(&pktgen_notifier_block);
3821 remove_proc_entry(PGCTRL, pg_proc_dir);
3822 proc_net_remove(&init_net, PG_PROC_DIR);
3823 return -ENODEV;
3826 return 0;
3829 static void __exit pg_cleanup(void)
3831 struct pktgen_thread *t;
3832 struct list_head *q, *n;
3833 wait_queue_head_t queue;
3834 init_waitqueue_head(&queue);
3836 /* Stop all interfaces & threads */
3838 list_for_each_safe(q, n, &pktgen_threads) {
3839 t = list_entry(q, struct pktgen_thread, th_list);
3840 kthread_stop(t->tsk);
3841 kfree(t);
3844 /* Un-register us from receiving netdevice events */
3845 unregister_netdevice_notifier(&pktgen_notifier_block);
3847 /* Clean up proc file system */
3848 remove_proc_entry(PGCTRL, pg_proc_dir);
3849 proc_net_remove(&init_net, PG_PROC_DIR);
3852 module_init(pg_init);
3853 module_exit(pg_cleanup);
3855 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3856 MODULE_DESCRIPTION("Packet Generator tool");
3857 MODULE_LICENSE("GPL");
3858 module_param(pg_count_d, int, 0);
3859 module_param(pg_delay_d, int, 0);
3860 module_param(pg_clone_skb_d, int, 0);
3861 module_param(debug, int, 0);