bcast.c 21.9 KB
Newer Older
P
Per Liden 已提交
1 2
/*
 * net/tipc/bcast.c: TIPC broadcast code
3
 *
4
 * Copyright (c) 2004-2006, 2014-2017, Ericsson AB
P
Per Liden 已提交
5
 * Copyright (c) 2004, Intel Corporation.
6
 * Copyright (c) 2005, 2010-2011, Wind River Systems
P
Per Liden 已提交
7 8
 * All rights reserved.
 *
P
Per Liden 已提交
9
 * Redistribution and use in source and binary forms, with or without
P
Per Liden 已提交
10 11
 * modification, are permitted provided that the following conditions are met:
 *
P
Per Liden 已提交
12 13 14 15 16 17 18 19
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
P
Per Liden 已提交
20
 *
P
Per Liden 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
P
Per Liden 已提交
35 36 37
 * POSSIBILITY OF SUCH DAMAGE.
 */

38
#include <linux/tipc_config.h>
39 40
#include "socket.h"
#include "msg.h"
P
Per Liden 已提交
41
#include "bcast.h"
42
#include "link.h"
43
#include "name_table.h"
P
Per Liden 已提交
44

45 46
#define BCLINK_WIN_DEFAULT  50	/* bcast link window size (default) */
#define BCLINK_WIN_MIN      32	/* bcast minimum link window size */
P
Per Liden 已提交
47

48
const char tipc_bclink_name[] = "broadcast-link";
49
unsigned long sysctl_tipc_bc_retruni __read_mostly;
P
Per Liden 已提交
50

51
/**
52
 * struct tipc_bc_base - base structure for keeping broadcast send state
53
 * @link: broadcast send link structure
54
 * @inputq: data input queue; will only carry SOCK_WAKEUP messages
55
 * @dests: array keeping number of reachable destinations per bearer
56
 * @primary_bearer: a bearer having links to all broadcast destinations, if any
57
 * @bcast_support: indicates if primary bearer, if any, supports broadcast
58
 * @force_bcast: forces broadcast for multicast traffic
59
 * @rcast_support: indicates if all peer nodes support replicast
60
 * @force_rcast: forces replicast for multicast traffic
61
 * @rc_ratio: dest count as percentage of cluster size where send method changes
62
 * @bc_threshold: calculated from rc_ratio; if dests > threshold use broadcast
63 64
 */
struct tipc_bc_base {
65
	struct tipc_link *link;
66
	struct sk_buff_head inputq;
67 68
	int dests[MAX_BEARERS];
	int primary_bearer;
69
	bool bcast_support;
70
	bool force_bcast;
71
	bool rcast_support;
72
	bool force_rcast;
73 74
	int rc_ratio;
	int bc_threshold;
75 76
};

77 78 79 80 81
static struct tipc_bc_base *tipc_bc_base(struct net *net)
{
	return tipc_net(net)->bcbase;
}

82 83 84 85
/* tipc_bcast_get_mtu(): -get the MTU currently used by broadcast link
 * Note: the MTU is decremented to give room for a tunnel header, in
 * case the message needs to be sent as replicast
 */
86
int tipc_bcast_get_mtu(struct net *net)
87
{
88
	return tipc_link_mss(tipc_bc_sndlink(net));
89 90
}

91
void tipc_bcast_toggle_rcast(struct net *net, bool supp)
92
{
93
	tipc_bc_base(net)->rcast_support = supp;
94 95 96 97 98 99 100 101 102 103
}

static void tipc_bcbase_calc_bc_threshold(struct net *net)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);
	int cluster_size = tipc_link_bc_peers(tipc_bc_sndlink(net));

	bb->bc_threshold = 1 + (cluster_size * bb->rc_ratio / 100);
}

104 105 106 107 108 109 110
/* tipc_bcbase_select_primary(): find a bearer with links to all destinations,
 *                               if any, and make it primary bearer
 */
static void tipc_bcbase_select_primary(struct net *net)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);
	int all_dests =  tipc_link_bc_peers(bb->link);
111
	int i, mtu, prim;
112 113

	bb->primary_bearer = INVALID_BEARER_ID;
114
	bb->bcast_support = true;
115 116 117 118 119

	if (!all_dests)
		return;

	for (i = 0; i < MAX_BEARERS; i++) {
120 121 122 123 124 125
		if (!bb->dests[i])
			continue;

		mtu = tipc_bearer_mtu(net, i);
		if (mtu < tipc_link_mtu(bb->link))
			tipc_link_set_mtu(bb->link, mtu);
126
		bb->bcast_support &= tipc_bearer_bcast_support(net, i);
127 128 129 130 131 132 133 134 135
		if (bb->dests[i] < all_dests)
			continue;

		bb->primary_bearer = i;

		/* Reduce risk that all nodes select same primary */
		if ((i ^ tipc_own_addr(net)) & 1)
			break;
	}
136 137 138
	prim = bb->primary_bearer;
	if (prim != INVALID_BEARER_ID)
		bb->bcast_support = tipc_bearer_bcast_support(net, prim);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}

void tipc_bcast_inc_bearer_dst_cnt(struct net *net, int bearer_id)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);

	tipc_bcast_lock(net);
	bb->dests[bearer_id]++;
	tipc_bcbase_select_primary(net);
	tipc_bcast_unlock(net);
}

void tipc_bcast_dec_bearer_dst_cnt(struct net *net, int bearer_id)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);

	tipc_bcast_lock(net);
	bb->dests[bearer_id]--;
	tipc_bcbase_select_primary(net);
	tipc_bcast_unlock(net);
}

/* tipc_bcbase_xmit - broadcast a packet queue across one or more bearers
 *
 * Note that number of reachable destinations, as indicated in the dests[]
 * array, may transitionally differ from the number of destinations indicated
 * in each sent buffer. We can sustain this. Excess destination nodes will
 * drop and never acknowledge the unexpected packets, and missing destinations
 * will either require retransmission (if they are just about to be added to
 * the bearer), or be removed from the buffer's 'ackers' counter (if they
 * just went down)
 */
static void tipc_bcbase_xmit(struct net *net, struct sk_buff_head *xmitq)
{
	int bearer_id;
	struct tipc_bc_base *bb = tipc_bc_base(net);
	struct sk_buff *skb, *_skb;
	struct sk_buff_head _xmitq;

	if (skb_queue_empty(xmitq))
		return;

	/* The typical case: at least one bearer has links to all nodes */
	bearer_id = bb->primary_bearer;
	if (bearer_id >= 0) {
		tipc_bearer_bc_xmit(net, bearer_id, xmitq);
		return;
	}

	/* We have to transmit across all bearers */
189
	__skb_queue_head_init(&_xmitq);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
		if (!bb->dests[bearer_id])
			continue;

		skb_queue_walk(xmitq, skb) {
			_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
			if (!_skb)
				break;
			__skb_queue_tail(&_xmitq, _skb);
		}
		tipc_bearer_bc_xmit(net, bearer_id, &_xmitq);
	}
	__skb_queue_purge(xmitq);
	__skb_queue_purge(&_xmitq);
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static void tipc_bcast_select_xmit_method(struct net *net, int dests,
					  struct tipc_mc_method *method)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);
	unsigned long exp = method->expires;

	/* Broadcast supported by used bearer/bearers? */
	if (!bb->bcast_support) {
		method->rcast = true;
		return;
	}
	/* Any destinations which don't support replicast ? */
	if (!bb->rcast_support) {
		method->rcast = false;
		return;
	}
	/* Can current method be changed ? */
	method->expires = jiffies + TIPC_METHOD_EXPIRE;
224
	if (method->mandatory)
225 226
		return;

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	if (!(tipc_net(net)->capabilities & TIPC_MCAST_RBCTL) &&
	    time_before(jiffies, exp))
		return;

	/* Configuration as force 'broadcast' method */
	if (bb->force_bcast) {
		method->rcast = false;
		return;
	}
	/* Configuration as force 'replicast' method */
	if (bb->force_rcast) {
		method->rcast = true;
		return;
	}
	/* Configuration as 'autoselect' or default method */
242 243 244 245
	/* Determine method to use now */
	method->rcast = dests <= bb->bc_threshold;
}

246
/* tipc_bcast_xmit - broadcast the buffer chain to all external nodes
247
 * @net: the applicable net namespace
248 249
 * @pkts: chain of buffers containing message
 * @cong_link_cnt: set to 1 if broadcast link is congested, otherwise 0
250
 * Consumes the buffer chain.
251
 * Returns 0 if success, otherwise errno: -EHOSTUNREACH,-EMSGSIZE
252
 */
253 254
static int tipc_bcast_xmit(struct net *net, struct sk_buff_head *pkts,
			   u16 *cong_link_cnt)
255
{
256
	struct tipc_link *l = tipc_bc_sndlink(net);
257
	struct sk_buff_head xmitq;
258 259
	int rc = 0;

260
	__skb_queue_head_init(&xmitq);
261 262
	tipc_bcast_lock(net);
	if (tipc_link_bc_peers(l))
263
		rc = tipc_link_xmit(l, pkts, &xmitq);
264
	tipc_bcast_unlock(net);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	tipc_bcbase_xmit(net, &xmitq);
	__skb_queue_purge(pkts);
	if (rc == -ELINKCONG) {
		*cong_link_cnt = 1;
		rc = 0;
	}
	return rc;
}

/* tipc_rcast_xmit - replicate and send a message to given destination nodes
 * @net: the applicable net namespace
 * @pkts: chain of buffers containing message
 * @dests: list of destination nodes
 * @cong_link_cnt: returns number of congested links
 * @cong_links: returns identities of congested links
 * Returns 0 if success, otherwise errno
 */
static int tipc_rcast_xmit(struct net *net, struct sk_buff_head *pkts,
			   struct tipc_nlist *dests, u16 *cong_link_cnt)
{
J
Jon Maloy 已提交
285
	struct tipc_dest *dst, *tmp;
286
	struct sk_buff_head _pkts;
J
Jon Maloy 已提交
287
	u32 dnode, selector;
288 289

	selector = msg_link_selector(buf_msg(skb_peek(pkts)));
290
	__skb_queue_head_init(&_pkts);
291

J
Jon Maloy 已提交
292 293 294
	list_for_each_entry_safe(dst, tmp, &dests->list, list) {
		dnode = dst->node;
		if (!tipc_msg_pskb_copy(dnode, pkts, &_pkts))
295
			return -ENOMEM;
296

297
		/* Any other return value than -ELINKCONG is ignored */
J
Jon Maloy 已提交
298
		if (tipc_node_xmit(net, &_pkts, dnode, selector) == -ELINKCONG)
299
			(*cong_link_cnt)++;
300
	}
301 302
	return 0;
}
303

304 305 306 307 308 309 310 311 312
/* tipc_mcast_send_sync - deliver a dummy message with SYN bit
 * @net: the applicable net namespace
 * @skb: socket buffer to copy
 * @method: send method to be used
 * @dests: destination nodes for message.
 * Returns 0 if success, otherwise errno
 */
static int tipc_mcast_send_sync(struct net *net, struct sk_buff *skb,
				struct tipc_mc_method *method,
313
				struct tipc_nlist *dests)
314 315 316 317
{
	struct tipc_msg *hdr, *_hdr;
	struct sk_buff_head tmpq;
	struct sk_buff *_skb;
318 319
	u16 cong_link_cnt;
	int rc = 0;
320 321 322 323 324 325 326

	/* Is a cluster supporting with new capabilities ? */
	if (!(tipc_net(net)->capabilities & TIPC_MCAST_RBCTL))
		return 0;

	hdr = buf_msg(skb);
	if (msg_user(hdr) == MSG_FRAGMENTER)
327
		hdr = msg_inner_hdr(hdr);
328 329 330 331 332
	if (msg_type(hdr) != TIPC_MCAST_MSG)
		return 0;

	/* Allocate dummy message */
	_skb = tipc_buf_acquire(MCAST_H_SIZE, GFP_KERNEL);
333
	if (!_skb)
334 335 336 337 338 339 340 341 342 343 344 345 346
		return -ENOMEM;

	/* Preparing for 'synching' header */
	msg_set_syn(hdr, 1);

	/* Copy skb's header into a dummy header */
	skb_copy_to_linear_data(_skb, hdr, MCAST_H_SIZE);
	skb_orphan(_skb);

	/* Reverse method for dummy message */
	_hdr = buf_msg(_skb);
	msg_set_size(_hdr, MCAST_H_SIZE);
	msg_set_is_rcast(_hdr, !msg_is_rcast(hdr));
347
	msg_set_errcode(_hdr, TIPC_ERR_NO_PORT);
348

349
	__skb_queue_head_init(&tmpq);
350 351
	__skb_queue_tail(&tmpq, _skb);
	if (method->rcast)
352
		rc = tipc_bcast_xmit(net, &tmpq, &cong_link_cnt);
353
	else
354
		rc = tipc_rcast_xmit(net, &tmpq, dests, &cong_link_cnt);
355 356 357 358

	/* This queue should normally be empty by now */
	__skb_queue_purge(&tmpq);

359
	return rc;
360 361
}

362 363 364 365
/* tipc_mcast_xmit - deliver message to indicated destination nodes
 *                   and to identified node local sockets
 * @net: the applicable net namespace
 * @pkts: chain of buffers containing message
366 367
 * @method: send method to be used
 * @dests: destination nodes for message.
368 369 370 371 372
 * @cong_link_cnt: returns number of encountered congested destination links
 * Consumes buffer chain.
 * Returns 0 if success, otherwise errno
 */
int tipc_mcast_xmit(struct net *net, struct sk_buff_head *pkts,
373 374
		    struct tipc_mc_method *method, struct tipc_nlist *dests,
		    u16 *cong_link_cnt)
375 376
{
	struct sk_buff_head inputq, localq;
377 378 379
	bool rcast = method->rcast;
	struct tipc_msg *hdr;
	struct sk_buff *skb;
380 381 382
	int rc = 0;

	skb_queue_head_init(&inputq);
383
	__skb_queue_head_init(&localq);
384 385 386 387 388 389

	/* Clone packets before they are consumed by next call */
	if (dests->local && !tipc_msg_reassemble(pkts, &localq)) {
		rc = -ENOMEM;
		goto exit;
	}
390
	/* Send according to determined transmit method */
391
	if (dests->remote) {
392
		tipc_bcast_select_xmit_method(net, dests->remote, method);
393 394 395 396

		skb = skb_peek(pkts);
		hdr = buf_msg(skb);
		if (msg_user(hdr) == MSG_FRAGMENTER)
397
			hdr = msg_inner_hdr(hdr);
398 399 400
		msg_set_is_rcast(hdr, method->rcast);

		/* Switch method ? */
401 402 403 404 405 406 407 408
		if (rcast != method->rcast) {
			rc = tipc_mcast_send_sync(net, skb, method, dests);
			if (unlikely(rc)) {
				pr_err("Unable to send SYN: method %d, rc %d\n",
				       rcast, rc);
				goto exit;
			}
		}
409

410
		if (method->rcast)
411 412 413 414 415
			rc = tipc_rcast_xmit(net, pkts, dests, cong_link_cnt);
		else
			rc = tipc_bcast_xmit(net, pkts, cong_link_cnt);
	}

416 417
	if (dests->local) {
		tipc_loopback_trace(net, &localq);
418
		tipc_sk_mcast_rcv(net, &localq, &inputq);
419
	}
420
exit:
421
	/* This queue should normally be empty by now */
422
	__skb_queue_purge(pkts);
423
	return rc;
424
}
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

/* tipc_bcast_rcv - receive a broadcast packet, and deliver to rcv link
 *
 * RCU is locked, no other locks set
 */
int tipc_bcast_rcv(struct net *net, struct tipc_link *l, struct sk_buff *skb)
{
	struct tipc_msg *hdr = buf_msg(skb);
	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
	struct sk_buff_head xmitq;
	int rc;

	__skb_queue_head_init(&xmitq);

	if (msg_mc_netid(hdr) != tipc_netid(net) || !tipc_link_is_up(l)) {
		kfree_skb(skb);
		return 0;
	}

	tipc_bcast_lock(net);
	if (msg_user(hdr) == BCAST_PROTOCOL)
		rc = tipc_link_bc_nack_rcv(l, skb, &xmitq);
	else
		rc = tipc_link_rcv(l, skb, NULL);
	tipc_bcast_unlock(net);

451
	tipc_bcbase_xmit(net, &xmitq);
452 453 454 455 456 457 458 459 460 461 462 463

	/* Any socket wakeup messages ? */
	if (!skb_queue_empty(inputq))
		tipc_sk_rcv(net, inputq);

	return rc;
}

/* tipc_bcast_ack_rcv - receive and handle a broadcast acknowledge
 *
 * RCU is locked, no other locks set
 */
464 465
void tipc_bcast_ack_rcv(struct net *net, struct tipc_link *l,
			struct tipc_msg *hdr)
466 467
{
	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
468
	u16 acked = msg_bcast_ack(hdr);
469 470
	struct sk_buff_head xmitq;

471 472 473 474
	/* Ignore bc acks sent by peer before bcast synch point was received */
	if (msg_bc_ack_invalid(hdr))
		return;

475 476 477
	__skb_queue_head_init(&xmitq);

	tipc_bcast_lock(net);
478
	tipc_link_bc_ack_rcv(l, acked, 0, NULL, &xmitq, NULL);
479 480
	tipc_bcast_unlock(net);

481
	tipc_bcbase_xmit(net, &xmitq);
482 483 484 485 486 487 488 489 490 491

	/* Any socket wakeup messages ? */
	if (!skb_queue_empty(inputq))
		tipc_sk_rcv(net, inputq);
}

/* tipc_bcast_synch_rcv -  check and update rcv link with peer's send state
 *
 * RCU is locked, no other locks set
 */
492
int tipc_bcast_sync_rcv(struct net *net, struct tipc_link *l,
493 494
			struct tipc_msg *hdr,
			struct sk_buff_head *retrq)
495 496
{
	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
497
	struct tipc_gap_ack_blks *ga;
498
	struct sk_buff_head xmitq;
499
	int rc = 0;
500 501 502 503

	__skb_queue_head_init(&xmitq);

	tipc_bcast_lock(net);
504 505 506
	if (msg_type(hdr) != STATE_MSG) {
		tipc_link_bc_init_rcv(l, hdr);
	} else if (!msg_bc_ack_invalid(hdr)) {
507
		tipc_get_gap_ack_blks(&ga, l, hdr, false);
508 509
		if (!sysctl_tipc_bc_retruni)
			retrq = &xmitq;
510
		rc = tipc_link_bc_ack_rcv(l, msg_bcast_ack(hdr),
511 512
					  msg_bc_gap(hdr), ga, &xmitq,
					  retrq);
513
		rc |= tipc_link_bc_sync_rcv(l, hdr, &xmitq);
514 515 516
	}
	tipc_bcast_unlock(net);

517
	tipc_bcbase_xmit(net, &xmitq);
518 519 520 521

	/* Any socket wakeup messages ? */
	if (!skb_queue_empty(inputq))
		tipc_sk_rcv(net, inputq);
522
	return rc;
523 524 525 526 527 528
}

/* tipc_bcast_add_peer - add a peer node to broadcast link and bearer
 *
 * RCU is locked, node lock is set
 */
529
void tipc_bcast_add_peer(struct net *net, struct tipc_link *uc_l,
530 531 532 533
			 struct sk_buff_head *xmitq)
{
	struct tipc_link *snd_l = tipc_bc_sndlink(net);

534
	tipc_bcast_lock(net);
535
	tipc_link_add_bc_peer(snd_l, uc_l, xmitq);
536
	tipc_bcbase_select_primary(net);
537
	tipc_bcbase_calc_bc_threshold(net);
538
	tipc_bcast_unlock(net);
539 540 541 542 543 544
}

/* tipc_bcast_remove_peer - remove a peer node from broadcast link and bearer
 *
 * RCU is locked, node lock is set
 */
545
void tipc_bcast_remove_peer(struct net *net, struct tipc_link *rcv_l)
546 547
{
	struct tipc_link *snd_l = tipc_bc_sndlink(net);
548
	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
549 550 551 552
	struct sk_buff_head xmitq;

	__skb_queue_head_init(&xmitq);

553
	tipc_bcast_lock(net);
554
	tipc_link_remove_bc_peer(snd_l, rcv_l, &xmitq);
555
	tipc_bcbase_select_primary(net);
556
	tipc_bcbase_calc_bc_threshold(net);
557
	tipc_bcast_unlock(net);
558

559
	tipc_bcbase_xmit(net, &xmitq);
560 561 562 563 564 565

	/* Any socket wakeup messages ? */
	if (!skb_queue_empty(inputq))
		tipc_sk_rcv(net, inputq);
}

566
int tipc_bclink_reset_stats(struct net *net)
P
Per Liden 已提交
567
{
568
	struct tipc_link *l = tipc_bc_sndlink(net);
569

570
	if (!l)
P
Per Liden 已提交
571 572
		return -ENOPROTOOPT;

573
	tipc_bcast_lock(net);
574
	tipc_link_reset_stats(l);
575
	tipc_bcast_unlock(net);
576
	return 0;
P
Per Liden 已提交
577 578
}

579
static int tipc_bc_link_set_queue_limits(struct net *net, u32 max_win)
P
Per Liden 已提交
580
{
581
	struct tipc_link *l = tipc_bc_sndlink(net);
582

583
	if (!l)
P
Per Liden 已提交
584
		return -ENOPROTOOPT;
585 586 587
	if (max_win < BCLINK_WIN_MIN)
		max_win = BCLINK_WIN_MIN;
	if (max_win > TIPC_MAX_LINK_WIN)
P
Per Liden 已提交
588
		return -EINVAL;
589
	tipc_bcast_lock(net);
590
	tipc_link_set_queue_limits(l, BCLINK_WIN_MIN, max_win);
591
	tipc_bcast_unlock(net);
592
	return 0;
P
Per Liden 已提交
593 594
}

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
static int tipc_bc_link_set_broadcast_mode(struct net *net, u32 bc_mode)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);

	switch (bc_mode) {
	case BCLINK_MODE_BCAST:
		if (!bb->bcast_support)
			return -ENOPROTOOPT;

		bb->force_bcast = true;
		bb->force_rcast = false;
		break;
	case BCLINK_MODE_RCAST:
		if (!bb->rcast_support)
			return -ENOPROTOOPT;

		bb->force_bcast = false;
		bb->force_rcast = true;
		break;
	case BCLINK_MODE_SEL:
		if (!bb->bcast_support || !bb->rcast_support)
			return -ENOPROTOOPT;

		bb->force_bcast = false;
		bb->force_rcast = false;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int tipc_bc_link_set_broadcast_ratio(struct net *net, u32 bc_ratio)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);

	if (!bb->bcast_support || !bb->rcast_support)
		return -ENOPROTOOPT;

	if (bc_ratio > 100 || bc_ratio <= 0)
		return -EINVAL;

	bb->rc_ratio = bc_ratio;
	tipc_bcast_lock(net);
	tipc_bcbase_calc_bc_threshold(net);
	tipc_bcast_unlock(net);

	return 0;
}

646 647 648 649
int tipc_nl_bc_link_set(struct net *net, struct nlattr *attrs[])
{
	int err;
	u32 win;
650 651
	u32 bc_mode;
	u32 bc_ratio;
652 653 654 655 656 657 658 659 660
	struct nlattr *props[TIPC_NLA_PROP_MAX + 1];

	if (!attrs[TIPC_NLA_LINK_PROP])
		return -EINVAL;

	err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
	if (err)
		return err;

661 662 663
	if (!props[TIPC_NLA_PROP_WIN] &&
	    !props[TIPC_NLA_PROP_BROADCAST] &&
	    !props[TIPC_NLA_PROP_BROADCAST_RATIO]) {
664
		return -EOPNOTSUPP;
665 666 667 668 669 670
	}

	if (props[TIPC_NLA_PROP_BROADCAST]) {
		bc_mode = nla_get_u32(props[TIPC_NLA_PROP_BROADCAST]);
		err = tipc_bc_link_set_broadcast_mode(net, bc_mode);
	}
671

672 673 674 675
	if (!err && props[TIPC_NLA_PROP_BROADCAST_RATIO]) {
		bc_ratio = nla_get_u32(props[TIPC_NLA_PROP_BROADCAST_RATIO]);
		err = tipc_bc_link_set_broadcast_ratio(net, bc_ratio);
	}
676

677 678 679 680 681 682
	if (!err && props[TIPC_NLA_PROP_WIN]) {
		win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
		err = tipc_bc_link_set_queue_limits(net, win);
	}

	return err;
683 684
}

685
int tipc_bcast_init(struct net *net)
P
Per Liden 已提交
686
{
687 688 689 690
	struct tipc_net *tn = tipc_net(net);
	struct tipc_bc_base *bb = NULL;
	struct tipc_link *l = NULL;

691
	bb = kzalloc(sizeof(*bb), GFP_KERNEL);
692 693 694
	if (!bb)
		goto enomem;
	tn->bcbase = bb;
695
	spin_lock_init(&tipc_net(net)->bclock);
696

697
	if (!tipc_link_bc_create(net, 0, 0,
698
				 FB_MTU,
699
				 BCLINK_WIN_DEFAULT,
700
				 BCLINK_WIN_DEFAULT,
701
				 0,
702
				 &bb->inputq,
703
				 NULL,
704
				 NULL,
705 706 707 708
				 &l))
		goto enomem;
	bb->link = l;
	tn->bcl = l;
709
	bb->rc_ratio = 10;
710
	bb->rcast_support = true;
711
	return 0;
712 713 714 715
enomem:
	kfree(bb);
	kfree(l);
	return -ENOMEM;
P
Per Liden 已提交
716 717
}

718
void tipc_bcast_stop(struct net *net)
P
Per Liden 已提交
719
{
720 721
	struct tipc_net *tn = net_generic(net, tipc_net_id);

722
	synchronize_net();
723
	kfree(tn->bcbase);
724
	kfree(tn->bcl);
P
Per Liden 已提交
725
}
726 727 728 729 730 731 732 733 734 735 736 737

void tipc_nlist_init(struct tipc_nlist *nl, u32 self)
{
	memset(nl, 0, sizeof(*nl));
	INIT_LIST_HEAD(&nl->list);
	nl->self = self;
}

void tipc_nlist_add(struct tipc_nlist *nl, u32 node)
{
	if (node == nl->self)
		nl->local = true;
J
Jon Maloy 已提交
738
	else if (tipc_dest_push(&nl->list, node, 0))
739 740 741 742 743 744 745
		nl->remote++;
}

void tipc_nlist_del(struct tipc_nlist *nl, u32 node)
{
	if (node == nl->self)
		nl->local = false;
J
Jon Maloy 已提交
746
	else if (tipc_dest_del(&nl->list, node, 0))
747 748 749 750 751
		nl->remote--;
}

void tipc_nlist_purge(struct tipc_nlist *nl)
{
J
Jon Maloy 已提交
752
	tipc_dest_list_purge(&nl->list);
753
	nl->remote = 0;
754
	nl->local = false;
755
}
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

u32 tipc_bcast_get_broadcast_mode(struct net *net)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);

	if (bb->force_bcast)
		return BCLINK_MODE_BCAST;

	if (bb->force_rcast)
		return BCLINK_MODE_RCAST;

	if (bb->bcast_support && bb->rcast_support)
		return BCLINK_MODE_SEL;

	return 0;
}

u32 tipc_bcast_get_broadcast_ratio(struct net *net)
{
	struct tipc_bc_base *bb = tipc_bc_base(net);

	return bb->rc_ratio;
}
779

H
Hoang Le 已提交
780
void tipc_mcast_filter_msg(struct net *net, struct sk_buff_head *defq,
781 782 783 784 785 786 787 788
			   struct sk_buff_head *inputq)
{
	struct sk_buff *skb, *_skb, *tmp;
	struct tipc_msg *hdr, *_hdr;
	bool match = false;
	u32 node, port;

	skb = skb_peek(inputq);
H
Hoang Le 已提交
789 790 791
	if (!skb)
		return;

792 793 794 795 796 797
	hdr = buf_msg(skb);

	if (likely(!msg_is_syn(hdr) && skb_queue_empty(defq)))
		return;

	node = msg_orignode(hdr);
H
Hoang Le 已提交
798 799 800
	if (node == tipc_own_addr(net))
		return;

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	port = msg_origport(hdr);

	/* Has the twin SYN message already arrived ? */
	skb_queue_walk(defq, _skb) {
		_hdr = buf_msg(_skb);
		if (msg_orignode(_hdr) != node)
			continue;
		if (msg_origport(_hdr) != port)
			continue;
		match = true;
		break;
	}

	if (!match) {
		if (!msg_is_syn(hdr))
			return;
		__skb_dequeue(inputq);
		__skb_queue_tail(defq, skb);
		return;
	}

	/* Deliver non-SYN message from other link, otherwise queue it */
	if (!msg_is_syn(hdr)) {
		if (msg_is_rcast(hdr) != msg_is_rcast(_hdr))
			return;
		__skb_dequeue(inputq);
		__skb_queue_tail(defq, skb);
		return;
	}

	/* Queue non-SYN/SYN message from same link */
	if (msg_is_rcast(hdr) == msg_is_rcast(_hdr)) {
		__skb_dequeue(inputq);
		__skb_queue_tail(defq, skb);
		return;
	}

	/* Matching SYN messages => return the one with data, if any */
	__skb_unlink(_skb, defq);
	if (msg_data_sz(hdr)) {
		kfree_skb(_skb);
	} else {
		__skb_dequeue(inputq);
		kfree_skb(skb);
		__skb_queue_tail(inputq, _skb);
	}

	/* Deliver subsequent non-SYN messages from same peer */
	skb_queue_walk_safe(defq, _skb, tmp) {
		_hdr = buf_msg(_skb);
		if (msg_orignode(_hdr) != node)
			continue;
		if (msg_origport(_hdr) != port)
			continue;
		if (msg_is_syn(_hdr))
			break;
		__skb_unlink(_skb, defq);
		__skb_queue_tail(inputq, _skb);
	}
}