xref: /openbmc/linux/net/core/drop_monitor.c (revision 7a2eb736)
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/drop_monitor.h>
30 #include <net/genetlink.h>
31 #include <net/netevent.h>
32 
33 #include <trace/events/skb.h>
34 #include <trace/events/napi.h>
35 
36 #include <asm/unaligned.h>
37 
38 #define TRACE_ON 1
39 #define TRACE_OFF 0
40 
41 /*
42  * Globals, our netlink socket pointer
43  * and the work handle that will send up
44  * netlink alerts
45  */
46 static int trace_state = TRACE_OFF;
47 static bool monitor_hw;
48 
49 /* net_dm_mutex
50  *
51  * An overall lock guarding every operation coming from userspace.
52  * It also guards the global 'hw_stats_list' list.
53  */
54 static DEFINE_MUTEX(net_dm_mutex);
55 
56 struct net_dm_stats {
57 	u64 dropped;
58 	struct u64_stats_sync syncp;
59 };
60 
61 #define NET_DM_MAX_HW_TRAP_NAME_LEN 40
62 
63 struct net_dm_hw_entry {
64 	char trap_name[NET_DM_MAX_HW_TRAP_NAME_LEN];
65 	u32 count;
66 };
67 
68 struct net_dm_hw_entries {
69 	u32 num_entries;
70 	struct net_dm_hw_entry entries[0];
71 };
72 
73 struct per_cpu_dm_data {
74 	spinlock_t		lock;	/* Protects 'skb', 'hw_entries' and
75 					 * 'send_timer'
76 					 */
77 	union {
78 		struct sk_buff			*skb;
79 		struct net_dm_hw_entries	*hw_entries;
80 	};
81 	struct sk_buff_head	drop_queue;
82 	struct work_struct	dm_alert_work;
83 	struct timer_list	send_timer;
84 	struct net_dm_stats	stats;
85 };
86 
87 struct dm_hw_stat_delta {
88 	struct net_device *dev;
89 	unsigned long last_rx;
90 	struct list_head list;
91 	struct rcu_head rcu;
92 	unsigned long last_drop_val;
93 };
94 
95 static struct genl_family net_drop_monitor_family;
96 
97 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
98 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_hw_cpu_data);
99 
100 static int dm_hit_limit = 64;
101 static int dm_delay = 1;
102 static unsigned long dm_hw_check_delta = 2*HZ;
103 static LIST_HEAD(hw_stats_list);
104 
105 static enum net_dm_alert_mode net_dm_alert_mode = NET_DM_ALERT_MODE_SUMMARY;
106 static u32 net_dm_trunc_len;
107 static u32 net_dm_queue_len = 1000;
108 
109 struct net_dm_alert_ops {
110 	void (*kfree_skb_probe)(void *ignore, struct sk_buff *skb,
111 				void *location);
112 	void (*napi_poll_probe)(void *ignore, struct napi_struct *napi,
113 				int work, int budget);
114 	void (*work_item_func)(struct work_struct *work);
115 	void (*hw_work_item_func)(struct work_struct *work);
116 	void (*hw_probe)(struct sk_buff *skb,
117 			 const struct net_dm_hw_metadata *hw_metadata);
118 };
119 
120 struct net_dm_skb_cb {
121 	union {
122 		struct net_dm_hw_metadata *hw_metadata;
123 		void *pc;
124 	};
125 };
126 
127 #define NET_DM_SKB_CB(__skb) ((struct net_dm_skb_cb *)&((__skb)->cb[0]))
128 
129 static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
130 {
131 	size_t al;
132 	struct net_dm_alert_msg *msg;
133 	struct nlattr *nla;
134 	struct sk_buff *skb;
135 	unsigned long flags;
136 	void *msg_header;
137 
138 	al = sizeof(struct net_dm_alert_msg);
139 	al += dm_hit_limit * sizeof(struct net_dm_drop_point);
140 	al += sizeof(struct nlattr);
141 
142 	skb = genlmsg_new(al, GFP_KERNEL);
143 
144 	if (!skb)
145 		goto err;
146 
147 	msg_header = genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
148 				 0, NET_DM_CMD_ALERT);
149 	if (!msg_header) {
150 		nlmsg_free(skb);
151 		skb = NULL;
152 		goto err;
153 	}
154 	nla = nla_reserve(skb, NLA_UNSPEC,
155 			  sizeof(struct net_dm_alert_msg));
156 	if (!nla) {
157 		nlmsg_free(skb);
158 		skb = NULL;
159 		goto err;
160 	}
161 	msg = nla_data(nla);
162 	memset(msg, 0, al);
163 	goto out;
164 
165 err:
166 	mod_timer(&data->send_timer, jiffies + HZ / 10);
167 out:
168 	spin_lock_irqsave(&data->lock, flags);
169 	swap(data->skb, skb);
170 	spin_unlock_irqrestore(&data->lock, flags);
171 
172 	if (skb) {
173 		struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
174 		struct genlmsghdr *gnlh = (struct genlmsghdr *)nlmsg_data(nlh);
175 
176 		genlmsg_end(skb, genlmsg_data(gnlh));
177 	}
178 
179 	return skb;
180 }
181 
182 static const struct genl_multicast_group dropmon_mcgrps[] = {
183 	{ .name = "events", },
184 };
185 
186 static void send_dm_alert(struct work_struct *work)
187 {
188 	struct sk_buff *skb;
189 	struct per_cpu_dm_data *data;
190 
191 	data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
192 
193 	skb = reset_per_cpu_data(data);
194 
195 	if (skb)
196 		genlmsg_multicast(&net_drop_monitor_family, skb, 0,
197 				  0, GFP_KERNEL);
198 }
199 
200 /*
201  * This is the timer function to delay the sending of an alert
202  * in the event that more drops will arrive during the
203  * hysteresis period.
204  */
205 static void sched_send_work(struct timer_list *t)
206 {
207 	struct per_cpu_dm_data *data = from_timer(data, t, send_timer);
208 
209 	schedule_work(&data->dm_alert_work);
210 }
211 
212 static void trace_drop_common(struct sk_buff *skb, void *location)
213 {
214 	struct net_dm_alert_msg *msg;
215 	struct nlmsghdr *nlh;
216 	struct nlattr *nla;
217 	int i;
218 	struct sk_buff *dskb;
219 	struct per_cpu_dm_data *data;
220 	unsigned long flags;
221 
222 	local_irq_save(flags);
223 	data = this_cpu_ptr(&dm_cpu_data);
224 	spin_lock(&data->lock);
225 	dskb = data->skb;
226 
227 	if (!dskb)
228 		goto out;
229 
230 	nlh = (struct nlmsghdr *)dskb->data;
231 	nla = genlmsg_data(nlmsg_data(nlh));
232 	msg = nla_data(nla);
233 	for (i = 0; i < msg->entries; i++) {
234 		if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
235 			msg->points[i].count++;
236 			goto out;
237 		}
238 	}
239 	if (msg->entries == dm_hit_limit)
240 		goto out;
241 	/*
242 	 * We need to create a new entry
243 	 */
244 	__nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
245 	nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
246 	memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
247 	msg->points[msg->entries].count = 1;
248 	msg->entries++;
249 
250 	if (!timer_pending(&data->send_timer)) {
251 		data->send_timer.expires = jiffies + dm_delay * HZ;
252 		add_timer(&data->send_timer);
253 	}
254 
255 out:
256 	spin_unlock_irqrestore(&data->lock, flags);
257 }
258 
259 static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
260 {
261 	trace_drop_common(skb, location);
262 }
263 
264 static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi,
265 				int work, int budget)
266 {
267 	struct dm_hw_stat_delta *new_stat;
268 
269 	/*
270 	 * Don't check napi structures with no associated device
271 	 */
272 	if (!napi->dev)
273 		return;
274 
275 	rcu_read_lock();
276 	list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
277 		/*
278 		 * only add a note to our monitor buffer if:
279 		 * 1) this is the dev we received on
280 		 * 2) its after the last_rx delta
281 		 * 3) our rx_dropped count has gone up
282 		 */
283 		if ((new_stat->dev == napi->dev)  &&
284 		    (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
285 		    (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
286 			trace_drop_common(NULL, NULL);
287 			new_stat->last_drop_val = napi->dev->stats.rx_dropped;
288 			new_stat->last_rx = jiffies;
289 			break;
290 		}
291 	}
292 	rcu_read_unlock();
293 }
294 
295 static struct net_dm_hw_entries *
296 net_dm_hw_reset_per_cpu_data(struct per_cpu_dm_data *hw_data)
297 {
298 	struct net_dm_hw_entries *hw_entries;
299 	unsigned long flags;
300 
301 	hw_entries = kzalloc(struct_size(hw_entries, entries, dm_hit_limit),
302 			     GFP_KERNEL);
303 	if (!hw_entries) {
304 		/* If the memory allocation failed, we try to perform another
305 		 * allocation in 1/10 second. Otherwise, the probe function
306 		 * will constantly bail out.
307 		 */
308 		mod_timer(&hw_data->send_timer, jiffies + HZ / 10);
309 	}
310 
311 	spin_lock_irqsave(&hw_data->lock, flags);
312 	swap(hw_data->hw_entries, hw_entries);
313 	spin_unlock_irqrestore(&hw_data->lock, flags);
314 
315 	return hw_entries;
316 }
317 
318 static int net_dm_hw_entry_put(struct sk_buff *msg,
319 			       const struct net_dm_hw_entry *hw_entry)
320 {
321 	struct nlattr *attr;
322 
323 	attr = nla_nest_start(msg, NET_DM_ATTR_HW_ENTRY);
324 	if (!attr)
325 		return -EMSGSIZE;
326 
327 	if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_NAME, hw_entry->trap_name))
328 		goto nla_put_failure;
329 
330 	if (nla_put_u32(msg, NET_DM_ATTR_HW_TRAP_COUNT, hw_entry->count))
331 		goto nla_put_failure;
332 
333 	nla_nest_end(msg, attr);
334 
335 	return 0;
336 
337 nla_put_failure:
338 	nla_nest_cancel(msg, attr);
339 	return -EMSGSIZE;
340 }
341 
342 static int net_dm_hw_entries_put(struct sk_buff *msg,
343 				 const struct net_dm_hw_entries *hw_entries)
344 {
345 	struct nlattr *attr;
346 	int i;
347 
348 	attr = nla_nest_start(msg, NET_DM_ATTR_HW_ENTRIES);
349 	if (!attr)
350 		return -EMSGSIZE;
351 
352 	for (i = 0; i < hw_entries->num_entries; i++) {
353 		int rc;
354 
355 		rc = net_dm_hw_entry_put(msg, &hw_entries->entries[i]);
356 		if (rc)
357 			goto nla_put_failure;
358 	}
359 
360 	nla_nest_end(msg, attr);
361 
362 	return 0;
363 
364 nla_put_failure:
365 	nla_nest_cancel(msg, attr);
366 	return -EMSGSIZE;
367 }
368 
369 static int
370 net_dm_hw_summary_report_fill(struct sk_buff *msg,
371 			      const struct net_dm_hw_entries *hw_entries)
372 {
373 	struct net_dm_alert_msg anc_hdr = { 0 };
374 	void *hdr;
375 	int rc;
376 
377 	hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
378 			  NET_DM_CMD_ALERT);
379 	if (!hdr)
380 		return -EMSGSIZE;
381 
382 	/* We need to put the ancillary header in order not to break user
383 	 * space.
384 	 */
385 	if (nla_put(msg, NLA_UNSPEC, sizeof(anc_hdr), &anc_hdr))
386 		goto nla_put_failure;
387 
388 	rc = net_dm_hw_entries_put(msg, hw_entries);
389 	if (rc)
390 		goto nla_put_failure;
391 
392 	genlmsg_end(msg, hdr);
393 
394 	return 0;
395 
396 nla_put_failure:
397 	genlmsg_cancel(msg, hdr);
398 	return -EMSGSIZE;
399 }
400 
401 static void net_dm_hw_summary_work(struct work_struct *work)
402 {
403 	struct net_dm_hw_entries *hw_entries;
404 	struct per_cpu_dm_data *hw_data;
405 	struct sk_buff *msg;
406 	int rc;
407 
408 	hw_data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
409 
410 	hw_entries = net_dm_hw_reset_per_cpu_data(hw_data);
411 	if (!hw_entries)
412 		return;
413 
414 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
415 	if (!msg)
416 		goto out;
417 
418 	rc = net_dm_hw_summary_report_fill(msg, hw_entries);
419 	if (rc) {
420 		nlmsg_free(msg);
421 		goto out;
422 	}
423 
424 	genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
425 
426 out:
427 	kfree(hw_entries);
428 }
429 
430 static void
431 net_dm_hw_summary_probe(struct sk_buff *skb,
432 			const struct net_dm_hw_metadata *hw_metadata)
433 {
434 	struct net_dm_hw_entries *hw_entries;
435 	struct net_dm_hw_entry *hw_entry;
436 	struct per_cpu_dm_data *hw_data;
437 	unsigned long flags;
438 	int i;
439 
440 	hw_data = this_cpu_ptr(&dm_hw_cpu_data);
441 	spin_lock_irqsave(&hw_data->lock, flags);
442 	hw_entries = hw_data->hw_entries;
443 
444 	if (!hw_entries)
445 		goto out;
446 
447 	for (i = 0; i < hw_entries->num_entries; i++) {
448 		hw_entry = &hw_entries->entries[i];
449 		if (!strncmp(hw_entry->trap_name, hw_metadata->trap_name,
450 			     NET_DM_MAX_HW_TRAP_NAME_LEN - 1)) {
451 			hw_entry->count++;
452 			goto out;
453 		}
454 	}
455 	if (WARN_ON_ONCE(hw_entries->num_entries == dm_hit_limit))
456 		goto out;
457 
458 	hw_entry = &hw_entries->entries[hw_entries->num_entries];
459 	strlcpy(hw_entry->trap_name, hw_metadata->trap_name,
460 		NET_DM_MAX_HW_TRAP_NAME_LEN - 1);
461 	hw_entry->count = 1;
462 	hw_entries->num_entries++;
463 
464 	if (!timer_pending(&hw_data->send_timer)) {
465 		hw_data->send_timer.expires = jiffies + dm_delay * HZ;
466 		add_timer(&hw_data->send_timer);
467 	}
468 
469 out:
470 	spin_unlock_irqrestore(&hw_data->lock, flags);
471 }
472 
473 static const struct net_dm_alert_ops net_dm_alert_summary_ops = {
474 	.kfree_skb_probe	= trace_kfree_skb_hit,
475 	.napi_poll_probe	= trace_napi_poll_hit,
476 	.work_item_func		= send_dm_alert,
477 	.hw_work_item_func	= net_dm_hw_summary_work,
478 	.hw_probe		= net_dm_hw_summary_probe,
479 };
480 
481 static void net_dm_packet_trace_kfree_skb_hit(void *ignore,
482 					      struct sk_buff *skb,
483 					      void *location)
484 {
485 	ktime_t tstamp = ktime_get_real();
486 	struct per_cpu_dm_data *data;
487 	struct sk_buff *nskb;
488 	unsigned long flags;
489 
490 	nskb = skb_clone(skb, GFP_ATOMIC);
491 	if (!nskb)
492 		return;
493 
494 	NET_DM_SKB_CB(nskb)->pc = location;
495 	/* Override the timestamp because we care about the time when the
496 	 * packet was dropped.
497 	 */
498 	nskb->tstamp = tstamp;
499 
500 	data = this_cpu_ptr(&dm_cpu_data);
501 
502 	spin_lock_irqsave(&data->drop_queue.lock, flags);
503 	if (skb_queue_len(&data->drop_queue) < net_dm_queue_len)
504 		__skb_queue_tail(&data->drop_queue, nskb);
505 	else
506 		goto unlock_free;
507 	spin_unlock_irqrestore(&data->drop_queue.lock, flags);
508 
509 	schedule_work(&data->dm_alert_work);
510 
511 	return;
512 
513 unlock_free:
514 	spin_unlock_irqrestore(&data->drop_queue.lock, flags);
515 	u64_stats_update_begin(&data->stats.syncp);
516 	data->stats.dropped++;
517 	u64_stats_update_end(&data->stats.syncp);
518 	consume_skb(nskb);
519 }
520 
521 static void net_dm_packet_trace_napi_poll_hit(void *ignore,
522 					      struct napi_struct *napi,
523 					      int work, int budget)
524 {
525 }
526 
527 static size_t net_dm_in_port_size(void)
528 {
529 	       /* NET_DM_ATTR_IN_PORT nest */
530 	return nla_total_size(0) +
531 	       /* NET_DM_ATTR_PORT_NETDEV_IFINDEX */
532 	       nla_total_size(sizeof(u32)) +
533 	       /* NET_DM_ATTR_PORT_NETDEV_NAME */
534 	       nla_total_size(IFNAMSIZ + 1);
535 }
536 
537 #define NET_DM_MAX_SYMBOL_LEN 40
538 
539 static size_t net_dm_packet_report_size(size_t payload_len)
540 {
541 	size_t size;
542 
543 	size = nlmsg_msg_size(GENL_HDRLEN + net_drop_monitor_family.hdrsize);
544 
545 	return NLMSG_ALIGN(size) +
546 	       /* NET_DM_ATTR_ORIGIN */
547 	       nla_total_size(sizeof(u16)) +
548 	       /* NET_DM_ATTR_PC */
549 	       nla_total_size(sizeof(u64)) +
550 	       /* NET_DM_ATTR_SYMBOL */
551 	       nla_total_size(NET_DM_MAX_SYMBOL_LEN + 1) +
552 	       /* NET_DM_ATTR_IN_PORT */
553 	       net_dm_in_port_size() +
554 	       /* NET_DM_ATTR_TIMESTAMP */
555 	       nla_total_size(sizeof(u64)) +
556 	       /* NET_DM_ATTR_ORIG_LEN */
557 	       nla_total_size(sizeof(u32)) +
558 	       /* NET_DM_ATTR_PROTO */
559 	       nla_total_size(sizeof(u16)) +
560 	       /* NET_DM_ATTR_PAYLOAD */
561 	       nla_total_size(payload_len);
562 }
563 
564 static int net_dm_packet_report_in_port_put(struct sk_buff *msg, int ifindex,
565 					    const char *name)
566 {
567 	struct nlattr *attr;
568 
569 	attr = nla_nest_start(msg, NET_DM_ATTR_IN_PORT);
570 	if (!attr)
571 		return -EMSGSIZE;
572 
573 	if (ifindex &&
574 	    nla_put_u32(msg, NET_DM_ATTR_PORT_NETDEV_IFINDEX, ifindex))
575 		goto nla_put_failure;
576 
577 	if (name && nla_put_string(msg, NET_DM_ATTR_PORT_NETDEV_NAME, name))
578 		goto nla_put_failure;
579 
580 	nla_nest_end(msg, attr);
581 
582 	return 0;
583 
584 nla_put_failure:
585 	nla_nest_cancel(msg, attr);
586 	return -EMSGSIZE;
587 }
588 
589 static int net_dm_packet_report_fill(struct sk_buff *msg, struct sk_buff *skb,
590 				     size_t payload_len)
591 {
592 	u64 pc = (u64)(uintptr_t) NET_DM_SKB_CB(skb)->pc;
593 	char buf[NET_DM_MAX_SYMBOL_LEN];
594 	struct nlattr *attr;
595 	void *hdr;
596 	int rc;
597 
598 	hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
599 			  NET_DM_CMD_PACKET_ALERT);
600 	if (!hdr)
601 		return -EMSGSIZE;
602 
603 	if (nla_put_u16(msg, NET_DM_ATTR_ORIGIN, NET_DM_ORIGIN_SW))
604 		goto nla_put_failure;
605 
606 	if (nla_put_u64_64bit(msg, NET_DM_ATTR_PC, pc, NET_DM_ATTR_PAD))
607 		goto nla_put_failure;
608 
609 	snprintf(buf, sizeof(buf), "%pS", NET_DM_SKB_CB(skb)->pc);
610 	if (nla_put_string(msg, NET_DM_ATTR_SYMBOL, buf))
611 		goto nla_put_failure;
612 
613 	rc = net_dm_packet_report_in_port_put(msg, skb->skb_iif, NULL);
614 	if (rc)
615 		goto nla_put_failure;
616 
617 	if (nla_put_u64_64bit(msg, NET_DM_ATTR_TIMESTAMP,
618 			      ktime_to_ns(skb->tstamp), NET_DM_ATTR_PAD))
619 		goto nla_put_failure;
620 
621 	if (nla_put_u32(msg, NET_DM_ATTR_ORIG_LEN, skb->len))
622 		goto nla_put_failure;
623 
624 	if (!payload_len)
625 		goto out;
626 
627 	if (nla_put_u16(msg, NET_DM_ATTR_PROTO, be16_to_cpu(skb->protocol)))
628 		goto nla_put_failure;
629 
630 	attr = skb_put(msg, nla_total_size(payload_len));
631 	attr->nla_type = NET_DM_ATTR_PAYLOAD;
632 	attr->nla_len = nla_attr_size(payload_len);
633 	if (skb_copy_bits(skb, 0, nla_data(attr), payload_len))
634 		goto nla_put_failure;
635 
636 out:
637 	genlmsg_end(msg, hdr);
638 
639 	return 0;
640 
641 nla_put_failure:
642 	genlmsg_cancel(msg, hdr);
643 	return -EMSGSIZE;
644 }
645 
646 #define NET_DM_MAX_PACKET_SIZE (0xffff - NLA_HDRLEN - NLA_ALIGNTO)
647 
648 static void net_dm_packet_report(struct sk_buff *skb)
649 {
650 	struct sk_buff *msg;
651 	size_t payload_len;
652 	int rc;
653 
654 	/* Make sure we start copying the packet from the MAC header */
655 	if (skb->data > skb_mac_header(skb))
656 		skb_push(skb, skb->data - skb_mac_header(skb));
657 	else
658 		skb_pull(skb, skb_mac_header(skb) - skb->data);
659 
660 	/* Ensure packet fits inside a single netlink attribute */
661 	payload_len = min_t(size_t, skb->len, NET_DM_MAX_PACKET_SIZE);
662 	if (net_dm_trunc_len)
663 		payload_len = min_t(size_t, net_dm_trunc_len, payload_len);
664 
665 	msg = nlmsg_new(net_dm_packet_report_size(payload_len), GFP_KERNEL);
666 	if (!msg)
667 		goto out;
668 
669 	rc = net_dm_packet_report_fill(msg, skb, payload_len);
670 	if (rc) {
671 		nlmsg_free(msg);
672 		goto out;
673 	}
674 
675 	genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
676 
677 out:
678 	consume_skb(skb);
679 }
680 
681 static void net_dm_packet_work(struct work_struct *work)
682 {
683 	struct per_cpu_dm_data *data;
684 	struct sk_buff_head list;
685 	struct sk_buff *skb;
686 	unsigned long flags;
687 
688 	data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
689 
690 	__skb_queue_head_init(&list);
691 
692 	spin_lock_irqsave(&data->drop_queue.lock, flags);
693 	skb_queue_splice_tail_init(&data->drop_queue, &list);
694 	spin_unlock_irqrestore(&data->drop_queue.lock, flags);
695 
696 	while ((skb = __skb_dequeue(&list)))
697 		net_dm_packet_report(skb);
698 }
699 
700 static size_t
701 net_dm_hw_packet_report_size(size_t payload_len,
702 			     const struct net_dm_hw_metadata *hw_metadata)
703 {
704 	size_t size;
705 
706 	size = nlmsg_msg_size(GENL_HDRLEN + net_drop_monitor_family.hdrsize);
707 
708 	return NLMSG_ALIGN(size) +
709 	       /* NET_DM_ATTR_ORIGIN */
710 	       nla_total_size(sizeof(u16)) +
711 	       /* NET_DM_ATTR_HW_TRAP_GROUP_NAME */
712 	       nla_total_size(strlen(hw_metadata->trap_group_name) + 1) +
713 	       /* NET_DM_ATTR_HW_TRAP_NAME */
714 	       nla_total_size(strlen(hw_metadata->trap_name) + 1) +
715 	       /* NET_DM_ATTR_IN_PORT */
716 	       net_dm_in_port_size() +
717 	       /* NET_DM_ATTR_TIMESTAMP */
718 	       nla_total_size(sizeof(u64)) +
719 	       /* NET_DM_ATTR_ORIG_LEN */
720 	       nla_total_size(sizeof(u32)) +
721 	       /* NET_DM_ATTR_PROTO */
722 	       nla_total_size(sizeof(u16)) +
723 	       /* NET_DM_ATTR_PAYLOAD */
724 	       nla_total_size(payload_len);
725 }
726 
727 static int net_dm_hw_packet_report_fill(struct sk_buff *msg,
728 					struct sk_buff *skb, size_t payload_len)
729 {
730 	struct net_dm_hw_metadata *hw_metadata;
731 	struct nlattr *attr;
732 	void *hdr;
733 
734 	hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
735 
736 	hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
737 			  NET_DM_CMD_PACKET_ALERT);
738 	if (!hdr)
739 		return -EMSGSIZE;
740 
741 	if (nla_put_u16(msg, NET_DM_ATTR_ORIGIN, NET_DM_ORIGIN_HW))
742 		goto nla_put_failure;
743 
744 	if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_GROUP_NAME,
745 			   hw_metadata->trap_group_name))
746 		goto nla_put_failure;
747 
748 	if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_NAME,
749 			   hw_metadata->trap_name))
750 		goto nla_put_failure;
751 
752 	if (hw_metadata->input_dev) {
753 		struct net_device *dev = hw_metadata->input_dev;
754 		int rc;
755 
756 		rc = net_dm_packet_report_in_port_put(msg, dev->ifindex,
757 						      dev->name);
758 		if (rc)
759 			goto nla_put_failure;
760 	}
761 
762 	if (nla_put_u64_64bit(msg, NET_DM_ATTR_TIMESTAMP,
763 			      ktime_to_ns(skb->tstamp), NET_DM_ATTR_PAD))
764 		goto nla_put_failure;
765 
766 	if (nla_put_u32(msg, NET_DM_ATTR_ORIG_LEN, skb->len))
767 		goto nla_put_failure;
768 
769 	if (!payload_len)
770 		goto out;
771 
772 	if (nla_put_u16(msg, NET_DM_ATTR_PROTO, be16_to_cpu(skb->protocol)))
773 		goto nla_put_failure;
774 
775 	attr = skb_put(msg, nla_total_size(payload_len));
776 	attr->nla_type = NET_DM_ATTR_PAYLOAD;
777 	attr->nla_len = nla_attr_size(payload_len);
778 	if (skb_copy_bits(skb, 0, nla_data(attr), payload_len))
779 		goto nla_put_failure;
780 
781 out:
782 	genlmsg_end(msg, hdr);
783 
784 	return 0;
785 
786 nla_put_failure:
787 	genlmsg_cancel(msg, hdr);
788 	return -EMSGSIZE;
789 }
790 
791 static struct net_dm_hw_metadata *
792 net_dm_hw_metadata_clone(const struct net_dm_hw_metadata *hw_metadata)
793 {
794 	struct net_dm_hw_metadata *n_hw_metadata;
795 	const char *trap_group_name;
796 	const char *trap_name;
797 
798 	n_hw_metadata = kmalloc(sizeof(*hw_metadata), GFP_ATOMIC);
799 	if (!n_hw_metadata)
800 		return NULL;
801 
802 	trap_group_name = kmemdup(hw_metadata->trap_group_name,
803 				  strlen(hw_metadata->trap_group_name) + 1,
804 				  GFP_ATOMIC | __GFP_ZERO);
805 	if (!trap_group_name)
806 		goto free_hw_metadata;
807 	n_hw_metadata->trap_group_name = trap_group_name;
808 
809 	trap_name = kmemdup(hw_metadata->trap_name,
810 			    strlen(hw_metadata->trap_name) + 1,
811 			    GFP_ATOMIC | __GFP_ZERO);
812 	if (!trap_name)
813 		goto free_trap_group;
814 	n_hw_metadata->trap_name = trap_name;
815 
816 	n_hw_metadata->input_dev = hw_metadata->input_dev;
817 	if (n_hw_metadata->input_dev)
818 		dev_hold(n_hw_metadata->input_dev);
819 
820 	return n_hw_metadata;
821 
822 free_trap_group:
823 	kfree(trap_group_name);
824 free_hw_metadata:
825 	kfree(n_hw_metadata);
826 	return NULL;
827 }
828 
829 static void
830 net_dm_hw_metadata_free(const struct net_dm_hw_metadata *hw_metadata)
831 {
832 	if (hw_metadata->input_dev)
833 		dev_put(hw_metadata->input_dev);
834 	kfree(hw_metadata->trap_name);
835 	kfree(hw_metadata->trap_group_name);
836 	kfree(hw_metadata);
837 }
838 
839 static void net_dm_hw_packet_report(struct sk_buff *skb)
840 {
841 	struct net_dm_hw_metadata *hw_metadata;
842 	struct sk_buff *msg;
843 	size_t payload_len;
844 	int rc;
845 
846 	if (skb->data > skb_mac_header(skb))
847 		skb_push(skb, skb->data - skb_mac_header(skb));
848 	else
849 		skb_pull(skb, skb_mac_header(skb) - skb->data);
850 
851 	payload_len = min_t(size_t, skb->len, NET_DM_MAX_PACKET_SIZE);
852 	if (net_dm_trunc_len)
853 		payload_len = min_t(size_t, net_dm_trunc_len, payload_len);
854 
855 	hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
856 	msg = nlmsg_new(net_dm_hw_packet_report_size(payload_len, hw_metadata),
857 			GFP_KERNEL);
858 	if (!msg)
859 		goto out;
860 
861 	rc = net_dm_hw_packet_report_fill(msg, skb, payload_len);
862 	if (rc) {
863 		nlmsg_free(msg);
864 		goto out;
865 	}
866 
867 	genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
868 
869 out:
870 	net_dm_hw_metadata_free(NET_DM_SKB_CB(skb)->hw_metadata);
871 	consume_skb(skb);
872 }
873 
874 static void net_dm_hw_packet_work(struct work_struct *work)
875 {
876 	struct per_cpu_dm_data *hw_data;
877 	struct sk_buff_head list;
878 	struct sk_buff *skb;
879 	unsigned long flags;
880 
881 	hw_data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
882 
883 	__skb_queue_head_init(&list);
884 
885 	spin_lock_irqsave(&hw_data->drop_queue.lock, flags);
886 	skb_queue_splice_tail_init(&hw_data->drop_queue, &list);
887 	spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
888 
889 	while ((skb = __skb_dequeue(&list)))
890 		net_dm_hw_packet_report(skb);
891 }
892 
893 static void
894 net_dm_hw_packet_probe(struct sk_buff *skb,
895 		       const struct net_dm_hw_metadata *hw_metadata)
896 {
897 	struct net_dm_hw_metadata *n_hw_metadata;
898 	ktime_t tstamp = ktime_get_real();
899 	struct per_cpu_dm_data *hw_data;
900 	struct sk_buff *nskb;
901 	unsigned long flags;
902 
903 	nskb = skb_clone(skb, GFP_ATOMIC);
904 	if (!nskb)
905 		return;
906 
907 	n_hw_metadata = net_dm_hw_metadata_clone(hw_metadata);
908 	if (!n_hw_metadata)
909 		goto free;
910 
911 	NET_DM_SKB_CB(nskb)->hw_metadata = n_hw_metadata;
912 	nskb->tstamp = tstamp;
913 
914 	hw_data = this_cpu_ptr(&dm_hw_cpu_data);
915 
916 	spin_lock_irqsave(&hw_data->drop_queue.lock, flags);
917 	if (skb_queue_len(&hw_data->drop_queue) < net_dm_queue_len)
918 		__skb_queue_tail(&hw_data->drop_queue, nskb);
919 	else
920 		goto unlock_free;
921 	spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
922 
923 	schedule_work(&hw_data->dm_alert_work);
924 
925 	return;
926 
927 unlock_free:
928 	spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
929 	u64_stats_update_begin(&hw_data->stats.syncp);
930 	hw_data->stats.dropped++;
931 	u64_stats_update_end(&hw_data->stats.syncp);
932 	net_dm_hw_metadata_free(n_hw_metadata);
933 free:
934 	consume_skb(nskb);
935 }
936 
937 static const struct net_dm_alert_ops net_dm_alert_packet_ops = {
938 	.kfree_skb_probe	= net_dm_packet_trace_kfree_skb_hit,
939 	.napi_poll_probe	= net_dm_packet_trace_napi_poll_hit,
940 	.work_item_func		= net_dm_packet_work,
941 	.hw_work_item_func	= net_dm_hw_packet_work,
942 	.hw_probe		= net_dm_hw_packet_probe,
943 };
944 
945 static const struct net_dm_alert_ops *net_dm_alert_ops_arr[] = {
946 	[NET_DM_ALERT_MODE_SUMMARY]	= &net_dm_alert_summary_ops,
947 	[NET_DM_ALERT_MODE_PACKET]	= &net_dm_alert_packet_ops,
948 };
949 
950 void net_dm_hw_report(struct sk_buff *skb,
951 		      const struct net_dm_hw_metadata *hw_metadata)
952 {
953 	rcu_read_lock();
954 
955 	if (!monitor_hw)
956 		goto out;
957 
958 	net_dm_alert_ops_arr[net_dm_alert_mode]->hw_probe(skb, hw_metadata);
959 
960 out:
961 	rcu_read_unlock();
962 }
963 EXPORT_SYMBOL_GPL(net_dm_hw_report);
964 
965 static int net_dm_hw_monitor_start(struct netlink_ext_ack *extack)
966 {
967 	const struct net_dm_alert_ops *ops;
968 	int cpu;
969 
970 	if (monitor_hw) {
971 		NL_SET_ERR_MSG_MOD(extack, "Hardware monitoring already enabled");
972 		return -EAGAIN;
973 	}
974 
975 	ops = net_dm_alert_ops_arr[net_dm_alert_mode];
976 
977 	if (!try_module_get(THIS_MODULE)) {
978 		NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
979 		return -ENODEV;
980 	}
981 
982 	for_each_possible_cpu(cpu) {
983 		struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
984 		struct net_dm_hw_entries *hw_entries;
985 
986 		INIT_WORK(&hw_data->dm_alert_work, ops->hw_work_item_func);
987 		timer_setup(&hw_data->send_timer, sched_send_work, 0);
988 		hw_entries = net_dm_hw_reset_per_cpu_data(hw_data);
989 		kfree(hw_entries);
990 	}
991 
992 	monitor_hw = true;
993 
994 	return 0;
995 }
996 
997 static void net_dm_hw_monitor_stop(struct netlink_ext_ack *extack)
998 {
999 	int cpu;
1000 
1001 	if (!monitor_hw)
1002 		NL_SET_ERR_MSG_MOD(extack, "Hardware monitoring already disabled");
1003 
1004 	monitor_hw = false;
1005 
1006 	/* After this call returns we are guaranteed that no CPU is processing
1007 	 * any hardware drops.
1008 	 */
1009 	synchronize_rcu();
1010 
1011 	for_each_possible_cpu(cpu) {
1012 		struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1013 		struct sk_buff *skb;
1014 
1015 		del_timer_sync(&hw_data->send_timer);
1016 		cancel_work_sync(&hw_data->dm_alert_work);
1017 		while ((skb = __skb_dequeue(&hw_data->drop_queue))) {
1018 			struct net_dm_hw_metadata *hw_metadata;
1019 
1020 			hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
1021 			net_dm_hw_metadata_free(hw_metadata);
1022 			consume_skb(skb);
1023 		}
1024 	}
1025 
1026 	module_put(THIS_MODULE);
1027 }
1028 
1029 static int net_dm_trace_on_set(struct netlink_ext_ack *extack)
1030 {
1031 	const struct net_dm_alert_ops *ops;
1032 	int cpu, rc;
1033 
1034 	ops = net_dm_alert_ops_arr[net_dm_alert_mode];
1035 
1036 	if (!try_module_get(THIS_MODULE)) {
1037 		NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
1038 		return -ENODEV;
1039 	}
1040 
1041 	for_each_possible_cpu(cpu) {
1042 		struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
1043 		struct sk_buff *skb;
1044 
1045 		INIT_WORK(&data->dm_alert_work, ops->work_item_func);
1046 		timer_setup(&data->send_timer, sched_send_work, 0);
1047 		/* Allocate a new per-CPU skb for the summary alert message and
1048 		 * free the old one which might contain stale data from
1049 		 * previous tracing.
1050 		 */
1051 		skb = reset_per_cpu_data(data);
1052 		consume_skb(skb);
1053 	}
1054 
1055 	rc = register_trace_kfree_skb(ops->kfree_skb_probe, NULL);
1056 	if (rc) {
1057 		NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to kfree_skb() tracepoint");
1058 		goto err_module_put;
1059 	}
1060 
1061 	rc = register_trace_napi_poll(ops->napi_poll_probe, NULL);
1062 	if (rc) {
1063 		NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to napi_poll() tracepoint");
1064 		goto err_unregister_trace;
1065 	}
1066 
1067 	return 0;
1068 
1069 err_unregister_trace:
1070 	unregister_trace_kfree_skb(ops->kfree_skb_probe, NULL);
1071 err_module_put:
1072 	module_put(THIS_MODULE);
1073 	return rc;
1074 }
1075 
1076 static void net_dm_trace_off_set(void)
1077 {
1078 	struct dm_hw_stat_delta *new_stat, *temp;
1079 	const struct net_dm_alert_ops *ops;
1080 	int cpu;
1081 
1082 	ops = net_dm_alert_ops_arr[net_dm_alert_mode];
1083 
1084 	unregister_trace_napi_poll(ops->napi_poll_probe, NULL);
1085 	unregister_trace_kfree_skb(ops->kfree_skb_probe, NULL);
1086 
1087 	tracepoint_synchronize_unregister();
1088 
1089 	/* Make sure we do not send notifications to user space after request
1090 	 * to stop tracing returns.
1091 	 */
1092 	for_each_possible_cpu(cpu) {
1093 		struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
1094 		struct sk_buff *skb;
1095 
1096 		del_timer_sync(&data->send_timer);
1097 		cancel_work_sync(&data->dm_alert_work);
1098 		while ((skb = __skb_dequeue(&data->drop_queue)))
1099 			consume_skb(skb);
1100 	}
1101 
1102 	list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
1103 		if (new_stat->dev == NULL) {
1104 			list_del_rcu(&new_stat->list);
1105 			kfree_rcu(new_stat, rcu);
1106 		}
1107 	}
1108 
1109 	module_put(THIS_MODULE);
1110 }
1111 
1112 static int set_all_monitor_traces(int state, struct netlink_ext_ack *extack)
1113 {
1114 	int rc = 0;
1115 
1116 	if (state == trace_state) {
1117 		NL_SET_ERR_MSG_MOD(extack, "Trace state already set to requested state");
1118 		return -EAGAIN;
1119 	}
1120 
1121 	switch (state) {
1122 	case TRACE_ON:
1123 		rc = net_dm_trace_on_set(extack);
1124 		break;
1125 	case TRACE_OFF:
1126 		net_dm_trace_off_set();
1127 		break;
1128 	default:
1129 		rc = 1;
1130 		break;
1131 	}
1132 
1133 	if (!rc)
1134 		trace_state = state;
1135 	else
1136 		rc = -EINPROGRESS;
1137 
1138 	return rc;
1139 }
1140 
1141 static bool net_dm_is_monitoring(void)
1142 {
1143 	return trace_state == TRACE_ON || monitor_hw;
1144 }
1145 
1146 static int net_dm_alert_mode_get_from_info(struct genl_info *info,
1147 					   enum net_dm_alert_mode *p_alert_mode)
1148 {
1149 	u8 val;
1150 
1151 	val = nla_get_u8(info->attrs[NET_DM_ATTR_ALERT_MODE]);
1152 
1153 	switch (val) {
1154 	case NET_DM_ALERT_MODE_SUMMARY: /* fall-through */
1155 	case NET_DM_ALERT_MODE_PACKET:
1156 		*p_alert_mode = val;
1157 		break;
1158 	default:
1159 		return -EINVAL;
1160 	}
1161 
1162 	return 0;
1163 }
1164 
1165 static int net_dm_alert_mode_set(struct genl_info *info)
1166 {
1167 	struct netlink_ext_ack *extack = info->extack;
1168 	enum net_dm_alert_mode alert_mode;
1169 	int rc;
1170 
1171 	if (!info->attrs[NET_DM_ATTR_ALERT_MODE])
1172 		return 0;
1173 
1174 	rc = net_dm_alert_mode_get_from_info(info, &alert_mode);
1175 	if (rc) {
1176 		NL_SET_ERR_MSG_MOD(extack, "Invalid alert mode");
1177 		return -EINVAL;
1178 	}
1179 
1180 	net_dm_alert_mode = alert_mode;
1181 
1182 	return 0;
1183 }
1184 
1185 static void net_dm_trunc_len_set(struct genl_info *info)
1186 {
1187 	if (!info->attrs[NET_DM_ATTR_TRUNC_LEN])
1188 		return;
1189 
1190 	net_dm_trunc_len = nla_get_u32(info->attrs[NET_DM_ATTR_TRUNC_LEN]);
1191 }
1192 
1193 static void net_dm_queue_len_set(struct genl_info *info)
1194 {
1195 	if (!info->attrs[NET_DM_ATTR_QUEUE_LEN])
1196 		return;
1197 
1198 	net_dm_queue_len = nla_get_u32(info->attrs[NET_DM_ATTR_QUEUE_LEN]);
1199 }
1200 
1201 static int net_dm_cmd_config(struct sk_buff *skb,
1202 			struct genl_info *info)
1203 {
1204 	struct netlink_ext_ack *extack = info->extack;
1205 	int rc;
1206 
1207 	if (net_dm_is_monitoring()) {
1208 		NL_SET_ERR_MSG_MOD(extack, "Cannot configure drop monitor during monitoring");
1209 		return -EBUSY;
1210 	}
1211 
1212 	rc = net_dm_alert_mode_set(info);
1213 	if (rc)
1214 		return rc;
1215 
1216 	net_dm_trunc_len_set(info);
1217 
1218 	net_dm_queue_len_set(info);
1219 
1220 	return 0;
1221 }
1222 
1223 static int net_dm_monitor_start(bool set_sw, bool set_hw,
1224 				struct netlink_ext_ack *extack)
1225 {
1226 	bool sw_set = false;
1227 	int rc;
1228 
1229 	if (set_sw) {
1230 		rc = set_all_monitor_traces(TRACE_ON, extack);
1231 		if (rc)
1232 			return rc;
1233 		sw_set = true;
1234 	}
1235 
1236 	if (set_hw) {
1237 		rc = net_dm_hw_monitor_start(extack);
1238 		if (rc)
1239 			goto err_monitor_hw;
1240 	}
1241 
1242 	return 0;
1243 
1244 err_monitor_hw:
1245 	if (sw_set)
1246 		set_all_monitor_traces(TRACE_OFF, extack);
1247 	return rc;
1248 }
1249 
1250 static void net_dm_monitor_stop(bool set_sw, bool set_hw,
1251 				struct netlink_ext_ack *extack)
1252 {
1253 	if (set_hw)
1254 		net_dm_hw_monitor_stop(extack);
1255 	if (set_sw)
1256 		set_all_monitor_traces(TRACE_OFF, extack);
1257 }
1258 
1259 static int net_dm_cmd_trace(struct sk_buff *skb,
1260 			struct genl_info *info)
1261 {
1262 	bool set_sw = !!info->attrs[NET_DM_ATTR_SW_DROPS];
1263 	bool set_hw = !!info->attrs[NET_DM_ATTR_HW_DROPS];
1264 	struct netlink_ext_ack *extack = info->extack;
1265 
1266 	/* To maintain backward compatibility, we start / stop monitoring of
1267 	 * software drops if no flag is specified.
1268 	 */
1269 	if (!set_sw && !set_hw)
1270 		set_sw = true;
1271 
1272 	switch (info->genlhdr->cmd) {
1273 	case NET_DM_CMD_START:
1274 		return net_dm_monitor_start(set_sw, set_hw, extack);
1275 	case NET_DM_CMD_STOP:
1276 		net_dm_monitor_stop(set_sw, set_hw, extack);
1277 		return 0;
1278 	}
1279 
1280 	return -EOPNOTSUPP;
1281 }
1282 
1283 static int net_dm_config_fill(struct sk_buff *msg, struct genl_info *info)
1284 {
1285 	void *hdr;
1286 
1287 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1288 			  &net_drop_monitor_family, 0, NET_DM_CMD_CONFIG_NEW);
1289 	if (!hdr)
1290 		return -EMSGSIZE;
1291 
1292 	if (nla_put_u8(msg, NET_DM_ATTR_ALERT_MODE, net_dm_alert_mode))
1293 		goto nla_put_failure;
1294 
1295 	if (nla_put_u32(msg, NET_DM_ATTR_TRUNC_LEN, net_dm_trunc_len))
1296 		goto nla_put_failure;
1297 
1298 	if (nla_put_u32(msg, NET_DM_ATTR_QUEUE_LEN, net_dm_queue_len))
1299 		goto nla_put_failure;
1300 
1301 	genlmsg_end(msg, hdr);
1302 
1303 	return 0;
1304 
1305 nla_put_failure:
1306 	genlmsg_cancel(msg, hdr);
1307 	return -EMSGSIZE;
1308 }
1309 
1310 static int net_dm_cmd_config_get(struct sk_buff *skb, struct genl_info *info)
1311 {
1312 	struct sk_buff *msg;
1313 	int rc;
1314 
1315 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1316 	if (!msg)
1317 		return -ENOMEM;
1318 
1319 	rc = net_dm_config_fill(msg, info);
1320 	if (rc)
1321 		goto free_msg;
1322 
1323 	return genlmsg_reply(msg, info);
1324 
1325 free_msg:
1326 	nlmsg_free(msg);
1327 	return rc;
1328 }
1329 
1330 static void net_dm_stats_read(struct net_dm_stats *stats)
1331 {
1332 	int cpu;
1333 
1334 	memset(stats, 0, sizeof(*stats));
1335 	for_each_possible_cpu(cpu) {
1336 		struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
1337 		struct net_dm_stats *cpu_stats = &data->stats;
1338 		unsigned int start;
1339 		u64 dropped;
1340 
1341 		do {
1342 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1343 			dropped = cpu_stats->dropped;
1344 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1345 
1346 		stats->dropped += dropped;
1347 	}
1348 }
1349 
1350 static int net_dm_stats_put(struct sk_buff *msg)
1351 {
1352 	struct net_dm_stats stats;
1353 	struct nlattr *attr;
1354 
1355 	net_dm_stats_read(&stats);
1356 
1357 	attr = nla_nest_start(msg, NET_DM_ATTR_STATS);
1358 	if (!attr)
1359 		return -EMSGSIZE;
1360 
1361 	if (nla_put_u64_64bit(msg, NET_DM_ATTR_STATS_DROPPED,
1362 			      stats.dropped, NET_DM_ATTR_PAD))
1363 		goto nla_put_failure;
1364 
1365 	nla_nest_end(msg, attr);
1366 
1367 	return 0;
1368 
1369 nla_put_failure:
1370 	nla_nest_cancel(msg, attr);
1371 	return -EMSGSIZE;
1372 }
1373 
1374 static void net_dm_hw_stats_read(struct net_dm_stats *stats)
1375 {
1376 	int cpu;
1377 
1378 	memset(stats, 0, sizeof(*stats));
1379 	for_each_possible_cpu(cpu) {
1380 		struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1381 		struct net_dm_stats *cpu_stats = &hw_data->stats;
1382 		unsigned int start;
1383 		u64 dropped;
1384 
1385 		do {
1386 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1387 			dropped = cpu_stats->dropped;
1388 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1389 
1390 		stats->dropped += dropped;
1391 	}
1392 }
1393 
1394 static int net_dm_hw_stats_put(struct sk_buff *msg)
1395 {
1396 	struct net_dm_stats stats;
1397 	struct nlattr *attr;
1398 
1399 	net_dm_hw_stats_read(&stats);
1400 
1401 	attr = nla_nest_start(msg, NET_DM_ATTR_HW_STATS);
1402 	if (!attr)
1403 		return -EMSGSIZE;
1404 
1405 	if (nla_put_u64_64bit(msg, NET_DM_ATTR_STATS_DROPPED,
1406 			      stats.dropped, NET_DM_ATTR_PAD))
1407 		goto nla_put_failure;
1408 
1409 	nla_nest_end(msg, attr);
1410 
1411 	return 0;
1412 
1413 nla_put_failure:
1414 	nla_nest_cancel(msg, attr);
1415 	return -EMSGSIZE;
1416 }
1417 
1418 static int net_dm_stats_fill(struct sk_buff *msg, struct genl_info *info)
1419 {
1420 	void *hdr;
1421 	int rc;
1422 
1423 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1424 			  &net_drop_monitor_family, 0, NET_DM_CMD_STATS_NEW);
1425 	if (!hdr)
1426 		return -EMSGSIZE;
1427 
1428 	rc = net_dm_stats_put(msg);
1429 	if (rc)
1430 		goto nla_put_failure;
1431 
1432 	rc = net_dm_hw_stats_put(msg);
1433 	if (rc)
1434 		goto nla_put_failure;
1435 
1436 	genlmsg_end(msg, hdr);
1437 
1438 	return 0;
1439 
1440 nla_put_failure:
1441 	genlmsg_cancel(msg, hdr);
1442 	return -EMSGSIZE;
1443 }
1444 
1445 static int net_dm_cmd_stats_get(struct sk_buff *skb, struct genl_info *info)
1446 {
1447 	struct sk_buff *msg;
1448 	int rc;
1449 
1450 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1451 	if (!msg)
1452 		return -ENOMEM;
1453 
1454 	rc = net_dm_stats_fill(msg, info);
1455 	if (rc)
1456 		goto free_msg;
1457 
1458 	return genlmsg_reply(msg, info);
1459 
1460 free_msg:
1461 	nlmsg_free(msg);
1462 	return rc;
1463 }
1464 
1465 static int dropmon_net_event(struct notifier_block *ev_block,
1466 			     unsigned long event, void *ptr)
1467 {
1468 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1469 	struct dm_hw_stat_delta *new_stat = NULL;
1470 	struct dm_hw_stat_delta *tmp;
1471 
1472 	switch (event) {
1473 	case NETDEV_REGISTER:
1474 		new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
1475 
1476 		if (!new_stat)
1477 			goto out;
1478 
1479 		new_stat->dev = dev;
1480 		new_stat->last_rx = jiffies;
1481 		mutex_lock(&net_dm_mutex);
1482 		list_add_rcu(&new_stat->list, &hw_stats_list);
1483 		mutex_unlock(&net_dm_mutex);
1484 		break;
1485 	case NETDEV_UNREGISTER:
1486 		mutex_lock(&net_dm_mutex);
1487 		list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
1488 			if (new_stat->dev == dev) {
1489 				new_stat->dev = NULL;
1490 				if (trace_state == TRACE_OFF) {
1491 					list_del_rcu(&new_stat->list);
1492 					kfree_rcu(new_stat, rcu);
1493 					break;
1494 				}
1495 			}
1496 		}
1497 		mutex_unlock(&net_dm_mutex);
1498 		break;
1499 	}
1500 out:
1501 	return NOTIFY_DONE;
1502 }
1503 
1504 static const struct nla_policy net_dm_nl_policy[NET_DM_ATTR_MAX + 1] = {
1505 	[NET_DM_ATTR_UNSPEC] = { .strict_start_type = NET_DM_ATTR_UNSPEC + 1 },
1506 	[NET_DM_ATTR_ALERT_MODE] = { .type = NLA_U8 },
1507 	[NET_DM_ATTR_TRUNC_LEN] = { .type = NLA_U32 },
1508 	[NET_DM_ATTR_QUEUE_LEN] = { .type = NLA_U32 },
1509 	[NET_DM_ATTR_SW_DROPS]	= {. type = NLA_FLAG },
1510 	[NET_DM_ATTR_HW_DROPS]	= {. type = NLA_FLAG },
1511 };
1512 
1513 static const struct genl_ops dropmon_ops[] = {
1514 	{
1515 		.cmd = NET_DM_CMD_CONFIG,
1516 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1517 		.doit = net_dm_cmd_config,
1518 		.flags = GENL_ADMIN_PERM,
1519 	},
1520 	{
1521 		.cmd = NET_DM_CMD_START,
1522 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1523 		.doit = net_dm_cmd_trace,
1524 	},
1525 	{
1526 		.cmd = NET_DM_CMD_STOP,
1527 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1528 		.doit = net_dm_cmd_trace,
1529 	},
1530 	{
1531 		.cmd = NET_DM_CMD_CONFIG_GET,
1532 		.doit = net_dm_cmd_config_get,
1533 	},
1534 	{
1535 		.cmd = NET_DM_CMD_STATS_GET,
1536 		.doit = net_dm_cmd_stats_get,
1537 	},
1538 };
1539 
1540 static int net_dm_nl_pre_doit(const struct genl_ops *ops,
1541 			      struct sk_buff *skb, struct genl_info *info)
1542 {
1543 	mutex_lock(&net_dm_mutex);
1544 
1545 	return 0;
1546 }
1547 
1548 static void net_dm_nl_post_doit(const struct genl_ops *ops,
1549 				struct sk_buff *skb, struct genl_info *info)
1550 {
1551 	mutex_unlock(&net_dm_mutex);
1552 }
1553 
1554 static struct genl_family net_drop_monitor_family __ro_after_init = {
1555 	.hdrsize        = 0,
1556 	.name           = "NET_DM",
1557 	.version        = 2,
1558 	.maxattr	= NET_DM_ATTR_MAX,
1559 	.policy		= net_dm_nl_policy,
1560 	.pre_doit	= net_dm_nl_pre_doit,
1561 	.post_doit	= net_dm_nl_post_doit,
1562 	.module		= THIS_MODULE,
1563 	.ops		= dropmon_ops,
1564 	.n_ops		= ARRAY_SIZE(dropmon_ops),
1565 	.mcgrps		= dropmon_mcgrps,
1566 	.n_mcgrps	= ARRAY_SIZE(dropmon_mcgrps),
1567 };
1568 
1569 static struct notifier_block dropmon_net_notifier = {
1570 	.notifier_call = dropmon_net_event
1571 };
1572 
1573 static void __net_dm_cpu_data_init(struct per_cpu_dm_data *data)
1574 {
1575 	spin_lock_init(&data->lock);
1576 	skb_queue_head_init(&data->drop_queue);
1577 	u64_stats_init(&data->stats.syncp);
1578 }
1579 
1580 static void __net_dm_cpu_data_fini(struct per_cpu_dm_data *data)
1581 {
1582 	WARN_ON(!skb_queue_empty(&data->drop_queue));
1583 }
1584 
1585 static void net_dm_cpu_data_init(int cpu)
1586 {
1587 	struct per_cpu_dm_data *data;
1588 
1589 	data = &per_cpu(dm_cpu_data, cpu);
1590 	__net_dm_cpu_data_init(data);
1591 }
1592 
1593 static void net_dm_cpu_data_fini(int cpu)
1594 {
1595 	struct per_cpu_dm_data *data;
1596 
1597 	data = &per_cpu(dm_cpu_data, cpu);
1598 	/* At this point, we should have exclusive access
1599 	 * to this struct and can free the skb inside it.
1600 	 */
1601 	consume_skb(data->skb);
1602 	__net_dm_cpu_data_fini(data);
1603 }
1604 
1605 static void net_dm_hw_cpu_data_init(int cpu)
1606 {
1607 	struct per_cpu_dm_data *hw_data;
1608 
1609 	hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1610 	__net_dm_cpu_data_init(hw_data);
1611 }
1612 
1613 static void net_dm_hw_cpu_data_fini(int cpu)
1614 {
1615 	struct per_cpu_dm_data *hw_data;
1616 
1617 	hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1618 	kfree(hw_data->hw_entries);
1619 	__net_dm_cpu_data_fini(hw_data);
1620 }
1621 
1622 static int __init init_net_drop_monitor(void)
1623 {
1624 	int cpu, rc;
1625 
1626 	pr_info("Initializing network drop monitor service\n");
1627 
1628 	if (sizeof(void *) > 8) {
1629 		pr_err("Unable to store program counters on this arch, Drop monitor failed\n");
1630 		return -ENOSPC;
1631 	}
1632 
1633 	rc = genl_register_family(&net_drop_monitor_family);
1634 	if (rc) {
1635 		pr_err("Could not create drop monitor netlink family\n");
1636 		return rc;
1637 	}
1638 	WARN_ON(net_drop_monitor_family.mcgrp_offset != NET_DM_GRP_ALERT);
1639 
1640 	rc = register_netdevice_notifier(&dropmon_net_notifier);
1641 	if (rc < 0) {
1642 		pr_crit("Failed to register netdevice notifier\n");
1643 		goto out_unreg;
1644 	}
1645 
1646 	rc = 0;
1647 
1648 	for_each_possible_cpu(cpu) {
1649 		net_dm_cpu_data_init(cpu);
1650 		net_dm_hw_cpu_data_init(cpu);
1651 	}
1652 
1653 	goto out;
1654 
1655 out_unreg:
1656 	genl_unregister_family(&net_drop_monitor_family);
1657 out:
1658 	return rc;
1659 }
1660 
1661 static void exit_net_drop_monitor(void)
1662 {
1663 	int cpu;
1664 
1665 	BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier));
1666 
1667 	/*
1668 	 * Because of the module_get/put we do in the trace state change path
1669 	 * we are guarnateed not to have any current users when we get here
1670 	 */
1671 
1672 	for_each_possible_cpu(cpu) {
1673 		net_dm_hw_cpu_data_fini(cpu);
1674 		net_dm_cpu_data_fini(cpu);
1675 	}
1676 
1677 	BUG_ON(genl_unregister_family(&net_drop_monitor_family));
1678 }
1679 
1680 module_init(init_net_drop_monitor);
1681 module_exit(exit_net_drop_monitor);
1682 
1683 MODULE_LICENSE("GPL v2");
1684 MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
1685 MODULE_ALIAS_GENL_FAMILY("NET_DM");
1686