trafgen: add header include
[netsniff-ng.git] / src / trafgen.c
blob50cdb3c7122536a6be41218209572704df8980cb
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 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 "mtrand.h"
49 #include "ring_tx.h"
51 struct stats {
52 unsigned long tx_bytes;
53 unsigned long tx_packets;
56 struct mode {
57 #define CPU_UNKNOWN -1
58 #define CPU_NOTOUCH -2
59 struct stats stats;
60 char *device;
61 char *device_trans;
62 int cpu;
63 int rand;
64 int rfraw;
65 unsigned long kpull;
66 /* 0 for automatic, > 0 for manual */
67 unsigned int reserve_size;
68 int jumbo_support;
69 int verbose;
70 unsigned long num;
71 unsigned long gap;
74 static int sock;
75 static struct itimerval itimer;
76 static unsigned long interval = TX_KERNEL_PULL_INT;
78 sig_atomic_t sigint = 0;
80 struct packet *packets = NULL;
81 unsigned int packets_len = 0;
83 struct packet_dynamics *packet_dyns = NULL;
84 unsigned int packet_dyn_len = 0;
86 static const char *short_options = "d:c:n:t:vJhS:HQb:B:rk:i:o:VRA";
87 static const struct option long_options[] = {
88 {"dev", required_argument, NULL, 'd'},
89 {"out", required_argument, NULL, 'o'},
90 {"in", required_argument, NULL, 'i'},
91 {"conf", required_argument, NULL, 'c'},
92 {"num", required_argument, NULL, 'n'},
93 {"gap", required_argument, NULL, 't'},
94 {"ring-size", required_argument, NULL, 'S'},
95 {"bind-cpu", required_argument, NULL, 'b'},
96 {"unbind-cpu", required_argument, NULL, 'B'},
97 {"kernel-pull", required_argument, NULL, 'k'},
98 {"jumbo-support", no_argument, NULL, 'J'},
99 {"rfraw", no_argument, NULL, 'R'},
100 {"rand", no_argument, NULL, 'r'},
101 {"prio-high", no_argument, NULL, 'H'},
102 {"notouch-irq", no_argument, NULL, 'Q'},
103 {"verbose", no_argument, NULL, 'V'},
104 {"no-sock-mem", no_argument, NULL, 'A'},
105 {"version", no_argument, NULL, 'v'},
106 {"help", no_argument, NULL, 'h'},
107 {NULL, 0, NULL, 0}
110 static void signal_handler(int number)
112 switch (number) {
113 case SIGINT:
114 sigint = 1;
115 break;
116 case SIGHUP:
117 default:
118 break;
122 static void timer_elapsed(int number)
124 itimer.it_interval.tv_sec = 0;
125 itimer.it_interval.tv_usec = interval;
126 itimer.it_value.tv_sec = 0;
127 itimer.it_value.tv_usec = interval;
129 pull_and_flush_tx_ring(sock);
130 setitimer(ITIMER_REAL, &itimer, NULL);
133 static void header(void)
135 printf("%s%s%s\n", colorize_start(bold), "trafgen "
136 VERSION_STRING, colorize_end());
139 static void help(void)
141 printf("\n%s %s, zero-copy network packet generator\n",
142 PROGNAME_STRING, VERSION_STRING);
143 puts("http://www.netsniff-ng.org\n\n"
144 "Usage: trafgen [options]\n"
145 "Options:\n"
146 /* " -o|-d|--out|--dev <netdev|pcap> Networking Device i.e., eth0 or pcap\n" */
147 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
148 " -i|-c|--in|--conf <cfg-file> Packet configuration file\n"
149 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
150 " Default TX slot: 2048Byte\n"
151 " -R|--rfraw Inject raw 802.11 frames\n"
152 " -n|--num <uint> Number of packets until exit\n"
153 " `-- 0 Loop until interrupt (default)\n"
154 " `- n Send n packets and done\n"
155 " -r|--rand Randomize packet selection process\n"
156 " Instead of a round robin selection\n"
157 " -t|--gap <uint> Interpacket gap in us (approx)\n"
158 " -A|--no-sock-mem Don't tune core socket memory\n"
159 " -S|--ring-size <size> Manually set ring size to <size>:\n"
160 " mmap space in KB/MB/GB, e.g. \'10MB\'\n"
161 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
162 " Default is 10us where the TX_RING\n"
163 " is populated with payload from uspace\n"
164 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
165 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
166 " -H|--prio-high Make this high priority process\n"
167 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
168 " -v|--version Show version\n"
169 " -h|--help Guess what?!\n\n"
170 "Examples:\n"
171 " See trafgen.txf for configuration file examples.\n"
172 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n"
173 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf --bind-cpu 0 -A\n"
174 " trafgen --out eth0 --in trafgen.txf --bind-cpu 0\n"
175 /* " trafgen --out test.pcap --in trafgen.txf --bind-cpu 0\n" */
176 " trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n"
177 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n\n"
178 "Note:\n"
179 " This tool is targeted for network developers! You should\n"
180 " be aware of what you are doing and what these options above\n"
181 " mean! Only use this tool in an isolated LAN that you own!\n\n"
182 "Please report bugs to <bugs@netsniff-ng.org>\n"
183 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
184 "Swiss federal institute of technology (ETH Zurich)\n"
185 "License: GNU GPL version 2.0\n"
186 "This is free software: you are free to change and redistribute it.\n"
187 "There is NO WARRANTY, to the extent permitted by law.\n");
188 die();
191 static void version(void)
193 printf("\n%s %s, zero-copy network packet generator\n",
194 PROGNAME_STRING, VERSION_STRING);
195 puts("http://www.netsniff-ng.org\n\n"
196 "Please report bugs to <bugs@netsniff-ng.org>\n"
197 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
198 "Swiss federal institute of technology (ETH Zurich)\n"
199 "License: GNU GPL version 2.0\n"
200 "This is free software: you are free to change and redistribute it.\n"
201 "There is NO WARRANTY, to the extent permitted by law.\n");
202 die();
205 static inline void apply_counter(int i)
207 int j;
209 for (j = 0; j < packet_dyns[i].counter_len; ++j) {
210 uint8_t val;
211 struct counter *counter = &packet_dyns[i].counter[j];
213 val = counter->val;
214 val -= counter->min;
216 if (counter->type == TYPE_INC)
217 val = (val + counter->inc) %
218 (counter->max - counter->min + 1);
219 else
220 val = (val - counter->inc) %
221 (counter->min - counter->max + 1);
223 val += counter->min;
224 counter->val = val;
226 packets[i].payload[counter->off] = val;
230 static inline void apply_randomizer(int i)
232 int j;
234 for (j = 0; j < packet_dyns[i].randomizer_len; ++j) {
235 uint8_t val = (uint8_t) mt_rand_int32();
236 struct randomizer *randomizer = &packet_dyns[i].randomizer[j];
238 randomizer->val = val;
239 packets[i].payload[randomizer->off] = val;
243 static void tx_precheck(struct mode *mode)
245 int i, mtu;
247 if (!mode)
248 panic("Panic over invalid args for TX trigger!\n");
249 if (packets_len == 0 || packets_len != packet_dyn_len)
250 panic("Panic over invalid args for TX trigger!\n");
251 if (!mode->rfraw && !device_up_and_running(mode->device))
252 panic("Device not up and running!\n");
254 mtu = device_mtu(mode->device);
256 for (i = 0; i < packets_len; ++i) {
257 if (packets[i].len > mtu + 14)
258 panic("Device MTU < than your packet size!\n");
259 if (packets[i].len <= 14)
260 panic("Device packet size too short!\n");
264 static void tx_slowpath_or_die(struct mode *mode)
266 int ifindex, ret;
267 unsigned int i;
268 struct sockaddr_ll s_addr;
269 unsigned long num = 1;
270 struct timeval start, end, diff;
272 tx_precheck(mode);
274 sock = pf_socket();
276 if (mode->rfraw) {
277 mode->device_trans = xstrdup(mode->device);
278 xfree(mode->device);
280 enter_rfmon_mac80211(mode->device_trans, &mode->device);
283 ifindex = device_ifindex(mode->device);
285 if (mode->num > 0)
286 num = mode->num;
287 if (mode->rand)
288 printf("Note: randomizes output makes trafgen slower!\n");
290 printf("MD: TX slowpath %s %luus", mode->rand ? "RND" : "RR", mode->gap);
291 if (mode->rfraw)
292 printf(" 802.11 raw via %s", mode->device);
293 printf("\n\n");
294 printf("Running! Hang up with ^C!\n\n");
296 fmemset(&s_addr, 0, sizeof(s_addr));
297 s_addr.sll_family = PF_PACKET;
298 s_addr.sll_halen = ETH_ALEN;
299 s_addr.sll_ifindex = ifindex;
301 i = 0;
303 bug_on(gettimeofday(&start, NULL));
305 while (likely(sigint == 0) && likely(num > 0)) {
306 apply_counter(i);
307 apply_randomizer(i);
309 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
310 (struct sockaddr *) &s_addr, sizeof(s_addr));
311 if (ret < 0)
312 whine("sendto error!\n");
314 mode->stats.tx_bytes += packets[i].len;
315 mode->stats.tx_packets++;
317 if (mode->rand) {
318 i = mt_rand_int32() % packets_len;
319 } else {
320 i++;
321 atomic_cmp_swp(&i, packets_len, 0);
324 if (mode->num > 0)
325 num--;
327 usleep(mode->gap);
330 bug_on(gettimeofday(&end, NULL));
331 diff = tv_subtract(end, start);
333 if (mode->rfraw)
334 leave_rfmon_mac80211(mode->device_trans, mode->device);
336 close(sock);
338 fflush(stdout);
339 printf("\n");
340 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
341 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
342 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
345 static void tx_fastpath_or_die(struct mode *mode)
347 int irq, ifindex;
348 unsigned int i, size, it = 0;
349 unsigned long num = 1;
350 uint8_t *out = NULL;
351 struct ring tx_ring;
352 struct frame_map *hdr;
353 struct timeval start, end, diff;
355 tx_precheck(mode);
357 sock = pf_socket();
359 fmemset(&tx_ring, 0, sizeof(tx_ring));
361 if (mode->rfraw) {
362 mode->device_trans = xstrdup(mode->device);
363 xfree(mode->device);
365 enter_rfmon_mac80211(mode->device_trans, &mode->device);
368 ifindex = device_ifindex(mode->device);
369 size = ring_size(mode->device, mode->reserve_size);
371 set_sock_prio(sock, 512);
372 set_packet_loss_discard(sock);
373 setup_tx_ring_layout(sock, &tx_ring, size, mode->jumbo_support);
374 create_tx_ring(sock, &tx_ring);
375 mmap_tx_ring(sock, &tx_ring);
376 alloc_tx_ring_frames(&tx_ring);
377 bind_tx_ring(sock, &tx_ring, ifindex);
379 if (mode->cpu >= 0 && ifindex > 0) {
380 irq = device_irq_number(mode->device);
381 device_bind_irq_to_cpu(mode->cpu, irq);
382 printf("IRQ: %s:%d > CPU%d\n", mode->device, irq,
383 mode->cpu);
386 if (mode->kpull)
387 interval = mode->kpull;
388 if (mode->num > 0)
389 num = mode->num;
390 if (mode->rand)
391 printf("Note: randomizes output makes trafgen slower!\n");
393 printf("MD: TX fastpath %s %luus", mode->rand ? "RND" : "RR", interval);
394 if (mode->rfraw)
395 printf(" 802.11 raw via %s", mode->device);
396 printf("\n\n");
397 printf("Running! Hang up with ^C!\n\n");
399 itimer.it_interval.tv_sec = 0;
400 itimer.it_interval.tv_usec = interval;
401 itimer.it_value.tv_sec = 0;
402 itimer.it_value.tv_usec = interval;
403 setitimer(ITIMER_REAL, &itimer, NULL);
405 i = 0;
407 bug_on(gettimeofday(&start, NULL));
409 while (likely(sigint == 0) && likely(num > 0)) {
410 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) &&
411 likely(num > 0)) {
412 hdr = tx_ring.frames[it].iov_base;
414 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
415 * sizeof(struct sockaddr_ll); */
416 out = ((uint8_t *) hdr) + TPACKET_HDRLEN -
417 sizeof(struct sockaddr_ll);
419 hdr->tp_h.tp_snaplen = packets[i].len;
420 hdr->tp_h.tp_len = packets[i].len;
422 apply_counter(i);
423 apply_randomizer(i);
425 fmemcpy(out, packets[i].payload, packets[i].len);
427 mode->stats.tx_bytes += packets[i].len;
428 mode->stats.tx_packets++;
430 if (mode->rand) {
431 i = mt_rand_int32() % packets_len;
432 } else {
433 i++;
434 atomic_cmp_swp(&i, packets_len, 0);
437 kernel_may_pull_from_tx(&hdr->tp_h);
438 next_slot_prewr(&it, &tx_ring);
440 if (mode->num > 0)
441 num--;
442 if (unlikely(sigint == 1))
443 break;
447 bug_on(gettimeofday(&end, NULL));
448 diff = tv_subtract(end, start);
450 destroy_tx_ring(sock, &tx_ring);
452 if (mode->rfraw)
453 leave_rfmon_mac80211(mode->device_trans, mode->device);
455 close(sock);
457 fflush(stdout);
458 printf("\n");
459 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
460 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
461 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
464 static void main_loop(struct mode *mode, char *confname)
466 compile_packets(confname, mode->verbose);
468 if (mode->gap > 0)
469 tx_slowpath_or_die(mode);
470 else
471 tx_fastpath_or_die(mode);
473 cleanup_packets();
476 int main(int argc, char **argv)
478 int c, opt_index, i, j;
479 int vals[4] = {0};
480 char *confname = NULL, *ptr;
481 bool prio_high = false;
482 bool setsockmem = true;
483 struct mode mode;
485 check_for_root_maybe_die();
487 fmemset(&mode, 0, sizeof(mode));
488 mode.cpu = CPU_UNKNOWN;
489 mode.gap = 0;
490 mode.num = 0;
492 while ((c = getopt_long(argc, argv, short_options, long_options,
493 &opt_index)) != EOF) {
494 switch (c) {
495 case 'h':
496 help();
497 break;
498 case 'v':
499 version();
500 break;
501 case 'V':
502 mode.verbose = 1;
503 break;
504 case 'd':
505 case 'o':
506 mode.device = xstrndup(optarg, IFNAMSIZ);
507 break;
508 case 'r':
509 mode.rand = 1;
510 break;
511 case 'R':
512 mode.rfraw = 1;
513 break;
514 case 'J':
515 mode.jumbo_support = 1;
516 break;
517 case 'c':
518 case 'i':
519 confname = xstrdup(optarg);
520 break;
521 case 'k':
522 mode.kpull = atol(optarg);
523 break;
524 case 'n':
525 mode.num = atol(optarg);
526 break;
527 case 't':
528 mode.gap = atol(optarg);
529 break;
530 case 'A':
531 setsockmem = false;
532 break;
533 case 'S':
534 ptr = optarg;
535 mode.reserve_size = 0;
537 for (j = i = strlen(optarg); i > 0; --i) {
538 if (!isdigit(optarg[j - i]))
539 break;
540 ptr++;
543 if (!strncmp(ptr, "KB", strlen("KB")))
544 mode.reserve_size = 1 << 10;
545 else if (!strncmp(ptr, "MB", strlen("MB")))
546 mode.reserve_size = 1 << 20;
547 else if (!strncmp(ptr, "GB", strlen("GB")))
548 mode.reserve_size = 1 << 30;
549 else
550 panic("Syntax error in ring size param!\n");
551 *ptr = 0;
553 mode.reserve_size *= atoi(optarg);
554 break;
555 case 'b':
556 set_cpu_affinity(optarg, 0);
557 /* Take the first CPU for rebinding the IRQ */
558 if (mode.cpu != CPU_NOTOUCH)
559 mode.cpu = atoi(optarg);
560 break;
561 case 'B':
562 set_cpu_affinity(optarg, 1);
563 break;
564 case 'H':
565 prio_high = true;
566 break;
567 case 'Q':
568 mode.cpu = CPU_NOTOUCH;
569 break;
570 case '?':
571 switch (optopt) {
572 case 'd':
573 case 'c':
574 case 'n':
575 case 'S':
576 case 'b':
577 case 'o':
578 case 'i':
579 case 'k':
580 case 'B':
581 case 't':
582 panic("Option -%c requires an argument!\n",
583 optopt);
584 default:
585 if (isprint(optopt))
586 whine("Unknown option character "
587 "`0x%X\'!\n", optopt);
588 die();
590 default:
591 break;
595 if (argc < 5)
596 help();
597 if (mode.device == NULL)
598 panic("No networking device given!\n");
599 if (confname == NULL)
600 panic("No configuration file given!\n");
601 if (device_mtu(mode.device) == 0)
602 panic("This is no networking device!\n");
603 if (!mode.rfraw && device_up_and_running(mode.device) == 0)
604 panic("Networking device not running!\n");
606 register_signal(SIGINT, signal_handler);
607 register_signal(SIGHUP, signal_handler);
608 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
610 header();
612 if (prio_high == true) {
613 set_proc_prio(get_default_proc_prio());
614 set_sched_status(get_default_sched_policy(),
615 get_default_sched_prio());
618 if (setsockmem == true) {
619 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX)
620 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX);
621 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF)
622 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF);
623 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX)
624 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX);
625 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF)
626 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF);
629 main_loop(&mode, confname);
631 if (setsockmem == true) {
632 set_system_socket_mem(sock_rmem_max, vals[0]);
633 set_system_socket_mem(sock_rmem_def, vals[1]);
634 set_system_socket_mem(sock_wmem_max, vals[2]);
635 set_system_socket_mem(sock_wmem_def, vals[3]);
638 free(mode.device);
639 free(mode.device_trans);
640 free(confname);
642 return 0;