Skip to content

Commit 5cf2a54

Browse files
mkubecekdavem330
authored andcommitted
ethtool: add default notification handler
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>
1 parent a53f3d4 commit 5cf2a54

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎net/ethtool/netlink.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
static struct genl_family ethtool_genl_family;
88

99
static bool ethnl_ok __read_mostly;
10+
static u32 ethnl_bcast_seq;
1011

1112
static const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_MAX + 1] = {
1213
[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,
171172
return NULL;
172173
}
173174

175+
static void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
176+
{
177+
return genlmsg_put(skb, 0, ++ethnl_bcast_seq, &ethtool_genl_family, 0,
178+
cmd);
179+
}
180+
181+
static int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
182+
{
183+
return genlmsg_multicast_netns(&ethtool_genl_family, dev_net(dev), skb,
184+
0, ETHNL_MCGRP_MONITOR, GFP_KERNEL);
185+
}
186+
174187
/* GET request helpers */
175188

176189
/**
@@ -494,6 +507,82 @@ static int ethnl_default_done(struct netlink_callback *cb)
494507
return 0;
495508
}
496509

510+
static const struct ethnl_request_ops *
511+
ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
512+
};
513+
514+
/* default notification handler */
515+
static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
516+
const void *data)
517+
{
518+
struct ethnl_reply_data *reply_data;
519+
const struct ethnl_request_ops *ops;
520+
struct ethnl_req_info *req_info;
521+
struct sk_buff *skb;
522+
void *reply_payload;
523+
int reply_len;
524+
int ret;
525+
526+
if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
527+
!ethnl_default_notify_ops[cmd],
528+
"unexpected notification type %u\n", cmd))
529+
return;
530+
ops = ethnl_default_notify_ops[cmd];
531+
req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
532+
if (!req_info)
533+
return;
534+
reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
535+
if (!reply_data) {
536+
kfree(req_info);
537+
return;
538+
}
539+
540+
req_info->dev = dev;
541+
req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
542+
543+
ethnl_init_reply_data(reply_data, ops, dev);
544+
ret = ops->prepare_data(req_info, reply_data, NULL);
545+
if (ret < 0)
546+
goto err_cleanup;
547+
reply_len = ops->reply_size(req_info, reply_data);
548+
if (ret < 0)
549+
goto err_cleanup;
550+
ret = -ENOMEM;
551+
skb = genlmsg_new(reply_len, GFP_KERNEL);
552+
if (!skb)
553+
goto err_cleanup;
554+
reply_payload = ethnl_bcastmsg_put(skb, cmd);
555+
if (!reply_payload)
556+
goto err_skb;
557+
ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
558+
if (ret < 0)
559+
goto err_msg;
560+
ret = ops->fill_reply(skb, req_info, reply_data);
561+
if (ret < 0)
562+
goto err_msg;
563+
if (ops->cleanup_data)
564+
ops->cleanup_data(reply_data);
565+
566+
genlmsg_end(skb, reply_payload);
567+
kfree(reply_data);
568+
kfree(req_info);
569+
ethnl_multicast(skb, dev);
570+
return;
571+
572+
err_msg:
573+
WARN_ONCE(ret == -EMSGSIZE,
574+
"calculated message payload length (%d) not sufficient\n",
575+
reply_len);
576+
err_skb:
577+
nlmsg_free(skb);
578+
err_cleanup:
579+
if (ops->cleanup_data)
580+
ops->cleanup_data(reply_data);
581+
kfree(reply_data);
582+
kfree(req_info);
583+
return;
584+
}
585+
497586
/* notifications */
498587

499588
typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,

‎net/ethtool/netlink.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ static inline void ethnl_ops_complete(struct net_device *dev)
300300
* unified infrastructure. When used, a pointer to an instance of this
301301
* structure is to be added to &ethnl_default_requests array and generic
302302
* handlers ethnl_default_doit(), ethnl_default_dumpit(),
303-
* ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops.
303+
* ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops;
304+
* ethnl_default_notify() can be used in @ethnl_notify_handlers to send
305+
* notifications of the corresponding type.
304306
*/
305307
struct ethnl_request_ops {
306308
u8 request_cmd;

0 commit comments

Comments
 (0)