]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/netif-naming-scheme.c
Merge pull request #20846 from yuwata/sd-lldp-tx
[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
8static const NamingScheme naming_schemes[] = {
9 { "v238", NAMING_V238 },
10 { "v239", NAMING_V239 },
11 { "v240", NAMING_V240 },
96848152 12 { "v241", NAMING_V241 },
eaa9d507 13 { "v243", NAMING_V243 },
bc5ea049 14 { "v245", NAMING_V245 },
66ad93e8 15 { "v247", NAMING_V247 },
a496a238 16 { "v249", NAMING_V249 },
35b35190 17 /* … add more schemes here, as the logic to name devices is updated … */
c3138b46 18 /* also remember to update the list of options in meson_options.txt */
35b35190
ZJS
19};
20
21static const NamingScheme* naming_scheme_from_name(const char *name) {
22 size_t i;
23
24 if (streq(name, "latest"))
25 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
26
27 for (i = 0; i < ELEMENTSOF(naming_schemes); i++)
28 if (streq(naming_schemes[i].name, name))
29 return naming_schemes + i;
30
31 return NULL;
32}
33
34const NamingScheme* naming_scheme(void) {
35 static const NamingScheme *cache = NULL;
36 _cleanup_free_ char *buffer = NULL;
37 const char *e, *k;
38
39 if (cache)
40 return cache;
41
42 /* Acquire setting from the kernel command line */
43 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer);
44
45 /* Also acquire it from an env var */
46 e = getenv("NET_NAMING_SCHEME");
47 if (e) {
48 if (*e == ':') {
49 /* If prefixed with ':' the kernel cmdline takes precedence */
50 k = buffer ?: e + 1;
51 } else
52 k = e; /* Otherwise the env var takes precedence */
53 } else
54 k = buffer;
55
56 if (k) {
57 cache = naming_scheme_from_name(k);
58 if (cache) {
59 log_info("Using interface naming scheme '%s'.", cache->name);
60 return cache;
61 }
62
63 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
64 }
65
66 cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME);
67 assert(cache);
68 log_info("Using default interface naming scheme '%s'.", cache->name);
69
70 return cache;
71}