blob: cb10d20caef32398c31a39b6b0d4fc9f59953c90 [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
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800157 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 */
159
Sam Ravnborg05790c62006-03-20 22:37:04 -0800160static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161{
162 return (i + 3) & ~3u;
163}
164
Sam Ravnborg05790c62006-03-20 22:37:04 -0800165static void link_init_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166{
167 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900168
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
170 if (max_pkt > MAX_MSG_SIZE)
171 max_pkt = MAX_MSG_SIZE;
172
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900173 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
175 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900176 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 l_ptr->max_pkt = MAX_PKT_DEFAULT;
178
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900179 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180}
181
Sam Ravnborg05790c62006-03-20 22:37:04 -0800182static u32 link_next_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183{
184 if (l_ptr->next_out)
185 return msg_seqno(buf_msg(l_ptr->next_out));
186 return mod(l_ptr->next_out_no);
187}
188
Sam Ravnborg05790c62006-03-20 22:37:04 -0800189static u32 link_last_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190{
191 return mod(link_next_sent(l_ptr) - 1);
192}
193
194/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800195 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 */
197
Per Liden4323add2006-01-18 00:38:21 +0100198int tipc_link_is_up(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199{
200 if (!l_ptr)
201 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000202 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203}
204
Per Liden4323add2006-01-18 00:38:21 +0100205int tipc_link_is_active(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000207 return (l_ptr->owner->active_links[0] == l_ptr) ||
208 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209}
210
211/**
212 * link_name_validate - validate & (optionally) deconstruct link name
213 * @name - ptr to link name string
214 * @name_parts - ptr to area for link name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900215 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 * Returns 1 if link name is valid, otherwise 0.
217 */
218
219static int link_name_validate(const char *name, struct link_name *name_parts)
220{
221 char name_copy[TIPC_MAX_LINK_NAME];
222 char *addr_local;
223 char *if_local;
224 char *addr_peer;
225 char *if_peer;
226 char dummy;
227 u32 z_local, c_local, n_local;
228 u32 z_peer, c_peer, n_peer;
229 u32 if_local_len;
230 u32 if_peer_len;
231
232 /* copy link name & ensure length is OK */
233
234 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
235 /* need above in case non-Posix strncpy() doesn't pad with nulls */
236 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
237 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
238 return 0;
239
240 /* ensure all component parts of link name are present */
241
242 addr_local = name_copy;
243 if ((if_local = strchr(addr_local, ':')) == NULL)
244 return 0;
245 *(if_local++) = 0;
246 if ((addr_peer = strchr(if_local, '-')) == NULL)
247 return 0;
248 *(addr_peer++) = 0;
249 if_local_len = addr_peer - if_local;
250 if ((if_peer = strchr(addr_peer, ':')) == NULL)
251 return 0;
252 *(if_peer++) = 0;
253 if_peer_len = strlen(if_peer) + 1;
254
255 /* validate component parts of link name */
256
257 if ((sscanf(addr_local, "%u.%u.%u%c",
258 &z_local, &c_local, &n_local, &dummy) != 3) ||
259 (sscanf(addr_peer, "%u.%u.%u%c",
260 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
261 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
262 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900263 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
264 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
266 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
267 return 0;
268
269 /* return link name components, if necessary */
270
271 if (name_parts) {
272 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
273 strcpy(name_parts->if_local, if_local);
274 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
275 strcpy(name_parts->if_peer, if_peer);
276 }
277 return 1;
278}
279
280/**
281 * link_timeout - handle expiration of link timer
282 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900283 *
Per Liden4323add2006-01-18 00:38:21 +0100284 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
285 * with tipc_link_delete(). (There is no risk that the node will be deleted by
286 * another thread because tipc_link_delete() always cancels the link timer before
287 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288 */
289
290static void link_timeout(struct link *l_ptr)
291{
Per Liden4323add2006-01-18 00:38:21 +0100292 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293
294 /* update counters used in statistical profiling of send traffic */
295
296 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
297 l_ptr->stats.queue_sz_counts++;
298
299 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
300 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
301
302 if (l_ptr->first_out) {
303 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
304 u32 length = msg_size(msg);
305
Joe Perchesf64f9e72009-11-29 16:55:45 -0800306 if ((msg_user(msg) == MSG_FRAGMENTER) &&
307 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 length = msg_size(msg_get_wrapped(msg));
309 }
310 if (length) {
311 l_ptr->stats.msg_lengths_total += length;
312 l_ptr->stats.msg_length_counts++;
313 if (length <= 64)
314 l_ptr->stats.msg_length_profile[0]++;
315 else if (length <= 256)
316 l_ptr->stats.msg_length_profile[1]++;
317 else if (length <= 1024)
318 l_ptr->stats.msg_length_profile[2]++;
319 else if (length <= 4096)
320 l_ptr->stats.msg_length_profile[3]++;
321 else if (length <= 16384)
322 l_ptr->stats.msg_length_profile[4]++;
323 else if (length <= 32768)
324 l_ptr->stats.msg_length_profile[5]++;
325 else
326 l_ptr->stats.msg_length_profile[6]++;
327 }
328 }
329
330 /* do all other link processing performed on a periodic basis */
331
332 link_check_defragm_bufs(l_ptr);
333
334 link_state_event(l_ptr, TIMEOUT_EVT);
335
336 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100337 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338
Per Liden4323add2006-01-18 00:38:21 +0100339 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340}
341
Sam Ravnborg05790c62006-03-20 22:37:04 -0800342static void link_set_timer(struct link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343{
344 k_start_timer(&l_ptr->timer, time);
345}
346
347/**
Per Liden4323add2006-01-18 00:38:21 +0100348 * tipc_link_create - create a new link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 * @b_ptr: pointer to associated bearer
350 * @peer: network address of node at other end of link
351 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900352 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353 * Returns pointer to link.
354 */
355
Per Liden4323add2006-01-18 00:38:21 +0100356struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
357 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358{
359 struct link *l_ptr;
360 struct tipc_msg *msg;
361 char *if_name;
362
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700363 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 if (!l_ptr) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700365 warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 return NULL;
367 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368
Florian Westphal945710652007-07-26 00:05:07 -0700369 if (LINK_LOG_BUF_SIZE) {
370 char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
371
372 if (!pb) {
373 kfree(l_ptr);
374 warn("Link creation failed, no memory for print buffer\n");
375 return NULL;
376 }
377 tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
378 }
379
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 l_ptr->addr = peer;
381 if_name = strchr(b_ptr->publ.name, ':') + 1;
382 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
383 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900384 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 if_name,
386 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
387 /* note: peer i/f is appended to link name by reset/activate */
388 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 l_ptr->checkpoint = 1;
390 l_ptr->b_ptr = b_ptr;
391 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
392 l_ptr->state = RESET_UNKNOWN;
393
394 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
395 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000396 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700398 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 msg_set_bearer_id(msg, b_ptr->identity);
400 strcpy((char *)msg_data(msg), if_name);
401
402 l_ptr->priority = b_ptr->priority;
Per Liden4323add2006-01-18 00:38:21 +0100403 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404
405 link_init_max_pkt(l_ptr);
406
407 l_ptr->next_out_no = 1;
408 INIT_LIST_HEAD(&l_ptr->waiting_ports);
409
410 link_reset_statistics(l_ptr);
411
Per Liden4323add2006-01-18 00:38:21 +0100412 l_ptr->owner = tipc_node_attach_link(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 if (!l_ptr->owner) {
Florian Westphal945710652007-07-26 00:05:07 -0700414 if (LINK_LOG_BUF_SIZE)
415 kfree(l_ptr->print_buf.buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 kfree(l_ptr);
417 return NULL;
418 }
419
Florian Westphal945710652007-07-26 00:05:07 -0700420 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
421 list_add_tail(&l_ptr->link_list, &b_ptr->links);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000422 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 return l_ptr;
425}
426
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900427/**
Per Liden4323add2006-01-18 00:38:21 +0100428 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430 *
Per Liden4323add2006-01-18 00:38:21 +0100431 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900433 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 */
435
Per Liden4323add2006-01-18 00:38:21 +0100436void tipc_link_delete(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437{
438 if (!l_ptr) {
439 err("Attempt to delete non-existent link\n");
440 return;
441 }
442
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900444
Per Liden4323add2006-01-18 00:38:21 +0100445 tipc_node_lock(l_ptr->owner);
446 tipc_link_reset(l_ptr);
447 tipc_node_detach_link(l_ptr->owner, l_ptr);
448 tipc_link_stop(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 list_del_init(&l_ptr->link_list);
450 if (LINK_LOG_BUF_SIZE)
451 kfree(l_ptr->print_buf.buf);
Per Liden4323add2006-01-18 00:38:21 +0100452 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 k_term_timer(&l_ptr->timer);
454 kfree(l_ptr);
455}
456
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000457static void link_start(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 link_state_event(l_ptr, STARTING_EVT);
460}
461
462/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * @l_ptr: pointer to link
465 * @origport: reference to sending port
466 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900467 *
468 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 * has abated.
470 */
471
472static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
473{
474 struct port *p_ptr;
475
Per Liden4323add2006-01-18 00:38:21 +0100476 spin_lock_bh(&tipc_port_list_lock);
477 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478 if (p_ptr) {
479 if (!p_ptr->wakeup)
480 goto exit;
481 if (!list_empty(&p_ptr->wait_list))
482 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 p_ptr->publ.congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000484 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
486 l_ptr->stats.link_congs++;
487exit:
Per Liden4323add2006-01-18 00:38:21 +0100488 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 }
Per Liden4323add2006-01-18 00:38:21 +0100490 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 return -ELINKCONG;
492}
493
Per Liden4323add2006-01-18 00:38:21 +0100494void tipc_link_wakeup_ports(struct link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495{
496 struct port *p_ptr;
497 struct port *temp_p_ptr;
498 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
499
500 if (all)
501 win = 100000;
502 if (win <= 0)
503 return;
Per Liden4323add2006-01-18 00:38:21 +0100504 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 return;
506 if (link_congested(l_ptr))
507 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900508 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 wait_list) {
510 if (win <= 0)
511 break;
512 list_del_init(&p_ptr->wait_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 spin_lock_bh(p_ptr->publ.lock);
514 p_ptr->publ.congested = 0;
515 p_ptr->wakeup(&p_ptr->publ);
516 win -= p_ptr->waiting_pkts;
517 spin_unlock_bh(p_ptr->publ.lock);
518 }
519
520exit:
Per Liden4323add2006-01-18 00:38:21 +0100521 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522}
523
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900524/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 * link_release_outqueue - purge link's outbound message queue
526 * @l_ptr: pointer to link
527 */
528
529static void link_release_outqueue(struct link *l_ptr)
530{
531 struct sk_buff *buf = l_ptr->first_out;
532 struct sk_buff *next;
533
534 while (buf) {
535 next = buf->next;
536 buf_discard(buf);
537 buf = next;
538 }
539 l_ptr->first_out = NULL;
540 l_ptr->out_queue_size = 0;
541}
542
543/**
Per Liden4323add2006-01-18 00:38:21 +0100544 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 * @l_ptr: pointer to link
546 */
547
Per Liden4323add2006-01-18 00:38:21 +0100548void tipc_link_reset_fragments(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549{
550 struct sk_buff *buf = l_ptr->defragm_buf;
551 struct sk_buff *next;
552
553 while (buf) {
554 next = buf->next;
555 buf_discard(buf);
556 buf = next;
557 }
558 l_ptr->defragm_buf = NULL;
559}
560
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561/**
Per Liden4323add2006-01-18 00:38:21 +0100562 * tipc_link_stop - purge all inbound and outbound messages associated with link
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_stop(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567{
568 struct sk_buff *buf;
569 struct sk_buff *next;
570
571 buf = l_ptr->oldest_deferred_in;
572 while (buf) {
573 next = buf->next;
574 buf_discard(buf);
575 buf = next;
576 }
577
578 buf = l_ptr->first_out;
579 while (buf) {
580 next = buf->next;
581 buf_discard(buf);
582 buf = next;
583 }
584
Per Liden4323add2006-01-18 00:38:21 +0100585 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586
587 buf_discard(l_ptr->proto_msg_queue);
588 l_ptr->proto_msg_queue = NULL;
589}
590
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592#define link_send_event(fcn, l_ptr, up) do { } while (0)
593
Per Liden4323add2006-01-18 00:38:21 +0100594void tipc_link_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595{
596 struct sk_buff *buf;
597 u32 prev_state = l_ptr->state;
598 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700599 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900600
Allan Stephensa686e682008-06-04 17:29:39 -0700601 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602
Allan Stephensa686e682008-06-04 17:29:39 -0700603 /* Link is down, accept any session */
604 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 l_ptr->state = RESET_UNKNOWN;
610 dbg_link_state("Resetting Link\n");
611
612 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
613 return;
614
Per Liden4323add2006-01-18 00:38:21 +0100615 tipc_node_link_down(l_ptr->owner, l_ptr);
616 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000617
Allan Stephens5392d642006-06-25 23:52:50 -0700618 if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 l_ptr->owner->permit_changeover) {
620 l_ptr->reset_checkpoint = checkpoint;
621 l_ptr->exp_msg_count = START_CHANGEOVER;
622 }
623
624 /* Clean up all queues: */
625
626 link_release_outqueue(l_ptr);
627 buf_discard(l_ptr->proto_msg_queue);
628 l_ptr->proto_msg_queue = NULL;
629 buf = l_ptr->oldest_deferred_in;
630 while (buf) {
631 struct sk_buff *next = buf->next;
632 buf_discard(buf);
633 buf = next;
634 }
635 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100636 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637
638 l_ptr->retransm_queue_head = 0;
639 l_ptr->retransm_queue_size = 0;
640 l_ptr->last_out = NULL;
641 l_ptr->first_out = NULL;
642 l_ptr->next_out = NULL;
643 l_ptr->unacked_window = 0;
644 l_ptr->checkpoint = 1;
645 l_ptr->next_out_no = 1;
646 l_ptr->deferred_inqueue_sz = 0;
647 l_ptr->oldest_deferred_in = NULL;
648 l_ptr->newest_deferred_in = NULL;
649 l_ptr->fsm_msg_cnt = 0;
650 l_ptr->stale_count = 0;
651 link_reset_statistics(l_ptr);
652
Per Liden4323add2006-01-18 00:38:21 +0100653 link_send_event(tipc_cfg_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100655 link_send_event(tipc_disc_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656}
657
658
659static void link_activate(struct link *l_ptr)
660{
Allan Stephens5392d642006-06-25 23:52:50 -0700661 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100662 tipc_node_link_up(l_ptr->owner, l_ptr);
663 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
664 link_send_event(tipc_cfg_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100666 link_send_event(tipc_disc_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667}
668
669/**
670 * link_state_event - link finite state machine
671 * @l_ptr: pointer to link
672 * @event: state machine event to process
673 */
674
675static void link_state_event(struct link *l_ptr, unsigned event)
676{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 struct link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 u32 cont_intv = l_ptr->continuity_interval;
679
680 if (!l_ptr->started && (event != STARTING_EVT))
681 return; /* Not yet. */
682
683 if (link_blocked(l_ptr)) {
684 if (event == TIMEOUT_EVT) {
685 link_set_timer(l_ptr, cont_intv);
686 }
687 return; /* Changeover going on */
688 }
689 dbg_link("STATE_EV: <%s> ", l_ptr->name);
690
691 switch (l_ptr->state) {
692 case WORKING_WORKING:
693 dbg_link("WW/");
694 switch (event) {
695 case TRAFFIC_MSG_EVT:
696 dbg_link("TRF-");
697 /* fall through */
698 case ACTIVATE_MSG:
699 dbg_link("ACT\n");
700 break;
701 case TIMEOUT_EVT:
702 dbg_link("TIM ");
703 if (l_ptr->next_in_no != l_ptr->checkpoint) {
704 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100705 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900706 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100707 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 l_ptr->fsm_msg_cnt++;
709 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900710 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100711 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 l_ptr->fsm_msg_cnt++;
713 }
714 link_set_timer(l_ptr, cont_intv);
715 break;
716 }
717 dbg_link(" -> WU\n");
718 l_ptr->state = WORKING_UNKNOWN;
719 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100720 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 l_ptr->fsm_msg_cnt++;
722 link_set_timer(l_ptr, cont_intv / 4);
723 break;
724 case RESET_MSG:
725 dbg_link("RES -> RR\n");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900726 info("Resetting link <%s>, requested by peer\n",
Allan Stephensa10bd922006-06-25 23:52:17 -0700727 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100728 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 l_ptr->state = RESET_RESET;
730 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100731 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 l_ptr->fsm_msg_cnt++;
733 link_set_timer(l_ptr, cont_intv);
734 break;
735 default:
736 err("Unknown link event %u in WW state\n", event);
737 }
738 break;
739 case WORKING_UNKNOWN:
740 dbg_link("WU/");
741 switch (event) {
742 case TRAFFIC_MSG_EVT:
743 dbg_link("TRF-");
744 case ACTIVATE_MSG:
745 dbg_link("ACT -> WW\n");
746 l_ptr->state = WORKING_WORKING;
747 l_ptr->fsm_msg_cnt = 0;
748 link_set_timer(l_ptr, cont_intv);
749 break;
750 case RESET_MSG:
751 dbg_link("RES -> RR\n");
Allan Stephensa10bd922006-06-25 23:52:17 -0700752 info("Resetting link <%s>, requested by peer "
753 "while probing\n", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100754 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 l_ptr->state = RESET_RESET;
756 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100757 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 l_ptr->fsm_msg_cnt++;
759 link_set_timer(l_ptr, cont_intv);
760 break;
761 case TIMEOUT_EVT:
762 dbg_link("TIM ");
763 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Frans Popa570f092010-03-24 07:57:29 +0000764 dbg_link("-> WW\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765 l_ptr->state = WORKING_WORKING;
766 l_ptr->fsm_msg_cnt = 0;
767 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100768 if (tipc_bclink_acks_missing(l_ptr->owner)) {
769 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
770 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 l_ptr->fsm_msg_cnt++;
772 }
773 link_set_timer(l_ptr, cont_intv);
774 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
775 dbg_link("Probing %u/%u,timer = %u ms)\n",
776 l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
777 cont_intv / 4);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900778 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100779 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 l_ptr->fsm_msg_cnt++;
781 link_set_timer(l_ptr, cont_intv / 4);
782 } else { /* Link has failed */
783 dbg_link("-> RU (%u probes unanswered)\n",
784 l_ptr->fsm_msg_cnt);
Allan Stephensa10bd922006-06-25 23:52:17 -0700785 warn("Resetting link <%s>, peer not responding\n",
786 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100787 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 l_ptr->state = RESET_UNKNOWN;
789 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100790 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
791 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 l_ptr->fsm_msg_cnt++;
793 link_set_timer(l_ptr, cont_intv);
794 }
795 break;
796 default:
797 err("Unknown link event %u in WU state\n", event);
798 }
799 break;
800 case RESET_UNKNOWN:
801 dbg_link("RU/");
802 switch (event) {
803 case TRAFFIC_MSG_EVT:
804 dbg_link("TRF-\n");
805 break;
806 case ACTIVATE_MSG:
807 other = l_ptr->owner->active_links[0];
808 if (other && link_working_unknown(other)) {
809 dbg_link("ACT\n");
810 break;
811 }
812 dbg_link("ACT -> WW\n");
813 l_ptr->state = WORKING_WORKING;
814 l_ptr->fsm_msg_cnt = 0;
815 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100816 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 l_ptr->fsm_msg_cnt++;
818 link_set_timer(l_ptr, cont_intv);
819 break;
820 case RESET_MSG:
Frans Popa570f092010-03-24 07:57:29 +0000821 dbg_link("RES\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 dbg_link(" -> RR\n");
823 l_ptr->state = RESET_RESET;
824 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100825 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 l_ptr->fsm_msg_cnt++;
827 link_set_timer(l_ptr, cont_intv);
828 break;
829 case STARTING_EVT:
830 dbg_link("START-");
831 l_ptr->started = 1;
832 /* fall through */
833 case TIMEOUT_EVT:
Frans Popa570f092010-03-24 07:57:29 +0000834 dbg_link("TIM\n");
Per Liden4323add2006-01-18 00:38:21 +0100835 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 l_ptr->fsm_msg_cnt++;
837 link_set_timer(l_ptr, cont_intv);
838 break;
839 default:
840 err("Unknown link event %u in RU state\n", event);
841 }
842 break;
843 case RESET_RESET:
844 dbg_link("RR/ ");
845 switch (event) {
846 case TRAFFIC_MSG_EVT:
847 dbg_link("TRF-");
848 /* fall through */
849 case ACTIVATE_MSG:
850 other = l_ptr->owner->active_links[0];
851 if (other && link_working_unknown(other)) {
852 dbg_link("ACT\n");
853 break;
854 }
855 dbg_link("ACT -> WW\n");
856 l_ptr->state = WORKING_WORKING;
857 l_ptr->fsm_msg_cnt = 0;
858 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100859 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860 l_ptr->fsm_msg_cnt++;
861 link_set_timer(l_ptr, cont_intv);
862 break;
863 case RESET_MSG:
864 dbg_link("RES\n");
865 break;
866 case TIMEOUT_EVT:
867 dbg_link("TIM\n");
Per Liden4323add2006-01-18 00:38:21 +0100868 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 l_ptr->fsm_msg_cnt++;
870 link_set_timer(l_ptr, cont_intv);
871 dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
872 break;
873 default:
874 err("Unknown link event %u in RR state\n", event);
875 }
876 break;
877 default:
878 err("Unknown link state %u/%u\n", l_ptr->state, event);
879 }
880}
881
882/*
883 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900884 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 */
886
887static int link_bundle_buf(struct link *l_ptr,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900888 struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 struct sk_buff *buf)
890{
891 struct tipc_msg *bundler_msg = buf_msg(bundler);
892 struct tipc_msg *msg = buf_msg(buf);
893 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700894 u32 bundle_size = msg_size(bundler_msg);
895 u32 to_pos = align(bundle_size);
896 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897
898 if (msg_user(bundler_msg) != MSG_BUNDLER)
899 return 0;
900 if (msg_type(bundler_msg) != OPEN_MSG)
901 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700902 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000904 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700905 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906
Allan Stephense49060c2006-06-29 12:32:46 -0700907 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300908 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 msg_set_size(bundler_msg, to_pos + size);
910 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 buf_discard(buf);
912 l_ptr->stats.sent_bundled++;
913 return 1;
914}
915
Sam Ravnborg05790c62006-03-20 22:37:04 -0800916static void link_add_to_outqueue(struct link *l_ptr,
917 struct sk_buff *buf,
918 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919{
920 u32 ack = mod(l_ptr->next_in_no - 1);
921 u32 seqno = mod(l_ptr->next_out_no++);
922
923 msg_set_word(msg, 2, ((ack << 16) | seqno));
924 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
925 buf->next = NULL;
926 if (l_ptr->first_out) {
927 l_ptr->last_out->next = buf;
928 l_ptr->last_out = buf;
929 } else
930 l_ptr->first_out = l_ptr->last_out = buf;
931 l_ptr->out_queue_size++;
932}
933
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900934/*
935 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 * inside TIPC when the 'fast path' in tipc_send_buf
937 * has failed, and from link_send()
938 */
939
Per Liden4323add2006-01-18 00:38:21 +0100940int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941{
942 struct tipc_msg *msg = buf_msg(buf);
943 u32 size = msg_size(msg);
944 u32 dsz = msg_data_sz(msg);
945 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000946 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000948 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949
950 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
951
952 /* Match msg importance against queue limits: */
953
954 if (unlikely(queue_size >= queue_limit)) {
955 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
956 return link_schedule_port(l_ptr, msg_origport(msg),
957 size);
958 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 buf_discard(buf);
960 if (imp > CONN_MANAGER) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700961 warn("Resetting link <%s>, send queue full", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100962 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 }
964 return dsz;
965 }
966
967 /* Fragmentation needed ? */
968
969 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000970 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971
972 /* Packet can be queued or sent: */
973
974 if (queue_size > l_ptr->stats.max_queue_sz)
975 l_ptr->stats.max_queue_sz = queue_size;
976
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900977 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 !link_congested(l_ptr))) {
979 link_add_to_outqueue(l_ptr, buf, msg);
980
Per Liden4323add2006-01-18 00:38:21 +0100981 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 l_ptr->unacked_window = 0;
983 } else {
Per Liden4323add2006-01-18 00:38:21 +0100984 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 l_ptr->stats.bearer_congs++;
986 l_ptr->next_out = buf;
987 }
988 return dsz;
989 }
990 /* Congestion: can message be bundled ?: */
991
992 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
993 (msg_user(msg) != MSG_FRAGMENTER)) {
994
995 /* Try adding message to an existing bundle */
996
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900997 if (l_ptr->next_out &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
Per Liden4323add2006-01-18 00:38:21 +0100999 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 return dsz;
1001 }
1002
1003 /* Try creating a new bundle */
1004
1005 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001006 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 struct tipc_msg bundler_hdr;
1008
1009 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001010 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -07001011 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001012 skb_copy_to_linear_data(bundler, &bundler_hdr,
1013 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 skb_trim(bundler, INT_H_SIZE);
1015 link_bundle_buf(l_ptr, bundler, buf);
1016 buf = bundler;
1017 msg = buf_msg(buf);
1018 l_ptr->stats.sent_bundles++;
1019 }
1020 }
1021 }
1022 if (!l_ptr->next_out)
1023 l_ptr->next_out = buf;
1024 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001025 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 return dsz;
1027}
1028
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029/*
1030 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 * not been selected yet, and the the owner node is not locked
1032 * Called by TIPC internal users, e.g. the name distributor
1033 */
1034
Per Liden4323add2006-01-18 00:38:21 +01001035int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036{
1037 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001038 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 int res = -ELINKCONG;
1040
Per Liden4323add2006-01-18 00:38:21 +01001041 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001042 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001044 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 l_ptr = n_ptr->active_links[selector & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 if (l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001047 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensc33d53b2006-06-25 23:50:30 -07001048 } else {
Allan Stephensc33d53b2006-06-25 23:50:30 -07001049 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 }
Per Liden4323add2006-01-18 00:38:21 +01001051 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 buf_discard(buf);
1054 }
Per Liden4323add2006-01-18 00:38:21 +01001055 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056 return res;
1057}
1058
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001059/*
1060 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 * destination link is known and the header is complete,
1062 * inclusive total message length. Very time critical.
1063 * Link is locked. Returns user data length.
1064 */
1065
Sam Ravnborg05790c62006-03-20 22:37:04 -08001066static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
1067 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068{
1069 struct tipc_msg *msg = buf_msg(buf);
1070 int res = msg_data_sz(msg);
1071
1072 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +00001073 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
1075 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001076 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
1077 &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 return res;
1080 }
Per Liden4323add2006-01-18 00:38:21 +01001081 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 l_ptr->stats.bearer_congs++;
1083 l_ptr->next_out = buf;
1084 return res;
1085 }
1086 }
1087 else
Allan Stephens15e979d2010-05-11 14:30:10 +00001088 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 }
Per Liden4323add2006-01-18 00:38:21 +01001090 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091}
1092
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001093/*
1094 * tipc_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 * destination node is known and the header is complete,
1096 * inclusive total message length.
1097 * Returns user data length.
1098 */
1099int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
1100{
1101 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001102 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 int res;
1104 u32 selector = msg_origport(buf_msg(buf)) & 1;
1105 u32 dummy;
1106
1107 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001108 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109
Per Liden4323add2006-01-18 00:38:21 +01001110 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001111 n_ptr = tipc_node_find(destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 if (likely(n_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001113 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 l_ptr = n_ptr->active_links[selector];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 if (likely(l_ptr)) {
1116 res = link_send_buf_fast(l_ptr, buf, &dummy);
Per Liden4323add2006-01-18 00:38:21 +01001117 tipc_node_unlock(n_ptr);
1118 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 return res;
1120 }
Per Liden4323add2006-01-18 00:38:21 +01001121 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 }
Per Liden4323add2006-01-18 00:38:21 +01001123 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 res = msg_data_sz(buf_msg(buf));
1125 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1126 return res;
1127}
1128
1129
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001130/*
1131 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001133 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 * Returns user data length or errno.
1135 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001136int tipc_link_send_sections_fast(struct port *sender,
Per Liden4323add2006-01-18 00:38:21 +01001137 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001138 const u32 num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001139 u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140{
1141 struct tipc_msg *hdr = &sender->publ.phdr;
1142 struct link *l_ptr;
1143 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -07001144 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 int res;
1146 u32 selector = msg_origport(hdr) & 1;
1147
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148again:
1149 /*
1150 * Try building message using port's max_pkt hint.
1151 * (Must not hold any locks while building message.)
1152 */
1153
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001154 res = tipc_msg_build(hdr, msg_sect, num_sect, sender->publ.max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 !sender->user_port, &buf);
1156
Per Liden4323add2006-01-18 00:38:21 +01001157 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001158 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001160 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 l_ptr = node->active_links[selector];
1162 if (likely(l_ptr)) {
1163 if (likely(buf)) {
1164 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens05646c92007-06-10 17:25:24 -07001165 &sender->publ.max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 if (unlikely(res < 0))
1167 buf_discard(buf);
1168exit:
Per Liden4323add2006-01-18 00:38:21 +01001169 tipc_node_unlock(node);
1170 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 return res;
1172 }
1173
1174 /* Exit if build request was invalid */
1175
1176 if (unlikely(res < 0))
1177 goto exit;
1178
1179 /* Exit if link (or bearer) is congested */
1180
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001181 if (link_congested(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 !list_empty(&l_ptr->b_ptr->cong_links)) {
1183 res = link_schedule_port(l_ptr,
1184 sender->publ.ref, res);
1185 goto exit;
1186 }
1187
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001188 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 * Message size exceeds max_pkt hint; update hint,
1190 * then re-try fast path or fragment the message
1191 */
1192
Allan Stephens15e979d2010-05-11 14:30:10 +00001193 sender->publ.max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001194 tipc_node_unlock(node);
1195 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196
1197
Allan Stephens05646c92007-06-10 17:25:24 -07001198 if ((msg_hdr_sz(hdr) + res) <= sender->publ.max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 goto again;
1200
1201 return link_send_sections_long(sender, msg_sect,
1202 num_sect, destaddr);
1203 }
Per Liden4323add2006-01-18 00:38:21 +01001204 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 }
Per Liden4323add2006-01-18 00:38:21 +01001206 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207
1208 /* Couldn't find a link to the destination node */
1209
1210 if (buf)
1211 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1212 if (res >= 0)
Per Liden4323add2006-01-18 00:38:21 +01001213 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1214 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 return res;
1216}
1217
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001218/*
1219 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001221 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 * Link and bearer congestion status have been checked to be ok,
1223 * and are ignored if they change.
1224 *
1225 * Note that fragments do not use the full link MTU so that they won't have
1226 * to undergo refragmentation if link changeover causes them to be sent
1227 * over another link with an additional tunnel header added as prefix.
1228 * (Refragmentation will still occur if the other link has a smaller MTU.)
1229 *
1230 * Returns user data length or errno.
1231 */
1232static int link_send_sections_long(struct port *sender,
1233 struct iovec const *msg_sect,
1234 u32 num_sect,
1235 u32 destaddr)
1236{
1237 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001238 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 struct tipc_msg *hdr = &sender->publ.phdr;
1240 u32 dsz = msg_data_sz(hdr);
1241 u32 max_pkt,fragm_sz,rest;
1242 struct tipc_msg fragm_hdr;
1243 struct sk_buff *buf,*buf_chain,*prev;
1244 u32 fragm_crs,fragm_rest,hsz,sect_rest;
1245 const unchar *sect_crs;
1246 int curr_sect;
1247 u32 fragm_no;
1248
1249again:
1250 fragm_no = 1;
Allan Stephens05646c92007-06-10 17:25:24 -07001251 max_pkt = sender->publ.max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001253 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 /* leave room for fragmentation header in each fragment */
1255 rest = dsz;
1256 fragm_crs = 0;
1257 fragm_rest = 0;
1258 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001259 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 curr_sect = -1;
1261
1262 /* Prepare reusable fragment header: */
1263
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001264 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001265 INT_H_SIZE, msg_destnode(hdr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1267 msg_set_size(&fragm_hdr, max_pkt);
1268 msg_set_fragm_no(&fragm_hdr, 1);
1269
1270 /* Prepare header of first fragment: */
1271
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001272 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 if (!buf)
1274 return -ENOMEM;
1275 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001276 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001278 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279
1280 /* Chop up message: */
1281
1282 fragm_crs = INT_H_SIZE + hsz;
1283 fragm_rest = fragm_sz - hsz;
1284
1285 do { /* For all sections */
1286 u32 sz;
1287
1288 if (!sect_rest) {
1289 sect_rest = msg_sect[++curr_sect].iov_len;
1290 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1291 }
1292
1293 if (sect_rest < fragm_rest)
1294 sz = sect_rest;
1295 else
1296 sz = fragm_rest;
1297
1298 if (likely(!sender->user_port)) {
1299 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1300error:
1301 for (; buf_chain; buf_chain = buf) {
1302 buf = buf_chain->next;
1303 buf_discard(buf_chain);
1304 }
1305 return -EFAULT;
1306 }
1307 } else
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001308 skb_copy_to_linear_data_offset(buf, fragm_crs,
1309 sect_crs, sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 sect_crs += sz;
1311 sect_rest -= sz;
1312 fragm_crs += sz;
1313 fragm_rest -= sz;
1314 rest -= sz;
1315
1316 if (!fragm_rest && rest) {
1317
1318 /* Initiate new fragment: */
1319 if (rest <= fragm_sz) {
1320 fragm_sz = rest;
1321 msg_set_type(&fragm_hdr,LAST_FRAGMENT);
1322 } else {
1323 msg_set_type(&fragm_hdr, FRAGMENT);
1324 }
1325 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1326 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1327 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001328 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 if (!buf)
1330 goto error;
1331
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001332 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001334 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 fragm_crs = INT_H_SIZE;
1336 fragm_rest = fragm_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 }
1338 }
1339 while (rest > 0);
1340
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001341 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 * Now we have a buffer chain. Select a link and check
1343 * that packet size is still OK
1344 */
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001345 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001347 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 l_ptr = node->active_links[sender->publ.ref & 1];
1349 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001350 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 goto reject;
1352 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001353 if (l_ptr->max_pkt < max_pkt) {
1354 sender->publ.max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001355 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 for (; buf_chain; buf_chain = buf) {
1357 buf = buf_chain->next;
1358 buf_discard(buf_chain);
1359 }
1360 goto again;
1361 }
1362 } else {
1363reject:
1364 for (; buf_chain; buf_chain = buf) {
1365 buf = buf_chain->next;
1366 buf_discard(buf_chain);
1367 }
Per Liden4323add2006-01-18 00:38:21 +01001368 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1369 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 }
1371
1372 /* Append whole chain to send queue: */
1373
1374 buf = buf_chain;
1375 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1376 if (!l_ptr->next_out)
1377 l_ptr->next_out = buf_chain;
1378 l_ptr->stats.sent_fragmented++;
1379 while (buf) {
1380 struct sk_buff *next = buf->next;
1381 struct tipc_msg *msg = buf_msg(buf);
1382
1383 l_ptr->stats.sent_fragments++;
1384 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1385 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 buf = next;
1387 }
1388
1389 /* Send it, if possible: */
1390
Per Liden4323add2006-01-18 00:38:21 +01001391 tipc_link_push_queue(l_ptr);
1392 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 return dsz;
1394}
1395
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001396/*
Per Liden4323add2006-01-18 00:38:21 +01001397 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 */
Per Liden4323add2006-01-18 00:38:21 +01001399u32 tipc_link_push_packet(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400{
1401 struct sk_buff *buf = l_ptr->first_out;
1402 u32 r_q_size = l_ptr->retransm_queue_size;
1403 u32 r_q_head = l_ptr->retransm_queue_head;
1404
1405 /* Step to position where retransmission failed, if any, */
1406 /* consider that buffers may have been released in meantime */
1407
1408 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001409 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 link_last_sent(l_ptr));
1411 u32 first = msg_seqno(buf_msg(buf));
1412
1413 while (buf && less(first, r_q_head)) {
1414 first = mod(first + 1);
1415 buf = buf->next;
1416 }
1417 l_ptr->retransm_queue_head = r_q_head = first;
1418 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1419 }
1420
1421 /* Continue retransmission now, if there is anything: */
1422
Neil Hormanca509102010-03-15 07:58:45 +00001423 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001425 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001426 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 l_ptr->retransm_queue_head = mod(++r_q_head);
1428 l_ptr->retransm_queue_size = --r_q_size;
1429 l_ptr->stats.retransmitted++;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001430 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 } else {
1432 l_ptr->stats.bearer_congs++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 return PUSH_FAILED;
1434 }
1435 }
1436
1437 /* Send deferred protocol message, if any: */
1438
1439 buf = l_ptr->proto_msg_queue;
1440 if (buf) {
1441 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001442 msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001443 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 l_ptr->unacked_window = 0;
1445 buf_discard(buf);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001446 l_ptr->proto_msg_queue = NULL;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001447 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 l_ptr->stats.bearer_congs++;
1450 return PUSH_FAILED;
1451 }
1452 }
1453
1454 /* Send one deferred data message, if send window not full: */
1455
1456 buf = l_ptr->next_out;
1457 if (buf) {
1458 struct tipc_msg *msg = buf_msg(buf);
1459 u32 next = msg_seqno(msg);
1460 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1461
1462 if (mod(next - first) < l_ptr->queue_limit[0]) {
1463 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001464 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001465 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 if (msg_user(msg) == MSG_BUNDLER)
1467 msg_set_type(msg, CLOSED_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 l_ptr->next_out = buf->next;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001469 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 l_ptr->stats.bearer_congs++;
1472 return PUSH_FAILED;
1473 }
1474 }
1475 }
1476 return PUSH_FINISHED;
1477}
1478
1479/*
1480 * push_queue(): push out the unsent messages of a link where
1481 * congestion has abated. Node is locked
1482 */
Per Liden4323add2006-01-18 00:38:21 +01001483void tipc_link_push_queue(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484{
1485 u32 res;
1486
Per Liden4323add2006-01-18 00:38:21 +01001487 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 return;
1489
1490 do {
Per Liden4323add2006-01-18 00:38:21 +01001491 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001492 } while (!res);
1493
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 if (res == PUSH_FAILED)
Per Liden4323add2006-01-18 00:38:21 +01001495 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496}
1497
Allan Stephensd356eeb2006-06-25 23:40:01 -07001498static void link_reset_all(unsigned long addr)
1499{
David S. Miller6c000552008-09-02 23:38:32 -07001500 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001501 char addr_string[16];
1502 u32 i;
1503
1504 read_lock_bh(&tipc_net_lock);
1505 n_ptr = tipc_node_find((u32)addr);
1506 if (!n_ptr) {
1507 read_unlock_bh(&tipc_net_lock);
1508 return; /* node no longer exists */
1509 }
1510
1511 tipc_node_lock(n_ptr);
1512
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001513 warn("Resetting all links to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001514 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001515
1516 for (i = 0; i < MAX_BEARERS; i++) {
1517 if (n_ptr->links[i]) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001518 link_print(n_ptr->links[i], TIPC_OUTPUT,
Allan Stephensd356eeb2006-06-25 23:40:01 -07001519 "Resetting link\n");
1520 tipc_link_reset(n_ptr->links[i]);
1521 }
1522 }
1523
1524 tipc_node_unlock(n_ptr);
1525 read_unlock_bh(&tipc_net_lock);
1526}
1527
1528static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1529{
1530 struct tipc_msg *msg = buf_msg(buf);
1531
1532 warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001533
1534 if (l_ptr->addr) {
1535
1536 /* Handle failure on standard link */
1537
1538 link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
1539 tipc_link_reset(l_ptr);
1540
1541 } else {
1542
1543 /* Handle failure on broadcast link */
1544
David S. Miller6c000552008-09-02 23:38:32 -07001545 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001546 char addr_string[16];
1547
1548 tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg));
Jeff Garzik617dbea2006-10-03 16:25:34 -07001549 tipc_printf(TIPC_OUTPUT, "Outstanding acks: %lu\n",
1550 (unsigned long) TIPC_SKB_CB(buf)->handle);
1551
Allan Stephensd356eeb2006-06-25 23:40:01 -07001552 n_ptr = l_ptr->owner->next;
1553 tipc_node_lock(n_ptr);
1554
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 tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
1557 tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported);
1558 tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
1559 tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in);
1560 tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after);
1561 tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
1562 tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1563
1564 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1565
1566 tipc_node_unlock(n_ptr);
1567
1568 l_ptr->stale_count = 0;
1569 }
1570}
1571
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001572void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001573 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574{
1575 struct tipc_msg *msg;
1576
Allan Stephensd356eeb2006-06-25 23:40:01 -07001577 if (!buf)
1578 return;
1579
1580 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001581
Allan Stephensd356eeb2006-06-25 23:40:01 -07001582 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Neil Hormanca509102010-03-15 07:58:45 +00001583 if (l_ptr->retransm_queue_size == 0) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001584 dbg_print_link(l_ptr, " ");
1585 l_ptr->retransm_queue_head = msg_seqno(msg);
1586 l_ptr->retransm_queue_size = retransmits;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001587 } else {
Neil Hormanca509102010-03-15 07:58:45 +00001588 err("Unexpected retransmit on link %s (qsize=%d)\n",
1589 l_ptr->name, l_ptr->retransm_queue_size);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001590 }
Neil Hormanca509102010-03-15 07:58:45 +00001591 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001592 } else {
1593 /* Detect repeated retransmit failures on uncongested bearer */
1594
1595 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1596 if (++l_ptr->stale_count > 100) {
1597 link_retransmit_failure(l_ptr, buf);
1598 return;
1599 }
1600 } else {
1601 l_ptr->last_retransmitted = msg_seqno(msg);
1602 l_ptr->stale_count = 1;
1603 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001605
Neil Hormanca509102010-03-15 07:58:45 +00001606 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 msg = buf_msg(buf);
1608 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001609 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001610 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001611 buf = buf->next;
1612 retransmits--;
1613 l_ptr->stats.retransmitted++;
1614 } else {
Per Liden4323add2006-01-18 00:38:21 +01001615 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616 l_ptr->stats.bearer_congs++;
1617 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1618 l_ptr->retransm_queue_size = retransmits;
1619 return;
1620 }
1621 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001622
Per Lidenb97bf3f2006-01-02 19:04:38 +01001623 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1624}
1625
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001626/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627 * link_insert_deferred_queue - insert deferred messages back into receive chain
1628 */
1629
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001630static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631 struct sk_buff *buf)
1632{
1633 u32 seq_no;
1634
1635 if (l_ptr->oldest_deferred_in == NULL)
1636 return buf;
1637
1638 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1639 if (seq_no == mod(l_ptr->next_in_no)) {
1640 l_ptr->newest_deferred_in->next = buf;
1641 buf = l_ptr->oldest_deferred_in;
1642 l_ptr->oldest_deferred_in = NULL;
1643 l_ptr->deferred_inqueue_sz = 0;
1644 }
1645 return buf;
1646}
1647
Allan Stephens85035562008-04-15 19:04:54 -07001648/**
1649 * link_recv_buf_validate - validate basic format of received message
1650 *
1651 * This routine ensures a TIPC message has an acceptable header, and at least
1652 * as much data as the header indicates it should. The routine also ensures
1653 * that the entire message header is stored in the main fragment of the message
1654 * buffer, to simplify future access to message header fields.
1655 *
1656 * Note: Having extra info present in the message header or data areas is OK.
1657 * TIPC will ignore the excess, under the assumption that it is optional info
1658 * introduced by a later release of the protocol.
1659 */
1660
1661static int link_recv_buf_validate(struct sk_buff *buf)
1662{
1663 static u32 min_data_hdr_size[8] = {
1664 SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
1665 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1666 };
1667
1668 struct tipc_msg *msg;
1669 u32 tipc_hdr[2];
1670 u32 size;
1671 u32 hdr_size;
1672 u32 min_hdr_size;
1673
1674 if (unlikely(buf->len < MIN_H_SIZE))
1675 return 0;
1676
1677 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1678 if (msg == NULL)
1679 return 0;
1680
1681 if (unlikely(msg_version(msg) != TIPC_VERSION))
1682 return 0;
1683
1684 size = msg_size(msg);
1685 hdr_size = msg_hdr_sz(msg);
1686 min_hdr_size = msg_isdata(msg) ?
1687 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1688
1689 if (unlikely((hdr_size < min_hdr_size) ||
1690 (size < hdr_size) ||
1691 (buf->len < size) ||
1692 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1693 return 0;
1694
1695 return pskb_may_pull(buf, hdr_size);
1696}
1697
Allan Stephensb02b69c2010-08-17 11:00:07 +00001698/**
1699 * tipc_recv_msg - process TIPC messages arriving from off-node
1700 * @head: pointer to message buffer chain
1701 * @tb_ptr: pointer to bearer message arrived on
1702 *
1703 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1704 * structure (i.e. cannot be NULL), but bearer can be inactive.
1705 */
1706
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1708{
Per Liden4323add2006-01-18 00:38:21 +01001709 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 while (head) {
Allan Stephens1265a022008-06-04 17:32:35 -07001711 struct bearer *b_ptr = (struct bearer *)tb_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001712 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 struct link *l_ptr;
1714 struct sk_buff *crs;
1715 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001716 struct tipc_msg *msg;
1717 u32 seq_no;
1718 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 u32 released = 0;
1720 int type;
1721
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722 head = head->next;
Allan Stephens85035562008-04-15 19:04:54 -07001723
Allan Stephensb02b69c2010-08-17 11:00:07 +00001724 /* Ensure bearer is still enabled */
1725
1726 if (unlikely(!b_ptr->active))
1727 goto cont;
1728
Allan Stephens85035562008-04-15 19:04:54 -07001729 /* Ensure message is well-formed */
1730
1731 if (unlikely(!link_recv_buf_validate(buf)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 goto cont;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733
Allan Stephensfe13dda2008-04-15 19:03:23 -07001734 /* Ensure message data is a single contiguous unit */
1735
1736 if (unlikely(buf_linearize(buf))) {
1737 goto cont;
1738 }
1739
Allan Stephens85035562008-04-15 19:04:54 -07001740 /* Handle arrival of a non-unicast link message */
1741
1742 msg = buf_msg(buf);
1743
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001745 if (msg_user(msg) == LINK_CONFIG)
1746 tipc_disc_recv_msg(buf, b_ptr);
1747 else
1748 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 continue;
1750 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001751
Allan Stephens26008242006-06-25 23:39:31 -07001752 if (unlikely(!msg_short(msg) &&
1753 (msg_destnode(msg) != tipc_own_addr)))
1754 goto cont;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001755
Neil Hormande586572010-03-08 12:43:56 -08001756 /* Discard non-routeable messages destined for another node */
1757
1758 if (unlikely(!msg_isdata(msg) &&
1759 (msg_destnode(msg) != tipc_own_addr))) {
1760 if ((msg_user(msg) != CONN_MANAGER) &&
1761 (msg_user(msg) != MSG_FRAGMENTER))
1762 goto cont;
1763 }
1764
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001765 /* Locate neighboring node that sent message */
Allan Stephens85035562008-04-15 19:04:54 -07001766
Per Liden4323add2006-01-18 00:38:21 +01001767 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 if (unlikely(!n_ptr))
1769 goto cont;
Per Liden4323add2006-01-18 00:38:21 +01001770 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001771
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001772 /* Don't talk to neighbor during cleanup after last session */
1773
1774 if (n_ptr->cleanup_required) {
1775 tipc_node_unlock(n_ptr);
1776 goto cont;
1777 }
1778
1779 /* Locate unicast link endpoint that should handle message */
1780
Per Lidenb97bf3f2006-01-02 19:04:38 +01001781 l_ptr = n_ptr->links[b_ptr->identity];
1782 if (unlikely(!l_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001783 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 goto cont;
1785 }
Allan Stephens85035562008-04-15 19:04:54 -07001786
1787 /* Validate message sequence number info */
1788
1789 seq_no = msg_seqno(msg);
1790 ackd = msg_ack(msg);
1791
1792 /* Release acked messages */
1793
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001795 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1796 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797 }
1798
1799 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001800 while ((crs != l_ptr->next_out) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1802 struct sk_buff *next = crs->next;
1803
1804 buf_discard(crs);
1805 crs = next;
1806 released++;
1807 }
1808 if (released) {
1809 l_ptr->first_out = crs;
1810 l_ptr->out_queue_size -= released;
1811 }
Allan Stephens85035562008-04-15 19:04:54 -07001812
1813 /* Try sending any messages link endpoint has pending */
1814
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001816 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001818 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1820 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001821 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 }
1823
Allan Stephens85035562008-04-15 19:04:54 -07001824 /* Now (finally!) process the incoming message */
1825
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826protocol_check:
1827 if (likely(link_working_working(l_ptr))) {
1828 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1829 l_ptr->next_in_no++;
1830 if (unlikely(l_ptr->oldest_deferred_in))
1831 head = link_insert_deferred_queue(l_ptr,
1832 head);
1833 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1834deliver:
1835 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001836 tipc_node_unlock(n_ptr);
1837 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838 continue;
1839 }
1840 switch (msg_user(msg)) {
1841 case MSG_BUNDLER:
1842 l_ptr->stats.recv_bundles++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001843 l_ptr->stats.recv_bundled +=
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +01001845 tipc_node_unlock(n_ptr);
1846 tipc_link_recv_bundle(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 continue;
1848 case ROUTE_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001849 tipc_node_unlock(n_ptr);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001850 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851 continue;
1852 case NAME_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001853 tipc_node_unlock(n_ptr);
1854 tipc_named_recv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 continue;
1856 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001857 tipc_node_unlock(n_ptr);
1858 tipc_port_recv_proto_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 continue;
1860 case MSG_FRAGMENTER:
1861 l_ptr->stats.recv_fragments++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001862 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
Per Liden4323add2006-01-18 00:38:21 +01001863 &buf, &msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864 l_ptr->stats.recv_fragmented++;
1865 goto deliver;
1866 }
1867 break;
1868 case CHANGEOVER_PROTOCOL:
1869 type = msg_type(msg);
Per Liden4323add2006-01-18 00:38:21 +01001870 if (link_recv_changeover_msg(&l_ptr, &buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871 msg = buf_msg(buf);
1872 seq_no = msg_seqno(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 if (type == ORIGINAL_MSG)
1874 goto deliver;
1875 goto protocol_check;
1876 }
1877 break;
1878 }
1879 }
Per Liden4323add2006-01-18 00:38:21 +01001880 tipc_node_unlock(n_ptr);
1881 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 continue;
1883 }
1884 link_handle_out_of_seq_msg(l_ptr, buf);
1885 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001886 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 continue;
1888 }
1889
1890 if (msg_user(msg) == LINK_PROTOCOL) {
1891 link_recv_proto_msg(l_ptr, buf);
1892 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001893 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 continue;
1895 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1897
1898 if (link_working_working(l_ptr)) {
1899 /* Re-insert in front of queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 buf->next = head;
1901 head = buf;
Per Liden4323add2006-01-18 00:38:21 +01001902 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 continue;
1904 }
Per Liden4323add2006-01-18 00:38:21 +01001905 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906cont:
1907 buf_discard(buf);
1908 }
Per Liden4323add2006-01-18 00:38:21 +01001909 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910}
1911
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001912/*
1913 * link_defer_buf(): Sort a received out-of-sequence packet
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914 * into the deferred reception queue.
1915 * Returns the increase of the queue length,i.e. 0 or 1
1916 */
1917
Per Liden4323add2006-01-18 00:38:21 +01001918u32 tipc_link_defer_pkt(struct sk_buff **head,
1919 struct sk_buff **tail,
1920 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001922 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923 struct sk_buff *crs = *head;
1924 u32 seq_no = msg_seqno(buf_msg(buf));
1925
1926 buf->next = NULL;
1927
1928 /* Empty queue ? */
1929 if (*head == NULL) {
1930 *head = *tail = buf;
1931 return 1;
1932 }
1933
1934 /* Last ? */
1935 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1936 (*tail)->next = buf;
1937 *tail = buf;
1938 return 1;
1939 }
1940
1941 /* Scan through queue and sort it in */
1942 do {
1943 struct tipc_msg *msg = buf_msg(crs);
1944
1945 if (less(seq_no, msg_seqno(msg))) {
1946 buf->next = crs;
1947 if (prev)
1948 prev->next = buf;
1949 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001950 *head = buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 return 1;
1952 }
1953 if (seq_no == msg_seqno(msg)) {
1954 break;
1955 }
1956 prev = crs;
1957 crs = crs->next;
1958 }
1959 while (crs);
1960
1961 /* Message is a duplicate of an existing message */
1962
1963 buf_discard(buf);
1964 return 0;
1965}
1966
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001967/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1969 */
1970
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001971static void link_handle_out_of_seq_msg(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001972 struct sk_buff *buf)
1973{
1974 u32 seq_no = msg_seqno(buf_msg(buf));
1975
1976 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1977 link_recv_proto_msg(l_ptr, buf);
1978 return;
1979 }
1980
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981 /* Record OOS packet arrival (force mismatch on next timeout) */
1982
1983 l_ptr->checkpoint--;
1984
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001985 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986 * Discard packet if a duplicate; otherwise add it to deferred queue
1987 * and notify peer of gap as per protocol specification
1988 */
1989
1990 if (less(seq_no, mod(l_ptr->next_in_no))) {
1991 l_ptr->stats.duplicates++;
1992 buf_discard(buf);
1993 return;
1994 }
1995
Per Liden4323add2006-01-18 00:38:21 +01001996 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1997 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001998 l_ptr->deferred_inqueue_sz++;
1999 l_ptr->stats.deferred_recv++;
2000 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01002001 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002002 } else
2003 l_ptr->stats.duplicates++;
2004}
2005
2006/*
2007 * Send protocol message to the other endpoint.
2008 */
Per Liden4323add2006-01-18 00:38:21 +01002009void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
2010 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002012 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002013 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002014 u32 msg_size = sizeof(l_ptr->proto_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015
2016 if (link_blocked(l_ptr))
2017 return;
2018 msg_set_type(msg, msg_typ);
2019 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002020 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
Per Liden4323add2006-01-18 00:38:21 +01002021 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022
2023 if (msg_typ == STATE_MSG) {
2024 u32 next_sent = mod(l_ptr->next_out_no);
2025
Per Liden4323add2006-01-18 00:38:21 +01002026 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 return;
2028 if (l_ptr->next_out)
2029 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
2030 msg_set_next_sent(msg, next_sent);
2031 if (l_ptr->oldest_deferred_in) {
2032 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2033 gap = mod(rec - mod(l_ptr->next_in_no));
2034 }
2035 msg_set_seq_gap(msg, gap);
2036 if (gap)
2037 l_ptr->stats.sent_nacks++;
2038 msg_set_link_tolerance(msg, tolerance);
2039 msg_set_linkprio(msg, priority);
2040 msg_set_max_pkt(msg, ack_mtu);
2041 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
2042 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002043 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044 u32 mtu = l_ptr->max_pkt;
2045
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002046 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002047 link_working_working(l_ptr) &&
2048 l_ptr->fsm_msg_cnt) {
2049 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002050 if (l_ptr->max_pkt_probes == 10) {
2051 l_ptr->max_pkt_target = (msg_size - 4);
2052 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002054 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002056 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057
2058 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002059 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002060 l_ptr->stats.sent_states++;
2061 } else { /* RESET_MSG or ACTIVATE_MSG */
2062 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
2063 msg_set_seq_gap(msg, 0);
2064 msg_set_next_sent(msg, 1);
2065 msg_set_link_tolerance(msg, l_ptr->tolerance);
2066 msg_set_linkprio(msg, l_ptr->priority);
2067 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
2068 }
2069
Per Liden4323add2006-01-18 00:38:21 +01002070 if (tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071 msg_set_redundant_link(msg);
2072 } else {
2073 msg_clear_redundant_link(msg);
2074 }
2075 msg_set_linkprio(msg, l_ptr->priority);
2076
2077 /* Ensure sequence number will not fit : */
2078
2079 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
2080
2081 /* Congestion? */
2082
Per Liden4323add2006-01-18 00:38:21 +01002083 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084 if (!l_ptr->proto_msg_queue) {
2085 l_ptr->proto_msg_queue =
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002086 tipc_buf_acquire(sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002087 }
2088 buf = l_ptr->proto_msg_queue;
2089 if (!buf)
2090 return;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002091 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092 return;
2093 }
2094 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
2095
2096 /* Message can be sent */
2097
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002098 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099 if (!buf)
2100 return;
2101
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002102 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002103 msg_set_size(buf_msg(buf), msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104
Per Liden4323add2006-01-18 00:38:21 +01002105 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 l_ptr->unacked_window = 0;
2107 buf_discard(buf);
2108 return;
2109 }
2110
2111 /* New congestion */
Per Liden4323add2006-01-18 00:38:21 +01002112 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113 l_ptr->proto_msg_queue = buf;
2114 l_ptr->stats.bearer_congs++;
2115}
2116
2117/*
2118 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002119 * Note that network plane id propagates through the network, and may
2120 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121 */
2122
2123static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2124{
2125 u32 rec_gap = 0;
2126 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002127 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128 u32 msg_tol;
2129 struct tipc_msg *msg = buf_msg(buf);
2130
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 if (link_blocked(l_ptr))
2132 goto exit;
2133
2134 /* record unnumbered packet arrival (force mismatch on next timeout) */
2135
2136 l_ptr->checkpoint--;
2137
2138 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2139 if (tipc_own_addr > msg_prevnode(msg))
2140 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2141
2142 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2143
2144 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002145
Per Lidenb97bf3f2006-01-02 19:04:38 +01002146 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07002147 if (!link_working_unknown(l_ptr) &&
2148 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002149 if (msg_session(msg) == l_ptr->peer_session)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 break; /* duplicate: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002151 }
2152 /* fall thru' */
2153 case ACTIVATE_MSG:
2154 /* Update link settings according other endpoint's values */
2155
2156 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2157
2158 if ((msg_tol = msg_link_tolerance(msg)) &&
2159 (msg_tol > l_ptr->tolerance))
2160 link_set_supervision_props(l_ptr, msg_tol);
2161
2162 if (msg_linkprio(msg) > l_ptr->priority)
2163 l_ptr->priority = msg_linkprio(msg);
2164
2165 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002166 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002167 if (max_pkt_info < l_ptr->max_pkt_target)
2168 l_ptr->max_pkt_target = max_pkt_info;
2169 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2170 l_ptr->max_pkt = l_ptr->max_pkt_target;
2171 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002172 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002173 }
2174 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2175
2176 link_state_event(l_ptr, msg_type(msg));
2177
2178 l_ptr->peer_session = msg_session(msg);
2179 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2180
2181 /* Synchronize broadcast sequence numbers */
Per Liden4323add2006-01-18 00:38:21 +01002182 if (!tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002183 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2184 }
2185 break;
2186 case STATE_MSG:
2187
2188 if ((msg_tol = msg_link_tolerance(msg)))
2189 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002190
2191 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192 (msg_linkprio(msg) != l_ptr->priority)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002193 warn("Resetting link <%s>, priority change %u->%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002194 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2195 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01002196 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002197 break;
2198 }
2199 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2200 l_ptr->stats.recv_states++;
2201 if (link_reset_unknown(l_ptr))
2202 break;
2203
2204 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002205 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206 mod(l_ptr->next_in_no));
2207 }
2208
2209 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002210 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002211 l_ptr->max_pkt = max_pkt_ack;
2212 l_ptr->max_pkt_probes = 0;
2213 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214
2215 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002216 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217 l_ptr->stats.recv_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002218 if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2219 max_pkt_ack = msg_size(msg);
2220 }
2221 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002222
2223 /* Protocol message before retransmits, reduce loss risk */
2224
Per Liden4323add2006-01-18 00:38:21 +01002225 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002226
2227 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01002228 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2229 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002230 }
2231 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002232 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01002233 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2234 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002235 }
2236 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002237 }
2238exit:
2239 buf_discard(buf);
2240}
2241
2242
2243/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002244 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01002245 * another bearer. Owner node is locked.
2246 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002247static void tipc_link_tunnel(struct link *l_ptr,
2248 struct tipc_msg *tunnel_hdr,
2249 struct tipc_msg *msg,
2250 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002251{
2252 struct link *tunnel;
2253 struct sk_buff *buf;
2254 u32 length = msg_size(msg);
2255
2256 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07002257 if (!tipc_link_is_up(tunnel)) {
2258 warn("Link changeover error, "
2259 "tunnel link no longer available\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002260 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002261 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002262 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002263 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07002264 if (!buf) {
2265 warn("Link changeover error, "
2266 "unable to send tunnel msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002268 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002269 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2270 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Liden4323add2006-01-18 00:38:21 +01002271 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002272}
2273
2274
2275
2276/*
2277 * changeover(): Send whole message queue via the remaining link
2278 * Owner node is locked.
2279 */
2280
Per Liden4323add2006-01-18 00:38:21 +01002281void tipc_link_changeover(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002282{
2283 u32 msgcount = l_ptr->out_queue_size;
2284 struct sk_buff *crs = l_ptr->first_out;
2285 struct link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002287 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002288
2289 if (!tunnel)
2290 return;
2291
Allan Stephens5392d642006-06-25 23:52:50 -07002292 if (!l_ptr->owner->permit_changeover) {
2293 warn("Link changeover error, "
2294 "peer did not permit changeover\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002295 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002296 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002297
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002298 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002299 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002300 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2301 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002302
Per Lidenb97bf3f2006-01-02 19:04:38 +01002303 if (!l_ptr->first_out) {
2304 struct sk_buff *buf;
2305
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002306 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002307 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002308 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002309 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Per Liden4323add2006-01-18 00:38:21 +01002310 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002311 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002312 warn("Link changeover error, "
2313 "unable to send changeover msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002314 }
2315 return;
2316 }
Allan Stephensf1310722006-06-25 23:51:37 -07002317
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002318 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002319 l_ptr->owner->active_links[1]);
2320
Per Lidenb97bf3f2006-01-02 19:04:38 +01002321 while (crs) {
2322 struct tipc_msg *msg = buf_msg(crs);
2323
2324 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002325 struct tipc_msg *m = msg_get_wrapped(msg);
2326 unchar* pos = (unchar*)m;
2327
Florian Westphald788d802007-08-02 19:28:06 -07002328 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002329 while (msgcount--) {
2330 msg_set_seqno(m,msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002331 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2332 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002333 pos += align(msg_size(m));
2334 m = (struct tipc_msg *)pos;
2335 }
2336 } else {
Per Liden4323add2006-01-18 00:38:21 +01002337 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2338 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002339 }
2340 crs = crs->next;
2341 }
2342}
2343
Per Liden4323add2006-01-18 00:38:21 +01002344void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002345{
2346 struct sk_buff *iter;
2347 struct tipc_msg tunnel_hdr;
2348
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002349 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002350 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002351 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2352 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2353 iter = l_ptr->first_out;
2354 while (iter) {
2355 struct sk_buff *outbuf;
2356 struct tipc_msg *msg = buf_msg(iter);
2357 u32 length = msg_size(msg);
2358
2359 if (msg_user(msg) == MSG_BUNDLER)
2360 msg_set_type(msg, CLOSED_MSG);
2361 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002362 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002364 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002365 if (outbuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002366 warn("Link changeover error, "
2367 "unable to send duplicate msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002368 return;
2369 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002370 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2371 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2372 length);
Per Liden4323add2006-01-18 00:38:21 +01002373 tipc_link_send_buf(tunnel, outbuf);
2374 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375 return;
2376 iter = iter->next;
2377 }
2378}
2379
2380
2381
2382/**
2383 * buf_extract - extracts embedded TIPC message from another message
2384 * @skb: encapsulating message buffer
2385 * @from_pos: offset to extract from
2386 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002387 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002388 * encapsulating message itself is left unchanged.
2389 */
2390
2391static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2392{
2393 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2394 u32 size = msg_size(msg);
2395 struct sk_buff *eb;
2396
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002397 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002398 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002399 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002400 return eb;
2401}
2402
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002403/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002404 * link_recv_changeover_msg(): Receive tunneled packet sent
2405 * via other link. Node is locked. Return extracted buffer.
2406 */
2407
2408static int link_recv_changeover_msg(struct link **l_ptr,
2409 struct sk_buff **buf)
2410{
2411 struct sk_buff *tunnel_buf = *buf;
2412 struct link *dest_link;
2413 struct tipc_msg *msg;
2414 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2415 u32 msg_typ = msg_type(tunnel_msg);
2416 u32 msg_count = msg_msgcnt(tunnel_msg);
2417
2418 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
Allan Stephensb29f1422010-12-31 18:59:25 +00002419 if (!dest_link)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002420 goto exit;
Allan Stephensf1310722006-06-25 23:51:37 -07002421 if (dest_link == *l_ptr) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002422 err("Unexpected changeover message on link <%s>\n",
Allan Stephensf1310722006-06-25 23:51:37 -07002423 (*l_ptr)->name);
2424 goto exit;
2425 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002426 *l_ptr = dest_link;
2427 msg = msg_get_wrapped(tunnel_msg);
2428
2429 if (msg_typ == DUPLICATE_MSG) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002430 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002431 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002432 *buf = buf_extract(tunnel_buf,INT_H_SIZE);
2433 if (*buf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002434 warn("Link changeover error, duplicate msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435 goto exit;
2436 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 buf_discard(tunnel_buf);
2438 return 1;
2439 }
2440
2441 /* First original message ?: */
2442
Per Liden4323add2006-01-18 00:38:21 +01002443 if (tipc_link_is_up(dest_link)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002444 info("Resetting link <%s>, changeover initiated by peer\n",
2445 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002446 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002447 dest_link->exp_msg_count = msg_count;
2448 if (!msg_count)
2449 goto exit;
2450 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002451 dest_link->exp_msg_count = msg_count;
2452 if (!msg_count)
2453 goto exit;
2454 }
2455
2456 /* Receive original message */
2457
2458 if (dest_link->exp_msg_count == 0) {
Allan Stephens5392d642006-06-25 23:52:50 -07002459 warn("Link switchover error, "
2460 "got too many tunnelled messages\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002461 dbg_print_link(dest_link, "LINK:");
2462 goto exit;
2463 }
2464 dest_link->exp_msg_count--;
2465 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002466 goto exit;
2467 } else {
2468 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2469 if (*buf != NULL) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002470 buf_discard(tunnel_buf);
2471 return 1;
2472 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002473 warn("Link changeover error, original msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002474 }
2475 }
2476exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002477 *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002478 buf_discard(tunnel_buf);
2479 return 0;
2480}
2481
2482/*
2483 * Bundler functionality:
2484 */
Per Liden4323add2006-01-18 00:38:21 +01002485void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002486{
2487 u32 msgcount = msg_msgcnt(buf_msg(buf));
2488 u32 pos = INT_H_SIZE;
2489 struct sk_buff *obuf;
2490
Per Lidenb97bf3f2006-01-02 19:04:38 +01002491 while (msgcount--) {
2492 obuf = buf_extract(buf, pos);
2493 if (obuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002494 warn("Link unable to unbundle message(s)\n");
2495 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002496 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002497 pos += align(msg_size(buf_msg(obuf)));
Per Liden4323add2006-01-18 00:38:21 +01002498 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002499 }
2500 buf_discard(buf);
2501}
2502
2503/*
2504 * Fragmentation/defragmentation:
2505 */
2506
2507
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002508/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002509 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002510 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002511 * Returns user data length.
2512 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002513static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002514{
2515 struct tipc_msg *inmsg = buf_msg(buf);
2516 struct tipc_msg fragm_hdr;
2517 u32 insize = msg_size(inmsg);
2518 u32 dsz = msg_data_sz(inmsg);
2519 unchar *crs = buf->data;
2520 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002521 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002522 u32 fragm_sz = pack_sz - INT_H_SIZE;
2523 u32 fragm_no = 1;
Allan Stephens9c396a72008-06-04 17:36:58 -07002524 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002525
2526 if (msg_short(inmsg))
2527 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002528 else
2529 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002530
2531 if (msg_routed(inmsg))
2532 msg_set_prevnode(inmsg, tipc_own_addr);
2533
2534 /* Prepare reusable fragment header: */
2535
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002536 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002537 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002538 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2539 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2540 msg_set_fragm_no(&fragm_hdr, fragm_no);
2541 l_ptr->stats.sent_fragmented++;
2542
2543 /* Chop up message: */
2544
2545 while (rest > 0) {
2546 struct sk_buff *fragm;
2547
2548 if (rest <= fragm_sz) {
2549 fragm_sz = rest;
2550 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2551 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002552 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002553 if (fragm == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002554 warn("Link unable to fragment message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002555 dsz = -ENOMEM;
2556 goto exit;
2557 }
2558 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002559 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2560 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2561 fragm_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002562 /* Send queued messages first, if any: */
2563
2564 l_ptr->stats.sent_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01002565 tipc_link_send_buf(l_ptr, fragm);
2566 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002567 return dsz;
2568 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2569 rest -= fragm_sz;
2570 crs += fragm_sz;
2571 msg_set_type(&fragm_hdr, FRAGMENT);
2572 }
2573exit:
2574 buf_discard(buf);
2575 return dsz;
2576}
2577
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002578/*
2579 * A pending message being re-assembled must store certain values
2580 * to handle subsequent fragments correctly. The following functions
Per Lidenb97bf3f2006-01-02 19:04:38 +01002581 * help storing these values in unused, available fields in the
2582 * pending message. This makes dynamic memory allocation unecessary.
2583 */
2584
Sam Ravnborg05790c62006-03-20 22:37:04 -08002585static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586{
2587 msg_set_seqno(buf_msg(buf), seqno);
2588}
2589
Sam Ravnborg05790c62006-03-20 22:37:04 -08002590static u32 get_fragm_size(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002591{
2592 return msg_ack(buf_msg(buf));
2593}
2594
Sam Ravnborg05790c62006-03-20 22:37:04 -08002595static void set_fragm_size(struct sk_buff *buf, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002596{
2597 msg_set_ack(buf_msg(buf), sz);
2598}
2599
Sam Ravnborg05790c62006-03-20 22:37:04 -08002600static u32 get_expected_frags(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002601{
2602 return msg_bcast_ack(buf_msg(buf));
2603}
2604
Sam Ravnborg05790c62006-03-20 22:37:04 -08002605static void set_expected_frags(struct sk_buff *buf, u32 exp)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002606{
2607 msg_set_bcast_ack(buf_msg(buf), exp);
2608}
2609
Sam Ravnborg05790c62006-03-20 22:37:04 -08002610static u32 get_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611{
2612 return msg_reroute_cnt(buf_msg(buf));
2613}
2614
Sam Ravnborg05790c62006-03-20 22:37:04 -08002615static void incr_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002616{
2617 msg_incr_reroute_cnt(buf_msg(buf));
2618}
2619
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002620/*
2621 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622 * the reassembled buffer if message is complete.
2623 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002624int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
Per Liden4323add2006-01-18 00:38:21 +01002625 struct tipc_msg **m)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002626{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002627 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002628 struct sk_buff *fbuf = *fb;
2629 struct tipc_msg *fragm = buf_msg(fbuf);
2630 struct sk_buff *pbuf = *pending;
2631 u32 long_msg_seq_no = msg_long_msgno(fragm);
2632
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002633 *fb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002634
2635 /* Is there an incomplete message waiting for this fragment? */
2636
Joe Perchesf64f9e72009-11-29 16:55:45 -08002637 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
2638 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002639 prev = pbuf;
2640 pbuf = pbuf->next;
2641 }
2642
2643 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2644 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2645 u32 msg_sz = msg_size(imsg);
2646 u32 fragm_sz = msg_data_sz(fragm);
2647 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2648 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2649 if (msg_type(imsg) == TIPC_MCAST_MSG)
2650 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2651 if (msg_size(imsg) > max) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002652 buf_discard(fbuf);
2653 return 0;
2654 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002655 pbuf = tipc_buf_acquire(msg_size(imsg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002656 if (pbuf != NULL) {
2657 pbuf->next = *pending;
2658 *pending = pbuf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002659 skb_copy_to_linear_data(pbuf, imsg,
2660 msg_data_sz(fragm));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002661 /* Prepare buffer for subsequent fragments. */
2662
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002663 set_long_msg_seqno(pbuf, long_msg_seq_no);
2664 set_fragm_size(pbuf,fragm_sz);
2665 set_expected_frags(pbuf,exp_fragm_cnt - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002666 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002667 warn("Link unable to reassemble fragmented message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002668 }
2669 buf_discard(fbuf);
2670 return 0;
2671 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2672 u32 dsz = msg_data_sz(fragm);
2673 u32 fsz = get_fragm_size(pbuf);
2674 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2675 u32 exp_frags = get_expected_frags(pbuf) - 1;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002676 skb_copy_to_linear_data_offset(pbuf, crs,
2677 msg_data(fragm), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002678 buf_discard(fbuf);
2679
2680 /* Is message complete? */
2681
2682 if (exp_frags == 0) {
2683 if (prev)
2684 prev->next = pbuf->next;
2685 else
2686 *pending = pbuf->next;
2687 msg_reset_reroute_cnt(buf_msg(pbuf));
2688 *fb = pbuf;
2689 *m = buf_msg(pbuf);
2690 return 1;
2691 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002692 set_expected_frags(pbuf,exp_frags);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002693 return 0;
2694 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002695 buf_discard(fbuf);
2696 return 0;
2697}
2698
2699/**
2700 * link_check_defragm_bufs - flush stale incoming message fragments
2701 * @l_ptr: pointer to link
2702 */
2703
2704static void link_check_defragm_bufs(struct link *l_ptr)
2705{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002706 struct sk_buff *prev = NULL;
2707 struct sk_buff *next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002708 struct sk_buff *buf = l_ptr->defragm_buf;
2709
2710 if (!buf)
2711 return;
2712 if (!link_working_working(l_ptr))
2713 return;
2714 while (buf) {
2715 u32 cnt = get_timer_cnt(buf);
2716
2717 next = buf->next;
2718 if (cnt < 4) {
2719 incr_timer_cnt(buf);
2720 prev = buf;
2721 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002722 if (prev)
2723 prev->next = buf->next;
2724 else
2725 l_ptr->defragm_buf = buf->next;
2726 buf_discard(buf);
2727 }
2728 buf = next;
2729 }
2730}
2731
2732
2733
2734static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2735{
2736 l_ptr->tolerance = tolerance;
2737 l_ptr->continuity_interval =
2738 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2739 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2740}
2741
2742
Per Liden4323add2006-01-18 00:38:21 +01002743void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002744{
2745 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002746 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2747 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2748 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2749 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002750 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002751 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2752 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2753 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2754 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002755 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002756 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2757 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2758 /* FRAGMENT and LAST_FRAGMENT packets */
2759 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2760}
2761
2762/**
2763 * link_find_link - locate link by name
2764 * @name - ptr to link name string
2765 * @node - ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002766 *
Per Liden4323add2006-01-18 00:38:21 +01002767 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002768 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002769 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002770 * Returns pointer to link (or 0 if invalid link name).
2771 */
2772
David S. Miller6c000552008-09-02 23:38:32 -07002773static struct link *link_find_link(const char *name, struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002774{
2775 struct link_name link_name_parts;
2776 struct bearer *b_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002777 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002778
2779 if (!link_name_validate(name, &link_name_parts))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002780 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002781
Per Liden4323add2006-01-18 00:38:21 +01002782 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002783 if (!b_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002784 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002785
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002786 *node = tipc_node_find(link_name_parts.addr_peer);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002787 if (!*node)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002788 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002789
2790 l_ptr = (*node)->links[b_ptr->identity];
2791 if (!l_ptr || strcmp(l_ptr->name, name))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002792 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002793
2794 return l_ptr;
2795}
2796
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002797struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002798 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002799{
2800 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002801 u32 new_value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002802 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002803 struct tipc_node *node;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002804 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002805
2806 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002807 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002808
2809 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2810 new_value = ntohl(args->value);
2811
Per Liden4323add2006-01-18 00:38:21 +01002812 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002813 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002814 (tipc_bclink_set_queue_limits(new_value) == 0))
2815 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002816 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002817 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002818 }
2819
Per Liden4323add2006-01-18 00:38:21 +01002820 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002821 l_ptr = link_find_link(args->name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002822 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002823 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002824 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002825 }
2826
Per Liden4323add2006-01-18 00:38:21 +01002827 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002828 res = -EINVAL;
2829 switch (cmd) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002830 case TIPC_CMD_SET_LINK_TOL:
2831 if ((new_value >= TIPC_MIN_LINK_TOL) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002832 (new_value <= TIPC_MAX_LINK_TOL)) {
2833 link_set_supervision_props(l_ptr, new_value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002834 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002835 0, 0, new_value, 0, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002836 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002837 }
2838 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002839 case TIPC_CMD_SET_LINK_PRI:
Per Liden16cb4b32006-01-13 22:22:22 +01002840 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2841 (new_value <= TIPC_MAX_LINK_PRI)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002842 l_ptr->priority = new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002843 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002844 0, 0, 0, new_value, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002845 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002846 }
2847 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002848 case TIPC_CMD_SET_LINK_WINDOW:
2849 if ((new_value >= TIPC_MIN_LINK_WIN) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002850 (new_value <= TIPC_MAX_LINK_WIN)) {
Per Liden4323add2006-01-18 00:38:21 +01002851 tipc_link_set_queue_limits(l_ptr, new_value);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002852 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002853 }
2854 break;
2855 }
Per Liden4323add2006-01-18 00:38:21 +01002856 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002857
Per Liden4323add2006-01-18 00:38:21 +01002858 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002859 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002860 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002861
Per Liden4323add2006-01-18 00:38:21 +01002862 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002863}
2864
2865/**
2866 * link_reset_statistics - reset link statistics
2867 * @l_ptr: pointer to link
2868 */
2869
2870static void link_reset_statistics(struct link *l_ptr)
2871{
2872 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2873 l_ptr->stats.sent_info = l_ptr->next_out_no;
2874 l_ptr->stats.recv_info = l_ptr->next_in_no;
2875}
2876
Per Liden4323add2006-01-18 00:38:21 +01002877struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002878{
2879 char *link_name;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002880 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002881 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002882
2883 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002884 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002885
2886 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002887 if (!strcmp(link_name, tipc_bclink_name)) {
2888 if (tipc_bclink_reset_stats())
2889 return tipc_cfg_reply_error_string("link not found");
2890 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002891 }
2892
Per Liden4323add2006-01-18 00:38:21 +01002893 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002894 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002895 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002896 read_unlock_bh(&tipc_net_lock);
2897 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002898 }
2899
Per Liden4323add2006-01-18 00:38:21 +01002900 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002901 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002902 tipc_node_unlock(node);
2903 read_unlock_bh(&tipc_net_lock);
2904 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002905}
2906
2907/**
2908 * percent - convert count to a percentage of total (rounding up or down)
2909 */
2910
2911static u32 percent(u32 count, u32 total)
2912{
2913 return (count * 100 + (total / 2)) / total;
2914}
2915
2916/**
Per Liden4323add2006-01-18 00:38:21 +01002917 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002918 * @name: link name
2919 * @buf: print buffer area
2920 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002921 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002922 * Returns length of print buffer data string (or 0 if error)
2923 */
2924
Per Liden4323add2006-01-18 00:38:21 +01002925static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002926{
2927 struct print_buf pb;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002928 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002929 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002930 char *status;
2931 u32 profile_total = 0;
2932
Per Liden4323add2006-01-18 00:38:21 +01002933 if (!strcmp(name, tipc_bclink_name))
2934 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002935
Per Liden4323add2006-01-18 00:38:21 +01002936 tipc_printbuf_init(&pb, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002937
Per Liden4323add2006-01-18 00:38:21 +01002938 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002939 l_ptr = link_find_link(name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002940 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002941 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002942 return 0;
2943 }
Per Liden4323add2006-01-18 00:38:21 +01002944 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002945
Per Liden4323add2006-01-18 00:38:21 +01002946 if (tipc_link_is_active(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002947 status = "ACTIVE";
Per Liden4323add2006-01-18 00:38:21 +01002948 else if (tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002949 status = "STANDBY";
2950 else
2951 status = "DEFUNCT";
2952 tipc_printf(&pb, "Link <%s>\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002953 " %s MTU:%u Priority:%u Tolerance:%u ms"
2954 " Window:%u packets\n",
Allan Stephens15e979d2010-05-11 14:30:10 +00002955 l_ptr->name, status, l_ptr->max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002956 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002957 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002958 l_ptr->next_in_no - l_ptr->stats.recv_info,
2959 l_ptr->stats.recv_fragments,
2960 l_ptr->stats.recv_fragmented,
2961 l_ptr->stats.recv_bundles,
2962 l_ptr->stats.recv_bundled);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002963 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002964 l_ptr->next_out_no - l_ptr->stats.sent_info,
2965 l_ptr->stats.sent_fragments,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002966 l_ptr->stats.sent_fragmented,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002967 l_ptr->stats.sent_bundles,
2968 l_ptr->stats.sent_bundled);
2969 profile_total = l_ptr->stats.msg_length_counts;
2970 if (!profile_total)
2971 profile_total = 1;
2972 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002973 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2974 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002975 l_ptr->stats.msg_length_counts,
2976 l_ptr->stats.msg_lengths_total / profile_total,
2977 percent(l_ptr->stats.msg_length_profile[0], profile_total),
2978 percent(l_ptr->stats.msg_length_profile[1], profile_total),
2979 percent(l_ptr->stats.msg_length_profile[2], profile_total),
2980 percent(l_ptr->stats.msg_length_profile[3], profile_total),
2981 percent(l_ptr->stats.msg_length_profile[4], profile_total),
2982 percent(l_ptr->stats.msg_length_profile[5], profile_total),
2983 percent(l_ptr->stats.msg_length_profile[6], profile_total));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002984 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002985 l_ptr->stats.recv_states,
2986 l_ptr->stats.recv_probes,
2987 l_ptr->stats.recv_nacks,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002988 l_ptr->stats.deferred_recv,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002989 l_ptr->stats.duplicates);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002990 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
2991 l_ptr->stats.sent_states,
2992 l_ptr->stats.sent_probes,
2993 l_ptr->stats.sent_nacks,
2994 l_ptr->stats.sent_acks,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002995 l_ptr->stats.retransmitted);
2996 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
2997 l_ptr->stats.bearer_congs,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002998 l_ptr->stats.link_congs,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002999 l_ptr->stats.max_queue_sz,
3000 l_ptr->stats.queue_sz_counts
3001 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
3002 : 0);
3003
Per Liden4323add2006-01-18 00:38:21 +01003004 tipc_node_unlock(node);
3005 read_unlock_bh(&tipc_net_lock);
3006 return tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003007}
3008
3009#define MAX_LINK_STATS_INFO 2000
3010
Per Liden4323add2006-01-18 00:38:21 +01003011struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003012{
3013 struct sk_buff *buf;
3014 struct tlv_desc *rep_tlv;
3015 int str_len;
3016
3017 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01003018 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003019
Per Liden4323add2006-01-18 00:38:21 +01003020 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
Per Lidenb97bf3f2006-01-02 19:04:38 +01003021 if (!buf)
3022 return NULL;
3023
3024 rep_tlv = (struct tlv_desc *)buf->data;
3025
Per Liden4323add2006-01-18 00:38:21 +01003026 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
3027 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003028 if (!str_len) {
3029 buf_discard(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003030 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003031 }
3032
3033 skb_put(buf, TLV_SPACE(str_len));
3034 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
3035
3036 return buf;
3037}
3038
Per Lidenb97bf3f2006-01-02 19:04:38 +01003039/**
Per Liden4323add2006-01-18 00:38:21 +01003040 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01003041 * @dest: network address of destination node
3042 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003043 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003044 * If no active link can be found, uses default maximum packet size.
3045 */
3046
Per Liden4323add2006-01-18 00:38:21 +01003047u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003048{
David S. Miller6c000552008-09-02 23:38:32 -07003049 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003050 struct link *l_ptr;
3051 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003052
Per Lidenb97bf3f2006-01-02 19:04:38 +01003053 if (dest == tipc_own_addr)
3054 return MAX_MSG_SIZE;
3055
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003056 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00003057 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003058 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003059 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003060 l_ptr = n_ptr->active_links[selector & 1];
3061 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00003062 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01003063 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003064 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003065 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003066 return res;
3067}
3068
Per Lidenb97bf3f2006-01-02 19:04:38 +01003069static void link_print(struct link *l_ptr, struct print_buf *buf,
3070 const char *str)
3071{
3072 tipc_printf(buf, str);
3073 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
3074 return;
3075 tipc_printf(buf, "Link %x<%s>:",
3076 l_ptr->addr, l_ptr->b_ptr->publ.name);
3077 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
3078 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
3079 tipc_printf(buf, "SQUE");
3080 if (l_ptr->first_out) {
3081 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
3082 if (l_ptr->next_out)
3083 tipc_printf(buf, "%u..",
3084 msg_seqno(buf_msg(l_ptr->next_out)));
Allan Stephensb82834e2010-05-11 14:30:04 +00003085 tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003086 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
3087 msg_seqno(buf_msg(l_ptr->first_out)))
Joe Perchesf64f9e72009-11-29 16:55:45 -08003088 != (l_ptr->out_queue_size - 1)) ||
3089 (l_ptr->last_out->next != NULL)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01003090 tipc_printf(buf, "\nSend queue inconsistency\n");
3091 tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
3092 tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
3093 tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003094 }
3095 } else
3096 tipc_printf(buf, "[]");
3097 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
3098 if (l_ptr->oldest_deferred_in) {
3099 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
3100 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
3101 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
3102 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
3103 tipc_printf(buf, ":RQSIZ(%u)",
3104 l_ptr->deferred_inqueue_sz);
3105 }
3106 }
3107 if (link_working_unknown(l_ptr))
3108 tipc_printf(buf, ":WU");
3109 if (link_reset_reset(l_ptr))
3110 tipc_printf(buf, ":RR");
3111 if (link_reset_unknown(l_ptr))
3112 tipc_printf(buf, ":RU");
3113 if (link_working_working(l_ptr))
3114 tipc_printf(buf, ":WW");
3115 tipc_printf(buf, "\n");
3116}
3117