4bc7a43129672369fd735814a5ceae70f5bae1f0
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>
41 #include "trafgen_conf.h"
47 bool rand
, rfraw
, jumbo_support
, verbose
, smoke_test
, enforce
;
48 unsigned long kpull
, num
, gap
, reserve_size
, cpus
;
49 uid_t uid
; gid_t gid
; char *device
, *device_trans
, *rhost
;
50 struct sockaddr_in dest
;
54 unsigned long tv_sec
, tv_usec
;
55 unsigned long long tx_packets
, tx_bytes
;
56 unsigned long long cf_packets
, cf_bytes
;
57 unsigned long long cd_packets
;
61 sig_atomic_t sigint
= 0;
63 struct packet
*packets
= NULL
;
66 struct packet_dyn
*packet_dyn
= NULL
;
69 static const char *short_options
= "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:";
70 static const struct option long_options
[] = {
71 {"dev", required_argument
, NULL
, 'd'},
72 {"out", required_argument
, NULL
, 'o'},
73 {"in", required_argument
, NULL
, 'i'},
74 {"conf", required_argument
, NULL
, 'c'},
75 {"num", required_argument
, NULL
, 'n'},
76 {"gap", required_argument
, NULL
, 't'},
77 {"cpus", required_argument
, NULL
, 'P'},
78 {"ring-size", required_argument
, NULL
, 'S'},
79 {"kernel-pull", required_argument
, NULL
, 'k'},
80 {"smoke-test", required_argument
, NULL
, 's'},
81 {"seed", required_argument
, NULL
, 'E'},
82 {"user", required_argument
, NULL
, 'u'},
83 {"group", required_argument
, NULL
, 'g'},
84 {"jumbo-support", no_argument
, NULL
, 'J'},
85 {"cpp", no_argument
, NULL
, 'p'},
86 {"rfraw", no_argument
, NULL
, 'R'},
87 {"rand", no_argument
, NULL
, 'r'},
88 {"verbose", no_argument
, NULL
, 'V'},
89 {"version", no_argument
, NULL
, 'v'},
90 {"example", no_argument
, NULL
, 'e'},
91 {"help", no_argument
, NULL
, 'h'},
97 static struct itimerval itimer
;
99 static unsigned long interval
= TX_KERNEL_PULL_INT
;
101 static struct cpu_stats
*stats
;
105 #define CPU_STATS_STATE_CFG 1
106 #define CPU_STATS_STATE_CHK 2
107 #define CPU_STATS_STATE_RES 4
110 # define ICMP_FILTER 1
117 static void signal_handler(int number
)
128 static void timer_elapsed(int number
)
132 set_itimer_interval_value(&itimer
, 0, interval
);
134 ret
= pull_and_flush_tx_ring(sock
);
135 if (unlikely(ret
< 0)) {
136 /* We could hit EBADF if the socket has been closed before
137 * the timer was triggered.
139 if (errno
!= EBADF
&& errno
!= ENOBUFS
)
140 panic("Flushing TX_RING failed: %s!\n", strerror(errno
));
143 setitimer(ITIMER_REAL
, &itimer
, NULL
);
146 static void help(void)
148 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
149 puts("http://www.netsniff-ng.org\n\n"
150 "Usage: trafgen [options]\n"
152 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
153 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
154 " -p|--cpp Run packet config through C preprocessor\n"
155 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
156 " -R|--rfraw Inject raw 802.11 frames\n"
157 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
158 " -n|--num <uint> Number of packets until exit (def: 0)\n"
159 " -r|--rand Randomize packet selection (def: round robin)\n"
160 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
161 " -t|--gap <uint> Interpacket gap in us (approx)\n"
162 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
163 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
164 " -E|--seed <uint> Manually set srand(3) seed\n"
165 " -u|--user <userid> Drop privileges and change to userid\n"
166 " -g|--group <groupid> Drop privileges and change to groupid\n"
167 " -V|--verbose Be more verbose\n"
168 " -v|--version Show version\n"
169 " -e|--example Show built-in packet config example\n"
170 " -h|--help Guess what?!\n\n"
172 " See trafgen.txf for configuration file examples.\n"
173 " trafgen --dev eth0 --conf trafgen.cfg\n"
174 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
175 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
176 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
177 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000\n"
178 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
179 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
180 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
181 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
182 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
183 " Run packet only on CPU1-2: cpu(1:2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
185 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
186 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
187 " we assume the kernel crashed, thus we print the packet and quit.\n"
188 " In case you find a ping-of-death, please mention trafgen in your\n"
189 " commit message of the fix!\n\n"
190 " For introducing bit errors, delays with random variation and more,\n"
191 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
192 " For generating different package distributions, you can use scripting\n"
193 " to generate a trafgen config file with packet ratios as:\n\n"
194 " IMIX 64:7, 570:4, 1518:1\n"
195 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
196 " Cisco 64:7, 594:4, 1518:1\n"
197 " RPR Trimodal 64:60, 512:20, 1518:20\n"
198 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n\n"
199 "Please report bugs to <bugs@netsniff-ng.org>\n"
200 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
201 "Swiss federal institute of technology (ETH Zurich)\n"
202 "License: GNU GPL version 2.0\n"
203 "This is free software: you are free to change and redistribute it.\n"
204 "There is NO WARRANTY, to the extent permitted by law.\n");
208 static void example(void)
211 "/* Note: dynamic elements make trafgen slower! */\n"
212 "#include <stddef.h>\n\n"
214 " /* MAC Destination */\n"
215 " fill(0xff, ETH_ALEN),\n"
216 " /* MAC Source */\n"
217 " 0x00, 0x02, 0xb3, drnd(3),\n"
218 " /* IPv4 Protocol */\n"
220 " /* IPv4 Version, IHL, TOS */\n"
222 " /* IPv4 Total Len */\n"
224 " /* IPv4 Ident */\n"
226 " /* IPv4 Flags, Frag Off */\n"
232 " /* IPv4 Checksum (IP header from, to) */\n"
238 " /* TCP Source Port */\n"
240 " /* TCP Dest Port */\n"
242 " /* TCP Sequence Number */\n"
244 " /* TCP Ackn. Number */\n"
246 " /* TCP Header length + TCP SYN/ECN Flag */\n"
247 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
248 " /* Window Size */\n"
250 " /* TCP Checksum (offset IP, offset TCP) */\n"
251 " csumtcp(14, 34),\n"
252 " /* TCP Options */\n"
253 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
254 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
262 static void version(void)
264 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING
);
265 puts("http://www.netsniff-ng.org\n\n"
266 "Please report bugs to <bugs@netsniff-ng.org>\n"
267 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
268 "Swiss federal institute of technology (ETH Zurich)\n"
269 "License: GNU GPL version 2.0\n"
270 "This is free software: you are free to change and redistribute it.\n"
271 "There is NO WARRANTY, to the extent permitted by law.\n");
275 static void apply_counter(int counter_id
)
277 int j
, i
= counter_id
;
278 size_t counter_max
= packet_dyn
[i
].clen
;
280 for (j
= 0; j
< counter_max
; ++j
) {
282 struct counter
*counter
= &packet_dyn
[i
].cnt
[j
];
284 val
= counter
->val
- counter
->min
;
286 switch (counter
->type
) {
288 val
= (val
+ counter
->inc
) % (counter
->max
- counter
->min
+ 1);
291 val
= (val
- counter
->inc
) % (counter
->min
- counter
->max
+ 1);
297 counter
->val
= val
+ counter
->min
;
298 packets
[i
].payload
[counter
->off
] = val
;
302 static void apply_randomizer(int rand_id
)
305 size_t rand_max
= packet_dyn
[i
].rlen
;
307 for (j
= 0; j
< rand_max
; ++j
) {
308 uint8_t val
= (uint8_t) rand();
309 struct randomizer
*randomizer
= &packet_dyn
[i
].rnd
[j
];
311 packets
[i
].payload
[randomizer
->off
] = val
;
315 static void apply_csum16(int csum_id
)
318 size_t csum_max
= packet_dyn
[i
].slen
;
320 for (j
= 0; j
< csum_max
; ++j
) {
322 struct csum16
*csum
= &packet_dyn
[i
].csum
[j
];
324 fmemset(&packets
[i
].payload
[csum
->off
], 0, sizeof(sum
));
326 switch (csum
->which
) {
328 if (csum
->to
>= packets
[i
].len
)
329 csum
->to
= packets
[i
].len
- 1;
330 sum
= calc_csum(packets
[i
].payload
+ csum
->from
,
331 csum
->to
- csum
->from
+ 1, 0);
334 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
335 packets
[i
].payload
+ csum
->to
,
336 (packets
[i
].len
- csum
->to
),
340 sum
= p4_csum((void *) packets
[i
].payload
+ csum
->from
,
341 packets
[i
].payload
+ csum
->to
,
342 (packets
[i
].len
- csum
->to
),
347 fmemcpy(&packets
[i
].payload
[csum
->off
], &sum
, sizeof(sum
));
351 static struct cpu_stats
*setup_shared_var(unsigned long cpus
)
354 char zbuff
[cpus
* sizeof(struct cpu_stats
)], file
[256];
355 struct cpu_stats
*buff
;
357 fmemset(zbuff
, 0, sizeof(zbuff
));
358 slprintf(file
, sizeof(file
), ".tmp_mmap.%u", (unsigned int) rand());
360 fd
= creat(file
, S_IRUSR
| S_IWUSR
);
364 fd
= open_or_die_m(file
, O_RDWR
| O_CREAT
| O_TRUNC
,
366 write_or_die(fd
, zbuff
, sizeof(zbuff
));
368 buff
= (void *) mmap(0, sizeof(zbuff
), PROT_READ
| PROT_WRITE
,
370 if (buff
== (void *) -1)
371 panic("Cannot setup shared variable!\n");
376 memset(buff
, 0, sizeof(zbuff
));
381 static void destroy_shared_var(void *buff
, unsigned long cpus
)
383 munmap(buff
, cpus
* sizeof(struct cpu_stats
));
386 static void dump_trafgen_snippet(uint8_t *payload
, size_t len
)
391 for (i
= 0; i
< len
; ++i
) {
394 printf("0x%02x, ", payload
[i
]);
400 static int xmit_smoke_setup(struct ctx
*ctx
)
402 int icmp_sock
, ret
, ttl
= 64;
403 struct icmp_filter filter
;
405 icmp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
407 panic("Cannot get a ICMP socket: %s!\n", strerror(errno
));
409 filter
.data
= ~(1 << ICMP_ECHOREPLY
);
411 ret
= setsockopt(icmp_sock
, SOL_RAW
, ICMP_FILTER
, &filter
, sizeof(filter
));
413 panic("Cannot install filter!\n");
415 ret
= setsockopt(icmp_sock
, SOL_IP
, IP_TTL
, &ttl
, sizeof(ttl
));
417 panic("Cannot set TTL!\n");
419 memset(&ctx
->dest
, 0, sizeof(ctx
->dest
));
420 ctx
->dest
.sin_family
= AF_INET
;
421 ctx
->dest
.sin_port
= 0;
423 ret
= inet_aton(ctx
->rhost
, &ctx
->dest
.sin_addr
);
425 panic("Cannot resolv address!\n");
430 static int xmit_smoke_probe(int icmp_sock
, struct ctx
*ctx
)
432 int ret
, i
, j
= 0, probes
= 100;
433 short ident
, cnt
= 1, idstore
[probes
];
434 uint8_t outpack
[512], *data
;
435 struct icmphdr
*icmp
;
437 size_t len
= sizeof(*icmp
) + 56;
438 struct sockaddr_in from
;
440 struct pollfd fds
= {
445 fmemset(idstore
, 0, sizeof(idstore
));
446 while (probes
-- > 0) {
447 while ((ident
= htons((short) rand())) == 0)
449 idstore
[j
++] = ident
;
451 memset(outpack
, 0, sizeof(outpack
));
452 icmp
= (void *) outpack
;
453 icmp
->type
= ICMP_ECHO
;
456 icmp
->un
.echo
.id
= ident
;
457 icmp
->un
.echo
.sequence
= htons(cnt
++);
459 data
= ((uint8_t *) outpack
+ sizeof(*icmp
));
460 for (i
= 0; i
< 56; ++i
)
461 data
[i
] = (uint8_t) rand();
463 icmp
->checksum
= csum((unsigned short *) outpack
,
464 len
/ sizeof(unsigned short));
466 ret
= sendto(icmp_sock
, outpack
, len
, MSG_DONTWAIT
,
467 (struct sockaddr
*) &ctx
->dest
, sizeof(ctx
->dest
));
468 if (unlikely(ret
!= len
))
469 panic("Cannot send out probe: %s!\n", strerror(errno
));
471 ret
= poll(&fds
, 1, 50);
473 panic("Poll failed!\n");
475 if (fds
.revents
& POLLIN
) {
476 ret
= recvfrom(icmp_sock
, outpack
, sizeof(outpack
), 0,
477 (struct sockaddr
*) &from
, &from_len
);
478 if (unlikely(ret
<= 0))
479 panic("Probe receive failed!\n");
480 if (unlikely(from_len
!= sizeof(ctx
->dest
)))
482 if (unlikely(memcmp(&from
, &ctx
->dest
, sizeof(ctx
->dest
))))
484 if (unlikely(ret
< sizeof(*ip
) + sizeof(*icmp
)))
486 ip
= (void *) outpack
;
487 if (unlikely(ip
->ihl
* 4 + sizeof(*icmp
) > ret
))
489 icmp
= (void *) outpack
+ ip
->ihl
* 4;
490 for (i
= 0; i
< array_size(idstore
); ++i
) {
491 if (unlikely(icmp
->un
.echo
.id
!= idstore
[i
]))
501 static void xmit_slowpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
503 int ret
, icmp_sock
= -1;
504 unsigned long num
= 1, i
= 0;
505 struct timeval start
, end
, diff
;
506 unsigned long long tx_bytes
= 0, tx_packets
= 0;
507 struct packet_dyn
*pktd
;
508 struct sockaddr_ll saddr
= {
509 .sll_family
= PF_PACKET
,
510 .sll_halen
= ETH_ALEN
,
511 .sll_ifindex
= device_ifindex(ctx
->device
),
516 if (ctx
->num
== 0 && orig_num
> 0)
520 icmp_sock
= xmit_smoke_setup(ctx
);
522 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
524 bug_on(gettimeofday(&start
, NULL
));
526 while (likely(sigint
== 0) && likely(num
> 0) && likely(plen
> 0)) {
527 pktd
= &packet_dyn
[i
];
528 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
534 ret
= sendto(sock
, packets
[i
].payload
, packets
[i
].len
, 0,
535 (struct sockaddr
*) &saddr
, sizeof(saddr
));
536 if (unlikely(ret
< 0)) {
537 if (errno
== ENOBUFS
) {
542 panic("Sendto error: %s!\n", strerror(errno
));
545 tx_bytes
+= packets
[i
].len
;
548 if (ctx
->smoke_test
) {
549 ret
= xmit_smoke_probe(icmp_sock
, ctx
);
550 if (unlikely(ret
< 0)) {
551 printf("%sSmoke test alert:%s\n", colorize_start(bold
), colorize_end());
552 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
553 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
556 dump_trafgen_snippet(packets
[i
].payload
, packets
[i
].len
);
575 bug_on(gettimeofday(&end
, NULL
));
576 timersub(&end
, &start
, &diff
);
581 stats
[cpu
].tx_packets
= tx_packets
;
582 stats
[cpu
].tx_bytes
= tx_bytes
;
583 stats
[cpu
].tv_sec
= diff
.tv_sec
;
584 stats
[cpu
].tv_usec
= diff
.tv_usec
;
586 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
589 static void xmit_fastpath_or_die(struct ctx
*ctx
, int cpu
, unsigned long orig_num
)
591 int ifindex
= device_ifindex(ctx
->device
);
594 unsigned long num
= 1, i
= 0, size
;
596 struct frame_map
*hdr
;
597 struct timeval start
, end
, diff
;
598 struct packet_dyn
*pktd
;
599 unsigned long long tx_bytes
= 0, tx_packets
= 0;
601 fmemset(&tx_ring
, 0, sizeof(tx_ring
));
603 size
= ring_size(ctx
->device
, ctx
->reserve_size
);
605 set_sock_prio(sock
, 512);
606 set_packet_loss_discard(sock
);
608 setup_tx_ring_layout(sock
, &tx_ring
, size
, ctx
->jumbo_support
);
609 create_tx_ring(sock
, &tx_ring
, ctx
->verbose
);
610 mmap_tx_ring(sock
, &tx_ring
);
611 alloc_tx_ring_frames(&tx_ring
);
612 bind_tx_ring(sock
, &tx_ring
, ifindex
);
614 drop_privileges(ctx
->enforce
, ctx
->uid
, ctx
->gid
);
617 interval
= ctx
->kpull
;
620 if (ctx
->num
== 0 && orig_num
> 0)
623 set_itimer_interval_value(&itimer
, 0, interval
);
624 setitimer(ITIMER_REAL
, &itimer
, NULL
);
626 bug_on(gettimeofday(&start
, NULL
));
628 while (likely(sigint
== 0) && likely(num
> 0) && likely(plen
> 0)) {
629 while (user_may_pull_from_tx(tx_ring
.frames
[it
].iov_base
) && likely(num
> 0)) {
630 hdr
= tx_ring
.frames
[it
].iov_base
;
631 out
= ((uint8_t *) hdr
) + TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
633 hdr
->tp_h
.tp_snaplen
= packets
[i
].len
;
634 hdr
->tp_h
.tp_len
= packets
[i
].len
;
636 pktd
= &packet_dyn
[i
];
637 if (pktd
->clen
+ pktd
->rlen
+ pktd
->slen
) {
643 fmemcpy(out
, packets
[i
].payload
, packets
[i
].len
);
645 tx_bytes
+= packets
[i
].len
;
655 kernel_may_pull_from_tx(&hdr
->tp_h
);
658 if (it
>= tx_ring
.layout
.tp_frame_nr
)
664 if (unlikely(sigint
== 1))
669 bug_on(gettimeofday(&end
, NULL
));
670 timersub(&end
, &start
, &diff
);
672 destroy_tx_ring(sock
, &tx_ring
);
674 stats
[cpu
].tx_packets
= tx_packets
;
675 stats
[cpu
].tx_bytes
= tx_bytes
;
676 stats
[cpu
].tv_sec
= diff
.tv_sec
;
677 stats
[cpu
].tv_usec
= diff
.tv_usec
;
679 stats
[cpu
].state
|= CPU_STATS_STATE_RES
;
682 static inline void __set_state(int cpu
, sig_atomic_t s
)
684 stats
[cpu
].state
= s
;
687 static inline sig_atomic_t __get_state(int cpu
)
689 return stats
[cpu
].state
;
692 static unsigned long __wait_and_sum_others(struct ctx
*ctx
, int cpu
)
697 for (i
= 0, total
= plen
; i
< ctx
->cpus
; i
++) {
701 while ((__get_state(i
) &
702 (CPU_STATS_STATE_CFG
|
703 CPU_STATS_STATE_RES
)) == 0 &&
707 total
+= stats
[i
].cf_packets
;
713 static void __correct_global_delta(struct ctx
*ctx
, int cpu
, unsigned long orig
)
717 long long delta_correction
= 0;
719 for (i
= 0, total
= ctx
->num
; i
< ctx
->cpus
; i
++) {
723 while ((__get_state(i
) &
724 (CPU_STATS_STATE_CHK
|
725 CPU_STATS_STATE_RES
)) == 0 &&
729 total
+= stats
[i
].cd_packets
;
733 delta_correction
= -1 * ((long long) total
- orig
);
735 delta_correction
= +1 * ((long long) orig
- total
);
737 for (cpu_sel
= -1, i
= 0; i
< ctx
->cpus
; i
++) {
738 if (stats
[i
].cd_packets
> 0) {
739 if ((long long) stats
[i
].cd_packets
+
740 delta_correction
> 0) {
748 ctx
->num
+= delta_correction
;
751 static void __set_state_cf(int cpu
, unsigned long p
, unsigned long b
,
754 stats
[cpu
].cf_packets
= p
;
755 stats
[cpu
].cf_bytes
= b
;
756 stats
[cpu
].state
= s
;
759 static void __set_state_cd(int cpu
, unsigned long p
, sig_atomic_t s
)
761 stats
[cpu
].cd_packets
= p
;
762 stats
[cpu
].state
= s
;
765 static int xmit_packet_precheck(struct ctx
*ctx
, int cpu
)
768 unsigned long plen_total
, orig
= ctx
->num
;
769 size_t mtu
, total_len
= 0;
771 bug_on(plen
!= dlen
);
773 for (i
= 0; i
< plen
; ++i
)
774 total_len
+= packets
[i
].len
;
776 __set_state_cf(cpu
, plen
, total_len
, CPU_STATS_STATE_CFG
);
777 plen_total
= __wait_and_sum_others(ctx
, cpu
);
780 ctx
->num
= (unsigned long) nearbyint((1.0 * plen
/ plen_total
) * orig
);
782 __set_state_cd(cpu
, ctx
->num
, CPU_STATS_STATE_CHK
|
783 CPU_STATS_STATE_CFG
);
784 __correct_global_delta(ctx
, cpu
, orig
);
788 __set_state(cpu
, CPU_STATS_STATE_RES
);
792 for (mtu
= device_mtu(ctx
->device
), i
= 0; i
< plen
; ++i
) {
793 if (packets
[i
].len
> mtu
+ 14)
794 panic("Device MTU < than packet%d's size!\n", i
);
795 if (packets
[i
].len
<= 14)
796 panic("Packet%d's size too short!\n", i
);
802 static void main_loop(struct ctx
*ctx
, char *confname
, bool slow
,
803 int cpu
, bool invoke_cpp
, unsigned long orig_num
)
805 compile_packets(confname
, ctx
->verbose
, cpu
, invoke_cpp
);
806 if (xmit_packet_precheck(ctx
, cpu
) < 0)
811 size_t total_len
= 0, total_pkts
= 0;
813 for (i
= 0; i
< ctx
->cpus
; ++i
) {
814 total_len
+= stats
[i
].cf_bytes
;
815 total_pkts
+= stats
[i
].cf_packets
;
818 printf("%6zu packets to schedule\n", total_pkts
);
819 printf("%6zu bytes in total\n", total_len
);
820 printf("Running! Hang up with ^C!\n\n");
827 xmit_slowpath_or_die(ctx
, cpu
, orig_num
);
829 xmit_fastpath_or_die(ctx
, cpu
, orig_num
);
836 static unsigned int generate_srand_seed(void)
841 fd
= open("/dev/urandom", O_RDONLY
);
845 read_or_die(fd
, &seed
, sizeof(seed
));
851 int main(int argc
, char **argv
)
853 bool slow
= false, invoke_cpp
= false, reseed
= true;
854 int c
, opt_index
, i
, j
, vals
[4] = {0}, irq
;
855 char *confname
= NULL
, *ptr
;
856 unsigned long cpus_tmp
, orig_num
= 0;
857 unsigned long long tx_packets
, tx_bytes
;
860 fmemset(&ctx
, 0, sizeof(ctx
));
861 ctx
.cpus
= get_number_cpus_online();
865 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
866 &opt_index
)) != EOF
) {
884 cpus_tmp
= strtoul(optarg
, NULL
, 0);
885 if (cpus_tmp
> 0 && cpus_tmp
< ctx
.cpus
)
890 ctx
.device
= xstrndup(optarg
, IFNAMSIZ
);
898 ctx
.smoke_test
= true;
899 ctx
.rhost
= xstrdup(optarg
);
905 ctx
.jumbo_support
= true;
909 confname
= xstrdup(optarg
);
910 if (!strncmp("-", confname
, strlen("-")))
914 ctx
.uid
= strtoul(optarg
, NULL
, 0);
918 ctx
.gid
= strtoul(optarg
, NULL
, 0);
922 ctx
.kpull
= strtoul(optarg
, NULL
, 0);
925 seed
= strtoul(optarg
, NULL
, 0);
929 orig_num
= strtoul(optarg
, NULL
, 0);
934 ctx
.gap
= strtoul(optarg
, NULL
, 0);
936 /* Fall back to single core to not
937 * mess up correct timing. We are slow
944 ctx
.reserve_size
= 0;
946 for (j
= i
= strlen(optarg
); i
> 0; --i
) {
947 if (!isdigit(optarg
[j
- i
]))
952 if (!strncmp(ptr
, "KiB", strlen("KiB")))
953 ctx
.reserve_size
= 1 << 10;
954 else if (!strncmp(ptr
, "MiB", strlen("MiB")))
955 ctx
.reserve_size
= 1 << 20;
956 else if (!strncmp(ptr
, "GiB", strlen("GiB")))
957 ctx
.reserve_size
= 1 << 30;
959 panic("Syntax error in ring size param!\n");
962 ctx
.reserve_size
*= strtol(optarg
, NULL
, 0);
979 panic("Option -%c requires an argument!\n",
983 printf("Unknown option character `0x%X\'!\n", optopt
);
993 if (ctx
.device
== NULL
)
994 panic("No networking device given!\n");
995 if (confname
== NULL
)
996 panic("No configuration file given!\n");
997 if (device_mtu(ctx
.device
) == 0)
998 panic("This is no networking device!\n");
999 if (!ctx
.rfraw
&& device_up_and_running(ctx
.device
) == 0)
1000 panic("Networking device not running!\n");
1002 register_signal(SIGINT
, signal_handler
);
1003 register_signal(SIGHUP
, signal_handler
);
1004 register_signal_f(SIGALRM
, timer_elapsed
, SA_SIGINFO
);
1006 set_system_socket_memory(vals
, array_size(vals
));
1010 ctx
.device_trans
= xstrdup(ctx
.device
);
1013 enter_rfmon_mac80211(ctx
.device_trans
, &ctx
.device
);
1017 irq
= device_irq_number(ctx
.device
);
1018 device_set_irq_affinity_list(irq
, 0, ctx
.cpus
- 1);
1020 stats
= setup_shared_var(ctx
.cpus
);
1022 for (i
= 0; i
< ctx
.cpus
; i
++) {
1028 seed
= generate_srand_seed();
1032 main_loop(&ctx
, confname
, slow
, i
, invoke_cpp
, orig_num
);
1036 panic("Cannot fork processes!\n");
1040 for (i
= 0; i
< ctx
.cpus
; i
++) {
1044 if (WEXITSTATUS(status
) == EXIT_FAILURE
)
1049 leave_rfmon_mac80211(ctx
.device_trans
, ctx
.device
);
1051 reset_system_socket_memory(vals
, array_size(vals
));
1053 for (i
= 0, tx_packets
= tx_bytes
= 0; i
< ctx
.cpus
; i
++) {
1054 while ((__get_state(i
) & CPU_STATS_STATE_RES
) == 0)
1057 tx_packets
+= stats
[i
].tx_packets
;
1058 tx_bytes
+= stats
[i
].tx_bytes
;
1063 printf("\r%12llu packets outgoing\n", tx_packets
);
1064 printf("\r%12llu bytes outgoing\n", tx_bytes
);
1065 for (i
= 0; i
< ctx
.cpus
; i
++) {
1066 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1067 stats
[i
].tv_sec
, stats
[i
].tv_usec
, i
,
1068 stats
[i
].tx_packets
);
1073 destroy_shared_var(stats
, ctx
.cpus
);
1076 free(ctx
.device_trans
);