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 static void signal_handler(int number
)
154 static void __noreturn
help(void)
156 printf("trafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
157 puts("http://www.netsniff-ng.org\n\n"
158 "Usage: trafgen [options]\n"
160 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
161 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
162 " -p|--cpp Run packet config through C preprocessor\n"
163 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
164 " -R|--rfraw Inject raw 802.11 frames\n"
165 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
166 " -n|--num <uint> Number of packets until exit (def: 0)\n"
167 " -r|--rand Randomize packet selection (def: round robin)\n"
168 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
169 " -t|--gap <time> Set approx. interpacket gap (s/ms/us/ns, def: us)\n"
170 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
171 " -E|--seed <uint> Manually set srand(3) seed\n"
172 " -u|--user <userid> Drop privileges and change to userid\n"
173 " -g|--group <groupid> Drop privileges and change to groupid\n"
174 " -H|--prio-high Make this high priority process\n"
175 " -A|--no-sock-mem Don't tune core socket memory\n"
176 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
177 " -q|--qdisc-path Enabled qdisc kernel path (default off since 3.14)\n"
178 " -V|--verbose Be more verbose\n"
179 " -C|--no-cpu-stats Do not print CPU time statistics on exit\n"
180 " -v|--version Show version and exit\n"
181 " -e|--example Show built-in packet config example\n"
182 " -h|--help Guess what?!\n\n"
184 " trafgen --dev eth0 --conf trafgen.cfg\n"
185 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
186 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
187 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
188 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
189 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
190 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
191 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
192 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
193 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
194 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
195 "Generate config files from existing pcap using netsniff-ng:\n"
196 " netsniff-ng --in dump.pcap --out dump.cfg\n"
198 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
199 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
200 " we assume the kernel crashed, thus we print the packet and quit.\n"
201 " In case you find a ping-of-death, please mention trafgen in your\n"
202 " commit message of the fix!\n\n"
203 " For introducing bit errors, delays with random variation and more,\n"
204 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
205 " For generating different package distributions, you can use scripting\n"
206 " to generate a trafgen config file with packet ratios as:\n\n"
207 " IMIX 64:7, 570:4, 1518:1\n"
208 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
209 " Cisco 64:7, 594:4, 1518:1\n"
210 " RPR Trimodal 64:60, 512:20, 1518:20\n"
211 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n");
216 static void __noreturn
example(void)
219 "/* Note: dynamic elements make trafgen slower! */\n"
220 "#include <stddef.h>\n\n"
222 " /* MAC Destination */\n"
223 " fill(0xff, ETH_ALEN),\n"
224 " /* MAC Source */\n"
225 " 0x00, 0x02, 0xb3, drnd(3),\n"
226 " /* IPv4 Protocol */\n"
228 " /* IPv4 Version, IHL, TOS */\n"
230 " /* IPv4 Total Len */\n"
232 " /* IPv4 Ident */\n"
234 " /* IPv4 Flags, Frag Off */\n"
240 " /* IPv4 Checksum (IP header from, to) */\n"
246 " /* TCP Source Port */\n"
248 " /* TCP Dest Port */\n"
250 " /* TCP Sequence Number */\n"
252 " /* TCP Ackn. Number */\n"
254 " /* TCP Header length + TCP SYN/ECN Flag */\n"
255 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
256 " /* Window Size */\n"
258 " /* TCP Checksum (offset IP, offset TCP) */\n"
259 " csumtcp(14, 34),\n"
260 " /* TCP Options */\n"
261 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
262 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
270 static void __noreturn
version(void)
272 printf("trafgen %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
273 puts("multithreaded zero-copy network packet generator\n"
274 "http://www.netsniff-ng.org\n");
279 static void apply_counter(int id
)
281 size_t j
, counter_max
= packet_dyn
[id
].clen
;
283 for (j
= 0; j
< counter_max
; ++j
) {
285 struct counter
*counter
= &packet_dyn
[id
].cnt
[j
];
287 val
= counter
->val
- counter
->min
;
289 switch (counter
->type
) {
291 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
294 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
300 counter
->val
= val
+ counter
->min
;
301 packets
[id
].payload
[counter
->off
] = val
;
305 static void apply_randomizer(int id
)
307 size_t j
, rand_max
= packet_dyn
[id
].rlen
;
309 for (j
= 0; j
< rand_max
; ++j
) {
310 uint8_t val
= (uint8_t) rand();
311 struct randomizer
*randomizer
= &packet_dyn
[id
].rnd
[j
];
313 packets
[id
].payload
[randomizer
->off
] = val
;
317 static void apply_csum16(int id
)
319 size_t j
, csum_max
= packet_dyn
[id
].slen
;
321 for (j
= 0; j
< csum_max
; ++j
) {
323 struct csum16
*csum
= &packet_dyn
[id
].csum
[j
];
325 fmemset(&packets
[id
].payload
[csum
->off
], 0, sizeof(sum
));
326 if (unlikely((size_t) csum
->to
>= packets
[id
].len
))
327 csum
->to
= packets
[id
].len
- 1;
329 switch (csum
->which
) {
331 sum
= calc_csum(packets
[id
].payload
+ csum
->from
,
332 csum
->to
- csum
->from
+ 1, 0);
335 sum
= p4_csum((void *) packets
[id
].payload
+ csum
->from
,
336 packets
[id
].payload
+ csum
->to
,
337 (packets
[id
].len
- csum
->to
),
341 sum
= p4_csum((void *) packets
[id
].payload
+ csum
->from
,
342 packets
[id
].payload
+ csum
->to
,
343 (packets
[id
].len
- csum
->to
),
351 fmemcpy(&packets
[id
].payload
[csum
->off
], &sum
, sizeof(sum
));
355 static struct cpu_stats
*setup_shared_var(unsigned int cpus
)
358 size_t len
= cpus
* sizeof(struct cpu_stats
);
359 char *zbuff
, file
[256];
360 struct cpu_stats
*buff
;
362 zbuff
= xzmalloc(len
);
363 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
365 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
369 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
371 write_or_die(fd
, zbuff
, len
);
374 buff
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
376 if (buff
== MAP_FAILED
)
377 panic("Cannot setup shared variable!\n");
382 memset(buff
, 0, len
);
386 static void destroy_shared_var(void *buff
, unsigned int cpus
)
388 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
391 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
396 for (i
= 0; i
< len
; ++i
) {
399 printf("0x%02x, ", payload
[i
]);
405 static int xmit_smoke_setup(struct ctx
*ctx
)
407 int icmp_sock
, ret
, ttl
= 64;
408 struct icmp_filter filter
;
410 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
412 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
414 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
416 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
418 panic("Cannot install filter!\n");
420 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
422 panic("Cannot set TTL!\n");
424 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
425 ctx
->dest
.sin_family
= AF_INET
;
426 ctx
->dest
.sin_port
= 0;
428 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
430 panic("Cannot resolv address!\n");
435 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
439 short ident
, cnt
= 1, idstore
[SMOKE_N_PROBES
];
440 uint8_t outpack
[512], *data
;
441 struct icmphdr
*icmp
;
443 size_t len
= sizeof(*icmp
) + 56;
444 struct sockaddr_in from
;
446 struct pollfd fds
= {
451 fmemset(idstore
, 0, sizeof(idstore
));
452 for (j
= 0; j
< SMOKE_N_PROBES
; j
++) {
453 while ((ident
= htons((short) rand())) == 0)
457 memset(outpack
, 0, sizeof(outpack
));
458 icmp
= (void *) outpack
;
459 icmp
->type
= ICMP_ECHO
;
460 icmp
->un
.echo
.id
= ident
;
461 icmp
->un
.echo
.sequence
= htons(cnt
++);
463 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
464 for (i
= 0; i
< 56; ++i
)
465 data
[i
] = (uint8_t) rand();
467 icmp
->checksum
= csum((unsigned short *) outpack
,
468 len
/ sizeof(unsigned short));
470 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
471 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
472 if (unlikely(ret
!= (int) len
))
473 panic("Cannot send out probe: %s!\n", strerror(errno
));
475 ret
= poll(&fds
, 1, 50);
477 panic("Poll failed!\n");
479 if (fds
.revents
& POLLIN
) {
480 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
481 (struct sockaddr
*) &from
, &from_len
);
482 if (unlikely(ret
<= 0))
483 panic("Probe receive failed!\n");
484 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
486 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
488 if (unlikely((size_t) ret
< sizeof(*ip
) + sizeof(*icmp
)))
490 ip
= (void *) outpack
;
491 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > (size_t) ret
))
493 icmp
= (void *) outpack
+ ip
->ihl
* 4;
494 for (i
= 0; i
< array_size(idstore
); ++i
) {
495 if (unlikely(icmp
->un
.echo
.id
!= idstore
[i
]))
505 static void xmit_slowpath_or_die(struct ctx
*ctx
, unsigned int cpu
, unsigned long orig_num
)
507 int ret
, icmp_sock
= -1;
508 unsigned long num
= 1, i
= 0;
509 struct timeval start
, end
, diff
;
510 unsigned long long tx_bytes
= 0, tx_packets
= 0;
511 struct packet_dyn
*pktd
;
512 struct sockaddr_ll saddr
= {
513 .sll_family
= PF_PACKET
,
514 .sll_halen
= ETH_ALEN
,
515 .sll_ifindex
= device_ifindex(ctx
->device
),
520 if (ctx
->num
== 0 && orig_num
> 0)
524 icmp_sock
= xmit_smoke_setup(ctx
);
526 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
528 bug_on(gettimeofday(&start
, NULL
));
530 while (likely(sigint
== 0 && num
> 0 && plen
> 0)) {
531 pktd
= &packet_dyn
[i
];
532 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
538 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
539 (struct sockaddr
*) &saddr
, sizeof(saddr
));
540 if (unlikely(ret
< 0)) {
541 if (errno
== ENOBUFS
) {
546 panic("Sendto error: %s!\n", strerror(errno
));
549 tx_bytes
+= packets
[i
].len
;
552 if (ctx
->smoke_test
) {
553 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
554 if (unlikely(ret
< 0)) {
555 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
556 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
557 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
560 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
575 if ((ctx
->gap
.tv_sec
| ctx
->gap
.tv_nsec
) > 0)
576 nanosleep(&ctx
->gap
, NULL
);
579 bug_on(gettimeofday(&end
, NULL
));
580 timersub(&end
, &start
, &diff
);
585 stats
[cpu
].tx_packets
= tx_packets
;
586 stats
[cpu
].tx_bytes
= tx_bytes
;
587 stats
[cpu
].tv_sec
= diff
.tv_sec
;
588 stats
[cpu
].tv_usec
= diff
.tv_usec
;
590 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
593 static void xmit_fastpath_or_die(struct ctx
*ctx
, unsigned int cpu
, unsigned long orig_num
)
595 int ifindex
= device_ifindex(ctx
->device
);
598 unsigned long num
= 1, i
= 0;
599 size_t size
= ring_size(ctx
->device
, ctx
->reserve_size
);
601 struct frame_map
*hdr
;
602 struct timeval start
, end
, diff
;
603 struct packet_dyn
*pktd
;
604 unsigned long long tx_bytes
= 0, tx_packets
= 0;
606 set_sock_prio(sock
, 512);
608 ring_tx_setup(&tx_ring
, sock
, size
, ifindex
, ctx
->jumbo_support
, ctx
->verbose
);
610 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
614 if (ctx
->num
== 0 && orig_num
> 0)
617 bug_on(gettimeofday(&start
, NULL
));
619 while (likely(sigint
== 0 && num
> 0 && plen
> 0)) {
620 if (!user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
621 int ret
= pull_and_flush_tx_ring(sock
);
622 if (unlikely(ret
< 0)) {
623 /* We could hit EBADF if the socket has been closed before
624 * the timer was triggered.
626 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
627 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
633 hdr
= tx_ring
.frames
[it
].iov_base
;
634 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
636 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
637 hdr
->tp_h
.tp_len
= packets
[i
].len
;
639 pktd
= &packet_dyn
[i
];
640 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
646 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
648 tx_bytes
+= packets
[i
].len
;
658 kernel_may_pull_from_tx(&hdr
->tp_h
);
661 if (it
>= tx_ring
.layout
.tp_frame_nr
)
668 bug_on(gettimeofday(&end
, NULL
));
669 timersub(&end
, &start
, &diff
);
671 pull_and_flush_tx_ring_wait(sock
);
672 destroy_tx_ring(sock
, &tx_ring
);
674 stats
[cpu
].tx_packets
= tx_packets
;
675 stats
[cpu
].tx_bytes
= tx_bytes
;
676 stats
[cpu
].tv_sec
= diff
.tv_sec
;
677 stats
[cpu
].tv_usec
= diff
.tv_usec
;
679 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
682 static inline void __set_state(unsigned int cpu
, sig_atomic_t s
)
684 stats
[cpu
].state
= s
;
687 static inline sig_atomic_t __get_state(unsigned int cpu
)
689 return stats
[cpu
].state
;
692 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, unsigned int cpu
)
697 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
701 while ((__get_state(i
) &
702 (CPU_STATS_STATE_CFG
|
703 CPU_STATS_STATE_RES
)) == 0 &&
707 total
+= stats
[i
].cf_packets
;
713 static void __correct_global_delta(struct ctx
*ctx
, unsigned int cpu
, unsigned long orig
)
718 long long delta_correction
= 0;
720 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
724 while ((__get_state(i
) &
725 (CPU_STATS_STATE_CHK
|
726 CPU_STATS_STATE_RES
)) == 0 &&
730 total
+= stats
[i
].cd_packets
;
734 delta_correction
= -1 * ((long long) total
- orig
);
736 delta_correction
= +1 * ((long long) orig
- total
);
738 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
739 if (stats
[i
].cd_packets
> 0) {
740 if ((long long) stats
[i
].cd_packets
+
741 delta_correction
>= 0) {
748 if ((int) cpu
== cpu_sel
)
749 ctx
->num
+= delta_correction
;
752 static void __set_state_cf(unsigned int cpu
, unsigned long p
, unsigned long b
,
755 stats
[cpu
].cf_packets
= p
;
756 stats
[cpu
].cf_bytes
= b
;
757 stats
[cpu
].state
= s
;
760 static void __set_state_cd(unsigned int cpu
, unsigned long p
, sig_atomic_t s
)
762 stats
[cpu
].cd_packets
= p
;
763 stats
[cpu
].state
= s
;
766 static int xmit_packet_precheck(struct ctx
*ctx
, unsigned int cpu
)
769 unsigned long plen_total
, orig
= ctx
->num
;
770 size_t mtu
, total_len
= 0;
772 bug_on(plen
!= dlen
);
774 for (i
= 0; i
< plen
; ++i
)
775 total_len
+= packets
[i
].len
;
777 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
778 plen_total
= __wait_and_sum_others(ctx
, cpu
);
781 ctx
->num
= (unsigned long) round((1.0 * plen
/ plen_total
) * orig
);
783 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
784 CPU_STATS_STATE_CFG
);
785 __correct_global_delta(ctx
, cpu
, orig
);
789 __set_state(cpu
, CPU_STATS_STATE_RES
);
793 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
794 if (packets
[i
].len
> mtu
+ 14)
795 panic("Device MTU < than packet%d's size!\n", i
);
796 if (packets
[i
].len
<= 14)
797 panic("Packet%d's size too short!\n", i
);
803 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
804 unsigned int cpu
, bool invoke_cpp
, unsigned long orig_num
)
806 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
807 if (xmit_packet_precheck(ctx
, cpu
) < 0)
812 size_t total_len
= 0, total_pkts
= 0;
814 for (i
= 0; i
< ctx
->cpus
; ++i
) {
815 total_len
+= stats
[i
].cf_bytes
;
816 total_pkts
+= stats
[i
].cf_packets
;
819 printf("%6zu packets to schedule\n", total_pkts
);
820 printf("%6zu bytes in total\n", total_len
);
821 printf("Running! Hang up with ^C!\n\n");
827 if (ctx
->qdisc_path
== false)
828 set_sock_qdisc_bypass(sock
, ctx
->verbose
);
831 xmit_slowpath_or_die(ctx
, cpu
, orig_num
);
833 xmit_fastpath_or_die(ctx
, cpu
, orig_num
);
840 static unsigned int generate_srand_seed(void)
845 fd
= open("/dev/urandom", O_RDONLY
);
849 read_or_die(fd
, &_seed
, sizeof(_seed
));
855 static void on_panic_del_rfmon(void *arg
)
857 leave_rfmon_mac80211(arg
);
860 int main(int argc
, char **argv
)
862 bool slow
= false, invoke_cpp
= false, reseed
= true, cpustats
= true;
863 bool prio_high
= false, set_irq_aff
= true, set_sock_mem
= true;
864 int c
, opt_index
, vals
[4] = {0}, irq
;
867 char *confname
= NULL
, *ptr
;
868 unsigned long cpus_tmp
, orig_num
= 0;
869 unsigned long long tx_packets
, tx_bytes
;
872 fmemset(&ctx
, 0, sizeof(ctx
));
873 ctx
.cpus
= get_number_cpus_online();
876 ctx
.qdisc_path
= false;
878 /* Keep an initial small default size to reduce cache-misses. */
879 ctx
.reserve_size
= 512 * (1 << 10);
881 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
882 &opt_index
)) != EOF
) {
903 cpus_tmp
= strtoul(optarg
, NULL
, 0);
904 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
909 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
915 set_sock_mem
= false;
921 ctx
.qdisc_path
= true;
929 ctx
.smoke_test
= true;
930 ctx
.rhost
= xstrdup(optarg
);
936 ctx
.jumbo_support
= true;
940 confname
= xstrdup(optarg
);
941 if (!strncmp("-", confname
, strlen("-")))
945 ctx
.uid
= strtoul(optarg
, NULL
, 0);
949 ctx
.gid
= strtoul(optarg
, NULL
, 0);
953 printf("Option -k/--kernel-pull is no longer used and "
954 "will be removed in a future release!\n");
957 seed
= strtoul(optarg
, NULL
, 0);
961 orig_num
= strtoul(optarg
, NULL
, 0);
967 prctl(PR_SET_TIMERSLACK
, 1UL);
968 gap
= strtoul(optarg
, NULL
, 0);
970 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
971 if (!isdigit(optarg
[j
- i
]))
976 if (!strncmp(ptr
, "ns", strlen("ns"))) {
977 ctx
.gap
.tv_sec
= gap
/ 1000000000;
978 ctx
.gap
.tv_nsec
= gap
% 1000000000;
979 } else if (*ptr
== '\0' || !strncmp(ptr
, "us", strlen("us"))) {
980 /* Default to microseconds for backwards
981 * compatibility if no postfix is given.
983 ctx
.gap
.tv_sec
= gap
/ 1000000;
984 ctx
.gap
.tv_nsec
= (gap
% 1000000) * 1000;
985 } else if (!strncmp(ptr
, "ms", strlen("ms"))) {
986 ctx
.gap
.tv_sec
= gap
/ 1000;
987 ctx
.gap
.tv_nsec
= (gap
% 1000) * 1000000;
988 } else if (!strncmp(ptr
, "s", strlen("s"))) {
989 ctx
.gap
.tv_sec
= gap
;
992 panic("Syntax error in time param!\n");
995 /* Fall back to single core to not mess up
996 * correct timing. We are slow anyway!
1003 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1004 if (!isdigit(optarg
[j
- i
]))
1009 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1010 ctx
.reserve_size
= 1 << 10;
1011 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1012 ctx
.reserve_size
= 1 << 20;
1013 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1014 ctx
.reserve_size
= 1 << 30;
1016 panic("Syntax error in ring size param!\n");
1018 ctx
.reserve_size
*= strtoul(optarg
, NULL
, 0);
1035 panic("Option -%c requires an argument!\n",
1038 if (isprint(optopt
))
1039 printf("Unknown option character `0x%X\'!\n", optopt
);
1049 if (ctx
.device
== NULL
)
1050 panic("No networking device given!\n");
1051 if (confname
== NULL
)
1052 panic("No configuration file given!\n");
1053 if (device_mtu(ctx
.device
) == 0)
1054 panic("This is no networking device!\n");
1056 register_signal(SIGINT
, signal_handler
);
1057 register_signal(SIGQUIT
, signal_handler
);
1058 register_signal(SIGTERM
, signal_handler
);
1059 register_signal(SIGHUP
, signal_handler
);
1063 set_sched_status(SCHED_FIFO
, sched_get_priority_max(SCHED_FIFO
));
1067 set_system_socket_memory(vals
, array_size(vals
));
1071 ctx
.device_trans
= xstrdup(ctx
.device
);
1074 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1075 panic_handler_add(on_panic_del_rfmon
, ctx
.device
);
1080 * If number of packets is smaller than number of CPUs use only as
1081 * many CPUs as there are packets. Otherwise we end up sending more
1082 * packets than intended or none at all.
1085 ctx
.cpus
= min_t(unsigned int, ctx
.num
, ctx
.cpus
);
1087 irq
= device_irq_number(ctx
.device
);
1089 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1091 stats
= setup_shared_var(ctx
.cpus
);
1093 for (i
= 0; i
< ctx
.cpus
; i
++) {
1099 seed
= generate_srand_seed();
1103 main_loop(&ctx
, confname
, slow
, i
, invoke_cpp
, orig_num
);
1107 panic("Cannot fork processes!\n");
1111 for (i
= 0; i
< ctx
.cpus
; i
++) {
1115 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1120 leave_rfmon_mac80211(ctx
.device
);
1123 reset_system_socket_memory(vals
, array_size(vals
));
1125 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1126 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1129 tx_packets
+= stats
[i
].tx_packets
;
1130 tx_bytes
+= stats
[i
].tx_bytes
;
1135 printf("\r%12llu packets outgoing\n", tx_packets
);
1136 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1137 for (i
= 0; cpustats
&& i
< ctx
.cpus
; i
++) {
1138 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1139 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1140 stats
[i
].tx_packets
);
1145 destroy_shared_var(stats
, ctx
.cpus
);
1147 device_restore_irq_affinity_list();
1150 free(ctx
.device_trans
);