[netsniff-ng] Re: [PATCH v2 0/3] trafgen: Send packets from pcap file
On 2017-02-07 at 09:15:55 +0100, Vadim Kochan wrote: > Allow to send packets at specified rate from ".pcap" file > which might be speified via -i,--in options. > > By default packets will be send at original rate considering packet's > timestamp. > > v2: > 1) Use timespec in pcap_get_tstamp(...) > 2) Use ctx.pcap_in char * member to check if input pcap >file was specified. > 3) Replace strcmp("0", ...) to 'if (!rate)' > > BTW, shaper still works at usec minimum time precision so I did not convert > it to nsec yet, but I plan to do it in future patches if it is OK. Sure, this is fine. Thanks a lot for the updated patch set. -- You received this message because you are subscribed to the Google Groups "netsniff-ng" group. To unsubscribe from this group and stop receiving emails from it, send an email to netsniff-ng+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[netsniff-ng] [PATCH v2 1/3] pcap_io: Add function to get packet timestamp
Add pcap_get_tstamp(...) function to get packet's timestamp considering different packet types & bytes order. Signed-off-by: Vadim Kochan --- pcap_io.h | 53 + 1 file changed, 53 insertions(+) diff --git a/pcap_io.h b/pcap_io.h index 4e41362..3d70b21 100644 --- a/pcap_io.h +++ b/pcap_io.h @@ -373,6 +373,59 @@ static inline void pcap_set_length(pcap_pkthdr_t *phdr, enum pcap_type type, u32 } } +static inline void pcap_get_tstamp(pcap_pkthdr_t *phdr, enum pcap_type type, + struct timespec *ts) +{ + switch (type) { + case DEFAULT: + case DEFAULT_LL: + ts->tv_sec = phdr->ppo.ts.tv_sec; + ts->tv_nsec = phdr->ppo.ts.tv_usec * 1000; + break; + + case DEFAULT_SWAPPED: + case DEFAULT_LL_SWAPPED: + ts->tv_sec = ___constant_swab32(phdr->ppo.ts.tv_sec); + ts->tv_nsec = ___constant_swab32(phdr->ppo.ts.tv_usec) * 1000;; + break; + + case NSEC: + case NSEC_LL: + ts->tv_sec = phdr->ppn.ts.tv_sec; + ts->tv_nsec = phdr->ppn.ts.tv_nsec / 1000; + break; + + case NSEC_SWAPPED: + case NSEC_LL_SWAPPED: + ts->tv_sec = ___constant_swab32(phdr->ppn.ts.tv_sec); + ts->tv_nsec = ___constant_swab32(phdr->ppn.ts.tv_nsec); + break; + + case KUZNETZOV: + ts->tv_sec = phdr->ppk.ts.tv_sec; + ts->tv_nsec = phdr->ppk.ts.tv_usec; + break; + + case KUZNETZOV_SWAPPED: + ts->tv_sec = ___constant_swab32(phdr->ppk.ts.tv_sec); + ts->tv_nsec = ___constant_swab32(phdr->ppk.ts.tv_usec); + break; + + case BORKMANN: + ts->tv_sec = phdr->ppb.ts.tv_sec; + ts->tv_nsec = phdr->ppb.ts.tv_nsec; + break; + + case BORKMANN_SWAPPED: + ts->tv_sec = ___constant_swab32(phdr->ppb.ts.tv_sec); + ts->tv_nsec = ___constant_swab32(phdr->ppb.ts.tv_nsec); + break; + + default: + bug(); + } +} + static inline u32 pcap_get_hdr_length(pcap_pkthdr_t *phdr, enum pcap_type type) { switch (type) { -- 2.11.0 -- You received this message because you are subscribed to the Google Groups "netsniff-ng" group. To unsubscribe from this group and stop receiving emails from it, send an email to netsniff-ng+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[netsniff-ng] [PATCH v2 3/3] trafgen: man: Add description with pcap file for -i, --in option
Update -i, --in option with pcap file as input parameter. Signed-off-by: Vadim Kochan --- trafgen.8 | 5 - 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/trafgen.8 b/trafgen.8 index e09b7a0..fd9788a 100644 --- a/trafgen.8 +++ b/trafgen.8 @@ -66,10 +66,13 @@ given scenario. .PP .SH OPTIONS .PP -.SS -i , -c , --in , --conf +.SS -i , -c , --in , --conf Defines the input configuration file that can either be passed as a normal plain text file or via stdin (''-''). Note that currently, if a configuration is passed through stdin, only 1 CPU will be used. +It is also possible to specify PCAP file with .pcap extension via -i,--in option, +by default packets will be sent at rate considering timestamp from PCAP file which +might be reset via -b/-t options. .PP .SS -o , -d , --out , --dev Defines the outgoing networking device such as eth0, wlan0 and others. -- 2.11.0 -- You received this message because you are subscribed to the Google Groups "netsniff-ng" group. To unsubscribe from this group and stop receiving emails from it, send an email to netsniff-ng+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[netsniff-ng] [PATCH v2 0/3] trafgen: Send packets from pcap file
Allow to send packets at specified rate from ".pcap" file which might be speified via -i,--in options. By default packets will be send at original rate considering packet's timestamp. v2: 1) Use timespec in pcap_get_tstamp(...) 2) Use ctx.pcap_in char * member to check if input pcap file was specified. 3) Replace strcmp("0", ...) to 'if (!rate)' BTW, shaper still works at usec minimum time precision so I did not convert it to nsec yet, but I plan to do it in future patches if it is OK. Vadim Kochan (3): pcap_io: Add function to get packet timestamp trafgen: Allow send packets from pcap file trafgen: man: Add description with pcap file for -i, --in option pcap_io.h| 53 ++ trafgen.8| 5 +- trafgen.c| 166 +++ trafgen/Makefile | 4 ++ trafgen_conf.h | 3 + trafgen_parser.y | 2 +- 6 files changed, 197 insertions(+), 36 deletions(-) -- 2.11.0 -- You received this message because you are subscribed to the Google Groups "netsniff-ng" group. To unsubscribe from this group and stop receiving emails from it, send an email to netsniff-ng+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[netsniff-ng] [PATCH v2 2/3] trafgen: Allow send packets from pcap file
Add ability to send packets from pcap file if it has ".pcap" extension via "-i,--in" option. By default packet sending is delayed considering original packets timestamps if no rate or delay is specified via -b/-t options. Signed-off-by: Vadim Kochan --- trafgen.c| 166 +++ trafgen/Makefile | 4 ++ trafgen_conf.h | 3 + trafgen_parser.y | 2 +- 4 files changed, 140 insertions(+), 35 deletions(-) diff --git a/trafgen.c b/trafgen.c index 524b260..b25760f 100644 --- a/trafgen.c +++ b/trafgen.c @@ -5,6 +5,8 @@ * Subject to the GPL, version 2. */ +#define _GNU_SOURCE + #include #include #include @@ -16,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -55,27 +56,24 @@ #include "ring_tx.h" #include "csum.h" #include "trafgen_proto.h" - -#ifndef timeval_to_timespec -#define timeval_to_timespec(tv, ts) { \ - (ts)->tv_sec = (tv)->tv_sec; \ - (ts)->tv_nsec = (tv)->tv_usec * 1000; \ -} -#endif +#include "pcap_io.h" enum shaper_type { SHAPER_NONE, + SHAPER_DELAY, SHAPER_PKTS, SHAPER_BYTES, + SHAPER_TSTAMP, }; struct shaper { enum shaper_type type; unsigned long long sent; unsigned long long rate; + struct timeval tstamp; + struct timespec delay; struct timeval start; struct timeval end; - struct timespec delay; }; struct ctx { @@ -88,6 +86,7 @@ struct ctx { struct sockaddr_in dest; struct shaper sh; char *packet_str; + char *pcap_in; }; struct cpu_stats { @@ -556,15 +555,12 @@ static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx) static bool shaper_is_set(struct shaper *sh) { - if ((sh->delay.tv_sec | sh->delay.tv_nsec) > 0) - return true; - - return sh->type != SHAPER_NONE; + return sh->type != SHAPER_NONE; } static void shaper_init(struct shaper *sh) { - if (sh->type == SHAPER_NONE) + if (sh->type == SHAPER_NONE || sh->type == SHAPER_DELAY) return; memset(&sh->delay, 0, sizeof(struct timespec)); @@ -574,6 +570,12 @@ static void shaper_init(struct shaper *sh) static void shaper_set_delay(struct shaper *sh, time_t sec, long int ns) { + if (!(sec | ns)) { + sh->type = SHAPER_NONE; + return; + } + + sh->type = SHAPER_DELAY; sh->delay.tv_sec = sec; sh->delay.tv_nsec = ns; } @@ -586,23 +588,49 @@ static void shaper_set_rate(struct shaper *sh, unsigned long long rate, sh->type = type; } -static void shaper_delay(struct shaper *sh, unsigned long pkt_len) +static void shaper_set_tstamp(struct shaper *sh, struct timespec *ts) +{ + TIMESPEC_TO_TIMEVAL(&sh->tstamp, ts); +} + +static void shaper_delay(struct shaper *sh, struct packet *pkt) { - if (sh->type != SHAPER_NONE) + if (sh->type == SHAPER_BYTES || sh->type == SHAPER_PKTS) { + unsigned long pkt_len = pkt->len; + sh->sent += sh->type == SHAPER_BYTES ? pkt_len : 1; - if (sh->sent >= sh->rate && sh->rate > 0) { - struct timeval delay_us; - struct timeval time_sent; - struct timeval time_1s = { .tv_sec = 1 }; + if (sh->sent >= sh->rate && sh->rate > 0) { + struct timeval delay_us; + struct timeval time_sent; + struct timeval time_1s = { .tv_sec = 1 }; + + bug_on(gettimeofday(&sh->end, NULL)); + timersub(&sh->end, &sh->start, &time_sent); + + if (timercmp(&time_1s, &time_sent, > )) { + timersub(&time_1s, &time_sent, &delay_us); + TIMEVAL_TO_TIMESPEC(&delay_us, &sh->delay); + } + } + } else if (sh->type == SHAPER_TSTAMP) { + struct timeval tstamp; + struct timeval pkt_diff; + struct timeval diff; bug_on(gettimeofday(&sh->end, NULL)); - timersub(&sh->end, &sh->start, &time_sent); + TIMESPEC_TO_TIMEVAL(&tstamp, &pkt->tstamp); + timersub(&sh->end, &sh->start, &diff); + timersub(&tstamp, &sh->tstamp, &pkt_diff); + + if (timercmp(&diff, &pkt_diff, <)) { + struct timeval delay; - if (timercmp(&time_1s, &time_sent, > )) { - timersub(&time_1s, &time_sent, &delay_us); - timeval_to_timespec(&delay_us, &sh->delay); + timersub(&pkt_diff, &diff, &delay); + TIMEVAL_TO_TIMESPEC(&delay, &sh->delay); } + + memcpy(&sh->tstamp, &tstamp, sizeof(sh->tstamp)); } if ((sh->delay.tv_sec | sh->delay.tv_nsec) > 0)