doc: new fedora/rhel maintainer
[netsniff-ng.git] / src / trafgen.c
blobd1e469f6d7ad5f742aa4599604989b4b0f0d5f05
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 "trafgen_conf.h"
46 #include "tprintf.h"
47 #include "mtrand.h"
48 #include "ring_tx.h"
50 struct stats {
51 unsigned long tx_bytes;
52 unsigned long tx_packets;
55 struct mode {
56 #define CPU_UNKNOWN -1
57 #define CPU_NOTOUCH -2
58 struct stats stats;
59 char *device;
60 char *device_trans;
61 int cpu;
62 int rand;
63 int rfraw;
64 unsigned long kpull;
65 /* 0 for automatic, > 0 for manual */
66 unsigned int reserve_size;
67 int jumbo_support;
68 int verbose;
69 unsigned long num;
70 unsigned long gap;
73 static int sock;
74 static struct itimerval itimer;
75 static unsigned long interval = TX_KERNEL_PULL_INT;
77 sig_atomic_t sigint = 0;
79 struct packet *packets = NULL;
80 unsigned int packets_len = 0;
82 struct packet_dynamics *packet_dyns = NULL;
83 unsigned int packet_dyn_len = 0;
85 static const char *short_options = "d:c:n:t:vJhS:HQb:B:rk:i:o:VRA";
87 static struct option long_options[] = {
88 {"dev", required_argument, 0, 'd'},
89 {"out", required_argument, 0, 'o'},
90 {"in", required_argument, 0, 'i'},
91 {"conf", required_argument, 0, 'c'},
92 {"num", required_argument, 0, 'n'},
93 {"gap", required_argument, 0, 't'},
94 {"ring-size", required_argument, 0, 'S'},
95 {"bind-cpu", required_argument, 0, 'b'},
96 {"unbind-cpu", required_argument, 0, 'B'},
97 {"kernel-pull", required_argument, 0, 'k'},
98 {"jumbo-support", no_argument, 0, 'J'},
99 {"rfraw", no_argument, 0, 'R'},
100 {"rand", no_argument, 0, 'r'},
101 {"prio-high", no_argument, 0, 'H'},
102 {"notouch-irq", no_argument, 0, 'Q'},
103 {"verbose", no_argument, 0, 'V'},
104 {"no-sock-mem", no_argument, 0, 'A'},
105 {"version", no_argument, 0, 'v'},
106 {"help", no_argument, 0, 'h'},
107 {0, 0, 0, 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, high-perf zero-copy network packet generator\n",
142 VERSION_STRING);
143 printf("http://www.netsniff-ng.org\n\n");
144 printf("Usage: trafgen [options]\n");
145 printf("Options:\n");
146 /* printf(" -o|-d|--out|--dev <netdev|pcap> Networking Device i.e., eth0 or pcap\n"); */
147 printf(" -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n");
148 printf(" -i|-c|--in|--conf <cfg-file> Packet configuration file\n");
149 printf(" -J|--jumbo-support Support for 64KB Super Jumbo Frames\n");
150 printf(" Default TX slot: 2048Byte\n");
151 printf(" -R|--rfraw Inject raw 802.11 frames\n");
152 printf(" -n|--num <uint> Number of packets until exit\n");
153 printf(" `-- 0 Loop until interrupt (default)\n");
154 printf(" `- n Send n packets and done\n");
155 printf(" -r|--rand Randomize packet selection process\n");
156 printf(" Instead of a round robin selection\n");
157 printf(" -t|--gap <uint> Interpacket gap in us (approx)\n");
158 printf(" -A|--no-sock-mem Don't tune core socket memory\n");
159 printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
160 printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
161 printf(" -k|--kernel-pull <uint> Kernel pull from user interval in us\n");
162 printf(" Default is 10us where the TX_RING\n");
163 printf(" is populated with payload from uspace\n");
164 printf(" -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n");
165 printf(" -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n");
166 printf(" -H|--prio-high Make this high priority process\n");
167 printf(" -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n");
168 printf(" -v|--version Show version\n");
169 printf(" -h|--help Guess what?!\n");
170 printf("\n");
171 printf("Examples:\n");
172 printf(" See trafgen.txf for configuration file examples.\n");
173 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n");
174 printf(" trafgen --dev wlan0 --rfraw --conf beacon-test.txf --bind-cpu 0 -A\n");
175 printf(" trafgen --out eth0 --in trafgen.txf --bind-cpu 0\n");
176 /* printf(" trafgen --out test.pcap --in trafgen.txf --bind-cpu 0\n"); */
177 printf(" trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n");
178 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n");
179 printf("\n");
180 printf("Note:\n");
181 printf(" This tool is targeted for network developers! You should\n");
182 printf(" be aware of what you are doing and what these options above\n");
183 printf(" mean! Only use this tool in an isolated LAN that you own!\n");
184 printf("\n");
185 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
186 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
187 printf("Swiss federal institute of technology (ETH Zurich)\n");
188 printf("License: GNU GPL version 2\n");
189 printf("This is free software: you are free to change and redistribute it.\n");
190 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
191 die();
194 static void version(void)
196 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
197 VERSION_STRING);
198 printf("http://www.netsniff-ng.org\n\n");
199 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
200 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
201 printf("Swiss federal institute of technology (ETH Zurich)\n");
202 printf("License: GNU GPL version 2\n");
203 printf("This is free software: you are free to change and redistribute it.\n");
204 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
205 die();
208 static inline void apply_counter(int i)
210 int j;
212 for (j = 0; j < packet_dyns[i].counter_len; ++j) {
213 uint8_t val;
214 struct counter *counter = &packet_dyns[i].counter[j];
216 val = counter->val;
217 val -= counter->min;
219 if (counter->type == TYPE_INC)
220 val = (val + counter->inc) %
221 (counter->max - counter->min + 1);
222 else
223 val = (val - counter->inc) %
224 (counter->min - counter->max + 1);
226 val += counter->min;
227 counter->val = val;
229 packets[i].payload[counter->off] = val;
233 static inline void apply_randomizer(int i)
235 int j;
237 for (j = 0; j < packet_dyns[i].randomizer_len; ++j) {
238 uint8_t val = (uint8_t) mt_rand_int32();
239 struct randomizer *randomizer = &packet_dyns[i].randomizer[j];
241 randomizer->val = val;
242 packets[i].payload[randomizer->off] = val;
246 static void tx_precheck(struct mode *mode)
248 int i, mtu;
250 if (!mode)
251 panic("Panic over invalid args for TX trigger!\n");
252 if (packets_len == 0 || packets_len != packet_dyn_len)
253 panic("Panic over invalid args for TX trigger!\n");
254 if (!mode->rfraw && !device_up_and_running(mode->device))
255 panic("Device not up and running!\n");
257 mtu = device_mtu(mode->device);
259 for (i = 0; i < packets_len; ++i) {
260 if (packets[i].len > mtu + 14)
261 panic("Device MTU < than your packet size!\n");
262 if (packets[i].len <= 14)
263 panic("Device packet size too short!\n");
267 static void tx_slowpath_or_die(struct mode *mode)
269 int ifindex, ret;
270 unsigned int i;
271 struct sockaddr_ll s_addr;
272 unsigned long num = 1;
273 struct timeval start, end, diff;
275 tx_precheck(mode);
277 sock = pf_socket();
279 if (mode->rfraw) {
280 mode->device_trans = xstrdup(mode->device);
281 xfree(mode->device);
283 enter_rfmon_mac80211(mode->device_trans, &mode->device);
286 ifindex = device_ifindex(mode->device);
288 if (mode->num > 0)
289 num = mode->num;
290 if (mode->rand)
291 printf("Note: randomizes output makes trafgen slower!\n");
293 printf("MD: TX slowpath %s %luus", mode->rand ? "RND" : "RR", mode->gap);
294 if (mode->rfraw)
295 printf(" 802.11 raw via %s", mode->device);
296 printf("\n\n");
297 printf("Running! Hang up with ^C!\n\n");
299 fmemset(&s_addr, 0, sizeof(s_addr));
300 s_addr.sll_family = PF_PACKET;
301 s_addr.sll_halen = ETH_ALEN;
302 s_addr.sll_ifindex = ifindex;
304 i = 0;
306 gettimeofday(&start, NULL);
308 while (likely(sigint == 0) && likely(num > 0)) {
309 apply_counter(i);
310 apply_randomizer(i);
312 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
313 (struct sockaddr *) &s_addr, sizeof(s_addr));
314 if (ret < 0)
315 whine("sendto error!\n");
317 mode->stats.tx_bytes += packets[i].len;
318 mode->stats.tx_packets++;
320 if (mode->rand) {
321 i = mt_rand_int32() % packets_len;
322 } else {
323 i++;
324 atomic_cmp_swp(&i, packets_len, 0);
327 if (mode->num > 0)
328 num--;
330 usleep(mode->gap);
333 gettimeofday(&end, NULL);
334 diff = tv_subtract(end, start);
336 if (mode->rfraw)
337 leave_rfmon_mac80211(mode->device_trans, mode->device);
339 close(sock);
341 fflush(stdout);
342 printf("\n");
343 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
344 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
345 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
348 static void tx_fastpath_or_die(struct mode *mode)
350 int irq, ifindex;
351 unsigned int i, size, it = 0;
352 unsigned long num = 1;
353 uint8_t *out = NULL;
354 struct ring tx_ring;
355 struct frame_map *hdr;
356 struct timeval start, end, diff;
358 tx_precheck(mode);
360 sock = pf_socket();
362 fmemset(&tx_ring, 0, sizeof(tx_ring));
364 if (mode->rfraw) {
365 mode->device_trans = xstrdup(mode->device);
366 xfree(mode->device);
368 enter_rfmon_mac80211(mode->device_trans, &mode->device);
371 ifindex = device_ifindex(mode->device);
372 size = ring_size(mode->device, mode->reserve_size);
374 set_packet_loss_discard(sock);
375 setup_tx_ring_layout(sock, &tx_ring, size, mode->jumbo_support);
376 create_tx_ring(sock, &tx_ring);
377 mmap_tx_ring(sock, &tx_ring);
378 alloc_tx_ring_frames(&tx_ring);
379 bind_tx_ring(sock, &tx_ring, ifindex);
381 if (mode->cpu >= 0 && ifindex > 0) {
382 irq = device_irq_number(mode->device);
383 device_bind_irq_to_cpu(mode->cpu, irq);
384 printf("IRQ: %s:%d > CPU%d\n", mode->device, irq,
385 mode->cpu);
388 if (mode->kpull)
389 interval = mode->kpull;
390 if (mode->num > 0)
391 num = mode->num;
392 if (mode->rand)
393 printf("Note: randomizes output makes trafgen slower!\n");
395 printf("MD: TX fastpath %s %luus", mode->rand ? "RND" : "RR", interval);
396 if (mode->rfraw)
397 printf(" 802.11 raw via %s", mode->device);
398 printf("\n\n");
399 printf("Running! Hang up with ^C!\n\n");
401 itimer.it_interval.tv_sec = 0;
402 itimer.it_interval.tv_usec = interval;
403 itimer.it_value.tv_sec = 0;
404 itimer.it_value.tv_usec = interval;
405 setitimer(ITIMER_REAL, &itimer, NULL);
407 i = 0;
409 gettimeofday(&start, NULL);
411 while (likely(sigint == 0) && likely(num > 0)) {
412 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) &&
413 likely(num > 0)) {
414 hdr = tx_ring.frames[it].iov_base;
416 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
417 * sizeof(struct sockaddr_ll); */
418 out = ((uint8_t *) hdr) + TPACKET_HDRLEN -
419 sizeof(struct sockaddr_ll);
421 hdr->tp_h.tp_snaplen = packets[i].len;
422 hdr->tp_h.tp_len = packets[i].len;
424 apply_counter(i);
425 apply_randomizer(i);
427 fmemcpy(out, packets[i].payload, packets[i].len);
429 mode->stats.tx_bytes += packets[i].len;
430 mode->stats.tx_packets++;
432 if (mode->rand) {
433 i = mt_rand_int32() % packets_len;
434 } else {
435 i++;
436 atomic_cmp_swp(&i, packets_len, 0);
439 kernel_may_pull_from_tx(&hdr->tp_h);
440 next_slot_prewr(&it, &tx_ring);
442 if (mode->num > 0)
443 num--;
444 if (unlikely(sigint == 1))
445 break;
449 gettimeofday(&end, NULL);
450 diff = tv_subtract(end, start);
452 destroy_tx_ring(sock, &tx_ring);
454 if (mode->rfraw)
455 leave_rfmon_mac80211(mode->device_trans, mode->device);
457 close(sock);
459 fflush(stdout);
460 printf("\n");
461 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
462 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
463 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
466 static void main_loop(struct mode *mode, char *confname)
468 compile_packets(confname, mode->verbose);
470 if (mode->gap > 0)
471 tx_slowpath_or_die(mode);
472 else
473 tx_fastpath_or_die(mode);
475 cleanup_packets();
478 int main(int argc, char **argv)
480 int c, opt_index, i, j;
481 int vals[4] = {0};
482 char *confname = NULL, *ptr;
483 bool prio_high = false;
484 bool setsockmem = true;
485 struct mode mode;
487 check_for_root_maybe_die();
489 fmemset(&mode, 0, sizeof(mode));
490 mode.cpu = CPU_UNKNOWN;
491 mode.gap = 0;
492 mode.num = 0;
494 while ((c = getopt_long(argc, argv, short_options, long_options,
495 &opt_index)) != EOF) {
496 switch (c) {
497 case 'h':
498 help();
499 break;
500 case 'v':
501 version();
502 break;
503 case 'V':
504 mode.verbose = 1;
505 break;
506 case 'd':
507 case 'o':
508 mode.device = xstrndup(optarg, IFNAMSIZ);
509 break;
510 case 'r':
511 mode.rand = 1;
512 break;
513 case 'R':
514 mode.rfraw = 1;
515 break;
516 case 'J':
517 mode.jumbo_support = 1;
518 break;
519 case 'c':
520 case 'i':
521 confname = xstrdup(optarg);
522 break;
523 case 'k':
524 mode.kpull = atol(optarg);
525 break;
526 case 'n':
527 mode.num = atol(optarg);
528 break;
529 case 't':
530 mode.gap = atol(optarg);
531 break;
532 case 'A':
533 setsockmem = false;
534 break;
535 case 'S':
536 ptr = optarg;
537 mode.reserve_size = 0;
539 for (j = i = strlen(optarg); i > 0; --i) {
540 if (!isdigit(optarg[j - i]))
541 break;
542 ptr++;
545 if (!strncmp(ptr, "KB", strlen("KB")))
546 mode.reserve_size = 1 << 10;
547 else if (!strncmp(ptr, "MB", strlen("MB")))
548 mode.reserve_size = 1 << 20;
549 else if (!strncmp(ptr, "GB", strlen("GB")))
550 mode.reserve_size = 1 << 30;
551 else
552 panic("Syntax error in ring size param!\n");
553 *ptr = 0;
555 mode.reserve_size *= atoi(optarg);
556 break;
557 case 'b':
558 set_cpu_affinity(optarg, 0);
559 /* Take the first CPU for rebinding the IRQ */
560 if (mode.cpu != CPU_NOTOUCH)
561 mode.cpu = atoi(optarg);
562 break;
563 case 'B':
564 set_cpu_affinity(optarg, 1);
565 break;
566 case 'H':
567 prio_high = true;
568 break;
569 case 'Q':
570 mode.cpu = CPU_NOTOUCH;
571 break;
572 case '?':
573 switch (optopt) {
574 case 'd':
575 case 'c':
576 case 'n':
577 case 'S':
578 case 'b':
579 case 'o':
580 case 'i':
581 case 'k':
582 case 'B':
583 case 't':
584 panic("Option -%c requires an argument!\n",
585 optopt);
586 default:
587 if (isprint(optopt))
588 whine("Unknown option character "
589 "`0x%X\'!\n", optopt);
590 die();
592 default:
593 break;
597 if (argc < 5)
598 help();
599 if (mode.device == NULL)
600 panic("No networking device given!\n");
601 if (confname == NULL)
602 panic("No configuration file given!\n");
603 if (device_mtu(mode.device) == 0)
604 panic("This is no networking device!\n");
605 if (!mode.rfraw && device_up_and_running(mode.device) == 0)
606 panic("Networking device not running!\n");
608 register_signal(SIGINT, signal_handler);
609 register_signal(SIGHUP, signal_handler);
610 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
612 header();
614 if (prio_high == true) {
615 set_proc_prio(get_default_proc_prio());
616 set_sched_status(get_default_sched_policy(),
617 get_default_sched_prio());
620 if (setsockmem == true) {
621 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX)
622 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX);
623 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF)
624 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF);
625 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX)
626 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX);
627 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF)
628 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF);
631 main_loop(&mode, confname);
633 if (setsockmem == true) {
634 set_system_socket_mem(sock_rmem_max, vals[0]);
635 set_system_socket_mem(sock_rmem_def, vals[1]);
636 set_system_socket_mem(sock_wmem_max, vals[2]);
637 set_system_socket_mem(sock_wmem_def, vals[3]);
640 free(mode.device);
641 free(mode.device_trans);
642 free(confname);
644 return 0;