You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pkbuff.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef _GPXE_PKBUFF_H
  2. #define _GPXE_PKBUFF_H
  3. /** @file
  4. *
  5. * Packet buffers
  6. *
  7. * Packet buffers are used to contain network packets. Methods are
  8. * provided for appending, prepending, etc. data.
  9. *
  10. */
  11. #include <stdint.h>
  12. #include <assert.h>
  13. #include <gpxe/list.h>
  14. /**
  15. * Packet buffer alignment
  16. *
  17. * Packet buffers allocated via alloc_pkb() are guaranteed to be
  18. * physically aligned to this boundary. Some cards cannot DMA across
  19. * a 4kB boundary. With a standard Ethernet MTU, aligning to a 2kB
  20. * boundary is sufficient to guarantee no 4kB boundary crossings. For
  21. * a jumbo Ethernet MTU, a packet may be larger than 4kB anyway.
  22. */
  23. #define PKBUFF_ALIGN 2048
  24. /**
  25. * Minimum packet buffer length
  26. *
  27. * alloc_pkb() will round up the allocated length to this size if
  28. * necessary. This is used on behalf of hardware that is not capable
  29. * of auto-padding.
  30. */
  31. #define PKB_ZLEN 64
  32. /** A packet buffer
  33. *
  34. * This structure is used to represent a network packet within gPXE.
  35. */
  36. struct pk_buff {
  37. /** List of which this buffer is a member */
  38. struct list_head list;
  39. /** Start of the buffer */
  40. void *head;
  41. /** Start of data */
  42. void *data;
  43. /** End of data */
  44. void *tail;
  45. /** End of the buffer */
  46. void *end;
  47. };
  48. /**
  49. * Reserve space at start of packet buffer
  50. *
  51. * @v pkb Packet buffer
  52. * @v len Length to reserve
  53. * @ret data Pointer to new start of buffer
  54. */
  55. static inline void * pkb_reserve ( struct pk_buff *pkb, size_t len ) {
  56. pkb->data += len;
  57. pkb->tail += len;
  58. assert ( pkb->tail <= pkb->end );
  59. return pkb->data;
  60. }
  61. /**
  62. * Add data to start of packet buffer
  63. *
  64. * @v pkb Packet buffer
  65. * @v len Length to add
  66. * @ret data Pointer to new start of buffer
  67. */
  68. static inline void * pkb_push ( struct pk_buff *pkb, size_t len ) {
  69. pkb->data -= len;
  70. assert ( pkb->data >= pkb->head );
  71. return pkb->data;
  72. }
  73. /**
  74. * Remove data from start of packet buffer
  75. *
  76. * @v pkb Packet buffer
  77. * @v len Length to remove
  78. * @ret data Pointer to new start of buffer
  79. */
  80. static inline void * pkb_pull ( struct pk_buff *pkb, size_t len ) {
  81. pkb->data += len;
  82. assert ( pkb->data <= pkb->tail );
  83. return pkb->data;
  84. }
  85. /**
  86. * Add data to end of packet buffer
  87. *
  88. * @v pkb Packet buffer
  89. * @v len Length to add
  90. * @ret data Pointer to newly added space
  91. */
  92. static inline void * pkb_put ( struct pk_buff *pkb, size_t len ) {
  93. void *old_tail = pkb->tail;
  94. pkb->tail += len;
  95. assert ( pkb->tail <= pkb->end );
  96. return old_tail;
  97. }
  98. /**
  99. * Remove data from end of packet buffer
  100. *
  101. * @v pkb Packet buffer
  102. * @v len Length to remove
  103. */
  104. static inline void pkb_unput ( struct pk_buff *pkb, size_t len ) {
  105. pkb->tail -= len;
  106. assert ( pkb->tail >= pkb->data );
  107. }
  108. /**
  109. * Empty a packet buffer
  110. *
  111. * @v pkb Packet buffer
  112. */
  113. static inline void pkb_empty ( struct pk_buff *pkb ) {
  114. pkb->tail = pkb->data;
  115. }
  116. /**
  117. * Calculate length of data in a packet buffer
  118. *
  119. * @v pkb Packet buffer
  120. * @ret len Length of data in buffer
  121. */
  122. static inline size_t pkb_len ( struct pk_buff *pkb ) {
  123. return ( pkb->tail - pkb->data );
  124. }
  125. /**
  126. * Calculate available space at start of a packet buffer
  127. *
  128. * @v pkb Packet buffer
  129. * @ret len Length of data available at start of buffer
  130. */
  131. static inline size_t pkb_headroom ( struct pk_buff *pkb ) {
  132. return ( pkb->data - pkb->head );
  133. }
  134. /**
  135. * Calculate available space at end of a packet buffer
  136. *
  137. * @v pkb Packet buffer
  138. * @ret len Length of data available at end of buffer
  139. */
  140. static inline size_t pkb_tailroom ( struct pk_buff *pkb ) {
  141. return ( pkb->end - pkb->tail );
  142. }
  143. extern struct pk_buff * alloc_pkb ( size_t len );
  144. extern void free_pkb ( struct pk_buff *pkb );
  145. extern void pkb_pad ( struct pk_buff *pkb, size_t min_len );
  146. #endif /* _GPXE_PKBUFF_H */