cluster.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * net/tipc/cluster.c: TIPC cluster management routines
  3. *
  4. * Copyright (c) 2003-2005, Ericsson Research Canada
  5. * Copyright (c) 2005, Wind River Systems
  6. * Copyright (c) 2005-2006, Ericsson AB
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "core.h"
  34. #include "cluster.h"
  35. #include "addr.h"
  36. #include "node_subscr.h"
  37. #include "link.h"
  38. #include "node.h"
  39. #include "net.h"
  40. #include "msg.h"
  41. #include "bearer.h"
  42. void cluster_multicast(struct cluster *c_ptr, struct sk_buff *buf,
  43. u32 lower, u32 upper);
  44. struct sk_buff *cluster_prepare_routing_msg(u32 data_size, u32 dest);
  45. struct node **local_nodes = 0;
  46. struct node_map cluster_bcast_nodes = {0,{0,}};
  47. u32 highest_allowed_slave = 0;
  48. struct cluster *cluster_create(u32 addr)
  49. {
  50. struct _zone *z_ptr;
  51. struct cluster *c_ptr;
  52. int max_nodes;
  53. int alloc;
  54. c_ptr = (struct cluster *)kmalloc(sizeof(*c_ptr), GFP_ATOMIC);
  55. if (c_ptr == NULL)
  56. return 0;
  57. memset(c_ptr, 0, sizeof(*c_ptr));
  58. c_ptr->addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
  59. if (in_own_cluster(addr))
  60. max_nodes = LOWEST_SLAVE + tipc_max_slaves;
  61. else
  62. max_nodes = tipc_max_nodes + 1;
  63. alloc = sizeof(void *) * (max_nodes + 1);
  64. c_ptr->nodes = (struct node **)kmalloc(alloc, GFP_ATOMIC);
  65. if (c_ptr->nodes == NULL) {
  66. kfree(c_ptr);
  67. return 0;
  68. }
  69. memset(c_ptr->nodes, 0, alloc);
  70. if (in_own_cluster(addr))
  71. local_nodes = c_ptr->nodes;
  72. c_ptr->highest_slave = LOWEST_SLAVE - 1;
  73. c_ptr->highest_node = 0;
  74. z_ptr = zone_find(tipc_zone(addr));
  75. if (z_ptr == NULL) {
  76. z_ptr = zone_create(addr);
  77. }
  78. if (z_ptr != NULL) {
  79. zone_attach_cluster(z_ptr, c_ptr);
  80. c_ptr->owner = z_ptr;
  81. }
  82. else {
  83. kfree(c_ptr);
  84. c_ptr = 0;
  85. }
  86. return c_ptr;
  87. }
  88. void cluster_delete(struct cluster *c_ptr)
  89. {
  90. u32 n_num;
  91. if (!c_ptr)
  92. return;
  93. for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
  94. node_delete(c_ptr->nodes[n_num]);
  95. }
  96. for (n_num = LOWEST_SLAVE; n_num <= c_ptr->highest_slave; n_num++) {
  97. node_delete(c_ptr->nodes[n_num]);
  98. }
  99. kfree(c_ptr->nodes);
  100. kfree(c_ptr);
  101. }
  102. u32 cluster_next_node(struct cluster *c_ptr, u32 addr)
  103. {
  104. struct node *n_ptr;
  105. u32 n_num = tipc_node(addr) + 1;
  106. if (!c_ptr)
  107. return addr;
  108. for (; n_num <= c_ptr->highest_node; n_num++) {
  109. n_ptr = c_ptr->nodes[n_num];
  110. if (n_ptr && node_has_active_links(n_ptr))
  111. return n_ptr->addr;
  112. }
  113. for (n_num = 1; n_num < tipc_node(addr); n_num++) {
  114. n_ptr = c_ptr->nodes[n_num];
  115. if (n_ptr && node_has_active_links(n_ptr))
  116. return n_ptr->addr;
  117. }
  118. return 0;
  119. }
  120. void cluster_attach_node(struct cluster *c_ptr, struct node *n_ptr)
  121. {
  122. u32 n_num = tipc_node(n_ptr->addr);
  123. u32 max_n_num = tipc_max_nodes;
  124. if (in_own_cluster(n_ptr->addr))
  125. max_n_num = highest_allowed_slave;
  126. assert(n_num > 0);
  127. assert(n_num <= max_n_num);
  128. assert(c_ptr->nodes[n_num] == 0);
  129. c_ptr->nodes[n_num] = n_ptr;
  130. if (n_num > c_ptr->highest_node)
  131. c_ptr->highest_node = n_num;
  132. }
  133. /**
  134. * cluster_select_router - select router to a cluster
  135. *
  136. * Uses deterministic and fair algorithm.
  137. */
  138. u32 cluster_select_router(struct cluster *c_ptr, u32 ref)
  139. {
  140. u32 n_num;
  141. u32 ulim = c_ptr->highest_node;
  142. u32 mask;
  143. u32 tstart;
  144. assert(!in_own_cluster(c_ptr->addr));
  145. if (!ulim)
  146. return 0;
  147. /* Start entry must be random */
  148. mask = tipc_max_nodes;
  149. while (mask > ulim)
  150. mask >>= 1;
  151. tstart = ref & mask;
  152. n_num = tstart;
  153. /* Lookup upwards with wrap-around */
  154. do {
  155. if (node_is_up(c_ptr->nodes[n_num]))
  156. break;
  157. } while (++n_num <= ulim);
  158. if (n_num > ulim) {
  159. n_num = 1;
  160. do {
  161. if (node_is_up(c_ptr->nodes[n_num]))
  162. break;
  163. } while (++n_num < tstart);
  164. if (n_num == tstart)
  165. return 0;
  166. }
  167. assert(n_num <= ulim);
  168. return node_select_router(c_ptr->nodes[n_num], ref);
  169. }
  170. /**
  171. * cluster_select_node - select destination node within a remote cluster
  172. *
  173. * Uses deterministic and fair algorithm.
  174. */
  175. struct node *cluster_select_node(struct cluster *c_ptr, u32 selector)
  176. {
  177. u32 n_num;
  178. u32 mask = tipc_max_nodes;
  179. u32 start_entry;
  180. assert(!in_own_cluster(c_ptr->addr));
  181. if (!c_ptr->highest_node)
  182. return 0;
  183. /* Start entry must be random */
  184. while (mask > c_ptr->highest_node) {
  185. mask >>= 1;
  186. }
  187. start_entry = (selector & mask) ? selector & mask : 1u;
  188. assert(start_entry <= c_ptr->highest_node);
  189. /* Lookup upwards with wrap-around */
  190. for (n_num = start_entry; n_num <= c_ptr->highest_node; n_num++) {
  191. if (node_has_active_links(c_ptr->nodes[n_num]))
  192. return c_ptr->nodes[n_num];
  193. }
  194. for (n_num = 1; n_num < start_entry; n_num++) {
  195. if (node_has_active_links(c_ptr->nodes[n_num]))
  196. return c_ptr->nodes[n_num];
  197. }
  198. return 0;
  199. }
  200. /*
  201. * Routing table management: See description in node.c
  202. */
  203. struct sk_buff *cluster_prepare_routing_msg(u32 data_size, u32 dest)
  204. {
  205. u32 size = INT_H_SIZE + data_size;
  206. struct sk_buff *buf = buf_acquire(size);
  207. struct tipc_msg *msg;
  208. if (buf) {
  209. msg = buf_msg(buf);
  210. memset((char *)msg, 0, size);
  211. msg_init(msg, ROUTE_DISTRIBUTOR, 0, TIPC_OK, INT_H_SIZE, dest);
  212. }
  213. return buf;
  214. }
  215. void cluster_bcast_new_route(struct cluster *c_ptr, u32 dest,
  216. u32 lower, u32 upper)
  217. {
  218. struct sk_buff *buf = cluster_prepare_routing_msg(0, c_ptr->addr);
  219. struct tipc_msg *msg;
  220. if (buf) {
  221. msg = buf_msg(buf);
  222. msg_set_remote_node(msg, dest);
  223. msg_set_type(msg, ROUTE_ADDITION);
  224. cluster_multicast(c_ptr, buf, lower, upper);
  225. } else {
  226. warn("Memory squeeze: broadcast of new route failed\n");
  227. }
  228. }
  229. void cluster_bcast_lost_route(struct cluster *c_ptr, u32 dest,
  230. u32 lower, u32 upper)
  231. {
  232. struct sk_buff *buf = cluster_prepare_routing_msg(0, c_ptr->addr);
  233. struct tipc_msg *msg;
  234. if (buf) {
  235. msg = buf_msg(buf);
  236. msg_set_remote_node(msg, dest);
  237. msg_set_type(msg, ROUTE_REMOVAL);
  238. cluster_multicast(c_ptr, buf, lower, upper);
  239. } else {
  240. warn("Memory squeeze: broadcast of lost route failed\n");
  241. }
  242. }
  243. void cluster_send_slave_routes(struct cluster *c_ptr, u32 dest)
  244. {
  245. struct sk_buff *buf;
  246. struct tipc_msg *msg;
  247. u32 highest = c_ptr->highest_slave;
  248. u32 n_num;
  249. int send = 0;
  250. assert(!is_slave(dest));
  251. assert(in_own_cluster(dest));
  252. assert(in_own_cluster(c_ptr->addr));
  253. if (highest <= LOWEST_SLAVE)
  254. return;
  255. buf = cluster_prepare_routing_msg(highest - LOWEST_SLAVE + 1,
  256. c_ptr->addr);
  257. if (buf) {
  258. msg = buf_msg(buf);
  259. msg_set_remote_node(msg, c_ptr->addr);
  260. msg_set_type(msg, SLAVE_ROUTING_TABLE);
  261. for (n_num = LOWEST_SLAVE; n_num <= highest; n_num++) {
  262. if (c_ptr->nodes[n_num] &&
  263. node_has_active_links(c_ptr->nodes[n_num])) {
  264. send = 1;
  265. msg_set_dataoctet(msg, n_num);
  266. }
  267. }
  268. if (send)
  269. link_send(buf, dest, dest);
  270. else
  271. buf_discard(buf);
  272. } else {
  273. warn("Memory squeeze: broadcast of lost route failed\n");
  274. }
  275. }
  276. void cluster_send_ext_routes(struct cluster *c_ptr, u32 dest)
  277. {
  278. struct sk_buff *buf;
  279. struct tipc_msg *msg;
  280. u32 highest = c_ptr->highest_node;
  281. u32 n_num;
  282. int send = 0;
  283. if (in_own_cluster(c_ptr->addr))
  284. return;
  285. assert(!is_slave(dest));
  286. assert(in_own_cluster(dest));
  287. highest = c_ptr->highest_node;
  288. buf = cluster_prepare_routing_msg(highest + 1, c_ptr->addr);
  289. if (buf) {
  290. msg = buf_msg(buf);
  291. msg_set_remote_node(msg, c_ptr->addr);
  292. msg_set_type(msg, EXT_ROUTING_TABLE);
  293. for (n_num = 1; n_num <= highest; n_num++) {
  294. if (c_ptr->nodes[n_num] &&
  295. node_has_active_links(c_ptr->nodes[n_num])) {
  296. send = 1;
  297. msg_set_dataoctet(msg, n_num);
  298. }
  299. }
  300. if (send)
  301. link_send(buf, dest, dest);
  302. else
  303. buf_discard(buf);
  304. } else {
  305. warn("Memory squeeze: broadcast of external route failed\n");
  306. }
  307. }
  308. void cluster_send_local_routes(struct cluster *c_ptr, u32 dest)
  309. {
  310. struct sk_buff *buf;
  311. struct tipc_msg *msg;
  312. u32 highest = c_ptr->highest_node;
  313. u32 n_num;
  314. int send = 0;
  315. assert(is_slave(dest));
  316. assert(in_own_cluster(c_ptr->addr));
  317. buf = cluster_prepare_routing_msg(highest, c_ptr->addr);
  318. if (buf) {
  319. msg = buf_msg(buf);
  320. msg_set_remote_node(msg, c_ptr->addr);
  321. msg_set_type(msg, LOCAL_ROUTING_TABLE);
  322. for (n_num = 1; n_num <= highest; n_num++) {
  323. if (c_ptr->nodes[n_num] &&
  324. node_has_active_links(c_ptr->nodes[n_num])) {
  325. send = 1;
  326. msg_set_dataoctet(msg, n_num);
  327. }
  328. }
  329. if (send)
  330. link_send(buf, dest, dest);
  331. else
  332. buf_discard(buf);
  333. } else {
  334. warn("Memory squeeze: broadcast of local route failed\n");
  335. }
  336. }
  337. void cluster_recv_routing_table(struct sk_buff *buf)
  338. {
  339. struct tipc_msg *msg = buf_msg(buf);
  340. struct cluster *c_ptr;
  341. struct node *n_ptr;
  342. unchar *node_table;
  343. u32 table_size;
  344. u32 router;
  345. u32 rem_node = msg_remote_node(msg);
  346. u32 z_num;
  347. u32 c_num;
  348. u32 n_num;
  349. c_ptr = cluster_find(rem_node);
  350. if (!c_ptr) {
  351. c_ptr = cluster_create(rem_node);
  352. if (!c_ptr) {
  353. buf_discard(buf);
  354. return;
  355. }
  356. }
  357. node_table = buf->data + msg_hdr_sz(msg);
  358. table_size = msg_size(msg) - msg_hdr_sz(msg);
  359. router = msg_prevnode(msg);
  360. z_num = tipc_zone(rem_node);
  361. c_num = tipc_cluster(rem_node);
  362. switch (msg_type(msg)) {
  363. case LOCAL_ROUTING_TABLE:
  364. assert(is_slave(tipc_own_addr));
  365. case EXT_ROUTING_TABLE:
  366. for (n_num = 1; n_num < table_size; n_num++) {
  367. if (node_table[n_num]) {
  368. u32 addr = tipc_addr(z_num, c_num, n_num);
  369. n_ptr = c_ptr->nodes[n_num];
  370. if (!n_ptr) {
  371. n_ptr = node_create(addr);
  372. }
  373. if (n_ptr)
  374. node_add_router(n_ptr, router);
  375. }
  376. }
  377. break;
  378. case SLAVE_ROUTING_TABLE:
  379. assert(!is_slave(tipc_own_addr));
  380. assert(in_own_cluster(c_ptr->addr));
  381. for (n_num = 1; n_num < table_size; n_num++) {
  382. if (node_table[n_num]) {
  383. u32 slave_num = n_num + LOWEST_SLAVE;
  384. u32 addr = tipc_addr(z_num, c_num, slave_num);
  385. n_ptr = c_ptr->nodes[slave_num];
  386. if (!n_ptr) {
  387. n_ptr = node_create(addr);
  388. }
  389. if (n_ptr)
  390. node_add_router(n_ptr, router);
  391. }
  392. }
  393. break;
  394. case ROUTE_ADDITION:
  395. if (!is_slave(tipc_own_addr)) {
  396. assert(!in_own_cluster(c_ptr->addr)
  397. || is_slave(rem_node));
  398. } else {
  399. assert(in_own_cluster(c_ptr->addr)
  400. && !is_slave(rem_node));
  401. }
  402. n_ptr = c_ptr->nodes[tipc_node(rem_node)];
  403. if (!n_ptr)
  404. n_ptr = node_create(rem_node);
  405. if (n_ptr)
  406. node_add_router(n_ptr, router);
  407. break;
  408. case ROUTE_REMOVAL:
  409. if (!is_slave(tipc_own_addr)) {
  410. assert(!in_own_cluster(c_ptr->addr)
  411. || is_slave(rem_node));
  412. } else {
  413. assert(in_own_cluster(c_ptr->addr)
  414. && !is_slave(rem_node));
  415. }
  416. n_ptr = c_ptr->nodes[tipc_node(rem_node)];
  417. if (n_ptr)
  418. node_remove_router(n_ptr, router);
  419. break;
  420. default:
  421. assert(!"Illegal routing manager message received\n");
  422. }
  423. buf_discard(buf);
  424. }
  425. void cluster_remove_as_router(struct cluster *c_ptr, u32 router)
  426. {
  427. u32 start_entry;
  428. u32 tstop;
  429. u32 n_num;
  430. if (is_slave(router))
  431. return; /* Slave nodes can not be routers */
  432. if (in_own_cluster(c_ptr->addr)) {
  433. start_entry = LOWEST_SLAVE;
  434. tstop = c_ptr->highest_slave;
  435. } else {
  436. start_entry = 1;
  437. tstop = c_ptr->highest_node;
  438. }
  439. for (n_num = start_entry; n_num <= tstop; n_num++) {
  440. if (c_ptr->nodes[n_num]) {
  441. node_remove_router(c_ptr->nodes[n_num], router);
  442. }
  443. }
  444. }
  445. /**
  446. * cluster_multicast - multicast message to local nodes
  447. */
  448. void cluster_multicast(struct cluster *c_ptr, struct sk_buff *buf,
  449. u32 lower, u32 upper)
  450. {
  451. struct sk_buff *buf_copy;
  452. struct node *n_ptr;
  453. u32 n_num;
  454. u32 tstop;
  455. assert(lower <= upper);
  456. assert(((lower >= 1) && (lower <= tipc_max_nodes)) ||
  457. ((lower >= LOWEST_SLAVE) && (lower <= highest_allowed_slave)));
  458. assert(((upper >= 1) && (upper <= tipc_max_nodes)) ||
  459. ((upper >= LOWEST_SLAVE) && (upper <= highest_allowed_slave)));
  460. assert(in_own_cluster(c_ptr->addr));
  461. tstop = is_slave(upper) ? c_ptr->highest_slave : c_ptr->highest_node;
  462. if (tstop > upper)
  463. tstop = upper;
  464. for (n_num = lower; n_num <= tstop; n_num++) {
  465. n_ptr = c_ptr->nodes[n_num];
  466. if (n_ptr && node_has_active_links(n_ptr)) {
  467. buf_copy = skb_copy(buf, GFP_ATOMIC);
  468. if (buf_copy == NULL)
  469. break;
  470. msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
  471. link_send(buf_copy, n_ptr->addr, n_ptr->addr);
  472. }
  473. }
  474. buf_discard(buf);
  475. }
  476. /**
  477. * cluster_broadcast - broadcast message to all nodes within cluster
  478. */
  479. void cluster_broadcast(struct sk_buff *buf)
  480. {
  481. struct sk_buff *buf_copy;
  482. struct cluster *c_ptr;
  483. struct node *n_ptr;
  484. u32 n_num;
  485. u32 tstart;
  486. u32 tstop;
  487. u32 node_type;
  488. if (tipc_mode == TIPC_NET_MODE) {
  489. c_ptr = cluster_find(tipc_own_addr);
  490. assert(in_own_cluster(c_ptr->addr)); /* For now */
  491. /* Send to standard nodes, then repeat loop sending to slaves */
  492. tstart = 1;
  493. tstop = c_ptr->highest_node;
  494. for (node_type = 1; node_type <= 2; node_type++) {
  495. for (n_num = tstart; n_num <= tstop; n_num++) {
  496. n_ptr = c_ptr->nodes[n_num];
  497. if (n_ptr && node_has_active_links(n_ptr)) {
  498. buf_copy = skb_copy(buf, GFP_ATOMIC);
  499. if (buf_copy == NULL)
  500. goto exit;
  501. msg_set_destnode(buf_msg(buf_copy),
  502. n_ptr->addr);
  503. link_send(buf_copy, n_ptr->addr,
  504. n_ptr->addr);
  505. }
  506. }
  507. tstart = LOWEST_SLAVE;
  508. tstop = c_ptr->highest_slave;
  509. }
  510. }
  511. exit:
  512. buf_discard(buf);
  513. }
  514. int cluster_init(void)
  515. {
  516. highest_allowed_slave = LOWEST_SLAVE + tipc_max_slaves;
  517. return cluster_create(tipc_own_addr) ? TIPC_OK : -ENOMEM;
  518. }