aboutsummaryrefslogtreecommitdiffstats
path: root/tests/osmo-pcap-test
diff options
context:
space:
mode:
Diffstat (limited to 'tests/osmo-pcap-test')
-rw-r--r--tests/osmo-pcap-test/osmo_pcap.h4
-rw-r--r--tests/osmo-pcap-test/pcap.c29
2 files changed, 26 insertions, 7 deletions
diff --git a/tests/osmo-pcap-test/osmo_pcap.h b/tests/osmo-pcap-test/osmo_pcap.h
index 171360d..d10a7f7 100644
--- a/tests/osmo-pcap-test/osmo_pcap.h
+++ b/tests/osmo-pcap-test/osmo_pcap.h
@@ -3,6 +3,7 @@
#include <pcap.h>
#include <osmocom/core/timer.h>
+#include <time.h>
struct msgb;
@@ -11,7 +12,8 @@ void osmo_pcap_init(void);
struct osmo_pcap {
pcap_t *h;
struct osmo_timer_list timer;
- struct timeval last;
+ struct timespec start_sys;
+ struct timeval start_pcap;
struct msgb *deliver_msg;
};
diff --git a/tests/osmo-pcap-test/pcap.c b/tests/osmo-pcap-test/pcap.c
index f9304b9..a67b121 100644
--- a/tests/osmo-pcap-test/pcap.c
+++ b/tests/osmo-pcap-test/pcap.c
@@ -18,6 +18,7 @@
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
+#include <sys/time.h>
#include <linux/if_ether.h>
@@ -26,6 +27,7 @@
#include <osmocom/core/msgb.h>
#include <osmocom/core/timer.h>
+#include <osmocom/core/timer_compat.h>
#include <osmocom/core/select.h>
#include <osmocom/netif/osmux.h>
@@ -108,7 +110,8 @@ osmo_pcap_test_run(struct osmo_pcap *p, uint8_t pnum, int (*cb)(struct msgb *msg
struct osmo_pcap_proto_l4 *l4h;
struct pcap_pkthdr pcaph;
const u_char *l2pkt, *l3pkt;
- struct timeval res;
+ struct timespec now_ts, elapsed_sys_ts;
+ struct timeval res, elapsed_pcap, elapsed_sys;
uint8_t l4protonum;
if (p->deliver_msg) {
@@ -152,21 +155,35 @@ retry:
}
/* first packet that is going to be processed */
- if (osmo_pcap_test_stats.processed == 0)
- memcpy(&p->last, &pcaph.ts, sizeof(struct timeval));
+ if (osmo_pcap_test_stats.processed == 0) {
+ if (clock_gettime(CLOCK_MONOTONIC, &p->start_sys) < 0)
+ return -1;
+ memcpy(&p->start_pcap, &pcaph.ts, sizeof(struct timeval));
+ }
/* retry with next packet if this has been skipped. */
if (osmo_pcap_process_packet(&p->deliver_msg, l2pkt, pcaph.caplen, l2h, l3h, l4h, cb) < 0)
goto retry;
/* calculate waiting time */
- timersub(&pcaph.ts, &p->last, &res);
+ timersub(&pcaph.ts, &p->start_pcap, &elapsed_pcap);
+ if (clock_gettime(CLOCK_MONOTONIC, &now_ts) < 0)
+ return -1;
+ timespecsub(&now_ts, &p->start_sys, &elapsed_sys_ts);
+ elapsed_sys.tv_sec = elapsed_sys_ts.tv_sec;
+ elapsed_sys.tv_usec = elapsed_sys_ts.tv_nsec / 1000;
+
+ if (timercmp(&elapsed_sys, &elapsed_pcap, >)) {
+ printf("We are late!\n");
+ res.tv_sec = 0;
+ res.tv_usec = 0;
+ } else {
+ timersub(&elapsed_pcap, &elapsed_sys, &res);
+ }
printf("next packet comes in %lu.%.6lu seconds\n",
res.tv_sec, res.tv_usec);
osmo_timer_schedule(&p->timer, res.tv_sec, res.tv_usec);
- memcpy(&p->last, &pcaph.ts, sizeof(struct timeval));
-
return 0;
}