Refactor Packet I/O
This commit is contained in:
@@ -1,129 +1,119 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "packet_io.h"
|
||||
#include "packet_io_marsio.h"
|
||||
#include "packet_io_dumpfile.h"
|
||||
|
||||
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);
|
||||
#include "marsio_io.h"
|
||||
#include "dumpfile_io.h"
|
||||
|
||||
struct packet_io
|
||||
{
|
||||
void *handle;
|
||||
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;
|
||||
enum packet_io_mode mode;
|
||||
struct marsio_io *marsio;
|
||||
struct dumpfile_io *dumpfile;
|
||||
};
|
||||
|
||||
struct packet_io *packet_io_new(struct packet_io_options *opts)
|
||||
{
|
||||
struct packet_io *handle = (struct packet_io *)calloc(1, sizeof(struct packet_io));
|
||||
if (handle == NULL)
|
||||
struct packet_io *packet_io = (struct packet_io *)calloc(1, sizeof(struct packet_io));
|
||||
if (packet_io == NULL)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("unable to alloc packet io");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct packet_io_marsio_options opts_marsio;
|
||||
struct packet_io_dumpfile_options opts_dumpfile;
|
||||
void *opts_ptr = NULL;
|
||||
|
||||
packet_io->mode = opts->mode;
|
||||
if (opts->mode == PACKET_IO_MARSIO)
|
||||
{
|
||||
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;
|
||||
packet_io->marsio = marsio_io_new(opts->app_symbol, opts->dev_symbol, opts->cpu_mask, opts->nr_threads);
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
packet_io->dumpfile = dumpfile_io_new(opts->dumpfile_dir, opts->nr_threads);
|
||||
}
|
||||
|
||||
handle->handle = handle->new_func(opts_ptr);
|
||||
if (handle->handle == NULL)
|
||||
if (packet_io->marsio == NULL && packet_io->dumpfile == NULL)
|
||||
{
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
return handle;
|
||||
return packet_io;
|
||||
|
||||
error_out:
|
||||
packet_io_free(handle);
|
||||
packet_io_free(packet_io);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void packet_io_free(struct packet_io *handle)
|
||||
void packet_io_free(struct packet_io *packet_io)
|
||||
{
|
||||
if (handle)
|
||||
if (packet_io)
|
||||
{
|
||||
handle->free_func(handle->handle);
|
||||
free(handle);
|
||||
handle = NULL;
|
||||
if (packet_io->mode == PACKET_IO_MARSIO)
|
||||
{
|
||||
marsio_io_free(packet_io->marsio);
|
||||
}
|
||||
else
|
||||
{
|
||||
dumpfile_io_free(packet_io->dumpfile);
|
||||
}
|
||||
free(packet_io);
|
||||
packet_io = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct packet_io_stat *packet_io_get_stat(struct packet_io *handle)
|
||||
struct packet_stat *packet_io_stat(struct packet_io *packet_io)
|
||||
{
|
||||
return (struct packet_io_stat *)handle->stat_func(handle->handle);
|
||||
if (packet_io->mode == PACKET_IO_MARSIO)
|
||||
{
|
||||
return marsio_io_stat(packet_io->marsio);
|
||||
}
|
||||
else
|
||||
{
|
||||
return dumpfile_io_stat(packet_io->dumpfile);
|
||||
}
|
||||
}
|
||||
|
||||
// return 0: success
|
||||
// return -1: failed
|
||||
int packet_io_init(struct packet_io *handle, uint16_t thread_id)
|
||||
int packet_io_init(struct packet_io *packet_io, uint16_t thr_idx)
|
||||
{
|
||||
return handle->init_func(handle->handle, thread_id);
|
||||
if (packet_io->mode == PACKET_IO_MARSIO)
|
||||
{
|
||||
return marsio_io_init(packet_io->marsio, thr_idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
return dumpfile_io_init(packet_io->dumpfile, thr_idx);
|
||||
}
|
||||
}
|
||||
|
||||
// return number of packets received
|
||||
int packet_io_ingress(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
||||
int packet_io_ingress(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, int nr_pkts)
|
||||
{
|
||||
return handle->recv_func(handle->handle, thread_id, pkts, nr_pkts);
|
||||
if (packet_io->mode == PACKET_IO_MARSIO)
|
||||
{
|
||||
return marsio_io_ingress(packet_io->marsio, thr_idx, pkts, nr_pkts);
|
||||
}
|
||||
else
|
||||
{
|
||||
return dumpfile_io_ingress(packet_io->dumpfile, thr_idx, pkts, nr_pkts);
|
||||
}
|
||||
}
|
||||
|
||||
void packet_io_egress(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
||||
void packet_io_egress(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, int nr_pkts)
|
||||
{
|
||||
handle->send_func(handle->handle, thread_id, pkts, nr_pkts);
|
||||
if (packet_io->mode == PACKET_IO_MARSIO)
|
||||
{
|
||||
marsio_io_egress(packet_io->marsio, thr_idx, pkts, nr_pkts);
|
||||
}
|
||||
else
|
||||
{
|
||||
dumpfile_io_egress(packet_io->dumpfile, thr_idx, pkts, nr_pkts);
|
||||
}
|
||||
}
|
||||
|
||||
void packet_io_drop(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts)
|
||||
void packet_io_drop(struct packet_io *packet_io, uint16_t thr_idx, 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);
|
||||
if (packet_io->mode == PACKET_IO_MARSIO)
|
||||
{
|
||||
marsio_io_drop(packet_io->marsio, thr_idx, pkts, nr_pkts);
|
||||
}
|
||||
else
|
||||
{
|
||||
dumpfile_io_drop(packet_io->dumpfile, thr_idx, pkts, nr_pkts);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user