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-09 19:28:14 +08:00
|
|
|
typedef void *on_new(void *options);
|
|
|
|
|
typedef void on_free(void *handle);
|
|
|
|
|
typedef void *on_stat(void *handle);
|
|
|
|
|
typedef int on_init(void *handle, uint16_t thread_id);
|
|
|
|
|
typedef int on_recv(void *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts);
|
|
|
|
|
typedef void on_send(void *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts);
|
|
|
|
|
typedef void on_drop(void *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts);
|
|
|
|
|
typedef void on_inject(void *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts);
|
2024-02-28 16:30:03 +08:00
|
|
|
|
|
|
|
|
struct packet_io
|
|
|
|
|
{
|
|
|
|
|
void *handle;
|
2024-03-09 19:28:14 +08:00
|
|
|
on_new *new_func;
|
|
|
|
|
on_free *free_func;
|
|
|
|
|
on_stat *stat_func;
|
|
|
|
|
on_init *init_func;
|
|
|
|
|
on_recv *recv_func;
|
|
|
|
|
on_send *send_func;
|
|
|
|
|
on_drop *drop_func;
|
|
|
|
|
on_inject *inject_func;
|
2024-02-28 16:30:03 +08:00
|
|
|
};
|
|
|
|
|
|
2024-03-08 14:51:21 +08:00
|
|
|
struct packet_io *packet_io_new(struct packet_io_options *opts)
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
struct packet_io_marsio_options opts_marsio;
|
|
|
|
|
struct packet_io_dumpfile_options opts_dumpfile;
|
|
|
|
|
void *opts_ptr = NULL;
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-08 14:51:21 +08:00
|
|
|
if (opts->mode == PACKET_IO_MARSIO)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
strncpy(opts_marsio.app_symbol, opts->app_symbol, sizeof(opts_marsio.app_symbol));
|
|
|
|
|
strncpy(opts_marsio.dev_symbol, opts->dev_symbol, sizeof(opts_marsio.dev_symbol));
|
|
|
|
|
memcpy(opts_marsio.cpu_mask, opts->cpu_mask, sizeof(opts_marsio.cpu_mask));
|
|
|
|
|
opts_marsio.nr_threads = opts->nr_threads;
|
|
|
|
|
|
|
|
|
|
opts_ptr = &opts_marsio;
|
|
|
|
|
handle->new_func = (on_new *)packet_io_marsio_new;
|
|
|
|
|
handle->free_func = (on_free *)packet_io_marsio_free;
|
|
|
|
|
handle->stat_func = (on_stat *)packet_io_marsio_get_stat;
|
|
|
|
|
handle->init_func = (on_init *)packet_io_marsio_init;
|
|
|
|
|
handle->recv_func = (on_recv *)packet_io_marsio_ingress;
|
|
|
|
|
handle->send_func = (on_send *)packet_io_marsio_egress;
|
|
|
|
|
handle->drop_func = (on_drop *)packet_io_marsio_drop;
|
|
|
|
|
handle->inject_func = (on_inject *)packet_io_marsio_inject;
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
strncpy(opts_dumpfile.dumpfile_dir, opts->dumpfile_dir, sizeof(opts_dumpfile.dumpfile_dir));
|
|
|
|
|
opts_dumpfile.nr_threads = opts->nr_threads;
|
|
|
|
|
|
|
|
|
|
opts_ptr = &opts_dumpfile;
|
|
|
|
|
handle->new_func = (on_new *)packet_io_dumpfile_new;
|
|
|
|
|
handle->free_func = (on_free *)packet_io_dumpfile_free;
|
|
|
|
|
handle->stat_func = (on_stat *)packet_io_dumpfile_get_stat;
|
|
|
|
|
handle->init_func = (on_init *)packet_io_dumpfile_init;
|
|
|
|
|
handle->recv_func = (on_recv *)packet_io_dumpfile_ingress;
|
|
|
|
|
handle->send_func = (on_send *)packet_io_dumpfile_egress;
|
|
|
|
|
handle->drop_func = (on_drop *)packet_io_dumpfile_drop;
|
|
|
|
|
handle->inject_func = (on_inject *)packet_io_dumpfile_inject;
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
handle->handle = handle->new_func(opts_ptr);
|
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-09 19:28:14 +08:00
|
|
|
handle->free_func(handle->handle);
|
2024-02-28 16:30:03 +08:00
|
|
|
free(handle);
|
|
|
|
|
handle = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 14:25:01 +08:00
|
|
|
struct packet_io_stat *packet_io_get_stat(struct packet_io *handle)
|
|
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
return (struct packet_io_stat *)handle->stat_func(handle->handle);
|
2024-03-08 14:25:01 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
// return 0: success
|
|
|
|
|
// return -1: failed
|
2024-02-28 16:30:03 +08:00
|
|
|
int packet_io_init(struct packet_io *handle, uint16_t thread_id)
|
|
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
return handle->init_func(handle->handle, thread_id);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
// return number of packets received
|
2024-03-08 18:10:38 +08:00
|
|
|
int packet_io_ingress(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
return handle->recv_func(handle->handle, thread_id, pkts, nr_pkts);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
void packet_io_egress(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
handle->send_func(handle->handle, thread_id, pkts, nr_pkts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_io_drop(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
|
|
|
|
{
|
|
|
|
|
handle->drop_func(handle->handle, thread_id, pkts, nr_pkts);
|
|
|
|
|
}
|
|
|
|
|
void packet_io_inject(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
|
|
|
|
{
|
|
|
|
|
handle->inject_func(handle->handle, thread_id, pkts, nr_pkts);
|
2024-02-28 16:30:03 +08:00
|
|
|
}
|