trafgen: fix csum16 calculation
[netsniff-ng.git] / src / trafgen.c
blobe4eb64b607ef2d63962c0908c79a1ec340f32aec
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 - 2013 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'.
24 #include <stdio.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <ctype.h>
28 #include <stdbool.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
31 #include <sys/fsuid.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <sys/wait.h>
35 #include <sys/mman.h>
36 #include <net/ethernet.h>
37 #include <netinet/in.h>
38 #include <netinet/ip.h>
39 #include <linux/icmp.h>
40 #include <arpa/inet.h>
41 #include <signal.h>
42 #include <stdint.h>
43 #include <stdlib.h>
44 #include <fcntl.h>
45 #include <time.h>
46 #include <poll.h>
47 #include <netdb.h>
48 #include <math.h>
50 #include "xmalloc.h"
51 #include "die.h"
52 #include "mac80211.h"
53 #include "xutils.h"
54 #include "xio.h"
55 #include "built_in.h"
56 #include "trafgen_conf.h"
57 #include "tprintf.h"
58 #include "ring_tx.h"
59 #include "csum.h"
61 struct ctx {
62 bool rand, rfraw, jumbo_support, verbose, smoke_test;
63 unsigned long kpull, num, gap, reserve_size, cpus;
64 struct sockaddr_in dest;
65 char *device, *device_trans, *rhost;
68 struct cpu_stats {
69 unsigned long tv_sec, tv_usec;
70 unsigned long long tx_packets, tx_bytes;
71 unsigned long long cf_packets, cf_bytes;
72 unsigned long long cd_packets;
73 sig_atomic_t state;
76 sig_atomic_t sigint = 0;
78 struct packet *packets = NULL;
79 size_t plen = 0;
81 struct packet_dyn *packet_dyn = NULL;
82 size_t dlen = 0;
84 static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRsP:e";
85 static const struct option long_options[] = {
86 {"dev", required_argument, NULL, 'd'},
87 {"out", required_argument, NULL, 'o'},
88 {"in", required_argument, NULL, 'i'},
89 {"conf", required_argument, NULL, 'c'},
90 {"num", required_argument, NULL, 'n'},
91 {"gap", required_argument, NULL, 't'},
92 {"cpus", required_argument, NULL, 'P'},
93 {"ring-size", required_argument, NULL, 'S'},
94 {"kernel-pull", required_argument, NULL, 'k'},
95 {"smoke-test", required_argument, NULL, 's'},
96 {"jumbo-support", no_argument, NULL, 'J'},
97 {"rfraw", no_argument, NULL, 'R'},
98 {"rand", no_argument, NULL, 'r'},
99 {"verbose", no_argument, NULL, 'V'},
100 {"version", no_argument, NULL, 'v'},
101 {"example", no_argument, NULL, 'e'},
102 {"help", no_argument, NULL, 'h'},
103 {NULL, 0, NULL, 0}
106 static int sock;
108 static struct itimerval itimer;
110 static unsigned long interval = TX_KERNEL_PULL_INT;
112 static struct cpu_stats *stats;
114 #define CPU_STATS_STATE_CFG 1
115 #define CPU_STATS_STATE_CHK 2
116 #define CPU_STATS_STATE_RES 4
118 #define set_system_socket_memory(vals) \
119 do { \
120 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
121 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
122 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
123 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
124 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
125 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
126 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
127 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
128 } while (0)
130 #define reset_system_socket_memory(vals) \
131 do { \
132 set_system_socket_mem(sock_rmem_max, vals[0]); \
133 set_system_socket_mem(sock_rmem_def, vals[1]); \
134 set_system_socket_mem(sock_wmem_max, vals[2]); \
135 set_system_socket_mem(sock_wmem_def, vals[3]); \
136 } while (0)
138 #ifndef ICMP_FILTER
139 # define ICMP_FILTER 1
141 struct icmp_filter {
142 __u32 data;
144 #endif
146 static void signal_handler(int number)
148 switch (number) {
149 case SIGINT:
150 sigint = 1;
151 case SIGHUP:
152 default:
153 break;
157 static void timer_elapsed(int number)
159 itimer.it_interval.tv_sec = 0;
160 itimer.it_interval.tv_usec = interval;
162 itimer.it_value.tv_sec = 0;
163 itimer.it_value.tv_usec = interval;
165 pull_and_flush_tx_ring(sock);
166 setitimer(ITIMER_REAL, &itimer, NULL);
169 static void header(void)
171 printf("%s%s%s\n", colorize_start(bold), "trafgen " VERSION_STRING, colorize_end());
174 static void help(void)
176 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
177 puts("http://www.netsniff-ng.org\n\n"
178 "Usage: trafgen [options]\n"
179 "Options:\n"
180 " -o|-d|--out|--dev <netdev> Networking Device i.e., eth0\n"
181 " -i|-c|--in|--conf <cfg-file> Packet configuration file\n"
182 " -J|--jumbo-support Support 64KB Super Jumbo Frames (def: 2048B)\n"
183 " -R|--rfraw Inject raw 802.11 frames\n"
184 " -s|--smoke-test <ipv4-receiver> Test if machine survived packet\n"
185 " -n|--num <uint> Number of packets until exit (def: 0)\n"
186 " -r|--rand Randomize packet selection (def: round robin)\n"
187 " -P|--cpus <uint> Specify number of forks(<= CPUs) (def: #CPUs)\n"
188 " -t|--gap <uint> Interpacket gap in us (approx)\n"
189 " -S|--ring-size <size> Manually set mmap size (KB/MB/GB): e.g.\'10MB\'\n"
190 " -k|--kernel-pull <uint> Kernel batch interval in us (def: 10us)\n"
191 " -V|--verbose Be more verbose\n"
192 " -v|--version Show version\n"
193 " -e|--example Show built-in packet config example\n"
194 " -h|--help Guess what?!\n\n"
195 "Examples:\n"
196 " See trafgen.txf for configuration file examples.\n"
197 " trafgen --dev eth0 --conf trafgen.cfg\n"
198 " trafgen --dev eth0 --conf trafgen.cfg --smoke-test 10.0.0.1\n"
199 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
200 " trafgen --dev eth0 --conf trafgen.cfg --rand --gap 1000\n"
201 " trafgen --dev eth0 --conf trafgen.cfg --rand --num 1400000 -k1000\n\n"
202 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
203 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
204 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
205 " Run packet only on CPU1-2: cpu(1:2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
206 "Note:\n"
207 " Smoke test example: machine A, 10.0.0.2 (trafgen) is directly\n"
208 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
209 " we assume the kernel crashed, thus we print the packet and quit.\n\n"
210 " This tool is targeted for network developers! You should\n"
211 " be aware of what you are doing and what these options above\n"
212 " mean! Only use this tool in an isolated LAN that you own!\n\n"
213 "Please report bugs to <bugs@netsniff-ng.org>\n"
214 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
215 "Swiss federal institute of technology (ETH Zurich)\n"
216 "License: GNU GPL version 2.0\n"
217 "This is free software: you are free to change and redistribute it.\n"
218 "There is NO WARRANTY, to the extent permitted by law.\n");
219 die();
222 static void example(void)
224 puts("{\n"
225 " # MAC Destination\n"
226 " drnd(6),\n"
227 " # MAC Source\n"
228 " drnd(6),\n"
229 " # Protocol\n"
230 " 0x08,0x00,\n"
231 " # IP header bits'n'pieces\n"
232 " 0x45,0x00,0x00,0x34,0xaf,0xf1,0x00,0x00,0x37,0x06,\n"
233 " # Dynamic IP Checksum\n"
234 " csumip(14, 34),\n"
235 " # Source IP\n"
236 " drnd(4),\n"
237 " # Dest IP\n"
238 " drnd(4),\n"
239 " # TCP Proto\n"
240 " drnd(32),\n"
241 "}");
242 die();
245 static void version(void)
247 printf("\ntrafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
248 puts("http://www.netsniff-ng.org\n\n"
249 "Please report bugs to <bugs@netsniff-ng.org>\n"
250 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
251 "Swiss federal institute of technology (ETH Zurich)\n"
252 "License: GNU GPL version 2.0\n"
253 "This is free software: you are free to change and redistribute it.\n"
254 "There is NO WARRANTY, to the extent permitted by law.\n");
255 die();
258 static void apply_counter(int counter_id)
260 int j, i = counter_id;
261 size_t counter_max = packet_dyn[i].clen;
263 for (j = 0; j < counter_max; ++j) {
264 uint8_t val;
265 struct counter *counter = &packet_dyn[i].cnt[j];
267 val = counter->val - counter->min;
269 switch (counter->type) {
270 case TYPE_INC:
271 val = (val + counter->inc) % (counter->max - counter->min + 1);
272 break;
273 case TYPE_DEC:
274 val = (val - counter->inc) % (counter->min - counter->max + 1);
275 break;
276 default:
277 bug();
280 counter->val = val + counter->min;
281 packets[i].payload[counter->off] = val;
285 static void apply_randomizer(int rand_id)
287 int j, i = rand_id;
288 size_t rand_max = packet_dyn[i].rlen;
290 for (j = 0; j < rand_max; ++j) {
291 uint8_t val = (uint8_t) rand();
292 struct randomizer *randomizer = &packet_dyn[i].rnd[j];
294 packets[i].payload[randomizer->off] = val;
298 static void apply_csum16(int csum_id)
300 int j, i = csum_id;
301 size_t csum_max = packet_dyn[i].slen;
303 for (j = 0; j < csum_max; ++j) {
304 uint16_t sum;
305 uint8_t *psum;
306 struct csum16 *csum = &packet_dyn[i].csum[j];
308 packets[i].payload[csum->off] = 0;
309 packets[i].payload[csum->off + 1] = 0;
311 if (csum->to >= packets[i].len)
312 csum->to = packets[i].len - 1;
314 sum = htons(calc_csum(packets[i].payload + csum->from,
315 csum->to - csum->from, 0));
316 psum = (uint8_t *) &sum;
318 packets[i].payload[csum->off] = psum[0];
319 packets[i].payload[csum->off + 1] = psum[1];
323 static struct cpu_stats *setup_shared_var(unsigned long cpus)
325 int fd;
326 char zbuff[cpus * sizeof(struct cpu_stats)], file[256];
327 struct cpu_stats *buff;
329 memset(zbuff, 0, sizeof(zbuff));
330 slprintf(file, sizeof(file), ".tmp_mmap.%u", (unsigned int) rand());
332 fd = creat(file, S_IRUSR | S_IWUSR);
333 bug_on(fd < 0);
334 close(fd);
336 fd = open_or_die_m(file, O_RDWR | O_CREAT | O_TRUNC,
337 S_IRUSR | S_IWUSR);
338 write_or_die(fd, zbuff, sizeof(zbuff));
340 buff = (void *) mmap(0, sizeof(zbuff), PROT_READ | PROT_WRITE,
341 MAP_SHARED, fd, 0);
342 if (buff == (void *) -1)
343 panic("Cannot setup shared variable!\n");
345 close(fd);
346 unlink(file);
348 memset(buff, 0, sizeof(zbuff));
350 return buff;
353 static void destroy_shared_var(void *buff, unsigned long cpus)
355 munmap(buff, cpus * sizeof(struct cpu_stats));
358 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
360 int i;
362 printf("{");
363 for (i = 0; i < len; ++i) {
364 if (i % 15 == 0)
365 printf("\n ");
366 printf("0x%02x, ", payload[i]);
368 printf("\n}\n");
369 fflush(stdout);
372 static inline unsigned short csum(unsigned short *buf, int nwords)
374 unsigned long sum;
376 for (sum = 0; nwords > 0; nwords--)
377 sum += *buf++;
378 sum = (sum >> 16) + (sum & 0xffff);
379 sum += (sum >> 16);
381 return ~sum;
384 static int xmit_smoke_setup(struct ctx *ctx)
386 int icmp_sock, ret, ttl = 64;
387 struct icmp_filter filter;
389 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
390 if (icmp_sock < 0)
391 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
393 filter.data = ~(1 << ICMP_ECHOREPLY);
395 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
396 if (ret < 0)
397 panic("Cannot install filter!\n");
399 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
400 if (ret < 0)
401 panic("Cannot set TTL!\n");
403 memset(&ctx->dest, 0, sizeof(ctx->dest));
404 ctx->dest.sin_family = AF_INET;
405 ctx->dest.sin_port = 0;
407 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
408 if (ret < 0)
409 panic("Cannot resolv address!\n");
411 return icmp_sock;
414 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
416 int ret, i, probes = 5;
417 short ident, cnt = 1;
418 uint8_t outpack[512], *data;
419 struct icmphdr *icmp;
420 struct iphdr *ip;
421 size_t len = sizeof(*icmp) + 56;
422 struct sockaddr_in from;
423 socklen_t from_len;
424 struct pollfd fds = {
425 .fd = icmp_sock,
426 .events = POLLIN,
429 while (probes-- > 0) {
430 ident = htons((short) rand());
432 memset(outpack, 0, sizeof(outpack));
433 icmp = (void *) outpack;
434 icmp->type = ICMP_ECHO;
435 icmp->code = 0;
436 icmp->checksum = 0;
437 icmp->un.echo.id = ident;
438 icmp->un.echo.sequence = htons(cnt++);
440 data = ((uint8_t *) outpack + sizeof(*icmp));
441 for (i = 0; i < 56; ++i)
442 data[i] = (uint8_t) rand();
444 icmp->checksum = csum((unsigned short *) outpack,
445 len / sizeof(unsigned short));
447 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
448 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
449 if (unlikely(ret != len))
450 panic("Cannot send out probe: %s!\n", strerror(errno));
452 ret = poll(&fds, 1, 500);
453 if (ret < 0)
454 panic("Poll failed!\n");
456 if (fds.revents & POLLIN) {
457 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
458 (struct sockaddr *) &from, &from_len);
459 if (unlikely(ret <= 0))
460 panic("Probe receive failed!\n");
461 if (unlikely(from_len != sizeof(ctx->dest)))
462 continue;
463 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
464 continue;
465 if (unlikely(ret < sizeof(*ip) + sizeof(*icmp)))
466 continue;
467 ip = (void *) outpack;
468 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > ret))
469 continue;
470 icmp = (void *) outpack + ip->ihl * 4;
471 if (unlikely(icmp->un.echo.id != ident))
472 continue;
474 return 0;
478 return -1;
481 static void xmit_slowpath_or_die(struct ctx *ctx, int cpu)
483 int ret, icmp_sock = -1;
484 unsigned long num = 1, i = 0;
485 struct timeval start, end, diff;
486 unsigned long long tx_bytes = 0, tx_packets = 0;
487 struct packet_dyn *pktd;
488 struct sockaddr_ll saddr = {
489 .sll_family = PF_PACKET,
490 .sll_halen = ETH_ALEN,
491 .sll_ifindex = device_ifindex(ctx->device),
494 if (ctx->num > 0)
495 num = ctx->num;
497 if (ctx->smoke_test)
498 icmp_sock = xmit_smoke_setup(ctx);
500 bug_on(gettimeofday(&start, NULL));
502 while (likely(sigint == 0) && likely(num > 0)) {
503 pktd = &packet_dyn[i];
504 if (pktd->clen + pktd->rlen + pktd->slen) {
505 apply_counter(i);
506 apply_randomizer(i);
507 apply_csum16(i);
509 retry:
510 ret = sendto(sock, packets[i].payload, packets[i].len, 0,
511 (struct sockaddr *) &saddr, sizeof(saddr));
512 if (unlikely(ret < 0)) {
513 if (errno == ENOBUFS) {
514 sched_yield();
515 goto retry;
518 panic("Sendto error: %s!\n", strerror(errno));
521 tx_bytes += packets[i].len;
522 tx_packets++;
524 if (ctx->smoke_test) {
525 ret = xmit_smoke_probe(icmp_sock, ctx);
526 if (unlikely(ret < 0)) {
527 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
528 printf(" Remote host seems to be unresponsive to ICMP pings!\n");
529 printf(" Last instance was packet%lu, trafgen snippet:\n\n", i);
531 dump_trafgen_snippet(packets[i].payload, packets[i].len);
532 break;
536 if (!ctx->rand) {
537 i++;
538 if (i >= plen)
539 i = 0;
540 } else
541 i = rand() % plen;
543 if (ctx->num > 0)
544 num--;
546 if (ctx->gap > 0)
547 usleep(ctx->gap);
550 bug_on(gettimeofday(&end, NULL));
551 diff = tv_subtract(end, start);
553 if (ctx->smoke_test)
554 close(icmp_sock);
556 stats[cpu].tx_packets = tx_packets;
557 stats[cpu].tx_bytes = tx_bytes;
558 stats[cpu].tv_sec = diff.tv_sec;
559 stats[cpu].tv_usec = diff.tv_usec;
561 stats[cpu].state |= CPU_STATS_STATE_RES;
564 static void xmit_fastpath_or_die(struct ctx *ctx, int cpu)
566 int ifindex = device_ifindex(ctx->device);
567 uint8_t *out = NULL;
568 unsigned int it = 0;
569 unsigned long num = 1, i = 0, size;
570 struct ring tx_ring;
571 struct frame_map *hdr;
572 struct timeval start, end, diff;
573 struct packet_dyn *pktd;
574 unsigned long long tx_bytes = 0, tx_packets = 0;
576 fmemset(&tx_ring, 0, sizeof(tx_ring));
578 size = ring_size(ctx->device, ctx->reserve_size);
580 set_sock_prio(sock, 512);
581 set_packet_loss_discard(sock);
583 setup_tx_ring_layout(sock, &tx_ring, size, ctx->jumbo_support);
584 create_tx_ring(sock, &tx_ring, ctx->verbose);
585 mmap_tx_ring(sock, &tx_ring);
586 alloc_tx_ring_frames(&tx_ring);
587 bind_tx_ring(sock, &tx_ring, ifindex);
589 if (ctx->kpull)
590 interval = ctx->kpull;
591 if (ctx->num > 0)
592 num = ctx->num;
594 itimer.it_interval.tv_sec = 0;
595 itimer.it_interval.tv_usec = interval;
597 itimer.it_value.tv_sec = 0;
598 itimer.it_value.tv_usec = interval;
600 setitimer(ITIMER_REAL, &itimer, NULL);
602 bug_on(gettimeofday(&start, NULL));
604 while (likely(sigint == 0) && likely(num > 0)) {
605 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base) && likely(num > 0)) {
606 hdr = tx_ring.frames[it].iov_base;
608 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
609 * sizeof(struct sockaddr_ll); */
610 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
612 hdr->tp_h.tp_snaplen = packets[i].len;
613 hdr->tp_h.tp_len = packets[i].len;
615 pktd = &packet_dyn[i];
616 if (pktd->clen + pktd->rlen + pktd->slen) {
617 apply_counter(i);
618 apply_randomizer(i);
619 apply_csum16(i);
622 fmemcpy(out, packets[i].payload, packets[i].len);
624 tx_bytes += packets[i].len;
625 tx_packets++;
627 if (!ctx->rand) {
628 i++;
629 if (i >= plen)
630 i = 0;
631 } else
632 i = rand() % plen;
634 kernel_may_pull_from_tx(&hdr->tp_h);
636 it++;
637 if (it >= tx_ring.layout.tp_frame_nr)
638 it = 0;
640 if (ctx->num > 0)
641 num--;
643 if (unlikely(sigint == 1))
644 break;
648 bug_on(gettimeofday(&end, NULL));
649 diff = tv_subtract(end, start);
651 destroy_tx_ring(sock, &tx_ring);
653 stats[cpu].tx_packets = tx_packets;
654 stats[cpu].tx_bytes = tx_bytes;
655 stats[cpu].tv_sec = diff.tv_sec;
656 stats[cpu].tv_usec = diff.tv_usec;
658 stats[cpu].state |= CPU_STATS_STATE_RES;
661 static inline void __set_state(int cpu, sig_atomic_t s)
663 stats[cpu].state = s;
666 static inline sig_atomic_t __get_state(int cpu)
668 return stats[cpu].state;
671 static unsigned long __wait_and_sum_others(struct ctx *ctx, int cpu)
673 int i;
674 unsigned long total;
676 for (i = 0, total = plen; i < ctx->cpus; i++) {
677 if (i == cpu)
678 continue;
680 while ((__get_state(i) & CPU_STATS_STATE_CFG) == 0 &&
681 sigint == 0)
682 sched_yield();
684 total += stats[i].cf_packets;
687 return total;
690 static void __correct_global_delta(struct ctx *ctx, int cpu, unsigned long orig)
692 int i, cpu_sel;
693 unsigned long total;
694 long long delta_correction = 0;
696 for (i = 0, total = ctx->num; i < ctx->cpus; i++) {
697 if (i == cpu)
698 continue;
700 while ((__get_state(i) & CPU_STATS_STATE_CHK) == 0 &&
701 sigint == 0)
702 sched_yield();
704 total += stats[i].cd_packets;
707 if (total > orig)
708 delta_correction = -1 * ((long long) total - orig);
709 if (total < orig)
710 delta_correction = +1 * ((long long) orig - total);
712 for (cpu_sel = -1, i = 0; i < ctx->cpus; i++) {
713 if (stats[i].cd_packets > 0) {
714 if ((long long) stats[i].cd_packets +
715 delta_correction > 0) {
716 cpu_sel = i;
717 break;
722 if (cpu == cpu_sel)
723 ctx->num += delta_correction;
726 static void __set_state_cf(int cpu, unsigned long p, unsigned long b,
727 sig_atomic_t s)
729 stats[cpu].cf_packets = p;
730 stats[cpu].cf_bytes = b;
731 stats[cpu].state = s;
734 static void __set_state_cd(int cpu, unsigned long p, sig_atomic_t s)
736 stats[cpu].cd_packets = p;
737 stats[cpu].state = s;
740 static int xmit_packet_precheck(struct ctx *ctx, int cpu)
742 int i;
743 unsigned long plen_total, orig = ctx->num;
744 size_t mtu, total_len = 0;
746 bug_on(plen != dlen);
748 for (i = 0; i < plen; ++i)
749 total_len += packets[i].len;
751 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
752 plen_total = __wait_and_sum_others(ctx, cpu);
754 if (orig > 0) {
755 ctx->num = (unsigned long) nearbyint((1.0 * plen / plen_total) * orig);
757 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
758 CPU_STATS_STATE_CFG);
759 __correct_global_delta(ctx, cpu, orig);
762 if (plen == 0) {
763 __set_state(cpu, CPU_STATS_STATE_RES);
764 return -1;
767 for (mtu = device_mtu(ctx->device), i = 0; i < plen; ++i) {
768 if (packets[i].len > mtu + 14)
769 panic("Device MTU < than packet%d's size!\n", i);
770 if (packets[i].len <= 14)
771 panic("Packet%d's size too short!\n", i);
774 return 0;
777 static void main_loop(struct ctx *ctx, char *confname, bool slow, int cpu)
779 compile_packets(confname, ctx->verbose, cpu);
780 if (xmit_packet_precheck(ctx, cpu) < 0)
781 return;
783 if (cpu == 0) {
784 int i;
785 size_t total_len = 0, total_pkts = 0;
787 for (i = 0; i < ctx->cpus; ++i) {
788 total_len += stats[i].cf_bytes;
789 total_pkts += stats[i].cf_packets;
792 printf("%6zu packets to schedule\n", total_pkts);
793 printf("%6zu bytes in total\n", total_len);
794 printf("Running! Hang up with ^C!\n\n");
795 fflush(stdout);
798 sock = pf_socket();
800 if (slow)
801 xmit_slowpath_or_die(ctx, cpu);
802 else
803 xmit_fastpath_or_die(ctx, cpu);
805 close(sock);
807 cleanup_packets();
810 int main(int argc, char **argv)
812 bool slow = false;
813 int c, opt_index, i, j, vals[4] = {0}, irq;
814 char *confname = NULL, *ptr;
815 unsigned long cpus_tmp;
816 unsigned long long tx_packets, tx_bytes;
817 struct ctx ctx;
819 setfsuid(getuid());
820 setfsgid(getgid());
822 srand(time(NULL));
823 fmemset(&ctx, 0, sizeof(ctx));
824 ctx.cpus = get_number_cpus_online();
826 while ((c = getopt_long(argc, argv, short_options, long_options,
827 &opt_index)) != EOF) {
828 switch (c) {
829 case 'h':
830 help();
831 break;
832 case 'v':
833 version();
834 break;
835 case 'e':
836 example();
837 break;
838 case 'V':
839 ctx.verbose = true;
840 break;
841 case 'P':
842 cpus_tmp = strtoul(optarg, NULL, 0);
843 if (cpus_tmp > 0 && cpus_tmp < ctx.cpus)
844 ctx.cpus = cpus_tmp;
845 break;
846 case 'd':
847 case 'o':
848 ctx.device = xstrndup(optarg, IFNAMSIZ);
849 break;
850 case 'r':
851 ctx.rand = true;
852 break;
853 case 's':
854 slow = true;
855 ctx.cpus = 1;
856 ctx.smoke_test = true;
857 ctx.rhost = xstrdup(optarg);
858 break;
859 case 'R':
860 ctx.rfraw = true;
861 break;
862 case 'J':
863 ctx.jumbo_support = true;
864 break;
865 case 'c':
866 case 'i':
867 confname = xstrdup(optarg);
868 break;
869 case 'k':
870 ctx.kpull = strtoul(optarg, NULL, 0);
871 break;
872 case 'n':
873 ctx.num = strtoul(optarg, NULL, 0);
874 break;
875 case 't':
876 slow = true;
877 ctx.gap = strtoul(optarg, NULL, 0);
878 if (ctx.gap > 0)
879 /* Fall back to single core to not
880 * mess up correct timing. We are slow
881 * anyway!
883 ctx.cpus = 1;
884 break;
885 case 'S':
886 ptr = optarg;
887 ctx.reserve_size = 0;
889 for (j = i = strlen(optarg); i > 0; --i) {
890 if (!isdigit(optarg[j - i]))
891 break;
892 ptr++;
895 if (!strncmp(ptr, "KB", strlen("KB")))
896 ctx.reserve_size = 1 << 10;
897 else if (!strncmp(ptr, "MB", strlen("MB")))
898 ctx.reserve_size = 1 << 20;
899 else if (!strncmp(ptr, "GB", strlen("GB")))
900 ctx.reserve_size = 1 << 30;
901 else
902 panic("Syntax error in ring size param!\n");
903 *ptr = 0;
905 ctx.reserve_size *= strtol(optarg, NULL, 0);
906 break;
907 case '?':
908 switch (optopt) {
909 case 'd':
910 case 'c':
911 case 'n':
912 case 'S':
913 case 's':
914 case 'P':
915 case 'o':
916 case 'i':
917 case 'k':
918 case 't':
919 panic("Option -%c requires an argument!\n",
920 optopt);
921 default:
922 if (isprint(optopt))
923 whine("Unknown option character "
924 "`0x%X\'!\n", optopt);
925 die();
927 default:
928 break;
932 if (argc < 5)
933 help();
934 if (ctx.device == NULL)
935 panic("No networking device given!\n");
936 if (confname == NULL)
937 panic("No configuration file given!\n");
938 if (device_mtu(ctx.device) == 0)
939 panic("This is no networking device!\n");
940 if (!ctx.rfraw && device_up_and_running(ctx.device) == 0)
941 panic("Networking device not running!\n");
943 register_signal(SIGINT, signal_handler);
944 register_signal(SIGHUP, signal_handler);
945 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
947 header();
949 set_system_socket_memory(vals);
951 if (ctx.rfraw) {
952 ctx.device_trans = xstrdup(ctx.device);
953 xfree(ctx.device);
955 enter_rfmon_mac80211(ctx.device_trans, &ctx.device);
956 sleep(0);
959 irq = device_irq_number(ctx.device);
960 device_reset_irq_affinity(irq);
962 if (ctx.num > 0 && ctx.num <= ctx.cpus)
963 ctx.cpus = 1;
965 stats = setup_shared_var(ctx.cpus);
967 for (i = 0; i < ctx.cpus; i++) {
968 pid_t pid = fork();
970 switch (pid) {
971 case 0:
972 cpu_affinity(i);
973 main_loop(&ctx, confname, slow, i);
975 goto thread_out;
976 case -1:
977 panic("Cannot fork processes!\n");
981 for (i = 0; i < ctx.cpus; i++) {
982 int status;
984 wait(&status);
987 if (ctx.rfraw)
988 leave_rfmon_mac80211(ctx.device_trans, ctx.device);
990 reset_system_socket_memory(vals);
992 for (i = 0, tx_packets = tx_bytes = 0; i < ctx.cpus; i++) {
993 while ((__get_state(i) & CPU_STATS_STATE_RES) == 0)
994 sched_yield();
996 tx_packets += stats[i].tx_packets;
997 tx_bytes += stats[i].tx_bytes;
1000 fflush(stdout);
1001 printf("\n");
1002 printf("\r%12llu packets outgoing\n", tx_packets);
1003 printf("\r%12llu bytes outgoing\n", tx_bytes);
1004 for (i = 0; i < ctx.cpus; i++) {
1005 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1006 stats[i].tv_sec, stats[i].tv_usec, i,
1007 stats[i].tx_packets);
1010 thread_out:
1011 destroy_shared_var(stats, ctx.cpus);
1013 free(ctx.device);
1014 free(ctx.device_trans);
1015 free(ctx.rhost);
1016 free(confname);
1018 return 0;