1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/*
* BSD LICENSE
*
* Copyright(c) 2019-2023 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.O
*
*/
#include "common.h"
#include "log.h"
#include "pqos.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <unistd.h>
/* Maximum required file descriptors per core */
#define MAX_FD_PER_CORE 5
/* pqos tool opens some file descriptors while using msr interface */
#define MAX_PQOS_FD 100
FILE *
pqos_fopen(const char *name, const char *mode)
{
int fd;
FILE *stream = NULL;
struct stat lstat_val;
struct stat fstat_val;
/* collect any link info about the file */
/* coverity[fs_check_call] */
if (lstat(name, &lstat_val) == -1)
return NULL;
stream = fopen(name, mode);
if (stream == NULL)
return stream;
fd = fileno(stream);
if (fd == -1)
goto pqos_fopen_error;
/* collect info about the opened file */
if (fstat(fd, &fstat_val) == -1)
goto pqos_fopen_error;
/* we should not have followed a symbolic link */
if (lstat_val.st_mode != fstat_val.st_mode ||
lstat_val.st_ino != fstat_val.st_ino ||
lstat_val.st_dev != fstat_val.st_dev) {
LOG_ERROR("File %s is a symlink\n", name);
goto pqos_fopen_error;
}
return stream;
pqos_fopen_error:
if (stream != NULL)
fclose(stream);
return NULL;
}
int
pqos_fclose(FILE *stream)
{
return fclose(stream);
}
int
pqos_open(const char *pathname, int flags)
{
int fd;
struct stat lstat_val;
struct stat fstat_val;
/* collect any link info about the file */
/* coverity[fs_check_call] */
if (lstat(pathname, &lstat_val) == -1)
return -1;
/* open the file */
fd = open(pathname, flags);
if (fd == -1)
return -1;
/* collect info about the opened file */
if (fstat(fd, &fstat_val) == -1) {
close(fd);
return -1;
}
/* we should not have followed a symbolic link */
if (lstat_val.st_mode != fstat_val.st_mode ||
lstat_val.st_ino != fstat_val.st_ino ||
lstat_val.st_dev != fstat_val.st_dev) {
printf("File %s is a symlink\n", pathname);
close(fd);
return -1;
}
return fd;
}
char *
pqos_strcat(char *dst, const char *src, size_t size)
{
return strncat(dst, src, size - strnlen(dst, size));
}
char *
pqos_fgets(char *s, int n, FILE *stream)
{
char *line = NULL;
size_t line_len = 0;
ssize_t line_read;
ssize_t i;
line_read = getline(&line, &line_len, stream);
if (line_read != -1) {
char *p = strchr(line, '\n');
if (p == NULL)
goto pqos_fgets_error;
*p = '\0';
if ((ssize_t)strlen(line) != line_read - 1 || line_read > n)
goto pqos_fgets_error;
for (i = 0; i < line_read - 1; ++i)
if (!isascii(line[i]))
goto pqos_fgets_error;
strncpy(s, line, n - 1);
s[n - 1] = '\0';
free(line);
return s;
}
pqos_fgets_error:
if (line != NULL)
free(line);
return NULL;
}
/**
* @brief Read integer from file
*
* @param [in] path file path
* @param [out] value parsed value
*
* @return Operations status
* @retval PQOS_RETVAL_OK on success
*/
int
pqos_fread_uint(const char *path, unsigned *value)
{
int ret = PQOS_RETVAL_OK;
FILE *fd;
fd = pqos_fopen(path, "r");
if (fd != NULL) {
if (fscanf(fd, "%u", value) != 1)
ret = PQOS_RETVAL_ERROR;
pqos_fclose(fd);
} else
return PQOS_RETVAL_RESOURCE;
return ret;
}
int
pqos_fread_uint64(const char *fname, unsigned base, uint64_t *value)
{
FILE *fd;
char buf[17] = "\0";
char *s = buf;
char *endptr = NULL;
size_t bytes;
unsigned long long val;
ASSERT(fname != NULL);
ASSERT(value != NULL);
fd = pqos_fopen(fname, "r");
if (fd == NULL)
return PQOS_RETVAL_ERROR;
bytes = fread(buf, sizeof(buf) - 1, 1, fd);
if (bytes == 0 && !feof(fd)) {
pqos_fclose(fd);
return PQOS_RETVAL_ERROR;
}
pqos_fclose(fd);
val = strtoull(s, &endptr, base);
if (!((*s != '\0' && *s != '\n') &&
(*endptr == '\0' || *endptr == '\n'))) {
LOG_ERROR("Error converting '%s' to unsigned number!\n", buf);
return PQOS_RETVAL_ERROR;
}
if (val < UINT64_MAX) {
*value = val;
return PQOS_RETVAL_OK;
}
return PQOS_RETVAL_ERROR;
}
int
pqos_file_exists(const char *path)
{
return access(path, F_OK) == 0;
}
/**
* @brief Checks if directory exists
*
* @param [in] path file path
*
* @return If file exists
* @retval 1 if file exists
* @retval 0 if file not exists
*/
int
pqos_dir_exists(const char *path)
{
struct stat st;
return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
}
int
pqos_file_contains(const char *fname, const char *str, int *found)
{
FILE *fd;
char temp[1024];
int check_symlink = 1;
if (fname == NULL || str == NULL || found == NULL)
return PQOS_RETVAL_PARAM;
if (strncmp(fname, "/proc/", 6) == 0)
check_symlink = 0;
if (check_symlink)
fd = pqos_fopen(fname, "r");
else
fd = fopen(fname, "r");
if (fd == NULL) {
LOG_DEBUG("%s not found.\n", fname);
*found = 0;
return PQOS_RETVAL_OK;
}
*found = 0;
while (fgets(temp, sizeof(temp), fd) != NULL) {
if (strstr(temp, str) != NULL) {
*found = 1;
break;
}
}
fclose(fd);
return PQOS_RETVAL_OK;
}
#define DEV_MEM "/dev/mem"
uint8_t *
pqos_mmap_read(uint64_t address, const uint64_t size)
{
uint32_t offset;
uint64_t page_size;
uint8_t *mem;
int fd;
fd = pqos_open(DEV_MEM, O_RDONLY);
if (fd < 0) {
LOG_ERROR("Could not open %s\n", DEV_MEM);
return NULL;
}
page_size = sysconf(_SC_PAGESIZE);
offset = address % page_size;
mem = mmap(NULL, size + offset, PROT_READ, MAP_PRIVATE, fd,
address - offset);
if (mem == MAP_FAILED) {
LOG_ERROR("Memory map failed, address=%llx size=%llu\n",
(unsigned long long)address,
(unsigned long long)size);
close(fd);
return NULL;
}
close(fd);
return mem + offset;
}
uint8_t *
pqos_mmap_write(uint64_t address, const uint64_t size)
{
uint32_t offset;
uint64_t page_size;
uint8_t *mem;
int fd;
fd = pqos_open(DEV_MEM, O_RDWR);
if (fd < 0) {
LOG_ERROR("Could not open %s\n", DEV_MEM);
return NULL;
}
page_size = sysconf(_SC_PAGESIZE);
offset = address % page_size;
mem = mmap(NULL, size + offset, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
address - offset);
if (mem == MAP_FAILED) {
LOG_ERROR("Memory map failed, address=%llx size=%llu\n",
(unsigned long long)address,
(unsigned long long)size);
close(fd);
return NULL;
}
close(fd);
return mem + offset;
}
void
pqos_munmap(void *mem, const uint64_t size)
{
uint64_t offset;
uint64_t page_size;
page_size = sysconf(_SC_PAGESIZE);
offset = (uint64_t)mem % page_size;
munmap((uint8_t *)mem - offset, size + offset);
}
ssize_t
pqos_read(int fd, void *buf, size_t count)
{
size_t len = count;
char *byte_ptr = (char *)buf;
ssize_t ret;
if (buf == NULL) {
errno = EFAULT;
return -1;
}
while (len != 0 && (ret = read(fd, byte_ptr, len)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
return ret;
}
len -= ret;
byte_ptr += ret;
}
return count;
}
int
pqos_set_no_files_limit(unsigned long max_core_count)
{
struct rlimit files_limit;
const rlim_t required_fd =
(max_core_count * MAX_FD_PER_CORE) + MAX_PQOS_FD;
if (getrlimit(RLIMIT_NOFILE, &files_limit))
return PQOS_RETVAL_ERROR;
/* Check Kernel allows to open required file descriptors */
if (files_limit.rlim_max < required_fd ||
files_limit.rlim_cur < required_fd) {
if (files_limit.rlim_max < required_fd)
files_limit.rlim_max = required_fd;
files_limit.rlim_cur = required_fd;
if (setrlimit(RLIMIT_NOFILE, &files_limit))
return PQOS_RETVAL_ERROR;
}
return PQOS_RETVAL_OK;
}
|