node.c 14 KB

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