Allocating Net Buffer Lists (NBLs), Net Buffers (NBs), and associated contexts dynamically within the receive and send paths of an NDIS filter driver can impact performance if not managed efficiently. While NDIS provides functions like NdisAllocateNetBuffer(), NdisAllocateNetBufferLists(), and NdisAllocateNetBufferListContext() to allocate these structures, frequent allocations and deallocations can lead to memory fragmentation and increased latency.
Performance Considerations
- Memory Fragmentation: Continuous allocation and deallocation of NBLs and NBs can cause memory fragmentation, leading to inefficient memory usage and potential allocation failures.
- Latency: Dynamic allocation during packet processing can introduce latency, affecting the overall performance of the network stack.
- Cache Efficiency: NDIS does not inherently cache NBLs, NBs, or contexts in a manner similar to lookaside lists, meaning each allocation may involve more overhead.
Recommended Approaches
To mitigate performance impacts, consider the following strategies:
- Pre-allocate Buffers: Allocate a pool of NBLs and NBs during driver initialization. This approach reduces the need for dynamic allocations during packet processing, thereby minimizing latency and fragmentation.
- Use Lookaside Lists: Implement lookaside lists to manage memory for NBLs and NBs. This technique allows for efficient reuse of memory, reducing the overhead associated with frequent allocations and deallocations.
- Allocate Contexts Only: If the underlying or overlying driver supports it, allocate context for NBLs using NdisAllocateNetBufferListContext(). This method allows modification of certain fields without the need for full NBL allocation, reducing overhead.
- Optimize MDL Usage: When allocating NBs, consider using NdisAllocateNetBufferMdlAndData() to allocate the NET_BUFFER structure, MDL, and data in a single memory buffer. This approach can improve performance by reducing the number of allocations and simplifying memory management.
While dynamic allocation of NBLs, NBs, and contexts is feasible, it is advisable to minimize such allocations within the receive and send paths to enhance performance. Implementing pre-allocation strategies and utilizing memory management techniques like lookaside lists can significantly improve the efficiency of your NDIS filter driver.