node.c 14 KB

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