dccp: check sk for closed state in dccp_sendmsg()
[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 };
65
66 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
67
68 static int dm_hit_limit = 64;
69 static int dm_delay = 1;
70 static unsigned long dm_hw_check_delta = 2*HZ;
71 static LIST_HEAD(hw_stats_list);
72
73 static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
74 {
75         size_t al;
76         struct net_dm_alert_msg *msg;
77         struct nlattr *nla;
78         struct sk_buff *skb;
79         unsigned long flags;
80         void *msg_header;
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                 goto err;
90
91         msg_header = genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
92                                  0, NET_DM_CMD_ALERT);
93         if (!msg_header) {
94                 nlmsg_free(skb);
95                 skb = NULL;
96                 goto err;
97         }
98         nla = nla_reserve(skb, NLA_UNSPEC,
99                           sizeof(struct net_dm_alert_msg));
100         if (!nla) {
101                 nlmsg_free(skb);
102                 skb = NULL;
103                 goto err;
104         }
105         msg = nla_data(nla);
106         memset(msg, 0, al);
107         goto out;
108
109 err:
110         mod_timer(&data->send_timer, jiffies + HZ / 10);
111 out:
112         spin_lock_irqsave(&data->lock, flags);
113         swap(data->skb, skb);
114         spin_unlock_irqrestore(&data->lock, flags);
115
116         if (skb) {
117                 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
118                 struct genlmsghdr *gnlh = (struct genlmsghdr *)nlmsg_data(nlh);
119
120                 genlmsg_end(skb, genlmsg_data(gnlh));
121         }
122
123         return skb;
124 }
125
126 static void send_dm_alert(struct work_struct *work)
127 {
128         struct sk_buff *skb;
129         struct per_cpu_dm_data *data;
130
131         data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
132
133         skb = reset_per_cpu_data(data);
134
135         if (skb)
136                 genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
137 }
138
139 /*
140  * This is the timer function to delay the sending of an alert
141  * in the event that more drops will arrive during the
142  * hysteresis period.
143  */
144 static void sched_send_work(unsigned long _data)
145 {
146         struct per_cpu_dm_data *data = (struct per_cpu_dm_data *)_data;
147
148         schedule_work(&data->dm_alert_work);
149 }
150
151 static void trace_drop_common(struct sk_buff *skb, void *location)
152 {
153         struct net_dm_alert_msg *msg;
154         struct nlmsghdr *nlh;
155         struct nlattr *nla;
156         int i;
157         struct sk_buff *dskb;
158         struct per_cpu_dm_data *data;
159         unsigned long flags;
160
161         local_irq_save(flags);
162         data = &__get_cpu_var(dm_cpu_data);
163         spin_lock(&data->lock);
164         dskb = data->skb;
165
166         if (!dskb)
167                 goto out;
168
169         nlh = (struct nlmsghdr *)dskb->data;
170         nla = genlmsg_data(nlmsg_data(nlh));
171         msg = nla_data(nla);
172         for (i = 0; i < msg->entries; i++) {
173                 if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
174                         msg->points[i].count++;
175                         goto out;
176                 }
177         }
178         if (msg->entries == dm_hit_limit)
179                 goto out;
180         /*
181          * We need to create a new entry
182          */
183         __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
184         nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
185         memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
186         msg->points[msg->entries].count = 1;
187         msg->entries++;
188
189         if (!timer_pending(&data->send_timer)) {
190                 data->send_timer.expires = jiffies + dm_delay * HZ;
191                 add_timer(&data->send_timer);
192         }
193
194 out:
195         spin_unlock_irqrestore(&data->lock, flags);
196 }
197
198 static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
199 {
200         trace_drop_common(skb, location);
201 }
202
203 static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi)
204 {
205         struct dm_hw_stat_delta *new_stat;
206
207         /*
208          * Don't check napi structures with no associated device
209          */
210         if (!napi->dev)
211                 return;
212
213         rcu_read_lock();
214         list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
215                 /*
216                  * only add a note to our monitor buffer if:
217                  * 1) this is the dev we received on
218                  * 2) its after the last_rx delta
219                  * 3) our rx_dropped count has gone up
220                  */
221                 if ((new_stat->dev == napi->dev)  &&
222                     (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
223                     (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
224                         trace_drop_common(NULL, NULL);
225                         new_stat->last_drop_val = napi->dev->stats.rx_dropped;
226                         new_stat->last_rx = jiffies;
227                         break;
228                 }
229         }
230         rcu_read_unlock();
231 }
232
233 static int set_all_monitor_traces(int state)
234 {
235         int rc = 0;
236         struct dm_hw_stat_delta *new_stat = NULL;
237         struct dm_hw_stat_delta *temp;
238
239         mutex_lock(&trace_state_mutex);
240
241         if (state == trace_state) {
242                 rc = -EAGAIN;
243                 goto out_unlock;
244         }
245
246         switch (state) {
247         case TRACE_ON:
248                 rc |= register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
249                 rc |= register_trace_napi_poll(trace_napi_poll_hit, NULL);
250                 break;
251         case TRACE_OFF:
252                 rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
253                 rc |= unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
254
255                 tracepoint_synchronize_unregister();
256
257                 /*
258                  * Clean the device list
259                  */
260                 list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
261                         if (new_stat->dev == NULL) {
262                                 list_del_rcu(&new_stat->list);
263                                 kfree_rcu(new_stat, rcu);
264                         }
265                 }
266                 break;
267         default:
268                 rc = 1;
269                 break;
270         }
271
272         if (!rc)
273                 trace_state = state;
274         else
275                 rc = -EINPROGRESS;
276
277 out_unlock:
278         mutex_unlock(&trace_state_mutex);
279
280         return rc;
281 }
282
283
284 static int net_dm_cmd_config(struct sk_buff *skb,
285                         struct genl_info *info)
286 {
287         return -ENOTSUPP;
288 }
289
290 static int net_dm_cmd_trace(struct sk_buff *skb,
291                         struct genl_info *info)
292 {
293         switch (info->genlhdr->cmd) {
294         case NET_DM_CMD_START:
295                 return set_all_monitor_traces(TRACE_ON);
296                 break;
297         case NET_DM_CMD_STOP:
298                 return set_all_monitor_traces(TRACE_OFF);
299                 break;
300         }
301
302         return -ENOTSUPP;
303 }
304
305 static int dropmon_net_event(struct notifier_block *ev_block,
306                         unsigned long event, void *ptr)
307 {
308         struct net_device *dev = ptr;
309         struct dm_hw_stat_delta *new_stat = NULL;
310         struct dm_hw_stat_delta *tmp;
311
312         switch (event) {
313         case NETDEV_REGISTER:
314                 new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
315
316                 if (!new_stat)
317                         goto out;
318
319                 new_stat->dev = dev;
320                 new_stat->last_rx = jiffies;
321                 mutex_lock(&trace_state_mutex);
322                 list_add_rcu(&new_stat->list, &hw_stats_list);
323                 mutex_unlock(&trace_state_mutex);
324                 break;
325         case NETDEV_UNREGISTER:
326                 mutex_lock(&trace_state_mutex);
327                 list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
328                         if (new_stat->dev == dev) {
329                                 new_stat->dev = NULL;
330                                 if (trace_state == TRACE_OFF) {
331                                         list_del_rcu(&new_stat->list);
332                                         kfree_rcu(new_stat, rcu);
333                                         break;
334                                 }
335                         }
336                 }
337                 mutex_unlock(&trace_state_mutex);
338                 break;
339         }
340 out:
341         return NOTIFY_DONE;
342 }
343
344 static struct genl_ops dropmon_ops[] = {
345         {
346                 .cmd = NET_DM_CMD_CONFIG,
347                 .doit = net_dm_cmd_config,
348         },
349         {
350                 .cmd = NET_DM_CMD_START,
351                 .doit = net_dm_cmd_trace,
352         },
353         {
354                 .cmd = NET_DM_CMD_STOP,
355                 .doit = net_dm_cmd_trace,
356         },
357 };
358
359 static struct notifier_block dropmon_net_notifier = {
360         .notifier_call = dropmon_net_event
361 };
362
363 static int __init init_net_drop_monitor(void)
364 {
365         struct per_cpu_dm_data *data;
366         int cpu, rc;
367
368         printk(KERN_INFO "Initializing network drop monitor service\n");
369
370         if (sizeof(void *) > 8) {
371                 printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
372                 return -ENOSPC;
373         }
374
375         rc = genl_register_family_with_ops(&net_drop_monitor_family,
376                                            dropmon_ops,
377                                            ARRAY_SIZE(dropmon_ops));
378         if (rc) {
379                 printk(KERN_ERR "Could not create drop monitor netlink family\n");
380                 return rc;
381         }
382
383         rc = register_netdevice_notifier(&dropmon_net_notifier);
384         if (rc < 0) {
385                 printk(KERN_CRIT "Failed to register netdevice notifier\n");
386                 goto out_unreg;
387         }
388
389         rc = 0;
390
391         for_each_present_cpu(cpu) {
392                 data = &per_cpu(dm_cpu_data, cpu);
393                 INIT_WORK(&data->dm_alert_work, send_dm_alert);
394                 init_timer(&data->send_timer);
395                 data->send_timer.data = (unsigned long)data;
396                 data->send_timer.function = sched_send_work;
397                 spin_lock_init(&data->lock);
398                 reset_per_cpu_data(data);
399         }
400
401
402         goto out;
403
404 out_unreg:
405         genl_unregister_family(&net_drop_monitor_family);
406 out:
407         return rc;
408 }
409
410 late_initcall(init_net_drop_monitor);