appldata_net_sum.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Data gathering module for Linux-VM Monitor Stream, Stage 1.
  4. * Collects accumulated network statistics (Packets received/transmitted,
  5. * dropped, errors, ...).
  6. *
  7. * Copyright IBM Corp. 2003, 2006
  8. *
  9. * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/errno.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/netdevice.h>
  16. #include <net/net_namespace.h>
  17. #include "appldata.h"
  18. /*
  19. * Network data
  20. *
  21. * This is accessed as binary data by z/VM. If changes to it can't be avoided,
  22. * the structure version (product ID, see appldata_base.c) needs to be changed
  23. * as well and all documentation and z/VM applications using it must be updated.
  24. */
  25. struct appldata_net_sum_data {
  26. u64 timestamp;
  27. u32 sync_count_1; /* after VM collected the record data, */
  28. u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
  29. same. If not, the record has been updated on
  30. the Linux side while VM was collecting the
  31. (possibly corrupt) data */
  32. u32 nr_interfaces; /* nr. of network interfaces being monitored */
  33. u32 padding; /* next value is 64-bit aligned, so these */
  34. /* 4 byte would be padded out by compiler */
  35. u64 rx_packets; /* total packets received */
  36. u64 tx_packets; /* total packets transmitted */
  37. u64 rx_bytes; /* total bytes received */
  38. u64 tx_bytes; /* total bytes transmitted */
  39. u64 rx_errors; /* bad packets received */
  40. u64 tx_errors; /* packet transmit problems */
  41. u64 rx_dropped; /* no space in linux buffers */
  42. u64 tx_dropped; /* no space available in linux */
  43. u64 collisions; /* collisions while transmitting */
  44. } __packed;
  45. /*
  46. * appldata_get_net_sum_data()
  47. *
  48. * gather accumulated network statistics
  49. */
  50. static void appldata_get_net_sum_data(void *data)
  51. {
  52. int i;
  53. struct appldata_net_sum_data *net_data;
  54. struct net_device *dev;
  55. unsigned long rx_packets, tx_packets, rx_bytes, tx_bytes, rx_errors,
  56. tx_errors, rx_dropped, tx_dropped, collisions;
  57. net_data = data;
  58. net_data->sync_count_1++;
  59. i = 0;
  60. rx_packets = 0;
  61. tx_packets = 0;
  62. rx_bytes = 0;
  63. tx_bytes = 0;
  64. rx_errors = 0;
  65. tx_errors = 0;
  66. rx_dropped = 0;
  67. tx_dropped = 0;
  68. collisions = 0;
  69. rcu_read_lock();
  70. for_each_netdev_rcu(&init_net, dev) {
  71. const struct rtnl_link_stats64 *stats;
  72. struct rtnl_link_stats64 temp;
  73. stats = dev_get_stats(dev, &temp);
  74. rx_packets += stats->rx_packets;
  75. tx_packets += stats->tx_packets;
  76. rx_bytes += stats->rx_bytes;
  77. tx_bytes += stats->tx_bytes;
  78. rx_errors += stats->rx_errors;
  79. tx_errors += stats->tx_errors;
  80. rx_dropped += stats->rx_dropped;
  81. tx_dropped += stats->tx_dropped;
  82. collisions += stats->collisions;
  83. i++;
  84. }
  85. rcu_read_unlock();
  86. net_data->nr_interfaces = i;
  87. net_data->rx_packets = rx_packets;
  88. net_data->tx_packets = tx_packets;
  89. net_data->rx_bytes = rx_bytes;
  90. net_data->tx_bytes = tx_bytes;
  91. net_data->rx_errors = rx_errors;
  92. net_data->tx_errors = tx_errors;
  93. net_data->rx_dropped = rx_dropped;
  94. net_data->tx_dropped = tx_dropped;
  95. net_data->collisions = collisions;
  96. net_data->timestamp = get_tod_clock();
  97. net_data->sync_count_2++;
  98. }
  99. static struct appldata_ops ops = {
  100. .name = "net_sum",
  101. .record_nr = APPLDATA_RECORD_NET_SUM_ID,
  102. .size = sizeof(struct appldata_net_sum_data),
  103. .callback = &appldata_get_net_sum_data,
  104. .owner = THIS_MODULE,
  105. .mod_lvl = {0xF0, 0xF0}, /* EBCDIC "00" */
  106. };
  107. /*
  108. * appldata_net_init()
  109. *
  110. * init data, register ops
  111. */
  112. static int __init appldata_net_init(void)
  113. {
  114. int ret;
  115. ops.data = kzalloc(sizeof(struct appldata_net_sum_data), GFP_KERNEL);
  116. if (!ops.data)
  117. return -ENOMEM;
  118. ret = appldata_register_ops(&ops);
  119. if (ret)
  120. kfree(ops.data);
  121. return ret;
  122. }
  123. /*
  124. * appldata_net_exit()
  125. *
  126. * unregister ops
  127. */
  128. static void __exit appldata_net_exit(void)
  129. {
  130. appldata_unregister_ops(&ops);
  131. kfree(ops.data);
  132. }
  133. module_init(appldata_net_init);
  134. module_exit(appldata_net_exit);
  135. MODULE_LICENSE("GPL");
  136. MODULE_AUTHOR("Gerald Schaefer");
  137. MODULE_DESCRIPTION("Linux-VM Monitor Stream, accumulated network statistics");