appldata_net_sum.c 4.2 KB

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