201 lines
5.3 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-only
#include "netlink.h"
#include "common.h"
#include "bitset.h"
struct stats_req_info {
struct ethnl_req_info base;
DECLARE_BITMAP(stat_mask, __ETHTOOL_STATS_CNT);
};
#define STATS_REQINFO(__req_base) \
container_of(__req_base, struct stats_req_info, base)
struct stats_reply_data {
struct ethnl_reply_data base;
struct ethtool_eth_phy_stats phy_stats;
};
#define STATS_REPDATA(__reply_base) \
container_of(__reply_base, struct stats_reply_data, base)
const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN] = {
[ETHTOOL_STATS_ETH_PHY] = "eth-phy",
};
const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN] = {
[ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR] = "SymbolErrorDuringCarrier",
};
const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_GROUPS + 1] = {
[ETHTOOL_A_STATS_HEADER] =
NLA_POLICY_NESTED(ethnl_header_policy),
[ETHTOOL_A_STATS_GROUPS] = { .type = NLA_NESTED },
};
static int stats_parse_request(struct ethnl_req_info *req_base,
struct nlattr **tb,
struct netlink_ext_ack *extack)
{
struct stats_req_info *req_info = STATS_REQINFO(req_base);
bool mod = false;
int err;
err = ethnl_update_bitset(req_info->stat_mask, __ETHTOOL_STATS_CNT,
tb[ETHTOOL_A_STATS_GROUPS], stats_std_names,
extack, &mod);
if (err)
return err;
if (!mod) {
NL_SET_ERR_MSG(extack, "no stats requested");
return -EINVAL;
}
return 0;
}
static int stats_prepare_data(const struct ethnl_req_info *req_base,
struct ethnl_reply_data *reply_base,
struct genl_info *info)
{
const struct stats_req_info *req_info = STATS_REQINFO(req_base);
struct stats_reply_data *data = STATS_REPDATA(reply_base);
struct net_device *dev = reply_base->dev;
int ret;
ret = ethnl_ops_begin(dev);
if (ret < 0)
return ret;
memset(&data->phy_stats, 0xff, sizeof(data->phy_stats));
if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask) &&
dev->ethtool_ops->get_eth_phy_stats)
dev->ethtool_ops->get_eth_phy_stats(dev, &data->phy_stats);
ethnl_ops_complete(dev);
return 0;
}
static int stats_reply_size(const struct ethnl_req_info *req_base,
const struct ethnl_reply_data *reply_base)
{
const struct stats_req_info *req_info = STATS_REQINFO(req_base);
unsigned int n_grps = 0, n_stats = 0;
int len = 0;
if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask)) {
n_stats += sizeof(struct ethtool_eth_phy_stats) / sizeof(u64);
n_grps++;
}
len += n_grps * (nla_total_size(0) + /* _A_STATS_GRP */
nla_total_size(4) + /* _A_STATS_GRP_ID */
nla_total_size(4)); /* _A_STATS_GRP_SS_ID */
len += n_stats * (nla_total_size(0) + /* _A_STATS_GRP_STAT */
nla_total_size_64bit(sizeof(u64)));
return len;
}
static int stat_put(struct sk_buff *skb, u16 attrtype, u64 val)
{
struct nlattr *nest;
int ret;
if (val == ETHTOOL_STAT_NOT_SET)
return 0;
/* We want to start stats attr types from 0, so we don't have a type
* for pad inside ETHTOOL_A_STATS_GRP_STAT. Pad things on the outside
* of ETHTOOL_A_STATS_GRP_STAT. Since we're one nest away from the
* actual attr we're 4B off - nla_need_padding_for_64bit() & co.
* can't be used.
*/
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
if (!IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
if (!nla_reserve(skb, ETHTOOL_A_STATS_GRP_PAD, 0))
return -EMSGSIZE;
#endif
nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP_STAT);
if (!nest)
return -EMSGSIZE;
ret = nla_put_u64_64bit(skb, attrtype, val, -1 /* not used */);
if (ret) {
nla_nest_cancel(skb, nest);
return ret;
}
nla_nest_end(skb, nest);
return 0;
}
static int stats_put_phy_stats(struct sk_buff *skb,
const struct stats_reply_data *data)
{
if (stat_put(skb, ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR,
data->phy_stats.SymbolErrorDuringCarrier))
return -EMSGSIZE;
return 0;
}
static int stats_put_stats(struct sk_buff *skb,
const struct stats_reply_data *data,
u32 id, u32 ss_id,
int (*cb)(struct sk_buff *skb,
const struct stats_reply_data *data))
{
struct nlattr *nest;
nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP);
if (!nest)
return -EMSGSIZE;
if (nla_put_u32(skb, ETHTOOL_A_STATS_GRP_ID, id) ||
nla_put_u32(skb, ETHTOOL_A_STATS_GRP_SS_ID, ss_id))
goto err_cancel;
if (cb(skb, data))
goto err_cancel;
nla_nest_end(skb, nest);
return 0;
err_cancel:
nla_nest_cancel(skb, nest);
return -EMSGSIZE;
}
static int stats_fill_reply(struct sk_buff *skb,
const struct ethnl_req_info *req_base,
const struct ethnl_reply_data *reply_base)
{
const struct stats_req_info *req_info = STATS_REQINFO(req_base);
const struct stats_reply_data *data = STATS_REPDATA(reply_base);
int ret = 0;
if (!ret && test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask))
ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_PHY,
ETH_SS_STATS_ETH_PHY,
stats_put_phy_stats);
return ret;
}
const struct ethnl_request_ops ethnl_stats_request_ops = {
.request_cmd = ETHTOOL_MSG_STATS_GET,
.reply_cmd = ETHTOOL_MSG_STATS_GET_REPLY,
.hdr_attr = ETHTOOL_A_STATS_HEADER,
.req_info_size = sizeof(struct stats_req_info),
.reply_data_size = sizeof(struct stats_reply_data),
.parse_request = stats_parse_request,
.prepare_data = stats_prepare_data,
.reply_size = stats_reply_size,
.fill_reply = stats_fill_reply,
};