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