netsniff-ng: Rotate pcap files prematurely on SIGHUP
[netsniff-ng.git] / trafgen.c
blobdeef1f2218d626e94beb6d69be413b189080fe1f
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/prctl.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/wait.h>
21 #include <sys/mman.h>
22 #include <net/ethernet.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <linux/icmp.h>
26 #include <linux/if.h>
27 #include <arpa/inet.h>
28 #include <signal.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <poll.h>
34 #include <netdb.h>
35 #include <math.h>
36 #include <unistd.h>
38 #include "xmalloc.h"
39 #include "die.h"
40 #include "str.h"
41 #include "sig.h"
42 #include "sock.h"
43 #include "cpus.h"
44 #include "lockme.h"
45 #include "privs.h"
46 #include "proc.h"
47 #include "mac80211.h"
48 #include "ioops.h"
49 #include "irq.h"
50 #include "config.h"
51 #include "built_in.h"
52 #include "trafgen_conf.h"
53 #include "tprintf.h"
54 #include "timer.h"
55 #include "ring_tx.h"
56 #include "csum.h"
58 struct ctx {
59 bool rand, rfraw, jumbo_support, verbose, smoke_test, enforce, qdisc_path;
60 size_t reserve_size;
61 unsigned long num;
62 unsigned int cpus;
63 uid_t uid; gid_t gid;
64 char *device, *device_trans, *rhost;
65 struct timespec gap;
66 struct sockaddr_in dest;
69 struct cpu_stats {
70 unsigned long tv_sec, tv_usec;
71 unsigned long long tx_packets, tx_bytes;
72 unsigned long long cf_packets, cf_bytes;
73 unsigned long long cd_packets;
74 sig_atomic_t state;
77 static sig_atomic_t sigint = 0;
79 struct packet *packets = NULL;
80 size_t plen = 0;
82 struct packet_dyn *packet_dyn = NULL;
83 size_t dlen = 0;
85 static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQq";
86 static const struct option long_options[] = {
87 {"dev", required_argument, NULL, 'd'},
88 {"out", required_argument, NULL, 'o'},
89 {"in", required_argument, NULL, 'i'},
90 {"conf", required_argument, NULL, 'c'},
91 {"num", required_argument, NULL, 'n'},
92 {"gap", required_argument, NULL, 't'},
93 {"cpus", required_argument, NULL, 'P'},
94 {"ring-size", required_argument, NULL, 'S'},
95 {"kernel-pull", required_argument, NULL, 'k'},
96 {"smoke-test", required_argument, NULL, 's'},
97 {"seed", required_argument, NULL, 'E'},
98 {"user", required_argument, NULL, 'u'},
99 {"group", required_argument, NULL, 'g'},
100 {"prio-high", no_argument, NULL, 'H'},
101 {"notouch-irq", no_argument, NULL, 'Q'},
102 {"no-sock-mem", no_argument, NULL, 'A'},
103 {"qdisc-path", no_argument, NULL, 'q'},
104 {"jumbo-support", no_argument, NULL, 'J'},
105 {"no-cpu-stats", no_argument, NULL, 'C'},
106 {"cpp", no_argument, NULL, 'p'},
107 {"rfraw", no_argument, NULL, 'R'},
108 {"rand", no_argument, NULL, 'r'},
109 {"verbose", no_argument, NULL, 'V'},
110 {"version", no_argument, NULL, 'v'},
111 {"example", no_argument, NULL, 'e'},
112 {"help", no_argument, NULL, 'h'},
113 {NULL, 0, NULL, 0}
116 static const char *copyright = "Please report bugs to <bugs@netsniff-ng.org>\n"
117 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
118 "Swiss federal institute of technology (ETH Zurich)\n"
119 "License: GNU GPL version 2.0\n"
120 "This is free software: you are free to change and redistribute it.\n"
121 "There is NO WARRANTY, to the extent permitted by law.";
123 static int sock;
124 static struct cpu_stats *stats;
125 static unsigned int seed;
127 #define CPU_STATS_STATE_CFG 1
128 #define CPU_STATS_STATE_CHK 2
129 #define CPU_STATS_STATE_RES 4
131 #ifndef ICMP_FILTER
132 # define ICMP_FILTER 1
134 struct icmp_filter {
135 __u32 data;
137 #endif
139 #define SMOKE_N_PROBES 100
141 static void signal_handler(int number)
143 switch (number) {
144 case SIGINT:
145 case SIGQUIT:
146 case SIGTERM:
147 sigint = 1;
148 case SIGHUP:
149 default:
150 break;
154 static void __noreturn help(void)
156 printf("trafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
157 puts("http://www.netsniff-ng.org\n\n"
158 "Usage: trafgen [options]\n"
159 "Options:\n"
160 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
161 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
162 " -p|--cpp Run packet config through C preprocessor\n"
163 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
164 " -R|--rfraw Inject raw 802.11 frames\n"
165 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
166 " -n|--num <uint> Number of packets until exit (def: 0)\n"
167 " -r|--rand Randomize packet selection (def: round robin)\n"
168 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
169 " -t|--gap <time> Set approx. interpacket gap (s/ms/us/ns, def: us)\n"
170 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
171 " -E|--seed <uint> Manually set srand(3) seed\n"
172 " -u|--user <userid> Drop privileges and change to userid\n"
173 " -g|--group <groupid> Drop privileges and change to groupid\n"
174 " -H|--prio-high Make this high priority process\n"
175 " -A|--no-sock-mem Don't tune core socket memory\n"
176 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
177 " -q|--qdisc-path Enabled qdisc kernel path (default off since 3.14)\n"
178 " -V|--verbose Be more verbose\n"
179 " -C|--no-cpu-stats Do not print CPU time statistics on exit\n"
180 " -v|--version Show version and exit\n"
181 " -e|--example Show built-in packet config example\n"
182 " -h|--help Guess what?!\n\n"
183 "Examples:\n"
184 " trafgen --dev eth0 --conf trafgen.cfg\n"
185 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
186 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
187 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
188 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
189 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
190 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
191 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
192 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
193 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
194 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
195 "Generate config files from existing pcap using netsniff-ng:\n"
196 " netsniff-ng --in dump.pcap --out dump.cfg\n"
197 "Note:\n"
198 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
199 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
200 " we assume the kernel crashed, thus we print the packet and quit.\n"
201 " In case you find a ping-of-death, please mention trafgen in your\n"
202 " commit message of the fix!\n\n"
203 " For introducing bit errors, delays with random variation and more,\n"
204 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
205 " For generating different package distributions, you can use scripting\n"
206 " to generate a trafgen config file with packet ratios as:\n\n"
207 " IMIX 64:7, 570:4, 1518:1\n"
208 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
209 " Cisco 64:7, 594:4, 1518:1\n"
210 " RPR Trimodal 64:60, 512:20, 1518:20\n"
211 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n");
212 puts(copyright);
213 die();
216 static void __noreturn example(void)
218 const char *e =
219 "/* Note: dynamic elements make trafgen slower! */\n"
220 "#include <stddef.h>\n\n"
221 "{\n"
222 " /* MAC Destination */\n"
223 " fill(0xff, ETH_ALEN),\n"
224 " /* MAC Source */\n"
225 " 0x00, 0x02, 0xb3, drnd(3),\n"
226 " /* IPv4 Protocol */\n"
227 " c16(ETH_P_IP),\n"
228 " /* IPv4 Version, IHL, TOS */\n"
229 " 0b01000101, 0,\n"
230 " /* IPv4 Total Len */\n"
231 " c16(59),\n"
232 " /* IPv4 Ident */\n"
233 " drnd(2),\n"
234 " /* IPv4 Flags, Frag Off */\n"
235 " 0b01000000, 0,\n"
236 " /* IPv4 TTL */\n"
237 " 64,\n"
238 " /* Proto TCP */\n"
239 " 0x06,\n"
240 " /* IPv4 Checksum (IP header from, to) */\n"
241 " csumip(14, 33),\n"
242 " /* Source IP */\n"
243 " drnd(4),\n"
244 " /* Dest IP */\n"
245 " drnd(4),\n"
246 " /* TCP Source Port */\n"
247 " drnd(2),\n"
248 " /* TCP Dest Port */\n"
249 " c16(80),\n"
250 " /* TCP Sequence Number */\n"
251 " drnd(4),\n"
252 " /* TCP Ackn. Number */\n"
253 " c32(0),\n"
254 " /* TCP Header length + TCP SYN/ECN Flag */\n"
255 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
256 " /* Window Size */\n"
257 " c16(16),\n"
258 " /* TCP Checksum (offset IP, offset TCP) */\n"
259 " csumtcp(14, 34),\n"
260 " /* TCP Options */\n"
261 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
262 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
263 " /* Data blob */\n"
264 " \"gotcha!\",\n"
265 "}";
266 puts(e);
267 die();
270 static void __noreturn version(void)
272 printf("trafgen %s, Git id: %s\n", VERSION_LONG, GITVERSION);
273 puts("multithreaded zero-copy network packet generator\n"
274 "http://www.netsniff-ng.org\n");
275 puts(copyright);
276 die();
279 static void apply_counter(int id)
281 size_t j, counter_max = packet_dyn[id].clen;
283 for (j = 0; j < counter_max; ++j) {
284 uint8_t val;
285 struct counter *counter = &packet_dyn[id].cnt[j];
287 val = counter->val - counter->min;
289 switch (counter->type) {
290 case TYPE_INC:
291 val = (val + counter->inc) % (counter->max - counter->min + 1);
292 break;
293 case TYPE_DEC:
294 val = (val - counter->inc) % (counter->min - counter->max + 1);
295 break;
296 default:
297 bug();
300 counter->val = val + counter->min;
301 packets[id].payload[counter->off] = val;
305 static void apply_randomizer(int id)
307 size_t j, rand_max = packet_dyn[id].rlen;
309 for (j = 0; j < rand_max; ++j) {
310 uint8_t val = (uint8_t) rand();
311 struct randomizer *randomizer = &packet_dyn[id].rnd[j];
313 packets[id].payload[randomizer->off] = val;
317 static void apply_csum16(int id)
319 size_t j, csum_max = packet_dyn[id].slen;
321 for (j = 0; j < csum_max; ++j) {
322 uint16_t sum = 0;
323 struct csum16 *csum = &packet_dyn[id].csum[j];
325 fmemset(&packets[id].payload[csum->off], 0, sizeof(sum));
326 if (unlikely((size_t) csum->to >= packets[id].len))
327 csum->to = packets[id].len - 1;
329 switch (csum->which) {
330 case CSUM_IP:
331 sum = calc_csum(packets[id].payload + csum->from,
332 csum->to - csum->from + 1, 0);
333 break;
334 case CSUM_UDP:
335 sum = p4_csum((void *) packets[id].payload + csum->from,
336 packets[id].payload + csum->to,
337 (packets[id].len - csum->to),
338 IPPROTO_UDP);
339 break;
340 case CSUM_TCP:
341 sum = p4_csum((void *) packets[id].payload + csum->from,
342 packets[id].payload + csum->to,
343 (packets[id].len - csum->to),
344 IPPROTO_TCP);
345 break;
346 default:
347 bug();
348 break;
351 fmemcpy(&packets[id].payload[csum->off], &sum, sizeof(sum));
355 static struct cpu_stats *setup_shared_var(unsigned int cpus)
357 int fd;
358 size_t len = cpus * sizeof(struct cpu_stats);
359 char *zbuff, file[256];
360 struct cpu_stats *buff;
362 zbuff = xzmalloc(len);
363 slprintf(file, sizeof(file), ".tmp_mmap.%u", (unsigned int) rand());
365 fd = creat(file, S_IRUSR | S_IWUSR);
366 bug_on(fd < 0);
367 close(fd);
369 fd = open_or_die_m(file, O_RDWR | O_CREAT | O_TRUNC,
370 S_IRUSR | S_IWUSR);
371 write_or_die(fd, zbuff, len);
372 xfree(zbuff);
374 buff = mmap(NULL, len, PROT_READ | PROT_WRITE,
375 MAP_SHARED, fd, 0);
376 if (buff == MAP_FAILED)
377 panic("Cannot setup shared variable!\n");
379 close(fd);
380 unlink(file);
382 memset(buff, 0, len);
383 return buff;
386 static void destroy_shared_var(void *buff, unsigned int cpus)
388 munmap(buff, cpus * sizeof(struct cpu_stats));
391 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
393 size_t i;
395 printf("{");
396 for (i = 0; i < len; ++i) {
397 if (i % 15 == 0)
398 printf("\n ");
399 printf("0x%02x, ", payload[i]);
401 printf("\n}\n");
402 fflush(stdout);
405 static int xmit_smoke_setup(struct ctx *ctx)
407 int icmp_sock, ret, ttl = 64;
408 struct icmp_filter filter;
410 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
411 if (icmp_sock < 0)
412 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
414 filter.data = ~(1 << ICMP_ECHOREPLY);
416 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
417 if (ret < 0)
418 panic("Cannot install filter!\n");
420 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
421 if (ret < 0)
422 panic("Cannot set TTL!\n");
424 memset(&ctx->dest, 0, sizeof(ctx->dest));
425 ctx->dest.sin_family = AF_INET;
426 ctx->dest.sin_port = 0;
428 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
429 if (ret < 0)
430 panic("Cannot resolv address!\n");
432 return icmp_sock;
435 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
437 int ret;
438 unsigned int i, j;
439 short ident, cnt = 1, idstore[SMOKE_N_PROBES];
440 uint8_t outpack[512], *data;
441 struct icmphdr *icmp;
442 struct iphdr *ip;
443 size_t len = sizeof(*icmp) + 56;
444 struct sockaddr_in from;
445 socklen_t from_len;
446 struct pollfd fds = {
447 .fd = icmp_sock,
448 .events = POLLIN,
451 fmemset(idstore, 0, sizeof(idstore));
452 for (j = 0; j < SMOKE_N_PROBES; j++) {
453 while ((ident = htons((short) rand())) == 0)
454 sleep(0);
455 idstore[j] = ident;
457 memset(outpack, 0, sizeof(outpack));
458 icmp = (void *) outpack;
459 icmp->type = ICMP_ECHO;
460 icmp->un.echo.id = ident;
461 icmp->un.echo.sequence = htons(cnt++);
463 data = ((uint8_t *) outpack + sizeof(*icmp));
464 for (i = 0; i < 56; ++i)
465 data[i] = (uint8_t) rand();
467 icmp->checksum = csum((unsigned short *) outpack,
468 len / sizeof(unsigned short));
470 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
471 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
472 if (unlikely(ret != (int) len))
473 panic("Cannot send out probe: %s!\n", strerror(errno));
475 ret = poll(&fds, 1, 50);
476 if (ret < 0)
477 panic("Poll failed!\n");
479 if (fds.revents & POLLIN) {
480 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
481 (struct sockaddr *) &from, &from_len);
482 if (unlikely(ret <= 0))
483 panic("Probe receive failed!\n");
484 if (unlikely(from_len != sizeof(ctx->dest)))
485 continue;
486 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
487 continue;
488 if (unlikely((size_t) ret < sizeof(*ip) + sizeof(*icmp)))
489 continue;
490 ip = (void *) outpack;
491 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > (size_t) ret))
492 continue;
493 icmp = (void *) outpack + ip->ihl * 4;
494 for (i = 0; i < array_size(idstore); ++i) {
495 if (unlikely(icmp->un.echo.id != idstore[i]))
496 continue;
497 return 0;
502 return -1;
505 static void xmit_slowpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned long orig_num)
507 int ret, icmp_sock = -1;
508 unsigned long num = 1, i = 0;
509 struct timeval start, end, diff;
510 unsigned long long tx_bytes = 0, tx_packets = 0;
511 struct packet_dyn *pktd;
512 struct sockaddr_ll saddr = {
513 .sll_family = PF_PACKET,
514 .sll_halen = ETH_ALEN,
515 .sll_ifindex = device_ifindex(ctx->device),
518 if (ctx->num > 0)
519 num = ctx->num;
520 if (ctx->num == 0 && orig_num > 0)
521 num = 0;
523 if (ctx->smoke_test)
524 icmp_sock = xmit_smoke_setup(ctx);
526 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
528 bug_on(gettimeofday(&start, NULL));
530 while (likely(sigint == 0 && num > 0 && plen > 0)) {
531 pktd = &packet_dyn[i];
532 if (pktd->clen + pktd->rlen + pktd->slen) {
533 apply_counter(i);
534 apply_randomizer(i);
535 apply_csum16(i);
537 retry:
538 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
539 (struct sockaddr *) &saddr, sizeof(saddr));
540 if (unlikely(ret < 0)) {
541 if (errno == ENOBUFS) {
542 sched_yield();
543 goto retry;
545 if (ctx->smoke_test)
546 panic("Sendto error: %s!\n", strerror(errno));
549 tx_bytes += packets[i].len;
550 tx_packets++;
552 if (ctx->smoke_test) {
553 ret = xmit_smoke_probe(icmp_sock, ctx);
554 if (unlikely(ret < 0)) {
555 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
556 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
557 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
558 i, seed);
560 dump_trafgen_snippet(packets[i].payload, packets[i].len);
561 break;
565 if (!ctx->rand) {
566 i++;
567 if (i >= plen)
568 i = 0;
569 } else
570 i = rand() % plen;
572 if (ctx->num > 0)
573 num--;
575 if ((ctx->gap.tv_sec | ctx->gap.tv_nsec) > 0)
576 nanosleep(&ctx->gap, NULL);
579 bug_on(gettimeofday(&end, NULL));
580 timersub(&end, &start, &diff);
582 if (ctx->smoke_test)
583 close(icmp_sock);
585 stats[cpu].tx_packets = tx_packets;
586 stats[cpu].tx_bytes = tx_bytes;
587 stats[cpu].tv_sec = diff.tv_sec;
588 stats[cpu].tv_usec = diff.tv_usec;
590 stats[cpu].state |= CPU_STATS_STATE_RES;
593 static void xmit_fastpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned long orig_num)
595 int ifindex = device_ifindex(ctx->device);
596 uint8_t *out = NULL;
597 unsigned int it = 0;
598 unsigned long num = 1, i = 0;
599 size_t size = ring_size(ctx->device, ctx->reserve_size);
600 struct ring tx_ring;
601 struct frame_map *hdr;
602 struct timeval start, end, diff;
603 struct packet_dyn *pktd;
604 unsigned long long tx_bytes = 0, tx_packets = 0;
606 set_sock_prio(sock, 512);
608 ring_tx_setup(&tx_ring, sock, size, ifindex, ctx->jumbo_support, ctx->verbose);
610 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
612 if (ctx->num > 0)
613 num = ctx->num;
614 if (ctx->num == 0 && orig_num > 0)
615 num = 0;
617 bug_on(gettimeofday(&start, NULL));
619 while (likely(sigint == 0 && num > 0 && plen > 0)) {
620 if (!user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
621 int ret = pull_and_flush_tx_ring(sock);
622 if (unlikely(ret < 0)) {
623 /* We could hit EBADF if the socket has been closed before
624 * the timer was triggered.
626 if (errno != EBADF && errno != ENOBUFS)
627 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
630 continue;
633 hdr = tx_ring.frames[it].iov_base;
634 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
636 hdr->tp_h.tp_snaplen = packets[i].len;
637 hdr->tp_h.tp_len = packets[i].len;
639 pktd = &packet_dyn[i];
640 if (pktd->clen + pktd->rlen + pktd->slen) {
641 apply_counter(i);
642 apply_randomizer(i);
643 apply_csum16(i);
646 fmemcpy(out, packets[i].payload, packets[i].len);
648 tx_bytes += packets[i].len;
649 tx_packets++;
651 if (!ctx->rand) {
652 i++;
653 if (i >= plen)
654 i = 0;
655 } else
656 i = rand() % plen;
658 kernel_may_pull_from_tx(&hdr->tp_h);
660 it++;
661 if (it >= tx_ring.layout.tp_frame_nr)
662 it = 0;
664 if (ctx->num > 0)
665 num--;
668 bug_on(gettimeofday(&end, NULL));
669 timersub(&end, &start, &diff);
671 pull_and_flush_tx_ring_wait(sock);
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(unsigned int cpu, sig_atomic_t s)
684 stats[cpu].state = s;
687 static inline sig_atomic_t __get_state(unsigned int cpu)
689 return stats[cpu].state;
692 static unsigned long __wait_and_sum_others(struct ctx *ctx, unsigned int cpu)
694 unsigned int i;
695 unsigned long total;
697 for (i = 0, total = plen; i < ctx->cpus; i++) {
698 if (i == cpu)
699 continue;
701 while ((__get_state(i) &
702 (CPU_STATS_STATE_CFG |
703 CPU_STATS_STATE_RES)) == 0 &&
704 sigint == 0)
705 sched_yield();
707 total += stats[i].cf_packets;
710 return total;
713 static void __correct_global_delta(struct ctx *ctx, unsigned int cpu, unsigned long orig)
715 unsigned int i;
716 unsigned long total;
717 int cpu_sel;
718 long long delta_correction = 0;
720 for (i = 0, total = ctx->num; i < ctx->cpus; i++) {
721 if (i == cpu)
722 continue;
724 while ((__get_state(i) &
725 (CPU_STATS_STATE_CHK |
726 CPU_STATS_STATE_RES)) == 0 &&
727 sigint == 0)
728 sched_yield();
730 total += stats[i].cd_packets;
733 if (total > orig)
734 delta_correction = -1 * ((long long) total - orig);
735 if (total < orig)
736 delta_correction = +1 * ((long long) orig - total);
738 for (cpu_sel = -1, i = 0; i < ctx->cpus; i++) {
739 if (stats[i].cd_packets > 0) {
740 if ((long long) stats[i].cd_packets +
741 delta_correction >= 0) {
742 cpu_sel = i;
743 break;
748 if ((int) cpu == cpu_sel)
749 ctx->num += delta_correction;
752 static void __set_state_cf(unsigned int cpu, unsigned long p, unsigned long b,
753 sig_atomic_t s)
755 stats[cpu].cf_packets = p;
756 stats[cpu].cf_bytes = b;
757 stats[cpu].state = s;
760 static void __set_state_cd(unsigned int cpu, unsigned long p, sig_atomic_t s)
762 stats[cpu].cd_packets = p;
763 stats[cpu].state = s;
766 static int xmit_packet_precheck(struct ctx *ctx, unsigned int cpu)
768 unsigned int i;
769 unsigned long plen_total, orig = ctx->num;
770 size_t mtu, total_len = 0;
772 bug_on(plen != dlen);
774 for (i = 0; i < plen; ++i)
775 total_len += packets[i].len;
777 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
778 plen_total = __wait_and_sum_others(ctx, cpu);
780 if (orig > 0) {
781 ctx->num = (unsigned long) round((1.0 * plen / plen_total) * orig);
783 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
784 CPU_STATS_STATE_CFG);
785 __correct_global_delta(ctx, cpu, orig);
788 if (plen == 0) {
789 __set_state(cpu, CPU_STATS_STATE_RES);
790 return 0;
793 for (mtu = device_mtu(ctx->device), i = 0; i < plen; ++i) {
794 if (packets[i].len > mtu + 14)
795 panic("Device MTU < than packet%d's size!\n", i);
796 if (packets[i].len <= 14)
797 panic("Packet%d's size too short!\n", i);
800 return 0;
803 static void main_loop(struct ctx *ctx, char *confname, bool slow,
804 unsigned int cpu, bool invoke_cpp, unsigned long orig_num)
806 compile_packets(confname, ctx->verbose, cpu, invoke_cpp);
807 if (xmit_packet_precheck(ctx, cpu) < 0)
808 return;
810 if (cpu == 0) {
811 unsigned int i;
812 size_t total_len = 0, total_pkts = 0;
814 for (i = 0; i < ctx->cpus; ++i) {
815 total_len += stats[i].cf_bytes;
816 total_pkts += stats[i].cf_packets;
819 printf("%6zu packets to schedule\n", total_pkts);
820 printf("%6zu bytes in total\n", total_len);
821 printf("Running! Hang up with ^C!\n\n");
822 fflush(stdout);
825 sock = pf_tx_socket();
827 if (ctx->qdisc_path == false)
828 set_sock_qdisc_bypass(sock, ctx->verbose);
830 if (slow)
831 xmit_slowpath_or_die(ctx, cpu, orig_num);
832 else
833 xmit_fastpath_or_die(ctx, cpu, orig_num);
835 close(sock);
837 cleanup_packets();
840 static unsigned int generate_srand_seed(void)
842 int fd;
843 unsigned int _seed;
845 fd = open("/dev/urandom", O_RDONLY);
846 if (fd < 0)
847 return time(NULL);
849 read_or_die(fd, &_seed, sizeof(_seed));
851 close(fd);
852 return _seed;
855 int main(int argc, char **argv)
857 bool slow = false, invoke_cpp = false, reseed = true, cpustats = true;
858 bool prio_high = false, set_irq_aff = true, set_sock_mem = true;
859 int c, opt_index, vals[4] = {0}, irq;
860 uint64_t gap = 0;
861 unsigned int i, j;
862 char *confname = NULL, *ptr;
863 unsigned long cpus_tmp, orig_num = 0;
864 unsigned long long tx_packets, tx_bytes;
865 struct ctx ctx;
867 fmemset(&ctx, 0, sizeof(ctx));
868 ctx.cpus = get_number_cpus_online();
869 ctx.uid = getuid();
870 ctx.gid = getgid();
871 ctx.qdisc_path = false;
873 /* Keep an initial small default size to reduce cache-misses. */
874 ctx.reserve_size = 512 * (1 << 10);
876 while ((c = getopt_long(argc, argv, short_options, long_options,
877 &opt_index)) != EOF) {
878 switch (c) {
879 case 'h':
880 help();
881 break;
882 case 'v':
883 version();
884 break;
885 case 'C':
886 cpustats = false;
887 break;
888 case 'e':
889 example();
890 break;
891 case 'p':
892 invoke_cpp = true;
893 break;
894 case 'V':
895 ctx.verbose = true;
896 break;
897 case 'P':
898 cpus_tmp = strtoul(optarg, NULL, 0);
899 if (cpus_tmp > 0 && cpus_tmp < ctx.cpus)
900 ctx.cpus = cpus_tmp;
901 break;
902 case 'd':
903 case 'o':
904 ctx.device = xstrndup(optarg, IFNAMSIZ);
905 break;
906 case 'H':
907 prio_high = true;
908 break;
909 case 'A':
910 set_sock_mem = false;
911 break;
912 case 'Q':
913 set_irq_aff = false;
914 break;
915 case 'q':
916 ctx.qdisc_path = true;
917 break;
918 case 'r':
919 ctx.rand = true;
920 break;
921 case 's':
922 slow = true;
923 ctx.cpus = 1;
924 ctx.smoke_test = true;
925 ctx.rhost = xstrdup(optarg);
926 break;
927 case 'R':
928 ctx.rfraw = true;
929 break;
930 case 'J':
931 ctx.jumbo_support = true;
932 break;
933 case 'c':
934 case 'i':
935 confname = xstrdup(optarg);
936 if (!strncmp("-", confname, strlen("-")))
937 ctx.cpus = 1;
938 break;
939 case 'u':
940 ctx.uid = strtoul(optarg, NULL, 0);
941 ctx.enforce = true;
942 break;
943 case 'g':
944 ctx.gid = strtoul(optarg, NULL, 0);
945 ctx.enforce = true;
946 break;
947 case 'k':
948 printf("Option -k/--kernel-pull is no longer used and "
949 "will be removed in a future release!\n");
950 break;
951 case 'E':
952 seed = strtoul(optarg, NULL, 0);
953 reseed = false;
954 break;
955 case 'n':
956 orig_num = strtoul(optarg, NULL, 0);
957 ctx.num = orig_num;
958 break;
959 case 't':
960 slow = true;
961 ptr = optarg;
962 prctl(PR_SET_TIMERSLACK, 1UL);
963 gap = strtoul(optarg, NULL, 0);
965 for (j = i = strlen(optarg); i > 0; --i) {
966 if (!isdigit(optarg[j - i]))
967 break;
968 ptr++;
971 if (!strncmp(ptr, "ns", strlen("ns"))) {
972 ctx.gap.tv_sec = gap / 1000000000;
973 ctx.gap.tv_nsec = gap % 1000000000;
974 } else if (*ptr == '\0' || !strncmp(ptr, "us", strlen("us"))) {
975 /* Default to microseconds for backwards
976 * compatibility if no postfix is given.
978 ctx.gap.tv_sec = gap / 1000000;
979 ctx.gap.tv_nsec = (gap % 1000000) * 1000;
980 } else if (!strncmp(ptr, "ms", strlen("ms"))) {
981 ctx.gap.tv_sec = gap / 1000;
982 ctx.gap.tv_nsec = (gap % 1000) * 1000000;
983 } else if (!strncmp(ptr, "s", strlen("s"))) {
984 ctx.gap.tv_sec = gap;
985 ctx.gap.tv_nsec = 0;
986 } else
987 panic("Syntax error in time param!\n");
989 if (gap > 0)
990 /* Fall back to single core to not mess up
991 * correct timing. We are slow anyway!
993 ctx.cpus = 1;
994 break;
995 case 'S':
996 ptr = optarg;
998 for (j = i = strlen(optarg); i > 0; --i) {
999 if (!isdigit(optarg[j - i]))
1000 break;
1001 ptr++;
1004 if (!strncmp(ptr, "KiB", strlen("KiB")))
1005 ctx.reserve_size = 1 << 10;
1006 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1007 ctx.reserve_size = 1 << 20;
1008 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1009 ctx.reserve_size = 1 << 30;
1010 else
1011 panic("Syntax error in ring size param!\n");
1013 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1014 break;
1015 case '?':
1016 switch (optopt) {
1017 case 'd':
1018 case 'c':
1019 case 'n':
1020 case 'S':
1021 case 's':
1022 case 'P':
1023 case 'o':
1024 case 'E':
1025 case 'i':
1026 case 'k':
1027 case 'u':
1028 case 'g':
1029 case 't':
1030 panic("Option -%c requires an argument!\n",
1031 optopt);
1032 default:
1033 if (isprint(optopt))
1034 printf("Unknown option character `0x%X\'!\n", optopt);
1035 die();
1037 default:
1038 break;
1042 if (argc < 5)
1043 help();
1044 if (ctx.device == NULL)
1045 panic("No networking device given!\n");
1046 if (confname == NULL)
1047 panic("No configuration file given!\n");
1048 if (device_mtu(ctx.device) == 0)
1049 panic("This is no networking device!\n");
1051 register_signal(SIGINT, signal_handler);
1052 register_signal(SIGQUIT, signal_handler);
1053 register_signal(SIGTERM, signal_handler);
1054 register_signal(SIGHUP, signal_handler);
1056 if (prio_high) {
1057 set_proc_prio(-20);
1058 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1061 if (set_sock_mem)
1062 set_system_socket_memory(vals, array_size(vals));
1063 xlockme();
1065 if (ctx.rfraw) {
1066 ctx.device_trans = xstrdup(ctx.device);
1067 xfree(ctx.device);
1069 enter_rfmon_mac80211(ctx.device_trans, &ctx.device);
1070 sleep(0);
1074 * If number of packets is smaller than number of CPUs use only as
1075 * many CPUs as there are packets. Otherwise we end up sending more
1076 * packets than intended or none at all.
1078 if (ctx.num)
1079 ctx.cpus = min_t(unsigned int, ctx.num, ctx.cpus);
1081 irq = device_irq_number(ctx.device);
1082 if (set_irq_aff)
1083 device_set_irq_affinity_list(irq, 0, ctx.cpus - 1);
1085 stats = setup_shared_var(ctx.cpus);
1087 for (i = 0; i < ctx.cpus; i++) {
1088 pid_t pid = fork();
1090 switch (pid) {
1091 case 0:
1092 if (reseed)
1093 seed = generate_srand_seed();
1094 srand(seed);
1096 cpu_affinity(i);
1097 main_loop(&ctx, confname, slow, i, invoke_cpp, orig_num);
1099 goto thread_out;
1100 case -1:
1101 panic("Cannot fork processes!\n");
1105 for (i = 0; i < ctx.cpus; i++) {
1106 int status;
1108 wait(&status);
1109 if (WEXITSTATUS(status) == EXIT_FAILURE)
1110 die();
1113 if (ctx.rfraw)
1114 leave_rfmon_mac80211(ctx.device);
1116 if (set_sock_mem)
1117 reset_system_socket_memory(vals, array_size(vals));
1119 for (i = 0, tx_packets = tx_bytes = 0; i < ctx.cpus; i++) {
1120 while ((__get_state(i) & CPU_STATS_STATE_RES) == 0)
1121 sched_yield();
1123 tx_packets += stats[i].tx_packets;
1124 tx_bytes += stats[i].tx_bytes;
1127 fflush(stdout);
1128 printf("\n");
1129 printf("\r%12llu packets outgoing\n", tx_packets);
1130 printf("\r%12llu bytes outgoing\n", tx_bytes);
1131 for (i = 0; cpustats && i < ctx.cpus; i++) {
1132 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1133 stats[i].tv_sec, stats[i].tv_usec, i,
1134 stats[i].tx_packets);
1137 thread_out:
1138 xunlockme();
1139 destroy_shared_var(stats, ctx.cpus);
1140 if (set_irq_aff)
1141 device_restore_irq_affinity_list();
1143 free(ctx.device);
1144 free(ctx.device_trans);
1145 free(ctx.rhost);
1146 free(confname);
1148 return 0;