xdp.c 9.3 KB

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