main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Copyright (C) 2017-2018 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 "../nfpcore/nfp_nffw.h"
  36. #include "../nfpcore/nfp_nsp.h"
  37. #include "../nfp_app.h"
  38. #include "../nfp_main.h"
  39. #include "../nfp_net.h"
  40. #include "../nfp_port.h"
  41. #include "fw.h"
  42. #include "main.h"
  43. const struct rhashtable_params nfp_bpf_maps_neutral_params = {
  44. .nelem_hint = 4,
  45. .key_len = FIELD_SIZEOF(struct bpf_map, id),
  46. .key_offset = offsetof(struct nfp_bpf_neutral_map, map_id),
  47. .head_offset = offsetof(struct nfp_bpf_neutral_map, l),
  48. .automatic_shrinking = true,
  49. };
  50. static bool nfp_net_ebpf_capable(struct nfp_net *nn)
  51. {
  52. #ifdef __LITTLE_ENDIAN
  53. if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
  54. nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
  55. return true;
  56. #endif
  57. return false;
  58. }
  59. static int
  60. nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
  61. struct bpf_prog *prog, struct netlink_ext_ack *extack)
  62. {
  63. bool running, xdp_running;
  64. if (!nfp_net_ebpf_capable(nn))
  65. return -EINVAL;
  66. running = nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
  67. xdp_running = running && nn->xdp_hw.prog;
  68. if (!prog && !xdp_running)
  69. return 0;
  70. if (prog && running && !xdp_running)
  71. return -EBUSY;
  72. return nfp_net_bpf_offload(nn, prog, running, extack);
  73. }
  74. static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
  75. {
  76. return nfp_net_ebpf_capable(nn) ? "BPF" : "";
  77. }
  78. static int
  79. nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
  80. {
  81. struct nfp_pf *pf = app->pf;
  82. struct nfp_bpf_vnic *bv;
  83. int err;
  84. if (!pf->eth_tbl) {
  85. nfp_err(pf->cpp, "No ETH table\n");
  86. return -EINVAL;
  87. }
  88. if (pf->max_data_vnics != pf->eth_tbl->count) {
  89. nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
  90. pf->max_data_vnics, pf->eth_tbl->count);
  91. return -EINVAL;
  92. }
  93. bv = kzalloc(sizeof(*bv), GFP_KERNEL);
  94. if (!bv)
  95. return -ENOMEM;
  96. nn->app_priv = bv;
  97. err = nfp_app_nic_vnic_alloc(app, nn, id);
  98. if (err)
  99. goto err_free_priv;
  100. bv->start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
  101. bv->tgt_done = nn_readw(nn, NFP_NET_CFG_BPF_DONE);
  102. return 0;
  103. err_free_priv:
  104. kfree(nn->app_priv);
  105. return err;
  106. }
  107. static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
  108. {
  109. struct nfp_bpf_vnic *bv = nn->app_priv;
  110. WARN_ON(bv->tc_prog);
  111. kfree(bv);
  112. }
  113. static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
  114. void *type_data, void *cb_priv)
  115. {
  116. struct tc_cls_bpf_offload *cls_bpf = type_data;
  117. struct nfp_net *nn = cb_priv;
  118. struct bpf_prog *oldprog;
  119. struct nfp_bpf_vnic *bv;
  120. int err;
  121. if (type != TC_SETUP_CLSBPF) {
  122. NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
  123. "only offload of BPF classifiers supported");
  124. return -EOPNOTSUPP;
  125. }
  126. if (!tc_cls_can_offload_and_chain0(nn->dp.netdev, &cls_bpf->common))
  127. return -EOPNOTSUPP;
  128. if (!nfp_net_ebpf_capable(nn)) {
  129. NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
  130. "NFP firmware does not support eBPF offload");
  131. return -EOPNOTSUPP;
  132. }
  133. if (cls_bpf->common.protocol != htons(ETH_P_ALL)) {
  134. NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
  135. "only ETH_P_ALL supported as filter protocol");
  136. return -EOPNOTSUPP;
  137. }
  138. /* Only support TC direct action */
  139. if (!cls_bpf->exts_integrated ||
  140. tcf_exts_has_actions(cls_bpf->exts)) {
  141. NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
  142. "only direct action with no legacy actions supported");
  143. return -EOPNOTSUPP;
  144. }
  145. if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
  146. return -EOPNOTSUPP;
  147. bv = nn->app_priv;
  148. oldprog = cls_bpf->oldprog;
  149. /* Don't remove if oldprog doesn't match driver's state */
  150. if (bv->tc_prog != oldprog) {
  151. oldprog = NULL;
  152. if (!cls_bpf->prog)
  153. return 0;
  154. }
  155. err = nfp_net_bpf_offload(nn, cls_bpf->prog, oldprog,
  156. cls_bpf->common.extack);
  157. if (err)
  158. return err;
  159. bv->tc_prog = cls_bpf->prog;
  160. nn->port->tc_offload_cnt = !!bv->tc_prog;
  161. return 0;
  162. }
  163. static int nfp_bpf_setup_tc_block(struct net_device *netdev,
  164. struct tc_block_offload *f)
  165. {
  166. struct nfp_net *nn = netdev_priv(netdev);
  167. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  168. return -EOPNOTSUPP;
  169. switch (f->command) {
  170. case TC_BLOCK_BIND:
  171. return tcf_block_cb_register(f->block,
  172. nfp_bpf_setup_tc_block_cb,
  173. nn, nn, f->extack);
  174. case TC_BLOCK_UNBIND:
  175. tcf_block_cb_unregister(f->block,
  176. nfp_bpf_setup_tc_block_cb,
  177. nn);
  178. return 0;
  179. default:
  180. return -EOPNOTSUPP;
  181. }
  182. }
  183. static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
  184. enum tc_setup_type type, void *type_data)
  185. {
  186. switch (type) {
  187. case TC_SETUP_BLOCK:
  188. return nfp_bpf_setup_tc_block(netdev, type_data);
  189. default:
  190. return -EOPNOTSUPP;
  191. }
  192. }
  193. static int
  194. nfp_bpf_check_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
  195. {
  196. struct nfp_net *nn = netdev_priv(netdev);
  197. unsigned int max_mtu;
  198. if (~nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
  199. return 0;
  200. max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
  201. if (new_mtu > max_mtu) {
  202. nn_info(nn, "BPF offload active, MTU over %u not supported\n",
  203. max_mtu);
  204. return -EBUSY;
  205. }
  206. return 0;
  207. }
  208. static int
  209. nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
  210. u32 length)
  211. {
  212. struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
  213. struct nfp_cpp *cpp = bpf->app->pf->cpp;
  214. if (length < sizeof(*cap)) {
  215. nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
  216. return -EINVAL;
  217. }
  218. bpf->adjust_head.flags = readl(&cap->flags);
  219. bpf->adjust_head.off_min = readl(&cap->off_min);
  220. bpf->adjust_head.off_max = readl(&cap->off_max);
  221. bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
  222. bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
  223. if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
  224. nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
  225. return -EINVAL;
  226. }
  227. if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
  228. !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
  229. nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
  230. memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
  231. return 0;
  232. }
  233. return 0;
  234. }
  235. static int
  236. nfp_bpf_parse_cap_func(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
  237. {
  238. struct nfp_bpf_cap_tlv_func __iomem *cap = value;
  239. if (length < sizeof(*cap)) {
  240. nfp_err(bpf->app->cpp, "truncated function TLV: %d\n", length);
  241. return -EINVAL;
  242. }
  243. switch (readl(&cap->func_id)) {
  244. case BPF_FUNC_map_lookup_elem:
  245. bpf->helpers.map_lookup = readl(&cap->func_addr);
  246. break;
  247. case BPF_FUNC_map_update_elem:
  248. bpf->helpers.map_update = readl(&cap->func_addr);
  249. break;
  250. case BPF_FUNC_map_delete_elem:
  251. bpf->helpers.map_delete = readl(&cap->func_addr);
  252. break;
  253. case BPF_FUNC_perf_event_output:
  254. bpf->helpers.perf_event_output = readl(&cap->func_addr);
  255. break;
  256. }
  257. return 0;
  258. }
  259. static int
  260. nfp_bpf_parse_cap_maps(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
  261. {
  262. struct nfp_bpf_cap_tlv_maps __iomem *cap = value;
  263. if (length < sizeof(*cap)) {
  264. nfp_err(bpf->app->cpp, "truncated maps TLV: %d\n", length);
  265. return -EINVAL;
  266. }
  267. bpf->maps.types = readl(&cap->types);
  268. bpf->maps.max_maps = readl(&cap->max_maps);
  269. bpf->maps.max_elems = readl(&cap->max_elems);
  270. bpf->maps.max_key_sz = readl(&cap->max_key_sz);
  271. bpf->maps.max_val_sz = readl(&cap->max_val_sz);
  272. bpf->maps.max_elem_sz = readl(&cap->max_elem_sz);
  273. return 0;
  274. }
  275. static int
  276. nfp_bpf_parse_cap_random(struct nfp_app_bpf *bpf, void __iomem *value,
  277. u32 length)
  278. {
  279. bpf->pseudo_random = true;
  280. return 0;
  281. }
  282. static int
  283. nfp_bpf_parse_cap_qsel(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
  284. {
  285. bpf->queue_select = true;
  286. return 0;
  287. }
  288. static int nfp_bpf_parse_capabilities(struct nfp_app *app)
  289. {
  290. struct nfp_cpp *cpp = app->pf->cpp;
  291. struct nfp_cpp_area *area;
  292. u8 __iomem *mem, *start;
  293. mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
  294. 8, &area);
  295. if (IS_ERR(mem))
  296. return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
  297. start = mem;
  298. while (mem - start + 8 <= nfp_cpp_area_size(area)) {
  299. u8 __iomem *value;
  300. u32 type, length;
  301. type = readl(mem);
  302. length = readl(mem + 4);
  303. value = mem + 8;
  304. mem += 8 + length;
  305. if (mem - start > nfp_cpp_area_size(area))
  306. goto err_release_free;
  307. switch (type) {
  308. case NFP_BPF_CAP_TYPE_FUNC:
  309. if (nfp_bpf_parse_cap_func(app->priv, value, length))
  310. goto err_release_free;
  311. break;
  312. case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
  313. if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
  314. length))
  315. goto err_release_free;
  316. break;
  317. case NFP_BPF_CAP_TYPE_MAPS:
  318. if (nfp_bpf_parse_cap_maps(app->priv, value, length))
  319. goto err_release_free;
  320. break;
  321. case NFP_BPF_CAP_TYPE_RANDOM:
  322. if (nfp_bpf_parse_cap_random(app->priv, value, length))
  323. goto err_release_free;
  324. break;
  325. case NFP_BPF_CAP_TYPE_QUEUE_SELECT:
  326. if (nfp_bpf_parse_cap_qsel(app->priv, value, length))
  327. goto err_release_free;
  328. break;
  329. default:
  330. nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
  331. break;
  332. }
  333. }
  334. if (mem - start != nfp_cpp_area_size(area)) {
  335. nfp_err(cpp, "BPF capabilities left after parsing, parsed:%zd total length:%zu\n",
  336. mem - start, nfp_cpp_area_size(area));
  337. goto err_release_free;
  338. }
  339. nfp_cpp_area_release_free(area);
  340. return 0;
  341. err_release_free:
  342. nfp_err(cpp, "invalid BPF capabilities at offset:%zd\n", mem - start);
  343. nfp_cpp_area_release_free(area);
  344. return -EINVAL;
  345. }
  346. static int nfp_bpf_ndo_init(struct nfp_app *app, struct net_device *netdev)
  347. {
  348. struct nfp_app_bpf *bpf = app->priv;
  349. return bpf_offload_dev_netdev_register(bpf->bpf_dev, netdev);
  350. }
  351. static void nfp_bpf_ndo_uninit(struct nfp_app *app, struct net_device *netdev)
  352. {
  353. struct nfp_app_bpf *bpf = app->priv;
  354. bpf_offload_dev_netdev_unregister(bpf->bpf_dev, netdev);
  355. }
  356. static int nfp_bpf_init(struct nfp_app *app)
  357. {
  358. struct nfp_app_bpf *bpf;
  359. int err;
  360. bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
  361. if (!bpf)
  362. return -ENOMEM;
  363. bpf->app = app;
  364. app->priv = bpf;
  365. skb_queue_head_init(&bpf->cmsg_replies);
  366. init_waitqueue_head(&bpf->cmsg_wq);
  367. INIT_LIST_HEAD(&bpf->map_list);
  368. err = rhashtable_init(&bpf->maps_neutral, &nfp_bpf_maps_neutral_params);
  369. if (err)
  370. goto err_free_bpf;
  371. err = nfp_bpf_parse_capabilities(app);
  372. if (err)
  373. goto err_free_neutral_maps;
  374. bpf->bpf_dev = bpf_offload_dev_create();
  375. err = PTR_ERR_OR_ZERO(bpf->bpf_dev);
  376. if (err)
  377. goto err_free_neutral_maps;
  378. return 0;
  379. err_free_neutral_maps:
  380. rhashtable_destroy(&bpf->maps_neutral);
  381. err_free_bpf:
  382. kfree(bpf);
  383. return err;
  384. }
  385. static void nfp_check_rhashtable_empty(void *ptr, void *arg)
  386. {
  387. WARN_ON_ONCE(1);
  388. }
  389. static void nfp_bpf_clean(struct nfp_app *app)
  390. {
  391. struct nfp_app_bpf *bpf = app->priv;
  392. bpf_offload_dev_destroy(bpf->bpf_dev);
  393. WARN_ON(!skb_queue_empty(&bpf->cmsg_replies));
  394. WARN_ON(!list_empty(&bpf->map_list));
  395. WARN_ON(bpf->maps_in_use || bpf->map_elems_in_use);
  396. rhashtable_free_and_destroy(&bpf->maps_neutral,
  397. nfp_check_rhashtable_empty, NULL);
  398. kfree(bpf);
  399. }
  400. const struct nfp_app_type app_bpf = {
  401. .id = NFP_APP_BPF_NIC,
  402. .name = "ebpf",
  403. .ctrl_cap_mask = 0,
  404. .init = nfp_bpf_init,
  405. .clean = nfp_bpf_clean,
  406. .check_mtu = nfp_bpf_check_mtu,
  407. .extra_cap = nfp_bpf_extra_cap,
  408. .ndo_init = nfp_bpf_ndo_init,
  409. .ndo_uninit = nfp_bpf_ndo_uninit,
  410. .vnic_alloc = nfp_bpf_vnic_alloc,
  411. .vnic_free = nfp_bpf_vnic_free,
  412. .ctrl_msg_rx = nfp_bpf_ctrl_msg_rx,
  413. .ctrl_msg_rx_raw = nfp_bpf_ctrl_msg_rx_raw,
  414. .setup_tc = nfp_bpf_setup_tc,
  415. .bpf = nfp_ndo_bpf,
  416. .xdp_offload = nfp_bpf_xdp_offload,
  417. };