xref: /openbmc/linux/net/tipc/name_distr.c (revision e11aa059715e2bacd4e62d57be5557dda697af8e)
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 /**
713f8375feSAllan Stephens  * struct publ_list - list of publications made by this node
723f8375feSAllan Stephens  * @list: circular list of publications
733f8375feSAllan Stephens  * @list_size: number of entries in list
74b97bf3fdSPer Liden  */
753f8375feSAllan Stephens struct publ_list {
763f8375feSAllan Stephens 	struct list_head list;
773f8375feSAllan Stephens 	u32 size;
783f8375feSAllan Stephens };
79b97bf3fdSPer Liden 
803f8375feSAllan Stephens static struct publ_list publ_cluster = {
813f8375feSAllan Stephens 	.list = LIST_HEAD_INIT(publ_cluster.list),
823f8375feSAllan Stephens 	.size = 0,
833f8375feSAllan Stephens };
84b97bf3fdSPer Liden 
85b97bf3fdSPer Liden /**
86b97bf3fdSPer Liden  * publ_to_item - add publication info to a publication message
87b97bf3fdSPer Liden  */
88b97bf3fdSPer Liden 
89b97bf3fdSPer Liden static void publ_to_item(struct distr_item *i, struct publication *p)
90b97bf3fdSPer Liden {
91b97bf3fdSPer Liden 	i->type = htonl(p->type);
92b97bf3fdSPer Liden 	i->lower = htonl(p->lower);
93b97bf3fdSPer Liden 	i->upper = htonl(p->upper);
94b97bf3fdSPer Liden 	i->ref = htonl(p->ref);
95b97bf3fdSPer Liden 	i->key = htonl(p->key);
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 {
104741d9eb7SAllan Stephens 	struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size);
105b97bf3fdSPer Liden 	struct tipc_msg *msg;
106b97bf3fdSPer Liden 
107b97bf3fdSPer Liden 	if (buf != NULL) {
108b97bf3fdSPer Liden 		msg = buf_msg(buf);
109741d9eb7SAllan Stephens 		tipc_msg_init(msg, NAME_DISTRIBUTOR, type, INT_H_SIZE, dest);
110741d9eb7SAllan Stephens 		msg_set_size(msg, INT_H_SIZE + size);
111b97bf3fdSPer Liden 	}
112b97bf3fdSPer Liden 	return buf;
113b97bf3fdSPer Liden }
114b97bf3fdSPer Liden 
1158f92df6aSAllan Stephens static void named_cluster_distribute(struct sk_buff *buf)
1168f92df6aSAllan Stephens {
1178f92df6aSAllan Stephens 	struct sk_buff *buf_copy;
1188f92df6aSAllan Stephens 	struct tipc_node *n_ptr;
1198f92df6aSAllan Stephens 
120672d99e1SAllan Stephens 	list_for_each_entry(n_ptr, &tipc_node_list, list) {
1218f19afb2SPaul Gortmaker 		if (tipc_node_active_links(n_ptr)) {
1228f92df6aSAllan Stephens 			buf_copy = skb_copy(buf, GFP_ATOMIC);
1238f92df6aSAllan Stephens 			if (!buf_copy)
1248f92df6aSAllan Stephens 				break;
1258f92df6aSAllan Stephens 			msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
1268f92df6aSAllan Stephens 			tipc_link_send(buf_copy, n_ptr->addr, n_ptr->addr);
1278f92df6aSAllan Stephens 		}
1288f92df6aSAllan Stephens 	}
1298f92df6aSAllan Stephens 
1305f6d9123SAllan Stephens 	kfree_skb(buf);
1318f92df6aSAllan Stephens }
1328f92df6aSAllan Stephens 
133b97bf3fdSPer Liden /**
1344323add6SPer Liden  * tipc_named_publish - tell other nodes about a new publication by this node
135b97bf3fdSPer Liden  */
136b97bf3fdSPer Liden 
1374323add6SPer Liden void tipc_named_publish(struct publication *publ)
138b97bf3fdSPer Liden {
139b97bf3fdSPer Liden 	struct sk_buff *buf;
140b97bf3fdSPer Liden 	struct distr_item *item;
141b97bf3fdSPer Liden 
1423f8375feSAllan Stephens 	list_add_tail(&publ->local_list, &publ_cluster.list);
1433f8375feSAllan Stephens 	publ_cluster.size++;
144b97bf3fdSPer Liden 
145b97bf3fdSPer Liden 	buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
146b97bf3fdSPer Liden 	if (!buf) {
147a10bd924SAllan Stephens 		warn("Publication distribution failure\n");
148b97bf3fdSPer Liden 		return;
149b97bf3fdSPer Liden 	}
150b97bf3fdSPer Liden 
151b97bf3fdSPer Liden 	item = (struct distr_item *)msg_data(buf_msg(buf));
152b97bf3fdSPer Liden 	publ_to_item(item, publ);
1538f92df6aSAllan Stephens 	named_cluster_distribute(buf);
154b97bf3fdSPer Liden }
155b97bf3fdSPer Liden 
156b97bf3fdSPer Liden /**
1574323add6SPer Liden  * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
158b97bf3fdSPer Liden  */
159b97bf3fdSPer Liden 
1604323add6SPer Liden void tipc_named_withdraw(struct publication *publ)
161b97bf3fdSPer Liden {
162b97bf3fdSPer Liden 	struct sk_buff *buf;
163b97bf3fdSPer Liden 	struct distr_item *item;
164b97bf3fdSPer Liden 
165b97bf3fdSPer Liden 	list_del(&publ->local_list);
1663f8375feSAllan Stephens 	publ_cluster.size--;
167b97bf3fdSPer Liden 
168b97bf3fdSPer Liden 	buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
169b97bf3fdSPer Liden 	if (!buf) {
17025985edcSLucas De Marchi 		warn("Withdrawal distribution failure\n");
171b97bf3fdSPer Liden 		return;
172b97bf3fdSPer Liden 	}
173b97bf3fdSPer Liden 
174b97bf3fdSPer Liden 	item = (struct distr_item *)msg_data(buf_msg(buf));
175b97bf3fdSPer Liden 	publ_to_item(item, publ);
1768f92df6aSAllan Stephens 	named_cluster_distribute(buf);
177b97bf3fdSPer Liden }
178b97bf3fdSPer Liden 
179*e11aa059SAllan Stephens /*
180*e11aa059SAllan Stephens  * named_distribute - prepare name info for bulk distribution to another node
181*e11aa059SAllan Stephens  */
182*e11aa059SAllan Stephens static void named_distribute(struct list_head *message_list, u32 node,
183*e11aa059SAllan Stephens 			     struct publ_list *pls, u32 max_item_buf)
184*e11aa059SAllan Stephens {
185*e11aa059SAllan Stephens 	struct publication *publ;
186*e11aa059SAllan Stephens 	struct sk_buff *buf = NULL;
187*e11aa059SAllan Stephens 	struct distr_item *item = NULL;
188*e11aa059SAllan Stephens 	u32 left = 0;
189*e11aa059SAllan Stephens 	u32 rest = pls->size * ITEM_SIZE;
190*e11aa059SAllan Stephens 
191*e11aa059SAllan Stephens 	list_for_each_entry(publ, &pls->list, local_list) {
192*e11aa059SAllan Stephens 		if (!buf) {
193*e11aa059SAllan Stephens 			left = (rest <= max_item_buf) ? rest : max_item_buf;
194*e11aa059SAllan Stephens 			rest -= left;
195*e11aa059SAllan Stephens 			buf = named_prepare_buf(PUBLICATION, left, node);
196*e11aa059SAllan Stephens 			if (!buf) {
197*e11aa059SAllan Stephens 				warn("Bulk publication failure\n");
198*e11aa059SAllan Stephens 				return;
199*e11aa059SAllan Stephens 			}
200*e11aa059SAllan Stephens 			item = (struct distr_item *)msg_data(buf_msg(buf));
201*e11aa059SAllan Stephens 		}
202*e11aa059SAllan Stephens 		publ_to_item(item, publ);
203*e11aa059SAllan Stephens 		item++;
204*e11aa059SAllan Stephens 		left -= ITEM_SIZE;
205*e11aa059SAllan Stephens 		if (!left) {
206*e11aa059SAllan Stephens 			list_add_tail((struct list_head *)buf, message_list);
207*e11aa059SAllan Stephens 			buf = NULL;
208*e11aa059SAllan Stephens 		}
209*e11aa059SAllan Stephens 	}
210*e11aa059SAllan Stephens }
211*e11aa059SAllan Stephens 
212b97bf3fdSPer Liden /**
2134323add6SPer Liden  * tipc_named_node_up - tell specified node about all publications by this node
214b97bf3fdSPer Liden  */
215b97bf3fdSPer Liden 
2161c553bb5SPaul Gortmaker void tipc_named_node_up(unsigned long nodearg)
217b97bf3fdSPer Liden {
218149ce37cSAllan Stephens 	struct tipc_node *n_ptr;
219a18c4bc3SPaul Gortmaker 	struct tipc_link *l_ptr;
2209aa88c2aSAllan Stephens 	struct list_head message_list;
2211c553bb5SPaul Gortmaker 	u32 node = (u32)nodearg;
222149ce37cSAllan Stephens 	u32 max_item_buf = 0;
223149ce37cSAllan Stephens 
224149ce37cSAllan Stephens 	/* compute maximum amount of publication data to send per message */
225149ce37cSAllan Stephens 
226149ce37cSAllan Stephens 	read_lock_bh(&tipc_net_lock);
2271c553bb5SPaul Gortmaker 	n_ptr = tipc_node_find(node);
228149ce37cSAllan Stephens 	if (n_ptr) {
229149ce37cSAllan Stephens 		tipc_node_lock(n_ptr);
230149ce37cSAllan Stephens 		l_ptr = n_ptr->active_links[0];
231149ce37cSAllan Stephens 		if (l_ptr)
232149ce37cSAllan Stephens 			max_item_buf = ((l_ptr->max_pkt - INT_H_SIZE) /
233149ce37cSAllan Stephens 				ITEM_SIZE) * ITEM_SIZE;
234149ce37cSAllan Stephens 		tipc_node_unlock(n_ptr);
235149ce37cSAllan Stephens 	}
236149ce37cSAllan Stephens 	read_unlock_bh(&tipc_net_lock);
237149ce37cSAllan Stephens 	if (!max_item_buf)
238149ce37cSAllan Stephens 		return;
239b97bf3fdSPer Liden 
2409aa88c2aSAllan Stephens 	/* create list of publication messages, then send them as a unit */
2419aa88c2aSAllan Stephens 
2429aa88c2aSAllan Stephens 	INIT_LIST_HEAD(&message_list);
2439aa88c2aSAllan Stephens 
2444323add6SPer Liden 	read_lock_bh(&tipc_nametbl_lock);
245*e11aa059SAllan Stephens 	named_distribute(&message_list, node, &publ_cluster, max_item_buf);
2464323add6SPer Liden 	read_unlock_bh(&tipc_nametbl_lock);
2479aa88c2aSAllan Stephens 
2489aa88c2aSAllan Stephens 	tipc_link_send_names(&message_list, (u32)node);
249b97bf3fdSPer Liden }
250b97bf3fdSPer Liden 
251b97bf3fdSPer Liden /**
252f1379173SAllan Stephens  * named_purge_publ - remove publication associated with a failed node
253b97bf3fdSPer Liden  *
254b97bf3fdSPer Liden  * Invoked for each publication issued by a newly failed node.
255b97bf3fdSPer Liden  * Removes publication structure from name table & deletes it.
256b97bf3fdSPer Liden  */
257b97bf3fdSPer Liden 
258f1379173SAllan Stephens static void named_purge_publ(struct publication *publ)
259b97bf3fdSPer Liden {
260b97bf3fdSPer Liden 	struct publication *p;
261f131072cSAllan Stephens 
2624323add6SPer Liden 	write_lock_bh(&tipc_nametbl_lock);
2634323add6SPer Liden 	p = tipc_nametbl_remove_publ(publ->type, publ->lower,
264b97bf3fdSPer Liden 				     publ->node, publ->ref, publ->key);
265431697ebSAllan Stephens 	if (p)
266431697ebSAllan Stephens 		tipc_nodesub_unsubscribe(&p->subscr);
2674323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
268f131072cSAllan Stephens 
269f131072cSAllan Stephens 	if (p != publ) {
270f131072cSAllan Stephens 		err("Unable to remove publication from failed node\n"
271f131072cSAllan Stephens 		    "(type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
272f131072cSAllan Stephens 		    publ->type, publ->lower, publ->node, publ->ref, publ->key);
273f131072cSAllan Stephens 	}
274f131072cSAllan Stephens 
275f131072cSAllan Stephens 	kfree(p);
276f131072cSAllan Stephens }
277b97bf3fdSPer Liden 
278b97bf3fdSPer Liden /**
2794323add6SPer Liden  * tipc_named_recv - process name table update message sent by another node
280b97bf3fdSPer Liden  */
281b97bf3fdSPer Liden 
2824323add6SPer Liden void tipc_named_recv(struct sk_buff *buf)
283b97bf3fdSPer Liden {
284b97bf3fdSPer Liden 	struct publication *publ;
285b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
286b97bf3fdSPer Liden 	struct distr_item *item = (struct distr_item *)msg_data(msg);
287b97bf3fdSPer Liden 	u32 count = msg_data_sz(msg) / ITEM_SIZE;
288b97bf3fdSPer Liden 
2894323add6SPer Liden 	write_lock_bh(&tipc_nametbl_lock);
290b97bf3fdSPer Liden 	while (count--) {
291b97bf3fdSPer Liden 		if (msg_type(msg) == PUBLICATION) {
2924323add6SPer Liden 			publ = tipc_nametbl_insert_publ(ntohl(item->type),
293b97bf3fdSPer Liden 							ntohl(item->lower),
294b97bf3fdSPer Liden 							ntohl(item->upper),
295b97bf3fdSPer Liden 							TIPC_CLUSTER_SCOPE,
296b97bf3fdSPer Liden 							msg_orignode(msg),
297b97bf3fdSPer Liden 							ntohl(item->ref),
298b97bf3fdSPer Liden 							ntohl(item->key));
299b97bf3fdSPer Liden 			if (publ) {
3004323add6SPer Liden 				tipc_nodesub_subscribe(&publ->subscr,
301b97bf3fdSPer Liden 						       msg_orignode(msg),
302b97bf3fdSPer Liden 						       publ,
303f1379173SAllan Stephens 						       (net_ev_handler)
304f1379173SAllan Stephens 						       named_purge_publ);
305b97bf3fdSPer Liden 			}
306b97bf3fdSPer Liden 		} else if (msg_type(msg) == WITHDRAWAL) {
3074323add6SPer Liden 			publ = tipc_nametbl_remove_publ(ntohl(item->type),
308b97bf3fdSPer Liden 							ntohl(item->lower),
309b97bf3fdSPer Liden 							msg_orignode(msg),
310b97bf3fdSPer Liden 							ntohl(item->ref),
311b97bf3fdSPer Liden 							ntohl(item->key));
312b97bf3fdSPer Liden 
313b97bf3fdSPer Liden 			if (publ) {
3144323add6SPer Liden 				tipc_nodesub_unsubscribe(&publ->subscr);
315b97bf3fdSPer Liden 				kfree(publ);
316f131072cSAllan Stephens 			} else {
317f131072cSAllan Stephens 				err("Unable to remove publication by node 0x%x\n"
318f131072cSAllan Stephens 				    "(type=%u, lower=%u, ref=%u, key=%u)\n",
319f131072cSAllan Stephens 				    msg_orignode(msg),
320f131072cSAllan Stephens 				    ntohl(item->type), ntohl(item->lower),
321f131072cSAllan Stephens 				    ntohl(item->ref), ntohl(item->key));
322b97bf3fdSPer Liden 			}
323b97bf3fdSPer Liden 		} else {
324a10bd924SAllan Stephens 			warn("Unrecognized name table message received\n");
325b97bf3fdSPer Liden 		}
326b97bf3fdSPer Liden 		item++;
327b97bf3fdSPer Liden 	}
3284323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
3295f6d9123SAllan Stephens 	kfree_skb(buf);
330b97bf3fdSPer Liden }
331b97bf3fdSPer Liden 
332b97bf3fdSPer Liden /**
3334323add6SPer Liden  * tipc_named_reinit - re-initialize local publication list
334b97bf3fdSPer Liden  *
335945af1c3SAllan Stephens  * This routine is called whenever TIPC networking is enabled.
336b97bf3fdSPer Liden  * All existing publications by this node that have "cluster" or "zone" scope
337945af1c3SAllan Stephens  * are updated to reflect the node's new network address.
338b97bf3fdSPer Liden  */
339b97bf3fdSPer Liden 
3404323add6SPer Liden void tipc_named_reinit(void)
341b97bf3fdSPer Liden {
342b97bf3fdSPer Liden 	struct publication *publ;
343b97bf3fdSPer Liden 
3444323add6SPer Liden 	write_lock_bh(&tipc_nametbl_lock);
345945af1c3SAllan Stephens 
3463f8375feSAllan Stephens 	list_for_each_entry(publ, &publ_cluster.list, local_list)
347b97bf3fdSPer Liden 		publ->node = tipc_own_addr;
348945af1c3SAllan Stephens 
3494323add6SPer Liden 	write_unlock_bh(&tipc_nametbl_lock);
350b97bf3fdSPer Liden }
351