blob: ef8cff4ad7438251e36f02861dbe5180e292d47f [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/bcast.c: TIPC broadcast code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2004-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2004, Intel Corporation.
Allan Stephens2d627b92011-01-07 13:00:11 -05006 * Copyright (c) 2005, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01007 * All rights reserved.
8 *
Per Liden9ea1fd32006-01-11 13:30:43 +01009 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +010010 * modification, are permitted provided that the following conditions are met:
11 *
Per Liden9ea1fd32006-01-11 13:30:43 +010012 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010020 *
Per Liden9ea1fd32006-01-11 13:30:43 +010021 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010035 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "bcast.h"
Allan Stephens9f6bdcd42011-04-07 14:57:53 -040042#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
Ying Xue987b58b2014-03-27 12:54:35 +080044#define MAX_PKT_DEFAULT_MCAST 1500 /* bcast link max packet size (fixed) */
45#define BCLINK_WIN_DEFAULT 20 /* bcast link window size (default) */
46#define BCBEARER MAX_BEARERS
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
Per Lidenb97bf3f2006-01-02 19:04:38 +010048/**
Paul Gortmaker7f9ab6a2011-12-29 20:55:27 -050049 * struct tipc_bcbearer_pair - a pair of bearers used by broadcast link
Per Lidenb97bf3f2006-01-02 19:04:38 +010050 * @primary: pointer to primary bearer
51 * @secondary: pointer to secondary bearer
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090052 *
53 * Bearers must have same priority and same set of reachable destinations
Per Lidenb97bf3f2006-01-02 19:04:38 +010054 * to be paired.
55 */
56
Paul Gortmaker7f9ab6a2011-12-29 20:55:27 -050057struct tipc_bcbearer_pair {
Allan Stephens2d627b92011-01-07 13:00:11 -050058 struct tipc_bearer *primary;
59 struct tipc_bearer *secondary;
Per Lidenb97bf3f2006-01-02 19:04:38 +010060};
61
62/**
Paul Gortmaker7f9ab6a2011-12-29 20:55:27 -050063 * struct tipc_bcbearer - bearer used by broadcast link
Per Lidenb97bf3f2006-01-02 19:04:38 +010064 * @bearer: (non-standard) broadcast bearer structure
65 * @media: (non-standard) broadcast media structure
66 * @bpairs: array of bearer pairs
Allan Stephens65f51ef2006-06-25 23:53:20 -070067 * @bpairs_temp: temporary array of bearer pairs used by tipc_bcbearer_sort()
68 * @remains: temporary node map used by tipc_bcbearer_send()
69 * @remains_new: temporary node map used tipc_bcbearer_send()
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090070 *
Allan Stephens65f51ef2006-06-25 23:53:20 -070071 * Note: The fields labelled "temporary" are incorporated into the bearer
72 * to avoid consuming potentially limited stack space through the use of
73 * large local variables within multicast routines. Concurrent access is
Ying Xued69afc92014-05-05 08:56:15 +080074 * prevented through use of the spinlock "bclink_lock".
Per Lidenb97bf3f2006-01-02 19:04:38 +010075 */
Paul Gortmaker7f9ab6a2011-12-29 20:55:27 -050076struct tipc_bcbearer {
Allan Stephens2d627b92011-01-07 13:00:11 -050077 struct tipc_bearer bearer;
Paul Gortmaker358a0d12011-12-29 20:19:42 -050078 struct tipc_media media;
Paul Gortmaker7f9ab6a2011-12-29 20:55:27 -050079 struct tipc_bcbearer_pair bpairs[MAX_BEARERS];
80 struct tipc_bcbearer_pair bpairs_temp[TIPC_MAX_LINK_PRI + 1];
David S. Miller6c000552008-09-02 23:38:32 -070081 struct tipc_node_map remains;
82 struct tipc_node_map remains_new;
Per Lidenb97bf3f2006-01-02 19:04:38 +010083};
84
85/**
Paul Gortmaker6765fd62011-12-29 20:52:18 -050086 * struct tipc_bclink - link used for broadcast messages
Ying Xued69afc92014-05-05 08:56:15 +080087 * @lock: spinlock governing access to structure
Per Lidenb97bf3f2006-01-02 19:04:38 +010088 * @link: (non-standard) broadcast link structure
89 * @node: (non-standard) node structure representing b'cast link's peer node
Allan Stephenscd3decd2011-10-24 11:18:12 -040090 * @bcast_nodes: map of broadcast-capable nodes
Allan Stephens01d83ed2011-01-18 13:53:16 -050091 * @retransmit_to: node that most recently requested a retransmit
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090092 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010093 * Handles sequence numbering, fragmentation, bundling, etc.
94 */
Paul Gortmaker6765fd62011-12-29 20:52:18 -050095struct tipc_bclink {
Ying Xued69afc92014-05-05 08:56:15 +080096 spinlock_t lock;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050097 struct tipc_link link;
David S. Miller6c000552008-09-02 23:38:32 -070098 struct tipc_node node;
Allan Stephenscd3decd2011-10-24 11:18:12 -040099 struct tipc_node_map bcast_nodes;
Allan Stephens01d83ed2011-01-18 13:53:16 -0500100 struct tipc_node *retransmit_to;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101};
102
Ying Xueeb8b00f2014-05-05 08:56:16 +0800103static struct tipc_bcbearer *bcbearer;
104static struct tipc_bclink *bclink;
105static struct tipc_link *bcl;
Allan Stephensc47e9b92011-10-24 10:29:26 -0400106
Allan Stephens3aec9cc2010-05-11 14:30:07 +0000107const char tipc_bclink_name[] = "broadcast-link";
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000109static void tipc_nmap_diff(struct tipc_node_map *nm_a,
110 struct tipc_node_map *nm_b,
111 struct tipc_node_map *nm_diff);
Ying Xue28dd9412014-04-21 10:55:51 +0800112static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node);
113static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114
Ying Xued69afc92014-05-05 08:56:15 +0800115static void tipc_bclink_lock(void)
116{
117 spin_lock_bh(&bclink->lock);
118}
119
120static void tipc_bclink_unlock(void)
121{
122 spin_unlock_bh(&bclink->lock);
123}
124
Sam Ravnborg05790c62006-03-20 22:37:04 -0800125static u32 bcbuf_acks(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126{
David S. Miller880b0052006-01-12 13:22:32 -0800127 return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128}
129
Sam Ravnborg05790c62006-03-20 22:37:04 -0800130static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131{
David S. Miller880b0052006-01-12 13:22:32 -0800132 TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133}
134
Sam Ravnborg05790c62006-03-20 22:37:04 -0800135static void bcbuf_decr_acks(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136{
137 bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
138}
139
Allan Stephenscd3decd2011-10-24 11:18:12 -0400140void tipc_bclink_add_node(u32 addr)
141{
Ying Xued69afc92014-05-05 08:56:15 +0800142 tipc_bclink_lock();
Allan Stephenscd3decd2011-10-24 11:18:12 -0400143 tipc_nmap_add(&bclink->bcast_nodes, addr);
Ying Xued69afc92014-05-05 08:56:15 +0800144 tipc_bclink_unlock();
Allan Stephenscd3decd2011-10-24 11:18:12 -0400145}
146
147void tipc_bclink_remove_node(u32 addr)
148{
Ying Xued69afc92014-05-05 08:56:15 +0800149 tipc_bclink_lock();
Allan Stephenscd3decd2011-10-24 11:18:12 -0400150 tipc_nmap_remove(&bclink->bcast_nodes, addr);
Ying Xued69afc92014-05-05 08:56:15 +0800151 tipc_bclink_unlock();
Allan Stephenscd3decd2011-10-24 11:18:12 -0400152}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153
Allan Stephens5b1f7bd2010-08-17 11:00:09 +0000154static void bclink_set_last_sent(void)
155{
156 if (bcl->next_out)
157 bcl->fsm_msg_cnt = mod(buf_seqno(bcl->next_out) - 1);
158 else
159 bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
160}
161
162u32 tipc_bclink_get_last_sent(void)
163{
164 return bcl->fsm_msg_cnt;
165}
166
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400167static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168{
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400169 node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
170 seqno : node->bclink.last_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171}
172
173
Ben Hutchings2c530402012-07-10 10:55:09 +0000174/**
Allan Stephens01d83ed2011-01-18 13:53:16 -0500175 * tipc_bclink_retransmit_to - get most recent node to request retransmission
176 *
Ying Xued69afc92014-05-05 08:56:15 +0800177 * Called with bclink_lock locked
Allan Stephens01d83ed2011-01-18 13:53:16 -0500178 */
Allan Stephens01d83ed2011-01-18 13:53:16 -0500179struct tipc_node *tipc_bclink_retransmit_to(void)
180{
181 return bclink->retransmit_to;
182}
183
184/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 * bclink_retransmit_pkt - retransmit broadcast packets
186 * @after: sequence number of last packet to *not* retransmit
187 * @to: sequence number of last packet to retransmit
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900188 *
Ying Xued69afc92014-05-05 08:56:15 +0800189 * Called with bclink_lock locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191static void bclink_retransmit_pkt(u32 after, u32 to)
192{
193 struct sk_buff *buf;
194
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195 buf = bcl->first_out;
Allan Stephensa0168922010-12-31 18:59:35 +0000196 while (buf && less_eq(buf_seqno(buf), after))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900197 buf = buf->next;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700198 tipc_link_retransmit(bcl, buf, mod(to - after));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199}
200
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900201/**
Per Liden4323add2006-01-18 00:38:21 +0100202 * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203 * @n_ptr: node that sent acknowledgement info
204 * @acked: broadcast sequence # that has been acknowledged
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900205 *
Ying Xued69afc92014-05-05 08:56:15 +0800206 * Node is locked, bclink_lock unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 */
David S. Miller6c000552008-09-02 23:38:32 -0700208void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209{
210 struct sk_buff *crs;
211 struct sk_buff *next;
212 unsigned int released = 0;
213
Ying Xued69afc92014-05-05 08:56:15 +0800214 tipc_bclink_lock();
Allan Stephens365595912011-10-24 15:26:24 -0400215 /* Bail out if tx queue is empty (no clean up is required) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 crs = bcl->first_out;
Allan Stephens365595912011-10-24 15:26:24 -0400217 if (!crs)
218 goto exit;
219
220 /* Determine which messages need to be acknowledged */
221 if (acked == INVALID_LINK_SEQ) {
222 /*
223 * Contact with specified node has been lost, so need to
224 * acknowledge sent messages only (if other nodes still exist)
225 * or both sent and unsent messages (otherwise)
226 */
227 if (bclink->bcast_nodes.count)
228 acked = bcl->fsm_msg_cnt;
229 else
230 acked = bcl->next_out_no;
231 } else {
232 /*
233 * Bail out if specified sequence number does not correspond
234 * to a message that has been sent and not yet acknowledged
235 */
236 if (less(acked, buf_seqno(crs)) ||
237 less(bcl->fsm_msg_cnt, acked) ||
238 less_eq(acked, n_ptr->bclink.acked))
239 goto exit;
240 }
241
242 /* Skip over packets that node has previously acknowledged */
Allan Stephensa0168922010-12-31 18:59:35 +0000243 while (crs && less_eq(buf_seqno(crs), n_ptr->bclink.acked))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 crs = crs->next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245
246 /* Update packets that node is now acknowledging */
247
248 while (crs && less_eq(buf_seqno(crs), acked)) {
249 next = crs->next;
Allan Stephens10745cd2011-10-24 14:59:20 -0400250
251 if (crs != bcl->next_out)
252 bcbuf_decr_acks(crs);
Allan Stephens10745cd2011-10-24 14:59:20 -0400253 else {
254 bcbuf_set_acks(crs, 0);
255 bcl->next_out = next;
256 bclink_set_last_sent();
257 }
258
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 if (bcbuf_acks(crs) == 0) {
260 bcl->first_out = next;
261 bcl->out_queue_size--;
Allan Stephens5f6d9122011-11-04 13:24:29 -0400262 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 released = 1;
264 }
265 crs = next;
266 }
267 n_ptr->bclink.acked = acked;
268
269 /* Try resolving broadcast link congestion, if necessary */
270
Allan Stephens5b1f7bd2010-08-17 11:00:09 +0000271 if (unlikely(bcl->next_out)) {
Per Liden4323add2006-01-18 00:38:21 +0100272 tipc_link_push_queue(bcl);
Allan Stephens5b1f7bd2010-08-17 11:00:09 +0000273 bclink_set_last_sent();
274 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 if (unlikely(released && !list_empty(&bcl->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +0100276 tipc_link_wakeup_ports(bcl, 0);
Allan Stephens365595912011-10-24 15:26:24 -0400277exit:
Ying Xued69afc92014-05-05 08:56:15 +0800278 tipc_bclink_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279}
280
Ben Hutchings2c530402012-07-10 10:55:09 +0000281/**
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400282 * tipc_bclink_update_link_state - update broadcast link state
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900283 *
Ying Xue7216cd92014-04-21 10:55:48 +0800284 * RCU and node lock set
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 */
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400286void tipc_bclink_update_link_state(struct tipc_node *n_ptr, u32 last_sent)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287{
288 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400290 /* Ignore "stale" link state info */
291
292 if (less_eq(last_sent, n_ptr->bclink.last_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 return;
294
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400295 /* Update link synchronization state; quit if in sync */
296
297 bclink_update_last_sent(n_ptr, last_sent);
298
299 if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
300 return;
301
302 /* Update out-of-sync state; quit if loss is still unconfirmed */
303
304 if ((++n_ptr->bclink.oos_state) == 1) {
305 if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
306 return;
307 n_ptr->bclink.oos_state++;
308 }
309
310 /* Don't NACK if one has been recently sent (or seen) */
311
312 if (n_ptr->bclink.oos_state & 0x1)
313 return;
314
315 /* Send NACK */
316
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000317 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 if (buf) {
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400319 struct tipc_msg *msg = buf_msg(buf);
320
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000321 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG,
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400322 INT_H_SIZE, n_ptr->addr);
Allan Stephensbf781ec2011-01-25 16:12:39 -0500323 msg_set_non_seq(msg, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 msg_set_mc_netid(msg, tipc_net_id);
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400325 msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
326 msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
327 msg_set_bcgap_to(msg, n_ptr->bclink.deferred_head
328 ? buf_seqno(n_ptr->bclink.deferred_head) - 1
329 : n_ptr->bclink.last_sent);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330
Ying Xued69afc92014-05-05 08:56:15 +0800331 tipc_bclink_lock();
Ying Xue7a2f7d12014-04-21 10:55:46 +0800332 tipc_bearer_send(MAX_BEARERS, buf, NULL);
Allan Stephens0f385132011-04-07 15:47:48 -0400333 bcl->stats.sent_nacks++;
Ying Xued69afc92014-05-05 08:56:15 +0800334 tipc_bclink_unlock();
Allan Stephens5f6d9122011-11-04 13:24:29 -0400335 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400337 n_ptr->bclink.oos_state++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 }
339}
340
Ben Hutchings2c530402012-07-10 10:55:09 +0000341/**
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400342 * bclink_peek_nack - monitor retransmission requests sent by other nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 *
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400344 * Delay any upcoming NACK by this node if another node has already
345 * requested the first message this node is going to ask for.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 */
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400347static void bclink_peek_nack(struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348{
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400349 struct tipc_node *n_ptr = tipc_node_find(msg_destnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400351 if (unlikely(!n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 return;
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400353
Per Liden4323add2006-01-18 00:38:21 +0100354 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355
Ying Xue389dd9b2012-11-16 13:51:30 +0800356 if (n_ptr->bclink.recv_permitted &&
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400357 (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
358 (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
359 n_ptr->bclink.oos_state = 2;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360
Per Liden4323add2006-01-18 00:38:21 +0100361 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362}
363
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400364/*
Ying Xue247f0f32014-02-18 16:06:46 +0800365 * tipc_bclink_xmit - broadcast a packet to all nodes in cluster
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 */
Ying Xue247f0f32014-02-18 16:06:46 +0800367int tipc_bclink_xmit(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368{
369 int res;
370
Ying Xued69afc92014-05-05 08:56:15 +0800371 tipc_bclink_lock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372
Allan Stephens2b78f9a2011-10-24 13:05:55 -0400373 if (!bclink->bcast_nodes.count) {
374 res = msg_data_sz(buf_msg(buf));
Allan Stephens5f6d9122011-11-04 13:24:29 -0400375 kfree_skb(buf);
Allan Stephens2b78f9a2011-10-24 13:05:55 -0400376 goto exit;
377 }
378
Ying Xue247f0f32014-02-18 16:06:46 +0800379 res = __tipc_link_xmit(bcl, buf);
Allan Stephens9157baf2011-10-24 13:27:31 -0400380 if (likely(res >= 0)) {
Allan Stephens5b1f7bd2010-08-17 11:00:09 +0000381 bclink_set_last_sent();
Allan Stephens9157baf2011-10-24 13:27:31 -0400382 bcl->stats.queue_sz_counts++;
383 bcl->stats.accu_queue_sz += bcl->out_queue_size;
384 }
Allan Stephens2b78f9a2011-10-24 13:05:55 -0400385exit:
Ying Xued69afc92014-05-05 08:56:15 +0800386 tipc_bclink_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 return res;
388}
389
Ben Hutchings2c530402012-07-10 10:55:09 +0000390/**
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400391 * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
392 *
Ying Xued69afc92014-05-05 08:56:15 +0800393 * Called with both sending node's lock and bclink_lock taken.
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400394 */
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400395static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
396{
397 bclink_update_last_sent(node, seqno);
398 node->bclink.last_in = seqno;
399 node->bclink.oos_state = 0;
400 bcl->stats.recv_info++;
401
402 /*
403 * Unicast an ACK periodically, ensuring that
404 * all nodes in the cluster don't ACK at the same time
405 */
406
407 if (((seqno - tipc_own_addr) % TIPC_MIN_LINK_WIN) == 0) {
Ying Xue247f0f32014-02-18 16:06:46 +0800408 tipc_link_proto_xmit(node->active_links[node->addr & 1],
409 STATE_MSG, 0, 0, 0, 0, 0);
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400410 bcl->stats.sent_acks++;
411 }
412}
413
Ben Hutchings2c530402012-07-10 10:55:09 +0000414/**
Ying Xue247f0f32014-02-18 16:06:46 +0800415 * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900416 *
Ying Xue7216cd92014-04-21 10:55:48 +0800417 * RCU is locked, no other locks set
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 */
Ying Xue247f0f32014-02-18 16:06:46 +0800419void tipc_bclink_rcv(struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700420{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens5d3c4882011-04-07 13:57:25 -0400422 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 u32 next_in;
424 u32 seqno;
Allan Stephens8a275a62011-10-26 15:33:44 -0400425 int deferred;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426
Allan Stephens5d3c4882011-04-07 13:57:25 -0400427 /* Screen out unwanted broadcast messages */
428
429 if (msg_mc_netid(msg) != tipc_net_id)
430 goto exit;
431
432 node = tipc_node_find(msg_prevnode(msg));
433 if (unlikely(!node))
434 goto exit;
435
436 tipc_node_lock(node);
Ying Xue389dd9b2012-11-16 13:51:30 +0800437 if (unlikely(!node->bclink.recv_permitted))
Allan Stephens5d3c4882011-04-07 13:57:25 -0400438 goto unlock;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439
Allan Stephens8a275a62011-10-26 15:33:44 -0400440 /* Handle broadcast protocol message */
441
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
Allan Stephens9f6bdcd42011-04-07 14:57:53 -0400443 if (msg_type(msg) != STATE_MSG)
444 goto unlock;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 if (msg_destnode(msg) == tipc_own_addr) {
Per Liden4323add2006-01-18 00:38:21 +0100446 tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
447 tipc_node_unlock(node);
Ying Xued69afc92014-05-05 08:56:15 +0800448 tipc_bclink_lock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 bcl->stats.recv_nacks++;
Allan Stephens01d83ed2011-01-18 13:53:16 -0500450 bclink->retransmit_to = node;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 bclink_retransmit_pkt(msg_bcgap_after(msg),
452 msg_bcgap_to(msg));
Ying Xued69afc92014-05-05 08:56:15 +0800453 tipc_bclink_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 } else {
Allan Stephens5d3c4882011-04-07 13:57:25 -0400455 tipc_node_unlock(node);
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400456 bclink_peek_nack(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 }
Allan Stephens5d3c4882011-04-07 13:57:25 -0400458 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 }
460
Allan Stephens5d3c4882011-04-07 13:57:25 -0400461 /* Handle in-sequence broadcast message */
462
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 seqno = msg_seqno(msg);
Allan Stephens8a275a62011-10-26 15:33:44 -0400464 next_in = mod(node->bclink.last_in + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465
466 if (likely(seqno == next_in)) {
Allan Stephens8a275a62011-10-26 15:33:44 -0400467receive:
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400468 /* Deliver message to destination */
469
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 if (likely(msg_isdata(msg))) {
Ying Xued69afc92014-05-05 08:56:15 +0800471 tipc_bclink_lock();
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400472 bclink_accept_pkt(node, seqno);
Ying Xued69afc92014-05-05 08:56:15 +0800473 tipc_bclink_unlock();
Per Liden4323add2006-01-18 00:38:21 +0100474 tipc_node_unlock(node);
Allan Stephens9f6bdcd42011-04-07 14:57:53 -0400475 if (likely(msg_mcast(msg)))
Ying Xue247f0f32014-02-18 16:06:46 +0800476 tipc_port_mcast_rcv(buf, NULL);
Allan Stephens9f6bdcd42011-04-07 14:57:53 -0400477 else
Allan Stephens5f6d9122011-11-04 13:24:29 -0400478 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 } else if (msg_user(msg) == MSG_BUNDLER) {
Ying Xued69afc92014-05-05 08:56:15 +0800480 tipc_bclink_lock();
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400481 bclink_accept_pkt(node, seqno);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 bcl->stats.recv_bundles++;
483 bcl->stats.recv_bundled += msg_msgcnt(msg);
Ying Xued69afc92014-05-05 08:56:15 +0800484 tipc_bclink_unlock();
Per Liden4323add2006-01-18 00:38:21 +0100485 tipc_node_unlock(node);
Ying Xue247f0f32014-02-18 16:06:46 +0800486 tipc_link_bundle_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 } else if (msg_user(msg) == MSG_FRAGMENTER) {
Erik Hugne40ba3cd2013-11-06 09:28:06 +0100488 int ret;
Jon Paul Maloy03b92012014-02-13 17:29:05 -0500489 ret = tipc_link_frag_rcv(&node->bclink.reasm_head,
490 &node->bclink.reasm_tail,
491 &buf);
Erik Hugne40ba3cd2013-11-06 09:28:06 +0100492 if (ret == LINK_REASM_ERROR)
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400493 goto unlock;
Ying Xued69afc92014-05-05 08:56:15 +0800494 tipc_bclink_lock();
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400495 bclink_accept_pkt(node, seqno);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 bcl->stats.recv_fragments++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +0100497 if (ret == LINK_REASM_COMPLETE) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 bcl->stats.recv_fragmented++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +0100499 /* Point msg to inner header */
500 msg = buf_msg(buf);
Ying Xued69afc92014-05-05 08:56:15 +0800501 tipc_bclink_unlock();
Erik Hugne528f6f42013-11-06 09:28:05 +0100502 goto receive;
503 }
Ying Xued69afc92014-05-05 08:56:15 +0800504 tipc_bclink_unlock();
Per Liden4323add2006-01-18 00:38:21 +0100505 tipc_node_unlock(node);
Allan Stephens9f6bdcd42011-04-07 14:57:53 -0400506 } else if (msg_user(msg) == NAME_DISTRIBUTOR) {
Ying Xued69afc92014-05-05 08:56:15 +0800507 tipc_bclink_lock();
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400508 bclink_accept_pkt(node, seqno);
Ying Xued69afc92014-05-05 08:56:15 +0800509 tipc_bclink_unlock();
Allan Stephens9f6bdcd42011-04-07 14:57:53 -0400510 tipc_node_unlock(node);
Ying Xue247f0f32014-02-18 16:06:46 +0800511 tipc_named_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 } else {
Ying Xued69afc92014-05-05 08:56:15 +0800513 tipc_bclink_lock();
Allan Stephens63e7f1a2011-10-27 16:43:09 -0400514 bclink_accept_pkt(node, seqno);
Ying Xued69afc92014-05-05 08:56:15 +0800515 tipc_bclink_unlock();
Per Liden4323add2006-01-18 00:38:21 +0100516 tipc_node_unlock(node);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400517 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 }
Allan Stephens5d3c4882011-04-07 13:57:25 -0400519 buf = NULL;
Allan Stephens8a275a62011-10-26 15:33:44 -0400520
521 /* Determine new synchronization state */
522
Allan Stephens5d3c4882011-04-07 13:57:25 -0400523 tipc_node_lock(node);
Allan Stephens8a275a62011-10-26 15:33:44 -0400524 if (unlikely(!tipc_node_is_up(node)))
525 goto unlock;
526
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400527 if (node->bclink.last_in == node->bclink.last_sent)
Allan Stephens8a275a62011-10-26 15:33:44 -0400528 goto unlock;
529
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400530 if (!node->bclink.deferred_head) {
531 node->bclink.oos_state = 1;
532 goto unlock;
533 }
534
Allan Stephens8a275a62011-10-26 15:33:44 -0400535 msg = buf_msg(node->bclink.deferred_head);
536 seqno = msg_seqno(msg);
537 next_in = mod(next_in + 1);
538 if (seqno != next_in)
539 goto unlock;
540
541 /* Take in-sequence message from deferred queue & deliver it */
542
543 buf = node->bclink.deferred_head;
544 node->bclink.deferred_head = buf->next;
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400545 node->bclink.deferred_size--;
Allan Stephens8a275a62011-10-26 15:33:44 -0400546 goto receive;
547 }
548
549 /* Handle out-of-sequence broadcast message */
550
551 if (less(next_in, seqno)) {
Allan Stephens8a275a62011-10-26 15:33:44 -0400552 deferred = tipc_link_defer_pkt(&node->bclink.deferred_head,
553 &node->bclink.deferred_tail,
554 buf);
Allan Stephens7a54d4a2011-10-27 14:17:53 -0400555 node->bclink.deferred_size += deferred;
556 bclink_update_last_sent(node, seqno);
Allan Stephens5d3c4882011-04-07 13:57:25 -0400557 buf = NULL;
Allan Stephens8a275a62011-10-26 15:33:44 -0400558 } else
559 deferred = 0;
560
Ying Xued69afc92014-05-05 08:56:15 +0800561 tipc_bclink_lock();
Allan Stephensb98158e2011-10-26 16:13:35 -0400562
Allan Stephens8a275a62011-10-26 15:33:44 -0400563 if (deferred)
564 bcl->stats.deferred_recv++;
Allan Stephens0232c5a2011-10-26 15:57:26 -0400565 else
566 bcl->stats.duplicates++;
Allan Stephens8a275a62011-10-26 15:33:44 -0400567
Ying Xued69afc92014-05-05 08:56:15 +0800568 tipc_bclink_unlock();
Allan Stephensb98158e2011-10-26 16:13:35 -0400569
Allan Stephens5d3c4882011-04-07 13:57:25 -0400570unlock:
Per Liden4323add2006-01-18 00:38:21 +0100571 tipc_node_unlock(node);
Allan Stephens5d3c4882011-04-07 13:57:25 -0400572exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400573 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574}
575
David S. Miller6c000552008-09-02 23:38:32 -0700576u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577{
Ying Xue389dd9b2012-11-16 13:51:30 +0800578 return (n_ptr->bclink.recv_permitted &&
Per Liden4323add2006-01-18 00:38:21 +0100579 (tipc_bclink_get_last_sent() != n_ptr->bclink.acked));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580}
581
582
583/**
Per Liden4323add2006-01-18 00:38:21 +0100584 * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900585 *
Allan Stephens2ff9f922011-04-07 10:44:54 -0400586 * Send packet over as many bearers as necessary to reach all nodes
587 * that have joined the broadcast link.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900588 *
Allan Stephens2ff9f922011-04-07 10:44:54 -0400589 * Returns 0 (packet sent successfully) under all circumstances,
590 * since the broadcast link's pseudo-bearer never blocks
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400592static int tipc_bcbearer_send(struct sk_buff *buf, struct tipc_bearer *unused1,
Adrian Bunk988f0882006-03-20 22:37:52 -0800593 struct tipc_media_addr *unused2)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 int bp_index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596
Gerlando Falautoe6160712013-05-01 12:04:44 +0000597 /* Prepare broadcast link message for reliable transmission,
Allan Stephens2ff9f922011-04-07 10:44:54 -0400598 * if first time trying to send it;
599 * preparation is skipped for broadcast link protocol messages
600 * since they are sent in an unreliable manner and don't need it
601 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 if (likely(!msg_non_seq(buf_msg(buf)))) {
603 struct tipc_msg *msg;
604
Allan Stephenscd3decd2011-10-24 11:18:12 -0400605 bcbuf_set_acks(buf, bclink->bcast_nodes.count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 msg = buf_msg(buf);
Allan Stephens40aecb12008-06-04 17:54:48 -0700607 msg_set_non_seq(msg, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 msg_set_mc_netid(msg, tipc_net_id);
Allan Stephens0048b822010-08-17 11:00:10 +0000609 bcl->stats.sent_info++;
Allan Stephens5e726902011-05-23 13:14:18 -0400610
Allan Stephenscd3decd2011-10-24 11:18:12 -0400611 if (WARN_ON(!bclink->bcast_nodes.count)) {
Allan Stephens5e726902011-05-23 13:14:18 -0400612 dump_stack();
613 return 0;
614 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 }
616
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 /* Send buffer over bearers until all targets reached */
Allan Stephenscd3decd2011-10-24 11:18:12 -0400618 bcbearer->remains = bclink->bcast_nodes;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619
620 for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
Allan Stephens2d627b92011-01-07 13:00:11 -0500621 struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
622 struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
Gerlando Falauto77861d92013-05-01 12:04:45 +0000623 struct tipc_bearer *b = p;
Gerlando Falauto488fc9a2013-05-01 12:04:46 +0000624 struct sk_buff *tbuf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625
626 if (!p)
Gerlando Falautoe6160712013-05-01 12:04:44 +0000627 break; /* No more bearers to try */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628
Gerlando Falauto77861d92013-05-01 12:04:45 +0000629 tipc_nmap_diff(&bcbearer->remains, &b->nodes,
Gerlando Falautoe6160712013-05-01 12:04:44 +0000630 &bcbearer->remains_new);
Allan Stephens65f51ef2006-06-25 23:53:20 -0700631 if (bcbearer->remains_new.count == bcbearer->remains.count)
Gerlando Falautoe6160712013-05-01 12:04:44 +0000632 continue; /* Nothing added by bearer pair */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633
Gerlando Falauto488fc9a2013-05-01 12:04:46 +0000634 if (bp_index == 0) {
635 /* Use original buffer for first bearer */
Ying Xue7a2f7d12014-04-21 10:55:46 +0800636 tipc_bearer_send(b->identity, buf, &b->bcast_addr);
Gerlando Falauto488fc9a2013-05-01 12:04:46 +0000637 } else {
638 /* Avoid concurrent buffer access */
639 tbuf = pskb_copy(buf, GFP_ATOMIC);
640 if (!tbuf)
641 break;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800642 tipc_bearer_send(b->identity, tbuf, &b->bcast_addr);
Gerlando Falauto488fc9a2013-05-01 12:04:46 +0000643 kfree_skb(tbuf); /* Bearer keeps a clone */
644 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645
Gerlando Falautoe6160712013-05-01 12:04:44 +0000646 /* Swap bearers for next packet */
Neil Horman1a624832010-03-15 08:02:24 +0000647 if (s) {
648 bcbearer->bpairs[bp_index].primary = s;
649 bcbearer->bpairs[bp_index].secondary = p;
650 }
651
Allan Stephens65f51ef2006-06-25 23:53:20 -0700652 if (bcbearer->remains_new.count == 0)
Gerlando Falautoe6160712013-05-01 12:04:44 +0000653 break; /* All targets reached */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654
Allan Stephens65f51ef2006-06-25 23:53:20 -0700655 bcbearer->remains = bcbearer->remains_new;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900657
Allan Stephens2ff9f922011-04-07 10:44:54 -0400658 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659}
660
661/**
Per Liden4323add2006-01-18 00:38:21 +0100662 * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 */
Ying Xue28dd9412014-04-21 10:55:51 +0800664void tipc_bcbearer_sort(struct tipc_node_map *nm_ptr, u32 node, bool action)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665{
Paul Gortmaker7f9ab6a2011-12-29 20:55:27 -0500666 struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
667 struct tipc_bcbearer_pair *bp_curr;
Ying Xuef8322df2014-04-21 10:55:45 +0800668 struct tipc_bearer *b;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 int b_index;
670 int pri;
671
Ying Xued69afc92014-05-05 08:56:15 +0800672 tipc_bclink_lock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673
Ying Xue28dd9412014-04-21 10:55:51 +0800674 if (action)
675 tipc_nmap_add(nm_ptr, node);
676 else
677 tipc_nmap_remove(nm_ptr, node);
678
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 /* Group bearers by priority (can assume max of two per priority) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
681
Ying Xuef8322df2014-04-21 10:55:45 +0800682 rcu_read_lock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
Ying Xuef8322df2014-04-21 10:55:45 +0800684 b = rcu_dereference_rtnl(bearer_list[b_index]);
Ying Xuef47de122014-03-27 12:54:34 +0800685 if (!b || !b->nodes.count)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 continue;
687
688 if (!bp_temp[b->priority].primary)
689 bp_temp[b->priority].primary = b;
690 else
691 bp_temp[b->priority].secondary = b;
692 }
Ying Xuef8322df2014-04-21 10:55:45 +0800693 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694
695 /* Create array of bearer pairs for broadcasting */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 bp_curr = bcbearer->bpairs;
697 memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
698
Per Liden16cb4b32006-01-13 22:22:22 +0100699 for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700
701 if (!bp_temp[pri].primary)
702 continue;
703
704 bp_curr->primary = bp_temp[pri].primary;
705
706 if (bp_temp[pri].secondary) {
Per Liden4323add2006-01-18 00:38:21 +0100707 if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
708 &bp_temp[pri].secondary->nodes)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 bp_curr->secondary = bp_temp[pri].secondary;
710 } else {
711 bp_curr++;
712 bp_curr->primary = bp_temp[pri].secondary;
713 }
714 }
715
716 bp_curr++;
717 }
718
Ying Xued69afc92014-05-05 08:56:15 +0800719 tipc_bclink_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720}
721
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722
Per Liden4323add2006-01-18 00:38:21 +0100723int tipc_bclink_stats(char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724{
Erik Hugnedc1aed32012-06-29 00:50:23 -0400725 int ret;
726 struct tipc_stats *s;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727
728 if (!bcl)
729 return 0;
730
Ying Xued69afc92014-05-05 08:56:15 +0800731 tipc_bclink_lock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732
Erik Hugnedc1aed32012-06-29 00:50:23 -0400733 s = &bcl->stats;
734
735 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
736 " Window:%u packets\n",
737 bcl->name, bcl->queue_limit[0]);
738 ret += tipc_snprintf(buf + ret, buf_size - ret,
739 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
740 s->recv_info, s->recv_fragments,
741 s->recv_fragmented, s->recv_bundles,
742 s->recv_bundled);
743 ret += tipc_snprintf(buf + ret, buf_size - ret,
744 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
745 s->sent_info, s->sent_fragments,
746 s->sent_fragmented, s->sent_bundles,
747 s->sent_bundled);
748 ret += tipc_snprintf(buf + ret, buf_size - ret,
749 " RX naks:%u defs:%u dups:%u\n",
750 s->recv_nacks, s->deferred_recv, s->duplicates);
751 ret += tipc_snprintf(buf + ret, buf_size - ret,
752 " TX naks:%u acks:%u dups:%u\n",
753 s->sent_nacks, s->sent_acks, s->retransmitted);
754 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +0800755 " Congestion link:%u Send queue max:%u avg:%u\n",
756 s->link_congs, s->max_queue_sz,
Erik Hugnedc1aed32012-06-29 00:50:23 -0400757 s->queue_sz_counts ?
758 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759
Ying Xued69afc92014-05-05 08:56:15 +0800760 tipc_bclink_unlock();
Erik Hugnedc1aed32012-06-29 00:50:23 -0400761 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762}
763
Per Liden4323add2006-01-18 00:38:21 +0100764int tipc_bclink_reset_stats(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765{
766 if (!bcl)
767 return -ENOPROTOOPT;
768
Ying Xued69afc92014-05-05 08:56:15 +0800769 tipc_bclink_lock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770 memset(&bcl->stats, 0, sizeof(bcl->stats));
Ying Xued69afc92014-05-05 08:56:15 +0800771 tipc_bclink_unlock();
Allan Stephens0e35fd52008-07-14 22:44:01 -0700772 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773}
774
Per Liden4323add2006-01-18 00:38:21 +0100775int tipc_bclink_set_queue_limits(u32 limit)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776{
777 if (!bcl)
778 return -ENOPROTOOPT;
779 if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
780 return -EINVAL;
781
Ying Xued69afc92014-05-05 08:56:15 +0800782 tipc_bclink_lock();
Per Liden4323add2006-01-18 00:38:21 +0100783 tipc_link_set_queue_limits(bcl, limit);
Ying Xued69afc92014-05-05 08:56:15 +0800784 tipc_bclink_unlock();
Allan Stephens0e35fd52008-07-14 22:44:01 -0700785 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786}
787
Ying Xueeb8b00f2014-05-05 08:56:16 +0800788int tipc_bclink_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789{
Ying Xueeb8b00f2014-05-05 08:56:16 +0800790 bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
791 if (!bcbearer)
792 return -ENOMEM;
793
794 bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
795 if (!bclink) {
796 kfree(bcbearer);
797 return -ENOMEM;
798 }
799
800 bcl = &bclink->link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 bcbearer->bearer.media = &bcbearer->media;
Per Liden4323add2006-01-18 00:38:21 +0100802 bcbearer->media.send_msg = tipc_bcbearer_send;
Allan Stephens2e2d9be2011-04-07 10:22:31 -0400803 sprintf(bcbearer->media.name, "tipc-broadcast");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804
Ying Xued69afc92014-05-05 08:56:15 +0800805 spin_lock_init(&bclink->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 INIT_LIST_HEAD(&bcl->waiting_ports);
807 bcl->next_out_no = 1;
Ingo Molnar34af9462006-06-27 02:53:55 -0700808 spin_lock_init(&bclink->node.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 bcl->owner = &bclink->node;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900810 bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
Per Liden4323add2006-01-18 00:38:21 +0100811 tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800812 bcl->bearer_id = MAX_BEARERS;
Ying Xuef8322df2014-04-21 10:55:45 +0800813 rcu_assign_pointer(bearer_list[MAX_BEARERS], &bcbearer->bearer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 bcl->state = WORKING_WORKING;
Stephen Hemminger4b704d52009-03-18 19:11:29 -0700815 strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
Ying Xueeb8b00f2014-05-05 08:56:16 +0800816 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817}
818
Per Liden4323add2006-01-18 00:38:21 +0100819void tipc_bclink_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820{
Ying Xued69afc92014-05-05 08:56:15 +0800821 tipc_bclink_lock();
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500822 tipc_link_purge_queues(bcl);
Ying Xued69afc92014-05-05 08:56:15 +0800823 tipc_bclink_unlock();
Allan Stephensc47e9b92011-10-24 10:29:26 -0400824
Ying Xuef8322df2014-04-21 10:55:45 +0800825 RCU_INIT_POINTER(bearer_list[BCBEARER], NULL);
Ying Xueeb8b00f2014-05-05 08:56:16 +0800826 synchronize_net();
827 kfree(bcbearer);
828 kfree(bclink);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829}
830
Allan Stephens3e22e622010-05-11 14:30:14 +0000831/**
832 * tipc_nmap_add - add a node to a node map
833 */
Ying Xue28dd9412014-04-21 10:55:51 +0800834static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
Allan Stephens3e22e622010-05-11 14:30:14 +0000835{
836 int n = tipc_node(node);
837 int w = n / WSIZE;
838 u32 mask = (1 << (n % WSIZE));
839
840 if ((nm_ptr->map[w] & mask) == 0) {
841 nm_ptr->count++;
842 nm_ptr->map[w] |= mask;
843 }
844}
845
846/**
847 * tipc_nmap_remove - remove a node from a node map
848 */
Ying Xue28dd9412014-04-21 10:55:51 +0800849static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
Allan Stephens3e22e622010-05-11 14:30:14 +0000850{
851 int n = tipc_node(node);
852 int w = n / WSIZE;
853 u32 mask = (1 << (n % WSIZE));
854
855 if ((nm_ptr->map[w] & mask) != 0) {
856 nm_ptr->map[w] &= ~mask;
857 nm_ptr->count--;
858 }
859}
860
861/**
862 * tipc_nmap_diff - find differences between node maps
863 * @nm_a: input node map A
864 * @nm_b: input node map B
865 * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
866 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000867static void tipc_nmap_diff(struct tipc_node_map *nm_a,
868 struct tipc_node_map *nm_b,
869 struct tipc_node_map *nm_diff)
Allan Stephens3e22e622010-05-11 14:30:14 +0000870{
871 int stop = ARRAY_SIZE(nm_a->map);
872 int w;
873 int b;
874 u32 map;
875
876 memset(nm_diff, 0, sizeof(*nm_diff));
877 for (w = 0; w < stop; w++) {
878 map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
879 nm_diff->map[w] = map;
880 if (map != 0) {
881 for (b = 0 ; b < WSIZE; b++) {
882 if (map & (1 << b))
883 nm_diff->count++;
884 }
885 }
886 }
887}
Allan Stephens43608ed2010-05-11 14:30:15 +0000888
889/**
890 * tipc_port_list_add - add a port to a port list, ensuring no duplicates
891 */
Paul Gortmaker45843102011-12-29 20:33:30 -0500892void tipc_port_list_add(struct tipc_port_list *pl_ptr, u32 port)
Allan Stephens43608ed2010-05-11 14:30:15 +0000893{
Paul Gortmaker45843102011-12-29 20:33:30 -0500894 struct tipc_port_list *item = pl_ptr;
Allan Stephens43608ed2010-05-11 14:30:15 +0000895 int i;
896 int item_sz = PLSIZE;
897 int cnt = pl_ptr->count;
898
899 for (; ; cnt -= item_sz, item = item->next) {
900 if (cnt < PLSIZE)
901 item_sz = cnt;
902 for (i = 0; i < item_sz; i++)
903 if (item->ports[i] == port)
904 return;
905 if (i < PLSIZE) {
906 item->ports[i] = port;
907 pl_ptr->count++;
908 return;
909 }
910 if (!item->next) {
911 item->next = kmalloc(sizeof(*item), GFP_ATOMIC);
912 if (!item->next) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400913 pr_warn("Incomplete multicast delivery, no memory\n");
Allan Stephens43608ed2010-05-11 14:30:15 +0000914 return;
915 }
916 item->next->next = NULL;
917 }
918 }
919}
920
921/**
922 * tipc_port_list_free - free dynamically created entries in port_list chain
923 *
924 */
Paul Gortmaker45843102011-12-29 20:33:30 -0500925void tipc_port_list_free(struct tipc_port_list *pl_ptr)
Allan Stephens43608ed2010-05-11 14:30:15 +0000926{
Paul Gortmaker45843102011-12-29 20:33:30 -0500927 struct tipc_port_list *item;
928 struct tipc_port_list *next;
Allan Stephens43608ed2010-05-11 14:30:15 +0000929
930 for (item = pl_ptr->next; item; item = next) {
931 next = item->next;
932 kfree(item);
933 }
934}