xref: /linux/net/tipc/name_distr.c (revision 3e6c8cd5669c1202fe806ce3e13d701f20a71c7e)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/name_distr.c: TIPC name distribution code
3b97bf3fdSPer Liden  *
4593a5f22SPer Liden  * Copyright (c) 2000-2006, Ericsson AB
5b97bf3fdSPer Liden  * Copyright (c) 2005, Wind River Systems
6b97bf3fdSPer Liden  * All rights reserved.
7b97bf3fdSPer Liden  *
8b97bf3fdSPer Liden  * Redistribution and use in source and binary forms, with or without
9b97bf3fdSPer Liden  * modification, are permitted provided that the following conditions are met:
10b97bf3fdSPer Liden  *
119ea1fd3cSPer Liden  * 1. Redistributions of source code must retain the above copyright
129ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer.
139ea1fd3cSPer Liden  * 2. Redistributions in binary form must reproduce the above copyright
149ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer in the
159ea1fd3cSPer Liden  *    documentation and/or other materials provided with the distribution.
169ea1fd3cSPer Liden  * 3. Neither the names of the copyright holders nor the names of its
179ea1fd3cSPer Liden  *    contributors may be used to endorse or promote products derived from
189ea1fd3cSPer Liden  *    this software without specific prior written permission.
199ea1fd3cSPer Liden  *
209ea1fd3cSPer Liden  * Alternatively, this software may be distributed under the terms of the
219ea1fd3cSPer Liden  * GNU General Public License ("GPL") version 2 as published by the Free
229ea1fd3cSPer Liden  * Software Foundation.
23b97bf3fdSPer Liden  *
24b97bf3fdSPer Liden  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25b97bf3fdSPer Liden  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26b97bf3fdSPer Liden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27b97bf3fdSPer Liden  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28b97bf3fdSPer Liden  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29b97bf3fdSPer Liden  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30b97bf3fdSPer Liden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31b97bf3fdSPer Liden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32b97bf3fdSPer Liden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33b97bf3fdSPer Liden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34b97bf3fdSPer Liden  * POSSIBILITY OF SUCH DAMAGE.
35b97bf3fdSPer Liden  */
36b97bf3fdSPer Liden 
37b97bf3fdSPer Liden #include "core.h"
38b97bf3fdSPer Liden #include "cluster.h"
39b97bf3fdSPer Liden #include "dbg.h"
40b97bf3fdSPer Liden #include "link.h"
41b97bf3fdSPer Liden #include "msg.h"
42b97bf3fdSPer Liden #include "name_distr.h"
43b97bf3fdSPer Liden 
44b97bf3fdSPer Liden #undef  DBG_OUTPUT
45b97bf3fdSPer Liden #define DBG_OUTPUT NULL
46b97bf3fdSPer Liden 
47b97bf3fdSPer Liden #define ITEM_SIZE sizeof(struct distr_item)
48b97bf3fdSPer Liden 
49b97bf3fdSPer Liden /**
50b97bf3fdSPer Liden  * struct distr_item - publication info distributed to other nodes
51b97bf3fdSPer Liden  * @type: name sequence type
52b97bf3fdSPer Liden  * @lower: name sequence lower bound
53b97bf3fdSPer Liden  * @upper: name sequence upper bound
54b97bf3fdSPer Liden  * @ref: publishing port reference
55b97bf3fdSPer Liden  * @key: publication key
56b97bf3fdSPer Liden  *
57b97bf3fdSPer Liden  * ===> All fields are stored in network byte order. <===
58b97bf3fdSPer Liden  *
59b97bf3fdSPer Liden  * First 3 fields identify (name or) name sequence being published.
60b97bf3fdSPer Liden  * Reference field uniquely identifies port that published name sequence.
61b97bf3fdSPer Liden  * Key field uniquely identifies publication, in the event a port has
62b97bf3fdSPer Liden  * multiple publications of the same name sequence.
63b97bf3fdSPer Liden  *
64b97bf3fdSPer Liden  * Note: There is no field that identifies the publishing node because it is
65b97bf3fdSPer Liden  * the same for all items contained within a publication message.
66b97bf3fdSPer Liden  */
67b97bf3fdSPer Liden 
68b97bf3fdSPer Liden struct distr_item {
69*3e6c8cd5SAl Viro 	__be32 type;
70*3e6c8cd5SAl Viro 	__be32 lower;
71*3e6c8cd5SAl Viro 	__be32 upper;
72*3e6c8cd5SAl Viro 	__be32 ref;
73*3e6c8cd5SAl Viro 	__be32 key;
74b97bf3fdSPer Liden };
75b97bf3fdSPer Liden 
76b97bf3fdSPer Liden /**
77b97bf3fdSPer Liden  * List of externally visible publications by this node --
78b97bf3fdSPer Liden  * that is, all publications having scope > TIPC_NODE_SCOPE.
79b97bf3fdSPer Liden  */
80b97bf3fdSPer Liden 
81b97bf3fdSPer Liden static LIST_HEAD(publ_root);
82b97bf3fdSPer Liden static u32 publ_cnt = 0;
83b97bf3fdSPer Liden 
84b97bf3fdSPer Liden /**
85b97bf3fdSPer Liden  * publ_to_item - add publication info to a publication message
86b97bf3fdSPer Liden  */
87b97bf3fdSPer Liden 
88b97bf3fdSPer Liden static void publ_to_item(struct distr_item *i, struct publication *p)
89b97bf3fdSPer Liden {
90b97bf3fdSPer Liden 	i->type = htonl(p->type);
91b97bf3fdSPer Liden 	i->lower = htonl(p->lower);
92b97bf3fdSPer Liden 	i->upper = htonl(p->upper);
93b97bf3fdSPer Liden 	i->ref = htonl(p->ref);
94b97bf3fdSPer Liden 	i->key = htonl(p->key);
95b97bf3fdSPer Liden 	dbg("publ_to_item: %u, %u, %u\n", p->type, p->lower, p->upper);
96b97bf3fdSPer Liden }
97b97bf3fdSPer Liden 
98b97bf3fdSPer Liden /**
99b97bf3fdSPer Liden  * named_prepare_buf - allocate & initialize a publication message
100b97bf3fdSPer Liden  */
101b97bf3fdSPer Liden 
102b97bf3fdSPer Liden static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest)
103b97bf3fdSPer Liden {
104b97bf3fdSPer Liden 	struct sk_buff *buf = buf_acquire(LONG_H_SIZE + size);
105b97bf3fdSPer Liden 	struct tipc_msg *msg;
106b97bf3fdSPer Liden 
107b97bf3fdSPer Liden 	if (buf != NULL) {
108b97bf3fdSPer Liden 		msg = buf_msg(buf);
109b97bf3fdSPer Liden 		msg_init(msg, NAME_DISTRIBUTOR, type, TIPC_OK,
110b97bf3fdSPer Liden 			 LONG_H_SIZE, dest);
111b97bf3fdSPer Liden 		msg_set_size(msg, LONG_H_SIZE + size);
112b97bf3fdSPer Liden 	}
113b97bf3fdSPer Liden 	return buf;
114b97bf3fdSPer Liden }
115b97bf3fdSPer Liden 
116b97bf3fdSPer Liden /**
1174323add6SPer Liden  * tipc_named_publish - tell other nodes about a new publication by this node
118b97bf3fdSPer Liden  */
119b97bf3fdSPer Liden 
1204323add6SPer Liden void tipc_named_publish(struct publication *publ)
121b97bf3fdSPer Liden {
122b97bf3fdSPer Liden 	struct sk_buff *buf;
123b97bf3fdSPer Liden 	struct distr_item *item;
124b97bf3fdSPer Liden 
12508c31f71SAllan Stephens 	list_add_tail(&publ->local_list, &publ_root);
126b97bf3fdSPer Liden 	publ_cnt++;
127b97bf3fdSPer Liden 
128b97bf3fdSPer Liden 	buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
129b97bf3fdSPer Liden 	if (!buf) {
130a10bd924SAllan Stephens 		warn("Publication distribution failure\n");
131b97bf3fdSPer Liden 		return;
132b97bf3fdSPer Liden 	}
133b97bf3fdSPer Liden 
134b97bf3fdSPer Liden 	item = (struct distr_item *)msg_data(buf_msg(buf));
135b97bf3fdSPer Liden 	publ_to_item(item, publ);
1364323add6SPer Liden 	dbg("tipc_named_withdraw: broadcasting publish msg\n");
1374323add6SPer Liden 	tipc_cltr_broadcast(buf);
138b97bf3fdSPer Liden }
139b97bf3fdSPer Liden 
140b97bf3fdSPer Liden /**
1414323add6SPer Liden  * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
142b97bf3fdSPer Liden  */
143b97bf3fdSPer Liden 
1444323add6SPer Liden void tipc_named_withdraw(struct publication *publ)
145b97bf3fdSPer Liden {
146b97bf3fdSPer Liden 	struct sk_buff *buf;
147b97bf3fdSPer Liden 	struct distr_item *item;
148b97bf3fdSPer Liden 
149b97bf3fdSPer Liden 	list_del(&publ->local_list);
150b97bf3fdSPer Liden 	publ_cnt--;
151b97bf3fdSPer Liden 
152b97bf3fdSPer Liden 	buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
153b97bf3fdSPer Liden 	if (!buf) {
154a10bd924SAllan Stephens 		warn("Withdrawl distribution failure\n");
155b97bf3fdSPer Liden 		return;
156b97bf3fdSPer Liden 	}
157b97bf3fdSPer Liden 
158b97bf3fdSPer Liden 	item = (struct distr_item *)msg_data(buf_msg(buf));
159b97bf3fdSPer Liden 	publ_to_item(item, publ);
1604323add6SPer Liden 	dbg("tipc_named_withdraw: broadcasting withdraw msg\n");
1614323add6SPer Liden 	tipc_cltr_broadcast(buf);
162b97bf3fdSPer Liden }
163b97bf3fdSPer Liden 
164b97bf3fdSPer Liden /**
1654323add6SPer Liden  * tipc_named_node_up - tell specified node about all publications by this node
166b97bf3fdSPer Liden  */
167b97bf3fdSPer Liden 
1684323add6SPer Liden void tipc_named_node_up(unsigned long node)
169b97bf3fdSPer Liden {
170b97bf3fdSPer Liden 	struct publication *publ;
1711fc54d8fSSam Ravnborg 	struct distr_item *item = NULL;
1721fc54d8fSSam Ravnborg 	struct sk_buff *buf = NULL;
173b97bf3fdSPer Liden 	u32 left = 0;
174b97bf3fdSPer Liden 	u32 rest;
175b97bf3fdSPer Liden 	u32 max_item_buf;
176b97bf3fdSPer Liden 
1774323add6SPer Liden 	read_lock_bh(&tipc_nametbl_lock);
178b97bf3fdSPer Liden 	max_item_buf = TIPC_MAX_USER_MSG_SIZE / ITEM_SIZE;
179b97bf3fdSPer Liden 	max_item_buf *= ITEM_SIZE;
180b97bf3fdSPer Liden 	rest = publ_cnt * ITEM_SIZE;
181b97bf3fdSPer Liden 
182b97bf3fdSPer Liden 	list_for_each_entry(publ, &publ_root, local_list) {
183b97bf3fdSPer Liden 		if (!buf) {
184b97bf3fdSPer Liden 			left = (rest <= max_item_buf) ? rest : max_item_buf;
185b97bf3fdSPer Liden 			rest -= left;
186b97bf3fdSPer Liden 			buf = named_prepare_buf(PUBLICATION, left, node);
187a10bd924SAllan Stephens 			if (!buf) {
188a10bd924SAllan Stephens 				warn("Bulk publication distribution failure\n");
189b97bf3fdSPer Liden 				goto exit;
190b97bf3fdSPer Liden 			}
191b97bf3fdSPer Liden 			item = (struct distr_item *)msg_data(buf_msg(buf));
192b97bf3fdSPer Liden 		}
193b97bf3fdSPer Liden 		publ_to_item(item, publ);
194b97bf3fdSPer Liden 		item++;
195b97bf3fdSPer Liden 		left -= ITEM_SIZE;
196b97bf3fdSPer Liden 		if (!left) {
197b97bf3fdSPer Liden 			msg_set_link_selector(buf_msg(buf), node);
1984323add6SPer Liden 			dbg("tipc_named_node_up: sending publish msg to "
199b97bf3fdSPer Liden 			    "<%u.%u.%u>\n", tipc_zone(node),
200b97bf3fdSPer Liden 			    tipc_cluster(node), tipc_node(node));
2014323add6SPer Liden 			tipc_link_send(buf, node, node);
2021fc54d8fSSam Ravnborg 			buf = NULL;
203b97bf3fdSPer Liden 		}
204b97bf3fdSPer Liden 	}
205b97bf3fdSPer Liden exit:
2064323add6SPer Liden 	read_unlock_bh(&tipc_nametbl_lock);
207b97bf3fdSPer Liden }
208b97bf3fdSPer Liden 
209b97bf3fdSPer Liden /**
210b97bf3fdSPer Liden  * node_is_down - remove publication associated with a failed node
211b97bf3fdSPer Liden  *
212b97bf3fdSPer Liden  * Invoked for each publication issued by a newly failed node.
213b97bf3fdSPer Liden  * Removes publication structure from name table & deletes it.
214b97bf3fdSPer Liden  * In rare cases the link may have come back up again when this
215b97bf3fdSPer Liden  * function is called, and we have two items representing the same
216b97bf3fdSPer Liden  * publication. Nudge this item's key to distinguish it from the other.
217b97bf3fdSPer Liden  * (Note: Publication's node subscription is already unsubscribed.)
218b97bf3fdSPer Liden  */
219b97bf3fdSPer Liden 
220b97bf3fdSPer Liden static void node_is_down(struct publication *publ)
221b97bf3fdSPer Liden {
222b97bf3fdSPer Liden 	struct publication *p;
223f131072cSAllan Stephens 
2244323add6SPer Liden         write_lock_bh(&tipc_nametbl_lock);
225b97bf3fdSPer Liden 	dbg("node_is_down: withdrawing %u, %u, %u\n",
226b97bf3fdSPer Liden 	    publ->type, publ->lower, publ->upper);
227b97bf3fdSPer Liden         publ->key += 1222345;
2284323add6SPer Liden 	p = tipc_nametbl_remove_publ(publ->type, publ->lower,
229b97bf3fdSPer Liden 				     publ->node, publ->ref, publ->key);
2304323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
231f131072cSAllan Stephens 
232f131072cSAllan Stephens         if (p != publ) {
233f131072cSAllan Stephens 		err("Unable to remove publication from failed node\n"
234f131072cSAllan Stephens 		    "(type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
235f131072cSAllan Stephens 		    publ->type, publ->lower, publ->node, publ->ref, publ->key);
236f131072cSAllan Stephens 	}
237f131072cSAllan Stephens 
238f131072cSAllan Stephens 	if (p) {
239f131072cSAllan Stephens 		kfree(p);
240f131072cSAllan Stephens 	}
241b97bf3fdSPer Liden }
242b97bf3fdSPer Liden 
243b97bf3fdSPer Liden /**
2444323add6SPer Liden  * tipc_named_recv - process name table update message sent by another node
245b97bf3fdSPer Liden  */
246b97bf3fdSPer Liden 
2474323add6SPer Liden void tipc_named_recv(struct sk_buff *buf)
248b97bf3fdSPer Liden {
249b97bf3fdSPer Liden 	struct publication *publ;
250b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
251b97bf3fdSPer Liden 	struct distr_item *item = (struct distr_item *)msg_data(msg);
252b97bf3fdSPer Liden 	u32 count = msg_data_sz(msg) / ITEM_SIZE;
253b97bf3fdSPer Liden 
2544323add6SPer Liden 	write_lock_bh(&tipc_nametbl_lock);
255b97bf3fdSPer Liden 	while (count--) {
256b97bf3fdSPer Liden 		if (msg_type(msg) == PUBLICATION) {
2574323add6SPer Liden 			dbg("tipc_named_recv: got publication for %u, %u, %u\n",
258b97bf3fdSPer Liden 			    ntohl(item->type), ntohl(item->lower),
259b97bf3fdSPer Liden 			    ntohl(item->upper));
2604323add6SPer Liden 			publ = tipc_nametbl_insert_publ(ntohl(item->type),
261b97bf3fdSPer Liden 							ntohl(item->lower),
262b97bf3fdSPer Liden 							ntohl(item->upper),
263b97bf3fdSPer Liden 							TIPC_CLUSTER_SCOPE,
264b97bf3fdSPer Liden 							msg_orignode(msg),
265b97bf3fdSPer Liden 							ntohl(item->ref),
266b97bf3fdSPer Liden 							ntohl(item->key));
267b97bf3fdSPer Liden 			if (publ) {
2684323add6SPer Liden 				tipc_nodesub_subscribe(&publ->subscr,
269b97bf3fdSPer Liden 						       msg_orignode(msg),
270b97bf3fdSPer Liden 						       publ,
271b97bf3fdSPer Liden 						       (net_ev_handler)node_is_down);
272b97bf3fdSPer Liden 			}
273b97bf3fdSPer Liden 		} else if (msg_type(msg) == WITHDRAWAL) {
2744323add6SPer Liden 			dbg("tipc_named_recv: got withdrawl for %u, %u, %u\n",
275b97bf3fdSPer Liden 			    ntohl(item->type), ntohl(item->lower),
276b97bf3fdSPer Liden 			    ntohl(item->upper));
2774323add6SPer Liden 			publ = tipc_nametbl_remove_publ(ntohl(item->type),
278b97bf3fdSPer Liden 							ntohl(item->lower),
279b97bf3fdSPer Liden 							msg_orignode(msg),
280b97bf3fdSPer Liden 							ntohl(item->ref),
281b97bf3fdSPer Liden 							ntohl(item->key));
282b97bf3fdSPer Liden 
283b97bf3fdSPer Liden 			if (publ) {
2844323add6SPer Liden 				tipc_nodesub_unsubscribe(&publ->subscr);
285b97bf3fdSPer Liden         			kfree(publ);
286f131072cSAllan Stephens 			} else {
287f131072cSAllan Stephens 				err("Unable to remove publication by node 0x%x\n"
288f131072cSAllan Stephens 				    "(type=%u, lower=%u, ref=%u, key=%u)\n",
289f131072cSAllan Stephens 				    msg_orignode(msg),
290f131072cSAllan Stephens 				    ntohl(item->type), ntohl(item->lower),
291f131072cSAllan Stephens 				    ntohl(item->ref), ntohl(item->key));
292b97bf3fdSPer Liden 			}
293b97bf3fdSPer Liden 		} else {
294a10bd924SAllan Stephens 			warn("Unrecognized name table message received\n");
295b97bf3fdSPer Liden 		}
296b97bf3fdSPer Liden 		item++;
297b97bf3fdSPer Liden 	}
2984323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
299b97bf3fdSPer Liden 	buf_discard(buf);
300b97bf3fdSPer Liden }
301b97bf3fdSPer Liden 
302b97bf3fdSPer Liden /**
3034323add6SPer Liden  * tipc_named_reinit - re-initialize local publication list
304b97bf3fdSPer Liden  *
305b97bf3fdSPer Liden  * This routine is called whenever TIPC networking is (re)enabled.
306b97bf3fdSPer Liden  * All existing publications by this node that have "cluster" or "zone" scope
307b97bf3fdSPer Liden  * are updated to reflect the node's current network address.
308b97bf3fdSPer Liden  * (If the node's address is unchanged, the update loop terminates immediately.)
309b97bf3fdSPer Liden  */
310b97bf3fdSPer Liden 
3114323add6SPer Liden void tipc_named_reinit(void)
312b97bf3fdSPer Liden {
313b97bf3fdSPer Liden 	struct publication *publ;
314b97bf3fdSPer Liden 
3154323add6SPer Liden 	write_lock_bh(&tipc_nametbl_lock);
316b97bf3fdSPer Liden 	list_for_each_entry(publ, &publ_root, local_list) {
317b97bf3fdSPer Liden 		if (publ->node == tipc_own_addr)
318b97bf3fdSPer Liden 			break;
319b97bf3fdSPer Liden 		publ->node = tipc_own_addr;
320b97bf3fdSPer Liden 	}
3214323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
322b97bf3fdSPer Liden }
323