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>
48 #include "trafgen_conf.h"
54 bool rand
, rfraw
, jumbo_support
, verbose
, smoke_test
, enforce
;
55 unsigned long kpull
, num
, gap
, reserve_size
, cpus
;
56 uid_t uid
; gid_t gid
; char *device
, *device_trans
, *rhost
;
57 struct sockaddr_in dest
;
61 unsigned long tv_sec
, tv_usec
;
62 unsigned long long tx_packets
, tx_bytes
;
63 unsigned long long cf_packets
, cf_bytes
;
64 unsigned long long cd_packets
;
68 static sig_atomic_t sigint
= 0;
70 struct packet
*packets
= NULL
;
73 struct packet_dyn
*packet_dyn
= NULL
;
76 static const char *short_options
= "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:";
77 static const struct option long_options
[] = {
78 {"dev", required_argument
, NULL
, 'd'},
79 {"out", required_argument
, NULL
, 'o'},
80 {"in", required_argument
, NULL
, 'i'},
81 {"conf", required_argument
, NULL
, 'c'},
82 {"num", required_argument
, NULL
, 'n'},
83 {"gap", required_argument
, NULL
, 't'},
84 {"cpus", required_argument
, NULL
, 'P'},
85 {"ring-size", required_argument
, NULL
, 'S'},
86 {"kernel-pull", required_argument
, NULL
, 'k'},
87 {"smoke-test", required_argument
, NULL
, 's'},
88 {"seed", required_argument
, NULL
, 'E'},
89 {"user", required_argument
, NULL
, 'u'},
90 {"group", required_argument
, NULL
, 'g'},
91 {"jumbo-support", no_argument
, NULL
, 'J'},
92 {"cpp", no_argument
, NULL
, 'p'},
93 {"rfraw", no_argument
, NULL
, 'R'},
94 {"rand", no_argument
, NULL
, 'r'},
95 {"verbose", no_argument
, NULL
, 'V'},
96 {"version", no_argument
, NULL
, 'v'},
97 {"example", no_argument
, NULL
, 'e'},
98 {"help", no_argument
, NULL
, 'h'},
103 static struct itimerval itimer
;
104 static unsigned long interval
= TX_KERNEL_PULL_INT
;
105 static struct cpu_stats
*stats
;
108 #define CPU_STATS_STATE_CFG 1
109 #define CPU_STATS_STATE_CHK 2
110 #define CPU_STATS_STATE_RES 4
113 # define ICMP_FILTER 1
120 static void signal_handler(int number
)
131 static void timer_elapsed(int number
)
133 int ret
= pull_and_flush_tx_ring(sock
);
134 if (unlikely(ret
< 0)) {
135 /* We could hit EBADF if the socket has been closed before
136 * the timer was triggered.
138 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
139 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
142 set_itimer_interval_value(&itimer
, 0, interval
);
143 setitimer(ITIMER_REAL
, &itimer
, NULL
);
146 static void timer_purge(void)
150 ret
= pull_and_flush_tx_ring_wait(sock
);
151 if (unlikely(ret
< 0)) {
152 /* We could hit EBADF if the socket has been closed before
153 * the timer was triggered.
155 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
156 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
159 set_itimer_interval_value(&itimer
, 0, 0);
160 setitimer(ITIMER_REAL
, &itimer
, NULL
);
163 static void __noreturn
help(void)
165 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
166 puts("http://www.netsniff-ng.org\n\n"
167 "Usage: trafgen [options]\n"
169 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
170 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
171 " -p|--cpp Run packet config through C preprocessor\n"
172 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
173 " -R|--rfraw Inject raw 802.11 frames\n"
174 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
175 " -n|--num <uint> Number of packets until exit (def: 0)\n"
176 " -r|--rand Randomize packet selection (def: round robin)\n"
177 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
178 " -t|--gap <uint> Interpacket gap in us (approx)\n"
179 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
180 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
181 " -E|--seed <uint> Manually set srand(3) seed\n"
182 " -u|--user <userid> Drop privileges and change to userid\n"
183 " -g|--group <groupid> Drop privileges and change to groupid\n"
184 " -V|--verbose Be more verbose\n"
185 " -v|--version Show version and exit\n"
186 " -e|--example Show built-in packet config example\n"
187 " -h|--help Guess what?!\n\n"
189 " See trafgen.txf for configuration file examples.\n"
190 " trafgen --dev eth0 --conf trafgen.cfg\n"
191 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
192 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
193 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
194 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000\n"
195 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
196 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
197 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
198 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
199 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
200 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
202 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
203 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
204 " we assume the kernel crashed, thus we print the packet and quit.\n"
205 " In case you find a ping-of-death, please mention trafgen in your\n"
206 " commit message of the fix!\n\n"
207 " For introducing bit errors, delays with random variation and more,\n"
208 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
209 " For generating different package distributions, you can use scripting\n"
210 " to generate a trafgen config file with packet ratios as:\n\n"
211 " IMIX 64:7, 570:4, 1518:1\n"
212 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
213 " Cisco 64:7, 594:4, 1518:1\n"
214 " RPR Trimodal 64:60, 512:20, 1518:20\n"
215 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n\n"
216 "Please report bugs to <bugs@netsniff-ng.org>\n"
217 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
218 "Swiss federal institute of technology (ETH Zurich)\n"
219 "License: GNU GPL version 2.0\n"
220 "This is free software: you are free to change and redistribute it.\n"
221 "There is NO WARRANTY, to the extent permitted by law.\n");
225 static void __noreturn
example(void)
228 "/* Note: dynamic elements make trafgen slower! */\n"
229 "#include <stddef.h>\n\n"
231 " /* MAC Destination */\n"
232 " fill(0xff, ETH_ALEN),\n"
233 " /* MAC Source */\n"
234 " 0x00, 0x02, 0xb3, drnd(3),\n"
235 " /* IPv4 Protocol */\n"
237 " /* IPv4 Version, IHL, TOS */\n"
239 " /* IPv4 Total Len */\n"
241 " /* IPv4 Ident */\n"
243 " /* IPv4 Flags, Frag Off */\n"
249 " /* IPv4 Checksum (IP header from, to) */\n"
255 " /* TCP Source Port */\n"
257 " /* TCP Dest Port */\n"
259 " /* TCP Sequence Number */\n"
261 " /* TCP Ackn. Number */\n"
263 " /* TCP Header length + TCP SYN/ECN Flag */\n"
264 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
265 " /* Window Size */\n"
267 " /* TCP Checksum (offset IP, offset TCP) */\n"
268 " csumtcp(14, 34),\n"
269 " /* TCP Options */\n"
270 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
271 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
279 static void __noreturn
version(void)
281 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_LONG
);
282 puts("http://www.netsniff-ng.org\n\n"
283 "Please report bugs to <bugs@netsniff-ng.org>\n"
284 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
285 "Swiss federal institute of technology (ETH Zurich)\n"
286 "License: GNU GPL version 2.0\n"
287 "This is free software: you are free to change and redistribute it.\n"
288 "There is NO WARRANTY, to the extent permitted by law.\n");
292 static void apply_counter(int counter_id
)
294 int j
, i
= counter_id
;
295 size_t counter_max
= packet_dyn
[i
].clen
;
297 for (j
= 0; j
< counter_max
; ++j
) {
299 struct counter
*counter
= &packet_dyn
[i
].cnt
[j
];
301 val
= counter
->val
- counter
->min
;
303 switch (counter
->type
) {
305 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
308 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
314 counter
->val
= val
+ counter
->min
;
315 packets
[i
].payload
[counter
->off
] = val
;
319 static void apply_randomizer(int rand_id
)
322 size_t rand_max
= packet_dyn
[i
].rlen
;
324 for (j
= 0; j
< rand_max
; ++j
) {
325 uint8_t val
= (uint8_t) rand();
326 struct randomizer
*randomizer
= &packet_dyn
[i
].rnd
[j
];
328 packets
[i
].payload
[randomizer
->off
] = val
;
332 static void apply_csum16(int csum_id
)
335 size_t csum_max
= packet_dyn
[i
].slen
;
337 for (j
= 0; j
< csum_max
; ++j
) {
339 struct csum16
*csum
= &packet_dyn
[i
].csum
[j
];
341 fmemset(&packets
[i
].payload
[csum
->off
], 0, sizeof(sum
));
343 switch (csum
->which
) {
345 if (csum
->to
>= packets
[i
].len
)
346 csum
->to
= packets
[i
].len
- 1;
347 sum
= calc_csum(packets
[i
].payload
+ csum
->from
,
348 csum
->to
- csum
->from
+ 1, 0);
351 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
352 packets
[i
].payload
+ csum
->to
,
353 (packets
[i
].len
- csum
->to
),
357 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
358 packets
[i
].payload
+ csum
->to
,
359 (packets
[i
].len
- csum
->to
),
364 fmemcpy(&packets
[i
].payload
[csum
->off
], &sum
, sizeof(sum
));
368 static struct cpu_stats
*setup_shared_var(unsigned long cpus
)
371 char zbuff
[cpus
* sizeof(struct cpu_stats
)], file
[256];
372 struct cpu_stats
*buff
;
374 fmemset(zbuff
, 0, sizeof(zbuff
));
375 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
377 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
381 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
383 write_or_die(fd
, zbuff
, sizeof(zbuff
));
385 buff
= (void *) mmap(0, sizeof(zbuff
), PROT_READ
| PROT_WRITE
,
387 if (buff
== (void *) -1)
388 panic("Cannot setup shared variable!\n");
393 memset(buff
, 0, sizeof(zbuff
));
398 static void destroy_shared_var(void *buff
, unsigned long cpus
)
400 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
403 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
408 for (i
= 0; i
< len
; ++i
) {
411 printf("0x%02x, ", payload
[i
]);
417 static int xmit_smoke_setup(struct ctx
*ctx
)
419 int icmp_sock
, ret
, ttl
= 64;
420 struct icmp_filter filter
;
422 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
424 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
426 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
428 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
430 panic("Cannot install filter!\n");
432 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
434 panic("Cannot set TTL!\n");
436 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
437 ctx
->dest
.sin_family
= AF_INET
;
438 ctx
->dest
.sin_port
= 0;
440 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
442 panic("Cannot resolv address!\n");
447 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
449 int ret
, i
, j
= 0, probes
= 100;
450 short ident
, cnt
= 1, idstore
[probes
];
451 uint8_t outpack
[512], *data
;
452 struct icmphdr
*icmp
;
454 size_t len
= sizeof(*icmp
) + 56;
455 struct sockaddr_in from
;
457 struct pollfd fds
= {
462 fmemset(idstore
, 0, sizeof(idstore
));
463 while (probes
-- > 0) {
464 while ((ident
= htons((short) rand())) == 0)
466 idstore
[j
++] = ident
;
468 memset(outpack
, 0, sizeof(outpack
));
469 icmp
= (void *) outpack
;
470 icmp
->type
= ICMP_ECHO
;
471 icmp
->un
.echo
.id
= ident
;
472 icmp
->un
.echo
.sequence
= htons(cnt
++);
474 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
475 for (i
= 0; i
< 56; ++i
)
476 data
[i
] = (uint8_t) rand();
478 icmp
->checksum
= csum((unsigned short *) outpack
,
479 len
/ sizeof(unsigned short));
481 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
482 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
483 if (unlikely(ret
!= len
))
484 panic("Cannot send out probe: %s!\n", strerror(errno
));
486 ret
= poll(&fds
, 1, 50);
488 panic("Poll failed!\n");
490 if (fds
.revents
& POLLIN
) {
491 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
492 (struct sockaddr
*) &from
, &from_len
);
493 if (unlikely(ret
<= 0))
494 panic("Probe receive failed!\n");
495 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
497 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
499 if (unlikely(ret
< sizeof(*ip
) + sizeof(*icmp
)))
501 ip
= (void *) outpack
;
502 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > ret
))
504 icmp
= (void *) outpack
+ ip
->ihl
* 4;
505 for (i
= 0; i
< array_size(idstore
); ++i
) {
506 if (unlikely(icmp
->un
.echo
.id
!= idstore
[i
]))
516 static void xmit_slowpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
518 int ret
, icmp_sock
= -1;
519 unsigned long num
= 1, i
= 0;
520 struct timeval start
, end
, diff
;
521 unsigned long long tx_bytes
= 0, tx_packets
= 0;
522 struct packet_dyn
*pktd
;
523 struct sockaddr_ll saddr
= {
524 .sll_family
= PF_PACKET
,
525 .sll_halen
= ETH_ALEN
,
526 .sll_ifindex
= device_ifindex(ctx
->device
),
531 if (ctx
->num
== 0 && orig_num
> 0)
535 icmp_sock
= xmit_smoke_setup(ctx
);
537 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
539 bug_on(gettimeofday(&start
, NULL
));
541 while (likely(sigint
== 0) && likely(num
> 0) && likely(plen
> 0)) {
542 pktd
= &packet_dyn
[i
];
543 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
549 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
550 (struct sockaddr
*) &saddr
, sizeof(saddr
));
551 if (unlikely(ret
< 0)) {
552 if (errno
== ENOBUFS
) {
557 panic("Sendto error: %s!\n", strerror(errno
));
560 tx_bytes
+= packets
[i
].len
;
563 if (ctx
->smoke_test
) {
564 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
565 if (unlikely(ret
< 0)) {
566 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
567 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
568 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
571 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
590 bug_on(gettimeofday(&end
, NULL
));
591 timersub(&end
, &start
, &diff
);
596 stats
[cpu
].tx_packets
= tx_packets
;
597 stats
[cpu
].tx_bytes
= tx_bytes
;
598 stats
[cpu
].tv_sec
= diff
.tv_sec
;
599 stats
[cpu
].tv_usec
= diff
.tv_usec
;
601 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
604 static void xmit_fastpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
606 int ifindex
= device_ifindex(ctx
->device
);
609 unsigned long num
= 1, i
= 0, size
;
611 struct frame_map
*hdr
;
612 struct timeval start
, end
, diff
;
613 struct packet_dyn
*pktd
;
614 unsigned long long tx_bytes
= 0, tx_packets
= 0;
616 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
618 size
= ring_size(ctx
->device
, ctx
->reserve_size
);
620 set_sock_prio(sock
, 512);
621 set_packet_loss_discard(sock
);
623 setup_tx_ring_layout(sock
, &tx_ring
, size
, ctx
->jumbo_support
);
624 create_tx_ring(sock
, &tx_ring
, ctx
->verbose
);
625 mmap_tx_ring(sock
, &tx_ring
);
626 alloc_tx_ring_frames(sock
, &tx_ring
);
627 bind_tx_ring(sock
, &tx_ring
, ifindex
);
629 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
632 interval
= ctx
->kpull
;
635 if (ctx
->num
== 0 && orig_num
> 0)
638 set_itimer_interval_value(&itimer
, 0, interval
);
639 setitimer(ITIMER_REAL
, &itimer
, NULL
);
641 bug_on(gettimeofday(&start
, NULL
));
643 while (likely(sigint
== 0) && likely(num
> 0) && likely(plen
> 0)) {
644 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) && likely(num
> 0)) {
645 hdr
= tx_ring
.frames
[it
].iov_base
;
646 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
648 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
649 hdr
->tp_h
.tp_len
= packets
[i
].len
;
651 pktd
= &packet_dyn
[i
];
652 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
658 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
660 tx_bytes
+= packets
[i
].len
;
670 kernel_may_pull_from_tx(&hdr
->tp_h
);
673 if (it
>= tx_ring
.layout
.tp_frame_nr
)
679 if (unlikely(sigint
== 1))
684 bug_on(gettimeofday(&end
, NULL
));
685 timersub(&end
, &start
, &diff
);
689 destroy_tx_ring(sock
, &tx_ring
);
691 stats
[cpu
].tx_packets
= tx_packets
;
692 stats
[cpu
].tx_bytes
= tx_bytes
;
693 stats
[cpu
].tv_sec
= diff
.tv_sec
;
694 stats
[cpu
].tv_usec
= diff
.tv_usec
;
696 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
699 static inline void __set_state(int cpu
, sig_atomic_t s
)
701 stats
[cpu
].state
= s
;
704 static inline sig_atomic_t __get_state(int cpu
)
706 return stats
[cpu
].state
;
709 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, int cpu
)
714 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
718 while ((__get_state(i
) &
719 (CPU_STATS_STATE_CFG
|
720 CPU_STATS_STATE_RES
)) == 0 &&
724 total
+= stats
[i
].cf_packets
;
730 static void __correct_global_delta(struct ctx
*ctx
, int cpu
, unsigned long orig
)
734 long long delta_correction
= 0;
736 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
740 while ((__get_state(i
) &
741 (CPU_STATS_STATE_CHK
|
742 CPU_STATS_STATE_RES
)) == 0 &&
746 total
+= stats
[i
].cd_packets
;
750 delta_correction
= -1 * ((long long) total
- orig
);
752 delta_correction
= +1 * ((long long) orig
- total
);
754 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
755 if (stats
[i
].cd_packets
> 0) {
756 if ((long long) stats
[i
].cd_packets
+
757 delta_correction
> 0) {
765 ctx
->num
+= delta_correction
;
768 static void __set_state_cf(int cpu
, unsigned long p
, unsigned long b
,
771 stats
[cpu
].cf_packets
= p
;
772 stats
[cpu
].cf_bytes
= b
;
773 stats
[cpu
].state
= s
;
776 static void __set_state_cd(int cpu
, unsigned long p
, sig_atomic_t s
)
778 stats
[cpu
].cd_packets
= p
;
779 stats
[cpu
].state
= s
;
782 static int xmit_packet_precheck(struct ctx
*ctx
, int cpu
)
785 unsigned long plen_total
, orig
= ctx
->num
;
786 size_t mtu
, total_len
= 0;
788 bug_on(plen
!= dlen
);
790 for (i
= 0; i
< plen
; ++i
)
791 total_len
+= packets
[i
].len
;
793 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
794 plen_total
= __wait_and_sum_others(ctx
, cpu
);
797 ctx
->num
= (unsigned long) nearbyint((1.0 * plen
/ plen_total
) * orig
);
799 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
800 CPU_STATS_STATE_CFG
);
801 __correct_global_delta(ctx
, cpu
, orig
);
805 __set_state(cpu
, CPU_STATS_STATE_RES
);
809 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
810 if (packets
[i
].len
> mtu
+ 14)
811 panic("Device MTU < than packet%d's size!\n", i
);
812 if (packets
[i
].len
<= 14)
813 panic("Packet%d's size too short!\n", i
);
819 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
820 int cpu
, bool invoke_cpp
, unsigned long orig_num
)
822 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
823 if (xmit_packet_precheck(ctx
, cpu
) < 0)
828 size_t total_len
= 0, total_pkts
= 0;
830 for (i
= 0; i
< ctx
->cpus
; ++i
) {
831 total_len
+= stats
[i
].cf_bytes
;
832 total_pkts
+= stats
[i
].cf_packets
;
835 printf("%6zu packets to schedule\n", total_pkts
);
836 printf("%6zu bytes in total\n", total_len
);
837 printf("Running! Hang up with ^C!\n\n");
844 xmit_slowpath_or_die(ctx
, cpu
, orig_num
);
846 xmit_fastpath_or_die(ctx
, cpu
, orig_num
);
853 static unsigned int generate_srand_seed(void)
858 fd
= open("/dev/urandom", O_RDONLY
);
862 read_or_die(fd
, &seed
, sizeof(seed
));
868 int main(int argc
, char **argv
)
870 bool slow
= false, invoke_cpp
= false, reseed
= true;
871 int c
, opt_index
, i
, j
, vals
[4] = {0}, irq
;
872 char *confname
= NULL
, *ptr
;
873 unsigned long cpus_tmp
, orig_num
= 0;
874 unsigned long long tx_packets
, tx_bytes
;
877 fmemset(&ctx
, 0, sizeof(ctx
));
878 ctx
.cpus
= get_number_cpus_online();
882 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
883 &opt_index
)) != EOF
) {
901 cpus_tmp
= strtoul(optarg
, NULL
, 0);
902 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
907 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
915 ctx
.smoke_test
= true;
916 ctx
.rhost
= xstrdup(optarg
);
922 ctx
.jumbo_support
= true;
926 confname
= xstrdup(optarg
);
927 if (!strncmp("-", confname
, strlen("-")))
931 ctx
.uid
= strtoul(optarg
, NULL
, 0);
935 ctx
.gid
= strtoul(optarg
, NULL
, 0);
939 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
942 seed
= strtoul(optarg
, NULL
, 0);
946 orig_num
= strtoul(optarg
, NULL
, 0);
951 ctx
.gap
= strtoul(optarg
, NULL
, 0);
953 /* Fall back to single core to not
954 * mess up correct timing. We are slow
961 ctx
.reserve_size
= 0;
963 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
964 if (!isdigit(optarg
[j
- i
]))
969 if (!strncmp(ptr
, "KiB", strlen("KiB")))
970 ctx
.reserve_size
= 1 << 10;
971 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
972 ctx
.reserve_size
= 1 << 20;
973 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
974 ctx
.reserve_size
= 1 << 30;
976 panic("Syntax error in ring size param!\n");
979 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
996 panic("Option -%c requires an argument!\n",
1000 printf("Unknown option character `0x%X\'!\n", optopt
);
1010 if (ctx
.device
== NULL
)
1011 panic("No networking device given!\n");
1012 if (confname
== NULL
)
1013 panic("No configuration file given!\n");
1014 if (device_mtu(ctx
.device
) == 0)
1015 panic("This is no networking device!\n");
1017 register_signal(SIGINT
, signal_handler
);
1018 register_signal(SIGHUP
, signal_handler
);
1019 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1021 set_system_socket_memory(vals
, array_size(vals
));
1025 ctx
.device_trans
= xstrdup(ctx
.device
);
1028 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1032 irq
= device_irq_number(ctx
.device
);
1033 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1035 stats
= setup_shared_var(ctx
.cpus
);
1037 for (i
= 0; i
< ctx
.cpus
; i
++) {
1043 seed
= generate_srand_seed();
1047 main_loop(&ctx
, confname
, slow
, i
, invoke_cpp
, orig_num
);
1051 panic("Cannot fork processes!\n");
1055 for (i
= 0; i
< ctx
.cpus
; i
++) {
1059 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1064 leave_rfmon_mac80211(ctx
.device_trans
, ctx
.device
);
1066 reset_system_socket_memory(vals
, array_size(vals
));
1068 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1069 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1072 tx_packets
+= stats
[i
].tx_packets
;
1073 tx_bytes
+= stats
[i
].tx_bytes
;
1078 printf("\r%12llu packets outgoing\n", tx_packets
);
1079 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1080 for (i
= 0; i
< ctx
.cpus
; i
++) {
1081 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1082 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1083 stats
[i
].tx_packets
);
1088 destroy_shared_var(stats
, ctx
.cpus
);
1089 device_restore_irq_affinity_list();
1092 free(ctx
.device_trans
);