]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/netdev.h
tree-wide: use unsigned for refcount
[thirdparty/systemd.git] / src / network / netdev / netdev.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include "sd-netlink.h"
5
6 #include "list.h"
7 #include "time-util.h"
8
9 typedef struct netdev_join_callback netdev_join_callback;
10 typedef struct Link Link;
11
12 struct netdev_join_callback {
13 sd_netlink_message_handler_t callback;
14 Link *link;
15
16 LIST_FIELDS(netdev_join_callback, callbacks);
17 };
18
19 typedef enum NetDevKind {
20 NETDEV_KIND_BRIDGE,
21 NETDEV_KIND_BOND,
22 NETDEV_KIND_VLAN,
23 NETDEV_KIND_MACVLAN,
24 NETDEV_KIND_MACVTAP,
25 NETDEV_KIND_IPVLAN,
26 NETDEV_KIND_VXLAN,
27 NETDEV_KIND_IPIP,
28 NETDEV_KIND_GRE,
29 NETDEV_KIND_GRETAP,
30 NETDEV_KIND_IP6GRE,
31 NETDEV_KIND_IP6GRETAP,
32 NETDEV_KIND_SIT,
33 NETDEV_KIND_VETH,
34 NETDEV_KIND_VTI,
35 NETDEV_KIND_VTI6,
36 NETDEV_KIND_IP6TNL,
37 NETDEV_KIND_DUMMY,
38 NETDEV_KIND_TUN,
39 NETDEV_KIND_TAP,
40 NETDEV_KIND_VRF,
41 NETDEV_KIND_VCAN,
42 NETDEV_KIND_GENEVE,
43 NETDEV_KIND_VXCAN,
44 NETDEV_KIND_WIREGUARD,
45 NETDEV_KIND_NETDEVSIM,
46 NETDEV_KIND_FOU,
47 _NETDEV_KIND_MAX,
48 _NETDEV_KIND_INVALID = -1
49 } NetDevKind;
50
51 typedef enum NetDevState {
52 NETDEV_STATE_LOADING,
53 NETDEV_STATE_FAILED,
54 NETDEV_STATE_CREATING,
55 NETDEV_STATE_READY,
56 NETDEV_STATE_LINGER,
57 _NETDEV_STATE_MAX,
58 _NETDEV_STATE_INVALID = -1,
59 } NetDevState;
60
61 typedef enum NetDevCreateType {
62 NETDEV_CREATE_INDEPENDENT,
63 NETDEV_CREATE_MASTER,
64 NETDEV_CREATE_STACKED,
65 _NETDEV_CREATE_MAX,
66 _NETDEV_CREATE_INVALID = -1,
67 } NetDevCreateType;
68
69 typedef struct Manager Manager;
70 typedef struct Condition Condition;
71
72 typedef struct NetDev {
73 Manager *manager;
74
75 unsigned n_ref;
76
77 char *filename;
78
79 Condition *match_host;
80 Condition *match_virt;
81 Condition *match_kernel_cmdline;
82 Condition *match_kernel_version;
83 Condition *match_arch;
84
85 NetDevState state;
86 NetDevKind kind;
87 char *description;
88 char *ifname;
89 struct ether_addr *mac;
90 uint32_t mtu;
91 int ifindex;
92
93 LIST_HEAD(netdev_join_callback, callbacks);
94 } NetDev;
95
96 typedef struct NetDevVTable {
97 /* How much memory does an object of this unit type need */
98 size_t object_size;
99
100 /* Config file sections this netdev kind understands, separated
101 * by NUL chars */
102 const char *sections;
103
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
107 * to be set != 0. */
108 void (*init)(NetDev *n);
109
110 /* This should free all kind-specific variables. It should be
111 * idempotent. */
112 void (*done)(NetDev *n);
113
114 /* fill in message to create netdev */
115 int (*fill_message_create)(NetDev *netdev, Link *link, sd_netlink_message *message);
116
117 /* specifies if netdev is independent, or a master device or a stacked device */
118 NetDevCreateType create_type;
119
120 /* create netdev, if not done via rtnl */
121 int (*create)(NetDev *netdev);
122
123 /* perform additional configuration after netdev has been createad */
124 int (*post_create)(NetDev *netdev, Link *link, sd_netlink_message *message);
125
126 /* verify that compulsory configuration options were specified */
127 int (*config_verify)(NetDev *netdev, const char *filename);
128 } NetDevVTable;
129
130 extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
131
132 #define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
133
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) \
140 return NULL; \
141 \
142 return (MixedCase*) n; \
143 }
144
145 /* For casting the various netdev kinds into a netdev */
146 #define NETDEV(n) (&(n)->meta)
147
148 int netdev_load(Manager *manager);
149 void netdev_drop(NetDev *netdev);
150
151 NetDev *netdev_unref(NetDev *netdev);
152 NetDev *netdev_ref(NetDev *netdev);
153
154 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
155
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);
161
162 const char *netdev_kind_to_string(NetDevKind d) _const_;
163 NetDevKind netdev_kind_from_string(const char *d) _pure_;
164
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);
166
167 /* gperf */
168 const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
169
170 /* Macros which append INTERFACE= to the message */
171
172 #define log_netdev_full(netdev, level, error, ...) \
173 ({ \
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__); \
177 })
178
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__)
184
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__)
190
191 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
192 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname