root/net/core/drop_monitor.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. reset_per_cpu_data
  2. send_dm_alert
  3. sched_send_work
  4. trace_drop_common
  5. trace_kfree_skb_hit
  6. trace_napi_poll_hit
  7. net_dm_hw_reset_per_cpu_data
  8. net_dm_hw_entry_put
  9. net_dm_hw_entries_put
  10. net_dm_hw_summary_report_fill
  11. net_dm_hw_summary_work
  12. net_dm_hw_summary_probe
  13. net_dm_packet_trace_kfree_skb_hit
  14. net_dm_packet_trace_napi_poll_hit
  15. net_dm_in_port_size
  16. net_dm_packet_report_size
  17. net_dm_packet_report_in_port_put
  18. net_dm_packet_report_fill
  19. net_dm_packet_report
  20. net_dm_packet_work
  21. net_dm_hw_packet_report_size
  22. net_dm_hw_packet_report_fill
  23. net_dm_hw_metadata_clone
  24. net_dm_hw_metadata_free
  25. net_dm_hw_packet_report
  26. net_dm_hw_packet_work
  27. net_dm_hw_packet_probe
  28. net_dm_hw_report
  29. net_dm_hw_monitor_start
  30. net_dm_hw_monitor_stop
  31. net_dm_trace_on_set
  32. net_dm_trace_off_set
  33. set_all_monitor_traces
  34. net_dm_is_monitoring
  35. net_dm_alert_mode_get_from_info
  36. net_dm_alert_mode_set
  37. net_dm_trunc_len_set
  38. net_dm_queue_len_set
  39. net_dm_cmd_config
  40. net_dm_monitor_start
  41. net_dm_monitor_stop
  42. net_dm_cmd_trace
  43. net_dm_config_fill
  44. net_dm_cmd_config_get
  45. net_dm_stats_read
  46. net_dm_stats_put
  47. net_dm_hw_stats_read
  48. net_dm_hw_stats_put
  49. net_dm_stats_fill
  50. net_dm_cmd_stats_get
  51. dropmon_net_event
  52. net_dm_nl_pre_doit
  53. net_dm_nl_post_doit
  54. __net_dm_cpu_data_init
  55. __net_dm_cpu_data_fini
  56. net_dm_cpu_data_init
  57. net_dm_cpu_data_fini
  58. net_dm_hw_cpu_data_init
  59. net_dm_hw_cpu_data_fini
  60. init_net_drop_monitor
  61. exit_net_drop_monitor

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

/* [<][>][^][v][top][bottom][index][help] */