bcast.c 15.3 KB
Newer Older
P
Per Liden 已提交
1 2
/*
 * net/tipc/bcast.c: TIPC broadcast code
3
 *
4
 * Copyright (c) 2004-2006, 2014-2016, 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";
P
Per Liden 已提交
49

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

72 73 74 75 76
static struct tipc_bc_base *tipc_bc_base(struct net *net)
{
	return tipc_net(net)->bcbase;
}

77
int tipc_bcast_get_mtu(struct net *net)
78
{
79
	return tipc_link_mtu(tipc_bc_sndlink(net)) - INT_H_SIZE;
80 81
}

82 83 84 85 86 87 88 89 90 91 92 93 94
void tipc_bcast_disable_rcast(struct net *net)
{
	tipc_bc_base(net)->rcast_support = false;
}

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);
}

95 96 97 98 99 100 101
/* 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);
102
	int i, mtu, prim;
103 104

	bb->primary_bearer = INVALID_BEARER_ID;
105
	bb->bcast_support = true;
106 107 108 109 110

	if (!all_dests)
		return;

	for (i = 0; i < MAX_BEARERS; i++) {
111 112 113 114 115 116
		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);
117
		bb->bcast_support &= tipc_bearer_bcast_support(net, i);
118 119 120 121 122 123 124 125 126
		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;
	}
127 128 129
	prim = bb->primary_bearer;
	if (prim != INVALID_BEARER_ID)
		bb->bcast_support = tipc_bearer_bcast_support(net, prim);
130 131 132 133 134 135 136 137 138 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 189 190 191 192 193 194 195 196
}

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 */
	skb_queue_head_init(&_xmitq);
	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);
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
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;
	if (method->mandatory || time_before(jiffies, exp))
		return;

	/* Determine method to use now */
	method->rcast = dests <= bb->bc_threshold;
}

222
/* tipc_bcast_xmit - broadcast the buffer chain to all external nodes
223
 * @net: the applicable net namespace
224 225
 * @pkts: chain of buffers containing message
 * @cong_link_cnt: set to 1 if broadcast link is congested, otherwise 0
226
 * Consumes the buffer chain.
227
 * Returns 0 if success, otherwise errno: -EHOSTUNREACH,-EMSGSIZE
228
 */
229 230
static int tipc_bcast_xmit(struct net *net, struct sk_buff_head *pkts,
			   u16 *cong_link_cnt)
231
{
232
	struct tipc_link *l = tipc_bc_sndlink(net);
233
	struct sk_buff_head xmitq;
234 235
	int rc = 0;

236
	skb_queue_head_init(&xmitq);
237 238
	tipc_bcast_lock(net);
	if (tipc_link_bc_peers(l))
239
		rc = tipc_link_xmit(l, pkts, &xmitq);
240
	tipc_bcast_unlock(net);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	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 已提交
261
	struct tipc_dest *dst, *tmp;
262
	struct sk_buff_head _pkts;
J
Jon Maloy 已提交
263
	u32 dnode, selector;
264 265

	selector = msg_link_selector(buf_msg(skb_peek(pkts)));
266
	skb_queue_head_init(&_pkts);
267

J
Jon Maloy 已提交
268 269 270
	list_for_each_entry_safe(dst, tmp, &dests->list, list) {
		dnode = dst->node;
		if (!tipc_msg_pskb_copy(dnode, pkts, &_pkts))
271
			return -ENOMEM;
272

273
		/* Any other return value than -ELINKCONG is ignored */
J
Jon Maloy 已提交
274
		if (tipc_node_xmit(net, &_pkts, dnode, selector) == -ELINKCONG)
275
			(*cong_link_cnt)++;
276
	}
277 278
	return 0;
}
279

280 281 282 283
/* 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
284 285
 * @method: send method to be used
 * @dests: destination nodes for message.
286 287 288 289 290
 * @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,
291 292
		    struct tipc_mc_method *method, struct tipc_nlist *dests,
		    u16 *cong_link_cnt)
293 294 295 296 297 298 299 300 301 302 303 304
{
	struct sk_buff_head inputq, localq;
	int rc = 0;

	skb_queue_head_init(&inputq);
	skb_queue_head_init(&localq);

	/* Clone packets before they are consumed by next call */
	if (dests->local && !tipc_msg_reassemble(pkts, &localq)) {
		rc = -ENOMEM;
		goto exit;
	}
305
	/* Send according to determined transmit method */
306
	if (dests->remote) {
307 308
		tipc_bcast_select_xmit_method(net, dests->remote, method);
		if (method->rcast)
309 310 311 312 313 314 315 316
			rc = tipc_rcast_xmit(net, pkts, dests, cong_link_cnt);
		else
			rc = tipc_bcast_xmit(net, pkts, cong_link_cnt);
	}

	if (dests->local)
		tipc_sk_mcast_rcv(net, &localq, &inputq);
exit:
317
	/* This queue should normally be empty by now */
318
	__skb_queue_purge(pkts);
319
	return rc;
320
}
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

/* 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);

347
	tipc_bcbase_xmit(net, &xmitq);
348 349 350 351 352 353 354 355 356 357 358 359

	/* 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
 */
360 361
void tipc_bcast_ack_rcv(struct net *net, struct tipc_link *l,
			struct tipc_msg *hdr)
362 363
{
	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
364
	u16 acked = msg_bcast_ack(hdr);
365 366
	struct sk_buff_head xmitq;

367 368 369 370
	/* Ignore bc acks sent by peer before bcast synch point was received */
	if (msg_bc_ack_invalid(hdr))
		return;

371 372 373 374 375 376
	__skb_queue_head_init(&xmitq);

	tipc_bcast_lock(net);
	tipc_link_bc_ack_rcv(l, acked, &xmitq);
	tipc_bcast_unlock(net);

377
	tipc_bcbase_xmit(net, &xmitq);
378 379 380 381 382 383 384 385 386 387

	/* 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
 */
388 389
int tipc_bcast_sync_rcv(struct net *net, struct tipc_link *l,
			struct tipc_msg *hdr)
390 391 392
{
	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
	struct sk_buff_head xmitq;
393
	int rc = 0;
394 395 396 397

	__skb_queue_head_init(&xmitq);

	tipc_bcast_lock(net);
398 399 400
	if (msg_type(hdr) != STATE_MSG) {
		tipc_link_bc_init_rcv(l, hdr);
	} else if (!msg_bc_ack_invalid(hdr)) {
401
		tipc_link_bc_ack_rcv(l, msg_bcast_ack(hdr), &xmitq);
402
		rc = tipc_link_bc_sync_rcv(l, hdr, &xmitq);
403 404 405
	}
	tipc_bcast_unlock(net);

406
	tipc_bcbase_xmit(net, &xmitq);
407 408 409 410

	/* Any socket wakeup messages ? */
	if (!skb_queue_empty(inputq))
		tipc_sk_rcv(net, inputq);
411
	return rc;
412 413 414 415 416 417
}

/* tipc_bcast_add_peer - add a peer node to broadcast link and bearer
 *
 * RCU is locked, node lock is set
 */
418
void tipc_bcast_add_peer(struct net *net, struct tipc_link *uc_l,
419 420 421 422
			 struct sk_buff_head *xmitq)
{
	struct tipc_link *snd_l = tipc_bc_sndlink(net);

423
	tipc_bcast_lock(net);
424
	tipc_link_add_bc_peer(snd_l, uc_l, xmitq);
425
	tipc_bcbase_select_primary(net);
426
	tipc_bcbase_calc_bc_threshold(net);
427
	tipc_bcast_unlock(net);
428 429 430 431 432 433
}

/* tipc_bcast_remove_peer - remove a peer node from broadcast link and bearer
 *
 * RCU is locked, node lock is set
 */
434
void tipc_bcast_remove_peer(struct net *net, struct tipc_link *rcv_l)
435 436
{
	struct tipc_link *snd_l = tipc_bc_sndlink(net);
437
	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
438 439 440 441
	struct sk_buff_head xmitq;

	__skb_queue_head_init(&xmitq);

442
	tipc_bcast_lock(net);
443
	tipc_link_remove_bc_peer(snd_l, rcv_l, &xmitq);
444
	tipc_bcbase_select_primary(net);
445
	tipc_bcbase_calc_bc_threshold(net);
446
	tipc_bcast_unlock(net);
447

448
	tipc_bcbase_xmit(net, &xmitq);
449 450 451 452 453 454

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

455
int tipc_bclink_reset_stats(struct net *net)
P
Per Liden 已提交
456
{
457
	struct tipc_link *l = tipc_bc_sndlink(net);
458

459
	if (!l)
P
Per Liden 已提交
460 461
		return -ENOPROTOOPT;

462
	tipc_bcast_lock(net);
463
	tipc_link_reset_stats(l);
464
	tipc_bcast_unlock(net);
465
	return 0;
P
Per Liden 已提交
466 467
}

468
static int tipc_bc_link_set_queue_limits(struct net *net, u32 limit)
P
Per Liden 已提交
469
{
470
	struct tipc_link *l = tipc_bc_sndlink(net);
471

472
	if (!l)
P
Per Liden 已提交
473
		return -ENOPROTOOPT;
474 475 476
	if (limit < BCLINK_WIN_MIN)
		limit = BCLINK_WIN_MIN;
	if (limit > TIPC_MAX_LINK_WIN)
P
Per Liden 已提交
477
		return -EINVAL;
478 479 480
	tipc_bcast_lock(net);
	tipc_link_set_queue_limits(l, limit);
	tipc_bcast_unlock(net);
481
	return 0;
P
Per Liden 已提交
482 483
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
int tipc_nl_bc_link_set(struct net *net, struct nlattr *attrs[])
{
	int err;
	u32 win;
	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;

	if (!props[TIPC_NLA_PROP_WIN])
		return -EOPNOTSUPP;

	win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);

502
	return tipc_bc_link_set_queue_limits(net, win);
503 504
}

505
int tipc_bcast_init(struct net *net)
P
Per Liden 已提交
506
{
507 508 509 510 511 512 513 514
	struct tipc_net *tn = tipc_net(net);
	struct tipc_bc_base *bb = NULL;
	struct tipc_link *l = NULL;

	bb = kzalloc(sizeof(*bb), GFP_ATOMIC);
	if (!bb)
		goto enomem;
	tn->bcbase = bb;
515
	spin_lock_init(&tipc_net(net)->bclock);
516

517
	if (!tipc_link_bc_create(net, 0, 0,
518
				 U16_MAX,
519
				 BCLINK_WIN_DEFAULT,
520
				 0,
521
				 &bb->inputq,
522
				 NULL,
523
				 NULL,
524 525 526 527
				 &l))
		goto enomem;
	bb->link = l;
	tn->bcl = l;
528 529
	bb->rc_ratio = 25;
	bb->rcast_support = true;
530
	return 0;
531 532 533 534
enomem:
	kfree(bb);
	kfree(l);
	return -ENOMEM;
P
Per Liden 已提交
535 536
}

537
void tipc_bcast_stop(struct net *net)
P
Per Liden 已提交
538
{
539 540
	struct tipc_net *tn = net_generic(net, tipc_net_id);

541
	synchronize_net();
542
	kfree(tn->bcbase);
543
	kfree(tn->bcl);
P
Per Liden 已提交
544
}
545 546 547 548 549 550 551 552 553 554 555 556

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 已提交
557
	else if (tipc_dest_push(&nl->list, node, 0))
558 559 560 561 562 563 564
		nl->remote++;
}

void tipc_nlist_del(struct tipc_nlist *nl, u32 node)
{
	if (node == nl->self)
		nl->local = false;
J
Jon Maloy 已提交
565
	else if (tipc_dest_del(&nl->list, node, 0))
566 567 568 569 570
		nl->remote--;
}

void tipc_nlist_purge(struct tipc_nlist *nl)
{
J
Jon Maloy 已提交
571
	tipc_dest_list_purge(&nl->list);
572 573 574
	nl->remote = 0;
	nl->local = 0;
}