2024-08-30 10:21:44 +08:00
|
|
|
#include "pcap_io.h"
|
2024-10-23 10:01:20 +08:00
|
|
|
#include "mars_io.h"
|
|
|
|
|
#include "utils_internal.h"
|
2024-08-30 10:21:44 +08:00
|
|
|
|
2024-02-28 16:30:03 +08:00
|
|
|
struct packet_io
|
|
|
|
|
{
|
2024-08-30 10:21:44 +08:00
|
|
|
void *handle;
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
void *(*new_func)(const char *toml_file);
|
2024-08-30 10:21:44 +08:00
|
|
|
void (*free_func)(void *handle);
|
|
|
|
|
int (*isbreak_func)(void *handle);
|
|
|
|
|
|
|
|
|
|
int (*init_func)(void *handle, uint16_t thr_idx);
|
2024-10-23 10:01:20 +08:00
|
|
|
int (*recv_func)(void *handle, uint16_t thr_idx, struct packet *pkts[], int nr_pkts);
|
|
|
|
|
void (*send_func)(void *handle, uint16_t thr_idx, struct packet *pkts[], int nr_pkts);
|
|
|
|
|
void (*drop_func)(void *handle, uint16_t thr_idx, struct packet *pkts[], int nr_pkts);
|
2024-08-30 10:21:44 +08:00
|
|
|
void (*yield_func)(void *handle, uint16_t thr_idx);
|
2024-10-23 10:01:20 +08:00
|
|
|
void (*polling_func)(void *handle, uint16_t thr_idx);
|
2024-08-30 10:21:44 +08:00
|
|
|
struct packet_io_stat *(*stat_func)(void *handle, uint16_t thr_idx);
|
2024-02-28 16:30:03 +08:00
|
|
|
};
|
|
|
|
|
|
2024-09-20 18:41:07 +08:00
|
|
|
struct packet_io *packet_io_new(const char *toml_file)
|
2024-04-18 14:20:28 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
char mode[64] = {0};
|
2024-08-30 10:21:44 +08:00
|
|
|
struct packet_io *pkt_io = (struct packet_io *)calloc(1, sizeof(struct packet_io));
|
|
|
|
|
if (pkt_io == NULL)
|
2024-04-18 14:20:28 +08:00
|
|
|
{
|
2024-08-30 10:21:44 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
load_toml_str_config(toml_file, "packet_io.mode", mode);
|
|
|
|
|
if (strcmp(mode, "marsio") == 0)
|
2024-09-20 18:41:07 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
pkt_io->new_func = mars_io_new;
|
|
|
|
|
pkt_io->free_func = mars_io_free;
|
|
|
|
|
pkt_io->isbreak_func = mars_io_isbreak;
|
|
|
|
|
pkt_io->init_func = mars_io_init;
|
|
|
|
|
pkt_io->recv_func = mars_io_recv;
|
|
|
|
|
pkt_io->send_func = mars_io_send;
|
|
|
|
|
pkt_io->drop_func = mars_io_drop;
|
|
|
|
|
pkt_io->yield_func = mars_io_yield;
|
|
|
|
|
pkt_io->polling_func = mars_io_polling;
|
|
|
|
|
pkt_io->stat_func = mars_io_stat;
|
2024-04-18 14:20:28 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-08-30 10:21:44 +08:00
|
|
|
pkt_io->new_func = pcap_io_new;
|
|
|
|
|
pkt_io->free_func = pcap_io_free;
|
|
|
|
|
pkt_io->isbreak_func = pcap_io_isbreak;
|
|
|
|
|
pkt_io->init_func = pcap_io_init;
|
2024-10-23 10:01:20 +08:00
|
|
|
pkt_io->recv_func = pcap_io_recv;
|
|
|
|
|
pkt_io->send_func = pcap_io_send;
|
2024-08-30 10:21:44 +08:00
|
|
|
pkt_io->drop_func = pcap_io_drop;
|
|
|
|
|
pkt_io->yield_func = pcap_io_yield;
|
2024-10-23 10:01:20 +08:00
|
|
|
pkt_io->polling_func = pcap_io_polling;
|
2024-08-30 10:21:44 +08:00
|
|
|
pkt_io->stat_func = pcap_io_stat;
|
2024-04-18 14:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
pkt_io->handle = pkt_io->new_func(toml_file);
|
2024-08-30 10:21:44 +08:00
|
|
|
if (pkt_io->handle == NULL)
|
2024-04-18 14:20:28 +08:00
|
|
|
{
|
2024-08-30 10:21:44 +08:00
|
|
|
packet_io_free(pkt_io);
|
|
|
|
|
return NULL;
|
2024-04-18 14:20:28 +08:00
|
|
|
}
|
2024-08-30 10:21:44 +08:00
|
|
|
|
|
|
|
|
return pkt_io;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_io_free(struct packet_io *pkt_io)
|
|
|
|
|
{
|
|
|
|
|
if (pkt_io)
|
2024-04-18 14:20:28 +08:00
|
|
|
{
|
2024-08-30 10:21:44 +08:00
|
|
|
if (pkt_io->handle)
|
|
|
|
|
{
|
|
|
|
|
pkt_io->free_func(pkt_io->handle);
|
|
|
|
|
}
|
|
|
|
|
free(pkt_io);
|
|
|
|
|
pkt_io = NULL;
|
2024-04-18 14:20:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-30 10:21:44 +08:00
|
|
|
|
|
|
|
|
int packet_io_isbreak(struct packet_io *pkt_io)
|
|
|
|
|
{
|
|
|
|
|
return pkt_io->isbreak_func(pkt_io->handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int packet_io_init(struct packet_io *pkt_io, uint16_t thr_idx)
|
|
|
|
|
{
|
|
|
|
|
return pkt_io->init_func(pkt_io->handle, thr_idx);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
int packet_io_recv(struct packet_io *pkt_io, uint16_t thr_idx, struct packet *pkts[], int nr_pkts)
|
2024-08-30 10:21:44 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
return pkt_io->recv_func(pkt_io->handle, thr_idx, pkts, nr_pkts);
|
2024-08-30 10:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
void packet_io_send(struct packet_io *pkt_io, uint16_t thr_idx, struct packet *pkts[], int nr_pkts)
|
2024-08-30 10:21:44 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
pkt_io->send_func(pkt_io->handle, thr_idx, pkts, nr_pkts);
|
2024-08-30 10:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
void packet_io_drop(struct packet_io *pkt_io, uint16_t thr_idx, struct packet *pkts[], int nr_pkts)
|
2024-08-30 10:21:44 +08:00
|
|
|
{
|
|
|
|
|
pkt_io->drop_func(pkt_io->handle, thr_idx, pkts, nr_pkts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_io_yield(struct packet_io *pkt_io, uint16_t thr_idx)
|
|
|
|
|
{
|
|
|
|
|
pkt_io->yield_func(pkt_io->handle, thr_idx);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
void packet_io_polling(struct packet_io *pkt_io, uint16_t thr_idx)
|
|
|
|
|
{
|
|
|
|
|
pkt_io->polling_func(pkt_io->handle, thr_idx);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 10:21:44 +08:00
|
|
|
struct packet_io_stat *packet_io_stat(struct packet_io *pkt_io, uint16_t thr_idx)
|
|
|
|
|
{
|
|
|
|
|
return pkt_io->stat_func(pkt_io->handle, thr_idx);
|
|
|
|
|
}
|