node.c 19.2 KB
Newer Older
P
Per Liden 已提交
1 2
/*
 * net/tipc/node.c: TIPC node management routines
3
 *
P
Per Liden 已提交
4
 * Copyright (c) 2000-2006, Ericsson AB
5
 * Copyright (c) 2005-2006, Wind River Systems
P
Per Liden 已提交
6 7
 * All rights reserved.
 *
P
Per Liden 已提交
8
 * Redistribution and use in source and binary forms, with or without
P
Per Liden 已提交
9 10
 * modification, are permitted provided that the following conditions are met:
 *
P
Per Liden 已提交
11 12 13 14 15 16 17 18
 * 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 已提交
19
 *
P
Per Liden 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * 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 已提交
34 35 36 37 38 39 40 41 42
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "config.h"
#include "node.h"
#include "port.h"
#include "name_distr.h"

43 44
static void node_lost_contact(struct tipc_node *n_ptr);
static void node_established_contact(struct tipc_node *n_ptr);
P
Per Liden 已提交
45

46 47
/* sorted list of nodes within cluster */
static struct tipc_node *tipc_nodes = NULL;
P
Per Liden 已提交
48

49 50
static DEFINE_SPINLOCK(node_create_lock);

P
Per Liden 已提交
51 52
u32 tipc_own_tag = 0;

53 54 55 56 57 58 59 60 61 62
/**
 * tipc_node_create - create neighboring node
 *
 * Currently, this routine is called by neighbor discovery code, which holds
 * net_lock for reading only.  We must take node_create_lock to ensure a node
 * isn't created twice if two different bearers discover the node at the same
 * time.  (It would be preferable to switch to holding net_lock in write mode,
 * but this is a non-trivial change.)
 */

63
struct tipc_node *tipc_node_create(u32 addr)
P
Per Liden 已提交
64 65
{
	struct cluster *c_ptr;
66 67
	struct tipc_node *n_ptr;
	struct tipc_node **curr_node;
P
Per Liden 已提交
68

69 70 71 72 73 74 75 76 77 78 79
	spin_lock_bh(&node_create_lock);

	for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
		if (addr < n_ptr->addr)
			break;
		if (addr == n_ptr->addr) {
			spin_unlock_bh(&node_create_lock);
			return n_ptr;
		}
	}

80
	n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
81
	if (!n_ptr) {
82
		spin_unlock_bh(&node_create_lock);
83 84 85 86 87 88 89 90 91
		warn("Node creation failed, no memory\n");
		return NULL;
	}

	c_ptr = tipc_cltr_find(addr);
	if (!c_ptr) {
		c_ptr = tipc_cltr_create(addr);
	}
	if (!c_ptr) {
92
		spin_unlock_bh(&node_create_lock);
93 94 95
		kfree(n_ptr);
		return NULL;
	}
96

97
	n_ptr->addr = addr;
98
		spin_lock_init(&n_ptr->lock);
99 100 101 102 103 104
	INIT_LIST_HEAD(&n_ptr->nsub);
	n_ptr->owner = c_ptr;
	tipc_cltr_attach_node(c_ptr, n_ptr);
	n_ptr->last_router = -1;

	/* Insert node into ordered list */
105
	for (curr_node = &tipc_nodes; *curr_node;
106 107 108 109 110 111 112
	     curr_node = &(*curr_node)->next) {
		if (addr < (*curr_node)->addr) {
			n_ptr->next = *curr_node;
			break;
		}
	}
	(*curr_node) = n_ptr;
113
	spin_unlock_bh(&node_create_lock);
P
Per Liden 已提交
114 115 116
	return n_ptr;
}

117
void tipc_node_delete(struct tipc_node *n_ptr)
P
Per Liden 已提交
118 119 120 121 122 123 124 125 126 127
{
	if (!n_ptr)
		return;

	dbg("node %x deleted\n", n_ptr->addr);
	kfree(n_ptr);
}


/**
128
 * tipc_node_link_up - handle addition of link
129
 *
P
Per Liden 已提交
130 131 132
 * Link becomes active (alone or shared) or standby, depending on its priority.
 */

133
void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
P
Per Liden 已提交
134 135 136
{
	struct link **active = &n_ptr->active_links[0];

137 138
	n_ptr->working_links++;

P
Per Liden 已提交
139 140
	info("Established link <%s> on network plane %c\n",
	     l_ptr->name, l_ptr->b_ptr->net_plane);
141

P
Per Liden 已提交
142 143 144 145 146 147
	if (!active[0]) {
		dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
		active[0] = active[1] = l_ptr;
		node_established_contact(n_ptr);
		return;
	}
148
	if (l_ptr->priority < active[0]->priority) {
149
		info("New link <%s> becomes standby\n", l_ptr->name);
P
Per Liden 已提交
150 151
		return;
	}
152
	tipc_link_send_duplicate(active[0], l_ptr);
153
	if (l_ptr->priority == active[0]->priority) {
P
Per Liden 已提交
154 155 156
		active[0] = l_ptr;
		return;
	}
157 158 159
	info("Old link <%s> becomes standby\n", active[0]->name);
	if (active[1] != active[0])
		info("Old link <%s> becomes standby\n", active[1]->name);
P
Per Liden 已提交
160 161 162 163 164 165 166
	active[0] = active[1] = l_ptr;
}

/**
 * node_select_active_links - select active link
 */

167
static void node_select_active_links(struct tipc_node *n_ptr)
P
Per Liden 已提交
168 169 170 171 172
{
	struct link **active = &n_ptr->active_links[0];
	u32 i;
	u32 highest_prio = 0;

173
	active[0] = active[1] = NULL;
P
Per Liden 已提交
174 175

	for (i = 0; i < MAX_BEARERS; i++) {
176
		struct link *l_ptr = n_ptr->links[i];
P
Per Liden 已提交
177

178
		if (!l_ptr || !tipc_link_is_up(l_ptr) ||
P
Per Liden 已提交
179 180 181 182
		    (l_ptr->priority < highest_prio))
			continue;

		if (l_ptr->priority > highest_prio) {
183
			highest_prio = l_ptr->priority;
P
Per Liden 已提交
184 185 186 187 188 189 190 191
			active[0] = active[1] = l_ptr;
		} else {
			active[1] = l_ptr;
		}
	}
}

/**
192
 * tipc_node_link_down - handle loss of link
P
Per Liden 已提交
193 194
 */

195
void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
P
Per Liden 已提交
196 197 198
{
	struct link **active;

199 200
	n_ptr->working_links--;

201
	if (!tipc_link_is_active(l_ptr)) {
P
Per Liden 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215
		info("Lost standby link <%s> on network plane %c\n",
		     l_ptr->name, l_ptr->b_ptr->net_plane);
		return;
	}
	info("Lost link <%s> on network plane %c\n",
		l_ptr->name, l_ptr->b_ptr->net_plane);

	active = &n_ptr->active_links[0];
	if (active[0] == l_ptr)
		active[0] = active[1];
	if (active[1] == l_ptr)
		active[1] = active[0];
	if (active[0] == l_ptr)
		node_select_active_links(n_ptr);
216
	if (tipc_node_is_up(n_ptr))
217
		tipc_link_changeover(l_ptr);
218
	else
P
Per Liden 已提交
219 220 221
		node_lost_contact(n_ptr);
}

222
int tipc_node_has_active_links(struct tipc_node *n_ptr)
P
Per Liden 已提交
223
{
224
	return n_ptr->active_links[0] != NULL;
P
Per Liden 已提交
225 226
}

227
int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
P
Per Liden 已提交
228
{
E
Eric Dumazet 已提交
229
	return n_ptr->working_links > 1;
P
Per Liden 已提交
230 231
}

232
static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
P
Per Liden 已提交
233
{
E
Eric Dumazet 已提交
234
	return n_ptr && (n_ptr->last_router >= 0);
P
Per Liden 已提交
235 236
}

237
int tipc_node_is_up(struct tipc_node *n_ptr)
P
Per Liden 已提交
238
{
E
Eric Dumazet 已提交
239
	return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr);
P
Per Liden 已提交
240 241
}

242
struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
P
Per Liden 已提交
243
{
244
	struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
P
Per Liden 已提交
245 246

	if (!n_ptr)
247
		n_ptr = tipc_node_create(l_ptr->addr);
248
	if (n_ptr) {
P
Per Liden 已提交
249 250 251
		u32 bearer_id = l_ptr->b_ptr->identity;
		char addr_string[16];

252 253
		if (n_ptr->link_cnt >= 2) {
			err("Attempt to create third link to %s\n",
254
			    tipc_addr_string_fill(addr_string, n_ptr->addr));
255 256 257 258 259 260 261 262 263
			return NULL;
		}

		if (!n_ptr->links[bearer_id]) {
			n_ptr->links[bearer_id] = l_ptr;
			tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
			n_ptr->link_cnt++;
			return n_ptr;
		}
264
		err("Attempt to establish second link on <%s> to %s\n",
265
		    l_ptr->b_ptr->publ.name,
266
		    tipc_addr_string_fill(addr_string, l_ptr->addr));
267
	}
268
	return NULL;
P
Per Liden 已提交
269 270
}

271
void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
P
Per Liden 已提交
272
{
273
	n_ptr->links[l_ptr->b_ptr->identity] = NULL;
274
	tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
P
Per Liden 已提交
275 276 277 278 279 280 281
	n_ptr->link_cnt--;
}

/*
 * Routing table management - five cases to handle:
 *
 * 1: A link towards a zone/cluster external node comes up.
282 283 284
 *    => Send a multicast message updating routing tables of all
 *    system nodes within own cluster that the new destination
 *    can be reached via this node.
P
Per Liden 已提交
285 286 287
 *    (node.establishedContact()=>cluster.multicastNewRoute())
 *
 * 2: A link towards a slave node comes up.
288 289 290
 *    => Send a multicast message updating routing tables of all
 *    system nodes within own cluster that the new destination
 *    can be reached via this node.
P
Per Liden 已提交
291
 *    (node.establishedContact()=>cluster.multicastNewRoute())
292
 *    => Send a  message to the slave node about existence
P
Per Liden 已提交
293 294 295 296 297 298 299 300 301
 *    of all system nodes within cluster:
 *    (node.establishedContact()=>cluster.sendLocalRoutes())
 *
 * 3: A new cluster local system node becomes available.
 *    => Send message(s) to this particular node containing
 *    information about all cluster external and slave
 *     nodes which can be reached via this node.
 *    (node.establishedContact()==>network.sendExternalRoutes())
 *    (node.establishedContact()==>network.sendSlaveRoutes())
302
 *    => Send messages to all directly connected slave nodes
P
Per Liden 已提交
303 304
 *    containing information about the existence of the new node
 *    (node.establishedContact()=>cluster.multicastNewRoute())
305
 *
P
Per Liden 已提交
306 307
 * 4: The link towards a zone/cluster external node or slave
 *    node goes down.
308
 *    => Send a multcast message updating routing tables of all
P
Per Liden 已提交
309 310 311 312 313 314 315 316 317
 *    nodes within cluster that the new destination can not any
 *    longer be reached via this node.
 *    (node.lostAllLinks()=>cluster.bcastLostRoute())
 *
 * 5: A cluster local system node becomes unavailable.
 *    => Remove all references to this node from the local
 *    routing tables. Note: This is a completely node
 *    local operation.
 *    (node.lostAllLinks()=>network.removeAsRouter())
318
 *    => Send messages to all directly connected slave nodes
P
Per Liden 已提交
319 320 321 322 323
 *    containing information about loss of the node
 *    (node.establishedContact()=>cluster.multicastLostRoute())
 *
 */

324
static void node_established_contact(struct tipc_node *n_ptr)
P
Per Liden 已提交
325 326 327 328
{
	struct cluster *c_ptr;

	dbg("node_established_contact:-> %x\n", n_ptr->addr);
329
	if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
330
		tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
P
Per Liden 已提交
331 332
	}

333 334
	/* Syncronize broadcast acks */
	n_ptr->bclink.acked = tipc_bclink_get_last_sent();
P
Per Liden 已提交
335 336 337 338 339

	if (is_slave(tipc_own_addr))
		return;
	if (!in_own_cluster(n_ptr->addr)) {
		/* Usage case 1 (see above) */
340
		c_ptr = tipc_cltr_find(tipc_own_addr);
P
Per Liden 已提交
341
		if (!c_ptr)
342
			c_ptr = tipc_cltr_create(tipc_own_addr);
343 344
		if (c_ptr)
			tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
345
						  tipc_max_nodes);
P
Per Liden 已提交
346
		return;
347
	}
P
Per Liden 已提交
348 349 350 351

	c_ptr = n_ptr->owner;
	if (is_slave(n_ptr->addr)) {
		/* Usage case 2 (see above) */
352 353
		tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
		tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
P
Per Liden 已提交
354 355 356 357
		return;
	}

	if (n_ptr->bclink.supported) {
358
		tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
P
Per Liden 已提交
359 360 361 362 363
		if (n_ptr->addr < tipc_own_addr)
			tipc_own_tag++;
	}

	/* Case 3 (see above) */
364 365 366 367
	tipc_net_send_external_routes(n_ptr->addr);
	tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
	tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
				  tipc_highest_allowed_slave);
P
Per Liden 已提交
368 369
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383
static void node_cleanup_finished(unsigned long node_addr)
{
	struct tipc_node *n_ptr;

	read_lock_bh(&tipc_net_lock);
	n_ptr = tipc_node_find(node_addr);
	if (n_ptr) {
		tipc_node_lock(n_ptr);
		n_ptr->cleanup_required = 0;
		tipc_node_unlock(n_ptr);
	}
	read_unlock_bh(&tipc_net_lock);
}

384
static void node_lost_contact(struct tipc_node *n_ptr)
P
Per Liden 已提交
385 386
{
	struct cluster *c_ptr;
387
	struct tipc_node_subscr *ns, *tns;
P
Per Liden 已提交
388 389 390
	char addr_string[16];
	u32 i;

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	/* Clean up broadcast reception remains */
	n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
	while (n_ptr->bclink.deferred_head) {
		struct sk_buff* buf = n_ptr->bclink.deferred_head;
		n_ptr->bclink.deferred_head = buf->next;
		buf_discard(buf);
	}
	if (n_ptr->bclink.defragm) {
		buf_discard(n_ptr->bclink.defragm);
		n_ptr->bclink.defragm = NULL;
	}
	if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
		tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
	}

	/* Update routing tables */
P
Per Liden 已提交
407
	if (is_slave(tipc_own_addr)) {
408
		tipc_net_remove_as_router(n_ptr->addr);
P
Per Liden 已提交
409
	} else {
410
		if (!in_own_cluster(n_ptr->addr)) {
P
Per Liden 已提交
411
			/* Case 4 (see above) */
412 413 414
			c_ptr = tipc_cltr_find(tipc_own_addr);
			tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
						   tipc_max_nodes);
P
Per Liden 已提交
415 416
		} else {
			/* Case 5 (see above) */
417
			c_ptr = tipc_cltr_find(n_ptr->addr);
P
Per Liden 已提交
418
			if (is_slave(n_ptr->addr)) {
419 420
				tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
							   tipc_max_nodes);
P
Per Liden 已提交
421 422
			} else {
				if (n_ptr->bclink.supported) {
423
					tipc_nmap_remove(&tipc_cltr_bcast_nodes,
424
							 n_ptr->addr);
P
Per Liden 已提交
425 426 427
					if (n_ptr->addr < tipc_own_addr)
						tipc_own_tag--;
				}
428 429 430 431
				tipc_net_remove_as_router(n_ptr->addr);
				tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
							   LOWEST_SLAVE,
							   tipc_highest_allowed_slave);
P
Per Liden 已提交
432 433 434
			}
		}
	}
435
	if (tipc_node_has_active_routes(n_ptr))
P
Per Liden 已提交
436 437
		return;

438
	info("Lost contact with %s\n",
439
	     tipc_addr_string_fill(addr_string, n_ptr->addr));
P
Per Liden 已提交
440 441 442 443

	/* Abort link changeover */
	for (i = 0; i < MAX_BEARERS; i++) {
		struct link *l_ptr = n_ptr->links[i];
444
		if (!l_ptr)
P
Per Liden 已提交
445 446 447
			continue;
		l_ptr->reset_checkpoint = l_ptr->next_in_no;
		l_ptr->exp_msg_count = 0;
448
		tipc_link_reset_fragments(l_ptr);
P
Per Liden 已提交
449 450 451 452
	}

	/* Notify subscribers */
	list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
453
		ns->node = NULL;
P
Per Liden 已提交
454
		list_del_init(&ns->nodesub_list);
455 456
		tipc_k_signal((Handler)ns->handle_node_down,
			      (unsigned long)ns->usr_handle);
P
Per Liden 已提交
457
	}
458 459 460 461 462

	/* Prevent re-contact with node until all cleanup is done */

	n_ptr->cleanup_required = 1;
	tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
P
Per Liden 已提交
463 464 465
}

/**
466
 * tipc_node_select_next_hop - find the next-hop node for a message
467
 *
P
Per Liden 已提交
468 469 470
 * Called by when cluster local lookup has failed.
 */

471
struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
P
Per Liden 已提交
472
{
473
	struct tipc_node *n_ptr;
P
Per Liden 已提交
474 475
	u32 router_addr;

476 477
	if (!tipc_addr_domain_valid(addr))
		return NULL;
P
Per Liden 已提交
478 479

	/* Look for direct link to destination processsor */
480 481
	n_ptr = tipc_node_find(addr);
	if (n_ptr && tipc_node_has_active_links(n_ptr))
482
		return n_ptr;
P
Per Liden 已提交
483 484 485

	/* Cluster local system nodes *must* have direct links */
	if (!is_slave(addr) && in_own_cluster(addr))
486
		return NULL;
P
Per Liden 已提交
487 488

	/* Look for cluster local router with direct link to node */
489
	router_addr = tipc_node_select_router(n_ptr, selector);
490 491
	if (router_addr)
		return tipc_node_select(router_addr, selector);
P
Per Liden 已提交
492

493
	/* Slave nodes can only be accessed within own cluster via a
P
Per Liden 已提交
494 495
	   known router with direct link -- if no router was found,give up */
	if (is_slave(addr))
496
		return NULL;
P
Per Liden 已提交
497 498 499

	/* Inter zone/cluster -- find any direct link to remote cluster */
	addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
500 501
	n_ptr = tipc_net_select_remote_node(addr, selector);
	if (n_ptr && tipc_node_has_active_links(n_ptr))
502
		return n_ptr;
P
Per Liden 已提交
503 504

	/* Last resort -- look for any router to anywhere in remote zone */
505
	router_addr =  tipc_net_select_router(addr, selector);
506 507
	if (router_addr)
		return tipc_node_select(router_addr, selector);
P
Per Liden 已提交
508

509
	return NULL;
P
Per Liden 已提交
510 511 512
}

/**
513
 * tipc_node_select_router - select router to reach specified node
514 515
 *
 * Uses a deterministic and fair algorithm for selecting router node.
P
Per Liden 已提交
516 517
 */

518
u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
P
Per Liden 已提交
519 520 521 522 523 524
{
	u32 ulim;
	u32 mask;
	u32 start;
	u32 r;

525 526
	if (!n_ptr)
		return 0;
P
Per Liden 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

	if (n_ptr->last_router < 0)
		return 0;
	ulim = ((n_ptr->last_router + 1) * 32) - 1;

	/* Start entry must be random */
	mask = tipc_max_nodes;
	while (mask > ulim)
		mask >>= 1;
	start = ref & mask;
	r = start;

	/* Lookup upwards with wrap-around */
	do {
		if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
			break;
	} while (++r <= ulim);
	if (r > ulim) {
		r = 1;
		do {
			if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
				break;
		} while (++r < start);
		assert(r != start);
	}
	assert(r && (r <= ulim));
	return tipc_addr(own_zone(), own_cluster(), r);
}

556
void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
P
Per Liden 已提交
557 558 559
{
	u32 r_num = tipc_node(router);

560
	n_ptr->routers[r_num / 32] =
P
Per Liden 已提交
561 562
		((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
	n_ptr->last_router = tipc_max_nodes / 32;
563
	while ((--n_ptr->last_router >= 0) &&
P
Per Liden 已提交
564 565 566
	       !n_ptr->routers[n_ptr->last_router]);
}

567
void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
P
Per Liden 已提交
568 569 570 571 572 573 574 575 576
{
	u32 r_num = tipc_node(router);

	if (n_ptr->last_router < 0)
		return;		/* No routes */

	n_ptr->routers[r_num / 32] =
		((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
	n_ptr->last_router = tipc_max_nodes / 32;
577
	while ((--n_ptr->last_router >= 0) &&
P
Per Liden 已提交
578 579
	       !n_ptr->routers[n_ptr->last_router]);

580
	if (!tipc_node_is_up(n_ptr))
P
Per Liden 已提交
581 582 583
		node_lost_contact(n_ptr);
}

584
struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
P
Per Liden 已提交
585 586 587
{
	u32 domain;
	struct sk_buff *buf;
588
	struct tipc_node *n_ptr;
589
	struct tipc_node_info node_info;
590
	u32 payload_size;
P
Per Liden 已提交
591 592

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
593
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
594

A
Al Viro 已提交
595
	domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
596 597 598
	if (!tipc_addr_domain_valid(domain))
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (network address)");
P
Per Liden 已提交
599

600 601 602
	read_lock_bh(&tipc_net_lock);
	if (!tipc_nodes) {
		read_unlock_bh(&tipc_net_lock);
603
		return tipc_cfg_reply_none();
604
	}
P
Per Liden 已提交
605

606
	/* For now, get space for all other nodes
P
Per Liden 已提交
607 608
	   (will need to modify this when slave nodes are supported */

609
	payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
610 611
	if (payload_size > 32768u) {
		read_unlock_bh(&tipc_net_lock);
612 613
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
						   " (too many nodes)");
614
	}
615
	buf = tipc_cfg_reply_alloc(payload_size);
616 617
	if (!buf) {
		read_unlock_bh(&tipc_net_lock);
P
Per Liden 已提交
618
		return NULL;
619
	}
P
Per Liden 已提交
620 621 622

	/* Add TLVs for all nodes in scope */

623
	for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
624
		if (!tipc_in_scope(domain, n_ptr->addr))
P
Per Liden 已提交
625
			continue;
626 627 628
		node_info.addr = htonl(n_ptr->addr);
		node_info.up = htonl(tipc_node_is_up(n_ptr));
		tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
629
				    &node_info, sizeof(node_info));
P
Per Liden 已提交
630 631
	}

632
	read_unlock_bh(&tipc_net_lock);
P
Per Liden 已提交
633 634 635
	return buf;
}

636
struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
P
Per Liden 已提交
637 638 639
{
	u32 domain;
	struct sk_buff *buf;
640
	struct tipc_node *n_ptr;
641
	struct tipc_link_info link_info;
642
	u32 payload_size;
P
Per Liden 已提交
643 644

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
645
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
646

A
Al Viro 已提交
647
	domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
648 649 650
	if (!tipc_addr_domain_valid(domain))
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (network address)");
P
Per Liden 已提交
651

652 653 654
	if (tipc_mode != TIPC_NET_MODE)
		return tipc_cfg_reply_none();

655 656
	read_lock_bh(&tipc_net_lock);

657 658 659 660
	/* Get space for all unicast links + multicast link */

	payload_size = TLV_SPACE(sizeof(link_info)) *
		(tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
661 662
	if (payload_size > 32768u) {
		read_unlock_bh(&tipc_net_lock);
663 664
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
						   " (too many links)");
665
	}
666
	buf = tipc_cfg_reply_alloc(payload_size);
667 668
	if (!buf) {
		read_unlock_bh(&tipc_net_lock);
P
Per Liden 已提交
669
		return NULL;
670
	}
P
Per Liden 已提交
671 672 673

	/* Add TLV for broadcast link */

674 675
	link_info.dest = htonl(tipc_own_addr & 0xfffff00);
	link_info.up = htonl(1);
676
	strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
677
	tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
P
Per Liden 已提交
678 679 680

	/* Add TLVs for any other links in scope */

681
	for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
682
		u32 i;
P
Per Liden 已提交
683

684
		if (!tipc_in_scope(domain, n_ptr->addr))
P
Per Liden 已提交
685
			continue;
686
		tipc_node_lock(n_ptr);
687 688 689 690 691 692 693
		for (i = 0; i < MAX_BEARERS; i++) {
			if (!n_ptr->links[i])
				continue;
			link_info.dest = htonl(n_ptr->addr);
			link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
			strcpy(link_info.str, n_ptr->links[i]->name);
			tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
694
					    &link_info, sizeof(link_info));
695
		}
696
		tipc_node_unlock(n_ptr);
P
Per Liden 已提交
697 698
	}

699
	read_unlock_bh(&tipc_net_lock);
P
Per Liden 已提交
700 701
	return buf;
}