1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/netdevice.h>
4 #include <linux/notifier.h>
5 #include <linux/rtnetlink.h>
6 #include <net/net_namespace.h>
9 #include <net/xdp_sock.h>
10 #include <net/netdev_rx_queue.h>
11 #include <net/netdev_queues.h>
12 #include <net/busy_poll.h>
14 #include "netdev-genl-gen.h"
17 struct netdev_nl_dump_ctx {
18 unsigned long ifindex;
24 static struct netdev_nl_dump_ctx *netdev_dump_ctx(struct netlink_callback *cb)
26 NL_ASSERT_DUMP_CTX_FITS(struct netdev_nl_dump_ctx);
28 return (struct netdev_nl_dump_ctx *)cb->ctx;
32 netdev_nl_dev_fill(struct net_device *netdev, struct sk_buff *rsp,
33 const struct genl_info *info)
39 hdr = genlmsg_iput(rsp, info);
43 #define XDP_METADATA_KFUNC(_, flag, __, xmo) \
44 if (netdev->xdp_metadata_ops && netdev->xdp_metadata_ops->xmo) \
46 XDP_METADATA_KFUNC_xxx
47 #undef XDP_METADATA_KFUNC
49 if (netdev->xsk_tx_metadata_ops) {
50 if (netdev->xsk_tx_metadata_ops->tmo_fill_timestamp)
51 xsk_features |= NETDEV_XSK_FLAGS_TX_TIMESTAMP;
52 if (netdev->xsk_tx_metadata_ops->tmo_request_checksum)
53 xsk_features |= NETDEV_XSK_FLAGS_TX_CHECKSUM;
56 if (nla_put_u32(rsp, NETDEV_A_DEV_IFINDEX, netdev->ifindex) ||
57 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_FEATURES,
58 netdev->xdp_features, NETDEV_A_DEV_PAD) ||
59 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_RX_METADATA_FEATURES,
60 xdp_rx_meta, NETDEV_A_DEV_PAD) ||
61 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XSK_FEATURES,
62 xsk_features, NETDEV_A_DEV_PAD)) {
63 genlmsg_cancel(rsp, hdr);
67 if (netdev->xdp_features & NETDEV_XDP_ACT_XSK_ZEROCOPY) {
68 if (nla_put_u32(rsp, NETDEV_A_DEV_XDP_ZC_MAX_SEGS,
69 netdev->xdp_zc_max_segs)) {
70 genlmsg_cancel(rsp, hdr);
75 genlmsg_end(rsp, hdr);
81 netdev_genl_dev_notify(struct net_device *netdev, int cmd)
83 struct genl_info info;
86 if (!genl_has_listeners(&netdev_nl_family, dev_net(netdev),
90 genl_info_init_ntf(&info, &netdev_nl_family, cmd);
92 ntf = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
96 if (netdev_nl_dev_fill(netdev, ntf, &info)) {
101 genlmsg_multicast_netns(&netdev_nl_family, dev_net(netdev), ntf,
102 0, NETDEV_NLGRP_MGMT, GFP_KERNEL);
105 int netdev_nl_dev_get_doit(struct sk_buff *skb, struct genl_info *info)
107 struct net_device *netdev;
112 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_DEV_IFINDEX))
115 ifindex = nla_get_u32(info->attrs[NETDEV_A_DEV_IFINDEX]);
117 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
123 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
125 err = netdev_nl_dev_fill(netdev, rsp, info);
134 return genlmsg_reply(rsp, info);
141 int netdev_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
143 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb);
144 struct net *net = sock_net(skb->sk);
145 struct net_device *netdev;
149 for_each_netdev_dump(net, netdev, ctx->ifindex) {
150 err = netdev_nl_dev_fill(netdev, skb, genl_info_dump(cb));
160 netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,
161 const struct genl_info *info)
166 if (WARN_ON_ONCE(!napi->dev))
168 if (!(napi->dev->flags & IFF_UP))
171 hdr = genlmsg_iput(rsp, info);
175 if (napi->napi_id >= MIN_NAPI_ID &&
176 nla_put_u32(rsp, NETDEV_A_NAPI_ID, napi->napi_id))
177 goto nla_put_failure;
179 if (nla_put_u32(rsp, NETDEV_A_NAPI_IFINDEX, napi->dev->ifindex))
180 goto nla_put_failure;
182 if (napi->irq >= 0 && nla_put_u32(rsp, NETDEV_A_NAPI_IRQ, napi->irq))
183 goto nla_put_failure;
186 pid = task_pid_nr(napi->thread);
187 if (nla_put_u32(rsp, NETDEV_A_NAPI_PID, pid))
188 goto nla_put_failure;
191 genlmsg_end(rsp, hdr);
196 genlmsg_cancel(rsp, hdr);
200 int netdev_nl_napi_get_doit(struct sk_buff *skb, struct genl_info *info)
202 struct napi_struct *napi;
207 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_NAPI_ID))
210 napi_id = nla_get_u32(info->attrs[NETDEV_A_NAPI_ID]);
212 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
218 napi = napi_by_id(napi_id);
220 err = netdev_nl_napi_fill_one(rsp, napi, info);
229 return genlmsg_reply(rsp, info);
237 netdev_nl_napi_dump_one(struct net_device *netdev, struct sk_buff *rsp,
238 const struct genl_info *info,
239 struct netdev_nl_dump_ctx *ctx)
241 struct napi_struct *napi;
244 if (!(netdev->flags & IFF_UP))
247 list_for_each_entry(napi, &netdev->napi_list, dev_list) {
248 if (ctx->napi_id && napi->napi_id >= ctx->napi_id)
251 err = netdev_nl_napi_fill_one(rsp, napi, info);
254 ctx->napi_id = napi->napi_id;
259 int netdev_nl_napi_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
261 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb);
262 const struct genl_info *info = genl_info_dump(cb);
263 struct net *net = sock_net(skb->sk);
264 struct net_device *netdev;
268 if (info->attrs[NETDEV_A_NAPI_IFINDEX])
269 ifindex = nla_get_u32(info->attrs[NETDEV_A_NAPI_IFINDEX]);
273 netdev = __dev_get_by_index(net, ifindex);
275 err = netdev_nl_napi_dump_one(netdev, skb, info, ctx);
279 for_each_netdev_dump(net, netdev, ctx->ifindex) {
280 err = netdev_nl_napi_dump_one(netdev, skb, info, ctx);
292 netdev_nl_queue_fill_one(struct sk_buff *rsp, struct net_device *netdev,
293 u32 q_idx, u32 q_type, const struct genl_info *info)
295 struct netdev_rx_queue *rxq;
296 struct netdev_queue *txq;
299 hdr = genlmsg_iput(rsp, info);
303 if (nla_put_u32(rsp, NETDEV_A_QUEUE_ID, q_idx) ||
304 nla_put_u32(rsp, NETDEV_A_QUEUE_TYPE, q_type) ||
305 nla_put_u32(rsp, NETDEV_A_QUEUE_IFINDEX, netdev->ifindex))
306 goto nla_put_failure;
309 case NETDEV_QUEUE_TYPE_RX:
310 rxq = __netif_get_rx_queue(netdev, q_idx);
311 if (rxq->napi && nla_put_u32(rsp, NETDEV_A_QUEUE_NAPI_ID,
313 goto nla_put_failure;
315 case NETDEV_QUEUE_TYPE_TX:
316 txq = netdev_get_tx_queue(netdev, q_idx);
317 if (txq->napi && nla_put_u32(rsp, NETDEV_A_QUEUE_NAPI_ID,
319 goto nla_put_failure;
322 genlmsg_end(rsp, hdr);
327 genlmsg_cancel(rsp, hdr);
331 static int netdev_nl_queue_validate(struct net_device *netdev, u32 q_id,
335 case NETDEV_QUEUE_TYPE_RX:
336 if (q_id >= netdev->real_num_rx_queues)
339 case NETDEV_QUEUE_TYPE_TX:
340 if (q_id >= netdev->real_num_tx_queues)
347 netdev_nl_queue_fill(struct sk_buff *rsp, struct net_device *netdev, u32 q_idx,
348 u32 q_type, const struct genl_info *info)
352 if (!(netdev->flags & IFF_UP))
355 err = netdev_nl_queue_validate(netdev, q_idx, q_type);
359 return netdev_nl_queue_fill_one(rsp, netdev, q_idx, q_type, info);
362 int netdev_nl_queue_get_doit(struct sk_buff *skb, struct genl_info *info)
364 u32 q_id, q_type, ifindex;
365 struct net_device *netdev;
369 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_ID) ||
370 GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_TYPE) ||
371 GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_IFINDEX))
374 q_id = nla_get_u32(info->attrs[NETDEV_A_QUEUE_ID]);
375 q_type = nla_get_u32(info->attrs[NETDEV_A_QUEUE_TYPE]);
376 ifindex = nla_get_u32(info->attrs[NETDEV_A_QUEUE_IFINDEX]);
378 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
384 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
386 err = netdev_nl_queue_fill(rsp, netdev, q_id, q_type, info);
395 return genlmsg_reply(rsp, info);
403 netdev_nl_queue_dump_one(struct net_device *netdev, struct sk_buff *rsp,
404 const struct genl_info *info,
405 struct netdev_nl_dump_ctx *ctx)
410 if (!(netdev->flags & IFF_UP))
413 for (i = ctx->rxq_idx; i < netdev->real_num_rx_queues;) {
414 err = netdev_nl_queue_fill_one(rsp, netdev, i,
415 NETDEV_QUEUE_TYPE_RX, info);
420 for (i = ctx->txq_idx; i < netdev->real_num_tx_queues;) {
421 err = netdev_nl_queue_fill_one(rsp, netdev, i,
422 NETDEV_QUEUE_TYPE_TX, info);
431 int netdev_nl_queue_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
433 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb);
434 const struct genl_info *info = genl_info_dump(cb);
435 struct net *net = sock_net(skb->sk);
436 struct net_device *netdev;
440 if (info->attrs[NETDEV_A_QUEUE_IFINDEX])
441 ifindex = nla_get_u32(info->attrs[NETDEV_A_QUEUE_IFINDEX]);
445 netdev = __dev_get_by_index(net, ifindex);
447 err = netdev_nl_queue_dump_one(netdev, skb, info, ctx);
451 for_each_netdev_dump(net, netdev, ctx->ifindex) {
452 err = netdev_nl_queue_dump_one(netdev, skb, info, ctx);
464 #define NETDEV_STAT_NOT_SET (~0ULL)
466 static void netdev_nl_stats_add(void *_sum, const void *_add, size_t size)
468 const u64 *add = _add;
472 if (*add != NETDEV_STAT_NOT_SET && *sum != NETDEV_STAT_NOT_SET)
480 static int netdev_stat_put(struct sk_buff *rsp, unsigned int attr_id, u64 value)
482 if (value == NETDEV_STAT_NOT_SET)
484 return nla_put_uint(rsp, attr_id, value);
488 netdev_nl_stats_write_rx(struct sk_buff *rsp, struct netdev_queue_stats_rx *rx)
490 if (netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_PACKETS, rx->packets) ||
491 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_BYTES, rx->bytes) ||
492 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_ALLOC_FAIL, rx->alloc_fail))
498 netdev_nl_stats_write_tx(struct sk_buff *rsp, struct netdev_queue_stats_tx *tx)
500 if (netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_PACKETS, tx->packets) ||
501 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_BYTES, tx->bytes))
507 netdev_nl_stats_queue(struct net_device *netdev, struct sk_buff *rsp,
508 u32 q_type, int i, const struct genl_info *info)
510 const struct netdev_stat_ops *ops = netdev->stat_ops;
511 struct netdev_queue_stats_rx rx;
512 struct netdev_queue_stats_tx tx;
515 hdr = genlmsg_iput(rsp, info);
518 if (nla_put_u32(rsp, NETDEV_A_QSTATS_IFINDEX, netdev->ifindex) ||
519 nla_put_u32(rsp, NETDEV_A_QSTATS_QUEUE_TYPE, q_type) ||
520 nla_put_u32(rsp, NETDEV_A_QSTATS_QUEUE_ID, i))
521 goto nla_put_failure;
524 case NETDEV_QUEUE_TYPE_RX:
525 memset(&rx, 0xff, sizeof(rx));
526 ops->get_queue_stats_rx(netdev, i, &rx);
527 if (!memchr_inv(&rx, 0xff, sizeof(rx)))
529 if (netdev_nl_stats_write_rx(rsp, &rx))
530 goto nla_put_failure;
532 case NETDEV_QUEUE_TYPE_TX:
533 memset(&tx, 0xff, sizeof(tx));
534 ops->get_queue_stats_tx(netdev, i, &tx);
535 if (!memchr_inv(&tx, 0xff, sizeof(tx)))
537 if (netdev_nl_stats_write_tx(rsp, &tx))
538 goto nla_put_failure;
542 genlmsg_end(rsp, hdr);
546 genlmsg_cancel(rsp, hdr);
549 genlmsg_cancel(rsp, hdr);
554 netdev_nl_stats_by_queue(struct net_device *netdev, struct sk_buff *rsp,
555 const struct genl_info *info,
556 struct netdev_nl_dump_ctx *ctx)
558 const struct netdev_stat_ops *ops = netdev->stat_ops;
561 if (!(netdev->flags & IFF_UP))
565 while (ops->get_queue_stats_rx && i < netdev->real_num_rx_queues) {
566 err = netdev_nl_stats_queue(netdev, rsp, NETDEV_QUEUE_TYPE_RX,
573 while (ops->get_queue_stats_tx && i < netdev->real_num_tx_queues) {
574 err = netdev_nl_stats_queue(netdev, rsp, NETDEV_QUEUE_TYPE_TX,
587 netdev_nl_stats_by_netdev(struct net_device *netdev, struct sk_buff *rsp,
588 const struct genl_info *info)
590 struct netdev_queue_stats_rx rx_sum, rx;
591 struct netdev_queue_stats_tx tx_sum, tx;
592 const struct netdev_stat_ops *ops;
596 ops = netdev->stat_ops;
597 /* Netdev can't guarantee any complete counters */
598 if (!ops->get_base_stats)
601 memset(&rx_sum, 0xff, sizeof(rx_sum));
602 memset(&tx_sum, 0xff, sizeof(tx_sum));
604 ops->get_base_stats(netdev, &rx_sum, &tx_sum);
606 /* The op was there, but nothing reported, don't bother */
607 if (!memchr_inv(&rx_sum, 0xff, sizeof(rx_sum)) &&
608 !memchr_inv(&tx_sum, 0xff, sizeof(tx_sum)))
611 hdr = genlmsg_iput(rsp, info);
614 if (nla_put_u32(rsp, NETDEV_A_QSTATS_IFINDEX, netdev->ifindex))
615 goto nla_put_failure;
617 for (i = 0; i < netdev->real_num_rx_queues; i++) {
618 memset(&rx, 0xff, sizeof(rx));
619 if (ops->get_queue_stats_rx)
620 ops->get_queue_stats_rx(netdev, i, &rx);
621 netdev_nl_stats_add(&rx_sum, &rx, sizeof(rx));
623 for (i = 0; i < netdev->real_num_tx_queues; i++) {
624 memset(&tx, 0xff, sizeof(tx));
625 if (ops->get_queue_stats_tx)
626 ops->get_queue_stats_tx(netdev, i, &tx);
627 netdev_nl_stats_add(&tx_sum, &tx, sizeof(tx));
630 if (netdev_nl_stats_write_rx(rsp, &rx_sum) ||
631 netdev_nl_stats_write_tx(rsp, &tx_sum))
632 goto nla_put_failure;
634 genlmsg_end(rsp, hdr);
638 genlmsg_cancel(rsp, hdr);
642 int netdev_nl_qstats_get_dumpit(struct sk_buff *skb,
643 struct netlink_callback *cb)
645 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb);
646 const struct genl_info *info = genl_info_dump(cb);
647 struct net *net = sock_net(skb->sk);
648 struct net_device *netdev;
653 if (info->attrs[NETDEV_A_QSTATS_SCOPE])
654 scope = nla_get_uint(info->attrs[NETDEV_A_QSTATS_SCOPE]);
657 for_each_netdev_dump(net, netdev, ctx->ifindex) {
658 if (!netdev->stat_ops)
663 err = netdev_nl_stats_by_netdev(netdev, skb, info);
665 case NETDEV_QSTATS_SCOPE_QUEUE:
666 err = netdev_nl_stats_by_queue(netdev, skb, info, ctx);
677 static int netdev_genl_netdevice_event(struct notifier_block *nb,
678 unsigned long event, void *ptr)
680 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
683 case NETDEV_REGISTER:
684 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_ADD_NTF);
686 case NETDEV_UNREGISTER:
687 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_DEL_NTF);
689 case NETDEV_XDP_FEAT_CHANGE:
690 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_CHANGE_NTF);
697 static struct notifier_block netdev_genl_nb = {
698 .notifier_call = netdev_genl_netdevice_event,
701 static int __init netdev_genl_init(void)
705 err = register_netdevice_notifier(&netdev_genl_nb);
709 err = genl_register_family(&netdev_nl_family);
716 unregister_netdevice_notifier(&netdev_genl_nb);
720 subsys_initcall(netdev_genl_init);