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>
56 #include "trafgen_conf.h"
62 bool rand
, rfraw
, jumbo_support
, verbose
, smoke_test
;
63 unsigned long kpull
, num
, gap
, reserve_size
, cpus
;
64 struct sockaddr_in dest
;
65 char *device
, *device_trans
, *rhost
;
69 unsigned long tv_sec
, tv_usec
;
70 unsigned long long tx_packets
, tx_bytes
;
71 unsigned long long cf_packets
, cf_bytes
;
72 unsigned long long cd_packets
;
76 sig_atomic_t sigint
= 0;
78 struct packet
*packets
= NULL
;
81 struct packet_dyn
*packet_dyn
= NULL
;
84 static const char *short_options
= "d:c:n:t:vJhS:rk:i:o:VRsP:eE:";
85 static const struct option long_options
[] = {
86 {"dev", required_argument
, NULL
, 'd'},
87 {"out", required_argument
, NULL
, 'o'},
88 {"in", required_argument
, NULL
, 'i'},
89 {"conf", required_argument
, NULL
, 'c'},
90 {"num", required_argument
, NULL
, 'n'},
91 {"gap", required_argument
, NULL
, 't'},
92 {"cpus", required_argument
, NULL
, 'P'},
93 {"ring-size", required_argument
, NULL
, 'S'},
94 {"kernel-pull", required_argument
, NULL
, 'k'},
95 {"smoke-test", required_argument
, NULL
, 's'},
96 {"seed", required_argument
, NULL
, 'E'},
97 {"jumbo-support", no_argument
, NULL
, 'J'},
98 {"rfraw", no_argument
, NULL
, 'R'},
99 {"rand", no_argument
, NULL
, 'r'},
100 {"verbose", no_argument
, NULL
, 'V'},
101 {"version", no_argument
, NULL
, 'v'},
102 {"example", no_argument
, NULL
, 'e'},
103 {"help", no_argument
, NULL
, 'h'},
109 static struct itimerval itimer
;
111 static unsigned long interval
= TX_KERNEL_PULL_INT
;
113 static struct cpu_stats
*stats
;
117 #define CPU_STATS_STATE_CFG 1
118 #define CPU_STATS_STATE_CHK 2
119 #define CPU_STATS_STATE_RES 4
121 #define set_system_socket_memory(vals) \
123 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
124 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
125 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
126 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
127 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
128 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
129 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
130 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
133 #define reset_system_socket_memory(vals) \
135 set_system_socket_mem(sock_rmem_max, vals[0]); \
136 set_system_socket_mem(sock_rmem_def, vals[1]); \
137 set_system_socket_mem(sock_wmem_max, vals[2]); \
138 set_system_socket_mem(sock_wmem_def, vals[3]); \
142 # define ICMP_FILTER 1
149 static void signal_handler(int number
)
160 static void timer_elapsed(int number
)
162 itimer
.it_interval
.tv_sec
= 0;
163 itimer
.it_interval
.tv_usec
= interval
;
165 itimer
.it_value
.tv_sec
= 0;
166 itimer
.it_value
.tv_usec
= interval
;
168 pull_and_flush_tx_ring(sock
);
169 setitimer(ITIMER_REAL
, &itimer
, NULL
);
172 static void header(void)
174 printf("%s%s%s\n", colorize_start(bold
), "trafgen " VERSION_STRING
, colorize_end());
177 static void help(void)
179 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
180 puts("http://www.netsniff-ng.org\n\n"
181 "Usage: trafgen [options]\n"
183 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
184 " -i|-c|--in|--conf <cfg-file> Packet configuration file\n"
185 " -J|--jumbo-support Support 64KB Super Jumbo Frames (def: 2048B)\n"
186 " -R|--rfraw Inject raw 802.11 frames\n"
187 " -s|--smoke-test <ipv4-receiver> Test if machine survived packet\n"
188 " -n|--num <uint> Number of packets until exit (def: 0)\n"
189 " -r|--rand Randomize packet selection (def: round robin)\n"
190 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
191 " -t|--gap <uint> Interpacket gap in us (approx)\n"
192 " -S|--ring-size <size> Manually set mmap size (KB/MB/GB): e.g.\'10MB\'\n"
193 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
194 " -E|--seed <uint> Manually set srand(3) seed\n"
195 " -V|--verbose Be more verbose\n"
196 " -v|--version Show version\n"
197 " -e|--example Show built-in packet config example\n"
198 " -h|--help Guess what?!\n\n"
200 " See trafgen.txf for configuration file examples.\n"
201 " trafgen --dev eth0 --conf trafgen.cfg\n"
202 " trafgen --dev eth0 --conf trafgen.cfg --smoke-test 10.0.0.1\n"
203 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
204 " trafgen --dev eth0 --conf trafgen.cfg --rand --gap 1000\n"
205 " trafgen --dev eth0 --conf trafgen.cfg --rand --num 1400000 -k1000\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 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 " This tool is targeted for network developers! You should\n"
217 " be aware of what you are doing and what these options above\n"
218 " mean! Only use this tool in an isolated LAN that you own!\n\n"
219 "Please report bugs to <bugs@netsniff-ng.org>\n"
220 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
221 "Swiss federal institute of technology (ETH Zurich)\n"
222 "License: GNU GPL version 2.0\n"
223 "This is free software: you are free to change and redistribute it.\n"
224 "There is NO WARRANTY, to the extent permitted by law.\n");
228 static void example(void)
231 "# Note: dynamic elements make trafgen slower!\n"
234 " # MAC Destination\n"
237 " 0x00, 0x02, 0xb3, drnd(3),\n"
240 " # IPv4 Version, IHL, TOS\n"
242 " # IPv4 Total Len\n"
246 " # IPv4 Flags, Frag Off\n"
252 " # IPv4 Checksum (IP header from, to)\n"
258 " # TCP Source Port\n"
262 " # TCP Sequence Number\n"
264 " # TCP Ackn. Number\n"
266 " # TCP Header length + TCP SYN/ECN Flag\n"
267 " c16((0x8 << 12) | (1 << 1) | (1 << 6))\n"
270 " # TCP Checksum (offset IP, offset TCP)\n"
271 " csumtcp(14, 34),\n"
273 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
274 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
282 static void version(void)
284 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
285 puts("http://www.netsniff-ng.org\n\n"
286 "Please report bugs to <bugs@netsniff-ng.org>\n"
287 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
288 "Swiss federal institute of technology (ETH Zurich)\n"
289 "License: GNU GPL version 2.0\n"
290 "This is free software: you are free to change and redistribute it.\n"
291 "There is NO WARRANTY, to the extent permitted by law.\n");
295 static void apply_counter(int counter_id
)
297 int j
, i
= counter_id
;
298 size_t counter_max
= packet_dyn
[i
].clen
;
300 for (j
= 0; j
< counter_max
; ++j
) {
302 struct counter
*counter
= &packet_dyn
[i
].cnt
[j
];
304 val
= counter
->val
- counter
->min
;
306 switch (counter
->type
) {
308 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
311 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
317 counter
->val
= val
+ counter
->min
;
318 packets
[i
].payload
[counter
->off
] = val
;
322 static void apply_randomizer(int rand_id
)
325 size_t rand_max
= packet_dyn
[i
].rlen
;
327 for (j
= 0; j
< rand_max
; ++j
) {
328 uint8_t val
= (uint8_t) rand();
329 struct randomizer
*randomizer
= &packet_dyn
[i
].rnd
[j
];
331 packets
[i
].payload
[randomizer
->off
] = val
;
335 /* Taken and modified from tcpdump, Copyright belongs to them! */
342 #define ADDCARRY(x) \
343 do { if ((x) > 65535) \
350 sum = l_util.s[0] + l_util.s[1]; \
354 static u16
__in_cksum(const struct cksum_vec
*vec
, int veclen
)
356 register const u16
*w
;
357 register int sum
= 0, mlen
= 0;
358 int byte_swapped
= 0;
368 for (; veclen
!= 0; vec
++, veclen
--) {
372 w
= (const u16
*) (void *) vec
->ptr
;
375 s_util
.c
[1] = *(const u8
*) w
;
377 w
= (const u16
*) (void *) ((const u8
*) w
+ 1);
382 if ((1 & (unsigned long) w
) && (mlen
> 0)) {
385 s_util
.c
[0] = *(const u8
*) w
;
386 w
= (const u16
*) (void *) ((const u8
*) w
+ 1);
391 while ((mlen
-= 32) >= 0) {
392 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
393 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
394 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
395 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
401 while ((mlen
-= 8) >= 0) {
402 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
408 if (mlen
== 0 && byte_swapped
== 0)
413 while ((mlen
-= 2) >= 0) {
423 s_util
.c
[1] = *(const u8
*) w
;
428 } else if (mlen
== -1)
429 s_util
.c
[0] = *(const u8
*) w
;
439 return (~sum
& 0xffff);
442 static u16
p4_csum(const struct ip
*ip
, const u8
*data
, u16 len
,
445 struct cksum_vec vec
[2];
454 memset(&ph
, 0, sizeof(ph
));
457 ph
.proto
= next_proto
;
458 ph
.src
= ip
->ip_src
.s_addr
;
459 ph
.dst
= ip
->ip_dst
.s_addr
;
461 vec
[0].ptr
= (const u8
*) (void *) &ph
;
462 vec
[0].len
= sizeof(ph
);
467 return __in_cksum(vec
, 2);
470 static void apply_csum16(int csum_id
)
473 size_t csum_max
= packet_dyn
[i
].slen
;
475 for (j
= 0; j
< csum_max
; ++j
) {
477 struct csum16
*csum
= &packet_dyn
[i
].csum
[j
];
479 fmemset(&packets
[i
].payload
[csum
->off
], 0, sizeof(sum
));
481 switch (csum
->which
) {
483 if (csum
->to
>= packets
[i
].len
)
484 csum
->to
= packets
[i
].len
- 1;
485 sum
= calc_csum(packets
[i
].payload
+ csum
->from
,
486 csum
->to
- csum
->from
+ 1, 0);
489 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
490 packets
[i
].payload
+ csum
->to
,
491 (packets
[i
].len
- csum
->to
),
495 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
496 packets
[i
].payload
+ csum
->to
,
497 (packets
[i
].len
- csum
->to
),
502 fmemcpy(&packets
[i
].payload
[csum
->off
], &sum
, sizeof(sum
));
506 static struct cpu_stats
*setup_shared_var(unsigned long cpus
)
509 char zbuff
[cpus
* sizeof(struct cpu_stats
)], file
[256];
510 struct cpu_stats
*buff
;
512 fmemset(zbuff
, 0, sizeof(zbuff
));
513 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
515 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
519 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
521 write_or_die(fd
, zbuff
, sizeof(zbuff
));
523 buff
= (void *) mmap(0, sizeof(zbuff
), PROT_READ
| PROT_WRITE
,
525 if (buff
== (void *) -1)
526 panic("Cannot setup shared variable!\n");
531 memset(buff
, 0, sizeof(zbuff
));
536 static void destroy_shared_var(void *buff
, unsigned long cpus
)
538 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
541 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
546 for (i
= 0; i
< len
; ++i
) {
549 printf("0x%02x, ", payload
[i
]);
555 static inline unsigned short csum(unsigned short *buf
, int nwords
)
559 for (sum
= 0; nwords
> 0; nwords
--)
561 sum
= (sum
>> 16) + (sum
& 0xffff);
567 static int xmit_smoke_setup(struct ctx
*ctx
)
569 int icmp_sock
, ret
, ttl
= 64;
570 struct icmp_filter filter
;
572 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
574 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
576 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
578 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
580 panic("Cannot install filter!\n");
582 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
584 panic("Cannot set TTL!\n");
586 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
587 ctx
->dest
.sin_family
= AF_INET
;
588 ctx
->dest
.sin_port
= 0;
590 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
592 panic("Cannot resolv address!\n");
597 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
599 int ret
, i
, probes
= 5;
600 short ident
, cnt
= 1;
601 uint8_t outpack
[512], *data
;
602 struct icmphdr
*icmp
;
604 size_t len
= sizeof(*icmp
) + 56;
605 struct sockaddr_in from
;
607 struct pollfd fds
= {
612 while (probes
-- > 0) {
613 ident
= htons((short) rand());
615 memset(outpack
, 0, sizeof(outpack
));
616 icmp
= (void *) outpack
;
617 icmp
->type
= ICMP_ECHO
;
620 icmp
->un
.echo
.id
= ident
;
621 icmp
->un
.echo
.sequence
= htons(cnt
++);
623 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
624 for (i
= 0; i
< 56; ++i
)
625 data
[i
] = (uint8_t) rand();
627 icmp
->checksum
= csum((unsigned short *) outpack
,
628 len
/ sizeof(unsigned short));
630 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
631 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
632 if (unlikely(ret
!= len
))
633 panic("Cannot send out probe: %s!\n", strerror(errno
));
635 ret
= poll(&fds
, 1, 500);
637 panic("Poll failed!\n");
639 if (fds
.revents
& POLLIN
) {
640 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
641 (struct sockaddr
*) &from
, &from_len
);
642 if (unlikely(ret
<= 0))
643 panic("Probe receive failed!\n");
644 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
646 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
648 if (unlikely(ret
< sizeof(*ip
) + sizeof(*icmp
)))
650 ip
= (void *) outpack
;
651 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > ret
))
653 icmp
= (void *) outpack
+ ip
->ihl
* 4;
654 if (unlikely(icmp
->un
.echo
.id
!= ident
))
664 static void xmit_slowpath_or_die(struct ctx
*ctx
, int cpu
)
666 int ret
, icmp_sock
= -1;
667 unsigned long num
= 1, i
= 0;
668 struct timeval start
, end
, diff
;
669 unsigned long long tx_bytes
= 0, tx_packets
= 0;
670 struct packet_dyn
*pktd
;
671 struct sockaddr_ll saddr
= {
672 .sll_family
= PF_PACKET
,
673 .sll_halen
= ETH_ALEN
,
674 .sll_ifindex
= device_ifindex(ctx
->device
),
681 icmp_sock
= xmit_smoke_setup(ctx
);
683 bug_on(gettimeofday(&start
, NULL
));
685 while (likely(sigint
== 0) && likely(num
> 0)) {
686 pktd
= &packet_dyn
[i
];
687 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
693 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
694 (struct sockaddr
*) &saddr
, sizeof(saddr
));
695 if (unlikely(ret
< 0)) {
696 if (errno
== ENOBUFS
) {
701 panic("Sendto error: %s!\n", strerror(errno
));
704 tx_bytes
+= packets
[i
].len
;
707 if (ctx
->smoke_test
) {
708 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
709 if (unlikely(ret
< 0)) {
710 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
711 printf(" Remote host seems to be unresponsive to ICMP pings!\n");
712 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
715 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
734 bug_on(gettimeofday(&end
, NULL
));
735 diff
= tv_subtract(end
, start
);
740 stats
[cpu
].tx_packets
= tx_packets
;
741 stats
[cpu
].tx_bytes
= tx_bytes
;
742 stats
[cpu
].tv_sec
= diff
.tv_sec
;
743 stats
[cpu
].tv_usec
= diff
.tv_usec
;
745 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
748 static void xmit_fastpath_or_die(struct ctx
*ctx
, int cpu
)
750 int ifindex
= device_ifindex(ctx
->device
);
753 unsigned long num
= 1, i
= 0, size
;
755 struct frame_map
*hdr
;
756 struct timeval start
, end
, diff
;
757 struct packet_dyn
*pktd
;
758 unsigned long long tx_bytes
= 0, tx_packets
= 0;
760 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
762 size
= ring_size(ctx
->device
, ctx
->reserve_size
);
764 set_sock_prio(sock
, 512);
765 set_packet_loss_discard(sock
);
767 setup_tx_ring_layout(sock
, &tx_ring
, size
, ctx
->jumbo_support
);
768 create_tx_ring(sock
, &tx_ring
, ctx
->verbose
);
769 mmap_tx_ring(sock
, &tx_ring
);
770 alloc_tx_ring_frames(&tx_ring
);
771 bind_tx_ring(sock
, &tx_ring
, ifindex
);
774 interval
= ctx
->kpull
;
778 itimer
.it_interval
.tv_sec
= 0;
779 itimer
.it_interval
.tv_usec
= interval
;
781 itimer
.it_value
.tv_sec
= 0;
782 itimer
.it_value
.tv_usec
= interval
;
784 setitimer(ITIMER_REAL
, &itimer
, NULL
);
786 bug_on(gettimeofday(&start
, NULL
));
788 while (likely(sigint
== 0) && likely(num
> 0)) {
789 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) && likely(num
> 0)) {
790 hdr
= tx_ring
.frames
[it
].iov_base
;
792 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
793 * sizeof(struct sockaddr_ll); */
794 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
796 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
797 hdr
->tp_h
.tp_len
= packets
[i
].len
;
799 pktd
= &packet_dyn
[i
];
800 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
806 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
808 tx_bytes
+= packets
[i
].len
;
818 kernel_may_pull_from_tx(&hdr
->tp_h
);
821 if (it
>= tx_ring
.layout
.tp_frame_nr
)
827 if (unlikely(sigint
== 1))
832 bug_on(gettimeofday(&end
, NULL
));
833 diff
= tv_subtract(end
, start
);
835 destroy_tx_ring(sock
, &tx_ring
);
837 stats
[cpu
].tx_packets
= tx_packets
;
838 stats
[cpu
].tx_bytes
= tx_bytes
;
839 stats
[cpu
].tv_sec
= diff
.tv_sec
;
840 stats
[cpu
].tv_usec
= diff
.tv_usec
;
842 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
845 static inline void __set_state(int cpu
, sig_atomic_t s
)
847 stats
[cpu
].state
= s
;
850 static inline sig_atomic_t __get_state(int cpu
)
852 return stats
[cpu
].state
;
855 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, int cpu
)
860 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
864 while ((__get_state(i
) & CPU_STATS_STATE_CFG
) == 0 &&
868 total
+= stats
[i
].cf_packets
;
874 static void __correct_global_delta(struct ctx
*ctx
, int cpu
, unsigned long orig
)
878 long long delta_correction
= 0;
880 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
884 while ((__get_state(i
) & CPU_STATS_STATE_CHK
) == 0 &&
888 total
+= stats
[i
].cd_packets
;
892 delta_correction
= -1 * ((long long) total
- orig
);
894 delta_correction
= +1 * ((long long) orig
- total
);
896 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
897 if (stats
[i
].cd_packets
> 0) {
898 if ((long long) stats
[i
].cd_packets
+
899 delta_correction
> 0) {
907 ctx
->num
+= delta_correction
;
910 static void __set_state_cf(int cpu
, unsigned long p
, unsigned long b
,
913 stats
[cpu
].cf_packets
= p
;
914 stats
[cpu
].cf_bytes
= b
;
915 stats
[cpu
].state
= s
;
918 static void __set_state_cd(int cpu
, unsigned long p
, sig_atomic_t s
)
920 stats
[cpu
].cd_packets
= p
;
921 stats
[cpu
].state
= s
;
924 static int xmit_packet_precheck(struct ctx
*ctx
, int cpu
)
927 unsigned long plen_total
, orig
= ctx
->num
;
928 size_t mtu
, total_len
= 0;
930 bug_on(plen
!= dlen
);
932 for (i
= 0; i
< plen
; ++i
)
933 total_len
+= packets
[i
].len
;
935 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
936 plen_total
= __wait_and_sum_others(ctx
, cpu
);
939 ctx
->num
= (unsigned long) nearbyint((1.0 * plen
/ plen_total
) * orig
);
941 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
942 CPU_STATS_STATE_CFG
);
943 __correct_global_delta(ctx
, cpu
, orig
);
947 __set_state(cpu
, CPU_STATS_STATE_RES
);
951 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
952 if (packets
[i
].len
> mtu
+ 14)
953 panic("Device MTU < than packet%d's size!\n", i
);
954 if (packets
[i
].len
<= 14)
955 panic("Packet%d's size too short!\n", i
);
961 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
, int cpu
)
963 compile_packets(confname
, ctx
->verbose
, cpu
);
964 if (xmit_packet_precheck(ctx
, cpu
) < 0)
969 size_t total_len
= 0, total_pkts
= 0;
971 for (i
= 0; i
< ctx
->cpus
; ++i
) {
972 total_len
+= stats
[i
].cf_bytes
;
973 total_pkts
+= stats
[i
].cf_packets
;
976 printf("%6zu packets to schedule\n", total_pkts
);
977 printf("%6zu bytes in total\n", total_len
);
978 printf("Running! Hang up with ^C!\n\n");
985 xmit_slowpath_or_die(ctx
, cpu
);
987 xmit_fastpath_or_die(ctx
, cpu
);
994 static unsigned int generate_srand_seed(void)
999 fd
= open("/dev/random", O_RDONLY
);
1003 read_or_die(fd
, &seed
, sizeof(seed
));
1009 int main(int argc
, char **argv
)
1012 int c
, opt_index
, i
, j
, vals
[4] = {0}, irq
;
1013 char *confname
= NULL
, *ptr
;
1014 unsigned long cpus_tmp
;
1015 unsigned long long tx_packets
, tx_bytes
;
1021 fmemset(&ctx
, 0, sizeof(ctx
));
1022 ctx
.cpus
= get_number_cpus_online();
1024 seed
= generate_srand_seed();
1026 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1027 &opt_index
)) != EOF
) {
1042 cpus_tmp
= strtoul(optarg
, NULL
, 0);
1043 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
1044 ctx
.cpus
= cpus_tmp
;
1048 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
1056 ctx
.smoke_test
= true;
1057 ctx
.rhost
= xstrdup(optarg
);
1063 ctx
.jumbo_support
= true;
1067 confname
= xstrdup(optarg
);
1070 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
1073 seed
= strtoul(optarg
, NULL
, 0);
1076 ctx
.num
= strtoul(optarg
, NULL
, 0);
1080 ctx
.gap
= strtoul(optarg
, NULL
, 0);
1082 /* Fall back to single core to not
1083 * mess up correct timing. We are slow
1090 ctx
.reserve_size
= 0;
1092 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1093 if (!isdigit(optarg
[j
- i
]))
1098 if (!strncmp(ptr
, "KB", strlen("KB")))
1099 ctx
.reserve_size
= 1 << 10;
1100 else if (!strncmp(ptr
, "MB", strlen("MB")))
1101 ctx
.reserve_size
= 1 << 20;
1102 else if (!strncmp(ptr
, "GB", strlen("GB")))
1103 ctx
.reserve_size
= 1 << 30;
1105 panic("Syntax error in ring size param!\n");
1108 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1123 panic("Option -%c requires an argument!\n",
1126 if (isprint(optopt
))
1127 whine("Unknown option character "
1128 "`0x%X\'!\n", optopt
);
1138 if (ctx
.device
== NULL
)
1139 panic("No networking device given!\n");
1140 if (confname
== NULL
)
1141 panic("No configuration file given!\n");
1142 if (device_mtu(ctx
.device
) == 0)
1143 panic("This is no networking device!\n");
1144 if (!ctx
.rfraw
&& device_up_and_running(ctx
.device
) == 0)
1145 panic("Networking device not running!\n");
1147 register_signal(SIGINT
, signal_handler
);
1148 register_signal(SIGHUP
, signal_handler
);
1149 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1155 set_system_socket_memory(vals
);
1158 ctx
.device_trans
= xstrdup(ctx
.device
);
1161 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1165 irq
= device_irq_number(ctx
.device
);
1166 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1168 if (ctx
.num
> 0 && ctx
.num
<= ctx
.cpus
)
1171 stats
= setup_shared_var(ctx
.cpus
);
1173 for (i
= 0; i
< ctx
.cpus
; i
++) {
1179 main_loop(&ctx
, confname
, slow
, i
);
1183 panic("Cannot fork processes!\n");
1187 for (i
= 0; i
< ctx
.cpus
; i
++) {
1191 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1196 leave_rfmon_mac80211(ctx
.device_trans
, ctx
.device
);
1198 reset_system_socket_memory(vals
);
1200 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1201 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1204 tx_packets
+= stats
[i
].tx_packets
;
1205 tx_bytes
+= stats
[i
].tx_bytes
;
1210 printf("\r%12llu packets outgoing\n", tx_packets
);
1211 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1212 for (i
= 0; i
< ctx
.cpus
; i
++) {
1213 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1214 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1215 stats
[i
].tx_packets
);
1219 destroy_shared_var(stats
, ctx
.cpus
);
1222 free(ctx
.device_trans
);