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