node.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 "cluster.h"
  40. #include "net.h"
  41. #include "addr.h"
  42. #include "node_subscr.h"
  43. #include "link.h"
  44. #include "port.h"
  45. #include "bearer.h"
  46. #include "name_distr.h"
  47. void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str);
  48. static void node_lost_contact(struct tipc_node *n_ptr);
  49. static void node_established_contact(struct tipc_node *n_ptr);
  50. struct tipc_node *tipc_nodes = NULL; /* sorted list of nodes within cluster */
  51. static DEFINE_SPINLOCK(node_create_lock);
  52. u32 tipc_own_tag = 0;
  53. /**
  54. * tipc_node_create - create neighboring node
  55. *
  56. * Currently, this routine is called by neighbor discovery code, which holds
  57. * net_lock for reading only. We must take node_create_lock to ensure a node
  58. * isn't created twice if two different bearers discover the node at the same
  59. * time. (It would be preferable to switch to holding net_lock in write mode,
  60. * but this is a non-trivial change.)
  61. */
  62. struct tipc_node *tipc_node_create(u32 addr)
  63. {
  64. struct cluster *c_ptr;
  65. struct tipc_node *n_ptr;
  66. struct tipc_node **curr_node;
  67. spin_lock_bh(&node_create_lock);
  68. for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
  69. if (addr < n_ptr->addr)
  70. break;
  71. if (addr == n_ptr->addr) {
  72. spin_unlock_bh(&node_create_lock);
  73. return n_ptr;
  74. }
  75. }
  76. n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
  77. if (!n_ptr) {
  78. spin_unlock_bh(&node_create_lock);
  79. warn("Node creation failed, no memory\n");
  80. return NULL;
  81. }
  82. c_ptr = tipc_cltr_find(addr);
  83. if (!c_ptr) {
  84. c_ptr = tipc_cltr_create(addr);
  85. }
  86. if (!c_ptr) {
  87. spin_unlock_bh(&node_create_lock);
  88. kfree(n_ptr);
  89. return NULL;
  90. }
  91. n_ptr->addr = addr;
  92. spin_lock_init(&n_ptr->lock);
  93. INIT_LIST_HEAD(&n_ptr->nsub);
  94. n_ptr->owner = c_ptr;
  95. tipc_cltr_attach_node(c_ptr, n_ptr);
  96. n_ptr->last_router = -1;
  97. /* Insert node into ordered list */
  98. for (curr_node = &tipc_nodes; *curr_node;
  99. curr_node = &(*curr_node)->next) {
  100. if (addr < (*curr_node)->addr) {
  101. n_ptr->next = *curr_node;
  102. break;
  103. }
  104. }
  105. (*curr_node) = n_ptr;
  106. spin_unlock_bh(&node_create_lock);
  107. return n_ptr;
  108. }
  109. void tipc_node_delete(struct tipc_node *n_ptr)
  110. {
  111. if (!n_ptr)
  112. return;
  113. #if 0
  114. /* Not needed because links are already deleted via tipc_bearer_stop() */
  115. u32 l_num;
  116. for (l_num = 0; l_num < MAX_BEARERS; l_num++) {
  117. link_delete(n_ptr->links[l_num]);
  118. }
  119. #endif
  120. dbg("node %x deleted\n", n_ptr->addr);
  121. kfree(n_ptr);
  122. }
  123. /**
  124. * tipc_node_link_up - handle addition of link
  125. *
  126. * Link becomes active (alone or shared) or standby, depending on its priority.
  127. */
  128. void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
  129. {
  130. struct link **active = &n_ptr->active_links[0];
  131. n_ptr->working_links++;
  132. info("Established link <%s> on network plane %c\n",
  133. l_ptr->name, l_ptr->b_ptr->net_plane);
  134. if (!active[0]) {
  135. dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
  136. active[0] = active[1] = l_ptr;
  137. node_established_contact(n_ptr);
  138. return;
  139. }
  140. if (l_ptr->priority < active[0]->priority) {
  141. info("New link <%s> becomes standby\n", l_ptr->name);
  142. return;
  143. }
  144. tipc_link_send_duplicate(active[0], l_ptr);
  145. if (l_ptr->priority == active[0]->priority) {
  146. active[0] = l_ptr;
  147. return;
  148. }
  149. info("Old link <%s> becomes standby\n", active[0]->name);
  150. if (active[1] != active[0])
  151. info("Old link <%s> becomes standby\n", active[1]->name);
  152. active[0] = active[1] = l_ptr;
  153. }
  154. /**
  155. * node_select_active_links - select active link
  156. */
  157. static void node_select_active_links(struct tipc_node *n_ptr)
  158. {
  159. struct link **active = &n_ptr->active_links[0];
  160. u32 i;
  161. u32 highest_prio = 0;
  162. active[0] = active[1] = NULL;
  163. for (i = 0; i < MAX_BEARERS; i++) {
  164. struct link *l_ptr = n_ptr->links[i];
  165. if (!l_ptr || !tipc_link_is_up(l_ptr) ||
  166. (l_ptr->priority < highest_prio))
  167. continue;
  168. if (l_ptr->priority > highest_prio) {
  169. highest_prio = l_ptr->priority;
  170. active[0] = active[1] = l_ptr;
  171. } else {
  172. active[1] = l_ptr;
  173. }
  174. }
  175. }
  176. /**
  177. * tipc_node_link_down - handle loss of link
  178. */
  179. void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
  180. {
  181. struct link **active;
  182. n_ptr->working_links--;
  183. if (!tipc_link_is_active(l_ptr)) {
  184. info("Lost standby link <%s> on network plane %c\n",
  185. l_ptr->name, l_ptr->b_ptr->net_plane);
  186. return;
  187. }
  188. info("Lost link <%s> on network plane %c\n",
  189. l_ptr->name, l_ptr->b_ptr->net_plane);
  190. active = &n_ptr->active_links[0];
  191. if (active[0] == l_ptr)
  192. active[0] = active[1];
  193. if (active[1] == l_ptr)
  194. active[1] = active[0];
  195. if (active[0] == l_ptr)
  196. node_select_active_links(n_ptr);
  197. if (tipc_node_is_up(n_ptr))
  198. tipc_link_changeover(l_ptr);
  199. else
  200. node_lost_contact(n_ptr);
  201. }
  202. int tipc_node_has_active_links(struct tipc_node *n_ptr)
  203. {
  204. return (n_ptr &&
  205. ((n_ptr->active_links[0]) || (n_ptr->active_links[1])));
  206. }
  207. int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
  208. {
  209. return (n_ptr->working_links > 1);
  210. }
  211. static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
  212. {
  213. return (n_ptr && (n_ptr->last_router >= 0));
  214. }
  215. int tipc_node_is_up(struct tipc_node *n_ptr)
  216. {
  217. return (tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr));
  218. }
  219. struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
  220. {
  221. struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
  222. if (!n_ptr)
  223. n_ptr = tipc_node_create(l_ptr->addr);
  224. if (n_ptr) {
  225. u32 bearer_id = l_ptr->b_ptr->identity;
  226. char addr_string[16];
  227. if (n_ptr->link_cnt >= 2) {
  228. err("Attempt to create third link to %s\n",
  229. addr_string_fill(addr_string, n_ptr->addr));
  230. return NULL;
  231. }
  232. if (!n_ptr->links[bearer_id]) {
  233. n_ptr->links[bearer_id] = l_ptr;
  234. tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
  235. n_ptr->link_cnt++;
  236. return n_ptr;
  237. }
  238. err("Attempt to establish second link on <%s> to %s \n",
  239. l_ptr->b_ptr->publ.name,
  240. addr_string_fill(addr_string, l_ptr->addr));
  241. }
  242. return NULL;
  243. }
  244. void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
  245. {
  246. n_ptr->links[l_ptr->b_ptr->identity] = NULL;
  247. tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
  248. n_ptr->link_cnt--;
  249. }
  250. /*
  251. * Routing table management - five cases to handle:
  252. *
  253. * 1: A link towards a zone/cluster external node comes up.
  254. * => Send a multicast message updating routing tables of all
  255. * system nodes within own cluster that the new destination
  256. * can be reached via this node.
  257. * (node.establishedContact()=>cluster.multicastNewRoute())
  258. *
  259. * 2: A link towards a slave node comes up.
  260. * => Send a multicast message updating routing tables of all
  261. * system nodes within own cluster that the new destination
  262. * can be reached via this node.
  263. * (node.establishedContact()=>cluster.multicastNewRoute())
  264. * => Send a message to the slave node about existence
  265. * of all system nodes within cluster:
  266. * (node.establishedContact()=>cluster.sendLocalRoutes())
  267. *
  268. * 3: A new cluster local system node becomes available.
  269. * => Send message(s) to this particular node containing
  270. * information about all cluster external and slave
  271. * nodes which can be reached via this node.
  272. * (node.establishedContact()==>network.sendExternalRoutes())
  273. * (node.establishedContact()==>network.sendSlaveRoutes())
  274. * => Send messages to all directly connected slave nodes
  275. * containing information about the existence of the new node
  276. * (node.establishedContact()=>cluster.multicastNewRoute())
  277. *
  278. * 4: The link towards a zone/cluster external node or slave
  279. * node goes down.
  280. * => Send a multcast message updating routing tables of all
  281. * nodes within cluster that the new destination can not any
  282. * longer be reached via this node.
  283. * (node.lostAllLinks()=>cluster.bcastLostRoute())
  284. *
  285. * 5: A cluster local system node becomes unavailable.
  286. * => Remove all references to this node from the local
  287. * routing tables. Note: This is a completely node
  288. * local operation.
  289. * (node.lostAllLinks()=>network.removeAsRouter())
  290. * => Send messages to all directly connected slave nodes
  291. * containing information about loss of the node
  292. * (node.establishedContact()=>cluster.multicastLostRoute())
  293. *
  294. */
  295. static void node_established_contact(struct tipc_node *n_ptr)
  296. {
  297. struct cluster *c_ptr;
  298. dbg("node_established_contact:-> %x\n", n_ptr->addr);
  299. if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
  300. tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
  301. }
  302. /* Syncronize broadcast acks */
  303. n_ptr->bclink.acked = tipc_bclink_get_last_sent();
  304. if (is_slave(tipc_own_addr))
  305. return;
  306. if (!in_own_cluster(n_ptr->addr)) {
  307. /* Usage case 1 (see above) */
  308. c_ptr = tipc_cltr_find(tipc_own_addr);
  309. if (!c_ptr)
  310. c_ptr = tipc_cltr_create(tipc_own_addr);
  311. if (c_ptr)
  312. tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
  313. tipc_max_nodes);
  314. return;
  315. }
  316. c_ptr = n_ptr->owner;
  317. if (is_slave(n_ptr->addr)) {
  318. /* Usage case 2 (see above) */
  319. tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
  320. tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
  321. return;
  322. }
  323. if (n_ptr->bclink.supported) {
  324. tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
  325. if (n_ptr->addr < tipc_own_addr)
  326. tipc_own_tag++;
  327. }
  328. /* Case 3 (see above) */
  329. tipc_net_send_external_routes(n_ptr->addr);
  330. tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
  331. tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
  332. tipc_highest_allowed_slave);
  333. }
  334. static void node_lost_contact(struct tipc_node *n_ptr)
  335. {
  336. struct cluster *c_ptr;
  337. struct tipc_node_subscr *ns, *tns;
  338. char addr_string[16];
  339. u32 i;
  340. /* Clean up broadcast reception remains */
  341. n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
  342. while (n_ptr->bclink.deferred_head) {
  343. struct sk_buff* buf = n_ptr->bclink.deferred_head;
  344. n_ptr->bclink.deferred_head = buf->next;
  345. buf_discard(buf);
  346. }
  347. if (n_ptr->bclink.defragm) {
  348. buf_discard(n_ptr->bclink.defragm);
  349. n_ptr->bclink.defragm = NULL;
  350. }
  351. if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
  352. tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
  353. }
  354. /* Update routing tables */
  355. if (is_slave(tipc_own_addr)) {
  356. tipc_net_remove_as_router(n_ptr->addr);
  357. } else {
  358. if (!in_own_cluster(n_ptr->addr)) {
  359. /* Case 4 (see above) */
  360. c_ptr = tipc_cltr_find(tipc_own_addr);
  361. tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
  362. tipc_max_nodes);
  363. } else {
  364. /* Case 5 (see above) */
  365. c_ptr = tipc_cltr_find(n_ptr->addr);
  366. if (is_slave(n_ptr->addr)) {
  367. tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
  368. tipc_max_nodes);
  369. } else {
  370. if (n_ptr->bclink.supported) {
  371. tipc_nmap_remove(&tipc_cltr_bcast_nodes,
  372. n_ptr->addr);
  373. if (n_ptr->addr < tipc_own_addr)
  374. tipc_own_tag--;
  375. }
  376. tipc_net_remove_as_router(n_ptr->addr);
  377. tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
  378. LOWEST_SLAVE,
  379. tipc_highest_allowed_slave);
  380. }
  381. }
  382. }
  383. if (tipc_node_has_active_routes(n_ptr))
  384. return;
  385. info("Lost contact with %s\n",
  386. addr_string_fill(addr_string, n_ptr->addr));
  387. /* Abort link changeover */
  388. for (i = 0; i < MAX_BEARERS; i++) {
  389. struct link *l_ptr = n_ptr->links[i];
  390. if (!l_ptr)
  391. continue;
  392. l_ptr->reset_checkpoint = l_ptr->next_in_no;
  393. l_ptr->exp_msg_count = 0;
  394. tipc_link_reset_fragments(l_ptr);
  395. }
  396. /* Notify subscribers */
  397. list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
  398. ns->node = NULL;
  399. list_del_init(&ns->nodesub_list);
  400. tipc_k_signal((Handler)ns->handle_node_down,
  401. (unsigned long)ns->usr_handle);
  402. }
  403. }
  404. /**
  405. * tipc_node_select_next_hop - find the next-hop node for a message
  406. *
  407. * Called by when cluster local lookup has failed.
  408. */
  409. struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
  410. {
  411. struct tipc_node *n_ptr;
  412. u32 router_addr;
  413. if (!tipc_addr_domain_valid(addr))
  414. return NULL;
  415. /* Look for direct link to destination processsor */
  416. n_ptr = tipc_node_find(addr);
  417. if (n_ptr && tipc_node_has_active_links(n_ptr))
  418. return n_ptr;
  419. /* Cluster local system nodes *must* have direct links */
  420. if (!is_slave(addr) && in_own_cluster(addr))
  421. return NULL;
  422. /* Look for cluster local router with direct link to node */
  423. router_addr = tipc_node_select_router(n_ptr, selector);
  424. if (router_addr)
  425. return tipc_node_select(router_addr, selector);
  426. /* Slave nodes can only be accessed within own cluster via a
  427. known router with direct link -- if no router was found,give up */
  428. if (is_slave(addr))
  429. return NULL;
  430. /* Inter zone/cluster -- find any direct link to remote cluster */
  431. addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
  432. n_ptr = tipc_net_select_remote_node(addr, selector);
  433. if (n_ptr && tipc_node_has_active_links(n_ptr))
  434. return n_ptr;
  435. /* Last resort -- look for any router to anywhere in remote zone */
  436. router_addr = tipc_net_select_router(addr, selector);
  437. if (router_addr)
  438. return tipc_node_select(router_addr, selector);
  439. return NULL;
  440. }
  441. /**
  442. * tipc_node_select_router - select router to reach specified node
  443. *
  444. * Uses a deterministic and fair algorithm for selecting router node.
  445. */
  446. u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
  447. {
  448. u32 ulim;
  449. u32 mask;
  450. u32 start;
  451. u32 r;
  452. if (!n_ptr)
  453. return 0;
  454. if (n_ptr->last_router < 0)
  455. return 0;
  456. ulim = ((n_ptr->last_router + 1) * 32) - 1;
  457. /* Start entry must be random */
  458. mask = tipc_max_nodes;
  459. while (mask > ulim)
  460. mask >>= 1;
  461. start = ref & mask;
  462. r = start;
  463. /* Lookup upwards with wrap-around */
  464. do {
  465. if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
  466. break;
  467. } while (++r <= ulim);
  468. if (r > ulim) {
  469. r = 1;
  470. do {
  471. if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
  472. break;
  473. } while (++r < start);
  474. assert(r != start);
  475. }
  476. assert(r && (r <= ulim));
  477. return tipc_addr(own_zone(), own_cluster(), r);
  478. }
  479. void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
  480. {
  481. u32 r_num = tipc_node(router);
  482. n_ptr->routers[r_num / 32] =
  483. ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
  484. n_ptr->last_router = tipc_max_nodes / 32;
  485. while ((--n_ptr->last_router >= 0) &&
  486. !n_ptr->routers[n_ptr->last_router]);
  487. }
  488. void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
  489. {
  490. u32 r_num = tipc_node(router);
  491. if (n_ptr->last_router < 0)
  492. return; /* No routes */
  493. n_ptr->routers[r_num / 32] =
  494. ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
  495. n_ptr->last_router = tipc_max_nodes / 32;
  496. while ((--n_ptr->last_router >= 0) &&
  497. !n_ptr->routers[n_ptr->last_router]);
  498. if (!tipc_node_is_up(n_ptr))
  499. node_lost_contact(n_ptr);
  500. }
  501. #if 0
  502. void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str)
  503. {
  504. u32 i;
  505. tipc_printf(buf, "\n\n%s", str);
  506. for (i = 0; i < MAX_BEARERS; i++) {
  507. if (!n_ptr->links[i])
  508. continue;
  509. tipc_printf(buf, "Links[%u]: %x, ", i, n_ptr->links[i]);
  510. }
  511. tipc_printf(buf, "Active links: [%x,%x]\n",
  512. n_ptr->active_links[0], n_ptr->active_links[1]);
  513. }
  514. #endif
  515. u32 tipc_available_nodes(const u32 domain)
  516. {
  517. struct tipc_node *n_ptr;
  518. u32 cnt = 0;
  519. read_lock_bh(&tipc_net_lock);
  520. for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
  521. if (!in_scope(domain, n_ptr->addr))
  522. continue;
  523. if (tipc_node_is_up(n_ptr))
  524. cnt++;
  525. }
  526. read_unlock_bh(&tipc_net_lock);
  527. return cnt;
  528. }
  529. struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
  530. {
  531. u32 domain;
  532. struct sk_buff *buf;
  533. struct tipc_node *n_ptr;
  534. struct tipc_node_info node_info;
  535. u32 payload_size;
  536. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  537. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  538. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  539. if (!tipc_addr_domain_valid(domain))
  540. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  541. " (network address)");
  542. read_lock_bh(&tipc_net_lock);
  543. if (!tipc_nodes) {
  544. read_unlock_bh(&tipc_net_lock);
  545. return tipc_cfg_reply_none();
  546. }
  547. /* For now, get space for all other nodes
  548. (will need to modify this when slave nodes are supported */
  549. payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
  550. if (payload_size > 32768u) {
  551. read_unlock_bh(&tipc_net_lock);
  552. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  553. " (too many nodes)");
  554. }
  555. buf = tipc_cfg_reply_alloc(payload_size);
  556. if (!buf) {
  557. read_unlock_bh(&tipc_net_lock);
  558. return NULL;
  559. }
  560. /* Add TLVs for all nodes in scope */
  561. for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
  562. if (!in_scope(domain, n_ptr->addr))
  563. continue;
  564. node_info.addr = htonl(n_ptr->addr);
  565. node_info.up = htonl(tipc_node_is_up(n_ptr));
  566. tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
  567. &node_info, sizeof(node_info));
  568. }
  569. read_unlock_bh(&tipc_net_lock);
  570. return buf;
  571. }
  572. struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
  573. {
  574. u32 domain;
  575. struct sk_buff *buf;
  576. struct tipc_node *n_ptr;
  577. struct tipc_link_info link_info;
  578. u32 payload_size;
  579. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  580. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  581. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  582. if (!tipc_addr_domain_valid(domain))
  583. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  584. " (network address)");
  585. if (tipc_mode != TIPC_NET_MODE)
  586. return tipc_cfg_reply_none();
  587. read_lock_bh(&tipc_net_lock);
  588. /* Get space for all unicast links + multicast link */
  589. payload_size = TLV_SPACE(sizeof(link_info)) *
  590. (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
  591. if (payload_size > 32768u) {
  592. read_unlock_bh(&tipc_net_lock);
  593. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  594. " (too many links)");
  595. }
  596. buf = tipc_cfg_reply_alloc(payload_size);
  597. if (!buf) {
  598. read_unlock_bh(&tipc_net_lock);
  599. return NULL;
  600. }
  601. /* Add TLV for broadcast link */
  602. link_info.dest = htonl(tipc_own_addr & 0xfffff00);
  603. link_info.up = htonl(1);
  604. strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  605. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
  606. /* Add TLVs for any other links in scope */
  607. for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
  608. u32 i;
  609. if (!in_scope(domain, n_ptr->addr))
  610. continue;
  611. tipc_node_lock(n_ptr);
  612. for (i = 0; i < MAX_BEARERS; i++) {
  613. if (!n_ptr->links[i])
  614. continue;
  615. link_info.dest = htonl(n_ptr->addr);
  616. link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
  617. strcpy(link_info.str, n_ptr->links[i]->name);
  618. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
  619. &link_info, sizeof(link_info));
  620. }
  621. tipc_node_unlock(n_ptr);
  622. }
  623. read_unlock_bh(&tipc_net_lock);
  624. return buf;
  625. }