all: minor: updated copyright year
[netsniff-ng.git] / src / trafgen.c
blob4d6fb57ec693bc0b6b86b2c9e28206d2f562da0d
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][-n|--num <uint>]
33 [-r|--rand][-t|--gap <usec>][-S|--ring-size <size>]
34 [-k|--kernel-pull <usec>][-b|--bind-cpu <cpu>][-B|--unbind-cpu <cpu>]
35 [-H|--prio-high][-Q|--notouch-irq][-v|--version][-h|--help]
37 =head1 DESCRIPTION
39 A high-performance network traffic generator that uses the zero-copy TX_RING
40 for network I/O. For instance, on comodity Gigabit hardware up to 1,488,095 pps
41 64 Byte pps have been achieved with trafgen.
43 =head1 OPTIONS
45 =over
47 =item trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0
49 Use packet configuration trafgen.txf, eth0 as transmission device and CPU0
50 for binding the process.
52 =back
54 =head1 OPTIONS
56 =over
58 =item -h|--help
60 Print help text and lists all options.
62 =item -v|--version
64 Print version.
66 =item -d|--dev <netdev>
68 Device for transmission i.e., eth0.
70 =item -c|--conf <conf>
72 Path to packet configuration file.
74 =item -J|--jumbo-support
76 Support for 64KB Super Jumbo Frames
78 =item -n|--num <uint>
80 Number of packets to generate before exiting.
81 0 means forever until SIGINT.
83 =item -r|--rand
85 Randomize packet selection process instead of round-robin.
87 =item -t|--gap <uint>
89 Interpacket gap in microseconds.
91 =item -S|--ring-size <size>
93 Manually set ring size to <size>: mmap space in KB/MB/GB.
95 =item -k|--kernel-pull <uint>
97 Kernel pull from user interval in microseconds.
98 Default value is 10 microseconds.
100 =item -b|--bind-cpu <cpu>
102 Bind to specific CPU (or CPU-range).
104 =item -B|--unbind-cpu <cpu>
106 Forbid to use specific CPU (or CPU-range).
108 =item -H|--prio-high
110 Make this high priority process.
112 =item -Q|--notouch-irq
114 Do not touch IRQ CPU affinity of NIC.
116 =back
118 =head1 EXAMPLES
120 =over
122 =item Generate traffic defined in trafgen.txf on eth0 using CPU 0
124 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0
126 =item Generate traffic on eth0 using CPU 0, wait 100 us between packets
128 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --gap 100
130 =item Generate 100,000 packet on eth0 using CPU 0
132 trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 100000
134 =back
136 =head1 AUTHOR
138 Written by Daniel Borkmann <daniel@netsniff-ng.org>
140 =head1 DOCUMENTATION
142 Documentation by Emmanuel Roullit <emmanuel@netsniff-ng.org>
144 =head1 BUGS
146 Please report bugs to <bugs@netsniff-ng.org>
148 =cut
152 #include <stdio.h>
153 #include <string.h>
154 #include <getopt.h>
155 #include <ctype.h>
156 #include <stdbool.h>
157 #include <sys/socket.h>
158 #include <sys/types.h>
159 #include <sys/stat.h>
160 #include <sys/time.h>
161 #include <signal.h>
162 #include <stdint.h>
163 #include <stdlib.h>
164 #include <assert.h>
165 #include <fcntl.h>
166 #include <time.h>
167 #include <net/ethernet.h>
169 #include "xmalloc.h"
170 #include "opt_memcpy.h"
171 #include "xstring.h"
172 #include "die.h"
173 #include "xsys.h"
174 #include "xio.h"
175 #include "mtrand.h"
176 #include "ring_tx.h"
178 struct counter {
179 uint16_t id;
180 uint8_t min;
181 uint8_t max;
182 uint8_t inc;
183 uint8_t val;
184 off_t off;
187 struct randomizer {
188 uint8_t val;
189 off_t off;
192 struct packet {
193 uint8_t *payload;
194 size_t plen;
195 struct counter *cnt;
196 size_t clen;
197 struct randomizer *rnd;
198 size_t rlen;
201 struct pktconf {
202 unsigned long num;
203 unsigned long gap;
204 struct packet *pkts;
205 size_t len;
208 struct stats {
209 unsigned long tx_bytes;
210 unsigned long tx_packets;
213 struct mode {
214 struct stats stats;
215 char *device;
216 int cpu;
217 int rand;
218 unsigned long kpull;
219 /* 0 for automatic, > 0 for manual */
220 unsigned int reserve_size;
221 int jumbo_support;
224 #define CPU_UNKNOWN -1
225 #define CPU_NOTOUCH -2
227 sig_atomic_t sigint = 0;
229 static const char *short_options = "d:c:n:t:vJhS:HQb:B:rk:";
231 static struct option long_options[] = {
232 {"dev", required_argument, 0, 'd'},
233 {"conf", required_argument, 0, 'c'},
234 {"num", required_argument, 0, 'n'},
235 {"gap", required_argument, 0, 't'},
236 {"ring-size", required_argument, 0, 'S'},
237 {"bind-cpu", required_argument, 0, 'b'},
238 {"unbind-cpu", required_argument, 0, 'B'},
239 {"kernel-pull", required_argument, 0, 'k'},
240 {"jumbo-support", no_argument, 0, 'J'},
241 {"rand", no_argument, 0, 'r'},
242 {"prio-high", no_argument, 0, 'H'},
243 {"notouch-irq", no_argument, 0, 'Q'},
244 {"version", no_argument, 0, 'v'},
245 {"help", no_argument, 0, 'h'},
246 {0, 0, 0, 0}
249 static struct itimerval itimer;
250 static int sock;
251 static unsigned long interval = TX_KERNEL_PULL_INT;
253 static inline uint8_t lcrand(uint8_t val)
255 return 0xFF & (3 * val + 3);
258 static void signal_handler(int number)
260 switch (number) {
261 case SIGINT:
262 sigint = 1;
263 break;
264 case SIGHUP:
265 break;
266 default:
267 break;
271 static void timer_elapsed(int number)
273 itimer.it_interval.tv_sec = 0;
274 itimer.it_interval.tv_usec = interval;
275 itimer.it_value.tv_sec = 0;
276 itimer.it_value.tv_usec = interval;
278 pull_and_flush_tx_ring(sock);
279 setitimer(ITIMER_REAL, &itimer, NULL);
282 static void header(void)
284 printf("%s%s%s\n", colorize_start(bold), "trafgen "
285 VERSION_STRING, colorize_end());
288 static void help(void)
290 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
291 VERSION_STRING);
292 printf("http://www.netsniff-ng.org\n\n");
293 printf("Usage: trafgen [options]\n");
294 printf("Options:\n");
295 printf(" -d|--dev <netdev> Networking Device i.e., eth0\n");
296 printf(" -c|--conf <file> Packet configuration file\n");
297 printf(" -J|--jumbo-support Support for 64KB Super Jumbo Frames\n");
298 printf(" Default TX slot: 2048Byte\n");
299 printf(" -n|--num <uint> Number of packets until exit\n");
300 printf(" `-- 0 Loop until interrupt (default)\n");
301 printf(" `- n Send n packets and done\n");
302 printf(" -r|--rand Randomize packet selection process\n");
303 printf(" Instead of a round robin selection\n");
304 printf(" -t|--gap <uint> Interpacket gap in us (approx)\n");
305 printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
306 printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
307 printf(" -k|--kernel-pull <uint>Kernel pull from user interval in us\n");
308 printf(" Default is 10us where the TX_RING\n");
309 printf(" is populated with payload from uspace\n");
310 printf(" -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n");
311 printf(" -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n");
312 printf(" -H|--prio-high Make this high priority process\n");
313 printf(" -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n");
314 printf(" -v|--version Show version\n");
315 printf(" -h|--help Guess what?!\n");
316 printf("\n");
317 printf("Examples:\n");
318 printf(" See trafgen.txf for configuration file examples.\n");
319 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n");
320 printf(" trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n");
321 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n");
322 printf("\n");
323 printf("Note:\n");
324 printf(" This tool is targeted for network developers! You should\n");
325 printf(" be aware of what you are doing and what these options above\n");
326 printf(" mean! Only use this tool in an isolated LAN that you own!\n");
327 printf("\n");
328 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
329 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
330 printf("Swiss federal institute of technology (ETH Zurich)\n");
331 printf("License: GNU GPL version 2\n");
332 printf("This is free software: you are free to change and redistribute it.\n");
333 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
334 die();
337 static void version(void)
339 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
340 VERSION_STRING);
341 printf("http://www.netsniff-ng.org\n\n");
342 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
343 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
344 printf("Swiss federal institute of technology (ETH Zurich)\n");
345 printf("License: GNU GPL version 2\n");
346 printf("This is free software: you are free to change and redistribute it.\n");
347 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
348 die();
352 * TX_RING doen't allow us to specify inter-packet gaps, see
353 * http://lingrok.org/source/xref/linux-2.6-linus/net/packet/af_packet.c,
354 * function tpacket_fill_skb(), so instead, we use sendto(2). Since
355 * this is also not high-perf, copying between address spaces is okay.
357 static void tx_tgap_or_die(struct mode *mode, struct pktconf *cfg)
359 int ifindex, mtu, ret;
360 size_t l, c, r;
361 struct sockaddr_ll s_addr;
362 unsigned long num = 1;
363 char *pkt;
364 struct counter *cnt;
365 struct randomizer *rnd;
367 if (!mode || !cfg)
368 panic("Panic over invalid args for TX trigger!\n");
369 if (cfg->len == 0)
370 panic("Panic over invalid args for TX trigger!\n");
371 if (!device_up_and_running(mode->device))
372 panic("Device not up and running!\n");
374 mtu = device_mtu(mode->device);
375 for (l = 0; l < cfg->len; ++l) {
376 /* eth src + eth dst + type == 14, fcs added by driver */
377 if (cfg->pkts[l].plen > mtu + 14)
378 panic("Device MTU < than your packet size!\n");
379 if (cfg->pkts[l].plen <= 14)
380 panic("Device packet size too short!\n");
383 set_memcpy();
384 sock = pf_socket();
385 pkt = xmalloc_aligned(mtu, 64);
386 memset(pkt, 0, mtu);
387 ifindex = device_ifindex(mode->device);
389 if (cfg->num > 0)
390 num = cfg->num;
392 printf("MD: TX %s %luus\n\n", mode->rand ? "RND" : "RR", cfg->gap);
393 printf("Running! Hang up with ^C!\n\n");
395 memset(&s_addr, 0, sizeof(s_addr));
396 s_addr.sll_family = PF_PACKET;
397 s_addr.sll_halen = ETH_ALEN;
398 s_addr.sll_ifindex = ifindex;
400 l = 0;
401 while (likely(sigint == 0) && likely(num > 0)) {
402 for (c = 0; c < cfg->pkts[l].clen; ++c) {
403 cnt = &(cfg->pkts[l].cnt[c]);
404 cnt->val -= cnt->min;
405 cnt->val = (cnt->val + cnt->inc) %
406 (cnt->max - cnt->min + 1);
407 cnt->val += cnt->min;
408 cfg->pkts[l].payload[cnt->off] = cnt->val;
411 for (r = 0; r < cfg->pkts[l].rlen; ++r) {
412 rnd = &(cfg->pkts[l].rnd[r]);
413 rnd->val = lcrand(rnd->val);
414 cfg->pkts[l].payload[rnd->off] = rnd->val;
417 __memcpy(pkt, cfg->pkts[l].payload, cfg->pkts[l].plen);
418 mode->stats.tx_bytes += cfg->pkts[l].plen;
419 mode->stats.tx_packets++;
421 ret = sendto(sock, pkt, cfg->pkts[l].plen, 0,
422 (struct sockaddr *) &s_addr, sizeof(s_addr));
423 if (ret < 0)
424 whine("sendto error!\n");
425 if (mode->rand)
426 l = mt_rand_int32() % cfg->len;
427 else {
428 l++;
429 if (l >= cfg->len)
430 l = 0;
432 if (cfg->num > 0)
433 num--;
435 usleep(cfg->gap);
438 close(sock);
439 xfree(pkt);
441 fflush(stdout);
442 printf("\n");
443 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
444 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
447 static void tx_fire_or_die(struct mode *mode, struct pktconf *cfg)
449 int irq, ifindex, mtu;
450 unsigned int size, it = 0;
451 unsigned long num = 1;
452 uint8_t *out = NULL;
453 size_t l , c, r;
454 struct ring tx_ring;
455 struct frame_map *hdr;
456 struct counter *cnt;
457 struct randomizer *rnd;
459 if (!mode || !cfg)
460 panic("Panic over invalid args for TX trigger!\n");
461 if (cfg->len == 0)
462 panic("Panic over invalid args for TX trigger!\n");
463 if (!device_up_and_running(mode->device))
464 panic("Device not up and running!\n");
466 mtu = device_mtu(mode->device);
467 for (l = 0; l < cfg->len; ++l) {
468 /* eth src + eth dst + type == 14, fcs added by driver */
469 if (cfg->pkts[l].plen > mtu + 14)
470 panic("Device MTU < than your packet size!\n");
471 if (cfg->pkts[l].plen <= 14)
472 panic("Device packet size too short!\n");
475 set_memcpy();
476 sock = pf_socket();
478 memset(&tx_ring, 0, sizeof(tx_ring));
480 ifindex = device_ifindex(mode->device);
481 size = ring_size(mode->device, mode->reserve_size);
483 set_packet_loss_discard(sock);
484 setup_tx_ring_layout(sock, &tx_ring, size, mode->jumbo_support);
485 create_tx_ring(sock, &tx_ring);
486 mmap_tx_ring(sock, &tx_ring);
487 alloc_tx_ring_frames(&tx_ring);
488 bind_tx_ring(sock, &tx_ring, ifindex);
489 mt_init_by_seed_time();
491 if (mode->cpu >= 0 && ifindex > 0) {
492 irq = device_irq_number(mode->device);
493 device_bind_irq_to_cpu(mode->cpu, irq);
494 printf("IRQ: %s:%d > CPU%d\n", mode->device, irq,
495 mode->cpu);
498 if (mode->kpull)
499 interval = mode->kpull;
500 if (cfg->num > 0)
501 num = cfg->num;
503 printf("MD: FIRE %s %luus\n\n", mode->rand ? "RND" : "RR", interval);
504 printf("Running! Hang up with ^C!\n\n");
506 itimer.it_interval.tv_sec = 0;
507 itimer.it_interval.tv_usec = interval;
508 itimer.it_value.tv_sec = 0;
509 itimer.it_value.tv_usec = interval;
510 setitimer(ITIMER_REAL, &itimer, NULL);
512 l = 0;
513 while (likely(sigint == 0) && likely(num > 0)) {
514 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) &&
515 likely(num > 0)) {
516 hdr = tx_ring.frames[it].iov_base;
517 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
518 * sizeof(struct sockaddr_ll); */
519 out = ((uint8_t *) hdr) + TPACKET_HDRLEN -
520 sizeof(struct sockaddr_ll);
522 hdr->tp_h.tp_snaplen = cfg->pkts[l].plen;
523 hdr->tp_h.tp_len = cfg->pkts[l].plen;
525 for (c = 0; c < cfg->pkts[l].clen; ++c) {
526 cnt = &(cfg->pkts[l].cnt[c]);
527 cnt->val -= cnt->min;
528 cnt->val = (cnt->val + cnt->inc) %
529 (cnt->max - cnt->min + 1);
530 cnt->val += cnt->min;
531 cfg->pkts[l].payload[cnt->off] = cnt->val;
534 for (r = 0; r < cfg->pkts[l].rlen; ++r) {
535 rnd = &(cfg->pkts[l].rnd[r]);
536 rnd->val = lcrand(rnd->val);
537 cfg->pkts[l].payload[rnd->off] = rnd->val;
540 __memcpy(out, cfg->pkts[l].payload, cfg->pkts[l].plen);
541 mode->stats.tx_bytes += cfg->pkts[l].plen;
542 mode->stats.tx_packets++;
544 if (mode->rand)
545 l = mt_rand_int32() % cfg->len;
546 else {
547 l++;
548 if (l >= cfg->len)
549 l = 0;
552 kernel_may_pull_from_tx(&hdr->tp_h);
553 next_slot(&it, &tx_ring);
555 if (cfg->num > 0)
556 num--;
557 if (unlikely(sigint == 1))
558 break;
562 destroy_tx_ring(sock, &tx_ring);
563 close(sock);
565 fflush(stdout);
566 printf("\n");
567 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
568 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
571 #define TYPE_NUM 0
572 #define TYPE_CNT 1
573 #define TYPE_RND 2
574 #define TYPE_EOL 3
576 static inline char *getuint_or_obj(char *in, uint32_t *out, int *type)
578 if (*in == '\n') {
579 *type = TYPE_EOL;
580 } else if (*in == '$') {
581 in++;
582 if (!strncmp("II", in, strlen("II"))) {
583 in += 2;
584 in = getuint(in, out);
585 *type = TYPE_CNT;
586 } else if (!strncmp("PRB", in, strlen("PRB"))) {
587 *type = TYPE_RND;
588 in += 3;
589 } else
590 panic("Syntax error!\n");
591 } else {
592 in = getuint(in, out);
593 *type = TYPE_NUM;
596 return in;
599 static void dump_conf(struct pktconf *cfg)
601 size_t i, j;
603 printf("n %lu, gap %lu us, pkts %zu\n", cfg->num, cfg->gap, cfg->len);
604 if (cfg->len == 0)
605 return;
606 for (i = 0; i < cfg->len; ++i) {
607 printf("[%zu] pkt\n", i);
608 printf(" len %zu cnts %zu rnds %zu\n", cfg->pkts[i].plen,
609 cfg->pkts[i].clen, cfg->pkts[i].rlen);
610 printf(" payload ");
611 for (j = 0; j < cfg->pkts[i].plen; ++j)
612 printf("%02x ", cfg->pkts[i].payload[j]);
613 printf("\n");
614 for (j = 0; j < cfg->pkts[i].clen; ++j)
615 printf(" cnt%zu [%u,%u], inc %u, off %zu\n",
616 j, cfg->pkts[i].cnt[j].min,
617 cfg->pkts[i].cnt[j].max,
618 cfg->pkts[i].cnt[j].inc,
619 cfg->pkts[i].cnt[j].off);
620 for (j = 0; j < cfg->pkts[i].rlen; ++j)
621 printf(" rnd%zu off %zu\n",
622 j, cfg->pkts[i].rnd[j].off);
626 /* Seems to need a rewrite later ;-) */
627 static void parse_conf_or_die(char *file, struct pktconf *cfg)
629 unsigned int withinpkt = 0;
630 unsigned long line = 0;
631 char *pb, buff[1024];
632 FILE *fp;
633 struct counter *cnts = NULL;
634 size_t l = 0;
635 off_t offset = 0;
637 if (!file || !cfg)
638 panic("Panic over invalid args for the parser!\n");
640 fp = fopen(file, "r");
641 if (!fp)
642 panic("Cannot open config file!\n");
643 memset(buff, 0, sizeof(buff));
645 printf("CFG:\n");
646 srand(time(NULL));
648 while (fgets(buff, sizeof(buff), fp) != NULL) {
649 line++;
650 buff[sizeof(buff) - 1] = 0;
651 pb = skips(buff);
653 /* A comment or junk. Skip this line */
654 if (*pb == '#' || *pb == '\n') {
655 memset(buff, 0, sizeof(buff));
656 continue;
659 if (!withinpkt && *pb == '$') {
660 pb++;
661 if (!strncmp("II", pb, strlen("II"))) {
662 uint32_t id, min = 0, max = 0xFF, inc = 1;
663 pb += 2;
664 pb = getuint(pb, &id);
665 pb = skipchar(pb, ':');
666 pb = skips(pb);
667 pb = getuint(pb, &min);
668 pb = skipchar(pb, ',');
669 pb = getuint(pb, &max);
670 pb = skipchar(pb, ',');
671 pb = getuint(pb, &inc);
672 l++;
673 cnts = xrealloc(cnts, 1, l * sizeof(*cnts));
674 cnts[l - 1].id = 0xFF & id;
675 cnts[l - 1].min = 0xFF & min;
676 cnts[l - 1].max = 0xFF & max;
677 cnts[l - 1].inc = 0xFF & inc;
678 if (cnts[l - 1].min >= cnts[l - 1].max)
679 panic("Counter min >= max!\n");
680 if (cnts[l - 1].inc >= cnts[l - 1].max)
681 panic("Counter inc >= max!\n");
682 } else if (!strncmp("P", pb, strlen("P"))) {
683 uint32_t id;
684 pb++;
685 pb = getuint(pb, &id);
686 pb = skips(pb);
687 pb = skipchar(pb, '{');
688 withinpkt = 1;
689 cfg->len++;
690 cfg->pkts = xrealloc(cfg->pkts, 1,
691 cfg->len * sizeof(*cfg->pkts));
692 memset(&cfg->pkts[cfg->len - 1], 0,
693 sizeof(cfg->pkts[cfg->len - 1]));
694 offset = 0;
695 } else
696 panic("Unknown instruction! Syntax error "
697 "on line %lu!\n", line);
698 } else if (withinpkt && *pb == '}') {
699 withinpkt = 0;
700 } else if (withinpkt) {
701 int type, i, found;
702 uint32_t val = 0;
703 while (1) {
704 found = 0;
705 pb = getuint_or_obj(pb, &val, &type);
706 if (type == TYPE_EOL)
707 break;
708 if (type == TYPE_CNT) {
709 size_t z;
710 struct counter *new;
711 for (i = 0; i < l; ++i) {
712 if (val == cnts[i].id) {
713 found = 1;
714 break;
717 if (!found)
718 panic("Counter %u not found!\n");
720 val = cnts[i].min;
721 z = ++(cfg->pkts[cfg->len - 1].clen);
722 cfg->pkts[cfg->len - 1].cnt =
723 xrealloc(cfg->pkts[cfg->len - 1].cnt,
724 1, z * sizeof(struct counter));
725 new = &cfg->pkts[cfg->len - 1].cnt[z - 1];
726 new->min = cnts[i].min;
727 new->max = cnts[i].max;
728 new->inc = cnts[i].inc;
729 new->off = offset;
730 new->val = val;
731 } else if (type == TYPE_RND) {
732 size_t z;
733 struct randomizer *new;
735 val = 0xFF & rand();
736 z = ++(cfg->pkts[cfg->len - 1].rlen);
737 cfg->pkts[cfg->len - 1].rnd =
738 xrealloc(cfg->pkts[cfg->len - 1].rnd,
739 1, z * sizeof(struct randomizer));
740 new = &cfg->pkts[cfg->len - 1].rnd[z - 1];
741 new->val = val;
742 new->off = offset;
745 cfg->pkts[cfg->len - 1].plen++;
746 cfg->pkts[cfg->len - 1].payload =
747 xrealloc(cfg->pkts[cfg->len - 1].payload,
748 1, cfg->pkts[cfg->len - 1].plen);
749 cfg->pkts[cfg->len - 1].payload[cfg->pkts[cfg->len - 1].plen - 1] =
750 0xFF & val;
751 offset++;
752 pb = skipchar_s(pb, ',');
754 } else
755 panic("Syntax error!\n");
756 memset(buff, 0, sizeof(buff));
759 fclose(fp);
760 if (cnts)
761 xfree(cnts);
762 dump_conf(cfg);
765 static void cleanup_cfg(struct pktconf *cfg)
767 size_t l;
769 for (l = 0; l < cfg->len; ++l) {
770 if (cfg->pkts[l].plen > 0)
771 xfree(cfg->pkts[l].payload);
772 if (cfg->pkts[l].clen > 0)
773 xfree(cfg->pkts[l].cnt);
774 if (cfg->pkts[l].rlen > 0)
775 xfree(cfg->pkts[l].rnd);
778 if (cfg->len > 0)
779 xfree(cfg->pkts);
782 static int main_loop(struct mode *mode, char *confname, unsigned long pkts,
783 unsigned long gap)
785 struct pktconf cfg = {
786 .num = pkts,
787 .gap = gap,
788 .len = 0,
791 parse_conf_or_die(confname, &cfg);
792 if (gap > 0)
793 tx_tgap_or_die(mode, &cfg);
794 else
795 tx_fire_or_die(mode, &cfg);
796 cleanup_cfg(&cfg);
798 return 0;
801 int main(int argc, char **argv)
803 int c, opt_index, ret, i, j;
804 char *confname = NULL, *ptr;
805 unsigned long pkts = 0, gap = 0;
806 bool prio_high = false;
807 struct mode mode;
809 check_for_root_maybe_die();
811 memset(&mode, 0, sizeof(mode));
812 mode.cpu = CPU_UNKNOWN;
814 while ((c = getopt_long(argc, argv, short_options, long_options,
815 &opt_index)) != EOF) {
816 switch (c) {
817 case 'h':
818 help();
819 break;
820 case 'v':
821 version();
822 break;
823 case 'd':
824 mode.device = xstrndup(optarg, IFNAMSIZ);
825 break;
826 case 'r':
827 mode.rand = 1;
828 break;
829 case 'J':
830 mode.jumbo_support = 1;
831 break;
832 case 'c':
833 confname = xstrdup(optarg);
834 break;
835 case 'k':
836 mode.kpull = atol(optarg);
837 break;
838 case 'n':
839 pkts = atol(optarg);
840 break;
841 case 't':
842 gap = atol(optarg);
843 break;
844 case 'S':
845 ptr = optarg;
846 mode.reserve_size = 0;
848 for (j = i = strlen(optarg); i > 0; --i) {
849 if (!isdigit(optarg[j - i]))
850 break;
851 ptr++;
854 if (!strncmp(ptr, "KB", strlen("KB")))
855 mode.reserve_size = 1 << 10;
856 else if (!strncmp(ptr, "MB", strlen("MB")))
857 mode.reserve_size = 1 << 20;
858 else if (!strncmp(ptr, "GB", strlen("GB")))
859 mode.reserve_size = 1 << 30;
860 else
861 panic("Syntax error in ring size param!\n");
863 *ptr = 0;
864 mode.reserve_size *= atoi(optarg);
865 break;
866 case 'b':
867 set_cpu_affinity(optarg, 0);
868 /* Take the first CPU for rebinding the IRQ */
869 if (mode.cpu != CPU_NOTOUCH)
870 mode.cpu = atoi(optarg);
871 break;
872 case 'B':
873 set_cpu_affinity(optarg, 1);
874 break;
875 case 'H':
876 prio_high = true;
877 break;
878 case 'Q':
879 mode.cpu = CPU_NOTOUCH;
880 break;
881 case '?':
882 switch (optopt) {
883 case 'd':
884 case 'c':
885 case 'n':
886 case 'S':
887 case 'b':
888 case 'k':
889 case 'B':
890 case 't':
891 panic("Option -%c requires an argument!\n",
892 optopt);
893 default:
894 if (isprint(optopt))
895 whine("Unknown option character "
896 "`0x%X\'!\n", optopt);
897 die();
899 default:
900 break;
904 if (argc < 5)
905 help();
906 if (mode.device == NULL)
907 panic("No networking device given!\n");
908 if (confname == NULL)
909 panic("No configuration file given!\n");
910 if (device_mtu(mode.device) == 0)
911 panic("This is no networking device!\n");
912 if (device_up_and_running(mode.device) == 0)
913 panic("Networking device not running!\n");
915 register_signal(SIGINT, signal_handler);
916 register_signal(SIGHUP, signal_handler);
917 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
919 header();
921 if (prio_high == true) {
922 set_proc_prio(get_default_proc_prio());
923 set_sched_status(get_default_sched_policy(),
924 get_default_sched_prio());
927 ret = main_loop(&mode, confname, pkts, gap);
929 xfree(mode.device);
930 xfree(confname);
931 return ret;