blob: 5adfdfd49d61b65dc0164cd1fcecaff88d4fa558 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/link.c: TIPC link code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Allan Stephens05646c92007-06-10 17:25:24 -07004 * Copyright (c) 1996-2007, Ericsson AB
5 * Copyright (c) 2004-2007, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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.
23 *
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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "dbg.h"
39#include "link.h"
40#include "net.h"
41#include "node.h"
42#include "port.h"
43#include "addr.h"
44#include "node_subscr.h"
45#include "name_distr.h"
46#include "bearer.h"
47#include "name_table.h"
48#include "discover.h"
49#include "config.h"
50#include "bcast.h"
51
52
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090053/*
54 * Limit for deferred reception queue:
Per Lidenb97bf3f2006-01-02 19:04:38 +010055 */
56
57#define DEF_QUEUE_LIMIT 256u
58
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090059/*
60 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010061 */
62
63#define STARTING_EVT 856384768 /* link processing trigger */
64#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
65#define TIMEOUT_EVT 560817u /* link timer expired */
66
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090067/*
68 * The following two 'message types' is really just implementation
69 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010070 * They must not be considered part of the protocol
71 */
72#define OPEN_MSG 0
73#define CLOSED_MSG 1
74
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090075/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010076 * State value stored in 'exp_msg_count'
77 */
78
79#define START_CHANGEOVER 100000u
80
81/**
82 * struct link_name - deconstructed link name
83 * @addr_local: network address of node at this end
84 * @if_local: name of interface at this end
85 * @addr_peer: network address of node at far end
86 * @if_peer: name of interface at far end
87 */
88
89struct link_name {
90 u32 addr_local;
91 char if_local[TIPC_MAX_IF_NAME];
92 u32 addr_peer;
93 char if_peer[TIPC_MAX_IF_NAME];
94};
95
96#if 0
97
98/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
99
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900100/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101 * struct link_event - link up/down event notification
102 */
103
104struct link_event {
105 u32 addr;
106 int up;
107 void (*fcn)(u32, char *, int);
108 char name[TIPC_MAX_LINK_NAME];
109};
110
111#endif
112
113static void link_handle_out_of_seq_msg(struct link *l_ptr,
114 struct sk_buff *buf);
115static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
116static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
117static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
118static int link_send_sections_long(struct port *sender,
119 struct iovec const *msg_sect,
120 u32 num_sect, u32 destnode);
121static void link_check_defragm_bufs(struct link *l_ptr);
122static void link_state_event(struct link *l_ptr, u32 event);
123static void link_reset_statistics(struct link *l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900124static void link_print(struct link *l_ptr, struct print_buf *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 const char *str);
126
127/*
128 * Debugging code used by link routines only
129 *
130 * When debugging link problems on a system that has multiple links,
131 * the standard TIPC debugging routines may not be useful since they
132 * allow the output from multiple links to be intermixed. For this reason
133 * routines of the form "dbg_link_XXX()" have been created that will capture
134 * debug info into a link's personal print buffer, which can then be dumped
Allan Stephensa3df92c2006-10-16 21:49:03 -0700135 * into the TIPC system log (TIPC_LOG) upon request.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 *
137 * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size
138 * of the print buffer used by each link. If LINK_LOG_BUF_SIZE is set to 0,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900139 * the dbg_link_XXX() routines simply send their output to the standard
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful
141 * when there is only a single link in the system being debugged.
142 *
143 * Notes:
Allan Stephensa3df92c2006-10-16 21:49:03 -0700144 * - When enabled, LINK_LOG_BUF_SIZE should be set to at least TIPC_PB_MIN_SIZE
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900145 * - "l_ptr" must be valid when using dbg_link_XXX() macros
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 */
147
148#define LINK_LOG_BUF_SIZE 0
149
150#define dbg_link(fmt, arg...) do {if (LINK_LOG_BUF_SIZE) tipc_printf(&l_ptr->print_buf, fmt, ## arg); } while(0)
Per Liden4323add2006-01-18 00:38:21 +0100151#define dbg_link_msg(msg, txt) do {if (LINK_LOG_BUF_SIZE) tipc_msg_print(&l_ptr->print_buf, msg, txt); } while(0)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152#define dbg_link_state(txt) do {if (LINK_LOG_BUF_SIZE) link_print(l_ptr, &l_ptr->print_buf, txt); } while(0)
153#define dbg_link_dump() do { \
154 if (LINK_LOG_BUF_SIZE) { \
155 tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \
Per Liden4323add2006-01-18 00:38:21 +0100156 tipc_printbuf_move(LOG, &l_ptr->print_buf); \
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 } \
158} while (0)
159
Sam Ravnborg05790c62006-03-20 22:37:04 -0800160static void dbg_print_link(struct link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161{
Allan Stephensa3df92c2006-10-16 21:49:03 -0700162 if (DBG_OUTPUT != TIPC_NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 link_print(l_ptr, DBG_OUTPUT, str);
164}
165
Sam Ravnborg05790c62006-03-20 22:37:04 -0800166static void dbg_print_buf_chain(struct sk_buff *root_buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167{
Allan Stephensa3df92c2006-10-16 21:49:03 -0700168 if (DBG_OUTPUT != TIPC_NULL) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 struct sk_buff *buf = root_buf;
170
171 while (buf) {
172 msg_dbg(buf_msg(buf), "In chain: ");
173 buf = buf->next;
174 }
175 }
176}
177
178/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800179 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 */
181
Sam Ravnborg05790c62006-03-20 22:37:04 -0800182static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183{
184 return (i + 3) & ~3u;
185}
186
Sam Ravnborg05790c62006-03-20 22:37:04 -0800187static int link_working_working(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188{
189 return (l_ptr->state == WORKING_WORKING);
190}
191
Sam Ravnborg05790c62006-03-20 22:37:04 -0800192static int link_working_unknown(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193{
194 return (l_ptr->state == WORKING_UNKNOWN);
195}
196
Sam Ravnborg05790c62006-03-20 22:37:04 -0800197static int link_reset_unknown(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198{
199 return (l_ptr->state == RESET_UNKNOWN);
200}
201
Sam Ravnborg05790c62006-03-20 22:37:04 -0800202static int link_reset_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203{
204 return (l_ptr->state == RESET_RESET);
205}
206
Sam Ravnborg05790c62006-03-20 22:37:04 -0800207static int link_blocked(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208{
209 return (l_ptr->exp_msg_count || l_ptr->blocked);
210}
211
Sam Ravnborg05790c62006-03-20 22:37:04 -0800212static int link_congested(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213{
214 return (l_ptr->out_queue_size >= l_ptr->queue_limit[0]);
215}
216
Sam Ravnborg05790c62006-03-20 22:37:04 -0800217static u32 link_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218{
219 return l_ptr->max_pkt;
220}
221
Sam Ravnborg05790c62006-03-20 22:37:04 -0800222static void link_init_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223{
224 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900225
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
227 if (max_pkt > MAX_MSG_SIZE)
228 max_pkt = MAX_MSG_SIZE;
229
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900230 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
232 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900233 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 l_ptr->max_pkt = MAX_PKT_DEFAULT;
235
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900236 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237}
238
Sam Ravnborg05790c62006-03-20 22:37:04 -0800239static u32 link_next_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240{
241 if (l_ptr->next_out)
242 return msg_seqno(buf_msg(l_ptr->next_out));
243 return mod(l_ptr->next_out_no);
244}
245
Sam Ravnborg05790c62006-03-20 22:37:04 -0800246static u32 link_last_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247{
248 return mod(link_next_sent(l_ptr) - 1);
249}
250
251/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800252 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 */
254
Per Liden4323add2006-01-18 00:38:21 +0100255int tipc_link_is_up(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256{
257 if (!l_ptr)
258 return 0;
259 return (link_working_working(l_ptr) || link_working_unknown(l_ptr));
260}
261
Per Liden4323add2006-01-18 00:38:21 +0100262int tipc_link_is_active(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263{
264 return ((l_ptr->owner->active_links[0] == l_ptr) ||
265 (l_ptr->owner->active_links[1] == l_ptr));
266}
267
268/**
269 * link_name_validate - validate & (optionally) deconstruct link name
270 * @name - ptr to link name string
271 * @name_parts - ptr to area for link name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900272 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 * Returns 1 if link name is valid, otherwise 0.
274 */
275
276static int link_name_validate(const char *name, struct link_name *name_parts)
277{
278 char name_copy[TIPC_MAX_LINK_NAME];
279 char *addr_local;
280 char *if_local;
281 char *addr_peer;
282 char *if_peer;
283 char dummy;
284 u32 z_local, c_local, n_local;
285 u32 z_peer, c_peer, n_peer;
286 u32 if_local_len;
287 u32 if_peer_len;
288
289 /* copy link name & ensure length is OK */
290
291 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
292 /* need above in case non-Posix strncpy() doesn't pad with nulls */
293 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
294 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
295 return 0;
296
297 /* ensure all component parts of link name are present */
298
299 addr_local = name_copy;
300 if ((if_local = strchr(addr_local, ':')) == NULL)
301 return 0;
302 *(if_local++) = 0;
303 if ((addr_peer = strchr(if_local, '-')) == NULL)
304 return 0;
305 *(addr_peer++) = 0;
306 if_local_len = addr_peer - if_local;
307 if ((if_peer = strchr(addr_peer, ':')) == NULL)
308 return 0;
309 *(if_peer++) = 0;
310 if_peer_len = strlen(if_peer) + 1;
311
312 /* validate component parts of link name */
313
314 if ((sscanf(addr_local, "%u.%u.%u%c",
315 &z_local, &c_local, &n_local, &dummy) != 3) ||
316 (sscanf(addr_peer, "%u.%u.%u%c",
317 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
318 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
319 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
321 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
323 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
324 return 0;
325
326 /* return link name components, if necessary */
327
328 if (name_parts) {
329 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
330 strcpy(name_parts->if_local, if_local);
331 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
332 strcpy(name_parts->if_peer, if_peer);
333 }
334 return 1;
335}
336
337/**
338 * link_timeout - handle expiration of link timer
339 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900340 *
Per Liden4323add2006-01-18 00:38:21 +0100341 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
342 * with tipc_link_delete(). (There is no risk that the node will be deleted by
343 * another thread because tipc_link_delete() always cancels the link timer before
344 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 */
346
347static void link_timeout(struct link *l_ptr)
348{
Per Liden4323add2006-01-18 00:38:21 +0100349 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350
351 /* update counters used in statistical profiling of send traffic */
352
353 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
354 l_ptr->stats.queue_sz_counts++;
355
356 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
357 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
358
359 if (l_ptr->first_out) {
360 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
361 u32 length = msg_size(msg);
362
363 if ((msg_user(msg) == MSG_FRAGMENTER)
364 && (msg_type(msg) == FIRST_FRAGMENT)) {
365 length = msg_size(msg_get_wrapped(msg));
366 }
367 if (length) {
368 l_ptr->stats.msg_lengths_total += length;
369 l_ptr->stats.msg_length_counts++;
370 if (length <= 64)
371 l_ptr->stats.msg_length_profile[0]++;
372 else if (length <= 256)
373 l_ptr->stats.msg_length_profile[1]++;
374 else if (length <= 1024)
375 l_ptr->stats.msg_length_profile[2]++;
376 else if (length <= 4096)
377 l_ptr->stats.msg_length_profile[3]++;
378 else if (length <= 16384)
379 l_ptr->stats.msg_length_profile[4]++;
380 else if (length <= 32768)
381 l_ptr->stats.msg_length_profile[5]++;
382 else
383 l_ptr->stats.msg_length_profile[6]++;
384 }
385 }
386
387 /* do all other link processing performed on a periodic basis */
388
389 link_check_defragm_bufs(l_ptr);
390
391 link_state_event(l_ptr, TIMEOUT_EVT);
392
393 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100394 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395
Per Liden4323add2006-01-18 00:38:21 +0100396 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397}
398
Sam Ravnborg05790c62006-03-20 22:37:04 -0800399static void link_set_timer(struct link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400{
401 k_start_timer(&l_ptr->timer, time);
402}
403
404/**
Per Liden4323add2006-01-18 00:38:21 +0100405 * tipc_link_create - create a new link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 * @b_ptr: pointer to associated bearer
407 * @peer: network address of node at other end of link
408 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900409 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 * Returns pointer to link.
411 */
412
Per Liden4323add2006-01-18 00:38:21 +0100413struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
414 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415{
416 struct link *l_ptr;
417 struct tipc_msg *msg;
418 char *if_name;
419
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700420 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 if (!l_ptr) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700422 warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 return NULL;
424 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425
426 l_ptr->addr = peer;
427 if_name = strchr(b_ptr->publ.name, ':') + 1;
428 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
429 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 if_name,
432 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
433 /* note: peer i/f is appended to link name by reset/activate */
434 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
435 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
436 list_add_tail(&l_ptr->link_list, &b_ptr->links);
437 l_ptr->checkpoint = 1;
438 l_ptr->b_ptr = b_ptr;
439 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
440 l_ptr->state = RESET_UNKNOWN;
441
442 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
443 msg = l_ptr->pmsg;
444 msg_init(msg, LINK_PROTOCOL, RESET_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
445 msg_set_size(msg, sizeof(l_ptr->proto_msg));
446 msg_set_session(msg, tipc_random);
447 msg_set_bearer_id(msg, b_ptr->identity);
448 strcpy((char *)msg_data(msg), if_name);
449
450 l_ptr->priority = b_ptr->priority;
Per Liden4323add2006-01-18 00:38:21 +0100451 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452
453 link_init_max_pkt(l_ptr);
454
455 l_ptr->next_out_no = 1;
456 INIT_LIST_HEAD(&l_ptr->waiting_ports);
457
458 link_reset_statistics(l_ptr);
459
Per Liden4323add2006-01-18 00:38:21 +0100460 l_ptr->owner = tipc_node_attach_link(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 if (!l_ptr->owner) {
462 kfree(l_ptr);
463 return NULL;
464 }
465
466 if (LINK_LOG_BUF_SIZE) {
467 char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
468
469 if (!pb) {
470 kfree(l_ptr);
Allan Stephensa10bd922006-06-25 23:52:17 -0700471 warn("Link creation failed, no memory for print buffer\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 return NULL;
473 }
Per Liden4323add2006-01-18 00:38:21 +0100474 tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 }
476
Per Liden4323add2006-01-18 00:38:21 +0100477 tipc_k_signal((Handler)tipc_link_start, (unsigned long)l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478
Per Liden4323add2006-01-18 00:38:21 +0100479 dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 return l_ptr;
483}
484
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900485/**
Per Liden4323add2006-01-18 00:38:21 +0100486 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900488 *
Per Liden4323add2006-01-18 00:38:21 +0100489 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900491 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 */
493
Per Liden4323add2006-01-18 00:38:21 +0100494void tipc_link_delete(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495{
496 if (!l_ptr) {
497 err("Attempt to delete non-existent link\n");
498 return;
499 }
500
Per Liden4323add2006-01-18 00:38:21 +0100501 dbg("tipc_link_delete()\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502
503 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900504
Per Liden4323add2006-01-18 00:38:21 +0100505 tipc_node_lock(l_ptr->owner);
506 tipc_link_reset(l_ptr);
507 tipc_node_detach_link(l_ptr->owner, l_ptr);
508 tipc_link_stop(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 list_del_init(&l_ptr->link_list);
510 if (LINK_LOG_BUF_SIZE)
511 kfree(l_ptr->print_buf.buf);
Per Liden4323add2006-01-18 00:38:21 +0100512 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 k_term_timer(&l_ptr->timer);
514 kfree(l_ptr);
515}
516
Per Liden4323add2006-01-18 00:38:21 +0100517void tipc_link_start(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518{
Per Liden4323add2006-01-18 00:38:21 +0100519 dbg("tipc_link_start %x\n", l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 link_state_event(l_ptr, STARTING_EVT);
521}
522
523/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900524 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 * @l_ptr: pointer to link
526 * @origport: reference to sending port
527 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900528 *
529 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 * has abated.
531 */
532
533static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
534{
535 struct port *p_ptr;
536
Per Liden4323add2006-01-18 00:38:21 +0100537 spin_lock_bh(&tipc_port_list_lock);
538 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 if (p_ptr) {
540 if (!p_ptr->wakeup)
541 goto exit;
542 if (!list_empty(&p_ptr->wait_list))
543 goto exit;
544 p_ptr->congested_link = l_ptr;
545 p_ptr->publ.congested = 1;
546 p_ptr->waiting_pkts = 1 + ((sz - 1) / link_max_pkt(l_ptr));
547 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
548 l_ptr->stats.link_congs++;
549exit:
Per Liden4323add2006-01-18 00:38:21 +0100550 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 }
Per Liden4323add2006-01-18 00:38:21 +0100552 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 return -ELINKCONG;
554}
555
Per Liden4323add2006-01-18 00:38:21 +0100556void tipc_link_wakeup_ports(struct link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557{
558 struct port *p_ptr;
559 struct port *temp_p_ptr;
560 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
561
562 if (all)
563 win = 100000;
564 if (win <= 0)
565 return;
Per Liden4323add2006-01-18 00:38:21 +0100566 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 return;
568 if (link_congested(l_ptr))
569 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900570 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 wait_list) {
572 if (win <= 0)
573 break;
574 list_del_init(&p_ptr->wait_list);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800575 p_ptr->congested_link = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 spin_lock_bh(p_ptr->publ.lock);
577 p_ptr->publ.congested = 0;
578 p_ptr->wakeup(&p_ptr->publ);
579 win -= p_ptr->waiting_pkts;
580 spin_unlock_bh(p_ptr->publ.lock);
581 }
582
583exit:
Per Liden4323add2006-01-18 00:38:21 +0100584 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585}
586
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 * link_release_outqueue - purge link's outbound message queue
589 * @l_ptr: pointer to link
590 */
591
592static void link_release_outqueue(struct link *l_ptr)
593{
594 struct sk_buff *buf = l_ptr->first_out;
595 struct sk_buff *next;
596
597 while (buf) {
598 next = buf->next;
599 buf_discard(buf);
600 buf = next;
601 }
602 l_ptr->first_out = NULL;
603 l_ptr->out_queue_size = 0;
604}
605
606/**
Per Liden4323add2006-01-18 00:38:21 +0100607 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 * @l_ptr: pointer to link
609 */
610
Per Liden4323add2006-01-18 00:38:21 +0100611void tipc_link_reset_fragments(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612{
613 struct sk_buff *buf = l_ptr->defragm_buf;
614 struct sk_buff *next;
615
616 while (buf) {
617 next = buf->next;
618 buf_discard(buf);
619 buf = next;
620 }
621 l_ptr->defragm_buf = NULL;
622}
623
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624/**
Per Liden4323add2006-01-18 00:38:21 +0100625 * tipc_link_stop - purge all inbound and outbound messages associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 * @l_ptr: pointer to link
627 */
628
Per Liden4323add2006-01-18 00:38:21 +0100629void tipc_link_stop(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630{
631 struct sk_buff *buf;
632 struct sk_buff *next;
633
634 buf = l_ptr->oldest_deferred_in;
635 while (buf) {
636 next = buf->next;
637 buf_discard(buf);
638 buf = next;
639 }
640
641 buf = l_ptr->first_out;
642 while (buf) {
643 next = buf->next;
644 buf_discard(buf);
645 buf = next;
646 }
647
Per Liden4323add2006-01-18 00:38:21 +0100648 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649
650 buf_discard(l_ptr->proto_msg_queue);
651 l_ptr->proto_msg_queue = NULL;
652}
653
654#if 0
655
656/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
657
658static void link_recv_event(struct link_event *ev)
659{
660 ev->fcn(ev->addr, ev->name, ev->up);
661 kfree(ev);
662}
663
664static void link_send_event(void (*fcn)(u32 a, char *n, int up),
665 struct link *l_ptr, int up)
666{
667 struct link_event *ev;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900668
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
670 if (!ev) {
671 warn("Link event allocation failure\n");
672 return;
673 }
674 ev->addr = l_ptr->addr;
675 ev->up = up;
676 ev->fcn = fcn;
677 memcpy(ev->name, l_ptr->name, TIPC_MAX_LINK_NAME);
Per Liden4323add2006-01-18 00:38:21 +0100678 tipc_k_signal((Handler)link_recv_event, (unsigned long)ev);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679}
680
681#else
682
683#define link_send_event(fcn, l_ptr, up) do { } while (0)
684
685#endif
686
Per Liden4323add2006-01-18 00:38:21 +0100687void tipc_link_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688{
689 struct sk_buff *buf;
690 u32 prev_state = l_ptr->state;
691 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700692 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694 msg_set_session(l_ptr->pmsg, msg_session(l_ptr->pmsg) + 1);
695
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696 /* Link is down, accept any session: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697 l_ptr->peer_session = 0;
698
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900699 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900701
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702 l_ptr->state = RESET_UNKNOWN;
703 dbg_link_state("Resetting Link\n");
704
705 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
706 return;
707
Per Liden4323add2006-01-18 00:38:21 +0100708 tipc_node_link_down(l_ptr->owner, l_ptr);
709 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710#if 0
Per Liden4323add2006-01-18 00:38:21 +0100711 tipc_printf(TIPC_CONS, "\nReset link <%s>\n", l_ptr->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 dbg_link_dump();
713#endif
Allan Stephens5392d642006-06-25 23:52:50 -0700714 if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 l_ptr->owner->permit_changeover) {
716 l_ptr->reset_checkpoint = checkpoint;
717 l_ptr->exp_msg_count = START_CHANGEOVER;
718 }
719
720 /* Clean up all queues: */
721
722 link_release_outqueue(l_ptr);
723 buf_discard(l_ptr->proto_msg_queue);
724 l_ptr->proto_msg_queue = NULL;
725 buf = l_ptr->oldest_deferred_in;
726 while (buf) {
727 struct sk_buff *next = buf->next;
728 buf_discard(buf);
729 buf = next;
730 }
731 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100732 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733
734 l_ptr->retransm_queue_head = 0;
735 l_ptr->retransm_queue_size = 0;
736 l_ptr->last_out = NULL;
737 l_ptr->first_out = NULL;
738 l_ptr->next_out = NULL;
739 l_ptr->unacked_window = 0;
740 l_ptr->checkpoint = 1;
741 l_ptr->next_out_no = 1;
742 l_ptr->deferred_inqueue_sz = 0;
743 l_ptr->oldest_deferred_in = NULL;
744 l_ptr->newest_deferred_in = NULL;
745 l_ptr->fsm_msg_cnt = 0;
746 l_ptr->stale_count = 0;
747 link_reset_statistics(l_ptr);
748
Per Liden4323add2006-01-18 00:38:21 +0100749 link_send_event(tipc_cfg_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100751 link_send_event(tipc_disc_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752}
753
754
755static void link_activate(struct link *l_ptr)
756{
Allan Stephens5392d642006-06-25 23:52:50 -0700757 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100758 tipc_node_link_up(l_ptr->owner, l_ptr);
759 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
760 link_send_event(tipc_cfg_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100762 link_send_event(tipc_disc_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763}
764
765/**
766 * link_state_event - link finite state machine
767 * @l_ptr: pointer to link
768 * @event: state machine event to process
769 */
770
771static void link_state_event(struct link *l_ptr, unsigned event)
772{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900773 struct link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 u32 cont_intv = l_ptr->continuity_interval;
775
776 if (!l_ptr->started && (event != STARTING_EVT))
777 return; /* Not yet. */
778
779 if (link_blocked(l_ptr)) {
780 if (event == TIMEOUT_EVT) {
781 link_set_timer(l_ptr, cont_intv);
782 }
783 return; /* Changeover going on */
784 }
785 dbg_link("STATE_EV: <%s> ", l_ptr->name);
786
787 switch (l_ptr->state) {
788 case WORKING_WORKING:
789 dbg_link("WW/");
790 switch (event) {
791 case TRAFFIC_MSG_EVT:
792 dbg_link("TRF-");
793 /* fall through */
794 case ACTIVATE_MSG:
795 dbg_link("ACT\n");
796 break;
797 case TIMEOUT_EVT:
798 dbg_link("TIM ");
799 if (l_ptr->next_in_no != l_ptr->checkpoint) {
800 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100801 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900802 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100803 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 l_ptr->fsm_msg_cnt++;
805 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900806 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100807 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 l_ptr->fsm_msg_cnt++;
809 }
810 link_set_timer(l_ptr, cont_intv);
811 break;
812 }
813 dbg_link(" -> WU\n");
814 l_ptr->state = WORKING_UNKNOWN;
815 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100816 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 l_ptr->fsm_msg_cnt++;
818 link_set_timer(l_ptr, cont_intv / 4);
819 break;
820 case RESET_MSG:
821 dbg_link("RES -> RR\n");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900822 info("Resetting link <%s>, requested by peer\n",
Allan Stephensa10bd922006-06-25 23:52:17 -0700823 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100824 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 l_ptr->state = RESET_RESET;
826 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100827 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 l_ptr->fsm_msg_cnt++;
829 link_set_timer(l_ptr, cont_intv);
830 break;
831 default:
832 err("Unknown link event %u in WW state\n", event);
833 }
834 break;
835 case WORKING_UNKNOWN:
836 dbg_link("WU/");
837 switch (event) {
838 case TRAFFIC_MSG_EVT:
839 dbg_link("TRF-");
840 case ACTIVATE_MSG:
841 dbg_link("ACT -> WW\n");
842 l_ptr->state = WORKING_WORKING;
843 l_ptr->fsm_msg_cnt = 0;
844 link_set_timer(l_ptr, cont_intv);
845 break;
846 case RESET_MSG:
847 dbg_link("RES -> RR\n");
Allan Stephensa10bd922006-06-25 23:52:17 -0700848 info("Resetting link <%s>, requested by peer "
849 "while probing\n", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100850 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 l_ptr->state = RESET_RESET;
852 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100853 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 l_ptr->fsm_msg_cnt++;
855 link_set_timer(l_ptr, cont_intv);
856 break;
857 case TIMEOUT_EVT:
858 dbg_link("TIM ");
859 if (l_ptr->next_in_no != l_ptr->checkpoint) {
860 dbg_link("-> WW \n");
861 l_ptr->state = WORKING_WORKING;
862 l_ptr->fsm_msg_cnt = 0;
863 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100864 if (tipc_bclink_acks_missing(l_ptr->owner)) {
865 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
866 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867 l_ptr->fsm_msg_cnt++;
868 }
869 link_set_timer(l_ptr, cont_intv);
870 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
871 dbg_link("Probing %u/%u,timer = %u ms)\n",
872 l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
873 cont_intv / 4);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900874 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100875 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 l_ptr->fsm_msg_cnt++;
877 link_set_timer(l_ptr, cont_intv / 4);
878 } else { /* Link has failed */
879 dbg_link("-> RU (%u probes unanswered)\n",
880 l_ptr->fsm_msg_cnt);
Allan Stephensa10bd922006-06-25 23:52:17 -0700881 warn("Resetting link <%s>, peer not responding\n",
882 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100883 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884 l_ptr->state = RESET_UNKNOWN;
885 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100886 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
887 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 l_ptr->fsm_msg_cnt++;
889 link_set_timer(l_ptr, cont_intv);
890 }
891 break;
892 default:
893 err("Unknown link event %u in WU state\n", event);
894 }
895 break;
896 case RESET_UNKNOWN:
897 dbg_link("RU/");
898 switch (event) {
899 case TRAFFIC_MSG_EVT:
900 dbg_link("TRF-\n");
901 break;
902 case ACTIVATE_MSG:
903 other = l_ptr->owner->active_links[0];
904 if (other && link_working_unknown(other)) {
905 dbg_link("ACT\n");
906 break;
907 }
908 dbg_link("ACT -> WW\n");
909 l_ptr->state = WORKING_WORKING;
910 l_ptr->fsm_msg_cnt = 0;
911 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100912 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 l_ptr->fsm_msg_cnt++;
914 link_set_timer(l_ptr, cont_intv);
915 break;
916 case RESET_MSG:
917 dbg_link("RES \n");
918 dbg_link(" -> RR\n");
919 l_ptr->state = RESET_RESET;
920 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100921 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 l_ptr->fsm_msg_cnt++;
923 link_set_timer(l_ptr, cont_intv);
924 break;
925 case STARTING_EVT:
926 dbg_link("START-");
927 l_ptr->started = 1;
928 /* fall through */
929 case TIMEOUT_EVT:
930 dbg_link("TIM \n");
Per Liden4323add2006-01-18 00:38:21 +0100931 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932 l_ptr->fsm_msg_cnt++;
933 link_set_timer(l_ptr, cont_intv);
934 break;
935 default:
936 err("Unknown link event %u in RU state\n", event);
937 }
938 break;
939 case RESET_RESET:
940 dbg_link("RR/ ");
941 switch (event) {
942 case TRAFFIC_MSG_EVT:
943 dbg_link("TRF-");
944 /* fall through */
945 case ACTIVATE_MSG:
946 other = l_ptr->owner->active_links[0];
947 if (other && link_working_unknown(other)) {
948 dbg_link("ACT\n");
949 break;
950 }
951 dbg_link("ACT -> WW\n");
952 l_ptr->state = WORKING_WORKING;
953 l_ptr->fsm_msg_cnt = 0;
954 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100955 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 l_ptr->fsm_msg_cnt++;
957 link_set_timer(l_ptr, cont_intv);
958 break;
959 case RESET_MSG:
960 dbg_link("RES\n");
961 break;
962 case TIMEOUT_EVT:
963 dbg_link("TIM\n");
Per Liden4323add2006-01-18 00:38:21 +0100964 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 l_ptr->fsm_msg_cnt++;
966 link_set_timer(l_ptr, cont_intv);
967 dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
968 break;
969 default:
970 err("Unknown link event %u in RR state\n", event);
971 }
972 break;
973 default:
974 err("Unknown link state %u/%u\n", l_ptr->state, event);
975 }
976}
977
978/*
979 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900980 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 */
982
983static int link_bundle_buf(struct link *l_ptr,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900984 struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 struct sk_buff *buf)
986{
987 struct tipc_msg *bundler_msg = buf_msg(bundler);
988 struct tipc_msg *msg = buf_msg(buf);
989 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700990 u32 bundle_size = msg_size(bundler_msg);
991 u32 to_pos = align(bundle_size);
992 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993
994 if (msg_user(bundler_msg) != MSG_BUNDLER)
995 return 0;
996 if (msg_type(bundler_msg) != OPEN_MSG)
997 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700998 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 return 0;
Allan Stephens863fae62006-07-03 19:39:36 -07001000 if (link_max_pkt(l_ptr) < (to_pos + size))
1001 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002
Allan Stephense49060c2006-06-29 12:32:46 -07001003 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001004 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 msg_set_size(bundler_msg, to_pos + size);
1006 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
1007 dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
1008 msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
1009 msg_dbg(msg, "PACKD:");
1010 buf_discard(buf);
1011 l_ptr->stats.sent_bundled++;
1012 return 1;
1013}
1014
Sam Ravnborg05790c62006-03-20 22:37:04 -08001015static void link_add_to_outqueue(struct link *l_ptr,
1016 struct sk_buff *buf,
1017 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018{
1019 u32 ack = mod(l_ptr->next_in_no - 1);
1020 u32 seqno = mod(l_ptr->next_out_no++);
1021
1022 msg_set_word(msg, 2, ((ack << 16) | seqno));
1023 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1024 buf->next = NULL;
1025 if (l_ptr->first_out) {
1026 l_ptr->last_out->next = buf;
1027 l_ptr->last_out = buf;
1028 } else
1029 l_ptr->first_out = l_ptr->last_out = buf;
1030 l_ptr->out_queue_size++;
1031}
1032
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033/*
1034 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 * inside TIPC when the 'fast path' in tipc_send_buf
1036 * has failed, and from link_send()
1037 */
1038
Per Liden4323add2006-01-18 00:38:21 +01001039int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040{
1041 struct tipc_msg *msg = buf_msg(buf);
1042 u32 size = msg_size(msg);
1043 u32 dsz = msg_data_sz(msg);
1044 u32 queue_size = l_ptr->out_queue_size;
1045 u32 imp = msg_tot_importance(msg);
1046 u32 queue_limit = l_ptr->queue_limit[imp];
1047 u32 max_packet = link_max_pkt(l_ptr);
1048
1049 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
1050
1051 /* Match msg importance against queue limits: */
1052
1053 if (unlikely(queue_size >= queue_limit)) {
1054 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
1055 return link_schedule_port(l_ptr, msg_origport(msg),
1056 size);
1057 }
1058 msg_dbg(msg, "TIPC: Congestion, throwing away\n");
1059 buf_discard(buf);
1060 if (imp > CONN_MANAGER) {
Allan Stephensa10bd922006-06-25 23:52:17 -07001061 warn("Resetting link <%s>, send queue full", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +01001062 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 }
1064 return dsz;
1065 }
1066
1067 /* Fragmentation needed ? */
1068
1069 if (size > max_packet)
Per Liden4323add2006-01-18 00:38:21 +01001070 return tipc_link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071
1072 /* Packet can be queued or sent: */
1073
1074 if (queue_size > l_ptr->stats.max_queue_sz)
1075 l_ptr->stats.max_queue_sz = queue_size;
1076
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001077 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 !link_congested(l_ptr))) {
1079 link_add_to_outqueue(l_ptr, buf, msg);
1080
Per Liden4323add2006-01-18 00:38:21 +01001081 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 l_ptr->unacked_window = 0;
1083 } else {
Per Liden4323add2006-01-18 00:38:21 +01001084 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 l_ptr->stats.bearer_congs++;
1086 l_ptr->next_out = buf;
1087 }
1088 return dsz;
1089 }
1090 /* Congestion: can message be bundled ?: */
1091
1092 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
1093 (msg_user(msg) != MSG_FRAGMENTER)) {
1094
1095 /* Try adding message to an existing bundle */
1096
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001097 if (l_ptr->next_out &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001099 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 return dsz;
1101 }
1102
1103 /* Try creating a new bundle */
1104
1105 if (size <= max_packet * 2 / 3) {
1106 struct sk_buff *bundler = buf_acquire(max_packet);
1107 struct tipc_msg bundler_hdr;
1108
1109 if (bundler) {
1110 msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
1111 TIPC_OK, INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001112 skb_copy_to_linear_data(bundler, &bundler_hdr,
1113 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 skb_trim(bundler, INT_H_SIZE);
1115 link_bundle_buf(l_ptr, bundler, buf);
1116 buf = bundler;
1117 msg = buf_msg(buf);
1118 l_ptr->stats.sent_bundles++;
1119 }
1120 }
1121 }
1122 if (!l_ptr->next_out)
1123 l_ptr->next_out = buf;
1124 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001125 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 return dsz;
1127}
1128
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001129/*
1130 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131 * not been selected yet, and the the owner node is not locked
1132 * Called by TIPC internal users, e.g. the name distributor
1133 */
1134
Per Liden4323add2006-01-18 00:38:21 +01001135int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136{
1137 struct link *l_ptr;
1138 struct node *n_ptr;
1139 int res = -ELINKCONG;
1140
Per Liden4323add2006-01-18 00:38:21 +01001141 read_lock_bh(&tipc_net_lock);
1142 n_ptr = tipc_node_select(dest, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001144 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 l_ptr = n_ptr->active_links[selector & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 if (l_ptr) {
Allan Stephensc33d53b2006-06-25 23:50:30 -07001147 dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
Per Liden4323add2006-01-18 00:38:21 +01001148 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensc33d53b2006-06-25 23:50:30 -07001149 } else {
1150 dbg("Attempt to send msg to unreachable node:\n");
1151 msg_dbg(buf_msg(buf),">>>");
1152 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 }
Per Liden4323add2006-01-18 00:38:21 +01001154 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 } else {
1156 dbg("Attempt to send msg to unknown node:\n");
1157 msg_dbg(buf_msg(buf),">>>");
1158 buf_discard(buf);
1159 }
Per Liden4323add2006-01-18 00:38:21 +01001160 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 return res;
1162}
1163
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001164/*
1165 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 * destination link is known and the header is complete,
1167 * inclusive total message length. Very time critical.
1168 * Link is locked. Returns user data length.
1169 */
1170
Sam Ravnborg05790c62006-03-20 22:37:04 -08001171static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
1172 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173{
1174 struct tipc_msg *msg = buf_msg(buf);
1175 int res = msg_data_sz(msg);
1176
1177 if (likely(!link_congested(l_ptr))) {
1178 if (likely(msg_size(msg) <= link_max_pkt(l_ptr))) {
1179 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
1180 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001181 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
1182 &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 l_ptr->unacked_window = 0;
1184 msg_dbg(msg,"SENT_FAST:");
1185 return res;
1186 }
1187 dbg("failed sent fast...\n");
Per Liden4323add2006-01-18 00:38:21 +01001188 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 l_ptr->stats.bearer_congs++;
1190 l_ptr->next_out = buf;
1191 return res;
1192 }
1193 }
1194 else
1195 *used_max_pkt = link_max_pkt(l_ptr);
1196 }
Per Liden4323add2006-01-18 00:38:21 +01001197 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198}
1199
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001200/*
1201 * tipc_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 * destination node is known and the header is complete,
1203 * inclusive total message length.
1204 * Returns user data length.
1205 */
1206int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
1207{
1208 struct link *l_ptr;
1209 struct node *n_ptr;
1210 int res;
1211 u32 selector = msg_origport(buf_msg(buf)) & 1;
1212 u32 dummy;
1213
1214 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001215 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216
Per Liden4323add2006-01-18 00:38:21 +01001217 read_lock_bh(&tipc_net_lock);
1218 n_ptr = tipc_node_select(destnode, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 if (likely(n_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001220 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 l_ptr = n_ptr->active_links[selector];
1222 dbg("send_fast: buf %x selected %x, destnode = %x\n",
1223 buf, l_ptr, destnode);
1224 if (likely(l_ptr)) {
1225 res = link_send_buf_fast(l_ptr, buf, &dummy);
Per Liden4323add2006-01-18 00:38:21 +01001226 tipc_node_unlock(n_ptr);
1227 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 return res;
1229 }
Per Liden4323add2006-01-18 00:38:21 +01001230 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 }
Per Liden4323add2006-01-18 00:38:21 +01001232 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 res = msg_data_sz(buf_msg(buf));
1234 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1235 return res;
1236}
1237
1238
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001239/*
1240 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001242 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 * Returns user data length or errno.
1244 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001245int tipc_link_send_sections_fast(struct port *sender,
Per Liden4323add2006-01-18 00:38:21 +01001246 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001247 const u32 num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001248 u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249{
1250 struct tipc_msg *hdr = &sender->publ.phdr;
1251 struct link *l_ptr;
1252 struct sk_buff *buf;
1253 struct node *node;
1254 int res;
1255 u32 selector = msg_origport(hdr) & 1;
1256
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257again:
1258 /*
1259 * Try building message using port's max_pkt hint.
1260 * (Must not hold any locks while building message.)
1261 */
1262
Allan Stephens05646c92007-06-10 17:25:24 -07001263 res = msg_build(hdr, msg_sect, num_sect, sender->publ.max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 !sender->user_port, &buf);
1265
Per Liden4323add2006-01-18 00:38:21 +01001266 read_lock_bh(&tipc_net_lock);
1267 node = tipc_node_select(destaddr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001269 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 l_ptr = node->active_links[selector];
1271 if (likely(l_ptr)) {
1272 if (likely(buf)) {
1273 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens05646c92007-06-10 17:25:24 -07001274 &sender->publ.max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275 if (unlikely(res < 0))
1276 buf_discard(buf);
1277exit:
Per Liden4323add2006-01-18 00:38:21 +01001278 tipc_node_unlock(node);
1279 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 return res;
1281 }
1282
1283 /* Exit if build request was invalid */
1284
1285 if (unlikely(res < 0))
1286 goto exit;
1287
1288 /* Exit if link (or bearer) is congested */
1289
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001290 if (link_congested(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291 !list_empty(&l_ptr->b_ptr->cong_links)) {
1292 res = link_schedule_port(l_ptr,
1293 sender->publ.ref, res);
1294 goto exit;
1295 }
1296
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001297 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 * Message size exceeds max_pkt hint; update hint,
1299 * then re-try fast path or fragment the message
1300 */
1301
Allan Stephens05646c92007-06-10 17:25:24 -07001302 sender->publ.max_pkt = link_max_pkt(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001303 tipc_node_unlock(node);
1304 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305
1306
Allan Stephens05646c92007-06-10 17:25:24 -07001307 if ((msg_hdr_sz(hdr) + res) <= sender->publ.max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 goto again;
1309
1310 return link_send_sections_long(sender, msg_sect,
1311 num_sect, destaddr);
1312 }
Per Liden4323add2006-01-18 00:38:21 +01001313 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 }
Per Liden4323add2006-01-18 00:38:21 +01001315 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316
1317 /* Couldn't find a link to the destination node */
1318
1319 if (buf)
1320 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1321 if (res >= 0)
Per Liden4323add2006-01-18 00:38:21 +01001322 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1323 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 return res;
1325}
1326
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001327/*
1328 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001330 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 * Link and bearer congestion status have been checked to be ok,
1332 * and are ignored if they change.
1333 *
1334 * Note that fragments do not use the full link MTU so that they won't have
1335 * to undergo refragmentation if link changeover causes them to be sent
1336 * over another link with an additional tunnel header added as prefix.
1337 * (Refragmentation will still occur if the other link has a smaller MTU.)
1338 *
1339 * Returns user data length or errno.
1340 */
1341static int link_send_sections_long(struct port *sender,
1342 struct iovec const *msg_sect,
1343 u32 num_sect,
1344 u32 destaddr)
1345{
1346 struct link *l_ptr;
1347 struct node *node;
1348 struct tipc_msg *hdr = &sender->publ.phdr;
1349 u32 dsz = msg_data_sz(hdr);
1350 u32 max_pkt,fragm_sz,rest;
1351 struct tipc_msg fragm_hdr;
1352 struct sk_buff *buf,*buf_chain,*prev;
1353 u32 fragm_crs,fragm_rest,hsz,sect_rest;
1354 const unchar *sect_crs;
1355 int curr_sect;
1356 u32 fragm_no;
1357
1358again:
1359 fragm_no = 1;
Allan Stephens05646c92007-06-10 17:25:24 -07001360 max_pkt = sender->publ.max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001362 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 /* leave room for fragmentation header in each fragment */
1364 rest = dsz;
1365 fragm_crs = 0;
1366 fragm_rest = 0;
1367 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001368 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369 curr_sect = -1;
1370
1371 /* Prepare reusable fragment header: */
1372
1373 msg_dbg(hdr, ">FRAGMENTING>");
1374 msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
1375 TIPC_OK, INT_H_SIZE, msg_destnode(hdr));
1376 msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1377 msg_set_size(&fragm_hdr, max_pkt);
1378 msg_set_fragm_no(&fragm_hdr, 1);
1379
1380 /* Prepare header of first fragment: */
1381
1382 buf_chain = buf = buf_acquire(max_pkt);
1383 if (!buf)
1384 return -ENOMEM;
1385 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001386 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001388 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389 msg_dbg(buf_msg(buf), ">BUILD>");
1390
1391 /* Chop up message: */
1392
1393 fragm_crs = INT_H_SIZE + hsz;
1394 fragm_rest = fragm_sz - hsz;
1395
1396 do { /* For all sections */
1397 u32 sz;
1398
1399 if (!sect_rest) {
1400 sect_rest = msg_sect[++curr_sect].iov_len;
1401 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1402 }
1403
1404 if (sect_rest < fragm_rest)
1405 sz = sect_rest;
1406 else
1407 sz = fragm_rest;
1408
1409 if (likely(!sender->user_port)) {
1410 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1411error:
1412 for (; buf_chain; buf_chain = buf) {
1413 buf = buf_chain->next;
1414 buf_discard(buf_chain);
1415 }
1416 return -EFAULT;
1417 }
1418 } else
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001419 skb_copy_to_linear_data_offset(buf, fragm_crs,
1420 sect_crs, sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 sect_crs += sz;
1422 sect_rest -= sz;
1423 fragm_crs += sz;
1424 fragm_rest -= sz;
1425 rest -= sz;
1426
1427 if (!fragm_rest && rest) {
1428
1429 /* Initiate new fragment: */
1430 if (rest <= fragm_sz) {
1431 fragm_sz = rest;
1432 msg_set_type(&fragm_hdr,LAST_FRAGMENT);
1433 } else {
1434 msg_set_type(&fragm_hdr, FRAGMENT);
1435 }
1436 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1437 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1438 prev = buf;
1439 buf = buf_acquire(fragm_sz + INT_H_SIZE);
1440 if (!buf)
1441 goto error;
1442
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001443 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001445 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 fragm_crs = INT_H_SIZE;
1447 fragm_rest = fragm_sz;
1448 msg_dbg(buf_msg(buf)," >BUILD>");
1449 }
1450 }
1451 while (rest > 0);
1452
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001453 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 * Now we have a buffer chain. Select a link and check
1455 * that packet size is still OK
1456 */
Per Liden4323add2006-01-18 00:38:21 +01001457 node = tipc_node_select(destaddr, sender->publ.ref & 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001459 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 l_ptr = node->active_links[sender->publ.ref & 1];
1461 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001462 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 goto reject;
1464 }
1465 if (link_max_pkt(l_ptr) < max_pkt) {
Allan Stephens05646c92007-06-10 17:25:24 -07001466 sender->publ.max_pkt = link_max_pkt(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001467 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 for (; buf_chain; buf_chain = buf) {
1469 buf = buf_chain->next;
1470 buf_discard(buf_chain);
1471 }
1472 goto again;
1473 }
1474 } else {
1475reject:
1476 for (; buf_chain; buf_chain = buf) {
1477 buf = buf_chain->next;
1478 buf_discard(buf_chain);
1479 }
Per Liden4323add2006-01-18 00:38:21 +01001480 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1481 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482 }
1483
1484 /* Append whole chain to send queue: */
1485
1486 buf = buf_chain;
1487 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1488 if (!l_ptr->next_out)
1489 l_ptr->next_out = buf_chain;
1490 l_ptr->stats.sent_fragmented++;
1491 while (buf) {
1492 struct sk_buff *next = buf->next;
1493 struct tipc_msg *msg = buf_msg(buf);
1494
1495 l_ptr->stats.sent_fragments++;
1496 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1497 link_add_to_outqueue(l_ptr, buf, msg);
1498 msg_dbg(msg, ">ADD>");
1499 buf = next;
1500 }
1501
1502 /* Send it, if possible: */
1503
Per Liden4323add2006-01-18 00:38:21 +01001504 tipc_link_push_queue(l_ptr);
1505 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506 return dsz;
1507}
1508
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509/*
Per Liden4323add2006-01-18 00:38:21 +01001510 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 */
Per Liden4323add2006-01-18 00:38:21 +01001512u32 tipc_link_push_packet(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513{
1514 struct sk_buff *buf = l_ptr->first_out;
1515 u32 r_q_size = l_ptr->retransm_queue_size;
1516 u32 r_q_head = l_ptr->retransm_queue_head;
1517
1518 /* Step to position where retransmission failed, if any, */
1519 /* consider that buffers may have been released in meantime */
1520
1521 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001522 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001523 link_last_sent(l_ptr));
1524 u32 first = msg_seqno(buf_msg(buf));
1525
1526 while (buf && less(first, r_q_head)) {
1527 first = mod(first + 1);
1528 buf = buf->next;
1529 }
1530 l_ptr->retransm_queue_head = r_q_head = first;
1531 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1532 }
1533
1534 /* Continue retransmission now, if there is anything: */
1535
1536 if (r_q_size && buf && !skb_cloned(buf)) {
1537 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001538 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001539 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540 msg_dbg(buf_msg(buf), ">DEF-RETR>");
1541 l_ptr->retransm_queue_head = mod(++r_q_head);
1542 l_ptr->retransm_queue_size = --r_q_size;
1543 l_ptr->stats.retransmitted++;
1544 return TIPC_OK;
1545 } else {
1546 l_ptr->stats.bearer_congs++;
1547 msg_dbg(buf_msg(buf), "|>DEF-RETR>");
1548 return PUSH_FAILED;
1549 }
1550 }
1551
1552 /* Send deferred protocol message, if any: */
1553
1554 buf = l_ptr->proto_msg_queue;
1555 if (buf) {
1556 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001557 msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001558 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 msg_dbg(buf_msg(buf), ">DEF-PROT>");
1560 l_ptr->unacked_window = 0;
1561 buf_discard(buf);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001562 l_ptr->proto_msg_queue = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563 return TIPC_OK;
1564 } else {
1565 msg_dbg(buf_msg(buf), "|>DEF-PROT>");
1566 l_ptr->stats.bearer_congs++;
1567 return PUSH_FAILED;
1568 }
1569 }
1570
1571 /* Send one deferred data message, if send window not full: */
1572
1573 buf = l_ptr->next_out;
1574 if (buf) {
1575 struct tipc_msg *msg = buf_msg(buf);
1576 u32 next = msg_seqno(msg);
1577 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1578
1579 if (mod(next - first) < l_ptr->queue_limit[0]) {
1580 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001581 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001582 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 if (msg_user(msg) == MSG_BUNDLER)
1584 msg_set_type(msg, CLOSED_MSG);
1585 msg_dbg(msg, ">PUSH-DATA>");
1586 l_ptr->next_out = buf->next;
1587 return TIPC_OK;
1588 } else {
1589 msg_dbg(msg, "|PUSH-DATA|");
1590 l_ptr->stats.bearer_congs++;
1591 return PUSH_FAILED;
1592 }
1593 }
1594 }
1595 return PUSH_FINISHED;
1596}
1597
1598/*
1599 * push_queue(): push out the unsent messages of a link where
1600 * congestion has abated. Node is locked
1601 */
Per Liden4323add2006-01-18 00:38:21 +01001602void tipc_link_push_queue(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603{
1604 u32 res;
1605
Per Liden4323add2006-01-18 00:38:21 +01001606 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 return;
1608
1609 do {
Per Liden4323add2006-01-18 00:38:21 +01001610 res = tipc_link_push_packet(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001611 }
1612 while (res == TIPC_OK);
1613 if (res == PUSH_FAILED)
Per Liden4323add2006-01-18 00:38:21 +01001614 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615}
1616
Allan Stephensd356eeb2006-06-25 23:40:01 -07001617static void link_reset_all(unsigned long addr)
1618{
1619 struct node *n_ptr;
1620 char addr_string[16];
1621 u32 i;
1622
1623 read_lock_bh(&tipc_net_lock);
1624 n_ptr = tipc_node_find((u32)addr);
1625 if (!n_ptr) {
1626 read_unlock_bh(&tipc_net_lock);
1627 return; /* node no longer exists */
1628 }
1629
1630 tipc_node_lock(n_ptr);
1631
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001632 warn("Resetting all links to %s\n",
Allan Stephensd356eeb2006-06-25 23:40:01 -07001633 addr_string_fill(addr_string, n_ptr->addr));
1634
1635 for (i = 0; i < MAX_BEARERS; i++) {
1636 if (n_ptr->links[i]) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001637 link_print(n_ptr->links[i], TIPC_OUTPUT,
Allan Stephensd356eeb2006-06-25 23:40:01 -07001638 "Resetting link\n");
1639 tipc_link_reset(n_ptr->links[i]);
1640 }
1641 }
1642
1643 tipc_node_unlock(n_ptr);
1644 read_unlock_bh(&tipc_net_lock);
1645}
1646
1647static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1648{
1649 struct tipc_msg *msg = buf_msg(buf);
1650
1651 warn("Retransmission failure on link <%s>\n", l_ptr->name);
1652 tipc_msg_print(TIPC_OUTPUT, msg, ">RETR-FAIL>");
1653
1654 if (l_ptr->addr) {
1655
1656 /* Handle failure on standard link */
1657
1658 link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
1659 tipc_link_reset(l_ptr);
1660
1661 } else {
1662
1663 /* Handle failure on broadcast link */
1664
1665 struct node *n_ptr;
1666 char addr_string[16];
1667
1668 tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg));
Jeff Garzik617dbea2006-10-03 16:25:34 -07001669 tipc_printf(TIPC_OUTPUT, "Outstanding acks: %lu\n",
1670 (unsigned long) TIPC_SKB_CB(buf)->handle);
1671
Allan Stephensd356eeb2006-06-25 23:40:01 -07001672 n_ptr = l_ptr->owner->next;
1673 tipc_node_lock(n_ptr);
1674
1675 addr_string_fill(addr_string, n_ptr->addr);
1676 tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
1677 tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported);
1678 tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
1679 tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in);
1680 tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after);
1681 tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
1682 tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1683
1684 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1685
1686 tipc_node_unlock(n_ptr);
1687
1688 l_ptr->stale_count = 0;
1689 }
1690}
1691
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001692void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001693 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694{
1695 struct tipc_msg *msg;
1696
Allan Stephensd356eeb2006-06-25 23:40:01 -07001697 if (!buf)
1698 return;
1699
1700 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001701
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
1703
Allan Stephensd356eeb2006-06-25 23:40:01 -07001704 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
1705 if (!skb_cloned(buf)) {
1706 msg_dbg(msg, ">NO_RETR->BCONG>");
1707 dbg_print_link(l_ptr, " ");
1708 l_ptr->retransm_queue_head = msg_seqno(msg);
1709 l_ptr->retransm_queue_size = retransmits;
1710 return;
1711 } else {
1712 /* Don't retransmit if driver already has the buffer */
1713 }
1714 } else {
1715 /* Detect repeated retransmit failures on uncongested bearer */
1716
1717 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1718 if (++l_ptr->stale_count > 100) {
1719 link_retransmit_failure(l_ptr, buf);
1720 return;
1721 }
1722 } else {
1723 l_ptr->last_retransmitted = msg_seqno(msg);
1724 l_ptr->stale_count = 1;
1725 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001727
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 while (retransmits && (buf != l_ptr->next_out) && buf && !skb_cloned(buf)) {
1729 msg = buf_msg(buf);
1730 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001731 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001732 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733 msg_dbg(buf_msg(buf), ">RETR>");
1734 buf = buf->next;
1735 retransmits--;
1736 l_ptr->stats.retransmitted++;
1737 } else {
Per Liden4323add2006-01-18 00:38:21 +01001738 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 l_ptr->stats.bearer_congs++;
1740 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1741 l_ptr->retransm_queue_size = retransmits;
1742 return;
1743 }
1744 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001745
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1747}
1748
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001749/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 * link_recv_non_seq: Receive packets which are outside
1751 * the link sequence flow
1752 */
1753
1754static void link_recv_non_seq(struct sk_buff *buf)
1755{
1756 struct tipc_msg *msg = buf_msg(buf);
1757
1758 if (msg_user(msg) == LINK_CONFIG)
Per Liden4323add2006-01-18 00:38:21 +01001759 tipc_disc_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 else
Per Liden4323add2006-01-18 00:38:21 +01001761 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762}
1763
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001764/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 * link_insert_deferred_queue - insert deferred messages back into receive chain
1766 */
1767
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001768static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001769 struct sk_buff *buf)
1770{
1771 u32 seq_no;
1772
1773 if (l_ptr->oldest_deferred_in == NULL)
1774 return buf;
1775
1776 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1777 if (seq_no == mod(l_ptr->next_in_no)) {
1778 l_ptr->newest_deferred_in->next = buf;
1779 buf = l_ptr->oldest_deferred_in;
1780 l_ptr->oldest_deferred_in = NULL;
1781 l_ptr->deferred_inqueue_sz = 0;
1782 }
1783 return buf;
1784}
1785
1786void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1787{
Per Liden4323add2006-01-18 00:38:21 +01001788 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789 while (head) {
1790 struct bearer *b_ptr;
1791 struct node *n_ptr;
1792 struct link *l_ptr;
1793 struct sk_buff *crs;
1794 struct sk_buff *buf = head;
1795 struct tipc_msg *msg = buf_msg(buf);
1796 u32 seq_no = msg_seqno(msg);
1797 u32 ackd = msg_ack(msg);
1798 u32 released = 0;
1799 int type;
1800
1801 b_ptr = (struct bearer *)tb_ptr;
1802 TIPC_SKB_CB(buf)->handle = b_ptr;
1803
1804 head = head->next;
1805 if (unlikely(msg_version(msg) != TIPC_VERSION))
1806 goto cont;
1807#if 0
1808 if (msg_user(msg) != LINK_PROTOCOL)
1809#endif
1810 msg_dbg(msg,"<REC<");
1811
1812 if (unlikely(msg_non_seq(msg))) {
1813 link_recv_non_seq(buf);
1814 continue;
1815 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001816
Allan Stephens26008242006-06-25 23:39:31 -07001817 if (unlikely(!msg_short(msg) &&
1818 (msg_destnode(msg) != tipc_own_addr)))
1819 goto cont;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001820
Per Liden4323add2006-01-18 00:38:21 +01001821 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 if (unlikely(!n_ptr))
1823 goto cont;
1824
Per Liden4323add2006-01-18 00:38:21 +01001825 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 l_ptr = n_ptr->links[b_ptr->identity];
1827 if (unlikely(!l_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001828 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 goto cont;
1830 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001831 /*
1832 * Release acked messages
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 */
1834 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001835 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1836 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 }
1838
1839 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001840 while ((crs != l_ptr->next_out) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1842 struct sk_buff *next = crs->next;
1843
1844 buf_discard(crs);
1845 crs = next;
1846 released++;
1847 }
1848 if (released) {
1849 l_ptr->first_out = crs;
1850 l_ptr->out_queue_size -= released;
1851 }
1852 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001853 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001855 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1857 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001858 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 }
1860
1861protocol_check:
1862 if (likely(link_working_working(l_ptr))) {
1863 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1864 l_ptr->next_in_no++;
1865 if (unlikely(l_ptr->oldest_deferred_in))
1866 head = link_insert_deferred_queue(l_ptr,
1867 head);
1868 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1869deliver:
1870 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001871 tipc_node_unlock(n_ptr);
1872 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 continue;
1874 }
1875 switch (msg_user(msg)) {
1876 case MSG_BUNDLER:
1877 l_ptr->stats.recv_bundles++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001878 l_ptr->stats.recv_bundled +=
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +01001880 tipc_node_unlock(n_ptr);
1881 tipc_link_recv_bundle(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 continue;
1883 case ROUTE_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001884 tipc_node_unlock(n_ptr);
1885 tipc_cltr_recv_routing_table(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 continue;
1887 case NAME_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001888 tipc_node_unlock(n_ptr);
1889 tipc_named_recv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 continue;
1891 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001892 tipc_node_unlock(n_ptr);
1893 tipc_port_recv_proto_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 continue;
1895 case MSG_FRAGMENTER:
1896 l_ptr->stats.recv_fragments++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001897 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
Per Liden4323add2006-01-18 00:38:21 +01001898 &buf, &msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899 l_ptr->stats.recv_fragmented++;
1900 goto deliver;
1901 }
1902 break;
1903 case CHANGEOVER_PROTOCOL:
1904 type = msg_type(msg);
Per Liden4323add2006-01-18 00:38:21 +01001905 if (link_recv_changeover_msg(&l_ptr, &buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906 msg = buf_msg(buf);
1907 seq_no = msg_seqno(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001908 TIPC_SKB_CB(buf)->handle
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909 = b_ptr;
1910 if (type == ORIGINAL_MSG)
1911 goto deliver;
1912 goto protocol_check;
1913 }
1914 break;
1915 }
1916 }
Per Liden4323add2006-01-18 00:38:21 +01001917 tipc_node_unlock(n_ptr);
1918 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919 continue;
1920 }
1921 link_handle_out_of_seq_msg(l_ptr, buf);
1922 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001923 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924 continue;
1925 }
1926
1927 if (msg_user(msg) == LINK_PROTOCOL) {
1928 link_recv_proto_msg(l_ptr, buf);
1929 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001930 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001931 continue;
1932 }
1933 msg_dbg(msg,"NSEQ<REC<");
1934 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1935
1936 if (link_working_working(l_ptr)) {
1937 /* Re-insert in front of queue */
1938 msg_dbg(msg,"RECV-REINS:");
1939 buf->next = head;
1940 head = buf;
Per Liden4323add2006-01-18 00:38:21 +01001941 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942 continue;
1943 }
Per Liden4323add2006-01-18 00:38:21 +01001944 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945cont:
1946 buf_discard(buf);
1947 }
Per Liden4323add2006-01-18 00:38:21 +01001948 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001949}
1950
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001951/*
1952 * link_defer_buf(): Sort a received out-of-sequence packet
Per Lidenb97bf3f2006-01-02 19:04:38 +01001953 * into the deferred reception queue.
1954 * Returns the increase of the queue length,i.e. 0 or 1
1955 */
1956
Per Liden4323add2006-01-18 00:38:21 +01001957u32 tipc_link_defer_pkt(struct sk_buff **head,
1958 struct sk_buff **tail,
1959 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001961 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001962 struct sk_buff *crs = *head;
1963 u32 seq_no = msg_seqno(buf_msg(buf));
1964
1965 buf->next = NULL;
1966
1967 /* Empty queue ? */
1968 if (*head == NULL) {
1969 *head = *tail = buf;
1970 return 1;
1971 }
1972
1973 /* Last ? */
1974 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1975 (*tail)->next = buf;
1976 *tail = buf;
1977 return 1;
1978 }
1979
1980 /* Scan through queue and sort it in */
1981 do {
1982 struct tipc_msg *msg = buf_msg(crs);
1983
1984 if (less(seq_no, msg_seqno(msg))) {
1985 buf->next = crs;
1986 if (prev)
1987 prev->next = buf;
1988 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001989 *head = buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990 return 1;
1991 }
1992 if (seq_no == msg_seqno(msg)) {
1993 break;
1994 }
1995 prev = crs;
1996 crs = crs->next;
1997 }
1998 while (crs);
1999
2000 /* Message is a duplicate of an existing message */
2001
2002 buf_discard(buf);
2003 return 0;
2004}
2005
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002006/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
2008 */
2009
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002010static void link_handle_out_of_seq_msg(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 struct sk_buff *buf)
2012{
2013 u32 seq_no = msg_seqno(buf_msg(buf));
2014
2015 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
2016 link_recv_proto_msg(l_ptr, buf);
2017 return;
2018 }
2019
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002020 dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021 seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
2022
2023 /* Record OOS packet arrival (force mismatch on next timeout) */
2024
2025 l_ptr->checkpoint--;
2026
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002027 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002028 * Discard packet if a duplicate; otherwise add it to deferred queue
2029 * and notify peer of gap as per protocol specification
2030 */
2031
2032 if (less(seq_no, mod(l_ptr->next_in_no))) {
2033 l_ptr->stats.duplicates++;
2034 buf_discard(buf);
2035 return;
2036 }
2037
Per Liden4323add2006-01-18 00:38:21 +01002038 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
2039 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002040 l_ptr->deferred_inqueue_sz++;
2041 l_ptr->stats.deferred_recv++;
2042 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01002043 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044 } else
2045 l_ptr->stats.duplicates++;
2046}
2047
2048/*
2049 * Send protocol message to the other endpoint.
2050 */
Per Liden4323add2006-01-18 00:38:21 +01002051void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
2052 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002054 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002056 u32 msg_size = sizeof(l_ptr->proto_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057
2058 if (link_blocked(l_ptr))
2059 return;
2060 msg_set_type(msg, msg_typ);
2061 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002062 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
Per Liden4323add2006-01-18 00:38:21 +01002063 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064
2065 if (msg_typ == STATE_MSG) {
2066 u32 next_sent = mod(l_ptr->next_out_no);
2067
Per Liden4323add2006-01-18 00:38:21 +01002068 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 return;
2070 if (l_ptr->next_out)
2071 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
2072 msg_set_next_sent(msg, next_sent);
2073 if (l_ptr->oldest_deferred_in) {
2074 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2075 gap = mod(rec - mod(l_ptr->next_in_no));
2076 }
2077 msg_set_seq_gap(msg, gap);
2078 if (gap)
2079 l_ptr->stats.sent_nacks++;
2080 msg_set_link_tolerance(msg, tolerance);
2081 msg_set_linkprio(msg, priority);
2082 msg_set_max_pkt(msg, ack_mtu);
2083 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
2084 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002085 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086 u32 mtu = l_ptr->max_pkt;
2087
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002088 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 link_working_working(l_ptr) &&
2090 l_ptr->fsm_msg_cnt) {
2091 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002092 if (l_ptr->max_pkt_probes == 10) {
2093 l_ptr->max_pkt_target = (msg_size - 4);
2094 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002096 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002098 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099
2100 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002101 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002102 l_ptr->stats.sent_states++;
2103 } else { /* RESET_MSG or ACTIVATE_MSG */
2104 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
2105 msg_set_seq_gap(msg, 0);
2106 msg_set_next_sent(msg, 1);
2107 msg_set_link_tolerance(msg, l_ptr->tolerance);
2108 msg_set_linkprio(msg, l_ptr->priority);
2109 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
2110 }
2111
Per Liden4323add2006-01-18 00:38:21 +01002112 if (tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113 msg_set_redundant_link(msg);
2114 } else {
2115 msg_clear_redundant_link(msg);
2116 }
2117 msg_set_linkprio(msg, l_ptr->priority);
2118
2119 /* Ensure sequence number will not fit : */
2120
2121 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
2122
2123 /* Congestion? */
2124
Per Liden4323add2006-01-18 00:38:21 +01002125 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126 if (!l_ptr->proto_msg_queue) {
2127 l_ptr->proto_msg_queue =
2128 buf_acquire(sizeof(l_ptr->proto_msg));
2129 }
2130 buf = l_ptr->proto_msg_queue;
2131 if (!buf)
2132 return;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002133 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002134 return;
2135 }
2136 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
2137
2138 /* Message can be sent */
2139
2140 msg_dbg(msg, ">>");
2141
2142 buf = buf_acquire(msg_size);
2143 if (!buf)
2144 return;
2145
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002146 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002147 msg_set_size(buf_msg(buf), msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002148
Per Liden4323add2006-01-18 00:38:21 +01002149 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 l_ptr->unacked_window = 0;
2151 buf_discard(buf);
2152 return;
2153 }
2154
2155 /* New congestion */
Per Liden4323add2006-01-18 00:38:21 +01002156 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157 l_ptr->proto_msg_queue = buf;
2158 l_ptr->stats.bearer_congs++;
2159}
2160
2161/*
2162 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002163 * Note that network plane id propagates through the network, and may
2164 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01002165 */
2166
2167static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2168{
2169 u32 rec_gap = 0;
2170 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002171 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002172 u32 msg_tol;
2173 struct tipc_msg *msg = buf_msg(buf);
2174
2175 dbg("AT(%u):", jiffies_to_msecs(jiffies));
2176 msg_dbg(msg, "<<");
2177 if (link_blocked(l_ptr))
2178 goto exit;
2179
2180 /* record unnumbered packet arrival (force mismatch on next timeout) */
2181
2182 l_ptr->checkpoint--;
2183
2184 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2185 if (tipc_own_addr > msg_prevnode(msg))
2186 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2187
2188 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2189
2190 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002191
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192 case RESET_MSG:
2193 if (!link_working_unknown(l_ptr) && l_ptr->peer_session) {
2194 if (msg_session(msg) == l_ptr->peer_session) {
2195 dbg("Duplicate RESET: %u<->%u\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002196 msg_session(msg), l_ptr->peer_session);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002197 break; /* duplicate: ignore */
2198 }
2199 }
2200 /* fall thru' */
2201 case ACTIVATE_MSG:
2202 /* Update link settings according other endpoint's values */
2203
2204 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2205
2206 if ((msg_tol = msg_link_tolerance(msg)) &&
2207 (msg_tol > l_ptr->tolerance))
2208 link_set_supervision_props(l_ptr, msg_tol);
2209
2210 if (msg_linkprio(msg) > l_ptr->priority)
2211 l_ptr->priority = msg_linkprio(msg);
2212
2213 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002214 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002215 if (max_pkt_info < l_ptr->max_pkt_target)
2216 l_ptr->max_pkt_target = max_pkt_info;
2217 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2218 l_ptr->max_pkt = l_ptr->max_pkt_target;
2219 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002220 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002221 }
2222 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2223
2224 link_state_event(l_ptr, msg_type(msg));
2225
2226 l_ptr->peer_session = msg_session(msg);
2227 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2228
2229 /* Synchronize broadcast sequence numbers */
Per Liden4323add2006-01-18 00:38:21 +01002230 if (!tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002231 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2232 }
2233 break;
2234 case STATE_MSG:
2235
2236 if ((msg_tol = msg_link_tolerance(msg)))
2237 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002238
2239 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002240 (msg_linkprio(msg) != l_ptr->priority)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002241 warn("Resetting link <%s>, priority change %u->%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002242 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2243 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01002244 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002245 break;
2246 }
2247 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2248 l_ptr->stats.recv_states++;
2249 if (link_reset_unknown(l_ptr))
2250 break;
2251
2252 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002253 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01002254 mod(l_ptr->next_in_no));
2255 }
2256
2257 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002258 if (max_pkt_ack > l_ptr->max_pkt) {
2259 dbg("Link <%s> updated MTU %u -> %u\n",
2260 l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
2261 l_ptr->max_pkt = max_pkt_ack;
2262 l_ptr->max_pkt_probes = 0;
2263 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002264
2265 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002266 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267 l_ptr->stats.recv_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002268 if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2269 max_pkt_ack = msg_size(msg);
2270 }
2271 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002272
2273 /* Protocol message before retransmits, reduce loss risk */
2274
Per Liden4323add2006-01-18 00:38:21 +01002275 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002276
2277 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01002278 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2279 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002280 }
2281 if (msg_seq_gap(msg)) {
2282 msg_dbg(msg, "With Gap:");
2283 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01002284 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2285 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286 }
2287 break;
2288 default:
2289 msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
2290 }
2291exit:
2292 buf_discard(buf);
2293}
2294
2295
2296/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002297 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01002298 * another bearer. Owner node is locked.
2299 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002300void tipc_link_tunnel(struct link *l_ptr,
2301 struct tipc_msg *tunnel_hdr,
Per Liden4323add2006-01-18 00:38:21 +01002302 struct tipc_msg *msg,
2303 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002304{
2305 struct link *tunnel;
2306 struct sk_buff *buf;
2307 u32 length = msg_size(msg);
2308
2309 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07002310 if (!tipc_link_is_up(tunnel)) {
2311 warn("Link changeover error, "
2312 "tunnel link no longer available\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002313 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002314 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002315 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
2316 buf = buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07002317 if (!buf) {
2318 warn("Link changeover error, "
2319 "unable to send tunnel msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002320 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002321 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002322 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2323 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002324 dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
2325 msg_dbg(buf_msg(buf), ">SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002326 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002327}
2328
2329
2330
2331/*
2332 * changeover(): Send whole message queue via the remaining link
2333 * Owner node is locked.
2334 */
2335
Per Liden4323add2006-01-18 00:38:21 +01002336void tipc_link_changeover(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337{
2338 u32 msgcount = l_ptr->out_queue_size;
2339 struct sk_buff *crs = l_ptr->first_out;
2340 struct link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002341 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002342 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002343
2344 if (!tunnel)
2345 return;
2346
Allan Stephens5392d642006-06-25 23:52:50 -07002347 if (!l_ptr->owner->permit_changeover) {
2348 warn("Link changeover error, "
2349 "peer did not permit changeover\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002351 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002352
2353 msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2354 ORIGINAL_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
2355 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2356 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephens5392d642006-06-25 23:52:50 -07002357 dbg("Link changeover requires %u tunnel messages\n", msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002358
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359 if (!l_ptr->first_out) {
2360 struct sk_buff *buf;
2361
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362 buf = buf_acquire(INT_H_SIZE);
2363 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002364 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002365 msg_set_size(&tunnel_hdr, INT_H_SIZE);
2366 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2367 tunnel->b_ptr->net_plane);
2368 msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002369 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002370 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002371 warn("Link changeover error, "
2372 "unable to send changeover msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002373 }
2374 return;
2375 }
Allan Stephensf1310722006-06-25 23:51:37 -07002376
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002377 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002378 l_ptr->owner->active_links[1]);
2379
Per Lidenb97bf3f2006-01-02 19:04:38 +01002380 while (crs) {
2381 struct tipc_msg *msg = buf_msg(crs);
2382
2383 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
2384 u32 msgcount = msg_msgcnt(msg);
2385 struct tipc_msg *m = msg_get_wrapped(msg);
2386 unchar* pos = (unchar*)m;
2387
2388 while (msgcount--) {
2389 msg_set_seqno(m,msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002390 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2391 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002392 pos += align(msg_size(m));
2393 m = (struct tipc_msg *)pos;
2394 }
2395 } else {
Per Liden4323add2006-01-18 00:38:21 +01002396 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2397 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002398 }
2399 crs = crs->next;
2400 }
2401}
2402
Per Liden4323add2006-01-18 00:38:21 +01002403void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002404{
2405 struct sk_buff *iter;
2406 struct tipc_msg tunnel_hdr;
2407
2408 msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2409 DUPLICATE_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
2410 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2411 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2412 iter = l_ptr->first_out;
2413 while (iter) {
2414 struct sk_buff *outbuf;
2415 struct tipc_msg *msg = buf_msg(iter);
2416 u32 length = msg_size(msg);
2417
2418 if (msg_user(msg) == MSG_BUNDLER)
2419 msg_set_type(msg, CLOSED_MSG);
2420 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002421 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002422 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
2423 outbuf = buf_acquire(length + INT_H_SIZE);
2424 if (outbuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002425 warn("Link changeover error, "
2426 "unable to send duplicate msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002427 return;
2428 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002429 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2430 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2431 length);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002432 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2433 tunnel->b_ptr->net_plane);
2434 msg_dbg(buf_msg(outbuf), ">SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002435 tipc_link_send_buf(tunnel, outbuf);
2436 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 return;
2438 iter = iter->next;
2439 }
2440}
2441
2442
2443
2444/**
2445 * buf_extract - extracts embedded TIPC message from another message
2446 * @skb: encapsulating message buffer
2447 * @from_pos: offset to extract from
2448 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002449 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450 * encapsulating message itself is left unchanged.
2451 */
2452
2453static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2454{
2455 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2456 u32 size = msg_size(msg);
2457 struct sk_buff *eb;
2458
2459 eb = buf_acquire(size);
2460 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002461 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002462 return eb;
2463}
2464
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002465/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002466 * link_recv_changeover_msg(): Receive tunneled packet sent
2467 * via other link. Node is locked. Return extracted buffer.
2468 */
2469
2470static int link_recv_changeover_msg(struct link **l_ptr,
2471 struct sk_buff **buf)
2472{
2473 struct sk_buff *tunnel_buf = *buf;
2474 struct link *dest_link;
2475 struct tipc_msg *msg;
2476 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2477 u32 msg_typ = msg_type(tunnel_msg);
2478 u32 msg_count = msg_msgcnt(tunnel_msg);
2479
2480 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002481 if (!dest_link) {
2482 msg_dbg(tunnel_msg, "NOLINK/<REC<");
2483 goto exit;
2484 }
Allan Stephensf1310722006-06-25 23:51:37 -07002485 if (dest_link == *l_ptr) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002486 err("Unexpected changeover message on link <%s>\n",
Allan Stephensf1310722006-06-25 23:51:37 -07002487 (*l_ptr)->name);
2488 goto exit;
2489 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002490 dbg("%c<-%c:", dest_link->b_ptr->net_plane,
2491 (*l_ptr)->b_ptr->net_plane);
2492 *l_ptr = dest_link;
2493 msg = msg_get_wrapped(tunnel_msg);
2494
2495 if (msg_typ == DUPLICATE_MSG) {
2496 if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
2497 msg_dbg(tunnel_msg, "DROP/<REC<");
2498 goto exit;
2499 }
2500 *buf = buf_extract(tunnel_buf,INT_H_SIZE);
2501 if (*buf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002502 warn("Link changeover error, duplicate msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002503 goto exit;
2504 }
2505 msg_dbg(tunnel_msg, "TNL<REC<");
2506 buf_discard(tunnel_buf);
2507 return 1;
2508 }
2509
2510 /* First original message ?: */
2511
Per Liden4323add2006-01-18 00:38:21 +01002512 if (tipc_link_is_up(dest_link)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002513 msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
Allan Stephensa10bd922006-06-25 23:52:17 -07002514 info("Resetting link <%s>, changeover initiated by peer\n",
2515 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002516 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002517 dest_link->exp_msg_count = msg_count;
Allan Stephens5392d642006-06-25 23:52:50 -07002518 dbg("Expecting %u tunnelled messages\n", msg_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002519 if (!msg_count)
2520 goto exit;
2521 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
2522 msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
2523 dest_link->exp_msg_count = msg_count;
Allan Stephens5392d642006-06-25 23:52:50 -07002524 dbg("Expecting %u tunnelled messages\n", msg_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002525 if (!msg_count)
2526 goto exit;
2527 }
2528
2529 /* Receive original message */
2530
2531 if (dest_link->exp_msg_count == 0) {
Allan Stephens5392d642006-06-25 23:52:50 -07002532 warn("Link switchover error, "
2533 "got too many tunnelled messages\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002534 msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
2535 dbg_print_link(dest_link, "LINK:");
2536 goto exit;
2537 }
2538 dest_link->exp_msg_count--;
2539 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
2540 msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
2541 goto exit;
2542 } else {
2543 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2544 if (*buf != NULL) {
2545 msg_dbg(tunnel_msg, "TNL<REC<");
2546 buf_discard(tunnel_buf);
2547 return 1;
2548 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002549 warn("Link changeover error, original msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550 }
2551 }
2552exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002553 *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554 buf_discard(tunnel_buf);
2555 return 0;
2556}
2557
2558/*
2559 * Bundler functionality:
2560 */
Per Liden4323add2006-01-18 00:38:21 +01002561void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002562{
2563 u32 msgcount = msg_msgcnt(buf_msg(buf));
2564 u32 pos = INT_H_SIZE;
2565 struct sk_buff *obuf;
2566
2567 msg_dbg(buf_msg(buf), "<BNDL<: ");
2568 while (msgcount--) {
2569 obuf = buf_extract(buf, pos);
2570 if (obuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002571 warn("Link unable to unbundle message(s)\n");
2572 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002573 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002574 pos += align(msg_size(buf_msg(obuf)));
2575 msg_dbg(buf_msg(obuf), " /");
Per Liden4323add2006-01-18 00:38:21 +01002576 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002577 }
2578 buf_discard(buf);
2579}
2580
2581/*
2582 * Fragmentation/defragmentation:
2583 */
2584
2585
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002586/*
Per Liden4323add2006-01-18 00:38:21 +01002587 * tipc_link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002588 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002589 * Returns user data length.
2590 */
Per Liden4323add2006-01-18 00:38:21 +01002591int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002592{
2593 struct tipc_msg *inmsg = buf_msg(buf);
2594 struct tipc_msg fragm_hdr;
2595 u32 insize = msg_size(inmsg);
2596 u32 dsz = msg_data_sz(inmsg);
2597 unchar *crs = buf->data;
2598 u32 rest = insize;
2599 u32 pack_sz = link_max_pkt(l_ptr);
2600 u32 fragm_sz = pack_sz - INT_H_SIZE;
2601 u32 fragm_no = 1;
2602 u32 destaddr = msg_destnode(inmsg);
2603
2604 if (msg_short(inmsg))
2605 destaddr = l_ptr->addr;
2606
2607 if (msg_routed(inmsg))
2608 msg_set_prevnode(inmsg, tipc_own_addr);
2609
2610 /* Prepare reusable fragment header: */
2611
2612 msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
2613 TIPC_OK, INT_H_SIZE, destaddr);
2614 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2615 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2616 msg_set_fragm_no(&fragm_hdr, fragm_no);
2617 l_ptr->stats.sent_fragmented++;
2618
2619 /* Chop up message: */
2620
2621 while (rest > 0) {
2622 struct sk_buff *fragm;
2623
2624 if (rest <= fragm_sz) {
2625 fragm_sz = rest;
2626 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2627 }
2628 fragm = buf_acquire(fragm_sz + INT_H_SIZE);
2629 if (fragm == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002630 warn("Link unable to fragment message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002631 dsz = -ENOMEM;
2632 goto exit;
2633 }
2634 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002635 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2636 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2637 fragm_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002638 /* Send queued messages first, if any: */
2639
2640 l_ptr->stats.sent_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01002641 tipc_link_send_buf(l_ptr, fragm);
2642 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002643 return dsz;
2644 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2645 rest -= fragm_sz;
2646 crs += fragm_sz;
2647 msg_set_type(&fragm_hdr, FRAGMENT);
2648 }
2649exit:
2650 buf_discard(buf);
2651 return dsz;
2652}
2653
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002654/*
2655 * A pending message being re-assembled must store certain values
2656 * to handle subsequent fragments correctly. The following functions
Per Lidenb97bf3f2006-01-02 19:04:38 +01002657 * help storing these values in unused, available fields in the
2658 * pending message. This makes dynamic memory allocation unecessary.
2659 */
2660
Sam Ravnborg05790c62006-03-20 22:37:04 -08002661static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002662{
2663 msg_set_seqno(buf_msg(buf), seqno);
2664}
2665
Sam Ravnborg05790c62006-03-20 22:37:04 -08002666static u32 get_fragm_size(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002667{
2668 return msg_ack(buf_msg(buf));
2669}
2670
Sam Ravnborg05790c62006-03-20 22:37:04 -08002671static void set_fragm_size(struct sk_buff *buf, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002672{
2673 msg_set_ack(buf_msg(buf), sz);
2674}
2675
Sam Ravnborg05790c62006-03-20 22:37:04 -08002676static u32 get_expected_frags(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002677{
2678 return msg_bcast_ack(buf_msg(buf));
2679}
2680
Sam Ravnborg05790c62006-03-20 22:37:04 -08002681static void set_expected_frags(struct sk_buff *buf, u32 exp)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002682{
2683 msg_set_bcast_ack(buf_msg(buf), exp);
2684}
2685
Sam Ravnborg05790c62006-03-20 22:37:04 -08002686static u32 get_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002687{
2688 return msg_reroute_cnt(buf_msg(buf));
2689}
2690
Sam Ravnborg05790c62006-03-20 22:37:04 -08002691static void incr_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002692{
2693 msg_incr_reroute_cnt(buf_msg(buf));
2694}
2695
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002696/*
2697 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002698 * the reassembled buffer if message is complete.
2699 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002700int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
Per Liden4323add2006-01-18 00:38:21 +01002701 struct tipc_msg **m)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002702{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002703 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002704 struct sk_buff *fbuf = *fb;
2705 struct tipc_msg *fragm = buf_msg(fbuf);
2706 struct sk_buff *pbuf = *pending;
2707 u32 long_msg_seq_no = msg_long_msgno(fragm);
2708
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002709 *fb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710 msg_dbg(fragm,"FRG<REC<");
2711
2712 /* Is there an incomplete message waiting for this fragment? */
2713
2714 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no)
2715 || (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
2716 prev = pbuf;
2717 pbuf = pbuf->next;
2718 }
2719
2720 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2721 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2722 u32 msg_sz = msg_size(imsg);
2723 u32 fragm_sz = msg_data_sz(fragm);
2724 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2725 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2726 if (msg_type(imsg) == TIPC_MCAST_MSG)
2727 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2728 if (msg_size(imsg) > max) {
2729 msg_dbg(fragm,"<REC<Oversized: ");
2730 buf_discard(fbuf);
2731 return 0;
2732 }
2733 pbuf = buf_acquire(msg_size(imsg));
2734 if (pbuf != NULL) {
2735 pbuf->next = *pending;
2736 *pending = pbuf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002737 skb_copy_to_linear_data(pbuf, imsg,
2738 msg_data_sz(fragm));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002739 /* Prepare buffer for subsequent fragments. */
2740
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002741 set_long_msg_seqno(pbuf, long_msg_seq_no);
2742 set_fragm_size(pbuf,fragm_sz);
2743 set_expected_frags(pbuf,exp_fragm_cnt - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002744 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002745 warn("Link unable to reassemble fragmented message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002746 }
2747 buf_discard(fbuf);
2748 return 0;
2749 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2750 u32 dsz = msg_data_sz(fragm);
2751 u32 fsz = get_fragm_size(pbuf);
2752 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2753 u32 exp_frags = get_expected_frags(pbuf) - 1;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002754 skb_copy_to_linear_data_offset(pbuf, crs,
2755 msg_data(fragm), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002756 buf_discard(fbuf);
2757
2758 /* Is message complete? */
2759
2760 if (exp_frags == 0) {
2761 if (prev)
2762 prev->next = pbuf->next;
2763 else
2764 *pending = pbuf->next;
2765 msg_reset_reroute_cnt(buf_msg(pbuf));
2766 *fb = pbuf;
2767 *m = buf_msg(pbuf);
2768 return 1;
2769 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002770 set_expected_frags(pbuf,exp_frags);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002771 return 0;
2772 }
2773 dbg(" Discarding orphan fragment %x\n",fbuf);
2774 msg_dbg(fragm,"ORPHAN:");
2775 dbg("Pending long buffers:\n");
2776 dbg_print_buf_chain(*pending);
2777 buf_discard(fbuf);
2778 return 0;
2779}
2780
2781/**
2782 * link_check_defragm_bufs - flush stale incoming message fragments
2783 * @l_ptr: pointer to link
2784 */
2785
2786static void link_check_defragm_bufs(struct link *l_ptr)
2787{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002788 struct sk_buff *prev = NULL;
2789 struct sk_buff *next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002790 struct sk_buff *buf = l_ptr->defragm_buf;
2791
2792 if (!buf)
2793 return;
2794 if (!link_working_working(l_ptr))
2795 return;
2796 while (buf) {
2797 u32 cnt = get_timer_cnt(buf);
2798
2799 next = buf->next;
2800 if (cnt < 4) {
2801 incr_timer_cnt(buf);
2802 prev = buf;
2803 } else {
2804 dbg(" Discarding incomplete long buffer\n");
2805 msg_dbg(buf_msg(buf), "LONG:");
2806 dbg_print_link(l_ptr, "curr:");
2807 dbg("Pending long buffers:\n");
2808 dbg_print_buf_chain(l_ptr->defragm_buf);
2809 if (prev)
2810 prev->next = buf->next;
2811 else
2812 l_ptr->defragm_buf = buf->next;
2813 buf_discard(buf);
2814 }
2815 buf = next;
2816 }
2817}
2818
2819
2820
2821static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2822{
2823 l_ptr->tolerance = tolerance;
2824 l_ptr->continuity_interval =
2825 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2826 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2827}
2828
2829
Per Liden4323add2006-01-18 00:38:21 +01002830void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002831{
2832 /* Data messages from this node, inclusive FIRST_FRAGM */
2833 l_ptr->queue_limit[DATA_LOW] = window;
2834 l_ptr->queue_limit[DATA_MEDIUM] = (window / 3) * 4;
2835 l_ptr->queue_limit[DATA_HIGH] = (window / 3) * 5;
2836 l_ptr->queue_limit[DATA_CRITICAL] = (window / 3) * 6;
2837 /* Transiting data messages,inclusive FIRST_FRAGM */
2838 l_ptr->queue_limit[DATA_LOW + 4] = 300;
2839 l_ptr->queue_limit[DATA_MEDIUM + 4] = 600;
2840 l_ptr->queue_limit[DATA_HIGH + 4] = 900;
2841 l_ptr->queue_limit[DATA_CRITICAL + 4] = 1200;
2842 l_ptr->queue_limit[CONN_MANAGER] = 1200;
2843 l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
2844 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2845 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2846 /* FRAGMENT and LAST_FRAGMENT packets */
2847 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2848}
2849
2850/**
2851 * link_find_link - locate link by name
2852 * @name - ptr to link name string
2853 * @node - ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002854 *
Per Liden4323add2006-01-18 00:38:21 +01002855 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002856 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002857 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002858 * Returns pointer to link (or 0 if invalid link name).
2859 */
2860
2861static struct link *link_find_link(const char *name, struct node **node)
2862{
2863 struct link_name link_name_parts;
2864 struct bearer *b_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002865 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002866
2867 if (!link_name_validate(name, &link_name_parts))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002868 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002869
Per Liden4323add2006-01-18 00:38:21 +01002870 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002871 if (!b_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002872 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002873
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002874 *node = tipc_node_find(link_name_parts.addr_peer);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002875 if (!*node)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002876 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002877
2878 l_ptr = (*node)->links[b_ptr->identity];
2879 if (!l_ptr || strcmp(l_ptr->name, name))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002880 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002881
2882 return l_ptr;
2883}
2884
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002885struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002886 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002887{
2888 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002889 u32 new_value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002890 struct link *l_ptr;
2891 struct node *node;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002892 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002893
2894 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002895 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002896
2897 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2898 new_value = ntohl(args->value);
2899
Per Liden4323add2006-01-18 00:38:21 +01002900 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002901 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002902 (tipc_bclink_set_queue_limits(new_value) == 0))
2903 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002904 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002905 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002906 }
2907
Per Liden4323add2006-01-18 00:38:21 +01002908 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002909 l_ptr = link_find_link(args->name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002910 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002911 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002912 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002913 }
2914
Per Liden4323add2006-01-18 00:38:21 +01002915 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002916 res = -EINVAL;
2917 switch (cmd) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002918 case TIPC_CMD_SET_LINK_TOL:
2919 if ((new_value >= TIPC_MIN_LINK_TOL) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002920 (new_value <= TIPC_MAX_LINK_TOL)) {
2921 link_set_supervision_props(l_ptr, new_value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002922 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002923 0, 0, new_value, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002924 res = TIPC_OK;
2925 }
2926 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002927 case TIPC_CMD_SET_LINK_PRI:
Per Liden16cb4b32006-01-13 22:22:22 +01002928 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2929 (new_value <= TIPC_MAX_LINK_PRI)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002930 l_ptr->priority = new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002931 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002932 0, 0, 0, new_value, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002933 res = TIPC_OK;
2934 }
2935 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002936 case TIPC_CMD_SET_LINK_WINDOW:
2937 if ((new_value >= TIPC_MIN_LINK_WIN) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002938 (new_value <= TIPC_MAX_LINK_WIN)) {
Per Liden4323add2006-01-18 00:38:21 +01002939 tipc_link_set_queue_limits(l_ptr, new_value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002940 res = TIPC_OK;
2941 }
2942 break;
2943 }
Per Liden4323add2006-01-18 00:38:21 +01002944 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002945
Per Liden4323add2006-01-18 00:38:21 +01002946 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002947 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002948 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002949
Per Liden4323add2006-01-18 00:38:21 +01002950 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002951}
2952
2953/**
2954 * link_reset_statistics - reset link statistics
2955 * @l_ptr: pointer to link
2956 */
2957
2958static void link_reset_statistics(struct link *l_ptr)
2959{
2960 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2961 l_ptr->stats.sent_info = l_ptr->next_out_no;
2962 l_ptr->stats.recv_info = l_ptr->next_in_no;
2963}
2964
Per Liden4323add2006-01-18 00:38:21 +01002965struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002966{
2967 char *link_name;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002968 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002969 struct node *node;
2970
2971 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002972 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002973
2974 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002975 if (!strcmp(link_name, tipc_bclink_name)) {
2976 if (tipc_bclink_reset_stats())
2977 return tipc_cfg_reply_error_string("link not found");
2978 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002979 }
2980
Per Liden4323add2006-01-18 00:38:21 +01002981 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002982 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002983 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002984 read_unlock_bh(&tipc_net_lock);
2985 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002986 }
2987
Per Liden4323add2006-01-18 00:38:21 +01002988 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002989 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002990 tipc_node_unlock(node);
2991 read_unlock_bh(&tipc_net_lock);
2992 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002993}
2994
2995/**
2996 * percent - convert count to a percentage of total (rounding up or down)
2997 */
2998
2999static u32 percent(u32 count, u32 total)
3000{
3001 return (count * 100 + (total / 2)) / total;
3002}
3003
3004/**
Per Liden4323add2006-01-18 00:38:21 +01003005 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01003006 * @name: link name
3007 * @buf: print buffer area
3008 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003009 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003010 * Returns length of print buffer data string (or 0 if error)
3011 */
3012
Per Liden4323add2006-01-18 00:38:21 +01003013static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003014{
3015 struct print_buf pb;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003016 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003017 struct node *node;
3018 char *status;
3019 u32 profile_total = 0;
3020
Per Liden4323add2006-01-18 00:38:21 +01003021 if (!strcmp(name, tipc_bclink_name))
3022 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003023
Per Liden4323add2006-01-18 00:38:21 +01003024 tipc_printbuf_init(&pb, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003025
Per Liden4323add2006-01-18 00:38:21 +01003026 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003027 l_ptr = link_find_link(name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003028 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003029 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003030 return 0;
3031 }
Per Liden4323add2006-01-18 00:38:21 +01003032 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003033
Per Liden4323add2006-01-18 00:38:21 +01003034 if (tipc_link_is_active(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003035 status = "ACTIVE";
Per Liden4323add2006-01-18 00:38:21 +01003036 else if (tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003037 status = "STANDBY";
3038 else
3039 status = "DEFUNCT";
3040 tipc_printf(&pb, "Link <%s>\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003041 " %s MTU:%u Priority:%u Tolerance:%u ms"
3042 " Window:%u packets\n",
3043 l_ptr->name, status, link_max_pkt(l_ptr),
Per Lidenb97bf3f2006-01-02 19:04:38 +01003044 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003045 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003046 l_ptr->next_in_no - l_ptr->stats.recv_info,
3047 l_ptr->stats.recv_fragments,
3048 l_ptr->stats.recv_fragmented,
3049 l_ptr->stats.recv_bundles,
3050 l_ptr->stats.recv_bundled);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003051 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003052 l_ptr->next_out_no - l_ptr->stats.sent_info,
3053 l_ptr->stats.sent_fragments,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003054 l_ptr->stats.sent_fragmented,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003055 l_ptr->stats.sent_bundles,
3056 l_ptr->stats.sent_bundled);
3057 profile_total = l_ptr->stats.msg_length_counts;
3058 if (!profile_total)
3059 profile_total = 1;
3060 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003061 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
3062 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003063 l_ptr->stats.msg_length_counts,
3064 l_ptr->stats.msg_lengths_total / profile_total,
3065 percent(l_ptr->stats.msg_length_profile[0], profile_total),
3066 percent(l_ptr->stats.msg_length_profile[1], profile_total),
3067 percent(l_ptr->stats.msg_length_profile[2], profile_total),
3068 percent(l_ptr->stats.msg_length_profile[3], profile_total),
3069 percent(l_ptr->stats.msg_length_profile[4], profile_total),
3070 percent(l_ptr->stats.msg_length_profile[5], profile_total),
3071 percent(l_ptr->stats.msg_length_profile[6], profile_total));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003072 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003073 l_ptr->stats.recv_states,
3074 l_ptr->stats.recv_probes,
3075 l_ptr->stats.recv_nacks,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003076 l_ptr->stats.deferred_recv,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003077 l_ptr->stats.duplicates);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003078 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
3079 l_ptr->stats.sent_states,
3080 l_ptr->stats.sent_probes,
3081 l_ptr->stats.sent_nacks,
3082 l_ptr->stats.sent_acks,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003083 l_ptr->stats.retransmitted);
3084 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
3085 l_ptr->stats.bearer_congs,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003086 l_ptr->stats.link_congs,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003087 l_ptr->stats.max_queue_sz,
3088 l_ptr->stats.queue_sz_counts
3089 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
3090 : 0);
3091
Per Liden4323add2006-01-18 00:38:21 +01003092 tipc_node_unlock(node);
3093 read_unlock_bh(&tipc_net_lock);
3094 return tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003095}
3096
3097#define MAX_LINK_STATS_INFO 2000
3098
Per Liden4323add2006-01-18 00:38:21 +01003099struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003100{
3101 struct sk_buff *buf;
3102 struct tlv_desc *rep_tlv;
3103 int str_len;
3104
3105 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01003106 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003107
Per Liden4323add2006-01-18 00:38:21 +01003108 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
Per Lidenb97bf3f2006-01-02 19:04:38 +01003109 if (!buf)
3110 return NULL;
3111
3112 rep_tlv = (struct tlv_desc *)buf->data;
3113
Per Liden4323add2006-01-18 00:38:21 +01003114 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
3115 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003116 if (!str_len) {
3117 buf_discard(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003118 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003119 }
3120
3121 skb_put(buf, TLV_SPACE(str_len));
3122 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
3123
3124 return buf;
3125}
3126
3127#if 0
3128int link_control(const char *name, u32 op, u32 val)
3129{
3130 int res = -EINVAL;
3131 struct link *l_ptr;
3132 u32 bearer_id;
3133 struct node * node;
3134 u32 a;
3135
3136 a = link_name2addr(name, &bearer_id);
Per Liden4323add2006-01-18 00:38:21 +01003137 read_lock_bh(&tipc_net_lock);
3138 node = tipc_node_find(a);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003139 if (node) {
Per Liden4323add2006-01-18 00:38:21 +01003140 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003141 l_ptr = node->links[bearer_id];
3142 if (l_ptr) {
3143 if (op == TIPC_REMOVE_LINK) {
3144 struct bearer *b_ptr = l_ptr->b_ptr;
3145 spin_lock_bh(&b_ptr->publ.lock);
Per Liden4323add2006-01-18 00:38:21 +01003146 tipc_link_delete(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003147 spin_unlock_bh(&b_ptr->publ.lock);
3148 }
3149 if (op == TIPC_CMD_BLOCK_LINK) {
Per Liden4323add2006-01-18 00:38:21 +01003150 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003151 l_ptr->blocked = 1;
3152 }
3153 if (op == TIPC_CMD_UNBLOCK_LINK) {
3154 l_ptr->blocked = 0;
3155 }
3156 res = TIPC_OK;
3157 }
Per Liden4323add2006-01-18 00:38:21 +01003158 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003159 }
Per Liden4323add2006-01-18 00:38:21 +01003160 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003161 return res;
3162}
3163#endif
3164
3165/**
Per Liden4323add2006-01-18 00:38:21 +01003166 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01003167 * @dest: network address of destination node
3168 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003169 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003170 * If no active link can be found, uses default maximum packet size.
3171 */
3172
Per Liden4323add2006-01-18 00:38:21 +01003173u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003174{
3175 struct node *n_ptr;
3176 struct link *l_ptr;
3177 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003178
Per Lidenb97bf3f2006-01-02 19:04:38 +01003179 if (dest == tipc_own_addr)
3180 return MAX_MSG_SIZE;
3181
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003182 read_lock_bh(&tipc_net_lock);
Per Liden4323add2006-01-18 00:38:21 +01003183 n_ptr = tipc_node_select(dest, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003184 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003185 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003186 l_ptr = n_ptr->active_links[selector & 1];
3187 if (l_ptr)
3188 res = link_max_pkt(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01003189 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003190 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003191 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003192 return res;
3193}
3194
3195#if 0
3196static void link_dump_rec_queue(struct link *l_ptr)
3197{
3198 struct sk_buff *crs;
3199
3200 if (!l_ptr->oldest_deferred_in) {
3201 info("Reception queue empty\n");
3202 return;
3203 }
3204 info("Contents of Reception queue:\n");
3205 crs = l_ptr->oldest_deferred_in;
3206 while (crs) {
3207 if (crs->data == (void *)0x0000a3a3) {
3208 info("buffer %x invalid\n", crs);
3209 return;
3210 }
3211 msg_dbg(buf_msg(crs), "In rec queue: \n");
3212 crs = crs->next;
3213 }
3214}
3215#endif
3216
3217static void link_dump_send_queue(struct link *l_ptr)
3218{
3219 if (l_ptr->next_out) {
3220 info("\nContents of unsent queue:\n");
3221 dbg_print_buf_chain(l_ptr->next_out);
3222 }
3223 info("\nContents of send queue:\n");
3224 if (l_ptr->first_out) {
3225 dbg_print_buf_chain(l_ptr->first_out);
3226 }
3227 info("Empty send queue\n");
3228}
3229
3230static void link_print(struct link *l_ptr, struct print_buf *buf,
3231 const char *str)
3232{
3233 tipc_printf(buf, str);
3234 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
3235 return;
3236 tipc_printf(buf, "Link %x<%s>:",
3237 l_ptr->addr, l_ptr->b_ptr->publ.name);
3238 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
3239 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
3240 tipc_printf(buf, "SQUE");
3241 if (l_ptr->first_out) {
3242 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
3243 if (l_ptr->next_out)
3244 tipc_printf(buf, "%u..",
3245 msg_seqno(buf_msg(l_ptr->next_out)));
3246 tipc_printf(buf, "%u]",
3247 msg_seqno(buf_msg
3248 (l_ptr->last_out)), l_ptr->out_queue_size);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003249 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
3250 msg_seqno(buf_msg(l_ptr->first_out)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003251 != (l_ptr->out_queue_size - 1))
3252 || (l_ptr->last_out->next != 0)) {
3253 tipc_printf(buf, "\nSend queue inconsistency\n");
3254 tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
3255 tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
3256 tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
3257 link_dump_send_queue(l_ptr);
3258 }
3259 } else
3260 tipc_printf(buf, "[]");
3261 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
3262 if (l_ptr->oldest_deferred_in) {
3263 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
3264 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
3265 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
3266 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
3267 tipc_printf(buf, ":RQSIZ(%u)",
3268 l_ptr->deferred_inqueue_sz);
3269 }
3270 }
3271 if (link_working_unknown(l_ptr))
3272 tipc_printf(buf, ":WU");
3273 if (link_reset_reset(l_ptr))
3274 tipc_printf(buf, ":RR");
3275 if (link_reset_unknown(l_ptr))
3276 tipc_printf(buf, ":RU");
3277 if (link_working_working(l_ptr))
3278 tipc_printf(buf, ":WW");
3279 tipc_printf(buf, "\n");
3280}
3281