misc: uidstat: Adding uid stat driver to collect network statistics.
[firefly-linux-kernel-4.4.55.git] / drivers / misc / uid_stat.c
1 /* drivers/misc/uid_stat.c
2  *
3  * Copyright (C) 2008 - 2009 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <asm/atomic.h>
17
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/proc_fs.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/stat.h>
26 #include <linux/uid_stat.h>
27
28 static DEFINE_SPINLOCK(uid_lock);
29 static LIST_HEAD(uid_list);
30 static struct proc_dir_entry *parent;
31
32 struct uid_stat {
33         struct list_head link;
34         uid_t uid;
35         atomic_t tcp_rcv;
36         atomic_t tcp_snd;
37 };
38
39 static struct uid_stat *find_uid_stat(uid_t uid) {
40         unsigned long flags;
41         struct uid_stat *entry;
42
43         spin_lock_irqsave(&uid_lock, flags);
44         list_for_each_entry(entry, &uid_list, link) {
45                 if (entry->uid == uid) {
46                         spin_unlock_irqrestore(&uid_lock, flags);
47                         return entry;
48                 }
49         }
50         spin_unlock_irqrestore(&uid_lock, flags);
51         return NULL;
52 }
53
54 static int tcp_snd_read_proc(char *page, char **start, off_t off,
55                                 int count, int *eof, void *data)
56 {
57         int len;
58         unsigned int bytes;
59         char *p = page;
60         struct uid_stat *uid_entry = (struct uid_stat *) data;
61         if (!data)
62                 return 0;
63
64         bytes = (unsigned int) (atomic_read(&uid_entry->tcp_snd) + INT_MIN);
65         p += sprintf(p, "%u\n", bytes);
66         len = (p - page) - off;
67         *eof = (len <= count) ? 1 : 0;
68         *start = page + off;
69         return len;
70 }
71
72 static int tcp_rcv_read_proc(char *page, char **start, off_t off,
73                                 int count, int *eof, void *data)
74 {
75         int len;
76         unsigned int bytes;
77         char *p = page;
78         struct uid_stat *uid_entry = (struct uid_stat *) data;
79         if (!data)
80                 return 0;
81
82         bytes = (unsigned int) (atomic_read(&uid_entry->tcp_rcv) + INT_MIN);
83         p += sprintf(p, "%u\n", bytes);
84         len = (p - page) - off;
85         *eof = (len <= count) ? 1 : 0;
86         *start = page + off;
87         return len;
88 }
89
90 /* Create a new entry for tracking the specified uid. */
91 static struct uid_stat *create_stat(uid_t uid) {
92         unsigned long flags;
93         char uid_s[32];
94         struct uid_stat *new_uid;
95         struct proc_dir_entry *entry;
96
97         /* Create the uid stat struct and append it to the list. */
98         if ((new_uid = kmalloc(sizeof(struct uid_stat), GFP_KERNEL)) == NULL)
99                 return NULL;
100
101         new_uid->uid = uid;
102         /* Counters start at INT_MIN, so we can track 4GB of network traffic. */
103         atomic_set(&new_uid->tcp_rcv, INT_MIN);
104         atomic_set(&new_uid->tcp_snd, INT_MIN);
105
106         spin_lock_irqsave(&uid_lock, flags);
107         list_add_tail(&new_uid->link, &uid_list);
108         spin_unlock_irqrestore(&uid_lock, flags);
109
110         sprintf(uid_s, "%d", uid);
111         entry = proc_mkdir(uid_s, parent);
112
113         /* Keep reference to uid_stat so we know what uid to read stats from. */
114         create_proc_read_entry("tcp_snd", S_IRUGO, entry , tcp_snd_read_proc,
115                 (void *) new_uid);
116
117         create_proc_read_entry("tcp_rcv", S_IRUGO, entry, tcp_rcv_read_proc,
118                 (void *) new_uid);
119
120         return new_uid;
121 }
122
123 int uid_stat_tcp_snd(uid_t uid, int size) {
124         struct uid_stat *entry;
125         if ((entry = find_uid_stat(uid)) == NULL &&
126                 ((entry = create_stat(uid)) == NULL)) {
127                         return -1;
128         }
129         atomic_add(size, &entry->tcp_snd);
130         return 0;
131 }
132
133 int uid_stat_tcp_rcv(uid_t uid, int size) {
134         struct uid_stat *entry;
135         if ((entry = find_uid_stat(uid)) == NULL &&
136                 ((entry = create_stat(uid)) == NULL)) {
137                         return -1;
138         }
139         atomic_add(size, &entry->tcp_rcv);
140         return 0;
141 }
142
143 static int __init uid_stat_init(void)
144 {
145         parent = proc_mkdir("uid_stat", NULL);
146         if (!parent) {
147                 pr_err("uid_stat: failed to create proc entry\n");
148                 return -1;
149         }
150         return 0;
151 }
152
153 __initcall(uid_stat_init);