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