4 * Copyright 2016 6WIND S.A.
5 * Copyright 2016 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
37 #pragma GCC diagnostic ignored "-Wpedantic"
39 #include <infiniband/verbs.h>
41 #pragma GCC diagnostic error "-Wpedantic"
44 #include <rte_mempool.h>
45 #include <rte_malloc.h>
48 #include "mlx5_rxtx.h"
50 struct mlx5_check_mempool_data {
56 /* Called by mlx5_check_mempool() when iterating the memory chunks. */
58 mlx5_check_mempool_cb(struct rte_mempool *mp,
59 void *opaque, struct rte_mempool_memhdr *memhdr,
62 struct mlx5_check_mempool_data *data = opaque;
67 /* It already failed, skip the next chunks. */
70 /* It is the first chunk. */
71 if (data->start == NULL && data->end == NULL) {
72 data->start = memhdr->addr;
73 data->end = data->start + memhdr->len;
76 if (data->end == memhdr->addr) {
77 data->end += memhdr->len;
80 if (data->start == (char *)memhdr->addr + memhdr->len) {
81 data->start -= memhdr->len;
84 /* Error, mempool is not virtually contiguous. */
89 * Check if a mempool can be used: it must be virtually contiguous.
92 * Pointer to memory pool.
94 * Pointer to the start address of the mempool virtual memory area
96 * Pointer to the end address of the mempool virtual memory area
99 * 0 on success (mempool is virtually contiguous), -1 on error.
101 static int mlx5_check_mempool(struct rte_mempool *mp, uintptr_t *start,
104 struct mlx5_check_mempool_data data;
106 memset(&data, 0, sizeof(data));
107 rte_mempool_mem_iter(mp, mlx5_check_mempool_cb, &data);
108 *start = (uintptr_t)data.start;
109 *end = (uintptr_t)data.end;
115 * Register a Memory Region (MR) <-> Memory Pool (MP) association in
116 * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
118 * This function should only be called by txq_mp2mr().
121 * Pointer to private structure.
123 * Pointer to TX queue structure.
125 * Memory Pool for which a Memory Region lkey must be returned.
127 * Index of the next available entry.
130 * mr on success, NULL on failure.
133 priv_txq_mp2mr_reg(struct priv *priv, struct mlx5_txq_data *txq,
134 struct rte_mempool *mp, unsigned int idx)
136 struct mlx5_txq_ctrl *txq_ctrl =
137 container_of(txq, struct mlx5_txq_ctrl, txq);
140 /* Add a new entry, register MR first. */
141 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
142 (void *)txq_ctrl, mp->name, (void *)mp);
143 mr = priv_mr_get(priv, mp);
145 mr = priv_mr_new(priv, mp);
146 if (unlikely(mr == NULL)) {
147 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
151 if (unlikely(idx == RTE_DIM(txq->mp2mr))) {
152 /* Table is full, remove oldest entry. */
153 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
156 priv_mr_release(priv, txq->mp2mr[0]);
157 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
158 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
160 /* Store the new entry. */
161 txq_ctrl->txq.mp2mr[idx] = mr;
162 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
163 (void *)txq_ctrl, mp->name, (void *)mp,
164 txq_ctrl->txq.mp2mr[idx]->lkey);
169 * Register a Memory Region (MR) <-> Memory Pool (MP) association in
170 * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
172 * This function should only be called by txq_mp2mr().
175 * Pointer to TX queue structure.
177 * Memory Pool for which a Memory Region lkey must be returned.
179 * Index of the next available entry.
182 * mr on success, NULL on failure.
185 mlx5_txq_mp2mr_reg(struct mlx5_txq_data *txq, struct rte_mempool *mp,
188 struct mlx5_txq_ctrl *txq_ctrl =
189 container_of(txq, struct mlx5_txq_ctrl, txq);
192 priv_lock(txq_ctrl->priv);
193 mr = priv_txq_mp2mr_reg(txq_ctrl->priv, txq, mp, idx);
194 priv_unlock(txq_ctrl->priv);
198 struct mlx5_mp2mr_mbuf_check_data {
203 * Callback function for rte_mempool_obj_iter() to check whether a given
204 * mempool object looks like a mbuf.
207 * The mempool pointer
209 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
214 * Object index, unused.
217 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
218 uint32_t index __rte_unused)
220 struct mlx5_mp2mr_mbuf_check_data *data = arg;
221 struct rte_mbuf *buf = obj;
224 * Check whether mbuf structure fits element size and whether mempool
227 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
232 * Iterator function for rte_mempool_walk() to register existing mempools and
233 * fill the MP to MR cache of a TX queue.
236 * Memory Pool to register.
238 * Pointer to TX queue structure.
241 mlx5_mp2mr_iter(struct rte_mempool *mp, void *arg)
243 struct priv *priv = (struct priv *)arg;
244 struct mlx5_mp2mr_mbuf_check_data data = {
249 /* Register mempool only if the first element looks like a mbuf. */
250 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
253 mr = priv_mr_get(priv, mp);
255 priv_mr_release(priv, mr);
258 priv_mr_new(priv, mp);
262 * Register a new memory region from the mempool and store it in the memory
266 * Pointer to private structure.
268 * Pointer to the memory pool to register.
270 * The memory region on success.
273 priv_mr_new(struct priv *priv, struct rte_mempool *mp)
275 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
281 mr = rte_zmalloc_socket(__func__, sizeof(*mr), 0, mp->socket_id);
283 DEBUG("unable to configure MR, ibv_reg_mr() failed.");
286 if (mlx5_check_mempool(mp, &start, &end) != 0) {
287 ERROR("mempool %p: not virtually contiguous",
291 DEBUG("mempool %p area start=%p end=%p size=%zu",
292 (void *)mp, (void *)start, (void *)end,
293 (size_t)(end - start));
294 /* Round start and end to page boundary if found in memory segments. */
295 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
296 uintptr_t addr = (uintptr_t)ms[i].addr;
297 size_t len = ms[i].len;
298 unsigned int align = ms[i].hugepage_sz;
300 if ((start > addr) && (start < addr + len))
301 start = RTE_ALIGN_FLOOR(start, align);
302 if ((end > addr) && (end < addr + len))
303 end = RTE_ALIGN_CEIL(end, align);
305 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
306 (void *)mp, (void *)start, (void *)end,
307 (size_t)(end - start));
308 mr->mr = ibv_reg_mr(priv->pd, (void *)start, end - start,
309 IBV_ACCESS_LOCAL_WRITE);
311 mr->lkey = rte_cpu_to_be_32(mr->mr->lkey);
313 mr->end = (uintptr_t)mr->mr->addr + mr->mr->length;
314 rte_atomic32_inc(&mr->refcnt);
315 DEBUG("%p: new Memory Region %p refcnt: %d", (void *)priv,
316 (void *)mr, rte_atomic32_read(&mr->refcnt));
317 LIST_INSERT_HEAD(&priv->mr, mr, next);
322 * Search the memory region object in the memory region list.
325 * Pointer to private structure.
327 * Pointer to the memory pool to register.
329 * The memory region on success.
332 priv_mr_get(struct priv *priv, struct rte_mempool *mp)
337 if (LIST_EMPTY(&priv->mr))
339 LIST_FOREACH(mr, &priv->mr, next) {
341 rte_atomic32_inc(&mr->refcnt);
342 DEBUG("Memory Region %p refcnt: %d",
343 (void *)mr, rte_atomic32_read(&mr->refcnt));
351 * Release the memory region object.
354 * Pointer to memory region to release.
357 * 0 on success, errno on failure.
360 priv_mr_release(struct priv *priv, struct mlx5_mr *mr)
364 DEBUG("Memory Region %p refcnt: %d",
365 (void *)mr, rte_atomic32_read(&mr->refcnt));
366 if (rte_atomic32_dec_and_test(&mr->refcnt)) {
367 claim_zero(ibv_dereg_mr(mr->mr));
368 LIST_REMOVE(mr, next);
376 * Verify the flow list is empty
379 * Pointer to private structure.
381 * @return the number of object not released.
384 priv_mr_verify(struct priv *priv)
389 LIST_FOREACH(mr, &priv->mr, next) {
390 DEBUG("%p: mr %p still referenced", (void *)priv,