tipc: Allow connect() to wait indefinitely
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / tipc / link.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/link.c: TIPC link code
c4307285 3 *
05646c91
AS
4 * Copyright (c) 1996-2007, Ericsson AB
5 * Copyright (c) 2004-2007, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
b97bf3fd 19 *
9ea1fd3c
PL
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "dbg.h"
39#include "link.h"
40#include "net.h"
41#include "node.h"
42#include "port.h"
43#include "addr.h"
44#include "node_subscr.h"
45#include "name_distr.h"
46#include "bearer.h"
47#include "name_table.h"
48#include "discover.h"
49#include "config.h"
50#include "bcast.h"
51
52
a686e685
AS
53/*
54 * Out-of-range value for link session numbers
55 */
56
57#define INVALID_SESSION 0x10000
58
c4307285
YH
59/*
60 * Limit for deferred reception queue:
b97bf3fd
PL
61 */
62
63#define DEF_QUEUE_LIMIT 256u
64
c4307285
YH
65/*
66 * Link state events:
b97bf3fd
PL
67 */
68
69#define STARTING_EVT 856384768 /* link processing trigger */
70#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
71#define TIMEOUT_EVT 560817u /* link timer expired */
72
c4307285
YH
73/*
74 * The following two 'message types' is really just implementation
75 * data conveniently stored in the message header.
b97bf3fd
PL
76 * They must not be considered part of the protocol
77 */
78#define OPEN_MSG 0
79#define CLOSED_MSG 1
80
c4307285 81/*
b97bf3fd
PL
82 * State value stored in 'exp_msg_count'
83 */
84
85#define START_CHANGEOVER 100000u
86
87/**
88 * struct link_name - deconstructed link name
89 * @addr_local: network address of node at this end
90 * @if_local: name of interface at this end
91 * @addr_peer: network address of node at far end
92 * @if_peer: name of interface at far end
93 */
94
95struct link_name {
96 u32 addr_local;
97 char if_local[TIPC_MAX_IF_NAME];
98 u32 addr_peer;
99 char if_peer[TIPC_MAX_IF_NAME];
100};
101
102#if 0
103
104/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
105
c4307285 106/**
b97bf3fd
PL
107 * struct link_event - link up/down event notification
108 */
109
110struct link_event {
111 u32 addr;
112 int up;
113 void (*fcn)(u32, char *, int);
114 char name[TIPC_MAX_LINK_NAME];
115};
116
117#endif
118
119static void link_handle_out_of_seq_msg(struct link *l_ptr,
120 struct sk_buff *buf);
121static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
122static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
123static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
124static int link_send_sections_long(struct port *sender,
125 struct iovec const *msg_sect,
126 u32 num_sect, u32 destnode);
127static void link_check_defragm_bufs(struct link *l_ptr);
128static void link_state_event(struct link *l_ptr, u32 event);
129static void link_reset_statistics(struct link *l_ptr);
c4307285 130static void link_print(struct link *l_ptr, struct print_buf *buf,
b97bf3fd
PL
131 const char *str);
132
133/*
134 * Debugging code used by link routines only
135 *
136 * When debugging link problems on a system that has multiple links,
137 * the standard TIPC debugging routines may not be useful since they
138 * allow the output from multiple links to be intermixed. For this reason
139 * routines of the form "dbg_link_XXX()" have been created that will capture
140 * debug info into a link's personal print buffer, which can then be dumped
a3df92c7 141 * into the TIPC system log (TIPC_LOG) upon request.
b97bf3fd
PL
142 *
143 * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size
144 * of the print buffer used by each link. If LINK_LOG_BUF_SIZE is set to 0,
c4307285 145 * the dbg_link_XXX() routines simply send their output to the standard
b97bf3fd
PL
146 * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful
147 * when there is only a single link in the system being debugged.
148 *
149 * Notes:
a3df92c7 150 * - When enabled, LINK_LOG_BUF_SIZE should be set to at least TIPC_PB_MIN_SIZE
c4307285 151 * - "l_ptr" must be valid when using dbg_link_XXX() macros
b97bf3fd
PL
152 */
153
154#define LINK_LOG_BUF_SIZE 0
155
48c97139
AS
156#define dbg_link(fmt, arg...) \
157 do { \
158 if (LINK_LOG_BUF_SIZE) \
159 tipc_printf(&l_ptr->print_buf, fmt, ## arg); \
160 } while (0)
161#define dbg_link_msg(msg, txt) \
162 do { \
163 if (LINK_LOG_BUF_SIZE) \
164 tipc_msg_dbg(&l_ptr->print_buf, msg, txt); \
165 } while (0)
166#define dbg_link_state(txt) \
167 do { \
168 if (LINK_LOG_BUF_SIZE) \
169 link_print(l_ptr, &l_ptr->print_buf, txt); \
170 } while (0)
b97bf3fd
PL
171#define dbg_link_dump() do { \
172 if (LINK_LOG_BUF_SIZE) { \
173 tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \
4323add6 174 tipc_printbuf_move(LOG, &l_ptr->print_buf); \
b97bf3fd
PL
175 } \
176} while (0)
177
05790c64 178static void dbg_print_link(struct link *l_ptr, const char *str)
b97bf3fd 179{
a3df92c7 180 if (DBG_OUTPUT != TIPC_NULL)
b97bf3fd
PL
181 link_print(l_ptr, DBG_OUTPUT, str);
182}
183
05790c64 184static void dbg_print_buf_chain(struct sk_buff *root_buf)
b97bf3fd 185{
a3df92c7 186 if (DBG_OUTPUT != TIPC_NULL) {
b97bf3fd
PL
187 struct sk_buff *buf = root_buf;
188
189 while (buf) {
190 msg_dbg(buf_msg(buf), "In chain: ");
191 buf = buf->next;
192 }
193 }
194}
195
196/*
05790c64 197 * Simple link routines
b97bf3fd
PL
198 */
199
05790c64 200static unsigned int align(unsigned int i)
b97bf3fd
PL
201{
202 return (i + 3) & ~3u;
203}
204
05790c64 205static void link_init_max_pkt(struct link *l_ptr)
b97bf3fd
PL
206{
207 u32 max_pkt;
c4307285 208
b97bf3fd
PL
209 max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
210 if (max_pkt > MAX_MSG_SIZE)
211 max_pkt = MAX_MSG_SIZE;
212
c4307285 213 l_ptr->max_pkt_target = max_pkt;
b97bf3fd
PL
214 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
215 l_ptr->max_pkt = l_ptr->max_pkt_target;
c4307285 216 else
b97bf3fd
PL
217 l_ptr->max_pkt = MAX_PKT_DEFAULT;
218
c4307285 219 l_ptr->max_pkt_probes = 0;
b97bf3fd
PL
220}
221
05790c64 222static u32 link_next_sent(struct link *l_ptr)
b97bf3fd
PL
223{
224 if (l_ptr->next_out)
225 return msg_seqno(buf_msg(l_ptr->next_out));
226 return mod(l_ptr->next_out_no);
227}
228
05790c64 229static u32 link_last_sent(struct link *l_ptr)
b97bf3fd
PL
230{
231 return mod(link_next_sent(l_ptr) - 1);
232}
233
234/*
05790c64 235 * Simple non-static link routines (i.e. referenced outside this file)
b97bf3fd
PL
236 */
237
4323add6 238int tipc_link_is_up(struct link *l_ptr)
b97bf3fd
PL
239{
240 if (!l_ptr)
241 return 0;
242 return (link_working_working(l_ptr) || link_working_unknown(l_ptr));
243}
244
4323add6 245int tipc_link_is_active(struct link *l_ptr)
b97bf3fd
PL
246{
247 return ((l_ptr->owner->active_links[0] == l_ptr) ||
248 (l_ptr->owner->active_links[1] == l_ptr));
249}
250
251/**
252 * link_name_validate - validate & (optionally) deconstruct link name
253 * @name - ptr to link name string
254 * @name_parts - ptr to area for link name components (or NULL if not needed)
c4307285 255 *
b97bf3fd
PL
256 * Returns 1 if link name is valid, otherwise 0.
257 */
258
259static int link_name_validate(const char *name, struct link_name *name_parts)
260{
261 char name_copy[TIPC_MAX_LINK_NAME];
262 char *addr_local;
263 char *if_local;
264 char *addr_peer;
265 char *if_peer;
266 char dummy;
267 u32 z_local, c_local, n_local;
268 u32 z_peer, c_peer, n_peer;
269 u32 if_local_len;
270 u32 if_peer_len;
271
272 /* copy link name & ensure length is OK */
273
274 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
275 /* need above in case non-Posix strncpy() doesn't pad with nulls */
276 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
277 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
278 return 0;
279
280 /* ensure all component parts of link name are present */
281
282 addr_local = name_copy;
283 if ((if_local = strchr(addr_local, ':')) == NULL)
284 return 0;
285 *(if_local++) = 0;
286 if ((addr_peer = strchr(if_local, '-')) == NULL)
287 return 0;
288 *(addr_peer++) = 0;
289 if_local_len = addr_peer - if_local;
290 if ((if_peer = strchr(addr_peer, ':')) == NULL)
291 return 0;
292 *(if_peer++) = 0;
293 if_peer_len = strlen(if_peer) + 1;
294
295 /* validate component parts of link name */
296
297 if ((sscanf(addr_local, "%u.%u.%u%c",
298 &z_local, &c_local, &n_local, &dummy) != 3) ||
299 (sscanf(addr_peer, "%u.%u.%u%c",
300 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
301 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
302 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
c4307285
YH
303 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
304 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
b97bf3fd
PL
305 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
306 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
307 return 0;
308
309 /* return link name components, if necessary */
310
311 if (name_parts) {
312 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
313 strcpy(name_parts->if_local, if_local);
314 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
315 strcpy(name_parts->if_peer, if_peer);
316 }
317 return 1;
318}
319
320/**
321 * link_timeout - handle expiration of link timer
322 * @l_ptr: pointer to link
c4307285 323 *
4323add6
PL
324 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
325 * with tipc_link_delete(). (There is no risk that the node will be deleted by
326 * another thread because tipc_link_delete() always cancels the link timer before
327 * tipc_node_delete() is called.)
b97bf3fd
PL
328 */
329
330static void link_timeout(struct link *l_ptr)
331{
4323add6 332 tipc_node_lock(l_ptr->owner);
b97bf3fd
PL
333
334 /* update counters used in statistical profiling of send traffic */
335
336 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
337 l_ptr->stats.queue_sz_counts++;
338
339 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
340 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
341
342 if (l_ptr->first_out) {
343 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
344 u32 length = msg_size(msg);
345
f64f9e71
JP
346 if ((msg_user(msg) == MSG_FRAGMENTER) &&
347 (msg_type(msg) == FIRST_FRAGMENT)) {
b97bf3fd
PL
348 length = msg_size(msg_get_wrapped(msg));
349 }
350 if (length) {
351 l_ptr->stats.msg_lengths_total += length;
352 l_ptr->stats.msg_length_counts++;
353 if (length <= 64)
354 l_ptr->stats.msg_length_profile[0]++;
355 else if (length <= 256)
356 l_ptr->stats.msg_length_profile[1]++;
357 else if (length <= 1024)
358 l_ptr->stats.msg_length_profile[2]++;
359 else if (length <= 4096)
360 l_ptr->stats.msg_length_profile[3]++;
361 else if (length <= 16384)
362 l_ptr->stats.msg_length_profile[4]++;
363 else if (length <= 32768)
364 l_ptr->stats.msg_length_profile[5]++;
365 else
366 l_ptr->stats.msg_length_profile[6]++;
367 }
368 }
369
370 /* do all other link processing performed on a periodic basis */
371
372 link_check_defragm_bufs(l_ptr);
373
374 link_state_event(l_ptr, TIMEOUT_EVT);
375
376 if (l_ptr->next_out)
4323add6 377 tipc_link_push_queue(l_ptr);
b97bf3fd 378
4323add6 379 tipc_node_unlock(l_ptr->owner);
b97bf3fd
PL
380}
381
05790c64 382static void link_set_timer(struct link *l_ptr, u32 time)
b97bf3fd
PL
383{
384 k_start_timer(&l_ptr->timer, time);
385}
386
387/**
4323add6 388 * tipc_link_create - create a new link
b97bf3fd
PL
389 * @b_ptr: pointer to associated bearer
390 * @peer: network address of node at other end of link
391 * @media_addr: media address to use when sending messages over link
c4307285 392 *
b97bf3fd
PL
393 * Returns pointer to link.
394 */
395
4323add6
PL
396struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
397 const struct tipc_media_addr *media_addr)
b97bf3fd
PL
398{
399 struct link *l_ptr;
400 struct tipc_msg *msg;
401 char *if_name;
402
0da974f4 403 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
b97bf3fd 404 if (!l_ptr) {
a10bd924 405 warn("Link creation failed, no memory\n");
b97bf3fd
PL
406 return NULL;
407 }
b97bf3fd 408
94571065
FW
409 if (LINK_LOG_BUF_SIZE) {
410 char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
411
412 if (!pb) {
413 kfree(l_ptr);
414 warn("Link creation failed, no memory for print buffer\n");
415 return NULL;
416 }
417 tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
418 }
419
b97bf3fd
PL
420 l_ptr->addr = peer;
421 if_name = strchr(b_ptr->publ.name, ':') + 1;
422 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
423 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
c4307285 424 tipc_node(tipc_own_addr),
b97bf3fd
PL
425 if_name,
426 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
427 /* note: peer i/f is appended to link name by reset/activate */
428 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
b97bf3fd
PL
429 l_ptr->checkpoint = 1;
430 l_ptr->b_ptr = b_ptr;
431 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
432 l_ptr->state = RESET_UNKNOWN;
433
434 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
435 msg = l_ptr->pmsg;
c68ca7b7 436 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd 437 msg_set_size(msg, sizeof(l_ptr->proto_msg));
a686e685 438 msg_set_session(msg, (tipc_random & 0xffff));
b97bf3fd
PL
439 msg_set_bearer_id(msg, b_ptr->identity);
440 strcpy((char *)msg_data(msg), if_name);
441
442 l_ptr->priority = b_ptr->priority;
4323add6 443 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
b97bf3fd
PL
444
445 link_init_max_pkt(l_ptr);
446
447 l_ptr->next_out_no = 1;
448 INIT_LIST_HEAD(&l_ptr->waiting_ports);
449
450 link_reset_statistics(l_ptr);
451
4323add6 452 l_ptr->owner = tipc_node_attach_link(l_ptr);
b97bf3fd 453 if (!l_ptr->owner) {
94571065
FW
454 if (LINK_LOG_BUF_SIZE)
455 kfree(l_ptr->print_buf.buf);
b97bf3fd
PL
456 kfree(l_ptr);
457 return NULL;
458 }
459
94571065
FW
460 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
461 list_add_tail(&l_ptr->link_list, &b_ptr->links);
4323add6 462 tipc_k_signal((Handler)tipc_link_start, (unsigned long)l_ptr);
b97bf3fd 463
4323add6 464 dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n",
b97bf3fd 465 l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit);
c4307285 466
b97bf3fd
PL
467 return l_ptr;
468}
469
c4307285 470/**
4323add6 471 * tipc_link_delete - delete a link
b97bf3fd 472 * @l_ptr: pointer to link
c4307285 473 *
4323add6 474 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
b97bf3fd 475 * This routine must not grab the node lock until after link timer cancellation
c4307285 476 * to avoid a potential deadlock situation.
b97bf3fd
PL
477 */
478
4323add6 479void tipc_link_delete(struct link *l_ptr)
b97bf3fd
PL
480{
481 if (!l_ptr) {
482 err("Attempt to delete non-existent link\n");
483 return;
484 }
485
4323add6 486 dbg("tipc_link_delete()\n");
b97bf3fd
PL
487
488 k_cancel_timer(&l_ptr->timer);
c4307285 489
4323add6
PL
490 tipc_node_lock(l_ptr->owner);
491 tipc_link_reset(l_ptr);
492 tipc_node_detach_link(l_ptr->owner, l_ptr);
493 tipc_link_stop(l_ptr);
b97bf3fd
PL
494 list_del_init(&l_ptr->link_list);
495 if (LINK_LOG_BUF_SIZE)
496 kfree(l_ptr->print_buf.buf);
4323add6 497 tipc_node_unlock(l_ptr->owner);
b97bf3fd
PL
498 k_term_timer(&l_ptr->timer);
499 kfree(l_ptr);
500}
501
4323add6 502void tipc_link_start(struct link *l_ptr)
b97bf3fd 503{
4323add6 504 dbg("tipc_link_start %x\n", l_ptr);
b97bf3fd
PL
505 link_state_event(l_ptr, STARTING_EVT);
506}
507
508/**
c4307285 509 * link_schedule_port - schedule port for deferred sending
b97bf3fd
PL
510 * @l_ptr: pointer to link
511 * @origport: reference to sending port
512 * @sz: amount of data to be sent
c4307285
YH
513 *
514 * Schedules port for renewed sending of messages after link congestion
b97bf3fd
PL
515 * has abated.
516 */
517
518static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
519{
520 struct port *p_ptr;
521
4323add6
PL
522 spin_lock_bh(&tipc_port_list_lock);
523 p_ptr = tipc_port_lock(origport);
b97bf3fd
PL
524 if (p_ptr) {
525 if (!p_ptr->wakeup)
526 goto exit;
527 if (!list_empty(&p_ptr->wait_list))
528 goto exit;
b97bf3fd 529 p_ptr->publ.congested = 1;
15e979da 530 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
b97bf3fd
PL
531 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
532 l_ptr->stats.link_congs++;
533exit:
4323add6 534 tipc_port_unlock(p_ptr);
b97bf3fd 535 }
4323add6 536 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
537 return -ELINKCONG;
538}
539
4323add6 540void tipc_link_wakeup_ports(struct link *l_ptr, int all)
b97bf3fd
PL
541{
542 struct port *p_ptr;
543 struct port *temp_p_ptr;
544 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
545
546 if (all)
547 win = 100000;
548 if (win <= 0)
549 return;
4323add6 550 if (!spin_trylock_bh(&tipc_port_list_lock))
b97bf3fd
PL
551 return;
552 if (link_congested(l_ptr))
553 goto exit;
c4307285 554 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
b97bf3fd
PL
555 wait_list) {
556 if (win <= 0)
557 break;
558 list_del_init(&p_ptr->wait_list);
b97bf3fd
PL
559 spin_lock_bh(p_ptr->publ.lock);
560 p_ptr->publ.congested = 0;
561 p_ptr->wakeup(&p_ptr->publ);
562 win -= p_ptr->waiting_pkts;
563 spin_unlock_bh(p_ptr->publ.lock);
564 }
565
566exit:
4323add6 567 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
568}
569
c4307285 570/**
b97bf3fd
PL
571 * link_release_outqueue - purge link's outbound message queue
572 * @l_ptr: pointer to link
573 */
574
575static void link_release_outqueue(struct link *l_ptr)
576{
577 struct sk_buff *buf = l_ptr->first_out;
578 struct sk_buff *next;
579
580 while (buf) {
581 next = buf->next;
582 buf_discard(buf);
583 buf = next;
584 }
585 l_ptr->first_out = NULL;
586 l_ptr->out_queue_size = 0;
587}
588
589/**
4323add6 590 * tipc_link_reset_fragments - purge link's inbound message fragments queue
b97bf3fd
PL
591 * @l_ptr: pointer to link
592 */
593
4323add6 594void tipc_link_reset_fragments(struct link *l_ptr)
b97bf3fd
PL
595{
596 struct sk_buff *buf = l_ptr->defragm_buf;
597 struct sk_buff *next;
598
599 while (buf) {
600 next = buf->next;
601 buf_discard(buf);
602 buf = next;
603 }
604 l_ptr->defragm_buf = NULL;
605}
606
c4307285 607/**
4323add6 608 * tipc_link_stop - purge all inbound and outbound messages associated with link
b97bf3fd
PL
609 * @l_ptr: pointer to link
610 */
611
4323add6 612void tipc_link_stop(struct link *l_ptr)
b97bf3fd
PL
613{
614 struct sk_buff *buf;
615 struct sk_buff *next;
616
617 buf = l_ptr->oldest_deferred_in;
618 while (buf) {
619 next = buf->next;
620 buf_discard(buf);
621 buf = next;
622 }
623
624 buf = l_ptr->first_out;
625 while (buf) {
626 next = buf->next;
627 buf_discard(buf);
628 buf = next;
629 }
630
4323add6 631 tipc_link_reset_fragments(l_ptr);
b97bf3fd
PL
632
633 buf_discard(l_ptr->proto_msg_queue);
634 l_ptr->proto_msg_queue = NULL;
635}
636
637#if 0
638
639/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
640
641static void link_recv_event(struct link_event *ev)
642{
643 ev->fcn(ev->addr, ev->name, ev->up);
644 kfree(ev);
645}
646
647static void link_send_event(void (*fcn)(u32 a, char *n, int up),
648 struct link *l_ptr, int up)
649{
650 struct link_event *ev;
c4307285 651
b97bf3fd
PL
652 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
653 if (!ev) {
654 warn("Link event allocation failure\n");
655 return;
656 }
657 ev->addr = l_ptr->addr;
658 ev->up = up;
659 ev->fcn = fcn;
660 memcpy(ev->name, l_ptr->name, TIPC_MAX_LINK_NAME);
4323add6 661 tipc_k_signal((Handler)link_recv_event, (unsigned long)ev);
b97bf3fd
PL
662}
663
664#else
665
666#define link_send_event(fcn, l_ptr, up) do { } while (0)
667
668#endif
669
4323add6 670void tipc_link_reset(struct link *l_ptr)
b97bf3fd
PL
671{
672 struct sk_buff *buf;
673 u32 prev_state = l_ptr->state;
674 u32 checkpoint = l_ptr->next_in_no;
5392d646 675 int was_active_link = tipc_link_is_active(l_ptr);
c4307285 676
a686e685 677 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
b97bf3fd 678
a686e685
AS
679 /* Link is down, accept any session */
680 l_ptr->peer_session = INVALID_SESSION;
b97bf3fd 681
c4307285 682 /* Prepare for max packet size negotiation */
b97bf3fd 683 link_init_max_pkt(l_ptr);
c4307285 684
b97bf3fd
PL
685 l_ptr->state = RESET_UNKNOWN;
686 dbg_link_state("Resetting Link\n");
687
688 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
689 return;
690
4323add6
PL
691 tipc_node_link_down(l_ptr->owner, l_ptr);
692 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
b97bf3fd 693#if 0
4323add6 694 tipc_printf(TIPC_CONS, "\nReset link <%s>\n", l_ptr->name);
b97bf3fd
PL
695 dbg_link_dump();
696#endif
5392d646 697 if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
b97bf3fd
PL
698 l_ptr->owner->permit_changeover) {
699 l_ptr->reset_checkpoint = checkpoint;
700 l_ptr->exp_msg_count = START_CHANGEOVER;
701 }
702
703 /* Clean up all queues: */
704
705 link_release_outqueue(l_ptr);
706 buf_discard(l_ptr->proto_msg_queue);
707 l_ptr->proto_msg_queue = NULL;
708 buf = l_ptr->oldest_deferred_in;
709 while (buf) {
710 struct sk_buff *next = buf->next;
711 buf_discard(buf);
712 buf = next;
713 }
714 if (!list_empty(&l_ptr->waiting_ports))
4323add6 715 tipc_link_wakeup_ports(l_ptr, 1);
b97bf3fd
PL
716
717 l_ptr->retransm_queue_head = 0;
718 l_ptr->retransm_queue_size = 0;
719 l_ptr->last_out = NULL;
720 l_ptr->first_out = NULL;
721 l_ptr->next_out = NULL;
722 l_ptr->unacked_window = 0;
723 l_ptr->checkpoint = 1;
724 l_ptr->next_out_no = 1;
725 l_ptr->deferred_inqueue_sz = 0;
726 l_ptr->oldest_deferred_in = NULL;
727 l_ptr->newest_deferred_in = NULL;
728 l_ptr->fsm_msg_cnt = 0;
729 l_ptr->stale_count = 0;
730 link_reset_statistics(l_ptr);
731
4323add6 732 link_send_event(tipc_cfg_link_event, l_ptr, 0);
b97bf3fd 733 if (!in_own_cluster(l_ptr->addr))
4323add6 734 link_send_event(tipc_disc_link_event, l_ptr, 0);
b97bf3fd
PL
735}
736
737
738static void link_activate(struct link *l_ptr)
739{
5392d646 740 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
4323add6
PL
741 tipc_node_link_up(l_ptr->owner, l_ptr);
742 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
743 link_send_event(tipc_cfg_link_event, l_ptr, 1);
b97bf3fd 744 if (!in_own_cluster(l_ptr->addr))
4323add6 745 link_send_event(tipc_disc_link_event, l_ptr, 1);
b97bf3fd
PL
746}
747
748/**
749 * link_state_event - link finite state machine
750 * @l_ptr: pointer to link
751 * @event: state machine event to process
752 */
753
754static void link_state_event(struct link *l_ptr, unsigned event)
755{
c4307285 756 struct link *other;
b97bf3fd
PL
757 u32 cont_intv = l_ptr->continuity_interval;
758
759 if (!l_ptr->started && (event != STARTING_EVT))
760 return; /* Not yet. */
761
762 if (link_blocked(l_ptr)) {
763 if (event == TIMEOUT_EVT) {
764 link_set_timer(l_ptr, cont_intv);
765 }
766 return; /* Changeover going on */
767 }
768 dbg_link("STATE_EV: <%s> ", l_ptr->name);
769
770 switch (l_ptr->state) {
771 case WORKING_WORKING:
772 dbg_link("WW/");
773 switch (event) {
774 case TRAFFIC_MSG_EVT:
775 dbg_link("TRF-");
776 /* fall through */
777 case ACTIVATE_MSG:
778 dbg_link("ACT\n");
779 break;
780 case TIMEOUT_EVT:
781 dbg_link("TIM ");
782 if (l_ptr->next_in_no != l_ptr->checkpoint) {
783 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6 784 if (tipc_bclink_acks_missing(l_ptr->owner)) {
c4307285 785 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
4323add6 786 0, 0, 0, 0, 0);
b97bf3fd
PL
787 l_ptr->fsm_msg_cnt++;
788 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
c4307285 789 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
4323add6 790 1, 0, 0, 0, 0);
b97bf3fd
PL
791 l_ptr->fsm_msg_cnt++;
792 }
793 link_set_timer(l_ptr, cont_intv);
794 break;
795 }
796 dbg_link(" -> WU\n");
797 l_ptr->state = WORKING_UNKNOWN;
798 l_ptr->fsm_msg_cnt = 0;
4323add6 799 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
800 l_ptr->fsm_msg_cnt++;
801 link_set_timer(l_ptr, cont_intv / 4);
802 break;
803 case RESET_MSG:
804 dbg_link("RES -> RR\n");
c4307285 805 info("Resetting link <%s>, requested by peer\n",
a10bd924 806 l_ptr->name);
4323add6 807 tipc_link_reset(l_ptr);
b97bf3fd
PL
808 l_ptr->state = RESET_RESET;
809 l_ptr->fsm_msg_cnt = 0;
4323add6 810 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
811 l_ptr->fsm_msg_cnt++;
812 link_set_timer(l_ptr, cont_intv);
813 break;
814 default:
815 err("Unknown link event %u in WW state\n", event);
816 }
817 break;
818 case WORKING_UNKNOWN:
819 dbg_link("WU/");
820 switch (event) {
821 case TRAFFIC_MSG_EVT:
822 dbg_link("TRF-");
823 case ACTIVATE_MSG:
824 dbg_link("ACT -> WW\n");
825 l_ptr->state = WORKING_WORKING;
826 l_ptr->fsm_msg_cnt = 0;
827 link_set_timer(l_ptr, cont_intv);
828 break;
829 case RESET_MSG:
830 dbg_link("RES -> RR\n");
a10bd924
AS
831 info("Resetting link <%s>, requested by peer "
832 "while probing\n", l_ptr->name);
4323add6 833 tipc_link_reset(l_ptr);
b97bf3fd
PL
834 l_ptr->state = RESET_RESET;
835 l_ptr->fsm_msg_cnt = 0;
4323add6 836 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
837 l_ptr->fsm_msg_cnt++;
838 link_set_timer(l_ptr, cont_intv);
839 break;
840 case TIMEOUT_EVT:
841 dbg_link("TIM ");
842 if (l_ptr->next_in_no != l_ptr->checkpoint) {
a570f095 843 dbg_link("-> WW\n");
b97bf3fd
PL
844 l_ptr->state = WORKING_WORKING;
845 l_ptr->fsm_msg_cnt = 0;
846 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6
PL
847 if (tipc_bclink_acks_missing(l_ptr->owner)) {
848 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
849 0, 0, 0, 0, 0);
b97bf3fd
PL
850 l_ptr->fsm_msg_cnt++;
851 }
852 link_set_timer(l_ptr, cont_intv);
853 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
854 dbg_link("Probing %u/%u,timer = %u ms)\n",
855 l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
856 cont_intv / 4);
c4307285 857 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
4323add6 858 1, 0, 0, 0, 0);
b97bf3fd
PL
859 l_ptr->fsm_msg_cnt++;
860 link_set_timer(l_ptr, cont_intv / 4);
861 } else { /* Link has failed */
862 dbg_link("-> RU (%u probes unanswered)\n",
863 l_ptr->fsm_msg_cnt);
a10bd924
AS
864 warn("Resetting link <%s>, peer not responding\n",
865 l_ptr->name);
4323add6 866 tipc_link_reset(l_ptr);
b97bf3fd
PL
867 l_ptr->state = RESET_UNKNOWN;
868 l_ptr->fsm_msg_cnt = 0;
4323add6
PL
869 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
870 0, 0, 0, 0, 0);
b97bf3fd
PL
871 l_ptr->fsm_msg_cnt++;
872 link_set_timer(l_ptr, cont_intv);
873 }
874 break;
875 default:
876 err("Unknown link event %u in WU state\n", event);
877 }
878 break;
879 case RESET_UNKNOWN:
880 dbg_link("RU/");
881 switch (event) {
882 case TRAFFIC_MSG_EVT:
883 dbg_link("TRF-\n");
884 break;
885 case ACTIVATE_MSG:
886 other = l_ptr->owner->active_links[0];
887 if (other && link_working_unknown(other)) {
888 dbg_link("ACT\n");
889 break;
890 }
891 dbg_link("ACT -> WW\n");
892 l_ptr->state = WORKING_WORKING;
893 l_ptr->fsm_msg_cnt = 0;
894 link_activate(l_ptr);
4323add6 895 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
896 l_ptr->fsm_msg_cnt++;
897 link_set_timer(l_ptr, cont_intv);
898 break;
899 case RESET_MSG:
a570f095 900 dbg_link("RES\n");
b97bf3fd
PL
901 dbg_link(" -> RR\n");
902 l_ptr->state = RESET_RESET;
903 l_ptr->fsm_msg_cnt = 0;
4323add6 904 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
905 l_ptr->fsm_msg_cnt++;
906 link_set_timer(l_ptr, cont_intv);
907 break;
908 case STARTING_EVT:
909 dbg_link("START-");
910 l_ptr->started = 1;
911 /* fall through */
912 case TIMEOUT_EVT:
a570f095 913 dbg_link("TIM\n");
4323add6 914 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
915 l_ptr->fsm_msg_cnt++;
916 link_set_timer(l_ptr, cont_intv);
917 break;
918 default:
919 err("Unknown link event %u in RU state\n", event);
920 }
921 break;
922 case RESET_RESET:
923 dbg_link("RR/ ");
924 switch (event) {
925 case TRAFFIC_MSG_EVT:
926 dbg_link("TRF-");
927 /* fall through */
928 case ACTIVATE_MSG:
929 other = l_ptr->owner->active_links[0];
930 if (other && link_working_unknown(other)) {
931 dbg_link("ACT\n");
932 break;
933 }
934 dbg_link("ACT -> WW\n");
935 l_ptr->state = WORKING_WORKING;
936 l_ptr->fsm_msg_cnt = 0;
937 link_activate(l_ptr);
4323add6 938 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
939 l_ptr->fsm_msg_cnt++;
940 link_set_timer(l_ptr, cont_intv);
941 break;
942 case RESET_MSG:
943 dbg_link("RES\n");
944 break;
945 case TIMEOUT_EVT:
946 dbg_link("TIM\n");
4323add6 947 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
948 l_ptr->fsm_msg_cnt++;
949 link_set_timer(l_ptr, cont_intv);
950 dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
951 break;
952 default:
953 err("Unknown link event %u in RR state\n", event);
954 }
955 break;
956 default:
957 err("Unknown link state %u/%u\n", l_ptr->state, event);
958 }
959}
960
961/*
962 * link_bundle_buf(): Append contents of a buffer to
c4307285 963 * the tail of an existing one.
b97bf3fd
PL
964 */
965
966static int link_bundle_buf(struct link *l_ptr,
c4307285 967 struct sk_buff *bundler,
b97bf3fd
PL
968 struct sk_buff *buf)
969{
970 struct tipc_msg *bundler_msg = buf_msg(bundler);
971 struct tipc_msg *msg = buf_msg(buf);
972 u32 size = msg_size(msg);
e49060c7
AS
973 u32 bundle_size = msg_size(bundler_msg);
974 u32 to_pos = align(bundle_size);
975 u32 pad = to_pos - bundle_size;
b97bf3fd
PL
976
977 if (msg_user(bundler_msg) != MSG_BUNDLER)
978 return 0;
979 if (msg_type(bundler_msg) != OPEN_MSG)
980 return 0;
e49060c7 981 if (skb_tailroom(bundler) < (pad + size))
b97bf3fd 982 return 0;
15e979da 983 if (l_ptr->max_pkt < (to_pos + size))
863fae66 984 return 0;
b97bf3fd 985
e49060c7 986 skb_put(bundler, pad + size);
27d7ff46 987 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
b97bf3fd
PL
988 msg_set_size(bundler_msg, to_pos + size);
989 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
990 dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
991 msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
992 msg_dbg(msg, "PACKD:");
993 buf_discard(buf);
994 l_ptr->stats.sent_bundled++;
995 return 1;
996}
997
05790c64
SR
998static void link_add_to_outqueue(struct link *l_ptr,
999 struct sk_buff *buf,
1000 struct tipc_msg *msg)
b97bf3fd
PL
1001{
1002 u32 ack = mod(l_ptr->next_in_no - 1);
1003 u32 seqno = mod(l_ptr->next_out_no++);
1004
1005 msg_set_word(msg, 2, ((ack << 16) | seqno));
1006 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1007 buf->next = NULL;
1008 if (l_ptr->first_out) {
1009 l_ptr->last_out->next = buf;
1010 l_ptr->last_out = buf;
1011 } else
1012 l_ptr->first_out = l_ptr->last_out = buf;
1013 l_ptr->out_queue_size++;
1014}
1015
c4307285
YH
1016/*
1017 * tipc_link_send_buf() is the 'full path' for messages, called from
b97bf3fd
PL
1018 * inside TIPC when the 'fast path' in tipc_send_buf
1019 * has failed, and from link_send()
1020 */
1021
4323add6 1022int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
b97bf3fd
PL
1023{
1024 struct tipc_msg *msg = buf_msg(buf);
1025 u32 size = msg_size(msg);
1026 u32 dsz = msg_data_sz(msg);
1027 u32 queue_size = l_ptr->out_queue_size;
c68ca7b7 1028 u32 imp = tipc_msg_tot_importance(msg);
b97bf3fd 1029 u32 queue_limit = l_ptr->queue_limit[imp];
15e979da 1030 u32 max_packet = l_ptr->max_pkt;
b97bf3fd
PL
1031
1032 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
1033
1034 /* Match msg importance against queue limits: */
1035
1036 if (unlikely(queue_size >= queue_limit)) {
1037 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
1038 return link_schedule_port(l_ptr, msg_origport(msg),
1039 size);
1040 }
1041 msg_dbg(msg, "TIPC: Congestion, throwing away\n");
1042 buf_discard(buf);
1043 if (imp > CONN_MANAGER) {
a10bd924 1044 warn("Resetting link <%s>, send queue full", l_ptr->name);
4323add6 1045 tipc_link_reset(l_ptr);
b97bf3fd
PL
1046 }
1047 return dsz;
1048 }
1049
1050 /* Fragmentation needed ? */
1051
1052 if (size > max_packet)
4323add6 1053 return tipc_link_send_long_buf(l_ptr, buf);
b97bf3fd
PL
1054
1055 /* Packet can be queued or sent: */
1056
1057 if (queue_size > l_ptr->stats.max_queue_sz)
1058 l_ptr->stats.max_queue_sz = queue_size;
1059
c4307285 1060 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
b97bf3fd
PL
1061 !link_congested(l_ptr))) {
1062 link_add_to_outqueue(l_ptr, buf, msg);
1063
4323add6 1064 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
b97bf3fd
PL
1065 l_ptr->unacked_window = 0;
1066 } else {
4323add6 1067 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
b97bf3fd
PL
1068 l_ptr->stats.bearer_congs++;
1069 l_ptr->next_out = buf;
1070 }
1071 return dsz;
1072 }
1073 /* Congestion: can message be bundled ?: */
1074
1075 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
1076 (msg_user(msg) != MSG_FRAGMENTER)) {
1077
1078 /* Try adding message to an existing bundle */
1079
c4307285 1080 if (l_ptr->next_out &&
b97bf3fd 1081 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
4323add6 1082 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
b97bf3fd
PL
1083 return dsz;
1084 }
1085
1086 /* Try creating a new bundle */
1087
1088 if (size <= max_packet * 2 / 3) {
1089 struct sk_buff *bundler = buf_acquire(max_packet);
1090 struct tipc_msg bundler_hdr;
1091
1092 if (bundler) {
c68ca7b7 1093 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
75715217 1094 INT_H_SIZE, l_ptr->addr);
27d7ff46
ACM
1095 skb_copy_to_linear_data(bundler, &bundler_hdr,
1096 INT_H_SIZE);
b97bf3fd
PL
1097 skb_trim(bundler, INT_H_SIZE);
1098 link_bundle_buf(l_ptr, bundler, buf);
1099 buf = bundler;
1100 msg = buf_msg(buf);
1101 l_ptr->stats.sent_bundles++;
1102 }
1103 }
1104 }
1105 if (!l_ptr->next_out)
1106 l_ptr->next_out = buf;
1107 link_add_to_outqueue(l_ptr, buf, msg);
4323add6 1108 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
b97bf3fd
PL
1109 return dsz;
1110}
1111
c4307285
YH
1112/*
1113 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
b97bf3fd
PL
1114 * not been selected yet, and the the owner node is not locked
1115 * Called by TIPC internal users, e.g. the name distributor
1116 */
1117
4323add6 1118int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
b97bf3fd
PL
1119{
1120 struct link *l_ptr;
6c00055a 1121 struct tipc_node *n_ptr;
b97bf3fd
PL
1122 int res = -ELINKCONG;
1123
4323add6
PL
1124 read_lock_bh(&tipc_net_lock);
1125 n_ptr = tipc_node_select(dest, selector);
b97bf3fd 1126 if (n_ptr) {
4323add6 1127 tipc_node_lock(n_ptr);
b97bf3fd 1128 l_ptr = n_ptr->active_links[selector & 1];
b97bf3fd 1129 if (l_ptr) {
c33d53b2 1130 dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
4323add6 1131 res = tipc_link_send_buf(l_ptr, buf);
c33d53b2
AS
1132 } else {
1133 dbg("Attempt to send msg to unreachable node:\n");
1134 msg_dbg(buf_msg(buf),">>>");
1135 buf_discard(buf);
b97bf3fd 1136 }
4323add6 1137 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1138 } else {
1139 dbg("Attempt to send msg to unknown node:\n");
1140 msg_dbg(buf_msg(buf),">>>");
1141 buf_discard(buf);
1142 }
4323add6 1143 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1144 return res;
1145}
1146
c4307285
YH
1147/*
1148 * link_send_buf_fast: Entry for data messages where the
b97bf3fd
PL
1149 * destination link is known and the header is complete,
1150 * inclusive total message length. Very time critical.
1151 * Link is locked. Returns user data length.
1152 */
1153
05790c64
SR
1154static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
1155 u32 *used_max_pkt)
b97bf3fd
PL
1156{
1157 struct tipc_msg *msg = buf_msg(buf);
1158 int res = msg_data_sz(msg);
1159
1160 if (likely(!link_congested(l_ptr))) {
15e979da 1161 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
b97bf3fd
PL
1162 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
1163 link_add_to_outqueue(l_ptr, buf, msg);
4323add6
PL
1164 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
1165 &l_ptr->media_addr))) {
b97bf3fd
PL
1166 l_ptr->unacked_window = 0;
1167 msg_dbg(msg,"SENT_FAST:");
1168 return res;
1169 }
1170 dbg("failed sent fast...\n");
4323add6 1171 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
b97bf3fd
PL
1172 l_ptr->stats.bearer_congs++;
1173 l_ptr->next_out = buf;
1174 return res;
1175 }
1176 }
1177 else
15e979da 1178 *used_max_pkt = l_ptr->max_pkt;
b97bf3fd 1179 }
4323add6 1180 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
b97bf3fd
PL
1181}
1182
c4307285
YH
1183/*
1184 * tipc_send_buf_fast: Entry for data messages where the
b97bf3fd
PL
1185 * destination node is known and the header is complete,
1186 * inclusive total message length.
1187 * Returns user data length.
1188 */
1189int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
1190{
1191 struct link *l_ptr;
6c00055a 1192 struct tipc_node *n_ptr;
b97bf3fd
PL
1193 int res;
1194 u32 selector = msg_origport(buf_msg(buf)) & 1;
1195 u32 dummy;
1196
1197 if (destnode == tipc_own_addr)
4323add6 1198 return tipc_port_recv_msg(buf);
b97bf3fd 1199
4323add6
PL
1200 read_lock_bh(&tipc_net_lock);
1201 n_ptr = tipc_node_select(destnode, selector);
b97bf3fd 1202 if (likely(n_ptr)) {
4323add6 1203 tipc_node_lock(n_ptr);
b97bf3fd
PL
1204 l_ptr = n_ptr->active_links[selector];
1205 dbg("send_fast: buf %x selected %x, destnode = %x\n",
1206 buf, l_ptr, destnode);
1207 if (likely(l_ptr)) {
1208 res = link_send_buf_fast(l_ptr, buf, &dummy);
4323add6
PL
1209 tipc_node_unlock(n_ptr);
1210 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1211 return res;
1212 }
4323add6 1213 tipc_node_unlock(n_ptr);
b97bf3fd 1214 }
4323add6 1215 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1216 res = msg_data_sz(buf_msg(buf));
1217 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1218 return res;
1219}
1220
1221
c4307285
YH
1222/*
1223 * tipc_link_send_sections_fast: Entry for messages where the
b97bf3fd 1224 * destination processor is known and the header is complete,
c4307285 1225 * except for total message length.
b97bf3fd
PL
1226 * Returns user data length or errno.
1227 */
c4307285 1228int tipc_link_send_sections_fast(struct port *sender,
4323add6 1229 struct iovec const *msg_sect,
c4307285 1230 const u32 num_sect,
4323add6 1231 u32 destaddr)
b97bf3fd
PL
1232{
1233 struct tipc_msg *hdr = &sender->publ.phdr;
1234 struct link *l_ptr;
1235 struct sk_buff *buf;
6c00055a 1236 struct tipc_node *node;
b97bf3fd
PL
1237 int res;
1238 u32 selector = msg_origport(hdr) & 1;
1239
b97bf3fd
PL
1240again:
1241 /*
1242 * Try building message using port's max_pkt hint.
1243 * (Must not hold any locks while building message.)
1244 */
1245
c68ca7b7 1246 res = tipc_msg_build(hdr, msg_sect, num_sect, sender->publ.max_pkt,
b97bf3fd
PL
1247 !sender->user_port, &buf);
1248
4323add6
PL
1249 read_lock_bh(&tipc_net_lock);
1250 node = tipc_node_select(destaddr, selector);
b97bf3fd 1251 if (likely(node)) {
4323add6 1252 tipc_node_lock(node);
b97bf3fd
PL
1253 l_ptr = node->active_links[selector];
1254 if (likely(l_ptr)) {
1255 if (likely(buf)) {
1256 res = link_send_buf_fast(l_ptr, buf,
05646c91 1257 &sender->publ.max_pkt);
b97bf3fd
PL
1258 if (unlikely(res < 0))
1259 buf_discard(buf);
1260exit:
4323add6
PL
1261 tipc_node_unlock(node);
1262 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1263 return res;
1264 }
1265
1266 /* Exit if build request was invalid */
1267
1268 if (unlikely(res < 0))
1269 goto exit;
1270
1271 /* Exit if link (or bearer) is congested */
1272
c4307285 1273 if (link_congested(l_ptr) ||
b97bf3fd
PL
1274 !list_empty(&l_ptr->b_ptr->cong_links)) {
1275 res = link_schedule_port(l_ptr,
1276 sender->publ.ref, res);
1277 goto exit;
1278 }
1279
c4307285 1280 /*
b97bf3fd
PL
1281 * Message size exceeds max_pkt hint; update hint,
1282 * then re-try fast path or fragment the message
1283 */
1284
15e979da 1285 sender->publ.max_pkt = l_ptr->max_pkt;
4323add6
PL
1286 tipc_node_unlock(node);
1287 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1288
1289
05646c91 1290 if ((msg_hdr_sz(hdr) + res) <= sender->publ.max_pkt)
b97bf3fd
PL
1291 goto again;
1292
1293 return link_send_sections_long(sender, msg_sect,
1294 num_sect, destaddr);
1295 }
4323add6 1296 tipc_node_unlock(node);
b97bf3fd 1297 }
4323add6 1298 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1299
1300 /* Couldn't find a link to the destination node */
1301
1302 if (buf)
1303 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1304 if (res >= 0)
4323add6
PL
1305 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1306 TIPC_ERR_NO_NODE);
b97bf3fd
PL
1307 return res;
1308}
1309
c4307285
YH
1310/*
1311 * link_send_sections_long(): Entry for long messages where the
b97bf3fd 1312 * destination node is known and the header is complete,
c4307285 1313 * inclusive total message length.
b97bf3fd
PL
1314 * Link and bearer congestion status have been checked to be ok,
1315 * and are ignored if they change.
1316 *
1317 * Note that fragments do not use the full link MTU so that they won't have
1318 * to undergo refragmentation if link changeover causes them to be sent
1319 * over another link with an additional tunnel header added as prefix.
1320 * (Refragmentation will still occur if the other link has a smaller MTU.)
1321 *
1322 * Returns user data length or errno.
1323 */
1324static int link_send_sections_long(struct port *sender,
1325 struct iovec const *msg_sect,
1326 u32 num_sect,
1327 u32 destaddr)
1328{
1329 struct link *l_ptr;
6c00055a 1330 struct tipc_node *node;
b97bf3fd
PL
1331 struct tipc_msg *hdr = &sender->publ.phdr;
1332 u32 dsz = msg_data_sz(hdr);
1333 u32 max_pkt,fragm_sz,rest;
1334 struct tipc_msg fragm_hdr;
1335 struct sk_buff *buf,*buf_chain,*prev;
1336 u32 fragm_crs,fragm_rest,hsz,sect_rest;
1337 const unchar *sect_crs;
1338 int curr_sect;
1339 u32 fragm_no;
1340
1341again:
1342 fragm_no = 1;
05646c91 1343 max_pkt = sender->publ.max_pkt - INT_H_SIZE;
b97bf3fd 1344 /* leave room for tunnel header in case of link changeover */
c4307285 1345 fragm_sz = max_pkt - INT_H_SIZE;
b97bf3fd
PL
1346 /* leave room for fragmentation header in each fragment */
1347 rest = dsz;
1348 fragm_crs = 0;
1349 fragm_rest = 0;
1350 sect_rest = 0;
1fc54d8f 1351 sect_crs = NULL;
b97bf3fd
PL
1352 curr_sect = -1;
1353
1354 /* Prepare reusable fragment header: */
1355
1356 msg_dbg(hdr, ">FRAGMENTING>");
c68ca7b7 1357 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
75715217 1358 INT_H_SIZE, msg_destnode(hdr));
b97bf3fd
PL
1359 msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1360 msg_set_size(&fragm_hdr, max_pkt);
1361 msg_set_fragm_no(&fragm_hdr, 1);
1362
1363 /* Prepare header of first fragment: */
1364
1365 buf_chain = buf = buf_acquire(max_pkt);
1366 if (!buf)
1367 return -ENOMEM;
1368 buf->next = NULL;
27d7ff46 1369 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
b97bf3fd 1370 hsz = msg_hdr_sz(hdr);
27d7ff46 1371 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
b97bf3fd
PL
1372 msg_dbg(buf_msg(buf), ">BUILD>");
1373
1374 /* Chop up message: */
1375
1376 fragm_crs = INT_H_SIZE + hsz;
1377 fragm_rest = fragm_sz - hsz;
1378
1379 do { /* For all sections */
1380 u32 sz;
1381
1382 if (!sect_rest) {
1383 sect_rest = msg_sect[++curr_sect].iov_len;
1384 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1385 }
1386
1387 if (sect_rest < fragm_rest)
1388 sz = sect_rest;
1389 else
1390 sz = fragm_rest;
1391
1392 if (likely(!sender->user_port)) {
1393 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1394error:
1395 for (; buf_chain; buf_chain = buf) {
1396 buf = buf_chain->next;
1397 buf_discard(buf_chain);
1398 }
1399 return -EFAULT;
1400 }
1401 } else
27d7ff46
ACM
1402 skb_copy_to_linear_data_offset(buf, fragm_crs,
1403 sect_crs, sz);
b97bf3fd
PL
1404 sect_crs += sz;
1405 sect_rest -= sz;
1406 fragm_crs += sz;
1407 fragm_rest -= sz;
1408 rest -= sz;
1409
1410 if (!fragm_rest && rest) {
1411
1412 /* Initiate new fragment: */
1413 if (rest <= fragm_sz) {
1414 fragm_sz = rest;
1415 msg_set_type(&fragm_hdr,LAST_FRAGMENT);
1416 } else {
1417 msg_set_type(&fragm_hdr, FRAGMENT);
1418 }
1419 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1420 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1421 prev = buf;
1422 buf = buf_acquire(fragm_sz + INT_H_SIZE);
1423 if (!buf)
1424 goto error;
1425
c4307285 1426 buf->next = NULL;
b97bf3fd 1427 prev->next = buf;
27d7ff46 1428 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
b97bf3fd
PL
1429 fragm_crs = INT_H_SIZE;
1430 fragm_rest = fragm_sz;
1431 msg_dbg(buf_msg(buf)," >BUILD>");
1432 }
1433 }
1434 while (rest > 0);
1435
c4307285 1436 /*
b97bf3fd
PL
1437 * Now we have a buffer chain. Select a link and check
1438 * that packet size is still OK
1439 */
4323add6 1440 node = tipc_node_select(destaddr, sender->publ.ref & 1);
b97bf3fd 1441 if (likely(node)) {
4323add6 1442 tipc_node_lock(node);
b97bf3fd
PL
1443 l_ptr = node->active_links[sender->publ.ref & 1];
1444 if (!l_ptr) {
4323add6 1445 tipc_node_unlock(node);
b97bf3fd
PL
1446 goto reject;
1447 }
15e979da
AS
1448 if (l_ptr->max_pkt < max_pkt) {
1449 sender->publ.max_pkt = l_ptr->max_pkt;
4323add6 1450 tipc_node_unlock(node);
b97bf3fd
PL
1451 for (; buf_chain; buf_chain = buf) {
1452 buf = buf_chain->next;
1453 buf_discard(buf_chain);
1454 }
1455 goto again;
1456 }
1457 } else {
1458reject:
1459 for (; buf_chain; buf_chain = buf) {
1460 buf = buf_chain->next;
1461 buf_discard(buf_chain);
1462 }
4323add6
PL
1463 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1464 TIPC_ERR_NO_NODE);
b97bf3fd
PL
1465 }
1466
1467 /* Append whole chain to send queue: */
1468
1469 buf = buf_chain;
1470 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1471 if (!l_ptr->next_out)
1472 l_ptr->next_out = buf_chain;
1473 l_ptr->stats.sent_fragmented++;
1474 while (buf) {
1475 struct sk_buff *next = buf->next;
1476 struct tipc_msg *msg = buf_msg(buf);
1477
1478 l_ptr->stats.sent_fragments++;
1479 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1480 link_add_to_outqueue(l_ptr, buf, msg);
1481 msg_dbg(msg, ">ADD>");
1482 buf = next;
1483 }
1484
1485 /* Send it, if possible: */
1486
4323add6
PL
1487 tipc_link_push_queue(l_ptr);
1488 tipc_node_unlock(node);
b97bf3fd
PL
1489 return dsz;
1490}
1491
c4307285 1492/*
4323add6 1493 * tipc_link_push_packet: Push one unsent packet to the media
b97bf3fd 1494 */
4323add6 1495u32 tipc_link_push_packet(struct link *l_ptr)
b97bf3fd
PL
1496{
1497 struct sk_buff *buf = l_ptr->first_out;
1498 u32 r_q_size = l_ptr->retransm_queue_size;
1499 u32 r_q_head = l_ptr->retransm_queue_head;
1500
1501 /* Step to position where retransmission failed, if any, */
1502 /* consider that buffers may have been released in meantime */
1503
1504 if (r_q_size && buf) {
c4307285 1505 u32 last = lesser(mod(r_q_head + r_q_size),
b97bf3fd
PL
1506 link_last_sent(l_ptr));
1507 u32 first = msg_seqno(buf_msg(buf));
1508
1509 while (buf && less(first, r_q_head)) {
1510 first = mod(first + 1);
1511 buf = buf->next;
1512 }
1513 l_ptr->retransm_queue_head = r_q_head = first;
1514 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1515 }
1516
1517 /* Continue retransmission now, if there is anything: */
1518
ca509101 1519 if (r_q_size && buf) {
b97bf3fd 1520 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
c4307285 1521 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
4323add6 1522 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
b97bf3fd
PL
1523 msg_dbg(buf_msg(buf), ">DEF-RETR>");
1524 l_ptr->retransm_queue_head = mod(++r_q_head);
1525 l_ptr->retransm_queue_size = --r_q_size;
1526 l_ptr->stats.retransmitted++;
0e35fd5e 1527 return 0;
b97bf3fd
PL
1528 } else {
1529 l_ptr->stats.bearer_congs++;
1530 msg_dbg(buf_msg(buf), "|>DEF-RETR>");
1531 return PUSH_FAILED;
1532 }
1533 }
1534
1535 /* Send deferred protocol message, if any: */
1536
1537 buf = l_ptr->proto_msg_queue;
1538 if (buf) {
1539 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
c4307285 1540 msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
4323add6 1541 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
b97bf3fd
PL
1542 msg_dbg(buf_msg(buf), ">DEF-PROT>");
1543 l_ptr->unacked_window = 0;
1544 buf_discard(buf);
1fc54d8f 1545 l_ptr->proto_msg_queue = NULL;
0e35fd5e 1546 return 0;
b97bf3fd
PL
1547 } else {
1548 msg_dbg(buf_msg(buf), "|>DEF-PROT>");
1549 l_ptr->stats.bearer_congs++;
1550 return PUSH_FAILED;
1551 }
1552 }
1553
1554 /* Send one deferred data message, if send window not full: */
1555
1556 buf = l_ptr->next_out;
1557 if (buf) {
1558 struct tipc_msg *msg = buf_msg(buf);
1559 u32 next = msg_seqno(msg);
1560 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1561
1562 if (mod(next - first) < l_ptr->queue_limit[0]) {
1563 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 1564 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
4323add6 1565 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
b97bf3fd
PL
1566 if (msg_user(msg) == MSG_BUNDLER)
1567 msg_set_type(msg, CLOSED_MSG);
1568 msg_dbg(msg, ">PUSH-DATA>");
1569 l_ptr->next_out = buf->next;
0e35fd5e 1570 return 0;
b97bf3fd
PL
1571 } else {
1572 msg_dbg(msg, "|PUSH-DATA|");
1573 l_ptr->stats.bearer_congs++;
1574 return PUSH_FAILED;
1575 }
1576 }
1577 }
1578 return PUSH_FINISHED;
1579}
1580
1581/*
1582 * push_queue(): push out the unsent messages of a link where
1583 * congestion has abated. Node is locked
1584 */
4323add6 1585void tipc_link_push_queue(struct link *l_ptr)
b97bf3fd
PL
1586{
1587 u32 res;
1588
4323add6 1589 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
b97bf3fd
PL
1590 return;
1591
1592 do {
4323add6 1593 res = tipc_link_push_packet(l_ptr);
0e35fd5e
AS
1594 } while (!res);
1595
b97bf3fd 1596 if (res == PUSH_FAILED)
4323add6 1597 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
b97bf3fd
PL
1598}
1599
d356eeba
AS
1600static void link_reset_all(unsigned long addr)
1601{
6c00055a 1602 struct tipc_node *n_ptr;
d356eeba
AS
1603 char addr_string[16];
1604 u32 i;
1605
1606 read_lock_bh(&tipc_net_lock);
1607 n_ptr = tipc_node_find((u32)addr);
1608 if (!n_ptr) {
1609 read_unlock_bh(&tipc_net_lock);
1610 return; /* node no longer exists */
1611 }
1612
1613 tipc_node_lock(n_ptr);
1614
c4307285 1615 warn("Resetting all links to %s\n",
c68ca7b7 1616 tipc_addr_string_fill(addr_string, n_ptr->addr));
d356eeba
AS
1617
1618 for (i = 0; i < MAX_BEARERS; i++) {
1619 if (n_ptr->links[i]) {
c4307285 1620 link_print(n_ptr->links[i], TIPC_OUTPUT,
d356eeba
AS
1621 "Resetting link\n");
1622 tipc_link_reset(n_ptr->links[i]);
1623 }
1624 }
1625
1626 tipc_node_unlock(n_ptr);
1627 read_unlock_bh(&tipc_net_lock);
1628}
1629
1630static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1631{
1632 struct tipc_msg *msg = buf_msg(buf);
1633
1634 warn("Retransmission failure on link <%s>\n", l_ptr->name);
48c97139 1635 tipc_msg_dbg(TIPC_OUTPUT, msg, ">RETR-FAIL>");
d356eeba
AS
1636
1637 if (l_ptr->addr) {
1638
1639 /* Handle failure on standard link */
1640
1641 link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
1642 tipc_link_reset(l_ptr);
1643
1644 } else {
1645
1646 /* Handle failure on broadcast link */
1647
6c00055a 1648 struct tipc_node *n_ptr;
d356eeba
AS
1649 char addr_string[16];
1650
1651 tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg));
617dbeaa
JG
1652 tipc_printf(TIPC_OUTPUT, "Outstanding acks: %lu\n",
1653 (unsigned long) TIPC_SKB_CB(buf)->handle);
1654
d356eeba
AS
1655 n_ptr = l_ptr->owner->next;
1656 tipc_node_lock(n_ptr);
1657
c68ca7b7 1658 tipc_addr_string_fill(addr_string, n_ptr->addr);
d356eeba
AS
1659 tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
1660 tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported);
1661 tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
1662 tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in);
1663 tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after);
1664 tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
1665 tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1666
1667 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1668
1669 tipc_node_unlock(n_ptr);
1670
1671 l_ptr->stale_count = 0;
1672 }
1673}
1674
c4307285 1675void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
4323add6 1676 u32 retransmits)
b97bf3fd
PL
1677{
1678 struct tipc_msg *msg;
1679
d356eeba
AS
1680 if (!buf)
1681 return;
1682
1683 msg = buf_msg(buf);
c4307285 1684
b97bf3fd
PL
1685 dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
1686
d356eeba 1687 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
ca509101 1688 if (l_ptr->retransm_queue_size == 0) {
d356eeba
AS
1689 msg_dbg(msg, ">NO_RETR->BCONG>");
1690 dbg_print_link(l_ptr, " ");
1691 l_ptr->retransm_queue_head = msg_seqno(msg);
1692 l_ptr->retransm_queue_size = retransmits;
d356eeba 1693 } else {
ca509101
NH
1694 err("Unexpected retransmit on link %s (qsize=%d)\n",
1695 l_ptr->name, l_ptr->retransm_queue_size);
d356eeba 1696 }
ca509101 1697 return;
d356eeba
AS
1698 } else {
1699 /* Detect repeated retransmit failures on uncongested bearer */
1700
1701 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1702 if (++l_ptr->stale_count > 100) {
1703 link_retransmit_failure(l_ptr, buf);
1704 return;
1705 }
1706 } else {
1707 l_ptr->last_retransmitted = msg_seqno(msg);
1708 l_ptr->stale_count = 1;
1709 }
b97bf3fd 1710 }
d356eeba 1711
ca509101 1712 while (retransmits && (buf != l_ptr->next_out) && buf) {
b97bf3fd
PL
1713 msg = buf_msg(buf);
1714 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 1715 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
4323add6 1716 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
b97bf3fd
PL
1717 msg_dbg(buf_msg(buf), ">RETR>");
1718 buf = buf->next;
1719 retransmits--;
1720 l_ptr->stats.retransmitted++;
1721 } else {
4323add6 1722 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
b97bf3fd
PL
1723 l_ptr->stats.bearer_congs++;
1724 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1725 l_ptr->retransm_queue_size = retransmits;
1726 return;
1727 }
1728 }
d356eeba 1729
b97bf3fd
PL
1730 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1731}
1732
c4307285 1733/**
b97bf3fd
PL
1734 * link_insert_deferred_queue - insert deferred messages back into receive chain
1735 */
1736
c4307285 1737static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
b97bf3fd
PL
1738 struct sk_buff *buf)
1739{
1740 u32 seq_no;
1741
1742 if (l_ptr->oldest_deferred_in == NULL)
1743 return buf;
1744
1745 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1746 if (seq_no == mod(l_ptr->next_in_no)) {
1747 l_ptr->newest_deferred_in->next = buf;
1748 buf = l_ptr->oldest_deferred_in;
1749 l_ptr->oldest_deferred_in = NULL;
1750 l_ptr->deferred_inqueue_sz = 0;
1751 }
1752 return buf;
1753}
1754
85035568
AS
1755/**
1756 * link_recv_buf_validate - validate basic format of received message
1757 *
1758 * This routine ensures a TIPC message has an acceptable header, and at least
1759 * as much data as the header indicates it should. The routine also ensures
1760 * that the entire message header is stored in the main fragment of the message
1761 * buffer, to simplify future access to message header fields.
1762 *
1763 * Note: Having extra info present in the message header or data areas is OK.
1764 * TIPC will ignore the excess, under the assumption that it is optional info
1765 * introduced by a later release of the protocol.
1766 */
1767
1768static int link_recv_buf_validate(struct sk_buff *buf)
1769{
1770 static u32 min_data_hdr_size[8] = {
1771 SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
1772 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1773 };
1774
1775 struct tipc_msg *msg;
1776 u32 tipc_hdr[2];
1777 u32 size;
1778 u32 hdr_size;
1779 u32 min_hdr_size;
1780
1781 if (unlikely(buf->len < MIN_H_SIZE))
1782 return 0;
1783
1784 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1785 if (msg == NULL)
1786 return 0;
1787
1788 if (unlikely(msg_version(msg) != TIPC_VERSION))
1789 return 0;
1790
1791 size = msg_size(msg);
1792 hdr_size = msg_hdr_sz(msg);
1793 min_hdr_size = msg_isdata(msg) ?
1794 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1795
1796 if (unlikely((hdr_size < min_hdr_size) ||
1797 (size < hdr_size) ||
1798 (buf->len < size) ||
1799 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1800 return 0;
1801
1802 return pskb_may_pull(buf, hdr_size);
1803}
1804
b02b69c8
AS
1805/**
1806 * tipc_recv_msg - process TIPC messages arriving from off-node
1807 * @head: pointer to message buffer chain
1808 * @tb_ptr: pointer to bearer message arrived on
1809 *
1810 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1811 * structure (i.e. cannot be NULL), but bearer can be inactive.
1812 */
1813
b97bf3fd
PL
1814void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1815{
4323add6 1816 read_lock_bh(&tipc_net_lock);
b97bf3fd 1817 while (head) {
1265a021 1818 struct bearer *b_ptr = (struct bearer *)tb_ptr;
6c00055a 1819 struct tipc_node *n_ptr;
b97bf3fd
PL
1820 struct link *l_ptr;
1821 struct sk_buff *crs;
1822 struct sk_buff *buf = head;
85035568
AS
1823 struct tipc_msg *msg;
1824 u32 seq_no;
1825 u32 ackd;
b97bf3fd
PL
1826 u32 released = 0;
1827 int type;
1828
b97bf3fd 1829 head = head->next;
85035568 1830
b02b69c8
AS
1831 /* Ensure bearer is still enabled */
1832
1833 if (unlikely(!b_ptr->active))
1834 goto cont;
1835
85035568
AS
1836 /* Ensure message is well-formed */
1837
1838 if (unlikely(!link_recv_buf_validate(buf)))
b97bf3fd 1839 goto cont;
b97bf3fd 1840
fe13dda2
AS
1841 /* Ensure message data is a single contiguous unit */
1842
1843 if (unlikely(buf_linearize(buf))) {
1844 goto cont;
1845 }
1846
85035568
AS
1847 /* Handle arrival of a non-unicast link message */
1848
1849 msg = buf_msg(buf);
1850
b97bf3fd 1851 if (unlikely(msg_non_seq(msg))) {
1265a021
AS
1852 if (msg_user(msg) == LINK_CONFIG)
1853 tipc_disc_recv_msg(buf, b_ptr);
1854 else
1855 tipc_bclink_recv_pkt(buf);
b97bf3fd
PL
1856 continue;
1857 }
c4307285 1858
26008247
AS
1859 if (unlikely(!msg_short(msg) &&
1860 (msg_destnode(msg) != tipc_own_addr)))
1861 goto cont;
c4307285 1862
de586571
NH
1863 /* Discard non-routeable messages destined for another node */
1864
1865 if (unlikely(!msg_isdata(msg) &&
1866 (msg_destnode(msg) != tipc_own_addr))) {
1867 if ((msg_user(msg) != CONN_MANAGER) &&
1868 (msg_user(msg) != MSG_FRAGMENTER))
1869 goto cont;
1870 }
1871
85035568
AS
1872 /* Locate unicast link endpoint that should handle message */
1873
4323add6 1874 n_ptr = tipc_node_find(msg_prevnode(msg));
b97bf3fd
PL
1875 if (unlikely(!n_ptr))
1876 goto cont;
4323add6 1877 tipc_node_lock(n_ptr);
85035568 1878
b97bf3fd
PL
1879 l_ptr = n_ptr->links[b_ptr->identity];
1880 if (unlikely(!l_ptr)) {
4323add6 1881 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1882 goto cont;
1883 }
85035568
AS
1884
1885 /* Validate message sequence number info */
1886
1887 seq_no = msg_seqno(msg);
1888 ackd = msg_ack(msg);
1889
1890 /* Release acked messages */
1891
b97bf3fd 1892 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
4323add6
PL
1893 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1894 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
b97bf3fd
PL
1895 }
1896
1897 crs = l_ptr->first_out;
c4307285 1898 while ((crs != l_ptr->next_out) &&
b97bf3fd
PL
1899 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1900 struct sk_buff *next = crs->next;
1901
1902 buf_discard(crs);
1903 crs = next;
1904 released++;
1905 }
1906 if (released) {
1907 l_ptr->first_out = crs;
1908 l_ptr->out_queue_size -= released;
1909 }
85035568
AS
1910
1911 /* Try sending any messages link endpoint has pending */
1912
b97bf3fd 1913 if (unlikely(l_ptr->next_out))
4323add6 1914 tipc_link_push_queue(l_ptr);
b97bf3fd 1915 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
4323add6 1916 tipc_link_wakeup_ports(l_ptr, 0);
b97bf3fd
PL
1917 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1918 l_ptr->stats.sent_acks++;
4323add6 1919 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
1920 }
1921
85035568
AS
1922 /* Now (finally!) process the incoming message */
1923
b97bf3fd
PL
1924protocol_check:
1925 if (likely(link_working_working(l_ptr))) {
1926 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1927 l_ptr->next_in_no++;
1928 if (unlikely(l_ptr->oldest_deferred_in))
1929 head = link_insert_deferred_queue(l_ptr,
1930 head);
1931 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1932deliver:
1933 if (likely(msg_isdata(msg))) {
4323add6
PL
1934 tipc_node_unlock(n_ptr);
1935 tipc_port_recv_msg(buf);
b97bf3fd
PL
1936 continue;
1937 }
1938 switch (msg_user(msg)) {
1939 case MSG_BUNDLER:
1940 l_ptr->stats.recv_bundles++;
c4307285 1941 l_ptr->stats.recv_bundled +=
b97bf3fd 1942 msg_msgcnt(msg);
4323add6
PL
1943 tipc_node_unlock(n_ptr);
1944 tipc_link_recv_bundle(buf);
b97bf3fd
PL
1945 continue;
1946 case ROUTE_DISTRIBUTOR:
4323add6
PL
1947 tipc_node_unlock(n_ptr);
1948 tipc_cltr_recv_routing_table(buf);
b97bf3fd
PL
1949 continue;
1950 case NAME_DISTRIBUTOR:
4323add6
PL
1951 tipc_node_unlock(n_ptr);
1952 tipc_named_recv(buf);
b97bf3fd
PL
1953 continue;
1954 case CONN_MANAGER:
4323add6
PL
1955 tipc_node_unlock(n_ptr);
1956 tipc_port_recv_proto_msg(buf);
b97bf3fd
PL
1957 continue;
1958 case MSG_FRAGMENTER:
1959 l_ptr->stats.recv_fragments++;
c4307285 1960 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
4323add6 1961 &buf, &msg)) {
b97bf3fd
PL
1962 l_ptr->stats.recv_fragmented++;
1963 goto deliver;
1964 }
1965 break;
1966 case CHANGEOVER_PROTOCOL:
1967 type = msg_type(msg);
4323add6 1968 if (link_recv_changeover_msg(&l_ptr, &buf)) {
b97bf3fd
PL
1969 msg = buf_msg(buf);
1970 seq_no = msg_seqno(msg);
b97bf3fd
PL
1971 if (type == ORIGINAL_MSG)
1972 goto deliver;
1973 goto protocol_check;
1974 }
1975 break;
1976 }
1977 }
4323add6
PL
1978 tipc_node_unlock(n_ptr);
1979 tipc_net_route_msg(buf);
b97bf3fd
PL
1980 continue;
1981 }
1982 link_handle_out_of_seq_msg(l_ptr, buf);
1983 head = link_insert_deferred_queue(l_ptr, head);
4323add6 1984 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1985 continue;
1986 }
1987
1988 if (msg_user(msg) == LINK_PROTOCOL) {
1989 link_recv_proto_msg(l_ptr, buf);
1990 head = link_insert_deferred_queue(l_ptr, head);
4323add6 1991 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1992 continue;
1993 }
1994 msg_dbg(msg,"NSEQ<REC<");
1995 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1996
1997 if (link_working_working(l_ptr)) {
1998 /* Re-insert in front of queue */
1999 msg_dbg(msg,"RECV-REINS:");
2000 buf->next = head;
2001 head = buf;
4323add6 2002 tipc_node_unlock(n_ptr);
b97bf3fd
PL
2003 continue;
2004 }
4323add6 2005 tipc_node_unlock(n_ptr);
b97bf3fd
PL
2006cont:
2007 buf_discard(buf);
2008 }
4323add6 2009 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
2010}
2011
c4307285
YH
2012/*
2013 * link_defer_buf(): Sort a received out-of-sequence packet
b97bf3fd
PL
2014 * into the deferred reception queue.
2015 * Returns the increase of the queue length,i.e. 0 or 1
2016 */
2017
4323add6
PL
2018u32 tipc_link_defer_pkt(struct sk_buff **head,
2019 struct sk_buff **tail,
2020 struct sk_buff *buf)
b97bf3fd 2021{
1fc54d8f 2022 struct sk_buff *prev = NULL;
b97bf3fd
PL
2023 struct sk_buff *crs = *head;
2024 u32 seq_no = msg_seqno(buf_msg(buf));
2025
2026 buf->next = NULL;
2027
2028 /* Empty queue ? */
2029 if (*head == NULL) {
2030 *head = *tail = buf;
2031 return 1;
2032 }
2033
2034 /* Last ? */
2035 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
2036 (*tail)->next = buf;
2037 *tail = buf;
2038 return 1;
2039 }
2040
2041 /* Scan through queue and sort it in */
2042 do {
2043 struct tipc_msg *msg = buf_msg(crs);
2044
2045 if (less(seq_no, msg_seqno(msg))) {
2046 buf->next = crs;
2047 if (prev)
2048 prev->next = buf;
2049 else
c4307285 2050 *head = buf;
b97bf3fd
PL
2051 return 1;
2052 }
2053 if (seq_no == msg_seqno(msg)) {
2054 break;
2055 }
2056 prev = crs;
2057 crs = crs->next;
2058 }
2059 while (crs);
2060
2061 /* Message is a duplicate of an existing message */
2062
2063 buf_discard(buf);
2064 return 0;
2065}
2066
c4307285 2067/**
b97bf3fd
PL
2068 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
2069 */
2070
c4307285 2071static void link_handle_out_of_seq_msg(struct link *l_ptr,
b97bf3fd
PL
2072 struct sk_buff *buf)
2073{
2074 u32 seq_no = msg_seqno(buf_msg(buf));
2075
2076 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
2077 link_recv_proto_msg(l_ptr, buf);
2078 return;
2079 }
2080
c4307285 2081 dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
b97bf3fd
PL
2082 seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
2083
2084 /* Record OOS packet arrival (force mismatch on next timeout) */
2085
2086 l_ptr->checkpoint--;
2087
c4307285 2088 /*
b97bf3fd
PL
2089 * Discard packet if a duplicate; otherwise add it to deferred queue
2090 * and notify peer of gap as per protocol specification
2091 */
2092
2093 if (less(seq_no, mod(l_ptr->next_in_no))) {
2094 l_ptr->stats.duplicates++;
2095 buf_discard(buf);
2096 return;
2097 }
2098
4323add6
PL
2099 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
2100 &l_ptr->newest_deferred_in, buf)) {
b97bf3fd
PL
2101 l_ptr->deferred_inqueue_sz++;
2102 l_ptr->stats.deferred_recv++;
2103 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
4323add6 2104 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
2105 } else
2106 l_ptr->stats.duplicates++;
2107}
2108
2109/*
2110 * Send protocol message to the other endpoint.
2111 */
4323add6
PL
2112void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
2113 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
b97bf3fd 2114{
1fc54d8f 2115 struct sk_buff *buf = NULL;
b97bf3fd 2116 struct tipc_msg *msg = l_ptr->pmsg;
c4307285 2117 u32 msg_size = sizeof(l_ptr->proto_msg);
b97bf3fd
PL
2118
2119 if (link_blocked(l_ptr))
2120 return;
2121 msg_set_type(msg, msg_typ);
2122 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
c4307285 2123 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
4323add6 2124 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
b97bf3fd
PL
2125
2126 if (msg_typ == STATE_MSG) {
2127 u32 next_sent = mod(l_ptr->next_out_no);
2128
4323add6 2129 if (!tipc_link_is_up(l_ptr))
b97bf3fd
PL
2130 return;
2131 if (l_ptr->next_out)
2132 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
2133 msg_set_next_sent(msg, next_sent);
2134 if (l_ptr->oldest_deferred_in) {
2135 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2136 gap = mod(rec - mod(l_ptr->next_in_no));
2137 }
2138 msg_set_seq_gap(msg, gap);
2139 if (gap)
2140 l_ptr->stats.sent_nacks++;
2141 msg_set_link_tolerance(msg, tolerance);
2142 msg_set_linkprio(msg, priority);
2143 msg_set_max_pkt(msg, ack_mtu);
2144 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
2145 msg_set_probe(msg, probe_msg != 0);
c4307285 2146 if (probe_msg) {
b97bf3fd
PL
2147 u32 mtu = l_ptr->max_pkt;
2148
c4307285 2149 if ((mtu < l_ptr->max_pkt_target) &&
b97bf3fd
PL
2150 link_working_working(l_ptr) &&
2151 l_ptr->fsm_msg_cnt) {
2152 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285
YH
2153 if (l_ptr->max_pkt_probes == 10) {
2154 l_ptr->max_pkt_target = (msg_size - 4);
2155 l_ptr->max_pkt_probes = 0;
b97bf3fd 2156 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285 2157 }
b97bf3fd 2158 l_ptr->max_pkt_probes++;
c4307285 2159 }
b97bf3fd
PL
2160
2161 l_ptr->stats.sent_probes++;
c4307285 2162 }
b97bf3fd
PL
2163 l_ptr->stats.sent_states++;
2164 } else { /* RESET_MSG or ACTIVATE_MSG */
2165 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
2166 msg_set_seq_gap(msg, 0);
2167 msg_set_next_sent(msg, 1);
2168 msg_set_link_tolerance(msg, l_ptr->tolerance);
2169 msg_set_linkprio(msg, l_ptr->priority);
2170 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
2171 }
2172
4323add6 2173 if (tipc_node_has_redundant_links(l_ptr->owner)) {
b97bf3fd
PL
2174 msg_set_redundant_link(msg);
2175 } else {
2176 msg_clear_redundant_link(msg);
2177 }
2178 msg_set_linkprio(msg, l_ptr->priority);
2179
2180 /* Ensure sequence number will not fit : */
2181
2182 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
2183
2184 /* Congestion? */
2185
4323add6 2186 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
b97bf3fd
PL
2187 if (!l_ptr->proto_msg_queue) {
2188 l_ptr->proto_msg_queue =
2189 buf_acquire(sizeof(l_ptr->proto_msg));
2190 }
2191 buf = l_ptr->proto_msg_queue;
2192 if (!buf)
2193 return;
27d7ff46 2194 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
b97bf3fd
PL
2195 return;
2196 }
2197 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
2198
2199 /* Message can be sent */
2200
2201 msg_dbg(msg, ">>");
2202
2203 buf = buf_acquire(msg_size);
2204 if (!buf)
2205 return;
2206
27d7ff46 2207 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
c4307285 2208 msg_set_size(buf_msg(buf), msg_size);
b97bf3fd 2209
4323add6 2210 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
b97bf3fd
PL
2211 l_ptr->unacked_window = 0;
2212 buf_discard(buf);
2213 return;
2214 }
2215
2216 /* New congestion */
4323add6 2217 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
b97bf3fd
PL
2218 l_ptr->proto_msg_queue = buf;
2219 l_ptr->stats.bearer_congs++;
2220}
2221
2222/*
2223 * Receive protocol message :
c4307285
YH
2224 * Note that network plane id propagates through the network, and may
2225 * change at any time. The node with lowest address rules
b97bf3fd
PL
2226 */
2227
2228static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2229{
2230 u32 rec_gap = 0;
2231 u32 max_pkt_info;
c4307285 2232 u32 max_pkt_ack;
b97bf3fd
PL
2233 u32 msg_tol;
2234 struct tipc_msg *msg = buf_msg(buf);
2235
2236 dbg("AT(%u):", jiffies_to_msecs(jiffies));
2237 msg_dbg(msg, "<<");
2238 if (link_blocked(l_ptr))
2239 goto exit;
2240
2241 /* record unnumbered packet arrival (force mismatch on next timeout) */
2242
2243 l_ptr->checkpoint--;
2244
2245 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2246 if (tipc_own_addr > msg_prevnode(msg))
2247 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2248
2249 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2250
2251 switch (msg_type(msg)) {
c4307285 2252
b97bf3fd 2253 case RESET_MSG:
a686e685
AS
2254 if (!link_working_unknown(l_ptr) &&
2255 (l_ptr->peer_session != INVALID_SESSION)) {
b97bf3fd
PL
2256 if (msg_session(msg) == l_ptr->peer_session) {
2257 dbg("Duplicate RESET: %u<->%u\n",
c4307285 2258 msg_session(msg), l_ptr->peer_session);
b97bf3fd
PL
2259 break; /* duplicate: ignore */
2260 }
2261 }
2262 /* fall thru' */
2263 case ACTIVATE_MSG:
2264 /* Update link settings according other endpoint's values */
2265
2266 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2267
2268 if ((msg_tol = msg_link_tolerance(msg)) &&
2269 (msg_tol > l_ptr->tolerance))
2270 link_set_supervision_props(l_ptr, msg_tol);
2271
2272 if (msg_linkprio(msg) > l_ptr->priority)
2273 l_ptr->priority = msg_linkprio(msg);
2274
2275 max_pkt_info = msg_max_pkt(msg);
c4307285 2276 if (max_pkt_info) {
b97bf3fd
PL
2277 if (max_pkt_info < l_ptr->max_pkt_target)
2278 l_ptr->max_pkt_target = max_pkt_info;
2279 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2280 l_ptr->max_pkt = l_ptr->max_pkt_target;
2281 } else {
c4307285 2282 l_ptr->max_pkt = l_ptr->max_pkt_target;
b97bf3fd
PL
2283 }
2284 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2285
2286 link_state_event(l_ptr, msg_type(msg));
2287
2288 l_ptr->peer_session = msg_session(msg);
2289 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2290
2291 /* Synchronize broadcast sequence numbers */
4323add6 2292 if (!tipc_node_has_redundant_links(l_ptr->owner)) {
b97bf3fd
PL
2293 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2294 }
2295 break;
2296 case STATE_MSG:
2297
2298 if ((msg_tol = msg_link_tolerance(msg)))
2299 link_set_supervision_props(l_ptr, msg_tol);
c4307285
YH
2300
2301 if (msg_linkprio(msg) &&
b97bf3fd 2302 (msg_linkprio(msg) != l_ptr->priority)) {
a10bd924 2303 warn("Resetting link <%s>, priority change %u->%u\n",
b97bf3fd
PL
2304 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2305 l_ptr->priority = msg_linkprio(msg);
4323add6 2306 tipc_link_reset(l_ptr); /* Enforce change to take effect */
b97bf3fd
PL
2307 break;
2308 }
2309 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2310 l_ptr->stats.recv_states++;
2311 if (link_reset_unknown(l_ptr))
2312 break;
2313
2314 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
c4307285 2315 rec_gap = mod(msg_next_sent(msg) -
b97bf3fd
PL
2316 mod(l_ptr->next_in_no));
2317 }
2318
2319 max_pkt_ack = msg_max_pkt(msg);
c4307285
YH
2320 if (max_pkt_ack > l_ptr->max_pkt) {
2321 dbg("Link <%s> updated MTU %u -> %u\n",
2322 l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
2323 l_ptr->max_pkt = max_pkt_ack;
2324 l_ptr->max_pkt_probes = 0;
2325 }
b97bf3fd
PL
2326
2327 max_pkt_ack = 0;
c4307285 2328 if (msg_probe(msg)) {
b97bf3fd 2329 l_ptr->stats.recv_probes++;
c4307285
YH
2330 if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2331 max_pkt_ack = msg_size(msg);
2332 }
2333 }
b97bf3fd
PL
2334
2335 /* Protocol message before retransmits, reduce loss risk */
2336
4323add6 2337 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
b97bf3fd
PL
2338
2339 if (rec_gap || (msg_probe(msg))) {
4323add6
PL
2340 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2341 0, rec_gap, 0, 0, max_pkt_ack);
b97bf3fd
PL
2342 }
2343 if (msg_seq_gap(msg)) {
2344 msg_dbg(msg, "With Gap:");
2345 l_ptr->stats.recv_nacks++;
4323add6
PL
2346 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2347 msg_seq_gap(msg));
b97bf3fd
PL
2348 }
2349 break;
2350 default:
2351 msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
2352 }
2353exit:
2354 buf_discard(buf);
2355}
2356
2357
2358/*
c4307285 2359 * tipc_link_tunnel(): Send one message via a link belonging to
b97bf3fd
PL
2360 * another bearer. Owner node is locked.
2361 */
c4307285
YH
2362void tipc_link_tunnel(struct link *l_ptr,
2363 struct tipc_msg *tunnel_hdr,
4323add6
PL
2364 struct tipc_msg *msg,
2365 u32 selector)
b97bf3fd
PL
2366{
2367 struct link *tunnel;
2368 struct sk_buff *buf;
2369 u32 length = msg_size(msg);
2370
2371 tunnel = l_ptr->owner->active_links[selector & 1];
5392d646
AS
2372 if (!tipc_link_is_up(tunnel)) {
2373 warn("Link changeover error, "
2374 "tunnel link no longer available\n");
b97bf3fd 2375 return;
5392d646 2376 }
b97bf3fd
PL
2377 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
2378 buf = buf_acquire(length + INT_H_SIZE);
5392d646
AS
2379 if (!buf) {
2380 warn("Link changeover error, "
2381 "unable to send tunnel msg\n");
b97bf3fd 2382 return;
5392d646 2383 }
27d7ff46
ACM
2384 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2385 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
b97bf3fd
PL
2386 dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
2387 msg_dbg(buf_msg(buf), ">SEND>");
4323add6 2388 tipc_link_send_buf(tunnel, buf);
b97bf3fd
PL
2389}
2390
2391
2392
2393/*
2394 * changeover(): Send whole message queue via the remaining link
2395 * Owner node is locked.
2396 */
2397
4323add6 2398void tipc_link_changeover(struct link *l_ptr)
b97bf3fd
PL
2399{
2400 u32 msgcount = l_ptr->out_queue_size;
2401 struct sk_buff *crs = l_ptr->first_out;
2402 struct link *tunnel = l_ptr->owner->active_links[0];
b97bf3fd 2403 struct tipc_msg tunnel_hdr;
5392d646 2404 int split_bundles;
b97bf3fd
PL
2405
2406 if (!tunnel)
2407 return;
2408
5392d646
AS
2409 if (!l_ptr->owner->permit_changeover) {
2410 warn("Link changeover error, "
2411 "peer did not permit changeover\n");
b97bf3fd 2412 return;
5392d646 2413 }
b97bf3fd 2414
c68ca7b7 2415 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
75715217 2416 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd
PL
2417 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2418 msg_set_msgcnt(&tunnel_hdr, msgcount);
5392d646 2419 dbg("Link changeover requires %u tunnel messages\n", msgcount);
f131072c 2420
b97bf3fd
PL
2421 if (!l_ptr->first_out) {
2422 struct sk_buff *buf;
2423
b97bf3fd
PL
2424 buf = buf_acquire(INT_H_SIZE);
2425 if (buf) {
27d7ff46 2426 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
b97bf3fd
PL
2427 msg_set_size(&tunnel_hdr, INT_H_SIZE);
2428 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2429 tunnel->b_ptr->net_plane);
2430 msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
4323add6 2431 tipc_link_send_buf(tunnel, buf);
b97bf3fd 2432 } else {
a10bd924
AS
2433 warn("Link changeover error, "
2434 "unable to send changeover msg\n");
b97bf3fd
PL
2435 }
2436 return;
2437 }
f131072c 2438
c4307285 2439 split_bundles = (l_ptr->owner->active_links[0] !=
5392d646
AS
2440 l_ptr->owner->active_links[1]);
2441
b97bf3fd
PL
2442 while (crs) {
2443 struct tipc_msg *msg = buf_msg(crs);
2444
2445 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
b97bf3fd
PL
2446 struct tipc_msg *m = msg_get_wrapped(msg);
2447 unchar* pos = (unchar*)m;
2448
d788d805 2449 msgcount = msg_msgcnt(msg);
b97bf3fd
PL
2450 while (msgcount--) {
2451 msg_set_seqno(m,msg_seqno(msg));
4323add6
PL
2452 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2453 msg_link_selector(m));
b97bf3fd
PL
2454 pos += align(msg_size(m));
2455 m = (struct tipc_msg *)pos;
2456 }
2457 } else {
4323add6
PL
2458 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2459 msg_link_selector(msg));
b97bf3fd
PL
2460 }
2461 crs = crs->next;
2462 }
2463}
2464
4323add6 2465void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
b97bf3fd
PL
2466{
2467 struct sk_buff *iter;
2468 struct tipc_msg tunnel_hdr;
2469
c68ca7b7 2470 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
75715217 2471 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd
PL
2472 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2473 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2474 iter = l_ptr->first_out;
2475 while (iter) {
2476 struct sk_buff *outbuf;
2477 struct tipc_msg *msg = buf_msg(iter);
2478 u32 length = msg_size(msg);
2479
2480 if (msg_user(msg) == MSG_BUNDLER)
2481 msg_set_type(msg, CLOSED_MSG);
2482 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
c4307285 2483 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
b97bf3fd
PL
2484 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
2485 outbuf = buf_acquire(length + INT_H_SIZE);
2486 if (outbuf == NULL) {
a10bd924
AS
2487 warn("Link changeover error, "
2488 "unable to send duplicate msg\n");
b97bf3fd
PL
2489 return;
2490 }
27d7ff46
ACM
2491 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2492 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2493 length);
b97bf3fd
PL
2494 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2495 tunnel->b_ptr->net_plane);
2496 msg_dbg(buf_msg(outbuf), ">SEND>");
4323add6
PL
2497 tipc_link_send_buf(tunnel, outbuf);
2498 if (!tipc_link_is_up(l_ptr))
b97bf3fd
PL
2499 return;
2500 iter = iter->next;
2501 }
2502}
2503
2504
2505
2506/**
2507 * buf_extract - extracts embedded TIPC message from another message
2508 * @skb: encapsulating message buffer
2509 * @from_pos: offset to extract from
2510 *
c4307285 2511 * Returns a new message buffer containing an embedded message. The
b97bf3fd
PL
2512 * encapsulating message itself is left unchanged.
2513 */
2514
2515static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2516{
2517 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2518 u32 size = msg_size(msg);
2519 struct sk_buff *eb;
2520
2521 eb = buf_acquire(size);
2522 if (eb)
27d7ff46 2523 skb_copy_to_linear_data(eb, msg, size);
b97bf3fd
PL
2524 return eb;
2525}
2526
c4307285 2527/*
b97bf3fd
PL
2528 * link_recv_changeover_msg(): Receive tunneled packet sent
2529 * via other link. Node is locked. Return extracted buffer.
2530 */
2531
2532static int link_recv_changeover_msg(struct link **l_ptr,
2533 struct sk_buff **buf)
2534{
2535 struct sk_buff *tunnel_buf = *buf;
2536 struct link *dest_link;
2537 struct tipc_msg *msg;
2538 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2539 u32 msg_typ = msg_type(tunnel_msg);
2540 u32 msg_count = msg_msgcnt(tunnel_msg);
2541
2542 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
b97bf3fd
PL
2543 if (!dest_link) {
2544 msg_dbg(tunnel_msg, "NOLINK/<REC<");
2545 goto exit;
2546 }
f131072c 2547 if (dest_link == *l_ptr) {
c4307285 2548 err("Unexpected changeover message on link <%s>\n",
f131072c
AS
2549 (*l_ptr)->name);
2550 goto exit;
2551 }
b97bf3fd
PL
2552 dbg("%c<-%c:", dest_link->b_ptr->net_plane,
2553 (*l_ptr)->b_ptr->net_plane);
2554 *l_ptr = dest_link;
2555 msg = msg_get_wrapped(tunnel_msg);
2556
2557 if (msg_typ == DUPLICATE_MSG) {
2558 if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
2559 msg_dbg(tunnel_msg, "DROP/<REC<");
2560 goto exit;
2561 }
2562 *buf = buf_extract(tunnel_buf,INT_H_SIZE);
2563 if (*buf == NULL) {
a10bd924 2564 warn("Link changeover error, duplicate msg dropped\n");
b97bf3fd
PL
2565 goto exit;
2566 }
2567 msg_dbg(tunnel_msg, "TNL<REC<");
2568 buf_discard(tunnel_buf);
2569 return 1;
2570 }
2571
2572 /* First original message ?: */
2573
4323add6 2574 if (tipc_link_is_up(dest_link)) {
b97bf3fd 2575 msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
a10bd924
AS
2576 info("Resetting link <%s>, changeover initiated by peer\n",
2577 dest_link->name);
4323add6 2578 tipc_link_reset(dest_link);
b97bf3fd 2579 dest_link->exp_msg_count = msg_count;
5392d646 2580 dbg("Expecting %u tunnelled messages\n", msg_count);
b97bf3fd
PL
2581 if (!msg_count)
2582 goto exit;
2583 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
2584 msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
2585 dest_link->exp_msg_count = msg_count;
5392d646 2586 dbg("Expecting %u tunnelled messages\n", msg_count);
b97bf3fd
PL
2587 if (!msg_count)
2588 goto exit;
2589 }
2590
2591 /* Receive original message */
2592
2593 if (dest_link->exp_msg_count == 0) {
5392d646
AS
2594 warn("Link switchover error, "
2595 "got too many tunnelled messages\n");
b97bf3fd
PL
2596 msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
2597 dbg_print_link(dest_link, "LINK:");
2598 goto exit;
2599 }
2600 dest_link->exp_msg_count--;
2601 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
2602 msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
2603 goto exit;
2604 } else {
2605 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2606 if (*buf != NULL) {
2607 msg_dbg(tunnel_msg, "TNL<REC<");
2608 buf_discard(tunnel_buf);
2609 return 1;
2610 } else {
a10bd924 2611 warn("Link changeover error, original msg dropped\n");
b97bf3fd
PL
2612 }
2613 }
2614exit:
1fc54d8f 2615 *buf = NULL;
b97bf3fd
PL
2616 buf_discard(tunnel_buf);
2617 return 0;
2618}
2619
2620/*
2621 * Bundler functionality:
2622 */
4323add6 2623void tipc_link_recv_bundle(struct sk_buff *buf)
b97bf3fd
PL
2624{
2625 u32 msgcount = msg_msgcnt(buf_msg(buf));
2626 u32 pos = INT_H_SIZE;
2627 struct sk_buff *obuf;
2628
2629 msg_dbg(buf_msg(buf), "<BNDL<: ");
2630 while (msgcount--) {
2631 obuf = buf_extract(buf, pos);
2632 if (obuf == NULL) {
a10bd924
AS
2633 warn("Link unable to unbundle message(s)\n");
2634 break;
3ff50b79 2635 }
b97bf3fd
PL
2636 pos += align(msg_size(buf_msg(obuf)));
2637 msg_dbg(buf_msg(obuf), " /");
4323add6 2638 tipc_net_route_msg(obuf);
b97bf3fd
PL
2639 }
2640 buf_discard(buf);
2641}
2642
2643/*
2644 * Fragmentation/defragmentation:
2645 */
2646
2647
c4307285 2648/*
4323add6 2649 * tipc_link_send_long_buf: Entry for buffers needing fragmentation.
c4307285 2650 * The buffer is complete, inclusive total message length.
b97bf3fd
PL
2651 * Returns user data length.
2652 */
4323add6 2653int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
b97bf3fd
PL
2654{
2655 struct tipc_msg *inmsg = buf_msg(buf);
2656 struct tipc_msg fragm_hdr;
2657 u32 insize = msg_size(inmsg);
2658 u32 dsz = msg_data_sz(inmsg);
2659 unchar *crs = buf->data;
2660 u32 rest = insize;
15e979da 2661 u32 pack_sz = l_ptr->max_pkt;
b97bf3fd
PL
2662 u32 fragm_sz = pack_sz - INT_H_SIZE;
2663 u32 fragm_no = 1;
9c396a7b 2664 u32 destaddr;
b97bf3fd
PL
2665
2666 if (msg_short(inmsg))
2667 destaddr = l_ptr->addr;
9c396a7b
AS
2668 else
2669 destaddr = msg_destnode(inmsg);
b97bf3fd
PL
2670
2671 if (msg_routed(inmsg))
2672 msg_set_prevnode(inmsg, tipc_own_addr);
2673
2674 /* Prepare reusable fragment header: */
2675
c68ca7b7 2676 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
75715217 2677 INT_H_SIZE, destaddr);
b97bf3fd
PL
2678 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2679 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2680 msg_set_fragm_no(&fragm_hdr, fragm_no);
2681 l_ptr->stats.sent_fragmented++;
2682
2683 /* Chop up message: */
2684
2685 while (rest > 0) {
2686 struct sk_buff *fragm;
2687
2688 if (rest <= fragm_sz) {
2689 fragm_sz = rest;
2690 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2691 }
2692 fragm = buf_acquire(fragm_sz + INT_H_SIZE);
2693 if (fragm == NULL) {
a10bd924 2694 warn("Link unable to fragment message\n");
b97bf3fd
PL
2695 dsz = -ENOMEM;
2696 goto exit;
2697 }
2698 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
27d7ff46
ACM
2699 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2700 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2701 fragm_sz);
b97bf3fd
PL
2702 /* Send queued messages first, if any: */
2703
2704 l_ptr->stats.sent_fragments++;
4323add6
PL
2705 tipc_link_send_buf(l_ptr, fragm);
2706 if (!tipc_link_is_up(l_ptr))
b97bf3fd
PL
2707 return dsz;
2708 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2709 rest -= fragm_sz;
2710 crs += fragm_sz;
2711 msg_set_type(&fragm_hdr, FRAGMENT);
2712 }
2713exit:
2714 buf_discard(buf);
2715 return dsz;
2716}
2717
c4307285
YH
2718/*
2719 * A pending message being re-assembled must store certain values
2720 * to handle subsequent fragments correctly. The following functions
b97bf3fd
PL
2721 * help storing these values in unused, available fields in the
2722 * pending message. This makes dynamic memory allocation unecessary.
2723 */
2724
05790c64 2725static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
b97bf3fd
PL
2726{
2727 msg_set_seqno(buf_msg(buf), seqno);
2728}
2729
05790c64 2730static u32 get_fragm_size(struct sk_buff *buf)
b97bf3fd
PL
2731{
2732 return msg_ack(buf_msg(buf));
2733}
2734
05790c64 2735static void set_fragm_size(struct sk_buff *buf, u32 sz)
b97bf3fd
PL
2736{
2737 msg_set_ack(buf_msg(buf), sz);
2738}
2739
05790c64 2740static u32 get_expected_frags(struct sk_buff *buf)
b97bf3fd
PL
2741{
2742 return msg_bcast_ack(buf_msg(buf));
2743}
2744
05790c64 2745static void set_expected_frags(struct sk_buff *buf, u32 exp)
b97bf3fd
PL
2746{
2747 msg_set_bcast_ack(buf_msg(buf), exp);
2748}
2749
05790c64 2750static u32 get_timer_cnt(struct sk_buff *buf)
b97bf3fd
PL
2751{
2752 return msg_reroute_cnt(buf_msg(buf));
2753}
2754
05790c64 2755static void incr_timer_cnt(struct sk_buff *buf)
b97bf3fd
PL
2756{
2757 msg_incr_reroute_cnt(buf_msg(buf));
2758}
2759
c4307285
YH
2760/*
2761 * tipc_link_recv_fragment(): Called with node lock on. Returns
b97bf3fd
PL
2762 * the reassembled buffer if message is complete.
2763 */
c4307285 2764int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
4323add6 2765 struct tipc_msg **m)
b97bf3fd 2766{
1fc54d8f 2767 struct sk_buff *prev = NULL;
b97bf3fd
PL
2768 struct sk_buff *fbuf = *fb;
2769 struct tipc_msg *fragm = buf_msg(fbuf);
2770 struct sk_buff *pbuf = *pending;
2771 u32 long_msg_seq_no = msg_long_msgno(fragm);
2772
1fc54d8f 2773 *fb = NULL;
b97bf3fd
PL
2774 msg_dbg(fragm,"FRG<REC<");
2775
2776 /* Is there an incomplete message waiting for this fragment? */
2777
f64f9e71
JP
2778 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
2779 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
b97bf3fd
PL
2780 prev = pbuf;
2781 pbuf = pbuf->next;
2782 }
2783
2784 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2785 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2786 u32 msg_sz = msg_size(imsg);
2787 u32 fragm_sz = msg_data_sz(fragm);
2788 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2789 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2790 if (msg_type(imsg) == TIPC_MCAST_MSG)
2791 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2792 if (msg_size(imsg) > max) {
2793 msg_dbg(fragm,"<REC<Oversized: ");
2794 buf_discard(fbuf);
2795 return 0;
2796 }
2797 pbuf = buf_acquire(msg_size(imsg));
2798 if (pbuf != NULL) {
2799 pbuf->next = *pending;
2800 *pending = pbuf;
27d7ff46
ACM
2801 skb_copy_to_linear_data(pbuf, imsg,
2802 msg_data_sz(fragm));
b97bf3fd
PL
2803 /* Prepare buffer for subsequent fragments. */
2804
c4307285
YH
2805 set_long_msg_seqno(pbuf, long_msg_seq_no);
2806 set_fragm_size(pbuf,fragm_sz);
2807 set_expected_frags(pbuf,exp_fragm_cnt - 1);
b97bf3fd 2808 } else {
a10bd924 2809 warn("Link unable to reassemble fragmented message\n");
b97bf3fd
PL
2810 }
2811 buf_discard(fbuf);
2812 return 0;
2813 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2814 u32 dsz = msg_data_sz(fragm);
2815 u32 fsz = get_fragm_size(pbuf);
2816 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2817 u32 exp_frags = get_expected_frags(pbuf) - 1;
27d7ff46
ACM
2818 skb_copy_to_linear_data_offset(pbuf, crs,
2819 msg_data(fragm), dsz);
b97bf3fd
PL
2820 buf_discard(fbuf);
2821
2822 /* Is message complete? */
2823
2824 if (exp_frags == 0) {
2825 if (prev)
2826 prev->next = pbuf->next;
2827 else
2828 *pending = pbuf->next;
2829 msg_reset_reroute_cnt(buf_msg(pbuf));
2830 *fb = pbuf;
2831 *m = buf_msg(pbuf);
2832 return 1;
2833 }
c4307285 2834 set_expected_frags(pbuf,exp_frags);
b97bf3fd
PL
2835 return 0;
2836 }
2837 dbg(" Discarding orphan fragment %x\n",fbuf);
2838 msg_dbg(fragm,"ORPHAN:");
2839 dbg("Pending long buffers:\n");
2840 dbg_print_buf_chain(*pending);
2841 buf_discard(fbuf);
2842 return 0;
2843}
2844
2845/**
2846 * link_check_defragm_bufs - flush stale incoming message fragments
2847 * @l_ptr: pointer to link
2848 */
2849
2850static void link_check_defragm_bufs(struct link *l_ptr)
2851{
1fc54d8f
SR
2852 struct sk_buff *prev = NULL;
2853 struct sk_buff *next = NULL;
b97bf3fd
PL
2854 struct sk_buff *buf = l_ptr->defragm_buf;
2855
2856 if (!buf)
2857 return;
2858 if (!link_working_working(l_ptr))
2859 return;
2860 while (buf) {
2861 u32 cnt = get_timer_cnt(buf);
2862
2863 next = buf->next;
2864 if (cnt < 4) {
2865 incr_timer_cnt(buf);
2866 prev = buf;
2867 } else {
2868 dbg(" Discarding incomplete long buffer\n");
2869 msg_dbg(buf_msg(buf), "LONG:");
2870 dbg_print_link(l_ptr, "curr:");
2871 dbg("Pending long buffers:\n");
2872 dbg_print_buf_chain(l_ptr->defragm_buf);
2873 if (prev)
2874 prev->next = buf->next;
2875 else
2876 l_ptr->defragm_buf = buf->next;
2877 buf_discard(buf);
2878 }
2879 buf = next;
2880 }
2881}
2882
2883
2884
2885static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2886{
2887 l_ptr->tolerance = tolerance;
2888 l_ptr->continuity_interval =
2889 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2890 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2891}
2892
2893
4323add6 2894void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
b97bf3fd
PL
2895{
2896 /* Data messages from this node, inclusive FIRST_FRAGM */
06d82c91
AS
2897 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2898 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2899 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2900 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
b97bf3fd 2901 /* Transiting data messages,inclusive FIRST_FRAGM */
06d82c91
AS
2902 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2903 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2904 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2905 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
b97bf3fd
PL
2906 l_ptr->queue_limit[CONN_MANAGER] = 1200;
2907 l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
2908 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2909 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2910 /* FRAGMENT and LAST_FRAGMENT packets */
2911 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2912}
2913
2914/**
2915 * link_find_link - locate link by name
2916 * @name - ptr to link name string
2917 * @node - ptr to area to be filled with ptr to associated node
c4307285 2918 *
4323add6 2919 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
b97bf3fd 2920 * this also prevents link deletion.
c4307285 2921 *
b97bf3fd
PL
2922 * Returns pointer to link (or 0 if invalid link name).
2923 */
2924
6c00055a 2925static struct link *link_find_link(const char *name, struct tipc_node **node)
b97bf3fd
PL
2926{
2927 struct link_name link_name_parts;
2928 struct bearer *b_ptr;
c4307285 2929 struct link *l_ptr;
b97bf3fd
PL
2930
2931 if (!link_name_validate(name, &link_name_parts))
1fc54d8f 2932 return NULL;
b97bf3fd 2933
4323add6 2934 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
b97bf3fd 2935 if (!b_ptr)
1fc54d8f 2936 return NULL;
b97bf3fd 2937
c4307285 2938 *node = tipc_node_find(link_name_parts.addr_peer);
b97bf3fd 2939 if (!*node)
1fc54d8f 2940 return NULL;
b97bf3fd
PL
2941
2942 l_ptr = (*node)->links[b_ptr->identity];
2943 if (!l_ptr || strcmp(l_ptr->name, name))
1fc54d8f 2944 return NULL;
b97bf3fd
PL
2945
2946 return l_ptr;
2947}
2948
c4307285 2949struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
4323add6 2950 u16 cmd)
b97bf3fd
PL
2951{
2952 struct tipc_link_config *args;
c4307285 2953 u32 new_value;
b97bf3fd 2954 struct link *l_ptr;
6c00055a 2955 struct tipc_node *node;
c4307285 2956 int res;
b97bf3fd
PL
2957
2958 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
4323add6 2959 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd
PL
2960
2961 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2962 new_value = ntohl(args->value);
2963
4323add6 2964 if (!strcmp(args->name, tipc_bclink_name)) {
b97bf3fd 2965 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
4323add6
PL
2966 (tipc_bclink_set_queue_limits(new_value) == 0))
2967 return tipc_cfg_reply_none();
c4307285 2968 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
4323add6 2969 " (cannot change setting on broadcast link)");
b97bf3fd
PL
2970 }
2971
4323add6 2972 read_lock_bh(&tipc_net_lock);
c4307285 2973 l_ptr = link_find_link(args->name, &node);
b97bf3fd 2974 if (!l_ptr) {
4323add6 2975 read_unlock_bh(&tipc_net_lock);
c4307285 2976 return tipc_cfg_reply_error_string("link not found");
b97bf3fd
PL
2977 }
2978
4323add6 2979 tipc_node_lock(node);
b97bf3fd
PL
2980 res = -EINVAL;
2981 switch (cmd) {
c4307285
YH
2982 case TIPC_CMD_SET_LINK_TOL:
2983 if ((new_value >= TIPC_MIN_LINK_TOL) &&
b97bf3fd
PL
2984 (new_value <= TIPC_MAX_LINK_TOL)) {
2985 link_set_supervision_props(l_ptr, new_value);
c4307285 2986 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
4323add6 2987 0, 0, new_value, 0, 0);
0e35fd5e 2988 res = 0;
b97bf3fd
PL
2989 }
2990 break;
c4307285 2991 case TIPC_CMD_SET_LINK_PRI:
16cb4b33
PL
2992 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2993 (new_value <= TIPC_MAX_LINK_PRI)) {
b97bf3fd 2994 l_ptr->priority = new_value;
c4307285 2995 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
4323add6 2996 0, 0, 0, new_value, 0);
0e35fd5e 2997 res = 0;
b97bf3fd
PL
2998 }
2999 break;
c4307285
YH
3000 case TIPC_CMD_SET_LINK_WINDOW:
3001 if ((new_value >= TIPC_MIN_LINK_WIN) &&
b97bf3fd 3002 (new_value <= TIPC_MAX_LINK_WIN)) {
4323add6 3003 tipc_link_set_queue_limits(l_ptr, new_value);
0e35fd5e 3004 res = 0;
b97bf3fd
PL
3005 }
3006 break;
3007 }
4323add6 3008 tipc_node_unlock(node);
b97bf3fd 3009
4323add6 3010 read_unlock_bh(&tipc_net_lock);
b97bf3fd 3011 if (res)
c4307285 3012 return tipc_cfg_reply_error_string("cannot change link setting");
b97bf3fd 3013
4323add6 3014 return tipc_cfg_reply_none();
b97bf3fd
PL
3015}
3016
3017/**
3018 * link_reset_statistics - reset link statistics
3019 * @l_ptr: pointer to link
3020 */
3021
3022static void link_reset_statistics(struct link *l_ptr)
3023{
3024 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
3025 l_ptr->stats.sent_info = l_ptr->next_out_no;
3026 l_ptr->stats.recv_info = l_ptr->next_in_no;
3027}
3028
4323add6 3029struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
3030{
3031 char *link_name;
c4307285 3032 struct link *l_ptr;
6c00055a 3033 struct tipc_node *node;
b97bf3fd
PL
3034
3035 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
4323add6 3036 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd
PL
3037
3038 link_name = (char *)TLV_DATA(req_tlv_area);
4323add6
PL
3039 if (!strcmp(link_name, tipc_bclink_name)) {
3040 if (tipc_bclink_reset_stats())
3041 return tipc_cfg_reply_error_string("link not found");
3042 return tipc_cfg_reply_none();
b97bf3fd
PL
3043 }
3044
4323add6 3045 read_lock_bh(&tipc_net_lock);
c4307285 3046 l_ptr = link_find_link(link_name, &node);
b97bf3fd 3047 if (!l_ptr) {
4323add6
PL
3048 read_unlock_bh(&tipc_net_lock);
3049 return tipc_cfg_reply_error_string("link not found");
b97bf3fd
PL
3050 }
3051
4323add6 3052 tipc_node_lock(node);
b97bf3fd 3053 link_reset_statistics(l_ptr);
4323add6
PL
3054 tipc_node_unlock(node);
3055 read_unlock_bh(&tipc_net_lock);
3056 return tipc_cfg_reply_none();
b97bf3fd
PL
3057}
3058
3059/**
3060 * percent - convert count to a percentage of total (rounding up or down)
3061 */
3062
3063static u32 percent(u32 count, u32 total)
3064{
3065 return (count * 100 + (total / 2)) / total;
3066}
3067
3068/**
4323add6 3069 * tipc_link_stats - print link statistics
b97bf3fd
PL
3070 * @name: link name
3071 * @buf: print buffer area
3072 * @buf_size: size of print buffer area
c4307285 3073 *
b97bf3fd
PL
3074 * Returns length of print buffer data string (or 0 if error)
3075 */
3076
4323add6 3077static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
b97bf3fd
PL
3078{
3079 struct print_buf pb;
c4307285 3080 struct link *l_ptr;
6c00055a 3081 struct tipc_node *node;
b97bf3fd
PL
3082 char *status;
3083 u32 profile_total = 0;
3084
4323add6
PL
3085 if (!strcmp(name, tipc_bclink_name))
3086 return tipc_bclink_stats(buf, buf_size);
b97bf3fd 3087
4323add6 3088 tipc_printbuf_init(&pb, buf, buf_size);
b97bf3fd 3089
4323add6 3090 read_lock_bh(&tipc_net_lock);
c4307285 3091 l_ptr = link_find_link(name, &node);
b97bf3fd 3092 if (!l_ptr) {
4323add6 3093 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
3094 return 0;
3095 }
4323add6 3096 tipc_node_lock(node);
b97bf3fd 3097
4323add6 3098 if (tipc_link_is_active(l_ptr))
b97bf3fd 3099 status = "ACTIVE";
4323add6 3100 else if (tipc_link_is_up(l_ptr))
b97bf3fd
PL
3101 status = "STANDBY";
3102 else
3103 status = "DEFUNCT";
3104 tipc_printf(&pb, "Link <%s>\n"
c4307285
YH
3105 " %s MTU:%u Priority:%u Tolerance:%u ms"
3106 " Window:%u packets\n",
15e979da 3107 l_ptr->name, status, l_ptr->max_pkt,
b97bf3fd 3108 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
c4307285 3109 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
b97bf3fd
PL
3110 l_ptr->next_in_no - l_ptr->stats.recv_info,
3111 l_ptr->stats.recv_fragments,
3112 l_ptr->stats.recv_fragmented,
3113 l_ptr->stats.recv_bundles,
3114 l_ptr->stats.recv_bundled);
c4307285 3115 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
b97bf3fd
PL
3116 l_ptr->next_out_no - l_ptr->stats.sent_info,
3117 l_ptr->stats.sent_fragments,
c4307285 3118 l_ptr->stats.sent_fragmented,
b97bf3fd
PL
3119 l_ptr->stats.sent_bundles,
3120 l_ptr->stats.sent_bundled);
3121 profile_total = l_ptr->stats.msg_length_counts;
3122 if (!profile_total)
3123 profile_total = 1;
3124 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
c4307285
YH
3125 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
3126 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
b97bf3fd
PL
3127 l_ptr->stats.msg_length_counts,
3128 l_ptr->stats.msg_lengths_total / profile_total,
3129 percent(l_ptr->stats.msg_length_profile[0], profile_total),
3130 percent(l_ptr->stats.msg_length_profile[1], profile_total),
3131 percent(l_ptr->stats.msg_length_profile[2], profile_total),
3132 percent(l_ptr->stats.msg_length_profile[3], profile_total),
3133 percent(l_ptr->stats.msg_length_profile[4], profile_total),
3134 percent(l_ptr->stats.msg_length_profile[5], profile_total),
3135 percent(l_ptr->stats.msg_length_profile[6], profile_total));
c4307285 3136 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
b97bf3fd
PL
3137 l_ptr->stats.recv_states,
3138 l_ptr->stats.recv_probes,
3139 l_ptr->stats.recv_nacks,
c4307285 3140 l_ptr->stats.deferred_recv,
b97bf3fd 3141 l_ptr->stats.duplicates);
c4307285
YH
3142 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
3143 l_ptr->stats.sent_states,
3144 l_ptr->stats.sent_probes,
3145 l_ptr->stats.sent_nacks,
3146 l_ptr->stats.sent_acks,
b97bf3fd
PL
3147 l_ptr->stats.retransmitted);
3148 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
3149 l_ptr->stats.bearer_congs,
c4307285 3150 l_ptr->stats.link_congs,
b97bf3fd
PL
3151 l_ptr->stats.max_queue_sz,
3152 l_ptr->stats.queue_sz_counts
3153 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
3154 : 0);
3155
4323add6
PL
3156 tipc_node_unlock(node);
3157 read_unlock_bh(&tipc_net_lock);
3158 return tipc_printbuf_validate(&pb);
b97bf3fd
PL
3159}
3160
3161#define MAX_LINK_STATS_INFO 2000
3162
4323add6 3163struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
3164{
3165 struct sk_buff *buf;
3166 struct tlv_desc *rep_tlv;
3167 int str_len;
3168
3169 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
4323add6 3170 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 3171
4323add6 3172 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
b97bf3fd
PL
3173 if (!buf)
3174 return NULL;
3175
3176 rep_tlv = (struct tlv_desc *)buf->data;
3177
4323add6
PL
3178 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
3179 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
b97bf3fd
PL
3180 if (!str_len) {
3181 buf_discard(buf);
c4307285 3182 return tipc_cfg_reply_error_string("link not found");
b97bf3fd
PL
3183 }
3184
3185 skb_put(buf, TLV_SPACE(str_len));
3186 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
3187
3188 return buf;
3189}
3190
3191#if 0
3192int link_control(const char *name, u32 op, u32 val)
3193{
3194 int res = -EINVAL;
3195 struct link *l_ptr;
3196 u32 bearer_id;
6c00055a 3197 struct tipc_node * node;
b97bf3fd
PL
3198 u32 a;
3199
3200 a = link_name2addr(name, &bearer_id);
4323add6
PL
3201 read_lock_bh(&tipc_net_lock);
3202 node = tipc_node_find(a);
b97bf3fd 3203 if (node) {
4323add6 3204 tipc_node_lock(node);
b97bf3fd
PL
3205 l_ptr = node->links[bearer_id];
3206 if (l_ptr) {
3207 if (op == TIPC_REMOVE_LINK) {
3208 struct bearer *b_ptr = l_ptr->b_ptr;
3209 spin_lock_bh(&b_ptr->publ.lock);
4323add6 3210 tipc_link_delete(l_ptr);
b97bf3fd
PL
3211 spin_unlock_bh(&b_ptr->publ.lock);
3212 }
3213 if (op == TIPC_CMD_BLOCK_LINK) {
4323add6 3214 tipc_link_reset(l_ptr);
b97bf3fd
PL
3215 l_ptr->blocked = 1;
3216 }
3217 if (op == TIPC_CMD_UNBLOCK_LINK) {
3218 l_ptr->blocked = 0;
3219 }
0e35fd5e 3220 res = 0;
b97bf3fd 3221 }
4323add6 3222 tipc_node_unlock(node);
b97bf3fd 3223 }
4323add6 3224 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
3225 return res;
3226}
3227#endif
3228
3229/**
4323add6 3230 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
b97bf3fd
PL
3231 * @dest: network address of destination node
3232 * @selector: used to select from set of active links
c4307285 3233 *
b97bf3fd
PL
3234 * If no active link can be found, uses default maximum packet size.
3235 */
3236
4323add6 3237u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
b97bf3fd 3238{
6c00055a 3239 struct tipc_node *n_ptr;
b97bf3fd
PL
3240 struct link *l_ptr;
3241 u32 res = MAX_PKT_DEFAULT;
c4307285 3242
b97bf3fd
PL
3243 if (dest == tipc_own_addr)
3244 return MAX_MSG_SIZE;
3245
c4307285 3246 read_lock_bh(&tipc_net_lock);
4323add6 3247 n_ptr = tipc_node_select(dest, selector);
b97bf3fd 3248 if (n_ptr) {
4323add6 3249 tipc_node_lock(n_ptr);
b97bf3fd
PL
3250 l_ptr = n_ptr->active_links[selector & 1];
3251 if (l_ptr)
15e979da 3252 res = l_ptr->max_pkt;
4323add6 3253 tipc_node_unlock(n_ptr);
b97bf3fd 3254 }
c4307285 3255 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
3256 return res;
3257}
3258
3259#if 0
3260static void link_dump_rec_queue(struct link *l_ptr)
3261{
3262 struct sk_buff *crs;
3263
3264 if (!l_ptr->oldest_deferred_in) {
3265 info("Reception queue empty\n");
3266 return;
3267 }
3268 info("Contents of Reception queue:\n");
3269 crs = l_ptr->oldest_deferred_in;
3270 while (crs) {
3271 if (crs->data == (void *)0x0000a3a3) {
3272 info("buffer %x invalid\n", crs);
3273 return;
3274 }
a570f095 3275 msg_dbg(buf_msg(crs), "In rec queue:\n");
b97bf3fd
PL
3276 crs = crs->next;
3277 }
3278}
3279#endif
3280
3281static void link_dump_send_queue(struct link *l_ptr)
3282{
3283 if (l_ptr->next_out) {
3284 info("\nContents of unsent queue:\n");
3285 dbg_print_buf_chain(l_ptr->next_out);
3286 }
3287 info("\nContents of send queue:\n");
3288 if (l_ptr->first_out) {
3289 dbg_print_buf_chain(l_ptr->first_out);
3290 }
3291 info("Empty send queue\n");
3292}
3293
3294static void link_print(struct link *l_ptr, struct print_buf *buf,
3295 const char *str)
3296{
3297 tipc_printf(buf, str);
3298 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
3299 return;
3300 tipc_printf(buf, "Link %x<%s>:",
3301 l_ptr->addr, l_ptr->b_ptr->publ.name);
3302 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
3303 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
3304 tipc_printf(buf, "SQUE");
3305 if (l_ptr->first_out) {
3306 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
3307 if (l_ptr->next_out)
3308 tipc_printf(buf, "%u..",
3309 msg_seqno(buf_msg(l_ptr->next_out)));
b82834e6 3310 tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
c4307285
YH
3311 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
3312 msg_seqno(buf_msg(l_ptr->first_out)))
f64f9e71
JP
3313 != (l_ptr->out_queue_size - 1)) ||
3314 (l_ptr->last_out->next != NULL)) {
b97bf3fd
PL
3315 tipc_printf(buf, "\nSend queue inconsistency\n");
3316 tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
3317 tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
3318 tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
3319 link_dump_send_queue(l_ptr);
3320 }
3321 } else
3322 tipc_printf(buf, "[]");
3323 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
3324 if (l_ptr->oldest_deferred_in) {
3325 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
3326 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
3327 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
3328 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
3329 tipc_printf(buf, ":RQSIZ(%u)",
3330 l_ptr->deferred_inqueue_sz);
3331 }
3332 }
3333 if (link_working_unknown(l_ptr))
3334 tipc_printf(buf, ":WU");
3335 if (link_reset_reset(l_ptr))
3336 tipc_printf(buf, ":RR");
3337 if (link_reset_unknown(l_ptr))
3338 tipc_printf(buf, ":RU");
3339 if (link_working_working(l_ptr))
3340 tipc_printf(buf, ":WW");
3341 tipc_printf(buf, "\n");
3342}
3343