main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
  43. nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
  44. return true;
  45. return false;
  46. }
  47. static int
  48. nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
  49. struct bpf_prog *prog)
  50. {
  51. struct tc_cls_bpf_offload cmd = {
  52. .prog = prog,
  53. };
  54. int ret;
  55. if (!nfp_net_ebpf_capable(nn))
  56. return -EINVAL;
  57. if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
  58. if (!nn->dp.bpf_offload_xdp)
  59. return prog ? -EBUSY : 0;
  60. cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
  61. } else {
  62. if (!prog)
  63. return 0;
  64. cmd.command = TC_CLSBPF_ADD;
  65. }
  66. ret = nfp_net_bpf_offload(nn, &cmd);
  67. /* Stop offload if replace not possible */
  68. if (ret && cmd.command == TC_CLSBPF_REPLACE)
  69. nfp_bpf_xdp_offload(app, nn, NULL);
  70. nn->dp.bpf_offload_xdp = prog && !ret;
  71. return ret;
  72. }
  73. static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
  74. {
  75. return nfp_net_ebpf_capable(nn) ? "BPF" : "";
  76. }
  77. static int
  78. nfp_bpf_vnic_init(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
  79. {
  80. struct nfp_net_bpf_priv *priv;
  81. int ret;
  82. /* Limit to single port, otherwise it's just a NIC */
  83. if (id > 0) {
  84. nfp_warn(app->cpp,
  85. "BPF NIC doesn't support more than one port right now\n");
  86. nn->port = nfp_port_alloc(app, NFP_PORT_INVALID, nn->dp.netdev);
  87. return PTR_ERR_OR_ZERO(nn->port);
  88. }
  89. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  90. if (!priv)
  91. return -ENOMEM;
  92. nn->app_priv = priv;
  93. spin_lock_init(&priv->rx_filter_lock);
  94. setup_timer(&priv->rx_filter_stats_timer,
  95. nfp_net_filter_stats_timer, (unsigned long)nn);
  96. ret = nfp_app_nic_vnic_init(app, nn, id);
  97. if (ret)
  98. kfree(priv);
  99. return ret;
  100. }
  101. static void nfp_bpf_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
  102. {
  103. if (nn->dp.bpf_offload_xdp)
  104. nfp_bpf_xdp_offload(app, nn, NULL);
  105. kfree(nn->app_priv);
  106. }
  107. static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
  108. enum tc_setup_type type, u32 handle, __be16 proto,
  109. struct tc_to_netdev *tc)
  110. {
  111. struct nfp_net *nn = netdev_priv(netdev);
  112. if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
  113. return -EOPNOTSUPP;
  114. if (proto != htons(ETH_P_ALL))
  115. return -EOPNOTSUPP;
  116. if (type == TC_SETUP_CLSBPF && nfp_net_ebpf_capable(nn)) {
  117. if (!nn->dp.bpf_offload_xdp)
  118. return nfp_net_bpf_offload(nn, tc->cls_bpf);
  119. else
  120. return -EBUSY;
  121. }
  122. return -EINVAL;
  123. }
  124. static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
  125. {
  126. return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
  127. }
  128. const struct nfp_app_type app_bpf = {
  129. .id = NFP_APP_BPF_NIC,
  130. .name = "ebpf",
  131. .extra_cap = nfp_bpf_extra_cap,
  132. .vnic_init = nfp_bpf_vnic_init,
  133. .vnic_clean = nfp_bpf_vnic_clean,
  134. .setup_tc = nfp_bpf_setup_tc,
  135. .tc_busy = nfp_bpf_tc_busy,
  136. .xdp_offload = nfp_bpf_xdp_offload,
  137. };