2024-02-28 16:30:03 +08:00
|
|
|
#include <pcap/pcap.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <unistd.h>
|
2024-03-09 19:28:14 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
2024-04-30 15:47:45 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-06-24 17:07:05 +08:00
|
|
|
#include "log.h"
|
|
|
|
|
#include "tuple.h"
|
2024-08-16 10:43:00 +08:00
|
|
|
#include "utils.h"
|
2024-04-30 15:47:45 +08:00
|
|
|
#include "dumpfile_io.h"
|
2024-08-16 15:13:37 +08:00
|
|
|
#include "packet_private.h"
|
2024-08-16 11:49:54 +08:00
|
|
|
#include "packet_parser.h"
|
2024-07-01 15:51:36 +08:00
|
|
|
#include "packet_dump.h"
|
2024-03-09 19:28:14 +08:00
|
|
|
#include "lock_free_queue.h"
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-06-27 15:07:54 +08:00
|
|
|
#define PACKET_IO_LOG_STATE(format, ...) LOG_STATE("dumpfile", format, ##__VA_ARGS__)
|
|
|
|
|
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("dumpfile", format, ##__VA_ARGS__)
|
2024-06-24 17:07:05 +08:00
|
|
|
|
2024-02-28 16:30:03 +08:00
|
|
|
#define MAX_PACKET_QUEUE_SIZE (4096 * 1000)
|
|
|
|
|
|
2024-04-10 17:50:51 +08:00
|
|
|
struct dumpfile_io
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-04-25 16:48:50 +08:00
|
|
|
uint16_t nr_threads;
|
2024-05-17 19:50:51 +08:00
|
|
|
char work_dir[256];
|
2024-04-10 17:50:51 +08:00
|
|
|
char directory[256];
|
2024-02-28 16:30:03 +08:00
|
|
|
|
|
|
|
|
pcap_t *pcap;
|
2024-03-09 19:28:14 +08:00
|
|
|
struct lock_free_queue *queue[MAX_THREAD_NUM];
|
2024-06-25 14:08:33 +08:00
|
|
|
struct packet_io_stat stat[MAX_THREAD_NUM];
|
2024-02-28 16:30:03 +08:00
|
|
|
uint64_t io_thread_need_exit;
|
|
|
|
|
uint64_t io_thread_is_runing;
|
2024-05-15 11:40:00 +08:00
|
|
|
uint64_t io_thread_wait_exit;
|
2024-02-28 16:30:03 +08:00
|
|
|
};
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
struct pcap_pkt
|
|
|
|
|
{
|
|
|
|
|
char *data;
|
|
|
|
|
int len;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-28 16:30:03 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Private API
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-04-30 15:47:45 +08:00
|
|
|
typedef int file_handle(const char *file, void *arg);
|
|
|
|
|
|
|
|
|
|
static int scan_directory(const char *dir, file_handle *handler, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct stat statbuf;
|
|
|
|
|
struct dirent *entry;
|
|
|
|
|
|
|
|
|
|
DIR *dp = opendir(dir);
|
|
|
|
|
if (NULL == dp)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("opendir %s failed, %s", dir, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (chdir(dir) == -1)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("chdir %s failed, %s", dir, strerror(errno));
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while ((entry = readdir(dp)))
|
|
|
|
|
{
|
|
|
|
|
if (lstat(entry->d_name, &statbuf) == -1)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("lstat %s failed, %s", entry->d_name, strerror(errno));
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (S_IFDIR & statbuf.st_mode)
|
|
|
|
|
{
|
|
|
|
|
if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (scan_directory(entry->d_name, handler, arg) == -1)
|
|
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (handler(entry->d_name, arg) == -1)
|
|
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (chdir("..") == -1)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("chdir .. failed, %s", strerror(errno));
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir(dp);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
error_out:
|
|
|
|
|
closedir(dp);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pcap_packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-04-10 17:50:51 +08:00
|
|
|
struct dumpfile_io *handle = (struct dumpfile_io *)user;
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
// copy packet data to new memory
|
|
|
|
|
struct pcap_pkt *pcap_pkt = (struct pcap_pkt *)calloc(1, sizeof(struct pcap_pkt) + h->caplen);
|
|
|
|
|
if (pcap_pkt == NULL)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to alloc packet");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-03-09 19:28:14 +08:00
|
|
|
pcap_pkt->data = (char *)pcap_pkt + sizeof(struct pcap_pkt);
|
|
|
|
|
pcap_pkt->len = h->caplen;
|
|
|
|
|
memcpy((char *)pcap_pkt->data, bytes, h->caplen);
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
// calculate packet hash
|
|
|
|
|
struct packet pkt;
|
2024-06-14 19:24:27 +08:00
|
|
|
memset(&pkt, 0, sizeof(struct packet));
|
2024-03-09 19:28:14 +08:00
|
|
|
packet_parse(&pkt, pcap_pkt->data, pcap_pkt->len);
|
2024-08-16 17:07:52 +08:00
|
|
|
uint64_t hash = packet_ldbc_hash(&pkt, PKT_LDBC_METH_OUTERMOST_INT_EXT_IP, PACKET_DIRECTION_OUTGOING);
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
// push packet to queue
|
|
|
|
|
struct lock_free_queue *queue = handle->queue[hash % handle->nr_threads];
|
2024-04-11 19:44:02 +08:00
|
|
|
while (lock_free_queue_push(queue, pcap_pkt) == -1)
|
|
|
|
|
{
|
|
|
|
|
if (ATOMIC_READ(&handle->io_thread_need_exit))
|
|
|
|
|
{
|
|
|
|
|
free(pcap_pkt);
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile io thread need exit");
|
|
|
|
|
pcap_breakloop(handle->pcap);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
usleep(1000);
|
|
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
|
|
|
|
|
if (ATOMIC_READ(&handle->io_thread_need_exit))
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile io thread need exit");
|
|
|
|
|
pcap_breakloop(handle->pcap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 15:47:45 +08:00
|
|
|
static int dumpfile_handler(const char *file, void *arg)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-03-26 15:09:03 +08:00
|
|
|
char resolved_path[256];
|
|
|
|
|
char pcap_errbuf[PCAP_ERRBUF_SIZE];
|
2024-04-10 17:50:51 +08:00
|
|
|
struct dumpfile_io *handle = (struct dumpfile_io *)arg;
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-26 15:09:03 +08:00
|
|
|
realpath(file, resolved_path);
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile %s in-processing", resolved_path)
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-26 15:09:03 +08:00
|
|
|
handle->pcap = pcap_open_offline(file, pcap_errbuf);
|
2024-02-28 16:30:03 +08:00
|
|
|
if (handle->pcap == NULL)
|
|
|
|
|
{
|
2024-03-26 15:09:03 +08:00
|
|
|
PACKET_IO_LOG_ERROR("unable to open pcap file: %s, %s", resolved_path, pcap_errbuf);
|
2024-02-28 16:30:03 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2024-04-30 15:47:45 +08:00
|
|
|
pcap_loop(handle->pcap, -1, pcap_packet_handler, (u_char *)handle);
|
2024-02-28 16:30:03 +08:00
|
|
|
pcap_close(handle->pcap);
|
|
|
|
|
|
2024-03-26 15:09:03 +08:00
|
|
|
PACKET_IO_LOG_STATE("dumpfile %s processed", resolved_path)
|
2024-02-28 16:30:03 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-15 11:40:00 +08:00
|
|
|
static int all_packet_processed(struct dumpfile_io *handle)
|
|
|
|
|
{
|
|
|
|
|
for (uint16_t i = 0; i < handle->nr_threads; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!lock_free_queue_empty(handle->queue[i]))
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 19:44:02 +08:00
|
|
|
static void *dumpfile_thread(void *arg)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-04-10 17:50:51 +08:00
|
|
|
struct dumpfile_io *handle = (struct dumpfile_io *)arg;
|
2024-02-28 16:30:03 +08:00
|
|
|
|
|
|
|
|
ATOMIC_SET(&handle->io_thread_is_runing, 1);
|
|
|
|
|
PACKET_IO_LOG_STATE("dumpfile io thread is running");
|
|
|
|
|
|
2024-04-30 15:47:45 +08:00
|
|
|
scan_directory(handle->directory, dumpfile_handler, arg);
|
2024-08-15 19:03:48 +08:00
|
|
|
PACKET_IO_LOG_STATE("dumpfile io thread processed all pcap files");
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-27 17:11:38 +08:00
|
|
|
while (ATOMIC_READ(&handle->io_thread_need_exit) == 0)
|
|
|
|
|
{
|
2024-05-15 11:40:00 +08:00
|
|
|
if (all_packet_processed(handle))
|
|
|
|
|
{
|
|
|
|
|
ATOMIC_SET(&handle->io_thread_wait_exit, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 19:03:48 +08:00
|
|
|
usleep(1000); // 1ms
|
2024-03-27 17:11:38 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-15 19:03:48 +08:00
|
|
|
PACKET_IO_LOG_STATE("dumpfile io thread exit");
|
2024-02-28 16:30:03 +08:00
|
|
|
ATOMIC_SET(&handle->io_thread_is_runing, 0);
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* Public API
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-04-25 16:48:50 +08:00
|
|
|
struct dumpfile_io *dumpfile_io_new(const char *directory, uint16_t nr_threads)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
pthread_t tid;
|
2024-04-10 17:50:51 +08:00
|
|
|
struct dumpfile_io *handle = (struct dumpfile_io *)calloc(1, sizeof(struct dumpfile_io));
|
2024-02-28 16:30:03 +08:00
|
|
|
if (handle == NULL)
|
|
|
|
|
{
|
2024-04-10 17:50:51 +08:00
|
|
|
PACKET_IO_LOG_ERROR("unable to allocate memory for dumpfile_io");
|
2024-02-28 16:30:03 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-05-17 19:50:51 +08:00
|
|
|
if (getcwd(handle->work_dir, sizeof(handle->work_dir)) == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to get current work directory");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-04-10 17:50:51 +08:00
|
|
|
handle->nr_threads = nr_threads;
|
2024-04-11 19:44:02 +08:00
|
|
|
strncpy(handle->directory, directory, MIN(strlen(directory), sizeof(handle->directory)));
|
2024-02-28 16:30:03 +08:00
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < handle->nr_threads; i++)
|
|
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
handle->queue[i] = lock_free_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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-11 19:44:02 +08:00
|
|
|
if (pthread_create(&tid, NULL, dumpfile_thread, (void *)handle) != 0)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to create packet io thread");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
|
|
|
|
|
error_out:
|
2024-04-10 17:50:51 +08:00
|
|
|
dumpfile_io_free(handle);
|
2024-02-28 16:30:03 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 17:50:51 +08:00
|
|
|
void dumpfile_io_free(struct dumpfile_io *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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 19:44:02 +08:00
|
|
|
struct pcap_pkt *pcap_pkt = NULL;
|
2024-02-28 16:30:03 +08:00
|
|
|
for (uint16_t i = 0; i < handle->nr_threads; i++)
|
|
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
lock_free_queue_pop(handle->queue[i], (void **)&pcap_pkt);
|
|
|
|
|
if (pcap_pkt)
|
|
|
|
|
{
|
|
|
|
|
free(pcap_pkt);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
lock_free_queue_free(handle->queue[i]);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
free(handle);
|
|
|
|
|
handle = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-15 11:40:00 +08:00
|
|
|
int dumpfile_io_wait_exit(struct dumpfile_io *handle)
|
|
|
|
|
{
|
|
|
|
|
return ATOMIC_READ(&handle->io_thread_wait_exit);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 13:34:56 +08:00
|
|
|
int dumpfile_io_init(struct dumpfile_io *handle __attribute__((unused)), uint16_t thr_idx __attribute__((unused)))
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 17:50:51 +08:00
|
|
|
int dumpfile_io_ingress(struct dumpfile_io *handle, uint16_t thr_idx, struct packet *pkts, int nr_pkts)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-04-10 17:50:51 +08:00
|
|
|
struct lock_free_queue *queue = handle->queue[thr_idx];
|
2024-06-25 14:08:33 +08:00
|
|
|
struct packet_io_stat *stat = &handle->stat[thr_idx];
|
2024-03-09 19:28:14 +08:00
|
|
|
struct pcap_pkt *pcap_pkt = NULL;
|
2024-04-11 19:44:02 +08:00
|
|
|
struct packet *pkt;
|
2024-03-08 18:10:38 +08:00
|
|
|
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-09 19:28:14 +08:00
|
|
|
lock_free_queue_pop(queue, (void **)&pcap_pkt);
|
|
|
|
|
if (pcap_pkt == NULL)
|
2024-03-08 18:10:38 +08:00
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-08-16 17:44:23 +08:00
|
|
|
stat->pkts_rx++;
|
|
|
|
|
stat->bytes_rx += pcap_pkt->len;
|
2024-04-10 17:50:51 +08:00
|
|
|
|
2024-08-16 17:44:23 +08:00
|
|
|
stat->raw_pkts_rx++;
|
|
|
|
|
stat->raw_bytes_rx += pcap_pkt->len;
|
2024-03-09 19:28:14 +08:00
|
|
|
|
2024-04-11 19:44:02 +08:00
|
|
|
pkt = &pkts[nr_parsed];
|
2024-03-09 19:28:14 +08:00
|
|
|
packet_parse(pkt, pcap_pkt->data, pcap_pkt->len);
|
2024-08-14 10:50:33 +08:00
|
|
|
memset(&pkt->meta, 0, sizeof(pkt->meta));
|
2024-05-08 18:24:26 +08:00
|
|
|
packet_set_origin_ctx(pkt, pcap_pkt);
|
2024-05-09 14:57:12 +08:00
|
|
|
packet_set_action(pkt, PACKET_ACTION_FORWARD);
|
2024-04-11 19:44:02 +08:00
|
|
|
nr_parsed++;
|
2024-03-08 18:10:38 +08:00
|
|
|
}
|
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-04-10 17:50:51 +08:00
|
|
|
void dumpfile_io_egress(struct dumpfile_io *handle, uint16_t thr_idx, struct packet *pkts, int nr_pkts)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-04-10 17:50:51 +08:00
|
|
|
int len;
|
2024-03-08 18:10:38 +08:00
|
|
|
struct packet *pkt = NULL;
|
2024-06-25 14:08:33 +08:00
|
|
|
struct packet_io_stat *stat = &handle->stat[thr_idx];
|
2024-04-18 14:20:28 +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
|
|
|
pkt = &pkts[i];
|
2024-06-14 19:24:27 +08:00
|
|
|
len = packet_get_raw_len(pkt);
|
2024-03-08 18:10:38 +08:00
|
|
|
|
2024-08-16 17:44:23 +08:00
|
|
|
stat->pkts_tx++;
|
|
|
|
|
stat->bytes_tx += len;
|
2024-03-09 19:28:14 +08:00
|
|
|
|
2024-08-16 17:44:23 +08:00
|
|
|
stat->raw_pkts_tx++;
|
|
|
|
|
stat->raw_bytes_tx += len;
|
2024-03-09 19:28:14 +08:00
|
|
|
|
2024-05-08 18:24:26 +08:00
|
|
|
struct pcap_pkt *pcap_pkt = (struct pcap_pkt *)packet_get_origin_ctx(pkt);
|
2024-04-10 11:40:26 +08:00
|
|
|
if (pcap_pkt)
|
|
|
|
|
{
|
|
|
|
|
free(pcap_pkt);
|
|
|
|
|
}
|
|
|
|
|
packet_free(pkt);
|
2024-03-09 19:28:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 17:50:51 +08:00
|
|
|
void dumpfile_io_drop(struct dumpfile_io *handle, uint16_t thr_idx, struct packet *pkts, int nr_pkts)
|
2024-03-09 19:28:14 +08:00
|
|
|
{
|
|
|
|
|
struct packet *pkt = NULL;
|
2024-06-25 14:08:33 +08:00
|
|
|
struct packet_io_stat *stat = &handle->stat[thr_idx];
|
2024-04-30 15:25:34 +08:00
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
for (int i = 0; i < nr_pkts; i++)
|
|
|
|
|
{
|
|
|
|
|
pkt = &pkts[i];
|
2024-05-08 18:24:26 +08:00
|
|
|
struct pcap_pkt *pcap_pkt = (struct pcap_pkt *)packet_get_origin_ctx(pkt);
|
2024-04-10 11:40:26 +08:00
|
|
|
if (pcap_pkt)
|
|
|
|
|
{
|
2024-08-16 17:44:23 +08:00
|
|
|
stat->pkts_dropped++;
|
|
|
|
|
stat->bytes_dropped += packet_get_raw_len(pkt);
|
2024-04-10 11:40:26 +08:00
|
|
|
free(pcap_pkt);
|
|
|
|
|
}
|
2024-03-08 18:10:38 +08:00
|
|
|
packet_free(pkt);
|
|
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
2024-04-18 14:20:28 +08:00
|
|
|
|
2024-04-25 15:34:46 +08:00
|
|
|
int dumpfile_io_inject(struct dumpfile_io *handle, uint16_t thr_idx, struct packet *pkts, int nr_pkts)
|
|
|
|
|
{
|
2024-07-01 15:51:36 +08:00
|
|
|
uint16_t len;
|
2024-04-25 15:34:46 +08:00
|
|
|
struct packet *pkt = NULL;
|
2024-06-25 14:08:33 +08:00
|
|
|
struct packet_io_stat *stat = &handle->stat[thr_idx];
|
2024-07-01 15:51:36 +08:00
|
|
|
struct tuple6 tuple;
|
|
|
|
|
|
|
|
|
|
char file[1024] = {0};
|
|
|
|
|
char src_addr[INET6_ADDRSTRLEN] = {0};
|
|
|
|
|
char dst_addr[INET6_ADDRSTRLEN] = {0};
|
2024-04-25 15:34:46 +08:00
|
|
|
|
|
|
|
|
for (int i = 0; i < nr_pkts; i++)
|
|
|
|
|
{
|
|
|
|
|
pkt = &pkts[i];
|
2024-06-14 19:24:27 +08:00
|
|
|
len = packet_get_raw_len(pkt);
|
2024-04-25 15:34:46 +08:00
|
|
|
|
2024-08-16 17:44:23 +08:00
|
|
|
stat->pkts_injected++;
|
|
|
|
|
stat->bytes_injected += len;
|
2024-04-25 15:34:46 +08:00
|
|
|
|
2024-08-16 17:44:23 +08:00
|
|
|
stat->raw_pkts_tx++;
|
|
|
|
|
stat->raw_bytes_tx += len;
|
2024-04-25 15:34:46 +08:00
|
|
|
|
2024-08-16 17:44:23 +08:00
|
|
|
stat->pkts_tx++;
|
|
|
|
|
stat->bytes_tx += len;
|
2024-04-25 15:34:46 +08:00
|
|
|
|
2024-07-01 15:51:36 +08:00
|
|
|
memset(&tuple, 0, sizeof(struct tuple6));
|
|
|
|
|
packet_get_innermost_tuple6(pkt, &tuple);
|
2024-05-15 11:40:00 +08:00
|
|
|
|
2024-07-25 18:29:57 +08:00
|
|
|
if (tuple.addr_family == AF_INET)
|
2024-07-01 15:51:36 +08:00
|
|
|
{
|
|
|
|
|
inet_ntop(AF_INET, &tuple.src_addr.v4, src_addr, INET6_ADDRSTRLEN);
|
|
|
|
|
inet_ntop(AF_INET, &tuple.dst_addr.v4, dst_addr, INET6_ADDRSTRLEN);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
inet_ntop(AF_INET6, &tuple.src_addr.v6, src_addr, INET6_ADDRSTRLEN);
|
|
|
|
|
inet_ntop(AF_INET6, &tuple.dst_addr.v6, dst_addr, INET6_ADDRSTRLEN);
|
|
|
|
|
}
|
2024-08-16 17:44:23 +08:00
|
|
|
snprintf(file, sizeof(file), "%s/inject-%s:%u-%s:%u-%lu.pcap", handle->work_dir, src_addr, ntohs(tuple.src_port), dst_addr, ntohs(tuple.dst_port), stat->pkts_injected);
|
2024-07-01 15:51:36 +08:00
|
|
|
|
2024-07-02 17:55:55 +08:00
|
|
|
if (packet_dump_pcap(pkt, file) == -1)
|
2024-07-01 15:51:36 +08:00
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to dump pcap file: %s", file);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_STATE("dump inject packet: %s", file);
|
|
|
|
|
}
|
2024-04-25 15:34:46 +08:00
|
|
|
packet_free(pkt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nr_pkts;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 10:50:33 +08:00
|
|
|
void dumpfile_io_yield(struct dumpfile_io *handle __attribute__((unused)), uint16_t thr_idx __attribute__((unused)), uint64_t timeout_ms __attribute__((unused)))
|
2024-04-18 14:20:28 +08:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-25 14:08:33 +08:00
|
|
|
struct packet_io_stat *dumpfile_io_stat(struct dumpfile_io *handle, uint16_t thr_idx)
|
2024-04-18 14:20:28 +08:00
|
|
|
{
|
|
|
|
|
return &handle->stat[thr_idx];
|
|
|
|
|
}
|