trafgen: use bool for flags
[netsniff-ng.git] / src / trafgen.c
blobeda756e2b8a68e05ead2e53af4ec77bd96dc1a8c
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/stat.h>
32 #include <sys/time.h>
33 #include <signal.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <time.h>
38 #include <net/ethernet.h>
40 #include "xmalloc.h"
41 #include "die.h"
42 #include "mac80211.h"
43 #include "xutils.h"
44 #include "xio.h"
45 #include "built_in.h"
46 #include "trafgen_conf.h"
47 #include "tprintf.h"
48 #include "ring_tx.h"
50 struct ctx {
51 bool rand, rfraw, jumbo_support, verbose;
52 unsigned long kpull, num, gap, reserve_size;
53 char *device, *device_trans;
54 int cpu;
57 sig_atomic_t sigint = 0;
59 struct packet *packets = NULL;
60 size_t plen = 0;
62 struct packet_dyn *packet_dyn = NULL;
63 size_t dlen = 0;
65 static const char *short_options = "d:c:n:t:vJhS:HQb:B:rk:i:o:VRA";
66 static const struct option long_options[] = {
67 {"dev", required_argument, NULL, 'd'},
68 {"out", required_argument, NULL, 'o'},
69 {"in", required_argument, NULL, 'i'},
70 {"conf", required_argument, NULL, 'c'},
71 {"num", required_argument, NULL, 'n'},
72 {"gap", required_argument, NULL, 't'},
73 {"ring-size", required_argument, NULL, 'S'},
74 {"bind-cpu", required_argument, NULL, 'b'},
75 {"unbind-cpu", required_argument, NULL, 'B'},
76 {"kernel-pull", required_argument, NULL, 'k'},
77 {"jumbo-support", no_argument, NULL, 'J'},
78 {"rfraw", no_argument, NULL, 'R'},
79 {"rand", no_argument, NULL, 'r'},
80 {"prio-high", no_argument, NULL, 'H'},
81 {"notouch-irq", no_argument, NULL, 'Q'},
82 {"verbose", no_argument, NULL, 'V'},
83 {"no-sock-mem", no_argument, NULL, 'A'},
84 {"version", no_argument, NULL, 'v'},
85 {"help", no_argument, NULL, 'h'},
86 {NULL, 0, NULL, 0}
89 static int sock;
91 static struct itimerval itimer;
93 static unsigned long interval = TX_KERNEL_PULL_INT;
95 #define set_system_socket_memory(vals) \
96 do { \
97 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
98 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
99 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
100 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
101 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
102 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
103 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
104 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
105 } while (0)
107 #define reset_system_socket_memory(vals) \
108 do { \
109 set_system_socket_mem(sock_rmem_max, vals[0]); \
110 set_system_socket_mem(sock_rmem_def, vals[1]); \
111 set_system_socket_mem(sock_wmem_max, vals[2]); \
112 set_system_socket_mem(sock_wmem_def, vals[3]); \
113 } while (0)
115 static void signal_handler(int number)
117 switch (number) {
118 case SIGINT:
119 sigint = 1;
120 case SIGHUP:
121 default:
122 break;
126 static void timer_elapsed(int number)
128 itimer.it_interval.tv_sec = 0;
129 itimer.it_interval.tv_usec = interval;
131 itimer.it_value.tv_sec = 0;
132 itimer.it_value.tv_usec = interval;
134 pull_and_flush_tx_ring(sock);
135 setitimer(ITIMER_REAL, &itimer, NULL);
138 static void header(void)
140 printf("%s%s%s\n", colorize_start(bold), "trafgen " VERSION_STRING, colorize_end());
143 static void help(void)
145 printf("\ntrafgen %s, zero-copy network packet generator\n", VERSION_STRING);
146 puts("http://www.netsniff-ng.org\n\n"
147 "Usage: trafgen [options]\n"
148 "Options:\n"
149 /* " -o|-d|--out|--dev <netdev|pcap> Networking Device i.e., eth0 or pcap\n" */
150 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
151 " -i|-c|--in|--conf <cfg-file> Packet configuration file\n"
152 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
153 " Default TX slot: 2048Byte\n"
154 " -R|--rfraw Inject raw 802.11 frames\n"
155 " -n|--num <uint> Number of packets until exit\n"
156 " `-- 0 Loop until interrupt (default)\n"
157 " `- n Send n packets and done\n"
158 " -r|--rand Randomize packet selection process\n"
159 " Instead of a round robin selection\n"
160 " -t|--gap <uint> Interpacket gap in us (approx)\n"
161 " -A|--no-sock-mem Don't tune core socket memory\n"
162 " -S|--ring-size <size> Manually set ring size to <size>:\n"
163 " mmap space in KB/MB/GB, e.g. \'10MB\'\n"
164 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
165 " Default is 10us where the TX_RING\n"
166 " is populated with payload from uspace\n"
167 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
168 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
169 " -H|--prio-high Make this high priority process\n"
170 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
171 " -V|--verbose Be more verbose\n"
172 " -v|--version Show version\n"
173 " -h|--help Guess what?!\n\n"
174 "Examples:\n"
175 " See trafgen.txf for configuration file examples.\n"
176 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n"
177 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf --bind-cpu 0 -A -V\n"
178 " trafgen --out eth0 --in trafgen.txf --bind-cpu 0\n"
179 /* " trafgen --out test.pcap --in trafgen.txf --bind-cpu 0\n" */
180 " trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n"
181 " trafgen --dev eth0 --conf trafgen.txf --rand --num 1400000 -k1000\n"
182 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n\n"
183 "Note:\n"
184 " This tool is targeted for network developers! You should\n"
185 " be aware of what you are doing and what these options above\n"
186 " mean! Only use this tool in an isolated LAN that you own!\n\n"
187 "Please report bugs to <bugs@netsniff-ng.org>\n"
188 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
189 "Swiss federal institute of technology (ETH Zurich)\n"
190 "License: GNU GPL version 2.0\n"
191 "This is free software: you are free to change and redistribute it.\n"
192 "There is NO WARRANTY, to the extent permitted by law.\n");
193 die();
196 static void version(void)
198 printf("\ntrafgen %s, zero-copy network packet generator\n", VERSION_STRING);
199 puts("http://www.netsniff-ng.org\n\n"
200 "Please report bugs to <bugs@netsniff-ng.org>\n"
201 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
202 "Swiss federal institute of technology (ETH Zurich)\n"
203 "License: GNU GPL version 2.0\n"
204 "This is free software: you are free to change and redistribute it.\n"
205 "There is NO WARRANTY, to the extent permitted by law.\n");
206 die();
209 static inline void apply_counter(int counter_id)
211 int j;
212 size_t counter_max = packet_dyn[counter_id].clen;
214 for (j = 0; j < counter_max; ++j) {
215 uint8_t val;
216 struct counter *counter;
218 counter = &packet_dyn[counter_id].cnt[j];
219 val = counter->val - counter->min;
221 switch (counter->type) {
222 case TYPE_INC:
223 val = (val + counter->inc) % (counter->max - counter->min + 1);
224 break;
225 case TYPE_DEC:
226 val = (val - counter->inc) % (counter->min - counter->max + 1);
227 break;
228 default:
229 bug();
232 counter->val = val + counter->min;
233 packets[counter_id].payload[counter->off] = val;
237 static inline void apply_randomizer(int rand_id)
239 int j;
240 size_t rand_max = packet_dyn[rand_id].rlen;
242 for (j = 0; j < rand_max; ++j) {
243 uint8_t val;
244 struct randomizer *randomizer;
246 val = (uint8_t) rand();
248 randomizer = &packet_dyn[rand_id].rnd[j];
249 randomizer->val = val;
251 packets[rand_id].payload[randomizer->off] = val;
255 static void xmit_precheck(const struct ctx *ctx)
257 int i;
258 size_t mtu;
260 if (!ctx)
261 panic("Panic, invalid args for TX trigger!\n");
262 if (plen == 0 || plen != dlen)
263 panic("Panic, invalid args for TX trigger!\n");
264 if (!ctx->rfraw && !device_up_and_running(ctx->device))
265 panic("Device not up and running!\n");
266 for (mtu = device_mtu(ctx->device), i = 0; i < plen; ++i) {
267 if (packets[i].len > mtu + 14)
268 panic("Device MTU < than your packet size!\n");
269 if (packets[i].len <= 14)
270 panic("Device packet size too short!\n");
274 static void xmit_slowpath_or_die(struct ctx *ctx)
276 int ret;
277 unsigned long num = 1, i = 0;
278 struct timeval start, end, diff;
279 unsigned long long tx_bytes = 0, tx_packets = 0;
280 struct sockaddr_ll saddr = {
281 .sll_family = PF_PACKET,
282 .sll_halen = ETH_ALEN,
285 if (ctx->rfraw) {
286 ctx->device_trans = xstrdup(ctx->device);
287 xfree(ctx->device);
289 enter_rfmon_mac80211(ctx->device_trans, &ctx->device);
292 if (ctx->num > 0)
293 num = ctx->num;
295 if (ctx->verbose) {
296 if (ctx->rand)
297 printf("Note: randomized output makes trafgen slower!\n");
298 printf("MD: TX sendto %s %luus", ctx->rand ? "RND" : "RR", ctx->gap);
299 if (ctx->rfraw)
300 printf(" 802.11 raw via %s", ctx->device);
301 printf("\n\n");
303 printf("Running! Hang up with ^C!\n\n");
304 fflush(stdout);
306 saddr.sll_ifindex = device_ifindex(ctx->device);
308 bug_on(gettimeofday(&start, NULL));
310 while (likely(sigint == 0) && likely(num > 0)) {
311 apply_counter(i);
312 apply_randomizer(i);
314 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
315 (struct sockaddr *) &saddr, sizeof(saddr));
316 if (ret < 0)
317 whine("sendto error!\n");
319 tx_bytes += packets[i].len;
320 tx_packets++;
322 if (!ctx->rand) {
323 i++;
324 atomic_cmp_swp(&i, plen, 0);
325 } else {
326 i = rand() % plen;
329 if (ctx->num > 0)
330 num--;
332 if (ctx->gap > 0)
333 usleep(ctx->gap);
336 bug_on(gettimeofday(&end, NULL));
337 diff = tv_subtract(end, start);
339 if (ctx->rfraw)
340 leave_rfmon_mac80211(ctx->device_trans, ctx->device);
342 fflush(stdout);
343 printf("\n");
344 printf("\r%12llu packets outgoing\n", tx_packets);
345 printf("\r%12llu bytes outgoing\n", tx_bytes);
346 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
350 static void xmit_fastpath_or_die(struct ctx *ctx)
352 int irq, ifindex;
353 uint8_t *out = NULL;
354 unsigned int it = 0;
355 unsigned long num = 1, i = 0, size;
356 struct ring tx_ring;
357 struct frame_map *hdr;
358 struct timeval start, end, diff;
359 unsigned long long tx_bytes = 0, tx_packets = 0;
361 if (ctx->rfraw) {
362 ctx->device_trans = xstrdup(ctx->device);
363 xfree(ctx->device);
365 enter_rfmon_mac80211(ctx->device_trans, &ctx->device);
368 fmemset(&tx_ring, 0, sizeof(tx_ring));
370 ifindex = device_ifindex(ctx->device);
371 size = ring_size(ctx->device, ctx->reserve_size);
373 set_sock_prio(sock, 512);
374 set_packet_loss_discard(sock);
376 setup_tx_ring_layout(sock, &tx_ring, size, ctx->jumbo_support);
377 create_tx_ring(sock, &tx_ring, ctx->verbose);
378 mmap_tx_ring(sock, &tx_ring);
379 alloc_tx_ring_frames(&tx_ring);
380 bind_tx_ring(sock, &tx_ring, ifindex);
382 if (ctx->cpu >= 0 && ifindex > 0) {
383 irq = device_irq_number(ctx->device);
384 device_bind_irq_to_cpu(irq, ctx->cpu);
386 if (ctx->verbose)
387 printf("IRQ: %s:%d > CPU%d\n",
388 ctx->device, irq, ctx->cpu);
391 if (ctx->kpull)
392 interval = ctx->kpull;
393 if (ctx->num > 0)
394 num = ctx->num;
396 if (ctx->verbose) {
397 if (ctx->rand)
398 printf("Note: randomized output makes trafgen slower!\n");
399 printf("MD: TX fastpath %s %luus", ctx->rand ? "RND" : "RR", interval);
400 if (ctx->rfraw)
401 printf(" 802.11 raw via %s", ctx->device);
402 printf("\n\n");
404 printf("Running! Hang up with ^C!\n\n");
405 fflush(stdout);
407 itimer.it_interval.tv_sec = 0;
408 itimer.it_interval.tv_usec = interval;
410 itimer.it_value.tv_sec = 0;
411 itimer.it_value.tv_usec = interval;
413 setitimer(ITIMER_REAL, &itimer, NULL);
415 bug_on(gettimeofday(&start, NULL));
417 while (likely(sigint == 0) && likely(num > 0)) {
418 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) && likely(num > 0)) {
419 hdr = tx_ring.frames[it].iov_base;
421 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
422 * sizeof(struct sockaddr_ll); */
423 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
425 hdr->tp_h.tp_snaplen = packets[i].len;
426 hdr->tp_h.tp_len = packets[i].len;
428 apply_counter(i);
429 apply_randomizer(i);
431 fmemcpy(out, packets[i].payload, packets[i].len);
433 tx_bytes += packets[i].len;
434 tx_packets++;
436 if (!ctx->rand) {
437 i++;
438 if (i >= plen)
439 i = 0;
440 } else
441 i = rand() % plen;
443 kernel_may_pull_from_tx(&hdr->tp_h);
445 it++;
446 if (it >= tx_ring.layout.tp_frame_nr)
447 it = 0;
449 if (ctx->num > 0)
450 num--;
452 if (unlikely(sigint == 1))
453 break;
457 bug_on(gettimeofday(&end, NULL));
458 diff = tv_subtract(end, start);
460 destroy_tx_ring(sock, &tx_ring);
462 if (ctx->rfraw)
463 leave_rfmon_mac80211(ctx->device_trans, ctx->device);
465 fflush(stdout);
466 printf("\n");
467 printf("\r%12llu packets outgoing\n", tx_packets);
468 printf("\r%12llu bytes outgoing\n", tx_bytes);
469 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
472 static void main_loop(struct ctx *ctx, char *confname, bool slow)
474 compile_packets(confname, ctx->verbose);
475 xmit_precheck(ctx);
477 sock = pf_socket();
479 if (slow)
480 xmit_slowpath_or_die(ctx);
481 else
482 xmit_fastpath_or_die(ctx);
484 close(sock);
486 cleanup_packets();
489 int main(int argc, char **argv)
491 int c, opt_index, i, j, vals[4] = {0};
492 char *confname = NULL, *ptr;
493 bool prio_high = false, setsockmem = true, slow = false;
494 struct ctx ctx;
496 srand(time(NULL));
498 fmemset(&ctx, 0, sizeof(ctx));
499 ctx.cpu = -1;
501 while ((c = getopt_long(argc, argv, short_options, long_options,
502 &opt_index)) != EOF) {
503 switch (c) {
504 case 'h':
505 help();
506 break;
507 case 'v':
508 version();
509 break;
510 case 'V':
511 ctx.verbose = true;
512 break;
513 case 'd':
514 case 'o':
515 ctx.device = xstrndup(optarg, IFNAMSIZ);
516 break;
517 case 'r':
518 ctx.rand = true;
519 break;
520 case 'R':
521 ctx.rfraw = true;
522 break;
523 case 'J':
524 ctx.jumbo_support = true;
525 break;
526 case 'c':
527 case 'i':
528 confname = xstrdup(optarg);
529 break;
530 case 'k':
531 ctx.kpull = atol(optarg);
532 break;
533 case 'n':
534 ctx.num = atol(optarg);
535 break;
536 case 't':
537 slow = true;
538 ctx.gap = atol(optarg);
539 break;
540 case 'A':
541 setsockmem = false;
542 break;
543 case 'S':
544 ptr = optarg;
545 ctx.reserve_size = 0;
547 for (j = i = strlen(optarg); i > 0; --i) {
548 if (!isdigit(optarg[j - i]))
549 break;
550 ptr++;
553 if (!strncmp(ptr, "KB", strlen("KB")))
554 ctx.reserve_size = 1 << 10;
555 else if (!strncmp(ptr, "MB", strlen("MB")))
556 ctx.reserve_size = 1 << 20;
557 else if (!strncmp(ptr, "GB", strlen("GB")))
558 ctx.reserve_size = 1 << 30;
559 else
560 panic("Syntax error in ring size param!\n");
561 *ptr = 0;
563 ctx.reserve_size *= strtol(optarg, NULL, 0);
564 break;
565 case 'b':
566 set_cpu_affinity(optarg, 0);
567 /* Take the first CPU for rebinding the IRQ */
568 if (ctx.cpu != -2)
569 ctx.cpu = strtol(optarg, NULL, 0);
570 break;
571 case 'B':
572 set_cpu_affinity(optarg, 1);
573 break;
574 case 'H':
575 prio_high = true;
576 break;
577 case 'Q':
578 ctx.cpu = -2;
579 break;
580 case '?':
581 switch (optopt) {
582 case 'd':
583 case 'c':
584 case 'n':
585 case 'S':
586 case 'b':
587 case 'o':
588 case 'i':
589 case 'k':
590 case 'B':
591 case 't':
592 panic("Option -%c requires an argument!\n",
593 optopt);
594 default:
595 if (isprint(optopt))
596 whine("Unknown option character "
597 "`0x%X\'!\n", optopt);
598 die();
600 default:
601 break;
605 if (argc < 5)
606 help();
607 if (ctx.device == NULL)
608 panic("No networking device given!\n");
609 if (confname == NULL)
610 panic("No configuration file given!\n");
611 if (device_mtu(ctx.device) == 0)
612 panic("This is no networking device!\n");
613 if (!ctx.rfraw && device_up_and_running(ctx.device) == 0)
614 panic("Networking device not running!\n");
616 register_signal(SIGINT, signal_handler);
617 register_signal(SIGHUP, signal_handler);
618 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
620 header();
622 if (prio_high) {
623 set_proc_prio(get_default_proc_prio());
624 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
627 if (setsockmem)
628 set_system_socket_memory(vals);
630 main_loop(&ctx, confname, slow);
632 if (setsockmem)
633 reset_system_socket_memory(vals);
635 free(ctx.device);
636 free(ctx.device_trans);
637 free(confname);
639 return 0;