1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 Copyright © 2013 Tom Gundersen <teg@jklm.no>
8 #include "sd-netlink.h"
11 #include "time-util.h"
13 typedef struct netdev_join_callback netdev_join_callback
;
14 typedef struct Link Link
;
16 struct netdev_join_callback
{
17 sd_netlink_message_handler_t callback
;
20 LIST_FIELDS(netdev_join_callback
, callbacks
);
23 typedef enum NetDevKind
{
35 NETDEV_KIND_IP6GRETAP
,
48 NETDEV_KIND_WIREGUARD
,
49 NETDEV_KIND_NETDEVSIM
,
51 _NETDEV_KIND_INVALID
= -1
54 typedef enum NetDevState
{
57 NETDEV_STATE_CREATING
,
61 _NETDEV_STATE_INVALID
= -1,
64 typedef enum NetDevCreateType
{
65 NETDEV_CREATE_INDEPENDENT
,
67 NETDEV_CREATE_STACKED
,
69 _NETDEV_CREATE_INVALID
= -1,
72 typedef struct Manager Manager
;
73 typedef struct Condition Condition
;
75 typedef struct NetDev
{
82 Condition
*match_host
;
83 Condition
*match_virt
;
84 Condition
*match_kernel_cmdline
;
85 Condition
*match_kernel_version
;
86 Condition
*match_arch
;
92 struct ether_addr
*mac
;
96 LIST_HEAD(netdev_join_callback
, callbacks
);
99 typedef struct NetDevVTable
{
100 /* How much memory does an object of this unit type need */
103 /* Config file sections this netdev kind understands, separated
105 const char *sections
;
107 /* This should reset all type-specific variables. This should
108 * not allocate memory, and is called with zero-initialized
109 * data. It should hence only initialize variables that need
111 void (*init
)(NetDev
*n
);
113 /* This should free all kind-specific variables. It should be
115 void (*done
)(NetDev
*n
);
117 /* fill in message to create netdev */
118 int (*fill_message_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
120 /* specifies if netdev is independent, or a master device or a stacked device */
121 NetDevCreateType create_type
;
123 /* create netdev, if not done via rtnl */
124 int (*create
)(NetDev
*netdev
);
126 /* perform additional configuration after netdev has been createad */
127 int (*post_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
129 /* verify that compulsory configuration options were specified */
130 int (*config_verify
)(NetDev
*netdev
, const char *filename
);
133 extern const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
];
135 #define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
137 /* For casting a netdev into the various netdev kinds */
138 #define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
139 static inline MixedCase* UPPERCASE(NetDev *n) { \
140 if (_unlikely_(!n || \
141 n->kind != NETDEV_KIND_##UPPERCASE) || \
142 n->state == _NETDEV_STATE_INVALID) \
145 return (MixedCase*) n; \
148 /* For casting the various netdev kinds into a netdev */
149 #define NETDEV(n) (&(n)->meta)
151 int netdev_load(Manager
*manager
);
152 void netdev_drop(NetDev
*netdev
);
154 NetDev
*netdev_unref(NetDev
*netdev
);
155 NetDev
*netdev_ref(NetDev
*netdev
);
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_enslave(NetDev
*netdev
, Link
*link
, sd_netlink_message_handler_t callback
);
162 int netdev_get_mac(const char *ifname
, struct ether_addr
**ret
);
163 int netdev_join(NetDev
*netdev
, Link
*link
, sd_netlink_message_handler_t cb
);
165 const char *netdev_kind_to_string(NetDevKind d
) _const_
;
166 NetDevKind
netdev_kind_from_string(const char *d
) _pure_
;
168 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
);
171 const struct ConfigPerfItem
* network_netdev_gperf_lookup(const char *key
, GPERF_LEN_TYPE length
);
173 /* Macros which append INTERFACE= to the message */
175 #define log_netdev_full(netdev, level, error, ...) \
177 const NetDev *_n = (netdev); \
178 _n ? log_object_internal(level, error, __FILE__, __LINE__, __func__, "INTERFACE=", _n->ifname, NULL, NULL, ##__VA_ARGS__) : \
179 log_internal(level, error, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
182 #define log_netdev_debug(netdev, ...) log_netdev_full(netdev, LOG_DEBUG, 0, ##__VA_ARGS__)
183 #define log_netdev_info(netdev, ...) log_netdev_full(netdev, LOG_INFO, 0, ##__VA_ARGS__)
184 #define log_netdev_notice(netdev, ...) log_netdev_full(netdev, LOG_NOTICE, 0, ##__VA_ARGS__)
185 #define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING, 0, ## __VA_ARGS__)
186 #define log_netdev_error(netdev, ...) log_netdev_full(netdev, LOG_ERR, 0, ##__VA_ARGS__)
188 #define log_netdev_debug_errno(netdev, error, ...) log_netdev_full(netdev, LOG_DEBUG, error, ##__VA_ARGS__)
189 #define log_netdev_info_errno(netdev, error, ...) log_netdev_full(netdev, LOG_INFO, error, ##__VA_ARGS__)
190 #define log_netdev_notice_errno(netdev, error, ...) log_netdev_full(netdev, LOG_NOTICE, error, ##__VA_ARGS__)
191 #define log_netdev_warning_errno(netdev, error, ...) log_netdev_full(netdev, LOG_WARNING, error, ##__VA_ARGS__)
192 #define log_netdev_error_errno(netdev, error, ...) log_netdev_full(netdev, LOG_ERR, error, ##__VA_ARGS__)
194 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
195 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname