]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-netdev.h
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / network / networkd-netdev.h
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #pragma once
21
22 #include "list.h"
23
24 typedef struct NetDev NetDev;
25 typedef struct NetDevVTable NetDevVTable;
26
27 #include "networkd-link.h"
28 #include "networkd.h"
29
30 typedef struct netdev_join_callback netdev_join_callback;
31
32 struct netdev_join_callback {
33 sd_netlink_message_handler_t callback;
34 Link *link;
35
36 LIST_FIELDS(netdev_join_callback, callbacks);
37 };
38
39 typedef enum NetDevKind {
40 NETDEV_KIND_BRIDGE,
41 NETDEV_KIND_BOND,
42 NETDEV_KIND_VLAN,
43 NETDEV_KIND_MACVLAN,
44 NETDEV_KIND_MACVTAP,
45 NETDEV_KIND_IPVLAN,
46 NETDEV_KIND_VXLAN,
47 NETDEV_KIND_IPIP,
48 NETDEV_KIND_GRE,
49 NETDEV_KIND_GRETAP,
50 NETDEV_KIND_IP6GRE,
51 NETDEV_KIND_IP6GRETAP,
52 NETDEV_KIND_SIT,
53 NETDEV_KIND_VETH,
54 NETDEV_KIND_VTI,
55 NETDEV_KIND_VTI6,
56 NETDEV_KIND_IP6TNL,
57 NETDEV_KIND_DUMMY,
58 NETDEV_KIND_TUN,
59 NETDEV_KIND_TAP,
60 _NETDEV_KIND_MAX,
61 _NETDEV_KIND_INVALID = -1
62 } NetDevKind;
63
64 typedef enum NetDevState {
65 NETDEV_STATE_FAILED,
66 NETDEV_STATE_CREATING,
67 NETDEV_STATE_READY,
68 NETDEV_STATE_LINGER,
69 _NETDEV_STATE_MAX,
70 _NETDEV_STATE_INVALID = -1,
71 } NetDevState;
72
73 typedef enum NetDevCreateType {
74 NETDEV_CREATE_INDEPENDENT,
75 NETDEV_CREATE_MASTER,
76 NETDEV_CREATE_STACKED,
77 _NETDEV_CREATE_MAX,
78 _NETDEV_CREATE_INVALID = -1,
79 } NetDevCreateType;
80
81 struct NetDev {
82 Manager *manager;
83
84 int n_ref;
85
86 char *filename;
87
88 Condition *match_host;
89 Condition *match_virt;
90 Condition *match_kernel;
91 Condition *match_arch;
92
93 NetDevState state;
94 NetDevKind kind;
95 char *description;
96 char *ifname;
97 struct ether_addr *mac;
98 size_t mtu;
99 int ifindex;
100
101 LIST_HEAD(netdev_join_callback, callbacks);
102 };
103
104 #include "networkd-netdev-bond.h"
105 #include "networkd-netdev-bridge.h"
106 #include "networkd-netdev-dummy.h"
107 #include "networkd-netdev-ipvlan.h"
108 #include "networkd-netdev-macvlan.h"
109 #include "networkd-netdev-tunnel.h"
110 #include "networkd-netdev-tuntap.h"
111 #include "networkd-netdev-veth.h"
112 #include "networkd-netdev-vlan.h"
113 #include "networkd-netdev-vxlan.h"
114
115 struct NetDevVTable {
116 /* How much memory does an object of this unit type need */
117 size_t object_size;
118
119 /* Config file sections this netdev kind understands, separated
120 * by NUL chars */
121 const char *sections;
122
123 /* This should reset all type-specific variables. This should
124 * not allocate memory, and is called with zero-initialized
125 * data. It should hence only initialize variables that need
126 * to be set != 0. */
127 void (*init)(NetDev *n);
128
129 /* This should free all kind-specific variables. It should be
130 * idempotent. */
131 void (*done)(NetDev *n);
132
133 /* fill in message to create netdev */
134 int (*fill_message_create)(NetDev *netdev, Link *link, sd_netlink_message *message);
135
136 /* specifies if netdev is independent, or a master device or a stacked device */
137 NetDevCreateType create_type;
138
139 /* create netdev, if not done via rtnl */
140 int (*create)(NetDev *netdev);
141
142 /* perform additional configuration after netdev has been createad */
143 int (*post_create)(NetDev *netdev, Link *link, sd_netlink_message *message);
144
145 /* verify that compulsory configuration options were specified */
146 int (*config_verify)(NetDev *netdev, const char *filename);
147 };
148
149 extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
150
151 #define NETDEV_VTABLE(n) netdev_vtable[(n)->kind]
152
153 /* For casting a netdev into the various netdev kinds */
154 #define DEFINE_CAST(UPPERCASE, MixedCase) \
155 static inline MixedCase* UPPERCASE(NetDev *n) { \
156 if (_unlikely_(!n || n->kind != NETDEV_KIND_##UPPERCASE)) \
157 return NULL; \
158 \
159 return (MixedCase*) n; \
160 }
161
162 /* For casting the various netdev kinds into a netdev */
163 #define NETDEV(n) (&(n)->meta)
164
165 DEFINE_CAST(BRIDGE, Bridge);
166 DEFINE_CAST(BOND, Bond);
167 DEFINE_CAST(VLAN, VLan);
168 DEFINE_CAST(MACVLAN, MacVlan);
169 DEFINE_CAST(MACVTAP, MacVlan);
170 DEFINE_CAST(IPVLAN, IPVlan);
171 DEFINE_CAST(VXLAN, VxLan);
172 DEFINE_CAST(IPIP, Tunnel);
173 DEFINE_CAST(GRE, Tunnel);
174 DEFINE_CAST(GRETAP, Tunnel);
175 DEFINE_CAST(IP6GRE, Tunnel);
176 DEFINE_CAST(IP6GRETAP, Tunnel);
177 DEFINE_CAST(SIT, Tunnel);
178 DEFINE_CAST(VTI, Tunnel);
179 DEFINE_CAST(VTI6, Tunnel);
180 DEFINE_CAST(IP6TNL, Tunnel);
181 DEFINE_CAST(VETH, Veth);
182 DEFINE_CAST(DUMMY, Dummy);
183 DEFINE_CAST(TUN, TunTap);
184 DEFINE_CAST(TAP, TunTap);
185
186 int netdev_load(Manager *manager);
187 void netdev_drop(NetDev *netdev);
188
189 NetDev *netdev_unref(NetDev *netdev);
190 NetDev *netdev_ref(NetDev *netdev);
191
192 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
193 #define _cleanup_netdev_unref_ _cleanup_(netdev_unrefp)
194
195 int netdev_get(Manager *manager, const char *name, NetDev **ret);
196 int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *newlink);
197 int netdev_enslave(NetDev *netdev, Link *link, sd_netlink_message_handler_t callback);
198 int netdev_get_mac(const char *ifname, struct ether_addr **ret);
199 int netdev_join(NetDev *netdev, Link *link, sd_netlink_message_handler_t cb);
200
201 const char *netdev_kind_to_string(NetDevKind d) _const_;
202 NetDevKind netdev_kind_from_string(const char *d) _pure_;
203
204 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);
205
206 /* gperf */
207 const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, unsigned length);
208
209 /* Macros which append INTERFACE= to the message */
210
211 #define log_netdev_full(netdev, level, error, ...) \
212 ({ \
213 NetDev *_n = (netdev); \
214 _n ? log_object_internal(level, error, __FILE__, __LINE__, __func__, "INTERFACE=", _n->ifname, ##__VA_ARGS__) : \
215 log_internal(level, error, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
216 })
217
218 #define log_netdev_debug(netdev, ...) log_netdev_full(netdev, LOG_DEBUG, 0, ##__VA_ARGS__)
219 #define log_netdev_info(netdev, ...) log_netdev_full(netdev, LOG_INFO, 0, ##__VA_ARGS__)
220 #define log_netdev_notice(netdev, ...) log_netdev_full(netdev, LOG_NOTICE, 0, ##__VA_ARGS__)
221 #define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING, 0, ## __VA_ARGS__)
222 #define log_netdev_error(netdev, ...) log_netdev_full(netdev, LOG_ERR, 0, ##__VA_ARGS__)
223
224 #define log_netdev_debug_errno(netdev, error, ...) log_netdev_full(netdev, LOG_DEBUG, error, ##__VA_ARGS__)
225 #define log_netdev_info_errno(netdev, error, ...) log_netdev_full(netdev, LOG_INFO, error, ##__VA_ARGS__)
226 #define log_netdev_notice_errno(netdev, error, ...) log_netdev_full(netdev, LOG_NOTICE, error, ##__VA_ARGS__)
227 #define log_netdev_warning_errno(netdev, error, ...) log_netdev_full(netdev, LOG_WARNING, error, ##__VA_ARGS__)
228 #define log_netdev_error_errno(netdev, error, ...) log_netdev_full(netdev, LOG_ERR, error, ##__VA_ARGS__)
229
230 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
231 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname