trafgen: Fix output pcap file name length trimming
[netsniff-ng.git] / trafgen.c
blobb40d362f135a3b3d9e29e18a0c1500136d0a407a
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
4 * Swiss federal institute of technology (ETH Zurich)
5 * Subject to the GPL, version 2.
6 */
8 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <string.h>
12 #include <getopt.h>
13 #include <ctype.h>
14 #include <stdbool.h>
15 #include <sched.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/fsuid.h>
19 #include <sys/prctl.h>
20 #include <sys/stat.h>
21 #include <sys/wait.h>
22 #include <sys/mman.h>
23 #include <net/ethernet.h>
24 #include <netinet/in.h>
25 #include <netinet/ip.h>
26 #include <linux/icmp.h>
27 #include <linux/if.h>
28 #include <arpa/inet.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <time.h>
34 #include <poll.h>
35 #include <netdb.h>
36 #include <math.h>
37 #include <unistd.h>
39 #include "xmalloc.h"
40 #include "die.h"
41 #include "str.h"
42 #include "sig.h"
43 #include "sock.h"
44 #include "cpus.h"
45 #include "lockme.h"
46 #include "privs.h"
47 #include "proc.h"
48 #include "mac80211.h"
49 #include "ioops.h"
50 #include "irq.h"
51 #include "config.h"
52 #include "built_in.h"
53 #include "trafgen_conf.h"
54 #include "tprintf.h"
55 #include "timer.h"
56 #include "ring_tx.h"
57 #include "csum.h"
58 #include "trafgen_proto.h"
59 #include "pcap_io.h"
60 #include "trafgen_dev.h"
62 enum shaper_type {
63 SHAPER_NONE,
64 SHAPER_DELAY,
65 SHAPER_PKTS,
66 SHAPER_BYTES,
67 SHAPER_TSTAMP,
70 struct shaper {
71 enum shaper_type type;
72 unsigned long long sent;
73 unsigned long long rate;
74 struct timeval tstamp;
75 struct timespec delay;
76 struct timeval start;
77 struct timeval end;
80 struct ctx {
81 bool rand, rfraw, jumbo_support, verbose, smoke_test, enforce, qdisc_path;
82 size_t reserve_size;
83 struct dev_io *dev_out;
84 struct dev_io *dev_in;
85 unsigned long num;
86 unsigned int cpus;
87 uid_t uid; gid_t gid;
88 char *device, *device_trans, *rhost;
89 struct sockaddr_in dest;
90 struct shaper sh;
91 char *packet_str;
92 char *pcap_in;
95 struct cpu_stats {
96 unsigned long tv_sec, tv_usec;
97 unsigned long long tx_packets, tx_bytes;
98 unsigned long long cf_packets, cf_bytes;
99 unsigned long long cd_packets;
100 sig_atomic_t state;
103 static sig_atomic_t sigint = 0;
105 struct packet *packets = NULL;
106 size_t plen = 0;
108 struct packet_dyn *packet_dyn = NULL;
109 size_t dlen = 0;
111 static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQqD:b:";
112 static const struct option long_options[] = {
113 {"dev", required_argument, NULL, 'd'},
114 {"out", required_argument, NULL, 'o'},
115 {"in", required_argument, NULL, 'i'},
116 {"conf", required_argument, NULL, 'c'},
117 {"num", required_argument, NULL, 'n'},
118 {"gap", required_argument, NULL, 't'},
119 {"rate", required_argument, NULL, 'b'},
120 {"cpus", required_argument, NULL, 'P'},
121 {"ring-size", required_argument, NULL, 'S'},
122 {"kernel-pull", required_argument, NULL, 'k'},
123 {"smoke-test", required_argument, NULL, 's'},
124 {"seed", required_argument, NULL, 'E'},
125 {"user", required_argument, NULL, 'u'},
126 {"group", required_argument, NULL, 'g'},
127 {"prio-high", no_argument, NULL, 'H'},
128 {"notouch-irq", no_argument, NULL, 'Q'},
129 {"no-sock-mem", no_argument, NULL, 'A'},
130 {"qdisc-path", no_argument, NULL, 'q'},
131 {"jumbo-support", no_argument, NULL, 'J'},
132 {"no-cpu-stats", no_argument, NULL, 'C'},
133 {"cpp", no_argument, NULL, 'p'},
134 {"define", required_argument, NULL, 'D'},
135 {"rfraw", no_argument, NULL, 'R'},
136 {"rand", no_argument, NULL, 'r'},
137 {"verbose", no_argument, NULL, 'V'},
138 {"version", no_argument, NULL, 'v'},
139 {"example", no_argument, NULL, 'e'},
140 {"help", no_argument, NULL, 'h'},
141 {NULL, 0, NULL, 0}
144 static const char *copyright = "Please report bugs to <netsniff-ng@googlegroups.com>\n"
145 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
146 "Swiss federal institute of technology (ETH Zurich)\n"
147 "License: GNU GPL version 2.0\n"
148 "This is free software: you are free to change and redistribute it.\n"
149 "There is NO WARRANTY, to the extent permitted by law.";
151 static struct cpu_stats *stats;
152 static unsigned int seed;
154 #define CPU_STATS_STATE_CFG 1
155 #define CPU_STATS_STATE_CHK 2
156 #define CPU_STATS_STATE_RES 4
158 #ifndef ICMP_FILTER
159 # define ICMP_FILTER 1
161 struct icmp_filter {
162 __u32 data;
164 #endif
166 #define SMOKE_N_PROBES 100
168 #define PKT_MIN_LEN 14
170 static void signal_handler(int number)
172 switch (number) {
173 case SIGINT:
174 case SIGQUIT:
175 case SIGTERM:
176 sigint = 1;
177 case SIGHUP:
178 default:
179 break;
183 static void __noreturn help(void)
185 printf("trafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
186 puts("http://www.netsniff-ng.org\n\n"
187 "Usage: trafgen [options] [packet]\n"
188 "Options:\n"
189 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
190 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
191 " -p|--cpp Run packet config through C preprocessor\n"
192 " -D|--define Add macro/define for C preprocessor\n"
193 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
194 " -R|--rfraw Inject raw 802.11 frames\n"
195 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
196 " -n|--num <uint> Number of packets until exit (def: 0)\n"
197 " -r|--rand Randomize packet selection (def: round robin)\n"
198 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
199 " -t|--gap <time> Set approx. interpacket gap (s/ms/us/ns, def: us)\n"
200 " -b|--rate <rate> Send traffic at specified rate (pps/B/kB/MB/GB/kbit/Mbit/Gbit/KiB/MiB/GiB)\n"
201 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
202 " -E|--seed <uint> Manually set srand(3) seed\n"
203 " -u|--user <userid> Drop privileges and change to userid\n"
204 " -g|--group <groupid> Drop privileges and change to groupid\n"
205 " -H|--prio-high Make this high priority process\n"
206 " -A|--no-sock-mem Don't tune core socket memory\n"
207 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
208 " -q|--qdisc-path Enable qdisc kernel path (default off since 3.14)\n"
209 " -V|--verbose Be more verbose\n"
210 " -C|--no-cpu-stats Do not print CPU time statistics on exit\n"
211 " -v|--version Show version and exit\n"
212 " -e|--example Show built-in packet config example\n"
213 " -h|--help Guess what?!\n\n"
214 "Examples:\n"
215 " trafgen --dev eth0 --conf trafgen.cfg\n"
216 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
217 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
218 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
219 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
220 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
221 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n"
222 " trafgen --dev eth0 '{ fill(0xff, 6), 0x00, 0x02, 0xb3, rnd(3), c16(0x0800), fill(0xca, 64) }'\n\n"
223 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
224 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
225 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
226 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
227 "Generate config files from existing pcap using netsniff-ng:\n"
228 " netsniff-ng --in dump.pcap --out dump.cfg\n\n"
229 "Note:\n"
230 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
231 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
232 " we assume the kernel crashed, thus we print the packet and quit.\n"
233 " In case you find a ping-of-death, please mention trafgen in your\n"
234 " commit message of the fix!\n\n"
235 " For introducing bit errors, delays with random variation and more,\n"
236 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
237 " For generating different package distributions, you can use scripting\n"
238 " to generate a trafgen config file with packet ratios as:\n\n"
239 " IMIX 64:7, 570:4, 1518:1\n"
240 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
241 " Cisco 64:7, 594:4, 1518:1\n"
242 " RPR Trimodal 64:60, 512:20, 1518:20\n"
243 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n");
244 puts(copyright);
245 die();
248 static void __noreturn example(void)
250 const char *e =
251 "/* Note: dynamic elements make trafgen slower! */\n"
252 "#include <stddef.h>\n\n"
253 "{\n"
254 " /* MAC Destination */\n"
255 " fill(0xff, ETH_ALEN),\n"
256 " /* MAC Source */\n"
257 " 0x00, 0x02, 0xb3, drnd(3),\n"
258 " /* IPv4 Protocol */\n"
259 " c16(ETH_P_IP),\n"
260 " /* IPv4 Version, IHL, TOS */\n"
261 " 0b01000101, 0,\n"
262 " /* IPv4 Total Len */\n"
263 " c16(59),\n"
264 " /* IPv4 Ident */\n"
265 " drnd(2),\n"
266 " /* IPv4 Flags, Frag Off */\n"
267 " 0b01000000, 0,\n"
268 " /* IPv4 TTL */\n"
269 " 64,\n"
270 " /* Proto TCP */\n"
271 " 0x06,\n"
272 " /* IPv4 Checksum (IP header from, to) */\n"
273 " csumip(14, 33),\n"
274 " /* Source IP */\n"
275 " drnd(4),\n"
276 " /* Dest IP */\n"
277 " drnd(4),\n"
278 " /* TCP Source Port */\n"
279 " drnd(2),\n"
280 " /* TCP Dest Port */\n"
281 " c16(80),\n"
282 " /* TCP Sequence Number */\n"
283 " drnd(4),\n"
284 " /* TCP Ackn. Number */\n"
285 " c32(0),\n"
286 " /* TCP Header length + TCP SYN/ECN Flag */\n"
287 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
288 " /* Window Size */\n"
289 " c16(16),\n"
290 " /* TCP Checksum (offset IP, offset TCP) */\n"
291 " csumtcp(14, 34),\n"
292 " /* TCP Options */\n"
293 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
294 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
295 " /* Data blob */\n"
296 " \"gotcha!\",\n"
297 "}";
298 puts(e);
299 die();
302 static void __noreturn version(void)
304 printf("trafgen %s, Git id: %s\n", VERSION_LONG, GITVERSION);
305 puts("multithreaded zero-copy network packet generator\n"
306 "http://www.netsniff-ng.org\n");
307 puts(copyright);
308 die();
311 static void apply_counter(int id)
313 size_t j, counter_max = packet_dyn[id].clen;
315 for (j = 0; j < counter_max; ++j) {
316 uint8_t val;
317 struct counter *counter = &packet_dyn[id].cnt[j];
319 val = counter->val - counter->min;
321 switch (counter->type) {
322 case TYPE_INC:
323 val = (val + counter->inc) % (counter->max - counter->min + 1);
324 break;
325 case TYPE_DEC:
326 val = (val - counter->inc) % (counter->min - counter->max + 1);
327 break;
328 default:
329 bug();
332 counter->val = val + counter->min;
333 packets[id].payload[counter->off] = val;
337 static void apply_randomizer(int id)
339 size_t j, rand_max = packet_dyn[id].rlen;
341 for (j = 0; j < rand_max; ++j) {
342 uint8_t val = (uint8_t) rand();
343 struct randomizer *randomizer = &packet_dyn[id].rnd[j];
345 packets[id].payload[randomizer->off] = val;
349 static void apply_csum16(int id)
351 size_t j, csum_max = packet_dyn[id].slen;
353 for (j = 0; j < csum_max; ++j) {
354 uint16_t sum = 0;
355 struct csum16 *csum = &packet_dyn[id].csum[j];
357 fmemset(&packets[id].payload[csum->off], 0, sizeof(sum));
358 if (unlikely((size_t) csum->to >= packets[id].len))
359 csum->to = packets[id].len - 1;
361 switch (csum->which) {
362 case CSUM_IP:
363 sum = calc_csum(packets[id].payload + csum->from,
364 csum->to - csum->from + 1);
365 break;
366 case CSUM_UDP:
367 sum = p4_csum((void *) packets[id].payload + csum->from,
368 packets[id].payload + csum->to,
369 (packets[id].len - csum->to),
370 IPPROTO_UDP);
371 break;
372 case CSUM_TCP:
373 sum = p4_csum((void *) packets[id].payload + csum->from,
374 packets[id].payload + csum->to,
375 (packets[id].len - csum->to),
376 IPPROTO_TCP);
377 break;
378 case CSUM_UDP6:
379 sum = p6_csum((void *) packets[id].payload + csum->from,
380 packets[id].payload + csum->to,
381 (packets[id].len - csum->to),
382 IPPROTO_UDP);
383 break;
384 case CSUM_TCP6:
385 sum = p6_csum((void *) packets[id].payload + csum->from,
386 packets[id].payload + csum->to,
387 (packets[id].len - csum->to),
388 IPPROTO_TCP);
389 break;
390 default:
391 bug();
392 break;
395 fmemcpy(&packets[id].payload[csum->off], &sum, sizeof(sum));
399 static void preprocess_packets(void)
401 size_t i;
403 for (i = 0; i < plen; i++) {
404 struct packet_dyn *pktd = &packet_dyn[i];
406 if (packet_dyn_has_only_csums(pktd)) {
407 apply_csum16(i);
408 pktd->slen = 0;
409 xfree(pktd->csum);
414 static struct cpu_stats *setup_shared_var(unsigned int cpus)
416 int fd;
417 size_t len = cpus * sizeof(struct cpu_stats);
418 char *zbuff, file[256];
419 struct cpu_stats *buff;
421 slprintf(file, sizeof(file), ".tmp_mmap.XXXXXX");
422 fd = mkostemp_or_die(file, O_RDWR | O_CREAT | O_TRUNC);
423 zbuff = xzmalloc(len);
424 write_or_die(fd, zbuff, len);
425 xfree(zbuff);
427 buff = mmap(NULL, len, PROT_READ | PROT_WRITE,
428 MAP_SHARED, fd, 0);
429 if (buff == MAP_FAILED)
430 panic("Cannot setup shared variable!\n");
432 close(fd);
433 unlink(file);
435 memset(buff, 0, len);
436 return buff;
439 static void destroy_shared_var(void *buff, unsigned int cpus)
441 munmap(buff, cpus * sizeof(struct cpu_stats));
444 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
446 size_t i;
448 printf("{");
449 for (i = 0; i < len; ++i) {
450 if (i % 15 == 0)
451 printf("\n ");
452 printf("0x%02x, ", payload[i]);
454 printf("\n}\n");
455 fflush(stdout);
458 static int xmit_smoke_setup(struct ctx *ctx)
460 int icmp_sock, ret, ttl = 64;
461 struct icmp_filter filter;
463 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
464 if (icmp_sock < 0)
465 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
467 filter.data = ~(1 << ICMP_ECHOREPLY);
469 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
470 if (ret < 0)
471 panic("Cannot install filter!\n");
473 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
474 if (ret < 0)
475 panic("Cannot set TTL!\n");
477 memset(&ctx->dest, 0, sizeof(ctx->dest));
478 ctx->dest.sin_family = AF_INET;
479 ctx->dest.sin_port = 0;
481 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
482 if (ret < 0)
483 panic("Cannot resolve address!\n");
485 return icmp_sock;
488 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
490 int ret;
491 unsigned int i, j;
492 short ident, cnt = 1, idstore[SMOKE_N_PROBES];
493 uint8_t outpack[512], *data;
494 struct icmphdr *icmp;
495 struct iphdr *ip;
496 size_t len = sizeof(*icmp) + 56;
497 struct sockaddr_in from;
498 socklen_t from_len;
499 struct pollfd fds = {
500 .fd = icmp_sock,
501 .events = POLLIN,
504 fmemset(idstore, 0, sizeof(idstore));
505 for (j = 0; j < SMOKE_N_PROBES; j++) {
506 while ((ident = htons((short) rand())) == 0)
507 sleep(0);
508 idstore[j] = ident;
510 memset(outpack, 0, sizeof(outpack));
511 icmp = (void *) outpack;
512 icmp->type = ICMP_ECHO;
513 icmp->un.echo.id = ident;
514 icmp->un.echo.sequence = htons(cnt++);
516 data = ((uint8_t *) outpack + sizeof(*icmp));
517 for (i = 0; i < 56; ++i)
518 data[i] = (uint8_t) rand();
520 icmp->checksum = csum((unsigned short *) outpack,
521 len / sizeof(unsigned short));
523 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
524 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
525 if (unlikely(ret != (int) len))
526 panic("Cannot send out probe: %s!\n", strerror(errno));
528 ret = poll(&fds, 1, 50);
529 if (ret < 0)
530 panic("Poll failed!\n");
532 if (fds.revents & POLLIN) {
533 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
534 (struct sockaddr *) &from, &from_len);
535 if (unlikely(ret <= 0))
536 panic("Probe receive failed!\n");
537 if (unlikely(from_len != sizeof(ctx->dest)))
538 continue;
539 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
540 continue;
541 if (unlikely((size_t) ret < sizeof(*ip) + sizeof(*icmp)))
542 continue;
543 ip = (void *) outpack;
544 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > (size_t) ret))
545 continue;
546 icmp = (void *) outpack + ip->ihl * 4;
547 for (i = 0; i < array_size(idstore); ++i) {
548 if (unlikely(icmp->un.echo.id != idstore[i]))
549 continue;
550 return 0;
555 return -1;
558 static bool shaper_is_set(struct shaper *sh)
560 return sh->type != SHAPER_NONE;
563 static void shaper_init(struct shaper *sh)
565 if (sh->type == SHAPER_NONE || sh->type == SHAPER_DELAY)
566 return;
568 memset(&sh->delay, 0, sizeof(struct timespec));
569 bug_on(gettimeofday(&sh->start, NULL));
570 sh->sent = 0;
573 static void shaper_set_delay(struct shaper *sh, time_t sec, long int ns)
575 if (!(sec | ns)) {
576 sh->type = SHAPER_NONE;
577 return;
580 sh->type = SHAPER_DELAY;
581 sh->delay.tv_sec = sec;
582 sh->delay.tv_nsec = ns;
585 static void shaper_set_rate(struct shaper *sh, unsigned long long rate,
586 enum shaper_type type)
588 memset(sh, 0, sizeof(struct shaper));
589 sh->rate = rate;
590 sh->type = type;
593 static void shaper_set_tstamp(struct shaper *sh, struct timespec *ts)
595 TIMESPEC_TO_TIMEVAL(&sh->tstamp, ts);
598 static void shaper_delay(struct shaper *sh, struct packet *pkt)
600 if (sh->type == SHAPER_BYTES || sh->type == SHAPER_PKTS) {
601 unsigned long pkt_len = pkt->len;
603 sh->sent += sh->type == SHAPER_BYTES ? pkt_len : 1;
605 if (sh->sent >= sh->rate && sh->rate > 0) {
606 struct timeval delay_us;
607 struct timeval time_sent;
608 struct timeval time_1s = { .tv_sec = 1 };
610 bug_on(gettimeofday(&sh->end, NULL));
611 timersub(&sh->end, &sh->start, &time_sent);
613 if (timercmp(&time_1s, &time_sent, > )) {
614 timersub(&time_1s, &time_sent, &delay_us);
615 TIMEVAL_TO_TIMESPEC(&delay_us, &sh->delay);
618 } else if (sh->type == SHAPER_TSTAMP) {
619 struct timeval tstamp;
620 struct timeval pkt_diff;
621 struct timeval diff;
623 bug_on(gettimeofday(&sh->end, NULL));
624 TIMESPEC_TO_TIMEVAL(&tstamp, &pkt->tstamp);
625 timersub(&sh->end, &sh->start, &diff);
626 timersub(&tstamp, &sh->tstamp, &pkt_diff);
628 if (timercmp(&diff, &pkt_diff, <)) {
629 struct timeval delay;
631 timersub(&pkt_diff, &diff, &delay);
632 TIMEVAL_TO_TIMESPEC(&delay, &sh->delay);
635 memcpy(&sh->tstamp, &tstamp, sizeof(sh->tstamp));
638 if ((sh->delay.tv_sec | sh->delay.tv_nsec) > 0) {
639 nanosleep(&sh->delay, NULL);
641 shaper_init(sh);
645 static inline void packet_apply_dyn_elements(int idx)
647 if (packet_dyn_has_elems(&packet_dyn[idx])) {
648 apply_counter(idx);
649 apply_randomizer(idx);
650 apply_csum16(idx);
653 if (packet_dyn_has_fields(&packet_dyn[idx])) {
654 uint32_t i;
656 for (i = 0; i < packet_dyn[idx].flen; i++)
657 proto_field_dyn_apply(packet_dyn[idx].fields[i]);
659 proto_packet_update(idx);
663 static void xmit_slowpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned long orig_num)
665 int ret, icmp_sock = -1;
666 unsigned long num = 1, i = 0;
667 struct timeval start, end, diff;
668 unsigned long long tx_bytes = 0, tx_packets = 0;
670 if (ctx->num > 0)
671 num = ctx->num;
672 if (ctx->num == 0 && orig_num > 0)
673 num = 0;
675 if (ctx->smoke_test)
676 icmp_sock = xmit_smoke_setup(ctx);
678 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
680 bug_on(gettimeofday(&start, NULL));
682 if (shaper_is_set(&ctx->sh))
683 shaper_init(&ctx->sh);
685 while (likely(sigint == 0 && num > 0 && plen > 0)) {
686 packet_apply_dyn_elements(i);
687 retry:
688 ret = dev_io_write(ctx->dev_out, packets[i].payload, packets[i].len);
689 if (unlikely(ret < 0)) {
690 if (errno == ENOBUFS) {
691 sched_yield();
692 goto retry;
694 if (ctx->smoke_test)
695 panic("Sendto error: %s!\n", strerror(errno));
698 tx_bytes += packets[i].len;
699 tx_packets++;
701 if (ctx->smoke_test) {
702 ret = xmit_smoke_probe(icmp_sock, ctx);
703 if (unlikely(ret < 0)) {
704 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
705 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
706 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
707 i, seed);
709 dump_trafgen_snippet(packets[i].payload, packets[i].len);
710 break;
714 if (!ctx->rand) {
715 i++;
716 if (i >= plen)
717 i = 0;
718 } else
719 i = rand() % plen;
721 if (ctx->num > 0)
722 num--;
724 if (shaper_is_set(&ctx->sh))
725 shaper_delay(&ctx->sh, &packets[i]);
728 bug_on(gettimeofday(&end, NULL));
729 timersub(&end, &start, &diff);
731 if (ctx->smoke_test)
732 close(icmp_sock);
734 stats[cpu].tx_packets = tx_packets;
735 stats[cpu].tx_bytes = tx_bytes;
736 stats[cpu].tv_sec = diff.tv_sec;
737 stats[cpu].tv_usec = diff.tv_usec;
739 stats[cpu].state |= CPU_STATS_STATE_RES;
742 static void xmit_fastpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned long orig_num)
744 int ifindex = dev_io_ifindex_get(ctx->dev_out);
745 uint8_t *out = NULL;
746 unsigned int it = 0;
747 unsigned long num = 1, i = 0;
748 size_t size = ring_size(dev_io_name_get(ctx->dev_out), ctx->reserve_size);
749 struct ring tx_ring;
750 struct frame_map *hdr;
751 struct timeval start, end, diff;
752 unsigned long long tx_bytes = 0, tx_packets = 0;
753 int sock = dev_io_fd_get(ctx->dev_out);
755 set_sock_prio(sock, 512);
757 ring_tx_setup(&tx_ring, sock, size, ifindex, ctx->jumbo_support, ctx->verbose);
759 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
761 if (ctx->num > 0)
762 num = ctx->num;
763 if (ctx->num == 0 && orig_num > 0)
764 num = 0;
766 bug_on(gettimeofday(&start, NULL));
768 while (likely(sigint == 0 && num > 0 && plen > 0)) {
769 if (!user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
770 int ret = pull_and_flush_tx_ring(sock);
771 if (unlikely(ret < 0)) {
772 /* We could hit EBADF if the socket has been closed before
773 * the timer was triggered.
775 if (errno != EBADF && errno != ENOBUFS)
776 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
779 continue;
782 hdr = tx_ring.frames[it].iov_base;
783 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
785 hdr->tp_h.tp_snaplen = packets[i].len;
786 hdr->tp_h.tp_len = packets[i].len;
788 packet_apply_dyn_elements(i);
790 fmemcpy(out, packets[i].payload, packets[i].len);
792 tx_bytes += packets[i].len;
793 tx_packets++;
795 if (!ctx->rand) {
796 i++;
797 if (i >= plen)
798 i = 0;
799 } else
800 i = rand() % plen;
802 kernel_may_pull_from_tx(&hdr->tp_h);
804 it++;
805 if (it >= tx_ring.layout.tp_frame_nr)
806 it = 0;
808 if (ctx->num > 0)
809 num--;
812 bug_on(gettimeofday(&end, NULL));
813 timersub(&end, &start, &diff);
815 pull_and_flush_tx_ring_wait(sock);
816 destroy_tx_ring(sock, &tx_ring);
818 stats[cpu].tx_packets = tx_packets;
819 stats[cpu].tx_bytes = tx_bytes;
820 stats[cpu].tv_sec = diff.tv_sec;
821 stats[cpu].tv_usec = diff.tv_usec;
823 stats[cpu].state |= CPU_STATS_STATE_RES;
826 static inline void __set_state(unsigned int cpu, sig_atomic_t s)
828 stats[cpu].state = s;
831 static inline sig_atomic_t __get_state(unsigned int cpu)
833 return stats[cpu].state;
836 static unsigned long __wait_and_sum_others(struct ctx *ctx, unsigned int cpu)
838 unsigned int i;
839 unsigned long total;
841 for (i = 0, total = plen; i < ctx->cpus; i++) {
842 if (i == cpu)
843 continue;
845 while ((__get_state(i) &
846 (CPU_STATS_STATE_CFG |
847 CPU_STATS_STATE_RES)) == 0 &&
848 sigint == 0)
849 sched_yield();
851 total += stats[i].cf_packets;
854 return total;
857 static void __correct_global_delta(struct ctx *ctx, unsigned int cpu, unsigned long orig)
859 unsigned int i;
860 unsigned long total;
861 int cpu_sel;
862 long long delta_correction = 0;
864 for (i = 0, total = ctx->num; i < ctx->cpus; i++) {
865 if (i == cpu)
866 continue;
868 while ((__get_state(i) &
869 (CPU_STATS_STATE_CHK |
870 CPU_STATS_STATE_RES)) == 0 &&
871 sigint == 0)
872 sched_yield();
874 total += stats[i].cd_packets;
877 if (total > orig)
878 delta_correction = -1 * ((long long) total - orig);
879 if (total < orig)
880 delta_correction = +1 * ((long long) orig - total);
882 for (cpu_sel = -1, i = 0; i < ctx->cpus; i++) {
883 if (stats[i].cd_packets > 0) {
884 if ((long long) stats[i].cd_packets +
885 delta_correction >= 0) {
886 cpu_sel = i;
887 break;
892 if ((int) cpu == cpu_sel)
893 ctx->num += delta_correction;
896 static void __set_state_cf(unsigned int cpu, unsigned long p, unsigned long b,
897 sig_atomic_t s)
899 stats[cpu].cf_packets = p;
900 stats[cpu].cf_bytes = b;
901 stats[cpu].state = s;
904 static void __set_state_cd(unsigned int cpu, unsigned long p, sig_atomic_t s)
906 stats[cpu].cd_packets = p;
907 stats[cpu].state = s;
910 static void xmit_packet_precheck(struct ctx *ctx, unsigned int cpu)
912 unsigned long plen_total, orig = ctx->num;
913 size_t total_len = 0;
914 unsigned int i;
916 bug_on(plen != dlen);
918 for (i = 0; i < plen; ++i)
919 total_len += packets[i].len;
921 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
922 plen_total = __wait_and_sum_others(ctx, cpu);
924 if (orig > 0) {
925 ctx->num = (unsigned long) round((1.0 * plen / plen_total) * orig);
927 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
928 CPU_STATS_STATE_CFG);
929 __correct_global_delta(ctx, cpu, orig);
932 if (plen == 0) {
933 __set_state(cpu, CPU_STATS_STATE_RES);
934 return;
938 static void pcap_load_packets(struct dev_io *dev)
940 struct timespec tstamp;
941 size_t buf_len;
942 uint8_t *buf;
943 int pkt_len;
945 buf_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
946 buf = xmalloc_aligned(buf_len, CO_CACHE_LINE_SIZE);
948 while ((pkt_len = dev_io_read(dev, buf, buf_len, &tstamp)) > 0) {
949 struct packet *pkt;
951 realloc_packet();
953 pkt = current_packet();
954 pkt->len = pkt_len;
955 pkt->payload = xzmalloc(pkt_len);
956 memcpy(pkt->payload, buf, pkt_len);
957 memcpy(&pkt->tstamp, &tstamp, sizeof(tstamp));
960 free(buf);
963 static void main_loop(struct ctx *ctx, char *confname, bool slow,
964 unsigned int cpu, bool invoke_cpp, char **cpp_argv,
965 unsigned long orig_num)
967 if (ctx->dev_in && dev_io_is_pcap(ctx->dev_in)) {
968 pcap_load_packets(ctx->dev_in);
969 shaper_set_tstamp(&ctx->sh, &packets[0].tstamp);
970 ctx->num = plen;
971 } else {
972 if (ctx->packet_str)
973 compile_packets_str(ctx->packet_str, ctx->verbose, cpu);
974 else
975 compile_packets(confname, ctx->verbose, cpu, invoke_cpp, cpp_argv);
977 preprocess_packets();
980 xmit_packet_precheck(ctx, cpu);
982 if (cpu == 0) {
983 unsigned int i;
984 size_t total_len = 0, total_pkts = 0;
986 for (i = 0; i < ctx->cpus; ++i) {
987 total_len += stats[i].cf_bytes;
988 total_pkts += stats[i].cf_packets;
991 printf("%6zu packets to schedule\n", total_pkts);
992 printf("%6zu bytes in total\n", total_len);
993 printf("Running! Hang up with ^C!\n\n");
994 fflush(stdout);
997 if (dev_io_is_netdev(ctx->dev_out) && ctx->qdisc_path == false)
998 set_sock_qdisc_bypass(dev_io_fd_get(ctx->dev_out), ctx->verbose);
1000 if (slow)
1001 xmit_slowpath_or_die(ctx, cpu, orig_num);
1002 else
1003 xmit_fastpath_or_die(ctx, cpu, orig_num);
1005 cleanup_packets();
1008 static unsigned int generate_srand_seed(void)
1010 int fd;
1011 unsigned int _seed;
1013 fd = open("/dev/urandom", O_RDONLY);
1014 if (fd < 0)
1015 return time(NULL);
1017 read_or_die(fd, &_seed, sizeof(_seed));
1019 close(fd);
1020 return _seed;
1023 static void on_panic_del_rfmon(void *arg)
1025 leave_rfmon_mac80211(arg);
1028 int main(int argc, char **argv)
1030 bool slow = false, invoke_cpp = false, reseed = true, cpustats = true;
1031 bool prio_high = false, set_irq_aff = true, set_sock_mem = true;
1032 int c, vals[4] = {0}, irq;
1033 uint64_t gap = 0;
1034 unsigned int i;
1035 char *confname = NULL, *ptr;
1036 unsigned long cpus_tmp, orig_num = 0;
1037 unsigned long long tx_packets, tx_bytes;
1038 struct ctx ctx;
1039 int min_opts = 5;
1040 char **cpp_argv = NULL;
1041 size_t cpp_argc = 0;
1042 unsigned long long rate;
1043 enum shaper_type shape_type;
1044 struct timespec delay;
1046 fmemset(&ctx, 0, sizeof(ctx));
1047 ctx.cpus = get_number_cpus_online();
1048 ctx.uid = getuid();
1049 ctx.gid = getgid();
1050 ctx.qdisc_path = false;
1052 /* Keep an initial small default size to reduce cache-misses. */
1053 ctx.reserve_size = 512 * (1 << 10);
1055 while ((c = getopt_long(argc, argv, short_options, long_options,
1056 NULL)) != EOF) {
1057 switch (c) {
1058 case 'h':
1059 help();
1060 break;
1061 case 'v':
1062 version();
1063 break;
1064 case 'C':
1065 cpustats = false;
1066 break;
1067 case 'e':
1068 example();
1069 break;
1070 case 'p':
1071 invoke_cpp = true;
1072 break;
1073 case 'D':
1074 cpp_argv = argv_insert(cpp_argv, &cpp_argc, "-D");
1075 cpp_argv = argv_insert(cpp_argv, &cpp_argc, optarg);
1076 break;
1077 case 'V':
1078 ctx.verbose = true;
1079 break;
1080 case 'P':
1081 cpus_tmp = strtoul(optarg, NULL, 0);
1082 if (cpus_tmp > 0 && cpus_tmp < ctx.cpus)
1083 ctx.cpus = cpus_tmp;
1084 break;
1085 case 'd':
1086 case 'o':
1087 ctx.device = xstrdup(optarg);
1088 break;
1089 case 'H':
1090 prio_high = true;
1091 break;
1092 case 'A':
1093 set_sock_mem = false;
1094 break;
1095 case 'Q':
1096 set_irq_aff = false;
1097 break;
1098 case 'q':
1099 ctx.qdisc_path = true;
1100 break;
1101 case 'r':
1102 ctx.rand = true;
1103 break;
1104 case 's':
1105 slow = true;
1106 ctx.cpus = 1;
1107 ctx.smoke_test = true;
1108 ctx.rhost = xstrdup(optarg);
1109 break;
1110 case 'R':
1111 ctx.rfraw = true;
1112 break;
1113 case 'J':
1114 ctx.jumbo_support = true;
1115 break;
1116 case 'i':
1117 confname = xstrdup(optarg);
1118 if (strstr(confname, ".pcap")) {
1119 ctx.sh.type = SHAPER_TSTAMP;
1120 ctx.pcap_in = confname;
1121 break;
1123 case 'c':
1124 if (!strncmp("-", confname, strlen("-")))
1125 ctx.cpus = 1;
1126 break;
1127 case 'u':
1128 ctx.uid = strtoul(optarg, NULL, 0);
1129 ctx.enforce = true;
1130 break;
1131 case 'g':
1132 ctx.gid = strtoul(optarg, NULL, 0);
1133 ctx.enforce = true;
1134 break;
1135 case 'k':
1136 printf("Option -k/--kernel-pull is no longer used and "
1137 "will be removed in a future release!\n");
1138 break;
1139 case 'E':
1140 seed = strtoul(optarg, NULL, 0);
1141 reseed = false;
1142 break;
1143 case 'n':
1144 orig_num = strtoul(optarg, NULL, 0);
1145 ctx.num = orig_num;
1146 break;
1147 case 't':
1148 gap = strtoul(optarg, &ptr, 0);
1149 if (!gap && optarg == ptr)
1150 panic("Invalid gap param\n");
1152 if (!strncmp(ptr, "ns", strlen("ns"))) {
1153 delay.tv_sec = gap / 1000000000;
1154 delay.tv_nsec = gap % 1000000000;
1155 } else if (*ptr == '\0' || !strncmp(ptr, "us", strlen("us"))) {
1156 /* Default to microseconds for backwards
1157 * compatibility if no postfix is given.
1159 delay.tv_sec = gap / 1000000;
1160 delay.tv_nsec = (gap % 1000000) * 1000;
1161 } else if (!strncmp(ptr, "ms", strlen("ms"))) {
1162 delay.tv_sec = gap / 1000;
1163 delay.tv_nsec = (gap % 1000) * 1000000;
1164 } else if (!strncmp(ptr, "s", strlen("s"))) {
1165 delay.tv_sec = gap;
1166 delay.tv_nsec = 0;
1167 } else {
1168 panic("Syntax error in time param!\n");
1171 shaper_set_delay(&ctx.sh, delay.tv_sec, delay.tv_nsec);
1172 break;
1173 case 'b':
1174 rate = strtoul(optarg, &ptr, 0);
1175 if (!rate && optarg == ptr)
1176 panic("Invalid rate param\n");
1178 if (strncmp(ptr, "pps", strlen("pps")) == 0) {
1179 shape_type = SHAPER_PKTS;
1180 } else if (strncmp(ptr, "B", strlen("B")) == 0) {
1181 shape_type = SHAPER_BYTES;
1182 } else if (strncmp(ptr, "kB", strlen("kB")) == 0) {
1183 shape_type = SHAPER_BYTES;
1184 rate *= 1000;
1185 } else if (strncmp(ptr, "MB", strlen("MB")) == 0) {
1186 shape_type = SHAPER_BYTES;
1187 rate *= 1000 * 1000;
1188 } else if (strncmp(ptr, "GB", strlen("GB")) == 0) {
1189 shape_type = SHAPER_BYTES;
1190 rate *= 1000 * 1000 * 1000;
1191 } else if (strncmp(ptr, "kbit", strlen("kbit")) == 0) {
1192 shape_type = SHAPER_BYTES;
1193 rate *= 1000 / 8;
1194 } else if (strncmp(ptr, "Mbit", strlen("Mbit")) == 0) {
1195 shape_type = SHAPER_BYTES;
1196 rate *= 1000 * 1000 / 8;
1197 } else if (strncmp(ptr, "Gbit", strlen("Gbit")) == 0) {
1198 shape_type = SHAPER_BYTES;
1199 rate *= 1000 * 1000 * 1000 / 8;
1200 } else if (strncmp(ptr, "KiB", strlen("KiB")) == 0) {
1201 shape_type = SHAPER_BYTES;
1202 rate *= 1 << 10;
1203 } else if (strncmp(ptr, "MiB", strlen("MiB")) == 0) {
1204 shape_type = SHAPER_BYTES;
1205 rate *= 1 << 20;
1206 } else if (strncmp(ptr, "GiB", strlen("GiB")) == 0) {
1207 shape_type = SHAPER_BYTES;
1208 rate *= 1 << 30;
1209 } else if (!rate) {
1210 shape_type = SHAPER_NONE;
1211 } else {
1212 panic("Invalid unit type for rate\n");
1215 shaper_set_rate(&ctx.sh, rate, shape_type);
1216 break;
1217 case 'S':
1218 ctx.reserve_size = strtoul(optarg, &ptr, 0);
1219 if (ctx.reserve_size == 0 && ptr == optarg)
1220 panic("Invalid ring size param\n");
1222 if (!strncmp(ptr, "KiB", strlen("KiB")))
1223 ctx.reserve_size *= 1 << 10;
1224 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1225 ctx.reserve_size = 1 << 20;
1226 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1227 ctx.reserve_size *= 1 << 30;
1228 else
1229 panic("Invalid ring size unit type\n");
1231 break;
1232 case '?':
1233 switch (optopt) {
1234 case 'd':
1235 case 'c':
1236 case 'n':
1237 case 'S':
1238 case 's':
1239 case 'P':
1240 case 'o':
1241 case 'E':
1242 case 'i':
1243 case 'k':
1244 case 'u':
1245 case 'g':
1246 case 't':
1247 panic("Option -%c requires an argument!\n",
1248 optopt);
1249 default:
1250 if (isprint(optopt))
1251 printf("Unknown option character `0x%X\'!\n", optopt);
1252 die();
1254 default:
1255 break;
1259 if (argc >= optind) {
1260 min_opts = 4;
1261 ctx.packet_str = argv2str(optind, argc, argv);
1264 if (argc < min_opts)
1265 help();
1266 if (ctx.device == NULL)
1267 panic("No networking device given!\n");
1268 if (confname == NULL && !ctx.packet_str)
1269 panic("No configuration file or packet string given!\n");
1271 register_signal(SIGINT, signal_handler);
1272 register_signal(SIGQUIT, signal_handler);
1273 register_signal(SIGTERM, signal_handler);
1274 register_signal(SIGHUP, signal_handler);
1276 if (prio_high) {
1277 set_proc_prio(-20);
1278 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1281 if (set_sock_mem)
1282 set_system_socket_memory(vals, array_size(vals));
1283 xlockme();
1285 if (ctx.rfraw) {
1286 ctx.device_trans = xstrdup(ctx.device);
1287 xfree(ctx.device);
1289 enter_rfmon_mac80211(ctx.device_trans, &ctx.device);
1290 panic_handler_add(on_panic_del_rfmon, ctx.device);
1291 sleep(0);
1294 if (ctx.pcap_in) {
1295 ctx.dev_in = dev_io_open(ctx.pcap_in, DEV_IO_IN);
1296 if (!ctx.dev_in)
1297 panic("Failed to open input device\n");
1300 ctx.dev_out = dev_io_open(ctx.device, DEV_IO_OUT);
1301 if (!ctx.dev_out)
1302 panic("Failed to open output device\n");
1304 protos_init(ctx.dev_out);
1306 if (shaper_is_set(&ctx.sh) || (ctx.dev_in && dev_io_is_pcap(ctx.dev_in))
1307 || dev_io_is_pcap(ctx.dev_out)) {
1309 prctl(PR_SET_TIMERSLACK, 1UL);
1310 /* Fall back to single core to not mess up correct timing.
1311 * We are slow anyway!
1313 ctx.cpus = 1;
1314 slow = true;
1318 * If number of packets is smaller than number of CPUs use only as
1319 * many CPUs as there are packets. Otherwise we end up sending more
1320 * packets than intended or none at all.
1322 if (ctx.num)
1323 ctx.cpus = min_t(unsigned int, ctx.num, ctx.cpus);
1325 if (set_irq_aff && dev_io_is_netdev(ctx.dev_out)) {
1326 irq = device_irq_number(ctx.device);
1327 device_set_irq_affinity_list(irq, 0, ctx.cpus - 1);
1330 stats = setup_shared_var(ctx.cpus);
1332 for (i = 0; i < ctx.cpus; i++) {
1333 pid_t pid = fork();
1335 switch (pid) {
1336 case 0:
1337 if (reseed)
1338 seed = generate_srand_seed();
1339 srand(seed);
1341 cpu_affinity(i);
1342 main_loop(&ctx, confname, slow, i, invoke_cpp,
1343 cpp_argv, orig_num);
1345 goto thread_out;
1346 case -1:
1347 panic("Cannot fork processes!\n");
1351 for (i = 0; i < ctx.cpus; i++) {
1352 int status;
1354 wait(&status);
1355 if (WEXITSTATUS(status) == EXIT_FAILURE)
1356 die();
1359 if (ctx.rfraw)
1360 leave_rfmon_mac80211(ctx.device);
1362 if (set_sock_mem)
1363 reset_system_socket_memory(vals, array_size(vals));
1365 for (i = 0, tx_packets = tx_bytes = 0; i < ctx.cpus; i++) {
1366 while ((__get_state(i) & CPU_STATS_STATE_RES) == 0)
1367 sched_yield();
1369 tx_packets += stats[i].tx_packets;
1370 tx_bytes += stats[i].tx_bytes;
1373 fflush(stdout);
1374 printf("\n");
1375 printf("\r%12llu packets outgoing\n", tx_packets);
1376 printf("\r%12llu bytes outgoing\n", tx_bytes);
1377 for (i = 0; cpustats && i < ctx.cpus; i++) {
1378 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1379 stats[i].tv_sec, stats[i].tv_usec, i,
1380 stats[i].tx_packets);
1383 thread_out:
1384 xunlockme();
1385 destroy_shared_var(stats, ctx.cpus);
1386 if (dev_io_is_netdev(ctx.dev_out) && set_irq_aff)
1387 device_restore_irq_affinity_list();
1389 dev_io_close(ctx.dev_out);
1390 if (ctx.dev_in)
1391 dev_io_close(ctx.dev_in);
1393 argv_free(cpp_argv);
1394 free(ctx.device);
1395 free(ctx.device_trans);
1396 free(ctx.rhost);
1397 free(confname);
1398 free(ctx.packet_str);
1400 return 0;