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