proto_ipv6_mobility_hdr.h: Headerfixing
[netsniff-ng.git] / src / trafgen.c
blob6aca0ec3de593abf435a2512255072a594e99455
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 "xstring.h"
175 #include "die.h"
176 #include "xsys.h"
177 #include "xio.h"
178 #include "tprintf.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 extern int main_loop_interactive(struct mode *mode, char *confname);
233 sig_atomic_t sigint = 0;
235 static const char *short_options = "d:c:n:t:vJhS:HQb:B:rk:x";
237 static struct option long_options[] = {
238 {"dev", required_argument, 0, 'd'},
239 {"conf", required_argument, 0, 'c'},
240 {"num", required_argument, 0, 'n'},
241 {"gap", required_argument, 0, 't'},
242 {"ring-size", required_argument, 0, 'S'},
243 {"bind-cpu", required_argument, 0, 'b'},
244 {"unbind-cpu", required_argument, 0, 'B'},
245 {"kernel-pull", required_argument, 0, 'k'},
246 {"jumbo-support", no_argument, 0, 'J'},
247 {"interactive", no_argument, 0, 'x'},
248 {"rand", no_argument, 0, 'r'},
249 {"prio-high", no_argument, 0, 'H'},
250 {"notouch-irq", no_argument, 0, 'Q'},
251 {"version", no_argument, 0, 'v'},
252 {"help", no_argument, 0, 'h'},
253 {0, 0, 0, 0}
256 static struct itimerval itimer;
257 static int sock;
258 static unsigned long interval = TX_KERNEL_PULL_INT;
260 static inline uint8_t lcrand(uint8_t val)
262 return 0xFF & (3 * val + 3);
265 static void signal_handler(int number)
267 switch (number) {
268 case SIGINT:
269 sigint = 1;
270 break;
271 case SIGHUP:
272 break;
273 default:
274 break;
278 static void timer_elapsed(int number)
280 itimer.it_interval.tv_sec = 0;
281 itimer.it_interval.tv_usec = interval;
282 itimer.it_value.tv_sec = 0;
283 itimer.it_value.tv_usec = interval;
285 pull_and_flush_tx_ring(sock);
286 setitimer(ITIMER_REAL, &itimer, NULL);
289 static void header(void)
291 printf("%s%s%s\n", colorize_start(bold), "trafgen "
292 VERSION_STRING, colorize_end());
295 static void help(void)
297 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
298 VERSION_STRING);
299 printf("http://www.netsniff-ng.org\n\n");
300 printf("Usage: trafgen [options]\n");
301 printf("Options:\n");
302 printf(" -d|--dev <netdev> Networking Device i.e., eth0\n");
303 printf(" -c|--conf <file> Packet configuration file\n");
304 printf(" -x|--interactive Start trafgen in interactive mode\n");
305 printf(" -J|--jumbo-support Support for 64KB Super Jumbo Frames\n");
306 printf(" Default TX slot: 2048Byte\n");
307 printf(" -n|--num <uint> Number of packets until exit\n");
308 printf(" `-- 0 Loop until interrupt (default)\n");
309 printf(" `- n Send n packets and done\n");
310 printf(" -r|--rand Randomize packet selection process\n");
311 printf(" Instead of a round robin selection\n");
312 printf(" -t|--gap <uint> Interpacket gap in us (approx)\n");
313 printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
314 printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
315 printf(" -k|--kernel-pull <uint>Kernel pull from user interval in us\n");
316 printf(" Default is 10us where the TX_RING\n");
317 printf(" is populated with payload from uspace\n");
318 printf(" -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n");
319 printf(" -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n");
320 printf(" -H|--prio-high Make this high priority process\n");
321 printf(" -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n");
322 printf(" -v|--version Show version\n");
323 printf(" -h|--help Guess what?!\n");
324 printf("\n");
325 printf("Examples:\n");
326 printf(" See trafgen.txf for configuration file examples.\n");
327 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0\n");
328 printf(" trafgen --dev eth0 --conf trafgen.txf --rand --gap 1000\n");
329 printf(" trafgen --dev eth0 --conf trafgen.txf --bind-cpu 0 --num 10 --rand\n");
330 printf(" trafgen --interactive\n");
331 printf(" trafgen --interactive --dev mgmt0 (only start server on mgmt0)\n");
332 printf(" trafgen --interactive --conf trafgen-cli.batch\n");
333 printf("\n");
334 printf("Note:\n");
335 printf(" This tool is targeted for network developers! You should\n");
336 printf(" be aware of what you are doing and what these options above\n");
337 printf(" mean! Only use this tool in an isolated LAN that you own!\n");
338 printf("\n");
339 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
340 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
341 printf("Swiss federal institute of technology (ETH Zurich)\n");
342 printf("License: GNU GPL version 2\n");
343 printf("This is free software: you are free to change and redistribute it.\n");
344 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
345 die();
348 static void version(void)
350 printf("\ntrafgen %s, high-perf zero-copy network packet generator\n",
351 VERSION_STRING);
352 printf("http://www.netsniff-ng.org\n\n");
353 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
354 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
355 printf("Swiss federal institute of technology (ETH Zurich)\n");
356 printf("License: GNU GPL version 2\n");
357 printf("This is free software: you are free to change and redistribute it.\n");
358 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
359 die();
363 * TX_RING doen't allow us to specify inter-packet gaps, see
364 * http://lingrok.org/source/xref/linux-2.6-linus/net/packet/af_packet.c,
365 * function tpacket_fill_skb(), so instead, we use sendto(2). Since
366 * this is also not high-perf, copying between address spaces is okay.
368 static void tx_tgap_or_die(struct mode *mode, struct pktconf *cfg)
370 int ifindex, mtu, ret;
371 size_t l, c, r;
372 struct sockaddr_ll s_addr;
373 unsigned long num = 1;
374 char *pkt;
375 struct counter *cnt;
376 struct randomizer *rnd;
378 if (!mode || !cfg)
379 panic("Panic over invalid args for TX trigger!\n");
380 if (cfg->len == 0)
381 panic("Panic over invalid args for TX trigger!\n");
382 if (!device_up_and_running(mode->device))
383 panic("Device not up and running!\n");
385 mtu = device_mtu(mode->device);
386 for (l = 0; l < cfg->len; ++l) {
387 /* eth src + eth dst + type == 14, fcs added by driver */
388 if (cfg->pkts[l].plen > mtu + 14)
389 panic("Device MTU < than your packet size!\n");
390 if (cfg->pkts[l].plen <= 14)
391 panic("Device packet size too short!\n");
394 sock = pf_socket();
395 pkt = xmalloc_aligned(mtu, 64);
396 fmemset(pkt, 0, mtu);
397 ifindex = device_ifindex(mode->device);
399 if (cfg->num > 0)
400 num = cfg->num;
402 printf("MD: TX %s %luus\n\n", mode->rand ? "RND" : "RR", cfg->gap);
403 printf("Running! Hang up with ^C!\n\n");
405 fmemset(&s_addr, 0, sizeof(s_addr));
406 s_addr.sll_family = PF_PACKET;
407 s_addr.sll_halen = ETH_ALEN;
408 s_addr.sll_ifindex = ifindex;
410 l = 0;
411 while (likely(sigint == 0) && likely(num > 0)) {
412 for (c = 0; c < cfg->pkts[l].clen; ++c) {
413 cnt = &(cfg->pkts[l].cnt[c]);
414 cnt->val -= cnt->min;
415 cnt->val = (cnt->val + cnt->inc) %
416 (cnt->max - cnt->min + 1);
417 cnt->val += cnt->min;
418 cfg->pkts[l].payload[cnt->off] = cnt->val;
421 for (r = 0; r < cfg->pkts[l].rlen; ++r) {
422 rnd = &(cfg->pkts[l].rnd[r]);
423 rnd->val = lcrand(rnd->val);
424 cfg->pkts[l].payload[rnd->off] = rnd->val;
427 fmemcpy(pkt, cfg->pkts[l].payload, cfg->pkts[l].plen);
428 mode->stats.tx_bytes += cfg->pkts[l].plen;
429 mode->stats.tx_packets++;
431 ret = sendto(sock, pkt, cfg->pkts[l].plen, 0,
432 (struct sockaddr *) &s_addr, sizeof(s_addr));
433 if (ret < 0)
434 whine("sendto error!\n");
435 if (mode->rand)
436 l = mt_rand_int32() % cfg->len;
437 else {
438 l++;
439 if (l >= cfg->len)
440 l = 0;
442 if (cfg->num > 0)
443 num--;
445 usleep(cfg->gap);
448 close(sock);
449 xfree(pkt);
451 fflush(stdout);
452 printf("\n");
453 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
454 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
457 static void tx_fire_or_die(struct mode *mode, struct pktconf *cfg)
459 int irq, ifindex, mtu;
460 unsigned int size, it = 0;
461 unsigned long num = 1;
462 uint8_t *out = NULL;
463 size_t l , c, r;
464 struct ring tx_ring;
465 struct frame_map *hdr;
466 struct counter *cnt;
467 struct randomizer *rnd;
469 if (!mode || !cfg)
470 panic("Panic over invalid args for TX trigger!\n");
471 if (cfg->len == 0)
472 panic("Panic over invalid args for TX trigger!\n");
473 if (!device_up_and_running(mode->device))
474 panic("Device not up and running!\n");
476 mtu = device_mtu(mode->device);
477 for (l = 0; l < cfg->len; ++l) {
478 /* eth src + eth dst + type == 14, fcs added by driver */
479 if (cfg->pkts[l].plen > mtu + 14)
480 panic("Device MTU < than your packet size!\n");
481 if (cfg->pkts[l].plen <= 14)
482 panic("Device packet size too short!\n");
485 sock = pf_socket();
487 fmemset(&tx_ring, 0, sizeof(tx_ring));
489 ifindex = device_ifindex(mode->device);
490 size = ring_size(mode->device, mode->reserve_size);
492 set_packet_loss_discard(sock);
493 setup_tx_ring_layout(sock, &tx_ring, size, mode->jumbo_support);
494 create_tx_ring(sock, &tx_ring);
495 mmap_tx_ring(sock, &tx_ring);
496 alloc_tx_ring_frames(&tx_ring);
497 bind_tx_ring(sock, &tx_ring, ifindex);
498 mt_init_by_seed_time();
500 if (mode->cpu >= 0 && ifindex > 0) {
501 irq = device_irq_number(mode->device);
502 device_bind_irq_to_cpu(mode->cpu, irq);
503 printf("IRQ: %s:%d > CPU%d\n", mode->device, irq,
504 mode->cpu);
507 if (mode->kpull)
508 interval = mode->kpull;
509 if (cfg->num > 0)
510 num = cfg->num;
512 printf("MD: FIRE %s %luus\n\n", mode->rand ? "RND" : "RR", interval);
513 printf("Running! Hang up with ^C!\n\n");
515 itimer.it_interval.tv_sec = 0;
516 itimer.it_interval.tv_usec = interval;
517 itimer.it_value.tv_sec = 0;
518 itimer.it_value.tv_usec = interval;
519 setitimer(ITIMER_REAL, &itimer, NULL);
521 l = 0;
522 while (likely(sigint == 0) && likely(num > 0)) {
523 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) &&
524 likely(num > 0)) {
525 hdr = tx_ring.frames[it].iov_base;
526 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
527 * sizeof(struct sockaddr_ll); */
528 out = ((uint8_t *) hdr) + TPACKET_HDRLEN -
529 sizeof(struct sockaddr_ll);
531 hdr->tp_h.tp_snaplen = cfg->pkts[l].plen;
532 hdr->tp_h.tp_len = cfg->pkts[l].plen;
534 for (c = 0; c < cfg->pkts[l].clen; ++c) {
535 cnt = &(cfg->pkts[l].cnt[c]);
536 cnt->val -= cnt->min;
537 cnt->val = (cnt->val + cnt->inc) %
538 (cnt->max - cnt->min + 1);
539 cnt->val += cnt->min;
540 cfg->pkts[l].payload[cnt->off] = cnt->val;
543 for (r = 0; r < cfg->pkts[l].rlen; ++r) {
544 rnd = &(cfg->pkts[l].rnd[r]);
545 rnd->val = lcrand(rnd->val);
546 cfg->pkts[l].payload[rnd->off] = rnd->val;
549 fmemcpy(out, cfg->pkts[l].payload, cfg->pkts[l].plen);
550 mode->stats.tx_bytes += cfg->pkts[l].plen;
551 mode->stats.tx_packets++;
553 if (mode->rand)
554 l = mt_rand_int32() % cfg->len;
555 else {
556 l++;
557 if (l >= cfg->len)
558 l = 0;
561 kernel_may_pull_from_tx(&hdr->tp_h);
562 next_slot_prewr(&it, &tx_ring);
564 if (cfg->num > 0)
565 num--;
566 if (unlikely(sigint == 1))
567 break;
571 destroy_tx_ring(sock, &tx_ring);
572 close(sock);
574 fflush(stdout);
575 printf("\n");
576 printf("\r%12lu frames outgoing\n", mode->stats.tx_packets);
577 printf("\r%12lu bytes outgoing\n", mode->stats.tx_bytes);
580 #define TYPE_NUM 0
581 #define TYPE_CNT 1
582 #define TYPE_RND 2
583 #define TYPE_EOL 3
585 static inline char *getuint_or_obj(char *in, uint32_t *out, int *type)
587 if (*in == '\n') {
588 *type = TYPE_EOL;
589 } else if (*in == '$') {
590 in++;
591 if (!strncmp("II", in, strlen("II"))) {
592 in += 2;
593 in = getuint(in, out);
594 *type = TYPE_CNT;
595 } else if (!strncmp("PRB", in, strlen("PRB"))) {
596 *type = TYPE_RND;
597 in += 3;
598 } else
599 panic("Syntax error!\n");
600 } else {
601 in = getuint(in, out);
602 *type = TYPE_NUM;
605 return in;
608 static void dump_conf(struct pktconf *cfg)
610 size_t i, j;
612 printf("n %lu, gap %lu us, pkts %zu\n", cfg->num, cfg->gap, cfg->len);
613 if (cfg->len == 0)
614 return;
615 for (i = 0; i < cfg->len; ++i) {
616 printf("[%zu] pkt\n", i);
617 printf(" len %zu cnts %zu rnds %zu\n", cfg->pkts[i].plen,
618 cfg->pkts[i].clen, cfg->pkts[i].rlen);
619 printf(" payload ");
620 for (j = 0; j < cfg->pkts[i].plen; ++j)
621 printf("%02x ", cfg->pkts[i].payload[j]);
622 printf("\n");
623 for (j = 0; j < cfg->pkts[i].clen; ++j)
624 printf(" cnt%zu [%u,%u], inc %u, off %ld\n",
625 j, cfg->pkts[i].cnt[j].min,
626 cfg->pkts[i].cnt[j].max,
627 cfg->pkts[i].cnt[j].inc,
628 cfg->pkts[i].cnt[j].off);
629 for (j = 0; j < cfg->pkts[i].rlen; ++j)
630 printf(" rnd%zu off %ld\n",
631 j, cfg->pkts[i].rnd[j].off);
635 /* Seems to need a rewrite later ;-) */
636 static void parse_conf_or_die(char *file, struct pktconf *cfg)
638 unsigned int withinpkt = 0;
639 unsigned long line = 0;
640 char *pb, buff[1024];
641 FILE *fp;
642 struct counter *cnts = NULL;
643 size_t l = 0;
644 off_t offset = 0;
646 if (!file || !cfg)
647 panic("Panic over invalid args for the parser!\n");
649 fp = fopen(file, "r");
650 if (!fp)
651 panic("Cannot open config file!\n");
652 fmemset(buff, 0, sizeof(buff));
654 printf("CFG:\n");
655 srand(time(NULL));
657 while (fgets(buff, sizeof(buff), fp) != NULL) {
658 line++;
659 buff[sizeof(buff) - 1] = 0;
660 pb = skips(buff);
662 /* A comment or junk. Skip this line */
663 if (*pb == '#' || *pb == '\n') {
664 fmemset(buff, 0, sizeof(buff));
665 continue;
668 if (!withinpkt && *pb == '$') {
669 pb++;
670 if (!strncmp("II", pb, strlen("II"))) {
671 uint32_t id, min = 0, max = 0xFF, inc = 1;
672 pb += 2;
673 pb = getuint(pb, &id);
674 pb = skipchar(pb, ':');
675 pb = skips(pb);
676 pb = getuint(pb, &min);
677 pb = skipchar(pb, ',');
678 pb = getuint(pb, &max);
679 pb = skipchar(pb, ',');
680 pb = getuint(pb, &inc);
681 l++;
682 cnts = xrealloc(cnts, 1, l * sizeof(*cnts));
683 cnts[l - 1].id = 0xFF & id;
684 cnts[l - 1].min = 0xFF & min;
685 cnts[l - 1].max = 0xFF & max;
686 cnts[l - 1].inc = 0xFF & inc;
687 if (cnts[l - 1].min >= cnts[l - 1].max)
688 panic("Counter min >= max!\n");
689 if (cnts[l - 1].inc >= cnts[l - 1].max)
690 panic("Counter inc >= max!\n");
691 } else if (!strncmp("P", pb, strlen("P"))) {
692 uint32_t id;
693 pb++;
694 pb = getuint(pb, &id);
695 pb = skips(pb);
696 pb = skipchar(pb, '{');
697 withinpkt = 1;
698 cfg->len++;
699 cfg->pkts = xrealloc(cfg->pkts, 1,
700 cfg->len * sizeof(*cfg->pkts));
701 fmemset(&cfg->pkts[cfg->len - 1], 0,
702 sizeof(cfg->pkts[cfg->len - 1]));
703 offset = 0;
704 } else
705 panic("Unknown instruction! Syntax error "
706 "on line %lu!\n", line);
707 } else if (withinpkt && *pb == '}') {
708 withinpkt = 0;
709 } else if (withinpkt) {
710 int type, i, found;
711 uint32_t val = 0;
712 while (1) {
713 found = 0;
714 pb = getuint_or_obj(pb, &val, &type);
715 if (type == TYPE_EOL)
716 break;
717 if (type == TYPE_CNT) {
718 size_t z;
719 struct counter *new;
720 for (i = 0; i < l; ++i) {
721 if (val == cnts[i].id) {
722 found = 1;
723 break;
726 if (!found)
727 panic("Counter %u not found!\n");
729 val = cnts[i].min;
730 z = ++(cfg->pkts[cfg->len - 1].clen);
731 cfg->pkts[cfg->len - 1].cnt =
732 xrealloc(cfg->pkts[cfg->len - 1].cnt,
733 1, z * sizeof(struct counter));
734 new = &cfg->pkts[cfg->len - 1].cnt[z - 1];
735 new->min = cnts[i].min;
736 new->max = cnts[i].max;
737 new->inc = cnts[i].inc;
738 new->off = offset;
739 new->val = val;
740 } else if (type == TYPE_RND) {
741 size_t z;
742 struct randomizer *new;
744 val = 0xFF & rand();
745 z = ++(cfg->pkts[cfg->len - 1].rlen);
746 cfg->pkts[cfg->len - 1].rnd =
747 xrealloc(cfg->pkts[cfg->len - 1].rnd,
748 1, z * sizeof(struct randomizer));
749 new = &cfg->pkts[cfg->len - 1].rnd[z - 1];
750 new->val = val;
751 new->off = offset;
754 cfg->pkts[cfg->len - 1].plen++;
755 cfg->pkts[cfg->len - 1].payload =
756 xrealloc(cfg->pkts[cfg->len - 1].payload,
757 1, cfg->pkts[cfg->len - 1].plen);
758 cfg->pkts[cfg->len - 1].payload[cfg->pkts[cfg->len - 1].plen - 1] =
759 0xFF & val;
760 offset++;
761 pb = skipchar_s(pb, ',');
763 } else
764 panic("Syntax error!\n");
765 fmemset(buff, 0, sizeof(buff));
768 fclose(fp);
769 if (cnts)
770 xfree(cnts);
771 dump_conf(cfg);
774 static void cleanup_cfg(struct pktconf *cfg)
776 size_t l;
778 for (l = 0; l < cfg->len; ++l) {
779 if (cfg->pkts[l].plen > 0)
780 xfree(cfg->pkts[l].payload);
781 if (cfg->pkts[l].clen > 0)
782 xfree(cfg->pkts[l].cnt);
783 if (cfg->pkts[l].rlen > 0)
784 xfree(cfg->pkts[l].rnd);
787 if (cfg->len > 0)
788 xfree(cfg->pkts);
791 static int main_loop(struct mode *mode, char *confname, unsigned long pkts,
792 unsigned long gap)
794 struct pktconf cfg = {
795 .num = pkts,
796 .gap = gap,
797 .len = 0,
800 parse_conf_or_die(confname, &cfg);
801 if (gap > 0)
802 tx_tgap_or_die(mode, &cfg);
803 else
804 tx_fire_or_die(mode, &cfg);
805 cleanup_cfg(&cfg);
807 return 0;
810 int main(int argc, char **argv)
812 int c, opt_index, ret, i, j, interactive = 0;
813 char *confname = NULL, *ptr;
814 unsigned long pkts = 0, gap = 0;
815 bool prio_high = false;
816 struct mode mode;
818 check_for_root_maybe_die();
820 fmemset(&mode, 0, sizeof(mode));
821 mode.cpu = CPU_UNKNOWN;
823 while ((c = getopt_long(argc, argv, short_options, long_options,
824 &opt_index)) != EOF) {
825 switch (c) {
826 case 'h':
827 help();
828 break;
829 case 'v':
830 version();
831 break;
832 case 'd':
833 mode.device = xstrndup(optarg, IFNAMSIZ);
834 break;
835 case 'x':
836 interactive = 1;
837 break;
838 case 'r':
839 mode.rand = 1;
840 break;
841 case 'J':
842 mode.jumbo_support = 1;
843 break;
844 case 'c':
845 confname = xstrdup(optarg);
846 break;
847 case 'k':
848 mode.kpull = atol(optarg);
849 break;
850 case 'n':
851 pkts = atol(optarg);
852 break;
853 case 't':
854 gap = atol(optarg);
855 break;
856 case 'S':
857 ptr = optarg;
858 mode.reserve_size = 0;
860 for (j = i = strlen(optarg); i > 0; --i) {
861 if (!isdigit(optarg[j - i]))
862 break;
863 ptr++;
866 if (!strncmp(ptr, "KB", strlen("KB")))
867 mode.reserve_size = 1 << 10;
868 else if (!strncmp(ptr, "MB", strlen("MB")))
869 mode.reserve_size = 1 << 20;
870 else if (!strncmp(ptr, "GB", strlen("GB")))
871 mode.reserve_size = 1 << 30;
872 else
873 panic("Syntax error in ring size param!\n");
875 *ptr = 0;
876 mode.reserve_size *= atoi(optarg);
877 break;
878 case 'b':
879 set_cpu_affinity(optarg, 0);
880 /* Take the first CPU for rebinding the IRQ */
881 if (mode.cpu != CPU_NOTOUCH)
882 mode.cpu = atoi(optarg);
883 break;
884 case 'B':
885 set_cpu_affinity(optarg, 1);
886 break;
887 case 'H':
888 prio_high = true;
889 break;
890 case 'Q':
891 mode.cpu = CPU_NOTOUCH;
892 break;
893 case '?':
894 switch (optopt) {
895 case 'd':
896 case 'c':
897 case 'n':
898 case 'S':
899 case 'b':
900 case 'k':
901 case 'B':
902 case 't':
903 panic("Option -%c requires an argument!\n",
904 optopt);
905 default:
906 if (isprint(optopt))
907 whine("Unknown option character "
908 "`0x%X\'!\n", optopt);
909 die();
911 default:
912 break;
916 if (!interactive && argc < 5)
917 help();
918 if (interactive && argc < 2)
919 help();
920 if (!interactive && mode.device == NULL)
921 panic("No networking device given!\n");
922 if (!interactive && confname == NULL)
923 panic("No configuration file given!\n");
924 if (!interactive && device_mtu(mode.device) == 0)
925 panic("This is no networking device!\n");
926 if (!interactive && device_up_and_running(mode.device) == 0)
927 panic("Networking device not running!\n");
929 register_signal(SIGINT, signal_handler);
930 register_signal(SIGHUP, signal_handler);
931 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
933 header();
935 if (prio_high == true) {
936 set_proc_prio(get_default_proc_prio());
937 set_sched_status(get_default_sched_policy(),
938 get_default_sched_prio());
941 if (interactive)
942 ret = main_loop_interactive(&mode, confname);
943 else
944 ret = main_loop(&mode, confname, pkts, gap);
946 if (mode.device)
947 xfree(mode.device);
948 if (confname)
949 xfree(confname);
950 return ret;