Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/name_distr.c: TIPC name distribution code |
| 3 | * |
| 4 | * Copyright (c) 2003-2005, Ericsson Research Canada |
| 5 | * Copyright (c) 2005, Wind River Systems |
| 6 | * Copyright (c) 2005-2006, Ericsson AB |
| 7 | * All rights reserved. |
| 8 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame^] | 9 | * Redistribution and use in source and binary forms, with or without |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 10 | * modification, are permitted provided that the following conditions are met: |
| 11 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame^] | 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the names of the copyright holders nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived from |
| 19 | * this software without specific prior written permission. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 20 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame^] | 21 | * Alternatively, this software may be distributed under the terms of the |
| 22 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 23 | * Software Foundation. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 35 | * POSSIBILITY OF SUCH DAMAGE. |
| 36 | */ |
| 37 | |
| 38 | #include "core.h" |
| 39 | #include "cluster.h" |
| 40 | #include "dbg.h" |
| 41 | #include "link.h" |
| 42 | #include "msg.h" |
| 43 | #include "name_distr.h" |
| 44 | |
| 45 | #undef DBG_OUTPUT |
| 46 | #define DBG_OUTPUT NULL |
| 47 | |
| 48 | #define ITEM_SIZE sizeof(struct distr_item) |
| 49 | |
| 50 | /** |
| 51 | * struct distr_item - publication info distributed to other nodes |
| 52 | * @type: name sequence type |
| 53 | * @lower: name sequence lower bound |
| 54 | * @upper: name sequence upper bound |
| 55 | * @ref: publishing port reference |
| 56 | * @key: publication key |
| 57 | * |
| 58 | * ===> All fields are stored in network byte order. <=== |
| 59 | * |
| 60 | * First 3 fields identify (name or) name sequence being published. |
| 61 | * Reference field uniquely identifies port that published name sequence. |
| 62 | * Key field uniquely identifies publication, in the event a port has |
| 63 | * multiple publications of the same name sequence. |
| 64 | * |
| 65 | * Note: There is no field that identifies the publishing node because it is |
| 66 | * the same for all items contained within a publication message. |
| 67 | */ |
| 68 | |
| 69 | struct distr_item { |
| 70 | u32 type; |
| 71 | u32 lower; |
| 72 | u32 upper; |
| 73 | u32 ref; |
| 74 | u32 key; |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * List of externally visible publications by this node -- |
| 79 | * that is, all publications having scope > TIPC_NODE_SCOPE. |
| 80 | */ |
| 81 | |
| 82 | static LIST_HEAD(publ_root); |
| 83 | static u32 publ_cnt = 0; |
| 84 | |
| 85 | /** |
| 86 | * publ_to_item - add publication info to a publication message |
| 87 | */ |
| 88 | |
| 89 | static void publ_to_item(struct distr_item *i, struct publication *p) |
| 90 | { |
| 91 | i->type = htonl(p->type); |
| 92 | i->lower = htonl(p->lower); |
| 93 | i->upper = htonl(p->upper); |
| 94 | i->ref = htonl(p->ref); |
| 95 | i->key = htonl(p->key); |
| 96 | dbg("publ_to_item: %u, %u, %u\n", p->type, p->lower, p->upper); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * named_prepare_buf - allocate & initialize a publication message |
| 101 | */ |
| 102 | |
| 103 | static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest) |
| 104 | { |
| 105 | struct sk_buff *buf = buf_acquire(LONG_H_SIZE + size); |
| 106 | struct tipc_msg *msg; |
| 107 | |
| 108 | if (buf != NULL) { |
| 109 | msg = buf_msg(buf); |
| 110 | msg_init(msg, NAME_DISTRIBUTOR, type, TIPC_OK, |
| 111 | LONG_H_SIZE, dest); |
| 112 | msg_set_size(msg, LONG_H_SIZE + size); |
| 113 | } |
| 114 | return buf; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * named_publish - tell other nodes about a new publication by this node |
| 119 | */ |
| 120 | |
| 121 | void named_publish(struct publication *publ) |
| 122 | { |
| 123 | struct sk_buff *buf; |
| 124 | struct distr_item *item; |
| 125 | |
| 126 | list_add(&publ->local_list, &publ_root); |
| 127 | publ_cnt++; |
| 128 | |
| 129 | buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0); |
| 130 | if (!buf) { |
| 131 | warn("Memory squeeze; failed to distribute publication\n"); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | item = (struct distr_item *)msg_data(buf_msg(buf)); |
| 136 | publ_to_item(item, publ); |
| 137 | dbg("named_withdraw: broadcasting publish msg\n"); |
| 138 | cluster_broadcast(buf); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * named_withdraw - tell other nodes about a withdrawn publication by this node |
| 143 | */ |
| 144 | |
| 145 | void named_withdraw(struct publication *publ) |
| 146 | { |
| 147 | struct sk_buff *buf; |
| 148 | struct distr_item *item; |
| 149 | |
| 150 | list_del(&publ->local_list); |
| 151 | publ_cnt--; |
| 152 | |
| 153 | buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0); |
| 154 | if (!buf) { |
| 155 | warn("Memory squeeze; failed to distribute withdrawal\n"); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | item = (struct distr_item *)msg_data(buf_msg(buf)); |
| 160 | publ_to_item(item, publ); |
| 161 | dbg("named_withdraw: broadcasting withdraw msg\n"); |
| 162 | cluster_broadcast(buf); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * named_node_up - tell specified node about all publications by this node |
| 167 | */ |
| 168 | |
| 169 | void named_node_up(unsigned long node) |
| 170 | { |
| 171 | struct publication *publ; |
| 172 | struct distr_item *item = 0; |
| 173 | struct sk_buff *buf = 0; |
| 174 | u32 left = 0; |
| 175 | u32 rest; |
| 176 | u32 max_item_buf; |
| 177 | |
| 178 | assert(in_own_cluster(node)); |
| 179 | read_lock_bh(&nametbl_lock); |
| 180 | max_item_buf = TIPC_MAX_USER_MSG_SIZE / ITEM_SIZE; |
| 181 | max_item_buf *= ITEM_SIZE; |
| 182 | rest = publ_cnt * ITEM_SIZE; |
| 183 | |
| 184 | list_for_each_entry(publ, &publ_root, local_list) { |
| 185 | if (!buf) { |
| 186 | left = (rest <= max_item_buf) ? rest : max_item_buf; |
| 187 | rest -= left; |
| 188 | buf = named_prepare_buf(PUBLICATION, left, node); |
| 189 | if (buf == NULL) { |
| 190 | warn("Memory Squeeze; could not send publication\n"); |
| 191 | goto exit; |
| 192 | } |
| 193 | item = (struct distr_item *)msg_data(buf_msg(buf)); |
| 194 | } |
| 195 | publ_to_item(item, publ); |
| 196 | item++; |
| 197 | left -= ITEM_SIZE; |
| 198 | if (!left) { |
| 199 | msg_set_link_selector(buf_msg(buf), node); |
| 200 | dbg("named_node_up: sending publish msg to " |
| 201 | "<%u.%u.%u>\n", tipc_zone(node), |
| 202 | tipc_cluster(node), tipc_node(node)); |
| 203 | link_send(buf, node, node); |
| 204 | buf = 0; |
| 205 | } |
| 206 | } |
| 207 | exit: |
| 208 | read_unlock_bh(&nametbl_lock); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * node_is_down - remove publication associated with a failed node |
| 213 | * |
| 214 | * Invoked for each publication issued by a newly failed node. |
| 215 | * Removes publication structure from name table & deletes it. |
| 216 | * In rare cases the link may have come back up again when this |
| 217 | * function is called, and we have two items representing the same |
| 218 | * publication. Nudge this item's key to distinguish it from the other. |
| 219 | * (Note: Publication's node subscription is already unsubscribed.) |
| 220 | */ |
| 221 | |
| 222 | static void node_is_down(struct publication *publ) |
| 223 | { |
| 224 | struct publication *p; |
| 225 | write_lock_bh(&nametbl_lock); |
| 226 | dbg("node_is_down: withdrawing %u, %u, %u\n", |
| 227 | publ->type, publ->lower, publ->upper); |
| 228 | publ->key += 1222345; |
| 229 | p = nametbl_remove_publ(publ->type, publ->lower, |
| 230 | publ->node, publ->ref, publ->key); |
| 231 | assert(p == publ); |
| 232 | write_unlock_bh(&nametbl_lock); |
| 233 | if (publ) |
| 234 | kfree(publ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * named_recv - process name table update message sent by another node |
| 239 | */ |
| 240 | |
| 241 | void named_recv(struct sk_buff *buf) |
| 242 | { |
| 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 | |
| 248 | write_lock_bh(&nametbl_lock); |
| 249 | while (count--) { |
| 250 | if (msg_type(msg) == PUBLICATION) { |
| 251 | dbg("named_recv: got publication for %u, %u, %u\n", |
| 252 | ntohl(item->type), ntohl(item->lower), |
| 253 | ntohl(item->upper)); |
| 254 | publ = nametbl_insert_publ(ntohl(item->type), |
| 255 | ntohl(item->lower), |
| 256 | ntohl(item->upper), |
| 257 | TIPC_CLUSTER_SCOPE, |
| 258 | msg_orignode(msg), |
| 259 | ntohl(item->ref), |
| 260 | ntohl(item->key)); |
| 261 | if (publ) { |
| 262 | nodesub_subscribe(&publ->subscr, |
| 263 | msg_orignode(msg), |
| 264 | publ, |
| 265 | (net_ev_handler)node_is_down); |
| 266 | } |
| 267 | } else if (msg_type(msg) == WITHDRAWAL) { |
| 268 | dbg("named_recv: got withdrawl for %u, %u, %u\n", |
| 269 | ntohl(item->type), ntohl(item->lower), |
| 270 | ntohl(item->upper)); |
| 271 | publ = nametbl_remove_publ(ntohl(item->type), |
| 272 | ntohl(item->lower), |
| 273 | msg_orignode(msg), |
| 274 | ntohl(item->ref), |
| 275 | ntohl(item->key)); |
| 276 | |
| 277 | if (publ) { |
| 278 | nodesub_unsubscribe(&publ->subscr); |
| 279 | kfree(publ); |
| 280 | } |
| 281 | } else { |
| 282 | warn("named_recv: unknown msg\n"); |
| 283 | } |
| 284 | item++; |
| 285 | } |
| 286 | write_unlock_bh(&nametbl_lock); |
| 287 | buf_discard(buf); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * named_reinit - re-initialize local publication list |
| 292 | * |
| 293 | * This routine is called whenever TIPC networking is (re)enabled. |
| 294 | * All existing publications by this node that have "cluster" or "zone" scope |
| 295 | * are updated to reflect the node's current network address. |
| 296 | * (If the node's address is unchanged, the update loop terminates immediately.) |
| 297 | */ |
| 298 | |
| 299 | void named_reinit(void) |
| 300 | { |
| 301 | struct publication *publ; |
| 302 | |
| 303 | write_lock_bh(&nametbl_lock); |
| 304 | list_for_each_entry(publ, &publ_root, local_list) { |
| 305 | if (publ->node == tipc_own_addr) |
| 306 | break; |
| 307 | publ->node = tipc_own_addr; |
| 308 | } |
| 309 | write_unlock_bh(&nametbl_lock); |
| 310 | } |