2 * $Id: pktgen.c,v 1.8 2002/07/15 19:30:17 robert Exp $
3 * pktgen.c: Packet Generator for performance evaluation.
5 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
6 * Uppsala University, Sweden
8 * A tool for loading the network with preconfigurated packets.
9 * The tool is implemented as a linux module. Parameters are output
10 * device, IPG (interpacket gap), number of packets, and whether
11 * to use multiple SKBs or just the same one.
12 * pktgen uses the installed interface's output routine.
14 * Additional hacking by:
16 * Jens.Laas@data.slu.se
17 * Improved by ANK. 010120.
18 * Improved by ANK even more. 010212.
19 * MAC address typo fixed. 010417 --ro
20 * Integrated. 020301 --DaveM
21 * Added multiskb option 020301 --DaveM
22 * Scaling of results. 020417--sigurdur@linpro.no
23 * Significant re-work of the module:
24 * * Updated to support generation over multiple interfaces at once
25 * by creating 32 /proc/net/pg* files. Each file can be manipulated
27 * * Converted many counters to __u64 to allow longer runs.
28 * * Allow configuration of ranges, like min/max IP address, MACs,
29 * and UDP-ports, for both source and destination, and can
30 * set to use a random distribution or sequentially walk the range.
31 * * Can now change some values after starting.
32 * * Place 12-byte packet in UDP payload with magic number,
33 * sequence number, and timestamp. Will write receiver next.
34 * * The new changes seem to have a performance impact of around 1%,
35 * as far as I can tell.
36 * --Ben Greear <greearb@candelatech.com>
37 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
39 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
40 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
41 * as a "fastpath" with a configurable number of clones after alloc's.
43 * clone_skb=0 means all packets are allocated this also means ranges time
44 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
47 * Also moved to /proc/net/pktgen/
50 * Fix refcount off by one if first packet fails, potential null deref,
53 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
55 * See Documentation/networking/pktgen.txt for how to use this.
58 #include <linux/module.h>
59 #include <linux/kernel.h>
60 #include <linux/sched.h>
61 #include <linux/types.h>
62 #include <linux/string.h>
63 #include <linux/ptrace.h>
64 #include <linux/errno.h>
65 #include <linux/ioport.h>
66 #include <linux/slab.h>
67 #include <linux/interrupt.h>
68 #include <linux/pci.h>
69 #include <linux/delay.h>
70 #include <linux/init.h>
71 #include <linux/inet.h>
72 #include <asm/byteorder.h>
73 #include <asm/bitops.h>
76 #include <asm/uaccess.h>
80 #include <linux/udp.h>
81 #include <linux/skbuff.h>
82 #include <linux/netdevice.h>
83 #include <linux/inetdevice.h>
84 #include <linux/rtnetlink.h>
85 #include <linux/proc_fs.h>
86 #include <linux/if_arp.h>
87 #include <net/checksum.h>
88 #include <asm/timex.h>
90 #define cycles() ((u32)get_cycles())
93 #define VERSION "pktgen version 1.32"
94 static char version[] __initdata =
95 "pktgen.c: v1.3: Packet Generator for packet performance testing.\n";
97 /* Used to help with determining the pkts on receive */
99 #define PKTGEN_MAGIC 0xbe9be955
102 /* Keep information per interface */
106 /* If min != max, then we will either do a linear iteration, or
107 * we will do a random selection from within the range.
111 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
112 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
113 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
114 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
115 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
116 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
117 #define F_SET_SRCMAC (1<<6) /* Specify-Src-Mac
118 (default is to use Interface's MAC Addr) */
119 #define F_SET_SRCIP (1<<7) /* Specify-Src-IP
120 (default is to use Interface's IP Addr) */
123 int pkt_size; /* = ETH_ZLEN; */
125 __u32 ipg; /* Default Interpacket gap in nsec */
126 __u64 count; /* Default No packets to send */
127 __u64 sofar; /* How many pkts we've sent so far */
128 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
129 struct timeval started_at;
130 struct timeval stopped_at;
134 int clone_skb; /* Use multiple SKBs during packet gen. If this number
135 * is greater than 1, then that many coppies of the same
136 * packet will be sent before a new packet is allocated.
137 * For instance, if you want to send 1024 identical packets
138 * before creating a new packet, set clone_skb to 1024.
141 int do_run_run; /* if this changes to false, the test will stop */
149 /* If we're doing ranges, random or incremental, then this
150 * defines the min/max for those ranges.
152 __u32 saddr_min; /* inclusive, source IP address */
153 __u32 saddr_max; /* exclusive, source IP address */
154 __u32 daddr_min; /* inclusive, dest IP address */
155 __u32 daddr_max; /* exclusive, dest IP address */
157 __u16 udp_src_min; /* inclusive, source UDP port */
158 __u16 udp_src_max; /* exclusive, source UDP port */
159 __u16 udp_dst_min; /* inclusive, dest UDP port */
160 __u16 udp_dst_max; /* exclusive, dest UDP port */
162 __u32 src_mac_count; /* How many MACs to iterate through */
163 __u32 dst_mac_count; /* How many MACs to iterate through */
165 unsigned char dst_mac[6];
166 unsigned char src_mac[6];
168 __u32 cur_dst_mac_offset;
169 __u32 cur_src_mac_offset;
177 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
179 We fill in SRC address later
180 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
184 __u16 pad; /* pad out the hh struct to an even 16 bytes */
187 /* proc file names */
191 struct proc_dir_entry *proc_ent;
192 struct proc_dir_entry *busy_proc_ent;
202 static int cpu_speed;
205 /* Module parameters, defaults. */
206 static int count_d = 100000;
208 static int clone_skb_d;
212 static struct pktgen_info pginfos[MAX_PKTGEN];
215 /** Convert to miliseconds */
216 static inline __u64 tv_to_ms(const struct timeval* tv) {
217 __u64 ms = tv->tv_usec / 1000;
218 ms += (__u64)tv->tv_sec * (__u64)1000;
222 static inline __u64 getCurMs(void) {
224 do_gettimeofday(&tv);
225 return tv_to_ms(&tv);
228 #define PG_PROC_DIR "pktgen"
229 static struct proc_dir_entry *proc_dir;
231 static struct net_device *setup_inject(struct pktgen_info* info)
233 struct net_device *odev;
235 odev = dev_get_by_name(info->outdev);
237 sprintf(info->result, "No such netdevice: \"%s\"", info->outdev);
241 if (odev->type != ARPHRD_ETHER) {
242 sprintf(info->result, "Not ethernet device: \"%s\"", info->outdev);
246 if (!netif_running(odev)) {
247 sprintf(info->result, "Device is down: \"%s\"", info->outdev);
251 /* Default to the interface's mac if not explicitly set. */
252 if (!(info->flags & F_SET_SRCMAC)) {
253 memcpy(&(info->hh[6]), odev->dev_addr, 6);
256 memcpy(&(info->hh[6]), info->src_mac, 6);
259 /* Set up Dest MAC */
260 memcpy(&(info->hh[0]), info->dst_mac, 6);
264 if (strlen(info->src_min) == 0) {
265 struct in_device *in_dev = in_dev_get(odev);
267 if (in_dev->ifa_list) {
268 info->saddr_min = in_dev->ifa_list->ifa_address;
269 info->saddr_max = info->saddr_min;
275 info->saddr_min = in_aton(info->src_min);
276 info->saddr_max = in_aton(info->src_max);
279 info->daddr_min = in_aton(info->dst_min);
280 info->daddr_max = in_aton(info->dst_max);
282 /* Initialize current values. */
283 info->cur_dst_mac_offset = 0;
284 info->cur_src_mac_offset = 0;
285 info->cur_saddr = info->saddr_min;
286 info->cur_daddr = info->daddr_min;
287 info->cur_udp_dst = info->udp_dst_min;
288 info->cur_udp_src = info->udp_src_min;
298 static void nanospin(int ipg, struct pktgen_info* info)
300 u32 idle_start, idle;
302 idle_start = cycles();
306 idle = cycles() - idle_start;
307 if (idle * 1000 >= ipg * cpu_speed)
310 info->idle_acc += idle;
313 static int calc_mhz(void)
315 struct timeval start, stop;
316 u32 start_s, elapsed;
318 do_gettimeofday(&start);
322 elapsed = cycles() - start_s;
325 } while (elapsed < 1000 * 50000);
326 do_gettimeofday(&stop);
327 return elapsed/(stop.tv_usec-start.tv_usec+1000000*(stop.tv_sec-start.tv_sec));
330 static void cycles_calibrate(void)
334 for (i = 0; i < 3; i++) {
335 int res = calc_mhz();
342 /* Increment/randomize headers according to flags and current values
343 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
345 static void mod_cur_headers(struct pktgen_info* info) {
349 /* Deal with source MAC */
350 if (info->src_mac_count > 1) {
353 if (info->flags & F_MACSRC_RND) {
354 mc = net_random() % (info->src_mac_count);
357 mc = info->cur_src_mac_offset++;
358 if (info->cur_src_mac_offset > info->src_mac_count) {
359 info->cur_src_mac_offset = 0;
363 tmp = info->src_mac[5] + (mc & 0xFF);
365 tmp = (info->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
367 tmp = (info->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
369 tmp = (info->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
371 tmp = (info->src_mac[1] + (tmp >> 8));
375 /* Deal with Destination MAC */
376 if (info->dst_mac_count > 1) {
379 if (info->flags & F_MACDST_RND) {
380 mc = net_random() % (info->dst_mac_count);
383 mc = info->cur_dst_mac_offset++;
384 if (info->cur_dst_mac_offset > info->dst_mac_count) {
385 info->cur_dst_mac_offset = 0;
389 tmp = info->dst_mac[5] + (mc & 0xFF);
391 tmp = (info->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
393 tmp = (info->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
395 tmp = (info->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
397 tmp = (info->dst_mac[1] + (tmp >> 8));
401 if (info->udp_src_min < info->udp_src_max) {
402 if (info->flags & F_UDPSRC_RND) {
403 info->cur_udp_src = ((net_random() % (info->udp_src_max - info->udp_src_min))
404 + info->udp_src_min);
408 if (info->cur_udp_src >= info->udp_src_max) {
409 info->cur_udp_src = info->udp_src_min;
414 if (info->udp_dst_min < info->udp_dst_max) {
415 if (info->flags & F_UDPDST_RND) {
416 info->cur_udp_dst = ((net_random() % (info->udp_dst_max - info->udp_dst_min))
417 + info->udp_dst_min);
421 if (info->cur_udp_dst >= info->udp_dst_max) {
422 info->cur_udp_dst = info->udp_dst_min;
427 if ((imn = ntohl(info->saddr_min)) < (imx = ntohl(info->saddr_max))) {
429 if (info->flags & F_IPSRC_RND) {
430 t = ((net_random() % (imx - imn)) + imn);
433 t = ntohl(info->cur_saddr);
439 info->cur_saddr = htonl(t);
442 if ((imn = ntohl(info->daddr_min)) < (imx = ntohl(info->daddr_max))) {
444 if (info->flags & F_IPDST_RND) {
445 t = ((net_random() % (imx - imn)) + imn);
448 t = ntohl(info->cur_daddr);
454 info->cur_daddr = htonl(t);
456 }/* mod_cur_headers */
459 static struct sk_buff *fill_packet(struct net_device *odev, struct pktgen_info* info)
461 struct sk_buff *skb = NULL;
466 struct pktgen_hdr *pgh = NULL;
468 skb = alloc_skb(info->pkt_size + 64 + 16, GFP_ATOMIC);
470 sprintf(info->result, "No memory");
474 skb_reserve(skb, 16);
476 /* Reserve for ethernet and IP header */
477 eth = (__u8 *) skb_push(skb, 14);
478 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
479 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
481 /* Update any of the values, used when we're incrementing various
484 mod_cur_headers(info);
486 memcpy(eth, info->hh, 14);
488 datalen = info->pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */
489 if (datalen < sizeof(struct pktgen_hdr)) {
490 datalen = sizeof(struct pktgen_hdr);
493 udph->source = htons(info->cur_udp_src);
494 udph->dest = htons(info->cur_udp_dst);
495 udph->len = htons(datalen + 8); /* DATA + udphdr */
496 udph->check = 0; /* No checksum */
502 iph->protocol = IPPROTO_UDP; /* UDP */
503 iph->saddr = info->cur_saddr;
504 iph->daddr = info->cur_daddr;
506 iplen = 20 + 8 + datalen;
507 iph->tot_len = htons(iplen);
509 iph->check = ip_fast_csum((void *) iph, iph->ihl);
510 skb->protocol = __constant_htons(ETH_P_IP);
511 skb->mac.raw = ((u8 *)iph) - 14;
513 skb->pkt_type = PACKET_HOST;
515 if (info->nfrags <= 0) {
516 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
518 int frags = info->nfrags;
521 /* TODO: Verify this is OK...it sure is ugly. --Ben */
522 pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
524 if (frags > MAX_SKB_FRAGS)
525 frags = MAX_SKB_FRAGS;
526 if (datalen > frags*PAGE_SIZE) {
527 skb_put(skb, datalen-frags*PAGE_SIZE);
528 datalen = frags*PAGE_SIZE;
532 while (datalen > 0) {
533 struct page *page = alloc_pages(GFP_KERNEL, 0);
534 skb_shinfo(skb)->frags[i].page = page;
535 skb_shinfo(skb)->frags[i].page_offset = 0;
536 skb_shinfo(skb)->frags[i].size =
537 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
538 datalen -= skb_shinfo(skb)->frags[i].size;
539 skb->len += skb_shinfo(skb)->frags[i].size;
540 skb->data_len += skb_shinfo(skb)->frags[i].size;
542 skb_shinfo(skb)->nr_frags = i;
551 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
555 skb_shinfo(skb)->frags[i - 1].size -= rem;
557 skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
558 get_page(skb_shinfo(skb)->frags[i].page);
559 skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
560 skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
561 skb_shinfo(skb)->frags[i].size = rem;
563 skb_shinfo(skb)->nr_frags = i;
567 /* Stamp the time, and sequence number, convert them to network byte order */
569 struct timeval timestamp;
571 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
572 pgh->seq_num = htonl(info->seq_num);
574 do_gettimeofday(×tamp);
575 pgh->tv_sec = htonl(timestamp.tv_sec);
576 pgh->tv_usec = htonl(timestamp.tv_usec);
583 static void inject(struct pktgen_info* info)
585 struct net_device *odev = NULL;
586 struct sk_buff *skb = NULL;
591 int last_ok = 1; /* Was last skb sent?
592 * Or a failed transmit of some sort? This will keep
593 * sequence numbers in order, for example.
598 odev = setup_inject(info);
602 info->do_run_run = 1; /* Cranke yeself! */
605 lcount = info->count;
608 /* Build our initial pkt and place it as a re-try pkt. */
609 skb = fill_packet(odev, info);
610 if (skb == NULL) goto out_reldev;
612 do_gettimeofday(&(info->started_at));
614 while(info->do_run_run) {
616 /* Set a time-stamp, so build a new pkt each time */
619 if (++fp_tmp >= info->clone_skb ) {
621 skb = fill_packet(odev, info);
626 fp_tmp = 0; /* reset counter */
630 nr_frags = skb_shinfo(skb)->nr_frags;
632 spin_lock_bh(&odev->xmit_lock);
633 if (!netif_queue_stopped(odev)) {
635 atomic_inc(&skb->users);
637 if (odev->hard_start_xmit(skb, odev)) {
639 atomic_dec(&skb->users);
640 if (net_ratelimit()) {
641 printk(KERN_INFO "Hard xmit error\n");
653 /* Re-try it next time */
658 spin_unlock_bh(&odev->xmit_lock);
661 /* Try not to busy-spin if we have larger sleep times.
662 * TODO: Investigate better ways to do this.
664 if (info->ipg < 10000) { /* 10 usecs or less */
665 nanospin(info->ipg, info);
667 else if (info->ipg < 10000000) { /* 10ms or less */
668 udelay(info->ipg / 1000);
671 mdelay(info->ipg / 1000000);
675 if (signal_pending(current)) {
679 /* If lcount is zero, then run forever */
680 if ((lcount != 0) && (--lcount == 0)) {
681 if (atomic_read(&skb->users) != 1) {
682 u32 idle_start, idle;
684 idle_start = cycles();
685 while (atomic_read(&skb->users) != 1) {
686 if (signal_pending(current)) {
691 idle = cycles() - idle_start;
692 info->idle_acc += idle;
697 if (netif_queue_stopped(odev) || need_resched()) {
698 u32 idle_start, idle;
700 idle_start = cycles();
702 if (signal_pending(current)) {
703 info->do_run_run = 0;
706 if (!netif_running(odev)) {
707 info->do_run_run = 0;
714 } while (netif_queue_stopped(odev));
715 idle = cycles() - idle_start;
716 info->idle_acc += idle;
718 }/* while we should be running */
720 do_gettimeofday(&(info->stopped_at));
722 total = (info->stopped_at.tv_sec - info->started_at.tv_sec) * 1000000 +
723 info->stopped_at.tv_usec - info->started_at.tv_usec;
725 idle = (__u32)(info->idle_acc)/(__u32)(cpu_speed);
728 char *p = info->result;
732 pps = (__u32)(info->sofar * 1000) / ((__u32)(total) / 1000);
734 pps = (__u32)(info->sofar * 10000) / ((__u32)(total) / 100);
736 pps = (__u32)(info->sofar * 100000) / ((__u32)(total) / 10);
738 pps = (__u32)(info->sofar * 1000000) / (__u32)total;
740 bps = pps * 8 * (info->pkt_size + 4); /* take 32bit ethernet CRC into account */
741 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags) %llupps %lluMb/sec (%llubps) errors: %llu",
742 (unsigned long long) total,
743 (unsigned long long) (total - idle),
744 (unsigned long long) idle,
745 (unsigned long long) info->sofar,
746 skb->len + 4, /* Add 4 to account for the ethernet checksum */
748 (unsigned long long) pps,
749 (unsigned long long) (bps / (u64) 1024 / (u64) 1024),
750 (unsigned long long) bps,
751 (unsigned long long) info->errors
767 /* proc/net/pktgen/pg */
769 static int proc_busy_read(char *buf , char **start, off_t offset,
770 int len, int *eof, void *data)
773 int idx = (int)(long)(data);
774 struct pktgen_info* info = NULL;
776 if ((idx < 0) || (idx >= MAX_PKTGEN)) {
777 printk("ERROR: idx: %i is out of range in proc_write\n", idx);
780 info = &(pginfos[idx]);
783 p += sprintf(p, "%d\n", info->busy);
789 static int proc_read(char *buf , char **start, off_t offset,
790 int len, int *eof, void *data)
794 int idx = (int)(long)(data);
795 struct pktgen_info* info = NULL;
798 __u64 now = getCurMs();
800 if ((idx < 0) || (idx >= MAX_PKTGEN)) {
801 printk("ERROR: idx: %i is out of range in proc_write\n", idx);
804 info = &(pginfos[idx]);
807 p += sprintf(p, "%s\n", VERSION); /* Help with parsing compatibility */
808 p += sprintf(p, "Params: count %llu pkt_size: %u frags: %d ipg: %u clone_skb: %d odev \"%s\"\n",
809 (unsigned long long) info->count,
810 info->pkt_size, info->nfrags, info->ipg,
811 info->clone_skb, info->outdev);
812 p += sprintf(p, " dst_min: %s dst_max: %s src_min: %s src_max: %s\n",
813 info->dst_min, info->dst_max, info->src_min, info->src_max);
814 p += sprintf(p, " src_mac: ");
815 for (i = 0; i < 6; i++) {
816 p += sprintf(p, "%02X%s", info->src_mac[i], i == 5 ? " " : ":");
818 p += sprintf(p, "dst_mac: ");
819 for (i = 0; i < 6; i++) {
820 p += sprintf(p, "%02X%s", info->dst_mac[i], i == 5 ? "\n" : ":");
822 p += sprintf(p, " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
823 info->udp_src_min, info->udp_src_max, info->udp_dst_min,
825 p += sprintf(p, " src_mac_count: %d dst_mac_count: %d\n Flags: ",
826 info->src_mac_count, info->dst_mac_count);
827 if (info->flags & F_IPSRC_RND) {
828 p += sprintf(p, "IPSRC_RND ");
830 if (info->flags & F_IPDST_RND) {
831 p += sprintf(p, "IPDST_RND ");
833 if (info->flags & F_UDPSRC_RND) {
834 p += sprintf(p, "UDPSRC_RND ");
836 if (info->flags & F_UDPDST_RND) {
837 p += sprintf(p, "UDPDST_RND ");
839 if (info->flags & F_MACSRC_RND) {
840 p += sprintf(p, "MACSRC_RND ");
842 if (info->flags & F_MACDST_RND) {
843 p += sprintf(p, "MACDST_RND ");
845 p += sprintf(p, "\n");
847 sa = tv_to_ms(&(info->started_at));
848 stopped = tv_to_ms(&(info->stopped_at));
849 if (info->do_run_run) {
850 stopped = now; /* not really stopped, more like last-running-at */
852 p += sprintf(p, "Current:\n pkts-sofar: %llu errors: %llu\n started: %llums stopped: %llums now: %llums idle: %lluns\n",
853 (unsigned long long) info->sofar,
854 (unsigned long long) info->errors,
855 (unsigned long long) sa,
856 (unsigned long long) stopped,
857 (unsigned long long) now,
858 (unsigned long long) info->idle_acc);
859 p += sprintf(p, " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
860 info->seq_num, info->cur_dst_mac_offset, info->cur_src_mac_offset);
861 p += sprintf(p, " cur_saddr: 0x%x cur_daddr: 0x%x cur_udp_dst: %d cur_udp_src: %d\n",
862 info->cur_saddr, info->cur_daddr, info->cur_udp_dst, info->cur_udp_src);
865 p += sprintf(p, "Result: %s\n", info->result);
867 p += sprintf(p, "Result: Idle\n");
873 static int count_trail_chars(const char __user *user_buffer, unsigned int maxlen)
877 for (i = 0; i < maxlen; i++) {
880 if (get_user(c, &user_buffer[i]))
898 static unsigned long num_arg(const char __user *user_buffer, unsigned long maxlen,
905 for(; i < maxlen; i++) {
908 if (get_user(c, &user_buffer[i]))
910 if ((c >= '0') && (c <= '9')) {
919 static int strn_len(const char __user *user_buffer, unsigned int maxlen)
923 for(; i < maxlen; i++) {
926 if (get_user(c, &user_buffer[i]))
943 static int proc_write(struct file *file, const char __user *user_buffer,
944 unsigned long count, void *data)
947 char name[16], valstr[32];
948 unsigned long value = 0;
949 int idx = (int)(long)(data);
950 struct pktgen_info* info = NULL;
954 if ((idx < 0) || (idx >= MAX_PKTGEN)) {
955 printk("ERROR: idx: %i is out of range in proc_write\n", idx);
958 info = &(pginfos[idx]);
959 result = &(info->result[0]);
962 sprintf(result, "Wrong command format");
967 tmp = count_trail_chars(&user_buffer[i], max);
972 /* Read variable name */
974 len = strn_len(&user_buffer[i], sizeof(name) - 1);
977 memset(name, 0, sizeof(name));
978 if (copy_from_user(name, &user_buffer[i], len))
983 len = count_trail_chars(&user_buffer[i], max);
989 printk("pg: %s,%lu\n", name, count);
991 if (!strcmp(name, "stop")) {
992 if (info->do_run_run) {
993 strcpy(result, "Stopping");
996 strcpy(result, "Already stopped...\n");
998 info->do_run_run = 0;
1002 if (!strcmp(name, "pkt_size")) {
1003 len = num_arg(&user_buffer[i], 10, &value);
1007 if (value < 14+20+8)
1009 info->pkt_size = value;
1010 sprintf(result, "OK: pkt_size=%u", info->pkt_size);
1013 if (!strcmp(name, "frags")) {
1014 len = num_arg(&user_buffer[i], 10, &value);
1018 info->nfrags = value;
1019 sprintf(result, "OK: frags=%u", info->nfrags);
1022 if (!strcmp(name, "ipg")) {
1023 len = num_arg(&user_buffer[i], 10, &value);
1028 sprintf(result, "OK: ipg=%u", info->ipg);
1031 if (!strcmp(name, "udp_src_min")) {
1032 len = num_arg(&user_buffer[i], 10, &value);
1036 info->udp_src_min = value;
1037 sprintf(result, "OK: udp_src_min=%u", info->udp_src_min);
1040 if (!strcmp(name, "udp_dst_min")) {
1041 len = num_arg(&user_buffer[i], 10, &value);
1045 info->udp_dst_min = value;
1046 sprintf(result, "OK: udp_dst_min=%u", info->udp_dst_min);
1049 if (!strcmp(name, "udp_src_max")) {
1050 len = num_arg(&user_buffer[i], 10, &value);
1054 info->udp_src_max = value;
1055 sprintf(result, "OK: udp_src_max=%u", info->udp_src_max);
1058 if (!strcmp(name, "udp_dst_max")) {
1059 len = num_arg(&user_buffer[i], 10, &value);
1063 info->udp_dst_max = value;
1064 sprintf(result, "OK: udp_dst_max=%u", info->udp_dst_max);
1067 if (!strcmp(name, "clone_skb")) {
1068 len = num_arg(&user_buffer[i], 10, &value);
1072 info->clone_skb = value;
1074 sprintf(result, "OK: clone_skb=%d", info->clone_skb);
1077 if (!strcmp(name, "count")) {
1078 len = num_arg(&user_buffer[i], 10, &value);
1082 info->count = value;
1083 sprintf(result, "OK: count=%llu", (unsigned long long) info->count);
1086 if (!strcmp(name, "src_mac_count")) {
1087 len = num_arg(&user_buffer[i], 10, &value);
1091 info->src_mac_count = value;
1092 sprintf(result, "OK: src_mac_count=%d", info->src_mac_count);
1095 if (!strcmp(name, "dst_mac_count")) {
1096 len = num_arg(&user_buffer[i], 10, &value);
1100 info->dst_mac_count = value;
1101 sprintf(result, "OK: dst_mac_count=%d", info->dst_mac_count);
1104 if (!strcmp(name, "odev")) {
1105 len = strn_len(&user_buffer[i], sizeof(info->outdev) - 1);
1108 memset(info->outdev, 0, sizeof(info->outdev));
1109 if (copy_from_user(info->outdev, &user_buffer[i], len))
1112 sprintf(result, "OK: odev=%s", info->outdev);
1115 if (!strcmp(name, "flag")) {
1117 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1121 if (copy_from_user(f, &user_buffer[i], len))
1124 if (strcmp(f, "IPSRC_RND") == 0) {
1125 info->flags |= F_IPSRC_RND;
1127 else if (strcmp(f, "!IPSRC_RND") == 0) {
1128 info->flags &= ~F_IPSRC_RND;
1130 else if (strcmp(f, "IPDST_RND") == 0) {
1131 info->flags |= F_IPDST_RND;
1133 else if (strcmp(f, "!IPDST_RND") == 0) {
1134 info->flags &= ~F_IPDST_RND;
1136 else if (strcmp(f, "UDPSRC_RND") == 0) {
1137 info->flags |= F_UDPSRC_RND;
1139 else if (strcmp(f, "!UDPSRC_RND") == 0) {
1140 info->flags &= ~F_UDPSRC_RND;
1142 else if (strcmp(f, "UDPDST_RND") == 0) {
1143 info->flags |= F_UDPDST_RND;
1145 else if (strcmp(f, "!UDPDST_RND") == 0) {
1146 info->flags &= ~F_UDPDST_RND;
1148 else if (strcmp(f, "MACSRC_RND") == 0) {
1149 info->flags |= F_MACSRC_RND;
1151 else if (strcmp(f, "!MACSRC_RND") == 0) {
1152 info->flags &= ~F_MACSRC_RND;
1154 else if (strcmp(f, "MACDST_RND") == 0) {
1155 info->flags |= F_MACDST_RND;
1157 else if (strcmp(f, "!MACDST_RND") == 0) {
1158 info->flags &= ~F_MACDST_RND;
1161 sprintf(result, "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1163 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1166 sprintf(result, "OK: flags=0x%x", info->flags);
1169 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1170 len = strn_len(&user_buffer[i], sizeof(info->dst_min) - 1);
1173 memset(info->dst_min, 0, sizeof(info->dst_min));
1174 if (copy_from_user(info->dst_min, &user_buffer[i], len))
1177 printk("pg: dst_min set to: %s\n", info->dst_min);
1179 sprintf(result, "OK: dst_min=%s", info->dst_min);
1182 if (!strcmp(name, "dst_max")) {
1183 len = strn_len(&user_buffer[i], sizeof(info->dst_max) - 1);
1186 memset(info->dst_max, 0, sizeof(info->dst_max));
1187 if (copy_from_user(info->dst_max, &user_buffer[i], len))
1190 printk("pg: dst_max set to: %s\n", info->dst_max);
1192 sprintf(result, "OK: dst_max=%s", info->dst_max);
1195 if (!strcmp(name, "src_min")) {
1196 len = strn_len(&user_buffer[i], sizeof(info->src_min) - 1);
1199 memset(info->src_min, 0, sizeof(info->src_min));
1200 if (copy_from_user(info->src_min, &user_buffer[i], len))
1203 printk("pg: src_min set to: %s\n", info->src_min);
1205 sprintf(result, "OK: src_min=%s", info->src_min);
1208 if (!strcmp(name, "src_max")) {
1209 len = strn_len(&user_buffer[i], sizeof(info->src_max) - 1);
1212 memset(info->src_max, 0, sizeof(info->src_max));
1213 if (copy_from_user(info->src_max, &user_buffer[i], len))
1216 printk("pg: src_max set to: %s\n", info->src_max);
1218 sprintf(result, "OK: src_max=%s", info->src_max);
1221 if (!strcmp(name, "dstmac")) {
1223 unsigned char *m = info->dst_mac;
1225 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1228 memset(valstr, 0, sizeof(valstr));
1229 if (copy_from_user(valstr, &user_buffer[i], len))
1233 for(*m = 0;*v && m < info->dst_mac + 6; v++) {
1234 if (*v >= '0' && *v <= '9') {
1238 if (*v >= 'A' && *v <= 'F') {
1240 *m += *v - 'A' + 10;
1242 if (*v >= 'a' && *v <= 'f') {
1244 *m += *v - 'a' + 10;
1251 sprintf(result, "OK: dstmac");
1254 if (!strcmp(name, "srcmac")) {
1256 unsigned char *m = info->src_mac;
1258 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1261 memset(valstr, 0, sizeof(valstr));
1262 if (copy_from_user(valstr, &user_buffer[i], len))
1266 for(*m = 0;*v && m < info->src_mac + 6; v++) {
1267 if (*v >= '0' && *v <= '9') {
1271 if (*v >= 'A' && *v <= 'F') {
1273 *m += *v - 'A' + 10;
1275 if (*v >= 'a' && *v <= 'f') {
1277 *m += *v - 'a' + 10;
1284 sprintf(result, "OK: srcmac");
1288 if (!strcmp(name, "inject") || !strcmp(name, "start")) {
1290 strcpy(info->result, "Already running...\n");
1294 strcpy(info->result, "Starting");
1301 sprintf(info->result, "No such parameter \"%s\"", name);
1306 static int create_proc_dir(void)
1309 /* does proc_dir already exists */
1310 len = strlen(PG_PROC_DIR);
1312 for (proc_dir = proc_net->subdir; proc_dir;
1313 proc_dir=proc_dir->next) {
1314 if ((proc_dir->namelen == len) &&
1315 (! memcmp(proc_dir->name, PG_PROC_DIR, len)))
1319 proc_dir = create_proc_entry(PG_PROC_DIR, S_IFDIR, proc_net);
1320 if (!proc_dir) return -ENODEV;
1324 static int remove_proc_dir(void)
1326 remove_proc_entry(PG_PROC_DIR, proc_net);
1330 static int __init init(void)
1335 if (cpu_speed == 0) {
1336 printk("pktgen: Error: your machine does not have working cycle counter.\n");
1342 for (i = 0; i<MAX_PKTGEN; i++) {
1343 memset(&(pginfos[i]), 0, sizeof(pginfos[i]));
1344 pginfos[i].pkt_size = ETH_ZLEN;
1345 pginfos[i].nfrags = 0;
1346 pginfos[i].clone_skb = clone_skb_d;
1347 pginfos[i].ipg = ipg_d;
1348 pginfos[i].count = count_d;
1349 pginfos[i].sofar = 0;
1350 pginfos[i].hh[12] = 0x08; /* fill in protocol. Rest is filled in later. */
1351 pginfos[i].hh[13] = 0x00;
1352 pginfos[i].udp_src_min = 9; /* sink NULL */
1353 pginfos[i].udp_src_max = 9;
1354 pginfos[i].udp_dst_min = 9;
1355 pginfos[i].udp_dst_max = 9;
1357 sprintf(pginfos[i].fname, "net/%s/pg%i", PG_PROC_DIR, i);
1358 pginfos[i].proc_ent = create_proc_entry(pginfos[i].fname, 0600, 0);
1359 if (!pginfos[i].proc_ent) {
1360 printk("pktgen: Error: cannot create net/%s/pg procfs entry.\n", PG_PROC_DIR);
1363 pginfos[i].proc_ent->read_proc = proc_read;
1364 pginfos[i].proc_ent->write_proc = proc_write;
1365 pginfos[i].proc_ent->data = (void*)(long)(i);
1366 pginfos[i].proc_ent->owner = THIS_MODULE;
1368 sprintf(pginfos[i].busy_fname, "net/%s/pg_busy%i", PG_PROC_DIR, i);
1369 pginfos[i].busy_proc_ent = create_proc_entry(pginfos[i].busy_fname, 0, 0);
1370 if (!pginfos[i].busy_proc_ent) {
1371 printk("pktgen: Error: cannot create net/%s/pg_busy procfs entry.\n", PG_PROC_DIR);
1374 pginfos[i].busy_proc_ent->read_proc = proc_busy_read;
1375 pginfos[i].busy_proc_ent->data = (void*)(long)(i);
1380 for (i = 0; i<MAX_PKTGEN; i++) {
1381 if (strlen(pginfos[i].fname)) {
1382 remove_proc_entry(pginfos[i].fname, NULL);
1384 if (strlen(pginfos[i].busy_fname)) {
1385 remove_proc_entry(pginfos[i].busy_fname, NULL);
1392 static void __exit cleanup(void)
1395 for (i = 0; i<MAX_PKTGEN; i++) {
1396 if (strlen(pginfos[i].fname)) {
1397 remove_proc_entry(pginfos[i].fname, NULL);
1399 if (strlen(pginfos[i].busy_fname)) {
1400 remove_proc_entry(pginfos[i].busy_fname, NULL);
1407 module_exit(cleanup);
1409 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
1410 MODULE_DESCRIPTION("Packet Generator tool");
1411 MODULE_LICENSE("GPL");
1412 MODULE_PARM(count_d, "i");
1413 MODULE_PARM(ipg_d, "i");
1414 MODULE_PARM(cpu_speed, "i");
1415 MODULE_PARM(clone_skb_d, "i");