main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2017 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. #include <net/pkt_cls.h>
  34. #include "../nfpcore/nfp_cpp.h"
  35. #include "../nfp_app.h"
  36. #include "../nfp_main.h"
  37. #include "../nfp_net.h"
  38. #include "../nfp_port.h"
  39. #include "main.h"
  40. static bool nfp_net_ebpf_capable(struct nfp_net *nn)
  41. {
  42. #ifdef __LITTLE_ENDIAN
  43. if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
  44. nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
  45. return true;
  46. #endif
  47. return false;
  48. }
  49. static int
  50. nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
  51. struct bpf_prog *prog)
  52. {
  53. struct tc_cls_bpf_offload cmd = {
  54. .prog = prog,
  55. };
  56. int ret;
  57. if (!nfp_net_ebpf_capable(nn))
  58. return -EINVAL;
  59. if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
  60. if (!nn->dp.bpf_offload_xdp)
  61. return prog ? -EBUSY : 0;
  62. cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
  63. } else {
  64. if (!prog)
  65. return 0;
  66. cmd.command = TC_CLSBPF_ADD;
  67. }
  68. ret = nfp_net_bpf_offload(nn, &cmd);
  69. /* Stop offload if replace not possible */
  70. if (ret && cmd.command == TC_CLSBPF_REPLACE)
  71. nfp_bpf_xdp_offload(app, nn, NULL);
  72. nn->dp.bpf_offload_xdp = prog && !ret;
  73. return ret;
  74. }
  75. static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
  76. {
  77. return nfp_net_ebpf_capable(nn) ? "BPF" : "";
  78. }
  79. static int
  80. nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
  81. {
  82. struct nfp_net_bpf_priv *priv;
  83. int ret;
  84. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  85. if (!priv)
  86. return -ENOMEM;
  87. nn->app_priv = priv;
  88. spin_lock_init(&priv->rx_filter_lock);
  89. setup_timer(&priv->rx_filter_stats_timer,
  90. nfp_net_filter_stats_timer, (unsigned long)nn);
  91. ret = nfp_app_nic_vnic_alloc(app, nn, id);
  92. if (ret)
  93. kfree(priv);
  94. return ret;
  95. }
  96. static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
  97. {
  98. if (nn->dp.bpf_offload_xdp)
  99. nfp_bpf_xdp_offload(app, nn, NULL);
  100. kfree(nn->app_priv);
  101. }
  102. static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
  103. void *type_data, void *cb_priv)
  104. {
  105. struct tc_cls_bpf_offload *cls_bpf = type_data;
  106. struct nfp_net *nn = cb_priv;
  107. switch (type) {
  108. case TC_SETUP_CLSBPF:
  109. if (!nfp_net_ebpf_capable(nn) ||
  110. cls_bpf->common.protocol != htons(ETH_P_ALL) ||
  111. cls_bpf->common.chain_index)
  112. return -EOPNOTSUPP;
  113. return nfp_net_bpf_offload(nn, cls_bpf);
  114. default:
  115. return -EOPNOTSUPP;
  116. }
  117. }
  118. static int nfp_bpf_setup_tc_block(struct net_device *netdev,
  119. struct tc_block_offload *f)
  120. {
  121. struct nfp_net *nn = netdev_priv(netdev);
  122. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  123. return -EOPNOTSUPP;
  124. switch (f->command) {
  125. case TC_BLOCK_BIND:
  126. return tcf_block_cb_register(f->block,
  127. nfp_bpf_setup_tc_block_cb,
  128. nn, nn);
  129. case TC_BLOCK_UNBIND:
  130. tcf_block_cb_unregister(f->block,
  131. nfp_bpf_setup_tc_block_cb,
  132. nn);
  133. return 0;
  134. default:
  135. return -EOPNOTSUPP;
  136. }
  137. }
  138. static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
  139. enum tc_setup_type type, void *type_data)
  140. {
  141. switch (type) {
  142. case TC_SETUP_BLOCK:
  143. return nfp_bpf_setup_tc_block(netdev, type_data);
  144. default:
  145. return -EOPNOTSUPP;
  146. }
  147. }
  148. static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
  149. {
  150. return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
  151. }
  152. const struct nfp_app_type app_bpf = {
  153. .id = NFP_APP_BPF_NIC,
  154. .name = "ebpf",
  155. .extra_cap = nfp_bpf_extra_cap,
  156. .vnic_alloc = nfp_bpf_vnic_alloc,
  157. .vnic_free = nfp_bpf_vnic_free,
  158. .setup_tc = nfp_bpf_setup_tc,
  159. .tc_busy = nfp_bpf_tc_busy,
  160. .xdp_offload = nfp_bpf_xdp_offload,
  161. };