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"
49 #include "mlx5_glue.h"
51 struct mlx5_check_mempool_data {
57 /* Called by mlx5_check_mempool() when iterating the memory chunks. */
59 mlx5_check_mempool_cb(struct rte_mempool *mp,
60 void *opaque, struct rte_mempool_memhdr *memhdr,
63 struct mlx5_check_mempool_data *data = opaque;
68 /* It already failed, skip the next chunks. */
71 /* It is the first chunk. */
72 if (data->start == NULL && data->end == NULL) {
73 data->start = memhdr->addr;
74 data->end = data->start + memhdr->len;
77 if (data->end == memhdr->addr) {
78 data->end += memhdr->len;
81 if (data->start == (char *)memhdr->addr + memhdr->len) {
82 data->start -= memhdr->len;
85 /* Error, mempool is not virtually contiguous. */
90 * Check if a mempool can be used: it must be virtually contiguous.
93 * Pointer to memory pool.
95 * Pointer to the start address of the mempool virtual memory area
97 * Pointer to the end address of the mempool virtual memory area
100 * 0 on success (mempool is virtually contiguous), -1 on error.
102 static int mlx5_check_mempool(struct rte_mempool *mp, uintptr_t *start,
105 struct mlx5_check_mempool_data data;
107 memset(&data, 0, sizeof(data));
108 rte_mempool_mem_iter(mp, mlx5_check_mempool_cb, &data);
109 *start = (uintptr_t)data.start;
110 *end = (uintptr_t)data.end;
116 * Register a Memory Region (MR) <-> Memory Pool (MP) association in
117 * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
119 * This function should only be called by txq_mp2mr().
122 * Pointer to private structure.
124 * Pointer to TX queue structure.
126 * Memory Pool for which a Memory Region lkey must be returned.
128 * Index of the next available entry.
131 * mr on success, NULL on failure.
134 priv_txq_mp2mr_reg(struct priv *priv, struct mlx5_txq_data *txq,
135 struct rte_mempool *mp, unsigned int idx)
137 struct mlx5_txq_ctrl *txq_ctrl =
138 container_of(txq, struct mlx5_txq_ctrl, txq);
141 /* Add a new entry, register MR first. */
142 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
143 (void *)txq_ctrl, mp->name, (void *)mp);
144 mr = priv_mr_get(priv, mp);
146 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
147 DEBUG("Using unregistered mempool 0x%p(%s) in secondary process,"
148 " please create mempool before rte_eth_dev_start()",
149 (void *)mp, mp->name);
152 mr = priv_mr_new(priv, mp);
154 if (unlikely(mr == NULL)) {
155 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
159 if (unlikely(idx == RTE_DIM(txq->mp2mr))) {
160 /* Table is full, remove oldest entry. */
161 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
164 priv_mr_release(priv, txq->mp2mr[0]);
165 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
166 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
168 /* Store the new entry. */
169 txq_ctrl->txq.mp2mr[idx] = mr;
170 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
171 (void *)txq_ctrl, mp->name, (void *)mp,
172 txq_ctrl->txq.mp2mr[idx]->lkey);
177 * Register a Memory Region (MR) <-> Memory Pool (MP) association in
178 * txq->mp2mr[]. If mp2mr[] is full, remove an entry first.
180 * This function should only be called by txq_mp2mr().
183 * Pointer to TX queue structure.
185 * Memory Pool for which a Memory Region lkey must be returned.
187 * Index of the next available entry.
190 * mr on success, NULL on failure.
193 mlx5_txq_mp2mr_reg(struct mlx5_txq_data *txq, struct rte_mempool *mp,
196 struct mlx5_txq_ctrl *txq_ctrl =
197 container_of(txq, struct mlx5_txq_ctrl, txq);
200 priv_lock(txq_ctrl->priv);
201 mr = priv_txq_mp2mr_reg(txq_ctrl->priv, txq, mp, idx);
202 priv_unlock(txq_ctrl->priv);
206 struct mlx5_mp2mr_mbuf_check_data {
211 * Callback function for rte_mempool_obj_iter() to check whether a given
212 * mempool object looks like a mbuf.
215 * The mempool pointer
217 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
222 * Object index, unused.
225 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
226 uint32_t index __rte_unused)
228 struct mlx5_mp2mr_mbuf_check_data *data = arg;
229 struct rte_mbuf *buf = obj;
232 * Check whether mbuf structure fits element size and whether mempool
235 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
240 * Iterator function for rte_mempool_walk() to register existing mempools and
241 * fill the MP to MR cache of a TX queue.
244 * Memory Pool to register.
246 * Pointer to TX queue structure.
249 mlx5_mp2mr_iter(struct rte_mempool *mp, void *arg)
251 struct priv *priv = (struct priv *)arg;
252 struct mlx5_mp2mr_mbuf_check_data data = {
257 /* Register mempool only if the first element looks like a mbuf. */
258 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
261 mr = priv_mr_get(priv, mp);
263 priv_mr_release(priv, mr);
266 priv_mr_new(priv, mp);
270 * Register a new memory region from the mempool and store it in the memory
274 * Pointer to private structure.
276 * Pointer to the memory pool to register.
278 * The memory region on success.
281 priv_mr_new(struct priv *priv, struct rte_mempool *mp)
283 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
289 mr = rte_zmalloc_socket(__func__, sizeof(*mr), 0, mp->socket_id);
291 DEBUG("unable to configure MR, ibv_reg_mr() failed.");
294 if (mlx5_check_mempool(mp, &start, &end) != 0) {
295 ERROR("mempool %p: not virtually contiguous",
299 DEBUG("mempool %p area start=%p end=%p size=%zu",
300 (void *)mp, (void *)start, (void *)end,
301 (size_t)(end - start));
302 /* Save original addresses for exact MR lookup. */
305 /* Round start and end to page boundary if found in memory segments. */
306 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
307 uintptr_t addr = (uintptr_t)ms[i].addr;
308 size_t len = ms[i].len;
309 unsigned int align = ms[i].hugepage_sz;
311 if ((start > addr) && (start < addr + len))
312 start = RTE_ALIGN_FLOOR(start, align);
313 if ((end > addr) && (end < addr + len))
314 end = RTE_ALIGN_CEIL(end, align);
316 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
317 (void *)mp, (void *)start, (void *)end,
318 (size_t)(end - start));
319 mr->mr = mlx5_glue->reg_mr(priv->pd, (void *)start, end - start,
320 IBV_ACCESS_LOCAL_WRITE);
322 mr->lkey = rte_cpu_to_be_32(mr->mr->lkey);
323 rte_atomic32_inc(&mr->refcnt);
324 DEBUG("%p: new Memory Region %p refcnt: %d", (void *)priv,
325 (void *)mr, rte_atomic32_read(&mr->refcnt));
326 LIST_INSERT_HEAD(&priv->mr, mr, next);
331 * Search the memory region object in the memory region list.
334 * Pointer to private structure.
336 * Pointer to the memory pool to register.
338 * The memory region on success.
341 priv_mr_get(struct priv *priv, struct rte_mempool *mp)
346 if (LIST_EMPTY(&priv->mr))
348 LIST_FOREACH(mr, &priv->mr, next) {
350 rte_atomic32_inc(&mr->refcnt);
351 DEBUG("Memory Region %p refcnt: %d",
352 (void *)mr, rte_atomic32_read(&mr->refcnt));
360 * Release the memory region object.
363 * Pointer to memory region to release.
366 * 0 on success, errno on failure.
369 priv_mr_release(struct priv *priv, struct mlx5_mr *mr)
373 DEBUG("Memory Region %p refcnt: %d",
374 (void *)mr, rte_atomic32_read(&mr->refcnt));
375 if (rte_atomic32_dec_and_test(&mr->refcnt)) {
376 claim_zero(mlx5_glue->dereg_mr(mr->mr));
377 LIST_REMOVE(mr, next);
385 * Verify the flow list is empty
388 * Pointer to private structure.
390 * @return the number of object not released.
393 priv_mr_verify(struct priv *priv)
398 LIST_FOREACH(mr, &priv->mr, next) {
399 DEBUG("%p: mr %p still referenced", (void *)priv,