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