Commit c7211b6e authored by Paolo Abeni's avatar Paolo Abeni
Browse files

Merge branch 'decouple-receive-and-transmit-enablement-in-team-driver'



Marc Harvey says:

====================
Decouple receive and transmit enablement in team driver

Allow independent control over receive and transmit enablement states
for aggregated ports in the team driver.

The motivation is that IEE 802.3ad LACP "independent control" can't
be implemented for the team driver currently. This was added to the
bonding driver in commit 240fd405 ("bonding: Add independent
control state machine").

This series also has a few patches that add tests to show that the old
coupled enablement still works and that the new decoupled enablement
works as intended (4, 5, and 10).

There are three patches with small fixes as well, with the goal of
making the final decoupling patch clearer (1, 2, and 3).

Signed-off-by: default avatarMarc Harvey <marcharvey@google.com>
====================

Link: https://patch.msgid.link/20260409-teaming-driver-internal-v7-0-f47e7589685d@google.com


Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parents 8806d502 d3870724
Loading
Loading
Loading
Loading
+199 −38
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ static void team_lower_state_changed(struct team_port *port)
	struct netdev_lag_lower_state_info info;

	info.link_up = port->linkup;
	info.tx_enabled = team_port_enabled(port);
	info.tx_enabled = team_port_tx_enabled(port);
	netdev_lower_state_changed(port->dev, &info);
}

@@ -532,13 +532,13 @@ static void team_adjust_ops(struct team *team)
	 * correct ops are always set.
	 */

	if (!team->en_port_count || !team_is_mode_set(team) ||
	if (!team->tx_en_port_count || !team_is_mode_set(team) ||
	    !team->mode->ops->transmit)
		team->ops.transmit = team_dummy_transmit;
	else
		team->ops.transmit = team->mode->ops->transmit;

	if (!team->en_port_count || !team_is_mode_set(team) ||
	if (!team->rx_en_port_count || !team_is_mode_set(team) ||
	    !team->mode->ops->receive)
		team->ops.receive = team_dummy_receive;
	else
@@ -734,7 +734,7 @@ static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)

	port = team_port_get_rcu(skb->dev);
	team = port->team;
	if (!team_port_enabled(port)) {
	if (!team_port_rx_enabled(port)) {
		if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
			/* link-local packets are mostly useful when stack receives them
			 * with the link they arrive on.
@@ -831,7 +831,7 @@ static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
		return true;
	if (port->priority > cur->priority)
		return false;
	if (port->index < cur->index)
	if (port->tx_index < cur->tx_index)
		return true;
	return false;
}
@@ -876,7 +876,7 @@ static void __team_queue_override_enabled_check(struct team *team)
static void team_queue_override_port_prio_changed(struct team *team,
						  struct team_port *port)
{
	if (!port->queue_id || !team_port_enabled(port))
	if (!port->queue_id || !team_port_tx_enabled(port))
		return;
	__team_queue_override_port_del(team, port);
	__team_queue_override_port_add(team, port);
@@ -887,7 +887,7 @@ static void team_queue_override_port_change_queue_id(struct team *team,
						     struct team_port *port,
						     u16 new_queue_id)
{
	if (team_port_enabled(port)) {
	if (team_port_tx_enabled(port)) {
		__team_queue_override_port_del(team, port);
		port->queue_id = new_queue_id;
		__team_queue_override_port_add(team, port);
@@ -927,56 +927,163 @@ static bool team_port_find(const struct team *team,
	return false;
}

static void __team_port_enable_rx(struct team *team,
				  struct team_port *port)
{
	team->rx_en_port_count++;
	WRITE_ONCE(port->rx_enabled, true);
}

static void __team_port_disable_rx(struct team *team,
				   struct team_port *port)
{
	team->rx_en_port_count--;
	WRITE_ONCE(port->rx_enabled, false);
}

static void team_port_enable_rx(struct team *team,
				struct team_port *port)
{
	if (team_port_rx_enabled(port))
		return;

	__team_port_enable_rx(team, port);
	team_adjust_ops(team);
	team_notify_peers(team);
	team_mcast_rejoin(team);
}

static void team_port_disable_rx(struct team *team,
				 struct team_port *port)
{
	if (!team_port_rx_enabled(port))
		return;

	__team_port_disable_rx(team, port);
	team_adjust_ops(team);
}

/*
 * Enable/disable port by adding to enabled port hashlist and setting
 * port->index (Might be racy so reader could see incorrect ifindex when
 * processing a flying packet, but that is not a problem). Write guarded
 * by RTNL.
 * Enable just TX on the port by adding to tx-enabled port hashlist and
 * setting port->tx_index (Might be racy so reader could see incorrect
 * ifindex when processing a flying packet, but that is not a problem).
 * Write guarded by RTNL.
 */
static void team_port_enable(struct team *team,
static void __team_port_enable_tx(struct team *team,
				  struct team_port *port)
{
	if (team_port_enabled(port))
	WRITE_ONCE(port->tx_index, team->tx_en_port_count);
	WRITE_ONCE(team->tx_en_port_count, team->tx_en_port_count + 1);
	hlist_add_head_rcu(&port->tx_hlist,
			   team_tx_port_index_hash(team, port->tx_index));
}

static void team_port_enable_tx(struct team *team,
				struct team_port *port)
{
	if (team_port_tx_enabled(port))
		return;
	port->index = team->en_port_count++;
	hlist_add_head_rcu(&port->hlist,
			   team_port_index_hash(team, port->index));

	__team_port_enable_tx(team, port);
	team_adjust_ops(team);
	team_queue_override_port_add(team, port);
	if (team->ops.port_enabled)
		team->ops.port_enabled(team, port);

	/* Don't rejoin multicast, since this port might not be receiving. */
	team_notify_peers(team);
	team_mcast_rejoin(team);
	team_lower_state_changed(port);
}

static void __reconstruct_port_hlist(struct team *team, int rm_index)
{
	int i;
	struct hlist_head *tx_port_index_hash;
	struct team_port *port;
	int i;

	for (i = rm_index + 1; i < team->tx_en_port_count; i++) {
		port = team_get_port_by_tx_index(team, i);
		hlist_del_rcu(&port->tx_hlist);
		WRITE_ONCE(port->tx_index, port->tx_index - 1);
		tx_port_index_hash = team_tx_port_index_hash(team,
							     port->tx_index);
		hlist_add_head_rcu(&port->tx_hlist, tx_port_index_hash);
	}
}

static void __team_port_disable_tx(struct team *team,
				   struct team_port *port)
{
	if (team->ops.port_tx_disabled)
		team->ops.port_tx_disabled(team, port);

	hlist_del_rcu(&port->tx_hlist);
	__reconstruct_port_hlist(team, port->tx_index);

	for (i = rm_index + 1; i < team->en_port_count; i++) {
		port = team_get_port_by_index(team, i);
		hlist_del_rcu(&port->hlist);
		port->index--;
		hlist_add_head_rcu(&port->hlist,
				   team_port_index_hash(team, port->index));
	WRITE_ONCE(port->tx_index, -1);
	WRITE_ONCE(team->tx_en_port_count, team->tx_en_port_count - 1);
}

static void team_port_disable_tx(struct team *team,
				 struct team_port *port)
{
	if (!team_port_tx_enabled(port))
		return;

	__team_port_disable_tx(team, port);

	team_queue_override_port_del(team, port);
	team_adjust_ops(team);
	team_lower_state_changed(port);
}

/*
 * Enable TX AND RX on the port.
 */
static void team_port_enable(struct team *team,
			     struct team_port *port)
{
	bool rx_was_enabled;
	bool tx_was_enabled;

	if (team_port_enabled(port))
		return;

	rx_was_enabled = team_port_rx_enabled(port);
	tx_was_enabled = team_port_tx_enabled(port);

	if (!rx_was_enabled)
		__team_port_enable_rx(team, port);
	if (!tx_was_enabled)
		__team_port_enable_tx(team, port);

	team_adjust_ops(team);
	if (!tx_was_enabled)
		team_queue_override_port_add(team, port);
	team_notify_peers(team);
	if (!rx_was_enabled)
		team_mcast_rejoin(team);
	if (!tx_was_enabled)
		team_lower_state_changed(port);
}

static void team_port_disable(struct team *team,
			      struct team_port *port)
{
	if (!team_port_enabled(port))
	bool rx_was_enabled = team_port_rx_enabled(port);
	bool tx_was_enabled = team_port_tx_enabled(port);

	if (!tx_was_enabled && !rx_was_enabled)
		return;
	if (team->ops.port_disabled)
		team->ops.port_disabled(team, port);
	hlist_del_rcu(&port->hlist);
	__reconstruct_port_hlist(team, port->index);
	port->index = -1;
	team->en_port_count--;

	if (tx_was_enabled) {
		__team_port_disable_tx(team, port);
		team_queue_override_port_del(team, port);
	}
	if (rx_was_enabled)
		__team_port_disable_rx(team, port);

	team_adjust_ops(team);

	if (tx_was_enabled)
		team_lower_state_changed(port);
}

@@ -1245,7 +1352,7 @@ static int team_port_add(struct team *team, struct net_device *port_dev,
		netif_addr_unlock_bh(dev);
	}

	port->index = -1;
	WRITE_ONCE(port->tx_index, -1);
	list_add_tail_rcu(&port->list, &team->port_list);
	team_port_enable(team, port);
	netdev_compute_master_upper_features(dev, true);
@@ -1430,6 +1537,46 @@ static int team_port_en_option_set(struct team *team,
	return 0;
}

static void team_port_rx_en_option_get(struct team *team,
				       struct team_gsetter_ctx *ctx)
{
	struct team_port *port = ctx->info->port;

	ctx->data.bool_val = team_port_rx_enabled(port);
}

static int team_port_rx_en_option_set(struct team *team,
				      struct team_gsetter_ctx *ctx)
{
	struct team_port *port = ctx->info->port;

	if (ctx->data.bool_val)
		team_port_enable_rx(team, port);
	else
		team_port_disable_rx(team, port);
	return 0;
}

static void team_port_tx_en_option_get(struct team *team,
				       struct team_gsetter_ctx *ctx)
{
	struct team_port *port = ctx->info->port;

	ctx->data.bool_val = team_port_tx_enabled(port);
}

static int team_port_tx_en_option_set(struct team *team,
				      struct team_gsetter_ctx *ctx)
{
	struct team_port *port = ctx->info->port;

	if (ctx->data.bool_val)
		team_port_enable_tx(team, port);
	else
		team_port_disable_tx(team, port);
	return 0;
}

static void team_user_linkup_option_get(struct team *team,
					struct team_gsetter_ctx *ctx)
{
@@ -1551,6 +1698,20 @@ static const struct team_option team_options[] = {
		.getter = team_port_en_option_get,
		.setter = team_port_en_option_set,
	},
	{
		.name = "rx_enabled",
		.type = TEAM_OPTION_TYPE_BOOL,
		.per_port = true,
		.getter = team_port_rx_en_option_get,
		.setter = team_port_rx_en_option_set,
	},
	{
		.name = "tx_enabled",
		.type = TEAM_OPTION_TYPE_BOOL,
		.per_port = true,
		.getter = team_port_tx_en_option_get,
		.setter = team_port_tx_en_option_set,
	},
	{
		.name = "user_linkup",
		.type = TEAM_OPTION_TYPE_BOOL,
@@ -1596,7 +1757,7 @@ static int team_init(struct net_device *dev)
		return -ENOMEM;

	for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
		INIT_HLIST_HEAD(&team->en_port_hlist[i]);
		INIT_HLIST_HEAD(&team->tx_en_port_hlist[i]);
	INIT_LIST_HEAD(&team->port_list);
	err = team_queue_override_init(team);
	if (err)
+4 −4
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ static struct team_port *lb_hash_select_tx_port(struct team *team,
{
	int port_index = team_num_to_port_index(team, hash);

	return team_get_port_by_index_rcu(team, port_index);
	return team_get_port_by_tx_index_rcu(team, port_index);
}

/* Hash to port mapping select tx port */
@@ -380,7 +380,7 @@ static int lb_tx_hash_to_port_mapping_set(struct team *team,

	list_for_each_entry(port, &team->port_list, list) {
		if (ctx->data.u32_val == port->dev->ifindex &&
		    team_port_enabled(port)) {
		    team_port_tx_enabled(port)) {
			rcu_assign_pointer(LB_HTPM_PORT_BY_HASH(lb_priv, hash),
					   port);
			return 0;
@@ -655,7 +655,7 @@ static void lb_port_leave(struct team *team, struct team_port *port)
	free_percpu(lb_port_priv->pcpu_stats);
}

static void lb_port_disabled(struct team *team, struct team_port *port)
static void lb_port_tx_disabled(struct team *team, struct team_port *port)
{
	lb_tx_hash_to_port_mapping_null_port(team, port);
}
@@ -665,7 +665,7 @@ static const struct team_mode_ops lb_mode_ops = {
	.exit			= lb_exit,
	.port_enter		= lb_port_enter,
	.port_leave		= lb_port_leave,
	.port_disabled		= lb_port_disabled,
	.port_tx_disabled	= lb_port_tx_disabled,
	.receive		= lb_receive,
	.transmit		= lb_transmit,
};
+2 −2
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@ static bool rnd_transmit(struct team *team, struct sk_buff *skb)
	struct team_port *port;
	int port_index;

	port_index = get_random_u32_below(team->en_port_count);
	port = team_get_port_by_index_rcu(team, port_index);
	port_index = get_random_u32_below(READ_ONCE(team->tx_en_port_count));
	port = team_get_port_by_tx_index_rcu(team, port_index);
	if (unlikely(!port))
		goto drop;
	port = team_get_first_port_txable_rcu(team, port);
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ static bool rr_transmit(struct team *team, struct sk_buff *skb)

	port_index = team_num_to_port_index(team,
					    rr_priv(team)->sent_packets++);
	port = team_get_port_by_index_rcu(team, port_index);
	port = team_get_port_by_tx_index_rcu(team, port_index);
	if (unlikely(!port))
		goto drop;
	port = team_get_first_port_txable_rcu(team, port);
+38 −25
Original line number Diff line number Diff line
@@ -27,10 +27,11 @@ struct team;

struct team_port {
	struct net_device *dev;
	struct hlist_node hlist; /* node in enabled ports hash list */
	struct hlist_node tx_hlist; /* node in tx-enabled ports hash list */
	struct list_head list; /* node in ordinary list */
	struct team *team;
	int index; /* index of enabled port. If disabled, it's set to -1 */
	int tx_index; /* index of tx enabled port. If disabled, -1 */
	bool rx_enabled;

	bool linkup; /* either state.linkup or user.linkup */

@@ -75,14 +76,24 @@ static inline struct team_port *team_port_get_rcu(const struct net_device *dev)
	return rcu_dereference(dev->rx_handler_data);
}

static inline bool team_port_rx_enabled(struct team_port *port)
{
	return READ_ONCE(port->rx_enabled);
}

static inline bool team_port_tx_enabled(struct team_port *port)
{
	return READ_ONCE(port->tx_index) != -1;
}

static inline bool team_port_enabled(struct team_port *port)
{
	return port->index != -1;
	return team_port_rx_enabled(port) && team_port_tx_enabled(port);
}

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

static inline bool team_port_dev_txable(const struct net_device *port_dev)
@@ -121,8 +132,7 @@ struct team_mode_ops {
	int (*port_enter)(struct team *team, struct team_port *port);
	void (*port_leave)(struct team *team, struct team_port *port);
	void (*port_change_dev_addr)(struct team *team, struct team_port *port);
	void (*port_enabled)(struct team *team, struct team_port *port);
	void (*port_disabled)(struct team *team, struct team_port *port);
	void (*port_tx_disabled)(struct team *team, struct team_port *port);
};

extern int team_modeop_port_enter(struct team *team, struct team_port *port);
@@ -191,10 +201,11 @@ struct team {
	const struct header_ops *header_ops_cache;

	/*
	 * List of enabled ports and their count
	 * List of tx-enabled ports and counts of rx and tx-enabled ports.
	 */
	int en_port_count;
	struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
	int tx_en_port_count;
	int rx_en_port_count;
	struct hlist_head tx_en_port_hlist[TEAM_PORT_HASHENTRIES];

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

@@ -238,41 +249,43 @@ static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
	return dev_queue_xmit(skb);
}

static inline struct hlist_head *team_port_index_hash(struct team *team,
						      int port_index)
static inline struct hlist_head *team_tx_port_index_hash(struct team *team,
							 int tx_port_index)
{
	return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
	unsigned int list_entry = tx_port_index & (TEAM_PORT_HASHENTRIES - 1);

	return &team->tx_en_port_hlist[list_entry];
}

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

	hlist_for_each_entry(port, head, hlist)
		if (port->index == port_index)
	hlist_for_each_entry(port, head, tx_hlist)
		if (port->tx_index == tx_port_index)
			return port;
	return NULL;
}

static inline int team_num_to_port_index(struct team *team, unsigned int num)
{
	int en_port_count = READ_ONCE(team->en_port_count);
	int tx_en_port_count = READ_ONCE(team->tx_en_port_count);

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

static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
							   int port_index)
static inline struct team_port *team_get_port_by_tx_index_rcu(struct team *team,
							      int tx_port_index)
{
	struct hlist_head *head = team_tx_port_index_hash(team, tx_port_index);
	struct team_port *port;
	struct hlist_head *head = team_port_index_hash(team, port_index);

	hlist_for_each_entry_rcu(port, head, hlist)
		if (port->index == port_index)
	hlist_for_each_entry_rcu(port, head, tx_hlist)
		if (READ_ONCE(port->tx_index) == tx_port_index)
			return port;
	return NULL;
}
Loading