]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/netif-naming-scheme.c
Merge pull request #21167 from poettering/various-doc-tweaks
[thirdparty/systemd.git] / src / shared / netif-naming-scheme.c
CommitLineData
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
77faadfd
ZJS
8#ifdef _DEFAULT_NET_NAMING_SCHEME_TEST
9/* The primary purpose of this check is to verify that _DEFAULT_NET_NAMING_SCHEME_TEST
10 * is a valid identifier. If an invalid name is given during configuration, this will
11 * fail with a name error. */
12assert_cc(_DEFAULT_NET_NAMING_SCHEME_TEST >= 0);
13#endif
14
35b35190
ZJS
15static const NamingScheme naming_schemes[] = {
16 { "v238", NAMING_V238 },
17 { "v239", NAMING_V239 },
18 { "v240", NAMING_V240 },
96848152 19 { "v241", NAMING_V241 },
eaa9d507 20 { "v243", NAMING_V243 },
bc5ea049 21 { "v245", NAMING_V245 },
66ad93e8 22 { "v247", NAMING_V247 },
a496a238 23 { "v249", NAMING_V249 },
35b35190 24 /* … add more schemes here, as the logic to name devices is updated … */
681cb84a
ZJS
25
26 EXTRA_NET_NAMING_MAP
35b35190
ZJS
27};
28
77faadfd 29const NamingScheme* naming_scheme_from_name(const char *name) {
681cb84a
ZJS
30 /* "latest" may either be defined explicitly by the extra map, in which case we we will find it in
31 * the table like any other name. After iterating through the table, we check for "latest" again,
32 * which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */
35b35190 33
acaa6368 34 for (size_t i = 0; i < ELEMENTSOF(naming_schemes); i++)
35b35190
ZJS
35 if (streq(naming_schemes[i].name, name))
36 return naming_schemes + i;
37
681cb84a
ZJS
38 if (streq(name, "latest"))
39 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
40
35b35190
ZJS
41 return NULL;
42}
43
44const NamingScheme* naming_scheme(void) {
45 static const NamingScheme *cache = NULL;
46 _cleanup_free_ char *buffer = NULL;
47 const char *e, *k;
48
49 if (cache)
50 return cache;
51
52 /* Acquire setting from the kernel command line */
53 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer);
54
55 /* Also acquire it from an env var */
56 e = getenv("NET_NAMING_SCHEME");
57 if (e) {
58 if (*e == ':') {
59 /* If prefixed with ':' the kernel cmdline takes precedence */
60 k = buffer ?: e + 1;
61 } else
62 k = e; /* Otherwise the env var takes precedence */
63 } else
64 k = buffer;
65
66 if (k) {
67 cache = naming_scheme_from_name(k);
68 if (cache) {
69 log_info("Using interface naming scheme '%s'.", cache->name);
70 return cache;
71 }
72
73 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
74 }
75
76 cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME);
77 assert(cache);
78 log_info("Using default interface naming scheme '%s'.", cache->name);
79
80 return cache;
81}