1
2
3
4
5
6
7
8
9
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
18#include "appldata.h"
19
20
21
22
23
24
25
26
27
28
29
30
31
32struct appldata_net_sum_data {
33 u64 timestamp;
34 u32 sync_count_1;
35 u32 sync_count_2;
36
37
38
39
40 u32 nr_interfaces;
41
42 u32 padding;
43
44
45 u64 rx_packets;
46 u64 tx_packets;
47 u64 rx_bytes;
48 u64 tx_bytes;
49 u64 rx_errors;
50 u64 tx_errors;
51 u64 rx_dropped;
52 u64 tx_dropped;
53 u64 collisions;
54} __packed;
55
56
57
58
59
60
61
62static void appldata_get_net_sum_data(void *data)
63{
64 int i;
65 struct appldata_net_sum_data *net_data;
66 struct net_device *dev;
67 unsigned long rx_packets, tx_packets, rx_bytes, tx_bytes, rx_errors,
68 tx_errors, rx_dropped, tx_dropped, collisions;
69
70 net_data = data;
71 net_data->sync_count_1++;
72
73 i = 0;
74 rx_packets = 0;
75 tx_packets = 0;
76 rx_bytes = 0;
77 tx_bytes = 0;
78 rx_errors = 0;
79 tx_errors = 0;
80 rx_dropped = 0;
81 tx_dropped = 0;
82 collisions = 0;
83
84 rcu_read_lock();
85 for_each_netdev_rcu(&init_net, dev) {
86 const struct rtnl_link_stats64 *stats;
87 struct rtnl_link_stats64 temp;
88
89 stats = dev_get_stats(dev, &temp);
90 rx_packets += stats->rx_packets;
91 tx_packets += stats->tx_packets;
92 rx_bytes += stats->rx_bytes;
93 tx_bytes += stats->tx_bytes;
94 rx_errors += stats->rx_errors;
95 tx_errors += stats->tx_errors;
96 rx_dropped += stats->rx_dropped;
97 tx_dropped += stats->tx_dropped;
98 collisions += stats->collisions;
99 i++;
100 }
101 rcu_read_unlock();
102
103 net_data->nr_interfaces = i;
104 net_data->rx_packets = rx_packets;
105 net_data->tx_packets = tx_packets;
106 net_data->rx_bytes = rx_bytes;
107 net_data->tx_bytes = tx_bytes;
108 net_data->rx_errors = rx_errors;
109 net_data->tx_errors = tx_errors;
110 net_data->rx_dropped = rx_dropped;
111 net_data->tx_dropped = tx_dropped;
112 net_data->collisions = collisions;
113
114 net_data->timestamp = get_tod_clock();
115 net_data->sync_count_2++;
116}
117
118
119static struct appldata_ops ops = {
120 .name = "net_sum",
121 .record_nr = APPLDATA_RECORD_NET_SUM_ID,
122 .size = sizeof(struct appldata_net_sum_data),
123 .callback = &appldata_get_net_sum_data,
124 .owner = THIS_MODULE,
125 .mod_lvl = {0xF0, 0xF0},
126};
127
128
129
130
131
132
133
134static int __init appldata_net_init(void)
135{
136 int ret;
137
138 ops.data = kzalloc(sizeof(struct appldata_net_sum_data), GFP_KERNEL);
139 if (!ops.data)
140 return -ENOMEM;
141
142 ret = appldata_register_ops(&ops);
143 if (ret)
144 kfree(ops.data);
145
146 return ret;
147}
148
149
150
151
152
153
154static void __exit appldata_net_exit(void)
155{
156 appldata_unregister_ops(&ops);
157 kfree(ops.data);
158}
159
160
161module_init(appldata_net_init);
162module_exit(appldata_net_exit);
163
164MODULE_LICENSE("GPL");
165MODULE_AUTHOR("Gerald Schaefer");
166MODULE_DESCRIPTION("Linux-VM Monitor Stream, accumulated network statistics");
167