Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[deliverable/linux.git] / net / core / pktgen.c
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
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
10 *
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.
15 *
16 *
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.
22 *
23 * Additional hacking by:
24 *
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>
46 *
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.
53 *
54 * Also moved to /proc/net/pktgen/
55 * --ro
56 *
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>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
63 *
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
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.
74 *
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.
79 *
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
83 *
84 * Fix refcount off by one if first packet fails, potential null deref,
85 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
92 *
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
98 *
99 * Randy Dunlap fixed u64 printk compiler waring
100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
108 * 050103
109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
114 */
115 #include <linux/sys.h>
116 #include <linux/types.h>
117 #include <linux/module.h>
118 #include <linux/moduleparam.h>
119 #include <linux/kernel.h>
120 #include <linux/mutex.h>
121 #include <linux/sched.h>
122 #include <linux/slab.h>
123 #include <linux/vmalloc.h>
124 #include <linux/unistd.h>
125 #include <linux/string.h>
126 #include <linux/ptrace.h>
127 #include <linux/errno.h>
128 #include <linux/ioport.h>
129 #include <linux/interrupt.h>
130 #include <linux/capability.h>
131 #include <linux/freezer.h>
132 #include <linux/delay.h>
133 #include <linux/timer.h>
134 #include <linux/list.h>
135 #include <linux/init.h>
136 #include <linux/skbuff.h>
137 #include <linux/netdevice.h>
138 #include <linux/inet.h>
139 #include <linux/inetdevice.h>
140 #include <linux/rtnetlink.h>
141 #include <linux/if_arp.h>
142 #include <linux/if_vlan.h>
143 #include <linux/in.h>
144 #include <linux/ip.h>
145 #include <linux/ipv6.h>
146 #include <linux/udp.h>
147 #include <linux/proc_fs.h>
148 #include <linux/seq_file.h>
149 #include <linux/wait.h>
150 #include <linux/etherdevice.h>
151 #include <linux/kthread.h>
152 #include <net/checksum.h>
153 #include <net/ipv6.h>
154 #include <net/addrconf.h>
155 #ifdef CONFIG_XFRM
156 #include <net/xfrm.h>
157 #endif
158 #include <asm/byteorder.h>
159 #include <linux/rcupdate.h>
160 #include <asm/bitops.h>
161 #include <asm/io.h>
162 #include <asm/dma.h>
163 #include <asm/uaccess.h>
164 #include <asm/div64.h> /* do_div */
165 #include <asm/timex.h>
166
167 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
168
169 /* The buckets are exponential in 'width' */
170 #define LAT_BUCKETS_MAX 32
171 #define IP_NAME_SZ 32
172 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
173 #define MPLS_STACK_BOTTOM htonl(0x00000100)
174
175 /* Device flag bits */
176 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
177 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
178 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
179 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
180 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
181 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
182 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
183 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
184 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
185 #define F_VID_RND (1<<9) /* Random VLAN ID */
186 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
187 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
188 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
189
190 /* Thread control flag bits */
191 #define T_TERMINATE (1<<0)
192 #define T_STOP (1<<1) /* Stop run */
193 #define T_RUN (1<<2) /* Start run */
194 #define T_REMDEVALL (1<<3) /* Remove all devs */
195 #define T_REMDEV (1<<4) /* Remove one dev */
196
197 /* If lock -- can be removed after some work */
198 #define if_lock(t) spin_lock(&(t->if_lock));
199 #define if_unlock(t) spin_unlock(&(t->if_lock));
200
201 /* Used to help with determining the pkts on receive */
202 #define PKTGEN_MAGIC 0xbe9be955
203 #define PG_PROC_DIR "pktgen"
204 #define PGCTRL "pgctrl"
205 static struct proc_dir_entry *pg_proc_dir = NULL;
206
207 #define MAX_CFLOWS 65536
208
209 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
210 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
211
212 struct flow_state {
213 __be32 cur_daddr;
214 int count;
215 #ifdef CONFIG_XFRM
216 struct xfrm_state *x;
217 #endif
218 __u32 flags;
219 };
220
221 /* flow flag bits */
222 #define F_INIT (1<<0) /* flow has been initialized */
223
224 struct pktgen_dev {
225 /*
226 * Try to keep frequent/infrequent used vars. separated.
227 */
228 struct proc_dir_entry *entry; /* proc file */
229 struct pktgen_thread *pg_thread;/* the owner */
230 struct list_head list; /* Used for chaining in the thread's run-queue */
231
232 int running; /* if this changes to false, the test will stop */
233
234 /* If min != max, then we will either do a linear iteration, or
235 * we will do a random selection from within the range.
236 */
237 __u32 flags;
238 int removal_mark; /* non-zero => the device is marked for
239 * removal by worker thread */
240
241 int min_pkt_size; /* = ETH_ZLEN; */
242 int max_pkt_size; /* = ETH_ZLEN; */
243 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
244 int nfrags;
245 __u32 delay_us; /* Default delay */
246 __u32 delay_ns;
247 __u64 count; /* Default No packets to send */
248 __u64 sofar; /* How many pkts we've sent so far */
249 __u64 tx_bytes; /* How many bytes we've transmitted */
250 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
251
252 /* runtime counters relating to clone_skb */
253 __u64 next_tx_us; /* timestamp of when to tx next */
254 __u32 next_tx_ns;
255
256 __u64 allocated_skbs;
257 __u32 clone_count;
258 int last_ok; /* Was last skb sent?
259 * Or a failed transmit of some sort? This will keep
260 * sequence numbers in order, for example.
261 */
262 __u64 started_at; /* micro-seconds */
263 __u64 stopped_at; /* micro-seconds */
264 __u64 idle_acc; /* micro-seconds */
265 __u32 seq_num;
266
267 int clone_skb; /* Use multiple SKBs during packet gen. If this number
268 * is greater than 1, then that many copies of the same
269 * packet will be sent before a new packet is allocated.
270 * For instance, if you want to send 1024 identical packets
271 * before creating a new packet, set clone_skb to 1024.
272 */
273
274 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
275 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
276 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
277 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278
279 struct in6_addr in6_saddr;
280 struct in6_addr in6_daddr;
281 struct in6_addr cur_in6_daddr;
282 struct in6_addr cur_in6_saddr;
283 /* For ranges */
284 struct in6_addr min_in6_daddr;
285 struct in6_addr max_in6_daddr;
286 struct in6_addr min_in6_saddr;
287 struct in6_addr max_in6_saddr;
288
289 /* If we're doing ranges, random or incremental, then this
290 * defines the min/max for those ranges.
291 */
292 __be32 saddr_min; /* inclusive, source IP address */
293 __be32 saddr_max; /* exclusive, source IP address */
294 __be32 daddr_min; /* inclusive, dest IP address */
295 __be32 daddr_max; /* exclusive, dest IP address */
296
297 __u16 udp_src_min; /* inclusive, source UDP port */
298 __u16 udp_src_max; /* exclusive, source UDP port */
299 __u16 udp_dst_min; /* inclusive, dest UDP port */
300 __u16 udp_dst_max; /* exclusive, dest UDP port */
301
302 /* DSCP + ECN */
303 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
304 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
305
306 /* MPLS */
307 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
308 __be32 labels[MAX_MPLS_LABELS];
309
310 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
311 __u8 vlan_p;
312 __u8 vlan_cfi;
313 __u16 vlan_id; /* 0xffff means no vlan tag */
314
315 __u8 svlan_p;
316 __u8 svlan_cfi;
317 __u16 svlan_id; /* 0xffff means no svlan tag */
318
319 __u32 src_mac_count; /* How many MACs to iterate through */
320 __u32 dst_mac_count; /* How many MACs to iterate through */
321
322 unsigned char dst_mac[ETH_ALEN];
323 unsigned char src_mac[ETH_ALEN];
324
325 __u32 cur_dst_mac_offset;
326 __u32 cur_src_mac_offset;
327 __be32 cur_saddr;
328 __be32 cur_daddr;
329 __u16 cur_udp_dst;
330 __u16 cur_udp_src;
331 __u32 cur_pkt_size;
332
333 __u8 hh[14];
334 /* = {
335 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
336
337 We fill in SRC address later
338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
339 0x08, 0x00
340 };
341 */
342 __u16 pad; /* pad out the hh struct to an even 16 bytes */
343
344 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
345 * are transmitting the same one multiple times
346 */
347 struct net_device *odev; /* The out-going device. Note that the device should
348 * have it's pg_info pointer pointing back to this
349 * device. This will be set when the user specifies
350 * the out-going device name (not when the inject is
351 * started as it used to do.)
352 */
353 struct flow_state *flows;
354 unsigned cflows; /* Concurrent flows (config) */
355 unsigned lflow; /* Flow length (config) */
356 unsigned nflows; /* accumulated flows (stats) */
357 unsigned curfl; /* current sequenced flow (state)*/
358 #ifdef CONFIG_XFRM
359 __u8 ipsmode; /* IPSEC mode (config) */
360 __u8 ipsproto; /* IPSEC type (config) */
361 #endif
362 char result[512];
363 };
364
365 struct pktgen_hdr {
366 __be32 pgh_magic;
367 __be32 seq_num;
368 __be32 tv_sec;
369 __be32 tv_usec;
370 };
371
372 struct pktgen_thread {
373 spinlock_t if_lock;
374 struct list_head if_list; /* All device here */
375 struct list_head th_list;
376 struct task_struct *tsk;
377 char result[512];
378 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
379
380 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
381
382 u32 control;
383 int cpu;
384
385 wait_queue_head_t queue;
386 };
387
388 #define REMOVE 1
389 #define FIND 0
390
391 /* This code works around the fact that do_div cannot handle two 64-bit
392 numbers, and regular 64-bit division doesn't work on x86 kernels.
393 --Ben
394 */
395
396 #define PG_DIV 0
397
398 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
399 * Function copied/adapted/optimized from:
400 *
401 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
402 *
403 * Copyright 1994, University of Cambridge Computer Laboratory
404 * All Rights Reserved.
405 *
406 */
407 static inline s64 divremdi3(s64 x, s64 y, int type)
408 {
409 u64 a = (x < 0) ? -x : x;
410 u64 b = (y < 0) ? -y : y;
411 u64 res = 0, d = 1;
412
413 if (b > 0) {
414 while (b < a) {
415 b <<= 1;
416 d <<= 1;
417 }
418 }
419
420 do {
421 if (a >= b) {
422 a -= b;
423 res += d;
424 }
425 b >>= 1;
426 d >>= 1;
427 }
428 while (d);
429
430 if (PG_DIV == type) {
431 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
432 } else {
433 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
434 }
435 }
436
437 /* End of hacks to deal with 64-bit math on x86 */
438
439 /** Convert to milliseconds */
440 static inline __u64 tv_to_ms(const struct timeval *tv)
441 {
442 __u64 ms = tv->tv_usec / 1000;
443 ms += (__u64) tv->tv_sec * (__u64) 1000;
444 return ms;
445 }
446
447 /** Convert to micro-seconds */
448 static inline __u64 tv_to_us(const struct timeval *tv)
449 {
450 __u64 us = tv->tv_usec;
451 us += (__u64) tv->tv_sec * (__u64) 1000000;
452 return us;
453 }
454
455 static inline __u64 pg_div(__u64 n, __u32 base)
456 {
457 __u64 tmp = n;
458 do_div(tmp, base);
459 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
460 n, base, tmp); */
461 return tmp;
462 }
463
464 static inline __u64 pg_div64(__u64 n, __u64 base)
465 {
466 __u64 tmp = n;
467 /*
468 * How do we know if the architecture we are running on
469 * supports division with 64 bit base?
470 *
471 */
472 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
473
474 do_div(tmp, base);
475 #else
476 tmp = divremdi3(n, base, PG_DIV);
477 #endif
478 return tmp;
479 }
480
481 static inline __u64 getCurMs(void)
482 {
483 struct timeval tv;
484 do_gettimeofday(&tv);
485 return tv_to_ms(&tv);
486 }
487
488 static inline __u64 getCurUs(void)
489 {
490 struct timeval tv;
491 do_gettimeofday(&tv);
492 return tv_to_us(&tv);
493 }
494
495 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
496 {
497 return tv_to_us(a) - tv_to_us(b);
498 }
499
500 /* old include end */
501
502 static char version[] __initdata = VERSION;
503
504 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
505 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
506 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
507 const char *ifname);
508 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
509 static void pktgen_run_all_threads(void);
510 static void pktgen_stop_all_threads_ifs(void);
511 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
512 static void pktgen_stop(struct pktgen_thread *t);
513 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
514
515 static unsigned int scan_ip6(const char *s, char ip[16]);
516 static unsigned int fmt_ip6(char *s, const char ip[16]);
517
518 /* Module parameters, defaults. */
519 static int pg_count_d = 1000; /* 1000 pkts by default */
520 static int pg_delay_d;
521 static int pg_clone_skb_d;
522 static int debug;
523
524 static DEFINE_MUTEX(pktgen_thread_lock);
525 static LIST_HEAD(pktgen_threads);
526
527 static struct notifier_block pktgen_notifier_block = {
528 .notifier_call = pktgen_device_event,
529 };
530
531 /*
532 * /proc handling functions
533 *
534 */
535
536 static int pgctrl_show(struct seq_file *seq, void *v)
537 {
538 seq_puts(seq, VERSION);
539 return 0;
540 }
541
542 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
543 size_t count, loff_t * ppos)
544 {
545 int err = 0;
546 char data[128];
547
548 if (!capable(CAP_NET_ADMIN)) {
549 err = -EPERM;
550 goto out;
551 }
552
553 if (count > sizeof(data))
554 count = sizeof(data);
555
556 if (copy_from_user(data, buf, count)) {
557 err = -EFAULT;
558 goto out;
559 }
560 data[count - 1] = 0; /* Make string */
561
562 if (!strcmp(data, "stop"))
563 pktgen_stop_all_threads_ifs();
564
565 else if (!strcmp(data, "start"))
566 pktgen_run_all_threads();
567
568 else
569 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
570
571 err = count;
572
573 out:
574 return err;
575 }
576
577 static int pgctrl_open(struct inode *inode, struct file *file)
578 {
579 return single_open(file, pgctrl_show, PDE(inode)->data);
580 }
581
582 static const struct file_operations pktgen_fops = {
583 .owner = THIS_MODULE,
584 .open = pgctrl_open,
585 .read = seq_read,
586 .llseek = seq_lseek,
587 .write = pgctrl_write,
588 .release = single_release,
589 };
590
591 static int pktgen_if_show(struct seq_file *seq, void *v)
592 {
593 int i;
594 struct pktgen_dev *pkt_dev = seq->private;
595 __u64 sa;
596 __u64 stopped;
597 __u64 now = getCurUs();
598
599 seq_printf(seq,
600 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
601 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
602 pkt_dev->max_pkt_size);
603
604 seq_printf(seq,
605 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
606 pkt_dev->nfrags,
607 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
608 pkt_dev->clone_skb, pkt_dev->odev->name);
609
610 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
611 pkt_dev->lflow);
612
613 if (pkt_dev->flags & F_IPV6) {
614 char b1[128], b2[128], b3[128];
615 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
616 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
617 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
618 seq_printf(seq,
619 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
620 b2, b3);
621
622 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
623 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
624 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
625 seq_printf(seq,
626 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
627 b2, b3);
628
629 } else
630 seq_printf(seq,
631 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
632 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
633 pkt_dev->src_max);
634
635 seq_puts(seq, " src_mac: ");
636
637 if (is_zero_ether_addr(pkt_dev->src_mac))
638 for (i = 0; i < 6; i++)
639 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
640 i == 5 ? " " : ":");
641 else
642 for (i = 0; i < 6; i++)
643 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
644 i == 5 ? " " : ":");
645
646 seq_printf(seq, "dst_mac: ");
647 for (i = 0; i < 6; i++)
648 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
649 i == 5 ? "\n" : ":");
650
651 seq_printf(seq,
652 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
653 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
654 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
655
656 seq_printf(seq,
657 " src_mac_count: %d dst_mac_count: %d\n",
658 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
659
660 if (pkt_dev->nr_labels) {
661 unsigned i;
662 seq_printf(seq, " mpls: ");
663 for (i = 0; i < pkt_dev->nr_labels; i++)
664 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
665 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
666 }
667
668 if (pkt_dev->vlan_id != 0xffff) {
669 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
670 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
671 }
672
673 if (pkt_dev->svlan_id != 0xffff) {
674 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
675 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
676 }
677
678 if (pkt_dev->tos) {
679 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
680 }
681
682 if (pkt_dev->traffic_class) {
683 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
684 }
685
686 seq_printf(seq, " Flags: ");
687
688 if (pkt_dev->flags & F_IPV6)
689 seq_printf(seq, "IPV6 ");
690
691 if (pkt_dev->flags & F_IPSRC_RND)
692 seq_printf(seq, "IPSRC_RND ");
693
694 if (pkt_dev->flags & F_IPDST_RND)
695 seq_printf(seq, "IPDST_RND ");
696
697 if (pkt_dev->flags & F_TXSIZE_RND)
698 seq_printf(seq, "TXSIZE_RND ");
699
700 if (pkt_dev->flags & F_UDPSRC_RND)
701 seq_printf(seq, "UDPSRC_RND ");
702
703 if (pkt_dev->flags & F_UDPDST_RND)
704 seq_printf(seq, "UDPDST_RND ");
705
706 if (pkt_dev->flags & F_MPLS_RND)
707 seq_printf(seq, "MPLS_RND ");
708
709 if (pkt_dev->cflows) {
710 if (pkt_dev->flags & F_FLOW_SEQ)
711 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
712 else
713 seq_printf(seq, "FLOW_RND ");
714 }
715
716 #ifdef CONFIG_XFRM
717 if (pkt_dev->flags & F_IPSEC_ON)
718 seq_printf(seq, "IPSEC ");
719 #endif
720
721 if (pkt_dev->flags & F_MACSRC_RND)
722 seq_printf(seq, "MACSRC_RND ");
723
724 if (pkt_dev->flags & F_MACDST_RND)
725 seq_printf(seq, "MACDST_RND ");
726
727 if (pkt_dev->flags & F_VID_RND)
728 seq_printf(seq, "VID_RND ");
729
730 if (pkt_dev->flags & F_SVID_RND)
731 seq_printf(seq, "SVID_RND ");
732
733 seq_puts(seq, "\n");
734
735 sa = pkt_dev->started_at;
736 stopped = pkt_dev->stopped_at;
737 if (pkt_dev->running)
738 stopped = now; /* not really stopped, more like last-running-at */
739
740 seq_printf(seq,
741 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
742 (unsigned long long)pkt_dev->sofar,
743 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
744 (unsigned long long)stopped,
745 (unsigned long long)pkt_dev->idle_acc);
746
747 seq_printf(seq,
748 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
749 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
750 pkt_dev->cur_src_mac_offset);
751
752 if (pkt_dev->flags & F_IPV6) {
753 char b1[128], b2[128];
754 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
755 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
756 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
757 } else
758 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
759 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
760
761 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
762 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
763
764 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
765
766 if (pkt_dev->result[0])
767 seq_printf(seq, "Result: %s\n", pkt_dev->result);
768 else
769 seq_printf(seq, "Result: Idle\n");
770
771 return 0;
772 }
773
774
775 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
776 {
777 int i = 0;
778 *num = 0;
779
780 for (; i < maxlen; i++) {
781 char c;
782 *num <<= 4;
783 if (get_user(c, &user_buffer[i]))
784 return -EFAULT;
785 if ((c >= '0') && (c <= '9'))
786 *num |= c - '0';
787 else if ((c >= 'a') && (c <= 'f'))
788 *num |= c - 'a' + 10;
789 else if ((c >= 'A') && (c <= 'F'))
790 *num |= c - 'A' + 10;
791 else
792 break;
793 }
794 return i;
795 }
796
797 static int count_trail_chars(const char __user * user_buffer,
798 unsigned int maxlen)
799 {
800 int i;
801
802 for (i = 0; i < maxlen; i++) {
803 char c;
804 if (get_user(c, &user_buffer[i]))
805 return -EFAULT;
806 switch (c) {
807 case '\"':
808 case '\n':
809 case '\r':
810 case '\t':
811 case ' ':
812 case '=':
813 break;
814 default:
815 goto done;
816 }
817 }
818 done:
819 return i;
820 }
821
822 static unsigned long num_arg(const char __user * user_buffer,
823 unsigned long maxlen, unsigned long *num)
824 {
825 int i = 0;
826 *num = 0;
827
828 for (; i < maxlen; i++) {
829 char c;
830 if (get_user(c, &user_buffer[i]))
831 return -EFAULT;
832 if ((c >= '0') && (c <= '9')) {
833 *num *= 10;
834 *num += c - '0';
835 } else
836 break;
837 }
838 return i;
839 }
840
841 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
842 {
843 int i = 0;
844
845 for (; i < maxlen; i++) {
846 char c;
847 if (get_user(c, &user_buffer[i]))
848 return -EFAULT;
849 switch (c) {
850 case '\"':
851 case '\n':
852 case '\r':
853 case '\t':
854 case ' ':
855 goto done_str;
856 break;
857 default:
858 break;
859 }
860 }
861 done_str:
862 return i;
863 }
864
865 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
866 {
867 unsigned n = 0;
868 char c;
869 ssize_t i = 0;
870 int len;
871
872 pkt_dev->nr_labels = 0;
873 do {
874 __u32 tmp;
875 len = hex32_arg(&buffer[i], 8, &tmp);
876 if (len <= 0)
877 return len;
878 pkt_dev->labels[n] = htonl(tmp);
879 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
880 pkt_dev->flags |= F_MPLS_RND;
881 i += len;
882 if (get_user(c, &buffer[i]))
883 return -EFAULT;
884 i++;
885 n++;
886 if (n >= MAX_MPLS_LABELS)
887 return -E2BIG;
888 } while (c == ',');
889
890 pkt_dev->nr_labels = n;
891 return i;
892 }
893
894 static ssize_t pktgen_if_write(struct file *file,
895 const char __user * user_buffer, size_t count,
896 loff_t * offset)
897 {
898 struct seq_file *seq = (struct seq_file *)file->private_data;
899 struct pktgen_dev *pkt_dev = seq->private;
900 int i = 0, max, len;
901 char name[16], valstr[32];
902 unsigned long value = 0;
903 char *pg_result = NULL;
904 int tmp = 0;
905 char buf[128];
906
907 pg_result = &(pkt_dev->result[0]);
908
909 if (count < 1) {
910 printk(KERN_WARNING "pktgen: wrong command format\n");
911 return -EINVAL;
912 }
913
914 max = count - i;
915 tmp = count_trail_chars(&user_buffer[i], max);
916 if (tmp < 0) {
917 printk(KERN_WARNING "pktgen: illegal format\n");
918 return tmp;
919 }
920 i += tmp;
921
922 /* Read variable name */
923
924 len = strn_len(&user_buffer[i], sizeof(name) - 1);
925 if (len < 0) {
926 return len;
927 }
928 memset(name, 0, sizeof(name));
929 if (copy_from_user(name, &user_buffer[i], len))
930 return -EFAULT;
931 i += len;
932
933 max = count - i;
934 len = count_trail_chars(&user_buffer[i], max);
935 if (len < 0)
936 return len;
937
938 i += len;
939
940 if (debug) {
941 char tb[count + 1];
942 if (copy_from_user(tb, user_buffer, count))
943 return -EFAULT;
944 tb[count] = 0;
945 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
946 (unsigned long)count, tb);
947 }
948
949 if (!strcmp(name, "min_pkt_size")) {
950 len = num_arg(&user_buffer[i], 10, &value);
951 if (len < 0) {
952 return len;
953 }
954 i += len;
955 if (value < 14 + 20 + 8)
956 value = 14 + 20 + 8;
957 if (value != pkt_dev->min_pkt_size) {
958 pkt_dev->min_pkt_size = value;
959 pkt_dev->cur_pkt_size = value;
960 }
961 sprintf(pg_result, "OK: min_pkt_size=%u",
962 pkt_dev->min_pkt_size);
963 return count;
964 }
965
966 if (!strcmp(name, "max_pkt_size")) {
967 len = num_arg(&user_buffer[i], 10, &value);
968 if (len < 0) {
969 return len;
970 }
971 i += len;
972 if (value < 14 + 20 + 8)
973 value = 14 + 20 + 8;
974 if (value != pkt_dev->max_pkt_size) {
975 pkt_dev->max_pkt_size = value;
976 pkt_dev->cur_pkt_size = value;
977 }
978 sprintf(pg_result, "OK: max_pkt_size=%u",
979 pkt_dev->max_pkt_size);
980 return count;
981 }
982
983 /* Shortcut for min = max */
984
985 if (!strcmp(name, "pkt_size")) {
986 len = num_arg(&user_buffer[i], 10, &value);
987 if (len < 0) {
988 return len;
989 }
990 i += len;
991 if (value < 14 + 20 + 8)
992 value = 14 + 20 + 8;
993 if (value != pkt_dev->min_pkt_size) {
994 pkt_dev->min_pkt_size = value;
995 pkt_dev->max_pkt_size = value;
996 pkt_dev->cur_pkt_size = value;
997 }
998 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
999 return count;
1000 }
1001
1002 if (!strcmp(name, "debug")) {
1003 len = num_arg(&user_buffer[i], 10, &value);
1004 if (len < 0) {
1005 return len;
1006 }
1007 i += len;
1008 debug = value;
1009 sprintf(pg_result, "OK: debug=%u", debug);
1010 return count;
1011 }
1012
1013 if (!strcmp(name, "frags")) {
1014 len = num_arg(&user_buffer[i], 10, &value);
1015 if (len < 0) {
1016 return len;
1017 }
1018 i += len;
1019 pkt_dev->nfrags = value;
1020 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1021 return count;
1022 }
1023 if (!strcmp(name, "delay")) {
1024 len = num_arg(&user_buffer[i], 10, &value);
1025 if (len < 0) {
1026 return len;
1027 }
1028 i += len;
1029 if (value == 0x7FFFFFFF) {
1030 pkt_dev->delay_us = 0x7FFFFFFF;
1031 pkt_dev->delay_ns = 0;
1032 } else {
1033 pkt_dev->delay_us = value / 1000;
1034 pkt_dev->delay_ns = value % 1000;
1035 }
1036 sprintf(pg_result, "OK: delay=%u",
1037 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1038 return count;
1039 }
1040 if (!strcmp(name, "udp_src_min")) {
1041 len = num_arg(&user_buffer[i], 10, &value);
1042 if (len < 0) {
1043 return len;
1044 }
1045 i += len;
1046 if (value != pkt_dev->udp_src_min) {
1047 pkt_dev->udp_src_min = value;
1048 pkt_dev->cur_udp_src = value;
1049 }
1050 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1051 return count;
1052 }
1053 if (!strcmp(name, "udp_dst_min")) {
1054 len = num_arg(&user_buffer[i], 10, &value);
1055 if (len < 0) {
1056 return len;
1057 }
1058 i += len;
1059 if (value != pkt_dev->udp_dst_min) {
1060 pkt_dev->udp_dst_min = value;
1061 pkt_dev->cur_udp_dst = value;
1062 }
1063 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1064 return count;
1065 }
1066 if (!strcmp(name, "udp_src_max")) {
1067 len = num_arg(&user_buffer[i], 10, &value);
1068 if (len < 0) {
1069 return len;
1070 }
1071 i += len;
1072 if (value != pkt_dev->udp_src_max) {
1073 pkt_dev->udp_src_max = value;
1074 pkt_dev->cur_udp_src = value;
1075 }
1076 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1077 return count;
1078 }
1079 if (!strcmp(name, "udp_dst_max")) {
1080 len = num_arg(&user_buffer[i], 10, &value);
1081 if (len < 0) {
1082 return len;
1083 }
1084 i += len;
1085 if (value != pkt_dev->udp_dst_max) {
1086 pkt_dev->udp_dst_max = value;
1087 pkt_dev->cur_udp_dst = value;
1088 }
1089 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1090 return count;
1091 }
1092 if (!strcmp(name, "clone_skb")) {
1093 len = num_arg(&user_buffer[i], 10, &value);
1094 if (len < 0) {
1095 return len;
1096 }
1097 i += len;
1098 pkt_dev->clone_skb = value;
1099
1100 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1101 return count;
1102 }
1103 if (!strcmp(name, "count")) {
1104 len = num_arg(&user_buffer[i], 10, &value);
1105 if (len < 0) {
1106 return len;
1107 }
1108 i += len;
1109 pkt_dev->count = value;
1110 sprintf(pg_result, "OK: count=%llu",
1111 (unsigned long long)pkt_dev->count);
1112 return count;
1113 }
1114 if (!strcmp(name, "src_mac_count")) {
1115 len = num_arg(&user_buffer[i], 10, &value);
1116 if (len < 0) {
1117 return len;
1118 }
1119 i += len;
1120 if (pkt_dev->src_mac_count != value) {
1121 pkt_dev->src_mac_count = value;
1122 pkt_dev->cur_src_mac_offset = 0;
1123 }
1124 sprintf(pg_result, "OK: src_mac_count=%d",
1125 pkt_dev->src_mac_count);
1126 return count;
1127 }
1128 if (!strcmp(name, "dst_mac_count")) {
1129 len = num_arg(&user_buffer[i], 10, &value);
1130 if (len < 0) {
1131 return len;
1132 }
1133 i += len;
1134 if (pkt_dev->dst_mac_count != value) {
1135 pkt_dev->dst_mac_count = value;
1136 pkt_dev->cur_dst_mac_offset = 0;
1137 }
1138 sprintf(pg_result, "OK: dst_mac_count=%d",
1139 pkt_dev->dst_mac_count);
1140 return count;
1141 }
1142 if (!strcmp(name, "flag")) {
1143 char f[32];
1144 memset(f, 0, 32);
1145 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1146 if (len < 0) {
1147 return len;
1148 }
1149 if (copy_from_user(f, &user_buffer[i], len))
1150 return -EFAULT;
1151 i += len;
1152 if (strcmp(f, "IPSRC_RND") == 0)
1153 pkt_dev->flags |= F_IPSRC_RND;
1154
1155 else if (strcmp(f, "!IPSRC_RND") == 0)
1156 pkt_dev->flags &= ~F_IPSRC_RND;
1157
1158 else if (strcmp(f, "TXSIZE_RND") == 0)
1159 pkt_dev->flags |= F_TXSIZE_RND;
1160
1161 else if (strcmp(f, "!TXSIZE_RND") == 0)
1162 pkt_dev->flags &= ~F_TXSIZE_RND;
1163
1164 else if (strcmp(f, "IPDST_RND") == 0)
1165 pkt_dev->flags |= F_IPDST_RND;
1166
1167 else if (strcmp(f, "!IPDST_RND") == 0)
1168 pkt_dev->flags &= ~F_IPDST_RND;
1169
1170 else if (strcmp(f, "UDPSRC_RND") == 0)
1171 pkt_dev->flags |= F_UDPSRC_RND;
1172
1173 else if (strcmp(f, "!UDPSRC_RND") == 0)
1174 pkt_dev->flags &= ~F_UDPSRC_RND;
1175
1176 else if (strcmp(f, "UDPDST_RND") == 0)
1177 pkt_dev->flags |= F_UDPDST_RND;
1178
1179 else if (strcmp(f, "!UDPDST_RND") == 0)
1180 pkt_dev->flags &= ~F_UDPDST_RND;
1181
1182 else if (strcmp(f, "MACSRC_RND") == 0)
1183 pkt_dev->flags |= F_MACSRC_RND;
1184
1185 else if (strcmp(f, "!MACSRC_RND") == 0)
1186 pkt_dev->flags &= ~F_MACSRC_RND;
1187
1188 else if (strcmp(f, "MACDST_RND") == 0)
1189 pkt_dev->flags |= F_MACDST_RND;
1190
1191 else if (strcmp(f, "!MACDST_RND") == 0)
1192 pkt_dev->flags &= ~F_MACDST_RND;
1193
1194 else if (strcmp(f, "MPLS_RND") == 0)
1195 pkt_dev->flags |= F_MPLS_RND;
1196
1197 else if (strcmp(f, "!MPLS_RND") == 0)
1198 pkt_dev->flags &= ~F_MPLS_RND;
1199
1200 else if (strcmp(f, "VID_RND") == 0)
1201 pkt_dev->flags |= F_VID_RND;
1202
1203 else if (strcmp(f, "!VID_RND") == 0)
1204 pkt_dev->flags &= ~F_VID_RND;
1205
1206 else if (strcmp(f, "SVID_RND") == 0)
1207 pkt_dev->flags |= F_SVID_RND;
1208
1209 else if (strcmp(f, "!SVID_RND") == 0)
1210 pkt_dev->flags &= ~F_SVID_RND;
1211
1212 else if (strcmp(f, "FLOW_SEQ") == 0)
1213 pkt_dev->flags |= F_FLOW_SEQ;
1214
1215 #ifdef CONFIG_XFRM
1216 else if (strcmp(f, "IPSEC") == 0)
1217 pkt_dev->flags |= F_IPSEC_ON;
1218 #endif
1219
1220 else if (strcmp(f, "!IPV6") == 0)
1221 pkt_dev->flags &= ~F_IPV6;
1222
1223 else {
1224 sprintf(pg_result,
1225 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1226 f,
1227 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1228 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1229 return count;
1230 }
1231 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1232 return count;
1233 }
1234 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1235 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1236 if (len < 0) {
1237 return len;
1238 }
1239
1240 if (copy_from_user(buf, &user_buffer[i], len))
1241 return -EFAULT;
1242 buf[len] = 0;
1243 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1244 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1245 strncpy(pkt_dev->dst_min, buf, len);
1246 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1247 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1248 }
1249 if (debug)
1250 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1251 pkt_dev->dst_min);
1252 i += len;
1253 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1254 return count;
1255 }
1256 if (!strcmp(name, "dst_max")) {
1257 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1258 if (len < 0) {
1259 return len;
1260 }
1261
1262 if (copy_from_user(buf, &user_buffer[i], len))
1263 return -EFAULT;
1264
1265 buf[len] = 0;
1266 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1267 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1268 strncpy(pkt_dev->dst_max, buf, len);
1269 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1270 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1271 }
1272 if (debug)
1273 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1274 pkt_dev->dst_max);
1275 i += len;
1276 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1277 return count;
1278 }
1279 if (!strcmp(name, "dst6")) {
1280 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1281 if (len < 0)
1282 return len;
1283
1284 pkt_dev->flags |= F_IPV6;
1285
1286 if (copy_from_user(buf, &user_buffer[i], len))
1287 return -EFAULT;
1288 buf[len] = 0;
1289
1290 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1291 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1292
1293 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1294
1295 if (debug)
1296 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1297
1298 i += len;
1299 sprintf(pg_result, "OK: dst6=%s", buf);
1300 return count;
1301 }
1302 if (!strcmp(name, "dst6_min")) {
1303 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1304 if (len < 0)
1305 return len;
1306
1307 pkt_dev->flags |= F_IPV6;
1308
1309 if (copy_from_user(buf, &user_buffer[i], len))
1310 return -EFAULT;
1311 buf[len] = 0;
1312
1313 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1314 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1315
1316 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1317 &pkt_dev->min_in6_daddr);
1318 if (debug)
1319 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1320
1321 i += len;
1322 sprintf(pg_result, "OK: dst6_min=%s", buf);
1323 return count;
1324 }
1325 if (!strcmp(name, "dst6_max")) {
1326 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1327 if (len < 0)
1328 return len;
1329
1330 pkt_dev->flags |= F_IPV6;
1331
1332 if (copy_from_user(buf, &user_buffer[i], len))
1333 return -EFAULT;
1334 buf[len] = 0;
1335
1336 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1337 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1338
1339 if (debug)
1340 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1341
1342 i += len;
1343 sprintf(pg_result, "OK: dst6_max=%s", buf);
1344 return count;
1345 }
1346 if (!strcmp(name, "src6")) {
1347 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1348 if (len < 0)
1349 return len;
1350
1351 pkt_dev->flags |= F_IPV6;
1352
1353 if (copy_from_user(buf, &user_buffer[i], len))
1354 return -EFAULT;
1355 buf[len] = 0;
1356
1357 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1358 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1359
1360 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1361
1362 if (debug)
1363 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1364
1365 i += len;
1366 sprintf(pg_result, "OK: src6=%s", buf);
1367 return count;
1368 }
1369 if (!strcmp(name, "src_min")) {
1370 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1371 if (len < 0) {
1372 return len;
1373 }
1374 if (copy_from_user(buf, &user_buffer[i], len))
1375 return -EFAULT;
1376 buf[len] = 0;
1377 if (strcmp(buf, pkt_dev->src_min) != 0) {
1378 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1379 strncpy(pkt_dev->src_min, buf, len);
1380 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1381 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1382 }
1383 if (debug)
1384 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1385 pkt_dev->src_min);
1386 i += len;
1387 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1388 return count;
1389 }
1390 if (!strcmp(name, "src_max")) {
1391 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1392 if (len < 0) {
1393 return len;
1394 }
1395 if (copy_from_user(buf, &user_buffer[i], len))
1396 return -EFAULT;
1397 buf[len] = 0;
1398 if (strcmp(buf, pkt_dev->src_max) != 0) {
1399 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1400 strncpy(pkt_dev->src_max, buf, len);
1401 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1402 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1403 }
1404 if (debug)
1405 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1406 pkt_dev->src_max);
1407 i += len;
1408 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1409 return count;
1410 }
1411 if (!strcmp(name, "dst_mac")) {
1412 char *v = valstr;
1413 unsigned char old_dmac[ETH_ALEN];
1414 unsigned char *m = pkt_dev->dst_mac;
1415 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1416
1417 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1418 if (len < 0) {
1419 return len;
1420 }
1421 memset(valstr, 0, sizeof(valstr));
1422 if (copy_from_user(valstr, &user_buffer[i], len))
1423 return -EFAULT;
1424 i += len;
1425
1426 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1427 if (*v >= '0' && *v <= '9') {
1428 *m *= 16;
1429 *m += *v - '0';
1430 }
1431 if (*v >= 'A' && *v <= 'F') {
1432 *m *= 16;
1433 *m += *v - 'A' + 10;
1434 }
1435 if (*v >= 'a' && *v <= 'f') {
1436 *m *= 16;
1437 *m += *v - 'a' + 10;
1438 }
1439 if (*v == ':') {
1440 m++;
1441 *m = 0;
1442 }
1443 }
1444
1445 /* Set up Dest MAC */
1446 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1447 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1448
1449 sprintf(pg_result, "OK: dstmac");
1450 return count;
1451 }
1452 if (!strcmp(name, "src_mac")) {
1453 char *v = valstr;
1454 unsigned char *m = pkt_dev->src_mac;
1455
1456 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1457 if (len < 0) {
1458 return len;
1459 }
1460 memset(valstr, 0, sizeof(valstr));
1461 if (copy_from_user(valstr, &user_buffer[i], len))
1462 return -EFAULT;
1463 i += len;
1464
1465 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1466 if (*v >= '0' && *v <= '9') {
1467 *m *= 16;
1468 *m += *v - '0';
1469 }
1470 if (*v >= 'A' && *v <= 'F') {
1471 *m *= 16;
1472 *m += *v - 'A' + 10;
1473 }
1474 if (*v >= 'a' && *v <= 'f') {
1475 *m *= 16;
1476 *m += *v - 'a' + 10;
1477 }
1478 if (*v == ':') {
1479 m++;
1480 *m = 0;
1481 }
1482 }
1483
1484 sprintf(pg_result, "OK: srcmac");
1485 return count;
1486 }
1487
1488 if (!strcmp(name, "clear_counters")) {
1489 pktgen_clear_counters(pkt_dev);
1490 sprintf(pg_result, "OK: Clearing counters.\n");
1491 return count;
1492 }
1493
1494 if (!strcmp(name, "flows")) {
1495 len = num_arg(&user_buffer[i], 10, &value);
1496 if (len < 0) {
1497 return len;
1498 }
1499 i += len;
1500 if (value > MAX_CFLOWS)
1501 value = MAX_CFLOWS;
1502
1503 pkt_dev->cflows = value;
1504 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1505 return count;
1506 }
1507
1508 if (!strcmp(name, "flowlen")) {
1509 len = num_arg(&user_buffer[i], 10, &value);
1510 if (len < 0) {
1511 return len;
1512 }
1513 i += len;
1514 pkt_dev->lflow = value;
1515 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1516 return count;
1517 }
1518
1519 if (!strcmp(name, "mpls")) {
1520 unsigned n, offset;
1521 len = get_labels(&user_buffer[i], pkt_dev);
1522 if (len < 0) { return len; }
1523 i += len;
1524 offset = sprintf(pg_result, "OK: mpls=");
1525 for (n = 0; n < pkt_dev->nr_labels; n++)
1526 offset += sprintf(pg_result + offset,
1527 "%08x%s", ntohl(pkt_dev->labels[n]),
1528 n == pkt_dev->nr_labels-1 ? "" : ",");
1529
1530 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1531 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1532 pkt_dev->svlan_id = 0xffff;
1533
1534 if (debug)
1535 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1536 }
1537 return count;
1538 }
1539
1540 if (!strcmp(name, "vlan_id")) {
1541 len = num_arg(&user_buffer[i], 4, &value);
1542 if (len < 0) {
1543 return len;
1544 }
1545 i += len;
1546 if (value <= 4095) {
1547 pkt_dev->vlan_id = value; /* turn on VLAN */
1548
1549 if (debug)
1550 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1551
1552 if (debug && pkt_dev->nr_labels)
1553 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1554
1555 pkt_dev->nr_labels = 0; /* turn off MPLS */
1556 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1557 } else {
1558 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1559 pkt_dev->svlan_id = 0xffff;
1560
1561 if (debug)
1562 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1563 }
1564 return count;
1565 }
1566
1567 if (!strcmp(name, "vlan_p")) {
1568 len = num_arg(&user_buffer[i], 1, &value);
1569 if (len < 0) {
1570 return len;
1571 }
1572 i += len;
1573 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1574 pkt_dev->vlan_p = value;
1575 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1576 } else {
1577 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1578 }
1579 return count;
1580 }
1581
1582 if (!strcmp(name, "vlan_cfi")) {
1583 len = num_arg(&user_buffer[i], 1, &value);
1584 if (len < 0) {
1585 return len;
1586 }
1587 i += len;
1588 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1589 pkt_dev->vlan_cfi = value;
1590 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1591 } else {
1592 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1593 }
1594 return count;
1595 }
1596
1597 if (!strcmp(name, "svlan_id")) {
1598 len = num_arg(&user_buffer[i], 4, &value);
1599 if (len < 0) {
1600 return len;
1601 }
1602 i += len;
1603 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1604 pkt_dev->svlan_id = value; /* turn on SVLAN */
1605
1606 if (debug)
1607 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1608
1609 if (debug && pkt_dev->nr_labels)
1610 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1611
1612 pkt_dev->nr_labels = 0; /* turn off MPLS */
1613 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1614 } else {
1615 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1616 pkt_dev->svlan_id = 0xffff;
1617
1618 if (debug)
1619 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1620 }
1621 return count;
1622 }
1623
1624 if (!strcmp(name, "svlan_p")) {
1625 len = num_arg(&user_buffer[i], 1, &value);
1626 if (len < 0) {
1627 return len;
1628 }
1629 i += len;
1630 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1631 pkt_dev->svlan_p = value;
1632 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1633 } else {
1634 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1635 }
1636 return count;
1637 }
1638
1639 if (!strcmp(name, "svlan_cfi")) {
1640 len = num_arg(&user_buffer[i], 1, &value);
1641 if (len < 0) {
1642 return len;
1643 }
1644 i += len;
1645 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1646 pkt_dev->svlan_cfi = value;
1647 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1648 } else {
1649 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1650 }
1651 return count;
1652 }
1653
1654 if (!strcmp(name, "tos")) {
1655 __u32 tmp_value = 0;
1656 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1657 if (len < 0) {
1658 return len;
1659 }
1660 i += len;
1661 if (len == 2) {
1662 pkt_dev->tos = tmp_value;
1663 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1664 } else {
1665 sprintf(pg_result, "ERROR: tos must be 00-ff");
1666 }
1667 return count;
1668 }
1669
1670 if (!strcmp(name, "traffic_class")) {
1671 __u32 tmp_value = 0;
1672 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1673 if (len < 0) {
1674 return len;
1675 }
1676 i += len;
1677 if (len == 2) {
1678 pkt_dev->traffic_class = tmp_value;
1679 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1680 } else {
1681 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1682 }
1683 return count;
1684 }
1685
1686 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1687 return -EINVAL;
1688 }
1689
1690 static int pktgen_if_open(struct inode *inode, struct file *file)
1691 {
1692 return single_open(file, pktgen_if_show, PDE(inode)->data);
1693 }
1694
1695 static const struct file_operations pktgen_if_fops = {
1696 .owner = THIS_MODULE,
1697 .open = pktgen_if_open,
1698 .read = seq_read,
1699 .llseek = seq_lseek,
1700 .write = pktgen_if_write,
1701 .release = single_release,
1702 };
1703
1704 static int pktgen_thread_show(struct seq_file *seq, void *v)
1705 {
1706 struct pktgen_thread *t = seq->private;
1707 struct pktgen_dev *pkt_dev;
1708
1709 BUG_ON(!t);
1710
1711 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1712 t->tsk->comm, t->max_before_softirq);
1713
1714 seq_printf(seq, "Running: ");
1715
1716 if_lock(t);
1717 list_for_each_entry(pkt_dev, &t->if_list, list)
1718 if (pkt_dev->running)
1719 seq_printf(seq, "%s ", pkt_dev->odev->name);
1720
1721 seq_printf(seq, "\nStopped: ");
1722
1723 list_for_each_entry(pkt_dev, &t->if_list, list)
1724 if (!pkt_dev->running)
1725 seq_printf(seq, "%s ", pkt_dev->odev->name);
1726
1727 if (t->result[0])
1728 seq_printf(seq, "\nResult: %s\n", t->result);
1729 else
1730 seq_printf(seq, "\nResult: NA\n");
1731
1732 if_unlock(t);
1733
1734 return 0;
1735 }
1736
1737 static ssize_t pktgen_thread_write(struct file *file,
1738 const char __user * user_buffer,
1739 size_t count, loff_t * offset)
1740 {
1741 struct seq_file *seq = (struct seq_file *)file->private_data;
1742 struct pktgen_thread *t = seq->private;
1743 int i = 0, max, len, ret;
1744 char name[40];
1745 char *pg_result;
1746 unsigned long value = 0;
1747
1748 if (count < 1) {
1749 // sprintf(pg_result, "Wrong command format");
1750 return -EINVAL;
1751 }
1752
1753 max = count - i;
1754 len = count_trail_chars(&user_buffer[i], max);
1755 if (len < 0)
1756 return len;
1757
1758 i += len;
1759
1760 /* Read variable name */
1761
1762 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1763 if (len < 0)
1764 return len;
1765
1766 memset(name, 0, sizeof(name));
1767 if (copy_from_user(name, &user_buffer[i], len))
1768 return -EFAULT;
1769 i += len;
1770
1771 max = count - i;
1772 len = count_trail_chars(&user_buffer[i], max);
1773 if (len < 0)
1774 return len;
1775
1776 i += len;
1777
1778 if (debug)
1779 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1780 name, (unsigned long)count);
1781
1782 if (!t) {
1783 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1784 ret = -EINVAL;
1785 goto out;
1786 }
1787
1788 pg_result = &(t->result[0]);
1789
1790 if (!strcmp(name, "add_device")) {
1791 char f[32];
1792 memset(f, 0, 32);
1793 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1794 if (len < 0) {
1795 ret = len;
1796 goto out;
1797 }
1798 if (copy_from_user(f, &user_buffer[i], len))
1799 return -EFAULT;
1800 i += len;
1801 mutex_lock(&pktgen_thread_lock);
1802 pktgen_add_device(t, f);
1803 mutex_unlock(&pktgen_thread_lock);
1804 ret = count;
1805 sprintf(pg_result, "OK: add_device=%s", f);
1806 goto out;
1807 }
1808
1809 if (!strcmp(name, "rem_device_all")) {
1810 mutex_lock(&pktgen_thread_lock);
1811 t->control |= T_REMDEVALL;
1812 mutex_unlock(&pktgen_thread_lock);
1813 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1814 ret = count;
1815 sprintf(pg_result, "OK: rem_device_all");
1816 goto out;
1817 }
1818
1819 if (!strcmp(name, "max_before_softirq")) {
1820 len = num_arg(&user_buffer[i], 10, &value);
1821 mutex_lock(&pktgen_thread_lock);
1822 t->max_before_softirq = value;
1823 mutex_unlock(&pktgen_thread_lock);
1824 ret = count;
1825 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1826 goto out;
1827 }
1828
1829 ret = -EINVAL;
1830 out:
1831 return ret;
1832 }
1833
1834 static int pktgen_thread_open(struct inode *inode, struct file *file)
1835 {
1836 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1837 }
1838
1839 static const struct file_operations pktgen_thread_fops = {
1840 .owner = THIS_MODULE,
1841 .open = pktgen_thread_open,
1842 .read = seq_read,
1843 .llseek = seq_lseek,
1844 .write = pktgen_thread_write,
1845 .release = single_release,
1846 };
1847
1848 /* Think find or remove for NN */
1849 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1850 {
1851 struct pktgen_thread *t;
1852 struct pktgen_dev *pkt_dev = NULL;
1853
1854 list_for_each_entry(t, &pktgen_threads, th_list) {
1855 pkt_dev = pktgen_find_dev(t, ifname);
1856 if (pkt_dev) {
1857 if (remove) {
1858 if_lock(t);
1859 pkt_dev->removal_mark = 1;
1860 t->control |= T_REMDEV;
1861 if_unlock(t);
1862 }
1863 break;
1864 }
1865 }
1866 return pkt_dev;
1867 }
1868
1869 /*
1870 * mark a device for removal
1871 */
1872 static void pktgen_mark_device(const char *ifname)
1873 {
1874 struct pktgen_dev *pkt_dev = NULL;
1875 const int max_tries = 10, msec_per_try = 125;
1876 int i = 0;
1877
1878 mutex_lock(&pktgen_thread_lock);
1879 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1880
1881 while (1) {
1882
1883 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1884 if (pkt_dev == NULL)
1885 break; /* success */
1886
1887 mutex_unlock(&pktgen_thread_lock);
1888 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1889 "to disappear....\n", ifname);
1890 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1891 mutex_lock(&pktgen_thread_lock);
1892
1893 if (++i >= max_tries) {
1894 printk(KERN_ERR "pktgen_mark_device: timed out after "
1895 "waiting %d msec for device %s to be removed\n",
1896 msec_per_try * i, ifname);
1897 break;
1898 }
1899
1900 }
1901
1902 mutex_unlock(&pktgen_thread_lock);
1903 }
1904
1905 static void pktgen_change_name(struct net_device *dev)
1906 {
1907 struct pktgen_thread *t;
1908
1909 list_for_each_entry(t, &pktgen_threads, th_list) {
1910 struct pktgen_dev *pkt_dev;
1911
1912 list_for_each_entry(pkt_dev, &t->if_list, list) {
1913 if (pkt_dev->odev != dev)
1914 continue;
1915
1916 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1917
1918 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1919 pg_proc_dir);
1920 if (!pkt_dev->entry)
1921 printk(KERN_ERR "pktgen: can't move proc "
1922 " entry for '%s'\n", dev->name);
1923 break;
1924 }
1925 }
1926 }
1927
1928 static int pktgen_device_event(struct notifier_block *unused,
1929 unsigned long event, void *ptr)
1930 {
1931 struct net_device *dev = ptr;
1932
1933 /* It is OK that we do not hold the group lock right now,
1934 * as we run under the RTNL lock.
1935 */
1936
1937 switch (event) {
1938 case NETDEV_CHANGENAME:
1939 pktgen_change_name(dev);
1940 break;
1941
1942 case NETDEV_UNREGISTER:
1943 pktgen_mark_device(dev->name);
1944 break;
1945 }
1946
1947 return NOTIFY_DONE;
1948 }
1949
1950 /* Associate pktgen_dev with a device. */
1951
1952 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1953 {
1954 struct net_device *odev;
1955 int err;
1956
1957 /* Clean old setups */
1958 if (pkt_dev->odev) {
1959 dev_put(pkt_dev->odev);
1960 pkt_dev->odev = NULL;
1961 }
1962
1963 odev = dev_get_by_name(ifname);
1964 if (!odev) {
1965 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1966 return -ENODEV;
1967 }
1968
1969 if (odev->type != ARPHRD_ETHER) {
1970 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1971 err = -EINVAL;
1972 } else if (!netif_running(odev)) {
1973 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1974 err = -ENETDOWN;
1975 } else {
1976 pkt_dev->odev = odev;
1977 return 0;
1978 }
1979
1980 dev_put(odev);
1981 return err;
1982 }
1983
1984 /* Read pkt_dev from the interface and set up internal pktgen_dev
1985 * structure to have the right information to create/send packets
1986 */
1987 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1988 {
1989 if (!pkt_dev->odev) {
1990 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1991 "setup_inject.\n");
1992 sprintf(pkt_dev->result,
1993 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1994 return;
1995 }
1996
1997 /* Default to the interface's mac if not explicitly set. */
1998
1999 if (is_zero_ether_addr(pkt_dev->src_mac))
2000 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2001
2002 /* Set up Dest MAC */
2003 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2004
2005 /* Set up pkt size */
2006 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2007
2008 if (pkt_dev->flags & F_IPV6) {
2009 /*
2010 * Skip this automatic address setting until locks or functions
2011 * gets exported
2012 */
2013
2014 #ifdef NOTNOW
2015 int i, set = 0, err = 1;
2016 struct inet6_dev *idev;
2017
2018 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2019 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2020 set = 1;
2021 break;
2022 }
2023
2024 if (!set) {
2025
2026 /*
2027 * Use linklevel address if unconfigured.
2028 *
2029 * use ipv6_get_lladdr if/when it's get exported
2030 */
2031
2032 rcu_read_lock();
2033 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2034 struct inet6_ifaddr *ifp;
2035
2036 read_lock_bh(&idev->lock);
2037 for (ifp = idev->addr_list; ifp;
2038 ifp = ifp->if_next) {
2039 if (ifp->scope == IFA_LINK
2040 && !(ifp->
2041 flags & IFA_F_TENTATIVE)) {
2042 ipv6_addr_copy(&pkt_dev->
2043 cur_in6_saddr,
2044 &ifp->addr);
2045 err = 0;
2046 break;
2047 }
2048 }
2049 read_unlock_bh(&idev->lock);
2050 }
2051 rcu_read_unlock();
2052 if (err)
2053 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2054 "address not availble.\n");
2055 }
2056 #endif
2057 } else {
2058 pkt_dev->saddr_min = 0;
2059 pkt_dev->saddr_max = 0;
2060 if (strlen(pkt_dev->src_min) == 0) {
2061
2062 struct in_device *in_dev;
2063
2064 rcu_read_lock();
2065 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2066 if (in_dev) {
2067 if (in_dev->ifa_list) {
2068 pkt_dev->saddr_min =
2069 in_dev->ifa_list->ifa_address;
2070 pkt_dev->saddr_max = pkt_dev->saddr_min;
2071 }
2072 }
2073 rcu_read_unlock();
2074 } else {
2075 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2076 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2077 }
2078
2079 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2080 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2081 }
2082 /* Initialize current values. */
2083 pkt_dev->cur_dst_mac_offset = 0;
2084 pkt_dev->cur_src_mac_offset = 0;
2085 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2086 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2087 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2088 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2089 pkt_dev->nflows = 0;
2090 }
2091
2092 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2093 {
2094 __u64 start;
2095 __u64 now;
2096
2097 start = now = getCurUs();
2098 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2099 while (now < spin_until_us) {
2100 /* TODO: optimize sleeping behavior */
2101 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2102 schedule_timeout_interruptible(1);
2103 else if (spin_until_us - now > 100) {
2104 do_softirq();
2105 if (!pkt_dev->running)
2106 return;
2107 if (need_resched())
2108 schedule();
2109 }
2110
2111 now = getCurUs();
2112 }
2113
2114 pkt_dev->idle_acc += now - start;
2115 }
2116
2117 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2118 {
2119 pkt_dev->pkt_overhead = 0;
2120 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2121 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2122 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2123 }
2124
2125 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2126 {
2127
2128 if (pkt_dev->flows[flow].flags & F_INIT)
2129 return 1;
2130 else
2131 return 0;
2132 }
2133
2134 static inline int f_pick(struct pktgen_dev *pkt_dev)
2135 {
2136 int flow = pkt_dev->curfl;
2137
2138 if (pkt_dev->flags & F_FLOW_SEQ) {
2139 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2140 /* reset time */
2141 pkt_dev->flows[flow].count = 0;
2142 pkt_dev->curfl += 1;
2143 if (pkt_dev->curfl >= pkt_dev->cflows)
2144 pkt_dev->curfl = 0; /*reset */
2145 }
2146 } else {
2147 flow = random32() % pkt_dev->cflows;
2148
2149 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2150 pkt_dev->flows[flow].count = 0;
2151 }
2152
2153 return pkt_dev->curfl;
2154 }
2155
2156
2157 #ifdef CONFIG_XFRM
2158 /* If there was already an IPSEC SA, we keep it as is, else
2159 * we go look for it ...
2160 */
2161 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2162 {
2163 struct xfrm_state *x = pkt_dev->flows[flow].x;
2164 if (!x) {
2165 /*slow path: we dont already have xfrm_state*/
2166 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2167 (xfrm_address_t *)&pkt_dev->cur_saddr,
2168 AF_INET,
2169 pkt_dev->ipsmode,
2170 pkt_dev->ipsproto, 0);
2171 if (x) {
2172 pkt_dev->flows[flow].x = x;
2173 set_pkt_overhead(pkt_dev);
2174 pkt_dev->pkt_overhead+=x->props.header_len;
2175 }
2176
2177 }
2178 }
2179 #endif
2180 /* Increment/randomize headers according to flags and current values
2181 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2182 */
2183 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2184 {
2185 __u32 imn;
2186 __u32 imx;
2187 int flow = 0;
2188
2189 if (pkt_dev->cflows)
2190 flow = f_pick(pkt_dev);
2191
2192 /* Deal with source MAC */
2193 if (pkt_dev->src_mac_count > 1) {
2194 __u32 mc;
2195 __u32 tmp;
2196
2197 if (pkt_dev->flags & F_MACSRC_RND)
2198 mc = random32() % pkt_dev->src_mac_count;
2199 else {
2200 mc = pkt_dev->cur_src_mac_offset++;
2201 if (pkt_dev->cur_src_mac_offset >
2202 pkt_dev->src_mac_count)
2203 pkt_dev->cur_src_mac_offset = 0;
2204 }
2205
2206 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2207 pkt_dev->hh[11] = tmp;
2208 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2209 pkt_dev->hh[10] = tmp;
2210 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2211 pkt_dev->hh[9] = tmp;
2212 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2213 pkt_dev->hh[8] = tmp;
2214 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2215 pkt_dev->hh[7] = tmp;
2216 }
2217
2218 /* Deal with Destination MAC */
2219 if (pkt_dev->dst_mac_count > 1) {
2220 __u32 mc;
2221 __u32 tmp;
2222
2223 if (pkt_dev->flags & F_MACDST_RND)
2224 mc = random32() % pkt_dev->dst_mac_count;
2225
2226 else {
2227 mc = pkt_dev->cur_dst_mac_offset++;
2228 if (pkt_dev->cur_dst_mac_offset >
2229 pkt_dev->dst_mac_count) {
2230 pkt_dev->cur_dst_mac_offset = 0;
2231 }
2232 }
2233
2234 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2235 pkt_dev->hh[5] = tmp;
2236 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2237 pkt_dev->hh[4] = tmp;
2238 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2239 pkt_dev->hh[3] = tmp;
2240 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2241 pkt_dev->hh[2] = tmp;
2242 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2243 pkt_dev->hh[1] = tmp;
2244 }
2245
2246 if (pkt_dev->flags & F_MPLS_RND) {
2247 unsigned i;
2248 for (i = 0; i < pkt_dev->nr_labels; i++)
2249 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2250 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2251 ((__force __be32)random32() &
2252 htonl(0x000fffff));
2253 }
2254
2255 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2256 pkt_dev->vlan_id = random32() & (4096-1);
2257 }
2258
2259 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2260 pkt_dev->svlan_id = random32() & (4096 - 1);
2261 }
2262
2263 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2264 if (pkt_dev->flags & F_UDPSRC_RND)
2265 pkt_dev->cur_udp_src = random32() %
2266 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2267 + pkt_dev->udp_src_min;
2268
2269 else {
2270 pkt_dev->cur_udp_src++;
2271 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2272 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2273 }
2274 }
2275
2276 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2277 if (pkt_dev->flags & F_UDPDST_RND) {
2278 pkt_dev->cur_udp_dst = random32() %
2279 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2280 + pkt_dev->udp_dst_min;
2281 } else {
2282 pkt_dev->cur_udp_dst++;
2283 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2284 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2285 }
2286 }
2287
2288 if (!(pkt_dev->flags & F_IPV6)) {
2289
2290 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2291 ntohl(pkt_dev->
2292 saddr_max))) {
2293 __u32 t;
2294 if (pkt_dev->flags & F_IPSRC_RND)
2295 t = random32() % (imx - imn) + imn;
2296 else {
2297 t = ntohl(pkt_dev->cur_saddr);
2298 t++;
2299 if (t > imx) {
2300 t = imn;
2301 }
2302 }
2303 pkt_dev->cur_saddr = htonl(t);
2304 }
2305
2306 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2307 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2308 } else {
2309 imn = ntohl(pkt_dev->daddr_min);
2310 imx = ntohl(pkt_dev->daddr_max);
2311 if (imn < imx) {
2312 __u32 t;
2313 __be32 s;
2314 if (pkt_dev->flags & F_IPDST_RND) {
2315
2316 t = random32() % (imx - imn) + imn;
2317 s = htonl(t);
2318
2319 while (LOOPBACK(s) || MULTICAST(s)
2320 || BADCLASS(s) || ZERONET(s)
2321 || LOCAL_MCAST(s)) {
2322 t = random32() % (imx - imn) + imn;
2323 s = htonl(t);
2324 }
2325 pkt_dev->cur_daddr = s;
2326 } else {
2327 t = ntohl(pkt_dev->cur_daddr);
2328 t++;
2329 if (t > imx) {
2330 t = imn;
2331 }
2332 pkt_dev->cur_daddr = htonl(t);
2333 }
2334 }
2335 if (pkt_dev->cflows) {
2336 pkt_dev->flows[flow].flags |= F_INIT;
2337 pkt_dev->flows[flow].cur_daddr =
2338 pkt_dev->cur_daddr;
2339 #ifdef CONFIG_XFRM
2340 if (pkt_dev->flags & F_IPSEC_ON)
2341 get_ipsec_sa(pkt_dev, flow);
2342 #endif
2343 pkt_dev->nflows++;
2344 }
2345 }
2346 } else { /* IPV6 * */
2347
2348 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2349 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2350 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2351 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2352 else {
2353 int i;
2354
2355 /* Only random destinations yet */
2356
2357 for (i = 0; i < 4; i++) {
2358 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2359 (((__force __be32)random32() |
2360 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2361 pkt_dev->max_in6_daddr.s6_addr32[i]);
2362 }
2363 }
2364 }
2365
2366 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2367 __u32 t;
2368 if (pkt_dev->flags & F_TXSIZE_RND) {
2369 t = random32() %
2370 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2371 + pkt_dev->min_pkt_size;
2372 } else {
2373 t = pkt_dev->cur_pkt_size + 1;
2374 if (t > pkt_dev->max_pkt_size)
2375 t = pkt_dev->min_pkt_size;
2376 }
2377 pkt_dev->cur_pkt_size = t;
2378 }
2379
2380 pkt_dev->flows[flow].count++;
2381 }
2382
2383
2384 #ifdef CONFIG_XFRM
2385 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2386 {
2387 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2388 int err = 0;
2389 struct iphdr *iph;
2390
2391 if (!x)
2392 return 0;
2393 /* XXX: we dont support tunnel mode for now until
2394 * we resolve the dst issue */
2395 if (x->props.mode != XFRM_MODE_TRANSPORT)
2396 return 0;
2397
2398 spin_lock(&x->lock);
2399 iph = ip_hdr(skb);
2400
2401 err = x->mode->output(x, skb);
2402 if (err)
2403 goto error;
2404 err = x->type->output(x, skb);
2405 if (err)
2406 goto error;
2407
2408 x->curlft.bytes +=skb->len;
2409 x->curlft.packets++;
2410 spin_unlock(&x->lock);
2411
2412 error:
2413 spin_unlock(&x->lock);
2414 return err;
2415 }
2416
2417 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2418 {
2419 if (pkt_dev->cflows) {
2420 /* let go of the SAs if we have them */
2421 int i = 0;
2422 for (; i < pkt_dev->nflows; i++){
2423 struct xfrm_state *x = pkt_dev->flows[i].x;
2424 if (x) {
2425 xfrm_state_put(x);
2426 pkt_dev->flows[i].x = NULL;
2427 }
2428 }
2429 }
2430 }
2431
2432 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2433 struct sk_buff *skb, __be16 protocol)
2434 {
2435 if (pkt_dev->flags & F_IPSEC_ON) {
2436 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2437 int nhead = 0;
2438 if (x) {
2439 int ret;
2440 __u8 *eth;
2441 nhead = x->props.header_len - skb_headroom(skb);
2442 if (nhead >0) {
2443 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2444 if (ret < 0) {
2445 printk(KERN_ERR "Error expanding "
2446 "ipsec packet %d\n",ret);
2447 return 0;
2448 }
2449 }
2450
2451 /* ipsec is not expecting ll header */
2452 skb_pull(skb, ETH_HLEN);
2453 ret = pktgen_output_ipsec(skb, pkt_dev);
2454 if (ret) {
2455 printk(KERN_ERR "Error creating ipsec "
2456 "packet %d\n",ret);
2457 kfree_skb(skb);
2458 return 0;
2459 }
2460 /* restore ll */
2461 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2462 memcpy(eth, pkt_dev->hh, 12);
2463 *(u16 *) & eth[12] = protocol;
2464 }
2465 }
2466 return 1;
2467 }
2468 #endif
2469
2470 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2471 {
2472 unsigned i;
2473 for (i = 0; i < pkt_dev->nr_labels; i++) {
2474 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2475 }
2476 mpls--;
2477 *mpls |= MPLS_STACK_BOTTOM;
2478 }
2479
2480 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2481 unsigned int prio)
2482 {
2483 return htons(id | (cfi << 12) | (prio << 13));
2484 }
2485
2486 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2487 struct pktgen_dev *pkt_dev)
2488 {
2489 struct sk_buff *skb = NULL;
2490 __u8 *eth;
2491 struct udphdr *udph;
2492 int datalen, iplen;
2493 struct iphdr *iph;
2494 struct pktgen_hdr *pgh = NULL;
2495 __be16 protocol = htons(ETH_P_IP);
2496 __be32 *mpls;
2497 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2498 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2499 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2500 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2501
2502
2503 if (pkt_dev->nr_labels)
2504 protocol = htons(ETH_P_MPLS_UC);
2505
2506 if (pkt_dev->vlan_id != 0xffff)
2507 protocol = htons(ETH_P_8021Q);
2508
2509 /* Update any of the values, used when we're incrementing various
2510 * fields.
2511 */
2512 mod_cur_headers(pkt_dev);
2513
2514 datalen = (odev->hard_header_len + 16) & ~0xf;
2515 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2516 pkt_dev->pkt_overhead, GFP_ATOMIC);
2517 if (!skb) {
2518 sprintf(pkt_dev->result, "No memory");
2519 return NULL;
2520 }
2521
2522 skb_reserve(skb, datalen);
2523
2524 /* Reserve for ethernet and IP header */
2525 eth = (__u8 *) skb_push(skb, 14);
2526 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2527 if (pkt_dev->nr_labels)
2528 mpls_push(mpls, pkt_dev);
2529
2530 if (pkt_dev->vlan_id != 0xffff) {
2531 if (pkt_dev->svlan_id != 0xffff) {
2532 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2533 *svlan_tci = build_tci(pkt_dev->svlan_id,
2534 pkt_dev->svlan_cfi,
2535 pkt_dev->svlan_p);
2536 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2537 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2538 }
2539 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2540 *vlan_tci = build_tci(pkt_dev->vlan_id,
2541 pkt_dev->vlan_cfi,
2542 pkt_dev->vlan_p);
2543 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2544 *vlan_encapsulated_proto = htons(ETH_P_IP);
2545 }
2546
2547 skb->network_header = skb->tail;
2548 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2549 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2550
2551 iph = ip_hdr(skb);
2552 udph = udp_hdr(skb);
2553
2554 memcpy(eth, pkt_dev->hh, 12);
2555 *(__be16 *) & eth[12] = protocol;
2556
2557 /* Eth + IPh + UDPh + mpls */
2558 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2559 pkt_dev->pkt_overhead;
2560 if (datalen < sizeof(struct pktgen_hdr))
2561 datalen = sizeof(struct pktgen_hdr);
2562
2563 udph->source = htons(pkt_dev->cur_udp_src);
2564 udph->dest = htons(pkt_dev->cur_udp_dst);
2565 udph->len = htons(datalen + 8); /* DATA + udphdr */
2566 udph->check = 0; /* No checksum */
2567
2568 iph->ihl = 5;
2569 iph->version = 4;
2570 iph->ttl = 32;
2571 iph->tos = pkt_dev->tos;
2572 iph->protocol = IPPROTO_UDP; /* UDP */
2573 iph->saddr = pkt_dev->cur_saddr;
2574 iph->daddr = pkt_dev->cur_daddr;
2575 iph->frag_off = 0;
2576 iplen = 20 + 8 + datalen;
2577 iph->tot_len = htons(iplen);
2578 iph->check = 0;
2579 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2580 skb->protocol = protocol;
2581 skb->mac_header = (skb->network_header - ETH_HLEN -
2582 pkt_dev->pkt_overhead);
2583 skb->dev = odev;
2584 skb->pkt_type = PACKET_HOST;
2585
2586 if (pkt_dev->nfrags <= 0)
2587 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2588 else {
2589 int frags = pkt_dev->nfrags;
2590 int i;
2591
2592 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2593
2594 if (frags > MAX_SKB_FRAGS)
2595 frags = MAX_SKB_FRAGS;
2596 if (datalen > frags * PAGE_SIZE) {
2597 skb_put(skb, datalen - frags * PAGE_SIZE);
2598 datalen = frags * PAGE_SIZE;
2599 }
2600
2601 i = 0;
2602 while (datalen > 0) {
2603 struct page *page = alloc_pages(GFP_KERNEL, 0);
2604 skb_shinfo(skb)->frags[i].page = page;
2605 skb_shinfo(skb)->frags[i].page_offset = 0;
2606 skb_shinfo(skb)->frags[i].size =
2607 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2608 datalen -= skb_shinfo(skb)->frags[i].size;
2609 skb->len += skb_shinfo(skb)->frags[i].size;
2610 skb->data_len += skb_shinfo(skb)->frags[i].size;
2611 i++;
2612 skb_shinfo(skb)->nr_frags = i;
2613 }
2614
2615 while (i < frags) {
2616 int rem;
2617
2618 if (i == 0)
2619 break;
2620
2621 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2622 if (rem == 0)
2623 break;
2624
2625 skb_shinfo(skb)->frags[i - 1].size -= rem;
2626
2627 skb_shinfo(skb)->frags[i] =
2628 skb_shinfo(skb)->frags[i - 1];
2629 get_page(skb_shinfo(skb)->frags[i].page);
2630 skb_shinfo(skb)->frags[i].page =
2631 skb_shinfo(skb)->frags[i - 1].page;
2632 skb_shinfo(skb)->frags[i].page_offset +=
2633 skb_shinfo(skb)->frags[i - 1].size;
2634 skb_shinfo(skb)->frags[i].size = rem;
2635 i++;
2636 skb_shinfo(skb)->nr_frags = i;
2637 }
2638 }
2639
2640 /* Stamp the time, and sequence number, convert them to network byte order */
2641
2642 if (pgh) {
2643 struct timeval timestamp;
2644
2645 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2646 pgh->seq_num = htonl(pkt_dev->seq_num);
2647
2648 do_gettimeofday(&timestamp);
2649 pgh->tv_sec = htonl(timestamp.tv_sec);
2650 pgh->tv_usec = htonl(timestamp.tv_usec);
2651 }
2652
2653 #ifdef CONFIG_XFRM
2654 if (!process_ipsec(pkt_dev, skb, protocol))
2655 return NULL;
2656 #endif
2657
2658 return skb;
2659 }
2660
2661 /*
2662 * scan_ip6, fmt_ip taken from dietlibc-0.21
2663 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2664 *
2665 * Slightly modified for kernel.
2666 * Should be candidate for net/ipv4/utils.c
2667 * --ro
2668 */
2669
2670 static unsigned int scan_ip6(const char *s, char ip[16])
2671 {
2672 unsigned int i;
2673 unsigned int len = 0;
2674 unsigned long u;
2675 char suffix[16];
2676 unsigned int prefixlen = 0;
2677 unsigned int suffixlen = 0;
2678 __be32 tmp;
2679
2680 for (i = 0; i < 16; i++)
2681 ip[i] = 0;
2682
2683 for (;;) {
2684 if (*s == ':') {
2685 len++;
2686 if (s[1] == ':') { /* Found "::", skip to part 2 */
2687 s += 2;
2688 len++;
2689 break;
2690 }
2691 s++;
2692 }
2693 {
2694 char *tmp;
2695 u = simple_strtoul(s, &tmp, 16);
2696 i = tmp - s;
2697 }
2698
2699 if (!i)
2700 return 0;
2701 if (prefixlen == 12 && s[i] == '.') {
2702
2703 /* the last 4 bytes may be written as IPv4 address */
2704
2705 tmp = in_aton(s);
2706 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2707 return i + len;
2708 }
2709 ip[prefixlen++] = (u >> 8);
2710 ip[prefixlen++] = (u & 255);
2711 s += i;
2712 len += i;
2713 if (prefixlen == 16)
2714 return len;
2715 }
2716
2717 /* part 2, after "::" */
2718 for (;;) {
2719 if (*s == ':') {
2720 if (suffixlen == 0)
2721 break;
2722 s++;
2723 len++;
2724 } else if (suffixlen != 0)
2725 break;
2726 {
2727 char *tmp;
2728 u = simple_strtol(s, &tmp, 16);
2729 i = tmp - s;
2730 }
2731 if (!i) {
2732 if (*s)
2733 len--;
2734 break;
2735 }
2736 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2737 tmp = in_aton(s);
2738 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2739 sizeof(tmp));
2740 suffixlen += 4;
2741 len += strlen(s);
2742 break;
2743 }
2744 suffix[suffixlen++] = (u >> 8);
2745 suffix[suffixlen++] = (u & 255);
2746 s += i;
2747 len += i;
2748 if (prefixlen + suffixlen == 16)
2749 break;
2750 }
2751 for (i = 0; i < suffixlen; i++)
2752 ip[16 - suffixlen + i] = suffix[i];
2753 return len;
2754 }
2755
2756 static char tohex(char hexdigit)
2757 {
2758 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2759 }
2760
2761 static int fmt_xlong(char *s, unsigned int i)
2762 {
2763 char *bak = s;
2764 *s = tohex((i >> 12) & 0xf);
2765 if (s != bak || *s != '0')
2766 ++s;
2767 *s = tohex((i >> 8) & 0xf);
2768 if (s != bak || *s != '0')
2769 ++s;
2770 *s = tohex((i >> 4) & 0xf);
2771 if (s != bak || *s != '0')
2772 ++s;
2773 *s = tohex(i & 0xf);
2774 return s - bak + 1;
2775 }
2776
2777 static unsigned int fmt_ip6(char *s, const char ip[16])
2778 {
2779 unsigned int len;
2780 unsigned int i;
2781 unsigned int temp;
2782 unsigned int compressing;
2783 int j;
2784
2785 len = 0;
2786 compressing = 0;
2787 for (j = 0; j < 16; j += 2) {
2788
2789 #ifdef V4MAPPEDPREFIX
2790 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2791 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2792 temp = strlen(s);
2793 return len + temp;
2794 }
2795 #endif
2796 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2797 (unsigned long)(unsigned char)ip[j + 1];
2798 if (temp == 0) {
2799 if (!compressing) {
2800 compressing = 1;
2801 if (j == 0) {
2802 *s++ = ':';
2803 ++len;
2804 }
2805 }
2806 } else {
2807 if (compressing) {
2808 compressing = 0;
2809 *s++ = ':';
2810 ++len;
2811 }
2812 i = fmt_xlong(s, temp);
2813 len += i;
2814 s += i;
2815 if (j < 14) {
2816 *s++ = ':';
2817 ++len;
2818 }
2819 }
2820 }
2821 if (compressing) {
2822 *s++ = ':';
2823 ++len;
2824 }
2825 *s = 0;
2826 return len;
2827 }
2828
2829 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2830 struct pktgen_dev *pkt_dev)
2831 {
2832 struct sk_buff *skb = NULL;
2833 __u8 *eth;
2834 struct udphdr *udph;
2835 int datalen;
2836 struct ipv6hdr *iph;
2837 struct pktgen_hdr *pgh = NULL;
2838 __be16 protocol = htons(ETH_P_IPV6);
2839 __be32 *mpls;
2840 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2841 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2842 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2843 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2844
2845 if (pkt_dev->nr_labels)
2846 protocol = htons(ETH_P_MPLS_UC);
2847
2848 if (pkt_dev->vlan_id != 0xffff)
2849 protocol = htons(ETH_P_8021Q);
2850
2851 /* Update any of the values, used when we're incrementing various
2852 * fields.
2853 */
2854 mod_cur_headers(pkt_dev);
2855
2856 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2857 pkt_dev->pkt_overhead, GFP_ATOMIC);
2858 if (!skb) {
2859 sprintf(pkt_dev->result, "No memory");
2860 return NULL;
2861 }
2862
2863 skb_reserve(skb, 16);
2864
2865 /* Reserve for ethernet and IP header */
2866 eth = (__u8 *) skb_push(skb, 14);
2867 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2868 if (pkt_dev->nr_labels)
2869 mpls_push(mpls, pkt_dev);
2870
2871 if (pkt_dev->vlan_id != 0xffff) {
2872 if (pkt_dev->svlan_id != 0xffff) {
2873 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2874 *svlan_tci = build_tci(pkt_dev->svlan_id,
2875 pkt_dev->svlan_cfi,
2876 pkt_dev->svlan_p);
2877 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2878 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2879 }
2880 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2881 *vlan_tci = build_tci(pkt_dev->vlan_id,
2882 pkt_dev->vlan_cfi,
2883 pkt_dev->vlan_p);
2884 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2885 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2886 }
2887
2888 skb->network_header = skb->tail;
2889 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2890 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2891
2892 iph = ipv6_hdr(skb);
2893 udph = udp_hdr(skb);
2894
2895 memcpy(eth, pkt_dev->hh, 12);
2896 *(__be16 *) & eth[12] = protocol;
2897
2898 /* Eth + IPh + UDPh + mpls */
2899 datalen = pkt_dev->cur_pkt_size - 14 -
2900 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2901 pkt_dev->pkt_overhead;
2902
2903 if (datalen < sizeof(struct pktgen_hdr)) {
2904 datalen = sizeof(struct pktgen_hdr);
2905 if (net_ratelimit())
2906 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2907 datalen);
2908 }
2909
2910 udph->source = htons(pkt_dev->cur_udp_src);
2911 udph->dest = htons(pkt_dev->cur_udp_dst);
2912 udph->len = htons(datalen + sizeof(struct udphdr));
2913 udph->check = 0; /* No checksum */
2914
2915 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2916
2917 if (pkt_dev->traffic_class) {
2918 /* Version + traffic class + flow (0) */
2919 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2920 }
2921
2922 iph->hop_limit = 32;
2923
2924 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2925 iph->nexthdr = IPPROTO_UDP;
2926
2927 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2928 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2929
2930 skb->mac_header = (skb->network_header - ETH_HLEN -
2931 pkt_dev->pkt_overhead);
2932 skb->protocol = protocol;
2933 skb->dev = odev;
2934 skb->pkt_type = PACKET_HOST;
2935
2936 if (pkt_dev->nfrags <= 0)
2937 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2938 else {
2939 int frags = pkt_dev->nfrags;
2940 int i;
2941
2942 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2943
2944 if (frags > MAX_SKB_FRAGS)
2945 frags = MAX_SKB_FRAGS;
2946 if (datalen > frags * PAGE_SIZE) {
2947 skb_put(skb, datalen - frags * PAGE_SIZE);
2948 datalen = frags * PAGE_SIZE;
2949 }
2950
2951 i = 0;
2952 while (datalen > 0) {
2953 struct page *page = alloc_pages(GFP_KERNEL, 0);
2954 skb_shinfo(skb)->frags[i].page = page;
2955 skb_shinfo(skb)->frags[i].page_offset = 0;
2956 skb_shinfo(skb)->frags[i].size =
2957 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2958 datalen -= skb_shinfo(skb)->frags[i].size;
2959 skb->len += skb_shinfo(skb)->frags[i].size;
2960 skb->data_len += skb_shinfo(skb)->frags[i].size;
2961 i++;
2962 skb_shinfo(skb)->nr_frags = i;
2963 }
2964
2965 while (i < frags) {
2966 int rem;
2967
2968 if (i == 0)
2969 break;
2970
2971 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2972 if (rem == 0)
2973 break;
2974
2975 skb_shinfo(skb)->frags[i - 1].size -= rem;
2976
2977 skb_shinfo(skb)->frags[i] =
2978 skb_shinfo(skb)->frags[i - 1];
2979 get_page(skb_shinfo(skb)->frags[i].page);
2980 skb_shinfo(skb)->frags[i].page =
2981 skb_shinfo(skb)->frags[i - 1].page;
2982 skb_shinfo(skb)->frags[i].page_offset +=
2983 skb_shinfo(skb)->frags[i - 1].size;
2984 skb_shinfo(skb)->frags[i].size = rem;
2985 i++;
2986 skb_shinfo(skb)->nr_frags = i;
2987 }
2988 }
2989
2990 /* Stamp the time, and sequence number, convert them to network byte order */
2991 /* should we update cloned packets too ? */
2992 if (pgh) {
2993 struct timeval timestamp;
2994
2995 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2996 pgh->seq_num = htonl(pkt_dev->seq_num);
2997
2998 do_gettimeofday(&timestamp);
2999 pgh->tv_sec = htonl(timestamp.tv_sec);
3000 pgh->tv_usec = htonl(timestamp.tv_usec);
3001 }
3002 /* pkt_dev->seq_num++; FF: you really mean this? */
3003
3004 return skb;
3005 }
3006
3007 static inline struct sk_buff *fill_packet(struct net_device *odev,
3008 struct pktgen_dev *pkt_dev)
3009 {
3010 if (pkt_dev->flags & F_IPV6)
3011 return fill_packet_ipv6(odev, pkt_dev);
3012 else
3013 return fill_packet_ipv4(odev, pkt_dev);
3014 }
3015
3016 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3017 {
3018 pkt_dev->seq_num = 1;
3019 pkt_dev->idle_acc = 0;
3020 pkt_dev->sofar = 0;
3021 pkt_dev->tx_bytes = 0;
3022 pkt_dev->errors = 0;
3023 }
3024
3025 /* Set up structure for sending pkts, clear counters */
3026
3027 static void pktgen_run(struct pktgen_thread *t)
3028 {
3029 struct pktgen_dev *pkt_dev;
3030 int started = 0;
3031
3032 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3033
3034 if_lock(t);
3035 list_for_each_entry(pkt_dev, &t->if_list, list) {
3036
3037 /*
3038 * setup odev and create initial packet.
3039 */
3040 pktgen_setup_inject(pkt_dev);
3041
3042 if (pkt_dev->odev) {
3043 pktgen_clear_counters(pkt_dev);
3044 pkt_dev->running = 1; /* Cranke yeself! */
3045 pkt_dev->skb = NULL;
3046 pkt_dev->started_at = getCurUs();
3047 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
3048 pkt_dev->next_tx_ns = 0;
3049 set_pkt_overhead(pkt_dev);
3050
3051 strcpy(pkt_dev->result, "Starting");
3052 started++;
3053 } else
3054 strcpy(pkt_dev->result, "Error starting");
3055 }
3056 if_unlock(t);
3057 if (started)
3058 t->control &= ~(T_STOP);
3059 }
3060
3061 static void pktgen_stop_all_threads_ifs(void)
3062 {
3063 struct pktgen_thread *t;
3064
3065 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3066
3067 mutex_lock(&pktgen_thread_lock);
3068
3069 list_for_each_entry(t, &pktgen_threads, th_list)
3070 t->control |= T_STOP;
3071
3072 mutex_unlock(&pktgen_thread_lock);
3073 }
3074
3075 static int thread_is_running(struct pktgen_thread *t)
3076 {
3077 struct pktgen_dev *pkt_dev;
3078 int res = 0;
3079
3080 list_for_each_entry(pkt_dev, &t->if_list, list)
3081 if (pkt_dev->running) {
3082 res = 1;
3083 break;
3084 }
3085 return res;
3086 }
3087
3088 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3089 {
3090 if_lock(t);
3091
3092 while (thread_is_running(t)) {
3093
3094 if_unlock(t);
3095
3096 msleep_interruptible(100);
3097
3098 if (signal_pending(current))
3099 goto signal;
3100 if_lock(t);
3101 }
3102 if_unlock(t);
3103 return 1;
3104 signal:
3105 return 0;
3106 }
3107
3108 static int pktgen_wait_all_threads_run(void)
3109 {
3110 struct pktgen_thread *t;
3111 int sig = 1;
3112
3113 mutex_lock(&pktgen_thread_lock);
3114
3115 list_for_each_entry(t, &pktgen_threads, th_list) {
3116 sig = pktgen_wait_thread_run(t);
3117 if (sig == 0)
3118 break;
3119 }
3120
3121 if (sig == 0)
3122 list_for_each_entry(t, &pktgen_threads, th_list)
3123 t->control |= (T_STOP);
3124
3125 mutex_unlock(&pktgen_thread_lock);
3126 return sig;
3127 }
3128
3129 static void pktgen_run_all_threads(void)
3130 {
3131 struct pktgen_thread *t;
3132
3133 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3134
3135 mutex_lock(&pktgen_thread_lock);
3136
3137 list_for_each_entry(t, &pktgen_threads, th_list)
3138 t->control |= (T_RUN);
3139
3140 mutex_unlock(&pktgen_thread_lock);
3141
3142 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3143
3144 pktgen_wait_all_threads_run();
3145 }
3146
3147 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3148 {
3149 __u64 total_us, bps, mbps, pps, idle;
3150 char *p = pkt_dev->result;
3151
3152 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3153
3154 idle = pkt_dev->idle_acc;
3155
3156 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3157 (unsigned long long)total_us,
3158 (unsigned long long)(total_us - idle),
3159 (unsigned long long)idle,
3160 (unsigned long long)pkt_dev->sofar,
3161 pkt_dev->cur_pkt_size, nr_frags);
3162
3163 pps = pkt_dev->sofar * USEC_PER_SEC;
3164
3165 while ((total_us >> 32) != 0) {
3166 pps >>= 1;
3167 total_us >>= 1;
3168 }
3169
3170 do_div(pps, total_us);
3171
3172 bps = pps * 8 * pkt_dev->cur_pkt_size;
3173
3174 mbps = bps;
3175 do_div(mbps, 1000000);
3176 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3177 (unsigned long long)pps,
3178 (unsigned long long)mbps,
3179 (unsigned long long)bps,
3180 (unsigned long long)pkt_dev->errors);
3181 }
3182
3183 /* Set stopped-at timer, remove from running list, do counters & statistics */
3184
3185 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3186 {
3187 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3188
3189 if (!pkt_dev->running) {
3190 printk(KERN_WARNING "pktgen: interface: %s is already "
3191 "stopped\n", pkt_dev->odev->name);
3192 return -EINVAL;
3193 }
3194
3195 pkt_dev->stopped_at = getCurUs();
3196 pkt_dev->running = 0;
3197
3198 show_results(pkt_dev, nr_frags);
3199
3200 return 0;
3201 }
3202
3203 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3204 {
3205 struct pktgen_dev *pkt_dev, *best = NULL;
3206
3207 if_lock(t);
3208
3209 list_for_each_entry(pkt_dev, &t->if_list, list) {
3210 if (!pkt_dev->running)
3211 continue;
3212 if (best == NULL)
3213 best = pkt_dev;
3214 else if (pkt_dev->next_tx_us < best->next_tx_us)
3215 best = pkt_dev;
3216 }
3217 if_unlock(t);
3218 return best;
3219 }
3220
3221 static void pktgen_stop(struct pktgen_thread *t)
3222 {
3223 struct pktgen_dev *pkt_dev;
3224
3225 pr_debug("pktgen: entering pktgen_stop\n");
3226
3227 if_lock(t);
3228
3229 list_for_each_entry(pkt_dev, &t->if_list, list) {
3230 pktgen_stop_device(pkt_dev);
3231 if (pkt_dev->skb)
3232 kfree_skb(pkt_dev->skb);
3233
3234 pkt_dev->skb = NULL;
3235 }
3236
3237 if_unlock(t);
3238 }
3239
3240 /*
3241 * one of our devices needs to be removed - find it
3242 * and remove it
3243 */
3244 static void pktgen_rem_one_if(struct pktgen_thread *t)
3245 {
3246 struct list_head *q, *n;
3247 struct pktgen_dev *cur;
3248
3249 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3250
3251 if_lock(t);
3252
3253 list_for_each_safe(q, n, &t->if_list) {
3254 cur = list_entry(q, struct pktgen_dev, list);
3255
3256 if (!cur->removal_mark)
3257 continue;
3258
3259 if (cur->skb)
3260 kfree_skb(cur->skb);
3261 cur->skb = NULL;
3262
3263 pktgen_remove_device(t, cur);
3264
3265 break;
3266 }
3267
3268 if_unlock(t);
3269 }
3270
3271 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3272 {
3273 struct list_head *q, *n;
3274 struct pktgen_dev *cur;
3275
3276 /* Remove all devices, free mem */
3277
3278 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3279 if_lock(t);
3280
3281 list_for_each_safe(q, n, &t->if_list) {
3282 cur = list_entry(q, struct pktgen_dev, list);
3283
3284 if (cur->skb)
3285 kfree_skb(cur->skb);
3286 cur->skb = NULL;
3287
3288 pktgen_remove_device(t, cur);
3289 }
3290
3291 if_unlock(t);
3292 }
3293
3294 static void pktgen_rem_thread(struct pktgen_thread *t)
3295 {
3296 /* Remove from the thread list */
3297
3298 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3299
3300 mutex_lock(&pktgen_thread_lock);
3301
3302 list_del(&t->th_list);
3303
3304 mutex_unlock(&pktgen_thread_lock);
3305 }
3306
3307 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3308 {
3309 struct net_device *odev = NULL;
3310 __u64 idle_start = 0;
3311 int ret;
3312
3313 odev = pkt_dev->odev;
3314
3315 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3316 u64 now;
3317
3318 now = getCurUs();
3319 if (now < pkt_dev->next_tx_us)
3320 spin(pkt_dev, pkt_dev->next_tx_us);
3321
3322 /* This is max DELAY, this has special meaning of
3323 * "never transmit"
3324 */
3325 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3326 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3327 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3328 goto out;
3329 }
3330 }
3331
3332 if ((netif_queue_stopped(odev) ||
3333 (pkt_dev->skb &&
3334 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) ||
3335 need_resched()) {
3336 idle_start = getCurUs();
3337
3338 if (!netif_running(odev)) {
3339 pktgen_stop_device(pkt_dev);
3340 if (pkt_dev->skb)
3341 kfree_skb(pkt_dev->skb);
3342 pkt_dev->skb = NULL;
3343 goto out;
3344 }
3345 if (need_resched())
3346 schedule();
3347
3348 pkt_dev->idle_acc += getCurUs() - idle_start;
3349
3350 if (netif_queue_stopped(odev) ||
3351 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3352 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3353 pkt_dev->next_tx_ns = 0;
3354 goto out; /* Try the next interface */
3355 }
3356 }
3357
3358 if (pkt_dev->last_ok || !pkt_dev->skb) {
3359 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3360 || (!pkt_dev->skb)) {
3361 /* build a new pkt */
3362 if (pkt_dev->skb)
3363 kfree_skb(pkt_dev->skb);
3364
3365 pkt_dev->skb = fill_packet(odev, pkt_dev);
3366 if (pkt_dev->skb == NULL) {
3367 printk(KERN_ERR "pktgen: ERROR: couldn't "
3368 "allocate skb in fill_packet.\n");
3369 schedule();
3370 pkt_dev->clone_count--; /* back out increment, OOM */
3371 goto out;
3372 }
3373 pkt_dev->allocated_skbs++;
3374 pkt_dev->clone_count = 0; /* reset counter */
3375 }
3376 }
3377
3378 netif_tx_lock_bh(odev);
3379 if (!netif_queue_stopped(odev) &&
3380 !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3381
3382 atomic_inc(&(pkt_dev->skb->users));
3383 retry_now:
3384 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3385 if (likely(ret == NETDEV_TX_OK)) {
3386 pkt_dev->last_ok = 1;
3387 pkt_dev->sofar++;
3388 pkt_dev->seq_num++;
3389 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3390
3391 } else if (ret == NETDEV_TX_LOCKED
3392 && (odev->features & NETIF_F_LLTX)) {
3393 cpu_relax();
3394 goto retry_now;
3395 } else { /* Retry it next time */
3396
3397 atomic_dec(&(pkt_dev->skb->users));
3398
3399 if (debug && net_ratelimit())
3400 printk(KERN_INFO "pktgen: Hard xmit error\n");
3401
3402 pkt_dev->errors++;
3403 pkt_dev->last_ok = 0;
3404 }
3405
3406 pkt_dev->next_tx_us = getCurUs();
3407 pkt_dev->next_tx_ns = 0;
3408
3409 pkt_dev->next_tx_us += pkt_dev->delay_us;
3410 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3411
3412 if (pkt_dev->next_tx_ns > 1000) {
3413 pkt_dev->next_tx_us++;
3414 pkt_dev->next_tx_ns -= 1000;
3415 }
3416 }
3417
3418 else { /* Retry it next time */
3419 pkt_dev->last_ok = 0;
3420 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3421 pkt_dev->next_tx_ns = 0;
3422 }
3423
3424 netif_tx_unlock_bh(odev);
3425
3426 /* If pkt_dev->count is zero, then run forever */
3427 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3428 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3429 idle_start = getCurUs();
3430 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3431 if (signal_pending(current)) {
3432 break;
3433 }
3434 schedule();
3435 }
3436 pkt_dev->idle_acc += getCurUs() - idle_start;
3437 }
3438
3439 /* Done with this */
3440 pktgen_stop_device(pkt_dev);
3441 if (pkt_dev->skb)
3442 kfree_skb(pkt_dev->skb);
3443 pkt_dev->skb = NULL;
3444 }
3445 out:;
3446 }
3447
3448 /*
3449 * Main loop of the thread goes here
3450 */
3451
3452 static int pktgen_thread_worker(void *arg)
3453 {
3454 DEFINE_WAIT(wait);
3455 struct pktgen_thread *t = arg;
3456 struct pktgen_dev *pkt_dev = NULL;
3457 int cpu = t->cpu;
3458 u32 max_before_softirq;
3459 u32 tx_since_softirq = 0;
3460
3461 BUG_ON(smp_processor_id() != cpu);
3462
3463 init_waitqueue_head(&t->queue);
3464
3465 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
3466
3467 max_before_softirq = t->max_before_softirq;
3468
3469 set_current_state(TASK_INTERRUPTIBLE);
3470
3471 set_freezable();
3472
3473 while (!kthread_should_stop()) {
3474 pkt_dev = next_to_run(t);
3475
3476 if (!pkt_dev &&
3477 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3478 == 0) {
3479 prepare_to_wait(&(t->queue), &wait,
3480 TASK_INTERRUPTIBLE);
3481 schedule_timeout(HZ / 10);
3482 finish_wait(&(t->queue), &wait);
3483 }
3484
3485 __set_current_state(TASK_RUNNING);
3486
3487 if (pkt_dev) {
3488
3489 pktgen_xmit(pkt_dev);
3490
3491 /*
3492 * We like to stay RUNNING but must also give
3493 * others fair share.
3494 */
3495
3496 tx_since_softirq += pkt_dev->last_ok;
3497
3498 if (tx_since_softirq > max_before_softirq) {
3499 if (local_softirq_pending())
3500 do_softirq();
3501 tx_since_softirq = 0;
3502 }
3503 }
3504
3505 if (t->control & T_STOP) {
3506 pktgen_stop(t);
3507 t->control &= ~(T_STOP);
3508 }
3509
3510 if (t->control & T_RUN) {
3511 pktgen_run(t);
3512 t->control &= ~(T_RUN);
3513 }
3514
3515 if (t->control & T_REMDEVALL) {
3516 pktgen_rem_all_ifs(t);
3517 t->control &= ~(T_REMDEVALL);
3518 }
3519
3520 if (t->control & T_REMDEV) {
3521 pktgen_rem_one_if(t);
3522 t->control &= ~(T_REMDEV);
3523 }
3524
3525 try_to_freeze();
3526
3527 set_current_state(TASK_INTERRUPTIBLE);
3528 }
3529
3530 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3531 pktgen_stop(t);
3532
3533 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3534 pktgen_rem_all_ifs(t);
3535
3536 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3537 pktgen_rem_thread(t);
3538
3539 return 0;
3540 }
3541
3542 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3543 const char *ifname)
3544 {
3545 struct pktgen_dev *p, *pkt_dev = NULL;
3546 if_lock(t);
3547
3548 list_for_each_entry(p, &t->if_list, list)
3549 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3550 pkt_dev = p;
3551 break;
3552 }
3553
3554 if_unlock(t);
3555 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3556 return pkt_dev;
3557 }
3558
3559 /*
3560 * Adds a dev at front of if_list.
3561 */
3562
3563 static int add_dev_to_thread(struct pktgen_thread *t,
3564 struct pktgen_dev *pkt_dev)
3565 {
3566 int rv = 0;
3567
3568 if_lock(t);
3569
3570 if (pkt_dev->pg_thread) {
3571 printk(KERN_ERR "pktgen: ERROR: already assigned "
3572 "to a thread.\n");
3573 rv = -EBUSY;
3574 goto out;
3575 }
3576
3577 list_add(&pkt_dev->list, &t->if_list);
3578 pkt_dev->pg_thread = t;
3579 pkt_dev->running = 0;
3580
3581 out:
3582 if_unlock(t);
3583 return rv;
3584 }
3585
3586 /* Called under thread lock */
3587
3588 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3589 {
3590 struct pktgen_dev *pkt_dev;
3591 int err;
3592
3593 /* We don't allow a device to be on several threads */
3594
3595 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3596 if (pkt_dev) {
3597 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3598 return -EBUSY;
3599 }
3600
3601 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3602 if (!pkt_dev)
3603 return -ENOMEM;
3604
3605 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3606 if (pkt_dev->flows == NULL) {
3607 kfree(pkt_dev);
3608 return -ENOMEM;
3609 }
3610 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3611
3612 pkt_dev->removal_mark = 0;
3613 pkt_dev->min_pkt_size = ETH_ZLEN;
3614 pkt_dev->max_pkt_size = ETH_ZLEN;
3615 pkt_dev->nfrags = 0;
3616 pkt_dev->clone_skb = pg_clone_skb_d;
3617 pkt_dev->delay_us = pg_delay_d / 1000;
3618 pkt_dev->delay_ns = pg_delay_d % 1000;
3619 pkt_dev->count = pg_count_d;
3620 pkt_dev->sofar = 0;
3621 pkt_dev->udp_src_min = 9; /* sink port */
3622 pkt_dev->udp_src_max = 9;
3623 pkt_dev->udp_dst_min = 9;
3624 pkt_dev->udp_dst_max = 9;
3625
3626 pkt_dev->vlan_p = 0;
3627 pkt_dev->vlan_cfi = 0;
3628 pkt_dev->vlan_id = 0xffff;
3629 pkt_dev->svlan_p = 0;
3630 pkt_dev->svlan_cfi = 0;
3631 pkt_dev->svlan_id = 0xffff;
3632
3633 err = pktgen_setup_dev(pkt_dev, ifname);
3634 if (err)
3635 goto out1;
3636
3637 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3638 if (!pkt_dev->entry) {
3639 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3640 PG_PROC_DIR, ifname);
3641 err = -EINVAL;
3642 goto out2;
3643 }
3644 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3645 pkt_dev->entry->data = pkt_dev;
3646 #ifdef CONFIG_XFRM
3647 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3648 pkt_dev->ipsproto = IPPROTO_ESP;
3649 #endif
3650
3651 return add_dev_to_thread(t, pkt_dev);
3652 out2:
3653 dev_put(pkt_dev->odev);
3654 out1:
3655 #ifdef CONFIG_XFRM
3656 free_SAs(pkt_dev);
3657 #endif
3658 if (pkt_dev->flows)
3659 vfree(pkt_dev->flows);
3660 kfree(pkt_dev);
3661 return err;
3662 }
3663
3664 static int __init pktgen_create_thread(int cpu)
3665 {
3666 struct pktgen_thread *t;
3667 struct proc_dir_entry *pe;
3668 struct task_struct *p;
3669
3670 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3671 if (!t) {
3672 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3673 "create new thread.\n");
3674 return -ENOMEM;
3675 }
3676
3677 spin_lock_init(&t->if_lock);
3678 t->cpu = cpu;
3679
3680 INIT_LIST_HEAD(&t->if_list);
3681
3682 list_add_tail(&t->th_list, &pktgen_threads);
3683
3684 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3685 if (IS_ERR(p)) {
3686 printk(KERN_ERR "pktgen: kernel_thread() failed "
3687 "for cpu %d\n", t->cpu);
3688 list_del(&t->th_list);
3689 kfree(t);
3690 return PTR_ERR(p);
3691 }
3692 kthread_bind(p, cpu);
3693 t->tsk = p;
3694
3695 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3696 if (!pe) {
3697 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3698 PG_PROC_DIR, t->tsk->comm);
3699 kthread_stop(p);
3700 list_del(&t->th_list);
3701 kfree(t);
3702 return -EINVAL;
3703 }
3704
3705 pe->proc_fops = &pktgen_thread_fops;
3706 pe->data = t;
3707
3708 wake_up_process(p);
3709
3710 return 0;
3711 }
3712
3713 /*
3714 * Removes a device from the thread if_list.
3715 */
3716 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3717 struct pktgen_dev *pkt_dev)
3718 {
3719 struct list_head *q, *n;
3720 struct pktgen_dev *p;
3721
3722 list_for_each_safe(q, n, &t->if_list) {
3723 p = list_entry(q, struct pktgen_dev, list);
3724 if (p == pkt_dev)
3725 list_del(&p->list);
3726 }
3727 }
3728
3729 static int pktgen_remove_device(struct pktgen_thread *t,
3730 struct pktgen_dev *pkt_dev)
3731 {
3732
3733 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3734
3735 if (pkt_dev->running) {
3736 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3737 "running interface, stopping it now.\n");
3738 pktgen_stop_device(pkt_dev);
3739 }
3740
3741 /* Dis-associate from the interface */
3742
3743 if (pkt_dev->odev) {
3744 dev_put(pkt_dev->odev);
3745 pkt_dev->odev = NULL;
3746 }
3747
3748 /* And update the thread if_list */
3749
3750 _rem_dev_from_if_list(t, pkt_dev);
3751
3752 if (pkt_dev->entry)
3753 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3754
3755 #ifdef CONFIG_XFRM
3756 free_SAs(pkt_dev);
3757 #endif
3758 if (pkt_dev->flows)
3759 vfree(pkt_dev->flows);
3760 kfree(pkt_dev);
3761 return 0;
3762 }
3763
3764 static int __init pg_init(void)
3765 {
3766 int cpu;
3767 struct proc_dir_entry *pe;
3768
3769 printk(KERN_INFO "%s", version);
3770
3771 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3772 if (!pg_proc_dir)
3773 return -ENODEV;
3774 pg_proc_dir->owner = THIS_MODULE;
3775
3776 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3777 if (pe == NULL) {
3778 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3779 "procfs entry.\n", PGCTRL);
3780 proc_net_remove(PG_PROC_DIR);
3781 return -EINVAL;
3782 }
3783
3784 pe->proc_fops = &pktgen_fops;
3785 pe->data = NULL;
3786
3787 /* Register us to receive netdevice events */
3788 register_netdevice_notifier(&pktgen_notifier_block);
3789
3790 for_each_online_cpu(cpu) {
3791 int err;
3792
3793 err = pktgen_create_thread(cpu);
3794 if (err)
3795 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3796 "thread for cpu %d (%d)\n", cpu, err);
3797 }
3798
3799 if (list_empty(&pktgen_threads)) {
3800 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3801 "all threads\n");
3802 unregister_netdevice_notifier(&pktgen_notifier_block);
3803 remove_proc_entry(PGCTRL, pg_proc_dir);
3804 proc_net_remove(PG_PROC_DIR);
3805 return -ENODEV;
3806 }
3807
3808 return 0;
3809 }
3810
3811 static void __exit pg_cleanup(void)
3812 {
3813 struct pktgen_thread *t;
3814 struct list_head *q, *n;
3815 wait_queue_head_t queue;
3816 init_waitqueue_head(&queue);
3817
3818 /* Stop all interfaces & threads */
3819
3820 list_for_each_safe(q, n, &pktgen_threads) {
3821 t = list_entry(q, struct pktgen_thread, th_list);
3822 kthread_stop(t->tsk);
3823 kfree(t);
3824 }
3825
3826 /* Un-register us from receiving netdevice events */
3827 unregister_netdevice_notifier(&pktgen_notifier_block);
3828
3829 /* Clean up proc file system */
3830 remove_proc_entry(PGCTRL, pg_proc_dir);
3831 proc_net_remove(PG_PROC_DIR);
3832 }
3833
3834 module_init(pg_init);
3835 module_exit(pg_cleanup);
3836
3837 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3838 MODULE_DESCRIPTION("Packet Generator tool");
3839 MODULE_LICENSE("GPL");
3840 module_param(pg_count_d, int, 0);
3841 module_param(pg_delay_d, int, 0);
3842 module_param(pg_clone_skb_d, int, 0);
3843 module_param(debug, int, 0);
This page took 0.172131 seconds and 5 git commands to generate.