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 * 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.
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.
39 #include "name_table.h"
40 #include "name_distr.h"
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.
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.
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.
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).
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
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.
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.
115 rwlock_t net_lock = RW_LOCK_UNLOCKED;
116 struct network net = { 0 };
118 struct node *net_select_remote_node(u32 addr, u32 ref)
120 return zone_select_remote_node(net.zones[tipc_zone(addr)], addr, ref);
123 u32 net_select_router(u32 addr, u32 ref)
125 return zone_select_router(net.zones[tipc_zone(addr)], addr, ref);
129 u32 net_next_node(u32 a)
131 if (net.zones[tipc_zone(a)])
132 return zone_next_node(a);
136 void net_remove_as_router(u32 router)
140 for (z_num = 1; z_num <= tipc_max_zones; z_num++) {
141 if (!net.zones[z_num])
143 zone_remove_as_router(net.zones[z_num], router);
147 void net_send_external_routes(u32 dest)
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);
159 u32 sz = sizeof(struct _zone *) * (tipc_max_zones + 1);
161 memset(&net, 0, sizeof(net));
162 net.zones = (struct _zone **)kmalloc(sz, GFP_ATOMIC);
166 memset(net.zones, 0, sz);
177 for (z_num = 1; z_num <= tipc_max_zones; z_num++) {
178 zone_delete(net.zones[z_num]);
184 static void net_route_named_msg(struct sk_buff *buf)
186 struct tipc_msg *msg = buf_msg(buf);
190 if (!msg_named(msg)) {
191 msg_dbg(msg, "net->drop_nam:");
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);
201 msg_set_destnode(msg, dnode);
202 msg_set_destport(msg, dport);
206 msg_dbg(msg, "net->rej:NO NAME: ");
207 tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
210 void net_route_msg(struct sk_buff *buf)
212 struct tipc_msg *msg;
219 msg_incr_reroute_cnt(msg);
220 if (msg_reroute_cnt(msg) > 6) {
221 if (msg_errcode(msg)) {
222 msg_dbg(msg, "NET>DISC>:");
225 msg_dbg(msg, "NET>REJ>:");
226 tipc_reject_msg(buf, msg_destport(msg) ?
227 TIPC_ERR_NO_PORT : TIPC_ERR_NO_NAME);
232 msg_dbg(msg, "net->rout: ");
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)) {
239 port_recv_mcast(buf, NULL);
240 else if (msg_destport(msg))
243 net_route_named_msg(buf);
246 switch (msg_user(msg)) {
247 case ROUTE_DISTRIBUTOR:
248 cluster_recv_routing_table(buf);
250 case NAME_DISTRIBUTOR:
254 port_recv_proto_msg(buf);
257 msg_dbg(msg,"DROP/NET/<REC<");
263 /* Handle message for another node */
264 msg_dbg(msg, "NET>SEND>: ");
265 link_send(buf, dnode, msg_link_selector(msg));
268 int tipc_start_net(void)
270 char addr_string[16];
273 if (tipc_mode != TIPC_NODE_MODE)
276 tipc_mode = TIPC_NET_MODE;
280 if ((res = bearer_init()) ||
281 (res = net_init()) ||
282 (res = cluster_init()) ||
283 (res = bclink_init())) {
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);
296 void tipc_stop_net(void)
298 if (tipc_mode != TIPC_NET_MODE)
300 write_lock_bh(&net_lock);
302 tipc_mode = TIPC_NODE_MODE;
305 write_unlock_bh(&net_lock);
306 info("Left network mode \n");