cluster.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * net/tipc/cluster.c: TIPC cluster management routines
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2005, 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 "cluster.h"
  38. #include "addr.h"
  39. #include "node_subscr.h"
  40. #include "link.h"
  41. #include "node.h"
  42. #include "net.h"
  43. #include "msg.h"
  44. #include "bearer.h"
  45. static void tipc_cltr_multicast(struct cluster *c_ptr, struct sk_buff *buf,
  46. u32 lower, u32 upper);
  47. static struct sk_buff *tipc_cltr_prepare_routing_msg(u32 data_size, u32 dest);
  48. struct tipc_node **tipc_local_nodes = NULL;
  49. struct tipc_node_map tipc_cltr_bcast_nodes = {0,{0,}};
  50. u32 tipc_highest_allowed_slave = 0;
  51. struct cluster *tipc_cltr_create(u32 addr)
  52. {
  53. struct _zone *z_ptr;
  54. struct cluster *c_ptr;
  55. int max_nodes;
  56. c_ptr = kzalloc(sizeof(*c_ptr), GFP_ATOMIC);
  57. if (c_ptr == NULL) {
  58. warn("Cluster creation failure, no memory\n");
  59. return NULL;
  60. }
  61. c_ptr->addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
  62. if (in_own_cluster(addr))
  63. max_nodes = LOWEST_SLAVE + tipc_max_slaves;
  64. else
  65. max_nodes = tipc_max_nodes + 1;
  66. c_ptr->nodes = kcalloc(max_nodes + 1, sizeof(void*), GFP_ATOMIC);
  67. if (c_ptr->nodes == NULL) {
  68. warn("Cluster creation failure, no memory for node area\n");
  69. kfree(c_ptr);
  70. return NULL;
  71. }
  72. if (in_own_cluster(addr))
  73. tipc_local_nodes = c_ptr->nodes;
  74. c_ptr->highest_slave = LOWEST_SLAVE - 1;
  75. c_ptr->highest_node = 0;
  76. z_ptr = tipc_zone_find(tipc_zone(addr));
  77. if (!z_ptr) {
  78. z_ptr = tipc_zone_create(addr);
  79. }
  80. if (!z_ptr) {
  81. kfree(c_ptr->nodes);
  82. kfree(c_ptr);
  83. return NULL;
  84. }
  85. tipc_zone_attach_cluster(z_ptr, c_ptr);
  86. c_ptr->owner = z_ptr;
  87. return c_ptr;
  88. }
  89. void tipc_cltr_delete(struct cluster *c_ptr)
  90. {
  91. u32 n_num;
  92. if (!c_ptr)
  93. return;
  94. for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
  95. tipc_node_delete(c_ptr->nodes[n_num]);
  96. }
  97. for (n_num = LOWEST_SLAVE; n_num <= c_ptr->highest_slave; n_num++) {
  98. tipc_node_delete(c_ptr->nodes[n_num]);
  99. }
  100. kfree(c_ptr->nodes);
  101. kfree(c_ptr);
  102. }
  103. u32 tipc_cltr_next_node(struct cluster *c_ptr, u32 addr)
  104. {
  105. struct tipc_node *n_ptr;
  106. u32 n_num = tipc_node(addr) + 1;
  107. if (!c_ptr)
  108. return addr;
  109. for (; n_num <= c_ptr->highest_node; n_num++) {
  110. n_ptr = c_ptr->nodes[n_num];
  111. if (n_ptr && tipc_node_has_active_links(n_ptr))
  112. return n_ptr->addr;
  113. }
  114. for (n_num = 1; n_num < tipc_node(addr); n_num++) {
  115. n_ptr = c_ptr->nodes[n_num];
  116. if (n_ptr && tipc_node_has_active_links(n_ptr))
  117. return n_ptr->addr;
  118. }
  119. return 0;
  120. }
  121. void tipc_cltr_attach_node(struct cluster *c_ptr, struct tipc_node *n_ptr)
  122. {
  123. u32 n_num = tipc_node(n_ptr->addr);
  124. u32 max_n_num = tipc_max_nodes;
  125. if (in_own_cluster(n_ptr->addr))
  126. max_n_num = tipc_highest_allowed_slave;
  127. assert(n_num > 0);
  128. assert(n_num <= max_n_num);
  129. assert(c_ptr->nodes[n_num] == NULL);
  130. c_ptr->nodes[n_num] = n_ptr;
  131. if (n_num > c_ptr->highest_node)
  132. c_ptr->highest_node = n_num;
  133. }
  134. /**
  135. * tipc_cltr_select_router - select router to a cluster
  136. *
  137. * Uses deterministic and fair algorithm.
  138. */
  139. u32 tipc_cltr_select_router(struct cluster *c_ptr, u32 ref)
  140. {
  141. u32 n_num;
  142. u32 ulim = c_ptr->highest_node;
  143. u32 mask;
  144. u32 tstart;
  145. assert(!in_own_cluster(c_ptr->addr));
  146. if (!ulim)
  147. return 0;
  148. /* Start entry must be random */
  149. mask = tipc_max_nodes;
  150. while (mask > ulim)
  151. mask >>= 1;
  152. tstart = ref & mask;
  153. n_num = tstart;
  154. /* Lookup upwards with wrap-around */
  155. do {
  156. if (tipc_node_is_up(c_ptr->nodes[n_num]))
  157. break;
  158. } while (++n_num <= ulim);
  159. if (n_num > ulim) {
  160. n_num = 1;
  161. do {
  162. if (tipc_node_is_up(c_ptr->nodes[n_num]))
  163. break;
  164. } while (++n_num < tstart);
  165. if (n_num == tstart)
  166. return 0;
  167. }
  168. assert(n_num <= ulim);
  169. return tipc_node_select_router(c_ptr->nodes[n_num], ref);
  170. }
  171. /**
  172. * tipc_cltr_select_node - select destination node within a remote cluster
  173. *
  174. * Uses deterministic and fair algorithm.
  175. */
  176. struct tipc_node *tipc_cltr_select_node(struct cluster *c_ptr, u32 selector)
  177. {
  178. u32 n_num;
  179. u32 mask = tipc_max_nodes;
  180. u32 start_entry;
  181. assert(!in_own_cluster(c_ptr->addr));
  182. if (!c_ptr->highest_node)
  183. return NULL;
  184. /* Start entry must be random */
  185. while (mask > c_ptr->highest_node) {
  186. mask >>= 1;
  187. }
  188. start_entry = (selector & mask) ? selector & mask : 1u;
  189. assert(start_entry <= c_ptr->highest_node);
  190. /* Lookup upwards with wrap-around */
  191. for (n_num = start_entry; n_num <= c_ptr->highest_node; n_num++) {
  192. if (tipc_node_has_active_links(c_ptr->nodes[n_num]))
  193. return c_ptr->nodes[n_num];
  194. }
  195. for (n_num = 1; n_num < start_entry; n_num++) {
  196. if (tipc_node_has_active_links(c_ptr->nodes[n_num]))
  197. return c_ptr->nodes[n_num];
  198. }
  199. return NULL;
  200. }
  201. /*
  202. * Routing table management: See description in node.c
  203. */
  204. static struct sk_buff *tipc_cltr_prepare_routing_msg(u32 data_size, u32 dest)
  205. {
  206. u32 size = INT_H_SIZE + data_size;
  207. struct sk_buff *buf = buf_acquire(size);
  208. struct tipc_msg *msg;
  209. if (buf) {
  210. msg = buf_msg(buf);
  211. memset((char *)msg, 0, size);
  212. msg_init(msg, ROUTE_DISTRIBUTOR, 0, INT_H_SIZE, dest);
  213. }
  214. return buf;
  215. }
  216. void tipc_cltr_bcast_new_route(struct cluster *c_ptr, u32 dest,
  217. u32 lower, u32 upper)
  218. {
  219. struct sk_buff *buf = tipc_cltr_prepare_routing_msg(0, c_ptr->addr);
  220. struct tipc_msg *msg;
  221. if (buf) {
  222. msg = buf_msg(buf);
  223. msg_set_remote_node(msg, dest);
  224. msg_set_type(msg, ROUTE_ADDITION);
  225. tipc_cltr_multicast(c_ptr, buf, lower, upper);
  226. } else {
  227. warn("Memory squeeze: broadcast of new route failed\n");
  228. }
  229. }
  230. void tipc_cltr_bcast_lost_route(struct cluster *c_ptr, u32 dest,
  231. u32 lower, u32 upper)
  232. {
  233. struct sk_buff *buf = tipc_cltr_prepare_routing_msg(0, c_ptr->addr);
  234. struct tipc_msg *msg;
  235. if (buf) {
  236. msg = buf_msg(buf);
  237. msg_set_remote_node(msg, dest);
  238. msg_set_type(msg, ROUTE_REMOVAL);
  239. tipc_cltr_multicast(c_ptr, buf, lower, upper);
  240. } else {
  241. warn("Memory squeeze: broadcast of lost route failed\n");
  242. }
  243. }
  244. void tipc_cltr_send_slave_routes(struct cluster *c_ptr, u32 dest)
  245. {
  246. struct sk_buff *buf;
  247. struct tipc_msg *msg;
  248. u32 highest = c_ptr->highest_slave;
  249. u32 n_num;
  250. int send = 0;
  251. assert(!is_slave(dest));
  252. assert(in_own_cluster(dest));
  253. assert(in_own_cluster(c_ptr->addr));
  254. if (highest <= LOWEST_SLAVE)
  255. return;
  256. buf = tipc_cltr_prepare_routing_msg(highest - LOWEST_SLAVE + 1,
  257. c_ptr->addr);
  258. if (buf) {
  259. msg = buf_msg(buf);
  260. msg_set_remote_node(msg, c_ptr->addr);
  261. msg_set_type(msg, SLAVE_ROUTING_TABLE);
  262. for (n_num = LOWEST_SLAVE; n_num <= highest; n_num++) {
  263. if (c_ptr->nodes[n_num] &&
  264. tipc_node_has_active_links(c_ptr->nodes[n_num])) {
  265. send = 1;
  266. msg_set_dataoctet(msg, n_num);
  267. }
  268. }
  269. if (send)
  270. tipc_link_send(buf, dest, dest);
  271. else
  272. buf_discard(buf);
  273. } else {
  274. warn("Memory squeeze: broadcast of lost route failed\n");
  275. }
  276. }
  277. void tipc_cltr_send_ext_routes(struct cluster *c_ptr, u32 dest)
  278. {
  279. struct sk_buff *buf;
  280. struct tipc_msg *msg;
  281. u32 highest = c_ptr->highest_node;
  282. u32 n_num;
  283. int send = 0;
  284. if (in_own_cluster(c_ptr->addr))
  285. return;
  286. assert(!is_slave(dest));
  287. assert(in_own_cluster(dest));
  288. highest = c_ptr->highest_node;
  289. buf = tipc_cltr_prepare_routing_msg(highest + 1, c_ptr->addr);
  290. if (buf) {
  291. msg = buf_msg(buf);
  292. msg_set_remote_node(msg, c_ptr->addr);
  293. msg_set_type(msg, EXT_ROUTING_TABLE);
  294. for (n_num = 1; n_num <= highest; n_num++) {
  295. if (c_ptr->nodes[n_num] &&
  296. tipc_node_has_active_links(c_ptr->nodes[n_num])) {
  297. send = 1;
  298. msg_set_dataoctet(msg, n_num);
  299. }
  300. }
  301. if (send)
  302. tipc_link_send(buf, dest, dest);
  303. else
  304. buf_discard(buf);
  305. } else {
  306. warn("Memory squeeze: broadcast of external route failed\n");
  307. }
  308. }
  309. void tipc_cltr_send_local_routes(struct cluster *c_ptr, u32 dest)
  310. {
  311. struct sk_buff *buf;
  312. struct tipc_msg *msg;
  313. u32 highest = c_ptr->highest_node;
  314. u32 n_num;
  315. int send = 0;
  316. assert(is_slave(dest));
  317. assert(in_own_cluster(c_ptr->addr));
  318. buf = tipc_cltr_prepare_routing_msg(highest, c_ptr->addr);
  319. if (buf) {
  320. msg = buf_msg(buf);
  321. msg_set_remote_node(msg, c_ptr->addr);
  322. msg_set_type(msg, LOCAL_ROUTING_TABLE);
  323. for (n_num = 1; n_num <= highest; n_num++) {
  324. if (c_ptr->nodes[n_num] &&
  325. tipc_node_has_active_links(c_ptr->nodes[n_num])) {
  326. send = 1;
  327. msg_set_dataoctet(msg, n_num);
  328. }
  329. }
  330. if (send)
  331. tipc_link_send(buf, dest, dest);
  332. else
  333. buf_discard(buf);
  334. } else {
  335. warn("Memory squeeze: broadcast of local route failed\n");
  336. }
  337. }
  338. void tipc_cltr_recv_routing_table(struct sk_buff *buf)
  339. {
  340. struct tipc_msg *msg = buf_msg(buf);
  341. struct cluster *c_ptr;
  342. struct tipc_node *n_ptr;
  343. unchar *node_table;
  344. u32 table_size;
  345. u32 router;
  346. u32 rem_node = msg_remote_node(msg);
  347. u32 z_num;
  348. u32 c_num;
  349. u32 n_num;
  350. c_ptr = tipc_cltr_find(rem_node);
  351. if (!c_ptr) {
  352. c_ptr = tipc_cltr_create(rem_node);
  353. if (!c_ptr) {
  354. buf_discard(buf);
  355. return;
  356. }
  357. }
  358. node_table = buf->data + msg_hdr_sz(msg);
  359. table_size = msg_size(msg) - msg_hdr_sz(msg);
  360. router = msg_prevnode(msg);
  361. z_num = tipc_zone(rem_node);
  362. c_num = tipc_cluster(rem_node);
  363. switch (msg_type(msg)) {
  364. case LOCAL_ROUTING_TABLE:
  365. assert(is_slave(tipc_own_addr));
  366. case EXT_ROUTING_TABLE:
  367. for (n_num = 1; n_num < table_size; n_num++) {
  368. if (node_table[n_num]) {
  369. u32 addr = tipc_addr(z_num, c_num, n_num);
  370. n_ptr = c_ptr->nodes[n_num];
  371. if (!n_ptr) {
  372. n_ptr = tipc_node_create(addr);
  373. }
  374. if (n_ptr)
  375. tipc_node_add_router(n_ptr, router);
  376. }
  377. }
  378. break;
  379. case SLAVE_ROUTING_TABLE:
  380. assert(!is_slave(tipc_own_addr));
  381. assert(in_own_cluster(c_ptr->addr));
  382. for (n_num = 1; n_num < table_size; n_num++) {
  383. if (node_table[n_num]) {
  384. u32 slave_num = n_num + LOWEST_SLAVE;
  385. u32 addr = tipc_addr(z_num, c_num, slave_num);
  386. n_ptr = c_ptr->nodes[slave_num];
  387. if (!n_ptr) {
  388. n_ptr = tipc_node_create(addr);
  389. }
  390. if (n_ptr)
  391. tipc_node_add_router(n_ptr, router);
  392. }
  393. }
  394. break;
  395. case ROUTE_ADDITION:
  396. if (!is_slave(tipc_own_addr)) {
  397. assert(!in_own_cluster(c_ptr->addr)
  398. || is_slave(rem_node));
  399. } else {
  400. assert(in_own_cluster(c_ptr->addr)
  401. && !is_slave(rem_node));
  402. }
  403. n_ptr = c_ptr->nodes[tipc_node(rem_node)];
  404. if (!n_ptr)
  405. n_ptr = tipc_node_create(rem_node);
  406. if (n_ptr)
  407. tipc_node_add_router(n_ptr, router);
  408. break;
  409. case ROUTE_REMOVAL:
  410. if (!is_slave(tipc_own_addr)) {
  411. assert(!in_own_cluster(c_ptr->addr)
  412. || is_slave(rem_node));
  413. } else {
  414. assert(in_own_cluster(c_ptr->addr)
  415. && !is_slave(rem_node));
  416. }
  417. n_ptr = c_ptr->nodes[tipc_node(rem_node)];
  418. if (n_ptr)
  419. tipc_node_remove_router(n_ptr, router);
  420. break;
  421. default:
  422. assert(!"Illegal routing manager message received\n");
  423. }
  424. buf_discard(buf);
  425. }
  426. void tipc_cltr_remove_as_router(struct cluster *c_ptr, u32 router)
  427. {
  428. u32 start_entry;
  429. u32 tstop;
  430. u32 n_num;
  431. if (is_slave(router))
  432. return; /* Slave nodes can not be routers */
  433. if (in_own_cluster(c_ptr->addr)) {
  434. start_entry = LOWEST_SLAVE;
  435. tstop = c_ptr->highest_slave;
  436. } else {
  437. start_entry = 1;
  438. tstop = c_ptr->highest_node;
  439. }
  440. for (n_num = start_entry; n_num <= tstop; n_num++) {
  441. if (c_ptr->nodes[n_num]) {
  442. tipc_node_remove_router(c_ptr->nodes[n_num], router);
  443. }
  444. }
  445. }
  446. /**
  447. * tipc_cltr_multicast - multicast message to local nodes
  448. */
  449. static void tipc_cltr_multicast(struct cluster *c_ptr, struct sk_buff *buf,
  450. u32 lower, u32 upper)
  451. {
  452. struct sk_buff *buf_copy;
  453. struct tipc_node *n_ptr;
  454. u32 n_num;
  455. u32 tstop;
  456. assert(lower <= upper);
  457. assert(((lower >= 1) && (lower <= tipc_max_nodes)) ||
  458. ((lower >= LOWEST_SLAVE) && (lower <= tipc_highest_allowed_slave)));
  459. assert(((upper >= 1) && (upper <= tipc_max_nodes)) ||
  460. ((upper >= LOWEST_SLAVE) && (upper <= tipc_highest_allowed_slave)));
  461. assert(in_own_cluster(c_ptr->addr));
  462. tstop = is_slave(upper) ? c_ptr->highest_slave : c_ptr->highest_node;
  463. if (tstop > upper)
  464. tstop = upper;
  465. for (n_num = lower; n_num <= tstop; n_num++) {
  466. n_ptr = c_ptr->nodes[n_num];
  467. if (n_ptr && tipc_node_has_active_links(n_ptr)) {
  468. buf_copy = skb_copy(buf, GFP_ATOMIC);
  469. if (buf_copy == NULL)
  470. break;
  471. msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
  472. tipc_link_send(buf_copy, n_ptr->addr, n_ptr->addr);
  473. }
  474. }
  475. buf_discard(buf);
  476. }
  477. /**
  478. * tipc_cltr_broadcast - broadcast message to all nodes within cluster
  479. */
  480. void tipc_cltr_broadcast(struct sk_buff *buf)
  481. {
  482. struct sk_buff *buf_copy;
  483. struct cluster *c_ptr;
  484. struct tipc_node *n_ptr;
  485. u32 n_num;
  486. u32 tstart;
  487. u32 tstop;
  488. u32 node_type;
  489. if (tipc_mode == TIPC_NET_MODE) {
  490. c_ptr = tipc_cltr_find(tipc_own_addr);
  491. assert(in_own_cluster(c_ptr->addr)); /* For now */
  492. /* Send to standard nodes, then repeat loop sending to slaves */
  493. tstart = 1;
  494. tstop = c_ptr->highest_node;
  495. for (node_type = 1; node_type <= 2; node_type++) {
  496. for (n_num = tstart; n_num <= tstop; n_num++) {
  497. n_ptr = c_ptr->nodes[n_num];
  498. if (n_ptr && tipc_node_has_active_links(n_ptr)) {
  499. buf_copy = skb_copy(buf, GFP_ATOMIC);
  500. if (buf_copy == NULL)
  501. goto exit;
  502. msg_set_destnode(buf_msg(buf_copy),
  503. n_ptr->addr);
  504. tipc_link_send(buf_copy, n_ptr->addr,
  505. n_ptr->addr);
  506. }
  507. }
  508. tstart = LOWEST_SLAVE;
  509. tstop = c_ptr->highest_slave;
  510. }
  511. }
  512. exit:
  513. buf_discard(buf);
  514. }
  515. int tipc_cltr_init(void)
  516. {
  517. tipc_highest_allowed_slave = LOWEST_SLAVE + tipc_max_slaves;
  518. return tipc_cltr_create(tipc_own_addr) ? 0 : -ENOMEM;
  519. }