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