4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 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.
36 * Utility functions used by the mlx4 driver.
45 #include <rte_errno.h>
46 #include <rte_malloc.h>
47 #include <rte_memory.h>
49 #include "mlx4_utils.h"
52 * Make a file descriptor non-blocking.
55 * File descriptor to alter.
58 * 0 on success, negative errno value otherwise and rte_errno is set.
61 mlx4_fd_set_non_blocking(int fd)
63 int ret = fcntl(fd, F_GETFL);
65 if (ret != -1 && !fcntl(fd, F_SETFL, ret | O_NONBLOCK))
73 * Internal helper to allocate memory once for several disparate objects.
75 * The most restrictive alignment constraint for standard objects is assumed
76 * to be sizeof(double) and is used as a default value.
78 * C11 code would include stdalign.h and use alignof(max_align_t) however
79 * we'll stick with C99 for the time being.
82 mlx4_mallocv_inline(const char *type, const struct mlx4_malloc_vec *vec,
83 unsigned int cnt, int zero, int socket)
89 int fill = !vec[0].addr;
94 for (i = 0; i < cnt; ++i) {
95 size_t align = (uintptr_t)vec[i].align;
98 align = sizeof(double);
99 } else if (!rte_is_power_of_2(align)) {
105 align = RTE_ALIGN_CEIL(size, align);
106 size = align + vec[i].size;
107 if (fill && vec[i].addr)
108 *vec[i].addr = data + align;
113 data = rte_malloc_socket(type, size, least, socket);
115 data = rte_zmalloc_socket(type, size, least, socket);
122 for (i = 0; i != cnt; ++i)
129 * Allocate memory once for several disparate objects.
131 * This function adds iovec-like semantics (e.g. readv()) to rte_malloc().
132 * Memory is allocated once for several contiguous objects of nonuniform
133 * sizes and alignment constraints.
135 * Each entry of @p vec describes the size, alignment constraint and
136 * provides a buffer address where the resulting object pointer must be
139 * The buffer of the first entry is guaranteed to point to the beginning of
140 * the allocated region and is safe to use with rte_free().
142 * NULL buffers are silently ignored.
144 * Providing a NULL buffer in the first entry prevents this function from
145 * allocating any memory but has otherwise no effect on its behavior. In
146 * this case, the contents of remaining non-NULL buffers are updated with
147 * addresses relative to zero (i.e. offsets that would have been used during
151 * A string identifying the type of allocated objects (useful for debug
152 * purposes, such as identifying the cause of a memory leak). Can be NULL.
153 * @param[in, out] vec
154 * Description of objects to allocate memory for.
156 * Number of entries in @p vec.
159 * Size in bytes of the allocated region including any padding. In case of
160 * error, rte_errno is set, 0 is returned and NULL is stored in the
161 * non-NULL buffers pointed by @p vec.
163 * @see struct mlx4_malloc_vec
167 mlx4_mallocv(const char *type, const struct mlx4_malloc_vec *vec,
170 return mlx4_mallocv_inline(type, vec, cnt, 0, SOCKET_ID_ANY);
174 * Combines the semantics of mlx4_mallocv() with those of rte_zmalloc().
176 * @see mlx4_mallocv()
180 mlx4_zmallocv(const char *type, const struct mlx4_malloc_vec *vec,
183 return mlx4_mallocv_inline(type, vec, cnt, 1, SOCKET_ID_ANY);
187 * Socket-aware version of mlx4_mallocv().
189 * This function takes one additional parameter.
192 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this
193 * function will behave the same as mlx4_mallocv().
195 * @see mlx4_mallocv()
196 * @see rte_malloc_socket()
199 mlx4_mallocv_socket(const char *type, const struct mlx4_malloc_vec *vec,
200 unsigned int cnt, int socket)
202 return mlx4_mallocv_inline(type, vec, cnt, 0, socket);
206 * Combines the semantics of mlx4_mallocv_socket() with those of
207 * mlx4_zmalloc_socket().
209 * @see mlx4_mallocv_socket()
210 * @see rte_zmalloc_socket()
213 mlx4_zmallocv_socket(const char *type, const struct mlx4_malloc_vec *vec,
214 unsigned int cnt, int socket)
216 return mlx4_mallocv_inline(type, vec, cnt, 1, socket);