x86: cpa: move clflush_cache_range()
[wrt350n-kernel.git] / net / core / pktgen.c
blobeebccdbdbacafb1c913be9dc6ec51b57875afc46
1 /*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
23 * Additional hacking by:
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
52 * clones.
54 * Also moved to /proc/net/pktgen/
55 * --ro
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
67 * The new operation:
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * to /proc gives result code thats should be read be the "writer".
78 * For practical use this should be no problem.
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
82 * --ro
84 * Fix refcount off by one if first packet fails, potential null deref,
85 * memleak 030710- KJP
87 * First "ranges" functionality for ipv6 030726 --ro
89 * Included flow support. 030802 ANK.
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
99 * Randy Dunlap fixed u64 printk compiler waring
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
108 * 050103
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/freezer.h>
135 #include <linux/delay.h>
136 #include <linux/timer.h>
137 #include <linux/list.h>
138 #include <linux/init.h>
139 #include <linux/skbuff.h>
140 #include <linux/netdevice.h>
141 #include <linux/inet.h>
142 #include <linux/inetdevice.h>
143 #include <linux/rtnetlink.h>
144 #include <linux/if_arp.h>
145 #include <linux/if_vlan.h>
146 #include <linux/in.h>
147 #include <linux/ip.h>
148 #include <linux/ipv6.h>
149 #include <linux/udp.h>
150 #include <linux/proc_fs.h>
151 #include <linux/seq_file.h>
152 #include <linux/wait.h>
153 #include <linux/etherdevice.h>
154 #include <linux/kthread.h>
155 #include <net/net_namespace.h>
156 #include <net/checksum.h>
157 #include <net/ipv6.h>
158 #include <net/addrconf.h>
159 #ifdef CONFIG_XFRM
160 #include <net/xfrm.h>
161 #endif
162 #include <asm/byteorder.h>
163 #include <linux/rcupdate.h>
164 #include <linux/bitops.h>
165 #include <asm/io.h>
166 #include <asm/dma.h>
167 #include <asm/uaccess.h>
168 #include <asm/div64.h> /* do_div */
169 #include <asm/timex.h>
171 #define VERSION "pktgen v2.69: Packet Generator for packet performance testing.\n"
173 /* The buckets are exponential in 'width' */
174 #define LAT_BUCKETS_MAX 32
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 */
195 /* Thread control flag bits */
196 #define T_TERMINATE (1<<0)
197 #define T_STOP (1<<1) /* Stop run */
198 #define T_RUN (1<<2) /* Start run */
199 #define T_REMDEVALL (1<<3) /* Remove all devs */
200 #define T_REMDEV (1<<4) /* Remove one dev */
202 /* If lock -- can be removed after some work */
203 #define if_lock(t) spin_lock(&(t->if_lock));
204 #define if_unlock(t) spin_unlock(&(t->if_lock));
206 /* Used to help with determining the pkts on receive */
207 #define PKTGEN_MAGIC 0xbe9be955
208 #define PG_PROC_DIR "pktgen"
209 #define PGCTRL "pgctrl"
210 static struct proc_dir_entry *pg_proc_dir = NULL;
212 #define MAX_CFLOWS 65536
214 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
215 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
217 struct flow_state {
218 __be32 cur_daddr;
219 int count;
220 #ifdef CONFIG_XFRM
221 struct xfrm_state *x;
222 #endif
223 __u32 flags;
226 /* flow flag bits */
227 #define F_INIT (1<<0) /* flow has been initialized */
229 struct pktgen_dev {
231 * Try to keep frequent/infrequent used vars. separated.
233 struct proc_dir_entry *entry; /* proc file */
234 struct pktgen_thread *pg_thread;/* the owner */
235 struct list_head list; /* Used for chaining in the thread's run-queue */
237 int running; /* if this changes to false, the test will stop */
239 /* If min != max, then we will either do a linear iteration, or
240 * we will do a random selection from within the range.
242 __u32 flags;
243 int removal_mark; /* non-zero => the device is marked for
244 * removal by worker thread */
246 int min_pkt_size; /* = ETH_ZLEN; */
247 int max_pkt_size; /* = ETH_ZLEN; */
248 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
249 int nfrags;
250 __u32 delay_us; /* Default delay */
251 __u32 delay_ns;
252 __u64 count; /* Default No packets to send */
253 __u64 sofar; /* How many pkts we've sent so far */
254 __u64 tx_bytes; /* How many bytes we've transmitted */
255 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
257 /* runtime counters relating to clone_skb */
258 __u64 next_tx_us; /* timestamp of when to tx next */
259 __u32 next_tx_ns;
261 __u64 allocated_skbs;
262 __u32 clone_count;
263 int last_ok; /* Was last skb sent?
264 * Or a failed transmit of some sort? This will keep
265 * sequence numbers in order, for example.
267 __u64 started_at; /* micro-seconds */
268 __u64 stopped_at; /* micro-seconds */
269 __u64 idle_acc; /* micro-seconds */
270 __u32 seq_num;
272 int clone_skb; /* Use multiple SKBs during packet gen. If this number
273 * is greater than 1, then that many copies of the same
274 * packet will be sent before a new packet is allocated.
275 * For instance, if you want to send 1024 identical packets
276 * before creating a new packet, set clone_skb to 1024.
279 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
284 struct in6_addr in6_saddr;
285 struct in6_addr in6_daddr;
286 struct in6_addr cur_in6_daddr;
287 struct in6_addr cur_in6_saddr;
288 /* For ranges */
289 struct in6_addr min_in6_daddr;
290 struct in6_addr max_in6_daddr;
291 struct in6_addr min_in6_saddr;
292 struct in6_addr max_in6_saddr;
294 /* If we're doing ranges, random or incremental, then this
295 * defines the min/max for those ranges.
297 __be32 saddr_min; /* inclusive, source IP address */
298 __be32 saddr_max; /* exclusive, source IP address */
299 __be32 daddr_min; /* inclusive, dest IP address */
300 __be32 daddr_max; /* exclusive, dest IP address */
302 __u16 udp_src_min; /* inclusive, source UDP port */
303 __u16 udp_src_max; /* exclusive, source UDP port */
304 __u16 udp_dst_min; /* inclusive, dest UDP port */
305 __u16 udp_dst_max; /* exclusive, dest UDP port */
307 /* DSCP + ECN */
308 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
309 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
311 /* MPLS */
312 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
313 __be32 labels[MAX_MPLS_LABELS];
315 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
316 __u8 vlan_p;
317 __u8 vlan_cfi;
318 __u16 vlan_id; /* 0xffff means no vlan tag */
320 __u8 svlan_p;
321 __u8 svlan_cfi;
322 __u16 svlan_id; /* 0xffff means no svlan tag */
324 __u32 src_mac_count; /* How many MACs to iterate through */
325 __u32 dst_mac_count; /* How many MACs to iterate through */
327 unsigned char dst_mac[ETH_ALEN];
328 unsigned char src_mac[ETH_ALEN];
330 __u32 cur_dst_mac_offset;
331 __u32 cur_src_mac_offset;
332 __be32 cur_saddr;
333 __be32 cur_daddr;
334 __u16 cur_udp_dst;
335 __u16 cur_udp_src;
336 __u16 cur_queue_map;
337 __u32 cur_pkt_size;
339 __u8 hh[14];
340 /* = {
341 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
343 We fill in SRC address later
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x08, 0x00
348 __u16 pad; /* pad out the hh struct to an even 16 bytes */
350 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
351 * are transmitting the same one multiple times
353 struct net_device *odev; /* The out-going device. Note that the device should
354 * have it's pg_info pointer pointing back to this
355 * device. This will be set when the user specifies
356 * the out-going device name (not when the inject is
357 * started as it used to do.)
359 struct flow_state *flows;
360 unsigned cflows; /* Concurrent flows (config) */
361 unsigned lflow; /* Flow length (config) */
362 unsigned nflows; /* accumulated flows (stats) */
363 unsigned curfl; /* current sequenced flow (state)*/
365 u16 queue_map_min;
366 u16 queue_map_max;
368 #ifdef CONFIG_XFRM
369 __u8 ipsmode; /* IPSEC mode (config) */
370 __u8 ipsproto; /* IPSEC type (config) */
371 #endif
372 char result[512];
375 struct pktgen_hdr {
376 __be32 pgh_magic;
377 __be32 seq_num;
378 __be32 tv_sec;
379 __be32 tv_usec;
382 struct pktgen_thread {
383 spinlock_t if_lock;
384 struct list_head if_list; /* All device here */
385 struct list_head th_list;
386 struct task_struct *tsk;
387 char result[512];
389 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
391 u32 control;
392 int cpu;
394 wait_queue_head_t queue;
397 #define REMOVE 1
398 #define FIND 0
400 /** Convert to micro-seconds */
401 static inline __u64 tv_to_us(const struct timeval *tv)
403 __u64 us = tv->tv_usec;
404 us += (__u64) tv->tv_sec * (__u64) 1000000;
405 return us;
408 static __u64 getCurUs(void)
410 struct timeval tv;
411 do_gettimeofday(&tv);
412 return tv_to_us(&tv);
415 /* old include end */
417 static char version[] __initdata = VERSION;
419 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
420 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
421 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
422 const char *ifname);
423 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
424 static void pktgen_run_all_threads(void);
425 static void pktgen_stop_all_threads_ifs(void);
426 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
427 static void pktgen_stop(struct pktgen_thread *t);
428 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
430 static unsigned int scan_ip6(const char *s, char ip[16]);
431 static unsigned int fmt_ip6(char *s, const char ip[16]);
433 /* Module parameters, defaults. */
434 static int pg_count_d = 1000; /* 1000 pkts by default */
435 static int pg_delay_d;
436 static int pg_clone_skb_d;
437 static int debug;
439 static DEFINE_MUTEX(pktgen_thread_lock);
440 static LIST_HEAD(pktgen_threads);
442 static struct notifier_block pktgen_notifier_block = {
443 .notifier_call = pktgen_device_event,
447 * /proc handling functions
451 static int pgctrl_show(struct seq_file *seq, void *v)
453 seq_puts(seq, VERSION);
454 return 0;
457 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
458 size_t count, loff_t * ppos)
460 int err = 0;
461 char data[128];
463 if (!capable(CAP_NET_ADMIN)) {
464 err = -EPERM;
465 goto out;
468 if (count > sizeof(data))
469 count = sizeof(data);
471 if (copy_from_user(data, buf, count)) {
472 err = -EFAULT;
473 goto out;
475 data[count - 1] = 0; /* Make string */
477 if (!strcmp(data, "stop"))
478 pktgen_stop_all_threads_ifs();
480 else if (!strcmp(data, "start"))
481 pktgen_run_all_threads();
483 else
484 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
486 err = count;
488 out:
489 return err;
492 static int pgctrl_open(struct inode *inode, struct file *file)
494 return single_open(file, pgctrl_show, PDE(inode)->data);
497 static const struct file_operations pktgen_fops = {
498 .owner = THIS_MODULE,
499 .open = pgctrl_open,
500 .read = seq_read,
501 .llseek = seq_lseek,
502 .write = pgctrl_write,
503 .release = single_release,
506 static int pktgen_if_show(struct seq_file *seq, void *v)
508 struct pktgen_dev *pkt_dev = seq->private;
509 __u64 sa;
510 __u64 stopped;
511 __u64 now = getCurUs();
512 DECLARE_MAC_BUF(mac);
514 seq_printf(seq,
515 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
516 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
517 pkt_dev->max_pkt_size);
519 seq_printf(seq,
520 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
521 pkt_dev->nfrags,
522 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
523 pkt_dev->clone_skb, pkt_dev->odev->name);
525 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
526 pkt_dev->lflow);
528 seq_printf(seq,
529 " queue_map_min: %u queue_map_max: %u\n",
530 pkt_dev->queue_map_min,
531 pkt_dev->queue_map_max);
533 if (pkt_dev->flags & F_IPV6) {
534 char b1[128], b2[128], b3[128];
535 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
536 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
537 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
538 seq_printf(seq,
539 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
540 b2, b3);
542 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
543 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
544 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
545 seq_printf(seq,
546 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
547 b2, b3);
549 } else
550 seq_printf(seq,
551 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
552 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
553 pkt_dev->src_max);
555 seq_puts(seq, " src_mac: ");
557 seq_printf(seq, "%s ",
558 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
559 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
561 seq_printf(seq, "dst_mac: ");
562 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
564 seq_printf(seq,
565 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
566 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
567 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
569 seq_printf(seq,
570 " src_mac_count: %d dst_mac_count: %d\n",
571 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
573 if (pkt_dev->nr_labels) {
574 unsigned i;
575 seq_printf(seq, " mpls: ");
576 for (i = 0; i < pkt_dev->nr_labels; i++)
577 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
578 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
581 if (pkt_dev->vlan_id != 0xffff) {
582 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
583 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
586 if (pkt_dev->svlan_id != 0xffff) {
587 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
588 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
591 if (pkt_dev->tos) {
592 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
595 if (pkt_dev->traffic_class) {
596 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
599 seq_printf(seq, " Flags: ");
601 if (pkt_dev->flags & F_IPV6)
602 seq_printf(seq, "IPV6 ");
604 if (pkt_dev->flags & F_IPSRC_RND)
605 seq_printf(seq, "IPSRC_RND ");
607 if (pkt_dev->flags & F_IPDST_RND)
608 seq_printf(seq, "IPDST_RND ");
610 if (pkt_dev->flags & F_TXSIZE_RND)
611 seq_printf(seq, "TXSIZE_RND ");
613 if (pkt_dev->flags & F_UDPSRC_RND)
614 seq_printf(seq, "UDPSRC_RND ");
616 if (pkt_dev->flags & F_UDPDST_RND)
617 seq_printf(seq, "UDPDST_RND ");
619 if (pkt_dev->flags & F_MPLS_RND)
620 seq_printf(seq, "MPLS_RND ");
622 if (pkt_dev->flags & F_QUEUE_MAP_RND)
623 seq_printf(seq, "QUEUE_MAP_RND ");
625 if (pkt_dev->cflows) {
626 if (pkt_dev->flags & F_FLOW_SEQ)
627 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
628 else
629 seq_printf(seq, "FLOW_RND ");
632 #ifdef CONFIG_XFRM
633 if (pkt_dev->flags & F_IPSEC_ON)
634 seq_printf(seq, "IPSEC ");
635 #endif
637 if (pkt_dev->flags & F_MACSRC_RND)
638 seq_printf(seq, "MACSRC_RND ");
640 if (pkt_dev->flags & F_MACDST_RND)
641 seq_printf(seq, "MACDST_RND ");
643 if (pkt_dev->flags & F_VID_RND)
644 seq_printf(seq, "VID_RND ");
646 if (pkt_dev->flags & F_SVID_RND)
647 seq_printf(seq, "SVID_RND ");
649 seq_puts(seq, "\n");
651 sa = pkt_dev->started_at;
652 stopped = pkt_dev->stopped_at;
653 if (pkt_dev->running)
654 stopped = now; /* not really stopped, more like last-running-at */
656 seq_printf(seq,
657 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
658 (unsigned long long)pkt_dev->sofar,
659 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
660 (unsigned long long)stopped,
661 (unsigned long long)pkt_dev->idle_acc);
663 seq_printf(seq,
664 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
665 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
666 pkt_dev->cur_src_mac_offset);
668 if (pkt_dev->flags & F_IPV6) {
669 char b1[128], b2[128];
670 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
671 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
672 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
673 } else
674 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
675 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
677 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
678 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
680 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
682 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
684 if (pkt_dev->result[0])
685 seq_printf(seq, "Result: %s\n", pkt_dev->result);
686 else
687 seq_printf(seq, "Result: Idle\n");
689 return 0;
693 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
695 int i = 0;
696 *num = 0;
698 for (; i < maxlen; i++) {
699 char c;
700 *num <<= 4;
701 if (get_user(c, &user_buffer[i]))
702 return -EFAULT;
703 if ((c >= '0') && (c <= '9'))
704 *num |= c - '0';
705 else if ((c >= 'a') && (c <= 'f'))
706 *num |= c - 'a' + 10;
707 else if ((c >= 'A') && (c <= 'F'))
708 *num |= c - 'A' + 10;
709 else
710 break;
712 return i;
715 static int count_trail_chars(const char __user * user_buffer,
716 unsigned int maxlen)
718 int i;
720 for (i = 0; i < maxlen; i++) {
721 char c;
722 if (get_user(c, &user_buffer[i]))
723 return -EFAULT;
724 switch (c) {
725 case '\"':
726 case '\n':
727 case '\r':
728 case '\t':
729 case ' ':
730 case '=':
731 break;
732 default:
733 goto done;
736 done:
737 return i;
740 static unsigned long num_arg(const char __user * user_buffer,
741 unsigned long maxlen, unsigned long *num)
743 int i = 0;
744 *num = 0;
746 for (; i < maxlen; i++) {
747 char c;
748 if (get_user(c, &user_buffer[i]))
749 return -EFAULT;
750 if ((c >= '0') && (c <= '9')) {
751 *num *= 10;
752 *num += c - '0';
753 } else
754 break;
756 return i;
759 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
761 int i = 0;
763 for (; i < maxlen; i++) {
764 char c;
765 if (get_user(c, &user_buffer[i]))
766 return -EFAULT;
767 switch (c) {
768 case '\"':
769 case '\n':
770 case '\r':
771 case '\t':
772 case ' ':
773 goto done_str;
774 break;
775 default:
776 break;
779 done_str:
780 return i;
783 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
785 unsigned n = 0;
786 char c;
787 ssize_t i = 0;
788 int len;
790 pkt_dev->nr_labels = 0;
791 do {
792 __u32 tmp;
793 len = hex32_arg(&buffer[i], 8, &tmp);
794 if (len <= 0)
795 return len;
796 pkt_dev->labels[n] = htonl(tmp);
797 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
798 pkt_dev->flags |= F_MPLS_RND;
799 i += len;
800 if (get_user(c, &buffer[i]))
801 return -EFAULT;
802 i++;
803 n++;
804 if (n >= MAX_MPLS_LABELS)
805 return -E2BIG;
806 } while (c == ',');
808 pkt_dev->nr_labels = n;
809 return i;
812 static ssize_t pktgen_if_write(struct file *file,
813 const char __user * user_buffer, size_t count,
814 loff_t * offset)
816 struct seq_file *seq = (struct seq_file *)file->private_data;
817 struct pktgen_dev *pkt_dev = seq->private;
818 int i = 0, max, len;
819 char name[16], valstr[32];
820 unsigned long value = 0;
821 char *pg_result = NULL;
822 int tmp = 0;
823 char buf[128];
825 pg_result = &(pkt_dev->result[0]);
827 if (count < 1) {
828 printk(KERN_WARNING "pktgen: wrong command format\n");
829 return -EINVAL;
832 max = count - i;
833 tmp = count_trail_chars(&user_buffer[i], max);
834 if (tmp < 0) {
835 printk(KERN_WARNING "pktgen: illegal format\n");
836 return tmp;
838 i += tmp;
840 /* Read variable name */
842 len = strn_len(&user_buffer[i], sizeof(name) - 1);
843 if (len < 0) {
844 return len;
846 memset(name, 0, sizeof(name));
847 if (copy_from_user(name, &user_buffer[i], len))
848 return -EFAULT;
849 i += len;
851 max = count - i;
852 len = count_trail_chars(&user_buffer[i], max);
853 if (len < 0)
854 return len;
856 i += len;
858 if (debug) {
859 char tb[count + 1];
860 if (copy_from_user(tb, user_buffer, count))
861 return -EFAULT;
862 tb[count] = 0;
863 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
864 (unsigned long)count, tb);
867 if (!strcmp(name, "min_pkt_size")) {
868 len = num_arg(&user_buffer[i], 10, &value);
869 if (len < 0) {
870 return len;
872 i += len;
873 if (value < 14 + 20 + 8)
874 value = 14 + 20 + 8;
875 if (value != pkt_dev->min_pkt_size) {
876 pkt_dev->min_pkt_size = value;
877 pkt_dev->cur_pkt_size = value;
879 sprintf(pg_result, "OK: min_pkt_size=%u",
880 pkt_dev->min_pkt_size);
881 return count;
884 if (!strcmp(name, "max_pkt_size")) {
885 len = num_arg(&user_buffer[i], 10, &value);
886 if (len < 0) {
887 return len;
889 i += len;
890 if (value < 14 + 20 + 8)
891 value = 14 + 20 + 8;
892 if (value != pkt_dev->max_pkt_size) {
893 pkt_dev->max_pkt_size = value;
894 pkt_dev->cur_pkt_size = value;
896 sprintf(pg_result, "OK: max_pkt_size=%u",
897 pkt_dev->max_pkt_size);
898 return count;
901 /* Shortcut for min = max */
903 if (!strcmp(name, "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->min_pkt_size) {
912 pkt_dev->min_pkt_size = value;
913 pkt_dev->max_pkt_size = value;
914 pkt_dev->cur_pkt_size = value;
916 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
917 return count;
920 if (!strcmp(name, "debug")) {
921 len = num_arg(&user_buffer[i], 10, &value);
922 if (len < 0) {
923 return len;
925 i += len;
926 debug = value;
927 sprintf(pg_result, "OK: debug=%u", debug);
928 return count;
931 if (!strcmp(name, "frags")) {
932 len = num_arg(&user_buffer[i], 10, &value);
933 if (len < 0) {
934 return len;
936 i += len;
937 pkt_dev->nfrags = value;
938 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
939 return count;
941 if (!strcmp(name, "delay")) {
942 len = num_arg(&user_buffer[i], 10, &value);
943 if (len < 0) {
944 return len;
946 i += len;
947 if (value == 0x7FFFFFFF) {
948 pkt_dev->delay_us = 0x7FFFFFFF;
949 pkt_dev->delay_ns = 0;
950 } else {
951 pkt_dev->delay_us = value / 1000;
952 pkt_dev->delay_ns = value % 1000;
954 sprintf(pg_result, "OK: delay=%u",
955 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
956 return count;
958 if (!strcmp(name, "udp_src_min")) {
959 len = num_arg(&user_buffer[i], 10, &value);
960 if (len < 0) {
961 return len;
963 i += len;
964 if (value != pkt_dev->udp_src_min) {
965 pkt_dev->udp_src_min = value;
966 pkt_dev->cur_udp_src = value;
968 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
969 return count;
971 if (!strcmp(name, "udp_dst_min")) {
972 len = num_arg(&user_buffer[i], 10, &value);
973 if (len < 0) {
974 return len;
976 i += len;
977 if (value != pkt_dev->udp_dst_min) {
978 pkt_dev->udp_dst_min = value;
979 pkt_dev->cur_udp_dst = value;
981 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
982 return count;
984 if (!strcmp(name, "udp_src_max")) {
985 len = num_arg(&user_buffer[i], 10, &value);
986 if (len < 0) {
987 return len;
989 i += len;
990 if (value != pkt_dev->udp_src_max) {
991 pkt_dev->udp_src_max = value;
992 pkt_dev->cur_udp_src = value;
994 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
995 return count;
997 if (!strcmp(name, "udp_dst_max")) {
998 len = num_arg(&user_buffer[i], 10, &value);
999 if (len < 0) {
1000 return len;
1002 i += len;
1003 if (value != pkt_dev->udp_dst_max) {
1004 pkt_dev->udp_dst_max = value;
1005 pkt_dev->cur_udp_dst = value;
1007 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1008 return count;
1010 if (!strcmp(name, "clone_skb")) {
1011 len = num_arg(&user_buffer[i], 10, &value);
1012 if (len < 0) {
1013 return len;
1015 i += len;
1016 pkt_dev->clone_skb = value;
1018 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1019 return count;
1021 if (!strcmp(name, "count")) {
1022 len = num_arg(&user_buffer[i], 10, &value);
1023 if (len < 0) {
1024 return len;
1026 i += len;
1027 pkt_dev->count = value;
1028 sprintf(pg_result, "OK: count=%llu",
1029 (unsigned long long)pkt_dev->count);
1030 return count;
1032 if (!strcmp(name, "src_mac_count")) {
1033 len = num_arg(&user_buffer[i], 10, &value);
1034 if (len < 0) {
1035 return len;
1037 i += len;
1038 if (pkt_dev->src_mac_count != value) {
1039 pkt_dev->src_mac_count = value;
1040 pkt_dev->cur_src_mac_offset = 0;
1042 sprintf(pg_result, "OK: src_mac_count=%d",
1043 pkt_dev->src_mac_count);
1044 return count;
1046 if (!strcmp(name, "dst_mac_count")) {
1047 len = num_arg(&user_buffer[i], 10, &value);
1048 if (len < 0) {
1049 return len;
1051 i += len;
1052 if (pkt_dev->dst_mac_count != value) {
1053 pkt_dev->dst_mac_count = value;
1054 pkt_dev->cur_dst_mac_offset = 0;
1056 sprintf(pg_result, "OK: dst_mac_count=%d",
1057 pkt_dev->dst_mac_count);
1058 return count;
1060 if (!strcmp(name, "flag")) {
1061 char f[32];
1062 memset(f, 0, 32);
1063 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1064 if (len < 0) {
1065 return len;
1067 if (copy_from_user(f, &user_buffer[i], len))
1068 return -EFAULT;
1069 i += len;
1070 if (strcmp(f, "IPSRC_RND") == 0)
1071 pkt_dev->flags |= F_IPSRC_RND;
1073 else if (strcmp(f, "!IPSRC_RND") == 0)
1074 pkt_dev->flags &= ~F_IPSRC_RND;
1076 else if (strcmp(f, "TXSIZE_RND") == 0)
1077 pkt_dev->flags |= F_TXSIZE_RND;
1079 else if (strcmp(f, "!TXSIZE_RND") == 0)
1080 pkt_dev->flags &= ~F_TXSIZE_RND;
1082 else if (strcmp(f, "IPDST_RND") == 0)
1083 pkt_dev->flags |= F_IPDST_RND;
1085 else if (strcmp(f, "!IPDST_RND") == 0)
1086 pkt_dev->flags &= ~F_IPDST_RND;
1088 else if (strcmp(f, "UDPSRC_RND") == 0)
1089 pkt_dev->flags |= F_UDPSRC_RND;
1091 else if (strcmp(f, "!UDPSRC_RND") == 0)
1092 pkt_dev->flags &= ~F_UDPSRC_RND;
1094 else if (strcmp(f, "UDPDST_RND") == 0)
1095 pkt_dev->flags |= F_UDPDST_RND;
1097 else if (strcmp(f, "!UDPDST_RND") == 0)
1098 pkt_dev->flags &= ~F_UDPDST_RND;
1100 else if (strcmp(f, "MACSRC_RND") == 0)
1101 pkt_dev->flags |= F_MACSRC_RND;
1103 else if (strcmp(f, "!MACSRC_RND") == 0)
1104 pkt_dev->flags &= ~F_MACSRC_RND;
1106 else if (strcmp(f, "MACDST_RND") == 0)
1107 pkt_dev->flags |= F_MACDST_RND;
1109 else if (strcmp(f, "!MACDST_RND") == 0)
1110 pkt_dev->flags &= ~F_MACDST_RND;
1112 else if (strcmp(f, "MPLS_RND") == 0)
1113 pkt_dev->flags |= F_MPLS_RND;
1115 else if (strcmp(f, "!MPLS_RND") == 0)
1116 pkt_dev->flags &= ~F_MPLS_RND;
1118 else if (strcmp(f, "VID_RND") == 0)
1119 pkt_dev->flags |= F_VID_RND;
1121 else if (strcmp(f, "!VID_RND") == 0)
1122 pkt_dev->flags &= ~F_VID_RND;
1124 else if (strcmp(f, "SVID_RND") == 0)
1125 pkt_dev->flags |= F_SVID_RND;
1127 else if (strcmp(f, "!SVID_RND") == 0)
1128 pkt_dev->flags &= ~F_SVID_RND;
1130 else if (strcmp(f, "FLOW_SEQ") == 0)
1131 pkt_dev->flags |= F_FLOW_SEQ;
1133 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1134 pkt_dev->flags |= F_QUEUE_MAP_RND;
1136 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1137 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1138 #ifdef CONFIG_XFRM
1139 else if (strcmp(f, "IPSEC") == 0)
1140 pkt_dev->flags |= F_IPSEC_ON;
1141 #endif
1143 else if (strcmp(f, "!IPV6") == 0)
1144 pkt_dev->flags &= ~F_IPV6;
1146 else {
1147 sprintf(pg_result,
1148 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1150 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1151 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1152 return count;
1154 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1155 return count;
1157 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1158 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1159 if (len < 0) {
1160 return len;
1163 if (copy_from_user(buf, &user_buffer[i], len))
1164 return -EFAULT;
1165 buf[len] = 0;
1166 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1167 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1168 strncpy(pkt_dev->dst_min, buf, len);
1169 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1170 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1172 if (debug)
1173 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1174 pkt_dev->dst_min);
1175 i += len;
1176 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1177 return count;
1179 if (!strcmp(name, "dst_max")) {
1180 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1181 if (len < 0) {
1182 return len;
1185 if (copy_from_user(buf, &user_buffer[i], len))
1186 return -EFAULT;
1188 buf[len] = 0;
1189 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1190 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1191 strncpy(pkt_dev->dst_max, buf, len);
1192 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1193 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1195 if (debug)
1196 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1197 pkt_dev->dst_max);
1198 i += len;
1199 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1200 return count;
1202 if (!strcmp(name, "dst6")) {
1203 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1204 if (len < 0)
1205 return len;
1207 pkt_dev->flags |= F_IPV6;
1209 if (copy_from_user(buf, &user_buffer[i], len))
1210 return -EFAULT;
1211 buf[len] = 0;
1213 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1214 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1216 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1218 if (debug)
1219 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1221 i += len;
1222 sprintf(pg_result, "OK: dst6=%s", buf);
1223 return count;
1225 if (!strcmp(name, "dst6_min")) {
1226 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1227 if (len < 0)
1228 return len;
1230 pkt_dev->flags |= F_IPV6;
1232 if (copy_from_user(buf, &user_buffer[i], len))
1233 return -EFAULT;
1234 buf[len] = 0;
1236 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1237 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1239 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1240 &pkt_dev->min_in6_daddr);
1241 if (debug)
1242 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1244 i += len;
1245 sprintf(pg_result, "OK: dst6_min=%s", buf);
1246 return count;
1248 if (!strcmp(name, "dst6_max")) {
1249 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1250 if (len < 0)
1251 return len;
1253 pkt_dev->flags |= F_IPV6;
1255 if (copy_from_user(buf, &user_buffer[i], len))
1256 return -EFAULT;
1257 buf[len] = 0;
1259 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1260 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1262 if (debug)
1263 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1265 i += len;
1266 sprintf(pg_result, "OK: dst6_max=%s", buf);
1267 return count;
1269 if (!strcmp(name, "src6")) {
1270 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1271 if (len < 0)
1272 return len;
1274 pkt_dev->flags |= F_IPV6;
1276 if (copy_from_user(buf, &user_buffer[i], len))
1277 return -EFAULT;
1278 buf[len] = 0;
1280 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1281 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1283 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1285 if (debug)
1286 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1288 i += len;
1289 sprintf(pg_result, "OK: src6=%s", buf);
1290 return count;
1292 if (!strcmp(name, "src_min")) {
1293 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1294 if (len < 0) {
1295 return len;
1297 if (copy_from_user(buf, &user_buffer[i], len))
1298 return -EFAULT;
1299 buf[len] = 0;
1300 if (strcmp(buf, pkt_dev->src_min) != 0) {
1301 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1302 strncpy(pkt_dev->src_min, buf, len);
1303 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1304 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1306 if (debug)
1307 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1308 pkt_dev->src_min);
1309 i += len;
1310 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1311 return count;
1313 if (!strcmp(name, "src_max")) {
1314 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1315 if (len < 0) {
1316 return len;
1318 if (copy_from_user(buf, &user_buffer[i], len))
1319 return -EFAULT;
1320 buf[len] = 0;
1321 if (strcmp(buf, pkt_dev->src_max) != 0) {
1322 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1323 strncpy(pkt_dev->src_max, buf, len);
1324 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1325 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1327 if (debug)
1328 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1329 pkt_dev->src_max);
1330 i += len;
1331 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1332 return count;
1334 if (!strcmp(name, "dst_mac")) {
1335 char *v = valstr;
1336 unsigned char old_dmac[ETH_ALEN];
1337 unsigned char *m = pkt_dev->dst_mac;
1338 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1340 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1341 if (len < 0) {
1342 return len;
1344 memset(valstr, 0, sizeof(valstr));
1345 if (copy_from_user(valstr, &user_buffer[i], len))
1346 return -EFAULT;
1347 i += len;
1349 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1350 if (*v >= '0' && *v <= '9') {
1351 *m *= 16;
1352 *m += *v - '0';
1354 if (*v >= 'A' && *v <= 'F') {
1355 *m *= 16;
1356 *m += *v - 'A' + 10;
1358 if (*v >= 'a' && *v <= 'f') {
1359 *m *= 16;
1360 *m += *v - 'a' + 10;
1362 if (*v == ':') {
1363 m++;
1364 *m = 0;
1368 /* Set up Dest MAC */
1369 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1370 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1372 sprintf(pg_result, "OK: dstmac");
1373 return count;
1375 if (!strcmp(name, "src_mac")) {
1376 char *v = valstr;
1377 unsigned char old_smac[ETH_ALEN];
1378 unsigned char *m = pkt_dev->src_mac;
1380 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1382 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1383 if (len < 0) {
1384 return len;
1386 memset(valstr, 0, sizeof(valstr));
1387 if (copy_from_user(valstr, &user_buffer[i], len))
1388 return -EFAULT;
1389 i += len;
1391 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1392 if (*v >= '0' && *v <= '9') {
1393 *m *= 16;
1394 *m += *v - '0';
1396 if (*v >= 'A' && *v <= 'F') {
1397 *m *= 16;
1398 *m += *v - 'A' + 10;
1400 if (*v >= 'a' && *v <= 'f') {
1401 *m *= 16;
1402 *m += *v - 'a' + 10;
1404 if (*v == ':') {
1405 m++;
1406 *m = 0;
1410 /* Set up Src MAC */
1411 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1412 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1414 sprintf(pg_result, "OK: srcmac");
1415 return count;
1418 if (!strcmp(name, "clear_counters")) {
1419 pktgen_clear_counters(pkt_dev);
1420 sprintf(pg_result, "OK: Clearing counters.\n");
1421 return count;
1424 if (!strcmp(name, "flows")) {
1425 len = num_arg(&user_buffer[i], 10, &value);
1426 if (len < 0) {
1427 return len;
1429 i += len;
1430 if (value > MAX_CFLOWS)
1431 value = MAX_CFLOWS;
1433 pkt_dev->cflows = value;
1434 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1435 return count;
1438 if (!strcmp(name, "flowlen")) {
1439 len = num_arg(&user_buffer[i], 10, &value);
1440 if (len < 0) {
1441 return len;
1443 i += len;
1444 pkt_dev->lflow = value;
1445 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1446 return count;
1449 if (!strcmp(name, "queue_map_min")) {
1450 len = num_arg(&user_buffer[i], 5, &value);
1451 if (len < 0) {
1452 return len;
1454 i += len;
1455 pkt_dev->queue_map_min = value;
1456 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1457 return count;
1460 if (!strcmp(name, "queue_map_max")) {
1461 len = num_arg(&user_buffer[i], 5, &value);
1462 if (len < 0) {
1463 return len;
1465 i += len;
1466 pkt_dev->queue_map_max = value;
1467 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1468 return count;
1471 if (!strcmp(name, "mpls")) {
1472 unsigned n, cnt;
1474 len = get_labels(&user_buffer[i], pkt_dev);
1475 if (len < 0)
1476 return len;
1477 i += len;
1478 cnt = sprintf(pg_result, "OK: mpls=");
1479 for (n = 0; n < pkt_dev->nr_labels; n++)
1480 cnt += sprintf(pg_result + cnt,
1481 "%08x%s", ntohl(pkt_dev->labels[n]),
1482 n == pkt_dev->nr_labels-1 ? "" : ",");
1484 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1485 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1486 pkt_dev->svlan_id = 0xffff;
1488 if (debug)
1489 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1491 return count;
1494 if (!strcmp(name, "vlan_id")) {
1495 len = num_arg(&user_buffer[i], 4, &value);
1496 if (len < 0) {
1497 return len;
1499 i += len;
1500 if (value <= 4095) {
1501 pkt_dev->vlan_id = value; /* turn on VLAN */
1503 if (debug)
1504 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1506 if (debug && pkt_dev->nr_labels)
1507 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1509 pkt_dev->nr_labels = 0; /* turn off MPLS */
1510 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1511 } else {
1512 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1513 pkt_dev->svlan_id = 0xffff;
1515 if (debug)
1516 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1518 return count;
1521 if (!strcmp(name, "vlan_p")) {
1522 len = num_arg(&user_buffer[i], 1, &value);
1523 if (len < 0) {
1524 return len;
1526 i += len;
1527 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1528 pkt_dev->vlan_p = value;
1529 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1530 } else {
1531 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1533 return count;
1536 if (!strcmp(name, "vlan_cfi")) {
1537 len = num_arg(&user_buffer[i], 1, &value);
1538 if (len < 0) {
1539 return len;
1541 i += len;
1542 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1543 pkt_dev->vlan_cfi = value;
1544 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1545 } else {
1546 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1548 return count;
1551 if (!strcmp(name, "svlan_id")) {
1552 len = num_arg(&user_buffer[i], 4, &value);
1553 if (len < 0) {
1554 return len;
1556 i += len;
1557 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1558 pkt_dev->svlan_id = value; /* turn on SVLAN */
1560 if (debug)
1561 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1563 if (debug && pkt_dev->nr_labels)
1564 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1566 pkt_dev->nr_labels = 0; /* turn off MPLS */
1567 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1568 } else {
1569 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1570 pkt_dev->svlan_id = 0xffff;
1572 if (debug)
1573 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1575 return count;
1578 if (!strcmp(name, "svlan_p")) {
1579 len = num_arg(&user_buffer[i], 1, &value);
1580 if (len < 0) {
1581 return len;
1583 i += len;
1584 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1585 pkt_dev->svlan_p = value;
1586 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1587 } else {
1588 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1590 return count;
1593 if (!strcmp(name, "svlan_cfi")) {
1594 len = num_arg(&user_buffer[i], 1, &value);
1595 if (len < 0) {
1596 return len;
1598 i += len;
1599 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1600 pkt_dev->svlan_cfi = value;
1601 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1602 } else {
1603 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1605 return count;
1608 if (!strcmp(name, "tos")) {
1609 __u32 tmp_value = 0;
1610 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1611 if (len < 0) {
1612 return len;
1614 i += len;
1615 if (len == 2) {
1616 pkt_dev->tos = tmp_value;
1617 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1618 } else {
1619 sprintf(pg_result, "ERROR: tos must be 00-ff");
1621 return count;
1624 if (!strcmp(name, "traffic_class")) {
1625 __u32 tmp_value = 0;
1626 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1627 if (len < 0) {
1628 return len;
1630 i += len;
1631 if (len == 2) {
1632 pkt_dev->traffic_class = tmp_value;
1633 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1634 } else {
1635 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1637 return count;
1640 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1641 return -EINVAL;
1644 static int pktgen_if_open(struct inode *inode, struct file *file)
1646 return single_open(file, pktgen_if_show, PDE(inode)->data);
1649 static const struct file_operations pktgen_if_fops = {
1650 .owner = THIS_MODULE,
1651 .open = pktgen_if_open,
1652 .read = seq_read,
1653 .llseek = seq_lseek,
1654 .write = pktgen_if_write,
1655 .release = single_release,
1658 static int pktgen_thread_show(struct seq_file *seq, void *v)
1660 struct pktgen_thread *t = seq->private;
1661 struct pktgen_dev *pkt_dev;
1663 BUG_ON(!t);
1665 seq_printf(seq, "Running: ");
1667 if_lock(t);
1668 list_for_each_entry(pkt_dev, &t->if_list, list)
1669 if (pkt_dev->running)
1670 seq_printf(seq, "%s ", pkt_dev->odev->name);
1672 seq_printf(seq, "\nStopped: ");
1674 list_for_each_entry(pkt_dev, &t->if_list, list)
1675 if (!pkt_dev->running)
1676 seq_printf(seq, "%s ", pkt_dev->odev->name);
1678 if (t->result[0])
1679 seq_printf(seq, "\nResult: %s\n", t->result);
1680 else
1681 seq_printf(seq, "\nResult: NA\n");
1683 if_unlock(t);
1685 return 0;
1688 static ssize_t pktgen_thread_write(struct file *file,
1689 const char __user * user_buffer,
1690 size_t count, loff_t * offset)
1692 struct seq_file *seq = (struct seq_file *)file->private_data;
1693 struct pktgen_thread *t = seq->private;
1694 int i = 0, max, len, ret;
1695 char name[40];
1696 char *pg_result;
1698 if (count < 1) {
1699 // sprintf(pg_result, "Wrong command format");
1700 return -EINVAL;
1703 max = count - i;
1704 len = count_trail_chars(&user_buffer[i], max);
1705 if (len < 0)
1706 return len;
1708 i += len;
1710 /* Read variable name */
1712 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1713 if (len < 0)
1714 return len;
1716 memset(name, 0, sizeof(name));
1717 if (copy_from_user(name, &user_buffer[i], len))
1718 return -EFAULT;
1719 i += len;
1721 max = count - i;
1722 len = count_trail_chars(&user_buffer[i], max);
1723 if (len < 0)
1724 return len;
1726 i += len;
1728 if (debug)
1729 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1730 name, (unsigned long)count);
1732 if (!t) {
1733 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1734 ret = -EINVAL;
1735 goto out;
1738 pg_result = &(t->result[0]);
1740 if (!strcmp(name, "add_device")) {
1741 char f[32];
1742 memset(f, 0, 32);
1743 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1744 if (len < 0) {
1745 ret = len;
1746 goto out;
1748 if (copy_from_user(f, &user_buffer[i], len))
1749 return -EFAULT;
1750 i += len;
1751 mutex_lock(&pktgen_thread_lock);
1752 pktgen_add_device(t, f);
1753 mutex_unlock(&pktgen_thread_lock);
1754 ret = count;
1755 sprintf(pg_result, "OK: add_device=%s", f);
1756 goto out;
1759 if (!strcmp(name, "rem_device_all")) {
1760 mutex_lock(&pktgen_thread_lock);
1761 t->control |= T_REMDEVALL;
1762 mutex_unlock(&pktgen_thread_lock);
1763 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1764 ret = count;
1765 sprintf(pg_result, "OK: rem_device_all");
1766 goto out;
1769 if (!strcmp(name, "max_before_softirq")) {
1770 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1771 ret = count;
1772 goto out;
1775 ret = -EINVAL;
1776 out:
1777 return ret;
1780 static int pktgen_thread_open(struct inode *inode, struct file *file)
1782 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1785 static const struct file_operations pktgen_thread_fops = {
1786 .owner = THIS_MODULE,
1787 .open = pktgen_thread_open,
1788 .read = seq_read,
1789 .llseek = seq_lseek,
1790 .write = pktgen_thread_write,
1791 .release = single_release,
1794 /* Think find or remove for NN */
1795 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1797 struct pktgen_thread *t;
1798 struct pktgen_dev *pkt_dev = NULL;
1800 list_for_each_entry(t, &pktgen_threads, th_list) {
1801 pkt_dev = pktgen_find_dev(t, ifname);
1802 if (pkt_dev) {
1803 if (remove) {
1804 if_lock(t);
1805 pkt_dev->removal_mark = 1;
1806 t->control |= T_REMDEV;
1807 if_unlock(t);
1809 break;
1812 return pkt_dev;
1816 * mark a device for removal
1818 static void pktgen_mark_device(const char *ifname)
1820 struct pktgen_dev *pkt_dev = NULL;
1821 const int max_tries = 10, msec_per_try = 125;
1822 int i = 0;
1824 mutex_lock(&pktgen_thread_lock);
1825 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1827 while (1) {
1829 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1830 if (pkt_dev == NULL)
1831 break; /* success */
1833 mutex_unlock(&pktgen_thread_lock);
1834 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1835 "to disappear....\n", ifname);
1836 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1837 mutex_lock(&pktgen_thread_lock);
1839 if (++i >= max_tries) {
1840 printk(KERN_ERR "pktgen_mark_device: timed out after "
1841 "waiting %d msec for device %s to be removed\n",
1842 msec_per_try * i, ifname);
1843 break;
1848 mutex_unlock(&pktgen_thread_lock);
1851 static void pktgen_change_name(struct net_device *dev)
1853 struct pktgen_thread *t;
1855 list_for_each_entry(t, &pktgen_threads, th_list) {
1856 struct pktgen_dev *pkt_dev;
1858 list_for_each_entry(pkt_dev, &t->if_list, list) {
1859 if (pkt_dev->odev != dev)
1860 continue;
1862 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1864 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1865 pg_proc_dir);
1866 if (!pkt_dev->entry)
1867 printk(KERN_ERR "pktgen: can't move proc "
1868 " entry for '%s'\n", dev->name);
1869 break;
1874 static int pktgen_device_event(struct notifier_block *unused,
1875 unsigned long event, void *ptr)
1877 struct net_device *dev = ptr;
1879 if (dev->nd_net != &init_net)
1880 return NOTIFY_DONE;
1882 /* It is OK that we do not hold the group lock right now,
1883 * as we run under the RTNL lock.
1886 switch (event) {
1887 case NETDEV_CHANGENAME:
1888 pktgen_change_name(dev);
1889 break;
1891 case NETDEV_UNREGISTER:
1892 pktgen_mark_device(dev->name);
1893 break;
1896 return NOTIFY_DONE;
1899 /* Associate pktgen_dev with a device. */
1901 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1903 struct net_device *odev;
1904 int err;
1906 /* Clean old setups */
1907 if (pkt_dev->odev) {
1908 dev_put(pkt_dev->odev);
1909 pkt_dev->odev = NULL;
1912 odev = dev_get_by_name(&init_net, ifname);
1913 if (!odev) {
1914 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1915 return -ENODEV;
1918 if (odev->type != ARPHRD_ETHER) {
1919 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1920 err = -EINVAL;
1921 } else if (!netif_running(odev)) {
1922 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1923 err = -ENETDOWN;
1924 } else {
1925 pkt_dev->odev = odev;
1926 return 0;
1929 dev_put(odev);
1930 return err;
1933 /* Read pkt_dev from the interface and set up internal pktgen_dev
1934 * structure to have the right information to create/send packets
1936 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1938 if (!pkt_dev->odev) {
1939 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1940 "setup_inject.\n");
1941 sprintf(pkt_dev->result,
1942 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1943 return;
1946 /* Default to the interface's mac if not explicitly set. */
1948 if (is_zero_ether_addr(pkt_dev->src_mac))
1949 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1951 /* Set up Dest MAC */
1952 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1954 /* Set up pkt size */
1955 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1957 if (pkt_dev->flags & F_IPV6) {
1959 * Skip this automatic address setting until locks or functions
1960 * gets exported
1963 #ifdef NOTNOW
1964 int i, set = 0, err = 1;
1965 struct inet6_dev *idev;
1967 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1968 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1969 set = 1;
1970 break;
1973 if (!set) {
1976 * Use linklevel address if unconfigured.
1978 * use ipv6_get_lladdr if/when it's get exported
1981 rcu_read_lock();
1982 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1983 struct inet6_ifaddr *ifp;
1985 read_lock_bh(&idev->lock);
1986 for (ifp = idev->addr_list; ifp;
1987 ifp = ifp->if_next) {
1988 if (ifp->scope == IFA_LINK
1989 && !(ifp->
1990 flags & IFA_F_TENTATIVE)) {
1991 ipv6_addr_copy(&pkt_dev->
1992 cur_in6_saddr,
1993 &ifp->addr);
1994 err = 0;
1995 break;
1998 read_unlock_bh(&idev->lock);
2000 rcu_read_unlock();
2001 if (err)
2002 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2003 "address not availble.\n");
2005 #endif
2006 } else {
2007 pkt_dev->saddr_min = 0;
2008 pkt_dev->saddr_max = 0;
2009 if (strlen(pkt_dev->src_min) == 0) {
2011 struct in_device *in_dev;
2013 rcu_read_lock();
2014 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2015 if (in_dev) {
2016 if (in_dev->ifa_list) {
2017 pkt_dev->saddr_min =
2018 in_dev->ifa_list->ifa_address;
2019 pkt_dev->saddr_max = pkt_dev->saddr_min;
2022 rcu_read_unlock();
2023 } else {
2024 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2025 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2028 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2029 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2031 /* Initialize current values. */
2032 pkt_dev->cur_dst_mac_offset = 0;
2033 pkt_dev->cur_src_mac_offset = 0;
2034 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2035 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2036 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2037 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2038 pkt_dev->nflows = 0;
2041 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2043 __u64 start;
2044 __u64 now;
2046 start = now = getCurUs();
2047 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2048 while (now < spin_until_us) {
2049 /* TODO: optimize sleeping behavior */
2050 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2051 schedule_timeout_interruptible(1);
2052 else if (spin_until_us - now > 100) {
2053 if (!pkt_dev->running)
2054 return;
2055 if (need_resched())
2056 schedule();
2059 now = getCurUs();
2062 pkt_dev->idle_acc += now - start;
2065 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2067 pkt_dev->pkt_overhead = 0;
2068 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2069 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2070 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2073 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2076 if (pkt_dev->flows[flow].flags & F_INIT)
2077 return 1;
2078 else
2079 return 0;
2082 static inline int f_pick(struct pktgen_dev *pkt_dev)
2084 int flow = pkt_dev->curfl;
2086 if (pkt_dev->flags & F_FLOW_SEQ) {
2087 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2088 /* reset time */
2089 pkt_dev->flows[flow].count = 0;
2090 pkt_dev->curfl += 1;
2091 if (pkt_dev->curfl >= pkt_dev->cflows)
2092 pkt_dev->curfl = 0; /*reset */
2094 } else {
2095 flow = random32() % pkt_dev->cflows;
2097 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2098 pkt_dev->flows[flow].count = 0;
2101 return pkt_dev->curfl;
2105 #ifdef CONFIG_XFRM
2106 /* If there was already an IPSEC SA, we keep it as is, else
2107 * we go look for it ...
2109 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2111 struct xfrm_state *x = pkt_dev->flows[flow].x;
2112 if (!x) {
2113 /*slow path: we dont already have xfrm_state*/
2114 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2115 (xfrm_address_t *)&pkt_dev->cur_saddr,
2116 AF_INET,
2117 pkt_dev->ipsmode,
2118 pkt_dev->ipsproto, 0);
2119 if (x) {
2120 pkt_dev->flows[flow].x = x;
2121 set_pkt_overhead(pkt_dev);
2122 pkt_dev->pkt_overhead+=x->props.header_len;
2127 #endif
2128 /* Increment/randomize headers according to flags and current values
2129 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2131 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2133 __u32 imn;
2134 __u32 imx;
2135 int flow = 0;
2137 if (pkt_dev->cflows)
2138 flow = f_pick(pkt_dev);
2140 /* Deal with source MAC */
2141 if (pkt_dev->src_mac_count > 1) {
2142 __u32 mc;
2143 __u32 tmp;
2145 if (pkt_dev->flags & F_MACSRC_RND)
2146 mc = random32() % pkt_dev->src_mac_count;
2147 else {
2148 mc = pkt_dev->cur_src_mac_offset++;
2149 if (pkt_dev->cur_src_mac_offset >
2150 pkt_dev->src_mac_count)
2151 pkt_dev->cur_src_mac_offset = 0;
2154 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2155 pkt_dev->hh[11] = tmp;
2156 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2157 pkt_dev->hh[10] = tmp;
2158 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2159 pkt_dev->hh[9] = tmp;
2160 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2161 pkt_dev->hh[8] = tmp;
2162 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2163 pkt_dev->hh[7] = tmp;
2166 /* Deal with Destination MAC */
2167 if (pkt_dev->dst_mac_count > 1) {
2168 __u32 mc;
2169 __u32 tmp;
2171 if (pkt_dev->flags & F_MACDST_RND)
2172 mc = random32() % pkt_dev->dst_mac_count;
2174 else {
2175 mc = pkt_dev->cur_dst_mac_offset++;
2176 if (pkt_dev->cur_dst_mac_offset >
2177 pkt_dev->dst_mac_count) {
2178 pkt_dev->cur_dst_mac_offset = 0;
2182 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2183 pkt_dev->hh[5] = tmp;
2184 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2185 pkt_dev->hh[4] = tmp;
2186 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2187 pkt_dev->hh[3] = tmp;
2188 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2189 pkt_dev->hh[2] = tmp;
2190 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2191 pkt_dev->hh[1] = tmp;
2194 if (pkt_dev->flags & F_MPLS_RND) {
2195 unsigned i;
2196 for (i = 0; i < pkt_dev->nr_labels; i++)
2197 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2198 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2199 ((__force __be32)random32() &
2200 htonl(0x000fffff));
2203 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2204 pkt_dev->vlan_id = random32() & (4096-1);
2207 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2208 pkt_dev->svlan_id = random32() & (4096 - 1);
2211 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2212 if (pkt_dev->flags & F_UDPSRC_RND)
2213 pkt_dev->cur_udp_src = random32() %
2214 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2215 + pkt_dev->udp_src_min;
2217 else {
2218 pkt_dev->cur_udp_src++;
2219 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2220 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2224 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2225 if (pkt_dev->flags & F_UDPDST_RND) {
2226 pkt_dev->cur_udp_dst = random32() %
2227 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2228 + pkt_dev->udp_dst_min;
2229 } else {
2230 pkt_dev->cur_udp_dst++;
2231 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2232 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2236 if (!(pkt_dev->flags & F_IPV6)) {
2238 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2239 ntohl(pkt_dev->
2240 saddr_max))) {
2241 __u32 t;
2242 if (pkt_dev->flags & F_IPSRC_RND)
2243 t = random32() % (imx - imn) + imn;
2244 else {
2245 t = ntohl(pkt_dev->cur_saddr);
2246 t++;
2247 if (t > imx) {
2248 t = imn;
2251 pkt_dev->cur_saddr = htonl(t);
2254 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2255 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2256 } else {
2257 imn = ntohl(pkt_dev->daddr_min);
2258 imx = ntohl(pkt_dev->daddr_max);
2259 if (imn < imx) {
2260 __u32 t;
2261 __be32 s;
2262 if (pkt_dev->flags & F_IPDST_RND) {
2264 t = random32() % (imx - imn) + imn;
2265 s = htonl(t);
2267 while (ipv4_is_loopback(s) ||
2268 ipv4_is_multicast(s) ||
2269 ipv4_is_lbcast(s) ||
2270 ipv4_is_zeronet(s) ||
2271 ipv4_is_local_multicast(s)) {
2272 t = random32() % (imx - imn) + imn;
2273 s = htonl(t);
2275 pkt_dev->cur_daddr = s;
2276 } else {
2277 t = ntohl(pkt_dev->cur_daddr);
2278 t++;
2279 if (t > imx) {
2280 t = imn;
2282 pkt_dev->cur_daddr = htonl(t);
2285 if (pkt_dev->cflows) {
2286 pkt_dev->flows[flow].flags |= F_INIT;
2287 pkt_dev->flows[flow].cur_daddr =
2288 pkt_dev->cur_daddr;
2289 #ifdef CONFIG_XFRM
2290 if (pkt_dev->flags & F_IPSEC_ON)
2291 get_ipsec_sa(pkt_dev, flow);
2292 #endif
2293 pkt_dev->nflows++;
2296 } else { /* IPV6 * */
2298 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2299 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2300 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2301 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2302 else {
2303 int i;
2305 /* Only random destinations yet */
2307 for (i = 0; i < 4; i++) {
2308 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2309 (((__force __be32)random32() |
2310 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2311 pkt_dev->max_in6_daddr.s6_addr32[i]);
2316 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2317 __u32 t;
2318 if (pkt_dev->flags & F_TXSIZE_RND) {
2319 t = random32() %
2320 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2321 + pkt_dev->min_pkt_size;
2322 } else {
2323 t = pkt_dev->cur_pkt_size + 1;
2324 if (t > pkt_dev->max_pkt_size)
2325 t = pkt_dev->min_pkt_size;
2327 pkt_dev->cur_pkt_size = t;
2330 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2331 __u16 t;
2332 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2333 t = random32() %
2334 (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
2335 + pkt_dev->queue_map_min;
2336 } else {
2337 t = pkt_dev->cur_queue_map + 1;
2338 if (t > pkt_dev->queue_map_max)
2339 t = pkt_dev->queue_map_min;
2341 pkt_dev->cur_queue_map = t;
2344 pkt_dev->flows[flow].count++;
2348 #ifdef CONFIG_XFRM
2349 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2351 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2352 int err = 0;
2353 struct iphdr *iph;
2355 if (!x)
2356 return 0;
2357 /* XXX: we dont support tunnel mode for now until
2358 * we resolve the dst issue */
2359 if (x->props.mode != XFRM_MODE_TRANSPORT)
2360 return 0;
2362 spin_lock(&x->lock);
2363 iph = ip_hdr(skb);
2365 err = x->outer_mode->output(x, skb);
2366 if (err)
2367 goto error;
2368 err = x->type->output(x, skb);
2369 if (err)
2370 goto error;
2372 x->curlft.bytes +=skb->len;
2373 x->curlft.packets++;
2374 error:
2375 spin_unlock(&x->lock);
2376 return err;
2379 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2381 if (pkt_dev->cflows) {
2382 /* let go of the SAs if we have them */
2383 int i = 0;
2384 for (; i < pkt_dev->nflows; i++){
2385 struct xfrm_state *x = pkt_dev->flows[i].x;
2386 if (x) {
2387 xfrm_state_put(x);
2388 pkt_dev->flows[i].x = NULL;
2394 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2395 struct sk_buff *skb, __be16 protocol)
2397 if (pkt_dev->flags & F_IPSEC_ON) {
2398 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2399 int nhead = 0;
2400 if (x) {
2401 int ret;
2402 __u8 *eth;
2403 nhead = x->props.header_len - skb_headroom(skb);
2404 if (nhead >0) {
2405 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2406 if (ret < 0) {
2407 printk(KERN_ERR "Error expanding "
2408 "ipsec packet %d\n",ret);
2409 return 0;
2413 /* ipsec is not expecting ll header */
2414 skb_pull(skb, ETH_HLEN);
2415 ret = pktgen_output_ipsec(skb, pkt_dev);
2416 if (ret) {
2417 printk(KERN_ERR "Error creating ipsec "
2418 "packet %d\n",ret);
2419 kfree_skb(skb);
2420 return 0;
2422 /* restore ll */
2423 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2424 memcpy(eth, pkt_dev->hh, 12);
2425 *(u16 *) & eth[12] = protocol;
2428 return 1;
2430 #endif
2432 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2434 unsigned i;
2435 for (i = 0; i < pkt_dev->nr_labels; i++) {
2436 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2438 mpls--;
2439 *mpls |= MPLS_STACK_BOTTOM;
2442 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2443 unsigned int prio)
2445 return htons(id | (cfi << 12) | (prio << 13));
2448 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2449 struct pktgen_dev *pkt_dev)
2451 struct sk_buff *skb = NULL;
2452 __u8 *eth;
2453 struct udphdr *udph;
2454 int datalen, iplen;
2455 struct iphdr *iph;
2456 struct pktgen_hdr *pgh = NULL;
2457 __be16 protocol = htons(ETH_P_IP);
2458 __be32 *mpls;
2459 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2460 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2461 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2462 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2465 if (pkt_dev->nr_labels)
2466 protocol = htons(ETH_P_MPLS_UC);
2468 if (pkt_dev->vlan_id != 0xffff)
2469 protocol = htons(ETH_P_8021Q);
2471 /* Update any of the values, used when we're incrementing various
2472 * fields.
2474 mod_cur_headers(pkt_dev);
2476 datalen = (odev->hard_header_len + 16) & ~0xf;
2477 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2478 pkt_dev->pkt_overhead, GFP_ATOMIC);
2479 if (!skb) {
2480 sprintf(pkt_dev->result, "No memory");
2481 return NULL;
2484 skb_reserve(skb, datalen);
2486 /* Reserve for ethernet and IP header */
2487 eth = (__u8 *) skb_push(skb, 14);
2488 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2489 if (pkt_dev->nr_labels)
2490 mpls_push(mpls, pkt_dev);
2492 if (pkt_dev->vlan_id != 0xffff) {
2493 if (pkt_dev->svlan_id != 0xffff) {
2494 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2495 *svlan_tci = build_tci(pkt_dev->svlan_id,
2496 pkt_dev->svlan_cfi,
2497 pkt_dev->svlan_p);
2498 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2499 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2501 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2502 *vlan_tci = build_tci(pkt_dev->vlan_id,
2503 pkt_dev->vlan_cfi,
2504 pkt_dev->vlan_p);
2505 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2506 *vlan_encapsulated_proto = htons(ETH_P_IP);
2509 skb->network_header = skb->tail;
2510 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2511 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2512 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
2513 iph = ip_hdr(skb);
2514 udph = udp_hdr(skb);
2516 memcpy(eth, pkt_dev->hh, 12);
2517 *(__be16 *) & eth[12] = protocol;
2519 /* Eth + IPh + UDPh + mpls */
2520 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2521 pkt_dev->pkt_overhead;
2522 if (datalen < sizeof(struct pktgen_hdr))
2523 datalen = sizeof(struct pktgen_hdr);
2525 udph->source = htons(pkt_dev->cur_udp_src);
2526 udph->dest = htons(pkt_dev->cur_udp_dst);
2527 udph->len = htons(datalen + 8); /* DATA + udphdr */
2528 udph->check = 0; /* No checksum */
2530 iph->ihl = 5;
2531 iph->version = 4;
2532 iph->ttl = 32;
2533 iph->tos = pkt_dev->tos;
2534 iph->protocol = IPPROTO_UDP; /* UDP */
2535 iph->saddr = pkt_dev->cur_saddr;
2536 iph->daddr = pkt_dev->cur_daddr;
2537 iph->frag_off = 0;
2538 iplen = 20 + 8 + datalen;
2539 iph->tot_len = htons(iplen);
2540 iph->check = 0;
2541 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2542 skb->protocol = protocol;
2543 skb->mac_header = (skb->network_header - ETH_HLEN -
2544 pkt_dev->pkt_overhead);
2545 skb->dev = odev;
2546 skb->pkt_type = PACKET_HOST;
2548 if (pkt_dev->nfrags <= 0)
2549 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2550 else {
2551 int frags = pkt_dev->nfrags;
2552 int i;
2554 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2556 if (frags > MAX_SKB_FRAGS)
2557 frags = MAX_SKB_FRAGS;
2558 if (datalen > frags * PAGE_SIZE) {
2559 skb_put(skb, datalen - frags * PAGE_SIZE);
2560 datalen = frags * PAGE_SIZE;
2563 i = 0;
2564 while (datalen > 0) {
2565 struct page *page = alloc_pages(GFP_KERNEL, 0);
2566 skb_shinfo(skb)->frags[i].page = page;
2567 skb_shinfo(skb)->frags[i].page_offset = 0;
2568 skb_shinfo(skb)->frags[i].size =
2569 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2570 datalen -= skb_shinfo(skb)->frags[i].size;
2571 skb->len += skb_shinfo(skb)->frags[i].size;
2572 skb->data_len += skb_shinfo(skb)->frags[i].size;
2573 i++;
2574 skb_shinfo(skb)->nr_frags = i;
2577 while (i < frags) {
2578 int rem;
2580 if (i == 0)
2581 break;
2583 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2584 if (rem == 0)
2585 break;
2587 skb_shinfo(skb)->frags[i - 1].size -= rem;
2589 skb_shinfo(skb)->frags[i] =
2590 skb_shinfo(skb)->frags[i - 1];
2591 get_page(skb_shinfo(skb)->frags[i].page);
2592 skb_shinfo(skb)->frags[i].page =
2593 skb_shinfo(skb)->frags[i - 1].page;
2594 skb_shinfo(skb)->frags[i].page_offset +=
2595 skb_shinfo(skb)->frags[i - 1].size;
2596 skb_shinfo(skb)->frags[i].size = rem;
2597 i++;
2598 skb_shinfo(skb)->nr_frags = i;
2602 /* Stamp the time, and sequence number, convert them to network byte order */
2604 if (pgh) {
2605 struct timeval timestamp;
2607 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2608 pgh->seq_num = htonl(pkt_dev->seq_num);
2610 do_gettimeofday(&timestamp);
2611 pgh->tv_sec = htonl(timestamp.tv_sec);
2612 pgh->tv_usec = htonl(timestamp.tv_usec);
2615 #ifdef CONFIG_XFRM
2616 if (!process_ipsec(pkt_dev, skb, protocol))
2617 return NULL;
2618 #endif
2620 return skb;
2624 * scan_ip6, fmt_ip taken from dietlibc-0.21
2625 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2627 * Slightly modified for kernel.
2628 * Should be candidate for net/ipv4/utils.c
2629 * --ro
2632 static unsigned int scan_ip6(const char *s, char ip[16])
2634 unsigned int i;
2635 unsigned int len = 0;
2636 unsigned long u;
2637 char suffix[16];
2638 unsigned int prefixlen = 0;
2639 unsigned int suffixlen = 0;
2640 __be32 tmp;
2641 char *pos;
2643 for (i = 0; i < 16; i++)
2644 ip[i] = 0;
2646 for (;;) {
2647 if (*s == ':') {
2648 len++;
2649 if (s[1] == ':') { /* Found "::", skip to part 2 */
2650 s += 2;
2651 len++;
2652 break;
2654 s++;
2657 u = simple_strtoul(s, &pos, 16);
2658 i = pos - s;
2659 if (!i)
2660 return 0;
2661 if (prefixlen == 12 && s[i] == '.') {
2663 /* the last 4 bytes may be written as IPv4 address */
2665 tmp = in_aton(s);
2666 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2667 return i + len;
2669 ip[prefixlen++] = (u >> 8);
2670 ip[prefixlen++] = (u & 255);
2671 s += i;
2672 len += i;
2673 if (prefixlen == 16)
2674 return len;
2677 /* part 2, after "::" */
2678 for (;;) {
2679 if (*s == ':') {
2680 if (suffixlen == 0)
2681 break;
2682 s++;
2683 len++;
2684 } else if (suffixlen != 0)
2685 break;
2687 u = simple_strtol(s, &pos, 16);
2688 i = pos - s;
2689 if (!i) {
2690 if (*s)
2691 len--;
2692 break;
2694 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2695 tmp = in_aton(s);
2696 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2697 sizeof(tmp));
2698 suffixlen += 4;
2699 len += strlen(s);
2700 break;
2702 suffix[suffixlen++] = (u >> 8);
2703 suffix[suffixlen++] = (u & 255);
2704 s += i;
2705 len += i;
2706 if (prefixlen + suffixlen == 16)
2707 break;
2709 for (i = 0; i < suffixlen; i++)
2710 ip[16 - suffixlen + i] = suffix[i];
2711 return len;
2714 static char tohex(char hexdigit)
2716 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2719 static int fmt_xlong(char *s, unsigned int i)
2721 char *bak = s;
2722 *s = tohex((i >> 12) & 0xf);
2723 if (s != bak || *s != '0')
2724 ++s;
2725 *s = tohex((i >> 8) & 0xf);
2726 if (s != bak || *s != '0')
2727 ++s;
2728 *s = tohex((i >> 4) & 0xf);
2729 if (s != bak || *s != '0')
2730 ++s;
2731 *s = tohex(i & 0xf);
2732 return s - bak + 1;
2735 static unsigned int fmt_ip6(char *s, const char ip[16])
2737 unsigned int len;
2738 unsigned int i;
2739 unsigned int temp;
2740 unsigned int compressing;
2741 int j;
2743 len = 0;
2744 compressing = 0;
2745 for (j = 0; j < 16; j += 2) {
2747 #ifdef V4MAPPEDPREFIX
2748 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2749 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2750 temp = strlen(s);
2751 return len + temp;
2753 #endif
2754 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2755 (unsigned long)(unsigned char)ip[j + 1];
2756 if (temp == 0) {
2757 if (!compressing) {
2758 compressing = 1;
2759 if (j == 0) {
2760 *s++ = ':';
2761 ++len;
2764 } else {
2765 if (compressing) {
2766 compressing = 0;
2767 *s++ = ':';
2768 ++len;
2770 i = fmt_xlong(s, temp);
2771 len += i;
2772 s += i;
2773 if (j < 14) {
2774 *s++ = ':';
2775 ++len;
2779 if (compressing) {
2780 *s++ = ':';
2781 ++len;
2783 *s = 0;
2784 return len;
2787 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2788 struct pktgen_dev *pkt_dev)
2790 struct sk_buff *skb = NULL;
2791 __u8 *eth;
2792 struct udphdr *udph;
2793 int datalen;
2794 struct ipv6hdr *iph;
2795 struct pktgen_hdr *pgh = NULL;
2796 __be16 protocol = htons(ETH_P_IPV6);
2797 __be32 *mpls;
2798 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2799 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2800 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2801 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2803 if (pkt_dev->nr_labels)
2804 protocol = htons(ETH_P_MPLS_UC);
2806 if (pkt_dev->vlan_id != 0xffff)
2807 protocol = htons(ETH_P_8021Q);
2809 /* Update any of the values, used when we're incrementing various
2810 * fields.
2812 mod_cur_headers(pkt_dev);
2814 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2815 pkt_dev->pkt_overhead, GFP_ATOMIC);
2816 if (!skb) {
2817 sprintf(pkt_dev->result, "No memory");
2818 return NULL;
2821 skb_reserve(skb, 16);
2823 /* Reserve for ethernet and IP header */
2824 eth = (__u8 *) skb_push(skb, 14);
2825 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2826 if (pkt_dev->nr_labels)
2827 mpls_push(mpls, pkt_dev);
2829 if (pkt_dev->vlan_id != 0xffff) {
2830 if (pkt_dev->svlan_id != 0xffff) {
2831 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2832 *svlan_tci = build_tci(pkt_dev->svlan_id,
2833 pkt_dev->svlan_cfi,
2834 pkt_dev->svlan_p);
2835 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2836 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2838 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2839 *vlan_tci = build_tci(pkt_dev->vlan_id,
2840 pkt_dev->vlan_cfi,
2841 pkt_dev->vlan_p);
2842 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2843 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2846 skb->network_header = skb->tail;
2847 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2848 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2849 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
2850 iph = ipv6_hdr(skb);
2851 udph = udp_hdr(skb);
2853 memcpy(eth, pkt_dev->hh, 12);
2854 *(__be16 *) & eth[12] = protocol;
2856 /* Eth + IPh + UDPh + mpls */
2857 datalen = pkt_dev->cur_pkt_size - 14 -
2858 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2859 pkt_dev->pkt_overhead;
2861 if (datalen < sizeof(struct pktgen_hdr)) {
2862 datalen = sizeof(struct pktgen_hdr);
2863 if (net_ratelimit())
2864 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2865 datalen);
2868 udph->source = htons(pkt_dev->cur_udp_src);
2869 udph->dest = htons(pkt_dev->cur_udp_dst);
2870 udph->len = htons(datalen + sizeof(struct udphdr));
2871 udph->check = 0; /* No checksum */
2873 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2875 if (pkt_dev->traffic_class) {
2876 /* Version + traffic class + flow (0) */
2877 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2880 iph->hop_limit = 32;
2882 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2883 iph->nexthdr = IPPROTO_UDP;
2885 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2886 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2888 skb->mac_header = (skb->network_header - ETH_HLEN -
2889 pkt_dev->pkt_overhead);
2890 skb->protocol = protocol;
2891 skb->dev = odev;
2892 skb->pkt_type = PACKET_HOST;
2894 if (pkt_dev->nfrags <= 0)
2895 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2896 else {
2897 int frags = pkt_dev->nfrags;
2898 int i;
2900 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2902 if (frags > MAX_SKB_FRAGS)
2903 frags = MAX_SKB_FRAGS;
2904 if (datalen > frags * PAGE_SIZE) {
2905 skb_put(skb, datalen - frags * PAGE_SIZE);
2906 datalen = frags * PAGE_SIZE;
2909 i = 0;
2910 while (datalen > 0) {
2911 struct page *page = alloc_pages(GFP_KERNEL, 0);
2912 skb_shinfo(skb)->frags[i].page = page;
2913 skb_shinfo(skb)->frags[i].page_offset = 0;
2914 skb_shinfo(skb)->frags[i].size =
2915 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2916 datalen -= skb_shinfo(skb)->frags[i].size;
2917 skb->len += skb_shinfo(skb)->frags[i].size;
2918 skb->data_len += skb_shinfo(skb)->frags[i].size;
2919 i++;
2920 skb_shinfo(skb)->nr_frags = i;
2923 while (i < frags) {
2924 int rem;
2926 if (i == 0)
2927 break;
2929 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2930 if (rem == 0)
2931 break;
2933 skb_shinfo(skb)->frags[i - 1].size -= rem;
2935 skb_shinfo(skb)->frags[i] =
2936 skb_shinfo(skb)->frags[i - 1];
2937 get_page(skb_shinfo(skb)->frags[i].page);
2938 skb_shinfo(skb)->frags[i].page =
2939 skb_shinfo(skb)->frags[i - 1].page;
2940 skb_shinfo(skb)->frags[i].page_offset +=
2941 skb_shinfo(skb)->frags[i - 1].size;
2942 skb_shinfo(skb)->frags[i].size = rem;
2943 i++;
2944 skb_shinfo(skb)->nr_frags = i;
2948 /* Stamp the time, and sequence number, convert them to network byte order */
2949 /* should we update cloned packets too ? */
2950 if (pgh) {
2951 struct timeval timestamp;
2953 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2954 pgh->seq_num = htonl(pkt_dev->seq_num);
2956 do_gettimeofday(&timestamp);
2957 pgh->tv_sec = htonl(timestamp.tv_sec);
2958 pgh->tv_usec = htonl(timestamp.tv_usec);
2960 /* pkt_dev->seq_num++; FF: you really mean this? */
2962 return skb;
2965 static inline struct sk_buff *fill_packet(struct net_device *odev,
2966 struct pktgen_dev *pkt_dev)
2968 if (pkt_dev->flags & F_IPV6)
2969 return fill_packet_ipv6(odev, pkt_dev);
2970 else
2971 return fill_packet_ipv4(odev, pkt_dev);
2974 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2976 pkt_dev->seq_num = 1;
2977 pkt_dev->idle_acc = 0;
2978 pkt_dev->sofar = 0;
2979 pkt_dev->tx_bytes = 0;
2980 pkt_dev->errors = 0;
2983 /* Set up structure for sending pkts, clear counters */
2985 static void pktgen_run(struct pktgen_thread *t)
2987 struct pktgen_dev *pkt_dev;
2988 int started = 0;
2990 pr_debug("pktgen: entering pktgen_run. %p\n", t);
2992 if_lock(t);
2993 list_for_each_entry(pkt_dev, &t->if_list, list) {
2996 * setup odev and create initial packet.
2998 pktgen_setup_inject(pkt_dev);
3000 if (pkt_dev->odev) {
3001 pktgen_clear_counters(pkt_dev);
3002 pkt_dev->running = 1; /* Cranke yeself! */
3003 pkt_dev->skb = NULL;
3004 pkt_dev->started_at = getCurUs();
3005 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
3006 pkt_dev->next_tx_ns = 0;
3007 set_pkt_overhead(pkt_dev);
3009 strcpy(pkt_dev->result, "Starting");
3010 started++;
3011 } else
3012 strcpy(pkt_dev->result, "Error starting");
3014 if_unlock(t);
3015 if (started)
3016 t->control &= ~(T_STOP);
3019 static void pktgen_stop_all_threads_ifs(void)
3021 struct pktgen_thread *t;
3023 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3025 mutex_lock(&pktgen_thread_lock);
3027 list_for_each_entry(t, &pktgen_threads, th_list)
3028 t->control |= T_STOP;
3030 mutex_unlock(&pktgen_thread_lock);
3033 static int thread_is_running(struct pktgen_thread *t)
3035 struct pktgen_dev *pkt_dev;
3036 int res = 0;
3038 list_for_each_entry(pkt_dev, &t->if_list, list)
3039 if (pkt_dev->running) {
3040 res = 1;
3041 break;
3043 return res;
3046 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3048 if_lock(t);
3050 while (thread_is_running(t)) {
3052 if_unlock(t);
3054 msleep_interruptible(100);
3056 if (signal_pending(current))
3057 goto signal;
3058 if_lock(t);
3060 if_unlock(t);
3061 return 1;
3062 signal:
3063 return 0;
3066 static int pktgen_wait_all_threads_run(void)
3068 struct pktgen_thread *t;
3069 int sig = 1;
3071 mutex_lock(&pktgen_thread_lock);
3073 list_for_each_entry(t, &pktgen_threads, th_list) {
3074 sig = pktgen_wait_thread_run(t);
3075 if (sig == 0)
3076 break;
3079 if (sig == 0)
3080 list_for_each_entry(t, &pktgen_threads, th_list)
3081 t->control |= (T_STOP);
3083 mutex_unlock(&pktgen_thread_lock);
3084 return sig;
3087 static void pktgen_run_all_threads(void)
3089 struct pktgen_thread *t;
3091 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3093 mutex_lock(&pktgen_thread_lock);
3095 list_for_each_entry(t, &pktgen_threads, th_list)
3096 t->control |= (T_RUN);
3098 mutex_unlock(&pktgen_thread_lock);
3100 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3102 pktgen_wait_all_threads_run();
3105 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3107 __u64 total_us, bps, mbps, pps, idle;
3108 char *p = pkt_dev->result;
3110 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3112 idle = pkt_dev->idle_acc;
3114 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3115 (unsigned long long)total_us,
3116 (unsigned long long)(total_us - idle),
3117 (unsigned long long)idle,
3118 (unsigned long long)pkt_dev->sofar,
3119 pkt_dev->cur_pkt_size, nr_frags);
3121 pps = pkt_dev->sofar * USEC_PER_SEC;
3123 while ((total_us >> 32) != 0) {
3124 pps >>= 1;
3125 total_us >>= 1;
3128 do_div(pps, total_us);
3130 bps = pps * 8 * pkt_dev->cur_pkt_size;
3132 mbps = bps;
3133 do_div(mbps, 1000000);
3134 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3135 (unsigned long long)pps,
3136 (unsigned long long)mbps,
3137 (unsigned long long)bps,
3138 (unsigned long long)pkt_dev->errors);
3141 /* Set stopped-at timer, remove from running list, do counters & statistics */
3143 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3145 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3147 if (!pkt_dev->running) {
3148 printk(KERN_WARNING "pktgen: interface: %s is already "
3149 "stopped\n", pkt_dev->odev->name);
3150 return -EINVAL;
3153 pkt_dev->stopped_at = getCurUs();
3154 pkt_dev->running = 0;
3156 show_results(pkt_dev, nr_frags);
3158 return 0;
3161 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3163 struct pktgen_dev *pkt_dev, *best = NULL;
3165 if_lock(t);
3167 list_for_each_entry(pkt_dev, &t->if_list, list) {
3168 if (!pkt_dev->running)
3169 continue;
3170 if (best == NULL)
3171 best = pkt_dev;
3172 else if (pkt_dev->next_tx_us < best->next_tx_us)
3173 best = pkt_dev;
3175 if_unlock(t);
3176 return best;
3179 static void pktgen_stop(struct pktgen_thread *t)
3181 struct pktgen_dev *pkt_dev;
3183 pr_debug("pktgen: entering pktgen_stop\n");
3185 if_lock(t);
3187 list_for_each_entry(pkt_dev, &t->if_list, list) {
3188 pktgen_stop_device(pkt_dev);
3189 if (pkt_dev->skb)
3190 kfree_skb(pkt_dev->skb);
3192 pkt_dev->skb = NULL;
3195 if_unlock(t);
3199 * one of our devices needs to be removed - find it
3200 * and remove it
3202 static void pktgen_rem_one_if(struct pktgen_thread *t)
3204 struct list_head *q, *n;
3205 struct pktgen_dev *cur;
3207 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3209 if_lock(t);
3211 list_for_each_safe(q, n, &t->if_list) {
3212 cur = list_entry(q, struct pktgen_dev, list);
3214 if (!cur->removal_mark)
3215 continue;
3217 if (cur->skb)
3218 kfree_skb(cur->skb);
3219 cur->skb = NULL;
3221 pktgen_remove_device(t, cur);
3223 break;
3226 if_unlock(t);
3229 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3231 struct list_head *q, *n;
3232 struct pktgen_dev *cur;
3234 /* Remove all devices, free mem */
3236 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3237 if_lock(t);
3239 list_for_each_safe(q, n, &t->if_list) {
3240 cur = list_entry(q, struct pktgen_dev, list);
3242 if (cur->skb)
3243 kfree_skb(cur->skb);
3244 cur->skb = NULL;
3246 pktgen_remove_device(t, cur);
3249 if_unlock(t);
3252 static void pktgen_rem_thread(struct pktgen_thread *t)
3254 /* Remove from the thread list */
3256 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3258 mutex_lock(&pktgen_thread_lock);
3260 list_del(&t->th_list);
3262 mutex_unlock(&pktgen_thread_lock);
3265 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3267 struct net_device *odev = NULL;
3268 __u64 idle_start = 0;
3269 int ret;
3271 odev = pkt_dev->odev;
3273 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3274 u64 now;
3276 now = getCurUs();
3277 if (now < pkt_dev->next_tx_us)
3278 spin(pkt_dev, pkt_dev->next_tx_us);
3280 /* This is max DELAY, this has special meaning of
3281 * "never transmit"
3283 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3284 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3285 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3286 goto out;
3290 if ((netif_queue_stopped(odev) ||
3291 (pkt_dev->skb &&
3292 netif_subqueue_stopped(odev, pkt_dev->skb))) ||
3293 need_resched()) {
3294 idle_start = getCurUs();
3296 if (!netif_running(odev)) {
3297 pktgen_stop_device(pkt_dev);
3298 if (pkt_dev->skb)
3299 kfree_skb(pkt_dev->skb);
3300 pkt_dev->skb = NULL;
3301 goto out;
3303 if (need_resched())
3304 schedule();
3306 pkt_dev->idle_acc += getCurUs() - idle_start;
3308 if (netif_queue_stopped(odev) ||
3309 netif_subqueue_stopped(odev, pkt_dev->skb)) {
3310 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3311 pkt_dev->next_tx_ns = 0;
3312 goto out; /* Try the next interface */
3316 if (pkt_dev->last_ok || !pkt_dev->skb) {
3317 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3318 || (!pkt_dev->skb)) {
3319 /* build a new pkt */
3320 if (pkt_dev->skb)
3321 kfree_skb(pkt_dev->skb);
3323 pkt_dev->skb = fill_packet(odev, pkt_dev);
3324 if (pkt_dev->skb == NULL) {
3325 printk(KERN_ERR "pktgen: ERROR: couldn't "
3326 "allocate skb in fill_packet.\n");
3327 schedule();
3328 pkt_dev->clone_count--; /* back out increment, OOM */
3329 goto out;
3331 pkt_dev->allocated_skbs++;
3332 pkt_dev->clone_count = 0; /* reset counter */
3336 netif_tx_lock_bh(odev);
3337 if (!netif_queue_stopped(odev) &&
3338 !netif_subqueue_stopped(odev, pkt_dev->skb)) {
3340 atomic_inc(&(pkt_dev->skb->users));
3341 retry_now:
3342 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3343 if (likely(ret == NETDEV_TX_OK)) {
3344 pkt_dev->last_ok = 1;
3345 pkt_dev->sofar++;
3346 pkt_dev->seq_num++;
3347 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3349 } else if (ret == NETDEV_TX_LOCKED
3350 && (odev->features & NETIF_F_LLTX)) {
3351 cpu_relax();
3352 goto retry_now;
3353 } else { /* Retry it next time */
3355 atomic_dec(&(pkt_dev->skb->users));
3357 if (debug && net_ratelimit())
3358 printk(KERN_INFO "pktgen: Hard xmit error\n");
3360 pkt_dev->errors++;
3361 pkt_dev->last_ok = 0;
3364 pkt_dev->next_tx_us = getCurUs();
3365 pkt_dev->next_tx_ns = 0;
3367 pkt_dev->next_tx_us += pkt_dev->delay_us;
3368 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3370 if (pkt_dev->next_tx_ns > 1000) {
3371 pkt_dev->next_tx_us++;
3372 pkt_dev->next_tx_ns -= 1000;
3376 else { /* Retry it next time */
3377 pkt_dev->last_ok = 0;
3378 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3379 pkt_dev->next_tx_ns = 0;
3382 netif_tx_unlock_bh(odev);
3384 /* If pkt_dev->count is zero, then run forever */
3385 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3386 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3387 idle_start = getCurUs();
3388 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3389 if (signal_pending(current)) {
3390 break;
3392 schedule();
3394 pkt_dev->idle_acc += getCurUs() - idle_start;
3397 /* Done with this */
3398 pktgen_stop_device(pkt_dev);
3399 if (pkt_dev->skb)
3400 kfree_skb(pkt_dev->skb);
3401 pkt_dev->skb = NULL;
3403 out:;
3407 * Main loop of the thread goes here
3410 static int pktgen_thread_worker(void *arg)
3412 DEFINE_WAIT(wait);
3413 struct pktgen_thread *t = arg;
3414 struct pktgen_dev *pkt_dev = NULL;
3415 int cpu = t->cpu;
3417 BUG_ON(smp_processor_id() != cpu);
3419 init_waitqueue_head(&t->queue);
3421 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3423 set_current_state(TASK_INTERRUPTIBLE);
3425 set_freezable();
3427 while (!kthread_should_stop()) {
3428 pkt_dev = next_to_run(t);
3430 if (!pkt_dev &&
3431 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3432 == 0) {
3433 prepare_to_wait(&(t->queue), &wait,
3434 TASK_INTERRUPTIBLE);
3435 schedule_timeout(HZ / 10);
3436 finish_wait(&(t->queue), &wait);
3439 __set_current_state(TASK_RUNNING);
3441 if (pkt_dev)
3442 pktgen_xmit(pkt_dev);
3444 if (t->control & T_STOP) {
3445 pktgen_stop(t);
3446 t->control &= ~(T_STOP);
3449 if (t->control & T_RUN) {
3450 pktgen_run(t);
3451 t->control &= ~(T_RUN);
3454 if (t->control & T_REMDEVALL) {
3455 pktgen_rem_all_ifs(t);
3456 t->control &= ~(T_REMDEVALL);
3459 if (t->control & T_REMDEV) {
3460 pktgen_rem_one_if(t);
3461 t->control &= ~(T_REMDEV);
3464 try_to_freeze();
3466 set_current_state(TASK_INTERRUPTIBLE);
3469 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3470 pktgen_stop(t);
3472 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3473 pktgen_rem_all_ifs(t);
3475 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3476 pktgen_rem_thread(t);
3478 return 0;
3481 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3482 const char *ifname)
3484 struct pktgen_dev *p, *pkt_dev = NULL;
3485 if_lock(t);
3487 list_for_each_entry(p, &t->if_list, list)
3488 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3489 pkt_dev = p;
3490 break;
3493 if_unlock(t);
3494 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3495 return pkt_dev;
3499 * Adds a dev at front of if_list.
3502 static int add_dev_to_thread(struct pktgen_thread *t,
3503 struct pktgen_dev *pkt_dev)
3505 int rv = 0;
3507 if_lock(t);
3509 if (pkt_dev->pg_thread) {
3510 printk(KERN_ERR "pktgen: ERROR: already assigned "
3511 "to a thread.\n");
3512 rv = -EBUSY;
3513 goto out;
3516 list_add(&pkt_dev->list, &t->if_list);
3517 pkt_dev->pg_thread = t;
3518 pkt_dev->running = 0;
3520 out:
3521 if_unlock(t);
3522 return rv;
3525 /* Called under thread lock */
3527 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3529 struct pktgen_dev *pkt_dev;
3530 int err;
3532 /* We don't allow a device to be on several threads */
3534 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3535 if (pkt_dev) {
3536 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3537 return -EBUSY;
3540 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3541 if (!pkt_dev)
3542 return -ENOMEM;
3544 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3545 if (pkt_dev->flows == NULL) {
3546 kfree(pkt_dev);
3547 return -ENOMEM;
3549 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3551 pkt_dev->removal_mark = 0;
3552 pkt_dev->min_pkt_size = ETH_ZLEN;
3553 pkt_dev->max_pkt_size = ETH_ZLEN;
3554 pkt_dev->nfrags = 0;
3555 pkt_dev->clone_skb = pg_clone_skb_d;
3556 pkt_dev->delay_us = pg_delay_d / 1000;
3557 pkt_dev->delay_ns = pg_delay_d % 1000;
3558 pkt_dev->count = pg_count_d;
3559 pkt_dev->sofar = 0;
3560 pkt_dev->udp_src_min = 9; /* sink port */
3561 pkt_dev->udp_src_max = 9;
3562 pkt_dev->udp_dst_min = 9;
3563 pkt_dev->udp_dst_max = 9;
3565 pkt_dev->vlan_p = 0;
3566 pkt_dev->vlan_cfi = 0;
3567 pkt_dev->vlan_id = 0xffff;
3568 pkt_dev->svlan_p = 0;
3569 pkt_dev->svlan_cfi = 0;
3570 pkt_dev->svlan_id = 0xffff;
3572 err = pktgen_setup_dev(pkt_dev, ifname);
3573 if (err)
3574 goto out1;
3576 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3577 if (!pkt_dev->entry) {
3578 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3579 PG_PROC_DIR, ifname);
3580 err = -EINVAL;
3581 goto out2;
3583 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3584 pkt_dev->entry->data = pkt_dev;
3585 #ifdef CONFIG_XFRM
3586 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3587 pkt_dev->ipsproto = IPPROTO_ESP;
3588 #endif
3590 return add_dev_to_thread(t, pkt_dev);
3591 out2:
3592 dev_put(pkt_dev->odev);
3593 out1:
3594 #ifdef CONFIG_XFRM
3595 free_SAs(pkt_dev);
3596 #endif
3597 if (pkt_dev->flows)
3598 vfree(pkt_dev->flows);
3599 kfree(pkt_dev);
3600 return err;
3603 static int __init pktgen_create_thread(int cpu)
3605 struct pktgen_thread *t;
3606 struct proc_dir_entry *pe;
3607 struct task_struct *p;
3609 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3610 if (!t) {
3611 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3612 "create new thread.\n");
3613 return -ENOMEM;
3616 spin_lock_init(&t->if_lock);
3617 t->cpu = cpu;
3619 INIT_LIST_HEAD(&t->if_list);
3621 list_add_tail(&t->th_list, &pktgen_threads);
3623 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3624 if (IS_ERR(p)) {
3625 printk(KERN_ERR "pktgen: kernel_thread() failed "
3626 "for cpu %d\n", t->cpu);
3627 list_del(&t->th_list);
3628 kfree(t);
3629 return PTR_ERR(p);
3631 kthread_bind(p, cpu);
3632 t->tsk = p;
3634 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3635 if (!pe) {
3636 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3637 PG_PROC_DIR, t->tsk->comm);
3638 kthread_stop(p);
3639 list_del(&t->th_list);
3640 kfree(t);
3641 return -EINVAL;
3644 pe->proc_fops = &pktgen_thread_fops;
3645 pe->data = t;
3647 wake_up_process(p);
3649 return 0;
3653 * Removes a device from the thread if_list.
3655 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3656 struct pktgen_dev *pkt_dev)
3658 struct list_head *q, *n;
3659 struct pktgen_dev *p;
3661 list_for_each_safe(q, n, &t->if_list) {
3662 p = list_entry(q, struct pktgen_dev, list);
3663 if (p == pkt_dev)
3664 list_del(&p->list);
3668 static int pktgen_remove_device(struct pktgen_thread *t,
3669 struct pktgen_dev *pkt_dev)
3672 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3674 if (pkt_dev->running) {
3675 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3676 "running interface, stopping it now.\n");
3677 pktgen_stop_device(pkt_dev);
3680 /* Dis-associate from the interface */
3682 if (pkt_dev->odev) {
3683 dev_put(pkt_dev->odev);
3684 pkt_dev->odev = NULL;
3687 /* And update the thread if_list */
3689 _rem_dev_from_if_list(t, pkt_dev);
3691 if (pkt_dev->entry)
3692 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3694 #ifdef CONFIG_XFRM
3695 free_SAs(pkt_dev);
3696 #endif
3697 if (pkt_dev->flows)
3698 vfree(pkt_dev->flows);
3699 kfree(pkt_dev);
3700 return 0;
3703 static int __init pg_init(void)
3705 int cpu;
3706 struct proc_dir_entry *pe;
3708 printk(KERN_INFO "%s", version);
3710 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3711 if (!pg_proc_dir)
3712 return -ENODEV;
3713 pg_proc_dir->owner = THIS_MODULE;
3715 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3716 if (pe == NULL) {
3717 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3718 "procfs entry.\n", PGCTRL);
3719 proc_net_remove(&init_net, PG_PROC_DIR);
3720 return -EINVAL;
3723 pe->proc_fops = &pktgen_fops;
3724 pe->data = NULL;
3726 /* Register us to receive netdevice events */
3727 register_netdevice_notifier(&pktgen_notifier_block);
3729 for_each_online_cpu(cpu) {
3730 int err;
3732 err = pktgen_create_thread(cpu);
3733 if (err)
3734 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3735 "thread for cpu %d (%d)\n", cpu, err);
3738 if (list_empty(&pktgen_threads)) {
3739 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3740 "all threads\n");
3741 unregister_netdevice_notifier(&pktgen_notifier_block);
3742 remove_proc_entry(PGCTRL, pg_proc_dir);
3743 proc_net_remove(&init_net, PG_PROC_DIR);
3744 return -ENODEV;
3747 return 0;
3750 static void __exit pg_cleanup(void)
3752 struct pktgen_thread *t;
3753 struct list_head *q, *n;
3754 wait_queue_head_t queue;
3755 init_waitqueue_head(&queue);
3757 /* Stop all interfaces & threads */
3759 list_for_each_safe(q, n, &pktgen_threads) {
3760 t = list_entry(q, struct pktgen_thread, th_list);
3761 kthread_stop(t->tsk);
3762 kfree(t);
3765 /* Un-register us from receiving netdevice events */
3766 unregister_netdevice_notifier(&pktgen_notifier_block);
3768 /* Clean up proc file system */
3769 remove_proc_entry(PGCTRL, pg_proc_dir);
3770 proc_net_remove(&init_net, PG_PROC_DIR);
3773 module_init(pg_init);
3774 module_exit(pg_cleanup);
3776 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3777 MODULE_DESCRIPTION("Packet Generator tool");
3778 MODULE_LICENSE("GPL");
3779 module_param(pg_count_d, int, 0);
3780 module_param(pg_delay_d, int, 0);
3781 module_param(pg_clone_skb_d, int, 0);
3782 module_param(debug, int, 0);