xdp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. static 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 (id == 0)
  84. return;
  85. mutex_lock(&mem_id_lock);
  86. xa = rhashtable_lookup_fast(mem_id_ht, &id, mem_id_rht_params);
  87. if (xa && !rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params))
  88. call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
  89. mutex_unlock(&mem_id_lock);
  90. }
  91. void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
  92. {
  93. /* Simplify driver cleanup code paths, allow unreg "unused" */
  94. if (xdp_rxq->reg_state == REG_STATE_UNUSED)
  95. return;
  96. WARN(!(xdp_rxq->reg_state == REG_STATE_REGISTERED), "Driver BUG");
  97. __xdp_rxq_info_unreg_mem_model(xdp_rxq);
  98. xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
  99. xdp_rxq->dev = NULL;
  100. /* Reset mem info to defaults */
  101. xdp_rxq->mem.id = 0;
  102. xdp_rxq->mem.type = 0;
  103. }
  104. EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
  105. static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
  106. {
  107. memset(xdp_rxq, 0, sizeof(*xdp_rxq));
  108. }
  109. /* Returns 0 on success, negative on failure */
  110. int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  111. struct net_device *dev, u32 queue_index)
  112. {
  113. if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
  114. WARN(1, "Driver promised not to register this");
  115. return -EINVAL;
  116. }
  117. if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
  118. WARN(1, "Missing unregister, handled but fix driver");
  119. xdp_rxq_info_unreg(xdp_rxq);
  120. }
  121. if (!dev) {
  122. WARN(1, "Missing net_device from driver");
  123. return -ENODEV;
  124. }
  125. /* State either UNREGISTERED or NEW */
  126. xdp_rxq_info_init(xdp_rxq);
  127. xdp_rxq->dev = dev;
  128. xdp_rxq->queue_index = queue_index;
  129. xdp_rxq->reg_state = REG_STATE_REGISTERED;
  130. return 0;
  131. }
  132. EXPORT_SYMBOL_GPL(xdp_rxq_info_reg);
  133. void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
  134. {
  135. xdp_rxq->reg_state = REG_STATE_UNUSED;
  136. }
  137. EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);
  138. bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq)
  139. {
  140. return (xdp_rxq->reg_state == REG_STATE_REGISTERED);
  141. }
  142. EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
  143. static int __mem_id_init_hash_table(void)
  144. {
  145. struct rhashtable *rht;
  146. int ret;
  147. if (unlikely(mem_id_init))
  148. return 0;
  149. rht = kzalloc(sizeof(*rht), GFP_KERNEL);
  150. if (!rht)
  151. return -ENOMEM;
  152. ret = rhashtable_init(rht, &mem_id_rht_params);
  153. if (ret < 0) {
  154. kfree(rht);
  155. return ret;
  156. }
  157. mem_id_ht = rht;
  158. smp_mb(); /* mutex lock should provide enough pairing */
  159. mem_id_init = true;
  160. return 0;
  161. }
  162. /* Allocate a cyclic ID that maps to allocator pointer.
  163. * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
  164. *
  165. * Caller must lock mem_id_lock.
  166. */
  167. static int __mem_id_cyclic_get(gfp_t gfp)
  168. {
  169. int retries = 1;
  170. int id;
  171. again:
  172. id = ida_simple_get(&mem_id_pool, mem_id_next, MEM_ID_MAX, gfp);
  173. if (id < 0) {
  174. if (id == -ENOSPC) {
  175. /* Cyclic allocator, reset next id */
  176. if (retries--) {
  177. mem_id_next = MEM_ID_MIN;
  178. goto again;
  179. }
  180. }
  181. return id; /* errno */
  182. }
  183. mem_id_next = id + 1;
  184. return id;
  185. }
  186. static bool __is_supported_mem_type(enum xdp_mem_type type)
  187. {
  188. if (type == MEM_TYPE_PAGE_POOL)
  189. return is_page_pool_compiled_in();
  190. if (type >= MEM_TYPE_MAX)
  191. return false;
  192. return true;
  193. }
  194. int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
  195. enum xdp_mem_type type, void *allocator)
  196. {
  197. struct xdp_mem_allocator *xdp_alloc;
  198. gfp_t gfp = GFP_KERNEL;
  199. int id, errno, ret;
  200. void *ptr;
  201. if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
  202. WARN(1, "Missing register, driver bug");
  203. return -EFAULT;
  204. }
  205. if (!__is_supported_mem_type(type))
  206. return -EOPNOTSUPP;
  207. xdp_rxq->mem.type = type;
  208. if (!allocator) {
  209. if (type == MEM_TYPE_PAGE_POOL || type == MEM_TYPE_ZERO_COPY)
  210. return -EINVAL; /* Setup time check page_pool req */
  211. return 0;
  212. }
  213. /* Delay init of rhashtable to save memory if feature isn't used */
  214. if (!mem_id_init) {
  215. mutex_lock(&mem_id_lock);
  216. ret = __mem_id_init_hash_table();
  217. mutex_unlock(&mem_id_lock);
  218. if (ret < 0) {
  219. WARN_ON(1);
  220. return ret;
  221. }
  222. }
  223. xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
  224. if (!xdp_alloc)
  225. return -ENOMEM;
  226. mutex_lock(&mem_id_lock);
  227. id = __mem_id_cyclic_get(gfp);
  228. if (id < 0) {
  229. errno = id;
  230. goto err;
  231. }
  232. xdp_rxq->mem.id = id;
  233. xdp_alloc->mem = xdp_rxq->mem;
  234. xdp_alloc->allocator = allocator;
  235. /* Insert allocator into ID lookup table */
  236. ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
  237. if (IS_ERR(ptr)) {
  238. errno = PTR_ERR(ptr);
  239. goto err;
  240. }
  241. mutex_unlock(&mem_id_lock);
  242. return 0;
  243. err:
  244. mutex_unlock(&mem_id_lock);
  245. kfree(xdp_alloc);
  246. return errno;
  247. }
  248. EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
  249. /* XDP RX runs under NAPI protection, and in different delivery error
  250. * scenarios (e.g. queue full), it is possible to return the xdp_frame
  251. * while still leveraging this protection. The @napi_direct boolian
  252. * is used for those calls sites. Thus, allowing for faster recycling
  253. * of xdp_frames/pages in those cases.
  254. */
  255. static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
  256. unsigned long handle)
  257. {
  258. struct xdp_mem_allocator *xa;
  259. struct page *page;
  260. switch (mem->type) {
  261. case MEM_TYPE_PAGE_POOL:
  262. rcu_read_lock();
  263. /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
  264. xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
  265. page = virt_to_head_page(data);
  266. if (xa) {
  267. napi_direct &= !xdp_return_frame_no_direct();
  268. page_pool_put_page(xa->page_pool, page, napi_direct);
  269. } else {
  270. put_page(page);
  271. }
  272. rcu_read_unlock();
  273. break;
  274. case MEM_TYPE_PAGE_SHARED:
  275. page_frag_free(data);
  276. break;
  277. case MEM_TYPE_PAGE_ORDER0:
  278. page = virt_to_page(data); /* Assumes order0 page*/
  279. put_page(page);
  280. break;
  281. case MEM_TYPE_ZERO_COPY:
  282. /* NB! Only valid from an xdp_buff! */
  283. rcu_read_lock();
  284. /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
  285. xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
  286. xa->zc_alloc->free(xa->zc_alloc, handle);
  287. rcu_read_unlock();
  288. default:
  289. /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
  290. break;
  291. }
  292. }
  293. void xdp_return_frame(struct xdp_frame *xdpf)
  294. {
  295. __xdp_return(xdpf->data, &xdpf->mem, false, 0);
  296. }
  297. EXPORT_SYMBOL_GPL(xdp_return_frame);
  298. void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
  299. {
  300. __xdp_return(xdpf->data, &xdpf->mem, true, 0);
  301. }
  302. EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
  303. void xdp_return_buff(struct xdp_buff *xdp)
  304. {
  305. __xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle);
  306. }
  307. EXPORT_SYMBOL_GPL(xdp_return_buff);
  308. int xdp_attachment_query(struct xdp_attachment_info *info,
  309. struct netdev_bpf *bpf)
  310. {
  311. bpf->prog_id = info->prog ? info->prog->aux->id : 0;
  312. bpf->prog_flags = info->prog ? info->flags : 0;
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(xdp_attachment_query);
  316. bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
  317. struct netdev_bpf *bpf)
  318. {
  319. if (info->prog && (bpf->flags ^ info->flags) & XDP_FLAGS_MODES) {
  320. NL_SET_ERR_MSG(bpf->extack,
  321. "program loaded with different flags");
  322. return false;
  323. }
  324. return true;
  325. }
  326. EXPORT_SYMBOL_GPL(xdp_attachment_flags_ok);
  327. void xdp_attachment_setup(struct xdp_attachment_info *info,
  328. struct netdev_bpf *bpf)
  329. {
  330. if (info->prog)
  331. bpf_prog_put(info->prog);
  332. info->prog = bpf->prog;
  333. info->flags = bpf->flags;
  334. }
  335. EXPORT_SYMBOL_GPL(xdp_attachment_setup);