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 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
146 DEBUG("Using unregistered mempool 0x%p(%s) in secondary process,"
147 " please create mempool before rte_eth_dev_start()",
148 (void *)mp, mp->name);
151 mr = priv_mr_new(priv, mp);
153 if (unlikely(mr == NULL)) {
154 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
158 if (unlikely(idx == RTE_DIM(txq->mp2mr))) {
159 /* Table is full, remove oldest entry. */
160 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
163 priv_mr_release(priv, txq->mp2mr[0]);
164 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
165 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
167 /* Store the new entry. */
168 txq_ctrl->txq.mp2mr[idx] = mr;
169 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
170 (void *)txq_ctrl, mp->name, (void *)mp,
171 txq_ctrl->txq.mp2mr[idx]->lkey);
176 * Register a Memory Region (MR) <-> Memory Pool (MP) association in
177 * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
179 * This function should only be called by txq_mp2mr().
182 * Pointer to TX queue structure.
184 * Memory Pool for which a Memory Region lkey must be returned.
186 * Index of the next available entry.
189 * mr on success, NULL on failure.
192 mlx5_txq_mp2mr_reg(struct mlx5_txq_data *txq, struct rte_mempool *mp,
195 struct mlx5_txq_ctrl *txq_ctrl =
196 container_of(txq, struct mlx5_txq_ctrl, txq);
199 priv_lock(txq_ctrl->priv);
200 mr = priv_txq_mp2mr_reg(txq_ctrl->priv, txq, mp, idx);
201 priv_unlock(txq_ctrl->priv);
205 struct mlx5_mp2mr_mbuf_check_data {
210 * Callback function for rte_mempool_obj_iter() to check whether a given
211 * mempool object looks like a mbuf.
214 * The mempool pointer
216 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
221 * Object index, unused.
224 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
225 uint32_t index __rte_unused)
227 struct mlx5_mp2mr_mbuf_check_data *data = arg;
228 struct rte_mbuf *buf = obj;
231 * Check whether mbuf structure fits element size and whether mempool
234 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
239 * Iterator function for rte_mempool_walk() to register existing mempools and
240 * fill the MP to MR cache of a TX queue.
243 * Memory Pool to register.
245 * Pointer to TX queue structure.
248 mlx5_mp2mr_iter(struct rte_mempool *mp, void *arg)
250 struct priv *priv = (struct priv *)arg;
251 struct mlx5_mp2mr_mbuf_check_data data = {
256 /* Register mempool only if the first element looks like a mbuf. */
257 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
260 mr = priv_mr_get(priv, mp);
262 priv_mr_release(priv, mr);
265 priv_mr_new(priv, mp);
269 * Register a new memory region from the mempool and store it in the memory
273 * Pointer to private structure.
275 * Pointer to the memory pool to register.
277 * The memory region on success.
280 priv_mr_new(struct priv *priv, struct rte_mempool *mp)
282 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
288 mr = rte_zmalloc_socket(__func__, sizeof(*mr), 0, mp->socket_id);
290 DEBUG("unable to configure MR, ibv_reg_mr() failed.");
293 if (mlx5_check_mempool(mp, &start, &end) != 0) {
294 ERROR("mempool %p: not virtually contiguous",
298 DEBUG("mempool %p area start=%p end=%p size=%zu",
299 (void *)mp, (void *)start, (void *)end,
300 (size_t)(end - start));
301 /* Save original addresses for exact MR lookup. */
304 /* Round start and end to page boundary if found in memory segments. */
305 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
306 uintptr_t addr = (uintptr_t)ms[i].addr;
307 size_t len = ms[i].len;
308 unsigned int align = ms[i].hugepage_sz;
310 if ((start > addr) && (start < addr + len))
311 start = RTE_ALIGN_FLOOR(start, align);
312 if ((end > addr) && (end < addr + len))
313 end = RTE_ALIGN_CEIL(end, align);
315 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
316 (void *)mp, (void *)start, (void *)end,
317 (size_t)(end - start));
318 mr->mr = ibv_reg_mr(priv->pd, (void *)start, end - start,
319 IBV_ACCESS_LOCAL_WRITE);
321 mr->lkey = rte_cpu_to_be_32(mr->mr->lkey);
322 rte_atomic32_inc(&mr->refcnt);
323 DEBUG("%p: new Memory Region %p refcnt: %d", (void *)priv,
324 (void *)mr, rte_atomic32_read(&mr->refcnt));
325 LIST_INSERT_HEAD(&priv->mr, mr, next);
330 * Search the memory region object in the memory region list.
333 * Pointer to private structure.
335 * Pointer to the memory pool to register.
337 * The memory region on success.
340 priv_mr_get(struct priv *priv, struct rte_mempool *mp)
345 if (LIST_EMPTY(&priv->mr))
347 LIST_FOREACH(mr, &priv->mr, next) {
349 rte_atomic32_inc(&mr->refcnt);
350 DEBUG("Memory Region %p refcnt: %d",
351 (void *)mr, rte_atomic32_read(&mr->refcnt));
359 * Release the memory region object.
362 * Pointer to memory region to release.
365 * 0 on success, errno on failure.
368 priv_mr_release(struct priv *priv, struct mlx5_mr *mr)
372 DEBUG("Memory Region %p refcnt: %d",
373 (void *)mr, rte_atomic32_read(&mr->refcnt));
374 if (rte_atomic32_dec_and_test(&mr->refcnt)) {
375 claim_zero(ibv_dereg_mr(mr->mr));
376 LIST_REMOVE(mr, next);
384 * Verify the flow list is empty
387 * Pointer to private structure.
389 * @return the number of object not released.
392 priv_mr_verify(struct priv *priv)
397 LIST_FOREACH(mr, &priv->mr, next) {
398 DEBUG("%p: mr %p still referenced", (void *)priv,