summaryrefslogtreecommitdiffstats
blob: a92158648751617113a4dd20cd010b452ec0470f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * Copyright (C) ARM Limited 2010-2011. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include "gator.h"
#include <linux/netdevice.h>

#define NETRX		0
#define NETTX		1
#define NETDRV		2
#define TOTALNET	(NETDRV+1)

static ulong netdrv_enabled;
static ulong netrx_enabled;
static ulong nettx_enabled;
static ulong netdrv_key;
static ulong netrx_key;
static ulong nettx_key;
static int rx_total, tx_total;
static ulong netPrev[TOTALNET];
static int netGet[TOTALNET * 2];

static void get_network_stats(struct work_struct *wsptr) {
	int rx = 0, tx = 0;
	struct net_device *dev;

	for_each_netdev(&init_net, dev) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
		const struct net_device_stats *stats = dev_get_stats(dev);
#else
		struct rtnl_link_stats64 temp;
		const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
#endif
		rx += stats->rx_bytes;
		tx += stats->tx_bytes;
	}
	rx_total = rx;
	tx_total = tx;
}
DECLARE_WORK(wq_get_stats, get_network_stats);

static void calculate_delta(int *drv, int *rx, int *tx)
{
	int drv_calc, rx_calc, tx_calc;

	drv_calc = gator_net_traffic - netPrev[NETDRV];
	if (drv_calc > 0) {
		netPrev[NETDRV] += drv_calc;
		netPrev[NETTX] += drv_calc;
		// remove tcp/ip header overhead
		//  approximation based on empirical measurement
		netPrev[NETRX] += drv_calc / 42;
		netPrev[NETTX] += drv_calc / 18;
	}

	rx_calc = (int)(rx_total - netPrev[NETRX]);
	if (rx_calc < 0)
		rx_calc = 0;
	netPrev[NETRX] += rx_calc;

	tx_calc = (int)(tx_total - netPrev[NETTX]);
	if (tx_calc < 0)
		tx_calc = 0;
	netPrev[NETTX] += tx_calc;

	*drv = drv_calc;
	*rx = rx_calc;
	*tx = tx_calc;
}

static int gator_events_net_create_files(struct super_block *sb, struct dentry *root)
{
	struct dentry *dir;

	dir = gatorfs_mkdir(sb, root, "Linux_net_drv");
	if (!dir) {
		return -1;
	}
	gatorfs_create_ulong(sb, dir, "enabled", &netdrv_enabled);
	gatorfs_create_ro_ulong(sb, dir, "key", &netdrv_key);

	dir = gatorfs_mkdir(sb, root, "Linux_net_rx");
	if (!dir) {
		return -1;
	}
	gatorfs_create_ulong(sb, dir, "enabled", &netrx_enabled);
	gatorfs_create_ro_ulong(sb, dir, "key", &netrx_key);

	dir = gatorfs_mkdir(sb, root, "Linux_net_tx");
	if (!dir) {
		return -1;
	}
	gatorfs_create_ulong(sb, dir, "enabled", &nettx_enabled);
	gatorfs_create_ro_ulong(sb, dir, "key", &nettx_key);

	return 0;
}

static int gator_events_net_start(void)
{
	get_network_stats(NULL);
	netPrev[NETDRV] = 0;
	netPrev[NETRX] = rx_total;
	netPrev[NETTX] = tx_total;
	return 0;
}

static void gator_events_net_stop(void)
{
	netdrv_enabled = 0;
	netrx_enabled = 0;
	nettx_enabled = 0;
}

static int gator_events_net_read(int **buffer)
{
	int len, drv_delta, rx_delta, tx_delta;
	static int last_drv_delta = 0, last_rx_delta = 0, last_tx_delta = 0;

	if (smp_processor_id() != 0)
		return 0;

	schedule_work(&wq_get_stats);
	calculate_delta(&drv_delta, &rx_delta, &tx_delta);

	len = 0;
	if (netdrv_enabled && last_drv_delta != drv_delta) {
		last_drv_delta = drv_delta;
		netGet[len++] = netdrv_key;
		netGet[len++] = drv_delta;
	}

	if (netrx_enabled && last_rx_delta != rx_delta) {
		last_rx_delta = rx_delta;
		netGet[len++] = netrx_key;
		netGet[len++] = rx_delta;
	}

	if (nettx_enabled && last_tx_delta != tx_delta) {
		last_tx_delta = tx_delta;
		netGet[len++] = nettx_key;
		netGet[len++] = tx_delta;
	}

	if (buffer)
		*buffer = netGet;

	return len;
}

static struct gator_interface gator_events_net_interface = {
	.create_files = gator_events_net_create_files,
	.start = gator_events_net_start,
	.stop = gator_events_net_stop,
	.read = gator_events_net_read,
};

int gator_events_net_init(void)
{
	gator_net_traffic++;

	netdrv_key = gator_events_get_key();
	netrx_key = gator_events_get_key();
	nettx_key = gator_events_get_key();

	netdrv_enabled = 0;
	netrx_enabled = 0;
	nettx_enabled = 0;

	return gator_events_install(&gator_events_net_interface);
}
gator_events_init(gator_events_net_init);