Skip to content

Commit

Permalink
ethtool: add default notification handler
Browse files Browse the repository at this point in the history
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>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Michal Kubecek authored and David S. Miller committed Dec 28, 2019
1 parent a53f3d4 commit 5cf2a54
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
89 changes: 89 additions & 0 deletions net/ethtool/netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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 */

/**
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion net/ethtool/netlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5cf2a54

Please sign in to comment.