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