1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include "sd-netlink.h"
6 #include "conf-parser.h"
8 #include "../networkd-link.h"
11 typedef struct netdev_join_callback netdev_join_callback
;
13 struct netdev_join_callback
{
14 link_netlink_message_handler_t callback
;
17 LIST_FIELDS(netdev_join_callback
, callbacks
);
20 typedef enum NetDevKind
{
32 NETDEV_KIND_IP6GRETAP
,
45 NETDEV_KIND_WIREGUARD
,
46 NETDEV_KIND_NETDEVSIM
,
50 _NETDEV_KIND_INVALID
= -1
53 typedef enum NetDevState
{
56 NETDEV_STATE_CREATING
,
60 _NETDEV_STATE_INVALID
= -1,
63 typedef enum NetDevCreateType
{
64 NETDEV_CREATE_INDEPENDENT
,
66 NETDEV_CREATE_STACKED
,
68 _NETDEV_CREATE_INVALID
= -1,
71 typedef struct Manager Manager
;
72 typedef struct Condition Condition
;
74 typedef struct NetDev
{
81 Condition
*match_host
;
82 Condition
*match_virt
;
83 Condition
*match_kernel_cmdline
;
84 Condition
*match_kernel_version
;
85 Condition
*match_arch
;
91 struct ether_addr
*mac
;
95 LIST_HEAD(netdev_join_callback
, callbacks
);
98 typedef struct NetDevVTable
{
99 /* How much memory does an object of this unit type need */
102 /* Config file sections this netdev kind understands, separated
104 const char *sections
;
106 /* This should reset all type-specific variables. This should
107 * not allocate memory, and is called with zero-initialized
108 * data. It should hence only initialize variables that need
110 void (*init
)(NetDev
*n
);
112 /* This should free all kind-specific variables. It should be
114 void (*done
)(NetDev
*n
);
116 /* fill in message to create netdev */
117 int (*fill_message_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
119 /* specifies if netdev is independent, or a master device or a stacked device */
120 NetDevCreateType create_type
;
122 /* create netdev, if not done via rtnl */
123 int (*create
)(NetDev
*netdev
);
125 /* perform additional configuration after netdev has been createad */
126 int (*post_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
128 /* verify that compulsory configuration options were specified */
129 int (*config_verify
)(NetDev
*netdev
, const char *filename
);
132 extern const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
];
134 #define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
136 /* For casting a netdev into the various netdev kinds */
137 #define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
138 static inline MixedCase* UPPERCASE(NetDev *n) { \
139 if (_unlikely_(!n || \
140 n->kind != NETDEV_KIND_##UPPERCASE) || \
141 n->state == _NETDEV_STATE_INVALID) \
144 return (MixedCase*) n; \
147 /* For casting the various netdev kinds into a netdev */
148 #define NETDEV(n) (&(n)->meta)
150 int netdev_load(Manager
*manager
);
151 int netdev_load_one(Manager
*manager
, const char *filename
);
152 void netdev_drop(NetDev
*netdev
);
154 NetDev
*netdev_unref(NetDev
*netdev
);
155 NetDev
*netdev_ref(NetDev
*netdev
);
156 DEFINE_TRIVIAL_DESTRUCTOR(netdev_destroy_callback
, NetDev
, netdev_unref
);
157 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev
*, netdev_unref
);
159 int netdev_get(Manager
*manager
, const char *name
, NetDev
**ret
);
160 int netdev_set_ifindex(NetDev
*netdev
, sd_netlink_message
*newlink
);
161 int netdev_get_mac(const char *ifname
, struct ether_addr
**ret
);
162 int netdev_join(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t cb
);
164 const char *netdev_kind_to_string(NetDevKind d
) _const_
;
165 NetDevKind
netdev_kind_from_string(const char *d
) _pure_
;
167 CONFIG_PARSER_PROTOTYPE(config_parse_netdev_kind
);
170 const struct ConfigPerfItem
* network_netdev_gperf_lookup(const char *key
, GPERF_LEN_TYPE length
);
172 /* Macros which append INTERFACE= to the message */
174 #define log_netdev_full(netdev, level, error, ...) \
176 const NetDev *_n = (netdev); \
177 _n ? log_object_internal(level, error, __FILE__, __LINE__, __func__, "INTERFACE=", _n->ifname, NULL, NULL, ##__VA_ARGS__) : \
178 log_internal(level, error, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
181 #define log_netdev_debug(netdev, ...) log_netdev_full(netdev, LOG_DEBUG, 0, ##__VA_ARGS__)
182 #define log_netdev_info(netdev, ...) log_netdev_full(netdev, LOG_INFO, 0, ##__VA_ARGS__)
183 #define log_netdev_notice(netdev, ...) log_netdev_full(netdev, LOG_NOTICE, 0, ##__VA_ARGS__)
184 #define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING, 0, ## __VA_ARGS__)
185 #define log_netdev_error(netdev, ...) log_netdev_full(netdev, LOG_ERR, 0, ##__VA_ARGS__)
187 #define log_netdev_debug_errno(netdev, error, ...) log_netdev_full(netdev, LOG_DEBUG, error, ##__VA_ARGS__)
188 #define log_netdev_info_errno(netdev, error, ...) log_netdev_full(netdev, LOG_INFO, error, ##__VA_ARGS__)
189 #define log_netdev_notice_errno(netdev, error, ...) log_netdev_full(netdev, LOG_NOTICE, error, ##__VA_ARGS__)
190 #define log_netdev_warning_errno(netdev, error, ...) log_netdev_full(netdev, LOG_WARNING, error, ##__VA_ARGS__)
191 #define log_netdev_error_errno(netdev, error, ...) log_netdev_full(netdev, LOG_ERR, error, ##__VA_ARGS__)
193 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
194 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname