xref: /openbmc/linux/net/tipc/bcast.c (revision 99941754)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/bcast.c: TIPC broadcast code
3b97bf3fdSPer Liden  *
4593a5f22SPer Liden  * Copyright (c) 2004-2006, 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 
38b97bf3fdSPer Liden #include "core.h"
39b97bf3fdSPer Liden #include "link.h"
40b97bf3fdSPer Liden #include "port.h"
41b97bf3fdSPer Liden #include "bcast.h"
429f6bdcd4SAllan Stephens #include "name_distr.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) */
46987b58beSYing Xue #define	BCBEARER		MAX_BEARERS
47b97bf3fdSPer Liden 
48b97bf3fdSPer Liden /**
497f9ab6acSPaul Gortmaker  * struct tipc_bcbearer_pair - a pair of bearers used by broadcast link
50b97bf3fdSPer Liden  * @primary: pointer to primary bearer
51b97bf3fdSPer Liden  * @secondary: pointer to secondary bearer
52b97bf3fdSPer Liden  *
53b97bf3fdSPer Liden  * Bearers must have same priority and same set of reachable destinations
54b97bf3fdSPer Liden  * to be paired.
55b97bf3fdSPer Liden  */
56b97bf3fdSPer Liden 
577f9ab6acSPaul Gortmaker struct tipc_bcbearer_pair {
582d627b92SAllan Stephens 	struct tipc_bearer *primary;
592d627b92SAllan Stephens 	struct tipc_bearer *secondary;
60b97bf3fdSPer Liden };
61b97bf3fdSPer Liden 
62b97bf3fdSPer Liden /**
637f9ab6acSPaul Gortmaker  * struct tipc_bcbearer - bearer used by broadcast link
64b97bf3fdSPer Liden  * @bearer: (non-standard) broadcast bearer structure
65b97bf3fdSPer Liden  * @media: (non-standard) broadcast media structure
66b97bf3fdSPer Liden  * @bpairs: array of bearer pairs
6765f51ef0SAllan Stephens  * @bpairs_temp: temporary array of bearer pairs used by tipc_bcbearer_sort()
6865f51ef0SAllan Stephens  * @remains: temporary node map used by tipc_bcbearer_send()
6965f51ef0SAllan Stephens  * @remains_new: temporary node map used tipc_bcbearer_send()
7065f51ef0SAllan Stephens  *
7165f51ef0SAllan Stephens  * Note: The fields labelled "temporary" are incorporated into the bearer
7265f51ef0SAllan Stephens  * to avoid consuming potentially limited stack space through the use of
7365f51ef0SAllan Stephens  * large local variables within multicast routines.  Concurrent access is
74d69afc90SYing Xue  * prevented through use of the spinlock "bclink_lock".
75b97bf3fdSPer Liden  */
767f9ab6acSPaul Gortmaker struct tipc_bcbearer {
772d627b92SAllan Stephens 	struct tipc_bearer bearer;
78358a0d1cSPaul Gortmaker 	struct tipc_media media;
797f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair bpairs[MAX_BEARERS];
807f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair bpairs_temp[TIPC_MAX_LINK_PRI + 1];
816c00055aSDavid S. Miller 	struct tipc_node_map remains;
826c00055aSDavid S. Miller 	struct tipc_node_map remains_new;
83b97bf3fdSPer Liden };
84b97bf3fdSPer Liden 
85b97bf3fdSPer Liden /**
866765fd67SPaul Gortmaker  * struct tipc_bclink - link used for broadcast messages
87d69afc90SYing Xue  * @lock: spinlock governing access to structure
88b97bf3fdSPer Liden  * @link: (non-standard) broadcast link structure
89b97bf3fdSPer Liden  * @node: (non-standard) node structure representing b'cast link's peer node
903f5a12bdSYing Xue  * @flags: represent bclink states
91cd3decdfSAllan Stephens  * @bcast_nodes: map of broadcast-capable nodes
9201d83eddSAllan Stephens  * @retransmit_to: node that most recently requested a retransmit
93b97bf3fdSPer Liden  *
94b97bf3fdSPer Liden  * Handles sequence numbering, fragmentation, bundling, etc.
95b97bf3fdSPer Liden  */
966765fd67SPaul Gortmaker struct tipc_bclink {
97d69afc90SYing Xue 	spinlock_t lock;
98a18c4bc3SPaul Gortmaker 	struct tipc_link link;
996c00055aSDavid S. Miller 	struct tipc_node node;
1003f5a12bdSYing Xue 	unsigned int flags;
101cd3decdfSAllan Stephens 	struct tipc_node_map bcast_nodes;
10201d83eddSAllan Stephens 	struct tipc_node *retransmit_to;
103b97bf3fdSPer Liden };
104b97bf3fdSPer Liden 
105eb8b00f5SYing Xue static struct tipc_bcbearer *bcbearer;
106eb8b00f5SYing Xue static struct tipc_bclink *bclink;
107eb8b00f5SYing Xue static struct tipc_link *bcl;
108c47e9b91SAllan Stephens 
1093aec9cc9SAllan Stephens const char tipc_bclink_name[] = "broadcast-link";
110b97bf3fdSPer Liden 
11131e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
11231e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
11331e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_diff);
11428dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node);
11528dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node);
116b97bf3fdSPer Liden 
117d69afc90SYing Xue static void tipc_bclink_lock(void)
118d69afc90SYing Xue {
119d69afc90SYing Xue 	spin_lock_bh(&bclink->lock);
120d69afc90SYing Xue }
121d69afc90SYing Xue 
122d69afc90SYing Xue static void tipc_bclink_unlock(void)
123d69afc90SYing Xue {
1243f5a12bdSYing Xue 	struct tipc_node *node = NULL;
1253f5a12bdSYing Xue 
1263f5a12bdSYing Xue 	if (likely(!bclink->flags)) {
127d69afc90SYing Xue 		spin_unlock_bh(&bclink->lock);
1283f5a12bdSYing Xue 		return;
1293f5a12bdSYing Xue 	}
1303f5a12bdSYing Xue 
1313f5a12bdSYing Xue 	if (bclink->flags & TIPC_BCLINK_RESET) {
1323f5a12bdSYing Xue 		bclink->flags &= ~TIPC_BCLINK_RESET;
1333f5a12bdSYing Xue 		node = tipc_bclink_retransmit_to();
1343f5a12bdSYing Xue 	}
1353f5a12bdSYing Xue 	spin_unlock_bh(&bclink->lock);
1363f5a12bdSYing Xue 
1373f5a12bdSYing Xue 	if (node)
1383f5a12bdSYing Xue 		tipc_link_reset_all(node);
1393f5a12bdSYing Xue }
1403f5a12bdSYing Xue 
1413f5a12bdSYing Xue void tipc_bclink_set_flags(unsigned int flags)
1423f5a12bdSYing Xue {
1433f5a12bdSYing Xue 	bclink->flags |= flags;
144d69afc90SYing Xue }
145d69afc90SYing Xue 
14605790c64SSam Ravnborg static u32 bcbuf_acks(struct sk_buff *buf)
147b97bf3fdSPer Liden {
148880b005fSDavid S. Miller 	return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
149b97bf3fdSPer Liden }
150b97bf3fdSPer Liden 
15105790c64SSam Ravnborg static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
152b97bf3fdSPer Liden {
153880b005fSDavid S. Miller 	TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
154b97bf3fdSPer Liden }
155b97bf3fdSPer Liden 
15605790c64SSam Ravnborg static void bcbuf_decr_acks(struct sk_buff *buf)
157b97bf3fdSPer Liden {
158b97bf3fdSPer Liden 	bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
159b97bf3fdSPer Liden }
160b97bf3fdSPer Liden 
161cd3decdfSAllan Stephens void tipc_bclink_add_node(u32 addr)
162cd3decdfSAllan Stephens {
163d69afc90SYing Xue 	tipc_bclink_lock();
164cd3decdfSAllan Stephens 	tipc_nmap_add(&bclink->bcast_nodes, addr);
165d69afc90SYing Xue 	tipc_bclink_unlock();
166cd3decdfSAllan Stephens }
167cd3decdfSAllan Stephens 
168cd3decdfSAllan Stephens void tipc_bclink_remove_node(u32 addr)
169cd3decdfSAllan Stephens {
170d69afc90SYing Xue 	tipc_bclink_lock();
171cd3decdfSAllan Stephens 	tipc_nmap_remove(&bclink->bcast_nodes, addr);
172d69afc90SYing Xue 	tipc_bclink_unlock();
173cd3decdfSAllan Stephens }
174b97bf3fdSPer Liden 
1755b1f7bdeSAllan Stephens static void bclink_set_last_sent(void)
1765b1f7bdeSAllan Stephens {
1775b1f7bdeSAllan Stephens 	if (bcl->next_out)
1785b1f7bdeSAllan Stephens 		bcl->fsm_msg_cnt = mod(buf_seqno(bcl->next_out) - 1);
1795b1f7bdeSAllan Stephens 	else
1805b1f7bdeSAllan Stephens 		bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
1815b1f7bdeSAllan Stephens }
1825b1f7bdeSAllan Stephens 
1835b1f7bdeSAllan Stephens u32 tipc_bclink_get_last_sent(void)
1845b1f7bdeSAllan Stephens {
1855b1f7bdeSAllan Stephens 	return bcl->fsm_msg_cnt;
1865b1f7bdeSAllan Stephens }
1875b1f7bdeSAllan Stephens 
1887a54d4a9SAllan Stephens static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
189b97bf3fdSPer Liden {
1907a54d4a9SAllan Stephens 	node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
1917a54d4a9SAllan Stephens 						seqno : node->bclink.last_sent;
192b97bf3fdSPer Liden }
193b97bf3fdSPer Liden 
194b97bf3fdSPer Liden 
1952c53040fSBen Hutchings /**
19601d83eddSAllan Stephens  * tipc_bclink_retransmit_to - get most recent node to request retransmission
19701d83eddSAllan Stephens  *
198d69afc90SYing Xue  * Called with bclink_lock locked
19901d83eddSAllan Stephens  */
20001d83eddSAllan Stephens struct tipc_node *tipc_bclink_retransmit_to(void)
20101d83eddSAllan Stephens {
20201d83eddSAllan Stephens 	return bclink->retransmit_to;
20301d83eddSAllan Stephens }
20401d83eddSAllan Stephens 
20501d83eddSAllan Stephens /**
206b97bf3fdSPer Liden  * bclink_retransmit_pkt - retransmit broadcast packets
207b97bf3fdSPer Liden  * @after: sequence number of last packet to *not* retransmit
208b97bf3fdSPer Liden  * @to: sequence number of last packet to retransmit
209b97bf3fdSPer Liden  *
210d69afc90SYing Xue  * Called with bclink_lock locked
211b97bf3fdSPer Liden  */
212b97bf3fdSPer Liden static void bclink_retransmit_pkt(u32 after, u32 to)
213b97bf3fdSPer Liden {
214b97bf3fdSPer Liden 	struct sk_buff *buf;
215b97bf3fdSPer Liden 
216b97bf3fdSPer Liden 	buf = bcl->first_out;
217a016892cSAllan Stephens 	while (buf && less_eq(buf_seqno(buf), after))
218b97bf3fdSPer Liden 		buf = buf->next;
2194323add6SPer Liden 	tipc_link_retransmit(bcl, buf, mod(to - after));
220b97bf3fdSPer Liden }
221b97bf3fdSPer Liden 
222b97bf3fdSPer Liden /**
2234323add6SPer Liden  * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
224b97bf3fdSPer Liden  * @n_ptr: node that sent acknowledgement info
225b97bf3fdSPer Liden  * @acked: broadcast sequence # that has been acknowledged
226b97bf3fdSPer Liden  *
227d69afc90SYing Xue  * Node is locked, bclink_lock unlocked.
228b97bf3fdSPer Liden  */
2296c00055aSDavid S. Miller void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
230b97bf3fdSPer Liden {
231b97bf3fdSPer Liden 	struct sk_buff *crs;
232b97bf3fdSPer Liden 	struct sk_buff *next;
233b97bf3fdSPer Liden 	unsigned int released = 0;
234b97bf3fdSPer Liden 
235d69afc90SYing Xue 	tipc_bclink_lock();
23636559591SAllan Stephens 	/* Bail out if tx queue is empty (no clean up is required) */
237b97bf3fdSPer Liden 	crs = bcl->first_out;
23836559591SAllan Stephens 	if (!crs)
23936559591SAllan Stephens 		goto exit;
24036559591SAllan Stephens 
24136559591SAllan Stephens 	/* Determine which messages need to be acknowledged */
24236559591SAllan Stephens 	if (acked == INVALID_LINK_SEQ) {
24336559591SAllan Stephens 		/*
24436559591SAllan Stephens 		 * Contact with specified node has been lost, so need to
24536559591SAllan Stephens 		 * acknowledge sent messages only (if other nodes still exist)
24636559591SAllan Stephens 		 * or both sent and unsent messages (otherwise)
24736559591SAllan Stephens 		 */
24836559591SAllan Stephens 		if (bclink->bcast_nodes.count)
24936559591SAllan Stephens 			acked = bcl->fsm_msg_cnt;
25036559591SAllan Stephens 		else
25136559591SAllan Stephens 			acked = bcl->next_out_no;
25236559591SAllan Stephens 	} else {
25336559591SAllan Stephens 		/*
25436559591SAllan Stephens 		 * Bail out if specified sequence number does not correspond
25536559591SAllan Stephens 		 * to a message that has been sent and not yet acknowledged
25636559591SAllan Stephens 		 */
25736559591SAllan Stephens 		if (less(acked, buf_seqno(crs)) ||
25836559591SAllan Stephens 		    less(bcl->fsm_msg_cnt, acked) ||
25936559591SAllan Stephens 		    less_eq(acked, n_ptr->bclink.acked))
26036559591SAllan Stephens 			goto exit;
26136559591SAllan Stephens 	}
26236559591SAllan Stephens 
26336559591SAllan Stephens 	/* Skip over packets that node has previously acknowledged */
264a016892cSAllan Stephens 	while (crs && less_eq(buf_seqno(crs), n_ptr->bclink.acked))
265b97bf3fdSPer Liden 		crs = crs->next;
266b97bf3fdSPer Liden 
267b97bf3fdSPer Liden 	/* Update packets that node is now acknowledging */
268b97bf3fdSPer Liden 
269b97bf3fdSPer Liden 	while (crs && less_eq(buf_seqno(crs), acked)) {
270b97bf3fdSPer Liden 		next = crs->next;
27110745cd5SAllan Stephens 
27210745cd5SAllan Stephens 		if (crs != bcl->next_out)
273b97bf3fdSPer Liden 			bcbuf_decr_acks(crs);
27410745cd5SAllan Stephens 		else {
27510745cd5SAllan Stephens 			bcbuf_set_acks(crs, 0);
27610745cd5SAllan Stephens 			bcl->next_out = next;
27710745cd5SAllan Stephens 			bclink_set_last_sent();
27810745cd5SAllan Stephens 		}
27910745cd5SAllan Stephens 
280b97bf3fdSPer Liden 		if (bcbuf_acks(crs) == 0) {
281b97bf3fdSPer Liden 			bcl->first_out = next;
282b97bf3fdSPer Liden 			bcl->out_queue_size--;
2835f6d9123SAllan Stephens 			kfree_skb(crs);
284b97bf3fdSPer Liden 			released = 1;
285b97bf3fdSPer Liden 		}
286b97bf3fdSPer Liden 		crs = next;
287b97bf3fdSPer Liden 	}
288b97bf3fdSPer Liden 	n_ptr->bclink.acked = acked;
289b97bf3fdSPer Liden 
290b97bf3fdSPer Liden 	/* Try resolving broadcast link congestion, if necessary */
291b97bf3fdSPer Liden 
2925b1f7bdeSAllan Stephens 	if (unlikely(bcl->next_out)) {
2934323add6SPer Liden 		tipc_link_push_queue(bcl);
2945b1f7bdeSAllan Stephens 		bclink_set_last_sent();
2955b1f7bdeSAllan Stephens 	}
296b97bf3fdSPer Liden 	if (unlikely(released && !list_empty(&bcl->waiting_ports)))
2974323add6SPer Liden 		tipc_link_wakeup_ports(bcl, 0);
29836559591SAllan Stephens exit:
299d69afc90SYing Xue 	tipc_bclink_unlock();
300b97bf3fdSPer Liden }
301b97bf3fdSPer Liden 
3022c53040fSBen Hutchings /**
3037a54d4a9SAllan Stephens  * tipc_bclink_update_link_state - update broadcast link state
304b97bf3fdSPer Liden  *
3057216cd94SYing Xue  * RCU and node lock set
306b97bf3fdSPer Liden  */
3077a54d4a9SAllan Stephens void tipc_bclink_update_link_state(struct tipc_node *n_ptr, u32 last_sent)
308b97bf3fdSPer Liden {
309b97bf3fdSPer Liden 	struct sk_buff *buf;
310b97bf3fdSPer Liden 
3117a54d4a9SAllan Stephens 	/* Ignore "stale" link state info */
3127a54d4a9SAllan Stephens 
3137a54d4a9SAllan Stephens 	if (less_eq(last_sent, n_ptr->bclink.last_in))
314b97bf3fdSPer Liden 		return;
315b97bf3fdSPer Liden 
3167a54d4a9SAllan Stephens 	/* Update link synchronization state; quit if in sync */
3177a54d4a9SAllan Stephens 
3187a54d4a9SAllan Stephens 	bclink_update_last_sent(n_ptr, last_sent);
3197a54d4a9SAllan Stephens 
3207a54d4a9SAllan Stephens 	if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
3217a54d4a9SAllan Stephens 		return;
3227a54d4a9SAllan Stephens 
3237a54d4a9SAllan Stephens 	/* Update out-of-sync state; quit if loss is still unconfirmed */
3247a54d4a9SAllan Stephens 
3257a54d4a9SAllan Stephens 	if ((++n_ptr->bclink.oos_state) == 1) {
3267a54d4a9SAllan Stephens 		if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
3277a54d4a9SAllan Stephens 			return;
3287a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
3297a54d4a9SAllan Stephens 	}
3307a54d4a9SAllan Stephens 
3317a54d4a9SAllan Stephens 	/* Don't NACK if one has been recently sent (or seen) */
3327a54d4a9SAllan Stephens 
3337a54d4a9SAllan Stephens 	if (n_ptr->bclink.oos_state & 0x1)
3347a54d4a9SAllan Stephens 		return;
3357a54d4a9SAllan Stephens 
3367a54d4a9SAllan Stephens 	/* Send NACK */
3377a54d4a9SAllan Stephens 
33831e3c3f6Sstephen hemminger 	buf = tipc_buf_acquire(INT_H_SIZE);
339b97bf3fdSPer Liden 	if (buf) {
3407a54d4a9SAllan Stephens 		struct tipc_msg *msg = buf_msg(buf);
3417a54d4a9SAllan Stephens 
342c68ca7b7SAllan Stephens 		tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG,
34375715217SAllan Stephens 			      INT_H_SIZE, n_ptr->addr);
344bf781ecfSAllan Stephens 		msg_set_non_seq(msg, 1);
345b97bf3fdSPer Liden 		msg_set_mc_netid(msg, tipc_net_id);
3467a54d4a9SAllan Stephens 		msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
3477a54d4a9SAllan Stephens 		msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
3487a54d4a9SAllan Stephens 		msg_set_bcgap_to(msg, n_ptr->bclink.deferred_head
3497a54d4a9SAllan Stephens 				 ? buf_seqno(n_ptr->bclink.deferred_head) - 1
3507a54d4a9SAllan Stephens 				 : n_ptr->bclink.last_sent);
351b97bf3fdSPer Liden 
352d69afc90SYing Xue 		tipc_bclink_lock();
3537a2f7d18SYing Xue 		tipc_bearer_send(MAX_BEARERS, buf, NULL);
354b97bf3fdSPer Liden 		bcl->stats.sent_nacks++;
355d69afc90SYing Xue 		tipc_bclink_unlock();
3565f6d9123SAllan Stephens 		kfree_skb(buf);
357b97bf3fdSPer Liden 
3587a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
3597a54d4a9SAllan Stephens 	}
3607a54d4a9SAllan Stephens }
3617a54d4a9SAllan Stephens 
3622c53040fSBen Hutchings /**
3637a54d4a9SAllan Stephens  * bclink_peek_nack - monitor retransmission requests sent by other nodes
364b97bf3fdSPer Liden  *
3657a54d4a9SAllan Stephens  * Delay any upcoming NACK by this node if another node has already
3667a54d4a9SAllan Stephens  * requested the first message this node is going to ask for.
367b97bf3fdSPer Liden  */
3687a54d4a9SAllan Stephens static void bclink_peek_nack(struct tipc_msg *msg)
369b97bf3fdSPer Liden {
3707a54d4a9SAllan Stephens 	struct tipc_node *n_ptr = tipc_node_find(msg_destnode(msg));
371b97bf3fdSPer Liden 
3727a54d4a9SAllan Stephens 	if (unlikely(!n_ptr))
373b97bf3fdSPer Liden 		return;
3747a54d4a9SAllan Stephens 
3754323add6SPer Liden 	tipc_node_lock(n_ptr);
376b97bf3fdSPer Liden 
377389dd9bcSYing Xue 	if (n_ptr->bclink.recv_permitted &&
3787a54d4a9SAllan Stephens 	    (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
3797a54d4a9SAllan Stephens 	    (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
3807a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state = 2;
381b97bf3fdSPer Liden 
3824323add6SPer Liden 	tipc_node_unlock(n_ptr);
383b97bf3fdSPer Liden }
384b97bf3fdSPer Liden 
3857a54d4a9SAllan Stephens /*
386247f0f3cSYing Xue  * tipc_bclink_xmit - broadcast a packet to all nodes in cluster
387b97bf3fdSPer Liden  */
388247f0f3cSYing Xue int tipc_bclink_xmit(struct sk_buff *buf)
389b97bf3fdSPer Liden {
390b97bf3fdSPer Liden 	int res;
391b97bf3fdSPer Liden 
392d69afc90SYing Xue 	tipc_bclink_lock();
393b97bf3fdSPer Liden 
3942b78f9a0SAllan Stephens 	if (!bclink->bcast_nodes.count) {
3952b78f9a0SAllan Stephens 		res = msg_data_sz(buf_msg(buf));
3965f6d9123SAllan Stephens 		kfree_skb(buf);
3972b78f9a0SAllan Stephens 		goto exit;
3982b78f9a0SAllan Stephens 	}
3992b78f9a0SAllan Stephens 
400247f0f3cSYing Xue 	res = __tipc_link_xmit(bcl, buf);
4019157bafbSAllan Stephens 	if (likely(res >= 0)) {
4025b1f7bdeSAllan Stephens 		bclink_set_last_sent();
403b97bf3fdSPer Liden 		bcl->stats.queue_sz_counts++;
404b97bf3fdSPer Liden 		bcl->stats.accu_queue_sz += bcl->out_queue_size;
4059157bafbSAllan Stephens 	}
4062b78f9a0SAllan Stephens exit:
407d69afc90SYing Xue 	tipc_bclink_unlock();
408b97bf3fdSPer Liden 	return res;
409b97bf3fdSPer Liden }
410b97bf3fdSPer Liden 
4112c53040fSBen Hutchings /**
41263e7f1acSAllan Stephens  * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
41363e7f1acSAllan Stephens  *
414d69afc90SYing Xue  * Called with both sending node's lock and bclink_lock taken.
41563e7f1acSAllan Stephens  */
41663e7f1acSAllan Stephens static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
41763e7f1acSAllan Stephens {
41863e7f1acSAllan Stephens 	bclink_update_last_sent(node, seqno);
41963e7f1acSAllan Stephens 	node->bclink.last_in = seqno;
42063e7f1acSAllan Stephens 	node->bclink.oos_state = 0;
42163e7f1acSAllan Stephens 	bcl->stats.recv_info++;
42263e7f1acSAllan Stephens 
42363e7f1acSAllan Stephens 	/*
42463e7f1acSAllan Stephens 	 * Unicast an ACK periodically, ensuring that
42563e7f1acSAllan Stephens 	 * all nodes in the cluster don't ACK at the same time
42663e7f1acSAllan Stephens 	 */
42763e7f1acSAllan Stephens 
42863e7f1acSAllan Stephens 	if (((seqno - tipc_own_addr) % TIPC_MIN_LINK_WIN) == 0) {
429247f0f3cSYing Xue 		tipc_link_proto_xmit(node->active_links[node->addr & 1],
43063e7f1acSAllan Stephens 				     STATE_MSG, 0, 0, 0, 0, 0);
43163e7f1acSAllan Stephens 		bcl->stats.sent_acks++;
43263e7f1acSAllan Stephens 	}
43363e7f1acSAllan Stephens }
43463e7f1acSAllan Stephens 
4352c53040fSBen Hutchings /**
436247f0f3cSYing Xue  * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
437b97bf3fdSPer Liden  *
4387216cd94SYing Xue  * RCU is locked, no other locks set
439b97bf3fdSPer Liden  */
440247f0f3cSYing Xue void tipc_bclink_rcv(struct sk_buff *buf)
441b97bf3fdSPer Liden {
442b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
4435d3c488dSAllan Stephens 	struct tipc_node *node;
444b97bf3fdSPer Liden 	u32 next_in;
445b97bf3fdSPer Liden 	u32 seqno;
4468a275a6aSAllan Stephens 	int deferred;
447b97bf3fdSPer Liden 
4485d3c488dSAllan Stephens 	/* Screen out unwanted broadcast messages */
4495d3c488dSAllan Stephens 
4505d3c488dSAllan Stephens 	if (msg_mc_netid(msg) != tipc_net_id)
4515d3c488dSAllan Stephens 		goto exit;
4525d3c488dSAllan Stephens 
4535d3c488dSAllan Stephens 	node = tipc_node_find(msg_prevnode(msg));
4545d3c488dSAllan Stephens 	if (unlikely(!node))
4555d3c488dSAllan Stephens 		goto exit;
4565d3c488dSAllan Stephens 
4575d3c488dSAllan Stephens 	tipc_node_lock(node);
458389dd9bcSYing Xue 	if (unlikely(!node->bclink.recv_permitted))
4595d3c488dSAllan Stephens 		goto unlock;
460b97bf3fdSPer Liden 
4618a275a6aSAllan Stephens 	/* Handle broadcast protocol message */
4628a275a6aSAllan Stephens 
463b97bf3fdSPer Liden 	if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
4649f6bdcd4SAllan Stephens 		if (msg_type(msg) != STATE_MSG)
4659f6bdcd4SAllan Stephens 			goto unlock;
466b97bf3fdSPer Liden 		if (msg_destnode(msg) == tipc_own_addr) {
4674323add6SPer Liden 			tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
4684323add6SPer Liden 			tipc_node_unlock(node);
469d69afc90SYing Xue 			tipc_bclink_lock();
470b97bf3fdSPer Liden 			bcl->stats.recv_nacks++;
47101d83eddSAllan Stephens 			bclink->retransmit_to = node;
472b97bf3fdSPer Liden 			bclink_retransmit_pkt(msg_bcgap_after(msg),
473b97bf3fdSPer Liden 					      msg_bcgap_to(msg));
474d69afc90SYing Xue 			tipc_bclink_unlock();
475b97bf3fdSPer Liden 		} else {
4765d3c488dSAllan Stephens 			tipc_node_unlock(node);
4777a54d4a9SAllan Stephens 			bclink_peek_nack(msg);
478b97bf3fdSPer Liden 		}
4795d3c488dSAllan Stephens 		goto exit;
480b97bf3fdSPer Liden 	}
481b97bf3fdSPer Liden 
4825d3c488dSAllan Stephens 	/* Handle in-sequence broadcast message */
4835d3c488dSAllan Stephens 
484b97bf3fdSPer Liden 	seqno = msg_seqno(msg);
4858a275a6aSAllan Stephens 	next_in = mod(node->bclink.last_in + 1);
486b97bf3fdSPer Liden 
487b97bf3fdSPer Liden 	if (likely(seqno == next_in)) {
4888a275a6aSAllan Stephens receive:
4897a54d4a9SAllan Stephens 		/* Deliver message to destination */
4907a54d4a9SAllan Stephens 
491b97bf3fdSPer Liden 		if (likely(msg_isdata(msg))) {
492d69afc90SYing Xue 			tipc_bclink_lock();
49363e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
494d69afc90SYing Xue 			tipc_bclink_unlock();
4954323add6SPer Liden 			tipc_node_unlock(node);
4969f6bdcd4SAllan Stephens 			if (likely(msg_mcast(msg)))
497247f0f3cSYing Xue 				tipc_port_mcast_rcv(buf, NULL);
4989f6bdcd4SAllan Stephens 			else
4995f6d9123SAllan Stephens 				kfree_skb(buf);
500b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_BUNDLER) {
501d69afc90SYing Xue 			tipc_bclink_lock();
50263e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
503b97bf3fdSPer Liden 			bcl->stats.recv_bundles++;
504b97bf3fdSPer Liden 			bcl->stats.recv_bundled += msg_msgcnt(msg);
505d69afc90SYing Xue 			tipc_bclink_unlock();
5064323add6SPer Liden 			tipc_node_unlock(node);
507247f0f3cSYing Xue 			tipc_link_bundle_rcv(buf);
508b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_FRAGMENTER) {
50937e22164SJon Paul Maloy 			tipc_buf_append(&node->bclink.reasm_buf, &buf);
51037e22164SJon Paul Maloy 			if (unlikely(!buf && !node->bclink.reasm_buf))
51163e7f1acSAllan Stephens 				goto unlock;
512d69afc90SYing Xue 			tipc_bclink_lock();
51363e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
514b97bf3fdSPer Liden 			bcl->stats.recv_fragments++;
51537e22164SJon Paul Maloy 			if (buf) {
516b97bf3fdSPer Liden 				bcl->stats.recv_fragmented++;
51740ba3cdfSErik Hugne 				msg = buf_msg(buf);
518d69afc90SYing Xue 				tipc_bclink_unlock();
519528f6f4bSErik Hugne 				goto receive;
520528f6f4bSErik Hugne 			}
521d69afc90SYing Xue 			tipc_bclink_unlock();
5224323add6SPer Liden 			tipc_node_unlock(node);
5239f6bdcd4SAllan Stephens 		} else if (msg_user(msg) == NAME_DISTRIBUTOR) {
524d69afc90SYing Xue 			tipc_bclink_lock();
52563e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
526d69afc90SYing Xue 			tipc_bclink_unlock();
5279f6bdcd4SAllan Stephens 			tipc_node_unlock(node);
528247f0f3cSYing Xue 			tipc_named_rcv(buf);
529b97bf3fdSPer Liden 		} else {
530d69afc90SYing Xue 			tipc_bclink_lock();
53163e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
532d69afc90SYing Xue 			tipc_bclink_unlock();
5334323add6SPer Liden 			tipc_node_unlock(node);
5345f6d9123SAllan Stephens 			kfree_skb(buf);
535b97bf3fdSPer Liden 		}
5365d3c488dSAllan Stephens 		buf = NULL;
5378a275a6aSAllan Stephens 
5388a275a6aSAllan Stephens 		/* Determine new synchronization state */
5398a275a6aSAllan Stephens 
5404323add6SPer Liden 		tipc_node_lock(node);
5418a275a6aSAllan Stephens 		if (unlikely(!tipc_node_is_up(node)))
5428a275a6aSAllan Stephens 			goto unlock;
5438a275a6aSAllan Stephens 
5447a54d4a9SAllan Stephens 		if (node->bclink.last_in == node->bclink.last_sent)
5458a275a6aSAllan Stephens 			goto unlock;
5468a275a6aSAllan Stephens 
5477a54d4a9SAllan Stephens 		if (!node->bclink.deferred_head) {
5487a54d4a9SAllan Stephens 			node->bclink.oos_state = 1;
5497a54d4a9SAllan Stephens 			goto unlock;
5507a54d4a9SAllan Stephens 		}
5517a54d4a9SAllan Stephens 
5528a275a6aSAllan Stephens 		msg = buf_msg(node->bclink.deferred_head);
5538a275a6aSAllan Stephens 		seqno = msg_seqno(msg);
5548a275a6aSAllan Stephens 		next_in = mod(next_in + 1);
5558a275a6aSAllan Stephens 		if (seqno != next_in)
5568a275a6aSAllan Stephens 			goto unlock;
5578a275a6aSAllan Stephens 
5588a275a6aSAllan Stephens 		/* Take in-sequence message from deferred queue & deliver it */
5598a275a6aSAllan Stephens 
5608a275a6aSAllan Stephens 		buf = node->bclink.deferred_head;
5618a275a6aSAllan Stephens 		node->bclink.deferred_head = buf->next;
562*99941754SJon Paul Maloy 		buf->next = NULL;
5637a54d4a9SAllan Stephens 		node->bclink.deferred_size--;
564b97bf3fdSPer Liden 		goto receive;
565b97bf3fdSPer Liden 	}
5668a275a6aSAllan Stephens 
5678a275a6aSAllan Stephens 	/* Handle out-of-sequence broadcast message */
5688a275a6aSAllan Stephens 
5698a275a6aSAllan Stephens 	if (less(next_in, seqno)) {
5708a275a6aSAllan Stephens 		deferred = tipc_link_defer_pkt(&node->bclink.deferred_head,
571b97bf3fdSPer Liden 					       &node->bclink.deferred_tail,
5728a275a6aSAllan Stephens 					       buf);
5737a54d4a9SAllan Stephens 		node->bclink.deferred_size += deferred;
5747a54d4a9SAllan Stephens 		bclink_update_last_sent(node, seqno);
5755d3c488dSAllan Stephens 		buf = NULL;
5768a275a6aSAllan Stephens 	} else
5778a275a6aSAllan Stephens 		deferred = 0;
5788a275a6aSAllan Stephens 
579d69afc90SYing Xue 	tipc_bclink_lock();
580b98158e3SAllan Stephens 
5818a275a6aSAllan Stephens 	if (deferred)
5828a275a6aSAllan Stephens 		bcl->stats.deferred_recv++;
5830232c5a5SAllan Stephens 	else
5840232c5a5SAllan Stephens 		bcl->stats.duplicates++;
5858a275a6aSAllan Stephens 
586d69afc90SYing Xue 	tipc_bclink_unlock();
587b98158e3SAllan Stephens 
5885d3c488dSAllan Stephens unlock:
5894323add6SPer Liden 	tipc_node_unlock(node);
5905d3c488dSAllan Stephens exit:
5915f6d9123SAllan Stephens 	kfree_skb(buf);
592b97bf3fdSPer Liden }
593b97bf3fdSPer Liden 
5946c00055aSDavid S. Miller u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
595b97bf3fdSPer Liden {
596389dd9bcSYing Xue 	return (n_ptr->bclink.recv_permitted &&
5974323add6SPer Liden 		(tipc_bclink_get_last_sent() != n_ptr->bclink.acked));
598b97bf3fdSPer Liden }
599b97bf3fdSPer Liden 
600b97bf3fdSPer Liden 
601b97bf3fdSPer Liden /**
6024323add6SPer Liden  * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
603b97bf3fdSPer Liden  *
6042ff9f924SAllan Stephens  * Send packet over as many bearers as necessary to reach all nodes
6052ff9f924SAllan Stephens  * that have joined the broadcast link.
606b97bf3fdSPer Liden  *
6072ff9f924SAllan Stephens  * Returns 0 (packet sent successfully) under all circumstances,
6082ff9f924SAllan Stephens  * since the broadcast link's pseudo-bearer never blocks
609b97bf3fdSPer Liden  */
610ae8509c4SPaul Gortmaker static int tipc_bcbearer_send(struct sk_buff *buf, struct tipc_bearer *unused1,
611b97bf3fdSPer Liden 			      struct tipc_media_addr *unused2)
612b97bf3fdSPer Liden {
613b97bf3fdSPer Liden 	int bp_index;
614b97bf3fdSPer Liden 
615e6160710SGerlando Falauto 	/* Prepare broadcast link message for reliable transmission,
6162ff9f924SAllan Stephens 	 * if first time trying to send it;
6172ff9f924SAllan Stephens 	 * preparation is skipped for broadcast link protocol messages
6182ff9f924SAllan Stephens 	 * since they are sent in an unreliable manner and don't need it
6192ff9f924SAllan Stephens 	 */
620b97bf3fdSPer Liden 	if (likely(!msg_non_seq(buf_msg(buf)))) {
621b97bf3fdSPer Liden 		struct tipc_msg *msg;
622b97bf3fdSPer Liden 
623cd3decdfSAllan Stephens 		bcbuf_set_acks(buf, bclink->bcast_nodes.count);
624b97bf3fdSPer Liden 		msg = buf_msg(buf);
62540aecb1bSAllan Stephens 		msg_set_non_seq(msg, 1);
626b97bf3fdSPer Liden 		msg_set_mc_netid(msg, tipc_net_id);
6270048b826SAllan Stephens 		bcl->stats.sent_info++;
6285e726900SAllan Stephens 
629cd3decdfSAllan Stephens 		if (WARN_ON(!bclink->bcast_nodes.count)) {
6305e726900SAllan Stephens 			dump_stack();
6315e726900SAllan Stephens 			return 0;
6325e726900SAllan Stephens 		}
633b97bf3fdSPer Liden 	}
634b97bf3fdSPer Liden 
635b97bf3fdSPer Liden 	/* Send buffer over bearers until all targets reached */
636cd3decdfSAllan Stephens 	bcbearer->remains = bclink->bcast_nodes;
637b97bf3fdSPer Liden 
638b97bf3fdSPer Liden 	for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
6392d627b92SAllan Stephens 		struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
6402d627b92SAllan Stephens 		struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
64177861d9cSGerlando Falauto 		struct tipc_bearer *b = p;
642488fc9afSGerlando Falauto 		struct sk_buff *tbuf;
643b97bf3fdSPer Liden 
644b97bf3fdSPer Liden 		if (!p)
645e6160710SGerlando Falauto 			break; /* No more bearers to try */
646b97bf3fdSPer Liden 
64777861d9cSGerlando Falauto 		tipc_nmap_diff(&bcbearer->remains, &b->nodes,
648e6160710SGerlando Falauto 			       &bcbearer->remains_new);
64965f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == bcbearer->remains.count)
650e6160710SGerlando Falauto 			continue; /* Nothing added by bearer pair */
651b97bf3fdSPer Liden 
652488fc9afSGerlando Falauto 		if (bp_index == 0) {
653488fc9afSGerlando Falauto 			/* Use original buffer for first bearer */
6547a2f7d18SYing Xue 			tipc_bearer_send(b->identity, buf, &b->bcast_addr);
655488fc9afSGerlando Falauto 		} else {
656488fc9afSGerlando Falauto 			/* Avoid concurrent buffer access */
657bad93e9dSOctavian Purdila 			tbuf = pskb_copy_for_clone(buf, GFP_ATOMIC);
658488fc9afSGerlando Falauto 			if (!tbuf)
659488fc9afSGerlando Falauto 				break;
6607a2f7d18SYing Xue 			tipc_bearer_send(b->identity, tbuf, &b->bcast_addr);
661488fc9afSGerlando Falauto 			kfree_skb(tbuf); /* Bearer keeps a clone */
662488fc9afSGerlando Falauto 		}
663b97bf3fdSPer Liden 
664e6160710SGerlando Falauto 		/* Swap bearers for next packet */
6651a624832SNeil Horman 		if (s) {
666b97bf3fdSPer Liden 			bcbearer->bpairs[bp_index].primary = s;
667b97bf3fdSPer Liden 			bcbearer->bpairs[bp_index].secondary = p;
6681a624832SNeil Horman 		}
6691a624832SNeil Horman 
67065f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == 0)
671e6160710SGerlando Falauto 			break; /* All targets reached */
672b97bf3fdSPer Liden 
67365f51ef0SAllan Stephens 		bcbearer->remains = bcbearer->remains_new;
674b97bf3fdSPer Liden 	}
675b97bf3fdSPer Liden 
6762ff9f924SAllan Stephens 	return 0;
677b97bf3fdSPer Liden }
678b97bf3fdSPer Liden 
679b97bf3fdSPer Liden /**
6804323add6SPer Liden  * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
681b97bf3fdSPer Liden  */
68228dd9418SYing Xue void tipc_bcbearer_sort(struct tipc_node_map *nm_ptr, u32 node, bool action)
683b97bf3fdSPer Liden {
6847f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
6857f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_curr;
686f8322dfcSYing Xue 	struct tipc_bearer *b;
687b97bf3fdSPer Liden 	int b_index;
688b97bf3fdSPer Liden 	int pri;
689b97bf3fdSPer Liden 
690d69afc90SYing Xue 	tipc_bclink_lock();
691b97bf3fdSPer Liden 
69228dd9418SYing Xue 	if (action)
69328dd9418SYing Xue 		tipc_nmap_add(nm_ptr, node);
69428dd9418SYing Xue 	else
69528dd9418SYing Xue 		tipc_nmap_remove(nm_ptr, node);
69628dd9418SYing Xue 
697b97bf3fdSPer Liden 	/* Group bearers by priority (can assume max of two per priority) */
698b97bf3fdSPer Liden 	memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
699b97bf3fdSPer Liden 
700f8322dfcSYing Xue 	rcu_read_lock();
701b97bf3fdSPer Liden 	for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
702f8322dfcSYing Xue 		b = rcu_dereference_rtnl(bearer_list[b_index]);
703f47de12bSYing Xue 		if (!b || !b->nodes.count)
704b97bf3fdSPer Liden 			continue;
705b97bf3fdSPer Liden 
706b97bf3fdSPer Liden 		if (!bp_temp[b->priority].primary)
707b97bf3fdSPer Liden 			bp_temp[b->priority].primary = b;
708b97bf3fdSPer Liden 		else
709b97bf3fdSPer Liden 			bp_temp[b->priority].secondary = b;
710b97bf3fdSPer Liden 	}
711f8322dfcSYing Xue 	rcu_read_unlock();
712b97bf3fdSPer Liden 
713b97bf3fdSPer Liden 	/* Create array of bearer pairs for broadcasting */
714b97bf3fdSPer Liden 	bp_curr = bcbearer->bpairs;
715b97bf3fdSPer Liden 	memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
716b97bf3fdSPer Liden 
71716cb4b33SPer Liden 	for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
718b97bf3fdSPer Liden 
719b97bf3fdSPer Liden 		if (!bp_temp[pri].primary)
720b97bf3fdSPer Liden 			continue;
721b97bf3fdSPer Liden 
722b97bf3fdSPer Liden 		bp_curr->primary = bp_temp[pri].primary;
723b97bf3fdSPer Liden 
724b97bf3fdSPer Liden 		if (bp_temp[pri].secondary) {
7254323add6SPer Liden 			if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
726b97bf3fdSPer Liden 					    &bp_temp[pri].secondary->nodes)) {
727b97bf3fdSPer Liden 				bp_curr->secondary = bp_temp[pri].secondary;
728b97bf3fdSPer Liden 			} else {
729b97bf3fdSPer Liden 				bp_curr++;
730b97bf3fdSPer Liden 				bp_curr->primary = bp_temp[pri].secondary;
731b97bf3fdSPer Liden 			}
732b97bf3fdSPer Liden 		}
733b97bf3fdSPer Liden 
734b97bf3fdSPer Liden 		bp_curr++;
735b97bf3fdSPer Liden 	}
736b97bf3fdSPer Liden 
737d69afc90SYing Xue 	tipc_bclink_unlock();
738b97bf3fdSPer Liden }
739b97bf3fdSPer Liden 
740b97bf3fdSPer Liden 
7414323add6SPer Liden int tipc_bclink_stats(char *buf, const u32 buf_size)
742b97bf3fdSPer Liden {
743dc1aed37SErik Hugne 	int ret;
744dc1aed37SErik Hugne 	struct tipc_stats *s;
745b97bf3fdSPer Liden 
746b97bf3fdSPer Liden 	if (!bcl)
747b97bf3fdSPer Liden 		return 0;
748b97bf3fdSPer Liden 
749d69afc90SYing Xue 	tipc_bclink_lock();
750b97bf3fdSPer Liden 
751dc1aed37SErik Hugne 	s = &bcl->stats;
752dc1aed37SErik Hugne 
753dc1aed37SErik Hugne 	ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
754b97bf3fdSPer Liden 			    "  Window:%u packets\n",
755b97bf3fdSPer Liden 			    bcl->name, bcl->queue_limit[0]);
756dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
757dc1aed37SErik Hugne 			     "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
758dc1aed37SErik Hugne 			     s->recv_info, s->recv_fragments,
759dc1aed37SErik Hugne 			     s->recv_fragmented, s->recv_bundles,
760dc1aed37SErik Hugne 			     s->recv_bundled);
761dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
762dc1aed37SErik Hugne 			     "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
763dc1aed37SErik Hugne 			     s->sent_info, s->sent_fragments,
764dc1aed37SErik Hugne 			     s->sent_fragmented, s->sent_bundles,
765dc1aed37SErik Hugne 			     s->sent_bundled);
766dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
767dc1aed37SErik Hugne 			     "  RX naks:%u defs:%u dups:%u\n",
768dc1aed37SErik Hugne 			     s->recv_nacks, s->deferred_recv, s->duplicates);
769dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
770dc1aed37SErik Hugne 			     "  TX naks:%u acks:%u dups:%u\n",
771dc1aed37SErik Hugne 			     s->sent_nacks, s->sent_acks, s->retransmitted);
772dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
7733c294cb3SYing Xue 			     "  Congestion link:%u  Send queue max:%u avg:%u\n",
7743c294cb3SYing Xue 			     s->link_congs, s->max_queue_sz,
775dc1aed37SErik Hugne 			     s->queue_sz_counts ?
776dc1aed37SErik Hugne 			     (s->accu_queue_sz / s->queue_sz_counts) : 0);
777b97bf3fdSPer Liden 
778d69afc90SYing Xue 	tipc_bclink_unlock();
779dc1aed37SErik Hugne 	return ret;
780b97bf3fdSPer Liden }
781b97bf3fdSPer Liden 
7824323add6SPer Liden int tipc_bclink_reset_stats(void)
783b97bf3fdSPer Liden {
784b97bf3fdSPer Liden 	if (!bcl)
785b97bf3fdSPer Liden 		return -ENOPROTOOPT;
786b97bf3fdSPer Liden 
787d69afc90SYing Xue 	tipc_bclink_lock();
788b97bf3fdSPer Liden 	memset(&bcl->stats, 0, sizeof(bcl->stats));
789d69afc90SYing Xue 	tipc_bclink_unlock();
7900e35fd5eSAllan Stephens 	return 0;
791b97bf3fdSPer Liden }
792b97bf3fdSPer Liden 
7934323add6SPer Liden int tipc_bclink_set_queue_limits(u32 limit)
794b97bf3fdSPer Liden {
795b97bf3fdSPer Liden 	if (!bcl)
796b97bf3fdSPer Liden 		return -ENOPROTOOPT;
797b97bf3fdSPer Liden 	if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
798b97bf3fdSPer Liden 		return -EINVAL;
799b97bf3fdSPer Liden 
800d69afc90SYing Xue 	tipc_bclink_lock();
8014323add6SPer Liden 	tipc_link_set_queue_limits(bcl, limit);
802d69afc90SYing Xue 	tipc_bclink_unlock();
8030e35fd5eSAllan Stephens 	return 0;
804b97bf3fdSPer Liden }
805b97bf3fdSPer Liden 
806eb8b00f5SYing Xue int tipc_bclink_init(void)
807b97bf3fdSPer Liden {
808eb8b00f5SYing Xue 	bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
809eb8b00f5SYing Xue 	if (!bcbearer)
810eb8b00f5SYing Xue 		return -ENOMEM;
811eb8b00f5SYing Xue 
812eb8b00f5SYing Xue 	bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
813eb8b00f5SYing Xue 	if (!bclink) {
814eb8b00f5SYing Xue 		kfree(bcbearer);
815eb8b00f5SYing Xue 		return -ENOMEM;
816eb8b00f5SYing Xue 	}
817eb8b00f5SYing Xue 
818eb8b00f5SYing Xue 	bcl = &bclink->link;
819b97bf3fdSPer Liden 	bcbearer->bearer.media = &bcbearer->media;
8204323add6SPer Liden 	bcbearer->media.send_msg = tipc_bcbearer_send;
8212e2d9be8SAllan Stephens 	sprintf(bcbearer->media.name, "tipc-broadcast");
822b97bf3fdSPer Liden 
823d69afc90SYing Xue 	spin_lock_init(&bclink->lock);
824b97bf3fdSPer Liden 	INIT_LIST_HEAD(&bcl->waiting_ports);
825b97bf3fdSPer Liden 	bcl->next_out_no = 1;
82634af946aSIngo Molnar 	spin_lock_init(&bclink->node.lock);
827b97bf3fdSPer Liden 	bcl->owner = &bclink->node;
828b97bf3fdSPer Liden 	bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
8294323add6SPer Liden 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
8307a2f7d18SYing Xue 	bcl->bearer_id = MAX_BEARERS;
831f8322dfcSYing Xue 	rcu_assign_pointer(bearer_list[MAX_BEARERS], &bcbearer->bearer);
832b97bf3fdSPer Liden 	bcl->state = WORKING_WORKING;
8334b704d59SStephen Hemminger 	strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
834eb8b00f5SYing Xue 	return 0;
835b97bf3fdSPer Liden }
836b97bf3fdSPer Liden 
8374323add6SPer Liden void tipc_bclink_stop(void)
838b97bf3fdSPer Liden {
839d69afc90SYing Xue 	tipc_bclink_lock();
840581465faSJon Paul Maloy 	tipc_link_purge_queues(bcl);
841d69afc90SYing Xue 	tipc_bclink_unlock();
842c47e9b91SAllan Stephens 
843f8322dfcSYing Xue 	RCU_INIT_POINTER(bearer_list[BCBEARER], NULL);
844eb8b00f5SYing Xue 	synchronize_net();
845eb8b00f5SYing Xue 	kfree(bcbearer);
846eb8b00f5SYing Xue 	kfree(bclink);
847b97bf3fdSPer Liden }
848b97bf3fdSPer Liden 
8493e22e62bSAllan Stephens /**
8503e22e62bSAllan Stephens  * tipc_nmap_add - add a node to a node map
8513e22e62bSAllan Stephens  */
85228dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
8533e22e62bSAllan Stephens {
8543e22e62bSAllan Stephens 	int n = tipc_node(node);
8553e22e62bSAllan Stephens 	int w = n / WSIZE;
8563e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
8573e22e62bSAllan Stephens 
8583e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) == 0) {
8593e22e62bSAllan Stephens 		nm_ptr->count++;
8603e22e62bSAllan Stephens 		nm_ptr->map[w] |= mask;
8613e22e62bSAllan Stephens 	}
8623e22e62bSAllan Stephens }
8633e22e62bSAllan Stephens 
8643e22e62bSAllan Stephens /**
8653e22e62bSAllan Stephens  * tipc_nmap_remove - remove a node from a node map
8663e22e62bSAllan Stephens  */
86728dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
8683e22e62bSAllan Stephens {
8693e22e62bSAllan Stephens 	int n = tipc_node(node);
8703e22e62bSAllan Stephens 	int w = n / WSIZE;
8713e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
8723e22e62bSAllan Stephens 
8733e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) != 0) {
8743e22e62bSAllan Stephens 		nm_ptr->map[w] &= ~mask;
8753e22e62bSAllan Stephens 		nm_ptr->count--;
8763e22e62bSAllan Stephens 	}
8773e22e62bSAllan Stephens }
8783e22e62bSAllan Stephens 
8793e22e62bSAllan Stephens /**
8803e22e62bSAllan Stephens  * tipc_nmap_diff - find differences between node maps
8813e22e62bSAllan Stephens  * @nm_a: input node map A
8823e22e62bSAllan Stephens  * @nm_b: input node map B
8833e22e62bSAllan Stephens  * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
8843e22e62bSAllan Stephens  */
88531e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
88631e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
8873e22e62bSAllan Stephens 			   struct tipc_node_map *nm_diff)
8883e22e62bSAllan Stephens {
8893e22e62bSAllan Stephens 	int stop = ARRAY_SIZE(nm_a->map);
8903e22e62bSAllan Stephens 	int w;
8913e22e62bSAllan Stephens 	int b;
8923e22e62bSAllan Stephens 	u32 map;
8933e22e62bSAllan Stephens 
8943e22e62bSAllan Stephens 	memset(nm_diff, 0, sizeof(*nm_diff));
8953e22e62bSAllan Stephens 	for (w = 0; w < stop; w++) {
8963e22e62bSAllan Stephens 		map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
8973e22e62bSAllan Stephens 		nm_diff->map[w] = map;
8983e22e62bSAllan Stephens 		if (map != 0) {
8993e22e62bSAllan Stephens 			for (b = 0 ; b < WSIZE; b++) {
9003e22e62bSAllan Stephens 				if (map & (1 << b))
9013e22e62bSAllan Stephens 					nm_diff->count++;
9023e22e62bSAllan Stephens 			}
9033e22e62bSAllan Stephens 		}
9043e22e62bSAllan Stephens 	}
9053e22e62bSAllan Stephens }
90643608edcSAllan Stephens 
90743608edcSAllan Stephens /**
90843608edcSAllan Stephens  * tipc_port_list_add - add a port to a port list, ensuring no duplicates
90943608edcSAllan Stephens  */
9104584310bSPaul Gortmaker void tipc_port_list_add(struct tipc_port_list *pl_ptr, u32 port)
91143608edcSAllan Stephens {
9124584310bSPaul Gortmaker 	struct tipc_port_list *item = pl_ptr;
91343608edcSAllan Stephens 	int i;
91443608edcSAllan Stephens 	int item_sz = PLSIZE;
91543608edcSAllan Stephens 	int cnt = pl_ptr->count;
91643608edcSAllan Stephens 
91743608edcSAllan Stephens 	for (; ; cnt -= item_sz, item = item->next) {
91843608edcSAllan Stephens 		if (cnt < PLSIZE)
91943608edcSAllan Stephens 			item_sz = cnt;
92043608edcSAllan Stephens 		for (i = 0; i < item_sz; i++)
92143608edcSAllan Stephens 			if (item->ports[i] == port)
92243608edcSAllan Stephens 				return;
92343608edcSAllan Stephens 		if (i < PLSIZE) {
92443608edcSAllan Stephens 			item->ports[i] = port;
92543608edcSAllan Stephens 			pl_ptr->count++;
92643608edcSAllan Stephens 			return;
92743608edcSAllan Stephens 		}
92843608edcSAllan Stephens 		if (!item->next) {
92943608edcSAllan Stephens 			item->next = kmalloc(sizeof(*item), GFP_ATOMIC);
93043608edcSAllan Stephens 			if (!item->next) {
9312cf8aa19SErik Hugne 				pr_warn("Incomplete multicast delivery, no memory\n");
93243608edcSAllan Stephens 				return;
93343608edcSAllan Stephens 			}
93443608edcSAllan Stephens 			item->next->next = NULL;
93543608edcSAllan Stephens 		}
93643608edcSAllan Stephens 	}
93743608edcSAllan Stephens }
93843608edcSAllan Stephens 
93943608edcSAllan Stephens /**
94043608edcSAllan Stephens  * tipc_port_list_free - free dynamically created entries in port_list chain
94143608edcSAllan Stephens  *
94243608edcSAllan Stephens  */
9434584310bSPaul Gortmaker void tipc_port_list_free(struct tipc_port_list *pl_ptr)
94443608edcSAllan Stephens {
9454584310bSPaul Gortmaker 	struct tipc_port_list *item;
9464584310bSPaul Gortmaker 	struct tipc_port_list *next;
94743608edcSAllan Stephens 
94843608edcSAllan Stephens 	for (item = pl_ptr->next; item; item = next) {
94943608edcSAllan Stephens 		next = item->next;
95043608edcSAllan Stephens 		kfree(item);
95143608edcSAllan Stephens 	}
95243608edcSAllan Stephens }
953