Linux Audio

Check our new training course

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