blob: 10ff48be3c01e6746d70ccbcb25a29d7ad22b7db [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
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2005, 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"
38#include "cluster.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_distr.h"
41
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#define ITEM_SIZE sizeof(struct distr_item)
43
44/**
45 * struct distr_item - publication info distributed to other nodes
46 * @type: name sequence type
47 * @lower: name sequence lower bound
48 * @upper: name sequence upper bound
49 * @ref: publishing port reference
50 * @key: publication key
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090051 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010052 * ===> All fields are stored in network byte order. <===
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090053 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010054 * First 3 fields identify (name or) name sequence being published.
55 * Reference field uniquely identifies port that published name sequence.
56 * Key field uniquely identifies publication, in the event a port has
57 * multiple publications of the same name sequence.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090058 *
59 * Note: There is no field that identifies the publishing node because it is
Per Lidenb97bf3f2006-01-02 19:04:38 +010060 * the same for all items contained within a publication message.
61 */
62
63struct distr_item {
Al Viro3e6c8cd2006-11-08 00:19:09 -080064 __be32 type;
65 __be32 lower;
66 __be32 upper;
67 __be32 ref;
68 __be32 key;
Per Lidenb97bf3f2006-01-02 19:04:38 +010069};
70
71/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090072 * List of externally visible publications by this node --
Per Lidenb97bf3f2006-01-02 19:04:38 +010073 * that is, all publications having scope > TIPC_NODE_SCOPE.
74 */
75
76static LIST_HEAD(publ_root);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090077static u32 publ_cnt = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +010078
79/**
80 * publ_to_item - add publication info to a publication message
81 */
82
83static void publ_to_item(struct distr_item *i, struct publication *p)
84{
85 i->type = htonl(p->type);
86 i->lower = htonl(p->lower);
87 i->upper = htonl(p->upper);
88 i->ref = htonl(p->ref);
89 i->key = htonl(p->key);
90 dbg("publ_to_item: %u, %u, %u\n", p->type, p->lower, p->upper);
91}
92
93/**
94 * named_prepare_buf - allocate & initialize a publication message
95 */
96
97static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest)
98{
stephen hemminger31e3c3f2010-10-13 13:20:35 +000099 struct sk_buff *buf = tipc_buf_acquire(LONG_H_SIZE + size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100 struct tipc_msg *msg;
101
102 if (buf != NULL) {
103 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000104 tipc_msg_init(msg, NAME_DISTRIBUTOR, type, LONG_H_SIZE, dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 msg_set_size(msg, LONG_H_SIZE + size);
106 }
107 return buf;
108}
109
110/**
Per Liden4323add2006-01-18 00:38:21 +0100111 * tipc_named_publish - tell other nodes about a new publication by this node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 */
113
Per Liden4323add2006-01-18 00:38:21 +0100114void tipc_named_publish(struct publication *publ)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115{
116 struct sk_buff *buf;
117 struct distr_item *item;
118
Allan Stephens08c31f72006-10-16 21:56:04 -0700119 list_add_tail(&publ->local_list, &publ_root);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 publ_cnt++;
121
122 buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
123 if (!buf) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700124 warn("Publication distribution failure\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 return;
126 }
127
128 item = (struct distr_item *)msg_data(buf_msg(buf));
129 publ_to_item(item, publ);
Per Liden4323add2006-01-18 00:38:21 +0100130 dbg("tipc_named_withdraw: broadcasting publish msg\n");
131 tipc_cltr_broadcast(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132}
133
134/**
Per Liden4323add2006-01-18 00:38:21 +0100135 * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 */
137
Per Liden4323add2006-01-18 00:38:21 +0100138void tipc_named_withdraw(struct publication *publ)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139{
140 struct sk_buff *buf;
141 struct distr_item *item;
142
143 list_del(&publ->local_list);
144 publ_cnt--;
145
146 buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
147 if (!buf) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700148 warn("Withdrawl distribution failure\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 return;
150 }
151
152 item = (struct distr_item *)msg_data(buf_msg(buf));
153 publ_to_item(item, publ);
Per Liden4323add2006-01-18 00:38:21 +0100154 dbg("tipc_named_withdraw: broadcasting withdraw msg\n");
155 tipc_cltr_broadcast(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156}
157
158/**
Per Liden4323add2006-01-18 00:38:21 +0100159 * tipc_named_node_up - tell specified node about all publications by this node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 */
161
Per Liden4323add2006-01-18 00:38:21 +0100162void tipc_named_node_up(unsigned long node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163{
164 struct publication *publ;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800165 struct distr_item *item = NULL;
166 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 u32 left = 0;
168 u32 rest;
169 u32 max_item_buf;
170
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900171 read_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 max_item_buf = TIPC_MAX_USER_MSG_SIZE / ITEM_SIZE;
173 max_item_buf *= ITEM_SIZE;
174 rest = publ_cnt * ITEM_SIZE;
175
176 list_for_each_entry(publ, &publ_root, local_list) {
177 if (!buf) {
178 left = (rest <= max_item_buf) ? rest : max_item_buf;
179 rest -= left;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900180 buf = named_prepare_buf(PUBLICATION, left, node);
Allan Stephensa10bd922006-06-25 23:52:17 -0700181 if (!buf) {
182 warn("Bulk publication distribution failure\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 goto exit;
184 }
185 item = (struct distr_item *)msg_data(buf_msg(buf));
186 }
187 publ_to_item(item, publ);
188 item++;
189 left -= ITEM_SIZE;
190 if (!left) {
191 msg_set_link_selector(buf_msg(buf), node);
Per Liden4323add2006-01-18 00:38:21 +0100192 dbg("tipc_named_node_up: sending publish msg to "
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900193 "<%u.%u.%u>\n", tipc_zone(node),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 tipc_cluster(node), tipc_node(node));
Per Liden4323add2006-01-18 00:38:21 +0100195 tipc_link_send(buf, node, node);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800196 buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 }
198 }
199exit:
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900200 read_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201}
202
203/**
204 * node_is_down - remove publication associated with a failed node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900205 *
206 * Invoked for each publication issued by a newly failed node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 * Removes publication structure from name table & deletes it.
208 * In rare cases the link may have come back up again when this
209 * function is called, and we have two items representing the same
210 * publication. Nudge this item's key to distinguish it from the other.
211 * (Note: Publication's node subscription is already unsubscribed.)
212 */
213
214static void node_is_down(struct publication *publ)
215{
216 struct publication *p;
Allan Stephensf1310722006-06-25 23:51:37 -0700217
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900218 write_lock_bh(&tipc_nametbl_lock);
219 dbg("node_is_down: withdrawing %u, %u, %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 publ->type, publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900221 publ->key += 1222345;
222 p = tipc_nametbl_remove_publ(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100223 publ->node, publ->ref, publ->key);
Per Liden4323add2006-01-18 00:38:21 +0100224 write_unlock_bh(&tipc_nametbl_lock);
Allan Stephensf1310722006-06-25 23:51:37 -0700225
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900226 if (p != publ) {
Allan Stephensf1310722006-06-25 23:51:37 -0700227 err("Unable to remove publication from failed node\n"
228 "(type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
229 publ->type, publ->lower, publ->node, publ->ref, publ->key);
230 }
231
232 if (p) {
233 kfree(p);
234 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235}
236
237/**
Per Liden4323add2006-01-18 00:38:21 +0100238 * tipc_named_recv - process name table update message sent by another node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 */
240
Per Liden4323add2006-01-18 00:38:21 +0100241void tipc_named_recv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242{
243 struct publication *publ;
244 struct tipc_msg *msg = buf_msg(buf);
245 struct distr_item *item = (struct distr_item *)msg_data(msg);
246 u32 count = msg_data_sz(msg) / ITEM_SIZE;
247
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900248 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 while (count--) {
250 if (msg_type(msg) == PUBLICATION) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900251 dbg("tipc_named_recv: got publication for %u, %u, %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 ntohl(item->type), ntohl(item->lower),
253 ntohl(item->upper));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900254 publ = tipc_nametbl_insert_publ(ntohl(item->type),
Per Liden4323add2006-01-18 00:38:21 +0100255 ntohl(item->lower),
256 ntohl(item->upper),
257 TIPC_CLUSTER_SCOPE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900258 msg_orignode(msg),
Per Liden4323add2006-01-18 00:38:21 +0100259 ntohl(item->ref),
260 ntohl(item->key));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 if (publ) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900262 tipc_nodesub_subscribe(&publ->subscr,
263 msg_orignode(msg),
Per Liden4323add2006-01-18 00:38:21 +0100264 publ,
265 (net_ev_handler)node_is_down);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 }
267 } else if (msg_type(msg) == WITHDRAWAL) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900268 dbg("tipc_named_recv: got withdrawl for %u, %u, %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269 ntohl(item->type), ntohl(item->lower),
270 ntohl(item->upper));
Per Liden4323add2006-01-18 00:38:21 +0100271 publ = tipc_nametbl_remove_publ(ntohl(item->type),
272 ntohl(item->lower),
273 msg_orignode(msg),
274 ntohl(item->ref),
275 ntohl(item->key));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276
277 if (publ) {
Per Liden4323add2006-01-18 00:38:21 +0100278 tipc_nodesub_unsubscribe(&publ->subscr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900279 kfree(publ);
Allan Stephensf1310722006-06-25 23:51:37 -0700280 } else {
281 err("Unable to remove publication by node 0x%x\n"
282 "(type=%u, lower=%u, ref=%u, key=%u)\n",
283 msg_orignode(msg),
284 ntohl(item->type), ntohl(item->lower),
285 ntohl(item->ref), ntohl(item->key));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 }
287 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -0700288 warn("Unrecognized name table message received\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 }
290 item++;
291 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900292 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 buf_discard(buf);
294}
295
296/**
Per Liden4323add2006-01-18 00:38:21 +0100297 * tipc_named_reinit - re-initialize local publication list
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 * This routine is called whenever TIPC networking is (re)enabled.
300 * All existing publications by this node that have "cluster" or "zone" scope
301 * are updated to reflect the node's current network address.
302 * (If the node's address is unchanged, the update loop terminates immediately.)
303 */
304
Per Liden4323add2006-01-18 00:38:21 +0100305void tipc_named_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306{
307 struct publication *publ;
308
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900309 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 list_for_each_entry(publ, &publ_root, local_list) {
311 if (publ->node == tipc_own_addr)
312 break;
313 publ->node = tipc_own_addr;
314 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900315 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316}