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