[PACKET_IO] finish pcap_file mode

This commit is contained in:
liuwentan
2022-08-05 11:03:11 +08:00
parent 2cccb3ec04
commit 9cf895fa07
19 changed files with 773 additions and 51 deletions

View File

@@ -12,8 +12,10 @@
#include <limits.h>
#include <stdint.h>
#include <time.h>
#define DEV_MAX_CNT 64
#define STR_MAX_LEN 1024
enum packet_io_run_mode {
PACKET_IO_RUN_MODE_PCAP_FILE,
@@ -42,6 +44,19 @@ struct packet_io_config {
/* device counts */
uint32_t dev_cnt;
/* bpf filter string, such as "tcp and port 25"*/
char bpf_string[STR_MAX_LEN];
/* delete after the pcap file is read */
bool should_delete;
/* loop read pcap file in directory */
bool should_loop;
time_t delay;
time_t poll_interval;
};
struct lib_config {

View File

@@ -0,0 +1,74 @@
/*
**********************************************************************************************
* File: packet_queue.cpp
* Description:
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#include "../../sdk/include/utils.h"
#include "packet_queue.h"
void packet_enqueue(struct packet_queue *q, struct packet *p)
{
if (nullptr == p)
return;
/* more packets in queue */
if (q->top != nullptr) {
p->prev = nullptr;
p->next = q->top;
q->top->prev = p;
q->top = p;
/* only packet */
} else {
p->prev = nullptr;
p->next = nullptr;
q->top = p;
q->bot = p;
}
q->len++;
}
struct packet *packet_dequeue(struct packet_queue *q)
{
struct packet *p = NULL;
/* if the queue is empty there are no packets left. */
if (q->len == 0) {
return nullptr;
}
q->len--;
/* pull the bottom packet from the queue */
p = q->bot;
/* more packets in queue */
if (q->bot->prev != nullptr) {
q->bot = q->bot->prev;
q->bot->next = nullptr;
/* just the one we remove, so now empty */
} else {
q->top = nullptr;
q->bot = nullptr;
}
p->next = nullptr;
p->prev = nullptr;
return p;
}
void release_packet_queue(struct packet_queue *q)
{
if (nullptr == q) {
return;
}
while (q->len != 0) {
struct packet *p = packet_dequeue(q);
FREE(p);
}
}

24
src/common/packet_queue.h Normal file
View File

@@ -0,0 +1,24 @@
/*
**********************************************************************************************
* File: packet_queue.h
* Description: packet queue structure and api
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#pragma once
#include "../../sdk/include/packet.h"
struct packet_queue {
struct packet *top;
struct packet *bot;
uint32_t len;
pthread_mutex_t mutex_q;
};
void packet_enqueue(struct packet_queue *, struct packet *);
struct packet *packet_dequeue(struct packet_queue *);
void release_packet_queue(struct packet_queue *);

View File

@@ -0,0 +1,50 @@
/*
**********************************************************************************************
* File: time_help.cpp
* Description:
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#include <sys/time.h>
#include "time_helper.h"
void get_current_timespec(struct timespec *tm)
{
struct timeval now;
if(gettimeofday(&now, NULL) == 0) {
tm->tv_sec = now.tv_sec;
tm->tv_nsec = now.tv_usec * 1000L;
}
}
int compare_timespec(struct timespec *left, struct timespec *right)
{
if (left->tv_sec < right->tv_sec) {
return -1;
} else if (left->tv_sec > right->tv_sec) {
return 1;
} else {
if (left->tv_nsec < right->tv_nsec) {
return -1;
} else if (left->tv_nsec > right->tv_nsec) {
return 1;
} else {
return 0;
}
}
}
void copy_timespec(struct timespec *from, struct timespec *to)
{
to->tv_sec = from->tv_sec;
to->tv_nsec = from->tv_nsec;
}
uint64_t timespec_to_millisecond(const struct timespec* ts)
{
return ts->tv_sec * 1000L + ts->tv_nsec / 1000000L;
}

22
src/common/time_helper.h Normal file
View File

@@ -0,0 +1,22 @@
/*
**********************************************************************************************
* File: time_help.h
* Description: api about time
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#pragma once
#include <time.h>
#include <stdint.h>
void get_current_timespec(struct timespec *tm);
int compare_timespec(struct timespec *left, struct timespec *right);
void copy_timespec(struct timespec *from, struct timespec *to);
uint64_t timespec_to_millisecond(const struct timespec* ts);