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 80a909804fSAllan Stephens static struct publ_list publ_zone = { 81a909804fSAllan Stephens .list = LIST_HEAD_INIT(publ_zone.list), 82a909804fSAllan Stephens .size = 0, 83a909804fSAllan Stephens }; 84a909804fSAllan Stephens 853f8375feSAllan Stephens static struct publ_list publ_cluster = { 863f8375feSAllan Stephens .list = LIST_HEAD_INIT(publ_cluster.list), 873f8375feSAllan Stephens .size = 0, 883f8375feSAllan Stephens }; 89b97bf3fdSPer Liden 90a909804fSAllan Stephens static struct publ_list publ_node = { 91a909804fSAllan Stephens .list = LIST_HEAD_INIT(publ_node.list), 92a909804fSAllan Stephens .size = 0, 93a909804fSAllan Stephens }; 94a909804fSAllan Stephens 95a909804fSAllan Stephens static struct publ_list *publ_lists[] = { 96a909804fSAllan Stephens NULL, 97a909804fSAllan Stephens &publ_zone, /* publ_lists[TIPC_ZONE_SCOPE] */ 98a909804fSAllan Stephens &publ_cluster, /* publ_lists[TIPC_CLUSTER_SCOPE] */ 99a909804fSAllan Stephens &publ_node /* publ_lists[TIPC_NODE_SCOPE] */ 100a909804fSAllan Stephens }; 101a909804fSAllan Stephens 102a909804fSAllan Stephens 103b97bf3fdSPer Liden /** 104b97bf3fdSPer Liden * publ_to_item - add publication info to a publication message 105b97bf3fdSPer Liden */ 106b97bf3fdSPer Liden 107b97bf3fdSPer Liden static void publ_to_item(struct distr_item *i, struct publication *p) 108b97bf3fdSPer Liden { 109b97bf3fdSPer Liden i->type = htonl(p->type); 110b97bf3fdSPer Liden i->lower = htonl(p->lower); 111b97bf3fdSPer Liden i->upper = htonl(p->upper); 112b97bf3fdSPer Liden i->ref = htonl(p->ref); 113b97bf3fdSPer Liden i->key = htonl(p->key); 114b97bf3fdSPer Liden } 115b97bf3fdSPer Liden 116b97bf3fdSPer Liden /** 117b97bf3fdSPer Liden * named_prepare_buf - allocate & initialize a publication message 118b97bf3fdSPer Liden */ 119b97bf3fdSPer Liden 120b97bf3fdSPer Liden static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest) 121b97bf3fdSPer Liden { 122741d9eb7SAllan Stephens struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size); 123b97bf3fdSPer Liden struct tipc_msg *msg; 124b97bf3fdSPer Liden 125b97bf3fdSPer Liden if (buf != NULL) { 126b97bf3fdSPer Liden msg = buf_msg(buf); 127741d9eb7SAllan Stephens tipc_msg_init(msg, NAME_DISTRIBUTOR, type, INT_H_SIZE, dest); 128741d9eb7SAllan Stephens msg_set_size(msg, INT_H_SIZE + size); 129b97bf3fdSPer Liden } 130b97bf3fdSPer Liden return buf; 131b97bf3fdSPer Liden } 132b97bf3fdSPer Liden 1338f92df6aSAllan Stephens static void named_cluster_distribute(struct sk_buff *buf) 1348f92df6aSAllan Stephens { 1358f92df6aSAllan Stephens struct sk_buff *buf_copy; 1368f92df6aSAllan Stephens struct tipc_node *n_ptr; 1378f92df6aSAllan Stephens 138672d99e1SAllan Stephens list_for_each_entry(n_ptr, &tipc_node_list, list) { 1398f19afb2SPaul Gortmaker if (tipc_node_active_links(n_ptr)) { 1408f92df6aSAllan Stephens buf_copy = skb_copy(buf, GFP_ATOMIC); 1418f92df6aSAllan Stephens if (!buf_copy) 1428f92df6aSAllan Stephens break; 1438f92df6aSAllan Stephens msg_set_destnode(buf_msg(buf_copy), n_ptr->addr); 1448f92df6aSAllan Stephens tipc_link_send(buf_copy, n_ptr->addr, n_ptr->addr); 1458f92df6aSAllan Stephens } 1468f92df6aSAllan Stephens } 1478f92df6aSAllan Stephens 1485f6d9123SAllan Stephens kfree_skb(buf); 1498f92df6aSAllan Stephens } 1508f92df6aSAllan Stephens 151b97bf3fdSPer Liden /** 1524323add6SPer Liden * tipc_named_publish - tell other nodes about a new publication by this node 153b97bf3fdSPer Liden */ 154b97bf3fdSPer Liden 1554323add6SPer Liden void tipc_named_publish(struct publication *publ) 156b97bf3fdSPer Liden { 157b97bf3fdSPer Liden struct sk_buff *buf; 158b97bf3fdSPer Liden struct distr_item *item; 159b97bf3fdSPer Liden 160a909804fSAllan Stephens list_add_tail(&publ->local_list, &publ_lists[publ->scope]->list); 161a909804fSAllan Stephens publ_lists[publ->scope]->size++; 162b97bf3fdSPer Liden 163*1110b8d3SAllan Stephens if (publ->scope == TIPC_NODE_SCOPE) 164*1110b8d3SAllan Stephens return; 165*1110b8d3SAllan Stephens 166b97bf3fdSPer Liden buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0); 167b97bf3fdSPer Liden if (!buf) { 168a10bd924SAllan Stephens warn("Publication distribution failure\n"); 169b97bf3fdSPer Liden return; 170b97bf3fdSPer Liden } 171b97bf3fdSPer Liden 172b97bf3fdSPer Liden item = (struct distr_item *)msg_data(buf_msg(buf)); 173b97bf3fdSPer Liden publ_to_item(item, publ); 1748f92df6aSAllan Stephens named_cluster_distribute(buf); 175b97bf3fdSPer Liden } 176b97bf3fdSPer Liden 177b97bf3fdSPer Liden /** 1784323add6SPer Liden * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node 179b97bf3fdSPer Liden */ 180b97bf3fdSPer Liden 1814323add6SPer Liden void tipc_named_withdraw(struct publication *publ) 182b97bf3fdSPer Liden { 183b97bf3fdSPer Liden struct sk_buff *buf; 184b97bf3fdSPer Liden struct distr_item *item; 185b97bf3fdSPer Liden 186b97bf3fdSPer Liden list_del(&publ->local_list); 187a909804fSAllan Stephens publ_lists[publ->scope]->size--; 188b97bf3fdSPer Liden 189*1110b8d3SAllan Stephens if (publ->scope == TIPC_NODE_SCOPE) 190*1110b8d3SAllan Stephens return; 191*1110b8d3SAllan Stephens 192b97bf3fdSPer Liden buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0); 193b97bf3fdSPer Liden if (!buf) { 19425985edcSLucas De Marchi warn("Withdrawal distribution failure\n"); 195b97bf3fdSPer Liden return; 196b97bf3fdSPer Liden } 197b97bf3fdSPer Liden 198b97bf3fdSPer Liden item = (struct distr_item *)msg_data(buf_msg(buf)); 199b97bf3fdSPer Liden publ_to_item(item, publ); 2008f92df6aSAllan Stephens named_cluster_distribute(buf); 201b97bf3fdSPer Liden } 202b97bf3fdSPer Liden 203e11aa059SAllan Stephens /* 204e11aa059SAllan Stephens * named_distribute - prepare name info for bulk distribution to another node 205e11aa059SAllan Stephens */ 206e11aa059SAllan Stephens static void named_distribute(struct list_head *message_list, u32 node, 207e11aa059SAllan Stephens struct publ_list *pls, u32 max_item_buf) 208e11aa059SAllan Stephens { 209e11aa059SAllan Stephens struct publication *publ; 210e11aa059SAllan Stephens struct sk_buff *buf = NULL; 211e11aa059SAllan Stephens struct distr_item *item = NULL; 212e11aa059SAllan Stephens u32 left = 0; 213e11aa059SAllan Stephens u32 rest = pls->size * ITEM_SIZE; 214e11aa059SAllan Stephens 215e11aa059SAllan Stephens list_for_each_entry(publ, &pls->list, local_list) { 216e11aa059SAllan Stephens if (!buf) { 217e11aa059SAllan Stephens left = (rest <= max_item_buf) ? rest : max_item_buf; 218e11aa059SAllan Stephens rest -= left; 219e11aa059SAllan Stephens buf = named_prepare_buf(PUBLICATION, left, node); 220e11aa059SAllan Stephens if (!buf) { 221e11aa059SAllan Stephens warn("Bulk publication failure\n"); 222e11aa059SAllan Stephens return; 223e11aa059SAllan Stephens } 224e11aa059SAllan Stephens item = (struct distr_item *)msg_data(buf_msg(buf)); 225e11aa059SAllan Stephens } 226e11aa059SAllan Stephens publ_to_item(item, publ); 227e11aa059SAllan Stephens item++; 228e11aa059SAllan Stephens left -= ITEM_SIZE; 229e11aa059SAllan Stephens if (!left) { 230e11aa059SAllan Stephens list_add_tail((struct list_head *)buf, message_list); 231e11aa059SAllan Stephens buf = NULL; 232e11aa059SAllan Stephens } 233e11aa059SAllan Stephens } 234e11aa059SAllan Stephens } 235e11aa059SAllan Stephens 236b97bf3fdSPer Liden /** 2374323add6SPer Liden * tipc_named_node_up - tell specified node about all publications by this node 238b97bf3fdSPer Liden */ 239b97bf3fdSPer Liden 2401c553bb5SPaul Gortmaker void tipc_named_node_up(unsigned long nodearg) 241b97bf3fdSPer Liden { 242149ce37cSAllan Stephens struct tipc_node *n_ptr; 243a18c4bc3SPaul Gortmaker struct tipc_link *l_ptr; 2449aa88c2aSAllan Stephens struct list_head message_list; 2451c553bb5SPaul Gortmaker u32 node = (u32)nodearg; 246149ce37cSAllan Stephens u32 max_item_buf = 0; 247149ce37cSAllan Stephens 248149ce37cSAllan Stephens /* compute maximum amount of publication data to send per message */ 249149ce37cSAllan Stephens 250149ce37cSAllan Stephens read_lock_bh(&tipc_net_lock); 2511c553bb5SPaul Gortmaker n_ptr = tipc_node_find(node); 252149ce37cSAllan Stephens if (n_ptr) { 253149ce37cSAllan Stephens tipc_node_lock(n_ptr); 254149ce37cSAllan Stephens l_ptr = n_ptr->active_links[0]; 255149ce37cSAllan Stephens if (l_ptr) 256149ce37cSAllan Stephens max_item_buf = ((l_ptr->max_pkt - INT_H_SIZE) / 257149ce37cSAllan Stephens ITEM_SIZE) * ITEM_SIZE; 258149ce37cSAllan Stephens tipc_node_unlock(n_ptr); 259149ce37cSAllan Stephens } 260149ce37cSAllan Stephens read_unlock_bh(&tipc_net_lock); 261149ce37cSAllan Stephens if (!max_item_buf) 262149ce37cSAllan Stephens return; 263b97bf3fdSPer Liden 2649aa88c2aSAllan Stephens /* create list of publication messages, then send them as a unit */ 2659aa88c2aSAllan Stephens 2669aa88c2aSAllan Stephens INIT_LIST_HEAD(&message_list); 2679aa88c2aSAllan Stephens 2684323add6SPer Liden read_lock_bh(&tipc_nametbl_lock); 269e11aa059SAllan Stephens named_distribute(&message_list, node, &publ_cluster, max_item_buf); 270a909804fSAllan Stephens named_distribute(&message_list, node, &publ_zone, max_item_buf); 2714323add6SPer Liden read_unlock_bh(&tipc_nametbl_lock); 2729aa88c2aSAllan Stephens 2739aa88c2aSAllan Stephens tipc_link_send_names(&message_list, (u32)node); 274b97bf3fdSPer Liden } 275b97bf3fdSPer Liden 276b97bf3fdSPer Liden /** 277f1379173SAllan Stephens * named_purge_publ - remove publication associated with a failed node 278b97bf3fdSPer Liden * 279b97bf3fdSPer Liden * Invoked for each publication issued by a newly failed node. 280b97bf3fdSPer Liden * Removes publication structure from name table & deletes it. 281b97bf3fdSPer Liden */ 282b97bf3fdSPer Liden 283f1379173SAllan Stephens static void named_purge_publ(struct publication *publ) 284b97bf3fdSPer Liden { 285b97bf3fdSPer Liden struct publication *p; 286f131072cSAllan Stephens 2874323add6SPer Liden write_lock_bh(&tipc_nametbl_lock); 2884323add6SPer Liden p = tipc_nametbl_remove_publ(publ->type, publ->lower, 289b97bf3fdSPer Liden publ->node, publ->ref, publ->key); 290431697ebSAllan Stephens if (p) 291431697ebSAllan Stephens tipc_nodesub_unsubscribe(&p->subscr); 2924323add6SPer Liden write_unlock_bh(&tipc_nametbl_lock); 293f131072cSAllan Stephens 294f131072cSAllan Stephens if (p != publ) { 295f131072cSAllan Stephens err("Unable to remove publication from failed node\n" 296f131072cSAllan Stephens "(type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n", 297f131072cSAllan Stephens publ->type, publ->lower, publ->node, publ->ref, publ->key); 298f131072cSAllan Stephens } 299f131072cSAllan Stephens 300f131072cSAllan Stephens kfree(p); 301f131072cSAllan Stephens } 302b97bf3fdSPer Liden 303b97bf3fdSPer Liden /** 3044323add6SPer Liden * tipc_named_recv - process name table update message sent by another node 305b97bf3fdSPer Liden */ 306b97bf3fdSPer Liden 3074323add6SPer Liden void tipc_named_recv(struct sk_buff *buf) 308b97bf3fdSPer Liden { 309b97bf3fdSPer Liden struct publication *publ; 310b97bf3fdSPer Liden struct tipc_msg *msg = buf_msg(buf); 311b97bf3fdSPer Liden struct distr_item *item = (struct distr_item *)msg_data(msg); 312b97bf3fdSPer Liden u32 count = msg_data_sz(msg) / ITEM_SIZE; 313b97bf3fdSPer Liden 3144323add6SPer Liden write_lock_bh(&tipc_nametbl_lock); 315b97bf3fdSPer Liden while (count--) { 316b97bf3fdSPer Liden if (msg_type(msg) == PUBLICATION) { 3174323add6SPer Liden publ = tipc_nametbl_insert_publ(ntohl(item->type), 318b97bf3fdSPer Liden ntohl(item->lower), 319b97bf3fdSPer Liden ntohl(item->upper), 320b97bf3fdSPer Liden TIPC_CLUSTER_SCOPE, 321b97bf3fdSPer Liden msg_orignode(msg), 322b97bf3fdSPer Liden ntohl(item->ref), 323b97bf3fdSPer Liden ntohl(item->key)); 324b97bf3fdSPer Liden if (publ) { 3254323add6SPer Liden tipc_nodesub_subscribe(&publ->subscr, 326b97bf3fdSPer Liden msg_orignode(msg), 327b97bf3fdSPer Liden publ, 328f1379173SAllan Stephens (net_ev_handler) 329f1379173SAllan Stephens named_purge_publ); 330b97bf3fdSPer Liden } 331b97bf3fdSPer Liden } else if (msg_type(msg) == WITHDRAWAL) { 3324323add6SPer Liden publ = tipc_nametbl_remove_publ(ntohl(item->type), 333b97bf3fdSPer Liden ntohl(item->lower), 334b97bf3fdSPer Liden msg_orignode(msg), 335b97bf3fdSPer Liden ntohl(item->ref), 336b97bf3fdSPer Liden ntohl(item->key)); 337b97bf3fdSPer Liden 338b97bf3fdSPer Liden if (publ) { 3394323add6SPer Liden tipc_nodesub_unsubscribe(&publ->subscr); 340b97bf3fdSPer Liden kfree(publ); 341f131072cSAllan Stephens } else { 342f131072cSAllan Stephens err("Unable to remove publication by node 0x%x\n" 343f131072cSAllan Stephens "(type=%u, lower=%u, ref=%u, key=%u)\n", 344f131072cSAllan Stephens msg_orignode(msg), 345f131072cSAllan Stephens ntohl(item->type), ntohl(item->lower), 346f131072cSAllan Stephens ntohl(item->ref), ntohl(item->key)); 347b97bf3fdSPer Liden } 348b97bf3fdSPer Liden } else { 349a10bd924SAllan Stephens warn("Unrecognized name table message received\n"); 350b97bf3fdSPer Liden } 351b97bf3fdSPer Liden item++; 352b97bf3fdSPer Liden } 3534323add6SPer Liden write_unlock_bh(&tipc_nametbl_lock); 3545f6d9123SAllan Stephens kfree_skb(buf); 355b97bf3fdSPer Liden } 356b97bf3fdSPer Liden 357b97bf3fdSPer Liden /** 358*1110b8d3SAllan Stephens * tipc_named_reinit - re-initialize local publications 359b97bf3fdSPer Liden * 360945af1c3SAllan Stephens * This routine is called whenever TIPC networking is enabled. 361*1110b8d3SAllan Stephens * All name table entries published by this node are updated to reflect 362*1110b8d3SAllan Stephens * the node's new network address. 363b97bf3fdSPer Liden */ 364b97bf3fdSPer Liden 3654323add6SPer Liden void tipc_named_reinit(void) 366b97bf3fdSPer Liden { 367b97bf3fdSPer Liden struct publication *publ; 368a909804fSAllan Stephens int scope; 369b97bf3fdSPer Liden 3704323add6SPer Liden write_lock_bh(&tipc_nametbl_lock); 371945af1c3SAllan Stephens 372*1110b8d3SAllan Stephens for (scope = TIPC_ZONE_SCOPE; scope <= TIPC_NODE_SCOPE; scope++) 373a909804fSAllan Stephens list_for_each_entry(publ, &publ_lists[scope]->list, local_list) 374b97bf3fdSPer Liden publ->node = tipc_own_addr; 375945af1c3SAllan Stephens 3764323add6SPer Liden write_unlock_bh(&tipc_nametbl_lock); 377b97bf3fdSPer Liden } 378