2 * Copyright (C) ARM Limited 2010-2013. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
11 #include <linux/netdevice.h>
12 #include <linux/hardirq.h>
18 static ulong netrx_enabled;
19 static ulong nettx_enabled;
20 static ulong netrx_key;
21 static ulong nettx_key;
22 static int rx_total, tx_total;
23 static ulong netPrev[TOTALNET];
24 static int netGet[TOTALNET * 4];
26 static struct timer_list net_wake_up_timer;
28 // Must be run in process context as the kernel function dev_get_stats() can sleep
29 static void get_network_stats(struct work_struct *wsptr)
32 struct net_device *dev;
34 for_each_netdev(&init_net, dev) {
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
36 const struct net_device_stats *stats = dev_get_stats(dev);
38 struct rtnl_link_stats64 temp;
39 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
41 rx += stats->rx_bytes;
42 tx += stats->tx_bytes;
48 DECLARE_WORK(wq_get_stats, get_network_stats);
50 static void net_wake_up_handler(unsigned long unused_data)
52 // had to delay scheduling work as attempting to schedule work during the context switch is illegal in kernel versions 3.5 and greater
53 schedule_work(&wq_get_stats);
56 static void calculate_delta(int *rx, int *tx)
60 rx_calc = (int)(rx_total - netPrev[NETRX]);
63 netPrev[NETRX] += rx_calc;
65 tx_calc = (int)(tx_total - netPrev[NETTX]);
68 netPrev[NETTX] += tx_calc;
74 static int gator_events_net_create_files(struct super_block *sb, struct dentry *root)
78 dir = gatorfs_mkdir(sb, root, "Linux_net_rx");
82 gatorfs_create_ulong(sb, dir, "enabled", &netrx_enabled);
83 gatorfs_create_ro_ulong(sb, dir, "key", &netrx_key);
85 dir = gatorfs_mkdir(sb, root, "Linux_net_tx");
89 gatorfs_create_ulong(sb, dir, "enabled", &nettx_enabled);
90 gatorfs_create_ro_ulong(sb, dir, "key", &nettx_key);
95 static int gator_events_net_start(void)
98 netPrev[NETRX] = rx_total;
99 netPrev[NETTX] = tx_total;
100 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
101 setup_timer(&net_wake_up_timer, net_wake_up_handler, 0);
103 setup_deferrable_timer_on_stack(&net_wake_up_timer, net_wake_up_handler, 0);
108 static void gator_events_net_stop(void)
110 del_timer_sync(&net_wake_up_timer);
115 static int gator_events_net_read(int **buffer)
117 int len, rx_delta, tx_delta;
118 static int last_rx_delta = 0, last_tx_delta = 0;
120 if (!on_primary_core())
123 if (!netrx_enabled && !nettx_enabled)
126 mod_timer(&net_wake_up_timer, jiffies + 1);
128 calculate_delta(&rx_delta, &tx_delta);
131 if (netrx_enabled && last_rx_delta != rx_delta) {
132 last_rx_delta = rx_delta;
133 netGet[len++] = netrx_key;
134 netGet[len++] = 0; // indicates to Streamline that rx_delta bytes were transmitted now, not since the last message
135 netGet[len++] = netrx_key;
136 netGet[len++] = rx_delta;
139 if (nettx_enabled && last_tx_delta != tx_delta) {
140 last_tx_delta = tx_delta;
141 netGet[len++] = nettx_key;
142 netGet[len++] = 0; // indicates to Streamline that tx_delta bytes were transmitted now, not since the last message
143 netGet[len++] = nettx_key;
144 netGet[len++] = tx_delta;
153 static struct gator_interface gator_events_net_interface = {
154 .create_files = gator_events_net_create_files,
155 .start = gator_events_net_start,
156 .stop = gator_events_net_stop,
157 .read = gator_events_net_read,
160 int gator_events_net_init(void)
162 netrx_key = gator_events_get_key();
163 nettx_key = gator_events_get_key();
168 return gator_events_install(&gator_events_net_interface);
171 gator_events_init(gator_events_net_init);