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