]> asedeno.scripts.mit.edu Git - linux.git/blob - net/core/drop_monitor.c
b266dc1660edc20d1dc5ba924ecd8e0c2744af92
[linux.git] / net / core / drop_monitor.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Monitoring code for network dropped packet alerts
4  *
5  * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/string.h>
13 #include <linux/if_arp.h>
14 #include <linux/inetdevice.h>
15 #include <linux/inet.h>
16 #include <linux/interrupt.h>
17 #include <linux/netpoll.h>
18 #include <linux/sched.h>
19 #include <linux/delay.h>
20 #include <linux/types.h>
21 #include <linux/workqueue.h>
22 #include <linux/netlink.h>
23 #include <linux/net_dropmon.h>
24 #include <linux/percpu.h>
25 #include <linux/timer.h>
26 #include <linux/bitops.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <net/genetlink.h>
30 #include <net/netevent.h>
31
32 #include <trace/events/skb.h>
33 #include <trace/events/napi.h>
34
35 #include <asm/unaligned.h>
36
37 #define TRACE_ON 1
38 #define TRACE_OFF 0
39
40 /*
41  * Globals, our netlink socket pointer
42  * and the work handle that will send up
43  * netlink alerts
44  */
45 static int trace_state = TRACE_OFF;
46
47 /* net_dm_mutex
48  *
49  * An overall lock guarding every operation coming from userspace.
50  * It also guards the global 'hw_stats_list' list.
51  */
52 static DEFINE_MUTEX(net_dm_mutex);
53
54 struct per_cpu_dm_data {
55         spinlock_t              lock;   /* Protects 'skb' and 'send_timer' */
56         struct sk_buff          *skb;
57         struct work_struct      dm_alert_work;
58         struct timer_list       send_timer;
59 };
60
61 struct dm_hw_stat_delta {
62         struct net_device *dev;
63         unsigned long last_rx;
64         struct list_head list;
65         struct rcu_head rcu;
66         unsigned long last_drop_val;
67 };
68
69 static struct genl_family net_drop_monitor_family;
70
71 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
72
73 static int dm_hit_limit = 64;
74 static int dm_delay = 1;
75 static unsigned long dm_hw_check_delta = 2*HZ;
76 static LIST_HEAD(hw_stats_list);
77
78 static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
79 {
80         size_t al;
81         struct net_dm_alert_msg *msg;
82         struct nlattr *nla;
83         struct sk_buff *skb;
84         unsigned long flags;
85         void *msg_header;
86
87         al = sizeof(struct net_dm_alert_msg);
88         al += dm_hit_limit * sizeof(struct net_dm_drop_point);
89         al += sizeof(struct nlattr);
90
91         skb = genlmsg_new(al, GFP_KERNEL);
92
93         if (!skb)
94                 goto err;
95
96         msg_header = genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
97                                  0, NET_DM_CMD_ALERT);
98         if (!msg_header) {
99                 nlmsg_free(skb);
100                 skb = NULL;
101                 goto err;
102         }
103         nla = nla_reserve(skb, NLA_UNSPEC,
104                           sizeof(struct net_dm_alert_msg));
105         if (!nla) {
106                 nlmsg_free(skb);
107                 skb = NULL;
108                 goto err;
109         }
110         msg = nla_data(nla);
111         memset(msg, 0, al);
112         goto out;
113
114 err:
115         mod_timer(&data->send_timer, jiffies + HZ / 10);
116 out:
117         spin_lock_irqsave(&data->lock, flags);
118         swap(data->skb, skb);
119         spin_unlock_irqrestore(&data->lock, flags);
120
121         if (skb) {
122                 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
123                 struct genlmsghdr *gnlh = (struct genlmsghdr *)nlmsg_data(nlh);
124
125                 genlmsg_end(skb, genlmsg_data(gnlh));
126         }
127
128         return skb;
129 }
130
131 static const struct genl_multicast_group dropmon_mcgrps[] = {
132         { .name = "events", },
133 };
134
135 static void send_dm_alert(struct work_struct *work)
136 {
137         struct sk_buff *skb;
138         struct per_cpu_dm_data *data;
139
140         data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
141
142         skb = reset_per_cpu_data(data);
143
144         if (skb)
145                 genlmsg_multicast(&net_drop_monitor_family, skb, 0,
146                                   0, GFP_KERNEL);
147 }
148
149 /*
150  * This is the timer function to delay the sending of an alert
151  * in the event that more drops will arrive during the
152  * hysteresis period.
153  */
154 static void sched_send_work(struct timer_list *t)
155 {
156         struct per_cpu_dm_data *data = from_timer(data, t, send_timer);
157
158         schedule_work(&data->dm_alert_work);
159 }
160
161 static void trace_drop_common(struct sk_buff *skb, void *location)
162 {
163         struct net_dm_alert_msg *msg;
164         struct nlmsghdr *nlh;
165         struct nlattr *nla;
166         int i;
167         struct sk_buff *dskb;
168         struct per_cpu_dm_data *data;
169         unsigned long flags;
170
171         local_irq_save(flags);
172         data = this_cpu_ptr(&dm_cpu_data);
173         spin_lock(&data->lock);
174         dskb = data->skb;
175
176         if (!dskb)
177                 goto out;
178
179         nlh = (struct nlmsghdr *)dskb->data;
180         nla = genlmsg_data(nlmsg_data(nlh));
181         msg = nla_data(nla);
182         for (i = 0; i < msg->entries; i++) {
183                 if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
184                         msg->points[i].count++;
185                         goto out;
186                 }
187         }
188         if (msg->entries == dm_hit_limit)
189                 goto out;
190         /*
191          * We need to create a new entry
192          */
193         __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
194         nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
195         memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
196         msg->points[msg->entries].count = 1;
197         msg->entries++;
198
199         if (!timer_pending(&data->send_timer)) {
200                 data->send_timer.expires = jiffies + dm_delay * HZ;
201                 add_timer(&data->send_timer);
202         }
203
204 out:
205         spin_unlock_irqrestore(&data->lock, flags);
206 }
207
208 static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
209 {
210         trace_drop_common(skb, location);
211 }
212
213 static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi,
214                                 int work, int budget)
215 {
216         struct dm_hw_stat_delta *new_stat;
217
218         /*
219          * Don't check napi structures with no associated device
220          */
221         if (!napi->dev)
222                 return;
223
224         rcu_read_lock();
225         list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
226                 /*
227                  * only add a note to our monitor buffer if:
228                  * 1) this is the dev we received on
229                  * 2) its after the last_rx delta
230                  * 3) our rx_dropped count has gone up
231                  */
232                 if ((new_stat->dev == napi->dev)  &&
233                     (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
234                     (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
235                         trace_drop_common(NULL, NULL);
236                         new_stat->last_drop_val = napi->dev->stats.rx_dropped;
237                         new_stat->last_rx = jiffies;
238                         break;
239                 }
240         }
241         rcu_read_unlock();
242 }
243
244 static int net_dm_trace_on_set(struct netlink_ext_ack *extack)
245 {
246         int cpu, rc;
247
248         if (!try_module_get(THIS_MODULE)) {
249                 NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
250                 return -ENODEV;
251         }
252
253         for_each_possible_cpu(cpu) {
254                 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
255
256                 INIT_WORK(&data->dm_alert_work, send_dm_alert);
257                 timer_setup(&data->send_timer, sched_send_work, 0);
258         }
259
260         rc = register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
261         if (rc) {
262                 NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to kfree_skb() tracepoint");
263                 goto err_module_put;
264         }
265
266         rc = register_trace_napi_poll(trace_napi_poll_hit, NULL);
267         if (rc) {
268                 NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to napi_poll() tracepoint");
269                 goto err_unregister_trace;
270         }
271
272         return 0;
273
274 err_unregister_trace:
275         unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
276 err_module_put:
277         module_put(THIS_MODULE);
278         return rc;
279 }
280
281 static void net_dm_trace_off_set(void)
282 {
283         struct dm_hw_stat_delta *new_stat, *temp;
284         int cpu;
285
286         unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
287         unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
288
289         tracepoint_synchronize_unregister();
290
291         /* Make sure we do not send notifications to user space after request
292          * to stop tracing returns.
293          */
294         for_each_possible_cpu(cpu) {
295                 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
296
297                 del_timer_sync(&data->send_timer);
298                 cancel_work_sync(&data->dm_alert_work);
299         }
300
301         list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
302                 if (new_stat->dev == NULL) {
303                         list_del_rcu(&new_stat->list);
304                         kfree_rcu(new_stat, rcu);
305                 }
306         }
307
308         module_put(THIS_MODULE);
309 }
310
311 static int set_all_monitor_traces(int state, struct netlink_ext_ack *extack)
312 {
313         int rc = 0;
314
315         if (state == trace_state) {
316                 NL_SET_ERR_MSG_MOD(extack, "Trace state already set to requested state");
317                 return -EAGAIN;
318         }
319
320         switch (state) {
321         case TRACE_ON:
322                 rc = net_dm_trace_on_set(extack);
323                 break;
324         case TRACE_OFF:
325                 net_dm_trace_off_set();
326                 break;
327         default:
328                 rc = 1;
329                 break;
330         }
331
332         if (!rc)
333                 trace_state = state;
334         else
335                 rc = -EINPROGRESS;
336
337         return rc;
338 }
339
340 static int net_dm_cmd_config(struct sk_buff *skb,
341                         struct genl_info *info)
342 {
343         NL_SET_ERR_MSG_MOD(info->extack, "Command not supported");
344
345         return -EOPNOTSUPP;
346 }
347
348 static int net_dm_cmd_trace(struct sk_buff *skb,
349                         struct genl_info *info)
350 {
351         switch (info->genlhdr->cmd) {
352         case NET_DM_CMD_START:
353                 return set_all_monitor_traces(TRACE_ON, info->extack);
354         case NET_DM_CMD_STOP:
355                 return set_all_monitor_traces(TRACE_OFF, info->extack);
356         }
357
358         return -EOPNOTSUPP;
359 }
360
361 static int dropmon_net_event(struct notifier_block *ev_block,
362                              unsigned long event, void *ptr)
363 {
364         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
365         struct dm_hw_stat_delta *new_stat = NULL;
366         struct dm_hw_stat_delta *tmp;
367
368         switch (event) {
369         case NETDEV_REGISTER:
370                 new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
371
372                 if (!new_stat)
373                         goto out;
374
375                 new_stat->dev = dev;
376                 new_stat->last_rx = jiffies;
377                 mutex_lock(&net_dm_mutex);
378                 list_add_rcu(&new_stat->list, &hw_stats_list);
379                 mutex_unlock(&net_dm_mutex);
380                 break;
381         case NETDEV_UNREGISTER:
382                 mutex_lock(&net_dm_mutex);
383                 list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
384                         if (new_stat->dev == dev) {
385                                 new_stat->dev = NULL;
386                                 if (trace_state == TRACE_OFF) {
387                                         list_del_rcu(&new_stat->list);
388                                         kfree_rcu(new_stat, rcu);
389                                         break;
390                                 }
391                         }
392                 }
393                 mutex_unlock(&net_dm_mutex);
394                 break;
395         }
396 out:
397         return NOTIFY_DONE;
398 }
399
400 static const struct genl_ops dropmon_ops[] = {
401         {
402                 .cmd = NET_DM_CMD_CONFIG,
403                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
404                 .doit = net_dm_cmd_config,
405         },
406         {
407                 .cmd = NET_DM_CMD_START,
408                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
409                 .doit = net_dm_cmd_trace,
410         },
411         {
412                 .cmd = NET_DM_CMD_STOP,
413                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
414                 .doit = net_dm_cmd_trace,
415         },
416 };
417
418 static int net_dm_nl_pre_doit(const struct genl_ops *ops,
419                               struct sk_buff *skb, struct genl_info *info)
420 {
421         mutex_lock(&net_dm_mutex);
422
423         return 0;
424 }
425
426 static void net_dm_nl_post_doit(const struct genl_ops *ops,
427                                 struct sk_buff *skb, struct genl_info *info)
428 {
429         mutex_unlock(&net_dm_mutex);
430 }
431
432 static struct genl_family net_drop_monitor_family __ro_after_init = {
433         .hdrsize        = 0,
434         .name           = "NET_DM",
435         .version        = 2,
436         .pre_doit       = net_dm_nl_pre_doit,
437         .post_doit      = net_dm_nl_post_doit,
438         .module         = THIS_MODULE,
439         .ops            = dropmon_ops,
440         .n_ops          = ARRAY_SIZE(dropmon_ops),
441         .mcgrps         = dropmon_mcgrps,
442         .n_mcgrps       = ARRAY_SIZE(dropmon_mcgrps),
443 };
444
445 static struct notifier_block dropmon_net_notifier = {
446         .notifier_call = dropmon_net_event
447 };
448
449 static int __init init_net_drop_monitor(void)
450 {
451         struct per_cpu_dm_data *data;
452         int cpu, rc;
453
454         pr_info("Initializing network drop monitor service\n");
455
456         if (sizeof(void *) > 8) {
457                 pr_err("Unable to store program counters on this arch, Drop monitor failed\n");
458                 return -ENOSPC;
459         }
460
461         rc = genl_register_family(&net_drop_monitor_family);
462         if (rc) {
463                 pr_err("Could not create drop monitor netlink family\n");
464                 return rc;
465         }
466         WARN_ON(net_drop_monitor_family.mcgrp_offset != NET_DM_GRP_ALERT);
467
468         rc = register_netdevice_notifier(&dropmon_net_notifier);
469         if (rc < 0) {
470                 pr_crit("Failed to register netdevice notifier\n");
471                 goto out_unreg;
472         }
473
474         rc = 0;
475
476         for_each_possible_cpu(cpu) {
477                 data = &per_cpu(dm_cpu_data, cpu);
478                 INIT_WORK(&data->dm_alert_work, send_dm_alert);
479                 timer_setup(&data->send_timer, sched_send_work, 0);
480                 spin_lock_init(&data->lock);
481                 reset_per_cpu_data(data);
482         }
483
484         goto out;
485
486 out_unreg:
487         genl_unregister_family(&net_drop_monitor_family);
488 out:
489         return rc;
490 }
491
492 static void exit_net_drop_monitor(void)
493 {
494         struct per_cpu_dm_data *data;
495         int cpu;
496
497         BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier));
498
499         /*
500          * Because of the module_get/put we do in the trace state change path
501          * we are guarnateed not to have any current users when we get here
502          */
503
504         for_each_possible_cpu(cpu) {
505                 data = &per_cpu(dm_cpu_data, cpu);
506                 /*
507                  * At this point, we should have exclusive access
508                  * to this struct and can free the skb inside it
509                  */
510                 kfree_skb(data->skb);
511         }
512
513         BUG_ON(genl_unregister_family(&net_drop_monitor_family));
514 }
515
516 module_init(init_net_drop_monitor);
517 module_exit(exit_net_drop_monitor);
518
519 MODULE_LICENSE("GPL v2");
520 MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
521 MODULE_ALIAS_GENL_FAMILY("NET_DM");