skb_array.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Definitions for the 'struct skb_array' datastructure.
  4. *
  5. * Author:
  6. * Michael S. Tsirkin <mst@redhat.com>
  7. *
  8. * Copyright (C) 2016 Red Hat, Inc.
  9. *
  10. * Limited-size FIFO of skbs. Can be used more or less whenever
  11. * sk_buff_head can be used, except you need to know the queue size in
  12. * advance.
  13. * Implemented as a type-safe wrapper around ptr_ring.
  14. */
  15. #ifndef _LINUX_SKB_ARRAY_H
  16. #define _LINUX_SKB_ARRAY_H 1
  17. #ifdef __KERNEL__
  18. #include <linux/ptr_ring.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/if_vlan.h>
  21. #endif
  22. struct skb_array {
  23. struct ptr_ring ring;
  24. };
  25. /* Might be slightly faster than skb_array_full below, but callers invoking
  26. * this in a loop must use a compiler barrier, for example cpu_relax().
  27. */
  28. static inline bool __skb_array_full(struct skb_array *a)
  29. {
  30. return __ptr_ring_full(&a->ring);
  31. }
  32. static inline bool skb_array_full(struct skb_array *a)
  33. {
  34. return ptr_ring_full(&a->ring);
  35. }
  36. static inline int skb_array_produce(struct skb_array *a, struct sk_buff *skb)
  37. {
  38. return ptr_ring_produce(&a->ring, skb);
  39. }
  40. static inline int skb_array_produce_irq(struct skb_array *a, struct sk_buff *skb)
  41. {
  42. return ptr_ring_produce_irq(&a->ring, skb);
  43. }
  44. static inline int skb_array_produce_bh(struct skb_array *a, struct sk_buff *skb)
  45. {
  46. return ptr_ring_produce_bh(&a->ring, skb);
  47. }
  48. static inline int skb_array_produce_any(struct skb_array *a, struct sk_buff *skb)
  49. {
  50. return ptr_ring_produce_any(&a->ring, skb);
  51. }
  52. /* Might be slightly faster than skb_array_empty below, but only safe if the
  53. * array is never resized. Also, callers invoking this in a loop must take care
  54. * to use a compiler barrier, for example cpu_relax().
  55. */
  56. static inline bool __skb_array_empty(struct skb_array *a)
  57. {
  58. return __ptr_ring_empty(&a->ring);
  59. }
  60. static inline struct sk_buff *__skb_array_peek(struct skb_array *a)
  61. {
  62. return __ptr_ring_peek(&a->ring);
  63. }
  64. static inline bool skb_array_empty(struct skb_array *a)
  65. {
  66. return ptr_ring_empty(&a->ring);
  67. }
  68. static inline bool skb_array_empty_bh(struct skb_array *a)
  69. {
  70. return ptr_ring_empty_bh(&a->ring);
  71. }
  72. static inline bool skb_array_empty_irq(struct skb_array *a)
  73. {
  74. return ptr_ring_empty_irq(&a->ring);
  75. }
  76. static inline bool skb_array_empty_any(struct skb_array *a)
  77. {
  78. return ptr_ring_empty_any(&a->ring);
  79. }
  80. static inline struct sk_buff *__skb_array_consume(struct skb_array *a)
  81. {
  82. return __ptr_ring_consume(&a->ring);
  83. }
  84. static inline struct sk_buff *skb_array_consume(struct skb_array *a)
  85. {
  86. return ptr_ring_consume(&a->ring);
  87. }
  88. static inline int skb_array_consume_batched(struct skb_array *a,
  89. struct sk_buff **array, int n)
  90. {
  91. return ptr_ring_consume_batched(&a->ring, (void **)array, n);
  92. }
  93. static inline struct sk_buff *skb_array_consume_irq(struct skb_array *a)
  94. {
  95. return ptr_ring_consume_irq(&a->ring);
  96. }
  97. static inline int skb_array_consume_batched_irq(struct skb_array *a,
  98. struct sk_buff **array, int n)
  99. {
  100. return ptr_ring_consume_batched_irq(&a->ring, (void **)array, n);
  101. }
  102. static inline struct sk_buff *skb_array_consume_any(struct skb_array *a)
  103. {
  104. return ptr_ring_consume_any(&a->ring);
  105. }
  106. static inline int skb_array_consume_batched_any(struct skb_array *a,
  107. struct sk_buff **array, int n)
  108. {
  109. return ptr_ring_consume_batched_any(&a->ring, (void **)array, n);
  110. }
  111. static inline struct sk_buff *skb_array_consume_bh(struct skb_array *a)
  112. {
  113. return ptr_ring_consume_bh(&a->ring);
  114. }
  115. static inline int skb_array_consume_batched_bh(struct skb_array *a,
  116. struct sk_buff **array, int n)
  117. {
  118. return ptr_ring_consume_batched_bh(&a->ring, (void **)array, n);
  119. }
  120. static inline int __skb_array_len_with_tag(struct sk_buff *skb)
  121. {
  122. if (likely(skb)) {
  123. int len = skb->len;
  124. if (skb_vlan_tag_present(skb))
  125. len += VLAN_HLEN;
  126. return len;
  127. } else {
  128. return 0;
  129. }
  130. }
  131. static inline int skb_array_peek_len(struct skb_array *a)
  132. {
  133. return PTR_RING_PEEK_CALL(&a->ring, __skb_array_len_with_tag);
  134. }
  135. static inline int skb_array_peek_len_irq(struct skb_array *a)
  136. {
  137. return PTR_RING_PEEK_CALL_IRQ(&a->ring, __skb_array_len_with_tag);
  138. }
  139. static inline int skb_array_peek_len_bh(struct skb_array *a)
  140. {
  141. return PTR_RING_PEEK_CALL_BH(&a->ring, __skb_array_len_with_tag);
  142. }
  143. static inline int skb_array_peek_len_any(struct skb_array *a)
  144. {
  145. return PTR_RING_PEEK_CALL_ANY(&a->ring, __skb_array_len_with_tag);
  146. }
  147. static inline int skb_array_init(struct skb_array *a, int size, gfp_t gfp)
  148. {
  149. return ptr_ring_init(&a->ring, size, gfp);
  150. }
  151. static void __skb_array_destroy_skb(void *ptr)
  152. {
  153. kfree_skb(ptr);
  154. }
  155. static inline void skb_array_unconsume(struct skb_array *a,
  156. struct sk_buff **skbs, int n)
  157. {
  158. ptr_ring_unconsume(&a->ring, (void **)skbs, n, __skb_array_destroy_skb);
  159. }
  160. static inline int skb_array_resize(struct skb_array *a, int size, gfp_t gfp)
  161. {
  162. return ptr_ring_resize(&a->ring, size, gfp, __skb_array_destroy_skb);
  163. }
  164. static inline int skb_array_resize_multiple(struct skb_array **rings,
  165. int nrings, unsigned int size,
  166. gfp_t gfp)
  167. {
  168. BUILD_BUG_ON(offsetof(struct skb_array, ring));
  169. return ptr_ring_resize_multiple((struct ptr_ring **)rings,
  170. nrings, size, gfp,
  171. __skb_array_destroy_skb);
  172. }
  173. static inline void skb_array_cleanup(struct skb_array *a)
  174. {
  175. ptr_ring_cleanup(&a->ring, __skb_array_destroy_skb);
  176. }
  177. #endif /* _LINUX_SKB_ARRAY_H */