chiark / gitweb /
3c447958a60836069fb76e16f7ee7028b4d725b2
[elogind.git] / src / network / networkd-netdev.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Tom Gundersen <teg@jklm.no>
7
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.
12
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.
17
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/>.
20 ***/
21
22 #pragma once
23
24 #include "networkd.h"
25 #include "hashmap.h"
26 #include "list.h"
27 #include "set.h"
28 #include "in-addr-util.h"
29
30 typedef struct NetDevVTable NetDevVTable;
31
32 typedef struct netdev_join_callback netdev_join_callback;
33
34 struct netdev_join_callback {
35         sd_rtnl_message_handler_t callback;
36         Link *link;
37
38         LIST_FIELDS(netdev_join_callback, callbacks);
39 };
40
41 typedef enum NetDevKind {
42         NETDEV_KIND_BRIDGE,
43         NETDEV_KIND_BOND,
44         NETDEV_KIND_VLAN,
45         NETDEV_KIND_MACVLAN,
46         NETDEV_KIND_IPVLAN,
47         NETDEV_KIND_VXLAN,
48         NETDEV_KIND_IPIP,
49         NETDEV_KIND_GRE,
50         NETDEV_KIND_GRETAP,
51         NETDEV_KIND_SIT,
52         NETDEV_KIND_VETH,
53         NETDEV_KIND_VTI,
54         NETDEV_KIND_DUMMY,
55         NETDEV_KIND_TUN,
56         NETDEV_KIND_TAP,
57         _NETDEV_KIND_MAX,
58         _NETDEV_KIND_INVALID = -1
59 } NetDevKind;
60
61 typedef enum NetDevState {
62         NETDEV_STATE_FAILED,
63         NETDEV_STATE_CREATING,
64         NETDEV_STATE_READY,
65         NETDEV_STATE_LINGER,
66         _NETDEV_STATE_MAX,
67         _NETDEV_STATE_INVALID = -1,
68 } NetDevState;
69
70 typedef enum NetDevCreateType {
71         NETDEV_CREATE_INDEPENDENT,
72         NETDEV_CREATE_MASTER,
73         NETDEV_CREATE_STACKED,
74         _NETDEV_CREATE_MAX,
75         _NETDEV_CREATE_INVALID = -1,
76 } NetDevCreateType;
77
78 struct NetDev {
79         Manager *manager;
80
81         int n_ref;
82
83         char *filename;
84
85         Condition *match_host;
86         Condition *match_virt;
87         Condition *match_kernel;
88         Condition *match_arch;
89
90         NetDevState state;
91         NetDevKind kind;
92         char *description;
93         char *ifname;
94         struct ether_addr *mac;
95         size_t mtu;
96         int ifindex;
97
98         LIST_HEAD(netdev_join_callback, callbacks);
99 };
100
101 #include "networkd-netdev-bridge.h"
102 #include "networkd-netdev-bond.h"
103 #include "networkd-netdev-vlan.h"
104 #include "networkd-netdev-macvlan.h"
105 #include "networkd-netdev-ipvlan.h"
106 #include "networkd-netdev-vxlan.h"
107 #include "networkd-netdev-veth.h"
108 #include "networkd-netdev-tunnel.h"
109 #include "networkd-netdev-dummy.h"
110 #include "networkd-netdev-tuntap.h"
111
112 struct NetDevVTable {
113         /* How much memory does an object of this unit type need */
114         size_t object_size;
115
116         /* Config file sections this netdev kind understands, separated
117          * by NUL chars */
118         const char *sections;
119
120         /* This should reset all type-specific variables. This should
121          * not allocate memory, and is called with zero-initialized
122          * data. It should hence only initialize variables that need
123          * to be set != 0. */
124         void (*init)(NetDev *n);
125
126         /* This should free all kind-specific variables. It should be
127          * idempotent. */
128         void (*done)(NetDev *n);
129
130         /* fill in message to create netdev */
131         int (*fill_message_create)(NetDev *netdev, Link *link, sd_rtnl_message *message);
132
133         /* specifies if netdev is independent, or a master device or a stacked device */
134         NetDevCreateType create_type;
135
136         /* create netdev, if not done via rtnl */
137         int (*create)(NetDev *netdev);
138
139         /* verify that compulsory configuration options were specified */
140         int (*config_verify)(NetDev *netdev, const char *filename);
141 };
142
143 extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
144
145 #define NETDEV_VTABLE(n) netdev_vtable[(n)->kind]
146
147 /* For casting a netdev into the various netdev kinds */
148 #define DEFINE_CAST(UPPERCASE, MixedCase)                                   \
149         static inline MixedCase* UPPERCASE(NetDev *n) {                     \
150                 if (_unlikely_(!n || n->kind != NETDEV_KIND_##UPPERCASE))   \
151                         return NULL;                                        \
152                                                                             \
153                 return (MixedCase*) n;                                      \
154         }
155
156 /* For casting the various netdev kinds into a netdev */
157 #define NETDEV(n) (&(n)->meta)
158
159 DEFINE_CAST(BRIDGE, Bridge);
160 DEFINE_CAST(BOND, Bond);
161 DEFINE_CAST(VLAN, VLan);
162 DEFINE_CAST(MACVLAN, MacVlan);
163 DEFINE_CAST(IPVLAN, IPVlan);
164 DEFINE_CAST(VXLAN, VxLan);
165 DEFINE_CAST(IPIP, Tunnel);
166 DEFINE_CAST(GRE, Tunnel);
167 DEFINE_CAST(GRETAP, Tunnel);
168 DEFINE_CAST(SIT, Tunnel);
169 DEFINE_CAST(VTI, Tunnel);
170 DEFINE_CAST(VETH, Veth);
171 DEFINE_CAST(DUMMY, Dummy);
172 DEFINE_CAST(TUN, TunTap);
173 DEFINE_CAST(TAP, TunTap);
174
175 int netdev_load(Manager *manager);
176 void netdev_drop(NetDev *netdev);
177
178 NetDev *netdev_unref(NetDev *netdev);
179 NetDev *netdev_ref(NetDev *netdev);
180
181 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
182 #define _cleanup_netdev_unref_ _cleanup_(netdev_unrefp)
183
184 int netdev_get(Manager *manager, const char *name, NetDev **ret);
185 int netdev_set_ifindex(NetDev *netdev, sd_rtnl_message *newlink);
186 int netdev_enslave(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback);
187 int netdev_get_mac(const char *ifname, struct ether_addr **ret);
188 int netdev_join(NetDev *netdev, Link *link, sd_rtnl_message_handler_t cb);
189
190 const char *netdev_kind_to_string(NetDevKind d) _const_;
191 NetDevKind netdev_kind_from_string(const char *d) _pure_;
192
193 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);
194
195 /* gperf */
196 const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, unsigned length);
197
198 /* Macros which append INTERFACE= to the message */
199
200 #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__)
201 #define log_netdev_debug(netdev, ...)       log_full_netdev(LOG_DEBUG, netdev, ##__VA_ARGS__)
202 #define log_info_netdev(netdev, ...)        log_full_netdev(LOG_INFO, netdev, ##__VA_ARGS__)
203 #define log_notice_netdev(netdev, ...)      log_full_netdev(LOG_NOTICE, netdev, ##__VA_ARGS__)
204 #define log_warning_netdev(netdev, ...)     log_full_netdev(LOG_WARNING, netdev,## __VA_ARGS__)
205 #define log_netdev_error(netdev, ...)       log_full_netdev(LOG_ERR, netdev, ##__VA_ARGS__)
206
207 #define log_struct_netdev(level, netdev, ...) log_struct(level, "INTERFACE=%s", netdev->ifname, __VA_ARGS__)
208
209 #define NETDEVIF(netdev) "INTERFACE=%s", netdev->ifname