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