1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include "sd-netlink.h"
9 typedef struct netdev_join_callback netdev_join_callback
;
10 typedef struct Link Link
;
12 struct netdev_join_callback
{
13 sd_netlink_message_handler_t callback
;
16 LIST_FIELDS(netdev_join_callback
, callbacks
);
19 typedef enum NetDevKind
{
31 NETDEV_KIND_IP6GRETAP
,
44 NETDEV_KIND_WIREGUARD
,
45 NETDEV_KIND_NETDEVSIM
,
48 _NETDEV_KIND_INVALID
= -1
51 typedef enum NetDevState
{
54 NETDEV_STATE_CREATING
,
58 _NETDEV_STATE_INVALID
= -1,
61 typedef enum NetDevCreateType
{
62 NETDEV_CREATE_INDEPENDENT
,
64 NETDEV_CREATE_STACKED
,
66 _NETDEV_CREATE_INVALID
= -1,
69 typedef struct Manager Manager
;
70 typedef struct Condition Condition
;
72 typedef struct NetDev
{
79 Condition
*match_host
;
80 Condition
*match_virt
;
81 Condition
*match_kernel_cmdline
;
82 Condition
*match_kernel_version
;
83 Condition
*match_arch
;
89 struct ether_addr
*mac
;
93 LIST_HEAD(netdev_join_callback
, callbacks
);
96 typedef struct NetDevVTable
{
97 /* How much memory does an object of this unit type need */
100 /* Config file sections this netdev kind understands, separated
102 const char *sections
;
104 /* This should reset all type-specific variables. This should
105 * not allocate memory, and is called with zero-initialized
106 * data. It should hence only initialize variables that need
108 void (*init
)(NetDev
*n
);
110 /* This should free all kind-specific variables. It should be
112 void (*done
)(NetDev
*n
);
114 /* fill in message to create netdev */
115 int (*fill_message_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
117 /* specifies if netdev is independent, or a master device or a stacked device */
118 NetDevCreateType create_type
;
120 /* create netdev, if not done via rtnl */
121 int (*create
)(NetDev
*netdev
);
123 /* perform additional configuration after netdev has been createad */
124 int (*post_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
126 /* verify that compulsory configuration options were specified */
127 int (*config_verify
)(NetDev
*netdev
, const char *filename
);
130 extern const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
];
132 #define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
134 /* For casting a netdev into the various netdev kinds */
135 #define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
136 static inline MixedCase* UPPERCASE(NetDev *n) { \
137 if (_unlikely_(!n || \
138 n->kind != NETDEV_KIND_##UPPERCASE) || \
139 n->state == _NETDEV_STATE_INVALID) \
142 return (MixedCase*) n; \
145 /* For casting the various netdev kinds into a netdev */
146 #define NETDEV(n) (&(n)->meta)
148 int netdev_load(Manager
*manager
);
149 void netdev_drop(NetDev
*netdev
);
151 NetDev
*netdev_unref(NetDev
*netdev
);
152 NetDev
*netdev_ref(NetDev
*netdev
);
154 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev
*, netdev_unref
);
156 int netdev_get(Manager
*manager
, const char *name
, NetDev
**ret
);
157 int netdev_set_ifindex(NetDev
*netdev
, sd_netlink_message
*newlink
);
158 int netdev_enslave(NetDev
*netdev
, Link
*link
, sd_netlink_message_handler_t callback
);
159 int netdev_get_mac(const char *ifname
, struct ether_addr
**ret
);
160 int netdev_join(NetDev
*netdev
, Link
*link
, sd_netlink_message_handler_t cb
);
162 const char *netdev_kind_to_string(NetDevKind d
) _const_
;
163 NetDevKind
netdev_kind_from_string(const char *d
) _pure_
;
165 int config_parse_netdev_kind(const char *unit
, const char *filename
, unsigned line
, const char *section
, unsigned section_line
, const char *lvalue
, int ltype
, const char *rvalue
, void *data
, void *userdata
);
168 const struct ConfigPerfItem
* network_netdev_gperf_lookup(const char *key
, GPERF_LEN_TYPE length
);
170 /* Macros which append INTERFACE= to the message */
172 #define log_netdev_full(netdev, level, error, ...) \
174 const NetDev *_n = (netdev); \
175 _n ? log_object_internal(level, error, __FILE__, __LINE__, __func__, "INTERFACE=", _n->ifname, NULL, NULL, ##__VA_ARGS__) : \
176 log_internal(level, error, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
179 #define log_netdev_debug(netdev, ...) log_netdev_full(netdev, LOG_DEBUG, 0, ##__VA_ARGS__)
180 #define log_netdev_info(netdev, ...) log_netdev_full(netdev, LOG_INFO, 0, ##__VA_ARGS__)
181 #define log_netdev_notice(netdev, ...) log_netdev_full(netdev, LOG_NOTICE, 0, ##__VA_ARGS__)
182 #define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING, 0, ## __VA_ARGS__)
183 #define log_netdev_error(netdev, ...) log_netdev_full(netdev, LOG_ERR, 0, ##__VA_ARGS__)
185 #define log_netdev_debug_errno(netdev, error, ...) log_netdev_full(netdev, LOG_DEBUG, error, ##__VA_ARGS__)
186 #define log_netdev_info_errno(netdev, error, ...) log_netdev_full(netdev, LOG_INFO, error, ##__VA_ARGS__)
187 #define log_netdev_notice_errno(netdev, error, ...) log_netdev_full(netdev, LOG_NOTICE, error, ##__VA_ARGS__)
188 #define log_netdev_warning_errno(netdev, error, ...) log_netdev_full(netdev, LOG_WARNING, error, ##__VA_ARGS__)
189 #define log_netdev_error_errno(netdev, error, ...) log_netdev_full(netdev, LOG_ERR, error, ##__VA_ARGS__)
191 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
192 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname