67c9b4e398aa5fd0c321ba1fe3d8f5969abd16b1
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 - 2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
8 * A high-performance network traffic generator that uses the zero-copy
9 * kernelspace TX_RING for network I/O. On comodity Gigabit hardware up
10 * to 1,488,095 pps 64 Byte pps have been achieved with 2 trafgen instances
11 * bound to different CPUs from the userspace and turned off pause frames,
12 * ask Ronald from NST (Network Security Toolkit) for more details. ;-)
13 * So, this line-rate result is the very same as pktgen from kernelspace!
15 * Who can now hold the fords when the King of the Nine Riders comes? And
16 * other armies will come. I am too late. All is lost. I tarried on the
17 * way. All is lost. Even if my errand is performed, no one will ever
18 * know. There will be no one I can tell. It will be in vain.
20 * -- The Lord of the Rings, Frodo thinking,
21 * Chapter 'The Stairs of Cirith Ungol'.
29 #include <sys/socket.h>
30 #include <sys/types.h>
31 #include <sys/fsuid.h>
36 #include <net/ethernet.h>
37 #include <netinet/in.h>
38 #include <netinet/ip.h>
39 #include <linux/icmp.h>
40 #include <arpa/inet.h>
57 #include "trafgen_conf.h"
63 bool rand
, rfraw
, jumbo_support
, verbose
, smoke_test
, enforce
;
64 unsigned long kpull
, num
, gap
, reserve_size
, cpus
;
65 uid_t uid
; gid_t gid
; 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 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:VRsP:eE:pu:g:";
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 {"jumbo-support", no_argument
, NULL
, 'J'},
101 {"cpp", no_argument
, NULL
, 'p'},
102 {"rfraw", no_argument
, NULL
, 'R'},
103 {"rand", no_argument
, NULL
, 'r'},
104 {"verbose", no_argument
, NULL
, 'V'},
105 {"version", no_argument
, NULL
, 'v'},
106 {"example", no_argument
, NULL
, 'e'},
107 {"help", no_argument
, NULL
, 'h'},
113 static struct itimerval itimer
;
115 static unsigned long interval
= TX_KERNEL_PULL_INT
;
117 static struct cpu_stats
*stats
;
121 #define CPU_STATS_STATE_CFG 1
122 #define CPU_STATS_STATE_CHK 2
123 #define CPU_STATS_STATE_RES 4
126 # define ICMP_FILTER 1
133 static void signal_handler(int number
)
144 static void timer_elapsed(int number
)
146 set_itimer_interval_value(&itimer
, 0, interval
);
147 pull_and_flush_tx_ring(sock
);
148 setitimer(ITIMER_REAL
, &itimer
, NULL
);
151 static void header(void)
153 printf("%s%s%s\n", colorize_start(bold
), "trafgen " VERSION_STRING
, colorize_end());
156 static void help(void)
158 printf("\ntrafgen %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-file/-> 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-receiver> 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 <uint> Interpacket gap in us (approx)\n"
172 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
173 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
174 " -E|--seed <uint> Manually set srand(3) seed\n"
175 " -u|--user <userid> Drop privileges and change to userid\n"
176 " -g|--group <groupid> Drop privileges and change to groupid\n"
177 " -V|--verbose Be more verbose\n"
178 " -v|--version Show version\n"
179 " -e|--example Show built-in packet config example\n"
180 " -h|--help Guess what?!\n\n"
182 " See trafgen.txf for configuration file examples.\n"
183 " trafgen --dev eth0 --conf trafgen.cfg\n"
184 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
185 " trafgen --dev eth0 --conf trafgen.cfg --smoke-test 10.0.0.1\n"
186 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
187 " trafgen --dev eth0 --conf trafgen.cfg --rand --gap 1000\n"
188 " trafgen --dev eth0 --conf trafgen.cfg --rand --num 1400000 -k1000\n"
189 " trafgen --dev eth0 --conf trafgen.cfg -u `id -u bob` -g `id -g bob`\n\n"
190 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
191 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
192 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
193 " Run packet only on CPU1-2: cpu(1:2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
195 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
196 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
197 " we assume the kernel crashed, thus we print the packet and quit.\n"
198 " In case you find a ping-of-death, please mention trafgen in your\n"
199 " commit message of the fix!\n\n"
200 " For introducing bit errors, delays with random variation and more,\n"
201 " make use of tc(8) with its difference disciplines (e.g. netem).\n\n"
202 " For generating different package distributions, you can use some\n"
203 " scripting to generate a trafgen config file with packet ratios as:\n\n"
204 " IMIX 64:7, 570:4, 1518:1\n"
205 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
206 " Cisco 64:7, 594:4, 1518:1\n"
207 " RPR Trimodal 64:60, 512:20, 1518:20\n"
208 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n\n"
209 "Please report bugs to <bugs@netsniff-ng.org>\n"
210 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
211 "Swiss federal institute of technology (ETH Zurich)\n"
212 "License: GNU GPL version 2.0\n"
213 "This is free software: you are free to change and redistribute it.\n"
214 "There is NO WARRANTY, to the extent permitted by law.\n");
218 static void example(void)
221 "/* Note: dynamic elements make trafgen slower! */\n\n"
222 "#define ETH_P_IP 0x0800\n\n"
223 "#define SYN (1 << 1)\n"
224 "#define ECN (1 << 6)\n\n"
226 " /* MAC Destination */\n"
228 " /* MAC Source */\n"
229 " 0x00, 0x02, 0xb3, drnd(3),\n"
230 " /* IPv4 Protocol */\n"
232 " /* IPv4 Version, IHL, TOS */\n"
234 " /* IPv4 Total Len */\n"
236 " /* IPv4 Ident */\n"
238 " /* IPv4 Flags, Frag Off */\n"
244 " /* IPv4 Checksum (IP header from, to) */\n"
250 " /* TCP Source Port */\n"
252 " /* TCP Dest Port */\n"
254 " /* TCP Sequence Number */\n"
256 " /* TCP Ackn. Number */\n"
258 " /* TCP Header length + TCP SYN/ECN Flag */\n"
259 " c16((0x8 << 12) | SYN | ECN)\n"
260 " /* Window Size */\n"
262 " /* TCP Checksum (offset IP, offset TCP) */\n"
263 " csumtcp(14, 34),\n"
264 " /* TCP Options */\n"
265 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
266 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
274 static void version(void)
276 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
277 puts("http://www.netsniff-ng.org\n\n"
278 "Please report bugs to <bugs@netsniff-ng.org>\n"
279 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
280 "Swiss federal institute of technology (ETH Zurich)\n"
281 "License: GNU GPL version 2.0\n"
282 "This is free software: you are free to change and redistribute it.\n"
283 "There is NO WARRANTY, to the extent permitted by law.\n");
287 static void apply_counter(int counter_id
)
289 int j
, i
= counter_id
;
290 size_t counter_max
= packet_dyn
[i
].clen
;
292 for (j
= 0; j
< counter_max
; ++j
) {
294 struct counter
*counter
= &packet_dyn
[i
].cnt
[j
];
296 val
= counter
->val
- counter
->min
;
298 switch (counter
->type
) {
300 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
303 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
309 counter
->val
= val
+ counter
->min
;
310 packets
[i
].payload
[counter
->off
] = val
;
314 static void apply_randomizer(int rand_id
)
317 size_t rand_max
= packet_dyn
[i
].rlen
;
319 for (j
= 0; j
< rand_max
; ++j
) {
320 uint8_t val
= (uint8_t) rand();
321 struct randomizer
*randomizer
= &packet_dyn
[i
].rnd
[j
];
323 packets
[i
].payload
[randomizer
->off
] = val
;
327 /* Taken and modified from tcpdump, Copyright belongs to them! */
334 #define ADDCARRY(x) \
335 do { if ((x) > 65535) \
342 sum = l_util.s[0] + l_util.s[1]; \
346 static u16
__in_cksum(const struct cksum_vec
*vec
, int veclen
)
348 register const u16
*w
;
349 register int sum
= 0, mlen
= 0;
350 int byte_swapped
= 0;
360 for (; veclen
!= 0; vec
++, veclen
--) {
364 w
= (const u16
*) (void *) vec
->ptr
;
367 s_util
.c
[1] = *(const u8
*) w
;
369 w
= (const u16
*) (void *) ((const u8
*) w
+ 1);
374 if ((1 & (unsigned long) w
) && (mlen
> 0)) {
377 s_util
.c
[0] = *(const u8
*) w
;
378 w
= (const u16
*) (void *) ((const u8
*) w
+ 1);
383 while ((mlen
-= 32) >= 0) {
384 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
385 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
386 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
387 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
393 while ((mlen
-= 8) >= 0) {
394 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
400 if (mlen
== 0 && byte_swapped
== 0)
405 while ((mlen
-= 2) >= 0) {
415 s_util
.c
[1] = *(const u8
*) w
;
420 } else if (mlen
== -1)
421 s_util
.c
[0] = *(const u8
*) w
;
431 return (~sum
& 0xffff);
434 static u16
p4_csum(const struct ip
*ip
, const u8
*data
, u16 len
,
437 struct cksum_vec vec
[2];
446 memset(&ph
, 0, sizeof(ph
));
449 ph
.proto
= next_proto
;
450 ph
.src
= ip
->ip_src
.s_addr
;
451 ph
.dst
= ip
->ip_dst
.s_addr
;
453 vec
[0].ptr
= (const u8
*) (void *) &ph
;
454 vec
[0].len
= sizeof(ph
);
459 return __in_cksum(vec
, 2);
462 static void apply_csum16(int csum_id
)
465 size_t csum_max
= packet_dyn
[i
].slen
;
467 for (j
= 0; j
< csum_max
; ++j
) {
469 struct csum16
*csum
= &packet_dyn
[i
].csum
[j
];
471 fmemset(&packets
[i
].payload
[csum
->off
], 0, sizeof(sum
));
473 switch (csum
->which
) {
475 if (csum
->to
>= packets
[i
].len
)
476 csum
->to
= packets
[i
].len
- 1;
477 sum
= calc_csum(packets
[i
].payload
+ csum
->from
,
478 csum
->to
- csum
->from
+ 1, 0);
481 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
482 packets
[i
].payload
+ csum
->to
,
483 (packets
[i
].len
- csum
->to
),
487 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
488 packets
[i
].payload
+ csum
->to
,
489 (packets
[i
].len
- csum
->to
),
494 fmemcpy(&packets
[i
].payload
[csum
->off
], &sum
, sizeof(sum
));
498 static struct cpu_stats
*setup_shared_var(unsigned long cpus
)
501 char zbuff
[cpus
* sizeof(struct cpu_stats
)], file
[256];
502 struct cpu_stats
*buff
;
504 fmemset(zbuff
, 0, sizeof(zbuff
));
505 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
507 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
511 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
513 write_or_die(fd
, zbuff
, sizeof(zbuff
));
515 buff
= (void *) mmap(0, sizeof(zbuff
), PROT_READ
| PROT_WRITE
,
517 if (buff
== (void *) -1)
518 panic("Cannot setup shared variable!\n");
523 memset(buff
, 0, sizeof(zbuff
));
528 static void destroy_shared_var(void *buff
, unsigned long cpus
)
530 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
533 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
538 for (i
= 0; i
< len
; ++i
) {
541 printf("0x%02x, ", payload
[i
]);
547 static inline unsigned short csum(unsigned short *buf
, int nwords
)
551 for (sum
= 0; nwords
> 0; nwords
--)
553 sum
= (sum
>> 16) + (sum
& 0xffff);
559 static int xmit_smoke_setup(struct ctx
*ctx
)
561 int icmp_sock
, ret
, ttl
= 64;
562 struct icmp_filter filter
;
564 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
566 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
568 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
570 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
572 panic("Cannot install filter!\n");
574 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
576 panic("Cannot set TTL!\n");
578 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
579 ctx
->dest
.sin_family
= AF_INET
;
580 ctx
->dest
.sin_port
= 0;
582 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
584 panic("Cannot resolv address!\n");
589 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
591 int ret
, i
, probes
= 5;
592 short ident
, cnt
= 1;
593 uint8_t outpack
[512], *data
;
594 struct icmphdr
*icmp
;
596 size_t len
= sizeof(*icmp
) + 56;
597 struct sockaddr_in from
;
599 struct pollfd fds
= {
604 while (probes
-- > 0) {
605 ident
= htons((short) rand());
607 memset(outpack
, 0, sizeof(outpack
));
608 icmp
= (void *) outpack
;
609 icmp
->type
= ICMP_ECHO
;
612 icmp
->un
.echo
.id
= ident
;
613 icmp
->un
.echo
.sequence
= htons(cnt
++);
615 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
616 for (i
= 0; i
< 56; ++i
)
617 data
[i
] = (uint8_t) rand();
619 icmp
->checksum
= csum((unsigned short *) outpack
,
620 len
/ sizeof(unsigned short));
622 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
623 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
624 if (unlikely(ret
!= len
))
625 panic("Cannot send out probe: %s!\n", strerror(errno
));
627 ret
= poll(&fds
, 1, 500);
629 panic("Poll failed!\n");
631 if (fds
.revents
& POLLIN
) {
632 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
633 (struct sockaddr
*) &from
, &from_len
);
634 if (unlikely(ret
<= 0))
635 panic("Probe receive failed!\n");
636 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
638 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
640 if (unlikely(ret
< sizeof(*ip
) + sizeof(*icmp
)))
642 ip
= (void *) outpack
;
643 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > ret
))
645 icmp
= (void *) outpack
+ ip
->ihl
* 4;
646 if (unlikely(icmp
->un
.echo
.id
!= ident
))
656 static void xmit_slowpath_or_die(struct ctx
*ctx
, int cpu
)
658 int ret
, icmp_sock
= -1;
659 unsigned long num
= 1, i
= 0;
660 struct timeval start
, end
, diff
;
661 unsigned long long tx_bytes
= 0, tx_packets
= 0;
662 struct packet_dyn
*pktd
;
663 struct sockaddr_ll saddr
= {
664 .sll_family
= PF_PACKET
,
665 .sll_halen
= ETH_ALEN
,
666 .sll_ifindex
= device_ifindex(ctx
->device
),
673 icmp_sock
= xmit_smoke_setup(ctx
);
675 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
677 bug_on(gettimeofday(&start
, NULL
));
679 while (likely(sigint
== 0) && likely(num
> 0)) {
680 pktd
= &packet_dyn
[i
];
681 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
687 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
688 (struct sockaddr
*) &saddr
, sizeof(saddr
));
689 if (unlikely(ret
< 0)) {
690 if (errno
== ENOBUFS
) {
695 panic("Sendto error: %s!\n", strerror(errno
));
698 tx_bytes
+= packets
[i
].len
;
701 if (ctx
->smoke_test
) {
702 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
703 if (unlikely(ret
< 0)) {
704 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
705 printf(" Remote host seems to be unresponsive to ICMP pings!\n");
706 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
709 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
728 bug_on(gettimeofday(&end
, NULL
));
729 diff
= tv_subtract(end
, start
);
734 stats
[cpu
].tx_packets
= tx_packets
;
735 stats
[cpu
].tx_bytes
= tx_bytes
;
736 stats
[cpu
].tv_sec
= diff
.tv_sec
;
737 stats
[cpu
].tv_usec
= diff
.tv_usec
;
739 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
742 static void xmit_fastpath_or_die(struct ctx
*ctx
, int cpu
)
744 int ifindex
= device_ifindex(ctx
->device
);
747 unsigned long num
= 1, i
= 0, size
;
749 struct frame_map
*hdr
;
750 struct timeval start
, end
, diff
;
751 struct packet_dyn
*pktd
;
752 unsigned long long tx_bytes
= 0, tx_packets
= 0;
754 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
756 size
= ring_size(ctx
->device
, ctx
->reserve_size
);
758 set_sock_prio(sock
, 512);
759 set_packet_loss_discard(sock
);
761 setup_tx_ring_layout(sock
, &tx_ring
, size
, ctx
->jumbo_support
);
762 create_tx_ring(sock
, &tx_ring
, ctx
->verbose
);
763 mmap_tx_ring(sock
, &tx_ring
);
764 alloc_tx_ring_frames(&tx_ring
);
765 bind_tx_ring(sock
, &tx_ring
, ifindex
);
767 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
770 interval
= ctx
->kpull
;
774 set_itimer_interval_value(&itimer
, 0, interval
);
775 setitimer(ITIMER_REAL
, &itimer
, NULL
);
777 bug_on(gettimeofday(&start
, NULL
));
779 while (likely(sigint
== 0) && likely(num
> 0)) {
780 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) && likely(num
> 0)) {
781 hdr
= tx_ring
.frames
[it
].iov_base
;
783 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
784 * sizeof(struct sockaddr_ll); */
785 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
787 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
788 hdr
->tp_h
.tp_len
= packets
[i
].len
;
790 pktd
= &packet_dyn
[i
];
791 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
797 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
799 tx_bytes
+= packets
[i
].len
;
809 kernel_may_pull_from_tx(&hdr
->tp_h
);
812 if (it
>= tx_ring
.layout
.tp_frame_nr
)
818 if (unlikely(sigint
== 1))
823 bug_on(gettimeofday(&end
, NULL
));
824 diff
= tv_subtract(end
, start
);
826 destroy_tx_ring(sock
, &tx_ring
);
828 stats
[cpu
].tx_packets
= tx_packets
;
829 stats
[cpu
].tx_bytes
= tx_bytes
;
830 stats
[cpu
].tv_sec
= diff
.tv_sec
;
831 stats
[cpu
].tv_usec
= diff
.tv_usec
;
833 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
836 static inline void __set_state(int cpu
, sig_atomic_t s
)
838 stats
[cpu
].state
= s
;
841 static inline sig_atomic_t __get_state(int cpu
)
843 return stats
[cpu
].state
;
846 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, int cpu
)
851 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
855 while ((__get_state(i
) & CPU_STATS_STATE_CFG
) == 0 &&
859 total
+= stats
[i
].cf_packets
;
865 static void __correct_global_delta(struct ctx
*ctx
, int cpu
, unsigned long orig
)
869 long long delta_correction
= 0;
871 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
875 while ((__get_state(i
) & CPU_STATS_STATE_CHK
) == 0 &&
879 total
+= stats
[i
].cd_packets
;
883 delta_correction
= -1 * ((long long) total
- orig
);
885 delta_correction
= +1 * ((long long) orig
- total
);
887 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
888 if (stats
[i
].cd_packets
> 0) {
889 if ((long long) stats
[i
].cd_packets
+
890 delta_correction
> 0) {
898 ctx
->num
+= delta_correction
;
901 static void __set_state_cf(int cpu
, unsigned long p
, unsigned long b
,
904 stats
[cpu
].cf_packets
= p
;
905 stats
[cpu
].cf_bytes
= b
;
906 stats
[cpu
].state
= s
;
909 static void __set_state_cd(int cpu
, unsigned long p
, sig_atomic_t s
)
911 stats
[cpu
].cd_packets
= p
;
912 stats
[cpu
].state
= s
;
915 static int xmit_packet_precheck(struct ctx
*ctx
, int cpu
)
918 unsigned long plen_total
, orig
= ctx
->num
;
919 size_t mtu
, total_len
= 0;
921 bug_on(plen
!= dlen
);
923 for (i
= 0; i
< plen
; ++i
)
924 total_len
+= packets
[i
].len
;
926 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
927 plen_total
= __wait_and_sum_others(ctx
, cpu
);
930 ctx
->num
= (unsigned long) nearbyint((1.0 * plen
/ plen_total
) * orig
);
932 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
933 CPU_STATS_STATE_CFG
);
934 __correct_global_delta(ctx
, cpu
, orig
);
938 __set_state(cpu
, CPU_STATS_STATE_RES
);
942 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
943 if (packets
[i
].len
> mtu
+ 14)
944 panic("Device MTU < than packet%d's size!\n", i
);
945 if (packets
[i
].len
<= 14)
946 panic("Packet%d's size too short!\n", i
);
952 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
953 int cpu
, bool invoke_cpp
)
955 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
956 if (xmit_packet_precheck(ctx
, cpu
) < 0)
961 size_t total_len
= 0, total_pkts
= 0;
963 for (i
= 0; i
< ctx
->cpus
; ++i
) {
964 total_len
+= stats
[i
].cf_bytes
;
965 total_pkts
+= stats
[i
].cf_packets
;
968 printf("%6zu packets to schedule\n", total_pkts
);
969 printf("%6zu bytes in total\n", total_len
);
970 printf("Running! Hang up with ^C!\n\n");
977 xmit_slowpath_or_die(ctx
, cpu
);
979 xmit_fastpath_or_die(ctx
, cpu
);
986 static unsigned int generate_srand_seed(void)
991 fd
= open("/dev/urandom", O_RDONLY
);
995 read_or_die(fd
, &seed
, sizeof(seed
));
1001 int main(int argc
, char **argv
)
1003 bool slow
= false, invoke_cpp
= false, reseed
= true;
1004 int c
, opt_index
, i
, j
, vals
[4] = {0}, irq
;
1005 char *confname
= NULL
, *ptr
;
1006 unsigned long cpus_tmp
;
1007 unsigned long long tx_packets
, tx_bytes
;
1010 fmemset(&ctx
, 0, sizeof(ctx
));
1011 ctx
.cpus
= get_number_cpus_online();
1015 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1016 &opt_index
)) != EOF
) {
1034 cpus_tmp
= strtoul(optarg
, NULL
, 0);
1035 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
1036 ctx
.cpus
= cpus_tmp
;
1040 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
1048 ctx
.smoke_test
= true;
1049 ctx
.rhost
= xstrdup(optarg
);
1055 ctx
.jumbo_support
= true;
1059 confname
= xstrdup(optarg
);
1060 if (!strncmp("-", confname
, strlen("-")))
1064 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1068 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1072 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
1075 seed
= strtoul(optarg
, NULL
, 0);
1079 ctx
.num
= strtoul(optarg
, NULL
, 0);
1083 ctx
.gap
= strtoul(optarg
, NULL
, 0);
1085 /* Fall back to single core to not
1086 * mess up correct timing. We are slow
1093 ctx
.reserve_size
= 0;
1095 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1096 if (!isdigit(optarg
[j
- i
]))
1101 if (!strncmp(ptr
, "KiB", strlen("KiB")))
1102 ctx
.reserve_size
= 1 << 10;
1103 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
1104 ctx
.reserve_size
= 1 << 20;
1105 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
1106 ctx
.reserve_size
= 1 << 30;
1108 panic("Syntax error in ring size param!\n");
1111 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1128 panic("Option -%c requires an argument!\n",
1131 if (isprint(optopt
))
1132 printf("Unknown option character `0x%X\'!\n", optopt
);
1142 if (ctx
.device
== NULL
)
1143 panic("No networking device given!\n");
1144 if (confname
== NULL
)
1145 panic("No configuration file given!\n");
1146 if (device_mtu(ctx
.device
) == 0)
1147 panic("This is no networking device!\n");
1148 if (!ctx
.rfraw
&& device_up_and_running(ctx
.device
) == 0)
1149 panic("Networking device not running!\n");
1151 register_signal(SIGINT
, signal_handler
);
1152 register_signal(SIGHUP
, signal_handler
);
1153 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1157 set_system_socket_memory(vals
, array_size(vals
));
1161 ctx
.device_trans
= xstrdup(ctx
.device
);
1164 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1168 irq
= device_irq_number(ctx
.device
);
1169 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1171 if (ctx
.num
> 0 && ctx
.num
<= ctx
.cpus
)
1174 stats
= setup_shared_var(ctx
.cpus
);
1176 for (i
= 0; i
< ctx
.cpus
; i
++) {
1182 seed
= generate_srand_seed();
1186 main_loop(&ctx
, confname
, slow
, i
, invoke_cpp
);
1190 panic("Cannot fork processes!\n");
1194 for (i
= 0; i
< ctx
.cpus
; i
++) {
1198 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1203 leave_rfmon_mac80211(ctx
.device_trans
, ctx
.device
);
1205 reset_system_socket_memory(vals
, array_size(vals
));
1207 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1208 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1211 tx_packets
+= stats
[i
].tx_packets
;
1212 tx_bytes
+= stats
[i
].tx_bytes
;
1217 printf("\r%12llu packets outgoing\n", tx_packets
);
1218 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1219 for (i
= 0; i
< ctx
.cpus
; i
++) {
1220 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1221 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1222 stats
[i
].tx_packets
);
1227 destroy_shared_var(stats
, ctx
.cpus
);
1230 free(ctx
.device_trans
);