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