Skip to content

Commit

Permalink
Use pcap-thread timed run to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jelu committed Jan 16, 2017
1 parent 0b44240 commit 15a1dc0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 77 deletions.
37 changes: 6 additions & 31 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ main(int argc, char *argv[])
#endif
int err;
struct timeval now;
size_t usec_adjust;

progname = xstrdup(strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0]);
if (NULL == progname)
Expand Down Expand Up @@ -531,43 +530,19 @@ main(int argc, char *argv[])
dsyslog(LOG_INFO, "Running");

do {
usec_adjust = 0;

useArena(); /* Initialize a memory arena for data collection. */
if (break_start.tv_sec > 0) {
if (debug_flag && break_start.tv_sec > 0) {
gettimeofday(&now, NULL);
/* TODO: Remove before release */
dsyslogf(LOG_INFO, "break start %lu.%lu, now %lu.%lu", break_start.tv_sec, break_start.tv_usec, now.tv_sec, now.tv_usec);

if (now.tv_sec == break_start.tv_sec) {
if (break_start.tv_usec < now.tv_usec)
usec_adjust = now.tv_usec - break_start.tv_usec;
else if (break_start.tv_usec > now.tv_usec)
dsyslog(LOG_INFO, "Time shift detected, not adjusting for inter-run processing");
}
else if (now.tv_sec > break_start.tv_sec) {
usec_adjust = now.tv_sec - break_start.tv_sec;
if (usec_adjust > 1) {
dsyslogf(LOG_WARNING, "Large time shift detected, >%lu seconds, not adjusting for inter-run processing", usec_adjust);
usec_adjust = 0;
}
else {
usec_adjust = 1000000 - break_start.tv_usec + now.tv_usec;
if (usec_adjust > 950000) {
dsyslogf(LOG_INFO, "Inter-run processing is taking too long, %lu useconds, limiting adjustment", usec_adjust);
usec_adjust = 950000;
}
}
}
else
dsyslog(LOG_INFO, "Time shift detected, not adjusting for inter-run processing");
dsyslogf(LOG_INFO, "inter-run processing delay: %ld ms",
(now.tv_usec - break_start.tv_usec) / 1000 + 1000 * (now.tv_sec - break_start.tv_sec));
}

/* Indicate we might have reports to dump on exit */
have_reports = 1;

result = Pcap_run(usec_adjust);
gettimeofday(&break_start, NULL);
result = Pcap_run();
if (debug_flag)
gettimeofday(&break_start, NULL);

if (0 == fork()) {
struct sigaction action;
Expand Down
2 changes: 1 addition & 1 deletion src/pcap-thread
Submodule pcap-thread updated 2 files
+30 −2 pcap_thread.c
+6 −3 pcap_thread.h
56 changes: 12 additions & 44 deletions src/pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -931,11 +931,10 @@ void _stats(u_char* user, const struct pcap_stat* stats, const char* name, int d
}

int
Pcap_run(const size_t usec_adjust)
Pcap_run(void)
{
int i, err;
extern uint64_t statistics_interval;
struct timeval timedrun = { 0, 0 };

for (i = 0; i < n_interfaces; i++)
interfaces[i].pkts_captured = 0;
Expand Down Expand Up @@ -1011,52 +1010,14 @@ Pcap_run(const size_t usec_adjust)
gettimeofday(&last_ts, NULL);
finish_ts.tv_sec = ((start_ts.tv_sec / statistics_interval) + 1) * statistics_interval;
finish_ts.tv_usec = 0;

/*
* Calculate timed run, add a second to start_ts because we wait
* the remaining useconds
*/
if ((start_ts.tv_sec + 1) % statistics_interval)
timedrun.tv_sec = statistics_interval - ((start_ts.tv_sec + 1) % statistics_interval);
else
timedrun.tv_sec = 0;
if (start_ts.tv_usec < 1000000)
timedrun.tv_usec = 1000000 - start_ts.tv_usec;
else
timedrun.tv_usec = 0;

/* TODO: Remove before release */
dsyslogf(LOG_INFO, "Timed run for %lu.%lu seconds", timedrun.tv_sec, timedrun.tv_usec);

/*
* Adjust for inter-run processing time
*/
if (usec_adjust < 1000000) {
if (timedrun.tv_usec < usec_adjust) {
if (timedrun.tv_sec) {
timedrun.tv_sec--;
timedrun.tv_usec = 1000000 - (usec_adjust - timedrun.tv_usec);
}
else {
timedrun.tv_sec = 0;
timedrun.tv_usec = 0;
}
}
else
timedrun.tv_usec -= usec_adjust;

/* TODO: Remove before release */
dsyslogf(LOG_INFO, "Adjusted to %lu.%lu seconds", timedrun.tv_sec, timedrun.tv_usec);
}
else {
dsyslogf(LOG_ERR, "Adjustment invalid, %lu larger then 999999 useconds", usec_adjust);
}

if ((err = pcap_thread_set_timedrun(&pcap_thread, timedrun))) {
if ((err = pcap_thread_set_timedrun_to(&pcap_thread, finish_ts))) {
dsyslogf(LOG_ERR, "unable to set pcap thread timed run: %s", pcap_thread_strerr(err));
exit(1);
}

/* TODO: Remove before release */
dsyslogf(LOG_INFO, "Timed run set to %lu.%lu, now %lu.%lu", finish_ts.tv_sec, finish_ts.tv_usec, start_ts.tv_sec, start_ts.tv_usec);

if ((err = pcap_thread_run(&pcap_thread))) {
dsyslogf(LOG_ERR, "unable to pcap thread run: %s", pcap_thread_strerr(err));
if (err == PCAP_THREAD_EPCAP) {
Expand All @@ -1077,6 +1038,13 @@ Pcap_run(const size_t usec_adjust)
exit(1);
}

/* TODO: Remove before release */
{
struct timeval now = { 0, 0 };
gettimeofday(&now, 0);
dsyslogf(LOG_INFO, "Ran to %lu.%lu", now.tv_sec, now.tv_usec);
}

if ((err = pcap_thread_stats(&pcap_thread, _stats, 0))) {
dsyslogf(LOG_ERR, "unable to get pcap thread stats: %s", pcap_thread_strerr(err));
if (err == PCAP_THREAD_EPCAP) {
Expand Down
2 changes: 1 addition & 1 deletion src/pcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#define __dsc_pcap_h

void Pcap_init(const char *device, int promisc, int monitor, int immediate, int threads, int buffer_size);
int Pcap_run(const size_t usec_adjust);
int Pcap_run();
void Pcap_stop(void);
void Pcap_close(void);
int Pcap_start_time(void);
Expand Down

0 comments on commit 15a1dc0

Please sign in to comment.