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("\n%s %s, zero-copy network packet generator\n",
142 PROGNAME_STRING
, VERSION_STRING
);
143 puts("http://www.netsniff-ng.org\n\n"
144 "Usage: trafgen [options]\n"
146 /* " -o|-d|--out|--dev <netdev|pcap> Networking Device i.e., eth0 or pcap\n" */
147 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
148 " -i|-c|--in|--conf <cfg-file> Packet configuration file\n"
149 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
150 " Default TX slot: 2048Byte\n"
151 " -R|--rfraw Inject raw 802.11 frames\n"
152 " -n|--num <uint> Number of packets until exit\n"
153 " `-- 0 Loop until interrupt (default)\n"
154 " `- n Send n packets and done\n"
155 " -r|--rand Randomize packet selection process\n"
156 " Instead of a round robin selection\n"
157 " -t|--gap <uint> Interpacket gap in us (approx)\n"
158 " -A|--no-sock-mem Don't tune core socket memory\n"
159 " -S|--ring-size <size> Manually set ring size to <size>:\n"
160 " mmap space in KB/MB/GB, e.g. \'10MB\'\n"
161 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
162 " Default is 10us where the TX_RING\n"
163 " is populated with payload from uspace\n"
164 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
165 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
166 " -H|--prio-high Make this high priority process\n"
167 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
168 " -v|--version Show version\n"
169 " -h|--help Guess what?!\n\n"
171 " See trafgen.txf for configuration file examples.\n"
172 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n"
173 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf --bind-cpu 0 -A\n"
174 " trafgen --out eth0 --in trafgen.txf --bind-cpu 0\n"
175 /* " trafgen --out test.pcap --in trafgen.txf --bind-cpu 0\n" */
176 " trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n"
177 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n\n"
179 " This tool is targeted for network developers! You should\n"
180 " be aware of what you are doing and what these options above\n"
181 " mean! Only use this tool in an isolated LAN that you own!\n\n"
182 "Please report bugs to <bugs@netsniff-ng.org>\n"
183 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
184 "Swiss federal institute of technology (ETH Zurich)\n"
185 "License: GNU GPL version 2.0\n"
186 "This is free software: you are free to change and redistribute it.\n"
187 "There is NO WARRANTY, to the extent permitted by law.\n");
191 static void version(void)
193 printf("\n%s %s, zero-copy network packet generator\n",
194 PROGNAME_STRING
, VERSION_STRING
);
195 puts("http://www.netsniff-ng.org\n\n"
196 "Please report bugs to <bugs@netsniff-ng.org>\n"
197 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
198 "Swiss federal institute of technology (ETH Zurich)\n"
199 "License: GNU GPL version 2.0\n"
200 "This is free software: you are free to change and redistribute it.\n"
201 "There is NO WARRANTY, to the extent permitted by law.\n");
205 static inline void apply_counter(int i
)
209 for (j
= 0; j
< packet_dyns
[i
].counter_len
; ++j
) {
211 struct counter
*counter
= &packet_dyns
[i
].counter
[j
];
216 if (counter
->type
== TYPE_INC
)
217 val
= (val
+ counter
->inc
) %
218 (counter
->max
- counter
->min
+ 1);
220 val
= (val
- counter
->inc
) %
221 (counter
->min
- counter
->max
+ 1);
226 packets
[i
].payload
[counter
->off
] = val
;
230 static inline void apply_randomizer(int i
)
234 for (j
= 0; j
< packet_dyns
[i
].randomizer_len
; ++j
) {
235 uint8_t val
= (uint8_t) mt_rand_int32();
236 struct randomizer
*randomizer
= &packet_dyns
[i
].randomizer
[j
];
238 randomizer
->val
= val
;
239 packets
[i
].payload
[randomizer
->off
] = val
;
243 static void tx_precheck(struct mode
*mode
)
248 panic("Panic over invalid args for TX trigger!\n");
249 if (packets_len
== 0 || packets_len
!= packet_dyn_len
)
250 panic("Panic over invalid args for TX trigger!\n");
251 if (!mode
->rfraw
&& !device_up_and_running(mode
->device
))
252 panic("Device not up and running!\n");
254 mtu
= device_mtu(mode
->device
);
256 for (i
= 0; i
< packets_len
; ++i
) {
257 if (packets
[i
].len
> mtu
+ 14)
258 panic("Device MTU < than your packet size!\n");
259 if (packets
[i
].len
<= 14)
260 panic("Device packet size too short!\n");
264 static void tx_slowpath_or_die(struct mode
*mode
)
268 struct sockaddr_ll s_addr
;
269 unsigned long num
= 1;
270 struct timeval start
, end
, diff
;
277 mode
->device_trans
= xstrdup(mode
->device
);
280 enter_rfmon_mac80211(mode
->device_trans
, &mode
->device
);
283 ifindex
= device_ifindex(mode
->device
);
288 printf("Note: randomizes output makes trafgen slower!\n");
290 printf("MD: TX slowpath %s %luus", mode
->rand
? "RND" : "RR", mode
->gap
);
292 printf(" 802.11 raw via %s", mode
->device
);
294 printf("Running! Hang up with ^C!\n\n");
296 fmemset(&s_addr
, 0, sizeof(s_addr
));
297 s_addr
.sll_family
= PF_PACKET
;
298 s_addr
.sll_halen
= ETH_ALEN
;
299 s_addr
.sll_ifindex
= ifindex
;
303 bug_on(gettimeofday(&start
, NULL
));
305 while (likely(sigint
== 0) && likely(num
> 0)) {
309 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
310 (struct sockaddr
*) &s_addr
, sizeof(s_addr
));
312 whine("sendto error!\n");
314 mode
->stats
.tx_bytes
+= packets
[i
].len
;
315 mode
->stats
.tx_packets
++;
318 i
= mt_rand_int32() % packets_len
;
321 atomic_cmp_swp(&i
, packets_len
, 0);
330 bug_on(gettimeofday(&end
, NULL
));
331 diff
= tv_subtract(end
, start
);
334 leave_rfmon_mac80211(mode
->device_trans
, mode
->device
);
340 printf("\r%12lu frames outgoing\n", mode
->stats
.tx_packets
);
341 printf("\r%12lu bytes outgoing\n", mode
->stats
.tx_bytes
);
342 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
345 static void tx_fastpath_or_die(struct mode
*mode
)
348 unsigned int i
, size
, it
= 0;
349 unsigned long num
= 1;
352 struct frame_map
*hdr
;
353 struct timeval start
, end
, diff
;
359 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
362 mode
->device_trans
= xstrdup(mode
->device
);
365 enter_rfmon_mac80211(mode
->device_trans
, &mode
->device
);
368 ifindex
= device_ifindex(mode
->device
);
369 size
= ring_size(mode
->device
, mode
->reserve_size
);
371 set_sock_prio(sock
, 512);
372 set_packet_loss_discard(sock
);
373 setup_tx_ring_layout(sock
, &tx_ring
, size
, mode
->jumbo_support
);
374 create_tx_ring(sock
, &tx_ring
);
375 mmap_tx_ring(sock
, &tx_ring
);
376 alloc_tx_ring_frames(&tx_ring
);
377 bind_tx_ring(sock
, &tx_ring
, ifindex
);
379 if (mode
->cpu
>= 0 && ifindex
> 0) {
380 irq
= device_irq_number(mode
->device
);
381 device_bind_irq_to_cpu(mode
->cpu
, irq
);
382 printf("IRQ: %s:%d > CPU%d\n", mode
->device
, irq
,
387 interval
= mode
->kpull
;
391 printf("Note: randomizes output makes trafgen slower!\n");
393 printf("MD: TX fastpath %s %luus", mode
->rand
? "RND" : "RR", interval
);
395 printf(" 802.11 raw via %s", mode
->device
);
397 printf("Running! Hang up with ^C!\n\n");
399 itimer
.it_interval
.tv_sec
= 0;
400 itimer
.it_interval
.tv_usec
= interval
;
401 itimer
.it_value
.tv_sec
= 0;
402 itimer
.it_value
.tv_usec
= interval
;
403 setitimer(ITIMER_REAL
, &itimer
, NULL
);
407 bug_on(gettimeofday(&start
, NULL
));
409 while (likely(sigint
== 0) && likely(num
> 0)) {
410 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) &&
412 hdr
= tx_ring
.frames
[it
].iov_base
;
414 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
415 * sizeof(struct sockaddr_ll); */
416 out
= ((uint8_t *) hdr
) + TPACKET_HDRLEN
-
417 sizeof(struct sockaddr_ll
);
419 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
420 hdr
->tp_h
.tp_len
= packets
[i
].len
;
425 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
427 mode
->stats
.tx_bytes
+= packets
[i
].len
;
428 mode
->stats
.tx_packets
++;
431 i
= mt_rand_int32() % packets_len
;
434 atomic_cmp_swp(&i
, packets_len
, 0);
437 kernel_may_pull_from_tx(&hdr
->tp_h
);
438 next_slot_prewr(&it
, &tx_ring
);
442 if (unlikely(sigint
== 1))
447 bug_on(gettimeofday(&end
, NULL
));
448 diff
= tv_subtract(end
, start
);
450 destroy_tx_ring(sock
, &tx_ring
);
453 leave_rfmon_mac80211(mode
->device_trans
, mode
->device
);
459 printf("\r%12lu frames outgoing\n", mode
->stats
.tx_packets
);
460 printf("\r%12lu bytes outgoing\n", mode
->stats
.tx_bytes
);
461 printf("\r%12lu sec, %lu usec in total\n", diff
.tv_sec
, diff
.tv_usec
);
464 static void main_loop(struct mode
*mode
, char *confname
)
466 compile_packets(confname
, mode
->verbose
);
469 tx_slowpath_or_die(mode
);
471 tx_fastpath_or_die(mode
);
476 int main(int argc
, char **argv
)
478 int c
, opt_index
, i
, j
;
480 char *confname
= NULL
, *ptr
;
481 bool prio_high
= false;
482 bool setsockmem
= true;
485 check_for_root_maybe_die();
487 fmemset(&mode
, 0, sizeof(mode
));
488 mode
.cpu
= CPU_UNKNOWN
;
492 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
493 &opt_index
)) != EOF
) {
506 mode
.device
= xstrndup(optarg
, IFNAMSIZ
);
515 mode
.jumbo_support
= 1;
519 confname
= xstrdup(optarg
);
522 mode
.kpull
= atol(optarg
);
525 mode
.num
= atol(optarg
);
528 mode
.gap
= atol(optarg
);
535 mode
.reserve_size
= 0;
537 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
538 if (!isdigit(optarg
[j
- i
]))
543 if (!strncmp(ptr
, "KB", strlen("KB")))
544 mode
.reserve_size
= 1 << 10;
545 else if (!strncmp(ptr
, "MB", strlen("MB")))
546 mode
.reserve_size
= 1 << 20;
547 else if (!strncmp(ptr
, "GB", strlen("GB")))
548 mode
.reserve_size
= 1 << 30;
550 panic("Syntax error in ring size param!\n");
553 mode
.reserve_size
*= atoi(optarg
);
556 set_cpu_affinity(optarg
, 0);
557 /* Take the first CPU for rebinding the IRQ */
558 if (mode
.cpu
!= CPU_NOTOUCH
)
559 mode
.cpu
= atoi(optarg
);
562 set_cpu_affinity(optarg
, 1);
568 mode
.cpu
= CPU_NOTOUCH
;
582 panic("Option -%c requires an argument!\n",
586 whine("Unknown option character "
587 "`0x%X\'!\n", optopt
);
597 if (mode
.device
== NULL
)
598 panic("No networking device given!\n");
599 if (confname
== NULL
)
600 panic("No configuration file given!\n");
601 if (device_mtu(mode
.device
) == 0)
602 panic("This is no networking device!\n");
603 if (!mode
.rfraw
&& device_up_and_running(mode
.device
) == 0)
604 panic("Networking device not running!\n");
606 register_signal(SIGINT
, signal_handler
);
607 register_signal(SIGHUP
, signal_handler
);
608 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
612 if (prio_high
== true) {
613 set_proc_prio(get_default_proc_prio());
614 set_sched_status(get_default_sched_policy(),
615 get_default_sched_prio());
618 if (setsockmem
== true) {
619 if ((vals
[0] = get_system_socket_mem(sock_rmem_max
)) < SMEM_SUG_MAX
)
620 set_system_socket_mem(sock_rmem_max
, SMEM_SUG_MAX
);
621 if ((vals
[1] = get_system_socket_mem(sock_rmem_def
)) < SMEM_SUG_DEF
)
622 set_system_socket_mem(sock_rmem_def
, SMEM_SUG_DEF
);
623 if ((vals
[2] = get_system_socket_mem(sock_wmem_max
)) < SMEM_SUG_MAX
)
624 set_system_socket_mem(sock_wmem_max
, SMEM_SUG_MAX
);
625 if ((vals
[3] = get_system_socket_mem(sock_wmem_def
)) < SMEM_SUG_DEF
)
626 set_system_socket_mem(sock_wmem_def
, SMEM_SUG_DEF
);
629 main_loop(&mode
, confname
);
631 if (setsockmem
== true) {
632 set_system_socket_mem(sock_rmem_max
, vals
[0]);
633 set_system_socket_mem(sock_rmem_def
, vals
[1]);
634 set_system_socket_mem(sock_wmem_max
, vals
[2]);
635 set_system_socket_mem(sock_wmem_def
, vals
[3]);
639 free(mode
.device_trans
);