[ARM] 5113/1: PXA SSP: Additional register definitions for PXA3xx SSP
[zen-stable.git] / net / core / pktgen.c
blob8dca21110493156fbcfc107a39d95343713d94b6
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 #define IP_NAME_SZ 32
174 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
175 #define MPLS_STACK_BOTTOM htonl(0x00000100)
177 /* Device flag bits */
178 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
179 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
180 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
186 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
187 #define F_VID_RND (1<<9) /* Random VLAN ID */
188 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
189 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
190 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
191 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
193 /* Thread control flag bits */
194 #define T_TERMINATE (1<<0)
195 #define T_STOP (1<<1) /* Stop run */
196 #define T_RUN (1<<2) /* Start run */
197 #define T_REMDEVALL (1<<3) /* Remove all devs */
198 #define T_REMDEV (1<<4) /* Remove one dev */
200 /* If lock -- can be removed after some work */
201 #define if_lock(t) spin_lock(&(t->if_lock));
202 #define if_unlock(t) spin_unlock(&(t->if_lock));
204 /* Used to help with determining the pkts on receive */
205 #define PKTGEN_MAGIC 0xbe9be955
206 #define PG_PROC_DIR "pktgen"
207 #define PGCTRL "pgctrl"
208 static struct proc_dir_entry *pg_proc_dir = NULL;
210 #define MAX_CFLOWS 65536
212 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
215 struct flow_state {
216 __be32 cur_daddr;
217 int count;
218 #ifdef CONFIG_XFRM
219 struct xfrm_state *x;
220 #endif
221 __u32 flags;
224 /* flow flag bits */
225 #define F_INIT (1<<0) /* flow has been initialized */
227 struct pktgen_dev {
229 * Try to keep frequent/infrequent used vars. separated.
231 struct proc_dir_entry *entry; /* proc file */
232 struct pktgen_thread *pg_thread;/* the owner */
233 struct list_head list; /* Used for chaining in the thread's run-queue */
235 int running; /* if this changes to false, the test will stop */
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
240 __u32 flags;
241 int removal_mark; /* non-zero => the device is marked for
242 * removal by worker thread */
244 int min_pkt_size; /* = ETH_ZLEN; */
245 int max_pkt_size; /* = ETH_ZLEN; */
246 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
247 int nfrags;
248 __u32 delay_us; /* Default delay */
249 __u32 delay_ns;
250 __u64 count; /* Default No packets to send */
251 __u64 sofar; /* How many pkts we've sent so far */
252 __u64 tx_bytes; /* How many bytes we've transmitted */
253 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
255 /* runtime counters relating to clone_skb */
256 __u64 next_tx_us; /* timestamp of when to tx next */
257 __u32 next_tx_ns;
259 __u64 allocated_skbs;
260 __u32 clone_count;
261 int last_ok; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
265 __u64 started_at; /* micro-seconds */
266 __u64 stopped_at; /* micro-seconds */
267 __u64 idle_acc; /* micro-seconds */
268 __u32 seq_num;
270 int clone_skb; /* Use multiple SKBs during packet gen. If this number
271 * is greater than 1, then that many copies of the same
272 * packet will be sent before a new packet is allocated.
273 * For instance, if you want to send 1024 identical packets
274 * before creating a new packet, set clone_skb to 1024.
277 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 struct in6_addr in6_saddr;
283 struct in6_addr in6_daddr;
284 struct in6_addr cur_in6_daddr;
285 struct in6_addr cur_in6_saddr;
286 /* For ranges */
287 struct in6_addr min_in6_daddr;
288 struct in6_addr max_in6_daddr;
289 struct in6_addr min_in6_saddr;
290 struct in6_addr max_in6_saddr;
292 /* If we're doing ranges, random or incremental, then this
293 * defines the min/max for those ranges.
295 __be32 saddr_min; /* inclusive, source IP address */
296 __be32 saddr_max; /* exclusive, source IP address */
297 __be32 daddr_min; /* inclusive, dest IP address */
298 __be32 daddr_max; /* exclusive, dest IP address */
300 __u16 udp_src_min; /* inclusive, source UDP port */
301 __u16 udp_src_max; /* exclusive, source UDP port */
302 __u16 udp_dst_min; /* inclusive, dest UDP port */
303 __u16 udp_dst_max; /* exclusive, dest UDP port */
305 /* DSCP + ECN */
306 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
307 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
309 /* MPLS */
310 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
311 __be32 labels[MAX_MPLS_LABELS];
313 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
314 __u8 vlan_p;
315 __u8 vlan_cfi;
316 __u16 vlan_id; /* 0xffff means no vlan tag */
318 __u8 svlan_p;
319 __u8 svlan_cfi;
320 __u16 svlan_id; /* 0xffff means no svlan tag */
322 __u32 src_mac_count; /* How many MACs to iterate through */
323 __u32 dst_mac_count; /* How many MACs to iterate through */
325 unsigned char dst_mac[ETH_ALEN];
326 unsigned char src_mac[ETH_ALEN];
328 __u32 cur_dst_mac_offset;
329 __u32 cur_src_mac_offset;
330 __be32 cur_saddr;
331 __be32 cur_daddr;
332 __u16 cur_udp_dst;
333 __u16 cur_udp_src;
334 __u16 cur_queue_map;
335 __u32 cur_pkt_size;
337 __u8 hh[14];
338 /* = {
339 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
341 We fill in SRC address later
342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343 0x08, 0x00
346 __u16 pad; /* pad out the hh struct to an even 16 bytes */
348 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
349 * are transmitting the same one multiple times
351 struct net_device *odev; /* The out-going device. Note that the device should
352 * have it's pg_info pointer pointing back to this
353 * device. This will be set when the user specifies
354 * the out-going device name (not when the inject is
355 * started as it used to do.)
357 struct flow_state *flows;
358 unsigned cflows; /* Concurrent flows (config) */
359 unsigned lflow; /* Flow length (config) */
360 unsigned nflows; /* accumulated flows (stats) */
361 unsigned curfl; /* current sequenced flow (state)*/
363 u16 queue_map_min;
364 u16 queue_map_max;
366 #ifdef CONFIG_XFRM
367 __u8 ipsmode; /* IPSEC mode (config) */
368 __u8 ipsproto; /* IPSEC type (config) */
369 #endif
370 char result[512];
373 struct pktgen_hdr {
374 __be32 pgh_magic;
375 __be32 seq_num;
376 __be32 tv_sec;
377 __be32 tv_usec;
380 struct pktgen_thread {
381 spinlock_t if_lock;
382 struct list_head if_list; /* All device here */
383 struct list_head th_list;
384 struct task_struct *tsk;
385 char result[512];
387 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
389 u32 control;
390 int cpu;
392 wait_queue_head_t queue;
395 #define REMOVE 1
396 #define FIND 0
398 /** Convert to micro-seconds */
399 static inline __u64 tv_to_us(const struct timeval *tv)
401 __u64 us = tv->tv_usec;
402 us += (__u64) tv->tv_sec * (__u64) 1000000;
403 return us;
406 static __u64 getCurUs(void)
408 struct timeval tv;
409 do_gettimeofday(&tv);
410 return tv_to_us(&tv);
413 /* old include end */
415 static char version[] __initdata = VERSION;
417 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
418 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
419 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
420 const char *ifname);
421 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
422 static void pktgen_run_all_threads(void);
423 static void pktgen_stop_all_threads_ifs(void);
424 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
425 static void pktgen_stop(struct pktgen_thread *t);
426 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
428 static unsigned int scan_ip6(const char *s, char ip[16]);
429 static unsigned int fmt_ip6(char *s, const char ip[16]);
431 /* Module parameters, defaults. */
432 static int pg_count_d = 1000; /* 1000 pkts by default */
433 static int pg_delay_d;
434 static int pg_clone_skb_d;
435 static int debug;
437 static DEFINE_MUTEX(pktgen_thread_lock);
438 static LIST_HEAD(pktgen_threads);
440 static struct notifier_block pktgen_notifier_block = {
441 .notifier_call = pktgen_device_event,
445 * /proc handling functions
449 static int pgctrl_show(struct seq_file *seq, void *v)
451 seq_puts(seq, VERSION);
452 return 0;
455 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
456 size_t count, loff_t * ppos)
458 int err = 0;
459 char data[128];
461 if (!capable(CAP_NET_ADMIN)) {
462 err = -EPERM;
463 goto out;
466 if (count > sizeof(data))
467 count = sizeof(data);
469 if (copy_from_user(data, buf, count)) {
470 err = -EFAULT;
471 goto out;
473 data[count - 1] = 0; /* Make string */
475 if (!strcmp(data, "stop"))
476 pktgen_stop_all_threads_ifs();
478 else if (!strcmp(data, "start"))
479 pktgen_run_all_threads();
481 else
482 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
484 err = count;
486 out:
487 return err;
490 static int pgctrl_open(struct inode *inode, struct file *file)
492 return single_open(file, pgctrl_show, PDE(inode)->data);
495 static const struct file_operations pktgen_fops = {
496 .owner = THIS_MODULE,
497 .open = pgctrl_open,
498 .read = seq_read,
499 .llseek = seq_lseek,
500 .write = pgctrl_write,
501 .release = single_release,
504 static int pktgen_if_show(struct seq_file *seq, void *v)
506 struct pktgen_dev *pkt_dev = seq->private;
507 __u64 sa;
508 __u64 stopped;
509 __u64 now = getCurUs();
510 DECLARE_MAC_BUF(mac);
512 seq_printf(seq,
513 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
514 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
515 pkt_dev->max_pkt_size);
517 seq_printf(seq,
518 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
519 pkt_dev->nfrags,
520 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
521 pkt_dev->clone_skb, pkt_dev->odev->name);
523 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
524 pkt_dev->lflow);
526 seq_printf(seq,
527 " queue_map_min: %u queue_map_max: %u\n",
528 pkt_dev->queue_map_min,
529 pkt_dev->queue_map_max);
531 if (pkt_dev->flags & F_IPV6) {
532 char b1[128], b2[128], b3[128];
533 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
534 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
535 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
536 seq_printf(seq,
537 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
538 b2, b3);
540 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
541 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
542 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
543 seq_printf(seq,
544 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
545 b2, b3);
547 } else
548 seq_printf(seq,
549 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
550 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
551 pkt_dev->src_max);
553 seq_puts(seq, " src_mac: ");
555 seq_printf(seq, "%s ",
556 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
557 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
559 seq_printf(seq, "dst_mac: ");
560 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
562 seq_printf(seq,
563 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
564 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
565 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
567 seq_printf(seq,
568 " src_mac_count: %d dst_mac_count: %d\n",
569 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
571 if (pkt_dev->nr_labels) {
572 unsigned i;
573 seq_printf(seq, " mpls: ");
574 for (i = 0; i < pkt_dev->nr_labels; i++)
575 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
576 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
579 if (pkt_dev->vlan_id != 0xffff) {
580 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
581 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
584 if (pkt_dev->svlan_id != 0xffff) {
585 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
586 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
589 if (pkt_dev->tos) {
590 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
593 if (pkt_dev->traffic_class) {
594 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
597 seq_printf(seq, " Flags: ");
599 if (pkt_dev->flags & F_IPV6)
600 seq_printf(seq, "IPV6 ");
602 if (pkt_dev->flags & F_IPSRC_RND)
603 seq_printf(seq, "IPSRC_RND ");
605 if (pkt_dev->flags & F_IPDST_RND)
606 seq_printf(seq, "IPDST_RND ");
608 if (pkt_dev->flags & F_TXSIZE_RND)
609 seq_printf(seq, "TXSIZE_RND ");
611 if (pkt_dev->flags & F_UDPSRC_RND)
612 seq_printf(seq, "UDPSRC_RND ");
614 if (pkt_dev->flags & F_UDPDST_RND)
615 seq_printf(seq, "UDPDST_RND ");
617 if (pkt_dev->flags & F_MPLS_RND)
618 seq_printf(seq, "MPLS_RND ");
620 if (pkt_dev->flags & F_QUEUE_MAP_RND)
621 seq_printf(seq, "QUEUE_MAP_RND ");
623 if (pkt_dev->cflows) {
624 if (pkt_dev->flags & F_FLOW_SEQ)
625 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
626 else
627 seq_printf(seq, "FLOW_RND ");
630 #ifdef CONFIG_XFRM
631 if (pkt_dev->flags & F_IPSEC_ON)
632 seq_printf(seq, "IPSEC ");
633 #endif
635 if (pkt_dev->flags & F_MACSRC_RND)
636 seq_printf(seq, "MACSRC_RND ");
638 if (pkt_dev->flags & F_MACDST_RND)
639 seq_printf(seq, "MACDST_RND ");
641 if (pkt_dev->flags & F_VID_RND)
642 seq_printf(seq, "VID_RND ");
644 if (pkt_dev->flags & F_SVID_RND)
645 seq_printf(seq, "SVID_RND ");
647 seq_puts(seq, "\n");
649 sa = pkt_dev->started_at;
650 stopped = pkt_dev->stopped_at;
651 if (pkt_dev->running)
652 stopped = now; /* not really stopped, more like last-running-at */
654 seq_printf(seq,
655 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
656 (unsigned long long)pkt_dev->sofar,
657 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
658 (unsigned long long)stopped,
659 (unsigned long long)pkt_dev->idle_acc);
661 seq_printf(seq,
662 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
663 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
664 pkt_dev->cur_src_mac_offset);
666 if (pkt_dev->flags & F_IPV6) {
667 char b1[128], b2[128];
668 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
669 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
670 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
671 } else
672 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
673 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
675 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
676 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
678 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
680 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
682 if (pkt_dev->result[0])
683 seq_printf(seq, "Result: %s\n", pkt_dev->result);
684 else
685 seq_printf(seq, "Result: Idle\n");
687 return 0;
691 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
693 int i = 0;
694 *num = 0;
696 for (; i < maxlen; i++) {
697 char c;
698 *num <<= 4;
699 if (get_user(c, &user_buffer[i]))
700 return -EFAULT;
701 if ((c >= '0') && (c <= '9'))
702 *num |= c - '0';
703 else if ((c >= 'a') && (c <= 'f'))
704 *num |= c - 'a' + 10;
705 else if ((c >= 'A') && (c <= 'F'))
706 *num |= c - 'A' + 10;
707 else
708 break;
710 return i;
713 static int count_trail_chars(const char __user * user_buffer,
714 unsigned int maxlen)
716 int i;
718 for (i = 0; i < maxlen; i++) {
719 char c;
720 if (get_user(c, &user_buffer[i]))
721 return -EFAULT;
722 switch (c) {
723 case '\"':
724 case '\n':
725 case '\r':
726 case '\t':
727 case ' ':
728 case '=':
729 break;
730 default:
731 goto done;
734 done:
735 return i;
738 static unsigned long num_arg(const char __user * user_buffer,
739 unsigned long maxlen, unsigned long *num)
741 int i = 0;
742 *num = 0;
744 for (; i < maxlen; i++) {
745 char c;
746 if (get_user(c, &user_buffer[i]))
747 return -EFAULT;
748 if ((c >= '0') && (c <= '9')) {
749 *num *= 10;
750 *num += c - '0';
751 } else
752 break;
754 return i;
757 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
759 int i = 0;
761 for (; i < maxlen; i++) {
762 char c;
763 if (get_user(c, &user_buffer[i]))
764 return -EFAULT;
765 switch (c) {
766 case '\"':
767 case '\n':
768 case '\r':
769 case '\t':
770 case ' ':
771 goto done_str;
772 break;
773 default:
774 break;
777 done_str:
778 return i;
781 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
783 unsigned n = 0;
784 char c;
785 ssize_t i = 0;
786 int len;
788 pkt_dev->nr_labels = 0;
789 do {
790 __u32 tmp;
791 len = hex32_arg(&buffer[i], 8, &tmp);
792 if (len <= 0)
793 return len;
794 pkt_dev->labels[n] = htonl(tmp);
795 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
796 pkt_dev->flags |= F_MPLS_RND;
797 i += len;
798 if (get_user(c, &buffer[i]))
799 return -EFAULT;
800 i++;
801 n++;
802 if (n >= MAX_MPLS_LABELS)
803 return -E2BIG;
804 } while (c == ',');
806 pkt_dev->nr_labels = n;
807 return i;
810 static ssize_t pktgen_if_write(struct file *file,
811 const char __user * user_buffer, size_t count,
812 loff_t * offset)
814 struct seq_file *seq = (struct seq_file *)file->private_data;
815 struct pktgen_dev *pkt_dev = seq->private;
816 int i = 0, max, len;
817 char name[16], valstr[32];
818 unsigned long value = 0;
819 char *pg_result = NULL;
820 int tmp = 0;
821 char buf[128];
823 pg_result = &(pkt_dev->result[0]);
825 if (count < 1) {
826 printk(KERN_WARNING "pktgen: wrong command format\n");
827 return -EINVAL;
830 max = count - i;
831 tmp = count_trail_chars(&user_buffer[i], max);
832 if (tmp < 0) {
833 printk(KERN_WARNING "pktgen: illegal format\n");
834 return tmp;
836 i += tmp;
838 /* Read variable name */
840 len = strn_len(&user_buffer[i], sizeof(name) - 1);
841 if (len < 0) {
842 return len;
844 memset(name, 0, sizeof(name));
845 if (copy_from_user(name, &user_buffer[i], len))
846 return -EFAULT;
847 i += len;
849 max = count - i;
850 len = count_trail_chars(&user_buffer[i], max);
851 if (len < 0)
852 return len;
854 i += len;
856 if (debug) {
857 char tb[count + 1];
858 if (copy_from_user(tb, user_buffer, count))
859 return -EFAULT;
860 tb[count] = 0;
861 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
862 (unsigned long)count, tb);
865 if (!strcmp(name, "min_pkt_size")) {
866 len = num_arg(&user_buffer[i], 10, &value);
867 if (len < 0) {
868 return len;
870 i += len;
871 if (value < 14 + 20 + 8)
872 value = 14 + 20 + 8;
873 if (value != pkt_dev->min_pkt_size) {
874 pkt_dev->min_pkt_size = value;
875 pkt_dev->cur_pkt_size = value;
877 sprintf(pg_result, "OK: min_pkt_size=%u",
878 pkt_dev->min_pkt_size);
879 return count;
882 if (!strcmp(name, "max_pkt_size")) {
883 len = num_arg(&user_buffer[i], 10, &value);
884 if (len < 0) {
885 return len;
887 i += len;
888 if (value < 14 + 20 + 8)
889 value = 14 + 20 + 8;
890 if (value != pkt_dev->max_pkt_size) {
891 pkt_dev->max_pkt_size = value;
892 pkt_dev->cur_pkt_size = value;
894 sprintf(pg_result, "OK: max_pkt_size=%u",
895 pkt_dev->max_pkt_size);
896 return count;
899 /* Shortcut for min = max */
901 if (!strcmp(name, "pkt_size")) {
902 len = num_arg(&user_buffer[i], 10, &value);
903 if (len < 0) {
904 return len;
906 i += len;
907 if (value < 14 + 20 + 8)
908 value = 14 + 20 + 8;
909 if (value != pkt_dev->min_pkt_size) {
910 pkt_dev->min_pkt_size = value;
911 pkt_dev->max_pkt_size = value;
912 pkt_dev->cur_pkt_size = value;
914 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
915 return count;
918 if (!strcmp(name, "debug")) {
919 len = num_arg(&user_buffer[i], 10, &value);
920 if (len < 0) {
921 return len;
923 i += len;
924 debug = value;
925 sprintf(pg_result, "OK: debug=%u", debug);
926 return count;
929 if (!strcmp(name, "frags")) {
930 len = num_arg(&user_buffer[i], 10, &value);
931 if (len < 0) {
932 return len;
934 i += len;
935 pkt_dev->nfrags = value;
936 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
937 return count;
939 if (!strcmp(name, "delay")) {
940 len = num_arg(&user_buffer[i], 10, &value);
941 if (len < 0) {
942 return len;
944 i += len;
945 if (value == 0x7FFFFFFF) {
946 pkt_dev->delay_us = 0x7FFFFFFF;
947 pkt_dev->delay_ns = 0;
948 } else {
949 pkt_dev->delay_us = value / 1000;
950 pkt_dev->delay_ns = value % 1000;
952 sprintf(pg_result, "OK: delay=%u",
953 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
954 return count;
956 if (!strcmp(name, "udp_src_min")) {
957 len = num_arg(&user_buffer[i], 10, &value);
958 if (len < 0) {
959 return len;
961 i += len;
962 if (value != pkt_dev->udp_src_min) {
963 pkt_dev->udp_src_min = value;
964 pkt_dev->cur_udp_src = value;
966 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
967 return count;
969 if (!strcmp(name, "udp_dst_min")) {
970 len = num_arg(&user_buffer[i], 10, &value);
971 if (len < 0) {
972 return len;
974 i += len;
975 if (value != pkt_dev->udp_dst_min) {
976 pkt_dev->udp_dst_min = value;
977 pkt_dev->cur_udp_dst = value;
979 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
980 return count;
982 if (!strcmp(name, "udp_src_max")) {
983 len = num_arg(&user_buffer[i], 10, &value);
984 if (len < 0) {
985 return len;
987 i += len;
988 if (value != pkt_dev->udp_src_max) {
989 pkt_dev->udp_src_max = value;
990 pkt_dev->cur_udp_src = value;
992 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
993 return count;
995 if (!strcmp(name, "udp_dst_max")) {
996 len = num_arg(&user_buffer[i], 10, &value);
997 if (len < 0) {
998 return len;
1000 i += len;
1001 if (value != pkt_dev->udp_dst_max) {
1002 pkt_dev->udp_dst_max = value;
1003 pkt_dev->cur_udp_dst = value;
1005 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1006 return count;
1008 if (!strcmp(name, "clone_skb")) {
1009 len = num_arg(&user_buffer[i], 10, &value);
1010 if (len < 0) {
1011 return len;
1013 i += len;
1014 pkt_dev->clone_skb = value;
1016 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1017 return count;
1019 if (!strcmp(name, "count")) {
1020 len = num_arg(&user_buffer[i], 10, &value);
1021 if (len < 0) {
1022 return len;
1024 i += len;
1025 pkt_dev->count = value;
1026 sprintf(pg_result, "OK: count=%llu",
1027 (unsigned long long)pkt_dev->count);
1028 return count;
1030 if (!strcmp(name, "src_mac_count")) {
1031 len = num_arg(&user_buffer[i], 10, &value);
1032 if (len < 0) {
1033 return len;
1035 i += len;
1036 if (pkt_dev->src_mac_count != value) {
1037 pkt_dev->src_mac_count = value;
1038 pkt_dev->cur_src_mac_offset = 0;
1040 sprintf(pg_result, "OK: src_mac_count=%d",
1041 pkt_dev->src_mac_count);
1042 return count;
1044 if (!strcmp(name, "dst_mac_count")) {
1045 len = num_arg(&user_buffer[i], 10, &value);
1046 if (len < 0) {
1047 return len;
1049 i += len;
1050 if (pkt_dev->dst_mac_count != value) {
1051 pkt_dev->dst_mac_count = value;
1052 pkt_dev->cur_dst_mac_offset = 0;
1054 sprintf(pg_result, "OK: dst_mac_count=%d",
1055 pkt_dev->dst_mac_count);
1056 return count;
1058 if (!strcmp(name, "flag")) {
1059 char f[32];
1060 memset(f, 0, 32);
1061 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1062 if (len < 0) {
1063 return len;
1065 if (copy_from_user(f, &user_buffer[i], len))
1066 return -EFAULT;
1067 i += len;
1068 if (strcmp(f, "IPSRC_RND") == 0)
1069 pkt_dev->flags |= F_IPSRC_RND;
1071 else if (strcmp(f, "!IPSRC_RND") == 0)
1072 pkt_dev->flags &= ~F_IPSRC_RND;
1074 else if (strcmp(f, "TXSIZE_RND") == 0)
1075 pkt_dev->flags |= F_TXSIZE_RND;
1077 else if (strcmp(f, "!TXSIZE_RND") == 0)
1078 pkt_dev->flags &= ~F_TXSIZE_RND;
1080 else if (strcmp(f, "IPDST_RND") == 0)
1081 pkt_dev->flags |= F_IPDST_RND;
1083 else if (strcmp(f, "!IPDST_RND") == 0)
1084 pkt_dev->flags &= ~F_IPDST_RND;
1086 else if (strcmp(f, "UDPSRC_RND") == 0)
1087 pkt_dev->flags |= F_UDPSRC_RND;
1089 else if (strcmp(f, "!UDPSRC_RND") == 0)
1090 pkt_dev->flags &= ~F_UDPSRC_RND;
1092 else if (strcmp(f, "UDPDST_RND") == 0)
1093 pkt_dev->flags |= F_UDPDST_RND;
1095 else if (strcmp(f, "!UDPDST_RND") == 0)
1096 pkt_dev->flags &= ~F_UDPDST_RND;
1098 else if (strcmp(f, "MACSRC_RND") == 0)
1099 pkt_dev->flags |= F_MACSRC_RND;
1101 else if (strcmp(f, "!MACSRC_RND") == 0)
1102 pkt_dev->flags &= ~F_MACSRC_RND;
1104 else if (strcmp(f, "MACDST_RND") == 0)
1105 pkt_dev->flags |= F_MACDST_RND;
1107 else if (strcmp(f, "!MACDST_RND") == 0)
1108 pkt_dev->flags &= ~F_MACDST_RND;
1110 else if (strcmp(f, "MPLS_RND") == 0)
1111 pkt_dev->flags |= F_MPLS_RND;
1113 else if (strcmp(f, "!MPLS_RND") == 0)
1114 pkt_dev->flags &= ~F_MPLS_RND;
1116 else if (strcmp(f, "VID_RND") == 0)
1117 pkt_dev->flags |= F_VID_RND;
1119 else if (strcmp(f, "!VID_RND") == 0)
1120 pkt_dev->flags &= ~F_VID_RND;
1122 else if (strcmp(f, "SVID_RND") == 0)
1123 pkt_dev->flags |= F_SVID_RND;
1125 else if (strcmp(f, "!SVID_RND") == 0)
1126 pkt_dev->flags &= ~F_SVID_RND;
1128 else if (strcmp(f, "FLOW_SEQ") == 0)
1129 pkt_dev->flags |= F_FLOW_SEQ;
1131 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1132 pkt_dev->flags |= F_QUEUE_MAP_RND;
1134 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1135 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1136 #ifdef CONFIG_XFRM
1137 else if (strcmp(f, "IPSEC") == 0)
1138 pkt_dev->flags |= F_IPSEC_ON;
1139 #endif
1141 else if (strcmp(f, "!IPV6") == 0)
1142 pkt_dev->flags &= ~F_IPV6;
1144 else {
1145 sprintf(pg_result,
1146 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1148 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1149 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1150 return count;
1152 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1153 return count;
1155 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1156 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1157 if (len < 0) {
1158 return len;
1161 if (copy_from_user(buf, &user_buffer[i], len))
1162 return -EFAULT;
1163 buf[len] = 0;
1164 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1165 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1166 strncpy(pkt_dev->dst_min, buf, len);
1167 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1168 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1170 if (debug)
1171 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1172 pkt_dev->dst_min);
1173 i += len;
1174 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1175 return count;
1177 if (!strcmp(name, "dst_max")) {
1178 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1179 if (len < 0) {
1180 return len;
1183 if (copy_from_user(buf, &user_buffer[i], len))
1184 return -EFAULT;
1186 buf[len] = 0;
1187 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1188 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1189 strncpy(pkt_dev->dst_max, buf, len);
1190 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1191 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1193 if (debug)
1194 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1195 pkt_dev->dst_max);
1196 i += len;
1197 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1198 return count;
1200 if (!strcmp(name, "dst6")) {
1201 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1202 if (len < 0)
1203 return len;
1205 pkt_dev->flags |= F_IPV6;
1207 if (copy_from_user(buf, &user_buffer[i], len))
1208 return -EFAULT;
1209 buf[len] = 0;
1211 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1212 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1214 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1216 if (debug)
1217 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1219 i += len;
1220 sprintf(pg_result, "OK: dst6=%s", buf);
1221 return count;
1223 if (!strcmp(name, "dst6_min")) {
1224 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1225 if (len < 0)
1226 return len;
1228 pkt_dev->flags |= F_IPV6;
1230 if (copy_from_user(buf, &user_buffer[i], len))
1231 return -EFAULT;
1232 buf[len] = 0;
1234 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1235 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1237 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1238 &pkt_dev->min_in6_daddr);
1239 if (debug)
1240 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1242 i += len;
1243 sprintf(pg_result, "OK: dst6_min=%s", buf);
1244 return count;
1246 if (!strcmp(name, "dst6_max")) {
1247 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1248 if (len < 0)
1249 return len;
1251 pkt_dev->flags |= F_IPV6;
1253 if (copy_from_user(buf, &user_buffer[i], len))
1254 return -EFAULT;
1255 buf[len] = 0;
1257 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1258 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1260 if (debug)
1261 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1263 i += len;
1264 sprintf(pg_result, "OK: dst6_max=%s", buf);
1265 return count;
1267 if (!strcmp(name, "src6")) {
1268 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1269 if (len < 0)
1270 return len;
1272 pkt_dev->flags |= F_IPV6;
1274 if (copy_from_user(buf, &user_buffer[i], len))
1275 return -EFAULT;
1276 buf[len] = 0;
1278 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1279 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1281 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1283 if (debug)
1284 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1286 i += len;
1287 sprintf(pg_result, "OK: src6=%s", buf);
1288 return count;
1290 if (!strcmp(name, "src_min")) {
1291 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1292 if (len < 0) {
1293 return len;
1295 if (copy_from_user(buf, &user_buffer[i], len))
1296 return -EFAULT;
1297 buf[len] = 0;
1298 if (strcmp(buf, pkt_dev->src_min) != 0) {
1299 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1300 strncpy(pkt_dev->src_min, buf, len);
1301 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1302 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1304 if (debug)
1305 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1306 pkt_dev->src_min);
1307 i += len;
1308 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1309 return count;
1311 if (!strcmp(name, "src_max")) {
1312 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1313 if (len < 0) {
1314 return len;
1316 if (copy_from_user(buf, &user_buffer[i], len))
1317 return -EFAULT;
1318 buf[len] = 0;
1319 if (strcmp(buf, pkt_dev->src_max) != 0) {
1320 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1321 strncpy(pkt_dev->src_max, buf, len);
1322 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1323 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1325 if (debug)
1326 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1327 pkt_dev->src_max);
1328 i += len;
1329 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1330 return count;
1332 if (!strcmp(name, "dst_mac")) {
1333 char *v = valstr;
1334 unsigned char old_dmac[ETH_ALEN];
1335 unsigned char *m = pkt_dev->dst_mac;
1336 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1338 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1339 if (len < 0) {
1340 return len;
1342 memset(valstr, 0, sizeof(valstr));
1343 if (copy_from_user(valstr, &user_buffer[i], len))
1344 return -EFAULT;
1345 i += len;
1347 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1348 if (*v >= '0' && *v <= '9') {
1349 *m *= 16;
1350 *m += *v - '0';
1352 if (*v >= 'A' && *v <= 'F') {
1353 *m *= 16;
1354 *m += *v - 'A' + 10;
1356 if (*v >= 'a' && *v <= 'f') {
1357 *m *= 16;
1358 *m += *v - 'a' + 10;
1360 if (*v == ':') {
1361 m++;
1362 *m = 0;
1366 /* Set up Dest MAC */
1367 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1368 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1370 sprintf(pg_result, "OK: dstmac");
1371 return count;
1373 if (!strcmp(name, "src_mac")) {
1374 char *v = valstr;
1375 unsigned char old_smac[ETH_ALEN];
1376 unsigned char *m = pkt_dev->src_mac;
1378 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1380 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1381 if (len < 0) {
1382 return len;
1384 memset(valstr, 0, sizeof(valstr));
1385 if (copy_from_user(valstr, &user_buffer[i], len))
1386 return -EFAULT;
1387 i += len;
1389 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1390 if (*v >= '0' && *v <= '9') {
1391 *m *= 16;
1392 *m += *v - '0';
1394 if (*v >= 'A' && *v <= 'F') {
1395 *m *= 16;
1396 *m += *v - 'A' + 10;
1398 if (*v >= 'a' && *v <= 'f') {
1399 *m *= 16;
1400 *m += *v - 'a' + 10;
1402 if (*v == ':') {
1403 m++;
1404 *m = 0;
1408 /* Set up Src MAC */
1409 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1410 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1412 sprintf(pg_result, "OK: srcmac");
1413 return count;
1416 if (!strcmp(name, "clear_counters")) {
1417 pktgen_clear_counters(pkt_dev);
1418 sprintf(pg_result, "OK: Clearing counters.\n");
1419 return count;
1422 if (!strcmp(name, "flows")) {
1423 len = num_arg(&user_buffer[i], 10, &value);
1424 if (len < 0) {
1425 return len;
1427 i += len;
1428 if (value > MAX_CFLOWS)
1429 value = MAX_CFLOWS;
1431 pkt_dev->cflows = value;
1432 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1433 return count;
1436 if (!strcmp(name, "flowlen")) {
1437 len = num_arg(&user_buffer[i], 10, &value);
1438 if (len < 0) {
1439 return len;
1441 i += len;
1442 pkt_dev->lflow = value;
1443 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1444 return count;
1447 if (!strcmp(name, "queue_map_min")) {
1448 len = num_arg(&user_buffer[i], 5, &value);
1449 if (len < 0) {
1450 return len;
1452 i += len;
1453 pkt_dev->queue_map_min = value;
1454 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1455 return count;
1458 if (!strcmp(name, "queue_map_max")) {
1459 len = num_arg(&user_buffer[i], 5, &value);
1460 if (len < 0) {
1461 return len;
1463 i += len;
1464 pkt_dev->queue_map_max = value;
1465 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1466 return count;
1469 if (!strcmp(name, "mpls")) {
1470 unsigned n, cnt;
1472 len = get_labels(&user_buffer[i], pkt_dev);
1473 if (len < 0)
1474 return len;
1475 i += len;
1476 cnt = sprintf(pg_result, "OK: mpls=");
1477 for (n = 0; n < pkt_dev->nr_labels; n++)
1478 cnt += sprintf(pg_result + cnt,
1479 "%08x%s", ntohl(pkt_dev->labels[n]),
1480 n == pkt_dev->nr_labels-1 ? "" : ",");
1482 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1483 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1484 pkt_dev->svlan_id = 0xffff;
1486 if (debug)
1487 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1489 return count;
1492 if (!strcmp(name, "vlan_id")) {
1493 len = num_arg(&user_buffer[i], 4, &value);
1494 if (len < 0) {
1495 return len;
1497 i += len;
1498 if (value <= 4095) {
1499 pkt_dev->vlan_id = value; /* turn on VLAN */
1501 if (debug)
1502 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1504 if (debug && pkt_dev->nr_labels)
1505 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1507 pkt_dev->nr_labels = 0; /* turn off MPLS */
1508 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1509 } else {
1510 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1511 pkt_dev->svlan_id = 0xffff;
1513 if (debug)
1514 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1516 return count;
1519 if (!strcmp(name, "vlan_p")) {
1520 len = num_arg(&user_buffer[i], 1, &value);
1521 if (len < 0) {
1522 return len;
1524 i += len;
1525 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1526 pkt_dev->vlan_p = value;
1527 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1528 } else {
1529 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1531 return count;
1534 if (!strcmp(name, "vlan_cfi")) {
1535 len = num_arg(&user_buffer[i], 1, &value);
1536 if (len < 0) {
1537 return len;
1539 i += len;
1540 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1541 pkt_dev->vlan_cfi = value;
1542 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1543 } else {
1544 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1546 return count;
1549 if (!strcmp(name, "svlan_id")) {
1550 len = num_arg(&user_buffer[i], 4, &value);
1551 if (len < 0) {
1552 return len;
1554 i += len;
1555 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1556 pkt_dev->svlan_id = value; /* turn on SVLAN */
1558 if (debug)
1559 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1561 if (debug && pkt_dev->nr_labels)
1562 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1564 pkt_dev->nr_labels = 0; /* turn off MPLS */
1565 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1566 } else {
1567 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1568 pkt_dev->svlan_id = 0xffff;
1570 if (debug)
1571 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1573 return count;
1576 if (!strcmp(name, "svlan_p")) {
1577 len = num_arg(&user_buffer[i], 1, &value);
1578 if (len < 0) {
1579 return len;
1581 i += len;
1582 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1583 pkt_dev->svlan_p = value;
1584 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1585 } else {
1586 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1588 return count;
1591 if (!strcmp(name, "svlan_cfi")) {
1592 len = num_arg(&user_buffer[i], 1, &value);
1593 if (len < 0) {
1594 return len;
1596 i += len;
1597 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1598 pkt_dev->svlan_cfi = value;
1599 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1600 } else {
1601 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1603 return count;
1606 if (!strcmp(name, "tos")) {
1607 __u32 tmp_value = 0;
1608 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1609 if (len < 0) {
1610 return len;
1612 i += len;
1613 if (len == 2) {
1614 pkt_dev->tos = tmp_value;
1615 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1616 } else {
1617 sprintf(pg_result, "ERROR: tos must be 00-ff");
1619 return count;
1622 if (!strcmp(name, "traffic_class")) {
1623 __u32 tmp_value = 0;
1624 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1625 if (len < 0) {
1626 return len;
1628 i += len;
1629 if (len == 2) {
1630 pkt_dev->traffic_class = tmp_value;
1631 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1632 } else {
1633 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1635 return count;
1638 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1639 return -EINVAL;
1642 static int pktgen_if_open(struct inode *inode, struct file *file)
1644 return single_open(file, pktgen_if_show, PDE(inode)->data);
1647 static const struct file_operations pktgen_if_fops = {
1648 .owner = THIS_MODULE,
1649 .open = pktgen_if_open,
1650 .read = seq_read,
1651 .llseek = seq_lseek,
1652 .write = pktgen_if_write,
1653 .release = single_release,
1656 static int pktgen_thread_show(struct seq_file *seq, void *v)
1658 struct pktgen_thread *t = seq->private;
1659 struct pktgen_dev *pkt_dev;
1661 BUG_ON(!t);
1663 seq_printf(seq, "Running: ");
1665 if_lock(t);
1666 list_for_each_entry(pkt_dev, &t->if_list, list)
1667 if (pkt_dev->running)
1668 seq_printf(seq, "%s ", pkt_dev->odev->name);
1670 seq_printf(seq, "\nStopped: ");
1672 list_for_each_entry(pkt_dev, &t->if_list, list)
1673 if (!pkt_dev->running)
1674 seq_printf(seq, "%s ", pkt_dev->odev->name);
1676 if (t->result[0])
1677 seq_printf(seq, "\nResult: %s\n", t->result);
1678 else
1679 seq_printf(seq, "\nResult: NA\n");
1681 if_unlock(t);
1683 return 0;
1686 static ssize_t pktgen_thread_write(struct file *file,
1687 const char __user * user_buffer,
1688 size_t count, loff_t * offset)
1690 struct seq_file *seq = (struct seq_file *)file->private_data;
1691 struct pktgen_thread *t = seq->private;
1692 int i = 0, max, len, ret;
1693 char name[40];
1694 char *pg_result;
1696 if (count < 1) {
1697 // sprintf(pg_result, "Wrong command format");
1698 return -EINVAL;
1701 max = count - i;
1702 len = count_trail_chars(&user_buffer[i], max);
1703 if (len < 0)
1704 return len;
1706 i += len;
1708 /* Read variable name */
1710 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1711 if (len < 0)
1712 return len;
1714 memset(name, 0, sizeof(name));
1715 if (copy_from_user(name, &user_buffer[i], len))
1716 return -EFAULT;
1717 i += len;
1719 max = count - i;
1720 len = count_trail_chars(&user_buffer[i], max);
1721 if (len < 0)
1722 return len;
1724 i += len;
1726 if (debug)
1727 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1728 name, (unsigned long)count);
1730 if (!t) {
1731 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1732 ret = -EINVAL;
1733 goto out;
1736 pg_result = &(t->result[0]);
1738 if (!strcmp(name, "add_device")) {
1739 char f[32];
1740 memset(f, 0, 32);
1741 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1742 if (len < 0) {
1743 ret = len;
1744 goto out;
1746 if (copy_from_user(f, &user_buffer[i], len))
1747 return -EFAULT;
1748 i += len;
1749 mutex_lock(&pktgen_thread_lock);
1750 pktgen_add_device(t, f);
1751 mutex_unlock(&pktgen_thread_lock);
1752 ret = count;
1753 sprintf(pg_result, "OK: add_device=%s", f);
1754 goto out;
1757 if (!strcmp(name, "rem_device_all")) {
1758 mutex_lock(&pktgen_thread_lock);
1759 t->control |= T_REMDEVALL;
1760 mutex_unlock(&pktgen_thread_lock);
1761 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1762 ret = count;
1763 sprintf(pg_result, "OK: rem_device_all");
1764 goto out;
1767 if (!strcmp(name, "max_before_softirq")) {
1768 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1769 ret = count;
1770 goto out;
1773 ret = -EINVAL;
1774 out:
1775 return ret;
1778 static int pktgen_thread_open(struct inode *inode, struct file *file)
1780 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1783 static const struct file_operations pktgen_thread_fops = {
1784 .owner = THIS_MODULE,
1785 .open = pktgen_thread_open,
1786 .read = seq_read,
1787 .llseek = seq_lseek,
1788 .write = pktgen_thread_write,
1789 .release = single_release,
1792 /* Think find or remove for NN */
1793 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1795 struct pktgen_thread *t;
1796 struct pktgen_dev *pkt_dev = NULL;
1798 list_for_each_entry(t, &pktgen_threads, th_list) {
1799 pkt_dev = pktgen_find_dev(t, ifname);
1800 if (pkt_dev) {
1801 if (remove) {
1802 if_lock(t);
1803 pkt_dev->removal_mark = 1;
1804 t->control |= T_REMDEV;
1805 if_unlock(t);
1807 break;
1810 return pkt_dev;
1814 * mark a device for removal
1816 static void pktgen_mark_device(const char *ifname)
1818 struct pktgen_dev *pkt_dev = NULL;
1819 const int max_tries = 10, msec_per_try = 125;
1820 int i = 0;
1822 mutex_lock(&pktgen_thread_lock);
1823 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1825 while (1) {
1827 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1828 if (pkt_dev == NULL)
1829 break; /* success */
1831 mutex_unlock(&pktgen_thread_lock);
1832 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1833 "to disappear....\n", ifname);
1834 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1835 mutex_lock(&pktgen_thread_lock);
1837 if (++i >= max_tries) {
1838 printk(KERN_ERR "pktgen_mark_device: timed out after "
1839 "waiting %d msec for device %s to be removed\n",
1840 msec_per_try * i, ifname);
1841 break;
1846 mutex_unlock(&pktgen_thread_lock);
1849 static void pktgen_change_name(struct net_device *dev)
1851 struct pktgen_thread *t;
1853 list_for_each_entry(t, &pktgen_threads, th_list) {
1854 struct pktgen_dev *pkt_dev;
1856 list_for_each_entry(pkt_dev, &t->if_list, list) {
1857 if (pkt_dev->odev != dev)
1858 continue;
1860 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1862 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1863 pg_proc_dir);
1864 if (!pkt_dev->entry)
1865 printk(KERN_ERR "pktgen: can't move proc "
1866 " entry for '%s'\n", dev->name);
1867 break;
1872 static int pktgen_device_event(struct notifier_block *unused,
1873 unsigned long event, void *ptr)
1875 struct net_device *dev = ptr;
1877 if (dev_net(dev) != &init_net)
1878 return NOTIFY_DONE;
1880 /* It is OK that we do not hold the group lock right now,
1881 * as we run under the RTNL lock.
1884 switch (event) {
1885 case NETDEV_CHANGENAME:
1886 pktgen_change_name(dev);
1887 break;
1889 case NETDEV_UNREGISTER:
1890 pktgen_mark_device(dev->name);
1891 break;
1894 return NOTIFY_DONE;
1897 /* Associate pktgen_dev with a device. */
1899 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1901 struct net_device *odev;
1902 int err;
1904 /* Clean old setups */
1905 if (pkt_dev->odev) {
1906 dev_put(pkt_dev->odev);
1907 pkt_dev->odev = NULL;
1910 odev = dev_get_by_name(&init_net, ifname);
1911 if (!odev) {
1912 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1913 return -ENODEV;
1916 if (odev->type != ARPHRD_ETHER) {
1917 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1918 err = -EINVAL;
1919 } else if (!netif_running(odev)) {
1920 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1921 err = -ENETDOWN;
1922 } else {
1923 pkt_dev->odev = odev;
1924 return 0;
1927 dev_put(odev);
1928 return err;
1931 /* Read pkt_dev from the interface and set up internal pktgen_dev
1932 * structure to have the right information to create/send packets
1934 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1936 if (!pkt_dev->odev) {
1937 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1938 "setup_inject.\n");
1939 sprintf(pkt_dev->result,
1940 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1941 return;
1944 /* Default to the interface's mac if not explicitly set. */
1946 if (is_zero_ether_addr(pkt_dev->src_mac))
1947 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1949 /* Set up Dest MAC */
1950 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1952 /* Set up pkt size */
1953 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1955 if (pkt_dev->flags & F_IPV6) {
1957 * Skip this automatic address setting until locks or functions
1958 * gets exported
1961 #ifdef NOTNOW
1962 int i, set = 0, err = 1;
1963 struct inet6_dev *idev;
1965 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1966 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1967 set = 1;
1968 break;
1971 if (!set) {
1974 * Use linklevel address if unconfigured.
1976 * use ipv6_get_lladdr if/when it's get exported
1979 rcu_read_lock();
1980 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1981 struct inet6_ifaddr *ifp;
1983 read_lock_bh(&idev->lock);
1984 for (ifp = idev->addr_list; ifp;
1985 ifp = ifp->if_next) {
1986 if (ifp->scope == IFA_LINK
1987 && !(ifp->
1988 flags & IFA_F_TENTATIVE)) {
1989 ipv6_addr_copy(&pkt_dev->
1990 cur_in6_saddr,
1991 &ifp->addr);
1992 err = 0;
1993 break;
1996 read_unlock_bh(&idev->lock);
1998 rcu_read_unlock();
1999 if (err)
2000 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2001 "address not availble.\n");
2003 #endif
2004 } else {
2005 pkt_dev->saddr_min = 0;
2006 pkt_dev->saddr_max = 0;
2007 if (strlen(pkt_dev->src_min) == 0) {
2009 struct in_device *in_dev;
2011 rcu_read_lock();
2012 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2013 if (in_dev) {
2014 if (in_dev->ifa_list) {
2015 pkt_dev->saddr_min =
2016 in_dev->ifa_list->ifa_address;
2017 pkt_dev->saddr_max = pkt_dev->saddr_min;
2020 rcu_read_unlock();
2021 } else {
2022 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2023 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2026 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2027 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2029 /* Initialize current values. */
2030 pkt_dev->cur_dst_mac_offset = 0;
2031 pkt_dev->cur_src_mac_offset = 0;
2032 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2033 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2034 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2035 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2036 pkt_dev->nflows = 0;
2039 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2041 __u64 start;
2042 __u64 now;
2044 start = now = getCurUs();
2045 while (now < spin_until_us) {
2046 /* TODO: optimize sleeping behavior */
2047 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2048 schedule_timeout_interruptible(1);
2049 else if (spin_until_us - now > 100) {
2050 if (!pkt_dev->running)
2051 return;
2052 if (need_resched())
2053 schedule();
2056 now = getCurUs();
2059 pkt_dev->idle_acc += now - start;
2062 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2064 pkt_dev->pkt_overhead = 0;
2065 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2066 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2067 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2070 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2073 if (pkt_dev->flows[flow].flags & F_INIT)
2074 return 1;
2075 else
2076 return 0;
2079 static inline int f_pick(struct pktgen_dev *pkt_dev)
2081 int flow = pkt_dev->curfl;
2083 if (pkt_dev->flags & F_FLOW_SEQ) {
2084 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2085 /* reset time */
2086 pkt_dev->flows[flow].count = 0;
2087 pkt_dev->curfl += 1;
2088 if (pkt_dev->curfl >= pkt_dev->cflows)
2089 pkt_dev->curfl = 0; /*reset */
2091 } else {
2092 flow = random32() % pkt_dev->cflows;
2094 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2095 pkt_dev->flows[flow].count = 0;
2098 return pkt_dev->curfl;
2102 #ifdef CONFIG_XFRM
2103 /* If there was already an IPSEC SA, we keep it as is, else
2104 * we go look for it ...
2106 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2108 struct xfrm_state *x = pkt_dev->flows[flow].x;
2109 if (!x) {
2110 /*slow path: we dont already have xfrm_state*/
2111 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2112 (xfrm_address_t *)&pkt_dev->cur_saddr,
2113 AF_INET,
2114 pkt_dev->ipsmode,
2115 pkt_dev->ipsproto, 0);
2116 if (x) {
2117 pkt_dev->flows[flow].x = x;
2118 set_pkt_overhead(pkt_dev);
2119 pkt_dev->pkt_overhead+=x->props.header_len;
2124 #endif
2125 /* Increment/randomize headers according to flags and current values
2126 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2128 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2130 __u32 imn;
2131 __u32 imx;
2132 int flow = 0;
2134 if (pkt_dev->cflows)
2135 flow = f_pick(pkt_dev);
2137 /* Deal with source MAC */
2138 if (pkt_dev->src_mac_count > 1) {
2139 __u32 mc;
2140 __u32 tmp;
2142 if (pkt_dev->flags & F_MACSRC_RND)
2143 mc = random32() % pkt_dev->src_mac_count;
2144 else {
2145 mc = pkt_dev->cur_src_mac_offset++;
2146 if (pkt_dev->cur_src_mac_offset >
2147 pkt_dev->src_mac_count)
2148 pkt_dev->cur_src_mac_offset = 0;
2151 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2152 pkt_dev->hh[11] = tmp;
2153 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2154 pkt_dev->hh[10] = tmp;
2155 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2156 pkt_dev->hh[9] = tmp;
2157 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2158 pkt_dev->hh[8] = tmp;
2159 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2160 pkt_dev->hh[7] = tmp;
2163 /* Deal with Destination MAC */
2164 if (pkt_dev->dst_mac_count > 1) {
2165 __u32 mc;
2166 __u32 tmp;
2168 if (pkt_dev->flags & F_MACDST_RND)
2169 mc = random32() % pkt_dev->dst_mac_count;
2171 else {
2172 mc = pkt_dev->cur_dst_mac_offset++;
2173 if (pkt_dev->cur_dst_mac_offset >
2174 pkt_dev->dst_mac_count) {
2175 pkt_dev->cur_dst_mac_offset = 0;
2179 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2180 pkt_dev->hh[5] = tmp;
2181 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2182 pkt_dev->hh[4] = tmp;
2183 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2184 pkt_dev->hh[3] = tmp;
2185 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2186 pkt_dev->hh[2] = tmp;
2187 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2188 pkt_dev->hh[1] = tmp;
2191 if (pkt_dev->flags & F_MPLS_RND) {
2192 unsigned i;
2193 for (i = 0; i < pkt_dev->nr_labels; i++)
2194 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2195 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2196 ((__force __be32)random32() &
2197 htonl(0x000fffff));
2200 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2201 pkt_dev->vlan_id = random32() & (4096-1);
2204 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2205 pkt_dev->svlan_id = random32() & (4096 - 1);
2208 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2209 if (pkt_dev->flags & F_UDPSRC_RND)
2210 pkt_dev->cur_udp_src = random32() %
2211 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2212 + pkt_dev->udp_src_min;
2214 else {
2215 pkt_dev->cur_udp_src++;
2216 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2217 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2221 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2222 if (pkt_dev->flags & F_UDPDST_RND) {
2223 pkt_dev->cur_udp_dst = random32() %
2224 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2225 + pkt_dev->udp_dst_min;
2226 } else {
2227 pkt_dev->cur_udp_dst++;
2228 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2229 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2233 if (!(pkt_dev->flags & F_IPV6)) {
2235 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2236 ntohl(pkt_dev->
2237 saddr_max))) {
2238 __u32 t;
2239 if (pkt_dev->flags & F_IPSRC_RND)
2240 t = random32() % (imx - imn) + imn;
2241 else {
2242 t = ntohl(pkt_dev->cur_saddr);
2243 t++;
2244 if (t > imx) {
2245 t = imn;
2248 pkt_dev->cur_saddr = htonl(t);
2251 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2252 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2253 } else {
2254 imn = ntohl(pkt_dev->daddr_min);
2255 imx = ntohl(pkt_dev->daddr_max);
2256 if (imn < imx) {
2257 __u32 t;
2258 __be32 s;
2259 if (pkt_dev->flags & F_IPDST_RND) {
2261 t = random32() % (imx - imn) + imn;
2262 s = htonl(t);
2264 while (ipv4_is_loopback(s) ||
2265 ipv4_is_multicast(s) ||
2266 ipv4_is_lbcast(s) ||
2267 ipv4_is_zeronet(s) ||
2268 ipv4_is_local_multicast(s)) {
2269 t = random32() % (imx - imn) + imn;
2270 s = htonl(t);
2272 pkt_dev->cur_daddr = s;
2273 } else {
2274 t = ntohl(pkt_dev->cur_daddr);
2275 t++;
2276 if (t > imx) {
2277 t = imn;
2279 pkt_dev->cur_daddr = htonl(t);
2282 if (pkt_dev->cflows) {
2283 pkt_dev->flows[flow].flags |= F_INIT;
2284 pkt_dev->flows[flow].cur_daddr =
2285 pkt_dev->cur_daddr;
2286 #ifdef CONFIG_XFRM
2287 if (pkt_dev->flags & F_IPSEC_ON)
2288 get_ipsec_sa(pkt_dev, flow);
2289 #endif
2290 pkt_dev->nflows++;
2293 } else { /* IPV6 * */
2295 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2296 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2297 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2298 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2299 else {
2300 int i;
2302 /* Only random destinations yet */
2304 for (i = 0; i < 4; i++) {
2305 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2306 (((__force __be32)random32() |
2307 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2308 pkt_dev->max_in6_daddr.s6_addr32[i]);
2313 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2314 __u32 t;
2315 if (pkt_dev->flags & F_TXSIZE_RND) {
2316 t = random32() %
2317 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2318 + pkt_dev->min_pkt_size;
2319 } else {
2320 t = pkt_dev->cur_pkt_size + 1;
2321 if (t > pkt_dev->max_pkt_size)
2322 t = pkt_dev->min_pkt_size;
2324 pkt_dev->cur_pkt_size = t;
2327 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2328 __u16 t;
2329 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2330 t = random32() %
2331 (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
2332 + pkt_dev->queue_map_min;
2333 } else {
2334 t = pkt_dev->cur_queue_map + 1;
2335 if (t > pkt_dev->queue_map_max)
2336 t = pkt_dev->queue_map_min;
2338 pkt_dev->cur_queue_map = t;
2341 pkt_dev->flows[flow].count++;
2345 #ifdef CONFIG_XFRM
2346 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2348 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2349 int err = 0;
2350 struct iphdr *iph;
2352 if (!x)
2353 return 0;
2354 /* XXX: we dont support tunnel mode for now until
2355 * we resolve the dst issue */
2356 if (x->props.mode != XFRM_MODE_TRANSPORT)
2357 return 0;
2359 spin_lock(&x->lock);
2360 iph = ip_hdr(skb);
2362 err = x->outer_mode->output(x, skb);
2363 if (err)
2364 goto error;
2365 err = x->type->output(x, skb);
2366 if (err)
2367 goto error;
2369 x->curlft.bytes +=skb->len;
2370 x->curlft.packets++;
2371 error:
2372 spin_unlock(&x->lock);
2373 return err;
2376 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2378 if (pkt_dev->cflows) {
2379 /* let go of the SAs if we have them */
2380 int i = 0;
2381 for (; i < pkt_dev->nflows; i++){
2382 struct xfrm_state *x = pkt_dev->flows[i].x;
2383 if (x) {
2384 xfrm_state_put(x);
2385 pkt_dev->flows[i].x = NULL;
2391 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2392 struct sk_buff *skb, __be16 protocol)
2394 if (pkt_dev->flags & F_IPSEC_ON) {
2395 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2396 int nhead = 0;
2397 if (x) {
2398 int ret;
2399 __u8 *eth;
2400 nhead = x->props.header_len - skb_headroom(skb);
2401 if (nhead >0) {
2402 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2403 if (ret < 0) {
2404 printk(KERN_ERR "Error expanding "
2405 "ipsec packet %d\n",ret);
2406 return 0;
2410 /* ipsec is not expecting ll header */
2411 skb_pull(skb, ETH_HLEN);
2412 ret = pktgen_output_ipsec(skb, pkt_dev);
2413 if (ret) {
2414 printk(KERN_ERR "Error creating ipsec "
2415 "packet %d\n",ret);
2416 kfree_skb(skb);
2417 return 0;
2419 /* restore ll */
2420 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2421 memcpy(eth, pkt_dev->hh, 12);
2422 *(u16 *) & eth[12] = protocol;
2425 return 1;
2427 #endif
2429 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2431 unsigned i;
2432 for (i = 0; i < pkt_dev->nr_labels; i++) {
2433 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2435 mpls--;
2436 *mpls |= MPLS_STACK_BOTTOM;
2439 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2440 unsigned int prio)
2442 return htons(id | (cfi << 12) | (prio << 13));
2445 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2446 struct pktgen_dev *pkt_dev)
2448 struct sk_buff *skb = NULL;
2449 __u8 *eth;
2450 struct udphdr *udph;
2451 int datalen, iplen;
2452 struct iphdr *iph;
2453 struct pktgen_hdr *pgh = NULL;
2454 __be16 protocol = htons(ETH_P_IP);
2455 __be32 *mpls;
2456 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2457 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2458 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2459 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2462 if (pkt_dev->nr_labels)
2463 protocol = htons(ETH_P_MPLS_UC);
2465 if (pkt_dev->vlan_id != 0xffff)
2466 protocol = htons(ETH_P_8021Q);
2468 /* Update any of the values, used when we're incrementing various
2469 * fields.
2471 mod_cur_headers(pkt_dev);
2473 datalen = (odev->hard_header_len + 16) & ~0xf;
2474 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2475 pkt_dev->pkt_overhead, GFP_ATOMIC);
2476 if (!skb) {
2477 sprintf(pkt_dev->result, "No memory");
2478 return NULL;
2481 skb_reserve(skb, datalen);
2483 /* Reserve for ethernet and IP header */
2484 eth = (__u8 *) skb_push(skb, 14);
2485 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2486 if (pkt_dev->nr_labels)
2487 mpls_push(mpls, pkt_dev);
2489 if (pkt_dev->vlan_id != 0xffff) {
2490 if (pkt_dev->svlan_id != 0xffff) {
2491 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2492 *svlan_tci = build_tci(pkt_dev->svlan_id,
2493 pkt_dev->svlan_cfi,
2494 pkt_dev->svlan_p);
2495 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2496 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2498 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2499 *vlan_tci = build_tci(pkt_dev->vlan_id,
2500 pkt_dev->vlan_cfi,
2501 pkt_dev->vlan_p);
2502 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2503 *vlan_encapsulated_proto = htons(ETH_P_IP);
2506 skb->network_header = skb->tail;
2507 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2508 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2509 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
2510 iph = ip_hdr(skb);
2511 udph = udp_hdr(skb);
2513 memcpy(eth, pkt_dev->hh, 12);
2514 *(__be16 *) & eth[12] = protocol;
2516 /* Eth + IPh + UDPh + mpls */
2517 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2518 pkt_dev->pkt_overhead;
2519 if (datalen < sizeof(struct pktgen_hdr))
2520 datalen = sizeof(struct pktgen_hdr);
2522 udph->source = htons(pkt_dev->cur_udp_src);
2523 udph->dest = htons(pkt_dev->cur_udp_dst);
2524 udph->len = htons(datalen + 8); /* DATA + udphdr */
2525 udph->check = 0; /* No checksum */
2527 iph->ihl = 5;
2528 iph->version = 4;
2529 iph->ttl = 32;
2530 iph->tos = pkt_dev->tos;
2531 iph->protocol = IPPROTO_UDP; /* UDP */
2532 iph->saddr = pkt_dev->cur_saddr;
2533 iph->daddr = pkt_dev->cur_daddr;
2534 iph->frag_off = 0;
2535 iplen = 20 + 8 + datalen;
2536 iph->tot_len = htons(iplen);
2537 iph->check = 0;
2538 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2539 skb->protocol = protocol;
2540 skb->mac_header = (skb->network_header - ETH_HLEN -
2541 pkt_dev->pkt_overhead);
2542 skb->dev = odev;
2543 skb->pkt_type = PACKET_HOST;
2545 if (pkt_dev->nfrags <= 0)
2546 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2547 else {
2548 int frags = pkt_dev->nfrags;
2549 int i;
2551 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2553 if (frags > MAX_SKB_FRAGS)
2554 frags = MAX_SKB_FRAGS;
2555 if (datalen > frags * PAGE_SIZE) {
2556 skb_put(skb, datalen - frags * PAGE_SIZE);
2557 datalen = frags * PAGE_SIZE;
2560 i = 0;
2561 while (datalen > 0) {
2562 struct page *page = alloc_pages(GFP_KERNEL, 0);
2563 skb_shinfo(skb)->frags[i].page = page;
2564 skb_shinfo(skb)->frags[i].page_offset = 0;
2565 skb_shinfo(skb)->frags[i].size =
2566 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2567 datalen -= skb_shinfo(skb)->frags[i].size;
2568 skb->len += skb_shinfo(skb)->frags[i].size;
2569 skb->data_len += skb_shinfo(skb)->frags[i].size;
2570 i++;
2571 skb_shinfo(skb)->nr_frags = i;
2574 while (i < frags) {
2575 int rem;
2577 if (i == 0)
2578 break;
2580 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2581 if (rem == 0)
2582 break;
2584 skb_shinfo(skb)->frags[i - 1].size -= rem;
2586 skb_shinfo(skb)->frags[i] =
2587 skb_shinfo(skb)->frags[i - 1];
2588 get_page(skb_shinfo(skb)->frags[i].page);
2589 skb_shinfo(skb)->frags[i].page =
2590 skb_shinfo(skb)->frags[i - 1].page;
2591 skb_shinfo(skb)->frags[i].page_offset +=
2592 skb_shinfo(skb)->frags[i - 1].size;
2593 skb_shinfo(skb)->frags[i].size = rem;
2594 i++;
2595 skb_shinfo(skb)->nr_frags = i;
2599 /* Stamp the time, and sequence number, convert them to network byte order */
2601 if (pgh) {
2602 struct timeval timestamp;
2604 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2605 pgh->seq_num = htonl(pkt_dev->seq_num);
2607 do_gettimeofday(&timestamp);
2608 pgh->tv_sec = htonl(timestamp.tv_sec);
2609 pgh->tv_usec = htonl(timestamp.tv_usec);
2612 #ifdef CONFIG_XFRM
2613 if (!process_ipsec(pkt_dev, skb, protocol))
2614 return NULL;
2615 #endif
2617 return skb;
2621 * scan_ip6, fmt_ip taken from dietlibc-0.21
2622 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2624 * Slightly modified for kernel.
2625 * Should be candidate for net/ipv4/utils.c
2626 * --ro
2629 static unsigned int scan_ip6(const char *s, char ip[16])
2631 unsigned int i;
2632 unsigned int len = 0;
2633 unsigned long u;
2634 char suffix[16];
2635 unsigned int prefixlen = 0;
2636 unsigned int suffixlen = 0;
2637 __be32 tmp;
2638 char *pos;
2640 for (i = 0; i < 16; i++)
2641 ip[i] = 0;
2643 for (;;) {
2644 if (*s == ':') {
2645 len++;
2646 if (s[1] == ':') { /* Found "::", skip to part 2 */
2647 s += 2;
2648 len++;
2649 break;
2651 s++;
2654 u = simple_strtoul(s, &pos, 16);
2655 i = pos - s;
2656 if (!i)
2657 return 0;
2658 if (prefixlen == 12 && s[i] == '.') {
2660 /* the last 4 bytes may be written as IPv4 address */
2662 tmp = in_aton(s);
2663 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2664 return i + len;
2666 ip[prefixlen++] = (u >> 8);
2667 ip[prefixlen++] = (u & 255);
2668 s += i;
2669 len += i;
2670 if (prefixlen == 16)
2671 return len;
2674 /* part 2, after "::" */
2675 for (;;) {
2676 if (*s == ':') {
2677 if (suffixlen == 0)
2678 break;
2679 s++;
2680 len++;
2681 } else if (suffixlen != 0)
2682 break;
2684 u = simple_strtol(s, &pos, 16);
2685 i = pos - s;
2686 if (!i) {
2687 if (*s)
2688 len--;
2689 break;
2691 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2692 tmp = in_aton(s);
2693 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2694 sizeof(tmp));
2695 suffixlen += 4;
2696 len += strlen(s);
2697 break;
2699 suffix[suffixlen++] = (u >> 8);
2700 suffix[suffixlen++] = (u & 255);
2701 s += i;
2702 len += i;
2703 if (prefixlen + suffixlen == 16)
2704 break;
2706 for (i = 0; i < suffixlen; i++)
2707 ip[16 - suffixlen + i] = suffix[i];
2708 return len;
2711 static char tohex(char hexdigit)
2713 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2716 static int fmt_xlong(char *s, unsigned int i)
2718 char *bak = s;
2719 *s = tohex((i >> 12) & 0xf);
2720 if (s != bak || *s != '0')
2721 ++s;
2722 *s = tohex((i >> 8) & 0xf);
2723 if (s != bak || *s != '0')
2724 ++s;
2725 *s = tohex((i >> 4) & 0xf);
2726 if (s != bak || *s != '0')
2727 ++s;
2728 *s = tohex(i & 0xf);
2729 return s - bak + 1;
2732 static unsigned int fmt_ip6(char *s, const char ip[16])
2734 unsigned int len;
2735 unsigned int i;
2736 unsigned int temp;
2737 unsigned int compressing;
2738 int j;
2740 len = 0;
2741 compressing = 0;
2742 for (j = 0; j < 16; j += 2) {
2744 #ifdef V4MAPPEDPREFIX
2745 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2746 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2747 temp = strlen(s);
2748 return len + temp;
2750 #endif
2751 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2752 (unsigned long)(unsigned char)ip[j + 1];
2753 if (temp == 0) {
2754 if (!compressing) {
2755 compressing = 1;
2756 if (j == 0) {
2757 *s++ = ':';
2758 ++len;
2761 } else {
2762 if (compressing) {
2763 compressing = 0;
2764 *s++ = ':';
2765 ++len;
2767 i = fmt_xlong(s, temp);
2768 len += i;
2769 s += i;
2770 if (j < 14) {
2771 *s++ = ':';
2772 ++len;
2776 if (compressing) {
2777 *s++ = ':';
2778 ++len;
2780 *s = 0;
2781 return len;
2784 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2785 struct pktgen_dev *pkt_dev)
2787 struct sk_buff *skb = NULL;
2788 __u8 *eth;
2789 struct udphdr *udph;
2790 int datalen;
2791 struct ipv6hdr *iph;
2792 struct pktgen_hdr *pgh = NULL;
2793 __be16 protocol = htons(ETH_P_IPV6);
2794 __be32 *mpls;
2795 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2796 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2797 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2798 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2800 if (pkt_dev->nr_labels)
2801 protocol = htons(ETH_P_MPLS_UC);
2803 if (pkt_dev->vlan_id != 0xffff)
2804 protocol = htons(ETH_P_8021Q);
2806 /* Update any of the values, used when we're incrementing various
2807 * fields.
2809 mod_cur_headers(pkt_dev);
2811 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2812 pkt_dev->pkt_overhead, GFP_ATOMIC);
2813 if (!skb) {
2814 sprintf(pkt_dev->result, "No memory");
2815 return NULL;
2818 skb_reserve(skb, 16);
2820 /* Reserve for ethernet and IP header */
2821 eth = (__u8 *) skb_push(skb, 14);
2822 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2823 if (pkt_dev->nr_labels)
2824 mpls_push(mpls, pkt_dev);
2826 if (pkt_dev->vlan_id != 0xffff) {
2827 if (pkt_dev->svlan_id != 0xffff) {
2828 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2829 *svlan_tci = build_tci(pkt_dev->svlan_id,
2830 pkt_dev->svlan_cfi,
2831 pkt_dev->svlan_p);
2832 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2833 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2835 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2836 *vlan_tci = build_tci(pkt_dev->vlan_id,
2837 pkt_dev->vlan_cfi,
2838 pkt_dev->vlan_p);
2839 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2840 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2843 skb->network_header = skb->tail;
2844 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2845 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2846 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
2847 iph = ipv6_hdr(skb);
2848 udph = udp_hdr(skb);
2850 memcpy(eth, pkt_dev->hh, 12);
2851 *(__be16 *) & eth[12] = protocol;
2853 /* Eth + IPh + UDPh + mpls */
2854 datalen = pkt_dev->cur_pkt_size - 14 -
2855 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2856 pkt_dev->pkt_overhead;
2858 if (datalen < sizeof(struct pktgen_hdr)) {
2859 datalen = sizeof(struct pktgen_hdr);
2860 if (net_ratelimit())
2861 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2862 datalen);
2865 udph->source = htons(pkt_dev->cur_udp_src);
2866 udph->dest = htons(pkt_dev->cur_udp_dst);
2867 udph->len = htons(datalen + sizeof(struct udphdr));
2868 udph->check = 0; /* No checksum */
2870 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2872 if (pkt_dev->traffic_class) {
2873 /* Version + traffic class + flow (0) */
2874 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2877 iph->hop_limit = 32;
2879 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2880 iph->nexthdr = IPPROTO_UDP;
2882 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2883 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2885 skb->mac_header = (skb->network_header - ETH_HLEN -
2886 pkt_dev->pkt_overhead);
2887 skb->protocol = protocol;
2888 skb->dev = odev;
2889 skb->pkt_type = PACKET_HOST;
2891 if (pkt_dev->nfrags <= 0)
2892 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2893 else {
2894 int frags = pkt_dev->nfrags;
2895 int i;
2897 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2899 if (frags > MAX_SKB_FRAGS)
2900 frags = MAX_SKB_FRAGS;
2901 if (datalen > frags * PAGE_SIZE) {
2902 skb_put(skb, datalen - frags * PAGE_SIZE);
2903 datalen = frags * PAGE_SIZE;
2906 i = 0;
2907 while (datalen > 0) {
2908 struct page *page = alloc_pages(GFP_KERNEL, 0);
2909 skb_shinfo(skb)->frags[i].page = page;
2910 skb_shinfo(skb)->frags[i].page_offset = 0;
2911 skb_shinfo(skb)->frags[i].size =
2912 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2913 datalen -= skb_shinfo(skb)->frags[i].size;
2914 skb->len += skb_shinfo(skb)->frags[i].size;
2915 skb->data_len += skb_shinfo(skb)->frags[i].size;
2916 i++;
2917 skb_shinfo(skb)->nr_frags = i;
2920 while (i < frags) {
2921 int rem;
2923 if (i == 0)
2924 break;
2926 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2927 if (rem == 0)
2928 break;
2930 skb_shinfo(skb)->frags[i - 1].size -= rem;
2932 skb_shinfo(skb)->frags[i] =
2933 skb_shinfo(skb)->frags[i - 1];
2934 get_page(skb_shinfo(skb)->frags[i].page);
2935 skb_shinfo(skb)->frags[i].page =
2936 skb_shinfo(skb)->frags[i - 1].page;
2937 skb_shinfo(skb)->frags[i].page_offset +=
2938 skb_shinfo(skb)->frags[i - 1].size;
2939 skb_shinfo(skb)->frags[i].size = rem;
2940 i++;
2941 skb_shinfo(skb)->nr_frags = i;
2945 /* Stamp the time, and sequence number, convert them to network byte order */
2946 /* should we update cloned packets too ? */
2947 if (pgh) {
2948 struct timeval timestamp;
2950 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2951 pgh->seq_num = htonl(pkt_dev->seq_num);
2953 do_gettimeofday(&timestamp);
2954 pgh->tv_sec = htonl(timestamp.tv_sec);
2955 pgh->tv_usec = htonl(timestamp.tv_usec);
2957 /* pkt_dev->seq_num++; FF: you really mean this? */
2959 return skb;
2962 static inline struct sk_buff *fill_packet(struct net_device *odev,
2963 struct pktgen_dev *pkt_dev)
2965 if (pkt_dev->flags & F_IPV6)
2966 return fill_packet_ipv6(odev, pkt_dev);
2967 else
2968 return fill_packet_ipv4(odev, pkt_dev);
2971 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2973 pkt_dev->seq_num = 1;
2974 pkt_dev->idle_acc = 0;
2975 pkt_dev->sofar = 0;
2976 pkt_dev->tx_bytes = 0;
2977 pkt_dev->errors = 0;
2980 /* Set up structure for sending pkts, clear counters */
2982 static void pktgen_run(struct pktgen_thread *t)
2984 struct pktgen_dev *pkt_dev;
2985 int started = 0;
2987 pr_debug("pktgen: entering pktgen_run. %p\n", t);
2989 if_lock(t);
2990 list_for_each_entry(pkt_dev, &t->if_list, list) {
2993 * setup odev and create initial packet.
2995 pktgen_setup_inject(pkt_dev);
2997 if (pkt_dev->odev) {
2998 pktgen_clear_counters(pkt_dev);
2999 pkt_dev->running = 1; /* Cranke yeself! */
3000 pkt_dev->skb = NULL;
3001 pkt_dev->started_at = getCurUs();
3002 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
3003 pkt_dev->next_tx_ns = 0;
3004 set_pkt_overhead(pkt_dev);
3006 strcpy(pkt_dev->result, "Starting");
3007 started++;
3008 } else
3009 strcpy(pkt_dev->result, "Error starting");
3011 if_unlock(t);
3012 if (started)
3013 t->control &= ~(T_STOP);
3016 static void pktgen_stop_all_threads_ifs(void)
3018 struct pktgen_thread *t;
3020 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3022 mutex_lock(&pktgen_thread_lock);
3024 list_for_each_entry(t, &pktgen_threads, th_list)
3025 t->control |= T_STOP;
3027 mutex_unlock(&pktgen_thread_lock);
3030 static int thread_is_running(struct pktgen_thread *t)
3032 struct pktgen_dev *pkt_dev;
3033 int res = 0;
3035 list_for_each_entry(pkt_dev, &t->if_list, list)
3036 if (pkt_dev->running) {
3037 res = 1;
3038 break;
3040 return res;
3043 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3045 if_lock(t);
3047 while (thread_is_running(t)) {
3049 if_unlock(t);
3051 msleep_interruptible(100);
3053 if (signal_pending(current))
3054 goto signal;
3055 if_lock(t);
3057 if_unlock(t);
3058 return 1;
3059 signal:
3060 return 0;
3063 static int pktgen_wait_all_threads_run(void)
3065 struct pktgen_thread *t;
3066 int sig = 1;
3068 mutex_lock(&pktgen_thread_lock);
3070 list_for_each_entry(t, &pktgen_threads, th_list) {
3071 sig = pktgen_wait_thread_run(t);
3072 if (sig == 0)
3073 break;
3076 if (sig == 0)
3077 list_for_each_entry(t, &pktgen_threads, th_list)
3078 t->control |= (T_STOP);
3080 mutex_unlock(&pktgen_thread_lock);
3081 return sig;
3084 static void pktgen_run_all_threads(void)
3086 struct pktgen_thread *t;
3088 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3090 mutex_lock(&pktgen_thread_lock);
3092 list_for_each_entry(t, &pktgen_threads, th_list)
3093 t->control |= (T_RUN);
3095 mutex_unlock(&pktgen_thread_lock);
3097 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3099 pktgen_wait_all_threads_run();
3102 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3104 __u64 total_us, bps, mbps, pps, idle;
3105 char *p = pkt_dev->result;
3107 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3109 idle = pkt_dev->idle_acc;
3111 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3112 (unsigned long long)total_us,
3113 (unsigned long long)(total_us - idle),
3114 (unsigned long long)idle,
3115 (unsigned long long)pkt_dev->sofar,
3116 pkt_dev->cur_pkt_size, nr_frags);
3118 pps = pkt_dev->sofar * USEC_PER_SEC;
3120 while ((total_us >> 32) != 0) {
3121 pps >>= 1;
3122 total_us >>= 1;
3125 do_div(pps, total_us);
3127 bps = pps * 8 * pkt_dev->cur_pkt_size;
3129 mbps = bps;
3130 do_div(mbps, 1000000);
3131 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3132 (unsigned long long)pps,
3133 (unsigned long long)mbps,
3134 (unsigned long long)bps,
3135 (unsigned long long)pkt_dev->errors);
3138 /* Set stopped-at timer, remove from running list, do counters & statistics */
3140 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3142 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3144 if (!pkt_dev->running) {
3145 printk(KERN_WARNING "pktgen: interface: %s is already "
3146 "stopped\n", pkt_dev->odev->name);
3147 return -EINVAL;
3150 pkt_dev->stopped_at = getCurUs();
3151 pkt_dev->running = 0;
3153 show_results(pkt_dev, nr_frags);
3155 return 0;
3158 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3160 struct pktgen_dev *pkt_dev, *best = NULL;
3162 if_lock(t);
3164 list_for_each_entry(pkt_dev, &t->if_list, list) {
3165 if (!pkt_dev->running)
3166 continue;
3167 if (best == NULL)
3168 best = pkt_dev;
3169 else if (pkt_dev->next_tx_us < best->next_tx_us)
3170 best = pkt_dev;
3172 if_unlock(t);
3173 return best;
3176 static void pktgen_stop(struct pktgen_thread *t)
3178 struct pktgen_dev *pkt_dev;
3180 pr_debug("pktgen: entering pktgen_stop\n");
3182 if_lock(t);
3184 list_for_each_entry(pkt_dev, &t->if_list, list) {
3185 pktgen_stop_device(pkt_dev);
3186 if (pkt_dev->skb)
3187 kfree_skb(pkt_dev->skb);
3189 pkt_dev->skb = NULL;
3192 if_unlock(t);
3196 * one of our devices needs to be removed - find it
3197 * and remove it
3199 static void pktgen_rem_one_if(struct pktgen_thread *t)
3201 struct list_head *q, *n;
3202 struct pktgen_dev *cur;
3204 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3206 if_lock(t);
3208 list_for_each_safe(q, n, &t->if_list) {
3209 cur = list_entry(q, struct pktgen_dev, list);
3211 if (!cur->removal_mark)
3212 continue;
3214 if (cur->skb)
3215 kfree_skb(cur->skb);
3216 cur->skb = NULL;
3218 pktgen_remove_device(t, cur);
3220 break;
3223 if_unlock(t);
3226 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3228 struct list_head *q, *n;
3229 struct pktgen_dev *cur;
3231 /* Remove all devices, free mem */
3233 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3234 if_lock(t);
3236 list_for_each_safe(q, n, &t->if_list) {
3237 cur = list_entry(q, struct pktgen_dev, list);
3239 if (cur->skb)
3240 kfree_skb(cur->skb);
3241 cur->skb = NULL;
3243 pktgen_remove_device(t, cur);
3246 if_unlock(t);
3249 static void pktgen_rem_thread(struct pktgen_thread *t)
3251 /* Remove from the thread list */
3253 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3255 mutex_lock(&pktgen_thread_lock);
3257 list_del(&t->th_list);
3259 mutex_unlock(&pktgen_thread_lock);
3262 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3264 struct net_device *odev = NULL;
3265 __u64 idle_start = 0;
3266 int ret;
3268 odev = pkt_dev->odev;
3270 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3271 u64 now;
3273 now = getCurUs();
3274 if (now < pkt_dev->next_tx_us)
3275 spin(pkt_dev, pkt_dev->next_tx_us);
3277 /* This is max DELAY, this has special meaning of
3278 * "never transmit"
3280 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3281 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3282 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3283 goto out;
3287 if ((netif_queue_stopped(odev) ||
3288 (pkt_dev->skb &&
3289 netif_subqueue_stopped(odev, pkt_dev->skb))) ||
3290 need_resched()) {
3291 idle_start = getCurUs();
3293 if (!netif_running(odev)) {
3294 pktgen_stop_device(pkt_dev);
3295 if (pkt_dev->skb)
3296 kfree_skb(pkt_dev->skb);
3297 pkt_dev->skb = NULL;
3298 goto out;
3300 if (need_resched())
3301 schedule();
3303 pkt_dev->idle_acc += getCurUs() - idle_start;
3305 if (netif_queue_stopped(odev) ||
3306 netif_subqueue_stopped(odev, pkt_dev->skb)) {
3307 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3308 pkt_dev->next_tx_ns = 0;
3309 goto out; /* Try the next interface */
3313 if (pkt_dev->last_ok || !pkt_dev->skb) {
3314 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3315 || (!pkt_dev->skb)) {
3316 /* build a new pkt */
3317 if (pkt_dev->skb)
3318 kfree_skb(pkt_dev->skb);
3320 pkt_dev->skb = fill_packet(odev, pkt_dev);
3321 if (pkt_dev->skb == NULL) {
3322 printk(KERN_ERR "pktgen: ERROR: couldn't "
3323 "allocate skb in fill_packet.\n");
3324 schedule();
3325 pkt_dev->clone_count--; /* back out increment, OOM */
3326 goto out;
3328 pkt_dev->allocated_skbs++;
3329 pkt_dev->clone_count = 0; /* reset counter */
3333 netif_tx_lock_bh(odev);
3334 if (!netif_queue_stopped(odev) &&
3335 !netif_subqueue_stopped(odev, pkt_dev->skb)) {
3337 atomic_inc(&(pkt_dev->skb->users));
3338 retry_now:
3339 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3340 if (likely(ret == NETDEV_TX_OK)) {
3341 pkt_dev->last_ok = 1;
3342 pkt_dev->sofar++;
3343 pkt_dev->seq_num++;
3344 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3346 } else if (ret == NETDEV_TX_LOCKED
3347 && (odev->features & NETIF_F_LLTX)) {
3348 cpu_relax();
3349 goto retry_now;
3350 } else { /* Retry it next time */
3352 atomic_dec(&(pkt_dev->skb->users));
3354 if (debug && net_ratelimit())
3355 printk(KERN_INFO "pktgen: Hard xmit error\n");
3357 pkt_dev->errors++;
3358 pkt_dev->last_ok = 0;
3361 pkt_dev->next_tx_us = getCurUs();
3362 pkt_dev->next_tx_ns = 0;
3364 pkt_dev->next_tx_us += pkt_dev->delay_us;
3365 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3367 if (pkt_dev->next_tx_ns > 1000) {
3368 pkt_dev->next_tx_us++;
3369 pkt_dev->next_tx_ns -= 1000;
3373 else { /* Retry it next time */
3374 pkt_dev->last_ok = 0;
3375 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3376 pkt_dev->next_tx_ns = 0;
3379 netif_tx_unlock_bh(odev);
3381 /* If pkt_dev->count is zero, then run forever */
3382 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3383 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3384 idle_start = getCurUs();
3385 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3386 if (signal_pending(current)) {
3387 break;
3389 schedule();
3391 pkt_dev->idle_acc += getCurUs() - idle_start;
3394 /* Done with this */
3395 pktgen_stop_device(pkt_dev);
3396 if (pkt_dev->skb)
3397 kfree_skb(pkt_dev->skb);
3398 pkt_dev->skb = NULL;
3400 out:;
3404 * Main loop of the thread goes here
3407 static int pktgen_thread_worker(void *arg)
3409 DEFINE_WAIT(wait);
3410 struct pktgen_thread *t = arg;
3411 struct pktgen_dev *pkt_dev = NULL;
3412 int cpu = t->cpu;
3414 BUG_ON(smp_processor_id() != cpu);
3416 init_waitqueue_head(&t->queue);
3418 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3420 set_current_state(TASK_INTERRUPTIBLE);
3422 set_freezable();
3424 while (!kthread_should_stop()) {
3425 pkt_dev = next_to_run(t);
3427 if (!pkt_dev &&
3428 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3429 == 0) {
3430 prepare_to_wait(&(t->queue), &wait,
3431 TASK_INTERRUPTIBLE);
3432 schedule_timeout(HZ / 10);
3433 finish_wait(&(t->queue), &wait);
3436 __set_current_state(TASK_RUNNING);
3438 if (pkt_dev)
3439 pktgen_xmit(pkt_dev);
3441 if (t->control & T_STOP) {
3442 pktgen_stop(t);
3443 t->control &= ~(T_STOP);
3446 if (t->control & T_RUN) {
3447 pktgen_run(t);
3448 t->control &= ~(T_RUN);
3451 if (t->control & T_REMDEVALL) {
3452 pktgen_rem_all_ifs(t);
3453 t->control &= ~(T_REMDEVALL);
3456 if (t->control & T_REMDEV) {
3457 pktgen_rem_one_if(t);
3458 t->control &= ~(T_REMDEV);
3461 try_to_freeze();
3463 set_current_state(TASK_INTERRUPTIBLE);
3466 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3467 pktgen_stop(t);
3469 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3470 pktgen_rem_all_ifs(t);
3472 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3473 pktgen_rem_thread(t);
3475 return 0;
3478 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3479 const char *ifname)
3481 struct pktgen_dev *p, *pkt_dev = NULL;
3482 if_lock(t);
3484 list_for_each_entry(p, &t->if_list, list)
3485 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3486 pkt_dev = p;
3487 break;
3490 if_unlock(t);
3491 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3492 return pkt_dev;
3496 * Adds a dev at front of if_list.
3499 static int add_dev_to_thread(struct pktgen_thread *t,
3500 struct pktgen_dev *pkt_dev)
3502 int rv = 0;
3504 if_lock(t);
3506 if (pkt_dev->pg_thread) {
3507 printk(KERN_ERR "pktgen: ERROR: already assigned "
3508 "to a thread.\n");
3509 rv = -EBUSY;
3510 goto out;
3513 list_add(&pkt_dev->list, &t->if_list);
3514 pkt_dev->pg_thread = t;
3515 pkt_dev->running = 0;
3517 out:
3518 if_unlock(t);
3519 return rv;
3522 /* Called under thread lock */
3524 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3526 struct pktgen_dev *pkt_dev;
3527 int err;
3529 /* We don't allow a device to be on several threads */
3531 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3532 if (pkt_dev) {
3533 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3534 return -EBUSY;
3537 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3538 if (!pkt_dev)
3539 return -ENOMEM;
3541 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3542 if (pkt_dev->flows == NULL) {
3543 kfree(pkt_dev);
3544 return -ENOMEM;
3546 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3548 pkt_dev->removal_mark = 0;
3549 pkt_dev->min_pkt_size = ETH_ZLEN;
3550 pkt_dev->max_pkt_size = ETH_ZLEN;
3551 pkt_dev->nfrags = 0;
3552 pkt_dev->clone_skb = pg_clone_skb_d;
3553 pkt_dev->delay_us = pg_delay_d / 1000;
3554 pkt_dev->delay_ns = pg_delay_d % 1000;
3555 pkt_dev->count = pg_count_d;
3556 pkt_dev->sofar = 0;
3557 pkt_dev->udp_src_min = 9; /* sink port */
3558 pkt_dev->udp_src_max = 9;
3559 pkt_dev->udp_dst_min = 9;
3560 pkt_dev->udp_dst_max = 9;
3562 pkt_dev->vlan_p = 0;
3563 pkt_dev->vlan_cfi = 0;
3564 pkt_dev->vlan_id = 0xffff;
3565 pkt_dev->svlan_p = 0;
3566 pkt_dev->svlan_cfi = 0;
3567 pkt_dev->svlan_id = 0xffff;
3569 err = pktgen_setup_dev(pkt_dev, ifname);
3570 if (err)
3571 goto out1;
3573 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3574 &pktgen_if_fops, pkt_dev);
3575 if (!pkt_dev->entry) {
3576 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3577 PG_PROC_DIR, ifname);
3578 err = -EINVAL;
3579 goto out2;
3581 #ifdef CONFIG_XFRM
3582 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3583 pkt_dev->ipsproto = IPPROTO_ESP;
3584 #endif
3586 return add_dev_to_thread(t, pkt_dev);
3587 out2:
3588 dev_put(pkt_dev->odev);
3589 out1:
3590 #ifdef CONFIG_XFRM
3591 free_SAs(pkt_dev);
3592 #endif
3593 if (pkt_dev->flows)
3594 vfree(pkt_dev->flows);
3595 kfree(pkt_dev);
3596 return err;
3599 static int __init pktgen_create_thread(int cpu)
3601 struct pktgen_thread *t;
3602 struct proc_dir_entry *pe;
3603 struct task_struct *p;
3605 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3606 if (!t) {
3607 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3608 "create new thread.\n");
3609 return -ENOMEM;
3612 spin_lock_init(&t->if_lock);
3613 t->cpu = cpu;
3615 INIT_LIST_HEAD(&t->if_list);
3617 list_add_tail(&t->th_list, &pktgen_threads);
3619 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3620 if (IS_ERR(p)) {
3621 printk(KERN_ERR "pktgen: kernel_thread() failed "
3622 "for cpu %d\n", t->cpu);
3623 list_del(&t->th_list);
3624 kfree(t);
3625 return PTR_ERR(p);
3627 kthread_bind(p, cpu);
3628 t->tsk = p;
3630 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3631 &pktgen_thread_fops, t);
3632 if (!pe) {
3633 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3634 PG_PROC_DIR, t->tsk->comm);
3635 kthread_stop(p);
3636 list_del(&t->th_list);
3637 kfree(t);
3638 return -EINVAL;
3641 wake_up_process(p);
3643 return 0;
3647 * Removes a device from the thread if_list.
3649 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3650 struct pktgen_dev *pkt_dev)
3652 struct list_head *q, *n;
3653 struct pktgen_dev *p;
3655 list_for_each_safe(q, n, &t->if_list) {
3656 p = list_entry(q, struct pktgen_dev, list);
3657 if (p == pkt_dev)
3658 list_del(&p->list);
3662 static int pktgen_remove_device(struct pktgen_thread *t,
3663 struct pktgen_dev *pkt_dev)
3666 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3668 if (pkt_dev->running) {
3669 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3670 "running interface, stopping it now.\n");
3671 pktgen_stop_device(pkt_dev);
3674 /* Dis-associate from the interface */
3676 if (pkt_dev->odev) {
3677 dev_put(pkt_dev->odev);
3678 pkt_dev->odev = NULL;
3681 /* And update the thread if_list */
3683 _rem_dev_from_if_list(t, pkt_dev);
3685 if (pkt_dev->entry)
3686 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3688 #ifdef CONFIG_XFRM
3689 free_SAs(pkt_dev);
3690 #endif
3691 if (pkt_dev->flows)
3692 vfree(pkt_dev->flows);
3693 kfree(pkt_dev);
3694 return 0;
3697 static int __init pg_init(void)
3699 int cpu;
3700 struct proc_dir_entry *pe;
3702 printk(KERN_INFO "%s", version);
3704 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3705 if (!pg_proc_dir)
3706 return -ENODEV;
3707 pg_proc_dir->owner = THIS_MODULE;
3709 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3710 if (pe == NULL) {
3711 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3712 "procfs entry.\n", PGCTRL);
3713 proc_net_remove(&init_net, PG_PROC_DIR);
3714 return -EINVAL;
3717 /* Register us to receive netdevice events */
3718 register_netdevice_notifier(&pktgen_notifier_block);
3720 for_each_online_cpu(cpu) {
3721 int err;
3723 err = pktgen_create_thread(cpu);
3724 if (err)
3725 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3726 "thread for cpu %d (%d)\n", cpu, err);
3729 if (list_empty(&pktgen_threads)) {
3730 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3731 "all threads\n");
3732 unregister_netdevice_notifier(&pktgen_notifier_block);
3733 remove_proc_entry(PGCTRL, pg_proc_dir);
3734 proc_net_remove(&init_net, PG_PROC_DIR);
3735 return -ENODEV;
3738 return 0;
3741 static void __exit pg_cleanup(void)
3743 struct pktgen_thread *t;
3744 struct list_head *q, *n;
3745 wait_queue_head_t queue;
3746 init_waitqueue_head(&queue);
3748 /* Stop all interfaces & threads */
3750 list_for_each_safe(q, n, &pktgen_threads) {
3751 t = list_entry(q, struct pktgen_thread, th_list);
3752 kthread_stop(t->tsk);
3753 kfree(t);
3756 /* Un-register us from receiving netdevice events */
3757 unregister_netdevice_notifier(&pktgen_notifier_block);
3759 /* Clean up proc file system */
3760 remove_proc_entry(PGCTRL, pg_proc_dir);
3761 proc_net_remove(&init_net, PG_PROC_DIR);
3764 module_init(pg_init);
3765 module_exit(pg_cleanup);
3767 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3768 MODULE_DESCRIPTION("Packet Generator tool");
3769 MODULE_LICENSE("GPL");
3770 module_param(pg_count_d, int, 0);
3771 module_param(pg_delay_d, int, 0);
3772 module_param(pg_clone_skb_d, int, 0);
3773 module_param(debug, int, 0);