nfp_net_offload.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2016 Netronome Systems, Inc.
  3. *
  4. * This software is dual licensed under the GNU General License Version 2,
  5. * June 1991 as shown in the file COPYING in the top-level directory of this
  6. * source tree or the BSD 2-Clause License provided below. You have the
  7. * option to license this software under the complete terms of either license.
  8. *
  9. * The BSD 2-Clause License:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /*
  34. * nfp_net_offload.c
  35. * Netronome network device driver: TC offload functions for PF and VF
  36. */
  37. #include <linux/kernel.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/pci.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/timer.h>
  42. #include <linux/list.h>
  43. #include <net/pkt_cls.h>
  44. #include <net/tc_act/tc_gact.h>
  45. #include <net/tc_act/tc_mirred.h>
  46. #include "nfp_bpf.h"
  47. #include "nfp_net_ctrl.h"
  48. #include "nfp_net.h"
  49. void nfp_net_filter_stats_timer(unsigned long data)
  50. {
  51. struct nfp_net *nn = (void *)data;
  52. struct nfp_stat_pair latest;
  53. spin_lock_bh(&nn->rx_filter_lock);
  54. if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
  55. mod_timer(&nn->rx_filter_stats_timer,
  56. jiffies + NFP_NET_STAT_POLL_IVL);
  57. spin_unlock_bh(&nn->rx_filter_lock);
  58. latest.pkts = nn_readq(nn, NFP_NET_CFG_STATS_APP1_FRAMES);
  59. latest.bytes = nn_readq(nn, NFP_NET_CFG_STATS_APP1_BYTES);
  60. if (latest.pkts != nn->rx_filter.pkts)
  61. nn->rx_filter_change = jiffies;
  62. nn->rx_filter = latest;
  63. }
  64. static void nfp_net_bpf_stats_reset(struct nfp_net *nn)
  65. {
  66. nn->rx_filter.pkts = nn_readq(nn, NFP_NET_CFG_STATS_APP1_FRAMES);
  67. nn->rx_filter.bytes = nn_readq(nn, NFP_NET_CFG_STATS_APP1_BYTES);
  68. nn->rx_filter_prev = nn->rx_filter;
  69. nn->rx_filter_change = jiffies;
  70. }
  71. static int
  72. nfp_net_bpf_stats_update(struct nfp_net *nn, struct tc_cls_bpf_offload *cls_bpf)
  73. {
  74. struct tc_action *a;
  75. LIST_HEAD(actions);
  76. u64 bytes, pkts;
  77. pkts = nn->rx_filter.pkts - nn->rx_filter_prev.pkts;
  78. bytes = nn->rx_filter.bytes - nn->rx_filter_prev.bytes;
  79. bytes -= pkts * ETH_HLEN;
  80. nn->rx_filter_prev = nn->rx_filter;
  81. preempt_disable();
  82. tcf_exts_to_list(cls_bpf->exts, &actions);
  83. list_for_each_entry(a, &actions, list)
  84. tcf_action_stats_update(a, bytes, pkts, nn->rx_filter_change);
  85. preempt_enable();
  86. return 0;
  87. }
  88. static int
  89. nfp_net_bpf_get_act(struct nfp_net *nn, struct tc_cls_bpf_offload *cls_bpf)
  90. {
  91. const struct tc_action *a;
  92. LIST_HEAD(actions);
  93. if (!cls_bpf->exts)
  94. return NN_ACT_XDP;
  95. /* TC direct action */
  96. if (cls_bpf->exts_integrated) {
  97. if (tc_no_actions(cls_bpf->exts))
  98. return NN_ACT_DIRECT;
  99. return -EOPNOTSUPP;
  100. }
  101. /* TC legacy mode */
  102. if (!tc_single_action(cls_bpf->exts))
  103. return -EOPNOTSUPP;
  104. tcf_exts_to_list(cls_bpf->exts, &actions);
  105. list_for_each_entry(a, &actions, list) {
  106. if (is_tcf_gact_shot(a))
  107. return NN_ACT_TC_DROP;
  108. if (is_tcf_mirred_egress_redirect(a) &&
  109. tcf_mirred_ifindex(a) == nn->dp.netdev->ifindex)
  110. return NN_ACT_TC_REDIR;
  111. }
  112. return -EOPNOTSUPP;
  113. }
  114. static int
  115. nfp_net_bpf_offload_prepare(struct nfp_net *nn,
  116. struct tc_cls_bpf_offload *cls_bpf,
  117. struct nfp_bpf_result *res,
  118. void **code, dma_addr_t *dma_addr, u16 max_instr)
  119. {
  120. unsigned int code_sz = max_instr * sizeof(u64);
  121. enum nfp_bpf_action_type act;
  122. u16 start_off, done_off;
  123. unsigned int max_mtu;
  124. int ret;
  125. if (!IS_ENABLED(CONFIG_BPF_SYSCALL))
  126. return -EOPNOTSUPP;
  127. ret = nfp_net_bpf_get_act(nn, cls_bpf);
  128. if (ret < 0)
  129. return ret;
  130. act = ret;
  131. max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
  132. if (max_mtu < nn->dp.netdev->mtu) {
  133. nn_info(nn, "BPF offload not supported with MTU larger than HW packet split boundary\n");
  134. return -EOPNOTSUPP;
  135. }
  136. start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
  137. done_off = nn_readw(nn, NFP_NET_CFG_BPF_DONE);
  138. *code = dma_zalloc_coherent(nn->dp.dev, code_sz, dma_addr, GFP_KERNEL);
  139. if (!*code)
  140. return -ENOMEM;
  141. ret = nfp_bpf_jit(cls_bpf->prog, *code, act, start_off, done_off,
  142. max_instr, res);
  143. if (ret)
  144. goto out;
  145. return 0;
  146. out:
  147. dma_free_coherent(nn->dp.dev, code_sz, *code, *dma_addr);
  148. return ret;
  149. }
  150. static void
  151. nfp_net_bpf_load_and_start(struct nfp_net *nn, u32 tc_flags,
  152. void *code, dma_addr_t dma_addr,
  153. unsigned int code_sz, unsigned int n_instr,
  154. bool dense_mode)
  155. {
  156. u64 bpf_addr = dma_addr;
  157. int err;
  158. nn->dp.bpf_offload_skip_sw = !!(tc_flags & TCA_CLS_FLAGS_SKIP_SW);
  159. if (dense_mode)
  160. bpf_addr |= NFP_NET_CFG_BPF_CFG_8CTX;
  161. nn_writew(nn, NFP_NET_CFG_BPF_SIZE, n_instr);
  162. nn_writeq(nn, NFP_NET_CFG_BPF_ADDR, bpf_addr);
  163. /* Load up the JITed code */
  164. err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_BPF);
  165. if (err)
  166. nn_err(nn, "FW command error while loading BPF: %d\n", err);
  167. /* Enable passing packets through BPF function */
  168. nn->dp.ctrl |= NFP_NET_CFG_CTRL_BPF;
  169. nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
  170. err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
  171. if (err)
  172. nn_err(nn, "FW command error while enabling BPF: %d\n", err);
  173. dma_free_coherent(nn->dp.dev, code_sz, code, dma_addr);
  174. nfp_net_bpf_stats_reset(nn);
  175. mod_timer(&nn->rx_filter_stats_timer, jiffies + NFP_NET_STAT_POLL_IVL);
  176. }
  177. static int nfp_net_bpf_stop(struct nfp_net *nn)
  178. {
  179. if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF))
  180. return 0;
  181. spin_lock_bh(&nn->rx_filter_lock);
  182. nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_BPF;
  183. spin_unlock_bh(&nn->rx_filter_lock);
  184. nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
  185. del_timer_sync(&nn->rx_filter_stats_timer);
  186. nn->dp.bpf_offload_skip_sw = 0;
  187. return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
  188. }
  189. int nfp_net_bpf_offload(struct nfp_net *nn, struct tc_cls_bpf_offload *cls_bpf)
  190. {
  191. struct nfp_bpf_result res;
  192. dma_addr_t dma_addr;
  193. u16 max_instr;
  194. void *code;
  195. int err;
  196. max_instr = nn_readw(nn, NFP_NET_CFG_BPF_MAX_LEN);
  197. switch (cls_bpf->command) {
  198. case TC_CLSBPF_REPLACE:
  199. /* There is nothing stopping us from implementing seamless
  200. * replace but the simple method of loading I adopted in
  201. * the firmware does not handle atomic replace (i.e. we have to
  202. * stop the BPF offload and re-enable it). Leaking-in a few
  203. * frames which didn't have BPF applied in the hardware should
  204. * be fine if software fallback is available, though.
  205. */
  206. if (nn->dp.bpf_offload_skip_sw)
  207. return -EBUSY;
  208. err = nfp_net_bpf_offload_prepare(nn, cls_bpf, &res, &code,
  209. &dma_addr, max_instr);
  210. if (err)
  211. return err;
  212. nfp_net_bpf_stop(nn);
  213. nfp_net_bpf_load_and_start(nn, cls_bpf->gen_flags, code,
  214. dma_addr, max_instr * sizeof(u64),
  215. res.n_instr, res.dense_mode);
  216. return 0;
  217. case TC_CLSBPF_ADD:
  218. if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
  219. return -EBUSY;
  220. err = nfp_net_bpf_offload_prepare(nn, cls_bpf, &res, &code,
  221. &dma_addr, max_instr);
  222. if (err)
  223. return err;
  224. nfp_net_bpf_load_and_start(nn, cls_bpf->gen_flags, code,
  225. dma_addr, max_instr * sizeof(u64),
  226. res.n_instr, res.dense_mode);
  227. return 0;
  228. case TC_CLSBPF_DESTROY:
  229. return nfp_net_bpf_stop(nn);
  230. case TC_CLSBPF_STATS:
  231. return nfp_net_bpf_stats_update(nn, cls_bpf);
  232. default:
  233. return -EOPNOTSUPP;
  234. }
  235. }