Merge branch 'linux-3.10.y' of git://git.kernel.org/pub/scm/linux/kernel/git/stable...
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_events_net.c
1 /**
2  * Copyright (C) ARM Limited 2010-2015. All rights reserved.
3  *
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.
7  *
8  */
9
10 #include "gator.h"
11 #include <linux/netdevice.h>
12 #include <linux/hardirq.h>
13
14 #define NETRX           0
15 #define NETTX           1
16 #define TOTALNET        2
17
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];
25
26 static struct timer_list net_wake_up_timer;
27
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)
30 {
31         int rx = 0, tx = 0;
32         struct net_device *dev;
33
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);
37 #else
38                 struct rtnl_link_stats64 temp;
39                 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
40 #endif
41                 rx += stats->rx_bytes;
42                 tx += stats->tx_bytes;
43         }
44         rx_total = rx;
45         tx_total = tx;
46 }
47
48 DECLARE_WORK(wq_get_stats, get_network_stats);
49
50 static void net_wake_up_handler(unsigned long unused_data)
51 {
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);
54 }
55
56 static void calculate_delta(int *rx, int *tx)
57 {
58         int rx_calc, tx_calc;
59
60         rx_calc = (int)(rx_total - netPrev[NETRX]);
61         if (rx_calc < 0)
62                 rx_calc = 0;
63         netPrev[NETRX] += rx_calc;
64
65         tx_calc = (int)(tx_total - netPrev[NETTX]);
66         if (tx_calc < 0)
67                 tx_calc = 0;
68         netPrev[NETTX] += tx_calc;
69
70         *rx = rx_calc;
71         *tx = tx_calc;
72 }
73
74 static int gator_events_net_create_files(struct super_block *sb, struct dentry *root)
75 {
76         /* Network counters are not currently supported in RT-Preempt full because mod_timer is used */
77 #ifndef CONFIG_PREEMPT_RT_FULL
78         struct dentry *dir;
79
80         dir = gatorfs_mkdir(sb, root, "Linux_net_rx");
81         if (!dir)
82                 return -1;
83         gatorfs_create_ulong(sb, dir, "enabled", &netrx_enabled);
84         gatorfs_create_ro_ulong(sb, dir, "key", &netrx_key);
85
86         dir = gatorfs_mkdir(sb, root, "Linux_net_tx");
87         if (!dir)
88                 return -1;
89         gatorfs_create_ulong(sb, dir, "enabled", &nettx_enabled);
90         gatorfs_create_ro_ulong(sb, dir, "key", &nettx_key);
91 #endif
92
93         return 0;
94 }
95
96 static int gator_events_net_start(void)
97 {
98         get_network_stats(0);
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);
103 #else
104         setup_deferrable_timer_on_stack(&net_wake_up_timer, net_wake_up_handler, 0);
105 #endif
106         return 0;
107 }
108
109 static void gator_events_net_stop(void)
110 {
111         del_timer_sync(&net_wake_up_timer);
112         netrx_enabled = 0;
113         nettx_enabled = 0;
114 }
115
116 static int gator_events_net_read(int **buffer, bool sched_switch)
117 {
118         int len, rx_delta, tx_delta;
119         static int last_rx_delta, last_tx_delta;
120
121         if (!on_primary_core())
122                 return 0;
123
124         if (!netrx_enabled && !nettx_enabled)
125                 return 0;
126
127         mod_timer(&net_wake_up_timer, jiffies + 1);
128
129         calculate_delta(&rx_delta, &tx_delta);
130
131         len = 0;
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 */
136                 netGet[len++] = 0;
137                 netGet[len++] = netrx_key;
138                 netGet[len++] = rx_delta;
139         }
140
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 */
145                 netGet[len++] = 0;
146                 netGet[len++] = nettx_key;
147                 netGet[len++] = tx_delta;
148         }
149
150         if (buffer)
151                 *buffer = netGet;
152
153         return len;
154 }
155
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,
161 };
162
163 int gator_events_net_init(void)
164 {
165         netrx_key = gator_events_get_key();
166         nettx_key = gator_events_get_key();
167
168         netrx_enabled = 0;
169         nettx_enabled = 0;
170
171         return gator_events_install(&gator_events_net_interface);
172 }