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