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