netvsc_bpf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2019, Microsoft Corporation.
  3. *
  4. * Author:
  5. * Haiyang Zhang <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/netdevice.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/ethtool.h>
  11. #include <linux/netpoll.h>
  12. #include <linux/bpf.h>
  13. #include <linux/bpf_trace.h>
  14. #include <linux/kernel.h>
  15. #include <net/xdp.h>
  16. #include <linux/mutex.h>
  17. #include <linux/rtnetlink.h>
  18. #include "hyperv_net.h"
  19. u32 netvsc_run_xdp(struct net_device *ndev, struct netvsc_channel *nvchan,
  20. struct xdp_buff *xdp)
  21. {
  22. struct netvsc_stats_rx *rx_stats = &nvchan->rx_stats;
  23. void *data = nvchan->rsc.data[0];
  24. u32 len = nvchan->rsc.len[0];
  25. struct page *page = NULL;
  26. struct bpf_prog *prog;
  27. u32 act = XDP_PASS;
  28. bool drop = true;
  29. xdp->data_hard_start = NULL;
  30. rcu_read_lock();
  31. prog = rcu_dereference(nvchan->bpf_prog);
  32. if (!prog)
  33. goto out;
  34. /* Ensure that the below memcpy() won't overflow the page buffer. */
  35. if (len > ndev->mtu + ETH_HLEN) {
  36. act = XDP_DROP;
  37. goto out;
  38. }
  39. /* allocate page buffer for data */
  40. page = alloc_page(GFP_ATOMIC);
  41. if (!page) {
  42. act = XDP_DROP;
  43. goto out;
  44. }
  45. xdp_init_buff(xdp, PAGE_SIZE, &nvchan->xdp_rxq);
  46. xdp_prepare_buff(xdp, page_address(page), NETVSC_XDP_HDRM, len, false);
  47. memcpy(xdp->data, data, len);
  48. act = bpf_prog_run_xdp(prog, xdp);
  49. switch (act) {
  50. case XDP_PASS:
  51. case XDP_TX:
  52. drop = false;
  53. break;
  54. case XDP_DROP:
  55. break;
  56. case XDP_REDIRECT:
  57. if (!xdp_do_redirect(ndev, xdp, prog)) {
  58. nvchan->xdp_flush = true;
  59. drop = false;
  60. u64_stats_update_begin(&rx_stats->syncp);
  61. rx_stats->xdp_redirect++;
  62. rx_stats->packets++;
  63. rx_stats->bytes += nvchan->rsc.pktlen;
  64. u64_stats_update_end(&rx_stats->syncp);
  65. break;
  66. } else {
  67. u64_stats_update_begin(&rx_stats->syncp);
  68. rx_stats->xdp_drop++;
  69. u64_stats_update_end(&rx_stats->syncp);
  70. }
  71. fallthrough;
  72. case XDP_ABORTED:
  73. trace_xdp_exception(ndev, prog, act);
  74. break;
  75. default:
  76. bpf_warn_invalid_xdp_action(ndev, prog, act);
  77. }
  78. out:
  79. rcu_read_unlock();
  80. if (page && drop) {
  81. __free_page(page);
  82. xdp->data_hard_start = NULL;
  83. }
  84. return act;
  85. }
  86. unsigned int netvsc_xdp_fraglen(unsigned int len)
  87. {
  88. return SKB_DATA_ALIGN(len) +
  89. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  90. }
  91. struct bpf_prog *netvsc_xdp_get(struct netvsc_device *nvdev)
  92. {
  93. return rtnl_dereference(nvdev->chan_table[0].bpf_prog);
  94. }
  95. int netvsc_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  96. struct netlink_ext_ack *extack,
  97. struct netvsc_device *nvdev)
  98. {
  99. struct bpf_prog *old_prog;
  100. int buf_max, i;
  101. old_prog = netvsc_xdp_get(nvdev);
  102. if (!old_prog && !prog)
  103. return 0;
  104. buf_max = NETVSC_XDP_HDRM + netvsc_xdp_fraglen(dev->mtu + ETH_HLEN);
  105. if (prog && buf_max > PAGE_SIZE) {
  106. netdev_err(dev, "XDP: mtu:%u too large, buf_max:%u\n",
  107. dev->mtu, buf_max);
  108. NL_SET_ERR_MSG_MOD(extack, "XDP: mtu too large");
  109. return -EOPNOTSUPP;
  110. }
  111. if (prog && (dev->features & NETIF_F_LRO)) {
  112. netdev_err(dev, "XDP: not support LRO\n");
  113. NL_SET_ERR_MSG_MOD(extack, "XDP: not support LRO");
  114. return -EOPNOTSUPP;
  115. }
  116. if (prog)
  117. bpf_prog_add(prog, nvdev->num_chn - 1);
  118. for (i = 0; i < nvdev->num_chn; i++)
  119. rcu_assign_pointer(nvdev->chan_table[i].bpf_prog, prog);
  120. if (old_prog)
  121. for (i = 0; i < nvdev->num_chn; i++)
  122. bpf_prog_put(old_prog);
  123. return 0;
  124. }
  125. int netvsc_vf_setxdp(struct net_device *vf_netdev, struct bpf_prog *prog)
  126. {
  127. struct netdev_bpf xdp;
  128. int ret;
  129. ASSERT_RTNL();
  130. if (!vf_netdev)
  131. return 0;
  132. if (!vf_netdev->netdev_ops->ndo_bpf)
  133. return 0;
  134. memset(&xdp, 0, sizeof(xdp));
  135. if (prog)
  136. bpf_prog_inc(prog);
  137. xdp.command = XDP_SETUP_PROG;
  138. xdp.prog = prog;
  139. ret = vf_netdev->netdev_ops->ndo_bpf(vf_netdev, &xdp);
  140. if (ret && prog)
  141. bpf_prog_put(prog);
  142. return ret;
  143. }
  144. int netvsc_bpf(struct net_device *dev, struct netdev_bpf *bpf)
  145. {
  146. struct net_device_context *ndevctx = netdev_priv(dev);
  147. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  148. struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
  149. struct netlink_ext_ack *extack = bpf->extack;
  150. int ret;
  151. if (!nvdev || nvdev->destroy) {
  152. return -ENODEV;
  153. }
  154. switch (bpf->command) {
  155. case XDP_SETUP_PROG:
  156. ret = netvsc_xdp_set(dev, bpf->prog, extack, nvdev);
  157. if (ret)
  158. return ret;
  159. ret = netvsc_vf_setxdp(vf_netdev, bpf->prog);
  160. if (ret) {
  161. netdev_err(dev, "vf_setxdp failed:%d\n", ret);
  162. NL_SET_ERR_MSG_MOD(extack, "vf_setxdp failed");
  163. netvsc_xdp_set(dev, NULL, extack, nvdev);
  164. }
  165. return ret;
  166. default:
  167. return -EINVAL;
  168. }
  169. }
  170. static int netvsc_ndoxdp_xmit_fm(struct net_device *ndev,
  171. struct xdp_frame *frame, u16 q_idx)
  172. {
  173. struct sk_buff *skb;
  174. skb = xdp_build_skb_from_frame(frame, ndev);
  175. if (unlikely(!skb))
  176. return -ENOMEM;
  177. netvsc_get_hash(skb, netdev_priv(ndev));
  178. skb_record_rx_queue(skb, q_idx);
  179. netvsc_xdp_xmit(skb, ndev);
  180. return 0;
  181. }
  182. int netvsc_ndoxdp_xmit(struct net_device *ndev, int n,
  183. struct xdp_frame **frames, u32 flags)
  184. {
  185. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  186. const struct net_device_ops *vf_ops;
  187. struct netvsc_stats_tx *tx_stats;
  188. struct netvsc_device *nvsc_dev;
  189. struct net_device *vf_netdev;
  190. int i, count = 0;
  191. u16 q_idx;
  192. /* Don't transmit if netvsc_device is gone */
  193. nvsc_dev = rcu_dereference_bh(ndev_ctx->nvdev);
  194. if (unlikely(!nvsc_dev || nvsc_dev->destroy))
  195. return 0;
  196. /* If VF is present and up then redirect packets to it.
  197. * Skip the VF if it is marked down or has no carrier.
  198. * If netpoll is in uses, then VF can not be used either.
  199. */
  200. vf_netdev = rcu_dereference_bh(ndev_ctx->vf_netdev);
  201. if (vf_netdev && netif_running(vf_netdev) &&
  202. netif_carrier_ok(vf_netdev) && !netpoll_tx_running(ndev) &&
  203. vf_netdev->netdev_ops->ndo_xdp_xmit &&
  204. ndev_ctx->data_path_is_vf) {
  205. vf_ops = vf_netdev->netdev_ops;
  206. return vf_ops->ndo_xdp_xmit(vf_netdev, n, frames, flags);
  207. }
  208. q_idx = smp_processor_id() % ndev->real_num_tx_queues;
  209. for (i = 0; i < n; i++) {
  210. if (netvsc_ndoxdp_xmit_fm(ndev, frames[i], q_idx))
  211. break;
  212. count++;
  213. }
  214. tx_stats = &nvsc_dev->chan_table[q_idx].tx_stats;
  215. u64_stats_update_begin(&tx_stats->syncp);
  216. tx_stats->xdp_xmit += count;
  217. u64_stats_update_end(&tx_stats->syncp);
  218. return count;
  219. }