mausezahn: use getopt_long instead of getopt
[netsniff-ng.git] / trafgen.c
blob569cf0634fcb93e2e3cd0315324d8831379dd8c3
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 "ioops.h"
49 #include "irq.h"
50 #include "config.h"
51 #include "built_in.h"
52 #include "trafgen_conf.h"
53 #include "tprintf.h"
54 #include "timer.h"
55 #include "ring_tx.h"
56 #include "csum.h"
57 #include "trafgen_proto.h"
58 #include "pcap_io.h"
59 #include "trafgen_dev.h"
61 enum shaper_type {
62 SHAPER_NONE,
63 SHAPER_DELAY,
64 SHAPER_PKTS,
65 SHAPER_BYTES,
66 SHAPER_TSTAMP,
69 struct shaper {
70 enum shaper_type type;
71 unsigned long long sent;
72 unsigned long long rate;
73 struct timeval tstamp;
74 struct timespec delay;
75 struct timeval start;
76 struct timeval end;
79 struct ctx {
80 bool rand, rfraw, jumbo_support, verbose, smoke_test, enforce, qdisc_path;
81 size_t reserve_size;
82 struct dev_io *dev_out;
83 struct dev_io *dev_in;
84 unsigned long num;
85 unsigned int cpu_start;
86 unsigned int cpu_num;
87 uid_t uid; gid_t gid;
88 char *device, *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 =
145 "Please report bugs at https://github.com/netsniff-ng/netsniff-ng/issues\n"
146 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
147 "Swiss federal institute of technology (ETH Zurich)\n"
148 "License: GNU GPL version 2.0\n"
149 "This is free software: you are free to change and redistribute it.\n"
150 "There is NO WARRANTY, to the extent permitted by law.";
152 static struct cpu_stats *stats;
153 static unsigned int seed;
155 #define CPU_STATS_STATE_CFG 1
156 #define CPU_STATS_STATE_CHK 2
157 #define CPU_STATS_STATE_RES 4
159 #ifndef ICMP_FILTER
160 # define ICMP_FILTER 1
162 struct icmp_filter {
163 __u32 data;
165 #endif
167 #define SMOKE_N_PROBES 100
169 #define PKT_MIN_LEN 14
171 static void signal_handler(int number)
173 switch (number) {
174 case SIGINT:
175 case SIGQUIT:
176 case SIGTERM:
177 sigint = 1;
178 case SIGHUP:
179 default:
180 break;
184 static void __noreturn help(void)
186 printf("trafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING);
187 puts("http://www.netsniff-ng.org\n\n"
188 "Usage: trafgen [options] [packet]\n"
189 "Options:\n"
190 " -i|-c|--in|--conf <cfg/-> Packet configuration file/stdin\n"
191 " -o|-d|--out|--dev <netdev|.cfg|.pcap> Networking device or configuration file i.e., eth0\n"
192 " -p|--cpp Run packet config through C preprocessor\n"
193 " -D|--define Add macro/define for C preprocessor\n"
194 " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n"
195 " -R|--rfraw Inject raw 802.11 frames\n"
196 " -s|--smoke-test <ipv4> Probe if machine survived fuzz-tested packet\n"
197 " -n|--num <uint> Number of packets until exit (def: 0)\n"
198 " -r|--rand Randomize packet selection (def: round robin)\n"
199 " -P|--cpus <uint>[-<uint>] Specify number of forks(<= CPUs) (def: #CPUs)\n"
200 " -t|--gap <time> Set approx. interpacket gap (s/ms/us/ns, def: us)\n"
201 " -b|--rate <rate> Send traffic at specified rate (pps/kpps/Mpps/B/kB/MB/GB/kbit/Mbit/Gbit/KiB/MiB/GiB)\n"
202 " -S|--ring-size <size> Manually set mmap size (KiB/MiB/GiB)\n"
203 " -E|--seed <uint> Manually set srand(3) seed\n"
204 " -u|--user <userid> Drop privileges and change to userid\n"
205 " -g|--group <groupid> Drop privileges and change to groupid\n"
206 " -H|--prio-high Make this high priority process\n"
207 " -A|--no-sock-mem Don't tune core socket memory\n"
208 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
209 " -q|--qdisc-path Enable qdisc kernel path (default off since 3.14)\n"
210 " -V|--verbose Be more verbose\n"
211 " -C|--no-cpu-stats Do not print CPU time statistics on exit\n"
212 " -v|--version Show version and exit\n"
213 " -e|--example Show built-in packet config example\n"
214 " -h|--help Guess what?!\n\n"
215 "Examples:\n"
216 " trafgen --dev eth0 --conf trafgen.cfg\n"
217 " trafgen --dev eth0 --conf trafgen.cfg --cpus 2-4\n"
218 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
219 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
220 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
221 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
222 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
223 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n"
224 " trafgen --dev eth0 '{ fill(0xff, 6), 0x00, 0x02, 0xb3, rnd(3), c16(0x0800), fill(0xca, 64) }'\n\n"
225 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
226 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
227 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
228 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
229 "Generate config files from existing pcap using netsniff-ng:\n"
230 " netsniff-ng --in dump.pcap --out dump.cfg\n\n"
231 "Note:\n"
232 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
233 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
234 " we assume the kernel crashed, thus we print the packet and quit.\n"
235 " In case you find a ping-of-death, please mention trafgen in your\n"
236 " commit message of the fix!\n\n"
237 " For introducing bit errors, delays with random variation and more,\n"
238 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
239 " For generating different package distributions, you can use scripting\n"
240 " to generate a trafgen config file with packet ratios as:\n\n"
241 " IMIX 64:7, 570:4, 1518:1\n"
242 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
243 " Cisco 64:7, 594:4, 1518:1\n"
244 " RPR Trimodal 64:60, 512:20, 1518:20\n"
245 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n");
246 puts(copyright);
247 die();
250 static void __noreturn example(void)
252 const char *e =
253 "/* Note: dynamic elements make trafgen slower! */\n"
254 "#include <stddef.h>\n\n"
255 "{\n"
256 " /* MAC Destination */\n"
257 " fill(0xff, ETH_ALEN),\n"
258 " /* MAC Source */\n"
259 " 0x00, 0x02, 0xb3, drnd(3),\n"
260 " /* IPv4 Protocol */\n"
261 " c16(ETH_P_IP),\n"
262 " /* IPv4 Version, IHL, TOS */\n"
263 " 0b01000101, 0,\n"
264 " /* IPv4 Total Len */\n"
265 " c16(59),\n"
266 " /* IPv4 Ident */\n"
267 " drnd(2),\n"
268 " /* IPv4 Flags, Frag Off */\n"
269 " 0b01000000, 0,\n"
270 " /* IPv4 TTL */\n"
271 " 64,\n"
272 " /* Proto TCP */\n"
273 " 0x06,\n"
274 " /* IPv4 Checksum (IP header from, to) */\n"
275 " csumip(14, 33),\n"
276 " /* Source IP */\n"
277 " drnd(4),\n"
278 " /* Dest IP */\n"
279 " drnd(4),\n"
280 " /* TCP Source Port */\n"
281 " drnd(2),\n"
282 " /* TCP Dest Port */\n"
283 " c16(80),\n"
284 " /* TCP Sequence Number */\n"
285 " drnd(4),\n"
286 " /* TCP Ackn. Number */\n"
287 " c32(0),\n"
288 " /* TCP Header length + TCP SYN/ECN Flag */\n"
289 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
290 " /* Window Size */\n"
291 " c16(16),\n"
292 " /* TCP Checksum (offset IP, offset TCP) */\n"
293 " csumtcp(14, 34),\n"
294 " /* TCP Options */\n"
295 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
296 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
297 " /* Data blob */\n"
298 " \"gotcha!\",\n"
299 "}";
300 puts(e);
301 die();
304 static void __noreturn version(void)
306 printf("trafgen %s, Git id: %s\n", VERSION_LONG, GITVERSION);
307 puts("multithreaded zero-copy network packet generator\n"
308 "http://www.netsniff-ng.org\n");
309 puts(copyright);
310 die();
313 static void apply_counter(int id)
315 size_t j, counter_max = packet_dyn[id].clen;
317 for (j = 0; j < counter_max; ++j) {
318 uint8_t val;
319 struct counter *counter = &packet_dyn[id].cnt[j];
321 val = counter->val - counter->min;
322 val = (val + counter->inc) % (counter->max - counter->min + 1);
324 counter->val = val + counter->min;
325 packets[id].payload[counter->off] = counter->val;
329 static void apply_randomizer(int id)
331 size_t j, rand_max = packet_dyn[id].rlen;
333 for (j = 0; j < rand_max; ++j) {
334 uint8_t val = (uint8_t) rand();
335 struct randomizer *randomizer = &packet_dyn[id].rnd[j];
337 packets[id].payload[randomizer->off] = val;
341 static void apply_csum16(int id)
343 size_t j, csum_max = packet_dyn[id].slen;
345 for (j = 0; j < csum_max; ++j) {
346 uint16_t sum = 0;
347 struct csum16 *csum = &packet_dyn[id].csum[j];
349 memset(&packets[id].payload[csum->off], 0, sizeof(sum));
350 if (unlikely((size_t) csum->to >= packets[id].len))
351 csum->to = packets[id].len - 1;
353 switch (csum->which) {
354 case CSUM_IP:
355 sum = calc_csum(packets[id].payload + csum->from,
356 csum->to - csum->from + 1);
357 break;
358 case CSUM_UDP:
359 sum = p4_csum((void *) packets[id].payload + csum->from,
360 packets[id].payload + csum->to,
361 (packets[id].len - csum->to),
362 IPPROTO_UDP);
363 break;
364 case CSUM_TCP:
365 sum = p4_csum((void *) packets[id].payload + csum->from,
366 packets[id].payload + csum->to,
367 (packets[id].len - csum->to),
368 IPPROTO_TCP);
369 break;
370 case CSUM_UDP6:
371 sum = p6_csum((void *) packets[id].payload + csum->from,
372 packets[id].payload + csum->to,
373 (packets[id].len - csum->to),
374 IPPROTO_UDP);
375 break;
376 case CSUM_TCP6:
377 sum = p6_csum((void *) packets[id].payload + csum->from,
378 packets[id].payload + csum->to,
379 (packets[id].len - csum->to),
380 IPPROTO_TCP);
381 break;
382 case CSUM_ICMP6:
383 sum = p6_csum((void *) packets[id].payload + csum->from,
384 packets[id].payload + csum->to,
385 (packets[id].len - csum->to),
386 IPPROTO_ICMPV6);
387 break;
388 default:
389 bug();
390 break;
393 memcpy(&packets[id].payload[csum->off], &sum, sizeof(sum));
397 static void preprocess_packets(void)
399 size_t i;
401 for (i = 0; i < plen; i++) {
402 struct packet_dyn *pktd = &packet_dyn[i];
404 if (packet_dyn_has_only_csums(pktd)) {
405 apply_csum16(i);
406 pktd->slen = 0;
407 xfree(pktd->csum);
412 static struct cpu_stats *setup_shared_var(unsigned int cpus)
414 int fd;
415 size_t len = cpus * sizeof(struct cpu_stats);
416 char *zbuff, file[256];
417 struct cpu_stats *buff;
419 slprintf(file, sizeof(file), ".tmp_mmap.XXXXXX");
420 fd = mkostemp_or_die(file, O_RDWR | O_CREAT | O_TRUNC);
421 zbuff = xzmalloc(len);
422 write_or_die(fd, zbuff, len);
423 xfree(zbuff);
425 buff = mmap(NULL, len, PROT_READ | PROT_WRITE,
426 MAP_SHARED, fd, 0);
427 if (buff == MAP_FAILED)
428 panic("Cannot setup shared variable!\n");
430 close(fd);
431 unlink(file);
433 memset(buff, 0, len);
434 return buff;
437 static void destroy_shared_var(void *buff, unsigned int cpus)
439 munmap(buff, cpus * sizeof(struct cpu_stats));
442 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
444 size_t i;
446 printf("{");
447 for (i = 0; i < len; ++i) {
448 if (i % 15 == 0)
449 printf("\n ");
450 printf("0x%02x, ", payload[i]);
452 printf("\n}\n");
453 fflush(stdout);
456 static int xmit_smoke_setup(struct ctx *ctx)
458 int icmp_sock, ret, ttl = 64;
459 struct icmp_filter filter;
461 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
462 if (icmp_sock < 0)
463 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
465 filter.data = ~(1 << ICMP_ECHOREPLY);
467 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
468 if (ret < 0)
469 panic("Cannot install filter!\n");
471 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
472 if (ret < 0)
473 panic("Cannot set TTL!\n");
475 memset(&ctx->dest, 0, sizeof(ctx->dest));
476 ctx->dest.sin_family = AF_INET;
477 ctx->dest.sin_port = 0;
479 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
480 if (ret < 0)
481 panic("Cannot resolve address!\n");
483 return icmp_sock;
486 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
488 int ret;
489 unsigned int i, j;
490 short ident, cnt = 1, idstore[SMOKE_N_PROBES];
491 uint8_t outpack[512], *data;
492 struct icmphdr *icmp;
493 struct iphdr *ip;
494 size_t len = sizeof(*icmp) + 56;
495 struct sockaddr_in from;
496 socklen_t from_len;
497 struct pollfd fds = {
498 .fd = icmp_sock,
499 .events = POLLIN,
502 memset(idstore, 0, sizeof(idstore));
503 for (j = 0; j < SMOKE_N_PROBES; j++) {
504 while ((ident = htons((short) rand())) == 0)
505 sleep(0);
506 idstore[j] = ident;
508 memset(outpack, 0, sizeof(outpack));
509 icmp = (void *) outpack;
510 icmp->type = ICMP_ECHO;
511 icmp->un.echo.id = ident;
512 icmp->un.echo.sequence = htons(cnt++);
514 data = ((uint8_t *) outpack + sizeof(*icmp));
515 for (i = 0; i < 56; ++i)
516 data[i] = (uint8_t) rand();
518 icmp->checksum = csum((unsigned short *) outpack,
519 len / sizeof(unsigned short));
521 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
522 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
523 if (unlikely(ret != (int) len))
524 panic("Cannot send out probe: %s!\n", strerror(errno));
526 ret = poll(&fds, 1, 50);
527 if (ret < 0)
528 panic("Poll failed!\n");
530 if (fds.revents & POLLIN) {
531 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
532 (struct sockaddr *) &from, &from_len);
533 if (unlikely(ret <= 0))
534 panic("Probe receive failed!\n");
535 if (unlikely(from_len != sizeof(ctx->dest)))
536 continue;
537 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
538 continue;
539 if (unlikely((size_t) ret < sizeof(*ip) + sizeof(*icmp)))
540 continue;
541 ip = (void *) outpack;
542 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > (size_t) ret))
543 continue;
544 icmp = (void *) outpack + ip->ihl * 4;
545 for (i = 0; i < array_size(idstore); ++i) {
546 if (unlikely(icmp->un.echo.id != idstore[i]))
547 continue;
548 return 0;
553 return -1;
556 static bool shaper_is_set(struct shaper *sh)
558 return sh->type != SHAPER_NONE;
561 static void shaper_init(struct shaper *sh)
563 if (sh->type == SHAPER_NONE || sh->type == SHAPER_DELAY)
564 return;
566 memset(&sh->delay, 0, sizeof(struct timespec));
567 bug_on(gettimeofday(&sh->start, NULL));
568 sh->sent = 0;
571 static void shaper_set_delay(struct shaper *sh, time_t sec, long int ns)
573 sh->type = SHAPER_DELAY;
574 sh->delay.tv_sec = sec;
575 sh->delay.tv_nsec = ns;
578 static void shaper_set_rate(struct shaper *sh, unsigned long long rate,
579 enum shaper_type type)
581 memset(sh, 0, sizeof(struct shaper));
582 sh->rate = rate;
583 sh->type = type;
586 static void shaper_set_tstamp(struct shaper *sh, struct timespec *ts)
588 TIMESPEC_TO_TIMEVAL(&sh->tstamp, ts);
591 static void shaper_delay(struct shaper *sh, struct packet *pkt)
593 if (sh->type == SHAPER_BYTES || sh->type == SHAPER_PKTS) {
594 unsigned long pkt_len = pkt->len;
596 sh->sent += sh->type == SHAPER_BYTES ? pkt_len : 1;
598 if (sh->sent >= sh->rate && sh->rate > 0) {
599 struct timeval delay_us;
600 struct timeval time_sent;
601 struct timeval time_1s = { .tv_sec = 1 };
603 bug_on(gettimeofday(&sh->end, NULL));
604 timersub(&sh->end, &sh->start, &time_sent);
606 if (timercmp(&time_1s, &time_sent, > )) {
607 timersub(&time_1s, &time_sent, &delay_us);
608 TIMEVAL_TO_TIMESPEC(&delay_us, &sh->delay);
611 } else if (sh->type == SHAPER_TSTAMP) {
612 struct timeval tstamp;
613 struct timeval pkt_diff;
614 struct timeval diff;
616 bug_on(gettimeofday(&sh->end, NULL));
617 TIMESPEC_TO_TIMEVAL(&tstamp, &pkt->tstamp);
618 timersub(&sh->end, &sh->start, &diff);
619 timersub(&tstamp, &sh->tstamp, &pkt_diff);
621 if (timercmp(&diff, &pkt_diff, <)) {
622 struct timeval delay;
624 timersub(&pkt_diff, &diff, &delay);
625 TIMEVAL_TO_TIMESPEC(&delay, &sh->delay);
628 memcpy(&sh->tstamp, &tstamp, sizeof(sh->tstamp));
631 if ((sh->delay.tv_sec | sh->delay.tv_nsec) > 0) {
632 nanosleep(&sh->delay, NULL);
634 shaper_init(sh);
638 static inline void packet_apply_dyn_elements(int idx)
640 if (packet_dyn_has_elems(&packet_dyn[idx])) {
641 apply_counter(idx);
642 apply_randomizer(idx);
643 apply_csum16(idx);
646 if (packet_dyn_has_fields(&packet_dyn[idx])) {
647 uint32_t i;
649 for (i = 0; i < packet_dyn[idx].flen; i++)
650 proto_field_dyn_apply(packet_dyn[idx].fields[i]);
652 proto_packet_update(idx);
656 static void xmit_slowpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned long orig_num)
658 int ret, icmp_sock = -1;
659 unsigned long num = 1, i = 0;
660 struct timeval start, end, diff;
661 unsigned long long tx_bytes = 0, tx_packets = 0;
663 if (ctx->num > 0)
664 num = ctx->num;
665 if (ctx->num == 0 && orig_num > 0)
666 num = 0;
668 if (ctx->smoke_test)
669 icmp_sock = xmit_smoke_setup(ctx);
671 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
673 bug_on(gettimeofday(&start, NULL));
675 if (shaper_is_set(&ctx->sh))
676 shaper_init(&ctx->sh);
678 while (likely(sigint == 0 && num > 0 && plen > 0)) {
679 packet_apply_dyn_elements(i);
680 retry:
681 ret = dev_io_write(ctx->dev_out, &packets[i]);
682 if (unlikely(ret < 0)) {
683 if (errno == ENOBUFS) {
684 sched_yield();
685 goto retry;
687 if (ctx->smoke_test)
688 panic("Sendto error: %s!\n", strerror(errno));
691 tx_bytes += packets[i].len;
692 tx_packets++;
694 if (ctx->smoke_test) {
695 ret = xmit_smoke_probe(icmp_sock, ctx);
696 if (unlikely(ret < 0)) {
697 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
698 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
699 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
700 i, seed);
702 dump_trafgen_snippet(packets[i].payload, packets[i].len);
703 break;
707 if (!ctx->rand) {
708 i++;
709 if (i >= plen)
710 i = 0;
711 } else
712 i = rand() % plen;
714 if (ctx->num > 0)
715 num--;
717 if (shaper_is_set(&ctx->sh))
718 shaper_delay(&ctx->sh, &packets[i]);
721 bug_on(gettimeofday(&end, NULL));
722 timersub(&end, &start, &diff);
724 if (ctx->smoke_test)
725 close(icmp_sock);
727 stats[cpu].tx_packets = tx_packets;
728 stats[cpu].tx_bytes = tx_bytes;
729 stats[cpu].tv_sec = diff.tv_sec;
730 stats[cpu].tv_usec = diff.tv_usec;
732 stats[cpu].state |= CPU_STATS_STATE_RES;
735 static void xmit_fastpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned long orig_num)
737 int ifindex = dev_io_ifindex_get(ctx->dev_out);
738 uint8_t *out = NULL;
739 unsigned int it = 0, retry = 100;
740 unsigned long num = 1, i = 0;
741 size_t size = ring_size(dev_io_name_get(ctx->dev_out), ctx->reserve_size);
742 struct ring tx_ring;
743 struct frame_map *hdr;
744 struct timeval start, end, diff;
745 unsigned long long tx_bytes = 0, tx_packets = 0;
746 int sock = dev_io_fd_get(ctx->dev_out);
748 set_sock_prio(sock, 512);
750 ring_tx_setup(&tx_ring, sock, size, ifindex, ctx->jumbo_support, ctx->verbose);
752 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
754 if (ctx->num > 0)
755 num = ctx->num;
756 if (ctx->num == 0 && orig_num > 0)
757 num = 0;
759 bug_on(gettimeofday(&start, NULL));
761 while (likely(sigint == 0 && num > 0 && plen > 0)) {
762 if (!user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
763 int ret = pull_and_flush_tx_ring(sock);
764 if (unlikely(ret < 0)) {
765 /* We could hit EBADF if the socket has been closed before
766 * the timer was triggered.
768 if (errno != EBADF && errno != ENOBUFS)
769 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
772 continue;
775 hdr = tx_ring.frames[it].iov_base;
776 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
778 hdr->tp_h.tp_snaplen = packets[i].len;
779 hdr->tp_h.tp_len = packets[i].len;
781 packet_apply_dyn_elements(i);
783 memcpy(out, packets[i].payload, packets[i].len);
785 tx_bytes += packets[i].len;
786 tx_packets++;
788 if (!ctx->rand) {
789 i++;
790 if (i >= plen)
791 i = 0;
792 } else
793 i = rand() % plen;
795 kernel_may_pull_from_tx(&hdr->tp_h);
797 it++;
798 if (it >= tx_ring.layout.tp_frame_nr)
799 it = 0;
801 if (ctx->num > 0)
802 num--;
805 bug_on(gettimeofday(&end, NULL));
806 timersub(&end, &start, &diff);
808 while (pull_and_flush_tx_ring_wait(sock) < 0 && errno == ENOBUFS && retry-- > 0)
809 usleep(10000);
810 destroy_tx_ring(sock, &tx_ring);
812 stats[cpu].tx_packets = tx_packets;
813 stats[cpu].tx_bytes = tx_bytes;
814 stats[cpu].tv_sec = diff.tv_sec;
815 stats[cpu].tv_usec = diff.tv_usec;
817 stats[cpu].state |= CPU_STATS_STATE_RES;
820 static inline void __set_state(unsigned int cpu, sig_atomic_t s)
822 stats[cpu].state = s;
825 static inline sig_atomic_t __get_state(unsigned int cpu)
827 return stats[cpu].state;
830 static unsigned long __wait_and_sum_others(struct ctx *ctx, unsigned int cpu)
832 unsigned int i;
833 unsigned long total;
835 for (i = 0, total = plen; i < ctx->cpu_num; i++) {
836 if (i == cpu)
837 continue;
839 while ((__get_state(i) &
840 (CPU_STATS_STATE_CFG |
841 CPU_STATS_STATE_RES)) == 0 &&
842 sigint == 0)
843 sched_yield();
845 total += stats[i].cf_packets;
848 return total;
851 static void __correct_global_delta(struct ctx *ctx, unsigned int cpu, unsigned long orig)
853 unsigned int i;
854 unsigned long total;
855 int cpu_sel;
856 long long delta_correction = 0;
858 for (i = 0, total = ctx->num; i < ctx->cpu_num; i++) {
859 if (i == cpu)
860 continue;
862 while ((__get_state(i) &
863 (CPU_STATS_STATE_CHK |
864 CPU_STATS_STATE_RES)) == 0 &&
865 sigint == 0)
866 sched_yield();
868 total += stats[i].cd_packets;
871 if (total > orig)
872 delta_correction = -1 * ((long long) total - orig);
873 if (total < orig)
874 delta_correction = +1 * ((long long) orig - total);
876 for (cpu_sel = -1, i = 0; i < ctx->cpu_num; i++) {
877 if (stats[i].cd_packets > 0) {
878 if ((long long) stats[i].cd_packets +
879 delta_correction >= 0) {
880 cpu_sel = i;
881 break;
886 if ((int) cpu == cpu_sel)
887 ctx->num += delta_correction;
890 static void __set_state_cf(unsigned int cpu, unsigned long p, unsigned long b,
891 sig_atomic_t s)
893 stats[cpu].cf_packets = p;
894 stats[cpu].cf_bytes = b;
895 stats[cpu].state = s;
898 static void __set_state_cd(unsigned int cpu, unsigned long p, sig_atomic_t s)
900 stats[cpu].cd_packets = p;
901 stats[cpu].state = s;
904 static void xmit_packet_precheck(struct ctx *ctx, unsigned int cpu)
906 unsigned long plen_total, orig = ctx->num;
907 size_t total_len = 0;
908 unsigned int i;
910 bug_on(plen != dlen);
912 for (i = 0; i < plen; ++i)
913 total_len += packets[i].len;
915 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
916 plen_total = __wait_and_sum_others(ctx, cpu);
918 if (orig > 0) {
919 ctx->num = (unsigned long) round((1.0 * plen / plen_total) * orig);
921 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
922 CPU_STATS_STATE_CFG);
923 __correct_global_delta(ctx, cpu, orig);
926 if (plen == 0) {
927 __set_state(cpu, CPU_STATS_STATE_RES);
928 return;
932 static void pcap_load_packets(struct dev_io *dev)
934 while (dev_io_read(dev))
935 /* nothing to do */;
938 static void main_loop(struct ctx *ctx, char *confname, bool slow,
939 unsigned int cpu, bool invoke_cpp, char **cpp_argv,
940 unsigned long orig_num)
942 if (ctx->dev_in && dev_io_is_pcap(ctx->dev_in)) {
943 pcap_load_packets(ctx->dev_in);
944 shaper_set_tstamp(&ctx->sh, &packets[0].tstamp);
945 ctx->num = plen;
946 } else {
947 if (ctx->packet_str)
948 compile_packets_str(ctx->packet_str, ctx->verbose, cpu);
949 else
950 compile_packets(confname, ctx->verbose, cpu, invoke_cpp, cpp_argv);
952 preprocess_packets();
955 xmit_packet_precheck(ctx, cpu);
957 if (cpu == 0) {
958 unsigned int i;
959 size_t total_len = 0, total_pkts = 0;
961 for (i = 0; i < ctx->cpu_num; ++i) {
962 total_len += stats[i].cf_bytes;
963 total_pkts += stats[i].cf_packets;
966 printf("%6zu packets to schedule\n", total_pkts);
967 printf("%6zu bytes in total\n", total_len);
968 printf("Running! Hang up with ^C!\n\n");
969 fflush(stdout);
972 dev_io_open(ctx->dev_out);
973 if (dev_io_is_netdev(ctx->dev_out) && ctx->qdisc_path == false)
974 set_sock_qdisc_bypass(dev_io_fd_get(ctx->dev_out), ctx->verbose);
976 if (slow)
977 xmit_slowpath_or_die(ctx, cpu, orig_num);
978 else
979 xmit_fastpath_or_die(ctx, cpu, orig_num);
981 dev_io_close(ctx->dev_out);
982 if (ctx->dev_in)
983 dev_io_close(ctx->dev_in);
985 cleanup_packets();
988 static unsigned int generate_srand_seed(void)
990 int fd;
991 unsigned int _seed;
993 fd = open("/dev/urandom", O_RDONLY);
994 if (fd < 0)
995 return time(NULL);
997 read_or_die(fd, &_seed, sizeof(_seed));
999 close(fd);
1000 return _seed;
1003 static void on_panic_del_rfmon(void *arg)
1005 dev_io_close(arg);
1008 int main(int argc, char **argv)
1010 bool slow = false, invoke_cpp = false, reseed = true, cpustats = true;
1011 bool prio_high = false, set_irq_aff = true, set_sock_mem = true;
1012 int c, vals[4] = {0}, irq;
1013 uint64_t gap = 0;
1014 unsigned int i;
1015 char *confname = NULL, *ptr;
1016 unsigned long cpu_n, orig_num = 0;
1017 unsigned long long tx_packets, tx_bytes;
1018 struct ctx ctx;
1019 int min_opts = 5;
1020 char **cpp_argv = NULL;
1021 size_t cpp_argc = 0;
1022 unsigned long long rate;
1023 enum shaper_type shape_type;
1024 struct timespec delay;
1026 memset(&ctx, 0, sizeof(ctx));
1027 ctx.cpu_num = get_number_cpus_online();
1028 ctx.uid = getuid();
1029 ctx.gid = getgid();
1030 ctx.qdisc_path = false;
1032 /* Keep an initial small default size to reduce cache-misses. */
1033 ctx.reserve_size = 512 * (1 << 10);
1035 while ((c = getopt_long(argc, argv, short_options, long_options,
1036 NULL)) != EOF) {
1037 switch (c) {
1038 case 'h':
1039 help();
1040 break;
1041 case 'v':
1042 version();
1043 break;
1044 case 'C':
1045 cpustats = false;
1046 break;
1047 case 'e':
1048 example();
1049 break;
1050 case 'p':
1051 invoke_cpp = true;
1052 break;
1053 case 'D':
1054 cpp_argv = argv_insert(cpp_argv, &cpp_argc, "-D");
1055 cpp_argv = argv_insert(cpp_argv, &cpp_argc, optarg);
1056 break;
1057 case 'V':
1058 ctx.verbose = true;
1059 break;
1060 case 'P':
1061 if (slow)
1062 break;
1063 cpu_n = strtoul(optarg, &ptr, 0);
1064 if (ptr && *ptr == '-') {
1065 if (cpu_n < 0 || cpu_n >= ctx.cpu_num)
1066 break;
1067 ctx.cpu_start = cpu_n;
1068 cpu_n = strtoul(ptr + 1, NULL, 0);
1069 if (cpu_n < ctx.cpu_start || cpu_n >= ctx.cpu_num)
1070 ctx.cpu_num -= ctx.cpu_start;
1071 else
1072 ctx.cpu_num = cpu_n - ctx.cpu_start + 1;
1073 } else if (cpu_n > 0 && cpu_n <= ctx.cpu_num)
1074 ctx.cpu_num = cpu_n;
1075 break;
1076 case 'd':
1077 case 'o':
1078 ctx.device = xstrdup(optarg);
1079 break;
1080 case 'H':
1081 prio_high = true;
1082 break;
1083 case 'A':
1084 set_sock_mem = false;
1085 break;
1086 case 'Q':
1087 set_irq_aff = false;
1088 break;
1089 case 'q':
1090 ctx.qdisc_path = true;
1091 break;
1092 case 'r':
1093 ctx.rand = true;
1094 break;
1095 case 's':
1096 slow = true;
1097 ctx.cpu_start = 0;
1098 ctx.cpu_num = 1;
1099 ctx.smoke_test = true;
1100 ctx.rhost = xstrdup(optarg);
1101 break;
1102 case 'R':
1103 ctx.rfraw = true;
1104 break;
1105 case 'J':
1106 ctx.jumbo_support = true;
1107 break;
1108 case 'c':
1109 case 'i':
1110 confname = xstrdup(optarg);
1111 if (c == 'i' && strstr(confname, ".pcap")) {
1112 ctx.sh.type = SHAPER_TSTAMP;
1113 ctx.pcap_in = confname;
1114 } else if (!strncmp("-", confname, strlen("-"))) {
1115 ctx.cpu_start = 0;
1116 ctx.cpu_num = 1;
1118 break;
1119 case 'u':
1120 ctx.uid = strtoul(optarg, NULL, 0);
1121 ctx.enforce = true;
1122 break;
1123 case 'g':
1124 ctx.gid = strtoul(optarg, NULL, 0);
1125 ctx.enforce = true;
1126 break;
1127 case 'k':
1128 printf("Option -k/--kernel-pull is no longer used and "
1129 "will be removed in a future release!\n");
1130 break;
1131 case 'E':
1132 seed = strtoul(optarg, NULL, 0);
1133 reseed = false;
1134 break;
1135 case 'n':
1136 orig_num = strtoul(optarg, NULL, 0);
1137 ctx.num = orig_num;
1138 break;
1139 case 't':
1140 gap = strtoul(optarg, &ptr, 0);
1141 if (!gap && optarg == ptr)
1142 panic("Invalid gap param\n");
1144 if (!strncmp(ptr, "ns", strlen("ns"))) {
1145 delay.tv_sec = gap / 1000000000;
1146 delay.tv_nsec = gap % 1000000000;
1147 } else if (*ptr == '\0' || !strncmp(ptr, "us", strlen("us"))) {
1148 /* Default to microseconds for backwards
1149 * compatibility if no postfix is given.
1151 delay.tv_sec = gap / 1000000;
1152 delay.tv_nsec = (gap % 1000000) * 1000;
1153 } else if (!strncmp(ptr, "ms", strlen("ms"))) {
1154 delay.tv_sec = gap / 1000;
1155 delay.tv_nsec = (gap % 1000) * 1000000;
1156 } else if (!strncmp(ptr, "s", strlen("s"))) {
1157 delay.tv_sec = gap;
1158 delay.tv_nsec = 0;
1159 } else {
1160 panic("Syntax error in time param!\n");
1163 shaper_set_delay(&ctx.sh, delay.tv_sec, delay.tv_nsec);
1164 break;
1165 case 'b':
1166 rate = strtoul(optarg, &ptr, 0);
1167 if (!rate && optarg == ptr)
1168 panic("Invalid rate param\n");
1170 if (strncmp(ptr, "pps", strlen("pps")) == 0) {
1171 shape_type = SHAPER_PKTS;
1172 } else if (strncmp(ptr, "kpps", strlen("kpps")) == 0) {
1173 shape_type = SHAPER_PKTS;
1174 rate *= 1000;
1175 } else if (strncmp(ptr, "Mpps", strlen("Mpps")) == 0) {
1176 shape_type = SHAPER_PKTS;
1177 rate *= 1000 * 1000;
1178 } else if (strncmp(ptr, "B", strlen("B")) == 0) {
1179 shape_type = SHAPER_BYTES;
1180 } else if (strncmp(ptr, "kB", strlen("kB")) == 0) {
1181 shape_type = SHAPER_BYTES;
1182 rate *= 1000;
1183 } else if (strncmp(ptr, "MB", strlen("MB")) == 0) {
1184 shape_type = SHAPER_BYTES;
1185 rate *= 1000 * 1000;
1186 } else if (strncmp(ptr, "GB", strlen("GB")) == 0) {
1187 shape_type = SHAPER_BYTES;
1188 rate *= 1000 * 1000 * 1000;
1189 } else if (strncmp(ptr, "kbit", strlen("kbit")) == 0) {
1190 shape_type = SHAPER_BYTES;
1191 rate *= 1000 / 8;
1192 } else if (strncmp(ptr, "Mbit", strlen("Mbit")) == 0) {
1193 shape_type = SHAPER_BYTES;
1194 rate *= 1000 * 1000 / 8;
1195 } else if (strncmp(ptr, "Gbit", strlen("Gbit")) == 0) {
1196 shape_type = SHAPER_BYTES;
1197 rate *= 1000 * 1000 * 1000 / 8;
1198 } else if (strncmp(ptr, "KiB", strlen("KiB")) == 0) {
1199 shape_type = SHAPER_BYTES;
1200 rate *= 1 << 10;
1201 } else if (strncmp(ptr, "MiB", strlen("MiB")) == 0) {
1202 shape_type = SHAPER_BYTES;
1203 rate *= 1 << 20;
1204 } else if (strncmp(ptr, "GiB", strlen("GiB")) == 0) {
1205 shape_type = SHAPER_BYTES;
1206 rate *= 1 << 30;
1207 } else if (!rate) {
1208 shape_type = SHAPER_NONE;
1209 } else {
1210 panic("Invalid unit type for rate\n");
1213 shaper_set_rate(&ctx.sh, rate, shape_type);
1214 break;
1215 case 'S':
1216 ctx.reserve_size = strtoul(optarg, &ptr, 0);
1217 if (ctx.reserve_size == 0 && ptr == optarg)
1218 panic("Invalid ring size param\n");
1220 if (!strncmp(ptr, "KiB", strlen("KiB")))
1221 ctx.reserve_size *= 1 << 10;
1222 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1223 ctx.reserve_size = 1 << 20;
1224 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1225 ctx.reserve_size *= 1 << 30;
1226 else
1227 panic("Invalid ring size unit type\n");
1229 break;
1230 case '?':
1231 switch (optopt) {
1232 case 'd':
1233 case 'c':
1234 case 'n':
1235 case 'S':
1236 case 's':
1237 case 'P':
1238 case 'o':
1239 case 'E':
1240 case 'i':
1241 case 'k':
1242 case 'u':
1243 case 'g':
1244 case 't':
1245 panic("Option -%c requires an argument!\n",
1246 optopt);
1247 default:
1248 if (isprint(optopt))
1249 printf("Unknown option character `0x%X\'!\n", optopt);
1250 die();
1252 default:
1253 break;
1257 if (argc >= optind) {
1258 min_opts = 4;
1259 ctx.packet_str = argv2str(optind, argc, argv);
1262 if (argc < min_opts)
1263 help();
1264 if (ctx.device == NULL)
1265 panic("No networking device given!\n");
1266 if (confname == NULL && !ctx.packet_str)
1267 panic("No configuration file or packet string given!\n");
1269 register_signal(SIGINT, signal_handler);
1270 register_signal(SIGQUIT, signal_handler);
1271 register_signal(SIGTERM, signal_handler);
1272 register_signal(SIGHUP, signal_handler);
1274 if (prio_high) {
1275 set_proc_prio(-20);
1276 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1279 if (set_sock_mem)
1280 set_system_socket_memory(vals, array_size(vals));
1281 xlockme();
1283 if (ctx.pcap_in) {
1284 ctx.dev_in = dev_io_create(ctx.pcap_in, DEV_IO_IN);
1285 if (!ctx.dev_in)
1286 panic("Failed to open input device\n");
1287 dev_io_open(ctx.dev_in);
1290 ctx.dev_out = dev_io_create(ctx.device, DEV_IO_OUT);
1291 if (!ctx.dev_out)
1292 panic("Failed to open output device\n");
1294 if (ctx.rfraw) {
1295 if (dev_io_link_type_set(ctx.dev_out, LINKTYPE_IEEE802_11_RADIOTAP))
1296 panic("Failed to setup rfraw device\n");
1298 panic_handler_add(on_panic_del_rfmon, ctx.dev_out);
1301 protos_init(ctx.dev_out);
1303 if (shaper_is_set(&ctx.sh) || (ctx.dev_in && !dev_io_is_netdev(ctx.dev_in))
1304 || !dev_io_is_netdev(ctx.dev_out)) {
1306 prctl(PR_SET_TIMERSLACK, 1UL);
1307 /* Fall back to single core to not mess up correct timing.
1308 * We are slow anyway!
1310 ctx.cpu_start = 0;
1311 ctx.cpu_num = 1;
1312 slow = true;
1316 * If number of packets is smaller than number of CPUs use only as
1317 * many CPUs as there are packets. Otherwise we end up sending more
1318 * packets than intended or none at all.
1320 if (ctx.num)
1321 ctx.cpu_num = min_t(unsigned int, ctx.num, ctx.cpu_num);
1323 if (set_irq_aff && dev_io_is_netdev(ctx.dev_out)) {
1324 irq = device_irq_number(ctx.device);
1325 device_set_irq_affinity_list(irq, ctx.cpu_start,
1326 ctx.cpu_start + ctx.cpu_num - 1);
1329 stats = setup_shared_var(ctx.cpu_num);
1332 if (ctx.verbose)
1333 printf("Start %u worker processes on cpus [%u-%u].\n",
1334 ctx.cpu_num, ctx.cpu_start, ctx.cpu_start + ctx.cpu_num - 1);
1335 for (i = 0; i < ctx.cpu_num; i++) {
1336 pid_t pid = fork();
1338 switch (pid) {
1339 case 0:
1340 if (reseed)
1341 seed = generate_srand_seed();
1342 srand(seed);
1344 cpu_affinity(ctx.cpu_start + i);
1345 main_loop(&ctx, confname, slow, i, invoke_cpp,
1346 cpp_argv, orig_num);
1348 goto thread_out;
1349 case -1:
1350 panic("Cannot fork processes!\n");
1354 for (i = 0; i < ctx.cpu_num; i++) {
1355 int status;
1357 wait(&status);
1358 if (WEXITSTATUS(status) == EXIT_FAILURE)
1359 die();
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.cpu_num; 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.cpu_num; 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.cpu_num);
1386 if (dev_io_is_netdev(ctx.dev_out) && set_irq_aff)
1387 device_restore_irq_affinity_list();
1389 argv_free(cpp_argv);
1390 free(ctx.device);
1391 free(ctx.rhost);
1392 free(confname);
1393 free(ctx.packet_str);
1395 return 0;