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