Blame view

net/tipc/net.c 7.73 KB
b97bf3fd8   Per Liden   [TIPC] Initial merge
1
2
  /*
   * net/tipc/net.c: TIPC network routing code
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
3
   *
593a5f22d   Per Liden   [TIPC] More updat...
4
   * Copyright (c) 1995-2006, Ericsson AB
9df3b7eb6   Allan Stephens   tipc: Fix problem...
5
   * Copyright (c) 2005, 2010-2011, Wind River Systems
b97bf3fd8   Per Liden   [TIPC] Initial merge
6
7
8
9
10
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are met:
   *
9ea1fd3c1   Per Liden   [TIPC] License he...
11
12
13
14
15
16
17
18
19
20
21
22
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in the
   *    documentation and/or other materials provided with the distribution.
   * 3. Neither the names of the copyright holders nor the names of its
   *    contributors may be used to endorse or promote products derived from
   *    this software without specific prior written permission.
   *
   * Alternatively, this software may be distributed under the terms of the
   * GNU General Public License ("GPL") version 2 as published by the Free
   * Software Foundation.
b97bf3fd8   Per Liden   [TIPC] Initial merge
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   * POSSIBILITY OF SUCH DAMAGE.
   */
  
  #include "core.h"
b97bf3fd8   Per Liden   [TIPC] Initial merge
38
  #include "net.h"
b97bf3fd8   Per Liden   [TIPC] Initial merge
39
40
  #include "name_distr.h"
  #include "subscr.h"
b97bf3fd8   Per Liden   [TIPC] Initial merge
41
  #include "port.h"
672d99e19   Allan Stephens   tipc: Convert nod...
42
  #include "node.h"
b97bf3fd8   Per Liden   [TIPC] Initial merge
43
  #include "config.h"
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
44
  /*
b97bf3fd8   Per Liden   [TIPC] Initial merge
45
46
   * The TIPC locking policy is designed to ensure a very fine locking
   * granularity, permitting complete parallel access to individual
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
47
   * port and node/link instances. The code consists of three major
b97bf3fd8   Per Liden   [TIPC] Initial merge
48
49
50
   * locking domains, each protected with their own disjunct set of locks.
   *
   * 1: The routing hierarchy.
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
51
52
53
54
55
   *    Comprises the structures 'zone', 'cluster', 'node', 'link'
   *    and 'bearer'. The whole hierarchy is protected by a big
   *    read/write lock, tipc_net_lock, to enssure that nothing is added
   *    or removed while code is accessing any of these structures.
   *    This layer must not be called from the two others while they
b97bf3fd8   Per Liden   [TIPC] Initial merge
56
57
   *    hold any of their own locks.
   *    Neither must it itself do any upcalls to the other two before
4323add67   Per Liden   [TIPC] Avoid poll...
58
   *    it has released tipc_net_lock and other protective locks.
b97bf3fd8   Per Liden   [TIPC] Initial merge
59
   *
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
60
   *   Within the tipc_net_lock domain there are two sub-domains;'node' and
b97bf3fd8   Per Liden   [TIPC] Initial merge
61
62
   *   'bearer', where local write operations are permitted,
   *   provided that those are protected by individual spin_locks
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
63
   *   per instance. Code holding tipc_net_lock(read) and a node spin_lock
b97bf3fd8   Per Liden   [TIPC] Initial merge
64
   *   is permitted to poke around in both the node itself and its
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
65
66
67
   *   subordinate links. I.e, it can update link counters and queues,
   *   change link state, send protocol messages, and alter the
   *   "active_links" array in the node; but it can _not_ remove a link
b97bf3fd8   Per Liden   [TIPC] Initial merge
68
   *   or a node from the overall structure.
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
69
70
   *   Correspondingly, individual bearers may change status within a
   *   tipc_net_lock(read), protected by an individual spin_lock ber bearer
4323add67   Per Liden   [TIPC] Avoid poll...
71
   *   instance, but it needs tipc_net_lock(write) to remove/add any bearers.
b97bf3fd8   Per Liden   [TIPC] Initial merge
72
   *
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
73
74
75
76
77
   *
   *  2: The transport level of the protocol.
   *     This consists of the structures port, (and its user level
   *     representations, such as user_port and tipc_sock), reference and
   *     tipc_user (port.c, reg.c, socket.c).
b97bf3fd8   Per Liden   [TIPC] Initial merge
78
79
80
   *
   *     This layer has four different locks:
   *     - The tipc_port spin_lock. This is protecting each port instance
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
81
82
   *       from parallel data access and removal. Since we can not place
   *       this lock in the port itself, it has been placed in the
b97bf3fd8   Per Liden   [TIPC] Initial merge
83
   *       corresponding reference table entry, which has the same life
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
84
85
86
   *       cycle as the module. This entry is difficult to access from
   *       outside the TIPC core, however, so a pointer to the lock has
   *       been added in the port instance, -to be used for unlocking
b97bf3fd8   Per Liden   [TIPC] Initial merge
87
   *       only.
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
88
89
   *     - A read/write lock to protect the reference table itself (teg.c).
   *       (Nobody is using read-only access to this, so it can just as
b97bf3fd8   Per Liden   [TIPC] Initial merge
90
91
   *       well be changed to a spin_lock)
   *     - A spin lock to protect the registry of kernel/driver users (reg.c)
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
92
   *     - A global spin_lock (tipc_port_lock), which only task is to ensure
b97bf3fd8   Per Liden   [TIPC] Initial merge
93
94
95
96
   *       consistency where more than one port is involved in an operation,
   *       i.e., whe a port is part of a linked list of ports.
   *       There are two such lists; 'port_list', which is used for management,
   *       and 'wait_list', which is used to queue ports during congestion.
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
97
   *
b97bf3fd8   Per Liden   [TIPC] Initial merge
98
   *  3: The name table (name_table.c, name_distr.c, subscription.c)
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
99
100
   *     - There is one big read/write-lock (tipc_nametbl_lock) protecting the
   *       overall name table structure. Nothing must be added/removed to
b97bf3fd8   Per Liden   [TIPC] Initial merge
101
102
   *       this structure without holding write access to it.
   *     - There is one local spin_lock per sub_sequence, which can be seen
4323add67   Per Liden   [TIPC] Avoid poll...
103
   *       as a sub-domain to the tipc_nametbl_lock domain. It is used only
b97bf3fd8   Per Liden   [TIPC] Initial merge
104
105
   *       for translation operations, and is needed because a translation
   *       steps the root of the 'publication' linked list between each lookup.
4323add67   Per Liden   [TIPC] Avoid poll...
106
   *       This is always used within the scope of a tipc_nametbl_lock(read).
b97bf3fd8   Per Liden   [TIPC] Initial merge
107
108
   *     - A local spin_lock protecting the queue of subscriber events.
  */
34af946a2   Ingo Molnar   [PATCH] spin/rwlo...
109
  DEFINE_RWLOCK(tipc_net_lock);
b97bf3fd8   Per Liden   [TIPC] Initial merge
110
111
112
113
114
115
116
117
  
  static void net_route_named_msg(struct sk_buff *buf)
  {
  	struct tipc_msg *msg = buf_msg(buf);
  	u32 dnode;
  	u32 dport;
  
  	if (!msg_named(msg)) {
b97bf3fd8   Per Liden   [TIPC] Initial merge
118
119
120
121
122
  		buf_discard(buf);
  		return;
  	}
  
  	dnode = addr_domain(msg_lookup_scope(msg));
4323add67   Per Liden   [TIPC] Avoid poll...
123
  	dport = tipc_nametbl_translate(msg_nametype(msg), msg_nameinst(msg), &dnode);
b97bf3fd8   Per Liden   [TIPC] Initial merge
124
125
126
  	if (dport) {
  		msg_set_destnode(msg, dnode);
  		msg_set_destport(msg, dport);
4323add67   Per Liden   [TIPC] Avoid poll...
127
  		tipc_net_route_msg(buf);
b97bf3fd8   Per Liden   [TIPC] Initial merge
128
129
  		return;
  	}
b97bf3fd8   Per Liden   [TIPC] Initial merge
130
131
  	tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
  }
4323add67   Per Liden   [TIPC] Avoid poll...
132
  void tipc_net_route_msg(struct sk_buff *buf)
b97bf3fd8   Per Liden   [TIPC] Initial merge
133
134
135
136
137
138
139
  {
  	struct tipc_msg *msg;
  	u32 dnode;
  
  	if (!buf)
  		return;
  	msg = buf_msg(buf);
b97bf3fd8   Per Liden   [TIPC] Initial merge
140
141
  	/* Handle message for this node */
  	dnode = msg_short(msg) ? tipc_own_addr : msg_destnode(msg);
c68ca7b72   Allan Stephens   tipc: add tipc_ p...
142
  	if (tipc_in_scope(dnode, tipc_own_addr)) {
b97bf3fd8   Per Liden   [TIPC] Initial merge
143
  		if (msg_isdata(msg)) {
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
144
  			if (msg_mcast(msg))
4323add67   Per Liden   [TIPC] Avoid poll...
145
  				tipc_port_recv_mcast(buf, NULL);
b97bf3fd8   Per Liden   [TIPC] Initial merge
146
  			else if (msg_destport(msg))
4323add67   Per Liden   [TIPC] Avoid poll...
147
  				tipc_port_recv_msg(buf);
b97bf3fd8   Per Liden   [TIPC] Initial merge
148
149
150
151
152
  			else
  				net_route_named_msg(buf);
  			return;
  		}
  		switch (msg_user(msg)) {
b97bf3fd8   Per Liden   [TIPC] Initial merge
153
  		case NAME_DISTRIBUTOR:
4323add67   Per Liden   [TIPC] Avoid poll...
154
  			tipc_named_recv(buf);
b97bf3fd8   Per Liden   [TIPC] Initial merge
155
156
  			break;
  		case CONN_MANAGER:
4323add67   Per Liden   [TIPC] Avoid poll...
157
  			tipc_port_recv_proto_msg(buf);
b97bf3fd8   Per Liden   [TIPC] Initial merge
158
159
  			break;
  		default:
b97bf3fd8   Per Liden   [TIPC] Initial merge
160
161
162
163
164
165
  			buf_discard(buf);
  		}
  		return;
  	}
  
  	/* Handle message for another node */
b2abd4c03   Paul Gortmaker   tipc: Optimize ha...
166
  	skb_trim(buf, msg_size(msg));
4323add67   Per Liden   [TIPC] Avoid poll...
167
  	tipc_link_send(buf, dnode, msg_link_selector(msg));
b97bf3fd8   Per Liden   [TIPC] Initial merge
168
  }
03194379a   Allan Stephens   tipc: Fix initial...
169
  int tipc_net_start(u32 addr)
b97bf3fd8   Per Liden   [TIPC] Initial merge
170
171
  {
  	char addr_string[16];
b97bf3fd8   Per Liden   [TIPC] Initial merge
172
173
174
  
  	if (tipc_mode != TIPC_NODE_MODE)
  		return -ENOPROTOOPT;
03194379a   Allan Stephens   tipc: Fix initial...
175
176
177
178
  	tipc_subscr_stop();
  	tipc_cfg_stop();
  
  	tipc_own_addr = addr;
b97bf3fd8   Per Liden   [TIPC] Initial merge
179
  	tipc_mode = TIPC_NET_MODE;
4323add67   Per Liden   [TIPC] Avoid poll...
180
181
  	tipc_named_reinit();
  	tipc_port_reinit();
b97bf3fd8   Per Liden   [TIPC] Initial merge
182

c47e9b918   Allan Stephens   tipc: Eliminate d...
183
  	tipc_bclink_init();
03194379a   Allan Stephens   tipc: Fix initial...
184

4323add67   Per Liden   [TIPC] Avoid poll...
185
186
  	tipc_k_signal((Handler)tipc_subscr_start, 0);
  	tipc_k_signal((Handler)tipc_cfg_init, 0);
03194379a   Allan Stephens   tipc: Fix initial...
187

b97bf3fd8   Per Liden   [TIPC] Initial merge
188
189
190
191
  	info("Started in network mode
  ");
  	info("Own node address %s, network identity %u
  ",
c68ca7b72   Allan Stephens   tipc: add tipc_ p...
192
  	     tipc_addr_string_fill(addr_string, tipc_own_addr), tipc_net_id);
0e35fd5e5   Allan Stephens   tipc: Eliminate i...
193
  	return 0;
b97bf3fd8   Per Liden   [TIPC] Initial merge
194
  }
4323add67   Per Liden   [TIPC] Avoid poll...
195
  void tipc_net_stop(void)
b97bf3fd8   Per Liden   [TIPC] Initial merge
196
  {
34e46258c   Allan Stephens   tipc: manually in...
197
  	struct tipc_node *node, *t_node;
b97bf3fd8   Per Liden   [TIPC] Initial merge
198
199
  	if (tipc_mode != TIPC_NET_MODE)
  		return;
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
200
  	write_lock_bh(&tipc_net_lock);
b97bf3fd8   Per Liden   [TIPC] Initial merge
201
  	tipc_mode = TIPC_NODE_MODE;
97f1b625d   Allan Stephens   tipc: Minor corre...
202
  	tipc_bearer_stop();
4323add67   Per Liden   [TIPC] Avoid poll...
203
  	tipc_bclink_stop();
1fa073803   Paul Gortmaker   tipc: delete extr...
204
  	list_for_each_entry_safe(node, t_node, &tipc_node_list, list)
34e46258c   Allan Stephens   tipc: manually in...
205
  		tipc_node_delete(node);
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
206
  	write_unlock_bh(&tipc_net_lock);
a570f095e   Frans Pop   tipc: remove trai...
207
208
  	info("Left network mode
  ");
b97bf3fd8   Per Liden   [TIPC] Initial merge
209
  }