1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
9 static const NamingScheme naming_schemes
[] = {
10 { "v238", NAMING_V238
},
11 { "v239", NAMING_V239
},
12 { "v240", NAMING_V240
},
13 { "v241", NAMING_V241
},
14 { "v243", NAMING_V243
},
15 { "v245", NAMING_V245
},
16 { "v247", NAMING_V247
},
17 { "v249", NAMING_V249
},
18 { "v250", NAMING_V250
},
19 { "v251", NAMING_V251
},
20 { "v252", NAMING_V252
},
21 { "v253", NAMING_V253
},
22 { "v254", NAMING_V254
},
23 { "v255", NAMING_V255
},
24 /* … add more schemes here, as the logic to name devices is updated … */
29 const NamingScheme
* naming_scheme_from_name(const char *name
) {
30 /* "latest" may either be defined explicitly by the extra map, in which case 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. */
34 for (size_t i
= 0; i
< ELEMENTSOF(naming_schemes
); i
++)
35 if (streq(naming_schemes
[i
].name
, name
))
36 return naming_schemes
+ i
;
38 if (streq(name
, "latest"))
39 return naming_schemes
+ ELEMENTSOF(naming_schemes
) - 1;
44 const NamingScheme
* naming_scheme(void) {
45 static const NamingScheme
*cache
= NULL
;
46 _cleanup_free_
char *buffer
= NULL
;
52 /* Acquire setting from the kernel command line */
53 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer
);
55 /* Also acquire it from an env var */
56 e
= getenv("NET_NAMING_SCHEME");
59 /* If prefixed with ':' the kernel cmdline takes precedence */
62 k
= e
; /* Otherwise the env var takes precedence */
67 cache
= naming_scheme_from_name(k
);
69 log_info("Using interface naming scheme '%s'.", cache
->name
);
73 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k
);
76 cache
= naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME
);
78 log_info("Using default interface naming scheme '%s'.", cache
->name
);
83 static const char* const name_policy_table
[_NAMEPOLICY_MAX
] = {
84 [NAMEPOLICY_KERNEL
] = "kernel",
85 [NAMEPOLICY_KEEP
] = "keep",
86 [NAMEPOLICY_DATABASE
] = "database",
87 [NAMEPOLICY_ONBOARD
] = "onboard",
88 [NAMEPOLICY_SLOT
] = "slot",
89 [NAMEPOLICY_PATH
] = "path",
90 [NAMEPOLICY_MAC
] = "mac",
93 DEFINE_STRING_TABLE_LOOKUP(name_policy
, NamePolicy
);
95 static const char* const alternative_names_policy_table
[_NAMEPOLICY_MAX
] = {
96 [NAMEPOLICY_DATABASE
] = "database",
97 [NAMEPOLICY_ONBOARD
] = "onboard",
98 [NAMEPOLICY_SLOT
] = "slot",
99 [NAMEPOLICY_PATH
] = "path",
100 [NAMEPOLICY_MAC
] = "mac",
103 DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy
, NamePolicy
);