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>
21 #include <net/ethernet.h>
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <linux/icmp.h>
26 #include <arpa/inet.h>
51 #include "trafgen_conf.h"
58 bool rand
, rfraw
, jumbo_support
, verbose
, smoke_test
, enforce
;
59 unsigned long kpull
, num
, reserve_size
;
62 char *device
, *device_trans
, *rhost
;
64 struct sockaddr_in dest
;
68 unsigned long tv_sec
, tv_usec
;
69 unsigned long long tx_packets
, tx_bytes
;
70 unsigned long long cf_packets
, cf_bytes
;
71 unsigned long long cd_packets
;
75 static sig_atomic_t sigint
= 0;
77 struct packet
*packets
= NULL
;
80 struct packet_dyn
*packet_dyn
= NULL
;
83 static const char *short_options
= "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQ";
84 static const struct option long_options
[] = {
85 {"dev", required_argument
, NULL
, 'd'},
86 {"out", required_argument
, NULL
, 'o'},
87 {"in", required_argument
, NULL
, 'i'},
88 {"conf", required_argument
, NULL
, 'c'},
89 {"num", required_argument
, NULL
, 'n'},
90 {"gap", required_argument
, NULL
, 't'},
91 {"cpus", required_argument
, NULL
, 'P'},
92 {"ring-size", required_argument
, NULL
, 'S'},
93 {"kernel-pull", required_argument
, NULL
, 'k'},
94 {"smoke-test", required_argument
, NULL
, 's'},
95 {"seed", required_argument
, NULL
, 'E'},
96 {"user", required_argument
, NULL
, 'u'},
97 {"group", required_argument
, NULL
, 'g'},
98 {"prio-high", no_argument
, NULL
, 'H'},
99 {"notouch-irq", no_argument
, NULL
, 'Q'},
100 {"jumbo-support", no_argument
, NULL
, 'J'},
101 {"no-cpu-stats", no_argument
, NULL
, 'C'},
102 {"cpp", no_argument
, NULL
, 'p'},
103 {"rfraw", no_argument
, NULL
, 'R'},
104 {"rand", no_argument
, NULL
, 'r'},
105 {"verbose", no_argument
, NULL
, 'V'},
106 {"version", no_argument
, NULL
, 'v'},
107 {"example", no_argument
, NULL
, 'e'},
108 {"help", no_argument
, NULL
, 'h'},
113 static struct itimerval itimer
;
114 static unsigned long interval
= TX_KERNEL_PULL_INT
;
115 static struct cpu_stats
*stats
;
116 static unsigned int seed
;
118 #define CPU_STATS_STATE_CFG 1
119 #define CPU_STATS_STATE_CHK 2
120 #define CPU_STATS_STATE_RES 4
123 # define ICMP_FILTER 1
130 static void signal_handler(int number
)
141 static void timer_elapsed(int unused __maybe_unused
)
143 int ret
= pull_and_flush_tx_ring(sock
);
144 if (unlikely(ret
< 0)) {
145 /* We could hit EBADF if the socket has been closed before
146 * the timer was triggered.
148 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
149 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
152 set_itimer_interval_value(&itimer
, 0, interval
);
153 setitimer(ITIMER_REAL
, &itimer
, NULL
);
156 static void timer_purge(void)
160 ret
= pull_and_flush_tx_ring_wait(sock
);
161 if (unlikely(ret
< 0)) {
162 /* We could hit EBADF if the socket has been closed before
163 * the timer was triggered.
165 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
166 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
169 set_itimer_interval_value(&itimer
, 0, 0);
170 setitimer(ITIMER_REAL
, &itimer
, NULL
);
173 static void __noreturn
help(void)
175 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
176 puts("http://www.netsniff-ng.org\n\n"
177 "Usage: trafgen [options]\n"
179 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
180 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
181 " -p|--cpp Run packet config through C preprocessor\n"
182 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
183 " -R|--rfraw Inject raw 802.11 frames\n"
184 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
185 " -n|--num <uint> Number of packets until exit (def: 0)\n"
186 " -r|--rand Randomize packet selection (def: round robin)\n"
187 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
188 " -t|--gap <time> Set approx. interpacket gap (s/ms/us/ns, def: us)\n"
189 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
190 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
191 " -E|--seed <uint> Manually set srand(3) seed\n"
192 " -u|--user <userid> Drop privileges and change to userid\n"
193 " -g|--group <groupid> Drop privileges and change to groupid\n"
194 " -H|--prio-high Make this high priority process\n"
195 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
196 " -V|--verbose Be more verbose\n"
197 " -C|--no-cpu-stats Do not print CPU time statistics on exit\n"
198 " -v|--version Show version and exit\n"
199 " -e|--example Show built-in packet config example\n"
200 " -h|--help Guess what?!\n\n"
202 " See trafgen.txf for configuration file examples.\n"
203 " trafgen --dev eth0 --conf trafgen.cfg\n"
204 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
205 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
206 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
207 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
208 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
209 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
210 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
211 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
212 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
213 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
215 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
216 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
217 " we assume the kernel crashed, thus we print the packet and quit.\n"
218 " In case you find a ping-of-death, please mention trafgen in your\n"
219 " commit message of the fix!\n\n"
220 " For introducing bit errors, delays with random variation and more,\n"
221 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
222 " For generating different package distributions, you can use scripting\n"
223 " to generate a trafgen config file with packet ratios as:\n\n"
224 " IMIX 64:7, 570:4, 1518:1\n"
225 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
226 " Cisco 64:7, 594:4, 1518:1\n"
227 " RPR Trimodal 64:60, 512:20, 1518:20\n"
228 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n\n"
229 "Please report bugs to <bugs@netsniff-ng.org>\n"
230 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
231 "Swiss federal institute of technology (ETH Zurich)\n"
232 "License: GNU GPL version 2.0\n"
233 "This is free software: you are free to change and redistribute it.\n"
234 "There is NO WARRANTY, to the extent permitted by law.\n");
238 static void __noreturn
example(void)
241 "/* Note: dynamic elements make trafgen slower! */\n"
242 "#include <stddef.h>\n\n"
244 " /* MAC Destination */\n"
245 " fill(0xff, ETH_ALEN),\n"
246 " /* MAC Source */\n"
247 " 0x00, 0x02, 0xb3, drnd(3),\n"
248 " /* IPv4 Protocol */\n"
250 " /* IPv4 Version, IHL, TOS */\n"
252 " /* IPv4 Total Len */\n"
254 " /* IPv4 Ident */\n"
256 " /* IPv4 Flags, Frag Off */\n"
262 " /* IPv4 Checksum (IP header from, to) */\n"
268 " /* TCP Source Port */\n"
270 " /* TCP Dest Port */\n"
272 " /* TCP Sequence Number */\n"
274 " /* TCP Ackn. Number */\n"
276 " /* TCP Header length + TCP SYN/ECN Flag */\n"
277 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
278 " /* Window Size */\n"
280 " /* TCP Checksum (offset IP, offset TCP) */\n"
281 " csumtcp(14, 34),\n"
282 " /* TCP Options */\n"
283 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
284 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
292 static void __noreturn
version(void)
294 printf("\ntrafgen %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
295 puts("multithreaded zero-copy network packet generator\n"
296 "http://www.netsniff-ng.org\n\n"
297 "Please report bugs to <bugs@netsniff-ng.org>\n"
298 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
299 "Swiss federal institute of technology (ETH Zurich)\n"
300 "License: GNU GPL version 2.0\n"
301 "This is free software: you are free to change and redistribute it.\n"
302 "There is NO WARRANTY, to the extent permitted by law.\n");
306 static void apply_counter(int id
)
308 size_t j
, counter_max
= packet_dyn
[id
].clen
;
310 for (j
= 0; j
< counter_max
; ++j
) {
312 struct counter
*counter
= &packet_dyn
[id
].cnt
[j
];
314 val
= counter
->val
- counter
->min
;
316 switch (counter
->type
) {
318 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
321 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
327 counter
->val
= val
+ counter
->min
;
328 packets
[id
].payload
[counter
->off
] = val
;
332 static void apply_randomizer(int id
)
334 size_t j
, rand_max
= packet_dyn
[id
].rlen
;
336 for (j
= 0; j
< rand_max
; ++j
) {
337 uint8_t val
= (uint8_t) rand();
338 struct randomizer
*randomizer
= &packet_dyn
[id
].rnd
[j
];
340 packets
[id
].payload
[randomizer
->off
] = val
;
344 static void apply_csum16(int id
)
346 size_t j
, csum_max
= packet_dyn
[id
].slen
;
348 for (j
= 0; j
< csum_max
; ++j
) {
350 struct csum16
*csum
= &packet_dyn
[id
].csum
[j
];
352 fmemset(&packets
[id
].payload
[csum
->off
], 0, sizeof(sum
));
353 if (unlikely((size_t) csum
->to
>= packets
[id
].len
))
354 csum
->to
= packets
[id
].len
- 1;
356 switch (csum
->which
) {
358 sum
= calc_csum(packets
[id
].payload
+ csum
->from
,
359 csum
->to
- csum
->from
+ 1, 0);
362 sum
= p4_csum((void *) packets
[id
].payload
+ csum
->from
,
363 packets
[id
].payload
+ csum
->to
,
364 (packets
[id
].len
- csum
->to
),
368 sum
= p4_csum((void *) packets
[id
].payload
+ csum
->from
,
369 packets
[id
].payload
+ csum
->to
,
370 (packets
[id
].len
- csum
->to
),
378 fmemcpy(&packets
[id
].payload
[csum
->off
], &sum
, sizeof(sum
));
382 static struct cpu_stats
*setup_shared_var(unsigned long cpus
)
385 size_t len
= cpus
* sizeof(struct cpu_stats
);
386 char zbuff
[len
], file
[256];
387 struct cpu_stats
*buff
;
389 fmemset(zbuff
, 0, len
);
390 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
392 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
396 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
398 write_or_die(fd
, zbuff
, len
);
400 buff
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
402 if (buff
== MAP_FAILED
)
403 panic("Cannot setup shared variable!\n");
408 memset(buff
, 0, len
);
412 static void destroy_shared_var(void *buff
, unsigned long cpus
)
414 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
417 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
422 for (i
= 0; i
< len
; ++i
) {
425 printf("0x%02x, ", payload
[i
]);
431 static int xmit_smoke_setup(struct ctx
*ctx
)
433 int icmp_sock
, ret
, ttl
= 64;
434 struct icmp_filter filter
;
436 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
438 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
440 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
442 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
444 panic("Cannot install filter!\n");
446 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
448 panic("Cannot set TTL!\n");
450 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
451 ctx
->dest
.sin_family
= AF_INET
;
452 ctx
->dest
.sin_port
= 0;
454 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
456 panic("Cannot resolv address!\n");
461 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
464 unsigned int i
, j
= 0, probes
= 100;
465 short ident
, cnt
= 1, idstore
[probes
];
466 uint8_t outpack
[512], *data
;
467 struct icmphdr
*icmp
;
469 size_t len
= sizeof(*icmp
) + 56;
470 struct sockaddr_in from
;
472 struct pollfd fds
= {
477 fmemset(idstore
, 0, sizeof(idstore
));
478 while (probes
-- > 0) {
479 while ((ident
= htons((short) rand())) == 0)
481 idstore
[j
++] = ident
;
483 memset(outpack
, 0, sizeof(outpack
));
484 icmp
= (void *) outpack
;
485 icmp
->type
= ICMP_ECHO
;
486 icmp
->un
.echo
.id
= ident
;
487 icmp
->un
.echo
.sequence
= htons(cnt
++);
489 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
490 for (i
= 0; i
< 56; ++i
)
491 data
[i
] = (uint8_t) rand();
493 icmp
->checksum
= csum((unsigned short *) outpack
,
494 len
/ sizeof(unsigned short));
496 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
497 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
498 if (unlikely(ret
!= (int) len
))
499 panic("Cannot send out probe: %s!\n", strerror(errno
));
501 ret
= poll(&fds
, 1, 50);
503 panic("Poll failed!\n");
505 if (fds
.revents
& POLLIN
) {
506 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
507 (struct sockaddr
*) &from
, &from_len
);
508 if (unlikely(ret
<= 0))
509 panic("Probe receive failed!\n");
510 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
512 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
514 if (unlikely((size_t) ret
< sizeof(*ip
) + sizeof(*icmp
)))
516 ip
= (void *) outpack
;
517 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > (size_t) ret
))
519 icmp
= (void *) outpack
+ ip
->ihl
* 4;
520 for (i
= 0; i
< array_size(idstore
); ++i
) {
521 if (unlikely(icmp
->un
.echo
.id
!= idstore
[i
]))
531 static void xmit_slowpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
533 int ret
, icmp_sock
= -1;
534 unsigned long num
= 1, i
= 0;
535 struct timeval start
, end
, diff
;
536 unsigned long long tx_bytes
= 0, tx_packets
= 0;
537 struct packet_dyn
*pktd
;
538 struct sockaddr_ll saddr
= {
539 .sll_family
= PF_PACKET
,
540 .sll_halen
= ETH_ALEN
,
541 .sll_ifindex
= device_ifindex(ctx
->device
),
546 if (ctx
->num
== 0 && orig_num
> 0)
550 icmp_sock
= xmit_smoke_setup(ctx
);
552 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
554 bug_on(gettimeofday(&start
, NULL
));
556 while (likely(sigint
== 0 && num
> 0 && plen
> 0)) {
557 pktd
= &packet_dyn
[i
];
558 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
564 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
565 (struct sockaddr
*) &saddr
, sizeof(saddr
));
566 if (unlikely(ret
< 0)) {
567 if (errno
== ENOBUFS
) {
572 panic("Sendto error: %s!\n", strerror(errno
));
575 tx_bytes
+= packets
[i
].len
;
578 if (ctx
->smoke_test
) {
579 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
580 if (unlikely(ret
< 0)) {
581 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
582 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
583 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
586 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
601 if ((ctx
->gap
.tv_sec
| ctx
->gap
.tv_nsec
) > 0)
602 nanosleep(&ctx
->gap
, NULL
);
605 bug_on(gettimeofday(&end
, NULL
));
606 timersub(&end
, &start
, &diff
);
611 stats
[cpu
].tx_packets
= tx_packets
;
612 stats
[cpu
].tx_bytes
= tx_bytes
;
613 stats
[cpu
].tv_sec
= diff
.tv_sec
;
614 stats
[cpu
].tv_usec
= diff
.tv_usec
;
616 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
619 static void xmit_fastpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
621 int ifindex
= device_ifindex(ctx
->device
);
624 unsigned long num
= 1, i
= 0, size
;
626 struct frame_map
*hdr
;
627 struct timeval start
, end
, diff
;
628 struct packet_dyn
*pktd
;
629 unsigned long long tx_bytes
= 0, tx_packets
= 0;
631 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
633 size
= ring_size(ctx
->device
, ctx
->reserve_size
);
635 set_sock_prio(sock
, 512);
636 set_packet_loss_discard(sock
);
638 setup_tx_ring_layout(sock
, &tx_ring
, size
, ctx
->jumbo_support
);
639 create_tx_ring(sock
, &tx_ring
, ctx
->verbose
);
640 mmap_tx_ring(sock
, &tx_ring
);
641 alloc_tx_ring_frames(sock
, &tx_ring
);
642 bind_tx_ring(sock
, &tx_ring
, ifindex
);
644 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
647 interval
= ctx
->kpull
;
650 if (ctx
->num
== 0 && orig_num
> 0)
653 set_itimer_interval_value(&itimer
, 0, interval
);
654 setitimer(ITIMER_REAL
, &itimer
, NULL
);
656 bug_on(gettimeofday(&start
, NULL
));
658 while (likely(sigint
== 0 && num
> 0 && plen
> 0)) {
659 if (!user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
664 hdr
= tx_ring
.frames
[it
].iov_base
;
665 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
667 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
668 hdr
->tp_h
.tp_len
= packets
[i
].len
;
670 pktd
= &packet_dyn
[i
];
671 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
677 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
679 tx_bytes
+= packets
[i
].len
;
689 kernel_may_pull_from_tx(&hdr
->tp_h
);
692 if (it
>= tx_ring
.layout
.tp_frame_nr
)
699 bug_on(gettimeofday(&end
, NULL
));
700 timersub(&end
, &start
, &diff
);
704 destroy_tx_ring(sock
, &tx_ring
);
706 stats
[cpu
].tx_packets
= tx_packets
;
707 stats
[cpu
].tx_bytes
= tx_bytes
;
708 stats
[cpu
].tv_sec
= diff
.tv_sec
;
709 stats
[cpu
].tv_usec
= diff
.tv_usec
;
711 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
714 static inline void __set_state(int cpu
, sig_atomic_t s
)
716 stats
[cpu
].state
= s
;
719 static inline sig_atomic_t __get_state(int cpu
)
721 return stats
[cpu
].state
;
724 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, unsigned int cpu
)
729 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
733 while ((__get_state(i
) &
734 (CPU_STATS_STATE_CFG
|
735 CPU_STATS_STATE_RES
)) == 0 &&
739 total
+= stats
[i
].cf_packets
;
745 static void __correct_global_delta(struct ctx
*ctx
, unsigned int cpu
, unsigned long orig
)
750 long long delta_correction
= 0;
752 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
756 while ((__get_state(i
) &
757 (CPU_STATS_STATE_CHK
|
758 CPU_STATS_STATE_RES
)) == 0 &&
762 total
+= stats
[i
].cd_packets
;
766 delta_correction
= -1 * ((long long) total
- orig
);
768 delta_correction
= +1 * ((long long) orig
- total
);
770 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
771 if (stats
[i
].cd_packets
> 0) {
772 if ((long long) stats
[i
].cd_packets
+
773 delta_correction
>= 0) {
780 if ((int) cpu
== cpu_sel
)
781 ctx
->num
+= delta_correction
;
784 static void __set_state_cf(int cpu
, unsigned long p
, unsigned long b
,
787 stats
[cpu
].cf_packets
= p
;
788 stats
[cpu
].cf_bytes
= b
;
789 stats
[cpu
].state
= s
;
792 static void __set_state_cd(int cpu
, unsigned long p
, sig_atomic_t s
)
794 stats
[cpu
].cd_packets
= p
;
795 stats
[cpu
].state
= s
;
798 static int xmit_packet_precheck(struct ctx
*ctx
, int cpu
)
801 unsigned long plen_total
, orig
= ctx
->num
;
802 size_t mtu
, total_len
= 0;
804 bug_on(plen
!= dlen
);
806 for (i
= 0; i
< plen
; ++i
)
807 total_len
+= packets
[i
].len
;
809 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
810 plen_total
= __wait_and_sum_others(ctx
, cpu
);
813 ctx
->num
= (unsigned long) round((1.0 * plen
/ plen_total
) * orig
);
815 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
816 CPU_STATS_STATE_CFG
);
817 __correct_global_delta(ctx
, cpu
, orig
);
821 __set_state(cpu
, CPU_STATS_STATE_RES
);
825 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
826 if (packets
[i
].len
> mtu
+ 14)
827 panic("Device MTU < than packet%d's size!\n", i
);
828 if (packets
[i
].len
<= 14)
829 panic("Packet%d's size too short!\n", i
);
835 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
836 unsigned int cpu
, bool invoke_cpp
, unsigned long orig_num
)
838 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
839 if (xmit_packet_precheck(ctx
, cpu
) < 0)
844 size_t total_len
= 0, total_pkts
= 0;
846 for (i
= 0; i
< ctx
->cpus
; ++i
) {
847 total_len
+= stats
[i
].cf_bytes
;
848 total_pkts
+= stats
[i
].cf_packets
;
851 printf("%6zu packets to schedule\n", total_pkts
);
852 printf("%6zu bytes in total\n", total_len
);
853 printf("Running! Hang up with ^C!\n\n");
860 xmit_slowpath_or_die(ctx
, cpu
, orig_num
);
862 xmit_fastpath_or_die(ctx
, cpu
, orig_num
);
869 static unsigned int generate_srand_seed(void)
874 fd
= open("/dev/urandom", O_RDONLY
);
878 read_or_die(fd
, &_seed
, sizeof(_seed
));
884 int main(int argc
, char **argv
)
886 bool slow
= false, invoke_cpp
= false, reseed
= true, cpustats
= true;
887 bool prio_high
= false, set_irq_aff
= true;
888 int c
, opt_index
, vals
[4] = {0}, irq
;
891 char *confname
= NULL
, *ptr
;
892 unsigned long cpus_tmp
, orig_num
= 0;
893 unsigned long long tx_packets
, tx_bytes
;
896 fmemset(&ctx
, 0, sizeof(ctx
));
897 ctx
.cpus
= get_number_cpus_online();
901 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
902 &opt_index
)) != EOF
) {
923 cpus_tmp
= strtoul(optarg
, NULL
, 0);
924 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
929 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
943 ctx
.smoke_test
= true;
944 ctx
.rhost
= xstrdup(optarg
);
950 ctx
.jumbo_support
= true;
954 confname
= xstrdup(optarg
);
955 if (!strncmp("-", confname
, strlen("-")))
959 ctx
.uid
= strtoul(optarg
, NULL
, 0);
963 ctx
.gid
= strtoul(optarg
, NULL
, 0);
967 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
970 seed
= strtoul(optarg
, NULL
, 0);
974 orig_num
= strtoul(optarg
, NULL
, 0);
980 gap
= strtoul(optarg
, NULL
, 0);
982 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
983 if (!isdigit(optarg
[j
- i
]))
988 if (!strncmp(ptr
, "ns", strlen("ns"))) {
989 ctx
.gap
.tv_sec
= gap
/ 1000000000;
990 ctx
.gap
.tv_nsec
= gap
% 1000000000;
991 } else if (*ptr
== '\0' || !strncmp(ptr
, "us", strlen("us"))) {
992 /* Default to microseconds for backwards
993 * compatibility if no postfix is given.
995 ctx
.gap
.tv_sec
= gap
/ 1000000;
996 ctx
.gap
.tv_nsec
= (gap
% 1000000) * 1000;
997 } else if (!strncmp(ptr
, "ms", strlen("ms"))) {
998 ctx
.gap
.tv_sec
= gap
/ 1000;
999 ctx
.gap
.tv_nsec
= (gap
% 1000) * 1000000;
1000 } else if (!strncmp(ptr
, "s", strlen("s"))) {
1001 ctx
.gap
.tv_sec
= gap
;
1002 ctx
.gap
.tv_nsec
= 0;
1004 panic("Syntax error in time param!\n");
1007 /* Fall back to single core to not mess up
1008 * correct timing. We are slow anyway!
1014 ctx
.reserve_size
= 0;
1016 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1017 if (!isdigit(optarg
[j
- i
]))
1022 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1023 ctx
.reserve_size
= 1 << 10;
1024 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1025 ctx
.reserve_size
= 1 << 20;
1026 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1027 ctx
.reserve_size
= 1 << 30;
1029 panic("Syntax error in ring size param!\n");
1032 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1049 panic("Option -%c requires an argument!\n",
1052 if (isprint(optopt
))
1053 printf("Unknown option character `0x%X\'!\n", optopt
);
1063 if (ctx
.device
== NULL
)
1064 panic("No networking device given!\n");
1065 if (confname
== NULL
)
1066 panic("No configuration file given!\n");
1067 if (device_mtu(ctx
.device
) == 0)
1068 panic("This is no networking device!\n");
1070 register_signal(SIGINT
, signal_handler
);
1071 register_signal(SIGHUP
, signal_handler
);
1072 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1076 set_sched_status(SCHED_FIFO
, sched_get_priority_max(SCHED_FIFO
));
1079 set_system_socket_memory(vals
, array_size(vals
));
1083 ctx
.device_trans
= xstrdup(ctx
.device
);
1086 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
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
);
1125 reset_system_socket_memory(vals
, array_size(vals
));
1127 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1128 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1131 tx_packets
+= stats
[i
].tx_packets
;
1132 tx_bytes
+= stats
[i
].tx_bytes
;
1137 printf("\r%12llu packets outgoing\n", tx_packets
);
1138 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1139 for (i
= 0; cpustats
&& i
< ctx
.cpus
; i
++) {
1140 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1141 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1142 stats
[i
].tx_packets
);
1147 destroy_shared_var(stats
, ctx
.cpus
);
1149 device_restore_irq_affinity_list();
1152 free(ctx
.device_trans
);