]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/netif-naming-scheme.c
Merge pull request #14465 from poettering/setprio-rework
[thirdparty/systemd.git] / src / shared / netif-naming-scheme.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "netif-naming-scheme.h"
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 },
12 { "v241", NAMING_V241 },
13 { "v243", NAMING_V243 },
14 { "v245", NAMING_V245 },
15 /* … add more schemes here, as the logic to name devices is updated … */
16 };
17
18 static const NamingScheme* naming_scheme_from_name(const char *name) {
19 size_t i;
20
21 if (streq(name, "latest"))
22 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
23
24 for (i = 0; i < ELEMENTSOF(naming_schemes); i++)
25 if (streq(naming_schemes[i].name, name))
26 return naming_schemes + i;
27
28 return NULL;
29 }
30
31 const NamingScheme* naming_scheme(void) {
32 static const NamingScheme *cache = NULL;
33 _cleanup_free_ char *buffer = NULL;
34 const char *e, *k;
35
36 if (cache)
37 return cache;
38
39 /* Acquire setting from the kernel command line */
40 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer);
41
42 /* Also acquire it from an env var */
43 e = getenv("NET_NAMING_SCHEME");
44 if (e) {
45 if (*e == ':') {
46 /* If prefixed with ':' the kernel cmdline takes precedence */
47 k = buffer ?: e + 1;
48 } else
49 k = e; /* Otherwise the env var takes precedence */
50 } else
51 k = buffer;
52
53 if (k) {
54 cache = naming_scheme_from_name(k);
55 if (cache) {
56 log_info("Using interface naming scheme '%s'.", cache->name);
57 return cache;
58 }
59
60 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
61 }
62
63 cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME);
64 assert(cache);
65 log_info("Using default interface naming scheme '%s'.", cache->name);
66
67 return cache;
68 }