node.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * net/tipc/node.c: TIPC node management routines
  3. *
  4. * Copyright (c) 2000-2006, 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. static void node_lost_contact(struct tipc_node *n_ptr);
  41. static void node_established_contact(struct tipc_node *n_ptr);
  42. static DEFINE_SPINLOCK(node_create_lock);
  43. static struct hlist_head node_htable[NODE_HTABLE_SIZE];
  44. LIST_HEAD(tipc_node_list);
  45. static u32 tipc_num_nodes;
  46. u32 tipc_own_tag;
  47. /**
  48. * tipc_node_find - locate specified node object, if it exists
  49. */
  50. struct tipc_node *tipc_node_find(u32 addr)
  51. {
  52. struct tipc_node *node;
  53. struct hlist_node *pos;
  54. if (unlikely(!in_own_cluster(addr)))
  55. return NULL;
  56. hlist_for_each_entry(node, pos, &node_htable[tipc_hashfn(addr)], hash) {
  57. if (node->addr == addr)
  58. return node;
  59. }
  60. return NULL;
  61. }
  62. /**
  63. * tipc_node_create - create neighboring node
  64. *
  65. * Currently, this routine is called by neighbor discovery code, which holds
  66. * net_lock for reading only. We must take node_create_lock to ensure a node
  67. * isn't created twice if two different bearers discover the node at the same
  68. * time. (It would be preferable to switch to holding net_lock in write mode,
  69. * but this is a non-trivial change.)
  70. */
  71. struct tipc_node *tipc_node_create(u32 addr)
  72. {
  73. struct tipc_node *n_ptr, *temp_node;
  74. spin_lock_bh(&node_create_lock);
  75. n_ptr = tipc_node_find(addr);
  76. if (n_ptr) {
  77. spin_unlock_bh(&node_create_lock);
  78. return n_ptr;
  79. }
  80. n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
  81. if (!n_ptr) {
  82. spin_unlock_bh(&node_create_lock);
  83. warn("Node creation failed, no memory\n");
  84. return NULL;
  85. }
  86. n_ptr->addr = addr;
  87. spin_lock_init(&n_ptr->lock);
  88. INIT_HLIST_NODE(&n_ptr->hash);
  89. INIT_LIST_HEAD(&n_ptr->list);
  90. INIT_LIST_HEAD(&n_ptr->nsub);
  91. hlist_add_head(&n_ptr->hash, &node_htable[tipc_hashfn(addr)]);
  92. list_for_each_entry(temp_node, &tipc_node_list, list) {
  93. if (n_ptr->addr < temp_node->addr)
  94. break;
  95. }
  96. list_add_tail(&n_ptr->list, &temp_node->list);
  97. tipc_num_nodes++;
  98. spin_unlock_bh(&node_create_lock);
  99. return n_ptr;
  100. }
  101. void tipc_node_delete(struct tipc_node *n_ptr)
  102. {
  103. list_del(&n_ptr->list);
  104. hlist_del(&n_ptr->hash);
  105. kfree(n_ptr);
  106. tipc_num_nodes--;
  107. }
  108. /**
  109. * tipc_node_link_up - handle addition of link
  110. *
  111. * Link becomes active (alone or shared) or standby, depending on its priority.
  112. */
  113. void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
  114. {
  115. struct link **active = &n_ptr->active_links[0];
  116. n_ptr->working_links++;
  117. info("Established link <%s> on network plane %c\n",
  118. l_ptr->name, l_ptr->b_ptr->net_plane);
  119. if (!active[0]) {
  120. active[0] = active[1] = l_ptr;
  121. node_established_contact(n_ptr);
  122. return;
  123. }
  124. if (l_ptr->priority < active[0]->priority) {
  125. info("New link <%s> becomes standby\n", l_ptr->name);
  126. return;
  127. }
  128. tipc_link_send_duplicate(active[0], l_ptr);
  129. if (l_ptr->priority == active[0]->priority) {
  130. active[0] = l_ptr;
  131. return;
  132. }
  133. info("Old link <%s> becomes standby\n", active[0]->name);
  134. if (active[1] != active[0])
  135. info("Old link <%s> becomes standby\n", active[1]->name);
  136. active[0] = active[1] = l_ptr;
  137. }
  138. /**
  139. * node_select_active_links - select active link
  140. */
  141. static void node_select_active_links(struct tipc_node *n_ptr)
  142. {
  143. struct link **active = &n_ptr->active_links[0];
  144. u32 i;
  145. u32 highest_prio = 0;
  146. active[0] = active[1] = NULL;
  147. for (i = 0; i < MAX_BEARERS; i++) {
  148. struct link *l_ptr = n_ptr->links[i];
  149. if (!l_ptr || !tipc_link_is_up(l_ptr) ||
  150. (l_ptr->priority < highest_prio))
  151. continue;
  152. if (l_ptr->priority > highest_prio) {
  153. highest_prio = l_ptr->priority;
  154. active[0] = active[1] = l_ptr;
  155. } else {
  156. active[1] = l_ptr;
  157. }
  158. }
  159. }
  160. /**
  161. * tipc_node_link_down - handle loss of link
  162. */
  163. void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
  164. {
  165. struct link **active;
  166. n_ptr->working_links--;
  167. if (!tipc_link_is_active(l_ptr)) {
  168. info("Lost standby link <%s> on network plane %c\n",
  169. l_ptr->name, l_ptr->b_ptr->net_plane);
  170. return;
  171. }
  172. info("Lost link <%s> on network plane %c\n",
  173. l_ptr->name, l_ptr->b_ptr->net_plane);
  174. active = &n_ptr->active_links[0];
  175. if (active[0] == l_ptr)
  176. active[0] = active[1];
  177. if (active[1] == l_ptr)
  178. active[1] = active[0];
  179. if (active[0] == l_ptr)
  180. node_select_active_links(n_ptr);
  181. if (tipc_node_is_up(n_ptr))
  182. tipc_link_changeover(l_ptr);
  183. else
  184. node_lost_contact(n_ptr);
  185. }
  186. int tipc_node_has_active_links(struct tipc_node *n_ptr)
  187. {
  188. return n_ptr->active_links[0] != NULL;
  189. }
  190. int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
  191. {
  192. return n_ptr->working_links > 1;
  193. }
  194. int tipc_node_is_up(struct tipc_node *n_ptr)
  195. {
  196. return tipc_node_has_active_links(n_ptr);
  197. }
  198. struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
  199. {
  200. struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
  201. if (!n_ptr)
  202. n_ptr = tipc_node_create(l_ptr->addr);
  203. if (n_ptr) {
  204. u32 bearer_id = l_ptr->b_ptr->identity;
  205. char addr_string[16];
  206. if (n_ptr->link_cnt >= 2) {
  207. err("Attempt to create third link to %s\n",
  208. tipc_addr_string_fill(addr_string, n_ptr->addr));
  209. return NULL;
  210. }
  211. if (!n_ptr->links[bearer_id]) {
  212. n_ptr->links[bearer_id] = l_ptr;
  213. atomic_inc(&tipc_num_links);
  214. n_ptr->link_cnt++;
  215. return n_ptr;
  216. }
  217. err("Attempt to establish second link on <%s> to %s\n",
  218. l_ptr->b_ptr->name,
  219. tipc_addr_string_fill(addr_string, l_ptr->addr));
  220. }
  221. return NULL;
  222. }
  223. void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
  224. {
  225. n_ptr->links[l_ptr->b_ptr->identity] = NULL;
  226. atomic_dec(&tipc_num_links);
  227. n_ptr->link_cnt--;
  228. }
  229. /*
  230. * Routing table management - five cases to handle:
  231. *
  232. * 1: A link towards a zone/cluster external node comes up.
  233. * => Send a multicast message updating routing tables of all
  234. * system nodes within own cluster that the new destination
  235. * can be reached via this node.
  236. * (node.establishedContact()=>cluster.multicastNewRoute())
  237. *
  238. * 2: A link towards a slave node comes up.
  239. * => Send a multicast message updating routing tables of all
  240. * system nodes within own cluster that the new destination
  241. * can be reached via this node.
  242. * (node.establishedContact()=>cluster.multicastNewRoute())
  243. * => Send a message to the slave node about existence
  244. * of all system nodes within cluster:
  245. * (node.establishedContact()=>cluster.sendLocalRoutes())
  246. *
  247. * 3: A new cluster local system node becomes available.
  248. * => Send message(s) to this particular node containing
  249. * information about all cluster external and slave
  250. * nodes which can be reached via this node.
  251. * (node.establishedContact()==>network.sendExternalRoutes())
  252. * (node.establishedContact()==>network.sendSlaveRoutes())
  253. * => Send messages to all directly connected slave nodes
  254. * containing information about the existence of the new node
  255. * (node.establishedContact()=>cluster.multicastNewRoute())
  256. *
  257. * 4: The link towards a zone/cluster external node or slave
  258. * node goes down.
  259. * => Send a multcast message updating routing tables of all
  260. * nodes within cluster that the new destination can not any
  261. * longer be reached via this node.
  262. * (node.lostAllLinks()=>cluster.bcastLostRoute())
  263. *
  264. * 5: A cluster local system node becomes unavailable.
  265. * => Remove all references to this node from the local
  266. * routing tables. Note: This is a completely node
  267. * local operation.
  268. * (node.lostAllLinks()=>network.removeAsRouter())
  269. * => Send messages to all directly connected slave nodes
  270. * containing information about loss of the node
  271. * (node.establishedContact()=>cluster.multicastLostRoute())
  272. *
  273. */
  274. static void node_established_contact(struct tipc_node *n_ptr)
  275. {
  276. tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
  277. /* Syncronize broadcast acks */
  278. n_ptr->bclink.acked = tipc_bclink_get_last_sent();
  279. if (n_ptr->bclink.supported) {
  280. tipc_nmap_add(&tipc_bcast_nmap, n_ptr->addr);
  281. if (n_ptr->addr < tipc_own_addr)
  282. tipc_own_tag++;
  283. }
  284. }
  285. static void node_cleanup_finished(unsigned long node_addr)
  286. {
  287. struct tipc_node *n_ptr;
  288. read_lock_bh(&tipc_net_lock);
  289. n_ptr = tipc_node_find(node_addr);
  290. if (n_ptr) {
  291. tipc_node_lock(n_ptr);
  292. n_ptr->cleanup_required = 0;
  293. tipc_node_unlock(n_ptr);
  294. }
  295. read_unlock_bh(&tipc_net_lock);
  296. }
  297. static void node_lost_contact(struct tipc_node *n_ptr)
  298. {
  299. char addr_string[16];
  300. u32 i;
  301. /* Clean up broadcast reception remains */
  302. n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
  303. while (n_ptr->bclink.deferred_head) {
  304. struct sk_buff *buf = n_ptr->bclink.deferred_head;
  305. n_ptr->bclink.deferred_head = buf->next;
  306. buf_discard(buf);
  307. }
  308. if (n_ptr->bclink.defragm) {
  309. buf_discard(n_ptr->bclink.defragm);
  310. n_ptr->bclink.defragm = NULL;
  311. }
  312. if (n_ptr->bclink.supported) {
  313. tipc_bclink_acknowledge(n_ptr,
  314. mod(n_ptr->bclink.acked + 10000));
  315. tipc_nmap_remove(&tipc_bcast_nmap, n_ptr->addr);
  316. if (n_ptr->addr < tipc_own_addr)
  317. tipc_own_tag--;
  318. }
  319. info("Lost contact with %s\n",
  320. tipc_addr_string_fill(addr_string, n_ptr->addr));
  321. /* Abort link changeover */
  322. for (i = 0; i < MAX_BEARERS; i++) {
  323. struct link *l_ptr = n_ptr->links[i];
  324. if (!l_ptr)
  325. continue;
  326. l_ptr->reset_checkpoint = l_ptr->next_in_no;
  327. l_ptr->exp_msg_count = 0;
  328. tipc_link_reset_fragments(l_ptr);
  329. }
  330. /* Notify subscribers */
  331. tipc_nodesub_notify(n_ptr);
  332. /* Prevent re-contact with node until all cleanup is done */
  333. n_ptr->cleanup_required = 1;
  334. tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
  335. }
  336. struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
  337. {
  338. u32 domain;
  339. struct sk_buff *buf;
  340. struct tipc_node *n_ptr;
  341. struct tipc_node_info node_info;
  342. u32 payload_size;
  343. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  344. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  345. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  346. if (!tipc_addr_domain_valid(domain))
  347. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  348. " (network address)");
  349. read_lock_bh(&tipc_net_lock);
  350. if (!tipc_num_nodes) {
  351. read_unlock_bh(&tipc_net_lock);
  352. return tipc_cfg_reply_none();
  353. }
  354. /* For now, get space for all other nodes */
  355. payload_size = TLV_SPACE(sizeof(node_info)) * tipc_num_nodes;
  356. if (payload_size > 32768u) {
  357. read_unlock_bh(&tipc_net_lock);
  358. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  359. " (too many nodes)");
  360. }
  361. buf = tipc_cfg_reply_alloc(payload_size);
  362. if (!buf) {
  363. read_unlock_bh(&tipc_net_lock);
  364. return NULL;
  365. }
  366. /* Add TLVs for all nodes in scope */
  367. list_for_each_entry(n_ptr, &tipc_node_list, list) {
  368. if (!tipc_in_scope(domain, n_ptr->addr))
  369. continue;
  370. node_info.addr = htonl(n_ptr->addr);
  371. node_info.up = htonl(tipc_node_is_up(n_ptr));
  372. tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
  373. &node_info, sizeof(node_info));
  374. }
  375. read_unlock_bh(&tipc_net_lock);
  376. return buf;
  377. }
  378. struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
  379. {
  380. u32 domain;
  381. struct sk_buff *buf;
  382. struct tipc_node *n_ptr;
  383. struct tipc_link_info link_info;
  384. u32 payload_size;
  385. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  386. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  387. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  388. if (!tipc_addr_domain_valid(domain))
  389. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  390. " (network address)");
  391. if (tipc_mode != TIPC_NET_MODE)
  392. return tipc_cfg_reply_none();
  393. read_lock_bh(&tipc_net_lock);
  394. /* Get space for all unicast links + multicast link */
  395. payload_size = TLV_SPACE(sizeof(link_info)) *
  396. (atomic_read(&tipc_num_links) + 1);
  397. if (payload_size > 32768u) {
  398. read_unlock_bh(&tipc_net_lock);
  399. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  400. " (too many links)");
  401. }
  402. buf = tipc_cfg_reply_alloc(payload_size);
  403. if (!buf) {
  404. read_unlock_bh(&tipc_net_lock);
  405. return NULL;
  406. }
  407. /* Add TLV for broadcast link */
  408. link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr));
  409. link_info.up = htonl(1);
  410. strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  411. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
  412. /* Add TLVs for any other links in scope */
  413. list_for_each_entry(n_ptr, &tipc_node_list, list) {
  414. u32 i;
  415. if (!tipc_in_scope(domain, n_ptr->addr))
  416. continue;
  417. tipc_node_lock(n_ptr);
  418. for (i = 0; i < MAX_BEARERS; i++) {
  419. if (!n_ptr->links[i])
  420. continue;
  421. link_info.dest = htonl(n_ptr->addr);
  422. link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
  423. strcpy(link_info.str, n_ptr->links[i]->name);
  424. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
  425. &link_info, sizeof(link_info));
  426. }
  427. tipc_node_unlock(n_ptr);
  428. }
  429. read_unlock_bh(&tipc_net_lock);
  430. return buf;
  431. }