#ifndef _PACKET_IO_H
#define _PACKET_IO_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "log.h"
#include "macro.h"
#include "packet_priv.h"

#define PACKET_IO_LOG_STATE(format, ...) LOG_STATE("packet_io", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("packet_io", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_DEBUG(format, ...) LOG_DEBUG("packet_io", format, ##__VA_ARGS__)

struct io_stat
{
    // device packet
    uint64_t dev_rx_pkts;
    uint64_t dev_rx_bytes;

    uint64_t dev_tx_pkts;
    uint64_t dev_tx_bytes;

    // keep-alive packet
    uint64_t keep_alive_pkts;
    uint64_t keep_alive_bytes;

    // raw packet
    uint64_t raw_rx_pkts;
    uint64_t raw_rx_bytes;

    uint64_t raw_tx_pkts;
    uint64_t raw_tx_bytes;

    // drop packet
    uint64_t drop_pkts;
    uint64_t drop_bytes;

    // inject packet
    uint64_t inject_pkts;
    uint64_t inject_bytes;

    // ctrl packet
    uint64_t ctrl_rx_pkts;
    uint64_t ctrl_rx_bytes;

    uint64_t ctrl_tx_pkts;
    uint64_t ctrl_tx_bytes;
};

enum packet_io_mode
{
    PACKET_IO_DUMPFILE = 0,
    PACKET_IO_MARSIO = 1,
};

struct packet_io_options
{
    enum packet_io_mode mode;

    // for dumpfile
    char dumpfile_dir[256];

    // for marsio
    char app_symbol[64];
    char dev_symbol[64];

    uint16_t nr_threads;
    uint16_t cpu_mask[MAX_THREAD_NUM];
};

struct inject_packet_meta
{
    struct route_ctx route;
    struct sid_list sids;
    uint64_t session_id;
    uint16_t link_id;
    int is_ctrl;
};

struct packet_io;
struct packet_io *packet_io_new(struct packet_io_options *opts);
void packet_io_free(struct packet_io *packet_io);
int packet_io_wait_exit(struct packet_io *packet_io); // used for dumpfile mode

int packet_io_init(struct packet_io *packet_io, uint16_t thr_idx);
int packet_io_ingress(struct packet_io *packet_io, uint16_t thr_idx, 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);
void packet_io_drop(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, int nr_pkts);
int packet_io_inject(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, int nr_pkts);
void packet_io_yield(struct packet_io *packet_io, uint16_t thr_idx, uint64_t timeout_ms);
struct io_stat *packet_io_stat(struct packet_io *packet_io, uint16_t thr_idx);

#ifdef __cplusplus
}
#endif

#endif
