ionic_stats.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
  3. #include <linux/ethtool.h>
  4. #include <linux/kernel.h>
  5. #include <linux/mutex.h>
  6. #include <linux/netdevice.h>
  7. #include "ionic.h"
  8. #include "ionic_lif.h"
  9. #include "ionic_stats.h"
  10. static const struct ionic_stat_desc ionic_lif_stats_desc[] = {
  11. IONIC_LIF_STAT_DESC(tx_packets),
  12. IONIC_LIF_STAT_DESC(tx_bytes),
  13. IONIC_LIF_STAT_DESC(rx_packets),
  14. IONIC_LIF_STAT_DESC(rx_bytes),
  15. IONIC_LIF_STAT_DESC(tx_tso),
  16. IONIC_LIF_STAT_DESC(tx_tso_bytes),
  17. IONIC_LIF_STAT_DESC(tx_csum_none),
  18. IONIC_LIF_STAT_DESC(tx_csum),
  19. IONIC_LIF_STAT_DESC(rx_csum_none),
  20. IONIC_LIF_STAT_DESC(rx_csum_complete),
  21. IONIC_LIF_STAT_DESC(rx_csum_error),
  22. IONIC_LIF_STAT_DESC(hw_tx_dropped),
  23. IONIC_LIF_STAT_DESC(hw_rx_dropped),
  24. IONIC_LIF_STAT_DESC(hw_rx_over_errors),
  25. IONIC_LIF_STAT_DESC(hw_rx_missed_errors),
  26. IONIC_LIF_STAT_DESC(hw_tx_aborted_errors),
  27. IONIC_LIF_STAT_DESC(xdp_drop),
  28. IONIC_LIF_STAT_DESC(xdp_aborted),
  29. IONIC_LIF_STAT_DESC(xdp_pass),
  30. IONIC_LIF_STAT_DESC(xdp_tx),
  31. IONIC_LIF_STAT_DESC(xdp_redirect),
  32. IONIC_LIF_STAT_DESC(xdp_frames),
  33. };
  34. static const struct ionic_stat_desc ionic_port_stats_desc[] = {
  35. IONIC_PORT_STAT_DESC(frames_rx_ok),
  36. IONIC_PORT_STAT_DESC(frames_rx_all),
  37. IONIC_PORT_STAT_DESC(frames_rx_bad_fcs),
  38. IONIC_PORT_STAT_DESC(frames_rx_bad_all),
  39. IONIC_PORT_STAT_DESC(octets_rx_ok),
  40. IONIC_PORT_STAT_DESC(octets_rx_all),
  41. IONIC_PORT_STAT_DESC(frames_rx_unicast),
  42. IONIC_PORT_STAT_DESC(frames_rx_multicast),
  43. IONIC_PORT_STAT_DESC(frames_rx_broadcast),
  44. IONIC_PORT_STAT_DESC(frames_rx_pause),
  45. IONIC_PORT_STAT_DESC(frames_rx_bad_length),
  46. IONIC_PORT_STAT_DESC(frames_rx_undersized),
  47. IONIC_PORT_STAT_DESC(frames_rx_oversized),
  48. IONIC_PORT_STAT_DESC(frames_rx_fragments),
  49. IONIC_PORT_STAT_DESC(frames_rx_jabber),
  50. IONIC_PORT_STAT_DESC(frames_rx_pripause),
  51. IONIC_PORT_STAT_DESC(frames_rx_stomped_crc),
  52. IONIC_PORT_STAT_DESC(frames_rx_too_long),
  53. IONIC_PORT_STAT_DESC(frames_rx_vlan_good),
  54. IONIC_PORT_STAT_DESC(frames_rx_dropped),
  55. IONIC_PORT_STAT_DESC(frames_rx_less_than_64b),
  56. IONIC_PORT_STAT_DESC(frames_rx_64b),
  57. IONIC_PORT_STAT_DESC(frames_rx_65b_127b),
  58. IONIC_PORT_STAT_DESC(frames_rx_128b_255b),
  59. IONIC_PORT_STAT_DESC(frames_rx_256b_511b),
  60. IONIC_PORT_STAT_DESC(frames_rx_512b_1023b),
  61. IONIC_PORT_STAT_DESC(frames_rx_1024b_1518b),
  62. IONIC_PORT_STAT_DESC(frames_rx_1519b_2047b),
  63. IONIC_PORT_STAT_DESC(frames_rx_2048b_4095b),
  64. IONIC_PORT_STAT_DESC(frames_rx_4096b_8191b),
  65. IONIC_PORT_STAT_DESC(frames_rx_8192b_9215b),
  66. IONIC_PORT_STAT_DESC(frames_rx_other),
  67. IONIC_PORT_STAT_DESC(frames_tx_ok),
  68. IONIC_PORT_STAT_DESC(frames_tx_all),
  69. IONIC_PORT_STAT_DESC(frames_tx_bad),
  70. IONIC_PORT_STAT_DESC(octets_tx_ok),
  71. IONIC_PORT_STAT_DESC(octets_tx_total),
  72. IONIC_PORT_STAT_DESC(frames_tx_unicast),
  73. IONIC_PORT_STAT_DESC(frames_tx_multicast),
  74. IONIC_PORT_STAT_DESC(frames_tx_broadcast),
  75. IONIC_PORT_STAT_DESC(frames_tx_pause),
  76. IONIC_PORT_STAT_DESC(frames_tx_pripause),
  77. IONIC_PORT_STAT_DESC(frames_tx_vlan),
  78. IONIC_PORT_STAT_DESC(frames_tx_less_than_64b),
  79. IONIC_PORT_STAT_DESC(frames_tx_64b),
  80. IONIC_PORT_STAT_DESC(frames_tx_65b_127b),
  81. IONIC_PORT_STAT_DESC(frames_tx_128b_255b),
  82. IONIC_PORT_STAT_DESC(frames_tx_256b_511b),
  83. IONIC_PORT_STAT_DESC(frames_tx_512b_1023b),
  84. IONIC_PORT_STAT_DESC(frames_tx_1024b_1518b),
  85. IONIC_PORT_STAT_DESC(frames_tx_1519b_2047b),
  86. IONIC_PORT_STAT_DESC(frames_tx_2048b_4095b),
  87. IONIC_PORT_STAT_DESC(frames_tx_4096b_8191b),
  88. IONIC_PORT_STAT_DESC(frames_tx_8192b_9215b),
  89. IONIC_PORT_STAT_DESC(frames_tx_other),
  90. IONIC_PORT_STAT_DESC(frames_tx_pri_0),
  91. IONIC_PORT_STAT_DESC(frames_tx_pri_1),
  92. IONIC_PORT_STAT_DESC(frames_tx_pri_2),
  93. IONIC_PORT_STAT_DESC(frames_tx_pri_3),
  94. IONIC_PORT_STAT_DESC(frames_tx_pri_4),
  95. IONIC_PORT_STAT_DESC(frames_tx_pri_5),
  96. IONIC_PORT_STAT_DESC(frames_tx_pri_6),
  97. IONIC_PORT_STAT_DESC(frames_tx_pri_7),
  98. IONIC_PORT_STAT_DESC(frames_rx_pri_0),
  99. IONIC_PORT_STAT_DESC(frames_rx_pri_1),
  100. IONIC_PORT_STAT_DESC(frames_rx_pri_2),
  101. IONIC_PORT_STAT_DESC(frames_rx_pri_3),
  102. IONIC_PORT_STAT_DESC(frames_rx_pri_4),
  103. IONIC_PORT_STAT_DESC(frames_rx_pri_5),
  104. IONIC_PORT_STAT_DESC(frames_rx_pri_6),
  105. IONIC_PORT_STAT_DESC(frames_rx_pri_7),
  106. IONIC_PORT_STAT_DESC(tx_pripause_0_1us_count),
  107. IONIC_PORT_STAT_DESC(tx_pripause_1_1us_count),
  108. IONIC_PORT_STAT_DESC(tx_pripause_2_1us_count),
  109. IONIC_PORT_STAT_DESC(tx_pripause_3_1us_count),
  110. IONIC_PORT_STAT_DESC(tx_pripause_4_1us_count),
  111. IONIC_PORT_STAT_DESC(tx_pripause_5_1us_count),
  112. IONIC_PORT_STAT_DESC(tx_pripause_6_1us_count),
  113. IONIC_PORT_STAT_DESC(tx_pripause_7_1us_count),
  114. IONIC_PORT_STAT_DESC(rx_pripause_0_1us_count),
  115. IONIC_PORT_STAT_DESC(rx_pripause_1_1us_count),
  116. IONIC_PORT_STAT_DESC(rx_pripause_2_1us_count),
  117. IONIC_PORT_STAT_DESC(rx_pripause_3_1us_count),
  118. IONIC_PORT_STAT_DESC(rx_pripause_4_1us_count),
  119. IONIC_PORT_STAT_DESC(rx_pripause_5_1us_count),
  120. IONIC_PORT_STAT_DESC(rx_pripause_6_1us_count),
  121. IONIC_PORT_STAT_DESC(rx_pripause_7_1us_count),
  122. IONIC_PORT_STAT_DESC(rx_pause_1us_count),
  123. IONIC_PORT_STAT_DESC(frames_tx_truncated),
  124. };
  125. static const struct ionic_stat_desc ionic_tx_stats_desc[] = {
  126. IONIC_TX_STAT_DESC(pkts),
  127. IONIC_TX_STAT_DESC(bytes),
  128. IONIC_TX_STAT_DESC(clean),
  129. IONIC_TX_STAT_DESC(dma_map_err),
  130. IONIC_TX_STAT_DESC(linearize),
  131. IONIC_TX_STAT_DESC(frags),
  132. IONIC_TX_STAT_DESC(tso),
  133. IONIC_TX_STAT_DESC(tso_bytes),
  134. IONIC_TX_STAT_DESC(hwstamp_valid),
  135. IONIC_TX_STAT_DESC(hwstamp_invalid),
  136. IONIC_TX_STAT_DESC(csum_none),
  137. IONIC_TX_STAT_DESC(csum),
  138. IONIC_TX_STAT_DESC(vlan_inserted),
  139. IONIC_TX_STAT_DESC(xdp_frames),
  140. };
  141. static const struct ionic_stat_desc ionic_rx_stats_desc[] = {
  142. IONIC_RX_STAT_DESC(pkts),
  143. IONIC_RX_STAT_DESC(bytes),
  144. IONIC_RX_STAT_DESC(dma_map_err),
  145. IONIC_RX_STAT_DESC(alloc_err),
  146. IONIC_RX_STAT_DESC(csum_none),
  147. IONIC_RX_STAT_DESC(csum_complete),
  148. IONIC_RX_STAT_DESC(csum_error),
  149. IONIC_RX_STAT_DESC(hwstamp_valid),
  150. IONIC_RX_STAT_DESC(hwstamp_invalid),
  151. IONIC_RX_STAT_DESC(dropped),
  152. IONIC_RX_STAT_DESC(vlan_stripped),
  153. IONIC_RX_STAT_DESC(xdp_drop),
  154. IONIC_RX_STAT_DESC(xdp_aborted),
  155. IONIC_RX_STAT_DESC(xdp_pass),
  156. IONIC_RX_STAT_DESC(xdp_tx),
  157. IONIC_RX_STAT_DESC(xdp_redirect),
  158. };
  159. #define IONIC_NUM_LIF_STATS ARRAY_SIZE(ionic_lif_stats_desc)
  160. #define IONIC_NUM_PORT_STATS ARRAY_SIZE(ionic_port_stats_desc)
  161. #define IONIC_NUM_TX_STATS ARRAY_SIZE(ionic_tx_stats_desc)
  162. #define IONIC_NUM_RX_STATS ARRAY_SIZE(ionic_rx_stats_desc)
  163. #define MAX_Q(lif) ((lif)->netdev->real_num_tx_queues)
  164. static void ionic_add_lif_txq_stats(struct ionic_lif *lif, int q_num,
  165. struct ionic_lif_sw_stats *stats)
  166. {
  167. struct ionic_tx_stats *txstats = &lif->txqstats[q_num];
  168. stats->tx_packets += txstats->pkts;
  169. stats->tx_bytes += txstats->bytes;
  170. stats->tx_tso += txstats->tso;
  171. stats->tx_tso_bytes += txstats->tso_bytes;
  172. stats->tx_csum_none += txstats->csum_none;
  173. stats->tx_csum += txstats->csum;
  174. stats->tx_hwstamp_valid += txstats->hwstamp_valid;
  175. stats->tx_hwstamp_invalid += txstats->hwstamp_invalid;
  176. stats->xdp_frames += txstats->xdp_frames;
  177. }
  178. static void ionic_add_lif_rxq_stats(struct ionic_lif *lif, int q_num,
  179. struct ionic_lif_sw_stats *stats)
  180. {
  181. struct ionic_rx_stats *rxstats = &lif->rxqstats[q_num];
  182. stats->rx_packets += rxstats->pkts;
  183. stats->rx_bytes += rxstats->bytes;
  184. stats->rx_csum_none += rxstats->csum_none;
  185. stats->rx_csum_complete += rxstats->csum_complete;
  186. stats->rx_csum_error += rxstats->csum_error;
  187. stats->rx_hwstamp_valid += rxstats->hwstamp_valid;
  188. stats->rx_hwstamp_invalid += rxstats->hwstamp_invalid;
  189. stats->xdp_drop += rxstats->xdp_drop;
  190. stats->xdp_aborted += rxstats->xdp_aborted;
  191. stats->xdp_pass += rxstats->xdp_pass;
  192. stats->xdp_tx += rxstats->xdp_tx;
  193. stats->xdp_redirect += rxstats->xdp_redirect;
  194. }
  195. static void ionic_get_lif_stats(struct ionic_lif *lif,
  196. struct ionic_lif_sw_stats *stats)
  197. {
  198. struct rtnl_link_stats64 ns;
  199. int q_num;
  200. memset(stats, 0, sizeof(*stats));
  201. for (q_num = 0; q_num < MAX_Q(lif); q_num++) {
  202. ionic_add_lif_txq_stats(lif, q_num, stats);
  203. ionic_add_lif_rxq_stats(lif, q_num, stats);
  204. }
  205. if (lif->hwstamp_txq)
  206. ionic_add_lif_txq_stats(lif, lif->hwstamp_txq->q.index, stats);
  207. if (lif->hwstamp_rxq)
  208. ionic_add_lif_rxq_stats(lif, lif->hwstamp_rxq->q.index, stats);
  209. ionic_get_stats64(lif->netdev, &ns);
  210. stats->hw_tx_dropped = ns.tx_dropped;
  211. stats->hw_rx_dropped = ns.rx_dropped;
  212. stats->hw_rx_over_errors = ns.rx_over_errors;
  213. stats->hw_rx_missed_errors = ns.rx_missed_errors;
  214. stats->hw_tx_aborted_errors = ns.tx_aborted_errors;
  215. }
  216. static u64 ionic_sw_stats_get_count(struct ionic_lif *lif)
  217. {
  218. u64 total = 0, tx_queues = MAX_Q(lif), rx_queues = MAX_Q(lif);
  219. if (lif->hwstamp_txq)
  220. tx_queues += 1;
  221. if (lif->hwstamp_rxq)
  222. rx_queues += 1;
  223. total += IONIC_NUM_LIF_STATS;
  224. total += IONIC_NUM_PORT_STATS;
  225. total += tx_queues * IONIC_NUM_TX_STATS;
  226. total += rx_queues * IONIC_NUM_RX_STATS;
  227. return total;
  228. }
  229. static void ionic_sw_stats_get_tx_strings(struct ionic_lif *lif, u8 **buf,
  230. int q_num)
  231. {
  232. int i;
  233. for (i = 0; i < IONIC_NUM_TX_STATS; i++)
  234. ethtool_sprintf(buf, "tx_%d_%s", q_num,
  235. ionic_tx_stats_desc[i].name);
  236. }
  237. static void ionic_sw_stats_get_rx_strings(struct ionic_lif *lif, u8 **buf,
  238. int q_num)
  239. {
  240. int i;
  241. for (i = 0; i < IONIC_NUM_RX_STATS; i++)
  242. ethtool_sprintf(buf, "rx_%d_%s", q_num,
  243. ionic_rx_stats_desc[i].name);
  244. }
  245. static void ionic_sw_stats_get_strings(struct ionic_lif *lif, u8 **buf)
  246. {
  247. int i, q_num;
  248. for (i = 0; i < IONIC_NUM_LIF_STATS; i++)
  249. ethtool_puts(buf, ionic_lif_stats_desc[i].name);
  250. for (i = 0; i < IONIC_NUM_PORT_STATS; i++)
  251. ethtool_puts(buf, ionic_port_stats_desc[i].name);
  252. for (q_num = 0; q_num < MAX_Q(lif); q_num++)
  253. ionic_sw_stats_get_tx_strings(lif, buf, q_num);
  254. if (lif->hwstamp_txq)
  255. ionic_sw_stats_get_tx_strings(lif, buf, lif->hwstamp_txq->q.index);
  256. for (q_num = 0; q_num < MAX_Q(lif); q_num++)
  257. ionic_sw_stats_get_rx_strings(lif, buf, q_num);
  258. if (lif->hwstamp_rxq)
  259. ionic_sw_stats_get_rx_strings(lif, buf, lif->hwstamp_rxq->q.index);
  260. }
  261. static void ionic_sw_stats_get_txq_values(struct ionic_lif *lif, u64 **buf,
  262. int q_num)
  263. {
  264. struct ionic_tx_stats *txstats;
  265. int i;
  266. txstats = &lif->txqstats[q_num];
  267. for (i = 0; i < IONIC_NUM_TX_STATS; i++) {
  268. **buf = IONIC_READ_STAT64(txstats, &ionic_tx_stats_desc[i]);
  269. (*buf)++;
  270. }
  271. }
  272. static void ionic_sw_stats_get_rxq_values(struct ionic_lif *lif, u64 **buf,
  273. int q_num)
  274. {
  275. struct ionic_rx_stats *rxstats;
  276. int i;
  277. rxstats = &lif->rxqstats[q_num];
  278. for (i = 0; i < IONIC_NUM_RX_STATS; i++) {
  279. **buf = IONIC_READ_STAT64(rxstats, &ionic_rx_stats_desc[i]);
  280. (*buf)++;
  281. }
  282. }
  283. static void ionic_sw_stats_get_values(struct ionic_lif *lif, u64 **buf)
  284. {
  285. struct ionic_port_stats *port_stats;
  286. struct ionic_lif_sw_stats lif_stats;
  287. int i, q_num;
  288. ionic_get_lif_stats(lif, &lif_stats);
  289. for (i = 0; i < IONIC_NUM_LIF_STATS; i++) {
  290. **buf = IONIC_READ_STAT64(&lif_stats, &ionic_lif_stats_desc[i]);
  291. (*buf)++;
  292. }
  293. port_stats = &lif->ionic->idev.port_info->stats;
  294. for (i = 0; i < IONIC_NUM_PORT_STATS; i++) {
  295. **buf = IONIC_READ_STAT_LE64(port_stats,
  296. &ionic_port_stats_desc[i]);
  297. (*buf)++;
  298. }
  299. for (q_num = 0; q_num < MAX_Q(lif); q_num++)
  300. ionic_sw_stats_get_txq_values(lif, buf, q_num);
  301. if (lif->hwstamp_txq)
  302. ionic_sw_stats_get_txq_values(lif, buf, lif->hwstamp_txq->q.index);
  303. for (q_num = 0; q_num < MAX_Q(lif); q_num++)
  304. ionic_sw_stats_get_rxq_values(lif, buf, q_num);
  305. if (lif->hwstamp_rxq)
  306. ionic_sw_stats_get_rxq_values(lif, buf, lif->hwstamp_rxq->q.index);
  307. }
  308. const struct ionic_stats_group_intf ionic_stats_groups[] = {
  309. /* SW Stats group */
  310. {
  311. .get_strings = ionic_sw_stats_get_strings,
  312. .get_values = ionic_sw_stats_get_values,
  313. .get_count = ionic_sw_stats_get_count,
  314. },
  315. /* Add more stat groups here */
  316. };
  317. const int ionic_num_stats_grps = ARRAY_SIZE(ionic_stats_groups);