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