]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
b355d0c9 | 2 | |
35b35190 | 3 | #include "alloc-util.h" |
b355d0c9 | 4 | #include "netif-naming-scheme.h" |
35b35190 ZJS |
5 | #include "proc-cmdline.h" |
6 | #include "string-util.h" | |
7 | ||
8 | static const NamingScheme naming_schemes[] = { | |
9 | { "v238", NAMING_V238 }, | |
10 | { "v239", NAMING_V239 }, | |
11 | { "v240", NAMING_V240 }, | |
96848152 | 12 | { "v241", NAMING_V241 }, |
eaa9d507 | 13 | { "v243", NAMING_V243 }, |
bc5ea049 | 14 | { "v245", NAMING_V245 }, |
66ad93e8 | 15 | { "v247", NAMING_V247 }, |
a496a238 | 16 | { "v249", NAMING_V249 }, |
35b35190 | 17 | /* … add more schemes here, as the logic to name devices is updated … */ |
c3138b46 | 18 | /* also remember to update the list of options in meson_options.txt */ |
35b35190 ZJS |
19 | }; |
20 | ||
21 | static const NamingScheme* naming_scheme_from_name(const char *name) { | |
35b35190 ZJS |
22 | if (streq(name, "latest")) |
23 | return naming_schemes + ELEMENTSOF(naming_schemes) - 1; | |
24 | ||
acaa6368 | 25 | for (size_t i = 0; i < ELEMENTSOF(naming_schemes); i++) |
35b35190 ZJS |
26 | if (streq(naming_schemes[i].name, name)) |
27 | return naming_schemes + i; | |
28 | ||
29 | return NULL; | |
30 | } | |
31 | ||
32 | const NamingScheme* naming_scheme(void) { | |
33 | static const NamingScheme *cache = NULL; | |
34 | _cleanup_free_ char *buffer = NULL; | |
35 | const char *e, *k; | |
36 | ||
37 | if (cache) | |
38 | return cache; | |
39 | ||
40 | /* Acquire setting from the kernel command line */ | |
41 | (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer); | |
42 | ||
43 | /* Also acquire it from an env var */ | |
44 | e = getenv("NET_NAMING_SCHEME"); | |
45 | if (e) { | |
46 | if (*e == ':') { | |
47 | /* If prefixed with ':' the kernel cmdline takes precedence */ | |
48 | k = buffer ?: e + 1; | |
49 | } else | |
50 | k = e; /* Otherwise the env var takes precedence */ | |
51 | } else | |
52 | k = buffer; | |
53 | ||
54 | if (k) { | |
55 | cache = naming_scheme_from_name(k); | |
56 | if (cache) { | |
57 | log_info("Using interface naming scheme '%s'.", cache->name); | |
58 | return cache; | |
59 | } | |
60 | ||
61 | log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k); | |
62 | } | |
63 | ||
64 | cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME); | |
65 | assert(cache); | |
66 | log_info("Using default interface naming scheme '%s'.", cache->name); | |
67 | ||
68 | return cache; | |
69 | } |