]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include "alloc-util.h" | |
4 | #include "netif-naming-scheme.h" | |
5 | #include "proc-cmdline.h" | |
6 | #include "string-util.h" | |
7 | #include "string-table.h" | |
8 | ||
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 | ||
16 | static const NamingScheme naming_schemes[] = { | |
17 | { "v238", NAMING_V238 }, | |
18 | { "v239", NAMING_V239 }, | |
19 | { "v240", NAMING_V240 }, | |
20 | { "v241", NAMING_V241 }, | |
21 | { "v243", NAMING_V243 }, | |
22 | { "v245", NAMING_V245 }, | |
23 | { "v247", NAMING_V247 }, | |
24 | { "v249", NAMING_V249 }, | |
25 | { "v250", NAMING_V250 }, | |
26 | { "v251", NAMING_V251 }, | |
27 | { "v252", NAMING_V252 }, | |
28 | { "v253", NAMING_V253 }, | |
29 | /* … add more schemes here, as the logic to name devices is updated … */ | |
30 | ||
31 | EXTRA_NET_NAMING_MAP | |
32 | }; | |
33 | ||
34 | const NamingScheme* naming_scheme_from_name(const char *name) { | |
35 | /* "latest" may either be defined explicitly by the extra map, in which case we will find it in | |
36 | * the table like any other name. After iterating through the table, we check for "latest" again, | |
37 | * which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */ | |
38 | ||
39 | for (size_t i = 0; i < ELEMENTSOF(naming_schemes); i++) | |
40 | if (streq(naming_schemes[i].name, name)) | |
41 | return naming_schemes + i; | |
42 | ||
43 | if (streq(name, "latest")) | |
44 | return naming_schemes + ELEMENTSOF(naming_schemes) - 1; | |
45 | ||
46 | return NULL; | |
47 | } | |
48 | ||
49 | const NamingScheme* naming_scheme(void) { | |
50 | static const NamingScheme *cache = NULL; | |
51 | _cleanup_free_ char *buffer = NULL; | |
52 | const char *e, *k; | |
53 | ||
54 | if (cache) | |
55 | return cache; | |
56 | ||
57 | /* Acquire setting from the kernel command line */ | |
58 | (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer); | |
59 | ||
60 | /* Also acquire it from an env var */ | |
61 | e = getenv("NET_NAMING_SCHEME"); | |
62 | if (e) { | |
63 | if (*e == ':') { | |
64 | /* If prefixed with ':' the kernel cmdline takes precedence */ | |
65 | k = buffer ?: e + 1; | |
66 | } else | |
67 | k = e; /* Otherwise the env var takes precedence */ | |
68 | } else | |
69 | k = buffer; | |
70 | ||
71 | if (k) { | |
72 | cache = naming_scheme_from_name(k); | |
73 | if (cache) { | |
74 | log_info("Using interface naming scheme '%s'.", cache->name); | |
75 | return cache; | |
76 | } | |
77 | ||
78 | log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k); | |
79 | } | |
80 | ||
81 | cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME); | |
82 | assert(cache); | |
83 | log_info("Using default interface naming scheme '%s'.", cache->name); | |
84 | ||
85 | return cache; | |
86 | } | |
87 | ||
88 | static const char* const name_policy_table[_NAMEPOLICY_MAX] = { | |
89 | [NAMEPOLICY_KERNEL] = "kernel", | |
90 | [NAMEPOLICY_KEEP] = "keep", | |
91 | [NAMEPOLICY_DATABASE] = "database", | |
92 | [NAMEPOLICY_ONBOARD] = "onboard", | |
93 | [NAMEPOLICY_SLOT] = "slot", | |
94 | [NAMEPOLICY_PATH] = "path", | |
95 | [NAMEPOLICY_MAC] = "mac", | |
96 | }; | |
97 | ||
98 | DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy); | |
99 | ||
100 | static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = { | |
101 | [NAMEPOLICY_DATABASE] = "database", | |
102 | [NAMEPOLICY_ONBOARD] = "onboard", | |
103 | [NAMEPOLICY_SLOT] = "slot", | |
104 | [NAMEPOLICY_PATH] = "path", | |
105 | [NAMEPOLICY_MAC] = "mac", | |
106 | }; | |
107 | ||
108 | DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy); |