2 * net/tipc/net.c: TIPC network routing code
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
43 #include "name_table.h"
44 #include "name_distr.h"
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.
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.
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.
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).
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
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.
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.
119 rwlock_t net_lock = RW_LOCK_UNLOCKED;
120 struct network net = { 0 };
122 struct node *net_select_remote_node(u32 addr, u32 ref)
124 return zone_select_remote_node(net.zones[tipc_zone(addr)], addr, ref);
127 u32 net_select_router(u32 addr, u32 ref)
129 return zone_select_router(net.zones[tipc_zone(addr)], addr, ref);
133 u32 net_next_node(u32 a)
135 if (net.zones[tipc_zone(a)])
136 return zone_next_node(a);
140 void net_remove_as_router(u32 router)
144 for (z_num = 1; z_num <= tipc_max_zones; z_num++) {
145 if (!net.zones[z_num])
147 zone_remove_as_router(net.zones[z_num], router);
151 void net_send_external_routes(u32 dest)
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);
163 u32 sz = sizeof(struct _zone *) * (tipc_max_zones + 1);
165 memset(&net, 0, sizeof(net));
166 net.zones = (struct _zone **)kmalloc(sz, GFP_ATOMIC);
170 memset(net.zones, 0, sz);
181 for (z_num = 1; z_num <= tipc_max_zones; z_num++) {
182 zone_delete(net.zones[z_num]);
188 static void net_route_named_msg(struct sk_buff *buf)
190 struct tipc_msg *msg = buf_msg(buf);
194 if (!msg_named(msg)) {
195 msg_dbg(msg, "net->drop_nam:");
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);
205 msg_set_destnode(msg, dnode);
206 msg_set_destport(msg, dport);
210 msg_dbg(msg, "net->rej:NO NAME: ");
211 tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
214 void net_route_msg(struct sk_buff *buf)
216 struct tipc_msg *msg;
223 msg_incr_reroute_cnt(msg);
224 if (msg_reroute_cnt(msg) > 6) {
225 if (msg_errcode(msg)) {
226 msg_dbg(msg, "NET>DISC>:");
229 msg_dbg(msg, "NET>REJ>:");
230 tipc_reject_msg(buf, msg_destport(msg) ?
231 TIPC_ERR_NO_PORT : TIPC_ERR_NO_NAME);
236 msg_dbg(msg, "net->rout: ");
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)) {
243 port_recv_mcast(buf, NULL);
244 else if (msg_destport(msg))
247 net_route_named_msg(buf);
250 switch (msg_user(msg)) {
251 case ROUTE_DISTRIBUTOR:
252 cluster_recv_routing_table(buf);
254 case NAME_DISTRIBUTOR:
258 port_recv_proto_msg(buf);
261 msg_dbg(msg,"DROP/NET/<REC<");
267 /* Handle message for another node */
268 msg_dbg(msg, "NET>SEND>: ");
269 link_send(buf, dnode, msg_link_selector(msg));
272 int tipc_start_net(void)
274 char addr_string[16];
277 if (tipc_mode != TIPC_NODE_MODE)
280 tipc_mode = TIPC_NET_MODE;
284 if ((res = bearer_init()) ||
285 (res = net_init()) ||
286 (res = cluster_init()) ||
287 (res = bclink_init())) {
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);
300 void tipc_stop_net(void)
302 if (tipc_mode != TIPC_NET_MODE)
304 write_lock_bh(&net_lock);
306 tipc_mode = TIPC_NODE_MODE;
309 write_unlock_bh(&net_lock);
310 info("Left network mode \n");