discover.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * net/tipc/discover.c
  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. * Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "core.h"
  34. #include "dbg.h"
  35. #include "link.h"
  36. #include "zone.h"
  37. #include "discover.h"
  38. #include "port.h"
  39. #include "name_table.h"
  40. #define TIPC_LINK_REQ_INIT 125 /* min delay during bearer start up */
  41. #define TIPC_LINK_REQ_FAST 2000 /* normal delay if bearer has no links */
  42. #define TIPC_LINK_REQ_SLOW 600000 /* normal delay if bearer has links */
  43. #if 0
  44. #define GET_NODE_INFO 300
  45. #define GET_NODE_INFO_RESULT 301
  46. #define FORWARD_LINK_PROBE 302
  47. #define LINK_REQUEST_REJECTED 303
  48. #define LINK_REQUEST_ACCEPTED 304
  49. #define DROP_LINK_REQUEST 305
  50. #define CHECK_LINK_COUNT 306
  51. #endif
  52. /*
  53. * TODO: Most of the inter-cluster setup stuff should be
  54. * rewritten, and be made conformant with specification.
  55. */
  56. /**
  57. * struct link_req - information about an ongoing link setup request
  58. * @bearer: bearer issuing requests
  59. * @dest: destination address for request messages
  60. * @buf: request message to be (repeatedly) sent
  61. * @timer: timer governing period between requests
  62. * @timer_intv: current interval between requests (in ms)
  63. */
  64. struct link_req {
  65. struct bearer *bearer;
  66. struct tipc_media_addr dest;
  67. struct sk_buff *buf;
  68. struct timer_list timer;
  69. unsigned int timer_intv;
  70. };
  71. #if 0
  72. int disc_create_link(const struct tipc_link_create *argv)
  73. {
  74. /*
  75. * Code for inter cluster link setup here
  76. */
  77. return TIPC_OK;
  78. }
  79. #endif
  80. /*
  81. * disc_lost_link(): A link has lost contact
  82. */
  83. void disc_link_event(u32 addr, char *name, int up)
  84. {
  85. if (in_own_cluster(addr))
  86. return;
  87. /*
  88. * Code for inter cluster link setup here
  89. */
  90. }
  91. /**
  92. * disc_init_msg - initialize a link setup message
  93. * @type: message type (request or response)
  94. * @req_links: number of links associated with message
  95. * @dest_domain: network domain of node(s) which should respond to message
  96. * @b_ptr: ptr to bearer issuing message
  97. */
  98. struct sk_buff *disc_init_msg(u32 type,
  99. u32 req_links,
  100. u32 dest_domain,
  101. struct bearer *b_ptr)
  102. {
  103. struct sk_buff *buf = buf_acquire(DSC_H_SIZE);
  104. struct tipc_msg *msg;
  105. if (buf) {
  106. msg = buf_msg(buf);
  107. msg_init(msg, LINK_CONFIG, type, TIPC_OK, DSC_H_SIZE,
  108. dest_domain);
  109. msg_set_non_seq(msg);
  110. msg_set_req_links(msg, req_links);
  111. msg_set_dest_domain(msg, dest_domain);
  112. msg_set_bc_netid(msg, tipc_net_id);
  113. msg_set_media_addr(msg, &b_ptr->publ.addr);
  114. }
  115. return buf;
  116. }
  117. /**
  118. * disc_recv_msg - handle incoming link setup message (request or response)
  119. * @buf: buffer containing message
  120. */
  121. void disc_recv_msg(struct sk_buff *buf)
  122. {
  123. struct bearer *b_ptr = (struct bearer *)TIPC_SKB_CB(buf)->handle;
  124. struct link *link;
  125. struct tipc_media_addr media_addr;
  126. struct tipc_msg *msg = buf_msg(buf);
  127. u32 dest = msg_dest_domain(msg);
  128. u32 orig = msg_prevnode(msg);
  129. u32 net_id = msg_bc_netid(msg);
  130. u32 type = msg_type(msg);
  131. msg_get_media_addr(msg,&media_addr);
  132. msg_dbg(msg, "RECV:");
  133. buf_discard(buf);
  134. if (net_id != tipc_net_id)
  135. return;
  136. if (!addr_domain_valid(dest))
  137. return;
  138. if (!addr_node_valid(orig))
  139. return;
  140. if (orig == tipc_own_addr)
  141. return;
  142. if (!in_scope(dest, tipc_own_addr))
  143. return;
  144. if (is_slave(tipc_own_addr) && is_slave(orig))
  145. return;
  146. if (is_slave(orig) && !in_own_cluster(orig))
  147. return;
  148. if (in_own_cluster(orig)) {
  149. /* Always accept link here */
  150. struct sk_buff *rbuf;
  151. struct tipc_media_addr *addr;
  152. struct node *n_ptr = node_find(orig);
  153. int link_up;
  154. dbg(" in own cluster\n");
  155. if (n_ptr == NULL) {
  156. n_ptr = node_create(orig);
  157. }
  158. if (n_ptr == NULL) {
  159. warn("Memory squeeze; Failed to create node\n");
  160. return;
  161. }
  162. spin_lock_bh(&n_ptr->lock);
  163. link = n_ptr->links[b_ptr->identity];
  164. if (!link) {
  165. dbg("creating link\n");
  166. link = link_create(b_ptr, orig, &media_addr);
  167. if (!link) {
  168. spin_unlock_bh(&n_ptr->lock);
  169. return;
  170. }
  171. }
  172. addr = &link->media_addr;
  173. if (memcmp(addr, &media_addr, sizeof(*addr))) {
  174. char addr_string[16];
  175. warn("New bearer address for %s\n",
  176. addr_string_fill(addr_string, orig));
  177. memcpy(addr, &media_addr, sizeof(*addr));
  178. link_reset(link);
  179. }
  180. link_up = link_is_up(link);
  181. spin_unlock_bh(&n_ptr->lock);
  182. if ((type == DSC_RESP_MSG) || link_up)
  183. return;
  184. rbuf = disc_init_msg(DSC_RESP_MSG, 1, orig, b_ptr);
  185. if (rbuf != NULL) {
  186. msg_dbg(buf_msg(rbuf),"SEND:");
  187. b_ptr->media->send_msg(rbuf, &b_ptr->publ, &media_addr);
  188. buf_discard(rbuf);
  189. }
  190. }
  191. }
  192. /**
  193. * disc_stop_link_req - stop sending periodic link setup requests
  194. * @req: ptr to link request structure
  195. */
  196. void disc_stop_link_req(struct link_req *req)
  197. {
  198. if (!req)
  199. return;
  200. k_cancel_timer(&req->timer);
  201. k_term_timer(&req->timer);
  202. buf_discard(req->buf);
  203. kfree(req);
  204. }
  205. /**
  206. * disc_update_link_req - update frequency of periodic link setup requests
  207. * @req: ptr to link request structure
  208. */
  209. void disc_update_link_req(struct link_req *req)
  210. {
  211. if (!req)
  212. return;
  213. if (req->timer_intv == TIPC_LINK_REQ_SLOW) {
  214. if (!req->bearer->nodes.count) {
  215. req->timer_intv = TIPC_LINK_REQ_FAST;
  216. k_start_timer(&req->timer, req->timer_intv);
  217. }
  218. } else if (req->timer_intv == TIPC_LINK_REQ_FAST) {
  219. if (req->bearer->nodes.count) {
  220. req->timer_intv = TIPC_LINK_REQ_SLOW;
  221. k_start_timer(&req->timer, req->timer_intv);
  222. }
  223. } else {
  224. /* leave timer "as is" if haven't yet reached a "normal" rate */
  225. }
  226. }
  227. /**
  228. * disc_timeout - send a periodic link setup request
  229. * @req: ptr to link request structure
  230. *
  231. * Called whenever a link setup request timer associated with a bearer expires.
  232. */
  233. static void disc_timeout(struct link_req *req)
  234. {
  235. struct tipc_msg *msg = buf_msg(req->buf);
  236. spin_lock_bh(&req->bearer->publ.lock);
  237. #if 0
  238. /* CURRENTLY DON'T SUPPORT INTER-ZONE LINKS */
  239. u32 dest_domain = msg_dest_domain(msg);
  240. int stop = 0;
  241. if (!in_scope(dest_domain, tipc_own_addr)) {
  242. struct _zone *z_ptr = zone_find(dest_domain);
  243. if (z_ptr && (z_ptr->links >= msg_req_links(msg)))
  244. stop = 1;
  245. if (req->timer_intv >= 32000)
  246. stop = 1;
  247. }
  248. if (stop) {
  249. k_cancel_timer(&req->timer);
  250. buf_discard(req->buf);
  251. kfree(req);
  252. spin_unlock_bh(&req->bearer->publ.lock);
  253. return;
  254. }
  255. #endif
  256. msg_dbg(msg,"SEND:");
  257. req->bearer->media->send_msg(req->buf, &req->bearer->publ, &req->dest);
  258. if ((req->timer_intv == TIPC_LINK_REQ_SLOW) ||
  259. (req->timer_intv == TIPC_LINK_REQ_FAST)) {
  260. /* leave timer interval "as is" if already at a "normal" rate */
  261. } else {
  262. req->timer_intv *= 2;
  263. if (req->timer_intv > TIPC_LINK_REQ_FAST)
  264. req->timer_intv = TIPC_LINK_REQ_FAST;
  265. if ((req->timer_intv == TIPC_LINK_REQ_FAST) &&
  266. (req->bearer->nodes.count))
  267. req->timer_intv = TIPC_LINK_REQ_SLOW;
  268. }
  269. k_start_timer(&req->timer, req->timer_intv);
  270. spin_unlock_bh(&req->bearer->publ.lock);
  271. }
  272. /**
  273. * disc_init_link_req - start sending periodic link setup requests
  274. * @b_ptr: ptr to bearer issuing requests
  275. * @dest: destination address for request messages
  276. * @dest_domain: network domain of node(s) which should respond to message
  277. * @req_links: max number of desired links
  278. *
  279. * Returns pointer to link request structure, or NULL if unable to create.
  280. */
  281. struct link_req *disc_init_link_req(struct bearer *b_ptr,
  282. const struct tipc_media_addr *dest,
  283. u32 dest_domain,
  284. u32 req_links)
  285. {
  286. struct link_req *req;
  287. req = (struct link_req *)kmalloc(sizeof(*req), GFP_ATOMIC);
  288. if (!req)
  289. return NULL;
  290. req->buf = disc_init_msg(DSC_REQ_MSG, req_links, dest_domain, b_ptr);
  291. if (!req->buf) {
  292. kfree(req);
  293. return NULL;
  294. }
  295. memcpy(&req->dest, dest, sizeof(*dest));
  296. req->bearer = b_ptr;
  297. req->timer_intv = TIPC_LINK_REQ_INIT;
  298. k_init_timer(&req->timer, (Handler)disc_timeout, (unsigned long)req);
  299. k_start_timer(&req->timer, req->timer_intv);
  300. return req;
  301. }