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 struct sockaddr_in dest
;
68 char *device
, *device_trans
, *rhost
;
72 unsigned long tv_sec
, tv_usec
;
73 unsigned long long tx_packets
, tx_bytes
;
74 unsigned long long cf_packets
, cf_bytes
;
75 unsigned long long cd_packets
;
79 sig_atomic_t sigint
= 0;
81 struct packet
*packets
= NULL
;
84 struct packet_dyn
*packet_dyn
= NULL
;
87 static const char *short_options
= "d:c:n:t:vJhS:rk:i:o:VRsP:eE:pu:g:";
88 static const struct option long_options
[] = {
89 {"dev", required_argument
, NULL
, 'd'},
90 {"out", required_argument
, NULL
, 'o'},
91 {"in", required_argument
, NULL
, 'i'},
92 {"conf", required_argument
, NULL
, 'c'},
93 {"num", required_argument
, NULL
, 'n'},
94 {"gap", required_argument
, NULL
, 't'},
95 {"cpus", required_argument
, NULL
, 'P'},
96 {"ring-size", required_argument
, NULL
, 'S'},
97 {"kernel-pull", required_argument
, NULL
, 'k'},
98 {"smoke-test", required_argument
, NULL
, 's'},
99 {"seed", required_argument
, NULL
, 'E'},
100 {"user", required_argument
, NULL
, 'u'},
101 {"group", required_argument
, NULL
, 'g'},
102 {"jumbo-support", no_argument
, NULL
, 'J'},
103 {"cpp", no_argument
, NULL
, 'p'},
104 {"rfraw", no_argument
, NULL
, 'R'},
105 {"rand", no_argument
, NULL
, 'r'},
106 {"verbose", no_argument
, NULL
, 'V'},
107 {"version", no_argument
, NULL
, 'v'},
108 {"example", no_argument
, NULL
, 'e'},
109 {"help", no_argument
, NULL
, 'h'},
115 static struct itimerval itimer
;
117 static unsigned long interval
= TX_KERNEL_PULL_INT
;
119 static struct cpu_stats
*stats
;
123 #define CPU_STATS_STATE_CFG 1
124 #define CPU_STATS_STATE_CHK 2
125 #define CPU_STATS_STATE_RES 4
127 #define set_system_socket_memory(vals) \
129 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
130 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
131 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
132 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
133 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
134 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
135 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
136 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
139 #define reset_system_socket_memory(vals) \
141 set_system_socket_mem(sock_rmem_max, vals[0]); \
142 set_system_socket_mem(sock_rmem_def, vals[1]); \
143 set_system_socket_mem(sock_wmem_max, vals[2]); \
144 set_system_socket_mem(sock_wmem_def, vals[3]); \
148 # define ICMP_FILTER 1
155 static void signal_handler(int number
)
166 static void timer_elapsed(int number
)
168 itimer
.it_interval
.tv_sec
= 0;
169 itimer
.it_interval
.tv_usec
= interval
;
171 itimer
.it_value
.tv_sec
= 0;
172 itimer
.it_value
.tv_usec
= interval
;
174 pull_and_flush_tx_ring(sock
);
175 setitimer(ITIMER_REAL
, &itimer
, NULL
);
178 static void header(void)
180 printf("%s%s%s\n", colorize_start(bold
), "trafgen " VERSION_STRING
, colorize_end());
183 static void help(void)
185 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
186 puts("http://www.netsniff-ng.org\n\n"
187 "Usage: trafgen [options]\n"
189 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
190 " -i|-c|--in|--conf <cfg-file/-> Packet configuration file/stdin\n"
191 " -p|--cpp Run packet config through preprocessor\n"
192 " -J|--jumbo-support Support 64KB Super Jumbo Frames (def: 2048B)\n"
193 " -R|--rfraw Inject raw 802.11 frames\n"
194 " -s|--smoke-test <ipv4-receiver> Test if machine survived packet\n"
195 " -n|--num <uint> Number of packets until exit (def: 0)\n"
196 " -r|--rand Randomize packet selection (def: round robin)\n"
197 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
198 " -t|--gap <uint> Interpacket gap in us (approx)\n"
199 " -S|--ring-size <size> Manually set mmap size (KB/MB/GB): e.g.\'10MB\'\n"
200 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
201 " -E|--seed <uint> Manually set srand(3) seed\n"
202 " -u|--user <userid> Drop privileges and change to userid\n"
203 " -g|--group <groupid> Drop privileges and change to groupid\n"
204 " -V|--verbose Be more verbose\n"
205 " -v|--version Show version\n"
206 " -e|--example Show built-in packet config example\n"
207 " -h|--help Guess what?!\n\n"
209 " See trafgen.txf for configuration file examples.\n"
210 " trafgen --dev eth0 --conf trafgen.cfg\n"
211 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
212 " trafgen --dev eth0 --conf trafgen.cfg --smoke-test 10.0.0.1\n"
213 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
214 " trafgen --dev eth0 --conf trafgen.cfg --rand --gap 1000\n"
215 " trafgen --dev eth0 --conf trafgen.cfg --rand --num 1400000 -k1000\n"
216 " trafgen --dev eth0 --conf trafgen.cfg -u `id -u bob` -g `id -g bob`\n\n"
217 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
218 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
219 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
220 " Run packet only on CPU1-2: cpu(1:2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
222 " Smoke test example: machine A, 10.0.0.2 (trafgen) is directly\n"
223 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
224 " we assume the kernel crashed, thus we print the packet and quit.\n"
225 " In case you find a ping-of-death, please mention trafgen in your\n"
226 " commit message of the fix!\n\n"
227 " For introducing bit errors, delays with random variation and more,\n"
228 " make use of tc(8) with its difference disciplines (e.g. netem)\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 example(void)
241 "/* Note: dynamic elements make trafgen slower! */\n\n"
242 "#define ETH_P_IP 0x0800\n\n"
243 "#define SYN (1 << 1)\n"
244 "#define ECN (1 << 6)\n\n"
246 " /* MAC Destination */\n"
248 " /* MAC Source */\n"
249 " 0x00, 0x02, 0xb3, drnd(3),\n"
250 " /* IPv4 Protocol */\n"
252 " /* IPv4 Version, IHL, TOS */\n"
254 " /* IPv4 Total Len */\n"
256 " /* IPv4 Ident */\n"
258 " /* IPv4 Flags, Frag Off */\n"
264 " /* IPv4 Checksum (IP header from, to) */\n"
270 " /* TCP Source Port */\n"
272 " /* TCP Dest Port */\n"
274 " /* TCP Sequence Number */\n"
276 " /* TCP Ackn. Number */\n"
278 " /* TCP Header length + TCP SYN/ECN Flag */\n"
279 " c16((0x8 << 12) | SYN | ECN)\n"
280 " /* Window Size */\n"
282 " /* TCP Checksum (offset IP, offset TCP) */\n"
283 " csumtcp(14, 34),\n"
284 " /* TCP Options */\n"
285 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
286 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
294 static void version(void)
296 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
297 puts("http://www.netsniff-ng.org\n\n"
298 "Please report bugs to <bugs@netsniff-ng.org>\n"
299 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
300 "Swiss federal institute of technology (ETH Zurich)\n"
301 "License: GNU GPL version 2.0\n"
302 "This is free software: you are free to change and redistribute it.\n"
303 "There is NO WARRANTY, to the extent permitted by law.\n");
307 static void apply_counter(int counter_id
)
309 int j
, i
= counter_id
;
310 size_t counter_max
= packet_dyn
[i
].clen
;
312 for (j
= 0; j
< counter_max
; ++j
) {
314 struct counter
*counter
= &packet_dyn
[i
].cnt
[j
];
316 val
= counter
->val
- counter
->min
;
318 switch (counter
->type
) {
320 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
323 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
329 counter
->val
= val
+ counter
->min
;
330 packets
[i
].payload
[counter
->off
] = val
;
334 static void apply_randomizer(int rand_id
)
337 size_t rand_max
= packet_dyn
[i
].rlen
;
339 for (j
= 0; j
< rand_max
; ++j
) {
340 uint8_t val
= (uint8_t) rand();
341 struct randomizer
*randomizer
= &packet_dyn
[i
].rnd
[j
];
343 packets
[i
].payload
[randomizer
->off
] = val
;
347 /* Taken and modified from tcpdump, Copyright belongs to them! */
354 #define ADDCARRY(x) \
355 do { if ((x) > 65535) \
362 sum = l_util.s[0] + l_util.s[1]; \
366 static u16
__in_cksum(const struct cksum_vec
*vec
, int veclen
)
368 register const u16
*w
;
369 register int sum
= 0, mlen
= 0;
370 int byte_swapped
= 0;
380 for (; veclen
!= 0; vec
++, veclen
--) {
384 w
= (const u16
*) (void *) vec
->ptr
;
387 s_util
.c
[1] = *(const u8
*) w
;
389 w
= (const u16
*) (void *) ((const u8
*) w
+ 1);
394 if ((1 & (unsigned long) w
) && (mlen
> 0)) {
397 s_util
.c
[0] = *(const u8
*) w
;
398 w
= (const u16
*) (void *) ((const u8
*) w
+ 1);
403 while ((mlen
-= 32) >= 0) {
404 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
405 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
406 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
407 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
413 while ((mlen
-= 8) >= 0) {
414 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
420 if (mlen
== 0 && byte_swapped
== 0)
425 while ((mlen
-= 2) >= 0) {
435 s_util
.c
[1] = *(const u8
*) w
;
440 } else if (mlen
== -1)
441 s_util
.c
[0] = *(const u8
*) w
;
451 return (~sum
& 0xffff);
454 static u16
p4_csum(const struct ip
*ip
, const u8
*data
, u16 len
,
457 struct cksum_vec vec
[2];
466 memset(&ph
, 0, sizeof(ph
));
469 ph
.proto
= next_proto
;
470 ph
.src
= ip
->ip_src
.s_addr
;
471 ph
.dst
= ip
->ip_dst
.s_addr
;
473 vec
[0].ptr
= (const u8
*) (void *) &ph
;
474 vec
[0].len
= sizeof(ph
);
479 return __in_cksum(vec
, 2);
482 static void apply_csum16(int csum_id
)
485 size_t csum_max
= packet_dyn
[i
].slen
;
487 for (j
= 0; j
< csum_max
; ++j
) {
489 struct csum16
*csum
= &packet_dyn
[i
].csum
[j
];
491 fmemset(&packets
[i
].payload
[csum
->off
], 0, sizeof(sum
));
493 switch (csum
->which
) {
495 if (csum
->to
>= packets
[i
].len
)
496 csum
->to
= packets
[i
].len
- 1;
497 sum
= calc_csum(packets
[i
].payload
+ csum
->from
,
498 csum
->to
- csum
->from
+ 1, 0);
501 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
502 packets
[i
].payload
+ csum
->to
,
503 (packets
[i
].len
- csum
->to
),
507 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
508 packets
[i
].payload
+ csum
->to
,
509 (packets
[i
].len
- csum
->to
),
514 fmemcpy(&packets
[i
].payload
[csum
->off
], &sum
, sizeof(sum
));
518 static struct cpu_stats
*setup_shared_var(unsigned long cpus
)
521 char zbuff
[cpus
* sizeof(struct cpu_stats
)], file
[256];
522 struct cpu_stats
*buff
;
524 fmemset(zbuff
, 0, sizeof(zbuff
));
525 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
527 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
531 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
533 write_or_die(fd
, zbuff
, sizeof(zbuff
));
535 buff
= (void *) mmap(0, sizeof(zbuff
), PROT_READ
| PROT_WRITE
,
537 if (buff
== (void *) -1)
538 panic("Cannot setup shared variable!\n");
543 memset(buff
, 0, sizeof(zbuff
));
548 static void destroy_shared_var(void *buff
, unsigned long cpus
)
550 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
553 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
558 for (i
= 0; i
< len
; ++i
) {
561 printf("0x%02x, ", payload
[i
]);
567 static inline unsigned short csum(unsigned short *buf
, int nwords
)
571 for (sum
= 0; nwords
> 0; nwords
--)
573 sum
= (sum
>> 16) + (sum
& 0xffff);
579 static int xmit_smoke_setup(struct ctx
*ctx
)
581 int icmp_sock
, ret
, ttl
= 64;
582 struct icmp_filter filter
;
584 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
586 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
588 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
590 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
592 panic("Cannot install filter!\n");
594 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
596 panic("Cannot set TTL!\n");
598 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
599 ctx
->dest
.sin_family
= AF_INET
;
600 ctx
->dest
.sin_port
= 0;
602 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
604 panic("Cannot resolv address!\n");
609 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
611 int ret
, i
, probes
= 5;
612 short ident
, cnt
= 1;
613 uint8_t outpack
[512], *data
;
614 struct icmphdr
*icmp
;
616 size_t len
= sizeof(*icmp
) + 56;
617 struct sockaddr_in from
;
619 struct pollfd fds
= {
624 while (probes
-- > 0) {
625 ident
= htons((short) rand());
627 memset(outpack
, 0, sizeof(outpack
));
628 icmp
= (void *) outpack
;
629 icmp
->type
= ICMP_ECHO
;
632 icmp
->un
.echo
.id
= ident
;
633 icmp
->un
.echo
.sequence
= htons(cnt
++);
635 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
636 for (i
= 0; i
< 56; ++i
)
637 data
[i
] = (uint8_t) rand();
639 icmp
->checksum
= csum((unsigned short *) outpack
,
640 len
/ sizeof(unsigned short));
642 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
643 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
644 if (unlikely(ret
!= len
))
645 panic("Cannot send out probe: %s!\n", strerror(errno
));
647 ret
= poll(&fds
, 1, 500);
649 panic("Poll failed!\n");
651 if (fds
.revents
& POLLIN
) {
652 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
653 (struct sockaddr
*) &from
, &from_len
);
654 if (unlikely(ret
<= 0))
655 panic("Probe receive failed!\n");
656 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
658 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
660 if (unlikely(ret
< sizeof(*ip
) + sizeof(*icmp
)))
662 ip
= (void *) outpack
;
663 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > ret
))
665 icmp
= (void *) outpack
+ ip
->ihl
* 4;
666 if (unlikely(icmp
->un
.echo
.id
!= ident
))
676 static void xmit_slowpath_or_die(struct ctx
*ctx
, int cpu
)
678 int ret
, icmp_sock
= -1;
679 unsigned long num
= 1, i
= 0;
680 struct timeval start
, end
, diff
;
681 unsigned long long tx_bytes
= 0, tx_packets
= 0;
682 struct packet_dyn
*pktd
;
683 struct sockaddr_ll saddr
= {
684 .sll_family
= PF_PACKET
,
685 .sll_halen
= ETH_ALEN
,
686 .sll_ifindex
= device_ifindex(ctx
->device
),
693 icmp_sock
= xmit_smoke_setup(ctx
);
695 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
697 bug_on(gettimeofday(&start
, NULL
));
699 while (likely(sigint
== 0) && likely(num
> 0)) {
700 pktd
= &packet_dyn
[i
];
701 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
707 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
708 (struct sockaddr
*) &saddr
, sizeof(saddr
));
709 if (unlikely(ret
< 0)) {
710 if (errno
== ENOBUFS
) {
715 panic("Sendto error: %s!\n", strerror(errno
));
718 tx_bytes
+= packets
[i
].len
;
721 if (ctx
->smoke_test
) {
722 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
723 if (unlikely(ret
< 0)) {
724 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
725 printf(" Remote host seems to be unresponsive to ICMP pings!\n");
726 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
729 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
748 bug_on(gettimeofday(&end
, NULL
));
749 diff
= tv_subtract(end
, start
);
754 stats
[cpu
].tx_packets
= tx_packets
;
755 stats
[cpu
].tx_bytes
= tx_bytes
;
756 stats
[cpu
].tv_sec
= diff
.tv_sec
;
757 stats
[cpu
].tv_usec
= diff
.tv_usec
;
759 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
762 static void xmit_fastpath_or_die(struct ctx
*ctx
, int cpu
)
764 int ifindex
= device_ifindex(ctx
->device
);
767 unsigned long num
= 1, i
= 0, size
;
769 struct frame_map
*hdr
;
770 struct timeval start
, end
, diff
;
771 struct packet_dyn
*pktd
;
772 unsigned long long tx_bytes
= 0, tx_packets
= 0;
774 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
776 size
= ring_size(ctx
->device
, ctx
->reserve_size
);
778 set_sock_prio(sock
, 512);
779 set_packet_loss_discard(sock
);
781 setup_tx_ring_layout(sock
, &tx_ring
, size
, ctx
->jumbo_support
);
782 create_tx_ring(sock
, &tx_ring
, ctx
->verbose
);
783 mmap_tx_ring(sock
, &tx_ring
);
784 alloc_tx_ring_frames(&tx_ring
);
785 bind_tx_ring(sock
, &tx_ring
, ifindex
);
787 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
790 interval
= ctx
->kpull
;
794 itimer
.it_interval
.tv_sec
= 0;
795 itimer
.it_interval
.tv_usec
= interval
;
797 itimer
.it_value
.tv_sec
= 0;
798 itimer
.it_value
.tv_usec
= interval
;
800 setitimer(ITIMER_REAL
, &itimer
, NULL
);
802 bug_on(gettimeofday(&start
, NULL
));
804 while (likely(sigint
== 0) && likely(num
> 0)) {
805 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) && likely(num
> 0)) {
806 hdr
= tx_ring
.frames
[it
].iov_base
;
808 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
809 * sizeof(struct sockaddr_ll); */
810 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
812 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
813 hdr
->tp_h
.tp_len
= packets
[i
].len
;
815 pktd
= &packet_dyn
[i
];
816 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
822 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
824 tx_bytes
+= packets
[i
].len
;
834 kernel_may_pull_from_tx(&hdr
->tp_h
);
837 if (it
>= tx_ring
.layout
.tp_frame_nr
)
843 if (unlikely(sigint
== 1))
848 bug_on(gettimeofday(&end
, NULL
));
849 diff
= tv_subtract(end
, start
);
851 destroy_tx_ring(sock
, &tx_ring
);
853 stats
[cpu
].tx_packets
= tx_packets
;
854 stats
[cpu
].tx_bytes
= tx_bytes
;
855 stats
[cpu
].tv_sec
= diff
.tv_sec
;
856 stats
[cpu
].tv_usec
= diff
.tv_usec
;
858 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
861 static inline void __set_state(int cpu
, sig_atomic_t s
)
863 stats
[cpu
].state
= s
;
866 static inline sig_atomic_t __get_state(int cpu
)
868 return stats
[cpu
].state
;
871 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, int cpu
)
876 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
880 while ((__get_state(i
) & CPU_STATS_STATE_CFG
) == 0 &&
884 total
+= stats
[i
].cf_packets
;
890 static void __correct_global_delta(struct ctx
*ctx
, int cpu
, unsigned long orig
)
894 long long delta_correction
= 0;
896 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
900 while ((__get_state(i
) & CPU_STATS_STATE_CHK
) == 0 &&
904 total
+= stats
[i
].cd_packets
;
908 delta_correction
= -1 * ((long long) total
- orig
);
910 delta_correction
= +1 * ((long long) orig
- total
);
912 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
913 if (stats
[i
].cd_packets
> 0) {
914 if ((long long) stats
[i
].cd_packets
+
915 delta_correction
> 0) {
923 ctx
->num
+= delta_correction
;
926 static void __set_state_cf(int cpu
, unsigned long p
, unsigned long b
,
929 stats
[cpu
].cf_packets
= p
;
930 stats
[cpu
].cf_bytes
= b
;
931 stats
[cpu
].state
= s
;
934 static void __set_state_cd(int cpu
, unsigned long p
, sig_atomic_t s
)
936 stats
[cpu
].cd_packets
= p
;
937 stats
[cpu
].state
= s
;
940 static int xmit_packet_precheck(struct ctx
*ctx
, int cpu
)
943 unsigned long plen_total
, orig
= ctx
->num
;
944 size_t mtu
, total_len
= 0;
946 bug_on(plen
!= dlen
);
948 for (i
= 0; i
< plen
; ++i
)
949 total_len
+= packets
[i
].len
;
951 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
952 plen_total
= __wait_and_sum_others(ctx
, cpu
);
955 ctx
->num
= (unsigned long) nearbyint((1.0 * plen
/ plen_total
) * orig
);
957 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
958 CPU_STATS_STATE_CFG
);
959 __correct_global_delta(ctx
, cpu
, orig
);
963 __set_state(cpu
, CPU_STATS_STATE_RES
);
967 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
968 if (packets
[i
].len
> mtu
+ 14)
969 panic("Device MTU < than packet%d's size!\n", i
);
970 if (packets
[i
].len
<= 14)
971 panic("Packet%d's size too short!\n", i
);
977 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
978 int cpu
, bool invoke_cpp
)
980 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
981 if (xmit_packet_precheck(ctx
, cpu
) < 0)
986 size_t total_len
= 0, total_pkts
= 0;
988 for (i
= 0; i
< ctx
->cpus
; ++i
) {
989 total_len
+= stats
[i
].cf_bytes
;
990 total_pkts
+= stats
[i
].cf_packets
;
993 printf("%6zu packets to schedule\n", total_pkts
);
994 printf("%6zu bytes in total\n", total_len
);
995 printf("Running! Hang up with ^C!\n\n");
1002 xmit_slowpath_or_die(ctx
, cpu
);
1004 xmit_fastpath_or_die(ctx
, cpu
);
1011 static unsigned int generate_srand_seed(void)
1016 fd
= open("/dev/random", O_RDONLY
);
1020 read_or_die(fd
, &seed
, sizeof(seed
));
1026 int main(int argc
, char **argv
)
1028 bool slow
= false, invoke_cpp
= false, reseed
= true;
1029 int c
, opt_index
, i
, j
, vals
[4] = {0}, irq
;
1030 char *confname
= NULL
, *ptr
;
1031 unsigned long cpus_tmp
;
1032 unsigned long long tx_packets
, tx_bytes
;
1035 fmemset(&ctx
, 0, sizeof(ctx
));
1036 ctx
.cpus
= get_number_cpus_online();
1040 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1041 &opt_index
)) != EOF
) {
1059 cpus_tmp
= strtoul(optarg
, NULL
, 0);
1060 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
1061 ctx
.cpus
= cpus_tmp
;
1065 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
1073 ctx
.smoke_test
= true;
1074 ctx
.rhost
= xstrdup(optarg
);
1080 ctx
.jumbo_support
= true;
1084 confname
= xstrdup(optarg
);
1085 if (!strncmp("-", confname
, strlen("-")))
1089 ctx
.uid
= strtoul(optarg
, NULL
, 0);
1093 ctx
.gid
= strtoul(optarg
, NULL
, 0);
1097 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
1100 seed
= strtoul(optarg
, NULL
, 0);
1104 ctx
.num
= strtoul(optarg
, NULL
, 0);
1108 ctx
.gap
= strtoul(optarg
, NULL
, 0);
1110 /* Fall back to single core to not
1111 * mess up correct timing. We are slow
1118 ctx
.reserve_size
= 0;
1120 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
1121 if (!isdigit(optarg
[j
- i
]))
1126 if (!strncmp(ptr
, "KB", strlen("KB")))
1127 ctx
.reserve_size
= 1 << 10;
1128 else if (!strncmp(ptr
, "MB", strlen("MB")))
1129 ctx
.reserve_size
= 1 << 20;
1130 else if (!strncmp(ptr
, "GB", strlen("GB")))
1131 ctx
.reserve_size
= 1 << 30;
1133 panic("Syntax error in ring size param!\n");
1136 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
1153 panic("Option -%c requires an argument!\n",
1156 if (isprint(optopt
))
1157 whine("Unknown option character "
1158 "`0x%X\'!\n", optopt
);
1168 if (ctx
.device
== NULL
)
1169 panic("No networking device given!\n");
1170 if (confname
== NULL
)
1171 panic("No configuration file given!\n");
1172 if (device_mtu(ctx
.device
) == 0)
1173 panic("This is no networking device!\n");
1174 if (!ctx
.rfraw
&& device_up_and_running(ctx
.device
) == 0)
1175 panic("Networking device not running!\n");
1177 register_signal(SIGINT
, signal_handler
);
1178 register_signal(SIGHUP
, signal_handler
);
1179 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1183 set_system_socket_memory(vals
);
1187 ctx
.device_trans
= xstrdup(ctx
.device
);
1190 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1194 irq
= device_irq_number(ctx
.device
);
1195 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1197 if (ctx
.num
> 0 && ctx
.num
<= ctx
.cpus
)
1200 stats
= setup_shared_var(ctx
.cpus
);
1202 for (i
= 0; i
< ctx
.cpus
; i
++) {
1208 seed
= generate_srand_seed();
1212 main_loop(&ctx
, confname
, slow
, i
, invoke_cpp
);
1216 panic("Cannot fork processes!\n");
1220 for (i
= 0; i
< ctx
.cpus
; i
++) {
1224 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1229 leave_rfmon_mac80211(ctx
.device_trans
, ctx
.device
);
1231 reset_system_socket_memory(vals
);
1233 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1234 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1237 tx_packets
+= stats
[i
].tx_packets
;
1238 tx_bytes
+= stats
[i
].tx_bytes
;
1243 printf("\r%12llu packets outgoing\n", tx_packets
);
1244 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1245 for (i
= 0; i
< ctx
.cpus
; i
++) {
1246 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1247 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1248 stats
[i
].tx_packets
);
1253 destroy_shared_var(stats
, ctx
.cpus
);
1256 free(ctx
.device_trans
);