skb_array.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 callers invoking
  57. * this in a loop must take care to use a compiler barrier, for example
  58. * 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 struct sk_buff *skb_array_consume(struct skb_array *a)
  69. {
  70. return ptr_ring_consume(&a->ring);
  71. }
  72. static inline struct sk_buff *skb_array_consume_irq(struct skb_array *a)
  73. {
  74. return ptr_ring_consume_irq(&a->ring);
  75. }
  76. static inline struct sk_buff *skb_array_consume_any(struct skb_array *a)
  77. {
  78. return ptr_ring_consume_any(&a->ring);
  79. }
  80. static inline struct sk_buff *skb_array_consume_bh(struct skb_array *a)
  81. {
  82. return ptr_ring_consume_bh(&a->ring);
  83. }
  84. static inline int __skb_array_len_with_tag(struct sk_buff *skb)
  85. {
  86. if (likely(skb)) {
  87. int len = skb->len;
  88. if (skb_vlan_tag_present(skb))
  89. len += VLAN_HLEN;
  90. return len;
  91. } else {
  92. return 0;
  93. }
  94. }
  95. static inline int skb_array_peek_len(struct skb_array *a)
  96. {
  97. return PTR_RING_PEEK_CALL(&a->ring, __skb_array_len_with_tag);
  98. }
  99. static inline int skb_array_peek_len_irq(struct skb_array *a)
  100. {
  101. return PTR_RING_PEEK_CALL_IRQ(&a->ring, __skb_array_len_with_tag);
  102. }
  103. static inline int skb_array_peek_len_bh(struct skb_array *a)
  104. {
  105. return PTR_RING_PEEK_CALL_BH(&a->ring, __skb_array_len_with_tag);
  106. }
  107. static inline int skb_array_peek_len_any(struct skb_array *a)
  108. {
  109. return PTR_RING_PEEK_CALL_ANY(&a->ring, __skb_array_len_with_tag);
  110. }
  111. static inline int skb_array_init(struct skb_array *a, int size, gfp_t gfp)
  112. {
  113. return ptr_ring_init(&a->ring, size, gfp);
  114. }
  115. static inline void skb_array_cleanup(struct skb_array *a)
  116. {
  117. ptr_ring_cleanup(&a->ring);
  118. }
  119. #endif /* _LINUX_SKB_ARRAY_H */