skb_array.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Definitions for the 'struct skb_array' datastructure.
  3. *
  4. * Author:
  5. * Michael S. Tsirkin <mst@redhat.com>
  6. *
  7. * Copyright (C) 2016 Red Hat, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * Limited-size FIFO of skbs. Can be used more or less whenever
  15. * sk_buff_head can be used, except you need to know the queue size in
  16. * advance.
  17. * Implemented as a type-safe wrapper around ptr_ring.
  18. */
  19. #ifndef _LINUX_SKB_ARRAY_H
  20. #define _LINUX_SKB_ARRAY_H 1
  21. #ifdef __KERNEL__
  22. #include <linux/ptr_ring.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/if_vlan.h>
  25. #endif
  26. struct skb_array {
  27. struct ptr_ring ring;
  28. };
  29. /* Might be slightly faster than skb_array_full below, but callers invoking
  30. * this in a loop must use a compiler barrier, for example cpu_relax().
  31. */
  32. static inline bool __skb_array_full(struct skb_array *a)
  33. {
  34. return __ptr_ring_full(&a->ring);
  35. }
  36. static inline bool skb_array_full(struct skb_array *a)
  37. {
  38. return ptr_ring_full(&a->ring);
  39. }
  40. static inline int skb_array_produce(struct skb_array *a, struct sk_buff *skb)
  41. {
  42. return ptr_ring_produce(&a->ring, skb);
  43. }
  44. static inline int skb_array_produce_irq(struct skb_array *a, struct sk_buff *skb)
  45. {
  46. return ptr_ring_produce_irq(&a->ring, skb);
  47. }
  48. static inline int skb_array_produce_bh(struct skb_array *a, struct sk_buff *skb)
  49. {
  50. return ptr_ring_produce_bh(&a->ring, skb);
  51. }
  52. static inline int skb_array_produce_any(struct skb_array *a, struct sk_buff *skb)
  53. {
  54. return ptr_ring_produce_any(&a->ring, skb);
  55. }
  56. /* Might be slightly faster than skb_array_empty below, but only safe if the
  57. * array is never resized. Also, callers invoking this in a loop must take care
  58. * to use a compiler barrier, for example cpu_relax().
  59. */
  60. static inline bool __skb_array_empty(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_irq(struct skb_array *a)
  85. {
  86. return ptr_ring_consume_irq(&a->ring);
  87. }
  88. static inline struct sk_buff *skb_array_consume_any(struct skb_array *a)
  89. {
  90. return ptr_ring_consume_any(&a->ring);
  91. }
  92. static inline struct sk_buff *skb_array_consume_bh(struct skb_array *a)
  93. {
  94. return ptr_ring_consume_bh(&a->ring);
  95. }
  96. static inline int __skb_array_len_with_tag(struct sk_buff *skb)
  97. {
  98. if (likely(skb)) {
  99. int len = skb->len;
  100. if (skb_vlan_tag_present(skb))
  101. len += VLAN_HLEN;
  102. return len;
  103. } else {
  104. return 0;
  105. }
  106. }
  107. static inline int skb_array_peek_len(struct skb_array *a)
  108. {
  109. return PTR_RING_PEEK_CALL(&a->ring, __skb_array_len_with_tag);
  110. }
  111. static inline int skb_array_peek_len_irq(struct skb_array *a)
  112. {
  113. return PTR_RING_PEEK_CALL_IRQ(&a->ring, __skb_array_len_with_tag);
  114. }
  115. static inline int skb_array_peek_len_bh(struct skb_array *a)
  116. {
  117. return PTR_RING_PEEK_CALL_BH(&a->ring, __skb_array_len_with_tag);
  118. }
  119. static inline int skb_array_peek_len_any(struct skb_array *a)
  120. {
  121. return PTR_RING_PEEK_CALL_ANY(&a->ring, __skb_array_len_with_tag);
  122. }
  123. static inline int skb_array_init(struct skb_array *a, int size, gfp_t gfp)
  124. {
  125. return ptr_ring_init(&a->ring, size, gfp);
  126. }
  127. static void __skb_array_destroy_skb(void *ptr)
  128. {
  129. kfree_skb(ptr);
  130. }
  131. static inline int skb_array_resize(struct skb_array *a, int size, gfp_t gfp)
  132. {
  133. return ptr_ring_resize(&a->ring, size, gfp, __skb_array_destroy_skb);
  134. }
  135. static inline int skb_array_resize_multiple(struct skb_array **rings,
  136. int nrings, unsigned int size,
  137. gfp_t gfp)
  138. {
  139. BUILD_BUG_ON(offsetof(struct skb_array, ring));
  140. return ptr_ring_resize_multiple((struct ptr_ring **)rings,
  141. nrings, size, gfp,
  142. __skb_array_destroy_skb);
  143. }
  144. static inline void skb_array_cleanup(struct skb_array *a)
  145. {
  146. ptr_ring_cleanup(&a->ring, __skb_array_destroy_skb);
  147. }
  148. #endif /* _LINUX_SKB_ARRAY_H */