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