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/net_namespace.h> 7 #include <net/sock.h> 8 #include <net/xdp.h> 9 #include <net/xdp_sock.h> 10 #include <net/netdev_rx_queue.h> 11 #include <net/busy_poll.h> 12 13 #include "netdev-genl-gen.h" 14 #include "dev.h" 15 16 struct netdev_nl_dump_ctx { 17 unsigned long ifindex; 18 unsigned int rxq_idx; 19 unsigned int txq_idx; 20 unsigned int napi_id; 21 }; 22 23 static struct netdev_nl_dump_ctx *netdev_dump_ctx(struct netlink_callback *cb) 24 { 25 NL_ASSERT_DUMP_CTX_FITS(struct netdev_nl_dump_ctx); 26 27 return (struct netdev_nl_dump_ctx *)cb->ctx; 28 } 29 30 static int 31 netdev_nl_dev_fill(struct net_device *netdev, struct sk_buff *rsp, 32 const struct genl_info *info) 33 { 34 u64 xsk_features = 0; 35 u64 xdp_rx_meta = 0; 36 void *hdr; 37 38 hdr = genlmsg_iput(rsp, info); 39 if (!hdr) 40 return -EMSGSIZE; 41 42 #define XDP_METADATA_KFUNC(_, flag, __, xmo) \ 43 if (netdev->xdp_metadata_ops && netdev->xdp_metadata_ops->xmo) \ 44 xdp_rx_meta |= flag; 45 XDP_METADATA_KFUNC_xxx 46 #undef XDP_METADATA_KFUNC 47 48 if (netdev->xsk_tx_metadata_ops) { 49 if (netdev->xsk_tx_metadata_ops->tmo_fill_timestamp) 50 xsk_features |= NETDEV_XSK_FLAGS_TX_TIMESTAMP; 51 if (netdev->xsk_tx_metadata_ops->tmo_request_checksum) 52 xsk_features |= NETDEV_XSK_FLAGS_TX_CHECKSUM; 53 } 54 55 if (nla_put_u32(rsp, NETDEV_A_DEV_IFINDEX, netdev->ifindex) || 56 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_FEATURES, 57 netdev->xdp_features, NETDEV_A_DEV_PAD) || 58 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_RX_METADATA_FEATURES, 59 xdp_rx_meta, NETDEV_A_DEV_PAD) || 60 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XSK_FEATURES, 61 xsk_features, NETDEV_A_DEV_PAD)) { 62 genlmsg_cancel(rsp, hdr); 63 return -EINVAL; 64 } 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 genlmsg_cancel(rsp, hdr); 70 return -EINVAL; 71 } 72 } 73 74 genlmsg_end(rsp, hdr); 75 76 return 0; 77 } 78 79 static void 80 netdev_genl_dev_notify(struct net_device *netdev, int cmd) 81 { 82 struct genl_info info; 83 struct sk_buff *ntf; 84 85 if (!genl_has_listeners(&netdev_nl_family, dev_net(netdev), 86 NETDEV_NLGRP_MGMT)) 87 return; 88 89 genl_info_init_ntf(&info, &netdev_nl_family, cmd); 90 91 ntf = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 92 if (!ntf) 93 return; 94 95 if (netdev_nl_dev_fill(netdev, ntf, &info)) { 96 nlmsg_free(ntf); 97 return; 98 } 99 100 genlmsg_multicast_netns(&netdev_nl_family, dev_net(netdev), ntf, 101 0, NETDEV_NLGRP_MGMT, GFP_KERNEL); 102 } 103 104 int netdev_nl_dev_get_doit(struct sk_buff *skb, struct genl_info *info) 105 { 106 struct net_device *netdev; 107 struct sk_buff *rsp; 108 u32 ifindex; 109 int err; 110 111 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_DEV_IFINDEX)) 112 return -EINVAL; 113 114 ifindex = nla_get_u32(info->attrs[NETDEV_A_DEV_IFINDEX]); 115 116 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 117 if (!rsp) 118 return -ENOMEM; 119 120 rtnl_lock(); 121 122 netdev = __dev_get_by_index(genl_info_net(info), ifindex); 123 if (netdev) 124 err = netdev_nl_dev_fill(netdev, rsp, info); 125 else 126 err = -ENODEV; 127 128 rtnl_unlock(); 129 130 if (err) 131 goto err_free_msg; 132 133 return genlmsg_reply(rsp, info); 134 135 err_free_msg: 136 nlmsg_free(rsp); 137 return err; 138 } 139 140 int netdev_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 141 { 142 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb); 143 struct net *net = sock_net(skb->sk); 144 struct net_device *netdev; 145 int err = 0; 146 147 rtnl_lock(); 148 for_each_netdev_dump(net, netdev, ctx->ifindex) { 149 err = netdev_nl_dev_fill(netdev, skb, genl_info_dump(cb)); 150 if (err < 0) 151 break; 152 } 153 rtnl_unlock(); 154 155 if (err != -EMSGSIZE) 156 return err; 157 158 return skb->len; 159 } 160 161 static int 162 netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi, 163 const struct genl_info *info) 164 { 165 void *hdr; 166 pid_t pid; 167 168 if (WARN_ON_ONCE(!napi->dev)) 169 return -EINVAL; 170 if (!(napi->dev->flags & IFF_UP)) 171 return 0; 172 173 hdr = genlmsg_iput(rsp, info); 174 if (!hdr) 175 return -EMSGSIZE; 176 177 if (napi->napi_id >= MIN_NAPI_ID && 178 nla_put_u32(rsp, NETDEV_A_NAPI_ID, napi->napi_id)) 179 goto nla_put_failure; 180 181 if (nla_put_u32(rsp, NETDEV_A_NAPI_IFINDEX, napi->dev->ifindex)) 182 goto nla_put_failure; 183 184 if (napi->irq >= 0 && nla_put_u32(rsp, NETDEV_A_NAPI_IRQ, napi->irq)) 185 goto nla_put_failure; 186 187 if (napi->thread) { 188 pid = task_pid_nr(napi->thread); 189 if (nla_put_u32(rsp, NETDEV_A_NAPI_PID, pid)) 190 goto nla_put_failure; 191 } 192 193 genlmsg_end(rsp, hdr); 194 195 return 0; 196 197 nla_put_failure: 198 genlmsg_cancel(rsp, hdr); 199 return -EMSGSIZE; 200 } 201 202 int netdev_nl_napi_get_doit(struct sk_buff *skb, struct genl_info *info) 203 { 204 struct napi_struct *napi; 205 struct sk_buff *rsp; 206 u32 napi_id; 207 int err; 208 209 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_NAPI_ID)) 210 return -EINVAL; 211 212 napi_id = nla_get_u32(info->attrs[NETDEV_A_NAPI_ID]); 213 214 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 215 if (!rsp) 216 return -ENOMEM; 217 218 rtnl_lock(); 219 220 napi = napi_by_id(napi_id); 221 if (napi) 222 err = netdev_nl_napi_fill_one(rsp, napi, info); 223 else 224 err = -EINVAL; 225 226 rtnl_unlock(); 227 228 if (err) 229 goto err_free_msg; 230 231 return genlmsg_reply(rsp, info); 232 233 err_free_msg: 234 nlmsg_free(rsp); 235 return err; 236 } 237 238 static int 239 netdev_nl_napi_dump_one(struct net_device *netdev, struct sk_buff *rsp, 240 const struct genl_info *info, 241 struct netdev_nl_dump_ctx *ctx) 242 { 243 struct napi_struct *napi; 244 int err = 0; 245 246 if (!(netdev->flags & IFF_UP)) 247 return err; 248 249 list_for_each_entry(napi, &netdev->napi_list, dev_list) { 250 if (ctx->napi_id && napi->napi_id >= ctx->napi_id) 251 continue; 252 253 err = netdev_nl_napi_fill_one(rsp, napi, info); 254 if (err) 255 return err; 256 ctx->napi_id = napi->napi_id; 257 } 258 return err; 259 } 260 261 int netdev_nl_napi_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 262 { 263 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb); 264 const struct genl_info *info = genl_info_dump(cb); 265 struct net *net = sock_net(skb->sk); 266 struct net_device *netdev; 267 u32 ifindex = 0; 268 int err = 0; 269 270 if (info->attrs[NETDEV_A_NAPI_IFINDEX]) 271 ifindex = nla_get_u32(info->attrs[NETDEV_A_NAPI_IFINDEX]); 272 273 rtnl_lock(); 274 if (ifindex) { 275 netdev = __dev_get_by_index(net, ifindex); 276 if (netdev) 277 err = netdev_nl_napi_dump_one(netdev, skb, info, ctx); 278 else 279 err = -ENODEV; 280 } else { 281 for_each_netdev_dump(net, netdev, ctx->ifindex) { 282 err = netdev_nl_napi_dump_one(netdev, skb, info, ctx); 283 if (err < 0) 284 break; 285 ctx->napi_id = 0; 286 } 287 } 288 rtnl_unlock(); 289 290 if (err != -EMSGSIZE) 291 return err; 292 293 return skb->len; 294 } 295 296 static int 297 netdev_nl_queue_fill_one(struct sk_buff *rsp, struct net_device *netdev, 298 u32 q_idx, u32 q_type, const struct genl_info *info) 299 { 300 struct netdev_rx_queue *rxq; 301 struct netdev_queue *txq; 302 void *hdr; 303 304 hdr = genlmsg_iput(rsp, info); 305 if (!hdr) 306 return -EMSGSIZE; 307 308 if (nla_put_u32(rsp, NETDEV_A_QUEUE_ID, q_idx) || 309 nla_put_u32(rsp, NETDEV_A_QUEUE_TYPE, q_type) || 310 nla_put_u32(rsp, NETDEV_A_QUEUE_IFINDEX, netdev->ifindex)) 311 goto nla_put_failure; 312 313 switch (q_type) { 314 case NETDEV_QUEUE_TYPE_RX: 315 rxq = __netif_get_rx_queue(netdev, q_idx); 316 if (rxq->napi && nla_put_u32(rsp, NETDEV_A_QUEUE_NAPI_ID, 317 rxq->napi->napi_id)) 318 goto nla_put_failure; 319 break; 320 case NETDEV_QUEUE_TYPE_TX: 321 txq = netdev_get_tx_queue(netdev, q_idx); 322 if (txq->napi && nla_put_u32(rsp, NETDEV_A_QUEUE_NAPI_ID, 323 txq->napi->napi_id)) 324 goto nla_put_failure; 325 } 326 327 genlmsg_end(rsp, hdr); 328 329 return 0; 330 331 nla_put_failure: 332 genlmsg_cancel(rsp, hdr); 333 return -EMSGSIZE; 334 } 335 336 static int netdev_nl_queue_validate(struct net_device *netdev, u32 q_id, 337 u32 q_type) 338 { 339 switch (q_type) { 340 case NETDEV_QUEUE_TYPE_RX: 341 if (q_id >= netdev->real_num_rx_queues) 342 return -EINVAL; 343 return 0; 344 case NETDEV_QUEUE_TYPE_TX: 345 if (q_id >= netdev->real_num_tx_queues) 346 return -EINVAL; 347 } 348 return 0; 349 } 350 351 static int 352 netdev_nl_queue_fill(struct sk_buff *rsp, struct net_device *netdev, u32 q_idx, 353 u32 q_type, const struct genl_info *info) 354 { 355 int err = 0; 356 357 if (!(netdev->flags & IFF_UP)) 358 return err; 359 360 err = netdev_nl_queue_validate(netdev, q_idx, q_type); 361 if (err) 362 return err; 363 364 return netdev_nl_queue_fill_one(rsp, netdev, q_idx, q_type, info); 365 } 366 367 int netdev_nl_queue_get_doit(struct sk_buff *skb, struct genl_info *info) 368 { 369 u32 q_id, q_type, ifindex; 370 struct net_device *netdev; 371 struct sk_buff *rsp; 372 int err; 373 374 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_ID) || 375 GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_TYPE) || 376 GENL_REQ_ATTR_CHECK(info, NETDEV_A_QUEUE_IFINDEX)) 377 return -EINVAL; 378 379 q_id = nla_get_u32(info->attrs[NETDEV_A_QUEUE_ID]); 380 q_type = nla_get_u32(info->attrs[NETDEV_A_QUEUE_TYPE]); 381 ifindex = nla_get_u32(info->attrs[NETDEV_A_QUEUE_IFINDEX]); 382 383 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 384 if (!rsp) 385 return -ENOMEM; 386 387 rtnl_lock(); 388 389 netdev = __dev_get_by_index(genl_info_net(info), ifindex); 390 if (netdev) 391 err = netdev_nl_queue_fill(rsp, netdev, q_id, q_type, info); 392 else 393 err = -ENODEV; 394 395 rtnl_unlock(); 396 397 if (err) 398 goto err_free_msg; 399 400 return genlmsg_reply(rsp, info); 401 402 err_free_msg: 403 nlmsg_free(rsp); 404 return err; 405 } 406 407 static int 408 netdev_nl_queue_dump_one(struct net_device *netdev, struct sk_buff *rsp, 409 const struct genl_info *info, 410 struct netdev_nl_dump_ctx *ctx) 411 { 412 int err = 0; 413 int i; 414 415 if (!(netdev->flags & IFF_UP)) 416 return err; 417 418 for (i = ctx->rxq_idx; i < netdev->real_num_rx_queues;) { 419 err = netdev_nl_queue_fill_one(rsp, netdev, i, 420 NETDEV_QUEUE_TYPE_RX, info); 421 if (err) 422 return err; 423 ctx->rxq_idx = i++; 424 } 425 for (i = ctx->txq_idx; i < netdev->real_num_tx_queues;) { 426 err = netdev_nl_queue_fill_one(rsp, netdev, i, 427 NETDEV_QUEUE_TYPE_TX, info); 428 if (err) 429 return err; 430 ctx->txq_idx = i++; 431 } 432 433 return err; 434 } 435 436 int netdev_nl_queue_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 437 { 438 struct netdev_nl_dump_ctx *ctx = netdev_dump_ctx(cb); 439 const struct genl_info *info = genl_info_dump(cb); 440 struct net *net = sock_net(skb->sk); 441 struct net_device *netdev; 442 u32 ifindex = 0; 443 int err = 0; 444 445 if (info->attrs[NETDEV_A_QUEUE_IFINDEX]) 446 ifindex = nla_get_u32(info->attrs[NETDEV_A_QUEUE_IFINDEX]); 447 448 rtnl_lock(); 449 if (ifindex) { 450 netdev = __dev_get_by_index(net, ifindex); 451 if (netdev) 452 err = netdev_nl_queue_dump_one(netdev, skb, info, ctx); 453 else 454 err = -ENODEV; 455 } else { 456 for_each_netdev_dump(net, netdev, ctx->ifindex) { 457 err = netdev_nl_queue_dump_one(netdev, skb, info, ctx); 458 if (err < 0) 459 break; 460 ctx->rxq_idx = 0; 461 ctx->txq_idx = 0; 462 } 463 } 464 rtnl_unlock(); 465 466 if (err != -EMSGSIZE) 467 return err; 468 469 return skb->len; 470 } 471 472 static int netdev_genl_netdevice_event(struct notifier_block *nb, 473 unsigned long event, void *ptr) 474 { 475 struct net_device *netdev = netdev_notifier_info_to_dev(ptr); 476 477 switch (event) { 478 case NETDEV_REGISTER: 479 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_ADD_NTF); 480 break; 481 case NETDEV_UNREGISTER: 482 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_DEL_NTF); 483 break; 484 case NETDEV_XDP_FEAT_CHANGE: 485 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_CHANGE_NTF); 486 break; 487 } 488 489 return NOTIFY_OK; 490 } 491 492 static struct notifier_block netdev_genl_nb = { 493 .notifier_call = netdev_genl_netdevice_event, 494 }; 495 496 static int __init netdev_genl_init(void) 497 { 498 int err; 499 500 err = register_netdevice_notifier(&netdev_genl_nb); 501 if (err) 502 return err; 503 504 err = genl_register_family(&netdev_nl_family); 505 if (err) 506 goto err_unreg_ntf; 507 508 return 0; 509 510 err_unreg_ntf: 511 unregister_netdevice_notifier(&netdev_genl_nb); 512 return err; 513 } 514 515 subsys_initcall(netdev_genl_init); 516