cluster.c 14 KB

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