net.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * net/tipc/net.c: TIPC network routing code
  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 "bearer.h"
  35. #include "net.h"
  36. #include "zone.h"
  37. #include "addr.h"
  38. #include "name_table.h"
  39. #include "name_distr.h"
  40. #include "subscr.h"
  41. #include "link.h"
  42. #include "msg.h"
  43. #include "port.h"
  44. #include "bcast.h"
  45. #include "discover.h"
  46. #include "config.h"
  47. /*
  48. * The TIPC locking policy is designed to ensure a very fine locking
  49. * granularity, permitting complete parallel access to individual
  50. * port and node/link instances. The code consists of three major
  51. * locking domains, each protected with their own disjunct set of locks.
  52. *
  53. * 1: The routing hierarchy.
  54. * Comprises the structures 'zone', 'cluster', 'node', 'link'
  55. * and 'bearer'. The whole hierarchy is protected by a big
  56. * read/write lock, net_lock, to enssure that nothing is added
  57. * or removed while code is accessing any of these structures.
  58. * This layer must not be called from the two others while they
  59. * hold any of their own locks.
  60. * Neither must it itself do any upcalls to the other two before
  61. * it has released net_lock and other protective locks.
  62. *
  63. * Within the net_lock domain there are two sub-domains;'node' and
  64. * 'bearer', where local write operations are permitted,
  65. * provided that those are protected by individual spin_locks
  66. * per instance. Code holding net_lock(read) and a node spin_lock
  67. * is permitted to poke around in both the node itself and its
  68. * subordinate links. I.e, it can update link counters and queues,
  69. * change link state, send protocol messages, and alter the
  70. * "active_links" array in the node; but it can _not_ remove a link
  71. * or a node from the overall structure.
  72. * Correspondingly, individual bearers may change status within a
  73. * net_lock(read), protected by an individual spin_lock ber bearer
  74. * instance, but it needs net_lock(write) to remove/add any bearers.
  75. *
  76. *
  77. * 2: The transport level of the protocol.
  78. * This consists of the structures port, (and its user level
  79. * representations, such as user_port and tipc_sock), reference and
  80. * tipc_user (port.c, reg.c, socket.c).
  81. *
  82. * This layer has four different locks:
  83. * - The tipc_port spin_lock. This is protecting each port instance
  84. * from parallel data access and removal. Since we can not place
  85. * this lock in the port itself, it has been placed in the
  86. * corresponding reference table entry, which has the same life
  87. * cycle as the module. This entry is difficult to access from
  88. * outside the TIPC core, however, so a pointer to the lock has
  89. * been added in the port instance, -to be used for unlocking
  90. * only.
  91. * - A read/write lock to protect the reference table itself (teg.c).
  92. * (Nobody is using read-only access to this, so it can just as
  93. * well be changed to a spin_lock)
  94. * - A spin lock to protect the registry of kernel/driver users (reg.c)
  95. * - A global spin_lock (port_lock), which only task is to ensure
  96. * consistency where more than one port is involved in an operation,
  97. * i.e., whe a port is part of a linked list of ports.
  98. * There are two such lists; 'port_list', which is used for management,
  99. * and 'wait_list', which is used to queue ports during congestion.
  100. *
  101. * 3: The name table (name_table.c, name_distr.c, subscription.c)
  102. * - There is one big read/write-lock (nametbl_lock) protecting the
  103. * overall name table structure. Nothing must be added/removed to
  104. * this structure without holding write access to it.
  105. * - There is one local spin_lock per sub_sequence, which can be seen
  106. * as a sub-domain to the nametbl_lock domain. It is used only
  107. * for translation operations, and is needed because a translation
  108. * steps the root of the 'publication' linked list between each lookup.
  109. * This is always used within the scope of a nametbl_lock(read).
  110. * - A local spin_lock protecting the queue of subscriber events.
  111. */
  112. rwlock_t net_lock = RW_LOCK_UNLOCKED;
  113. struct network net = { 0 };
  114. struct node *net_select_remote_node(u32 addr, u32 ref)
  115. {
  116. return zone_select_remote_node(net.zones[tipc_zone(addr)], addr, ref);
  117. }
  118. u32 net_select_router(u32 addr, u32 ref)
  119. {
  120. return zone_select_router(net.zones[tipc_zone(addr)], addr, ref);
  121. }
  122. u32 net_next_node(u32 a)
  123. {
  124. if (net.zones[tipc_zone(a)])
  125. return zone_next_node(a);
  126. return 0;
  127. }
  128. void net_remove_as_router(u32 router)
  129. {
  130. u32 z_num;
  131. for (z_num = 1; z_num <= tipc_max_zones; z_num++) {
  132. if (!net.zones[z_num])
  133. continue;
  134. zone_remove_as_router(net.zones[z_num], router);
  135. }
  136. }
  137. void net_send_external_routes(u32 dest)
  138. {
  139. u32 z_num;
  140. for (z_num = 1; z_num <= tipc_max_zones; z_num++) {
  141. if (net.zones[z_num])
  142. zone_send_external_routes(net.zones[z_num], dest);
  143. }
  144. }
  145. int net_init(void)
  146. {
  147. u32 sz = sizeof(struct _zone *) * (tipc_max_zones + 1);
  148. memset(&net, 0, sizeof(net));
  149. net.zones = (struct _zone **)kmalloc(sz, GFP_ATOMIC);
  150. if (!net.zones) {
  151. return -ENOMEM;
  152. }
  153. memset(net.zones, 0, sz);
  154. return TIPC_OK;
  155. }
  156. void net_stop(void)
  157. {
  158. u32 z_num;
  159. if (!net.zones)
  160. return;
  161. for (z_num = 1; z_num <= tipc_max_zones; z_num++) {
  162. zone_delete(net.zones[z_num]);
  163. }
  164. kfree(net.zones);
  165. net.zones = 0;
  166. }
  167. static void net_route_named_msg(struct sk_buff *buf)
  168. {
  169. struct tipc_msg *msg = buf_msg(buf);
  170. u32 dnode;
  171. u32 dport;
  172. if (!msg_named(msg)) {
  173. msg_dbg(msg, "net->drop_nam:");
  174. buf_discard(buf);
  175. return;
  176. }
  177. dnode = addr_domain(msg_lookup_scope(msg));
  178. dport = nametbl_translate(msg_nametype(msg), msg_nameinst(msg), &dnode);
  179. dbg("net->lookup<%u,%u>-><%u,%x>\n",
  180. msg_nametype(msg), msg_nameinst(msg), dport, dnode);
  181. if (dport) {
  182. msg_set_destnode(msg, dnode);
  183. msg_set_destport(msg, dport);
  184. net_route_msg(buf);
  185. return;
  186. }
  187. msg_dbg(msg, "net->rej:NO NAME: ");
  188. tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
  189. }
  190. void net_route_msg(struct sk_buff *buf)
  191. {
  192. struct tipc_msg *msg;
  193. u32 dnode;
  194. if (!buf)
  195. return;
  196. msg = buf_msg(buf);
  197. msg_incr_reroute_cnt(msg);
  198. if (msg_reroute_cnt(msg) > 6) {
  199. if (msg_errcode(msg)) {
  200. msg_dbg(msg, "NET>DISC>:");
  201. buf_discard(buf);
  202. } else {
  203. msg_dbg(msg, "NET>REJ>:");
  204. tipc_reject_msg(buf, msg_destport(msg) ?
  205. TIPC_ERR_NO_PORT : TIPC_ERR_NO_NAME);
  206. }
  207. return;
  208. }
  209. msg_dbg(msg, "net->rout: ");
  210. /* Handle message for this node */
  211. dnode = msg_short(msg) ? tipc_own_addr : msg_destnode(msg);
  212. if (in_scope(dnode, tipc_own_addr)) {
  213. if (msg_isdata(msg)) {
  214. if (msg_mcast(msg))
  215. port_recv_mcast(buf, NULL);
  216. else if (msg_destport(msg))
  217. port_recv_msg(buf);
  218. else
  219. net_route_named_msg(buf);
  220. return;
  221. }
  222. switch (msg_user(msg)) {
  223. case ROUTE_DISTRIBUTOR:
  224. cluster_recv_routing_table(buf);
  225. break;
  226. case NAME_DISTRIBUTOR:
  227. named_recv(buf);
  228. break;
  229. case CONN_MANAGER:
  230. port_recv_proto_msg(buf);
  231. break;
  232. default:
  233. msg_dbg(msg,"DROP/NET/<REC<");
  234. buf_discard(buf);
  235. }
  236. return;
  237. }
  238. /* Handle message for another node */
  239. msg_dbg(msg, "NET>SEND>: ");
  240. link_send(buf, dnode, msg_link_selector(msg));
  241. }
  242. int tipc_start_net(void)
  243. {
  244. char addr_string[16];
  245. int res;
  246. if (tipc_mode != TIPC_NODE_MODE)
  247. return -ENOPROTOOPT;
  248. tipc_mode = TIPC_NET_MODE;
  249. named_reinit();
  250. port_reinit();
  251. if ((res = bearer_init()) ||
  252. (res = net_init()) ||
  253. (res = cluster_init()) ||
  254. (res = bclink_init())) {
  255. return res;
  256. }
  257. subscr_stop();
  258. cfg_stop();
  259. k_signal((Handler)subscr_start, 0);
  260. k_signal((Handler)cfg_init, 0);
  261. info("Started in network mode\n");
  262. info("Own node address %s, network identity %u\n",
  263. addr_string_fill(addr_string, tipc_own_addr), tipc_net_id);
  264. return TIPC_OK;
  265. }
  266. void tipc_stop_net(void)
  267. {
  268. if (tipc_mode != TIPC_NET_MODE)
  269. return;
  270. write_lock_bh(&net_lock);
  271. bearer_stop();
  272. tipc_mode = TIPC_NODE_MODE;
  273. bclink_stop();
  274. net_stop();
  275. write_unlock_bh(&net_lock);
  276. info("Left network mode \n");
  277. }