]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/netdev/netdev.h
tree-wide: use unsigned for refcount
[thirdparty/systemd.git] / src / network / netdev / netdev.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
0ef6f454
LP
2#pragma once
3
634f0f98 4#include "sd-netlink.h"
3be1d7e0 5
634f0f98
ZJS
6#include "list.h"
7#include "time-util.h"
fc2f9534 8
3be1d7e0 9typedef struct netdev_join_callback netdev_join_callback;
634f0f98 10typedef struct Link Link;
3be1d7e0
TG
11
12struct netdev_join_callback {
1c4baffc 13 sd_netlink_message_handler_t callback;
3be1d7e0
TG
14 Link *link;
15
16 LIST_FIELDS(netdev_join_callback, callbacks);
17};
18
19typedef enum NetDevKind {
20 NETDEV_KIND_BRIDGE,
21 NETDEV_KIND_BOND,
22 NETDEV_KIND_VLAN,
23 NETDEV_KIND_MACVLAN,
f33ff02b 24 NETDEV_KIND_MACVTAP,
c4a5ddc9 25 NETDEV_KIND_IPVLAN,
3be1d7e0
TG
26 NETDEV_KIND_VXLAN,
27 NETDEV_KIND_IPIP,
28 NETDEV_KIND_GRE,
1af2536a 29 NETDEV_KIND_GRETAP,
b16492f8
SS
30 NETDEV_KIND_IP6GRE,
31 NETDEV_KIND_IP6GRETAP,
3be1d7e0
TG
32 NETDEV_KIND_SIT,
33 NETDEV_KIND_VETH,
34 NETDEV_KIND_VTI,
9011ce77 35 NETDEV_KIND_VTI6,
855ee1a1 36 NETDEV_KIND_IP6TNL,
3be1d7e0
TG
37 NETDEV_KIND_DUMMY,
38 NETDEV_KIND_TUN,
39 NETDEV_KIND_TAP,
20897a0d 40 NETDEV_KIND_VRF,
92c918b0 41 NETDEV_KIND_VCAN,
6598e046 42 NETDEV_KIND_GENEVE,
d6df583c 43 NETDEV_KIND_VXCAN,
e5719363 44 NETDEV_KIND_WIREGUARD,
56e7fb50 45 NETDEV_KIND_NETDEVSIM,
53cb501a 46 NETDEV_KIND_FOU,
3be1d7e0
TG
47 _NETDEV_KIND_MAX,
48 _NETDEV_KIND_INVALID = -1
49} NetDevKind;
50
51typedef enum NetDevState {
f3c33b23 52 NETDEV_STATE_LOADING,
3be1d7e0
TG
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
aa9f1140
TG
61typedef 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
634f0f98
ZJS
69typedef struct Manager Manager;
70typedef struct Condition Condition;
71
72typedef struct NetDev {
3be1d7e0
TG
73 Manager *manager;
74
cf4b2f99 75 unsigned n_ref;
3be1d7e0
TG
76
77 char *filename;
78
79 Condition *match_host;
80 Condition *match_virt;
5022f08a
LP
81 Condition *match_kernel_cmdline;
82 Condition *match_kernel_version;
3be1d7e0
TG
83 Condition *match_arch;
84
aa9f1140
TG
85 NetDevState state;
86 NetDevKind kind;
3be1d7e0
TG
87 char *description;
88 char *ifname;
3be1d7e0 89 struct ether_addr *mac;
4e964aa0 90 uint32_t mtu;
3be1d7e0 91 int ifindex;
3be1d7e0
TG
92
93 LIST_HEAD(netdev_join_callback, callbacks);
634f0f98 94} NetDev;
3be1d7e0 95
634f0f98 96typedef struct NetDevVTable {
aa9f1140
TG
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;
3be1d7e0 103
aa9f1140
TG
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);
3be1d7e0 109
aa9f1140
TG
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 */
1c4baffc 115 int (*fill_message_create)(NetDev *netdev, Link *link, sd_netlink_message *message);
aa9f1140
TG
116
117 /* specifies if netdev is independent, or a master device or a stacked device */
118 NetDevCreateType create_type;
3be1d7e0
TG
119
120 /* create netdev, if not done via rtnl */
121 int (*create)(NetDev *netdev);
122
540eb5f0
SS
123 /* perform additional configuration after netdev has been createad */
124 int (*post_create)(NetDev *netdev, Link *link, sd_netlink_message *message);
125
3be1d7e0
TG
126 /* verify that compulsory configuration options were specified */
127 int (*config_verify)(NetDev *netdev, const char *filename);
634f0f98 128} NetDevVTable;
3be1d7e0
TG
129
130extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
131
281bb5c1 132#define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
3be1d7e0 133
aa9f1140 134/* For casting a netdev into the various netdev kinds */
634f0f98 135#define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
aa9f1140 136 static inline MixedCase* UPPERCASE(NetDev *n) { \
f3c33b23
LP
137 if (_unlikely_(!n || \
138 n->kind != NETDEV_KIND_##UPPERCASE) || \
139 n->state == _NETDEV_STATE_INVALID) \
aa9f1140
TG
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
3be1d7e0
TG
148int netdev_load(Manager *manager);
149void netdev_drop(NetDev *netdev);
150
151NetDev *netdev_unref(NetDev *netdev);
152NetDev *netdev_ref(NetDev *netdev);
153
154DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
3be1d7e0
TG
155
156int netdev_get(Manager *manager, const char *name, NetDev **ret);
1c4baffc
TG
157int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *newlink);
158int netdev_enslave(NetDev *netdev, Link *link, sd_netlink_message_handler_t callback);
3be1d7e0 159int netdev_get_mac(const char *ifname, struct ether_addr **ret);
1c4baffc 160int netdev_join(NetDev *netdev, Link *link, sd_netlink_message_handler_t cb);
3be1d7e0
TG
161
162const char *netdev_kind_to_string(NetDevKind d) _const_;
163NetDevKind netdev_kind_from_string(const char *d) _pure_;
164
165int 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 */
c9f7b4d3 168const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
3be1d7e0
TG
169
170/* Macros which append INTERFACE= to the message */
171
f2341e0a
LP
172#define log_netdev_full(netdev, level, error, ...) \
173 ({ \
8dec4a9d 174 const NetDev *_n = (netdev); \
4b58153d 175 _n ? log_object_internal(level, error, __FILE__, __LINE__, __func__, "INTERFACE=", _n->ifname, NULL, NULL, ##__VA_ARGS__) : \
f2341e0a
LP
176 log_internal(level, error, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
177 })
6a7a4e4d
LP
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__)
98b32556 190
f2341e0a
LP
191#define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
192#define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname