]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/tipc/bcast.c
Merge branch 'for-4.3/upstream' into for-4.3/wacom
[mirror_ubuntu-artful-kernel.git] / net / tipc / bcast.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/bcast.c: TIPC broadcast code
c4307285 3 *
3c724acd 4 * Copyright (c) 2004-2006, 2014-2015, Ericsson AB
b97bf3fd 5 * Copyright (c) 2004, Intel Corporation.
2d627b92 6 * Copyright (c) 2005, 2010-2011, Wind River Systems
b97bf3fd
PL
7 * All rights reserved.
8 *
9ea1fd3c 9 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
10 * modification, are permitted provided that the following conditions are met:
11 *
9ea1fd3c
PL
12 * 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.
b97bf3fd 20 *
9ea1fd3c
PL
21 * 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
b97bf3fd
PL
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
078bec82
JPM
38#include "socket.h"
39#include "msg.h"
b97bf3fd 40#include "bcast.h"
9f6bdcd4 41#include "name_distr.h"
1da46568 42#include "core.h"
b97bf3fd 43
987b58be
YX
44#define MAX_PKT_DEFAULT_MCAST 1500 /* bcast link max packet size (fixed) */
45#define BCLINK_WIN_DEFAULT 20 /* bcast link window size (default) */
b97bf3fd 46
3aec9cc9 47const char tipc_bclink_name[] = "broadcast-link";
b97bf3fd 48
31e3c3f6 49static void tipc_nmap_diff(struct tipc_node_map *nm_a,
50 struct tipc_node_map *nm_b,
51 struct tipc_node_map *nm_diff);
28dd9418
YX
52static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node);
53static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node);
b97bf3fd 54
1da46568 55static void tipc_bclink_lock(struct net *net)
d69afc90 56{
1da46568
YX
57 struct tipc_net *tn = net_generic(net, tipc_net_id);
58
59 spin_lock_bh(&tn->bclink->lock);
d69afc90
YX
60}
61
1da46568 62static void tipc_bclink_unlock(struct net *net)
d69afc90 63{
1da46568 64 struct tipc_net *tn = net_generic(net, tipc_net_id);
3f5a12bd 65
1da46568 66 spin_unlock_bh(&tn->bclink->lock);
3f5a12bd
YX
67}
68
cb1b7280
JPM
69void tipc_bclink_input(struct net *net)
70{
71 struct tipc_net *tn = net_generic(net, tipc_net_id);
72
73 tipc_sk_mcast_rcv(net, &tn->bclink->arrvq, &tn->bclink->inputq);
74}
75
078bec82
JPM
76uint tipc_bclink_get_mtu(void)
77{
78 return MAX_PKT_DEFAULT_MCAST;
79}
80
05790c64 81static u32 bcbuf_acks(struct sk_buff *buf)
b97bf3fd 82{
880b005f 83 return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
b97bf3fd
PL
84}
85
05790c64 86static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
b97bf3fd 87{
880b005f 88 TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
b97bf3fd
PL
89}
90
05790c64 91static void bcbuf_decr_acks(struct sk_buff *buf)
b97bf3fd
PL
92{
93 bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
94}
95
1da46568 96void tipc_bclink_add_node(struct net *net, u32 addr)
cd3decdf 97{
1da46568
YX
98 struct tipc_net *tn = net_generic(net, tipc_net_id);
99
100 tipc_bclink_lock(net);
101 tipc_nmap_add(&tn->bclink->bcast_nodes, addr);
102 tipc_bclink_unlock(net);
cd3decdf
AS
103}
104
1da46568 105void tipc_bclink_remove_node(struct net *net, u32 addr)
cd3decdf 106{
1da46568
YX
107 struct tipc_net *tn = net_generic(net, tipc_net_id);
108
109 tipc_bclink_lock(net);
110 tipc_nmap_remove(&tn->bclink->bcast_nodes, addr);
111 tipc_bclink_unlock(net);
cd3decdf 112}
b97bf3fd 113
1da46568 114static void bclink_set_last_sent(struct net *net)
5b1f7bde 115{
1da46568
YX
116 struct tipc_net *tn = net_generic(net, tipc_net_id);
117 struct tipc_link *bcl = tn->bcl;
05dcc5aa 118 struct sk_buff *skb = skb_peek(&bcl->backlogq);
1da46568 119
05dcc5aa
JPM
120 if (skb)
121 bcl->fsm_msg_cnt = mod(buf_seqno(skb) - 1);
5b1f7bde
AS
122 else
123 bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
124}
125
1da46568 126u32 tipc_bclink_get_last_sent(struct net *net)
5b1f7bde 127{
1da46568
YX
128 struct tipc_net *tn = net_generic(net, tipc_net_id);
129
130 return tn->bcl->fsm_msg_cnt;
5b1f7bde
AS
131}
132
7a54d4a9 133static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
b97bf3fd 134{
7a54d4a9
AS
135 node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
136 seqno : node->bclink.last_sent;
b97bf3fd
PL
137}
138
2c53040f 139/**
01d83edd
AS
140 * tipc_bclink_retransmit_to - get most recent node to request retransmission
141 *
d69afc90 142 * Called with bclink_lock locked
01d83edd 143 */
1da46568 144struct tipc_node *tipc_bclink_retransmit_to(struct net *net)
01d83edd 145{
1da46568
YX
146 struct tipc_net *tn = net_generic(net, tipc_net_id);
147
148 return tn->bclink->retransmit_to;
01d83edd
AS
149}
150
c4307285 151/**
b97bf3fd
PL
152 * bclink_retransmit_pkt - retransmit broadcast packets
153 * @after: sequence number of last packet to *not* retransmit
154 * @to: sequence number of last packet to retransmit
c4307285 155 *
d69afc90 156 * Called with bclink_lock locked
b97bf3fd 157 */
1da46568 158static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
b97bf3fd 159{
58dc55f2 160 struct sk_buff *skb;
1da46568 161 struct tipc_link *bcl = tn->bcl;
b97bf3fd 162
05dcc5aa 163 skb_queue_walk(&bcl->transmq, skb) {
703068ee
JM
164 if (more(buf_seqno(skb), after)) {
165 tipc_link_retransmit(bcl, skb, mod(to - after));
58dc55f2 166 break;
703068ee 167 }
58dc55f2 168 }
b97bf3fd
PL
169}
170
908344cd
JM
171/**
172 * tipc_bclink_wakeup_users - wake up pending users
173 *
174 * Called with no locks taken
175 */
f2f9800d 176void tipc_bclink_wakeup_users(struct net *net)
908344cd 177{
1da46568 178 struct tipc_net *tn = net_generic(net, tipc_net_id);
4988bb4a 179
c637c103 180 tipc_sk_rcv(net, &tn->bclink->link.wakeupq);
908344cd
JM
181}
182
c4307285 183/**
4323add6 184 * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
b97bf3fd
PL
185 * @n_ptr: node that sent acknowledgement info
186 * @acked: broadcast sequence # that has been acknowledged
c4307285 187 *
d69afc90 188 * Node is locked, bclink_lock unlocked.
b97bf3fd 189 */
6c00055a 190void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
b97bf3fd 191{
58dc55f2 192 struct sk_buff *skb, *tmp;
b97bf3fd 193 unsigned int released = 0;
1da46568
YX
194 struct net *net = n_ptr->net;
195 struct tipc_net *tn = net_generic(net, tipc_net_id);
b97bf3fd 196
2cdf3918
JPM
197 if (unlikely(!n_ptr->bclink.recv_permitted))
198 return;
199
1da46568 200 tipc_bclink_lock(net);
2cdf3918 201
36559591 202 /* Bail out if tx queue is empty (no clean up is required) */
05dcc5aa 203 skb = skb_peek(&tn->bcl->transmq);
58dc55f2 204 if (!skb)
36559591
AS
205 goto exit;
206
207 /* Determine which messages need to be acknowledged */
208 if (acked == INVALID_LINK_SEQ) {
209 /*
210 * Contact with specified node has been lost, so need to
211 * acknowledge sent messages only (if other nodes still exist)
212 * or both sent and unsent messages (otherwise)
213 */
1da46568
YX
214 if (tn->bclink->bcast_nodes.count)
215 acked = tn->bcl->fsm_msg_cnt;
36559591 216 else
1da46568 217 acked = tn->bcl->next_out_no;
36559591
AS
218 } else {
219 /*
220 * Bail out if specified sequence number does not correspond
221 * to a message that has been sent and not yet acknowledged
222 */
58dc55f2 223 if (less(acked, buf_seqno(skb)) ||
1da46568 224 less(tn->bcl->fsm_msg_cnt, acked) ||
36559591
AS
225 less_eq(acked, n_ptr->bclink.acked))
226 goto exit;
227 }
228
229 /* Skip over packets that node has previously acknowledged */
05dcc5aa 230 skb_queue_walk(&tn->bcl->transmq, skb) {
58dc55f2
YX
231 if (more(buf_seqno(skb), n_ptr->bclink.acked))
232 break;
233 }
b97bf3fd
PL
234
235 /* Update packets that node is now acknowledging */
05dcc5aa 236 skb_queue_walk_from_safe(&tn->bcl->transmq, skb, tmp) {
58dc55f2
YX
237 if (more(buf_seqno(skb), acked))
238 break;
05dcc5aa
JPM
239 bcbuf_decr_acks(skb);
240 bclink_set_last_sent(net);
58dc55f2 241 if (bcbuf_acks(skb) == 0) {
05dcc5aa 242 __skb_unlink(skb, &tn->bcl->transmq);
58dc55f2 243 kfree_skb(skb);
b97bf3fd
PL
244 released = 1;
245 }
b97bf3fd
PL
246 }
247 n_ptr->bclink.acked = acked;
248
249 /* Try resolving broadcast link congestion, if necessary */
05dcc5aa 250 if (unlikely(skb_peek(&tn->bcl->backlogq))) {
1da46568
YX
251 tipc_link_push_packets(tn->bcl);
252 bclink_set_last_sent(net);
5b1f7bde 253 }
c637c103 254 if (unlikely(released && !skb_queue_empty(&tn->bcl->wakeupq)))
908344cd 255 n_ptr->action_flags |= TIPC_WAKEUP_BCAST_USERS;
36559591 256exit:
1da46568 257 tipc_bclink_unlock(net);
b97bf3fd
PL
258}
259
2c53040f 260/**
7a54d4a9 261 * tipc_bclink_update_link_state - update broadcast link state
c4307285 262 *
7216cd94 263 * RCU and node lock set
b97bf3fd 264 */
c5898636 265void tipc_bclink_update_link_state(struct tipc_node *n_ptr,
c93d3baa 266 u32 last_sent)
b97bf3fd 267{
7a54d4a9 268 struct sk_buff *buf;
c5898636 269 struct net *net = n_ptr->net;
c93d3baa 270 struct tipc_net *tn = net_generic(net, tipc_net_id);
b97bf3fd 271
7a54d4a9 272 /* Ignore "stale" link state info */
7a54d4a9
AS
273 if (less_eq(last_sent, n_ptr->bclink.last_in))
274 return;
b97bf3fd 275
7a54d4a9 276 /* Update link synchronization state; quit if in sync */
7a54d4a9
AS
277 bclink_update_last_sent(n_ptr, last_sent);
278
279 if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
280 return;
281
282 /* Update out-of-sync state; quit if loss is still unconfirmed */
7a54d4a9
AS
283 if ((++n_ptr->bclink.oos_state) == 1) {
284 if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
285 return;
286 n_ptr->bclink.oos_state++;
287 }
288
289 /* Don't NACK if one has been recently sent (or seen) */
7a54d4a9 290 if (n_ptr->bclink.oos_state & 0x1)
b97bf3fd
PL
291 return;
292
7a54d4a9 293 /* Send NACK */
31e3c3f6 294 buf = tipc_buf_acquire(INT_H_SIZE);
b97bf3fd 295 if (buf) {
7a54d4a9 296 struct tipc_msg *msg = buf_msg(buf);
05dcc5aa 297 struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferdq);
bc6fecd4 298 u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
7a54d4a9 299
c5898636 300 tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG,
7a54d4a9 301 INT_H_SIZE, n_ptr->addr);
bf781ecf 302 msg_set_non_seq(msg, 1);
c93d3baa 303 msg_set_mc_netid(msg, tn->net_id);
7a54d4a9
AS
304 msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
305 msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
bc6fecd4 306 msg_set_bcgap_to(msg, to);
b97bf3fd 307
1da46568 308 tipc_bclink_lock(net);
7f9f95d9 309 tipc_bearer_send(net, MAX_BEARERS, buf, NULL);
1da46568
YX
310 tn->bcl->stats.sent_nacks++;
311 tipc_bclink_unlock(net);
5f6d9123 312 kfree_skb(buf);
b97bf3fd 313
7a54d4a9 314 n_ptr->bclink.oos_state++;
b97bf3fd
PL
315 }
316}
317
2c53040f 318/**
7a54d4a9 319 * bclink_peek_nack - monitor retransmission requests sent by other nodes
b97bf3fd 320 *
7a54d4a9
AS
321 * Delay any upcoming NACK by this node if another node has already
322 * requested the first message this node is going to ask for.
b97bf3fd 323 */
f2f9800d 324static void bclink_peek_nack(struct net *net, struct tipc_msg *msg)
b97bf3fd 325{
f2f9800d 326 struct tipc_node *n_ptr = tipc_node_find(net, msg_destnode(msg));
b97bf3fd 327
7a54d4a9 328 if (unlikely(!n_ptr))
b97bf3fd 329 return;
7a54d4a9 330
4323add6 331 tipc_node_lock(n_ptr);
389dd9bc 332 if (n_ptr->bclink.recv_permitted &&
7a54d4a9
AS
333 (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
334 (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
335 n_ptr->bclink.oos_state = 2;
4323add6 336 tipc_node_unlock(n_ptr);
8a0f6ebe 337 tipc_node_put(n_ptr);
b97bf3fd
PL
338}
339
cb1b7280 340/* tipc_bclink_xmit - deliver buffer chain to all nodes in cluster
9fbfb8b1 341 * and to identified node local sockets
f2f9800d 342 * @net: the applicable net namespace
a6ca1094 343 * @list: chain of buffers containing message
078bec82
JPM
344 * Consumes the buffer chain, except when returning -ELINKCONG
345 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
346 */
f2f9800d 347int tipc_bclink_xmit(struct net *net, struct sk_buff_head *list)
078bec82 348{
1da46568
YX
349 struct tipc_net *tn = net_generic(net, tipc_net_id);
350 struct tipc_link *bcl = tn->bcl;
351 struct tipc_bclink *bclink = tn->bclink;
078bec82
JPM
352 int rc = 0;
353 int bc = 0;
a6ca1094 354 struct sk_buff *skb;
cb1b7280
JPM
355 struct sk_buff_head arrvq;
356 struct sk_buff_head inputq;
078bec82
JPM
357
358 /* Prepare clone of message for local node */
a6ca1094
YX
359 skb = tipc_msg_reassemble(list);
360 if (unlikely(!skb)) {
361 __skb_queue_purge(list);
078bec82
JPM
362 return -EHOSTUNREACH;
363 }
cb1b7280 364 /* Broadcast to all nodes */
078bec82 365 if (likely(bclink)) {
1da46568 366 tipc_bclink_lock(net);
078bec82 367 if (likely(bclink->bcast_nodes.count)) {
7f9f95d9 368 rc = __tipc_link_xmit(net, bcl, list);
078bec82 369 if (likely(!rc)) {
05dcc5aa 370 u32 len = skb_queue_len(&bcl->transmq);
58dc55f2 371
1da46568 372 bclink_set_last_sent(net);
078bec82 373 bcl->stats.queue_sz_counts++;
58dc55f2 374 bcl->stats.accu_queue_sz += len;
078bec82
JPM
375 }
376 bc = 1;
377 }
1da46568 378 tipc_bclink_unlock(net);
078bec82
JPM
379 }
380
381 if (unlikely(!bc))
a6ca1094 382 __skb_queue_purge(list);
078bec82 383
cb1b7280 384 if (unlikely(rc)) {
a6ca1094 385 kfree_skb(skb);
cb1b7280
JPM
386 return rc;
387 }
388 /* Deliver message clone */
389 __skb_queue_head_init(&arrvq);
390 skb_queue_head_init(&inputq);
391 __skb_queue_tail(&arrvq, skb);
392 tipc_sk_mcast_rcv(net, &arrvq, &inputq);
078bec82
JPM
393 return rc;
394}
395
2c53040f 396/**
63e7f1ac
AS
397 * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
398 *
d69afc90 399 * Called with both sending node's lock and bclink_lock taken.
63e7f1ac 400 */
63e7f1ac
AS
401static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
402{
1da46568
YX
403 struct tipc_net *tn = net_generic(node->net, tipc_net_id);
404
63e7f1ac
AS
405 bclink_update_last_sent(node, seqno);
406 node->bclink.last_in = seqno;
407 node->bclink.oos_state = 0;
1da46568 408 tn->bcl->stats.recv_info++;
63e7f1ac
AS
409
410 /*
411 * Unicast an ACK periodically, ensuring that
412 * all nodes in the cluster don't ACK at the same time
413 */
34747539 414 if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
247f0f3c 415 tipc_link_proto_xmit(node->active_links[node->addr & 1],
ed193ece 416 STATE_MSG, 0, 0, 0, 0);
1da46568 417 tn->bcl->stats.sent_acks++;
63e7f1ac
AS
418 }
419}
420
2c53040f 421/**
247f0f3c 422 * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
c4307285 423 *
7216cd94 424 * RCU is locked, no other locks set
b97bf3fd 425 */
c93d3baa 426void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
d356eeba 427{
c93d3baa 428 struct tipc_net *tn = net_generic(net, tipc_net_id);
1da46568 429 struct tipc_link *bcl = tn->bcl;
b97bf3fd 430 struct tipc_msg *msg = buf_msg(buf);
5d3c488d 431 struct tipc_node *node;
b97bf3fd
PL
432 u32 next_in;
433 u32 seqno;
0abd8ff2 434 int deferred = 0;
c637c103
JPM
435 int pos = 0;
436 struct sk_buff *iskb;
cb1b7280 437 struct sk_buff_head *arrvq, *inputq;
b97bf3fd 438
5d3c488d 439 /* Screen out unwanted broadcast messages */
c93d3baa 440 if (msg_mc_netid(msg) != tn->net_id)
5d3c488d
AS
441 goto exit;
442
f2f9800d 443 node = tipc_node_find(net, msg_prevnode(msg));
5d3c488d
AS
444 if (unlikely(!node))
445 goto exit;
446
447 tipc_node_lock(node);
389dd9bc 448 if (unlikely(!node->bclink.recv_permitted))
5d3c488d 449 goto unlock;
b97bf3fd 450
8a275a6a 451 /* Handle broadcast protocol message */
b97bf3fd 452 if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
9f6bdcd4
AS
453 if (msg_type(msg) != STATE_MSG)
454 goto unlock;
34747539 455 if (msg_destnode(msg) == tn->own_addr) {
4323add6 456 tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
1da46568 457 tipc_bclink_lock(net);
b97bf3fd 458 bcl->stats.recv_nacks++;
1da46568
YX
459 tn->bclink->retransmit_to = node;
460 bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
b97bf3fd 461 msg_bcgap_to(msg));
1da46568 462 tipc_bclink_unlock(net);
b952b2be 463 tipc_node_unlock(node);
b97bf3fd 464 } else {
5d3c488d 465 tipc_node_unlock(node);
f2f9800d 466 bclink_peek_nack(net, msg);
b97bf3fd 467 }
8a0f6ebe 468 tipc_node_put(node);
5d3c488d 469 goto exit;
b97bf3fd
PL
470 }
471
5d3c488d 472 /* Handle in-sequence broadcast message */
b97bf3fd 473 seqno = msg_seqno(msg);
8a275a6a 474 next_in = mod(node->bclink.last_in + 1);
cb1b7280
JPM
475 arrvq = &tn->bclink->arrvq;
476 inputq = &tn->bclink->inputq;
b97bf3fd
PL
477
478 if (likely(seqno == next_in)) {
8a275a6a 479receive:
7a54d4a9 480 /* Deliver message to destination */
b97bf3fd 481 if (likely(msg_isdata(msg))) {
1da46568 482 tipc_bclink_lock(net);
63e7f1ac 483 bclink_accept_pkt(node, seqno);
cb1b7280
JPM
484 spin_lock_bh(&inputq->lock);
485 __skb_queue_tail(arrvq, buf);
486 spin_unlock_bh(&inputq->lock);
487 node->action_flags |= TIPC_BCAST_MSG_EVT;
1da46568 488 tipc_bclink_unlock(net);
4323add6 489 tipc_node_unlock(node);
b97bf3fd 490 } else if (msg_user(msg) == MSG_BUNDLER) {
1da46568 491 tipc_bclink_lock(net);
63e7f1ac 492 bclink_accept_pkt(node, seqno);
b97bf3fd
PL
493 bcl->stats.recv_bundles++;
494 bcl->stats.recv_bundled += msg_msgcnt(msg);
cb1b7280
JPM
495 pos = 0;
496 while (tipc_msg_extract(buf, &iskb, &pos)) {
497 spin_lock_bh(&inputq->lock);
498 __skb_queue_tail(arrvq, iskb);
499 spin_unlock_bh(&inputq->lock);
500 }
501 node->action_flags |= TIPC_BCAST_MSG_EVT;
1da46568 502 tipc_bclink_unlock(net);
4323add6 503 tipc_node_unlock(node);
b97bf3fd 504 } else if (msg_user(msg) == MSG_FRAGMENTER) {
1da46568 505 tipc_bclink_lock(net);
63e7f1ac 506 bclink_accept_pkt(node, seqno);
bc14b8d6
YX
507 tipc_buf_append(&node->bclink.reasm_buf, &buf);
508 if (unlikely(!buf && !node->bclink.reasm_buf)) {
509 tipc_bclink_unlock(net);
510 goto unlock;
511 }
b97bf3fd 512 bcl->stats.recv_fragments++;
37e22164 513 if (buf) {
b97bf3fd 514 bcl->stats.recv_fragmented++;
40ba3cdf 515 msg = buf_msg(buf);
1da46568 516 tipc_bclink_unlock(net);
528f6f4b
EH
517 goto receive;
518 }
1da46568 519 tipc_bclink_unlock(net);
4323add6 520 tipc_node_unlock(node);
b97bf3fd 521 } else {
1da46568 522 tipc_bclink_lock(net);
63e7f1ac 523 bclink_accept_pkt(node, seqno);
1da46568 524 tipc_bclink_unlock(net);
4323add6 525 tipc_node_unlock(node);
5f6d9123 526 kfree_skb(buf);
b97bf3fd 527 }
5d3c488d 528 buf = NULL;
8a275a6a
AS
529
530 /* Determine new synchronization state */
5d3c488d 531 tipc_node_lock(node);
8a275a6a
AS
532 if (unlikely(!tipc_node_is_up(node)))
533 goto unlock;
534
7a54d4a9 535 if (node->bclink.last_in == node->bclink.last_sent)
8a275a6a
AS
536 goto unlock;
537
05dcc5aa 538 if (skb_queue_empty(&node->bclink.deferdq)) {
7a54d4a9
AS
539 node->bclink.oos_state = 1;
540 goto unlock;
541 }
542
05dcc5aa 543 msg = buf_msg(skb_peek(&node->bclink.deferdq));
8a275a6a
AS
544 seqno = msg_seqno(msg);
545 next_in = mod(next_in + 1);
546 if (seqno != next_in)
547 goto unlock;
548
549 /* Take in-sequence message from deferred queue & deliver it */
05dcc5aa 550 buf = __skb_dequeue(&node->bclink.deferdq);
8a275a6a
AS
551 goto receive;
552 }
553
554 /* Handle out-of-sequence broadcast message */
8a275a6a 555 if (less(next_in, seqno)) {
05dcc5aa 556 deferred = tipc_link_defer_pkt(&node->bclink.deferdq,
8a275a6a 557 buf);
7a54d4a9 558 bclink_update_last_sent(node, seqno);
5d3c488d 559 buf = NULL;
0abd8ff2 560 }
8a275a6a 561
1da46568 562 tipc_bclink_lock(net);
b98158e3 563
8a275a6a
AS
564 if (deferred)
565 bcl->stats.deferred_recv++;
0232c5a5
AS
566 else
567 bcl->stats.duplicates++;
8a275a6a 568
1da46568 569 tipc_bclink_unlock(net);
b98158e3 570
5d3c488d 571unlock:
4323add6 572 tipc_node_unlock(node);
8a0f6ebe 573 tipc_node_put(node);
5d3c488d 574exit:
5f6d9123 575 kfree_skb(buf);
b97bf3fd
PL
576}
577
6c00055a 578u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
b97bf3fd 579{
389dd9bc 580 return (n_ptr->bclink.recv_permitted &&
1da46568 581 (tipc_bclink_get_last_sent(n_ptr->net) != n_ptr->bclink.acked));
b97bf3fd
PL
582}
583
584
585/**
4323add6 586 * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
c4307285 587 *
2ff9f924
AS
588 * Send packet over as many bearers as necessary to reach all nodes
589 * that have joined the broadcast link.
c4307285 590 *
2ff9f924
AS
591 * Returns 0 (packet sent successfully) under all circumstances,
592 * since the broadcast link's pseudo-bearer never blocks
b97bf3fd 593 */
1da46568
YX
594static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
595 struct tipc_bearer *unused1,
988f088a 596 struct tipc_media_addr *unused2)
b97bf3fd 597{
b97bf3fd 598 int bp_index;
6f92ee54 599 struct tipc_msg *msg = buf_msg(buf);
c93d3baa 600 struct tipc_net *tn = net_generic(net, tipc_net_id);
1da46568
YX
601 struct tipc_bcbearer *bcbearer = tn->bcbearer;
602 struct tipc_bclink *bclink = tn->bclink;
b97bf3fd 603
e6160710 604 /* Prepare broadcast link message for reliable transmission,
2ff9f924
AS
605 * if first time trying to send it;
606 * preparation is skipped for broadcast link protocol messages
607 * since they are sent in an unreliable manner and don't need it
608 */
b97bf3fd 609 if (likely(!msg_non_seq(buf_msg(buf)))) {
cd3decdf 610 bcbuf_set_acks(buf, bclink->bcast_nodes.count);
40aecb1b 611 msg_set_non_seq(msg, 1);
c93d3baa 612 msg_set_mc_netid(msg, tn->net_id);
1da46568 613 tn->bcl->stats.sent_info++;
cd3decdf 614 if (WARN_ON(!bclink->bcast_nodes.count)) {
5e726900
AS
615 dump_stack();
616 return 0;
617 }
b97bf3fd
PL
618 }
619
b97bf3fd 620 /* Send buffer over bearers until all targets reached */
cd3decdf 621 bcbearer->remains = bclink->bcast_nodes;
b97bf3fd
PL
622
623 for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
2d627b92
AS
624 struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
625 struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
6f92ee54
JPM
626 struct tipc_bearer *bp[2] = {p, s};
627 struct tipc_bearer *b = bp[msg_link_selector(msg)];
488fc9af 628 struct sk_buff *tbuf;
b97bf3fd
PL
629
630 if (!p)
e6160710 631 break; /* No more bearers to try */
6f92ee54
JPM
632 if (!b)
633 b = p;
77861d9c 634 tipc_nmap_diff(&bcbearer->remains, &b->nodes,
e6160710 635 &bcbearer->remains_new);
65f51ef0 636 if (bcbearer->remains_new.count == bcbearer->remains.count)
e6160710 637 continue; /* Nothing added by bearer pair */
b97bf3fd 638
488fc9af
GF
639 if (bp_index == 0) {
640 /* Use original buffer for first bearer */
7f9f95d9 641 tipc_bearer_send(net, b->identity, buf, &b->bcast_addr);
488fc9af
GF
642 } else {
643 /* Avoid concurrent buffer access */
bad93e9d 644 tbuf = pskb_copy_for_clone(buf, GFP_ATOMIC);
488fc9af
GF
645 if (!tbuf)
646 break;
7f9f95d9
YX
647 tipc_bearer_send(net, b->identity, tbuf,
648 &b->bcast_addr);
488fc9af
GF
649 kfree_skb(tbuf); /* Bearer keeps a clone */
650 }
65f51ef0 651 if (bcbearer->remains_new.count == 0)
e6160710 652 break; /* All targets reached */
b97bf3fd 653
65f51ef0 654 bcbearer->remains = bcbearer->remains_new;
b97bf3fd 655 }
c4307285 656
2ff9f924 657 return 0;
b97bf3fd
PL
658}
659
660/**
4323add6 661 * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
b97bf3fd 662 */
7f9f95d9
YX
663void tipc_bcbearer_sort(struct net *net, struct tipc_node_map *nm_ptr,
664 u32 node, bool action)
b97bf3fd 665{
7f9f95d9 666 struct tipc_net *tn = net_generic(net, tipc_net_id);
1da46568 667 struct tipc_bcbearer *bcbearer = tn->bcbearer;
7f9ab6ac
PG
668 struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
669 struct tipc_bcbearer_pair *bp_curr;
f8322dfc 670 struct tipc_bearer *b;
b97bf3fd
PL
671 int b_index;
672 int pri;
673
1da46568 674 tipc_bclink_lock(net);
b97bf3fd 675
28dd9418
YX
676 if (action)
677 tipc_nmap_add(nm_ptr, node);
678 else
679 tipc_nmap_remove(nm_ptr, node);
680
b97bf3fd 681 /* Group bearers by priority (can assume max of two per priority) */
b97bf3fd
PL
682 memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
683
f8322dfc 684 rcu_read_lock();
b97bf3fd 685 for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
7f9f95d9 686 b = rcu_dereference_rtnl(tn->bearer_list[b_index]);
f47de12b 687 if (!b || !b->nodes.count)
b97bf3fd
PL
688 continue;
689
690 if (!bp_temp[b->priority].primary)
691 bp_temp[b->priority].primary = b;
692 else
693 bp_temp[b->priority].secondary = b;
694 }
f8322dfc 695 rcu_read_unlock();
b97bf3fd
PL
696
697 /* Create array of bearer pairs for broadcasting */
b97bf3fd
PL
698 bp_curr = bcbearer->bpairs;
699 memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
700
16cb4b33 701 for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
b97bf3fd
PL
702
703 if (!bp_temp[pri].primary)
704 continue;
705
706 bp_curr->primary = bp_temp[pri].primary;
707
708 if (bp_temp[pri].secondary) {
4323add6
PL
709 if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
710 &bp_temp[pri].secondary->nodes)) {
b97bf3fd
PL
711 bp_curr->secondary = bp_temp[pri].secondary;
712 } else {
713 bp_curr++;
714 bp_curr->primary = bp_temp[pri].secondary;
715 }
716 }
717
718 bp_curr++;
719 }
720
1da46568 721 tipc_bclink_unlock(net);
b97bf3fd
PL
722}
723
d8182804
RA
724static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
725 struct tipc_stats *stats)
7be57fc6
RA
726{
727 int i;
728 struct nlattr *nest;
729
730 struct nla_map {
731 __u32 key;
732 __u32 val;
733 };
734
735 struct nla_map map[] = {
736 {TIPC_NLA_STATS_RX_INFO, stats->recv_info},
737 {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
738 {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
739 {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
740 {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
741 {TIPC_NLA_STATS_TX_INFO, stats->sent_info},
742 {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
743 {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
744 {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
745 {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
746 {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
747 {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
748 {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
749 {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
750 {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
751 {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
752 {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
753 {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
754 {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
755 (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
756 };
757
758 nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
759 if (!nest)
760 return -EMSGSIZE;
761
762 for (i = 0; i < ARRAY_SIZE(map); i++)
763 if (nla_put_u32(skb, map[i].key, map[i].val))
764 goto msg_full;
765
766 nla_nest_end(skb, nest);
767
768 return 0;
769msg_full:
770 nla_nest_cancel(skb, nest);
771
772 return -EMSGSIZE;
773}
774
1da46568 775int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
7be57fc6
RA
776{
777 int err;
778 void *hdr;
779 struct nlattr *attrs;
780 struct nlattr *prop;
1da46568
YX
781 struct tipc_net *tn = net_generic(net, tipc_net_id);
782 struct tipc_link *bcl = tn->bcl;
7be57fc6
RA
783
784 if (!bcl)
785 return 0;
786
1da46568 787 tipc_bclink_lock(net);
7be57fc6 788
bfb3e5dd 789 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
7be57fc6
RA
790 NLM_F_MULTI, TIPC_NL_LINK_GET);
791 if (!hdr)
792 return -EMSGSIZE;
793
794 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
795 if (!attrs)
796 goto msg_full;
797
798 /* The broadcast link is always up */
799 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
800 goto attr_msg_full;
801
802 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
803 goto attr_msg_full;
804 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
805 goto attr_msg_full;
806 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->next_in_no))
807 goto attr_msg_full;
808 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->next_out_no))
809 goto attr_msg_full;
810
811 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
812 if (!prop)
813 goto attr_msg_full;
1f66d161 814 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
7be57fc6
RA
815 goto prop_msg_full;
816 nla_nest_end(msg->skb, prop);
817
818 err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
819 if (err)
820 goto attr_msg_full;
821
1da46568 822 tipc_bclink_unlock(net);
7be57fc6
RA
823 nla_nest_end(msg->skb, attrs);
824 genlmsg_end(msg->skb, hdr);
825
826 return 0;
827
828prop_msg_full:
829 nla_nest_cancel(msg->skb, prop);
830attr_msg_full:
831 nla_nest_cancel(msg->skb, attrs);
832msg_full:
1da46568 833 tipc_bclink_unlock(net);
7be57fc6
RA
834 genlmsg_cancel(msg->skb, hdr);
835
836 return -EMSGSIZE;
837}
b97bf3fd 838
1da46568 839int tipc_bclink_reset_stats(struct net *net)
b97bf3fd 840{
1da46568
YX
841 struct tipc_net *tn = net_generic(net, tipc_net_id);
842 struct tipc_link *bcl = tn->bcl;
843
b97bf3fd
PL
844 if (!bcl)
845 return -ENOPROTOOPT;
846
1da46568 847 tipc_bclink_lock(net);
b97bf3fd 848 memset(&bcl->stats, 0, sizeof(bcl->stats));
1da46568 849 tipc_bclink_unlock(net);
0e35fd5e 850 return 0;
b97bf3fd
PL
851}
852
1da46568 853int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
b97bf3fd 854{
1da46568
YX
855 struct tipc_net *tn = net_generic(net, tipc_net_id);
856 struct tipc_link *bcl = tn->bcl;
857
b97bf3fd
PL
858 if (!bcl)
859 return -ENOPROTOOPT;
860 if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
861 return -EINVAL;
862
1da46568 863 tipc_bclink_lock(net);
4323add6 864 tipc_link_set_queue_limits(bcl, limit);
1da46568 865 tipc_bclink_unlock(net);
0e35fd5e 866 return 0;
b97bf3fd
PL
867}
868
7f9f95d9 869int tipc_bclink_init(struct net *net)
b97bf3fd 870{
7f9f95d9 871 struct tipc_net *tn = net_generic(net, tipc_net_id);
1da46568
YX
872 struct tipc_bcbearer *bcbearer;
873 struct tipc_bclink *bclink;
874 struct tipc_link *bcl;
7f9f95d9 875
eb8b00f5
YX
876 bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
877 if (!bcbearer)
878 return -ENOMEM;
879
880 bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
881 if (!bclink) {
882 kfree(bcbearer);
883 return -ENOMEM;
884 }
885
886 bcl = &bclink->link;
b97bf3fd 887 bcbearer->bearer.media = &bcbearer->media;
4323add6 888 bcbearer->media.send_msg = tipc_bcbearer_send;
2e2d9be8 889 sprintf(bcbearer->media.name, "tipc-broadcast");
b97bf3fd 890
d69afc90 891 spin_lock_init(&bclink->lock);
05dcc5aa
JPM
892 __skb_queue_head_init(&bcl->transmq);
893 __skb_queue_head_init(&bcl->backlogq);
894 __skb_queue_head_init(&bcl->deferdq);
c637c103 895 skb_queue_head_init(&bcl->wakeupq);
b97bf3fd 896 bcl->next_out_no = 1;
34af946a 897 spin_lock_init(&bclink->node.lock);
cb1b7280
JPM
898 __skb_queue_head_init(&bclink->arrvq);
899 skb_queue_head_init(&bclink->inputq);
b97bf3fd 900 bcl->owner = &bclink->node;
1da46568 901 bcl->owner->net = net;
ed193ece 902 bcl->mtu = MAX_PKT_DEFAULT_MCAST;
4323add6 903 tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
7a2f7d18 904 bcl->bearer_id = MAX_BEARERS;
7f9f95d9 905 rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
b97bf3fd 906 bcl->state = WORKING_WORKING;
c5898636
JPM
907 bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
908 msg_set_prevnode(bcl->pmsg, tn->own_addr);
4b704d59 909 strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
1da46568
YX
910 tn->bcbearer = bcbearer;
911 tn->bclink = bclink;
912 tn->bcl = bcl;
eb8b00f5 913 return 0;
b97bf3fd
PL
914}
915
7f9f95d9 916void tipc_bclink_stop(struct net *net)
b97bf3fd 917{
7f9f95d9
YX
918 struct tipc_net *tn = net_generic(net, tipc_net_id);
919
1da46568
YX
920 tipc_bclink_lock(net);
921 tipc_link_purge_queues(tn->bcl);
922 tipc_bclink_unlock(net);
c47e9b91 923
7f9f95d9 924 RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
eb8b00f5 925 synchronize_net();
1da46568
YX
926 kfree(tn->bcbearer);
927 kfree(tn->bclink);
b97bf3fd
PL
928}
929
3e22e62b
AS
930/**
931 * tipc_nmap_add - add a node to a node map
932 */
28dd9418 933static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
3e22e62b
AS
934{
935 int n = tipc_node(node);
936 int w = n / WSIZE;
937 u32 mask = (1 << (n % WSIZE));
938
939 if ((nm_ptr->map[w] & mask) == 0) {
940 nm_ptr->count++;
941 nm_ptr->map[w] |= mask;
942 }
943}
944
945/**
946 * tipc_nmap_remove - remove a node from a node map
947 */
28dd9418 948static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
3e22e62b
AS
949{
950 int n = tipc_node(node);
951 int w = n / WSIZE;
952 u32 mask = (1 << (n % WSIZE));
953
954 if ((nm_ptr->map[w] & mask) != 0) {
955 nm_ptr->map[w] &= ~mask;
956 nm_ptr->count--;
957 }
958}
959
960/**
961 * tipc_nmap_diff - find differences between node maps
962 * @nm_a: input node map A
963 * @nm_b: input node map B
964 * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
965 */
31e3c3f6 966static void tipc_nmap_diff(struct tipc_node_map *nm_a,
967 struct tipc_node_map *nm_b,
968 struct tipc_node_map *nm_diff)
3e22e62b
AS
969{
970 int stop = ARRAY_SIZE(nm_a->map);
971 int w;
972 int b;
973 u32 map;
974
975 memset(nm_diff, 0, sizeof(*nm_diff));
976 for (w = 0; w < stop; w++) {
977 map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
978 nm_diff->map[w] = map;
979 if (map != 0) {
980 for (b = 0 ; b < WSIZE; b++) {
981 if (map & (1 << b))
982 nm_diff->count++;
983 }
984 }
985 }
986}