trafgen: improve comment in -e example
[netsniff-ng.git] / src / trafgen.c
blob0e8818fb8aca6f763d015f9817894da37c02d79f
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>
50 #include "xmalloc.h"
51 #include "die.h"
52 #include "mac80211.h"
53 #include "xutils.h"
54 #include "xio.h"
55 #include "built_in.h"
56 #include "trafgen_conf.h"
57 #include "tprintf.h"
58 #include "ring_tx.h"
59 #include "csum.h"
61 struct ctx {
62 bool rand, rfraw, jumbo_support, verbose, smoke_test;
63 unsigned long kpull, num, gap, reserve_size, cpus;
64 struct sockaddr_in dest;
65 char *device, *device_trans, *rhost;
68 struct cpu_stats {
69 unsigned long tv_sec, tv_usec;
70 unsigned long long tx_packets, tx_bytes;
71 unsigned long long cf_packets, cf_bytes;
72 unsigned long long cd_packets;
73 sig_atomic_t state;
76 sig_atomic_t sigint = 0;
78 struct packet *packets = NULL;
79 size_t plen = 0;
81 struct packet_dyn *packet_dyn = NULL;
82 size_t dlen = 0;
84 static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRsP:eE:";
85 static const struct option long_options[] = {
86 {"dev", required_argument, NULL, 'd'},
87 {"out", required_argument, NULL, 'o'},
88 {"in", required_argument, NULL, 'i'},
89 {"conf", required_argument, NULL, 'c'},
90 {"num", required_argument, NULL, 'n'},
91 {"gap", required_argument, NULL, 't'},
92 {"cpus", required_argument, NULL, 'P'},
93 {"ring-size", required_argument, NULL, 'S'},
94 {"kernel-pull", required_argument, NULL, 'k'},
95 {"smoke-test", required_argument, NULL, 's'},
96 {"seed", required_argument, NULL, 'E'},
97 {"jumbo-support", no_argument, NULL, 'J'},
98 {"rfraw", no_argument, NULL, 'R'},
99 {"rand", no_argument, NULL, 'r'},
100 {"verbose", no_argument, NULL, 'V'},
101 {"version", no_argument, NULL, 'v'},
102 {"example", no_argument, NULL, 'e'},
103 {"help", no_argument, NULL, 'h'},
104 {NULL, 0, NULL, 0}
107 static int sock;
109 static struct itimerval itimer;
111 static unsigned long interval = TX_KERNEL_PULL_INT;
113 static struct cpu_stats *stats;
115 unsigned int seed;
117 #define CPU_STATS_STATE_CFG 1
118 #define CPU_STATS_STATE_CHK 2
119 #define CPU_STATS_STATE_RES 4
121 #define set_system_socket_memory(vals) \
122 do { \
123 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
124 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
125 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
126 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
127 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
128 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
129 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
130 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
131 } while (0)
133 #define reset_system_socket_memory(vals) \
134 do { \
135 set_system_socket_mem(sock_rmem_max, vals[0]); \
136 set_system_socket_mem(sock_rmem_def, vals[1]); \
137 set_system_socket_mem(sock_wmem_max, vals[2]); \
138 set_system_socket_mem(sock_wmem_def, vals[3]); \
139 } while (0)
141 #ifndef ICMP_FILTER
142 # define ICMP_FILTER 1
144 struct icmp_filter {
145 __u32 data;
147 #endif
149 static void signal_handler(int number)
151 switch (number) {
152 case SIGINT:
153 sigint = 1;
154 case SIGHUP:
155 default:
156 break;
160 static void timer_elapsed(int number)
162 itimer.it_interval.tv_sec = 0;
163 itimer.it_interval.tv_usec = interval;
165 itimer.it_value.tv_sec = 0;
166 itimer.it_value.tv_usec = interval;
168 pull_and_flush_tx_ring(sock);
169 setitimer(ITIMER_REAL, &itimer, NULL);
172 static void header(void)
174 printf("%s%s%s\n", colorize_start(bold), "trafgen " VERSION_STRING, colorize_end());
177 static void help(void)
179 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
180 puts("http://www.netsniff-ng.org\n\n"
181 "Usage: trafgen [options]\n"
182 "Options:\n"
183 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
184 " -i|-c|--in|--conf <cfg-file> Packet configuration file\n"
185 " -J|--jumbo-support Support 64KB Super Jumbo Frames (def: 2048B)\n"
186 " -R|--rfraw Inject raw 802.11 frames\n"
187 " -s|--smoke-test <ipv4-receiver> Test if machine survived packet\n"
188 " -n|--num <uint> Number of packets until exit (def: 0)\n"
189 " -r|--rand Randomize packet selection (def: round robin)\n"
190 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
191 " -t|--gap <uint> Interpacket gap in us (approx)\n"
192 " -S|--ring-size <size> Manually set mmap size (KB/MB/GB): e.g.\'10MB\'\n"
193 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
194 " -E|--seed <uint> Manually set srand(3) seed\n"
195 " -V|--verbose Be more verbose\n"
196 " -v|--version Show version\n"
197 " -e|--example Show built-in packet config example\n"
198 " -h|--help Guess what?!\n\n"
199 "Examples:\n"
200 " See trafgen.txf for configuration file examples.\n"
201 " trafgen --dev eth0 --conf trafgen.cfg\n"
202 " trafgen --dev eth0 --conf trafgen.cfg --smoke-test 10.0.0.1\n"
203 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
204 " trafgen --dev eth0 --conf trafgen.cfg --rand --gap 1000\n"
205 " trafgen --dev eth0 --conf trafgen.cfg --rand --num 1400000 -k1000\n\n"
206 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
207 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
208 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
209 " Run packet only on CPU1-2: cpu(1:2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
210 "Note:\n"
211 " Smoke test example: machine A, 10.0.0.2 (trafgen) is directly\n"
212 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
213 " we assume the kernel crashed, thus we print the packet and quit.\n"
214 " In case you find a ping-of-death, please mention trafgen in your\n"
215 " commit message of the fix!\n\n"
216 " This tool is targeted for network developers! You should\n"
217 " be aware of what you are doing and what these options above\n"
218 " mean! Only use this tool in an isolated LAN that you own!\n\n"
219 "Please report bugs to <bugs@netsniff-ng.org>\n"
220 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
221 "Swiss federal institute of technology (ETH Zurich)\n"
222 "License: GNU GPL version 2.0\n"
223 "This is free software: you are free to change and redistribute it.\n"
224 "There is NO WARRANTY, to the extent permitted by law.\n");
225 die();
228 static void example(void)
230 const char *e =
231 "# Note: dynamic elements make trafgen slower!\n"
232 "\n"
233 "{\n"
234 " # MAC Destination\n"
235 " fill(0xff, 6),\n"
236 " # MAC Source\n"
237 " 0x00, 0x02, 0xb3, drnd(3),\n"
238 " # IPv4 Protocol\n"
239 " c16(0x0800),\n"
240 " # IPv4 Version, IHL, TOS\n"
241 " 0b01000101, 0,\n"
242 " # IPv4 Total Len\n"
243 " c16(59),\n"
244 " # IPv4 Ident\n"
245 " drnd(2),\n"
246 " # IPv4 Flags, Frag Off\n"
247 " 0b01000000, 0,\n"
248 " # IPv4 TTL\n"
249 " 64,\n"
250 " # Proto TCP\n"
251 " 0x06,\n"
252 " # IPv4 Checksum (IP header from, to)\n"
253 " csumip(14, 33),\n"
254 " # Source IP\n"
255 " drnd(4),\n"
256 " # Dest IP\n"
257 " drnd(4),\n"
258 " # TCP Source Port\n"
259 " drnd(2),\n"
260 " # TCP Dest Port\n"
261 " c16(80),\n"
262 " # TCP Sequence Number\n"
263 " drnd(4),\n"
264 " # TCP Ackn. Number\n"
265 " c32(0),\n"
266 " # TCP Header length + TCP SYN/ECN Flag\n"
267 " c16((0x8 << 12) | (1 << 1) | (1 << 6))\n"
268 " # Window Size\n"
269 " c16(16),\n"
270 " # TCP Checksum (offset IP, offset TCP)\n"
271 " csumtcp(14, 34),\n"
272 " # TCP Options\n"
273 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
274 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
275 " # Data blob\n"
276 " \"gotcha!\",\n"
277 "}";
278 puts(e);
279 die();
282 static void version(void)
284 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
285 puts("http://www.netsniff-ng.org\n\n"
286 "Please report bugs to <bugs@netsniff-ng.org>\n"
287 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
288 "Swiss federal institute of technology (ETH Zurich)\n"
289 "License: GNU GPL version 2.0\n"
290 "This is free software: you are free to change and redistribute it.\n"
291 "There is NO WARRANTY, to the extent permitted by law.\n");
292 die();
295 static void apply_counter(int counter_id)
297 int j, i = counter_id;
298 size_t counter_max = packet_dyn[i].clen;
300 for (j = 0; j < counter_max; ++j) {
301 uint8_t val;
302 struct counter *counter = &packet_dyn[i].cnt[j];
304 val = counter->val - counter->min;
306 switch (counter->type) {
307 case TYPE_INC:
308 val = (val + counter->inc) % (counter->max - counter->min + 1);
309 break;
310 case TYPE_DEC:
311 val = (val - counter->inc) % (counter->min - counter->max + 1);
312 break;
313 default:
314 bug();
317 counter->val = val + counter->min;
318 packets[i].payload[counter->off] = val;
322 static void apply_randomizer(int rand_id)
324 int j, i = rand_id;
325 size_t rand_max = packet_dyn[i].rlen;
327 for (j = 0; j < rand_max; ++j) {
328 uint8_t val = (uint8_t) rand();
329 struct randomizer *randomizer = &packet_dyn[i].rnd[j];
331 packets[i].payload[randomizer->off] = val;
335 /* Taken and modified from tcpdump, Copyright belongs to them! */
337 struct cksum_vec {
338 const u8 *ptr;
339 int len;
342 #define ADDCARRY(x) \
343 do { if ((x) > 65535) \
344 (x) -= 65535; \
345 } while (0)
347 #define REDUCE \
348 do { \
349 l_util.l = sum; \
350 sum = l_util.s[0] + l_util.s[1]; \
351 ADDCARRY(sum); \
352 } while (0)
354 static u16 __in_cksum(const struct cksum_vec *vec, int veclen)
356 register const u16 *w;
357 register int sum = 0, mlen = 0;
358 int byte_swapped = 0;
359 union {
360 u8 c[2];
361 u16 s;
362 } s_util;
363 union {
364 u16 s[2];
365 u32 l;
366 } l_util;
368 for (; veclen != 0; vec++, veclen--) {
369 if (vec->len == 0)
370 continue;
372 w = (const u16 *) (void *) vec->ptr;
374 if (mlen == -1) {
375 s_util.c[1] = *(const u8 *) w;
376 sum += s_util.s;
377 w = (const u16 *) (void *) ((const u8 *) w + 1);
378 mlen = vec->len - 1;
379 } else
380 mlen = vec->len;
382 if ((1 & (unsigned long) w) && (mlen > 0)) {
383 REDUCE;
384 sum <<= 8;
385 s_util.c[0] = *(const u8 *) w;
386 w = (const u16 *) (void *) ((const u8 *) w + 1);
387 mlen--;
388 byte_swapped = 1;
391 while ((mlen -= 32) >= 0) {
392 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
393 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
394 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
395 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
396 w += 16;
399 mlen += 32;
401 while ((mlen -= 8) >= 0) {
402 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
403 w += 4;
406 mlen += 8;
408 if (mlen == 0 && byte_swapped == 0)
409 continue;
411 REDUCE;
413 while ((mlen -= 2) >= 0) {
414 sum += *w++;
417 if (byte_swapped) {
418 REDUCE;
419 sum <<= 8;
420 byte_swapped = 0;
422 if (mlen == -1) {
423 s_util.c[1] = *(const u8 *) w;
424 sum += s_util.s;
425 mlen = 0;
426 } else
427 mlen = -1;
428 } else if (mlen == -1)
429 s_util.c[0] = *(const u8 *) w;
432 if (mlen == -1) {
433 s_util.c[1] = 0;
434 sum += s_util.s;
437 REDUCE;
439 return (~sum & 0xffff);
442 static u16 p4_csum(const struct ip *ip, const u8 *data, u16 len,
443 u8 next_proto)
445 struct cksum_vec vec[2];
446 struct pseudo_hdr {
447 u32 src;
448 u32 dst;
449 u8 mbz;
450 u8 proto;
451 u16 len;
452 } ph;
454 memset(&ph, 0, sizeof(ph));
455 ph.len = htons(len);
456 ph.mbz = 0;
457 ph.proto = next_proto;
458 ph.src = ip->ip_src.s_addr;
459 ph.dst = ip->ip_dst.s_addr;
461 vec[0].ptr = (const u8 *) (void *) &ph;
462 vec[0].len = sizeof(ph);
464 vec[1].ptr = data;
465 vec[1].len = len;
467 return __in_cksum(vec, 2);
470 static void apply_csum16(int csum_id)
472 int j, i = csum_id;
473 size_t csum_max = packet_dyn[i].slen;
475 for (j = 0; j < csum_max; ++j) {
476 uint16_t sum = 0;
477 struct csum16 *csum = &packet_dyn[i].csum[j];
479 fmemset(&packets[i].payload[csum->off], 0, sizeof(sum));
481 switch (csum->which) {
482 case CSUM_IP:
483 if (csum->to >= packets[i].len)
484 csum->to = packets[i].len - 1;
485 sum = calc_csum(packets[i].payload + csum->from,
486 csum->to - csum->from + 1, 0);
487 break;
488 case CSUM_UDP:
489 sum = p4_csum((void *) packets[i].payload + csum->from,
490 packets[i].payload + csum->to,
491 (packets[i].len - csum->to),
492 IPPROTO_UDP);
493 break;
494 case CSUM_TCP:
495 sum = p4_csum((void *) packets[i].payload + csum->from,
496 packets[i].payload + csum->to,
497 (packets[i].len - csum->to),
498 IPPROTO_TCP);
499 break;
502 fmemcpy(&packets[i].payload[csum->off], &sum, sizeof(sum));
506 static struct cpu_stats *setup_shared_var(unsigned long cpus)
508 int fd;
509 char zbuff[cpus * sizeof(struct cpu_stats)], file[256];
510 struct cpu_stats *buff;
512 fmemset(zbuff, 0, sizeof(zbuff));
513 slprintf(file, sizeof(file), ".tmp_mmap.%u", (unsigned int) rand());
515 fd = creat(file, S_IRUSR | S_IWUSR);
516 bug_on(fd < 0);
517 close(fd);
519 fd = open_or_die_m(file, O_RDWR | O_CREAT | O_TRUNC,
520 S_IRUSR | S_IWUSR);
521 write_or_die(fd, zbuff, sizeof(zbuff));
523 buff = (void *) mmap(0, sizeof(zbuff), PROT_READ | PROT_WRITE,
524 MAP_SHARED, fd, 0);
525 if (buff == (void *) -1)
526 panic("Cannot setup shared variable!\n");
528 close(fd);
529 unlink(file);
531 memset(buff, 0, sizeof(zbuff));
533 return buff;
536 static void destroy_shared_var(void *buff, unsigned long cpus)
538 munmap(buff, cpus * sizeof(struct cpu_stats));
541 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
543 int i;
545 printf("{");
546 for (i = 0; i < len; ++i) {
547 if (i % 15 == 0)
548 printf("\n ");
549 printf("0x%02x, ", payload[i]);
551 printf("\n}\n");
552 fflush(stdout);
555 static inline unsigned short csum(unsigned short *buf, int nwords)
557 unsigned long sum;
559 for (sum = 0; nwords > 0; nwords--)
560 sum += *buf++;
561 sum = (sum >> 16) + (sum & 0xffff);
562 sum += (sum >> 16);
564 return ~sum;
567 static int xmit_smoke_setup(struct ctx *ctx)
569 int icmp_sock, ret, ttl = 64;
570 struct icmp_filter filter;
572 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
573 if (icmp_sock < 0)
574 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
576 filter.data = ~(1 << ICMP_ECHOREPLY);
578 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
579 if (ret < 0)
580 panic("Cannot install filter!\n");
582 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
583 if (ret < 0)
584 panic("Cannot set TTL!\n");
586 memset(&ctx->dest, 0, sizeof(ctx->dest));
587 ctx->dest.sin_family = AF_INET;
588 ctx->dest.sin_port = 0;
590 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
591 if (ret < 0)
592 panic("Cannot resolv address!\n");
594 return icmp_sock;
597 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
599 int ret, i, probes = 5;
600 short ident, cnt = 1;
601 uint8_t outpack[512], *data;
602 struct icmphdr *icmp;
603 struct iphdr *ip;
604 size_t len = sizeof(*icmp) + 56;
605 struct sockaddr_in from;
606 socklen_t from_len;
607 struct pollfd fds = {
608 .fd = icmp_sock,
609 .events = POLLIN,
612 while (probes-- > 0) {
613 ident = htons((short) rand());
615 memset(outpack, 0, sizeof(outpack));
616 icmp = (void *) outpack;
617 icmp->type = ICMP_ECHO;
618 icmp->code = 0;
619 icmp->checksum = 0;
620 icmp->un.echo.id = ident;
621 icmp->un.echo.sequence = htons(cnt++);
623 data = ((uint8_t *) outpack + sizeof(*icmp));
624 for (i = 0; i < 56; ++i)
625 data[i] = (uint8_t) rand();
627 icmp->checksum = csum((unsigned short *) outpack,
628 len / sizeof(unsigned short));
630 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
631 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
632 if (unlikely(ret != len))
633 panic("Cannot send out probe: %s!\n", strerror(errno));
635 ret = poll(&fds, 1, 500);
636 if (ret < 0)
637 panic("Poll failed!\n");
639 if (fds.revents & POLLIN) {
640 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
641 (struct sockaddr *) &from, &from_len);
642 if (unlikely(ret <= 0))
643 panic("Probe receive failed!\n");
644 if (unlikely(from_len != sizeof(ctx->dest)))
645 continue;
646 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
647 continue;
648 if (unlikely(ret < sizeof(*ip) + sizeof(*icmp)))
649 continue;
650 ip = (void *) outpack;
651 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > ret))
652 continue;
653 icmp = (void *) outpack + ip->ihl * 4;
654 if (unlikely(icmp->un.echo.id != ident))
655 continue;
657 return 0;
661 return -1;
664 static void xmit_slowpath_or_die(struct ctx *ctx, int cpu)
666 int ret, icmp_sock = -1;
667 unsigned long num = 1, i = 0;
668 struct timeval start, end, diff;
669 unsigned long long tx_bytes = 0, tx_packets = 0;
670 struct packet_dyn *pktd;
671 struct sockaddr_ll saddr = {
672 .sll_family = PF_PACKET,
673 .sll_halen = ETH_ALEN,
674 .sll_ifindex = device_ifindex(ctx->device),
677 if (ctx->num > 0)
678 num = ctx->num;
680 if (ctx->smoke_test)
681 icmp_sock = xmit_smoke_setup(ctx);
683 bug_on(gettimeofday(&start, NULL));
685 while (likely(sigint == 0) && likely(num > 0)) {
686 pktd = &packet_dyn[i];
687 if (pktd->clen + pktd->rlen + pktd->slen) {
688 apply_counter(i);
689 apply_randomizer(i);
690 apply_csum16(i);
692 retry:
693 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
694 (struct sockaddr *) &saddr, sizeof(saddr));
695 if (unlikely(ret < 0)) {
696 if (errno == ENOBUFS) {
697 sched_yield();
698 goto retry;
701 panic("Sendto error: %s!\n", strerror(errno));
704 tx_bytes += packets[i].len;
705 tx_packets++;
707 if (ctx->smoke_test) {
708 ret = xmit_smoke_probe(icmp_sock, ctx);
709 if (unlikely(ret < 0)) {
710 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
711 printf(" Remote host seems to be unresponsive to ICMP pings!\n");
712 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
713 i, seed);
715 dump_trafgen_snippet(packets[i].payload, packets[i].len);
716 break;
720 if (!ctx->rand) {
721 i++;
722 if (i >= plen)
723 i = 0;
724 } else
725 i = rand() % plen;
727 if (ctx->num > 0)
728 num--;
730 if (ctx->gap > 0)
731 usleep(ctx->gap);
734 bug_on(gettimeofday(&end, NULL));
735 diff = tv_subtract(end, start);
737 if (ctx->smoke_test)
738 close(icmp_sock);
740 stats[cpu].tx_packets = tx_packets;
741 stats[cpu].tx_bytes = tx_bytes;
742 stats[cpu].tv_sec = diff.tv_sec;
743 stats[cpu].tv_usec = diff.tv_usec;
745 stats[cpu].state |= CPU_STATS_STATE_RES;
748 static void xmit_fastpath_or_die(struct ctx *ctx, int cpu)
750 int ifindex = device_ifindex(ctx->device);
751 uint8_t *out = NULL;
752 unsigned int it = 0;
753 unsigned long num = 1, i = 0, size;
754 struct ring tx_ring;
755 struct frame_map *hdr;
756 struct timeval start, end, diff;
757 struct packet_dyn *pktd;
758 unsigned long long tx_bytes = 0, tx_packets = 0;
760 fmemset(&tx_ring, 0, sizeof(tx_ring));
762 size = ring_size(ctx->device, ctx->reserve_size);
764 set_sock_prio(sock, 512);
765 set_packet_loss_discard(sock);
767 setup_tx_ring_layout(sock, &tx_ring, size, ctx->jumbo_support);
768 create_tx_ring(sock, &tx_ring, ctx->verbose);
769 mmap_tx_ring(sock, &tx_ring);
770 alloc_tx_ring_frames(&tx_ring);
771 bind_tx_ring(sock, &tx_ring, ifindex);
773 if (ctx->kpull)
774 interval = ctx->kpull;
775 if (ctx->num > 0)
776 num = ctx->num;
778 itimer.it_interval.tv_sec = 0;
779 itimer.it_interval.tv_usec = interval;
781 itimer.it_value.tv_sec = 0;
782 itimer.it_value.tv_usec = interval;
784 setitimer(ITIMER_REAL, &itimer, NULL);
786 bug_on(gettimeofday(&start, NULL));
788 while (likely(sigint == 0) && likely(num > 0)) {
789 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) && likely(num > 0)) {
790 hdr = tx_ring.frames[it].iov_base;
792 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
793 * sizeof(struct sockaddr_ll); */
794 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
796 hdr->tp_h.tp_snaplen = packets[i].len;
797 hdr->tp_h.tp_len = packets[i].len;
799 pktd = &packet_dyn[i];
800 if (pktd->clen + pktd->rlen + pktd->slen) {
801 apply_counter(i);
802 apply_randomizer(i);
803 apply_csum16(i);
806 fmemcpy(out, packets[i].payload, packets[i].len);
808 tx_bytes += packets[i].len;
809 tx_packets++;
811 if (!ctx->rand) {
812 i++;
813 if (i >= plen)
814 i = 0;
815 } else
816 i = rand() % plen;
818 kernel_may_pull_from_tx(&hdr->tp_h);
820 it++;
821 if (it >= tx_ring.layout.tp_frame_nr)
822 it = 0;
824 if (ctx->num > 0)
825 num--;
827 if (unlikely(sigint == 1))
828 break;
832 bug_on(gettimeofday(&end, NULL));
833 diff = tv_subtract(end, start);
835 destroy_tx_ring(sock, &tx_ring);
837 stats[cpu].tx_packets = tx_packets;
838 stats[cpu].tx_bytes = tx_bytes;
839 stats[cpu].tv_sec = diff.tv_sec;
840 stats[cpu].tv_usec = diff.tv_usec;
842 stats[cpu].state |= CPU_STATS_STATE_RES;
845 static inline void __set_state(int cpu, sig_atomic_t s)
847 stats[cpu].state = s;
850 static inline sig_atomic_t __get_state(int cpu)
852 return stats[cpu].state;
855 static unsigned long __wait_and_sum_others(struct ctx *ctx, int cpu)
857 int i;
858 unsigned long total;
860 for (i = 0, total = plen; i < ctx->cpus; i++) {
861 if (i == cpu)
862 continue;
864 while ((__get_state(i) & CPU_STATS_STATE_CFG) == 0 &&
865 sigint == 0)
866 sched_yield();
868 total += stats[i].cf_packets;
871 return total;
874 static void __correct_global_delta(struct ctx *ctx, int cpu, unsigned long orig)
876 int i, cpu_sel;
877 unsigned long total;
878 long long delta_correction = 0;
880 for (i = 0, total = ctx->num; i < ctx->cpus; i++) {
881 if (i == cpu)
882 continue;
884 while ((__get_state(i) & CPU_STATS_STATE_CHK) == 0 &&
885 sigint == 0)
886 sched_yield();
888 total += stats[i].cd_packets;
891 if (total > orig)
892 delta_correction = -1 * ((long long) total - orig);
893 if (total < orig)
894 delta_correction = +1 * ((long long) orig - total);
896 for (cpu_sel = -1, i = 0; i < ctx->cpus; i++) {
897 if (stats[i].cd_packets > 0) {
898 if ((long long) stats[i].cd_packets +
899 delta_correction > 0) {
900 cpu_sel = i;
901 break;
906 if (cpu == cpu_sel)
907 ctx->num += delta_correction;
910 static void __set_state_cf(int cpu, unsigned long p, unsigned long b,
911 sig_atomic_t s)
913 stats[cpu].cf_packets = p;
914 stats[cpu].cf_bytes = b;
915 stats[cpu].state = s;
918 static void __set_state_cd(int cpu, unsigned long p, sig_atomic_t s)
920 stats[cpu].cd_packets = p;
921 stats[cpu].state = s;
924 static int xmit_packet_precheck(struct ctx *ctx, int cpu)
926 int i;
927 unsigned long plen_total, orig = ctx->num;
928 size_t mtu, total_len = 0;
930 bug_on(plen != dlen);
932 for (i = 0; i < plen; ++i)
933 total_len += packets[i].len;
935 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
936 plen_total = __wait_and_sum_others(ctx, cpu);
938 if (orig > 0) {
939 ctx->num = (unsigned long) nearbyint((1.0 * plen / plen_total) * orig);
941 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
942 CPU_STATS_STATE_CFG);
943 __correct_global_delta(ctx, cpu, orig);
946 if (plen == 0) {
947 __set_state(cpu, CPU_STATS_STATE_RES);
948 return -1;
951 for (mtu = device_mtu(ctx->device), i = 0; i < plen; ++i) {
952 if (packets[i].len > mtu + 14)
953 panic("Device MTU < than packet%d's size!\n", i);
954 if (packets[i].len <= 14)
955 panic("Packet%d's size too short!\n", i);
958 return 0;
961 static void main_loop(struct ctx *ctx, char *confname, bool slow, int cpu)
963 compile_packets(confname, ctx->verbose, cpu);
964 if (xmit_packet_precheck(ctx, cpu) < 0)
965 return;
967 if (cpu == 0) {
968 int i;
969 size_t total_len = 0, total_pkts = 0;
971 for (i = 0; i < ctx->cpus; ++i) {
972 total_len += stats[i].cf_bytes;
973 total_pkts += stats[i].cf_packets;
976 printf("%6zu packets to schedule\n", total_pkts);
977 printf("%6zu bytes in total\n", total_len);
978 printf("Running! Hang up with ^C!\n\n");
979 fflush(stdout);
982 sock = pf_socket();
984 if (slow)
985 xmit_slowpath_or_die(ctx, cpu);
986 else
987 xmit_fastpath_or_die(ctx, cpu);
989 close(sock);
991 cleanup_packets();
994 static unsigned int generate_srand_seed(void)
996 int fd;
997 unsigned int seed;
999 fd = open("/dev/random", O_RDONLY);
1000 if (fd < 0)
1001 return time(0);
1003 read_or_die(fd, &seed, sizeof(seed));
1005 close(fd);
1006 return seed;
1009 int main(int argc, char **argv)
1011 bool slow = false;
1012 int c, opt_index, i, j, vals[4] = {0}, irq;
1013 char *confname = NULL, *ptr;
1014 unsigned long cpus_tmp;
1015 unsigned long long tx_packets, tx_bytes;
1016 struct ctx ctx;
1018 setfsuid(getuid());
1019 setfsgid(getgid());
1021 fmemset(&ctx, 0, sizeof(ctx));
1022 ctx.cpus = get_number_cpus_online();
1024 seed = generate_srand_seed();
1026 while ((c = getopt_long(argc, argv, short_options, long_options,
1027 &opt_index)) != EOF) {
1028 switch (c) {
1029 case 'h':
1030 help();
1031 break;
1032 case 'v':
1033 version();
1034 break;
1035 case 'e':
1036 example();
1037 break;
1038 case 'V':
1039 ctx.verbose = true;
1040 break;
1041 case 'P':
1042 cpus_tmp = strtoul(optarg, NULL, 0);
1043 if (cpus_tmp > 0 && cpus_tmp < ctx.cpus)
1044 ctx.cpus = cpus_tmp;
1045 break;
1046 case 'd':
1047 case 'o':
1048 ctx.device = xstrndup(optarg, IFNAMSIZ);
1049 break;
1050 case 'r':
1051 ctx.rand = true;
1052 break;
1053 case 's':
1054 slow = true;
1055 ctx.cpus = 1;
1056 ctx.smoke_test = true;
1057 ctx.rhost = xstrdup(optarg);
1058 break;
1059 case 'R':
1060 ctx.rfraw = true;
1061 break;
1062 case 'J':
1063 ctx.jumbo_support = true;
1064 break;
1065 case 'c':
1066 case 'i':
1067 confname = xstrdup(optarg);
1068 break;
1069 case 'k':
1070 ctx.kpull = strtoul(optarg, NULL, 0);
1071 break;
1072 case 'E':
1073 seed = strtoul(optarg, NULL, 0);
1074 break;
1075 case 'n':
1076 ctx.num = strtoul(optarg, NULL, 0);
1077 break;
1078 case 't':
1079 slow = true;
1080 ctx.gap = strtoul(optarg, NULL, 0);
1081 if (ctx.gap > 0)
1082 /* Fall back to single core to not
1083 * mess up correct timing. We are slow
1084 * anyway!
1086 ctx.cpus = 1;
1087 break;
1088 case 'S':
1089 ptr = optarg;
1090 ctx.reserve_size = 0;
1092 for (j = i = strlen(optarg); i > 0; --i) {
1093 if (!isdigit(optarg[j - i]))
1094 break;
1095 ptr++;
1098 if (!strncmp(ptr, "KB", strlen("KB")))
1099 ctx.reserve_size = 1 << 10;
1100 else if (!strncmp(ptr, "MB", strlen("MB")))
1101 ctx.reserve_size = 1 << 20;
1102 else if (!strncmp(ptr, "GB", strlen("GB")))
1103 ctx.reserve_size = 1 << 30;
1104 else
1105 panic("Syntax error in ring size param!\n");
1106 *ptr = 0;
1108 ctx.reserve_size *= strtol(optarg, NULL, 0);
1109 break;
1110 case '?':
1111 switch (optopt) {
1112 case 'd':
1113 case 'c':
1114 case 'n':
1115 case 'S':
1116 case 's':
1117 case 'P':
1118 case 'o':
1119 case 'E':
1120 case 'i':
1121 case 'k':
1122 case 't':
1123 panic("Option -%c requires an argument!\n",
1124 optopt);
1125 default:
1126 if (isprint(optopt))
1127 whine("Unknown option character "
1128 "`0x%X\'!\n", optopt);
1129 die();
1131 default:
1132 break;
1136 if (argc < 5)
1137 help();
1138 if (ctx.device == NULL)
1139 panic("No networking device given!\n");
1140 if (confname == NULL)
1141 panic("No configuration file given!\n");
1142 if (device_mtu(ctx.device) == 0)
1143 panic("This is no networking device!\n");
1144 if (!ctx.rfraw && device_up_and_running(ctx.device) == 0)
1145 panic("Networking device not running!\n");
1147 register_signal(SIGINT, signal_handler);
1148 register_signal(SIGHUP, signal_handler);
1149 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1151 header();
1153 srand(seed);
1155 set_system_socket_memory(vals);
1157 if (ctx.rfraw) {
1158 ctx.device_trans = xstrdup(ctx.device);
1159 xfree(ctx.device);
1161 enter_rfmon_mac80211(ctx.device_trans, &ctx.device);
1162 sleep(0);
1165 irq = device_irq_number(ctx.device);
1166 device_set_irq_affinity_list(irq, 0, ctx.cpus - 1);
1168 if (ctx.num > 0 && ctx.num <= ctx.cpus)
1169 ctx.cpus = 1;
1171 stats = setup_shared_var(ctx.cpus);
1173 for (i = 0; i < ctx.cpus; i++) {
1174 pid_t pid = fork();
1176 switch (pid) {
1177 case 0:
1178 cpu_affinity(i);
1179 main_loop(&ctx, confname, slow, i);
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);
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 destroy_shared_var(stats, ctx.cpus);
1221 free(ctx.device);
1222 free(ctx.device_trans);
1223 free(ctx.rhost);
1224 free(confname);
1226 return 0;