node.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * net/tipc/node.c: TIPC node management routines
  3. *
  4. * Copyright (c) 2000-2006, 2012 Ericsson AB
  5. * Copyright (c) 2005-2006, 2010-2011, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 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.
  19. *
  20. * 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
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "config.h"
  38. #include "node.h"
  39. #include "name_distr.h"
  40. #define NODE_HTABLE_SIZE 512
  41. static void node_lost_contact(struct tipc_node *n_ptr);
  42. static void node_established_contact(struct tipc_node *n_ptr);
  43. static DEFINE_SPINLOCK(node_create_lock);
  44. static struct hlist_head node_htable[NODE_HTABLE_SIZE];
  45. LIST_HEAD(tipc_node_list);
  46. static u32 tipc_num_nodes;
  47. static atomic_t tipc_num_links = ATOMIC_INIT(0);
  48. /*
  49. * A trivial power-of-two bitmask technique is used for speed, since this
  50. * operation is done for every incoming TIPC packet. The number of hash table
  51. * entries has been chosen so that no hash chain exceeds 8 nodes and will
  52. * usually be much smaller (typically only a single node).
  53. */
  54. static unsigned int tipc_hashfn(u32 addr)
  55. {
  56. return addr & (NODE_HTABLE_SIZE - 1);
  57. }
  58. /*
  59. * tipc_node_find - locate specified node object, if it exists
  60. */
  61. struct tipc_node *tipc_node_find(u32 addr)
  62. {
  63. struct tipc_node *node;
  64. if (unlikely(!in_own_cluster_exact(addr)))
  65. return NULL;
  66. hlist_for_each_entry(node, &node_htable[tipc_hashfn(addr)], hash) {
  67. if (node->addr == addr)
  68. return node;
  69. }
  70. return NULL;
  71. }
  72. /**
  73. * tipc_node_create - create neighboring node
  74. *
  75. * Currently, this routine is called by neighbor discovery code, which holds
  76. * net_lock for reading only. We must take node_create_lock to ensure a node
  77. * isn't created twice if two different bearers discover the node at the same
  78. * time. (It would be preferable to switch to holding net_lock in write mode,
  79. * but this is a non-trivial change.)
  80. */
  81. struct tipc_node *tipc_node_create(u32 addr)
  82. {
  83. struct tipc_node *n_ptr, *temp_node;
  84. spin_lock_bh(&node_create_lock);
  85. n_ptr = tipc_node_find(addr);
  86. if (n_ptr) {
  87. spin_unlock_bh(&node_create_lock);
  88. return n_ptr;
  89. }
  90. n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
  91. if (!n_ptr) {
  92. spin_unlock_bh(&node_create_lock);
  93. pr_warn("Node creation failed, no memory\n");
  94. return NULL;
  95. }
  96. n_ptr->addr = addr;
  97. spin_lock_init(&n_ptr->lock);
  98. INIT_HLIST_NODE(&n_ptr->hash);
  99. INIT_LIST_HEAD(&n_ptr->list);
  100. INIT_LIST_HEAD(&n_ptr->nsub);
  101. hlist_add_head(&n_ptr->hash, &node_htable[tipc_hashfn(addr)]);
  102. list_for_each_entry(temp_node, &tipc_node_list, list) {
  103. if (n_ptr->addr < temp_node->addr)
  104. break;
  105. }
  106. list_add_tail(&n_ptr->list, &temp_node->list);
  107. n_ptr->block_setup = WAIT_PEER_DOWN;
  108. n_ptr->signature = INVALID_NODE_SIG;
  109. tipc_num_nodes++;
  110. spin_unlock_bh(&node_create_lock);
  111. return n_ptr;
  112. }
  113. void tipc_node_delete(struct tipc_node *n_ptr)
  114. {
  115. list_del(&n_ptr->list);
  116. hlist_del(&n_ptr->hash);
  117. kfree(n_ptr);
  118. tipc_num_nodes--;
  119. }
  120. /**
  121. * tipc_node_link_up - handle addition of link
  122. *
  123. * Link becomes active (alone or shared) or standby, depending on its priority.
  124. */
  125. void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  126. {
  127. struct tipc_link **active = &n_ptr->active_links[0];
  128. n_ptr->working_links++;
  129. pr_info("Established link <%s> on network plane %c\n",
  130. l_ptr->name, l_ptr->b_ptr->net_plane);
  131. if (!active[0]) {
  132. active[0] = active[1] = l_ptr;
  133. node_established_contact(n_ptr);
  134. return;
  135. }
  136. if (l_ptr->priority < active[0]->priority) {
  137. pr_info("New link <%s> becomes standby\n", l_ptr->name);
  138. return;
  139. }
  140. tipc_link_send_duplicate(active[0], l_ptr);
  141. if (l_ptr->priority == active[0]->priority) {
  142. active[0] = l_ptr;
  143. return;
  144. }
  145. pr_info("Old link <%s> becomes standby\n", active[0]->name);
  146. if (active[1] != active[0])
  147. pr_info("Old link <%s> becomes standby\n", active[1]->name);
  148. active[0] = active[1] = l_ptr;
  149. }
  150. /**
  151. * node_select_active_links - select active link
  152. */
  153. static void node_select_active_links(struct tipc_node *n_ptr)
  154. {
  155. struct tipc_link **active = &n_ptr->active_links[0];
  156. u32 i;
  157. u32 highest_prio = 0;
  158. active[0] = active[1] = NULL;
  159. for (i = 0; i < MAX_BEARERS; i++) {
  160. struct tipc_link *l_ptr = n_ptr->links[i];
  161. if (!l_ptr || !tipc_link_is_up(l_ptr) ||
  162. (l_ptr->priority < highest_prio))
  163. continue;
  164. if (l_ptr->priority > highest_prio) {
  165. highest_prio = l_ptr->priority;
  166. active[0] = active[1] = l_ptr;
  167. } else {
  168. active[1] = l_ptr;
  169. }
  170. }
  171. }
  172. /**
  173. * tipc_node_link_down - handle loss of link
  174. */
  175. void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  176. {
  177. struct tipc_link **active;
  178. n_ptr->working_links--;
  179. if (!tipc_link_is_active(l_ptr)) {
  180. pr_info("Lost standby link <%s> on network plane %c\n",
  181. l_ptr->name, l_ptr->b_ptr->net_plane);
  182. return;
  183. }
  184. pr_info("Lost link <%s> on network plane %c\n",
  185. l_ptr->name, l_ptr->b_ptr->net_plane);
  186. active = &n_ptr->active_links[0];
  187. if (active[0] == l_ptr)
  188. active[0] = active[1];
  189. if (active[1] == l_ptr)
  190. active[1] = active[0];
  191. if (active[0] == l_ptr)
  192. node_select_active_links(n_ptr);
  193. if (tipc_node_is_up(n_ptr))
  194. tipc_link_changeover(l_ptr);
  195. else
  196. node_lost_contact(n_ptr);
  197. }
  198. int tipc_node_active_links(struct tipc_node *n_ptr)
  199. {
  200. return n_ptr->active_links[0] != NULL;
  201. }
  202. int tipc_node_redundant_links(struct tipc_node *n_ptr)
  203. {
  204. return n_ptr->working_links > 1;
  205. }
  206. int tipc_node_is_up(struct tipc_node *n_ptr)
  207. {
  208. return tipc_node_active_links(n_ptr);
  209. }
  210. void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  211. {
  212. n_ptr->links[l_ptr->b_ptr->identity] = l_ptr;
  213. atomic_inc(&tipc_num_links);
  214. n_ptr->link_cnt++;
  215. }
  216. void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  217. {
  218. n_ptr->links[l_ptr->b_ptr->identity] = NULL;
  219. atomic_dec(&tipc_num_links);
  220. n_ptr->link_cnt--;
  221. }
  222. static void node_established_contact(struct tipc_node *n_ptr)
  223. {
  224. tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
  225. n_ptr->bclink.oos_state = 0;
  226. n_ptr->bclink.acked = tipc_bclink_get_last_sent();
  227. tipc_bclink_add_node(n_ptr->addr);
  228. }
  229. static void node_name_purge_complete(unsigned long node_addr)
  230. {
  231. struct tipc_node *n_ptr;
  232. read_lock_bh(&tipc_net_lock);
  233. n_ptr = tipc_node_find(node_addr);
  234. if (n_ptr) {
  235. tipc_node_lock(n_ptr);
  236. n_ptr->block_setup &= ~WAIT_NAMES_GONE;
  237. tipc_node_unlock(n_ptr);
  238. }
  239. read_unlock_bh(&tipc_net_lock);
  240. }
  241. static void node_lost_contact(struct tipc_node *n_ptr)
  242. {
  243. char addr_string[16];
  244. u32 i;
  245. pr_info("Lost contact with %s\n",
  246. tipc_addr_string_fill(addr_string, n_ptr->addr));
  247. /* Flush broadcast link info associated with lost node */
  248. if (n_ptr->bclink.recv_permitted) {
  249. while (n_ptr->bclink.deferred_head) {
  250. struct sk_buff *buf = n_ptr->bclink.deferred_head;
  251. n_ptr->bclink.deferred_head = buf->next;
  252. kfree_skb(buf);
  253. }
  254. n_ptr->bclink.deferred_size = 0;
  255. if (n_ptr->bclink.defragm) {
  256. kfree_skb(n_ptr->bclink.defragm);
  257. n_ptr->bclink.defragm = NULL;
  258. }
  259. tipc_bclink_remove_node(n_ptr->addr);
  260. tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ);
  261. n_ptr->bclink.recv_permitted = false;
  262. }
  263. /* Abort link changeover */
  264. for (i = 0; i < MAX_BEARERS; i++) {
  265. struct tipc_link *l_ptr = n_ptr->links[i];
  266. if (!l_ptr)
  267. continue;
  268. l_ptr->reset_checkpoint = l_ptr->next_in_no;
  269. l_ptr->exp_msg_count = 0;
  270. tipc_link_reset_fragments(l_ptr);
  271. }
  272. /* Notify subscribers */
  273. tipc_nodesub_notify(n_ptr);
  274. /* Prevent re-contact with node until cleanup is done */
  275. n_ptr->block_setup = WAIT_PEER_DOWN | WAIT_NAMES_GONE;
  276. tipc_k_signal((Handler)node_name_purge_complete, n_ptr->addr);
  277. }
  278. struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
  279. {
  280. u32 domain;
  281. struct sk_buff *buf;
  282. struct tipc_node *n_ptr;
  283. struct tipc_node_info node_info;
  284. u32 payload_size;
  285. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  286. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  287. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  288. if (!tipc_addr_domain_valid(domain))
  289. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  290. " (network address)");
  291. read_lock_bh(&tipc_net_lock);
  292. if (!tipc_num_nodes) {
  293. read_unlock_bh(&tipc_net_lock);
  294. return tipc_cfg_reply_none();
  295. }
  296. /* For now, get space for all other nodes */
  297. payload_size = TLV_SPACE(sizeof(node_info)) * tipc_num_nodes;
  298. if (payload_size > 32768u) {
  299. read_unlock_bh(&tipc_net_lock);
  300. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  301. " (too many nodes)");
  302. }
  303. buf = tipc_cfg_reply_alloc(payload_size);
  304. if (!buf) {
  305. read_unlock_bh(&tipc_net_lock);
  306. return NULL;
  307. }
  308. /* Add TLVs for all nodes in scope */
  309. list_for_each_entry(n_ptr, &tipc_node_list, list) {
  310. if (!tipc_in_scope(domain, n_ptr->addr))
  311. continue;
  312. node_info.addr = htonl(n_ptr->addr);
  313. node_info.up = htonl(tipc_node_is_up(n_ptr));
  314. tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
  315. &node_info, sizeof(node_info));
  316. }
  317. read_unlock_bh(&tipc_net_lock);
  318. return buf;
  319. }
  320. struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
  321. {
  322. u32 domain;
  323. struct sk_buff *buf;
  324. struct tipc_node *n_ptr;
  325. struct tipc_link_info link_info;
  326. u32 payload_size;
  327. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  328. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  329. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  330. if (!tipc_addr_domain_valid(domain))
  331. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  332. " (network address)");
  333. if (!tipc_own_addr)
  334. return tipc_cfg_reply_none();
  335. read_lock_bh(&tipc_net_lock);
  336. /* Get space for all unicast links + broadcast link */
  337. payload_size = TLV_SPACE(sizeof(link_info)) *
  338. (atomic_read(&tipc_num_links) + 1);
  339. if (payload_size > 32768u) {
  340. read_unlock_bh(&tipc_net_lock);
  341. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  342. " (too many links)");
  343. }
  344. buf = tipc_cfg_reply_alloc(payload_size);
  345. if (!buf) {
  346. read_unlock_bh(&tipc_net_lock);
  347. return NULL;
  348. }
  349. /* Add TLV for broadcast link */
  350. link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr));
  351. link_info.up = htonl(1);
  352. strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  353. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
  354. /* Add TLVs for any other links in scope */
  355. list_for_each_entry(n_ptr, &tipc_node_list, list) {
  356. u32 i;
  357. if (!tipc_in_scope(domain, n_ptr->addr))
  358. continue;
  359. tipc_node_lock(n_ptr);
  360. for (i = 0; i < MAX_BEARERS; i++) {
  361. if (!n_ptr->links[i])
  362. continue;
  363. link_info.dest = htonl(n_ptr->addr);
  364. link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
  365. strcpy(link_info.str, n_ptr->links[i]->name);
  366. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
  367. &link_info, sizeof(link_info));
  368. }
  369. tipc_node_unlock(n_ptr);
  370. }
  371. read_unlock_bh(&tipc_net_lock);
  372. return buf;
  373. }