Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / net / core / drop_monitor.c
1 /*
2  * Monitoring code for network dropped packet alerts
3  *
4  * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
5  */
6
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/string.h>
10 #include <linux/if_arp.h>
11 #include <linux/inetdevice.h>
12 #include <linux/inet.h>
13 #include <linux/interrupt.h>
14 #include <linux/netpoll.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <linux/types.h>
18 #include <linux/workqueue.h>
19 #include <linux/netlink.h>
20 #include <linux/net_dropmon.h>
21 #include <linux/percpu.h>
22 #include <linux/timer.h>
23 #include <linux/bitops.h>
24 #include <linux/slab.h>
25 #include <net/genetlink.h>
26 #include <net/netevent.h>
27
28 #include <trace/events/skb.h>
29 #include <trace/events/napi.h>
30
31 #include <asm/unaligned.h>
32
33 #define TRACE_ON 1
34 #define TRACE_OFF 0
35
36 /*
37  * Globals, our netlink socket pointer
38  * and the work handle that will send up
39  * netlink alerts
40  */
41 static int trace_state = TRACE_OFF;
42 static DEFINE_MUTEX(trace_state_mutex);
43
44 struct per_cpu_dm_data {
45         spinlock_t              lock;
46         struct sk_buff          *skb;
47         struct work_struct      dm_alert_work;
48         struct timer_list       send_timer;
49 };
50
51 struct dm_hw_stat_delta {
52         struct net_device *dev;
53         unsigned long last_rx;
54         struct list_head list;
55         struct rcu_head rcu;
56         unsigned long last_drop_val;
57 };
58
59 static struct genl_family net_drop_monitor_family = {
60         .id             = GENL_ID_GENERATE,
61         .hdrsize        = 0,
62         .name           = "NET_DM",
63         .version        = 2,
64         .maxattr        = NET_DM_CMD_MAX,
65 };
66
67 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
68
69 static int dm_hit_limit = 64;
70 static int dm_delay = 1;
71 static unsigned long dm_hw_check_delta = 2*HZ;
72 static LIST_HEAD(hw_stats_list);
73
74 static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
75 {
76         size_t al;
77         struct net_dm_alert_msg *msg;
78         struct nlattr *nla;
79         struct sk_buff *skb;
80         unsigned long flags;
81
82         al = sizeof(struct net_dm_alert_msg);
83         al += dm_hit_limit * sizeof(struct net_dm_drop_point);
84         al += sizeof(struct nlattr);
85
86         skb = genlmsg_new(al, GFP_KERNEL);
87
88         if (skb) {
89                 genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
90                                 0, NET_DM_CMD_ALERT);
91                 nla = nla_reserve(skb, NLA_UNSPEC,
92                                   sizeof(struct net_dm_alert_msg));
93                 msg = nla_data(nla);
94                 memset(msg, 0, al);
95         } else {
96                 mod_timer(&data->send_timer, jiffies + HZ / 10);
97         }
98
99         spin_lock_irqsave(&data->lock, flags);
100         swap(data->skb, skb);
101         spin_unlock_irqrestore(&data->lock, flags);
102
103         return skb;
104 }
105
106 static void send_dm_alert(struct work_struct *work)
107 {
108         struct sk_buff *skb;
109         struct per_cpu_dm_data *data;
110
111         data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
112
113         skb = reset_per_cpu_data(data);
114
115         if (skb)
116                 genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
117 }
118
119 /*
120  * This is the timer function to delay the sending of an alert
121  * in the event that more drops will arrive during the
122  * hysteresis period.
123  */
124 static void sched_send_work(unsigned long _data)
125 {
126         struct per_cpu_dm_data *data = (struct per_cpu_dm_data *)_data;
127
128         schedule_work(&data->dm_alert_work);
129 }
130
131 static void trace_drop_common(struct sk_buff *skb, void *location)
132 {
133         struct net_dm_alert_msg *msg;
134         struct nlmsghdr *nlh;
135         struct nlattr *nla;
136         int i;
137         struct sk_buff *dskb;
138         struct per_cpu_dm_data *data;
139         unsigned long flags;
140
141         local_irq_save(flags);
142         data = &__get_cpu_var(dm_cpu_data);
143         spin_lock(&data->lock);
144         dskb = data->skb;
145
146         if (!dskb)
147                 goto out;
148
149         nlh = (struct nlmsghdr *)dskb->data;
150         nla = genlmsg_data(nlmsg_data(nlh));
151         msg = nla_data(nla);
152         for (i = 0; i < msg->entries; i++) {
153                 if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
154                         msg->points[i].count++;
155                         goto out;
156                 }
157         }
158         if (msg->entries == dm_hit_limit)
159                 goto out;
160         /*
161          * We need to create a new entry
162          */
163         __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
164         nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
165         memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
166         msg->points[msg->entries].count = 1;
167         msg->entries++;
168
169         if (!timer_pending(&data->send_timer)) {
170                 data->send_timer.expires = jiffies + dm_delay * HZ;
171                 add_timer(&data->send_timer);
172         }
173
174 out:
175         spin_unlock_irqrestore(&data->lock, flags);
176 }
177
178 static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
179 {
180         trace_drop_common(skb, location);
181 }
182
183 static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi)
184 {
185         struct dm_hw_stat_delta *new_stat;
186
187         /*
188          * Don't check napi structures with no associated device
189          */
190         if (!napi->dev)
191                 return;
192
193         rcu_read_lock();
194         list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
195                 /*
196                  * only add a note to our monitor buffer if:
197                  * 1) this is the dev we received on
198                  * 2) its after the last_rx delta
199                  * 3) our rx_dropped count has gone up
200                  */
201                 if ((new_stat->dev == napi->dev)  &&
202                     (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
203                     (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
204                         trace_drop_common(NULL, NULL);
205                         new_stat->last_drop_val = napi->dev->stats.rx_dropped;
206                         new_stat->last_rx = jiffies;
207                         break;
208                 }
209         }
210         rcu_read_unlock();
211 }
212
213 static int set_all_monitor_traces(int state)
214 {
215         int rc = 0;
216         struct dm_hw_stat_delta *new_stat = NULL;
217         struct dm_hw_stat_delta *temp;
218
219         mutex_lock(&trace_state_mutex);
220
221         if (state == trace_state) {
222                 rc = -EAGAIN;
223                 goto out_unlock;
224         }
225
226         switch (state) {
227         case TRACE_ON:
228                 rc |= register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
229                 rc |= register_trace_napi_poll(trace_napi_poll_hit, NULL);
230                 break;
231         case TRACE_OFF:
232                 rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
233                 rc |= unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
234
235                 tracepoint_synchronize_unregister();
236
237                 /*
238                  * Clean the device list
239                  */
240                 list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
241                         if (new_stat->dev == NULL) {
242                                 list_del_rcu(&new_stat->list);
243                                 kfree_rcu(new_stat, rcu);
244                         }
245                 }
246                 break;
247         default:
248                 rc = 1;
249                 break;
250         }
251
252         if (!rc)
253                 trace_state = state;
254         else
255                 rc = -EINPROGRESS;
256
257 out_unlock:
258         mutex_unlock(&trace_state_mutex);
259
260         return rc;
261 }
262
263
264 static int net_dm_cmd_config(struct sk_buff *skb,
265                         struct genl_info *info)
266 {
267         return -ENOTSUPP;
268 }
269
270 static int net_dm_cmd_trace(struct sk_buff *skb,
271                         struct genl_info *info)
272 {
273         switch (info->genlhdr->cmd) {
274         case NET_DM_CMD_START:
275                 return set_all_monitor_traces(TRACE_ON);
276                 break;
277         case NET_DM_CMD_STOP:
278                 return set_all_monitor_traces(TRACE_OFF);
279                 break;
280         }
281
282         return -ENOTSUPP;
283 }
284
285 static int dropmon_net_event(struct notifier_block *ev_block,
286                         unsigned long event, void *ptr)
287 {
288         struct net_device *dev = ptr;
289         struct dm_hw_stat_delta *new_stat = NULL;
290         struct dm_hw_stat_delta *tmp;
291
292         switch (event) {
293         case NETDEV_REGISTER:
294                 new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
295
296                 if (!new_stat)
297                         goto out;
298
299                 new_stat->dev = dev;
300                 new_stat->last_rx = jiffies;
301                 mutex_lock(&trace_state_mutex);
302                 list_add_rcu(&new_stat->list, &hw_stats_list);
303                 mutex_unlock(&trace_state_mutex);
304                 break;
305         case NETDEV_UNREGISTER:
306                 mutex_lock(&trace_state_mutex);
307                 list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
308                         if (new_stat->dev == dev) {
309                                 new_stat->dev = NULL;
310                                 if (trace_state == TRACE_OFF) {
311                                         list_del_rcu(&new_stat->list);
312                                         kfree_rcu(new_stat, rcu);
313                                         break;
314                                 }
315                         }
316                 }
317                 mutex_unlock(&trace_state_mutex);
318                 break;
319         }
320 out:
321         return NOTIFY_DONE;
322 }
323
324 static struct genl_ops dropmon_ops[] = {
325         {
326                 .cmd = NET_DM_CMD_CONFIG,
327                 .doit = net_dm_cmd_config,
328         },
329         {
330                 .cmd = NET_DM_CMD_START,
331                 .doit = net_dm_cmd_trace,
332         },
333         {
334                 .cmd = NET_DM_CMD_STOP,
335                 .doit = net_dm_cmd_trace,
336         },
337 };
338
339 static struct notifier_block dropmon_net_notifier = {
340         .notifier_call = dropmon_net_event
341 };
342
343 static int __init init_net_drop_monitor(void)
344 {
345         struct per_cpu_dm_data *data;
346         int cpu, rc;
347
348         printk(KERN_INFO "Initializing network drop monitor service\n");
349
350         if (sizeof(void *) > 8) {
351                 printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
352                 return -ENOSPC;
353         }
354
355         rc = genl_register_family_with_ops(&net_drop_monitor_family,
356                                            dropmon_ops,
357                                            ARRAY_SIZE(dropmon_ops));
358         if (rc) {
359                 printk(KERN_ERR "Could not create drop monitor netlink family\n");
360                 return rc;
361         }
362
363         rc = register_netdevice_notifier(&dropmon_net_notifier);
364         if (rc < 0) {
365                 printk(KERN_CRIT "Failed to register netdevice notifier\n");
366                 goto out_unreg;
367         }
368
369         rc = 0;
370
371         for_each_present_cpu(cpu) {
372                 data = &per_cpu(dm_cpu_data, cpu);
373                 INIT_WORK(&data->dm_alert_work, send_dm_alert);
374                 init_timer(&data->send_timer);
375                 data->send_timer.data = (unsigned long)data;
376                 data->send_timer.function = sched_send_work;
377                 spin_lock_init(&data->lock);
378                 reset_per_cpu_data(data);
379         }
380
381
382         goto out;
383
384 out_unreg:
385         genl_unregister_family(&net_drop_monitor_family);
386 out:
387         return rc;
388 }
389
390 late_initcall(init_net_drop_monitor);