2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
4 * Swiss federal institute of technology (ETH Zurich)
5 * Subject to the GPL, version 2.
14 #include <sys/socket.h>
15 #include <sys/types.h>
16 #include <sys/fsuid.h>
17 #include <sys/prctl.h>
22 #include <net/ethernet.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <linux/icmp.h>
27 #include <arpa/inet.h>
52 #include "trafgen_conf.h"
59 bool rand
, rfraw
, jumbo_support
, verbose
, smoke_test
, enforce
, qdisc_path
;
64 char *device
, *device_trans
, *rhost
;
66 struct sockaddr_in dest
;
70 unsigned long tv_sec
, tv_usec
;
71 unsigned long long tx_packets
, tx_bytes
;
72 unsigned long long cf_packets
, cf_bytes
;
73 unsigned long long cd_packets
;
77 static sig_atomic_t sigint
= 0;
79 struct packet
*packets
= NULL
;
82 struct packet_dyn
*packet_dyn
= NULL
;
85 static const char *short_options
= "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQq";
86 static const struct option long_options
[] = {
87 {"dev", required_argument
, NULL
, 'd'},
88 {"out", required_argument
, NULL
, 'o'},
89 {"in", required_argument
, NULL
, 'i'},
90 {"conf", required_argument
, NULL
, 'c'},
91 {"num", required_argument
, NULL
, 'n'},
92 {"gap", required_argument
, NULL
, 't'},
93 {"cpus", required_argument
, NULL
, 'P'},
94 {"ring-size", required_argument
, NULL
, 'S'},
95 {"kernel-pull", required_argument
, NULL
, 'k'},
96 {"smoke-test", required_argument
, NULL
, 's'},
97 {"seed", required_argument
, NULL
, 'E'},
98 {"user", required_argument
, NULL
, 'u'},
99 {"group", required_argument
, NULL
, 'g'},
100 {"prio-high", no_argument
, NULL
, 'H'},
101 {"notouch-irq", no_argument
, NULL
, 'Q'},
102 {"no-sock-mem", no_argument
, NULL
, 'A'},
103 {"qdisc-path", no_argument
, NULL
, 'q'},
104 {"jumbo-support", no_argument
, NULL
, 'J'},
105 {"no-cpu-stats", no_argument
, NULL
, 'C'},
106 {"cpp", no_argument
, NULL
, 'p'},
107 {"rfraw", no_argument
, NULL
, 'R'},
108 {"rand", no_argument
, NULL
, 'r'},
109 {"verbose", no_argument
, NULL
, 'V'},
110 {"version", no_argument
, NULL
, 'v'},
111 {"example", no_argument
, NULL
, 'e'},
112 {"help", no_argument
, NULL
, 'h'},
116 static const char *copyright
= "Please report bugs to <bugs@netsniff-ng.org>\n"
117 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
118 "Swiss federal institute of technology (ETH Zurich)\n"
119 "License: GNU GPL version 2.0\n"
120 "This is free software: you are free to change and redistribute it.\n"
121 "There is NO WARRANTY, to the extent permitted by law.";
124 static struct cpu_stats
*stats
;
125 static unsigned int seed
;
127 #define CPU_STATS_STATE_CFG 1
128 #define CPU_STATS_STATE_CHK 2
129 #define CPU_STATS_STATE_RES 4
132 # define ICMP_FILTER 1
139 #define SMOKE_N_PROBES 100
141 #define PKT_MIN_LEN 14
143 static void signal_handler(int number
)
156 static void __noreturn
help(void)
158 printf("trafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
159 puts("http://www.netsniff-ng.org\n\n"
160 "Usage: trafgen [options]\n"
162 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
163 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
164 " -p|--cpp Run packet config through C preprocessor\n"
165 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
166 " -R|--rfraw Inject raw 802.11 frames\n"
167 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
168 " -n|--num <uint> Number of packets until exit (def: 0)\n"
169 " -r|--rand Randomize packet selection (def: round robin)\n"
170 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
171 " -t|--gap <time> Set approx. interpacket gap (s/ms/us/ns, def: us)\n"
172 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
173 " -E|--seed <uint> Manually set srand(3) seed\n"
174 " -u|--user <userid> Drop privileges and change to userid\n"
175 " -g|--group <groupid> Drop privileges and change to groupid\n"
176 " -H|--prio-high Make this high priority process\n"
177 " -A|--no-sock-mem Don't tune core socket memory\n"
178 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
179 " -q|--qdisc-path Enabled qdisc kernel path (default off since 3.14)\n"
180 " -V|--verbose Be more verbose\n"
181 " -C|--no-cpu-stats Do not print CPU time statistics on exit\n"
182 " -v|--version Show version and exit\n"
183 " -e|--example Show built-in packet config example\n"
184 " -h|--help Guess what?!\n\n"
186 " trafgen --dev eth0 --conf trafgen.cfg\n"
187 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
188 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
189 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
190 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
191 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
192 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
193 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
194 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
195 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
196 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
197 "Generate config files from existing pcap using netsniff-ng:\n"
198 " netsniff-ng --in dump.pcap --out dump.cfg\n"
200 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
201 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
202 " we assume the kernel crashed, thus we print the packet and quit.\n"
203 " In case you find a ping-of-death, please mention trafgen in your\n"
204 " commit message of the fix!\n\n"
205 " For introducing bit errors, delays with random variation and more,\n"
206 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
207 " For generating different package distributions, you can use scripting\n"
208 " to generate a trafgen config file with packet ratios as:\n\n"
209 " IMIX 64:7, 570:4, 1518:1\n"
210 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
211 " Cisco 64:7, 594:4, 1518:1\n"
212 " RPR Trimodal 64:60, 512:20, 1518:20\n"
213 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n");
218 static void __noreturn
example(void)
221 "/* Note: dynamic elements make trafgen slower! */\n"
222 "#include <stddef.h>\n\n"
224 " /* MAC Destination */\n"
225 " fill(0xff, ETH_ALEN),\n"
226 " /* MAC Source */\n"
227 " 0x00, 0x02, 0xb3, drnd(3),\n"
228 " /* IPv4 Protocol */\n"
230 " /* IPv4 Version, IHL, TOS */\n"
232 " /* IPv4 Total Len */\n"
234 " /* IPv4 Ident */\n"
236 " /* IPv4 Flags, Frag Off */\n"
242 " /* IPv4 Checksum (IP header from, to) */\n"
248 " /* TCP Source Port */\n"
250 " /* TCP Dest Port */\n"
252 " /* TCP Sequence Number */\n"
254 " /* TCP Ackn. Number */\n"
256 " /* TCP Header length + TCP SYN/ECN Flag */\n"
257 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
258 " /* Window Size */\n"
260 " /* TCP Checksum (offset IP, offset TCP) */\n"
261 " csumtcp(14, 34),\n"
262 " /* TCP Options */\n"
263 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
264 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
272 static void __noreturn
version(void)
274 printf("trafgen %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
275 puts("multithreaded zero-copy network packet generator\n"
276 "http://www.netsniff-ng.org\n");
281 static void apply_counter(int id
)
283 size_t j
, counter_max
= packet_dyn
[id
].clen
;
285 for (j
= 0; j
< counter_max
; ++j
) {
287 struct counter
*counter
= &packet_dyn
[id
].cnt
[j
];
289 val
= counter
->val
- counter
->min
;
291 switch (counter
->type
) {
293 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
296 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
302 counter
->val
= val
+ counter
->min
;
303 packets
[id
].payload
[counter
->off
] = val
;
307 static void apply_randomizer(int id
)
309 size_t j
, rand_max
= packet_dyn
[id
].rlen
;
311 for (j
= 0; j
< rand_max
; ++j
) {
312 uint8_t val
= (uint8_t) rand();
313 struct randomizer
*randomizer
= &packet_dyn
[id
].rnd
[j
];
315 packets
[id
].payload
[randomizer
->off
] = val
;
319 static void apply_csum16(int id
)
321 size_t j
, csum_max
= packet_dyn
[id
].slen
;
323 for (j
= 0; j
< csum_max
; ++j
) {
325 struct csum16
*csum
= &packet_dyn
[id
].csum
[j
];
327 fmemset(&packets
[id
].payload
[csum
->off
], 0, sizeof(sum
));
328 if (unlikely((size_t) csum
->to
>= packets
[id
].len
))
329 csum
->to
= packets
[id
].len
- 1;
331 switch (csum
->which
) {
333 sum
= calc_csum(packets
[id
].payload
+ csum
->from
,
334 csum
->to
- csum
->from
+ 1, 0);
337 sum
= p4_csum((void *) packets
[id
].payload
+ csum
->from
,
338 packets
[id
].payload
+ csum
->to
,
339 (packets
[id
].len
- csum
->to
),
343 sum
= p4_csum((void *) packets
[id
].payload
+ csum
->from
,
344 packets
[id
].payload
+ csum
->to
,
345 (packets
[id
].len
- csum
->to
),
353 fmemcpy(&packets
[id
].payload
[csum
->off
], &sum
, sizeof(sum
));
357 static struct cpu_stats
*setup_shared_var(unsigned int cpus
)
360 size_t len
= cpus
* sizeof(struct cpu_stats
);
361 char *zbuff
, file
[256];
362 struct cpu_stats
*buff
;
364 zbuff
= xzmalloc(len
);
365 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
367 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
371 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
373 write_or_die(fd
, zbuff
, len
);
376 buff
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
378 if (buff
== MAP_FAILED
)
379 panic("Cannot setup shared variable!\n");
384 memset(buff
, 0, len
);
388 static void destroy_shared_var(void *buff
, unsigned int cpus
)
390 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
393 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
398 for (i
= 0; i
< len
; ++i
) {
401 printf("0x%02x, ", payload
[i
]);
407 static int xmit_smoke_setup(struct ctx
*ctx
)
409 int icmp_sock
, ret
, ttl
= 64;
410 struct icmp_filter filter
;
412 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
414 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
416 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
418 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
420 panic("Cannot install filter!\n");
422 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
424 panic("Cannot set TTL!\n");
426 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
427 ctx
->dest
.sin_family
= AF_INET
;
428 ctx
->dest
.sin_port
= 0;
430 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
432 panic("Cannot resolv address!\n");
437 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
441 short ident
, cnt
= 1, idstore
[SMOKE_N_PROBES
];
442 uint8_t outpack
[512], *data
;
443 struct icmphdr
*icmp
;
445 size_t len
= sizeof(*icmp
) + 56;
446 struct sockaddr_in from
;
448 struct pollfd fds
= {
453 fmemset(idstore
, 0, sizeof(idstore
));
454 for (j
= 0; j
< SMOKE_N_PROBES
; j
++) {
455 while ((ident
= htons((short) rand())) == 0)
459 memset(outpack
, 0, sizeof(outpack
));
460 icmp
= (void *) outpack
;
461 icmp
->type
= ICMP_ECHO
;
462 icmp
->un
.echo
.id
= ident
;
463 icmp
->un
.echo
.sequence
= htons(cnt
++);
465 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
466 for (i
= 0; i
< 56; ++i
)
467 data
[i
] = (uint8_t) rand();
469 icmp
->checksum
= csum((unsigned short *) outpack
,
470 len
/ sizeof(unsigned short));
472 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
473 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
474 if (unlikely(ret
!= (int) len
))
475 panic("Cannot send out probe: %s!\n", strerror(errno
));
477 ret
= poll(&fds
, 1, 50);
479 panic("Poll failed!\n");
481 if (fds
.revents
& POLLIN
) {
482 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
483 (struct sockaddr
*) &from
, &from_len
);
484 if (unlikely(ret
<= 0))
485 panic("Probe receive failed!\n");
486 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
488 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
490 if (unlikely((size_t) ret
< sizeof(*ip
) + sizeof(*icmp
)))
492 ip
= (void *) outpack
;
493 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > (size_t) ret
))
495 icmp
= (void *) outpack
+ ip
->ihl
* 4;
496 for (i
= 0; i
< array_size(idstore
); ++i
) {
497 if (unlikely(icmp
->un
.echo
.id
!= idstore
[i
]))
507 static void xmit_slowpath_or_die(struct ctx
*ctx
, unsigned int cpu
, unsigned long orig_num
)
509 int ret
, icmp_sock
= -1;
510 unsigned long num
= 1, i
= 0;
511 struct timeval start
, end
, diff
;
512 unsigned long long tx_bytes
= 0, tx_packets
= 0;
513 struct packet_dyn
*pktd
;
514 struct sockaddr_ll saddr
= {
515 .sll_family
= PF_PACKET
,
516 .sll_halen
= ETH_ALEN
,
517 .sll_ifindex
= device_ifindex(ctx
->device
),
522 if (ctx
->num
== 0 && orig_num
> 0)
526 icmp_sock
= xmit_smoke_setup(ctx
);
528 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
530 bug_on(gettimeofday(&start
, NULL
));
532 while (likely(sigint
== 0 && num
> 0 && plen
> 0)) {
533 pktd
= &packet_dyn
[i
];
534 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
540 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
541 (struct sockaddr
*) &saddr
, sizeof(saddr
));
542 if (unlikely(ret
< 0)) {
543 if (errno
== ENOBUFS
) {
548 panic("Sendto error: %s!\n", strerror(errno
));
551 tx_bytes
+= packets
[i
].len
;
554 if (ctx
->smoke_test
) {
555 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
556 if (unlikely(ret
< 0)) {
557 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
558 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
559 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
562 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
577 if ((ctx
->gap
.tv_sec
| ctx
->gap
.tv_nsec
) > 0)
578 nanosleep(&ctx
->gap
, NULL
);
581 bug_on(gettimeofday(&end
, NULL
));
582 timersub(&end
, &start
, &diff
);
587 stats
[cpu
].tx_packets
= tx_packets
;
588 stats
[cpu
].tx_bytes
= tx_bytes
;
589 stats
[cpu
].tv_sec
= diff
.tv_sec
;
590 stats
[cpu
].tv_usec
= diff
.tv_usec
;
592 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
595 static void xmit_fastpath_or_die(struct ctx
*ctx
, unsigned int cpu
, unsigned long orig_num
)
597 int ifindex
= device_ifindex(ctx
->device
);
600 unsigned long num
= 1, i
= 0;
601 size_t size
= ring_size(ctx
->device
, ctx
->reserve_size
);
603 struct frame_map
*hdr
;
604 struct timeval start
, end
, diff
;
605 struct packet_dyn
*pktd
;
606 unsigned long long tx_bytes
= 0, tx_packets
= 0;
608 set_sock_prio(sock
, 512);
610 ring_tx_setup(&tx_ring
, sock
, size
, ifindex
, ctx
->jumbo_support
, ctx
->verbose
);
612 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
616 if (ctx
->num
== 0 && orig_num
> 0)
619 bug_on(gettimeofday(&start
, NULL
));
621 while (likely(sigint
== 0 && num
> 0 && plen
> 0)) {
622 if (!user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
623 int ret
= pull_and_flush_tx_ring(sock
);
624 if (unlikely(ret
< 0)) {
625 /* We could hit EBADF if the socket has been closed before
626 * the timer was triggered.
628 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
629 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
635 hdr
= tx_ring
.frames
[it
].iov_base
;
636 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
638 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
639 hdr
->tp_h
.tp_len
= packets
[i
].len
;
641 pktd
= &packet_dyn
[i
];
642 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
648 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
650 tx_bytes
+= packets
[i
].len
;
660 kernel_may_pull_from_tx(&hdr
->tp_h
);
663 if (it
>= tx_ring
.layout
.tp_frame_nr
)
670 bug_on(gettimeofday(&end
, NULL
));
671 timersub(&end
, &start
, &diff
);
673 pull_and_flush_tx_ring_wait(sock
);
674 destroy_tx_ring(sock
, &tx_ring
);
676 stats
[cpu
].tx_packets
= tx_packets
;
677 stats
[cpu
].tx_bytes
= tx_bytes
;
678 stats
[cpu
].tv_sec
= diff
.tv_sec
;
679 stats
[cpu
].tv_usec
= diff
.tv_usec
;
681 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
684 static inline void __set_state(unsigned int cpu
, sig_atomic_t s
)
686 stats
[cpu
].state
= s
;
689 static inline sig_atomic_t __get_state(unsigned int cpu
)
691 return stats
[cpu
].state
;
694 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, unsigned int cpu
)
699 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
703 while ((__get_state(i
) &
704 (CPU_STATS_STATE_CFG
|
705 CPU_STATS_STATE_RES
)) == 0 &&
709 total
+= stats
[i
].cf_packets
;
715 static void __correct_global_delta(struct ctx
*ctx
, unsigned int cpu
, unsigned long orig
)
720 long long delta_correction
= 0;
722 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
726 while ((__get_state(i
) &
727 (CPU_STATS_STATE_CHK
|
728 CPU_STATS_STATE_RES
)) == 0 &&
732 total
+= stats
[i
].cd_packets
;
736 delta_correction
= -1 * ((long long) total
- orig
);
738 delta_correction
= +1 * ((long long) orig
- total
);
740 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
741 if (stats
[i
].cd_packets
> 0) {
742 if ((long long) stats
[i
].cd_packets
+
743 delta_correction
>= 0) {
750 if ((int) cpu
== cpu_sel
)
751 ctx
->num
+= delta_correction
;
754 static void __set_state_cf(unsigned int cpu
, unsigned long p
, unsigned long b
,
757 stats
[cpu
].cf_packets
= p
;
758 stats
[cpu
].cf_bytes
= b
;
759 stats
[cpu
].state
= s
;
762 static void __set_state_cd(unsigned int cpu
, unsigned long p
, sig_atomic_t s
)
764 stats
[cpu
].cd_packets
= p
;
765 stats
[cpu
].state
= s
;
768 static int xmit_packet_precheck(struct ctx
*ctx
, unsigned int cpu
)
771 unsigned long plen_total
, orig
= ctx
->num
;
772 size_t mtu
, total_len
= 0;
774 bug_on(plen
!= dlen
);
776 for (i
= 0; i
< plen
; ++i
)
777 total_len
+= packets
[i
].len
;
779 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
780 plen_total
= __wait_and_sum_others(ctx
, cpu
);
783 ctx
->num
= (unsigned long) round((1.0 * plen
/ plen_total
) * orig
);
785 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
786 CPU_STATS_STATE_CFG
);
787 __correct_global_delta(ctx
, cpu
, orig
);
791 __set_state(cpu
, CPU_STATS_STATE_RES
);
795 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
796 if (packets
[i
].len
> mtu
+ PKT_MIN_LEN
)
797 panic("Device MTU < than packet%d's size!\n", i
);
798 if (packets
[i
].len
<= PKT_MIN_LEN
)
799 panic("Packet%d's size must be > %d bytes!\n",
806 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
807 unsigned int cpu
, bool invoke_cpp
, unsigned long orig_num
)
809 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
810 if (xmit_packet_precheck(ctx
, cpu
) < 0)
815 size_t total_len
= 0, total_pkts
= 0;
817 for (i
= 0; i
< ctx
->cpus
; ++i
) {
818 total_len
+= stats
[i
].cf_bytes
;
819 total_pkts
+= stats
[i
].cf_packets
;
822 printf("%6zu packets to schedule\n", total_pkts
);
823 printf("%6zu bytes in total\n", total_len
);
824 printf("Running! Hang up with ^C!\n\n");
830 if (ctx
->qdisc_path
== false)
831 set_sock_qdisc_bypass(sock
, ctx
->verbose
);
834 xmit_slowpath_or_die(ctx
, cpu
, orig_num
);
836 xmit_fastpath_or_die(ctx
, cpu
, orig_num
);
843 static unsigned int generate_srand_seed(void)
848 fd
= open("/dev/urandom", O_RDONLY
);
852 read_or_die(fd
, &_seed
, sizeof(_seed
));
858 static void on_panic_del_rfmon(void *arg
)
860 leave_rfmon_mac80211(arg
);
863 int main(int argc
, char **argv
)
865 bool slow
= false, invoke_cpp
= false, reseed
= true, cpustats
= true;
866 bool prio_high
= false, set_irq_aff
= true, set_sock_mem
= true;
867 int c
, opt_index
, vals
[4] = {0}, irq
;
870 char *confname
= NULL
, *ptr
;
871 unsigned long cpus_tmp
, orig_num
= 0;
872 unsigned long long tx_packets
, tx_bytes
;
875 fmemset(&ctx
, 0, sizeof(ctx
));
876 ctx
.cpus
= get_number_cpus_online();
879 ctx
.qdisc_path
= false;
881 /* Keep an initial small default size to reduce cache-misses. */
882 ctx
.reserve_size
= 512 * (1 << 10);
884 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
885 &opt_index
)) != EOF
) {
906 cpus_tmp
= strtoul(optarg
, NULL
, 0);
907 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
912 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
918 set_sock_mem
= false;
924 ctx
.qdisc_path
= true;
932 ctx
.smoke_test
= true;
933 ctx
.rhost
= xstrdup(optarg
);
939 ctx
.jumbo_support
= true;
943 confname
= xstrdup(optarg
);
944 if (!strncmp("-", confname
, strlen("-")))
948 ctx
.uid
= strtoul(optarg
, NULL
, 0);
952 ctx
.gid
= strtoul(optarg
, NULL
, 0);
956 printf("Option -k/--kernel-pull is no longer used and "
957 "will be removed in a future release!\n");
960 seed
= strtoul(optarg
, NULL
, 0);
964 orig_num
= strtoul(optarg
, NULL
, 0);
970 prctl(PR_SET_TIMERSLACK
, 1UL);
971 gap
= strtoul(optarg
, NULL
, 0);
973 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
974 if (!isdigit(optarg
[j
- i
]))
979 if (!strncmp(ptr
, "ns", strlen("ns"))) {
980 ctx
.gap
.tv_sec
= gap
/ 1000000000;
981 ctx
.gap
.tv_nsec
= gap
% 1000000000;
982 } else if (*ptr
== '\0' || !strncmp(ptr
, "us", strlen("us"))) {
983 /* Default to microseconds for backwards
984 * compatibility if no postfix is given.
986 ctx
.gap
.tv_sec
= gap
/ 1000000;
987 ctx
.gap
.tv_nsec
= (gap
% 1000000) * 1000;
988 } else if (!strncmp(ptr
, "ms", strlen("ms"))) {
989 ctx
.gap
.tv_sec
= gap
/ 1000;
990 ctx
.gap
.tv_nsec
= (gap
% 1000) * 1000000;
991 } else if (!strncmp(ptr
, "s", strlen("s"))) {
992 ctx
.gap
.tv_sec
= gap
;
995 panic("Syntax error in time param!\n");
998 /* Fall back to single core to not mess up
999 * correct timing. We are slow anyway!
1006 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1007 if (!isdigit(optarg
[j
- i
]))
1012 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1013 ctx
.reserve_size
= 1 << 10;
1014 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1015 ctx
.reserve_size
= 1 << 20;
1016 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1017 ctx
.reserve_size
= 1 << 30;
1019 panic("Syntax error in ring size param!\n");
1021 ctx
.reserve_size
*= strtoul(optarg
, NULL
, 0);
1038 panic("Option -%c requires an argument!\n",
1041 if (isprint(optopt
))
1042 printf("Unknown option character `0x%X\'!\n", optopt
);
1052 if (ctx
.device
== NULL
)
1053 panic("No networking device given!\n");
1054 if (confname
== NULL
)
1055 panic("No configuration file given!\n");
1056 if (device_mtu(ctx
.device
) == 0)
1057 panic("This is no networking device!\n");
1059 register_signal(SIGINT
, signal_handler
);
1060 register_signal(SIGQUIT
, signal_handler
);
1061 register_signal(SIGTERM
, signal_handler
);
1062 register_signal(SIGHUP
, signal_handler
);
1066 set_sched_status(SCHED_FIFO
, sched_get_priority_max(SCHED_FIFO
));
1070 set_system_socket_memory(vals
, array_size(vals
));
1074 ctx
.device_trans
= xstrdup(ctx
.device
);
1077 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1078 panic_handler_add(on_panic_del_rfmon
, ctx
.device
);
1083 * If number of packets is smaller than number of CPUs use only as
1084 * many CPUs as there are packets. Otherwise we end up sending more
1085 * packets than intended or none at all.
1088 ctx
.cpus
= min_t(unsigned int, ctx
.num
, ctx
.cpus
);
1090 irq
= device_irq_number(ctx
.device
);
1092 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1094 stats
= setup_shared_var(ctx
.cpus
);
1096 for (i
= 0; i
< ctx
.cpus
; i
++) {
1102 seed
= generate_srand_seed();
1106 main_loop(&ctx
, confname
, slow
, i
, invoke_cpp
, orig_num
);
1110 panic("Cannot fork processes!\n");
1114 for (i
= 0; i
< ctx
.cpus
; i
++) {
1118 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1123 leave_rfmon_mac80211(ctx
.device
);
1126 reset_system_socket_memory(vals
, array_size(vals
));
1128 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1129 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1132 tx_packets
+= stats
[i
].tx_packets
;
1133 tx_bytes
+= stats
[i
].tx_bytes
;
1138 printf("\r%12llu packets outgoing\n", tx_packets
);
1139 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1140 for (i
= 0; cpustats
&& i
< ctx
.cpus
; i
++) {
1141 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1142 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1143 stats
[i
].tx_packets
);
1148 destroy_shared_var(stats
, ctx
.cpus
);
1150 device_restore_irq_affinity_list();
1153 free(ctx
.device_trans
);