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