Merge "Fix latency accuracy and dumping latencies to file"
[samplevnf.git] / VNFs / DPPD-PROX / stats_port.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #include <string.h>
18 #include <stdio.h>
19
20 #include <rte_version.h>
21 #include <rte_ethdev.h>
22 #include <rte_cycles.h>
23 #include <rte_byteorder.h>
24
25 #include "prox_malloc.h"
26 #include "log.h"
27 #include "quit.h"
28 #include "stats_port.h"
29 #include "prox_port_cfg.h"
30 #include "rw_reg.h"
31
32 #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS)
33
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.
36  */
37
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
49
50 #define IXGBE_QUEUE_STAT_COUNTERS 16
51
52 static void ixgbe_read_stats(uint8_t port_id, struct port_stats_sample* stats, struct port_stats_sample *prev, int last_stat)
53 {
54         uint64_t before, after;
55         unsigned i;
56
57         struct rte_eth_dev* dev = &rte_eth_devices[port_id];
58
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);
61
62         stats->no_mbufs = dev->data->rx_mbuf_alloc_failed;
63
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!
66          */
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;
73
74         /* WARNING: In this implementation, we count as imiised only the "no descriptor"
75          * missed packets cases and not the actual receive errors.
76          */
77         before = rte_rdtsc();
78         for (i = 0; i < 8; i++) {
79                 stats->imissed += PROX_READ_REG(hw, PROX_IXGBE_MPC(i));
80         }
81
82         /* RX stats */
83 #if 0
84         /* This version is equivalent to what ixgbe PMD does. It only accounts for packets
85          * actually received on the host.
86          */
87         for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
88                 /* ipackets: */
89                 stats->rx_tot += PROX_READ_REG(hw, PROX_IXGBE_QPRC(i));
90                 /* ibytes: */
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);
93         }
94 #else
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)
97          */
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);
101 #endif
102
103         /* TX stats */
104         /* opackets: */
105         stats->tx_tot += PROX_READ_REG(hw, PROX_IXGBE_GPTC);
106         /* obytes: */
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);
109         after = rte_rdtsc();
110         stats->tsc = (before >> 1) + (after >> 1);
111 }
112
113 #endif
114
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;
121
122 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,1)
123 #define XSTATS_SUPPORT 1
124 #else
125 #define XSTATS_SUPPORT 0
126 #endif
127
128 #if XSTATS_SUPPORT
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};
132 #else
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};
135 #endif
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];
138 #endif
139
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)
142 #else
143 static int find_xstats_str(struct rte_eth_xstats *xstats, int n, const char *name)
144 #endif
145 {
146         for (int i = 0; i < n; i++) {
147                 if (strcmp(xstats[i].name, name) == 0)
148                         return i;
149         }
150
151         return -1;
152 }
153
154 void stats_port_init(void)
155 {
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;
162                 }
163         }
164 #if XSTATS_SUPPORT
165         nb_interface = prox_last_port_active() + 1;
166         n_ports = prox_nb_active_ports();
167
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");
177 #else
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]);
183 #endif
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");
188                         }
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");
196                         } else {
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");
199                         }
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]);
203 #endif
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;
207                         }
208                 }
209         }
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
213                         break;
214                 }
215         }
216         if ((num_ixgbe_xstats == 0) && (potential_ixgbe_warn))
217                 plog_warn("Failed to initialize ixgbe xstat, running without ixgbe xstats\n");
218 #endif
219 }
220
221 static void nic_read_stats(uint8_t port_id)
222 {
223         unsigned is_ixgbe = (0 == strcmp(prox_port_cfg[port_id].short_name, "ixgbe"));
224
225         struct port_stats_sample *stats = &port_stats[port_id].sample[last_stat];
226
227 #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS)
228         if (is_ixgbe) {
229                 struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat];
230                 ixgbe_read_stats(port_id, stats, prev, last_stat);
231                 return;
232         }
233 #endif
234         uint64_t before, after;
235
236         struct rte_eth_stats eth_stat;
237
238         before = rte_rdtsc();
239         rte_eth_stats_get(port_id, &eth_stat);
240         after = rte_rdtsc();
241
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;
248
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. */
254
255 #if XSTATS_SUPPORT
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;
261                         else
262                                 stats->tx_pkt_size[i] = -1;
263                 }
264         } else {
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;
267                 }
268         }
269 #endif
270         if (is_ixgbe) {
271 #if XSTATS_SUPPORT
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;
275                 } else
276 #endif
277                 {
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
287                            counted. */
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));
291                         else
292                                 stats->rx_bytes = eth_stat.ibytes +
293                                         (20 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
294                 }
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;
298         } else {
299                 stats->rx_tot = eth_stat.ipackets + eth_stat.imissed;
300         }
301         stats->tx_tot = eth_stat.opackets;
302         stats->tx_bytes = eth_stat.obytes;
303 }
304
305 void stats_port_reset(void)
306 {
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));
311                 }
312         }
313 }
314
315 void stats_port_update(void)
316 {
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);
320                 }
321         }
322 }
323
324 uint64_t stats_port_get_ierrors(void)
325 {
326         uint64_t ret = 0;
327
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;
331         }
332         return ret;
333 }
334
335 uint64_t stats_port_get_imissed(void)
336 {
337         uint64_t ret = 0;
338
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;
342         }
343         return ret;
344 }
345
346 uint64_t stats_port_get_rx_packets(void)
347 {
348         uint64_t ret = 0;
349
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;
353         }
354         return ret;
355 }
356
357 uint64_t stats_port_get_tx_packets(void)
358 {
359         uint64_t ret = 0;
360
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;
364         }
365         return ret;
366 }
367
368 int stats_get_n_ports(void)
369 {
370         return n_ports;
371 }
372
373 struct port_stats_sample *stats_get_port_stats_sample(uint32_t port_id, int l)
374 {
375         return &port_stats[port_id].sample[l == last_stat];
376 }
377
378 int stats_port(uint8_t port_id, struct get_port_stats *gps)
379 {
380         if (!prox_port_cfg[port_id].active)
381                 return -1;
382
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];
385
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;
402
403         gps->last_tsc = last->tsc;
404         gps->prev_tsc = prev->tsc;
405
406         return 0;
407 }