1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2013 Tom Gundersen <teg@jklm.no>
10 #include "sd-netlink.h"
13 #include "time-util.h"
15 typedef struct netdev_join_callback netdev_join_callback
;
16 typedef struct Link Link
;
18 struct netdev_join_callback
{
19 sd_netlink_message_handler_t callback
;
22 LIST_FIELDS(netdev_join_callback
, callbacks
);
25 typedef enum NetDevKind
{
37 NETDEV_KIND_IP6GRETAP
,
50 NETDEV_KIND_WIREGUARD
,
51 NETDEV_KIND_NETDEVSIM
,
53 _NETDEV_KIND_INVALID
= -1
56 typedef enum NetDevState
{
59 NETDEV_STATE_CREATING
,
63 _NETDEV_STATE_INVALID
= -1,
66 typedef enum NetDevCreateType
{
67 NETDEV_CREATE_INDEPENDENT
,
69 NETDEV_CREATE_STACKED
,
71 _NETDEV_CREATE_INVALID
= -1,
74 typedef struct Manager Manager
;
75 typedef struct Condition Condition
;
77 typedef struct NetDev
{
84 Condition
*match_host
;
85 Condition
*match_virt
;
86 Condition
*match_kernel_cmdline
;
87 Condition
*match_kernel_version
;
88 Condition
*match_arch
;
94 struct ether_addr
*mac
;
98 LIST_HEAD(netdev_join_callback
, callbacks
);
101 typedef struct NetDevVTable
{
102 /* How much memory does an object of this unit type need */
105 /* Config file sections this netdev kind understands, separated
107 const char *sections
;
109 /* This should reset all type-specific variables. This should
110 * not allocate memory, and is called with zero-initialized
111 * data. It should hence only initialize variables that need
113 void (*init
)(NetDev
*n
);
115 /* This should free all kind-specific variables. It should be
117 void (*done
)(NetDev
*n
);
119 /* fill in message to create netdev */
120 int (*fill_message_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
122 /* specifies if netdev is independent, or a master device or a stacked device */
123 NetDevCreateType create_type
;
125 /* create netdev, if not done via rtnl */
126 int (*create
)(NetDev
*netdev
);
128 /* perform additional configuration after netdev has been createad */
129 int (*post_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
131 /* verify that compulsory configuration options were specified */
132 int (*config_verify
)(NetDev
*netdev
, const char *filename
);
135 extern const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
];
137 #define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
139 /* For casting a netdev into the various netdev kinds */
140 #define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
141 static inline MixedCase* UPPERCASE(NetDev *n) { \
142 if (_unlikely_(!n || \
143 n->kind != NETDEV_KIND_##UPPERCASE) || \
144 n->state == _NETDEV_STATE_INVALID) \
147 return (MixedCase*) n; \
150 /* For casting the various netdev kinds into a netdev */
151 #define NETDEV(n) (&(n)->meta)
153 int netdev_load(Manager
*manager
);
154 void netdev_drop(NetDev
*netdev
);
156 NetDev
*netdev_unref(NetDev
*netdev
);
157 NetDev
*netdev_ref(NetDev
*netdev
);
159 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev
*, netdev_unref
);
161 int netdev_get(Manager
*manager
, const char *name
, NetDev
**ret
);
162 int netdev_set_ifindex(NetDev
*netdev
, sd_netlink_message
*newlink
);
163 int netdev_enslave(NetDev
*netdev
, Link
*link
, sd_netlink_message_handler_t callback
);
164 int netdev_get_mac(const char *ifname
, struct ether_addr
**ret
);
165 int netdev_join(NetDev
*netdev
, Link
*link
, sd_netlink_message_handler_t cb
);
167 const char *netdev_kind_to_string(NetDevKind d
) _const_
;
168 NetDevKind
netdev_kind_from_string(const char *d
) _pure_
;
170 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
);
173 const struct ConfigPerfItem
* network_netdev_gperf_lookup(const char *key
, GPERF_LEN_TYPE length
);
175 /* Macros which append INTERFACE= to the message */
177 #define log_netdev_full(netdev, level, error, ...) \
179 const NetDev *_n = (netdev); \
180 _n ? log_object_internal(level, error, __FILE__, __LINE__, __func__, "INTERFACE=", _n->ifname, NULL, NULL, ##__VA_ARGS__) : \
181 log_internal(level, error, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
184 #define log_netdev_debug(netdev, ...) log_netdev_full(netdev, LOG_DEBUG, 0, ##__VA_ARGS__)
185 #define log_netdev_info(netdev, ...) log_netdev_full(netdev, LOG_INFO, 0, ##__VA_ARGS__)
186 #define log_netdev_notice(netdev, ...) log_netdev_full(netdev, LOG_NOTICE, 0, ##__VA_ARGS__)
187 #define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING, 0, ## __VA_ARGS__)
188 #define log_netdev_error(netdev, ...) log_netdev_full(netdev, LOG_ERR, 0, ##__VA_ARGS__)
190 #define log_netdev_debug_errno(netdev, error, ...) log_netdev_full(netdev, LOG_DEBUG, error, ##__VA_ARGS__)
191 #define log_netdev_info_errno(netdev, error, ...) log_netdev_full(netdev, LOG_INFO, error, ##__VA_ARGS__)
192 #define log_netdev_notice_errno(netdev, error, ...) log_netdev_full(netdev, LOG_NOTICE, error, ##__VA_ARGS__)
193 #define log_netdev_warning_errno(netdev, error, ...) log_netdev_full(netdev, LOG_WARNING, error, ##__VA_ARGS__)
194 #define log_netdev_error_errno(netdev, error, ...) log_netdev_full(netdev, LOG_ERR, error, ##__VA_ARGS__)
196 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
197 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname