#include #include #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); 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; }; 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) { 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; 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; } 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; } handle->handle = handle->new_func(opts_ptr); if (handle->handle == NULL) { goto error_out; } return handle; error_out: packet_io_free(handle); return NULL; } void packet_io_free(struct packet_io *handle) { if (handle) { handle->free_func(handle->handle); free(handle); handle = NULL; } } struct packet_io_stat *packet_io_get_stat(struct packet_io *handle) { return (struct packet_io_stat *)handle->stat_func(handle->handle); } // return 0: success // return -1: failed int packet_io_init(struct packet_io *handle, uint16_t thread_id) { return handle->init_func(handle->handle, thread_id); } // return number of packets received int packet_io_ingress(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts) { return handle->recv_func(handle->handle, thread_id, pkts, nr_pkts); } void packet_io_egress(struct packet_io *handle, uint16_t thread_id, struct packet *pkts, int nr_pkts) { 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); }