2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
8 * A high-performance network traffic generator that uses the zero-copy
9 * kernelspace TX_RING for network I/O. On comodity Gigabit hardware up
10 * to 1,488,095 pps 64 Byte pps have been achieved with 2 trafgen instances
11 * bound to different CPUs from the userspace and turned off pause frames,
12 * ask Ronald from NST (Network Security Toolkit) for more details. ;-)
13 * So, this line-rate result is the very same as pktgen from kernelspace!
15 * Who can now hold the fords when the King of the Nine Riders comes? And
16 * other armies will come. I am too late. All is lost. I tarried on the
17 * way. All is lost. Even if my errand is performed, no one will ever
18 * know. There will be no one I can tell. It will be in vain.
20 * -- The Lord of the Rings, Frodo thinking,
21 * Chapter 'The Stairs of Cirith Ungol'.
28 trafgen - a high-performance zero-copy network packet generator
32 trafgen [-d|--dev <netdev>][-c|--conf <file>][-J|--jumbo-support]
33 [-x|--interactive][-n|--num <uint>][-r|--rand][-t|--gap <usec>]
34 [-S|--ring-size <size>][-k|--kernel-pull <usec>][-b|--bind-cpu <cpu>]
35 [-B|--unbind-cpu <cpu>][-H|--prio-high][-Q|--notouch-irq][-v|--version]
40 A high-performance network traffic generator that uses the zero-copy TX_RING
41 for network I/O. For instance, on comodity Gigabit hardware up to 1,488,095 pps
42 64 Byte pps have been achieved with trafgen.
48 =item trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0
50 Use packet configuration trafgen.txf, eth0 as transmission device and CPU0
51 for binding the process.
61 Print help text and lists all options.
67 =item -d|--dev <netdev>
69 Device for transmission i.e., eth0.
71 =item -c|--conf <conf>
73 Path to packet configuration file.
75 =item -x|--interactive
77 Start trafgen in interactive mode.
79 =item -J|--jumbo-support
81 Support for 64KB Super Jumbo Frames
85 Number of packets to generate before exiting.
86 0 means forever until SIGINT.
90 Randomize packet selection process instead of round-robin.
94 Interpacket gap in microseconds.
96 =item -S|--ring-size <size>
98 Manually set ring size to <size>: mmap space in KB/MB/GB.
100 =item -k|--kernel-pull <uint>
102 Kernel pull from user interval in microseconds.
103 Default value is 10 microseconds.
105 =item -b|--bind-cpu <cpu>
107 Bind to specific CPU (or CPU-range).
109 =item -B|--unbind-cpu <cpu>
111 Forbid to use specific CPU (or CPU-range).
115 Make this high priority process.
117 =item -Q|--notouch-irq
119 Do not touch IRQ CPU affinity of NIC.
127 =item Generate traffic defined in trafgen.txf on eth0 using CPU 0
129 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0
131 =item Generate traffic on eth0 using CPU 0, wait 100 us between packets
133 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --gap 100
135 =item Generate 100,000 packet on eth0 using CPU 0
137 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 100000
143 Written by Daniel Borkmann <daniel@netsniff-ng.org>
147 Documentation by Emmanuel Roullit <emmanuel@netsniff-ng.org>
151 Please report bugs to <bugs@netsniff-ng.org>
162 #include <sys/socket.h>
163 #include <sys/types.h>
164 #include <sys/stat.h>
165 #include <sys/time.h>
171 #include <net/ethernet.h>
176 #include "mac80211.h"
179 #include "trafgen_conf.h"
185 unsigned long tx_bytes
;
186 unsigned long tx_packets
;
190 #define CPU_UNKNOWN -1
191 #define CPU_NOTOUCH -2
199 /* 0 for automatic, > 0 for manual */
200 unsigned int reserve_size
;
208 static struct itimerval itimer
;
209 static unsigned long interval
= TX_KERNEL_PULL_INT
;
211 __export
sig_atomic_t sigint
= 0;
213 __export
struct packet
*packets
= NULL
;
214 __export
unsigned int packets_len
= 0;
215 __export
struct packet_dynamics
*packet_dyns
= NULL
;
216 __export
unsigned int packet_dyn_len
= 0;
218 static const char *short_options
= "d:c:n:t:vJhS:HQb:B:rk:xi:o:VR";
220 static struct option long_options
[] = {
221 {"dev", required_argument
, 0, 'd'},
222 {"out", required_argument
, 0, 'o'},
223 {"in", required_argument
, 0, 'i'},
224 {"conf", required_argument
, 0, 'c'},
225 {"num", required_argument
, 0, 'n'},
226 {"gap", required_argument
, 0, 't'},
227 {"ring-size", required_argument
, 0, 'S'},
228 {"bind-cpu", required_argument
, 0, 'b'},
229 {"unbind-cpu", required_argument
, 0, 'B'},
230 {"kernel-pull", required_argument
, 0, 'k'},
231 {"jumbo-support", no_argument
, 0, 'J'},
232 {"rfraw", no_argument
, 0, 'R'},
233 {"interactive", no_argument
, 0, 'x'},
234 {"rand", no_argument
, 0, 'r'},
235 {"prio-high", no_argument
, 0, 'H'},
236 {"notouch-irq", no_argument
, 0, 'Q'},
237 {"verbose", no_argument
, 0, 'V'},
238 {"version", no_argument
, 0, 'v'},
239 {"help", no_argument
, 0, 'h'},
243 static void signal_handler(int number
)
255 static void timer_elapsed(int number
)
257 itimer
.it_interval
.tv_sec
= 0;
258 itimer
.it_interval
.tv_usec
= interval
;
259 itimer
.it_value
.tv_sec
= 0;
260 itimer
.it_value
.tv_usec
= interval
;
262 pull_and_flush_tx_ring(sock
);
263 setitimer(ITIMER_REAL
, &itimer
, NULL
);
266 static void header(void)
268 printf("%s%s%s\n", colorize_start(bold
), "trafgen "
269 VERSION_STRING
, colorize_end());
272 static void help(void)
274 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
276 printf("http://www.netsniff-ng.org\n\n");
277 printf("Usage: trafgen [options]\n");
278 printf("Options:\n");
279 printf(" -o|-d|--out|--dev <netdev|pcap> Networking Device i.e., eth0 or pcap\n");
280 printf(" -i|-c|--in|--conf <cfg-file> Packet configuration file\n");
281 printf(" -x|--interactive Start trafgen in interactive server mode\n");
282 printf(" -J|--jumbo-support Support for 64KB Super Jumbo Frames\n");
283 printf(" Default TX slot: 2048Byte\n");
284 printf(" -R|--rfraw Inject raw 802.11 frames\n");
285 printf(" -n|--num <uint> Number of packets until exit\n");
286 printf(" `-- 0 Loop until interrupt (default)\n");
287 printf(" `- n Send n packets and done\n");
288 printf(" -r|--rand Randomize packet selection process\n");
289 printf(" Instead of a round robin selection\n");
290 printf(" -t|--gap <uint> Interpacket gap in us (approx)\n");
291 printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
292 printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
293 printf(" -k|--kernel-pull <uint> Kernel pull from user interval in us\n");
294 printf(" Default is 10us where the TX_RING\n");
295 printf(" is populated with payload from uspace\n");
296 printf(" -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n");
297 printf(" -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n");
298 printf(" -H|--prio-high Make this high priority process\n");
299 printf(" -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n");
300 printf(" -v|--version Show version\n");
301 printf(" -h|--help Guess what?!\n");
303 printf("Examples:\n");
304 printf(" See trafgen.txf for configuration file examples.\n");
305 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n");
306 printf(" trafgen --dev wlan0 --rfraw --conf beacon-test.txf --bind-cpu 0\n");
307 printf(" trafgen --out eth0 --in trafgen.txf --bind-cpu 0\n");
308 printf(" trafgen --out test.pcap --in trafgen.txf --bind-cpu 0\n");
309 printf(" trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n");
310 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n");
311 printf(" trafgen --interactive\n");
312 printf(" trafgen --interactive --dev mgmt0 (only start server on mgmt0)\n");
313 printf(" trafgen --interactive --conf trafgen-cli.batch\n");
316 printf(" This tool is targeted for network developers! You should\n");
317 printf(" be aware of what you are doing and what these options above\n");
318 printf(" mean! Only use this tool in an isolated LAN that you own!\n");
320 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
321 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
322 printf("Swiss federal institute of technology (ETH Zurich)\n");
323 printf("License: GNU GPL version 2\n");
324 printf("This is free software: you are free to change and redistribute it.\n");
325 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
329 static void version(void)
331 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
333 printf("http://www.netsniff-ng.org\n\n");
334 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
335 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
336 printf("Swiss federal institute of technology (ETH Zurich)\n");
337 printf("License: GNU GPL version 2\n");
338 printf("This is free software: you are free to change and redistribute it.\n");
339 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
343 static inline void apply_counter(int i
)
347 for (j
= 0; j
< packet_dyns
[i
].counter_len
; ++j
) {
349 struct counter
*counter
= &packet_dyns
[i
].counter
[j
];
354 if (counter
->type
== TYPE_INC
)
355 val
= (val
+ counter
->inc
) %
356 (counter
->max
- counter
->min
+ 1);
358 val
= (val
- counter
->inc
) %
359 (counter
->min
- counter
->max
+ 1);
364 packets
[i
].payload
[counter
->off
] = val
;
368 static inline void apply_randomizer(int i
)
372 for (j
= 0; j
< packet_dyns
[i
].randomizer_len
; ++j
) {
373 uint8_t val
= (uint8_t) mt_rand_int32();
374 struct randomizer
*randomizer
= &packet_dyns
[i
].randomizer
[j
];
376 randomizer
->val
= val
;
377 packets
[i
].payload
[randomizer
->off
] = val
;
381 static void tx_precheck(struct mode
*mode
)
386 panic("Panic over invalid args for TX trigger!\n");
387 if (packets_len
== 0 || packets_len
!= packet_dyn_len
)
388 panic("Panic over invalid args for TX trigger!\n");
389 if (!device_up_and_running(mode
->device
))
390 panic("Device not up and running!\n");
392 mtu
= device_mtu(mode
->device
);
394 for (i
= 0; i
< packets_len
; ++i
) {
395 if (packets
[i
].len
> mtu
+ 14)
396 panic("Device MTU < than your packet size!\n");
397 if (packets
[i
].len
<= 14)
398 panic("Device packet size too short!\n");
402 static void tx_slowpath_or_die(struct mode
*mode
)
406 struct sockaddr_ll s_addr
;
407 unsigned long num
= 1;
414 mode
->device_trans
= xstrdup(mode
->device
);
417 enter_rfmon_mac80211(mode
->device_trans
, &mode
->device
);
420 ifindex
= device_ifindex(mode
->device
);
425 printf("Note: randomizes output makes trafgen slower!\n");
427 printf("MD: TX slowpath %s %luus", mode
->rand
? "RND" : "RR", mode
->gap
);
429 printf(" 802.11 raw via %s", mode
->device
);
431 printf("Running! Hang up with ^C!\n\n");
433 fmemset(&s_addr
, 0, sizeof(s_addr
));
434 s_addr
.sll_family
= PF_PACKET
;
435 s_addr
.sll_halen
= ETH_ALEN
;
436 s_addr
.sll_ifindex
= ifindex
;
440 while (likely(sigint
== 0) && likely(num
> 0)) {
444 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
445 (struct sockaddr
*) &s_addr
, sizeof(s_addr
));
447 whine("sendto error!\n");
449 mode
->stats
.tx_bytes
+= packets
[i
].len
;
450 mode
->stats
.tx_packets
++;
453 i
= mt_rand_int32() % packets_len
;
456 atomic_cmp_swp(&i
, packets_len
, 0);
466 leave_rfmon_mac80211(mode
->device_trans
, mode
->device
);
472 printf("\r%12lu frames outgoing\n", mode
->stats
.tx_packets
);
473 printf("\r%12lu bytes outgoing\n", mode
->stats
.tx_bytes
);
476 static void tx_fastpath_or_die(struct mode
*mode
)
479 unsigned int i
, size
, it
= 0;
480 unsigned long num
= 1;
483 struct frame_map
*hdr
;
489 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
492 mode
->device_trans
= xstrdup(mode
->device
);
495 enter_rfmon_mac80211(mode
->device_trans
, &mode
->device
);
498 ifindex
= device_ifindex(mode
->device
);
499 size
= ring_size(mode
->device
, mode
->reserve_size
);
501 set_packet_loss_discard(sock
);
502 setup_tx_ring_layout(sock
, &tx_ring
, size
, mode
->jumbo_support
);
503 create_tx_ring(sock
, &tx_ring
);
504 mmap_tx_ring(sock
, &tx_ring
);
505 alloc_tx_ring_frames(&tx_ring
);
506 bind_tx_ring(sock
, &tx_ring
, ifindex
);
508 if (mode
->cpu
>= 0 && ifindex
> 0) {
509 irq
= device_irq_number(mode
->device
);
510 device_bind_irq_to_cpu(mode
->cpu
, irq
);
511 printf("IRQ: %s:%d > CPU%d\n", mode
->device
, irq
,
516 interval
= mode
->kpull
;
520 printf("Note: randomizes output makes trafgen slower!\n");
522 printf("MD: TX fastpath %s %luus", mode
->rand
? "RND" : "RR", interval
);
524 printf(" 802.11 raw via %s", mode
->device
);
526 printf("Running! Hang up with ^C!\n\n");
528 itimer
.it_interval
.tv_sec
= 0;
529 itimer
.it_interval
.tv_usec
= interval
;
530 itimer
.it_value
.tv_sec
= 0;
531 itimer
.it_value
.tv_usec
= interval
;
532 setitimer(ITIMER_REAL
, &itimer
, NULL
);
536 while (likely(sigint
== 0) && likely(num
> 0)) {
537 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) &&
539 hdr
= tx_ring
.frames
[it
].iov_base
;
541 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
542 * sizeof(struct sockaddr_ll); */
543 out
= ((uint8_t *) hdr
) + TPACKET_HDRLEN
-
544 sizeof(struct sockaddr_ll
);
546 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
547 hdr
->tp_h
.tp_len
= packets
[i
].len
;
552 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
554 mode
->stats
.tx_bytes
+= packets
[i
].len
;
555 mode
->stats
.tx_packets
++;
558 i
= mt_rand_int32() % packets_len
;
561 atomic_cmp_swp(&i
, packets_len
, 0);
564 kernel_may_pull_from_tx(&hdr
->tp_h
);
565 next_slot_prewr(&it
, &tx_ring
);
569 if (unlikely(sigint
== 1))
574 destroy_tx_ring(sock
, &tx_ring
);
577 leave_rfmon_mac80211(mode
->device_trans
, mode
->device
);
583 printf("\r%12lu frames outgoing\n", mode
->stats
.tx_packets
);
584 printf("\r%12lu bytes outgoing\n", mode
->stats
.tx_bytes
);
587 static void main_loop(struct mode
*mode
, char *confname
)
589 compile_packets(confname
, mode
->verbose
);
592 tx_slowpath_or_die(mode
);
594 tx_fastpath_or_die(mode
);
599 int main(int argc
, char **argv
)
601 int c
, opt_index
, i
, j
, interactive
= 0;
602 char *confname
= NULL
, *ptr
;
603 bool prio_high
= false;
606 check_for_root_maybe_die();
608 fmemset(&mode
, 0, sizeof(mode
));
609 mode
.cpu
= CPU_UNKNOWN
;
613 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
614 &opt_index
)) != EOF
) {
627 mode
.device
= xstrndup(optarg
, IFNAMSIZ
);
639 mode
.jumbo_support
= 1;
643 confname
= xstrdup(optarg
);
646 mode
.kpull
= atol(optarg
);
649 mode
.num
= atol(optarg
);
652 mode
.gap
= atol(optarg
);
656 mode
.reserve_size
= 0;
658 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
659 if (!isdigit(optarg
[j
- i
]))
664 if (!strncmp(ptr
, "KB", strlen("KB")))
665 mode
.reserve_size
= 1 << 10;
666 else if (!strncmp(ptr
, "MB", strlen("MB")))
667 mode
.reserve_size
= 1 << 20;
668 else if (!strncmp(ptr
, "GB", strlen("GB")))
669 mode
.reserve_size
= 1 << 30;
671 panic("Syntax error in ring size param!\n");
674 mode
.reserve_size
*= atoi(optarg
);
677 set_cpu_affinity(optarg
, 0);
678 /* Take the first CPU for rebinding the IRQ */
679 if (mode
.cpu
!= CPU_NOTOUCH
)
680 mode
.cpu
= atoi(optarg
);
683 set_cpu_affinity(optarg
, 1);
689 mode
.cpu
= CPU_NOTOUCH
;
703 panic("Option -%c requires an argument!\n",
707 whine("Unknown option character "
708 "`0x%X\'!\n", optopt
);
716 if (!interactive
&& argc
< 5)
718 if (interactive
&& argc
< 2)
720 if (!interactive
&& mode
.device
== NULL
)
721 panic("No networking device given!\n");
722 if (!interactive
&& confname
== NULL
)
723 panic("No configuration file given!\n");
724 if (!interactive
&& device_mtu(mode
.device
) == 0)
725 panic("This is no networking device!\n");
726 if (!interactive
&& device_up_and_running(mode
.device
) == 0)
727 panic("Networking device not running!\n");
729 register_signal(SIGINT
, signal_handler
);
730 register_signal(SIGHUP
, signal_handler
);
731 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
735 if (prio_high
== true) {
736 set_proc_prio(get_default_proc_prio());
737 set_sched_status(get_default_sched_policy(),
738 get_default_sched_prio());
742 main_loop_interactive(&mode
, confname
);
744 main_loop(&mode
, confname
);
748 if (mode
.device_trans
)
749 xfree(mode
.device_trans
);