1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/netdevice.h> 4 #include <linux/notifier.h> 5 #include <linux/rtnetlink.h> 6 #include <net/busy_poll.h> 7 #include <net/net_namespace.h> 8 #include <net/netdev_queues.h> 9 #include <net/netdev_rx_queue.h> 10 #include <net/sock.h> 11 #include <net/xdp.h> 12 #include <net/xdp_sock.h> 13 14 #include "dev.h" 15 #include "devmem.h" 16 #include "netdev-genl-gen.h" 17 18 struct netdev_nl_dump_ctx { 19 unsigned long ifindex; 20 unsigned int rxq_idx; 21 unsigned int txq_idx; 22 unsigned int napi_id; 23 }; 24 25 static struct netdev_nl_dump_ctx *netdev_dump_ctx(struct netlink_callback *cb) 26 { 27 NL_ASSERT_CTX_FITS(struct netdev_nl_dump_ctx); 28 29 return (struct netdev_nl_dump_ctx *)cb->ctx; 30 } 31 32 static int 33 netdev_nl_dev_fill(struct net_device *netdev, struct sk_buff *rsp, 34 const struct genl_info *info) 35 { 36 u64 xsk_features = 0; 37 u64 xdp_rx_meta = 0; 38 void *hdr; 39 40 hdr = genlmsg_iput(rsp, info); 41 if (!hdr) 42 return -EMSGSIZE; 43 44 #define XDP_METADATA_KFUNC(_, flag, __, xmo) \ 45 if (netdev->xdp_metadata_ops && netdev->xdp_metadata_ops->xmo) \ 46 xdp_rx_meta |= flag; 47 XDP_METADATA_KFUNC_xxx 48 #undef XDP_METADATA_KFUNC 49 50 if (netdev->xsk_tx_metadata_ops) { 51 if (netdev->xsk_tx_metadata_ops->tmo_fill_timestamp) 52 xsk_features |= NETDEV_XSK_FLAGS_TX_TIMESTAMP; 53 if (netdev->xsk_tx_metadata_ops->tmo_request_checksum) 54 xsk_features |= NETDEV_XSK_FLAGS_TX_CHECKSUM; 55 } 56 57 if (nla_put_u32(rsp, NETDEV_A_DEV_IFINDEX, netdev->ifindex) || 58 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_FEATURES, 59 netdev->xdp_features, NETDEV_A_DEV_PAD) || 60 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_RX_METADATA_FEATURES, 61 xdp_rx_meta, NETDEV_A_DEV_PAD) || 62 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XSK_FEATURES, 63 xsk_features, NETDEV_A_DEV_PAD)) 64 goto err_cancel_msg; 65 66 if (netdev->xdp_features & NETDEV_XDP_ACT_XSK_ZEROCOPY) { 67 if (nla_put_u32(rsp, NETDEV_A_DEV_XDP_ZC_MAX_SEGS, 68 netdev->xdp_zc_max_segs)) 69 goto err_cancel_msg; 70 } 71 72 genlmsg_end(rsp, hdr); 73 74 return 0; 75 76 err_cancel_msg: 77 genlmsg_cancel(rsp, hdr); 78 return -EMSGSIZE; 79 } 80 81 static void 82 netdev_genl_dev_notify(struct net_device *netdev, int cmd) 83 { 84 struct genl_info info; 85 struct sk_buff *ntf; 86 87 if (!genl_has_listeners(&netdev_nl_family, dev_net(netdev), 88 NETDEV_NLGRP_MGMT)) 89 return; 90 91 genl_info_init_ntf(&info, &netdev_nl_family, cmd); 92 93 ntf = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 94 if (!ntf) 95 return; 96 97 if (netdev_nl_dev_fill(netdev, ntf, &info)) { 98 nlmsg_free(ntf); 99 return; 100 } 101 102 genlmsg_multicast_netns(&netdev_nl_family, dev_net(netdev), ntf, 103 0, NETDEV_NLGRP_MGMT, GFP_KERNEL); 104 } 105 106 int netdev_nl_dev_get_doit(struct sk_buff *skb, struct genl_info *info) 107 { 108 struct net_device *netdev; 109 struct sk_buff *rsp; 110 u32 ifindex; 111 int err; 112 113 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_DEV_IFINDEX)) 114 return -EINVAL; 115 116 ifindex = nla_get_u32(info->attrs[NETDEV_A_DEV_IFINDEX]); 117 118 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 119 if (!rsp) 120 return -ENOMEM; 121 122 rtnl_lock(); 123 124 netdev = __dev_get_by_index(genl_info_net(info), ifindex); 125 if (netdev) 126 err = netdev_nl_dev_fill(netdev, rsp, info); 127 else 128 err = -ENODEV; 129 130 rtnl_unlock(); 131 132 if (err) 133 goto err_free_msg; 134 135 return genlmsg_reply(rsp, info); 136 137 err_free_msg: 138 nlmsg_free(rsp); 139 return err; 140 } 141 142 int netdev_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 143 { 144 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb); 145 struct net *net = sock_net(skb->sk); 146 struct net_device *netdev; 147 int err = 0; 148 149 rtnl_lock(); 150 for_each_netdev_dump(net, netdev, ctx->ifindex) { 151 err = netdev_nl_dev_fill(netdev, skb, genl_info_dump(cb)); 152 if (err < 0) 153 break; 154 } 155 rtnl_unlock(); 156 157 return err; 158 } 159 160 static int 161 netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi, 162 const struct genl_info *info) 163 { 164 unsigned long irq_suspend_timeout; 165 unsigned long gro_flush_timeout; 166 u32 napi_defer_hard_irqs; 167 void *hdr; 168 pid_t pid; 169 170 if (WARN_ON_ONCE(!napi->dev)) 171 return -EINVAL; 172 if (!(napi->dev->flags & IFF_UP)) 173 return 0; 174 175 hdr = genlmsg_iput(rsp, info); 176 if (!hdr) 177 return -EMSGSIZE; 178 179 if (napi->napi_id >= MIN_NAPI_ID && 180 nla_put_u32(rsp, NETDEV_A_NAPI_ID, napi->napi_id)) 181 goto nla_put_failure; 182 183 if (nla_put_u32(rsp, NETDEV_A_NAPI_IFINDEX, napi->dev->ifindex)) 184 goto nla_put_failure; 185 186 if (napi->irq >= 0 && nla_put_u32(rsp, NETDEV_A_NAPI_IRQ, napi->irq)) 187 goto nla_put_failure; 188 189 if (napi->thread) { 190 pid = task_pid_nr(napi->thread); 191 if (nla_put_u32(rsp, NETDEV_A_NAPI_PID, pid)) 192 goto nla_put_failure; 193 } 194 195 napi_defer_hard_irqs = napi_get_defer_hard_irqs(napi); 196 if (nla_put_s32(rsp, NETDEV_A_NAPI_DEFER_HARD_IRQS, 197 napi_defer_hard_irqs)) 198 goto nla_put_failure; 199 200 irq_suspend_timeout = napi_get_irq_suspend_timeout(napi); 201 if (nla_put_uint(rsp, NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT, 202 irq_suspend_timeout)) 203 goto nla_put_failure; 204 205 gro_flush_timeout = napi_get_gro_flush_timeout(napi); 206 if (nla_put_uint(rsp, NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT, 207 gro_flush_timeout)) 208 goto nla_put_failure; 209 210 genlmsg_end(rsp, hdr); 211 212 return 0; 213 214 nla_put_failure: 215 genlmsg_cancel(rsp, hdr); 216 return -EMSGSIZE; 217 } 218 219 int netdev_nl_napi_get_doit(struct sk_buff *skb, struct genl_info *info) 220 { 221 struct napi_struct *napi; 222 struct sk_buff *rsp; 223 u32 napi_id; 224 int err; 225 226 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_NAPI_ID)) 227 return -EINVAL; 228 229 napi_id = nla_get_u32(info->attrs[NETDEV_A_NAPI_ID]); 230 231 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 232 if (!rsp) 233 return -ENOMEM; 234 235 rtnl_lock(); 236 rcu_read_lock(); 237 238 napi = napi_by_id(napi_id); 239 if (napi) { 240 err = netdev_nl_napi_fill_one(rsp, napi, info); 241 } else { 242 NL_SET_BAD_ATTR(info->extack, info->attrs[NETDEV_A_NAPI_ID]); 243 err = -ENOENT; 244 } 245 246 rcu_read_unlock(); 247 rtnl_unlock(); 248 249 if (err) 250 goto err_free_msg; 251 252 return genlmsg_reply(rsp, info); 253 254 err_free_msg: 255 nlmsg_free(rsp); 256 return err; 257 } 258 259 static int 260 netdev_nl_napi_dump_one(struct net_device *netdev, struct sk_buff *rsp, 261 const struct genl_info *info, 262 struct netdev_nl_dump_ctx *ctx) 263 { 264 struct napi_struct *napi; 265 int err = 0; 266 267 if (!(netdev->flags & IFF_UP)) 268 return err; 269 270 list_for_each_entry(napi, &netdev->napi_list, dev_list) { 271 if (ctx->napi_id && napi->napi_id >= ctx->napi_id) 272 continue; 273 274 err = netdev_nl_napi_fill_one(rsp, napi, info); 275 if (err) 276 return err; 277 ctx->napi_id = napi->napi_id; 278 } 279 return err; 280 } 281 282 int netdev_nl_napi_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 283 { 284 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb); 285 const struct genl_info *info = genl_info_dump(cb); 286 struct net *net = sock_net(skb->sk); 287 struct net_device *netdev; 288 u32 ifindex = 0; 289 int err = 0; 290 291 if (info->attrs[NETDEV_A_NAPI_IFINDEX]) 292 ifindex = nla_get_u32(info->attrs[NETDEV_A_NAPI_IFINDEX]); 293 294 rtnl_lock(); 295 if (ifindex) { 296 netdev = __dev_get_by_index(net, ifindex); 297 if (netdev) 298 err = netdev_nl_napi_dump_one(netdev, skb, info, ctx); 299 else 300 err = -ENODEV; 301 } else { 302 for_each_netdev_dump(net, netdev, ctx->ifindex) { 303 err = netdev_nl_napi_dump_one(netdev, skb, info, ctx); 304 if (err < 0) 305 break; 306 ctx->napi_id = 0; 307 } 308 } 309 rtnl_unlock(); 310 311 return err; 312 } 313 314 static int 315 netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info) 316 { 317 u64 irq_suspend_timeout = 0; 318 u64 gro_flush_timeout = 0; 319 u32 defer = 0; 320 321 if (info->attrs[NETDEV_A_NAPI_DEFER_HARD_IRQS]) { 322 defer = nla_get_u32(info->attrs[NETDEV_A_NAPI_DEFER_HARD_IRQS]); 323 napi_set_defer_hard_irqs(napi, defer); 324 } 325 326 if (info->attrs[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT]) { 327 irq_suspend_timeout = nla_get_uint(info->attrs[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT]); 328 napi_set_irq_suspend_timeout(napi, irq_suspend_timeout); 329 } 330 331 if (info->attrs[NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT]) { 332 gro_flush_timeout = nla_get_uint(info->attrs[NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT]); 333 napi_set_gro_flush_timeout(napi, gro_flush_timeout); 334 } 335 336 return 0; 337 } 338 339 int netdev_nl_napi_set_doit(struct sk_buff *skb, struct genl_info *info) 340 { 341 struct napi_struct *napi; 342 unsigned int napi_id; 343 int err; 344 345 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_NAPI_ID)) 346 return -EINVAL; 347 348 napi_id = nla_get_u32(info->attrs[NETDEV_A_NAPI_ID]); 349 350 rtnl_lock(); 351 rcu_read_lock(); 352 353 napi = napi_by_id(napi_id); 354 if (napi) { 355 err = netdev_nl_napi_set_config(napi, info); 356 } else { 357 NL_SET_BAD_ATTR(info->extack, info->attrs[NETDEV_A_NAPI_ID]); 358 err = -ENOENT; 359 } 360 361 rcu_read_unlock(); 362 rtnl_unlock(); 363 364 return err; 365 } 366 367 static int 368 netdev_nl_queue_fill_one(struct sk_buff *rsp, struct net_device *netdev, 369 u32 q_idx, u32 q_type, const struct genl_info *info) 370 { 371 struct net_devmem_dmabuf_binding *binding; 372 struct netdev_rx_queue *rxq; 373 struct netdev_queue *txq; 374 void *hdr; 375 376 hdr = genlmsg_iput(rsp, info); 377 if (!hdr) 378 return -EMSGSIZE; 379 380 if (nla_put_u32(rsp, NETDEV_A_QUEUE_ID, q_idx) || 381 nla_put_u32(rsp, NETDEV_A_QUEUE_TYPE, q_type) || 382 nla_put_u32(rsp, NETDEV_A_QUEUE_IFINDEX, netdev->ifindex)) 383 goto nla_put_failure; 384 385 switch (q_type) { 386 case NETDEV_QUEUE_TYPE_RX: 387 rxq = __netif_get_rx_queue(netdev, q_idx); 388 if (rxq->napi && nla_put_u32(rsp, NETDEV_A_QUEUE_NAPI_ID, 389 rxq->napi->napi_id)) 390 goto nla_put_failure; 391 392 binding = rxq->mp_params.mp_priv; 393 if (binding && 394 nla_put_u32(rsp, NETDEV_A_QUEUE_DMABUF, binding->id)) 395 goto nla_put_failure; 396 397 break; 398 case NETDEV_QUEUE_TYPE_TX: 399 txq = netdev_get_tx_queue(netdev, q_idx); 400 if (txq->napi && nla_put_u32(rsp, NETDEV_A_QUEUE_NAPI_ID, 401 txq->napi->napi_id)) 402 goto nla_put_failure; 403 } 404 405 genlmsg_end(rsp, hdr); 406 407 return 0; 408 409 nla_put_failure: 410 genlmsg_cancel(rsp, hdr); 411 return -EMSGSIZE; 412 } 413 414 static int netdev_nl_queue_validate(struct net_device *netdev, u32 q_id, 415 u32 q_type) 416 { 417 switch (q_type) { 418 case NETDEV_QUEUE_TYPE_RX: 419 if (q_id >= netdev->real_num_rx_queues) 420 return -EINVAL; 421 return 0; 422 case NETDEV_QUEUE_TYPE_TX: 423 if (q_id >= netdev->real_num_tx_queues) 424 return -EINVAL; 425 } 426 return 0; 427 } 428 429 static int 430 netdev_nl_queue_fill(struct sk_buff *rsp, struct net_device *netdev, u32 q_idx, 431 u32 q_type, const struct genl_info *info) 432 { 433 int err = 0; 434 435 if (!(netdev->flags & IFF_UP)) 436 return err; 437 438 err = netdev_nl_queue_validate(netdev, q_idx, q_type); 439 if (err) 440 return err; 441 442 return netdev_nl_queue_fill_one(rsp, netdev, q_idx, q_type, info); 443 } 444 445 int netdev_nl_queue_get_doit(struct sk_buff *skb, struct genl_info *info) 446 { 447 u32 q_id, q_type, ifindex; 448 struct net_device *netdev; 449 struct sk_buff *rsp; 450 int err; 451 452 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_ID) || 453 GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_TYPE) || 454 GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_IFINDEX)) 455 return -EINVAL; 456 457 q_id = nla_get_u32(info->attrs[NETDEV_A_QUEUE_ID]); 458 q_type = nla_get_u32(info->attrs[NETDEV_A_QUEUE_TYPE]); 459 ifindex = nla_get_u32(info->attrs[NETDEV_A_QUEUE_IFINDEX]); 460 461 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 462 if (!rsp) 463 return -ENOMEM; 464 465 rtnl_lock(); 466 467 netdev = __dev_get_by_index(genl_info_net(info), ifindex); 468 if (netdev) 469 err = netdev_nl_queue_fill(rsp, netdev, q_id, q_type, info); 470 else 471 err = -ENODEV; 472 473 rtnl_unlock(); 474 475 if (err) 476 goto err_free_msg; 477 478 return genlmsg_reply(rsp, info); 479 480 err_free_msg: 481 nlmsg_free(rsp); 482 return err; 483 } 484 485 static int 486 netdev_nl_queue_dump_one(struct net_device *netdev, struct sk_buff *rsp, 487 const struct genl_info *info, 488 struct netdev_nl_dump_ctx *ctx) 489 { 490 int err = 0; 491 int i; 492 493 if (!(netdev->flags & IFF_UP)) 494 return err; 495 496 for (i = ctx->rxq_idx; i < netdev->real_num_rx_queues;) { 497 err = netdev_nl_queue_fill_one(rsp, netdev, i, 498 NETDEV_QUEUE_TYPE_RX, info); 499 if (err) 500 return err; 501 ctx->rxq_idx = i++; 502 } 503 for (i = ctx->txq_idx; i < netdev->real_num_tx_queues;) { 504 err = netdev_nl_queue_fill_one(rsp, netdev, i, 505 NETDEV_QUEUE_TYPE_TX, info); 506 if (err) 507 return err; 508 ctx->txq_idx = i++; 509 } 510 511 return err; 512 } 513 514 int netdev_nl_queue_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 515 { 516 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb); 517 const struct genl_info *info = genl_info_dump(cb); 518 struct net *net = sock_net(skb->sk); 519 struct net_device *netdev; 520 u32 ifindex = 0; 521 int err = 0; 522 523 if (info->attrs[NETDEV_A_QUEUE_IFINDEX]) 524 ifindex = nla_get_u32(info->attrs[NETDEV_A_QUEUE_IFINDEX]); 525 526 rtnl_lock(); 527 if (ifindex) { 528 netdev = __dev_get_by_index(net, ifindex); 529 if (netdev) 530 err = netdev_nl_queue_dump_one(netdev, skb, info, ctx); 531 else 532 err = -ENODEV; 533 } else { 534 for_each_netdev_dump(net, netdev, ctx->ifindex) { 535 err = netdev_nl_queue_dump_one(netdev, skb, info, ctx); 536 if (err < 0) 537 break; 538 ctx->rxq_idx = 0; 539 ctx->txq_idx = 0; 540 } 541 } 542 rtnl_unlock(); 543 544 return err; 545 } 546 547 #define NETDEV_STAT_NOT_SET (~0ULL) 548 549 static void netdev_nl_stats_add(void *_sum, const void *_add, size_t size) 550 { 551 const u64 *add = _add; 552 u64 *sum = _sum; 553 554 while (size) { 555 if (*add != NETDEV_STAT_NOT_SET && *sum != NETDEV_STAT_NOT_SET) 556 *sum += *add; 557 sum++; 558 add++; 559 size -= 8; 560 } 561 } 562 563 static int netdev_stat_put(struct sk_buff *rsp, unsigned int attr_id, u64 value) 564 { 565 if (value == NETDEV_STAT_NOT_SET) 566 return 0; 567 return nla_put_uint(rsp, attr_id, value); 568 } 569 570 static int 571 netdev_nl_stats_write_rx(struct sk_buff *rsp, struct netdev_queue_stats_rx *rx) 572 { 573 if (netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_PACKETS, rx->packets) || 574 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_BYTES, rx->bytes) || 575 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_ALLOC_FAIL, rx->alloc_fail) || 576 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_HW_DROPS, rx->hw_drops) || 577 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_HW_DROP_OVERRUNS, rx->hw_drop_overruns) || 578 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_CSUM_UNNECESSARY, rx->csum_unnecessary) || 579 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_CSUM_NONE, rx->csum_none) || 580 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_CSUM_BAD, rx->csum_bad) || 581 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_HW_GRO_PACKETS, rx->hw_gro_packets) || 582 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_HW_GRO_BYTES, rx->hw_gro_bytes) || 583 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_HW_GRO_WIRE_PACKETS, rx->hw_gro_wire_packets) || 584 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_HW_GRO_WIRE_BYTES, rx->hw_gro_wire_bytes) || 585 netdev_stat_put(rsp, NETDEV_A_QSTATS_RX_HW_DROP_RATELIMITS, rx->hw_drop_ratelimits)) 586 return -EMSGSIZE; 587 return 0; 588 } 589 590 static int 591 netdev_nl_stats_write_tx(struct sk_buff *rsp, struct netdev_queue_stats_tx *tx) 592 { 593 if (netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_PACKETS, tx->packets) || 594 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_BYTES, tx->bytes) || 595 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_HW_DROPS, tx->hw_drops) || 596 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_HW_DROP_ERRORS, tx->hw_drop_errors) || 597 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_CSUM_NONE, tx->csum_none) || 598 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_NEEDS_CSUM, tx->needs_csum) || 599 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_HW_GSO_PACKETS, tx->hw_gso_packets) || 600 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_HW_GSO_BYTES, tx->hw_gso_bytes) || 601 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_HW_GSO_WIRE_PACKETS, tx->hw_gso_wire_packets) || 602 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_HW_GSO_WIRE_BYTES, tx->hw_gso_wire_bytes) || 603 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_HW_DROP_RATELIMITS, tx->hw_drop_ratelimits) || 604 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_STOP, tx->stop) || 605 netdev_stat_put(rsp, NETDEV_A_QSTATS_TX_WAKE, tx->wake)) 606 return -EMSGSIZE; 607 return 0; 608 } 609 610 static int 611 netdev_nl_stats_queue(struct net_device *netdev, struct sk_buff *rsp, 612 u32 q_type, int i, const struct genl_info *info) 613 { 614 const struct netdev_stat_ops *ops = netdev->stat_ops; 615 struct netdev_queue_stats_rx rx; 616 struct netdev_queue_stats_tx tx; 617 void *hdr; 618 619 hdr = genlmsg_iput(rsp, info); 620 if (!hdr) 621 return -EMSGSIZE; 622 if (nla_put_u32(rsp, NETDEV_A_QSTATS_IFINDEX, netdev->ifindex) || 623 nla_put_u32(rsp, NETDEV_A_QSTATS_QUEUE_TYPE, q_type) || 624 nla_put_u32(rsp, NETDEV_A_QSTATS_QUEUE_ID, i)) 625 goto nla_put_failure; 626 627 switch (q_type) { 628 case NETDEV_QUEUE_TYPE_RX: 629 memset(&rx, 0xff, sizeof(rx)); 630 ops->get_queue_stats_rx(netdev, i, &rx); 631 if (!memchr_inv(&rx, 0xff, sizeof(rx))) 632 goto nla_cancel; 633 if (netdev_nl_stats_write_rx(rsp, &rx)) 634 goto nla_put_failure; 635 break; 636 case NETDEV_QUEUE_TYPE_TX: 637 memset(&tx, 0xff, sizeof(tx)); 638 ops->get_queue_stats_tx(netdev, i, &tx); 639 if (!memchr_inv(&tx, 0xff, sizeof(tx))) 640 goto nla_cancel; 641 if (netdev_nl_stats_write_tx(rsp, &tx)) 642 goto nla_put_failure; 643 break; 644 } 645 646 genlmsg_end(rsp, hdr); 647 return 0; 648 649 nla_cancel: 650 genlmsg_cancel(rsp, hdr); 651 return 0; 652 nla_put_failure: 653 genlmsg_cancel(rsp, hdr); 654 return -EMSGSIZE; 655 } 656 657 static int 658 netdev_nl_stats_by_queue(struct net_device *netdev, struct sk_buff *rsp, 659 const struct genl_info *info, 660 struct netdev_nl_dump_ctx *ctx) 661 { 662 const struct netdev_stat_ops *ops = netdev->stat_ops; 663 int i, err; 664 665 if (!(netdev->flags & IFF_UP)) 666 return 0; 667 668 i = ctx->rxq_idx; 669 while (ops->get_queue_stats_rx && i < netdev->real_num_rx_queues) { 670 err = netdev_nl_stats_queue(netdev, rsp, NETDEV_QUEUE_TYPE_RX, 671 i, info); 672 if (err) 673 return err; 674 ctx->rxq_idx = i++; 675 } 676 i = ctx->txq_idx; 677 while (ops->get_queue_stats_tx && i < netdev->real_num_tx_queues) { 678 err = netdev_nl_stats_queue(netdev, rsp, NETDEV_QUEUE_TYPE_TX, 679 i, info); 680 if (err) 681 return err; 682 ctx->txq_idx = i++; 683 } 684 685 ctx->rxq_idx = 0; 686 ctx->txq_idx = 0; 687 return 0; 688 } 689 690 static int 691 netdev_nl_stats_by_netdev(struct net_device *netdev, struct sk_buff *rsp, 692 const struct genl_info *info) 693 { 694 struct netdev_queue_stats_rx rx_sum, rx; 695 struct netdev_queue_stats_tx tx_sum, tx; 696 const struct netdev_stat_ops *ops; 697 void *hdr; 698 int i; 699 700 ops = netdev->stat_ops; 701 /* Netdev can't guarantee any complete counters */ 702 if (!ops->get_base_stats) 703 return 0; 704 705 memset(&rx_sum, 0xff, sizeof(rx_sum)); 706 memset(&tx_sum, 0xff, sizeof(tx_sum)); 707 708 ops->get_base_stats(netdev, &rx_sum, &tx_sum); 709 710 /* The op was there, but nothing reported, don't bother */ 711 if (!memchr_inv(&rx_sum, 0xff, sizeof(rx_sum)) && 712 !memchr_inv(&tx_sum, 0xff, sizeof(tx_sum))) 713 return 0; 714 715 hdr = genlmsg_iput(rsp, info); 716 if (!hdr) 717 return -EMSGSIZE; 718 if (nla_put_u32(rsp, NETDEV_A_QSTATS_IFINDEX, netdev->ifindex)) 719 goto nla_put_failure; 720 721 for (i = 0; i < netdev->real_num_rx_queues; i++) { 722 memset(&rx, 0xff, sizeof(rx)); 723 if (ops->get_queue_stats_rx) 724 ops->get_queue_stats_rx(netdev, i, &rx); 725 netdev_nl_stats_add(&rx_sum, &rx, sizeof(rx)); 726 } 727 for (i = 0; i < netdev->real_num_tx_queues; i++) { 728 memset(&tx, 0xff, sizeof(tx)); 729 if (ops->get_queue_stats_tx) 730 ops->get_queue_stats_tx(netdev, i, &tx); 731 netdev_nl_stats_add(&tx_sum, &tx, sizeof(tx)); 732 } 733 734 if (netdev_nl_stats_write_rx(rsp, &rx_sum) || 735 netdev_nl_stats_write_tx(rsp, &tx_sum)) 736 goto nla_put_failure; 737 738 genlmsg_end(rsp, hdr); 739 return 0; 740 741 nla_put_failure: 742 genlmsg_cancel(rsp, hdr); 743 return -EMSGSIZE; 744 } 745 746 static int 747 netdev_nl_qstats_get_dump_one(struct net_device *netdev, unsigned int scope, 748 struct sk_buff *skb, const struct genl_info *info, 749 struct netdev_nl_dump_ctx *ctx) 750 { 751 if (!netdev->stat_ops) 752 return 0; 753 754 switch (scope) { 755 case 0: 756 return netdev_nl_stats_by_netdev(netdev, skb, info); 757 case NETDEV_QSTATS_SCOPE_QUEUE: 758 return netdev_nl_stats_by_queue(netdev, skb, info, ctx); 759 } 760 761 return -EINVAL; /* Should not happen, per netlink policy */ 762 } 763 764 int netdev_nl_qstats_get_dumpit(struct sk_buff *skb, 765 struct netlink_callback *cb) 766 { 767 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb); 768 const struct genl_info *info = genl_info_dump(cb); 769 struct net *net = sock_net(skb->sk); 770 struct net_device *netdev; 771 unsigned int ifindex; 772 unsigned int scope; 773 int err = 0; 774 775 scope = 0; 776 if (info->attrs[NETDEV_A_QSTATS_SCOPE]) 777 scope = nla_get_uint(info->attrs[NETDEV_A_QSTATS_SCOPE]); 778 779 ifindex = 0; 780 if (info->attrs[NETDEV_A_QSTATS_IFINDEX]) 781 ifindex = nla_get_u32(info->attrs[NETDEV_A_QSTATS_IFINDEX]); 782 783 rtnl_lock(); 784 if (ifindex) { 785 netdev = __dev_get_by_index(net, ifindex); 786 if (netdev && netdev->stat_ops) { 787 err = netdev_nl_qstats_get_dump_one(netdev, scope, skb, 788 info, ctx); 789 } else { 790 NL_SET_BAD_ATTR(info->extack, 791 info->attrs[NETDEV_A_QSTATS_IFINDEX]); 792 err = netdev ? -EOPNOTSUPP : -ENODEV; 793 } 794 } else { 795 for_each_netdev_dump(net, netdev, ctx->ifindex) { 796 err = netdev_nl_qstats_get_dump_one(netdev, scope, skb, 797 info, ctx); 798 if (err < 0) 799 break; 800 } 801 } 802 rtnl_unlock(); 803 804 return err; 805 } 806 807 int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info) 808 { 809 struct nlattr *tb[ARRAY_SIZE(netdev_queue_id_nl_policy)]; 810 struct net_devmem_dmabuf_binding *binding; 811 struct list_head *sock_binding_list; 812 u32 ifindex, dmabuf_fd, rxq_idx; 813 struct net_device *netdev; 814 struct sk_buff *rsp; 815 struct nlattr *attr; 816 int rem, err = 0; 817 void *hdr; 818 819 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_DEV_IFINDEX) || 820 GENL_REQ_ATTR_CHECK(info, NETDEV_A_DMABUF_FD) || 821 GENL_REQ_ATTR_CHECK(info, NETDEV_A_DMABUF_QUEUES)) 822 return -EINVAL; 823 824 ifindex = nla_get_u32(info->attrs[NETDEV_A_DEV_IFINDEX]); 825 dmabuf_fd = nla_get_u32(info->attrs[NETDEV_A_DMABUF_FD]); 826 827 sock_binding_list = genl_sk_priv_get(&netdev_nl_family, 828 NETLINK_CB(skb).sk); 829 if (IS_ERR(sock_binding_list)) 830 return PTR_ERR(sock_binding_list); 831 832 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 833 if (!rsp) 834 return -ENOMEM; 835 836 hdr = genlmsg_iput(rsp, info); 837 if (!hdr) { 838 err = -EMSGSIZE; 839 goto err_genlmsg_free; 840 } 841 842 rtnl_lock(); 843 844 netdev = __dev_get_by_index(genl_info_net(info), ifindex); 845 if (!netdev || !netif_device_present(netdev)) { 846 err = -ENODEV; 847 goto err_unlock; 848 } 849 850 if (dev_xdp_prog_count(netdev)) { 851 NL_SET_ERR_MSG(info->extack, "unable to bind dmabuf to device with XDP program attached"); 852 err = -EEXIST; 853 goto err_unlock; 854 } 855 856 binding = net_devmem_bind_dmabuf(netdev, dmabuf_fd, info->extack); 857 if (IS_ERR(binding)) { 858 err = PTR_ERR(binding); 859 goto err_unlock; 860 } 861 862 nla_for_each_attr_type(attr, NETDEV_A_DMABUF_QUEUES, 863 genlmsg_data(info->genlhdr), 864 genlmsg_len(info->genlhdr), rem) { 865 err = nla_parse_nested( 866 tb, ARRAY_SIZE(netdev_queue_id_nl_policy) - 1, attr, 867 netdev_queue_id_nl_policy, info->extack); 868 if (err < 0) 869 goto err_unbind; 870 871 if (NL_REQ_ATTR_CHECK(info->extack, attr, tb, NETDEV_A_QUEUE_ID) || 872 NL_REQ_ATTR_CHECK(info->extack, attr, tb, NETDEV_A_QUEUE_TYPE)) { 873 err = -EINVAL; 874 goto err_unbind; 875 } 876 877 if (nla_get_u32(tb[NETDEV_A_QUEUE_TYPE]) != NETDEV_QUEUE_TYPE_RX) { 878 NL_SET_BAD_ATTR(info->extack, tb[NETDEV_A_QUEUE_TYPE]); 879 err = -EINVAL; 880 goto err_unbind; 881 } 882 883 rxq_idx = nla_get_u32(tb[NETDEV_A_QUEUE_ID]); 884 885 err = net_devmem_bind_dmabuf_to_queue(netdev, rxq_idx, binding, 886 info->extack); 887 if (err) 888 goto err_unbind; 889 } 890 891 list_add(&binding->list, sock_binding_list); 892 893 nla_put_u32(rsp, NETDEV_A_DMABUF_ID, binding->id); 894 genlmsg_end(rsp, hdr); 895 896 err = genlmsg_reply(rsp, info); 897 if (err) 898 goto err_unbind; 899 900 rtnl_unlock(); 901 902 return 0; 903 904 err_unbind: 905 net_devmem_unbind_dmabuf(binding); 906 err_unlock: 907 rtnl_unlock(); 908 err_genlmsg_free: 909 nlmsg_free(rsp); 910 return err; 911 } 912 913 void netdev_nl_sock_priv_init(struct list_head *priv) 914 { 915 INIT_LIST_HEAD(priv); 916 } 917 918 void netdev_nl_sock_priv_destroy(struct list_head *priv) 919 { 920 struct net_devmem_dmabuf_binding *binding; 921 struct net_devmem_dmabuf_binding *temp; 922 923 list_for_each_entry_safe(binding, temp, priv, list) { 924 rtnl_lock(); 925 net_devmem_unbind_dmabuf(binding); 926 rtnl_unlock(); 927 } 928 } 929 930 static int netdev_genl_netdevice_event(struct notifier_block *nb, 931 unsigned long event, void *ptr) 932 { 933 struct net_device *netdev = netdev_notifier_info_to_dev(ptr); 934 935 switch (event) { 936 case NETDEV_REGISTER: 937 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_ADD_NTF); 938 break; 939 case NETDEV_UNREGISTER: 940 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_DEL_NTF); 941 break; 942 case NETDEV_XDP_FEAT_CHANGE: 943 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_CHANGE_NTF); 944 break; 945 } 946 947 return NOTIFY_OK; 948 } 949 950 static struct notifier_block netdev_genl_nb = { 951 .notifier_call = netdev_genl_netdevice_event, 952 }; 953 954 static int __init netdev_genl_init(void) 955 { 956 int err; 957 958 err = register_netdevice_notifier(&netdev_genl_nb); 959 if (err) 960 return err; 961 962 err = genl_register_family(&netdev_nl_family); 963 if (err) 964 goto err_unreg_ntf; 965 966 return 0; 967 968 err_unreg_ntf: 969 unregister_netdevice_notifier(&netdev_genl_nb); 970 return err; 971 } 972 973 subsys_initcall(netdev_genl_init); 974