1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2017 6WIND S.A. 3 * Copyright 2017 Mellanox Technologies, Ltd 4 */ 5 6 /** 7 * @file 8 * Utility functions used by the mlx4 driver. 9 */ 10 11 #include <assert.h> 12 #include <errno.h> 13 #include <fcntl.h> 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <rte_errno.h> 18 #include <rte_malloc.h> 19 #include <rte_memory.h> 20 21 #include "mlx4_utils.h" 22 23 /** 24 * Make a file descriptor non-blocking. 25 * 26 * @param fd 27 * File descriptor to alter. 28 * 29 * @return 30 * 0 on success, negative errno value otherwise and rte_errno is set. 31 */ 32 int 33 mlx4_fd_set_non_blocking(int fd) 34 { 35 int ret = fcntl(fd, F_GETFL); 36 37 if (ret != -1 && !fcntl(fd, F_SETFL, ret | O_NONBLOCK)) 38 return 0; 39 assert(errno); 40 rte_errno = errno; 41 return -rte_errno; 42 } 43 44 /** 45 * Internal helper to allocate memory once for several disparate objects. 46 * 47 * The most restrictive alignment constraint for standard objects is assumed 48 * to be sizeof(double) and is used as a default value. 49 * 50 * C11 code would include stdalign.h and use alignof(max_align_t) however 51 * we'll stick with C99 for the time being. 52 */ 53 static inline size_t 54 mlx4_mallocv_inline(const char *type, const struct mlx4_malloc_vec *vec, 55 unsigned int cnt, int zero, int socket) 56 { 57 unsigned int i; 58 size_t size; 59 size_t least; 60 uint8_t *data = NULL; 61 int fill = !vec[0].addr; 62 63 fill: 64 size = 0; 65 least = 0; 66 for (i = 0; i < cnt; ++i) { 67 size_t align = (uintptr_t)vec[i].align; 68 69 if (!align) { 70 align = sizeof(double); 71 } else if (!rte_is_power_of_2(align)) { 72 rte_errno = EINVAL; 73 goto error; 74 } 75 if (least < align) 76 least = align; 77 align = RTE_ALIGN_CEIL(size, align); 78 size = align + vec[i].size; 79 if (fill && vec[i].addr) 80 *vec[i].addr = data + align; 81 } 82 if (fill) 83 return size; 84 if (!zero) 85 data = rte_malloc_socket(type, size, least, socket); 86 else 87 data = rte_zmalloc_socket(type, size, least, socket); 88 if (data) { 89 fill = 1; 90 goto fill; 91 } 92 rte_errno = ENOMEM; 93 error: 94 for (i = 0; i != cnt; ++i) 95 if (vec[i].addr) 96 *vec[i].addr = NULL; 97 return 0; 98 } 99 100 /** 101 * Allocate memory once for several disparate objects. 102 * 103 * This function adds iovec-like semantics (e.g. readv()) to rte_malloc(). 104 * Memory is allocated once for several contiguous objects of nonuniform 105 * sizes and alignment constraints. 106 * 107 * Each entry of @p vec describes the size, alignment constraint and 108 * provides a buffer address where the resulting object pointer must be 109 * stored. 110 * 111 * The buffer of the first entry is guaranteed to point to the beginning of 112 * the allocated region and is safe to use with rte_free(). 113 * 114 * NULL buffers are silently ignored. 115 * 116 * Providing a NULL buffer in the first entry prevents this function from 117 * allocating any memory but has otherwise no effect on its behavior. In 118 * this case, the contents of remaining non-NULL buffers are updated with 119 * addresses relative to zero (i.e. offsets that would have been used during 120 * the allocation). 121 * 122 * @param[in] type 123 * A string identifying the type of allocated objects (useful for debug 124 * purposes, such as identifying the cause of a memory leak). Can be NULL. 125 * @param[in, out] vec 126 * Description of objects to allocate memory for. 127 * @param cnt 128 * Number of entries in @p vec. 129 * 130 * @return 131 * Size in bytes of the allocated region including any padding. In case of 132 * error, rte_errno is set, 0 is returned and NULL is stored in the 133 * non-NULL buffers pointed by @p vec. 134 * 135 * @see struct mlx4_malloc_vec 136 * @see rte_malloc() 137 */ 138 size_t 139 mlx4_mallocv(const char *type, const struct mlx4_malloc_vec *vec, 140 unsigned int cnt) 141 { 142 return mlx4_mallocv_inline(type, vec, cnt, 0, SOCKET_ID_ANY); 143 } 144 145 /** 146 * Combines the semantics of mlx4_mallocv() with those of rte_zmalloc(). 147 * 148 * @see mlx4_mallocv() 149 * @see rte_zmalloc() 150 */ 151 size_t 152 mlx4_zmallocv(const char *type, const struct mlx4_malloc_vec *vec, 153 unsigned int cnt) 154 { 155 return mlx4_mallocv_inline(type, vec, cnt, 1, SOCKET_ID_ANY); 156 } 157 158 /** 159 * Socket-aware version of mlx4_mallocv(). 160 * 161 * This function takes one additional parameter. 162 * 163 * @param socket 164 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this 165 * function will behave the same as mlx4_mallocv(). 166 * 167 * @see mlx4_mallocv() 168 * @see rte_malloc_socket() 169 */ 170 size_t 171 mlx4_mallocv_socket(const char *type, const struct mlx4_malloc_vec *vec, 172 unsigned int cnt, int socket) 173 { 174 return mlx4_mallocv_inline(type, vec, cnt, 0, socket); 175 } 176 177 /** 178 * Combines the semantics of mlx4_mallocv_socket() with those of 179 * mlx4_zmalloc_socket(). 180 * 181 * @see mlx4_mallocv_socket() 182 * @see rte_zmalloc_socket() 183 */ 184 size_t 185 mlx4_zmallocv_socket(const char *type, const struct mlx4_malloc_vec *vec, 186 unsigned int cnt, int socket) 187 { 188 return mlx4_mallocv_inline(type, vec, cnt, 1, socket); 189 } 190