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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. struct net_protocol;
  15. struct ll_protocol;
  16. /**
  17. * Packet buffer alignment
  18. *
  19. * Packet buffers allocated via alloc_pkb() are guaranteed to be
  20. * physically aligned to this boundary. Some cards cannot DMA across
  21. * a 4kB boundary. With a standard Ethernet MTU, aligning to a 2kB
  22. * boundary is sufficient to guarantee no 4kB boundary crossings. For
  23. * a jumbo Ethernet MTU, a packet may be larger than 4kB anyway.
  24. */
  25. #define PKBUFF_ALIGN 2048
  26. /** A packet buffer
  27. *
  28. * This structure is used to represent a network packet within gPXE.
  29. */
  30. struct pk_buff {
  31. /** Start of the buffer */
  32. void *head;
  33. /** Start of data */
  34. void *data;
  35. /** End of data */
  36. void *tail;
  37. /** End of the buffer */
  38. void *end;
  39. /** List of which this buffer is a member */
  40. struct list_head list;
  41. /** The network-layer protocol */
  42. struct net_protocol *net_protocol;
  43. /** The link-layer protocol */
  44. struct ll_protocol *ll_protocol;
  45. };
  46. /**
  47. * Reserve space at start of packet buffer
  48. *
  49. * @v pkb Packet buffer
  50. * @v len Length to reserve
  51. * @ret data Pointer to new start of buffer
  52. */
  53. static inline void * pkb_reserve ( struct pk_buff *pkb, size_t len ) {
  54. pkb->data += len;
  55. pkb->tail += len;
  56. assert ( pkb->tail <= pkb->end );
  57. return pkb->data;
  58. }
  59. /**
  60. * Add data to start of packet buffer
  61. *
  62. * @v pkb Packet buffer
  63. * @v len Length to add
  64. * @ret data Pointer to new start of buffer
  65. */
  66. static inline void * pkb_push ( struct pk_buff *pkb, size_t len ) {
  67. pkb->data -= len;
  68. assert ( pkb->data >= pkb->head );
  69. return pkb->data;
  70. }
  71. /**
  72. * Remove data from start of packet buffer
  73. *
  74. * @v pkb Packet buffer
  75. * @v len Length to remove
  76. * @ret data Pointer to new start of buffer
  77. */
  78. static inline void * pkb_pull ( struct pk_buff *pkb, size_t len ) {
  79. pkb->data += len;
  80. assert ( pkb->data <= pkb->tail );
  81. return pkb->data;
  82. }
  83. /**
  84. * Add data to end of packet buffer
  85. *
  86. * @v pkb Packet buffer
  87. * @v len Length to add
  88. * @ret data Pointer to newly added space
  89. */
  90. static inline void * pkb_put ( struct pk_buff *pkb, size_t len ) {
  91. void *old_tail = pkb->tail;
  92. pkb->tail += len;
  93. assert ( pkb->tail <= pkb->end );
  94. return old_tail;
  95. }
  96. /**
  97. * Remove data from end of packet buffer
  98. *
  99. * @v pkb Packet buffer
  100. * @v len Length to remove
  101. */
  102. static inline void pkb_unput ( struct pk_buff *pkb, size_t len ) {
  103. pkb->tail -= len;
  104. assert ( pkb->tail >= pkb->data );
  105. }
  106. /**
  107. * Empty a packet buffer
  108. *
  109. * @v pkb Packet buffer
  110. */
  111. static inline void pkb_empty ( struct pk_buff *pkb ) {
  112. pkb->tail = pkb->data;
  113. }
  114. /**
  115. * Calculate length of data in a packet buffer
  116. *
  117. * @v pkb Packet buffer
  118. * @ret len Length of data in buffer
  119. */
  120. static inline size_t pkb_len ( struct pk_buff *pkb ) {
  121. return ( pkb->tail - pkb->data );
  122. }
  123. extern struct pk_buff * alloc_pkb ( size_t len );
  124. extern void free_pkb ( struct pk_buff *pkb );
  125. #endif /* _GPXE_PKBUFF_H */