| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/netdevice.h> |
| 4 |
#include <net/if.h> |
| 5 |
#include <net/if_arp.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-device.h" |
| 9 |
#include "sd-netlink.h" |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "arphrd-util.h" |
| 13 |
#include "condition.h" |
| 14 |
#include "conf-files.h" |
| 15 |
#include "conf-parser.h" |
| 16 |
#include "creds-util.h" |
| 17 |
#include "device-private.h" |
| 18 |
#include "device-util.h" |
| 19 |
#include "dirent-util.h" |
| 20 |
#include "escape.h" |
| 21 |
#include "ether-addr-util.h" |
| 22 |
#include "ethtool-util.h" |
| 23 |
#include "extract-word.h" |
| 24 |
#include "fd-util.h" |
| 25 |
#include "fileio.h" |
| 26 |
#include "hashmap.h" |
| 27 |
#include "link-config.h" |
| 28 |
#include "log-link.h" |
| 29 |
#include "memory-util.h" |
| 30 |
#include "net-condition.h" |
| 31 |
#include "netif-naming-scheme.h" |
| 32 |
#include "netif-sriov.h" |
| 33 |
#include "netif-util.h" |
| 34 |
#include "netlink-util.h" |
| 35 |
#include "network-util.h" |
| 36 |
#include "numa-util.h" |
| 37 |
#include "parse-util.h" |
| 38 |
#include "path-util.h" |
| 39 |
#include "proc-cmdline.h" |
| 40 |
#include "random-util.h" |
| 41 |
#include "socket-util.h" |
| 42 |
#include "sort-util.h" |
| 43 |
#include "specifier.h" |
| 44 |
#include "stat-util.h" |
| 45 |
#include "stdio-util.h" |
| 46 |
#include "string-table.h" |
| 47 |
#include "string-util.h" |
| 48 |
#include "strv.h" |
| 49 |
#include "udev-builtin.h" |
| 50 |
#include "utf8.h" |
| 51 |
|
| 52 |
static const Specifier link_specifier_table[] = { |
| 53 |
COMMON_SYSTEM_SPECIFIERS, |
| 54 |
COMMON_TMP_SPECIFIERS, |
| 55 |
{} |
| 56 |
}; |
| 57 |
|
| 58 |
struct LinkConfigContext { |
| 59 |
LIST_HEAD(LinkConfig, configs); |
| 60 |
int ethtool_fd; |
| 61 |
Hashmap *stats_by_path; |
| 62 |
}; |
| 63 |
|
| 64 |
static LinkConfig* link_config_free(LinkConfig *config) { |
| 65 |
if (!config) |
| 66 |
return NULL; |
| 67 |
|
| 68 |
free(config->filename); |
| 69 |
strv_free(config->dropins); |
| 70 |
|
| 71 |
net_match_clear(&config->match); |
| 72 |
condition_free_list(config->conditions); |
| 73 |
|
| 74 |
free(config->description); |
| 75 |
strv_free(config->properties); |
| 76 |
strv_free(config->import_properties); |
| 77 |
strv_free(config->unset_properties); |
| 78 |
free(config->name_policy); |
| 79 |
free(config->name); |
| 80 |
strv_free(config->alternative_names); |
| 81 |
free(config->alternative_names_policy); |
| 82 |
free(config->alias); |
| 83 |
free(config->wol_password_file); |
| 84 |
erase_and_free(config->wol_password); |
| 85 |
cpu_set_done(&config->rps_cpu_mask); |
| 86 |
cpu_set_done(&config->irq_affinity_cpus); |
| 87 |
|
| 88 |
ordered_hashmap_free(config->sr_iov_by_section); |
| 89 |
|
| 90 |
return mfree(config); |
| 91 |
} |
| 92 |
|
| 93 |
DEFINE_TRIVIAL_CLEANUP_FUNC(LinkConfig*, link_config_free); |
| 94 |
|
| 95 |
static void link_configs_free(LinkConfigContext *ctx) { |
| 96 |
if (!ctx) |
| 97 |
return; |
| 98 |
|
| 99 |
ctx->stats_by_path = hashmap_free(ctx->stats_by_path); |
| 100 |
|
| 101 |
LIST_FOREACH(configs, config, ctx->configs) |
| 102 |
link_config_free(config); |
| 103 |
} |
| 104 |
|
| 105 |
LinkConfigContext *link_config_ctx_free(LinkConfigContext *ctx) { |
| 106 |
if (!ctx) |
| 107 |
return NULL; |
| 108 |
|
| 109 |
safe_close(ctx->ethtool_fd); |
| 110 |
link_configs_free(ctx); |
| 111 |
return mfree(ctx); |
| 112 |
} |
| 113 |
|
| 114 |
int link_config_ctx_new(LinkConfigContext **ret) { |
| 115 |
_cleanup_(link_config_ctx_freep) LinkConfigContext *ctx = NULL; |
| 116 |
|
| 117 |
if (!ret) |
| 118 |
return -EINVAL; |
| 119 |
|
| 120 |
ctx = new(LinkConfigContext, 1); |
| 121 |
if (!ctx) |
| 122 |
return -ENOMEM; |
| 123 |
|
| 124 |
*ctx = (LinkConfigContext) { |
| 125 |
.ethtool_fd = -EBADF, |
| 126 |
}; |
| 127 |
|
| 128 |
*ret = TAKE_PTR(ctx); |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
static int link_parse_wol_password(LinkConfig *config, const char *str) { |
| 134 |
_cleanup_(erase_and_freep) uint8_t *p = NULL; |
| 135 |
int r; |
| 136 |
|
| 137 |
assert(config); |
| 138 |
assert(str); |
| 139 |
|
| 140 |
assert_cc(sizeof(struct ether_addr) == SOPASS_MAX); |
| 141 |
|
| 142 |
p = new(uint8_t, SOPASS_MAX); |
| 143 |
if (!p) |
| 144 |
return -ENOMEM; |
| 145 |
|
| 146 |
/* Reuse parse_ether_addr(), as their formats are equivalent. */ |
| 147 |
r = parse_ether_addr(str, (struct ether_addr*) p); |
| 148 |
if (r < 0) |
| 149 |
return r; |
| 150 |
|
| 151 |
erase_and_free(config->wol_password); |
| 152 |
config->wol_password = TAKE_PTR(p); |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
static int link_read_wol_password_from_file(LinkConfig *config) { |
| 157 |
_cleanup_(erase_and_freep) char *password = NULL; |
| 158 |
int r; |
| 159 |
|
| 160 |
assert(config); |
| 161 |
|
| 162 |
if (!config->wol_password_file) |
| 163 |
return 0; |
| 164 |
|
| 165 |
r = read_full_file_full( |
| 166 |
AT_FDCWD, config->wol_password_file, UINT64_MAX, SIZE_MAX, |
| 167 |
READ_FULL_FILE_SECURE | READ_FULL_FILE_WARN_WORLD_READABLE | READ_FULL_FILE_CONNECT_SOCKET, |
| 168 |
NULL, &password, NULL); |
| 169 |
if (r < 0) |
| 170 |
return r; |
| 171 |
|
| 172 |
return link_parse_wol_password(config, password); |
| 173 |
} |
| 174 |
|
| 175 |
static int link_read_wol_password_from_cred(LinkConfig *config) { |
| 176 |
_cleanup_free_ char *base = NULL, *cred_name = NULL; |
| 177 |
_cleanup_(erase_and_freep) char *password = NULL; |
| 178 |
int r; |
| 179 |
|
| 180 |
assert(config); |
| 181 |
assert(config->filename); |
| 182 |
|
| 183 |
if (config->wol == UINT32_MAX) |
| 184 |
return 0; /* WakeOnLan= is not specified. */ |
| 185 |
if (!FLAGS_SET(config->wol, WAKE_MAGICSECURE)) |
| 186 |
return 0; /* secureon is not specified in WakeOnLan=. */ |
| 187 |
if (config->wol_password) |
| 188 |
return 0; /* WakeOnLanPassword= is specified. */ |
| 189 |
if (config->wol_password_file) |
| 190 |
return 0; /* a file name is specified in WakeOnLanPassword=, but failed to read it. */ |
| 191 |
|
| 192 |
r = path_extract_filename(config->filename, &base); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
cred_name = strjoin(base, ".wol.password"); |
| 197 |
if (!cred_name) |
| 198 |
return -ENOMEM; |
| 199 |
|
| 200 |
r = read_credential(cred_name, (void**) &password, NULL); |
| 201 |
if (r == -ENOENT) |
| 202 |
r = read_credential("wol.password", (void**) &password, NULL); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
|
| 206 |
return link_parse_wol_password(config, password); |
| 207 |
} |
| 208 |
|
| 209 |
static int link_adjust_wol_options(LinkConfig *config) { |
| 210 |
int r; |
| 211 |
|
| 212 |
assert(config); |
| 213 |
|
| 214 |
r = link_read_wol_password_from_file(config); |
| 215 |
if (r == -ENOMEM) |
| 216 |
return log_oom(); |
| 217 |
if (r < 0) |
| 218 |
log_warning_errno(r, "Failed to read WakeOnLan password from %s, ignoring: %m", config->wol_password_file); |
| 219 |
|
| 220 |
r = link_read_wol_password_from_cred(config); |
| 221 |
if (r == -ENOMEM) |
| 222 |
return log_oom(); |
| 223 |
if (r < 0) |
| 224 |
log_warning_errno(r, "Failed to read WakeOnLan password from credential, ignoring: %m"); |
| 225 |
|
| 226 |
if (config->wol != UINT32_MAX && config->wol_password) |
| 227 |
/* Enable WAKE_MAGICSECURE flag when WakeOnLanPassword=. Note that when |
| 228 |
* WakeOnLanPassword= is set without WakeOnLan=, then ethtool_set_wol() enables |
| 229 |
* WAKE_MAGICSECURE flag and other flags are not changed. */ |
| 230 |
config->wol |= WAKE_MAGICSECURE; |
| 231 |
|
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
int link_load_one(LinkConfigContext *ctx, const char *filename) { |
| 236 |
_cleanup_(link_config_freep) LinkConfig *config = NULL; |
| 237 |
_cleanup_hashmap_free_ Hashmap *stats_by_path = NULL; |
| 238 |
_cleanup_free_ char *name = NULL, *file_basename = NULL; |
| 239 |
const char *dropin_dirname; |
| 240 |
int r; |
| 241 |
|
| 242 |
assert(ctx); |
| 243 |
assert(filename); |
| 244 |
|
| 245 |
r = null_or_empty_path(filename); |
| 246 |
if (r < 0) |
| 247 |
return log_warning_errno(r, "Failed to check if \"%s\" is empty: %m", filename); |
| 248 |
if (r > 0) { |
| 249 |
log_debug("Skipping empty file: %s", filename); |
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
name = strdup(filename); |
| 254 |
if (!name) |
| 255 |
return log_oom(); |
| 256 |
|
| 257 |
config = new(LinkConfig, 1); |
| 258 |
if (!config) |
| 259 |
return log_oom(); |
| 260 |
|
| 261 |
*config = (LinkConfig) { |
| 262 |
.filename = TAKE_PTR(name), |
| 263 |
.mac_address_policy = MAC_ADDRESS_POLICY_NONE, |
| 264 |
.wol = UINT32_MAX, /* UINT32_MAX means do not change WOL setting. */ |
| 265 |
.duplex = _DUP_INVALID, |
| 266 |
.port = _NET_DEV_PORT_INVALID, |
| 267 |
.autonegotiation = -1, |
| 268 |
.rx_flow_control = -1, |
| 269 |
.tx_flow_control = -1, |
| 270 |
.autoneg_flow_control = -1, |
| 271 |
.txqueuelen = UINT32_MAX, |
| 272 |
.coalesce.use_adaptive_rx_coalesce = -1, |
| 273 |
.coalesce.use_adaptive_tx_coalesce = -1, |
| 274 |
.mdi = ETH_TP_MDI_INVALID, |
| 275 |
.sr_iov_num_vfs = UINT32_MAX, |
| 276 |
.eee_enabled = -1, |
| 277 |
.eee_tx_lpi_enabled = -1, |
| 278 |
.eee_tx_lpi_timer_usec = USEC_INFINITY, |
| 279 |
.irq_affinity_policy = _IRQ_AFFINITY_POLICY_INVALID, |
| 280 |
.irq_affinity_numa = IRQ_AFFINITY_NUMA_UNSET, |
| 281 |
}; |
| 282 |
|
| 283 |
FOREACH_ELEMENT(feature, config->features) |
| 284 |
*feature = -1; |
| 285 |
|
| 286 |
r = path_extract_filename(filename, &file_basename); |
| 287 |
if (r < 0) |
| 288 |
return log_error_errno(r, "Failed to extract file name of '%s': %m", filename); |
| 289 |
|
| 290 |
dropin_dirname = strjoina(file_basename, ".d"); |
| 291 |
r = config_parse_many_full( |
| 292 |
STRV_MAKE_CONST(filename), |
| 293 |
NETWORK_DIRS, |
| 294 |
dropin_dirname, |
| 295 |
/* root= */ NULL, |
| 296 |
/* root_fd= */ -EBADF, |
| 297 |
"Match\0" |
| 298 |
"Link\0" |
| 299 |
"SR-IOV\0" |
| 300 |
"EnergyEfficientEthernet\0", |
| 301 |
config_item_perf_lookup, link_config_gperf_lookup, |
| 302 |
CONFIG_PARSE_WARN, |
| 303 |
config, |
| 304 |
&stats_by_path, |
| 305 |
&config->dropins); |
| 306 |
if (r < 0) |
| 307 |
return r; /* config_parse_many() logs internally. */ |
| 308 |
|
| 309 |
if (ctx->stats_by_path) { |
| 310 |
r = hashmap_move(ctx->stats_by_path, stats_by_path); |
| 311 |
if (r < 0) |
| 312 |
log_warning_errno(r, "Failed to save stats of '%s' and its drop-in configs, ignoring: %m", filename); |
| 313 |
} else |
| 314 |
ctx->stats_by_path = TAKE_PTR(stats_by_path); |
| 315 |
|
| 316 |
if (net_match_is_empty(&config->match) && !config->conditions) { |
| 317 |
log_warning("%s: No valid settings found in the [Match] section, ignoring file. " |
| 318 |
"To match all interfaces, add OriginalName=* in the [Match] section.", |
| 319 |
filename); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
if (!condition_test_list(config->conditions, environ, NULL, NULL, NULL)) { |
| 324 |
log_debug("%s: Conditions do not match the system environment, skipping.", filename); |
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
if (IN_SET(config->mac_address_policy, MAC_ADDRESS_POLICY_PERSISTENT, MAC_ADDRESS_POLICY_RANDOM) && |
| 329 |
config->hw_addr.length > 0) |
| 330 |
log_warning("%s: MACAddress= in [Link] section will be ignored when MACAddressPolicy= " |
| 331 |
"is set to \"persistent\" or \"random\".", |
| 332 |
filename); |
| 333 |
|
| 334 |
r = link_adjust_wol_options(config); |
| 335 |
if (r < 0) |
| 336 |
return r; /* link_adjust_wol_options() logs internally. */ |
| 337 |
|
| 338 |
r = sr_iov_drop_invalid_sections(config->sr_iov_num_vfs, config->sr_iov_by_section); |
| 339 |
if (r < 0) |
| 340 |
return r; /* sr_iov_drop_invalid_sections() logs internally. */ |
| 341 |
|
| 342 |
log_debug("Parsed configuration file \"%s\"", filename); |
| 343 |
|
| 344 |
LIST_PREPEND(configs, ctx->configs, TAKE_PTR(config)); |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
int link_config_load(LinkConfigContext *ctx) { |
| 349 |
_cleanup_strv_free_ char **files = NULL; |
| 350 |
int r; |
| 351 |
|
| 352 |
assert(ctx); |
| 353 |
|
| 354 |
link_configs_free(ctx); |
| 355 |
|
| 356 |
r = conf_files_list_strv(&files, ".link", /* root= */ NULL, CONF_FILES_WARN, NETWORK_DIRS); |
| 357 |
if (r < 0) |
| 358 |
return log_error_errno(r, "failed to enumerate link files: %m"); |
| 359 |
|
| 360 |
STRV_FOREACH_BACKWARDS(f, files) |
| 361 |
(void) link_load_one(ctx, *f); |
| 362 |
|
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
bool link_config_should_reload(LinkConfigContext *ctx) { |
| 367 |
_cleanup_hashmap_free_ Hashmap *stats_by_path = NULL; |
| 368 |
int r; |
| 369 |
|
| 370 |
assert(ctx); |
| 371 |
|
| 372 |
r = config_get_stats_by_path(".link", NULL, 0, NETWORK_DIRS, /* check_dropins= */ true, &stats_by_path); |
| 373 |
if (r < 0) { |
| 374 |
log_warning_errno(r, "Failed to get stats of .link files, ignoring: %m"); |
| 375 |
return true; |
| 376 |
} |
| 377 |
|
| 378 |
return !stats_by_path_equal(ctx->stats_by_path, stats_by_path); |
| 379 |
} |
| 380 |
|
| 381 |
Link* link_free(Link *link) { |
| 382 |
if (!link) |
| 383 |
return NULL; |
| 384 |
|
| 385 |
udev_event_unref(link->event); |
| 386 |
free(link->kind); |
| 387 |
return mfree(link); |
| 388 |
} |
| 389 |
|
| 390 |
int link_new(LinkConfigContext *ctx, UdevEvent *event, Link **ret) { |
| 391 |
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); |
| 392 |
_cleanup_(link_freep) Link *link = NULL; |
| 393 |
int r; |
| 394 |
|
| 395 |
assert(ctx); |
| 396 |
assert(event); |
| 397 |
assert(ret); |
| 398 |
|
| 399 |
link = new(Link, 1); |
| 400 |
if (!link) |
| 401 |
return -ENOMEM; |
| 402 |
|
| 403 |
*link = (Link) { |
| 404 |
.event = udev_event_ref(event), |
| 405 |
}; |
| 406 |
|
| 407 |
r = device_get_ifname(dev, &link->ifname); |
| 408 |
if (r < 0) |
| 409 |
return r; |
| 410 |
|
| 411 |
r = sd_device_get_ifindex(dev, &link->ifindex); |
| 412 |
if (r < 0) |
| 413 |
return r; |
| 414 |
|
| 415 |
r = sd_device_get_action(dev, &link->action); |
| 416 |
if (r < 0) |
| 417 |
return r; |
| 418 |
|
| 419 |
r = device_get_sysattr_unsigned(dev, "name_assign_type", &link->name_assign_type); |
| 420 |
if (r < 0) |
| 421 |
log_link_debug_errno(link, r, "Failed to get \"name_assign_type\" attribute, ignoring: %m"); |
| 422 |
else |
| 423 |
log_link_debug(link, "Device has name_assign_type attribute: %u", link->name_assign_type); |
| 424 |
|
| 425 |
r = device_get_sysattr_unsigned(dev, "addr_assign_type", &link->addr_assign_type); |
| 426 |
if (r < 0) |
| 427 |
log_link_debug_errno(link, r, "Failed to get \"addr_assign_type\" attribute, ignoring: %m"); |
| 428 |
else |
| 429 |
log_link_debug(link, "Device has addr_assign_type attribute: %u", link->addr_assign_type); |
| 430 |
|
| 431 |
r = rtnl_get_link_info(&event->rtnl, link->ifindex, &link->iftype, &link->flags, |
| 432 |
&link->kind, &link->hw_addr, &link->permanent_hw_addr); |
| 433 |
if (r < 0) |
| 434 |
return r; |
| 435 |
|
| 436 |
if (link->hw_addr.length > 0 && link->permanent_hw_addr.length == 0) { |
| 437 |
r = ethtool_get_permanent_hw_addr(&ctx->ethtool_fd, link->ifname, &link->permanent_hw_addr); |
| 438 |
if (r < 0) |
| 439 |
log_link_debug_errno(link, r, "Failed to get permanent hardware address, ignoring: %m"); |
| 440 |
} |
| 441 |
|
| 442 |
r = sd_device_get_property_value(dev, "ID_NET_DRIVER", &link->driver); |
| 443 |
if (r < 0 && r != -ENOENT) |
| 444 |
log_link_debug_errno(link, r, "Failed to get driver, ignoring: %m"); |
| 445 |
|
| 446 |
*ret = TAKE_PTR(link); |
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
int link_get_config(LinkConfigContext *ctx, Link *link) { |
| 451 |
int r; |
| 452 |
|
| 453 |
assert(ctx); |
| 454 |
assert(link); |
| 455 |
|
| 456 |
/* Do not configure loopback interfaces by .link files. */ |
| 457 |
if (link->flags & IFF_LOOPBACK) |
| 458 |
return -ENOENT; |
| 459 |
|
| 460 |
LIST_FOREACH(configs, config, ctx->configs) { |
| 461 |
r = net_match_config( |
| 462 |
&config->match, |
| 463 |
link->event->dev, |
| 464 |
&link->hw_addr, |
| 465 |
&link->permanent_hw_addr, |
| 466 |
link->driver, |
| 467 |
link->iftype, |
| 468 |
link->kind, |
| 469 |
link->ifname, |
| 470 |
/* alternative_names= */ NULL, |
| 471 |
/* wlan_iftype= */ 0, |
| 472 |
/* ssid= */ NULL, |
| 473 |
/* bssid= */ NULL); |
| 474 |
if (r < 0) |
| 475 |
return r; |
| 476 |
if (r == 0) |
| 477 |
continue; |
| 478 |
|
| 479 |
if (config->match.ifname && !strv_contains(config->match.ifname, "*") && link->name_assign_type == NET_NAME_ENUM) |
| 480 |
log_link_warning(link, "Config file %s is applied to device based on potentially unpredictable interface name.", |
| 481 |
config->filename); |
| 482 |
else |
| 483 |
log_link_debug(link, "Config file %s is applied", config->filename); |
| 484 |
|
| 485 |
link->config = config; |
| 486 |
return 0; |
| 487 |
} |
| 488 |
|
| 489 |
return -ENOENT; |
| 490 |
} |
| 491 |
|
| 492 |
static int link_apply_ethtool_settings(Link *link, int *ethtool_fd) { |
| 493 |
LinkConfig *config = ASSERT_PTR(ASSERT_PTR(link)->config); |
| 494 |
const char *name = ASSERT_PTR(link->ifname); |
| 495 |
int r; |
| 496 |
|
| 497 |
assert(link->event); |
| 498 |
assert(ethtool_fd); |
| 499 |
|
| 500 |
if (link->event->event_mode != EVENT_UDEV_WORKER) { |
| 501 |
log_link_debug(link, "Running in test mode, skipping application of ethtool settings."); |
| 502 |
return 0; |
| 503 |
} |
| 504 |
|
| 505 |
r = ethtool_set_link_settings(ethtool_fd, name, |
| 506 |
config->autonegotiation, config->advertise, |
| 507 |
config->speed, config->duplex, config->port, config->mdi); |
| 508 |
if (r < 0) { |
| 509 |
if (config->autonegotiation >= 0) |
| 510 |
log_link_warning_errno(link, r, "Could not %s auto negotiation, ignoring: %m", |
| 511 |
enable_disable(config->autonegotiation)); |
| 512 |
|
| 513 |
if (!eqzero(config->advertise)) |
| 514 |
log_link_warning_errno(link, r, "Could not set advertise mode, ignoring: %m"); |
| 515 |
|
| 516 |
if (config->speed > 0) |
| 517 |
log_link_warning_errno(link, r, "Could not set speed to %"PRIu64"Mbps, ignoring: %m", |
| 518 |
DIV_ROUND_UP(config->speed, 1000000)); |
| 519 |
|
| 520 |
if (config->duplex >= 0) |
| 521 |
log_link_warning_errno(link, r, "Could not set duplex to %s, ignoring: %m", |
| 522 |
duplex_to_string(config->duplex)); |
| 523 |
|
| 524 |
if (config->port >= 0) |
| 525 |
log_link_warning_errno(link, r, "Could not set port to '%s', ignoring: %m", |
| 526 |
port_to_string(config->port)); |
| 527 |
|
| 528 |
if (config->mdi != ETH_TP_MDI_INVALID) |
| 529 |
log_link_warning_errno(link, r, "Could not set MDI-X to '%s', ignoring: %m", |
| 530 |
mdi_to_string(config->mdi)); |
| 531 |
} |
| 532 |
|
| 533 |
r = ethtool_set_wol(ethtool_fd, name, config->wol, config->wol_password); |
| 534 |
if (r < 0) { |
| 535 |
_cleanup_free_ char *str = NULL; |
| 536 |
|
| 537 |
(void) wol_options_to_string_alloc(config->wol, &str); |
| 538 |
log_link_warning_errno(link, r, "Could not set WakeOnLan%s%s, ignoring: %m", |
| 539 |
isempty(str) ? "" : " to ", strempty(str)); |
| 540 |
} |
| 541 |
|
| 542 |
r = ethtool_set_features(ethtool_fd, name, config->features); |
| 543 |
if (r < 0) |
| 544 |
log_link_warning_errno(link, r, "Could not set offload features, ignoring: %m"); |
| 545 |
|
| 546 |
r = ethtool_set_channels(ethtool_fd, name, &config->channels); |
| 547 |
if (r < 0) |
| 548 |
log_link_warning_errno(link, r, "Could not set channels, ignoring: %m"); |
| 549 |
|
| 550 |
r = ethtool_set_nic_buffer_size(ethtool_fd, name, &config->ring); |
| 551 |
if (r < 0) |
| 552 |
log_link_warning_errno(link, r, "Could not set ring buffer, ignoring: %m"); |
| 553 |
|
| 554 |
r = ethtool_set_flow_control(ethtool_fd, name, config->rx_flow_control, config->tx_flow_control, config->autoneg_flow_control); |
| 555 |
if (r < 0) |
| 556 |
log_link_warning_errno(link, r, "Could not set flow control, ignoring: %m"); |
| 557 |
|
| 558 |
r = ethtool_set_nic_coalesce_settings(ethtool_fd, name, &config->coalesce); |
| 559 |
if (r < 0) |
| 560 |
log_link_warning_errno(link, r, "Could not set coalesce settings, ignoring: %m"); |
| 561 |
|
| 562 |
r = ethtool_set_eee_settings(ethtool_fd, name, config->eee_enabled, config->eee_tx_lpi_enabled, config->eee_tx_lpi_timer_usec, config->eee_advertise[0]); |
| 563 |
if (r < 0) |
| 564 |
log_link_warning_errno(link, r, "Could not set energy efficient ethernet settings, ignoring: %m"); |
| 565 |
|
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
static int link_generate_new_hw_addr(Link *link, struct hw_addr_data *ret) { |
| 570 |
struct hw_addr_data hw_addr = HW_ADDR_NULL; |
| 571 |
bool is_static = false; |
| 572 |
uint8_t *p; |
| 573 |
size_t len; |
| 574 |
int r; |
| 575 |
|
| 576 |
assert(link); |
| 577 |
assert(link->config); |
| 578 |
assert(link->event); |
| 579 |
assert(link->event->dev); |
| 580 |
assert(ret); |
| 581 |
|
| 582 |
if (link->hw_addr.length == 0) |
| 583 |
goto finalize; |
| 584 |
|
| 585 |
if (link->config->mac_address_policy == MAC_ADDRESS_POLICY_NONE) { |
| 586 |
log_link_debug(link, "Using static MAC address."); |
| 587 |
hw_addr = link->config->hw_addr; |
| 588 |
is_static = true; |
| 589 |
goto finalize; |
| 590 |
} |
| 591 |
|
| 592 |
if (!IN_SET(link->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND)) |
| 593 |
goto finalize; |
| 594 |
|
| 595 |
switch (link->addr_assign_type) { |
| 596 |
case NET_ADDR_SET: |
| 597 |
log_link_debug(link, "MAC address on the device already set by userspace."); |
| 598 |
goto finalize; |
| 599 |
case NET_ADDR_STOLEN: |
| 600 |
log_link_debug(link, "MAC address on the device already set based on another device."); |
| 601 |
goto finalize; |
| 602 |
case NET_ADDR_RANDOM: |
| 603 |
case NET_ADDR_PERM: |
| 604 |
break; |
| 605 |
default: |
| 606 |
log_link_warning(link, "Unknown addr_assign_type %u, ignoring", link->addr_assign_type); |
| 607 |
goto finalize; |
| 608 |
} |
| 609 |
|
| 610 |
if ((link->config->mac_address_policy == MAC_ADDRESS_POLICY_RANDOM) == (link->addr_assign_type == NET_ADDR_RANDOM)) { |
| 611 |
log_link_debug(link, "MAC address on the device already matches policy \"%s\".", |
| 612 |
mac_address_policy_to_string(link->config->mac_address_policy)); |
| 613 |
goto finalize; |
| 614 |
} |
| 615 |
|
| 616 |
hw_addr = (struct hw_addr_data) { |
| 617 |
.length = arphrd_to_hw_addr_len(link->iftype), |
| 618 |
}; |
| 619 |
|
| 620 |
switch (link->iftype) { |
| 621 |
case ARPHRD_ETHER: |
| 622 |
p = hw_addr.bytes; |
| 623 |
len = hw_addr.length; |
| 624 |
break; |
| 625 |
case ARPHRD_INFINIBAND: |
| 626 |
p = hw_addr.bytes + INFINIBAND_ALEN - 8; |
| 627 |
len = 8; |
| 628 |
break; |
| 629 |
default: |
| 630 |
assert_not_reached(); |
| 631 |
} |
| 632 |
|
| 633 |
if (link->config->mac_address_policy == MAC_ADDRESS_POLICY_RANDOM) |
| 634 |
/* We require genuine randomness here, since we want to make sure we won't collide with other |
| 635 |
* systems booting up at the very same time. */ |
| 636 |
for (;;) { |
| 637 |
random_bytes(p, len); |
| 638 |
if (hw_addr_is_valid(&hw_addr, link->iftype)) |
| 639 |
break; |
| 640 |
} |
| 641 |
|
| 642 |
else { |
| 643 |
uint64_t result; |
| 644 |
|
| 645 |
r = net_get_unique_predictable_data(link->event->dev, |
| 646 |
naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS), |
| 647 |
&result); |
| 648 |
if (r < 0) |
| 649 |
return log_link_warning_errno(link, r, "Could not generate persistent MAC address: %m"); |
| 650 |
|
| 651 |
assert(len <= sizeof(result)); |
| 652 |
memcpy(p, &result, len); |
| 653 |
if (!hw_addr_is_valid(&hw_addr, link->iftype)) |
| 654 |
return log_link_warning_errno(link, SYNTHETIC_ERRNO(EINVAL), |
| 655 |
"Could not generate valid persistent MAC address."); |
| 656 |
} |
| 657 |
|
| 658 |
finalize: |
| 659 |
|
| 660 |
r = net_verify_hardware_address(link->ifname, is_static, link->iftype, &link->hw_addr, &hw_addr); |
| 661 |
if (r < 0) |
| 662 |
return r; |
| 663 |
|
| 664 |
if (hw_addr_equal(&link->hw_addr, &hw_addr)) { |
| 665 |
*ret = HW_ADDR_NULL; |
| 666 |
return 0; |
| 667 |
} |
| 668 |
|
| 669 |
if (hw_addr.length > 0) |
| 670 |
log_link_debug(link, "Applying %s MAC address: %s", |
| 671 |
link->config->mac_address_policy == MAC_ADDRESS_POLICY_NONE ? "static" : |
| 672 |
mac_address_policy_to_string(link->config->mac_address_policy), |
| 673 |
HW_ADDR_TO_STR(&hw_addr)); |
| 674 |
|
| 675 |
*ret = hw_addr; |
| 676 |
return 0; |
| 677 |
} |
| 678 |
|
| 679 |
static int link_apply_rtnl_settings(Link *link) { |
| 680 |
struct hw_addr_data hw_addr = {}; |
| 681 |
LinkConfig *config = ASSERT_PTR(ASSERT_PTR(link)->config); |
| 682 |
int r; |
| 683 |
|
| 684 |
assert(link->event); |
| 685 |
|
| 686 |
if (link->event->event_mode != EVENT_UDEV_WORKER) { |
| 687 |
log_link_debug(link, "Running in test mode, skipping application of rtnl settings."); |
| 688 |
return 0; |
| 689 |
} |
| 690 |
|
| 691 |
(void) link_generate_new_hw_addr(link, &hw_addr); |
| 692 |
|
| 693 |
r = rtnl_set_link_properties(&link->event->rtnl, link->ifindex, config->alias, &hw_addr, |
| 694 |
config->txqueues, config->rxqueues, config->txqueuelen, |
| 695 |
config->mtu, config->gso_max_size, config->gso_max_segments); |
| 696 |
if (r < 0) |
| 697 |
log_link_warning_errno(link, r, |
| 698 |
"Could not set Alias=, MACAddress=/MACAddressPolicy=, " |
| 699 |
"TransmitQueues=, ReceiveQueues=, TransmitQueueLength=, MTUBytes=, " |
| 700 |
"GenericSegmentOffloadMaxBytes= or GenericSegmentOffloadMaxSegments=, " |
| 701 |
"ignoring: %m"); |
| 702 |
|
| 703 |
return 0; |
| 704 |
} |
| 705 |
|
| 706 |
static bool enable_name_policy(void) { |
| 707 |
static int cached = -1; |
| 708 |
bool b; |
| 709 |
int r; |
| 710 |
|
| 711 |
if (cached >= 0) |
| 712 |
return cached; |
| 713 |
|
| 714 |
r = proc_cmdline_get_bool("net.ifnames", /* flags= */ 0, &b); |
| 715 |
if (r < 0) |
| 716 |
log_warning_errno(r, "Failed to parse net.ifnames= kernel command line option, ignoring: %m"); |
| 717 |
if (r <= 0) |
| 718 |
return (cached = true); |
| 719 |
|
| 720 |
if (!b) |
| 721 |
log_info("Network interface NamePolicy= disabled on kernel command line."); |
| 722 |
|
| 723 |
return (cached = b); |
| 724 |
} |
| 725 |
|
| 726 |
static int link_generate_new_name(Link *link) { |
| 727 |
LinkConfig *config = ASSERT_PTR(ASSERT_PTR(link)->config);; |
| 728 |
sd_device *device = ASSERT_PTR(ASSERT_PTR(link->event)->dev); |
| 729 |
|
| 730 |
if (link->action != SD_DEVICE_ADD) { |
| 731 |
log_link_debug(link, "Not applying Name= and NamePolicy= on '%s' uevent.", |
| 732 |
device_action_to_string(link->action)); |
| 733 |
goto no_rename; |
| 734 |
} |
| 735 |
|
| 736 |
if (IN_SET(link->name_assign_type, NET_NAME_USER, NET_NAME_RENAMED) && |
| 737 |
!naming_scheme_has(NAMING_ALLOW_RERENAMES)) { |
| 738 |
log_link_debug(link, "Device already has a name given by userspace, not renaming."); |
| 739 |
goto no_rename; |
| 740 |
} |
| 741 |
|
| 742 |
if (enable_name_policy() && config->name_policy) |
| 743 |
for (NamePolicy *policy = config->name_policy; *policy != _NAMEPOLICY_INVALID; policy++) { |
| 744 |
const char *new_name = NULL; |
| 745 |
|
| 746 |
switch (*policy) { |
| 747 |
case NAMEPOLICY_KERNEL: |
| 748 |
if (link->name_assign_type != NET_NAME_PREDICTABLE) |
| 749 |
continue; |
| 750 |
|
| 751 |
/* The kernel claims to have given a predictable name, keep it. */ |
| 752 |
log_link_debug(link, "Policy *%s*: keeping predictable kernel name", |
| 753 |
name_policy_to_string(*policy)); |
| 754 |
goto no_rename; |
| 755 |
case NAMEPOLICY_KEEP: |
| 756 |
if (!IN_SET(link->name_assign_type, NET_NAME_USER, NET_NAME_RENAMED)) |
| 757 |
continue; |
| 758 |
|
| 759 |
log_link_debug(link, "Policy *%s*: keeping existing userspace name", |
| 760 |
name_policy_to_string(*policy)); |
| 761 |
goto no_rename; |
| 762 |
case NAMEPOLICY_DATABASE: |
| 763 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &new_name); |
| 764 |
break; |
| 765 |
case NAMEPOLICY_ONBOARD: |
| 766 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &new_name); |
| 767 |
break; |
| 768 |
case NAMEPOLICY_SLOT: |
| 769 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &new_name); |
| 770 |
break; |
| 771 |
case NAMEPOLICY_PATH: |
| 772 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &new_name); |
| 773 |
break; |
| 774 |
case NAMEPOLICY_MAC: |
| 775 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &new_name); |
| 776 |
break; |
| 777 |
default: |
| 778 |
assert_not_reached(); |
| 779 |
} |
| 780 |
if (ifname_valid(new_name)) { |
| 781 |
log_link_debug(link, "Policy *%s* yields \"%s\".", name_policy_to_string(*policy), new_name); |
| 782 |
link->new_name = new_name; |
| 783 |
return 0; |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
if (link->config->name) { |
| 788 |
log_link_debug(link, "Policies didn't yield a name, using specified Name=%s.", link->config->name); |
| 789 |
link->new_name = link->config->name; |
| 790 |
return 0; |
| 791 |
} |
| 792 |
|
| 793 |
log_link_debug(link, "Policies didn't yield a name and Name= is not given, not renaming."); |
| 794 |
no_rename: |
| 795 |
if (!naming_scheme_has(NAMING_USE_INTERFACE_PROPERTY)) |
| 796 |
return sd_device_get_sysname(device, &link->new_name); |
| 797 |
|
| 798 |
link->new_name = link->ifname; |
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
static int link_generate_alternative_names(Link *link) { |
| 803 |
_cleanup_strv_free_ char **altnames = NULL; |
| 804 |
LinkConfig *config = ASSERT_PTR(ASSERT_PTR(link)->config); |
| 805 |
sd_device *device = ASSERT_PTR(ASSERT_PTR(link->event)->dev); |
| 806 |
int r; |
| 807 |
|
| 808 |
assert(!ASSERT_PTR(link->event)->altnames); |
| 809 |
|
| 810 |
if (link->action != SD_DEVICE_ADD) { |
| 811 |
log_link_debug(link, "Not applying AlternativeNames= and AlternativeNamesPolicy= on '%s' uevent.", |
| 812 |
device_action_to_string(link->action)); |
| 813 |
return 0; |
| 814 |
} |
| 815 |
|
| 816 |
if (config->alternative_names) { |
| 817 |
altnames = strv_copy(config->alternative_names); |
| 818 |
if (!altnames) |
| 819 |
return log_oom(); |
| 820 |
} |
| 821 |
|
| 822 |
if (config->alternative_names_policy) |
| 823 |
for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) { |
| 824 |
const char *n = NULL; |
| 825 |
|
| 826 |
switch (*p) { |
| 827 |
case NAMEPOLICY_DATABASE: |
| 828 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &n); |
| 829 |
break; |
| 830 |
case NAMEPOLICY_ONBOARD: |
| 831 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &n); |
| 832 |
break; |
| 833 |
case NAMEPOLICY_SLOT: |
| 834 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &n); |
| 835 |
break; |
| 836 |
case NAMEPOLICY_PATH: |
| 837 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &n); |
| 838 |
break; |
| 839 |
case NAMEPOLICY_MAC: |
| 840 |
(void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &n); |
| 841 |
break; |
| 842 |
default: |
| 843 |
assert_not_reached(); |
| 844 |
} |
| 845 |
if (ifname_valid_full(n, IFNAME_VALID_ALTERNATIVE)) { |
| 846 |
r = strv_extend(&altnames, n); |
| 847 |
if (r < 0) |
| 848 |
return log_oom(); |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
link->event->altnames = TAKE_PTR(altnames); |
| 853 |
return 0; |
| 854 |
} |
| 855 |
|
| 856 |
static int sr_iov_configure(Link *link, sd_netlink **rtnl, SRIOV *sr_iov, SRIOVAttribute attr) { |
| 857 |
int r; |
| 858 |
|
| 859 |
assert(link); |
| 860 |
assert(rtnl); |
| 861 |
assert(link->ifindex > 0); |
| 862 |
|
| 863 |
if (!sr_iov_has_config(sr_iov, attr)) |
| 864 |
return 0; |
| 865 |
|
| 866 |
if (!*rtnl) { |
| 867 |
r = sd_netlink_open(rtnl); |
| 868 |
if (r < 0) |
| 869 |
return r; |
| 870 |
} |
| 871 |
|
| 872 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
| 873 |
r = sd_rtnl_message_new_link(*rtnl, &req, RTM_SETLINK, link->ifindex); |
| 874 |
if (r < 0) |
| 875 |
return r; |
| 876 |
|
| 877 |
r = sr_iov_set_netlink_message(sr_iov, attr, req); |
| 878 |
if (r < 0) |
| 879 |
return r; |
| 880 |
|
| 881 |
return sd_netlink_call(*rtnl, req, 0, NULL); |
| 882 |
} |
| 883 |
|
| 884 |
static int link_apply_sr_iov_config(Link *link) { |
| 885 |
SRIOV *sr_iov; |
| 886 |
uint32_t n; |
| 887 |
int r; |
| 888 |
|
| 889 |
assert(link); |
| 890 |
assert(link->config); |
| 891 |
assert(ASSERT_PTR(link->event)->dev); |
| 892 |
|
| 893 |
if (link->event->event_mode != EVENT_UDEV_WORKER) { |
| 894 |
log_link_debug(link, "Running in test mode, skipping application of SR-IOV settings."); |
| 895 |
return 0; |
| 896 |
} |
| 897 |
|
| 898 |
r = sr_iov_set_num_vfs(link->event->dev, link->config->sr_iov_num_vfs, link->config->sr_iov_by_section); |
| 899 |
if (r < 0) |
| 900 |
log_link_warning_errno(link, r, "Failed to set the number of SR-IOV virtual functions, ignoring: %m"); |
| 901 |
|
| 902 |
if (ordered_hashmap_isempty(link->config->sr_iov_by_section)) |
| 903 |
return 0; |
| 904 |
|
| 905 |
r = sr_iov_get_num_vfs(link->event->dev, &n); |
| 906 |
if (r < 0) { |
| 907 |
log_link_warning_errno(link, r, "Failed to get the number of SR-IOV virtual functions, ignoring all [SR-IOV] sections: %m"); |
| 908 |
return 0; |
| 909 |
} |
| 910 |
if (n == 0) { |
| 911 |
log_link_warning(link, "No SR-IOV virtual functions exist, ignoring all [SR-IOV] sections."); |
| 912 |
return 0; |
| 913 |
} |
| 914 |
|
| 915 |
ORDERED_HASHMAP_FOREACH(sr_iov, link->config->sr_iov_by_section) { |
| 916 |
if (sr_iov->vf >= n) { |
| 917 |
log_link_warning(link, "SR-IOV virtual function %"PRIu32" does not exist, ignoring [SR-IOV] section for the virtual function.", sr_iov->vf); |
| 918 |
continue; |
| 919 |
} |
| 920 |
|
| 921 |
for (SRIOVAttribute attr = 0; attr < _SR_IOV_ATTRIBUTE_MAX; attr++) { |
| 922 |
r = sr_iov_configure(link, &link->event->rtnl, sr_iov, attr); |
| 923 |
if (r < 0) |
| 924 |
log_link_warning_errno(link, r, |
| 925 |
"Failed to set up %s for SR-IOV virtual function %"PRIu32", ignoring: %m", |
| 926 |
sr_iov_attribute_to_string(attr), sr_iov->vf); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
return 0; |
| 931 |
} |
| 932 |
|
| 933 |
/* Get the local NUMA node for a network device from sysfs. |
| 934 |
* Returns -ENOENT if numa_node file doesn't exist or shows -1 (no NUMA). */ |
| 935 |
static int link_get_device_numa_node(Link *link, unsigned *ret) { |
| 936 |
int r, node; |
| 937 |
|
| 938 |
assert(link); |
| 939 |
assert(link->event); |
| 940 |
assert(link->event->dev); |
| 941 |
assert(ret); |
| 942 |
|
| 943 |
r = device_get_sysattr_int(link->event->dev, "device/numa_node", &node); |
| 944 |
if (r < 0) |
| 945 |
return r; |
| 946 |
|
| 947 |
/* -1 means no NUMA node (non-NUMA system or device not associated with a node) */ |
| 948 |
if (node < 0) |
| 949 |
return -ENOENT; |
| 950 |
|
| 951 |
*ret = (unsigned) node; |
| 952 |
return 0; |
| 953 |
} |
| 954 |
|
| 955 |
/* CPU topology information for IRQ affinity spread algorithm. */ |
| 956 |
typedef struct CPUTopology { |
| 957 |
unsigned cpu; |
| 958 |
unsigned numa_node; |
| 959 |
unsigned package_id; |
| 960 |
unsigned die_id; /* L3 cache domain / chiplet */ |
| 961 |
unsigned core_id; |
| 962 |
bool is_first_thread; /* First hyperthread of a physical core */ |
| 963 |
} CPUTopology; |
| 964 |
|
| 965 |
/* Die (L3 cache domain) information for spread algorithm */ |
| 966 |
typedef struct DieInfo { |
| 967 |
unsigned die_id; |
| 968 |
unsigned *cpus; /* CPUs in this die (first HT only, sorted by core) */ |
| 969 |
size_t cpu_count; |
| 970 |
size_t next_idx; /* For round-robin within die */ |
| 971 |
} DieInfo; |
| 972 |
|
| 973 |
/* Returns the first thread of a CPU siblings list */ |
| 974 |
static int cpu_topology_get_first_thread(sd_device *cpu_node, unsigned *ret) { |
| 975 |
const char *content, *end; |
| 976 |
int r; |
| 977 |
|
| 978 |
assert(cpu_node); |
| 979 |
assert(ret); |
| 980 |
|
| 981 |
r = sd_device_get_sysattr_value(cpu_node, "topology/thread_siblings_list", &content); |
| 982 |
if (r < 0) |
| 983 |
return r; |
| 984 |
|
| 985 |
end = content + strcspn(content, ",-"); |
| 986 |
|
| 987 |
_cleanup_free_ char *first = strndup(content, end - content); |
| 988 |
if (!first) |
| 989 |
return -ENOMEM; |
| 990 |
|
| 991 |
return safe_atou(first, ret); |
| 992 |
} |
| 993 |
|
| 994 |
static int cpu_topology_compare(const CPUTopology *a, const CPUTopology *b) { |
| 995 |
int r; |
| 996 |
|
| 997 |
assert(a); |
| 998 |
assert(b); |
| 999 |
|
| 1000 |
/* Sort by die first (for L3 cache grouping), then core, then CPU number */ |
| 1001 |
r = CMP(a->die_id, b->die_id); |
| 1002 |
if (r != 0) |
| 1003 |
return r; |
| 1004 |
|
| 1005 |
r = CMP(a->core_id, b->core_id); |
| 1006 |
if (r != 0) |
| 1007 |
return r; |
| 1008 |
|
| 1009 |
return CMP(a->cpu, b->cpu); |
| 1010 |
} |
| 1011 |
|
| 1012 |
/* Comparison function for sorting CPUs by CPU number (for die ID assignment) */ |
| 1013 |
static int cpu_number_compare(const CPUTopology *a, const CPUTopology *b) { |
| 1014 |
assert(a); |
| 1015 |
assert(b); |
| 1016 |
|
| 1017 |
return CMP(a->cpu, b->cpu); |
| 1018 |
} |
| 1019 |
|
| 1020 |
/* Assign logical die IDs based on L3 cache sharing topology. |
| 1021 |
* |
| 1022 |
* For IRQ spreading, the goal is to distribute interrupts across CPUs that |
| 1023 |
* don't share cache, minimizing cache line contention when processing packets. |
| 1024 |
* The L3 cache boundary is the key locality domain: CPUs sharing an L3 can |
| 1025 |
* exchange data cheaply, while cross-L3 communication is expensive. |
| 1026 |
* |
| 1027 |
* We use L3 shared_cpu_list rather than the kernel's physical die_id because: |
| 1028 |
* - On AMD EPYC, multiple CCXs on the same physical die have separate L3 caches |
| 1029 |
* - On Intel with Sub-NUMA Clustering, one die may have multiple L3 domains |
| 1030 |
* - L3 sharing reflects actual data locality, not physical packaging */ |
| 1031 |
static int assign_sequential_die_ids(CPUTopology *cpus, size_t count) { |
| 1032 |
_cleanup_strv_free_ char **l3_groups = NULL; |
| 1033 |
int r; |
| 1034 |
|
| 1035 |
assert(cpus); |
| 1036 |
|
| 1037 |
/* First, sort CPUs by CPU number for consistent discovery order */ |
| 1038 |
typesafe_qsort(cpus, count, cpu_number_compare); |
| 1039 |
|
| 1040 |
/* Assign die IDs based on order of L3 shared_cpu_list discovery */ |
| 1041 |
FOREACH_ARRAY(cpu, cpus, count) { |
| 1042 |
_cleanup_(sd_device_unrefp) sd_device *cpu_node = NULL; |
| 1043 |
char cpu_path[STRLEN("/sys/devices/system/cpu/cpu") + DECIMAL_STR_MAX(unsigned) + 1]; |
| 1044 |
const char *l3_list; |
| 1045 |
unsigned die_id = 0; |
| 1046 |
bool found = false; |
| 1047 |
|
| 1048 |
xsprintf(cpu_path, "/sys/devices/system/cpu/cpu%u", cpu->cpu); |
| 1049 |
r = sd_device_new_from_syspath(&cpu_node, cpu_path); |
| 1050 |
if (r < 0) |
| 1051 |
return r; |
| 1052 |
|
| 1053 |
r = sd_device_get_sysattr_value(cpu_node, "cache/index3/shared_cpu_list", &l3_list); |
| 1054 |
if (r < 0) { |
| 1055 |
/* No L3 info, fall back to package ID */ |
| 1056 |
cpu->die_id = cpu->package_id; |
| 1057 |
continue; |
| 1058 |
} |
| 1059 |
|
| 1060 |
/* Check if we've seen this L3 group before */ |
| 1061 |
STRV_FOREACH(g, l3_groups) { |
| 1062 |
if (streq(*g, l3_list)) { |
| 1063 |
cpu->die_id = die_id; |
| 1064 |
found = true; |
| 1065 |
break; |
| 1066 |
} |
| 1067 |
die_id++; |
| 1068 |
} |
| 1069 |
|
| 1070 |
if (!found) { |
| 1071 |
/* New L3 group, assign next sequential die ID */ |
| 1072 |
cpu->die_id = die_id; |
| 1073 |
r = strv_extend(&l3_groups, l3_list); |
| 1074 |
if (r < 0) |
| 1075 |
return r; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
return 0; |
| 1080 |
} |
| 1081 |
|
| 1082 |
static int discover_cpu_topology(CPUTopology **ret, size_t *ret_count) { |
| 1083 |
_cleanup_(sd_device_unrefp) sd_device *parent_node = NULL; |
| 1084 |
_cleanup_free_ CPUTopology *cpus = NULL; |
| 1085 |
const char *name; |
| 1086 |
size_t count = 0; |
| 1087 |
int r; |
| 1088 |
|
| 1089 |
assert(ret); |
| 1090 |
assert(ret_count); |
| 1091 |
|
| 1092 |
r = sd_device_new_from_syspath(&parent_node, "/sys/devices/system/cpu"); |
| 1093 |
if (r < 0) |
| 1094 |
return r; |
| 1095 |
|
| 1096 |
FOREACH_DEVICE_CHILD_WITH_SUFFIX(parent_node, cpu_node, name) { |
| 1097 |
char topo_path[STRLEN("/sys/devices/system/cpu/cpu/topology") + DECIMAL_STR_MAX(unsigned) + 1]; |
| 1098 |
const char *n; |
| 1099 |
unsigned cpu, online, first_thread; |
| 1100 |
|
| 1101 |
n = startswith(name, "cpu"); |
| 1102 |
if (!n) |
| 1103 |
continue; |
| 1104 |
|
| 1105 |
r = safe_atou(n, &cpu); |
| 1106 |
if (r < 0) { |
| 1107 |
log_debug_errno(r, "Failed to convert %s to unsigned, skipping: %m", n); |
| 1108 |
continue; |
| 1109 |
} |
| 1110 |
|
| 1111 |
r = device_get_sysattr_unsigned(cpu_node, "online", &online); |
| 1112 |
if (r == -ENOENT) |
| 1113 |
online = 1; /* CPU 0 lacks 'online' file, assume online */ |
| 1114 |
else if (r < 0 || online == 0) |
| 1115 |
continue; |
| 1116 |
|
| 1117 |
/* Check if topology directory exists (filters out cpu0 on some systems) */ |
| 1118 |
xsprintf(topo_path, "/sys/devices/system/cpu/cpu%u/topology", cpu); |
| 1119 |
if (access(topo_path, F_OK) < 0) { |
| 1120 |
log_debug_errno(errno, "Failed to access %s, ignoring: %m", topo_path); |
| 1121 |
continue; |
| 1122 |
} |
| 1123 |
|
| 1124 |
if (!GREEDY_REALLOC(cpus, count + 1)) |
| 1125 |
return -ENOMEM; |
| 1126 |
|
| 1127 |
cpus[count].cpu = cpu; |
| 1128 |
|
| 1129 |
r = numa_get_node_from_cpu(cpu, &cpus[count].numa_node); |
| 1130 |
if (r < 0) { |
| 1131 |
log_debug_errno(r, "Failed to get NUMA node for CPU %u, assuming NUMA node 0: %m", cpu); |
| 1132 |
cpus[count].numa_node = 0; |
| 1133 |
} |
| 1134 |
|
| 1135 |
r = device_get_sysattr_unsigned(cpu_node, "topology/physical_package_id", &cpus[count].package_id); |
| 1136 |
if (r < 0) { |
| 1137 |
log_device_debug_errno(cpu_node, r, "Failed to get physical_package_id, assuming package ID 0: %m"); |
| 1138 |
cpus[count].package_id = 0; |
| 1139 |
} |
| 1140 |
|
| 1141 |
/* die_id will be assigned later by assign_sequential_die_ids() */ |
| 1142 |
cpus[count].die_id = 0; |
| 1143 |
|
| 1144 |
r = device_get_sysattr_unsigned(cpu_node, "topology/core_id", &cpus[count].core_id); |
| 1145 |
if (r < 0) { |
| 1146 |
log_device_debug_errno(cpu_node, r, "Failed to get core_id, assuming core ID %u: %m", cpu); |
| 1147 |
cpus[count].core_id = cpu; |
| 1148 |
} |
| 1149 |
|
| 1150 |
r = cpu_topology_get_first_thread(cpu_node, &first_thread); |
| 1151 |
if (r < 0) |
| 1152 |
cpus[count].is_first_thread = true; |
| 1153 |
else |
| 1154 |
cpus[count].is_first_thread = (first_thread == cpu); |
| 1155 |
|
| 1156 |
count++; |
| 1157 |
} |
| 1158 |
|
| 1159 |
if (count == 0) |
| 1160 |
return -ENOENT; |
| 1161 |
|
| 1162 |
/* Assign sequential die IDs based on L3 discovery order */ |
| 1163 |
r = assign_sequential_die_ids(cpus, count); |
| 1164 |
if (r < 0) |
| 1165 |
return r; |
| 1166 |
|
| 1167 |
/* Sort CPUs by topology for consistent ordering */ |
| 1168 |
typesafe_qsort(cpus, count, cpu_topology_compare); |
| 1169 |
|
| 1170 |
*ret = TAKE_PTR(cpus); |
| 1171 |
*ret_count = count; |
| 1172 |
|
| 1173 |
return 0; |
| 1174 |
} |
| 1175 |
|
| 1176 |
/* Reorder indices so consecutive elements are maximally spread apart. |
| 1177 |
* |
| 1178 |
* Uses recursive divide-and-conquer: split in half, permute each half, |
| 1179 |
* then interleave. This ensures elements originally far apart become adjacent. |
| 1180 |
* |
| 1181 |
* Example trace for [0,1,2,3,4,5,6,7]: |
| 1182 |
* split into [0,1,2,3] and [4,5,6,7] |
| 1183 |
* recurse left: [0,1,2,3] -> [0,2,1,3] |
| 1184 |
* recurse right: [4,5,6,7] -> [4,6,5,7] |
| 1185 |
* interleave -> [0,4,2,6,1,5,3,7] |
| 1186 |
* |
| 1187 |
* The first N elements of the output are roughly evenly distributed across the |
| 1188 |
* original range, for any N. This is useful when assigning IRQs to CPUs: if a |
| 1189 |
* NIC has fewer IRQs than CPUs, the assigned CPUs will still be spread across |
| 1190 |
* the CPUs rather than all at the beginning. */ |
| 1191 |
static int equidist_permute(size_t *indices, size_t n_indices) { |
| 1192 |
_cleanup_free_ size_t *left = NULL, *right = NULL; |
| 1193 |
size_t left_count, right_count; |
| 1194 |
size_t li = 0, ri = 0, ti = 0; |
| 1195 |
int r; |
| 1196 |
|
| 1197 |
assert(indices); |
| 1198 |
|
| 1199 |
if (n_indices <= 1) |
| 1200 |
return 0; |
| 1201 |
|
| 1202 |
left_count = DIV_ROUND_UP(n_indices, 2); |
| 1203 |
right_count = n_indices - left_count; |
| 1204 |
|
| 1205 |
/* Recursively permute each half */ |
| 1206 |
left = newdup(size_t, indices, left_count); |
| 1207 |
right = newdup(size_t, &indices[left_count], right_count); |
| 1208 |
if (!left || !right) |
| 1209 |
return log_oom(); |
| 1210 |
|
| 1211 |
r = equidist_permute(left, left_count); |
| 1212 |
if (r < 0) |
| 1213 |
return r; |
| 1214 |
|
| 1215 |
r = equidist_permute(right, right_count); |
| 1216 |
if (r < 0) |
| 1217 |
return r; |
| 1218 |
|
| 1219 |
/* Interleave: left[0], right[0], left[1], right[1], ... */ |
| 1220 |
for (size_t i = 0; i < n_indices; i++) { |
| 1221 |
if (i % 2 == 0 && li < left_count) |
| 1222 |
indices[ti++] = left[li++]; |
| 1223 |
else if (ri < right_count) |
| 1224 |
indices[ti++] = right[ri++]; |
| 1225 |
else if (li < left_count) |
| 1226 |
indices[ti++] = left[li++]; |
| 1227 |
} |
| 1228 |
|
| 1229 |
return 0; |
| 1230 |
} |
| 1231 |
|
| 1232 |
static void die_info_free(DieInfo *dies, size_t count) { |
| 1233 |
assert(dies || count == 0); |
| 1234 |
|
| 1235 |
FOREACH_ARRAY(die, dies, count) |
| 1236 |
free(die->cpus); |
| 1237 |
free(dies); |
| 1238 |
} |
| 1239 |
|
| 1240 |
/* Build die information from topology, grouping CPUs by L3/die and filtering to first HT only */ |
| 1241 |
static int build_die_info(const CPUTopology *topology, size_t topology_count, DieInfo **ret, size_t *ret_count) { |
| 1242 |
DieInfo *dies = NULL; |
| 1243 |
size_t die_count = 0; |
| 1244 |
int r; |
| 1245 |
|
| 1246 |
assert(topology); |
| 1247 |
assert(ret); |
| 1248 |
assert(ret_count); |
| 1249 |
|
| 1250 |
CLEANUP_ARRAY(dies, die_count, die_info_free); |
| 1251 |
|
| 1252 |
FOREACH_ARRAY(cpu_topology, topology, topology_count) { |
| 1253 |
DieInfo *die = NULL; |
| 1254 |
|
| 1255 |
/* Only consider first hyperthreads for initial spread */ |
| 1256 |
if (!cpu_topology->is_first_thread) |
| 1257 |
continue; |
| 1258 |
|
| 1259 |
/* Find or create die entry */ |
| 1260 |
for (size_t j = 0; j < die_count; j++) |
| 1261 |
if (dies[j].die_id == cpu_topology->die_id) { |
| 1262 |
die = &dies[j]; |
| 1263 |
break; |
| 1264 |
} |
| 1265 |
|
| 1266 |
if (!die) { |
| 1267 |
if (!GREEDY_REALLOC(dies, die_count + 1)) |
| 1268 |
return log_oom(); |
| 1269 |
die = &dies[die_count++]; |
| 1270 |
*die = (DieInfo) { .die_id = cpu_topology->die_id }; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (!GREEDY_REALLOC(die->cpus, die->cpu_count + 1)) |
| 1274 |
return log_oom(); |
| 1275 |
|
| 1276 |
die->cpus[die->cpu_count++] = cpu_topology->cpu; |
| 1277 |
} |
| 1278 |
|
| 1279 |
/* Sort dies by die_id for determinism, then apply equidist to CPUs within each die */ |
| 1280 |
FOREACH_ARRAY(die, dies, die_count) { |
| 1281 |
_cleanup_free_ unsigned *reordered = NULL; |
| 1282 |
_cleanup_free_ size_t *indices = new(size_t, die->cpu_count); |
| 1283 |
if (!indices) |
| 1284 |
return log_oom(); |
| 1285 |
|
| 1286 |
for (size_t j = 0; j < die->cpu_count; j++) |
| 1287 |
indices[j] = j; |
| 1288 |
|
| 1289 |
r = equidist_permute(indices, die->cpu_count); |
| 1290 |
if (r < 0) |
| 1291 |
return r; |
| 1292 |
|
| 1293 |
/* Reorder CPUs according to equidist permutation */ |
| 1294 |
reordered = new(unsigned, die->cpu_count); |
| 1295 |
if (!reordered) |
| 1296 |
return log_oom(); |
| 1297 |
|
| 1298 |
for (size_t j = 0; j < die->cpu_count; j++) |
| 1299 |
reordered[j] = die->cpus[indices[j]]; |
| 1300 |
|
| 1301 |
memcpy(die->cpus, reordered, die->cpu_count * sizeof(unsigned)); |
| 1302 |
} |
| 1303 |
|
| 1304 |
*ret = TAKE_PTR(dies); |
| 1305 |
*ret_count = die_count; |
| 1306 |
|
| 1307 |
return 0; |
| 1308 |
} |
| 1309 |
|
| 1310 |
/* Select CPUs for IRQ affinity spreading with optimal topology distribution. |
| 1311 |
* |
| 1312 |
* Algorithm: |
| 1313 |
* 1. Group CPUs by die (L3 cache domain), using only first hyperthreads |
| 1314 |
* 2. Apply equidistant permutation to both die order and CPUs within each die, |
| 1315 |
* so consecutive selections are maximally spread (e.g., [0,1,2,3] -> [0,2,1,3]) |
| 1316 |
* 3. Round-robin across dies, picking one CPU per die per round |
| 1317 |
* 4. If more IRQs than physical cores, wrap around and reuse the same CPUs |
| 1318 |
* |
| 1319 |
* Ensures each IRQ gets a dedicated physical core before any core handles |
| 1320 |
* multiple IRQs. Two IRQs on one physical core time-share but benefit from warm |
| 1321 |
* cache, whereas spreading across SMT siblings causes resource contention with |
| 1322 |
* no cache benefit. |
| 1323 |
* Maximizes physical distance between consecutively assigned IRQs, improving |
| 1324 |
* cache distribution even when only a few IRQs are assigned. */ |
| 1325 |
static int select_spread_cpus( |
| 1326 |
const CPUTopology *topology, |
| 1327 |
size_t topology_count, |
| 1328 |
size_t n_irqs, |
| 1329 |
unsigned **ret, |
| 1330 |
size_t *ret_count) { |
| 1331 |
|
| 1332 |
_cleanup_free_ unsigned *selected = NULL; |
| 1333 |
_cleanup_free_ size_t *die_order = NULL; |
| 1334 |
DieInfo *dies = NULL; |
| 1335 |
size_t die_count = 0, selected_count = 0; |
| 1336 |
int r; |
| 1337 |
|
| 1338 |
assert(topology); |
| 1339 |
assert(ret); |
| 1340 |
assert(ret_count); |
| 1341 |
|
| 1342 |
CLEANUP_ARRAY(dies, die_count, die_info_free); |
| 1343 |
|
| 1344 |
selected = new(unsigned, n_irqs); |
| 1345 |
if (!selected) |
| 1346 |
return -ENOMEM; |
| 1347 |
|
| 1348 |
/* Build die information with first HT CPUs only */ |
| 1349 |
r = build_die_info(topology, topology_count, &dies, &die_count); |
| 1350 |
if (r < 0) |
| 1351 |
return r; |
| 1352 |
|
| 1353 |
if (die_count == 0) |
| 1354 |
return -ENOENT; |
| 1355 |
|
| 1356 |
/* Create equidistant die ordering */ |
| 1357 |
die_order = new(size_t, die_count); |
| 1358 |
if (!die_order) |
| 1359 |
return -ENOMEM; |
| 1360 |
|
| 1361 |
for (size_t i = 0; i < die_count; i++) |
| 1362 |
die_order[i] = i; |
| 1363 |
|
| 1364 |
r = equidist_permute(die_order, die_count); |
| 1365 |
if (r < 0) |
| 1366 |
return r; |
| 1367 |
|
| 1368 |
/* Round-robin across dies, picking one CPU from each die at a time */ |
| 1369 |
size_t dies_exhausted = 0; |
| 1370 |
while (selected_count < n_irqs) { |
| 1371 |
bool made_progress = false; |
| 1372 |
|
| 1373 |
for (size_t i = 0; i < die_count && selected_count < n_irqs; i++) { |
| 1374 |
DieInfo *die = &dies[die_order[i]]; |
| 1375 |
|
| 1376 |
if (die->next_idx >= die->cpu_count) |
| 1377 |
continue; |
| 1378 |
|
| 1379 |
selected[selected_count++] = die->cpus[die->next_idx++]; |
| 1380 |
made_progress = true; |
| 1381 |
|
| 1382 |
if (die->next_idx >= die->cpu_count) |
| 1383 |
dies_exhausted++; |
| 1384 |
} |
| 1385 |
|
| 1386 |
if (made_progress) |
| 1387 |
continue; |
| 1388 |
|
| 1389 |
/* All first HTs exhausted, wrap around for remaining IRQs */ |
| 1390 |
if (dies_exhausted < die_count) |
| 1391 |
break; |
| 1392 |
|
| 1393 |
/* Reset all dies for round-robin wrap */ |
| 1394 |
FOREACH_ARRAY(die, dies, die_count) |
| 1395 |
die->next_idx = 0; |
| 1396 |
dies_exhausted = 0; |
| 1397 |
} |
| 1398 |
|
| 1399 |
*ret = TAKE_PTR(selected); |
| 1400 |
*ret_count = selected_count; |
| 1401 |
|
| 1402 |
return 0; |
| 1403 |
} |
| 1404 |
|
| 1405 |
static int set_irq_affinity(Link *link, unsigned irq, unsigned cpu) { |
| 1406 |
_cleanup_free_ char *affinity_path = NULL, *mask_str = NULL; |
| 1407 |
unsigned n_groups = cpu / 32; |
| 1408 |
int r; |
| 1409 |
|
| 1410 |
assert(link); |
| 1411 |
|
| 1412 |
if (asprintf(&affinity_path, "/proc/irq/%u/smp_affinity", irq) < 0) |
| 1413 |
return log_oom(); |
| 1414 |
|
| 1415 |
/* Convert CPU number to hex bitmask. |
| 1416 |
* For CPU N, set bit N (1 << N). CPUs are split by comma-separated |
| 1417 |
* 32-bits groups. To assign CPU 32, we should write 1,00000000 */ |
| 1418 |
|
| 1419 |
if (asprintf(&mask_str, "%x", 1U << (cpu % 32)) < 0) |
| 1420 |
return log_oom(); |
| 1421 |
|
| 1422 |
for (unsigned i = 0; i < n_groups; i++) |
| 1423 |
if (!strextend(&mask_str, ",00000000")) |
| 1424 |
return log_oom(); |
| 1425 |
|
| 1426 |
r = write_string_file(affinity_path, mask_str, WRITE_STRING_FILE_DISABLE_BUFFER); |
| 1427 |
if (r < 0) |
| 1428 |
return log_link_warning_errno(link, r, "Failed to set IRQ %u affinity to CPU %u: %m", irq, cpu); |
| 1429 |
|
| 1430 |
log_link_debug(link, "Set IRQ %u affinity to CPU %u.", irq, cpu); |
| 1431 |
|
| 1432 |
return 0; |
| 1433 |
} |
| 1434 |
|
| 1435 |
static int link_apply_irq_affinity_spread(Link *link, const CPUSet *allowed_cpus) { |
| 1436 |
_cleanup_closedir_ DIR *dir = NULL; |
| 1437 |
_cleanup_free_ CPUTopology *topology = NULL; |
| 1438 |
_cleanup_free_ unsigned *irqs = NULL; |
| 1439 |
_cleanup_free_ unsigned *spread_cpus = NULL; |
| 1440 |
size_t topology_count = 0, irq_count = 0, spread_count = 0; |
| 1441 |
int r; |
| 1442 |
|
| 1443 |
assert(link); |
| 1444 |
|
| 1445 |
r = device_opendir(link->event->dev, "device/msi_irqs", &dir); |
| 1446 |
if (r < 0) { |
| 1447 |
if (r != -ENOENT) |
| 1448 |
return log_link_error_errno(link, r, "Failed to open device/msi_irqs: %m"); |
| 1449 |
log_link_debug_errno(link, r, "No MSI IRQs found, skipping IRQ affinity configuration: %m"); |
| 1450 |
return 0; |
| 1451 |
} |
| 1452 |
|
| 1453 |
FOREACH_DIRENT(de, dir, return log_link_error_errno(link, errno, "Failed to read directory device/msi_irqs: %m")) { |
| 1454 |
unsigned irq; |
| 1455 |
|
| 1456 |
r = safe_atou(de->d_name, &irq); |
| 1457 |
if (r < 0) |
| 1458 |
return log_link_error_errno(link, r, "Failed to convert parse IRQ number: %s", de->d_name); |
| 1459 |
|
| 1460 |
if (!GREEDY_REALLOC(irqs, irq_count + 1)) |
| 1461 |
return log_oom(); |
| 1462 |
|
| 1463 |
irqs[irq_count++] = irq; |
| 1464 |
} |
| 1465 |
|
| 1466 |
if (irq_count == 0) { |
| 1467 |
log_link_debug(link, "No IRQs found, skipping spread."); |
| 1468 |
return 0; |
| 1469 |
} |
| 1470 |
|
| 1471 |
typesafe_qsort(irqs, irq_count, cmp_unsigned); |
| 1472 |
|
| 1473 |
r = discover_cpu_topology(&topology, &topology_count); |
| 1474 |
if (r < 0) |
| 1475 |
return log_link_error_errno(link, r, "Failed to discover CPU topology: %m"); |
| 1476 |
|
| 1477 |
/* Filter topology by allowed CPUs if specified */ |
| 1478 |
if (allowed_cpus && allowed_cpus->set) { |
| 1479 |
_cleanup_free_ CPUTopology *filtered_topology = new(CPUTopology, topology_count); |
| 1480 |
size_t filtered_count = 0; |
| 1481 |
|
| 1482 |
if (!filtered_topology) |
| 1483 |
return log_oom(); |
| 1484 |
|
| 1485 |
for (size_t i = 0; i < topology_count; i++) |
| 1486 |
if (CPU_ISSET_S(topology[i].cpu, allowed_cpus->allocated, allowed_cpus->set)) |
| 1487 |
filtered_topology[filtered_count++] = topology[i]; |
| 1488 |
|
| 1489 |
if (filtered_count == 0) { |
| 1490 |
log_link_warning(link, "IRQAffinity= filter excludes all CPUs, skipping spread."); |
| 1491 |
return 0; |
| 1492 |
} |
| 1493 |
|
| 1494 |
log_link_debug(link, "Filtered to %zu CPUs (from %zu) based on IRQAffinity=.", filtered_count, topology_count); |
| 1495 |
|
| 1496 |
free_and_replace(topology, filtered_topology); |
| 1497 |
topology_count = filtered_count; |
| 1498 |
} |
| 1499 |
|
| 1500 |
log_link_debug(link, "Spreading %zu IRQs across %zu CPUs.", irq_count, topology_count); |
| 1501 |
|
| 1502 |
/* Select CPUs using maximum distance algorithm */ |
| 1503 |
r = select_spread_cpus(topology, topology_count, irq_count, &spread_cpus, &spread_count); |
| 1504 |
if (r < 0) |
| 1505 |
return log_link_error_errno(link, r, "Failed to select spread CPUs: %m"); |
| 1506 |
|
| 1507 |
for (size_t i = 0; i < spread_count; i++) |
| 1508 |
(void) set_irq_affinity(link, irqs[i], spread_cpus[i]); |
| 1509 |
|
| 1510 |
log_link_info(link, "Applied IRQ affinity policy 'spread' across %zu CPUs for %zu IRQs.", |
| 1511 |
MIN(topology_count, irq_count), irq_count); |
| 1512 |
|
| 1513 |
return 0; |
| 1514 |
} |
| 1515 |
|
| 1516 |
static int link_apply_irq_affinity_single(Link *link, const CPUSet *allowed_cpus) { |
| 1517 |
_cleanup_closedir_ DIR *dir = NULL; |
| 1518 |
unsigned target_cpu = 0; |
| 1519 |
int r; |
| 1520 |
|
| 1521 |
assert(link); |
| 1522 |
assert(link->config); |
| 1523 |
assert(ASSERT_PTR(link->event)->dev); |
| 1524 |
|
| 1525 |
/* If IRQAffinity= is specified, use the first allowed CPU instead of CPU 0 */ |
| 1526 |
if (allowed_cpus && allowed_cpus->set) { |
| 1527 |
bool found = false; |
| 1528 |
|
| 1529 |
for (unsigned cpu = 0; cpu < allowed_cpus->allocated * 8; cpu++) |
| 1530 |
if (CPU_ISSET_S(cpu, allowed_cpus->allocated, allowed_cpus->set)) { |
| 1531 |
target_cpu = cpu; |
| 1532 |
found = true; |
| 1533 |
break; |
| 1534 |
} |
| 1535 |
|
| 1536 |
if (!found) { |
| 1537 |
log_link_warning(link, "IRQAffinity= filter excludes all CPUs, skipping single."); |
| 1538 |
return 0; |
| 1539 |
} |
| 1540 |
} |
| 1541 |
|
| 1542 |
r = device_opendir(link->event->dev, "device/msi_irqs", &dir); |
| 1543 |
if (r < 0) { |
| 1544 |
if (r != -ENOENT) |
| 1545 |
return log_link_error_errno(link, r, "Failed to open device/msi_irqs: %m"); |
| 1546 |
log_link_debug_errno(link, r, "No MSI IRQs found, skipping IRQ affinity configuration: %m"); |
| 1547 |
return 0; |
| 1548 |
} |
| 1549 |
|
| 1550 |
FOREACH_DIRENT(de, dir, return log_link_error_errno(link, errno, "Failed to read directory device/msi_irqs: %m")) { |
| 1551 |
unsigned irq; |
| 1552 |
|
| 1553 |
r = safe_atou(de->d_name, &irq); |
| 1554 |
if (r < 0) |
| 1555 |
return log_link_error_errno(link, r, "Failed to convert parse IRQ number: %s", de->d_name); |
| 1556 |
|
| 1557 |
(void) set_irq_affinity(link, irq, target_cpu); |
| 1558 |
} |
| 1559 |
|
| 1560 |
log_link_info(link, "Applied IRQ affinity policy 'single' (pinning to CPU %u).", target_cpu); |
| 1561 |
|
| 1562 |
return 0; |
| 1563 |
} |
| 1564 |
|
| 1565 |
static int link_apply_irq_affinity(Link *link) { |
| 1566 |
_cleanup_(cpu_set_done) CPUSet effective_cpus = {}; |
| 1567 |
const char *syspath; |
| 1568 |
unsigned numa_node = IRQ_AFFINITY_NUMA_UNSET; |
| 1569 |
int r; |
| 1570 |
|
| 1571 |
assert(link); |
| 1572 |
assert(link->config); |
| 1573 |
assert(ASSERT_PTR(link->event)->dev); |
| 1574 |
|
| 1575 |
if (link->event->event_mode != EVENT_UDEV_WORKER) { |
| 1576 |
log_link_debug(link, "Running in test mode, skipping application of IRQ affinity settings."); |
| 1577 |
return 0; |
| 1578 |
} |
| 1579 |
|
| 1580 |
if (link->config->irq_affinity_policy < 0) |
| 1581 |
return 0; |
| 1582 |
|
| 1583 |
r = sd_device_get_syspath(link->event->dev, &syspath); |
| 1584 |
if (r < 0) |
| 1585 |
return log_link_warning_errno(link, r, "Failed to get syspath: %m"); |
| 1586 |
|
| 1587 |
/* Compute effective CPU set from IRQAffinity= and IRQAffinityNUMA= */ |
| 1588 |
if (link->config->irq_affinity_numa != IRQ_AFFINITY_NUMA_UNSET) { |
| 1589 |
_cleanup_(cpu_set_done) CPUSet numa_cpus = {}; |
| 1590 |
|
| 1591 |
/* Resolve "local" to the actual NUMA node */ |
| 1592 |
if (link->config->irq_affinity_numa == IRQ_AFFINITY_NUMA_LOCAL) { |
| 1593 |
r = link_get_device_numa_node(link, &numa_node); |
| 1594 |
if (r < 0) { |
| 1595 |
log_link_warning_errno( |
| 1596 |
link, r, |
| 1597 |
"Failed to determine local NUMA node for device, skipping IRQ affinity configuration: %m"); |
| 1598 |
return 0; |
| 1599 |
} |
| 1600 |
log_link_debug(link, "Device is on NUMA node %u.", numa_node); |
| 1601 |
} else |
| 1602 |
numa_node = link->config->irq_affinity_numa; |
| 1603 |
|
| 1604 |
/* Get CPUs for the NUMA node */ |
| 1605 |
r = numa_node_get_cpus(numa_node, &numa_cpus); |
| 1606 |
if (r < 0) { |
| 1607 |
log_link_warning_errno( |
| 1608 |
link, r, |
| 1609 |
"Failed to get CPUs for NUMA node %u, skipping IRQ affinity configuration: %m", |
| 1610 |
numa_node); |
| 1611 |
return 0; |
| 1612 |
} |
| 1613 |
|
| 1614 |
/* If IRQAffinity= is also specified, compute intersection */ |
| 1615 |
if (link->config->irq_affinity_cpus.set) { |
| 1616 |
/* Compute intersection of IRQAffinity= and NUMA CPUs */ |
| 1617 |
size_t max_allocated = MAX(numa_cpus.allocated, link->config->irq_affinity_cpus.allocated); |
| 1618 |
|
| 1619 |
r = cpu_set_realloc(&effective_cpus, max_allocated * 8); |
| 1620 |
if (r < 0) |
| 1621 |
return log_oom(); |
| 1622 |
|
| 1623 |
for (size_t i = 0; i < max_allocated * 8; i++) { |
| 1624 |
bool in_numa = i < numa_cpus.allocated * 8 && |
| 1625 |
CPU_ISSET_S(i, numa_cpus.allocated, numa_cpus.set); |
| 1626 |
bool in_affinity = i < link->config->irq_affinity_cpus.allocated * 8 && |
| 1627 |
CPU_ISSET_S(i, link->config->irq_affinity_cpus.allocated, link->config->irq_affinity_cpus.set); |
| 1628 |
|
| 1629 |
if (in_numa && in_affinity) { |
| 1630 |
r = cpu_set_add(&effective_cpus, i); |
| 1631 |
if (r < 0) |
| 1632 |
return log_oom(); |
| 1633 |
} |
| 1634 |
} |
| 1635 |
|
| 1636 |
/* Check if intersection is empty */ |
| 1637 |
if (!effective_cpus.set || CPU_COUNT_S(effective_cpus.allocated, effective_cpus.set) == 0) { |
| 1638 |
log_link_warning( |
| 1639 |
link, |
| 1640 |
"IRQAffinity= and IRQAffinityNUMA= intersection is empty, skipping IRQ affinity configuration."); |
| 1641 |
return 0; |
| 1642 |
} |
| 1643 |
|
| 1644 |
log_link_debug(link, "Using intersection of IRQAffinity= and NUMA node %u CPUs.", numa_node); |
| 1645 |
} else { |
| 1646 |
/* Only NUMA filtering, use NUMA CPUs directly */ |
| 1647 |
effective_cpus = TAKE_STRUCT(numa_cpus); |
| 1648 |
log_link_debug(link, "Using CPUs from NUMA node %u.", numa_node); |
| 1649 |
} |
| 1650 |
} else if (link->config->irq_affinity_cpus.set) { |
| 1651 |
/* Only IRQAffinity= specified, copy it */ |
| 1652 |
r = cpu_set_add_set(&effective_cpus, &link->config->irq_affinity_cpus); |
| 1653 |
if (r < 0) |
| 1654 |
return log_oom(); |
| 1655 |
} |
| 1656 |
/* else: no filtering, effective_cpus remains empty (meaning use all CPUs) */ |
| 1657 |
|
| 1658 |
switch (link->config->irq_affinity_policy) { |
| 1659 |
case IRQ_AFFINITY_POLICY_SINGLE: |
| 1660 |
return link_apply_irq_affinity_single(link, effective_cpus.set ? &effective_cpus : NULL); |
| 1661 |
case IRQ_AFFINITY_POLICY_SPREAD: |
| 1662 |
return link_apply_irq_affinity_spread(link, effective_cpus.set ? &effective_cpus : NULL); |
| 1663 |
default: |
| 1664 |
assert_not_reached(); |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
static int link_apply_rps_cpu_mask(Link *link) { |
| 1669 |
_cleanup_free_ char *mask_str = NULL; |
| 1670 |
LinkConfig *config; |
| 1671 |
int r; |
| 1672 |
|
| 1673 |
config = ASSERT_PTR(ASSERT_PTR(link)->config); |
| 1674 |
assert(ASSERT_PTR(link->event)->dev); |
| 1675 |
|
| 1676 |
if (link->event->event_mode != EVENT_UDEV_WORKER) { |
| 1677 |
log_link_debug(link, "Running in test mode, skipping application of RPS setting."); |
| 1678 |
return 0; |
| 1679 |
} |
| 1680 |
|
| 1681 |
/* Skip if the config is not specified. */ |
| 1682 |
if (!config->rps_cpu_mask.set) |
| 1683 |
return 0; |
| 1684 |
|
| 1685 |
mask_str = cpu_set_to_mask_string(&config->rps_cpu_mask); |
| 1686 |
if (!mask_str) |
| 1687 |
return log_oom(); |
| 1688 |
|
| 1689 |
log_link_debug(link, "Applying RPS CPU mask: %s", mask_str); |
| 1690 |
|
| 1691 |
/* Currently, this will set CPU mask to all rx queue of matched device. */ |
| 1692 |
FOREACH_DEVICE_SYSATTR(link->event->dev, attr) { |
| 1693 |
const char *c; |
| 1694 |
|
| 1695 |
c = path_startswith(attr, "queues/"); |
| 1696 |
if (!c) |
| 1697 |
continue; |
| 1698 |
|
| 1699 |
c = startswith(c, "rx-"); |
| 1700 |
if (!c) |
| 1701 |
continue; |
| 1702 |
|
| 1703 |
c += strcspn(c, "/"); |
| 1704 |
|
| 1705 |
if (!path_equal(c, "/rps_cpus")) |
| 1706 |
continue; |
| 1707 |
|
| 1708 |
r = sd_device_set_sysattr_value(link->event->dev, attr, mask_str); |
| 1709 |
if (r < 0) |
| 1710 |
log_link_warning_errno(link, r, "Failed to write %s sysfs attribute, ignoring: %m", attr); |
| 1711 |
} |
| 1712 |
|
| 1713 |
return 0; |
| 1714 |
} |
| 1715 |
|
| 1716 |
static int link_apply_udev_properties(Link *link) { |
| 1717 |
LinkConfig *config = ASSERT_PTR(ASSERT_PTR(link)->config); |
| 1718 |
UdevEvent *event = ASSERT_PTR(link->event); |
| 1719 |
|
| 1720 |
/* 1. apply ImportProperty=. */ |
| 1721 |
STRV_FOREACH(p, config->import_properties) |
| 1722 |
(void) udev_builtin_import_property(event, *p); |
| 1723 |
|
| 1724 |
/* 2. apply Property=. */ |
| 1725 |
STRV_FOREACH(p, config->properties) { |
| 1726 |
_cleanup_free_ char *key = NULL; |
| 1727 |
const char *eq; |
| 1728 |
|
| 1729 |
eq = strchr(*p, '='); |
| 1730 |
if (!eq) |
| 1731 |
continue; |
| 1732 |
|
| 1733 |
key = strndup(*p, eq - *p); |
| 1734 |
if (!key) |
| 1735 |
return log_oom(); |
| 1736 |
|
| 1737 |
(void) udev_builtin_add_property(event, key, eq + 1); |
| 1738 |
} |
| 1739 |
|
| 1740 |
/* 3. apply UnsetProperty=. */ |
| 1741 |
STRV_FOREACH(p, config->unset_properties) |
| 1742 |
(void) udev_builtin_add_property(event, *p, NULL); |
| 1743 |
|
| 1744 |
/* 4. set the default properties. */ |
| 1745 |
(void) udev_builtin_add_property(event, "ID_NET_LINK_FILE", config->filename); |
| 1746 |
|
| 1747 |
_cleanup_free_ char *joined = NULL; |
| 1748 |
STRV_FOREACH(d, config->dropins) { |
| 1749 |
_cleanup_free_ char *escaped = NULL; |
| 1750 |
|
| 1751 |
escaped = xescape(*d, ":"); |
| 1752 |
if (!escaped) |
| 1753 |
return log_oom(); |
| 1754 |
|
| 1755 |
if (!strextend_with_separator(&joined, ":", escaped)) |
| 1756 |
return log_oom(); |
| 1757 |
} |
| 1758 |
|
| 1759 |
(void) udev_builtin_add_property(event, "ID_NET_LINK_FILE_DROPINS", joined); |
| 1760 |
|
| 1761 |
if (link->new_name) |
| 1762 |
(void) udev_builtin_add_property(event, "ID_NET_NAME", link->new_name); |
| 1763 |
|
| 1764 |
return 0; |
| 1765 |
} |
| 1766 |
|
| 1767 |
int link_apply_config(LinkConfigContext *ctx, Link *link) { |
| 1768 |
int r; |
| 1769 |
|
| 1770 |
assert(ctx); |
| 1771 |
assert(link); |
| 1772 |
|
| 1773 |
r = link_apply_ethtool_settings(link, &ctx->ethtool_fd); |
| 1774 |
if (r < 0) |
| 1775 |
return r; |
| 1776 |
|
| 1777 |
r = link_apply_rtnl_settings(link); |
| 1778 |
if (r < 0) |
| 1779 |
return r; |
| 1780 |
|
| 1781 |
r = link_generate_new_name(link); |
| 1782 |
if (r < 0) |
| 1783 |
return r; |
| 1784 |
|
| 1785 |
r = link_generate_alternative_names(link); |
| 1786 |
if (r < 0) |
| 1787 |
return r; |
| 1788 |
|
| 1789 |
r = link_apply_sr_iov_config(link); |
| 1790 |
if (r < 0) |
| 1791 |
return r; |
| 1792 |
|
| 1793 |
r = link_apply_rps_cpu_mask(link); |
| 1794 |
if (r < 0) |
| 1795 |
return r; |
| 1796 |
|
| 1797 |
r = link_apply_irq_affinity(link); |
| 1798 |
if (r < 0) |
| 1799 |
return r; |
| 1800 |
|
| 1801 |
return link_apply_udev_properties(link); |
| 1802 |
} |
| 1803 |
|
| 1804 |
int config_parse_udev_property( |
| 1805 |
const char *unit, |
| 1806 |
const char *filename, |
| 1807 |
unsigned line, |
| 1808 |
const char *section, |
| 1809 |
unsigned section_line, |
| 1810 |
const char *lvalue, |
| 1811 |
int ltype, |
| 1812 |
const char *rvalue, |
| 1813 |
void *data, |
| 1814 |
void *userdata) { |
| 1815 |
|
| 1816 |
char ***properties = ASSERT_PTR(data); |
| 1817 |
int r; |
| 1818 |
|
| 1819 |
assert(filename); |
| 1820 |
assert(lvalue); |
| 1821 |
assert(rvalue); |
| 1822 |
|
| 1823 |
if (isempty(rvalue)) { |
| 1824 |
/* Empty assignment resets the list */ |
| 1825 |
*properties = strv_free(*properties); |
| 1826 |
return 0; |
| 1827 |
} |
| 1828 |
|
| 1829 |
for (const char *p = rvalue;; ) { |
| 1830 |
_cleanup_free_ char *word = NULL, *resolved = NULL, *key = NULL; |
| 1831 |
const char *eq; |
| 1832 |
|
| 1833 |
r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE); |
| 1834 |
if (r == -ENOMEM) |
| 1835 |
return log_oom(); |
| 1836 |
if (r < 0) { |
| 1837 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1838 |
"Invalid syntax, ignoring assignment: %s", rvalue); |
| 1839 |
return 0; |
| 1840 |
} |
| 1841 |
if (r == 0) |
| 1842 |
return 0; |
| 1843 |
|
| 1844 |
r = specifier_printf(word, SIZE_MAX, link_specifier_table, NULL, NULL, &resolved); |
| 1845 |
if (r < 0) { |
| 1846 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1847 |
"Failed to resolve specifiers in %s, ignoring assignment: %m", word); |
| 1848 |
continue; |
| 1849 |
} |
| 1850 |
|
| 1851 |
if (!udev_property_assignment_is_valid(resolved)) { |
| 1852 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1853 |
"Invalid udev property, ignoring assignment: %s", word); |
| 1854 |
continue; |
| 1855 |
} |
| 1856 |
|
| 1857 |
assert_se(eq = strchr(resolved, '=')); |
| 1858 |
key = strndup(resolved, eq - resolved); |
| 1859 |
if (!key) |
| 1860 |
return log_oom(); |
| 1861 |
|
| 1862 |
if (!device_property_can_set(key)) { |
| 1863 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1864 |
"Invalid udev property name '%s', ignoring assignment: %s", key, resolved); |
| 1865 |
continue; |
| 1866 |
} |
| 1867 |
|
| 1868 |
r = strv_env_replace_consume(properties, TAKE_PTR(resolved)); |
| 1869 |
if (r < 0) |
| 1870 |
return log_error_errno(r, "Failed to update properties: %m"); |
| 1871 |
} |
| 1872 |
} |
| 1873 |
|
| 1874 |
int config_parse_udev_property_name( |
| 1875 |
const char *unit, |
| 1876 |
const char *filename, |
| 1877 |
unsigned line, |
| 1878 |
const char *section, |
| 1879 |
unsigned section_line, |
| 1880 |
const char *lvalue, |
| 1881 |
int ltype, |
| 1882 |
const char *rvalue, |
| 1883 |
void *data, |
| 1884 |
void *userdata) { |
| 1885 |
|
| 1886 |
char ***properties = ASSERT_PTR(data); |
| 1887 |
int r; |
| 1888 |
|
| 1889 |
assert(filename); |
| 1890 |
assert(lvalue); |
| 1891 |
assert(rvalue); |
| 1892 |
|
| 1893 |
if (isempty(rvalue)) { |
| 1894 |
/* Empty assignment resets the list */ |
| 1895 |
*properties = strv_free(*properties); |
| 1896 |
return 0; |
| 1897 |
} |
| 1898 |
|
| 1899 |
for (const char *p = rvalue;; ) { |
| 1900 |
_cleanup_free_ char *word = NULL, *resolved = NULL; |
| 1901 |
|
| 1902 |
r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE); |
| 1903 |
if (r == -ENOMEM) |
| 1904 |
return log_oom(); |
| 1905 |
if (r < 0) { |
| 1906 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1907 |
"Invalid syntax, ignoring assignment: %s", rvalue); |
| 1908 |
return 0; |
| 1909 |
} |
| 1910 |
if (r == 0) |
| 1911 |
return 0; |
| 1912 |
|
| 1913 |
r = specifier_printf(word, SIZE_MAX, link_specifier_table, NULL, NULL, &resolved); |
| 1914 |
if (r < 0) { |
| 1915 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1916 |
"Failed to resolve specifiers in %s, ignoring assignment: %m", word); |
| 1917 |
continue; |
| 1918 |
} |
| 1919 |
|
| 1920 |
if (!udev_property_name_is_valid(resolved)) { |
| 1921 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1922 |
"Invalid udev property name, ignoring assignment: %s", resolved); |
| 1923 |
continue; |
| 1924 |
} |
| 1925 |
|
| 1926 |
if (!device_property_can_set(resolved)) { |
| 1927 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1928 |
"Invalid udev property name, ignoring assignment: %s", resolved); |
| 1929 |
continue; |
| 1930 |
} |
| 1931 |
|
| 1932 |
r = strv_consume(properties, TAKE_PTR(resolved)); |
| 1933 |
if (r < 0) |
| 1934 |
return log_error_errno(r, "Failed to update properties: %m"); |
| 1935 |
} |
| 1936 |
} |
| 1937 |
|
| 1938 |
int config_parse_ifalias( |
| 1939 |
const char *unit, |
| 1940 |
const char *filename, |
| 1941 |
unsigned line, |
| 1942 |
const char *section, |
| 1943 |
unsigned section_line, |
| 1944 |
const char *lvalue, |
| 1945 |
int ltype, |
| 1946 |
const char *rvalue, |
| 1947 |
void *data, |
| 1948 |
void *userdata) { |
| 1949 |
|
| 1950 |
char **s = ASSERT_PTR(data); |
| 1951 |
|
| 1952 |
assert(filename); |
| 1953 |
assert(lvalue); |
| 1954 |
assert(rvalue); |
| 1955 |
|
| 1956 |
if (isempty(rvalue)) { |
| 1957 |
*s = mfree(*s); |
| 1958 |
return 0; |
| 1959 |
} |
| 1960 |
|
| 1961 |
if (!ascii_is_valid(rvalue)) { |
| 1962 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1963 |
"Interface alias is not ASCII clean, ignoring assignment: %s", rvalue); |
| 1964 |
return 0; |
| 1965 |
} |
| 1966 |
|
| 1967 |
if (strlen(rvalue) >= IFALIASZ) { |
| 1968 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1969 |
"Interface alias is too long, ignoring assignment: %s", rvalue); |
| 1970 |
return 0; |
| 1971 |
} |
| 1972 |
|
| 1973 |
return free_and_strdup_warn(s, rvalue); |
| 1974 |
} |
| 1975 |
|
| 1976 |
int config_parse_rx_tx_queues( |
| 1977 |
const char *unit, |
| 1978 |
const char *filename, |
| 1979 |
unsigned line, |
| 1980 |
const char *section, |
| 1981 |
unsigned section_line, |
| 1982 |
const char *lvalue, |
| 1983 |
int ltype, |
| 1984 |
const char *rvalue, |
| 1985 |
void *data, |
| 1986 |
void *userdata) { |
| 1987 |
|
| 1988 |
uint32_t k, *v = data; |
| 1989 |
int r; |
| 1990 |
|
| 1991 |
if (isempty(rvalue)) { |
| 1992 |
*v = 0; |
| 1993 |
return 0; |
| 1994 |
} |
| 1995 |
|
| 1996 |
r = safe_atou32(rvalue, &k); |
| 1997 |
if (r < 0) { |
| 1998 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue); |
| 1999 |
return 0; |
| 2000 |
} |
| 2001 |
if (k == 0 || k > 16384) { |
| 2002 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid %s=, ignoring assignment: %s.", lvalue, rvalue); |
| 2003 |
return 0; |
| 2004 |
} |
| 2005 |
|
| 2006 |
*v = k; |
| 2007 |
return 0; |
| 2008 |
} |
| 2009 |
|
| 2010 |
int config_parse_txqueuelen( |
| 2011 |
const char *unit, |
| 2012 |
const char *filename, |
| 2013 |
unsigned line, |
| 2014 |
const char *section, |
| 2015 |
unsigned section_line, |
| 2016 |
const char *lvalue, |
| 2017 |
int ltype, |
| 2018 |
const char *rvalue, |
| 2019 |
void *data, |
| 2020 |
void *userdata) { |
| 2021 |
|
| 2022 |
uint32_t k, *v = data; |
| 2023 |
int r; |
| 2024 |
|
| 2025 |
if (isempty(rvalue)) { |
| 2026 |
*v = UINT32_MAX; |
| 2027 |
return 0; |
| 2028 |
} |
| 2029 |
|
| 2030 |
r = safe_atou32(rvalue, &k); |
| 2031 |
if (r < 0) { |
| 2032 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue); |
| 2033 |
return 0; |
| 2034 |
} |
| 2035 |
if (k == UINT32_MAX) { |
| 2036 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid %s=, ignoring assignment: %s.", lvalue, rvalue); |
| 2037 |
return 0; |
| 2038 |
} |
| 2039 |
|
| 2040 |
*v = k; |
| 2041 |
return 0; |
| 2042 |
} |
| 2043 |
|
| 2044 |
int config_parse_wol_password( |
| 2045 |
const char *unit, |
| 2046 |
const char *filename, |
| 2047 |
unsigned line, |
| 2048 |
const char *section, |
| 2049 |
unsigned section_line, |
| 2050 |
const char *lvalue, |
| 2051 |
int ltype, |
| 2052 |
const char *rvalue, |
| 2053 |
void *data, |
| 2054 |
void *userdata) { |
| 2055 |
|
| 2056 |
LinkConfig *config = ASSERT_PTR(userdata); |
| 2057 |
int r; |
| 2058 |
|
| 2059 |
assert(filename); |
| 2060 |
assert(lvalue); |
| 2061 |
assert(rvalue); |
| 2062 |
|
| 2063 |
if (isempty(rvalue)) { |
| 2064 |
config->wol_password = erase_and_free(config->wol_password); |
| 2065 |
config->wol_password_file = mfree(config->wol_password_file); |
| 2066 |
return 0; |
| 2067 |
} |
| 2068 |
|
| 2069 |
if (path_is_absolute(rvalue) && path_is_safe(rvalue)) { |
| 2070 |
config->wol_password = erase_and_free(config->wol_password); |
| 2071 |
return free_and_strdup_warn(&config->wol_password_file, rvalue); |
| 2072 |
} |
| 2073 |
|
| 2074 |
warn_file_is_world_accessible(filename, NULL, unit, line); |
| 2075 |
|
| 2076 |
r = link_parse_wol_password(config, rvalue); |
| 2077 |
if (r == -ENOMEM) |
| 2078 |
return log_oom(); |
| 2079 |
if (r < 0) { |
| 2080 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2081 |
"Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue); |
| 2082 |
return 0; |
| 2083 |
} |
| 2084 |
|
| 2085 |
config->wol_password_file = mfree(config->wol_password_file); |
| 2086 |
return 0; |
| 2087 |
} |
| 2088 |
|
| 2089 |
int config_parse_rps_cpu_mask( |
| 2090 |
const char *unit, |
| 2091 |
const char *filename, |
| 2092 |
unsigned line, |
| 2093 |
const char *section, |
| 2094 |
unsigned section_line, |
| 2095 |
const char *lvalue, |
| 2096 |
int ltype, |
| 2097 |
const char *rvalue, |
| 2098 |
void *data, |
| 2099 |
void *userdata) { |
| 2100 |
|
| 2101 |
CPUSet *mask = ASSERT_PTR(data); |
| 2102 |
int r; |
| 2103 |
|
| 2104 |
assert(rvalue); |
| 2105 |
|
| 2106 |
if (streq(rvalue, "disable")) { |
| 2107 |
_cleanup_(cpu_set_done) CPUSet c = {}; |
| 2108 |
|
| 2109 |
r = cpu_set_realloc(&c, 1); |
| 2110 |
if (r < 0) |
| 2111 |
return log_oom(); |
| 2112 |
|
| 2113 |
return cpu_set_done_and_replace(*mask, c); |
| 2114 |
} |
| 2115 |
|
| 2116 |
if (streq(rvalue, "all")) { |
| 2117 |
_cleanup_(cpu_set_done) CPUSet c = {}; |
| 2118 |
|
| 2119 |
r = cpu_set_add_all(&c); |
| 2120 |
if (r < 0) { |
| 2121 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2122 |
"Failed to create CPU affinity mask representing \"all\" cpus, ignoring: %m"); |
| 2123 |
return 0; |
| 2124 |
} |
| 2125 |
|
| 2126 |
return cpu_set_done_and_replace(*mask, c); |
| 2127 |
} |
| 2128 |
|
| 2129 |
return config_parse_cpu_set(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata); |
| 2130 |
} |
| 2131 |
|
| 2132 |
static const char* const mac_address_policy_table[_MAC_ADDRESS_POLICY_MAX] = { |
| 2133 |
[MAC_ADDRESS_POLICY_PERSISTENT] = "persistent", |
| 2134 |
[MAC_ADDRESS_POLICY_RANDOM] = "random", |
| 2135 |
[MAC_ADDRESS_POLICY_NONE] = "none", |
| 2136 |
}; |
| 2137 |
|
| 2138 |
DEFINE_STRING_TABLE_LOOKUP(mac_address_policy, MACAddressPolicy); |
| 2139 |
DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT( |
| 2140 |
config_parse_mac_address_policy, |
| 2141 |
mac_address_policy, |
| 2142 |
MACAddressPolicy, |
| 2143 |
MAC_ADDRESS_POLICY_NONE); |
| 2144 |
|
| 2145 |
DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, |
| 2146 |
_NAMEPOLICY_INVALID); |
| 2147 |
|
| 2148 |
DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy, |
| 2149 |
_NAMEPOLICY_INVALID); |
| 2150 |
|
| 2151 |
int config_parse_irq_affinity_numa( |
| 2152 |
const char *unit, |
| 2153 |
const char *filename, |
| 2154 |
unsigned line, |
| 2155 |
const char *section, |
| 2156 |
unsigned section_line, |
| 2157 |
const char *lvalue, |
| 2158 |
int ltype, |
| 2159 |
const char *rvalue, |
| 2160 |
void *data, |
| 2161 |
void *userdata) { |
| 2162 |
|
| 2163 |
unsigned tmp, *numa = ASSERT_PTR(data); |
| 2164 |
int r; |
| 2165 |
|
| 2166 |
assert(filename); |
| 2167 |
assert(lvalue); |
| 2168 |
assert(rvalue); |
| 2169 |
|
| 2170 |
if (isempty(rvalue)) { |
| 2171 |
*numa = IRQ_AFFINITY_NUMA_UNSET; |
| 2172 |
return 0; |
| 2173 |
} |
| 2174 |
|
| 2175 |
if (streq(rvalue, "local")) { |
| 2176 |
*numa = IRQ_AFFINITY_NUMA_LOCAL; |
| 2177 |
return 0; |
| 2178 |
} |
| 2179 |
|
| 2180 |
/* Parse as NUMA node number */ |
| 2181 |
r = safe_atou(rvalue, &tmp); |
| 2182 |
if (r < 0) |
| 2183 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2184 |
|
| 2185 |
/* UINT_MAX and UINT_MAX-1 are used to flag "unset" and "local NUMA node" respectively. */ |
| 2186 |
if (tmp >= IRQ_AFFINITY_NUMA_LOCAL) { |
| 2187 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2188 |
"Invalid NUMA node number %u, ignoring assignment: %s", tmp, rvalue); |
| 2189 |
return 0; |
| 2190 |
} |
| 2191 |
|
| 2192 |
*numa = tmp; |
| 2193 |
|
| 2194 |
return 0; |
| 2195 |
} |
| 2196 |
|
| 2197 |
static const char* const irq_affinity_policy_table[_IRQ_AFFINITY_POLICY_MAX] = { |
| 2198 |
[IRQ_AFFINITY_POLICY_SINGLE] = "single", |
| 2199 |
[IRQ_AFFINITY_POLICY_SPREAD] = "spread", |
| 2200 |
}; |
| 2201 |
|
| 2202 |
DEFINE_STRING_TABLE_LOOKUP(irq_affinity_policy, IRQAffinityPolicy); |
| 2203 |
DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT( |
| 2204 |
config_parse_irq_affinity_policy, |
| 2205 |
irq_affinity_policy, |
| 2206 |
IRQAffinityPolicy, |
| 2207 |
_IRQ_AFFINITY_POLICY_INVALID); |