xref: /openbmc/linux/net/tipc/bcast.c (revision 22d85c79)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/bcast.c: TIPC broadcast code
3b97bf3fdSPer Liden  *
43c724acdSJon Paul Maloy  * Copyright (c) 2004-2006, 2014-2015, Ericsson AB
5b97bf3fdSPer Liden  * Copyright (c) 2004, Intel Corporation.
62d627b92SAllan Stephens  * Copyright (c) 2005, 2010-2011, Wind River Systems
7b97bf3fdSPer Liden  * All rights reserved.
8b97bf3fdSPer Liden  *
9b97bf3fdSPer Liden  * Redistribution and use in source and binary forms, with or without
10b97bf3fdSPer Liden  * modification, are permitted provided that the following conditions are met:
11b97bf3fdSPer Liden  *
129ea1fd3cSPer Liden  * 1. Redistributions of source code must retain the above copyright
139ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer.
149ea1fd3cSPer Liden  * 2. Redistributions in binary form must reproduce the above copyright
159ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer in the
169ea1fd3cSPer Liden  *    documentation and/or other materials provided with the distribution.
179ea1fd3cSPer Liden  * 3. Neither the names of the copyright holders nor the names of its
189ea1fd3cSPer Liden  *    contributors may be used to endorse or promote products derived from
199ea1fd3cSPer Liden  *    this software without specific prior written permission.
209ea1fd3cSPer Liden  *
219ea1fd3cSPer Liden  * Alternatively, this software may be distributed under the terms of the
229ea1fd3cSPer Liden  * GNU General Public License ("GPL") version 2 as published by the Free
239ea1fd3cSPer Liden  * Software Foundation.
24b97bf3fdSPer Liden  *
25b97bf3fdSPer Liden  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26b97bf3fdSPer Liden  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27b97bf3fdSPer Liden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28b97bf3fdSPer Liden  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29b97bf3fdSPer Liden  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30b97bf3fdSPer Liden  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31b97bf3fdSPer Liden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32b97bf3fdSPer Liden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33b97bf3fdSPer Liden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34b97bf3fdSPer Liden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35b97bf3fdSPer Liden  * POSSIBILITY OF SUCH DAMAGE.
36b97bf3fdSPer Liden  */
37b97bf3fdSPer Liden 
38078bec82SJon Paul Maloy #include "socket.h"
39078bec82SJon Paul Maloy #include "msg.h"
40b97bf3fdSPer Liden #include "bcast.h"
419f6bdcd4SAllan Stephens #include "name_distr.h"
421da46568SYing Xue #include "core.h"
43b97bf3fdSPer Liden 
44b97bf3fdSPer Liden #define	MAX_PKT_DEFAULT_MCAST	1500	/* bcast link max packet size (fixed) */
45b97bf3fdSPer Liden #define	BCLINK_WIN_DEFAULT	20	/* bcast link window size (default) */
46c47e9b91SAllan Stephens 
473aec9cc9SAllan Stephens const char tipc_bclink_name[] = "broadcast-link";
48b97bf3fdSPer Liden 
4931e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
5031e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
5131e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_diff);
5228dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node);
5328dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node);
54b97bf3fdSPer Liden 
551da46568SYing Xue static void tipc_bclink_lock(struct net *net)
56d69afc90SYing Xue {
571da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
581da46568SYing Xue 
591da46568SYing Xue 	spin_lock_bh(&tn->bclink->lock);
60d69afc90SYing Xue }
61d69afc90SYing Xue 
621da46568SYing Xue static void tipc_bclink_unlock(struct net *net)
63d69afc90SYing Xue {
641da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
653f5a12bdSYing Xue 
661da46568SYing Xue 	spin_unlock_bh(&tn->bclink->lock);
673f5a12bdSYing Xue }
683f5a12bdSYing Xue 
69cb1b7280SJon Paul Maloy void tipc_bclink_input(struct net *net)
70cb1b7280SJon Paul Maloy {
71cb1b7280SJon Paul Maloy 	struct tipc_net *tn = net_generic(net, tipc_net_id);
72cb1b7280SJon Paul Maloy 
73cb1b7280SJon Paul Maloy 	tipc_sk_mcast_rcv(net, &tn->bclink->arrvq, &tn->bclink->inputq);
74cb1b7280SJon Paul Maloy }
75cb1b7280SJon Paul Maloy 
76078bec82SJon Paul Maloy uint  tipc_bclink_get_mtu(void)
77078bec82SJon Paul Maloy {
78078bec82SJon Paul Maloy 	return MAX_PKT_DEFAULT_MCAST;
79078bec82SJon Paul Maloy }
80078bec82SJon Paul Maloy 
8105790c64SSam Ravnborg static u32 bcbuf_acks(struct sk_buff *buf)
82b97bf3fdSPer Liden {
83880b005fSDavid S. Miller 	return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
84b97bf3fdSPer Liden }
85b97bf3fdSPer Liden 
8605790c64SSam Ravnborg static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
87b97bf3fdSPer Liden {
88880b005fSDavid S. Miller 	TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
89b97bf3fdSPer Liden }
90b97bf3fdSPer Liden 
9105790c64SSam Ravnborg static void bcbuf_decr_acks(struct sk_buff *buf)
92b97bf3fdSPer Liden {
93b97bf3fdSPer Liden 	bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
94b97bf3fdSPer Liden }
95b97bf3fdSPer Liden 
961da46568SYing Xue void tipc_bclink_add_node(struct net *net, u32 addr)
97cd3decdfSAllan Stephens {
981da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
991da46568SYing Xue 
1001da46568SYing Xue 	tipc_bclink_lock(net);
1011da46568SYing Xue 	tipc_nmap_add(&tn->bclink->bcast_nodes, addr);
1021da46568SYing Xue 	tipc_bclink_unlock(net);
103cd3decdfSAllan Stephens }
104cd3decdfSAllan Stephens 
1051da46568SYing Xue void tipc_bclink_remove_node(struct net *net, u32 addr)
106cd3decdfSAllan Stephens {
1071da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1081da46568SYing Xue 
1091da46568SYing Xue 	tipc_bclink_lock(net);
1101da46568SYing Xue 	tipc_nmap_remove(&tn->bclink->bcast_nodes, addr);
1117d967b67SJon Paul Maloy 
1127d967b67SJon Paul Maloy 	/* Last node? => reset backlog queue */
1137d967b67SJon Paul Maloy 	if (!tn->bclink->bcast_nodes.count)
1147d967b67SJon Paul Maloy 		tipc_link_purge_backlog(&tn->bclink->link);
1157d967b67SJon Paul Maloy 
1161da46568SYing Xue 	tipc_bclink_unlock(net);
117cd3decdfSAllan Stephens }
118b97bf3fdSPer Liden 
1191da46568SYing Xue static void bclink_set_last_sent(struct net *net)
1205b1f7bdeSAllan Stephens {
1211da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1221da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
1231da46568SYing Xue 
124a97b9d3fSJon Paul Maloy 	bcl->silent_intv_cnt = mod(bcl->snd_nxt - 1);
1255b1f7bdeSAllan Stephens }
1265b1f7bdeSAllan Stephens 
1271da46568SYing Xue u32 tipc_bclink_get_last_sent(struct net *net)
1285b1f7bdeSAllan Stephens {
1291da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1301da46568SYing Xue 
131a97b9d3fSJon Paul Maloy 	return tn->bcl->silent_intv_cnt;
1325b1f7bdeSAllan Stephens }
1335b1f7bdeSAllan Stephens 
1347a54d4a9SAllan Stephens static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
135b97bf3fdSPer Liden {
1367a54d4a9SAllan Stephens 	node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
1377a54d4a9SAllan Stephens 						seqno : node->bclink.last_sent;
138b97bf3fdSPer Liden }
139b97bf3fdSPer Liden 
1402c53040fSBen Hutchings /**
14101d83eddSAllan Stephens  * tipc_bclink_retransmit_to - get most recent node to request retransmission
14201d83eddSAllan Stephens  *
143d69afc90SYing Xue  * Called with bclink_lock locked
14401d83eddSAllan Stephens  */
1451da46568SYing Xue struct tipc_node *tipc_bclink_retransmit_to(struct net *net)
14601d83eddSAllan Stephens {
1471da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1481da46568SYing Xue 
1491da46568SYing Xue 	return tn->bclink->retransmit_to;
15001d83eddSAllan Stephens }
15101d83eddSAllan Stephens 
15201d83eddSAllan Stephens /**
153b97bf3fdSPer Liden  * bclink_retransmit_pkt - retransmit broadcast packets
154b97bf3fdSPer Liden  * @after: sequence number of last packet to *not* retransmit
155b97bf3fdSPer Liden  * @to: sequence number of last packet to retransmit
156b97bf3fdSPer Liden  *
157d69afc90SYing Xue  * Called with bclink_lock locked
158b97bf3fdSPer Liden  */
1591da46568SYing Xue static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
160b97bf3fdSPer Liden {
16158dc55f2SYing Xue 	struct sk_buff *skb;
1621da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
163b97bf3fdSPer Liden 
16405dcc5aaSJon Paul Maloy 	skb_queue_walk(&bcl->transmq, skb) {
165703068eeSJon Maloy 		if (more(buf_seqno(skb), after)) {
166703068eeSJon Maloy 			tipc_link_retransmit(bcl, skb, mod(to - after));
16758dc55f2SYing Xue 			break;
16858dc55f2SYing Xue 		}
169703068eeSJon Maloy 	}
170b97bf3fdSPer Liden }
171b97bf3fdSPer Liden 
172b97bf3fdSPer Liden /**
173908344cdSJon Maloy  * tipc_bclink_wakeup_users - wake up pending users
174908344cdSJon Maloy  *
175908344cdSJon Maloy  * Called with no locks taken
176908344cdSJon Maloy  */
177f2f9800dSYing Xue void tipc_bclink_wakeup_users(struct net *net)
178908344cdSJon Maloy {
1791da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1804988bb4aSErik Hugne 
181c637c103SJon Paul Maloy 	tipc_sk_rcv(net, &tn->bclink->link.wakeupq);
182908344cdSJon Maloy }
183908344cdSJon Maloy 
184908344cdSJon Maloy /**
1854323add6SPer Liden  * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
186b97bf3fdSPer Liden  * @n_ptr: node that sent acknowledgement info
187b97bf3fdSPer Liden  * @acked: broadcast sequence # that has been acknowledged
188b97bf3fdSPer Liden  *
189d69afc90SYing Xue  * Node is locked, bclink_lock unlocked.
190b97bf3fdSPer Liden  */
1916c00055aSDavid S. Miller void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
192b97bf3fdSPer Liden {
19358dc55f2SYing Xue 	struct sk_buff *skb, *tmp;
194b97bf3fdSPer Liden 	unsigned int released = 0;
1951da46568SYing Xue 	struct net *net = n_ptr->net;
1961da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
197b97bf3fdSPer Liden 
1982cdf3918SJon Paul Maloy 	if (unlikely(!n_ptr->bclink.recv_permitted))
1992cdf3918SJon Paul Maloy 		return;
2002cdf3918SJon Paul Maloy 
2011da46568SYing Xue 	tipc_bclink_lock(net);
2022cdf3918SJon Paul Maloy 
20336559591SAllan Stephens 	/* Bail out if tx queue is empty (no clean up is required) */
20405dcc5aaSJon Paul Maloy 	skb = skb_peek(&tn->bcl->transmq);
20558dc55f2SYing Xue 	if (!skb)
20636559591SAllan Stephens 		goto exit;
20736559591SAllan Stephens 
20836559591SAllan Stephens 	/* Determine which messages need to be acknowledged */
20936559591SAllan Stephens 	if (acked == INVALID_LINK_SEQ) {
21036559591SAllan Stephens 		/*
21136559591SAllan Stephens 		 * Contact with specified node has been lost, so need to
21236559591SAllan Stephens 		 * acknowledge sent messages only (if other nodes still exist)
21336559591SAllan Stephens 		 * or both sent and unsent messages (otherwise)
21436559591SAllan Stephens 		 */
2151da46568SYing Xue 		if (tn->bclink->bcast_nodes.count)
216a97b9d3fSJon Paul Maloy 			acked = tn->bcl->silent_intv_cnt;
21736559591SAllan Stephens 		else
218a97b9d3fSJon Paul Maloy 			acked = tn->bcl->snd_nxt;
21936559591SAllan Stephens 	} else {
22036559591SAllan Stephens 		/*
22136559591SAllan Stephens 		 * Bail out if specified sequence number does not correspond
22236559591SAllan Stephens 		 * to a message that has been sent and not yet acknowledged
22336559591SAllan Stephens 		 */
22458dc55f2SYing Xue 		if (less(acked, buf_seqno(skb)) ||
225a97b9d3fSJon Paul Maloy 		    less(tn->bcl->silent_intv_cnt, acked) ||
22636559591SAllan Stephens 		    less_eq(acked, n_ptr->bclink.acked))
22736559591SAllan Stephens 			goto exit;
22836559591SAllan Stephens 	}
22936559591SAllan Stephens 
23036559591SAllan Stephens 	/* Skip over packets that node has previously acknowledged */
23105dcc5aaSJon Paul Maloy 	skb_queue_walk(&tn->bcl->transmq, skb) {
23258dc55f2SYing Xue 		if (more(buf_seqno(skb), n_ptr->bclink.acked))
23358dc55f2SYing Xue 			break;
23458dc55f2SYing Xue 	}
235b97bf3fdSPer Liden 
236b97bf3fdSPer Liden 	/* Update packets that node is now acknowledging */
23705dcc5aaSJon Paul Maloy 	skb_queue_walk_from_safe(&tn->bcl->transmq, skb, tmp) {
23858dc55f2SYing Xue 		if (more(buf_seqno(skb), acked))
23958dc55f2SYing Xue 			break;
24058dc55f2SYing Xue 		bcbuf_decr_acks(skb);
2411da46568SYing Xue 		bclink_set_last_sent(net);
24258dc55f2SYing Xue 		if (bcbuf_acks(skb) == 0) {
24305dcc5aaSJon Paul Maloy 			__skb_unlink(skb, &tn->bcl->transmq);
24458dc55f2SYing Xue 			kfree_skb(skb);
245b97bf3fdSPer Liden 			released = 1;
246b97bf3fdSPer Liden 		}
247b97bf3fdSPer Liden 	}
248b97bf3fdSPer Liden 	n_ptr->bclink.acked = acked;
249b97bf3fdSPer Liden 
250b97bf3fdSPer Liden 	/* Try resolving broadcast link congestion, if necessary */
25105dcc5aaSJon Paul Maloy 	if (unlikely(skb_peek(&tn->bcl->backlogq))) {
2521da46568SYing Xue 		tipc_link_push_packets(tn->bcl);
2531da46568SYing Xue 		bclink_set_last_sent(net);
2545b1f7bdeSAllan Stephens 	}
255c637c103SJon Paul Maloy 	if (unlikely(released && !skb_queue_empty(&tn->bcl->wakeupq)))
256908344cdSJon Maloy 		n_ptr->action_flags |= TIPC_WAKEUP_BCAST_USERS;
25736559591SAllan Stephens exit:
2581da46568SYing Xue 	tipc_bclink_unlock(net);
259b97bf3fdSPer Liden }
260b97bf3fdSPer Liden 
2612c53040fSBen Hutchings /**
2627a54d4a9SAllan Stephens  * tipc_bclink_update_link_state - update broadcast link state
263b97bf3fdSPer Liden  *
2647216cd94SYing Xue  * RCU and node lock set
265b97bf3fdSPer Liden  */
266c5898636SJon Paul Maloy void tipc_bclink_update_link_state(struct tipc_node *n_ptr,
267c93d3baaSYing Xue 				   u32 last_sent)
268b97bf3fdSPer Liden {
269b97bf3fdSPer Liden 	struct sk_buff *buf;
270c5898636SJon Paul Maloy 	struct net *net = n_ptr->net;
271c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
272b97bf3fdSPer Liden 
2737a54d4a9SAllan Stephens 	/* Ignore "stale" link state info */
2747a54d4a9SAllan Stephens 	if (less_eq(last_sent, n_ptr->bclink.last_in))
275b97bf3fdSPer Liden 		return;
276b97bf3fdSPer Liden 
2777a54d4a9SAllan Stephens 	/* Update link synchronization state; quit if in sync */
2787a54d4a9SAllan Stephens 	bclink_update_last_sent(n_ptr, last_sent);
2797a54d4a9SAllan Stephens 
2807a54d4a9SAllan Stephens 	if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
2817a54d4a9SAllan Stephens 		return;
2827a54d4a9SAllan Stephens 
2837a54d4a9SAllan Stephens 	/* Update out-of-sync state; quit if loss is still unconfirmed */
2847a54d4a9SAllan Stephens 	if ((++n_ptr->bclink.oos_state) == 1) {
2857a54d4a9SAllan Stephens 		if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
2867a54d4a9SAllan Stephens 			return;
2877a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
2887a54d4a9SAllan Stephens 	}
2897a54d4a9SAllan Stephens 
2907a54d4a9SAllan Stephens 	/* Don't NACK if one has been recently sent (or seen) */
2917a54d4a9SAllan Stephens 	if (n_ptr->bclink.oos_state & 0x1)
2927a54d4a9SAllan Stephens 		return;
2937a54d4a9SAllan Stephens 
2947a54d4a9SAllan Stephens 	/* Send NACK */
29531e3c3f6Sstephen hemminger 	buf = tipc_buf_acquire(INT_H_SIZE);
296b97bf3fdSPer Liden 	if (buf) {
2977a54d4a9SAllan Stephens 		struct tipc_msg *msg = buf_msg(buf);
29805dcc5aaSJon Paul Maloy 		struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferdq);
299bc6fecd4SYing Xue 		u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
3007a54d4a9SAllan Stephens 
301c5898636SJon Paul Maloy 		tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG,
30275715217SAllan Stephens 			      INT_H_SIZE, n_ptr->addr);
303bf781ecfSAllan Stephens 		msg_set_non_seq(msg, 1);
304c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
3057a54d4a9SAllan Stephens 		msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
3067a54d4a9SAllan Stephens 		msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
307bc6fecd4SYing Xue 		msg_set_bcgap_to(msg, to);
308b97bf3fdSPer Liden 
3091da46568SYing Xue 		tipc_bclink_lock(net);
3107f9f95d9SYing Xue 		tipc_bearer_send(net, MAX_BEARERS, buf, NULL);
3111da46568SYing Xue 		tn->bcl->stats.sent_nacks++;
3121da46568SYing Xue 		tipc_bclink_unlock(net);
3135f6d9123SAllan Stephens 		kfree_skb(buf);
314b97bf3fdSPer Liden 
3157a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
3167a54d4a9SAllan Stephens 	}
3177a54d4a9SAllan Stephens }
3187a54d4a9SAllan Stephens 
3192c53040fSBen Hutchings /**
3207a54d4a9SAllan Stephens  * bclink_peek_nack - monitor retransmission requests sent by other nodes
321b97bf3fdSPer Liden  *
3227a54d4a9SAllan Stephens  * Delay any upcoming NACK by this node if another node has already
3237a54d4a9SAllan Stephens  * requested the first message this node is going to ask for.
324b97bf3fdSPer Liden  */
325f2f9800dSYing Xue static void bclink_peek_nack(struct net *net, struct tipc_msg *msg)
326b97bf3fdSPer Liden {
327f2f9800dSYing Xue 	struct tipc_node *n_ptr = tipc_node_find(net, msg_destnode(msg));
328b97bf3fdSPer Liden 
3297a54d4a9SAllan Stephens 	if (unlikely(!n_ptr))
330b97bf3fdSPer Liden 		return;
3317a54d4a9SAllan Stephens 
3324323add6SPer Liden 	tipc_node_lock(n_ptr);
333389dd9bcSYing Xue 	if (n_ptr->bclink.recv_permitted &&
3347a54d4a9SAllan Stephens 	    (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
3357a54d4a9SAllan Stephens 	    (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
3367a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state = 2;
3374323add6SPer Liden 	tipc_node_unlock(n_ptr);
3388a0f6ebeSYing Xue 	tipc_node_put(n_ptr);
339b97bf3fdSPer Liden }
340b97bf3fdSPer Liden 
341cb1b7280SJon Paul Maloy /* tipc_bclink_xmit - deliver buffer chain to all nodes in cluster
342078bec82SJon Paul Maloy  *                    and to identified node local sockets
343f2f9800dSYing Xue  * @net: the applicable net namespace
344a6ca1094SYing Xue  * @list: chain of buffers containing message
345078bec82SJon Paul Maloy  * Consumes the buffer chain, except when returning -ELINKCONG
346078bec82SJon Paul Maloy  * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
347078bec82SJon Paul Maloy  */
348f2f9800dSYing Xue int tipc_bclink_xmit(struct net *net, struct sk_buff_head *list)
349078bec82SJon Paul Maloy {
3501da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
3511da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
3521da46568SYing Xue 	struct tipc_bclink *bclink = tn->bclink;
353078bec82SJon Paul Maloy 	int rc = 0;
354078bec82SJon Paul Maloy 	int bc = 0;
355a6ca1094SYing Xue 	struct sk_buff *skb;
356cb1b7280SJon Paul Maloy 	struct sk_buff_head arrvq;
357cb1b7280SJon Paul Maloy 	struct sk_buff_head inputq;
358078bec82SJon Paul Maloy 
359078bec82SJon Paul Maloy 	/* Prepare clone of message for local node */
360a6ca1094SYing Xue 	skb = tipc_msg_reassemble(list);
361*22d85c79SJon Paul Maloy 	if (unlikely(!skb))
362078bec82SJon Paul Maloy 		return -EHOSTUNREACH;
363*22d85c79SJon Paul Maloy 
364cb1b7280SJon Paul Maloy 	/* Broadcast to all nodes */
365078bec82SJon Paul Maloy 	if (likely(bclink)) {
3661da46568SYing Xue 		tipc_bclink_lock(net);
367078bec82SJon Paul Maloy 		if (likely(bclink->bcast_nodes.count)) {
3687f9f95d9SYing Xue 			rc = __tipc_link_xmit(net, bcl, list);
369078bec82SJon Paul Maloy 			if (likely(!rc)) {
37005dcc5aaSJon Paul Maloy 				u32 len = skb_queue_len(&bcl->transmq);
37158dc55f2SYing Xue 
3721da46568SYing Xue 				bclink_set_last_sent(net);
373078bec82SJon Paul Maloy 				bcl->stats.queue_sz_counts++;
37458dc55f2SYing Xue 				bcl->stats.accu_queue_sz += len;
375078bec82SJon Paul Maloy 			}
376078bec82SJon Paul Maloy 			bc = 1;
377078bec82SJon Paul Maloy 		}
3781da46568SYing Xue 		tipc_bclink_unlock(net);
379078bec82SJon Paul Maloy 	}
380078bec82SJon Paul Maloy 
381078bec82SJon Paul Maloy 	if (unlikely(!bc))
382a6ca1094SYing Xue 		__skb_queue_purge(list);
383078bec82SJon Paul Maloy 
384cb1b7280SJon Paul Maloy 	if (unlikely(rc)) {
385a6ca1094SYing Xue 		kfree_skb(skb);
386cb1b7280SJon Paul Maloy 		return rc;
387cb1b7280SJon Paul Maloy 	}
388cb1b7280SJon Paul Maloy 	/* Deliver message clone */
389cb1b7280SJon Paul Maloy 	__skb_queue_head_init(&arrvq);
390cb1b7280SJon Paul Maloy 	skb_queue_head_init(&inputq);
391cb1b7280SJon Paul Maloy 	__skb_queue_tail(&arrvq, skb);
392cb1b7280SJon Paul Maloy 	tipc_sk_mcast_rcv(net, &arrvq, &inputq);
393078bec82SJon Paul Maloy 	return rc;
394078bec82SJon Paul Maloy }
395078bec82SJon Paul Maloy 
3962c53040fSBen Hutchings /**
39763e7f1acSAllan Stephens  * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
39863e7f1acSAllan Stephens  *
399d69afc90SYing Xue  * Called with both sending node's lock and bclink_lock taken.
40063e7f1acSAllan Stephens  */
40163e7f1acSAllan Stephens static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
40263e7f1acSAllan Stephens {
4031da46568SYing Xue 	struct tipc_net *tn = net_generic(node->net, tipc_net_id);
4041da46568SYing Xue 
40563e7f1acSAllan Stephens 	bclink_update_last_sent(node, seqno);
40663e7f1acSAllan Stephens 	node->bclink.last_in = seqno;
40763e7f1acSAllan Stephens 	node->bclink.oos_state = 0;
4081da46568SYing Xue 	tn->bcl->stats.recv_info++;
40963e7f1acSAllan Stephens 
41063e7f1acSAllan Stephens 	/*
41163e7f1acSAllan Stephens 	 * Unicast an ACK periodically, ensuring that
41263e7f1acSAllan Stephens 	 * all nodes in the cluster don't ACK at the same time
41363e7f1acSAllan Stephens 	 */
41434747539SYing Xue 	if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
4159d13ec65SJon Paul Maloy 		tipc_link_proto_xmit(node_active_link(node, node->addr),
416ed193eceSJon Paul Maloy 				     STATE_MSG, 0, 0, 0, 0);
4171da46568SYing Xue 		tn->bcl->stats.sent_acks++;
41863e7f1acSAllan Stephens 	}
41963e7f1acSAllan Stephens }
42063e7f1acSAllan Stephens 
4212c53040fSBen Hutchings /**
422247f0f3cSYing Xue  * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
423b97bf3fdSPer Liden  *
4247216cd94SYing Xue  * RCU is locked, no other locks set
425b97bf3fdSPer Liden  */
426c93d3baaSYing Xue void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
427b97bf3fdSPer Liden {
428c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
4291da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
430b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
4315d3c488dSAllan Stephens 	struct tipc_node *node;
432b97bf3fdSPer Liden 	u32 next_in;
433b97bf3fdSPer Liden 	u32 seqno;
4340abd8ff2SJon Paul Maloy 	int deferred = 0;
435c637c103SJon Paul Maloy 	int pos = 0;
436c637c103SJon Paul Maloy 	struct sk_buff *iskb;
437cb1b7280SJon Paul Maloy 	struct sk_buff_head *arrvq, *inputq;
438b97bf3fdSPer Liden 
4395d3c488dSAllan Stephens 	/* Screen out unwanted broadcast messages */
440c93d3baaSYing Xue 	if (msg_mc_netid(msg) != tn->net_id)
4415d3c488dSAllan Stephens 		goto exit;
4425d3c488dSAllan Stephens 
443f2f9800dSYing Xue 	node = tipc_node_find(net, msg_prevnode(msg));
4445d3c488dSAllan Stephens 	if (unlikely(!node))
4455d3c488dSAllan Stephens 		goto exit;
4465d3c488dSAllan Stephens 
4475d3c488dSAllan Stephens 	tipc_node_lock(node);
448389dd9bcSYing Xue 	if (unlikely(!node->bclink.recv_permitted))
4495d3c488dSAllan Stephens 		goto unlock;
450b97bf3fdSPer Liden 
4518a275a6aSAllan Stephens 	/* Handle broadcast protocol message */
452b97bf3fdSPer Liden 	if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
4539f6bdcd4SAllan Stephens 		if (msg_type(msg) != STATE_MSG)
4549f6bdcd4SAllan Stephens 			goto unlock;
45534747539SYing Xue 		if (msg_destnode(msg) == tn->own_addr) {
4564323add6SPer Liden 			tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
4571da46568SYing Xue 			tipc_bclink_lock(net);
458b97bf3fdSPer Liden 			bcl->stats.recv_nacks++;
4591da46568SYing Xue 			tn->bclink->retransmit_to = node;
4601da46568SYing Xue 			bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
461b97bf3fdSPer Liden 					      msg_bcgap_to(msg));
4621da46568SYing Xue 			tipc_bclink_unlock(net);
463b952b2beSYing Xue 			tipc_node_unlock(node);
464b97bf3fdSPer Liden 		} else {
4655d3c488dSAllan Stephens 			tipc_node_unlock(node);
466f2f9800dSYing Xue 			bclink_peek_nack(net, msg);
467b97bf3fdSPer Liden 		}
4688a0f6ebeSYing Xue 		tipc_node_put(node);
4695d3c488dSAllan Stephens 		goto exit;
470b97bf3fdSPer Liden 	}
471b97bf3fdSPer Liden 
4725d3c488dSAllan Stephens 	/* Handle in-sequence broadcast message */
473b97bf3fdSPer Liden 	seqno = msg_seqno(msg);
4748a275a6aSAllan Stephens 	next_in = mod(node->bclink.last_in + 1);
475cb1b7280SJon Paul Maloy 	arrvq = &tn->bclink->arrvq;
476cb1b7280SJon Paul Maloy 	inputq = &tn->bclink->inputq;
477b97bf3fdSPer Liden 
478b97bf3fdSPer Liden 	if (likely(seqno == next_in)) {
4798a275a6aSAllan Stephens receive:
4807a54d4a9SAllan Stephens 		/* Deliver message to destination */
481b97bf3fdSPer Liden 		if (likely(msg_isdata(msg))) {
4821da46568SYing Xue 			tipc_bclink_lock(net);
48363e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
484cb1b7280SJon Paul Maloy 			spin_lock_bh(&inputq->lock);
485cb1b7280SJon Paul Maloy 			__skb_queue_tail(arrvq, buf);
486cb1b7280SJon Paul Maloy 			spin_unlock_bh(&inputq->lock);
487cb1b7280SJon Paul Maloy 			node->action_flags |= TIPC_BCAST_MSG_EVT;
4881da46568SYing Xue 			tipc_bclink_unlock(net);
4894323add6SPer Liden 			tipc_node_unlock(node);
490b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_BUNDLER) {
4911da46568SYing Xue 			tipc_bclink_lock(net);
49263e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
493b97bf3fdSPer Liden 			bcl->stats.recv_bundles++;
494b97bf3fdSPer Liden 			bcl->stats.recv_bundled += msg_msgcnt(msg);
495cb1b7280SJon Paul Maloy 			pos = 0;
496cb1b7280SJon Paul Maloy 			while (tipc_msg_extract(buf, &iskb, &pos)) {
497cb1b7280SJon Paul Maloy 				spin_lock_bh(&inputq->lock);
498cb1b7280SJon Paul Maloy 				__skb_queue_tail(arrvq, iskb);
499cb1b7280SJon Paul Maloy 				spin_unlock_bh(&inputq->lock);
500cb1b7280SJon Paul Maloy 			}
501cb1b7280SJon Paul Maloy 			node->action_flags |= TIPC_BCAST_MSG_EVT;
5021da46568SYing Xue 			tipc_bclink_unlock(net);
5034323add6SPer Liden 			tipc_node_unlock(node);
504b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_FRAGMENTER) {
5051da46568SYing Xue 			tipc_bclink_lock(net);
50663e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
507bc14b8d6SYing Xue 			tipc_buf_append(&node->bclink.reasm_buf, &buf);
508bc14b8d6SYing Xue 			if (unlikely(!buf && !node->bclink.reasm_buf)) {
509bc14b8d6SYing Xue 				tipc_bclink_unlock(net);
510bc14b8d6SYing Xue 				goto unlock;
511bc14b8d6SYing Xue 			}
512b97bf3fdSPer Liden 			bcl->stats.recv_fragments++;
51337e22164SJon Paul Maloy 			if (buf) {
514b97bf3fdSPer Liden 				bcl->stats.recv_fragmented++;
51540ba3cdfSErik Hugne 				msg = buf_msg(buf);
5161da46568SYing Xue 				tipc_bclink_unlock(net);
517528f6f4bSErik Hugne 				goto receive;
518528f6f4bSErik Hugne 			}
5191da46568SYing Xue 			tipc_bclink_unlock(net);
5204323add6SPer Liden 			tipc_node_unlock(node);
521b97bf3fdSPer Liden 		} else {
5221da46568SYing Xue 			tipc_bclink_lock(net);
52363e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
5241da46568SYing Xue 			tipc_bclink_unlock(net);
5254323add6SPer Liden 			tipc_node_unlock(node);
5265f6d9123SAllan Stephens 			kfree_skb(buf);
527b97bf3fdSPer Liden 		}
5285d3c488dSAllan Stephens 		buf = NULL;
5298a275a6aSAllan Stephens 
5308a275a6aSAllan Stephens 		/* Determine new synchronization state */
5314323add6SPer Liden 		tipc_node_lock(node);
5328a275a6aSAllan Stephens 		if (unlikely(!tipc_node_is_up(node)))
5338a275a6aSAllan Stephens 			goto unlock;
5348a275a6aSAllan Stephens 
5357a54d4a9SAllan Stephens 		if (node->bclink.last_in == node->bclink.last_sent)
5368a275a6aSAllan Stephens 			goto unlock;
5378a275a6aSAllan Stephens 
53805dcc5aaSJon Paul Maloy 		if (skb_queue_empty(&node->bclink.deferdq)) {
5397a54d4a9SAllan Stephens 			node->bclink.oos_state = 1;
5407a54d4a9SAllan Stephens 			goto unlock;
5417a54d4a9SAllan Stephens 		}
5427a54d4a9SAllan Stephens 
54305dcc5aaSJon Paul Maloy 		msg = buf_msg(skb_peek(&node->bclink.deferdq));
5448a275a6aSAllan Stephens 		seqno = msg_seqno(msg);
5458a275a6aSAllan Stephens 		next_in = mod(next_in + 1);
5468a275a6aSAllan Stephens 		if (seqno != next_in)
5478a275a6aSAllan Stephens 			goto unlock;
5488a275a6aSAllan Stephens 
5498a275a6aSAllan Stephens 		/* Take in-sequence message from deferred queue & deliver it */
55005dcc5aaSJon Paul Maloy 		buf = __skb_dequeue(&node->bclink.deferdq);
551b97bf3fdSPer Liden 		goto receive;
552b97bf3fdSPer Liden 	}
5538a275a6aSAllan Stephens 
5548a275a6aSAllan Stephens 	/* Handle out-of-sequence broadcast message */
5558a275a6aSAllan Stephens 	if (less(next_in, seqno)) {
55605dcc5aaSJon Paul Maloy 		deferred = tipc_link_defer_pkt(&node->bclink.deferdq,
5578a275a6aSAllan Stephens 					       buf);
5587a54d4a9SAllan Stephens 		bclink_update_last_sent(node, seqno);
5595d3c488dSAllan Stephens 		buf = NULL;
5600abd8ff2SJon Paul Maloy 	}
5618a275a6aSAllan Stephens 
5621da46568SYing Xue 	tipc_bclink_lock(net);
563b98158e3SAllan Stephens 
5648a275a6aSAllan Stephens 	if (deferred)
5658a275a6aSAllan Stephens 		bcl->stats.deferred_recv++;
5660232c5a5SAllan Stephens 	else
5670232c5a5SAllan Stephens 		bcl->stats.duplicates++;
5688a275a6aSAllan Stephens 
5691da46568SYing Xue 	tipc_bclink_unlock(net);
570b98158e3SAllan Stephens 
5715d3c488dSAllan Stephens unlock:
5724323add6SPer Liden 	tipc_node_unlock(node);
5738a0f6ebeSYing Xue 	tipc_node_put(node);
5745d3c488dSAllan Stephens exit:
5755f6d9123SAllan Stephens 	kfree_skb(buf);
576b97bf3fdSPer Liden }
577b97bf3fdSPer Liden 
5786c00055aSDavid S. Miller u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
579b97bf3fdSPer Liden {
580389dd9bcSYing Xue 	return (n_ptr->bclink.recv_permitted &&
5811da46568SYing Xue 		(tipc_bclink_get_last_sent(n_ptr->net) != n_ptr->bclink.acked));
582b97bf3fdSPer Liden }
583b97bf3fdSPer Liden 
584b97bf3fdSPer Liden 
585b97bf3fdSPer Liden /**
5864323add6SPer Liden  * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
587b97bf3fdSPer Liden  *
5882ff9f924SAllan Stephens  * Send packet over as many bearers as necessary to reach all nodes
5892ff9f924SAllan Stephens  * that have joined the broadcast link.
590b97bf3fdSPer Liden  *
5912ff9f924SAllan Stephens  * Returns 0 (packet sent successfully) under all circumstances,
5922ff9f924SAllan Stephens  * since the broadcast link's pseudo-bearer never blocks
593b97bf3fdSPer Liden  */
5941da46568SYing Xue static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
5951da46568SYing Xue 			      struct tipc_bearer *unused1,
596b97bf3fdSPer Liden 			      struct tipc_media_addr *unused2)
597b97bf3fdSPer Liden {
598b97bf3fdSPer Liden 	int bp_index;
5996f92ee54SJon Paul Maloy 	struct tipc_msg *msg = buf_msg(buf);
600c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6011da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6021da46568SYing Xue 	struct tipc_bclink *bclink = tn->bclink;
603b97bf3fdSPer Liden 
604e6160710SGerlando Falauto 	/* Prepare broadcast link message for reliable transmission,
6052ff9f924SAllan Stephens 	 * if first time trying to send it;
6062ff9f924SAllan Stephens 	 * preparation is skipped for broadcast link protocol messages
6072ff9f924SAllan Stephens 	 * since they are sent in an unreliable manner and don't need it
6082ff9f924SAllan Stephens 	 */
609b97bf3fdSPer Liden 	if (likely(!msg_non_seq(buf_msg(buf)))) {
610cd3decdfSAllan Stephens 		bcbuf_set_acks(buf, bclink->bcast_nodes.count);
61140aecb1bSAllan Stephens 		msg_set_non_seq(msg, 1);
612c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
6131da46568SYing Xue 		tn->bcl->stats.sent_info++;
614cd3decdfSAllan Stephens 		if (WARN_ON(!bclink->bcast_nodes.count)) {
6155e726900SAllan Stephens 			dump_stack();
6165e726900SAllan Stephens 			return 0;
6175e726900SAllan Stephens 		}
618b97bf3fdSPer Liden 	}
619b97bf3fdSPer Liden 
620b97bf3fdSPer Liden 	/* Send buffer over bearers until all targets reached */
621cd3decdfSAllan Stephens 	bcbearer->remains = bclink->bcast_nodes;
622b97bf3fdSPer Liden 
623b97bf3fdSPer Liden 	for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
6242d627b92SAllan Stephens 		struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
6252d627b92SAllan Stephens 		struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
6266f92ee54SJon Paul Maloy 		struct tipc_bearer *bp[2] = {p, s};
6276f92ee54SJon Paul Maloy 		struct tipc_bearer *b = bp[msg_link_selector(msg)];
628488fc9afSGerlando Falauto 		struct sk_buff *tbuf;
629b97bf3fdSPer Liden 
630b97bf3fdSPer Liden 		if (!p)
631e6160710SGerlando Falauto 			break; /* No more bearers to try */
6326f92ee54SJon Paul Maloy 		if (!b)
6336f92ee54SJon Paul Maloy 			b = p;
63477861d9cSGerlando Falauto 		tipc_nmap_diff(&bcbearer->remains, &b->nodes,
635e6160710SGerlando Falauto 			       &bcbearer->remains_new);
63665f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == bcbearer->remains.count)
637e6160710SGerlando Falauto 			continue; /* Nothing added by bearer pair */
638b97bf3fdSPer Liden 
639488fc9afSGerlando Falauto 		if (bp_index == 0) {
640488fc9afSGerlando Falauto 			/* Use original buffer for first bearer */
6417f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, buf, &b->bcast_addr);
642488fc9afSGerlando Falauto 		} else {
643488fc9afSGerlando Falauto 			/* Avoid concurrent buffer access */
644bad93e9dSOctavian Purdila 			tbuf = pskb_copy_for_clone(buf, GFP_ATOMIC);
645488fc9afSGerlando Falauto 			if (!tbuf)
646488fc9afSGerlando Falauto 				break;
6477f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, tbuf,
6487f9f95d9SYing Xue 					 &b->bcast_addr);
649488fc9afSGerlando Falauto 			kfree_skb(tbuf); /* Bearer keeps a clone */
650488fc9afSGerlando Falauto 		}
65165f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == 0)
652e6160710SGerlando Falauto 			break; /* All targets reached */
653b97bf3fdSPer Liden 
65465f51ef0SAllan Stephens 		bcbearer->remains = bcbearer->remains_new;
655b97bf3fdSPer Liden 	}
656b97bf3fdSPer Liden 
6572ff9f924SAllan Stephens 	return 0;
658b97bf3fdSPer Liden }
659b97bf3fdSPer Liden 
660b97bf3fdSPer Liden /**
6614323add6SPer Liden  * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
662b97bf3fdSPer Liden  */
6637f9f95d9SYing Xue void tipc_bcbearer_sort(struct net *net, struct tipc_node_map *nm_ptr,
6647f9f95d9SYing Xue 			u32 node, bool action)
665b97bf3fdSPer Liden {
6667f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6671da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6687f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
6697f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_curr;
670f8322dfcSYing Xue 	struct tipc_bearer *b;
671b97bf3fdSPer Liden 	int b_index;
672b97bf3fdSPer Liden 	int pri;
673b97bf3fdSPer Liden 
6741da46568SYing Xue 	tipc_bclink_lock(net);
675b97bf3fdSPer Liden 
67628dd9418SYing Xue 	if (action)
67728dd9418SYing Xue 		tipc_nmap_add(nm_ptr, node);
67828dd9418SYing Xue 	else
67928dd9418SYing Xue 		tipc_nmap_remove(nm_ptr, node);
68028dd9418SYing Xue 
681b97bf3fdSPer Liden 	/* Group bearers by priority (can assume max of two per priority) */
682b97bf3fdSPer Liden 	memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
683b97bf3fdSPer Liden 
684f8322dfcSYing Xue 	rcu_read_lock();
685b97bf3fdSPer Liden 	for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
6867f9f95d9SYing Xue 		b = rcu_dereference_rtnl(tn->bearer_list[b_index]);
687f47de12bSYing Xue 		if (!b || !b->nodes.count)
688b97bf3fdSPer Liden 			continue;
689b97bf3fdSPer Liden 
690b97bf3fdSPer Liden 		if (!bp_temp[b->priority].primary)
691b97bf3fdSPer Liden 			bp_temp[b->priority].primary = b;
692b97bf3fdSPer Liden 		else
693b97bf3fdSPer Liden 			bp_temp[b->priority].secondary = b;
694b97bf3fdSPer Liden 	}
695f8322dfcSYing Xue 	rcu_read_unlock();
696b97bf3fdSPer Liden 
697b97bf3fdSPer Liden 	/* Create array of bearer pairs for broadcasting */
698b97bf3fdSPer Liden 	bp_curr = bcbearer->bpairs;
699b97bf3fdSPer Liden 	memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
700b97bf3fdSPer Liden 
70116cb4b33SPer Liden 	for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
702b97bf3fdSPer Liden 
703b97bf3fdSPer Liden 		if (!bp_temp[pri].primary)
704b97bf3fdSPer Liden 			continue;
705b97bf3fdSPer Liden 
706b97bf3fdSPer Liden 		bp_curr->primary = bp_temp[pri].primary;
707b97bf3fdSPer Liden 
708b97bf3fdSPer Liden 		if (bp_temp[pri].secondary) {
7094323add6SPer Liden 			if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
710b97bf3fdSPer Liden 					    &bp_temp[pri].secondary->nodes)) {
711b97bf3fdSPer Liden 				bp_curr->secondary = bp_temp[pri].secondary;
712b97bf3fdSPer Liden 			} else {
713b97bf3fdSPer Liden 				bp_curr++;
714b97bf3fdSPer Liden 				bp_curr->primary = bp_temp[pri].secondary;
715b97bf3fdSPer Liden 			}
716b97bf3fdSPer Liden 		}
717b97bf3fdSPer Liden 
718b97bf3fdSPer Liden 		bp_curr++;
719b97bf3fdSPer Liden 	}
720b97bf3fdSPer Liden 
7211da46568SYing Xue 	tipc_bclink_unlock(net);
722b97bf3fdSPer Liden }
723b97bf3fdSPer Liden 
724d8182804SRichard Alpe static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
725d8182804SRichard Alpe 				      struct tipc_stats *stats)
7267be57fc6SRichard Alpe {
7277be57fc6SRichard Alpe 	int i;
7287be57fc6SRichard Alpe 	struct nlattr *nest;
7297be57fc6SRichard Alpe 
7307be57fc6SRichard Alpe 	struct nla_map {
7317be57fc6SRichard Alpe 		__u32 key;
7327be57fc6SRichard Alpe 		__u32 val;
7337be57fc6SRichard Alpe 	};
7347be57fc6SRichard Alpe 
7357be57fc6SRichard Alpe 	struct nla_map map[] = {
7367be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_INFO, stats->recv_info},
7377be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
7387be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
7397be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
7407be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
7417be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_INFO, stats->sent_info},
7427be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
7437be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
7447be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
7457be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
7467be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
7477be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
7487be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
7497be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
7507be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
7517be57fc6SRichard Alpe 		{TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
7527be57fc6SRichard Alpe 		{TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
7537be57fc6SRichard Alpe 		{TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
7547be57fc6SRichard Alpe 		{TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
7557be57fc6SRichard Alpe 			(stats->accu_queue_sz / stats->queue_sz_counts) : 0}
7567be57fc6SRichard Alpe 	};
7577be57fc6SRichard Alpe 
7587be57fc6SRichard Alpe 	nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
7597be57fc6SRichard Alpe 	if (!nest)
7607be57fc6SRichard Alpe 		return -EMSGSIZE;
7617be57fc6SRichard Alpe 
7627be57fc6SRichard Alpe 	for (i = 0; i <  ARRAY_SIZE(map); i++)
7637be57fc6SRichard Alpe 		if (nla_put_u32(skb, map[i].key, map[i].val))
7647be57fc6SRichard Alpe 			goto msg_full;
7657be57fc6SRichard Alpe 
7667be57fc6SRichard Alpe 	nla_nest_end(skb, nest);
7677be57fc6SRichard Alpe 
7687be57fc6SRichard Alpe 	return 0;
7697be57fc6SRichard Alpe msg_full:
7707be57fc6SRichard Alpe 	nla_nest_cancel(skb, nest);
7717be57fc6SRichard Alpe 
7727be57fc6SRichard Alpe 	return -EMSGSIZE;
7737be57fc6SRichard Alpe }
7747be57fc6SRichard Alpe 
7751da46568SYing Xue int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
7767be57fc6SRichard Alpe {
7777be57fc6SRichard Alpe 	int err;
7787be57fc6SRichard Alpe 	void *hdr;
7797be57fc6SRichard Alpe 	struct nlattr *attrs;
7807be57fc6SRichard Alpe 	struct nlattr *prop;
7811da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
7821da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
7837be57fc6SRichard Alpe 
7847be57fc6SRichard Alpe 	if (!bcl)
7857be57fc6SRichard Alpe 		return 0;
7867be57fc6SRichard Alpe 
7871da46568SYing Xue 	tipc_bclink_lock(net);
7887be57fc6SRichard Alpe 
789bfb3e5ddSRichard Alpe 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
7907be57fc6SRichard Alpe 			  NLM_F_MULTI, TIPC_NL_LINK_GET);
7917be57fc6SRichard Alpe 	if (!hdr)
7927be57fc6SRichard Alpe 		return -EMSGSIZE;
7937be57fc6SRichard Alpe 
7947be57fc6SRichard Alpe 	attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
7957be57fc6SRichard Alpe 	if (!attrs)
7967be57fc6SRichard Alpe 		goto msg_full;
7977be57fc6SRichard Alpe 
7987be57fc6SRichard Alpe 	/* The broadcast link is always up */
7997be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
8007be57fc6SRichard Alpe 		goto attr_msg_full;
8017be57fc6SRichard Alpe 
8027be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
8037be57fc6SRichard Alpe 		goto attr_msg_full;
8047be57fc6SRichard Alpe 	if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
8057be57fc6SRichard Alpe 		goto attr_msg_full;
806a97b9d3fSJon Paul Maloy 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->rcv_nxt))
8077be57fc6SRichard Alpe 		goto attr_msg_full;
808a97b9d3fSJon Paul Maloy 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->snd_nxt))
8097be57fc6SRichard Alpe 		goto attr_msg_full;
8107be57fc6SRichard Alpe 
8117be57fc6SRichard Alpe 	prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
8127be57fc6SRichard Alpe 	if (!prop)
8137be57fc6SRichard Alpe 		goto attr_msg_full;
8141f66d161SJon Paul Maloy 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
8157be57fc6SRichard Alpe 		goto prop_msg_full;
8167be57fc6SRichard Alpe 	nla_nest_end(msg->skb, prop);
8177be57fc6SRichard Alpe 
8187be57fc6SRichard Alpe 	err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
8197be57fc6SRichard Alpe 	if (err)
8207be57fc6SRichard Alpe 		goto attr_msg_full;
8217be57fc6SRichard Alpe 
8221da46568SYing Xue 	tipc_bclink_unlock(net);
8237be57fc6SRichard Alpe 	nla_nest_end(msg->skb, attrs);
8247be57fc6SRichard Alpe 	genlmsg_end(msg->skb, hdr);
8257be57fc6SRichard Alpe 
8267be57fc6SRichard Alpe 	return 0;
8277be57fc6SRichard Alpe 
8287be57fc6SRichard Alpe prop_msg_full:
8297be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, prop);
8307be57fc6SRichard Alpe attr_msg_full:
8317be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, attrs);
8327be57fc6SRichard Alpe msg_full:
8331da46568SYing Xue 	tipc_bclink_unlock(net);
8347be57fc6SRichard Alpe 	genlmsg_cancel(msg->skb, hdr);
8357be57fc6SRichard Alpe 
8367be57fc6SRichard Alpe 	return -EMSGSIZE;
8377be57fc6SRichard Alpe }
838b97bf3fdSPer Liden 
8391da46568SYing Xue int tipc_bclink_reset_stats(struct net *net)
840b97bf3fdSPer Liden {
8411da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8421da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8431da46568SYing Xue 
844b97bf3fdSPer Liden 	if (!bcl)
845b97bf3fdSPer Liden 		return -ENOPROTOOPT;
846b97bf3fdSPer Liden 
8471da46568SYing Xue 	tipc_bclink_lock(net);
848b97bf3fdSPer Liden 	memset(&bcl->stats, 0, sizeof(bcl->stats));
8491da46568SYing Xue 	tipc_bclink_unlock(net);
8500e35fd5eSAllan Stephens 	return 0;
851b97bf3fdSPer Liden }
852b97bf3fdSPer Liden 
8531da46568SYing Xue int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
854b97bf3fdSPer Liden {
8551da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8561da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8571da46568SYing Xue 
858b97bf3fdSPer Liden 	if (!bcl)
859b97bf3fdSPer Liden 		return -ENOPROTOOPT;
860b97bf3fdSPer Liden 	if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
861b97bf3fdSPer Liden 		return -EINVAL;
862b97bf3fdSPer Liden 
8631da46568SYing Xue 	tipc_bclink_lock(net);
8644323add6SPer Liden 	tipc_link_set_queue_limits(bcl, limit);
8651da46568SYing Xue 	tipc_bclink_unlock(net);
8660e35fd5eSAllan Stephens 	return 0;
867b97bf3fdSPer Liden }
868b97bf3fdSPer Liden 
869670f4f88SRichard Alpe int tipc_nl_bc_link_set(struct net *net, struct nlattr *attrs[])
870670f4f88SRichard Alpe {
871670f4f88SRichard Alpe 	int err;
872670f4f88SRichard Alpe 	u32 win;
873670f4f88SRichard Alpe 	struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
874670f4f88SRichard Alpe 
875670f4f88SRichard Alpe 	if (!attrs[TIPC_NLA_LINK_PROP])
876670f4f88SRichard Alpe 		return -EINVAL;
877670f4f88SRichard Alpe 
878670f4f88SRichard Alpe 	err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
879670f4f88SRichard Alpe 	if (err)
880670f4f88SRichard Alpe 		return err;
881670f4f88SRichard Alpe 
882670f4f88SRichard Alpe 	if (!props[TIPC_NLA_PROP_WIN])
883670f4f88SRichard Alpe 		return -EOPNOTSUPP;
884670f4f88SRichard Alpe 
885670f4f88SRichard Alpe 	win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
886670f4f88SRichard Alpe 
887670f4f88SRichard Alpe 	return tipc_bclink_set_queue_limits(net, win);
888670f4f88SRichard Alpe }
889670f4f88SRichard Alpe 
8907f9f95d9SYing Xue int tipc_bclink_init(struct net *net)
891b97bf3fdSPer Liden {
8927f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8931da46568SYing Xue 	struct tipc_bcbearer *bcbearer;
8941da46568SYing Xue 	struct tipc_bclink *bclink;
8951da46568SYing Xue 	struct tipc_link *bcl;
8967f9f95d9SYing Xue 
897eb8b00f5SYing Xue 	bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
898eb8b00f5SYing Xue 	if (!bcbearer)
899eb8b00f5SYing Xue 		return -ENOMEM;
900eb8b00f5SYing Xue 
901eb8b00f5SYing Xue 	bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
902eb8b00f5SYing Xue 	if (!bclink) {
903eb8b00f5SYing Xue 		kfree(bcbearer);
904eb8b00f5SYing Xue 		return -ENOMEM;
905eb8b00f5SYing Xue 	}
906eb8b00f5SYing Xue 
907eb8b00f5SYing Xue 	bcl = &bclink->link;
908b97bf3fdSPer Liden 	bcbearer->bearer.media = &bcbearer->media;
9094323add6SPer Liden 	bcbearer->media.send_msg = tipc_bcbearer_send;
9102e2d9be8SAllan Stephens 	sprintf(bcbearer->media.name, "tipc-broadcast");
911b97bf3fdSPer Liden 
912d69afc90SYing Xue 	spin_lock_init(&bclink->lock);
91305dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->transmq);
91405dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->backlogq);
91505dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->deferdq);
916c637c103SJon Paul Maloy 	skb_queue_head_init(&bcl->wakeupq);
917a97b9d3fSJon Paul Maloy 	bcl->snd_nxt = 1;
91834af946aSIngo Molnar 	spin_lock_init(&bclink->node.lock);
919cb1b7280SJon Paul Maloy 	__skb_queue_head_init(&bclink->arrvq);
920cb1b7280SJon Paul Maloy 	skb_queue_head_init(&bclink->inputq);
921b97bf3fdSPer Liden 	bcl->owner = &bclink->node;
9221da46568SYing Xue 	bcl->owner->net = net;
923ed193eceSJon Paul Maloy 	bcl->mtu = MAX_PKT_DEFAULT_MCAST;
9244323add6SPer Liden 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
9257a2f7d18SYing Xue 	bcl->bearer_id = MAX_BEARERS;
9267f9f95d9SYing Xue 	rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
927b97bf3fdSPer Liden 	bcl->state = WORKING_WORKING;
928c5898636SJon Paul Maloy 	bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
929c5898636SJon Paul Maloy 	msg_set_prevnode(bcl->pmsg, tn->own_addr);
9304b704d59SStephen Hemminger 	strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
9311da46568SYing Xue 	tn->bcbearer = bcbearer;
9321da46568SYing Xue 	tn->bclink = bclink;
9331da46568SYing Xue 	tn->bcl = bcl;
934eb8b00f5SYing Xue 	return 0;
935b97bf3fdSPer Liden }
936b97bf3fdSPer Liden 
9377f9f95d9SYing Xue void tipc_bclink_stop(struct net *net)
938b97bf3fdSPer Liden {
9397f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9407f9f95d9SYing Xue 
9411da46568SYing Xue 	tipc_bclink_lock(net);
9421da46568SYing Xue 	tipc_link_purge_queues(tn->bcl);
9431da46568SYing Xue 	tipc_bclink_unlock(net);
944c47e9b91SAllan Stephens 
9457f9f95d9SYing Xue 	RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
946eb8b00f5SYing Xue 	synchronize_net();
9471da46568SYing Xue 	kfree(tn->bcbearer);
9481da46568SYing Xue 	kfree(tn->bclink);
949b97bf3fdSPer Liden }
950b97bf3fdSPer Liden 
9513e22e62bSAllan Stephens /**
9523e22e62bSAllan Stephens  * tipc_nmap_add - add a node to a node map
9533e22e62bSAllan Stephens  */
95428dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
9553e22e62bSAllan Stephens {
9563e22e62bSAllan Stephens 	int n = tipc_node(node);
9573e22e62bSAllan Stephens 	int w = n / WSIZE;
9583e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
9593e22e62bSAllan Stephens 
9603e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) == 0) {
9613e22e62bSAllan Stephens 		nm_ptr->count++;
9623e22e62bSAllan Stephens 		nm_ptr->map[w] |= mask;
9633e22e62bSAllan Stephens 	}
9643e22e62bSAllan Stephens }
9653e22e62bSAllan Stephens 
9663e22e62bSAllan Stephens /**
9673e22e62bSAllan Stephens  * tipc_nmap_remove - remove a node from a node map
9683e22e62bSAllan Stephens  */
96928dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
9703e22e62bSAllan Stephens {
9713e22e62bSAllan Stephens 	int n = tipc_node(node);
9723e22e62bSAllan Stephens 	int w = n / WSIZE;
9733e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
9743e22e62bSAllan Stephens 
9753e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) != 0) {
9763e22e62bSAllan Stephens 		nm_ptr->map[w] &= ~mask;
9773e22e62bSAllan Stephens 		nm_ptr->count--;
9783e22e62bSAllan Stephens 	}
9793e22e62bSAllan Stephens }
9803e22e62bSAllan Stephens 
9813e22e62bSAllan Stephens /**
9823e22e62bSAllan Stephens  * tipc_nmap_diff - find differences between node maps
9833e22e62bSAllan Stephens  * @nm_a: input node map A
9843e22e62bSAllan Stephens  * @nm_b: input node map B
9853e22e62bSAllan Stephens  * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
9863e22e62bSAllan Stephens  */
98731e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
98831e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
9893e22e62bSAllan Stephens 			   struct tipc_node_map *nm_diff)
9903e22e62bSAllan Stephens {
9913e22e62bSAllan Stephens 	int stop = ARRAY_SIZE(nm_a->map);
9923e22e62bSAllan Stephens 	int w;
9933e22e62bSAllan Stephens 	int b;
9943e22e62bSAllan Stephens 	u32 map;
9953e22e62bSAllan Stephens 
9963e22e62bSAllan Stephens 	memset(nm_diff, 0, sizeof(*nm_diff));
9973e22e62bSAllan Stephens 	for (w = 0; w < stop; w++) {
9983e22e62bSAllan Stephens 		map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
9993e22e62bSAllan Stephens 		nm_diff->map[w] = map;
10003e22e62bSAllan Stephens 		if (map != 0) {
10013e22e62bSAllan Stephens 			for (b = 0 ; b < WSIZE; b++) {
10023e22e62bSAllan Stephens 				if (map & (1 << b))
10033e22e62bSAllan Stephens 					nm_diff->count++;
10043e22e62bSAllan Stephens 			}
10053e22e62bSAllan Stephens 		}
10063e22e62bSAllan Stephens 	}
10073e22e62bSAllan Stephens }
1008