Commit | Line | Data |
---|---|---|
9a8afc8d NH |
1 | /* |
2 | * Monitoring code for network dropped packet alerts | |
3 | * | |
4 | * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com> | |
5 | */ | |
6 | ||
e005d193 JP |
7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
8 | ||
9a8afc8d NH |
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> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
cad456d5 | 27 | #include <linux/module.h> |
9a8afc8d | 28 | #include <net/genetlink.h> |
4ea7e386 | 29 | #include <net/netevent.h> |
9a8afc8d | 30 | |
ad8d75ff | 31 | #include <trace/events/skb.h> |
9cbc1cb8 | 32 | #include <trace/events/napi.h> |
9a8afc8d NH |
33 | |
34 | #include <asm/unaligned.h> | |
35 | ||
36 | #define TRACE_ON 1 | |
37 | #define TRACE_OFF 0 | |
38 | ||
9a8afc8d NH |
39 | /* |
40 | * Globals, our netlink socket pointer | |
41 | * and the work handle that will send up | |
42 | * netlink alerts | |
43 | */ | |
4ea7e386 | 44 | static int trace_state = TRACE_OFF; |
cde2e9a6 | 45 | static DEFINE_MUTEX(trace_state_mutex); |
9a8afc8d NH |
46 | |
47 | struct per_cpu_dm_data { | |
bec4596b ED |
48 | spinlock_t lock; |
49 | struct sk_buff *skb; | |
50 | struct work_struct dm_alert_work; | |
51 | struct timer_list send_timer; | |
9a8afc8d NH |
52 | }; |
53 | ||
4ea7e386 NH |
54 | struct dm_hw_stat_delta { |
55 | struct net_device *dev; | |
5848cc09 | 56 | unsigned long last_rx; |
4ea7e386 NH |
57 | struct list_head list; |
58 | struct rcu_head rcu; | |
59 | unsigned long last_drop_val; | |
60 | }; | |
61 | ||
9a8afc8d NH |
62 | static struct genl_family net_drop_monitor_family = { |
63 | .id = GENL_ID_GENERATE, | |
64 | .hdrsize = 0, | |
65 | .name = "NET_DM", | |
683703a2 | 66 | .version = 2, |
9a8afc8d NH |
67 | }; |
68 | ||
69 | static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data); | |
70 | ||
71 | static int dm_hit_limit = 64; | |
72 | static int dm_delay = 1; | |
4ea7e386 NH |
73 | static unsigned long dm_hw_check_delta = 2*HZ; |
74 | static LIST_HEAD(hw_stats_list); | |
9a8afc8d | 75 | |
bec4596b | 76 | static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data) |
9a8afc8d NH |
77 | { |
78 | size_t al; | |
79 | struct net_dm_alert_msg *msg; | |
683703a2 | 80 | struct nlattr *nla; |
3885ca78 | 81 | struct sk_buff *skb; |
bec4596b | 82 | unsigned long flags; |
0cfc0624 | 83 | void *msg_header; |
9a8afc8d NH |
84 | |
85 | al = sizeof(struct net_dm_alert_msg); | |
86 | al += dm_hit_limit * sizeof(struct net_dm_drop_point); | |
683703a2 NH |
87 | al += sizeof(struct nlattr); |
88 | ||
3885ca78 NH |
89 | skb = genlmsg_new(al, GFP_KERNEL); |
90 | ||
0cfc0624 RW |
91 | if (!skb) |
92 | goto err; | |
3885ca78 | 93 | |
0cfc0624 RW |
94 | msg_header = genlmsg_put(skb, 0, 0, &net_drop_monitor_family, |
95 | 0, NET_DM_CMD_ALERT); | |
96 | if (!msg_header) { | |
97 | nlmsg_free(skb); | |
98 | skb = NULL; | |
99 | goto err; | |
100 | } | |
101 | nla = nla_reserve(skb, NLA_UNSPEC, | |
102 | sizeof(struct net_dm_alert_msg)); | |
103 | if (!nla) { | |
104 | nlmsg_free(skb); | |
105 | skb = NULL; | |
106 | goto err; | |
107 | } | |
108 | msg = nla_data(nla); | |
109 | memset(msg, 0, al); | |
0cfc0624 RW |
110 | goto out; |
111 | ||
112 | err: | |
113 | mod_timer(&data->send_timer, jiffies + HZ / 10); | |
114 | out: | |
bec4596b ED |
115 | spin_lock_irqsave(&data->lock, flags); |
116 | swap(data->skb, skb); | |
117 | spin_unlock_irqrestore(&data->lock, flags); | |
118 | ||
e88f37a1 RW |
119 | if (skb) { |
120 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data; | |
121 | struct genlmsghdr *gnlh = (struct genlmsghdr *)nlmsg_data(nlh); | |
122 | ||
123 | genlmsg_end(skb, genlmsg_data(gnlh)); | |
124 | } | |
125 | ||
bec4596b | 126 | return skb; |
9a8afc8d NH |
127 | } |
128 | ||
bec4596b | 129 | static void send_dm_alert(struct work_struct *work) |
9a8afc8d NH |
130 | { |
131 | struct sk_buff *skb; | |
bec4596b | 132 | struct per_cpu_dm_data *data; |
9a8afc8d | 133 | |
bec4596b | 134 | data = container_of(work, struct per_cpu_dm_data, dm_alert_work); |
4fdcfa12 | 135 | |
bec4596b | 136 | skb = reset_per_cpu_data(data); |
9a8afc8d | 137 | |
3885ca78 NH |
138 | if (skb) |
139 | genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL); | |
9a8afc8d NH |
140 | } |
141 | ||
142 | /* | |
143 | * This is the timer function to delay the sending of an alert | |
144 | * in the event that more drops will arrive during the | |
bec4596b | 145 | * hysteresis period. |
9a8afc8d | 146 | */ |
bec4596b | 147 | static void sched_send_work(unsigned long _data) |
9a8afc8d | 148 | { |
bec4596b | 149 | struct per_cpu_dm_data *data = (struct per_cpu_dm_data *)_data; |
3885ca78 | 150 | |
bec4596b | 151 | schedule_work(&data->dm_alert_work); |
9a8afc8d NH |
152 | } |
153 | ||
4ea7e386 | 154 | static void trace_drop_common(struct sk_buff *skb, void *location) |
9a8afc8d NH |
155 | { |
156 | struct net_dm_alert_msg *msg; | |
157 | struct nlmsghdr *nlh; | |
683703a2 | 158 | struct nlattr *nla; |
9a8afc8d | 159 | int i; |
3885ca78 | 160 | struct sk_buff *dskb; |
bec4596b ED |
161 | struct per_cpu_dm_data *data; |
162 | unsigned long flags; | |
9a8afc8d | 163 | |
bec4596b ED |
164 | local_irq_save(flags); |
165 | data = &__get_cpu_var(dm_cpu_data); | |
166 | spin_lock(&data->lock); | |
167 | dskb = data->skb; | |
3885ca78 NH |
168 | |
169 | if (!dskb) | |
170 | goto out; | |
171 | ||
3885ca78 | 172 | nlh = (struct nlmsghdr *)dskb->data; |
683703a2 NH |
173 | nla = genlmsg_data(nlmsg_data(nlh)); |
174 | msg = nla_data(nla); | |
9a8afc8d NH |
175 | for (i = 0; i < msg->entries; i++) { |
176 | if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) { | |
177 | msg->points[i].count++; | |
178 | goto out; | |
179 | } | |
180 | } | |
bec4596b ED |
181 | if (msg->entries == dm_hit_limit) |
182 | goto out; | |
9a8afc8d NH |
183 | /* |
184 | * We need to create a new entry | |
185 | */ | |
3885ca78 | 186 | __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point)); |
683703a2 | 187 | nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point)); |
9a8afc8d NH |
188 | memcpy(msg->points[msg->entries].pc, &location, sizeof(void *)); |
189 | msg->points[msg->entries].count = 1; | |
190 | msg->entries++; | |
191 | ||
192 | if (!timer_pending(&data->send_timer)) { | |
193 | data->send_timer.expires = jiffies + dm_delay * HZ; | |
bec4596b | 194 | add_timer(&data->send_timer); |
9a8afc8d NH |
195 | } |
196 | ||
197 | out: | |
bec4596b | 198 | spin_unlock_irqrestore(&data->lock, flags); |
9a8afc8d NH |
199 | } |
200 | ||
38516ab5 | 201 | static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location) |
4ea7e386 NH |
202 | { |
203 | trace_drop_common(skb, location); | |
204 | } | |
205 | ||
38516ab5 | 206 | static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi) |
4ea7e386 NH |
207 | { |
208 | struct dm_hw_stat_delta *new_stat; | |
209 | ||
210 | /* | |
5848cc09 | 211 | * Don't check napi structures with no associated device |
4ea7e386 | 212 | */ |
5848cc09 | 213 | if (!napi->dev) |
4ea7e386 NH |
214 | return; |
215 | ||
216 | rcu_read_lock(); | |
217 | list_for_each_entry_rcu(new_stat, &hw_stats_list, list) { | |
5848cc09 NH |
218 | /* |
219 | * only add a note to our monitor buffer if: | |
220 | * 1) this is the dev we received on | |
221 | * 2) its after the last_rx delta | |
222 | * 3) our rx_dropped count has gone up | |
223 | */ | |
4ea7e386 | 224 | if ((new_stat->dev == napi->dev) && |
5848cc09 | 225 | (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) && |
4ea7e386 NH |
226 | (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) { |
227 | trace_drop_common(NULL, NULL); | |
228 | new_stat->last_drop_val = napi->dev->stats.rx_dropped; | |
5848cc09 | 229 | new_stat->last_rx = jiffies; |
4ea7e386 NH |
230 | break; |
231 | } | |
232 | } | |
233 | rcu_read_unlock(); | |
234 | } | |
235 | ||
9a8afc8d NH |
236 | static int set_all_monitor_traces(int state) |
237 | { | |
238 | int rc = 0; | |
4ea7e386 NH |
239 | struct dm_hw_stat_delta *new_stat = NULL; |
240 | struct dm_hw_stat_delta *temp; | |
241 | ||
cde2e9a6 | 242 | mutex_lock(&trace_state_mutex); |
9a8afc8d | 243 | |
4b706372 NH |
244 | if (state == trace_state) { |
245 | rc = -EAGAIN; | |
246 | goto out_unlock; | |
247 | } | |
248 | ||
9a8afc8d NH |
249 | switch (state) { |
250 | case TRACE_ON: | |
cad456d5 NH |
251 | if (!try_module_get(THIS_MODULE)) { |
252 | rc = -ENODEV; | |
253 | break; | |
254 | } | |
255 | ||
38516ab5 SR |
256 | rc |= register_trace_kfree_skb(trace_kfree_skb_hit, NULL); |
257 | rc |= register_trace_napi_poll(trace_napi_poll_hit, NULL); | |
9a8afc8d | 258 | break; |
cad456d5 | 259 | |
9a8afc8d | 260 | case TRACE_OFF: |
38516ab5 SR |
261 | rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL); |
262 | rc |= unregister_trace_napi_poll(trace_napi_poll_hit, NULL); | |
9a8afc8d NH |
263 | |
264 | tracepoint_synchronize_unregister(); | |
4ea7e386 NH |
265 | |
266 | /* | |
267 | * Clean the device list | |
268 | */ | |
269 | list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) { | |
270 | if (new_stat->dev == NULL) { | |
271 | list_del_rcu(&new_stat->list); | |
fa81c0e1 | 272 | kfree_rcu(new_stat, rcu); |
4ea7e386 NH |
273 | } |
274 | } | |
cad456d5 NH |
275 | |
276 | module_put(THIS_MODULE); | |
277 | ||
9a8afc8d NH |
278 | break; |
279 | default: | |
280 | rc = 1; | |
281 | break; | |
282 | } | |
283 | ||
4ea7e386 NH |
284 | if (!rc) |
285 | trace_state = state; | |
4b706372 NH |
286 | else |
287 | rc = -EINPROGRESS; | |
4ea7e386 | 288 | |
4b706372 | 289 | out_unlock: |
cde2e9a6 | 290 | mutex_unlock(&trace_state_mutex); |
4ea7e386 | 291 | |
9a8afc8d NH |
292 | return rc; |
293 | } | |
294 | ||
295 | ||
296 | static int net_dm_cmd_config(struct sk_buff *skb, | |
297 | struct genl_info *info) | |
298 | { | |
299 | return -ENOTSUPP; | |
300 | } | |
301 | ||
302 | static int net_dm_cmd_trace(struct sk_buff *skb, | |
303 | struct genl_info *info) | |
304 | { | |
305 | switch (info->genlhdr->cmd) { | |
306 | case NET_DM_CMD_START: | |
307 | return set_all_monitor_traces(TRACE_ON); | |
308 | break; | |
309 | case NET_DM_CMD_STOP: | |
310 | return set_all_monitor_traces(TRACE_OFF); | |
311 | break; | |
312 | } | |
313 | ||
314 | return -ENOTSUPP; | |
315 | } | |
316 | ||
4ea7e386 NH |
317 | static int dropmon_net_event(struct notifier_block *ev_block, |
318 | unsigned long event, void *ptr) | |
319 | { | |
320 | struct net_device *dev = ptr; | |
321 | struct dm_hw_stat_delta *new_stat = NULL; | |
322 | struct dm_hw_stat_delta *tmp; | |
323 | ||
324 | switch (event) { | |
325 | case NETDEV_REGISTER: | |
326 | new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL); | |
327 | ||
328 | if (!new_stat) | |
329 | goto out; | |
330 | ||
331 | new_stat->dev = dev; | |
5848cc09 | 332 | new_stat->last_rx = jiffies; |
cde2e9a6 | 333 | mutex_lock(&trace_state_mutex); |
4ea7e386 | 334 | list_add_rcu(&new_stat->list, &hw_stats_list); |
cde2e9a6 | 335 | mutex_unlock(&trace_state_mutex); |
4ea7e386 NH |
336 | break; |
337 | case NETDEV_UNREGISTER: | |
cde2e9a6 | 338 | mutex_lock(&trace_state_mutex); |
4ea7e386 NH |
339 | list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) { |
340 | if (new_stat->dev == dev) { | |
341 | new_stat->dev = NULL; | |
342 | if (trace_state == TRACE_OFF) { | |
343 | list_del_rcu(&new_stat->list); | |
fa81c0e1 | 344 | kfree_rcu(new_stat, rcu); |
4ea7e386 NH |
345 | break; |
346 | } | |
347 | } | |
348 | } | |
cde2e9a6 | 349 | mutex_unlock(&trace_state_mutex); |
4ea7e386 NH |
350 | break; |
351 | } | |
352 | out: | |
353 | return NOTIFY_DONE; | |
354 | } | |
9a8afc8d NH |
355 | |
356 | static struct genl_ops dropmon_ops[] = { | |
357 | { | |
358 | .cmd = NET_DM_CMD_CONFIG, | |
359 | .doit = net_dm_cmd_config, | |
360 | }, | |
361 | { | |
362 | .cmd = NET_DM_CMD_START, | |
363 | .doit = net_dm_cmd_trace, | |
364 | }, | |
365 | { | |
366 | .cmd = NET_DM_CMD_STOP, | |
367 | .doit = net_dm_cmd_trace, | |
368 | }, | |
369 | }; | |
370 | ||
4ea7e386 NH |
371 | static struct notifier_block dropmon_net_notifier = { |
372 | .notifier_call = dropmon_net_event | |
373 | }; | |
374 | ||
9a8afc8d NH |
375 | static int __init init_net_drop_monitor(void) |
376 | { | |
9a8afc8d | 377 | struct per_cpu_dm_data *data; |
a256be70 CG |
378 | int cpu, rc; |
379 | ||
e005d193 | 380 | pr_info("Initializing network drop monitor service\n"); |
9a8afc8d NH |
381 | |
382 | if (sizeof(void *) > 8) { | |
e005d193 | 383 | pr_err("Unable to store program counters on this arch, Drop monitor failed\n"); |
9a8afc8d NH |
384 | return -ENOSPC; |
385 | } | |
386 | ||
a256be70 CG |
387 | rc = genl_register_family_with_ops(&net_drop_monitor_family, |
388 | dropmon_ops, | |
389 | ARRAY_SIZE(dropmon_ops)); | |
390 | if (rc) { | |
e005d193 | 391 | pr_err("Could not create drop monitor netlink family\n"); |
a256be70 | 392 | return rc; |
9a8afc8d NH |
393 | } |
394 | ||
4ea7e386 NH |
395 | rc = register_netdevice_notifier(&dropmon_net_notifier); |
396 | if (rc < 0) { | |
e005d193 | 397 | pr_crit("Failed to register netdevice notifier\n"); |
4ea7e386 NH |
398 | goto out_unreg; |
399 | } | |
400 | ||
9a8afc8d NH |
401 | rc = 0; |
402 | ||
cad456d5 | 403 | for_each_possible_cpu(cpu) { |
9a8afc8d | 404 | data = &per_cpu(dm_cpu_data, cpu); |
9a8afc8d NH |
405 | INIT_WORK(&data->dm_alert_work, send_dm_alert); |
406 | init_timer(&data->send_timer); | |
bec4596b | 407 | data->send_timer.data = (unsigned long)data; |
9a8afc8d | 408 | data->send_timer.function = sched_send_work; |
bec4596b | 409 | spin_lock_init(&data->lock); |
4fdcfa12 | 410 | reset_per_cpu_data(data); |
9a8afc8d | 411 | } |
4ea7e386 | 412 | |
3885ca78 | 413 | |
9a8afc8d NH |
414 | goto out; |
415 | ||
416 | out_unreg: | |
417 | genl_unregister_family(&net_drop_monitor_family); | |
418 | out: | |
419 | return rc; | |
420 | } | |
421 | ||
cad456d5 NH |
422 | static void exit_net_drop_monitor(void) |
423 | { | |
424 | struct per_cpu_dm_data *data; | |
425 | int cpu; | |
426 | ||
427 | BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier)); | |
428 | ||
429 | /* | |
430 | * Because of the module_get/put we do in the trace state change path | |
431 | * we are guarnateed not to have any current users when we get here | |
432 | * all we need to do is make sure that we don't have any running timers | |
433 | * or pending schedule calls | |
434 | */ | |
435 | ||
436 | for_each_possible_cpu(cpu) { | |
437 | data = &per_cpu(dm_cpu_data, cpu); | |
438 | del_timer_sync(&data->send_timer); | |
439 | cancel_work_sync(&data->dm_alert_work); | |
440 | /* | |
441 | * At this point, we should have exclusive access | |
442 | * to this struct and can free the skb inside it | |
443 | */ | |
444 | kfree_skb(data->skb); | |
445 | } | |
446 | ||
447 | BUG_ON(genl_unregister_family(&net_drop_monitor_family)); | |
448 | } | |
449 | ||
450 | module_init(init_net_drop_monitor); | |
451 | module_exit(exit_net_drop_monitor); | |
452 | ||
453 | MODULE_LICENSE("GPL v2"); | |
454 | MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>"); | |
3fdcbd45 | 455 | MODULE_ALIAS_GENL_FAMILY("NET_DM"); |