xsys: removed signals.h and put stuff into xsys
[netsniff-ng.git] / src / trafgen.c
blob21da6cd17ada6f289f4a59b5d3a21209f953c36f
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 "strlcpy.h"
172 #include "parser.h"
173 #include "die.h"
174 #include "xsys.h"
175 #include "psched.h"
176 #include "xio.h"
177 #include "tty.h"
178 #include "timespec.h"
179 #include "mtrand.h"
180 #include "ring_tx.h"
182 struct counter {
183 uint16_t id;
184 uint8_t min;
185 uint8_t max;
186 uint8_t inc;
187 uint8_t val;
188 off_t off;
191 struct randomizer {
192 uint8_t val;
193 off_t off;
196 struct packet {
197 uint8_t *payload;
198 size_t plen;
199 struct counter *cnt;
200 size_t clen;
201 struct randomizer *rnd;
202 size_t rlen;
205 struct pktconf {
206 unsigned long num;
207 unsigned long gap;
208 struct packet *pkts;
209 size_t len;
212 struct stats {
213 unsigned long tx_bytes;
214 unsigned long tx_packets;
217 struct mode {
218 struct stats stats;
219 char *device;
220 int cpu;
221 int rand;
222 unsigned long kpull;
223 /* 0 for automatic, > 0 for manual */
224 unsigned int reserve_size;
225 int jumbo_support;
228 #define CPU_UNKNOWN -1
229 #define CPU_NOTOUCH -2
231 sig_atomic_t sigint = 0;
233 static const char *short_options = "d:c:n:t:vJhS:HQb:B:rk:";
235 static struct option long_options[] = {
236 {"dev", required_argument, 0, 'd'},
237 {"conf", required_argument, 0, 'c'},
238 {"num", required_argument, 0, 'n'},
239 {"gap", required_argument, 0, 't'},
240 {"ring-size", required_argument, 0, 'S'},
241 {"bind-cpu", required_argument, 0, 'b'},
242 {"unbind-cpu", required_argument, 0, 'B'},
243 {"kernel-pull", required_argument, 0, 'k'},
244 {"jumbo-support", no_argument, 0, 'J'},
245 {"rand", no_argument, 0, 'r'},
246 {"prio-high", no_argument, 0, 'H'},
247 {"notouch-irq", no_argument, 0, 'Q'},
248 {"version", no_argument, 0, 'v'},
249 {"help", no_argument, 0, 'h'},
250 {0, 0, 0, 0}
253 static struct itimerval itimer;
254 static int sock;
255 static unsigned long interval = TX_KERNEL_PULL_INT;
257 static inline uint8_t lcrand(uint8_t val)
259 return 0xFF & (3 * val + 3);
262 static void signal_handler(int number)
264 switch (number) {
265 case SIGINT:
266 sigint = 1;
267 break;
268 case SIGHUP:
269 break;
270 default:
271 break;
275 static void timer_elapsed(int number)
277 itimer.it_interval.tv_sec = 0;
278 itimer.it_interval.tv_usec = interval;
279 itimer.it_value.tv_sec = 0;
280 itimer.it_value.tv_usec = interval;
282 pull_and_flush_tx_ring(sock);
283 setitimer(ITIMER_REAL, &itimer, NULL);
286 static void header(void)
288 printf("%s%s%s\n", colorize_start(bold), "trafgen "
289 VERSION_STRING, colorize_end());
292 static void help(void)
294 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
295 VERSION_STRING);
296 printf("http://www.netsniff-ng.org\n\n");
297 printf("Usage: trafgen [options]\n");
298 printf("Options:\n");
299 printf(" -d|--dev <netdev> Networking Device i.e., eth0\n");
300 printf(" -c|--conf <file> Packet configuration file\n");
301 printf(" -J|--jumbo-support Support for 64KB Super Jumbo Frames\n");
302 printf(" Default TX slot: 2048Byte\n");
303 printf(" -n|--num <uint> Number of packets until exit\n");
304 printf(" `-- 0 Loop until interrupt (default)\n");
305 printf(" `- n Send n packets and done\n");
306 printf(" -r|--rand Randomize packet selection process\n");
307 printf(" Instead of a round robin selection\n");
308 printf(" -t|--gap <uint> Interpacket gap in us (approx)\n");
309 printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
310 printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
311 printf(" -k|--kernel-pull <uint>Kernel pull from user interval in us\n");
312 printf(" Default is 10us where the TX_RING\n");
313 printf(" is populated with payload from uspace\n");
314 printf(" -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n");
315 printf(" -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n");
316 printf(" -H|--prio-high Make this high priority process\n");
317 printf(" -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n");
318 printf(" -v|--version Show version\n");
319 printf(" -h|--help Guess what?!\n");
320 printf("\n");
321 printf("Examples:\n");
322 printf(" See trafgen.txf for configuration file examples.\n");
323 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n");
324 printf(" trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n");
325 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n");
326 printf("\n");
327 printf("Note:\n");
328 printf(" This tool is targeted for network developers! You should\n");
329 printf(" be aware of what you are doing and what these options above\n");
330 printf(" mean! Only use this tool in an isolated LAN that you own!\n");
331 printf("\n");
332 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
333 printf("Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
334 printf("Swiss federal institute of technology (ETH Zurich)\n");
335 printf("License: GNU GPL version 2\n");
336 printf("This is free software: you are free to change and redistribute it.\n");
337 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
339 die();
342 static void version(void)
344 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
345 VERSION_STRING);
346 printf("http://www.netsniff-ng.org\n\n");
347 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
348 printf("Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
349 printf("Swiss federal institute of technology (ETH Zurich)\n");
350 printf("License: GNU GPL version 2\n");
351 printf("This is free software: you are free to change and redistribute it.\n");
352 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
354 die();
358 * TX_RING doen't allow us to specify inter-packet gaps, see
359 * http://lingrok.org/source/xref/linux-2.6-linus/net/packet/af_packet.c,
360 * function tpacket_fill_skb(), so instead, we use sendto(2). Since
361 * this is also not high-perf, copying between address spaces is okay.
363 static void tx_tgap_or_die(struct mode *mode, struct pktconf *cfg)
365 int ifindex, mtu, ret;
366 size_t l, c, r;
367 struct sockaddr_ll s_addr;
368 unsigned long num = 1;
369 char *pkt;
370 struct counter *cnt;
371 struct randomizer *rnd;
373 if (!mode || !cfg)
374 panic("Panic over invalid args for TX trigger!\n");
375 if (cfg->len == 0)
376 panic("Panic over invalid args for TX trigger!\n");
377 if (!device_up_and_running(mode->device))
378 panic("Device not up and running!\n");
380 mtu = device_mtu(mode->device);
381 for (l = 0; l < cfg->len; ++l) {
382 /* eth src + eth dst + type == 14, fcs added by driver */
383 if (cfg->pkts[l].plen > mtu + 14)
384 panic("Device MTU < than your packet size!\n");
385 if (cfg->pkts[l].plen <= 14)
386 panic("Device packet size too short!\n");
389 set_memcpy();
390 sock = pf_socket();
391 pkt = xmalloc_aligned(mtu, 64);
392 memset(pkt, 0, mtu);
393 ifindex = device_ifindex(mode->device);
395 if (cfg->num > 0)
396 num = cfg->num;
398 printf("MD: TX %s %luus\n\n", mode->rand ? "RND" : "RR", cfg->gap);
399 printf("Running! Hang up with ^C!\n\n");
401 memset(&s_addr, 0, sizeof(s_addr));
402 s_addr.sll_family = PF_PACKET;
403 s_addr.sll_halen = ETH_ALEN;
404 s_addr.sll_ifindex = ifindex;
406 l = 0;
407 while (likely(sigint == 0) && likely(num > 0)) {
408 for (c = 0; c < cfg->pkts[l].clen; ++c) {
409 cnt = &(cfg->pkts[l].cnt[c]);
410 cnt->val -= cnt->min;
411 cnt->val = (cnt->val + cnt->inc) %
412 (cnt->max - cnt->min + 1);
413 cnt->val += cnt->min;
414 cfg->pkts[l].payload[cnt->off] = cnt->val;
417 for (r = 0; r < cfg->pkts[l].rlen; ++r) {
418 rnd = &(cfg->pkts[l].rnd[r]);
419 rnd->val = lcrand(rnd->val);
420 cfg->pkts[l].payload[rnd->off] = rnd->val;
423 __memcpy(pkt, cfg->pkts[l].payload, cfg->pkts[l].plen);
424 mode->stats.tx_bytes += cfg->pkts[l].plen;
425 mode->stats.tx_packets++;
427 ret = sendto(sock, pkt, cfg->pkts[l].plen, 0,
428 (struct sockaddr *) &s_addr, sizeof(s_addr));
429 if (ret < 0)
430 whine("sendto error!\n");
431 if (mode->rand)
432 l = mt_rand_int32() % cfg->len;
433 else {
434 l++;
435 if (l >= cfg->len)
436 l = 0;
438 if (cfg->num > 0)
439 num--;
441 xusleep2(cfg->gap);
444 close(sock);
445 xfree(pkt);
447 fflush(stdout);
448 printf("\n");
449 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
450 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
453 static void tx_fire_or_die(struct mode *mode, struct pktconf *cfg)
455 int irq, ifindex, mtu;
456 unsigned int size, it = 0;
457 unsigned long num = 1;
458 uint8_t *out = NULL;
459 size_t l , c, r;
460 struct ring tx_ring;
461 struct frame_map *hdr;
462 struct counter *cnt;
463 struct randomizer *rnd;
465 if (!mode || !cfg)
466 panic("Panic over invalid args for TX trigger!\n");
467 if (cfg->len == 0)
468 panic("Panic over invalid args for TX trigger!\n");
469 if (!device_up_and_running(mode->device))
470 panic("Device not up and running!\n");
472 mtu = device_mtu(mode->device);
473 for (l = 0; l < cfg->len; ++l) {
474 /* eth src + eth dst + type == 14, fcs added by driver */
475 if (cfg->pkts[l].plen > mtu + 14)
476 panic("Device MTU < than your packet size!\n");
477 if (cfg->pkts[l].plen <= 14)
478 panic("Device packet size too short!\n");
481 set_memcpy();
482 sock = pf_socket();
484 memset(&tx_ring, 0, sizeof(tx_ring));
486 ifindex = device_ifindex(mode->device);
487 size = ring_size(mode->device, mode->reserve_size);
489 set_packet_loss_discard(sock);
490 setup_tx_ring_layout(sock, &tx_ring, size, mode->jumbo_support);
491 create_tx_ring(sock, &tx_ring);
492 mmap_tx_ring(sock, &tx_ring);
493 alloc_tx_ring_frames(&tx_ring);
494 bind_tx_ring(sock, &tx_ring, ifindex);
495 mt_init_by_seed_time();
497 if (mode->cpu >= 0 && ifindex > 0) {
498 irq = device_irq_number(mode->device);
499 device_bind_irq_to_cpu(mode->cpu, irq);
500 printf("IRQ: %s:%d > CPU%d\n", mode->device, irq,
501 mode->cpu);
504 if (mode->kpull)
505 interval = mode->kpull;
506 if (cfg->num > 0)
507 num = cfg->num;
509 printf("MD: FIRE %s %luus\n\n", mode->rand ? "RND" : "RR", interval);
510 printf("Running! Hang up with ^C!\n\n");
512 itimer.it_interval.tv_sec = 0;
513 itimer.it_interval.tv_usec = interval;
514 itimer.it_value.tv_sec = 0;
515 itimer.it_value.tv_usec = interval;
516 setitimer(ITIMER_REAL, &itimer, NULL);
518 l = 0;
519 while (likely(sigint == 0) && likely(num > 0)) {
520 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) &&
521 likely(num > 0)) {
522 hdr = tx_ring.frames[it].iov_base;
523 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
524 * sizeof(struct sockaddr_ll); */
525 out = ((uint8_t *) hdr) + TPACKET_HDRLEN -
526 sizeof(struct sockaddr_ll);
528 hdr->tp_h.tp_snaplen = cfg->pkts[l].plen;
529 hdr->tp_h.tp_len = cfg->pkts[l].plen;
531 for (c = 0; c < cfg->pkts[l].clen; ++c) {
532 cnt = &(cfg->pkts[l].cnt[c]);
533 cnt->val -= cnt->min;
534 cnt->val = (cnt->val + cnt->inc) %
535 (cnt->max - cnt->min + 1);
536 cnt->val += cnt->min;
537 cfg->pkts[l].payload[cnt->off] = cnt->val;
540 for (r = 0; r < cfg->pkts[l].rlen; ++r) {
541 rnd = &(cfg->pkts[l].rnd[r]);
542 rnd->val = lcrand(rnd->val);
543 cfg->pkts[l].payload[rnd->off] = rnd->val;
546 __memcpy(out, cfg->pkts[l].payload, cfg->pkts[l].plen);
547 mode->stats.tx_bytes += cfg->pkts[l].plen;
548 mode->stats.tx_packets++;
550 if (mode->rand)
551 l = mt_rand_int32() % cfg->len;
552 else {
553 l++;
554 if (l >= cfg->len)
555 l = 0;
558 kernel_may_pull_from_tx(&hdr->tp_h);
559 next_slot(&it, &tx_ring);
561 if (cfg->num > 0)
562 num--;
563 if (unlikely(sigint == 1))
564 break;
568 destroy_tx_ring(sock, &tx_ring);
569 close(sock);
571 fflush(stdout);
572 printf("\n");
573 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
574 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
577 #define TYPE_NUM 0
578 #define TYPE_CNT 1
579 #define TYPE_RND 2
580 #define TYPE_EOL 3
582 static inline char *getuint_or_obj(char *in, uint32_t *out, int *type)
584 if (*in == '\n') {
585 *type = TYPE_EOL;
586 } else if (*in == '$') {
587 in++;
588 if (!strncmp("II", in, strlen("II"))) {
589 in += 2;
590 in = getuint(in, out);
591 *type = TYPE_CNT;
592 } else if (!strncmp("PRB", in, strlen("PRB"))) {
593 *type = TYPE_RND;
594 in += 3;
595 } else
596 panic("Syntax error!\n");
597 } else {
598 in = getuint(in, out);
599 *type = TYPE_NUM;
602 return in;
605 static void dump_conf(struct pktconf *cfg)
607 size_t i, j;
609 info("n %lu, gap %lu us, pkts %zu\n", cfg->num, cfg->gap, cfg->len);
610 if (cfg->len == 0)
611 return;
613 for (i = 0; i < cfg->len; ++i) {
614 info("[%zu] pkt\n", i);
615 info(" len %zu cnts %zu rnds %zu\n", cfg->pkts[i].plen,
616 cfg->pkts[i].clen, cfg->pkts[i].rlen);
617 info(" payload ");
618 for (j = 0; j < cfg->pkts[i].plen; ++j)
619 info("%02x ", cfg->pkts[i].payload[j]);
620 info("\n");
621 for (j = 0; j < cfg->pkts[i].clen; ++j)
622 info(" cnt%zu [%u,%u], inc %u, off %zu\n",
623 j, cfg->pkts[i].cnt[j].min,
624 cfg->pkts[i].cnt[j].max,
625 cfg->pkts[i].cnt[j].inc,
626 cfg->pkts[i].cnt[j].off);
627 for (j = 0; j < cfg->pkts[i].rlen; ++j)
628 info(" rnd%zu off %zu\n",
629 j, cfg->pkts[i].rnd[j].off);
633 /* Seems to need a rewrite later ;-) */
634 static void parse_conf_or_die(char *file, struct pktconf *cfg)
636 unsigned int withinpkt = 0;
637 unsigned long line = 0;
638 char *pb, buff[1024];
639 FILE *fp;
640 struct counter *cnts = NULL;
641 size_t l = 0;
642 off_t offset = 0;
644 if (!file || !cfg)
645 panic("Panic over invalid args for the parser!\n");
647 fp = fopen(file, "r");
648 if (!fp)
649 panic("Cannot open config file!\n");
650 memset(buff, 0, sizeof(buff));
652 info("CFG:\n");
653 srand(time(NULL));
655 while (fgets(buff, sizeof(buff), fp) != NULL) {
656 line++;
657 buff[sizeof(buff) - 1] = 0;
658 pb = skips(buff);
660 /* A comment or junk. Skip this line */
661 if (*pb == '#' || *pb == '\n') {
662 memset(buff, 0, sizeof(buff));
663 continue;
666 if (!withinpkt && *pb == '$') {
667 pb++;
668 if (!strncmp("II", pb, strlen("II"))) {
669 uint32_t id, min = 0, max = 0xFF, inc = 1;
670 pb += 2;
671 pb = getuint(pb, &id);
672 pb = skipchar(pb, ':');
673 pb = skips(pb);
674 pb = getuint(pb, &min);
675 pb = skipchar(pb, ',');
676 pb = getuint(pb, &max);
677 pb = skipchar(pb, ',');
678 pb = getuint(pb, &inc);
679 l++;
680 cnts = xrealloc(cnts, 1, l * sizeof(*cnts));
681 cnts[l - 1].id = 0xFF & id;
682 cnts[l - 1].min = 0xFF & min;
683 cnts[l - 1].max = 0xFF & max;
684 cnts[l - 1].inc = 0xFF & inc;
685 if (cnts[l - 1].min >= cnts[l - 1].max)
686 panic("Counter min >= max!\n");
687 if (cnts[l - 1].inc >= cnts[l - 1].max)
688 panic("Counter inc >= max!\n");
689 } else if (!strncmp("P", pb, strlen("P"))) {
690 uint32_t id;
691 pb++;
692 pb = getuint(pb, &id);
693 pb = skips(pb);
694 pb = skipchar(pb, '{');
695 withinpkt = 1;
696 cfg->len++;
697 cfg->pkts = xrealloc(cfg->pkts, 1,
698 cfg->len * sizeof(*cfg->pkts));
699 memset(&cfg->pkts[cfg->len - 1], 0,
700 sizeof(cfg->pkts[cfg->len - 1]));
701 offset = 0;
702 } else
703 panic("Unknown instruction! Syntax error "
704 "on line %lu!\n", line);
705 } else if (withinpkt && *pb == '}') {
706 withinpkt = 0;
707 } else if (withinpkt) {
708 int type, i, found;
709 uint32_t val = 0;
710 while (1) {
711 found = 0;
712 pb = getuint_or_obj(pb, &val, &type);
713 if (type == TYPE_EOL)
714 break;
715 if (type == TYPE_CNT) {
716 size_t z;
717 struct counter *new;
718 for (i = 0; i < l; ++i) {
719 if (val == cnts[i].id) {
720 found = 1;
721 break;
724 if (!found)
725 panic("Counter %u not found!\n");
727 val = cnts[i].min;
728 z = ++(cfg->pkts[cfg->len - 1].clen);
729 cfg->pkts[cfg->len - 1].cnt =
730 xrealloc(cfg->pkts[cfg->len - 1].cnt,
731 1, z * sizeof(struct counter));
732 new = &cfg->pkts[cfg->len - 1].cnt[z - 1];
733 new->min = cnts[i].min;
734 new->max = cnts[i].max;
735 new->inc = cnts[i].inc;
736 new->off = offset;
737 new->val = val;
738 } else if (type == TYPE_RND) {
739 size_t z;
740 struct randomizer *new;
742 val = 0xFF & rand();
743 z = ++(cfg->pkts[cfg->len - 1].rlen);
744 cfg->pkts[cfg->len - 1].rnd =
745 xrealloc(cfg->pkts[cfg->len - 1].rnd,
746 1, z * sizeof(struct randomizer));
747 new = &cfg->pkts[cfg->len - 1].rnd[z - 1];
748 new->val = val;
749 new->off = offset;
752 cfg->pkts[cfg->len - 1].plen++;
753 cfg->pkts[cfg->len - 1].payload =
754 xrealloc(cfg->pkts[cfg->len - 1].payload,
755 1, cfg->pkts[cfg->len - 1].plen);
756 cfg->pkts[cfg->len - 1].payload[cfg->pkts[cfg->len - 1].plen - 1] =
757 0xFF & val;
758 offset++;
759 pb = skipchar_s(pb, ',');
761 } else
762 panic("Syntax error!\n");
763 memset(buff, 0, sizeof(buff));
766 fclose(fp);
767 if (cnts)
768 xfree(cnts);
769 dump_conf(cfg);
772 static void cleanup_cfg(struct pktconf *cfg)
774 size_t l;
776 for (l = 0; l < cfg->len; ++l) {
777 if (cfg->pkts[l].plen > 0)
778 xfree(cfg->pkts[l].payload);
779 if (cfg->pkts[l].clen > 0)
780 xfree(cfg->pkts[l].cnt);
781 if (cfg->pkts[l].rlen > 0)
782 xfree(cfg->pkts[l].rnd);
785 if (cfg->len > 0)
786 xfree(cfg->pkts);
789 static int main_loop(struct mode *mode, char *confname, unsigned long pkts,
790 unsigned long gap)
792 struct pktconf cfg = {
793 .num = pkts,
794 .gap = gap,
795 .len = 0,
798 parse_conf_or_die(confname, &cfg);
799 if (gap > 0)
800 tx_tgap_or_die(mode, &cfg);
801 else
802 tx_fire_or_die(mode, &cfg);
803 cleanup_cfg(&cfg);
805 return 0;
808 int main(int argc, char **argv)
810 int c, opt_index, ret, i, j;
811 char *confname = NULL, *ptr;
812 unsigned long pkts = 0, gap = 0;
813 bool prio_high = false;
814 struct mode mode;
816 check_for_root_maybe_die();
818 memset(&mode, 0, sizeof(mode));
819 mode.cpu = CPU_UNKNOWN;
821 while ((c = getopt_long(argc, argv, short_options, long_options,
822 &opt_index)) != EOF) {
823 switch (c) {
824 case 'h':
825 help();
826 break;
827 case 'v':
828 version();
829 break;
830 case 'd':
831 mode.device = xstrndup(optarg, IFNAMSIZ);
832 break;
833 case 'r':
834 mode.rand = 1;
835 break;
836 case 'J':
837 mode.jumbo_support = 1;
838 break;
839 case 'c':
840 confname = xstrdup(optarg);
841 break;
842 case 'k':
843 mode.kpull = atol(optarg);
844 break;
845 case 'n':
846 pkts = atol(optarg);
847 break;
848 case 't':
849 gap = atol(optarg);
850 break;
851 case 'S':
852 ptr = optarg;
853 mode.reserve_size = 0;
855 for (j = i = strlen(optarg); i > 0; --i) {
856 if (!isdigit(optarg[j - i]))
857 break;
858 ptr++;
861 if (!strncmp(ptr, "KB", strlen("KB")))
862 mode.reserve_size = 1 << 10;
863 else if (!strncmp(ptr, "MB", strlen("MB")))
864 mode.reserve_size = 1 << 20;
865 else if (!strncmp(ptr, "GB", strlen("GB")))
866 mode.reserve_size = 1 << 30;
867 else
868 panic("Syntax error in ring size param!\n");
870 *ptr = 0;
871 mode.reserve_size *= atoi(optarg);
872 break;
873 case 'b':
874 set_cpu_affinity(optarg, 0);
875 /* Take the first CPU for rebinding the IRQ */
876 if (mode.cpu != CPU_NOTOUCH)
877 mode.cpu = atoi(optarg);
878 break;
879 case 'B':
880 set_cpu_affinity(optarg, 1);
881 break;
882 case 'H':
883 prio_high = true;
884 break;
885 case 'Q':
886 mode.cpu = CPU_NOTOUCH;
887 break;
888 case '?':
889 switch (optopt) {
890 case 'd':
891 case 'c':
892 case 'n':
893 case 'S':
894 case 'b':
895 case 'k':
896 case 'B':
897 case 't':
898 panic("Option -%c requires an argument!\n",
899 optopt);
900 default:
901 if (isprint(optopt))
902 whine("Unknown option character "
903 "`0x%X\'!\n", optopt);
904 die();
906 default:
907 break;
911 if (argc < 5)
912 help();
913 if (mode.device == NULL)
914 panic("No networking device given!\n");
915 if (confname == NULL)
916 panic("No configuration file given!\n");
917 if (device_mtu(mode.device) == 0)
918 panic("This is no networking device!\n");
919 if (device_up_and_running(mode.device) == 0)
920 panic("Networking device not running!\n");
922 register_signal(SIGINT, signal_handler);
923 register_signal(SIGHUP, signal_handler);
924 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
926 header();
928 if (prio_high == true) {
929 set_proc_prio(get_default_proc_prio());
930 set_sched_status(get_default_sched_policy(),
931 get_default_sched_prio());
934 ret = main_loop(&mode, confname, pkts, gap);
936 xfree(mode.device);
937 xfree(confname);
938 return ret;