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