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