stddef: add some more useful definitions
[netsniff-ng.git] / trafgen.c
blob9a52edf94b9ef086e7f6284b8975f0271f31a1c5
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 - 2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
8 * A high-performance network traffic generator that uses the zero-copy
9 * kernelspace TX_RING for network I/O. On comodity Gigabit hardware up
10 * to 1,488,095 pps 64 Byte pps have been achieved with 2 trafgen instances
11 * bound to different CPUs from the userspace and turned off pause frames,
12 * ask Ronald from NST (Network Security Toolkit) for more details. ;-)
13 * So, this line-rate result is the very same as pktgen from kernelspace!
15 * Who can now hold the fords when the King of the Nine Riders comes? And
16 * other armies will come. I am too late. All is lost. I tarried on the
17 * way. All is lost. Even if my errand is performed, no one will ever
18 * know. There will be no one I can tell. It will be in vain.
20 * -- The Lord of the Rings, Frodo thinking,
21 * Chapter 'The Stairs of Cirith Ungol'.
24 #include <stdio.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <ctype.h>
28 #include <stdbool.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
31 #include <sys/fsuid.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <sys/wait.h>
35 #include <sys/mman.h>
36 #include <net/ethernet.h>
37 #include <netinet/in.h>
38 #include <netinet/ip.h>
39 #include <linux/icmp.h>
40 #include <arpa/inet.h>
41 #include <signal.h>
42 #include <stdint.h>
43 #include <stdlib.h>
44 #include <fcntl.h>
45 #include <time.h>
46 #include <poll.h>
47 #include <netdb.h>
48 #include <math.h>
49 #include <unistd.h>
51 #include "xmalloc.h"
52 #include "die.h"
53 #include "mac80211.h"
54 #include "xutils.h"
55 #include "xio.h"
56 #include "built_in.h"
57 #include "trafgen_conf.h"
58 #include "tprintf.h"
59 #include "ring_tx.h"
60 #include "csum.h"
62 struct ctx {
63 bool rand, rfraw, jumbo_support, verbose, smoke_test, enforce;
64 unsigned long kpull, num, gap, reserve_size, cpus;
65 uid_t uid; gid_t gid; char *device, *device_trans, *rhost;
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 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:VRsP:eE:pu:g:";
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 {"jumbo-support", no_argument, NULL, 'J'},
101 {"cpp", no_argument, NULL, 'p'},
102 {"rfraw", no_argument, NULL, 'R'},
103 {"rand", no_argument, NULL, 'r'},
104 {"verbose", no_argument, NULL, 'V'},
105 {"version", no_argument, NULL, 'v'},
106 {"example", no_argument, NULL, 'e'},
107 {"help", no_argument, NULL, 'h'},
108 {NULL, 0, NULL, 0}
111 static int sock;
113 static struct itimerval itimer;
115 static unsigned long interval = TX_KERNEL_PULL_INT;
117 static struct cpu_stats *stats;
119 unsigned int seed;
121 #define CPU_STATS_STATE_CFG 1
122 #define CPU_STATS_STATE_CHK 2
123 #define CPU_STATS_STATE_RES 4
125 #ifndef ICMP_FILTER
126 # define ICMP_FILTER 1
128 struct icmp_filter {
129 __u32 data;
131 #endif
133 static void signal_handler(int number)
135 switch (number) {
136 case SIGINT:
137 sigint = 1;
138 case SIGHUP:
139 default:
140 break;
144 static void timer_elapsed(int number)
146 set_itimer_interval_value(&itimer, 0, interval);
147 pull_and_flush_tx_ring(sock);
148 setitimer(ITIMER_REAL, &itimer, NULL);
151 static void help(void)
153 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
154 puts("http://www.netsniff-ng.org\n\n"
155 "Usage: trafgen [options]\n"
156 "Options:\n"
157 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
158 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
159 " -p|--cpp Run packet config through C preprocessor\n"
160 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
161 " -R|--rfraw Inject raw 802.11 frames\n"
162 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
163 " -n|--num <uint> Number of packets until exit (def: 0)\n"
164 " -r|--rand Randomize packet selection (def: round robin)\n"
165 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
166 " -t|--gap <uint> Interpacket gap in us (approx)\n"
167 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
168 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
169 " -E|--seed <uint> Manually set srand(3) seed\n"
170 " -u|--user <userid> Drop privileges and change to userid\n"
171 " -g|--group <groupid> Drop privileges and change to groupid\n"
172 " -V|--verbose Be more verbose\n"
173 " -v|--version Show version\n"
174 " -e|--example Show built-in packet config example\n"
175 " -h|--help Guess what?!\n\n"
176 "Examples:\n"
177 " See trafgen.txf for configuration file examples.\n"
178 " trafgen --dev eth0 --conf trafgen.cfg\n"
179 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
180 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
181 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
182 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000\n"
183 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
184 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
185 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
186 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
187 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
188 " Run packet only on CPU1-2: cpu(1:2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
189 "Note:\n"
190 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
191 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
192 " we assume the kernel crashed, thus we print the packet and quit.\n"
193 " In case you find a ping-of-death, please mention trafgen in your\n"
194 " commit message of the fix!\n\n"
195 " For introducing bit errors, delays with random variation and more,\n"
196 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
197 " For generating different package distributions, you can use scripting\n"
198 " to generate a trafgen config file with packet ratios as:\n\n"
199 " IMIX 64:7, 570:4, 1518:1\n"
200 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
201 " Cisco 64:7, 594:4, 1518:1\n"
202 " RPR Trimodal 64:60, 512:20, 1518:20\n"
203 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n\n"
204 "Please report bugs to <bugs@netsniff-ng.org>\n"
205 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
206 "Swiss federal institute of technology (ETH Zurich)\n"
207 "License: GNU GPL version 2.0\n"
208 "This is free software: you are free to change and redistribute it.\n"
209 "There is NO WARRANTY, to the extent permitted by law.\n");
210 die();
213 static void example(void)
215 const char *e =
216 "/* Note: dynamic elements make trafgen slower! */\n\n"
217 "#include <stddef.h>\n"
218 "#define SYN (1 << 1)\n"
219 "#define ECN (1 << 6)\n\n"
220 "{\n"
221 " /* MAC Destination */\n"
222 " fill(0xff, ETH_ALEN),\n"
223 " /* MAC Source */\n"
224 " 0x00, 0x02, 0xb3, drnd(3),\n"
225 " /* IPv4 Protocol */\n"
226 " c16(ETH_P_IP),\n"
227 " /* IPv4 Version, IHL, TOS */\n"
228 " 0b01000101, 0,\n"
229 " /* IPv4 Total Len */\n"
230 " c16(59),\n"
231 " /* IPv4 Ident */\n"
232 " drnd(2),\n"
233 " /* IPv4 Flags, Frag Off */\n"
234 " 0b01000000, 0,\n"
235 " /* IPv4 TTL */\n"
236 " 64,\n"
237 " /* Proto TCP */\n"
238 " 0x06,\n"
239 " /* IPv4 Checksum (IP header from, to) */\n"
240 " csumip(14, 33),\n"
241 " /* Source IP */\n"
242 " drnd(4),\n"
243 " /* Dest IP */\n"
244 " drnd(4),\n"
245 " /* TCP Source Port */\n"
246 " drnd(2),\n"
247 " /* TCP Dest Port */\n"
248 " c16(80),\n"
249 " /* TCP Sequence Number */\n"
250 " drnd(4),\n"
251 " /* TCP Ackn. Number */\n"
252 " c32(0),\n"
253 " /* TCP Header length + TCP SYN/ECN Flag */\n"
254 " c16((0x8 << 12) | SYN | ECN)\n"
255 " /* Window Size */\n"
256 " c16(16),\n"
257 " /* TCP Checksum (offset IP, offset TCP) */\n"
258 " csumtcp(14, 34),\n"
259 " /* TCP Options */\n"
260 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
261 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
262 " /* Data blob */\n"
263 " \"gotcha!\",\n"
264 "}";
265 puts(e);
266 die();
269 static void version(void)
271 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
272 puts("http://www.netsniff-ng.org\n\n"
273 "Please report bugs to <bugs@netsniff-ng.org>\n"
274 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
275 "Swiss federal institute of technology (ETH Zurich)\n"
276 "License: GNU GPL version 2.0\n"
277 "This is free software: you are free to change and redistribute it.\n"
278 "There is NO WARRANTY, to the extent permitted by law.\n");
279 die();
282 static void apply_counter(int counter_id)
284 int j, i = counter_id;
285 size_t counter_max = packet_dyn[i].clen;
287 for (j = 0; j < counter_max; ++j) {
288 uint8_t val;
289 struct counter *counter = &packet_dyn[i].cnt[j];
291 val = counter->val - counter->min;
293 switch (counter->type) {
294 case TYPE_INC:
295 val = (val + counter->inc) % (counter->max - counter->min + 1);
296 break;
297 case TYPE_DEC:
298 val = (val - counter->inc) % (counter->min - counter->max + 1);
299 break;
300 default:
301 bug();
304 counter->val = val + counter->min;
305 packets[i].payload[counter->off] = val;
309 static void apply_randomizer(int rand_id)
311 int j, i = rand_id;
312 size_t rand_max = packet_dyn[i].rlen;
314 for (j = 0; j < rand_max; ++j) {
315 uint8_t val = (uint8_t) rand();
316 struct randomizer *randomizer = &packet_dyn[i].rnd[j];
318 packets[i].payload[randomizer->off] = val;
322 /* Taken and modified from tcpdump, Copyright belongs to them! */
324 struct cksum_vec {
325 const u8 *ptr;
326 int len;
329 #define ADDCARRY(x) \
330 do { if ((x) > 65535) \
331 (x) -= 65535; \
332 } while (0)
334 #define REDUCE \
335 do { \
336 l_util.l = sum; \
337 sum = l_util.s[0] + l_util.s[1]; \
338 ADDCARRY(sum); \
339 } while (0)
341 static u16 __in_cksum(const struct cksum_vec *vec, int veclen)
343 register const u16 *w;
344 register int sum = 0, mlen = 0;
345 int byte_swapped = 0;
346 union {
347 u8 c[2];
348 u16 s;
349 } s_util;
350 union {
351 u16 s[2];
352 u32 l;
353 } l_util;
355 for (; veclen != 0; vec++, veclen--) {
356 if (vec->len == 0)
357 continue;
359 w = (const u16 *) (void *) vec->ptr;
361 if (mlen == -1) {
362 s_util.c[1] = *(const u8 *) w;
363 sum += s_util.s;
364 w = (const u16 *) (void *) ((const u8 *) w + 1);
365 mlen = vec->len - 1;
366 } else
367 mlen = vec->len;
369 if ((1 & (unsigned long) w) && (mlen > 0)) {
370 REDUCE;
371 sum <<= 8;
372 s_util.c[0] = *(const u8 *) w;
373 w = (const u16 *) (void *) ((const u8 *) w + 1);
374 mlen--;
375 byte_swapped = 1;
378 while ((mlen -= 32) >= 0) {
379 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
380 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
381 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
382 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
383 w += 16;
386 mlen += 32;
388 while ((mlen -= 8) >= 0) {
389 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
390 w += 4;
393 mlen += 8;
395 if (mlen == 0 && byte_swapped == 0)
396 continue;
398 REDUCE;
400 while ((mlen -= 2) >= 0) {
401 sum += *w++;
404 if (byte_swapped) {
405 REDUCE;
406 sum <<= 8;
407 byte_swapped = 0;
409 if (mlen == -1) {
410 s_util.c[1] = *(const u8 *) w;
411 sum += s_util.s;
412 mlen = 0;
413 } else
414 mlen = -1;
415 } else if (mlen == -1)
416 s_util.c[0] = *(const u8 *) w;
419 if (mlen == -1) {
420 s_util.c[1] = 0;
421 sum += s_util.s;
424 REDUCE;
426 return (~sum & 0xffff);
429 static u16 p4_csum(const struct ip *ip, const u8 *data, u16 len,
430 u8 next_proto)
432 struct cksum_vec vec[2];
433 struct pseudo_hdr {
434 u32 src;
435 u32 dst;
436 u8 mbz;
437 u8 proto;
438 u16 len;
439 } ph;
441 memset(&ph, 0, sizeof(ph));
442 ph.len = htons(len);
443 ph.mbz = 0;
444 ph.proto = next_proto;
445 ph.src = ip->ip_src.s_addr;
446 ph.dst = ip->ip_dst.s_addr;
448 vec[0].ptr = (const u8 *) (void *) &ph;
449 vec[0].len = sizeof(ph);
451 vec[1].ptr = data;
452 vec[1].len = len;
454 return __in_cksum(vec, 2);
457 static void apply_csum16(int csum_id)
459 int j, i = csum_id;
460 size_t csum_max = packet_dyn[i].slen;
462 for (j = 0; j < csum_max; ++j) {
463 uint16_t sum = 0;
464 struct csum16 *csum = &packet_dyn[i].csum[j];
466 fmemset(&packets[i].payload[csum->off], 0, sizeof(sum));
468 switch (csum->which) {
469 case CSUM_IP:
470 if (csum->to >= packets[i].len)
471 csum->to = packets[i].len - 1;
472 sum = calc_csum(packets[i].payload + csum->from,
473 csum->to - csum->from + 1, 0);
474 break;
475 case CSUM_UDP:
476 sum = p4_csum((void *) packets[i].payload + csum->from,
477 packets[i].payload + csum->to,
478 (packets[i].len - csum->to),
479 IPPROTO_UDP);
480 break;
481 case CSUM_TCP:
482 sum = p4_csum((void *) packets[i].payload + csum->from,
483 packets[i].payload + csum->to,
484 (packets[i].len - csum->to),
485 IPPROTO_TCP);
486 break;
489 fmemcpy(&packets[i].payload[csum->off], &sum, sizeof(sum));
493 static struct cpu_stats *setup_shared_var(unsigned long cpus)
495 int fd;
496 char zbuff[cpus * sizeof(struct cpu_stats)], file[256];
497 struct cpu_stats *buff;
499 fmemset(zbuff, 0, sizeof(zbuff));
500 slprintf(file, sizeof(file), ".tmp_mmap.%u", (unsigned int) rand());
502 fd = creat(file, S_IRUSR | S_IWUSR);
503 bug_on(fd < 0);
504 close(fd);
506 fd = open_or_die_m(file, O_RDWR | O_CREAT | O_TRUNC,
507 S_IRUSR | S_IWUSR);
508 write_or_die(fd, zbuff, sizeof(zbuff));
510 buff = (void *) mmap(0, sizeof(zbuff), PROT_READ | PROT_WRITE,
511 MAP_SHARED, fd, 0);
512 if (buff == (void *) -1)
513 panic("Cannot setup shared variable!\n");
515 close(fd);
516 unlink(file);
518 memset(buff, 0, sizeof(zbuff));
520 return buff;
523 static void destroy_shared_var(void *buff, unsigned long cpus)
525 munmap(buff, cpus * sizeof(struct cpu_stats));
528 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
530 int i;
532 printf("{");
533 for (i = 0; i < len; ++i) {
534 if (i % 15 == 0)
535 printf("\n ");
536 printf("0x%02x, ", payload[i]);
538 printf("\n}\n");
539 fflush(stdout);
542 static inline unsigned short csum(unsigned short *buf, int nwords)
544 unsigned long sum;
546 for (sum = 0; nwords > 0; nwords--)
547 sum += *buf++;
548 sum = (sum >> 16) + (sum & 0xffff);
549 sum += (sum >> 16);
551 return ~sum;
554 static int xmit_smoke_setup(struct ctx *ctx)
556 int icmp_sock, ret, ttl = 64;
557 struct icmp_filter filter;
559 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
560 if (icmp_sock < 0)
561 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
563 filter.data = ~(1 << ICMP_ECHOREPLY);
565 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
566 if (ret < 0)
567 panic("Cannot install filter!\n");
569 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
570 if (ret < 0)
571 panic("Cannot set TTL!\n");
573 memset(&ctx->dest, 0, sizeof(ctx->dest));
574 ctx->dest.sin_family = AF_INET;
575 ctx->dest.sin_port = 0;
577 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
578 if (ret < 0)
579 panic("Cannot resolv address!\n");
581 return icmp_sock;
584 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
586 int ret, i, probes = 5;
587 short ident, cnt = 1;
588 uint8_t outpack[512], *data;
589 struct icmphdr *icmp;
590 struct iphdr *ip;
591 size_t len = sizeof(*icmp) + 56;
592 struct sockaddr_in from;
593 socklen_t from_len;
594 struct pollfd fds = {
595 .fd = icmp_sock,
596 .events = POLLIN,
599 while (probes-- > 0) {
600 ident = htons((short) rand());
602 memset(outpack, 0, sizeof(outpack));
603 icmp = (void *) outpack;
604 icmp->type = ICMP_ECHO;
605 icmp->code = 0;
606 icmp->checksum = 0;
607 icmp->un.echo.id = ident;
608 icmp->un.echo.sequence = htons(cnt++);
610 data = ((uint8_t *) outpack + sizeof(*icmp));
611 for (i = 0; i < 56; ++i)
612 data[i] = (uint8_t) rand();
614 icmp->checksum = csum((unsigned short *) outpack,
615 len / sizeof(unsigned short));
617 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
618 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
619 if (unlikely(ret != len))
620 panic("Cannot send out probe: %s!\n", strerror(errno));
622 ret = poll(&fds, 1, 500);
623 if (ret < 0)
624 panic("Poll failed!\n");
626 if (fds.revents & POLLIN) {
627 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
628 (struct sockaddr *) &from, &from_len);
629 if (unlikely(ret <= 0))
630 panic("Probe receive failed!\n");
631 if (unlikely(from_len != sizeof(ctx->dest)))
632 continue;
633 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
634 continue;
635 if (unlikely(ret < sizeof(*ip) + sizeof(*icmp)))
636 continue;
637 ip = (void *) outpack;
638 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > ret))
639 continue;
640 icmp = (void *) outpack + ip->ihl * 4;
641 if (unlikely(icmp->un.echo.id != ident))
642 continue;
644 return 0;
648 return -1;
651 static void xmit_slowpath_or_die(struct ctx *ctx, int cpu)
653 int ret, icmp_sock = -1;
654 unsigned long num = 1, i = 0;
655 struct timeval start, end, diff;
656 unsigned long long tx_bytes = 0, tx_packets = 0;
657 struct packet_dyn *pktd;
658 struct sockaddr_ll saddr = {
659 .sll_family = PF_PACKET,
660 .sll_halen = ETH_ALEN,
661 .sll_ifindex = device_ifindex(ctx->device),
664 if (ctx->num > 0)
665 num = ctx->num;
667 if (ctx->smoke_test)
668 icmp_sock = xmit_smoke_setup(ctx);
670 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
672 bug_on(gettimeofday(&start, NULL));
674 while (likely(sigint == 0) && likely(num > 0)) {
675 pktd = &packet_dyn[i];
676 if (pktd->clen + pktd->rlen + pktd->slen) {
677 apply_counter(i);
678 apply_randomizer(i);
679 apply_csum16(i);
681 retry:
682 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
683 (struct sockaddr *) &saddr, sizeof(saddr));
684 if (unlikely(ret < 0)) {
685 if (errno == ENOBUFS) {
686 sched_yield();
687 goto retry;
690 panic("Sendto error: %s!\n", strerror(errno));
693 tx_bytes += packets[i].len;
694 tx_packets++;
696 if (ctx->smoke_test) {
697 ret = xmit_smoke_probe(icmp_sock, ctx);
698 if (unlikely(ret < 0)) {
699 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
700 printf(" Remote host seems to be unresponsive to ICMP pings!\n");
701 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
702 i, seed);
704 dump_trafgen_snippet(packets[i].payload, packets[i].len);
705 break;
709 if (!ctx->rand) {
710 i++;
711 if (i >= plen)
712 i = 0;
713 } else
714 i = rand() % plen;
716 if (ctx->num > 0)
717 num--;
719 if (ctx->gap > 0)
720 usleep(ctx->gap);
723 bug_on(gettimeofday(&end, NULL));
724 diff = tv_subtract(end, start);
726 if (ctx->smoke_test)
727 close(icmp_sock);
729 stats[cpu].tx_packets = tx_packets;
730 stats[cpu].tx_bytes = tx_bytes;
731 stats[cpu].tv_sec = diff.tv_sec;
732 stats[cpu].tv_usec = diff.tv_usec;
734 stats[cpu].state |= CPU_STATS_STATE_RES;
737 static void xmit_fastpath_or_die(struct ctx *ctx, int cpu)
739 int ifindex = device_ifindex(ctx->device);
740 uint8_t *out = NULL;
741 unsigned int it = 0;
742 unsigned long num = 1, i = 0, size;
743 struct ring tx_ring;
744 struct frame_map *hdr;
745 struct timeval start, end, diff;
746 struct packet_dyn *pktd;
747 unsigned long long tx_bytes = 0, tx_packets = 0;
749 fmemset(&tx_ring, 0, sizeof(tx_ring));
751 size = ring_size(ctx->device, ctx->reserve_size);
753 set_sock_prio(sock, 512);
754 set_packet_loss_discard(sock);
756 setup_tx_ring_layout(sock, &tx_ring, size, ctx->jumbo_support);
757 create_tx_ring(sock, &tx_ring, ctx->verbose);
758 mmap_tx_ring(sock, &tx_ring);
759 alloc_tx_ring_frames(&tx_ring);
760 bind_tx_ring(sock, &tx_ring, ifindex);
762 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
764 if (ctx->kpull)
765 interval = ctx->kpull;
766 if (ctx->num > 0)
767 num = ctx->num;
769 set_itimer_interval_value(&itimer, 0, interval);
770 setitimer(ITIMER_REAL, &itimer, NULL);
772 bug_on(gettimeofday(&start, NULL));
774 while (likely(sigint == 0) && likely(num > 0)) {
775 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) && likely(num > 0)) {
776 hdr = tx_ring.frames[it].iov_base;
778 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
779 * sizeof(struct sockaddr_ll); */
780 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
782 hdr->tp_h.tp_snaplen = packets[i].len;
783 hdr->tp_h.tp_len = packets[i].len;
785 pktd = &packet_dyn[i];
786 if (pktd->clen + pktd->rlen + pktd->slen) {
787 apply_counter(i);
788 apply_randomizer(i);
789 apply_csum16(i);
792 fmemcpy(out, packets[i].payload, packets[i].len);
794 tx_bytes += packets[i].len;
795 tx_packets++;
797 if (!ctx->rand) {
798 i++;
799 if (i >= plen)
800 i = 0;
801 } else
802 i = rand() % plen;
804 kernel_may_pull_from_tx(&hdr->tp_h);
806 it++;
807 if (it >= tx_ring.layout.tp_frame_nr)
808 it = 0;
810 if (ctx->num > 0)
811 num--;
813 if (unlikely(sigint == 1))
814 break;
818 bug_on(gettimeofday(&end, NULL));
819 diff = tv_subtract(end, start);
821 destroy_tx_ring(sock, &tx_ring);
823 stats[cpu].tx_packets = tx_packets;
824 stats[cpu].tx_bytes = tx_bytes;
825 stats[cpu].tv_sec = diff.tv_sec;
826 stats[cpu].tv_usec = diff.tv_usec;
828 stats[cpu].state |= CPU_STATS_STATE_RES;
831 static inline void __set_state(int cpu, sig_atomic_t s)
833 stats[cpu].state = s;
836 static inline sig_atomic_t __get_state(int cpu)
838 return stats[cpu].state;
841 static unsigned long __wait_and_sum_others(struct ctx *ctx, int cpu)
843 int i;
844 unsigned long total;
846 for (i = 0, total = plen; i < ctx->cpus; i++) {
847 if (i == cpu)
848 continue;
850 while ((__get_state(i) & CPU_STATS_STATE_CFG) == 0 &&
851 sigint == 0)
852 sched_yield();
854 total += stats[i].cf_packets;
857 return total;
860 static void __correct_global_delta(struct ctx *ctx, int cpu, unsigned long orig)
862 int i, cpu_sel;
863 unsigned long total;
864 long long delta_correction = 0;
866 for (i = 0, total = ctx->num; i < ctx->cpus; i++) {
867 if (i == cpu)
868 continue;
870 while ((__get_state(i) & CPU_STATS_STATE_CHK) == 0 &&
871 sigint == 0)
872 sched_yield();
874 total += stats[i].cd_packets;
877 if (total > orig)
878 delta_correction = -1 * ((long long) total - orig);
879 if (total < orig)
880 delta_correction = +1 * ((long long) orig - total);
882 for (cpu_sel = -1, i = 0; i < ctx->cpus; i++) {
883 if (stats[i].cd_packets > 0) {
884 if ((long long) stats[i].cd_packets +
885 delta_correction > 0) {
886 cpu_sel = i;
887 break;
892 if (cpu == cpu_sel)
893 ctx->num += delta_correction;
896 static void __set_state_cf(int cpu, unsigned long p, unsigned long b,
897 sig_atomic_t s)
899 stats[cpu].cf_packets = p;
900 stats[cpu].cf_bytes = b;
901 stats[cpu].state = s;
904 static void __set_state_cd(int cpu, unsigned long p, sig_atomic_t s)
906 stats[cpu].cd_packets = p;
907 stats[cpu].state = s;
910 static int xmit_packet_precheck(struct ctx *ctx, int cpu)
912 int i;
913 unsigned long plen_total, orig = ctx->num;
914 size_t mtu, total_len = 0;
916 bug_on(plen != dlen);
918 for (i = 0; i < plen; ++i)
919 total_len += packets[i].len;
921 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
922 plen_total = __wait_and_sum_others(ctx, cpu);
924 if (orig > 0) {
925 ctx->num = (unsigned long) nearbyint((1.0 * plen / plen_total) * orig);
927 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
928 CPU_STATS_STATE_CFG);
929 __correct_global_delta(ctx, cpu, orig);
932 if (plen == 0) {
933 __set_state(cpu, CPU_STATS_STATE_RES);
934 return -1;
937 for (mtu = device_mtu(ctx->device), i = 0; i < plen; ++i) {
938 if (packets[i].len > mtu + 14)
939 panic("Device MTU < than packet%d's size!\n", i);
940 if (packets[i].len <= 14)
941 panic("Packet%d's size too short!\n", i);
944 return 0;
947 static void main_loop(struct ctx *ctx, char *confname, bool slow,
948 int cpu, bool invoke_cpp)
950 compile_packets(confname, ctx->verbose, cpu, invoke_cpp);
951 if (xmit_packet_precheck(ctx, cpu) < 0)
952 return;
954 if (cpu == 0) {
955 int i;
956 size_t total_len = 0, total_pkts = 0;
958 for (i = 0; i < ctx->cpus; ++i) {
959 total_len += stats[i].cf_bytes;
960 total_pkts += stats[i].cf_packets;
963 printf("%6zu packets to schedule\n", total_pkts);
964 printf("%6zu bytes in total\n", total_len);
965 printf("Running! Hang up with ^C!\n\n");
966 fflush(stdout);
969 sock = pf_socket();
971 if (slow)
972 xmit_slowpath_or_die(ctx, cpu);
973 else
974 xmit_fastpath_or_die(ctx, cpu);
976 close(sock);
978 cleanup_packets();
981 static unsigned int generate_srand_seed(void)
983 int fd;
984 unsigned int seed;
986 fd = open("/dev/urandom", O_RDONLY);
987 if (fd < 0)
988 return time(0);
990 read_or_die(fd, &seed, sizeof(seed));
992 close(fd);
993 return seed;
996 int main(int argc, char **argv)
998 bool slow = false, invoke_cpp = false, reseed = true;
999 int c, opt_index, i, j, vals[4] = {0}, irq;
1000 char *confname = NULL, *ptr;
1001 unsigned long cpus_tmp;
1002 unsigned long long tx_packets, tx_bytes;
1003 struct ctx ctx;
1005 fmemset(&ctx, 0, sizeof(ctx));
1006 ctx.cpus = get_number_cpus_online();
1007 ctx.uid = getuid();
1008 ctx.gid = getgid();
1010 while ((c = getopt_long(argc, argv, short_options, long_options,
1011 &opt_index)) != EOF) {
1012 switch (c) {
1013 case 'h':
1014 help();
1015 break;
1016 case 'v':
1017 version();
1018 break;
1019 case 'e':
1020 example();
1021 break;
1022 case 'p':
1023 invoke_cpp = true;
1024 break;
1025 case 'V':
1026 ctx.verbose = true;
1027 break;
1028 case 'P':
1029 cpus_tmp = strtoul(optarg, NULL, 0);
1030 if (cpus_tmp > 0 && cpus_tmp < ctx.cpus)
1031 ctx.cpus = cpus_tmp;
1032 break;
1033 case 'd':
1034 case 'o':
1035 ctx.device = xstrndup(optarg, IFNAMSIZ);
1036 break;
1037 case 'r':
1038 ctx.rand = true;
1039 break;
1040 case 's':
1041 slow = true;
1042 ctx.cpus = 1;
1043 ctx.smoke_test = true;
1044 ctx.rhost = xstrdup(optarg);
1045 break;
1046 case 'R':
1047 ctx.rfraw = true;
1048 break;
1049 case 'J':
1050 ctx.jumbo_support = true;
1051 break;
1052 case 'c':
1053 case 'i':
1054 confname = xstrdup(optarg);
1055 if (!strncmp("-", confname, strlen("-")))
1056 ctx.cpus = 1;
1057 break;
1058 case 'u':
1059 ctx.uid = strtoul(optarg, NULL, 0);
1060 ctx.enforce = true;
1061 break;
1062 case 'g':
1063 ctx.gid = strtoul(optarg, NULL, 0);
1064 ctx.enforce = true;
1065 break;
1066 case 'k':
1067 ctx.kpull = strtoul(optarg, NULL, 0);
1068 break;
1069 case 'E':
1070 seed = strtoul(optarg, NULL, 0);
1071 reseed = false;
1072 break;
1073 case 'n':
1074 ctx.num = strtoul(optarg, NULL, 0);
1075 break;
1076 case 't':
1077 slow = true;
1078 ctx.gap = strtoul(optarg, NULL, 0);
1079 if (ctx.gap > 0)
1080 /* Fall back to single core to not
1081 * mess up correct timing. We are slow
1082 * anyway!
1084 ctx.cpus = 1;
1085 break;
1086 case 'S':
1087 ptr = optarg;
1088 ctx.reserve_size = 0;
1090 for (j = i = strlen(optarg); i > 0; --i) {
1091 if (!isdigit(optarg[j - i]))
1092 break;
1093 ptr++;
1096 if (!strncmp(ptr, "KiB", strlen("KiB")))
1097 ctx.reserve_size = 1 << 10;
1098 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1099 ctx.reserve_size = 1 << 20;
1100 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1101 ctx.reserve_size = 1 << 30;
1102 else
1103 panic("Syntax error in ring size param!\n");
1104 *ptr = 0;
1106 ctx.reserve_size *= strtol(optarg, NULL, 0);
1107 break;
1108 case '?':
1109 switch (optopt) {
1110 case 'd':
1111 case 'c':
1112 case 'n':
1113 case 'S':
1114 case 's':
1115 case 'P':
1116 case 'o':
1117 case 'E':
1118 case 'i':
1119 case 'k':
1120 case 'u':
1121 case 'g':
1122 case 't':
1123 panic("Option -%c requires an argument!\n",
1124 optopt);
1125 default:
1126 if (isprint(optopt))
1127 printf("Unknown option character `0x%X\'!\n", optopt);
1128 die();
1130 default:
1131 break;
1135 if (argc < 5)
1136 help();
1137 if (ctx.device == NULL)
1138 panic("No networking device given!\n");
1139 if (confname == NULL)
1140 panic("No configuration file given!\n");
1141 if (device_mtu(ctx.device) == 0)
1142 panic("This is no networking device!\n");
1143 if (!ctx.rfraw && device_up_and_running(ctx.device) == 0)
1144 panic("Networking device not running!\n");
1146 register_signal(SIGINT, signal_handler);
1147 register_signal(SIGHUP, signal_handler);
1148 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1150 set_system_socket_memory(vals, array_size(vals));
1151 xlockme();
1153 if (ctx.rfraw) {
1154 ctx.device_trans = xstrdup(ctx.device);
1155 xfree(ctx.device);
1157 enter_rfmon_mac80211(ctx.device_trans, &ctx.device);
1158 sleep(0);
1161 irq = device_irq_number(ctx.device);
1162 device_set_irq_affinity_list(irq, 0, ctx.cpus - 1);
1164 if (ctx.num > 0 && ctx.num <= ctx.cpus)
1165 ctx.cpus = 1;
1167 stats = setup_shared_var(ctx.cpus);
1169 for (i = 0; i < ctx.cpus; i++) {
1170 pid_t pid = fork();
1172 switch (pid) {
1173 case 0:
1174 if (reseed)
1175 seed = generate_srand_seed();
1176 srand(seed);
1178 cpu_affinity(i);
1179 main_loop(&ctx, confname, slow, i, invoke_cpp);
1181 goto thread_out;
1182 case -1:
1183 panic("Cannot fork processes!\n");
1187 for (i = 0; i < ctx.cpus; i++) {
1188 int status;
1190 wait(&status);
1191 if (WEXITSTATUS(status) == EXIT_FAILURE)
1192 die();
1195 if (ctx.rfraw)
1196 leave_rfmon_mac80211(ctx.device_trans, ctx.device);
1198 reset_system_socket_memory(vals, array_size(vals));
1200 for (i = 0, tx_packets = tx_bytes = 0; i < ctx.cpus; i++) {
1201 while ((__get_state(i) & CPU_STATS_STATE_RES) == 0)
1202 sched_yield();
1204 tx_packets += stats[i].tx_packets;
1205 tx_bytes += stats[i].tx_bytes;
1208 fflush(stdout);
1209 printf("\n");
1210 printf("\r%12llu packets outgoing\n", tx_packets);
1211 printf("\r%12llu bytes outgoing\n", tx_bytes);
1212 for (i = 0; i < ctx.cpus; i++) {
1213 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1214 stats[i].tv_sec, stats[i].tv_usec, i,
1215 stats[i].tx_packets);
1218 thread_out:
1219 xunlockme();
1220 destroy_shared_var(stats, ctx.cpus);
1222 free(ctx.device);
1223 free(ctx.device_trans);
1224 free(ctx.rhost);
1225 free(confname);
1227 return 0;