netmem.rst 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==================================
  3. Netmem Support for Network Drivers
  4. ==================================
  5. This document outlines the requirements for network drivers to support netmem,
  6. an abstract memory type that enables features like device memory TCP. By
  7. supporting netmem, drivers can work with various underlying memory types
  8. with little to no modification.
  9. Benefits of Netmem :
  10. * Flexibility: Netmem can be backed by different memory types (e.g., struct
  11. page, DMA-buf), allowing drivers to support various use cases such as device
  12. memory TCP.
  13. * Future-proof: Drivers with netmem support are ready for upcoming
  14. features that rely on it.
  15. * Simplified Development: Drivers interact with a consistent API,
  16. regardless of the underlying memory implementation.
  17. Driver RX Requirements
  18. ======================
  19. 1. The driver must support page_pool.
  20. 2. The driver must support the tcp-data-split ethtool option.
  21. 3. The driver must use the page_pool netmem APIs for payload memory. The netmem
  22. APIs currently 1-to-1 correspond with page APIs. Conversion to netmem should
  23. be achievable by switching the page APIs to netmem APIs and tracking memory
  24. via netmem_refs in the driver rather than struct page * :
  25. - page_pool_alloc -> page_pool_alloc_netmem
  26. - page_pool_get_dma_addr -> page_pool_get_dma_addr_netmem
  27. - page_pool_put_page -> page_pool_put_netmem
  28. Not all page APIs have netmem equivalents at the moment. If your driver
  29. relies on a missing netmem API, feel free to add and propose to netdev@, or
  30. reach out to the maintainers and/or almasrymina@google.com for help adding
  31. the netmem API.
  32. 4. The driver must use the following PP_FLAGS:
  33. - PP_FLAG_DMA_MAP: netmem is not dma-mappable by the driver. The driver
  34. must delegate the dma mapping to the page_pool, which knows when
  35. dma-mapping is (or is not) appropriate.
  36. - PP_FLAG_DMA_SYNC_DEV: netmem dma addr is not necessarily dma-syncable
  37. by the driver. The driver must delegate the dma syncing to the page_pool,
  38. which knows when dma-syncing is (or is not) appropriate.
  39. - PP_FLAG_ALLOW_UNREADABLE_NETMEM. The driver must specify this flag iff
  40. tcp-data-split is enabled.
  41. 5. The driver must not assume the netmem is readable and/or backed by pages.
  42. The netmem returned by the page_pool may be unreadable, in which case
  43. netmem_address() will return NULL. The driver must correctly handle
  44. unreadable netmem, i.e. don't attempt to handle its contents when
  45. netmem_address() is NULL.
  46. Ideally, drivers should not have to check the underlying netmem type via
  47. helpers like netmem_is_net_iov() or convert the netmem to any of its
  48. underlying types via netmem_to_page() or netmem_to_net_iov(). In most cases,
  49. netmem or page_pool helpers that abstract this complexity are provided
  50. (and more can be added).
  51. 6. The driver must use page_pool_dma_sync_netmem_for_cpu() in lieu of
  52. dma_sync_single_range_for_cpu(). For some memory providers, dma_syncing for
  53. CPU will be done by the page_pool, for others (particularly dmabuf memory
  54. provider), dma syncing for CPU is the responsibility of the userspace using
  55. dmabuf APIs. The driver must delegate the entire dma-syncing operation to
  56. the page_pool which will do it correctly.
  57. 7. Avoid implementing driver-specific recycling on top of the page_pool. Drivers
  58. cannot hold onto a struct page to do their own recycling as the netmem may
  59. not be backed by a struct page. However, you may hold onto a page_pool
  60. reference with page_pool_fragment_netmem() or page_pool_ref_netmem() for
  61. that purpose, but be mindful that some netmem types might have longer
  62. circulation times, such as when userspace holds a reference in zerocopy
  63. scenarios.
  64. Driver TX Requirements
  65. ======================
  66. 1. The Driver must not pass the netmem dma_addr to any of the dma-mapping APIs
  67. directly. This is because netmem dma_addrs may come from a source like
  68. dma-buf that is not compatible with the dma-mapping APIs.
  69. Helpers like netmem_dma_unmap_page_attrs() & netmem_dma_unmap_addr_set()
  70. should be used in lieu of dma_unmap_page[_attrs](), dma_unmap_addr_set().
  71. The netmem variants will handle netmem dma_addrs correctly regardless of the
  72. source, delegating to the dma-mapping APIs when appropriate.
  73. Not all dma-mapping APIs have netmem equivalents at the moment. If your
  74. driver relies on a missing netmem API, feel free to add and propose to
  75. netdev@, or reach out to the maintainers and/or almasrymina@google.com for
  76. help adding the netmem API.
  77. 2. Driver should declare support by setting `netdev->netmem_tx = true`