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