AUTHORS: drop e-mail addresses, update with latest contributors
[netsniff-ng.git] / trafgen.c
blobedd32b1c61aa92ccf0f3f010424d666e5f121351
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 {"smoke-test", required_argument, NULL, 's'},
123 {"seed", required_argument, NULL, 'E'},
124 {"user", required_argument, NULL, 'u'},
125 {"group", required_argument, NULL, 'g'},
126 {"prio-high", no_argument, NULL, 'H'},
127 {"notouch-irq", no_argument, NULL, 'Q'},
128 {"no-sock-mem", no_argument, NULL, 'A'},
129 {"qdisc-path", no_argument, NULL, 'q'},
130 {"jumbo-support", no_argument, NULL, 'J'},
131 {"no-cpu-stats", no_argument, NULL, 'C'},
132 {"cpp", no_argument, NULL, 'p'},
133 {"define", required_argument, NULL, 'D'},
134 {"rfraw", no_argument, NULL, 'R'},
135 {"rand", no_argument, NULL, 'r'},
136 {"verbose", no_argument, NULL, 'V'},
137 {"version", no_argument, NULL, 'v'},
138 {"example", no_argument, NULL, 'e'},
139 {"help", no_argument, NULL, 'h'},
140 {NULL, 0, NULL, 0}
143 static const char *copyright =
144 "Please report bugs at https://github.com/netsniff-ng/netsniff-ng/issues\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|.cfg|.pcap> Networking device or configuration file 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>[-<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/kpps/Mpps/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 --dev eth0 --conf trafgen.cfg --cpus 2-4\n"
217 " trafgen -e | trafgen -i - -o eth0 --cpp -n 1\n"
218 " trafgen --dev eth0 --conf fuzzing.cfg --smoke-test 10.0.0.1\n"
219 " trafgen --dev wlan0 --rfraw --conf beacon-test.txf -V --cpus 2\n"
220 " trafgen --dev eth0 --conf frag_dos.cfg --rand --gap 1000us\n"
221 " trafgen --dev eth0 --conf icmp.cfg --rand --num 1400000 -k1000\n"
222 " trafgen --dev eth0 --conf tcp_syn.cfg -u `id -u bob` -g `id -g bob`\n"
223 " trafgen --dev eth0 '{ fill(0xff, 6), 0x00, 0x02, 0xb3, rnd(3), c16(0x0800), fill(0xca, 64) }'\n\n"
224 "Arbitrary packet config examples (e.g. trafgen -e > trafgen.cfg):\n"
225 " Run packet on all CPUs: { fill(0xff, 64) csum16(0, 64) }\n"
226 " Run packet only on CPU1: cpu(1): { rnd(64), 0b11001100, 0xaa }\n"
227 " Run packet only on CPU1-2: cpu(1-2): { drnd(64),'a',csum16(1, 8),'b',42 }\n\n"
228 "Generate config files from existing pcap using netsniff-ng:\n"
229 " netsniff-ng --in dump.pcap --out dump.cfg\n\n"
230 "Note:\n"
231 " Smoke/fuzz test example: machine A, 10.0.0.2 (trafgen) is directly\n"
232 " connected to machine B (test kernel), 10.0.0.1. If ICMP reply fails\n"
233 " we assume the kernel crashed, thus we print the packet and quit.\n"
234 " In case you find a ping-of-death, please mention trafgen in your\n"
235 " commit message of the fix!\n\n"
236 " For introducing bit errors, delays with random variation and more,\n"
237 " make use of tc(8) with its different disciplines, i.e. netem.\n\n"
238 " For generating different package distributions, you can use scripting\n"
239 " to generate a trafgen config file with packet ratios as:\n\n"
240 " IMIX 64:7, 570:4, 1518:1\n"
241 " Tolly 64:55, 78:5, 576:17, 1518:23\n"
242 " Cisco 64:7, 594:4, 1518:1\n"
243 " RPR Trimodal 64:60, 512:20, 1518:20\n"
244 " RPR Quadrimodal 64:50, 512:15, 1518:15, 9218:20\n");
245 puts(copyright);
246 die();
249 static void __noreturn example(void)
251 const char *e =
252 "/* Note: dynamic elements make trafgen slower! */\n"
253 "#include <stddef.h>\n\n"
254 "{\n"
255 " /* MAC Destination */\n"
256 " fill(0xff, ETH_ALEN),\n"
257 " /* MAC Source */\n"
258 " 0x00, 0x02, 0xb3, drnd(3),\n"
259 " /* IPv4 Protocol */\n"
260 " c16(ETH_P_IP),\n"
261 " /* IPv4 Version, IHL, TOS */\n"
262 " 0b01000101, 0,\n"
263 " /* IPv4 Total Len */\n"
264 " c16(59),\n"
265 " /* IPv4 Ident */\n"
266 " drnd(2),\n"
267 " /* IPv4 Flags, Frag Off */\n"
268 " 0b01000000, 0,\n"
269 " /* IPv4 TTL */\n"
270 " 64,\n"
271 " /* Proto TCP */\n"
272 " 0x06,\n"
273 " /* IPv4 Checksum (IP header from, to) */\n"
274 " csumip(14, 33),\n"
275 " /* Source IP */\n"
276 " drnd(4),\n"
277 " /* Dest IP */\n"
278 " drnd(4),\n"
279 " /* TCP Source Port */\n"
280 " drnd(2),\n"
281 " /* TCP Dest Port */\n"
282 " c16(80),\n"
283 " /* TCP Sequence Number */\n"
284 " drnd(4),\n"
285 " /* TCP Ackn. Number */\n"
286 " c32(0),\n"
287 " /* TCP Header length + TCP SYN/ECN Flag */\n"
288 " c16((8 << 12) | TCP_FLAG_SYN | TCP_FLAG_ECE)\n"
289 " /* Window Size */\n"
290 " c16(16),\n"
291 " /* TCP Checksum (offset IP, offset TCP) */\n"
292 " csumtcp(14, 34),\n"
293 " /* TCP Options */\n"
294 " 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x06,\n"
295 " 0x91, 0x68, 0x7d, 0x06, 0x91, 0x68, 0x6f,\n"
296 " /* Data blob */\n"
297 " \"gotcha!\",\n"
298 "}";
299 puts(e);
300 die();
303 static void __noreturn version(void)
305 printf("trafgen %s, Git id: %s\n", VERSION_LONG, GITVERSION);
306 puts("multithreaded zero-copy network packet generator\n"
307 "http://www.netsniff-ng.org\n");
308 puts(copyright);
309 die();
312 static void apply_counter(int id)
314 size_t j, counter_max = packet_dyn[id].clen;
316 for (j = 0; j < counter_max; ++j) {
317 uint8_t val;
318 struct counter *counter = &packet_dyn[id].cnt[j];
320 val = counter->val - counter->min;
321 val = (val + counter->inc) % (counter->max - counter->min + 1);
323 counter->val = val + counter->min;
324 packets[id].payload[counter->off] = counter->val;
328 static void apply_randomizer(int id)
330 size_t j, rand_max = packet_dyn[id].rlen;
332 for (j = 0; j < rand_max; ++j) {
333 uint8_t val = (uint8_t) rand();
334 struct randomizer *randomizer = &packet_dyn[id].rnd[j];
336 packets[id].payload[randomizer->off] = val;
340 static void apply_csum16(int id)
342 size_t j, csum_max = packet_dyn[id].slen;
344 for (j = 0; j < csum_max; ++j) {
345 uint16_t sum = 0;
346 struct csum16 *csum = &packet_dyn[id].csum[j];
348 memset(&packets[id].payload[csum->off], 0, sizeof(sum));
349 if (unlikely((size_t) csum->to >= packets[id].len))
350 csum->to = packets[id].len - 1;
352 switch (csum->which) {
353 case CSUM_IP:
354 sum = calc_csum(packets[id].payload + csum->from,
355 csum->to - csum->from + 1);
356 break;
357 case CSUM_UDP:
358 sum = p4_csum((void *) packets[id].payload + csum->from,
359 packets[id].payload + csum->to,
360 (packets[id].len - csum->to),
361 IPPROTO_UDP);
362 break;
363 case CSUM_TCP:
364 sum = p4_csum((void *) packets[id].payload + csum->from,
365 packets[id].payload + csum->to,
366 (packets[id].len - csum->to),
367 IPPROTO_TCP);
368 break;
369 case CSUM_UDP6:
370 sum = p6_csum((void *) packets[id].payload + csum->from,
371 packets[id].payload + csum->to,
372 (packets[id].len - csum->to),
373 IPPROTO_UDP);
374 break;
375 case CSUM_TCP6:
376 sum = p6_csum((void *) packets[id].payload + csum->from,
377 packets[id].payload + csum->to,
378 (packets[id].len - csum->to),
379 IPPROTO_TCP);
380 break;
381 case CSUM_ICMP6:
382 sum = p6_csum((void *) packets[id].payload + csum->from,
383 packets[id].payload + csum->to,
384 (packets[id].len - csum->to),
385 IPPROTO_ICMPV6);
386 break;
387 default:
388 bug();
389 break;
392 memcpy(&packets[id].payload[csum->off], &sum, sizeof(sum));
396 static void preprocess_packets(void)
398 size_t i;
400 for (i = 0; i < plen; i++) {
401 struct packet_dyn *pktd = &packet_dyn[i];
403 if (packet_dyn_has_only_csums(pktd)) {
404 apply_csum16(i);
405 pktd->slen = 0;
406 xfree(pktd->csum);
411 static struct cpu_stats *setup_shared_var(unsigned int cpus)
413 int fd;
414 size_t len = cpus * sizeof(struct cpu_stats);
415 char *zbuff, file[256];
416 struct cpu_stats *buff;
418 slprintf(file, sizeof(file), "/tmp/.tmp_mmap.XXXXXX");
419 fd = mkostemp_or_die(file, O_RDWR | O_CREAT | O_TRUNC);
420 zbuff = xzmalloc(len);
421 write_or_die(fd, zbuff, len);
422 xfree(zbuff);
424 buff = mmap(NULL, len, PROT_READ | PROT_WRITE,
425 MAP_SHARED, fd, 0);
426 if (buff == MAP_FAILED)
427 panic("Cannot setup shared variable!\n");
429 close(fd);
430 unlink(file);
432 memset(buff, 0, len);
433 return buff;
436 static void destroy_shared_var(void *buff, unsigned int cpus)
438 munmap(buff, cpus * sizeof(struct cpu_stats));
441 static void dump_trafgen_snippet(uint8_t *payload, size_t len)
443 size_t i;
445 printf("{");
446 for (i = 0; i < len; ++i) {
447 if (i % 15 == 0)
448 printf("\n ");
449 printf("0x%02x, ", payload[i]);
451 printf("\n}\n");
452 fflush(stdout);
455 static int xmit_smoke_setup(struct ctx *ctx)
457 int icmp_sock, ret, ttl = 64;
458 struct icmp_filter filter;
460 icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
461 if (icmp_sock < 0)
462 panic("Cannot get a ICMP socket: %s!\n", strerror(errno));
464 filter.data = ~(1 << ICMP_ECHOREPLY);
466 ret = setsockopt(icmp_sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(filter));
467 if (ret < 0)
468 panic("Cannot install filter!\n");
470 ret = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
471 if (ret < 0)
472 panic("Cannot set TTL!\n");
474 memset(&ctx->dest, 0, sizeof(ctx->dest));
475 ctx->dest.sin_family = AF_INET;
476 ctx->dest.sin_port = 0;
478 ret = inet_aton(ctx->rhost, &ctx->dest.sin_addr);
479 if (ret < 0)
480 panic("Cannot resolve address!\n");
482 return icmp_sock;
485 static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx)
487 int ret;
488 unsigned int i, j;
489 short ident, cnt = 1, idstore[SMOKE_N_PROBES];
490 uint8_t outpack[512], *data;
491 struct icmphdr *icmp;
492 struct iphdr *ip;
493 size_t len = sizeof(*icmp) + 56;
494 struct sockaddr_in from;
495 socklen_t from_len;
496 struct pollfd fds = {
497 .fd = icmp_sock,
498 .events = POLLIN,
501 memset(idstore, 0, sizeof(idstore));
502 for (j = 0; j < SMOKE_N_PROBES; j++) {
503 while ((ident = htons((short) rand())) == 0)
504 sleep(0);
505 idstore[j] = ident;
507 memset(outpack, 0, sizeof(outpack));
508 icmp = (void *) outpack;
509 icmp->type = ICMP_ECHO;
510 icmp->un.echo.id = ident;
511 icmp->un.echo.sequence = htons(cnt++);
513 data = ((uint8_t *) outpack + sizeof(*icmp));
514 for (i = 0; i < 56; ++i)
515 data[i] = (uint8_t) rand();
517 icmp->checksum = csum((unsigned short *) outpack,
518 len / sizeof(unsigned short));
520 ret = sendto(icmp_sock, outpack, len, MSG_DONTWAIT,
521 (struct sockaddr *) &ctx->dest, sizeof(ctx->dest));
522 if (unlikely(ret != (int) len))
523 panic("Cannot send out probe: %s!\n", strerror(errno));
525 ret = poll(&fds, 1, 50);
526 if (ret < 0)
527 panic("Poll failed!\n");
529 if (fds.revents & POLLIN) {
530 ret = recvfrom(icmp_sock, outpack, sizeof(outpack), 0,
531 (struct sockaddr *) &from, &from_len);
532 if (unlikely(ret <= 0))
533 panic("Probe receive failed!\n");
534 if (unlikely(from_len != sizeof(ctx->dest)))
535 continue;
536 if (unlikely(memcmp(&from, &ctx->dest, sizeof(ctx->dest))))
537 continue;
538 if (unlikely((size_t) ret < sizeof(*ip) + sizeof(*icmp)))
539 continue;
540 ip = (void *) outpack;
541 if (unlikely(ip->ihl * 4 + sizeof(*icmp) > (size_t) ret))
542 continue;
543 icmp = (void *) outpack + ip->ihl * 4;
544 for (i = 0; i < array_size(idstore); ++i) {
545 if (unlikely(icmp->un.echo.id != idstore[i]))
546 continue;
547 return 0;
552 return -1;
555 static bool shaper_is_set(struct shaper *sh)
557 return sh->type != SHAPER_NONE;
560 static void shaper_init(struct shaper *sh)
562 if (sh->type == SHAPER_NONE || sh->type == SHAPER_DELAY)
563 return;
565 memset(&sh->delay, 0, sizeof(struct timespec));
566 bug_on(gettimeofday(&sh->start, NULL));
567 sh->sent = 0;
570 static void shaper_set_delay(struct shaper *sh, time_t sec, long int ns)
572 sh->type = SHAPER_DELAY;
573 sh->delay.tv_sec = sec;
574 sh->delay.tv_nsec = ns;
577 static void shaper_set_rate(struct shaper *sh, unsigned long long rate,
578 enum shaper_type type)
580 memset(sh, 0, sizeof(struct shaper));
581 sh->rate = rate;
582 sh->type = type;
585 static void shaper_set_tstamp(struct shaper *sh, struct timespec *ts)
587 TIMESPEC_TO_TIMEVAL(&sh->tstamp, ts);
590 static void shaper_delay(struct shaper *sh, struct packet *pkt)
592 if (sh->type == SHAPER_BYTES || sh->type == SHAPER_PKTS) {
593 unsigned long pkt_len = pkt->len;
595 sh->sent += sh->type == SHAPER_BYTES ? pkt_len : 1;
597 if (sh->sent >= sh->rate && sh->rate > 0) {
598 struct timeval delay_us;
599 struct timeval time_sent;
600 struct timeval time_1s = { .tv_sec = 1 };
602 bug_on(gettimeofday(&sh->end, NULL));
603 timersub(&sh->end, &sh->start, &time_sent);
605 if (timercmp(&time_1s, &time_sent, > )) {
606 timersub(&time_1s, &time_sent, &delay_us);
607 TIMEVAL_TO_TIMESPEC(&delay_us, &sh->delay);
610 } else if (sh->type == SHAPER_TSTAMP) {
611 struct timeval tstamp;
612 struct timeval pkt_diff;
613 struct timeval diff;
615 bug_on(gettimeofday(&sh->end, NULL));
616 TIMESPEC_TO_TIMEVAL(&tstamp, &pkt->tstamp);
617 timersub(&sh->end, &sh->start, &diff);
618 timersub(&tstamp, &sh->tstamp, &pkt_diff);
620 if (timercmp(&diff, &pkt_diff, <)) {
621 struct timeval delay;
623 timersub(&pkt_diff, &diff, &delay);
624 TIMEVAL_TO_TIMESPEC(&delay, &sh->delay);
627 memcpy(&sh->tstamp, &tstamp, sizeof(sh->tstamp));
630 if ((sh->delay.tv_sec | sh->delay.tv_nsec) > 0) {
631 nanosleep(&sh->delay, NULL);
633 shaper_init(sh);
637 static inline void packet_apply_dyn_elements(int idx)
639 if (packet_dyn_has_elems(&packet_dyn[idx])) {
640 apply_counter(idx);
641 apply_randomizer(idx);
642 apply_csum16(idx);
645 if (packet_dyn_has_fields(&packet_dyn[idx])) {
646 uint32_t i;
648 for (i = 0; i < packet_dyn[idx].flen; i++)
649 proto_field_dyn_apply(packet_dyn[idx].fields[i]);
651 proto_packet_update(idx);
655 static void xmit_slowpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned long orig_num)
657 int ret, icmp_sock = -1;
658 unsigned long num = 1, i = 0;
659 struct timeval start, end, diff;
660 unsigned long long tx_bytes = 0, tx_packets = 0;
662 if (ctx->num > 0)
663 num = ctx->num;
664 if (ctx->num == 0 && orig_num > 0)
665 num = 0;
667 if (ctx->smoke_test)
668 icmp_sock = xmit_smoke_setup(ctx);
670 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
672 bug_on(gettimeofday(&start, NULL));
674 if (shaper_is_set(&ctx->sh))
675 shaper_init(&ctx->sh);
677 while (likely(sigint == 0 && num > 0 && plen > 0)) {
678 packet_apply_dyn_elements(i);
679 retry:
680 ret = dev_io_write(ctx->dev_out, &packets[i]);
681 if (unlikely(ret < 0)) {
682 if (errno == ENOBUFS) {
683 sched_yield();
684 goto retry;
686 if (ctx->smoke_test)
687 panic("Sendto error: %s!\n", strerror(errno));
690 tx_bytes += packets[i].len;
691 tx_packets++;
693 if (ctx->smoke_test) {
694 ret = xmit_smoke_probe(icmp_sock, ctx);
695 if (unlikely(ret < 0)) {
696 printf("%sSmoke test alert:%s\n", colorize_start(bold), colorize_end());
697 printf(" Remote host seems to be unresponsive to ICMP probes!\n");
698 printf(" Last instance was packet%lu, seed:%u, trafgen snippet:\n\n",
699 i, seed);
701 dump_trafgen_snippet(packets[i].payload, packets[i].len);
702 break;
706 if (!ctx->rand) {
707 i++;
708 if (i >= plen)
709 i = 0;
710 } else
711 i = rand() % plen;
713 if (ctx->num > 0)
714 num--;
716 if (shaper_is_set(&ctx->sh))
717 shaper_delay(&ctx->sh, &packets[i]);
720 bug_on(gettimeofday(&end, NULL));
721 timersub(&end, &start, &diff);
723 if (ctx->smoke_test)
724 close(icmp_sock);
726 stats[cpu].tx_packets = tx_packets;
727 stats[cpu].tx_bytes = tx_bytes;
728 stats[cpu].tv_sec = diff.tv_sec;
729 stats[cpu].tv_usec = diff.tv_usec;
731 stats[cpu].state |= CPU_STATS_STATE_RES;
734 static void xmit_fastpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned long orig_num)
736 int ifindex = dev_io_ifindex_get(ctx->dev_out);
737 uint8_t *out = NULL;
738 unsigned int it = 0, retry = 100;
739 unsigned long num = 1, i = 0;
740 size_t size = ring_size(dev_io_name_get(ctx->dev_out), ctx->reserve_size);
741 struct ring tx_ring;
742 struct frame_map *hdr;
743 struct timeval start, end, diff;
744 unsigned long long tx_bytes = 0, tx_packets = 0;
745 int sock = dev_io_fd_get(ctx->dev_out);
747 set_sock_prio(sock, 512);
749 ring_tx_setup(&tx_ring, sock, size, ifindex, ctx->jumbo_support, ctx->verbose);
751 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
753 if (ctx->num > 0)
754 num = ctx->num;
755 if (ctx->num == 0 && orig_num > 0)
756 num = 0;
758 bug_on(gettimeofday(&start, NULL));
760 while (likely(sigint == 0 && num > 0 && plen > 0)) {
761 if (!user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
762 int ret = pull_and_flush_tx_ring(sock);
763 if (unlikely(ret < 0)) {
764 /* We could hit EBADF if the socket has been closed before
765 * the timer was triggered.
767 if (errno != EBADF && errno != ENOBUFS)
768 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
771 continue;
774 hdr = tx_ring.frames[it].iov_base;
775 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
777 hdr->tp_h.tp_snaplen = packets[i].len;
778 hdr->tp_h.tp_len = packets[i].len;
780 packet_apply_dyn_elements(i);
782 memcpy(out, packets[i].payload, packets[i].len);
784 tx_bytes += packets[i].len;
785 tx_packets++;
787 if (!ctx->rand) {
788 i++;
789 if (i >= plen)
790 i = 0;
791 } else
792 i = rand() % plen;
794 kernel_may_pull_from_tx(&hdr->tp_h);
796 it++;
797 if (it >= tx_ring.layout.tp_frame_nr)
798 it = 0;
800 if (ctx->num > 0)
801 num--;
804 bug_on(gettimeofday(&end, NULL));
805 timersub(&end, &start, &diff);
807 while (pull_and_flush_tx_ring_wait(sock) < 0 && errno == ENOBUFS && retry-- > 0)
808 usleep(10000);
809 destroy_tx_ring(sock, &tx_ring);
811 stats[cpu].tx_packets = tx_packets;
812 stats[cpu].tx_bytes = tx_bytes;
813 stats[cpu].tv_sec = diff.tv_sec;
814 stats[cpu].tv_usec = diff.tv_usec;
816 stats[cpu].state |= CPU_STATS_STATE_RES;
819 static inline void __set_state(unsigned int cpu, sig_atomic_t s)
821 stats[cpu].state = s;
824 static inline sig_atomic_t __get_state(unsigned int cpu)
826 return stats[cpu].state;
829 static unsigned long __wait_and_sum_others(struct ctx *ctx, unsigned int cpu)
831 unsigned int i;
832 unsigned long total;
834 for (i = 0, total = plen; i < ctx->cpu_num; i++) {
835 if (i == cpu)
836 continue;
838 while ((__get_state(i) &
839 (CPU_STATS_STATE_CFG |
840 CPU_STATS_STATE_RES)) == 0 &&
841 sigint == 0)
842 sched_yield();
844 total += stats[i].cf_packets;
847 return total;
850 static void __correct_global_delta(struct ctx *ctx, unsigned int cpu, unsigned long orig)
852 unsigned int i;
853 unsigned long total;
854 int cpu_sel;
855 long long delta_correction = 0;
857 for (i = 0, total = ctx->num; i < ctx->cpu_num; i++) {
858 if (i == cpu)
859 continue;
861 while ((__get_state(i) &
862 (CPU_STATS_STATE_CHK |
863 CPU_STATS_STATE_RES)) == 0 &&
864 sigint == 0)
865 sched_yield();
867 total += stats[i].cd_packets;
870 if (total > orig)
871 delta_correction = -1 * ((long long) total - orig);
872 if (total < orig)
873 delta_correction = +1 * ((long long) orig - total);
875 for (cpu_sel = -1, i = 0; i < ctx->cpu_num; i++) {
876 if (stats[i].cd_packets > 0) {
877 if ((long long) stats[i].cd_packets +
878 delta_correction >= 0) {
879 cpu_sel = i;
880 break;
885 if ((int) cpu == cpu_sel)
886 ctx->num += delta_correction;
889 static void __set_state_cf(unsigned int cpu, unsigned long p, unsigned long b,
890 sig_atomic_t s)
892 stats[cpu].cf_packets = p;
893 stats[cpu].cf_bytes = b;
894 stats[cpu].state = s;
897 static void __set_state_cd(unsigned int cpu, unsigned long p, sig_atomic_t s)
899 stats[cpu].cd_packets = p;
900 stats[cpu].state = s;
903 static void xmit_packet_precheck(struct ctx *ctx, unsigned int cpu)
905 unsigned long plen_total, orig = ctx->num;
906 size_t total_len = 0;
907 unsigned int i;
909 bug_on(plen != dlen);
911 for (i = 0; i < plen; ++i)
912 total_len += packets[i].len;
914 __set_state_cf(cpu, plen, total_len, CPU_STATS_STATE_CFG);
915 plen_total = __wait_and_sum_others(ctx, cpu);
917 if (orig > 0) {
918 ctx->num = (unsigned long) round((1.0 * plen / plen_total) * orig);
920 __set_state_cd(cpu, ctx->num, CPU_STATS_STATE_CHK |
921 CPU_STATS_STATE_CFG);
922 __correct_global_delta(ctx, cpu, orig);
925 if (plen == 0) {
926 __set_state(cpu, CPU_STATS_STATE_RES);
927 return;
931 static void pcap_load_packets(struct dev_io *dev)
933 while (dev_io_read(dev))
934 /* nothing to do */;
937 static void main_loop(struct ctx *ctx, char *confname, bool slow,
938 unsigned int cpu, bool invoke_cpp, char **cpp_argv,
939 unsigned long orig_num)
941 if (ctx->dev_in && dev_io_is_pcap(ctx->dev_in)) {
942 pcap_load_packets(ctx->dev_in);
943 shaper_set_tstamp(&ctx->sh, &packets[0].tstamp);
944 ctx->num = plen;
945 } else {
946 if (ctx->packet_str)
947 compile_packets_str(ctx->packet_str, ctx->verbose, cpu);
948 else
949 compile_packets(confname, ctx->verbose, cpu, invoke_cpp, cpp_argv);
951 preprocess_packets();
954 xmit_packet_precheck(ctx, cpu);
956 if (cpu == 0) {
957 unsigned int i;
958 size_t total_len = 0, total_pkts = 0;
960 for (i = 0; i < ctx->cpu_num; ++i) {
961 total_len += stats[i].cf_bytes;
962 total_pkts += stats[i].cf_packets;
965 printf("%6zu packets to schedule\n", total_pkts);
966 printf("%6zu bytes in total\n", total_len);
967 printf("Running! Hang up with ^C!\n\n");
968 fflush(stdout);
971 dev_io_open(ctx->dev_out);
972 if (dev_io_is_netdev(ctx->dev_out) && ctx->qdisc_path == false)
973 set_sock_qdisc_bypass(dev_io_fd_get(ctx->dev_out), ctx->verbose);
975 if (slow)
976 xmit_slowpath_or_die(ctx, cpu, orig_num);
977 else
978 xmit_fastpath_or_die(ctx, cpu, orig_num);
980 dev_io_close(ctx->dev_out);
981 if (ctx->dev_in)
982 dev_io_close(ctx->dev_in);
984 cleanup_packets();
987 static unsigned int generate_srand_seed(void)
989 int fd;
990 unsigned int _seed;
992 fd = open("/dev/urandom", O_RDONLY);
993 if (fd < 0)
994 return time(NULL);
996 read_or_die(fd, &_seed, sizeof(_seed));
998 close(fd);
999 return _seed;
1002 static void on_panic_del_rfmon(void *arg)
1004 dev_io_close(arg);
1007 int main(int argc, char **argv)
1009 bool slow = false, invoke_cpp = false, reseed = true, cpustats = true;
1010 bool prio_high = false, set_irq_aff = true, set_sock_mem = true;
1011 int c, vals[4] = {0}, irq;
1012 uint64_t gap = 0;
1013 unsigned int i;
1014 char *confname = NULL, *ptr;
1015 unsigned long cpu_n, orig_num = 0;
1016 unsigned long long tx_packets, tx_bytes;
1017 struct ctx ctx;
1018 int min_opts = 5;
1019 char **cpp_argv = NULL;
1020 size_t cpp_argc = 0;
1021 unsigned long long rate;
1022 enum shaper_type shape_type;
1023 struct timespec delay;
1025 memset(&ctx, 0, sizeof(ctx));
1026 ctx.cpu_num = get_number_cpus_online();
1027 ctx.uid = getuid();
1028 ctx.gid = getgid();
1029 ctx.qdisc_path = false;
1031 /* Keep an initial small default size to reduce cache-misses. */
1032 ctx.reserve_size = 512 * (1 << 10);
1034 while ((c = getopt_long(argc, argv, short_options, long_options,
1035 NULL)) != EOF) {
1036 switch (c) {
1037 case 'h':
1038 help();
1039 break;
1040 case 'v':
1041 version();
1042 break;
1043 case 'C':
1044 cpustats = false;
1045 break;
1046 case 'e':
1047 example();
1048 break;
1049 case 'p':
1050 invoke_cpp = true;
1051 break;
1052 case 'D':
1053 cpp_argv = argv_insert(cpp_argv, &cpp_argc, "-D");
1054 cpp_argv = argv_insert(cpp_argv, &cpp_argc, optarg);
1055 break;
1056 case 'V':
1057 ctx.verbose = true;
1058 break;
1059 case 'P':
1060 if (slow)
1061 break;
1062 cpu_n = strtoul(optarg, &ptr, 0);
1063 if (ptr && *ptr == '-') {
1064 if (cpu_n < 0 || cpu_n >= ctx.cpu_num)
1065 break;
1066 ctx.cpu_start = cpu_n;
1067 cpu_n = strtoul(ptr + 1, NULL, 0);
1068 if (cpu_n < ctx.cpu_start || cpu_n >= ctx.cpu_num)
1069 ctx.cpu_num -= ctx.cpu_start;
1070 else
1071 ctx.cpu_num = cpu_n - ctx.cpu_start + 1;
1072 } else if (cpu_n > 0 && cpu_n <= ctx.cpu_num)
1073 ctx.cpu_num = cpu_n;
1074 break;
1075 case 'd':
1076 case 'o':
1077 ctx.device = xstrdup(optarg);
1078 break;
1079 case 'H':
1080 prio_high = true;
1081 break;
1082 case 'A':
1083 set_sock_mem = false;
1084 break;
1085 case 'Q':
1086 set_irq_aff = false;
1087 break;
1088 case 'q':
1089 ctx.qdisc_path = true;
1090 break;
1091 case 'r':
1092 ctx.rand = true;
1093 break;
1094 case 's':
1095 slow = true;
1096 ctx.cpu_start = 0;
1097 ctx.cpu_num = 1;
1098 ctx.smoke_test = true;
1099 ctx.rhost = xstrdup(optarg);
1100 break;
1101 case 'R':
1102 ctx.rfraw = true;
1103 break;
1104 case 'J':
1105 ctx.jumbo_support = true;
1106 break;
1107 case 'c':
1108 case 'i':
1109 confname = xstrdup(optarg);
1110 if (c == 'i' && strstr(confname, ".pcap")) {
1111 ctx.sh.type = SHAPER_TSTAMP;
1112 ctx.pcap_in = confname;
1113 } else if (!strncmp("-", confname, strlen("-"))) {
1114 ctx.cpu_start = 0;
1115 ctx.cpu_num = 1;
1117 break;
1118 case 'u':
1119 ctx.uid = strtoul(optarg, NULL, 0);
1120 ctx.enforce = true;
1121 break;
1122 case 'g':
1123 ctx.gid = strtoul(optarg, NULL, 0);
1124 ctx.enforce = true;
1125 break;
1126 case 'E':
1127 seed = strtoul(optarg, NULL, 0);
1128 reseed = false;
1129 break;
1130 case 'n':
1131 orig_num = strtoul(optarg, NULL, 0);
1132 ctx.num = orig_num;
1133 break;
1134 case 't':
1135 gap = strtoul(optarg, &ptr, 0);
1136 if (!gap && optarg == ptr)
1137 panic("Invalid gap param\n");
1139 if (!strncmp(ptr, "ns", strlen("ns"))) {
1140 delay.tv_sec = gap / 1000000000;
1141 delay.tv_nsec = gap % 1000000000;
1142 } else if (*ptr == '\0' || !strncmp(ptr, "us", strlen("us"))) {
1143 /* Default to microseconds for backwards
1144 * compatibility if no postfix is given.
1146 delay.tv_sec = gap / 1000000;
1147 delay.tv_nsec = (gap % 1000000) * 1000;
1148 } else if (!strncmp(ptr, "ms", strlen("ms"))) {
1149 delay.tv_sec = gap / 1000;
1150 delay.tv_nsec = (gap % 1000) * 1000000;
1151 } else if (!strncmp(ptr, "s", strlen("s"))) {
1152 delay.tv_sec = gap;
1153 delay.tv_nsec = 0;
1154 } else {
1155 panic("Syntax error in time param!\n");
1158 shaper_set_delay(&ctx.sh, delay.tv_sec, delay.tv_nsec);
1159 break;
1160 case 'b':
1161 rate = strtoul(optarg, &ptr, 0);
1162 if (!rate && optarg == ptr)
1163 panic("Invalid rate param\n");
1165 if (strncmp(ptr, "pps", strlen("pps")) == 0) {
1166 shape_type = SHAPER_PKTS;
1167 } else if (strncmp(ptr, "kpps", strlen("kpps")) == 0) {
1168 shape_type = SHAPER_PKTS;
1169 rate *= 1000;
1170 } else if (strncmp(ptr, "Mpps", strlen("Mpps")) == 0) {
1171 shape_type = SHAPER_PKTS;
1172 rate *= 1000 * 1000;
1173 } else if (strncmp(ptr, "B", strlen("B")) == 0) {
1174 shape_type = SHAPER_BYTES;
1175 } else if (strncmp(ptr, "kB", strlen("kB")) == 0) {
1176 shape_type = SHAPER_BYTES;
1177 rate *= 1000;
1178 } else if (strncmp(ptr, "MB", strlen("MB")) == 0) {
1179 shape_type = SHAPER_BYTES;
1180 rate *= 1000 * 1000;
1181 } else if (strncmp(ptr, "GB", strlen("GB")) == 0) {
1182 shape_type = SHAPER_BYTES;
1183 rate *= 1000 * 1000 * 1000;
1184 } else if (strncmp(ptr, "kbit", strlen("kbit")) == 0) {
1185 shape_type = SHAPER_BYTES;
1186 rate *= 1000 / 8;
1187 } else if (strncmp(ptr, "Mbit", strlen("Mbit")) == 0) {
1188 shape_type = SHAPER_BYTES;
1189 rate *= 1000 * 1000 / 8;
1190 } else if (strncmp(ptr, "Gbit", strlen("Gbit")) == 0) {
1191 shape_type = SHAPER_BYTES;
1192 rate *= 1000 * 1000 * 1000 / 8;
1193 } else if (strncmp(ptr, "KiB", strlen("KiB")) == 0) {
1194 shape_type = SHAPER_BYTES;
1195 rate *= 1 << 10;
1196 } else if (strncmp(ptr, "MiB", strlen("MiB")) == 0) {
1197 shape_type = SHAPER_BYTES;
1198 rate *= 1 << 20;
1199 } else if (strncmp(ptr, "GiB", strlen("GiB")) == 0) {
1200 shape_type = SHAPER_BYTES;
1201 rate *= 1 << 30;
1202 } else if (!rate) {
1203 shape_type = SHAPER_NONE;
1204 } else {
1205 panic("Invalid unit type for rate\n");
1208 shaper_set_rate(&ctx.sh, rate, shape_type);
1209 break;
1210 case 'S':
1211 ctx.reserve_size = strtoul(optarg, &ptr, 0);
1212 if (ctx.reserve_size == 0 && ptr == optarg)
1213 panic("Invalid ring size param\n");
1215 if (!strncmp(ptr, "KiB", strlen("KiB")))
1216 ctx.reserve_size *= 1 << 10;
1217 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1218 ctx.reserve_size = 1 << 20;
1219 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1220 ctx.reserve_size *= 1 << 30;
1221 else
1222 panic("Invalid ring size unit type\n");
1224 break;
1225 case '?':
1226 switch (optopt) {
1227 case 'd':
1228 case 'c':
1229 case 'n':
1230 case 'S':
1231 case 's':
1232 case 'P':
1233 case 'o':
1234 case 'E':
1235 case 'i':
1236 case 'u':
1237 case 'g':
1238 case 't':
1239 panic("Option -%c requires an argument!\n",
1240 optopt);
1241 default:
1242 if (isprint(optopt))
1243 printf("Unknown option character `0x%X\'!\n", optopt);
1244 die();
1246 default:
1247 break;
1251 if (argc >= optind) {
1252 min_opts = 4;
1253 ctx.packet_str = argv2str(optind, argc, argv);
1256 if (argc < min_opts)
1257 help();
1258 if (ctx.device == NULL)
1259 panic("No networking device given!\n");
1260 if (confname == NULL && !ctx.packet_str)
1261 panic("No configuration file or packet string given!\n");
1263 register_signal(SIGINT, signal_handler);
1264 register_signal(SIGQUIT, signal_handler);
1265 register_signal(SIGTERM, signal_handler);
1266 register_signal(SIGHUP, signal_handler);
1268 if (prio_high) {
1269 set_proc_prio(-20);
1270 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1273 if (set_sock_mem)
1274 set_system_socket_memory(vals, array_size(vals));
1275 xlockme();
1277 if (ctx.pcap_in) {
1278 ctx.dev_in = dev_io_create(ctx.pcap_in, DEV_IO_IN);
1279 if (!ctx.dev_in)
1280 panic("Failed to open input device\n");
1281 dev_io_open(ctx.dev_in);
1284 ctx.dev_out = dev_io_create(ctx.device, DEV_IO_OUT);
1285 if (!ctx.dev_out)
1286 panic("Failed to open output device\n");
1288 if (ctx.rfraw) {
1289 if (dev_io_link_type_set(ctx.dev_out, LINKTYPE_IEEE802_11_RADIOTAP))
1290 panic("Failed to setup rfraw device\n");
1292 panic_handler_add(on_panic_del_rfmon, ctx.dev_out);
1295 protos_init(ctx.dev_out);
1297 if (shaper_is_set(&ctx.sh) || (ctx.dev_in && !dev_io_is_netdev(ctx.dev_in))
1298 || !dev_io_is_netdev(ctx.dev_out)) {
1300 prctl(PR_SET_TIMERSLACK, 1UL);
1301 /* Fall back to single core to not mess up correct timing.
1302 * We are slow anyway!
1304 ctx.cpu_start = 0;
1305 ctx.cpu_num = 1;
1306 slow = true;
1310 * If number of packets is smaller than number of CPUs use only as
1311 * many CPUs as there are packets. Otherwise we end up sending more
1312 * packets than intended or none at all.
1314 if (ctx.num)
1315 ctx.cpu_num = min_t(unsigned int, ctx.num, ctx.cpu_num);
1317 if (set_irq_aff && dev_io_is_netdev(ctx.dev_out)) {
1318 irq = device_irq_number(ctx.device);
1319 device_set_irq_affinity_list(irq, ctx.cpu_start,
1320 ctx.cpu_start + ctx.cpu_num - 1);
1323 stats = setup_shared_var(ctx.cpu_num);
1326 if (ctx.verbose)
1327 printf("Start %u worker processes on cpus [%u-%u].\n",
1328 ctx.cpu_num, ctx.cpu_start, ctx.cpu_start + ctx.cpu_num - 1);
1329 for (i = 0; i < ctx.cpu_num; i++) {
1330 pid_t pid = fork();
1332 switch (pid) {
1333 case 0:
1334 if (reseed)
1335 seed = generate_srand_seed();
1336 srand(seed);
1338 cpu_affinity(ctx.cpu_start + i);
1339 main_loop(&ctx, confname, slow, i, invoke_cpp,
1340 cpp_argv, orig_num);
1342 goto thread_out;
1343 case -1:
1344 panic("Cannot fork processes!\n");
1348 for (i = 0; i < ctx.cpu_num; i++) {
1349 int status;
1351 wait(&status);
1352 if (WEXITSTATUS(status) == EXIT_FAILURE)
1353 die();
1356 if (set_sock_mem)
1357 reset_system_socket_memory(vals, array_size(vals));
1359 for (i = 0, tx_packets = tx_bytes = 0; i < ctx.cpu_num; i++) {
1360 while ((__get_state(i) & CPU_STATS_STATE_RES) == 0)
1361 sched_yield();
1363 tx_packets += stats[i].tx_packets;
1364 tx_bytes += stats[i].tx_bytes;
1367 fflush(stdout);
1368 printf("\n");
1369 printf("\r%12llu packets outgoing\n", tx_packets);
1370 printf("\r%12llu bytes outgoing\n", tx_bytes);
1371 for (i = 0; cpustats && i < ctx.cpu_num; i++) {
1372 printf("\r%12lu sec, %lu usec on CPU%d (%llu packets)\n",
1373 stats[i].tv_sec, stats[i].tv_usec, i,
1374 stats[i].tx_packets);
1377 thread_out:
1378 xunlockme();
1379 destroy_shared_var(stats, ctx.cpu_num);
1380 if (dev_io_is_netdev(ctx.dev_out) && set_irq_aff)
1381 device_restore_irq_affinity_list();
1383 argv_free(cpp_argv);
1384 free(ctx.device);
1385 free(ctx.rhost);
1386 free(confname);
1387 free(ctx.packet_str);
1389 return 0;