2024-02-28 16:30:03 +08:00
|
|
|
#include <pcap/pcap.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "stellar.h"
|
|
|
|
|
#include "file_scan.h"
|
|
|
|
|
#include "packet_io.h"
|
|
|
|
|
#include "packet_utils.h"
|
|
|
|
|
#include "packet_queue.h"
|
|
|
|
|
#include "packet_io_dumpfile.h"
|
|
|
|
|
|
|
|
|
|
#define MAX_PACKET_QUEUE_SIZE (4096 * 1000)
|
|
|
|
|
|
|
|
|
|
struct packet_io_dumpfile
|
|
|
|
|
{
|
|
|
|
|
uint8_t nr_threads;
|
|
|
|
|
char dumpfile_dir[256];
|
|
|
|
|
|
|
|
|
|
pcap_t *pcap;
|
|
|
|
|
struct packet_queue *queue[MAX_THREAD_NUM];
|
|
|
|
|
struct packet_io_stat stat;
|
|
|
|
|
uint64_t io_thread_need_exit;
|
|
|
|
|
uint64_t io_thread_is_runing;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* Private API
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
static void pcap_handle(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
|
|
|
|
|
{
|
|
|
|
|
struct packet_io_dumpfile *handle = (struct packet_io_dumpfile *)user;
|
|
|
|
|
|
|
|
|
|
struct packet *pkt = packet_new(h->caplen);
|
|
|
|
|
if (pkt == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to alloc packet");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy((char *)pkt->data_ptr, bytes, h->caplen);
|
|
|
|
|
packet_parse(pkt, pkt->data_ptr, h->caplen);
|
|
|
|
|
|
|
|
|
|
uint64_t hash = packet_get_hash(pkt, LDBC_METHOD_HASH_INT_IP_AND_EXT_IP, 0);
|
|
|
|
|
struct packet_queue *queue = handle->queue[hash % handle->nr_threads];
|
|
|
|
|
packet_queue_push(queue, pkt);
|
|
|
|
|
|
|
|
|
|
if (ATOMIC_READ(&handle->io_thread_need_exit))
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile io thread need exit");
|
|
|
|
|
pcap_breakloop(handle->pcap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int dumpfile_handle(const char *file, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct packet_io_dumpfile *handle = (struct packet_io_dumpfile *)arg;
|
|
|
|
|
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile %s inprocessing", file)
|
|
|
|
|
|
|
|
|
|
handle->pcap = pcap_open_offline(file, NULL);
|
|
|
|
|
if (handle->pcap == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to open pcap file: %s", file);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
pcap_loop(handle->pcap, -1, pcap_handle, (u_char *)handle);
|
|
|
|
|
pcap_close(handle->pcap);
|
|
|
|
|
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile %s processed", file)
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void *dumpfile_thread_cycle(void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct packet_io_dumpfile *handle = (struct packet_io_dumpfile *)arg;
|
|
|
|
|
|
|
|
|
|
ATOMIC_SET(&handle->io_thread_is_runing, 1);
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile io thread is running");
|
|
|
|
|
|
|
|
|
|
file_scan(handle->dumpfile_dir, dumpfile_handle, arg);
|
|
|
|
|
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile io thread is exiting");
|
|
|
|
|
ATOMIC_SET(&handle->io_thread_is_runing, 0);
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* Public API
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-03-08 14:51:21 +08:00
|
|
|
struct packet_io_dumpfile *packet_io_dumpfile_new(struct packet_io_dumpfile_opts *opts)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
pthread_t tid;
|
|
|
|
|
struct packet_io_dumpfile *handle = (struct packet_io_dumpfile *)calloc(1, sizeof(struct packet_io_dumpfile));
|
|
|
|
|
if (handle == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to allocate memory for packet_io_dumpfile");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 14:51:21 +08:00
|
|
|
handle->nr_threads = opts->nr_threads;
|
|
|
|
|
strncpy(handle->dumpfile_dir, opts->dumpfile_dir, sizeof(handle->dumpfile_dir));
|
2024-02-28 16:30:03 +08:00
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < handle->nr_threads; i++)
|
|
|
|
|
{
|
2024-03-08 14:20:36 +08:00
|
|
|
handle->queue[i] = packet_queue_new(MAX_PACKET_QUEUE_SIZE);
|
2024-02-28 16:30:03 +08:00
|
|
|
if (handle->queue[i] == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to create packet queue");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pthread_create(&tid, NULL, dumpfile_thread_cycle, (void *)handle) != 0)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to create packet io thread");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
|
|
|
|
|
error_out:
|
2024-03-08 14:20:36 +08:00
|
|
|
packet_io_dumpfile_free(handle);
|
2024-02-28 16:30:03 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 14:20:36 +08:00
|
|
|
void packet_io_dumpfile_free(struct packet_io_dumpfile *handle)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
if (handle)
|
|
|
|
|
{
|
|
|
|
|
ATOMIC_SET(&handle->io_thread_need_exit, 1);
|
|
|
|
|
|
|
|
|
|
while (ATOMIC_READ(&handle->io_thread_is_runing))
|
|
|
|
|
{
|
|
|
|
|
usleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < handle->nr_threads; i++)
|
|
|
|
|
{
|
2024-03-08 14:20:36 +08:00
|
|
|
packet_queue_free(handle->queue[i]);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
free(handle);
|
|
|
|
|
handle = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct packet_io_stat *packet_io_dumpfile_stat(struct packet_io_dumpfile *handle)
|
|
|
|
|
{
|
|
|
|
|
return &handle->stat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int packet_io_dumpfile_init(struct packet_io_dumpfile *handle, uint16_t thread_id)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
int packet_io_dumpfile_recv(struct packet_io_dumpfile *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
struct packet_queue *queue = handle->queue[thread_id];
|
2024-03-08 18:10:38 +08:00
|
|
|
struct packet *pkt = NULL;
|
|
|
|
|
int nr_parsed = 0;
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
for (int i = 0; i < nr_pkts; i++)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-03-08 18:10:38 +08:00
|
|
|
packet_queue_pop(queue, &pkt);
|
|
|
|
|
if (pkt == NULL)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ATOMIC_ADD(&handle->stat.rx_pkts, 1);
|
|
|
|
|
ATOMIC_ADD(&handle->stat.rx_bytes, packet_get_len(pkt));
|
|
|
|
|
|
|
|
|
|
struct packet *temp = &pkts[nr_parsed++];
|
|
|
|
|
memset(temp, 0, sizeof(struct packet));
|
|
|
|
|
packet_parse(temp, pkt->data_ptr, pkt->data_len);
|
|
|
|
|
packet_set_io_ctx(temp, pkt);
|
|
|
|
|
packet_set_type(temp, PACKET_TYPE_DATA);
|
|
|
|
|
packet_set_action(temp, PACKET_ACTION_FORWARD);
|
|
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
2024-03-08 18:10:38 +08:00
|
|
|
|
|
|
|
|
return nr_parsed;
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
void packet_io_dumpfile_send(struct packet_io_dumpfile *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-03-08 18:10:38 +08:00
|
|
|
struct packet *pkt = NULL;
|
|
|
|
|
for (int i = 0; i < nr_pkts; i++)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-03-08 18:10:38 +08:00
|
|
|
pkt = &pkts[i];
|
|
|
|
|
|
|
|
|
|
if (packet_get_action(pkt) == PACKET_ACTION_DROP)
|
|
|
|
|
{
|
|
|
|
|
ATOMIC_ADD(&handle->stat.drop_pkts, 1);
|
|
|
|
|
ATOMIC_ADD(&handle->stat.drop_bytes, packet_get_len(pkt));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ATOMIC_ADD(&handle->stat.tx_pkts, 1);
|
|
|
|
|
ATOMIC_ADD(&handle->stat.tx_bytes, packet_get_len(pkt));
|
|
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
packet_free((struct packet *)packet_get_io_ctx(pkt));
|
|
|
|
|
packet_free(pkt);
|
|
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|