xref: /openbmc/linux/net/tipc/bcast.c (revision b952b2be)
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);
1111da46568SYing Xue 	tipc_bclink_unlock(net);
112cd3decdfSAllan Stephens }
113b97bf3fdSPer Liden 
1141da46568SYing Xue static void bclink_set_last_sent(struct net *net)
1155b1f7bdeSAllan Stephens {
1161da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1171da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
11805dcc5aaSJon Paul Maloy 	struct sk_buff *skb = skb_peek(&bcl->backlogq);
1191da46568SYing Xue 
12005dcc5aaSJon Paul Maloy 	if (skb)
12105dcc5aaSJon Paul Maloy 		bcl->fsm_msg_cnt = mod(buf_seqno(skb) - 1);
1225b1f7bdeSAllan Stephens 	else
1235b1f7bdeSAllan Stephens 		bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
1245b1f7bdeSAllan Stephens }
1255b1f7bdeSAllan Stephens 
1261da46568SYing Xue u32 tipc_bclink_get_last_sent(struct net *net)
1275b1f7bdeSAllan Stephens {
1281da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1291da46568SYing Xue 
1301da46568SYing Xue 	return tn->bcl->fsm_msg_cnt;
1315b1f7bdeSAllan Stephens }
1325b1f7bdeSAllan Stephens 
1337a54d4a9SAllan Stephens static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
134b97bf3fdSPer Liden {
1357a54d4a9SAllan Stephens 	node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
1367a54d4a9SAllan Stephens 						seqno : node->bclink.last_sent;
137b97bf3fdSPer Liden }
138b97bf3fdSPer Liden 
1392c53040fSBen Hutchings /**
14001d83eddSAllan Stephens  * tipc_bclink_retransmit_to - get most recent node to request retransmission
14101d83eddSAllan Stephens  *
142d69afc90SYing Xue  * Called with bclink_lock locked
14301d83eddSAllan Stephens  */
1441da46568SYing Xue struct tipc_node *tipc_bclink_retransmit_to(struct net *net)
14501d83eddSAllan Stephens {
1461da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1471da46568SYing Xue 
1481da46568SYing Xue 	return tn->bclink->retransmit_to;
14901d83eddSAllan Stephens }
15001d83eddSAllan Stephens 
15101d83eddSAllan Stephens /**
152b97bf3fdSPer Liden  * bclink_retransmit_pkt - retransmit broadcast packets
153b97bf3fdSPer Liden  * @after: sequence number of last packet to *not* retransmit
154b97bf3fdSPer Liden  * @to: sequence number of last packet to retransmit
155b97bf3fdSPer Liden  *
156d69afc90SYing Xue  * Called with bclink_lock locked
157b97bf3fdSPer Liden  */
1581da46568SYing Xue static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
159b97bf3fdSPer Liden {
16058dc55f2SYing Xue 	struct sk_buff *skb;
1611da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
162b97bf3fdSPer Liden 
16305dcc5aaSJon Paul Maloy 	skb_queue_walk(&bcl->transmq, skb) {
164703068eeSJon Maloy 		if (more(buf_seqno(skb), after)) {
165703068eeSJon Maloy 			tipc_link_retransmit(bcl, skb, mod(to - after));
16658dc55f2SYing Xue 			break;
16758dc55f2SYing Xue 		}
168703068eeSJon Maloy 	}
169b97bf3fdSPer Liden }
170b97bf3fdSPer Liden 
171b97bf3fdSPer Liden /**
172908344cdSJon Maloy  * tipc_bclink_wakeup_users - wake up pending users
173908344cdSJon Maloy  *
174908344cdSJon Maloy  * Called with no locks taken
175908344cdSJon Maloy  */
176f2f9800dSYing Xue void tipc_bclink_wakeup_users(struct net *net)
177908344cdSJon Maloy {
1781da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1794988bb4aSErik Hugne 
180c637c103SJon Paul Maloy 	tipc_sk_rcv(net, &tn->bclink->link.wakeupq);
181908344cdSJon Maloy }
182908344cdSJon Maloy 
183908344cdSJon Maloy /**
1844323add6SPer Liden  * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
185b97bf3fdSPer Liden  * @n_ptr: node that sent acknowledgement info
186b97bf3fdSPer Liden  * @acked: broadcast sequence # that has been acknowledged
187b97bf3fdSPer Liden  *
188d69afc90SYing Xue  * Node is locked, bclink_lock unlocked.
189b97bf3fdSPer Liden  */
1906c00055aSDavid S. Miller void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
191b97bf3fdSPer Liden {
19258dc55f2SYing Xue 	struct sk_buff *skb, *tmp;
193b97bf3fdSPer Liden 	unsigned int released = 0;
1941da46568SYing Xue 	struct net *net = n_ptr->net;
1951da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
196b97bf3fdSPer Liden 
1972cdf3918SJon Paul Maloy 	if (unlikely(!n_ptr->bclink.recv_permitted))
1982cdf3918SJon Paul Maloy 		return;
1992cdf3918SJon Paul Maloy 
2001da46568SYing Xue 	tipc_bclink_lock(net);
2012cdf3918SJon Paul Maloy 
20236559591SAllan Stephens 	/* Bail out if tx queue is empty (no clean up is required) */
20305dcc5aaSJon Paul Maloy 	skb = skb_peek(&tn->bcl->transmq);
20458dc55f2SYing Xue 	if (!skb)
20536559591SAllan Stephens 		goto exit;
20636559591SAllan Stephens 
20736559591SAllan Stephens 	/* Determine which messages need to be acknowledged */
20836559591SAllan Stephens 	if (acked == INVALID_LINK_SEQ) {
20936559591SAllan Stephens 		/*
21036559591SAllan Stephens 		 * Contact with specified node has been lost, so need to
21136559591SAllan Stephens 		 * acknowledge sent messages only (if other nodes still exist)
21236559591SAllan Stephens 		 * or both sent and unsent messages (otherwise)
21336559591SAllan Stephens 		 */
2141da46568SYing Xue 		if (tn->bclink->bcast_nodes.count)
2151da46568SYing Xue 			acked = tn->bcl->fsm_msg_cnt;
21636559591SAllan Stephens 		else
2171da46568SYing Xue 			acked = tn->bcl->next_out_no;
21836559591SAllan Stephens 	} else {
21936559591SAllan Stephens 		/*
22036559591SAllan Stephens 		 * Bail out if specified sequence number does not correspond
22136559591SAllan Stephens 		 * to a message that has been sent and not yet acknowledged
22236559591SAllan Stephens 		 */
22358dc55f2SYing Xue 		if (less(acked, buf_seqno(skb)) ||
2241da46568SYing Xue 		    less(tn->bcl->fsm_msg_cnt, acked) ||
22536559591SAllan Stephens 		    less_eq(acked, n_ptr->bclink.acked))
22636559591SAllan Stephens 			goto exit;
22736559591SAllan Stephens 	}
22836559591SAllan Stephens 
22936559591SAllan Stephens 	/* Skip over packets that node has previously acknowledged */
23005dcc5aaSJon Paul Maloy 	skb_queue_walk(&tn->bcl->transmq, skb) {
23158dc55f2SYing Xue 		if (more(buf_seqno(skb), n_ptr->bclink.acked))
23258dc55f2SYing Xue 			break;
23358dc55f2SYing Xue 	}
234b97bf3fdSPer Liden 
235b97bf3fdSPer Liden 	/* Update packets that node is now acknowledging */
23605dcc5aaSJon Paul Maloy 	skb_queue_walk_from_safe(&tn->bcl->transmq, skb, tmp) {
23758dc55f2SYing Xue 		if (more(buf_seqno(skb), acked))
23858dc55f2SYing Xue 			break;
23958dc55f2SYing Xue 		bcbuf_decr_acks(skb);
2401da46568SYing Xue 		bclink_set_last_sent(net);
24158dc55f2SYing Xue 		if (bcbuf_acks(skb) == 0) {
24205dcc5aaSJon Paul Maloy 			__skb_unlink(skb, &tn->bcl->transmq);
24358dc55f2SYing Xue 			kfree_skb(skb);
244b97bf3fdSPer Liden 			released = 1;
245b97bf3fdSPer Liden 		}
246b97bf3fdSPer Liden 	}
247b97bf3fdSPer Liden 	n_ptr->bclink.acked = acked;
248b97bf3fdSPer Liden 
249b97bf3fdSPer Liden 	/* Try resolving broadcast link congestion, if necessary */
25005dcc5aaSJon Paul Maloy 	if (unlikely(skb_peek(&tn->bcl->backlogq))) {
2511da46568SYing Xue 		tipc_link_push_packets(tn->bcl);
2521da46568SYing Xue 		bclink_set_last_sent(net);
2535b1f7bdeSAllan Stephens 	}
254c637c103SJon Paul Maloy 	if (unlikely(released && !skb_queue_empty(&tn->bcl->wakeupq)))
255908344cdSJon Maloy 		n_ptr->action_flags |= TIPC_WAKEUP_BCAST_USERS;
25636559591SAllan Stephens exit:
2571da46568SYing Xue 	tipc_bclink_unlock(net);
258b97bf3fdSPer Liden }
259b97bf3fdSPer Liden 
2602c53040fSBen Hutchings /**
2617a54d4a9SAllan Stephens  * tipc_bclink_update_link_state - update broadcast link state
262b97bf3fdSPer Liden  *
2637216cd94SYing Xue  * RCU and node lock set
264b97bf3fdSPer Liden  */
265c5898636SJon Paul Maloy void tipc_bclink_update_link_state(struct tipc_node *n_ptr,
266c93d3baaSYing Xue 				   u32 last_sent)
267b97bf3fdSPer Liden {
268b97bf3fdSPer Liden 	struct sk_buff *buf;
269c5898636SJon Paul Maloy 	struct net *net = n_ptr->net;
270c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
271b97bf3fdSPer Liden 
2727a54d4a9SAllan Stephens 	/* Ignore "stale" link state info */
2737a54d4a9SAllan Stephens 	if (less_eq(last_sent, n_ptr->bclink.last_in))
274b97bf3fdSPer Liden 		return;
275b97bf3fdSPer Liden 
2767a54d4a9SAllan Stephens 	/* Update link synchronization state; quit if in sync */
2777a54d4a9SAllan Stephens 	bclink_update_last_sent(n_ptr, last_sent);
2787a54d4a9SAllan Stephens 
2797a54d4a9SAllan Stephens 	if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
2807a54d4a9SAllan Stephens 		return;
2817a54d4a9SAllan Stephens 
2827a54d4a9SAllan Stephens 	/* Update out-of-sync state; quit if loss is still unconfirmed */
2837a54d4a9SAllan Stephens 	if ((++n_ptr->bclink.oos_state) == 1) {
2847a54d4a9SAllan Stephens 		if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
2857a54d4a9SAllan Stephens 			return;
2867a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
2877a54d4a9SAllan Stephens 	}
2887a54d4a9SAllan Stephens 
2897a54d4a9SAllan Stephens 	/* Don't NACK if one has been recently sent (or seen) */
2907a54d4a9SAllan Stephens 	if (n_ptr->bclink.oos_state & 0x1)
2917a54d4a9SAllan Stephens 		return;
2927a54d4a9SAllan Stephens 
2937a54d4a9SAllan Stephens 	/* Send NACK */
29431e3c3f6Sstephen hemminger 	buf = tipc_buf_acquire(INT_H_SIZE);
295b97bf3fdSPer Liden 	if (buf) {
2967a54d4a9SAllan Stephens 		struct tipc_msg *msg = buf_msg(buf);
29705dcc5aaSJon Paul Maloy 		struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferdq);
298bc6fecd4SYing Xue 		u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
2997a54d4a9SAllan Stephens 
300c5898636SJon Paul Maloy 		tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG,
30175715217SAllan Stephens 			      INT_H_SIZE, n_ptr->addr);
302bf781ecfSAllan Stephens 		msg_set_non_seq(msg, 1);
303c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
3047a54d4a9SAllan Stephens 		msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
3057a54d4a9SAllan Stephens 		msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
306bc6fecd4SYing Xue 		msg_set_bcgap_to(msg, to);
307b97bf3fdSPer Liden 
3081da46568SYing Xue 		tipc_bclink_lock(net);
3097f9f95d9SYing Xue 		tipc_bearer_send(net, MAX_BEARERS, buf, NULL);
3101da46568SYing Xue 		tn->bcl->stats.sent_nacks++;
3111da46568SYing Xue 		tipc_bclink_unlock(net);
3125f6d9123SAllan Stephens 		kfree_skb(buf);
313b97bf3fdSPer Liden 
3147a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
3157a54d4a9SAllan Stephens 	}
3167a54d4a9SAllan Stephens }
3177a54d4a9SAllan Stephens 
3182c53040fSBen Hutchings /**
3197a54d4a9SAllan Stephens  * bclink_peek_nack - monitor retransmission requests sent by other nodes
320b97bf3fdSPer Liden  *
3217a54d4a9SAllan Stephens  * Delay any upcoming NACK by this node if another node has already
3227a54d4a9SAllan Stephens  * requested the first message this node is going to ask for.
323b97bf3fdSPer Liden  */
324f2f9800dSYing Xue static void bclink_peek_nack(struct net *net, struct tipc_msg *msg)
325b97bf3fdSPer Liden {
326f2f9800dSYing Xue 	struct tipc_node *n_ptr = tipc_node_find(net, msg_destnode(msg));
327b97bf3fdSPer Liden 
3287a54d4a9SAllan Stephens 	if (unlikely(!n_ptr))
329b97bf3fdSPer Liden 		return;
3307a54d4a9SAllan Stephens 
3314323add6SPer Liden 	tipc_node_lock(n_ptr);
332b97bf3fdSPer Liden 
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;
337b97bf3fdSPer Liden 
3384323add6SPer Liden 	tipc_node_unlock(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);
361a6ca1094SYing Xue 	if (unlikely(!skb)) {
362a6ca1094SYing Xue 		__skb_queue_purge(list);
363078bec82SJon Paul Maloy 		return -EHOSTUNREACH;
364078bec82SJon Paul Maloy 	}
365cb1b7280SJon Paul Maloy 	/* Broadcast to all nodes */
366078bec82SJon Paul Maloy 	if (likely(bclink)) {
3671da46568SYing Xue 		tipc_bclink_lock(net);
368078bec82SJon Paul Maloy 		if (likely(bclink->bcast_nodes.count)) {
3697f9f95d9SYing Xue 			rc = __tipc_link_xmit(net, bcl, list);
370078bec82SJon Paul Maloy 			if (likely(!rc)) {
37105dcc5aaSJon Paul Maloy 				u32 len = skb_queue_len(&bcl->transmq);
37258dc55f2SYing Xue 
3731da46568SYing Xue 				bclink_set_last_sent(net);
374078bec82SJon Paul Maloy 				bcl->stats.queue_sz_counts++;
37558dc55f2SYing Xue 				bcl->stats.accu_queue_sz += len;
376078bec82SJon Paul Maloy 			}
377078bec82SJon Paul Maloy 			bc = 1;
378078bec82SJon Paul Maloy 		}
3791da46568SYing Xue 		tipc_bclink_unlock(net);
380078bec82SJon Paul Maloy 	}
381078bec82SJon Paul Maloy 
382078bec82SJon Paul Maloy 	if (unlikely(!bc))
383a6ca1094SYing Xue 		__skb_queue_purge(list);
384078bec82SJon Paul Maloy 
385cb1b7280SJon Paul Maloy 	if (unlikely(rc)) {
386a6ca1094SYing Xue 		kfree_skb(skb);
387cb1b7280SJon Paul Maloy 		return rc;
388cb1b7280SJon Paul Maloy 	}
389cb1b7280SJon Paul Maloy 	/* Deliver message clone */
390cb1b7280SJon Paul Maloy 	__skb_queue_head_init(&arrvq);
391cb1b7280SJon Paul Maloy 	skb_queue_head_init(&inputq);
392cb1b7280SJon Paul Maloy 	__skb_queue_tail(&arrvq, skb);
393cb1b7280SJon Paul Maloy 	tipc_sk_mcast_rcv(net, &arrvq, &inputq);
394078bec82SJon Paul Maloy 	return rc;
395078bec82SJon Paul Maloy }
396078bec82SJon Paul Maloy 
3972c53040fSBen Hutchings /**
39863e7f1acSAllan Stephens  * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
39963e7f1acSAllan Stephens  *
400d69afc90SYing Xue  * Called with both sending node's lock and bclink_lock taken.
40163e7f1acSAllan Stephens  */
40263e7f1acSAllan Stephens static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
40363e7f1acSAllan Stephens {
4041da46568SYing Xue 	struct tipc_net *tn = net_generic(node->net, tipc_net_id);
4051da46568SYing Xue 
40663e7f1acSAllan Stephens 	bclink_update_last_sent(node, seqno);
40763e7f1acSAllan Stephens 	node->bclink.last_in = seqno;
40863e7f1acSAllan Stephens 	node->bclink.oos_state = 0;
4091da46568SYing Xue 	tn->bcl->stats.recv_info++;
41063e7f1acSAllan Stephens 
41163e7f1acSAllan Stephens 	/*
41263e7f1acSAllan Stephens 	 * Unicast an ACK periodically, ensuring that
41363e7f1acSAllan Stephens 	 * all nodes in the cluster don't ACK at the same time
41463e7f1acSAllan Stephens 	 */
41534747539SYing Xue 	if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
416247f0f3cSYing Xue 		tipc_link_proto_xmit(node->active_links[node->addr & 1],
41763e7f1acSAllan Stephens 				     STATE_MSG, 0, 0, 0, 0, 0);
4181da46568SYing Xue 		tn->bcl->stats.sent_acks++;
41963e7f1acSAllan Stephens 	}
42063e7f1acSAllan Stephens }
42163e7f1acSAllan Stephens 
4222c53040fSBen Hutchings /**
423247f0f3cSYing Xue  * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
424b97bf3fdSPer Liden  *
4257216cd94SYing Xue  * RCU is locked, no other locks set
426b97bf3fdSPer Liden  */
427c93d3baaSYing Xue void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
428b97bf3fdSPer Liden {
429c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
4301da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
431b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
4325d3c488dSAllan Stephens 	struct tipc_node *node;
433b97bf3fdSPer Liden 	u32 next_in;
434b97bf3fdSPer Liden 	u32 seqno;
4350abd8ff2SJon Paul Maloy 	int deferred = 0;
436c637c103SJon Paul Maloy 	int pos = 0;
437c637c103SJon Paul Maloy 	struct sk_buff *iskb;
438cb1b7280SJon Paul Maloy 	struct sk_buff_head *arrvq, *inputq;
439b97bf3fdSPer Liden 
4405d3c488dSAllan Stephens 	/* Screen out unwanted broadcast messages */
441c93d3baaSYing Xue 	if (msg_mc_netid(msg) != tn->net_id)
4425d3c488dSAllan Stephens 		goto exit;
4435d3c488dSAllan Stephens 
444f2f9800dSYing Xue 	node = tipc_node_find(net, msg_prevnode(msg));
4455d3c488dSAllan Stephens 	if (unlikely(!node))
4465d3c488dSAllan Stephens 		goto exit;
4475d3c488dSAllan Stephens 
4485d3c488dSAllan Stephens 	tipc_node_lock(node);
449389dd9bcSYing Xue 	if (unlikely(!node->bclink.recv_permitted))
4505d3c488dSAllan Stephens 		goto unlock;
451b97bf3fdSPer Liden 
4528a275a6aSAllan Stephens 	/* Handle broadcast protocol message */
453b97bf3fdSPer Liden 	if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
4549f6bdcd4SAllan Stephens 		if (msg_type(msg) != STATE_MSG)
4559f6bdcd4SAllan Stephens 			goto unlock;
45634747539SYing Xue 		if (msg_destnode(msg) == tn->own_addr) {
4574323add6SPer Liden 			tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
4581da46568SYing Xue 			tipc_bclink_lock(net);
459b97bf3fdSPer Liden 			bcl->stats.recv_nacks++;
4601da46568SYing Xue 			tn->bclink->retransmit_to = node;
4611da46568SYing Xue 			bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
462b97bf3fdSPer Liden 					      msg_bcgap_to(msg));
4631da46568SYing Xue 			tipc_bclink_unlock(net);
464*b952b2beSYing Xue 			tipc_node_unlock(node);
465b97bf3fdSPer Liden 		} else {
4665d3c488dSAllan Stephens 			tipc_node_unlock(node);
467f2f9800dSYing Xue 			bclink_peek_nack(net, msg);
468b97bf3fdSPer Liden 		}
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);
5735d3c488dSAllan Stephens exit:
5745f6d9123SAllan Stephens 	kfree_skb(buf);
575b97bf3fdSPer Liden }
576b97bf3fdSPer Liden 
5776c00055aSDavid S. Miller u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
578b97bf3fdSPer Liden {
579389dd9bcSYing Xue 	return (n_ptr->bclink.recv_permitted &&
5801da46568SYing Xue 		(tipc_bclink_get_last_sent(n_ptr->net) != n_ptr->bclink.acked));
581b97bf3fdSPer Liden }
582b97bf3fdSPer Liden 
583b97bf3fdSPer Liden 
584b97bf3fdSPer Liden /**
5854323add6SPer Liden  * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
586b97bf3fdSPer Liden  *
5872ff9f924SAllan Stephens  * Send packet over as many bearers as necessary to reach all nodes
5882ff9f924SAllan Stephens  * that have joined the broadcast link.
589b97bf3fdSPer Liden  *
5902ff9f924SAllan Stephens  * Returns 0 (packet sent successfully) under all circumstances,
5912ff9f924SAllan Stephens  * since the broadcast link's pseudo-bearer never blocks
592b97bf3fdSPer Liden  */
5931da46568SYing Xue static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
5941da46568SYing Xue 			      struct tipc_bearer *unused1,
595b97bf3fdSPer Liden 			      struct tipc_media_addr *unused2)
596b97bf3fdSPer Liden {
597b97bf3fdSPer Liden 	int bp_index;
5986f92ee54SJon Paul Maloy 	struct tipc_msg *msg = buf_msg(buf);
599c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6001da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6011da46568SYing Xue 	struct tipc_bclink *bclink = tn->bclink;
602b97bf3fdSPer Liden 
603e6160710SGerlando Falauto 	/* Prepare broadcast link message for reliable transmission,
6042ff9f924SAllan Stephens 	 * if first time trying to send it;
6052ff9f924SAllan Stephens 	 * preparation is skipped for broadcast link protocol messages
6062ff9f924SAllan Stephens 	 * since they are sent in an unreliable manner and don't need it
6072ff9f924SAllan Stephens 	 */
608b97bf3fdSPer Liden 	if (likely(!msg_non_seq(buf_msg(buf)))) {
609cd3decdfSAllan Stephens 		bcbuf_set_acks(buf, bclink->bcast_nodes.count);
61040aecb1bSAllan Stephens 		msg_set_non_seq(msg, 1);
611c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
6121da46568SYing Xue 		tn->bcl->stats.sent_info++;
613cd3decdfSAllan Stephens 		if (WARN_ON(!bclink->bcast_nodes.count)) {
6145e726900SAllan Stephens 			dump_stack();
6155e726900SAllan Stephens 			return 0;
6165e726900SAllan Stephens 		}
617b97bf3fdSPer Liden 	}
618b97bf3fdSPer Liden 
619b97bf3fdSPer Liden 	/* Send buffer over bearers until all targets reached */
620cd3decdfSAllan Stephens 	bcbearer->remains = bclink->bcast_nodes;
621b97bf3fdSPer Liden 
622b97bf3fdSPer Liden 	for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
6232d627b92SAllan Stephens 		struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
6242d627b92SAllan Stephens 		struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
6256f92ee54SJon Paul Maloy 		struct tipc_bearer *bp[2] = {p, s};
6266f92ee54SJon Paul Maloy 		struct tipc_bearer *b = bp[msg_link_selector(msg)];
627488fc9afSGerlando Falauto 		struct sk_buff *tbuf;
628b97bf3fdSPer Liden 
629b97bf3fdSPer Liden 		if (!p)
630e6160710SGerlando Falauto 			break; /* No more bearers to try */
6316f92ee54SJon Paul Maloy 		if (!b)
6326f92ee54SJon Paul Maloy 			b = p;
63377861d9cSGerlando Falauto 		tipc_nmap_diff(&bcbearer->remains, &b->nodes,
634e6160710SGerlando Falauto 			       &bcbearer->remains_new);
63565f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == bcbearer->remains.count)
636e6160710SGerlando Falauto 			continue; /* Nothing added by bearer pair */
637b97bf3fdSPer Liden 
638488fc9afSGerlando Falauto 		if (bp_index == 0) {
639488fc9afSGerlando Falauto 			/* Use original buffer for first bearer */
6407f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, buf, &b->bcast_addr);
641488fc9afSGerlando Falauto 		} else {
642488fc9afSGerlando Falauto 			/* Avoid concurrent buffer access */
643bad93e9dSOctavian Purdila 			tbuf = pskb_copy_for_clone(buf, GFP_ATOMIC);
644488fc9afSGerlando Falauto 			if (!tbuf)
645488fc9afSGerlando Falauto 				break;
6467f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, tbuf,
6477f9f95d9SYing Xue 					 &b->bcast_addr);
648488fc9afSGerlando Falauto 			kfree_skb(tbuf); /* Bearer keeps a clone */
649488fc9afSGerlando Falauto 		}
65065f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == 0)
651e6160710SGerlando Falauto 			break; /* All targets reached */
652b97bf3fdSPer Liden 
65365f51ef0SAllan Stephens 		bcbearer->remains = bcbearer->remains_new;
654b97bf3fdSPer Liden 	}
655b97bf3fdSPer Liden 
6562ff9f924SAllan Stephens 	return 0;
657b97bf3fdSPer Liden }
658b97bf3fdSPer Liden 
659b97bf3fdSPer Liden /**
6604323add6SPer Liden  * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
661b97bf3fdSPer Liden  */
6627f9f95d9SYing Xue void tipc_bcbearer_sort(struct net *net, struct tipc_node_map *nm_ptr,
6637f9f95d9SYing Xue 			u32 node, bool action)
664b97bf3fdSPer Liden {
6657f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6661da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6677f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
6687f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_curr;
669f8322dfcSYing Xue 	struct tipc_bearer *b;
670b97bf3fdSPer Liden 	int b_index;
671b97bf3fdSPer Liden 	int pri;
672b97bf3fdSPer Liden 
6731da46568SYing Xue 	tipc_bclink_lock(net);
674b97bf3fdSPer Liden 
67528dd9418SYing Xue 	if (action)
67628dd9418SYing Xue 		tipc_nmap_add(nm_ptr, node);
67728dd9418SYing Xue 	else
67828dd9418SYing Xue 		tipc_nmap_remove(nm_ptr, node);
67928dd9418SYing Xue 
680b97bf3fdSPer Liden 	/* Group bearers by priority (can assume max of two per priority) */
681b97bf3fdSPer Liden 	memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
682b97bf3fdSPer Liden 
683f8322dfcSYing Xue 	rcu_read_lock();
684b97bf3fdSPer Liden 	for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
6857f9f95d9SYing Xue 		b = rcu_dereference_rtnl(tn->bearer_list[b_index]);
686f47de12bSYing Xue 		if (!b || !b->nodes.count)
687b97bf3fdSPer Liden 			continue;
688b97bf3fdSPer Liden 
689b97bf3fdSPer Liden 		if (!bp_temp[b->priority].primary)
690b97bf3fdSPer Liden 			bp_temp[b->priority].primary = b;
691b97bf3fdSPer Liden 		else
692b97bf3fdSPer Liden 			bp_temp[b->priority].secondary = b;
693b97bf3fdSPer Liden 	}
694f8322dfcSYing Xue 	rcu_read_unlock();
695b97bf3fdSPer Liden 
696b97bf3fdSPer Liden 	/* Create array of bearer pairs for broadcasting */
697b97bf3fdSPer Liden 	bp_curr = bcbearer->bpairs;
698b97bf3fdSPer Liden 	memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
699b97bf3fdSPer Liden 
70016cb4b33SPer Liden 	for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
701b97bf3fdSPer Liden 
702b97bf3fdSPer Liden 		if (!bp_temp[pri].primary)
703b97bf3fdSPer Liden 			continue;
704b97bf3fdSPer Liden 
705b97bf3fdSPer Liden 		bp_curr->primary = bp_temp[pri].primary;
706b97bf3fdSPer Liden 
707b97bf3fdSPer Liden 		if (bp_temp[pri].secondary) {
7084323add6SPer Liden 			if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
709b97bf3fdSPer Liden 					    &bp_temp[pri].secondary->nodes)) {
710b97bf3fdSPer Liden 				bp_curr->secondary = bp_temp[pri].secondary;
711b97bf3fdSPer Liden 			} else {
712b97bf3fdSPer Liden 				bp_curr++;
713b97bf3fdSPer Liden 				bp_curr->primary = bp_temp[pri].secondary;
714b97bf3fdSPer Liden 			}
715b97bf3fdSPer Liden 		}
716b97bf3fdSPer Liden 
717b97bf3fdSPer Liden 		bp_curr++;
718b97bf3fdSPer Liden 	}
719b97bf3fdSPer Liden 
7201da46568SYing Xue 	tipc_bclink_unlock(net);
721b97bf3fdSPer Liden }
722b97bf3fdSPer Liden 
723d8182804SRichard Alpe static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
724d8182804SRichard Alpe 				      struct tipc_stats *stats)
7257be57fc6SRichard Alpe {
7267be57fc6SRichard Alpe 	int i;
7277be57fc6SRichard Alpe 	struct nlattr *nest;
7287be57fc6SRichard Alpe 
7297be57fc6SRichard Alpe 	struct nla_map {
7307be57fc6SRichard Alpe 		__u32 key;
7317be57fc6SRichard Alpe 		__u32 val;
7327be57fc6SRichard Alpe 	};
7337be57fc6SRichard Alpe 
7347be57fc6SRichard Alpe 	struct nla_map map[] = {
7357be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_INFO, stats->recv_info},
7367be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
7377be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
7387be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
7397be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
7407be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_INFO, stats->sent_info},
7417be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
7427be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
7437be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
7447be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
7457be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
7467be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
7477be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
7487be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
7497be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
7507be57fc6SRichard Alpe 		{TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
7517be57fc6SRichard Alpe 		{TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
7527be57fc6SRichard Alpe 		{TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
7537be57fc6SRichard Alpe 		{TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
7547be57fc6SRichard Alpe 			(stats->accu_queue_sz / stats->queue_sz_counts) : 0}
7557be57fc6SRichard Alpe 	};
7567be57fc6SRichard Alpe 
7577be57fc6SRichard Alpe 	nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
7587be57fc6SRichard Alpe 	if (!nest)
7597be57fc6SRichard Alpe 		return -EMSGSIZE;
7607be57fc6SRichard Alpe 
7617be57fc6SRichard Alpe 	for (i = 0; i <  ARRAY_SIZE(map); i++)
7627be57fc6SRichard Alpe 		if (nla_put_u32(skb, map[i].key, map[i].val))
7637be57fc6SRichard Alpe 			goto msg_full;
7647be57fc6SRichard Alpe 
7657be57fc6SRichard Alpe 	nla_nest_end(skb, nest);
7667be57fc6SRichard Alpe 
7677be57fc6SRichard Alpe 	return 0;
7687be57fc6SRichard Alpe msg_full:
7697be57fc6SRichard Alpe 	nla_nest_cancel(skb, nest);
7707be57fc6SRichard Alpe 
7717be57fc6SRichard Alpe 	return -EMSGSIZE;
7727be57fc6SRichard Alpe }
7737be57fc6SRichard Alpe 
7741da46568SYing Xue int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
7757be57fc6SRichard Alpe {
7767be57fc6SRichard Alpe 	int err;
7777be57fc6SRichard Alpe 	void *hdr;
7787be57fc6SRichard Alpe 	struct nlattr *attrs;
7797be57fc6SRichard Alpe 	struct nlattr *prop;
7801da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
7811da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
7827be57fc6SRichard Alpe 
7837be57fc6SRichard Alpe 	if (!bcl)
7847be57fc6SRichard Alpe 		return 0;
7857be57fc6SRichard Alpe 
7861da46568SYing Xue 	tipc_bclink_lock(net);
7877be57fc6SRichard Alpe 
788bfb3e5ddSRichard Alpe 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
7897be57fc6SRichard Alpe 			  NLM_F_MULTI, TIPC_NL_LINK_GET);
7907be57fc6SRichard Alpe 	if (!hdr)
7917be57fc6SRichard Alpe 		return -EMSGSIZE;
7927be57fc6SRichard Alpe 
7937be57fc6SRichard Alpe 	attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
7947be57fc6SRichard Alpe 	if (!attrs)
7957be57fc6SRichard Alpe 		goto msg_full;
7967be57fc6SRichard Alpe 
7977be57fc6SRichard Alpe 	/* The broadcast link is always up */
7987be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
7997be57fc6SRichard Alpe 		goto attr_msg_full;
8007be57fc6SRichard Alpe 
8017be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
8027be57fc6SRichard Alpe 		goto attr_msg_full;
8037be57fc6SRichard Alpe 	if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
8047be57fc6SRichard Alpe 		goto attr_msg_full;
8057be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->next_in_no))
8067be57fc6SRichard Alpe 		goto attr_msg_full;
8077be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->next_out_no))
8087be57fc6SRichard Alpe 		goto attr_msg_full;
8097be57fc6SRichard Alpe 
8107be57fc6SRichard Alpe 	prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
8117be57fc6SRichard Alpe 	if (!prop)
8127be57fc6SRichard Alpe 		goto attr_msg_full;
8131f66d161SJon Paul Maloy 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
8147be57fc6SRichard Alpe 		goto prop_msg_full;
8157be57fc6SRichard Alpe 	nla_nest_end(msg->skb, prop);
8167be57fc6SRichard Alpe 
8177be57fc6SRichard Alpe 	err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
8187be57fc6SRichard Alpe 	if (err)
8197be57fc6SRichard Alpe 		goto attr_msg_full;
8207be57fc6SRichard Alpe 
8211da46568SYing Xue 	tipc_bclink_unlock(net);
8227be57fc6SRichard Alpe 	nla_nest_end(msg->skb, attrs);
8237be57fc6SRichard Alpe 	genlmsg_end(msg->skb, hdr);
8247be57fc6SRichard Alpe 
8257be57fc6SRichard Alpe 	return 0;
8267be57fc6SRichard Alpe 
8277be57fc6SRichard Alpe prop_msg_full:
8287be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, prop);
8297be57fc6SRichard Alpe attr_msg_full:
8307be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, attrs);
8317be57fc6SRichard Alpe msg_full:
8321da46568SYing Xue 	tipc_bclink_unlock(net);
8337be57fc6SRichard Alpe 	genlmsg_cancel(msg->skb, hdr);
8347be57fc6SRichard Alpe 
8357be57fc6SRichard Alpe 	return -EMSGSIZE;
8367be57fc6SRichard Alpe }
837b97bf3fdSPer Liden 
8381da46568SYing Xue int tipc_bclink_reset_stats(struct net *net)
839b97bf3fdSPer Liden {
8401da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8411da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8421da46568SYing Xue 
843b97bf3fdSPer Liden 	if (!bcl)
844b97bf3fdSPer Liden 		return -ENOPROTOOPT;
845b97bf3fdSPer Liden 
8461da46568SYing Xue 	tipc_bclink_lock(net);
847b97bf3fdSPer Liden 	memset(&bcl->stats, 0, sizeof(bcl->stats));
8481da46568SYing Xue 	tipc_bclink_unlock(net);
8490e35fd5eSAllan Stephens 	return 0;
850b97bf3fdSPer Liden }
851b97bf3fdSPer Liden 
8521da46568SYing Xue int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
853b97bf3fdSPer Liden {
8541da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8551da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8561da46568SYing Xue 
857b97bf3fdSPer Liden 	if (!bcl)
858b97bf3fdSPer Liden 		return -ENOPROTOOPT;
859b97bf3fdSPer Liden 	if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
860b97bf3fdSPer Liden 		return -EINVAL;
861b97bf3fdSPer Liden 
8621da46568SYing Xue 	tipc_bclink_lock(net);
8634323add6SPer Liden 	tipc_link_set_queue_limits(bcl, limit);
8641da46568SYing Xue 	tipc_bclink_unlock(net);
8650e35fd5eSAllan Stephens 	return 0;
866b97bf3fdSPer Liden }
867b97bf3fdSPer Liden 
8687f9f95d9SYing Xue int tipc_bclink_init(struct net *net)
869b97bf3fdSPer Liden {
8707f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8711da46568SYing Xue 	struct tipc_bcbearer *bcbearer;
8721da46568SYing Xue 	struct tipc_bclink *bclink;
8731da46568SYing Xue 	struct tipc_link *bcl;
8747f9f95d9SYing Xue 
875eb8b00f5SYing Xue 	bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
876eb8b00f5SYing Xue 	if (!bcbearer)
877eb8b00f5SYing Xue 		return -ENOMEM;
878eb8b00f5SYing Xue 
879eb8b00f5SYing Xue 	bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
880eb8b00f5SYing Xue 	if (!bclink) {
881eb8b00f5SYing Xue 		kfree(bcbearer);
882eb8b00f5SYing Xue 		return -ENOMEM;
883eb8b00f5SYing Xue 	}
884eb8b00f5SYing Xue 
885eb8b00f5SYing Xue 	bcl = &bclink->link;
886b97bf3fdSPer Liden 	bcbearer->bearer.media = &bcbearer->media;
8874323add6SPer Liden 	bcbearer->media.send_msg = tipc_bcbearer_send;
8882e2d9be8SAllan Stephens 	sprintf(bcbearer->media.name, "tipc-broadcast");
889b97bf3fdSPer Liden 
890d69afc90SYing Xue 	spin_lock_init(&bclink->lock);
89105dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->transmq);
89205dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->backlogq);
89305dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->deferdq);
894c637c103SJon Paul Maloy 	skb_queue_head_init(&bcl->wakeupq);
895b97bf3fdSPer Liden 	bcl->next_out_no = 1;
89634af946aSIngo Molnar 	spin_lock_init(&bclink->node.lock);
897cb1b7280SJon Paul Maloy 	__skb_queue_head_init(&bclink->arrvq);
898cb1b7280SJon Paul Maloy 	skb_queue_head_init(&bclink->inputq);
899b97bf3fdSPer Liden 	bcl->owner = &bclink->node;
9001da46568SYing Xue 	bcl->owner->net = net;
901b97bf3fdSPer Liden 	bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
9024323add6SPer Liden 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
9037a2f7d18SYing Xue 	bcl->bearer_id = MAX_BEARERS;
9047f9f95d9SYing Xue 	rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
905b97bf3fdSPer Liden 	bcl->state = WORKING_WORKING;
906c5898636SJon Paul Maloy 	bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
907c5898636SJon Paul Maloy 	msg_set_prevnode(bcl->pmsg, tn->own_addr);
9084b704d59SStephen Hemminger 	strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
9091da46568SYing Xue 	tn->bcbearer = bcbearer;
9101da46568SYing Xue 	tn->bclink = bclink;
9111da46568SYing Xue 	tn->bcl = bcl;
912eb8b00f5SYing Xue 	return 0;
913b97bf3fdSPer Liden }
914b97bf3fdSPer Liden 
9157f9f95d9SYing Xue void tipc_bclink_stop(struct net *net)
916b97bf3fdSPer Liden {
9177f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9187f9f95d9SYing Xue 
9191da46568SYing Xue 	tipc_bclink_lock(net);
9201da46568SYing Xue 	tipc_link_purge_queues(tn->bcl);
9211da46568SYing Xue 	tipc_bclink_unlock(net);
922c47e9b91SAllan Stephens 
9237f9f95d9SYing Xue 	RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
924eb8b00f5SYing Xue 	synchronize_net();
9251da46568SYing Xue 	kfree(tn->bcbearer);
9261da46568SYing Xue 	kfree(tn->bclink);
927b97bf3fdSPer Liden }
928b97bf3fdSPer Liden 
9293e22e62bSAllan Stephens /**
9303e22e62bSAllan Stephens  * tipc_nmap_add - add a node to a node map
9313e22e62bSAllan Stephens  */
93228dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
9333e22e62bSAllan Stephens {
9343e22e62bSAllan Stephens 	int n = tipc_node(node);
9353e22e62bSAllan Stephens 	int w = n / WSIZE;
9363e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
9373e22e62bSAllan Stephens 
9383e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) == 0) {
9393e22e62bSAllan Stephens 		nm_ptr->count++;
9403e22e62bSAllan Stephens 		nm_ptr->map[w] |= mask;
9413e22e62bSAllan Stephens 	}
9423e22e62bSAllan Stephens }
9433e22e62bSAllan Stephens 
9443e22e62bSAllan Stephens /**
9453e22e62bSAllan Stephens  * tipc_nmap_remove - remove a node from a node map
9463e22e62bSAllan Stephens  */
94728dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
9483e22e62bSAllan Stephens {
9493e22e62bSAllan Stephens 	int n = tipc_node(node);
9503e22e62bSAllan Stephens 	int w = n / WSIZE;
9513e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
9523e22e62bSAllan Stephens 
9533e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) != 0) {
9543e22e62bSAllan Stephens 		nm_ptr->map[w] &= ~mask;
9553e22e62bSAllan Stephens 		nm_ptr->count--;
9563e22e62bSAllan Stephens 	}
9573e22e62bSAllan Stephens }
9583e22e62bSAllan Stephens 
9593e22e62bSAllan Stephens /**
9603e22e62bSAllan Stephens  * tipc_nmap_diff - find differences between node maps
9613e22e62bSAllan Stephens  * @nm_a: input node map A
9623e22e62bSAllan Stephens  * @nm_b: input node map B
9633e22e62bSAllan Stephens  * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
9643e22e62bSAllan Stephens  */
96531e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
96631e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
9673e22e62bSAllan Stephens 			   struct tipc_node_map *nm_diff)
9683e22e62bSAllan Stephens {
9693e22e62bSAllan Stephens 	int stop = ARRAY_SIZE(nm_a->map);
9703e22e62bSAllan Stephens 	int w;
9713e22e62bSAllan Stephens 	int b;
9723e22e62bSAllan Stephens 	u32 map;
9733e22e62bSAllan Stephens 
9743e22e62bSAllan Stephens 	memset(nm_diff, 0, sizeof(*nm_diff));
9753e22e62bSAllan Stephens 	for (w = 0; w < stop; w++) {
9763e22e62bSAllan Stephens 		map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
9773e22e62bSAllan Stephens 		nm_diff->map[w] = map;
9783e22e62bSAllan Stephens 		if (map != 0) {
9793e22e62bSAllan Stephens 			for (b = 0 ; b < WSIZE; b++) {
9803e22e62bSAllan Stephens 				if (map & (1 << b))
9813e22e62bSAllan Stephens 					nm_diff->count++;
9823e22e62bSAllan Stephens 			}
9833e22e62bSAllan Stephens 		}
9843e22e62bSAllan Stephens 	}
9853e22e62bSAllan Stephens }
986