cluster.c 14 KB

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