2024-02-28 16:30:03 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "packet_io.h"
|
|
|
|
|
#include "packet_io_marsio.h"
|
|
|
|
|
#include "packet_io_dumpfile.h"
|
|
|
|
|
|
2024-03-08 14:20:36 +08:00
|
|
|
typedef void *new_cb(void *config);
|
|
|
|
|
typedef void free_cb(void *handle);
|
|
|
|
|
typedef void *stat_cb(void *handle);
|
|
|
|
|
typedef int init_cb(void *handle, uint16_t thread_id);
|
|
|
|
|
typedef int recv_cb(void *handle, uint16_t thread_id, struct packet **pkt);
|
|
|
|
|
typedef void send_cb(void *handle, uint16_t thread_id, struct packet *pkt);
|
2024-02-28 16:30:03 +08:00
|
|
|
|
|
|
|
|
struct packet_io
|
|
|
|
|
{
|
|
|
|
|
void *handle;
|
2024-03-08 14:20:36 +08:00
|
|
|
new_cb *on_new;
|
|
|
|
|
free_cb *on_free;
|
|
|
|
|
stat_cb *on_stat;
|
|
|
|
|
init_cb *on_init;
|
|
|
|
|
recv_cb *on_recv;
|
|
|
|
|
send_cb *on_send;
|
2024-02-28 16:30:03 +08:00
|
|
|
};
|
|
|
|
|
|
2024-03-08 14:20:36 +08:00
|
|
|
struct packet_io *packet_io_new(struct packet_io_config *config)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
struct packet_io *handle = (struct packet_io *)calloc(1, sizeof(struct packet_io));
|
|
|
|
|
if (handle == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_IO_LOG_ERROR("unable to alloc packet io");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct packet_io_marsio_confg marsio_config;
|
|
|
|
|
strncpy(marsio_config.app_symbol, config->app_symbol, sizeof(marsio_config.app_symbol));
|
|
|
|
|
strncpy(marsio_config.dev_symbol, config->dev_symbol, sizeof(marsio_config.dev_symbol));
|
|
|
|
|
memcpy(marsio_config.cpu_mask, config->cpu_mask, sizeof(marsio_config.cpu_mask));
|
|
|
|
|
marsio_config.nr_threads = config->nr_threads;
|
|
|
|
|
|
|
|
|
|
struct packet_io_dumpfile_confg dumpfile_config;
|
|
|
|
|
strncpy(dumpfile_config.dumpfile_dir, config->dumpfile_dir, sizeof(dumpfile_config.dumpfile_dir));
|
|
|
|
|
dumpfile_config.nr_threads = config->nr_threads;
|
|
|
|
|
|
|
|
|
|
void *_config = NULL;
|
|
|
|
|
|
|
|
|
|
if (config->mode == PACKET_IO_MARSIO)
|
|
|
|
|
{
|
|
|
|
|
_config = &marsio_config;
|
2024-03-08 14:20:36 +08:00
|
|
|
handle->on_new = (new_cb *)packet_io_marsio_new;
|
|
|
|
|
handle->on_free = (free_cb *)packet_io_marsio_free;
|
|
|
|
|
handle->on_stat = (stat_cb *)packet_io_marsio_stat;
|
|
|
|
|
handle->on_init = (init_cb *)packet_io_marsio_init;
|
|
|
|
|
handle->on_recv = (recv_cb *)packet_io_marsio_recv;
|
|
|
|
|
handle->on_send = (send_cb *)packet_io_marsio_send;
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_config = &dumpfile_config;
|
2024-03-08 14:20:36 +08:00
|
|
|
handle->on_new = (new_cb *)packet_io_dumpfile_new;
|
|
|
|
|
handle->on_free = (free_cb *)packet_io_dumpfile_free;
|
|
|
|
|
handle->on_stat = (stat_cb *)packet_io_dumpfile_stat;
|
|
|
|
|
handle->on_init = (init_cb *)packet_io_dumpfile_init;
|
|
|
|
|
handle->on_recv = (recv_cb *)packet_io_dumpfile_recv;
|
|
|
|
|
handle->on_send = (send_cb *)packet_io_dumpfile_send;
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-08 14:20:36 +08:00
|
|
|
handle->handle = handle->on_new(_config);
|
2024-02-28 16:30:03 +08:00
|
|
|
if (handle->handle == NULL)
|
|
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
|
|
|
|
|
error_out:
|
2024-03-08 14:20:36 +08:00
|
|
|
packet_io_free(handle);
|
2024-02-28 16:30:03 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 14:20:36 +08:00
|
|
|
void packet_io_free(struct packet_io *handle)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
if (handle)
|
|
|
|
|
{
|
2024-03-08 14:20:36 +08:00
|
|
|
handle->on_free(handle->handle);
|
2024-02-28 16:30:03 +08:00
|
|
|
free(handle);
|
|
|
|
|
handle = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct packet_io_stat *packet_io_get_stat(struct packet_io *handle)
|
|
|
|
|
{
|
2024-03-08 14:20:36 +08:00
|
|
|
return (struct packet_io_stat *)handle->on_stat(handle->handle);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_io_print_stat(struct packet_io *handle)
|
|
|
|
|
{
|
|
|
|
|
struct packet_io_stat *stat = packet_io_get_stat(handle);
|
|
|
|
|
|
|
|
|
|
PACKET_IO_LOG_DEBUG("rx_pkts : %lu, rx_bytes : %lu", stat->rx_pkts, stat->rx_bytes);
|
|
|
|
|
PACKET_IO_LOG_DEBUG("tx_pkts : %lu, tx_bytes : %lu", stat->tx_pkts, stat->tx_bytes);
|
|
|
|
|
PACKET_IO_LOG_DEBUG("drop_pkts : %lu, drop_bytes : %lu", stat->drop_pkts, stat->drop_bytes);
|
|
|
|
|
PACKET_IO_LOG_DEBUG("inject_pkts : %lu, inject_bytes : %lu", stat->inject_pkts, stat->inject_bytes);
|
|
|
|
|
PACKET_IO_LOG_DEBUG("keepalive_pkts : %lu, keepalive_bytes : %lu", stat->keepalive_pkts, stat->keepalive_bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int packet_io_init(struct packet_io *handle, uint16_t thread_id)
|
|
|
|
|
{
|
2024-03-08 14:20:36 +08:00
|
|
|
return handle->on_init(handle->handle, thread_id);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int packet_io_recv(struct packet_io *handle, uint16_t thread_id, struct packet **pkt)
|
|
|
|
|
{
|
2024-03-08 14:20:36 +08:00
|
|
|
return handle->on_recv(handle->handle, thread_id, pkt);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_io_send(struct packet_io *handle, uint16_t thread_id, struct packet *pkt)
|
|
|
|
|
{
|
2024-03-08 14:20:36 +08:00
|
|
|
handle->on_send(handle->handle, thread_id, pkt);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|