2 * Monitoring code for network dropped packet alerts
4 * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
30 #include <trace/events/skb.h>
31 #include <trace/events/napi.h>
33 #include <asm/unaligned.h>
38 static void send_dm_alert(struct work_struct *unused);
42 * Globals, our netlink socket pointer
43 * and the work handle that will send up
46 static int trace_state = TRACE_OFF;
47 static DEFINE_MUTEX(trace_state_mutex);
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;
57 struct dm_hw_stat_delta {
58 struct net_device *dev;
59 unsigned long last_rx;
60 struct list_head list;
62 unsigned long last_drop_val;
65 static struct genl_family net_drop_monitor_family = {
66 .id = GENL_ID_GENERATE,
70 .maxattr = NET_DM_CMD_MAX,
73 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
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);
80 static void reset_per_cpu_data(struct per_cpu_dm_data *data)
83 struct net_dm_alert_msg *msg;
86 struct sk_buff *oskb = rcu_dereference_protected(data->skb, 1);
88 al = sizeof(struct net_dm_alert_msg);
89 al += dm_hit_limit * sizeof(struct net_dm_drop_point);
90 al += sizeof(struct nlattr);
92 skb = genlmsg_new(al, GFP_KERNEL);
95 genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
97 nla = nla_reserve(skb, NLA_UNSPEC,
98 sizeof(struct net_dm_alert_msg));
102 schedule_work_on(data->cpu, &data->dm_alert_work);
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.
112 rcu_assign_pointer(data->skb, skb);
116 atomic_set(&data->dm_hit_count, dm_hit_limit);
121 static void send_dm_alert(struct work_struct *unused)
124 struct per_cpu_dm_data *data = &get_cpu_var(dm_cpu_data);
126 WARN_ON_ONCE(data->cpu != smp_processor_id());
129 * Grab the skb we're about to send
131 skb = rcu_dereference_protected(data->skb, 1);
134 * Replace it with a new one
136 reset_per_cpu_data(data);
142 genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
144 put_cpu_var(dm_cpu_data);
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
153 static void sched_send_work(unsigned long unused)
155 struct per_cpu_dm_data *data = &get_cpu_var(dm_cpu_data);
157 schedule_work_on(smp_processor_id(), &data->dm_alert_work);
159 put_cpu_var(dm_cpu_data);
162 static void trace_drop_common(struct sk_buff *skb, void *location)
164 struct net_dm_alert_msg *msg;
165 struct nlmsghdr *nlh;
168 struct sk_buff *dskb;
169 struct per_cpu_dm_data *data = &get_cpu_var(dm_cpu_data);
173 dskb = rcu_dereference(data->skb);
178 if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
180 * we're already at zero, discard this hit
185 nlh = (struct nlmsghdr *)dskb->data;
186 nla = genlmsg_data(nlmsg_data(nlh));
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);
197 * We need to create a new entry
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;
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());
212 put_cpu_var(dm_cpu_data);
216 static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
218 trace_drop_common(skb, location);
221 static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi)
223 struct dm_hw_stat_delta *new_stat;
226 * Don't check napi structures with no associated device
232 list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
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
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;
251 static int set_all_monitor_traces(int state)
254 struct dm_hw_stat_delta *new_stat = NULL;
255 struct dm_hw_stat_delta *temp;
257 mutex_lock(&trace_state_mutex);
259 if (state == trace_state) {
266 rc |= register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
267 rc |= register_trace_napi_poll(trace_napi_poll_hit, NULL);
270 rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
271 rc |= unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
273 tracepoint_synchronize_unregister();
276 * Clean the device list
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);
296 mutex_unlock(&trace_state_mutex);
302 static int net_dm_cmd_config(struct sk_buff *skb,
303 struct genl_info *info)
308 static int net_dm_cmd_trace(struct sk_buff *skb,
309 struct genl_info *info)
311 switch (info->genlhdr->cmd) {
312 case NET_DM_CMD_START:
313 return set_all_monitor_traces(TRACE_ON);
315 case NET_DM_CMD_STOP:
316 return set_all_monitor_traces(TRACE_OFF);
323 static int dropmon_net_event(struct notifier_block *ev_block,
324 unsigned long event, void *ptr)
326 struct net_device *dev = ptr;
327 struct dm_hw_stat_delta *new_stat = NULL;
328 struct dm_hw_stat_delta *tmp;
331 case NETDEV_REGISTER:
332 new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
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);
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);
355 mutex_unlock(&trace_state_mutex);
362 static struct genl_ops dropmon_ops[] = {
364 .cmd = NET_DM_CMD_CONFIG,
365 .doit = net_dm_cmd_config,
368 .cmd = NET_DM_CMD_START,
369 .doit = net_dm_cmd_trace,
372 .cmd = NET_DM_CMD_STOP,
373 .doit = net_dm_cmd_trace,
377 static struct notifier_block dropmon_net_notifier = {
378 .notifier_call = dropmon_net_event
381 static int __init init_net_drop_monitor(void)
383 struct per_cpu_dm_data *data;
386 pr_info("Initializing network drop monitor service\n");
388 if (sizeof(void *) > 8) {
389 pr_err("Unable to store program counters on this arch, Drop monitor failed\n");
393 rc = genl_register_family_with_ops(&net_drop_monitor_family,
395 ARRAY_SIZE(dropmon_ops));
397 pr_err("Could not create drop monitor netlink family\n");
401 rc = register_netdevice_notifier(&dropmon_net_notifier);
403 pr_crit("Failed to register netdevice notifier\n");
409 for_each_present_cpu(cpu) {
410 data = &per_cpu(dm_cpu_data, 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);
423 genl_unregister_family(&net_drop_monitor_family);
428 late_initcall(init_net_drop_monitor);