cluster.c 14 KB

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