Update PROX to CRC STRIP default bahavior change in DPDK
[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 #include "prox_compat.h"
32
33 #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS)
34
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.
37  */
38
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
50
51 #define IXGBE_QUEUE_STAT_COUNTERS 16
52
53 static void ixgbe_read_stats(uint8_t port_id, struct port_stats_sample* stats, struct port_stats_sample *prev, int last_stat)
54 {
55         uint64_t before, after;
56         unsigned i;
57
58         struct rte_eth_dev* dev = &rte_eth_devices[port_id];
59
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);
62
63         stats->no_mbufs = dev->data->rx_mbuf_alloc_failed;
64
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!
67          */
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;
74
75         /* WARNING: In this implementation, we count as imiised only the "no descriptor"
76          * missed packets cases and not the actual receive errors.
77          */
78         before = rte_rdtsc();
79         for (i = 0; i < 8; i++) {
80                 stats->imissed += PROX_READ_REG(hw, PROX_IXGBE_MPC(i));
81         }
82
83         /* RX stats */
84 #if 0
85         /* This version is equivalent to what ixgbe PMD does. It only accounts for packets
86          * actually received on the host.
87          */
88         for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
89                 /* ipackets: */
90                 stats->rx_tot += PROX_READ_REG(hw, PROX_IXGBE_QPRC(i));
91                 /* ibytes: */
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);
94         }
95 #else
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)
98          */
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);
102 #endif
103
104         /* TX stats */
105         /* opackets: */
106         stats->tx_tot += PROX_READ_REG(hw, PROX_IXGBE_GPTC);
107         /* obytes: */
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);
110         after = rte_rdtsc();
111         stats->tsc = (before >> 1) + (after >> 1);
112 }
113
114 #endif
115
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;
122
123 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,1)
124 #define XSTATS_SUPPORT 1
125 #else
126 #define XSTATS_SUPPORT 0
127 #endif
128
129 #if XSTATS_SUPPORT
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};
133 #else
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};
136 #endif
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];
139 #endif
140
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)
143 #else
144 static int find_xstats_str(struct rte_eth_xstats *xstats, int n, const char *name)
145 #endif
146 {
147         for (int i = 0; i < n; i++) {
148                 if (strcmp(xstats[i].name, name) == 0)
149                         return i;
150         }
151
152         return -1;
153 }
154
155 void stats_port_init(void)
156 {
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;
163                 }
164         }
165 #if XSTATS_SUPPORT
166         nb_interface = prox_last_port_active() + 1;
167         n_ports = prox_nb_active_ports();
168
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");
178 #else
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]);
184 #endif
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");
189                         }
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");
197                         } else {
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");
200                         }
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]);
204 #endif
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;
208                         }
209                 }
210         }
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
214                         break;
215                 }
216         }
217         if ((num_ixgbe_xstats == 0) && (potential_ixgbe_warn))
218                 plog_warn("Failed to initialize ixgbe xstat, running without ixgbe xstats\n");
219 #endif
220 }
221
222 static void nic_read_stats(uint8_t port_id)
223 {
224         unsigned is_ixgbe = (0 == strcmp(prox_port_cfg[port_id].short_name, "ixgbe"));
225
226         struct port_stats_sample *stats = &port_stats[port_id].sample[last_stat];
227
228 #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS)
229         if (is_ixgbe) {
230                 struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat];
231                 ixgbe_read_stats(port_id, stats, prev, last_stat);
232                 return;
233         }
234 #endif
235         uint64_t before, after;
236
237         struct rte_eth_stats eth_stat;
238
239         before = rte_rdtsc();
240         rte_eth_stats_get(port_id, &eth_stat);
241         after = rte_rdtsc();
242
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;
249
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. */
255
256 #if XSTATS_SUPPORT
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;
262                         else
263                                 stats->tx_pkt_size[i] = -1;
264                 }
265         } else {
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;
268                 }
269         }
270 #endif
271         if (is_ixgbe) {
272 #if XSTATS_SUPPORT
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;
276                 } else
277 #endif
278                 {
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
288                            counted. */
289 #if defined (DEV_RX_OFFLOAD_CRC_STRIP)
290                         if (prox_port_cfg[port_id].requested_rx_offload & DEV_RX_OFFLOAD_CRC_STRIP)
291                                 stats->rx_bytes = eth_stat.ibytes +
292                                         (24 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
293                         else
294                                 stats->rx_bytes = eth_stat.ibytes +
295                                         (20 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
296 #else
297 #if defined (DEV_RX_OFFLOAD_KEEP_CRC)
298                         if (prox_port_cfg[port_id].requested_rx_offload & DEV_RX_OFFLOAD_KEEP_CRC)
299                                 stats->rx_bytes = eth_stat.ibytes +
300                                         (20 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
301                         else
302                                 stats->rx_bytes = eth_stat.ibytes +
303                                         (24 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed));
304 #endif
305 #endif
306                 }
307         } else if (strcmp(prox_port_cfg[port_id].short_name, "i40e_vf") == 0) {
308                 // For I40E VF, imissed already part of received packets
309                 stats->rx_tot = eth_stat.ipackets;
310         } else {
311                 stats->rx_tot = eth_stat.ipackets + eth_stat.imissed;
312         }
313         stats->tx_tot = eth_stat.opackets;
314         stats->tx_bytes = eth_stat.obytes;
315 }
316
317 void stats_port_reset(void)
318 {
319         for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
320                 if (prox_port_cfg[port_id].active) {
321                         rte_eth_stats_reset(port_id);
322                         memset(&port_stats[port_id], 0, sizeof(struct port_stats));
323                 }
324         }
325 }
326
327 void stats_port_update(void)
328 {
329         for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
330                 if (prox_port_cfg[port_id].active) {
331                         nic_read_stats(port_id);
332                 }
333         }
334 }
335
336 uint64_t stats_port_get_ierrors(void)
337 {
338         uint64_t ret = 0;
339
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].ierrors;
343         }
344         return ret;
345 }
346
347 uint64_t stats_port_get_imissed(void)
348 {
349         uint64_t ret = 0;
350
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].imissed;
354         }
355         return ret;
356 }
357
358 uint64_t stats_port_get_rx_packets(void)
359 {
360         uint64_t ret = 0;
361
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].rx_tot;
365         }
366         return ret;
367 }
368
369 uint64_t stats_port_get_tx_packets(void)
370 {
371         uint64_t ret = 0;
372
373         for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) {
374                 if (prox_port_cfg[port_id].active)
375                         ret += port_stats[port_id].sample[last_stat].tx_tot;
376         }
377         return ret;
378 }
379
380 int stats_get_n_ports(void)
381 {
382         return n_ports;
383 }
384
385 struct port_stats_sample *stats_get_port_stats_sample(uint32_t port_id, int l)
386 {
387         return &port_stats[port_id].sample[l == last_stat];
388 }
389
390 int stats_port(uint8_t port_id, struct get_port_stats *gps)
391 {
392         if (!prox_port_cfg[port_id].active)
393                 return -1;
394
395         struct port_stats_sample *last = &port_stats[port_id].sample[last_stat];
396         struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat];
397
398         gps->no_mbufs_diff = last->no_mbufs - prev->no_mbufs;
399         gps->ierrors_diff = last->ierrors - prev->ierrors;
400         gps->imissed_diff = last->imissed - prev->imissed;
401         gps->rx_bytes_diff = last->rx_bytes - prev->rx_bytes;
402         gps->tx_bytes_diff = last->tx_bytes - prev->tx_bytes;
403         gps->rx_pkts_diff = last->rx_tot - prev->rx_tot;
404         if (unlikely(prev->rx_tot > last->rx_tot))
405                 gps->rx_pkts_diff = 0;
406         gps->tx_pkts_diff = last->tx_tot - prev->tx_tot;
407         if (unlikely(prev->tx_tot > last->tx_tot))
408                 gps->rx_pkts_diff = 0;
409         gps->rx_tot = last->rx_tot;
410         gps->tx_tot = last->tx_tot;
411         gps->no_mbufs_tot = last->no_mbufs;
412         gps->ierrors_tot = last->ierrors;
413         gps->imissed_tot = last->imissed;
414
415         gps->last_tsc = last->tsc;
416         gps->prev_tsc = prev->tsc;
417
418         return 0;
419 }