blob: aee6579438c7db29e2b4a043da3afd297d4c5ccd [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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "discover.h"
42#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
44
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090045/*
Allan Stephensa686e682008-06-04 17:29:39 -070046 * Out-of-range value for link session numbers
47 */
48
49#define INVALID_SESSION 0x10000
50
51/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090052 * Limit for deferred reception queue:
Per Lidenb97bf3f2006-01-02 19:04:38 +010053 */
54
55#define DEF_QUEUE_LIMIT 256u
56
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090057/*
58 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010059 */
60
61#define STARTING_EVT 856384768 /* link processing trigger */
62#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
63#define TIMEOUT_EVT 560817u /* link timer expired */
64
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090065/*
66 * The following two 'message types' is really just implementation
67 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010068 * They must not be considered part of the protocol
69 */
70#define OPEN_MSG 0
71#define CLOSED_MSG 1
72
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090073/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010074 * State value stored in 'exp_msg_count'
75 */
76
77#define START_CHANGEOVER 100000u
78
79/**
80 * struct link_name - deconstructed link name
81 * @addr_local: network address of node at this end
82 * @if_local: name of interface at this end
83 * @addr_peer: network address of node at far end
84 * @if_peer: name of interface at far end
85 */
86
87struct link_name {
88 u32 addr_local;
89 char if_local[TIPC_MAX_IF_NAME];
90 u32 addr_peer;
91 char if_peer[TIPC_MAX_IF_NAME];
92};
93
Per Lidenb97bf3f2006-01-02 19:04:38 +010094static void link_handle_out_of_seq_msg(struct link *l_ptr,
95 struct sk_buff *buf);
96static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
97static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
98static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
99static int link_send_sections_long(struct port *sender,
100 struct iovec const *msg_sect,
101 u32 num_sect, u32 destnode);
102static void link_check_defragm_bufs(struct link *l_ptr);
103static void link_state_event(struct link *l_ptr, u32 event);
104static void link_reset_statistics(struct link *l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900105static void link_print(struct link *l_ptr, struct print_buf *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106 const char *str);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000107static void link_start(struct link *l_ptr);
108static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf);
109
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110
111/*
112 * Debugging code used by link routines only
113 *
114 * When debugging link problems on a system that has multiple links,
115 * the standard TIPC debugging routines may not be useful since they
116 * allow the output from multiple links to be intermixed. For this reason
117 * routines of the form "dbg_link_XXX()" have been created that will capture
118 * debug info into a link's personal print buffer, which can then be dumped
Allan Stephensa3df92c2006-10-16 21:49:03 -0700119 * into the TIPC system log (TIPC_LOG) upon request.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 *
121 * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size
122 * of the print buffer used by each link. If LINK_LOG_BUF_SIZE is set to 0,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900123 * the dbg_link_XXX() routines simply send their output to the standard
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful
125 * when there is only a single link in the system being debugged.
126 *
127 * Notes:
Allan Stephensa3df92c2006-10-16 21:49:03 -0700128 * - When enabled, LINK_LOG_BUF_SIZE should be set to at least TIPC_PB_MIN_SIZE
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900129 * - "l_ptr" must be valid when using dbg_link_XXX() macros
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130 */
131
132#define LINK_LOG_BUF_SIZE 0
133
Allan Stephens48c97132008-05-05 01:24:06 -0700134#define dbg_link(fmt, arg...) \
135 do { \
136 if (LINK_LOG_BUF_SIZE) \
137 tipc_printf(&l_ptr->print_buf, fmt, ## arg); \
138 } while (0)
139#define dbg_link_msg(msg, txt) \
140 do { \
141 if (LINK_LOG_BUF_SIZE) \
142 tipc_msg_dbg(&l_ptr->print_buf, msg, txt); \
143 } while (0)
144#define dbg_link_state(txt) \
145 do { \
146 if (LINK_LOG_BUF_SIZE) \
147 link_print(l_ptr, &l_ptr->print_buf, txt); \
148 } while (0)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149#define dbg_link_dump() do { \
150 if (LINK_LOG_BUF_SIZE) { \
151 tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \
Per Liden4323add2006-01-18 00:38:21 +0100152 tipc_printbuf_move(LOG, &l_ptr->print_buf); \
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 } \
154} while (0)
155
Sam Ravnborg05790c62006-03-20 22:37:04 -0800156static void dbg_print_link(struct link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157{
Allan Stephensa3df92c2006-10-16 21:49:03 -0700158 if (DBG_OUTPUT != TIPC_NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 link_print(l_ptr, DBG_OUTPUT, str);
160}
161
Sam Ravnborg05790c62006-03-20 22:37:04 -0800162static void dbg_print_buf_chain(struct sk_buff *root_buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163{
Allan Stephensa3df92c2006-10-16 21:49:03 -0700164 if (DBG_OUTPUT != TIPC_NULL) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 struct sk_buff *buf = root_buf;
166
167 while (buf) {
168 msg_dbg(buf_msg(buf), "In chain: ");
169 buf = buf->next;
170 }
171 }
172}
173
174/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800175 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 */
177
Sam Ravnborg05790c62006-03-20 22:37:04 -0800178static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179{
180 return (i + 3) & ~3u;
181}
182
Sam Ravnborg05790c62006-03-20 22:37:04 -0800183static void link_init_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184{
185 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900186
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
188 if (max_pkt > MAX_MSG_SIZE)
189 max_pkt = MAX_MSG_SIZE;
190
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900191 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
193 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900194 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195 l_ptr->max_pkt = MAX_PKT_DEFAULT;
196
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900197 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198}
199
Sam Ravnborg05790c62006-03-20 22:37:04 -0800200static u32 link_next_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201{
202 if (l_ptr->next_out)
203 return msg_seqno(buf_msg(l_ptr->next_out));
204 return mod(l_ptr->next_out_no);
205}
206
Sam Ravnborg05790c62006-03-20 22:37:04 -0800207static u32 link_last_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208{
209 return mod(link_next_sent(l_ptr) - 1);
210}
211
212/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800213 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 */
215
Per Liden4323add2006-01-18 00:38:21 +0100216int tipc_link_is_up(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217{
218 if (!l_ptr)
219 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000220 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221}
222
Per Liden4323add2006-01-18 00:38:21 +0100223int tipc_link_is_active(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000225 return (l_ptr->owner->active_links[0] == l_ptr) ||
226 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227}
228
229/**
230 * link_name_validate - validate & (optionally) deconstruct link name
231 * @name - ptr to link name string
232 * @name_parts - ptr to area for link name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900233 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 * Returns 1 if link name is valid, otherwise 0.
235 */
236
237static int link_name_validate(const char *name, struct link_name *name_parts)
238{
239 char name_copy[TIPC_MAX_LINK_NAME];
240 char *addr_local;
241 char *if_local;
242 char *addr_peer;
243 char *if_peer;
244 char dummy;
245 u32 z_local, c_local, n_local;
246 u32 z_peer, c_peer, n_peer;
247 u32 if_local_len;
248 u32 if_peer_len;
249
250 /* copy link name & ensure length is OK */
251
252 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
253 /* need above in case non-Posix strncpy() doesn't pad with nulls */
254 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
255 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
256 return 0;
257
258 /* ensure all component parts of link name are present */
259
260 addr_local = name_copy;
261 if ((if_local = strchr(addr_local, ':')) == NULL)
262 return 0;
263 *(if_local++) = 0;
264 if ((addr_peer = strchr(if_local, '-')) == NULL)
265 return 0;
266 *(addr_peer++) = 0;
267 if_local_len = addr_peer - if_local;
268 if ((if_peer = strchr(addr_peer, ':')) == NULL)
269 return 0;
270 *(if_peer++) = 0;
271 if_peer_len = strlen(if_peer) + 1;
272
273 /* validate component parts of link name */
274
275 if ((sscanf(addr_local, "%u.%u.%u%c",
276 &z_local, &c_local, &n_local, &dummy) != 3) ||
277 (sscanf(addr_peer, "%u.%u.%u%c",
278 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
279 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
280 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900281 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
282 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
284 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
285 return 0;
286
287 /* return link name components, if necessary */
288
289 if (name_parts) {
290 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
291 strcpy(name_parts->if_local, if_local);
292 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
293 strcpy(name_parts->if_peer, if_peer);
294 }
295 return 1;
296}
297
298/**
299 * link_timeout - handle expiration of link timer
300 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900301 *
Per Liden4323add2006-01-18 00:38:21 +0100302 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
303 * with tipc_link_delete(). (There is no risk that the node will be deleted by
304 * another thread because tipc_link_delete() always cancels the link timer before
305 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 */
307
308static void link_timeout(struct link *l_ptr)
309{
Per Liden4323add2006-01-18 00:38:21 +0100310 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311
312 /* update counters used in statistical profiling of send traffic */
313
314 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
315 l_ptr->stats.queue_sz_counts++;
316
317 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
318 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
319
320 if (l_ptr->first_out) {
321 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
322 u32 length = msg_size(msg);
323
Joe Perchesf64f9e72009-11-29 16:55:45 -0800324 if ((msg_user(msg) == MSG_FRAGMENTER) &&
325 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 length = msg_size(msg_get_wrapped(msg));
327 }
328 if (length) {
329 l_ptr->stats.msg_lengths_total += length;
330 l_ptr->stats.msg_length_counts++;
331 if (length <= 64)
332 l_ptr->stats.msg_length_profile[0]++;
333 else if (length <= 256)
334 l_ptr->stats.msg_length_profile[1]++;
335 else if (length <= 1024)
336 l_ptr->stats.msg_length_profile[2]++;
337 else if (length <= 4096)
338 l_ptr->stats.msg_length_profile[3]++;
339 else if (length <= 16384)
340 l_ptr->stats.msg_length_profile[4]++;
341 else if (length <= 32768)
342 l_ptr->stats.msg_length_profile[5]++;
343 else
344 l_ptr->stats.msg_length_profile[6]++;
345 }
346 }
347
348 /* do all other link processing performed on a periodic basis */
349
350 link_check_defragm_bufs(l_ptr);
351
352 link_state_event(l_ptr, TIMEOUT_EVT);
353
354 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100355 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356
Per Liden4323add2006-01-18 00:38:21 +0100357 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358}
359
Sam Ravnborg05790c62006-03-20 22:37:04 -0800360static void link_set_timer(struct link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361{
362 k_start_timer(&l_ptr->timer, time);
363}
364
365/**
Per Liden4323add2006-01-18 00:38:21 +0100366 * tipc_link_create - create a new link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 * @b_ptr: pointer to associated bearer
368 * @peer: network address of node at other end of link
369 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900370 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 * Returns pointer to link.
372 */
373
Per Liden4323add2006-01-18 00:38:21 +0100374struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
375 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376{
377 struct link *l_ptr;
378 struct tipc_msg *msg;
379 char *if_name;
380
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700381 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 if (!l_ptr) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700383 warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 return NULL;
385 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386
Florian Westphal945710652007-07-26 00:05:07 -0700387 if (LINK_LOG_BUF_SIZE) {
388 char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
389
390 if (!pb) {
391 kfree(l_ptr);
392 warn("Link creation failed, no memory for print buffer\n");
393 return NULL;
394 }
395 tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
396 }
397
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 l_ptr->addr = peer;
399 if_name = strchr(b_ptr->publ.name, ':') + 1;
400 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
401 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900402 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 if_name,
404 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
405 /* note: peer i/f is appended to link name by reset/activate */
406 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 l_ptr->checkpoint = 1;
408 l_ptr->b_ptr = b_ptr;
409 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
410 l_ptr->state = RESET_UNKNOWN;
411
412 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
413 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000414 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700416 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 msg_set_bearer_id(msg, b_ptr->identity);
418 strcpy((char *)msg_data(msg), if_name);
419
420 l_ptr->priority = b_ptr->priority;
Per Liden4323add2006-01-18 00:38:21 +0100421 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422
423 link_init_max_pkt(l_ptr);
424
425 l_ptr->next_out_no = 1;
426 INIT_LIST_HEAD(&l_ptr->waiting_ports);
427
428 link_reset_statistics(l_ptr);
429
Per Liden4323add2006-01-18 00:38:21 +0100430 l_ptr->owner = tipc_node_attach_link(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 if (!l_ptr->owner) {
Florian Westphal945710652007-07-26 00:05:07 -0700432 if (LINK_LOG_BUF_SIZE)
433 kfree(l_ptr->print_buf.buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 kfree(l_ptr);
435 return NULL;
436 }
437
Florian Westphal945710652007-07-26 00:05:07 -0700438 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
439 list_add_tail(&l_ptr->link_list, &b_ptr->links);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000440 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441
Per Liden4323add2006-01-18 00:38:21 +0100442 dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900444
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 return l_ptr;
446}
447
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900448/**
Per Liden4323add2006-01-18 00:38:21 +0100449 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900451 *
Per Liden4323add2006-01-18 00:38:21 +0100452 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900454 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 */
456
Per Liden4323add2006-01-18 00:38:21 +0100457void tipc_link_delete(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458{
459 if (!l_ptr) {
460 err("Attempt to delete non-existent link\n");
461 return;
462 }
463
Per Liden4323add2006-01-18 00:38:21 +0100464 dbg("tipc_link_delete()\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465
466 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900467
Per Liden4323add2006-01-18 00:38:21 +0100468 tipc_node_lock(l_ptr->owner);
469 tipc_link_reset(l_ptr);
470 tipc_node_detach_link(l_ptr->owner, l_ptr);
471 tipc_link_stop(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 list_del_init(&l_ptr->link_list);
473 if (LINK_LOG_BUF_SIZE)
474 kfree(l_ptr->print_buf.buf);
Per Liden4323add2006-01-18 00:38:21 +0100475 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476 k_term_timer(&l_ptr->timer);
477 kfree(l_ptr);
478}
479
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000480static void link_start(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481{
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000482 dbg("link_start %x\n", l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 link_state_event(l_ptr, STARTING_EVT);
484}
485
486/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900487 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 * @l_ptr: pointer to link
489 * @origport: reference to sending port
490 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900491 *
492 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 * has abated.
494 */
495
496static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
497{
498 struct port *p_ptr;
499
Per Liden4323add2006-01-18 00:38:21 +0100500 spin_lock_bh(&tipc_port_list_lock);
501 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 if (p_ptr) {
503 if (!p_ptr->wakeup)
504 goto exit;
505 if (!list_empty(&p_ptr->wait_list))
506 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 p_ptr->publ.congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000508 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
510 l_ptr->stats.link_congs++;
511exit:
Per Liden4323add2006-01-18 00:38:21 +0100512 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 }
Per Liden4323add2006-01-18 00:38:21 +0100514 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 return -ELINKCONG;
516}
517
Per Liden4323add2006-01-18 00:38:21 +0100518void tipc_link_wakeup_ports(struct link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519{
520 struct port *p_ptr;
521 struct port *temp_p_ptr;
522 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
523
524 if (all)
525 win = 100000;
526 if (win <= 0)
527 return;
Per Liden4323add2006-01-18 00:38:21 +0100528 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 return;
530 if (link_congested(l_ptr))
531 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900532 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 wait_list) {
534 if (win <= 0)
535 break;
536 list_del_init(&p_ptr->wait_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 spin_lock_bh(p_ptr->publ.lock);
538 p_ptr->publ.congested = 0;
539 p_ptr->wakeup(&p_ptr->publ);
540 win -= p_ptr->waiting_pkts;
541 spin_unlock_bh(p_ptr->publ.lock);
542 }
543
544exit:
Per Liden4323add2006-01-18 00:38:21 +0100545 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546}
547
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900548/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 * link_release_outqueue - purge link's outbound message queue
550 * @l_ptr: pointer to link
551 */
552
553static void link_release_outqueue(struct link *l_ptr)
554{
555 struct sk_buff *buf = l_ptr->first_out;
556 struct sk_buff *next;
557
558 while (buf) {
559 next = buf->next;
560 buf_discard(buf);
561 buf = next;
562 }
563 l_ptr->first_out = NULL;
564 l_ptr->out_queue_size = 0;
565}
566
567/**
Per Liden4323add2006-01-18 00:38:21 +0100568 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 * @l_ptr: pointer to link
570 */
571
Per Liden4323add2006-01-18 00:38:21 +0100572void tipc_link_reset_fragments(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573{
574 struct sk_buff *buf = l_ptr->defragm_buf;
575 struct sk_buff *next;
576
577 while (buf) {
578 next = buf->next;
579 buf_discard(buf);
580 buf = next;
581 }
582 l_ptr->defragm_buf = NULL;
583}
584
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900585/**
Per Liden4323add2006-01-18 00:38:21 +0100586 * tipc_link_stop - purge all inbound and outbound messages associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 * @l_ptr: pointer to link
588 */
589
Per Liden4323add2006-01-18 00:38:21 +0100590void tipc_link_stop(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591{
592 struct sk_buff *buf;
593 struct sk_buff *next;
594
595 buf = l_ptr->oldest_deferred_in;
596 while (buf) {
597 next = buf->next;
598 buf_discard(buf);
599 buf = next;
600 }
601
602 buf = l_ptr->first_out;
603 while (buf) {
604 next = buf->next;
605 buf_discard(buf);
606 buf = next;
607 }
608
Per Liden4323add2006-01-18 00:38:21 +0100609 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610
611 buf_discard(l_ptr->proto_msg_queue);
612 l_ptr->proto_msg_queue = NULL;
613}
614
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616#define link_send_event(fcn, l_ptr, up) do { } while (0)
617
Per Liden4323add2006-01-18 00:38:21 +0100618void tipc_link_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619{
620 struct sk_buff *buf;
621 u32 prev_state = l_ptr->state;
622 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700623 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624
Allan Stephensa686e682008-06-04 17:29:39 -0700625 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626
Allan Stephensa686e682008-06-04 17:29:39 -0700627 /* Link is down, accept any session */
628 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900632
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 l_ptr->state = RESET_UNKNOWN;
634 dbg_link_state("Resetting Link\n");
635
636 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
637 return;
638
Per Liden4323add2006-01-18 00:38:21 +0100639 tipc_node_link_down(l_ptr->owner, l_ptr);
640 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000641
Allan Stephens5392d642006-06-25 23:52:50 -0700642 if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 l_ptr->owner->permit_changeover) {
644 l_ptr->reset_checkpoint = checkpoint;
645 l_ptr->exp_msg_count = START_CHANGEOVER;
646 }
647
648 /* Clean up all queues: */
649
650 link_release_outqueue(l_ptr);
651 buf_discard(l_ptr->proto_msg_queue);
652 l_ptr->proto_msg_queue = NULL;
653 buf = l_ptr->oldest_deferred_in;
654 while (buf) {
655 struct sk_buff *next = buf->next;
656 buf_discard(buf);
657 buf = next;
658 }
659 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100660 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661
662 l_ptr->retransm_queue_head = 0;
663 l_ptr->retransm_queue_size = 0;
664 l_ptr->last_out = NULL;
665 l_ptr->first_out = NULL;
666 l_ptr->next_out = NULL;
667 l_ptr->unacked_window = 0;
668 l_ptr->checkpoint = 1;
669 l_ptr->next_out_no = 1;
670 l_ptr->deferred_inqueue_sz = 0;
671 l_ptr->oldest_deferred_in = NULL;
672 l_ptr->newest_deferred_in = NULL;
673 l_ptr->fsm_msg_cnt = 0;
674 l_ptr->stale_count = 0;
675 link_reset_statistics(l_ptr);
676
Per Liden4323add2006-01-18 00:38:21 +0100677 link_send_event(tipc_cfg_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100679 link_send_event(tipc_disc_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680}
681
682
683static void link_activate(struct link *l_ptr)
684{
Allan Stephens5392d642006-06-25 23:52:50 -0700685 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100686 tipc_node_link_up(l_ptr->owner, l_ptr);
687 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
688 link_send_event(tipc_cfg_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100690 link_send_event(tipc_disc_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691}
692
693/**
694 * link_state_event - link finite state machine
695 * @l_ptr: pointer to link
696 * @event: state machine event to process
697 */
698
699static void link_state_event(struct link *l_ptr, unsigned event)
700{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900701 struct link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702 u32 cont_intv = l_ptr->continuity_interval;
703
704 if (!l_ptr->started && (event != STARTING_EVT))
705 return; /* Not yet. */
706
707 if (link_blocked(l_ptr)) {
708 if (event == TIMEOUT_EVT) {
709 link_set_timer(l_ptr, cont_intv);
710 }
711 return; /* Changeover going on */
712 }
713 dbg_link("STATE_EV: <%s> ", l_ptr->name);
714
715 switch (l_ptr->state) {
716 case WORKING_WORKING:
717 dbg_link("WW/");
718 switch (event) {
719 case TRAFFIC_MSG_EVT:
720 dbg_link("TRF-");
721 /* fall through */
722 case ACTIVATE_MSG:
723 dbg_link("ACT\n");
724 break;
725 case TIMEOUT_EVT:
726 dbg_link("TIM ");
727 if (l_ptr->next_in_no != l_ptr->checkpoint) {
728 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100729 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100731 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 l_ptr->fsm_msg_cnt++;
733 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900734 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100735 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736 l_ptr->fsm_msg_cnt++;
737 }
738 link_set_timer(l_ptr, cont_intv);
739 break;
740 }
741 dbg_link(" -> WU\n");
742 l_ptr->state = WORKING_UNKNOWN;
743 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100744 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 l_ptr->fsm_msg_cnt++;
746 link_set_timer(l_ptr, cont_intv / 4);
747 break;
748 case RESET_MSG:
749 dbg_link("RES -> RR\n");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900750 info("Resetting link <%s>, requested by peer\n",
Allan Stephensa10bd922006-06-25 23:52:17 -0700751 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100752 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 l_ptr->state = RESET_RESET;
754 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100755 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756 l_ptr->fsm_msg_cnt++;
757 link_set_timer(l_ptr, cont_intv);
758 break;
759 default:
760 err("Unknown link event %u in WW state\n", event);
761 }
762 break;
763 case WORKING_UNKNOWN:
764 dbg_link("WU/");
765 switch (event) {
766 case TRAFFIC_MSG_EVT:
767 dbg_link("TRF-");
768 case ACTIVATE_MSG:
769 dbg_link("ACT -> WW\n");
770 l_ptr->state = WORKING_WORKING;
771 l_ptr->fsm_msg_cnt = 0;
772 link_set_timer(l_ptr, cont_intv);
773 break;
774 case RESET_MSG:
775 dbg_link("RES -> RR\n");
Allan Stephensa10bd922006-06-25 23:52:17 -0700776 info("Resetting link <%s>, requested by peer "
777 "while probing\n", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100778 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 l_ptr->state = RESET_RESET;
780 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100781 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 l_ptr->fsm_msg_cnt++;
783 link_set_timer(l_ptr, cont_intv);
784 break;
785 case TIMEOUT_EVT:
786 dbg_link("TIM ");
787 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Frans Popa570f092010-03-24 07:57:29 +0000788 dbg_link("-> WW\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 l_ptr->state = WORKING_WORKING;
790 l_ptr->fsm_msg_cnt = 0;
791 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100792 if (tipc_bclink_acks_missing(l_ptr->owner)) {
793 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
794 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 l_ptr->fsm_msg_cnt++;
796 }
797 link_set_timer(l_ptr, cont_intv);
798 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
799 dbg_link("Probing %u/%u,timer = %u ms)\n",
800 l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
801 cont_intv / 4);
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 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 l_ptr->fsm_msg_cnt++;
805 link_set_timer(l_ptr, cont_intv / 4);
806 } else { /* Link has failed */
807 dbg_link("-> RU (%u probes unanswered)\n",
808 l_ptr->fsm_msg_cnt);
Allan Stephensa10bd922006-06-25 23:52:17 -0700809 warn("Resetting link <%s>, peer not responding\n",
810 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100811 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 l_ptr->state = RESET_UNKNOWN;
813 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100814 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
815 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 l_ptr->fsm_msg_cnt++;
817 link_set_timer(l_ptr, cont_intv);
818 }
819 break;
820 default:
821 err("Unknown link event %u in WU state\n", event);
822 }
823 break;
824 case RESET_UNKNOWN:
825 dbg_link("RU/");
826 switch (event) {
827 case TRAFFIC_MSG_EVT:
828 dbg_link("TRF-\n");
829 break;
830 case ACTIVATE_MSG:
831 other = l_ptr->owner->active_links[0];
832 if (other && link_working_unknown(other)) {
833 dbg_link("ACT\n");
834 break;
835 }
836 dbg_link("ACT -> WW\n");
837 l_ptr->state = WORKING_WORKING;
838 l_ptr->fsm_msg_cnt = 0;
839 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100840 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 l_ptr->fsm_msg_cnt++;
842 link_set_timer(l_ptr, cont_intv);
843 break;
844 case RESET_MSG:
Frans Popa570f092010-03-24 07:57:29 +0000845 dbg_link("RES\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 dbg_link(" -> RR\n");
847 l_ptr->state = RESET_RESET;
848 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100849 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 l_ptr->fsm_msg_cnt++;
851 link_set_timer(l_ptr, cont_intv);
852 break;
853 case STARTING_EVT:
854 dbg_link("START-");
855 l_ptr->started = 1;
856 /* fall through */
857 case TIMEOUT_EVT:
Frans Popa570f092010-03-24 07:57:29 +0000858 dbg_link("TIM\n");
Per Liden4323add2006-01-18 00:38:21 +0100859 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860 l_ptr->fsm_msg_cnt++;
861 link_set_timer(l_ptr, cont_intv);
862 break;
863 default:
864 err("Unknown link event %u in RU state\n", event);
865 }
866 break;
867 case RESET_RESET:
868 dbg_link("RR/ ");
869 switch (event) {
870 case TRAFFIC_MSG_EVT:
871 dbg_link("TRF-");
872 /* fall through */
873 case ACTIVATE_MSG:
874 other = l_ptr->owner->active_links[0];
875 if (other && link_working_unknown(other)) {
876 dbg_link("ACT\n");
877 break;
878 }
879 dbg_link("ACT -> WW\n");
880 l_ptr->state = WORKING_WORKING;
881 l_ptr->fsm_msg_cnt = 0;
882 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100883 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884 l_ptr->fsm_msg_cnt++;
885 link_set_timer(l_ptr, cont_intv);
886 break;
887 case RESET_MSG:
888 dbg_link("RES\n");
889 break;
890 case TIMEOUT_EVT:
891 dbg_link("TIM\n");
Per Liden4323add2006-01-18 00:38:21 +0100892 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 l_ptr->fsm_msg_cnt++;
894 link_set_timer(l_ptr, cont_intv);
895 dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
896 break;
897 default:
898 err("Unknown link event %u in RR state\n", event);
899 }
900 break;
901 default:
902 err("Unknown link state %u/%u\n", l_ptr->state, event);
903 }
904}
905
906/*
907 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900908 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 */
910
911static int link_bundle_buf(struct link *l_ptr,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900912 struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 struct sk_buff *buf)
914{
915 struct tipc_msg *bundler_msg = buf_msg(bundler);
916 struct tipc_msg *msg = buf_msg(buf);
917 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700918 u32 bundle_size = msg_size(bundler_msg);
919 u32 to_pos = align(bundle_size);
920 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921
922 if (msg_user(bundler_msg) != MSG_BUNDLER)
923 return 0;
924 if (msg_type(bundler_msg) != OPEN_MSG)
925 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700926 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000928 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700929 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930
Allan Stephense49060c2006-06-29 12:32:46 -0700931 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300932 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 msg_set_size(bundler_msg, to_pos + size);
934 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
935 dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
936 msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
937 msg_dbg(msg, "PACKD:");
938 buf_discard(buf);
939 l_ptr->stats.sent_bundled++;
940 return 1;
941}
942
Sam Ravnborg05790c62006-03-20 22:37:04 -0800943static void link_add_to_outqueue(struct link *l_ptr,
944 struct sk_buff *buf,
945 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946{
947 u32 ack = mod(l_ptr->next_in_no - 1);
948 u32 seqno = mod(l_ptr->next_out_no++);
949
950 msg_set_word(msg, 2, ((ack << 16) | seqno));
951 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
952 buf->next = NULL;
953 if (l_ptr->first_out) {
954 l_ptr->last_out->next = buf;
955 l_ptr->last_out = buf;
956 } else
957 l_ptr->first_out = l_ptr->last_out = buf;
958 l_ptr->out_queue_size++;
959}
960
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900961/*
962 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 * inside TIPC when the 'fast path' in tipc_send_buf
964 * has failed, and from link_send()
965 */
966
Per Liden4323add2006-01-18 00:38:21 +0100967int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968{
969 struct tipc_msg *msg = buf_msg(buf);
970 u32 size = msg_size(msg);
971 u32 dsz = msg_data_sz(msg);
972 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000973 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000975 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976
977 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
978
979 /* Match msg importance against queue limits: */
980
981 if (unlikely(queue_size >= queue_limit)) {
982 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
983 return link_schedule_port(l_ptr, msg_origport(msg),
984 size);
985 }
986 msg_dbg(msg, "TIPC: Congestion, throwing away\n");
987 buf_discard(buf);
988 if (imp > CONN_MANAGER) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700989 warn("Resetting link <%s>, send queue full", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100990 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 }
992 return dsz;
993 }
994
995 /* Fragmentation needed ? */
996
997 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000998 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999
1000 /* Packet can be queued or sent: */
1001
1002 if (queue_size > l_ptr->stats.max_queue_sz)
1003 l_ptr->stats.max_queue_sz = queue_size;
1004
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001005 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 !link_congested(l_ptr))) {
1007 link_add_to_outqueue(l_ptr, buf, msg);
1008
Per Liden4323add2006-01-18 00:38:21 +01001009 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 l_ptr->unacked_window = 0;
1011 } else {
Per Liden4323add2006-01-18 00:38:21 +01001012 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 l_ptr->stats.bearer_congs++;
1014 l_ptr->next_out = buf;
1015 }
1016 return dsz;
1017 }
1018 /* Congestion: can message be bundled ?: */
1019
1020 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
1021 (msg_user(msg) != MSG_FRAGMENTER)) {
1022
1023 /* Try adding message to an existing bundle */
1024
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001025 if (l_ptr->next_out &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001027 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 return dsz;
1029 }
1030
1031 /* Try creating a new bundle */
1032
1033 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001034 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 struct tipc_msg bundler_hdr;
1036
1037 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001038 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -07001039 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001040 skb_copy_to_linear_data(bundler, &bundler_hdr,
1041 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 skb_trim(bundler, INT_H_SIZE);
1043 link_bundle_buf(l_ptr, bundler, buf);
1044 buf = bundler;
1045 msg = buf_msg(buf);
1046 l_ptr->stats.sent_bundles++;
1047 }
1048 }
1049 }
1050 if (!l_ptr->next_out)
1051 l_ptr->next_out = buf;
1052 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001053 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 return dsz;
1055}
1056
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001057/*
1058 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 * not been selected yet, and the the owner node is not locked
1060 * Called by TIPC internal users, e.g. the name distributor
1061 */
1062
Per Liden4323add2006-01-18 00:38:21 +01001063int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064{
1065 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001066 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 int res = -ELINKCONG;
1068
Per Liden4323add2006-01-18 00:38:21 +01001069 read_lock_bh(&tipc_net_lock);
1070 n_ptr = tipc_node_select(dest, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001072 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 l_ptr = n_ptr->active_links[selector & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 if (l_ptr) {
Allan Stephensc33d53b2006-06-25 23:50:30 -07001075 dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
Per Liden4323add2006-01-18 00:38:21 +01001076 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensc33d53b2006-06-25 23:50:30 -07001077 } else {
1078 dbg("Attempt to send msg to unreachable node:\n");
1079 msg_dbg(buf_msg(buf),">>>");
1080 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 }
Per Liden4323add2006-01-18 00:38:21 +01001082 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 } else {
1084 dbg("Attempt to send msg to unknown node:\n");
1085 msg_dbg(buf_msg(buf),">>>");
1086 buf_discard(buf);
1087 }
Per Liden4323add2006-01-18 00:38:21 +01001088 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 return res;
1090}
1091
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001092/*
1093 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094 * destination link is known and the header is complete,
1095 * inclusive total message length. Very time critical.
1096 * Link is locked. Returns user data length.
1097 */
1098
Sam Ravnborg05790c62006-03-20 22:37:04 -08001099static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
1100 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101{
1102 struct tipc_msg *msg = buf_msg(buf);
1103 int res = msg_data_sz(msg);
1104
1105 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +00001106 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
1108 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001109 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
1110 &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 l_ptr->unacked_window = 0;
1112 msg_dbg(msg,"SENT_FAST:");
1113 return res;
1114 }
1115 dbg("failed sent fast...\n");
Per Liden4323add2006-01-18 00:38:21 +01001116 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 l_ptr->stats.bearer_congs++;
1118 l_ptr->next_out = buf;
1119 return res;
1120 }
1121 }
1122 else
Allan Stephens15e979d2010-05-11 14:30:10 +00001123 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 }
Per Liden4323add2006-01-18 00:38:21 +01001125 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126}
1127
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001128/*
1129 * tipc_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 * destination node is known and the header is complete,
1131 * inclusive total message length.
1132 * Returns user data length.
1133 */
1134int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
1135{
1136 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001137 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 int res;
1139 u32 selector = msg_origport(buf_msg(buf)) & 1;
1140 u32 dummy;
1141
1142 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001143 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144
Per Liden4323add2006-01-18 00:38:21 +01001145 read_lock_bh(&tipc_net_lock);
1146 n_ptr = tipc_node_select(destnode, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 if (likely(n_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001148 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 l_ptr = n_ptr->active_links[selector];
1150 dbg("send_fast: buf %x selected %x, destnode = %x\n",
1151 buf, l_ptr, destnode);
1152 if (likely(l_ptr)) {
1153 res = link_send_buf_fast(l_ptr, buf, &dummy);
Per Liden4323add2006-01-18 00:38:21 +01001154 tipc_node_unlock(n_ptr);
1155 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 return res;
1157 }
Per Liden4323add2006-01-18 00:38:21 +01001158 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 }
Per Liden4323add2006-01-18 00:38:21 +01001160 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 res = msg_data_sz(buf_msg(buf));
1162 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1163 return res;
1164}
1165
1166
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001167/*
1168 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001170 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 * Returns user data length or errno.
1172 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001173int tipc_link_send_sections_fast(struct port *sender,
Per Liden4323add2006-01-18 00:38:21 +01001174 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001175 const u32 num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001176 u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177{
1178 struct tipc_msg *hdr = &sender->publ.phdr;
1179 struct link *l_ptr;
1180 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -07001181 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 int res;
1183 u32 selector = msg_origport(hdr) & 1;
1184
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185again:
1186 /*
1187 * Try building message using port's max_pkt hint.
1188 * (Must not hold any locks while building message.)
1189 */
1190
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001191 res = tipc_msg_build(hdr, msg_sect, num_sect, sender->publ.max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 !sender->user_port, &buf);
1193
Per Liden4323add2006-01-18 00:38:21 +01001194 read_lock_bh(&tipc_net_lock);
1195 node = tipc_node_select(destaddr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001197 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 l_ptr = node->active_links[selector];
1199 if (likely(l_ptr)) {
1200 if (likely(buf)) {
1201 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens05646c92007-06-10 17:25:24 -07001202 &sender->publ.max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 if (unlikely(res < 0))
1204 buf_discard(buf);
1205exit:
Per Liden4323add2006-01-18 00:38:21 +01001206 tipc_node_unlock(node);
1207 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 return res;
1209 }
1210
1211 /* Exit if build request was invalid */
1212
1213 if (unlikely(res < 0))
1214 goto exit;
1215
1216 /* Exit if link (or bearer) is congested */
1217
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001218 if (link_congested(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 !list_empty(&l_ptr->b_ptr->cong_links)) {
1220 res = link_schedule_port(l_ptr,
1221 sender->publ.ref, res);
1222 goto exit;
1223 }
1224
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001225 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 * Message size exceeds max_pkt hint; update hint,
1227 * then re-try fast path or fragment the message
1228 */
1229
Allan Stephens15e979d2010-05-11 14:30:10 +00001230 sender->publ.max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001231 tipc_node_unlock(node);
1232 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233
1234
Allan Stephens05646c92007-06-10 17:25:24 -07001235 if ((msg_hdr_sz(hdr) + res) <= sender->publ.max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 goto again;
1237
1238 return link_send_sections_long(sender, msg_sect,
1239 num_sect, destaddr);
1240 }
Per Liden4323add2006-01-18 00:38:21 +01001241 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 }
Per Liden4323add2006-01-18 00:38:21 +01001243 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244
1245 /* Couldn't find a link to the destination node */
1246
1247 if (buf)
1248 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1249 if (res >= 0)
Per Liden4323add2006-01-18 00:38:21 +01001250 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1251 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 return res;
1253}
1254
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001255/*
1256 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001258 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 * Link and bearer congestion status have been checked to be ok,
1260 * and are ignored if they change.
1261 *
1262 * Note that fragments do not use the full link MTU so that they won't have
1263 * to undergo refragmentation if link changeover causes them to be sent
1264 * over another link with an additional tunnel header added as prefix.
1265 * (Refragmentation will still occur if the other link has a smaller MTU.)
1266 *
1267 * Returns user data length or errno.
1268 */
1269static int link_send_sections_long(struct port *sender,
1270 struct iovec const *msg_sect,
1271 u32 num_sect,
1272 u32 destaddr)
1273{
1274 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001275 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 struct tipc_msg *hdr = &sender->publ.phdr;
1277 u32 dsz = msg_data_sz(hdr);
1278 u32 max_pkt,fragm_sz,rest;
1279 struct tipc_msg fragm_hdr;
1280 struct sk_buff *buf,*buf_chain,*prev;
1281 u32 fragm_crs,fragm_rest,hsz,sect_rest;
1282 const unchar *sect_crs;
1283 int curr_sect;
1284 u32 fragm_no;
1285
1286again:
1287 fragm_no = 1;
Allan Stephens05646c92007-06-10 17:25:24 -07001288 max_pkt = sender->publ.max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001290 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291 /* leave room for fragmentation header in each fragment */
1292 rest = dsz;
1293 fragm_crs = 0;
1294 fragm_rest = 0;
1295 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001296 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 curr_sect = -1;
1298
1299 /* Prepare reusable fragment header: */
1300
1301 msg_dbg(hdr, ">FRAGMENTING>");
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001302 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001303 INT_H_SIZE, msg_destnode(hdr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1305 msg_set_size(&fragm_hdr, max_pkt);
1306 msg_set_fragm_no(&fragm_hdr, 1);
1307
1308 /* Prepare header of first fragment: */
1309
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001310 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 if (!buf)
1312 return -ENOMEM;
1313 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001314 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001316 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 msg_dbg(buf_msg(buf), ">BUILD>");
1318
1319 /* Chop up message: */
1320
1321 fragm_crs = INT_H_SIZE + hsz;
1322 fragm_rest = fragm_sz - hsz;
1323
1324 do { /* For all sections */
1325 u32 sz;
1326
1327 if (!sect_rest) {
1328 sect_rest = msg_sect[++curr_sect].iov_len;
1329 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1330 }
1331
1332 if (sect_rest < fragm_rest)
1333 sz = sect_rest;
1334 else
1335 sz = fragm_rest;
1336
1337 if (likely(!sender->user_port)) {
1338 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1339error:
1340 for (; buf_chain; buf_chain = buf) {
1341 buf = buf_chain->next;
1342 buf_discard(buf_chain);
1343 }
1344 return -EFAULT;
1345 }
1346 } else
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001347 skb_copy_to_linear_data_offset(buf, fragm_crs,
1348 sect_crs, sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 sect_crs += sz;
1350 sect_rest -= sz;
1351 fragm_crs += sz;
1352 fragm_rest -= sz;
1353 rest -= sz;
1354
1355 if (!fragm_rest && rest) {
1356
1357 /* Initiate new fragment: */
1358 if (rest <= fragm_sz) {
1359 fragm_sz = rest;
1360 msg_set_type(&fragm_hdr,LAST_FRAGMENT);
1361 } else {
1362 msg_set_type(&fragm_hdr, FRAGMENT);
1363 }
1364 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1365 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1366 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001367 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 if (!buf)
1369 goto error;
1370
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001371 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001373 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 fragm_crs = INT_H_SIZE;
1375 fragm_rest = fragm_sz;
1376 msg_dbg(buf_msg(buf)," >BUILD>");
1377 }
1378 }
1379 while (rest > 0);
1380
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001381 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 * Now we have a buffer chain. Select a link and check
1383 * that packet size is still OK
1384 */
Per Liden4323add2006-01-18 00:38:21 +01001385 node = tipc_node_select(destaddr, sender->publ.ref & 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001387 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 l_ptr = node->active_links[sender->publ.ref & 1];
1389 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001390 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391 goto reject;
1392 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001393 if (l_ptr->max_pkt < max_pkt) {
1394 sender->publ.max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001395 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 for (; buf_chain; buf_chain = buf) {
1397 buf = buf_chain->next;
1398 buf_discard(buf_chain);
1399 }
1400 goto again;
1401 }
1402 } else {
1403reject:
1404 for (; buf_chain; buf_chain = buf) {
1405 buf = buf_chain->next;
1406 buf_discard(buf_chain);
1407 }
Per Liden4323add2006-01-18 00:38:21 +01001408 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1409 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 }
1411
1412 /* Append whole chain to send queue: */
1413
1414 buf = buf_chain;
1415 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1416 if (!l_ptr->next_out)
1417 l_ptr->next_out = buf_chain;
1418 l_ptr->stats.sent_fragmented++;
1419 while (buf) {
1420 struct sk_buff *next = buf->next;
1421 struct tipc_msg *msg = buf_msg(buf);
1422
1423 l_ptr->stats.sent_fragments++;
1424 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1425 link_add_to_outqueue(l_ptr, buf, msg);
1426 msg_dbg(msg, ">ADD>");
1427 buf = next;
1428 }
1429
1430 /* Send it, if possible: */
1431
Per Liden4323add2006-01-18 00:38:21 +01001432 tipc_link_push_queue(l_ptr);
1433 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 return dsz;
1435}
1436
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001437/*
Per Liden4323add2006-01-18 00:38:21 +01001438 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 */
Per Liden4323add2006-01-18 00:38:21 +01001440u32 tipc_link_push_packet(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441{
1442 struct sk_buff *buf = l_ptr->first_out;
1443 u32 r_q_size = l_ptr->retransm_queue_size;
1444 u32 r_q_head = l_ptr->retransm_queue_head;
1445
1446 /* Step to position where retransmission failed, if any, */
1447 /* consider that buffers may have been released in meantime */
1448
1449 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001450 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 link_last_sent(l_ptr));
1452 u32 first = msg_seqno(buf_msg(buf));
1453
1454 while (buf && less(first, r_q_head)) {
1455 first = mod(first + 1);
1456 buf = buf->next;
1457 }
1458 l_ptr->retransm_queue_head = r_q_head = first;
1459 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1460 }
1461
1462 /* Continue retransmission now, if there is anything: */
1463
Neil Hormanca509102010-03-15 07:58:45 +00001464 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001466 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001467 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 msg_dbg(buf_msg(buf), ">DEF-RETR>");
1469 l_ptr->retransm_queue_head = mod(++r_q_head);
1470 l_ptr->retransm_queue_size = --r_q_size;
1471 l_ptr->stats.retransmitted++;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001472 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 } else {
1474 l_ptr->stats.bearer_congs++;
1475 msg_dbg(buf_msg(buf), "|>DEF-RETR>");
1476 return PUSH_FAILED;
1477 }
1478 }
1479
1480 /* Send deferred protocol message, if any: */
1481
1482 buf = l_ptr->proto_msg_queue;
1483 if (buf) {
1484 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001485 msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001486 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 msg_dbg(buf_msg(buf), ">DEF-PROT>");
1488 l_ptr->unacked_window = 0;
1489 buf_discard(buf);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001490 l_ptr->proto_msg_queue = NULL;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001491 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492 } else {
1493 msg_dbg(buf_msg(buf), "|>DEF-PROT>");
1494 l_ptr->stats.bearer_congs++;
1495 return PUSH_FAILED;
1496 }
1497 }
1498
1499 /* Send one deferred data message, if send window not full: */
1500
1501 buf = l_ptr->next_out;
1502 if (buf) {
1503 struct tipc_msg *msg = buf_msg(buf);
1504 u32 next = msg_seqno(msg);
1505 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1506
1507 if (mod(next - first) < l_ptr->queue_limit[0]) {
1508 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001510 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 if (msg_user(msg) == MSG_BUNDLER)
1512 msg_set_type(msg, CLOSED_MSG);
1513 msg_dbg(msg, ">PUSH-DATA>");
1514 l_ptr->next_out = buf->next;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001515 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 } else {
1517 msg_dbg(msg, "|PUSH-DATA|");
1518 l_ptr->stats.bearer_congs++;
1519 return PUSH_FAILED;
1520 }
1521 }
1522 }
1523 return PUSH_FINISHED;
1524}
1525
1526/*
1527 * push_queue(): push out the unsent messages of a link where
1528 * congestion has abated. Node is locked
1529 */
Per Liden4323add2006-01-18 00:38:21 +01001530void tipc_link_push_queue(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001531{
1532 u32 res;
1533
Per Liden4323add2006-01-18 00:38:21 +01001534 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 return;
1536
1537 do {
Per Liden4323add2006-01-18 00:38:21 +01001538 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001539 } while (!res);
1540
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 if (res == PUSH_FAILED)
Per Liden4323add2006-01-18 00:38:21 +01001542 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001543}
1544
Allan Stephensd356eeb2006-06-25 23:40:01 -07001545static void link_reset_all(unsigned long addr)
1546{
David S. Miller6c000552008-09-02 23:38:32 -07001547 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001548 char addr_string[16];
1549 u32 i;
1550
1551 read_lock_bh(&tipc_net_lock);
1552 n_ptr = tipc_node_find((u32)addr);
1553 if (!n_ptr) {
1554 read_unlock_bh(&tipc_net_lock);
1555 return; /* node no longer exists */
1556 }
1557
1558 tipc_node_lock(n_ptr);
1559
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001560 warn("Resetting all links to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001561 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001562
1563 for (i = 0; i < MAX_BEARERS; i++) {
1564 if (n_ptr->links[i]) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001565 link_print(n_ptr->links[i], TIPC_OUTPUT,
Allan Stephensd356eeb2006-06-25 23:40:01 -07001566 "Resetting link\n");
1567 tipc_link_reset(n_ptr->links[i]);
1568 }
1569 }
1570
1571 tipc_node_unlock(n_ptr);
1572 read_unlock_bh(&tipc_net_lock);
1573}
1574
1575static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1576{
1577 struct tipc_msg *msg = buf_msg(buf);
1578
1579 warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephens48c97132008-05-05 01:24:06 -07001580 tipc_msg_dbg(TIPC_OUTPUT, msg, ">RETR-FAIL>");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001581
1582 if (l_ptr->addr) {
1583
1584 /* Handle failure on standard link */
1585
1586 link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
1587 tipc_link_reset(l_ptr);
1588
1589 } else {
1590
1591 /* Handle failure on broadcast link */
1592
David S. Miller6c000552008-09-02 23:38:32 -07001593 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001594 char addr_string[16];
1595
1596 tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg));
Jeff Garzik617dbea2006-10-03 16:25:34 -07001597 tipc_printf(TIPC_OUTPUT, "Outstanding acks: %lu\n",
1598 (unsigned long) TIPC_SKB_CB(buf)->handle);
1599
Allan Stephensd356eeb2006-06-25 23:40:01 -07001600 n_ptr = l_ptr->owner->next;
1601 tipc_node_lock(n_ptr);
1602
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001603 tipc_addr_string_fill(addr_string, n_ptr->addr);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001604 tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
1605 tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported);
1606 tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
1607 tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in);
1608 tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after);
1609 tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
1610 tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1611
1612 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1613
1614 tipc_node_unlock(n_ptr);
1615
1616 l_ptr->stale_count = 0;
1617 }
1618}
1619
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001620void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001621 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622{
1623 struct tipc_msg *msg;
1624
Allan Stephensd356eeb2006-06-25 23:40:01 -07001625 if (!buf)
1626 return;
1627
1628 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001629
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
1631
Allan Stephensd356eeb2006-06-25 23:40:01 -07001632 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Neil Hormanca509102010-03-15 07:58:45 +00001633 if (l_ptr->retransm_queue_size == 0) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001634 msg_dbg(msg, ">NO_RETR->BCONG>");
1635 dbg_print_link(l_ptr, " ");
1636 l_ptr->retransm_queue_head = msg_seqno(msg);
1637 l_ptr->retransm_queue_size = retransmits;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001638 } else {
Neil Hormanca509102010-03-15 07:58:45 +00001639 err("Unexpected retransmit on link %s (qsize=%d)\n",
1640 l_ptr->name, l_ptr->retransm_queue_size);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001641 }
Neil Hormanca509102010-03-15 07:58:45 +00001642 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001643 } else {
1644 /* Detect repeated retransmit failures on uncongested bearer */
1645
1646 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1647 if (++l_ptr->stale_count > 100) {
1648 link_retransmit_failure(l_ptr, buf);
1649 return;
1650 }
1651 } else {
1652 l_ptr->last_retransmitted = msg_seqno(msg);
1653 l_ptr->stale_count = 1;
1654 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001656
Neil Hormanca509102010-03-15 07:58:45 +00001657 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 msg = buf_msg(buf);
1659 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001660 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001661 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662 msg_dbg(buf_msg(buf), ">RETR>");
1663 buf = buf->next;
1664 retransmits--;
1665 l_ptr->stats.retransmitted++;
1666 } else {
Per Liden4323add2006-01-18 00:38:21 +01001667 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668 l_ptr->stats.bearer_congs++;
1669 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1670 l_ptr->retransm_queue_size = retransmits;
1671 return;
1672 }
1673 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001674
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1676}
1677
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001678/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 * link_insert_deferred_queue - insert deferred messages back into receive chain
1680 */
1681
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001682static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 struct sk_buff *buf)
1684{
1685 u32 seq_no;
1686
1687 if (l_ptr->oldest_deferred_in == NULL)
1688 return buf;
1689
1690 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1691 if (seq_no == mod(l_ptr->next_in_no)) {
1692 l_ptr->newest_deferred_in->next = buf;
1693 buf = l_ptr->oldest_deferred_in;
1694 l_ptr->oldest_deferred_in = NULL;
1695 l_ptr->deferred_inqueue_sz = 0;
1696 }
1697 return buf;
1698}
1699
Allan Stephens85035562008-04-15 19:04:54 -07001700/**
1701 * link_recv_buf_validate - validate basic format of received message
1702 *
1703 * This routine ensures a TIPC message has an acceptable header, and at least
1704 * as much data as the header indicates it should. The routine also ensures
1705 * that the entire message header is stored in the main fragment of the message
1706 * buffer, to simplify future access to message header fields.
1707 *
1708 * Note: Having extra info present in the message header or data areas is OK.
1709 * TIPC will ignore the excess, under the assumption that it is optional info
1710 * introduced by a later release of the protocol.
1711 */
1712
1713static int link_recv_buf_validate(struct sk_buff *buf)
1714{
1715 static u32 min_data_hdr_size[8] = {
1716 SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
1717 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1718 };
1719
1720 struct tipc_msg *msg;
1721 u32 tipc_hdr[2];
1722 u32 size;
1723 u32 hdr_size;
1724 u32 min_hdr_size;
1725
1726 if (unlikely(buf->len < MIN_H_SIZE))
1727 return 0;
1728
1729 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1730 if (msg == NULL)
1731 return 0;
1732
1733 if (unlikely(msg_version(msg) != TIPC_VERSION))
1734 return 0;
1735
1736 size = msg_size(msg);
1737 hdr_size = msg_hdr_sz(msg);
1738 min_hdr_size = msg_isdata(msg) ?
1739 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1740
1741 if (unlikely((hdr_size < min_hdr_size) ||
1742 (size < hdr_size) ||
1743 (buf->len < size) ||
1744 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1745 return 0;
1746
1747 return pskb_may_pull(buf, hdr_size);
1748}
1749
Allan Stephensb02b69c2010-08-17 11:00:07 +00001750/**
1751 * tipc_recv_msg - process TIPC messages arriving from off-node
1752 * @head: pointer to message buffer chain
1753 * @tb_ptr: pointer to bearer message arrived on
1754 *
1755 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1756 * structure (i.e. cannot be NULL), but bearer can be inactive.
1757 */
1758
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1760{
Per Liden4323add2006-01-18 00:38:21 +01001761 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762 while (head) {
Allan Stephens1265a022008-06-04 17:32:35 -07001763 struct bearer *b_ptr = (struct bearer *)tb_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001764 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 struct link *l_ptr;
1766 struct sk_buff *crs;
1767 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001768 struct tipc_msg *msg;
1769 u32 seq_no;
1770 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 u32 released = 0;
1772 int type;
1773
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 head = head->next;
Allan Stephens85035562008-04-15 19:04:54 -07001775
Allan Stephensb02b69c2010-08-17 11:00:07 +00001776 /* Ensure bearer is still enabled */
1777
1778 if (unlikely(!b_ptr->active))
1779 goto cont;
1780
Allan Stephens85035562008-04-15 19:04:54 -07001781 /* Ensure message is well-formed */
1782
1783 if (unlikely(!link_recv_buf_validate(buf)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 goto cont;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001785
Allan Stephensfe13dda2008-04-15 19:03:23 -07001786 /* Ensure message data is a single contiguous unit */
1787
1788 if (unlikely(buf_linearize(buf))) {
1789 goto cont;
1790 }
1791
Allan Stephens85035562008-04-15 19:04:54 -07001792 /* Handle arrival of a non-unicast link message */
1793
1794 msg = buf_msg(buf);
1795
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001797 if (msg_user(msg) == LINK_CONFIG)
1798 tipc_disc_recv_msg(buf, b_ptr);
1799 else
1800 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 continue;
1802 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001803
Allan Stephens26008242006-06-25 23:39:31 -07001804 if (unlikely(!msg_short(msg) &&
1805 (msg_destnode(msg) != tipc_own_addr)))
1806 goto cont;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001807
Neil Hormande586572010-03-08 12:43:56 -08001808 /* Discard non-routeable messages destined for another node */
1809
1810 if (unlikely(!msg_isdata(msg) &&
1811 (msg_destnode(msg) != tipc_own_addr))) {
1812 if ((msg_user(msg) != CONN_MANAGER) &&
1813 (msg_user(msg) != MSG_FRAGMENTER))
1814 goto cont;
1815 }
1816
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001817 /* Locate neighboring node that sent message */
Allan Stephens85035562008-04-15 19:04:54 -07001818
Per Liden4323add2006-01-18 00:38:21 +01001819 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820 if (unlikely(!n_ptr))
1821 goto cont;
Per Liden4323add2006-01-18 00:38:21 +01001822 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001823
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001824 /* Don't talk to neighbor during cleanup after last session */
1825
1826 if (n_ptr->cleanup_required) {
1827 tipc_node_unlock(n_ptr);
1828 goto cont;
1829 }
1830
1831 /* Locate unicast link endpoint that should handle message */
1832
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 l_ptr = n_ptr->links[b_ptr->identity];
1834 if (unlikely(!l_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001835 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001836 goto cont;
1837 }
Allan Stephens85035562008-04-15 19:04:54 -07001838
1839 /* Validate message sequence number info */
1840
1841 seq_no = msg_seqno(msg);
1842 ackd = msg_ack(msg);
1843
1844 /* Release acked messages */
1845
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001847 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1848 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849 }
1850
1851 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001852 while ((crs != l_ptr->next_out) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1854 struct sk_buff *next = crs->next;
1855
1856 buf_discard(crs);
1857 crs = next;
1858 released++;
1859 }
1860 if (released) {
1861 l_ptr->first_out = crs;
1862 l_ptr->out_queue_size -= released;
1863 }
Allan Stephens85035562008-04-15 19:04:54 -07001864
1865 /* Try sending any messages link endpoint has pending */
1866
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001868 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001870 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1872 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001873 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874 }
1875
Allan Stephens85035562008-04-15 19:04:54 -07001876 /* Now (finally!) process the incoming message */
1877
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878protocol_check:
1879 if (likely(link_working_working(l_ptr))) {
1880 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1881 l_ptr->next_in_no++;
1882 if (unlikely(l_ptr->oldest_deferred_in))
1883 head = link_insert_deferred_queue(l_ptr,
1884 head);
1885 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1886deliver:
1887 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001888 tipc_node_unlock(n_ptr);
1889 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 continue;
1891 }
1892 switch (msg_user(msg)) {
1893 case MSG_BUNDLER:
1894 l_ptr->stats.recv_bundles++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001895 l_ptr->stats.recv_bundled +=
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896 msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +01001897 tipc_node_unlock(n_ptr);
1898 tipc_link_recv_bundle(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899 continue;
1900 case ROUTE_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001901 tipc_node_unlock(n_ptr);
1902 tipc_cltr_recv_routing_table(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 continue;
1904 case NAME_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001905 tipc_node_unlock(n_ptr);
1906 tipc_named_recv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 continue;
1908 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001909 tipc_node_unlock(n_ptr);
1910 tipc_port_recv_proto_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 continue;
1912 case MSG_FRAGMENTER:
1913 l_ptr->stats.recv_fragments++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001914 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
Per Liden4323add2006-01-18 00:38:21 +01001915 &buf, &msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 l_ptr->stats.recv_fragmented++;
1917 goto deliver;
1918 }
1919 break;
1920 case CHANGEOVER_PROTOCOL:
1921 type = msg_type(msg);
Per Liden4323add2006-01-18 00:38:21 +01001922 if (link_recv_changeover_msg(&l_ptr, &buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923 msg = buf_msg(buf);
1924 seq_no = msg_seqno(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925 if (type == ORIGINAL_MSG)
1926 goto deliver;
1927 goto protocol_check;
1928 }
1929 break;
1930 }
1931 }
Per Liden4323add2006-01-18 00:38:21 +01001932 tipc_node_unlock(n_ptr);
1933 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 continue;
1935 }
1936 link_handle_out_of_seq_msg(l_ptr, buf);
1937 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001938 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939 continue;
1940 }
1941
1942 if (msg_user(msg) == LINK_PROTOCOL) {
1943 link_recv_proto_msg(l_ptr, buf);
1944 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001945 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 continue;
1947 }
1948 msg_dbg(msg,"NSEQ<REC<");
1949 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1950
1951 if (link_working_working(l_ptr)) {
1952 /* Re-insert in front of queue */
1953 msg_dbg(msg,"RECV-REINS:");
1954 buf->next = head;
1955 head = buf;
Per Liden4323add2006-01-18 00:38:21 +01001956 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001957 continue;
1958 }
Per Liden4323add2006-01-18 00:38:21 +01001959 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960cont:
1961 buf_discard(buf);
1962 }
Per Liden4323add2006-01-18 00:38:21 +01001963 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964}
1965
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001966/*
1967 * link_defer_buf(): Sort a received out-of-sequence packet
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 * into the deferred reception queue.
1969 * Returns the increase of the queue length,i.e. 0 or 1
1970 */
1971
Per Liden4323add2006-01-18 00:38:21 +01001972u32 tipc_link_defer_pkt(struct sk_buff **head,
1973 struct sk_buff **tail,
1974 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001976 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 struct sk_buff *crs = *head;
1978 u32 seq_no = msg_seqno(buf_msg(buf));
1979
1980 buf->next = NULL;
1981
1982 /* Empty queue ? */
1983 if (*head == NULL) {
1984 *head = *tail = buf;
1985 return 1;
1986 }
1987
1988 /* Last ? */
1989 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1990 (*tail)->next = buf;
1991 *tail = buf;
1992 return 1;
1993 }
1994
1995 /* Scan through queue and sort it in */
1996 do {
1997 struct tipc_msg *msg = buf_msg(crs);
1998
1999 if (less(seq_no, msg_seqno(msg))) {
2000 buf->next = crs;
2001 if (prev)
2002 prev->next = buf;
2003 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002004 *head = buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002005 return 1;
2006 }
2007 if (seq_no == msg_seqno(msg)) {
2008 break;
2009 }
2010 prev = crs;
2011 crs = crs->next;
2012 }
2013 while (crs);
2014
2015 /* Message is a duplicate of an existing message */
2016
2017 buf_discard(buf);
2018 return 0;
2019}
2020
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002021/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
2023 */
2024
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002025static void link_handle_out_of_seq_msg(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026 struct sk_buff *buf)
2027{
2028 u32 seq_no = msg_seqno(buf_msg(buf));
2029
2030 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
2031 link_recv_proto_msg(l_ptr, buf);
2032 return;
2033 }
2034
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002035 dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
2037
2038 /* Record OOS packet arrival (force mismatch on next timeout) */
2039
2040 l_ptr->checkpoint--;
2041
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002042 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002043 * Discard packet if a duplicate; otherwise add it to deferred queue
2044 * and notify peer of gap as per protocol specification
2045 */
2046
2047 if (less(seq_no, mod(l_ptr->next_in_no))) {
2048 l_ptr->stats.duplicates++;
2049 buf_discard(buf);
2050 return;
2051 }
2052
Per Liden4323add2006-01-18 00:38:21 +01002053 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
2054 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 l_ptr->deferred_inqueue_sz++;
2056 l_ptr->stats.deferred_recv++;
2057 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01002058 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002059 } else
2060 l_ptr->stats.duplicates++;
2061}
2062
2063/*
2064 * Send protocol message to the other endpoint.
2065 */
Per Liden4323add2006-01-18 00:38:21 +01002066void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
2067 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002069 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002071 u32 msg_size = sizeof(l_ptr->proto_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072
2073 if (link_blocked(l_ptr))
2074 return;
2075 msg_set_type(msg, msg_typ);
2076 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002077 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
Per Liden4323add2006-01-18 00:38:21 +01002078 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01002079
2080 if (msg_typ == STATE_MSG) {
2081 u32 next_sent = mod(l_ptr->next_out_no);
2082
Per Liden4323add2006-01-18 00:38:21 +01002083 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084 return;
2085 if (l_ptr->next_out)
2086 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
2087 msg_set_next_sent(msg, next_sent);
2088 if (l_ptr->oldest_deferred_in) {
2089 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2090 gap = mod(rec - mod(l_ptr->next_in_no));
2091 }
2092 msg_set_seq_gap(msg, gap);
2093 if (gap)
2094 l_ptr->stats.sent_nacks++;
2095 msg_set_link_tolerance(msg, tolerance);
2096 msg_set_linkprio(msg, priority);
2097 msg_set_max_pkt(msg, ack_mtu);
2098 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
2099 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002100 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002101 u32 mtu = l_ptr->max_pkt;
2102
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002103 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 link_working_working(l_ptr) &&
2105 l_ptr->fsm_msg_cnt) {
2106 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002107 if (l_ptr->max_pkt_probes == 10) {
2108 l_ptr->max_pkt_target = (msg_size - 4);
2109 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002110 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002111 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002113 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114
2115 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002116 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 l_ptr->stats.sent_states++;
2118 } else { /* RESET_MSG or ACTIVATE_MSG */
2119 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
2120 msg_set_seq_gap(msg, 0);
2121 msg_set_next_sent(msg, 1);
2122 msg_set_link_tolerance(msg, l_ptr->tolerance);
2123 msg_set_linkprio(msg, l_ptr->priority);
2124 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
2125 }
2126
Per Liden4323add2006-01-18 00:38:21 +01002127 if (tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128 msg_set_redundant_link(msg);
2129 } else {
2130 msg_clear_redundant_link(msg);
2131 }
2132 msg_set_linkprio(msg, l_ptr->priority);
2133
2134 /* Ensure sequence number will not fit : */
2135
2136 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
2137
2138 /* Congestion? */
2139
Per Liden4323add2006-01-18 00:38:21 +01002140 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002141 if (!l_ptr->proto_msg_queue) {
2142 l_ptr->proto_msg_queue =
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002143 tipc_buf_acquire(sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002144 }
2145 buf = l_ptr->proto_msg_queue;
2146 if (!buf)
2147 return;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002148 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 return;
2150 }
2151 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
2152
2153 /* Message can be sent */
2154
2155 msg_dbg(msg, ">>");
2156
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002157 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002158 if (!buf)
2159 return;
2160
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002161 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002162 msg_set_size(buf_msg(buf), msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002163
Per Liden4323add2006-01-18 00:38:21 +01002164 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002165 l_ptr->unacked_window = 0;
2166 buf_discard(buf);
2167 return;
2168 }
2169
2170 /* New congestion */
Per Liden4323add2006-01-18 00:38:21 +01002171 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002172 l_ptr->proto_msg_queue = buf;
2173 l_ptr->stats.bearer_congs++;
2174}
2175
2176/*
2177 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002178 * Note that network plane id propagates through the network, and may
2179 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01002180 */
2181
2182static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2183{
2184 u32 rec_gap = 0;
2185 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002186 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002187 u32 msg_tol;
2188 struct tipc_msg *msg = buf_msg(buf);
2189
2190 dbg("AT(%u):", jiffies_to_msecs(jiffies));
2191 msg_dbg(msg, "<<");
2192 if (link_blocked(l_ptr))
2193 goto exit;
2194
2195 /* record unnumbered packet arrival (force mismatch on next timeout) */
2196
2197 l_ptr->checkpoint--;
2198
2199 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2200 if (tipc_own_addr > msg_prevnode(msg))
2201 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2202
2203 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2204
2205 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002206
Per Lidenb97bf3f2006-01-02 19:04:38 +01002207 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07002208 if (!link_working_unknown(l_ptr) &&
2209 (l_ptr->peer_session != INVALID_SESSION)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210 if (msg_session(msg) == l_ptr->peer_session) {
2211 dbg("Duplicate RESET: %u<->%u\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002212 msg_session(msg), l_ptr->peer_session);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002213 break; /* duplicate: ignore */
2214 }
2215 }
2216 /* fall thru' */
2217 case ACTIVATE_MSG:
2218 /* Update link settings according other endpoint's values */
2219
2220 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2221
2222 if ((msg_tol = msg_link_tolerance(msg)) &&
2223 (msg_tol > l_ptr->tolerance))
2224 link_set_supervision_props(l_ptr, msg_tol);
2225
2226 if (msg_linkprio(msg) > l_ptr->priority)
2227 l_ptr->priority = msg_linkprio(msg);
2228
2229 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002230 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002231 if (max_pkt_info < l_ptr->max_pkt_target)
2232 l_ptr->max_pkt_target = max_pkt_info;
2233 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2234 l_ptr->max_pkt = l_ptr->max_pkt_target;
2235 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002236 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002237 }
2238 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2239
2240 link_state_event(l_ptr, msg_type(msg));
2241
2242 l_ptr->peer_session = msg_session(msg);
2243 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2244
2245 /* Synchronize broadcast sequence numbers */
Per Liden4323add2006-01-18 00:38:21 +01002246 if (!tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002247 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2248 }
2249 break;
2250 case STATE_MSG:
2251
2252 if ((msg_tol = msg_link_tolerance(msg)))
2253 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002254
2255 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002256 (msg_linkprio(msg) != l_ptr->priority)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002257 warn("Resetting link <%s>, priority change %u->%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002258 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2259 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01002260 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002261 break;
2262 }
2263 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2264 l_ptr->stats.recv_states++;
2265 if (link_reset_unknown(l_ptr))
2266 break;
2267
2268 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002269 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01002270 mod(l_ptr->next_in_no));
2271 }
2272
2273 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002274 if (max_pkt_ack > l_ptr->max_pkt) {
2275 dbg("Link <%s> updated MTU %u -> %u\n",
2276 l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
2277 l_ptr->max_pkt = max_pkt_ack;
2278 l_ptr->max_pkt_probes = 0;
2279 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002280
2281 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002282 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002283 l_ptr->stats.recv_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002284 if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2285 max_pkt_ack = msg_size(msg);
2286 }
2287 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002288
2289 /* Protocol message before retransmits, reduce loss risk */
2290
Per Liden4323add2006-01-18 00:38:21 +01002291 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002292
2293 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01002294 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2295 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002296 }
2297 if (msg_seq_gap(msg)) {
2298 msg_dbg(msg, "With Gap:");
2299 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01002300 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2301 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302 }
2303 break;
2304 default:
2305 msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
2306 }
2307exit:
2308 buf_discard(buf);
2309}
2310
2311
2312/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002313 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01002314 * another bearer. Owner node is locked.
2315 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002316static void tipc_link_tunnel(struct link *l_ptr,
2317 struct tipc_msg *tunnel_hdr,
2318 struct tipc_msg *msg,
2319 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002320{
2321 struct link *tunnel;
2322 struct sk_buff *buf;
2323 u32 length = msg_size(msg);
2324
2325 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07002326 if (!tipc_link_is_up(tunnel)) {
2327 warn("Link changeover error, "
2328 "tunnel link no longer available\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002329 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002330 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002331 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002332 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07002333 if (!buf) {
2334 warn("Link changeover error, "
2335 "unable to send tunnel msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002336 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002337 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002338 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2339 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002340 dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
2341 msg_dbg(buf_msg(buf), ">SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002342 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002343}
2344
2345
2346
2347/*
2348 * changeover(): Send whole message queue via the remaining link
2349 * Owner node is locked.
2350 */
2351
Per Liden4323add2006-01-18 00:38:21 +01002352void tipc_link_changeover(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002353{
2354 u32 msgcount = l_ptr->out_queue_size;
2355 struct sk_buff *crs = l_ptr->first_out;
2356 struct link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002357 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002358 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359
2360 if (!tunnel)
2361 return;
2362
Allan Stephens5392d642006-06-25 23:52:50 -07002363 if (!l_ptr->owner->permit_changeover) {
2364 warn("Link changeover error, "
2365 "peer did not permit changeover\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002366 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002367 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002368
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002369 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002370 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002371 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2372 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephens5392d642006-06-25 23:52:50 -07002373 dbg("Link changeover requires %u tunnel messages\n", msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002374
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375 if (!l_ptr->first_out) {
2376 struct sk_buff *buf;
2377
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002378 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002379 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002380 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002381 msg_set_size(&tunnel_hdr, INT_H_SIZE);
2382 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2383 tunnel->b_ptr->net_plane);
2384 msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002385 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002386 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002387 warn("Link changeover error, "
2388 "unable to send changeover msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002389 }
2390 return;
2391 }
Allan Stephensf1310722006-06-25 23:51:37 -07002392
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002393 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002394 l_ptr->owner->active_links[1]);
2395
Per Lidenb97bf3f2006-01-02 19:04:38 +01002396 while (crs) {
2397 struct tipc_msg *msg = buf_msg(crs);
2398
2399 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002400 struct tipc_msg *m = msg_get_wrapped(msg);
2401 unchar* pos = (unchar*)m;
2402
Florian Westphald788d802007-08-02 19:28:06 -07002403 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002404 while (msgcount--) {
2405 msg_set_seqno(m,msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002406 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2407 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002408 pos += align(msg_size(m));
2409 m = (struct tipc_msg *)pos;
2410 }
2411 } else {
Per Liden4323add2006-01-18 00:38:21 +01002412 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2413 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414 }
2415 crs = crs->next;
2416 }
2417}
2418
Per Liden4323add2006-01-18 00:38:21 +01002419void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002420{
2421 struct sk_buff *iter;
2422 struct tipc_msg tunnel_hdr;
2423
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002424 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002425 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002426 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2427 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2428 iter = l_ptr->first_out;
2429 while (iter) {
2430 struct sk_buff *outbuf;
2431 struct tipc_msg *msg = buf_msg(iter);
2432 u32 length = msg_size(msg);
2433
2434 if (msg_user(msg) == MSG_BUNDLER)
2435 msg_set_type(msg, CLOSED_MSG);
2436 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002437 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002438 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002439 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002440 if (outbuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002441 warn("Link changeover error, "
2442 "unable to send duplicate msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002443 return;
2444 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002445 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2446 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2447 length);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2449 tunnel->b_ptr->net_plane);
2450 msg_dbg(buf_msg(outbuf), ">SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002451 tipc_link_send_buf(tunnel, outbuf);
2452 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002453 return;
2454 iter = iter->next;
2455 }
2456}
2457
2458
2459
2460/**
2461 * buf_extract - extracts embedded TIPC message from another message
2462 * @skb: encapsulating message buffer
2463 * @from_pos: offset to extract from
2464 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002465 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002466 * encapsulating message itself is left unchanged.
2467 */
2468
2469static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2470{
2471 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2472 u32 size = msg_size(msg);
2473 struct sk_buff *eb;
2474
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002475 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002476 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002477 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002478 return eb;
2479}
2480
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002481/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002482 * link_recv_changeover_msg(): Receive tunneled packet sent
2483 * via other link. Node is locked. Return extracted buffer.
2484 */
2485
2486static int link_recv_changeover_msg(struct link **l_ptr,
2487 struct sk_buff **buf)
2488{
2489 struct sk_buff *tunnel_buf = *buf;
2490 struct link *dest_link;
2491 struct tipc_msg *msg;
2492 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2493 u32 msg_typ = msg_type(tunnel_msg);
2494 u32 msg_count = msg_msgcnt(tunnel_msg);
2495
2496 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002497 if (!dest_link) {
2498 msg_dbg(tunnel_msg, "NOLINK/<REC<");
2499 goto exit;
2500 }
Allan Stephensf1310722006-06-25 23:51:37 -07002501 if (dest_link == *l_ptr) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002502 err("Unexpected changeover message on link <%s>\n",
Allan Stephensf1310722006-06-25 23:51:37 -07002503 (*l_ptr)->name);
2504 goto exit;
2505 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002506 dbg("%c<-%c:", dest_link->b_ptr->net_plane,
2507 (*l_ptr)->b_ptr->net_plane);
2508 *l_ptr = dest_link;
2509 msg = msg_get_wrapped(tunnel_msg);
2510
2511 if (msg_typ == DUPLICATE_MSG) {
2512 if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
2513 msg_dbg(tunnel_msg, "DROP/<REC<");
2514 goto exit;
2515 }
2516 *buf = buf_extract(tunnel_buf,INT_H_SIZE);
2517 if (*buf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002518 warn("Link changeover error, duplicate msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002519 goto exit;
2520 }
2521 msg_dbg(tunnel_msg, "TNL<REC<");
2522 buf_discard(tunnel_buf);
2523 return 1;
2524 }
2525
2526 /* First original message ?: */
2527
Per Liden4323add2006-01-18 00:38:21 +01002528 if (tipc_link_is_up(dest_link)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002529 msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
Allan Stephensa10bd922006-06-25 23:52:17 -07002530 info("Resetting link <%s>, changeover initiated by peer\n",
2531 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002532 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002533 dest_link->exp_msg_count = msg_count;
Allan Stephens5392d642006-06-25 23:52:50 -07002534 dbg("Expecting %u tunnelled messages\n", msg_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002535 if (!msg_count)
2536 goto exit;
2537 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
2538 msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
2539 dest_link->exp_msg_count = msg_count;
Allan Stephens5392d642006-06-25 23:52:50 -07002540 dbg("Expecting %u tunnelled messages\n", msg_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002541 if (!msg_count)
2542 goto exit;
2543 }
2544
2545 /* Receive original message */
2546
2547 if (dest_link->exp_msg_count == 0) {
Allan Stephens5392d642006-06-25 23:52:50 -07002548 warn("Link switchover error, "
2549 "got too many tunnelled messages\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550 msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
2551 dbg_print_link(dest_link, "LINK:");
2552 goto exit;
2553 }
2554 dest_link->exp_msg_count--;
2555 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
2556 msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
2557 goto exit;
2558 } else {
2559 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2560 if (*buf != NULL) {
2561 msg_dbg(tunnel_msg, "TNL<REC<");
2562 buf_discard(tunnel_buf);
2563 return 1;
2564 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002565 warn("Link changeover error, original msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002566 }
2567 }
2568exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002569 *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002570 buf_discard(tunnel_buf);
2571 return 0;
2572}
2573
2574/*
2575 * Bundler functionality:
2576 */
Per Liden4323add2006-01-18 00:38:21 +01002577void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578{
2579 u32 msgcount = msg_msgcnt(buf_msg(buf));
2580 u32 pos = INT_H_SIZE;
2581 struct sk_buff *obuf;
2582
2583 msg_dbg(buf_msg(buf), "<BNDL<: ");
2584 while (msgcount--) {
2585 obuf = buf_extract(buf, pos);
2586 if (obuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002587 warn("Link unable to unbundle message(s)\n");
2588 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002589 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002590 pos += align(msg_size(buf_msg(obuf)));
2591 msg_dbg(buf_msg(obuf), " /");
Per Liden4323add2006-01-18 00:38:21 +01002592 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593 }
2594 buf_discard(buf);
2595}
2596
2597/*
2598 * Fragmentation/defragmentation:
2599 */
2600
2601
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002602/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002603 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002604 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605 * Returns user data length.
2606 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002607static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002608{
2609 struct tipc_msg *inmsg = buf_msg(buf);
2610 struct tipc_msg fragm_hdr;
2611 u32 insize = msg_size(inmsg);
2612 u32 dsz = msg_data_sz(inmsg);
2613 unchar *crs = buf->data;
2614 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002615 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002616 u32 fragm_sz = pack_sz - INT_H_SIZE;
2617 u32 fragm_no = 1;
Allan Stephens9c396a72008-06-04 17:36:58 -07002618 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002619
2620 if (msg_short(inmsg))
2621 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002622 else
2623 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002624
2625 if (msg_routed(inmsg))
2626 msg_set_prevnode(inmsg, tipc_own_addr);
2627
2628 /* Prepare reusable fragment header: */
2629
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002630 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002631 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002632 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2633 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2634 msg_set_fragm_no(&fragm_hdr, fragm_no);
2635 l_ptr->stats.sent_fragmented++;
2636
2637 /* Chop up message: */
2638
2639 while (rest > 0) {
2640 struct sk_buff *fragm;
2641
2642 if (rest <= fragm_sz) {
2643 fragm_sz = rest;
2644 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2645 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002646 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002647 if (fragm == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002648 warn("Link unable to fragment message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002649 dsz = -ENOMEM;
2650 goto exit;
2651 }
2652 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002653 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2654 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2655 fragm_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002656 /* Send queued messages first, if any: */
2657
2658 l_ptr->stats.sent_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01002659 tipc_link_send_buf(l_ptr, fragm);
2660 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002661 return dsz;
2662 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2663 rest -= fragm_sz;
2664 crs += fragm_sz;
2665 msg_set_type(&fragm_hdr, FRAGMENT);
2666 }
2667exit:
2668 buf_discard(buf);
2669 return dsz;
2670}
2671
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002672/*
2673 * A pending message being re-assembled must store certain values
2674 * to handle subsequent fragments correctly. The following functions
Per Lidenb97bf3f2006-01-02 19:04:38 +01002675 * help storing these values in unused, available fields in the
2676 * pending message. This makes dynamic memory allocation unecessary.
2677 */
2678
Sam Ravnborg05790c62006-03-20 22:37:04 -08002679static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002680{
2681 msg_set_seqno(buf_msg(buf), seqno);
2682}
2683
Sam Ravnborg05790c62006-03-20 22:37:04 -08002684static u32 get_fragm_size(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002685{
2686 return msg_ack(buf_msg(buf));
2687}
2688
Sam Ravnborg05790c62006-03-20 22:37:04 -08002689static void set_fragm_size(struct sk_buff *buf, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002690{
2691 msg_set_ack(buf_msg(buf), sz);
2692}
2693
Sam Ravnborg05790c62006-03-20 22:37:04 -08002694static u32 get_expected_frags(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002695{
2696 return msg_bcast_ack(buf_msg(buf));
2697}
2698
Sam Ravnborg05790c62006-03-20 22:37:04 -08002699static void set_expected_frags(struct sk_buff *buf, u32 exp)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002700{
2701 msg_set_bcast_ack(buf_msg(buf), exp);
2702}
2703
Sam Ravnborg05790c62006-03-20 22:37:04 -08002704static u32 get_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002705{
2706 return msg_reroute_cnt(buf_msg(buf));
2707}
2708
Sam Ravnborg05790c62006-03-20 22:37:04 -08002709static void incr_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710{
2711 msg_incr_reroute_cnt(buf_msg(buf));
2712}
2713
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002714/*
2715 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002716 * the reassembled buffer if message is complete.
2717 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002718int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
Per Liden4323add2006-01-18 00:38:21 +01002719 struct tipc_msg **m)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002720{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002721 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002722 struct sk_buff *fbuf = *fb;
2723 struct tipc_msg *fragm = buf_msg(fbuf);
2724 struct sk_buff *pbuf = *pending;
2725 u32 long_msg_seq_no = msg_long_msgno(fragm);
2726
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002727 *fb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002728 msg_dbg(fragm,"FRG<REC<");
2729
2730 /* Is there an incomplete message waiting for this fragment? */
2731
Joe Perchesf64f9e72009-11-29 16:55:45 -08002732 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
2733 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002734 prev = pbuf;
2735 pbuf = pbuf->next;
2736 }
2737
2738 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2739 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2740 u32 msg_sz = msg_size(imsg);
2741 u32 fragm_sz = msg_data_sz(fragm);
2742 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2743 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2744 if (msg_type(imsg) == TIPC_MCAST_MSG)
2745 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2746 if (msg_size(imsg) > max) {
2747 msg_dbg(fragm,"<REC<Oversized: ");
2748 buf_discard(fbuf);
2749 return 0;
2750 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002751 pbuf = tipc_buf_acquire(msg_size(imsg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002752 if (pbuf != NULL) {
2753 pbuf->next = *pending;
2754 *pending = pbuf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002755 skb_copy_to_linear_data(pbuf, imsg,
2756 msg_data_sz(fragm));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002757 /* Prepare buffer for subsequent fragments. */
2758
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002759 set_long_msg_seqno(pbuf, long_msg_seq_no);
2760 set_fragm_size(pbuf,fragm_sz);
2761 set_expected_frags(pbuf,exp_fragm_cnt - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002762 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002763 warn("Link unable to reassemble fragmented message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002764 }
2765 buf_discard(fbuf);
2766 return 0;
2767 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2768 u32 dsz = msg_data_sz(fragm);
2769 u32 fsz = get_fragm_size(pbuf);
2770 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2771 u32 exp_frags = get_expected_frags(pbuf) - 1;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002772 skb_copy_to_linear_data_offset(pbuf, crs,
2773 msg_data(fragm), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002774 buf_discard(fbuf);
2775
2776 /* Is message complete? */
2777
2778 if (exp_frags == 0) {
2779 if (prev)
2780 prev->next = pbuf->next;
2781 else
2782 *pending = pbuf->next;
2783 msg_reset_reroute_cnt(buf_msg(pbuf));
2784 *fb = pbuf;
2785 *m = buf_msg(pbuf);
2786 return 1;
2787 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002788 set_expected_frags(pbuf,exp_frags);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002789 return 0;
2790 }
2791 dbg(" Discarding orphan fragment %x\n",fbuf);
2792 msg_dbg(fragm,"ORPHAN:");
2793 dbg("Pending long buffers:\n");
2794 dbg_print_buf_chain(*pending);
2795 buf_discard(fbuf);
2796 return 0;
2797}
2798
2799/**
2800 * link_check_defragm_bufs - flush stale incoming message fragments
2801 * @l_ptr: pointer to link
2802 */
2803
2804static void link_check_defragm_bufs(struct link *l_ptr)
2805{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002806 struct sk_buff *prev = NULL;
2807 struct sk_buff *next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002808 struct sk_buff *buf = l_ptr->defragm_buf;
2809
2810 if (!buf)
2811 return;
2812 if (!link_working_working(l_ptr))
2813 return;
2814 while (buf) {
2815 u32 cnt = get_timer_cnt(buf);
2816
2817 next = buf->next;
2818 if (cnt < 4) {
2819 incr_timer_cnt(buf);
2820 prev = buf;
2821 } else {
2822 dbg(" Discarding incomplete long buffer\n");
2823 msg_dbg(buf_msg(buf), "LONG:");
2824 dbg_print_link(l_ptr, "curr:");
2825 dbg("Pending long buffers:\n");
2826 dbg_print_buf_chain(l_ptr->defragm_buf);
2827 if (prev)
2828 prev->next = buf->next;
2829 else
2830 l_ptr->defragm_buf = buf->next;
2831 buf_discard(buf);
2832 }
2833 buf = next;
2834 }
2835}
2836
2837
2838
2839static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2840{
2841 l_ptr->tolerance = tolerance;
2842 l_ptr->continuity_interval =
2843 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2844 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2845}
2846
2847
Per Liden4323add2006-01-18 00:38:21 +01002848void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002849{
2850 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002851 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2852 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2853 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2854 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002855 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002856 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2857 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2858 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2859 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002860 l_ptr->queue_limit[CONN_MANAGER] = 1200;
2861 l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
2862 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2863 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2864 /* FRAGMENT and LAST_FRAGMENT packets */
2865 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2866}
2867
2868/**
2869 * link_find_link - locate link by name
2870 * @name - ptr to link name string
2871 * @node - ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002872 *
Per Liden4323add2006-01-18 00:38:21 +01002873 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002874 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002875 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002876 * Returns pointer to link (or 0 if invalid link name).
2877 */
2878
David S. Miller6c000552008-09-02 23:38:32 -07002879static struct link *link_find_link(const char *name, struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002880{
2881 struct link_name link_name_parts;
2882 struct bearer *b_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002883 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002884
2885 if (!link_name_validate(name, &link_name_parts))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002886 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002887
Per Liden4323add2006-01-18 00:38:21 +01002888 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002889 if (!b_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002890 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002891
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002892 *node = tipc_node_find(link_name_parts.addr_peer);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002893 if (!*node)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002894 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002895
2896 l_ptr = (*node)->links[b_ptr->identity];
2897 if (!l_ptr || strcmp(l_ptr->name, name))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002898 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002899
2900 return l_ptr;
2901}
2902
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002903struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002904 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002905{
2906 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002907 u32 new_value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002908 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002909 struct tipc_node *node;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002910 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002911
2912 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002913 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002914
2915 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2916 new_value = ntohl(args->value);
2917
Per Liden4323add2006-01-18 00:38:21 +01002918 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002919 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002920 (tipc_bclink_set_queue_limits(new_value) == 0))
2921 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002922 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002923 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002924 }
2925
Per Liden4323add2006-01-18 00:38:21 +01002926 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002927 l_ptr = link_find_link(args->name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002928 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002929 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002930 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002931 }
2932
Per Liden4323add2006-01-18 00:38:21 +01002933 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002934 res = -EINVAL;
2935 switch (cmd) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002936 case TIPC_CMD_SET_LINK_TOL:
2937 if ((new_value >= TIPC_MIN_LINK_TOL) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002938 (new_value <= TIPC_MAX_LINK_TOL)) {
2939 link_set_supervision_props(l_ptr, new_value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002940 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002941 0, 0, new_value, 0, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002942 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002943 }
2944 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002945 case TIPC_CMD_SET_LINK_PRI:
Per Liden16cb4b32006-01-13 22:22:22 +01002946 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2947 (new_value <= TIPC_MAX_LINK_PRI)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002948 l_ptr->priority = new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002949 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002950 0, 0, 0, new_value, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002951 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002952 }
2953 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002954 case TIPC_CMD_SET_LINK_WINDOW:
2955 if ((new_value >= TIPC_MIN_LINK_WIN) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002956 (new_value <= TIPC_MAX_LINK_WIN)) {
Per Liden4323add2006-01-18 00:38:21 +01002957 tipc_link_set_queue_limits(l_ptr, new_value);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002958 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002959 }
2960 break;
2961 }
Per Liden4323add2006-01-18 00:38:21 +01002962 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002963
Per Liden4323add2006-01-18 00:38:21 +01002964 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002965 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002966 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002967
Per Liden4323add2006-01-18 00:38:21 +01002968 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002969}
2970
2971/**
2972 * link_reset_statistics - reset link statistics
2973 * @l_ptr: pointer to link
2974 */
2975
2976static void link_reset_statistics(struct link *l_ptr)
2977{
2978 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2979 l_ptr->stats.sent_info = l_ptr->next_out_no;
2980 l_ptr->stats.recv_info = l_ptr->next_in_no;
2981}
2982
Per Liden4323add2006-01-18 00:38:21 +01002983struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002984{
2985 char *link_name;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002986 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002987 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002988
2989 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002990 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002991
2992 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002993 if (!strcmp(link_name, tipc_bclink_name)) {
2994 if (tipc_bclink_reset_stats())
2995 return tipc_cfg_reply_error_string("link not found");
2996 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002997 }
2998
Per Liden4323add2006-01-18 00:38:21 +01002999 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003000 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003001 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003002 read_unlock_bh(&tipc_net_lock);
3003 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003004 }
3005
Per Liden4323add2006-01-18 00:38:21 +01003006 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003007 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01003008 tipc_node_unlock(node);
3009 read_unlock_bh(&tipc_net_lock);
3010 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01003011}
3012
3013/**
3014 * percent - convert count to a percentage of total (rounding up or down)
3015 */
3016
3017static u32 percent(u32 count, u32 total)
3018{
3019 return (count * 100 + (total / 2)) / total;
3020}
3021
3022/**
Per Liden4323add2006-01-18 00:38:21 +01003023 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01003024 * @name: link name
3025 * @buf: print buffer area
3026 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003027 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003028 * Returns length of print buffer data string (or 0 if error)
3029 */
3030
Per Liden4323add2006-01-18 00:38:21 +01003031static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003032{
3033 struct print_buf pb;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003034 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07003035 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003036 char *status;
3037 u32 profile_total = 0;
3038
Per Liden4323add2006-01-18 00:38:21 +01003039 if (!strcmp(name, tipc_bclink_name))
3040 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003041
Per Liden4323add2006-01-18 00:38:21 +01003042 tipc_printbuf_init(&pb, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003043
Per Liden4323add2006-01-18 00:38:21 +01003044 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003045 l_ptr = link_find_link(name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003046 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003047 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003048 return 0;
3049 }
Per Liden4323add2006-01-18 00:38:21 +01003050 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003051
Per Liden4323add2006-01-18 00:38:21 +01003052 if (tipc_link_is_active(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003053 status = "ACTIVE";
Per Liden4323add2006-01-18 00:38:21 +01003054 else if (tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003055 status = "STANDBY";
3056 else
3057 status = "DEFUNCT";
3058 tipc_printf(&pb, "Link <%s>\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003059 " %s MTU:%u Priority:%u Tolerance:%u ms"
3060 " Window:%u packets\n",
Allan Stephens15e979d2010-05-11 14:30:10 +00003061 l_ptr->name, status, l_ptr->max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003062 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003063 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003064 l_ptr->next_in_no - l_ptr->stats.recv_info,
3065 l_ptr->stats.recv_fragments,
3066 l_ptr->stats.recv_fragmented,
3067 l_ptr->stats.recv_bundles,
3068 l_ptr->stats.recv_bundled);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003069 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003070 l_ptr->next_out_no - l_ptr->stats.sent_info,
3071 l_ptr->stats.sent_fragments,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003072 l_ptr->stats.sent_fragmented,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003073 l_ptr->stats.sent_bundles,
3074 l_ptr->stats.sent_bundled);
3075 profile_total = l_ptr->stats.msg_length_counts;
3076 if (!profile_total)
3077 profile_total = 1;
3078 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003079 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
3080 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003081 l_ptr->stats.msg_length_counts,
3082 l_ptr->stats.msg_lengths_total / profile_total,
3083 percent(l_ptr->stats.msg_length_profile[0], profile_total),
3084 percent(l_ptr->stats.msg_length_profile[1], profile_total),
3085 percent(l_ptr->stats.msg_length_profile[2], profile_total),
3086 percent(l_ptr->stats.msg_length_profile[3], profile_total),
3087 percent(l_ptr->stats.msg_length_profile[4], profile_total),
3088 percent(l_ptr->stats.msg_length_profile[5], profile_total),
3089 percent(l_ptr->stats.msg_length_profile[6], profile_total));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003090 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003091 l_ptr->stats.recv_states,
3092 l_ptr->stats.recv_probes,
3093 l_ptr->stats.recv_nacks,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003094 l_ptr->stats.deferred_recv,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003095 l_ptr->stats.duplicates);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003096 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
3097 l_ptr->stats.sent_states,
3098 l_ptr->stats.sent_probes,
3099 l_ptr->stats.sent_nacks,
3100 l_ptr->stats.sent_acks,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003101 l_ptr->stats.retransmitted);
3102 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
3103 l_ptr->stats.bearer_congs,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003104 l_ptr->stats.link_congs,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003105 l_ptr->stats.max_queue_sz,
3106 l_ptr->stats.queue_sz_counts
3107 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
3108 : 0);
3109
Per Liden4323add2006-01-18 00:38:21 +01003110 tipc_node_unlock(node);
3111 read_unlock_bh(&tipc_net_lock);
3112 return tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003113}
3114
3115#define MAX_LINK_STATS_INFO 2000
3116
Per Liden4323add2006-01-18 00:38:21 +01003117struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003118{
3119 struct sk_buff *buf;
3120 struct tlv_desc *rep_tlv;
3121 int str_len;
3122
3123 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01003124 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003125
Per Liden4323add2006-01-18 00:38:21 +01003126 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
Per Lidenb97bf3f2006-01-02 19:04:38 +01003127 if (!buf)
3128 return NULL;
3129
3130 rep_tlv = (struct tlv_desc *)buf->data;
3131
Per Liden4323add2006-01-18 00:38:21 +01003132 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
3133 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003134 if (!str_len) {
3135 buf_discard(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003136 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003137 }
3138
3139 skb_put(buf, TLV_SPACE(str_len));
3140 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
3141
3142 return buf;
3143}
3144
Per Lidenb97bf3f2006-01-02 19:04:38 +01003145/**
Per Liden4323add2006-01-18 00:38:21 +01003146 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01003147 * @dest: network address of destination node
3148 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003149 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003150 * If no active link can be found, uses default maximum packet size.
3151 */
3152
Per Liden4323add2006-01-18 00:38:21 +01003153u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003154{
David S. Miller6c000552008-09-02 23:38:32 -07003155 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003156 struct link *l_ptr;
3157 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003158
Per Lidenb97bf3f2006-01-02 19:04:38 +01003159 if (dest == tipc_own_addr)
3160 return MAX_MSG_SIZE;
3161
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003162 read_lock_bh(&tipc_net_lock);
Per Liden4323add2006-01-18 00:38:21 +01003163 n_ptr = tipc_node_select(dest, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003164 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003165 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003166 l_ptr = n_ptr->active_links[selector & 1];
3167 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00003168 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01003169 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003170 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003171 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003172 return res;
3173}
3174
Per Lidenb97bf3f2006-01-02 19:04:38 +01003175static void link_dump_send_queue(struct link *l_ptr)
3176{
3177 if (l_ptr->next_out) {
3178 info("\nContents of unsent queue:\n");
3179 dbg_print_buf_chain(l_ptr->next_out);
3180 }
3181 info("\nContents of send queue:\n");
3182 if (l_ptr->first_out) {
3183 dbg_print_buf_chain(l_ptr->first_out);
3184 }
3185 info("Empty send queue\n");
3186}
3187
3188static void link_print(struct link *l_ptr, struct print_buf *buf,
3189 const char *str)
3190{
3191 tipc_printf(buf, str);
3192 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
3193 return;
3194 tipc_printf(buf, "Link %x<%s>:",
3195 l_ptr->addr, l_ptr->b_ptr->publ.name);
3196 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
3197 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
3198 tipc_printf(buf, "SQUE");
3199 if (l_ptr->first_out) {
3200 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
3201 if (l_ptr->next_out)
3202 tipc_printf(buf, "%u..",
3203 msg_seqno(buf_msg(l_ptr->next_out)));
Allan Stephensb82834e2010-05-11 14:30:04 +00003204 tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003205 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
3206 msg_seqno(buf_msg(l_ptr->first_out)))
Joe Perchesf64f9e72009-11-29 16:55:45 -08003207 != (l_ptr->out_queue_size - 1)) ||
3208 (l_ptr->last_out->next != NULL)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01003209 tipc_printf(buf, "\nSend queue inconsistency\n");
3210 tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
3211 tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
3212 tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
3213 link_dump_send_queue(l_ptr);
3214 }
3215 } else
3216 tipc_printf(buf, "[]");
3217 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
3218 if (l_ptr->oldest_deferred_in) {
3219 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
3220 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
3221 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
3222 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
3223 tipc_printf(buf, ":RQSIZ(%u)",
3224 l_ptr->deferred_inqueue_sz);
3225 }
3226 }
3227 if (link_working_unknown(l_ptr))
3228 tipc_printf(buf, ":WU");
3229 if (link_reset_reset(l_ptr))
3230 tipc_printf(buf, ":RR");
3231 if (link_reset_unknown(l_ptr))
3232 tipc_printf(buf, ":RU");
3233 if (link_working_working(l_ptr))
3234 tipc_printf(buf, ":WW");
3235 tipc_printf(buf, "\n");
3236}
3237