mausezahn: use getopt_long instead of getopt
[netsniff-ng.git] / trafgen.8
blob5191872716cbe8f896109b662c30ea1ccbae93ad
1 .\" netsniff-ng - the packet sniffing beast
2 .\" Copyright 2013 Daniel Borkmann.
3 .\" Subject to the GPL, version 2.
4 .TH TRAFGEN 8 "03 March 2013" "Linux" "netsniff-ng toolkit"
5 .SH NAME
6 trafgen \- a fast, multithreaded network packet generator
7 .PP
8 .SH SYNOPSIS
9 .PP
10 \fBtrafgen\fP [\fIoptions\fP] [\fIpacket\fP]
11 .PP
12 .SH DESCRIPTION
13 .PP
14 trafgen is a fast, zero-copy network traffic generator for debugging,
15 performance evaluation, and fuzz-testing. trafgen utilizes the
16 .BR 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.
24 .PP
25 trafgen is Linux specific, meaning there is no support for other operating
26 systems, same as
27 .BR netsniff-ng (8),
28 thus we can keep the code footprint quite minimal and to the point. trafgen
29 makes use of
30 .BR packet (7)
31 socket's TX_RING interface of the Linux kernel, which is a
32 .BR mmap (2)'ed
33 ring buffer shared between user and kernel space.
34 .PP
35 By default, trafgen starts as many processes as available CPUs, pins each
36 of them to their respective CPU and sets up the ring buffer each in their own
37 process space after having compiled a list of packets to transmit. Thus, this is
38 likely the fastest one can get out of the box in terms of transmission performance
39 from user space, without having to load unsupported or non-mainline third-party
40 kernel modules. On Gigabit Ethernet, trafgen has a comparable performance to
41 pktgen, the built-in Linux kernel traffic generator, except that trafgen is more
42 flexible in terms of packet configuration possibilities. On 10-Gigabit-per-second
43 Ethernet, trafgen might be slower than pktgen due to the user/kernel space
44 overhead but still has a fairly high performance for out of the box kernels.
45 .PP
46 trafgen has the potential to do fuzz testing, meaning a packet configuration can
47 be built with random numbers on all or certain packet offsets that are freshly
48 generated each time a packet is sent out. With a built-in IPv4 ping, trafgen can
49 send out an ICMP probe after each packet injection to the remote host in order
50 to test if it is still responsive/alive. Assuming there is no answer from the
51 remote host after a certain threshold of probes, the machine is considered dead
52 and the last sent packet is printed together with the random seed that was used
53 by trafgen. You might not really get lucky fuzz-testing the Linux kernel, but
54 presumably there are buggy closed-source embedded systems or network driver's
55 firmware files that are prone to bugs, where trafgen could help in finding them.
56 .PP
57 trafgen's configuration language is quite powerful, also due to the fact, that
58 it supports C preprocessor macros. A stddef.h is being shipped with trafgen for
59 this purpose, so that well known defines from Linux kernel or network programming
60 can be reused. After a configuration file has passed the C preprocessor stage,
61 it is processed by the trafgen packet compiler. The language itself supports a
62 couple of features that are useful when assembling packets, such as built-in
63 runtime checksum support for IP, UDP and TCP. Also it has an expression evaluator
64 where arithmetic (basic operations, bit operations, bit shifting, ...) on constant
65 expressions is being reduced to a single constant on compile time. Other features
66 are ''fill'' macros, where a packet can be filled with n bytes by a constant, a
67 compile-time random number or run-time random number (as mentioned with fuzz
68 testing). Also,
69 .BR netsniff-ng (8)
70 is able to convert a pcap file into a trafgen configuration file, thus such a
71 configuration can be further tweaked for a given scenario.
72 .PP
73 .SH OPTIONS
74 .TP
75 .B -i <cfg|pcap|->, -c <cfg|->, --in <cfg|pcap|->, --conf <cfg|->
76 Defines the input configuration file that can either be passed as a normal plain
77 text file or via stdin (''-''). Note that currently, if a configuration is
78 passed through stdin, only 1 CPU will be used.
79 It is also possible to specify PCAP file with .pcap extension via
80 \fB-i\fP/\fB--in\fP option, by default packets will be sent at rate considering
81 timestamp from PCAP file which might be reset via the \fB-b\fP or \fB-t\fP option.
82 .TP
83 .B -o <dev|.pcap|.cfg>, -d <dev|.pcap|.cfg>, --out <dev|.pcap|.cfg>, --dev <dev|.pcap|.cfg>
84 Defines the outgoing networking device such as eth0, wlan0 and others or
85 a *.pcap or *.cfg file. Pcap and configuration files are identified by extension.
86 .TP
87 .B -p, --cpp
88 Pass the packet configuration to the C preprocessor before reading it into
89 trafgen. This allows #define and #include directives (e.g. to include
90 definitions from system headers) to be used in the trafgen configuration file.
91 .TP
92 .B -D <name>=<definition>, --define <name>=<definition>
93 Add macro definition for the C preprocessor to use it within trafgen file. This
94 option is used in combination with the \fB-p\fP/\fB--cpp\fP option.
95 .TP
96 .B -J, --jumbo-support
97 By default trafgen's ring buffer frames are of a fixed size of 2048 bytes.
98 This means that if you're expecting jumbo frames or even super jumbo frames to
99 pass your line, then you will need to enable support for that with the help of
100 this option. However, this has the disadvantage of a performance regression and
101 a bigger memory footprint for the ring buffer.
103 .B -R, --rfraw
104 In case the output networking device is a wireless device, it is possible with
105 trafgen to turn this into monitor mode and create a mon<X> device that trafgen
106 will be transmitting on instead of wlan<X>, for instance. This enables trafgen
107 to inject raw 802.11 frames. In case if the output is a pcap file the link type
108 is set to 127 (ieee80211 radio tap).
110 .B -s <ipv4>, --smoke-test <ipv4>
111 In case this option is enabled, trafgen will perform a smoke test. In other
112 words, it will probe the remote end, specified by an <ipv4> address, that is
113 being ''attacked'' with trafgen network traffic, if it is still alive and
114 responsive. That means, after each transmitted packet that has been configured,
115 trafgen sends out ICMP echo requests and waits for an answer before it continues.
116 In case the remote end stays unresponsive, trafgen assumes that the machine
117 has crashed and will print out the content of the last packet as a trafgen
118 packet configuration and the random seed that has been used in order to
119 reproduce a possible bug. This might be useful when testing proprietary embedded
120 devices. It is recommended to have a direct link between the host running
121 trafgen and the host being attacked by trafgen.
123 .B -n <0|uint>, --num <0|uint>
124 Process a number of packets and then exit. If the number of packets is 0, then
125 this is equivalent to infinite packets resp. processing until interrupted.
126 Otherwise, a number given as an unsigned integer will limit processing.
128 .B -r, --rand
129 Randomize the packet selection of the configuration file. By default, if more
130 than one packet is defined in a packet configuration, packets are scheduled for
131 transmission in a round robin fashion. With this option, they are selected
132 randomly instread.
134 .B -P <uint>[-<uint>], --cpus <uint>[-<uint>]
135 Specify the number of processes trafgen shall
136 .BR fork (2)
137 off or list exact CPUs to use. By default trafgen will start as many processes
138 as CPUs that are online and pin them to each, respectively. A single integer
139 within interval [1,CPUs] overrides number of processes, which will be spawned
140 starting from the first CPU. A pair of integers within interval [0,CPUs-1], and
141 separated using  ''-'' represents an interval of CPUs, which will be used to
142 spawn worker processes.
144 .B -t <time>, --gap <time>
145 Specify a static inter-packet timegap in seconds, milliseconds, microseconds,
146 or nanoseconds: ''<num>s/ms/us/ns''. If no postfix is given default to
147 microseconds. If this option is given, then instead of
148 .BR packet (7)'s
149 TX_RING interface, trafgen will use
150 .BR sendto (2)
151 I/O for network packets, even if the <time> argument is 0. This option is useful
152 for a couple of reasons:
154   1) comparison between
155 .BR sendto (2)
156 and TX_RING performance,
157   2) low-traffic packet probing for a given interval,
158   3) ping-like debugging with specific payload patterns.
160 Furthermore, the TX_RING interface does not cope with interpacket gaps.
162 .B -b <rate>, --rate <rate>
163 Specify the packet send rate <num>pps/kpps/Mpps/B/kB/MB/GB/kbit/Mbit/Gbit/KiB/MiB/GiB units.
164 Like with the \fB-t\fP/\fB--gap\fP option, the packets are sent in slow mode.
166 .B -S <size>, --ring-size <size>
167 Manually define the TX_RING resp. TX_RING size in ''<num>KiB/MiB/GiB''. By
168 default the size is being determined based on the network connectivity rate.
170 .B -E <uint>, --seed <uint>
171 Manually set the seed for pseudo random number generator (PRNG) in trafgen. By
172 default, a random seed from /dev/urandom is used to feed glibc's PRNG. If that
173 fails, it falls back to the unix timestamp. It can be useful to set the seed
174 manually in order to be able to reproduce a trafgen session, e.g. after fuzz
175 testing.
177 .B -u <uid>, --user <uid> resp. -g <gid>, --group <gid>
178 After ring setup, drop privileges to a non-root user/group combination.
180 .B -H, --prio-high
181 Set this process as a high priority process in order to achieve a higher
182 scheduling rate resp. CPU time. This is however not the default setting, since
183 it could lead to starvation of other processes, for example low priority kernel
184 threads.
186 .B -A, --no-sock-mem
187 Do not change systems default socket memory setting during testrun.
188 Default is to boost socket buffer memory during the test to:
190   /proc/sys/net/core/rmem_default:4194304
191   /proc/sys/net/core/wmem_default:4194304
192   /proc/sys/net/core/rmem_max:104857600
193   /proc/sys/net/core/wmem_max:104857600
195 .B -Q, --notouch-irq
196 Do not reassign the NIC's IRQ CPU affinity settings.
198 .B -q, --qdisc-path
199 Since Linux 3.14, the kernel supports a socket option PACKET_QDISC_BYPASS,
200 which trafgen enables by default. This options disables the qdisc bypass,
201 and uses the normal send path through the kernel's qdisc (traffic control)
202 layer, which can be usefully for testing the qdisc path.
204 .B -V, --verbose
205 Let trafgen be more talkative and let it print the parsed configuration and
206 some ring buffer statistics.
208 .B -e, --example
209 Show a built-in packet configuration example. This might be a good starting
210 point for an initial packet configuration scenario.
212 .B -C, --no-cpu-stats
213 Do not print CPU time statistics on exit.
215 .B -v, --version
216 Show version information and exit.
218 .B -h, --help
219 Show user help and exit.
221 .SH SYNTAX
223 trafgen's packet configuration syntax is fairly simple. The very basic things
224 one needs to know is that a configuration file is a simple plain text file
225 where packets are defined. It can contain one or more packets. Packets are
226 enclosed by opening '{' and closing '}' braces, for example:
228    { /* packet 1 content goes here ... */ }
229    { /* packet 2 content goes here ... */ }
231 Alternatively, packets can also be specified directly on the command line, using
232 the same syntax as used in the configuration files.
234 When trafgen is started using multiple CPUs (default), then each of those packets
235 will be scheduled for transmission on all CPUs by default. However, it is possible
236 to tell trafgen to schedule a packet only on a particular CPU:
238    cpu(1): { /* packet 1 content goes here ... */ }
239    cpu(2-3): { /* packet 2 content goes here ... */ }
241 Thus, in case we have a 4 core machine with CPU0-CPU3, packet 1 will be scheduled
242 only on CPU1, packet 2 on CPU2 and CPU3. When using trafgen with \-\-num option,
243 then these constraints will still be valid and the packet is fairly distributed
244 among those CPUs.
246 Packet content is delimited either by a comma or whitespace, or both:
248    { 0xca, 0xfe, 0xba 0xbe }
250 Packet content can be of the following:
252    hex bytes:   0xca, xff
253    decimal:     42
254    binary:      0b11110000, b11110000
255    octal:       011
256    character:   'a'
257    string:      "hello world"
258    shellcode:   "\\x31\\xdb\\x8d\\x43\\x17\\x99\\xcd\\x80\\x31\\xc9"
260 Thus, a quite useless packet configuration might look like this (one can verify
261 this when running this with trafgen in combination with \-V):
263    { 0xca, 42, 0b11110000, 011, 'a', "hello world",
264      "\\x31\\xdb\\x8d\\x43\\x17\\x99\\xcd\\x80\\x31\\xc9" }
266 There are a couple of helper functions in trafgen's language to make life easier
267 to write configurations:
269 .B i) Fill with garbage functions:
271    byte fill function:      fill(<content>, <times>): fill(0xca, 128)
272    compile-time random:     rnd(<times>): rnd(128), rnd()
273    runtime random numbers:  drnd(<times>): drnd(128), drnd()
274    compile-time counter:    seqinc(<start-val>, <times>, <increment>)
275                             seqdec(<start-val>, <times>, <decrement>)
276    runtime counter (1byte): dinc(<min-val>, <max-val>, <increment>)
277                             ddec(<min-val>, <max-val>, <decrement>)
279 .B ii) Checksum helper functions (packet offsets start with 0):
281    IP/ICMP checksum:        csumip/csumicmp(<off-from>, <off-to>)
282    UDP checksum:            csumudp(<off-iphdr>, <off-udpdr>)
283    TCP checksum:            csumtcp(<off-iphdr>, <off-tcphdr>)
284    UDP checksum (IPv6):     csumudp6(<off-ip6hdr>, <off-udpdr>)
285    TCP checksum (IPv6):     csumtcp6(<off-ip6hdr>, <off-tcphdr>)
287 .B iii) Multibyte functions, compile-time expression evaluation:
289    const8(<content>), c8(<content>), const16(<content>), c16(<content>),
290    const32(<content>), c32(<content>), const64(<content>), c64(<content>)
292    These functions write their result in network byte order into the packet
293 configuration, e.g. const16(0xaa) will result in ''00 aa''. Within c*()
294 functions, it is possible to do some arithmetics: -,+,*,/,%,&,|,<<,>>,^
295 E.g. const16((((1<<8)+0x32)|0b110)*2) will be evaluated to ''02 6c''.
297 .B iv) Protocol header functions:
298 .in +4
299 The protocol header functions allow to fill protocol header fields by
300 using following generic syntax:
302 .in +4
303 <proto>(<field>=<value>,<field2>=<value2>,...,<field3>,...)
304 .in -4
306 .in -4
307 .in +4
308 If a field is not specified, then a default value will be used (usually 0).
309 Protocol fields might be set in any order. However, the offset of the fields in
310 the resulting packet is according to the respective protocol.
312 Each field might be set with a function which generates field value at runtime by
313 increment or randomize it. For L3/L4 protocols the checksum is calculated automatically
314 if the field was changed dynamically by specified function.  The following field
315 functions are supported:
316 .in +4
318 .B dinc
319 - increment field value at runtime. By default increment step is '1'.
320 .B min
322 .B max
323 parameters are used to increment field only in the specified range, by default original
324 field value is used. If the field length is greater than 4 then last 4 bytes are
325 incremented only (useful for MAC and IPv6 addresses):
326 .in +4
328 <field> = dinc() | dinc(min, max) | dinc(min, max, step)
329 .in -4
331 .B drnd
332 - randomize field value at runtime.
333 .B min
335 .B max
336 parameters are used to randomize field only in the specified range:
337 .in +4
339 <field> = drnd() | drnd(min, max)
340 .in -4
342 Example of using dynamic functions:
345 .in +2
346     eth(saddr=aa:bb:cc:dd:ee:ff, saddr=dinc()),
347     ipv4(saddr=dinc()),
348     udp(sport=dinc(1, 13, 2), dport=drnd(80, 100))
349 .in -2
352 .in -4
355 Fields might be further manipulated with a function at a specific offset:
357 .in +4
358 <field>[<index>] | <field>[<index>:<length>]
360 .in +4
361 <index> - relative field offset with range 0..<field.len> - 1
363 <length> - length/size of the value which will be set; either 1, 2 or 4 bytes (default: 1)
364 .in -4
366 The <index> starts from the field's first byte in network order.
368 The syntax is similar to the one used in pcap filters (man pcap-filter) for
369 matching header field at a specified offset.
371 Examples of using field offset (showing the effect in a shortenet output from
372 netsniff-ng):
374 .in +4
375 1) trafgen -o lo --cpus 1 -n 3 '{ eth(da=11:22:33:44:55:66, da[0]=dinc()), tcp() }'
377 .in +4
378 [ Eth MAC (00:00:00:00:00:00 => 11:22:33:44:55:66)
380 [ Eth MAC (00:00:00:00:00:00 => 12:22:33:44:55:66)
382 [ Eth MAC (00:00:00:00:00:00 => 13:22:33:44:55:66)
383 .in -4
385 2) trafgen -o lo --cpus 1 -n 3 '{ ipv4(da=1.2.3.4, da[0]=dinc()), tcp() }'
387 .in +4
388 [ IPv4 Addr (127.0.0.1 => 1.2.3.4)
390 [ IPv4 Addr (127.0.0.1 => 2.2.3.4)
392 [ IPv4 Addr (127.0.0.1 => 3.2.3.4)
393 .in -4
395 .in -4
396 .in -4
399 All required lower layer headers will be filled automatically if they were not
400 specified by the user. The headers will be filled in the order they were
401 specified. Each header will be filled with some mimimum required set of fields.
402 .in -4
404 .in +4
405 Supported protocol headers:
407 .I Ethernet
409 .B eth(da=<mac>, sa=<mac>, type=<number>)
411 .in +4
412 .B da|daddr
413 - Destination MAC address (default: 00:00:00:00:00:00)
415 .B sa|saddr
416 - Source MAC address (default: device MAC address)
418 .B etype|type|prot|proto
419 - Ethernet type (default: 0)
420 .in -4
423 .I PAUSE (IEEE 802.3X)
425 .B pause(code=<number>, time=<number>)
427 .in +4
428 .B code
429 - MAC Control opcode (default: 0x0001)
431 .B time
432 - Pause time (default: 0)
434 By default Ethernet header is added with a fields:
435 .in +4
437 Ethernet type - 0x8808
439 Destination MAC address - 01:80:C2:00:00:01
440 .in -4
442 .in -4
445 .I PFC
447 .B pfc(pri|prio(<number>)=<number>, time(<number>)=<number>)
449 .in +4
450 .B code
451 - MAC Control opcode (default: 0x0101)
453 .B pri|prio
454 - Priority enable vector (default: 0)
456 .B pri|prio(<number>)
457 - Enable/disable (0 - disable, 1 - enable) pause for priority <number> (default: 0)
459 .B time(<number>)
460 - Set pause time for priority <number> (default: 0)
462 By default Ethernet header is added with a fields:
463 .in +4
465 Ethernet type - 0x8808
467 Destination MAC address - 01:80:C2:00:00:01
468 .in -4
470 .in -4
471 .I VLAN
473 .B vlan(tpid=<number>, id=<number>, dei=<number>, tci=<number>, pcp=<number>, 1q, 1ad)
475 .in +4
476 .B tpid|prot|proto
477 - Tag Protocol Identifier (TPID) (default: 0x8100)
479 .B tci
480 - Tag Control Information (TCI) field (VLAN Id + PCP + DEI) (default: 0)
482 .B dei|cfi
483 - Drop Eligible Indicator (DEI), formerly Canonical Format Indicator (CFI) (default: 0)
485 .B pcp
486 - Priority code point (PCP) (default: 0)
488 .B id
489 - VLAN Identifier (default: 0)
491 .B 1q
492 - Set 802.1q header (TPID: 0x8100)
494 .B 1ad
495 - Set 802.1ad header (TPID: 0x88a8)
497 .in -4
498 By default, if the lower level header is Ethernet, its EtherType is set to
499 0x8100 (802.1q).
502 .I MPLS
504 .B mpls(label=<number>, tc|exp=<number>, last=<number>, ttl=<number>)
506 .in +4
507 .B label|lbl
508 - MPLS label value (default: 0)
510 .B tclass|tc|exp
511 - Traffic Class for QoS field (default: 0)
513 .B last
514 - Bottom of stack S-flag (default: 1 for most last label)
516 .B ttl
517 - Time To Live (TTL) (default: 0)
519 .in -4
520 By default, if the lower level header is Ethernet, its EtherType is set to
521 0x8847 (MPLS Unicast). S-flag is set automatically to 1 for the last label and
522 resets to 0 if the lower MPLS label was added after.
525 .I ARP
527 .B arp(htype=<number>, ptype=<number>, op=<request|reply|number>, request,
528 .B reply, smac=<mac>, sip=<ip4_addr>, tmac=<mac>, tip=<ip4_addr>)
530 .in +4
531 .B htype
532 - ARP hardware type (default: 1 [Ethernet])
534 .B ptype
535 - ARP protocol type (default: 0x0800 [IPv4])
537 .B op
538 - ARP operation type (request/reply) (default: request)
540 .B req|request
541 - ARP Request operation type
543 .B reply
544 - ARP Reply operation type
546 .B smac|sha
547 - Sender hardware (MAC) address (default: device MAC address)
549 .B sip|spa
550 - Sender protocol (IPv4) address (default: device IPv4 address)
552 .B tmac|tha
553 - Target hardware (MAC) address (default: 00:00:00:00:00:00)
555 .B tip|tpa
556 - Target protocol (IPv4) address (default: device IPv4 address)
557 .in -4
559 By default, the ARP operation field is set to request and the Ethernet
560 destination MAC address is set to the broadcast address (ff:ff:ff:ff:ff:ff).
562 .I IPv4
564 .B ip4|ipv4(ihl=<number>, ver=<number>, len=<number>, csum=<number>,
565 .B ttl=<number>, tos=<number>, dscp=<number>, ecn=<number>,
566 .in +16
567 .B id=<number>, flags=<number>, frag=<number>, df, mf, da=<ip4_addr>, sa=<ip4_addr>,
568 .B prot[o]=<number>)
569 .in -16
571 .in +4
572 .B ver|version
573 - Version field (default: 4)
575 .B ihl
576 - Header length in number of 32-bit words (default: 5)
578 .B tos
579 - Type of Service (ToS) field (default: 0)
581 .B dscp
582 - Differentiated Services Code Point (DSCP, DiffServ) field (default: 0)
584 .B ecn
585 - Explicit Congestion Notification (ECN) field (default: 0)
587 .B len|length
588 - Total length of header and payload (calculated by default)
590 .B id
591 - IPv4 datagram identification (default: 0)
593 .B flags
594 - IPv4 flags value (DF, MF) (default: 0)
596 .B df
597 - Don't fragment (DF) flag (default: 0)
599 .B mf
600 - More fragments (MF) flag (default: 0)
602 .B frag
603 - Fragment offset field in number of 8 byte blocks (default: 0)
605 .B ttl
606 - Time to live (TTL) field (default: 0)
608 .B csum
609 - Header checksum (calculated by default)
611 .B sa|saddr
612 - Source IPv4 address (default: device IPv4 address)
614 .B da|daddr
615 - Destination IPv4 address (default: 0.0.0.0)
617 .B prot|proto
618 - IPv4 protocol number (default: 0)
619 .in -4
621 By default, if the lower level header is Ethernet, its EtherType field is set to
622 0x0800 (IPv4). If the lower level header is IPv4, its protocol field is set to
623 0x4 (IP-in-IP).
625 .I IPv6
627 .B ip6|ipv6(ver=<number>, class=<number>, flow=<number> len=<number>,
628 .B nexthdr=<number>, hoplimit=<number>,
629 .in +16
630 .B da=<ip6_addr>, sa=<ip6_addr>)
631 .in -16
633 .in +4
634 .B ver|version
635 - Version field (default: 6)
637 .B tc|tclass
638 - Traffic class (default: 0)
640 .B fl|flow
641 - Flow label (default: 0)
643 .B len|length
644 - Payload length (calculated by default)
646 .B nh|nexthdr
647 - Type of next header, i.e. transport layer protocol number (default: 0)
649 .B hl|hoplimit|ttl
650 - Hop limit, i.e. time to live (default: 0)
652 .B sa|saddr
653 - Source IPv6 address (default: device IPv6 address)
655 .B da|daddr
656 - Destination IPv6 address (default: 0:0:0:0:0:0:0:0)
657 .in -4
659 By default, if the lower level header is Ethernet, its EtherType field is set to
660 0x86DD (IPv6).
662 .I ICMPv4
664 .B icmp4|icmpv4(type=<number>, code=<number>, echorequest, echoreply,
665 .B csum=<number>, mtu=<number>, seq=<number>, id=<number>, addr=<ip4_addr>)
667 .in +4
668 .B type
669 - Message type (default: 0 - Echo reply)
671 .B code
672 - Message code (default: 0)
674 .B echorequest
675 - ICMPv4 echo (ping) request (type: 8, code: 0)
677 .B echoreply
678 - ICMPv4 echo (ping) reply (type: 0, code: 0)
680 .B csum
681 - Checksum of ICMPv4 header and payload (calculated by default)
683 .B mtu
684 - Next-hop MTU field used in 'Datagram is too big' message type (default; 0)
686 .B seq
687 - Sequence number used in Echo/Timestamp/Address mask messages (default: 0)
689 .B id
690 - Identifier used in Echo/Timestamp/Address mask messages (default: 0)
692 .B addr
693 - IPv4 address used in Redirect messages (default: 0.0.0.0)
695 .in -4
696 Example ICMP echo request (ping):
697 .in +4
699 { icmpv4(echorequest, seq=1, id=1326) }
700 .in -4
702 .I ICMPv6
704 .B icmp6|icmpv6(type=<number>, echorequest, echoreply, code=<number>,
705 .B csum=<number>)
707 .in +4
708 .B type
709 - Message type (default: 0)
711 .B code
712 - Code (default: 0)
714 .B echorequest
715 - ICMPv6 echo (ping) request
717 .B echoreply
718 - ICMPv6 echo (ping) reply
720 .B csum
721 - Message checksum (calculated by default)
722 .in -4
724 By default, if the lower level header is IPv6, its Next Header field is set to
725 58 (ICMPv6).
727 .I UDP
729 .B udp(sp=<number>, dp=<number>, len=<number>, csum=<number>)
731 .in +4
732 .B sp|sport
733 - Source port (default: 0)
735 .B dp|dport
736 - Destination port (default: 0)
738 .B len|length
739 - Length of UDP header and data (calculated by default)
741 .B csum
742 - Checksum field over IPv4 pseudo header (calculated by default)
744 .in -4
745 By default, if the lower level header is IPv4, its protocol field is set to
746 0x11 (UDP).
748 .I TCP
750 .B tcp(sp=<number>, dp=<number>, seq=<number>, aseq|ackseq=<number>, doff|hlen=<number>,
751 .B cwr, ece|ecn, urg, ack, psh, rst, syn, fin, win|window=<number>, csum=<number>,
752 .B urgptr=<number>)
754 .in +4
755 .B sp|sport
756 - Source port (default: 0)
758 .B dp|dport
759 - Destination port (default: 0)
761 .B seq
762 - Sequence number (default: 0)
764 .B aseq|ackseq
765 - Acknowledgement number (default: 0)
767 .B doff|hlen
768 - Header size (data offset) in number of 32-bit words (default: 5)
770 .B cwr
771 - Congestion Window Reduced (CWR) flag (default: 0)
773 .B ece|ecn
774 - ECN-Echo (ECE) flag (default: 0)
776 .B urg
777 - Urgent flag (default: 0)
779 .B ack
780 - Acknowledgement flag (default: 0)
782 .B psh
783 - Push flag (default: 0)
785 .B rst
786 - Reset flag (default: 0)
788 .B syn
789 - Synchronize flag (default: 0)
791 .B fin
792 - Finish flag (default: 0)
794 .B win|window
795 - Receive window size (default: 0)
797 .B csum
798 - Checksum field over IPv4 pseudo header (calculated by default)
800 .B urgptr
801 - Urgent pointer (default: 0)
803 .in -4
804 By default, if the lower level header is IPv4, its protocol field is set to
805 0x6 (TCP).
807 Simple example of a UDP Echo packet:
809 .in +5
810    {
811      eth(da=11:22:33:44:55:66),
812      ipv4(daddr=1.2.3.4)
813      udp(dp=7),
814      "Hello world"
815    }
816 .in -5
818 Furthermore, there are two types of comments in trafgen configuration files:
820   1. Multi-line C-style comments:        /* put comment here */
821   2. Single-line Shell-style comments:   #  put comment here
823 Next to all of this, a configuration can be passed through the C preprocessor
824 before the trafgen compiler gets to see it with option \fB--cpp\fP. To give you a
825 taste of a more advanced example, run ''trafgen \-e'', fields are commented:
827    /* Note: dynamic elements make trafgen slower! */
828    #include <stddef.h>
830    {
831      /* MAC Destination */
832      fill(0xff, ETH_ALEN),
833      /* MAC Source */
834      0x00, 0x02, 0xb3, drnd(3),
835      /* IPv4 Protocol */
836      c16(ETH_P_IP),
837      /* IPv4 Version, IHL, TOS */
838      0b01000101, 0,
839      /* IPv4 Total Len */
840      c16(59),
841      /* IPv4 Ident */
842      drnd(2),
843      /* IPv4 Flags, Frag Off */
844      0b01000000, 0,
845      /* IPv4 TTL */
846      64,
847      /* Proto TCP */
848      0x06,
849      /* IPv4 Checksum (IP header from, to) */
850      csumip(14, 33),
851      /* Source IP */
852      drnd(4),
853      /* Dest IP */
854      drnd(4),
855      /* TCP Source Port */
856      drnd(2),
857      /* TCP Dest Port */
858      c16(80),
859      /* TCP Sequence Number */
860      drnd(4),
861      /* TCP Ackn. Number */
862      c32(0),
863      /* TCP Header length + TCP SYN/ECN Flag */
864      c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)
865      /* Window Size */
866      c16(16),
867      /* TCP Checksum (offset IP, offset TCP) */
868      csumtcp(14, 34),
869      /* TCP Options */
870      0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,
871      0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,
872      /* Data blob */
873      "gotcha!",
874    }
876 Another real-world example by Jesper Dangaard Brouer [1]:
878    {
879      # --- ethernet header ---
880      0x00, 0x1b, 0x21, 0x3c, 0x9d, 0xf8,  # mac destination
881      0x90, 0xe2, 0xba, 0x0a, 0x56, 0xb4,  # mac source
882      const16(0x0800), # protocol
883      # --- ip header ---
884      # ipv4 version (4-bit) + ihl (4-bit), tos
885      0b01000101, 0,
886      # ipv4 total len
887      const16(40),
888      # id (note: runtime dynamic random)
889      drnd(2),
890      # ipv4 3-bit flags + 13-bit fragment offset
891      # 001 = more fragments
892      0b00100000, 0,
893      64, # ttl
894      17, # proto udp
895      # dynamic ip checksum (note: offsets are zero indexed)
896      csumip(14, 33),
897      192, 168, 51, 1, # source ip
898      192, 168, 51, 2, # dest ip
899      # --- udp header ---
900      # as this is a fragment the below stuff does not matter too much
901      const16(48054), # src port
902      const16(43514), # dst port
903      const16(20),    # udp length
904      # udp checksum can be dyn calc via csumudp(offset ip, offset tcp)
905      # which is csumudp(14, 34), but for udp its allowed to be zero
906      const16(0),
907      # payload
908      'A',  fill(0x41, 11),
909    }
911    [1] https://marc.info/?l=linux-netdev&m=135903630614184
913 The above example rewritten using the header generation functions:
915    {
916      # --- ethernet header ---
917      eth(da=00:1b:21:3c:9d:f8, sa=90:e2:ba:0a:56:b4)
918      # --- ip header ---
919      ipv4(id=drnd(), mf, ttl=64, sa=192.168.51.1, da=192.168.51.2)
920      # --- udp header ---
921      udp(sport=48054, dport=43514, csum=0)
922      # payload
923      'A',  fill(0x41, 11),
924    }
926 .SH USAGE EXAMPLE
928 .B trafgen --dev eth0 --conf trafgen.cfg
929 This is the most simple and, probably, the most common use of trafgen. It
930 will generate traffic defined in the configuration file ''trafgen.cfg'' and
931 transmit this via the ''eth0'' networking device. All online CPUs are used.
933 .B trafgen --dev eth0 --conf trafgen.cfg --cpus 2-4
934 Instead of using all online CPUs, transmit traffic from CPUs 2, 3, and 4.
936 .B trafgen -e | trafgen -i - -o lo --cpp -n 1
937 This is an example where we send one packet of the built-in example through
938 the loopback device. The example configuration is passed via stdin and also
939 through the C preprocessor before trafgen's packet compiler will see it.
941 .B trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1
942 Read the ''fuzzing.cfg'' packet configuration file (which contains drnd()
943 calls) and send out the generated packets to the ''eth0'' device. After each
944 sent packet, ping probe the attacked host with address 10.0.0.1 to check if
945 it's still alive. This also means, that we utilize 1 CPU only, and do not
946 use the TX_RING, but sendto(2) packet I/O due to ''slow mode''.
948 .B trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2
949 As an output device ''wlan0'' is used and put into monitoring mode, thus we
950 are going to transmit raw 802.11 frames through the air. Use the ''beacon-test.txf''
951 configuration file, set trafgen into verbose mode and use only 2 CPUs starting
952 from CPU 0.
954 .B trafgen --dev em1 --conf frag_dos.cfg --rand --gap 1000us
955 Use trafgen in sendto(2) mode instead of TX_RING mode and sleep after each
956 sent packet a static timegap for 1000us. Generate packets from ''frag_dos.cfg''
957 and select next packets to send randomly instead of a round-robin fashion.
958 The output device for packets is ''em1''.
960 .B trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000
961 Send only 1400000 packets using the ''icmp.cfg'' configuration file and then
962 exit trafgen. Select packets randomly from that file for transmission and
963 send them out via ''eth0''. Also, trigger the kernel every 1000us for batching
964 the ring frames from user space (default is 10us).
966 .B trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`
967 Send out packets generated from the configuration file ''tcp_syn.cfg'' via
968 the ''eth0'' networking device. After setting up the ring for transmission,
969 drop credentials to the non-root user/group bob/bob.
971 .B trafgen --dev eth0 '{ fill(0xff, 6), 0x00, 0x02, 0xb3, rnd(3), c16(0x0800), fill(0xca, 64) }' -n 1
972 Send out 1 invaid IPv4 packet built from command line to all hosts.
974 .SH NOTE
976 trafgen can saturate a Gigabit Ethernet link without problems. As always,
977 of course, this depends on your hardware as well. Not everywhere where it
978 says Gigabit Ethernet on the box, will you reach almost physical line rate!
979 Please also read the
980 .BR netsniff-ng (8)
981 man page, section NOTE for further details about tuning your system e.g. with
982 .BR tuned (8).
984 If you intend to use trafgen on a 10-Gbit/s Ethernet NIC, make sure you
985 are using a multiqueue
986 .BR tc(8)
987 discipline, and make sure that the packets you generate with trafgen will have a
988 good distribution among tx_hashes so that you'll actually make use of
989 multiqueues.
991 For introducing bit errors, delays with random variation and more, there
992 is no built-in option in trafgen. Rather, one should reuse existing methods
993 for that which integrate nicely with trafgen, such as
994 .BR tc (8)
995 with its different disciplines, i.e. \fBnetem\fP.
997 For more complex packet configurations, it is recommended to use high-level
998 scripting for generating trafgen packet configurations in a more automated
999 way, i.e. also to create different traffic distributions that are common for
1000 industrial benchmarking:
1002     Traffic model              Distribution
1004     IMIX                       64:7,  570:4,  1518:1
1005     Tolly                      64:55,  78:5,   576:17, 1518:23
1006     Cisco                      64:7,  594:4,  1518:1
1007     RPR Trimodal               64:60, 512:20, 1518:20
1008     RPR Quadrimodal            64:50, 512:15, 1518:15, 9218:20
1010 The low-level nature of trafgen makes trafgen rather protocol independent
1011 and therefore useful in many scenarios when stress testing is needed, for
1012 instance. However, if a traffic generator with higher level packet
1013 descriptions is desired, netsniff-ng's mausezahn(8) can be of good use as
1014 well.
1016 For smoke/fuzz testing with trafgen, it is recommended to have a direct
1017 link between the host you want to analyze (''victim'' machine) and the host
1018 you run trafgen on (''attacker'' machine). If the ICMP reply from the victim
1019 fails, we assume that probably its kernel crashed, thus we print the last
1020 sent packet together with the seed and quit probing. It might be very unlikely
1021 to find such a ping-of-death on modern Linux systems. However, there might
1022 be a good chance to find it on some proprietary (e.g. embedded) systems or
1023 buggy driver firmwares that are in the wild. Also, fuzz testing can be done
1024 on raw 802.11 frames, of course. In case you find a ping-of-death, please
1025 mention that you were using trafgen in your commit message of the fix!
1027 .SH BUGS
1028 For old trafgen versions only, there could occur kernel crashes: we have fixed
1029 this bug in the mainline and stable kernels under commit 7f5c3e3a8 (''af_packet:
1030 remove BUG statement in tpacket_destruct_skb'') and also in trafgen.
1032 Probably the best is if you upgrade trafgen to the latest version.
1034 .SH LEGAL
1035 trafgen is licensed under the GNU GPL version 2.0.
1037 .SH HISTORY
1038 .B trafgen
1039 was originally written for the netsniff-ng toolkit by Daniel Borkmann. It
1040 is currently maintained by Tobias Klauser <tklauser@distanz.ch> and Daniel
1041 Borkmann <dborkma@tik.ee.ethz.ch>.
1043 .SH SEE ALSO
1044 .BR netsniff-ng (8),
1045 .BR mausezahn (8),
1046 .BR ifpps (8),
1047 .BR bpfc (8),
1048 .BR flowtop (8),
1049 .BR astraceroute (8),
1050 .BR curvetun (8)
1052 .SH AUTHOR
1053 Manpage was written by Daniel Borkmann.
1055 .SH COLOPHON
1056 This page is part of the Linux netsniff-ng toolkit project. A description of the project,
1057 and information about reporting bugs, can be found at http://netsniff-ng.org/.