//pmd 0 s a
#include<rte_ethdev.h>
#include<rte_malloc.h>
#include<pthread.h>
#include<signal.h>

_Bool received_sigint = false;

struct rte_eth_conf port_conf =
{
	.rxmode = { .mq_mode = RTE_ETH_MQ_RX_NONE, },
	.txmode = { .mq_mode = RTE_ETH_MQ_TX_NONE, },
};

static void SIGINT_signal_handler( const int signal )
{
	fprintf( stderr, "\b\bReceived Interrupt Signal SIGINT (%d). Exiting...\n", signal );
	received_sigint = true;
}

int main( int argc, char **argv )
{
	uint16_t pkt_count, num_of_pkts_rcvd;
	struct rte_eth_dev_info dev_info;

	if( signal( SIGINT, SIGINT_signal_handler ) == SIG_ERR )
	{
		fprintf( stderr, "%s %d SIGINT signal handling failed\n", __func__, __LINE__ );
		exit( 1 );
	}
	const int ret = rte_eal_init( argc, argv );
	if( ret < 0 )
		rte_exit( EXIT_FAILURE, "Error with EAL initialization\n" );
	argc -= ret;
	argv += ret;
	const int port_id = atoi( argv[1] );
	if( rte_eth_dev_info_get( port_id, &dev_info ) != 0 )
		rte_exit( EXIT_FAILURE, "%s %d rte_eth_dev_info_get\n", __func__, __LINE__ );
	uint16_t fetched_mtu = 0;
	if( rte_eth_dev_get_mtu( port_id, &fetched_mtu ) != 0 )
	{
		rte_exit( EXIT_FAILURE, "%s %d rte_eth_dev_stop port id: %u errno: %u Error: %s\n", __func__, __LINE__, port_id, rte_errno, rte_strerror( rte_errno ) );
	}
	port_conf.rxmode.mtu = dev_info.max_mtu = fetched_mtu;
	const int sock = rte_eth_dev_socket_id( port_id );
	if( sock == -1 )
		rte_exit( EXIT_FAILURE, "%s %d rte_eth_dev_socket_id port id: %u\n", __func__, __LINE__, port_id );
	char mem_pool_name[20];
	sprintf( mem_pool_name, "pool_%u_r", port_id );
	const uint32_t num_of_pkts_per_queue = 4096;
	struct rte_mbuf *mbuf[num_of_pkts_per_queue];
	//char *packet_buffer[num_of_pkts_per_queue];
	fprintf( stderr, "%s %d port id: %d num_of_pkts_per_queue: %u\n", __func__, __LINE__, port_id, num_of_pkts_per_queue );
	struct rte_mempool *mem_pool = rte_pktmbuf_pool_create( mem_pool_name, num_of_pkts_per_queue, RTE_MEMPOOL_CACHE_MAX_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, sock );
	if( mem_pool == NULL )
	{
		fprintf( stderr, "%d %s\n", rte_errno, rte_strerror(rte_errno) );
		rte_exit( EXIT_FAILURE, "%s %d rte_pktmbuf_pool_create port id: %u\n", __func__, __LINE__, port_id );
	}
	if( rte_eth_dev_configure( port_id, 1, 0, &port_conf ) != 0 )
	{
		fprintf( stderr, "%d %s\n", rte_errno, rte_strerror(rte_errno) );
		rte_exit( EXIT_FAILURE, "%s %d rte_eth_dev_configure port id: %u\n", __func__, __LINE__, port_id );
	}
	struct rte_eth_rxconf rxq_conf = {};
	rxq_conf = dev_info.default_rxconf;
	rxq_conf.offloads = port_conf.rxmode.offloads;
	if( rte_eth_rx_queue_setup( port_id, 0, num_of_pkts_per_queue, (unsigned int)sock, &rxq_conf, mem_pool ) < 0 )
	{
		fprintf( stderr, "%d %s\n", rte_errno, rte_strerror(rte_errno) );
		rte_exit( EXIT_FAILURE, "%s %d rte_eth_rx_queue_setup port id: %u\n", __func__, __LINE__, port_id );
	}
	if( rte_eth_dev_start( port_id ) < 0 )
		rte_exit( EXIT_FAILURE, "%s %d rte_eth_dev_start port id: %u\n", __func__, __LINE__, port_id );
	const time_t begin_time = time( NULL );
	uint64_t pkts_rcvd_till_now = 0;
	while( received_sigint == false )
	{
		num_of_pkts_rcvd = rte_eth_rx_burst( port_id, 0, mbuf, num_of_pkts_per_queue );
		if (num_of_pkts_rcvd == 0) continue;
		fprintf( stderr, "port: %u rte_eth_rx_burst %u num_of_pkts_rcvd\n", port_id, num_of_pkts_rcvd );
		pkts_rcvd_till_now += num_of_pkts_rcvd; 
		for( pkt_count=0; pkt_count<num_of_pkts_rcvd; pkt_count++ )
		{
			uint8_t *packet_data = rte_pktmbuf_mtod(mbuf[pkt_count], uint8_t *);
			uint16_t pkt_len = rte_pktmbuf_pkt_len(mbuf[pkt_count]);
			for (int z = 0; z < pkt_len; z++)
			{
				fprintf( stderr, "%02x ", packet_data[z]);
			}		
			fprintf(stderr, "\n");
			rte_pktmbuf_free( mbuf[pkt_count] );
		}
	}
	const time_t end_time =time( NULL );
	const time_t elapsed_time = end_time-begin_time;
	const uint64_t bw = ( 2048*8*pkts_rcvd_till_now )/ elapsed_time;
	fprintf( stderr, "%s %d time : %ld total pkts rcvd: %lu bandwidth: %lu\n", __func__, __LINE__, elapsed_time, pkts_rcvd_till_now, bw/1024/1024/1000 );
	//rte_pktmbuf_free_bulk( mbuf, num_of_pkts_per_queue );
	rte_mempool_free( mem_pool );
	if( rte_eth_dev_stop( port_id ) < 0 )
		rte_exit( EXIT_FAILURE, "%s %d rte_eth_dev_stop port id: %u\n", __func__, __LINE__, port_id );
	return 0;
}
