blob: 1e36731479d20417f7504d71af0d1f27f30278a3 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
Jon Medhurstb1d07442015-05-08 12:04:18 +01002 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
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
18static ulong netrx_enabled;
19static ulong nettx_enabled;
20static ulong netrx_key;
21static ulong nettx_key;
22static int rx_total, tx_total;
23static ulong netPrev[TOTALNET];
24static int netGet[TOTALNET * 4];
25
26static struct timer_list net_wake_up_timer;
27
Jon Medhurst96b56152014-10-30 18:01:15 +000028/* Must be run in process context as the kernel function dev_get_stats() can sleep */
Jon Medhurstaaf37a32013-06-11 12:10:56 +010029static 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
48DECLARE_WORK(wq_get_stats, get_network_stats);
49
50static void net_wake_up_handler(unsigned long unused_data)
51{
Jon Medhurst96b56152014-10-30 18:01:15 +000052 /* had to delay scheduling work as attempting to schedule work during the context switch is illegal in kernel versions 3.5 and greater */
Jon Medhurstaaf37a32013-06-11 12:10:56 +010053 schedule_work(&wq_get_stats);
54}
55
56static 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
74static int gator_events_net_create_files(struct super_block *sb, struct dentry *root)
75{
Jon Medhurst96b56152014-10-30 18:01:15 +000076 /* Network counters are not currently supported in RT-Preempt full because mod_timer is used */
Jon Medhurst34d97692013-12-19 09:23:06 +000077#ifndef CONFIG_PREEMPT_RT_FULL
Jon Medhurstaaf37a32013-06-11 12:10:56 +010078 struct dentry *dir;
79
80 dir = gatorfs_mkdir(sb, root, "Linux_net_rx");
Jon Medhurst96b56152014-10-30 18:01:15 +000081 if (!dir)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010082 return -1;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010083 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");
Jon Medhurst96b56152014-10-30 18:01:15 +000087 if (!dir)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010088 return -1;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010089 gatorfs_create_ulong(sb, dir, "enabled", &nettx_enabled);
90 gatorfs_create_ro_ulong(sb, dir, "key", &nettx_key);
Jon Medhurst34d97692013-12-19 09:23:06 +000091#endif
Jon Medhurstaaf37a32013-06-11 12:10:56 +010092
93 return 0;
94}
95
96static 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
109static 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
Jon Medhurst96b56152014-10-30 18:01:15 +0000116static int gator_events_net_read(int **buffer, bool sched_switch)
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100117{
118 int len, rx_delta, tx_delta;
Jon Medhurst96b56152014-10-30 18:01:15 +0000119 static int last_rx_delta, last_tx_delta;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100120
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;
Jon Medhurst96b56152014-10-30 18:01:15 +0000135 /* indicates to Streamline that rx_delta bytes were transmitted now, not since the last message */
136 netGet[len++] = 0;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100137 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;
Jon Medhurst96b56152014-10-30 18:01:15 +0000144 /* indicates to Streamline that tx_delta bytes were transmitted now, not since the last message */
145 netGet[len++] = 0;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100146 netGet[len++] = nettx_key;
147 netGet[len++] = tx_delta;
148 }
149
150 if (buffer)
151 *buffer = netGet;
152
153 return len;
154}
155
156static 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
163int 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}