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:C";
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 {"jumbo-support", no_argument
, NULL
, 'J'},
99 {"no-cpu-stats", no_argument
, NULL
, 'C'},
100 {"cpp", no_argument
, NULL
, 'p'},
101 {"rfraw", no_argument
, NULL
, 'R'},
102 {"rand", no_argument
, NULL
, 'r'},
103 {"verbose", no_argument
, NULL
, 'V'},
104 {"version", no_argument
, NULL
, 'v'},
105 {"example", no_argument
, NULL
, 'e'},
106 {"help", no_argument
, NULL
, 'h'},
111 static struct itimerval itimer
;
112 static unsigned long interval
= TX_KERNEL_PULL_INT
;
113 static struct cpu_stats
*stats
;
114 static unsigned int seed
;
116 #define CPU_STATS_STATE_CFG 1
117 #define CPU_STATS_STATE_CHK 2
118 #define CPU_STATS_STATE_RES 4
121 # define ICMP_FILTER 1
128 static void signal_handler(int number
)
139 static void timer_elapsed(int unused __maybe_unused
)
141 int ret
= pull_and_flush_tx_ring(sock
);
142 if (unlikely(ret
< 0)) {
143 /* We could hit EBADF if the socket has been closed before
144 * the timer was triggered.
146 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
147 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
150 set_itimer_interval_value(&itimer
, 0, interval
);
151 setitimer(ITIMER_REAL
, &itimer
, NULL
);
154 static void timer_purge(void)
158 ret
= pull_and_flush_tx_ring_wait(sock
);
159 if (unlikely(ret
< 0)) {
160 /* We could hit EBADF if the socket has been closed before
161 * the timer was triggered.
163 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
164 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
167 set_itimer_interval_value(&itimer
, 0, 0);
168 setitimer(ITIMER_REAL
, &itimer
, NULL
);
171 static void __noreturn
help(void)
173 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
174 puts("http://www.netsniff-ng.org\n\n"
175 "Usage: trafgen [options]\n"
177 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
178 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
179 " -p|--cpp Run packet config through C preprocessor\n"
180 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
181 " -R|--rfraw Inject raw 802.11 frames\n"
182 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
183 " -n|--num <uint> Number of packets until exit (def: 0)\n"
184 " -r|--rand Randomize packet selection (def: round robin)\n"
185 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
186 " -t|--gap <time> Set approx. interpacket gap (s/ms/us/ns, def: us)\n"
187 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
188 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
189 " -E|--seed <uint> Manually set srand(3) seed\n"
190 " -u|--user <userid> Drop privileges and change to userid\n"
191 " -g|--group <groupid> Drop privileges and change to groupid\n"
192 " -V|--verbose Be more verbose\n"
193 " -C|--no-cpu-stats Do not print CPU time statistics on exit\n"
194 " -v|--version Show version and exit\n"
195 " -e|--example Show built-in packet config example\n"
196 " -h|--help Guess what?!\n\n"
198 " See trafgen.txf for configuration file examples.\n"
199 " trafgen --dev eth0 --conf trafgen.cfg\n"
200 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
201 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
202 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
203 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
204 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
205 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
206 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
207 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
208 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
209 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
211 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
212 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
213 " we assume the kernel crashed, thus we print the packet and quit.\n"
214 " In case you find a ping-of-death, please mention trafgen in your\n"
215 " commit message of the fix!\n\n"
216 " For introducing bit errors, delays with random variation and more,\n"
217 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
218 " For generating different package distributions, you can use scripting\n"
219 " to generate a trafgen config file with packet ratios as:\n\n"
220 " IMIX 64:7, 570:4, 1518:1\n"
221 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
222 " Cisco 64:7, 594:4, 1518:1\n"
223 " RPR Trimodal 64:60, 512:20, 1518:20\n"
224 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n\n"
225 "Please report bugs to <bugs@netsniff-ng.org>\n"
226 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
227 "Swiss federal institute of technology (ETH Zurich)\n"
228 "License: GNU GPL version 2.0\n"
229 "This is free software: you are free to change and redistribute it.\n"
230 "There is NO WARRANTY, to the extent permitted by law.\n");
234 static void __noreturn
example(void)
237 "/* Note: dynamic elements make trafgen slower! */\n"
238 "#include <stddef.h>\n\n"
240 " /* MAC Destination */\n"
241 " fill(0xff, ETH_ALEN),\n"
242 " /* MAC Source */\n"
243 " 0x00, 0x02, 0xb3, drnd(3),\n"
244 " /* IPv4 Protocol */\n"
246 " /* IPv4 Version, IHL, TOS */\n"
248 " /* IPv4 Total Len */\n"
250 " /* IPv4 Ident */\n"
252 " /* IPv4 Flags, Frag Off */\n"
258 " /* IPv4 Checksum (IP header from, to) */\n"
264 " /* TCP Source Port */\n"
266 " /* TCP Dest Port */\n"
268 " /* TCP Sequence Number */\n"
270 " /* TCP Ackn. Number */\n"
272 " /* TCP Header length + TCP SYN/ECN Flag */\n"
273 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
274 " /* Window Size */\n"
276 " /* TCP Checksum (offset IP, offset TCP) */\n"
277 " csumtcp(14, 34),\n"
278 " /* TCP Options */\n"
279 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
280 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
288 static void __noreturn
version(void)
290 printf("\ntrafgen %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
291 puts("multithreaded zero-copy network packet generator\n"
292 "http://www.netsniff-ng.org\n\n"
293 "Please report bugs to <bugs@netsniff-ng.org>\n"
294 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
295 "Swiss federal institute of technology (ETH Zurich)\n"
296 "License: GNU GPL version 2.0\n"
297 "This is free software: you are free to change and redistribute it.\n"
298 "There is NO WARRANTY, to the extent permitted by law.\n");
302 static void apply_counter(int id
)
304 size_t j
, counter_max
= packet_dyn
[id
].clen
;
306 for (j
= 0; j
< counter_max
; ++j
) {
308 struct counter
*counter
= &packet_dyn
[id
].cnt
[j
];
310 val
= counter
->val
- counter
->min
;
312 switch (counter
->type
) {
314 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
317 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
323 counter
->val
= val
+ counter
->min
;
324 packets
[id
].payload
[counter
->off
] = val
;
328 static void apply_randomizer(int id
)
330 size_t j
, rand_max
= packet_dyn
[id
].rlen
;
332 for (j
= 0; j
< rand_max
; ++j
) {
333 uint8_t val
= (uint8_t) rand();
334 struct randomizer
*randomizer
= &packet_dyn
[id
].rnd
[j
];
336 packets
[id
].payload
[randomizer
->off
] = val
;
340 static void apply_csum16(int id
)
342 size_t j
, csum_max
= packet_dyn
[id
].slen
;
344 for (j
= 0; j
< csum_max
; ++j
) {
346 struct csum16
*csum
= &packet_dyn
[id
].csum
[j
];
348 fmemset(&packets
[id
].payload
[csum
->off
], 0, sizeof(sum
));
349 if (unlikely((size_t) csum
->to
>= packets
[id
].len
))
350 csum
->to
= packets
[id
].len
- 1;
352 switch (csum
->which
) {
354 sum
= calc_csum(packets
[id
].payload
+ csum
->from
,
355 csum
->to
- csum
->from
+ 1, 0);
358 sum
= p4_csum((void *) packets
[id
].payload
+ csum
->from
,
359 packets
[id
].payload
+ csum
->to
,
360 (packets
[id
].len
- csum
->to
),
364 sum
= p4_csum((void *) packets
[id
].payload
+ csum
->from
,
365 packets
[id
].payload
+ csum
->to
,
366 (packets
[id
].len
- csum
->to
),
374 fmemcpy(&packets
[id
].payload
[csum
->off
], &sum
, sizeof(sum
));
378 static struct cpu_stats
*setup_shared_var(unsigned long cpus
)
381 size_t len
= cpus
* sizeof(struct cpu_stats
);
382 char zbuff
[len
], file
[256];
383 struct cpu_stats
*buff
;
385 fmemset(zbuff
, 0, len
);
386 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
388 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
392 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
394 write_or_die(fd
, zbuff
, len
);
396 buff
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
398 if (buff
== MAP_FAILED
)
399 panic("Cannot setup shared variable!\n");
404 memset(buff
, 0, len
);
408 static void destroy_shared_var(void *buff
, unsigned long cpus
)
410 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
413 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
418 for (i
= 0; i
< len
; ++i
) {
421 printf("0x%02x, ", payload
[i
]);
427 static int xmit_smoke_setup(struct ctx
*ctx
)
429 int icmp_sock
, ret
, ttl
= 64;
430 struct icmp_filter filter
;
432 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
434 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
436 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
438 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
440 panic("Cannot install filter!\n");
442 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
444 panic("Cannot set TTL!\n");
446 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
447 ctx
->dest
.sin_family
= AF_INET
;
448 ctx
->dest
.sin_port
= 0;
450 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
452 panic("Cannot resolv address!\n");
457 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
460 unsigned int i
, j
= 0, probes
= 100;
461 short ident
, cnt
= 1, idstore
[probes
];
462 uint8_t outpack
[512], *data
;
463 struct icmphdr
*icmp
;
465 size_t len
= sizeof(*icmp
) + 56;
466 struct sockaddr_in from
;
468 struct pollfd fds
= {
473 fmemset(idstore
, 0, sizeof(idstore
));
474 while (probes
-- > 0) {
475 while ((ident
= htons((short) rand())) == 0)
477 idstore
[j
++] = ident
;
479 memset(outpack
, 0, sizeof(outpack
));
480 icmp
= (void *) outpack
;
481 icmp
->type
= ICMP_ECHO
;
482 icmp
->un
.echo
.id
= ident
;
483 icmp
->un
.echo
.sequence
= htons(cnt
++);
485 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
486 for (i
= 0; i
< 56; ++i
)
487 data
[i
] = (uint8_t) rand();
489 icmp
->checksum
= csum((unsigned short *) outpack
,
490 len
/ sizeof(unsigned short));
492 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
493 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
494 if (unlikely(ret
!= (int) len
))
495 panic("Cannot send out probe: %s!\n", strerror(errno
));
497 ret
= poll(&fds
, 1, 50);
499 panic("Poll failed!\n");
501 if (fds
.revents
& POLLIN
) {
502 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
503 (struct sockaddr
*) &from
, &from_len
);
504 if (unlikely(ret
<= 0))
505 panic("Probe receive failed!\n");
506 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
508 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
510 if (unlikely((size_t) ret
< sizeof(*ip
) + sizeof(*icmp
)))
512 ip
= (void *) outpack
;
513 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > (size_t) ret
))
515 icmp
= (void *) outpack
+ ip
->ihl
* 4;
516 for (i
= 0; i
< array_size(idstore
); ++i
) {
517 if (unlikely(icmp
->un
.echo
.id
!= idstore
[i
]))
527 static void xmit_slowpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
529 int ret
, icmp_sock
= -1;
530 unsigned long num
= 1, i
= 0;
531 struct timeval start
, end
, diff
;
532 unsigned long long tx_bytes
= 0, tx_packets
= 0;
533 struct packet_dyn
*pktd
;
534 struct sockaddr_ll saddr
= {
535 .sll_family
= PF_PACKET
,
536 .sll_halen
= ETH_ALEN
,
537 .sll_ifindex
= device_ifindex(ctx
->device
),
542 if (ctx
->num
== 0 && orig_num
> 0)
546 icmp_sock
= xmit_smoke_setup(ctx
);
548 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
550 bug_on(gettimeofday(&start
, NULL
));
552 while (likely(sigint
== 0 && num
> 0 && plen
> 0)) {
553 pktd
= &packet_dyn
[i
];
554 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
560 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
561 (struct sockaddr
*) &saddr
, sizeof(saddr
));
562 if (unlikely(ret
< 0)) {
563 if (errno
== ENOBUFS
) {
568 panic("Sendto error: %s!\n", strerror(errno
));
571 tx_bytes
+= packets
[i
].len
;
574 if (ctx
->smoke_test
) {
575 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
576 if (unlikely(ret
< 0)) {
577 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
578 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
579 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
582 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
597 if ((ctx
->gap
.tv_sec
| ctx
->gap
.tv_nsec
) > 0)
598 nanosleep(&ctx
->gap
, NULL
);
601 bug_on(gettimeofday(&end
, NULL
));
602 timersub(&end
, &start
, &diff
);
607 stats
[cpu
].tx_packets
= tx_packets
;
608 stats
[cpu
].tx_bytes
= tx_bytes
;
609 stats
[cpu
].tv_sec
= diff
.tv_sec
;
610 stats
[cpu
].tv_usec
= diff
.tv_usec
;
612 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
615 static void xmit_fastpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
617 int ifindex
= device_ifindex(ctx
->device
);
620 unsigned long num
= 1, i
= 0, size
;
622 struct frame_map
*hdr
;
623 struct timeval start
, end
, diff
;
624 struct packet_dyn
*pktd
;
625 unsigned long long tx_bytes
= 0, tx_packets
= 0;
627 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
629 size
= ring_size(ctx
->device
, ctx
->reserve_size
);
631 set_sock_prio(sock
, 512);
632 set_packet_loss_discard(sock
);
634 setup_tx_ring_layout(sock
, &tx_ring
, size
, ctx
->jumbo_support
);
635 create_tx_ring(sock
, &tx_ring
, ctx
->verbose
);
636 mmap_tx_ring(sock
, &tx_ring
);
637 alloc_tx_ring_frames(sock
, &tx_ring
);
638 bind_tx_ring(sock
, &tx_ring
, ifindex
);
640 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
643 interval
= ctx
->kpull
;
646 if (ctx
->num
== 0 && orig_num
> 0)
649 set_itimer_interval_value(&itimer
, 0, interval
);
650 setitimer(ITIMER_REAL
, &itimer
, NULL
);
652 bug_on(gettimeofday(&start
, NULL
));
654 while (likely(sigint
== 0 && num
> 0 && plen
> 0)) {
655 if (!user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
)) {
660 hdr
= tx_ring
.frames
[it
].iov_base
;
661 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
663 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
664 hdr
->tp_h
.tp_len
= packets
[i
].len
;
666 pktd
= &packet_dyn
[i
];
667 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
673 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
675 tx_bytes
+= packets
[i
].len
;
685 kernel_may_pull_from_tx(&hdr
->tp_h
);
688 if (it
>= tx_ring
.layout
.tp_frame_nr
)
695 bug_on(gettimeofday(&end
, NULL
));
696 timersub(&end
, &start
, &diff
);
700 destroy_tx_ring(sock
, &tx_ring
);
702 stats
[cpu
].tx_packets
= tx_packets
;
703 stats
[cpu
].tx_bytes
= tx_bytes
;
704 stats
[cpu
].tv_sec
= diff
.tv_sec
;
705 stats
[cpu
].tv_usec
= diff
.tv_usec
;
707 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
710 static inline void __set_state(int cpu
, sig_atomic_t s
)
712 stats
[cpu
].state
= s
;
715 static inline sig_atomic_t __get_state(int cpu
)
717 return stats
[cpu
].state
;
720 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, unsigned int cpu
)
725 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
729 while ((__get_state(i
) &
730 (CPU_STATS_STATE_CFG
|
731 CPU_STATS_STATE_RES
)) == 0 &&
735 total
+= stats
[i
].cf_packets
;
741 static void __correct_global_delta(struct ctx
*ctx
, unsigned int cpu
, unsigned long orig
)
746 long long delta_correction
= 0;
748 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
752 while ((__get_state(i
) &
753 (CPU_STATS_STATE_CHK
|
754 CPU_STATS_STATE_RES
)) == 0 &&
758 total
+= stats
[i
].cd_packets
;
762 delta_correction
= -1 * ((long long) total
- orig
);
764 delta_correction
= +1 * ((long long) orig
- total
);
766 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
767 if (stats
[i
].cd_packets
> 0) {
768 if ((long long) stats
[i
].cd_packets
+
769 delta_correction
>= 0) {
776 if ((int) cpu
== cpu_sel
)
777 ctx
->num
+= delta_correction
;
780 static void __set_state_cf(int cpu
, unsigned long p
, unsigned long b
,
783 stats
[cpu
].cf_packets
= p
;
784 stats
[cpu
].cf_bytes
= b
;
785 stats
[cpu
].state
= s
;
788 static void __set_state_cd(int cpu
, unsigned long p
, sig_atomic_t s
)
790 stats
[cpu
].cd_packets
= p
;
791 stats
[cpu
].state
= s
;
794 static int xmit_packet_precheck(struct ctx
*ctx
, int cpu
)
797 unsigned long plen_total
, orig
= ctx
->num
;
798 size_t mtu
, total_len
= 0;
800 bug_on(plen
!= dlen
);
802 for (i
= 0; i
< plen
; ++i
)
803 total_len
+= packets
[i
].len
;
805 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
806 plen_total
= __wait_and_sum_others(ctx
, cpu
);
809 ctx
->num
= (unsigned long) round((1.0 * plen
/ plen_total
) * orig
);
811 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
812 CPU_STATS_STATE_CFG
);
813 __correct_global_delta(ctx
, cpu
, orig
);
817 __set_state(cpu
, CPU_STATS_STATE_RES
);
821 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
822 if (packets
[i
].len
> mtu
+ 14)
823 panic("Device MTU < than packet%d's size!\n", i
);
824 if (packets
[i
].len
<= 14)
825 panic("Packet%d's size too short!\n", i
);
831 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
832 unsigned int cpu
, bool invoke_cpp
, unsigned long orig_num
)
834 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
835 if (xmit_packet_precheck(ctx
, cpu
) < 0)
840 size_t total_len
= 0, total_pkts
= 0;
842 for (i
= 0; i
< ctx
->cpus
; ++i
) {
843 total_len
+= stats
[i
].cf_bytes
;
844 total_pkts
+= stats
[i
].cf_packets
;
847 printf("%6zu packets to schedule\n", total_pkts
);
848 printf("%6zu bytes in total\n", total_len
);
849 printf("Running! Hang up with ^C!\n\n");
856 xmit_slowpath_or_die(ctx
, cpu
, orig_num
);
858 xmit_fastpath_or_die(ctx
, cpu
, orig_num
);
865 static unsigned int generate_srand_seed(void)
870 fd
= open("/dev/urandom", O_RDONLY
);
874 read_or_die(fd
, &_seed
, sizeof(_seed
));
880 int main(int argc
, char **argv
)
882 bool slow
= false, invoke_cpp
= false, reseed
= true, cpustats
= true;
883 int c
, opt_index
, vals
[4] = {0}, irq
;
886 char *confname
= NULL
, *ptr
;
887 unsigned long cpus_tmp
, orig_num
= 0;
888 unsigned long long tx_packets
, tx_bytes
;
891 fmemset(&ctx
, 0, sizeof(ctx
));
892 ctx
.cpus
= get_number_cpus_online();
896 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
897 &opt_index
)) != EOF
) {
918 cpus_tmp
= strtoul(optarg
, NULL
, 0);
919 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
924 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
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 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
959 seed
= strtoul(optarg
, NULL
, 0);
963 orig_num
= strtoul(optarg
, NULL
, 0);
969 gap
= strtoul(optarg
, NULL
, 0);
971 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
972 if (!isdigit(optarg
[j
- i
]))
977 if (!strncmp(ptr
, "ns", strlen("ns"))) {
978 ctx
.gap
.tv_sec
= gap
/ 1000000000;
979 ctx
.gap
.tv_nsec
= gap
% 1000000000;
980 } else if (*ptr
== '\0' || !strncmp(ptr
, "us", strlen("us"))) {
981 /* Default to microseconds for backwards
982 * compatibility if no postfix is given.
984 ctx
.gap
.tv_sec
= gap
/ 1000000;
985 ctx
.gap
.tv_nsec
= (gap
% 1000000) * 1000;
986 } else if (!strncmp(ptr
, "ms", strlen("ms"))) {
987 ctx
.gap
.tv_sec
= gap
/ 1000;
988 ctx
.gap
.tv_nsec
= (gap
% 1000) * 1000000;
989 } else if (!strncmp(ptr
, "s", strlen("s"))) {
990 ctx
.gap
.tv_sec
= gap
;
993 panic("Syntax error in time param!\n");
996 /* Fall back to single core to not mess up
997 * correct timing. We are slow anyway!
1003 ctx
.reserve_size
= 0;
1005 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1006 if (!isdigit(optarg
[j
- i
]))
1011 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1012 ctx
.reserve_size
= 1 << 10;
1013 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1014 ctx
.reserve_size
= 1 << 20;
1015 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1016 ctx
.reserve_size
= 1 << 30;
1018 panic("Syntax error in ring size param!\n");
1021 ctx
.reserve_size
*= strtol(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(SIGHUP
, signal_handler
);
1061 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1063 set_system_socket_memory(vals
, array_size(vals
));
1067 ctx
.device_trans
= xstrdup(ctx
.device
);
1070 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1074 irq
= device_irq_number(ctx
.device
);
1075 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1077 stats
= setup_shared_var(ctx
.cpus
);
1079 for (i
= 0; i
< ctx
.cpus
; i
++) {
1085 seed
= generate_srand_seed();
1089 main_loop(&ctx
, confname
, slow
, i
, invoke_cpp
, orig_num
);
1093 panic("Cannot fork processes!\n");
1097 for (i
= 0; i
< ctx
.cpus
; i
++) {
1101 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1106 leave_rfmon_mac80211(ctx
.device
);
1108 reset_system_socket_memory(vals
, array_size(vals
));
1110 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1111 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1114 tx_packets
+= stats
[i
].tx_packets
;
1115 tx_bytes
+= stats
[i
].tx_bytes
;
1120 printf("\r%12llu packets outgoing\n", tx_packets
);
1121 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1122 for (i
= 0; cpustats
&& i
< ctx
.cpus
; i
++) {
1123 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1124 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1125 stats
[i
].tx_packets
);
1130 destroy_shared_var(stats
, ctx
.cpus
);
1131 device_restore_irq_affinity_list();
1134 free(ctx
.device_trans
);