blob: 8bc14b0d1def0a6847437f7ade9a0abc4217b770 [file] [log] [blame]
Greg Kroah-Hartman0caa8cd2017-11-24 15:00:37 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Data gathering module for Linux-VM Monitor Stream, Stage 1.
4 * Collects accumulated network statistics (Packets received/transmitted,
5 * dropped, errors, ...).
6 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02007 * Copyright IBM Corp. 2003, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Gerald Schaefer5b5dd212006-06-29 15:08:35 +02009 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/kernel_stat.h>
16#include <linux/netdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070017#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "appldata.h"
20
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
23 * Network data
24 *
25 * This is accessed as binary data by z/VM. If changes to it can't be avoided,
26 * the structure version (product ID, see appldata_base.c) needs to be changed
27 * as well and all documentation and z/VM applications using it must be updated.
28 *
29 * The record layout is documented in the Linux for zSeries Device Drivers
30 * book:
31 * http://oss.software.ibm.com/developerworks/opensource/linux390/index.shtml
32 */
Sebastian Ott2ab14612013-06-25 15:37:44 +020033struct appldata_net_sum_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 u64 timestamp;
35 u32 sync_count_1; /* after VM collected the record data, */
36 u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
37 same. If not, the record has been updated on
38 the Linux side while VM was collecting the
39 (possibly corrupt) data */
40
41 u32 nr_interfaces; /* nr. of network interfaces being monitored */
42
43 u32 padding; /* next value is 64-bit aligned, so these */
44 /* 4 byte would be padded out by compiler */
45
46 u64 rx_packets; /* total packets received */
47 u64 tx_packets; /* total packets transmitted */
48 u64 rx_bytes; /* total bytes received */
49 u64 tx_bytes; /* total bytes transmitted */
50 u64 rx_errors; /* bad packets received */
51 u64 tx_errors; /* packet transmit problems */
52 u64 rx_dropped; /* no space in linux buffers */
53 u64 tx_dropped; /* no space available in linux */
54 u64 collisions; /* collisions while transmitting */
Sebastian Ott2ab14612013-06-25 15:37:44 +020055} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * appldata_get_net_sum_data()
60 *
61 * gather accumulated network statistics
62 */
63static void appldata_get_net_sum_data(void *data)
64{
65 int i;
66 struct appldata_net_sum_data *net_data;
67 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned long rx_packets, tx_packets, rx_bytes, tx_bytes, rx_errors,
69 tx_errors, rx_dropped, tx_dropped, collisions;
70
71 net_data = data;
72 net_data->sync_count_1++;
73
74 i = 0;
75 rx_packets = 0;
76 tx_packets = 0;
77 rx_bytes = 0;
78 tx_bytes = 0;
79 rx_errors = 0;
80 tx_errors = 0;
81 rx_dropped = 0;
82 tx_dropped = 0;
83 collisions = 0;
stephen hemmingere576b9e2009-11-10 07:54:52 +000084
85 rcu_read_lock();
86 for_each_netdev_rcu(&init_net, dev) {
Heiko Carstense402e382010-08-13 10:06:42 +020087 const struct rtnl_link_stats64 *stats;
Eric Dumazet28172732010-07-07 14:58:56 -070088 struct rtnl_link_stats64 temp;
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -080089
Heiko Carstense402e382010-08-13 10:06:42 +020090 stats = dev_get_stats(dev, &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 rx_packets += stats->rx_packets;
92 tx_packets += stats->tx_packets;
93 rx_bytes += stats->rx_bytes;
94 tx_bytes += stats->tx_bytes;
95 rx_errors += stats->rx_errors;
96 tx_errors += stats->tx_errors;
97 rx_dropped += stats->rx_dropped;
98 tx_dropped += stats->tx_dropped;
99 collisions += stats->collisions;
100 i++;
101 }
stephen hemmingere576b9e2009-11-10 07:54:52 +0000102 rcu_read_unlock();
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 net_data->nr_interfaces = i;
105 net_data->rx_packets = rx_packets;
106 net_data->tx_packets = tx_packets;
107 net_data->rx_bytes = rx_bytes;
108 net_data->tx_bytes = tx_bytes;
109 net_data->rx_errors = rx_errors;
110 net_data->tx_errors = tx_errors;
111 net_data->rx_dropped = rx_dropped;
112 net_data->tx_dropped = tx_dropped;
113 net_data->collisions = collisions;
114
Heiko Carstens1aae0562013-01-30 09:49:40 +0100115 net_data->timestamp = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 net_data->sync_count_2++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119
120static struct appldata_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .name = "net_sum",
122 .record_nr = APPLDATA_RECORD_NET_SUM_ID,
123 .size = sizeof(struct appldata_net_sum_data),
124 .callback = &appldata_get_net_sum_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .owner = THIS_MODULE,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200126 .mod_lvl = {0xF0, 0xF0}, /* EBCDIC "00" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129
130/*
131 * appldata_net_init()
132 *
133 * init data, register ops
134 */
135static int __init appldata_net_init(void)
136{
Sebastian Ott2ab14612013-06-25 15:37:44 +0200137 int ret;
138
139 ops.data = kzalloc(sizeof(struct appldata_net_sum_data), GFP_KERNEL);
140 if (!ops.data)
141 return -ENOMEM;
142
143 ret = appldata_register_ops(&ops);
144 if (ret)
145 kfree(ops.data);
146
147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150/*
151 * appldata_net_exit()
152 *
153 * unregister ops
154 */
155static void __exit appldata_net_exit(void)
156{
157 appldata_unregister_ops(&ops);
Sebastian Ott2ab14612013-06-25 15:37:44 +0200158 kfree(ops.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161
162module_init(appldata_net_init);
163module_exit(appldata_net_exit);
164
165MODULE_LICENSE("GPL");
166MODULE_AUTHOR("Gerald Schaefer");
167MODULE_DESCRIPTION("Linux-VM Monitor Stream, accumulated network statistics");