4 * Copyright (c) 2003-2006, Ericsson AB
5 * Copyright (c) 2005-2006, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
41 #include "name_table.h"
43 #define TIPC_LINK_REQ_INIT 125 /* min delay during bearer start up */
44 #define TIPC_LINK_REQ_FAST 2000 /* normal delay if bearer has no links */
45 #define TIPC_LINK_REQ_SLOW 600000 /* normal delay if bearer has links */
48 * TODO: Most of the inter-cluster setup stuff should be
49 * rewritten, and be made conformant with specification.
54 * struct link_req - information about an ongoing link setup request
55 * @bearer: bearer issuing requests
56 * @dest: destination address for request messages
57 * @buf: request message to be (repeatedly) sent
58 * @timer: timer governing period between requests
59 * @timer_intv: current interval between requests (in ms)
62 struct bearer *bearer;
63 struct tipc_media_addr dest;
65 struct timer_list timer;
66 unsigned int timer_intv;
70 * tipc_disc_init_msg - initialize a link setup message
71 * @type: message type (request or response)
72 * @req_links: number of links associated with message
73 * @dest_domain: network domain of node(s) which should respond to message
74 * @b_ptr: ptr to bearer issuing message
77 static struct sk_buff *tipc_disc_init_msg(u32 type,
82 struct sk_buff *buf = tipc_buf_acquire(DSC_H_SIZE);
87 tipc_msg_init(msg, LINK_CONFIG, type, DSC_H_SIZE, dest_domain);
88 msg_set_non_seq(msg, 1);
89 msg_set_req_links(msg, req_links);
90 msg_set_dest_domain(msg, dest_domain);
91 msg_set_bc_netid(msg, tipc_net_id);
92 msg_set_media_addr(msg, &b_ptr->publ.addr);
98 * disc_dupl_alert - issue node address duplication alert
99 * @b_ptr: pointer to bearer detecting duplication
100 * @node_addr: duplicated node address
101 * @media_addr: media address advertised by duplicated node
104 static void disc_dupl_alert(struct bearer *b_ptr, u32 node_addr,
105 struct tipc_media_addr *media_addr)
107 char node_addr_str[16];
108 char media_addr_str[64];
111 tipc_addr_string_fill(node_addr_str, node_addr);
112 tipc_printbuf_init(&pb, media_addr_str, sizeof(media_addr_str));
113 tipc_media_addr_printf(&pb, media_addr);
114 tipc_printbuf_validate(&pb);
115 warn("Duplicate %s using %s seen on <%s>\n",
116 node_addr_str, media_addr_str, b_ptr->publ.name);
120 * tipc_disc_recv_msg - handle incoming link setup message (request or response)
121 * @buf: buffer containing message
122 * @b_ptr: bearer that message arrived on
125 void tipc_disc_recv_msg(struct sk_buff *buf, struct bearer *b_ptr)
128 struct tipc_media_addr media_addr;
129 struct tipc_msg *msg = buf_msg(buf);
130 u32 dest = msg_dest_domain(msg);
131 u32 orig = msg_prevnode(msg);
132 u32 net_id = msg_bc_netid(msg);
133 u32 type = msg_type(msg);
135 msg_get_media_addr(msg,&media_addr);
136 msg_dbg(msg, "RECV:");
139 if (net_id != tipc_net_id)
141 if (!tipc_addr_domain_valid(dest))
143 if (!tipc_addr_node_valid(orig))
145 if (orig == tipc_own_addr) {
146 if (memcmp(&media_addr, &b_ptr->publ.addr, sizeof(media_addr)))
147 disc_dupl_alert(b_ptr, tipc_own_addr, &media_addr);
150 if (!tipc_in_scope(dest, tipc_own_addr))
152 if (in_own_cluster(orig)) {
153 /* Always accept link here */
154 struct sk_buff *rbuf;
155 struct tipc_media_addr *addr;
156 struct tipc_node *n_ptr = tipc_node_find(orig);
159 dbg(" in own cluster\n");
161 n_ptr = tipc_node_create(orig);
165 spin_lock_bh(&n_ptr->lock);
167 /* Don't talk to neighbor during cleanup after last session */
169 if (n_ptr->cleanup_required) {
170 spin_unlock_bh(&n_ptr->lock);
174 link = n_ptr->links[b_ptr->identity];
176 dbg("creating link\n");
177 link = tipc_link_create(b_ptr, orig, &media_addr);
179 spin_unlock_bh(&n_ptr->lock);
183 addr = &link->media_addr;
184 if (memcmp(addr, &media_addr, sizeof(*addr))) {
185 if (tipc_link_is_up(link) || (!link->started)) {
186 disc_dupl_alert(b_ptr, orig, &media_addr);
187 spin_unlock_bh(&n_ptr->lock);
190 warn("Resetting link <%s>, peer interface address changed\n",
192 memcpy(addr, &media_addr, sizeof(*addr));
193 tipc_link_reset(link);
195 link_fully_up = link_working_working(link);
196 spin_unlock_bh(&n_ptr->lock);
197 if ((type == DSC_RESP_MSG) || link_fully_up)
199 rbuf = tipc_disc_init_msg(DSC_RESP_MSG, 1, orig, b_ptr);
201 msg_dbg(buf_msg(rbuf),"SEND:");
202 b_ptr->media->send_msg(rbuf, &b_ptr->publ, &media_addr);
209 * tipc_disc_stop_link_req - stop sending periodic link setup requests
210 * @req: ptr to link request structure
213 void tipc_disc_stop_link_req(struct link_req *req)
218 k_cancel_timer(&req->timer);
219 k_term_timer(&req->timer);
220 buf_discard(req->buf);
225 * tipc_disc_update_link_req - update frequency of periodic link setup requests
226 * @req: ptr to link request structure
229 void tipc_disc_update_link_req(struct link_req *req)
234 if (req->timer_intv == TIPC_LINK_REQ_SLOW) {
235 if (!req->bearer->nodes.count) {
236 req->timer_intv = TIPC_LINK_REQ_FAST;
237 k_start_timer(&req->timer, req->timer_intv);
239 } else if (req->timer_intv == TIPC_LINK_REQ_FAST) {
240 if (req->bearer->nodes.count) {
241 req->timer_intv = TIPC_LINK_REQ_SLOW;
242 k_start_timer(&req->timer, req->timer_intv);
245 /* leave timer "as is" if haven't yet reached a "normal" rate */
250 * disc_timeout - send a periodic link setup request
251 * @req: ptr to link request structure
253 * Called whenever a link setup request timer associated with a bearer expires.
256 static void disc_timeout(struct link_req *req)
258 spin_lock_bh(&req->bearer->publ.lock);
260 req->bearer->media->send_msg(req->buf, &req->bearer->publ, &req->dest);
262 if ((req->timer_intv == TIPC_LINK_REQ_SLOW) ||
263 (req->timer_intv == TIPC_LINK_REQ_FAST)) {
264 /* leave timer interval "as is" if already at a "normal" rate */
266 req->timer_intv *= 2;
267 if (req->timer_intv > TIPC_LINK_REQ_FAST)
268 req->timer_intv = TIPC_LINK_REQ_FAST;
269 if ((req->timer_intv == TIPC_LINK_REQ_FAST) &&
270 (req->bearer->nodes.count))
271 req->timer_intv = TIPC_LINK_REQ_SLOW;
273 k_start_timer(&req->timer, req->timer_intv);
275 spin_unlock_bh(&req->bearer->publ.lock);
279 * tipc_disc_init_link_req - start sending periodic link setup requests
280 * @b_ptr: ptr to bearer issuing requests
281 * @dest: destination address for request messages
282 * @dest_domain: network domain of node(s) which should respond to message
283 * @req_links: max number of desired links
285 * Returns pointer to link request structure, or NULL if unable to create.
288 struct link_req *tipc_disc_init_link_req(struct bearer *b_ptr,
289 const struct tipc_media_addr *dest,
293 struct link_req *req;
295 req = kmalloc(sizeof(*req), GFP_ATOMIC);
299 req->buf = tipc_disc_init_msg(DSC_REQ_MSG, req_links, dest_domain, b_ptr);
305 memcpy(&req->dest, dest, sizeof(*dest));
307 req->timer_intv = TIPC_LINK_REQ_INIT;
308 k_init_timer(&req->timer, (Handler)disc_timeout, (unsigned long)req);
309 k_start_timer(&req->timer, req->timer_intv);