Commit | Line | Data |
---|---|---|
3d249d4c JP |
1 | /* |
2 | * include/linux/if_team.h - Network team device driver header | |
3 | * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | */ | |
10 | ||
11 | #ifndef _LINUX_IF_TEAM_H_ | |
12 | #define _LINUX_IF_TEAM_H_ | |
13 | ||
14 | #ifdef __KERNEL__ | |
15 | ||
bd2d0837 | 16 | #include <linux/netpoll.h> |
6c85f2bd | 17 | #include <net/sch_generic.h> |
bd2d0837 | 18 | |
3d249d4c JP |
19 | struct team_pcpu_stats { |
20 | u64 rx_packets; | |
21 | u64 rx_bytes; | |
22 | u64 rx_multicast; | |
23 | u64 tx_packets; | |
24 | u64 tx_bytes; | |
25 | struct u64_stats_sync syncp; | |
26 | u32 rx_dropped; | |
27 | u32 tx_dropped; | |
28 | }; | |
29 | ||
30 | struct team; | |
31 | ||
32 | struct team_port { | |
33 | struct net_device *dev; | |
19a0b58e | 34 | struct hlist_node hlist; /* node in enabled ports hash list */ |
3d249d4c JP |
35 | struct list_head list; /* node in ordinary list */ |
36 | struct team *team; | |
19a0b58e | 37 | int index; /* index of enabled port. If disabled, it's set to -1 */ |
3d249d4c | 38 | |
71472ec1 JP |
39 | bool linkup; /* either state.linkup or user.linkup */ |
40 | ||
41 | struct { | |
42 | bool linkup; | |
43 | u32 speed; | |
44 | u8 duplex; | |
45 | } state; | |
46 | ||
47 | /* Values set by userspace */ | |
48 | struct { | |
49 | bool linkup; | |
50 | bool linkup_enabled; | |
51 | } user; | |
52 | ||
53 | /* Custom gennetlink interface related flags */ | |
54 | bool changed; | |
55 | bool removed; | |
56 | ||
3d249d4c JP |
57 | /* |
58 | * A place for storing original values of the device before it | |
59 | * become a port. | |
60 | */ | |
61 | struct { | |
62 | unsigned char dev_addr[MAX_ADDR_LEN]; | |
63 | unsigned int mtu; | |
64 | } orig; | |
65 | ||
bd2d0837 JP |
66 | #ifdef CONFIG_NET_POLL_CONTROLLER |
67 | struct netpoll *np; | |
68 | #endif | |
69 | ||
a86fc6b7 | 70 | s32 priority; /* lower number ~ higher priority */ |
5149ee58 | 71 | long mode_priv[0]; |
3d249d4c JP |
72 | }; |
73 | ||
68c45042 JP |
74 | static inline bool team_port_enabled(struct team_port *port) |
75 | { | |
76 | return port->index != -1; | |
77 | } | |
78 | ||
79 | static inline bool team_port_txable(struct team_port *port) | |
80 | { | |
81 | return port->linkup && team_port_enabled(port); | |
82 | } | |
52a4fd77 | 83 | |
bd2d0837 JP |
84 | #ifdef CONFIG_NET_POLL_CONTROLLER |
85 | static inline void team_netpoll_send_skb(struct team_port *port, | |
86 | struct sk_buff *skb) | |
87 | { | |
88 | struct netpoll *np = port->np; | |
89 | ||
90 | if (np) | |
91 | netpoll_send_skb(np, skb); | |
92 | } | |
93 | #else | |
94 | static inline void team_netpoll_send_skb(struct team_port *port, | |
95 | struct sk_buff *skb) | |
96 | { | |
97 | } | |
98 | #endif | |
99 | ||
100 | static inline int team_dev_queue_xmit(struct team *team, struct team_port *port, | |
101 | struct sk_buff *skb) | |
102 | { | |
6c85f2bd JP |
103 | BUILD_BUG_ON(sizeof(skb->queue_mapping) != |
104 | sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping)); | |
105 | skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping); | |
106 | ||
bd2d0837 JP |
107 | skb->dev = port->dev; |
108 | if (unlikely(netpoll_tx_running(port->dev))) { | |
109 | team_netpoll_send_skb(port, skb); | |
110 | return 0; | |
111 | } | |
112 | return dev_queue_xmit(skb); | |
113 | } | |
114 | ||
3d249d4c JP |
115 | struct team_mode_ops { |
116 | int (*init)(struct team *team); | |
117 | void (*exit)(struct team *team); | |
118 | rx_handler_result_t (*receive)(struct team *team, | |
119 | struct team_port *port, | |
120 | struct sk_buff *skb); | |
121 | bool (*transmit)(struct team *team, struct sk_buff *skb); | |
122 | int (*port_enter)(struct team *team, struct team_port *port); | |
123 | void (*port_leave)(struct team *team, struct team_port *port); | |
124 | void (*port_change_mac)(struct team *team, struct team_port *port); | |
4bccfd17 JP |
125 | void (*port_enabled)(struct team *team, struct team_port *port); |
126 | void (*port_disabled)(struct team *team, struct team_port *port); | |
3d249d4c JP |
127 | }; |
128 | ||
129 | enum team_option_type { | |
130 | TEAM_OPTION_TYPE_U32, | |
131 | TEAM_OPTION_TYPE_STRING, | |
2615598f | 132 | TEAM_OPTION_TYPE_BINARY, |
14f066ba | 133 | TEAM_OPTION_TYPE_BOOL, |
69821638 | 134 | TEAM_OPTION_TYPE_S32, |
3d249d4c JP |
135 | }; |
136 | ||
85d59a87 JP |
137 | struct team_option_inst_info { |
138 | u32 array_index; | |
139 | struct team_port *port; /* != NULL if per-port */ | |
140 | }; | |
141 | ||
80f7c668 JP |
142 | struct team_gsetter_ctx { |
143 | union { | |
144 | u32 u32_val; | |
145 | const char *str_val; | |
146 | struct { | |
147 | const void *ptr; | |
148 | u32 len; | |
149 | } bin_val; | |
14f066ba | 150 | bool bool_val; |
69821638 | 151 | s32 s32_val; |
80f7c668 | 152 | } data; |
85d59a87 | 153 | struct team_option_inst_info *info; |
80f7c668 JP |
154 | }; |
155 | ||
3d249d4c JP |
156 | struct team_option { |
157 | struct list_head list; | |
158 | const char *name; | |
80f7c668 | 159 | bool per_port; |
b1303326 | 160 | unsigned int array_size; /* != 0 means the option is array */ |
3d249d4c | 161 | enum team_option_type type; |
85d59a87 | 162 | int (*init)(struct team *team, struct team_option_inst_info *info); |
80f7c668 JP |
163 | int (*getter)(struct team *team, struct team_gsetter_ctx *ctx); |
164 | int (*setter)(struct team *team, struct team_gsetter_ctx *ctx); | |
3d249d4c JP |
165 | }; |
166 | ||
0f1aad2b JP |
167 | extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info); |
168 | extern void team_options_change_check(struct team *team); | |
169 | ||
3d249d4c | 170 | struct team_mode { |
3d249d4c JP |
171 | const char *kind; |
172 | struct module *owner; | |
173 | size_t priv_size; | |
5149ee58 | 174 | size_t port_priv_size; |
3d249d4c JP |
175 | const struct team_mode_ops *ops; |
176 | }; | |
177 | ||
178 | #define TEAM_PORT_HASHBITS 4 | |
179 | #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS) | |
180 | ||
181 | #define TEAM_MODE_PRIV_LONGS 4 | |
182 | #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS) | |
183 | ||
184 | struct team { | |
185 | struct net_device *dev; /* associated netdevice */ | |
186 | struct team_pcpu_stats __percpu *pcpu_stats; | |
187 | ||
61dc3461 | 188 | struct mutex lock; /* used for overall locking, e.g. port lists write */ |
3d249d4c JP |
189 | |
190 | /* | |
19a0b58e | 191 | * List of enabled ports and their count |
3d249d4c | 192 | */ |
19a0b58e JP |
193 | int en_port_count; |
194 | struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES]; | |
195 | ||
196 | struct list_head port_list; /* list of all ports */ | |
3d249d4c JP |
197 | |
198 | struct list_head option_list; | |
80f7c668 | 199 | struct list_head option_inst_list; /* list of option instances */ |
3d249d4c JP |
200 | |
201 | const struct team_mode *mode; | |
202 | struct team_mode_ops ops; | |
203 | long mode_priv[TEAM_MODE_PRIV_LONGS]; | |
204 | }; | |
205 | ||
206 | static inline struct hlist_head *team_port_index_hash(struct team *team, | |
207 | int port_index) | |
208 | { | |
19a0b58e | 209 | return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)]; |
3d249d4c JP |
210 | } |
211 | ||
212 | static inline struct team_port *team_get_port_by_index(struct team *team, | |
213 | int port_index) | |
214 | { | |
215 | struct hlist_node *p; | |
216 | struct team_port *port; | |
217 | struct hlist_head *head = team_port_index_hash(team, port_index); | |
218 | ||
219 | hlist_for_each_entry(port, p, head, hlist) | |
220 | if (port->index == port_index) | |
221 | return port; | |
222 | return NULL; | |
223 | } | |
224 | static inline struct team_port *team_get_port_by_index_rcu(struct team *team, | |
225 | int port_index) | |
226 | { | |
227 | struct hlist_node *p; | |
228 | struct team_port *port; | |
229 | struct hlist_head *head = team_port_index_hash(team, port_index); | |
230 | ||
231 | hlist_for_each_entry_rcu(port, p, head, hlist) | |
232 | if (port->index == port_index) | |
233 | return port; | |
234 | return NULL; | |
235 | } | |
236 | ||
237 | extern int team_port_set_team_mac(struct team_port *port); | |
358b8382 JP |
238 | extern int team_options_register(struct team *team, |
239 | const struct team_option *option, | |
240 | size_t option_count); | |
3d249d4c | 241 | extern void team_options_unregister(struct team *team, |
358b8382 | 242 | const struct team_option *option, |
3d249d4c | 243 | size_t option_count); |
0402788a JP |
244 | extern int team_mode_register(const struct team_mode *mode); |
245 | extern void team_mode_unregister(const struct team_mode *mode); | |
3d249d4c | 246 | |
6c85f2bd JP |
247 | #define TEAM_DEFAULT_NUM_TX_QUEUES 16 |
248 | #define TEAM_DEFAULT_NUM_RX_QUEUES 16 | |
249 | ||
3d249d4c JP |
250 | #endif /* __KERNEL__ */ |
251 | ||
252 | #define TEAM_STRING_MAX_LEN 32 | |
253 | ||
254 | /********************************** | |
255 | * NETLINK_GENERIC netlink family. | |
256 | **********************************/ | |
257 | ||
258 | enum { | |
259 | TEAM_CMD_NOOP, | |
260 | TEAM_CMD_OPTIONS_SET, | |
261 | TEAM_CMD_OPTIONS_GET, | |
262 | TEAM_CMD_PORT_LIST_GET, | |
263 | ||
264 | __TEAM_CMD_MAX, | |
265 | TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1), | |
266 | }; | |
267 | ||
268 | enum { | |
269 | TEAM_ATTR_UNSPEC, | |
270 | TEAM_ATTR_TEAM_IFINDEX, /* u32 */ | |
271 | TEAM_ATTR_LIST_OPTION, /* nest */ | |
272 | TEAM_ATTR_LIST_PORT, /* nest */ | |
273 | ||
274 | __TEAM_ATTR_MAX, | |
275 | TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1, | |
276 | }; | |
277 | ||
278 | /* Nested layout of get/set msg: | |
279 | * | |
280 | * [TEAM_ATTR_LIST_OPTION] | |
281 | * [TEAM_ATTR_ITEM_OPTION] | |
282 | * [TEAM_ATTR_OPTION_*], ... | |
283 | * [TEAM_ATTR_ITEM_OPTION] | |
284 | * [TEAM_ATTR_OPTION_*], ... | |
285 | * ... | |
286 | * [TEAM_ATTR_LIST_PORT] | |
287 | * [TEAM_ATTR_ITEM_PORT] | |
288 | * [TEAM_ATTR_PORT_*], ... | |
289 | * [TEAM_ATTR_ITEM_PORT] | |
290 | * [TEAM_ATTR_PORT_*], ... | |
291 | * ... | |
292 | */ | |
293 | ||
294 | enum { | |
295 | TEAM_ATTR_ITEM_OPTION_UNSPEC, | |
296 | TEAM_ATTR_ITEM_OPTION, /* nest */ | |
297 | ||
298 | __TEAM_ATTR_ITEM_OPTION_MAX, | |
299 | TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1, | |
300 | }; | |
301 | ||
302 | enum { | |
303 | TEAM_ATTR_OPTION_UNSPEC, | |
304 | TEAM_ATTR_OPTION_NAME, /* string */ | |
305 | TEAM_ATTR_OPTION_CHANGED, /* flag */ | |
306 | TEAM_ATTR_OPTION_TYPE, /* u8 */ | |
307 | TEAM_ATTR_OPTION_DATA, /* dynamic */ | |
b82b9183 | 308 | TEAM_ATTR_OPTION_REMOVED, /* flag */ |
80f7c668 | 309 | TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */ |
b1303326 | 310 | TEAM_ATTR_OPTION_ARRAY_INDEX, /* u32 */ /* for array options */ |
3d249d4c JP |
311 | |
312 | __TEAM_ATTR_OPTION_MAX, | |
313 | TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1, | |
314 | }; | |
315 | ||
316 | enum { | |
317 | TEAM_ATTR_ITEM_PORT_UNSPEC, | |
318 | TEAM_ATTR_ITEM_PORT, /* nest */ | |
319 | ||
320 | __TEAM_ATTR_ITEM_PORT_MAX, | |
321 | TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1, | |
322 | }; | |
323 | ||
324 | enum { | |
325 | TEAM_ATTR_PORT_UNSPEC, | |
326 | TEAM_ATTR_PORT_IFINDEX, /* u32 */ | |
327 | TEAM_ATTR_PORT_CHANGED, /* flag */ | |
328 | TEAM_ATTR_PORT_LINKUP, /* flag */ | |
329 | TEAM_ATTR_PORT_SPEED, /* u32 */ | |
330 | TEAM_ATTR_PORT_DUPLEX, /* u8 */ | |
b82b9183 | 331 | TEAM_ATTR_PORT_REMOVED, /* flag */ |
3d249d4c JP |
332 | |
333 | __TEAM_ATTR_PORT_MAX, | |
334 | TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1, | |
335 | }; | |
336 | ||
337 | /* | |
338 | * NETLINK_GENERIC related info | |
339 | */ | |
340 | #define TEAM_GENL_NAME "team" | |
341 | #define TEAM_GENL_VERSION 0x1 | |
342 | #define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event" | |
343 | ||
344 | #endif /* _LINUX_IF_TEAM_H_ */ |