netdev-genl.c 23 KB

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