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