cluster.c 15 KB

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