man: improvements to bpfc.8, inconsistent use of `` and ''
[netsniff-ng.git] / trafgen.8
blob30638aa99da3afc7df4df57f5ab0621a77285d1f
1 .\" netsniff-ng - the packet sniffing beast
2 .\" Copyright 2013 Daniel Borkmann.
3 .\" Subject to the GPL, version 2.
5 .TH TRAFGEN 8 "03 March 2013" "Linux" "netsniff-ng toolkit"
6 .SH NAME
7 trafgen \- a fast, multithreaded network packet generator
9 .SH SYNOPSIS
11 \fB trafgen\fR [\fIoptions\fR]
13 .SH DESCRIPTION
15 trafgen is a fast, zero-copy network traffic generator for debugging,
16 performance evaluation and fuzz-testing purposes. trafgen utilizes the packet(7)
17 socket interface of Linux which postpones complete control over packet data
18 and packet headers into the user space. It has a powerful packet configuration
19 language, which is rather low-level and not limited to particular protocols.
20 Thus, trafgen can be used for many purposes. Its only limitation is that it
21 cannot mimic full streams resp. sessions. However, it is very useful for
22 various kinds of load testing in order to analyze and subsequently improve
23 systems behaviour under DoS attack scenarios, for instance.
25 trafgen is Linux specific only, meaning there is no support for other operating
26 systems just as in netsniff-ng(8), thus we can keep the code footprint quite
27 minimal and to the point. trafgen makes use of packet(7) socket's TX_RING
28 interface of the Linux kernel, which is a mmap(2)'ed ring buffer shared between
29 user and kernel space.
31 On default, trafgen starts as many processes as CPUs that are online, pins each
32 of them to their respective CPU and sets up the ring buffer each in their own
33 process space after having compiled a list of packets to transmit. Thus, this is
34 likely the fastest one can get out of the box in terms of transmission performance
35 from user space, without having to load unsupported or non-mainline third-party
36 kernel modules. On Gigabit Ethernet, trafgen has a comparable performance to
37 pktgen, the built-in Linux kernel traffic generator, only that trafgen is more
38 flexible in terms of packet configuration possibilities. On 10-Gigabit-per-second
39 Ethernet, trafgen might be slower than pktgen due to the user/kernel space
40 overhead but still has a fairly high performance for out of the box kernels.
42 trafgen has a possibility to do fuzz testing, meaning a packet configuration can
43 be built with random numbers on all or certain packet offsets that are freshly
44 generated each time a packet is sent out. With a built-in IPv4 ping, trafgen can
45 send out an ICMP probe after each packet injection to the remote host in order
46 to test if it is still responsive/alive. Assuming there is no answer from the
47 remote host after a certain threshold of probes, the machine is considered dead
48 and the last sent packet is printed together with the random seed that was used
49 by trafgen. You might not really get lucky fuzz-testing the Linux kernel, but
50 presumably there are buggy closed-source embedded systems or network driver's
51 firmware files that are prone to bugs, where trafgen could help in finding them.
53 trafgen's configuration language is quite powerful, also due to the fact, that
54 it supports C preprocessor macros. A stddef.h is being shipped with trafgen for
55 this purpose, so that well known defines from Linux kernel or network programming
56 can be reused. After a configuration file has passed the C preprocessor stage,
57 it is processed by the trafgen packet compiler. The language itself supports a
58 couple of features that are useful when assembling packets, such as built-in
59 runtime checksum support for IP, UDP and TCP. Also it has an expression evaluator
60 where arithmetic (basic operations, bit operations, bit shifting, ...) on constant
61 expressions is being reduced to a single constant on compile time. Other features
62 are ``fill'' macros, where a packet can be filled with n bytes by a constant, a
63 compile-time random number or run-time random number (as mentioned with fuzz
64 testing). Also, netsniff-ng(8) is able to convert a pcap file into a trafgen
65 configuration file, thus such a configuration can then be further tweaked for a
66 given scenario.
68 .SH OPTIONS
70 .SS -i <cfg|->, -c <cfg|i>, --in <cfg|->, --conf <cfg|->
71 Defines the input configuration file that can either be passed as a normal plain
72 text file or via stdin (``-''). Note that currently in case a configuration is
73 passed through stdin, only 1 CPU will be used.
75 .SS -o <dev>, -d <dev>, --out <dev>, --dev <dev>
76 Defines the outgoing networking device such as eth0, wlan0 and others.
78 .SS -p, --cpp
79 Pass the packet configuration to
81 .SS -J, --jumbo-support
82 On default trafgen's ring buffer frames are of a fixed size of 2048 bytes.
83 This means that if you're expecting jumbo frames or even super jumbo frames to
84 pass your line, then you need to enable support for that with the help of this
85 option. However, this has the disadvantage of a performance regression and a
86 bigger memory footprint for the ring buffer.
88 .SS -R, --rfraw
89 In case the output networking device is a wireless device, it is possible with
90 trafgen to turn this into monitor mode and create a mon<X> device that trafgen
91 will be transmitting on instead of wlan<X>, for instance. This enables trafgen
92 to inject raw 802.11 frames.
94 .SS -s <ipv4>, --smoke-test <ipv4>
95 In case this option is enabled, trafgen will perform a smoke test. In other
96 words, it will probe the remote end, specified by an <ipv4> address, that is
97 being ``attacked'' with trafgen network traffic, if it is still alive and
98 responsive. That means, after each transmitted packet that has been configured,
99 trafgen sends out ICMP echo requests and waits for an answer before it continues.
100 In case the remote end stays unresponsive, trafgen assumes that the machine
101 has crashed and will print out the content of the last packet as a trafgen
102 packet configuration and the random seed that has been used in order to
103 reproduce a possible bug. This might be useful when testing proprietary embedded
104 devices. It is recommended to have a direct link between the host running
105 trafgen and the host being attacked by trafgen.
107 .SS -n <0|uint>, --num <0|uint>
108 Process a number of packets and then exit. If the number of packets is 0, then
109 this is equivalent to infinite packets resp. processing until interrupted.
110 Otherwise, a number given as an unsigned integer will limit processing.
112 .SS -r, --rand
113 Randomize the packet selection of the configuration file. On default, if more
114 than one packet is defined in a packet configuration, packets are scheduled for
115 transmission in a round robin fashion. With this option, they are selected
116 randomly instread.
118 .SS -P <uint>, --cpus <uint>
119 Specify the number of processes trafgen shall fork(2) off. On default trafgen
120 will start as many processes as CPUs that are online and pin them to each,
121 respectively. Allowed value must be within interval [1,CPUs].
123 .SS -t <uint>, --gap <uint>
124 Specify a static inter-packet timegap in micro-seconds. If this option is given,
125 then instead of packet(7)'s TX_RING interface, trafgen will use sendto(2) I/O
126 for network packets, even if the <uint> argument is 0. This option is useful for
127 a couple of reasons: i) comparison between sendto(2) and TX_RING performance,
128 ii) low-traffic packet probing for a given interval, iii) ping-like debugging
129 with specific payload patterns. Furthermore, the TX_RING interface does not cope
130 with interpacket gaps.
132 .SS -S <size>, --ring-size <size>
133 Manually define the RX_RING resp. TX_RING size in ``<num>KiB/MiB/GiB''. On
134 default the size is being determined based on the network connectivity rate.
136 .SS -k <uint>, --kernel-pull <uint>
137 Manually define the interval in micro-seconds where the kernel should be triggered
138 to batch process the ring buffer frames. On default, it is every 10us, but it can
139 manually be prolonged, for instance..
141 .SS -E <uint>, --seed <uint>
142 Manually set the seed for trafgen. On default, a random seed from /dev/urandom
143 is being used to feed glibc's pseudo random number generator. If that fails, it
144 falls back to the unix timestamp. It can be useful to set it up manually to be
145 able to reproduce a trafgen session, e.g. after fuzz testing.
147 .SS -u <uid>, --user <uid> resp. -g <gid>, --group <gid>
148 After ring setup drop privileges to a non-root user/group combination.
150 .SS -V, --verbose
151 Let trafgen be more talkative and let it print the parsed configuration and
152 some ring buffer statistics.
154 .SS -e, --example
155 Show a built-in packet configuration example. This might be a good starting
156 point for an initial packet configuration scenario.
158 .SS -v, --version
159 Show versioning information.
161 .SS -h, --help
162 Show user help.
164 .SH SYNTAX
165 trafgen's packet configuration syntax is fairly simple. The very basic things
166 one need to know is that a configuration file is a simple plain text file
167 where packets are defined. It can contain one or more packets. Packet are
168 enclosed by opening '{' and closing '}' braces, for example:
170    { /* packet 1 content goes here ... */ }
171    { /* packet 2 content goes here ... */ }
173 When trafgen is started using multiple CPUs (default), then each of those packets
174 will be scheduled for transmission on all CPUs on default. However, it is possible
175 to tell trafgen to schedule a packet only on a particular CPU:
177    cpu(1): { /* packet 1 content goes here ... */ }
178    cpu(2-3): { /* packet 2 content goes here ... */ }
180 Thus, in case we have a 4 core machine with CPU0-CPU3, packet 1 will be scheduled
181 only on CPU1, packet 2 on CPU2 and CPU3. When using trafgen with --num option,
182 then these constraints will still be valid and the packet is fairly distributed
183 among those CPUs.
185 Packet content is delimited either by a comma or whitespace, or both:
187    { 0xca, 0xfe, 0xba 0xbe }
189 Packet content can be of the following:
191    hex bytes:   0xca, xff
192    decimal:     42
193    binary:      0b11110000, b11110000
194    octal:       011
195    character:   'a'
196    string:      "hello world"
197    shellcode:   "\\x31\\xdb\\x8d\\x43\\x17\\x99\\xcd\\x80\\x31\\xc9"
199 Thus, a quite useless packet packet configuration might look like this (one can
200 verify this when running this with trafgen in combination with -V):
202    { 0xca, 42, 0b11110000, 011, 'a', "hello world",
203      "\\x31\\xdb\\x8d\\x43\\x17\\x99\\xcd\\x80\\x31\\xc9" }
205 There are a couple of helper functions in trafgen's language to make life easier
206 to write configurations:
208 i) Fill with garbage functions:
210    byte fill function:      fill(<content>, <times>): fill(0xca, 128)
211    compile-time random:     rnd(<times>): rnd(128), rnd()
212    runtime random numbers:  drnd(<times>): drnd(128), drnd()
213    compile-time counter:    seqinc(<start-val>, <increment>, <times>)
214                             seqdec(<start-val>, <decrement>, <times>)
215    runtime counter (1byte): dinc(<min-val>, <max-val>, <increment>)
216                             ddec(<min-val>, <max-val>, <decrement>)
218 ii) Checksum helper functions (packet offsets start with 0):
220    IP/ICMP checksum:        csumip/csumicmp(<off-from>, <off-to>)
221    UDP checksum:            csumudp(<off-iphdr>, <off-udpdr>)
222    TCP checksum:            csumtcp(<off-iphdr>, <off-tcphdr>)
224 iii) Multibyte functions, compile-time expression evaluation:
226    const8(<content>), c8(<content>), const16(<content>), c16(<content>),
227    const32(<content>), c32(<content>), const64(<content>), c64(<content>)
229    These functions write their result in network byte order into the packet
230 configuration, e.g. const16(0xaa) will result in ``00 aa''. Within c*()
231 functions, it is possible to do some arithmetics: -,+,*,/,%,&,|,<<,>>,^
232 E.g. const16((((1<<8)+0x32)|0b110)*2) will be evaluated to ``02 6c''.
234 Furthermore, there are two types of comments in trafgen configuration files:
236   1. Multi-line C-style comments:        /* put comment here */
237   2. Single-line Shell-style comments:   #  put comment here
239 Next to all of this, a configuration can be passed through the C preprocessor
240 before the trafgen compiler gets to see it with option --cpp. To give you a
241 taste of a more advanced example, run ``trafgen -e'', fields are commented:
243    /* Note: dynamic elements make trafgen slower! */
244    #include <stddef.h>
246    {
247      /* MAC Destination */
248      fill(0xff, ETH_ALEN),
249      /* MAC Source */
250      0x00, 0x02, 0xb3, drnd(3),
251      /* IPv4 Protocol */
252      c16(ETH_P_IP),
253      /* IPv4 Version, IHL, TOS */
254      0b01000101, 0,
255      /* IPv4 Total Len */
256      c16(58),
257      /* IPv4 Ident */
258      drnd(2),
259      /* IPv4 Flags, Frag Off */
260      0b01000000, 0,
261      /* IPv4 TTL */
262      64,
263      /* Proto TCP */
264      0x06,
265      /* IPv4 Checksum (IP header from, to) */
266      csumip(14, 33),
267      /* Source IP */
268      drnd(4),
269      /* Dest IP */
270      drnd(4),
271      /* TCP Source Port */
272      drnd(2),
273      /* TCP Dest Port */
274      c16(80),
275      /* TCP Sequence Number */
276      drnd(4),
277      /* TCP Ackn. Number */
278      c32(0),
279      /* TCP Header length + TCP SYN/ECN Flag */
280      c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)
281      /* Window Size */
282      c16(16),
283      /* TCP Checksum (offset IP, offset TCP) */
284      csumtcp(14, 34),
285      /* TCP Options */
286      0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,
287      0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,
288      /* Data blob */
289      "gotcha!",
290    }
292 Another real-world example by Jesper Dangaard Brouer [1]:
294    {
295      # --- ethernet header ---
296      0x00, 0x1b, 0x21, 0x3c, 0x9d, 0xf8,  # mac destination
297      0x90, 0xe2, 0xba, 0x0a, 0x56, 0xb4,  # mac source
298      const16(0x0800), # protocol
299      # --- ip header ---
300      # ipv4 version (4-bit) + ihl (4-bit), tos
301      0b01000101, 0,
302      # ipv4 total len
303      const16(40),
304      # id (note: runtime dynamic random)
305      drnd(2),
306      # ipv4 3-bit flags + 13-bit fragment offset
307      # 001 = more fragments
308      0b00100000, 0,
309      64, # ttl
310      17, # proto udp
311      # dynamic ip checksum (note: offsets are zero indexed)
312      csumip(14, 33),
313      192, 168, 51, 1, # source ip
314      192, 168, 51, 2, # dest ip
315      # --- udp header ---
316      # as this is a fragment the below stuff does not matter too much
317      const16(48054), # src port
318      const16(43514), # dst port
319      const16(20),    # udp length
320      # udp checksum can be dyn calc via csumudp(offset ip, offset tcp)
321      # which is csumudp(14, 34), but for udp its allowed to be zero
322      const16(0),
323      # payload
324      'A',  fill(0x41, 11),
325    }
327    [1] http://thread.gmane.org/gmane.linux.network/257155
329 .SH USAGE EXAMPLE
331 .SS trafgen --dev eth0 --conf trafgen.cfg
332 This is the most simple and probably also most used command for trafgen. It
333 will generate traffic defined in the configuration file ``trafgen.cfg'' and
334 transmit this via the ``eth0'' networking device. All online CPUs are used.
336 .SS trafgen -e | trafgen -i - -o lo --cpp -n 1
337 This is an example where we send one packet of the built-in example through
338 the loopback device. The example configuration is passed via stdin and also
339 through the C preprocessor before trafgen's packet compiler will see it.
341 .SS trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1
342 Read the ``fuzzing.cfg'' packet configuration file (which contains drnd()
343 calls) and send out the generated packets to the ``eth0'' device. After each
344 sent packet, ping probe the attacked host with address 10.0.0.1 to check if
345 it's still alive. This also means, that we utilize 1 CPU only, and do not
346 use the TX_RING, but sendto(2) packet I/O due to ``slow mode''.
348 .SS trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2
349 As an output device ``wlan0'' is used and put into monitoring mode, thus we
350 are going to transmit raw 802.11 frames through the air. Use the
351 ``beacon-test.txf'' configuration file, set trafgen into verbose mode and
352 use only 2 CPUs.
354 .SS trafgen --dev em1 --conf frag_dos.cfg --rand --gap 1000
355 Use trafgen in sendto(2) mode instead of TX_RING mode and sleep after each
356 sent packet a static timegap for 1000us. Generate packets from ``frag_dos.cfg''
357 and select next packets to send randomly instead of a round-robin fashion.
358 The output device for packets is ``em1''.
360 .SS trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000
361 Send only 1400000 packets using the ``icmp.cfg'' configuration file and then
362 exit trafgen. Select packets randomly from that file for transmission and
363 send them out via ``eth0''. Also, trigger the kernel every 1000us for batching
364 the ring frames from user space (default is 10us).
366 .SS trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`
367 Send out packets generated from the configuration file ``tcp_syn.cfg'' via
368 the ``eth0'' networking device. After setting up the ring for transmission,
369 drop credentials to the non-root user/group bob/bob.
371 .SH NOTE
372 trafgen can saturate a Gigabit Ethernet link without problems. As always,
373 of course, this depends on your hardware as well. Not everywhere where it
374 says Gigabit Ethernet on the box, you'll reach almost physical line rate!
375 Please also read the netsniff-ng(8) man page, section NOTE for further
376 details about tuning your system e.g. with tuned(8).
378 If you intend to use trafgen on a 10-Gbit/s Ethernet NIC, make sure you
379 are using a multiqueue tc(8) discipline, and make sure that the packets
380 you generate with trafgen will have a good distribution among tx_hashes
381 so that you'll actually make use of multiqueues.
383 For introducing bit errors, delays with random variation and more, there
384 is no built-in option in trafgen. Rather, one should reuse existing methods
385 for that which integrate nicely with trafgen, such as tc(8) with its
386 different disciplines, i.e. netem.
388 For more complex packet configurations, it is recommended to use high-level
389 scripting for generating trafgen packet configurations in a more automated
390 way, i.e. also to create different traffic distributions that are common for
391 industrial benchmarking:
393     Traffic model              Distribution
395     IMIX                       64:7,  570:4,  1518:1
396     Tolly                      64:55,  78:5,   576:17, 1518:23
397     Cisco                      64:7,  594:4,  1518:1
398     RPR Trimodal               64:60, 512:20, 1518:20
399     RPR Quadrimodal            64:50, 512:15, 1518:15, 9218:20
401 The low-level nature of trafgen makes trafgen rather protocol independant
402 and therefore useful in many scenarios when stress testing is needed, for
403 instance. However, if a traffic generator with higher level packet
404 descriptions is desired, netsniff-ng's mausezahn(8) can be of good use as
405 well.
407 For smoke/fuzz testing with trafgen, it is recommended to have a direct
408 link between the host you want to analyze (``victim'' machine) and the host
409 you run trafgen on (``attacker'' machine). If the ICMP reply from the victim
410 fails, we assume that probably its kernel crashed, thus we print the last
411 sent packet togther with the seed and quit probing. It might be very unlikely
412 to find such a ping-of-death on modern Linux systems. However, there might
413 be a good chance to find it on some proprietary (e.g. embedded) systems or
414 buggy driver firmwares that are in the wild. Also, fuzz testing can be done
415 on raw 802.11 frames, of course. In case you find a ping-of-death, please
416 mention that you were using trafgen in your commit message of the fix!
418 .SH BUGS
419 When I start trafgen, my kernel crashes: we have fixed this bug in the
420 mainline and stable kernels under commit 7f5c3e3a8 (``af_packet: remove
421 BUG statement in tpacket_destruct_skb''). Either update your kernel to
422 the latest version, e.g. clone and build it from git.kernel.org, to a
423 stable kernel, or don't start multiple trafgen instances at once resp.
424 start trafgen with flag -A to disable temporary socket memory adaptation!
426 .SH LEGAL
427 trafgen is licensed under the GNU GPL version 2.0.
429 .SH HISTORY
430 .B trafgen
431 was originally written for the netsniff-ng toolkit by Daniel Borkmann. It
432 is currently maintained by Tobias Klauser <tklauser@distanz.ch> and Daniel
433 Borkmann <dborkma@tik.ee.ethz.ch>.
435 .SH SEE ALSO
436 .BR netsniff-ng (8),
437 .BR mausezahn (8),
438 .BR ifpps (8),
439 .BR bpfc (8),
440 .BR flowtop (8),
441 .BR astraceroute (8),
442 .BR curvetun (8)
444 .SH AUTHOR
445 Manpage was written by Daniel Borkmann.