trafgen: comment out unimplemented functions
[netsniff-ng.git] / src / trafgen.c
blob0dfd10833b6070582b6ae87294e5e97cbccbde19
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'.
26 =head1 NAME
28 trafgen - a high-performance zero-copy network packet generator
30 =head1 SYNOPSIS
32 trafgen [-d|--dev <netdev>][-c|--conf <file>][-J|--jumbo-support]
33 [-x|--interactive][-n|--num <uint>][-r|--rand][-t|--gap <usec>]
34 [-S|--ring-size <size>][-k|--kernel-pull <usec>][-b|--bind-cpu <cpu>]
35 [-B|--unbind-cpu <cpu>][-H|--prio-high][-Q|--notouch-irq][-v|--version]
36 [-h|--help]
38 =head1 DESCRIPTION
40 A high-performance network traffic generator that uses the zero-copy TX_RING
41 for network I/O. For instance, on comodity Gigabit hardware up to 1,488,095 pps
42 64 Byte pps have been achieved with trafgen.
44 =head1 OPTIONS
46 =over
48 =item trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0
50 Use packet configuration trafgen.txf, eth0 as transmission device and CPU0
51 for binding the process.
53 =back
55 =head1 OPTIONS
57 =over
59 =item -h|--help
61 Print help text and lists all options.
63 =item -v|--version
65 Print version.
67 =item -d|--dev <netdev>
69 Device for transmission i.e., eth0.
71 =item -c|--conf <conf>
73 Path to packet configuration file.
75 =item -x|--interactive
77 Start trafgen in interactive mode.
79 =item -J|--jumbo-support
81 Support for 64KB Super Jumbo Frames
83 =item -n|--num <uint>
85 Number of packets to generate before exiting.
86 0 means forever until SIGINT.
88 =item -r|--rand
90 Randomize packet selection process instead of round-robin.
92 =item -t|--gap <uint>
94 Interpacket gap in microseconds.
96 =item -S|--ring-size <size>
98 Manually set ring size to <size>: mmap space in KB/MB/GB.
100 =item -k|--kernel-pull <uint>
102 Kernel pull from user interval in microseconds.
103 Default value is 10 microseconds.
105 =item -b|--bind-cpu <cpu>
107 Bind to specific CPU (or CPU-range).
109 =item -B|--unbind-cpu <cpu>
111 Forbid to use specific CPU (or CPU-range).
113 =item -H|--prio-high
115 Make this high priority process.
117 =item -Q|--notouch-irq
119 Do not touch IRQ CPU affinity of NIC.
121 =back
123 =head1 EXAMPLES
125 =over
127 =item Generate traffic defined in trafgen.txf on eth0 using CPU 0
129 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0
131 =item Generate traffic on eth0 using CPU 0, wait 100 us between packets
133 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --gap 100
135 =item Generate 100,000 packet on eth0 using CPU 0
137 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 100000
139 =back
141 =head1 AUTHOR
143 Written by Daniel Borkmann <daniel@netsniff-ng.org>
145 =head1 DOCUMENTATION
147 Documentation by Emmanuel Roullit <emmanuel@netsniff-ng.org>
149 =head1 BUGS
151 Please report bugs to <bugs@netsniff-ng.org>
153 =cut
157 #include <stdio.h>
158 #include <string.h>
159 #include <getopt.h>
160 #include <ctype.h>
161 #include <stdbool.h>
162 #include <sys/socket.h>
163 #include <sys/types.h>
164 #include <sys/stat.h>
165 #include <sys/time.h>
166 #include <signal.h>
167 #include <stdint.h>
168 #include <stdlib.h>
169 #include <fcntl.h>
170 #include <time.h>
171 #include <net/ethernet.h>
173 #include "xmalloc.h"
174 #include "die.h"
175 #include "mac80211.h"
176 #include "xutils.h"
177 #include "xio.h"
178 #include "trafgen_conf.h"
179 #include "tprintf.h"
180 #include "mtrand.h"
181 #include "ring_tx.h"
183 struct stats {
184 unsigned long tx_bytes;
185 unsigned long tx_packets;
188 struct mode {
189 #define CPU_UNKNOWN -1
190 #define CPU_NOTOUCH -2
191 struct stats stats;
192 char *device;
193 char *device_trans;
194 int cpu;
195 int rand;
196 int rfraw;
197 unsigned long kpull;
198 /* 0 for automatic, > 0 for manual */
199 unsigned int reserve_size;
200 int jumbo_support;
201 int verbose;
202 unsigned long num;
203 unsigned long gap;
206 static int sock;
207 static struct itimerval itimer;
208 static unsigned long interval = TX_KERNEL_PULL_INT;
210 sig_atomic_t sigint = 0;
212 struct packet *packets = NULL;
213 unsigned int packets_len = 0;
215 struct packet_dynamics *packet_dyns = NULL;
216 unsigned int packet_dyn_len = 0;
218 static const char *short_options = "d:c:n:t:vJhS:HQb:B:rk:xi:o:VR";
220 static struct option long_options[] = {
221 {"dev", required_argument, 0, 'd'},
222 {"out", required_argument, 0, 'o'},
223 {"in", required_argument, 0, 'i'},
224 {"conf", required_argument, 0, 'c'},
225 {"num", required_argument, 0, 'n'},
226 {"gap", required_argument, 0, 't'},
227 {"ring-size", required_argument, 0, 'S'},
228 {"bind-cpu", required_argument, 0, 'b'},
229 {"unbind-cpu", required_argument, 0, 'B'},
230 {"kernel-pull", required_argument, 0, 'k'},
231 {"jumbo-support", no_argument, 0, 'J'},
232 {"rfraw", no_argument, 0, 'R'},
233 {"interactive", no_argument, 0, 'x'},
234 {"rand", no_argument, 0, 'r'},
235 {"prio-high", no_argument, 0, 'H'},
236 {"notouch-irq", no_argument, 0, 'Q'},
237 {"verbose", no_argument, 0, 'V'},
238 {"version", no_argument, 0, 'v'},
239 {"help", no_argument, 0, 'h'},
240 {0, 0, 0, 0}
243 static void signal_handler(int number)
245 switch (number) {
246 case SIGINT:
247 sigint = 1;
248 break;
249 case SIGHUP:
250 default:
251 break;
255 static void timer_elapsed(int number)
257 itimer.it_interval.tv_sec = 0;
258 itimer.it_interval.tv_usec = interval;
259 itimer.it_value.tv_sec = 0;
260 itimer.it_value.tv_usec = interval;
262 pull_and_flush_tx_ring(sock);
263 setitimer(ITIMER_REAL, &itimer, NULL);
266 static void header(void)
268 printf("%s%s%s\n", colorize_start(bold), "trafgen "
269 VERSION_STRING, colorize_end());
272 static void help(void)
274 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
275 VERSION_STRING);
276 printf("http://www.netsniff-ng.org\n\n");
277 printf("Usage: trafgen [options]\n");
278 printf("Options:\n");
279 /* printf(" -o|-d|--out|--dev <netdev|pcap> Networking Device i.e., eth0 or pcap\n"); */
280 printf(" -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n");
281 printf(" -i|-c|--in|--conf <cfg-file> Packet configuration file\n");
282 /* printf(" -x|--interactive Start trafgen in interactive server mode\n"); */
283 printf(" -J|--jumbo-support Support for 64KB Super Jumbo Frames\n");
284 printf(" Default TX slot: 2048Byte\n");
285 printf(" -R|--rfraw Inject raw 802.11 frames\n");
286 printf(" -n|--num <uint> Number of packets until exit\n");
287 printf(" `-- 0 Loop until interrupt (default)\n");
288 printf(" `- n Send n packets and done\n");
289 printf(" -r|--rand Randomize packet selection process\n");
290 printf(" Instead of a round robin selection\n");
291 printf(" -t|--gap <uint> Interpacket gap in us (approx)\n");
292 printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
293 printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
294 printf(" -k|--kernel-pull <uint> Kernel pull from user interval in us\n");
295 printf(" Default is 10us where the TX_RING\n");
296 printf(" is populated with payload from uspace\n");
297 printf(" -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n");
298 printf(" -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n");
299 printf(" -H|--prio-high Make this high priority process\n");
300 printf(" -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n");
301 printf(" -v|--version Show version\n");
302 printf(" -h|--help Guess what?!\n");
303 printf("\n");
304 printf("Examples:\n");
305 printf(" See trafgen.txf for configuration file examples.\n");
306 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n");
307 printf(" trafgen --dev wlan0 --rfraw --conf beacon-test.txf --bind-cpu 0\n");
308 printf(" trafgen --out eth0 --in trafgen.txf --bind-cpu 0\n");
309 /* printf(" trafgen --out test.pcap --in trafgen.txf --bind-cpu 0\n"); */
310 printf(" trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n");
311 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n");
312 /* printf(" trafgen --interactive\n");
313 printf(" trafgen --interactive --dev mgmt0 (only start server on mgmt0)\n");
314 printf(" trafgen --interactive --conf trafgen-cli.batch\n");*/
315 printf("\n");
316 printf("Note:\n");
317 printf(" This tool is targeted for network developers! You should\n");
318 printf(" be aware of what you are doing and what these options above\n");
319 printf(" mean! Only use this tool in an isolated LAN that you own!\n");
320 printf("\n");
321 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
322 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
323 printf("Swiss federal institute of technology (ETH Zurich)\n");
324 printf("License: GNU GPL version 2\n");
325 printf("This is free software: you are free to change and redistribute it.\n");
326 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
327 die();
330 static void version(void)
332 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
333 VERSION_STRING);
334 printf("http://www.netsniff-ng.org\n\n");
335 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
336 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
337 printf("Swiss federal institute of technology (ETH Zurich)\n");
338 printf("License: GNU GPL version 2\n");
339 printf("This is free software: you are free to change and redistribute it.\n");
340 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
341 die();
344 static inline void apply_counter(int i)
346 int j;
348 for (j = 0; j < packet_dyns[i].counter_len; ++j) {
349 uint8_t val;
350 struct counter *counter = &packet_dyns[i].counter[j];
352 val = counter->val;
353 val -= counter->min;
355 if (counter->type == TYPE_INC)
356 val = (val + counter->inc) %
357 (counter->max - counter->min + 1);
358 else
359 val = (val - counter->inc) %
360 (counter->min - counter->max + 1);
362 val += counter->min;
363 counter->val = val;
365 packets[i].payload[counter->off] = val;
369 static inline void apply_randomizer(int i)
371 int j;
373 for (j = 0; j < packet_dyns[i].randomizer_len; ++j) {
374 uint8_t val = (uint8_t) mt_rand_int32();
375 struct randomizer *randomizer = &packet_dyns[i].randomizer[j];
377 randomizer->val = val;
378 packets[i].payload[randomizer->off] = val;
382 static void tx_precheck(struct mode *mode)
384 int i, mtu;
386 if (!mode)
387 panic("Panic over invalid args for TX trigger!\n");
388 if (packets_len == 0 || packets_len != packet_dyn_len)
389 panic("Panic over invalid args for TX trigger!\n");
390 if (!mode->rfraw && !device_up_and_running(mode->device))
391 panic("Device not up and running!\n");
393 mtu = device_mtu(mode->device);
395 for (i = 0; i < packets_len; ++i) {
396 if (packets[i].len > mtu + 14)
397 panic("Device MTU < than your packet size!\n");
398 if (packets[i].len <= 14)
399 panic("Device packet size too short!\n");
403 static void tx_slowpath_or_die(struct mode *mode)
405 int ifindex, ret;
406 unsigned int i;
407 struct sockaddr_ll s_addr;
408 unsigned long num = 1;
409 struct timeval start, end, diff;
411 tx_precheck(mode);
413 sock = pf_socket();
415 if (mode->rfraw) {
416 mode->device_trans = xstrdup(mode->device);
417 xfree(mode->device);
419 enter_rfmon_mac80211(mode->device_trans, &mode->device);
422 ifindex = device_ifindex(mode->device);
424 if (mode->num > 0)
425 num = mode->num;
426 if (mode->rand)
427 printf("Note: randomizes output makes trafgen slower!\n");
429 printf("MD: TX slowpath %s %luus", mode->rand ? "RND" : "RR", mode->gap);
430 if (mode->rfraw)
431 printf(" 802.11 raw via %s", mode->device);
432 printf("\n\n");
433 printf("Running! Hang up with ^C!\n\n");
435 fmemset(&s_addr, 0, sizeof(s_addr));
436 s_addr.sll_family = PF_PACKET;
437 s_addr.sll_halen = ETH_ALEN;
438 s_addr.sll_ifindex = ifindex;
440 i = 0;
442 gettimeofday(&start, NULL);
444 while (likely(sigint == 0) && likely(num > 0)) {
445 apply_counter(i);
446 apply_randomizer(i);
448 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
449 (struct sockaddr *) &s_addr, sizeof(s_addr));
450 if (ret < 0)
451 whine("sendto error!\n");
453 mode->stats.tx_bytes += packets[i].len;
454 mode->stats.tx_packets++;
456 if (mode->rand) {
457 i = mt_rand_int32() % packets_len;
458 } else {
459 i++;
460 atomic_cmp_swp(&i, packets_len, 0);
463 if (mode->num > 0)
464 num--;
466 usleep(mode->gap);
469 gettimeofday(&end, NULL);
470 diff = tv_subtract(end, start);
472 if (mode->rfraw)
473 leave_rfmon_mac80211(mode->device_trans, mode->device);
475 close(sock);
477 fflush(stdout);
478 printf("\n");
479 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
480 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
481 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
484 static void tx_fastpath_or_die(struct mode *mode)
486 int irq, ifindex;
487 unsigned int i, size, it = 0;
488 unsigned long num = 1;
489 uint8_t *out = NULL;
490 struct ring tx_ring;
491 struct frame_map *hdr;
492 struct timeval start, end, diff;
494 tx_precheck(mode);
496 sock = pf_socket();
498 fmemset(&tx_ring, 0, sizeof(tx_ring));
500 if (mode->rfraw) {
501 mode->device_trans = xstrdup(mode->device);
502 xfree(mode->device);
504 enter_rfmon_mac80211(mode->device_trans, &mode->device);
507 ifindex = device_ifindex(mode->device);
508 size = ring_size(mode->device, mode->reserve_size);
510 set_packet_loss_discard(sock);
511 setup_tx_ring_layout(sock, &tx_ring, size, mode->jumbo_support);
512 create_tx_ring(sock, &tx_ring);
513 mmap_tx_ring(sock, &tx_ring);
514 alloc_tx_ring_frames(&tx_ring);
515 bind_tx_ring(sock, &tx_ring, ifindex);
517 if (mode->cpu >= 0 && ifindex > 0) {
518 irq = device_irq_number(mode->device);
519 device_bind_irq_to_cpu(mode->cpu, irq);
520 printf("IRQ: %s:%d > CPU%d\n", mode->device, irq,
521 mode->cpu);
524 if (mode->kpull)
525 interval = mode->kpull;
526 if (mode->num > 0)
527 num = mode->num;
528 if (mode->rand)
529 printf("Note: randomizes output makes trafgen slower!\n");
531 printf("MD: TX fastpath %s %luus", mode->rand ? "RND" : "RR", interval);
532 if (mode->rfraw)
533 printf(" 802.11 raw via %s", mode->device);
534 printf("\n\n");
535 printf("Running! Hang up with ^C!\n\n");
537 itimer.it_interval.tv_sec = 0;
538 itimer.it_interval.tv_usec = interval;
539 itimer.it_value.tv_sec = 0;
540 itimer.it_value.tv_usec = interval;
541 setitimer(ITIMER_REAL, &itimer, NULL);
543 i = 0;
545 gettimeofday(&start, NULL);
547 while (likely(sigint == 0) && likely(num > 0)) {
548 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) &&
549 likely(num > 0)) {
550 hdr = tx_ring.frames[it].iov_base;
552 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
553 * sizeof(struct sockaddr_ll); */
554 out = ((uint8_t *) hdr) + TPACKET_HDRLEN -
555 sizeof(struct sockaddr_ll);
557 hdr->tp_h.tp_snaplen = packets[i].len;
558 hdr->tp_h.tp_len = packets[i].len;
560 apply_counter(i);
561 apply_randomizer(i);
563 fmemcpy(out, packets[i].payload, packets[i].len);
565 mode->stats.tx_bytes += packets[i].len;
566 mode->stats.tx_packets++;
568 if (mode->rand) {
569 i = mt_rand_int32() % packets_len;
570 } else {
571 i++;
572 atomic_cmp_swp(&i, packets_len, 0);
575 kernel_may_pull_from_tx(&hdr->tp_h);
576 next_slot_prewr(&it, &tx_ring);
578 if (mode->num > 0)
579 num--;
580 if (unlikely(sigint == 1))
581 break;
585 gettimeofday(&end, NULL);
586 diff = tv_subtract(end, start);
588 destroy_tx_ring(sock, &tx_ring);
590 if (mode->rfraw)
591 leave_rfmon_mac80211(mode->device_trans, mode->device);
593 close(sock);
595 fflush(stdout);
596 printf("\n");
597 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
598 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
599 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
602 static void main_loop(struct mode *mode, char *confname)
604 compile_packets(confname, mode->verbose);
606 if (mode->gap > 0)
607 tx_slowpath_or_die(mode);
608 else
609 tx_fastpath_or_die(mode);
611 cleanup_packets();
614 int main(int argc, char **argv)
616 int c, opt_index, i, j, interactive = 0;
617 char *confname = NULL, *ptr;
618 bool prio_high = false;
619 struct mode mode;
621 check_for_root_maybe_die();
623 fmemset(&mode, 0, sizeof(mode));
624 mode.cpu = CPU_UNKNOWN;
625 mode.gap = 0;
626 mode.num = 0;
628 while ((c = getopt_long(argc, argv, short_options, long_options,
629 &opt_index)) != EOF) {
630 switch (c) {
631 case 'h':
632 help();
633 break;
634 case 'v':
635 version();
636 break;
637 case 'V':
638 mode.verbose = 1;
639 break;
640 case 'd':
641 case 'o':
642 mode.device = xstrndup(optarg, IFNAMSIZ);
643 break;
644 case 'x':
645 interactive = 1;
646 break;
647 case 'r':
648 mode.rand = 1;
649 break;
650 case 'R':
651 mode.rfraw = 1;
652 break;
653 case 'J':
654 mode.jumbo_support = 1;
655 break;
656 case 'c':
657 case 'i':
658 confname = xstrdup(optarg);
659 break;
660 case 'k':
661 mode.kpull = atol(optarg);
662 break;
663 case 'n':
664 mode.num = atol(optarg);
665 break;
666 case 't':
667 mode.gap = atol(optarg);
668 break;
669 case 'S':
670 ptr = optarg;
671 mode.reserve_size = 0;
673 for (j = i = strlen(optarg); i > 0; --i) {
674 if (!isdigit(optarg[j - i]))
675 break;
676 ptr++;
679 if (!strncmp(ptr, "KB", strlen("KB")))
680 mode.reserve_size = 1 << 10;
681 else if (!strncmp(ptr, "MB", strlen("MB")))
682 mode.reserve_size = 1 << 20;
683 else if (!strncmp(ptr, "GB", strlen("GB")))
684 mode.reserve_size = 1 << 30;
685 else
686 panic("Syntax error in ring size param!\n");
687 *ptr = 0;
689 mode.reserve_size *= atoi(optarg);
690 break;
691 case 'b':
692 set_cpu_affinity(optarg, 0);
693 /* Take the first CPU for rebinding the IRQ */
694 if (mode.cpu != CPU_NOTOUCH)
695 mode.cpu = atoi(optarg);
696 break;
697 case 'B':
698 set_cpu_affinity(optarg, 1);
699 break;
700 case 'H':
701 prio_high = true;
702 break;
703 case 'Q':
704 mode.cpu = CPU_NOTOUCH;
705 break;
706 case '?':
707 switch (optopt) {
708 case 'd':
709 case 'c':
710 case 'n':
711 case 'S':
712 case 'b':
713 case 'o':
714 case 'i':
715 case 'k':
716 case 'B':
717 case 't':
718 panic("Option -%c requires an argument!\n",
719 optopt);
720 default:
721 if (isprint(optopt))
722 whine("Unknown option character "
723 "`0x%X\'!\n", optopt);
724 die();
726 default:
727 break;
731 if (!interactive && argc < 5)
732 help();
733 if (interactive && argc < 2)
734 help();
735 if (!interactive && mode.device == NULL)
736 panic("No networking device given!\n");
737 if (!interactive && confname == NULL)
738 panic("No configuration file given!\n");
739 if (!interactive && device_mtu(mode.device) == 0)
740 panic("This is no networking device!\n");
741 if (!interactive && !mode.rfraw &&
742 device_up_and_running(mode.device) == 0)
743 panic("Networking device not running!\n");
745 register_signal(SIGINT, signal_handler);
746 register_signal(SIGHUP, signal_handler);
747 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
749 header();
751 if (prio_high == true) {
752 set_proc_prio(get_default_proc_prio());
753 set_sched_status(get_default_sched_policy(),
754 get_default_sched_prio());
757 if (interactive)
758 main_loop_interactive(&mode, confname);
759 else
760 main_loop(&mode, confname);
762 if (mode.device)
763 xfree(mode.device);
764 if (mode.device_trans)
765 xfree(mode.device_trans);
766 if (confname)
767 xfree(confname);
769 return 0;