2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
20 #include <rte_version.h>
21 #include <rte_ethdev.h>
22 #include <rte_cycles.h>
23 #include <rte_byteorder.h>
25 #include "prox_malloc.h"
28 #include "stats_port.h"
29 #include "prox_port_cfg.h"
32 #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS)
34 /* Directly access hardware counters instead of going through DPDK. This allows getting
35 * specific counters that DPDK does not report or aggregates with other ones.
38 /* Definitions for IXGBE (taken from PMD) */
39 #define PROX_IXGBE_MPC(_i) (0x03FA0 + ((_i) * 4)) /* 8 of these 3FA0-3FBC*/
40 #define PROX_IXGBE_QBRC_L(_i) (0x01034 + ((_i) * 0x40)) /* 16 of these */
41 #define PROX_IXGBE_QBRC_H(_i) (0x01038 + ((_i) * 0x40)) /* 16 of these */
42 #define PROX_IXGBE_QPRC(_i) (0x01030 + ((_i) * 0x40)) /* 16 of these */
43 #define PROX_IXGBE_GPTC 0x04080
44 #define PROX_IXGBE_TPR 0x040D0
45 #define PROX_IXGBE_TORL 0x040C0
46 #define PROX_IXGBE_TORH 0x040C4
47 #define PROX_IXGBE_GOTCL 0x04090
48 #define PROX_IXGBE_GOTCH 0x04094
50 #define IXGBE_QUEUE_STAT_COUNTERS 16
52 static void ixgbe_read_stats(uint8_t port_id, struct port_stats_sample* stats, struct port_stats_sample *prev, int last_stat)
54 uint64_t before, after;
57 struct rte_eth_dev* dev = &rte_eth_devices[port_id];
59 /* WARNING: Assumes hardware address is first field of structure! This may change! */
60 struct _dev_hw* hw = (struct _dev_hw *)(dev->data->dev_private);
62 stats->no_mbufs = dev->data->rx_mbuf_alloc_failed;
64 /* Since we only read deltas from the NIC, we have to add to previous values
65 * even though we actually substract again later to find out the rates!
67 stats->ierrors = prev->ierrors;
68 stats->imissed = prev->imissed;
69 stats->rx_bytes = prev->rx_bytes;
70 stats->rx_tot = prev->rx_tot;
71 stats->tx_bytes = prev->tx_bytes;
72 stats->tx_tot = prev->tx_tot;
74 /* WARNING: In this implementation, we count as imiised only the "no descriptor"
75 * missed packets cases and not the actual receive errors.
78 for (i = 0; i < 8; i++) {
79 stats->imissed += PROX_READ_REG(hw, PROX_IXGBE_MPC(i));
84 /* This version is equivalent to what ixgbe PMD does. It only accounts for packets
85 * actually received on the host.
87 for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
89 stats->rx_tot += PROX_READ_REG(hw, PROX_IXGBE_QPRC(i));
91 stats->rx_bytes += PROX_READ_REG(hw, PROX_IXGBE_QBRC_L(i));
92 stats->rx_bytes += ((uint64_t)PROX_READ_REG(hw, PROX_IXGBE_QBRC_H(i)) << 32);
95 /* This version reports the packets received by the NIC, regardless of whether they
96 * reached the host or not, etc. (no need to add ierrors or imissedto this packet count)
98 stats->rx_tot += PROX_READ_REG(hw, PROX_IXGBE_TPR);
99 stats->rx_bytes += PROX_READ_REG(hw, PROX_IXGBE_TORL);
100 stats->rx_bytes += ((uint64_t)PROX_READ_REG(hw, PROX_IXGBE_TORH) << 32);
105 stats->tx_tot += PROX_READ_REG(hw, PROX_IXGBE_GPTC);
107 stats->tx_bytes += PROX_READ_REG(hw, PROX_IXGBE_GOTCL);
108 stats->tx_bytes += ((uint64_t)PROX_READ_REG(hw, PROX_IXGBE_GOTCH) << 32);
110 stats->tsc = (before >> 1) + (after >> 1);
115 extern int last_stat;
116 static struct port_stats port_stats[PROX_MAX_PORTS];
117 static uint8_t nb_interface;
118 static uint8_t n_ports;
119 static int num_xstats[PROX_MAX_PORTS] = {0};
120 static int num_ixgbe_xstats = 0;
122 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,1)
123 #define XSTATS_SUPPORT 1
125 #define XSTATS_SUPPORT 0
129 #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0)
130 static struct rte_eth_xstat *eth_xstats[PROX_MAX_PORTS] = {0};
131 static struct rte_eth_xstat_name *eth_xstat_names[PROX_MAX_PORTS] = {0};
133 static struct rte_eth_xstats *eth_xstats[PROX_MAX_PORTS] = {0};
134 static struct rte_eth_xstats *eth_xstat_names[PROX_MAX_PORTS] = {0};
136 static int xstat_tpr_offset[PROX_MAX_PORTS] ={0}, xstat_tor_offset[PROX_MAX_PORTS] = {0};
137 static int tx_pkt_size_offset[PROX_MAX_PORTS][PKT_SIZE_COUNT];
140 #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0)
141 static int find_xstats_str(struct rte_eth_xstat_name *xstats, int n, const char *name)
143 static int find_xstats_str(struct rte_eth_xstats *xstats, int n, const char *name)
146 for (int i = 0; i < n; i++) {
147 if (strcmp(xstats[i].name, name) == 0)
154 void stats_port_init(void)
156 int potential_ixgbe_warn = 0;
157 for (int i = 0; i < PROX_MAX_PORTS; i++) {
158 xstat_tpr_offset[i] = -1;
159 xstat_tor_offset[i] = -1;
160 for (int j = 0; j < PKT_SIZE_COUNT; j++) {
161 tx_pkt_size_offset[i][j] = -1;
165 nb_interface = prox_last_port_active() + 1;
166 n_ports = prox_nb_active_ports();
168 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
169 if (prox_port_cfg[port_id].active) {
170 #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0)
171 num_xstats[port_id] = rte_eth_xstats_get_names(port_id, NULL, 0);
172 eth_xstat_names[port_id] = prox_zmalloc(num_xstats[port_id] * sizeof(struct rte_eth_xstat_name), prox_port_cfg[port_id].socket);
173 PROX_PANIC(eth_xstat_names[port_id] == NULL, "Error allocating memory for xstats");
174 num_xstats[port_id] = rte_eth_xstats_get_names(port_id, eth_xstat_names[port_id], num_xstats[port_id]);
175 eth_xstats[port_id] = prox_zmalloc(num_xstats[port_id] * sizeof(struct rte_eth_xstat), prox_port_cfg[port_id].socket);
176 PROX_PANIC(eth_xstats[port_id] == NULL, "Error allocating memory for xstats");
178 num_xstats[port_id] = rte_eth_xstats_get(port_id, NULL, 0);
179 eth_xstats[port_id] = prox_zmalloc(num_xstats[port_id] * sizeof(struct rte_eth_xstats), prox_port_cfg[port_id].socket);
180 PROX_PANIC(eth_xstats[port_id] == NULL, "Error allocating memory for xstats");
181 eth_xstat_names[port_id] = eth_xstats[port_id];
182 num_xstats[port_id] = rte_eth_xstats_get(port_id, eth_xstats[port_id], num_xstats[port_id]);
184 if (!strcmp(prox_port_cfg[port_id].short_name, "ixgbe")) {
185 potential_ixgbe_warn = 1;
186 xstat_tor_offset[port_id] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "rx_total_bytes");
187 xstat_tpr_offset[port_id] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "rx_total_packets");
189 tx_pkt_size_offset[port_id][PKT_SIZE_64] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_64_packets");
190 tx_pkt_size_offset[port_id][PKT_SIZE_65] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_65_to_127_packets");
191 tx_pkt_size_offset[port_id][PKT_SIZE_128] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_128_to_255_packets");
192 tx_pkt_size_offset[port_id][PKT_SIZE_256] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_256_to_511_packets");
193 tx_pkt_size_offset[port_id][PKT_SIZE_512] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_512_to_1023_packets");
194 if (0 == strcmp(prox_port_cfg[port_id].short_name, "ixgbe")) {
195 tx_pkt_size_offset[port_id][PKT_SIZE_1024] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_1024_to_max_packets");
197 tx_pkt_size_offset[port_id][PKT_SIZE_1024] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_1024_to_1522_packets");
198 tx_pkt_size_offset[port_id][PKT_SIZE_1522] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_1523_to_max_packets");
200 plog_info("offset = %d, %d, %d, %d, %d, %d %d\n", tx_pkt_size_offset[port_id][PKT_SIZE_64], tx_pkt_size_offset[port_id][PKT_SIZE_65], tx_pkt_size_offset[port_id][PKT_SIZE_128], tx_pkt_size_offset[port_id][PKT_SIZE_256], tx_pkt_size_offset[port_id][PKT_SIZE_512], tx_pkt_size_offset[port_id][PKT_SIZE_1024], tx_pkt_size_offset[port_id][PKT_SIZE_1522]);
201 #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0)
202 prox_free(eth_xstat_names[port_id]);
204 if (num_xstats[port_id] == 0 || eth_xstats[port_id] == NULL) {
205 plog_warn("Failed to initialize xstat for port %d, running without xstats\n", port_id);
206 num_xstats[port_id] = 0;
210 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
211 if ((xstat_tor_offset[port_id] != -1) && (xstat_tpr_offset[port_id] != -1)) {
212 num_ixgbe_xstats = 2; // ixgbe PMD supports tor and tpr xstats
216 if ((num_ixgbe_xstats == 0) && (potential_ixgbe_warn))
217 plog_warn("Failed to initialize ixgbe xstat, running without ixgbe xstats\n");
221 static void nic_read_stats(uint8_t port_id)
223 unsigned is_ixgbe = (0 == strcmp(prox_port_cfg[port_id].short_name, "ixgbe"));
225 struct port_stats_sample *stats = &port_stats[port_id].sample[last_stat];
227 #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS)
229 struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat];
230 ixgbe_read_stats(port_id, stats, prev, last_stat);
234 uint64_t before, after;
236 struct rte_eth_stats eth_stat;
238 before = rte_rdtsc();
239 rte_eth_stats_get(port_id, ð_stat);
242 stats->tsc = (before >> 1) + (after >> 1);
243 stats->no_mbufs = eth_stat.rx_nombuf;
244 stats->ierrors = eth_stat.ierrors;
245 stats->imissed = eth_stat.imissed;
246 stats->oerrors = eth_stat.oerrors;
247 stats->rx_bytes = eth_stat.ibytes;
249 /* The goal would be to get the total number of bytes received
250 by the NIC (including overhead). Without the patch
251 (i.e. num_ixgbe_xstats == 0) we can't do this directly with
252 DPDK 2.1 API. So, we report the number of bytes (including
253 overhead) received by the host. */
256 if (num_xstats[port_id]) {
257 rte_eth_xstats_get(port_id, eth_xstats[port_id], num_xstats[port_id]);
258 for (size_t i = 0; i < sizeof(tx_pkt_size_offset[0])/sizeof(tx_pkt_size_offset[0][0]); ++i) {
259 if (tx_pkt_size_offset[port_id][i] != -1)
260 stats->tx_pkt_size[i] = (eth_xstats[port_id][tx_pkt_size_offset[port_id][i]]).value;
262 stats->tx_pkt_size[i] = -1;
265 for (size_t i = 0; i < sizeof(tx_pkt_size_offset[0])/sizeof(tx_pkt_size_offset[0][0]); ++i) {
266 stats->tx_pkt_size[i] = -1;
272 if (num_ixgbe_xstats) {
273 stats->rx_tot = eth_xstats[port_id][xstat_tpr_offset[port_id]].value;
274 stats->rx_bytes = eth_xstats[port_id][xstat_tor_offset[port_id]].value;
278 stats->rx_tot = eth_stat.ipackets + eth_stat.ierrors + eth_stat.imissed;
279 /* On ixgbe, the rx_bytes counts bytes
280 received by Host without overhead. The
281 rx_tot counts the number of packets
282 received by the NIC. If we only add 20 *
283 rx_tot to rx_bytes, the result will also
284 take into account 20 * "number of packets
285 dropped by the nic". Note that in case CRC
286 is stripped on ixgbe, the CRC bytes are not
288 if (prox_port_cfg[port_id].port_conf.rxmode.hw_strip_crc == 1)
289 stats->rx_bytes = eth_stat.ibytes +
290 (24 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
292 stats->rx_bytes = eth_stat.ibytes +
293 (20 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
295 } else if (strcmp(prox_port_cfg[port_id].short_name, "i40e_vf") == 0) {
296 // For I40E VF, imissed already part of received packets
297 stats->rx_tot = eth_stat.ipackets;
299 stats->rx_tot = eth_stat.ipackets + eth_stat.imissed;
301 stats->tx_tot = eth_stat.opackets;
302 stats->tx_bytes = eth_stat.obytes;
305 void stats_port_reset(void)
307 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
308 if (prox_port_cfg[port_id].active) {
309 rte_eth_stats_reset(port_id);
310 memset(&port_stats[port_id], 0, sizeof(struct port_stats));
315 void stats_port_update(void)
317 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
318 if (prox_port_cfg[port_id].active) {
319 nic_read_stats(port_id);
324 uint64_t stats_port_get_ierrors(void)
328 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
329 if (prox_port_cfg[port_id].active)
330 ret += port_stats[port_id].sample[last_stat].ierrors;
335 uint64_t stats_port_get_imissed(void)
339 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
340 if (prox_port_cfg[port_id].active)
341 ret += port_stats[port_id].sample[last_stat].imissed;
346 uint64_t stats_port_get_rx_packets(void)
350 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
351 if (prox_port_cfg[port_id].active)
352 ret += port_stats[port_id].sample[last_stat].rx_tot;
357 uint64_t stats_port_get_tx_packets(void)
361 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
362 if (prox_port_cfg[port_id].active)
363 ret += port_stats[port_id].sample[last_stat].tx_tot;
368 int stats_get_n_ports(void)
373 struct port_stats_sample *stats_get_port_stats_sample(uint32_t port_id, int l)
375 return &port_stats[port_id].sample[l == last_stat];
378 int stats_port(uint8_t port_id, struct get_port_stats *gps)
380 if (!prox_port_cfg[port_id].active)
383 struct port_stats_sample *last = &port_stats[port_id].sample[last_stat];
384 struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat];
386 gps->no_mbufs_diff = last->no_mbufs - prev->no_mbufs;
387 gps->ierrors_diff = last->ierrors - prev->ierrors;
388 gps->imissed_diff = last->imissed - prev->imissed;
389 gps->rx_bytes_diff = last->rx_bytes - prev->rx_bytes;
390 gps->tx_bytes_diff = last->tx_bytes - prev->tx_bytes;
391 gps->rx_pkts_diff = last->rx_tot - prev->rx_tot;
392 if (unlikely(prev->rx_tot > last->rx_tot))
393 gps->rx_pkts_diff = 0;
394 gps->tx_pkts_diff = last->tx_tot - prev->tx_tot;
395 if (unlikely(prev->tx_tot > last->tx_tot))
396 gps->rx_pkts_diff = 0;
397 gps->rx_tot = last->rx_tot;
398 gps->tx_tot = last->tx_tot;
399 gps->no_mbufs_tot = last->no_mbufs;
400 gps->ierrors_tot = last->ierrors;
401 gps->imissed_tot = last->imissed;
403 gps->last_tsc = last->tsc;
404 gps->prev_tsc = prev->tsc;