node.c 20 KB

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