-
Notifications
You must be signed in to change notification settings - Fork 0
/
port.cc
162 lines (139 loc) · 4.35 KB
/
port.cc
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <vector>
#include "xellico.h"
#include "config.h"
#include "port.h"
#define NB_MBUF 8192
#define MEMPOOL_CACHE_SIZE 256
struct rte_eth_conf port_conf;
struct rte_mempool* pktmbuf_pool[RTE_MAX_ETHPORTS];
static void
rte_mempool_dump (rte_mempool* mp)
{
printf("name : %s \n", mp->name);
printf("socket_id: %u \n", mp->socket_id);
printf("size : %u (using %.0f%%) \n", mp->size,
rte_mempool_in_use_count(mp)/float(mp->size)*100);
printf("in-use : %u \n", rte_mempool_in_use_count(mp));
printf("avail : %u \n", rte_mempool_avail_count(mp));
}
void
dump_pktmbuf_pool (void)
{
const size_t n_port = rte_eth_dev_count ();
for (size_t i=0; i<n_port; i++)
{
rte_mempool_dump (pktmbuf_pool[i]);
printf ("----------------------\n");
}
}
void
init_port_conf (struct rte_eth_conf* conf)
{
conf->rxmode.hw_strip_crc = 1;
conf->rxmode.mq_mode = ETH_MQ_RX_RSS;
conf->txmode.mq_mode = ETH_MQ_TX_NONE;
conf->rx_adv_conf.rss_conf.rss_key = NULL;
conf->rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IP|ETH_RSS_TCP|ETH_RSS_UDP;
}
static void
port_mempool_init ()
{
const uint8_t nb_ports = rte_eth_dev_count ();
for (size_t i=0; i<nb_ports; i++)
{
char str[128];
snprintf (str, sizeof (str), "pktmbuf_pool[%zd]", i);
pktmbuf_pool[i] = rte_pktmbuf_pool_create (str, NB_MBUF,
MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
rte_eth_dev_socket_id(i));
if (pktmbuf_pool[i] == NULL)
rte_exit (EXIT_FAILURE, "Cannot init mbuf pool %s\n", str);
RTE_LOG (INFO, XELLICO,
"create mempool %s on socket %u\n",
str, rte_eth_dev_socket_id(i));
}
}
static size_t
get_nb_rxq (uint32_t port_id)
{
size_t cnt = 0;
for (size_t i=0; i<xeconf->all_qconf.size(); i++) {
if (xeconf->all_qconf[i].port_id == port_id) cnt ++;
}
return cnt;
}
void
port_init (void)
{
init_port_conf (&port_conf);
const uint8_t nb_ports = rte_eth_dev_count ();
if (nb_ports == 0)
rte_exit (EXIT_FAILURE, "No Ethernet ports - bye\n");
RTE_LOG (INFO, XELLICO, "%u Ethernet ports found\n", nb_ports);
port_mempool_init ();
uint8_t nb_ports_available = nb_ports;
for (uint8_t portid = 0; portid < nb_ports; portid++)
{
const size_t nb_rxq = get_nb_rxq (portid);
const size_t nb_txq = rte_lcore_count();
printf("Initializing port %u... \n", (unsigned) portid);
int ret = rte_eth_dev_configure (portid, nb_rxq, nb_txq, &port_conf);
if (ret < 0)
rte_exit (EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
ret, (unsigned) portid);
uint16_t nb_rxd = 128;
uint16_t nb_txd = 512;
ret = rte_eth_dev_adjust_nb_rx_tx_desc (portid, &nb_rxd, &nb_txd);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Cannot adjust number of descriptors: err=%d, port=%u\n",
ret, (unsigned) portid);
/* init one RX queue */
for (uint32_t q=0; q<nb_rxq; q++)
{
ret = rte_eth_rx_queue_setup (portid, q, nb_rxd,
rte_eth_dev_socket_id (portid),
NULL, pktmbuf_pool[portid]);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"rte_eth_rx_queue_setup:err=%d, port=%u, queue=%u\n",
ret, (unsigned) portid, q);
}
/* init one TX queue on each port */
for (uint32_t q=0; q<nb_txq; q++)
{
ret = rte_eth_tx_queue_setup (portid, q, nb_txd,
rte_eth_dev_socket_id (portid), NULL);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"rte_eth_tx_queue_setup:err=%d, port=%u, queue=%u\n",
ret, (unsigned) portid, q);
}
}
for (uint8_t portid = 0; portid < nb_ports; portid++)
{
int ret = rte_eth_dev_start (portid);
if (ret < 0)
rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
ret, (unsigned) portid);
printf("done: \n");
rte_eth_promiscuous_enable (portid);
}
if (!nb_ports_available)
{
rte_exit(EXIT_FAILURE,
"All available ports are disabled. Please set portmask.\n");
}
}
void
port_fini (void)
{
const uint8_t nb_ports = rte_eth_dev_count ();
for (uint8_t portid = 0; portid < nb_ports; portid++)
{
printf ("Closing port %d...", portid);
rte_eth_dev_stop (portid);
rte_eth_dev_close (portid);
printf (" Done\n");
}
}