Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5825356ybl; Sun, 22 Dec 2019 15:48:15 -0800 (PST) X-Google-Smtp-Source: APXvYqzYWHVBumfF3xZrO/XFnSrqhx2Ylr/uSv+LZECA+009MCZuIT3n4g0FpHx2pHBxqhrjDVYv X-Received: by 2002:a05:6830:2057:: with SMTP id f23mr25828024otp.110.1577058495347; Sun, 22 Dec 2019 15:48:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577058495; cv=none; d=google.com; s=arc-20160816; b=lBuIt3iYgQ+pkiIJlQI/+1Mx/oazOV35XllENKrkJ+L04n3VgoNAHUeyas9brH6YV3 8+Jk7Y2Sh03iqw5aFfWcOGwtLEiHz5N0DQYa+jfAgC/btFqcrNZr/PJHdFx1hEdRmtD8 PICloVetp6qTS5l++R9XQo/O7+HnJ+h50qST4E2KiiCbqcCQz02oXMpmm90cZ0gLdB8x euYR9rbvp+ddfQnKexid1VUSNogXw5YGaVDvrtljwbUX0jJfz50cUiWNIxV2Z3bsD48f bNeD5IO8udkxrh8+Qc8680AEcmVxZ1sXe+lKUk+rhrtmkMoHjcpZbvC9DUeygyLHaaf0 v2zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:cc:to:subject:from:references :in-reply-to:message-id; bh=9xSe3BxGLd3TiDWkC2swZ5Ufwkdu0XQZulR0EUbxDvQ=; b=v4mQ9C4WucoTNHM/RioG5Vo+pDhMgdOXKSRdGJUdNbs69rzfpIwU5Ba2XO2v91THFx FzxO/F/oGtPPjqNiKzvxdAmjUtSlqiDV6POHBe4iCGzIhp/M8DwS456OJheyqYq7Rv3/ fMFVCfJ6qoNQ9Wq0psyiOcGCaNm1cBHcmVmdim2dCVfqdrk1iE9qQzA/lkWEGWYTw/qj De8PvRrXEf+UfqEH+Gn40Zw7xfuf2g8DWuroOsI1tuXLtd9N3vuKRgpPBCifTZgVe55Z XZ6WpWyk8qLHxhUrxqYse11qtEicdtISQkEA4JmLI2+OjQ89wGBzgWpyYLbIkAiA3K11 rL6Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f190si8854272oig.229.2019.12.22.15.48.04; Sun, 22 Dec 2019 15:48:15 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727102AbfLVXqC (ORCPT + 99 others); Sun, 22 Dec 2019 18:46:02 -0500 Received: from mx2.suse.de ([195.135.220.15]:55456 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727066AbfLVXqB (ORCPT ); Sun, 22 Dec 2019 18:46:01 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 908B5AF27; Sun, 22 Dec 2019 23:45:59 +0000 (UTC) Received: by unicorn.suse.cz (Postfix, from userid 1000) id 43B21E03A8; Mon, 23 Dec 2019 00:45:59 +0100 (CET) Message-Id: In-Reply-To: References: From: Michal Kubecek Subject: [PATCH net-next v8 09/14] ethtool: add default notification handler To: David Miller , netdev@vger.kernel.org Cc: Jakub Kicinski , Jiri Pirko , Andrew Lunn , Florian Fainelli , John Linville , Stephen Hemminger , Johannes Berg , linux-kernel@vger.kernel.org Date: Mon, 23 Dec 2019 00:45:59 +0100 (CET) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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, ðtool_genl_family, 0, + cmd); +} + +static int ethnl_multicast(struct sk_buff *skb, struct net_device *dev) +{ + return genlmsg_multicast_netns(ðtool_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 ðnl_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