#include #include #include "packet_io.h" #include "packet_io_marsio.h" #include "packet_io_dumpfile.h" typedef void *on_create(void *config); typedef void on_destroy(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 **pkt); typedef void on_send(void *handle, uint16_t thread_id, struct packet *pkt); struct packet_io { void *handle; on_create *create; on_destroy *destroy; on_stat *stat; on_init *init; on_recv *recv; on_send *send; }; struct packet_io *packet_io_create(struct packet_io_config *config) { 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; handle->create = (on_create *)packet_io_marsio_create; handle->destroy = (on_destroy *)packet_io_marsio_destory; handle->stat = (on_stat *)packet_io_marsio_stat; handle->init = (on_init *)packet_io_marsio_init; handle->recv = (on_recv *)packet_io_marsio_recv; handle->send = (on_send *)packet_io_marsio_send; } else { _config = &dumpfile_config; handle->create = (on_create *)packet_io_dumpfile_create; handle->destroy = (on_destroy *)packet_io_dumpfile_destory; handle->stat = (on_stat *)packet_io_dumpfile_stat; handle->init = (on_init *)packet_io_dumpfile_init; handle->recv = (on_recv *)packet_io_dumpfile_recv; handle->send = (on_send *)packet_io_dumpfile_send; } handle->handle = handle->create(_config); if (handle->handle == NULL) { goto error_out; } return handle; error_out: packet_io_destroy(handle); return NULL; } void packet_io_destroy(struct packet_io *handle) { if (handle) { handle->destroy(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(handle->handle); } 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) { return handle->init(handle->handle, thread_id); } int packet_io_recv(struct packet_io *handle, uint16_t thread_id, struct packet **pkt) { return handle->recv(handle->handle, thread_id, pkt); } void packet_io_send(struct packet_io *handle, uint16_t thread_id, struct packet *pkt) { handle->send(handle->handle, thread_id, pkt); }