forked from cisco-system-traffic-generator/trex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpdk_trex_port_attr.cpp
310 lines (267 loc) · 9.4 KB
/
dpdk_trex_port_attr.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Copyright (c) 2015-2018 Cisco Systems, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "trex_port_attr.h"
#include "main_dpdk.h"
using namespace std;
DpdkTRexPortAttr::DpdkTRexPortAttr(uint8_t tvpid, uint8_t repid, bool is_virtual,
bool fc_change_allowed, bool is_prom_allowed, bool is_vxlan_fs_allowed, bool has_pci,
bool device_flush_needed, bool is_ieee1588_allowed) {
m_tvpid = tvpid;
m_repid = repid;
m_port_id = tvpid; /* child */
m_rx_filter_mode = RX_FILTER_MODE_HW;
flag_has_pci = has_pci;
flag_is_virtual = is_virtual;
int tmp;
flag_is_fc_change_supported = fc_change_allowed && (get_flow_ctrl(tmp) != -ENOTSUP);
flag_is_led_change_supported = (set_led(true) != -ENOTSUP);
flag_is_link_change_supported = (set_link_up(true) != -ENOTSUP);
flag_is_prom_change_supported = is_prom_allowed;
flag_is_vxlan_fs_supported = is_vxlan_fs_allowed;
flag_is_ieee1588_supported = is_ieee1588_allowed;
flag_is_device_flush_needed = device_flush_needed;
update_device_info();
update_description();
}
/*
Get user friendly devices description from saved env. var
Changes certain attributes based on description
*/
void DpdkTRexPortAttr::update_description(){
char * envvar;
string pci_envvar_name;
pci_envvar_name = "pci" + intf_info_st.pci_addr;
replace(pci_envvar_name.begin(), pci_envvar_name.end(), ':', '_');
replace(pci_envvar_name.begin(), pci_envvar_name.end(), '.', '_');
envvar = getenv(pci_envvar_name.c_str());
if (envvar) {
intf_info_st.description = envvar;
} else {
intf_info_st.description = "Unknown";
}
if (intf_info_st.description.find("82599ES") != string::npos) { // works for 82599EB etc. DPDK does not distinguish them
flag_is_link_change_supported = false;
}
if (intf_info_st.description.find("82545EM") != string::npos) { // in virtual E1000, DPDK claims fc is supported, but it's not
flag_is_fc_change_supported = false;
flag_is_led_change_supported = false;
}
if ( CGlobalInfo::m_options.preview.getVMode() > 0){
printf("port %d desc: %s\n", m_repid, intf_info_st.description.c_str());
}
}
int DpdkTRexPortAttr::set_led(bool on){
if (on) {
return rte_eth_led_on(m_repid);
}else{
return rte_eth_led_off(m_repid);
}
}
int DpdkTRexPortAttr::get_flow_ctrl(int &mode) {
int ret = rte_eth_dev_flow_ctrl_get(m_repid, &fc_conf_tmp);
if (ret) {
mode = -1;
return ret;
}
mode = (int) fc_conf_tmp.mode;
return 0;
}
int DpdkTRexPortAttr::set_flow_ctrl(int mode) {
if (!flag_is_fc_change_supported) {
return -ENOTSUP;
}
int ret = rte_eth_dev_flow_ctrl_get(m_repid, &fc_conf_tmp);
if (ret) {
return ret;
}
fc_conf_tmp.mode = (enum rte_eth_fc_mode) mode;
return rte_eth_dev_flow_ctrl_set(m_repid, &fc_conf_tmp);
}
void DpdkTRexPortAttr::reset_xstats() {
rte_eth_xstats_reset(m_repid);
}
int DpdkTRexPortAttr::get_xstats_values(xstats_values_t &xstats_values) {
int size = rte_eth_xstats_get(m_repid, NULL, 0);
if (size < 0) {
return size;
}
do {
xstats_values_tmp.resize(size);
xstats_values.resize(size);
size = rte_eth_xstats_get(m_repid, xstats_values_tmp.data(), size);
} while (size > xstats_values_tmp.size());
if (size < 0) {
return size;
}
for (int i=0; i<size; i++) {
xstats_values[xstats_values_tmp[i].id] = xstats_values_tmp[i].value;
}
return 0;
}
int DpdkTRexPortAttr::get_xstats_names(xstats_names_t &xstats_names){
int size = rte_eth_xstats_get_names(m_repid, NULL, 0);
if (size < 0) {
return size;
}
do {
xstats_names_tmp.resize(size);
xstats_names.resize(size);
size = rte_eth_xstats_get_names(m_repid, xstats_names_tmp.data(), size);
} while (size > xstats_names_tmp.size());
if (size < 0) {
return size;
}
for (int i=0; i<size; i++) {
xstats_names[i] = xstats_names_tmp[i].name;
}
return 0;
}
void DpdkTRexPortAttr::dump_link(FILE *fd){
fprintf(fd,"port : %d \n",(int)m_tvpid);
fprintf(fd,"------------\n");
fprintf(fd,"link : ");
if (m_link.link_status) {
fprintf(fd," link : Link Up - speed %u Mbps - %s\n",
(unsigned) get_link_speed(),
(m_link.link_duplex == RTE_ETH_LINK_FULL_DUPLEX) ?
("full-duplex") : ("half-duplex\n"));
} else {
fprintf(fd," Link Down\n");
}
fprintf(fd,"promiscuous : %d \n",get_promiscuous());
}
bool fill_pci_dev(struct rte_eth_dev_info *dev_info, struct rte_pci_device* pci_dev) {
if ( dev_info->device ) {
const struct rte_bus *bus = nullptr;
bus = rte_bus_find_by_device(dev_info->device);
if ( bus && !strcmp(bus->name, "pci") ) {
*pci_dev = *RTE_DEV_TO_PCI(dev_info->device);
return true;
}
}
return false;
}
void DpdkTRexPortAttr::update_device_info(){
rte_eth_dev_info_get(m_repid, &m_dev_info);
m_mtu = m_dev_info.max_rx_pktlen - trex_dev_get_overhead_len(m_dev_info.max_rx_pktlen,
m_dev_info.max_mtu);
bool ret = fill_pci_dev(&m_dev_info, &m_pci_dev);
if ( has_pci() ) {
assert(ret);
char pci[18];
rte_pci_device_name(&m_pci_dev.addr, pci, sizeof(pci));
intf_info_st.pci_addr = pci;
intf_info_st.numa_node = m_dev_info.device->numa_node;
} else {
//assert(!ret);
intf_info_st.pci_addr = "N/A";
intf_info_st.numa_node = -1;
}
}
void DpdkTRexPortAttr::get_supported_speeds(supp_speeds_t &supp_speeds){
uint32_t speed_capa = m_dev_info.speed_capa;
if (speed_capa & RTE_ETH_LINK_SPEED_1G)
supp_speeds.push_back(RTE_ETH_SPEED_NUM_1G);
if (speed_capa & RTE_ETH_LINK_SPEED_2_5G)
supp_speeds.push_back(RTE_ETH_SPEED_NUM_2_5G);
if (speed_capa & RTE_ETH_LINK_SPEED_10G)
supp_speeds.push_back(RTE_ETH_SPEED_NUM_10G);
if (speed_capa & RTE_ETH_LINK_SPEED_25G)
supp_speeds.push_back(RTE_ETH_SPEED_NUM_25G);
if (speed_capa & RTE_ETH_LINK_SPEED_40G)
supp_speeds.push_back(RTE_ETH_SPEED_NUM_40G);
if (speed_capa & RTE_ETH_LINK_SPEED_100G)
supp_speeds.push_back(RTE_ETH_SPEED_NUM_100G);
if (speed_capa & RTE_ETH_LINK_SPEED_200G)
supp_speeds.push_back(RTE_ETH_SPEED_NUM_200G);
}
void DpdkTRexPortAttr::update_link_status(){
rte_eth_link_get(m_repid, &m_link);
if (m_link.link_speed > RTE_ETH_SPEED_NUM_200G ){
m_link.link_speed = RTE_ETH_SPEED_NUM_200G;
}
}
int DpdkTRexPortAttr::set_promiscuous(bool enable){
if ( !is_prom_change_supported() ) {
return -ENOTSUP;
}
if (enable) {
rte_eth_promiscuous_enable(m_repid);
}else{
rte_eth_promiscuous_disable(m_repid);
}
return 0;
}
int DpdkTRexPortAttr::set_multicast(bool enable){
if (enable) {
rte_eth_allmulticast_enable(m_repid);
}else{
rte_eth_allmulticast_disable(m_repid);
}
return 0;
}
int DpdkTRexPortAttr::set_link_up(bool up){
if (up) {
return rte_eth_dev_set_link_up(m_repid);
}else{
return rte_eth_dev_set_link_down(m_repid);
}
}
int DpdkTRexPortAttr::set_vxlan_fs(vxlan_fs_ports_t &vxlan_fs_ports) {
if ( vxlan_fs_ports != m_vxlan_fs_ports ) {
rte_eth_udp_tunnel udp_tunnel;
udp_tunnel.prot_type = RTE_ETH_TUNNEL_TYPE_VXLAN;
int ret = 0;
while ( m_vxlan_fs_ports.size() ) { // delete old ports
const auto &it = m_vxlan_fs_ports.begin();
udp_tunnel.udp_port = *it;
ret = rte_eth_dev_udp_tunnel_port_delete(m_port_id, &udp_tunnel);
if ( ret ) {
return ret;
}
m_vxlan_fs_ports.erase(it);
CGlobalInfo::m_options.m_ip_cfg[m_port_id].set_vxlan_fs(false);
}
for (auto &vxlan_fs_port : vxlan_fs_ports) { // add new ports
udp_tunnel.udp_port = vxlan_fs_port;
ret = rte_eth_dev_udp_tunnel_port_add(m_port_id, &udp_tunnel);
if ( ret ) {
return ret;
}
m_vxlan_fs_ports.insert(vxlan_fs_port);
CGlobalInfo::m_options.m_ip_cfg[m_port_id].set_vxlan_fs(true);
}
}
return 0;
}
bool DpdkTRexPortAttr::get_promiscuous(){
int ret=rte_eth_promiscuous_get(m_repid);
if (ret<0) {
rte_exit(EXIT_FAILURE, "rte_eth_promiscuous_get: "
"err=%d, port=%u\n",
ret, m_repid);
}
return ( ret?true:false);
}
bool DpdkTRexPortAttr::get_multicast(){
int ret=rte_eth_allmulticast_get(m_repid);
if (ret<0) {
rte_exit(EXIT_FAILURE, "rte_eth_allmulticast_get: "
"err=%d, port=%u\n",
ret, m_repid);
}
return ( ret?true:false);
}
void DpdkTRexPortAttr::get_hw_src_mac(struct rte_ether_addr *mac_addr){
rte_eth_macaddr_get(m_repid, mac_addr);
}