xdp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* net/core/xdp.c
  2. *
  3. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  4. * Released under terms in GPL version 2. See COPYING.
  5. */
  6. #include <linux/bpf.h>
  7. #include <linux/filter.h>
  8. #include <linux/types.h>
  9. #include <linux/mm.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/slab.h>
  12. #include <linux/idr.h>
  13. #include <linux/rhashtable.h>
  14. #include <net/page_pool.h>
  15. #include <net/xdp.h>
  16. #define REG_STATE_NEW 0x0
  17. #define REG_STATE_REGISTERED 0x1
  18. #define REG_STATE_UNREGISTERED 0x2
  19. #define REG_STATE_UNUSED 0x3
  20. static DEFINE_IDA(mem_id_pool);
  21. static DEFINE_MUTEX(mem_id_lock);
  22. #define MEM_ID_MAX 0xFFFE
  23. #define MEM_ID_MIN 1
  24. static int mem_id_next = MEM_ID_MIN;
  25. static bool mem_id_init; /* false */
  26. static struct rhashtable *mem_id_ht;
  27. struct xdp_mem_allocator {
  28. struct xdp_mem_info mem;
  29. union {
  30. void *allocator;
  31. struct page_pool *page_pool;
  32. struct zero_copy_allocator *zc_alloc;
  33. };
  34. struct rhash_head node;
  35. struct rcu_head rcu;
  36. };
  37. static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed)
  38. {
  39. const u32 *k = data;
  40. const u32 key = *k;
  41. BUILD_BUG_ON(FIELD_SIZEOF(struct xdp_mem_allocator, mem.id)
  42. != sizeof(u32));
  43. /* Use cyclic increasing ID as direct hash key */
  44. return key;
  45. }
  46. static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg,
  47. const void *ptr)
  48. {
  49. const struct xdp_mem_allocator *xa = ptr;
  50. u32 mem_id = *(u32 *)arg->key;
  51. return xa->mem.id != mem_id;
  52. }
  53. static const struct rhashtable_params mem_id_rht_params = {
  54. .nelem_hint = 64,
  55. .head_offset = offsetof(struct xdp_mem_allocator, node),
  56. .key_offset = offsetof(struct xdp_mem_allocator, mem.id),
  57. .key_len = FIELD_SIZEOF(struct xdp_mem_allocator, mem.id),
  58. .max_size = MEM_ID_MAX,
  59. .min_size = 8,
  60. .automatic_shrinking = true,
  61. .hashfn = xdp_mem_id_hashfn,
  62. .obj_cmpfn = xdp_mem_id_cmp,
  63. };
  64. static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)
  65. {
  66. struct xdp_mem_allocator *xa;
  67. xa = container_of(rcu, struct xdp_mem_allocator, rcu);
  68. /* Allow this ID to be reused */
  69. ida_simple_remove(&mem_id_pool, xa->mem.id);
  70. /* Notice, driver is expected to free the *allocator,
  71. * e.g. page_pool, and MUST also use RCU free.
  72. */
  73. /* Poison memory */
  74. xa->mem.id = 0xFFFF;
  75. xa->mem.type = 0xF0F0;
  76. xa->allocator = (void *)0xDEAD9001;
  77. kfree(xa);
  78. }
  79. void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
  80. {
  81. struct xdp_mem_allocator *xa;
  82. int id = xdp_rxq->mem.id;
  83. if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
  84. WARN(1, "Missing register, driver bug");
  85. return;
  86. }
  87. if (xdp_rxq->mem.type != MEM_TYPE_PAGE_POOL &&
  88. xdp_rxq->mem.type != MEM_TYPE_ZERO_COPY) {
  89. return;
  90. }
  91. if (id == 0)
  92. return;
  93. mutex_lock(&mem_id_lock);
  94. xa = rhashtable_lookup_fast(mem_id_ht, &id, mem_id_rht_params);
  95. if (xa && !rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params))
  96. call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
  97. mutex_unlock(&mem_id_lock);
  98. }
  99. EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
  100. void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
  101. {
  102. /* Simplify driver cleanup code paths, allow unreg "unused" */
  103. if (xdp_rxq->reg_state == REG_STATE_UNUSED)
  104. return;
  105. WARN(!(xdp_rxq->reg_state == REG_STATE_REGISTERED), "Driver BUG");
  106. xdp_rxq_info_unreg_mem_model(xdp_rxq);
  107. xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
  108. xdp_rxq->dev = NULL;
  109. /* Reset mem info to defaults */
  110. xdp_rxq->mem.id = 0;
  111. xdp_rxq->mem.type = 0;
  112. }
  113. EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
  114. static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
  115. {
  116. memset(xdp_rxq, 0, sizeof(*xdp_rxq));
  117. }
  118. /* Returns 0 on success, negative on failure */
  119. int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  120. struct net_device *dev, u32 queue_index)
  121. {
  122. if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
  123. WARN(1, "Driver promised not to register this");
  124. return -EINVAL;
  125. }
  126. if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
  127. WARN(1, "Missing unregister, handled but fix driver");
  128. xdp_rxq_info_unreg(xdp_rxq);
  129. }
  130. if (!dev) {
  131. WARN(1, "Missing net_device from driver");
  132. return -ENODEV;
  133. }
  134. /* State either UNREGISTERED or NEW */
  135. xdp_rxq_info_init(xdp_rxq);
  136. xdp_rxq->dev = dev;
  137. xdp_rxq->queue_index = queue_index;
  138. xdp_rxq->reg_state = REG_STATE_REGISTERED;
  139. return 0;
  140. }
  141. EXPORT_SYMBOL_GPL(xdp_rxq_info_reg);
  142. void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
  143. {
  144. xdp_rxq->reg_state = REG_STATE_UNUSED;
  145. }
  146. EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);
  147. bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq)
  148. {
  149. return (xdp_rxq->reg_state == REG_STATE_REGISTERED);
  150. }
  151. EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
  152. static int __mem_id_init_hash_table(void)
  153. {
  154. struct rhashtable *rht;
  155. int ret;
  156. if (unlikely(mem_id_init))
  157. return 0;
  158. rht = kzalloc(sizeof(*rht), GFP_KERNEL);
  159. if (!rht)
  160. return -ENOMEM;
  161. ret = rhashtable_init(rht, &mem_id_rht_params);
  162. if (ret < 0) {
  163. kfree(rht);
  164. return ret;
  165. }
  166. mem_id_ht = rht;
  167. smp_mb(); /* mutex lock should provide enough pairing */
  168. mem_id_init = true;
  169. return 0;
  170. }
  171. /* Allocate a cyclic ID that maps to allocator pointer.
  172. * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
  173. *
  174. * Caller must lock mem_id_lock.
  175. */
  176. static int __mem_id_cyclic_get(gfp_t gfp)
  177. {
  178. int retries = 1;
  179. int id;
  180. again:
  181. id = ida_simple_get(&mem_id_pool, mem_id_next, MEM_ID_MAX, gfp);
  182. if (id < 0) {
  183. if (id == -ENOSPC) {
  184. /* Cyclic allocator, reset next id */
  185. if (retries--) {
  186. mem_id_next = MEM_ID_MIN;
  187. goto again;
  188. }
  189. }
  190. return id; /* errno */
  191. }
  192. mem_id_next = id + 1;
  193. return id;
  194. }
  195. static bool __is_supported_mem_type(enum xdp_mem_type type)
  196. {
  197. if (type == MEM_TYPE_PAGE_POOL)
  198. return is_page_pool_compiled_in();
  199. if (type >= MEM_TYPE_MAX)
  200. return false;
  201. return true;
  202. }
  203. int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
  204. enum xdp_mem_type type, void *allocator)
  205. {
  206. struct xdp_mem_allocator *xdp_alloc;
  207. gfp_t gfp = GFP_KERNEL;
  208. int id, errno, ret;
  209. void *ptr;
  210. if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
  211. WARN(1, "Missing register, driver bug");
  212. return -EFAULT;
  213. }
  214. if (!__is_supported_mem_type(type))
  215. return -EOPNOTSUPP;
  216. xdp_rxq->mem.type = type;
  217. if (!allocator) {
  218. if (type == MEM_TYPE_PAGE_POOL || type == MEM_TYPE_ZERO_COPY)
  219. return -EINVAL; /* Setup time check page_pool req */
  220. return 0;
  221. }
  222. /* Delay init of rhashtable to save memory if feature isn't used */
  223. if (!mem_id_init) {
  224. mutex_lock(&mem_id_lock);
  225. ret = __mem_id_init_hash_table();
  226. mutex_unlock(&mem_id_lock);
  227. if (ret < 0) {
  228. WARN_ON(1);
  229. return ret;
  230. }
  231. }
  232. xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
  233. if (!xdp_alloc)
  234. return -ENOMEM;
  235. mutex_lock(&mem_id_lock);
  236. id = __mem_id_cyclic_get(gfp);
  237. if (id < 0) {
  238. errno = id;
  239. goto err;
  240. }
  241. xdp_rxq->mem.id = id;
  242. xdp_alloc->mem = xdp_rxq->mem;
  243. xdp_alloc->allocator = allocator;
  244. /* Insert allocator into ID lookup table */
  245. ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
  246. if (IS_ERR(ptr)) {
  247. errno = PTR_ERR(ptr);
  248. goto err;
  249. }
  250. mutex_unlock(&mem_id_lock);
  251. return 0;
  252. err:
  253. mutex_unlock(&mem_id_lock);
  254. kfree(xdp_alloc);
  255. return errno;
  256. }
  257. EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
  258. /* XDP RX runs under NAPI protection, and in different delivery error
  259. * scenarios (e.g. queue full), it is possible to return the xdp_frame
  260. * while still leveraging this protection. The @napi_direct boolian
  261. * is used for those calls sites. Thus, allowing for faster recycling
  262. * of xdp_frames/pages in those cases.
  263. */
  264. static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
  265. unsigned long handle)
  266. {
  267. struct xdp_mem_allocator *xa;
  268. struct page *page;
  269. switch (mem->type) {
  270. case MEM_TYPE_PAGE_POOL:
  271. rcu_read_lock();
  272. /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
  273. xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
  274. page = virt_to_head_page(data);
  275. if (xa) {
  276. napi_direct &= !xdp_return_frame_no_direct();
  277. page_pool_put_page(xa->page_pool, page, napi_direct);
  278. } else {
  279. put_page(page);
  280. }
  281. rcu_read_unlock();
  282. break;
  283. case MEM_TYPE_PAGE_SHARED:
  284. page_frag_free(data);
  285. break;
  286. case MEM_TYPE_PAGE_ORDER0:
  287. page = virt_to_page(data); /* Assumes order0 page*/
  288. put_page(page);
  289. break;
  290. case MEM_TYPE_ZERO_COPY:
  291. /* NB! Only valid from an xdp_buff! */
  292. rcu_read_lock();
  293. /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
  294. xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
  295. xa->zc_alloc->free(xa->zc_alloc, handle);
  296. rcu_read_unlock();
  297. default:
  298. /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
  299. break;
  300. }
  301. }
  302. void xdp_return_frame(struct xdp_frame *xdpf)
  303. {
  304. __xdp_return(xdpf->data, &xdpf->mem, false, 0);
  305. }
  306. EXPORT_SYMBOL_GPL(xdp_return_frame);
  307. void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
  308. {
  309. __xdp_return(xdpf->data, &xdpf->mem, true, 0);
  310. }
  311. EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
  312. void xdp_return_buff(struct xdp_buff *xdp)
  313. {
  314. __xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle);
  315. }
  316. EXPORT_SYMBOL_GPL(xdp_return_buff);
  317. int xdp_attachment_query(struct xdp_attachment_info *info,
  318. struct netdev_bpf *bpf)
  319. {
  320. bpf->prog_id = info->prog ? info->prog->aux->id : 0;
  321. bpf->prog_flags = info->prog ? info->flags : 0;
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_GPL(xdp_attachment_query);
  325. bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
  326. struct netdev_bpf *bpf)
  327. {
  328. if (info->prog && (bpf->flags ^ info->flags) & XDP_FLAGS_MODES) {
  329. NL_SET_ERR_MSG(bpf->extack,
  330. "program loaded with different flags");
  331. return false;
  332. }
  333. return true;
  334. }
  335. EXPORT_SYMBOL_GPL(xdp_attachment_flags_ok);
  336. void xdp_attachment_setup(struct xdp_attachment_info *info,
  337. struct netdev_bpf *bpf)
  338. {
  339. if (info->prog)
  340. bpf_prog_put(info->prog);
  341. info->prog = bpf->prog;
  342. info->flags = bpf->flags;
  343. }
  344. EXPORT_SYMBOL_GPL(xdp_attachment_setup);
  345. struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp)
  346. {
  347. unsigned int metasize, totsize;
  348. void *addr, *data_to_copy;
  349. struct xdp_frame *xdpf;
  350. struct page *page;
  351. /* Clone into a MEM_TYPE_PAGE_ORDER0 xdp_frame. */
  352. metasize = xdp_data_meta_unsupported(xdp) ? 0 :
  353. xdp->data - xdp->data_meta;
  354. totsize = xdp->data_end - xdp->data + metasize;
  355. if (sizeof(*xdpf) + totsize > PAGE_SIZE)
  356. return NULL;
  357. page = dev_alloc_page();
  358. if (!page)
  359. return NULL;
  360. addr = page_to_virt(page);
  361. xdpf = addr;
  362. memset(xdpf, 0, sizeof(*xdpf));
  363. addr += sizeof(*xdpf);
  364. data_to_copy = metasize ? xdp->data_meta : xdp->data;
  365. memcpy(addr, data_to_copy, totsize);
  366. xdpf->data = addr + metasize;
  367. xdpf->len = totsize - metasize;
  368. xdpf->headroom = 0;
  369. xdpf->metasize = metasize;
  370. xdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
  371. xdp_return_buff(xdp);
  372. return xdpf;
  373. }
  374. EXPORT_SYMBOL_GPL(xdp_convert_zc_to_xdp_frame);