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.
13 #include <sys/socket.h>
14 #include <sys/types.h>
15 #include <sys/fsuid.h>
20 #include <net/ethernet.h>
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <linux/icmp.h>
24 #include <arpa/inet.h>
42 #include "trafgen_conf.h"
48 bool rand
, rfraw
, jumbo_support
, verbose
, smoke_test
, enforce
;
49 unsigned long kpull
, num
, gap
, reserve_size
, cpus
;
50 uid_t uid
; gid_t gid
; char *device
, *device_trans
, *rhost
;
51 struct sockaddr_in dest
;
55 unsigned long tv_sec
, tv_usec
;
56 unsigned long long tx_packets
, tx_bytes
;
57 unsigned long long cf_packets
, cf_bytes
;
58 unsigned long long cd_packets
;
62 sig_atomic_t sigint
= 0;
64 struct packet
*packets
= NULL
;
67 struct packet_dyn
*packet_dyn
= NULL
;
70 static const char *short_options
= "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:";
71 static const struct option long_options
[] = {
72 {"dev", required_argument
, NULL
, 'd'},
73 {"out", required_argument
, NULL
, 'o'},
74 {"in", required_argument
, NULL
, 'i'},
75 {"conf", required_argument
, NULL
, 'c'},
76 {"num", required_argument
, NULL
, 'n'},
77 {"gap", required_argument
, NULL
, 't'},
78 {"cpus", required_argument
, NULL
, 'P'},
79 {"ring-size", required_argument
, NULL
, 'S'},
80 {"kernel-pull", required_argument
, NULL
, 'k'},
81 {"smoke-test", required_argument
, NULL
, 's'},
82 {"seed", required_argument
, NULL
, 'E'},
83 {"user", required_argument
, NULL
, 'u'},
84 {"group", required_argument
, NULL
, 'g'},
85 {"jumbo-support", no_argument
, NULL
, 'J'},
86 {"cpp", no_argument
, NULL
, 'p'},
87 {"rfraw", no_argument
, NULL
, 'R'},
88 {"rand", no_argument
, NULL
, 'r'},
89 {"verbose", no_argument
, NULL
, 'V'},
90 {"version", no_argument
, NULL
, 'v'},
91 {"example", no_argument
, NULL
, 'e'},
92 {"help", no_argument
, NULL
, 'h'},
98 static struct itimerval itimer
;
100 static unsigned long interval
= TX_KERNEL_PULL_INT
;
102 static struct cpu_stats
*stats
;
106 #define CPU_STATS_STATE_CFG 1
107 #define CPU_STATS_STATE_CHK 2
108 #define CPU_STATS_STATE_RES 4
111 # define ICMP_FILTER 1
118 static void signal_handler(int number
)
129 static void timer_elapsed(int number
)
131 int ret
= pull_and_flush_tx_ring(sock
);
132 if (unlikely(ret
< 0)) {
133 /* We could hit EBADF if the socket has been closed before
134 * the timer was triggered.
136 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
137 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
140 set_itimer_interval_value(&itimer
, 0, interval
);
141 setitimer(ITIMER_REAL
, &itimer
, NULL
);
144 static void timer_purge(void)
148 ret
= pull_and_flush_tx_ring_wait(sock
);
149 if (unlikely(ret
< 0)) {
150 /* We could hit EBADF if the socket has been closed before
151 * the timer was triggered.
153 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
154 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
157 set_itimer_interval_value(&itimer
, 0, 0);
158 setitimer(ITIMER_REAL
, &itimer
, NULL
);
161 static void __noreturn
help(void)
163 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
164 puts("http://www.netsniff-ng.org\n\n"
165 "Usage: trafgen [options]\n"
167 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
168 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
169 " -p|--cpp Run packet config through C preprocessor\n"
170 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
171 " -R|--rfraw Inject raw 802.11 frames\n"
172 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
173 " -n|--num <uint> Number of packets until exit (def: 0)\n"
174 " -r|--rand Randomize packet selection (def: round robin)\n"
175 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
176 " -t|--gap <uint> Interpacket gap in us (approx)\n"
177 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
178 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
179 " -E|--seed <uint> Manually set srand(3) seed\n"
180 " -u|--user <userid> Drop privileges and change to userid\n"
181 " -g|--group <groupid> Drop privileges and change to groupid\n"
182 " -V|--verbose Be more verbose\n"
183 " -v|--version Show version and exit\n"
184 " -e|--example Show built-in packet config example\n"
185 " -h|--help Guess what?!\n\n"
187 " See trafgen.txf for configuration file examples.\n"
188 " trafgen --dev eth0 --conf trafgen.cfg\n"
189 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
190 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
191 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
192 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000\n"
193 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
194 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
195 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
196 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
197 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
198 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
200 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
201 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
202 " we assume the kernel crashed, thus we print the packet and quit.\n"
203 " In case you find a ping-of-death, please mention trafgen in your\n"
204 " commit message of the fix!\n\n"
205 " For introducing bit errors, delays with random variation and more,\n"
206 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
207 " For generating different package distributions, you can use scripting\n"
208 " to generate a trafgen config file with packet ratios as:\n\n"
209 " IMIX 64:7, 570:4, 1518:1\n"
210 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
211 " Cisco 64:7, 594:4, 1518:1\n"
212 " RPR Trimodal 64:60, 512:20, 1518:20\n"
213 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n\n"
214 "Please report bugs to <bugs@netsniff-ng.org>\n"
215 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
216 "Swiss federal institute of technology (ETH Zurich)\n"
217 "License: GNU GPL version 2.0\n"
218 "This is free software: you are free to change and redistribute it.\n"
219 "There is NO WARRANTY, to the extent permitted by law.\n");
223 static void __noreturn
example(void)
226 "/* Note: dynamic elements make trafgen slower! */\n"
227 "#include <stddef.h>\n\n"
229 " /* MAC Destination */\n"
230 " fill(0xff, ETH_ALEN),\n"
231 " /* MAC Source */\n"
232 " 0x00, 0x02, 0xb3, drnd(3),\n"
233 " /* IPv4 Protocol */\n"
235 " /* IPv4 Version, IHL, TOS */\n"
237 " /* IPv4 Total Len */\n"
239 " /* IPv4 Ident */\n"
241 " /* IPv4 Flags, Frag Off */\n"
247 " /* IPv4 Checksum (IP header from, to) */\n"
253 " /* TCP Source Port */\n"
255 " /* TCP Dest Port */\n"
257 " /* TCP Sequence Number */\n"
259 " /* TCP Ackn. Number */\n"
261 " /* TCP Header length + TCP SYN/ECN Flag */\n"
262 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
263 " /* Window Size */\n"
265 " /* TCP Checksum (offset IP, offset TCP) */\n"
266 " csumtcp(14, 34),\n"
267 " /* TCP Options */\n"
268 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
269 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
277 static void __noreturn
version(void)
279 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_LONG
);
280 puts("http://www.netsniff-ng.org\n\n"
281 "Please report bugs to <bugs@netsniff-ng.org>\n"
282 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
283 "Swiss federal institute of technology (ETH Zurich)\n"
284 "License: GNU GPL version 2.0\n"
285 "This is free software: you are free to change and redistribute it.\n"
286 "There is NO WARRANTY, to the extent permitted by law.\n");
290 static void apply_counter(int counter_id
)
292 int j
, i
= counter_id
;
293 size_t counter_max
= packet_dyn
[i
].clen
;
295 for (j
= 0; j
< counter_max
; ++j
) {
297 struct counter
*counter
= &packet_dyn
[i
].cnt
[j
];
299 val
= counter
->val
- counter
->min
;
301 switch (counter
->type
) {
303 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
306 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
312 counter
->val
= val
+ counter
->min
;
313 packets
[i
].payload
[counter
->off
] = val
;
317 static void apply_randomizer(int rand_id
)
320 size_t rand_max
= packet_dyn
[i
].rlen
;
322 for (j
= 0; j
< rand_max
; ++j
) {
323 uint8_t val
= (uint8_t) rand();
324 struct randomizer
*randomizer
= &packet_dyn
[i
].rnd
[j
];
326 packets
[i
].payload
[randomizer
->off
] = val
;
330 static void apply_csum16(int csum_id
)
333 size_t csum_max
= packet_dyn
[i
].slen
;
335 for (j
= 0; j
< csum_max
; ++j
) {
337 struct csum16
*csum
= &packet_dyn
[i
].csum
[j
];
339 fmemset(&packets
[i
].payload
[csum
->off
], 0, sizeof(sum
));
341 switch (csum
->which
) {
343 if (csum
->to
>= packets
[i
].len
)
344 csum
->to
= packets
[i
].len
- 1;
345 sum
= calc_csum(packets
[i
].payload
+ csum
->from
,
346 csum
->to
- csum
->from
+ 1, 0);
349 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
350 packets
[i
].payload
+ csum
->to
,
351 (packets
[i
].len
- csum
->to
),
355 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
356 packets
[i
].payload
+ csum
->to
,
357 (packets
[i
].len
- csum
->to
),
362 fmemcpy(&packets
[i
].payload
[csum
->off
], &sum
, sizeof(sum
));
366 static struct cpu_stats
*setup_shared_var(unsigned long cpus
)
369 char zbuff
[cpus
* sizeof(struct cpu_stats
)], file
[256];
370 struct cpu_stats
*buff
;
372 fmemset(zbuff
, 0, sizeof(zbuff
));
373 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
375 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
379 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
381 write_or_die(fd
, zbuff
, sizeof(zbuff
));
383 buff
= (void *) mmap(0, sizeof(zbuff
), PROT_READ
| PROT_WRITE
,
385 if (buff
== (void *) -1)
386 panic("Cannot setup shared variable!\n");
391 memset(buff
, 0, sizeof(zbuff
));
396 static void destroy_shared_var(void *buff
, unsigned long cpus
)
398 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
401 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
406 for (i
= 0; i
< len
; ++i
) {
409 printf("0x%02x, ", payload
[i
]);
415 static int xmit_smoke_setup(struct ctx
*ctx
)
417 int icmp_sock
, ret
, ttl
= 64;
418 struct icmp_filter filter
;
420 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
422 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
424 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
426 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
428 panic("Cannot install filter!\n");
430 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
432 panic("Cannot set TTL!\n");
434 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
435 ctx
->dest
.sin_family
= AF_INET
;
436 ctx
->dest
.sin_port
= 0;
438 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
440 panic("Cannot resolv address!\n");
445 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
447 int ret
, i
, j
= 0, probes
= 100;
448 short ident
, cnt
= 1, idstore
[probes
];
449 uint8_t outpack
[512], *data
;
450 struct icmphdr
*icmp
;
452 size_t len
= sizeof(*icmp
) + 56;
453 struct sockaddr_in from
;
455 struct pollfd fds
= {
460 fmemset(idstore
, 0, sizeof(idstore
));
461 while (probes
-- > 0) {
462 while ((ident
= htons((short) rand())) == 0)
464 idstore
[j
++] = ident
;
466 memset(outpack
, 0, sizeof(outpack
));
467 icmp
= (void *) outpack
;
468 icmp
->type
= ICMP_ECHO
;
469 icmp
->un
.echo
.id
= ident
;
470 icmp
->un
.echo
.sequence
= htons(cnt
++);
472 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
473 for (i
= 0; i
< 56; ++i
)
474 data
[i
] = (uint8_t) rand();
476 icmp
->checksum
= csum((unsigned short *) outpack
,
477 len
/ sizeof(unsigned short));
479 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
480 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
481 if (unlikely(ret
!= len
))
482 panic("Cannot send out probe: %s!\n", strerror(errno
));
484 ret
= poll(&fds
, 1, 50);
486 panic("Poll failed!\n");
488 if (fds
.revents
& POLLIN
) {
489 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
490 (struct sockaddr
*) &from
, &from_len
);
491 if (unlikely(ret
<= 0))
492 panic("Probe receive failed!\n");
493 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
495 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
497 if (unlikely(ret
< sizeof(*ip
) + sizeof(*icmp
)))
499 ip
= (void *) outpack
;
500 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > ret
))
502 icmp
= (void *) outpack
+ ip
->ihl
* 4;
503 for (i
= 0; i
< array_size(idstore
); ++i
) {
504 if (unlikely(icmp
->un
.echo
.id
!= idstore
[i
]))
514 static void xmit_slowpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
516 int ret
, icmp_sock
= -1;
517 unsigned long num
= 1, i
= 0;
518 struct timeval start
, end
, diff
;
519 unsigned long long tx_bytes
= 0, tx_packets
= 0;
520 struct packet_dyn
*pktd
;
521 struct sockaddr_ll saddr
= {
522 .sll_family
= PF_PACKET
,
523 .sll_halen
= ETH_ALEN
,
524 .sll_ifindex
= device_ifindex(ctx
->device
),
529 if (ctx
->num
== 0 && orig_num
> 0)
533 icmp_sock
= xmit_smoke_setup(ctx
);
535 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
537 bug_on(gettimeofday(&start
, NULL
));
539 while (likely(sigint
== 0) && likely(num
> 0) && likely(plen
> 0)) {
540 pktd
= &packet_dyn
[i
];
541 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
547 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
548 (struct sockaddr
*) &saddr
, sizeof(saddr
));
549 if (unlikely(ret
< 0)) {
550 if (errno
== ENOBUFS
) {
555 panic("Sendto error: %s!\n", strerror(errno
));
558 tx_bytes
+= packets
[i
].len
;
561 if (ctx
->smoke_test
) {
562 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
563 if (unlikely(ret
< 0)) {
564 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
565 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
566 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
569 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
588 bug_on(gettimeofday(&end
, NULL
));
589 timersub(&end
, &start
, &diff
);
594 stats
[cpu
].tx_packets
= tx_packets
;
595 stats
[cpu
].tx_bytes
= tx_bytes
;
596 stats
[cpu
].tv_sec
= diff
.tv_sec
;
597 stats
[cpu
].tv_usec
= diff
.tv_usec
;
599 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
602 static void xmit_fastpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
604 int ifindex
= device_ifindex(ctx
->device
);
607 unsigned long num
= 1, i
= 0, size
;
609 struct frame_map
*hdr
;
610 struct timeval start
, end
, diff
;
611 struct packet_dyn
*pktd
;
612 unsigned long long tx_bytes
= 0, tx_packets
= 0;
614 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
616 size
= ring_size(ctx
->device
, ctx
->reserve_size
);
618 set_sock_prio(sock
, 512);
619 set_packet_loss_discard(sock
);
621 setup_tx_ring_layout(sock
, &tx_ring
, size
, ctx
->jumbo_support
);
622 create_tx_ring(sock
, &tx_ring
, ctx
->verbose
);
623 mmap_tx_ring(sock
, &tx_ring
);
624 alloc_tx_ring_frames(sock
, &tx_ring
);
625 bind_tx_ring(sock
, &tx_ring
, ifindex
);
627 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
630 interval
= ctx
->kpull
;
633 if (ctx
->num
== 0 && orig_num
> 0)
636 set_itimer_interval_value(&itimer
, 0, interval
);
637 setitimer(ITIMER_REAL
, &itimer
, NULL
);
639 bug_on(gettimeofday(&start
, NULL
));
641 while (likely(sigint
== 0) && likely(num
> 0) && likely(plen
> 0)) {
642 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) && likely(num
> 0)) {
643 hdr
= tx_ring
.frames
[it
].iov_base
;
644 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
646 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
647 hdr
->tp_h
.tp_len
= packets
[i
].len
;
649 pktd
= &packet_dyn
[i
];
650 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
656 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
658 tx_bytes
+= packets
[i
].len
;
668 kernel_may_pull_from_tx(&hdr
->tp_h
);
671 if (it
>= tx_ring
.layout
.tp_frame_nr
)
677 if (unlikely(sigint
== 1))
682 bug_on(gettimeofday(&end
, NULL
));
683 timersub(&end
, &start
, &diff
);
687 destroy_tx_ring(sock
, &tx_ring
);
689 stats
[cpu
].tx_packets
= tx_packets
;
690 stats
[cpu
].tx_bytes
= tx_bytes
;
691 stats
[cpu
].tv_sec
= diff
.tv_sec
;
692 stats
[cpu
].tv_usec
= diff
.tv_usec
;
694 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
697 static inline void __set_state(int cpu
, sig_atomic_t s
)
699 stats
[cpu
].state
= s
;
702 static inline sig_atomic_t __get_state(int cpu
)
704 return stats
[cpu
].state
;
707 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, int cpu
)
712 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
716 while ((__get_state(i
) &
717 (CPU_STATS_STATE_CFG
|
718 CPU_STATS_STATE_RES
)) == 0 &&
722 total
+= stats
[i
].cf_packets
;
728 static void __correct_global_delta(struct ctx
*ctx
, int cpu
, unsigned long orig
)
732 long long delta_correction
= 0;
734 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
738 while ((__get_state(i
) &
739 (CPU_STATS_STATE_CHK
|
740 CPU_STATS_STATE_RES
)) == 0 &&
744 total
+= stats
[i
].cd_packets
;
748 delta_correction
= -1 * ((long long) total
- orig
);
750 delta_correction
= +1 * ((long long) orig
- total
);
752 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
753 if (stats
[i
].cd_packets
> 0) {
754 if ((long long) stats
[i
].cd_packets
+
755 delta_correction
> 0) {
763 ctx
->num
+= delta_correction
;
766 static void __set_state_cf(int cpu
, unsigned long p
, unsigned long b
,
769 stats
[cpu
].cf_packets
= p
;
770 stats
[cpu
].cf_bytes
= b
;
771 stats
[cpu
].state
= s
;
774 static void __set_state_cd(int cpu
, unsigned long p
, sig_atomic_t s
)
776 stats
[cpu
].cd_packets
= p
;
777 stats
[cpu
].state
= s
;
780 static int xmit_packet_precheck(struct ctx
*ctx
, int cpu
)
783 unsigned long plen_total
, orig
= ctx
->num
;
784 size_t mtu
, total_len
= 0;
786 bug_on(plen
!= dlen
);
788 for (i
= 0; i
< plen
; ++i
)
789 total_len
+= packets
[i
].len
;
791 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
792 plen_total
= __wait_and_sum_others(ctx
, cpu
);
795 ctx
->num
= (unsigned long) nearbyint((1.0 * plen
/ plen_total
) * orig
);
797 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
798 CPU_STATS_STATE_CFG
);
799 __correct_global_delta(ctx
, cpu
, orig
);
803 __set_state(cpu
, CPU_STATS_STATE_RES
);
807 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
808 if (packets
[i
].len
> mtu
+ 14)
809 panic("Device MTU < than packet%d's size!\n", i
);
810 if (packets
[i
].len
<= 14)
811 panic("Packet%d's size too short!\n", i
);
817 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
818 int cpu
, bool invoke_cpp
, unsigned long orig_num
)
820 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
821 if (xmit_packet_precheck(ctx
, cpu
) < 0)
826 size_t total_len
= 0, total_pkts
= 0;
828 for (i
= 0; i
< ctx
->cpus
; ++i
) {
829 total_len
+= stats
[i
].cf_bytes
;
830 total_pkts
+= stats
[i
].cf_packets
;
833 printf("%6zu packets to schedule\n", total_pkts
);
834 printf("%6zu bytes in total\n", total_len
);
835 printf("Running! Hang up with ^C!\n\n");
842 xmit_slowpath_or_die(ctx
, cpu
, orig_num
);
844 xmit_fastpath_or_die(ctx
, cpu
, orig_num
);
851 static unsigned int generate_srand_seed(void)
856 fd
= open("/dev/urandom", O_RDONLY
);
860 read_or_die(fd
, &seed
, sizeof(seed
));
866 int main(int argc
, char **argv
)
868 bool slow
= false, invoke_cpp
= false, reseed
= true;
869 int c
, opt_index
, i
, j
, vals
[4] = {0}, irq
;
870 char *confname
= NULL
, *ptr
;
871 unsigned long cpus_tmp
, orig_num
= 0;
872 unsigned long long tx_packets
, tx_bytes
;
875 fmemset(&ctx
, 0, sizeof(ctx
));
876 ctx
.cpus
= get_number_cpus_online();
880 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
881 &opt_index
)) != EOF
) {
899 cpus_tmp
= strtoul(optarg
, NULL
, 0);
900 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
905 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
913 ctx
.smoke_test
= true;
914 ctx
.rhost
= xstrdup(optarg
);
920 ctx
.jumbo_support
= true;
924 confname
= xstrdup(optarg
);
925 if (!strncmp("-", confname
, strlen("-")))
929 ctx
.uid
= strtoul(optarg
, NULL
, 0);
933 ctx
.gid
= strtoul(optarg
, NULL
, 0);
937 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
940 seed
= strtoul(optarg
, NULL
, 0);
944 orig_num
= strtoul(optarg
, NULL
, 0);
949 ctx
.gap
= strtoul(optarg
, NULL
, 0);
951 /* Fall back to single core to not
952 * mess up correct timing. We are slow
959 ctx
.reserve_size
= 0;
961 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
962 if (!isdigit(optarg
[j
- i
]))
967 if (!strncmp(ptr
, "KiB", strlen("KiB")))
968 ctx
.reserve_size
= 1 << 10;
969 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
970 ctx
.reserve_size
= 1 << 20;
971 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
972 ctx
.reserve_size
= 1 << 30;
974 panic("Syntax error in ring size param!\n");
977 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
994 panic("Option -%c requires an argument!\n",
998 printf("Unknown option character `0x%X\'!\n", optopt
);
1008 if (ctx
.device
== NULL
)
1009 panic("No networking device given!\n");
1010 if (confname
== NULL
)
1011 panic("No configuration file given!\n");
1012 if (device_mtu(ctx
.device
) == 0)
1013 panic("This is no networking device!\n");
1015 register_signal(SIGINT
, signal_handler
);
1016 register_signal(SIGHUP
, signal_handler
);
1017 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1019 set_system_socket_memory(vals
, array_size(vals
));
1023 ctx
.device_trans
= xstrdup(ctx
.device
);
1026 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1030 irq
= device_irq_number(ctx
.device
);
1031 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1033 stats
= setup_shared_var(ctx
.cpus
);
1035 for (i
= 0; i
< ctx
.cpus
; i
++) {
1041 seed
= generate_srand_seed();
1045 main_loop(&ctx
, confname
, slow
, i
, invoke_cpp
, orig_num
);
1049 panic("Cannot fork processes!\n");
1053 for (i
= 0; i
< ctx
.cpus
; i
++) {
1057 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1062 leave_rfmon_mac80211(ctx
.device_trans
, ctx
.device
);
1064 reset_system_socket_memory(vals
, array_size(vals
));
1066 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1067 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1070 tx_packets
+= stats
[i
].tx_packets
;
1071 tx_bytes
+= stats
[i
].tx_bytes
;
1076 printf("\r%12llu packets outgoing\n", tx_packets
);
1077 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1078 for (i
= 0; i
< ctx
.cpus
; i
++) {
1079 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1080 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1081 stats
[i
].tx_packets
);
1086 destroy_shared_var(stats
, ctx
.cpus
);
1087 device_restore_irq_affinity_list();
1090 free(ctx
.device_trans
);