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