linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michal Kubecek <mkubecek@suse.cz>
To: David Miller <davem@davemloft.net>, netdev@vger.kernel.org
Cc: Jakub Kicinski <jakub.kicinski@netronome.com>,
	Jiri Pirko <jiri@resnulli.us>, Andrew Lunn <andrew@lunn.ch>,
	Florian Fainelli <f.fainelli@gmail.com>,
	John Linville <linville@tuxdriver.com>,
	Stephen Hemminger <stephen@networkplumber.org>,
	Johannes Berg <johannes@sipsolutions.net>,
	linux-kernel@vger.kernel.org
Subject: [PATCH net-next v8 09/14] ethtool: add default notification handler
Date: Mon, 23 Dec 2019 00:45:59 +0100 (CET)	[thread overview]
Message-ID: <ae163843d4df6ccbccd4ca24ae5d7c19c2e1909d.1577052887.git.mkubecek@suse.cz> (raw)
In-Reply-To: <cover.1577052887.git.mkubecek@suse.cz>

The ethtool netlink notifications have the same format as related GET
replies so that if generic GET handling framework is used to process GET
requests, its callbacks and instance of struct get_request_ops can be
also used to compose corresponding notification message.

Provide function ethnl_std_notify() to be used as notification handler in
ethnl_notify_handlers table.

Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
---
 net/ethtool/netlink.c | 89 +++++++++++++++++++++++++++++++++++++++++++
 net/ethtool/netlink.h |  4 +-
 2 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/net/ethtool/netlink.c b/net/ethtool/netlink.c
index 7867425956f6..057b67f8ba8c 100644
--- a/net/ethtool/netlink.c
+++ b/net/ethtool/netlink.c
@@ -7,6 +7,7 @@
 static struct genl_family ethtool_genl_family;
 
 static bool ethnl_ok __read_mostly;
+static u32 ethnl_bcast_seq;
 
 static const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_MAX + 1] = {
 	[ETHTOOL_A_HEADER_UNSPEC]	= { .type = NLA_REJECT },
@@ -171,6 +172,18 @@ struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
 	return NULL;
 }
 
+static void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
+{
+	return genlmsg_put(skb, 0, ++ethnl_bcast_seq, &ethtool_genl_family, 0,
+			   cmd);
+}
+
+static int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
+{
+	return genlmsg_multicast_netns(&ethtool_genl_family, dev_net(dev), skb,
+				       0, ETHNL_MCGRP_MONITOR, GFP_KERNEL);
+}
+
 /* GET request helpers */
 
 /**
@@ -494,6 +507,82 @@ static int ethnl_default_done(struct netlink_callback *cb)
 	return 0;
 }
 
+static const struct ethnl_request_ops *
+ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
+};
+
+/* default notification handler */
+static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
+				 const void *data)
+{
+	struct ethnl_reply_data *reply_data;
+	const struct ethnl_request_ops *ops;
+	struct ethnl_req_info *req_info;
+	struct sk_buff *skb;
+	void *reply_payload;
+	int reply_len;
+	int ret;
+
+	if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
+		      !ethnl_default_notify_ops[cmd],
+		      "unexpected notification type %u\n", cmd))
+		return;
+	ops = ethnl_default_notify_ops[cmd];
+	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
+	if (!req_info)
+		return;
+	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
+	if (!reply_data) {
+		kfree(req_info);
+		return;
+	}
+
+	req_info->dev = dev;
+	req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
+
+	ethnl_init_reply_data(reply_data, ops, dev);
+	ret = ops->prepare_data(req_info, reply_data, NULL);
+	if (ret < 0)
+		goto err_cleanup;
+	reply_len = ops->reply_size(req_info, reply_data);
+	if (ret < 0)
+		goto err_cleanup;
+	ret = -ENOMEM;
+	skb = genlmsg_new(reply_len, GFP_KERNEL);
+	if (!skb)
+		goto err_cleanup;
+	reply_payload = ethnl_bcastmsg_put(skb, cmd);
+	if (!reply_payload)
+		goto err_skb;
+	ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
+	if (ret < 0)
+		goto err_msg;
+	ret = ops->fill_reply(skb, req_info, reply_data);
+	if (ret < 0)
+		goto err_msg;
+	if (ops->cleanup_data)
+		ops->cleanup_data(reply_data);
+
+	genlmsg_end(skb, reply_payload);
+	kfree(reply_data);
+	kfree(req_info);
+	ethnl_multicast(skb, dev);
+	return;
+
+err_msg:
+	WARN_ONCE(ret == -EMSGSIZE,
+		  "calculated message payload length (%d) not sufficient\n",
+		  reply_len);
+err_skb:
+	nlmsg_free(skb);
+err_cleanup:
+	if (ops->cleanup_data)
+		ops->cleanup_data(reply_data);
+	kfree(reply_data);
+	kfree(req_info);
+	return;
+}
+
 /* notifications */
 
 typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
diff --git a/net/ethtool/netlink.h b/net/ethtool/netlink.h
index 3448029e3ea2..72df4ffefe30 100644
--- a/net/ethtool/netlink.h
+++ b/net/ethtool/netlink.h
@@ -300,7 +300,9 @@ static inline void ethnl_ops_complete(struct net_device *dev)
  * unified infrastructure. When used, a pointer to an instance of this
  * structure is to be added to &ethnl_default_requests array and generic
  * handlers ethnl_default_doit(), ethnl_default_dumpit(),
- * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops.
+ * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops;
+ * ethnl_default_notify() can be used in @ethnl_notify_handlers to send
+ * notifications of the corresponding type.
  */
 struct ethnl_request_ops {
 	u8			request_cmd;
-- 
2.24.1


  parent reply	other threads:[~2019-12-22 23:46 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-22 23:45 [PATCH net-next v8 00/14] ethtool netlink interface, part 1 Michal Kubecek
2019-12-22 23:45 ` [PATCH net-next v8 01/14] ethtool: introduce ethtool netlink interface Michal Kubecek
2019-12-24  4:01   ` Florian Fainelli
2019-12-24  9:45   ` Andrew Lunn
2019-12-22 23:45 ` [PATCH net-next v8 02/14] ethtool: helper functions for " Michal Kubecek
2019-12-24  4:08   ` Florian Fainelli
2019-12-25 11:07     ` Michal Kubecek
2019-12-22 23:45 ` [PATCH net-next v8 03/14] ethtool: netlink bitset handling Michal Kubecek
2019-12-24  4:15   ` Florian Fainelli
2019-12-22 23:45 ` [PATCH net-next v8 04/14] ethtool: support for netlink notifications Michal Kubecek
2019-12-24  4:16   ` Florian Fainelli
2019-12-22 23:45 ` [PATCH net-next v8 05/14] ethtool: default handlers for GET requests Michal Kubecek
2019-12-24  4:23   ` Florian Fainelli
2019-12-22 23:45 ` [PATCH net-next v8 06/14] ethtool: provide string sets with STRSET_GET request Michal Kubecek
2019-12-24  4:28   ` Florian Fainelli
2019-12-22 23:45 ` [PATCH net-next v8 07/14] ethtool: provide link settings with LINKINFO_GET request Michal Kubecek
2019-12-24  4:32   ` Florian Fainelli
2019-12-22 23:45 ` [PATCH net-next v8 08/14] ethtool: set link settings with LINKINFO_SET request Michal Kubecek
2019-12-24  4:30   ` Florian Fainelli
2019-12-22 23:45 ` Michal Kubecek [this message]
2019-12-24  4:31   ` [PATCH net-next v8 09/14] ethtool: add default notification handler Florian Fainelli
2019-12-22 23:46 ` [PATCH net-next v8 10/14] ethtool: add LINKINFO_NTF notification Michal Kubecek
2019-12-24  4:34   ` Florian Fainelli
2019-12-22 23:46 ` [PATCH net-next v8 11/14] ethtool: provide link mode information with LINKMODES_GET request Michal Kubecek
2019-12-24  4:35   ` Florian Fainelli
2019-12-22 23:46 ` [PATCH net-next v8 12/14] ethtool: set link modes related data with LINKMODES_SET request Michal Kubecek
2019-12-24  4:40   ` Florian Fainelli
2019-12-22 23:46 ` [PATCH net-next v8 13/14] ethtool: add LINKMODES_NTF notification Michal Kubecek
2019-12-24  4:41   ` Florian Fainelli
2019-12-22 23:46 ` [PATCH net-next v8 14/14] ethtool: provide link state with LINKSTATE_GET request Michal Kubecek
2019-12-24  4:44   ` Florian Fainelli
2019-12-27 12:47     ` Michal Kubecek
2019-12-23 16:52 ` [PATCH net-next v8 00/14] ethtool netlink interface, part 1 Florian Fainelli
2019-12-23 22:05   ` Michal Kubecek
2019-12-24  4:45     ` Florian Fainelli
2019-12-25  7:18 ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ae163843d4df6ccbccd4ca24ae5d7c19c2e1909d.1577052887.git.mkubecek@suse.cz \
    --to=mkubecek@suse.cz \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=f.fainelli@gmail.com \
    --cc=jakub.kicinski@netronome.com \
    --cc=jiri@resnulli.us \
    --cc=johannes@sipsolutions.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linville@tuxdriver.com \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).