]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/arm-ds5-gator.git/blob - gator_events_net.c
gator: ARM DS-5.5 Streamline gator driver
[android-sdk/arm-ds5-gator.git] / gator_events_net.c
1 /**
2  * Copyright (C) ARM Limited 2010-2011. 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  */
10 #include "gator.h"
11 #include <linux/netdevice.h>
13 #define NETRX           0
14 #define NETTX           1
15 #define NETDRV          2
16 #define TOTALNET        (NETDRV+1)
18 static ulong netdrv_enabled;
19 static ulong netrx_enabled;
20 static ulong nettx_enabled;
21 static ulong netdrv_key;
22 static ulong netrx_key;
23 static ulong nettx_key;
24 static int rx_total, tx_total;
25 static ulong netPrev[TOTALNET];
26 static int netGet[TOTALNET * 2];
28 static void get_network_stats(struct work_struct *wsptr) {
29         int rx = 0, tx = 0;
30         struct net_device *dev;
32         for_each_netdev(&init_net, dev) {
33 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
34                 const struct net_device_stats *stats = dev_get_stats(dev);
35 #else
36                 struct rtnl_link_stats64 temp;
37                 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
38 #endif
39                 rx += stats->rx_bytes;
40                 tx += stats->tx_bytes;
41         }
42         rx_total = rx;
43         tx_total = tx;
44 }
45 DECLARE_WORK(wq_get_stats, get_network_stats);
47 static void calculate_delta(int *drv, int *rx, int *tx)
48 {
49         int drv_calc, rx_calc, tx_calc;
51         drv_calc = gator_net_traffic - netPrev[NETDRV];
52         if (drv_calc > 0) {
53                 netPrev[NETDRV] += drv_calc;
54                 netPrev[NETTX] += drv_calc;
55                 // remove tcp/ip header overhead
56                 //  approximation based on empirical measurement
57                 netPrev[NETRX] += drv_calc / 42;
58                 netPrev[NETTX] += drv_calc / 18;
59         }
61         rx_calc = (int)(rx_total - netPrev[NETRX]);
62         if (rx_calc < 0)
63                 rx_calc = 0;
64         netPrev[NETRX] += rx_calc;
66         tx_calc = (int)(tx_total - netPrev[NETTX]);
67         if (tx_calc < 0)
68                 tx_calc = 0;
69         netPrev[NETTX] += tx_calc;
71         *drv = drv_calc;
72         *rx = rx_calc;
73         *tx = tx_calc;
74 }
76 static int gator_events_net_create_files(struct super_block *sb, struct dentry *root)
77 {
78         struct dentry *dir;
80         dir = gatorfs_mkdir(sb, root, "Linux_net_drv");
81         if (!dir) {
82                 return -1;
83         }
84         gatorfs_create_ulong(sb, dir, "enabled", &netdrv_enabled);
85         gatorfs_create_ro_ulong(sb, dir, "key", &netdrv_key);
87         dir = gatorfs_mkdir(sb, root, "Linux_net_rx");
88         if (!dir) {
89                 return -1;
90         }
91         gatorfs_create_ulong(sb, dir, "enabled", &netrx_enabled);
92         gatorfs_create_ro_ulong(sb, dir, "key", &netrx_key);
94         dir = gatorfs_mkdir(sb, root, "Linux_net_tx");
95         if (!dir) {
96                 return -1;
97         }
98         gatorfs_create_ulong(sb, dir, "enabled", &nettx_enabled);
99         gatorfs_create_ro_ulong(sb, dir, "key", &nettx_key);
101         return 0;
104 static int gator_events_net_init(int *key)
106         netdrv_key = *key;
107         *key = *key + 1;
108         netrx_key = *key;
109         *key = *key + 1;
110         nettx_key = *key;
111         *key = *key + 1;
113         netdrv_enabled = 0;
114         netrx_enabled = 0;
115         nettx_enabled = 0;
117         return 0;
120 static int gator_events_net_start(void)
122         get_network_stats(NULL);
123         netPrev[NETDRV] = 0;
124         netPrev[NETRX] = rx_total;
125         netPrev[NETTX] = tx_total;
126         return 0;
129 static void gator_events_net_stop(void)
131         netdrv_enabled = 0;
132         netrx_enabled = 0;
133         nettx_enabled = 0;
136 static int gator_events_net_read(int **buffer)
138         int len, drv_delta, rx_delta, tx_delta;
139         static int last_drv_delta = 0, last_rx_delta = 0, last_tx_delta = 0;
141         if (raw_smp_processor_id() != 0)
142                 return 0;
144         schedule_work(&wq_get_stats);
145         calculate_delta(&drv_delta, &rx_delta, &tx_delta);
147         len = 0;
148         if (netdrv_enabled && last_drv_delta != drv_delta) {
149                 last_drv_delta = drv_delta;
150                 netGet[len++] = netdrv_key;
151                 netGet[len++] = drv_delta;
152         }
154         if (netrx_enabled && last_rx_delta != rx_delta) {
155                 last_rx_delta = rx_delta;
156                 netGet[len++] = netrx_key;
157                 netGet[len++] = rx_delta;
158         }
160         if (nettx_enabled && last_tx_delta != tx_delta) {
161                 last_tx_delta = tx_delta;
162                 netGet[len++] = nettx_key;
163                 netGet[len++] = tx_delta;
164         }
166         if (buffer)
167                 *buffer = netGet;
169         return len;
172 int gator_events_net_install(gator_interface *gi) {
173         gi->create_files = gator_events_net_create_files;
174         gi->init = gator_events_net_init;
175         gi->start = gator_events_net_start;
176         gi->stop = gator_events_net_stop;
177         gi->read = gator_events_net_read;
178         gator_net_traffic++;
179         return 0;