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