if_team.h 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * include/linux/if_team.h - Network team device driver header
 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */
#ifndef _LINUX_IF_TEAM_H_
#define _LINUX_IF_TEAM_H_

J
Jiri Pirko 已提交
13
#include <linux/netpoll.h>
J
Jiri Pirko 已提交
14
#include <net/sch_generic.h>
J
Jiri Pirko 已提交
15
#include <linux/types.h>
16
#include <uapi/linux/if_team.h>
J
Jiri Pirko 已提交
17

18 19 20 21 22 23 24 25 26
struct team_pcpu_stats {
	u64			rx_packets;
	u64			rx_bytes;
	u64			rx_multicast;
	u64			tx_packets;
	u64			tx_bytes;
	struct u64_stats_sync	syncp;
	u32			rx_dropped;
	u32			tx_dropped;
27
	u32			rx_nohandler;
28 29 30 31 32 33
};

struct team;

struct team_port {
	struct net_device *dev;
J
Jiri Pirko 已提交
34
	struct hlist_node hlist; /* node in enabled ports hash list */
35 36
	struct list_head list; /* node in ordinary list */
	struct team *team;
J
Jiri Pirko 已提交
37
	int index; /* index of enabled port. If disabled, it's set to -1 */
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	bool linkup; /* either state.linkup or user.linkup */

	struct {
		bool linkup;
		u32 speed;
		u8 duplex;
	} state;

	/* Values set by userspace */
	struct {
		bool linkup;
		bool linkup_enabled;
	} user;

	/* Custom gennetlink interface related flags */
	bool changed;
	bool removed;

57 58 59 60 61 62 63 64 65
	/*
	 * A place for storing original values of the device before it
	 * become a port.
	 */
	struct {
		unsigned char dev_addr[MAX_ADDR_LEN];
		unsigned int mtu;
	} orig;

J
Jiri Pirko 已提交
66 67 68 69
#ifdef CONFIG_NET_POLL_CONTROLLER
	struct netpoll *np;
#endif

J
Jiri Pirko 已提交
70
	s32 priority; /* lower number ~ higher priority */
71 72
	u16 queue_id;
	struct list_head qom_list; /* node in queue override mapping list */
73
	struct rcu_head	rcu;
J
Jiri Pirko 已提交
74
	long mode_priv[0];
75 76
};

77 78 79 80 81 82 83 84 85
static inline bool team_port_enabled(struct team_port *port)
{
	return port->index != -1;
}

static inline bool team_port_txable(struct team_port *port)
{
	return port->linkup && team_port_enabled(port);
}
86

J
Jiri Pirko 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#ifdef CONFIG_NET_POLL_CONTROLLER
static inline void team_netpoll_send_skb(struct team_port *port,
					 struct sk_buff *skb)
{
	struct netpoll *np = port->np;

	if (np)
		netpoll_send_skb(np, skb);
}
#else
static inline void team_netpoll_send_skb(struct team_port *port,
					 struct sk_buff *skb)
{
}
#endif

103 104 105 106 107 108 109 110 111
struct team_mode_ops {
	int (*init)(struct team *team);
	void (*exit)(struct team *team);
	rx_handler_result_t (*receive)(struct team *team,
				       struct team_port *port,
				       struct sk_buff *skb);
	bool (*transmit)(struct team *team, struct sk_buff *skb);
	int (*port_enter)(struct team *team, struct team_port *port);
	void (*port_leave)(struct team *team, struct team_port *port);
112
	void (*port_change_dev_addr)(struct team *team, struct team_port *port);
113 114
	void (*port_enabled)(struct team *team, struct team_port *port);
	void (*port_disabled)(struct team *team, struct team_port *port);
115 116
};

117 118 119 120
extern int team_modeop_port_enter(struct team *team, struct team_port *port);
extern void team_modeop_port_change_dev_addr(struct team *team,
					     struct team_port *port);

121 122 123
enum team_option_type {
	TEAM_OPTION_TYPE_U32,
	TEAM_OPTION_TYPE_STRING,
J
Jiri Pirko 已提交
124
	TEAM_OPTION_TYPE_BINARY,
J
Jiri Pirko 已提交
125
	TEAM_OPTION_TYPE_BOOL,
126
	TEAM_OPTION_TYPE_S32,
127 128
};

129 130 131 132 133
struct team_option_inst_info {
	u32 array_index;
	struct team_port *port; /* != NULL if per-port */
};

134 135 136 137 138 139 140 141
struct team_gsetter_ctx {
	union {
		u32 u32_val;
		const char *str_val;
		struct {
			const void *ptr;
			u32 len;
		} bin_val;
J
Jiri Pirko 已提交
142
		bool bool_val;
143
		s32 s32_val;
144
	} data;
145
	struct team_option_inst_info *info;
146 147
};

148 149 150
struct team_option {
	struct list_head list;
	const char *name;
151
	bool per_port;
J
Jiri Pirko 已提交
152
	unsigned int array_size; /* != 0 means the option is array */
153
	enum team_option_type type;
154
	int (*init)(struct team *team, struct team_option_inst_info *info);
155 156
	int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
	int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
157 158
};

J
Jiri Pirko 已提交
159 160 161
extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
extern void team_options_change_check(struct team *team);

162 163 164 165
struct team_mode {
	const char *kind;
	struct module *owner;
	size_t priv_size;
J
Jiri Pirko 已提交
166
	size_t port_priv_size;
167
	const struct team_mode_ops *ops;
168
	enum netdev_lag_tx_type lag_tx_type;
169 170 171 172 173 174 175 176 177 178 179 180
};

#define TEAM_PORT_HASHBITS 4
#define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)

#define TEAM_MODE_PRIV_LONGS 4
#define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)

struct team {
	struct net_device *dev; /* associated netdevice */
	struct team_pcpu_stats __percpu *pcpu_stats;

181
	struct mutex lock; /* used for overall locking, e.g. port lists write */
182 183

	/*
J
Jiri Pirko 已提交
184
	 * List of enabled ports and their count
185
	 */
J
Jiri Pirko 已提交
186 187 188 189
	int en_port_count;
	struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];

	struct list_head port_list; /* list of all ports */
190 191

	struct list_head option_list;
192
	struct list_head option_inst_list; /* list of option instances */
193 194 195

	const struct team_mode *mode;
	struct team_mode_ops ops;
196
	bool user_carrier_enabled;
197 198
	bool queue_override_enabled;
	struct list_head *qom_lists; /* array of queue override mapping lists */
J
Jiri Pirko 已提交
199
	bool port_mtu_change_allowed;
J
Jiri Pirko 已提交
200 201 202 203 204 205
	struct {
		unsigned int count;
		unsigned int interval; /* in ms */
		atomic_t count_pending;
		struct delayed_work dw;
	} notify_peers;
206 207 208 209 210 211
	struct {
		unsigned int count;
		unsigned int interval; /* in ms */
		atomic_t count_pending;
		struct delayed_work dw;
	} mcast_rejoin;
212 213 214
	long mode_priv[TEAM_MODE_PRIV_LONGS];
};

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
				      struct sk_buff *skb)
{
	BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
		     sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
	skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);

	skb->dev = port->dev;
	if (unlikely(netpoll_tx_running(team->dev))) {
		team_netpoll_send_skb(port, skb);
		return 0;
	}
	return dev_queue_xmit(skb);
}

230 231 232
static inline struct hlist_head *team_port_index_hash(struct team *team,
						      int port_index)
{
J
Jiri Pirko 已提交
233
	return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
234 235 236 237 238 239 240 241
}

static inline struct team_port *team_get_port_by_index(struct team *team,
						       int port_index)
{
	struct team_port *port;
	struct hlist_head *head = team_port_index_hash(team, port_index);

242
	hlist_for_each_entry(port, head, hlist)
243 244 245 246
		if (port->index == port_index)
			return port;
	return NULL;
}
247

248
static inline int team_num_to_port_index(struct team *team, unsigned int num)
249 250 251 252 253 254 255 256
{
	int en_port_count = ACCESS_ONCE(team->en_port_count);

	if (unlikely(!en_port_count))
		return 0;
	return num % en_port_count;
}

257 258 259 260 261 262
static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
							   int port_index)
{
	struct team_port *port;
	struct hlist_head *head = team_port_index_hash(team, port_index);

263
	hlist_for_each_entry_rcu(port, head, hlist)
264 265 266 267 268
		if (port->index == port_index)
			return port;
	return NULL;
}

J
Jiri Pirko 已提交
269 270 271 272 273 274 275 276 277
static inline struct team_port *
team_get_first_port_txable_rcu(struct team *team, struct team_port *port)
{
	struct team_port *cur;

	if (likely(team_port_txable(port)))
		return port;
	cur = port;
	list_for_each_entry_continue_rcu(cur, &team->port_list, list)
278
		if (team_port_txable(cur))
J
Jiri Pirko 已提交
279 280 281 282
			return cur;
	list_for_each_entry_rcu(cur, &team->port_list, list) {
		if (cur == port)
			break;
283
		if (team_port_txable(cur))
J
Jiri Pirko 已提交
284 285 286 287 288
			return cur;
	}
	return NULL;
}

J
Jiri Pirko 已提交
289 290 291
extern int team_options_register(struct team *team,
				 const struct team_option *option,
				 size_t option_count);
292
extern void team_options_unregister(struct team *team,
J
Jiri Pirko 已提交
293
				    const struct team_option *option,
294
				    size_t option_count);
J
Jiri Pirko 已提交
295 296
extern int team_mode_register(const struct team_mode *mode);
extern void team_mode_unregister(const struct team_mode *mode);
297

J
Jiri Pirko 已提交
298 299 300
#define TEAM_DEFAULT_NUM_TX_QUEUES 16
#define TEAM_DEFAULT_NUM_RX_QUEUES 16

301
#endif /* _LINUX_IF_TEAM_H_ */