xsk_queue.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* XDP user-space ring structure
  3. * Copyright(c) 2018 Intel Corporation.
  4. */
  5. #include <linux/slab.h>
  6. #include "xsk_queue.h"
  7. void xskq_set_umem(struct xsk_queue *q, struct xdp_umem_props *umem_props)
  8. {
  9. if (!q)
  10. return;
  11. q->umem_props = *umem_props;
  12. }
  13. static u32 xskq_umem_get_ring_size(struct xsk_queue *q)
  14. {
  15. return sizeof(struct xdp_umem_ring) + q->nentries * sizeof(u64);
  16. }
  17. static u32 xskq_rxtx_get_ring_size(struct xsk_queue *q)
  18. {
  19. return sizeof(struct xdp_ring) + q->nentries * sizeof(struct xdp_desc);
  20. }
  21. struct xsk_queue *xskq_create(u32 nentries, bool umem_queue)
  22. {
  23. struct xsk_queue *q;
  24. gfp_t gfp_flags;
  25. size_t size;
  26. q = kzalloc(sizeof(*q), GFP_KERNEL);
  27. if (!q)
  28. return NULL;
  29. q->nentries = nentries;
  30. q->ring_mask = nentries - 1;
  31. gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN |
  32. __GFP_COMP | __GFP_NORETRY;
  33. size = umem_queue ? xskq_umem_get_ring_size(q) :
  34. xskq_rxtx_get_ring_size(q);
  35. q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags,
  36. get_order(size));
  37. if (!q->ring) {
  38. kfree(q);
  39. return NULL;
  40. }
  41. return q;
  42. }
  43. void xskq_destroy(struct xsk_queue *q)
  44. {
  45. if (!q)
  46. return;
  47. page_frag_free(q->ring);
  48. kfree(q);
  49. }