blob: 8eb083b6041a7fae844be27a29e9082e828587cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 pratical 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 * Rany 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#include <linux/sys.h>
111#include <linux/types.h>
112#include <linux/module.h>
113#include <linux/moduleparam.h>
114#include <linux/kernel.h>
115#include <linux/smp_lock.h>
116#include <linux/sched.h>
117#include <linux/slab.h>
118#include <linux/vmalloc.h>
119#include <linux/sched.h>
120#include <linux/unistd.h>
121#include <linux/string.h>
122#include <linux/ptrace.h>
123#include <linux/errno.h>
124#include <linux/ioport.h>
125#include <linux/interrupt.h>
126#include <linux/delay.h>
127#include <linux/timer.h>
128#include <linux/init.h>
129#include <linux/skbuff.h>
130#include <linux/netdevice.h>
131#include <linux/inet.h>
132#include <linux/inetdevice.h>
133#include <linux/rtnetlink.h>
134#include <linux/if_arp.h>
135#include <linux/in.h>
136#include <linux/ip.h>
137#include <linux/ipv6.h>
138#include <linux/udp.h>
139#include <linux/proc_fs.h>
140#include <linux/wait.h>
141#include <net/checksum.h>
142#include <net/ipv6.h>
143#include <net/addrconf.h>
144#include <asm/byteorder.h>
145#include <linux/rcupdate.h>
146#include <asm/bitops.h>
147#include <asm/io.h>
148#include <asm/dma.h>
149#include <asm/uaccess.h>
150#include <asm/div64.h> /* do_div */
151#include <asm/timex.h>
152
153
Robert Olsson64053be2005-06-26 15:27:10 -0700154#define VERSION "pktgen v2.62: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156/* #define PG_DEBUG(a) a */
157#define PG_DEBUG(a)
158
159/* The buckets are exponential in 'width' */
160#define LAT_BUCKETS_MAX 32
161#define IP_NAME_SZ 32
162
163/* Device flag bits */
164#define F_IPSRC_RND (1<<0) /* IP-Src Random */
165#define F_IPDST_RND (1<<1) /* IP-Dst Random */
166#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
167#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
168#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
169#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
170#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
171#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
172
173/* Thread control flag bits */
174#define T_TERMINATE (1<<0)
175#define T_STOP (1<<1) /* Stop run */
176#define T_RUN (1<<2) /* Start run */
177#define T_REMDEV (1<<3) /* Remove all devs */
178
179/* Locks */
180#define thread_lock() spin_lock(&_thread_lock)
181#define thread_unlock() spin_unlock(&_thread_lock)
182
183/* If lock -- can be removed after some work */
184#define if_lock(t) spin_lock(&(t->if_lock));
185#define if_unlock(t) spin_unlock(&(t->if_lock));
186
187/* Used to help with determining the pkts on receive */
188#define PKTGEN_MAGIC 0xbe9be955
189#define PG_PROC_DIR "pktgen"
190
191#define MAX_CFLOWS 65536
192
193struct flow_state
194{
195 __u32 cur_daddr;
196 int count;
197};
198
199struct pktgen_dev {
200
201 /*
202 * Try to keep frequent/infrequent used vars. separated.
203 */
204
205 char ifname[32];
206 struct proc_dir_entry *proc_ent;
207 char result[512];
208 /* proc file names */
209 char fname[80];
210
211 struct pktgen_thread* pg_thread; /* the owner */
212 struct pktgen_dev *next; /* Used for chaining in the thread's run-queue */
213
214 int running; /* if this changes to false, the test will stop */
215
216 /* If min != max, then we will either do a linear iteration, or
217 * we will do a random selection from within the range.
218 */
219 __u32 flags;
220
221 int min_pkt_size; /* = ETH_ZLEN; */
222 int max_pkt_size; /* = ETH_ZLEN; */
223 int nfrags;
224 __u32 delay_us; /* Default delay */
225 __u32 delay_ns;
226 __u64 count; /* Default No packets to send */
227 __u64 sofar; /* How many pkts we've sent so far */
228 __u64 tx_bytes; /* How many bytes we've transmitted */
229 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
230
231 /* runtime counters relating to clone_skb */
232 __u64 next_tx_us; /* timestamp of when to tx next */
233 __u32 next_tx_ns;
234
235 __u64 allocated_skbs;
236 __u32 clone_count;
237 int last_ok; /* Was last skb sent?
238 * Or a failed transmit of some sort? This will keep
239 * sequence numbers in order, for example.
240 */
241 __u64 started_at; /* micro-seconds */
242 __u64 stopped_at; /* micro-seconds */
243 __u64 idle_acc; /* micro-seconds */
244 __u32 seq_num;
245
246 int clone_skb; /* Use multiple SKBs during packet gen. If this number
247 * is greater than 1, then that many coppies of the same
248 * packet will be sent before a new packet is allocated.
249 * For instance, if you want to send 1024 identical packets
250 * before creating a new packet, set clone_skb to 1024.
251 */
252
253 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
254 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
255 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
256 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
257
258 struct in6_addr in6_saddr;
259 struct in6_addr in6_daddr;
260 struct in6_addr cur_in6_daddr;
261 struct in6_addr cur_in6_saddr;
262 /* For ranges */
263 struct in6_addr min_in6_daddr;
264 struct in6_addr max_in6_daddr;
265 struct in6_addr min_in6_saddr;
266 struct in6_addr max_in6_saddr;
267
268 /* If we're doing ranges, random or incremental, then this
269 * defines the min/max for those ranges.
270 */
271 __u32 saddr_min; /* inclusive, source IP address */
272 __u32 saddr_max; /* exclusive, source IP address */
273 __u32 daddr_min; /* inclusive, dest IP address */
274 __u32 daddr_max; /* exclusive, dest IP address */
275
276 __u16 udp_src_min; /* inclusive, source UDP port */
277 __u16 udp_src_max; /* exclusive, source UDP port */
278 __u16 udp_dst_min; /* inclusive, dest UDP port */
279 __u16 udp_dst_max; /* exclusive, dest UDP port */
280
281 __u32 src_mac_count; /* How many MACs to iterate through */
282 __u32 dst_mac_count; /* How many MACs to iterate through */
283
284 unsigned char dst_mac[6];
285 unsigned char src_mac[6];
286
287 __u32 cur_dst_mac_offset;
288 __u32 cur_src_mac_offset;
289 __u32 cur_saddr;
290 __u32 cur_daddr;
291 __u16 cur_udp_dst;
292 __u16 cur_udp_src;
293 __u32 cur_pkt_size;
294
295 __u8 hh[14];
296 /* = {
297 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
298
299 We fill in SRC address later
300 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x08, 0x00
302 };
303 */
304 __u16 pad; /* pad out the hh struct to an even 16 bytes */
305
306 struct sk_buff* skb; /* skb we are to transmit next, mainly used for when we
307 * are transmitting the same one multiple times
308 */
309 struct net_device* odev; /* The out-going device. Note that the device should
310 * have it's pg_info pointer pointing back to this
311 * device. This will be set when the user specifies
312 * the out-going device name (not when the inject is
313 * started as it used to do.)
314 */
315 struct flow_state *flows;
316 unsigned cflows; /* Concurrent flows (config) */
317 unsigned lflow; /* Flow length (config) */
318 unsigned nflows; /* accumulated flows (stats) */
319};
320
321struct pktgen_hdr {
322 __u32 pgh_magic;
323 __u32 seq_num;
324 __u32 tv_sec;
325 __u32 tv_usec;
326};
327
328struct pktgen_thread {
329 spinlock_t if_lock;
330 struct pktgen_dev *if_list; /* All device here */
331 struct pktgen_thread* next;
332 char name[32];
333 char fname[128]; /* name of proc file */
334 struct proc_dir_entry *proc_ent;
335 char result[512];
336 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
337
338 /* Field for thread to receive "posted" events terminate, stop ifs etc.*/
339
340 u32 control;
341 int pid;
342 int cpu;
343
344 wait_queue_head_t queue;
345};
346
347#define REMOVE 1
348#define FIND 0
349
350/* This code works around the fact that do_div cannot handle two 64-bit
351 numbers, and regular 64-bit division doesn't work on x86 kernels.
352 --Ben
353*/
354
355#define PG_DIV 0
356
357/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
358 * Function copied/adapted/optimized from:
359 *
360 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
361 *
362 * Copyright 1994, University of Cambridge Computer Laboratory
363 * All Rights Reserved.
364 *
365 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700366static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 u64 a = (x < 0) ? -x : x;
369 u64 b = (y < 0) ? -y : y;
370 u64 res = 0, d = 1;
371
372 if (b > 0) {
373 while (b < a) {
374 b <<= 1;
375 d <<= 1;
376 }
377 }
378
379 do {
380 if ( a >= b ) {
381 a -= b;
382 res += d;
383 }
384 b >>= 1;
385 d >>= 1;
386 }
387 while (d);
388
389 if (PG_DIV == type) {
390 return (((x ^ y) & (1ll<<63)) == 0) ? res : -(s64)res;
391 }
392 else {
393 return ((x & (1ll<<63)) == 0) ? a : -(s64)a;
394 }
395}
396
397/* End of hacks to deal with 64-bit math on x86 */
398
399/** Convert to miliseconds */
400static inline __u64 tv_to_ms(const struct timeval* tv)
401{
402 __u64 ms = tv->tv_usec / 1000;
403 ms += (__u64)tv->tv_sec * (__u64)1000;
404 return ms;
405}
406
407
408/** Convert to micro-seconds */
409static inline __u64 tv_to_us(const struct timeval* tv)
410{
411 __u64 us = tv->tv_usec;
412 us += (__u64)tv->tv_sec * (__u64)1000000;
413 return us;
414}
415
416static inline __u64 pg_div(__u64 n, __u32 base) {
417 __u64 tmp = n;
418 do_div(tmp, base);
419 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
420 n, base, tmp); */
421 return tmp;
422}
423
424static inline __u64 pg_div64(__u64 n, __u64 base)
425{
426 __u64 tmp = n;
427/*
428 * How do we know if the architectrure we are running on
429 * supports division with 64 bit base?
430 *
431 */
432#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
433
434 do_div(tmp, base);
435#else
436 tmp = divremdi3(n, base, PG_DIV);
437#endif
438 return tmp;
439}
440
441static inline u32 pktgen_random(void)
442{
443#if 0
444 __u32 n;
445 get_random_bytes(&n, 4);
446 return n;
447#else
448 return net_random();
449#endif
450}
451
452static inline __u64 getCurMs(void)
453{
454 struct timeval tv;
455 do_gettimeofday(&tv);
456 return tv_to_ms(&tv);
457}
458
459static inline __u64 getCurUs(void)
460{
461 struct timeval tv;
462 do_gettimeofday(&tv);
463 return tv_to_us(&tv);
464}
465
466static inline __u64 tv_diff(const struct timeval* a, const struct timeval* b)
467{
468 return tv_to_us(a) - tv_to_us(b);
469}
470
471
472/* old include end */
473
474static char version[] __initdata = VERSION;
475
476static ssize_t proc_pgctrl_read(struct file* file, char __user * buf, size_t count, loff_t *ppos);
477static ssize_t proc_pgctrl_write(struct file* file, const char __user * buf, size_t count, loff_t *ppos);
478static int proc_if_read(char *buf , char **start, off_t offset, int len, int *eof, void *data);
479
480static int proc_thread_read(char *buf , char **start, off_t offset, int len, int *eof, void *data);
481static int proc_if_write(struct file *file, const char __user *user_buffer, unsigned long count, void *data);
482static int proc_thread_write(struct file *file, const char __user *user_buffer, unsigned long count, void *data);
483static int create_proc_dir(void);
484static int remove_proc_dir(void);
485
486static int pktgen_remove_device(struct pktgen_thread* t, struct pktgen_dev *i);
487static int pktgen_add_device(struct pktgen_thread* t, const char* ifname);
488static struct pktgen_thread* pktgen_find_thread(const char* name);
489static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread* t, const char* ifname);
490static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
491static void pktgen_run_all_threads(void);
492static void pktgen_stop_all_threads_ifs(void);
493static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
494static void pktgen_stop(struct pktgen_thread* t);
495static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
496static struct pktgen_dev *pktgen_NN_threads(const char* dev_name, int remove);
497static unsigned int scan_ip6(const char *s,char ip[16]);
498static unsigned int fmt_ip6(char *s,const char ip[16]);
499
500/* Module parameters, defaults. */
501static int pg_count_d = 1000; /* 1000 pkts by default */
502static int pg_delay_d = 0;
503static int pg_clone_skb_d = 0;
504static int debug = 0;
505
506static spinlock_t _thread_lock = SPIN_LOCK_UNLOCKED;
507static struct pktgen_thread *pktgen_threads = NULL;
508
509static char module_fname[128];
510static struct proc_dir_entry *module_proc_ent = NULL;
511
512static struct notifier_block pktgen_notifier_block = {
513 .notifier_call = pktgen_device_event,
514};
515
516static struct file_operations pktgen_fops = {
517 .read = proc_pgctrl_read,
518 .write = proc_pgctrl_write,
519 /* .ioctl = pktgen_ioctl, later maybe */
520};
521
522/*
523 * /proc handling functions
524 *
525 */
526
527static struct proc_dir_entry *pg_proc_dir = NULL;
528static int proc_pgctrl_read_eof=0;
529
530static ssize_t proc_pgctrl_read(struct file* file, char __user * buf,
531 size_t count, loff_t *ppos)
532{
533 char data[200];
534 int len = 0;
535
536 if(proc_pgctrl_read_eof) {
537 proc_pgctrl_read_eof=0;
538 len = 0;
539 goto out;
540 }
541
542 sprintf(data, "%s", VERSION);
543
544 len = strlen(data);
545
546 if(len > count) {
547 len =-EFAULT;
548 goto out;
549 }
550
551 if (copy_to_user(buf, data, len)) {
552 len =-EFAULT;
553 goto out;
554 }
555
556 *ppos += len;
557 proc_pgctrl_read_eof=1; /* EOF next call */
558
559 out:
560 return len;
561}
562
563static ssize_t proc_pgctrl_write(struct file* file,const char __user * buf,
564 size_t count, loff_t *ppos)
565{
566 char *data = NULL;
567 int err = 0;
568
569 if (!capable(CAP_NET_ADMIN)){
570 err = -EPERM;
571 goto out;
572 }
573
574 data = (void*)vmalloc ((unsigned int)count);
575
576 if(!data) {
577 err = -ENOMEM;
578 goto out;
579 }
580 if (copy_from_user(data, buf, count)) {
581 err =-EFAULT;
582 goto out_free;
583 }
584 data[count-1] = 0; /* Make string */
585
586 if (!strcmp(data, "stop"))
587 pktgen_stop_all_threads_ifs();
588
589 else if (!strcmp(data, "start"))
590 pktgen_run_all_threads();
591
592 else
593 printk("pktgen: Unknown command: %s\n", data);
594
595 err = count;
596
597 out_free:
598 vfree (data);
599 out:
600 return err;
601}
602
603static int proc_if_read(char *buf , char **start, off_t offset,
604 int len, int *eof, void *data)
605{
606 char *p;
607 int i;
608 struct pktgen_dev *pkt_dev = (struct pktgen_dev*)(data);
609 __u64 sa;
610 __u64 stopped;
611 __u64 now = getCurUs();
612
613 p = buf;
614 p += sprintf(p, "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
615 (unsigned long long) pkt_dev->count,
616 pkt_dev->min_pkt_size, pkt_dev->max_pkt_size);
617
618 p += sprintf(p, " frags: %d delay: %u clone_skb: %d ifname: %s\n",
619 pkt_dev->nfrags, 1000*pkt_dev->delay_us+pkt_dev->delay_ns, pkt_dev->clone_skb, pkt_dev->ifname);
620
621 p += sprintf(p, " flows: %u flowlen: %u\n", pkt_dev->cflows, pkt_dev->lflow);
622
623
624 if(pkt_dev->flags & F_IPV6) {
625 char b1[128], b2[128], b3[128];
626 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
627 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
628 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
629 p += sprintf(p, " saddr: %s min_saddr: %s max_saddr: %s\n", b1, b2, b3);
630
631 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
632 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
633 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
634 p += sprintf(p, " daddr: %s min_daddr: %s max_daddr: %s\n", b1, b2, b3);
635
636 }
637 else
638 p += sprintf(p, " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
639 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min, pkt_dev->src_max);
640
641 p += sprintf(p, " src_mac: ");
642
643 if ((pkt_dev->src_mac[0] == 0) &&
644 (pkt_dev->src_mac[1] == 0) &&
645 (pkt_dev->src_mac[2] == 0) &&
646 (pkt_dev->src_mac[3] == 0) &&
647 (pkt_dev->src_mac[4] == 0) &&
648 (pkt_dev->src_mac[5] == 0))
649
650 for (i = 0; i < 6; i++)
651 p += sprintf(p, "%02X%s", pkt_dev->odev->dev_addr[i], i == 5 ? " " : ":");
652
653 else
654 for (i = 0; i < 6; i++)
655 p += sprintf(p, "%02X%s", pkt_dev->src_mac[i], i == 5 ? " " : ":");
656
657 p += sprintf(p, "dst_mac: ");
658 for (i = 0; i < 6; i++)
659 p += sprintf(p, "%02X%s", pkt_dev->dst_mac[i], i == 5 ? "\n" : ":");
660
661 p += sprintf(p, " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
662 pkt_dev->udp_src_min, pkt_dev->udp_src_max, pkt_dev->udp_dst_min,
663 pkt_dev->udp_dst_max);
664
665 p += sprintf(p, " src_mac_count: %d dst_mac_count: %d \n Flags: ",
666 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
667
668
669 if (pkt_dev->flags & F_IPV6)
670 p += sprintf(p, "IPV6 ");
671
672 if (pkt_dev->flags & F_IPSRC_RND)
673 p += sprintf(p, "IPSRC_RND ");
674
675 if (pkt_dev->flags & F_IPDST_RND)
676 p += sprintf(p, "IPDST_RND ");
677
678 if (pkt_dev->flags & F_TXSIZE_RND)
679 p += sprintf(p, "TXSIZE_RND ");
680
681 if (pkt_dev->flags & F_UDPSRC_RND)
682 p += sprintf(p, "UDPSRC_RND ");
683
684 if (pkt_dev->flags & F_UDPDST_RND)
685 p += sprintf(p, "UDPDST_RND ");
686
687 if (pkt_dev->flags & F_MACSRC_RND)
688 p += sprintf(p, "MACSRC_RND ");
689
690 if (pkt_dev->flags & F_MACDST_RND)
691 p += sprintf(p, "MACDST_RND ");
692
693
694 p += sprintf(p, "\n");
695
696 sa = pkt_dev->started_at;
697 stopped = pkt_dev->stopped_at;
698 if (pkt_dev->running)
699 stopped = now; /* not really stopped, more like last-running-at */
700
701 p += sprintf(p, "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
702 (unsigned long long) pkt_dev->sofar,
703 (unsigned long long) pkt_dev->errors,
704 (unsigned long long) sa,
705 (unsigned long long) stopped,
706 (unsigned long long) pkt_dev->idle_acc);
707
708 p += sprintf(p, " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
709 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset, pkt_dev->cur_src_mac_offset);
710
711 if(pkt_dev->flags & F_IPV6) {
712 char b1[128], b2[128];
713 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
714 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
715 p += sprintf(p, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
716 }
717 else
718 p += sprintf(p, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
719 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
720
721
722 p += sprintf(p, " cur_udp_dst: %d cur_udp_src: %d\n",
723 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
724
725 p += sprintf(p, " flows: %u\n", pkt_dev->nflows);
726
727 if (pkt_dev->result[0])
728 p += sprintf(p, "Result: %s\n", pkt_dev->result);
729 else
730 p += sprintf(p, "Result: Idle\n");
731 *eof = 1;
732
733 return p - buf;
734}
735
736
737static int count_trail_chars(const char __user *user_buffer, unsigned int maxlen)
738{
739 int i;
740
741 for (i = 0; i < maxlen; i++) {
742 char c;
743 if (get_user(c, &user_buffer[i]))
744 return -EFAULT;
745 switch (c) {
746 case '\"':
747 case '\n':
748 case '\r':
749 case '\t':
750 case ' ':
751 case '=':
752 break;
753 default:
754 goto done;
755 };
756 }
757done:
758 return i;
759}
760
761static unsigned long num_arg(const char __user *user_buffer, unsigned long maxlen,
762 unsigned long *num)
763{
764 int i = 0;
765 *num = 0;
766
767 for(; i < maxlen; i++) {
768 char c;
769 if (get_user(c, &user_buffer[i]))
770 return -EFAULT;
771 if ((c >= '0') && (c <= '9')) {
772 *num *= 10;
773 *num += c -'0';
774 } else
775 break;
776 }
777 return i;
778}
779
780static int strn_len(const char __user *user_buffer, unsigned int maxlen)
781{
782 int i = 0;
783
784 for(; i < maxlen; i++) {
785 char c;
786 if (get_user(c, &user_buffer[i]))
787 return -EFAULT;
788 switch (c) {
789 case '\"':
790 case '\n':
791 case '\r':
792 case '\t':
793 case ' ':
794 goto done_str;
795 break;
796 default:
797 break;
798 };
799 }
800done_str:
801
802 return i;
803}
804
805static int proc_if_write(struct file *file, const char __user *user_buffer,
806 unsigned long count, void *data)
807{
808 int i = 0, max, len;
809 char name[16], valstr[32];
810 unsigned long value = 0;
811 struct pktgen_dev *pkt_dev = (struct pktgen_dev*)(data);
812 char* pg_result = NULL;
813 int tmp = 0;
814 char buf[128];
815
816 pg_result = &(pkt_dev->result[0]);
817
818 if (count < 1) {
819 printk("pktgen: wrong command format\n");
820 return -EINVAL;
821 }
822
823 max = count - i;
824 tmp = count_trail_chars(&user_buffer[i], max);
825 if (tmp < 0) {
826 printk("pktgen: illegal format\n");
827 return tmp;
828 }
829 i += tmp;
830
831 /* Read variable name */
832
833 len = strn_len(&user_buffer[i], sizeof(name) - 1);
834 if (len < 0) { return len; }
835 memset(name, 0, sizeof(name));
836 if (copy_from_user(name, &user_buffer[i], len) )
837 return -EFAULT;
838 i += len;
839
840 max = count -i;
841 len = count_trail_chars(&user_buffer[i], max);
842 if (len < 0)
843 return len;
844
845 i += len;
846
847 if (debug) {
848 char tb[count + 1];
849 if (copy_from_user(tb, user_buffer, count))
850 return -EFAULT;
851 tb[count] = 0;
852 printk("pktgen: %s,%lu buffer -:%s:-\n", name, count, tb);
853 }
854
855 if (!strcmp(name, "min_pkt_size")) {
856 len = num_arg(&user_buffer[i], 10, &value);
857 if (len < 0) { return len; }
858 i += len;
859 if (value < 14+20+8)
860 value = 14+20+8;
861 if (value != pkt_dev->min_pkt_size) {
862 pkt_dev->min_pkt_size = value;
863 pkt_dev->cur_pkt_size = value;
864 }
865 sprintf(pg_result, "OK: min_pkt_size=%u", pkt_dev->min_pkt_size);
866 return count;
867 }
868
869 if (!strcmp(name, "max_pkt_size")) {
870 len = num_arg(&user_buffer[i], 10, &value);
871 if (len < 0) { return len; }
872 i += len;
873 if (value < 14+20+8)
874 value = 14+20+8;
875 if (value != pkt_dev->max_pkt_size) {
876 pkt_dev->max_pkt_size = value;
877 pkt_dev->cur_pkt_size = value;
878 }
879 sprintf(pg_result, "OK: max_pkt_size=%u", pkt_dev->max_pkt_size);
880 return count;
881 }
882
883 /* Shortcut for min = max */
884
885 if (!strcmp(name, "pkt_size")) {
886 len = num_arg(&user_buffer[i], 10, &value);
887 if (len < 0) { return len; }
888 i += len;
889 if (value < 14+20+8)
890 value = 14+20+8;
891 if (value != pkt_dev->min_pkt_size) {
892 pkt_dev->min_pkt_size = value;
893 pkt_dev->max_pkt_size = value;
894 pkt_dev->cur_pkt_size = value;
895 }
896 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
897 return count;
898 }
899
900 if (!strcmp(name, "debug")) {
901 len = num_arg(&user_buffer[i], 10, &value);
902 if (len < 0) { return len; }
903 i += len;
904 debug = value;
905 sprintf(pg_result, "OK: debug=%u", debug);
906 return count;
907 }
908
909 if (!strcmp(name, "frags")) {
910 len = num_arg(&user_buffer[i], 10, &value);
911 if (len < 0) { return len; }
912 i += len;
913 pkt_dev->nfrags = value;
914 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
915 return count;
916 }
917 if (!strcmp(name, "delay")) {
918 len = num_arg(&user_buffer[i], 10, &value);
919 if (len < 0) { return len; }
920 i += len;
921 if (value == 0x7FFFFFFF) {
922 pkt_dev->delay_us = 0x7FFFFFFF;
923 pkt_dev->delay_ns = 0;
924 } else {
925 pkt_dev->delay_us = value / 1000;
926 pkt_dev->delay_ns = value % 1000;
927 }
928 sprintf(pg_result, "OK: delay=%u", 1000*pkt_dev->delay_us+pkt_dev->delay_ns);
929 return count;
930 }
931 if (!strcmp(name, "udp_src_min")) {
932 len = num_arg(&user_buffer[i], 10, &value);
933 if (len < 0) { return len; }
934 i += len;
935 if (value != pkt_dev->udp_src_min) {
936 pkt_dev->udp_src_min = value;
937 pkt_dev->cur_udp_src = value;
938 }
939 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
940 return count;
941 }
942 if (!strcmp(name, "udp_dst_min")) {
943 len = num_arg(&user_buffer[i], 10, &value);
944 if (len < 0) { return len; }
945 i += len;
946 if (value != pkt_dev->udp_dst_min) {
947 pkt_dev->udp_dst_min = value;
948 pkt_dev->cur_udp_dst = value;
949 }
950 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
951 return count;
952 }
953 if (!strcmp(name, "udp_src_max")) {
954 len = num_arg(&user_buffer[i], 10, &value);
955 if (len < 0) { return len; }
956 i += len;
957 if (value != pkt_dev->udp_src_max) {
958 pkt_dev->udp_src_max = value;
959 pkt_dev->cur_udp_src = value;
960 }
961 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
962 return count;
963 }
964 if (!strcmp(name, "udp_dst_max")) {
965 len = num_arg(&user_buffer[i], 10, &value);
966 if (len < 0) { return len; }
967 i += len;
968 if (value != pkt_dev->udp_dst_max) {
969 pkt_dev->udp_dst_max = value;
970 pkt_dev->cur_udp_dst = value;
971 }
972 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
973 return count;
974 }
975 if (!strcmp(name, "clone_skb")) {
976 len = num_arg(&user_buffer[i], 10, &value);
977 if (len < 0) { return len; }
978 i += len;
979 pkt_dev->clone_skb = value;
980
981 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
982 return count;
983 }
984 if (!strcmp(name, "count")) {
985 len = num_arg(&user_buffer[i], 10, &value);
986 if (len < 0) { return len; }
987 i += len;
988 pkt_dev->count = value;
989 sprintf(pg_result, "OK: count=%llu",
990 (unsigned long long) pkt_dev->count);
991 return count;
992 }
993 if (!strcmp(name, "src_mac_count")) {
994 len = num_arg(&user_buffer[i], 10, &value);
995 if (len < 0) { return len; }
996 i += len;
997 if (pkt_dev->src_mac_count != value) {
998 pkt_dev->src_mac_count = value;
999 pkt_dev->cur_src_mac_offset = 0;
1000 }
1001 sprintf(pg_result, "OK: src_mac_count=%d", pkt_dev->src_mac_count);
1002 return count;
1003 }
1004 if (!strcmp(name, "dst_mac_count")) {
1005 len = num_arg(&user_buffer[i], 10, &value);
1006 if (len < 0) { return len; }
1007 i += len;
1008 if (pkt_dev->dst_mac_count != value) {
1009 pkt_dev->dst_mac_count = value;
1010 pkt_dev->cur_dst_mac_offset = 0;
1011 }
1012 sprintf(pg_result, "OK: dst_mac_count=%d", pkt_dev->dst_mac_count);
1013 return count;
1014 }
1015 if (!strcmp(name, "flag")) {
1016 char f[32];
1017 memset(f, 0, 32);
1018 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1019 if (len < 0) { return len; }
1020 if (copy_from_user(f, &user_buffer[i], len))
1021 return -EFAULT;
1022 i += len;
1023 if (strcmp(f, "IPSRC_RND") == 0)
1024 pkt_dev->flags |= F_IPSRC_RND;
1025
1026 else if (strcmp(f, "!IPSRC_RND") == 0)
1027 pkt_dev->flags &= ~F_IPSRC_RND;
1028
1029 else if (strcmp(f, "TXSIZE_RND") == 0)
1030 pkt_dev->flags |= F_TXSIZE_RND;
1031
1032 else if (strcmp(f, "!TXSIZE_RND") == 0)
1033 pkt_dev->flags &= ~F_TXSIZE_RND;
1034
1035 else if (strcmp(f, "IPDST_RND") == 0)
1036 pkt_dev->flags |= F_IPDST_RND;
1037
1038 else if (strcmp(f, "!IPDST_RND") == 0)
1039 pkt_dev->flags &= ~F_IPDST_RND;
1040
1041 else if (strcmp(f, "UDPSRC_RND") == 0)
1042 pkt_dev->flags |= F_UDPSRC_RND;
1043
1044 else if (strcmp(f, "!UDPSRC_RND") == 0)
1045 pkt_dev->flags &= ~F_UDPSRC_RND;
1046
1047 else if (strcmp(f, "UDPDST_RND") == 0)
1048 pkt_dev->flags |= F_UDPDST_RND;
1049
1050 else if (strcmp(f, "!UDPDST_RND") == 0)
1051 pkt_dev->flags &= ~F_UDPDST_RND;
1052
1053 else if (strcmp(f, "MACSRC_RND") == 0)
1054 pkt_dev->flags |= F_MACSRC_RND;
1055
1056 else if (strcmp(f, "!MACSRC_RND") == 0)
1057 pkt_dev->flags &= ~F_MACSRC_RND;
1058
1059 else if (strcmp(f, "MACDST_RND") == 0)
1060 pkt_dev->flags |= F_MACDST_RND;
1061
1062 else if (strcmp(f, "!MACDST_RND") == 0)
1063 pkt_dev->flags &= ~F_MACDST_RND;
1064
1065 else {
1066 sprintf(pg_result, "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1067 f,
1068 "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1069 return count;
1070 }
1071 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1072 return count;
1073 }
1074 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1075 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1076 if (len < 0) { return len; }
1077
1078 if (copy_from_user(buf, &user_buffer[i], len))
1079 return -EFAULT;
1080 buf[len] = 0;
1081 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1082 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1083 strncpy(pkt_dev->dst_min, buf, len);
1084 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1085 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1086 }
1087 if(debug)
1088 printk("pktgen: dst_min set to: %s\n", pkt_dev->dst_min);
1089 i += len;
1090 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1091 return count;
1092 }
1093 if (!strcmp(name, "dst_max")) {
1094 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1095 if (len < 0) { return len; }
1096
1097 if (copy_from_user(buf, &user_buffer[i], len))
1098 return -EFAULT;
1099
1100 buf[len] = 0;
1101 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1102 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1103 strncpy(pkt_dev->dst_max, buf, len);
1104 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1105 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1106 }
1107 if(debug)
1108 printk("pktgen: dst_max set to: %s\n", pkt_dev->dst_max);
1109 i += len;
1110 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1111 return count;
1112 }
1113 if (!strcmp(name, "dst6")) {
1114 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1115 if (len < 0) return len;
1116
1117 pkt_dev->flags |= F_IPV6;
1118
1119 if (copy_from_user(buf, &user_buffer[i], len))
1120 return -EFAULT;
1121 buf[len] = 0;
1122
1123 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1124 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1125
1126 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1127
1128 if(debug)
1129 printk("pktgen: dst6 set to: %s\n", buf);
1130
1131 i += len;
1132 sprintf(pg_result, "OK: dst6=%s", buf);
1133 return count;
1134 }
1135 if (!strcmp(name, "dst6_min")) {
1136 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1137 if (len < 0) return len;
1138
1139 pkt_dev->flags |= F_IPV6;
1140
1141 if (copy_from_user(buf, &user_buffer[i], len))
1142 return -EFAULT;
1143 buf[len] = 0;
1144
1145 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1146 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1147
1148 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->min_in6_daddr);
1149 if(debug)
1150 printk("pktgen: dst6_min set to: %s\n", buf);
1151
1152 i += len;
1153 sprintf(pg_result, "OK: dst6_min=%s", buf);
1154 return count;
1155 }
1156 if (!strcmp(name, "dst6_max")) {
1157 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1158 if (len < 0) return len;
1159
1160 pkt_dev->flags |= F_IPV6;
1161
1162 if (copy_from_user(buf, &user_buffer[i], len))
1163 return -EFAULT;
1164 buf[len] = 0;
1165
1166 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1167 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1168
1169 if(debug)
1170 printk("pktgen: dst6_max set to: %s\n", buf);
1171
1172 i += len;
1173 sprintf(pg_result, "OK: dst6_max=%s", buf);
1174 return count;
1175 }
1176 if (!strcmp(name, "src6")) {
1177 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1178 if (len < 0) return len;
1179
1180 pkt_dev->flags |= F_IPV6;
1181
1182 if (copy_from_user(buf, &user_buffer[i], len))
1183 return -EFAULT;
1184 buf[len] = 0;
1185
1186 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1187 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1188
1189 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1190
1191 if(debug)
1192 printk("pktgen: src6 set to: %s\n", buf);
1193
1194 i += len;
1195 sprintf(pg_result, "OK: src6=%s", buf);
1196 return count;
1197 }
1198 if (!strcmp(name, "src_min")) {
1199 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1200 if (len < 0) { return len; }
1201 if (copy_from_user(buf, &user_buffer[i], len))
1202 return -EFAULT;
1203 buf[len] = 0;
1204 if (strcmp(buf, pkt_dev->src_min) != 0) {
1205 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1206 strncpy(pkt_dev->src_min, buf, len);
1207 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1208 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1209 }
1210 if(debug)
1211 printk("pktgen: src_min set to: %s\n", pkt_dev->src_min);
1212 i += len;
1213 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1214 return count;
1215 }
1216 if (!strcmp(name, "src_max")) {
1217 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1218 if (len < 0) { return len; }
1219 if (copy_from_user(buf, &user_buffer[i], len))
1220 return -EFAULT;
1221 buf[len] = 0;
1222 if (strcmp(buf, pkt_dev->src_max) != 0) {
1223 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1224 strncpy(pkt_dev->src_max, buf, len);
1225 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1226 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1227 }
1228 if(debug)
1229 printk("pktgen: src_max set to: %s\n", pkt_dev->src_max);
1230 i += len;
1231 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1232 return count;
1233 }
1234 if (!strcmp(name, "dst_mac")) {
1235 char *v = valstr;
1236 unsigned char old_dmac[6];
1237 unsigned char *m = pkt_dev->dst_mac;
1238 memcpy(old_dmac, pkt_dev->dst_mac, 6);
1239
1240 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1241 if (len < 0) { return len; }
1242 memset(valstr, 0, sizeof(valstr));
1243 if( copy_from_user(valstr, &user_buffer[i], len))
1244 return -EFAULT;
1245 i += len;
1246
1247 for(*m = 0;*v && m < pkt_dev->dst_mac + 6; v++) {
1248 if (*v >= '0' && *v <= '9') {
1249 *m *= 16;
1250 *m += *v - '0';
1251 }
1252 if (*v >= 'A' && *v <= 'F') {
1253 *m *= 16;
1254 *m += *v - 'A' + 10;
1255 }
1256 if (*v >= 'a' && *v <= 'f') {
1257 *m *= 16;
1258 *m += *v - 'a' + 10;
1259 }
1260 if (*v == ':') {
1261 m++;
1262 *m = 0;
1263 }
1264 }
1265
1266 /* Set up Dest MAC */
1267 if (memcmp(old_dmac, pkt_dev->dst_mac, 6) != 0)
1268 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, 6);
1269
1270 sprintf(pg_result, "OK: dstmac");
1271 return count;
1272 }
1273 if (!strcmp(name, "src_mac")) {
1274 char *v = valstr;
1275 unsigned char *m = pkt_dev->src_mac;
1276
1277 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1278 if (len < 0) { return len; }
1279 memset(valstr, 0, sizeof(valstr));
1280 if( copy_from_user(valstr, &user_buffer[i], len))
1281 return -EFAULT;
1282 i += len;
1283
1284 for(*m = 0;*v && m < pkt_dev->src_mac + 6; v++) {
1285 if (*v >= '0' && *v <= '9') {
1286 *m *= 16;
1287 *m += *v - '0';
1288 }
1289 if (*v >= 'A' && *v <= 'F') {
1290 *m *= 16;
1291 *m += *v - 'A' + 10;
1292 }
1293 if (*v >= 'a' && *v <= 'f') {
1294 *m *= 16;
1295 *m += *v - 'a' + 10;
1296 }
1297 if (*v == ':') {
1298 m++;
1299 *m = 0;
1300 }
1301 }
1302
1303 sprintf(pg_result, "OK: srcmac");
1304 return count;
1305 }
1306
1307 if (!strcmp(name, "clear_counters")) {
1308 pktgen_clear_counters(pkt_dev);
1309 sprintf(pg_result, "OK: Clearing counters.\n");
1310 return count;
1311 }
1312
1313 if (!strcmp(name, "flows")) {
1314 len = num_arg(&user_buffer[i], 10, &value);
1315 if (len < 0) { return len; }
1316 i += len;
1317 if (value > MAX_CFLOWS)
1318 value = MAX_CFLOWS;
1319
1320 pkt_dev->cflows = value;
1321 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1322 return count;
1323 }
1324
1325 if (!strcmp(name, "flowlen")) {
1326 len = num_arg(&user_buffer[i], 10, &value);
1327 if (len < 0) { return len; }
1328 i += len;
1329 pkt_dev->lflow = value;
1330 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1331 return count;
1332 }
1333
1334 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1335 return -EINVAL;
1336}
1337
1338static int proc_thread_read(char *buf , char **start, off_t offset,
1339 int len, int *eof, void *data)
1340{
1341 char *p;
1342 struct pktgen_thread *t = (struct pktgen_thread*)(data);
1343 struct pktgen_dev *pkt_dev = NULL;
1344
1345
1346 if (!t) {
1347 printk("pktgen: ERROR: could not find thread in proc_thread_read\n");
1348 return -EINVAL;
1349 }
1350
1351 p = buf;
1352 p += sprintf(p, "Name: %s max_before_softirq: %d\n",
1353 t->name, t->max_before_softirq);
1354
1355 p += sprintf(p, "Running: ");
1356
1357 if_lock(t);
1358 for(pkt_dev = t->if_list;pkt_dev; pkt_dev = pkt_dev->next)
1359 if(pkt_dev->running)
1360 p += sprintf(p, "%s ", pkt_dev->ifname);
1361
1362 p += sprintf(p, "\nStopped: ");
1363
1364 for(pkt_dev = t->if_list;pkt_dev; pkt_dev = pkt_dev->next)
1365 if(!pkt_dev->running)
1366 p += sprintf(p, "%s ", pkt_dev->ifname);
1367
1368 if (t->result[0])
1369 p += sprintf(p, "\nResult: %s\n", t->result);
1370 else
1371 p += sprintf(p, "\nResult: NA\n");
1372
1373 *eof = 1;
1374
1375 if_unlock(t);
1376
1377 return p - buf;
1378}
1379
1380static int proc_thread_write(struct file *file, const char __user *user_buffer,
1381 unsigned long count, void *data)
1382{
1383 int i = 0, max, len, ret;
1384 char name[40];
1385 struct pktgen_thread *t;
1386 char *pg_result;
1387 unsigned long value = 0;
1388
1389 if (count < 1) {
1390 // sprintf(pg_result, "Wrong command format");
1391 return -EINVAL;
1392 }
1393
1394 max = count - i;
1395 len = count_trail_chars(&user_buffer[i], max);
1396 if (len < 0)
1397 return len;
1398
1399 i += len;
1400
1401 /* Read variable name */
1402
1403 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1404 if (len < 0)
1405 return len;
1406
1407 memset(name, 0, sizeof(name));
1408 if (copy_from_user(name, &user_buffer[i], len))
1409 return -EFAULT;
1410 i += len;
1411
1412 max = count -i;
1413 len = count_trail_chars(&user_buffer[i], max);
1414 if (len < 0)
1415 return len;
1416
1417 i += len;
1418
1419 if (debug)
1420 printk("pktgen: t=%s, count=%lu\n", name, count);
1421
1422
1423 t = (struct pktgen_thread*)(data);
1424 if(!t) {
1425 printk("pktgen: ERROR: No thread\n");
1426 ret = -EINVAL;
1427 goto out;
1428 }
1429
1430 pg_result = &(t->result[0]);
1431
1432 if (!strcmp(name, "add_device")) {
1433 char f[32];
1434 memset(f, 0, 32);
1435 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1436 if (len < 0) {
1437 ret = len;
1438 goto out;
1439 }
1440 if( copy_from_user(f, &user_buffer[i], len) )
1441 return -EFAULT;
1442 i += len;
1443 thread_lock();
1444 pktgen_add_device(t, f);
1445 thread_unlock();
1446 ret = count;
1447 sprintf(pg_result, "OK: add_device=%s", f);
1448 goto out;
1449 }
1450
1451 if (!strcmp(name, "rem_device_all")) {
1452 thread_lock();
1453 t->control |= T_REMDEV;
1454 thread_unlock();
1455 current->state = TASK_INTERRUPTIBLE;
1456 schedule_timeout(HZ/8); /* Propagate thread->control */
1457 ret = count;
1458 sprintf(pg_result, "OK: rem_device_all");
1459 goto out;
1460 }
1461
1462 if (!strcmp(name, "max_before_softirq")) {
1463 len = num_arg(&user_buffer[i], 10, &value);
1464 thread_lock();
1465 t->max_before_softirq = value;
1466 thread_unlock();
1467 ret = count;
1468 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1469 goto out;
1470 }
1471
1472 ret = -EINVAL;
1473 out:
1474
1475 return ret;
1476}
1477
1478static int create_proc_dir(void)
1479{
1480 int len;
1481 /* does proc_dir already exists */
1482 len = strlen(PG_PROC_DIR);
1483
1484 for (pg_proc_dir = proc_net->subdir; pg_proc_dir; pg_proc_dir=pg_proc_dir->next) {
1485 if ((pg_proc_dir->namelen == len) &&
1486 (! memcmp(pg_proc_dir->name, PG_PROC_DIR, len)))
1487 break;
1488 }
1489
1490 if (!pg_proc_dir)
1491 pg_proc_dir = create_proc_entry(PG_PROC_DIR, S_IFDIR, proc_net);
1492
1493 if (!pg_proc_dir)
1494 return -ENODEV;
1495
1496 return 0;
1497}
1498
1499static int remove_proc_dir(void)
1500{
1501 remove_proc_entry(PG_PROC_DIR, proc_net);
1502 return 0;
1503}
1504
1505/* Think find or remove for NN */
1506static struct pktgen_dev *__pktgen_NN_threads(const char* ifname, int remove)
1507{
1508 struct pktgen_thread *t;
1509 struct pktgen_dev *pkt_dev = NULL;
1510
1511 t = pktgen_threads;
1512
1513 while (t) {
1514 pkt_dev = pktgen_find_dev(t, ifname);
1515 if (pkt_dev) {
1516 if(remove) {
1517 if_lock(t);
1518 pktgen_remove_device(t, pkt_dev);
1519 if_unlock(t);
1520 }
1521 break;
1522 }
1523 t = t->next;
1524 }
1525 return pkt_dev;
1526}
1527
1528static struct pktgen_dev *pktgen_NN_threads(const char* ifname, int remove)
1529{
1530 struct pktgen_dev *pkt_dev = NULL;
1531 thread_lock();
1532 pkt_dev = __pktgen_NN_threads(ifname, remove);
1533 thread_unlock();
1534 return pkt_dev;
1535}
1536
1537static int pktgen_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
1538{
1539 struct net_device *dev = (struct net_device *)(ptr);
1540
1541 /* It is OK that we do not hold the group lock right now,
1542 * as we run under the RTNL lock.
1543 */
1544
1545 switch (event) {
1546 case NETDEV_CHANGEADDR:
1547 case NETDEV_GOING_DOWN:
1548 case NETDEV_DOWN:
1549 case NETDEV_UP:
1550 /* Ignore for now */
1551 break;
1552
1553 case NETDEV_UNREGISTER:
1554 pktgen_NN_threads(dev->name, REMOVE);
1555 break;
1556 };
1557
1558 return NOTIFY_DONE;
1559}
1560
1561/* Associate pktgen_dev with a device. */
1562
1563static struct net_device* pktgen_setup_dev(struct pktgen_dev *pkt_dev) {
1564 struct net_device *odev;
1565
1566 /* Clean old setups */
1567
1568 if (pkt_dev->odev) {
1569 dev_put(pkt_dev->odev);
1570 pkt_dev->odev = NULL;
1571 }
1572
1573 odev = dev_get_by_name(pkt_dev->ifname);
1574
1575 if (!odev) {
1576 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1577 goto out;
1578 }
1579 if (odev->type != ARPHRD_ETHER) {
1580 printk("pktgen: not an ethernet device: \"%s\"\n", pkt_dev->ifname);
1581 goto out_put;
1582 }
1583 if (!netif_running(odev)) {
1584 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1585 goto out_put;
1586 }
1587 pkt_dev->odev = odev;
1588
1589 return pkt_dev->odev;
1590
1591out_put:
1592 dev_put(odev);
1593out:
1594 return NULL;
1595
1596}
1597
1598/* Read pkt_dev from the interface and set up internal pktgen_dev
1599 * structure to have the right information to create/send packets
1600 */
1601static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1602{
1603 /* Try once more, just in case it works now. */
1604 if (!pkt_dev->odev)
1605 pktgen_setup_dev(pkt_dev);
1606
1607 if (!pkt_dev->odev) {
1608 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1609 sprintf(pkt_dev->result, "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1610 return;
1611 }
1612
1613 /* Default to the interface's mac if not explicitly set. */
1614
1615 if ((pkt_dev->src_mac[0] == 0) &&
1616 (pkt_dev->src_mac[1] == 0) &&
1617 (pkt_dev->src_mac[2] == 0) &&
1618 (pkt_dev->src_mac[3] == 0) &&
1619 (pkt_dev->src_mac[4] == 0) &&
1620 (pkt_dev->src_mac[5] == 0)) {
1621
1622 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, 6);
1623 }
1624 /* Set up Dest MAC */
1625 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, 6);
1626
1627 /* Set up pkt size */
1628 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1629
1630 if(pkt_dev->flags & F_IPV6) {
1631 /*
1632 * Skip this automatic address setting until locks or functions
1633 * gets exported
1634 */
1635
1636#ifdef NOTNOW
1637 int i, set = 0, err=1;
1638 struct inet6_dev *idev;
1639
1640 for(i=0; i< IN6_ADDR_HSIZE; i++)
1641 if(pkt_dev->cur_in6_saddr.s6_addr[i]) {
1642 set = 1;
1643 break;
1644 }
1645
1646 if(!set) {
1647
1648 /*
1649 * Use linklevel address if unconfigured.
1650 *
1651 * use ipv6_get_lladdr if/when it's get exported
1652 */
1653
1654
1655 read_lock(&addrconf_lock);
1656 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1657 struct inet6_ifaddr *ifp;
1658
1659 read_lock_bh(&idev->lock);
1660 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1661 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1662 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &ifp->addr);
1663 err = 0;
1664 break;
1665 }
1666 }
1667 read_unlock_bh(&idev->lock);
1668 }
1669 read_unlock(&addrconf_lock);
1670 if(err) printk("pktgen: ERROR: IPv6 link address not availble.\n");
1671 }
1672#endif
1673 }
1674 else {
1675 pkt_dev->saddr_min = 0;
1676 pkt_dev->saddr_max = 0;
1677 if (strlen(pkt_dev->src_min) == 0) {
1678
1679 struct in_device *in_dev;
1680
1681 rcu_read_lock();
1682 in_dev = __in_dev_get(pkt_dev->odev);
1683 if (in_dev) {
1684 if (in_dev->ifa_list) {
1685 pkt_dev->saddr_min = in_dev->ifa_list->ifa_address;
1686 pkt_dev->saddr_max = pkt_dev->saddr_min;
1687 }
1688 __in_dev_put(in_dev);
1689 }
1690 rcu_read_unlock();
1691 }
1692 else {
1693 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1694 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1695 }
1696
1697 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1698 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1699 }
1700 /* Initialize current values. */
1701 pkt_dev->cur_dst_mac_offset = 0;
1702 pkt_dev->cur_src_mac_offset = 0;
1703 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1704 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1705 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
1706 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
1707 pkt_dev->nflows = 0;
1708}
1709
1710static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
1711{
1712 __u64 start;
1713 __u64 now;
1714
1715 start = now = getCurUs();
1716 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
1717 while (now < spin_until_us) {
1718 /* TODO: optimise sleeping behavior */
1719 if (spin_until_us - now > (1000000/HZ)+1) {
1720 current->state = TASK_INTERRUPTIBLE;
1721 schedule_timeout(1);
1722 } else if (spin_until_us - now > 100) {
1723 do_softirq();
1724 if (!pkt_dev->running)
1725 return;
1726 if (need_resched())
1727 schedule();
1728 }
1729
1730 now = getCurUs();
1731 }
1732
1733 pkt_dev->idle_acc += now - start;
1734}
1735
1736
1737/* Increment/randomize headers according to flags and current values
1738 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
1739 */
1740static void mod_cur_headers(struct pktgen_dev *pkt_dev) {
1741 __u32 imn;
1742 __u32 imx;
1743 int flow = 0;
1744
1745 if(pkt_dev->cflows) {
1746 flow = pktgen_random() % pkt_dev->cflows;
1747
1748 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
1749 pkt_dev->flows[flow].count = 0;
1750 }
1751
1752
1753 /* Deal with source MAC */
1754 if (pkt_dev->src_mac_count > 1) {
1755 __u32 mc;
1756 __u32 tmp;
1757
1758 if (pkt_dev->flags & F_MACSRC_RND)
1759 mc = pktgen_random() % (pkt_dev->src_mac_count);
1760 else {
1761 mc = pkt_dev->cur_src_mac_offset++;
1762 if (pkt_dev->cur_src_mac_offset > pkt_dev->src_mac_count)
1763 pkt_dev->cur_src_mac_offset = 0;
1764 }
1765
1766 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
1767 pkt_dev->hh[11] = tmp;
1768 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1769 pkt_dev->hh[10] = tmp;
1770 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1771 pkt_dev->hh[9] = tmp;
1772 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1773 pkt_dev->hh[8] = tmp;
1774 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
1775 pkt_dev->hh[7] = tmp;
1776 }
1777
1778 /* Deal with Destination MAC */
1779 if (pkt_dev->dst_mac_count > 1) {
1780 __u32 mc;
1781 __u32 tmp;
1782
1783 if (pkt_dev->flags & F_MACDST_RND)
1784 mc = pktgen_random() % (pkt_dev->dst_mac_count);
1785
1786 else {
1787 mc = pkt_dev->cur_dst_mac_offset++;
1788 if (pkt_dev->cur_dst_mac_offset > pkt_dev->dst_mac_count) {
1789 pkt_dev->cur_dst_mac_offset = 0;
1790 }
1791 }
1792
1793 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
1794 pkt_dev->hh[5] = tmp;
1795 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1796 pkt_dev->hh[4] = tmp;
1797 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1798 pkt_dev->hh[3] = tmp;
1799 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1800 pkt_dev->hh[2] = tmp;
1801 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
1802 pkt_dev->hh[1] = tmp;
1803 }
1804
1805 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
1806 if (pkt_dev->flags & F_UDPSRC_RND)
1807 pkt_dev->cur_udp_src = ((pktgen_random() % (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) + pkt_dev->udp_src_min);
1808
1809 else {
1810 pkt_dev->cur_udp_src++;
1811 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
1812 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
1813 }
1814 }
1815
1816 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
1817 if (pkt_dev->flags & F_UDPDST_RND) {
1818 pkt_dev->cur_udp_dst = ((pktgen_random() % (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) + pkt_dev->udp_dst_min);
1819 }
1820 else {
1821 pkt_dev->cur_udp_dst++;
1822 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
1823 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
1824 }
1825 }
1826
1827 if (!(pkt_dev->flags & F_IPV6)) {
1828
1829 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = ntohl(pkt_dev->saddr_max))) {
1830 __u32 t;
1831 if (pkt_dev->flags & F_IPSRC_RND)
1832 t = ((pktgen_random() % (imx - imn)) + imn);
1833 else {
1834 t = ntohl(pkt_dev->cur_saddr);
1835 t++;
1836 if (t > imx) {
1837 t = imn;
1838 }
1839 }
1840 pkt_dev->cur_saddr = htonl(t);
1841 }
1842
1843 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
1844 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
1845 } else {
1846
1847 if ((imn = ntohl(pkt_dev->daddr_min)) < (imx = ntohl(pkt_dev->daddr_max))) {
1848 __u32 t;
1849 if (pkt_dev->flags & F_IPDST_RND) {
1850
1851 t = ((pktgen_random() % (imx - imn)) + imn);
1852 t = htonl(t);
1853
1854 while( LOOPBACK(t) || MULTICAST(t) || BADCLASS(t) || ZERONET(t) || LOCAL_MCAST(t) ) {
1855 t = ((pktgen_random() % (imx - imn)) + imn);
1856 t = htonl(t);
1857 }
1858 pkt_dev->cur_daddr = t;
1859 }
1860
1861 else {
1862 t = ntohl(pkt_dev->cur_daddr);
1863 t++;
1864 if (t > imx) {
1865 t = imn;
1866 }
1867 pkt_dev->cur_daddr = htonl(t);
1868 }
1869 }
1870 if(pkt_dev->cflows) {
1871 pkt_dev->flows[flow].cur_daddr = pkt_dev->cur_daddr;
1872 pkt_dev->nflows++;
1873 }
1874 }
1875 }
1876 else /* IPV6 * */
1877 {
1878 if(pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
1879 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
1880 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
1881 pkt_dev->min_in6_daddr.s6_addr32[3] == 0);
1882 else {
1883 int i;
1884
1885 /* Only random destinations yet */
1886
1887 for(i=0; i < 4; i++) {
1888 pkt_dev->cur_in6_daddr.s6_addr32[i] =
1889 ((pktgen_random() |
1890 pkt_dev->min_in6_daddr.s6_addr32[i]) &
1891 pkt_dev->max_in6_daddr.s6_addr32[i]);
1892 }
1893 }
1894 }
1895
1896 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
1897 __u32 t;
1898 if (pkt_dev->flags & F_TXSIZE_RND) {
1899 t = ((pktgen_random() % (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
1900 + pkt_dev->min_pkt_size);
1901 }
1902 else {
1903 t = pkt_dev->cur_pkt_size + 1;
1904 if (t > pkt_dev->max_pkt_size)
1905 t = pkt_dev->min_pkt_size;
1906 }
1907 pkt_dev->cur_pkt_size = t;
1908 }
1909
1910 pkt_dev->flows[flow].count++;
1911}
1912
1913
1914static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
1915 struct pktgen_dev *pkt_dev)
1916{
1917 struct sk_buff *skb = NULL;
1918 __u8 *eth;
1919 struct udphdr *udph;
1920 int datalen, iplen;
1921 struct iphdr *iph;
1922 struct pktgen_hdr *pgh = NULL;
1923
Robert Olsson64053be2005-06-26 15:27:10 -07001924 /* Update any of the values, used when we're incrementing various
1925 * fields.
1926 */
1927 mod_cur_headers(pkt_dev);
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
1930 if (!skb) {
1931 sprintf(pkt_dev->result, "No memory");
1932 return NULL;
1933 }
1934
1935 skb_reserve(skb, 16);
1936
1937 /* Reserve for ethernet and IP header */
1938 eth = (__u8 *) skb_push(skb, 14);
1939 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
1940 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 memcpy(eth, pkt_dev->hh, 12);
1943 *(u16*)&eth[12] = __constant_htons(ETH_P_IP);
1944
1945 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */
1946 if (datalen < sizeof(struct pktgen_hdr))
1947 datalen = sizeof(struct pktgen_hdr);
1948
1949 udph->source = htons(pkt_dev->cur_udp_src);
1950 udph->dest = htons(pkt_dev->cur_udp_dst);
1951 udph->len = htons(datalen + 8); /* DATA + udphdr */
1952 udph->check = 0; /* No checksum */
1953
1954 iph->ihl = 5;
1955 iph->version = 4;
1956 iph->ttl = 32;
1957 iph->tos = 0;
1958 iph->protocol = IPPROTO_UDP; /* UDP */
1959 iph->saddr = pkt_dev->cur_saddr;
1960 iph->daddr = pkt_dev->cur_daddr;
1961 iph->frag_off = 0;
1962 iplen = 20 + 8 + datalen;
1963 iph->tot_len = htons(iplen);
1964 iph->check = 0;
1965 iph->check = ip_fast_csum((void *) iph, iph->ihl);
1966 skb->protocol = __constant_htons(ETH_P_IP);
1967 skb->mac.raw = ((u8 *)iph) - 14;
1968 skb->dev = odev;
1969 skb->pkt_type = PACKET_HOST;
1970
1971 if (pkt_dev->nfrags <= 0)
1972 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
1973 else {
1974 int frags = pkt_dev->nfrags;
1975 int i;
1976
1977 pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
1978
1979 if (frags > MAX_SKB_FRAGS)
1980 frags = MAX_SKB_FRAGS;
1981 if (datalen > frags*PAGE_SIZE) {
1982 skb_put(skb, datalen-frags*PAGE_SIZE);
1983 datalen = frags*PAGE_SIZE;
1984 }
1985
1986 i = 0;
1987 while (datalen > 0) {
1988 struct page *page = alloc_pages(GFP_KERNEL, 0);
1989 skb_shinfo(skb)->frags[i].page = page;
1990 skb_shinfo(skb)->frags[i].page_offset = 0;
1991 skb_shinfo(skb)->frags[i].size =
1992 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
1993 datalen -= skb_shinfo(skb)->frags[i].size;
1994 skb->len += skb_shinfo(skb)->frags[i].size;
1995 skb->data_len += skb_shinfo(skb)->frags[i].size;
1996 i++;
1997 skb_shinfo(skb)->nr_frags = i;
1998 }
1999
2000 while (i < frags) {
2001 int rem;
2002
2003 if (i == 0)
2004 break;
2005
2006 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2007 if (rem == 0)
2008 break;
2009
2010 skb_shinfo(skb)->frags[i - 1].size -= rem;
2011
2012 skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
2013 get_page(skb_shinfo(skb)->frags[i].page);
2014 skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
2015 skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
2016 skb_shinfo(skb)->frags[i].size = rem;
2017 i++;
2018 skb_shinfo(skb)->nr_frags = i;
2019 }
2020 }
2021
2022 /* Stamp the time, and sequence number, convert them to network byte order */
2023
2024 if (pgh) {
2025 struct timeval timestamp;
2026
2027 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2028 pgh->seq_num = htonl(pkt_dev->seq_num);
2029
2030 do_gettimeofday(&timestamp);
2031 pgh->tv_sec = htonl(timestamp.tv_sec);
2032 pgh->tv_usec = htonl(timestamp.tv_usec);
2033 }
2034 pkt_dev->seq_num++;
2035
2036 return skb;
2037}
2038
2039/*
2040 * scan_ip6, fmt_ip taken from dietlibc-0.21
2041 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2042 *
2043 * Slightly modified for kernel.
2044 * Should be candidate for net/ipv4/utils.c
2045 * --ro
2046 */
2047
2048static unsigned int scan_ip6(const char *s,char ip[16])
2049{
2050 unsigned int i;
2051 unsigned int len=0;
2052 unsigned long u;
2053 char suffix[16];
2054 unsigned int prefixlen=0;
2055 unsigned int suffixlen=0;
2056 __u32 tmp;
2057
2058 for (i=0; i<16; i++) ip[i]=0;
2059
2060 for (;;) {
2061 if (*s == ':') {
2062 len++;
2063 if (s[1] == ':') { /* Found "::", skip to part 2 */
2064 s+=2;
2065 len++;
2066 break;
2067 }
2068 s++;
2069 }
2070 {
2071 char *tmp;
2072 u=simple_strtoul(s,&tmp,16);
2073 i=tmp-s;
2074 }
2075
2076 if (!i) return 0;
2077 if (prefixlen==12 && s[i]=='.') {
2078
2079 /* the last 4 bytes may be written as IPv4 address */
2080
2081 tmp = in_aton(s);
2082 memcpy((struct in_addr*)(ip+12), &tmp, sizeof(tmp));
2083 return i+len;
2084 }
2085 ip[prefixlen++] = (u >> 8);
2086 ip[prefixlen++] = (u & 255);
2087 s += i; len += i;
2088 if (prefixlen==16)
2089 return len;
2090 }
2091
2092/* part 2, after "::" */
2093 for (;;) {
2094 if (*s == ':') {
2095 if (suffixlen==0)
2096 break;
2097 s++;
2098 len++;
2099 } else if (suffixlen!=0)
2100 break;
2101 {
2102 char *tmp;
2103 u=simple_strtol(s,&tmp,16);
2104 i=tmp-s;
2105 }
2106 if (!i) {
2107 if (*s) len--;
2108 break;
2109 }
2110 if (suffixlen+prefixlen<=12 && s[i]=='.') {
2111 tmp = in_aton(s);
2112 memcpy((struct in_addr*)(suffix+suffixlen), &tmp, sizeof(tmp));
2113 suffixlen+=4;
2114 len+=strlen(s);
2115 break;
2116 }
2117 suffix[suffixlen++] = (u >> 8);
2118 suffix[suffixlen++] = (u & 255);
2119 s += i; len += i;
2120 if (prefixlen+suffixlen==16)
2121 break;
2122 }
2123 for (i=0; i<suffixlen; i++)
2124 ip[16-suffixlen+i] = suffix[i];
2125 return len;
2126}
2127
2128static char tohex(char hexdigit) {
2129 return hexdigit>9?hexdigit+'a'-10:hexdigit+'0';
2130}
2131
2132static int fmt_xlong(char* s,unsigned int i) {
2133 char* bak=s;
2134 *s=tohex((i>>12)&0xf); if (s!=bak || *s!='0') ++s;
2135 *s=tohex((i>>8)&0xf); if (s!=bak || *s!='0') ++s;
2136 *s=tohex((i>>4)&0xf); if (s!=bak || *s!='0') ++s;
2137 *s=tohex(i&0xf);
2138 return s-bak+1;
2139}
2140
2141static unsigned int fmt_ip6(char *s,const char ip[16]) {
2142 unsigned int len;
2143 unsigned int i;
2144 unsigned int temp;
2145 unsigned int compressing;
2146 int j;
2147
2148 len = 0; compressing = 0;
2149 for (j=0; j<16; j+=2) {
2150
2151#ifdef V4MAPPEDPREFIX
2152 if (j==12 && !memcmp(ip,V4mappedprefix,12)) {
2153 inet_ntoa_r(*(struct in_addr*)(ip+12),s);
2154 temp=strlen(s);
2155 return len+temp;
2156 }
2157#endif
2158 temp = ((unsigned long) (unsigned char) ip[j] << 8) +
2159 (unsigned long) (unsigned char) ip[j+1];
2160 if (temp == 0) {
2161 if (!compressing) {
2162 compressing=1;
2163 if (j==0) {
2164 *s++=':'; ++len;
2165 }
2166 }
2167 } else {
2168 if (compressing) {
2169 compressing=0;
2170 *s++=':'; ++len;
2171 }
2172 i = fmt_xlong(s,temp); len += i; s += i;
2173 if (j<14) {
2174 *s++ = ':';
2175 ++len;
2176 }
2177 }
2178 }
2179 if (compressing) {
2180 *s++=':'; ++len;
2181 }
2182 *s=0;
2183 return len;
2184}
2185
2186static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2187 struct pktgen_dev *pkt_dev)
2188{
2189 struct sk_buff *skb = NULL;
2190 __u8 *eth;
2191 struct udphdr *udph;
2192 int datalen;
2193 struct ipv6hdr *iph;
2194 struct pktgen_hdr *pgh = NULL;
Robert Olsson64053be2005-06-26 15:27:10 -07002195
2196 /* Update any of the values, used when we're incrementing various
2197 * fields.
2198 */
2199 mod_cur_headers(pkt_dev);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
2202 if (!skb) {
2203 sprintf(pkt_dev->result, "No memory");
2204 return NULL;
2205 }
2206
2207 skb_reserve(skb, 16);
2208
2209 /* Reserve for ethernet and IP header */
2210 eth = (__u8 *) skb_push(skb, 14);
2211 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2212 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 memcpy(eth, pkt_dev->hh, 12);
2215 *(u16*)&eth[12] = __constant_htons(ETH_P_IPV6);
Robert Olsson64053be2005-06-26 15:27:10 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 datalen = pkt_dev->cur_pkt_size-14-
2218 sizeof(struct ipv6hdr)-sizeof(struct udphdr); /* Eth + IPh + UDPh */
2219
2220 if (datalen < sizeof(struct pktgen_hdr)) {
2221 datalen = sizeof(struct pktgen_hdr);
2222 if (net_ratelimit())
2223 printk(KERN_INFO "pktgen: increased datalen to %d\n", datalen);
2224 }
2225
2226 udph->source = htons(pkt_dev->cur_udp_src);
2227 udph->dest = htons(pkt_dev->cur_udp_dst);
2228 udph->len = htons(datalen + sizeof(struct udphdr));
2229 udph->check = 0; /* No checksum */
2230
2231 *(u32*)iph = __constant_htonl(0x60000000); /* Version + flow */
2232
2233 iph->hop_limit = 32;
2234
2235 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2236 iph->nexthdr = IPPROTO_UDP;
2237
2238 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2239 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2240
2241 skb->mac.raw = ((u8 *)iph) - 14;
2242 skb->protocol = __constant_htons(ETH_P_IPV6);
2243 skb->dev = odev;
2244 skb->pkt_type = PACKET_HOST;
2245
2246 if (pkt_dev->nfrags <= 0)
2247 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2248 else {
2249 int frags = pkt_dev->nfrags;
2250 int i;
2251
2252 pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
2253
2254 if (frags > MAX_SKB_FRAGS)
2255 frags = MAX_SKB_FRAGS;
2256 if (datalen > frags*PAGE_SIZE) {
2257 skb_put(skb, datalen-frags*PAGE_SIZE);
2258 datalen = frags*PAGE_SIZE;
2259 }
2260
2261 i = 0;
2262 while (datalen > 0) {
2263 struct page *page = alloc_pages(GFP_KERNEL, 0);
2264 skb_shinfo(skb)->frags[i].page = page;
2265 skb_shinfo(skb)->frags[i].page_offset = 0;
2266 skb_shinfo(skb)->frags[i].size =
2267 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2268 datalen -= skb_shinfo(skb)->frags[i].size;
2269 skb->len += skb_shinfo(skb)->frags[i].size;
2270 skb->data_len += skb_shinfo(skb)->frags[i].size;
2271 i++;
2272 skb_shinfo(skb)->nr_frags = i;
2273 }
2274
2275 while (i < frags) {
2276 int rem;
2277
2278 if (i == 0)
2279 break;
2280
2281 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2282 if (rem == 0)
2283 break;
2284
2285 skb_shinfo(skb)->frags[i - 1].size -= rem;
2286
2287 skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
2288 get_page(skb_shinfo(skb)->frags[i].page);
2289 skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
2290 skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
2291 skb_shinfo(skb)->frags[i].size = rem;
2292 i++;
2293 skb_shinfo(skb)->nr_frags = i;
2294 }
2295 }
2296
2297 /* Stamp the time, and sequence number, convert them to network byte order */
2298 /* should we update cloned packets too ? */
2299 if (pgh) {
2300 struct timeval timestamp;
2301
2302 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2303 pgh->seq_num = htonl(pkt_dev->seq_num);
2304
2305 do_gettimeofday(&timestamp);
2306 pgh->tv_sec = htonl(timestamp.tv_sec);
2307 pgh->tv_usec = htonl(timestamp.tv_usec);
2308 }
2309 pkt_dev->seq_num++;
2310
2311 return skb;
2312}
2313
2314static inline struct sk_buff *fill_packet(struct net_device *odev,
2315 struct pktgen_dev *pkt_dev)
2316{
2317 if(pkt_dev->flags & F_IPV6)
2318 return fill_packet_ipv6(odev, pkt_dev);
2319 else
2320 return fill_packet_ipv4(odev, pkt_dev);
2321}
2322
2323static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2324{
2325 pkt_dev->seq_num = 1;
2326 pkt_dev->idle_acc = 0;
2327 pkt_dev->sofar = 0;
2328 pkt_dev->tx_bytes = 0;
2329 pkt_dev->errors = 0;
2330}
2331
2332/* Set up structure for sending pkts, clear counters */
2333
2334static void pktgen_run(struct pktgen_thread *t)
2335{
2336 struct pktgen_dev *pkt_dev = NULL;
2337 int started = 0;
2338
2339 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
2340
2341 if_lock(t);
2342 for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) {
2343
2344 /*
2345 * setup odev and create initial packet.
2346 */
2347 pktgen_setup_inject(pkt_dev);
2348
2349 if(pkt_dev->odev) {
2350 pktgen_clear_counters(pkt_dev);
2351 pkt_dev->running = 1; /* Cranke yeself! */
2352 pkt_dev->skb = NULL;
2353 pkt_dev->started_at = getCurUs();
2354 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2355 pkt_dev->next_tx_ns = 0;
2356
2357 strcpy(pkt_dev->result, "Starting");
2358 started++;
2359 }
2360 else
2361 strcpy(pkt_dev->result, "Error starting");
2362 }
2363 if_unlock(t);
2364 if(started) t->control &= ~(T_STOP);
2365}
2366
2367static void pktgen_stop_all_threads_ifs(void)
2368{
2369 struct pktgen_thread *t = pktgen_threads;
2370
2371 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads.\n"));
2372
2373 thread_lock();
2374 while(t) {
2375 pktgen_stop(t);
2376 t = t->next;
2377 }
2378 thread_unlock();
2379}
2380
2381static int thread_is_running(struct pktgen_thread *t )
2382{
2383 struct pktgen_dev *next;
2384 int res = 0;
2385
2386 for(next=t->if_list; next; next=next->next) {
2387 if(next->running) {
2388 res = 1;
2389 break;
2390 }
2391 }
2392 return res;
2393}
2394
2395static int pktgen_wait_thread_run(struct pktgen_thread *t )
2396{
2397 if_lock(t);
2398
2399 while(thread_is_running(t)) {
2400
2401 if_unlock(t);
2402
2403 msleep_interruptible(100);
2404
2405 if (signal_pending(current))
2406 goto signal;
2407 if_lock(t);
2408 }
2409 if_unlock(t);
2410 return 1;
2411 signal:
2412 return 0;
2413}
2414
2415static int pktgen_wait_all_threads_run(void)
2416{
2417 struct pktgen_thread *t = pktgen_threads;
2418 int sig = 1;
2419
2420 while (t) {
2421 sig = pktgen_wait_thread_run(t);
2422 if( sig == 0 ) break;
2423 thread_lock();
2424 t=t->next;
2425 thread_unlock();
2426 }
2427 if(sig == 0) {
2428 thread_lock();
2429 while (t) {
2430 t->control |= (T_STOP);
2431 t=t->next;
2432 }
2433 thread_unlock();
2434 }
2435 return sig;
2436}
2437
2438static void pktgen_run_all_threads(void)
2439{
2440 struct pktgen_thread *t = pktgen_threads;
2441
2442 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2443
2444 thread_lock();
2445
2446 while(t) {
2447 t->control |= (T_RUN);
2448 t = t->next;
2449 }
2450 thread_unlock();
2451
2452 current->state = TASK_INTERRUPTIBLE;
2453 schedule_timeout(HZ/8); /* Propagate thread->control */
2454
2455 pktgen_wait_all_threads_run();
2456}
2457
2458
2459static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2460{
2461 __u64 total_us, bps, mbps, pps, idle;
2462 char *p = pkt_dev->result;
2463
2464 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
2465
2466 idle = pkt_dev->idle_acc;
2467
2468 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2469 (unsigned long long) total_us,
2470 (unsigned long long)(total_us - idle),
2471 (unsigned long long) idle,
2472 (unsigned long long) pkt_dev->sofar,
2473 pkt_dev->cur_pkt_size, nr_frags);
2474
2475 pps = pkt_dev->sofar * USEC_PER_SEC;
2476
2477 while ((total_us >> 32) != 0) {
2478 pps >>= 1;
2479 total_us >>= 1;
2480 }
2481
2482 do_div(pps, total_us);
2483
2484 bps = pps * 8 * pkt_dev->cur_pkt_size;
2485
2486 mbps = bps;
2487 do_div(mbps, 1000000);
2488 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2489 (unsigned long long) pps,
2490 (unsigned long long) mbps,
2491 (unsigned long long) bps,
2492 (unsigned long long) pkt_dev->errors);
2493}
2494
2495
2496/* Set stopped-at timer, remove from running list, do counters & statistics */
2497
2498static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
2499{
2500
2501 if (!pkt_dev->running) {
2502 printk("pktgen: interface: %s is already stopped\n", pkt_dev->ifname);
2503 return -EINVAL;
2504 }
2505
2506 pkt_dev->stopped_at = getCurUs();
2507 pkt_dev->running = 0;
2508
2509 show_results(pkt_dev, skb_shinfo(pkt_dev->skb)->nr_frags);
2510
2511 if (pkt_dev->skb)
2512 kfree_skb(pkt_dev->skb);
2513
2514 pkt_dev->skb = NULL;
2515
2516 return 0;
2517}
2518
2519static struct pktgen_dev *next_to_run(struct pktgen_thread *t )
2520{
2521 struct pktgen_dev *next, *best = NULL;
2522
2523 if_lock(t);
2524
2525 for(next=t->if_list; next ; next=next->next) {
2526 if(!next->running) continue;
2527 if(best == NULL) best=next;
2528 else if ( next->next_tx_us < best->next_tx_us)
2529 best = next;
2530 }
2531 if_unlock(t);
2532 return best;
2533}
2534
2535static void pktgen_stop(struct pktgen_thread *t) {
2536 struct pktgen_dev *next = NULL;
2537
2538 PG_DEBUG(printk("pktgen: entering pktgen_stop.\n"));
2539
2540 if_lock(t);
2541
2542 for(next=t->if_list; next; next=next->next)
2543 pktgen_stop_device(next);
2544
2545 if_unlock(t);
2546}
2547
2548static void pktgen_rem_all_ifs(struct pktgen_thread *t)
2549{
2550 struct pktgen_dev *cur, *next = NULL;
2551
2552 /* Remove all devices, free mem */
2553
2554 if_lock(t);
2555
2556 for(cur=t->if_list; cur; cur=next) {
2557 next = cur->next;
2558 pktgen_remove_device(t, cur);
2559 }
2560
2561 if_unlock(t);
2562}
2563
2564static void pktgen_rem_thread(struct pktgen_thread *t)
2565{
2566 /* Remove from the thread list */
2567
2568 struct pktgen_thread *tmp = pktgen_threads;
2569
2570 if (strlen(t->fname))
2571 remove_proc_entry(t->fname, NULL);
2572
2573 thread_lock();
2574
2575 if (tmp == t)
2576 pktgen_threads = tmp->next;
2577 else {
2578 while (tmp) {
2579 if (tmp->next == t) {
2580 tmp->next = t->next;
2581 t->next = NULL;
2582 break;
2583 }
2584 tmp = tmp->next;
2585 }
2586 }
2587 thread_unlock();
2588}
2589
2590static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
2591{
2592 struct net_device *odev = NULL;
2593 __u64 idle_start = 0;
2594 int ret;
2595
2596 odev = pkt_dev->odev;
2597
2598 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
2599 u64 now;
2600
2601 now = getCurUs();
2602 if (now < pkt_dev->next_tx_us)
2603 spin(pkt_dev, pkt_dev->next_tx_us);
2604
2605 /* This is max DELAY, this has special meaning of
2606 * "never transmit"
2607 */
2608 if (pkt_dev->delay_us == 0x7FFFFFFF) {
2609 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
2610 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
2611 goto out;
2612 }
2613 }
2614
2615 if (netif_queue_stopped(odev) || need_resched()) {
2616 idle_start = getCurUs();
2617
2618 if (!netif_running(odev)) {
2619 pktgen_stop_device(pkt_dev);
2620 goto out;
2621 }
2622 if (need_resched())
2623 schedule();
2624
2625 pkt_dev->idle_acc += getCurUs() - idle_start;
2626
2627 if (netif_queue_stopped(odev)) {
2628 pkt_dev->next_tx_us = getCurUs(); /* TODO */
2629 pkt_dev->next_tx_ns = 0;
2630 goto out; /* Try the next interface */
2631 }
2632 }
2633
2634 if (pkt_dev->last_ok || !pkt_dev->skb) {
2635 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb ) || (!pkt_dev->skb)) {
2636 /* build a new pkt */
2637 if (pkt_dev->skb)
2638 kfree_skb(pkt_dev->skb);
2639
2640 pkt_dev->skb = fill_packet(odev, pkt_dev);
2641 if (pkt_dev->skb == NULL) {
2642 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
2643 schedule();
2644 pkt_dev->clone_count--; /* back out increment, OOM */
2645 goto out;
2646 }
2647 pkt_dev->allocated_skbs++;
2648 pkt_dev->clone_count = 0; /* reset counter */
2649 }
2650 }
2651
2652 spin_lock_bh(&odev->xmit_lock);
2653 if (!netif_queue_stopped(odev)) {
2654
2655 atomic_inc(&(pkt_dev->skb->users));
2656retry_now:
2657 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
2658 if (likely(ret == NETDEV_TX_OK)) {
2659 pkt_dev->last_ok = 1;
2660 pkt_dev->sofar++;
2661 pkt_dev->seq_num++;
2662 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
2663
2664 } else if (ret == NETDEV_TX_LOCKED
2665 && (odev->features & NETIF_F_LLTX)) {
2666 cpu_relax();
2667 goto retry_now;
2668 } else { /* Retry it next time */
2669
2670 atomic_dec(&(pkt_dev->skb->users));
2671
2672 if (debug && net_ratelimit())
2673 printk(KERN_INFO "pktgen: Hard xmit error\n");
2674
2675 pkt_dev->errors++;
2676 pkt_dev->last_ok = 0;
2677 }
2678
2679 pkt_dev->next_tx_us = getCurUs();
2680 pkt_dev->next_tx_ns = 0;
2681
2682 pkt_dev->next_tx_us += pkt_dev->delay_us;
2683 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
2684
2685 if (pkt_dev->next_tx_ns > 1000) {
2686 pkt_dev->next_tx_us++;
2687 pkt_dev->next_tx_ns -= 1000;
2688 }
2689 }
2690
2691 else { /* Retry it next time */
2692 pkt_dev->last_ok = 0;
2693 pkt_dev->next_tx_us = getCurUs(); /* TODO */
2694 pkt_dev->next_tx_ns = 0;
2695 }
2696
2697 spin_unlock_bh(&odev->xmit_lock);
2698
2699 /* If pkt_dev->count is zero, then run forever */
2700 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
2701 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
2702 idle_start = getCurUs();
2703 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
2704 if (signal_pending(current)) {
2705 break;
2706 }
2707 schedule();
2708 }
2709 pkt_dev->idle_acc += getCurUs() - idle_start;
2710 }
2711
2712 /* Done with this */
2713 pktgen_stop_device(pkt_dev);
2714 }
2715 out:;
2716 }
2717
2718/*
2719 * Main loop of the thread goes here
2720 */
2721
2722static void pktgen_thread_worker(struct pktgen_thread *t)
2723{
2724 DEFINE_WAIT(wait);
2725 struct pktgen_dev *pkt_dev = NULL;
2726 int cpu = t->cpu;
2727 sigset_t tmpsig;
2728 u32 max_before_softirq;
2729 u32 tx_since_softirq = 0;
2730
2731 daemonize("pktgen/%d", cpu);
2732
2733 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
2734
2735 spin_lock_irq(&current->sighand->siglock);
2736 tmpsig = current->blocked;
2737 siginitsetinv(&current->blocked,
2738 sigmask(SIGKILL) |
2739 sigmask(SIGSTOP)|
2740 sigmask(SIGTERM));
2741
2742 recalc_sigpending();
2743 spin_unlock_irq(&current->sighand->siglock);
2744
2745 /* Migrate to the right CPU */
2746 set_cpus_allowed(current, cpumask_of_cpu(cpu));
2747 if (smp_processor_id() != cpu)
2748 BUG();
2749
2750 init_waitqueue_head(&t->queue);
2751
2752 t->control &= ~(T_TERMINATE);
2753 t->control &= ~(T_RUN);
2754 t->control &= ~(T_STOP);
2755 t->control &= ~(T_REMDEV);
2756
2757 t->pid = current->pid;
2758
2759 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
2760
2761 max_before_softirq = t->max_before_softirq;
2762
2763 __set_current_state(TASK_INTERRUPTIBLE);
2764 mb();
2765
2766 while (1) {
2767
2768 __set_current_state(TASK_RUNNING);
2769
2770 /*
2771 * Get next dev to xmit -- if any.
2772 */
2773
2774 pkt_dev = next_to_run(t);
2775
2776 if (pkt_dev) {
2777
2778 pktgen_xmit(pkt_dev);
2779
2780 /*
2781 * We like to stay RUNNING but must also give
2782 * others fair share.
2783 */
2784
2785 tx_since_softirq += pkt_dev->last_ok;
2786
2787 if (tx_since_softirq > max_before_softirq) {
2788 if (local_softirq_pending())
2789 do_softirq();
2790 tx_since_softirq = 0;
2791 }
2792 } else {
2793 prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
2794 schedule_timeout(HZ/10);
2795 finish_wait(&(t->queue), &wait);
2796 }
2797
2798 /*
2799 * Back from sleep, either due to the timeout or signal.
2800 * We check if we have any "posted" work for us.
2801 */
2802
2803 if (t->control & T_TERMINATE || signal_pending(current))
2804 /* we received a request to terminate ourself */
2805 break;
2806
2807
2808 if(t->control & T_STOP) {
2809 pktgen_stop(t);
2810 t->control &= ~(T_STOP);
2811 }
2812
2813 if(t->control & T_RUN) {
2814 pktgen_run(t);
2815 t->control &= ~(T_RUN);
2816 }
2817
2818 if(t->control & T_REMDEV) {
2819 pktgen_rem_all_ifs(t);
2820 t->control &= ~(T_REMDEV);
2821 }
2822
2823 if (need_resched())
2824 schedule();
2825 }
2826
2827 PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
2828 pktgen_stop(t);
2829
2830 PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
2831 pktgen_rem_all_ifs(t);
2832
2833 PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
2834 pktgen_rem_thread(t);
2835}
2836
2837static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, const char* ifname)
2838{
2839 struct pktgen_dev *pkt_dev = NULL;
2840 if_lock(t);
2841
2842 for(pkt_dev=t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) {
2843 if (strcmp(pkt_dev->ifname, ifname) == 0) {
2844 break;
2845 }
2846 }
2847
2848 if_unlock(t);
2849 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname,pkt_dev));
2850 return pkt_dev;
2851}
2852
2853/*
2854 * Adds a dev at front of if_list.
2855 */
2856
2857static int add_dev_to_thread(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
2858{
2859 int rv = 0;
2860
2861 if_lock(t);
2862
2863 if (pkt_dev->pg_thread) {
2864 printk("pktgen: ERROR: already assigned to a thread.\n");
2865 rv = -EBUSY;
2866 goto out;
2867 }
2868 pkt_dev->next =t->if_list; t->if_list=pkt_dev;
2869 pkt_dev->pg_thread = t;
2870 pkt_dev->running = 0;
2871
2872 out:
2873 if_unlock(t);
2874 return rv;
2875}
2876
2877/* Called under thread lock */
2878
2879static int pktgen_add_device(struct pktgen_thread *t, const char* ifname)
2880{
2881 struct pktgen_dev *pkt_dev;
2882
2883 /* We don't allow a device to be on several threads */
2884
2885 if( (pkt_dev = __pktgen_NN_threads(ifname, FIND)) == NULL) {
2886
2887 pkt_dev = kmalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
2888 if (!pkt_dev)
2889 return -ENOMEM;
2890
2891 memset(pkt_dev, 0, sizeof(struct pktgen_dev));
2892
2893 pkt_dev->flows = vmalloc(MAX_CFLOWS*sizeof(struct flow_state));
2894 if (pkt_dev->flows == NULL) {
2895 kfree(pkt_dev);
2896 return -ENOMEM;
2897 }
2898 memset(pkt_dev->flows, 0, MAX_CFLOWS*sizeof(struct flow_state));
2899
2900 pkt_dev->min_pkt_size = ETH_ZLEN;
2901 pkt_dev->max_pkt_size = ETH_ZLEN;
2902 pkt_dev->nfrags = 0;
2903 pkt_dev->clone_skb = pg_clone_skb_d;
2904 pkt_dev->delay_us = pg_delay_d / 1000;
2905 pkt_dev->delay_ns = pg_delay_d % 1000;
2906 pkt_dev->count = pg_count_d;
2907 pkt_dev->sofar = 0;
2908 pkt_dev->udp_src_min = 9; /* sink port */
2909 pkt_dev->udp_src_max = 9;
2910 pkt_dev->udp_dst_min = 9;
2911 pkt_dev->udp_dst_max = 9;
2912
2913 strncpy(pkt_dev->ifname, ifname, 31);
2914 sprintf(pkt_dev->fname, "net/%s/%s", PG_PROC_DIR, ifname);
2915
2916 if (! pktgen_setup_dev(pkt_dev)) {
2917 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
2918 if (pkt_dev->flows)
2919 vfree(pkt_dev->flows);
2920 kfree(pkt_dev);
2921 return -ENODEV;
2922 }
2923
2924 pkt_dev->proc_ent = create_proc_entry(pkt_dev->fname, 0600, NULL);
2925 if (!pkt_dev->proc_ent) {
2926 printk("pktgen: cannot create %s procfs entry.\n", pkt_dev->fname);
2927 if (pkt_dev->flows)
2928 vfree(pkt_dev->flows);
2929 kfree(pkt_dev);
2930 return -EINVAL;
2931 }
2932 pkt_dev->proc_ent->read_proc = proc_if_read;
2933 pkt_dev->proc_ent->write_proc = proc_if_write;
2934 pkt_dev->proc_ent->data = (void*)(pkt_dev);
2935 pkt_dev->proc_ent->owner = THIS_MODULE;
2936
2937 return add_dev_to_thread(t, pkt_dev);
2938 }
2939 else {
2940 printk("pktgen: ERROR: interface already used.\n");
2941 return -EBUSY;
2942 }
2943}
2944
2945static struct pktgen_thread *pktgen_find_thread(const char* name)
2946{
2947 struct pktgen_thread *t = NULL;
2948
2949 thread_lock();
2950
2951 t = pktgen_threads;
2952 while (t) {
2953 if (strcmp(t->name, name) == 0)
2954 break;
2955
2956 t = t->next;
2957 }
2958 thread_unlock();
2959 return t;
2960}
2961
2962static int pktgen_create_thread(const char* name, int cpu)
2963{
2964 struct pktgen_thread *t = NULL;
2965
2966 if (strlen(name) > 31) {
2967 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
2968 return -EINVAL;
2969 }
2970
2971 if (pktgen_find_thread(name)) {
2972 printk("pktgen: ERROR: thread: %s already exists\n", name);
2973 return -EINVAL;
2974 }
2975
2976 t = (struct pktgen_thread*)(kmalloc(sizeof(struct pktgen_thread), GFP_KERNEL));
2977 if (!t) {
2978 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
2979 return -ENOMEM;
2980 }
2981
2982 memset(t, 0, sizeof(struct pktgen_thread));
2983 strcpy(t->name, name);
2984 spin_lock_init(&t->if_lock);
2985 t->cpu = cpu;
2986
2987 sprintf(t->fname, "net/%s/%s", PG_PROC_DIR, t->name);
2988 t->proc_ent = create_proc_entry(t->fname, 0600, NULL);
2989 if (!t->proc_ent) {
2990 printk("pktgen: cannot create %s procfs entry.\n", t->fname);
2991 kfree(t);
2992 return -EINVAL;
2993 }
2994 t->proc_ent->read_proc = proc_thread_read;
2995 t->proc_ent->write_proc = proc_thread_write;
2996 t->proc_ent->data = (void*)(t);
2997 t->proc_ent->owner = THIS_MODULE;
2998
2999 t->next = pktgen_threads;
3000 pktgen_threads = t;
3001
3002 if (kernel_thread((void *) pktgen_thread_worker, (void *) t,
3003 CLONE_FS | CLONE_FILES | CLONE_SIGHAND) < 0)
3004 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3005
3006 return 0;
3007}
3008
3009/*
3010 * Removes a device from the thread if_list.
3011 */
3012static void _rem_dev_from_if_list(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
3013{
3014 struct pktgen_dev *i, *prev = NULL;
3015
3016 i = t->if_list;
3017
3018 while(i) {
3019 if(i == pkt_dev) {
3020 if(prev) prev->next = i->next;
3021 else t->if_list = NULL;
3022 break;
3023 }
3024 prev = i;
3025 i=i->next;
3026 }
3027}
3028
3029static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
3030{
3031
3032 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
3033
3034 if (pkt_dev->running) {
3035 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3036 pktgen_stop_device(pkt_dev);
3037 }
3038
3039 /* Dis-associate from the interface */
3040
3041 if (pkt_dev->odev) {
3042 dev_put(pkt_dev->odev);
3043 pkt_dev->odev = NULL;
3044 }
3045
3046 /* And update the thread if_list */
3047
3048 _rem_dev_from_if_list(t, pkt_dev);
3049
3050 /* Clean up proc file system */
3051
3052 if (strlen(pkt_dev->fname))
3053 remove_proc_entry(pkt_dev->fname, NULL);
3054
3055 if (pkt_dev->flows)
3056 vfree(pkt_dev->flows);
3057 kfree(pkt_dev);
3058 return 0;
3059}
3060
3061static int __init pg_init(void)
3062{
3063 int cpu;
3064 printk(version);
3065
3066 module_fname[0] = 0;
3067
3068 create_proc_dir();
3069
3070 sprintf(module_fname, "net/%s/pgctrl", PG_PROC_DIR);
3071 module_proc_ent = create_proc_entry(module_fname, 0600, NULL);
3072 if (!module_proc_ent) {
3073 printk("pktgen: ERROR: cannot create %s procfs entry.\n", module_fname);
3074 return -EINVAL;
3075 }
3076
3077 module_proc_ent->proc_fops = &pktgen_fops;
3078 module_proc_ent->data = NULL;
3079
3080 /* Register us to receive netdevice events */
3081 register_netdevice_notifier(&pktgen_notifier_block);
3082
3083 for (cpu = 0; cpu < NR_CPUS ; cpu++) {
3084 char buf[30];
3085
3086 if (!cpu_online(cpu))
3087 continue;
3088
3089 sprintf(buf, "kpktgend_%i", cpu);
3090 pktgen_create_thread(buf, cpu);
3091 }
3092 return 0;
3093}
3094
3095static void __exit pg_cleanup(void)
3096{
3097 wait_queue_head_t queue;
3098 init_waitqueue_head(&queue);
3099
3100 /* Stop all interfaces & threads */
3101
3102 while (pktgen_threads) {
3103 struct pktgen_thread *t = pktgen_threads;
3104 pktgen_threads->control |= (T_TERMINATE);
3105
3106 wait_event_interruptible_timeout(queue, (t != pktgen_threads), HZ);
3107 }
3108
3109 /* Un-register us from receiving netdevice events */
3110 unregister_netdevice_notifier(&pktgen_notifier_block);
3111
3112 /* Clean up proc file system */
3113
3114 remove_proc_entry(module_fname, NULL);
3115
3116 remove_proc_dir();
3117}
3118
3119
3120module_init(pg_init);
3121module_exit(pg_cleanup);
3122
3123MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3124MODULE_DESCRIPTION("Packet Generator tool");
3125MODULE_LICENSE("GPL");
3126module_param(pg_count_d, int, 0);
3127module_param(pg_delay_d, int, 0);
3128module_param(pg_clone_skb_d, int, 0);
3129module_param(debug, int, 0);