blob: cf414cf05e722800ebb32975586717c8b417f5c6 [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 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010053 */
54
55#define STARTING_EVT 856384768 /* link processing trigger */
56#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
57#define TIMEOUT_EVT 560817u /* link timer expired */
58
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090059/*
60 * The following two 'message types' is really just implementation
61 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010062 * They must not be considered part of the protocol
63 */
64#define OPEN_MSG 0
65#define CLOSED_MSG 1
66
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090067/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010068 * State value stored in 'exp_msg_count'
69 */
70
71#define START_CHANGEOVER 100000u
72
73/**
74 * struct link_name - deconstructed link name
75 * @addr_local: network address of node at this end
76 * @if_local: name of interface at this end
77 * @addr_peer: network address of node at far end
78 * @if_peer: name of interface at far end
79 */
80
81struct link_name {
82 u32 addr_local;
83 char if_local[TIPC_MAX_IF_NAME];
84 u32 addr_peer;
85 char if_peer[TIPC_MAX_IF_NAME];
86};
87
Per Lidenb97bf3f2006-01-02 19:04:38 +010088static void link_handle_out_of_seq_msg(struct link *l_ptr,
89 struct sk_buff *buf);
90static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
91static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
92static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
93static int link_send_sections_long(struct port *sender,
94 struct iovec const *msg_sect,
95 u32 num_sect, u32 destnode);
96static void link_check_defragm_bufs(struct link *l_ptr);
97static void link_state_event(struct link *l_ptr, u32 event);
98static void link_reset_statistics(struct link *l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090099static void link_print(struct link *l_ptr, struct print_buf *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100 const char *str);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000101static void link_start(struct link *l_ptr);
102static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf);
103
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104
105/*
106 * Debugging code used by link routines only
107 *
108 * When debugging link problems on a system that has multiple links,
109 * the standard TIPC debugging routines may not be useful since they
110 * allow the output from multiple links to be intermixed. For this reason
111 * routines of the form "dbg_link_XXX()" have been created that will capture
112 * debug info into a link's personal print buffer, which can then be dumped
Allan Stephensa3df92c2006-10-16 21:49:03 -0700113 * into the TIPC system log (TIPC_LOG) upon request.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 *
115 * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size
116 * of the print buffer used by each link. If LINK_LOG_BUF_SIZE is set to 0,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900117 * the dbg_link_XXX() routines simply send their output to the standard
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful
119 * when there is only a single link in the system being debugged.
120 *
121 * Notes:
Allan Stephensa3df92c2006-10-16 21:49:03 -0700122 * - When enabled, LINK_LOG_BUF_SIZE should be set to at least TIPC_PB_MIN_SIZE
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900123 * - "l_ptr" must be valid when using dbg_link_XXX() macros
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 */
125
126#define LINK_LOG_BUF_SIZE 0
127
Allan Stephens48c97132008-05-05 01:24:06 -0700128#define dbg_link(fmt, arg...) \
129 do { \
130 if (LINK_LOG_BUF_SIZE) \
131 tipc_printf(&l_ptr->print_buf, fmt, ## arg); \
132 } while (0)
133#define dbg_link_msg(msg, txt) \
134 do { \
135 if (LINK_LOG_BUF_SIZE) \
136 tipc_msg_dbg(&l_ptr->print_buf, msg, txt); \
137 } while (0)
138#define dbg_link_state(txt) \
139 do { \
140 if (LINK_LOG_BUF_SIZE) \
141 link_print(l_ptr, &l_ptr->print_buf, txt); \
142 } while (0)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143#define dbg_link_dump() do { \
144 if (LINK_LOG_BUF_SIZE) { \
145 tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \
Per Liden4323add2006-01-18 00:38:21 +0100146 tipc_printbuf_move(LOG, &l_ptr->print_buf); \
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 } \
148} while (0)
149
Sam Ravnborg05790c62006-03-20 22:37:04 -0800150static void dbg_print_link(struct link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151{
Allan Stephensa3df92c2006-10-16 21:49:03 -0700152 if (DBG_OUTPUT != TIPC_NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 link_print(l_ptr, DBG_OUTPUT, str);
154}
155
Sam Ravnborg05790c62006-03-20 22:37:04 -0800156static void dbg_print_buf_chain(struct sk_buff *root_buf)
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 struct sk_buff *buf = root_buf;
160
161 while (buf) {
162 msg_dbg(buf_msg(buf), "In chain: ");
163 buf = buf->next;
164 }
165 }
166}
167
168/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800169 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 */
171
Sam Ravnborg05790c62006-03-20 22:37:04 -0800172static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173{
174 return (i + 3) & ~3u;
175}
176
Sam Ravnborg05790c62006-03-20 22:37:04 -0800177static void link_init_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178{
179 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900180
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
182 if (max_pkt > MAX_MSG_SIZE)
183 max_pkt = MAX_MSG_SIZE;
184
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900185 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
187 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900188 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 l_ptr->max_pkt = MAX_PKT_DEFAULT;
190
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900191 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192}
193
Sam Ravnborg05790c62006-03-20 22:37:04 -0800194static u32 link_next_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195{
196 if (l_ptr->next_out)
197 return msg_seqno(buf_msg(l_ptr->next_out));
198 return mod(l_ptr->next_out_no);
199}
200
Sam Ravnborg05790c62006-03-20 22:37:04 -0800201static u32 link_last_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202{
203 return mod(link_next_sent(l_ptr) - 1);
204}
205
206/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800207 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 */
209
Per Liden4323add2006-01-18 00:38:21 +0100210int tipc_link_is_up(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211{
212 if (!l_ptr)
213 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000214 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215}
216
Per Liden4323add2006-01-18 00:38:21 +0100217int tipc_link_is_active(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000219 return (l_ptr->owner->active_links[0] == l_ptr) ||
220 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221}
222
223/**
224 * link_name_validate - validate & (optionally) deconstruct link name
225 * @name - ptr to link name string
226 * @name_parts - ptr to area for link name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900227 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 * Returns 1 if link name is valid, otherwise 0.
229 */
230
231static int link_name_validate(const char *name, struct link_name *name_parts)
232{
233 char name_copy[TIPC_MAX_LINK_NAME];
234 char *addr_local;
235 char *if_local;
236 char *addr_peer;
237 char *if_peer;
238 char dummy;
239 u32 z_local, c_local, n_local;
240 u32 z_peer, c_peer, n_peer;
241 u32 if_local_len;
242 u32 if_peer_len;
243
244 /* copy link name & ensure length is OK */
245
246 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
247 /* need above in case non-Posix strncpy() doesn't pad with nulls */
248 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
249 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
250 return 0;
251
252 /* ensure all component parts of link name are present */
253
254 addr_local = name_copy;
255 if ((if_local = strchr(addr_local, ':')) == NULL)
256 return 0;
257 *(if_local++) = 0;
258 if ((addr_peer = strchr(if_local, '-')) == NULL)
259 return 0;
260 *(addr_peer++) = 0;
261 if_local_len = addr_peer - if_local;
262 if ((if_peer = strchr(addr_peer, ':')) == NULL)
263 return 0;
264 *(if_peer++) = 0;
265 if_peer_len = strlen(if_peer) + 1;
266
267 /* validate component parts of link name */
268
269 if ((sscanf(addr_local, "%u.%u.%u%c",
270 &z_local, &c_local, &n_local, &dummy) != 3) ||
271 (sscanf(addr_peer, "%u.%u.%u%c",
272 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
273 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
274 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900275 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
276 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
278 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
279 return 0;
280
281 /* return link name components, if necessary */
282
283 if (name_parts) {
284 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
285 strcpy(name_parts->if_local, if_local);
286 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
287 strcpy(name_parts->if_peer, if_peer);
288 }
289 return 1;
290}
291
292/**
293 * link_timeout - handle expiration of link timer
294 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900295 *
Per Liden4323add2006-01-18 00:38:21 +0100296 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
297 * with tipc_link_delete(). (There is no risk that the node will be deleted by
298 * another thread because tipc_link_delete() always cancels the link timer before
299 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 */
301
302static void link_timeout(struct link *l_ptr)
303{
Per Liden4323add2006-01-18 00:38:21 +0100304 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305
306 /* update counters used in statistical profiling of send traffic */
307
308 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
309 l_ptr->stats.queue_sz_counts++;
310
311 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
312 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
313
314 if (l_ptr->first_out) {
315 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
316 u32 length = msg_size(msg);
317
Joe Perchesf64f9e72009-11-29 16:55:45 -0800318 if ((msg_user(msg) == MSG_FRAGMENTER) &&
319 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 length = msg_size(msg_get_wrapped(msg));
321 }
322 if (length) {
323 l_ptr->stats.msg_lengths_total += length;
324 l_ptr->stats.msg_length_counts++;
325 if (length <= 64)
326 l_ptr->stats.msg_length_profile[0]++;
327 else if (length <= 256)
328 l_ptr->stats.msg_length_profile[1]++;
329 else if (length <= 1024)
330 l_ptr->stats.msg_length_profile[2]++;
331 else if (length <= 4096)
332 l_ptr->stats.msg_length_profile[3]++;
333 else if (length <= 16384)
334 l_ptr->stats.msg_length_profile[4]++;
335 else if (length <= 32768)
336 l_ptr->stats.msg_length_profile[5]++;
337 else
338 l_ptr->stats.msg_length_profile[6]++;
339 }
340 }
341
342 /* do all other link processing performed on a periodic basis */
343
344 link_check_defragm_bufs(l_ptr);
345
346 link_state_event(l_ptr, TIMEOUT_EVT);
347
348 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100349 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350
Per Liden4323add2006-01-18 00:38:21 +0100351 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352}
353
Sam Ravnborg05790c62006-03-20 22:37:04 -0800354static void link_set_timer(struct link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
356 k_start_timer(&l_ptr->timer, time);
357}
358
359/**
Per Liden4323add2006-01-18 00:38:21 +0100360 * tipc_link_create - create a new link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 * @b_ptr: pointer to associated bearer
362 * @peer: network address of node at other end of link
363 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900364 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 * Returns pointer to link.
366 */
367
Per Liden4323add2006-01-18 00:38:21 +0100368struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
369 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370{
371 struct link *l_ptr;
372 struct tipc_msg *msg;
373 char *if_name;
374
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700375 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 if (!l_ptr) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700377 warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 return NULL;
379 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380
Florian Westphal945710652007-07-26 00:05:07 -0700381 if (LINK_LOG_BUF_SIZE) {
382 char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
383
384 if (!pb) {
385 kfree(l_ptr);
386 warn("Link creation failed, no memory for print buffer\n");
387 return NULL;
388 }
389 tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
390 }
391
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 l_ptr->addr = peer;
393 if_name = strchr(b_ptr->publ.name, ':') + 1;
394 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
395 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900396 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 if_name,
398 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
399 /* note: peer i/f is appended to link name by reset/activate */
400 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 l_ptr->checkpoint = 1;
402 l_ptr->b_ptr = b_ptr;
403 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
404 l_ptr->state = RESET_UNKNOWN;
405
406 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
407 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000408 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700410 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 msg_set_bearer_id(msg, b_ptr->identity);
412 strcpy((char *)msg_data(msg), if_name);
413
414 l_ptr->priority = b_ptr->priority;
Per Liden4323add2006-01-18 00:38:21 +0100415 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416
417 link_init_max_pkt(l_ptr);
418
419 l_ptr->next_out_no = 1;
420 INIT_LIST_HEAD(&l_ptr->waiting_ports);
421
422 link_reset_statistics(l_ptr);
423
Per Liden4323add2006-01-18 00:38:21 +0100424 l_ptr->owner = tipc_node_attach_link(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 if (!l_ptr->owner) {
Florian Westphal945710652007-07-26 00:05:07 -0700426 if (LINK_LOG_BUF_SIZE)
427 kfree(l_ptr->print_buf.buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 kfree(l_ptr);
429 return NULL;
430 }
431
Florian Westphal945710652007-07-26 00:05:07 -0700432 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
433 list_add_tail(&l_ptr->link_list, &b_ptr->links);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000434 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435
Per Liden4323add2006-01-18 00:38:21 +0100436 dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900438
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 return l_ptr;
440}
441
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900442/**
Per Liden4323add2006-01-18 00:38:21 +0100443 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900445 *
Per Liden4323add2006-01-18 00:38:21 +0100446 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900448 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 */
450
Per Liden4323add2006-01-18 00:38:21 +0100451void tipc_link_delete(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452{
453 if (!l_ptr) {
454 err("Attempt to delete non-existent link\n");
455 return;
456 }
457
Per Liden4323add2006-01-18 00:38:21 +0100458 dbg("tipc_link_delete()\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459
460 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900461
Per Liden4323add2006-01-18 00:38:21 +0100462 tipc_node_lock(l_ptr->owner);
463 tipc_link_reset(l_ptr);
464 tipc_node_detach_link(l_ptr->owner, l_ptr);
465 tipc_link_stop(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 list_del_init(&l_ptr->link_list);
467 if (LINK_LOG_BUF_SIZE)
468 kfree(l_ptr->print_buf.buf);
Per Liden4323add2006-01-18 00:38:21 +0100469 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 k_term_timer(&l_ptr->timer);
471 kfree(l_ptr);
472}
473
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000474static void link_start(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475{
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000476 dbg("link_start %x\n", l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 link_state_event(l_ptr, STARTING_EVT);
478}
479
480/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 * @l_ptr: pointer to link
483 * @origport: reference to sending port
484 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900485 *
486 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 * has abated.
488 */
489
490static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
491{
492 struct port *p_ptr;
493
Per Liden4323add2006-01-18 00:38:21 +0100494 spin_lock_bh(&tipc_port_list_lock);
495 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 if (p_ptr) {
497 if (!p_ptr->wakeup)
498 goto exit;
499 if (!list_empty(&p_ptr->wait_list))
500 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 p_ptr->publ.congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000502 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
504 l_ptr->stats.link_congs++;
505exit:
Per Liden4323add2006-01-18 00:38:21 +0100506 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 }
Per Liden4323add2006-01-18 00:38:21 +0100508 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 return -ELINKCONG;
510}
511
Per Liden4323add2006-01-18 00:38:21 +0100512void tipc_link_wakeup_ports(struct link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513{
514 struct port *p_ptr;
515 struct port *temp_p_ptr;
516 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
517
518 if (all)
519 win = 100000;
520 if (win <= 0)
521 return;
Per Liden4323add2006-01-18 00:38:21 +0100522 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 return;
524 if (link_congested(l_ptr))
525 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900526 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 wait_list) {
528 if (win <= 0)
529 break;
530 list_del_init(&p_ptr->wait_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 spin_lock_bh(p_ptr->publ.lock);
532 p_ptr->publ.congested = 0;
533 p_ptr->wakeup(&p_ptr->publ);
534 win -= p_ptr->waiting_pkts;
535 spin_unlock_bh(p_ptr->publ.lock);
536 }
537
538exit:
Per Liden4323add2006-01-18 00:38:21 +0100539 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540}
541
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 * link_release_outqueue - purge link's outbound message queue
544 * @l_ptr: pointer to link
545 */
546
547static void link_release_outqueue(struct link *l_ptr)
548{
549 struct sk_buff *buf = l_ptr->first_out;
550 struct sk_buff *next;
551
552 while (buf) {
553 next = buf->next;
554 buf_discard(buf);
555 buf = next;
556 }
557 l_ptr->first_out = NULL;
558 l_ptr->out_queue_size = 0;
559}
560
561/**
Per Liden4323add2006-01-18 00:38:21 +0100562 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 * @l_ptr: pointer to link
564 */
565
Per Liden4323add2006-01-18 00:38:21 +0100566void tipc_link_reset_fragments(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567{
568 struct sk_buff *buf = l_ptr->defragm_buf;
569 struct sk_buff *next;
570
571 while (buf) {
572 next = buf->next;
573 buf_discard(buf);
574 buf = next;
575 }
576 l_ptr->defragm_buf = NULL;
577}
578
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579/**
Per Liden4323add2006-01-18 00:38:21 +0100580 * tipc_link_stop - purge all inbound and outbound messages associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 * @l_ptr: pointer to link
582 */
583
Per Liden4323add2006-01-18 00:38:21 +0100584void tipc_link_stop(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585{
586 struct sk_buff *buf;
587 struct sk_buff *next;
588
589 buf = l_ptr->oldest_deferred_in;
590 while (buf) {
591 next = buf->next;
592 buf_discard(buf);
593 buf = next;
594 }
595
596 buf = l_ptr->first_out;
597 while (buf) {
598 next = buf->next;
599 buf_discard(buf);
600 buf = next;
601 }
602
Per Liden4323add2006-01-18 00:38:21 +0100603 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604
605 buf_discard(l_ptr->proto_msg_queue);
606 l_ptr->proto_msg_queue = NULL;
607}
608
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610#define link_send_event(fcn, l_ptr, up) do { } while (0)
611
Per Liden4323add2006-01-18 00:38:21 +0100612void tipc_link_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613{
614 struct sk_buff *buf;
615 u32 prev_state = l_ptr->state;
616 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700617 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618
Allan Stephensa686e682008-06-04 17:29:39 -0700619 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620
Allan Stephensa686e682008-06-04 17:29:39 -0700621 /* Link is down, accept any session */
622 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900626
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 l_ptr->state = RESET_UNKNOWN;
628 dbg_link_state("Resetting Link\n");
629
630 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
631 return;
632
Per Liden4323add2006-01-18 00:38:21 +0100633 tipc_node_link_down(l_ptr->owner, l_ptr);
634 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000635
Allan Stephens5392d642006-06-25 23:52:50 -0700636 if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 l_ptr->owner->permit_changeover) {
638 l_ptr->reset_checkpoint = checkpoint;
639 l_ptr->exp_msg_count = START_CHANGEOVER;
640 }
641
642 /* Clean up all queues: */
643
644 link_release_outqueue(l_ptr);
645 buf_discard(l_ptr->proto_msg_queue);
646 l_ptr->proto_msg_queue = NULL;
647 buf = l_ptr->oldest_deferred_in;
648 while (buf) {
649 struct sk_buff *next = buf->next;
650 buf_discard(buf);
651 buf = next;
652 }
653 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100654 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655
656 l_ptr->retransm_queue_head = 0;
657 l_ptr->retransm_queue_size = 0;
658 l_ptr->last_out = NULL;
659 l_ptr->first_out = NULL;
660 l_ptr->next_out = NULL;
661 l_ptr->unacked_window = 0;
662 l_ptr->checkpoint = 1;
663 l_ptr->next_out_no = 1;
664 l_ptr->deferred_inqueue_sz = 0;
665 l_ptr->oldest_deferred_in = NULL;
666 l_ptr->newest_deferred_in = NULL;
667 l_ptr->fsm_msg_cnt = 0;
668 l_ptr->stale_count = 0;
669 link_reset_statistics(l_ptr);
670
Per Liden4323add2006-01-18 00:38:21 +0100671 link_send_event(tipc_cfg_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100673 link_send_event(tipc_disc_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674}
675
676
677static void link_activate(struct link *l_ptr)
678{
Allan Stephens5392d642006-06-25 23:52:50 -0700679 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100680 tipc_node_link_up(l_ptr->owner, l_ptr);
681 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
682 link_send_event(tipc_cfg_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100684 link_send_event(tipc_disc_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685}
686
687/**
688 * link_state_event - link finite state machine
689 * @l_ptr: pointer to link
690 * @event: state machine event to process
691 */
692
693static void link_state_event(struct link *l_ptr, unsigned event)
694{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900695 struct link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 u32 cont_intv = l_ptr->continuity_interval;
697
698 if (!l_ptr->started && (event != STARTING_EVT))
699 return; /* Not yet. */
700
701 if (link_blocked(l_ptr)) {
702 if (event == TIMEOUT_EVT) {
703 link_set_timer(l_ptr, cont_intv);
704 }
705 return; /* Changeover going on */
706 }
707 dbg_link("STATE_EV: <%s> ", l_ptr->name);
708
709 switch (l_ptr->state) {
710 case WORKING_WORKING:
711 dbg_link("WW/");
712 switch (event) {
713 case TRAFFIC_MSG_EVT:
714 dbg_link("TRF-");
715 /* fall through */
716 case ACTIVATE_MSG:
717 dbg_link("ACT\n");
718 break;
719 case TIMEOUT_EVT:
720 dbg_link("TIM ");
721 if (l_ptr->next_in_no != l_ptr->checkpoint) {
722 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100723 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900724 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100725 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 l_ptr->fsm_msg_cnt++;
727 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100729 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 l_ptr->fsm_msg_cnt++;
731 }
732 link_set_timer(l_ptr, cont_intv);
733 break;
734 }
735 dbg_link(" -> WU\n");
736 l_ptr->state = WORKING_UNKNOWN;
737 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100738 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 l_ptr->fsm_msg_cnt++;
740 link_set_timer(l_ptr, cont_intv / 4);
741 break;
742 case RESET_MSG:
743 dbg_link("RES -> RR\n");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900744 info("Resetting link <%s>, requested by peer\n",
Allan Stephensa10bd922006-06-25 23:52:17 -0700745 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100746 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 l_ptr->state = RESET_RESET;
748 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100749 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 l_ptr->fsm_msg_cnt++;
751 link_set_timer(l_ptr, cont_intv);
752 break;
753 default:
754 err("Unknown link event %u in WW state\n", event);
755 }
756 break;
757 case WORKING_UNKNOWN:
758 dbg_link("WU/");
759 switch (event) {
760 case TRAFFIC_MSG_EVT:
761 dbg_link("TRF-");
762 case ACTIVATE_MSG:
763 dbg_link("ACT -> WW\n");
764 l_ptr->state = WORKING_WORKING;
765 l_ptr->fsm_msg_cnt = 0;
766 link_set_timer(l_ptr, cont_intv);
767 break;
768 case RESET_MSG:
769 dbg_link("RES -> RR\n");
Allan Stephensa10bd922006-06-25 23:52:17 -0700770 info("Resetting link <%s>, requested by peer "
771 "while probing\n", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100772 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 l_ptr->state = RESET_RESET;
774 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100775 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 l_ptr->fsm_msg_cnt++;
777 link_set_timer(l_ptr, cont_intv);
778 break;
779 case TIMEOUT_EVT:
780 dbg_link("TIM ");
781 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Frans Popa570f092010-03-24 07:57:29 +0000782 dbg_link("-> WW\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 l_ptr->state = WORKING_WORKING;
784 l_ptr->fsm_msg_cnt = 0;
785 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100786 if (tipc_bclink_acks_missing(l_ptr->owner)) {
787 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
788 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 l_ptr->fsm_msg_cnt++;
790 }
791 link_set_timer(l_ptr, cont_intv);
792 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
793 dbg_link("Probing %u/%u,timer = %u ms)\n",
794 l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
795 cont_intv / 4);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900796 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100797 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 l_ptr->fsm_msg_cnt++;
799 link_set_timer(l_ptr, cont_intv / 4);
800 } else { /* Link has failed */
801 dbg_link("-> RU (%u probes unanswered)\n",
802 l_ptr->fsm_msg_cnt);
Allan Stephensa10bd922006-06-25 23:52:17 -0700803 warn("Resetting link <%s>, peer not responding\n",
804 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100805 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 l_ptr->state = RESET_UNKNOWN;
807 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100808 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
809 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 l_ptr->fsm_msg_cnt++;
811 link_set_timer(l_ptr, cont_intv);
812 }
813 break;
814 default:
815 err("Unknown link event %u in WU state\n", event);
816 }
817 break;
818 case RESET_UNKNOWN:
819 dbg_link("RU/");
820 switch (event) {
821 case TRAFFIC_MSG_EVT:
822 dbg_link("TRF-\n");
823 break;
824 case ACTIVATE_MSG:
825 other = l_ptr->owner->active_links[0];
826 if (other && link_working_unknown(other)) {
827 dbg_link("ACT\n");
828 break;
829 }
830 dbg_link("ACT -> WW\n");
831 l_ptr->state = WORKING_WORKING;
832 l_ptr->fsm_msg_cnt = 0;
833 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100834 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 l_ptr->fsm_msg_cnt++;
836 link_set_timer(l_ptr, cont_intv);
837 break;
838 case RESET_MSG:
Frans Popa570f092010-03-24 07:57:29 +0000839 dbg_link("RES\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 dbg_link(" -> RR\n");
841 l_ptr->state = RESET_RESET;
842 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100843 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844 l_ptr->fsm_msg_cnt++;
845 link_set_timer(l_ptr, cont_intv);
846 break;
847 case STARTING_EVT:
848 dbg_link("START-");
849 l_ptr->started = 1;
850 /* fall through */
851 case TIMEOUT_EVT:
Frans Popa570f092010-03-24 07:57:29 +0000852 dbg_link("TIM\n");
Per Liden4323add2006-01-18 00:38:21 +0100853 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 l_ptr->fsm_msg_cnt++;
855 link_set_timer(l_ptr, cont_intv);
856 break;
857 default:
858 err("Unknown link event %u in RU state\n", event);
859 }
860 break;
861 case RESET_RESET:
862 dbg_link("RR/ ");
863 switch (event) {
864 case TRAFFIC_MSG_EVT:
865 dbg_link("TRF-");
866 /* fall through */
867 case ACTIVATE_MSG:
868 other = l_ptr->owner->active_links[0];
869 if (other && link_working_unknown(other)) {
870 dbg_link("ACT\n");
871 break;
872 }
873 dbg_link("ACT -> WW\n");
874 l_ptr->state = WORKING_WORKING;
875 l_ptr->fsm_msg_cnt = 0;
876 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100877 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 l_ptr->fsm_msg_cnt++;
879 link_set_timer(l_ptr, cont_intv);
880 break;
881 case RESET_MSG:
882 dbg_link("RES\n");
883 break;
884 case TIMEOUT_EVT:
885 dbg_link("TIM\n");
Per Liden4323add2006-01-18 00:38:21 +0100886 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 l_ptr->fsm_msg_cnt++;
888 link_set_timer(l_ptr, cont_intv);
889 dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
890 break;
891 default:
892 err("Unknown link event %u in RR state\n", event);
893 }
894 break;
895 default:
896 err("Unknown link state %u/%u\n", l_ptr->state, event);
897 }
898}
899
900/*
901 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900902 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 */
904
905static int link_bundle_buf(struct link *l_ptr,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900906 struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 struct sk_buff *buf)
908{
909 struct tipc_msg *bundler_msg = buf_msg(bundler);
910 struct tipc_msg *msg = buf_msg(buf);
911 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700912 u32 bundle_size = msg_size(bundler_msg);
913 u32 to_pos = align(bundle_size);
914 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915
916 if (msg_user(bundler_msg) != MSG_BUNDLER)
917 return 0;
918 if (msg_type(bundler_msg) != OPEN_MSG)
919 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700920 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000922 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700923 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924
Allan Stephense49060c2006-06-29 12:32:46 -0700925 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300926 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 msg_set_size(bundler_msg, to_pos + size);
928 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
929 dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
930 msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
931 msg_dbg(msg, "PACKD:");
932 buf_discard(buf);
933 l_ptr->stats.sent_bundled++;
934 return 1;
935}
936
Sam Ravnborg05790c62006-03-20 22:37:04 -0800937static void link_add_to_outqueue(struct link *l_ptr,
938 struct sk_buff *buf,
939 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940{
941 u32 ack = mod(l_ptr->next_in_no - 1);
942 u32 seqno = mod(l_ptr->next_out_no++);
943
944 msg_set_word(msg, 2, ((ack << 16) | seqno));
945 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
946 buf->next = NULL;
947 if (l_ptr->first_out) {
948 l_ptr->last_out->next = buf;
949 l_ptr->last_out = buf;
950 } else
951 l_ptr->first_out = l_ptr->last_out = buf;
952 l_ptr->out_queue_size++;
953}
954
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900955/*
956 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957 * inside TIPC when the 'fast path' in tipc_send_buf
958 * has failed, and from link_send()
959 */
960
Per Liden4323add2006-01-18 00:38:21 +0100961int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962{
963 struct tipc_msg *msg = buf_msg(buf);
964 u32 size = msg_size(msg);
965 u32 dsz = msg_data_sz(msg);
966 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000967 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000969 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970
971 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
972
973 /* Match msg importance against queue limits: */
974
975 if (unlikely(queue_size >= queue_limit)) {
976 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
977 return link_schedule_port(l_ptr, msg_origport(msg),
978 size);
979 }
980 msg_dbg(msg, "TIPC: Congestion, throwing away\n");
981 buf_discard(buf);
982 if (imp > CONN_MANAGER) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700983 warn("Resetting link <%s>, send queue full", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100984 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 }
986 return dsz;
987 }
988
989 /* Fragmentation needed ? */
990
991 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000992 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993
994 /* Packet can be queued or sent: */
995
996 if (queue_size > l_ptr->stats.max_queue_sz)
997 l_ptr->stats.max_queue_sz = queue_size;
998
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900999 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 !link_congested(l_ptr))) {
1001 link_add_to_outqueue(l_ptr, buf, msg);
1002
Per Liden4323add2006-01-18 00:38:21 +01001003 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004 l_ptr->unacked_window = 0;
1005 } else {
Per Liden4323add2006-01-18 00:38:21 +01001006 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 l_ptr->stats.bearer_congs++;
1008 l_ptr->next_out = buf;
1009 }
1010 return dsz;
1011 }
1012 /* Congestion: can message be bundled ?: */
1013
1014 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
1015 (msg_user(msg) != MSG_FRAGMENTER)) {
1016
1017 /* Try adding message to an existing bundle */
1018
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019 if (l_ptr->next_out &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001021 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 return dsz;
1023 }
1024
1025 /* Try creating a new bundle */
1026
1027 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001028 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 struct tipc_msg bundler_hdr;
1030
1031 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001032 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -07001033 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001034 skb_copy_to_linear_data(bundler, &bundler_hdr,
1035 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 skb_trim(bundler, INT_H_SIZE);
1037 link_bundle_buf(l_ptr, bundler, buf);
1038 buf = bundler;
1039 msg = buf_msg(buf);
1040 l_ptr->stats.sent_bundles++;
1041 }
1042 }
1043 }
1044 if (!l_ptr->next_out)
1045 l_ptr->next_out = buf;
1046 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001047 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 return dsz;
1049}
1050
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001051/*
1052 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 * not been selected yet, and the the owner node is not locked
1054 * Called by TIPC internal users, e.g. the name distributor
1055 */
1056
Per Liden4323add2006-01-18 00:38:21 +01001057int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058{
1059 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001060 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 int res = -ELINKCONG;
1062
Per Liden4323add2006-01-18 00:38:21 +01001063 read_lock_bh(&tipc_net_lock);
1064 n_ptr = tipc_node_select(dest, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001066 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 l_ptr = n_ptr->active_links[selector & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 if (l_ptr) {
Allan Stephensc33d53b2006-06-25 23:50:30 -07001069 dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
Per Liden4323add2006-01-18 00:38:21 +01001070 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensc33d53b2006-06-25 23:50:30 -07001071 } else {
1072 dbg("Attempt to send msg to unreachable node:\n");
1073 msg_dbg(buf_msg(buf),">>>");
1074 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 }
Per Liden4323add2006-01-18 00:38:21 +01001076 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 } else {
1078 dbg("Attempt to send msg to unknown node:\n");
1079 msg_dbg(buf_msg(buf),">>>");
1080 buf_discard(buf);
1081 }
Per Liden4323add2006-01-18 00:38:21 +01001082 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 return res;
1084}
1085
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001086/*
1087 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 * destination link is known and the header is complete,
1089 * inclusive total message length. Very time critical.
1090 * Link is locked. Returns user data length.
1091 */
1092
Sam Ravnborg05790c62006-03-20 22:37:04 -08001093static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
1094 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095{
1096 struct tipc_msg *msg = buf_msg(buf);
1097 int res = msg_data_sz(msg);
1098
1099 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +00001100 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
1102 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001103 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
1104 &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105 l_ptr->unacked_window = 0;
1106 msg_dbg(msg,"SENT_FAST:");
1107 return res;
1108 }
1109 dbg("failed sent fast...\n");
Per Liden4323add2006-01-18 00:38:21 +01001110 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 l_ptr->stats.bearer_congs++;
1112 l_ptr->next_out = buf;
1113 return res;
1114 }
1115 }
1116 else
Allan Stephens15e979d2010-05-11 14:30:10 +00001117 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 }
Per Liden4323add2006-01-18 00:38:21 +01001119 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120}
1121
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001122/*
1123 * tipc_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 * destination node is known and the header is complete,
1125 * inclusive total message length.
1126 * Returns user data length.
1127 */
1128int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
1129{
1130 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001131 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 int res;
1133 u32 selector = msg_origport(buf_msg(buf)) & 1;
1134 u32 dummy;
1135
1136 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001137 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138
Per Liden4323add2006-01-18 00:38:21 +01001139 read_lock_bh(&tipc_net_lock);
1140 n_ptr = tipc_node_select(destnode, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 if (likely(n_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001142 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 l_ptr = n_ptr->active_links[selector];
1144 dbg("send_fast: buf %x selected %x, destnode = %x\n",
1145 buf, l_ptr, destnode);
1146 if (likely(l_ptr)) {
1147 res = link_send_buf_fast(l_ptr, buf, &dummy);
Per Liden4323add2006-01-18 00:38:21 +01001148 tipc_node_unlock(n_ptr);
1149 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 return res;
1151 }
Per Liden4323add2006-01-18 00:38:21 +01001152 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 }
Per Liden4323add2006-01-18 00:38:21 +01001154 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 res = msg_data_sz(buf_msg(buf));
1156 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1157 return res;
1158}
1159
1160
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001161/*
1162 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001164 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 * Returns user data length or errno.
1166 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001167int tipc_link_send_sections_fast(struct port *sender,
Per Liden4323add2006-01-18 00:38:21 +01001168 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001169 const u32 num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001170 u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171{
1172 struct tipc_msg *hdr = &sender->publ.phdr;
1173 struct link *l_ptr;
1174 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -07001175 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 int res;
1177 u32 selector = msg_origport(hdr) & 1;
1178
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179again:
1180 /*
1181 * Try building message using port's max_pkt hint.
1182 * (Must not hold any locks while building message.)
1183 */
1184
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001185 res = tipc_msg_build(hdr, msg_sect, num_sect, sender->publ.max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 !sender->user_port, &buf);
1187
Per Liden4323add2006-01-18 00:38:21 +01001188 read_lock_bh(&tipc_net_lock);
1189 node = tipc_node_select(destaddr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001191 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 l_ptr = node->active_links[selector];
1193 if (likely(l_ptr)) {
1194 if (likely(buf)) {
1195 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens05646c92007-06-10 17:25:24 -07001196 &sender->publ.max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 if (unlikely(res < 0))
1198 buf_discard(buf);
1199exit:
Per Liden4323add2006-01-18 00:38:21 +01001200 tipc_node_unlock(node);
1201 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 return res;
1203 }
1204
1205 /* Exit if build request was invalid */
1206
1207 if (unlikely(res < 0))
1208 goto exit;
1209
1210 /* Exit if link (or bearer) is congested */
1211
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001212 if (link_congested(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 !list_empty(&l_ptr->b_ptr->cong_links)) {
1214 res = link_schedule_port(l_ptr,
1215 sender->publ.ref, res);
1216 goto exit;
1217 }
1218
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001219 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 * Message size exceeds max_pkt hint; update hint,
1221 * then re-try fast path or fragment the message
1222 */
1223
Allan Stephens15e979d2010-05-11 14:30:10 +00001224 sender->publ.max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001225 tipc_node_unlock(node);
1226 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227
1228
Allan Stephens05646c92007-06-10 17:25:24 -07001229 if ((msg_hdr_sz(hdr) + res) <= sender->publ.max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 goto again;
1231
1232 return link_send_sections_long(sender, msg_sect,
1233 num_sect, destaddr);
1234 }
Per Liden4323add2006-01-18 00:38:21 +01001235 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 }
Per Liden4323add2006-01-18 00:38:21 +01001237 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238
1239 /* Couldn't find a link to the destination node */
1240
1241 if (buf)
1242 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1243 if (res >= 0)
Per Liden4323add2006-01-18 00:38:21 +01001244 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1245 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 return res;
1247}
1248
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001249/*
1250 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001252 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 * Link and bearer congestion status have been checked to be ok,
1254 * and are ignored if they change.
1255 *
1256 * Note that fragments do not use the full link MTU so that they won't have
1257 * to undergo refragmentation if link changeover causes them to be sent
1258 * over another link with an additional tunnel header added as prefix.
1259 * (Refragmentation will still occur if the other link has a smaller MTU.)
1260 *
1261 * Returns user data length or errno.
1262 */
1263static int link_send_sections_long(struct port *sender,
1264 struct iovec const *msg_sect,
1265 u32 num_sect,
1266 u32 destaddr)
1267{
1268 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001269 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 struct tipc_msg *hdr = &sender->publ.phdr;
1271 u32 dsz = msg_data_sz(hdr);
1272 u32 max_pkt,fragm_sz,rest;
1273 struct tipc_msg fragm_hdr;
1274 struct sk_buff *buf,*buf_chain,*prev;
1275 u32 fragm_crs,fragm_rest,hsz,sect_rest;
1276 const unchar *sect_crs;
1277 int curr_sect;
1278 u32 fragm_no;
1279
1280again:
1281 fragm_no = 1;
Allan Stephens05646c92007-06-10 17:25:24 -07001282 max_pkt = sender->publ.max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001284 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 /* leave room for fragmentation header in each fragment */
1286 rest = dsz;
1287 fragm_crs = 0;
1288 fragm_rest = 0;
1289 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001290 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291 curr_sect = -1;
1292
1293 /* Prepare reusable fragment header: */
1294
1295 msg_dbg(hdr, ">FRAGMENTING>");
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001296 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001297 INT_H_SIZE, msg_destnode(hdr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1299 msg_set_size(&fragm_hdr, max_pkt);
1300 msg_set_fragm_no(&fragm_hdr, 1);
1301
1302 /* Prepare header of first fragment: */
1303
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001304 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 if (!buf)
1306 return -ENOMEM;
1307 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001308 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001310 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 msg_dbg(buf_msg(buf), ">BUILD>");
1312
1313 /* Chop up message: */
1314
1315 fragm_crs = INT_H_SIZE + hsz;
1316 fragm_rest = fragm_sz - hsz;
1317
1318 do { /* For all sections */
1319 u32 sz;
1320
1321 if (!sect_rest) {
1322 sect_rest = msg_sect[++curr_sect].iov_len;
1323 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1324 }
1325
1326 if (sect_rest < fragm_rest)
1327 sz = sect_rest;
1328 else
1329 sz = fragm_rest;
1330
1331 if (likely(!sender->user_port)) {
1332 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1333error:
1334 for (; buf_chain; buf_chain = buf) {
1335 buf = buf_chain->next;
1336 buf_discard(buf_chain);
1337 }
1338 return -EFAULT;
1339 }
1340 } else
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001341 skb_copy_to_linear_data_offset(buf, fragm_crs,
1342 sect_crs, sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 sect_crs += sz;
1344 sect_rest -= sz;
1345 fragm_crs += sz;
1346 fragm_rest -= sz;
1347 rest -= sz;
1348
1349 if (!fragm_rest && rest) {
1350
1351 /* Initiate new fragment: */
1352 if (rest <= fragm_sz) {
1353 fragm_sz = rest;
1354 msg_set_type(&fragm_hdr,LAST_FRAGMENT);
1355 } else {
1356 msg_set_type(&fragm_hdr, FRAGMENT);
1357 }
1358 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1359 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1360 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001361 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 if (!buf)
1363 goto error;
1364
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001365 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001367 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 fragm_crs = INT_H_SIZE;
1369 fragm_rest = fragm_sz;
1370 msg_dbg(buf_msg(buf)," >BUILD>");
1371 }
1372 }
1373 while (rest > 0);
1374
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001375 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 * Now we have a buffer chain. Select a link and check
1377 * that packet size is still OK
1378 */
Per Liden4323add2006-01-18 00:38:21 +01001379 node = tipc_node_select(destaddr, sender->publ.ref & 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001381 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 l_ptr = node->active_links[sender->publ.ref & 1];
1383 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001384 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 goto reject;
1386 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001387 if (l_ptr->max_pkt < max_pkt) {
1388 sender->publ.max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001389 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 for (; buf_chain; buf_chain = buf) {
1391 buf = buf_chain->next;
1392 buf_discard(buf_chain);
1393 }
1394 goto again;
1395 }
1396 } else {
1397reject:
1398 for (; buf_chain; buf_chain = buf) {
1399 buf = buf_chain->next;
1400 buf_discard(buf_chain);
1401 }
Per Liden4323add2006-01-18 00:38:21 +01001402 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1403 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 }
1405
1406 /* Append whole chain to send queue: */
1407
1408 buf = buf_chain;
1409 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1410 if (!l_ptr->next_out)
1411 l_ptr->next_out = buf_chain;
1412 l_ptr->stats.sent_fragmented++;
1413 while (buf) {
1414 struct sk_buff *next = buf->next;
1415 struct tipc_msg *msg = buf_msg(buf);
1416
1417 l_ptr->stats.sent_fragments++;
1418 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1419 link_add_to_outqueue(l_ptr, buf, msg);
1420 msg_dbg(msg, ">ADD>");
1421 buf = next;
1422 }
1423
1424 /* Send it, if possible: */
1425
Per Liden4323add2006-01-18 00:38:21 +01001426 tipc_link_push_queue(l_ptr);
1427 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 return dsz;
1429}
1430
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001431/*
Per Liden4323add2006-01-18 00:38:21 +01001432 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 */
Per Liden4323add2006-01-18 00:38:21 +01001434u32 tipc_link_push_packet(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435{
1436 struct sk_buff *buf = l_ptr->first_out;
1437 u32 r_q_size = l_ptr->retransm_queue_size;
1438 u32 r_q_head = l_ptr->retransm_queue_head;
1439
1440 /* Step to position where retransmission failed, if any, */
1441 /* consider that buffers may have been released in meantime */
1442
1443 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001444 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445 link_last_sent(l_ptr));
1446 u32 first = msg_seqno(buf_msg(buf));
1447
1448 while (buf && less(first, r_q_head)) {
1449 first = mod(first + 1);
1450 buf = buf->next;
1451 }
1452 l_ptr->retransm_queue_head = r_q_head = first;
1453 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1454 }
1455
1456 /* Continue retransmission now, if there is anything: */
1457
Neil Hormanca509102010-03-15 07:58:45 +00001458 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001460 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001461 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462 msg_dbg(buf_msg(buf), ">DEF-RETR>");
1463 l_ptr->retransm_queue_head = mod(++r_q_head);
1464 l_ptr->retransm_queue_size = --r_q_size;
1465 l_ptr->stats.retransmitted++;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001466 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 } else {
1468 l_ptr->stats.bearer_congs++;
1469 msg_dbg(buf_msg(buf), "|>DEF-RETR>");
1470 return PUSH_FAILED;
1471 }
1472 }
1473
1474 /* Send deferred protocol message, if any: */
1475
1476 buf = l_ptr->proto_msg_queue;
1477 if (buf) {
1478 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001479 msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001480 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481 msg_dbg(buf_msg(buf), ">DEF-PROT>");
1482 l_ptr->unacked_window = 0;
1483 buf_discard(buf);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001484 l_ptr->proto_msg_queue = NULL;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001485 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 } else {
1487 msg_dbg(buf_msg(buf), "|>DEF-PROT>");
1488 l_ptr->stats.bearer_congs++;
1489 return PUSH_FAILED;
1490 }
1491 }
1492
1493 /* Send one deferred data message, if send window not full: */
1494
1495 buf = l_ptr->next_out;
1496 if (buf) {
1497 struct tipc_msg *msg = buf_msg(buf);
1498 u32 next = msg_seqno(msg);
1499 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1500
1501 if (mod(next - first) < l_ptr->queue_limit[0]) {
1502 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001503 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001504 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 if (msg_user(msg) == MSG_BUNDLER)
1506 msg_set_type(msg, CLOSED_MSG);
1507 msg_dbg(msg, ">PUSH-DATA>");
1508 l_ptr->next_out = buf->next;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001509 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 } else {
1511 msg_dbg(msg, "|PUSH-DATA|");
1512 l_ptr->stats.bearer_congs++;
1513 return PUSH_FAILED;
1514 }
1515 }
1516 }
1517 return PUSH_FINISHED;
1518}
1519
1520/*
1521 * push_queue(): push out the unsent messages of a link where
1522 * congestion has abated. Node is locked
1523 */
Per Liden4323add2006-01-18 00:38:21 +01001524void tipc_link_push_queue(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525{
1526 u32 res;
1527
Per Liden4323add2006-01-18 00:38:21 +01001528 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529 return;
1530
1531 do {
Per Liden4323add2006-01-18 00:38:21 +01001532 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001533 } while (!res);
1534
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 if (res == PUSH_FAILED)
Per Liden4323add2006-01-18 00:38:21 +01001536 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537}
1538
Allan Stephensd356eeb2006-06-25 23:40:01 -07001539static void link_reset_all(unsigned long addr)
1540{
David S. Miller6c000552008-09-02 23:38:32 -07001541 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001542 char addr_string[16];
1543 u32 i;
1544
1545 read_lock_bh(&tipc_net_lock);
1546 n_ptr = tipc_node_find((u32)addr);
1547 if (!n_ptr) {
1548 read_unlock_bh(&tipc_net_lock);
1549 return; /* node no longer exists */
1550 }
1551
1552 tipc_node_lock(n_ptr);
1553
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001554 warn("Resetting all links to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001555 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001556
1557 for (i = 0; i < MAX_BEARERS; i++) {
1558 if (n_ptr->links[i]) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001559 link_print(n_ptr->links[i], TIPC_OUTPUT,
Allan Stephensd356eeb2006-06-25 23:40:01 -07001560 "Resetting link\n");
1561 tipc_link_reset(n_ptr->links[i]);
1562 }
1563 }
1564
1565 tipc_node_unlock(n_ptr);
1566 read_unlock_bh(&tipc_net_lock);
1567}
1568
1569static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1570{
1571 struct tipc_msg *msg = buf_msg(buf);
1572
1573 warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephens48c97132008-05-05 01:24:06 -07001574 tipc_msg_dbg(TIPC_OUTPUT, msg, ">RETR-FAIL>");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001575
1576 if (l_ptr->addr) {
1577
1578 /* Handle failure on standard link */
1579
1580 link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
1581 tipc_link_reset(l_ptr);
1582
1583 } else {
1584
1585 /* Handle failure on broadcast link */
1586
David S. Miller6c000552008-09-02 23:38:32 -07001587 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001588 char addr_string[16];
1589
1590 tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg));
Jeff Garzik617dbea2006-10-03 16:25:34 -07001591 tipc_printf(TIPC_OUTPUT, "Outstanding acks: %lu\n",
1592 (unsigned long) TIPC_SKB_CB(buf)->handle);
1593
Allan Stephensd356eeb2006-06-25 23:40:01 -07001594 n_ptr = l_ptr->owner->next;
1595 tipc_node_lock(n_ptr);
1596
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001597 tipc_addr_string_fill(addr_string, n_ptr->addr);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001598 tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
1599 tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported);
1600 tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
1601 tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in);
1602 tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after);
1603 tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
1604 tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1605
1606 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1607
1608 tipc_node_unlock(n_ptr);
1609
1610 l_ptr->stale_count = 0;
1611 }
1612}
1613
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001614void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001615 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616{
1617 struct tipc_msg *msg;
1618
Allan Stephensd356eeb2006-06-25 23:40:01 -07001619 if (!buf)
1620 return;
1621
1622 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001623
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
1625
Allan Stephensd356eeb2006-06-25 23:40:01 -07001626 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Neil Hormanca509102010-03-15 07:58:45 +00001627 if (l_ptr->retransm_queue_size == 0) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001628 msg_dbg(msg, ">NO_RETR->BCONG>");
1629 dbg_print_link(l_ptr, " ");
1630 l_ptr->retransm_queue_head = msg_seqno(msg);
1631 l_ptr->retransm_queue_size = retransmits;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001632 } else {
Neil Hormanca509102010-03-15 07:58:45 +00001633 err("Unexpected retransmit on link %s (qsize=%d)\n",
1634 l_ptr->name, l_ptr->retransm_queue_size);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001635 }
Neil Hormanca509102010-03-15 07:58:45 +00001636 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001637 } else {
1638 /* Detect repeated retransmit failures on uncongested bearer */
1639
1640 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1641 if (++l_ptr->stale_count > 100) {
1642 link_retransmit_failure(l_ptr, buf);
1643 return;
1644 }
1645 } else {
1646 l_ptr->last_retransmitted = msg_seqno(msg);
1647 l_ptr->stale_count = 1;
1648 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001649 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001650
Neil Hormanca509102010-03-15 07:58:45 +00001651 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 msg = buf_msg(buf);
1653 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001654 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001655 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 msg_dbg(buf_msg(buf), ">RETR>");
1657 buf = buf->next;
1658 retransmits--;
1659 l_ptr->stats.retransmitted++;
1660 } else {
Per Liden4323add2006-01-18 00:38:21 +01001661 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662 l_ptr->stats.bearer_congs++;
1663 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1664 l_ptr->retransm_queue_size = retransmits;
1665 return;
1666 }
1667 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001668
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1670}
1671
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001672/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673 * link_insert_deferred_queue - insert deferred messages back into receive chain
1674 */
1675
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001676static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 struct sk_buff *buf)
1678{
1679 u32 seq_no;
1680
1681 if (l_ptr->oldest_deferred_in == NULL)
1682 return buf;
1683
1684 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1685 if (seq_no == mod(l_ptr->next_in_no)) {
1686 l_ptr->newest_deferred_in->next = buf;
1687 buf = l_ptr->oldest_deferred_in;
1688 l_ptr->oldest_deferred_in = NULL;
1689 l_ptr->deferred_inqueue_sz = 0;
1690 }
1691 return buf;
1692}
1693
Allan Stephens85035562008-04-15 19:04:54 -07001694/**
1695 * link_recv_buf_validate - validate basic format of received message
1696 *
1697 * This routine ensures a TIPC message has an acceptable header, and at least
1698 * as much data as the header indicates it should. The routine also ensures
1699 * that the entire message header is stored in the main fragment of the message
1700 * buffer, to simplify future access to message header fields.
1701 *
1702 * Note: Having extra info present in the message header or data areas is OK.
1703 * TIPC will ignore the excess, under the assumption that it is optional info
1704 * introduced by a later release of the protocol.
1705 */
1706
1707static int link_recv_buf_validate(struct sk_buff *buf)
1708{
1709 static u32 min_data_hdr_size[8] = {
1710 SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
1711 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1712 };
1713
1714 struct tipc_msg *msg;
1715 u32 tipc_hdr[2];
1716 u32 size;
1717 u32 hdr_size;
1718 u32 min_hdr_size;
1719
1720 if (unlikely(buf->len < MIN_H_SIZE))
1721 return 0;
1722
1723 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1724 if (msg == NULL)
1725 return 0;
1726
1727 if (unlikely(msg_version(msg) != TIPC_VERSION))
1728 return 0;
1729
1730 size = msg_size(msg);
1731 hdr_size = msg_hdr_sz(msg);
1732 min_hdr_size = msg_isdata(msg) ?
1733 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1734
1735 if (unlikely((hdr_size < min_hdr_size) ||
1736 (size < hdr_size) ||
1737 (buf->len < size) ||
1738 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1739 return 0;
1740
1741 return pskb_may_pull(buf, hdr_size);
1742}
1743
Allan Stephensb02b69c2010-08-17 11:00:07 +00001744/**
1745 * tipc_recv_msg - process TIPC messages arriving from off-node
1746 * @head: pointer to message buffer chain
1747 * @tb_ptr: pointer to bearer message arrived on
1748 *
1749 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1750 * structure (i.e. cannot be NULL), but bearer can be inactive.
1751 */
1752
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1754{
Per Liden4323add2006-01-18 00:38:21 +01001755 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 while (head) {
Allan Stephens1265a022008-06-04 17:32:35 -07001757 struct bearer *b_ptr = (struct bearer *)tb_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001758 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759 struct link *l_ptr;
1760 struct sk_buff *crs;
1761 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001762 struct tipc_msg *msg;
1763 u32 seq_no;
1764 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 u32 released = 0;
1766 int type;
1767
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 head = head->next;
Allan Stephens85035562008-04-15 19:04:54 -07001769
Allan Stephensb02b69c2010-08-17 11:00:07 +00001770 /* Ensure bearer is still enabled */
1771
1772 if (unlikely(!b_ptr->active))
1773 goto cont;
1774
Allan Stephens85035562008-04-15 19:04:54 -07001775 /* Ensure message is well-formed */
1776
1777 if (unlikely(!link_recv_buf_validate(buf)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778 goto cont;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779
Allan Stephensfe13dda2008-04-15 19:03:23 -07001780 /* Ensure message data is a single contiguous unit */
1781
1782 if (unlikely(buf_linearize(buf))) {
1783 goto cont;
1784 }
1785
Allan Stephens85035562008-04-15 19:04:54 -07001786 /* Handle arrival of a non-unicast link message */
1787
1788 msg = buf_msg(buf);
1789
Per Lidenb97bf3f2006-01-02 19:04:38 +01001790 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001791 if (msg_user(msg) == LINK_CONFIG)
1792 tipc_disc_recv_msg(buf, b_ptr);
1793 else
1794 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795 continue;
1796 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001797
Allan Stephens26008242006-06-25 23:39:31 -07001798 if (unlikely(!msg_short(msg) &&
1799 (msg_destnode(msg) != tipc_own_addr)))
1800 goto cont;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001801
Neil Hormande586572010-03-08 12:43:56 -08001802 /* Discard non-routeable messages destined for another node */
1803
1804 if (unlikely(!msg_isdata(msg) &&
1805 (msg_destnode(msg) != tipc_own_addr))) {
1806 if ((msg_user(msg) != CONN_MANAGER) &&
1807 (msg_user(msg) != MSG_FRAGMENTER))
1808 goto cont;
1809 }
1810
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001811 /* Locate neighboring node that sent message */
Allan Stephens85035562008-04-15 19:04:54 -07001812
Per Liden4323add2006-01-18 00:38:21 +01001813 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 if (unlikely(!n_ptr))
1815 goto cont;
Per Liden4323add2006-01-18 00:38:21 +01001816 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001817
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001818 /* Don't talk to neighbor during cleanup after last session */
1819
1820 if (n_ptr->cleanup_required) {
1821 tipc_node_unlock(n_ptr);
1822 goto cont;
1823 }
1824
1825 /* Locate unicast link endpoint that should handle message */
1826
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 l_ptr = n_ptr->links[b_ptr->identity];
1828 if (unlikely(!l_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001829 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 goto cont;
1831 }
Allan Stephens85035562008-04-15 19:04:54 -07001832
1833 /* Validate message sequence number info */
1834
1835 seq_no = msg_seqno(msg);
1836 ackd = msg_ack(msg);
1837
1838 /* Release acked messages */
1839
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001841 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1842 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843 }
1844
1845 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001846 while ((crs != l_ptr->next_out) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1848 struct sk_buff *next = crs->next;
1849
1850 buf_discard(crs);
1851 crs = next;
1852 released++;
1853 }
1854 if (released) {
1855 l_ptr->first_out = crs;
1856 l_ptr->out_queue_size -= released;
1857 }
Allan Stephens85035562008-04-15 19:04:54 -07001858
1859 /* Try sending any messages link endpoint has pending */
1860
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001862 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001864 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1866 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001867 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868 }
1869
Allan Stephens85035562008-04-15 19:04:54 -07001870 /* Now (finally!) process the incoming message */
1871
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872protocol_check:
1873 if (likely(link_working_working(l_ptr))) {
1874 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1875 l_ptr->next_in_no++;
1876 if (unlikely(l_ptr->oldest_deferred_in))
1877 head = link_insert_deferred_queue(l_ptr,
1878 head);
1879 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1880deliver:
1881 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001882 tipc_node_unlock(n_ptr);
1883 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 continue;
1885 }
1886 switch (msg_user(msg)) {
1887 case MSG_BUNDLER:
1888 l_ptr->stats.recv_bundles++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001889 l_ptr->stats.recv_bundled +=
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +01001891 tipc_node_unlock(n_ptr);
1892 tipc_link_recv_bundle(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 continue;
1894 case ROUTE_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001895 tipc_node_unlock(n_ptr);
1896 tipc_cltr_recv_routing_table(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897 continue;
1898 case NAME_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001899 tipc_node_unlock(n_ptr);
1900 tipc_named_recv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 continue;
1902 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001903 tipc_node_unlock(n_ptr);
1904 tipc_port_recv_proto_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905 continue;
1906 case MSG_FRAGMENTER:
1907 l_ptr->stats.recv_fragments++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001908 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
Per Liden4323add2006-01-18 00:38:21 +01001909 &buf, &msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 l_ptr->stats.recv_fragmented++;
1911 goto deliver;
1912 }
1913 break;
1914 case CHANGEOVER_PROTOCOL:
1915 type = msg_type(msg);
Per Liden4323add2006-01-18 00:38:21 +01001916 if (link_recv_changeover_msg(&l_ptr, &buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917 msg = buf_msg(buf);
1918 seq_no = msg_seqno(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919 if (type == ORIGINAL_MSG)
1920 goto deliver;
1921 goto protocol_check;
1922 }
1923 break;
1924 }
1925 }
Per Liden4323add2006-01-18 00:38:21 +01001926 tipc_node_unlock(n_ptr);
1927 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928 continue;
1929 }
1930 link_handle_out_of_seq_msg(l_ptr, buf);
1931 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001932 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933 continue;
1934 }
1935
1936 if (msg_user(msg) == LINK_PROTOCOL) {
1937 link_recv_proto_msg(l_ptr, buf);
1938 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001939 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 continue;
1941 }
1942 msg_dbg(msg,"NSEQ<REC<");
1943 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1944
1945 if (link_working_working(l_ptr)) {
1946 /* Re-insert in front of queue */
1947 msg_dbg(msg,"RECV-REINS:");
1948 buf->next = head;
1949 head = buf;
Per Liden4323add2006-01-18 00:38:21 +01001950 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 continue;
1952 }
Per Liden4323add2006-01-18 00:38:21 +01001953 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954cont:
1955 buf_discard(buf);
1956 }
Per Liden4323add2006-01-18 00:38:21 +01001957 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958}
1959
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001960/*
1961 * link_defer_buf(): Sort a received out-of-sequence packet
Per Lidenb97bf3f2006-01-02 19:04:38 +01001962 * into the deferred reception queue.
1963 * Returns the increase of the queue length,i.e. 0 or 1
1964 */
1965
Per Liden4323add2006-01-18 00:38:21 +01001966u32 tipc_link_defer_pkt(struct sk_buff **head,
1967 struct sk_buff **tail,
1968 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001969{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001970 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971 struct sk_buff *crs = *head;
1972 u32 seq_no = msg_seqno(buf_msg(buf));
1973
1974 buf->next = NULL;
1975
1976 /* Empty queue ? */
1977 if (*head == NULL) {
1978 *head = *tail = buf;
1979 return 1;
1980 }
1981
1982 /* Last ? */
1983 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1984 (*tail)->next = buf;
1985 *tail = buf;
1986 return 1;
1987 }
1988
1989 /* Scan through queue and sort it in */
1990 do {
1991 struct tipc_msg *msg = buf_msg(crs);
1992
1993 if (less(seq_no, msg_seqno(msg))) {
1994 buf->next = crs;
1995 if (prev)
1996 prev->next = buf;
1997 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001998 *head = buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999 return 1;
2000 }
2001 if (seq_no == msg_seqno(msg)) {
2002 break;
2003 }
2004 prev = crs;
2005 crs = crs->next;
2006 }
2007 while (crs);
2008
2009 /* Message is a duplicate of an existing message */
2010
2011 buf_discard(buf);
2012 return 0;
2013}
2014
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002015/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01002016 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
2017 */
2018
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002019static void link_handle_out_of_seq_msg(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 struct sk_buff *buf)
2021{
2022 u32 seq_no = msg_seqno(buf_msg(buf));
2023
2024 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
2025 link_recv_proto_msg(l_ptr, buf);
2026 return;
2027 }
2028
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002029 dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030 seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
2031
2032 /* Record OOS packet arrival (force mismatch on next timeout) */
2033
2034 l_ptr->checkpoint--;
2035
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002036 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037 * Discard packet if a duplicate; otherwise add it to deferred queue
2038 * and notify peer of gap as per protocol specification
2039 */
2040
2041 if (less(seq_no, mod(l_ptr->next_in_no))) {
2042 l_ptr->stats.duplicates++;
2043 buf_discard(buf);
2044 return;
2045 }
2046
Per Liden4323add2006-01-18 00:38:21 +01002047 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
2048 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002049 l_ptr->deferred_inqueue_sz++;
2050 l_ptr->stats.deferred_recv++;
2051 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01002052 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053 } else
2054 l_ptr->stats.duplicates++;
2055}
2056
2057/*
2058 * Send protocol message to the other endpoint.
2059 */
Per Liden4323add2006-01-18 00:38:21 +01002060void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
2061 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002063 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002065 u32 msg_size = sizeof(l_ptr->proto_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002066
2067 if (link_blocked(l_ptr))
2068 return;
2069 msg_set_type(msg, msg_typ);
2070 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002071 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
Per Liden4323add2006-01-18 00:38:21 +01002072 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073
2074 if (msg_typ == STATE_MSG) {
2075 u32 next_sent = mod(l_ptr->next_out_no);
2076
Per Liden4323add2006-01-18 00:38:21 +01002077 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078 return;
2079 if (l_ptr->next_out)
2080 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
2081 msg_set_next_sent(msg, next_sent);
2082 if (l_ptr->oldest_deferred_in) {
2083 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2084 gap = mod(rec - mod(l_ptr->next_in_no));
2085 }
2086 msg_set_seq_gap(msg, gap);
2087 if (gap)
2088 l_ptr->stats.sent_nacks++;
2089 msg_set_link_tolerance(msg, tolerance);
2090 msg_set_linkprio(msg, priority);
2091 msg_set_max_pkt(msg, ack_mtu);
2092 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
2093 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002094 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095 u32 mtu = l_ptr->max_pkt;
2096
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002097 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098 link_working_working(l_ptr) &&
2099 l_ptr->fsm_msg_cnt) {
2100 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002101 if (l_ptr->max_pkt_probes == 10) {
2102 l_ptr->max_pkt_target = (msg_size - 4);
2103 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002105 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002107 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108
2109 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002110 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002111 l_ptr->stats.sent_states++;
2112 } else { /* RESET_MSG or ACTIVATE_MSG */
2113 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
2114 msg_set_seq_gap(msg, 0);
2115 msg_set_next_sent(msg, 1);
2116 msg_set_link_tolerance(msg, l_ptr->tolerance);
2117 msg_set_linkprio(msg, l_ptr->priority);
2118 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
2119 }
2120
Per Liden4323add2006-01-18 00:38:21 +01002121 if (tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122 msg_set_redundant_link(msg);
2123 } else {
2124 msg_clear_redundant_link(msg);
2125 }
2126 msg_set_linkprio(msg, l_ptr->priority);
2127
2128 /* Ensure sequence number will not fit : */
2129
2130 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
2131
2132 /* Congestion? */
2133
Per Liden4323add2006-01-18 00:38:21 +01002134 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135 if (!l_ptr->proto_msg_queue) {
2136 l_ptr->proto_msg_queue =
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002137 tipc_buf_acquire(sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002138 }
2139 buf = l_ptr->proto_msg_queue;
2140 if (!buf)
2141 return;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002142 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143 return;
2144 }
2145 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
2146
2147 /* Message can be sent */
2148
2149 msg_dbg(msg, ">>");
2150
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002151 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 if (!buf)
2153 return;
2154
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002155 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002156 msg_set_size(buf_msg(buf), msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157
Per Liden4323add2006-01-18 00:38:21 +01002158 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159 l_ptr->unacked_window = 0;
2160 buf_discard(buf);
2161 return;
2162 }
2163
2164 /* New congestion */
Per Liden4323add2006-01-18 00:38:21 +01002165 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002166 l_ptr->proto_msg_queue = buf;
2167 l_ptr->stats.bearer_congs++;
2168}
2169
2170/*
2171 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002172 * Note that network plane id propagates through the network, and may
2173 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01002174 */
2175
2176static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2177{
2178 u32 rec_gap = 0;
2179 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002180 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002181 u32 msg_tol;
2182 struct tipc_msg *msg = buf_msg(buf);
2183
2184 dbg("AT(%u):", jiffies_to_msecs(jiffies));
2185 msg_dbg(msg, "<<");
2186 if (link_blocked(l_ptr))
2187 goto exit;
2188
2189 /* record unnumbered packet arrival (force mismatch on next timeout) */
2190
2191 l_ptr->checkpoint--;
2192
2193 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2194 if (tipc_own_addr > msg_prevnode(msg))
2195 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2196
2197 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2198
2199 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002200
Per Lidenb97bf3f2006-01-02 19:04:38 +01002201 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07002202 if (!link_working_unknown(l_ptr) &&
2203 (l_ptr->peer_session != INVALID_SESSION)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002204 if (msg_session(msg) == l_ptr->peer_session) {
2205 dbg("Duplicate RESET: %u<->%u\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002206 msg_session(msg), l_ptr->peer_session);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002207 break; /* duplicate: ignore */
2208 }
2209 }
2210 /* fall thru' */
2211 case ACTIVATE_MSG:
2212 /* Update link settings according other endpoint's values */
2213
2214 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2215
2216 if ((msg_tol = msg_link_tolerance(msg)) &&
2217 (msg_tol > l_ptr->tolerance))
2218 link_set_supervision_props(l_ptr, msg_tol);
2219
2220 if (msg_linkprio(msg) > l_ptr->priority)
2221 l_ptr->priority = msg_linkprio(msg);
2222
2223 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002224 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002225 if (max_pkt_info < l_ptr->max_pkt_target)
2226 l_ptr->max_pkt_target = max_pkt_info;
2227 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2228 l_ptr->max_pkt = l_ptr->max_pkt_target;
2229 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002230 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002231 }
2232 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2233
2234 link_state_event(l_ptr, msg_type(msg));
2235
2236 l_ptr->peer_session = msg_session(msg);
2237 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2238
2239 /* Synchronize broadcast sequence numbers */
Per Liden4323add2006-01-18 00:38:21 +01002240 if (!tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002241 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2242 }
2243 break;
2244 case STATE_MSG:
2245
2246 if ((msg_tol = msg_link_tolerance(msg)))
2247 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002248
2249 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002250 (msg_linkprio(msg) != l_ptr->priority)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002251 warn("Resetting link <%s>, priority change %u->%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002252 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2253 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01002254 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002255 break;
2256 }
2257 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2258 l_ptr->stats.recv_states++;
2259 if (link_reset_unknown(l_ptr))
2260 break;
2261
2262 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002263 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01002264 mod(l_ptr->next_in_no));
2265 }
2266
2267 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002268 if (max_pkt_ack > l_ptr->max_pkt) {
2269 dbg("Link <%s> updated MTU %u -> %u\n",
2270 l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
2271 l_ptr->max_pkt = max_pkt_ack;
2272 l_ptr->max_pkt_probes = 0;
2273 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002274
2275 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002276 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002277 l_ptr->stats.recv_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002278 if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2279 max_pkt_ack = msg_size(msg);
2280 }
2281 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002282
2283 /* Protocol message before retransmits, reduce loss risk */
2284
Per Liden4323add2006-01-18 00:38:21 +01002285 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286
2287 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01002288 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2289 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002290 }
2291 if (msg_seq_gap(msg)) {
2292 msg_dbg(msg, "With Gap:");
2293 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01002294 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2295 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002296 }
2297 break;
2298 default:
2299 msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
2300 }
2301exit:
2302 buf_discard(buf);
2303}
2304
2305
2306/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002307 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01002308 * another bearer. Owner node is locked.
2309 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002310static void tipc_link_tunnel(struct link *l_ptr,
2311 struct tipc_msg *tunnel_hdr,
2312 struct tipc_msg *msg,
2313 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002314{
2315 struct link *tunnel;
2316 struct sk_buff *buf;
2317 u32 length = msg_size(msg);
2318
2319 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07002320 if (!tipc_link_is_up(tunnel)) {
2321 warn("Link changeover error, "
2322 "tunnel link no longer available\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002323 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002324 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002325 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002326 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07002327 if (!buf) {
2328 warn("Link changeover error, "
2329 "unable to send tunnel msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002330 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002331 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002332 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2333 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002334 dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
2335 msg_dbg(buf_msg(buf), ">SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002336 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337}
2338
2339
2340
2341/*
2342 * changeover(): Send whole message queue via the remaining link
2343 * Owner node is locked.
2344 */
2345
Per Liden4323add2006-01-18 00:38:21 +01002346void tipc_link_changeover(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002347{
2348 u32 msgcount = l_ptr->out_queue_size;
2349 struct sk_buff *crs = l_ptr->first_out;
2350 struct link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002351 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002352 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002353
2354 if (!tunnel)
2355 return;
2356
Allan Stephens5392d642006-06-25 23:52:50 -07002357 if (!l_ptr->owner->permit_changeover) {
2358 warn("Link changeover error, "
2359 "peer did not permit changeover\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002360 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002361 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002363 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002364 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002365 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2366 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephens5392d642006-06-25 23:52:50 -07002367 dbg("Link changeover requires %u tunnel messages\n", msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002368
Per Lidenb97bf3f2006-01-02 19:04:38 +01002369 if (!l_ptr->first_out) {
2370 struct sk_buff *buf;
2371
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002372 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002373 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002374 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375 msg_set_size(&tunnel_hdr, INT_H_SIZE);
2376 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2377 tunnel->b_ptr->net_plane);
2378 msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002379 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002380 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002381 warn("Link changeover error, "
2382 "unable to send changeover msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002383 }
2384 return;
2385 }
Allan Stephensf1310722006-06-25 23:51:37 -07002386
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002387 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002388 l_ptr->owner->active_links[1]);
2389
Per Lidenb97bf3f2006-01-02 19:04:38 +01002390 while (crs) {
2391 struct tipc_msg *msg = buf_msg(crs);
2392
2393 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002394 struct tipc_msg *m = msg_get_wrapped(msg);
2395 unchar* pos = (unchar*)m;
2396
Florian Westphald788d802007-08-02 19:28:06 -07002397 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002398 while (msgcount--) {
2399 msg_set_seqno(m,msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002400 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2401 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002402 pos += align(msg_size(m));
2403 m = (struct tipc_msg *)pos;
2404 }
2405 } else {
Per Liden4323add2006-01-18 00:38:21 +01002406 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2407 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002408 }
2409 crs = crs->next;
2410 }
2411}
2412
Per Liden4323add2006-01-18 00:38:21 +01002413void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414{
2415 struct sk_buff *iter;
2416 struct tipc_msg tunnel_hdr;
2417
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002418 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002419 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002420 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2421 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2422 iter = l_ptr->first_out;
2423 while (iter) {
2424 struct sk_buff *outbuf;
2425 struct tipc_msg *msg = buf_msg(iter);
2426 u32 length = msg_size(msg);
2427
2428 if (msg_user(msg) == MSG_BUNDLER)
2429 msg_set_type(msg, CLOSED_MSG);
2430 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002431 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002432 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002433 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002434 if (outbuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002435 warn("Link changeover error, "
2436 "unable to send duplicate msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 return;
2438 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002439 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2440 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2441 length);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002442 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2443 tunnel->b_ptr->net_plane);
2444 msg_dbg(buf_msg(outbuf), ">SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002445 tipc_link_send_buf(tunnel, outbuf);
2446 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002447 return;
2448 iter = iter->next;
2449 }
2450}
2451
2452
2453
2454/**
2455 * buf_extract - extracts embedded TIPC message from another message
2456 * @skb: encapsulating message buffer
2457 * @from_pos: offset to extract from
2458 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002459 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002460 * encapsulating message itself is left unchanged.
2461 */
2462
2463static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2464{
2465 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2466 u32 size = msg_size(msg);
2467 struct sk_buff *eb;
2468
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002469 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002470 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002471 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002472 return eb;
2473}
2474
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002475/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002476 * link_recv_changeover_msg(): Receive tunneled packet sent
2477 * via other link. Node is locked. Return extracted buffer.
2478 */
2479
2480static int link_recv_changeover_msg(struct link **l_ptr,
2481 struct sk_buff **buf)
2482{
2483 struct sk_buff *tunnel_buf = *buf;
2484 struct link *dest_link;
2485 struct tipc_msg *msg;
2486 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2487 u32 msg_typ = msg_type(tunnel_msg);
2488 u32 msg_count = msg_msgcnt(tunnel_msg);
2489
2490 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002491 if (!dest_link) {
2492 msg_dbg(tunnel_msg, "NOLINK/<REC<");
2493 goto exit;
2494 }
Allan Stephensf1310722006-06-25 23:51:37 -07002495 if (dest_link == *l_ptr) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002496 err("Unexpected changeover message on link <%s>\n",
Allan Stephensf1310722006-06-25 23:51:37 -07002497 (*l_ptr)->name);
2498 goto exit;
2499 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002500 dbg("%c<-%c:", dest_link->b_ptr->net_plane,
2501 (*l_ptr)->b_ptr->net_plane);
2502 *l_ptr = dest_link;
2503 msg = msg_get_wrapped(tunnel_msg);
2504
2505 if (msg_typ == DUPLICATE_MSG) {
2506 if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
2507 msg_dbg(tunnel_msg, "DROP/<REC<");
2508 goto exit;
2509 }
2510 *buf = buf_extract(tunnel_buf,INT_H_SIZE);
2511 if (*buf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002512 warn("Link changeover error, duplicate msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002513 goto exit;
2514 }
2515 msg_dbg(tunnel_msg, "TNL<REC<");
2516 buf_discard(tunnel_buf);
2517 return 1;
2518 }
2519
2520 /* First original message ?: */
2521
Per Liden4323add2006-01-18 00:38:21 +01002522 if (tipc_link_is_up(dest_link)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002523 msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
Allan Stephensa10bd922006-06-25 23:52:17 -07002524 info("Resetting link <%s>, changeover initiated by peer\n",
2525 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002526 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002527 dest_link->exp_msg_count = msg_count;
Allan Stephens5392d642006-06-25 23:52:50 -07002528 dbg("Expecting %u tunnelled messages\n", msg_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002529 if (!msg_count)
2530 goto exit;
2531 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
2532 msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
2533 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 }
2538
2539 /* Receive original message */
2540
2541 if (dest_link->exp_msg_count == 0) {
Allan Stephens5392d642006-06-25 23:52:50 -07002542 warn("Link switchover error, "
2543 "got too many tunnelled messages\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002544 msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
2545 dbg_print_link(dest_link, "LINK:");
2546 goto exit;
2547 }
2548 dest_link->exp_msg_count--;
2549 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
2550 msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
2551 goto exit;
2552 } else {
2553 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2554 if (*buf != NULL) {
2555 msg_dbg(tunnel_msg, "TNL<REC<");
2556 buf_discard(tunnel_buf);
2557 return 1;
2558 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002559 warn("Link changeover error, original msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002560 }
2561 }
2562exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002563 *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002564 buf_discard(tunnel_buf);
2565 return 0;
2566}
2567
2568/*
2569 * Bundler functionality:
2570 */
Per Liden4323add2006-01-18 00:38:21 +01002571void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002572{
2573 u32 msgcount = msg_msgcnt(buf_msg(buf));
2574 u32 pos = INT_H_SIZE;
2575 struct sk_buff *obuf;
2576
2577 msg_dbg(buf_msg(buf), "<BNDL<: ");
2578 while (msgcount--) {
2579 obuf = buf_extract(buf, pos);
2580 if (obuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002581 warn("Link unable to unbundle message(s)\n");
2582 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002583 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002584 pos += align(msg_size(buf_msg(obuf)));
2585 msg_dbg(buf_msg(obuf), " /");
Per Liden4323add2006-01-18 00:38:21 +01002586 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002587 }
2588 buf_discard(buf);
2589}
2590
2591/*
2592 * Fragmentation/defragmentation:
2593 */
2594
2595
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002596/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002597 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002598 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002599 * Returns user data length.
2600 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002601static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002602{
2603 struct tipc_msg *inmsg = buf_msg(buf);
2604 struct tipc_msg fragm_hdr;
2605 u32 insize = msg_size(inmsg);
2606 u32 dsz = msg_data_sz(inmsg);
2607 unchar *crs = buf->data;
2608 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002609 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002610 u32 fragm_sz = pack_sz - INT_H_SIZE;
2611 u32 fragm_no = 1;
Allan Stephens9c396a72008-06-04 17:36:58 -07002612 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002613
2614 if (msg_short(inmsg))
2615 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002616 else
2617 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618
2619 if (msg_routed(inmsg))
2620 msg_set_prevnode(inmsg, tipc_own_addr);
2621
2622 /* Prepare reusable fragment header: */
2623
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002624 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002625 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002626 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2627 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2628 msg_set_fragm_no(&fragm_hdr, fragm_no);
2629 l_ptr->stats.sent_fragmented++;
2630
2631 /* Chop up message: */
2632
2633 while (rest > 0) {
2634 struct sk_buff *fragm;
2635
2636 if (rest <= fragm_sz) {
2637 fragm_sz = rest;
2638 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2639 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002640 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002641 if (fragm == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002642 warn("Link unable to fragment message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002643 dsz = -ENOMEM;
2644 goto exit;
2645 }
2646 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002647 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2648 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2649 fragm_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002650 /* Send queued messages first, if any: */
2651
2652 l_ptr->stats.sent_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01002653 tipc_link_send_buf(l_ptr, fragm);
2654 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002655 return dsz;
2656 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2657 rest -= fragm_sz;
2658 crs += fragm_sz;
2659 msg_set_type(&fragm_hdr, FRAGMENT);
2660 }
2661exit:
2662 buf_discard(buf);
2663 return dsz;
2664}
2665
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002666/*
2667 * A pending message being re-assembled must store certain values
2668 * to handle subsequent fragments correctly. The following functions
Per Lidenb97bf3f2006-01-02 19:04:38 +01002669 * help storing these values in unused, available fields in the
2670 * pending message. This makes dynamic memory allocation unecessary.
2671 */
2672
Sam Ravnborg05790c62006-03-20 22:37:04 -08002673static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002674{
2675 msg_set_seqno(buf_msg(buf), seqno);
2676}
2677
Sam Ravnborg05790c62006-03-20 22:37:04 -08002678static u32 get_fragm_size(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002679{
2680 return msg_ack(buf_msg(buf));
2681}
2682
Sam Ravnborg05790c62006-03-20 22:37:04 -08002683static void set_fragm_size(struct sk_buff *buf, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002684{
2685 msg_set_ack(buf_msg(buf), sz);
2686}
2687
Sam Ravnborg05790c62006-03-20 22:37:04 -08002688static u32 get_expected_frags(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002689{
2690 return msg_bcast_ack(buf_msg(buf));
2691}
2692
Sam Ravnborg05790c62006-03-20 22:37:04 -08002693static void set_expected_frags(struct sk_buff *buf, u32 exp)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002694{
2695 msg_set_bcast_ack(buf_msg(buf), exp);
2696}
2697
Sam Ravnborg05790c62006-03-20 22:37:04 -08002698static u32 get_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002699{
2700 return msg_reroute_cnt(buf_msg(buf));
2701}
2702
Sam Ravnborg05790c62006-03-20 22:37:04 -08002703static void incr_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002704{
2705 msg_incr_reroute_cnt(buf_msg(buf));
2706}
2707
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002708/*
2709 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710 * the reassembled buffer if message is complete.
2711 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002712int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
Per Liden4323add2006-01-18 00:38:21 +01002713 struct tipc_msg **m)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002714{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002715 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002716 struct sk_buff *fbuf = *fb;
2717 struct tipc_msg *fragm = buf_msg(fbuf);
2718 struct sk_buff *pbuf = *pending;
2719 u32 long_msg_seq_no = msg_long_msgno(fragm);
2720
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002721 *fb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002722 msg_dbg(fragm,"FRG<REC<");
2723
2724 /* Is there an incomplete message waiting for this fragment? */
2725
Joe Perchesf64f9e72009-11-29 16:55:45 -08002726 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
2727 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002728 prev = pbuf;
2729 pbuf = pbuf->next;
2730 }
2731
2732 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2733 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2734 u32 msg_sz = msg_size(imsg);
2735 u32 fragm_sz = msg_data_sz(fragm);
2736 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2737 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2738 if (msg_type(imsg) == TIPC_MCAST_MSG)
2739 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2740 if (msg_size(imsg) > max) {
2741 msg_dbg(fragm,"<REC<Oversized: ");
2742 buf_discard(fbuf);
2743 return 0;
2744 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002745 pbuf = tipc_buf_acquire(msg_size(imsg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002746 if (pbuf != NULL) {
2747 pbuf->next = *pending;
2748 *pending = pbuf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002749 skb_copy_to_linear_data(pbuf, imsg,
2750 msg_data_sz(fragm));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002751 /* Prepare buffer for subsequent fragments. */
2752
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002753 set_long_msg_seqno(pbuf, long_msg_seq_no);
2754 set_fragm_size(pbuf,fragm_sz);
2755 set_expected_frags(pbuf,exp_fragm_cnt - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002756 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002757 warn("Link unable to reassemble fragmented message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002758 }
2759 buf_discard(fbuf);
2760 return 0;
2761 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2762 u32 dsz = msg_data_sz(fragm);
2763 u32 fsz = get_fragm_size(pbuf);
2764 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2765 u32 exp_frags = get_expected_frags(pbuf) - 1;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002766 skb_copy_to_linear_data_offset(pbuf, crs,
2767 msg_data(fragm), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002768 buf_discard(fbuf);
2769
2770 /* Is message complete? */
2771
2772 if (exp_frags == 0) {
2773 if (prev)
2774 prev->next = pbuf->next;
2775 else
2776 *pending = pbuf->next;
2777 msg_reset_reroute_cnt(buf_msg(pbuf));
2778 *fb = pbuf;
2779 *m = buf_msg(pbuf);
2780 return 1;
2781 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002782 set_expected_frags(pbuf,exp_frags);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002783 return 0;
2784 }
2785 dbg(" Discarding orphan fragment %x\n",fbuf);
2786 msg_dbg(fragm,"ORPHAN:");
2787 dbg("Pending long buffers:\n");
2788 dbg_print_buf_chain(*pending);
2789 buf_discard(fbuf);
2790 return 0;
2791}
2792
2793/**
2794 * link_check_defragm_bufs - flush stale incoming message fragments
2795 * @l_ptr: pointer to link
2796 */
2797
2798static void link_check_defragm_bufs(struct link *l_ptr)
2799{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002800 struct sk_buff *prev = NULL;
2801 struct sk_buff *next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002802 struct sk_buff *buf = l_ptr->defragm_buf;
2803
2804 if (!buf)
2805 return;
2806 if (!link_working_working(l_ptr))
2807 return;
2808 while (buf) {
2809 u32 cnt = get_timer_cnt(buf);
2810
2811 next = buf->next;
2812 if (cnt < 4) {
2813 incr_timer_cnt(buf);
2814 prev = buf;
2815 } else {
2816 dbg(" Discarding incomplete long buffer\n");
2817 msg_dbg(buf_msg(buf), "LONG:");
2818 dbg_print_link(l_ptr, "curr:");
2819 dbg("Pending long buffers:\n");
2820 dbg_print_buf_chain(l_ptr->defragm_buf);
2821 if (prev)
2822 prev->next = buf->next;
2823 else
2824 l_ptr->defragm_buf = buf->next;
2825 buf_discard(buf);
2826 }
2827 buf = next;
2828 }
2829}
2830
2831
2832
2833static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2834{
2835 l_ptr->tolerance = tolerance;
2836 l_ptr->continuity_interval =
2837 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2838 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2839}
2840
2841
Per Liden4323add2006-01-18 00:38:21 +01002842void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002843{
2844 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002845 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2846 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2847 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2848 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002849 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002850 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2851 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2852 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2853 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002854 l_ptr->queue_limit[CONN_MANAGER] = 1200;
2855 l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
2856 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2857 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2858 /* FRAGMENT and LAST_FRAGMENT packets */
2859 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2860}
2861
2862/**
2863 * link_find_link - locate link by name
2864 * @name - ptr to link name string
2865 * @node - ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002866 *
Per Liden4323add2006-01-18 00:38:21 +01002867 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002868 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002869 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002870 * Returns pointer to link (or 0 if invalid link name).
2871 */
2872
David S. Miller6c000552008-09-02 23:38:32 -07002873static struct link *link_find_link(const char *name, struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002874{
2875 struct link_name link_name_parts;
2876 struct bearer *b_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002877 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002878
2879 if (!link_name_validate(name, &link_name_parts))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002880 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002881
Per Liden4323add2006-01-18 00:38:21 +01002882 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002883 if (!b_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002884 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002885
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002886 *node = tipc_node_find(link_name_parts.addr_peer);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002887 if (!*node)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002888 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002889
2890 l_ptr = (*node)->links[b_ptr->identity];
2891 if (!l_ptr || strcmp(l_ptr->name, name))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002892 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002893
2894 return l_ptr;
2895}
2896
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002897struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002898 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002899{
2900 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002901 u32 new_value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002902 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002903 struct tipc_node *node;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002904 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002905
2906 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002907 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002908
2909 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2910 new_value = ntohl(args->value);
2911
Per Liden4323add2006-01-18 00:38:21 +01002912 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002913 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002914 (tipc_bclink_set_queue_limits(new_value) == 0))
2915 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002916 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002917 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002918 }
2919
Per Liden4323add2006-01-18 00:38:21 +01002920 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002921 l_ptr = link_find_link(args->name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002922 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002923 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002924 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002925 }
2926
Per Liden4323add2006-01-18 00:38:21 +01002927 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002928 res = -EINVAL;
2929 switch (cmd) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002930 case TIPC_CMD_SET_LINK_TOL:
2931 if ((new_value >= TIPC_MIN_LINK_TOL) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002932 (new_value <= TIPC_MAX_LINK_TOL)) {
2933 link_set_supervision_props(l_ptr, new_value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002934 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002935 0, 0, new_value, 0, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002936 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002937 }
2938 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002939 case TIPC_CMD_SET_LINK_PRI:
Per Liden16cb4b32006-01-13 22:22:22 +01002940 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2941 (new_value <= TIPC_MAX_LINK_PRI)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002942 l_ptr->priority = new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002943 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002944 0, 0, 0, new_value, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002945 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002946 }
2947 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002948 case TIPC_CMD_SET_LINK_WINDOW:
2949 if ((new_value >= TIPC_MIN_LINK_WIN) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002950 (new_value <= TIPC_MAX_LINK_WIN)) {
Per Liden4323add2006-01-18 00:38:21 +01002951 tipc_link_set_queue_limits(l_ptr, new_value);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002952 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002953 }
2954 break;
2955 }
Per Liden4323add2006-01-18 00:38:21 +01002956 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002957
Per Liden4323add2006-01-18 00:38:21 +01002958 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002959 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002960 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002961
Per Liden4323add2006-01-18 00:38:21 +01002962 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002963}
2964
2965/**
2966 * link_reset_statistics - reset link statistics
2967 * @l_ptr: pointer to link
2968 */
2969
2970static void link_reset_statistics(struct link *l_ptr)
2971{
2972 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2973 l_ptr->stats.sent_info = l_ptr->next_out_no;
2974 l_ptr->stats.recv_info = l_ptr->next_in_no;
2975}
2976
Per Liden4323add2006-01-18 00:38:21 +01002977struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002978{
2979 char *link_name;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002980 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002981 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002982
2983 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002984 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002985
2986 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002987 if (!strcmp(link_name, tipc_bclink_name)) {
2988 if (tipc_bclink_reset_stats())
2989 return tipc_cfg_reply_error_string("link not found");
2990 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002991 }
2992
Per Liden4323add2006-01-18 00:38:21 +01002993 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002994 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002995 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002996 read_unlock_bh(&tipc_net_lock);
2997 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002998 }
2999
Per Liden4323add2006-01-18 00:38:21 +01003000 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003001 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01003002 tipc_node_unlock(node);
3003 read_unlock_bh(&tipc_net_lock);
3004 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01003005}
3006
3007/**
3008 * percent - convert count to a percentage of total (rounding up or down)
3009 */
3010
3011static u32 percent(u32 count, u32 total)
3012{
3013 return (count * 100 + (total / 2)) / total;
3014}
3015
3016/**
Per Liden4323add2006-01-18 00:38:21 +01003017 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01003018 * @name: link name
3019 * @buf: print buffer area
3020 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003021 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003022 * Returns length of print buffer data string (or 0 if error)
3023 */
3024
Per Liden4323add2006-01-18 00:38:21 +01003025static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003026{
3027 struct print_buf pb;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003028 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07003029 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003030 char *status;
3031 u32 profile_total = 0;
3032
Per Liden4323add2006-01-18 00:38:21 +01003033 if (!strcmp(name, tipc_bclink_name))
3034 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003035
Per Liden4323add2006-01-18 00:38:21 +01003036 tipc_printbuf_init(&pb, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003037
Per Liden4323add2006-01-18 00:38:21 +01003038 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003039 l_ptr = link_find_link(name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003040 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003041 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003042 return 0;
3043 }
Per Liden4323add2006-01-18 00:38:21 +01003044 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003045
Per Liden4323add2006-01-18 00:38:21 +01003046 if (tipc_link_is_active(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003047 status = "ACTIVE";
Per Liden4323add2006-01-18 00:38:21 +01003048 else if (tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003049 status = "STANDBY";
3050 else
3051 status = "DEFUNCT";
3052 tipc_printf(&pb, "Link <%s>\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003053 " %s MTU:%u Priority:%u Tolerance:%u ms"
3054 " Window:%u packets\n",
Allan Stephens15e979d2010-05-11 14:30:10 +00003055 l_ptr->name, status, l_ptr->max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003056 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003057 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003058 l_ptr->next_in_no - l_ptr->stats.recv_info,
3059 l_ptr->stats.recv_fragments,
3060 l_ptr->stats.recv_fragmented,
3061 l_ptr->stats.recv_bundles,
3062 l_ptr->stats.recv_bundled);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003063 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003064 l_ptr->next_out_no - l_ptr->stats.sent_info,
3065 l_ptr->stats.sent_fragments,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003066 l_ptr->stats.sent_fragmented,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003067 l_ptr->stats.sent_bundles,
3068 l_ptr->stats.sent_bundled);
3069 profile_total = l_ptr->stats.msg_length_counts;
3070 if (!profile_total)
3071 profile_total = 1;
3072 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003073 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
3074 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003075 l_ptr->stats.msg_length_counts,
3076 l_ptr->stats.msg_lengths_total / profile_total,
3077 percent(l_ptr->stats.msg_length_profile[0], profile_total),
3078 percent(l_ptr->stats.msg_length_profile[1], profile_total),
3079 percent(l_ptr->stats.msg_length_profile[2], profile_total),
3080 percent(l_ptr->stats.msg_length_profile[3], profile_total),
3081 percent(l_ptr->stats.msg_length_profile[4], profile_total),
3082 percent(l_ptr->stats.msg_length_profile[5], profile_total),
3083 percent(l_ptr->stats.msg_length_profile[6], profile_total));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003084 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01003085 l_ptr->stats.recv_states,
3086 l_ptr->stats.recv_probes,
3087 l_ptr->stats.recv_nacks,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003088 l_ptr->stats.deferred_recv,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003089 l_ptr->stats.duplicates);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003090 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
3091 l_ptr->stats.sent_states,
3092 l_ptr->stats.sent_probes,
3093 l_ptr->stats.sent_nacks,
3094 l_ptr->stats.sent_acks,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003095 l_ptr->stats.retransmitted);
3096 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
3097 l_ptr->stats.bearer_congs,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003098 l_ptr->stats.link_congs,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003099 l_ptr->stats.max_queue_sz,
3100 l_ptr->stats.queue_sz_counts
3101 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
3102 : 0);
3103
Per Liden4323add2006-01-18 00:38:21 +01003104 tipc_node_unlock(node);
3105 read_unlock_bh(&tipc_net_lock);
3106 return tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003107}
3108
3109#define MAX_LINK_STATS_INFO 2000
3110
Per Liden4323add2006-01-18 00:38:21 +01003111struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003112{
3113 struct sk_buff *buf;
3114 struct tlv_desc *rep_tlv;
3115 int str_len;
3116
3117 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01003118 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003119
Per Liden4323add2006-01-18 00:38:21 +01003120 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
Per Lidenb97bf3f2006-01-02 19:04:38 +01003121 if (!buf)
3122 return NULL;
3123
3124 rep_tlv = (struct tlv_desc *)buf->data;
3125
Per Liden4323add2006-01-18 00:38:21 +01003126 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
3127 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003128 if (!str_len) {
3129 buf_discard(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003130 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003131 }
3132
3133 skb_put(buf, TLV_SPACE(str_len));
3134 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
3135
3136 return buf;
3137}
3138
Per Lidenb97bf3f2006-01-02 19:04:38 +01003139/**
Per Liden4323add2006-01-18 00:38:21 +01003140 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01003141 * @dest: network address of destination node
3142 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003143 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003144 * If no active link can be found, uses default maximum packet size.
3145 */
3146
Per Liden4323add2006-01-18 00:38:21 +01003147u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003148{
David S. Miller6c000552008-09-02 23:38:32 -07003149 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003150 struct link *l_ptr;
3151 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003152
Per Lidenb97bf3f2006-01-02 19:04:38 +01003153 if (dest == tipc_own_addr)
3154 return MAX_MSG_SIZE;
3155
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003156 read_lock_bh(&tipc_net_lock);
Per Liden4323add2006-01-18 00:38:21 +01003157 n_ptr = tipc_node_select(dest, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003158 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003159 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003160 l_ptr = n_ptr->active_links[selector & 1];
3161 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00003162 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01003163 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003164 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003165 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003166 return res;
3167}
3168
Per Lidenb97bf3f2006-01-02 19:04:38 +01003169static void link_dump_send_queue(struct link *l_ptr)
3170{
3171 if (l_ptr->next_out) {
3172 info("\nContents of unsent queue:\n");
3173 dbg_print_buf_chain(l_ptr->next_out);
3174 }
3175 info("\nContents of send queue:\n");
3176 if (l_ptr->first_out) {
3177 dbg_print_buf_chain(l_ptr->first_out);
3178 }
3179 info("Empty send queue\n");
3180}
3181
3182static void link_print(struct link *l_ptr, struct print_buf *buf,
3183 const char *str)
3184{
3185 tipc_printf(buf, str);
3186 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
3187 return;
3188 tipc_printf(buf, "Link %x<%s>:",
3189 l_ptr->addr, l_ptr->b_ptr->publ.name);
3190 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
3191 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
3192 tipc_printf(buf, "SQUE");
3193 if (l_ptr->first_out) {
3194 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
3195 if (l_ptr->next_out)
3196 tipc_printf(buf, "%u..",
3197 msg_seqno(buf_msg(l_ptr->next_out)));
Allan Stephensb82834e2010-05-11 14:30:04 +00003198 tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003199 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
3200 msg_seqno(buf_msg(l_ptr->first_out)))
Joe Perchesf64f9e72009-11-29 16:55:45 -08003201 != (l_ptr->out_queue_size - 1)) ||
3202 (l_ptr->last_out->next != NULL)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01003203 tipc_printf(buf, "\nSend queue inconsistency\n");
3204 tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
3205 tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
3206 tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
3207 link_dump_send_queue(l_ptr);
3208 }
3209 } else
3210 tipc_printf(buf, "[]");
3211 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
3212 if (l_ptr->oldest_deferred_in) {
3213 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
3214 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
3215 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
3216 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
3217 tipc_printf(buf, ":RQSIZ(%u)",
3218 l_ptr->deferred_inqueue_sz);
3219 }
3220 }
3221 if (link_working_unknown(l_ptr))
3222 tipc_printf(buf, ":WU");
3223 if (link_reset_reset(l_ptr))
3224 tipc_printf(buf, ":RR");
3225 if (link_reset_unknown(l_ptr))
3226 tipc_printf(buf, ":RU");
3227 if (link_working_working(l_ptr))
3228 tipc_printf(buf, ":WW");
3229 tipc_printf(buf, "\n");
3230}
3231