1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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 16 * distribution. 17 * * Neither the name of Intel Corporation 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. 20 * 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. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "env_internal.h" 37 38 #include "spdk/version.h" 39 40 #include <rte_config.h> 41 #include <rte_eal.h> 42 43 #define SPDK_ENV_DPDK_DEFAULT_NAME "spdk" 44 #define SPDK_ENV_DPDK_DEFAULT_SHM_ID -1 45 #define SPDK_ENV_DPDK_DEFAULT_MEM_SIZE -1 46 #define SPDK_ENV_DPDK_DEFAULT_MASTER_CORE -1 47 #define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL -1 48 #define SPDK_ENV_DPDK_DEFAULT_CORE_MASK "0x1" 49 50 static char **eal_cmdline; 51 static int eal_cmdline_argcount; 52 53 static char * 54 _sprintf_alloc(const char *format, ...) 55 { 56 va_list args; 57 va_list args_copy; 58 char *buf; 59 size_t bufsize; 60 int rc; 61 62 va_start(args, format); 63 64 /* Try with a small buffer first. */ 65 bufsize = 32; 66 67 /* Limit maximum buffer size to something reasonable so we don't loop forever. */ 68 while (bufsize <= 1024 * 1024) { 69 buf = malloc(bufsize); 70 if (buf == NULL) { 71 va_end(args); 72 return NULL; 73 } 74 75 va_copy(args_copy, args); 76 rc = vsnprintf(buf, bufsize, format, args_copy); 77 va_end(args_copy); 78 79 /* 80 * If vsnprintf() returned a count within our current buffer size, we are done. 81 * The count does not include the \0 terminator, so rc == bufsize is not OK. 82 */ 83 if (rc >= 0 && (size_t)rc < bufsize) { 84 va_end(args); 85 return buf; 86 } 87 88 /* 89 * vsnprintf() should return the required space, but some libc versions do not 90 * implement this correctly, so just double the buffer size and try again. 91 * 92 * We don't need the data in buf, so rather than realloc(), use free() and malloc() 93 * again to avoid a copy. 94 */ 95 free(buf); 96 bufsize *= 2; 97 } 98 99 va_end(args); 100 return NULL; 101 } 102 103 static void 104 spdk_env_unlink_shared_files(void) 105 { 106 char buffer[PATH_MAX]; 107 108 snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_config", getpid()); 109 if (unlink(buffer)) { 110 fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno); 111 } 112 113 snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_hugepage_info", getpid()); 114 if (unlink(buffer)) { 115 fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno); 116 } 117 } 118 119 void 120 spdk_env_opts_init(struct spdk_env_opts *opts) 121 { 122 if (!opts) { 123 return; 124 } 125 126 memset(opts, 0, sizeof(*opts)); 127 128 opts->name = SPDK_ENV_DPDK_DEFAULT_NAME; 129 opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK; 130 opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID; 131 opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE; 132 opts->master_core = SPDK_ENV_DPDK_DEFAULT_MASTER_CORE; 133 opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL; 134 } 135 136 static void 137 spdk_free_args(char **args, int argcount) 138 { 139 int i; 140 141 for (i = 0; i < argcount; i++) { 142 free(args[i]); 143 } 144 145 if (argcount) { 146 free(args); 147 } 148 } 149 150 static char ** 151 spdk_push_arg(char *args[], int *argcount, char *arg) 152 { 153 char **tmp; 154 155 if (arg == NULL) { 156 fprintf(stderr, "%s: NULL arg supplied\n", __func__); 157 spdk_free_args(args, *argcount); 158 return NULL; 159 } 160 161 tmp = realloc(args, sizeof(char *) * (*argcount + 1)); 162 if (tmp == NULL) { 163 spdk_free_args(args, *argcount); 164 return NULL; 165 } 166 167 tmp[*argcount] = arg; 168 (*argcount)++; 169 170 return tmp; 171 } 172 173 static void 174 spdk_destruct_eal_cmdline(void) 175 { 176 spdk_free_args(eal_cmdline, eal_cmdline_argcount); 177 } 178 179 180 static int 181 spdk_build_eal_cmdline(const struct spdk_env_opts *opts) 182 { 183 int argcount = 0; 184 char **args; 185 186 args = NULL; 187 188 /* set the program name */ 189 args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", opts->name)); 190 if (args == NULL) { 191 return -1; 192 } 193 194 /* set the coremask */ 195 /* NOTE: If coremask starts with '[' and ends with ']' it is a core list 196 */ 197 if (opts->core_mask[0] == '[') { 198 char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1); 199 int len = strlen(l_arg); 200 if (l_arg[len - 1] == ']') { 201 l_arg[len - 1] = '\0'; 202 } 203 args = spdk_push_arg(args, &argcount, l_arg); 204 } else { 205 args = spdk_push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask)); 206 } 207 208 if (args == NULL) { 209 return -1; 210 } 211 212 /* set the memory channel number */ 213 if (opts->mem_channel > 0) { 214 args = spdk_push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel)); 215 if (args == NULL) { 216 return -1; 217 } 218 } 219 220 /* set the memory size */ 221 if (opts->mem_size > 0) { 222 args = spdk_push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size)); 223 if (args == NULL) { 224 return -1; 225 } 226 } 227 228 /* set the master core */ 229 if (opts->master_core > 0) { 230 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--master-lcore=%d", 231 opts->master_core)); 232 if (args == NULL) { 233 return -1; 234 } 235 } 236 237 /* set no pci if enabled */ 238 if (opts->no_pci) { 239 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--no-pci")); 240 if (args == NULL) { 241 return -1; 242 } 243 } 244 245 /* create just one hugetlbfs file */ 246 if (opts->hugepage_single_segments) { 247 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--single-file-segments")); 248 if (args == NULL) { 249 return -1; 250 } 251 } 252 253 if (opts->num_pci_addr) { 254 size_t i; 255 char bdf[32]; 256 struct spdk_pci_addr *pci_addr = 257 opts->pci_blacklist ? opts->pci_blacklist : opts->pci_whitelist; 258 259 for (i = 0; i < opts->num_pci_addr; i++) { 260 spdk_pci_addr_fmt(bdf, 32, &pci_addr[i]); 261 args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s %s", 262 (opts->pci_blacklist ? "-b" : "-w"), bdf)); 263 if (args == NULL) { 264 return -1; 265 } 266 } 267 } 268 269 #ifdef __linux__ 270 if (opts->shm_id < 0) { 271 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d", 272 getpid())); 273 if (args == NULL) { 274 return -1; 275 } 276 } else { 277 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d", 278 opts->shm_id)); 279 if (args == NULL) { 280 return -1; 281 } 282 283 /* Set the base virtual address - it must be an address that is not in the 284 * ASAN shadow region, otherwise ASAN-enabled builds will ignore the 285 * mmap hint. 286 * 287 * Ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm 288 */ 289 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x200000000000")); 290 if (args == NULL) { 291 return -1; 292 } 293 294 /* set the process type */ 295 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto")); 296 if (args == NULL) { 297 return -1; 298 } 299 } 300 #endif 301 302 eal_cmdline = args; 303 eal_cmdline_argcount = argcount; 304 if (atexit(spdk_destruct_eal_cmdline) != 0) { 305 fprintf(stderr, "Failed to register cleanup handler\n"); 306 } 307 308 return argcount; 309 } 310 311 int spdk_env_init(const struct spdk_env_opts *opts) 312 { 313 char **dpdk_args = NULL; 314 int i, rc; 315 int orig_optind; 316 317 rc = spdk_build_eal_cmdline(opts); 318 if (rc < 0) { 319 fprintf(stderr, "Invalid arguments to initialize DPDK\n"); 320 return -1; 321 } 322 323 printf("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version()); 324 printf("[ DPDK EAL parameters: "); 325 for (i = 0; i < eal_cmdline_argcount; i++) { 326 printf("%s ", eal_cmdline[i]); 327 } 328 printf("]\n"); 329 330 /* DPDK rearranges the array we pass to it, so make a copy 331 * before passing so we can still free the individual strings 332 * correctly. 333 */ 334 dpdk_args = calloc(eal_cmdline_argcount, sizeof(char *)); 335 if (dpdk_args == NULL) { 336 fprintf(stderr, "Failed to allocate dpdk_args\n"); 337 return -1; 338 } 339 memcpy(dpdk_args, eal_cmdline, sizeof(char *) * eal_cmdline_argcount); 340 341 fflush(stdout); 342 orig_optind = optind; 343 optind = 1; 344 rc = rte_eal_init(eal_cmdline_argcount, dpdk_args); 345 optind = orig_optind; 346 347 free(dpdk_args); 348 349 if (rc < 0) { 350 fprintf(stderr, "Failed to initialize DPDK\n"); 351 return -1; 352 } 353 354 if (opts->shm_id < 0 && !opts->hugepage_single_segments) { 355 /* 356 * Unlink hugepage and config info files after init. This will ensure they get 357 * deleted on app exit, even if the app crashes and does not exit normally. 358 * Only do this when not in multi-process mode, since for multi-process other 359 * apps will need to open these files. These files are not created for 360 * "single file segments". 361 */ 362 spdk_env_unlink_shared_files(); 363 } 364 365 if (spdk_mem_map_init() < 0) { 366 fprintf(stderr, "Failed to allocate mem_map\n"); 367 return -1; 368 } 369 if (spdk_vtophys_init() < 0) { 370 fprintf(stderr, "Failed to initialize vtophys\n"); 371 return -1; 372 } 373 374 return 0; 375 } 376