1 // SPDX-License-Identifier: GPL-2.0-only
3 * Monitoring code for network dropped packet alerts
5 * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
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 struct per_cpu_dm_data {
62 spinlock_t lock; /* Protects 'skb' and 'send_timer' */
64 struct sk_buff_head drop_queue;
65 struct work_struct dm_alert_work;
66 struct timer_list send_timer;
67 struct net_dm_stats stats;
70 struct dm_hw_stat_delta {
71 struct net_device *dev;
72 unsigned long last_rx;
73 struct list_head list;
75 unsigned long last_drop_val;
78 static struct genl_family net_drop_monitor_family;
80 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
81 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_hw_cpu_data);
83 static int dm_hit_limit = 64;
84 static int dm_delay = 1;
85 static unsigned long dm_hw_check_delta = 2*HZ;
86 static LIST_HEAD(hw_stats_list);
88 static enum net_dm_alert_mode net_dm_alert_mode = NET_DM_ALERT_MODE_SUMMARY;
89 static u32 net_dm_trunc_len;
90 static u32 net_dm_queue_len = 1000;
92 struct net_dm_alert_ops {
93 void (*kfree_skb_probe)(void *ignore, struct sk_buff *skb,
95 void (*napi_poll_probe)(void *ignore, struct napi_struct *napi,
96 int work, int budget);
97 void (*work_item_func)(struct work_struct *work);
98 void (*hw_work_item_func)(struct work_struct *work);
99 void (*hw_probe)(struct sk_buff *skb,
100 const struct net_dm_hw_metadata *hw_metadata);
103 struct net_dm_skb_cb {
105 struct net_dm_hw_metadata *hw_metadata;
110 #define NET_DM_SKB_CB(__skb) ((struct net_dm_skb_cb *)&((__skb)->cb[0]))
112 static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
115 struct net_dm_alert_msg *msg;
121 al = sizeof(struct net_dm_alert_msg);
122 al += dm_hit_limit * sizeof(struct net_dm_drop_point);
123 al += sizeof(struct nlattr);
125 skb = genlmsg_new(al, GFP_KERNEL);
130 msg_header = genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
131 0, NET_DM_CMD_ALERT);
137 nla = nla_reserve(skb, NLA_UNSPEC,
138 sizeof(struct net_dm_alert_msg));
149 mod_timer(&data->send_timer, jiffies + HZ / 10);
151 spin_lock_irqsave(&data->lock, flags);
152 swap(data->skb, skb);
153 spin_unlock_irqrestore(&data->lock, flags);
156 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
157 struct genlmsghdr *gnlh = (struct genlmsghdr *)nlmsg_data(nlh);
159 genlmsg_end(skb, genlmsg_data(gnlh));
165 static const struct genl_multicast_group dropmon_mcgrps[] = {
166 { .name = "events", },
169 static void send_dm_alert(struct work_struct *work)
172 struct per_cpu_dm_data *data;
174 data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
176 skb = reset_per_cpu_data(data);
179 genlmsg_multicast(&net_drop_monitor_family, skb, 0,
184 * This is the timer function to delay the sending of an alert
185 * in the event that more drops will arrive during the
188 static void sched_send_work(struct timer_list *t)
190 struct per_cpu_dm_data *data = from_timer(data, t, send_timer);
192 schedule_work(&data->dm_alert_work);
195 static void trace_drop_common(struct sk_buff *skb, void *location)
197 struct net_dm_alert_msg *msg;
198 struct nlmsghdr *nlh;
201 struct sk_buff *dskb;
202 struct per_cpu_dm_data *data;
205 local_irq_save(flags);
206 data = this_cpu_ptr(&dm_cpu_data);
207 spin_lock(&data->lock);
213 nlh = (struct nlmsghdr *)dskb->data;
214 nla = genlmsg_data(nlmsg_data(nlh));
216 for (i = 0; i < msg->entries; i++) {
217 if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
218 msg->points[i].count++;
222 if (msg->entries == dm_hit_limit)
225 * We need to create a new entry
227 __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
228 nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
229 memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
230 msg->points[msg->entries].count = 1;
233 if (!timer_pending(&data->send_timer)) {
234 data->send_timer.expires = jiffies + dm_delay * HZ;
235 add_timer(&data->send_timer);
239 spin_unlock_irqrestore(&data->lock, flags);
242 static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
244 trace_drop_common(skb, location);
247 static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi,
248 int work, int budget)
250 struct dm_hw_stat_delta *new_stat;
253 * Don't check napi structures with no associated device
259 list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
261 * only add a note to our monitor buffer if:
262 * 1) this is the dev we received on
263 * 2) its after the last_rx delta
264 * 3) our rx_dropped count has gone up
266 if ((new_stat->dev == napi->dev) &&
267 (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
268 (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
269 trace_drop_common(NULL, NULL);
270 new_stat->last_drop_val = napi->dev->stats.rx_dropped;
271 new_stat->last_rx = jiffies;
279 net_dm_hw_summary_probe(struct sk_buff *skb,
280 const struct net_dm_hw_metadata *hw_metadata)
284 static const struct net_dm_alert_ops net_dm_alert_summary_ops = {
285 .kfree_skb_probe = trace_kfree_skb_hit,
286 .napi_poll_probe = trace_napi_poll_hit,
287 .work_item_func = send_dm_alert,
288 .hw_probe = net_dm_hw_summary_probe,
291 static void net_dm_packet_trace_kfree_skb_hit(void *ignore,
295 ktime_t tstamp = ktime_get_real();
296 struct per_cpu_dm_data *data;
297 struct sk_buff *nskb;
300 nskb = skb_clone(skb, GFP_ATOMIC);
304 NET_DM_SKB_CB(nskb)->pc = location;
305 /* Override the timestamp because we care about the time when the
306 * packet was dropped.
308 nskb->tstamp = tstamp;
310 data = this_cpu_ptr(&dm_cpu_data);
312 spin_lock_irqsave(&data->drop_queue.lock, flags);
313 if (skb_queue_len(&data->drop_queue) < net_dm_queue_len)
314 __skb_queue_tail(&data->drop_queue, nskb);
317 spin_unlock_irqrestore(&data->drop_queue.lock, flags);
319 schedule_work(&data->dm_alert_work);
324 spin_unlock_irqrestore(&data->drop_queue.lock, flags);
325 u64_stats_update_begin(&data->stats.syncp);
326 data->stats.dropped++;
327 u64_stats_update_end(&data->stats.syncp);
331 static void net_dm_packet_trace_napi_poll_hit(void *ignore,
332 struct napi_struct *napi,
333 int work, int budget)
337 static size_t net_dm_in_port_size(void)
339 /* NET_DM_ATTR_IN_PORT nest */
340 return nla_total_size(0) +
341 /* NET_DM_ATTR_PORT_NETDEV_IFINDEX */
342 nla_total_size(sizeof(u32)) +
343 /* NET_DM_ATTR_PORT_NETDEV_NAME */
344 nla_total_size(IFNAMSIZ + 1);
347 #define NET_DM_MAX_SYMBOL_LEN 40
349 static size_t net_dm_packet_report_size(size_t payload_len)
353 size = nlmsg_msg_size(GENL_HDRLEN + net_drop_monitor_family.hdrsize);
355 return NLMSG_ALIGN(size) +
356 /* NET_DM_ATTR_ORIGIN */
357 nla_total_size(sizeof(u16)) +
359 nla_total_size(sizeof(u64)) +
360 /* NET_DM_ATTR_SYMBOL */
361 nla_total_size(NET_DM_MAX_SYMBOL_LEN + 1) +
362 /* NET_DM_ATTR_IN_PORT */
363 net_dm_in_port_size() +
364 /* NET_DM_ATTR_TIMESTAMP */
365 nla_total_size(sizeof(struct timespec)) +
366 /* NET_DM_ATTR_ORIG_LEN */
367 nla_total_size(sizeof(u32)) +
368 /* NET_DM_ATTR_PROTO */
369 nla_total_size(sizeof(u16)) +
370 /* NET_DM_ATTR_PAYLOAD */
371 nla_total_size(payload_len);
374 static int net_dm_packet_report_in_port_put(struct sk_buff *msg, int ifindex,
379 attr = nla_nest_start(msg, NET_DM_ATTR_IN_PORT);
384 nla_put_u32(msg, NET_DM_ATTR_PORT_NETDEV_IFINDEX, ifindex))
385 goto nla_put_failure;
387 if (name && nla_put_string(msg, NET_DM_ATTR_PORT_NETDEV_NAME, name))
388 goto nla_put_failure;
390 nla_nest_end(msg, attr);
395 nla_nest_cancel(msg, attr);
399 static int net_dm_packet_report_fill(struct sk_buff *msg, struct sk_buff *skb,
402 u64 pc = (u64)(uintptr_t) NET_DM_SKB_CB(skb)->pc;
403 char buf[NET_DM_MAX_SYMBOL_LEN];
409 hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
410 NET_DM_CMD_PACKET_ALERT);
414 if (nla_put_u16(msg, NET_DM_ATTR_ORIGIN, NET_DM_ORIGIN_SW))
415 goto nla_put_failure;
417 if (nla_put_u64_64bit(msg, NET_DM_ATTR_PC, pc, NET_DM_ATTR_PAD))
418 goto nla_put_failure;
420 snprintf(buf, sizeof(buf), "%pS", NET_DM_SKB_CB(skb)->pc);
421 if (nla_put_string(msg, NET_DM_ATTR_SYMBOL, buf))
422 goto nla_put_failure;
424 rc = net_dm_packet_report_in_port_put(msg, skb->skb_iif, NULL);
426 goto nla_put_failure;
428 if (ktime_to_timespec_cond(skb->tstamp, &ts) &&
429 nla_put(msg, NET_DM_ATTR_TIMESTAMP, sizeof(ts), &ts))
430 goto nla_put_failure;
432 if (nla_put_u32(msg, NET_DM_ATTR_ORIG_LEN, skb->len))
433 goto nla_put_failure;
438 if (nla_put_u16(msg, NET_DM_ATTR_PROTO, be16_to_cpu(skb->protocol)))
439 goto nla_put_failure;
441 attr = skb_put(msg, nla_total_size(payload_len));
442 attr->nla_type = NET_DM_ATTR_PAYLOAD;
443 attr->nla_len = nla_attr_size(payload_len);
444 if (skb_copy_bits(skb, 0, nla_data(attr), payload_len))
445 goto nla_put_failure;
448 genlmsg_end(msg, hdr);
453 genlmsg_cancel(msg, hdr);
457 #define NET_DM_MAX_PACKET_SIZE (0xffff - NLA_HDRLEN - NLA_ALIGNTO)
459 static void net_dm_packet_report(struct sk_buff *skb)
465 /* Make sure we start copying the packet from the MAC header */
466 if (skb->data > skb_mac_header(skb))
467 skb_push(skb, skb->data - skb_mac_header(skb));
469 skb_pull(skb, skb_mac_header(skb) - skb->data);
471 /* Ensure packet fits inside a single netlink attribute */
472 payload_len = min_t(size_t, skb->len, NET_DM_MAX_PACKET_SIZE);
473 if (net_dm_trunc_len)
474 payload_len = min_t(size_t, net_dm_trunc_len, payload_len);
476 msg = nlmsg_new(net_dm_packet_report_size(payload_len), GFP_KERNEL);
480 rc = net_dm_packet_report_fill(msg, skb, payload_len);
486 genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
492 static void net_dm_packet_work(struct work_struct *work)
494 struct per_cpu_dm_data *data;
495 struct sk_buff_head list;
499 data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
501 __skb_queue_head_init(&list);
503 spin_lock_irqsave(&data->drop_queue.lock, flags);
504 skb_queue_splice_tail_init(&data->drop_queue, &list);
505 spin_unlock_irqrestore(&data->drop_queue.lock, flags);
507 while ((skb = __skb_dequeue(&list)))
508 net_dm_packet_report(skb);
512 net_dm_hw_packet_report_size(size_t payload_len,
513 const struct net_dm_hw_metadata *hw_metadata)
517 size = nlmsg_msg_size(GENL_HDRLEN + net_drop_monitor_family.hdrsize);
519 return NLMSG_ALIGN(size) +
520 /* NET_DM_ATTR_ORIGIN */
521 nla_total_size(sizeof(u16)) +
522 /* NET_DM_ATTR_HW_TRAP_GROUP_NAME */
523 nla_total_size(strlen(hw_metadata->trap_group_name) + 1) +
524 /* NET_DM_ATTR_HW_TRAP_NAME */
525 nla_total_size(strlen(hw_metadata->trap_name) + 1) +
526 /* NET_DM_ATTR_IN_PORT */
527 net_dm_in_port_size() +
528 /* NET_DM_ATTR_TIMESTAMP */
529 nla_total_size(sizeof(struct timespec)) +
530 /* NET_DM_ATTR_ORIG_LEN */
531 nla_total_size(sizeof(u32)) +
532 /* NET_DM_ATTR_PROTO */
533 nla_total_size(sizeof(u16)) +
534 /* NET_DM_ATTR_PAYLOAD */
535 nla_total_size(payload_len);
538 static int net_dm_hw_packet_report_fill(struct sk_buff *msg,
539 struct sk_buff *skb, size_t payload_len)
541 struct net_dm_hw_metadata *hw_metadata;
546 hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
548 hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
549 NET_DM_CMD_PACKET_ALERT);
553 if (nla_put_u16(msg, NET_DM_ATTR_ORIGIN, NET_DM_ORIGIN_HW))
554 goto nla_put_failure;
556 if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_GROUP_NAME,
557 hw_metadata->trap_group_name))
558 goto nla_put_failure;
560 if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_NAME,
561 hw_metadata->trap_name))
562 goto nla_put_failure;
564 if (hw_metadata->input_dev) {
565 struct net_device *dev = hw_metadata->input_dev;
568 rc = net_dm_packet_report_in_port_put(msg, dev->ifindex,
571 goto nla_put_failure;
574 if (ktime_to_timespec_cond(skb->tstamp, &ts) &&
575 nla_put(msg, NET_DM_ATTR_TIMESTAMP, sizeof(ts), &ts))
576 goto nla_put_failure;
578 if (nla_put_u32(msg, NET_DM_ATTR_ORIG_LEN, skb->len))
579 goto nla_put_failure;
584 if (nla_put_u16(msg, NET_DM_ATTR_PROTO, be16_to_cpu(skb->protocol)))
585 goto nla_put_failure;
587 attr = skb_put(msg, nla_total_size(payload_len));
588 attr->nla_type = NET_DM_ATTR_PAYLOAD;
589 attr->nla_len = nla_attr_size(payload_len);
590 if (skb_copy_bits(skb, 0, nla_data(attr), payload_len))
591 goto nla_put_failure;
594 genlmsg_end(msg, hdr);
599 genlmsg_cancel(msg, hdr);
603 static struct net_dm_hw_metadata *
604 net_dm_hw_metadata_clone(const struct net_dm_hw_metadata *hw_metadata)
606 struct net_dm_hw_metadata *n_hw_metadata;
607 const char *trap_group_name;
608 const char *trap_name;
610 n_hw_metadata = kmalloc(sizeof(*hw_metadata), GFP_ATOMIC);
614 trap_group_name = kmemdup(hw_metadata->trap_group_name,
615 strlen(hw_metadata->trap_group_name) + 1,
616 GFP_ATOMIC | __GFP_ZERO);
617 if (!trap_group_name)
618 goto free_hw_metadata;
619 n_hw_metadata->trap_group_name = trap_group_name;
621 trap_name = kmemdup(hw_metadata->trap_name,
622 strlen(hw_metadata->trap_name) + 1,
623 GFP_ATOMIC | __GFP_ZERO);
625 goto free_trap_group;
626 n_hw_metadata->trap_name = trap_name;
628 n_hw_metadata->input_dev = hw_metadata->input_dev;
629 if (n_hw_metadata->input_dev)
630 dev_hold(n_hw_metadata->input_dev);
632 return n_hw_metadata;
635 kfree(trap_group_name);
637 kfree(n_hw_metadata);
642 net_dm_hw_metadata_free(const struct net_dm_hw_metadata *hw_metadata)
644 if (hw_metadata->input_dev)
645 dev_put(hw_metadata->input_dev);
646 kfree(hw_metadata->trap_name);
647 kfree(hw_metadata->trap_group_name);
651 static void net_dm_hw_packet_report(struct sk_buff *skb)
653 struct net_dm_hw_metadata *hw_metadata;
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 payload_len = min_t(size_t, skb->len, NET_DM_MAX_PACKET_SIZE);
664 if (net_dm_trunc_len)
665 payload_len = min_t(size_t, net_dm_trunc_len, payload_len);
667 hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
668 msg = nlmsg_new(net_dm_hw_packet_report_size(payload_len, hw_metadata),
673 rc = net_dm_hw_packet_report_fill(msg, skb, payload_len);
679 genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
682 net_dm_hw_metadata_free(NET_DM_SKB_CB(skb)->hw_metadata);
686 static void net_dm_hw_packet_work(struct work_struct *work)
688 struct per_cpu_dm_data *hw_data;
689 struct sk_buff_head list;
693 hw_data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
695 __skb_queue_head_init(&list);
697 spin_lock_irqsave(&hw_data->drop_queue.lock, flags);
698 skb_queue_splice_tail_init(&hw_data->drop_queue, &list);
699 spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
701 while ((skb = __skb_dequeue(&list)))
702 net_dm_hw_packet_report(skb);
706 net_dm_hw_packet_probe(struct sk_buff *skb,
707 const struct net_dm_hw_metadata *hw_metadata)
709 struct net_dm_hw_metadata *n_hw_metadata;
710 ktime_t tstamp = ktime_get_real();
711 struct per_cpu_dm_data *hw_data;
712 struct sk_buff *nskb;
715 nskb = skb_clone(skb, GFP_ATOMIC);
719 n_hw_metadata = net_dm_hw_metadata_clone(hw_metadata);
723 NET_DM_SKB_CB(nskb)->hw_metadata = n_hw_metadata;
724 nskb->tstamp = tstamp;
726 hw_data = this_cpu_ptr(&dm_hw_cpu_data);
728 spin_lock_irqsave(&hw_data->drop_queue.lock, flags);
729 if (skb_queue_len(&hw_data->drop_queue) < net_dm_queue_len)
730 __skb_queue_tail(&hw_data->drop_queue, nskb);
733 spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
735 schedule_work(&hw_data->dm_alert_work);
740 spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
741 u64_stats_update_begin(&hw_data->stats.syncp);
742 hw_data->stats.dropped++;
743 u64_stats_update_end(&hw_data->stats.syncp);
744 net_dm_hw_metadata_free(n_hw_metadata);
749 static const struct net_dm_alert_ops net_dm_alert_packet_ops = {
750 .kfree_skb_probe = net_dm_packet_trace_kfree_skb_hit,
751 .napi_poll_probe = net_dm_packet_trace_napi_poll_hit,
752 .work_item_func = net_dm_packet_work,
753 .hw_work_item_func = net_dm_hw_packet_work,
754 .hw_probe = net_dm_hw_packet_probe,
757 static const struct net_dm_alert_ops *net_dm_alert_ops_arr[] = {
758 [NET_DM_ALERT_MODE_SUMMARY] = &net_dm_alert_summary_ops,
759 [NET_DM_ALERT_MODE_PACKET] = &net_dm_alert_packet_ops,
762 void net_dm_hw_report(struct sk_buff *skb,
763 const struct net_dm_hw_metadata *hw_metadata)
768 net_dm_alert_ops_arr[net_dm_alert_mode]->hw_probe(skb, hw_metadata);
770 EXPORT_SYMBOL_GPL(net_dm_hw_report);
772 static int net_dm_trace_on_set(struct netlink_ext_ack *extack)
774 const struct net_dm_alert_ops *ops;
777 ops = net_dm_alert_ops_arr[net_dm_alert_mode];
779 if (!try_module_get(THIS_MODULE)) {
780 NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
784 for_each_possible_cpu(cpu) {
785 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
788 INIT_WORK(&data->dm_alert_work, ops->work_item_func);
789 timer_setup(&data->send_timer, sched_send_work, 0);
790 /* Allocate a new per-CPU skb for the summary alert message and
791 * free the old one which might contain stale data from
794 skb = reset_per_cpu_data(data);
798 rc = register_trace_kfree_skb(ops->kfree_skb_probe, NULL);
800 NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to kfree_skb() tracepoint");
804 rc = register_trace_napi_poll(ops->napi_poll_probe, NULL);
806 NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to napi_poll() tracepoint");
807 goto err_unregister_trace;
812 err_unregister_trace:
813 unregister_trace_kfree_skb(ops->kfree_skb_probe, NULL);
815 module_put(THIS_MODULE);
819 static void net_dm_trace_off_set(void)
821 struct dm_hw_stat_delta *new_stat, *temp;
822 const struct net_dm_alert_ops *ops;
825 ops = net_dm_alert_ops_arr[net_dm_alert_mode];
827 unregister_trace_napi_poll(ops->napi_poll_probe, NULL);
828 unregister_trace_kfree_skb(ops->kfree_skb_probe, NULL);
830 tracepoint_synchronize_unregister();
832 /* Make sure we do not send notifications to user space after request
833 * to stop tracing returns.
835 for_each_possible_cpu(cpu) {
836 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
839 del_timer_sync(&data->send_timer);
840 cancel_work_sync(&data->dm_alert_work);
841 while ((skb = __skb_dequeue(&data->drop_queue)))
845 list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
846 if (new_stat->dev == NULL) {
847 list_del_rcu(&new_stat->list);
848 kfree_rcu(new_stat, rcu);
852 module_put(THIS_MODULE);
855 static int set_all_monitor_traces(int state, struct netlink_ext_ack *extack)
859 if (state == trace_state) {
860 NL_SET_ERR_MSG_MOD(extack, "Trace state already set to requested state");
866 rc = net_dm_trace_on_set(extack);
869 net_dm_trace_off_set();
884 static bool net_dm_is_monitoring(void)
886 return trace_state == TRACE_ON || monitor_hw;
889 static int net_dm_alert_mode_get_from_info(struct genl_info *info,
890 enum net_dm_alert_mode *p_alert_mode)
894 val = nla_get_u8(info->attrs[NET_DM_ATTR_ALERT_MODE]);
897 case NET_DM_ALERT_MODE_SUMMARY: /* fall-through */
898 case NET_DM_ALERT_MODE_PACKET:
908 static int net_dm_alert_mode_set(struct genl_info *info)
910 struct netlink_ext_ack *extack = info->extack;
911 enum net_dm_alert_mode alert_mode;
914 if (!info->attrs[NET_DM_ATTR_ALERT_MODE])
917 rc = net_dm_alert_mode_get_from_info(info, &alert_mode);
919 NL_SET_ERR_MSG_MOD(extack, "Invalid alert mode");
923 net_dm_alert_mode = alert_mode;
928 static void net_dm_trunc_len_set(struct genl_info *info)
930 if (!info->attrs[NET_DM_ATTR_TRUNC_LEN])
933 net_dm_trunc_len = nla_get_u32(info->attrs[NET_DM_ATTR_TRUNC_LEN]);
936 static void net_dm_queue_len_set(struct genl_info *info)
938 if (!info->attrs[NET_DM_ATTR_QUEUE_LEN])
941 net_dm_queue_len = nla_get_u32(info->attrs[NET_DM_ATTR_QUEUE_LEN]);
944 static int net_dm_cmd_config(struct sk_buff *skb,
945 struct genl_info *info)
947 struct netlink_ext_ack *extack = info->extack;
950 if (net_dm_is_monitoring()) {
951 NL_SET_ERR_MSG_MOD(extack, "Cannot configure drop monitor during monitoring");
955 rc = net_dm_alert_mode_set(info);
959 net_dm_trunc_len_set(info);
961 net_dm_queue_len_set(info);
966 static int net_dm_cmd_trace(struct sk_buff *skb,
967 struct genl_info *info)
969 switch (info->genlhdr->cmd) {
970 case NET_DM_CMD_START:
971 return set_all_monitor_traces(TRACE_ON, info->extack);
972 case NET_DM_CMD_STOP:
973 return set_all_monitor_traces(TRACE_OFF, info->extack);
979 static int net_dm_config_fill(struct sk_buff *msg, struct genl_info *info)
983 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
984 &net_drop_monitor_family, 0, NET_DM_CMD_CONFIG_NEW);
988 if (nla_put_u8(msg, NET_DM_ATTR_ALERT_MODE, net_dm_alert_mode))
989 goto nla_put_failure;
991 if (nla_put_u32(msg, NET_DM_ATTR_TRUNC_LEN, net_dm_trunc_len))
992 goto nla_put_failure;
994 if (nla_put_u32(msg, NET_DM_ATTR_QUEUE_LEN, net_dm_queue_len))
995 goto nla_put_failure;
997 genlmsg_end(msg, hdr);
1002 genlmsg_cancel(msg, hdr);
1006 static int net_dm_cmd_config_get(struct sk_buff *skb, struct genl_info *info)
1008 struct sk_buff *msg;
1011 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1015 rc = net_dm_config_fill(msg, info);
1019 return genlmsg_reply(msg, info);
1026 static void net_dm_stats_read(struct net_dm_stats *stats)
1030 memset(stats, 0, sizeof(*stats));
1031 for_each_possible_cpu(cpu) {
1032 struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
1033 struct net_dm_stats *cpu_stats = &data->stats;
1038 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1039 dropped = cpu_stats->dropped;
1040 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1042 stats->dropped += dropped;
1046 static int net_dm_stats_put(struct sk_buff *msg)
1048 struct net_dm_stats stats;
1049 struct nlattr *attr;
1051 net_dm_stats_read(&stats);
1053 attr = nla_nest_start(msg, NET_DM_ATTR_STATS);
1057 if (nla_put_u64_64bit(msg, NET_DM_ATTR_STATS_DROPPED,
1058 stats.dropped, NET_DM_ATTR_PAD))
1059 goto nla_put_failure;
1061 nla_nest_end(msg, attr);
1066 nla_nest_cancel(msg, attr);
1070 static void net_dm_hw_stats_read(struct net_dm_stats *stats)
1074 memset(stats, 0, sizeof(*stats));
1075 for_each_possible_cpu(cpu) {
1076 struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1077 struct net_dm_stats *cpu_stats = &hw_data->stats;
1082 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1083 dropped = cpu_stats->dropped;
1084 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1086 stats->dropped += dropped;
1090 static int net_dm_hw_stats_put(struct sk_buff *msg)
1092 struct net_dm_stats stats;
1093 struct nlattr *attr;
1095 net_dm_hw_stats_read(&stats);
1097 attr = nla_nest_start(msg, NET_DM_ATTR_HW_STATS);
1101 if (nla_put_u64_64bit(msg, NET_DM_ATTR_STATS_DROPPED,
1102 stats.dropped, NET_DM_ATTR_PAD))
1103 goto nla_put_failure;
1105 nla_nest_end(msg, attr);
1110 nla_nest_cancel(msg, attr);
1114 static int net_dm_stats_fill(struct sk_buff *msg, struct genl_info *info)
1119 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1120 &net_drop_monitor_family, 0, NET_DM_CMD_STATS_NEW);
1124 rc = net_dm_stats_put(msg);
1126 goto nla_put_failure;
1128 rc = net_dm_hw_stats_put(msg);
1130 goto nla_put_failure;
1132 genlmsg_end(msg, hdr);
1137 genlmsg_cancel(msg, hdr);
1141 static int net_dm_cmd_stats_get(struct sk_buff *skb, struct genl_info *info)
1143 struct sk_buff *msg;
1146 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1150 rc = net_dm_stats_fill(msg, info);
1154 return genlmsg_reply(msg, info);
1161 static int dropmon_net_event(struct notifier_block *ev_block,
1162 unsigned long event, void *ptr)
1164 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1165 struct dm_hw_stat_delta *new_stat = NULL;
1166 struct dm_hw_stat_delta *tmp;
1169 case NETDEV_REGISTER:
1170 new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
1175 new_stat->dev = dev;
1176 new_stat->last_rx = jiffies;
1177 mutex_lock(&net_dm_mutex);
1178 list_add_rcu(&new_stat->list, &hw_stats_list);
1179 mutex_unlock(&net_dm_mutex);
1181 case NETDEV_UNREGISTER:
1182 mutex_lock(&net_dm_mutex);
1183 list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
1184 if (new_stat->dev == dev) {
1185 new_stat->dev = NULL;
1186 if (trace_state == TRACE_OFF) {
1187 list_del_rcu(&new_stat->list);
1188 kfree_rcu(new_stat, rcu);
1193 mutex_unlock(&net_dm_mutex);
1200 static const struct nla_policy net_dm_nl_policy[NET_DM_ATTR_MAX + 1] = {
1201 [NET_DM_ATTR_UNSPEC] = { .strict_start_type = NET_DM_ATTR_UNSPEC + 1 },
1202 [NET_DM_ATTR_ALERT_MODE] = { .type = NLA_U8 },
1203 [NET_DM_ATTR_TRUNC_LEN] = { .type = NLA_U32 },
1204 [NET_DM_ATTR_QUEUE_LEN] = { .type = NLA_U32 },
1207 static const struct genl_ops dropmon_ops[] = {
1209 .cmd = NET_DM_CMD_CONFIG,
1210 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1211 .doit = net_dm_cmd_config,
1212 .flags = GENL_ADMIN_PERM,
1215 .cmd = NET_DM_CMD_START,
1216 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1217 .doit = net_dm_cmd_trace,
1220 .cmd = NET_DM_CMD_STOP,
1221 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1222 .doit = net_dm_cmd_trace,
1225 .cmd = NET_DM_CMD_CONFIG_GET,
1226 .doit = net_dm_cmd_config_get,
1229 .cmd = NET_DM_CMD_STATS_GET,
1230 .doit = net_dm_cmd_stats_get,
1234 static int net_dm_nl_pre_doit(const struct genl_ops *ops,
1235 struct sk_buff *skb, struct genl_info *info)
1237 mutex_lock(&net_dm_mutex);
1242 static void net_dm_nl_post_doit(const struct genl_ops *ops,
1243 struct sk_buff *skb, struct genl_info *info)
1245 mutex_unlock(&net_dm_mutex);
1248 static struct genl_family net_drop_monitor_family __ro_after_init = {
1252 .maxattr = NET_DM_ATTR_MAX,
1253 .policy = net_dm_nl_policy,
1254 .pre_doit = net_dm_nl_pre_doit,
1255 .post_doit = net_dm_nl_post_doit,
1256 .module = THIS_MODULE,
1258 .n_ops = ARRAY_SIZE(dropmon_ops),
1259 .mcgrps = dropmon_mcgrps,
1260 .n_mcgrps = ARRAY_SIZE(dropmon_mcgrps),
1263 static struct notifier_block dropmon_net_notifier = {
1264 .notifier_call = dropmon_net_event
1267 static void __net_dm_cpu_data_init(struct per_cpu_dm_data *data)
1269 spin_lock_init(&data->lock);
1270 skb_queue_head_init(&data->drop_queue);
1271 u64_stats_init(&data->stats.syncp);
1274 static void __net_dm_cpu_data_fini(struct per_cpu_dm_data *data)
1276 WARN_ON(!skb_queue_empty(&data->drop_queue));
1279 static void net_dm_cpu_data_init(int cpu)
1281 struct per_cpu_dm_data *data;
1283 data = &per_cpu(dm_cpu_data, cpu);
1284 __net_dm_cpu_data_init(data);
1287 static void net_dm_cpu_data_fini(int cpu)
1289 struct per_cpu_dm_data *data;
1291 data = &per_cpu(dm_cpu_data, cpu);
1292 /* At this point, we should have exclusive access
1293 * to this struct and can free the skb inside it.
1295 consume_skb(data->skb);
1296 __net_dm_cpu_data_fini(data);
1299 static void net_dm_hw_cpu_data_init(int cpu)
1301 struct per_cpu_dm_data *hw_data;
1303 hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1304 __net_dm_cpu_data_init(hw_data);
1307 static void net_dm_hw_cpu_data_fini(int cpu)
1309 struct per_cpu_dm_data *hw_data;
1311 hw_data = &per_cpu(dm_hw_cpu_data, cpu);
1312 __net_dm_cpu_data_fini(hw_data);
1315 static int __init init_net_drop_monitor(void)
1319 pr_info("Initializing network drop monitor service\n");
1321 if (sizeof(void *) > 8) {
1322 pr_err("Unable to store program counters on this arch, Drop monitor failed\n");
1326 rc = genl_register_family(&net_drop_monitor_family);
1328 pr_err("Could not create drop monitor netlink family\n");
1331 WARN_ON(net_drop_monitor_family.mcgrp_offset != NET_DM_GRP_ALERT);
1333 rc = register_netdevice_notifier(&dropmon_net_notifier);
1335 pr_crit("Failed to register netdevice notifier\n");
1341 for_each_possible_cpu(cpu) {
1342 net_dm_cpu_data_init(cpu);
1343 net_dm_hw_cpu_data_init(cpu);
1349 genl_unregister_family(&net_drop_monitor_family);
1354 static void exit_net_drop_monitor(void)
1358 BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier));
1361 * Because of the module_get/put we do in the trace state change path
1362 * we are guarnateed not to have any current users when we get here
1365 for_each_possible_cpu(cpu) {
1366 net_dm_hw_cpu_data_fini(cpu);
1367 net_dm_cpu_data_fini(cpu);
1370 BUG_ON(genl_unregister_family(&net_drop_monitor_family));
1373 module_init(init_net_drop_monitor);
1374 module_exit(exit_net_drop_monitor);
1376 MODULE_LICENSE("GPL v2");
1377 MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
1378 MODULE_ALIAS_GENL_FAMILY("NET_DM");