net.c 9.6 KB

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