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
{
33 NETDEV_KIND_IP6GRETAP
,
46 NETDEV_KIND_WIREGUARD
,
47 NETDEV_KIND_NETDEVSIM
,
54 _NETDEV_KIND_TUNNEL
, /* Used by config_parse_stacked_netdev() */
55 _NETDEV_KIND_INVALID
= -1
58 typedef enum NetDevState
{
61 NETDEV_STATE_CREATING
,
65 _NETDEV_STATE_INVALID
= -1,
68 typedef enum NetDevCreateType
{
69 NETDEV_CREATE_INDEPENDENT
,
71 NETDEV_CREATE_STACKED
,
72 NETDEV_CREATE_AFTER_CONFIGURED
,
74 _NETDEV_CREATE_INVALID
= -1,
77 typedef struct Manager Manager
;
78 typedef struct Condition Condition
;
80 typedef struct NetDev
{
87 LIST_HEAD(Condition
, conditions
);
93 struct ether_addr
*mac
;
97 LIST_HEAD(netdev_join_callback
, callbacks
);
100 typedef struct NetDevVTable
{
101 /* How much memory does an object of this unit type need */
104 /* Config file sections this netdev kind understands, separated
106 const char *sections
;
108 /* This should reset all type-specific variables. This should
109 * not allocate memory, and is called with zero-initialized
110 * data. It should hence only initialize variables that need
112 void (*init
)(NetDev
*n
);
114 /* This should free all kind-specific variables. It should be
116 void (*done
)(NetDev
*n
);
118 /* fill in message to create netdev */
119 int (*fill_message_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
121 /* specifies if netdev is independent, or a master device or a stacked device */
122 NetDevCreateType create_type
;
124 /* create netdev, if not done via rtnl */
125 int (*create
)(NetDev
*netdev
);
127 /* create netdev after link is fully configured */
128 int (*create_after_configured
)(NetDev
*netdev
, Link
*link
);
130 /* perform additional configuration after netdev has been createad */
131 int (*post_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
133 /* verify that compulsory configuration options were specified */
134 int (*config_verify
)(NetDev
*netdev
, const char *filename
);
136 /* Generate MAC address or not When MACAddress= is not specified. */
140 extern const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
];
142 #define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
144 /* For casting a netdev into the various netdev kinds */
145 #define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
146 static inline MixedCase* UPPERCASE(NetDev *n) { \
147 if (_unlikely_(!n || \
148 n->kind != NETDEV_KIND_##UPPERCASE) || \
149 n->state == _NETDEV_STATE_INVALID) \
152 return (MixedCase*) n; \
155 /* For casting the various netdev kinds into a netdev */
156 #define NETDEV(n) (&(n)->meta)
158 int netdev_load(Manager
*manager
);
159 int netdev_load_one(Manager
*manager
, const char *filename
);
160 void netdev_drop(NetDev
*netdev
);
162 NetDev
*netdev_unref(NetDev
*netdev
);
163 NetDev
*netdev_ref(NetDev
*netdev
);
164 DEFINE_TRIVIAL_DESTRUCTOR(netdev_destroy_callback
, NetDev
, netdev_unref
);
165 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev
*, netdev_unref
);
167 bool netdev_is_managed(NetDev
*netdev
);
168 int netdev_get(Manager
*manager
, const char *name
, NetDev
**ret
);
169 int netdev_set_ifindex(NetDev
*netdev
, sd_netlink_message
*newlink
);
170 int netdev_get_mac(const char *ifname
, struct ether_addr
**ret
);
171 int netdev_join(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t cb
);
172 int netdev_join_after_configured(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
);
174 const char *netdev_kind_to_string(NetDevKind d
) _const_
;
175 NetDevKind
netdev_kind_from_string(const char *d
) _pure_
;
177 static inline NetDevCreateType
netdev_get_create_type(NetDev
*netdev
) {
179 assert(NETDEV_VTABLE(netdev
));
181 return NETDEV_VTABLE(netdev
)->create_type
;
185 CONFIG_PARSER_PROTOTYPE(config_parse_netdev_kind
);
188 const struct ConfigPerfItem
* network_netdev_gperf_lookup(const char *key
, GPERF_LEN_TYPE length
);
190 /* Macros which append INTERFACE= to the message */
192 #define log_netdev_full(netdev, level, error, ...) \
194 const NetDev *_n = (netdev); \
195 _n ? log_object_internal(level, error, __FILE__, __LINE__, __func__, "INTERFACE=", _n->ifname, NULL, NULL, ##__VA_ARGS__) : \
196 log_internal(level, error, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
199 #define log_netdev_debug(netdev, ...) log_netdev_full(netdev, LOG_DEBUG, 0, ##__VA_ARGS__)
200 #define log_netdev_info(netdev, ...) log_netdev_full(netdev, LOG_INFO, 0, ##__VA_ARGS__)
201 #define log_netdev_notice(netdev, ...) log_netdev_full(netdev, LOG_NOTICE, 0, ##__VA_ARGS__)
202 #define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING, 0, ## __VA_ARGS__)
203 #define log_netdev_error(netdev, ...) log_netdev_full(netdev, LOG_ERR, 0, ##__VA_ARGS__)
205 #define log_netdev_debug_errno(netdev, error, ...) log_netdev_full(netdev, LOG_DEBUG, error, ##__VA_ARGS__)
206 #define log_netdev_info_errno(netdev, error, ...) log_netdev_full(netdev, LOG_INFO, error, ##__VA_ARGS__)
207 #define log_netdev_notice_errno(netdev, error, ...) log_netdev_full(netdev, LOG_NOTICE, error, ##__VA_ARGS__)
208 #define log_netdev_warning_errno(netdev, error, ...) log_netdev_full(netdev, LOG_WARNING, error, ##__VA_ARGS__)
209 #define log_netdev_error_errno(netdev, error, ...) log_netdev_full(netdev, LOG_ERR, error, ##__VA_ARGS__)
211 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
212 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname