blob: 31e242f39e7fb3eb4a691d6ccfadad17253ac4cf [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/discover.c
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 *
Per Liden9ea1fd32006-01-11 13:30:43 +01009 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +010010 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010020 *
Per Liden9ea1fd32006-01-11 13:30:43 +010021 * 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.
24 *
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
Per Lidenb97bf3f2006-01-02 19:04:38 +010035 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include "core.h"
39#include "dbg.h"
40#include "link.h"
41#include "zone.h"
42#include "discover.h"
43#include "port.h"
44#include "name_table.h"
45
46#define TIPC_LINK_REQ_INIT 125 /* min delay during bearer start up */
47#define TIPC_LINK_REQ_FAST 2000 /* normal delay if bearer has no links */
48#define TIPC_LINK_REQ_SLOW 600000 /* normal delay if bearer has links */
49
50#if 0
51#define GET_NODE_INFO 300
52#define GET_NODE_INFO_RESULT 301
53#define FORWARD_LINK_PROBE 302
54#define LINK_REQUEST_REJECTED 303
55#define LINK_REQUEST_ACCEPTED 304
56#define DROP_LINK_REQUEST 305
57#define CHECK_LINK_COUNT 306
58#endif
59
60/*
61 * TODO: Most of the inter-cluster setup stuff should be
62 * rewritten, and be made conformant with specification.
63 */
64
65
66/**
67 * struct link_req - information about an ongoing link setup request
68 * @bearer: bearer issuing requests
69 * @dest: destination address for request messages
70 * @buf: request message to be (repeatedly) sent
71 * @timer: timer governing period between requests
72 * @timer_intv: current interval between requests (in ms)
73 */
74struct link_req {
75 struct bearer *bearer;
76 struct tipc_media_addr dest;
77 struct sk_buff *buf;
78 struct timer_list timer;
79 unsigned int timer_intv;
80};
81
82
83#if 0
84int disc_create_link(const struct tipc_link_create *argv)
85{
86 /*
87 * Code for inter cluster link setup here
88 */
89 return TIPC_OK;
90}
91#endif
92
93/*
94 * disc_lost_link(): A link has lost contact
95 */
96
97void disc_link_event(u32 addr, char *name, int up)
98{
99 if (in_own_cluster(addr))
100 return;
101 /*
102 * Code for inter cluster link setup here
103 */
104}
105
106/**
107 * disc_init_msg - initialize a link setup message
108 * @type: message type (request or response)
109 * @req_links: number of links associated with message
110 * @dest_domain: network domain of node(s) which should respond to message
111 * @b_ptr: ptr to bearer issuing message
112 */
113
114struct sk_buff *disc_init_msg(u32 type,
115 u32 req_links,
116 u32 dest_domain,
117 struct bearer *b_ptr)
118{
119 struct sk_buff *buf = buf_acquire(DSC_H_SIZE);
120 struct tipc_msg *msg;
121
122 if (buf) {
123 msg = buf_msg(buf);
124 msg_init(msg, LINK_CONFIG, type, TIPC_OK, DSC_H_SIZE,
125 dest_domain);
126 msg_set_non_seq(msg);
127 msg_set_req_links(msg, req_links);
128 msg_set_dest_domain(msg, dest_domain);
129 msg_set_bc_netid(msg, tipc_net_id);
130 msg_set_media_addr(msg, &b_ptr->publ.addr);
131 }
132 return buf;
133}
134
135/**
136 * disc_recv_msg - handle incoming link setup message (request or response)
137 * @buf: buffer containing message
138 */
139
140void disc_recv_msg(struct sk_buff *buf)
141{
142 struct bearer *b_ptr = (struct bearer *)TIPC_SKB_CB(buf)->handle;
143 struct link *link;
144 struct tipc_media_addr media_addr;
145 struct tipc_msg *msg = buf_msg(buf);
146 u32 dest = msg_dest_domain(msg);
147 u32 orig = msg_prevnode(msg);
148 u32 net_id = msg_bc_netid(msg);
149 u32 type = msg_type(msg);
150
151 msg_get_media_addr(msg,&media_addr);
152 msg_dbg(msg, "RECV:");
153 buf_discard(buf);
154
155 if (net_id != tipc_net_id)
156 return;
157 if (!addr_domain_valid(dest))
158 return;
159 if (!addr_node_valid(orig))
160 return;
161 if (orig == tipc_own_addr)
162 return;
163 if (!in_scope(dest, tipc_own_addr))
164 return;
165 if (is_slave(tipc_own_addr) && is_slave(orig))
166 return;
167 if (is_slave(orig) && !in_own_cluster(orig))
168 return;
169 if (in_own_cluster(orig)) {
170 /* Always accept link here */
171 struct sk_buff *rbuf;
172 struct tipc_media_addr *addr;
173 struct node *n_ptr = node_find(orig);
174 int link_up;
175 dbg(" in own cluster\n");
176 if (n_ptr == NULL) {
177 n_ptr = node_create(orig);
178 }
179 if (n_ptr == NULL) {
180 warn("Memory squeeze; Failed to create node\n");
181 return;
182 }
183 spin_lock_bh(&n_ptr->lock);
184 link = n_ptr->links[b_ptr->identity];
185 if (!link) {
186 dbg("creating link\n");
187 link = link_create(b_ptr, orig, &media_addr);
188 if (!link) {
189 spin_unlock_bh(&n_ptr->lock);
190 return;
191 }
192 }
193 addr = &link->media_addr;
194 if (memcmp(addr, &media_addr, sizeof(*addr))) {
195 char addr_string[16];
196
197 warn("New bearer address for %s\n",
198 addr_string_fill(addr_string, orig));
199 memcpy(addr, &media_addr, sizeof(*addr));
200 link_reset(link);
201 }
202 link_up = link_is_up(link);
203 spin_unlock_bh(&n_ptr->lock);
204 if ((type == DSC_RESP_MSG) || link_up)
205 return;
206 rbuf = disc_init_msg(DSC_RESP_MSG, 1, orig, b_ptr);
207 if (rbuf != NULL) {
208 msg_dbg(buf_msg(rbuf),"SEND:");
209 b_ptr->media->send_msg(rbuf, &b_ptr->publ, &media_addr);
210 buf_discard(rbuf);
211 }
212 }
213}
214
215/**
216 * disc_stop_link_req - stop sending periodic link setup requests
217 * @req: ptr to link request structure
218 */
219
220void disc_stop_link_req(struct link_req *req)
221{
222 if (!req)
223 return;
224
225 k_cancel_timer(&req->timer);
226 k_term_timer(&req->timer);
227 buf_discard(req->buf);
228 kfree(req);
229}
230
231/**
232 * disc_update_link_req - update frequency of periodic link setup requests
233 * @req: ptr to link request structure
234 */
235
236void disc_update_link_req(struct link_req *req)
237{
238 if (!req)
239 return;
240
241 if (req->timer_intv == TIPC_LINK_REQ_SLOW) {
242 if (!req->bearer->nodes.count) {
243 req->timer_intv = TIPC_LINK_REQ_FAST;
244 k_start_timer(&req->timer, req->timer_intv);
245 }
246 } else if (req->timer_intv == TIPC_LINK_REQ_FAST) {
247 if (req->bearer->nodes.count) {
248 req->timer_intv = TIPC_LINK_REQ_SLOW;
249 k_start_timer(&req->timer, req->timer_intv);
250 }
251 } else {
252 /* leave timer "as is" if haven't yet reached a "normal" rate */
253 }
254}
255
256/**
257 * disc_timeout - send a periodic link setup request
258 * @req: ptr to link request structure
259 *
260 * Called whenever a link setup request timer associated with a bearer expires.
261 */
262
263static void disc_timeout(struct link_req *req)
264{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 spin_lock_bh(&req->bearer->publ.lock);
266
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 req->bearer->media->send_msg(req->buf, &req->bearer->publ, &req->dest);
268
269 if ((req->timer_intv == TIPC_LINK_REQ_SLOW) ||
270 (req->timer_intv == TIPC_LINK_REQ_FAST)) {
271 /* leave timer interval "as is" if already at a "normal" rate */
272 } else {
273 req->timer_intv *= 2;
Jon Maloyb70e4f42006-01-10 18:54:24 +0000274 if (req->timer_intv > TIPC_LINK_REQ_SLOW)
275 req->timer_intv = TIPC_LINK_REQ_SLOW;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 if ((req->timer_intv == TIPC_LINK_REQ_FAST) &&
277 (req->bearer->nodes.count))
278 req->timer_intv = TIPC_LINK_REQ_SLOW;
279 }
280 k_start_timer(&req->timer, req->timer_intv);
281
282 spin_unlock_bh(&req->bearer->publ.lock);
283}
284
285/**
286 * disc_init_link_req - start sending periodic link setup requests
287 * @b_ptr: ptr to bearer issuing requests
288 * @dest: destination address for request messages
289 * @dest_domain: network domain of node(s) which should respond to message
290 * @req_links: max number of desired links
291 *
292 * Returns pointer to link request structure, or NULL if unable to create.
293 */
294
295struct link_req *disc_init_link_req(struct bearer *b_ptr,
296 const struct tipc_media_addr *dest,
297 u32 dest_domain,
298 u32 req_links)
299{
300 struct link_req *req;
301
302 req = (struct link_req *)kmalloc(sizeof(*req), GFP_ATOMIC);
303 if (!req)
304 return NULL;
305
306 req->buf = disc_init_msg(DSC_REQ_MSG, req_links, dest_domain, b_ptr);
307 if (!req->buf) {
308 kfree(req);
309 return NULL;
310 }
311
312 memcpy(&req->dest, dest, sizeof(*dest));
313 req->bearer = b_ptr;
314 req->timer_intv = TIPC_LINK_REQ_INIT;
315 k_init_timer(&req->timer, (Handler)disc_timeout, (unsigned long)req);
316 k_start_timer(&req->timer, req->timer_intv);
317 return req;
318}
319