xdp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* net/core/xdp.c
  3. *
  4. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  5. */
  6. #include <linux/bpf.h>
  7. #include <linux/btf.h>
  8. #include <linux/btf_ids.h>
  9. #include <linux/filter.h>
  10. #include <linux/types.h>
  11. #include <linux/mm.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/slab.h>
  14. #include <linux/idr.h>
  15. #include <linux/rhashtable.h>
  16. #include <linux/bug.h>
  17. #include <net/page_pool/helpers.h>
  18. #include <net/hotdata.h>
  19. #include <net/xdp.h>
  20. #include <net/xdp_priv.h> /* struct xdp_mem_allocator */
  21. #include <trace/events/xdp.h>
  22. #include <net/xdp_sock_drv.h>
  23. #define REG_STATE_NEW 0x0
  24. #define REG_STATE_REGISTERED 0x1
  25. #define REG_STATE_UNREGISTERED 0x2
  26. #define REG_STATE_UNUSED 0x3
  27. static DEFINE_IDA(mem_id_pool);
  28. static DEFINE_MUTEX(mem_id_lock);
  29. #define MEM_ID_MAX 0xFFFE
  30. #define MEM_ID_MIN 1
  31. static int mem_id_next = MEM_ID_MIN;
  32. static bool mem_id_init; /* false */
  33. static struct rhashtable *mem_id_ht;
  34. static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed)
  35. {
  36. const u32 *k = data;
  37. const u32 key = *k;
  38. BUILD_BUG_ON(sizeof_field(struct xdp_mem_allocator, mem.id)
  39. != sizeof(u32));
  40. /* Use cyclic increasing ID as direct hash key */
  41. return key;
  42. }
  43. static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg,
  44. const void *ptr)
  45. {
  46. const struct xdp_mem_allocator *xa = ptr;
  47. u32 mem_id = *(u32 *)arg->key;
  48. return xa->mem.id != mem_id;
  49. }
  50. static const struct rhashtable_params mem_id_rht_params = {
  51. .nelem_hint = 64,
  52. .head_offset = offsetof(struct xdp_mem_allocator, node),
  53. .key_offset = offsetof(struct xdp_mem_allocator, mem.id),
  54. .key_len = sizeof_field(struct xdp_mem_allocator, mem.id),
  55. .max_size = MEM_ID_MAX,
  56. .min_size = 8,
  57. .automatic_shrinking = true,
  58. .hashfn = xdp_mem_id_hashfn,
  59. .obj_cmpfn = xdp_mem_id_cmp,
  60. };
  61. static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)
  62. {
  63. struct xdp_mem_allocator *xa;
  64. xa = container_of(rcu, struct xdp_mem_allocator, rcu);
  65. /* Allow this ID to be reused */
  66. ida_free(&mem_id_pool, xa->mem.id);
  67. kfree(xa);
  68. }
  69. static void mem_xa_remove(struct xdp_mem_allocator *xa)
  70. {
  71. trace_mem_disconnect(xa);
  72. if (!rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params))
  73. call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
  74. }
  75. static void mem_allocator_disconnect(void *allocator)
  76. {
  77. struct xdp_mem_allocator *xa;
  78. struct rhashtable_iter iter;
  79. mutex_lock(&mem_id_lock);
  80. rhashtable_walk_enter(mem_id_ht, &iter);
  81. do {
  82. rhashtable_walk_start(&iter);
  83. while ((xa = rhashtable_walk_next(&iter)) && !IS_ERR(xa)) {
  84. if (xa->allocator == allocator)
  85. mem_xa_remove(xa);
  86. }
  87. rhashtable_walk_stop(&iter);
  88. } while (xa == ERR_PTR(-EAGAIN));
  89. rhashtable_walk_exit(&iter);
  90. mutex_unlock(&mem_id_lock);
  91. }
  92. void xdp_unreg_mem_model(struct xdp_mem_info *mem)
  93. {
  94. struct xdp_mem_allocator *xa;
  95. int type = mem->type;
  96. int id = mem->id;
  97. /* Reset mem info to defaults */
  98. mem->id = 0;
  99. mem->type = 0;
  100. if (id == 0)
  101. return;
  102. if (type == MEM_TYPE_PAGE_POOL) {
  103. xa = rhashtable_lookup_fast(mem_id_ht, &id, mem_id_rht_params);
  104. page_pool_destroy(xa->page_pool);
  105. }
  106. }
  107. EXPORT_SYMBOL_GPL(xdp_unreg_mem_model);
  108. void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
  109. {
  110. if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
  111. WARN(1, "Missing register, driver bug");
  112. return;
  113. }
  114. xdp_unreg_mem_model(&xdp_rxq->mem);
  115. }
  116. EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
  117. void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
  118. {
  119. /* Simplify driver cleanup code paths, allow unreg "unused" */
  120. if (xdp_rxq->reg_state == REG_STATE_UNUSED)
  121. return;
  122. xdp_rxq_info_unreg_mem_model(xdp_rxq);
  123. xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
  124. xdp_rxq->dev = NULL;
  125. }
  126. EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
  127. static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
  128. {
  129. memset(xdp_rxq, 0, sizeof(*xdp_rxq));
  130. }
  131. /* Returns 0 on success, negative on failure */
  132. int __xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  133. struct net_device *dev, u32 queue_index,
  134. unsigned int napi_id, u32 frag_size)
  135. {
  136. if (!dev) {
  137. WARN(1, "Missing net_device from driver");
  138. return -ENODEV;
  139. }
  140. if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
  141. WARN(1, "Driver promised not to register this");
  142. return -EINVAL;
  143. }
  144. if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
  145. WARN(1, "Missing unregister, handled but fix driver");
  146. xdp_rxq_info_unreg(xdp_rxq);
  147. }
  148. /* State either UNREGISTERED or NEW */
  149. xdp_rxq_info_init(xdp_rxq);
  150. xdp_rxq->dev = dev;
  151. xdp_rxq->queue_index = queue_index;
  152. xdp_rxq->napi_id = napi_id;
  153. xdp_rxq->frag_size = frag_size;
  154. xdp_rxq->reg_state = REG_STATE_REGISTERED;
  155. return 0;
  156. }
  157. EXPORT_SYMBOL_GPL(__xdp_rxq_info_reg);
  158. void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
  159. {
  160. xdp_rxq->reg_state = REG_STATE_UNUSED;
  161. }
  162. EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);
  163. bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq)
  164. {
  165. return (xdp_rxq->reg_state == REG_STATE_REGISTERED);
  166. }
  167. EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
  168. static int __mem_id_init_hash_table(void)
  169. {
  170. struct rhashtable *rht;
  171. int ret;
  172. if (unlikely(mem_id_init))
  173. return 0;
  174. rht = kzalloc(sizeof(*rht), GFP_KERNEL);
  175. if (!rht)
  176. return -ENOMEM;
  177. ret = rhashtable_init(rht, &mem_id_rht_params);
  178. if (ret < 0) {
  179. kfree(rht);
  180. return ret;
  181. }
  182. mem_id_ht = rht;
  183. smp_mb(); /* mutex lock should provide enough pairing */
  184. mem_id_init = true;
  185. return 0;
  186. }
  187. /* Allocate a cyclic ID that maps to allocator pointer.
  188. * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
  189. *
  190. * Caller must lock mem_id_lock.
  191. */
  192. static int __mem_id_cyclic_get(gfp_t gfp)
  193. {
  194. int retries = 1;
  195. int id;
  196. again:
  197. id = ida_alloc_range(&mem_id_pool, mem_id_next, MEM_ID_MAX - 1, gfp);
  198. if (id < 0) {
  199. if (id == -ENOSPC) {
  200. /* Cyclic allocator, reset next id */
  201. if (retries--) {
  202. mem_id_next = MEM_ID_MIN;
  203. goto again;
  204. }
  205. }
  206. return id; /* errno */
  207. }
  208. mem_id_next = id + 1;
  209. return id;
  210. }
  211. static bool __is_supported_mem_type(enum xdp_mem_type type)
  212. {
  213. if (type == MEM_TYPE_PAGE_POOL)
  214. return is_page_pool_compiled_in();
  215. if (type >= MEM_TYPE_MAX)
  216. return false;
  217. return true;
  218. }
  219. static struct xdp_mem_allocator *__xdp_reg_mem_model(struct xdp_mem_info *mem,
  220. enum xdp_mem_type type,
  221. void *allocator)
  222. {
  223. struct xdp_mem_allocator *xdp_alloc;
  224. gfp_t gfp = GFP_KERNEL;
  225. int id, errno, ret;
  226. void *ptr;
  227. if (!__is_supported_mem_type(type))
  228. return ERR_PTR(-EOPNOTSUPP);
  229. mem->type = type;
  230. if (!allocator) {
  231. if (type == MEM_TYPE_PAGE_POOL)
  232. return ERR_PTR(-EINVAL); /* Setup time check page_pool req */
  233. return NULL;
  234. }
  235. /* Delay init of rhashtable to save memory if feature isn't used */
  236. if (!mem_id_init) {
  237. mutex_lock(&mem_id_lock);
  238. ret = __mem_id_init_hash_table();
  239. mutex_unlock(&mem_id_lock);
  240. if (ret < 0)
  241. return ERR_PTR(ret);
  242. }
  243. xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
  244. if (!xdp_alloc)
  245. return ERR_PTR(-ENOMEM);
  246. mutex_lock(&mem_id_lock);
  247. id = __mem_id_cyclic_get(gfp);
  248. if (id < 0) {
  249. errno = id;
  250. goto err;
  251. }
  252. mem->id = id;
  253. xdp_alloc->mem = *mem;
  254. xdp_alloc->allocator = allocator;
  255. /* Insert allocator into ID lookup table */
  256. ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
  257. if (IS_ERR(ptr)) {
  258. ida_free(&mem_id_pool, mem->id);
  259. mem->id = 0;
  260. errno = PTR_ERR(ptr);
  261. goto err;
  262. }
  263. if (type == MEM_TYPE_PAGE_POOL)
  264. page_pool_use_xdp_mem(allocator, mem_allocator_disconnect, mem);
  265. mutex_unlock(&mem_id_lock);
  266. return xdp_alloc;
  267. err:
  268. mutex_unlock(&mem_id_lock);
  269. kfree(xdp_alloc);
  270. return ERR_PTR(errno);
  271. }
  272. int xdp_reg_mem_model(struct xdp_mem_info *mem,
  273. enum xdp_mem_type type, void *allocator)
  274. {
  275. struct xdp_mem_allocator *xdp_alloc;
  276. xdp_alloc = __xdp_reg_mem_model(mem, type, allocator);
  277. if (IS_ERR(xdp_alloc))
  278. return PTR_ERR(xdp_alloc);
  279. return 0;
  280. }
  281. EXPORT_SYMBOL_GPL(xdp_reg_mem_model);
  282. int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
  283. enum xdp_mem_type type, void *allocator)
  284. {
  285. struct xdp_mem_allocator *xdp_alloc;
  286. if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
  287. WARN(1, "Missing register, driver bug");
  288. return -EFAULT;
  289. }
  290. xdp_alloc = __xdp_reg_mem_model(&xdp_rxq->mem, type, allocator);
  291. if (IS_ERR(xdp_alloc))
  292. return PTR_ERR(xdp_alloc);
  293. if (trace_mem_connect_enabled() && xdp_alloc)
  294. trace_mem_connect(xdp_alloc, xdp_rxq);
  295. return 0;
  296. }
  297. EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
  298. /* XDP RX runs under NAPI protection, and in different delivery error
  299. * scenarios (e.g. queue full), it is possible to return the xdp_frame
  300. * while still leveraging this protection. The @napi_direct boolean
  301. * is used for those calls sites. Thus, allowing for faster recycling
  302. * of xdp_frames/pages in those cases.
  303. */
  304. void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
  305. struct xdp_buff *xdp)
  306. {
  307. struct page *page;
  308. switch (mem->type) {
  309. case MEM_TYPE_PAGE_POOL:
  310. page = virt_to_head_page(data);
  311. if (napi_direct && xdp_return_frame_no_direct())
  312. napi_direct = false;
  313. /* No need to check netmem_is_pp() as mem->type knows this a
  314. * page_pool page
  315. */
  316. page_pool_put_full_page(page->pp, page, napi_direct);
  317. break;
  318. case MEM_TYPE_PAGE_SHARED:
  319. page_frag_free(data);
  320. break;
  321. case MEM_TYPE_PAGE_ORDER0:
  322. page = virt_to_page(data); /* Assumes order0 page*/
  323. put_page(page);
  324. break;
  325. case MEM_TYPE_XSK_BUFF_POOL:
  326. /* NB! Only valid from an xdp_buff! */
  327. xsk_buff_free(xdp);
  328. break;
  329. default:
  330. /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
  331. WARN(1, "Incorrect XDP memory type (%d) usage", mem->type);
  332. break;
  333. }
  334. }
  335. void xdp_return_frame(struct xdp_frame *xdpf)
  336. {
  337. struct skb_shared_info *sinfo;
  338. int i;
  339. if (likely(!xdp_frame_has_frags(xdpf)))
  340. goto out;
  341. sinfo = xdp_get_shared_info_from_frame(xdpf);
  342. for (i = 0; i < sinfo->nr_frags; i++) {
  343. struct page *page = skb_frag_page(&sinfo->frags[i]);
  344. __xdp_return(page_address(page), &xdpf->mem, false, NULL);
  345. }
  346. out:
  347. __xdp_return(xdpf->data, &xdpf->mem, false, NULL);
  348. }
  349. EXPORT_SYMBOL_GPL(xdp_return_frame);
  350. void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
  351. {
  352. struct skb_shared_info *sinfo;
  353. int i;
  354. if (likely(!xdp_frame_has_frags(xdpf)))
  355. goto out;
  356. sinfo = xdp_get_shared_info_from_frame(xdpf);
  357. for (i = 0; i < sinfo->nr_frags; i++) {
  358. struct page *page = skb_frag_page(&sinfo->frags[i]);
  359. __xdp_return(page_address(page), &xdpf->mem, true, NULL);
  360. }
  361. out:
  362. __xdp_return(xdpf->data, &xdpf->mem, true, NULL);
  363. }
  364. EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
  365. /* XDP bulk APIs introduce a defer/flush mechanism to return
  366. * pages belonging to the same xdp_mem_allocator object
  367. * (identified via the mem.id field) in bulk to optimize
  368. * I-cache and D-cache.
  369. * The bulk queue size is set to 16 to be aligned to how
  370. * XDP_REDIRECT bulking works. The bulk is flushed when
  371. * it is full or when mem.id changes.
  372. * xdp_frame_bulk is usually stored/allocated on the function
  373. * call-stack to avoid locking penalties.
  374. */
  375. void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq)
  376. {
  377. struct xdp_mem_allocator *xa = bq->xa;
  378. if (unlikely(!xa || !bq->count))
  379. return;
  380. page_pool_put_page_bulk(xa->page_pool, bq->q, bq->count);
  381. /* bq->xa is not cleared to save lookup, if mem.id same in next bulk */
  382. bq->count = 0;
  383. }
  384. EXPORT_SYMBOL_GPL(xdp_flush_frame_bulk);
  385. /* Must be called with rcu_read_lock held */
  386. void xdp_return_frame_bulk(struct xdp_frame *xdpf,
  387. struct xdp_frame_bulk *bq)
  388. {
  389. struct xdp_mem_info *mem = &xdpf->mem;
  390. struct xdp_mem_allocator *xa;
  391. if (mem->type != MEM_TYPE_PAGE_POOL) {
  392. xdp_return_frame(xdpf);
  393. return;
  394. }
  395. xa = bq->xa;
  396. if (unlikely(!xa)) {
  397. xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
  398. bq->count = 0;
  399. bq->xa = xa;
  400. }
  401. if (bq->count == XDP_BULK_QUEUE_SIZE)
  402. xdp_flush_frame_bulk(bq);
  403. if (unlikely(mem->id != xa->mem.id)) {
  404. xdp_flush_frame_bulk(bq);
  405. bq->xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
  406. }
  407. if (unlikely(xdp_frame_has_frags(xdpf))) {
  408. struct skb_shared_info *sinfo;
  409. int i;
  410. sinfo = xdp_get_shared_info_from_frame(xdpf);
  411. for (i = 0; i < sinfo->nr_frags; i++) {
  412. skb_frag_t *frag = &sinfo->frags[i];
  413. bq->q[bq->count++] = skb_frag_address(frag);
  414. if (bq->count == XDP_BULK_QUEUE_SIZE)
  415. xdp_flush_frame_bulk(bq);
  416. }
  417. }
  418. bq->q[bq->count++] = xdpf->data;
  419. }
  420. EXPORT_SYMBOL_GPL(xdp_return_frame_bulk);
  421. void xdp_return_buff(struct xdp_buff *xdp)
  422. {
  423. struct skb_shared_info *sinfo;
  424. int i;
  425. if (likely(!xdp_buff_has_frags(xdp)))
  426. goto out;
  427. sinfo = xdp_get_shared_info_from_buff(xdp);
  428. for (i = 0; i < sinfo->nr_frags; i++) {
  429. struct page *page = skb_frag_page(&sinfo->frags[i]);
  430. __xdp_return(page_address(page), &xdp->rxq->mem, true, xdp);
  431. }
  432. out:
  433. __xdp_return(xdp->data, &xdp->rxq->mem, true, xdp);
  434. }
  435. EXPORT_SYMBOL_GPL(xdp_return_buff);
  436. void xdp_attachment_setup(struct xdp_attachment_info *info,
  437. struct netdev_bpf *bpf)
  438. {
  439. if (info->prog)
  440. bpf_prog_put(info->prog);
  441. info->prog = bpf->prog;
  442. info->flags = bpf->flags;
  443. }
  444. EXPORT_SYMBOL_GPL(xdp_attachment_setup);
  445. struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp)
  446. {
  447. unsigned int metasize, totsize;
  448. void *addr, *data_to_copy;
  449. struct xdp_frame *xdpf;
  450. struct page *page;
  451. /* Clone into a MEM_TYPE_PAGE_ORDER0 xdp_frame. */
  452. metasize = xdp_data_meta_unsupported(xdp) ? 0 :
  453. xdp->data - xdp->data_meta;
  454. totsize = xdp->data_end - xdp->data + metasize;
  455. if (sizeof(*xdpf) + totsize > PAGE_SIZE)
  456. return NULL;
  457. page = dev_alloc_page();
  458. if (!page)
  459. return NULL;
  460. addr = page_to_virt(page);
  461. xdpf = addr;
  462. memset(xdpf, 0, sizeof(*xdpf));
  463. addr += sizeof(*xdpf);
  464. data_to_copy = metasize ? xdp->data_meta : xdp->data;
  465. memcpy(addr, data_to_copy, totsize);
  466. xdpf->data = addr + metasize;
  467. xdpf->len = totsize - metasize;
  468. xdpf->headroom = 0;
  469. xdpf->metasize = metasize;
  470. xdpf->frame_sz = PAGE_SIZE;
  471. xdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
  472. xsk_buff_free(xdp);
  473. return xdpf;
  474. }
  475. EXPORT_SYMBOL_GPL(xdp_convert_zc_to_xdp_frame);
  476. /* Used by XDP_WARN macro, to avoid inlining WARN() in fast-path */
  477. void xdp_warn(const char *msg, const char *func, const int line)
  478. {
  479. WARN(1, "XDP_WARN: %s(line:%d): %s\n", func, line, msg);
  480. };
  481. EXPORT_SYMBOL_GPL(xdp_warn);
  482. int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp)
  483. {
  484. n_skb = kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, gfp, n_skb, skbs);
  485. if (unlikely(!n_skb))
  486. return -ENOMEM;
  487. return 0;
  488. }
  489. EXPORT_SYMBOL_GPL(xdp_alloc_skb_bulk);
  490. struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
  491. struct sk_buff *skb,
  492. struct net_device *dev)
  493. {
  494. struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf);
  495. unsigned int headroom, frame_size;
  496. void *hard_start;
  497. u8 nr_frags;
  498. /* xdp frags frame */
  499. if (unlikely(xdp_frame_has_frags(xdpf)))
  500. nr_frags = sinfo->nr_frags;
  501. /* Part of headroom was reserved to xdpf */
  502. headroom = sizeof(*xdpf) + xdpf->headroom;
  503. /* Memory size backing xdp_frame data already have reserved
  504. * room for build_skb to place skb_shared_info in tailroom.
  505. */
  506. frame_size = xdpf->frame_sz;
  507. hard_start = xdpf->data - headroom;
  508. skb = build_skb_around(skb, hard_start, frame_size);
  509. if (unlikely(!skb))
  510. return NULL;
  511. skb_reserve(skb, headroom);
  512. __skb_put(skb, xdpf->len);
  513. if (xdpf->metasize)
  514. skb_metadata_set(skb, xdpf->metasize);
  515. if (unlikely(xdp_frame_has_frags(xdpf)))
  516. xdp_update_skb_shared_info(skb, nr_frags,
  517. sinfo->xdp_frags_size,
  518. nr_frags * xdpf->frame_sz,
  519. xdp_frame_is_frag_pfmemalloc(xdpf));
  520. /* Essential SKB info: protocol and skb->dev */
  521. skb->protocol = eth_type_trans(skb, dev);
  522. /* Optional SKB info, currently missing:
  523. * - HW checksum info (skb->ip_summed)
  524. * - HW RX hash (skb_set_hash)
  525. * - RX ring dev queue index (skb_record_rx_queue)
  526. */
  527. if (xdpf->mem.type == MEM_TYPE_PAGE_POOL)
  528. skb_mark_for_recycle(skb);
  529. /* Allow SKB to reuse area used by xdp_frame */
  530. xdp_scrub_frame(xdpf);
  531. return skb;
  532. }
  533. EXPORT_SYMBOL_GPL(__xdp_build_skb_from_frame);
  534. struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
  535. struct net_device *dev)
  536. {
  537. struct sk_buff *skb;
  538. skb = kmem_cache_alloc(net_hotdata.skbuff_cache, GFP_ATOMIC);
  539. if (unlikely(!skb))
  540. return NULL;
  541. memset(skb, 0, offsetof(struct sk_buff, tail));
  542. return __xdp_build_skb_from_frame(xdpf, skb, dev);
  543. }
  544. EXPORT_SYMBOL_GPL(xdp_build_skb_from_frame);
  545. struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf)
  546. {
  547. unsigned int headroom, totalsize;
  548. struct xdp_frame *nxdpf;
  549. struct page *page;
  550. void *addr;
  551. headroom = xdpf->headroom + sizeof(*xdpf);
  552. totalsize = headroom + xdpf->len;
  553. if (unlikely(totalsize > PAGE_SIZE))
  554. return NULL;
  555. page = dev_alloc_page();
  556. if (!page)
  557. return NULL;
  558. addr = page_to_virt(page);
  559. memcpy(addr, xdpf, totalsize);
  560. nxdpf = addr;
  561. nxdpf->data = addr + headroom;
  562. nxdpf->frame_sz = PAGE_SIZE;
  563. nxdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
  564. nxdpf->mem.id = 0;
  565. return nxdpf;
  566. }
  567. __bpf_kfunc_start_defs();
  568. /**
  569. * bpf_xdp_metadata_rx_timestamp - Read XDP frame RX timestamp.
  570. * @ctx: XDP context pointer.
  571. * @timestamp: Return value pointer.
  572. *
  573. * Return:
  574. * * Returns 0 on success or ``-errno`` on error.
  575. * * ``-EOPNOTSUPP`` : means device driver does not implement kfunc
  576. * * ``-ENODATA`` : means no RX-timestamp available for this frame
  577. */
  578. __bpf_kfunc int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
  579. {
  580. return -EOPNOTSUPP;
  581. }
  582. /**
  583. * bpf_xdp_metadata_rx_hash - Read XDP frame RX hash.
  584. * @ctx: XDP context pointer.
  585. * @hash: Return value pointer.
  586. * @rss_type: Return value pointer for RSS type.
  587. *
  588. * The RSS hash type (@rss_type) specifies what portion of packet headers NIC
  589. * hardware used when calculating RSS hash value. The RSS type can be decoded
  590. * via &enum xdp_rss_hash_type either matching on individual L3/L4 bits
  591. * ``XDP_RSS_L*`` or by combined traditional *RSS Hashing Types*
  592. * ``XDP_RSS_TYPE_L*``.
  593. *
  594. * Return:
  595. * * Returns 0 on success or ``-errno`` on error.
  596. * * ``-EOPNOTSUPP`` : means device driver doesn't implement kfunc
  597. * * ``-ENODATA`` : means no RX-hash available for this frame
  598. */
  599. __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,
  600. enum xdp_rss_hash_type *rss_type)
  601. {
  602. return -EOPNOTSUPP;
  603. }
  604. /**
  605. * bpf_xdp_metadata_rx_vlan_tag - Get XDP packet outermost VLAN tag
  606. * @ctx: XDP context pointer.
  607. * @vlan_proto: Destination pointer for VLAN Tag protocol identifier (TPID).
  608. * @vlan_tci: Destination pointer for VLAN TCI (VID + DEI + PCP)
  609. *
  610. * In case of success, ``vlan_proto`` contains *Tag protocol identifier (TPID)*,
  611. * usually ``ETH_P_8021Q`` or ``ETH_P_8021AD``, but some networks can use
  612. * custom TPIDs. ``vlan_proto`` is stored in **network byte order (BE)**
  613. * and should be used as follows:
  614. * ``if (vlan_proto == bpf_htons(ETH_P_8021Q)) do_something();``
  615. *
  616. * ``vlan_tci`` contains the remaining 16 bits of a VLAN tag.
  617. * Driver is expected to provide those in **host byte order (usually LE)**,
  618. * so the bpf program should not perform byte conversion.
  619. * According to 802.1Q standard, *VLAN TCI (Tag control information)*
  620. * is a bit field that contains:
  621. * *VLAN identifier (VID)* that can be read with ``vlan_tci & 0xfff``,
  622. * *Drop eligible indicator (DEI)* - 1 bit,
  623. * *Priority code point (PCP)* - 3 bits.
  624. * For detailed meaning of DEI and PCP, please refer to other sources.
  625. *
  626. * Return:
  627. * * Returns 0 on success or ``-errno`` on error.
  628. * * ``-EOPNOTSUPP`` : device driver doesn't implement kfunc
  629. * * ``-ENODATA`` : VLAN tag was not stripped or is not available
  630. */
  631. __bpf_kfunc int bpf_xdp_metadata_rx_vlan_tag(const struct xdp_md *ctx,
  632. __be16 *vlan_proto, u16 *vlan_tci)
  633. {
  634. return -EOPNOTSUPP;
  635. }
  636. __bpf_kfunc_end_defs();
  637. BTF_KFUNCS_START(xdp_metadata_kfunc_ids)
  638. #define XDP_METADATA_KFUNC(_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
  639. XDP_METADATA_KFUNC_xxx
  640. #undef XDP_METADATA_KFUNC
  641. BTF_KFUNCS_END(xdp_metadata_kfunc_ids)
  642. static const struct btf_kfunc_id_set xdp_metadata_kfunc_set = {
  643. .owner = THIS_MODULE,
  644. .set = &xdp_metadata_kfunc_ids,
  645. };
  646. BTF_ID_LIST(xdp_metadata_kfunc_ids_unsorted)
  647. #define XDP_METADATA_KFUNC(name, _, str, __) BTF_ID(func, str)
  648. XDP_METADATA_KFUNC_xxx
  649. #undef XDP_METADATA_KFUNC
  650. u32 bpf_xdp_metadata_kfunc_id(int id)
  651. {
  652. /* xdp_metadata_kfunc_ids is sorted and can't be used */
  653. return xdp_metadata_kfunc_ids_unsorted[id];
  654. }
  655. bool bpf_dev_bound_kfunc_id(u32 btf_id)
  656. {
  657. return btf_id_set8_contains(&xdp_metadata_kfunc_ids, btf_id);
  658. }
  659. static int __init xdp_metadata_init(void)
  660. {
  661. return register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_metadata_kfunc_set);
  662. }
  663. late_initcall(xdp_metadata_init);
  664. void xdp_set_features_flag(struct net_device *dev, xdp_features_t val)
  665. {
  666. val &= NETDEV_XDP_ACT_MASK;
  667. if (dev->xdp_features == val)
  668. return;
  669. dev->xdp_features = val;
  670. if (dev->reg_state == NETREG_REGISTERED)
  671. call_netdevice_notifiers(NETDEV_XDP_FEAT_CHANGE, dev);
  672. }
  673. EXPORT_SYMBOL_GPL(xdp_set_features_flag);
  674. void xdp_features_set_redirect_target(struct net_device *dev, bool support_sg)
  675. {
  676. xdp_features_t val = (dev->xdp_features | NETDEV_XDP_ACT_NDO_XMIT);
  677. if (support_sg)
  678. val |= NETDEV_XDP_ACT_NDO_XMIT_SG;
  679. xdp_set_features_flag(dev, val);
  680. }
  681. EXPORT_SYMBOL_GPL(xdp_features_set_redirect_target);
  682. void xdp_features_clear_redirect_target(struct net_device *dev)
  683. {
  684. xdp_features_t val = dev->xdp_features;
  685. val &= ~(NETDEV_XDP_ACT_NDO_XMIT | NETDEV_XDP_ACT_NDO_XMIT_SG);
  686. xdp_set_features_flag(dev, val);
  687. }
  688. EXPORT_SYMBOL_GPL(xdp_features_clear_redirect_target);