0001
0002
0003 #include "netlink.h"
0004 #include "common.h"
0005 #include "bitset.h"
0006
0007 struct stats_req_info {
0008 struct ethnl_req_info base;
0009 DECLARE_BITMAP(stat_mask, __ETHTOOL_STATS_CNT);
0010 };
0011
0012 #define STATS_REQINFO(__req_base) \
0013 container_of(__req_base, struct stats_req_info, base)
0014
0015 struct stats_reply_data {
0016 struct ethnl_reply_data base;
0017 struct_group(stats,
0018 struct ethtool_eth_phy_stats phy_stats;
0019 struct ethtool_eth_mac_stats mac_stats;
0020 struct ethtool_eth_ctrl_stats ctrl_stats;
0021 struct ethtool_rmon_stats rmon_stats;
0022 );
0023 const struct ethtool_rmon_hist_range *rmon_ranges;
0024 };
0025
0026 #define STATS_REPDATA(__reply_base) \
0027 container_of(__reply_base, struct stats_reply_data, base)
0028
0029 const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN] = {
0030 [ETHTOOL_STATS_ETH_PHY] = "eth-phy",
0031 [ETHTOOL_STATS_ETH_MAC] = "eth-mac",
0032 [ETHTOOL_STATS_ETH_CTRL] = "eth-ctrl",
0033 [ETHTOOL_STATS_RMON] = "rmon",
0034 };
0035
0036 const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN] = {
0037 [ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR] = "SymbolErrorDuringCarrier",
0038 };
0039
0040 const char stats_eth_mac_names[__ETHTOOL_A_STATS_ETH_MAC_CNT][ETH_GSTRING_LEN] = {
0041 [ETHTOOL_A_STATS_ETH_MAC_2_TX_PKT] = "FramesTransmittedOK",
0042 [ETHTOOL_A_STATS_ETH_MAC_3_SINGLE_COL] = "SingleCollisionFrames",
0043 [ETHTOOL_A_STATS_ETH_MAC_4_MULTI_COL] = "MultipleCollisionFrames",
0044 [ETHTOOL_A_STATS_ETH_MAC_5_RX_PKT] = "FramesReceivedOK",
0045 [ETHTOOL_A_STATS_ETH_MAC_6_FCS_ERR] = "FrameCheckSequenceErrors",
0046 [ETHTOOL_A_STATS_ETH_MAC_7_ALIGN_ERR] = "AlignmentErrors",
0047 [ETHTOOL_A_STATS_ETH_MAC_8_TX_BYTES] = "OctetsTransmittedOK",
0048 [ETHTOOL_A_STATS_ETH_MAC_9_TX_DEFER] = "FramesWithDeferredXmissions",
0049 [ETHTOOL_A_STATS_ETH_MAC_10_LATE_COL] = "LateCollisions",
0050 [ETHTOOL_A_STATS_ETH_MAC_11_XS_COL] = "FramesAbortedDueToXSColls",
0051 [ETHTOOL_A_STATS_ETH_MAC_12_TX_INT_ERR] = "FramesLostDueToIntMACXmitError",
0052 [ETHTOOL_A_STATS_ETH_MAC_13_CS_ERR] = "CarrierSenseErrors",
0053 [ETHTOOL_A_STATS_ETH_MAC_14_RX_BYTES] = "OctetsReceivedOK",
0054 [ETHTOOL_A_STATS_ETH_MAC_15_RX_INT_ERR] = "FramesLostDueToIntMACRcvError",
0055 [ETHTOOL_A_STATS_ETH_MAC_18_TX_MCAST] = "MulticastFramesXmittedOK",
0056 [ETHTOOL_A_STATS_ETH_MAC_19_TX_BCAST] = "BroadcastFramesXmittedOK",
0057 [ETHTOOL_A_STATS_ETH_MAC_20_XS_DEFER] = "FramesWithExcessiveDeferral",
0058 [ETHTOOL_A_STATS_ETH_MAC_21_RX_MCAST] = "MulticastFramesReceivedOK",
0059 [ETHTOOL_A_STATS_ETH_MAC_22_RX_BCAST] = "BroadcastFramesReceivedOK",
0060 [ETHTOOL_A_STATS_ETH_MAC_23_IR_LEN_ERR] = "InRangeLengthErrors",
0061 [ETHTOOL_A_STATS_ETH_MAC_24_OOR_LEN] = "OutOfRangeLengthField",
0062 [ETHTOOL_A_STATS_ETH_MAC_25_TOO_LONG_ERR] = "FrameTooLongErrors",
0063 };
0064
0065 const char stats_eth_ctrl_names[__ETHTOOL_A_STATS_ETH_CTRL_CNT][ETH_GSTRING_LEN] = {
0066 [ETHTOOL_A_STATS_ETH_CTRL_3_TX] = "MACControlFramesTransmitted",
0067 [ETHTOOL_A_STATS_ETH_CTRL_4_RX] = "MACControlFramesReceived",
0068 [ETHTOOL_A_STATS_ETH_CTRL_5_RX_UNSUP] = "UnsupportedOpcodesReceived",
0069 };
0070
0071 const char stats_rmon_names[__ETHTOOL_A_STATS_RMON_CNT][ETH_GSTRING_LEN] = {
0072 [ETHTOOL_A_STATS_RMON_UNDERSIZE] = "etherStatsUndersizePkts",
0073 [ETHTOOL_A_STATS_RMON_OVERSIZE] = "etherStatsOversizePkts",
0074 [ETHTOOL_A_STATS_RMON_FRAG] = "etherStatsFragments",
0075 [ETHTOOL_A_STATS_RMON_JABBER] = "etherStatsJabbers",
0076 };
0077
0078 const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_GROUPS + 1] = {
0079 [ETHTOOL_A_STATS_HEADER] =
0080 NLA_POLICY_NESTED(ethnl_header_policy),
0081 [ETHTOOL_A_STATS_GROUPS] = { .type = NLA_NESTED },
0082 };
0083
0084 static int stats_parse_request(struct ethnl_req_info *req_base,
0085 struct nlattr **tb,
0086 struct netlink_ext_ack *extack)
0087 {
0088 struct stats_req_info *req_info = STATS_REQINFO(req_base);
0089 bool mod = false;
0090 int err;
0091
0092 err = ethnl_update_bitset(req_info->stat_mask, __ETHTOOL_STATS_CNT,
0093 tb[ETHTOOL_A_STATS_GROUPS], stats_std_names,
0094 extack, &mod);
0095 if (err)
0096 return err;
0097
0098 if (!mod) {
0099 NL_SET_ERR_MSG(extack, "no stats requested");
0100 return -EINVAL;
0101 }
0102
0103 return 0;
0104 }
0105
0106 static int stats_prepare_data(const struct ethnl_req_info *req_base,
0107 struct ethnl_reply_data *reply_base,
0108 struct genl_info *info)
0109 {
0110 const struct stats_req_info *req_info = STATS_REQINFO(req_base);
0111 struct stats_reply_data *data = STATS_REPDATA(reply_base);
0112 struct net_device *dev = reply_base->dev;
0113 int ret;
0114
0115 ret = ethnl_ops_begin(dev);
0116 if (ret < 0)
0117 return ret;
0118
0119
0120
0121
0122 memset(&data->stats, 0xff, sizeof(data->stats));
0123
0124 if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask) &&
0125 dev->ethtool_ops->get_eth_phy_stats)
0126 dev->ethtool_ops->get_eth_phy_stats(dev, &data->phy_stats);
0127 if (test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask) &&
0128 dev->ethtool_ops->get_eth_mac_stats)
0129 dev->ethtool_ops->get_eth_mac_stats(dev, &data->mac_stats);
0130 if (test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask) &&
0131 dev->ethtool_ops->get_eth_ctrl_stats)
0132 dev->ethtool_ops->get_eth_ctrl_stats(dev, &data->ctrl_stats);
0133 if (test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask) &&
0134 dev->ethtool_ops->get_rmon_stats)
0135 dev->ethtool_ops->get_rmon_stats(dev, &data->rmon_stats,
0136 &data->rmon_ranges);
0137
0138 ethnl_ops_complete(dev);
0139 return 0;
0140 }
0141
0142 static int stats_reply_size(const struct ethnl_req_info *req_base,
0143 const struct ethnl_reply_data *reply_base)
0144 {
0145 const struct stats_req_info *req_info = STATS_REQINFO(req_base);
0146 unsigned int n_grps = 0, n_stats = 0;
0147 int len = 0;
0148
0149 if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask)) {
0150 n_stats += sizeof(struct ethtool_eth_phy_stats) / sizeof(u64);
0151 n_grps++;
0152 }
0153 if (test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask)) {
0154 n_stats += sizeof(struct ethtool_eth_mac_stats) / sizeof(u64);
0155 n_grps++;
0156 }
0157 if (test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask)) {
0158 n_stats += sizeof(struct ethtool_eth_ctrl_stats) / sizeof(u64);
0159 n_grps++;
0160 }
0161 if (test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask)) {
0162 n_stats += sizeof(struct ethtool_rmon_stats) / sizeof(u64);
0163 n_grps++;
0164
0165
0166 len += (nla_total_size(0) +
0167 nla_total_size(4) +
0168 nla_total_size(4)) *
0169 ETHTOOL_RMON_HIST_MAX * 2;
0170 }
0171
0172 len += n_grps * (nla_total_size(0) +
0173 nla_total_size(4) +
0174 nla_total_size(4));
0175 len += n_stats * (nla_total_size(0) +
0176 nla_total_size_64bit(sizeof(u64)));
0177
0178 return len;
0179 }
0180
0181 static int stat_put(struct sk_buff *skb, u16 attrtype, u64 val)
0182 {
0183 struct nlattr *nest;
0184 int ret;
0185
0186 if (val == ETHTOOL_STAT_NOT_SET)
0187 return 0;
0188
0189
0190
0191
0192
0193
0194
0195 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
0196 if (!IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
0197 if (!nla_reserve(skb, ETHTOOL_A_STATS_GRP_PAD, 0))
0198 return -EMSGSIZE;
0199 #endif
0200
0201 nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP_STAT);
0202 if (!nest)
0203 return -EMSGSIZE;
0204
0205 ret = nla_put_u64_64bit(skb, attrtype, val, -1 );
0206 if (ret) {
0207 nla_nest_cancel(skb, nest);
0208 return ret;
0209 }
0210
0211 nla_nest_end(skb, nest);
0212 return 0;
0213 }
0214
0215 static int stats_put_phy_stats(struct sk_buff *skb,
0216 const struct stats_reply_data *data)
0217 {
0218 if (stat_put(skb, ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR,
0219 data->phy_stats.SymbolErrorDuringCarrier))
0220 return -EMSGSIZE;
0221 return 0;
0222 }
0223
0224 static int stats_put_mac_stats(struct sk_buff *skb,
0225 const struct stats_reply_data *data)
0226 {
0227 if (stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_2_TX_PKT,
0228 data->mac_stats.FramesTransmittedOK) ||
0229 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_3_SINGLE_COL,
0230 data->mac_stats.SingleCollisionFrames) ||
0231 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_4_MULTI_COL,
0232 data->mac_stats.MultipleCollisionFrames) ||
0233 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_5_RX_PKT,
0234 data->mac_stats.FramesReceivedOK) ||
0235 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_6_FCS_ERR,
0236 data->mac_stats.FrameCheckSequenceErrors) ||
0237 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_7_ALIGN_ERR,
0238 data->mac_stats.AlignmentErrors) ||
0239 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_8_TX_BYTES,
0240 data->mac_stats.OctetsTransmittedOK) ||
0241 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_9_TX_DEFER,
0242 data->mac_stats.FramesWithDeferredXmissions) ||
0243 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_10_LATE_COL,
0244 data->mac_stats.LateCollisions) ||
0245 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_11_XS_COL,
0246 data->mac_stats.FramesAbortedDueToXSColls) ||
0247 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_12_TX_INT_ERR,
0248 data->mac_stats.FramesLostDueToIntMACXmitError) ||
0249 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_13_CS_ERR,
0250 data->mac_stats.CarrierSenseErrors) ||
0251 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_14_RX_BYTES,
0252 data->mac_stats.OctetsReceivedOK) ||
0253 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_15_RX_INT_ERR,
0254 data->mac_stats.FramesLostDueToIntMACRcvError) ||
0255 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_18_TX_MCAST,
0256 data->mac_stats.MulticastFramesXmittedOK) ||
0257 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_19_TX_BCAST,
0258 data->mac_stats.BroadcastFramesXmittedOK) ||
0259 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_20_XS_DEFER,
0260 data->mac_stats.FramesWithExcessiveDeferral) ||
0261 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_21_RX_MCAST,
0262 data->mac_stats.MulticastFramesReceivedOK) ||
0263 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_22_RX_BCAST,
0264 data->mac_stats.BroadcastFramesReceivedOK) ||
0265 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_23_IR_LEN_ERR,
0266 data->mac_stats.InRangeLengthErrors) ||
0267 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_24_OOR_LEN,
0268 data->mac_stats.OutOfRangeLengthField) ||
0269 stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_25_TOO_LONG_ERR,
0270 data->mac_stats.FrameTooLongErrors))
0271 return -EMSGSIZE;
0272 return 0;
0273 }
0274
0275 static int stats_put_ctrl_stats(struct sk_buff *skb,
0276 const struct stats_reply_data *data)
0277 {
0278 if (stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_3_TX,
0279 data->ctrl_stats.MACControlFramesTransmitted) ||
0280 stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_4_RX,
0281 data->ctrl_stats.MACControlFramesReceived) ||
0282 stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_5_RX_UNSUP,
0283 data->ctrl_stats.UnsupportedOpcodesReceived))
0284 return -EMSGSIZE;
0285 return 0;
0286 }
0287
0288 static int stats_put_rmon_hist(struct sk_buff *skb, u32 attr, const u64 *hist,
0289 const struct ethtool_rmon_hist_range *ranges)
0290 {
0291 struct nlattr *nest;
0292 int i;
0293
0294 if (!ranges)
0295 return 0;
0296
0297 for (i = 0; i < ETHTOOL_RMON_HIST_MAX; i++) {
0298 if (!ranges[i].low && !ranges[i].high)
0299 break;
0300 if (hist[i] == ETHTOOL_STAT_NOT_SET)
0301 continue;
0302
0303 nest = nla_nest_start(skb, attr);
0304 if (!nest)
0305 return -EMSGSIZE;
0306
0307 if (nla_put_u32(skb, ETHTOOL_A_STATS_GRP_HIST_BKT_LOW,
0308 ranges[i].low) ||
0309 nla_put_u32(skb, ETHTOOL_A_STATS_GRP_HIST_BKT_HI,
0310 ranges[i].high) ||
0311 nla_put_u64_64bit(skb, ETHTOOL_A_STATS_GRP_HIST_VAL,
0312 hist[i], ETHTOOL_A_STATS_GRP_PAD))
0313 goto err_cancel_hist;
0314
0315 nla_nest_end(skb, nest);
0316 }
0317
0318 return 0;
0319
0320 err_cancel_hist:
0321 nla_nest_cancel(skb, nest);
0322 return -EMSGSIZE;
0323 }
0324
0325 static int stats_put_rmon_stats(struct sk_buff *skb,
0326 const struct stats_reply_data *data)
0327 {
0328 if (stats_put_rmon_hist(skb, ETHTOOL_A_STATS_GRP_HIST_RX,
0329 data->rmon_stats.hist, data->rmon_ranges) ||
0330 stats_put_rmon_hist(skb, ETHTOOL_A_STATS_GRP_HIST_TX,
0331 data->rmon_stats.hist_tx, data->rmon_ranges))
0332 return -EMSGSIZE;
0333
0334 if (stat_put(skb, ETHTOOL_A_STATS_RMON_UNDERSIZE,
0335 data->rmon_stats.undersize_pkts) ||
0336 stat_put(skb, ETHTOOL_A_STATS_RMON_OVERSIZE,
0337 data->rmon_stats.oversize_pkts) ||
0338 stat_put(skb, ETHTOOL_A_STATS_RMON_FRAG,
0339 data->rmon_stats.fragments) ||
0340 stat_put(skb, ETHTOOL_A_STATS_RMON_JABBER,
0341 data->rmon_stats.jabbers))
0342 return -EMSGSIZE;
0343
0344 return 0;
0345 }
0346
0347 static int stats_put_stats(struct sk_buff *skb,
0348 const struct stats_reply_data *data,
0349 u32 id, u32 ss_id,
0350 int (*cb)(struct sk_buff *skb,
0351 const struct stats_reply_data *data))
0352 {
0353 struct nlattr *nest;
0354
0355 nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP);
0356 if (!nest)
0357 return -EMSGSIZE;
0358
0359 if (nla_put_u32(skb, ETHTOOL_A_STATS_GRP_ID, id) ||
0360 nla_put_u32(skb, ETHTOOL_A_STATS_GRP_SS_ID, ss_id))
0361 goto err_cancel;
0362
0363 if (cb(skb, data))
0364 goto err_cancel;
0365
0366 nla_nest_end(skb, nest);
0367 return 0;
0368
0369 err_cancel:
0370 nla_nest_cancel(skb, nest);
0371 return -EMSGSIZE;
0372 }
0373
0374 static int stats_fill_reply(struct sk_buff *skb,
0375 const struct ethnl_req_info *req_base,
0376 const struct ethnl_reply_data *reply_base)
0377 {
0378 const struct stats_req_info *req_info = STATS_REQINFO(req_base);
0379 const struct stats_reply_data *data = STATS_REPDATA(reply_base);
0380 int ret = 0;
0381
0382 if (!ret && test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask))
0383 ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_PHY,
0384 ETH_SS_STATS_ETH_PHY,
0385 stats_put_phy_stats);
0386 if (!ret && test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask))
0387 ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_MAC,
0388 ETH_SS_STATS_ETH_MAC,
0389 stats_put_mac_stats);
0390 if (!ret && test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask))
0391 ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_CTRL,
0392 ETH_SS_STATS_ETH_CTRL,
0393 stats_put_ctrl_stats);
0394 if (!ret && test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask))
0395 ret = stats_put_stats(skb, data, ETHTOOL_STATS_RMON,
0396 ETH_SS_STATS_RMON, stats_put_rmon_stats);
0397
0398 return ret;
0399 }
0400
0401 const struct ethnl_request_ops ethnl_stats_request_ops = {
0402 .request_cmd = ETHTOOL_MSG_STATS_GET,
0403 .reply_cmd = ETHTOOL_MSG_STATS_GET_REPLY,
0404 .hdr_attr = ETHTOOL_A_STATS_HEADER,
0405 .req_info_size = sizeof(struct stats_req_info),
0406 .reply_data_size = sizeof(struct stats_reply_data),
0407
0408 .parse_request = stats_parse_request,
0409 .prepare_data = stats_prepare_data,
0410 .reply_size = stats_reply_size,
0411 .fill_reply = stats_fill_reply,
0412 };