node.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * net/tipc/node.c: TIPC node management routines
  3. *
  4. * Copyright (c) 2000-2006, 2012-2014, Ericsson AB
  5. * Copyright (c) 2005-2006, 2010-2014, 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 struct hlist_head node_htable[NODE_HTABLE_SIZE];
  44. LIST_HEAD(tipc_node_list);
  45. static u32 tipc_num_nodes;
  46. static u32 tipc_num_links;
  47. static DEFINE_SPINLOCK(node_list_lock);
  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. rcu_read_lock();
  67. hlist_for_each_entry_rcu(node, &node_htable[tipc_hashfn(addr)], hash) {
  68. if (node->addr == addr) {
  69. rcu_read_unlock();
  70. return node;
  71. }
  72. }
  73. rcu_read_unlock();
  74. return NULL;
  75. }
  76. struct tipc_node *tipc_node_create(u32 addr)
  77. {
  78. struct tipc_node *n_ptr, *temp_node;
  79. spin_lock_bh(&node_list_lock);
  80. n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
  81. if (!n_ptr) {
  82. spin_unlock_bh(&node_list_lock);
  83. pr_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_rcu(&n_ptr->hash, &node_htable[tipc_hashfn(addr)]);
  92. list_for_each_entry_rcu(temp_node, &tipc_node_list, list) {
  93. if (n_ptr->addr < temp_node->addr)
  94. break;
  95. }
  96. list_add_tail_rcu(&n_ptr->list, &temp_node->list);
  97. n_ptr->action_flags = TIPC_WAIT_PEER_LINKS_DOWN;
  98. n_ptr->signature = INVALID_NODE_SIG;
  99. tipc_num_nodes++;
  100. spin_unlock_bh(&node_list_lock);
  101. return n_ptr;
  102. }
  103. static void tipc_node_delete(struct tipc_node *n_ptr)
  104. {
  105. list_del_rcu(&n_ptr->list);
  106. hlist_del_rcu(&n_ptr->hash);
  107. kfree_rcu(n_ptr, rcu);
  108. tipc_num_nodes--;
  109. }
  110. void tipc_node_stop(void)
  111. {
  112. struct tipc_node *node, *t_node;
  113. spin_lock_bh(&node_list_lock);
  114. list_for_each_entry_safe(node, t_node, &tipc_node_list, list)
  115. tipc_node_delete(node);
  116. spin_unlock_bh(&node_list_lock);
  117. }
  118. /**
  119. * tipc_node_link_up - handle addition of link
  120. *
  121. * Link becomes active (alone or shared) or standby, depending on its priority.
  122. */
  123. void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  124. {
  125. struct tipc_link **active = &n_ptr->active_links[0];
  126. u32 addr = n_ptr->addr;
  127. n_ptr->working_links++;
  128. tipc_nametbl_publish(TIPC_LINK_STATE, addr, addr, TIPC_NODE_SCOPE,
  129. l_ptr->bearer_id, addr);
  130. pr_info("Established link <%s> on network plane %c\n",
  131. l_ptr->name, l_ptr->net_plane);
  132. if (!active[0]) {
  133. active[0] = active[1] = l_ptr;
  134. node_established_contact(n_ptr);
  135. goto exit;
  136. }
  137. if (l_ptr->priority < active[0]->priority) {
  138. pr_info("New link <%s> becomes standby\n", l_ptr->name);
  139. goto exit;
  140. }
  141. tipc_link_dup_queue_xmit(active[0], l_ptr);
  142. if (l_ptr->priority == active[0]->priority) {
  143. active[0] = l_ptr;
  144. goto exit;
  145. }
  146. pr_info("Old link <%s> becomes standby\n", active[0]->name);
  147. if (active[1] != active[0])
  148. pr_info("Old link <%s> becomes standby\n", active[1]->name);
  149. active[0] = active[1] = l_ptr;
  150. exit:
  151. /* Leave room for changeover header when returning 'mtu' to users: */
  152. n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE;
  153. n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE;
  154. }
  155. /**
  156. * node_select_active_links - select active link
  157. */
  158. static void node_select_active_links(struct tipc_node *n_ptr)
  159. {
  160. struct tipc_link **active = &n_ptr->active_links[0];
  161. u32 i;
  162. u32 highest_prio = 0;
  163. active[0] = active[1] = NULL;
  164. for (i = 0; i < MAX_BEARERS; i++) {
  165. struct tipc_link *l_ptr = n_ptr->links[i];
  166. if (!l_ptr || !tipc_link_is_up(l_ptr) ||
  167. (l_ptr->priority < highest_prio))
  168. continue;
  169. if (l_ptr->priority > highest_prio) {
  170. highest_prio = l_ptr->priority;
  171. active[0] = active[1] = l_ptr;
  172. } else {
  173. active[1] = l_ptr;
  174. }
  175. }
  176. }
  177. /**
  178. * tipc_node_link_down - handle loss of link
  179. */
  180. void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  181. {
  182. struct tipc_link **active;
  183. u32 addr = n_ptr->addr;
  184. n_ptr->working_links--;
  185. tipc_nametbl_withdraw(TIPC_LINK_STATE, addr, l_ptr->bearer_id, addr);
  186. if (!tipc_link_is_active(l_ptr)) {
  187. pr_info("Lost standby link <%s> on network plane %c\n",
  188. l_ptr->name, l_ptr->net_plane);
  189. return;
  190. }
  191. pr_info("Lost link <%s> on network plane %c\n",
  192. l_ptr->name, l_ptr->net_plane);
  193. active = &n_ptr->active_links[0];
  194. if (active[0] == l_ptr)
  195. active[0] = active[1];
  196. if (active[1] == l_ptr)
  197. active[1] = active[0];
  198. if (active[0] == l_ptr)
  199. node_select_active_links(n_ptr);
  200. if (tipc_node_is_up(n_ptr))
  201. tipc_link_failover_send_queue(l_ptr);
  202. else
  203. node_lost_contact(n_ptr);
  204. /* Leave room for changeover header when returning 'mtu' to users: */
  205. if (active[0]) {
  206. n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE;
  207. n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE;
  208. return;
  209. }
  210. /* Loopback link went down? No fragmentation needed from now on. */
  211. if (n_ptr->addr == tipc_own_addr) {
  212. n_ptr->act_mtus[0] = MAX_MSG_SIZE;
  213. n_ptr->act_mtus[1] = MAX_MSG_SIZE;
  214. }
  215. }
  216. int tipc_node_active_links(struct tipc_node *n_ptr)
  217. {
  218. return n_ptr->active_links[0] != NULL;
  219. }
  220. int tipc_node_is_up(struct tipc_node *n_ptr)
  221. {
  222. return tipc_node_active_links(n_ptr);
  223. }
  224. void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  225. {
  226. n_ptr->links[l_ptr->bearer_id] = l_ptr;
  227. spin_lock_bh(&node_list_lock);
  228. tipc_num_links++;
  229. spin_unlock_bh(&node_list_lock);
  230. n_ptr->link_cnt++;
  231. }
  232. void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  233. {
  234. int i;
  235. for (i = 0; i < MAX_BEARERS; i++) {
  236. if (l_ptr != n_ptr->links[i])
  237. continue;
  238. n_ptr->links[i] = NULL;
  239. spin_lock_bh(&node_list_lock);
  240. tipc_num_links--;
  241. spin_unlock_bh(&node_list_lock);
  242. n_ptr->link_cnt--;
  243. }
  244. }
  245. static void node_established_contact(struct tipc_node *n_ptr)
  246. {
  247. n_ptr->action_flags |= TIPC_NOTIFY_NODE_UP;
  248. n_ptr->bclink.oos_state = 0;
  249. n_ptr->bclink.acked = tipc_bclink_get_last_sent();
  250. tipc_bclink_add_node(n_ptr->addr);
  251. }
  252. static void node_lost_contact(struct tipc_node *n_ptr)
  253. {
  254. char addr_string[16];
  255. u32 i;
  256. pr_info("Lost contact with %s\n",
  257. tipc_addr_string_fill(addr_string, n_ptr->addr));
  258. /* Flush broadcast link info associated with lost node */
  259. if (n_ptr->bclink.recv_permitted) {
  260. kfree_skb_list(n_ptr->bclink.deferred_head);
  261. n_ptr->bclink.deferred_size = 0;
  262. if (n_ptr->bclink.reasm_buf) {
  263. kfree_skb(n_ptr->bclink.reasm_buf);
  264. n_ptr->bclink.reasm_buf = NULL;
  265. }
  266. tipc_bclink_remove_node(n_ptr->addr);
  267. tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ);
  268. n_ptr->bclink.recv_permitted = false;
  269. }
  270. /* Abort link changeover */
  271. for (i = 0; i < MAX_BEARERS; i++) {
  272. struct tipc_link *l_ptr = n_ptr->links[i];
  273. if (!l_ptr)
  274. continue;
  275. l_ptr->reset_checkpoint = l_ptr->next_in_no;
  276. l_ptr->exp_msg_count = 0;
  277. tipc_link_reset_fragments(l_ptr);
  278. }
  279. n_ptr->action_flags &= ~TIPC_WAIT_OWN_LINKS_DOWN;
  280. /* Notify subscribers and prevent re-contact with node until
  281. * cleanup is done.
  282. */
  283. n_ptr->action_flags |= TIPC_WAIT_PEER_LINKS_DOWN |
  284. TIPC_NOTIFY_NODE_DOWN;
  285. }
  286. struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
  287. {
  288. u32 domain;
  289. struct sk_buff *buf;
  290. struct tipc_node *n_ptr;
  291. struct tipc_node_info node_info;
  292. u32 payload_size;
  293. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  294. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  295. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  296. if (!tipc_addr_domain_valid(domain))
  297. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  298. " (network address)");
  299. spin_lock_bh(&node_list_lock);
  300. if (!tipc_num_nodes) {
  301. spin_unlock_bh(&node_list_lock);
  302. return tipc_cfg_reply_none();
  303. }
  304. /* For now, get space for all other nodes */
  305. payload_size = TLV_SPACE(sizeof(node_info)) * tipc_num_nodes;
  306. if (payload_size > 32768u) {
  307. spin_unlock_bh(&node_list_lock);
  308. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  309. " (too many nodes)");
  310. }
  311. spin_unlock_bh(&node_list_lock);
  312. buf = tipc_cfg_reply_alloc(payload_size);
  313. if (!buf)
  314. return NULL;
  315. /* Add TLVs for all nodes in scope */
  316. rcu_read_lock();
  317. list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
  318. if (!tipc_in_scope(domain, n_ptr->addr))
  319. continue;
  320. node_info.addr = htonl(n_ptr->addr);
  321. node_info.up = htonl(tipc_node_is_up(n_ptr));
  322. tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
  323. &node_info, sizeof(node_info));
  324. }
  325. rcu_read_unlock();
  326. return buf;
  327. }
  328. struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
  329. {
  330. u32 domain;
  331. struct sk_buff *buf;
  332. struct tipc_node *n_ptr;
  333. struct tipc_link_info link_info;
  334. u32 payload_size;
  335. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  336. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  337. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  338. if (!tipc_addr_domain_valid(domain))
  339. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  340. " (network address)");
  341. if (!tipc_own_addr)
  342. return tipc_cfg_reply_none();
  343. spin_lock_bh(&node_list_lock);
  344. /* Get space for all unicast links + broadcast link */
  345. payload_size = TLV_SPACE((sizeof(link_info)) * (tipc_num_links + 1));
  346. if (payload_size > 32768u) {
  347. spin_unlock_bh(&node_list_lock);
  348. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  349. " (too many links)");
  350. }
  351. spin_unlock_bh(&node_list_lock);
  352. buf = tipc_cfg_reply_alloc(payload_size);
  353. if (!buf)
  354. return NULL;
  355. /* Add TLV for broadcast link */
  356. link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr));
  357. link_info.up = htonl(1);
  358. strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  359. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
  360. /* Add TLVs for any other links in scope */
  361. rcu_read_lock();
  362. list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
  363. u32 i;
  364. if (!tipc_in_scope(domain, n_ptr->addr))
  365. continue;
  366. tipc_node_lock(n_ptr);
  367. for (i = 0; i < MAX_BEARERS; i++) {
  368. if (!n_ptr->links[i])
  369. continue;
  370. link_info.dest = htonl(n_ptr->addr);
  371. link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
  372. strcpy(link_info.str, n_ptr->links[i]->name);
  373. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
  374. &link_info, sizeof(link_info));
  375. }
  376. tipc_node_unlock(n_ptr);
  377. }
  378. rcu_read_unlock();
  379. return buf;
  380. }
  381. /**
  382. * tipc_node_get_linkname - get the name of a link
  383. *
  384. * @bearer_id: id of the bearer
  385. * @node: peer node address
  386. * @linkname: link name output buffer
  387. *
  388. * Returns 0 on success
  389. */
  390. int tipc_node_get_linkname(u32 bearer_id, u32 addr, char *linkname, size_t len)
  391. {
  392. struct tipc_link *link;
  393. struct tipc_node *node = tipc_node_find(addr);
  394. if ((bearer_id >= MAX_BEARERS) || !node)
  395. return -EINVAL;
  396. tipc_node_lock(node);
  397. link = node->links[bearer_id];
  398. if (link) {
  399. strncpy(linkname, link->name, len);
  400. tipc_node_unlock(node);
  401. return 0;
  402. }
  403. tipc_node_unlock(node);
  404. return -EINVAL;
  405. }
  406. void tipc_node_unlock(struct tipc_node *node)
  407. {
  408. LIST_HEAD(nsub_list);
  409. struct tipc_link *link;
  410. int pkt_sz = 0;
  411. u32 addr = 0;
  412. if (likely(!node->action_flags)) {
  413. spin_unlock_bh(&node->lock);
  414. return;
  415. }
  416. if (node->action_flags & TIPC_NOTIFY_NODE_DOWN) {
  417. list_replace_init(&node->nsub, &nsub_list);
  418. node->action_flags &= ~TIPC_NOTIFY_NODE_DOWN;
  419. }
  420. if (node->action_flags & TIPC_NOTIFY_NODE_UP) {
  421. link = node->active_links[0];
  422. node->action_flags &= ~TIPC_NOTIFY_NODE_UP;
  423. if (link) {
  424. pkt_sz = ((link->max_pkt - INT_H_SIZE) / ITEM_SIZE) *
  425. ITEM_SIZE;
  426. addr = node->addr;
  427. }
  428. }
  429. spin_unlock_bh(&node->lock);
  430. if (!list_empty(&nsub_list))
  431. tipc_nodesub_notify(&nsub_list);
  432. if (pkt_sz)
  433. tipc_named_node_up(pkt_sz, addr);
  434. }