#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/queue.h>
#include <errno.h>
#include <stdarg.h>
#include <inttypes.h>
#include "common.h"
#include "args.h"
#include "init.h"
#define MBUFS_PER_NODE 1536
#define MBUFS_PER_PORT 1536
#define MBUF_CACHE_SIZE 512
#define RTE_MP_RX_DESC_DEFAULT 512
#define RTE_MP_TX_DESC_DEFAULT 512
#define NODE_QUEUE_RINGSIZE 128
#define NO_FLAGS 0
struct node *nodes;
struct rte_efd_table *efd_table;
struct shared_info *info;
static int
init_mbuf_pools(void)
{
    const unsigned int num_mbufs = (num_nodes * MBUFS_PER_NODE) +
            (info->num_ports * MBUFS_PER_PORT);
    
    printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
            PKTMBUF_POOL_NAME, num_mbufs);
    return pktmbuf_pool == NULL; 
}
static int
init_port(uint16_t port_num)
{
    
        },
    };
    const uint16_t rx_rings = 1, tx_rings = num_nodes;
    uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
    uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
    uint16_t q;
    int retval;
    printf("Port %u init ... ", port_num);
    fflush(stdout);
    
    if (retval != 0)
        return retval;
            &tx_ring_size);
    if (retval != 0)
        return retval;
    for (q = 0; q < rx_rings; q++) {
                NULL, pktmbuf_pool);
        if (retval < 0)
            return retval;
    }
    txconf = dev_info.default_txconf;
    for (q = 0; q < tx_rings; q++) {
                &txconf);
        if (retval < 0)
            return retval;
    }
    if (retval < 0)
        return retval;
    printf("done:\n");
    return 0;
}
static int
init_shm_rings(void)
{
    unsigned int i;
    unsigned int socket_id;
    const char *q_name;
    const unsigned int ringsize = NODE_QUEUE_RINGSIZE;
        sizeof(*nodes) * num_nodes, 0);
    if (nodes == NULL)
        rte_exit(EXIT_FAILURE, 
"Cannot allocate memory for "                 "node program details\n");
    for (i = 0; i < num_nodes; i++) {
        
        q_name = get_rx_queue_name(i);
                ringsize, socket_id,
        if (nodes[i].rx_q == NULL)
            rte_exit(EXIT_FAILURE, 
"Cannot create rx ring queue "                     "for node %u\n", i);
    }
    return 0;
}
static void
create_efd_table(void)
{
    
    efd_table = 
rte_efd_create(
"flow table", num_flows * 2, 
sizeof(uint32_t),
            1 << socket_id, socket_id);
    if (efd_table == NULL)
        rte_exit(EXIT_FAILURE, 
"Problem creating the flow table\n");
 }
static void
populate_efd_table(void)
{
    unsigned int i;
    int32_t ret;
    uint32_t ip_dst;
    uint64_t node_id;
    
    for (i = 0; i < num_flows; i++) {
        node_id = i % num_nodes;
                (void *)&ip_dst, (efd_value_t)node_id);
        if (ret < 0)
            rte_exit(EXIT_FAILURE, 
"Unable to add entry %u in "                     "EFD table\n", i);
    }
    printf("EFD table: Adding 0x%x keys\n", num_flows);
}
static void
check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
{
#define CHECK_INTERVAL 100 
#define MAX_CHECK_TIME 90 
    uint8_t count, all_ports_up, print_flag = 0;
    uint16_t portid;
    printf("\nChecking link status");
    fflush(stdout);
    for (count = 0; count <= MAX_CHECK_TIME; count++) {
        all_ports_up = 1;
        for (portid = 0; portid < port_num; portid++) {
            if ((port_mask & (1 << info->id[portid])) == 0)
                continue;
            memset(&link, 0, sizeof(link));
            
            if (print_flag == 1) {
                if (link.link_status)
                    printf(
                    "Port%d Link Up. Speed %u Mbps - %s\n",
                        info->id[portid],
                        link.link_speed,
                    ("full-duplex") : ("half-duplex\n"));
                else
                    printf("Port %d Link Down\n",
                        info->id[portid]);
                continue;
            }
            
                all_ports_up = 0;
                break;
            }
        }
        
        if (print_flag == 1)
            break;
        if (all_ports_up == 0) {
            printf(".");
            fflush(stdout);
        }
        
        if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
            print_flag = 1;
            printf("done\n");
        }
    }
}
int
init(int argc, char *argv[])
{
    int retval;
    uint8_t i, total_ports;
    
    if (retval < 0)
        return -1;
    argc -= retval;
    argv += retval;
    
    
    if (mz == NULL)
        rte_exit(EXIT_FAILURE, 
"Cannot reserve memory zone "                 "for port information\n");
    memset(mz->
addr, 0, 
sizeof(*info));
    
    retval = parse_app_args(total_ports, argc, argv);
    if (retval != 0)
        return -1;
    
    retval = init_mbuf_pools();
    if (retval != 0)
        rte_exit(EXIT_FAILURE, 
"Cannot create needed mbuf pools\n");
     
    for (i = 0; i < info->num_ports; i++) {
        retval = init_port(info->id[i]);
        if (retval != 0)
            rte_exit(EXIT_FAILURE, 
"Cannot initialise port %u\n",
                     (unsigned int) i);
    }
    check_all_ports_link_status(info->num_ports, (~0x0));
    
    init_shm_rings();
    
    create_efd_table();
    
    populate_efd_table();
    
    info->num_nodes = num_nodes;
    
    info->num_flows = num_flows;
    return 0;
}