]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/netif-naming-scheme.c
NEWS, man: move description of SR-IOV-R net naming to v255
[thirdparty/systemd.git] / src / shared / netif-naming-scheme.c
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 { "v254", NAMING_V254 },
30 { "v255", NAMING_V255 },
31 /* … add more schemes here, as the logic to name devices is updated … */
32
33 EXTRA_NET_NAMING_MAP
34 };
35
36 const NamingScheme* naming_scheme_from_name(const char *name) {
37 /* "latest" may either be defined explicitly by the extra map, in which case we will find it in
38 * the table like any other name. After iterating through the table, we check for "latest" again,
39 * which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */
40
41 for (size_t i = 0; i < ELEMENTSOF(naming_schemes); i++)
42 if (streq(naming_schemes[i].name, name))
43 return naming_schemes + i;
44
45 if (streq(name, "latest"))
46 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
47
48 return NULL;
49 }
50
51 const NamingScheme* naming_scheme(void) {
52 static const NamingScheme *cache = NULL;
53 _cleanup_free_ char *buffer = NULL;
54 const char *e, *k;
55
56 if (cache)
57 return cache;
58
59 /* Acquire setting from the kernel command line */
60 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer);
61
62 /* Also acquire it from an env var */
63 e = getenv("NET_NAMING_SCHEME");
64 if (e) {
65 if (*e == ':') {
66 /* If prefixed with ':' the kernel cmdline takes precedence */
67 k = buffer ?: e + 1;
68 } else
69 k = e; /* Otherwise the env var takes precedence */
70 } else
71 k = buffer;
72
73 if (k) {
74 cache = naming_scheme_from_name(k);
75 if (cache) {
76 log_info("Using interface naming scheme '%s'.", cache->name);
77 return cache;
78 }
79
80 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
81 }
82
83 cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME);
84 assert(cache);
85 log_info("Using default interface naming scheme '%s'.", cache->name);
86
87 return cache;
88 }
89
90 static const char* const name_policy_table[_NAMEPOLICY_MAX] = {
91 [NAMEPOLICY_KERNEL] = "kernel",
92 [NAMEPOLICY_KEEP] = "keep",
93 [NAMEPOLICY_DATABASE] = "database",
94 [NAMEPOLICY_ONBOARD] = "onboard",
95 [NAMEPOLICY_SLOT] = "slot",
96 [NAMEPOLICY_PATH] = "path",
97 [NAMEPOLICY_MAC] = "mac",
98 };
99
100 DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
101
102 static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = {
103 [NAMEPOLICY_DATABASE] = "database",
104 [NAMEPOLICY_ONBOARD] = "onboard",
105 [NAMEPOLICY_SLOT] = "slot",
106 [NAMEPOLICY_PATH] = "path",
107 [NAMEPOLICY_MAC] = "mac",
108 };
109
110 DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy);