|
| 1 | +/* |
| 2 | + * crun - OCI runtime written in C |
| 3 | + * |
| 4 | + * Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <[email protected]> |
| 5 | + * crun is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2.1 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * crun is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with crun. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <config.h> |
| 20 | +#include "linux.h" |
| 21 | +#include "utils.h" |
| 22 | +#include <ocispec/runtime_spec_schema_config_schema.h> |
| 23 | + |
| 24 | +#ifdef HAVE_NUMA |
| 25 | +#include <numa.h> |
| 26 | +#include <numaif.h> |
| 27 | + |
| 28 | +#define CRUN_NUMA_API_VERSION 2 /* numa.h LIBNUMA_API_VERSION at the time of writing */ |
| 29 | +#define CRUN_NUMA_MPOL_MAX 7 /* numaif.h MPOL_MAX at the time of writing */ |
| 30 | + |
| 31 | +#ifndef LIBNUMA_API_VERSION |
| 32 | +#error "Unable to determine libnuma api version" |
| 33 | +#else |
| 34 | +#if LIBNUMA_API_VERSION != CRUN_NUMA_API_VERSION |
| 35 | +#warning "This code was written with libnuma API version 2. numa.h reports a different version" |
| 36 | +#endif |
| 37 | +#if MPOL_MAX != CRUN_NUMA_MPOL_MAX |
| 38 | +#warning "This code was written with MPOL_MAX = 7. numaif.h reports a different value" |
| 39 | +#endif |
| 40 | +#endif |
| 41 | + |
| 42 | +typedef struct { |
| 43 | + const char *name; |
| 44 | + int value; |
| 45 | +} str2int_map_t; |
| 46 | + |
| 47 | +/* update mpol_mode_map based on numaif.h MPOL_MAX |
| 48 | + * the warn above will indicate that an update is required. */ |
| 49 | +static str2int_map_t mpol_mode_map[] = { |
| 50 | + { "MpolDefault", MPOL_DEFAULT }, |
| 51 | + { "MpolPreferred", MPOL_PREFERRED }, |
| 52 | + { "MpolBind", MPOL_BIND }, |
| 53 | + { "MpolInterleave", MPOL_INTERLEAVE }, |
| 54 | + { "MpolLocal", MPOL_LOCAL }, |
| 55 | + { "MpolPreferredMany", MPOL_PREFERRED_MANY }, |
| 56 | + { "MpolWeightedInterleave", MPOL_WEIGHTED_INTERLEAVE }, |
| 57 | + { NULL, -1 } |
| 58 | +}; |
| 59 | + |
| 60 | +/* flags cannot be tracked the same way as mode */ |
| 61 | +static str2int_map_t mpol_flag_map[] = { |
| 62 | + { "MpolFNumaBalancing", MPOL_F_NUMA_BALANCING }, |
| 63 | + { "MpolFRelativeNodes", MPOL_F_RELATIVE_NODES }, |
| 64 | + { "MpolFStaticNodes", MPOL_F_STATIC_NODES }, |
| 65 | + { NULL, -1 } |
| 66 | +}; |
| 67 | + |
| 68 | +static int mpol_str2int(const char *mode, const str2int_map_t *map) |
| 69 | +{ |
| 70 | + int idx = 0; |
| 71 | + |
| 72 | + while (map[idx].name != NULL) { |
| 73 | + if (!strcmp(map[idx].name, mode)) { |
| 74 | + return map[idx].value; |
| 75 | + } |
| 76 | + idx++; |
| 77 | + } |
| 78 | + |
| 79 | + errno = EINVAL; |
| 80 | + return -1; |
| 81 | +} |
| 82 | +#endif |
| 83 | + |
| 84 | +int |
| 85 | +libcrun_set_mempolicy (runtime_spec_schema_config_schema *def, libcrun_error_t *err) |
| 86 | +{ |
| 87 | +#ifdef HAVE_NUMA |
| 88 | + runtime_spec_schema_config_linux_memory_policy *memory_policy = NULL; |
| 89 | + int mpol_mode = MPOL_DEFAULT; |
| 90 | + int mpol_flags = 0; |
| 91 | + struct bitmask *nodemask = NULL; |
| 92 | + |
| 93 | + libcrun_debug("Initializing linux numa mempolicy"); |
| 94 | + |
| 95 | + if (def->linux && def->linux->memory_policy) { |
| 96 | + memory_policy = def->linux->memory_policy; |
| 97 | + |
| 98 | + libcrun_debug("Validating linux numa mempolicy"); |
| 99 | + |
| 100 | + /* validate memory policy mode */ |
| 101 | + if (!memory_policy->mode) { |
| 102 | + return crun_make_error(err, EINVAL, "linux numa mempolicy mode is missing from the configuration"); |
| 103 | + } |
| 104 | + libcrun_debug("Validating mode: %s", memory_policy->mode); |
| 105 | + mpol_mode = mpol_str2int(memory_policy->mode, mpol_mode_map); |
| 106 | + if (mpol_mode < 0) { |
| 107 | + return crun_make_error(err, EINVAL, "Requested linux numa mempolicy mode '%s' is unknown", memory_policy->mode); |
| 108 | + } |
| 109 | + |
| 110 | + /* validate memory nodes */ |
| 111 | + if (!memory_policy->nodes) { |
| 112 | + return crun_make_error(err, EINVAL, "linux numa mempolicy nodes is missing from the configuration"); |
| 113 | + } |
| 114 | + libcrun_debug("Validating nodes: %s", memory_policy->nodes); |
| 115 | + /* validation is done by libnuma based on hw environment |
| 116 | + * and numa_warn symbol is overridden in error.c to access |
| 117 | + * write_log */ |
| 118 | + nodemask = numa_parse_nodestring_all(memory_policy->nodes); |
| 119 | + if (!nodemask) { |
| 120 | + return crun_make_error(err, EINVAL, "numa_parse_nodestring_all validation failed"); |
| 121 | + } |
| 122 | + |
| 123 | + libcrun_debug("Checking hardware numa availability"); |
| 124 | + if (numa_available() < 0) { |
| 125 | + return crun_make_error(err, ENOENT, "linux numa not supported on current hardware"); |
| 126 | + } |
| 127 | + } else { |
| 128 | + libcrun_debug("no linux numa mempolicy configuration found"); |
| 129 | + } |
| 130 | +#endif |
| 131 | + return 0; |
| 132 | +} |
0 commit comments