iosched: break out io scheduling functions from xutils
[netsniff-ng.git] / trafgen.c
blobd493614fe45b4e446fd8553123a7982a434e4e49
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 #include <stdio.h>
9 #include <string.h>
10 #include <getopt.h>
11 #include <ctype.h>
12 #include <stdbool.h>
13 #include <sys/socket.h>
14 #include <sys/types.h>
15 #include <sys/fsuid.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <sys/wait.h>
19 #include <sys/mman.h>
20 #include <net/ethernet.h>
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <linux/icmp.h>
24 #include <arpa/inet.h>
25 #include <signal.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <poll.h>
31 #include <netdb.h>
32 #include <math.h>
33 #include <unistd.h>
35 #include "xmalloc.h"
36 #include "die.h"
37 #include "mac80211.h"
38 #include "xutils.h"
39 #include "xio.h"
40 #include "irq.h"
41 #include "built_in.h"
42 #include "trafgen_conf.h"
43 #include "tprintf.h"
44 #include "ring_tx.h"
45 #include "csum.h"
47 struct ctx {
48 bool rand, rfraw, jumbo_support, verbose, smoke_test, enforce;
49 unsigned long kpull, num, gap, reserve_size, cpus;
50 uid_t uid; gid_t gid; char *device, *device_trans, *rhost;
51 struct sockaddr_in dest;
54 struct cpu_stats {
55 unsigned long tv_sec, tv_usec;
56 unsigned long long tx_packets, tx_bytes;
57 unsigned long long cf_packets, cf_bytes;
58 unsigned long long cd_packets;
59 sig_atomic_t state;
62 sig_atomic_t sigint = 0;
64 struct packet *packets = NULL;
65 size_t plen = 0;
67 struct packet_dyn *packet_dyn = NULL;
68 size_t dlen = 0;
70 static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:";
71 static const struct option long_options[] = {
72 {"dev", required_argument, NULL, 'd'},
73 {"out", required_argument, NULL, 'o'},
74 {"in", required_argument, NULL, 'i'},
75 {"conf", required_argument, NULL, 'c'},
76 {"num", required_argument, NULL, 'n'},
77 {"gap", required_argument, NULL, 't'},
78 {"cpus", required_argument, NULL, 'P'},
79 {"ring-size", required_argument, NULL, 'S'},
80 {"kernel-pull", required_argument, NULL, 'k'},
81 {"smoke-test", required_argument, NULL, 's'},
82 {"seed", required_argument, NULL, 'E'},
83 {"user", required_argument, NULL, 'u'},
84 {"group", required_argument, NULL, 'g'},
85 {"jumbo-support", no_argument, NULL, 'J'},
86 {"cpp", no_argument, NULL, 'p'},
87 {"rfraw", no_argument, NULL, 'R'},
88 {"rand", no_argument, NULL, 'r'},
89 {"verbose", no_argument, NULL, 'V'},
90 {"version", no_argument, NULL, 'v'},
91 {"example", no_argument, NULL, 'e'},
92 {"help", no_argument, NULL, 'h'},
93 {NULL, 0, NULL, 0}
96 static int sock;
98 static struct itimerval itimer;
100 static unsigned long interval = TX_KERNEL_PULL_INT;
102 static struct cpu_stats *stats;
104 unsigned int seed;
106 #define CPU_STATS_STATE_CFG 1
107 #define CPU_STATS_STATE_CHK 2
108 #define CPU_STATS_STATE_RES 4
110 #ifndef ICMP_FILTER
111 # define ICMP_FILTER 1
113 struct icmp_filter {
114 __u32 data;
116 #endif
118 static void signal_handler(int number)
120 switch (number) {
121 case SIGINT:
122 sigint = 1;
123 case SIGHUP:
124 default:
125 break;
129 static void timer_elapsed(int number)
131 int ret = pull_and_flush_tx_ring(sock);
132 if (unlikely(ret < 0)) {
133 /* We could hit EBADF if the socket has been closed before
134 * the timer was triggered.
136 if (errno != EBADF && errno != ENOBUFS)
137 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
140 set_itimer_interval_value(&itimer, 0, interval);
141 setitimer(ITIMER_REAL, &itimer, NULL);
144 static void timer_purge(void)
146 int ret;
148 ret = pull_and_flush_tx_ring_wait(sock);
149 if (unlikely(ret < 0)) {
150 /* We could hit EBADF if the socket has been closed before
151 * the timer was triggered.
153 if (errno != EBADF && errno != ENOBUFS)
154 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
157 set_itimer_interval_value(&itimer, 0, 0);
158 setitimer(ITIMER_REAL, &itimer, NULL);
161 static void __noreturn help(void)
163 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
164 puts("http://www.netsniff-ng.org\n\n"
165 "Usage: trafgen [options]\n"
166 "Options:\n"
167 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
168 " -o|-d|--out|--dev <netdev> Networking device i.e., eth0\n"
169 " -p|--cpp Run packet config through C preprocessor\n"
170 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
171 " -R|--rfraw Inject raw 802.11 frames\n"
172 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
173 " -n|--num <uint> Number of packets until exit (def: 0)\n"
174 " -r|--rand Randomize packet selection (def: round robin)\n"
175 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
176 " -t|--gap <uint> Interpacket gap in us (approx)\n"
177 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
178 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
179 " -E|--seed <uint> Manually set srand(3) seed\n"
180 " -u|--user <userid> Drop privileges and change to userid\n"
181 " -g|--group <groupid> Drop privileges and change to groupid\n"
182 " -V|--verbose Be more verbose\n"
183 " -v|--version Show version and exit\n"
184 " -e|--example Show built-in packet config example\n"
185 " -h|--help Guess what?!\n\n"
186 "Examples:\n"
187 " See trafgen.txf for configuration file examples.\n"
188 " trafgen --dev eth0 --conf trafgen.cfg\n"
189 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
190 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
191 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
192 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000\n"
193 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
194 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n\n"
195 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
196 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
197 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
198 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
199 "Note:\n"
200 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
201 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
202 " we assume the kernel crashed, thus we print the packet and quit.\n"
203 " In case you find a ping-of-death, please mention trafgen in your\n"
204 " commit message of the fix!\n\n"
205 " For introducing bit errors, delays with random variation and more,\n"
206 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
207 " For generating different package distributions, you can use scripting\n"
208 " to generate a trafgen config file with packet ratios as:\n\n"
209 " IMIX 64:7, 570:4, 1518:1\n"
210 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
211 " Cisco 64:7, 594:4, 1518:1\n"
212 " RPR Trimodal 64:60, 512:20, 1518:20\n"
213 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n\n"
214 "Please report bugs to <bugs@netsniff-ng.org>\n"
215 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
216 "Swiss federal institute of technology (ETH Zurich)\n"
217 "License: GNU GPL version 2.0\n"
218 "This is free software: you are free to change and redistribute it.\n"
219 "There is NO WARRANTY, to the extent permitted by law.\n");
220 die();
223 static void __noreturn example(void)
225 const char *e =
226 "/* Note: dynamic elements make trafgen slower! */\n"
227 "#include <stddef.h>\n\n"
228 "{\n"
229 " /* MAC Destination */\n"
230 " fill(0xff, ETH_ALEN),\n"
231 " /* MAC Source */\n"
232 " 0x00, 0x02, 0xb3, drnd(3),\n"
233 " /* IPv4 Protocol */\n"
234 " c16(ETH_P_IP),\n"
235 " /* IPv4 Version, IHL, TOS */\n"
236 " 0b01000101, 0,\n"
237 " /* IPv4 Total Len */\n"
238 " c16(58),\n"
239 " /* IPv4 Ident */\n"
240 " drnd(2),\n"
241 " /* IPv4 Flags, Frag Off */\n"
242 " 0b01000000, 0,\n"
243 " /* IPv4 TTL */\n"
244 " 64,\n"
245 " /* Proto TCP */\n"
246 " 0x06,\n"
247 " /* IPv4 Checksum (IP header from, to) */\n"
248 " csumip(14, 33),\n"
249 " /* Source IP */\n"
250 " drnd(4),\n"
251 " /* Dest IP */\n"
252 " drnd(4),\n"
253 " /* TCP Source Port */\n"
254 " drnd(2),\n"
255 " /* TCP Dest Port */\n"
256 " c16(80),\n"
257 " /* TCP Sequence Number */\n"
258 " drnd(4),\n"
259 " /* TCP Ackn. Number */\n"
260 " c32(0),\n"
261 " /* TCP Header length + TCP SYN/ECN Flag */\n"
262 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
263 " /* Window Size */\n"
264 " c16(16),\n"
265 " /* TCP Checksum (offset IP, offset TCP) */\n"
266 " csumtcp(14, 34),\n"
267 " /* TCP Options */\n"
268 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
269 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
270 " /* Data blob */\n"
271 " \"gotcha!\",\n"
272 "}";
273 puts(e);
274 die();
277 static void __noreturn version(void)
279 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_LONG);
280 puts("http://www.netsniff-ng.org\n\n"
281 "Please report bugs to <bugs@netsniff-ng.org>\n"
282 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
283 "Swiss federal institute of technology (ETH Zurich)\n"
284 "License: GNU GPL version 2.0\n"
285 "This is free software: you are free to change and redistribute it.\n"
286 "There is NO WARRANTY, to the extent permitted by law.\n");
287 die();
290 static void apply_counter(int counter_id)
292 int j, i = counter_id;
293 size_t counter_max = packet_dyn[i].clen;
295 for (j = 0; j < counter_max; ++j) {
296 uint8_t val;
297 struct counter *counter = &packet_dyn[i].cnt[j];
299 val = counter->val - counter->min;
301 switch (counter->type) {
302 case TYPE_INC:
303 val = (val + counter->inc) % (counter->max - counter->min + 1);
304 break;
305 case TYPE_DEC:
306 val = (val - counter->inc) % (counter->min - counter->max + 1);
307 break;
308 default:
309 bug();
312 counter->val = val + counter->min;
313 packets[i].payload[counter->off] = val;
317 static void apply_randomizer(int rand_id)
319 int j, i = rand_id;
320 size_t rand_max = packet_dyn[i].rlen;
322 for (j = 0; j < rand_max; ++j) {
323 uint8_t val = (uint8_t) rand();
324 struct randomizer *randomizer = &packet_dyn[i].rnd[j];
326 packets[i].payload[randomizer->off] = val;
330 static void apply_csum16(int csum_id)
332 int j, i = csum_id;
333 size_t csum_max = packet_dyn[i].slen;
335 for (j = 0; j < csum_max; ++j) {
336 uint16_t sum = 0;
337 struct csum16 *csum = &packet_dyn[i].csum[j];
339 fmemset(&packets[i].payload[csum->off], 0, sizeof(sum));
341 switch (csum->which) {
342 case CSUM_IP:
343 if (csum->to >= packets[i].len)
344 csum->to = packets[i].len - 1;
345 sum = calc_csum(packets[i].payload + csum->from,
346 csum->to - csum->from + 1, 0);
347 break;
348 case CSUM_UDP:
349 sum = p4_csum((void *) packets[i].payload + csum->from,
350 packets[i].payload + csum->to,
351 (packets[i].len - csum->to),
352 IPPROTO_UDP);
353 break;
354 case CSUM_TCP:
355 sum = p4_csum((void *) packets[i].payload + csum->from,
356 packets[i].payload + csum->to,
357 (packets[i].len - csum->to),
358 IPPROTO_TCP);
359 break;
362 fmemcpy(&packets[i].payload[csum->off], &sum, sizeof(sum));
366 static struct cpu_stats *setup_shared_var(unsigned long cpus)
368 int fd;
369 char zbuff[cpus * sizeof(struct cpu_stats)], file[256];
370 struct cpu_stats *buff;
372 fmemset(zbuff, 0, sizeof(zbuff));
373 slprintf(file, sizeof(file), ".tmp_mmap.%u", (unsigned int) rand());
375 fd = creat(file, S_IRUSR | S_IWUSR);
376 bug_on(fd < 0);
377 close(fd);
379 fd = open_or_die_m(file, O_RDWR | O_CREAT | O_TRUNC,
380 S_IRUSR | S_IWUSR);
381 write_or_die(fd, zbuff, sizeof(zbuff));
383 buff = (void *) mmap(0, sizeof(zbuff), PROT_READ | PROT_WRITE,
384 MAP_SHARED, fd, 0);
385 if (buff == (void *) -1)
386 panic("Cannot setup shared variable!\n");
388 close(fd);
389 unlink(file);
391 memset(buff, 0, sizeof(zbuff));
393 return buff;
396 static void destroy_shared_var(void *buff, unsigned long cpus)
398 munmap(buff, cpus * sizeof(struct cpu_stats));
401 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
403 int i;
405 printf("{");
406 for (i = 0; i < len; ++i) {
407 if (i % 15 == 0)
408 printf("\n ");
409 printf("0x%02x, ", payload[i]);
411 printf("\n}\n");
412 fflush(stdout);
415 static int xmit_smoke_setup(struct ctx *ctx)
417 int icmp_sock, ret, ttl = 64;
418 struct icmp_filter filter;
420 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
421 if (icmp_sock < 0)
422 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
424 filter.data = ~(1 << ICMP_ECHOREPLY);
426 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
427 if (ret < 0)
428 panic("Cannot install filter!\n");
430 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
431 if (ret < 0)
432 panic("Cannot set TTL!\n");
434 memset(&ctx->dest, 0, sizeof(ctx->dest));
435 ctx->dest.sin_family = AF_INET;
436 ctx->dest.sin_port = 0;
438 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
439 if (ret < 0)
440 panic("Cannot resolv address!\n");
442 return icmp_sock;
445 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
447 int ret, i, j = 0, probes = 100;
448 short ident, cnt = 1, idstore[probes];
449 uint8_t outpack[512], *data;
450 struct icmphdr *icmp;
451 struct iphdr *ip;
452 size_t len = sizeof(*icmp) + 56;
453 struct sockaddr_in from;
454 socklen_t from_len;
455 struct pollfd fds = {
456 .fd = icmp_sock,
457 .events = POLLIN,
460 fmemset(idstore, 0, sizeof(idstore));
461 while (probes-- > 0) {
462 while ((ident = htons((short) rand())) == 0)
463 sleep(0);
464 idstore[j++] = ident;
466 memset(outpack, 0, sizeof(outpack));
467 icmp = (void *) outpack;
468 icmp->type = ICMP_ECHO;
469 icmp->un.echo.id = ident;
470 icmp->un.echo.sequence = htons(cnt++);
472 data = ((uint8_t *) outpack + sizeof(*icmp));
473 for (i = 0; i < 56; ++i)
474 data[i] = (uint8_t) rand();
476 icmp->checksum = csum((unsigned short *) outpack,
477 len / sizeof(unsigned short));
479 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
480 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
481 if (unlikely(ret != len))
482 panic("Cannot send out probe: %s!\n", strerror(errno));
484 ret = poll(&fds, 1, 50);
485 if (ret < 0)
486 panic("Poll failed!\n");
488 if (fds.revents & POLLIN) {
489 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
490 (struct sockaddr *) &from, &from_len);
491 if (unlikely(ret <= 0))
492 panic("Probe receive failed!\n");
493 if (unlikely(from_len != sizeof(ctx->dest)))
494 continue;
495 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
496 continue;
497 if (unlikely(ret < sizeof(*ip) + sizeof(*icmp)))
498 continue;
499 ip = (void *) outpack;
500 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > ret))
501 continue;
502 icmp = (void *) outpack + ip->ihl * 4;
503 for (i = 0; i < array_size(idstore); ++i) {
504 if (unlikely(icmp->un.echo.id != idstore[i]))
505 continue;
506 return 0;
511 return -1;
514 static void xmit_slowpath_or_die(struct ctx *ctx, int cpu, unsigned long orig_num)
516 int ret, icmp_sock = -1;
517 unsigned long num = 1, i = 0;
518 struct timeval start, end, diff;
519 unsigned long long tx_bytes = 0, tx_packets = 0;
520 struct packet_dyn *pktd;
521 struct sockaddr_ll saddr = {
522 .sll_family = PF_PACKET,
523 .sll_halen = ETH_ALEN,
524 .sll_ifindex = device_ifindex(ctx->device),
527 if (ctx->num > 0)
528 num = ctx->num;
529 if (ctx->num == 0 && orig_num > 0)
530 num = 0;
532 if (ctx->smoke_test)
533 icmp_sock = xmit_smoke_setup(ctx);
535 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
537 bug_on(gettimeofday(&start, NULL));
539 while (likely(sigint == 0) && likely(num > 0) && likely(plen > 0)) {
540 pktd = &packet_dyn[i];
541 if (pktd->clen + pktd->rlen + pktd->slen) {
542 apply_counter(i);
543 apply_randomizer(i);
544 apply_csum16(i);
546 retry:
547 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
548 (struct sockaddr *) &saddr, sizeof(saddr));
549 if (unlikely(ret < 0)) {
550 if (errno == ENOBUFS) {
551 sched_yield();
552 goto retry;
555 panic("Sendto error: %s!\n", strerror(errno));
558 tx_bytes += packets[i].len;
559 tx_packets++;
561 if (ctx->smoke_test) {
562 ret = xmit_smoke_probe(icmp_sock, ctx);
563 if (unlikely(ret < 0)) {
564 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
565 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
566 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
567 i, seed);
569 dump_trafgen_snippet(packets[i].payload, packets[i].len);
570 break;
574 if (!ctx->rand) {
575 i++;
576 if (i >= plen)
577 i = 0;
578 } else
579 i = rand() % plen;
581 if (ctx->num > 0)
582 num--;
584 if (ctx->gap > 0)
585 usleep(ctx->gap);
588 bug_on(gettimeofday(&end, NULL));
589 timersub(&end, &start, &diff);
591 if (ctx->smoke_test)
592 close(icmp_sock);
594 stats[cpu].tx_packets = tx_packets;
595 stats[cpu].tx_bytes = tx_bytes;
596 stats[cpu].tv_sec = diff.tv_sec;
597 stats[cpu].tv_usec = diff.tv_usec;
599 stats[cpu].state |= CPU_STATS_STATE_RES;
602 static void xmit_fastpath_or_die(struct ctx *ctx, int cpu, unsigned long orig_num)
604 int ifindex = device_ifindex(ctx->device);
605 uint8_t *out = NULL;
606 unsigned int it = 0;
607 unsigned long num = 1, i = 0, size;
608 struct ring tx_ring;
609 struct frame_map *hdr;
610 struct timeval start, end, diff;
611 struct packet_dyn *pktd;
612 unsigned long long tx_bytes = 0, tx_packets = 0;
614 fmemset(&tx_ring, 0, sizeof(tx_ring));
616 size = ring_size(ctx->device, ctx->reserve_size);
618 set_sock_prio(sock, 512);
619 set_packet_loss_discard(sock);
621 setup_tx_ring_layout(sock, &tx_ring, size, ctx->jumbo_support);
622 create_tx_ring(sock, &tx_ring, ctx->verbose);
623 mmap_tx_ring(sock, &tx_ring);
624 alloc_tx_ring_frames(sock, &tx_ring);
625 bind_tx_ring(sock, &tx_ring, ifindex);
627 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
629 if (ctx->kpull)
630 interval = ctx->kpull;
631 if (ctx->num > 0)
632 num = ctx->num;
633 if (ctx->num == 0 && orig_num > 0)
634 num = 0;
636 set_itimer_interval_value(&itimer, 0, interval);
637 setitimer(ITIMER_REAL, &itimer, NULL);
639 bug_on(gettimeofday(&start, NULL));
641 while (likely(sigint == 0) && likely(num > 0) && likely(plen > 0)) {
642 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) && likely(num > 0)) {
643 hdr = tx_ring.frames[it].iov_base;
644 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
646 hdr->tp_h.tp_snaplen = packets[i].len;
647 hdr->tp_h.tp_len = packets[i].len;
649 pktd = &packet_dyn[i];
650 if (pktd->clen + pktd->rlen + pktd->slen) {
651 apply_counter(i);
652 apply_randomizer(i);
653 apply_csum16(i);
656 fmemcpy(out, packets[i].payload, packets[i].len);
658 tx_bytes += packets[i].len;
659 tx_packets++;
661 if (!ctx->rand) {
662 i++;
663 if (i >= plen)
664 i = 0;
665 } else
666 i = rand() % plen;
668 kernel_may_pull_from_tx(&hdr->tp_h);
670 it++;
671 if (it >= tx_ring.layout.tp_frame_nr)
672 it = 0;
674 if (ctx->num > 0)
675 num--;
677 if (unlikely(sigint == 1))
678 break;
682 bug_on(gettimeofday(&end, NULL));
683 timersub(&end, &start, &diff);
685 timer_purge();
687 destroy_tx_ring(sock, &tx_ring);
689 stats[cpu].tx_packets = tx_packets;
690 stats[cpu].tx_bytes = tx_bytes;
691 stats[cpu].tv_sec = diff.tv_sec;
692 stats[cpu].tv_usec = diff.tv_usec;
694 stats[cpu].state |= CPU_STATS_STATE_RES;
697 static inline void __set_state(int cpu, sig_atomic_t s)
699 stats[cpu].state = s;
702 static inline sig_atomic_t __get_state(int cpu)
704 return stats[cpu].state;
707 static unsigned long __wait_and_sum_others(struct ctx *ctx, int cpu)
709 int i;
710 unsigned long total;
712 for (i = 0, total = plen; i < ctx->cpus; i++) {
713 if (i == cpu)
714 continue;
716 while ((__get_state(i) &
717 (CPU_STATS_STATE_CFG |
718 CPU_STATS_STATE_RES)) == 0 &&
719 sigint == 0)
720 sched_yield();
722 total += stats[i].cf_packets;
725 return total;
728 static void __correct_global_delta(struct ctx *ctx, int cpu, unsigned long orig)
730 int i, cpu_sel;
731 unsigned long total;
732 long long delta_correction = 0;
734 for (i = 0, total = ctx->num; i < ctx->cpus; i++) {
735 if (i == cpu)
736 continue;
738 while ((__get_state(i) &
739 (CPU_STATS_STATE_CHK |
740 CPU_STATS_STATE_RES)) == 0 &&
741 sigint == 0)
742 sched_yield();
744 total += stats[i].cd_packets;
747 if (total > orig)
748 delta_correction = -1 * ((long long) total - orig);
749 if (total < orig)
750 delta_correction = +1 * ((long long) orig - total);
752 for (cpu_sel = -1, i = 0; i < ctx->cpus; i++) {
753 if (stats[i].cd_packets > 0) {
754 if ((long long) stats[i].cd_packets +
755 delta_correction > 0) {
756 cpu_sel = i;
757 break;
762 if (cpu == cpu_sel)
763 ctx->num += delta_correction;
766 static void __set_state_cf(int cpu, unsigned long p, unsigned long b,
767 sig_atomic_t s)
769 stats[cpu].cf_packets = p;
770 stats[cpu].cf_bytes = b;
771 stats[cpu].state = s;
774 static void __set_state_cd(int cpu, unsigned long p, sig_atomic_t s)
776 stats[cpu].cd_packets = p;
777 stats[cpu].state = s;
780 static int xmit_packet_precheck(struct ctx *ctx, int cpu)
782 int i;
783 unsigned long plen_total, orig = ctx->num;
784 size_t mtu, total_len = 0;
786 bug_on(plen != dlen);
788 for (i = 0; i < plen; ++i)
789 total_len += packets[i].len;
791 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
792 plen_total = __wait_and_sum_others(ctx, cpu);
794 if (orig > 0) {
795 ctx->num = (unsigned long) nearbyint((1.0 * plen / plen_total) * orig);
797 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
798 CPU_STATS_STATE_CFG);
799 __correct_global_delta(ctx, cpu, orig);
802 if (plen == 0) {
803 __set_state(cpu, CPU_STATS_STATE_RES);
804 return 0;
807 for (mtu = device_mtu(ctx->device), i = 0; i < plen; ++i) {
808 if (packets[i].len > mtu + 14)
809 panic("Device MTU < than packet%d's size!\n", i);
810 if (packets[i].len <= 14)
811 panic("Packet%d's size too short!\n", i);
814 return 0;
817 static void main_loop(struct ctx *ctx, char *confname, bool slow,
818 int cpu, bool invoke_cpp, unsigned long orig_num)
820 compile_packets(confname, ctx->verbose, cpu, invoke_cpp);
821 if (xmit_packet_precheck(ctx, cpu) < 0)
822 return;
824 if (cpu == 0) {
825 int i;
826 size_t total_len = 0, total_pkts = 0;
828 for (i = 0; i < ctx->cpus; ++i) {
829 total_len += stats[i].cf_bytes;
830 total_pkts += stats[i].cf_packets;
833 printf("%6zu packets to schedule\n", total_pkts);
834 printf("%6zu bytes in total\n", total_len);
835 printf("Running! Hang up with ^C!\n\n");
836 fflush(stdout);
839 sock = pf_socket();
841 if (slow)
842 xmit_slowpath_or_die(ctx, cpu, orig_num);
843 else
844 xmit_fastpath_or_die(ctx, cpu, orig_num);
846 close(sock);
848 cleanup_packets();
851 static unsigned int generate_srand_seed(void)
853 int fd;
854 unsigned int seed;
856 fd = open("/dev/urandom", O_RDONLY);
857 if (fd < 0)
858 return time(0);
860 read_or_die(fd, &seed, sizeof(seed));
862 close(fd);
863 return seed;
866 int main(int argc, char **argv)
868 bool slow = false, invoke_cpp = false, reseed = true;
869 int c, opt_index, i, j, vals[4] = {0}, irq;
870 char *confname = NULL, *ptr;
871 unsigned long cpus_tmp, orig_num = 0;
872 unsigned long long tx_packets, tx_bytes;
873 struct ctx ctx;
875 fmemset(&ctx, 0, sizeof(ctx));
876 ctx.cpus = get_number_cpus_online();
877 ctx.uid = getuid();
878 ctx.gid = getgid();
880 while ((c = getopt_long(argc, argv, short_options, long_options,
881 &opt_index)) != EOF) {
882 switch (c) {
883 case 'h':
884 help();
885 break;
886 case 'v':
887 version();
888 break;
889 case 'e':
890 example();
891 break;
892 case 'p':
893 invoke_cpp = true;
894 break;
895 case 'V':
896 ctx.verbose = true;
897 break;
898 case 'P':
899 cpus_tmp = strtoul(optarg, NULL, 0);
900 if (cpus_tmp > 0 && cpus_tmp < ctx.cpus)
901 ctx.cpus = cpus_tmp;
902 break;
903 case 'd':
904 case 'o':
905 ctx.device = xstrndup(optarg, IFNAMSIZ);
906 break;
907 case 'r':
908 ctx.rand = true;
909 break;
910 case 's':
911 slow = true;
912 ctx.cpus = 1;
913 ctx.smoke_test = true;
914 ctx.rhost = xstrdup(optarg);
915 break;
916 case 'R':
917 ctx.rfraw = true;
918 break;
919 case 'J':
920 ctx.jumbo_support = true;
921 break;
922 case 'c':
923 case 'i':
924 confname = xstrdup(optarg);
925 if (!strncmp("-", confname, strlen("-")))
926 ctx.cpus = 1;
927 break;
928 case 'u':
929 ctx.uid = strtoul(optarg, NULL, 0);
930 ctx.enforce = true;
931 break;
932 case 'g':
933 ctx.gid = strtoul(optarg, NULL, 0);
934 ctx.enforce = true;
935 break;
936 case 'k':
937 ctx.kpull = strtoul(optarg, NULL, 0);
938 break;
939 case 'E':
940 seed = strtoul(optarg, NULL, 0);
941 reseed = false;
942 break;
943 case 'n':
944 orig_num = strtoul(optarg, NULL, 0);
945 ctx.num = orig_num;
946 break;
947 case 't':
948 slow = true;
949 ctx.gap = strtoul(optarg, NULL, 0);
950 if (ctx.gap > 0)
951 /* Fall back to single core to not
952 * mess up correct timing. We are slow
953 * anyway!
955 ctx.cpus = 1;
956 break;
957 case 'S':
958 ptr = optarg;
959 ctx.reserve_size = 0;
961 for (j = i = strlen(optarg); i > 0; --i) {
962 if (!isdigit(optarg[j - i]))
963 break;
964 ptr++;
967 if (!strncmp(ptr, "KiB", strlen("KiB")))
968 ctx.reserve_size = 1 << 10;
969 else if (!strncmp(ptr, "MiB", strlen("MiB")))
970 ctx.reserve_size = 1 << 20;
971 else if (!strncmp(ptr, "GiB", strlen("GiB")))
972 ctx.reserve_size = 1 << 30;
973 else
974 panic("Syntax error in ring size param!\n");
975 *ptr = 0;
977 ctx.reserve_size *= strtol(optarg, NULL, 0);
978 break;
979 case '?':
980 switch (optopt) {
981 case 'd':
982 case 'c':
983 case 'n':
984 case 'S':
985 case 's':
986 case 'P':
987 case 'o':
988 case 'E':
989 case 'i':
990 case 'k':
991 case 'u':
992 case 'g':
993 case 't':
994 panic("Option -%c requires an argument!\n",
995 optopt);
996 default:
997 if (isprint(optopt))
998 printf("Unknown option character `0x%X\'!\n", optopt);
999 die();
1001 default:
1002 break;
1006 if (argc < 5)
1007 help();
1008 if (ctx.device == NULL)
1009 panic("No networking device given!\n");
1010 if (confname == NULL)
1011 panic("No configuration file given!\n");
1012 if (device_mtu(ctx.device) == 0)
1013 panic("This is no networking device!\n");
1015 register_signal(SIGINT, signal_handler);
1016 register_signal(SIGHUP, signal_handler);
1017 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1019 set_system_socket_memory(vals, array_size(vals));
1020 xlockme();
1022 if (ctx.rfraw) {
1023 ctx.device_trans = xstrdup(ctx.device);
1024 xfree(ctx.device);
1026 enter_rfmon_mac80211(ctx.device_trans, &ctx.device);
1027 sleep(0);
1030 irq = device_irq_number(ctx.device);
1031 device_set_irq_affinity_list(irq, 0, ctx.cpus - 1);
1033 stats = setup_shared_var(ctx.cpus);
1035 for (i = 0; i < ctx.cpus; i++) {
1036 pid_t pid = fork();
1038 switch (pid) {
1039 case 0:
1040 if (reseed)
1041 seed = generate_srand_seed();
1042 srand(seed);
1044 cpu_affinity(i);
1045 main_loop(&ctx, confname, slow, i, invoke_cpp, orig_num);
1047 goto thread_out;
1048 case -1:
1049 panic("Cannot fork processes!\n");
1053 for (i = 0; i < ctx.cpus; i++) {
1054 int status;
1056 wait(&status);
1057 if (WEXITSTATUS(status) == EXIT_FAILURE)
1058 die();
1061 if (ctx.rfraw)
1062 leave_rfmon_mac80211(ctx.device_trans, ctx.device);
1064 reset_system_socket_memory(vals, array_size(vals));
1066 for (i = 0, tx_packets = tx_bytes = 0; i < ctx.cpus; i++) {
1067 while ((__get_state(i) & CPU_STATS_STATE_RES) == 0)
1068 sched_yield();
1070 tx_packets += stats[i].tx_packets;
1071 tx_bytes += stats[i].tx_bytes;
1074 fflush(stdout);
1075 printf("\n");
1076 printf("\r%12llu packets outgoing\n", tx_packets);
1077 printf("\r%12llu bytes outgoing\n", tx_bytes);
1078 for (i = 0; i < ctx.cpus; i++) {
1079 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1080 stats[i].tv_sec, stats[i].tv_usec, i,
1081 stats[i].tx_packets);
1084 thread_out:
1085 xunlockme();
1086 destroy_shared_var(stats, ctx.cpus);
1087 device_restore_irq_affinity_list();
1089 free(ctx.device);
1090 free(ctx.device_trans);
1091 free(ctx.rhost);
1092 free(confname);
1094 return 0;