cluster.c 15 KB

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