]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/core/drop_monitor.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[mirror_ubuntu-zesty-kernel.git] / net / core / drop_monitor.c
CommitLineData
9a8afc8d
NH
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 <net/genetlink.h>
4ea7e386 25#include <net/netevent.h>
9a8afc8d 26
ad8d75ff 27#include <trace/events/skb.h>
9cbc1cb8 28#include <trace/events/napi.h>
9a8afc8d
NH
29
30#include <asm/unaligned.h>
31
32#define TRACE_ON 1
33#define TRACE_OFF 0
34
35static void send_dm_alert(struct work_struct *unused);
36
37
38/*
39 * Globals, our netlink socket pointer
40 * and the work handle that will send up
41 * netlink alerts
42 */
4ea7e386
NH
43static int trace_state = TRACE_OFF;
44static spinlock_t trace_state_lock = SPIN_LOCK_UNLOCKED;
9a8afc8d
NH
45
46struct per_cpu_dm_data {
47 struct work_struct dm_alert_work;
48 struct sk_buff *skb;
49 atomic_t dm_hit_count;
50 struct timer_list send_timer;
51};
52
4ea7e386
NH
53struct dm_hw_stat_delta {
54 struct net_device *dev;
55 struct list_head list;
56 struct rcu_head rcu;
57 unsigned long last_drop_val;
58};
59
9a8afc8d
NH
60static struct genl_family net_drop_monitor_family = {
61 .id = GENL_ID_GENERATE,
62 .hdrsize = 0,
63 .name = "NET_DM",
683703a2 64 .version = 2,
9a8afc8d
NH
65 .maxattr = NET_DM_CMD_MAX,
66};
67
68static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
69
70static int dm_hit_limit = 64;
71static int dm_delay = 1;
4ea7e386
NH
72static unsigned long dm_hw_check_delta = 2*HZ;
73static LIST_HEAD(hw_stats_list);
9a8afc8d
NH
74
75static void reset_per_cpu_data(struct per_cpu_dm_data *data)
76{
77 size_t al;
78 struct net_dm_alert_msg *msg;
683703a2 79 struct nlattr *nla;
9a8afc8d
NH
80
81 al = sizeof(struct net_dm_alert_msg);
82 al += dm_hit_limit * sizeof(struct net_dm_drop_point);
683703a2
NH
83 al += sizeof(struct nlattr);
84
9a8afc8d
NH
85 data->skb = genlmsg_new(al, GFP_KERNEL);
86 genlmsg_put(data->skb, 0, 0, &net_drop_monitor_family,
87 0, NET_DM_CMD_ALERT);
683703a2
NH
88 nla = nla_reserve(data->skb, NLA_UNSPEC, sizeof(struct net_dm_alert_msg));
89 msg = nla_data(nla);
9a8afc8d
NH
90 memset(msg, 0, al);
91 atomic_set(&data->dm_hit_count, dm_hit_limit);
92}
93
94static void send_dm_alert(struct work_struct *unused)
95{
96 struct sk_buff *skb;
97 struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
98
99 /*
100 * Grab the skb we're about to send
101 */
102 skb = data->skb;
103
104 /*
105 * Replace it with a new one
106 */
107 reset_per_cpu_data(data);
108
109 /*
110 * Ship it!
111 */
112 genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
113
114}
115
116/*
117 * This is the timer function to delay the sending of an alert
118 * in the event that more drops will arrive during the
119 * hysteresis period. Note that it operates under the timer interrupt
120 * so we don't need to disable preemption here
121 */
122static void sched_send_work(unsigned long unused)
123{
124 struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
125
126 schedule_work(&data->dm_alert_work);
127}
128
4ea7e386 129static void trace_drop_common(struct sk_buff *skb, void *location)
9a8afc8d
NH
130{
131 struct net_dm_alert_msg *msg;
132 struct nlmsghdr *nlh;
683703a2 133 struct nlattr *nla;
9a8afc8d
NH
134 int i;
135 struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
136
137
138 if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
139 /*
140 * we're already at zero, discard this hit
141 */
142 goto out;
143 }
144
145 nlh = (struct nlmsghdr *)data->skb->data;
683703a2
NH
146 nla = genlmsg_data(nlmsg_data(nlh));
147 msg = nla_data(nla);
9a8afc8d
NH
148 for (i = 0; i < msg->entries; i++) {
149 if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
150 msg->points[i].count++;
151 goto out;
152 }
153 }
154
155 /*
156 * We need to create a new entry
157 */
158 __nla_reserve_nohdr(data->skb, sizeof(struct net_dm_drop_point));
683703a2 159 nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
9a8afc8d
NH
160 memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
161 msg->points[msg->entries].count = 1;
162 msg->entries++;
163
164 if (!timer_pending(&data->send_timer)) {
165 data->send_timer.expires = jiffies + dm_delay * HZ;
166 add_timer_on(&data->send_timer, smp_processor_id());
167 }
168
169out:
170 return;
171}
172
4ea7e386
NH
173static void trace_kfree_skb_hit(struct sk_buff *skb, void *location)
174{
175 trace_drop_common(skb, location);
176}
177
178static void trace_napi_poll_hit(struct napi_struct *napi)
179{
180 struct dm_hw_stat_delta *new_stat;
181
182 /*
183 * Ratelimit our check time to dm_hw_check_delta jiffies
184 */
f2798eb4
XG
185 if (!napi->dev ||
186 !time_after(jiffies, napi->dev->last_rx + dm_hw_check_delta))
4ea7e386
NH
187 return;
188
189 rcu_read_lock();
190 list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
191 if ((new_stat->dev == napi->dev) &&
192 (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
193 trace_drop_common(NULL, NULL);
194 new_stat->last_drop_val = napi->dev->stats.rx_dropped;
195 break;
196 }
197 }
198 rcu_read_unlock();
199}
200
201
202static void free_dm_hw_stat(struct rcu_head *head)
203{
204 struct dm_hw_stat_delta *n;
205 n = container_of(head, struct dm_hw_stat_delta, rcu);
206 kfree(n);
207}
208
9a8afc8d
NH
209static int set_all_monitor_traces(int state)
210{
211 int rc = 0;
4ea7e386
NH
212 struct dm_hw_stat_delta *new_stat = NULL;
213 struct dm_hw_stat_delta *temp;
214
215 spin_lock(&trace_state_lock);
9a8afc8d
NH
216
217 switch (state) {
218 case TRACE_ON:
219 rc |= register_trace_kfree_skb(trace_kfree_skb_hit);
4ea7e386 220 rc |= register_trace_napi_poll(trace_napi_poll_hit);
9a8afc8d
NH
221 break;
222 case TRACE_OFF:
223 rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit);
4ea7e386 224 rc |= unregister_trace_napi_poll(trace_napi_poll_hit);
9a8afc8d
NH
225
226 tracepoint_synchronize_unregister();
4ea7e386
NH
227
228 /*
229 * Clean the device list
230 */
231 list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
232 if (new_stat->dev == NULL) {
233 list_del_rcu(&new_stat->list);
234 call_rcu(&new_stat->rcu, free_dm_hw_stat);
235 }
236 }
9a8afc8d
NH
237 break;
238 default:
239 rc = 1;
240 break;
241 }
242
4ea7e386
NH
243 if (!rc)
244 trace_state = state;
245
246 spin_unlock(&trace_state_lock);
247
9a8afc8d
NH
248 if (rc)
249 return -EINPROGRESS;
250 return rc;
251}
252
253
254static int net_dm_cmd_config(struct sk_buff *skb,
255 struct genl_info *info)
256{
257 return -ENOTSUPP;
258}
259
260static int net_dm_cmd_trace(struct sk_buff *skb,
261 struct genl_info *info)
262{
263 switch (info->genlhdr->cmd) {
264 case NET_DM_CMD_START:
265 return set_all_monitor_traces(TRACE_ON);
266 break;
267 case NET_DM_CMD_STOP:
268 return set_all_monitor_traces(TRACE_OFF);
269 break;
270 }
271
272 return -ENOTSUPP;
273}
274
4ea7e386
NH
275static int dropmon_net_event(struct notifier_block *ev_block,
276 unsigned long event, void *ptr)
277{
278 struct net_device *dev = ptr;
279 struct dm_hw_stat_delta *new_stat = NULL;
280 struct dm_hw_stat_delta *tmp;
281
282 switch (event) {
283 case NETDEV_REGISTER:
284 new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
285
286 if (!new_stat)
287 goto out;
288
289 new_stat->dev = dev;
290 INIT_RCU_HEAD(&new_stat->rcu);
291 spin_lock(&trace_state_lock);
292 list_add_rcu(&new_stat->list, &hw_stats_list);
293 spin_unlock(&trace_state_lock);
294 break;
295 case NETDEV_UNREGISTER:
296 spin_lock(&trace_state_lock);
297 list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
298 if (new_stat->dev == dev) {
299 new_stat->dev = NULL;
300 if (trace_state == TRACE_OFF) {
301 list_del_rcu(&new_stat->list);
302 call_rcu(&new_stat->rcu, free_dm_hw_stat);
303 break;
304 }
305 }
306 }
307 spin_unlock(&trace_state_lock);
308 break;
309 }
310out:
311 return NOTIFY_DONE;
312}
9a8afc8d
NH
313
314static struct genl_ops dropmon_ops[] = {
315 {
316 .cmd = NET_DM_CMD_CONFIG,
317 .doit = net_dm_cmd_config,
318 },
319 {
320 .cmd = NET_DM_CMD_START,
321 .doit = net_dm_cmd_trace,
322 },
323 {
324 .cmd = NET_DM_CMD_STOP,
325 .doit = net_dm_cmd_trace,
326 },
327};
328
4ea7e386
NH
329static struct notifier_block dropmon_net_notifier = {
330 .notifier_call = dropmon_net_event
331};
332
9a8afc8d
NH
333static int __init init_net_drop_monitor(void)
334{
335 int cpu;
336 int rc, i, ret;
337 struct per_cpu_dm_data *data;
338 printk(KERN_INFO "Initalizing network drop monitor service\n");
339
340 if (sizeof(void *) > 8) {
341 printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
342 return -ENOSPC;
343 }
344
345 if (genl_register_family(&net_drop_monitor_family) < 0) {
346 printk(KERN_ERR "Could not create drop monitor netlink family\n");
347 return -EFAULT;
348 }
349
350 rc = -EFAULT;
351
352 for (i = 0; i < ARRAY_SIZE(dropmon_ops); i++) {
353 ret = genl_register_ops(&net_drop_monitor_family,
354 &dropmon_ops[i]);
355 if (ret) {
4ea7e386 356 printk(KERN_CRIT "Failed to register operation %d\n",
9a8afc8d
NH
357 dropmon_ops[i].cmd);
358 goto out_unreg;
359 }
360 }
361
4ea7e386
NH
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
9a8afc8d
NH
368 rc = 0;
369
370 for_each_present_cpu(cpu) {
371 data = &per_cpu(dm_cpu_data, cpu);
372 reset_per_cpu_data(data);
373 INIT_WORK(&data->dm_alert_work, send_dm_alert);
374 init_timer(&data->send_timer);
375 data->send_timer.data = cpu;
376 data->send_timer.function = sched_send_work;
377 }
4ea7e386 378
9a8afc8d
NH
379 goto out;
380
381out_unreg:
382 genl_unregister_family(&net_drop_monitor_family);
383out:
384 return rc;
385}
386
387late_initcall(init_net_drop_monitor);