trafgen: inherit netsniff-ng's -H -Q options
[netsniff-ng.git] / trafgen.c
blobb27cb70f7eacf254cc35d04325e5462ae80776e1
1 /*
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.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <getopt.h>
11 #include <ctype.h>
12 #include <stdbool.h>
13 #include <sched.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
16 #include <sys/fsuid.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <sys/wait.h>
20 #include <sys/mman.h>
21 #include <net/ethernet.h>
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <linux/icmp.h>
25 #include <linux/if.h>
26 #include <arpa/inet.h>
27 #include <signal.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <poll.h>
33 #include <netdb.h>
34 #include <math.h>
35 #include <unistd.h>
37 #include "xmalloc.h"
38 #include "die.h"
39 #include "str.h"
40 #include "sig.h"
41 #include "sock.h"
42 #include "cpus.h"
43 #include "lockme.h"
44 #include "privs.h"
45 #include "proc.h"
46 #include "mac80211.h"
47 #include "ioops.h"
48 #include "irq.h"
49 #include "config.h"
50 #include "built_in.h"
51 #include "trafgen_conf.h"
52 #include "tprintf.h"
53 #include "timer.h"
54 #include "ring_tx.h"
55 #include "csum.h"
57 struct ctx {
58 bool rand, rfraw, jumbo_support, verbose, smoke_test, enforce;
59 unsigned long kpull, num, reserve_size;
60 unsigned int cpus;
61 uid_t uid; gid_t gid;
62 char *device, *device_trans, *rhost;
63 struct timespec gap;
64 struct sockaddr_in dest;
67 struct cpu_stats {
68 unsigned long tv_sec, tv_usec;
69 unsigned long long tx_packets, tx_bytes;
70 unsigned long long cf_packets, cf_bytes;
71 unsigned long long cd_packets;
72 sig_atomic_t state;
75 static sig_atomic_t sigint = 0;
77 struct packet *packets = NULL;
78 size_t plen = 0;
80 struct packet_dyn *packet_dyn = NULL;
81 size_t dlen = 0;
83 static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQ";
84 static const struct option long_options[] = {
85 {"dev", required_argument, NULL, 'd'},
86 {"out", required_argument, NULL, 'o'},
87 {"in", required_argument, NULL, 'i'},
88 {"conf", required_argument, NULL, 'c'},
89 {"num", required_argument, NULL, 'n'},
90 {"gap", required_argument, NULL, 't'},
91 {"cpus", required_argument, NULL, 'P'},
92 {"ring-size", required_argument, NULL, 'S'},
93 {"kernel-pull", required_argument, NULL, 'k'},
94 {"smoke-test", required_argument, NULL, 's'},
95 {"seed", required_argument, NULL, 'E'},
96 {"user", required_argument, NULL, 'u'},
97 {"group", required_argument, NULL, 'g'},
98 {"prio-high", no_argument, NULL, 'H'},
99 {"notouch-irq", no_argument, NULL, 'Q'},
100 {"jumbo-support", no_argument, NULL, 'J'},
101 {"no-cpu-stats", no_argument, NULL, 'C'},
102 {"cpp", no_argument, NULL, 'p'},
103 {"rfraw", no_argument, NULL, 'R'},
104 {"rand", no_argument, NULL, 'r'},
105 {"verbose", no_argument, NULL, 'V'},
106 {"version", no_argument, NULL, 'v'},
107 {"example", no_argument, NULL, 'e'},
108 {"help", no_argument, NULL, 'h'},
109 {NULL, 0, NULL, 0}
112 static int sock;
113 static struct itimerval itimer;
114 static unsigned long interval = TX_KERNEL_PULL_INT;
115 static struct cpu_stats *stats;
116 static unsigned int seed;
118 #define CPU_STATS_STATE_CFG 1
119 #define CPU_STATS_STATE_CHK 2
120 #define CPU_STATS_STATE_RES 4
122 #ifndef ICMP_FILTER
123 # define ICMP_FILTER 1
125 struct icmp_filter {
126 __u32 data;
128 #endif
130 static void signal_handler(int number)
132 switch (number) {
133 case SIGINT:
134 sigint = 1;
135 case SIGHUP:
136 default:
137 break;
141 static void timer_elapsed(int unused __maybe_unused)
143 int ret = pull_and_flush_tx_ring(sock);
144 if (unlikely(ret < 0)) {
145 /* We could hit EBADF if the socket has been closed before
146 * the timer was triggered.
148 if (errno != EBADF && errno != ENOBUFS)
149 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
152 set_itimer_interval_value(&itimer, 0, interval);
153 setitimer(ITIMER_REAL, &itimer, NULL);
156 static void timer_purge(void)
158 int ret;
160 ret = pull_and_flush_tx_ring_wait(sock);
161 if (unlikely(ret < 0)) {
162 /* We could hit EBADF if the socket has been closed before
163 * the timer was triggered.
165 if (errno != EBADF && errno != ENOBUFS)
166 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
169 set_itimer_interval_value(&itimer, 0, 0);
170 setitimer(ITIMER_REAL, &itimer, NULL);
173 static void __noreturn help(void)
175 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
176 puts("http://www.netsniff-ng.org\n\n"
177 "Usage: trafgen [options]\n"
178 "Options:\n"
179 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
180 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
181 " -p|--cpp Run packet config through C preprocessor\n"
182 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
183 " -R|--rfraw Inject raw 802.11 frames\n"
184 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
185 " -n|--num <uint> Number of packets until exit (def: 0)\n"
186 " -r|--rand Randomize packet selection (def: round robin)\n"
187 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
188 " -t|--gap <time> Set approx. interpacket gap (s/ms/us/ns, def: us)\n"
189 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
190 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
191 " -E|--seed <uint> Manually set srand(3) seed\n"
192 " -u|--user <userid> Drop privileges and change to userid\n"
193 " -g|--group <groupid> Drop privileges and change to groupid\n"
194 " -H|--prio-high Make this high priority process\n"
195 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
196 " -V|--verbose Be more verbose\n"
197 " -C|--no-cpu-stats Do not print CPU time statistics on exit\n"
198 " -v|--version Show version and exit\n"
199 " -e|--example Show built-in packet config example\n"
200 " -h|--help Guess what?!\n\n"
201 "Examples:\n"
202 " See trafgen.txf for configuration file examples.\n"
203 " trafgen --dev eth0 --conf trafgen.cfg\n"
204 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
205 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
206 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
207 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
208 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
209 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
210 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
211 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
212 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
213 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
214 "Note:\n"
215 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
216 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
217 " we assume the kernel crashed, thus we print the packet and quit.\n"
218 " In case you find a ping-of-death, please mention trafgen in your\n"
219 " commit message of the fix!\n\n"
220 " For introducing bit errors, delays with random variation and more,\n"
221 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
222 " For generating different package distributions, you can use scripting\n"
223 " to generate a trafgen config file with packet ratios as:\n\n"
224 " IMIX 64:7, 570:4, 1518:1\n"
225 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
226 " Cisco 64:7, 594:4, 1518:1\n"
227 " RPR Trimodal 64:60, 512:20, 1518:20\n"
228 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\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");
235 die();
238 static void __noreturn example(void)
240 const char *e =
241 "/* Note: dynamic elements make trafgen slower! */\n"
242 "#include <stddef.h>\n\n"
243 "{\n"
244 " /* MAC Destination */\n"
245 " fill(0xff, ETH_ALEN),\n"
246 " /* MAC Source */\n"
247 " 0x00, 0x02, 0xb3, drnd(3),\n"
248 " /* IPv4 Protocol */\n"
249 " c16(ETH_P_IP),\n"
250 " /* IPv4 Version, IHL, TOS */\n"
251 " 0b01000101, 0,\n"
252 " /* IPv4 Total Len */\n"
253 " c16(59),\n"
254 " /* IPv4 Ident */\n"
255 " drnd(2),\n"
256 " /* IPv4 Flags, Frag Off */\n"
257 " 0b01000000, 0,\n"
258 " /* IPv4 TTL */\n"
259 " 64,\n"
260 " /* Proto TCP */\n"
261 " 0x06,\n"
262 " /* IPv4 Checksum (IP header from, to) */\n"
263 " csumip(14, 33),\n"
264 " /* Source IP */\n"
265 " drnd(4),\n"
266 " /* Dest IP */\n"
267 " drnd(4),\n"
268 " /* TCP Source Port */\n"
269 " drnd(2),\n"
270 " /* TCP Dest Port */\n"
271 " c16(80),\n"
272 " /* TCP Sequence Number */\n"
273 " drnd(4),\n"
274 " /* TCP Ackn. Number */\n"
275 " c32(0),\n"
276 " /* TCP Header length + TCP SYN/ECN Flag */\n"
277 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
278 " /* Window Size */\n"
279 " c16(16),\n"
280 " /* TCP Checksum (offset IP, offset TCP) */\n"
281 " csumtcp(14, 34),\n"
282 " /* TCP Options */\n"
283 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
284 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
285 " /* Data blob */\n"
286 " \"gotcha!\",\n"
287 "}";
288 puts(e);
289 die();
292 static void __noreturn version(void)
294 printf("\ntrafgen %s, Git id: %s\n", VERSION_LONG, GITVERSION);
295 puts("multithreaded zero-copy network packet generator\n"
296 "http://www.netsniff-ng.org\n\n"
297 "Please report bugs to <bugs@netsniff-ng.org>\n"
298 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
299 "Swiss federal institute of technology (ETH Zurich)\n"
300 "License: GNU GPL version 2.0\n"
301 "This is free software: you are free to change and redistribute it.\n"
302 "There is NO WARRANTY, to the extent permitted by law.\n");
303 die();
306 static void apply_counter(int id)
308 size_t j, counter_max = packet_dyn[id].clen;
310 for (j = 0; j < counter_max; ++j) {
311 uint8_t val;
312 struct counter *counter = &packet_dyn[id].cnt[j];
314 val = counter->val - counter->min;
316 switch (counter->type) {
317 case TYPE_INC:
318 val = (val + counter->inc) % (counter->max - counter->min + 1);
319 break;
320 case TYPE_DEC:
321 val = (val - counter->inc) % (counter->min - counter->max + 1);
322 break;
323 default:
324 bug();
327 counter->val = val + counter->min;
328 packets[id].payload[counter->off] = val;
332 static void apply_randomizer(int id)
334 size_t j, rand_max = packet_dyn[id].rlen;
336 for (j = 0; j < rand_max; ++j) {
337 uint8_t val = (uint8_t) rand();
338 struct randomizer *randomizer = &packet_dyn[id].rnd[j];
340 packets[id].payload[randomizer->off] = val;
344 static void apply_csum16(int id)
346 size_t j, csum_max = packet_dyn[id].slen;
348 for (j = 0; j < csum_max; ++j) {
349 uint16_t sum = 0;
350 struct csum16 *csum = &packet_dyn[id].csum[j];
352 fmemset(&packets[id].payload[csum->off], 0, sizeof(sum));
353 if (unlikely((size_t) csum->to >= packets[id].len))
354 csum->to = packets[id].len - 1;
356 switch (csum->which) {
357 case CSUM_IP:
358 sum = calc_csum(packets[id].payload + csum->from,
359 csum->to - csum->from + 1, 0);
360 break;
361 case CSUM_UDP:
362 sum = p4_csum((void *) packets[id].payload + csum->from,
363 packets[id].payload + csum->to,
364 (packets[id].len - csum->to),
365 IPPROTO_UDP);
366 break;
367 case CSUM_TCP:
368 sum = p4_csum((void *) packets[id].payload + csum->from,
369 packets[id].payload + csum->to,
370 (packets[id].len - csum->to),
371 IPPROTO_TCP);
372 break;
373 default:
374 bug();
375 break;
378 fmemcpy(&packets[id].payload[csum->off], &sum, sizeof(sum));
382 static struct cpu_stats *setup_shared_var(unsigned long cpus)
384 int fd;
385 size_t len = cpus * sizeof(struct cpu_stats);
386 char zbuff[len], file[256];
387 struct cpu_stats *buff;
389 fmemset(zbuff, 0, len);
390 slprintf(file, sizeof(file), ".tmp_mmap.%u", (unsigned int) rand());
392 fd = creat(file, S_IRUSR | S_IWUSR);
393 bug_on(fd < 0);
394 close(fd);
396 fd = open_or_die_m(file, O_RDWR | O_CREAT | O_TRUNC,
397 S_IRUSR | S_IWUSR);
398 write_or_die(fd, zbuff, len);
400 buff = mmap(NULL, len, PROT_READ | PROT_WRITE,
401 MAP_SHARED, fd, 0);
402 if (buff == MAP_FAILED)
403 panic("Cannot setup shared variable!\n");
405 close(fd);
406 unlink(file);
408 memset(buff, 0, len);
409 return buff;
412 static void destroy_shared_var(void *buff, unsigned long cpus)
414 munmap(buff, cpus * sizeof(struct cpu_stats));
417 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
419 size_t i;
421 printf("{");
422 for (i = 0; i < len; ++i) {
423 if (i % 15 == 0)
424 printf("\n ");
425 printf("0x%02x, ", payload[i]);
427 printf("\n}\n");
428 fflush(stdout);
431 static int xmit_smoke_setup(struct ctx *ctx)
433 int icmp_sock, ret, ttl = 64;
434 struct icmp_filter filter;
436 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
437 if (icmp_sock < 0)
438 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
440 filter.data = ~(1 << ICMP_ECHOREPLY);
442 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
443 if (ret < 0)
444 panic("Cannot install filter!\n");
446 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
447 if (ret < 0)
448 panic("Cannot set TTL!\n");
450 memset(&ctx->dest, 0, sizeof(ctx->dest));
451 ctx->dest.sin_family = AF_INET;
452 ctx->dest.sin_port = 0;
454 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
455 if (ret < 0)
456 panic("Cannot resolv address!\n");
458 return icmp_sock;
461 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
463 int ret;
464 unsigned int i, j = 0, probes = 100;
465 short ident, cnt = 1, idstore[probes];
466 uint8_t outpack[512], *data;
467 struct icmphdr *icmp;
468 struct iphdr *ip;
469 size_t len = sizeof(*icmp) + 56;
470 struct sockaddr_in from;
471 socklen_t from_len;
472 struct pollfd fds = {
473 .fd = icmp_sock,
474 .events = POLLIN,
477 fmemset(idstore, 0, sizeof(idstore));
478 while (probes-- > 0) {
479 while ((ident = htons((short) rand())) == 0)
480 sleep(0);
481 idstore[j++] = ident;
483 memset(outpack, 0, sizeof(outpack));
484 icmp = (void *) outpack;
485 icmp->type = ICMP_ECHO;
486 icmp->un.echo.id = ident;
487 icmp->un.echo.sequence = htons(cnt++);
489 data = ((uint8_t *) outpack + sizeof(*icmp));
490 for (i = 0; i < 56; ++i)
491 data[i] = (uint8_t) rand();
493 icmp->checksum = csum((unsigned short *) outpack,
494 len / sizeof(unsigned short));
496 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
497 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
498 if (unlikely(ret != (int) len))
499 panic("Cannot send out probe: %s!\n", strerror(errno));
501 ret = poll(&fds, 1, 50);
502 if (ret < 0)
503 panic("Poll failed!\n");
505 if (fds.revents & POLLIN) {
506 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
507 (struct sockaddr *) &from, &from_len);
508 if (unlikely(ret <= 0))
509 panic("Probe receive failed!\n");
510 if (unlikely(from_len != sizeof(ctx->dest)))
511 continue;
512 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
513 continue;
514 if (unlikely((size_t) ret < sizeof(*ip) + sizeof(*icmp)))
515 continue;
516 ip = (void *) outpack;
517 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > (size_t) ret))
518 continue;
519 icmp = (void *) outpack + ip->ihl * 4;
520 for (i = 0; i < array_size(idstore); ++i) {
521 if (unlikely(icmp->un.echo.id != idstore[i]))
522 continue;
523 return 0;
528 return -1;
531 static void xmit_slowpath_or_die(struct ctx *ctx, int cpu, unsigned long orig_num)
533 int ret, icmp_sock = -1;
534 unsigned long num = 1, i = 0;
535 struct timeval start, end, diff;
536 unsigned long long tx_bytes = 0, tx_packets = 0;
537 struct packet_dyn *pktd;
538 struct sockaddr_ll saddr = {
539 .sll_family = PF_PACKET,
540 .sll_halen = ETH_ALEN,
541 .sll_ifindex = device_ifindex(ctx->device),
544 if (ctx->num > 0)
545 num = ctx->num;
546 if (ctx->num == 0 && orig_num > 0)
547 num = 0;
549 if (ctx->smoke_test)
550 icmp_sock = xmit_smoke_setup(ctx);
552 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
554 bug_on(gettimeofday(&start, NULL));
556 while (likely(sigint == 0 && num > 0 && plen > 0)) {
557 pktd = &packet_dyn[i];
558 if (pktd->clen + pktd->rlen + pktd->slen) {
559 apply_counter(i);
560 apply_randomizer(i);
561 apply_csum16(i);
563 retry:
564 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
565 (struct sockaddr *) &saddr, sizeof(saddr));
566 if (unlikely(ret < 0)) {
567 if (errno == ENOBUFS) {
568 sched_yield();
569 goto retry;
571 if (ctx->smoke_test)
572 panic("Sendto error: %s!\n", strerror(errno));
575 tx_bytes += packets[i].len;
576 tx_packets++;
578 if (ctx->smoke_test) {
579 ret = xmit_smoke_probe(icmp_sock, ctx);
580 if (unlikely(ret < 0)) {
581 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
582 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
583 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
584 i, seed);
586 dump_trafgen_snippet(packets[i].payload, packets[i].len);
587 break;
591 if (!ctx->rand) {
592 i++;
593 if (i >= plen)
594 i = 0;
595 } else
596 i = rand() % plen;
598 if (ctx->num > 0)
599 num--;
601 if ((ctx->gap.tv_sec | ctx->gap.tv_nsec) > 0)
602 nanosleep(&ctx->gap, NULL);
605 bug_on(gettimeofday(&end, NULL));
606 timersub(&end, &start, &diff);
608 if (ctx->smoke_test)
609 close(icmp_sock);
611 stats[cpu].tx_packets = tx_packets;
612 stats[cpu].tx_bytes = tx_bytes;
613 stats[cpu].tv_sec = diff.tv_sec;
614 stats[cpu].tv_usec = diff.tv_usec;
616 stats[cpu].state |= CPU_STATS_STATE_RES;
619 static void xmit_fastpath_or_die(struct ctx *ctx, int cpu, unsigned long orig_num)
621 int ifindex = device_ifindex(ctx->device);
622 uint8_t *out = NULL;
623 unsigned int it = 0;
624 unsigned long num = 1, i = 0, size;
625 struct ring tx_ring;
626 struct frame_map *hdr;
627 struct timeval start, end, diff;
628 struct packet_dyn *pktd;
629 unsigned long long tx_bytes = 0, tx_packets = 0;
631 fmemset(&tx_ring, 0, sizeof(tx_ring));
633 size = ring_size(ctx->device, ctx->reserve_size);
635 set_sock_prio(sock, 512);
636 set_packet_loss_discard(sock);
638 setup_tx_ring_layout(sock, &tx_ring, size, ctx->jumbo_support);
639 create_tx_ring(sock, &tx_ring, ctx->verbose);
640 mmap_tx_ring(sock, &tx_ring);
641 alloc_tx_ring_frames(sock, &tx_ring);
642 bind_tx_ring(sock, &tx_ring, ifindex);
644 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
646 if (ctx->kpull)
647 interval = ctx->kpull;
648 if (ctx->num > 0)
649 num = ctx->num;
650 if (ctx->num == 0 && orig_num > 0)
651 num = 0;
653 set_itimer_interval_value(&itimer, 0, interval);
654 setitimer(ITIMER_REAL, &itimer, NULL);
656 bug_on(gettimeofday(&start, NULL));
658 while (likely(sigint == 0 && num > 0 && plen > 0)) {
659 if (!user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
660 sched_yield();
661 continue;
664 hdr = tx_ring.frames[it].iov_base;
665 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
667 hdr->tp_h.tp_snaplen = packets[i].len;
668 hdr->tp_h.tp_len = packets[i].len;
670 pktd = &packet_dyn[i];
671 if (pktd->clen + pktd->rlen + pktd->slen) {
672 apply_counter(i);
673 apply_randomizer(i);
674 apply_csum16(i);
677 fmemcpy(out, packets[i].payload, packets[i].len);
679 tx_bytes += packets[i].len;
680 tx_packets++;
682 if (!ctx->rand) {
683 i++;
684 if (i >= plen)
685 i = 0;
686 } else
687 i = rand() % plen;
689 kernel_may_pull_from_tx(&hdr->tp_h);
691 it++;
692 if (it >= tx_ring.layout.tp_frame_nr)
693 it = 0;
695 if (ctx->num > 0)
696 num--;
699 bug_on(gettimeofday(&end, NULL));
700 timersub(&end, &start, &diff);
702 timer_purge();
704 destroy_tx_ring(sock, &tx_ring);
706 stats[cpu].tx_packets = tx_packets;
707 stats[cpu].tx_bytes = tx_bytes;
708 stats[cpu].tv_sec = diff.tv_sec;
709 stats[cpu].tv_usec = diff.tv_usec;
711 stats[cpu].state |= CPU_STATS_STATE_RES;
714 static inline void __set_state(int cpu, sig_atomic_t s)
716 stats[cpu].state = s;
719 static inline sig_atomic_t __get_state(int cpu)
721 return stats[cpu].state;
724 static unsigned long __wait_and_sum_others(struct ctx *ctx, unsigned int cpu)
726 unsigned int i;
727 unsigned long total;
729 for (i = 0, total = plen; i < ctx->cpus; i++) {
730 if (i == cpu)
731 continue;
733 while ((__get_state(i) &
734 (CPU_STATS_STATE_CFG |
735 CPU_STATS_STATE_RES)) == 0 &&
736 sigint == 0)
737 sched_yield();
739 total += stats[i].cf_packets;
742 return total;
745 static void __correct_global_delta(struct ctx *ctx, unsigned int cpu, unsigned long orig)
747 unsigned int i;
748 unsigned long total;
749 int cpu_sel;
750 long long delta_correction = 0;
752 for (i = 0, total = ctx->num; i < ctx->cpus; i++) {
753 if (i == cpu)
754 continue;
756 while ((__get_state(i) &
757 (CPU_STATS_STATE_CHK |
758 CPU_STATS_STATE_RES)) == 0 &&
759 sigint == 0)
760 sched_yield();
762 total += stats[i].cd_packets;
765 if (total > orig)
766 delta_correction = -1 * ((long long) total - orig);
767 if (total < orig)
768 delta_correction = +1 * ((long long) orig - total);
770 for (cpu_sel = -1, i = 0; i < ctx->cpus; i++) {
771 if (stats[i].cd_packets > 0) {
772 if ((long long) stats[i].cd_packets +
773 delta_correction >= 0) {
774 cpu_sel = i;
775 break;
780 if ((int) cpu == cpu_sel)
781 ctx->num += delta_correction;
784 static void __set_state_cf(int cpu, unsigned long p, unsigned long b,
785 sig_atomic_t s)
787 stats[cpu].cf_packets = p;
788 stats[cpu].cf_bytes = b;
789 stats[cpu].state = s;
792 static void __set_state_cd(int cpu, unsigned long p, sig_atomic_t s)
794 stats[cpu].cd_packets = p;
795 stats[cpu].state = s;
798 static int xmit_packet_precheck(struct ctx *ctx, int cpu)
800 unsigned int i;
801 unsigned long plen_total, orig = ctx->num;
802 size_t mtu, total_len = 0;
804 bug_on(plen != dlen);
806 for (i = 0; i < plen; ++i)
807 total_len += packets[i].len;
809 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
810 plen_total = __wait_and_sum_others(ctx, cpu);
812 if (orig > 0) {
813 ctx->num = (unsigned long) round((1.0 * plen / plen_total) * orig);
815 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
816 CPU_STATS_STATE_CFG);
817 __correct_global_delta(ctx, cpu, orig);
820 if (plen == 0) {
821 __set_state(cpu, CPU_STATS_STATE_RES);
822 return 0;
825 for (mtu = device_mtu(ctx->device), i = 0; i < plen; ++i) {
826 if (packets[i].len > mtu + 14)
827 panic("Device MTU < than packet%d's size!\n", i);
828 if (packets[i].len <= 14)
829 panic("Packet%d's size too short!\n", i);
832 return 0;
835 static void main_loop(struct ctx *ctx, char *confname, bool slow,
836 unsigned int cpu, bool invoke_cpp, unsigned long orig_num)
838 compile_packets(confname, ctx->verbose, cpu, invoke_cpp);
839 if (xmit_packet_precheck(ctx, cpu) < 0)
840 return;
842 if (cpu == 0) {
843 unsigned int i;
844 size_t total_len = 0, total_pkts = 0;
846 for (i = 0; i < ctx->cpus; ++i) {
847 total_len += stats[i].cf_bytes;
848 total_pkts += stats[i].cf_packets;
851 printf("%6zu packets to schedule\n", total_pkts);
852 printf("%6zu bytes in total\n", total_len);
853 printf("Running! Hang up with ^C!\n\n");
854 fflush(stdout);
857 sock = pf_socket();
859 if (slow)
860 xmit_slowpath_or_die(ctx, cpu, orig_num);
861 else
862 xmit_fastpath_or_die(ctx, cpu, orig_num);
864 close(sock);
866 cleanup_packets();
869 static unsigned int generate_srand_seed(void)
871 int fd;
872 unsigned int _seed;
874 fd = open("/dev/urandom", O_RDONLY);
875 if (fd < 0)
876 return time(NULL);
878 read_or_die(fd, &_seed, sizeof(_seed));
880 close(fd);
881 return _seed;
884 int main(int argc, char **argv)
886 bool slow = false, invoke_cpp = false, reseed = true, cpustats = true;
887 bool prio_high = false, set_irq_aff = true;
888 int c, opt_index, vals[4] = {0}, irq;
889 uint64_t gap = 0;
890 unsigned int i, j;
891 char *confname = NULL, *ptr;
892 unsigned long cpus_tmp, orig_num = 0;
893 unsigned long long tx_packets, tx_bytes;
894 struct ctx ctx;
896 fmemset(&ctx, 0, sizeof(ctx));
897 ctx.cpus = get_number_cpus_online();
898 ctx.uid = getuid();
899 ctx.gid = getgid();
901 while ((c = getopt_long(argc, argv, short_options, long_options,
902 &opt_index)) != EOF) {
903 switch (c) {
904 case 'h':
905 help();
906 break;
907 case 'v':
908 version();
909 break;
910 case 'C':
911 cpustats = false;
912 break;
913 case 'e':
914 example();
915 break;
916 case 'p':
917 invoke_cpp = true;
918 break;
919 case 'V':
920 ctx.verbose = true;
921 break;
922 case 'P':
923 cpus_tmp = strtoul(optarg, NULL, 0);
924 if (cpus_tmp > 0 && cpus_tmp < ctx.cpus)
925 ctx.cpus = cpus_tmp;
926 break;
927 case 'd':
928 case 'o':
929 ctx.device = xstrndup(optarg, IFNAMSIZ);
930 break;
931 case 'H':
932 prio_high = true;
933 break;
934 case 'Q':
935 set_irq_aff = false;
936 break;
937 case 'r':
938 ctx.rand = true;
939 break;
940 case 's':
941 slow = true;
942 ctx.cpus = 1;
943 ctx.smoke_test = true;
944 ctx.rhost = xstrdup(optarg);
945 break;
946 case 'R':
947 ctx.rfraw = true;
948 break;
949 case 'J':
950 ctx.jumbo_support = true;
951 break;
952 case 'c':
953 case 'i':
954 confname = xstrdup(optarg);
955 if (!strncmp("-", confname, strlen("-")))
956 ctx.cpus = 1;
957 break;
958 case 'u':
959 ctx.uid = strtoul(optarg, NULL, 0);
960 ctx.enforce = true;
961 break;
962 case 'g':
963 ctx.gid = strtoul(optarg, NULL, 0);
964 ctx.enforce = true;
965 break;
966 case 'k':
967 ctx.kpull = strtoul(optarg, NULL, 0);
968 break;
969 case 'E':
970 seed = strtoul(optarg, NULL, 0);
971 reseed = false;
972 break;
973 case 'n':
974 orig_num = strtoul(optarg, NULL, 0);
975 ctx.num = orig_num;
976 break;
977 case 't':
978 slow = true;
979 ptr = optarg;
980 gap = strtoul(optarg, NULL, 0);
982 for (j = i = strlen(optarg); i > 0; --i) {
983 if (!isdigit(optarg[j - i]))
984 break;
985 ptr++;
988 if (!strncmp(ptr, "ns", strlen("ns"))) {
989 ctx.gap.tv_sec = gap / 1000000000;
990 ctx.gap.tv_nsec = gap % 1000000000;
991 } else if (*ptr == '\0' || !strncmp(ptr, "us", strlen("us"))) {
992 /* Default to microseconds for backwards
993 * compatibility if no postfix is given.
995 ctx.gap.tv_sec = gap / 1000000;
996 ctx.gap.tv_nsec = (gap % 1000000) * 1000;
997 } else if (!strncmp(ptr, "ms", strlen("ms"))) {
998 ctx.gap.tv_sec = gap / 1000;
999 ctx.gap.tv_nsec = (gap % 1000) * 1000000;
1000 } else if (!strncmp(ptr, "s", strlen("s"))) {
1001 ctx.gap.tv_sec = gap;
1002 ctx.gap.tv_nsec = 0;
1003 } else
1004 panic("Syntax error in time param!\n");
1006 if (gap > 0)
1007 /* Fall back to single core to not mess up
1008 * correct timing. We are slow anyway!
1010 ctx.cpus = 1;
1011 break;
1012 case 'S':
1013 ptr = optarg;
1014 ctx.reserve_size = 0;
1016 for (j = i = strlen(optarg); i > 0; --i) {
1017 if (!isdigit(optarg[j - i]))
1018 break;
1019 ptr++;
1022 if (!strncmp(ptr, "KiB", strlen("KiB")))
1023 ctx.reserve_size = 1 << 10;
1024 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1025 ctx.reserve_size = 1 << 20;
1026 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1027 ctx.reserve_size = 1 << 30;
1028 else
1029 panic("Syntax error in ring size param!\n");
1030 *ptr = 0;
1032 ctx.reserve_size *= strtol(optarg, NULL, 0);
1033 break;
1034 case '?':
1035 switch (optopt) {
1036 case 'd':
1037 case 'c':
1038 case 'n':
1039 case 'S':
1040 case 's':
1041 case 'P':
1042 case 'o':
1043 case 'E':
1044 case 'i':
1045 case 'k':
1046 case 'u':
1047 case 'g':
1048 case 't':
1049 panic("Option -%c requires an argument!\n",
1050 optopt);
1051 default:
1052 if (isprint(optopt))
1053 printf("Unknown option character `0x%X\'!\n", optopt);
1054 die();
1056 default:
1057 break;
1061 if (argc < 5)
1062 help();
1063 if (ctx.device == NULL)
1064 panic("No networking device given!\n");
1065 if (confname == NULL)
1066 panic("No configuration file given!\n");
1067 if (device_mtu(ctx.device) == 0)
1068 panic("This is no networking device!\n");
1070 register_signal(SIGINT, signal_handler);
1071 register_signal(SIGHUP, signal_handler);
1072 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1074 if (prio_high) {
1075 set_proc_prio(-20);
1076 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1079 set_system_socket_memory(vals, array_size(vals));
1080 xlockme();
1082 if (ctx.rfraw) {
1083 ctx.device_trans = xstrdup(ctx.device);
1084 xfree(ctx.device);
1086 enter_rfmon_mac80211(ctx.device_trans, &ctx.device);
1087 sleep(0);
1090 irq = device_irq_number(ctx.device);
1091 if (set_irq_aff)
1092 device_set_irq_affinity_list(irq, 0, ctx.cpus - 1);
1094 stats = setup_shared_var(ctx.cpus);
1096 for (i = 0; i < ctx.cpus; i++) {
1097 pid_t pid = fork();
1099 switch (pid) {
1100 case 0:
1101 if (reseed)
1102 seed = generate_srand_seed();
1103 srand(seed);
1105 cpu_affinity(i);
1106 main_loop(&ctx, confname, slow, i, invoke_cpp, orig_num);
1108 goto thread_out;
1109 case -1:
1110 panic("Cannot fork processes!\n");
1114 for (i = 0; i < ctx.cpus; i++) {
1115 int status;
1117 wait(&status);
1118 if (WEXITSTATUS(status) == EXIT_FAILURE)
1119 die();
1122 if (ctx.rfraw)
1123 leave_rfmon_mac80211(ctx.device);
1125 reset_system_socket_memory(vals, array_size(vals));
1127 for (i = 0, tx_packets = tx_bytes = 0; i < ctx.cpus; i++) {
1128 while ((__get_state(i) & CPU_STATS_STATE_RES) == 0)
1129 sched_yield();
1131 tx_packets += stats[i].tx_packets;
1132 tx_bytes += stats[i].tx_bytes;
1135 fflush(stdout);
1136 printf("\n");
1137 printf("\r%12llu packets outgoing\n", tx_packets);
1138 printf("\r%12llu bytes outgoing\n", tx_bytes);
1139 for (i = 0; cpustats && i < ctx.cpus; i++) {
1140 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1141 stats[i].tv_sec, stats[i].tv_usec, i,
1142 stats[i].tx_packets);
1145 thread_out:
1146 xunlockme();
1147 destroy_shared_var(stats, ctx.cpus);
1148 if (set_irq_aff)
1149 device_restore_irq_affinity_list();
1151 free(ctx.device);
1152 free(ctx.device_trans);
1153 free(ctx.rhost);
1154 free(confname);
1156 return 0;