]>
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" | |
ff516b43 | 7 | #include "string-table.h" |
35b35190 | 8 | |
77faadfd ZJS |
9 | #ifdef _DEFAULT_NET_NAMING_SCHEME_TEST |
10 | /* The primary purpose of this check is to verify that _DEFAULT_NET_NAMING_SCHEME_TEST | |
11 | * is a valid identifier. If an invalid name is given during configuration, this will | |
12 | * fail with a name error. */ | |
13 | assert_cc(_DEFAULT_NET_NAMING_SCHEME_TEST >= 0); | |
14 | #endif | |
15 | ||
35b35190 ZJS |
16 | static const NamingScheme naming_schemes[] = { |
17 | { "v238", NAMING_V238 }, | |
18 | { "v239", NAMING_V239 }, | |
19 | { "v240", NAMING_V240 }, | |
96848152 | 20 | { "v241", NAMING_V241 }, |
eaa9d507 | 21 | { "v243", NAMING_V243 }, |
bc5ea049 | 22 | { "v245", NAMING_V245 }, |
66ad93e8 | 23 | { "v247", NAMING_V247 }, |
a496a238 | 24 | { "v249", NAMING_V249 }, |
d6eda677 | 25 | { "v250", NAMING_V250 }, |
35b35190 | 26 | /* … add more schemes here, as the logic to name devices is updated … */ |
681cb84a ZJS |
27 | |
28 | EXTRA_NET_NAMING_MAP | |
35b35190 ZJS |
29 | }; |
30 | ||
77faadfd | 31 | const NamingScheme* naming_scheme_from_name(const char *name) { |
ad337e55 | 32 | /* "latest" may either be defined explicitly by the extra map, in which case we will find it in |
681cb84a ZJS |
33 | * the table like any other name. After iterating through the table, we check for "latest" again, |
34 | * which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */ | |
35b35190 | 35 | |
acaa6368 | 36 | for (size_t i = 0; i < ELEMENTSOF(naming_schemes); i++) |
35b35190 ZJS |
37 | if (streq(naming_schemes[i].name, name)) |
38 | return naming_schemes + i; | |
39 | ||
681cb84a ZJS |
40 | if (streq(name, "latest")) |
41 | return naming_schemes + ELEMENTSOF(naming_schemes) - 1; | |
42 | ||
35b35190 ZJS |
43 | return NULL; |
44 | } | |
45 | ||
46 | const NamingScheme* naming_scheme(void) { | |
47 | static const NamingScheme *cache = NULL; | |
48 | _cleanup_free_ char *buffer = NULL; | |
49 | const char *e, *k; | |
50 | ||
51 | if (cache) | |
52 | return cache; | |
53 | ||
54 | /* Acquire setting from the kernel command line */ | |
55 | (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer); | |
56 | ||
57 | /* Also acquire it from an env var */ | |
58 | e = getenv("NET_NAMING_SCHEME"); | |
59 | if (e) { | |
60 | if (*e == ':') { | |
61 | /* If prefixed with ':' the kernel cmdline takes precedence */ | |
62 | k = buffer ?: e + 1; | |
63 | } else | |
64 | k = e; /* Otherwise the env var takes precedence */ | |
65 | } else | |
66 | k = buffer; | |
67 | ||
68 | if (k) { | |
69 | cache = naming_scheme_from_name(k); | |
70 | if (cache) { | |
71 | log_info("Using interface naming scheme '%s'.", cache->name); | |
72 | return cache; | |
73 | } | |
74 | ||
75 | log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k); | |
76 | } | |
77 | ||
78 | cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME); | |
79 | assert(cache); | |
80 | log_info("Using default interface naming scheme '%s'.", cache->name); | |
81 | ||
82 | return cache; | |
83 | } | |
ff516b43 YW |
84 | |
85 | static const char* const name_policy_table[_NAMEPOLICY_MAX] = { | |
86 | [NAMEPOLICY_KERNEL] = "kernel", | |
87 | [NAMEPOLICY_KEEP] = "keep", | |
88 | [NAMEPOLICY_DATABASE] = "database", | |
89 | [NAMEPOLICY_ONBOARD] = "onboard", | |
90 | [NAMEPOLICY_SLOT] = "slot", | |
91 | [NAMEPOLICY_PATH] = "path", | |
92 | [NAMEPOLICY_MAC] = "mac", | |
93 | }; | |
94 | ||
95 | DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy); | |
96 | ||
97 | static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = { | |
98 | [NAMEPOLICY_DATABASE] = "database", | |
99 | [NAMEPOLICY_ONBOARD] = "onboard", | |
100 | [NAMEPOLICY_SLOT] = "slot", | |
101 | [NAMEPOLICY_PATH] = "path", | |
102 | [NAMEPOLICY_MAC] = "mac", | |
103 | }; | |
104 | ||
105 | DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy); |