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"
31 #include "prox_compat.h"
33 #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS)
35 /* Directly access hardware counters instead of going through DPDK. This allows getting
36 * specific counters that DPDK does not report or aggregates with other ones.
39 /* Definitions for IXGBE (taken from PMD) */
40 #define PROX_IXGBE_MPC(_i) (0x03FA0 + ((_i) * 4)) /* 8 of these 3FA0-3FBC*/
41 #define PROX_IXGBE_QBRC_L(_i) (0x01034 + ((_i) * 0x40)) /* 16 of these */
42 #define PROX_IXGBE_QBRC_H(_i) (0x01038 + ((_i) * 0x40)) /* 16 of these */
43 #define PROX_IXGBE_QPRC(_i) (0x01030 + ((_i) * 0x40)) /* 16 of these */
44 #define PROX_IXGBE_GPTC 0x04080
45 #define PROX_IXGBE_TPR 0x040D0
46 #define PROX_IXGBE_TORL 0x040C0
47 #define PROX_IXGBE_TORH 0x040C4
48 #define PROX_IXGBE_GOTCL 0x04090
49 #define PROX_IXGBE_GOTCH 0x04094
51 #define IXGBE_QUEUE_STAT_COUNTERS 16
53 static void ixgbe_read_stats(uint8_t port_id, struct port_stats_sample* stats, struct port_stats_sample *prev, int last_stat)
55 uint64_t before, after;
58 struct rte_eth_dev* dev = &rte_eth_devices[port_id];
60 /* WARNING: Assumes hardware address is first field of structure! This may change! */
61 struct _dev_hw* hw = (struct _dev_hw *)(dev->data->dev_private);
63 stats->no_mbufs = dev->data->rx_mbuf_alloc_failed;
65 /* Since we only read deltas from the NIC, we have to add to previous values
66 * even though we actually substract again later to find out the rates!
68 stats->ierrors = prev->ierrors;
69 stats->imissed = prev->imissed;
70 stats->rx_bytes = prev->rx_bytes;
71 stats->rx_tot = prev->rx_tot;
72 stats->tx_bytes = prev->tx_bytes;
73 stats->tx_tot = prev->tx_tot;
75 /* WARNING: In this implementation, we count as imiised only the "no descriptor"
76 * missed packets cases and not the actual receive errors.
79 for (i = 0; i < 8; i++) {
80 stats->imissed += PROX_READ_REG(hw, PROX_IXGBE_MPC(i));
85 /* This version is equivalent to what ixgbe PMD does. It only accounts for packets
86 * actually received on the host.
88 for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
90 stats->rx_tot += PROX_READ_REG(hw, PROX_IXGBE_QPRC(i));
92 stats->rx_bytes += PROX_READ_REG(hw, PROX_IXGBE_QBRC_L(i));
93 stats->rx_bytes += ((uint64_t)PROX_READ_REG(hw, PROX_IXGBE_QBRC_H(i)) << 32);
96 /* This version reports the packets received by the NIC, regardless of whether they
97 * reached the host or not, etc. (no need to add ierrors or imissedto this packet count)
99 stats->rx_tot += PROX_READ_REG(hw, PROX_IXGBE_TPR);
100 stats->rx_bytes += PROX_READ_REG(hw, PROX_IXGBE_TORL);
101 stats->rx_bytes += ((uint64_t)PROX_READ_REG(hw, PROX_IXGBE_TORH) << 32);
106 stats->tx_tot += PROX_READ_REG(hw, PROX_IXGBE_GPTC);
108 stats->tx_bytes += PROX_READ_REG(hw, PROX_IXGBE_GOTCL);
109 stats->tx_bytes += ((uint64_t)PROX_READ_REG(hw, PROX_IXGBE_GOTCH) << 32);
111 stats->tsc = (before >> 1) + (after >> 1);
116 extern int last_stat;
117 static struct port_stats port_stats[PROX_MAX_PORTS];
118 static uint8_t nb_interface;
119 static uint8_t n_ports;
120 static int num_xstats[PROX_MAX_PORTS] = {0};
121 static int num_ixgbe_xstats = 0;
123 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,1)
124 #define XSTATS_SUPPORT 1
126 #define XSTATS_SUPPORT 0
130 #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0)
131 static struct rte_eth_xstat *eth_xstats[PROX_MAX_PORTS] = {0};
132 static struct rte_eth_xstat_name *eth_xstat_names[PROX_MAX_PORTS] = {0};
134 static struct rte_eth_xstats *eth_xstats[PROX_MAX_PORTS] = {0};
135 static struct rte_eth_xstats *eth_xstat_names[PROX_MAX_PORTS] = {0};
137 static int xstat_tpr_offset[PROX_MAX_PORTS] ={0}, xstat_tor_offset[PROX_MAX_PORTS] = {0};
138 static int tx_pkt_size_offset[PROX_MAX_PORTS][PKT_SIZE_COUNT];
141 #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0)
142 static int find_xstats_str(struct rte_eth_xstat_name *xstats, int n, const char *name)
144 static int find_xstats_str(struct rte_eth_xstats *xstats, int n, const char *name)
147 for (int i = 0; i < n; i++) {
148 if (strcmp(xstats[i].name, name) == 0)
155 void stats_port_init(void)
157 int potential_ixgbe_warn = 0;
158 for (int i = 0; i < PROX_MAX_PORTS; i++) {
159 xstat_tpr_offset[i] = -1;
160 xstat_tor_offset[i] = -1;
161 for (int j = 0; j < PKT_SIZE_COUNT; j++) {
162 tx_pkt_size_offset[i][j] = -1;
166 nb_interface = prox_last_port_active() + 1;
167 n_ports = prox_nb_active_ports();
169 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
170 if (prox_port_cfg[port_id].active) {
171 #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0)
172 num_xstats[port_id] = rte_eth_xstats_get_names(port_id, NULL, 0);
173 eth_xstat_names[port_id] = prox_zmalloc(num_xstats[port_id] * sizeof(struct rte_eth_xstat_name), prox_port_cfg[port_id].socket);
174 PROX_PANIC(eth_xstat_names[port_id] == NULL, "Error allocating memory for xstats");
175 num_xstats[port_id] = rte_eth_xstats_get_names(port_id, eth_xstat_names[port_id], num_xstats[port_id]);
176 eth_xstats[port_id] = prox_zmalloc(num_xstats[port_id] * sizeof(struct rte_eth_xstat), prox_port_cfg[port_id].socket);
177 PROX_PANIC(eth_xstats[port_id] == NULL, "Error allocating memory for xstats");
179 num_xstats[port_id] = rte_eth_xstats_get(port_id, NULL, 0);
180 eth_xstats[port_id] = prox_zmalloc(num_xstats[port_id] * sizeof(struct rte_eth_xstats), prox_port_cfg[port_id].socket);
181 PROX_PANIC(eth_xstats[port_id] == NULL, "Error allocating memory for xstats");
182 eth_xstat_names[port_id] = eth_xstats[port_id];
183 num_xstats[port_id] = rte_eth_xstats_get(port_id, eth_xstats[port_id], num_xstats[port_id]);
185 if (!strcmp(prox_port_cfg[port_id].short_name, "ixgbe")) {
186 potential_ixgbe_warn = 1;
187 xstat_tor_offset[port_id] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "rx_total_bytes");
188 xstat_tpr_offset[port_id] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "rx_total_packets");
190 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");
191 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");
192 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");
193 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");
194 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");
195 if (0 == strcmp(prox_port_cfg[port_id].short_name, "ixgbe")) {
196 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");
198 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");
199 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");
201 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]);
202 #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0)
203 prox_free(eth_xstat_names[port_id]);
205 if (num_xstats[port_id] == 0 || eth_xstats[port_id] == NULL) {
206 plog_warn("Failed to initialize xstat for port %d, running without xstats\n", port_id);
207 num_xstats[port_id] = 0;
211 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
212 if ((xstat_tor_offset[port_id] != -1) && (xstat_tpr_offset[port_id] != -1)) {
213 num_ixgbe_xstats = 2; // ixgbe PMD supports tor and tpr xstats
217 if ((num_ixgbe_xstats == 0) && (potential_ixgbe_warn))
218 plog_warn("Failed to initialize ixgbe xstat, running without ixgbe xstats\n");
222 static void nic_read_stats(uint8_t port_id)
224 unsigned is_ixgbe = (0 == strcmp(prox_port_cfg[port_id].short_name, "ixgbe"));
226 struct port_stats_sample *stats = &port_stats[port_id].sample[last_stat];
228 #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS)
230 struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat];
231 ixgbe_read_stats(port_id, stats, prev, last_stat);
235 uint64_t before, after;
237 struct rte_eth_stats eth_stat;
239 before = rte_rdtsc();
240 rte_eth_stats_get(port_id, ð_stat);
243 stats->tsc = (before >> 1) + (after >> 1);
244 stats->no_mbufs = eth_stat.rx_nombuf;
245 stats->ierrors = eth_stat.ierrors;
246 stats->imissed = eth_stat.imissed;
247 stats->oerrors = eth_stat.oerrors;
248 stats->rx_bytes = eth_stat.ibytes;
250 /* The goal would be to get the total number of bytes received
251 by the NIC (including overhead). Without the patch
252 (i.e. num_ixgbe_xstats == 0) we can't do this directly with
253 DPDK 2.1 API. So, we report the number of bytes (including
254 overhead) received by the host. */
257 if (num_xstats[port_id]) {
258 rte_eth_xstats_get(port_id, eth_xstats[port_id], num_xstats[port_id]);
259 for (size_t i = 0; i < sizeof(tx_pkt_size_offset[0])/sizeof(tx_pkt_size_offset[0][0]); ++i) {
260 if (tx_pkt_size_offset[port_id][i] != -1)
261 stats->tx_pkt_size[i] = (eth_xstats[port_id][tx_pkt_size_offset[port_id][i]]).value;
263 stats->tx_pkt_size[i] = -1;
266 for (size_t i = 0; i < sizeof(tx_pkt_size_offset[0])/sizeof(tx_pkt_size_offset[0][0]); ++i) {
267 stats->tx_pkt_size[i] = -1;
273 if (num_ixgbe_xstats) {
274 stats->rx_tot = eth_xstats[port_id][xstat_tpr_offset[port_id]].value;
275 stats->rx_bytes = eth_xstats[port_id][xstat_tor_offset[port_id]].value;
279 stats->rx_tot = eth_stat.ipackets + eth_stat.ierrors + eth_stat.imissed;
280 /* On ixgbe, the rx_bytes counts bytes
281 received by Host without overhead. The
282 rx_tot counts the number of packets
283 received by the NIC. If we only add 20 *
284 rx_tot to rx_bytes, the result will also
285 take into account 20 * "number of packets
286 dropped by the nic". Note that in case CRC
287 is stripped on ixgbe, the CRC bytes are not
289 if (prox_port_cfg[port_id].requested_rx_offload & DEV_RX_OFFLOAD_CRC_STRIP)
290 stats->rx_bytes = eth_stat.ibytes +
291 (24 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
293 stats->rx_bytes = eth_stat.ibytes +
294 (20 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
296 } else if (strcmp(prox_port_cfg[port_id].short_name, "i40e_vf") == 0) {
297 // For I40E VF, imissed already part of received packets
298 stats->rx_tot = eth_stat.ipackets;
300 stats->rx_tot = eth_stat.ipackets + eth_stat.imissed;
302 stats->tx_tot = eth_stat.opackets;
303 stats->tx_bytes = eth_stat.obytes;
306 void stats_port_reset(void)
308 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
309 if (prox_port_cfg[port_id].active) {
310 rte_eth_stats_reset(port_id);
311 memset(&port_stats[port_id], 0, sizeof(struct port_stats));
316 void stats_port_update(void)
318 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
319 if (prox_port_cfg[port_id].active) {
320 nic_read_stats(port_id);
325 uint64_t stats_port_get_ierrors(void)
329 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
330 if (prox_port_cfg[port_id].active)
331 ret += port_stats[port_id].sample[last_stat].ierrors;
336 uint64_t stats_port_get_imissed(void)
340 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
341 if (prox_port_cfg[port_id].active)
342 ret += port_stats[port_id].sample[last_stat].imissed;
347 uint64_t stats_port_get_rx_packets(void)
351 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
352 if (prox_port_cfg[port_id].active)
353 ret += port_stats[port_id].sample[last_stat].rx_tot;
358 uint64_t stats_port_get_tx_packets(void)
362 for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
363 if (prox_port_cfg[port_id].active)
364 ret += port_stats[port_id].sample[last_stat].tx_tot;
369 int stats_get_n_ports(void)
374 struct port_stats_sample *stats_get_port_stats_sample(uint32_t port_id, int l)
376 return &port_stats[port_id].sample[l == last_stat];
379 int stats_port(uint8_t port_id, struct get_port_stats *gps)
381 if (!prox_port_cfg[port_id].active)
384 struct port_stats_sample *last = &port_stats[port_id].sample[last_stat];
385 struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat];
387 gps->no_mbufs_diff = last->no_mbufs - prev->no_mbufs;
388 gps->ierrors_diff = last->ierrors - prev->ierrors;
389 gps->imissed_diff = last->imissed - prev->imissed;
390 gps->rx_bytes_diff = last->rx_bytes - prev->rx_bytes;
391 gps->tx_bytes_diff = last->tx_bytes - prev->tx_bytes;
392 gps->rx_pkts_diff = last->rx_tot - prev->rx_tot;
393 if (unlikely(prev->rx_tot > last->rx_tot))
394 gps->rx_pkts_diff = 0;
395 gps->tx_pkts_diff = last->tx_tot - prev->tx_tot;
396 if (unlikely(prev->tx_tot > last->tx_tot))
397 gps->rx_pkts_diff = 0;
398 gps->rx_tot = last->rx_tot;
399 gps->tx_tot = last->tx_tot;
400 gps->no_mbufs_tot = last->no_mbufs;
401 gps->ierrors_tot = last->ierrors;
402 gps->imissed_tot = last->imissed;
404 gps->last_tsc = last->tsc;
405 gps->prev_tsc = prev->tsc;