-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpdk_init.c
76 lines (69 loc) · 2.11 KB
/
dpdk_init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Basic init tasks (memory pools, devices, dpdk framework) for DPDK applications.
*/
#include "utils/utils.h"
#include "dpdk_init.h"
#include <stdlib.h>
#include <rte_config.h>
#include <rte_common.h>
#include <rte_ethdev.h>
#include <rte_errno.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>
static void check_dpdk_error(uint32_t rc, const char *operation)
{
if (rc)
{
printf("could not %s: %s\n", operation, rte_strerror(-rc));
exit(1);
}
}
static const uint32_t RX_DESCS = 256;
static const uint32_t TX_DESCS = 256;
static const uint32_t MEMPOOL_CACHE_SIZE = 256;
static const uint32_t MEMPOOL_SIZE = 2047;
static const uint32_t MBUF_SIZE = 1600;
static struct rte_mempool *create_mempool()
{
static volatile int pool_id = 0;
char pool_name[32];
sprintf(pool_name, "pool%d", __sync_fetch_and_add(&pool_id, 1));
struct rte_mempool *pool = rte_pktmbuf_pool_create(pool_name, MEMPOOL_SIZE, MEMPOOL_CACHE_SIZE,
0, MBUF_SIZE + RTE_PKTMBUF_HEADROOM,
rte_socket_id());
if (!pool)
{
printf("could not allocate mempool\n");
exit(1);
}
return pool;
}
/**
* Initialize a device by configuring hardware queues.
*
* Number of allocated queues for device with port_id:
* - 1 RX queue
* - num_queues rx/tx queues
*/
void configure_device(uint8_t port_id, uint16_t num_queues)
{
struct rte_eth_conf port_conf = {.rxmode = {.hw_strip_crc = 1}};
check_dpdk_error(rte_eth_dev_configure(port_id, num_queues, num_queues, &port_conf), "configure device");
struct rte_eth_dev_info dev_info;
rte_eth_dev_info_get(port_id, &dev_info);
for (uint16_t queue = 0; queue < num_queues; ++queue)
{
check_dpdk_error(rte_eth_tx_queue_setup(port_id, queue, TX_DESCS, rte_socket_id(), &dev_info.default_txconf), "configure tx queue");
check_dpdk_error(rte_eth_rx_queue_setup(port_id, queue, RX_DESCS, rte_socket_id(), &dev_info.default_rxconf, create_mempool()), "configure rx queue");
}
check_dpdk_error(rte_eth_dev_start(port_id), "starting device");
}
void init_dpdk()
{
uint32_t argc = 1;
char *argv[argc];
//argv[0] = "-c1";
//argv[1] = "--lcores=(0-7)@0";
argv[0] = "-n1";
rte_eal_init(argc, argv);
}