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