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