1 // SPDX-License-Identifier: GPL-2.0-only
3 * Monitoring code for network dropped packet alerts
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/string.h>
13 #include <linux/if_arp.h>
14 #include <linux/inetdevice.h>
15 #include <linux/inet.h>
16 #include <linux/interrupt.h>
17 #include <linux/netpoll.h>
18 #include <linux/sched.h>
19 #include <linux/delay.h>
20 #include <linux/types.h>
21 #include <linux/workqueue.h>
22 #include <linux/netlink.h>
23 #include <linux/net_dropmon.h>
24 #include <linux/percpu.h>
25 #include <linux/timer.h>
26 #include <linux/bitops.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <net/drop_monitor.h>
30 #include <net/genetlink.h>
31 #include <net/netevent.h>
33 #include <trace/events/skb.h>
34 #include <trace/events/napi.h>
36 #include <asm/unaligned.h>
42 * Globals, our netlink socket pointer
43 * and the work handle that will send up
46 static int trace_state = TRACE_OFF;
47 static bool monitor_hw;
51 * An overall lock guarding every operation coming from userspace.
52 * It also guards the global 'hw_stats_list' list.
54 static DEFINE_MUTEX(net_dm_mutex);
58 struct u64_stats_sync syncp;
61 #define NET_DM_MAX_HW_TRAP_NAME_LEN 40
63 struct net_dm_hw_entry {
64 char trap_name[NET_DM_MAX_HW_TRAP_NAME_LEN];
68 struct net_dm_hw_entries {
70 struct net_dm_hw_entry entries[0];
73 struct per_cpu_dm_data {
74 spinlock_t lock; /* Protects 'skb', 'hw_entries' and
79 struct net_dm_hw_entries *hw_entries;
81 struct sk_buff_head drop_queue;
82 struct work_struct dm_alert_work;
83 struct timer_list send_timer;
84 struct net_dm_stats stats;
87 struct dm_hw_stat_delta {
88 struct net_device *dev;
89 unsigned long last_rx;
90 struct list_head list;
92 unsigned long last_drop_val;
95 static struct genl_family net_drop_monitor_family;
97 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
98 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_hw_cpu_data);
100 static int dm_hit_limit = 64;
101 static int dm_delay = 1;
102 static unsigned long dm_hw_check_delta = 2*HZ;
103 static LIST_HEAD(hw_stats_list);
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;
109 struct net_dm_alert_ops {
110 void (*kfree_skb_probe)(void *ignore, struct sk_buff *skb,
112 void (*napi_poll_probe)(void *ignore, struct napi_struct *napi,
113 int work, int budget);
114 void (*work_item_func)(struct work_struct *work);
115 void (*hw_work_item_func)(struct work_struct *work);
116 void (*hw_probe)(struct sk_buff *skb,
117 const struct net_dm_hw_metadata *hw_metadata);
120 struct net_dm_skb_cb {
122 struct net_dm_hw_metadata *hw_metadata;
127 #define NET_DM_SKB_CB(__skb) ((struct net_dm_skb_cb *)&((__skb)->cb[0]))
129 static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
132 struct net_dm_alert_msg *msg;
138 al = sizeof(struct net_dm_alert_msg);
139 al += dm_hit_limit * sizeof(struct net_dm_drop_point);
140 al += sizeof(struct nlattr);
142 skb = genlmsg_new(al, GFP_KERNEL);
147 msg_header = genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
148 0, NET_DM_CMD_ALERT);
154 nla = nla_reserve(skb, NLA_UNSPEC,
155 sizeof(struct net_dm_alert_msg));
166 mod_timer(&data->send_timer, jiffies + HZ / 10);
168 spin_lock_irqsave(&data->lock, flags);
169 swap(data->skb, skb);
170 spin_unlock_irqrestore(&data->lock, flags);
173 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
174 struct genlmsghdr *gnlh = (struct genlmsghdr *)nlmsg_data(nlh);
176 genlmsg_end(skb, genlmsg_data(gnlh));
182 static const struct genl_multicast_group dropmon_mcgrps[] = {
183 { .name = "events", },
186 static void send_dm_alert(struct work_struct *work)
189 struct per_cpu_dm_data *data;
191 data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
193 skb = reset_per_cpu_data(data);
196 genlmsg_multicast(&net_drop_monitor_family, skb, 0,
201 * This is the timer function to delay the sending of an alert
202 * in the event that more drops will arrive during the
205 static void sched_send_work(struct timer_list *t)
207 struct per_cpu_dm_data *data = from_timer(data, t, send_timer);
209 schedule_work(&data->dm_alert_work);
212 static void trace_drop_common(struct sk_buff *skb, void *location)
214 struct net_dm_alert_msg *msg;
215 struct nlmsghdr *nlh;
218 struct sk_buff *dskb;
219 struct per_cpu_dm_data *data;
222 local_irq_save(flags);
223 data = this_cpu_ptr(&dm_cpu_data);
224 spin_lock(&data->lock);
230 nlh = (struct nlmsghdr *)dskb->data;
231 nla = genlmsg_data(nlmsg_data(nlh));
233 for (i = 0; i < msg->entries; i++) {
234 if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
235 msg->points[i].count++;
239 if (msg->entries == dm_hit_limit)
242 * We need to create a new entry
244 __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
245 nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
246 memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
247 msg->points[msg->entries].count = 1;
250 if (!timer_pending(&data->send_timer)) {
251 data->send_timer.expires = jiffies + dm_delay * HZ;
252 add_timer(&data->send_timer);
256 spin_unlock_irqrestore(&data->lock, flags);
259 static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
261 trace_drop_common(skb, location);
264 static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi,
265 int work, int budget)
267 struct dm_hw_stat_delta *new_stat;
270 * Don't check napi structures with no associated device
276 list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
278 * only add a note to our monitor buffer if:
279 * 1) this is the dev we received on
280 * 2) its after the last_rx delta
281 * 3) our rx_dropped count has gone up
283 if ((new_stat->dev == napi->dev) &&
284 (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
285 (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
286 trace_drop_common(NULL, NULL);
287 new_stat->last_drop_val = napi->dev->stats.rx_dropped;
288 new_stat->last_rx = jiffies;
295 static struct net_dm_hw_entries *
296 net_dm_hw_reset_per_cpu_data(struct per_cpu_dm_data *hw_data)
298 struct net_dm_hw_entries *hw_entries;
301 hw_entries = kzalloc(struct_size(hw_entries, entries, dm_hit_limit),
304 /* If the memory allocation failed, we try to perform another
305 * allocation in 1/10 second. Otherwise, the probe function
306 * will constantly bail out.
308 mod_timer(&hw_data->send_timer, jiffies + HZ / 10);
311 spin_lock_irqsave(&hw_data->lock, flags);
312 swap(hw_data->hw_entries, hw_entries);
313 spin_unlock_irqrestore(&hw_data->lock, flags);
318 static int net_dm_hw_entry_put(struct sk_buff *msg,
319 const struct net_dm_hw_entry *hw_entry)
323 attr = nla_nest_start(msg, NET_DM_ATTR_HW_ENTRY);
327 if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_NAME, hw_entry->trap_name))
328 goto nla_put_failure;
330 if (nla_put_u32(msg, NET_DM_ATTR_HW_TRAP_COUNT, hw_entry->count))
331 goto nla_put_failure;
333 nla_nest_end(msg, attr);
338 nla_nest_cancel(msg, attr);
342 static int net_dm_hw_entries_put(struct sk_buff *msg,
343 const struct net_dm_hw_entries *hw_entries)
348 attr = nla_nest_start(msg, NET_DM_ATTR_HW_ENTRIES);
352 for (i = 0; i < hw_entries->num_entries; i++) {
355 rc = net_dm_hw_entry_put(msg, &hw_entries->entries[i]);
357 goto nla_put_failure;
360 nla_nest_end(msg, attr);
365 nla_nest_cancel(msg, attr);
370 net_dm_hw_summary_report_fill(struct sk_buff *msg,
371 const struct net_dm_hw_entries *hw_entries)
373 struct net_dm_alert_msg anc_hdr = { 0 };
377 hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
382 /* We need to put the ancillary header in order not to break user
385 if (nla_put(msg, NLA_UNSPEC, sizeof(anc_hdr), &anc_hdr))
386 goto nla_put_failure;
388 rc = net_dm_hw_entries_put(msg, hw_entries);
390 goto nla_put_failure;
392 genlmsg_end(msg, hdr);
397 genlmsg_cancel(msg, hdr);
401 static void net_dm_hw_summary_work(struct work_struct *work)
403 struct net_dm_hw_entries *hw_entries;
404 struct per_cpu_dm_data *hw_data;
408 hw_data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
410 hw_entries = net_dm_hw_reset_per_cpu_data(hw_data);
414 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
418 rc = net_dm_hw_summary_report_fill(msg, hw_entries);
424 genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
431 net_dm_hw_summary_probe(struct sk_buff *skb,
432 const struct net_dm_hw_metadata *hw_metadata)
434 struct net_dm_hw_entries *hw_entries;
435 struct net_dm_hw_entry *hw_entry;
436 struct per_cpu_dm_data *hw_data;
440 hw_data = this_cpu_ptr(&dm_hw_cpu_data);
441 spin_lock_irqsave(&hw_data->lock, flags);
442 hw_entries = hw_data->hw_entries;
447 for (i = 0; i < hw_entries->num_entries; i++) {
448 hw_entry = &hw_entries->entries[i];
449 if (!strncmp(hw_entry->trap_name, hw_metadata->trap_name,
450 NET_DM_MAX_HW_TRAP_NAME_LEN - 1)) {
455 if (WARN_ON_ONCE(hw_entries->num_entries == dm_hit_limit))
458 hw_entry = &hw_entries->entries[hw_entries->num_entries];
459 strlcpy(hw_entry->trap_name, hw_metadata->trap_name,
460 NET_DM_MAX_HW_TRAP_NAME_LEN - 1);
462 hw_entries->num_entries++;
464 if (!timer_pending(&hw_data->send_timer)) {
465 hw_data->send_timer.expires = jiffies + dm_delay * HZ;
466 add_timer(&hw_data->send_timer);
470 spin_unlock_irqrestore(&hw_data->lock, flags);
473 static const struct net_dm_alert_ops net_dm_alert_summary_ops = {
474 .kfree_skb_probe = trace_kfree_skb_hit,
475 .napi_poll_probe = trace_napi_poll_hit,
476 .work_item_func = send_dm_alert,
477 .hw_work_item_func = net_dm_hw_summary_work,
478 .hw_probe = net_dm_hw_summary_probe,
481 static void net_dm_packet_trace_kfree_skb_hit(void *ignore,
485 ktime_t tstamp = ktime_get_real();
486 struct per_cpu_dm_data *data;
487 struct sk_buff *nskb;
490 if (!skb_mac_header_was_set(skb))
493 nskb = skb_clone(skb, GFP_ATOMIC);
497 NET_DM_SKB_CB(nskb)->pc = location;
498 /* Override the timestamp because we care about the time when the
499 * packet was dropped.
501 nskb->tstamp = tstamp;
503 data = this_cpu_ptr(&dm_cpu_data);
505 spin_lock_irqsave(&data->drop_queue.lock, flags);
506 if (skb_queue_len(&data->drop_queue) < net_dm_queue_len)
507 __skb_queue_tail(&data->drop_queue, nskb);
510 spin_unlock_irqrestore(&data->drop_queue.lock, flags);
512 schedule_work(&data->dm_alert_work);
517 spin_unlock_irqrestore(&data->drop_queue.lock, flags);
518 u64_stats_update_begin(&data->stats.syncp);
519 data->stats.dropped++;
520 u64_stats_update_end(&data->stats.syncp);
524 static void net_dm_packet_trace_napi_poll_hit(void *ignore,
525 struct napi_struct *napi,
526 int work, int budget)
530 static size_t net_dm_in_port_size(void)
532 /* NET_DM_ATTR_IN_PORT nest */
533 return nla_total_size(0) +
534 /* NET_DM_ATTR_PORT_NETDEV_IFINDEX */
535 nla_total_size(sizeof(u32)) +
536 /* NET_DM_ATTR_PORT_NETDEV_NAME */
537 nla_total_size(IFNAMSIZ + 1);
540 #define NET_DM_MAX_SYMBOL_LEN 40
542 static size_t net_dm_packet_report_size(size_t payload_len)
546 size = nlmsg_msg_size(GENL_HDRLEN + net_drop_monitor_family.hdrsize);
548 return NLMSG_ALIGN(size) +
549 /* NET_DM_ATTR_ORIGIN */
550 nla_total_size(sizeof(u16)) +
552 nla_total_size(sizeof(u64)) +
553 /* NET_DM_ATTR_SYMBOL */
554 nla_total_size(NET_DM_MAX_SYMBOL_LEN + 1) +
555 /* NET_DM_ATTR_IN_PORT */
556 net_dm_in_port_size() +
557 /* NET_DM_ATTR_TIMESTAMP */
558 nla_total_size(sizeof(u64)) +
559 /* NET_DM_ATTR_ORIG_LEN */
560 nla_total_size(sizeof(u32)) +
561 /* NET_DM_ATTR_PROTO */
562 nla_total_size(sizeof(u16)) +
563 /* NET_DM_ATTR_PAYLOAD */
564 nla_total_size(payload_len);
567 static int net_dm_packet_report_in_port_put(struct sk_buff *msg, int ifindex,
572 attr = nla_nest_start(msg, NET_DM_ATTR_IN_PORT);
577 nla_put_u32(msg, NET_DM_ATTR_PORT_NETDEV_IFINDEX, ifindex))
578 goto nla_put_failure;
580 if (name && nla_put_string(msg, NET_DM_ATTR_PORT_NETDEV_NAME, name))
581 goto nla_put_failure;
583 nla_nest_end(msg, attr);
588 nla_nest_cancel(msg, attr);
592 static int net_dm_packet_report_fill(struct sk_buff *msg, struct sk_buff *skb,
595 u64 pc = (u64)(uintptr_t) NET_DM_SKB_CB(skb)->pc;
596 char buf[NET_DM_MAX_SYMBOL_LEN];
601 hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
602 NET_DM_CMD_PACKET_ALERT);
606 if (nla_put_u16(msg, NET_DM_ATTR_ORIGIN, NET_DM_ORIGIN_SW))
607 goto nla_put_failure;
609 if (nla_put_u64_64bit(msg, NET_DM_ATTR_PC, pc, NET_DM_ATTR_PAD))
610 goto nla_put_failure;
612 snprintf(buf, sizeof(buf), "%pS", NET_DM_SKB_CB(skb)->pc);
613 if (nla_put_string(msg, NET_DM_ATTR_SYMBOL, buf))
614 goto nla_put_failure;
616 rc = net_dm_packet_report_in_port_put(msg, skb->skb_iif, NULL);
618 goto nla_put_failure;
620 if (nla_put_u64_64bit(msg, NET_DM_ATTR_TIMESTAMP,
621 ktime_to_ns(skb->tstamp), NET_DM_ATTR_PAD))
622 goto nla_put_failure;
624 if (nla_put_u32(msg, NET_DM_ATTR_ORIG_LEN, skb->len))
625 goto nla_put_failure;
630 if (nla_put_u16(msg, NET_DM_ATTR_PROTO, be16_to_cpu(skb->protocol)))
631 goto nla_put_failure;
633 attr = skb_put(msg, nla_total_size(payload_len));
634 attr->nla_type = NET_DM_ATTR_PAYLOAD;
635 attr->nla_len = nla_attr_size(payload_len);
636 if (skb_copy_bits(skb, 0, nla_data(attr), payload_len))
637 goto nla_put_failure;
640 genlmsg_end(msg, hdr);
645 genlmsg_cancel(msg, hdr);
649 #define NET_DM_MAX_PACKET_SIZE (0xffff - NLA_HDRLEN - NLA_ALIGNTO)
651 static void net_dm_packet_report(struct sk_buff *skb)
657 /* Make sure we start copying the packet from the MAC header */
658 if (skb->data > skb_mac_header(skb))
659 skb_push(skb, skb->data - skb_mac_header(skb));
661 skb_pull(skb, skb_mac_header(skb) - skb->data);
663 /* Ensure packet fits inside a single netlink attribute */
664 payload_len = min_t(size_t, skb->len, NET_DM_MAX_PACKET_SIZE);
665 if (net_dm_trunc_len)
666 payload_len = min_t(size_t, net_dm_trunc_len, payload_len);
668 msg = nlmsg_new(net_dm_packet_report_size(payload_len), GFP_KERNEL);
672 rc = net_dm_packet_report_fill(msg, skb, payload_len);
678 genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
684 static void net_dm_packet_work(struct work_struct *work)
686 struct per_cpu_dm_data *data;
687 struct sk_buff_head list;
691 data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
693 __skb_queue_head_init(&list);
695 spin_lock_irqsave(&data->drop_queue.lock, flags);
696 skb_queue_splice_tail_init(&data->drop_queue, &list);
697 spin_unlock_irqrestore(&data->drop_queue.lock, flags);
699 while ((skb = __skb_dequeue(&list)))
700 net_dm_packet_report(skb);
704 net_dm_hw_packet_report_size(size_t payload_len,
705 const struct net_dm_hw_metadata *hw_metadata)
709 size = nlmsg_msg_size(GENL_HDRLEN + net_drop_monitor_family.hdrsize);
711 return NLMSG_ALIGN(size) +
712 /* NET_DM_ATTR_ORIGIN */
713 nla_total_size(sizeof(u16)) +
714 /* NET_DM_ATTR_HW_TRAP_GROUP_NAME */
715 nla_total_size(strlen(hw_metadata->trap_group_name) + 1) +
716 /* NET_DM_ATTR_HW_TRAP_NAME */
717 nla_total_size(strlen(hw_metadata->trap_name) + 1) +
718 /* NET_DM_ATTR_IN_PORT */
719 net_dm_in_port_size() +
720 /* NET_DM_ATTR_TIMESTAMP */
721 nla_total_size(sizeof(u64)) +
722 /* NET_DM_ATTR_ORIG_LEN */
723 nla_total_size(sizeof(u32)) +
724 /* NET_DM_ATTR_PROTO */
725 nla_total_size(sizeof(u16)) +
726 /* NET_DM_ATTR_PAYLOAD */
727 nla_total_size(payload_len);
730 static int net_dm_hw_packet_report_fill(struct sk_buff *msg,
731 struct sk_buff *skb, size_t payload_len)
733 struct net_dm_hw_metadata *hw_metadata;
737 hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
739 hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
740 NET_DM_CMD_PACKET_ALERT);
744 if (nla_put_u16(msg, NET_DM_ATTR_ORIGIN, NET_DM_ORIGIN_HW))
745 goto nla_put_failure;
747 if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_GROUP_NAME,
748 hw_metadata->trap_group_name))
749 goto nla_put_failure;
751 if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_NAME,
752 hw_metadata->trap_name))
753 goto nla_put_failure;
755 if (hw_metadata->input_dev) {
756 struct net_device *dev = hw_metadata->input_dev;
759 rc = net_dm_packet_report_in_port_put(msg, dev->ifindex,
762 goto nla_put_failure;
765 if (nla_put_u64_64bit(msg, NET_DM_ATTR_TIMESTAMP,
766 ktime_to_ns(skb->tstamp), NET_DM_ATTR_PAD))
767 goto nla_put_failure;
769 if (nla_put_u32(msg, NET_DM_ATTR_ORIG_LEN, skb->len))
770 goto nla_put_failure;
775 if (nla_put_u16(msg, NET_DM_ATTR_PROTO, be16_to_cpu(skb->protocol)))
776 goto nla_put_failure;
778 attr = skb_put(msg, nla_total_size(payload_len));
779 attr->nla_type = NET_DM_ATTR_PAYLOAD;
780 attr->nla_len = nla_attr_size(payload_len);
781 if (skb_copy_bits(skb, 0, nla_data(attr), payload_len))
782 goto nla_put_failure;
785 genlmsg_end(msg, hdr);
790 genlmsg_cancel(msg, hdr);
794 static struct net_dm_hw_metadata *
795 net_dm_hw_metadata_clone(const struct net_dm_hw_metadata *hw_metadata)
797 struct net_dm_hw_metadata *n_hw_metadata;
798 const char *trap_group_name;
799 const char *trap_name;
801 n_hw_metadata = kmalloc(sizeof(*hw_metadata), GFP_ATOMIC);
805 trap_group_name = kmemdup(hw_metadata->trap_group_name,
806 strlen(hw_metadata->trap_group_name) + 1,
807 GFP_ATOMIC | __GFP_ZERO);
808 if (!trap_group_name)
809 goto free_hw_metadata;
810 n_hw_metadata->trap_group_name = trap_group_name;
812 trap_name = kmemdup(hw_metadata->trap_name,
813 strlen(hw_metadata->trap_name) + 1,
814 GFP_ATOMIC | __GFP_ZERO);
816 goto free_trap_group;
817 n_hw_metadata->trap_name = trap_name;
819 n_hw_metadata->input_dev = hw_metadata->input_dev;
820 if (n_hw_metadata->input_dev)
821 dev_hold(n_hw_metadata->input_dev);
823 return n_hw_metadata;
826 kfree(trap_group_name);
828 kfree(n_hw_metadata);
833 net_dm_hw_metadata_free(const struct net_dm_hw_metadata *hw_metadata)
835 if (hw_metadata->input_dev)
836 dev_put(hw_metadata->input_dev);
837 kfree(hw_metadata->trap_name);
838 kfree(hw_metadata->trap_group_name);
842 static void net_dm_hw_packet_report(struct sk_buff *skb)
844 struct net_dm_hw_metadata *hw_metadata;
849 if (skb->data > skb_mac_header(skb))
850 skb_push(skb, skb->data - skb_mac_header(skb));
852 skb_pull(skb, skb_mac_header(skb) - skb->data);
854 payload_len = min_t(size_t, skb->len, NET_DM_MAX_PACKET_SIZE);
855 if (net_dm_trunc_len)
856 payload_len = min_t(size_t, net_dm_trunc_len, payload_len);
858 hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
859 msg = nlmsg_new(net_dm_hw_packet_report_size(payload_len, hw_metadata),
864 rc = net_dm_hw_packet_report_fill(msg, skb, payload_len);
870 genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
873 net_dm_hw_metadata_free(NET_DM_SKB_CB(skb)->hw_metadata);
877 static void net_dm_hw_packet_work(struct work_struct *work)
879 struct per_cpu_dm_data *hw_data;
880 struct sk_buff_head list;
884 hw_data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
886 __skb_queue_head_init(&list);
888 spin_lock_irqsave(&hw_data->drop_queue.lock, flags);
889 skb_queue_splice_tail_init(&hw_data->drop_queue, &list);
890 spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
892 while ((skb = __skb_dequeue(&list)))
893 net_dm_hw_packet_report(skb);
897 net_dm_hw_packet_probe(struct sk_buff *skb,
898 const struct net_dm_hw_metadata *hw_metadata)
900 struct net_dm_hw_metadata *n_hw_metadata;
901 ktime_t tstamp = ktime_get_real();
902 struct per_cpu_dm_data *hw_data;
903 struct sk_buff *nskb;
906 if (!skb_mac_header_was_set(skb))
909 nskb = skb_clone(skb, GFP_ATOMIC);
913 n_hw_metadata = net_dm_hw_metadata_clone(hw_metadata);
917 NET_DM_SKB_CB(nskb)->hw_metadata = n_hw_metadata;
918 nskb->tstamp = tstamp;
920 hw_data = this_cpu_ptr(&dm_hw_cpu_data);
922 spin_lock_irqsave(&hw_data->drop_queue.lock, flags);
923 if (skb_queue_len(&hw_data->drop_queue) < net_dm_queue_len)
924 __skb_queue_tail(&hw_data->drop_queue, nskb);
927 spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
929 schedule_work(&hw_data->dm_alert_work);
934 spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
935 u64_stats_update_begin(&hw_data->stats.syncp);
936 hw_data->stats.dropped++;
937 u64_stats_update_end(&hw_data->stats.syncp);
938 net_dm_hw_metadata_free(n_hw_metadata);
943 static const struct net_dm_alert_ops net_dm_alert_packet_ops = {
944 .kfree_skb_probe = net_dm_packet_trace_kfree_skb_hit,
945 .napi_poll_probe = net_dm_packet_trace_napi_poll_hit,
946 .work_item_func = net_dm_packet_work,
947 .hw_work_item_func = net_dm_hw_packet_work,
948 .hw_probe = net_dm_hw_packet_probe,
951 static const struct net_dm_alert_ops *net_dm_alert_ops_arr[] = {
952 [NET_DM_ALERT_MODE_SUMMARY] = &net_dm_alert_summary_ops,
953 [NET_DM_ALERT_MODE_PACKET] = &net_dm_alert_packet_ops,
956 void net_dm_hw_report(struct sk_buff *skb,
957 const struct net_dm_hw_metadata *hw_metadata)
964 net_dm_alert_ops_arr[net_dm_alert_mode]->hw_probe(skb, hw_metadata);
969 EXPORT_SYMBOL_GPL(net_dm_hw_report);
971 static int net_dm_hw_monitor_start(struct netlink_ext_ack *extack)
973 const struct net_dm_alert_ops *ops;
977 NL_SET_ERR_MSG_MOD(extack, "Hardware monitoring already enabled");
981 ops = net_dm_alert_ops_arr[net_dm_alert_mode];
983 if (!try_module_get(THIS_MODULE)) {
984 NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
988 for_each_possible_cpu(cpu) {
989 struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
990 struct net_dm_hw_entries *hw_entries;
992 INIT_WORK(&hw_data->dm_alert_work, ops->hw_work_item_func);
993 timer_setup(&hw_data->send_timer, sched_send_work, 0);
994 hw_entries = net_dm_hw_reset_per_cpu_data(hw_data);
1003 static void net_dm_hw_monitor_stop(struct netlink_ext_ack *extack)
1008 NL_SET_ERR_MSG_MOD(extack, "Hardware monitoring already disabled");
1012 /* After this call returns we are guaranteed that no CPU is processing
1013 * any hardware drops.
1017 for_each_possible_cpu(cpu) {
1018 struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1019 struct sk_buff *skb;
1021 del_timer_sync(&hw_data->send_timer);
1022 cancel_work_sync(&hw_data->dm_alert_work);
1023 while ((skb = __skb_dequeue(&hw_data->drop_queue))) {
1024 struct net_dm_hw_metadata *hw_metadata;
1026 hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
1027 net_dm_hw_metadata_free(hw_metadata);
1032 module_put(THIS_MODULE);
1035 static int net_dm_trace_on_set(struct netlink_ext_ack *extack)
1037 const struct net_dm_alert_ops *ops;
1040 ops = net_dm_alert_ops_arr[net_dm_alert_mode];
1042 if (!try_module_get(THIS_MODULE)) {
1043 NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
1047 for_each_possible_cpu(cpu) {
1048 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
1049 struct sk_buff *skb;
1051 INIT_WORK(&data->dm_alert_work, ops->work_item_func);
1052 timer_setup(&data->send_timer, sched_send_work, 0);
1053 /* Allocate a new per-CPU skb for the summary alert message and
1054 * free the old one which might contain stale data from
1057 skb = reset_per_cpu_data(data);
1061 rc = register_trace_kfree_skb(ops->kfree_skb_probe, NULL);
1063 NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to kfree_skb() tracepoint");
1064 goto err_module_put;
1067 rc = register_trace_napi_poll(ops->napi_poll_probe, NULL);
1069 NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to napi_poll() tracepoint");
1070 goto err_unregister_trace;
1075 err_unregister_trace:
1076 unregister_trace_kfree_skb(ops->kfree_skb_probe, NULL);
1078 module_put(THIS_MODULE);
1082 static void net_dm_trace_off_set(void)
1084 struct dm_hw_stat_delta *new_stat, *temp;
1085 const struct net_dm_alert_ops *ops;
1088 ops = net_dm_alert_ops_arr[net_dm_alert_mode];
1090 unregister_trace_napi_poll(ops->napi_poll_probe, NULL);
1091 unregister_trace_kfree_skb(ops->kfree_skb_probe, NULL);
1093 tracepoint_synchronize_unregister();
1095 /* Make sure we do not send notifications to user space after request
1096 * to stop tracing returns.
1098 for_each_possible_cpu(cpu) {
1099 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
1100 struct sk_buff *skb;
1102 del_timer_sync(&data->send_timer);
1103 cancel_work_sync(&data->dm_alert_work);
1104 while ((skb = __skb_dequeue(&data->drop_queue)))
1108 list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
1109 if (new_stat->dev == NULL) {
1110 list_del_rcu(&new_stat->list);
1111 kfree_rcu(new_stat, rcu);
1115 module_put(THIS_MODULE);
1118 static int set_all_monitor_traces(int state, struct netlink_ext_ack *extack)
1122 if (state == trace_state) {
1123 NL_SET_ERR_MSG_MOD(extack, "Trace state already set to requested state");
1129 rc = net_dm_trace_on_set(extack);
1132 net_dm_trace_off_set();
1140 trace_state = state;
1147 static bool net_dm_is_monitoring(void)
1149 return trace_state == TRACE_ON || monitor_hw;
1152 static int net_dm_alert_mode_get_from_info(struct genl_info *info,
1153 enum net_dm_alert_mode *p_alert_mode)
1157 val = nla_get_u8(info->attrs[NET_DM_ATTR_ALERT_MODE]);
1160 case NET_DM_ALERT_MODE_SUMMARY: /* fall-through */
1161 case NET_DM_ALERT_MODE_PACKET:
1162 *p_alert_mode = val;
1171 static int net_dm_alert_mode_set(struct genl_info *info)
1173 struct netlink_ext_ack *extack = info->extack;
1174 enum net_dm_alert_mode alert_mode;
1177 if (!info->attrs[NET_DM_ATTR_ALERT_MODE])
1180 rc = net_dm_alert_mode_get_from_info(info, &alert_mode);
1182 NL_SET_ERR_MSG_MOD(extack, "Invalid alert mode");
1186 net_dm_alert_mode = alert_mode;
1191 static void net_dm_trunc_len_set(struct genl_info *info)
1193 if (!info->attrs[NET_DM_ATTR_TRUNC_LEN])
1196 net_dm_trunc_len = nla_get_u32(info->attrs[NET_DM_ATTR_TRUNC_LEN]);
1199 static void net_dm_queue_len_set(struct genl_info *info)
1201 if (!info->attrs[NET_DM_ATTR_QUEUE_LEN])
1204 net_dm_queue_len = nla_get_u32(info->attrs[NET_DM_ATTR_QUEUE_LEN]);
1207 static int net_dm_cmd_config(struct sk_buff *skb,
1208 struct genl_info *info)
1210 struct netlink_ext_ack *extack = info->extack;
1213 if (net_dm_is_monitoring()) {
1214 NL_SET_ERR_MSG_MOD(extack, "Cannot configure drop monitor during monitoring");
1218 rc = net_dm_alert_mode_set(info);
1222 net_dm_trunc_len_set(info);
1224 net_dm_queue_len_set(info);
1229 static int net_dm_monitor_start(bool set_sw, bool set_hw,
1230 struct netlink_ext_ack *extack)
1232 bool sw_set = false;
1236 rc = set_all_monitor_traces(TRACE_ON, extack);
1243 rc = net_dm_hw_monitor_start(extack);
1245 goto err_monitor_hw;
1252 set_all_monitor_traces(TRACE_OFF, extack);
1256 static void net_dm_monitor_stop(bool set_sw, bool set_hw,
1257 struct netlink_ext_ack *extack)
1260 net_dm_hw_monitor_stop(extack);
1262 set_all_monitor_traces(TRACE_OFF, extack);
1265 static int net_dm_cmd_trace(struct sk_buff *skb,
1266 struct genl_info *info)
1268 bool set_sw = !!info->attrs[NET_DM_ATTR_SW_DROPS];
1269 bool set_hw = !!info->attrs[NET_DM_ATTR_HW_DROPS];
1270 struct netlink_ext_ack *extack = info->extack;
1272 /* To maintain backward compatibility, we start / stop monitoring of
1273 * software drops if no flag is specified.
1275 if (!set_sw && !set_hw)
1278 switch (info->genlhdr->cmd) {
1279 case NET_DM_CMD_START:
1280 return net_dm_monitor_start(set_sw, set_hw, extack);
1281 case NET_DM_CMD_STOP:
1282 net_dm_monitor_stop(set_sw, set_hw, extack);
1289 static int net_dm_config_fill(struct sk_buff *msg, struct genl_info *info)
1293 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1294 &net_drop_monitor_family, 0, NET_DM_CMD_CONFIG_NEW);
1298 if (nla_put_u8(msg, NET_DM_ATTR_ALERT_MODE, net_dm_alert_mode))
1299 goto nla_put_failure;
1301 if (nla_put_u32(msg, NET_DM_ATTR_TRUNC_LEN, net_dm_trunc_len))
1302 goto nla_put_failure;
1304 if (nla_put_u32(msg, NET_DM_ATTR_QUEUE_LEN, net_dm_queue_len))
1305 goto nla_put_failure;
1307 genlmsg_end(msg, hdr);
1312 genlmsg_cancel(msg, hdr);
1316 static int net_dm_cmd_config_get(struct sk_buff *skb, struct genl_info *info)
1318 struct sk_buff *msg;
1321 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1325 rc = net_dm_config_fill(msg, info);
1329 return genlmsg_reply(msg, info);
1336 static void net_dm_stats_read(struct net_dm_stats *stats)
1340 memset(stats, 0, sizeof(*stats));
1341 for_each_possible_cpu(cpu) {
1342 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
1343 struct net_dm_stats *cpu_stats = &data->stats;
1348 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1349 dropped = cpu_stats->dropped;
1350 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1352 stats->dropped += dropped;
1356 static int net_dm_stats_put(struct sk_buff *msg)
1358 struct net_dm_stats stats;
1359 struct nlattr *attr;
1361 net_dm_stats_read(&stats);
1363 attr = nla_nest_start(msg, NET_DM_ATTR_STATS);
1367 if (nla_put_u64_64bit(msg, NET_DM_ATTR_STATS_DROPPED,
1368 stats.dropped, NET_DM_ATTR_PAD))
1369 goto nla_put_failure;
1371 nla_nest_end(msg, attr);
1376 nla_nest_cancel(msg, attr);
1380 static void net_dm_hw_stats_read(struct net_dm_stats *stats)
1384 memset(stats, 0, sizeof(*stats));
1385 for_each_possible_cpu(cpu) {
1386 struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1387 struct net_dm_stats *cpu_stats = &hw_data->stats;
1392 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1393 dropped = cpu_stats->dropped;
1394 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1396 stats->dropped += dropped;
1400 static int net_dm_hw_stats_put(struct sk_buff *msg)
1402 struct net_dm_stats stats;
1403 struct nlattr *attr;
1405 net_dm_hw_stats_read(&stats);
1407 attr = nla_nest_start(msg, NET_DM_ATTR_HW_STATS);
1411 if (nla_put_u64_64bit(msg, NET_DM_ATTR_STATS_DROPPED,
1412 stats.dropped, NET_DM_ATTR_PAD))
1413 goto nla_put_failure;
1415 nla_nest_end(msg, attr);
1420 nla_nest_cancel(msg, attr);
1424 static int net_dm_stats_fill(struct sk_buff *msg, struct genl_info *info)
1429 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1430 &net_drop_monitor_family, 0, NET_DM_CMD_STATS_NEW);
1434 rc = net_dm_stats_put(msg);
1436 goto nla_put_failure;
1438 rc = net_dm_hw_stats_put(msg);
1440 goto nla_put_failure;
1442 genlmsg_end(msg, hdr);
1447 genlmsg_cancel(msg, hdr);
1451 static int net_dm_cmd_stats_get(struct sk_buff *skb, struct genl_info *info)
1453 struct sk_buff *msg;
1456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1460 rc = net_dm_stats_fill(msg, info);
1464 return genlmsg_reply(msg, info);
1471 static int dropmon_net_event(struct notifier_block *ev_block,
1472 unsigned long event, void *ptr)
1474 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1475 struct dm_hw_stat_delta *new_stat = NULL;
1476 struct dm_hw_stat_delta *tmp;
1479 case NETDEV_REGISTER:
1480 new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
1485 new_stat->dev = dev;
1486 new_stat->last_rx = jiffies;
1487 mutex_lock(&net_dm_mutex);
1488 list_add_rcu(&new_stat->list, &hw_stats_list);
1489 mutex_unlock(&net_dm_mutex);
1491 case NETDEV_UNREGISTER:
1492 mutex_lock(&net_dm_mutex);
1493 list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
1494 if (new_stat->dev == dev) {
1495 new_stat->dev = NULL;
1496 if (trace_state == TRACE_OFF) {
1497 list_del_rcu(&new_stat->list);
1498 kfree_rcu(new_stat, rcu);
1503 mutex_unlock(&net_dm_mutex);
1510 static const struct nla_policy net_dm_nl_policy[NET_DM_ATTR_MAX + 1] = {
1511 [NET_DM_ATTR_UNSPEC] = { .strict_start_type = NET_DM_ATTR_UNSPEC + 1 },
1512 [NET_DM_ATTR_ALERT_MODE] = { .type = NLA_U8 },
1513 [NET_DM_ATTR_TRUNC_LEN] = { .type = NLA_U32 },
1514 [NET_DM_ATTR_QUEUE_LEN] = { .type = NLA_U32 },
1515 [NET_DM_ATTR_SW_DROPS] = {. type = NLA_FLAG },
1516 [NET_DM_ATTR_HW_DROPS] = {. type = NLA_FLAG },
1519 static const struct genl_ops dropmon_ops[] = {
1521 .cmd = NET_DM_CMD_CONFIG,
1522 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1523 .doit = net_dm_cmd_config,
1524 .flags = GENL_ADMIN_PERM,
1527 .cmd = NET_DM_CMD_START,
1528 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1529 .doit = net_dm_cmd_trace,
1532 .cmd = NET_DM_CMD_STOP,
1533 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1534 .doit = net_dm_cmd_trace,
1537 .cmd = NET_DM_CMD_CONFIG_GET,
1538 .doit = net_dm_cmd_config_get,
1541 .cmd = NET_DM_CMD_STATS_GET,
1542 .doit = net_dm_cmd_stats_get,
1546 static int net_dm_nl_pre_doit(const struct genl_ops *ops,
1547 struct sk_buff *skb, struct genl_info *info)
1549 mutex_lock(&net_dm_mutex);
1554 static void net_dm_nl_post_doit(const struct genl_ops *ops,
1555 struct sk_buff *skb, struct genl_info *info)
1557 mutex_unlock(&net_dm_mutex);
1560 static struct genl_family net_drop_monitor_family __ro_after_init = {
1564 .maxattr = NET_DM_ATTR_MAX,
1565 .policy = net_dm_nl_policy,
1566 .pre_doit = net_dm_nl_pre_doit,
1567 .post_doit = net_dm_nl_post_doit,
1568 .module = THIS_MODULE,
1570 .n_ops = ARRAY_SIZE(dropmon_ops),
1571 .mcgrps = dropmon_mcgrps,
1572 .n_mcgrps = ARRAY_SIZE(dropmon_mcgrps),
1575 static struct notifier_block dropmon_net_notifier = {
1576 .notifier_call = dropmon_net_event
1579 static void __net_dm_cpu_data_init(struct per_cpu_dm_data *data)
1581 spin_lock_init(&data->lock);
1582 skb_queue_head_init(&data->drop_queue);
1583 u64_stats_init(&data->stats.syncp);
1586 static void __net_dm_cpu_data_fini(struct per_cpu_dm_data *data)
1588 WARN_ON(!skb_queue_empty(&data->drop_queue));
1591 static void net_dm_cpu_data_init(int cpu)
1593 struct per_cpu_dm_data *data;
1595 data = &per_cpu(dm_cpu_data, cpu);
1596 __net_dm_cpu_data_init(data);
1599 static void net_dm_cpu_data_fini(int cpu)
1601 struct per_cpu_dm_data *data;
1603 data = &per_cpu(dm_cpu_data, cpu);
1604 /* At this point, we should have exclusive access
1605 * to this struct and can free the skb inside it.
1607 consume_skb(data->skb);
1608 __net_dm_cpu_data_fini(data);
1611 static void net_dm_hw_cpu_data_init(int cpu)
1613 struct per_cpu_dm_data *hw_data;
1615 hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1616 __net_dm_cpu_data_init(hw_data);
1619 static void net_dm_hw_cpu_data_fini(int cpu)
1621 struct per_cpu_dm_data *hw_data;
1623 hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1624 kfree(hw_data->hw_entries);
1625 __net_dm_cpu_data_fini(hw_data);
1628 static int __init init_net_drop_monitor(void)
1632 pr_info("Initializing network drop monitor service\n");
1634 if (sizeof(void *) > 8) {
1635 pr_err("Unable to store program counters on this arch, Drop monitor failed\n");
1639 rc = genl_register_family(&net_drop_monitor_family);
1641 pr_err("Could not create drop monitor netlink family\n");
1644 WARN_ON(net_drop_monitor_family.mcgrp_offset != NET_DM_GRP_ALERT);
1646 rc = register_netdevice_notifier(&dropmon_net_notifier);
1648 pr_crit("Failed to register netdevice notifier\n");
1654 for_each_possible_cpu(cpu) {
1655 net_dm_cpu_data_init(cpu);
1656 net_dm_hw_cpu_data_init(cpu);
1662 genl_unregister_family(&net_drop_monitor_family);
1667 static void exit_net_drop_monitor(void)
1671 BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier));
1674 * Because of the module_get/put we do in the trace state change path
1675 * we are guarnateed not to have any current users when we get here
1678 for_each_possible_cpu(cpu) {
1679 net_dm_hw_cpu_data_fini(cpu);
1680 net_dm_cpu_data_fini(cpu);
1683 BUG_ON(genl_unregister_family(&net_drop_monitor_family));
1686 module_init(init_net_drop_monitor);
1687 module_exit(exit_net_drop_monitor);
1689 MODULE_LICENSE("GPL v2");
1691 MODULE_ALIAS_GENL_FAMILY("NET_DM");