blob: dcc15bcd569279a96b74cc052ee140e6f8e1bf3b [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/name_distr.c: TIPC name distribution code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Allan Stephens431697e2011-02-23 13:51:15 -05005 * Copyright (c) 2005, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "name_distr.h"
40
Per Lidenb97bf3f2006-01-02 19:04:38 +010041/**
Allan Stephens3f8375f2012-04-17 17:57:52 -040042 * struct publ_list - list of publications made by this node
43 * @list: circular list of publications
44 * @list_size: number of entries in list
Per Lidenb97bf3f2006-01-02 19:04:38 +010045 */
Allan Stephens3f8375f2012-04-17 17:57:52 -040046struct publ_list {
47 struct list_head list;
48 u32 size;
49};
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
Allan Stephensa9098042012-04-17 17:57:52 -040051static struct publ_list publ_zone = {
52 .list = LIST_HEAD_INIT(publ_zone.list),
53 .size = 0,
54};
55
Allan Stephens3f8375f2012-04-17 17:57:52 -040056static struct publ_list publ_cluster = {
57 .list = LIST_HEAD_INIT(publ_cluster.list),
58 .size = 0,
59};
Per Lidenb97bf3f2006-01-02 19:04:38 +010060
Allan Stephensa9098042012-04-17 17:57:52 -040061static struct publ_list publ_node = {
62 .list = LIST_HEAD_INIT(publ_node.list),
63 .size = 0,
64};
65
66static struct publ_list *publ_lists[] = {
67 NULL,
68 &publ_zone, /* publ_lists[TIPC_ZONE_SCOPE] */
69 &publ_cluster, /* publ_lists[TIPC_CLUSTER_SCOPE] */
70 &publ_node /* publ_lists[TIPC_NODE_SCOPE] */
71};
72
73
Per Lidenb97bf3f2006-01-02 19:04:38 +010074/**
75 * publ_to_item - add publication info to a publication message
76 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010077static void publ_to_item(struct distr_item *i, struct publication *p)
78{
79 i->type = htonl(p->type);
80 i->lower = htonl(p->lower);
81 i->upper = htonl(p->upper);
82 i->ref = htonl(p->ref);
83 i->key = htonl(p->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +010084}
85
86/**
87 * named_prepare_buf - allocate & initialize a publication message
88 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010089static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest)
90{
Allan Stephens741d9eb2011-05-31 15:03:18 -040091 struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size);
Per Lidenb97bf3f2006-01-02 19:04:38 +010092 struct tipc_msg *msg;
93
94 if (buf != NULL) {
95 msg = buf_msg(buf);
Allan Stephens741d9eb2011-05-31 15:03:18 -040096 tipc_msg_init(msg, NAME_DISTRIBUTOR, type, INT_H_SIZE, dest);
97 msg_set_size(msg, INT_H_SIZE + size);
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 }
99 return buf;
100}
101
Ying Xueeab8c0452014-04-28 18:00:10 +0800102void named_cluster_distribute(struct sk_buff *buf)
Allan Stephens8f92df62010-12-31 18:59:19 +0000103{
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400104 struct sk_buff *obuf;
105 struct tipc_node *node;
106 u32 dnode;
Allan Stephens8f92df62010-12-31 18:59:19 +0000107
Ying Xue6c7a7622014-03-27 12:54:37 +0800108 rcu_read_lock();
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400109 list_for_each_entry_rcu(node, &tipc_node_list, list) {
110 dnode = node->addr;
111 if (in_own_node(dnode))
112 continue;
113 if (!tipc_node_active_links(node))
114 continue;
115 obuf = skb_copy(buf, GFP_ATOMIC);
116 if (!obuf)
117 break;
118 msg_set_destnode(buf_msg(obuf), dnode);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400119 tipc_link_xmit(obuf, dnode, dnode);
Allan Stephens8f92df62010-12-31 18:59:19 +0000120 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800121 rcu_read_unlock();
Allan Stephens8f92df62010-12-31 18:59:19 +0000122
Allan Stephens5f6d9122011-11-04 13:24:29 -0400123 kfree_skb(buf);
Allan Stephens8f92df62010-12-31 18:59:19 +0000124}
125
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126/**
Per Liden4323add2006-01-18 00:38:21 +0100127 * tipc_named_publish - tell other nodes about a new publication by this node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 */
Ying Xueeab8c0452014-04-28 18:00:10 +0800129struct sk_buff *tipc_named_publish(struct publication *publ)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130{
131 struct sk_buff *buf;
132 struct distr_item *item;
133
Allan Stephensa9098042012-04-17 17:57:52 -0400134 list_add_tail(&publ->local_list, &publ_lists[publ->scope]->list);
135 publ_lists[publ->scope]->size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136
Allan Stephens1110b8d2012-04-17 17:57:52 -0400137 if (publ->scope == TIPC_NODE_SCOPE)
Ying Xueeab8c0452014-04-28 18:00:10 +0800138 return NULL;
Allan Stephens1110b8d2012-04-17 17:57:52 -0400139
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
141 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400142 pr_warn("Publication distribution failure\n");
Ying Xueeab8c0452014-04-28 18:00:10 +0800143 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 }
145
146 item = (struct distr_item *)msg_data(buf_msg(buf));
147 publ_to_item(item, publ);
Ying Xueeab8c0452014-04-28 18:00:10 +0800148 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149}
150
151/**
Per Liden4323add2006-01-18 00:38:21 +0100152 * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 */
Ying Xueeab8c0452014-04-28 18:00:10 +0800154struct sk_buff *tipc_named_withdraw(struct publication *publ)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155{
156 struct sk_buff *buf;
157 struct distr_item *item;
158
159 list_del(&publ->local_list);
Allan Stephensa9098042012-04-17 17:57:52 -0400160 publ_lists[publ->scope]->size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161
Allan Stephens1110b8d2012-04-17 17:57:52 -0400162 if (publ->scope == TIPC_NODE_SCOPE)
Ying Xueeab8c0452014-04-28 18:00:10 +0800163 return NULL;
Allan Stephens1110b8d2012-04-17 17:57:52 -0400164
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
166 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400167 pr_warn("Withdrawal distribution failure\n");
Ying Xueeab8c0452014-04-28 18:00:10 +0800168 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 }
170
171 item = (struct distr_item *)msg_data(buf_msg(buf));
172 publ_to_item(item, publ);
Ying Xueeab8c0452014-04-28 18:00:10 +0800173 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174}
175
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400176/**
Allan Stephense11aa052012-04-17 17:57:52 -0400177 * named_distribute - prepare name info for bulk distribution to another node
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400178 * @msg_list: list of messages (buffers) to be returned from this function
179 * @dnode: node to be updated
180 * @pls: linked list of publication items to be packed into buffer chain
Allan Stephense11aa052012-04-17 17:57:52 -0400181 */
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400182static void named_distribute(struct list_head *msg_list, u32 dnode,
183 struct publ_list *pls)
Allan Stephense11aa052012-04-17 17:57:52 -0400184{
185 struct publication *publ;
186 struct sk_buff *buf = NULL;
187 struct distr_item *item = NULL;
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400188 uint dsz = pls->size * ITEM_SIZE;
189 uint msg_dsz = (tipc_node_get_mtu(dnode, 0) / ITEM_SIZE) * ITEM_SIZE;
190 uint rem = dsz;
191 uint msg_rem = 0;
Allan Stephense11aa052012-04-17 17:57:52 -0400192
193 list_for_each_entry(publ, &pls->list, local_list) {
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400194 /* Prepare next buffer: */
Allan Stephense11aa052012-04-17 17:57:52 -0400195 if (!buf) {
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400196 msg_rem = min_t(uint, rem, msg_dsz);
197 rem -= msg_rem;
198 buf = named_prepare_buf(PUBLICATION, msg_rem, dnode);
Allan Stephense11aa052012-04-17 17:57:52 -0400199 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400200 pr_warn("Bulk publication failure\n");
Allan Stephense11aa052012-04-17 17:57:52 -0400201 return;
202 }
203 item = (struct distr_item *)msg_data(buf_msg(buf));
204 }
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400205
206 /* Pack publication into message: */
Allan Stephense11aa052012-04-17 17:57:52 -0400207 publ_to_item(item, publ);
208 item++;
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400209 msg_rem -= ITEM_SIZE;
210
211 /* Append full buffer to list: */
212 if (!msg_rem) {
213 list_add_tail((struct list_head *)buf, msg_list);
Allan Stephense11aa052012-04-17 17:57:52 -0400214 buf = NULL;
215 }
216 }
217}
218
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219/**
Per Liden4323add2006-01-18 00:38:21 +0100220 * tipc_named_node_up - tell specified node about all publications by this node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 */
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400222void tipc_named_node_up(u32 dnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223{
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400224 LIST_HEAD(msg_list);
225 struct sk_buff *buf_chain;
Allan Stephens9aa88c22011-05-31 13:38:02 -0400226
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900227 read_lock_bh(&tipc_nametbl_lock);
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400228 named_distribute(&msg_list, dnode, &publ_cluster);
229 named_distribute(&msg_list, dnode, &publ_zone);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900230 read_unlock_bh(&tipc_nametbl_lock);
Allan Stephens9aa88c22011-05-31 13:38:02 -0400231
Jon Paul Maloydbdf6d22014-07-16 20:40:58 -0400232 /* Convert circular list to linear list and send: */
233 buf_chain = (struct sk_buff *)msg_list.next;
234 ((struct sk_buff *)msg_list.prev)->next = NULL;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400235 tipc_link_xmit(buf_chain, dnode, dnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236}
237
238/**
Allan Stephensf1379172011-02-23 14:13:41 -0500239 * named_purge_publ - remove publication associated with a failed node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900240 *
241 * Invoked for each publication issued by a newly failed node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 * Removes publication structure from name table & deletes it.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 */
Allan Stephensf1379172011-02-23 14:13:41 -0500244static void named_purge_publ(struct publication *publ)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245{
246 struct publication *p;
Allan Stephensf1310722006-06-25 23:51:37 -0700247
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900248 write_lock_bh(&tipc_nametbl_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900249 p = tipc_nametbl_remove_publ(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100250 publ->node, publ->ref, publ->key);
Allan Stephens431697e2011-02-23 13:51:15 -0500251 if (p)
252 tipc_nodesub_unsubscribe(&p->subscr);
Per Liden4323add2006-01-18 00:38:21 +0100253 write_unlock_bh(&tipc_nametbl_lock);
Allan Stephensf1310722006-06-25 23:51:37 -0700254
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900255 if (p != publ) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400256 pr_err("Unable to remove publication from failed node\n"
257 " (type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
258 publ->type, publ->lower, publ->node, publ->ref,
259 publ->key);
Allan Stephensf1310722006-06-25 23:51:37 -0700260 }
261
Allan Stephense83504f2010-12-31 18:59:30 +0000262 kfree(p);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263}
264
265/**
Ying Xue247f0f32014-02-18 16:06:46 +0800266 * tipc_named_rcv - process name table update message sent by another node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 */
Ying Xue247f0f32014-02-18 16:06:46 +0800268void tipc_named_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269{
270 struct publication *publ;
271 struct tipc_msg *msg = buf_msg(buf);
272 struct distr_item *item = (struct distr_item *)msg_data(msg);
273 u32 count = msg_data_sz(msg) / ITEM_SIZE;
274
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900275 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 while (count--) {
277 if (msg_type(msg) == PUBLICATION) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900278 publ = tipc_nametbl_insert_publ(ntohl(item->type),
Per Liden4323add2006-01-18 00:38:21 +0100279 ntohl(item->lower),
280 ntohl(item->upper),
281 TIPC_CLUSTER_SCOPE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900282 msg_orignode(msg),
Per Liden4323add2006-01-18 00:38:21 +0100283 ntohl(item->ref),
284 ntohl(item->key));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 if (publ) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900286 tipc_nodesub_subscribe(&publ->subscr,
287 msg_orignode(msg),
Per Liden4323add2006-01-18 00:38:21 +0100288 publ,
Allan Stephensf1379172011-02-23 14:13:41 -0500289 (net_ev_handler)
290 named_purge_publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 }
292 } else if (msg_type(msg) == WITHDRAWAL) {
Per Liden4323add2006-01-18 00:38:21 +0100293 publ = tipc_nametbl_remove_publ(ntohl(item->type),
294 ntohl(item->lower),
295 msg_orignode(msg),
296 ntohl(item->ref),
297 ntohl(item->key));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298
299 if (publ) {
Per Liden4323add2006-01-18 00:38:21 +0100300 tipc_nodesub_unsubscribe(&publ->subscr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900301 kfree(publ);
Allan Stephensf1310722006-06-25 23:51:37 -0700302 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400303 pr_err("Unable to remove publication by node 0x%x\n"
304 " (type=%u, lower=%u, ref=%u, key=%u)\n",
305 msg_orignode(msg), ntohl(item->type),
306 ntohl(item->lower), ntohl(item->ref),
307 ntohl(item->key));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 }
309 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400310 pr_warn("Unrecognized name table message received\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 }
312 item++;
313 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900314 write_unlock_bh(&tipc_nametbl_lock);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400315 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316}
317
318/**
Allan Stephens1110b8d2012-04-17 17:57:52 -0400319 * tipc_named_reinit - re-initialize local publications
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320 *
Allan Stephens945af1c2011-10-14 14:42:25 -0400321 * This routine is called whenever TIPC networking is enabled.
Allan Stephens1110b8d2012-04-17 17:57:52 -0400322 * All name table entries published by this node are updated to reflect
323 * the node's new network address.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 */
Per Liden4323add2006-01-18 00:38:21 +0100325void tipc_named_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326{
327 struct publication *publ;
Allan Stephensa9098042012-04-17 17:57:52 -0400328 int scope;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900330 write_lock_bh(&tipc_nametbl_lock);
Allan Stephens945af1c2011-10-14 14:42:25 -0400331
Allan Stephens1110b8d2012-04-17 17:57:52 -0400332 for (scope = TIPC_ZONE_SCOPE; scope <= TIPC_NODE_SCOPE; scope++)
Allan Stephensa9098042012-04-17 17:57:52 -0400333 list_for_each_entry(publ, &publ_lists[scope]->list, local_list)
334 publ->node = tipc_own_addr;
Allan Stephens945af1c2011-10-14 14:42:25 -0400335
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900336 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337}