| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
|
| 3 |
#include <linux/net_tstamp.h> |
| 4 |
#include <linux/phy.h> |
| 5 |
#include <linux/phy_link_topology.h> |
| 6 |
#include <linux/ptp_clock_kernel.h> |
| 7 |
#include <net/netdev_lock.h> |
| 8 |
|
| 9 |
#include "netlink.h" |
| 10 |
#include "common.h" |
| 11 |
#include "bitset.h" |
| 12 |
#include "ts.h" |
| 13 |
|
| 14 |
struct tsinfo_req_info { |
| 15 |
struct ethnl_req_info base; |
| 16 |
struct hwtstamp_provider_desc hwprov_desc; |
| 17 |
}; |
| 18 |
|
| 19 |
struct tsinfo_reply_data { |
| 20 |
struct ethnl_reply_data base; |
| 21 |
struct kernel_ethtool_ts_info ts_info; |
| 22 |
struct ethtool_ts_stats stats; |
| 23 |
}; |
| 24 |
|
| 25 |
#define TSINFO_REQINFO(__req_base) \ |
| 26 |
container_of(__req_base, struct tsinfo_req_info, base) |
| 27 |
|
| 28 |
#define TSINFO_REPDATA(__reply_base) \ |
| 29 |
container_of(__reply_base, struct tsinfo_reply_data, base) |
| 30 |
|
| 31 |
#define ETHTOOL_TS_STAT_CNT \ |
| 32 |
(__ETHTOOL_A_TS_STAT_CNT - (ETHTOOL_A_TS_STAT_UNSPEC + 1)) |
| 33 |
|
| 34 |
const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_MAX + 1] = { |
| 35 |
[ETHTOOL_A_TSINFO_HEADER] = |
| 36 |
NLA_POLICY_NESTED(ethnl_header_policy_stats), |
| 37 |
[ETHTOOL_A_TSINFO_HWTSTAMP_PROVIDER] = |
| 38 |
NLA_POLICY_NESTED(ethnl_ts_hwtst_prov_policy), |
| 39 |
}; |
| 40 |
|
| 41 |
int ts_parse_hwtst_provider(const struct nlattr *nest, |
| 42 |
struct hwtstamp_provider_desc *hwprov_desc, |
| 43 |
struct netlink_ext_ack *extack, |
| 44 |
bool *mod) |
| 45 |
{ |
| 46 |
struct nlattr *tb[ARRAY_SIZE(ethnl_ts_hwtst_prov_policy)]; |
| 47 |
int ret; |
| 48 |
|
| 49 |
ret = nla_parse_nested(tb, |
| 50 |
ARRAY_SIZE(ethnl_ts_hwtst_prov_policy) - 1, |
| 51 |
nest, |
| 52 |
ethnl_ts_hwtst_prov_policy, extack); |
| 53 |
if (ret < 0) |
| 54 |
return ret; |
| 55 |
|
| 56 |
if (NL_REQ_ATTR_CHECK(extack, nest, tb, |
| 57 |
ETHTOOL_A_TS_HWTSTAMP_PROVIDER_INDEX) || |
| 58 |
NL_REQ_ATTR_CHECK(extack, nest, tb, |
| 59 |
ETHTOOL_A_TS_HWTSTAMP_PROVIDER_QUALIFIER)) |
| 60 |
return -EINVAL; |
| 61 |
|
| 62 |
ethnl_update_u32(&hwprov_desc->index, |
| 63 |
tb[ETHTOOL_A_TS_HWTSTAMP_PROVIDER_INDEX], |
| 64 |
mod); |
| 65 |
ethnl_update_u32(&hwprov_desc->qualifier, |
| 66 |
tb[ETHTOOL_A_TS_HWTSTAMP_PROVIDER_QUALIFIER], |
| 67 |
mod); |
| 68 |
|
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static int |
| 73 |
tsinfo_parse_request(struct ethnl_req_info *req_base, struct nlattr **tb, |
| 74 |
struct netlink_ext_ack *extack) |
| 75 |
{ |
| 76 |
struct tsinfo_req_info *req = TSINFO_REQINFO(req_base); |
| 77 |
bool mod = false; |
| 78 |
|
| 79 |
req->hwprov_desc.index = -1; |
| 80 |
|
| 81 |
if (!tb[ETHTOOL_A_TSINFO_HWTSTAMP_PROVIDER]) |
| 82 |
return 0; |
| 83 |
|
| 84 |
if (req_base->flags & ETHTOOL_FLAG_STATS) { |
| 85 |
NL_SET_ERR_MSG(extack, "can't query statistics for a provider"); |
| 86 |
return -EOPNOTSUPP; |
| 87 |
} |
| 88 |
|
| 89 |
return ts_parse_hwtst_provider(tb[ETHTOOL_A_TSINFO_HWTSTAMP_PROVIDER], |
| 90 |
&req->hwprov_desc, extack, &mod); |
| 91 |
} |
| 92 |
|
| 93 |
static int tsinfo_prepare_data(const struct ethnl_req_info *req_base, |
| 94 |
struct ethnl_reply_data *reply_base, |
| 95 |
const struct genl_info *info) |
| 96 |
{ |
| 97 |
struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base); |
| 98 |
struct tsinfo_req_info *req = TSINFO_REQINFO(req_base); |
| 99 |
struct net_device *dev = reply_base->dev; |
| 100 |
int ret; |
| 101 |
|
| 102 |
ret = ethnl_ops_begin(dev); |
| 103 |
if (ret < 0) |
| 104 |
return ret; |
| 105 |
|
| 106 |
if (req->hwprov_desc.index != -1) { |
| 107 |
ret = ethtool_get_ts_info_by_phc(dev, &data->ts_info, |
| 108 |
&req->hwprov_desc); |
| 109 |
ethnl_ops_complete(dev); |
| 110 |
return ret; |
| 111 |
} |
| 112 |
|
| 113 |
if (req_base->flags & ETHTOOL_FLAG_STATS) { |
| 114 |
ethtool_stats_init((u64 *)&data->stats, |
| 115 |
sizeof(data->stats) / sizeof(u64)); |
| 116 |
if (dev->ethtool_ops->get_ts_stats) |
| 117 |
dev->ethtool_ops->get_ts_stats(dev, &data->stats); |
| 118 |
} |
| 119 |
|
| 120 |
ret = __ethtool_get_ts_info(dev, &data->ts_info); |
| 121 |
ethnl_ops_complete(dev); |
| 122 |
|
| 123 |
return ret; |
| 124 |
} |
| 125 |
|
| 126 |
static int tsinfo_reply_size(const struct ethnl_req_info *req_base, |
| 127 |
const struct ethnl_reply_data *reply_base) |
| 128 |
{ |
| 129 |
const struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base); |
| 130 |
bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS; |
| 131 |
const struct kernel_ethtool_ts_info *ts_info = &data->ts_info; |
| 132 |
int len = 0; |
| 133 |
int ret; |
| 134 |
|
| 135 |
BUILD_BUG_ON(__SOF_TIMESTAMPING_CNT > 32); |
| 136 |
BUILD_BUG_ON(__HWTSTAMP_TX_CNT > 32); |
| 137 |
BUILD_BUG_ON(__HWTSTAMP_FILTER_CNT > 32); |
| 138 |
|
| 139 |
if (ts_info->so_timestamping) { |
| 140 |
ret = ethnl_bitset32_size(&ts_info->so_timestamping, NULL, |
| 141 |
__SOF_TIMESTAMPING_CNT, |
| 142 |
sof_timestamping_names, compact); |
| 143 |
if (ret < 0) |
| 144 |
return ret; |
| 145 |
len += ret; /* _TSINFO_TIMESTAMPING */ |
| 146 |
} |
| 147 |
if (ts_info->tx_types) { |
| 148 |
ret = ethnl_bitset32_size(&ts_info->tx_types, NULL, |
| 149 |
__HWTSTAMP_TX_CNT, |
| 150 |
ts_tx_type_names, compact); |
| 151 |
if (ret < 0) |
| 152 |
return ret; |
| 153 |
len += ret; /* _TSINFO_TX_TYPES */ |
| 154 |
} |
| 155 |
if (ts_info->rx_filters) { |
| 156 |
ret = ethnl_bitset32_size(&ts_info->rx_filters, NULL, |
| 157 |
__HWTSTAMP_FILTER_CNT, |
| 158 |
ts_rx_filter_names, compact); |
| 159 |
if (ret < 0) |
| 160 |
return ret; |
| 161 |
len += ret; /* _TSINFO_RX_FILTERS */ |
| 162 |
} |
| 163 |
if (ts_info->phc_index >= 0) { |
| 164 |
len += nla_total_size(sizeof(u32)); /* _TSINFO_PHC_INDEX */ |
| 165 |
/* _TSINFO_HWTSTAMP_PROVIDER */ |
| 166 |
len += nla_total_size(0) + 2 * nla_total_size(sizeof(u32)); |
| 167 |
} |
| 168 |
if (ts_info->phc_source) { |
| 169 |
len += nla_total_size(sizeof(u32)); /* _TSINFO_HWTSTAMP_SOURCE */ |
| 170 |
if (ts_info->phc_phyindex) |
| 171 |
/* _TSINFO_HWTSTAMP_PHYINDEX */ |
| 172 |
len += nla_total_size(sizeof(u32)); |
| 173 |
} |
| 174 |
if (req_base->flags & ETHTOOL_FLAG_STATS) |
| 175 |
len += nla_total_size(0) + /* _TSINFO_STATS */ |
| 176 |
nla_total_size_64bit(sizeof(u64)) * ETHTOOL_TS_STAT_CNT; |
| 177 |
|
| 178 |
return len; |
| 179 |
} |
| 180 |
|
| 181 |
static int tsinfo_put_stat(struct sk_buff *skb, u64 val, u16 attrtype) |
| 182 |
{ |
| 183 |
if (val == ETHTOOL_STAT_NOT_SET) |
| 184 |
return 0; |
| 185 |
if (nla_put_uint(skb, attrtype, val)) |
| 186 |
return -EMSGSIZE; |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static int tsinfo_put_stats(struct sk_buff *skb, |
| 191 |
const struct ethtool_ts_stats *stats) |
| 192 |
{ |
| 193 |
struct nlattr *nest; |
| 194 |
|
| 195 |
nest = nla_nest_start(skb, ETHTOOL_A_TSINFO_STATS); |
| 196 |
if (!nest) |
| 197 |
return -EMSGSIZE; |
| 198 |
|
| 199 |
if (tsinfo_put_stat(skb, stats->tx_stats.pkts, |
| 200 |
ETHTOOL_A_TS_STAT_TX_PKTS) || |
| 201 |
tsinfo_put_stat(skb, stats->tx_stats.onestep_pkts_unconfirmed, |
| 202 |
ETHTOOL_A_TS_STAT_TX_ONESTEP_PKTS_UNCONFIRMED) || |
| 203 |
tsinfo_put_stat(skb, stats->tx_stats.lost, |
| 204 |
ETHTOOL_A_TS_STAT_TX_LOST) || |
| 205 |
tsinfo_put_stat(skb, stats->tx_stats.err, |
| 206 |
ETHTOOL_A_TS_STAT_TX_ERR)) |
| 207 |
goto err_cancel; |
| 208 |
|
| 209 |
nla_nest_end(skb, nest); |
| 210 |
return 0; |
| 211 |
|
| 212 |
err_cancel: |
| 213 |
nla_nest_cancel(skb, nest); |
| 214 |
return -EMSGSIZE; |
| 215 |
} |
| 216 |
|
| 217 |
static int tsinfo_fill_reply(struct sk_buff *skb, |
| 218 |
const struct ethnl_req_info *req_base, |
| 219 |
const struct ethnl_reply_data *reply_base) |
| 220 |
{ |
| 221 |
const struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base); |
| 222 |
bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS; |
| 223 |
const struct kernel_ethtool_ts_info *ts_info = &data->ts_info; |
| 224 |
int ret; |
| 225 |
|
| 226 |
if (ts_info->so_timestamping) { |
| 227 |
ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_TIMESTAMPING, |
| 228 |
&ts_info->so_timestamping, NULL, |
| 229 |
__SOF_TIMESTAMPING_CNT, |
| 230 |
sof_timestamping_names, compact); |
| 231 |
if (ret < 0) |
| 232 |
return ret; |
| 233 |
} |
| 234 |
if (ts_info->tx_types) { |
| 235 |
ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_TX_TYPES, |
| 236 |
&ts_info->tx_types, NULL, |
| 237 |
__HWTSTAMP_TX_CNT, |
| 238 |
ts_tx_type_names, compact); |
| 239 |
if (ret < 0) |
| 240 |
return ret; |
| 241 |
} |
| 242 |
if (ts_info->rx_filters) { |
| 243 |
ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_RX_FILTERS, |
| 244 |
&ts_info->rx_filters, NULL, |
| 245 |
__HWTSTAMP_FILTER_CNT, |
| 246 |
ts_rx_filter_names, compact); |
| 247 |
if (ret < 0) |
| 248 |
return ret; |
| 249 |
} |
| 250 |
if (ts_info->phc_index >= 0) { |
| 251 |
struct nlattr *nest; |
| 252 |
|
| 253 |
ret = nla_put_u32(skb, ETHTOOL_A_TSINFO_PHC_INDEX, |
| 254 |
ts_info->phc_index); |
| 255 |
if (ret) |
| 256 |
return -EMSGSIZE; |
| 257 |
|
| 258 |
nest = nla_nest_start(skb, ETHTOOL_A_TSINFO_HWTSTAMP_PROVIDER); |
| 259 |
if (!nest) |
| 260 |
return -EMSGSIZE; |
| 261 |
|
| 262 |
if (nla_put_u32(skb, ETHTOOL_A_TS_HWTSTAMP_PROVIDER_INDEX, |
| 263 |
ts_info->phc_index) || |
| 264 |
nla_put_u32(skb, |
| 265 |
ETHTOOL_A_TS_HWTSTAMP_PROVIDER_QUALIFIER, |
| 266 |
ts_info->phc_qualifier)) { |
| 267 |
nla_nest_cancel(skb, nest); |
| 268 |
return -EMSGSIZE; |
| 269 |
} |
| 270 |
|
| 271 |
nla_nest_end(skb, nest); |
| 272 |
} |
| 273 |
if (ts_info->phc_source) { |
| 274 |
if (nla_put_u32(skb, ETHTOOL_A_TSINFO_HWTSTAMP_SOURCE, |
| 275 |
ts_info->phc_source)) |
| 276 |
return -EMSGSIZE; |
| 277 |
|
| 278 |
if (ts_info->phc_phyindex && |
| 279 |
nla_put_u32(skb, ETHTOOL_A_TSINFO_HWTSTAMP_PHYINDEX, |
| 280 |
ts_info->phc_phyindex)) |
| 281 |
return -EMSGSIZE; |
| 282 |
} |
| 283 |
if (req_base->flags & ETHTOOL_FLAG_STATS && |
| 284 |
tsinfo_put_stats(skb, &data->stats)) |
| 285 |
return -EMSGSIZE; |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
struct ethnl_tsinfo_dump_ctx { |
| 291 |
struct tsinfo_req_info *req_info; |
| 292 |
struct tsinfo_reply_data *reply_data; |
| 293 |
unsigned long pos_ifindex; |
| 294 |
bool netdev_dump_done; |
| 295 |
unsigned long pos_phyindex; |
| 296 |
enum hwtstamp_provider_qualifier pos_phcqualifier; |
| 297 |
}; |
| 298 |
|
| 299 |
static void *ethnl_tsinfo_prepare_dump(struct sk_buff *skb, |
| 300 |
struct net_device *dev, |
| 301 |
struct tsinfo_reply_data *reply_data, |
| 302 |
struct netlink_callback *cb) |
| 303 |
{ |
| 304 |
struct ethnl_tsinfo_dump_ctx *ctx = (void *)cb->ctx; |
| 305 |
void *ehdr = NULL; |
| 306 |
|
| 307 |
ehdr = ethnl_dump_put(skb, cb, |
| 308 |
ETHTOOL_MSG_TSINFO_GET_REPLY); |
| 309 |
if (!ehdr) |
| 310 |
return ERR_PTR(-EMSGSIZE); |
| 311 |
|
| 312 |
reply_data = ctx->reply_data; |
| 313 |
memset(reply_data, 0, sizeof(*reply_data)); |
| 314 |
reply_data->base.dev = dev; |
| 315 |
reply_data->ts_info.cmd = ETHTOOL_GET_TS_INFO; |
| 316 |
reply_data->ts_info.phc_index = -1; |
| 317 |
|
| 318 |
return ehdr; |
| 319 |
} |
| 320 |
|
| 321 |
static int ethnl_tsinfo_end_dump(struct sk_buff *skb, |
| 322 |
struct net_device *dev, |
| 323 |
struct tsinfo_req_info *req_info, |
| 324 |
struct tsinfo_reply_data *reply_data, |
| 325 |
void *ehdr) |
| 326 |
{ |
| 327 |
int ret; |
| 328 |
|
| 329 |
reply_data->ts_info.so_timestamping |= SOF_TIMESTAMPING_RX_SOFTWARE | |
| 330 |
SOF_TIMESTAMPING_SOFTWARE; |
| 331 |
|
| 332 |
ret = ethnl_fill_reply_header(skb, dev, ETHTOOL_A_TSINFO_HEADER); |
| 333 |
if (ret < 0) |
| 334 |
return ret; |
| 335 |
|
| 336 |
ret = tsinfo_fill_reply(skb, &req_info->base, &reply_data->base); |
| 337 |
if (ret < 0) |
| 338 |
return ret; |
| 339 |
|
| 340 |
reply_data->base.dev = NULL; |
| 341 |
genlmsg_end(skb, ehdr); |
| 342 |
|
| 343 |
return ret; |
| 344 |
} |
| 345 |
|
| 346 |
static int ethnl_tsinfo_dump_one_phydev(struct sk_buff *skb, |
| 347 |
struct net_device *dev, |
| 348 |
struct phy_device *phydev, |
| 349 |
struct netlink_callback *cb) |
| 350 |
{ |
| 351 |
struct ethnl_tsinfo_dump_ctx *ctx = (void *)cb->ctx; |
| 352 |
struct tsinfo_reply_data *reply_data; |
| 353 |
struct tsinfo_req_info *req_info; |
| 354 |
void *ehdr = NULL; |
| 355 |
int ret = 0; |
| 356 |
|
| 357 |
if (!phy_has_tsinfo(phydev)) |
| 358 |
return -EOPNOTSUPP; |
| 359 |
|
| 360 |
reply_data = ctx->reply_data; |
| 361 |
req_info = ctx->req_info; |
| 362 |
ehdr = ethnl_tsinfo_prepare_dump(skb, dev, reply_data, cb); |
| 363 |
if (IS_ERR(ehdr)) |
| 364 |
return PTR_ERR(ehdr); |
| 365 |
|
| 366 |
ret = phy_ts_info(phydev, &reply_data->ts_info); |
| 367 |
if (ret < 0) |
| 368 |
goto err; |
| 369 |
|
| 370 |
if (reply_data->ts_info.phc_index >= 0) { |
| 371 |
reply_data->ts_info.phc_source = HWTSTAMP_SOURCE_PHYLIB; |
| 372 |
reply_data->ts_info.phc_phyindex = phydev->phyindex; |
| 373 |
} |
| 374 |
|
| 375 |
ret = ethnl_tsinfo_end_dump(skb, dev, req_info, reply_data, ehdr); |
| 376 |
if (ret < 0) |
| 377 |
goto err; |
| 378 |
|
| 379 |
return ret; |
| 380 |
err: |
| 381 |
genlmsg_cancel(skb, ehdr); |
| 382 |
return ret; |
| 383 |
} |
| 384 |
|
| 385 |
static int ethnl_tsinfo_dump_one_netdev(struct sk_buff *skb, |
| 386 |
struct net_device *dev, |
| 387 |
struct netlink_callback *cb) |
| 388 |
{ |
| 389 |
struct ethnl_tsinfo_dump_ctx *ctx = (void *)cb->ctx; |
| 390 |
const struct ethtool_ops *ops = dev->ethtool_ops; |
| 391 |
struct tsinfo_reply_data *reply_data; |
| 392 |
struct tsinfo_req_info *req_info; |
| 393 |
void *ehdr = NULL; |
| 394 |
int ret = 0; |
| 395 |
|
| 396 |
if (!ops->get_ts_info) |
| 397 |
return -EOPNOTSUPP; |
| 398 |
|
| 399 |
reply_data = ctx->reply_data; |
| 400 |
req_info = ctx->req_info; |
| 401 |
for (; ctx->pos_phcqualifier < HWTSTAMP_PROVIDER_QUALIFIER_CNT; |
| 402 |
ctx->pos_phcqualifier++) { |
| 403 |
if (!net_support_hwtstamp_qualifier(dev, |
| 404 |
ctx->pos_phcqualifier)) |
| 405 |
continue; |
| 406 |
|
| 407 |
ehdr = ethnl_tsinfo_prepare_dump(skb, dev, reply_data, cb); |
| 408 |
if (IS_ERR(ehdr)) |
| 409 |
return PTR_ERR(ehdr); |
| 410 |
|
| 411 |
reply_data->ts_info.phc_qualifier = ctx->pos_phcqualifier; |
| 412 |
ret = ops->get_ts_info(dev, &reply_data->ts_info); |
| 413 |
if (ret < 0) |
| 414 |
goto err; |
| 415 |
|
| 416 |
if (reply_data->ts_info.phc_index >= 0) |
| 417 |
reply_data->ts_info.phc_source = HWTSTAMP_SOURCE_NETDEV; |
| 418 |
ret = ethnl_tsinfo_end_dump(skb, dev, req_info, reply_data, |
| 419 |
ehdr); |
| 420 |
if (ret < 0) |
| 421 |
goto err; |
| 422 |
} |
| 423 |
|
| 424 |
return ret; |
| 425 |
|
| 426 |
err: |
| 427 |
genlmsg_cancel(skb, ehdr); |
| 428 |
return ret; |
| 429 |
} |
| 430 |
|
| 431 |
static int ethnl_tsinfo_dump_one_net_topo(struct sk_buff *skb, |
| 432 |
struct net_device *dev, |
| 433 |
struct netlink_callback *cb) |
| 434 |
{ |
| 435 |
struct ethnl_tsinfo_dump_ctx *ctx = (void *)cb->ctx; |
| 436 |
struct phy_device_node *pdn; |
| 437 |
int ret = 0; |
| 438 |
|
| 439 |
if (!ctx->netdev_dump_done) { |
| 440 |
ret = ethnl_tsinfo_dump_one_netdev(skb, dev, cb); |
| 441 |
if (ret < 0 && ret != -EOPNOTSUPP) |
| 442 |
return ret; |
| 443 |
ctx->netdev_dump_done = true; |
| 444 |
} |
| 445 |
|
| 446 |
if (!dev->link_topo) { |
| 447 |
if (phy_has_tsinfo(dev->phydev)) { |
| 448 |
ret = ethnl_tsinfo_dump_one_phydev(skb, dev, |
| 449 |
dev->phydev, cb); |
| 450 |
if (ret < 0 && ret != -EOPNOTSUPP) |
| 451 |
return ret; |
| 452 |
} |
| 453 |
|
| 454 |
return 0; |
| 455 |
} |
| 456 |
|
| 457 |
xa_for_each_start(&dev->link_topo->phys, ctx->pos_phyindex, pdn, |
| 458 |
ctx->pos_phyindex) { |
| 459 |
if (phy_has_tsinfo(pdn->phy)) { |
| 460 |
ret = ethnl_tsinfo_dump_one_phydev(skb, dev, |
| 461 |
pdn->phy, cb); |
| 462 |
if (ret < 0 && ret != -EOPNOTSUPP) |
| 463 |
return ret; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
return ret; |
| 468 |
} |
| 469 |
|
| 470 |
int ethnl_tsinfo_dumpit(struct sk_buff *skb, struct netlink_callback *cb) |
| 471 |
{ |
| 472 |
struct ethnl_tsinfo_dump_ctx *ctx = (void *)cb->ctx; |
| 473 |
struct net *net = sock_net(skb->sk); |
| 474 |
struct net_device *dev; |
| 475 |
int ret = 0; |
| 476 |
|
| 477 |
rtnl_lock(); |
| 478 |
if (ctx->req_info->base.dev) { |
| 479 |
dev = ctx->req_info->base.dev; |
| 480 |
netdev_lock_ops(dev); |
| 481 |
ret = ethnl_tsinfo_dump_one_net_topo(skb, dev, cb); |
| 482 |
netdev_unlock_ops(dev); |
| 483 |
} else { |
| 484 |
for_each_netdev_dump(net, dev, ctx->pos_ifindex) { |
| 485 |
netdev_lock_ops(dev); |
| 486 |
ret = ethnl_tsinfo_dump_one_net_topo(skb, dev, cb); |
| 487 |
netdev_unlock_ops(dev); |
| 488 |
if (ret < 0 && ret != -EOPNOTSUPP) |
| 489 |
break; |
| 490 |
ctx->pos_phyindex = 0; |
| 491 |
ctx->netdev_dump_done = false; |
| 492 |
ctx->pos_phcqualifier = HWTSTAMP_PROVIDER_QUALIFIER_PRECISE; |
| 493 |
} |
| 494 |
} |
| 495 |
rtnl_unlock(); |
| 496 |
|
| 497 |
return ret; |
| 498 |
} |
| 499 |
|
| 500 |
int ethnl_tsinfo_start(struct netlink_callback *cb) |
| 501 |
{ |
| 502 |
const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
| 503 |
struct ethnl_tsinfo_dump_ctx *ctx = (void *)cb->ctx; |
| 504 |
struct nlattr **tb = info->info.attrs; |
| 505 |
struct tsinfo_reply_data *reply_data; |
| 506 |
struct tsinfo_req_info *req_info; |
| 507 |
int ret; |
| 508 |
|
| 509 |
BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); |
| 510 |
|
| 511 |
req_info = kzalloc_obj(*req_info); |
| 512 |
if (!req_info) |
| 513 |
return -ENOMEM; |
| 514 |
reply_data = kzalloc_obj(*reply_data); |
| 515 |
if (!reply_data) { |
| 516 |
ret = -ENOMEM; |
| 517 |
goto free_req_info; |
| 518 |
} |
| 519 |
|
| 520 |
ret = ethnl_parse_header_dev_get(&req_info->base, |
| 521 |
tb[ETHTOOL_A_TSINFO_HEADER], |
| 522 |
sock_net(cb->skb->sk), cb->extack, |
| 523 |
false); |
| 524 |
if (ret < 0) |
| 525 |
goto free_reply_data; |
| 526 |
|
| 527 |
if (req_info->base.flags & ETHTOOL_FLAG_STATS) { |
| 528 |
NL_SET_ERR_MSG(cb->extack, "stats not supported in dump"); |
| 529 |
ret = -EOPNOTSUPP; |
| 530 |
goto err_dev_put; |
| 531 |
} |
| 532 |
|
| 533 |
ctx->req_info = req_info; |
| 534 |
ctx->reply_data = reply_data; |
| 535 |
ctx->pos_ifindex = 0; |
| 536 |
ctx->pos_phyindex = 0; |
| 537 |
ctx->netdev_dump_done = false; |
| 538 |
ctx->pos_phcqualifier = HWTSTAMP_PROVIDER_QUALIFIER_PRECISE; |
| 539 |
|
| 540 |
return 0; |
| 541 |
|
| 542 |
err_dev_put: |
| 543 |
ethnl_parse_header_dev_put(&req_info->base); |
| 544 |
free_reply_data: |
| 545 |
kfree(reply_data); |
| 546 |
free_req_info: |
| 547 |
kfree(req_info); |
| 548 |
|
| 549 |
return ret; |
| 550 |
} |
| 551 |
|
| 552 |
int ethnl_tsinfo_done(struct netlink_callback *cb) |
| 553 |
{ |
| 554 |
struct ethnl_tsinfo_dump_ctx *ctx = (void *)cb->ctx; |
| 555 |
struct tsinfo_req_info *req_info = ctx->req_info; |
| 556 |
|
| 557 |
ethnl_parse_header_dev_put(&req_info->base); |
| 558 |
kfree(ctx->reply_data); |
| 559 |
kfree(ctx->req_info); |
| 560 |
|
| 561 |
return 0; |
| 562 |
} |
| 563 |
|
| 564 |
const struct ethnl_request_ops ethnl_tsinfo_request_ops = { |
| 565 |
.request_cmd = ETHTOOL_MSG_TSINFO_GET, |
| 566 |
.reply_cmd = ETHTOOL_MSG_TSINFO_GET_REPLY, |
| 567 |
.hdr_attr = ETHTOOL_A_TSINFO_HEADER, |
| 568 |
.req_info_size = sizeof(struct tsinfo_req_info), |
| 569 |
.reply_data_size = sizeof(struct tsinfo_reply_data), |
| 570 |
|
| 571 |
.parse_request = tsinfo_parse_request, |
| 572 |
.prepare_data = tsinfo_prepare_data, |
| 573 |
.reply_size = tsinfo_reply_size, |
| 574 |
.fill_reply = tsinfo_fill_reply, |
| 575 |
}; |