xref: /openbmc/linux/net/tipc/bcast.c (revision 3c724acd)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/bcast.c: TIPC broadcast code
3b97bf3fdSPer Liden  *
4*3c724acdSJon 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 	struct tipc_node *node = NULL;
663f5a12bdSYing Xue 
671da46568SYing Xue 	if (likely(!tn->bclink->flags)) {
681da46568SYing Xue 		spin_unlock_bh(&tn->bclink->lock);
693f5a12bdSYing Xue 		return;
703f5a12bdSYing Xue 	}
713f5a12bdSYing Xue 
721da46568SYing Xue 	if (tn->bclink->flags & TIPC_BCLINK_RESET) {
731da46568SYing Xue 		tn->bclink->flags &= ~TIPC_BCLINK_RESET;
741da46568SYing Xue 		node = tipc_bclink_retransmit_to(net);
753f5a12bdSYing Xue 	}
761da46568SYing Xue 	spin_unlock_bh(&tn->bclink->lock);
773f5a12bdSYing Xue 
783f5a12bdSYing Xue 	if (node)
793f5a12bdSYing Xue 		tipc_link_reset_all(node);
803f5a12bdSYing Xue }
813f5a12bdSYing Xue 
82078bec82SJon Paul Maloy uint  tipc_bclink_get_mtu(void)
83078bec82SJon Paul Maloy {
84078bec82SJon Paul Maloy 	return MAX_PKT_DEFAULT_MCAST;
85078bec82SJon Paul Maloy }
86078bec82SJon Paul Maloy 
871da46568SYing Xue void tipc_bclink_set_flags(struct net *net, unsigned int flags)
883f5a12bdSYing Xue {
891da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
901da46568SYing Xue 
911da46568SYing Xue 	tn->bclink->flags |= flags;
92d69afc90SYing Xue }
93d69afc90SYing Xue 
9405790c64SSam Ravnborg static u32 bcbuf_acks(struct sk_buff *buf)
95b97bf3fdSPer Liden {
96880b005fSDavid S. Miller 	return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
97b97bf3fdSPer Liden }
98b97bf3fdSPer Liden 
9905790c64SSam Ravnborg static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
100b97bf3fdSPer Liden {
101880b005fSDavid S. Miller 	TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
102b97bf3fdSPer Liden }
103b97bf3fdSPer Liden 
10405790c64SSam Ravnborg static void bcbuf_decr_acks(struct sk_buff *buf)
105b97bf3fdSPer Liden {
106b97bf3fdSPer Liden 	bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
107b97bf3fdSPer Liden }
108b97bf3fdSPer Liden 
1091da46568SYing Xue void tipc_bclink_add_node(struct net *net, u32 addr)
110cd3decdfSAllan Stephens {
1111da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1121da46568SYing Xue 
1131da46568SYing Xue 	tipc_bclink_lock(net);
1141da46568SYing Xue 	tipc_nmap_add(&tn->bclink->bcast_nodes, addr);
1151da46568SYing Xue 	tipc_bclink_unlock(net);
116cd3decdfSAllan Stephens }
117cd3decdfSAllan Stephens 
1181da46568SYing Xue void tipc_bclink_remove_node(struct net *net, u32 addr)
119cd3decdfSAllan Stephens {
1201da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1211da46568SYing Xue 
1221da46568SYing Xue 	tipc_bclink_lock(net);
1231da46568SYing Xue 	tipc_nmap_remove(&tn->bclink->bcast_nodes, addr);
1241da46568SYing Xue 	tipc_bclink_unlock(net);
125cd3decdfSAllan Stephens }
126b97bf3fdSPer Liden 
1271da46568SYing Xue static void bclink_set_last_sent(struct net *net)
1285b1f7bdeSAllan Stephens {
1291da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1301da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
1311da46568SYing Xue 
1325b1f7bdeSAllan Stephens 	if (bcl->next_out)
1335b1f7bdeSAllan Stephens 		bcl->fsm_msg_cnt = mod(buf_seqno(bcl->next_out) - 1);
1345b1f7bdeSAllan Stephens 	else
1355b1f7bdeSAllan Stephens 		bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
1365b1f7bdeSAllan Stephens }
1375b1f7bdeSAllan Stephens 
1381da46568SYing Xue u32 tipc_bclink_get_last_sent(struct net *net)
1395b1f7bdeSAllan Stephens {
1401da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1411da46568SYing Xue 
1421da46568SYing Xue 	return tn->bcl->fsm_msg_cnt;
1435b1f7bdeSAllan Stephens }
1445b1f7bdeSAllan Stephens 
1457a54d4a9SAllan Stephens static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
146b97bf3fdSPer Liden {
1477a54d4a9SAllan Stephens 	node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
1487a54d4a9SAllan Stephens 						seqno : node->bclink.last_sent;
149b97bf3fdSPer Liden }
150b97bf3fdSPer Liden 
151b97bf3fdSPer Liden 
1522c53040fSBen Hutchings /**
15301d83eddSAllan Stephens  * tipc_bclink_retransmit_to - get most recent node to request retransmission
15401d83eddSAllan Stephens  *
155d69afc90SYing Xue  * Called with bclink_lock locked
15601d83eddSAllan Stephens  */
1571da46568SYing Xue struct tipc_node *tipc_bclink_retransmit_to(struct net *net)
15801d83eddSAllan Stephens {
1591da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1601da46568SYing Xue 
1611da46568SYing Xue 	return tn->bclink->retransmit_to;
16201d83eddSAllan Stephens }
16301d83eddSAllan Stephens 
16401d83eddSAllan Stephens /**
165b97bf3fdSPer Liden  * bclink_retransmit_pkt - retransmit broadcast packets
166b97bf3fdSPer Liden  * @after: sequence number of last packet to *not* retransmit
167b97bf3fdSPer Liden  * @to: sequence number of last packet to retransmit
168b97bf3fdSPer Liden  *
169d69afc90SYing Xue  * Called with bclink_lock locked
170b97bf3fdSPer Liden  */
1711da46568SYing Xue static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
172b97bf3fdSPer Liden {
17358dc55f2SYing Xue 	struct sk_buff *skb;
1741da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
175b97bf3fdSPer Liden 
17658dc55f2SYing Xue 	skb_queue_walk(&bcl->outqueue, skb) {
177703068eeSJon Maloy 		if (more(buf_seqno(skb), after)) {
178703068eeSJon Maloy 			tipc_link_retransmit(bcl, skb, mod(to - after));
17958dc55f2SYing Xue 			break;
18058dc55f2SYing Xue 		}
181703068eeSJon Maloy 	}
182b97bf3fdSPer Liden }
183b97bf3fdSPer Liden 
184b97bf3fdSPer Liden /**
185908344cdSJon Maloy  * tipc_bclink_wakeup_users - wake up pending users
186908344cdSJon Maloy  *
187908344cdSJon Maloy  * Called with no locks taken
188908344cdSJon Maloy  */
189f2f9800dSYing Xue void tipc_bclink_wakeup_users(struct net *net)
190908344cdSJon Maloy {
1911da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1924988bb4aSErik Hugne 
193c637c103SJon Paul Maloy 	tipc_sk_rcv(net, &tn->bclink->link.wakeupq);
194908344cdSJon Maloy }
195908344cdSJon Maloy 
196908344cdSJon Maloy /**
1974323add6SPer Liden  * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
198b97bf3fdSPer Liden  * @n_ptr: node that sent acknowledgement info
199b97bf3fdSPer Liden  * @acked: broadcast sequence # that has been acknowledged
200b97bf3fdSPer Liden  *
201d69afc90SYing Xue  * Node is locked, bclink_lock unlocked.
202b97bf3fdSPer Liden  */
2036c00055aSDavid S. Miller void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
204b97bf3fdSPer Liden {
20558dc55f2SYing Xue 	struct sk_buff *skb, *tmp;
206b97bf3fdSPer Liden 	struct sk_buff *next;
207b97bf3fdSPer Liden 	unsigned int released = 0;
2081da46568SYing Xue 	struct net *net = n_ptr->net;
2091da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
210b97bf3fdSPer Liden 
2111da46568SYing Xue 	tipc_bclink_lock(net);
21236559591SAllan Stephens 	/* Bail out if tx queue is empty (no clean up is required) */
2131da46568SYing Xue 	skb = skb_peek(&tn->bcl->outqueue);
21458dc55f2SYing Xue 	if (!skb)
21536559591SAllan Stephens 		goto exit;
21636559591SAllan Stephens 
21736559591SAllan Stephens 	/* Determine which messages need to be acknowledged */
21836559591SAllan Stephens 	if (acked == INVALID_LINK_SEQ) {
21936559591SAllan Stephens 		/*
22036559591SAllan Stephens 		 * Contact with specified node has been lost, so need to
22136559591SAllan Stephens 		 * acknowledge sent messages only (if other nodes still exist)
22236559591SAllan Stephens 		 * or both sent and unsent messages (otherwise)
22336559591SAllan Stephens 		 */
2241da46568SYing Xue 		if (tn->bclink->bcast_nodes.count)
2251da46568SYing Xue 			acked = tn->bcl->fsm_msg_cnt;
22636559591SAllan Stephens 		else
2271da46568SYing Xue 			acked = tn->bcl->next_out_no;
22836559591SAllan Stephens 	} else {
22936559591SAllan Stephens 		/*
23036559591SAllan Stephens 		 * Bail out if specified sequence number does not correspond
23136559591SAllan Stephens 		 * to a message that has been sent and not yet acknowledged
23236559591SAllan Stephens 		 */
23358dc55f2SYing Xue 		if (less(acked, buf_seqno(skb)) ||
2341da46568SYing Xue 		    less(tn->bcl->fsm_msg_cnt, acked) ||
23536559591SAllan Stephens 		    less_eq(acked, n_ptr->bclink.acked))
23636559591SAllan Stephens 			goto exit;
23736559591SAllan Stephens 	}
23836559591SAllan Stephens 
23936559591SAllan Stephens 	/* Skip over packets that node has previously acknowledged */
2401da46568SYing Xue 	skb_queue_walk(&tn->bcl->outqueue, skb) {
24158dc55f2SYing Xue 		if (more(buf_seqno(skb), n_ptr->bclink.acked))
24258dc55f2SYing Xue 			break;
24358dc55f2SYing Xue 	}
244b97bf3fdSPer Liden 
245b97bf3fdSPer Liden 	/* Update packets that node is now acknowledging */
2461da46568SYing Xue 	skb_queue_walk_from_safe(&tn->bcl->outqueue, skb, tmp) {
24758dc55f2SYing Xue 		if (more(buf_seqno(skb), acked))
24858dc55f2SYing Xue 			break;
249b97bf3fdSPer Liden 
2501da46568SYing Xue 		next = tipc_skb_queue_next(&tn->bcl->outqueue, skb);
2511da46568SYing Xue 		if (skb != tn->bcl->next_out) {
25258dc55f2SYing Xue 			bcbuf_decr_acks(skb);
25358dc55f2SYing Xue 		} else {
25458dc55f2SYing Xue 			bcbuf_set_acks(skb, 0);
2551da46568SYing Xue 			tn->bcl->next_out = next;
2561da46568SYing Xue 			bclink_set_last_sent(net);
25710745cd5SAllan Stephens 		}
25810745cd5SAllan Stephens 
25958dc55f2SYing Xue 		if (bcbuf_acks(skb) == 0) {
2601da46568SYing Xue 			__skb_unlink(skb, &tn->bcl->outqueue);
26158dc55f2SYing Xue 			kfree_skb(skb);
262b97bf3fdSPer Liden 			released = 1;
263b97bf3fdSPer Liden 		}
264b97bf3fdSPer Liden 	}
265b97bf3fdSPer Liden 	n_ptr->bclink.acked = acked;
266b97bf3fdSPer Liden 
267b97bf3fdSPer Liden 	/* Try resolving broadcast link congestion, if necessary */
2681da46568SYing Xue 	if (unlikely(tn->bcl->next_out)) {
2691da46568SYing Xue 		tipc_link_push_packets(tn->bcl);
2701da46568SYing Xue 		bclink_set_last_sent(net);
2715b1f7bdeSAllan Stephens 	}
272c637c103SJon Paul Maloy 	if (unlikely(released && !skb_queue_empty(&tn->bcl->wakeupq)))
273908344cdSJon Maloy 		n_ptr->action_flags |= TIPC_WAKEUP_BCAST_USERS;
27436559591SAllan Stephens exit:
2751da46568SYing Xue 	tipc_bclink_unlock(net);
276b97bf3fdSPer Liden }
277b97bf3fdSPer Liden 
2782c53040fSBen Hutchings /**
2797a54d4a9SAllan Stephens  * tipc_bclink_update_link_state - update broadcast link state
280b97bf3fdSPer Liden  *
2817216cd94SYing Xue  * RCU and node lock set
282b97bf3fdSPer Liden  */
283c5898636SJon Paul Maloy void tipc_bclink_update_link_state(struct tipc_node *n_ptr,
284c93d3baaSYing Xue 				   u32 last_sent)
285b97bf3fdSPer Liden {
286b97bf3fdSPer Liden 	struct sk_buff *buf;
287c5898636SJon Paul Maloy 	struct net *net = n_ptr->net;
288c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
289b97bf3fdSPer Liden 
2907a54d4a9SAllan Stephens 	/* Ignore "stale" link state info */
2917a54d4a9SAllan Stephens 	if (less_eq(last_sent, n_ptr->bclink.last_in))
292b97bf3fdSPer Liden 		return;
293b97bf3fdSPer Liden 
2947a54d4a9SAllan Stephens 	/* Update link synchronization state; quit if in sync */
2957a54d4a9SAllan Stephens 	bclink_update_last_sent(n_ptr, last_sent);
2967a54d4a9SAllan Stephens 
2977a54d4a9SAllan Stephens 	if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
2987a54d4a9SAllan Stephens 		return;
2997a54d4a9SAllan Stephens 
3007a54d4a9SAllan Stephens 	/* Update out-of-sync state; quit if loss is still unconfirmed */
3017a54d4a9SAllan Stephens 	if ((++n_ptr->bclink.oos_state) == 1) {
3027a54d4a9SAllan Stephens 		if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
3037a54d4a9SAllan Stephens 			return;
3047a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
3057a54d4a9SAllan Stephens 	}
3067a54d4a9SAllan Stephens 
3077a54d4a9SAllan Stephens 	/* Don't NACK if one has been recently sent (or seen) */
3087a54d4a9SAllan Stephens 	if (n_ptr->bclink.oos_state & 0x1)
3097a54d4a9SAllan Stephens 		return;
3107a54d4a9SAllan Stephens 
3117a54d4a9SAllan Stephens 	/* Send NACK */
31231e3c3f6Sstephen hemminger 	buf = tipc_buf_acquire(INT_H_SIZE);
313b97bf3fdSPer Liden 	if (buf) {
3147a54d4a9SAllan Stephens 		struct tipc_msg *msg = buf_msg(buf);
315bc6fecd4SYing Xue 		struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferred_queue);
316bc6fecd4SYing Xue 		u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
3177a54d4a9SAllan Stephens 
318c5898636SJon Paul Maloy 		tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG,
31975715217SAllan Stephens 			      INT_H_SIZE, n_ptr->addr);
320bf781ecfSAllan Stephens 		msg_set_non_seq(msg, 1);
321c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
3227a54d4a9SAllan Stephens 		msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
3237a54d4a9SAllan Stephens 		msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
324bc6fecd4SYing Xue 		msg_set_bcgap_to(msg, to);
325b97bf3fdSPer Liden 
3261da46568SYing Xue 		tipc_bclink_lock(net);
3277f9f95d9SYing Xue 		tipc_bearer_send(net, MAX_BEARERS, buf, NULL);
3281da46568SYing Xue 		tn->bcl->stats.sent_nacks++;
3291da46568SYing Xue 		tipc_bclink_unlock(net);
3305f6d9123SAllan Stephens 		kfree_skb(buf);
331b97bf3fdSPer Liden 
3327a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
3337a54d4a9SAllan Stephens 	}
3347a54d4a9SAllan Stephens }
3357a54d4a9SAllan Stephens 
3362c53040fSBen Hutchings /**
3377a54d4a9SAllan Stephens  * bclink_peek_nack - monitor retransmission requests sent by other nodes
338b97bf3fdSPer Liden  *
3397a54d4a9SAllan Stephens  * Delay any upcoming NACK by this node if another node has already
3407a54d4a9SAllan Stephens  * requested the first message this node is going to ask for.
341b97bf3fdSPer Liden  */
342f2f9800dSYing Xue static void bclink_peek_nack(struct net *net, struct tipc_msg *msg)
343b97bf3fdSPer Liden {
344f2f9800dSYing Xue 	struct tipc_node *n_ptr = tipc_node_find(net, msg_destnode(msg));
345b97bf3fdSPer Liden 
3467a54d4a9SAllan Stephens 	if (unlikely(!n_ptr))
347b97bf3fdSPer Liden 		return;
3487a54d4a9SAllan Stephens 
3494323add6SPer Liden 	tipc_node_lock(n_ptr);
350b97bf3fdSPer Liden 
351389dd9bcSYing Xue 	if (n_ptr->bclink.recv_permitted &&
3527a54d4a9SAllan Stephens 	    (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
3537a54d4a9SAllan Stephens 	    (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
3547a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state = 2;
355b97bf3fdSPer Liden 
3564323add6SPer Liden 	tipc_node_unlock(n_ptr);
357b97bf3fdSPer Liden }
358b97bf3fdSPer Liden 
3599fbfb8b1SJon Paul Maloy /* tipc_bclink_xmit - broadcast buffer chain to all nodes in cluster
360078bec82SJon Paul Maloy  *                    and to identified node local sockets
361f2f9800dSYing Xue  * @net: the applicable net namespace
362a6ca1094SYing Xue  * @list: chain of buffers containing message
363078bec82SJon Paul Maloy  * Consumes the buffer chain, except when returning -ELINKCONG
364078bec82SJon Paul Maloy  * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
365078bec82SJon Paul Maloy  */
366f2f9800dSYing Xue int tipc_bclink_xmit(struct net *net, struct sk_buff_head *list)
367078bec82SJon Paul Maloy {
3681da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
3691da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
3701da46568SYing Xue 	struct tipc_bclink *bclink = tn->bclink;
371078bec82SJon Paul Maloy 	int rc = 0;
372078bec82SJon Paul Maloy 	int bc = 0;
373a6ca1094SYing Xue 	struct sk_buff *skb;
374078bec82SJon Paul Maloy 
375078bec82SJon Paul Maloy 	/* Prepare clone of message for local node */
376a6ca1094SYing Xue 	skb = tipc_msg_reassemble(list);
377a6ca1094SYing Xue 	if (unlikely(!skb)) {
378a6ca1094SYing Xue 		__skb_queue_purge(list);
379078bec82SJon Paul Maloy 		return -EHOSTUNREACH;
380078bec82SJon Paul Maloy 	}
381078bec82SJon Paul Maloy 
382078bec82SJon Paul Maloy 	/* Broadcast to all other nodes */
383078bec82SJon Paul Maloy 	if (likely(bclink)) {
3841da46568SYing Xue 		tipc_bclink_lock(net);
385078bec82SJon Paul Maloy 		if (likely(bclink->bcast_nodes.count)) {
3867f9f95d9SYing Xue 			rc = __tipc_link_xmit(net, bcl, list);
387078bec82SJon Paul Maloy 			if (likely(!rc)) {
38858dc55f2SYing Xue 				u32 len = skb_queue_len(&bcl->outqueue);
38958dc55f2SYing Xue 
3901da46568SYing Xue 				bclink_set_last_sent(net);
391078bec82SJon Paul Maloy 				bcl->stats.queue_sz_counts++;
39258dc55f2SYing Xue 				bcl->stats.accu_queue_sz += len;
393078bec82SJon Paul Maloy 			}
394078bec82SJon Paul Maloy 			bc = 1;
395078bec82SJon Paul Maloy 		}
3961da46568SYing Xue 		tipc_bclink_unlock(net);
397078bec82SJon Paul Maloy 	}
398078bec82SJon Paul Maloy 
399078bec82SJon Paul Maloy 	if (unlikely(!bc))
400a6ca1094SYing Xue 		__skb_queue_purge(list);
401078bec82SJon Paul Maloy 
402078bec82SJon Paul Maloy 	/* Deliver message clone */
403078bec82SJon Paul Maloy 	if (likely(!rc))
404f2f9800dSYing Xue 		tipc_sk_mcast_rcv(net, skb);
405078bec82SJon Paul Maloy 	else
406a6ca1094SYing Xue 		kfree_skb(skb);
407078bec82SJon Paul Maloy 
408078bec82SJon Paul Maloy 	return rc;
409078bec82SJon Paul Maloy }
410078bec82SJon Paul Maloy 
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 {
4181da46568SYing Xue 	struct tipc_net *tn = net_generic(node->net, tipc_net_id);
4191da46568SYing Xue 
42063e7f1acSAllan Stephens 	bclink_update_last_sent(node, seqno);
42163e7f1acSAllan Stephens 	node->bclink.last_in = seqno;
42263e7f1acSAllan Stephens 	node->bclink.oos_state = 0;
4231da46568SYing Xue 	tn->bcl->stats.recv_info++;
42463e7f1acSAllan Stephens 
42563e7f1acSAllan Stephens 	/*
42663e7f1acSAllan Stephens 	 * Unicast an ACK periodically, ensuring that
42763e7f1acSAllan Stephens 	 * all nodes in the cluster don't ACK at the same time
42863e7f1acSAllan Stephens 	 */
42934747539SYing Xue 	if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
430247f0f3cSYing Xue 		tipc_link_proto_xmit(node->active_links[node->addr & 1],
43163e7f1acSAllan Stephens 				     STATE_MSG, 0, 0, 0, 0, 0);
4321da46568SYing Xue 		tn->bcl->stats.sent_acks++;
43363e7f1acSAllan Stephens 	}
43463e7f1acSAllan Stephens }
43563e7f1acSAllan Stephens 
4362c53040fSBen Hutchings /**
437247f0f3cSYing Xue  * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
438b97bf3fdSPer Liden  *
4397216cd94SYing Xue  * RCU is locked, no other locks set
440b97bf3fdSPer Liden  */
441c93d3baaSYing Xue void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
442b97bf3fdSPer Liden {
443c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
4441da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
445b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
4465d3c488dSAllan Stephens 	struct tipc_node *node;
447b97bf3fdSPer Liden 	u32 next_in;
448b97bf3fdSPer Liden 	u32 seqno;
4490abd8ff2SJon Paul Maloy 	int deferred = 0;
450c637c103SJon Paul Maloy 	int pos = 0;
451c637c103SJon Paul Maloy 	struct sk_buff *iskb;
452c637c103SJon Paul Maloy 	struct sk_buff_head msgs;
453b97bf3fdSPer Liden 
4545d3c488dSAllan Stephens 	/* Screen out unwanted broadcast messages */
455c93d3baaSYing Xue 	if (msg_mc_netid(msg) != tn->net_id)
4565d3c488dSAllan Stephens 		goto exit;
4575d3c488dSAllan Stephens 
458f2f9800dSYing Xue 	node = tipc_node_find(net, msg_prevnode(msg));
4595d3c488dSAllan Stephens 	if (unlikely(!node))
4605d3c488dSAllan Stephens 		goto exit;
4615d3c488dSAllan Stephens 
4625d3c488dSAllan Stephens 	tipc_node_lock(node);
463389dd9bcSYing Xue 	if (unlikely(!node->bclink.recv_permitted))
4645d3c488dSAllan Stephens 		goto unlock;
465b97bf3fdSPer Liden 
4668a275a6aSAllan Stephens 	/* Handle broadcast protocol message */
467b97bf3fdSPer Liden 	if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
4689f6bdcd4SAllan Stephens 		if (msg_type(msg) != STATE_MSG)
4699f6bdcd4SAllan Stephens 			goto unlock;
47034747539SYing Xue 		if (msg_destnode(msg) == tn->own_addr) {
4714323add6SPer Liden 			tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
4724323add6SPer Liden 			tipc_node_unlock(node);
4731da46568SYing Xue 			tipc_bclink_lock(net);
474b97bf3fdSPer Liden 			bcl->stats.recv_nacks++;
4751da46568SYing Xue 			tn->bclink->retransmit_to = node;
4761da46568SYing Xue 			bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
477b97bf3fdSPer Liden 					      msg_bcgap_to(msg));
4781da46568SYing Xue 			tipc_bclink_unlock(net);
479b97bf3fdSPer Liden 		} else {
4805d3c488dSAllan Stephens 			tipc_node_unlock(node);
481f2f9800dSYing Xue 			bclink_peek_nack(net, msg);
482b97bf3fdSPer Liden 		}
4835d3c488dSAllan Stephens 		goto exit;
484b97bf3fdSPer Liden 	}
485b97bf3fdSPer Liden 
4865d3c488dSAllan Stephens 	/* Handle in-sequence broadcast message */
487b97bf3fdSPer Liden 	seqno = msg_seqno(msg);
4888a275a6aSAllan Stephens 	next_in = mod(node->bclink.last_in + 1);
489b97bf3fdSPer Liden 
490b97bf3fdSPer Liden 	if (likely(seqno == next_in)) {
4918a275a6aSAllan Stephens receive:
4927a54d4a9SAllan Stephens 		/* Deliver message to destination */
493b97bf3fdSPer Liden 		if (likely(msg_isdata(msg))) {
4941da46568SYing Xue 			tipc_bclink_lock(net);
49563e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
4961da46568SYing Xue 			tipc_bclink_unlock(net);
4974323add6SPer Liden 			tipc_node_unlock(node);
4989f6bdcd4SAllan Stephens 			if (likely(msg_mcast(msg)))
499f2f9800dSYing Xue 				tipc_sk_mcast_rcv(net, buf);
5009f6bdcd4SAllan Stephens 			else
5015f6d9123SAllan Stephens 				kfree_skb(buf);
502b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_BUNDLER) {
5031da46568SYing Xue 			tipc_bclink_lock(net);
50463e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
505b97bf3fdSPer Liden 			bcl->stats.recv_bundles++;
506b97bf3fdSPer Liden 			bcl->stats.recv_bundled += msg_msgcnt(msg);
5071da46568SYing Xue 			tipc_bclink_unlock(net);
5084323add6SPer Liden 			tipc_node_unlock(node);
509c637c103SJon Paul Maloy 			while (tipc_msg_extract(buf, &iskb, &pos))
510c637c103SJon Paul Maloy 				tipc_sk_mcast_rcv(net, iskb);
511b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_FRAGMENTER) {
51237e22164SJon Paul Maloy 			tipc_buf_append(&node->bclink.reasm_buf, &buf);
51337e22164SJon Paul Maloy 			if (unlikely(!buf && !node->bclink.reasm_buf))
51463e7f1acSAllan Stephens 				goto unlock;
5151da46568SYing Xue 			tipc_bclink_lock(net);
51663e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
517b97bf3fdSPer Liden 			bcl->stats.recv_fragments++;
51837e22164SJon Paul Maloy 			if (buf) {
519b97bf3fdSPer Liden 				bcl->stats.recv_fragmented++;
52040ba3cdfSErik Hugne 				msg = buf_msg(buf);
5211da46568SYing Xue 				tipc_bclink_unlock(net);
522528f6f4bSErik Hugne 				goto receive;
523528f6f4bSErik Hugne 			}
5241da46568SYing Xue 			tipc_bclink_unlock(net);
5254323add6SPer Liden 			tipc_node_unlock(node);
5269f6bdcd4SAllan Stephens 		} else if (msg_user(msg) == NAME_DISTRIBUTOR) {
5271da46568SYing Xue 			tipc_bclink_lock(net);
52863e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
5291da46568SYing Xue 			tipc_bclink_unlock(net);
5309f6bdcd4SAllan Stephens 			tipc_node_unlock(node);
531c637c103SJon Paul Maloy 			skb_queue_head_init(&msgs);
532c637c103SJon Paul Maloy 			skb_queue_tail(&msgs, buf);
533c637c103SJon Paul Maloy 			tipc_named_rcv(net, &msgs);
534b97bf3fdSPer Liden 		} else {
5351da46568SYing Xue 			tipc_bclink_lock(net);
53663e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
5371da46568SYing Xue 			tipc_bclink_unlock(net);
5384323add6SPer Liden 			tipc_node_unlock(node);
5395f6d9123SAllan Stephens 			kfree_skb(buf);
540b97bf3fdSPer Liden 		}
5415d3c488dSAllan Stephens 		buf = NULL;
5428a275a6aSAllan Stephens 
5438a275a6aSAllan Stephens 		/* Determine new synchronization state */
5444323add6SPer Liden 		tipc_node_lock(node);
5458a275a6aSAllan Stephens 		if (unlikely(!tipc_node_is_up(node)))
5468a275a6aSAllan Stephens 			goto unlock;
5478a275a6aSAllan Stephens 
5487a54d4a9SAllan Stephens 		if (node->bclink.last_in == node->bclink.last_sent)
5498a275a6aSAllan Stephens 			goto unlock;
5508a275a6aSAllan Stephens 
551bc6fecd4SYing Xue 		if (skb_queue_empty(&node->bclink.deferred_queue)) {
5527a54d4a9SAllan Stephens 			node->bclink.oos_state = 1;
5537a54d4a9SAllan Stephens 			goto unlock;
5547a54d4a9SAllan Stephens 		}
5557a54d4a9SAllan Stephens 
556bc6fecd4SYing Xue 		msg = buf_msg(skb_peek(&node->bclink.deferred_queue));
5578a275a6aSAllan Stephens 		seqno = msg_seqno(msg);
5588a275a6aSAllan Stephens 		next_in = mod(next_in + 1);
5598a275a6aSAllan Stephens 		if (seqno != next_in)
5608a275a6aSAllan Stephens 			goto unlock;
5618a275a6aSAllan Stephens 
5628a275a6aSAllan Stephens 		/* Take in-sequence message from deferred queue & deliver it */
563bc6fecd4SYing Xue 		buf = __skb_dequeue(&node->bclink.deferred_queue);
564b97bf3fdSPer Liden 		goto receive;
565b97bf3fdSPer Liden 	}
5668a275a6aSAllan Stephens 
5678a275a6aSAllan Stephens 	/* Handle out-of-sequence broadcast message */
5688a275a6aSAllan Stephens 	if (less(next_in, seqno)) {
569bc6fecd4SYing Xue 		deferred = tipc_link_defer_pkt(&node->bclink.deferred_queue,
5708a275a6aSAllan Stephens 					       buf);
5717a54d4a9SAllan Stephens 		bclink_update_last_sent(node, seqno);
5725d3c488dSAllan Stephens 		buf = NULL;
5730abd8ff2SJon Paul Maloy 	}
5748a275a6aSAllan Stephens 
5751da46568SYing Xue 	tipc_bclink_lock(net);
576b98158e3SAllan Stephens 
5778a275a6aSAllan Stephens 	if (deferred)
5788a275a6aSAllan Stephens 		bcl->stats.deferred_recv++;
5790232c5a5SAllan Stephens 	else
5800232c5a5SAllan Stephens 		bcl->stats.duplicates++;
5818a275a6aSAllan Stephens 
5821da46568SYing Xue 	tipc_bclink_unlock(net);
583b98158e3SAllan Stephens 
5845d3c488dSAllan Stephens unlock:
5854323add6SPer Liden 	tipc_node_unlock(node);
5865d3c488dSAllan Stephens exit:
5875f6d9123SAllan Stephens 	kfree_skb(buf);
588b97bf3fdSPer Liden }
589b97bf3fdSPer Liden 
5906c00055aSDavid S. Miller u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
591b97bf3fdSPer Liden {
592389dd9bcSYing Xue 	return (n_ptr->bclink.recv_permitted &&
5931da46568SYing Xue 		(tipc_bclink_get_last_sent(n_ptr->net) != n_ptr->bclink.acked));
594b97bf3fdSPer Liden }
595b97bf3fdSPer Liden 
596b97bf3fdSPer Liden 
597b97bf3fdSPer Liden /**
5984323add6SPer Liden  * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
599b97bf3fdSPer Liden  *
6002ff9f924SAllan Stephens  * Send packet over as many bearers as necessary to reach all nodes
6012ff9f924SAllan Stephens  * that have joined the broadcast link.
602b97bf3fdSPer Liden  *
6032ff9f924SAllan Stephens  * Returns 0 (packet sent successfully) under all circumstances,
6042ff9f924SAllan Stephens  * since the broadcast link's pseudo-bearer never blocks
605b97bf3fdSPer Liden  */
6061da46568SYing Xue static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
6071da46568SYing Xue 			      struct tipc_bearer *unused1,
608b97bf3fdSPer Liden 			      struct tipc_media_addr *unused2)
609b97bf3fdSPer Liden {
610b97bf3fdSPer Liden 	int bp_index;
6116f92ee54SJon Paul Maloy 	struct tipc_msg *msg = buf_msg(buf);
612c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6131da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6141da46568SYing Xue 	struct tipc_bclink *bclink = tn->bclink;
615b97bf3fdSPer Liden 
616e6160710SGerlando Falauto 	/* Prepare broadcast link message for reliable transmission,
6172ff9f924SAllan Stephens 	 * if first time trying to send it;
6182ff9f924SAllan Stephens 	 * preparation is skipped for broadcast link protocol messages
6192ff9f924SAllan Stephens 	 * since they are sent in an unreliable manner and don't need it
6202ff9f924SAllan Stephens 	 */
621b97bf3fdSPer Liden 	if (likely(!msg_non_seq(buf_msg(buf)))) {
622cd3decdfSAllan Stephens 		bcbuf_set_acks(buf, bclink->bcast_nodes.count);
62340aecb1bSAllan Stephens 		msg_set_non_seq(msg, 1);
624c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
6251da46568SYing Xue 		tn->bcl->stats.sent_info++;
6265e726900SAllan Stephens 
627cd3decdfSAllan Stephens 		if (WARN_ON(!bclink->bcast_nodes.count)) {
6285e726900SAllan Stephens 			dump_stack();
6295e726900SAllan Stephens 			return 0;
6305e726900SAllan Stephens 		}
631b97bf3fdSPer Liden 	}
632b97bf3fdSPer Liden 
633b97bf3fdSPer Liden 	/* Send buffer over bearers until all targets reached */
634cd3decdfSAllan Stephens 	bcbearer->remains = bclink->bcast_nodes;
635b97bf3fdSPer Liden 
636b97bf3fdSPer Liden 	for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
6372d627b92SAllan Stephens 		struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
6382d627b92SAllan Stephens 		struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
6396f92ee54SJon Paul Maloy 		struct tipc_bearer *bp[2] = {p, s};
6406f92ee54SJon Paul Maloy 		struct tipc_bearer *b = bp[msg_link_selector(msg)];
641488fc9afSGerlando Falauto 		struct sk_buff *tbuf;
642b97bf3fdSPer Liden 
643b97bf3fdSPer Liden 		if (!p)
644e6160710SGerlando Falauto 			break; /* No more bearers to try */
6456f92ee54SJon Paul Maloy 		if (!b)
6466f92ee54SJon Paul Maloy 			b = p;
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 */
6547f9f95d9SYing Xue 			tipc_bearer_send(net, 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;
6607f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, tbuf,
6617f9f95d9SYing Xue 					 &b->bcast_addr);
662488fc9afSGerlando Falauto 			kfree_skb(tbuf); /* Bearer keeps a clone */
663488fc9afSGerlando Falauto 		}
66465f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == 0)
665e6160710SGerlando Falauto 			break; /* All targets reached */
666b97bf3fdSPer Liden 
66765f51ef0SAllan Stephens 		bcbearer->remains = bcbearer->remains_new;
668b97bf3fdSPer Liden 	}
669b97bf3fdSPer Liden 
6702ff9f924SAllan Stephens 	return 0;
671b97bf3fdSPer Liden }
672b97bf3fdSPer Liden 
673b97bf3fdSPer Liden /**
6744323add6SPer Liden  * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
675b97bf3fdSPer Liden  */
6767f9f95d9SYing Xue void tipc_bcbearer_sort(struct net *net, struct tipc_node_map *nm_ptr,
6777f9f95d9SYing Xue 			u32 node, bool action)
678b97bf3fdSPer Liden {
6797f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6801da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6817f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
6827f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_curr;
683f8322dfcSYing Xue 	struct tipc_bearer *b;
684b97bf3fdSPer Liden 	int b_index;
685b97bf3fdSPer Liden 	int pri;
686b97bf3fdSPer Liden 
6871da46568SYing Xue 	tipc_bclink_lock(net);
688b97bf3fdSPer Liden 
68928dd9418SYing Xue 	if (action)
69028dd9418SYing Xue 		tipc_nmap_add(nm_ptr, node);
69128dd9418SYing Xue 	else
69228dd9418SYing Xue 		tipc_nmap_remove(nm_ptr, node);
69328dd9418SYing Xue 
694b97bf3fdSPer Liden 	/* Group bearers by priority (can assume max of two per priority) */
695b97bf3fdSPer Liden 	memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
696b97bf3fdSPer Liden 
697f8322dfcSYing Xue 	rcu_read_lock();
698b97bf3fdSPer Liden 	for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
6997f9f95d9SYing Xue 		b = rcu_dereference_rtnl(tn->bearer_list[b_index]);
700f47de12bSYing Xue 		if (!b || !b->nodes.count)
701b97bf3fdSPer Liden 			continue;
702b97bf3fdSPer Liden 
703b97bf3fdSPer Liden 		if (!bp_temp[b->priority].primary)
704b97bf3fdSPer Liden 			bp_temp[b->priority].primary = b;
705b97bf3fdSPer Liden 		else
706b97bf3fdSPer Liden 			bp_temp[b->priority].secondary = b;
707b97bf3fdSPer Liden 	}
708f8322dfcSYing Xue 	rcu_read_unlock();
709b97bf3fdSPer Liden 
710b97bf3fdSPer Liden 	/* Create array of bearer pairs for broadcasting */
711b97bf3fdSPer Liden 	bp_curr = bcbearer->bpairs;
712b97bf3fdSPer Liden 	memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
713b97bf3fdSPer Liden 
71416cb4b33SPer Liden 	for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
715b97bf3fdSPer Liden 
716b97bf3fdSPer Liden 		if (!bp_temp[pri].primary)
717b97bf3fdSPer Liden 			continue;
718b97bf3fdSPer Liden 
719b97bf3fdSPer Liden 		bp_curr->primary = bp_temp[pri].primary;
720b97bf3fdSPer Liden 
721b97bf3fdSPer Liden 		if (bp_temp[pri].secondary) {
7224323add6SPer Liden 			if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
723b97bf3fdSPer Liden 					    &bp_temp[pri].secondary->nodes)) {
724b97bf3fdSPer Liden 				bp_curr->secondary = bp_temp[pri].secondary;
725b97bf3fdSPer Liden 			} else {
726b97bf3fdSPer Liden 				bp_curr++;
727b97bf3fdSPer Liden 				bp_curr->primary = bp_temp[pri].secondary;
728b97bf3fdSPer Liden 			}
729b97bf3fdSPer Liden 		}
730b97bf3fdSPer Liden 
731b97bf3fdSPer Liden 		bp_curr++;
732b97bf3fdSPer Liden 	}
733b97bf3fdSPer Liden 
7341da46568SYing Xue 	tipc_bclink_unlock(net);
735b97bf3fdSPer Liden }
736b97bf3fdSPer Liden 
737d8182804SRichard Alpe static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
738d8182804SRichard Alpe 				      struct tipc_stats *stats)
7397be57fc6SRichard Alpe {
7407be57fc6SRichard Alpe 	int i;
7417be57fc6SRichard Alpe 	struct nlattr *nest;
7427be57fc6SRichard Alpe 
7437be57fc6SRichard Alpe 	struct nla_map {
7447be57fc6SRichard Alpe 		__u32 key;
7457be57fc6SRichard Alpe 		__u32 val;
7467be57fc6SRichard Alpe 	};
7477be57fc6SRichard Alpe 
7487be57fc6SRichard Alpe 	struct nla_map map[] = {
7497be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_INFO, stats->recv_info},
7507be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
7517be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
7527be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
7537be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
7547be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_INFO, stats->sent_info},
7557be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
7567be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
7577be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
7587be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
7597be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
7607be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
7617be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
7627be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
7637be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
7647be57fc6SRichard Alpe 		{TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
7657be57fc6SRichard Alpe 		{TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
7667be57fc6SRichard Alpe 		{TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
7677be57fc6SRichard Alpe 		{TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
7687be57fc6SRichard Alpe 			(stats->accu_queue_sz / stats->queue_sz_counts) : 0}
7697be57fc6SRichard Alpe 	};
7707be57fc6SRichard Alpe 
7717be57fc6SRichard Alpe 	nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
7727be57fc6SRichard Alpe 	if (!nest)
7737be57fc6SRichard Alpe 		return -EMSGSIZE;
7747be57fc6SRichard Alpe 
7757be57fc6SRichard Alpe 	for (i = 0; i <  ARRAY_SIZE(map); i++)
7767be57fc6SRichard Alpe 		if (nla_put_u32(skb, map[i].key, map[i].val))
7777be57fc6SRichard Alpe 			goto msg_full;
7787be57fc6SRichard Alpe 
7797be57fc6SRichard Alpe 	nla_nest_end(skb, nest);
7807be57fc6SRichard Alpe 
7817be57fc6SRichard Alpe 	return 0;
7827be57fc6SRichard Alpe msg_full:
7837be57fc6SRichard Alpe 	nla_nest_cancel(skb, nest);
7847be57fc6SRichard Alpe 
7857be57fc6SRichard Alpe 	return -EMSGSIZE;
7867be57fc6SRichard Alpe }
7877be57fc6SRichard Alpe 
7881da46568SYing Xue int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
7897be57fc6SRichard Alpe {
7907be57fc6SRichard Alpe 	int err;
7917be57fc6SRichard Alpe 	void *hdr;
7927be57fc6SRichard Alpe 	struct nlattr *attrs;
7937be57fc6SRichard Alpe 	struct nlattr *prop;
7941da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
7951da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
7967be57fc6SRichard Alpe 
7977be57fc6SRichard Alpe 	if (!bcl)
7987be57fc6SRichard Alpe 		return 0;
7997be57fc6SRichard Alpe 
8001da46568SYing Xue 	tipc_bclink_lock(net);
8017be57fc6SRichard Alpe 
8027be57fc6SRichard Alpe 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
8037be57fc6SRichard Alpe 			  NLM_F_MULTI, TIPC_NL_LINK_GET);
8047be57fc6SRichard Alpe 	if (!hdr)
8057be57fc6SRichard Alpe 		return -EMSGSIZE;
8067be57fc6SRichard Alpe 
8077be57fc6SRichard Alpe 	attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
8087be57fc6SRichard Alpe 	if (!attrs)
8097be57fc6SRichard Alpe 		goto msg_full;
8107be57fc6SRichard Alpe 
8117be57fc6SRichard Alpe 	/* The broadcast link is always up */
8127be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
8137be57fc6SRichard Alpe 		goto attr_msg_full;
8147be57fc6SRichard Alpe 
8157be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
8167be57fc6SRichard Alpe 		goto attr_msg_full;
8177be57fc6SRichard Alpe 	if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
8187be57fc6SRichard Alpe 		goto attr_msg_full;
8197be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->next_in_no))
8207be57fc6SRichard Alpe 		goto attr_msg_full;
8217be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->next_out_no))
8227be57fc6SRichard Alpe 		goto attr_msg_full;
8237be57fc6SRichard Alpe 
8247be57fc6SRichard Alpe 	prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
8257be57fc6SRichard Alpe 	if (!prop)
8267be57fc6SRichard Alpe 		goto attr_msg_full;
8277be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->queue_limit[0]))
8287be57fc6SRichard Alpe 		goto prop_msg_full;
8297be57fc6SRichard Alpe 	nla_nest_end(msg->skb, prop);
8307be57fc6SRichard Alpe 
8317be57fc6SRichard Alpe 	err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
8327be57fc6SRichard Alpe 	if (err)
8337be57fc6SRichard Alpe 		goto attr_msg_full;
8347be57fc6SRichard Alpe 
8351da46568SYing Xue 	tipc_bclink_unlock(net);
8367be57fc6SRichard Alpe 	nla_nest_end(msg->skb, attrs);
8377be57fc6SRichard Alpe 	genlmsg_end(msg->skb, hdr);
8387be57fc6SRichard Alpe 
8397be57fc6SRichard Alpe 	return 0;
8407be57fc6SRichard Alpe 
8417be57fc6SRichard Alpe prop_msg_full:
8427be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, prop);
8437be57fc6SRichard Alpe attr_msg_full:
8447be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, attrs);
8457be57fc6SRichard Alpe msg_full:
8461da46568SYing Xue 	tipc_bclink_unlock(net);
8477be57fc6SRichard Alpe 	genlmsg_cancel(msg->skb, hdr);
8487be57fc6SRichard Alpe 
8497be57fc6SRichard Alpe 	return -EMSGSIZE;
8507be57fc6SRichard Alpe }
851b97bf3fdSPer Liden 
8521da46568SYing Xue int tipc_bclink_stats(struct net *net, char *buf, const u32 buf_size)
853b97bf3fdSPer Liden {
854dc1aed37SErik Hugne 	int ret;
855dc1aed37SErik Hugne 	struct tipc_stats *s;
8561da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8571da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
858b97bf3fdSPer Liden 
859b97bf3fdSPer Liden 	if (!bcl)
860b97bf3fdSPer Liden 		return 0;
861b97bf3fdSPer Liden 
8621da46568SYing Xue 	tipc_bclink_lock(net);
863b97bf3fdSPer Liden 
864dc1aed37SErik Hugne 	s = &bcl->stats;
865dc1aed37SErik Hugne 
866dc1aed37SErik Hugne 	ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
867b97bf3fdSPer Liden 			    "  Window:%u packets\n",
868b97bf3fdSPer Liden 			    bcl->name, bcl->queue_limit[0]);
869dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
870dc1aed37SErik Hugne 			     "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
871dc1aed37SErik Hugne 			     s->recv_info, s->recv_fragments,
872dc1aed37SErik Hugne 			     s->recv_fragmented, s->recv_bundles,
873dc1aed37SErik Hugne 			     s->recv_bundled);
874dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
875dc1aed37SErik Hugne 			     "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
876dc1aed37SErik Hugne 			     s->sent_info, s->sent_fragments,
877dc1aed37SErik Hugne 			     s->sent_fragmented, s->sent_bundles,
878dc1aed37SErik Hugne 			     s->sent_bundled);
879dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
880dc1aed37SErik Hugne 			     "  RX naks:%u defs:%u dups:%u\n",
881dc1aed37SErik Hugne 			     s->recv_nacks, s->deferred_recv, s->duplicates);
882dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
883dc1aed37SErik Hugne 			     "  TX naks:%u acks:%u dups:%u\n",
884dc1aed37SErik Hugne 			     s->sent_nacks, s->sent_acks, s->retransmitted);
885dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
8863c294cb3SYing Xue 			     "  Congestion link:%u  Send queue max:%u avg:%u\n",
8873c294cb3SYing Xue 			     s->link_congs, s->max_queue_sz,
888dc1aed37SErik Hugne 			     s->queue_sz_counts ?
889dc1aed37SErik Hugne 			     (s->accu_queue_sz / s->queue_sz_counts) : 0);
890b97bf3fdSPer Liden 
8911da46568SYing Xue 	tipc_bclink_unlock(net);
892dc1aed37SErik Hugne 	return ret;
893b97bf3fdSPer Liden }
894b97bf3fdSPer Liden 
8951da46568SYing Xue int tipc_bclink_reset_stats(struct net *net)
896b97bf3fdSPer Liden {
8971da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8981da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8991da46568SYing Xue 
900b97bf3fdSPer Liden 	if (!bcl)
901b97bf3fdSPer Liden 		return -ENOPROTOOPT;
902b97bf3fdSPer Liden 
9031da46568SYing Xue 	tipc_bclink_lock(net);
904b97bf3fdSPer Liden 	memset(&bcl->stats, 0, sizeof(bcl->stats));
9051da46568SYing Xue 	tipc_bclink_unlock(net);
9060e35fd5eSAllan Stephens 	return 0;
907b97bf3fdSPer Liden }
908b97bf3fdSPer Liden 
9091da46568SYing Xue int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
910b97bf3fdSPer Liden {
9111da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9121da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
9131da46568SYing Xue 
914b97bf3fdSPer Liden 	if (!bcl)
915b97bf3fdSPer Liden 		return -ENOPROTOOPT;
916b97bf3fdSPer Liden 	if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
917b97bf3fdSPer Liden 		return -EINVAL;
918b97bf3fdSPer Liden 
9191da46568SYing Xue 	tipc_bclink_lock(net);
9204323add6SPer Liden 	tipc_link_set_queue_limits(bcl, limit);
9211da46568SYing Xue 	tipc_bclink_unlock(net);
9220e35fd5eSAllan Stephens 	return 0;
923b97bf3fdSPer Liden }
924b97bf3fdSPer Liden 
9257f9f95d9SYing Xue int tipc_bclink_init(struct net *net)
926b97bf3fdSPer Liden {
9277f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9281da46568SYing Xue 	struct tipc_bcbearer *bcbearer;
9291da46568SYing Xue 	struct tipc_bclink *bclink;
9301da46568SYing Xue 	struct tipc_link *bcl;
9317f9f95d9SYing Xue 
932eb8b00f5SYing Xue 	bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
933eb8b00f5SYing Xue 	if (!bcbearer)
934eb8b00f5SYing Xue 		return -ENOMEM;
935eb8b00f5SYing Xue 
936eb8b00f5SYing Xue 	bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
937eb8b00f5SYing Xue 	if (!bclink) {
938eb8b00f5SYing Xue 		kfree(bcbearer);
939eb8b00f5SYing Xue 		return -ENOMEM;
940eb8b00f5SYing Xue 	}
941eb8b00f5SYing Xue 
942eb8b00f5SYing Xue 	bcl = &bclink->link;
943b97bf3fdSPer Liden 	bcbearer->bearer.media = &bcbearer->media;
9444323add6SPer Liden 	bcbearer->media.send_msg = tipc_bcbearer_send;
9452e2d9be8SAllan Stephens 	sprintf(bcbearer->media.name, "tipc-broadcast");
946b97bf3fdSPer Liden 
947d69afc90SYing Xue 	spin_lock_init(&bclink->lock);
94858dc55f2SYing Xue 	__skb_queue_head_init(&bcl->outqueue);
949bc6fecd4SYing Xue 	__skb_queue_head_init(&bcl->deferred_queue);
950c637c103SJon Paul Maloy 	skb_queue_head_init(&bcl->wakeupq);
951b97bf3fdSPer Liden 	bcl->next_out_no = 1;
95234af946aSIngo Molnar 	spin_lock_init(&bclink->node.lock);
953b97bf3fdSPer Liden 	bcl->owner = &bclink->node;
9541da46568SYing Xue 	bcl->owner->net = net;
955b97bf3fdSPer Liden 	bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
9564323add6SPer Liden 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
9577a2f7d18SYing Xue 	bcl->bearer_id = MAX_BEARERS;
9587f9f95d9SYing Xue 	rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
959b97bf3fdSPer Liden 	bcl->state = WORKING_WORKING;
960c5898636SJon Paul Maloy 	bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
961c5898636SJon Paul Maloy 	msg_set_prevnode(bcl->pmsg, tn->own_addr);
9624b704d59SStephen Hemminger 	strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
9631da46568SYing Xue 	tn->bcbearer = bcbearer;
9641da46568SYing Xue 	tn->bclink = bclink;
9651da46568SYing Xue 	tn->bcl = bcl;
966eb8b00f5SYing Xue 	return 0;
967b97bf3fdSPer Liden }
968b97bf3fdSPer Liden 
9697f9f95d9SYing Xue void tipc_bclink_stop(struct net *net)
970b97bf3fdSPer Liden {
9717f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9727f9f95d9SYing Xue 
9731da46568SYing Xue 	tipc_bclink_lock(net);
9741da46568SYing Xue 	tipc_link_purge_queues(tn->bcl);
9751da46568SYing Xue 	tipc_bclink_unlock(net);
976c47e9b91SAllan Stephens 
9777f9f95d9SYing Xue 	RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
978eb8b00f5SYing Xue 	synchronize_net();
9791da46568SYing Xue 	kfree(tn->bcbearer);
9801da46568SYing Xue 	kfree(tn->bclink);
981b97bf3fdSPer Liden }
982b97bf3fdSPer Liden 
9833e22e62bSAllan Stephens /**
9843e22e62bSAllan Stephens  * tipc_nmap_add - add a node to a node map
9853e22e62bSAllan Stephens  */
98628dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
9873e22e62bSAllan Stephens {
9883e22e62bSAllan Stephens 	int n = tipc_node(node);
9893e22e62bSAllan Stephens 	int w = n / WSIZE;
9903e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
9913e22e62bSAllan Stephens 
9923e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) == 0) {
9933e22e62bSAllan Stephens 		nm_ptr->count++;
9943e22e62bSAllan Stephens 		nm_ptr->map[w] |= mask;
9953e22e62bSAllan Stephens 	}
9963e22e62bSAllan Stephens }
9973e22e62bSAllan Stephens 
9983e22e62bSAllan Stephens /**
9993e22e62bSAllan Stephens  * tipc_nmap_remove - remove a node from a node map
10003e22e62bSAllan Stephens  */
100128dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
10023e22e62bSAllan Stephens {
10033e22e62bSAllan Stephens 	int n = tipc_node(node);
10043e22e62bSAllan Stephens 	int w = n / WSIZE;
10053e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
10063e22e62bSAllan Stephens 
10073e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) != 0) {
10083e22e62bSAllan Stephens 		nm_ptr->map[w] &= ~mask;
10093e22e62bSAllan Stephens 		nm_ptr->count--;
10103e22e62bSAllan Stephens 	}
10113e22e62bSAllan Stephens }
10123e22e62bSAllan Stephens 
10133e22e62bSAllan Stephens /**
10143e22e62bSAllan Stephens  * tipc_nmap_diff - find differences between node maps
10153e22e62bSAllan Stephens  * @nm_a: input node map A
10163e22e62bSAllan Stephens  * @nm_b: input node map B
10173e22e62bSAllan Stephens  * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
10183e22e62bSAllan Stephens  */
101931e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
102031e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
10213e22e62bSAllan Stephens 			   struct tipc_node_map *nm_diff)
10223e22e62bSAllan Stephens {
10233e22e62bSAllan Stephens 	int stop = ARRAY_SIZE(nm_a->map);
10243e22e62bSAllan Stephens 	int w;
10253e22e62bSAllan Stephens 	int b;
10263e22e62bSAllan Stephens 	u32 map;
10273e22e62bSAllan Stephens 
10283e22e62bSAllan Stephens 	memset(nm_diff, 0, sizeof(*nm_diff));
10293e22e62bSAllan Stephens 	for (w = 0; w < stop; w++) {
10303e22e62bSAllan Stephens 		map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
10313e22e62bSAllan Stephens 		nm_diff->map[w] = map;
10323e22e62bSAllan Stephens 		if (map != 0) {
10333e22e62bSAllan Stephens 			for (b = 0 ; b < WSIZE; b++) {
10343e22e62bSAllan Stephens 				if (map & (1 << b))
10353e22e62bSAllan Stephens 					nm_diff->count++;
10363e22e62bSAllan Stephens 			}
10373e22e62bSAllan Stephens 		}
10383e22e62bSAllan Stephens 	}
10393e22e62bSAllan Stephens }
1040