1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "in-addr-util.h"
30 typedef struct NetDevVTable NetDevVTable;
32 typedef struct netdev_join_callback netdev_join_callback;
34 struct netdev_join_callback {
35 sd_rtnl_message_handler_t callback;
38 LIST_FIELDS(netdev_join_callback, callbacks);
41 typedef enum NetDevKind {
59 _NETDEV_KIND_INVALID = -1
62 typedef enum NetDevState {
64 NETDEV_STATE_CREATING,
68 _NETDEV_STATE_INVALID = -1,
71 typedef enum NetDevCreateType {
72 NETDEV_CREATE_INDEPENDENT,
74 NETDEV_CREATE_STACKED,
76 _NETDEV_CREATE_INVALID = -1,
86 Condition *match_host;
87 Condition *match_virt;
88 Condition *match_kernel;
89 Condition *match_arch;
95 struct ether_addr *mac;
99 LIST_HEAD(netdev_join_callback, callbacks);
102 #include "networkd-netdev-bridge.h"
103 #include "networkd-netdev-bond.h"
104 #include "networkd-netdev-vlan.h"
105 #include "networkd-netdev-macvlan.h"
106 #include "networkd-netdev-ipvlan.h"
107 #include "networkd-netdev-vxlan.h"
108 #include "networkd-netdev-veth.h"
109 #include "networkd-netdev-tunnel.h"
110 #include "networkd-netdev-dummy.h"
111 #include "networkd-netdev-tuntap.h"
113 struct NetDevVTable {
114 /* How much memory does an object of this unit type need */
117 /* Config file sections this netdev kind understands, separated
119 const char *sections;
121 /* This should reset all type-specific variables. This should
122 * not allocate memory, and is called with zero-initialized
123 * data. It should hence only initialize variables that need
125 void (*init)(NetDev *n);
127 /* This should free all kind-specific variables. It should be
129 void (*done)(NetDev *n);
131 /* fill in message to create netdev */
132 int (*fill_message_create)(NetDev *netdev, Link *link, sd_rtnl_message *message);
134 /* specifies if netdev is independent, or a master device or a stacked device */
135 NetDevCreateType create_type;
137 /* create netdev, if not done via rtnl */
138 int (*create)(NetDev *netdev);
140 /* verify that compulsory configuration options were specified */
141 int (*config_verify)(NetDev *netdev, const char *filename);
144 extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
146 #define NETDEV_VTABLE(n) netdev_vtable[(n)->kind]
148 /* For casting a netdev into the various netdev kinds */
149 #define DEFINE_CAST(UPPERCASE, MixedCase) \
150 static inline MixedCase* UPPERCASE(NetDev *n) { \
151 if (_unlikely_(!n || n->kind != NETDEV_KIND_##UPPERCASE)) \
154 return (MixedCase*) n; \
157 /* For casting the various netdev kinds into a netdev */
158 #define NETDEV(n) (&(n)->meta)
160 DEFINE_CAST(BRIDGE, Bridge);
161 DEFINE_CAST(BOND, Bond);
162 DEFINE_CAST(VLAN, VLan);
163 DEFINE_CAST(MACVLAN, MacVlan);
164 DEFINE_CAST(IPVLAN, IPVlan);
165 DEFINE_CAST(VXLAN, VxLan);
166 DEFINE_CAST(IPIP, Tunnel);
167 DEFINE_CAST(GRE, Tunnel);
168 DEFINE_CAST(GRETAP, Tunnel);
169 DEFINE_CAST(SIT, Tunnel);
170 DEFINE_CAST(VTI, Tunnel);
171 DEFINE_CAST(IP6TNL, Tunnel);
172 DEFINE_CAST(VETH, Veth);
173 DEFINE_CAST(DUMMY, Dummy);
174 DEFINE_CAST(TUN, TunTap);
175 DEFINE_CAST(TAP, TunTap);
177 int netdev_load(Manager *manager);
178 void netdev_drop(NetDev *netdev);
180 NetDev *netdev_unref(NetDev *netdev);
181 NetDev *netdev_ref(NetDev *netdev);
183 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
184 #define _cleanup_netdev_unref_ _cleanup_(netdev_unrefp)
186 int netdev_get(Manager *manager, const char *name, NetDev **ret);
187 int netdev_set_ifindex(NetDev *netdev, sd_rtnl_message *newlink);
188 int netdev_enslave(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback);
189 int netdev_get_mac(const char *ifname, struct ether_addr **ret);
190 int netdev_join(NetDev *netdev, Link *link, sd_rtnl_message_handler_t cb);
192 const char *netdev_kind_to_string(NetDevKind d) _const_;
193 NetDevKind netdev_kind_from_string(const char *d) _pure_;
195 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);
198 const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, unsigned length);
200 /* Macros which append INTERFACE= to the message */
202 #define log_full_netdev(level, netdev, fmt, ...) log_object_internal(level, 0, __FILE__, __LINE__, __func__, "INTERFACE=", netdev->ifname, "%-*s: " fmt, IFNAMSIZ, netdev->ifname, ##__VA_ARGS__)
203 #define log_netdev_debug(netdev, ...) log_full_netdev(LOG_DEBUG, netdev, ##__VA_ARGS__)
204 #define log_info_netdev(netdev, ...) log_full_netdev(LOG_INFO, netdev, ##__VA_ARGS__)
205 #define log_notice_netdev(netdev, ...) log_full_netdev(LOG_NOTICE, netdev, ##__VA_ARGS__)
206 #define log_warning_netdev(netdev, ...) log_full_netdev(LOG_WARNING, netdev,## __VA_ARGS__)
207 #define log_netdev_error(netdev, ...) log_full_netdev(LOG_ERR, netdev, ##__VA_ARGS__)
209 #define log_struct_netdev(level, netdev, ...) log_struct(level, "INTERFACE=%s", netdev->ifname, __VA_ARGS__)
211 #define NETDEVIF(netdev) "INTERFACE=%s", netdev->ifname