blob: b159b10d915d1422ff0907a190a6bd72e727d756 [file] [log] [blame]
Jiri Pirko3d249d42011-11-11 22:16:48 +00001/*
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
16struct team_pcpu_stats {
17 u64 rx_packets;
18 u64 rx_bytes;
19 u64 rx_multicast;
20 u64 tx_packets;
21 u64 tx_bytes;
22 struct u64_stats_sync syncp;
23 u32 rx_dropped;
24 u32 tx_dropped;
25};
26
27struct team;
28
29struct team_port {
30 struct net_device *dev;
31 struct hlist_node hlist; /* node in hash list */
32 struct list_head list; /* node in ordinary list */
33 struct team *team;
34 int index;
35
36 /*
37 * A place for storing original values of the device before it
38 * become a port.
39 */
40 struct {
41 unsigned char dev_addr[MAX_ADDR_LEN];
42 unsigned int mtu;
43 } orig;
44
45 bool linkup;
46 u32 speed;
47 u8 duplex;
48
Jiri Pirkob82b9182012-01-24 05:16:00 +000049 /* Custom gennetlink interface related flags */
50 bool changed;
51 bool removed;
52
Jiri Pirko3d249d42011-11-11 22:16:48 +000053 struct rcu_head rcu;
54};
55
56struct team_mode_ops {
57 int (*init)(struct team *team);
58 void (*exit)(struct team *team);
59 rx_handler_result_t (*receive)(struct team *team,
60 struct team_port *port,
61 struct sk_buff *skb);
62 bool (*transmit)(struct team *team, struct sk_buff *skb);
63 int (*port_enter)(struct team *team, struct team_port *port);
64 void (*port_leave)(struct team *team, struct team_port *port);
65 void (*port_change_mac)(struct team *team, struct team_port *port);
66};
67
68enum team_option_type {
69 TEAM_OPTION_TYPE_U32,
70 TEAM_OPTION_TYPE_STRING,
71};
72
73struct team_option {
74 struct list_head list;
75 const char *name;
76 enum team_option_type type;
77 int (*getter)(struct team *team, void *arg);
78 int (*setter)(struct team *team, void *arg);
Jiri Pirkob82b9182012-01-24 05:16:00 +000079
80 /* Custom gennetlink interface related flags */
81 bool changed;
82 bool removed;
Jiri Pirko3d249d42011-11-11 22:16:48 +000083};
84
85struct team_mode {
86 struct list_head list;
87 const char *kind;
88 struct module *owner;
89 size_t priv_size;
90 const struct team_mode_ops *ops;
91};
92
93#define TEAM_PORT_HASHBITS 4
94#define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
95
96#define TEAM_MODE_PRIV_LONGS 4
97#define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
98
99struct team {
100 struct net_device *dev; /* associated netdevice */
101 struct team_pcpu_stats __percpu *pcpu_stats;
102
Jiri Pirko61dc3462011-11-16 11:09:08 +0000103 struct mutex lock; /* used for overall locking, e.g. port lists write */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000104
105 /*
106 * port lists with port count
107 */
108 int port_count;
109 struct hlist_head port_hlist[TEAM_PORT_HASHENTRIES];
110 struct list_head port_list;
111
112 struct list_head option_list;
113
114 const struct team_mode *mode;
115 struct team_mode_ops ops;
Jiri Pirko077178c2014-05-29 20:46:17 +0200116 bool port_mtu_change_allowed;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000117 long mode_priv[TEAM_MODE_PRIV_LONGS];
118};
119
120static inline struct hlist_head *team_port_index_hash(struct team *team,
121 int port_index)
122{
123 return &team->port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
124}
125
126static inline struct team_port *team_get_port_by_index(struct team *team,
127 int port_index)
128{
129 struct hlist_node *p;
130 struct team_port *port;
131 struct hlist_head *head = team_port_index_hash(team, port_index);
132
133 hlist_for_each_entry(port, p, head, hlist)
134 if (port->index == port_index)
135 return port;
136 return NULL;
137}
138static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
139 int port_index)
140{
141 struct hlist_node *p;
142 struct team_port *port;
143 struct hlist_head *head = team_port_index_hash(team, port_index);
144
145 hlist_for_each_entry_rcu(port, p, head, hlist)
146 if (port->index == port_index)
147 return port;
148 return NULL;
149}
150
151extern int team_port_set_team_mac(struct team_port *port);
Jiri Pirko358b8382011-11-16 11:09:09 +0000152extern int team_options_register(struct team *team,
153 const struct team_option *option,
154 size_t option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000155extern void team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000156 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000157 size_t option_count);
158extern int team_mode_register(struct team_mode *mode);
159extern int team_mode_unregister(struct team_mode *mode);
160
161#endif /* __KERNEL__ */
162
163#define TEAM_STRING_MAX_LEN 32
164
165/**********************************
166 * NETLINK_GENERIC netlink family.
167 **********************************/
168
169enum {
170 TEAM_CMD_NOOP,
171 TEAM_CMD_OPTIONS_SET,
172 TEAM_CMD_OPTIONS_GET,
173 TEAM_CMD_PORT_LIST_GET,
174
175 __TEAM_CMD_MAX,
176 TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
177};
178
179enum {
180 TEAM_ATTR_UNSPEC,
181 TEAM_ATTR_TEAM_IFINDEX, /* u32 */
182 TEAM_ATTR_LIST_OPTION, /* nest */
183 TEAM_ATTR_LIST_PORT, /* nest */
184
185 __TEAM_ATTR_MAX,
186 TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
187};
188
189/* Nested layout of get/set msg:
190 *
191 * [TEAM_ATTR_LIST_OPTION]
192 * [TEAM_ATTR_ITEM_OPTION]
193 * [TEAM_ATTR_OPTION_*], ...
194 * [TEAM_ATTR_ITEM_OPTION]
195 * [TEAM_ATTR_OPTION_*], ...
196 * ...
197 * [TEAM_ATTR_LIST_PORT]
198 * [TEAM_ATTR_ITEM_PORT]
199 * [TEAM_ATTR_PORT_*], ...
200 * [TEAM_ATTR_ITEM_PORT]
201 * [TEAM_ATTR_PORT_*], ...
202 * ...
203 */
204
205enum {
206 TEAM_ATTR_ITEM_OPTION_UNSPEC,
207 TEAM_ATTR_ITEM_OPTION, /* nest */
208
209 __TEAM_ATTR_ITEM_OPTION_MAX,
210 TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
211};
212
213enum {
214 TEAM_ATTR_OPTION_UNSPEC,
215 TEAM_ATTR_OPTION_NAME, /* string */
216 TEAM_ATTR_OPTION_CHANGED, /* flag */
217 TEAM_ATTR_OPTION_TYPE, /* u8 */
218 TEAM_ATTR_OPTION_DATA, /* dynamic */
Jiri Pirkob82b9182012-01-24 05:16:00 +0000219 TEAM_ATTR_OPTION_REMOVED, /* flag */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000220
221 __TEAM_ATTR_OPTION_MAX,
222 TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
223};
224
225enum {
226 TEAM_ATTR_ITEM_PORT_UNSPEC,
227 TEAM_ATTR_ITEM_PORT, /* nest */
228
229 __TEAM_ATTR_ITEM_PORT_MAX,
230 TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
231};
232
233enum {
234 TEAM_ATTR_PORT_UNSPEC,
235 TEAM_ATTR_PORT_IFINDEX, /* u32 */
236 TEAM_ATTR_PORT_CHANGED, /* flag */
237 TEAM_ATTR_PORT_LINKUP, /* flag */
238 TEAM_ATTR_PORT_SPEED, /* u32 */
239 TEAM_ATTR_PORT_DUPLEX, /* u8 */
Jiri Pirkob82b9182012-01-24 05:16:00 +0000240 TEAM_ATTR_PORT_REMOVED, /* flag */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000241
242 __TEAM_ATTR_PORT_MAX,
243 TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
244};
245
246/*
247 * NETLINK_GENERIC related info
248 */
249#define TEAM_GENL_NAME "team"
250#define TEAM_GENL_VERSION 0x1
251#define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
252
253#endif /* _LINUX_IF_TEAM_H_ */