2 * Copyright (C) ARM Limited 2010-2015. 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)
76 /* Network counters are not currently supported in RT-Preempt full because mod_timer is used */
77 #ifndef CONFIG_PREEMPT_RT_FULL
80 dir = gatorfs_mkdir(sb, root, "Linux_net_rx");
83 gatorfs_create_ulong(sb, dir, "enabled", &netrx_enabled);
84 gatorfs_create_ro_ulong(sb, dir, "key", &netrx_key);
86 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);
96 static int gator_events_net_start(void)
99 netPrev[NETRX] = rx_total;
100 netPrev[NETTX] = tx_total;
101 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
102 setup_timer(&net_wake_up_timer, net_wake_up_handler, 0);
104 setup_deferrable_timer_on_stack(&net_wake_up_timer, net_wake_up_handler, 0);
109 static void gator_events_net_stop(void)
111 del_timer_sync(&net_wake_up_timer);
116 static int gator_events_net_read(int **buffer, bool sched_switch)
118 int len, rx_delta, tx_delta;
119 static int last_rx_delta, last_tx_delta;
121 if (!on_primary_core())
124 if (!netrx_enabled && !nettx_enabled)
127 mod_timer(&net_wake_up_timer, jiffies + 1);
129 calculate_delta(&rx_delta, &tx_delta);
132 if (netrx_enabled && last_rx_delta != rx_delta) {
133 last_rx_delta = rx_delta;
134 netGet[len++] = netrx_key;
135 /* indicates to Streamline that rx_delta bytes were transmitted now, not since the last message */
137 netGet[len++] = netrx_key;
138 netGet[len++] = rx_delta;
141 if (nettx_enabled && last_tx_delta != tx_delta) {
142 last_tx_delta = tx_delta;
143 netGet[len++] = nettx_key;
144 /* indicates to Streamline that tx_delta bytes were transmitted now, not since the last message */
146 netGet[len++] = nettx_key;
147 netGet[len++] = tx_delta;
156 static struct gator_interface gator_events_net_interface = {
157 .create_files = gator_events_net_create_files,
158 .start = gator_events_net_start,
159 .stop = gator_events_net_stop,
160 .read = gator_events_net_read,
163 int gator_events_net_init(void)
165 netrx_key = gator_events_get_key();
166 nettx_key = gator_events_get_key();
171 return gator_events_install(&gator_events_net_interface);