xref: /openbmc/linux/net/tipc/name_distr.c (revision 8f19afb2dbc885befef2a4e7931dfcb51702a212)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/name_distr.c: TIPC name distribution code
3b97bf3fdSPer Liden  *
4593a5f22SPer Liden  * Copyright (c) 2000-2006, Ericsson AB
5431697ebSAllan Stephens  * Copyright (c) 2005, 2010-2011, 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 "link.h"
39b97bf3fdSPer Liden #include "name_distr.h"
40b97bf3fdSPer Liden 
41b97bf3fdSPer Liden #define ITEM_SIZE sizeof(struct distr_item)
42b97bf3fdSPer Liden 
43b97bf3fdSPer Liden /**
44b97bf3fdSPer Liden  * struct distr_item - publication info distributed to other nodes
45b97bf3fdSPer Liden  * @type: name sequence type
46b97bf3fdSPer Liden  * @lower: name sequence lower bound
47b97bf3fdSPer Liden  * @upper: name sequence upper bound
48b97bf3fdSPer Liden  * @ref: publishing port reference
49b97bf3fdSPer Liden  * @key: publication key
50b97bf3fdSPer Liden  *
51b97bf3fdSPer Liden  * ===> All fields are stored in network byte order. <===
52b97bf3fdSPer Liden  *
53b97bf3fdSPer Liden  * First 3 fields identify (name or) name sequence being published.
54b97bf3fdSPer Liden  * Reference field uniquely identifies port that published name sequence.
55b97bf3fdSPer Liden  * Key field uniquely identifies publication, in the event a port has
56b97bf3fdSPer Liden  * multiple publications of the same name sequence.
57b97bf3fdSPer Liden  *
58b97bf3fdSPer Liden  * Note: There is no field that identifies the publishing node because it is
59b97bf3fdSPer Liden  * the same for all items contained within a publication message.
60b97bf3fdSPer Liden  */
61b97bf3fdSPer Liden 
62b97bf3fdSPer Liden struct distr_item {
633e6c8cd5SAl Viro 	__be32 type;
643e6c8cd5SAl Viro 	__be32 lower;
653e6c8cd5SAl Viro 	__be32 upper;
663e6c8cd5SAl Viro 	__be32 ref;
673e6c8cd5SAl Viro 	__be32 key;
68b97bf3fdSPer Liden };
69b97bf3fdSPer Liden 
70b97bf3fdSPer Liden /**
71b97bf3fdSPer Liden  * List of externally visible publications by this node --
72b97bf3fdSPer Liden  * that is, all publications having scope > TIPC_NODE_SCOPE.
73b97bf3fdSPer Liden  */
74b97bf3fdSPer Liden 
75b97bf3fdSPer Liden static LIST_HEAD(publ_root);
76e3ec9c7dSAllan Stephens static u32 publ_cnt;
77b97bf3fdSPer Liden 
78b97bf3fdSPer Liden /**
79b97bf3fdSPer Liden  * publ_to_item - add publication info to a publication message
80b97bf3fdSPer Liden  */
81b97bf3fdSPer Liden 
82b97bf3fdSPer Liden static void publ_to_item(struct distr_item *i, struct publication *p)
83b97bf3fdSPer Liden {
84b97bf3fdSPer Liden 	i->type = htonl(p->type);
85b97bf3fdSPer Liden 	i->lower = htonl(p->lower);
86b97bf3fdSPer Liden 	i->upper = htonl(p->upper);
87b97bf3fdSPer Liden 	i->ref = htonl(p->ref);
88b97bf3fdSPer Liden 	i->key = htonl(p->key);
89b97bf3fdSPer Liden }
90b97bf3fdSPer Liden 
91b97bf3fdSPer Liden /**
92b97bf3fdSPer Liden  * named_prepare_buf - allocate & initialize a publication message
93b97bf3fdSPer Liden  */
94b97bf3fdSPer Liden 
95b97bf3fdSPer Liden static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest)
96b97bf3fdSPer Liden {
9731e3c3f6Sstephen hemminger 	struct sk_buff *buf = tipc_buf_acquire(LONG_H_SIZE + size);
98b97bf3fdSPer Liden 	struct tipc_msg *msg;
99b97bf3fdSPer Liden 
100b97bf3fdSPer Liden 	if (buf != NULL) {
101b97bf3fdSPer Liden 		msg = buf_msg(buf);
102c68ca7b7SAllan Stephens 		tipc_msg_init(msg, NAME_DISTRIBUTOR, type, LONG_H_SIZE, dest);
103b97bf3fdSPer Liden 		msg_set_size(msg, LONG_H_SIZE + size);
104b97bf3fdSPer Liden 	}
105b97bf3fdSPer Liden 	return buf;
106b97bf3fdSPer Liden }
107b97bf3fdSPer Liden 
1088f92df6aSAllan Stephens static void named_cluster_distribute(struct sk_buff *buf)
1098f92df6aSAllan Stephens {
1108f92df6aSAllan Stephens 	struct sk_buff *buf_copy;
1118f92df6aSAllan Stephens 	struct tipc_node *n_ptr;
1128f92df6aSAllan Stephens 
113672d99e1SAllan Stephens 	list_for_each_entry(n_ptr, &tipc_node_list, list) {
114*8f19afb2SPaul Gortmaker 		if (tipc_node_active_links(n_ptr)) {
1158f92df6aSAllan Stephens 			buf_copy = skb_copy(buf, GFP_ATOMIC);
1168f92df6aSAllan Stephens 			if (!buf_copy)
1178f92df6aSAllan Stephens 				break;
1188f92df6aSAllan Stephens 			msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
1198f92df6aSAllan Stephens 			tipc_link_send(buf_copy, n_ptr->addr, n_ptr->addr);
1208f92df6aSAllan Stephens 		}
1218f92df6aSAllan Stephens 	}
1228f92df6aSAllan Stephens 
1238f92df6aSAllan Stephens 	buf_discard(buf);
1248f92df6aSAllan Stephens }
1258f92df6aSAllan Stephens 
126b97bf3fdSPer Liden /**
1274323add6SPer Liden  * tipc_named_publish - tell other nodes about a new publication by this node
128b97bf3fdSPer Liden  */
129b97bf3fdSPer Liden 
1304323add6SPer Liden void tipc_named_publish(struct publication *publ)
131b97bf3fdSPer Liden {
132b97bf3fdSPer Liden 	struct sk_buff *buf;
133b97bf3fdSPer Liden 	struct distr_item *item;
134b97bf3fdSPer Liden 
13508c31f71SAllan Stephens 	list_add_tail(&publ->local_list, &publ_root);
136b97bf3fdSPer Liden 	publ_cnt++;
137b97bf3fdSPer Liden 
138b97bf3fdSPer Liden 	buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
139b97bf3fdSPer Liden 	if (!buf) {
140a10bd924SAllan Stephens 		warn("Publication distribution failure\n");
141b97bf3fdSPer Liden 		return;
142b97bf3fdSPer Liden 	}
143b97bf3fdSPer Liden 
144b97bf3fdSPer Liden 	item = (struct distr_item *)msg_data(buf_msg(buf));
145b97bf3fdSPer Liden 	publ_to_item(item, publ);
1468f92df6aSAllan Stephens 	named_cluster_distribute(buf);
147b97bf3fdSPer Liden }
148b97bf3fdSPer Liden 
149b97bf3fdSPer Liden /**
1504323add6SPer Liden  * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
151b97bf3fdSPer Liden  */
152b97bf3fdSPer Liden 
1534323add6SPer Liden void tipc_named_withdraw(struct publication *publ)
154b97bf3fdSPer Liden {
155b97bf3fdSPer Liden 	struct sk_buff *buf;
156b97bf3fdSPer Liden 	struct distr_item *item;
157b97bf3fdSPer Liden 
158b97bf3fdSPer Liden 	list_del(&publ->local_list);
159b97bf3fdSPer Liden 	publ_cnt--;
160b97bf3fdSPer Liden 
161b97bf3fdSPer Liden 	buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
162b97bf3fdSPer Liden 	if (!buf) {
163a10bd924SAllan Stephens 		warn("Withdrawl distribution failure\n");
164b97bf3fdSPer Liden 		return;
165b97bf3fdSPer Liden 	}
166b97bf3fdSPer Liden 
167b97bf3fdSPer Liden 	item = (struct distr_item *)msg_data(buf_msg(buf));
168b97bf3fdSPer Liden 	publ_to_item(item, publ);
1698f92df6aSAllan Stephens 	named_cluster_distribute(buf);
170b97bf3fdSPer Liden }
171b97bf3fdSPer Liden 
172b97bf3fdSPer Liden /**
1734323add6SPer Liden  * tipc_named_node_up - tell specified node about all publications by this node
174b97bf3fdSPer Liden  */
175b97bf3fdSPer Liden 
1764323add6SPer Liden void tipc_named_node_up(unsigned long node)
177b97bf3fdSPer Liden {
178b97bf3fdSPer Liden 	struct publication *publ;
1791fc54d8fSSam Ravnborg 	struct distr_item *item = NULL;
1801fc54d8fSSam Ravnborg 	struct sk_buff *buf = NULL;
181b97bf3fdSPer Liden 	u32 left = 0;
182b97bf3fdSPer Liden 	u32 rest;
183b97bf3fdSPer Liden 	u32 max_item_buf;
184b97bf3fdSPer Liden 
1854323add6SPer Liden 	read_lock_bh(&tipc_nametbl_lock);
186b97bf3fdSPer Liden 	max_item_buf = TIPC_MAX_USER_MSG_SIZE / ITEM_SIZE;
187b97bf3fdSPer Liden 	max_item_buf *= ITEM_SIZE;
188b97bf3fdSPer Liden 	rest = publ_cnt * ITEM_SIZE;
189b97bf3fdSPer Liden 
190b97bf3fdSPer Liden 	list_for_each_entry(publ, &publ_root, local_list) {
191b97bf3fdSPer Liden 		if (!buf) {
192b97bf3fdSPer Liden 			left = (rest <= max_item_buf) ? rest : max_item_buf;
193b97bf3fdSPer Liden 			rest -= left;
194b97bf3fdSPer Liden 			buf = named_prepare_buf(PUBLICATION, left, node);
195a10bd924SAllan Stephens 			if (!buf) {
196a10bd924SAllan Stephens 				warn("Bulk publication distribution failure\n");
197b97bf3fdSPer Liden 				goto exit;
198b97bf3fdSPer Liden 			}
199b97bf3fdSPer Liden 			item = (struct distr_item *)msg_data(buf_msg(buf));
200b97bf3fdSPer Liden 		}
201b97bf3fdSPer Liden 		publ_to_item(item, publ);
202b97bf3fdSPer Liden 		item++;
203b97bf3fdSPer Liden 		left -= ITEM_SIZE;
204b97bf3fdSPer Liden 		if (!left) {
205b97bf3fdSPer Liden 			msg_set_link_selector(buf_msg(buf), node);
2064323add6SPer Liden 			tipc_link_send(buf, node, node);
2071fc54d8fSSam Ravnborg 			buf = NULL;
208b97bf3fdSPer Liden 		}
209b97bf3fdSPer Liden 	}
210b97bf3fdSPer Liden exit:
2114323add6SPer Liden 	read_unlock_bh(&tipc_nametbl_lock);
212b97bf3fdSPer Liden }
213b97bf3fdSPer Liden 
214b97bf3fdSPer Liden /**
215f1379173SAllan Stephens  * named_purge_publ - remove publication associated with a failed node
216b97bf3fdSPer Liden  *
217b97bf3fdSPer Liden  * Invoked for each publication issued by a newly failed node.
218b97bf3fdSPer Liden  * Removes publication structure from name table & deletes it.
219b97bf3fdSPer Liden  * In rare cases the link may have come back up again when this
220b97bf3fdSPer Liden  * function is called, and we have two items representing the same
221b97bf3fdSPer Liden  * publication. Nudge this item's key to distinguish it from the other.
222b97bf3fdSPer Liden  */
223b97bf3fdSPer Liden 
224f1379173SAllan Stephens static void named_purge_publ(struct publication *publ)
225b97bf3fdSPer Liden {
226b97bf3fdSPer Liden 	struct publication *p;
227f131072cSAllan Stephens 
2284323add6SPer Liden 	write_lock_bh(&tipc_nametbl_lock);
229b97bf3fdSPer Liden 	publ->key += 1222345;
2304323add6SPer Liden 	p = tipc_nametbl_remove_publ(publ->type, publ->lower,
231b97bf3fdSPer Liden 				     publ->node, publ->ref, publ->key);
232431697ebSAllan Stephens 	if (p)
233431697ebSAllan Stephens 		tipc_nodesub_unsubscribe(&p->subscr);
2344323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
235f131072cSAllan Stephens 
236f131072cSAllan Stephens 	if (p != publ) {
237f131072cSAllan Stephens 		err("Unable to remove publication from failed node\n"
238f131072cSAllan Stephens 		    "(type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
239f131072cSAllan Stephens 		    publ->type, publ->lower, publ->node, publ->ref, publ->key);
240f131072cSAllan Stephens 	}
241f131072cSAllan Stephens 
242f131072cSAllan Stephens 	kfree(p);
243f131072cSAllan Stephens }
244b97bf3fdSPer Liden 
245b97bf3fdSPer Liden /**
2464323add6SPer Liden  * tipc_named_recv - process name table update message sent by another node
247b97bf3fdSPer Liden  */
248b97bf3fdSPer Liden 
2494323add6SPer Liden void tipc_named_recv(struct sk_buff *buf)
250b97bf3fdSPer Liden {
251b97bf3fdSPer Liden 	struct publication *publ;
252b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
253b97bf3fdSPer Liden 	struct distr_item *item = (struct distr_item *)msg_data(msg);
254b97bf3fdSPer Liden 	u32 count = msg_data_sz(msg) / ITEM_SIZE;
255b97bf3fdSPer Liden 
2564323add6SPer Liden 	write_lock_bh(&tipc_nametbl_lock);
257b97bf3fdSPer Liden 	while (count--) {
258b97bf3fdSPer Liden 		if (msg_type(msg) == PUBLICATION) {
2594323add6SPer Liden 			publ = tipc_nametbl_insert_publ(ntohl(item->type),
260b97bf3fdSPer Liden 							ntohl(item->lower),
261b97bf3fdSPer Liden 							ntohl(item->upper),
262b97bf3fdSPer Liden 							TIPC_CLUSTER_SCOPE,
263b97bf3fdSPer Liden 							msg_orignode(msg),
264b97bf3fdSPer Liden 							ntohl(item->ref),
265b97bf3fdSPer Liden 							ntohl(item->key));
266b97bf3fdSPer Liden 			if (publ) {
2674323add6SPer Liden 				tipc_nodesub_subscribe(&publ->subscr,
268b97bf3fdSPer Liden 						       msg_orignode(msg),
269b97bf3fdSPer Liden 						       publ,
270f1379173SAllan Stephens 						       (net_ev_handler)
271f1379173SAllan Stephens 						       named_purge_publ);
272b97bf3fdSPer Liden 			}
273b97bf3fdSPer Liden 		} else if (msg_type(msg) == WITHDRAWAL) {
2744323add6SPer Liden 			publ = tipc_nametbl_remove_publ(ntohl(item->type),
275b97bf3fdSPer Liden 							ntohl(item->lower),
276b97bf3fdSPer Liden 							msg_orignode(msg),
277b97bf3fdSPer Liden 							ntohl(item->ref),
278b97bf3fdSPer Liden 							ntohl(item->key));
279b97bf3fdSPer Liden 
280b97bf3fdSPer Liden 			if (publ) {
2814323add6SPer Liden 				tipc_nodesub_unsubscribe(&publ->subscr);
282b97bf3fdSPer Liden 				kfree(publ);
283f131072cSAllan Stephens 			} else {
284f131072cSAllan Stephens 				err("Unable to remove publication by node 0x%x\n"
285f131072cSAllan Stephens 				    "(type=%u, lower=%u, ref=%u, key=%u)\n",
286f131072cSAllan Stephens 				    msg_orignode(msg),
287f131072cSAllan Stephens 				    ntohl(item->type), ntohl(item->lower),
288f131072cSAllan Stephens 				    ntohl(item->ref), ntohl(item->key));
289b97bf3fdSPer Liden 			}
290b97bf3fdSPer Liden 		} else {
291a10bd924SAllan Stephens 			warn("Unrecognized name table message received\n");
292b97bf3fdSPer Liden 		}
293b97bf3fdSPer Liden 		item++;
294b97bf3fdSPer Liden 	}
2954323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
296b97bf3fdSPer Liden 	buf_discard(buf);
297b97bf3fdSPer Liden }
298b97bf3fdSPer Liden 
299b97bf3fdSPer Liden /**
3004323add6SPer Liden  * tipc_named_reinit - re-initialize local publication list
301b97bf3fdSPer Liden  *
302b97bf3fdSPer Liden  * This routine is called whenever TIPC networking is (re)enabled.
303b97bf3fdSPer Liden  * All existing publications by this node that have "cluster" or "zone" scope
304b97bf3fdSPer Liden  * are updated to reflect the node's current network address.
305b97bf3fdSPer Liden  * (If the node's address is unchanged, the update loop terminates immediately.)
306b97bf3fdSPer Liden  */
307b97bf3fdSPer Liden 
3084323add6SPer Liden void tipc_named_reinit(void)
309b97bf3fdSPer Liden {
310b97bf3fdSPer Liden 	struct publication *publ;
311b97bf3fdSPer Liden 
3124323add6SPer Liden 	write_lock_bh(&tipc_nametbl_lock);
313b97bf3fdSPer Liden 	list_for_each_entry(publ, &publ_root, local_list) {
314b97bf3fdSPer Liden 		if (publ->node == tipc_own_addr)
315b97bf3fdSPer Liden 			break;
316b97bf3fdSPer Liden 		publ->node = tipc_own_addr;
317b97bf3fdSPer Liden 	}
3184323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
319b97bf3fdSPer Liden }
320