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