1 // SPDX-License-Identifier: GPL-2.0-only
3 * Monitoring code for network dropped packet alerts
5 * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
32 #include <trace/events/skb.h>
33 #include <trace/events/napi.h>
35 #include <asm/unaligned.h>
41 * Globals, our netlink socket pointer
42 * and the work handle that will send up
45 static int trace_state = TRACE_OFF;
49 * An overall lock guarding every operation coming from userspace.
50 * It also guards the global 'hw_stats_list' list.
52 static DEFINE_MUTEX(net_dm_mutex);
54 struct per_cpu_dm_data {
55 spinlock_t lock; /* Protects 'skb' and 'send_timer' */
57 struct work_struct dm_alert_work;
58 struct timer_list send_timer;
61 struct dm_hw_stat_delta {
62 struct net_device *dev;
63 unsigned long last_rx;
64 struct list_head list;
66 unsigned long last_drop_val;
69 static struct genl_family net_drop_monitor_family;
71 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
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);
78 static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
81 struct net_dm_alert_msg *msg;
87 al = sizeof(struct net_dm_alert_msg);
88 al += dm_hit_limit * sizeof(struct net_dm_drop_point);
89 al += sizeof(struct nlattr);
91 skb = genlmsg_new(al, GFP_KERNEL);
96 msg_header = genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
103 nla = nla_reserve(skb, NLA_UNSPEC,
104 sizeof(struct net_dm_alert_msg));
115 mod_timer(&data->send_timer, jiffies + HZ / 10);
117 spin_lock_irqsave(&data->lock, flags);
118 swap(data->skb, skb);
119 spin_unlock_irqrestore(&data->lock, flags);
122 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
123 struct genlmsghdr *gnlh = (struct genlmsghdr *)nlmsg_data(nlh);
125 genlmsg_end(skb, genlmsg_data(gnlh));
131 static const struct genl_multicast_group dropmon_mcgrps[] = {
132 { .name = "events", },
135 static void send_dm_alert(struct work_struct *work)
138 struct per_cpu_dm_data *data;
140 data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
142 skb = reset_per_cpu_data(data);
145 genlmsg_multicast(&net_drop_monitor_family, skb, 0,
150 * This is the timer function to delay the sending of an alert
151 * in the event that more drops will arrive during the
154 static void sched_send_work(struct timer_list *t)
156 struct per_cpu_dm_data *data = from_timer(data, t, send_timer);
158 schedule_work(&data->dm_alert_work);
161 static void trace_drop_common(struct sk_buff *skb, void *location)
163 struct net_dm_alert_msg *msg;
164 struct nlmsghdr *nlh;
167 struct sk_buff *dskb;
168 struct per_cpu_dm_data *data;
171 local_irq_save(flags);
172 data = this_cpu_ptr(&dm_cpu_data);
173 spin_lock(&data->lock);
179 nlh = (struct nlmsghdr *)dskb->data;
180 nla = genlmsg_data(nlmsg_data(nlh));
182 for (i = 0; i < msg->entries; i++) {
183 if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
184 msg->points[i].count++;
188 if (msg->entries == dm_hit_limit)
191 * We need to create a new entry
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;
199 if (!timer_pending(&data->send_timer)) {
200 data->send_timer.expires = jiffies + dm_delay * HZ;
201 add_timer(&data->send_timer);
205 spin_unlock_irqrestore(&data->lock, flags);
208 static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
210 trace_drop_common(skb, location);
213 static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi,
214 int work, int budget)
216 struct dm_hw_stat_delta *new_stat;
219 * Don't check napi structures with no associated device
225 list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
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
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;
244 static int net_dm_trace_on_set(struct netlink_ext_ack *extack)
248 if (!try_module_get(THIS_MODULE)) {
249 NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
253 for_each_possible_cpu(cpu) {
254 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
256 INIT_WORK(&data->dm_alert_work, send_dm_alert);
257 timer_setup(&data->send_timer, sched_send_work, 0);
260 rc = register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
262 NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to kfree_skb() tracepoint");
266 rc = register_trace_napi_poll(trace_napi_poll_hit, NULL);
268 NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to napi_poll() tracepoint");
269 goto err_unregister_trace;
274 err_unregister_trace:
275 unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
277 module_put(THIS_MODULE);
281 static void net_dm_trace_off_set(void)
283 struct dm_hw_stat_delta *new_stat, *temp;
286 unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
287 unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
289 tracepoint_synchronize_unregister();
291 /* Make sure we do not send notifications to user space after request
292 * to stop tracing returns.
294 for_each_possible_cpu(cpu) {
295 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
297 del_timer_sync(&data->send_timer);
298 cancel_work_sync(&data->dm_alert_work);
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);
308 module_put(THIS_MODULE);
311 static int set_all_monitor_traces(int state, struct netlink_ext_ack *extack)
315 if (state == trace_state) {
316 NL_SET_ERR_MSG_MOD(extack, "Trace state already set to requested state");
322 rc = net_dm_trace_on_set(extack);
325 net_dm_trace_off_set();
340 static int net_dm_cmd_config(struct sk_buff *skb,
341 struct genl_info *info)
343 NL_SET_ERR_MSG_MOD(info->extack, "Command not supported");
348 static int net_dm_cmd_trace(struct sk_buff *skb,
349 struct genl_info *info)
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);
361 static int dropmon_net_event(struct notifier_block *ev_block,
362 unsigned long event, void *ptr)
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;
369 case NETDEV_REGISTER:
370 new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
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);
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);
393 mutex_unlock(&net_dm_mutex);
400 static const struct genl_ops dropmon_ops[] = {
402 .cmd = NET_DM_CMD_CONFIG,
403 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
404 .doit = net_dm_cmd_config,
407 .cmd = NET_DM_CMD_START,
408 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
409 .doit = net_dm_cmd_trace,
412 .cmd = NET_DM_CMD_STOP,
413 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
414 .doit = net_dm_cmd_trace,
418 static int net_dm_nl_pre_doit(const struct genl_ops *ops,
419 struct sk_buff *skb, struct genl_info *info)
421 mutex_lock(&net_dm_mutex);
426 static void net_dm_nl_post_doit(const struct genl_ops *ops,
427 struct sk_buff *skb, struct genl_info *info)
429 mutex_unlock(&net_dm_mutex);
432 static struct genl_family net_drop_monitor_family __ro_after_init = {
436 .pre_doit = net_dm_nl_pre_doit,
437 .post_doit = net_dm_nl_post_doit,
438 .module = THIS_MODULE,
440 .n_ops = ARRAY_SIZE(dropmon_ops),
441 .mcgrps = dropmon_mcgrps,
442 .n_mcgrps = ARRAY_SIZE(dropmon_mcgrps),
445 static struct notifier_block dropmon_net_notifier = {
446 .notifier_call = dropmon_net_event
449 static int __init init_net_drop_monitor(void)
451 struct per_cpu_dm_data *data;
454 pr_info("Initializing network drop monitor service\n");
456 if (sizeof(void *) > 8) {
457 pr_err("Unable to store program counters on this arch, Drop monitor failed\n");
461 rc = genl_register_family(&net_drop_monitor_family);
463 pr_err("Could not create drop monitor netlink family\n");
466 WARN_ON(net_drop_monitor_family.mcgrp_offset != NET_DM_GRP_ALERT);
468 rc = register_netdevice_notifier(&dropmon_net_notifier);
470 pr_crit("Failed to register netdevice notifier\n");
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);
487 genl_unregister_family(&net_drop_monitor_family);
492 static void exit_net_drop_monitor(void)
494 struct per_cpu_dm_data *data;
497 BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier));
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
504 for_each_possible_cpu(cpu) {
505 data = &per_cpu(dm_cpu_data, cpu);
507 * At this point, we should have exclusive access
508 * to this struct and can free the skb inside it
510 kfree_skb(data->skb);
513 BUG_ON(genl_unregister_family(&net_drop_monitor_family));
516 module_init(init_net_drop_monitor);
517 module_exit(exit_net_drop_monitor);
519 MODULE_LICENSE("GPL v2");
520 MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
521 MODULE_ALIAS_GENL_FAMILY("NET_DM");