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'.
29 #include <sys/socket.h>
30 #include <sys/types.h>
38 #include <net/ethernet.h>
46 #include "trafgen_conf.h"
52 unsigned long tx_bytes
;
53 unsigned long tx_packets
;
57 #define CPU_UNKNOWN -1
58 #define CPU_NOTOUCH -2
66 /* 0 for automatic, > 0 for manual */
67 unsigned int reserve_size
;
75 static struct itimerval itimer
;
76 static unsigned long interval
= TX_KERNEL_PULL_INT
;
78 sig_atomic_t sigint
= 0;
80 struct packet
*packets
= NULL
;
81 unsigned int packets_len
= 0;
83 struct packet_dynamics
*packet_dyns
= NULL
;
84 unsigned int packet_dyn_len
= 0;
86 static const char *short_options
= "d:c:n:t:vJhS:HQb:B:rk:i:o:VRA";
87 static const struct option long_options
[] = {
88 {"dev", required_argument
, NULL
, 'd'},
89 {"out", required_argument
, NULL
, 'o'},
90 {"in", required_argument
, NULL
, 'i'},
91 {"conf", required_argument
, NULL
, 'c'},
92 {"num", required_argument
, NULL
, 'n'},
93 {"gap", required_argument
, NULL
, 't'},
94 {"ring-size", required_argument
, NULL
, 'S'},
95 {"bind-cpu", required_argument
, NULL
, 'b'},
96 {"unbind-cpu", required_argument
, NULL
, 'B'},
97 {"kernel-pull", required_argument
, NULL
, 'k'},
98 {"jumbo-support", no_argument
, NULL
, 'J'},
99 {"rfraw", no_argument
, NULL
, 'R'},
100 {"rand", no_argument
, NULL
, 'r'},
101 {"prio-high", no_argument
, NULL
, 'H'},
102 {"notouch-irq", no_argument
, NULL
, 'Q'},
103 {"verbose", no_argument
, NULL
, 'V'},
104 {"no-sock-mem", no_argument
, NULL
, 'A'},
105 {"version", no_argument
, NULL
, 'v'},
106 {"help", no_argument
, NULL
, 'h'},
110 static void signal_handler(int number
)
122 static void timer_elapsed(int number
)
124 itimer
.it_interval
.tv_sec
= 0;
125 itimer
.it_interval
.tv_usec
= interval
;
126 itimer
.it_value
.tv_sec
= 0;
127 itimer
.it_value
.tv_usec
= interval
;
129 pull_and_flush_tx_ring(sock
);
130 setitimer(ITIMER_REAL
, &itimer
, NULL
);
133 static void header(void)
135 printf("%s%s%s\n", colorize_start(bold
), "trafgen "
136 VERSION_STRING
, colorize_end());
139 static void help(void)
141 printf("\ntrafgen %s, zero-copy network packet generator\n", VERSION_STRING
);
142 puts("http://www.netsniff-ng.org\n\n"
143 "Usage: trafgen [options]\n"
145 /* " -o|-d|--out|--dev <netdev|pcap> Networking Device i.e., eth0 or pcap\n" */
146 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
147 " -i|-c|--in|--conf <cfg-file> Packet configuration file\n"
148 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
149 " Default TX slot: 2048Byte\n"
150 " -R|--rfraw Inject raw 802.11 frames\n"
151 " -n|--num <uint> Number of packets until exit\n"
152 " `-- 0 Loop until interrupt (default)\n"
153 " `- n Send n packets and done\n"
154 " -r|--rand Randomize packet selection process\n"
155 " Instead of a round robin selection\n"
156 " -t|--gap <uint> Interpacket gap in us (approx)\n"
157 " -A|--no-sock-mem Don't tune core socket memory\n"
158 " -S|--ring-size <size> Manually set ring size to <size>:\n"
159 " mmap space in KB/MB/GB, e.g. \'10MB\'\n"
160 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
161 " Default is 10us where the TX_RING\n"
162 " is populated with payload from uspace\n"
163 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
164 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
165 " -H|--prio-high Make this high priority process\n"
166 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
167 " -v|--version Show version\n"
168 " -h|--help Guess what?!\n\n"
170 " See trafgen.txf for configuration file examples.\n"
171 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n"
172 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf --bind-cpu 0 -A\n"
173 " trafgen --out eth0 --in trafgen.txf --bind-cpu 0\n"
174 /* " trafgen --out test.pcap --in trafgen.txf --bind-cpu 0\n" */
175 " trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n"
176 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n\n"
178 " This tool is targeted for network developers! You should\n"
179 " be aware of what you are doing and what these options above\n"
180 " mean! Only use this tool in an isolated LAN that you own!\n\n"
181 "Please report bugs to <bugs@netsniff-ng.org>\n"
182 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
183 "Swiss federal institute of technology (ETH Zurich)\n"
184 "License: GNU GPL version 2.0\n"
185 "This is free software: you are free to change and redistribute it.\n"
186 "There is NO WARRANTY, to the extent permitted by law.\n");
190 static void version(void)
192 printf("\ntrafgen %s, zero-copy network packet generator\n", VERSION_STRING
);
193 puts("http://www.netsniff-ng.org\n\n"
194 "Please report bugs to <bugs@netsniff-ng.org>\n"
195 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
196 "Swiss federal institute of technology (ETH Zurich)\n"
197 "License: GNU GPL version 2.0\n"
198 "This is free software: you are free to change and redistribute it.\n"
199 "There is NO WARRANTY, to the extent permitted by law.\n");
203 static inline void apply_counter(int i
)
207 for (j
= 0; j
< packet_dyns
[i
].counter_len
; ++j
) {
209 struct counter
*counter
= &packet_dyns
[i
].counter
[j
];
214 if (counter
->type
== TYPE_INC
)
215 val
= (val
+ counter
->inc
) %
216 (counter
->max
- counter
->min
+ 1);
218 val
= (val
- counter
->inc
) %
219 (counter
->min
- counter
->max
+ 1);
224 packets
[i
].payload
[counter
->off
] = val
;
228 static inline void apply_randomizer(int i
)
232 for (j
= 0; j
< packet_dyns
[i
].randomizer_len
; ++j
) {
233 uint8_t val
= (uint8_t) mt_rand_int32();
234 struct randomizer
*randomizer
= &packet_dyns
[i
].randomizer
[j
];
236 randomizer
->val
= val
;
237 packets
[i
].payload
[randomizer
->off
] = val
;
241 static void tx_precheck(struct mode
*mode
)
246 panic("Panic over invalid args for TX trigger!\n");
247 if (packets_len
== 0 || packets_len
!= packet_dyn_len
)
248 panic("Panic over invalid args for TX trigger!\n");
249 if (!mode
->rfraw
&& !device_up_and_running(mode
->device
))
250 panic("Device not up and running!\n");
252 mtu
= device_mtu(mode
->device
);
254 for (i
= 0; i
< packets_len
; ++i
) {
255 if (packets
[i
].len
> mtu
+ 14)
256 panic("Device MTU < than your packet size!\n");
257 if (packets
[i
].len
<= 14)
258 panic("Device packet size too short!\n");
262 static void tx_slowpath_or_die(struct mode
*mode
)
266 struct sockaddr_ll s_addr
;
267 unsigned long num
= 1;
268 struct timeval start
, end
, diff
;
275 mode
->device_trans
= xstrdup(mode
->device
);
278 enter_rfmon_mac80211(mode
->device_trans
, &mode
->device
);
281 ifindex
= device_ifindex(mode
->device
);
286 printf("Note: randomizes output makes trafgen slower!\n");
288 printf("MD: TX slowpath %s %luus", mode
->rand
? "RND" : "RR", mode
->gap
);
290 printf(" 802.11 raw via %s", mode
->device
);
292 printf("Running! Hang up with ^C!\n\n");
294 fmemset(&s_addr
, 0, sizeof(s_addr
));
295 s_addr
.sll_family
= PF_PACKET
;
296 s_addr
.sll_halen
= ETH_ALEN
;
297 s_addr
.sll_ifindex
= ifindex
;
301 bug_on(gettimeofday(&start
, NULL
));
303 while (likely(sigint
== 0) && likely(num
> 0)) {
307 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
308 (struct sockaddr
*) &s_addr
, sizeof(s_addr
));
310 whine("sendto error!\n");
312 mode
->stats
.tx_bytes
+= packets
[i
].len
;
313 mode
->stats
.tx_packets
++;
316 i
= mt_rand_int32() % packets_len
;
319 atomic_cmp_swp(&i
, packets_len
, 0);
328 bug_on(gettimeofday(&end
, NULL
));
329 diff
= tv_subtract(end
, start
);
332 leave_rfmon_mac80211(mode
->device_trans
, mode
->device
);
338 printf("\r%12lu frames outgoing\n", mode
->stats
.tx_packets
);
339 printf("\r%12lu bytes outgoing\n", mode
->stats
.tx_bytes
);
340 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
343 static void tx_fastpath_or_die(struct mode
*mode
)
346 unsigned int i
, size
, it
= 0;
347 unsigned long num
= 1;
350 struct frame_map
*hdr
;
351 struct timeval start
, end
, diff
;
357 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
360 mode
->device_trans
= xstrdup(mode
->device
);
363 enter_rfmon_mac80211(mode
->device_trans
, &mode
->device
);
366 ifindex
= device_ifindex(mode
->device
);
367 size
= ring_size(mode
->device
, mode
->reserve_size
);
369 set_sock_prio(sock
, 512);
370 set_packet_loss_discard(sock
);
371 setup_tx_ring_layout(sock
, &tx_ring
, size
, mode
->jumbo_support
);
372 create_tx_ring(sock
, &tx_ring
);
373 mmap_tx_ring(sock
, &tx_ring
);
374 alloc_tx_ring_frames(&tx_ring
);
375 bind_tx_ring(sock
, &tx_ring
, ifindex
);
377 if (mode
->cpu
>= 0 && ifindex
> 0) {
378 irq
= device_irq_number(mode
->device
);
379 device_bind_irq_to_cpu(mode
->cpu
, irq
);
380 printf("IRQ: %s:%d > CPU%d\n", mode
->device
, irq
,
385 interval
= mode
->kpull
;
389 printf("Note: randomizes output makes trafgen slower!\n");
391 printf("MD: TX fastpath %s %luus", mode
->rand
? "RND" : "RR", interval
);
393 printf(" 802.11 raw via %s", mode
->device
);
395 printf("Running! Hang up with ^C!\n\n");
397 itimer
.it_interval
.tv_sec
= 0;
398 itimer
.it_interval
.tv_usec
= interval
;
399 itimer
.it_value
.tv_sec
= 0;
400 itimer
.it_value
.tv_usec
= interval
;
401 setitimer(ITIMER_REAL
, &itimer
, NULL
);
405 bug_on(gettimeofday(&start
, NULL
));
407 while (likely(sigint
== 0) && likely(num
> 0)) {
408 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) &&
410 hdr
= tx_ring
.frames
[it
].iov_base
;
412 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
413 * sizeof(struct sockaddr_ll); */
414 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
-
415 sizeof(struct sockaddr_ll
);
417 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
418 hdr
->tp_h
.tp_len
= packets
[i
].len
;
423 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
425 mode
->stats
.tx_bytes
+= packets
[i
].len
;
426 mode
->stats
.tx_packets
++;
429 i
= mt_rand_int32() % packets_len
;
432 atomic_cmp_swp(&i
, packets_len
, 0);
435 kernel_may_pull_from_tx(&hdr
->tp_h
);
436 next_slot_prewr(&it
, &tx_ring
);
440 if (unlikely(sigint
== 1))
445 bug_on(gettimeofday(&end
, NULL
));
446 diff
= tv_subtract(end
, start
);
448 destroy_tx_ring(sock
, &tx_ring
);
451 leave_rfmon_mac80211(mode
->device_trans
, mode
->device
);
457 printf("\r%12lu frames outgoing\n", mode
->stats
.tx_packets
);
458 printf("\r%12lu bytes outgoing\n", mode
->stats
.tx_bytes
);
459 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
462 static void main_loop(struct mode
*mode
, char *confname
)
464 compile_packets(confname
, mode
->verbose
);
467 tx_slowpath_or_die(mode
);
469 tx_fastpath_or_die(mode
);
474 int main(int argc
, char **argv
)
476 int c
, opt_index
, i
, j
;
478 char *confname
= NULL
, *ptr
;
479 bool prio_high
= false;
480 bool setsockmem
= true;
483 fmemset(&mode
, 0, sizeof(mode
));
484 mode
.cpu
= CPU_UNKNOWN
;
488 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
489 &opt_index
)) != EOF
) {
502 mode
.device
= xstrndup(optarg
, IFNAMSIZ
);
511 mode
.jumbo_support
= 1;
515 confname
= xstrdup(optarg
);
518 mode
.kpull
= atol(optarg
);
521 mode
.num
= atol(optarg
);
524 mode
.gap
= atol(optarg
);
531 mode
.reserve_size
= 0;
533 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
534 if (!isdigit(optarg
[j
- i
]))
539 if (!strncmp(ptr
, "KB", strlen("KB")))
540 mode
.reserve_size
= 1 << 10;
541 else if (!strncmp(ptr
, "MB", strlen("MB")))
542 mode
.reserve_size
= 1 << 20;
543 else if (!strncmp(ptr
, "GB", strlen("GB")))
544 mode
.reserve_size
= 1 << 30;
546 panic("Syntax error in ring size param!\n");
549 mode
.reserve_size
*= atoi(optarg
);
552 set_cpu_affinity(optarg
, 0);
553 /* Take the first CPU for rebinding the IRQ */
554 if (mode
.cpu
!= CPU_NOTOUCH
)
555 mode
.cpu
= atoi(optarg
);
558 set_cpu_affinity(optarg
, 1);
564 mode
.cpu
= CPU_NOTOUCH
;
578 panic("Option -%c requires an argument!\n",
582 whine("Unknown option character "
583 "`0x%X\'!\n", optopt
);
593 if (mode
.device
== NULL
)
594 panic("No networking device given!\n");
595 if (confname
== NULL
)
596 panic("No configuration file given!\n");
597 if (device_mtu(mode
.device
) == 0)
598 panic("This is no networking device!\n");
599 if (!mode
.rfraw
&& device_up_and_running(mode
.device
) == 0)
600 panic("Networking device not running!\n");
602 register_signal(SIGINT
, signal_handler
);
603 register_signal(SIGHUP
, signal_handler
);
604 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
608 if (prio_high
== true) {
609 set_proc_prio(get_default_proc_prio());
610 set_sched_status(get_default_sched_policy(),
611 get_default_sched_prio());
614 if (setsockmem
== true) {
615 if ((vals
[0] = get_system_socket_mem(sock_rmem_max
)) < SMEM_SUG_MAX
)
616 set_system_socket_mem(sock_rmem_max
, SMEM_SUG_MAX
);
617 if ((vals
[1] = get_system_socket_mem(sock_rmem_def
)) < SMEM_SUG_DEF
)
618 set_system_socket_mem(sock_rmem_def
, SMEM_SUG_DEF
);
619 if ((vals
[2] = get_system_socket_mem(sock_wmem_max
)) < SMEM_SUG_MAX
)
620 set_system_socket_mem(sock_wmem_max
, SMEM_SUG_MAX
);
621 if ((vals
[3] = get_system_socket_mem(sock_wmem_def
)) < SMEM_SUG_DEF
)
622 set_system_socket_mem(sock_wmem_def
, SMEM_SUG_DEF
);
625 main_loop(&mode
, confname
);
627 if (setsockmem
== true) {
628 set_system_socket_mem(sock_rmem_max
, vals
[0]);
629 set_system_socket_mem(sock_rmem_def
, vals
[1]);
630 set_system_socket_mem(sock_wmem_max
, vals
[2]);
631 set_system_socket_mem(sock_wmem_def
, vals
[3]);
635 free(mode
.device_trans
);