proto_80211_mac_hdr.c: complete tclas element
[netsniff-ng.git] / src / trafgen.c
blobe8447a0c938c93052c8ec4bc760bb5ceb2fc814e
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("\ntrafgen %s, zero-copy network packet generator\n", VERSION_STRING);
142 puts("http://www.netsniff-ng.org\n\n"
143 "Usage: trafgen [options]\n"
144 "Options:\n"
145 /* " -o|-d|--out|--dev <netdev|pcap> Networking Device i.e., eth0 or pcap\n" */
146 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
147 " -i|-c|--in|--conf <cfg-file> Packet configuration file\n"
148 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
149 " Default TX slot: 2048Byte\n"
150 " -R|--rfraw Inject raw 802.11 frames\n"
151 " -n|--num <uint> Number of packets until exit\n"
152 " `-- 0 Loop until interrupt (default)\n"
153 " `- n Send n packets and done\n"
154 " -r|--rand Randomize packet selection process\n"
155 " Instead of a round robin selection\n"
156 " -t|--gap <uint> Interpacket gap in us (approx)\n"
157 " -A|--no-sock-mem Don't tune core socket memory\n"
158 " -S|--ring-size <size> Manually set ring size to <size>:\n"
159 " mmap space in KB/MB/GB, e.g. \'10MB\'\n"
160 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
161 " Default is 10us where the TX_RING\n"
162 " is populated with payload from uspace\n"
163 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
164 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
165 " -H|--prio-high Make this high priority process\n"
166 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
167 " -v|--version Show version\n"
168 " -h|--help Guess what?!\n\n"
169 "Examples:\n"
170 " See trafgen.txf for configuration file examples.\n"
171 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n"
172 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf --bind-cpu 0 -A\n"
173 " trafgen --out eth0 --in trafgen.txf --bind-cpu 0\n"
174 /* " trafgen --out test.pcap --in trafgen.txf --bind-cpu 0\n" */
175 " trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n"
176 " trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n\n"
177 "Note:\n"
178 " This tool is targeted for network developers! You should\n"
179 " be aware of what you are doing and what these options above\n"
180 " mean! Only use this tool in an isolated LAN that you own!\n\n"
181 "Please report bugs to <bugs@netsniff-ng.org>\n"
182 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
183 "Swiss federal institute of technology (ETH Zurich)\n"
184 "License: GNU GPL version 2.0\n"
185 "This is free software: you are free to change and redistribute it.\n"
186 "There is NO WARRANTY, to the extent permitted by law.\n");
187 die();
190 static void version(void)
192 printf("\ntrafgen %s, zero-copy network packet generator\n", VERSION_STRING);
193 puts("http://www.netsniff-ng.org\n\n"
194 "Please report bugs to <bugs@netsniff-ng.org>\n"
195 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
196 "Swiss federal institute of technology (ETH Zurich)\n"
197 "License: GNU GPL version 2.0\n"
198 "This is free software: you are free to change and redistribute it.\n"
199 "There is NO WARRANTY, to the extent permitted by law.\n");
200 die();
203 static inline void apply_counter(int i)
205 int j;
207 for (j = 0; j < packet_dyns[i].counter_len; ++j) {
208 uint8_t val;
209 struct counter *counter = &packet_dyns[i].counter[j];
211 val = counter->val;
212 val -= counter->min;
214 if (counter->type == TYPE_INC)
215 val = (val + counter->inc) %
216 (counter->max - counter->min + 1);
217 else
218 val = (val - counter->inc) %
219 (counter->min - counter->max + 1);
221 val += counter->min;
222 counter->val = val;
224 packets[i].payload[counter->off] = val;
228 static inline void apply_randomizer(int i)
230 int j;
232 for (j = 0; j < packet_dyns[i].randomizer_len; ++j) {
233 uint8_t val = (uint8_t) mt_rand_int32();
234 struct randomizer *randomizer = &packet_dyns[i].randomizer[j];
236 randomizer->val = val;
237 packets[i].payload[randomizer->off] = val;
241 static void tx_precheck(struct mode *mode)
243 int i, mtu;
245 if (!mode)
246 panic("Panic over invalid args for TX trigger!\n");
247 if (packets_len == 0 || packets_len != packet_dyn_len)
248 panic("Panic over invalid args for TX trigger!\n");
249 if (!mode->rfraw && !device_up_and_running(mode->device))
250 panic("Device not up and running!\n");
252 mtu = device_mtu(mode->device);
254 for (i = 0; i < packets_len; ++i) {
255 if (packets[i].len > mtu + 14)
256 panic("Device MTU < than your packet size!\n");
257 if (packets[i].len <= 14)
258 panic("Device packet size too short!\n");
262 static void tx_slowpath_or_die(struct mode *mode)
264 int ifindex, ret;
265 unsigned int i;
266 struct sockaddr_ll s_addr;
267 unsigned long num = 1;
268 struct timeval start, end, diff;
270 tx_precheck(mode);
272 sock = pf_socket();
274 if (mode->rfraw) {
275 mode->device_trans = xstrdup(mode->device);
276 xfree(mode->device);
278 enter_rfmon_mac80211(mode->device_trans, &mode->device);
281 ifindex = device_ifindex(mode->device);
283 if (mode->num > 0)
284 num = mode->num;
285 if (mode->rand)
286 printf("Note: randomizes output makes trafgen slower!\n");
288 printf("MD: TX slowpath %s %luus", mode->rand ? "RND" : "RR", mode->gap);
289 if (mode->rfraw)
290 printf(" 802.11 raw via %s", mode->device);
291 printf("\n\n");
292 printf("Running! Hang up with ^C!\n\n");
294 fmemset(&s_addr, 0, sizeof(s_addr));
295 s_addr.sll_family = PF_PACKET;
296 s_addr.sll_halen = ETH_ALEN;
297 s_addr.sll_ifindex = ifindex;
299 i = 0;
301 bug_on(gettimeofday(&start, NULL));
303 while (likely(sigint == 0) && likely(num > 0)) {
304 apply_counter(i);
305 apply_randomizer(i);
307 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
308 (struct sockaddr *) &s_addr, sizeof(s_addr));
309 if (ret < 0)
310 whine("sendto error!\n");
312 mode->stats.tx_bytes += packets[i].len;
313 mode->stats.tx_packets++;
315 if (mode->rand) {
316 i = mt_rand_int32() % packets_len;
317 } else {
318 i++;
319 atomic_cmp_swp(&i, packets_len, 0);
322 if (mode->num > 0)
323 num--;
325 usleep(mode->gap);
328 bug_on(gettimeofday(&end, NULL));
329 diff = tv_subtract(end, start);
331 if (mode->rfraw)
332 leave_rfmon_mac80211(mode->device_trans, mode->device);
334 close(sock);
336 fflush(stdout);
337 printf("\n");
338 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
339 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
340 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
343 static void tx_fastpath_or_die(struct mode *mode)
345 int irq, ifindex;
346 unsigned int i, size, it = 0;
347 unsigned long num = 1;
348 uint8_t *out = NULL;
349 struct ring tx_ring;
350 struct frame_map *hdr;
351 struct timeval start, end, diff;
353 tx_precheck(mode);
355 sock = pf_socket();
357 fmemset(&tx_ring, 0, sizeof(tx_ring));
359 if (mode->rfraw) {
360 mode->device_trans = xstrdup(mode->device);
361 xfree(mode->device);
363 enter_rfmon_mac80211(mode->device_trans, &mode->device);
366 ifindex = device_ifindex(mode->device);
367 size = ring_size(mode->device, mode->reserve_size);
369 set_sock_prio(sock, 512);
370 set_packet_loss_discard(sock);
371 setup_tx_ring_layout(sock, &tx_ring, size, mode->jumbo_support);
372 create_tx_ring(sock, &tx_ring);
373 mmap_tx_ring(sock, &tx_ring);
374 alloc_tx_ring_frames(&tx_ring);
375 bind_tx_ring(sock, &tx_ring, ifindex);
377 if (mode->cpu >= 0 && ifindex > 0) {
378 irq = device_irq_number(mode->device);
379 device_bind_irq_to_cpu(mode->cpu, irq);
380 printf("IRQ: %s:%d > CPU%d\n", mode->device, irq,
381 mode->cpu);
384 if (mode->kpull)
385 interval = mode->kpull;
386 if (mode->num > 0)
387 num = mode->num;
388 if (mode->rand)
389 printf("Note: randomizes output makes trafgen slower!\n");
391 printf("MD: TX fastpath %s %luus", mode->rand ? "RND" : "RR", interval);
392 if (mode->rfraw)
393 printf(" 802.11 raw via %s", mode->device);
394 printf("\n\n");
395 printf("Running! Hang up with ^C!\n\n");
397 itimer.it_interval.tv_sec = 0;
398 itimer.it_interval.tv_usec = interval;
399 itimer.it_value.tv_sec = 0;
400 itimer.it_value.tv_usec = interval;
401 setitimer(ITIMER_REAL, &itimer, NULL);
403 i = 0;
405 bug_on(gettimeofday(&start, NULL));
407 while (likely(sigint == 0) && likely(num > 0)) {
408 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) &&
409 likely(num > 0)) {
410 hdr = tx_ring.frames[it].iov_base;
412 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
413 * sizeof(struct sockaddr_ll); */
414 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN -
415 sizeof(struct sockaddr_ll);
417 hdr->tp_h.tp_snaplen = packets[i].len;
418 hdr->tp_h.tp_len = packets[i].len;
420 apply_counter(i);
421 apply_randomizer(i);
423 fmemcpy(out, packets[i].payload, packets[i].len);
425 mode->stats.tx_bytes += packets[i].len;
426 mode->stats.tx_packets++;
428 if (mode->rand) {
429 i = mt_rand_int32() % packets_len;
430 } else {
431 i++;
432 atomic_cmp_swp(&i, packets_len, 0);
435 kernel_may_pull_from_tx(&hdr->tp_h);
436 next_slot_prewr(&it, &tx_ring);
438 if (mode->num > 0)
439 num--;
440 if (unlikely(sigint == 1))
441 break;
445 bug_on(gettimeofday(&end, NULL));
446 diff = tv_subtract(end, start);
448 destroy_tx_ring(sock, &tx_ring);
450 if (mode->rfraw)
451 leave_rfmon_mac80211(mode->device_trans, mode->device);
453 close(sock);
455 fflush(stdout);
456 printf("\n");
457 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
458 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
459 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
462 static void main_loop(struct mode *mode, char *confname)
464 compile_packets(confname, mode->verbose);
466 if (mode->gap > 0)
467 tx_slowpath_or_die(mode);
468 else
469 tx_fastpath_or_die(mode);
471 cleanup_packets();
474 int main(int argc, char **argv)
476 int c, opt_index, i, j;
477 int vals[4] = {0};
478 char *confname = NULL, *ptr;
479 bool prio_high = false;
480 bool setsockmem = true;
481 struct mode mode;
483 fmemset(&mode, 0, sizeof(mode));
484 mode.cpu = CPU_UNKNOWN;
485 mode.gap = 0;
486 mode.num = 0;
488 while ((c = getopt_long(argc, argv, short_options, long_options,
489 &opt_index)) != EOF) {
490 switch (c) {
491 case 'h':
492 help();
493 break;
494 case 'v':
495 version();
496 break;
497 case 'V':
498 mode.verbose = 1;
499 break;
500 case 'd':
501 case 'o':
502 mode.device = xstrndup(optarg, IFNAMSIZ);
503 break;
504 case 'r':
505 mode.rand = 1;
506 break;
507 case 'R':
508 mode.rfraw = 1;
509 break;
510 case 'J':
511 mode.jumbo_support = 1;
512 break;
513 case 'c':
514 case 'i':
515 confname = xstrdup(optarg);
516 break;
517 case 'k':
518 mode.kpull = atol(optarg);
519 break;
520 case 'n':
521 mode.num = atol(optarg);
522 break;
523 case 't':
524 mode.gap = atol(optarg);
525 break;
526 case 'A':
527 setsockmem = false;
528 break;
529 case 'S':
530 ptr = optarg;
531 mode.reserve_size = 0;
533 for (j = i = strlen(optarg); i > 0; --i) {
534 if (!isdigit(optarg[j - i]))
535 break;
536 ptr++;
539 if (!strncmp(ptr, "KB", strlen("KB")))
540 mode.reserve_size = 1 << 10;
541 else if (!strncmp(ptr, "MB", strlen("MB")))
542 mode.reserve_size = 1 << 20;
543 else if (!strncmp(ptr, "GB", strlen("GB")))
544 mode.reserve_size = 1 << 30;
545 else
546 panic("Syntax error in ring size param!\n");
547 *ptr = 0;
549 mode.reserve_size *= atoi(optarg);
550 break;
551 case 'b':
552 set_cpu_affinity(optarg, 0);
553 /* Take the first CPU for rebinding the IRQ */
554 if (mode.cpu != CPU_NOTOUCH)
555 mode.cpu = atoi(optarg);
556 break;
557 case 'B':
558 set_cpu_affinity(optarg, 1);
559 break;
560 case 'H':
561 prio_high = true;
562 break;
563 case 'Q':
564 mode.cpu = CPU_NOTOUCH;
565 break;
566 case '?':
567 switch (optopt) {
568 case 'd':
569 case 'c':
570 case 'n':
571 case 'S':
572 case 'b':
573 case 'o':
574 case 'i':
575 case 'k':
576 case 'B':
577 case 't':
578 panic("Option -%c requires an argument!\n",
579 optopt);
580 default:
581 if (isprint(optopt))
582 whine("Unknown option character "
583 "`0x%X\'!\n", optopt);
584 die();
586 default:
587 break;
591 if (argc < 5)
592 help();
593 if (mode.device == NULL)
594 panic("No networking device given!\n");
595 if (confname == NULL)
596 panic("No configuration file given!\n");
597 if (device_mtu(mode.device) == 0)
598 panic("This is no networking device!\n");
599 if (!mode.rfraw && device_up_and_running(mode.device) == 0)
600 panic("Networking device not running!\n");
602 register_signal(SIGINT, signal_handler);
603 register_signal(SIGHUP, signal_handler);
604 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
606 header();
608 if (prio_high == true) {
609 set_proc_prio(get_default_proc_prio());
610 set_sched_status(get_default_sched_policy(),
611 get_default_sched_prio());
614 if (setsockmem == true) {
615 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX)
616 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX);
617 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF)
618 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF);
619 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX)
620 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX);
621 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF)
622 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF);
625 main_loop(&mode, confname);
627 if (setsockmem == true) {
628 set_system_socket_mem(sock_rmem_max, vals[0]);
629 set_system_socket_mem(sock_rmem_def, vals[1]);
630 set_system_socket_mem(sock_wmem_max, vals[2]);
631 set_system_socket_mem(sock_wmem_def, vals[3]);
634 free(mode.device);
635 free(mode.device_trans);
636 free(confname);
638 return 0;