Leaked source code of windows server 2003
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.

272 lines
5.5 KiB

  1. /*++
  2. Copyright(c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. brdgbuf.h
  5. Abstract:
  6. Ethernet MAC level bridge.
  7. Buffer management section
  8. PUBLIC header
  9. Author:
  10. Mark Aiken
  11. (original bridge by Jameel Hyder)
  12. Environment:
  13. Kernel mode driver
  14. Revision History:
  15. Feb 2000 - Original version
  16. --*/
  17. #include "brdgpkt.h"
  18. // ===========================================================================
  19. //
  20. // DECLARATIONS
  21. //
  22. // ===========================================================================
  23. typedef enum
  24. {
  25. BrdgOwnCopyPacket,
  26. BrdgOwnWrapperPacket,
  27. BrdgNotOwned
  28. } PACKET_OWNERSHIP;
  29. // ===========================================================================
  30. //
  31. // PROTOTYPES
  32. //
  33. // ===========================================================================
  34. NTSTATUS
  35. BrdgBufDriverInit();
  36. VOID
  37. BrdgBufCleanup();
  38. PNDIS_PACKET
  39. BrdgBufGetBaseCopyPacket(
  40. OUT PPACKET_INFO *pppi
  41. );
  42. VOID
  43. BrdgBufFreeWrapperPacket(
  44. IN PNDIS_PACKET pPacket,
  45. IN PPACKET_INFO ppi,
  46. IN PADAPT pQuotaOwner
  47. );
  48. VOID
  49. BrdgBufFreeBaseCopyPacket(
  50. IN PNDIS_PACKET pPacket,
  51. IN PPACKET_INFO ppi
  52. );
  53. VOID
  54. BrdgBufFreeBaseWrapperPacket(
  55. IN PNDIS_PACKET pPacket,
  56. IN PPACKET_INFO ppi
  57. );
  58. BOOLEAN
  59. BrdgBufAssignBasePacketQuota(
  60. IN PNDIS_PACKET pPacket,
  61. IN PADAPT pAdapt
  62. );
  63. VOID
  64. BrdgBufReleaseBasePacketQuota(
  65. IN PNDIS_PACKET pPacket,
  66. IN PADAPT pAdapt
  67. );
  68. PNDIS_PACKET
  69. BrdgBufGetWrapperPacket(
  70. OUT PPACKET_INFO *pppi,
  71. IN PADAPT pAdapt
  72. );
  73. NDIS_STATUS
  74. BrdgBufChainCopyBuffers(
  75. IN PNDIS_PACKET pTargetPacket,
  76. IN PNDIS_PACKET pSourcePacket
  77. );
  78. PACKET_OWNERSHIP
  79. BrdgBufGetPacketOwnership(
  80. IN PNDIS_PACKET pPacket
  81. );
  82. VOID
  83. BrdgBufGetStatistics(
  84. PBRIDGE_BUFFER_STATISTICS pStats
  85. );
  86. // ===========================================================================
  87. //
  88. // INLINES
  89. //
  90. // ===========================================================================
  91. //
  92. // Retrieves the first NDIS_BUFFER chained to a given packet (NULL if none)
  93. //
  94. __forceinline
  95. PNDIS_BUFFER
  96. BrdgBufPacketHeadBuffer(
  97. IN PNDIS_PACKET pPacket
  98. )
  99. {
  100. PNDIS_BUFFER pBuffer = NULL;
  101. SAFEASSERT( pPacket != NULL );
  102. if (pPacket)
  103. {
  104. NdisQueryPacket( pPacket, NULL, NULL, &pBuffer, NULL );
  105. }
  106. return pBuffer;
  107. }
  108. //
  109. // Retrieves the total size of all buffers chained to a packet
  110. //
  111. __forceinline
  112. UINT
  113. BrdgBufTotalPacketSize(
  114. IN PNDIS_PACKET pPacket
  115. )
  116. {
  117. UINT size = 0;
  118. SAFEASSERT( pPacket != NULL );
  119. if (pPacket)
  120. {
  121. NdisQueryPacket( pPacket, NULL, NULL, NULL, &size );
  122. }
  123. return size;
  124. }
  125. //
  126. // Retrieves the virtual address of the data in the first buffer chained
  127. // to a packet (holds the Ethernet header)
  128. //
  129. __forceinline
  130. PVOID
  131. BrdgBufGetPacketHeader(
  132. IN PNDIS_PACKET pPacket
  133. )
  134. {
  135. PNDIS_BUFFER pBuffer = NULL;
  136. PVOID pHeader = NULL;
  137. UINT Length = 0;
  138. SAFEASSERT( pPacket != NULL );
  139. if (pPacket)
  140. {
  141. pBuffer = BrdgBufPacketHeadBuffer( pPacket );
  142. }
  143. SAFEASSERT( pBuffer != NULL );
  144. if (pBuffer)
  145. {
  146. NdisQueryBufferSafe( pBuffer, &pHeader, &Length, NormalPagePriority );
  147. }
  148. SAFEASSERT( pHeader != NULL );
  149. return pHeader;
  150. }
  151. //
  152. // Unchains and frees all buffers chained to a given packet
  153. //
  154. __forceinline
  155. VOID
  156. BrdgBufUnchainCopyBuffers(
  157. IN PNDIS_PACKET pPacket
  158. )
  159. {
  160. PNDIS_BUFFER pCurBuf;
  161. if (pPacket)
  162. {
  163. NdisUnchainBufferAtFront( pPacket, &pCurBuf );
  164. while( pCurBuf != NULL )
  165. {
  166. NdisFreeBuffer( pCurBuf );
  167. NdisUnchainBufferAtFront( pPacket, &pCurBuf );
  168. }
  169. }
  170. }
  171. //
  172. // Determines whether this packet was allocated from our copy pool
  173. //
  174. __forceinline
  175. BOOLEAN
  176. BrdgBufIsCopyPacket(
  177. IN PNDIS_PACKET pPacket
  178. )
  179. {
  180. PACKET_OWNERSHIP Own = BrdgBufGetPacketOwnership(pPacket);
  181. return (BOOLEAN)(Own == BrdgOwnCopyPacket);
  182. }
  183. //
  184. // Determines whether this packet was allocated from our wrapper pool
  185. //
  186. __forceinline
  187. BOOLEAN
  188. BrdgBufIsWrapperPacket(
  189. IN PNDIS_PACKET pPacket
  190. )
  191. {
  192. PACKET_OWNERSHIP Own = BrdgBufGetPacketOwnership(pPacket);
  193. return (BOOLEAN)(Own == BrdgOwnWrapperPacket);
  194. }
  195. //
  196. // Initializes an ADAPTER_QUOTA structure
  197. //
  198. __forceinline
  199. VOID
  200. BrdgBufInitializeQuota(
  201. IN PADAPTER_QUOTA pQuota
  202. )
  203. {
  204. pQuota->UsedPackets[0] = pQuota->UsedPackets[1] = 0L;
  205. }
  206. // DO NOT use this variable directly outside of BrdgBuf.c
  207. extern NDIS_HANDLE gWrapperBufferPoolHandle;
  208. //
  209. // Allocates an NDIS_BUFFER from our pool
  210. //
  211. __forceinline
  212. PNDIS_BUFFER
  213. BrdgBufAllocateBuffer(
  214. IN PVOID p,
  215. IN UINT len
  216. )
  217. {
  218. PNDIS_BUFFER pBuf;
  219. NDIS_STATUS Status;
  220. NdisAllocateBuffer( &Status, &pBuf, gWrapperBufferPoolHandle, p, len );
  221. if( Status != NDIS_STATUS_SUCCESS )
  222. {
  223. THROTTLED_DBGPRINT(BUF, ("Failed to allocate a MDL in BrdgBufAllocateBuffer: %08x\n", Status));
  224. return NULL;
  225. }
  226. return pBuf;
  227. }