GNU Linux-libre 6.6.34-gnu
[releases.git] / Documentation / networking / page_pool.rst
1 .. SPDX-License-Identifier: GPL-2.0
2
3 =============
4 Page Pool API
5 =============
6
7 .. kernel-doc:: include/net/page_pool/helpers.h
8    :doc: page_pool allocator
9
10 Architecture overview
11 =====================
12
13 .. code-block:: none
14
15     +------------------+
16     |       Driver     |
17     +------------------+
18             ^
19             |
20             |
21             |
22             v
23     +--------------------------------------------+
24     |                request memory              |
25     +--------------------------------------------+
26         ^                                  ^
27         |                                  |
28         | Pool empty                       | Pool has entries
29         |                                  |
30         v                                  v
31     +-----------------------+     +------------------------+
32     | alloc (and map) pages |     |  get page from cache   |
33     +-----------------------+     +------------------------+
34                                     ^                    ^
35                                     |                    |
36                                     | cache available    | No entries, refill
37                                     |                    | from ptr-ring
38                                     |                    |
39                                     v                    v
40                           +-----------------+     +------------------+
41                           |   Fast cache    |     |  ptr-ring cache  |
42                           +-----------------+     +------------------+
43
44 API interface
45 =============
46 The number of pools created **must** match the number of hardware queues
47 unless hardware restrictions make that impossible. This would otherwise beat the
48 purpose of page pool, which is allocate pages fast from cache without locking.
49 This lockless guarantee naturally comes from running under a NAPI softirq.
50 The protection doesn't strictly have to be NAPI, any guarantee that allocating
51 a page will cause no race conditions is enough.
52
53 .. kernel-doc:: net/core/page_pool.c
54    :identifiers: page_pool_create
55
56 .. kernel-doc:: include/net/page_pool/types.h
57    :identifiers: struct page_pool_params
58
59 .. kernel-doc:: include/net/page_pool/helpers.h
60    :identifiers: page_pool_put_page page_pool_put_full_page
61                  page_pool_recycle_direct page_pool_dev_alloc_pages
62                  page_pool_get_dma_addr page_pool_get_dma_dir
63
64 .. kernel-doc:: net/core/page_pool.c
65    :identifiers: page_pool_put_page_bulk page_pool_get_stats
66
67 DMA sync
68 --------
69 Driver is always responsible for syncing the pages for the CPU.
70 Drivers may choose to take care of syncing for the device as well
71 or set the ``PP_FLAG_DMA_SYNC_DEV`` flag to request that pages
72 allocated from the page pool are already synced for the device.
73
74 If ``PP_FLAG_DMA_SYNC_DEV`` is set, the driver must inform the core what portion
75 of the buffer has to be synced. This allows the core to avoid syncing the entire
76 page when the drivers knows that the device only accessed a portion of the page.
77
78 Most drivers will reserve headroom in front of the frame. This part
79 of the buffer is not touched by the device, so to avoid syncing
80 it drivers can set the ``offset`` field in struct page_pool_params
81 appropriately.
82
83 For pages recycled on the XDP xmit and skb paths the page pool will
84 use the ``max_len`` member of struct page_pool_params to decide how
85 much of the page needs to be synced (starting at ``offset``).
86 When directly freeing pages in the driver (page_pool_put_page())
87 the ``dma_sync_size`` argument specifies how much of the buffer needs
88 to be synced.
89
90 If in doubt set ``offset`` to 0, ``max_len`` to ``PAGE_SIZE`` and
91 pass -1 as ``dma_sync_size``. That combination of arguments is always
92 correct.
93
94 Note that the syncing parameters are for the entire page.
95 This is important to remember when using fragments (``PP_FLAG_PAGE_FRAG``),
96 where allocated buffers may be smaller than a full page.
97 Unless the driver author really understands page pool internals
98 it's recommended to always use ``offset = 0``, ``max_len = PAGE_SIZE``
99 with fragmented page pools.
100
101 Stats API and structures
102 ------------------------
103 If the kernel is configured with ``CONFIG_PAGE_POOL_STATS=y``, the API
104 page_pool_get_stats() and structures described below are available.
105 It takes a  pointer to a ``struct page_pool`` and a pointer to a struct
106 page_pool_stats allocated by the caller.
107
108 The API will fill in the provided struct page_pool_stats with
109 statistics about the page_pool.
110
111 .. kernel-doc:: include/net/page_pool/types.h
112    :identifiers: struct page_pool_recycle_stats
113                  struct page_pool_alloc_stats
114                  struct page_pool_stats
115
116 Coding examples
117 ===============
118
119 Registration
120 ------------
121
122 .. code-block:: c
123
124     /* Page pool registration */
125     struct page_pool_params pp_params = { 0 };
126     struct xdp_rxq_info xdp_rxq;
127     int err;
128
129     pp_params.order = 0;
130     /* internal DMA mapping in page_pool */
131     pp_params.flags = PP_FLAG_DMA_MAP;
132     pp_params.pool_size = DESC_NUM;
133     pp_params.nid = NUMA_NO_NODE;
134     pp_params.dev = priv->dev;
135     pp_params.napi = napi; /* only if locking is tied to NAPI */
136     pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
137     page_pool = page_pool_create(&pp_params);
138
139     err = xdp_rxq_info_reg(&xdp_rxq, ndev, 0);
140     if (err)
141         goto err_out;
142
143     err = xdp_rxq_info_reg_mem_model(&xdp_rxq, MEM_TYPE_PAGE_POOL, page_pool);
144     if (err)
145         goto err_out;
146
147 NAPI poller
148 -----------
149
150
151 .. code-block:: c
152
153     /* NAPI Rx poller */
154     enum dma_data_direction dma_dir;
155
156     dma_dir = page_pool_get_dma_dir(dring->page_pool);
157     while (done < budget) {
158         if (some error)
159             page_pool_recycle_direct(page_pool, page);
160         if (packet_is_xdp) {
161             if XDP_DROP:
162                 page_pool_recycle_direct(page_pool, page);
163         } else (packet_is_skb) {
164             skb_mark_for_recycle(skb);
165             new_page = page_pool_dev_alloc_pages(page_pool);
166         }
167     }
168
169 Stats
170 -----
171
172 .. code-block:: c
173
174         #ifdef CONFIG_PAGE_POOL_STATS
175         /* retrieve stats */
176         struct page_pool_stats stats = { 0 };
177         if (page_pool_get_stats(page_pool, &stats)) {
178                 /* perhaps the driver reports statistics with ethool */
179                 ethtool_print_allocation_stats(&stats.alloc_stats);
180                 ethtool_print_recycle_stats(&stats.recycle_stats);
181         }
182         #endif
183
184 Driver unload
185 -------------
186
187 .. code-block:: c
188
189     /* Driver unload */
190     page_pool_put_full_page(page_pool, page, false);
191     xdp_rxq_info_unreg(&xdp_rxq);