blob: 2efced5a673c5478fdd3ce5c3ca5f332ebac8193 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/link.c: TIPC link code
3 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 1996-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2004-2005, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "dbg.h"
39#include "link.h"
40#include "net.h"
41#include "node.h"
42#include "port.h"
43#include "addr.h"
44#include "node_subscr.h"
45#include "name_distr.h"
46#include "bearer.h"
47#include "name_table.h"
48#include "discover.h"
49#include "config.h"
50#include "bcast.h"
51
52
53/*
54 * Limit for deferred reception queue:
55 */
56
57#define DEF_QUEUE_LIMIT 256u
58
59/*
60 * Link state events:
61 */
62
63#define STARTING_EVT 856384768 /* link processing trigger */
64#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
65#define TIMEOUT_EVT 560817u /* link timer expired */
66
67/*
68 * The following two 'message types' is really just implementation
69 * data conveniently stored in the message header.
70 * They must not be considered part of the protocol
71 */
72#define OPEN_MSG 0
73#define CLOSED_MSG 1
74
75/*
76 * State value stored in 'exp_msg_count'
77 */
78
79#define START_CHANGEOVER 100000u
80
81/**
82 * struct link_name - deconstructed link name
83 * @addr_local: network address of node at this end
84 * @if_local: name of interface at this end
85 * @addr_peer: network address of node at far end
86 * @if_peer: name of interface at far end
87 */
88
89struct link_name {
90 u32 addr_local;
91 char if_local[TIPC_MAX_IF_NAME];
92 u32 addr_peer;
93 char if_peer[TIPC_MAX_IF_NAME];
94};
95
96#if 0
97
98/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
99
100/**
101 * struct link_event - link up/down event notification
102 */
103
104struct link_event {
105 u32 addr;
106 int up;
107 void (*fcn)(u32, char *, int);
108 char name[TIPC_MAX_LINK_NAME];
109};
110
111#endif
112
113static void link_handle_out_of_seq_msg(struct link *l_ptr,
114 struct sk_buff *buf);
115static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
116static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
117static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
118static int link_send_sections_long(struct port *sender,
119 struct iovec const *msg_sect,
120 u32 num_sect, u32 destnode);
121static void link_check_defragm_bufs(struct link *l_ptr);
122static void link_state_event(struct link *l_ptr, u32 event);
123static void link_reset_statistics(struct link *l_ptr);
124static void link_print(struct link *l_ptr, struct print_buf *buf,
125 const char *str);
126
127/*
128 * Debugging code used by link routines only
129 *
130 * When debugging link problems on a system that has multiple links,
131 * the standard TIPC debugging routines may not be useful since they
132 * allow the output from multiple links to be intermixed. For this reason
133 * routines of the form "dbg_link_XXX()" have been created that will capture
134 * debug info into a link's personal print buffer, which can then be dumped
135 * into the TIPC system log (LOG) upon request.
136 *
137 * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size
138 * of the print buffer used by each link. If LINK_LOG_BUF_SIZE is set to 0,
139 * the dbg_link_XXX() routines simply send their output to the standard
140 * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful
141 * when there is only a single link in the system being debugged.
142 *
143 * Notes:
144 * - When enabled, LINK_LOG_BUF_SIZE should be set to at least 1000 (bytes)
145 * - "l_ptr" must be valid when using dbg_link_XXX() macros
146 */
147
148#define LINK_LOG_BUF_SIZE 0
149
150#define dbg_link(fmt, arg...) do {if (LINK_LOG_BUF_SIZE) tipc_printf(&l_ptr->print_buf, fmt, ## arg); } while(0)
Per Liden4323add2006-01-18 00:38:21 +0100151#define dbg_link_msg(msg, txt) do {if (LINK_LOG_BUF_SIZE) tipc_msg_print(&l_ptr->print_buf, msg, txt); } while(0)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152#define dbg_link_state(txt) do {if (LINK_LOG_BUF_SIZE) link_print(l_ptr, &l_ptr->print_buf, txt); } while(0)
153#define dbg_link_dump() do { \
154 if (LINK_LOG_BUF_SIZE) { \
155 tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \
Per Liden4323add2006-01-18 00:38:21 +0100156 tipc_printbuf_move(LOG, &l_ptr->print_buf); \
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 } \
158} while (0)
159
Sam Ravnborg05790c62006-03-20 22:37:04 -0800160static void dbg_print_link(struct link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161{
162 if (DBG_OUTPUT)
163 link_print(l_ptr, DBG_OUTPUT, str);
164}
165
Sam Ravnborg05790c62006-03-20 22:37:04 -0800166static void dbg_print_buf_chain(struct sk_buff *root_buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167{
168 if (DBG_OUTPUT) {
169 struct sk_buff *buf = root_buf;
170
171 while (buf) {
172 msg_dbg(buf_msg(buf), "In chain: ");
173 buf = buf->next;
174 }
175 }
176}
177
178/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800179 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 */
181
Sam Ravnborg05790c62006-03-20 22:37:04 -0800182static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183{
184 return (i + 3) & ~3u;
185}
186
Sam Ravnborg05790c62006-03-20 22:37:04 -0800187static int link_working_working(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188{
189 return (l_ptr->state == WORKING_WORKING);
190}
191
Sam Ravnborg05790c62006-03-20 22:37:04 -0800192static int link_working_unknown(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193{
194 return (l_ptr->state == WORKING_UNKNOWN);
195}
196
Sam Ravnborg05790c62006-03-20 22:37:04 -0800197static int link_reset_unknown(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198{
199 return (l_ptr->state == RESET_UNKNOWN);
200}
201
Sam Ravnborg05790c62006-03-20 22:37:04 -0800202static int link_reset_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203{
204 return (l_ptr->state == RESET_RESET);
205}
206
Sam Ravnborg05790c62006-03-20 22:37:04 -0800207static int link_blocked(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208{
209 return (l_ptr->exp_msg_count || l_ptr->blocked);
210}
211
Sam Ravnborg05790c62006-03-20 22:37:04 -0800212static int link_congested(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213{
214 return (l_ptr->out_queue_size >= l_ptr->queue_limit[0]);
215}
216
Sam Ravnborg05790c62006-03-20 22:37:04 -0800217static u32 link_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218{
219 return l_ptr->max_pkt;
220}
221
Sam Ravnborg05790c62006-03-20 22:37:04 -0800222static void link_init_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223{
224 u32 max_pkt;
225
226 max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
227 if (max_pkt > MAX_MSG_SIZE)
228 max_pkt = MAX_MSG_SIZE;
229
230 l_ptr->max_pkt_target = max_pkt;
231 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
232 l_ptr->max_pkt = l_ptr->max_pkt_target;
233 else
234 l_ptr->max_pkt = MAX_PKT_DEFAULT;
235
236 l_ptr->max_pkt_probes = 0;
237}
238
Sam Ravnborg05790c62006-03-20 22:37:04 -0800239static u32 link_next_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240{
241 if (l_ptr->next_out)
242 return msg_seqno(buf_msg(l_ptr->next_out));
243 return mod(l_ptr->next_out_no);
244}
245
Sam Ravnborg05790c62006-03-20 22:37:04 -0800246static u32 link_last_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247{
248 return mod(link_next_sent(l_ptr) - 1);
249}
250
251/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800252 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 */
254
Per Liden4323add2006-01-18 00:38:21 +0100255int tipc_link_is_up(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256{
257 if (!l_ptr)
258 return 0;
259 return (link_working_working(l_ptr) || link_working_unknown(l_ptr));
260}
261
Per Liden4323add2006-01-18 00:38:21 +0100262int tipc_link_is_active(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263{
264 return ((l_ptr->owner->active_links[0] == l_ptr) ||
265 (l_ptr->owner->active_links[1] == l_ptr));
266}
267
268/**
269 * link_name_validate - validate & (optionally) deconstruct link name
270 * @name - ptr to link name string
271 * @name_parts - ptr to area for link name components (or NULL if not needed)
272 *
273 * Returns 1 if link name is valid, otherwise 0.
274 */
275
276static int link_name_validate(const char *name, struct link_name *name_parts)
277{
278 char name_copy[TIPC_MAX_LINK_NAME];
279 char *addr_local;
280 char *if_local;
281 char *addr_peer;
282 char *if_peer;
283 char dummy;
284 u32 z_local, c_local, n_local;
285 u32 z_peer, c_peer, n_peer;
286 u32 if_local_len;
287 u32 if_peer_len;
288
289 /* copy link name & ensure length is OK */
290
291 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
292 /* need above in case non-Posix strncpy() doesn't pad with nulls */
293 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
294 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
295 return 0;
296
297 /* ensure all component parts of link name are present */
298
299 addr_local = name_copy;
300 if ((if_local = strchr(addr_local, ':')) == NULL)
301 return 0;
302 *(if_local++) = 0;
303 if ((addr_peer = strchr(if_local, '-')) == NULL)
304 return 0;
305 *(addr_peer++) = 0;
306 if_local_len = addr_peer - if_local;
307 if ((if_peer = strchr(addr_peer, ':')) == NULL)
308 return 0;
309 *(if_peer++) = 0;
310 if_peer_len = strlen(if_peer) + 1;
311
312 /* validate component parts of link name */
313
314 if ((sscanf(addr_local, "%u.%u.%u%c",
315 &z_local, &c_local, &n_local, &dummy) != 3) ||
316 (sscanf(addr_peer, "%u.%u.%u%c",
317 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
318 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
319 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
320 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
321 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
322 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
323 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
324 return 0;
325
326 /* return link name components, if necessary */
327
328 if (name_parts) {
329 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
330 strcpy(name_parts->if_local, if_local);
331 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
332 strcpy(name_parts->if_peer, if_peer);
333 }
334 return 1;
335}
336
337/**
338 * link_timeout - handle expiration of link timer
339 * @l_ptr: pointer to link
340 *
Per Liden4323add2006-01-18 00:38:21 +0100341 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
342 * with tipc_link_delete(). (There is no risk that the node will be deleted by
343 * another thread because tipc_link_delete() always cancels the link timer before
344 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 */
346
347static void link_timeout(struct link *l_ptr)
348{
Per Liden4323add2006-01-18 00:38:21 +0100349 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350
351 /* update counters used in statistical profiling of send traffic */
352
353 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
354 l_ptr->stats.queue_sz_counts++;
355
356 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
357 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
358
359 if (l_ptr->first_out) {
360 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
361 u32 length = msg_size(msg);
362
363 if ((msg_user(msg) == MSG_FRAGMENTER)
364 && (msg_type(msg) == FIRST_FRAGMENT)) {
365 length = msg_size(msg_get_wrapped(msg));
366 }
367 if (length) {
368 l_ptr->stats.msg_lengths_total += length;
369 l_ptr->stats.msg_length_counts++;
370 if (length <= 64)
371 l_ptr->stats.msg_length_profile[0]++;
372 else if (length <= 256)
373 l_ptr->stats.msg_length_profile[1]++;
374 else if (length <= 1024)
375 l_ptr->stats.msg_length_profile[2]++;
376 else if (length <= 4096)
377 l_ptr->stats.msg_length_profile[3]++;
378 else if (length <= 16384)
379 l_ptr->stats.msg_length_profile[4]++;
380 else if (length <= 32768)
381 l_ptr->stats.msg_length_profile[5]++;
382 else
383 l_ptr->stats.msg_length_profile[6]++;
384 }
385 }
386
387 /* do all other link processing performed on a periodic basis */
388
389 link_check_defragm_bufs(l_ptr);
390
391 link_state_event(l_ptr, TIMEOUT_EVT);
392
393 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100394 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395
Per Liden4323add2006-01-18 00:38:21 +0100396 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397}
398
Sam Ravnborg05790c62006-03-20 22:37:04 -0800399static void link_set_timer(struct link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400{
401 k_start_timer(&l_ptr->timer, time);
402}
403
404/**
Per Liden4323add2006-01-18 00:38:21 +0100405 * tipc_link_create - create a new link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 * @b_ptr: pointer to associated bearer
407 * @peer: network address of node at other end of link
408 * @media_addr: media address to use when sending messages over link
409 *
410 * Returns pointer to link.
411 */
412
Per Liden4323add2006-01-18 00:38:21 +0100413struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
414 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415{
416 struct link *l_ptr;
417 struct tipc_msg *msg;
418 char *if_name;
419
420 l_ptr = (struct link *)kmalloc(sizeof(*l_ptr), GFP_ATOMIC);
421 if (!l_ptr) {
422 warn("Memory squeeze; Failed to create link\n");
423 return NULL;
424 }
425 memset(l_ptr, 0, sizeof(*l_ptr));
426
427 l_ptr->addr = peer;
428 if_name = strchr(b_ptr->publ.name, ':') + 1;
429 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
430 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
431 tipc_node(tipc_own_addr),
432 if_name,
433 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
434 /* note: peer i/f is appended to link name by reset/activate */
435 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
436 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
437 list_add_tail(&l_ptr->link_list, &b_ptr->links);
438 l_ptr->checkpoint = 1;
439 l_ptr->b_ptr = b_ptr;
440 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
441 l_ptr->state = RESET_UNKNOWN;
442
443 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
444 msg = l_ptr->pmsg;
445 msg_init(msg, LINK_PROTOCOL, RESET_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
446 msg_set_size(msg, sizeof(l_ptr->proto_msg));
447 msg_set_session(msg, tipc_random);
448 msg_set_bearer_id(msg, b_ptr->identity);
449 strcpy((char *)msg_data(msg), if_name);
450
451 l_ptr->priority = b_ptr->priority;
Per Liden4323add2006-01-18 00:38:21 +0100452 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453
454 link_init_max_pkt(l_ptr);
455
456 l_ptr->next_out_no = 1;
457 INIT_LIST_HEAD(&l_ptr->waiting_ports);
458
459 link_reset_statistics(l_ptr);
460
Per Liden4323add2006-01-18 00:38:21 +0100461 l_ptr->owner = tipc_node_attach_link(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 if (!l_ptr->owner) {
463 kfree(l_ptr);
464 return NULL;
465 }
466
467 if (LINK_LOG_BUF_SIZE) {
468 char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
469
470 if (!pb) {
471 kfree(l_ptr);
472 warn("Memory squeeze; Failed to create link\n");
473 return NULL;
474 }
Per Liden4323add2006-01-18 00:38:21 +0100475 tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476 }
477
Per Liden4323add2006-01-18 00:38:21 +0100478 tipc_k_signal((Handler)tipc_link_start, (unsigned long)l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
Per Liden4323add2006-01-18 00:38:21 +0100480 dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit);
482
483 return l_ptr;
484}
485
486/**
Per Liden4323add2006-01-18 00:38:21 +0100487 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 * @l_ptr: pointer to link
489 *
Per Liden4323add2006-01-18 00:38:21 +0100490 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 * This routine must not grab the node lock until after link timer cancellation
492 * to avoid a potential deadlock situation.
493 */
494
Per Liden4323add2006-01-18 00:38:21 +0100495void tipc_link_delete(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496{
497 if (!l_ptr) {
498 err("Attempt to delete non-existent link\n");
499 return;
500 }
501
Per Liden4323add2006-01-18 00:38:21 +0100502 dbg("tipc_link_delete()\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503
504 k_cancel_timer(&l_ptr->timer);
505
Per Liden4323add2006-01-18 00:38:21 +0100506 tipc_node_lock(l_ptr->owner);
507 tipc_link_reset(l_ptr);
508 tipc_node_detach_link(l_ptr->owner, l_ptr);
509 tipc_link_stop(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 list_del_init(&l_ptr->link_list);
511 if (LINK_LOG_BUF_SIZE)
512 kfree(l_ptr->print_buf.buf);
Per Liden4323add2006-01-18 00:38:21 +0100513 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 k_term_timer(&l_ptr->timer);
515 kfree(l_ptr);
516}
517
Per Liden4323add2006-01-18 00:38:21 +0100518void tipc_link_start(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519{
Per Liden4323add2006-01-18 00:38:21 +0100520 dbg("tipc_link_start %x\n", l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 link_state_event(l_ptr, STARTING_EVT);
522}
523
524/**
525 * link_schedule_port - schedule port for deferred sending
526 * @l_ptr: pointer to link
527 * @origport: reference to sending port
528 * @sz: amount of data to be sent
529 *
530 * Schedules port for renewed sending of messages after link congestion
531 * has abated.
532 */
533
534static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
535{
536 struct port *p_ptr;
537
Per Liden4323add2006-01-18 00:38:21 +0100538 spin_lock_bh(&tipc_port_list_lock);
539 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 if (p_ptr) {
541 if (!p_ptr->wakeup)
542 goto exit;
543 if (!list_empty(&p_ptr->wait_list))
544 goto exit;
545 p_ptr->congested_link = l_ptr;
546 p_ptr->publ.congested = 1;
547 p_ptr->waiting_pkts = 1 + ((sz - 1) / link_max_pkt(l_ptr));
548 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
549 l_ptr->stats.link_congs++;
550exit:
Per Liden4323add2006-01-18 00:38:21 +0100551 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 }
Per Liden4323add2006-01-18 00:38:21 +0100553 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 return -ELINKCONG;
555}
556
Per Liden4323add2006-01-18 00:38:21 +0100557void tipc_link_wakeup_ports(struct link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558{
559 struct port *p_ptr;
560 struct port *temp_p_ptr;
561 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
562
563 if (all)
564 win = 100000;
565 if (win <= 0)
566 return;
Per Liden4323add2006-01-18 00:38:21 +0100567 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 return;
569 if (link_congested(l_ptr))
570 goto exit;
571 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
572 wait_list) {
573 if (win <= 0)
574 break;
575 list_del_init(&p_ptr->wait_list);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800576 p_ptr->congested_link = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 spin_lock_bh(p_ptr->publ.lock);
578 p_ptr->publ.congested = 0;
579 p_ptr->wakeup(&p_ptr->publ);
580 win -= p_ptr->waiting_pkts;
581 spin_unlock_bh(p_ptr->publ.lock);
582 }
583
584exit:
Per Liden4323add2006-01-18 00:38:21 +0100585 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586}
587
588/**
589 * link_release_outqueue - purge link's outbound message queue
590 * @l_ptr: pointer to link
591 */
592
593static void link_release_outqueue(struct link *l_ptr)
594{
595 struct sk_buff *buf = l_ptr->first_out;
596 struct sk_buff *next;
597
598 while (buf) {
599 next = buf->next;
600 buf_discard(buf);
601 buf = next;
602 }
603 l_ptr->first_out = NULL;
604 l_ptr->out_queue_size = 0;
605}
606
607/**
Per Liden4323add2006-01-18 00:38:21 +0100608 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 * @l_ptr: pointer to link
610 */
611
Per Liden4323add2006-01-18 00:38:21 +0100612void tipc_link_reset_fragments(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613{
614 struct sk_buff *buf = l_ptr->defragm_buf;
615 struct sk_buff *next;
616
617 while (buf) {
618 next = buf->next;
619 buf_discard(buf);
620 buf = next;
621 }
622 l_ptr->defragm_buf = NULL;
623}
624
625/**
Per Liden4323add2006-01-18 00:38:21 +0100626 * tipc_link_stop - purge all inbound and outbound messages associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 * @l_ptr: pointer to link
628 */
629
Per Liden4323add2006-01-18 00:38:21 +0100630void tipc_link_stop(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631{
632 struct sk_buff *buf;
633 struct sk_buff *next;
634
635 buf = l_ptr->oldest_deferred_in;
636 while (buf) {
637 next = buf->next;
638 buf_discard(buf);
639 buf = next;
640 }
641
642 buf = l_ptr->first_out;
643 while (buf) {
644 next = buf->next;
645 buf_discard(buf);
646 buf = next;
647 }
648
Per Liden4323add2006-01-18 00:38:21 +0100649 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650
651 buf_discard(l_ptr->proto_msg_queue);
652 l_ptr->proto_msg_queue = NULL;
653}
654
655#if 0
656
657/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
658
659static void link_recv_event(struct link_event *ev)
660{
661 ev->fcn(ev->addr, ev->name, ev->up);
662 kfree(ev);
663}
664
665static void link_send_event(void (*fcn)(u32 a, char *n, int up),
666 struct link *l_ptr, int up)
667{
668 struct link_event *ev;
669
670 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
671 if (!ev) {
672 warn("Link event allocation failure\n");
673 return;
674 }
675 ev->addr = l_ptr->addr;
676 ev->up = up;
677 ev->fcn = fcn;
678 memcpy(ev->name, l_ptr->name, TIPC_MAX_LINK_NAME);
Per Liden4323add2006-01-18 00:38:21 +0100679 tipc_k_signal((Handler)link_recv_event, (unsigned long)ev);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680}
681
682#else
683
684#define link_send_event(fcn, l_ptr, up) do { } while (0)
685
686#endif
687
Per Liden4323add2006-01-18 00:38:21 +0100688void tipc_link_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689{
690 struct sk_buff *buf;
691 u32 prev_state = l_ptr->state;
692 u32 checkpoint = l_ptr->next_in_no;
693
694 msg_set_session(l_ptr->pmsg, msg_session(l_ptr->pmsg) + 1);
695
696 /* Link is down, accept any session: */
697 l_ptr->peer_session = 0;
698
699 /* Prepare for max packet size negotiation */
700 link_init_max_pkt(l_ptr);
701
702 l_ptr->state = RESET_UNKNOWN;
703 dbg_link_state("Resetting Link\n");
704
705 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
706 return;
707
Per Liden4323add2006-01-18 00:38:21 +0100708 tipc_node_link_down(l_ptr->owner, l_ptr);
709 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710#if 0
Per Liden4323add2006-01-18 00:38:21 +0100711 tipc_printf(TIPC_CONS, "\nReset link <%s>\n", l_ptr->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 dbg_link_dump();
713#endif
Per Liden4323add2006-01-18 00:38:21 +0100714 if (tipc_node_has_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 l_ptr->owner->permit_changeover) {
716 l_ptr->reset_checkpoint = checkpoint;
717 l_ptr->exp_msg_count = START_CHANGEOVER;
718 }
719
720 /* Clean up all queues: */
721
722 link_release_outqueue(l_ptr);
723 buf_discard(l_ptr->proto_msg_queue);
724 l_ptr->proto_msg_queue = NULL;
725 buf = l_ptr->oldest_deferred_in;
726 while (buf) {
727 struct sk_buff *next = buf->next;
728 buf_discard(buf);
729 buf = next;
730 }
731 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100732 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733
734 l_ptr->retransm_queue_head = 0;
735 l_ptr->retransm_queue_size = 0;
736 l_ptr->last_out = NULL;
737 l_ptr->first_out = NULL;
738 l_ptr->next_out = NULL;
739 l_ptr->unacked_window = 0;
740 l_ptr->checkpoint = 1;
741 l_ptr->next_out_no = 1;
742 l_ptr->deferred_inqueue_sz = 0;
743 l_ptr->oldest_deferred_in = NULL;
744 l_ptr->newest_deferred_in = NULL;
745 l_ptr->fsm_msg_cnt = 0;
746 l_ptr->stale_count = 0;
747 link_reset_statistics(l_ptr);
748
Per Liden4323add2006-01-18 00:38:21 +0100749 link_send_event(tipc_cfg_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100751 link_send_event(tipc_disc_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752}
753
754
755static void link_activate(struct link *l_ptr)
756{
757 l_ptr->next_in_no = 1;
Per Liden4323add2006-01-18 00:38:21 +0100758 tipc_node_link_up(l_ptr->owner, l_ptr);
759 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
760 link_send_event(tipc_cfg_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100762 link_send_event(tipc_disc_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763}
764
765/**
766 * link_state_event - link finite state machine
767 * @l_ptr: pointer to link
768 * @event: state machine event to process
769 */
770
771static void link_state_event(struct link *l_ptr, unsigned event)
772{
773 struct link *other;
774 u32 cont_intv = l_ptr->continuity_interval;
775
776 if (!l_ptr->started && (event != STARTING_EVT))
777 return; /* Not yet. */
778
779 if (link_blocked(l_ptr)) {
780 if (event == TIMEOUT_EVT) {
781 link_set_timer(l_ptr, cont_intv);
782 }
783 return; /* Changeover going on */
784 }
785 dbg_link("STATE_EV: <%s> ", l_ptr->name);
786
787 switch (l_ptr->state) {
788 case WORKING_WORKING:
789 dbg_link("WW/");
790 switch (event) {
791 case TRAFFIC_MSG_EVT:
792 dbg_link("TRF-");
793 /* fall through */
794 case ACTIVATE_MSG:
795 dbg_link("ACT\n");
796 break;
797 case TIMEOUT_EVT:
798 dbg_link("TIM ");
799 if (l_ptr->next_in_no != l_ptr->checkpoint) {
800 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100801 if (tipc_bclink_acks_missing(l_ptr->owner)) {
802 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
803 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 l_ptr->fsm_msg_cnt++;
805 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Per Liden4323add2006-01-18 00:38:21 +0100806 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
807 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 l_ptr->fsm_msg_cnt++;
809 }
810 link_set_timer(l_ptr, cont_intv);
811 break;
812 }
813 dbg_link(" -> WU\n");
814 l_ptr->state = WORKING_UNKNOWN;
815 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100816 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 l_ptr->fsm_msg_cnt++;
818 link_set_timer(l_ptr, cont_intv / 4);
819 break;
820 case RESET_MSG:
821 dbg_link("RES -> RR\n");
Per Liden4323add2006-01-18 00:38:21 +0100822 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 l_ptr->state = RESET_RESET;
824 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100825 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 l_ptr->fsm_msg_cnt++;
827 link_set_timer(l_ptr, cont_intv);
828 break;
829 default:
830 err("Unknown link event %u in WW state\n", event);
831 }
832 break;
833 case WORKING_UNKNOWN:
834 dbg_link("WU/");
835 switch (event) {
836 case TRAFFIC_MSG_EVT:
837 dbg_link("TRF-");
838 case ACTIVATE_MSG:
839 dbg_link("ACT -> WW\n");
840 l_ptr->state = WORKING_WORKING;
841 l_ptr->fsm_msg_cnt = 0;
842 link_set_timer(l_ptr, cont_intv);
843 break;
844 case RESET_MSG:
845 dbg_link("RES -> RR\n");
Per Liden4323add2006-01-18 00:38:21 +0100846 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 l_ptr->state = RESET_RESET;
848 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100849 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 l_ptr->fsm_msg_cnt++;
851 link_set_timer(l_ptr, cont_intv);
852 break;
853 case TIMEOUT_EVT:
854 dbg_link("TIM ");
855 if (l_ptr->next_in_no != l_ptr->checkpoint) {
856 dbg_link("-> WW \n");
857 l_ptr->state = WORKING_WORKING;
858 l_ptr->fsm_msg_cnt = 0;
859 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100860 if (tipc_bclink_acks_missing(l_ptr->owner)) {
861 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
862 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 l_ptr->fsm_msg_cnt++;
864 }
865 link_set_timer(l_ptr, cont_intv);
866 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
867 dbg_link("Probing %u/%u,timer = %u ms)\n",
868 l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
869 cont_intv / 4);
Per Liden4323add2006-01-18 00:38:21 +0100870 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
871 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 l_ptr->fsm_msg_cnt++;
873 link_set_timer(l_ptr, cont_intv / 4);
874 } else { /* Link has failed */
875 dbg_link("-> RU (%u probes unanswered)\n",
876 l_ptr->fsm_msg_cnt);
Per Liden4323add2006-01-18 00:38:21 +0100877 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 l_ptr->state = RESET_UNKNOWN;
879 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100880 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
881 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 l_ptr->fsm_msg_cnt++;
883 link_set_timer(l_ptr, cont_intv);
884 }
885 break;
886 default:
887 err("Unknown link event %u in WU state\n", event);
888 }
889 break;
890 case RESET_UNKNOWN:
891 dbg_link("RU/");
892 switch (event) {
893 case TRAFFIC_MSG_EVT:
894 dbg_link("TRF-\n");
895 break;
896 case ACTIVATE_MSG:
897 other = l_ptr->owner->active_links[0];
898 if (other && link_working_unknown(other)) {
899 dbg_link("ACT\n");
900 break;
901 }
902 dbg_link("ACT -> WW\n");
903 l_ptr->state = WORKING_WORKING;
904 l_ptr->fsm_msg_cnt = 0;
905 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100906 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 l_ptr->fsm_msg_cnt++;
908 link_set_timer(l_ptr, cont_intv);
909 break;
910 case RESET_MSG:
911 dbg_link("RES \n");
912 dbg_link(" -> RR\n");
913 l_ptr->state = RESET_RESET;
914 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100915 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 l_ptr->fsm_msg_cnt++;
917 link_set_timer(l_ptr, cont_intv);
918 break;
919 case STARTING_EVT:
920 dbg_link("START-");
921 l_ptr->started = 1;
922 /* fall through */
923 case TIMEOUT_EVT:
924 dbg_link("TIM \n");
Per Liden4323add2006-01-18 00:38:21 +0100925 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 l_ptr->fsm_msg_cnt++;
927 link_set_timer(l_ptr, cont_intv);
928 break;
929 default:
930 err("Unknown link event %u in RU state\n", event);
931 }
932 break;
933 case RESET_RESET:
934 dbg_link("RR/ ");
935 switch (event) {
936 case TRAFFIC_MSG_EVT:
937 dbg_link("TRF-");
938 /* fall through */
939 case ACTIVATE_MSG:
940 other = l_ptr->owner->active_links[0];
941 if (other && link_working_unknown(other)) {
942 dbg_link("ACT\n");
943 break;
944 }
945 dbg_link("ACT -> WW\n");
946 l_ptr->state = WORKING_WORKING;
947 l_ptr->fsm_msg_cnt = 0;
948 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100949 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 l_ptr->fsm_msg_cnt++;
951 link_set_timer(l_ptr, cont_intv);
952 break;
953 case RESET_MSG:
954 dbg_link("RES\n");
955 break;
956 case TIMEOUT_EVT:
957 dbg_link("TIM\n");
Per Liden4323add2006-01-18 00:38:21 +0100958 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 l_ptr->fsm_msg_cnt++;
960 link_set_timer(l_ptr, cont_intv);
961 dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
962 break;
963 default:
964 err("Unknown link event %u in RR state\n", event);
965 }
966 break;
967 default:
968 err("Unknown link state %u/%u\n", l_ptr->state, event);
969 }
970}
971
972/*
973 * link_bundle_buf(): Append contents of a buffer to
974 * the tail of an existing one.
975 */
976
977static int link_bundle_buf(struct link *l_ptr,
978 struct sk_buff *bundler,
979 struct sk_buff *buf)
980{
981 struct tipc_msg *bundler_msg = buf_msg(bundler);
982 struct tipc_msg *msg = buf_msg(buf);
983 u32 size = msg_size(msg);
984 u32 to_pos = align(msg_size(bundler_msg));
985 u32 rest = link_max_pkt(l_ptr) - to_pos;
986
987 if (msg_user(bundler_msg) != MSG_BUNDLER)
988 return 0;
989 if (msg_type(bundler_msg) != OPEN_MSG)
990 return 0;
991 if (rest < align(size))
992 return 0;
993
994 skb_put(bundler, (to_pos - msg_size(bundler_msg)) + size);
995 memcpy(bundler->data + to_pos, buf->data, size);
996 msg_set_size(bundler_msg, to_pos + size);
997 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
998 dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
999 msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
1000 msg_dbg(msg, "PACKD:");
1001 buf_discard(buf);
1002 l_ptr->stats.sent_bundled++;
1003 return 1;
1004}
1005
Sam Ravnborg05790c62006-03-20 22:37:04 -08001006static void link_add_to_outqueue(struct link *l_ptr,
1007 struct sk_buff *buf,
1008 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009{
1010 u32 ack = mod(l_ptr->next_in_no - 1);
1011 u32 seqno = mod(l_ptr->next_out_no++);
1012
1013 msg_set_word(msg, 2, ((ack << 16) | seqno));
1014 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1015 buf->next = NULL;
1016 if (l_ptr->first_out) {
1017 l_ptr->last_out->next = buf;
1018 l_ptr->last_out = buf;
1019 } else
1020 l_ptr->first_out = l_ptr->last_out = buf;
1021 l_ptr->out_queue_size++;
1022}
1023
1024/*
Per Liden4323add2006-01-18 00:38:21 +01001025 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 * inside TIPC when the 'fast path' in tipc_send_buf
1027 * has failed, and from link_send()
1028 */
1029
Per Liden4323add2006-01-18 00:38:21 +01001030int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031{
1032 struct tipc_msg *msg = buf_msg(buf);
1033 u32 size = msg_size(msg);
1034 u32 dsz = msg_data_sz(msg);
1035 u32 queue_size = l_ptr->out_queue_size;
1036 u32 imp = msg_tot_importance(msg);
1037 u32 queue_limit = l_ptr->queue_limit[imp];
1038 u32 max_packet = link_max_pkt(l_ptr);
1039
1040 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
1041
1042 /* Match msg importance against queue limits: */
1043
1044 if (unlikely(queue_size >= queue_limit)) {
1045 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
1046 return link_schedule_port(l_ptr, msg_origport(msg),
1047 size);
1048 }
1049 msg_dbg(msg, "TIPC: Congestion, throwing away\n");
1050 buf_discard(buf);
1051 if (imp > CONN_MANAGER) {
1052 warn("Resetting <%s>, send queue full", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +01001053 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 }
1055 return dsz;
1056 }
1057
1058 /* Fragmentation needed ? */
1059
1060 if (size > max_packet)
Per Liden4323add2006-01-18 00:38:21 +01001061 return tipc_link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062
1063 /* Packet can be queued or sent: */
1064
1065 if (queue_size > l_ptr->stats.max_queue_sz)
1066 l_ptr->stats.max_queue_sz = queue_size;
1067
Per Liden4323add2006-01-18 00:38:21 +01001068 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 !link_congested(l_ptr))) {
1070 link_add_to_outqueue(l_ptr, buf, msg);
1071
Per Liden4323add2006-01-18 00:38:21 +01001072 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 l_ptr->unacked_window = 0;
1074 } else {
Per Liden4323add2006-01-18 00:38:21 +01001075 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 l_ptr->stats.bearer_congs++;
1077 l_ptr->next_out = buf;
1078 }
1079 return dsz;
1080 }
1081 /* Congestion: can message be bundled ?: */
1082
1083 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
1084 (msg_user(msg) != MSG_FRAGMENTER)) {
1085
1086 /* Try adding message to an existing bundle */
1087
1088 if (l_ptr->next_out &&
1089 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001090 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 return dsz;
1092 }
1093
1094 /* Try creating a new bundle */
1095
1096 if (size <= max_packet * 2 / 3) {
1097 struct sk_buff *bundler = buf_acquire(max_packet);
1098 struct tipc_msg bundler_hdr;
1099
1100 if (bundler) {
1101 msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
1102 TIPC_OK, INT_H_SIZE, l_ptr->addr);
1103 memcpy(bundler->data, (unchar *)&bundler_hdr,
1104 INT_H_SIZE);
1105 skb_trim(bundler, INT_H_SIZE);
1106 link_bundle_buf(l_ptr, bundler, buf);
1107 buf = bundler;
1108 msg = buf_msg(buf);
1109 l_ptr->stats.sent_bundles++;
1110 }
1111 }
1112 }
1113 if (!l_ptr->next_out)
1114 l_ptr->next_out = buf;
1115 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001116 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 return dsz;
1118}
1119
1120/*
Per Liden4323add2006-01-18 00:38:21 +01001121 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 * not been selected yet, and the the owner node is not locked
1123 * Called by TIPC internal users, e.g. the name distributor
1124 */
1125
Per Liden4323add2006-01-18 00:38:21 +01001126int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127{
1128 struct link *l_ptr;
1129 struct node *n_ptr;
1130 int res = -ELINKCONG;
1131
Per Liden4323add2006-01-18 00:38:21 +01001132 read_lock_bh(&tipc_net_lock);
1133 n_ptr = tipc_node_select(dest, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001135 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 l_ptr = n_ptr->active_links[selector & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 if (l_ptr) {
Allan Stephensc33d53b2006-06-25 23:50:30 -07001138 dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
Per Liden4323add2006-01-18 00:38:21 +01001139 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensc33d53b2006-06-25 23:50:30 -07001140 } else {
1141 dbg("Attempt to send msg to unreachable node:\n");
1142 msg_dbg(buf_msg(buf),">>>");
1143 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 }
Per Liden4323add2006-01-18 00:38:21 +01001145 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 } else {
1147 dbg("Attempt to send msg to unknown node:\n");
1148 msg_dbg(buf_msg(buf),">>>");
1149 buf_discard(buf);
1150 }
Per Liden4323add2006-01-18 00:38:21 +01001151 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 return res;
1153}
1154
1155/*
1156 * link_send_buf_fast: Entry for data messages where the
1157 * destination link is known and the header is complete,
1158 * inclusive total message length. Very time critical.
1159 * Link is locked. Returns user data length.
1160 */
1161
Sam Ravnborg05790c62006-03-20 22:37:04 -08001162static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
1163 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164{
1165 struct tipc_msg *msg = buf_msg(buf);
1166 int res = msg_data_sz(msg);
1167
1168 if (likely(!link_congested(l_ptr))) {
1169 if (likely(msg_size(msg) <= link_max_pkt(l_ptr))) {
1170 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
1171 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +01001172 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
1173 &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 l_ptr->unacked_window = 0;
1175 msg_dbg(msg,"SENT_FAST:");
1176 return res;
1177 }
1178 dbg("failed sent fast...\n");
Per Liden4323add2006-01-18 00:38:21 +01001179 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 l_ptr->stats.bearer_congs++;
1181 l_ptr->next_out = buf;
1182 return res;
1183 }
1184 }
1185 else
1186 *used_max_pkt = link_max_pkt(l_ptr);
1187 }
Per Liden4323add2006-01-18 00:38:21 +01001188 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189}
1190
1191/*
1192 * tipc_send_buf_fast: Entry for data messages where the
1193 * destination node is known and the header is complete,
1194 * inclusive total message length.
1195 * Returns user data length.
1196 */
1197int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
1198{
1199 struct link *l_ptr;
1200 struct node *n_ptr;
1201 int res;
1202 u32 selector = msg_origport(buf_msg(buf)) & 1;
1203 u32 dummy;
1204
1205 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001206 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207
Per Liden4323add2006-01-18 00:38:21 +01001208 read_lock_bh(&tipc_net_lock);
1209 n_ptr = tipc_node_select(destnode, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210 if (likely(n_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001211 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 l_ptr = n_ptr->active_links[selector];
1213 dbg("send_fast: buf %x selected %x, destnode = %x\n",
1214 buf, l_ptr, destnode);
1215 if (likely(l_ptr)) {
1216 res = link_send_buf_fast(l_ptr, buf, &dummy);
Per Liden4323add2006-01-18 00:38:21 +01001217 tipc_node_unlock(n_ptr);
1218 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 return res;
1220 }
Per Liden4323add2006-01-18 00:38:21 +01001221 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 }
Per Liden4323add2006-01-18 00:38:21 +01001223 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224 res = msg_data_sz(buf_msg(buf));
1225 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1226 return res;
1227}
1228
1229
1230/*
Per Liden4323add2006-01-18 00:38:21 +01001231 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 * destination processor is known and the header is complete,
1233 * except for total message length.
1234 * Returns user data length or errno.
1235 */
Per Liden4323add2006-01-18 00:38:21 +01001236int tipc_link_send_sections_fast(struct port *sender,
1237 struct iovec const *msg_sect,
1238 const u32 num_sect,
1239 u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240{
1241 struct tipc_msg *hdr = &sender->publ.phdr;
1242 struct link *l_ptr;
1243 struct sk_buff *buf;
1244 struct node *node;
1245 int res;
1246 u32 selector = msg_origport(hdr) & 1;
1247
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248again:
1249 /*
1250 * Try building message using port's max_pkt hint.
1251 * (Must not hold any locks while building message.)
1252 */
1253
1254 res = msg_build(hdr, msg_sect, num_sect, sender->max_pkt,
1255 !sender->user_port, &buf);
1256
Per Liden4323add2006-01-18 00:38:21 +01001257 read_lock_bh(&tipc_net_lock);
1258 node = tipc_node_select(destaddr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001260 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 l_ptr = node->active_links[selector];
1262 if (likely(l_ptr)) {
1263 if (likely(buf)) {
1264 res = link_send_buf_fast(l_ptr, buf,
1265 &sender->max_pkt);
1266 if (unlikely(res < 0))
1267 buf_discard(buf);
1268exit:
Per Liden4323add2006-01-18 00:38:21 +01001269 tipc_node_unlock(node);
1270 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 return res;
1272 }
1273
1274 /* Exit if build request was invalid */
1275
1276 if (unlikely(res < 0))
1277 goto exit;
1278
1279 /* Exit if link (or bearer) is congested */
1280
1281 if (link_congested(l_ptr) ||
1282 !list_empty(&l_ptr->b_ptr->cong_links)) {
1283 res = link_schedule_port(l_ptr,
1284 sender->publ.ref, res);
1285 goto exit;
1286 }
1287
1288 /*
1289 * Message size exceeds max_pkt hint; update hint,
1290 * then re-try fast path or fragment the message
1291 */
1292
1293 sender->max_pkt = link_max_pkt(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001294 tipc_node_unlock(node);
1295 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296
1297
1298 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
1299 goto again;
1300
1301 return link_send_sections_long(sender, msg_sect,
1302 num_sect, destaddr);
1303 }
Per Liden4323add2006-01-18 00:38:21 +01001304 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 }
Per Liden4323add2006-01-18 00:38:21 +01001306 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307
1308 /* Couldn't find a link to the destination node */
1309
1310 if (buf)
1311 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1312 if (res >= 0)
Per Liden4323add2006-01-18 00:38:21 +01001313 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1314 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 return res;
1316}
1317
1318/*
1319 * link_send_sections_long(): Entry for long messages where the
1320 * destination node is known and the header is complete,
1321 * inclusive total message length.
1322 * Link and bearer congestion status have been checked to be ok,
1323 * and are ignored if they change.
1324 *
1325 * Note that fragments do not use the full link MTU so that they won't have
1326 * to undergo refragmentation if link changeover causes them to be sent
1327 * over another link with an additional tunnel header added as prefix.
1328 * (Refragmentation will still occur if the other link has a smaller MTU.)
1329 *
1330 * Returns user data length or errno.
1331 */
1332static int link_send_sections_long(struct port *sender,
1333 struct iovec const *msg_sect,
1334 u32 num_sect,
1335 u32 destaddr)
1336{
1337 struct link *l_ptr;
1338 struct node *node;
1339 struct tipc_msg *hdr = &sender->publ.phdr;
1340 u32 dsz = msg_data_sz(hdr);
1341 u32 max_pkt,fragm_sz,rest;
1342 struct tipc_msg fragm_hdr;
1343 struct sk_buff *buf,*buf_chain,*prev;
1344 u32 fragm_crs,fragm_rest,hsz,sect_rest;
1345 const unchar *sect_crs;
1346 int curr_sect;
1347 u32 fragm_no;
1348
1349again:
1350 fragm_no = 1;
1351 max_pkt = sender->max_pkt - INT_H_SIZE;
1352 /* leave room for tunnel header in case of link changeover */
1353 fragm_sz = max_pkt - INT_H_SIZE;
1354 /* leave room for fragmentation header in each fragment */
1355 rest = dsz;
1356 fragm_crs = 0;
1357 fragm_rest = 0;
1358 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001359 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 curr_sect = -1;
1361
1362 /* Prepare reusable fragment header: */
1363
1364 msg_dbg(hdr, ">FRAGMENTING>");
1365 msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
1366 TIPC_OK, INT_H_SIZE, msg_destnode(hdr));
1367 msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1368 msg_set_size(&fragm_hdr, max_pkt);
1369 msg_set_fragm_no(&fragm_hdr, 1);
1370
1371 /* Prepare header of first fragment: */
1372
1373 buf_chain = buf = buf_acquire(max_pkt);
1374 if (!buf)
1375 return -ENOMEM;
1376 buf->next = NULL;
1377 memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE);
1378 hsz = msg_hdr_sz(hdr);
1379 memcpy(buf->data + INT_H_SIZE, (unchar *)hdr, hsz);
1380 msg_dbg(buf_msg(buf), ">BUILD>");
1381
1382 /* Chop up message: */
1383
1384 fragm_crs = INT_H_SIZE + hsz;
1385 fragm_rest = fragm_sz - hsz;
1386
1387 do { /* For all sections */
1388 u32 sz;
1389
1390 if (!sect_rest) {
1391 sect_rest = msg_sect[++curr_sect].iov_len;
1392 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1393 }
1394
1395 if (sect_rest < fragm_rest)
1396 sz = sect_rest;
1397 else
1398 sz = fragm_rest;
1399
1400 if (likely(!sender->user_port)) {
1401 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1402error:
1403 for (; buf_chain; buf_chain = buf) {
1404 buf = buf_chain->next;
1405 buf_discard(buf_chain);
1406 }
1407 return -EFAULT;
1408 }
1409 } else
1410 memcpy(buf->data + fragm_crs, sect_crs, sz);
1411
1412 sect_crs += sz;
1413 sect_rest -= sz;
1414 fragm_crs += sz;
1415 fragm_rest -= sz;
1416 rest -= sz;
1417
1418 if (!fragm_rest && rest) {
1419
1420 /* Initiate new fragment: */
1421 if (rest <= fragm_sz) {
1422 fragm_sz = rest;
1423 msg_set_type(&fragm_hdr,LAST_FRAGMENT);
1424 } else {
1425 msg_set_type(&fragm_hdr, FRAGMENT);
1426 }
1427 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1428 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1429 prev = buf;
1430 buf = buf_acquire(fragm_sz + INT_H_SIZE);
1431 if (!buf)
1432 goto error;
1433
1434 buf->next = NULL;
1435 prev->next = buf;
1436 memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE);
1437 fragm_crs = INT_H_SIZE;
1438 fragm_rest = fragm_sz;
1439 msg_dbg(buf_msg(buf)," >BUILD>");
1440 }
1441 }
1442 while (rest > 0);
1443
1444 /*
1445 * Now we have a buffer chain. Select a link and check
1446 * that packet size is still OK
1447 */
Per Liden4323add2006-01-18 00:38:21 +01001448 node = tipc_node_select(destaddr, sender->publ.ref & 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001450 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 l_ptr = node->active_links[sender->publ.ref & 1];
1452 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001453 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 goto reject;
1455 }
1456 if (link_max_pkt(l_ptr) < max_pkt) {
1457 sender->max_pkt = link_max_pkt(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001458 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 for (; buf_chain; buf_chain = buf) {
1460 buf = buf_chain->next;
1461 buf_discard(buf_chain);
1462 }
1463 goto again;
1464 }
1465 } else {
1466reject:
1467 for (; buf_chain; buf_chain = buf) {
1468 buf = buf_chain->next;
1469 buf_discard(buf_chain);
1470 }
Per Liden4323add2006-01-18 00:38:21 +01001471 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1472 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 }
1474
1475 /* Append whole chain to send queue: */
1476
1477 buf = buf_chain;
1478 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1479 if (!l_ptr->next_out)
1480 l_ptr->next_out = buf_chain;
1481 l_ptr->stats.sent_fragmented++;
1482 while (buf) {
1483 struct sk_buff *next = buf->next;
1484 struct tipc_msg *msg = buf_msg(buf);
1485
1486 l_ptr->stats.sent_fragments++;
1487 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1488 link_add_to_outqueue(l_ptr, buf, msg);
1489 msg_dbg(msg, ">ADD>");
1490 buf = next;
1491 }
1492
1493 /* Send it, if possible: */
1494
Per Liden4323add2006-01-18 00:38:21 +01001495 tipc_link_push_queue(l_ptr);
1496 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 return dsz;
1498}
1499
1500/*
Per Liden4323add2006-01-18 00:38:21 +01001501 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 */
Per Liden4323add2006-01-18 00:38:21 +01001503u32 tipc_link_push_packet(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504{
1505 struct sk_buff *buf = l_ptr->first_out;
1506 u32 r_q_size = l_ptr->retransm_queue_size;
1507 u32 r_q_head = l_ptr->retransm_queue_head;
1508
1509 /* Step to position where retransmission failed, if any, */
1510 /* consider that buffers may have been released in meantime */
1511
1512 if (r_q_size && buf) {
1513 u32 last = lesser(mod(r_q_head + r_q_size),
1514 link_last_sent(l_ptr));
1515 u32 first = msg_seqno(buf_msg(buf));
1516
1517 while (buf && less(first, r_q_head)) {
1518 first = mod(first + 1);
1519 buf = buf->next;
1520 }
1521 l_ptr->retransm_queue_head = r_q_head = first;
1522 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1523 }
1524
1525 /* Continue retransmission now, if there is anything: */
1526
1527 if (r_q_size && buf && !skb_cloned(buf)) {
1528 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1529 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001530 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001531 msg_dbg(buf_msg(buf), ">DEF-RETR>");
1532 l_ptr->retransm_queue_head = mod(++r_q_head);
1533 l_ptr->retransm_queue_size = --r_q_size;
1534 l_ptr->stats.retransmitted++;
1535 return TIPC_OK;
1536 } else {
1537 l_ptr->stats.bearer_congs++;
1538 msg_dbg(buf_msg(buf), "|>DEF-RETR>");
1539 return PUSH_FAILED;
1540 }
1541 }
1542
1543 /* Send deferred protocol message, if any: */
1544
1545 buf = l_ptr->proto_msg_queue;
1546 if (buf) {
1547 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1548 msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001549 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001550 msg_dbg(buf_msg(buf), ">DEF-PROT>");
1551 l_ptr->unacked_window = 0;
1552 buf_discard(buf);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001553 l_ptr->proto_msg_queue = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554 return TIPC_OK;
1555 } else {
1556 msg_dbg(buf_msg(buf), "|>DEF-PROT>");
1557 l_ptr->stats.bearer_congs++;
1558 return PUSH_FAILED;
1559 }
1560 }
1561
1562 /* Send one deferred data message, if send window not full: */
1563
1564 buf = l_ptr->next_out;
1565 if (buf) {
1566 struct tipc_msg *msg = buf_msg(buf);
1567 u32 next = msg_seqno(msg);
1568 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1569
1570 if (mod(next - first) < l_ptr->queue_limit[0]) {
1571 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1572 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001573 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 if (msg_user(msg) == MSG_BUNDLER)
1575 msg_set_type(msg, CLOSED_MSG);
1576 msg_dbg(msg, ">PUSH-DATA>");
1577 l_ptr->next_out = buf->next;
1578 return TIPC_OK;
1579 } else {
1580 msg_dbg(msg, "|PUSH-DATA|");
1581 l_ptr->stats.bearer_congs++;
1582 return PUSH_FAILED;
1583 }
1584 }
1585 }
1586 return PUSH_FINISHED;
1587}
1588
1589/*
1590 * push_queue(): push out the unsent messages of a link where
1591 * congestion has abated. Node is locked
1592 */
Per Liden4323add2006-01-18 00:38:21 +01001593void tipc_link_push_queue(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594{
1595 u32 res;
1596
Per Liden4323add2006-01-18 00:38:21 +01001597 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 return;
1599
1600 do {
Per Liden4323add2006-01-18 00:38:21 +01001601 res = tipc_link_push_packet(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602 }
1603 while (res == TIPC_OK);
1604 if (res == PUSH_FAILED)
Per Liden4323add2006-01-18 00:38:21 +01001605 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606}
1607
Allan Stephensd356eeb2006-06-25 23:40:01 -07001608static void link_reset_all(unsigned long addr)
1609{
1610 struct node *n_ptr;
1611 char addr_string[16];
1612 u32 i;
1613
1614 read_lock_bh(&tipc_net_lock);
1615 n_ptr = tipc_node_find((u32)addr);
1616 if (!n_ptr) {
1617 read_unlock_bh(&tipc_net_lock);
1618 return; /* node no longer exists */
1619 }
1620
1621 tipc_node_lock(n_ptr);
1622
1623 warn("Resetting all links to %s\n",
1624 addr_string_fill(addr_string, n_ptr->addr));
1625
1626 for (i = 0; i < MAX_BEARERS; i++) {
1627 if (n_ptr->links[i]) {
1628 link_print(n_ptr->links[i], TIPC_OUTPUT,
1629 "Resetting link\n");
1630 tipc_link_reset(n_ptr->links[i]);
1631 }
1632 }
1633
1634 tipc_node_unlock(n_ptr);
1635 read_unlock_bh(&tipc_net_lock);
1636}
1637
1638static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1639{
1640 struct tipc_msg *msg = buf_msg(buf);
1641
1642 warn("Retransmission failure on link <%s>\n", l_ptr->name);
1643 tipc_msg_print(TIPC_OUTPUT, msg, ">RETR-FAIL>");
1644
1645 if (l_ptr->addr) {
1646
1647 /* Handle failure on standard link */
1648
1649 link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
1650 tipc_link_reset(l_ptr);
1651
1652 } else {
1653
1654 /* Handle failure on broadcast link */
1655
1656 struct node *n_ptr;
1657 char addr_string[16];
1658
1659 tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg));
1660 tipc_printf(TIPC_OUTPUT, "Outstanding acks: %u\n", (u32)TIPC_SKB_CB(buf)->handle);
1661
1662 n_ptr = l_ptr->owner->next;
1663 tipc_node_lock(n_ptr);
1664
1665 addr_string_fill(addr_string, n_ptr->addr);
1666 tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
1667 tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported);
1668 tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
1669 tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in);
1670 tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after);
1671 tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
1672 tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1673
1674 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1675
1676 tipc_node_unlock(n_ptr);
1677
1678 l_ptr->stale_count = 0;
1679 }
1680}
1681
Per Liden4323add2006-01-18 00:38:21 +01001682void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
1683 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684{
1685 struct tipc_msg *msg;
1686
Allan Stephensd356eeb2006-06-25 23:40:01 -07001687 if (!buf)
1688 return;
1689
1690 msg = buf_msg(buf);
1691
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
1693
Allan Stephensd356eeb2006-06-25 23:40:01 -07001694 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
1695 if (!skb_cloned(buf)) {
1696 msg_dbg(msg, ">NO_RETR->BCONG>");
1697 dbg_print_link(l_ptr, " ");
1698 l_ptr->retransm_queue_head = msg_seqno(msg);
1699 l_ptr->retransm_queue_size = retransmits;
1700 return;
1701 } else {
1702 /* Don't retransmit if driver already has the buffer */
1703 }
1704 } else {
1705 /* Detect repeated retransmit failures on uncongested bearer */
1706
1707 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1708 if (++l_ptr->stale_count > 100) {
1709 link_retransmit_failure(l_ptr, buf);
1710 return;
1711 }
1712 } else {
1713 l_ptr->last_retransmitted = msg_seqno(msg);
1714 l_ptr->stale_count = 1;
1715 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001717
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718 while (retransmits && (buf != l_ptr->next_out) && buf && !skb_cloned(buf)) {
1719 msg = buf_msg(buf);
1720 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1721 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001722 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 msg_dbg(buf_msg(buf), ">RETR>");
1724 buf = buf->next;
1725 retransmits--;
1726 l_ptr->stats.retransmitted++;
1727 } else {
Per Liden4323add2006-01-18 00:38:21 +01001728 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729 l_ptr->stats.bearer_congs++;
1730 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1731 l_ptr->retransm_queue_size = retransmits;
1732 return;
1733 }
1734 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001735
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1737}
1738
1739/*
1740 * link_recv_non_seq: Receive packets which are outside
1741 * the link sequence flow
1742 */
1743
1744static void link_recv_non_seq(struct sk_buff *buf)
1745{
1746 struct tipc_msg *msg = buf_msg(buf);
1747
1748 if (msg_user(msg) == LINK_CONFIG)
Per Liden4323add2006-01-18 00:38:21 +01001749 tipc_disc_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 else
Per Liden4323add2006-01-18 00:38:21 +01001751 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752}
1753
1754/**
1755 * link_insert_deferred_queue - insert deferred messages back into receive chain
1756 */
1757
1758static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
1759 struct sk_buff *buf)
1760{
1761 u32 seq_no;
1762
1763 if (l_ptr->oldest_deferred_in == NULL)
1764 return buf;
1765
1766 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1767 if (seq_no == mod(l_ptr->next_in_no)) {
1768 l_ptr->newest_deferred_in->next = buf;
1769 buf = l_ptr->oldest_deferred_in;
1770 l_ptr->oldest_deferred_in = NULL;
1771 l_ptr->deferred_inqueue_sz = 0;
1772 }
1773 return buf;
1774}
1775
1776void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1777{
Per Liden4323add2006-01-18 00:38:21 +01001778 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779 while (head) {
1780 struct bearer *b_ptr;
1781 struct node *n_ptr;
1782 struct link *l_ptr;
1783 struct sk_buff *crs;
1784 struct sk_buff *buf = head;
1785 struct tipc_msg *msg = buf_msg(buf);
1786 u32 seq_no = msg_seqno(msg);
1787 u32 ackd = msg_ack(msg);
1788 u32 released = 0;
1789 int type;
1790
1791 b_ptr = (struct bearer *)tb_ptr;
1792 TIPC_SKB_CB(buf)->handle = b_ptr;
1793
1794 head = head->next;
1795 if (unlikely(msg_version(msg) != TIPC_VERSION))
1796 goto cont;
1797#if 0
1798 if (msg_user(msg) != LINK_PROTOCOL)
1799#endif
1800 msg_dbg(msg,"<REC<");
1801
1802 if (unlikely(msg_non_seq(msg))) {
1803 link_recv_non_seq(buf);
1804 continue;
1805 }
Allan Stephens26008242006-06-25 23:39:31 -07001806
1807 if (unlikely(!msg_short(msg) &&
1808 (msg_destnode(msg) != tipc_own_addr)))
1809 goto cont;
1810
Per Liden4323add2006-01-18 00:38:21 +01001811 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 if (unlikely(!n_ptr))
1813 goto cont;
1814
Per Liden4323add2006-01-18 00:38:21 +01001815 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 l_ptr = n_ptr->links[b_ptr->identity];
1817 if (unlikely(!l_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001818 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819 goto cont;
1820 }
1821 /*
1822 * Release acked messages
1823 */
1824 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001825 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1826 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 }
1828
1829 crs = l_ptr->first_out;
1830 while ((crs != l_ptr->next_out) &&
1831 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1832 struct sk_buff *next = crs->next;
1833
1834 buf_discard(crs);
1835 crs = next;
1836 released++;
1837 }
1838 if (released) {
1839 l_ptr->first_out = crs;
1840 l_ptr->out_queue_size -= released;
1841 }
1842 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001843 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001845 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1847 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001848 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849 }
1850
1851protocol_check:
1852 if (likely(link_working_working(l_ptr))) {
1853 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1854 l_ptr->next_in_no++;
1855 if (unlikely(l_ptr->oldest_deferred_in))
1856 head = link_insert_deferred_queue(l_ptr,
1857 head);
1858 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1859deliver:
1860 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001861 tipc_node_unlock(n_ptr);
1862 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 continue;
1864 }
1865 switch (msg_user(msg)) {
1866 case MSG_BUNDLER:
1867 l_ptr->stats.recv_bundles++;
1868 l_ptr->stats.recv_bundled +=
1869 msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +01001870 tipc_node_unlock(n_ptr);
1871 tipc_link_recv_bundle(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872 continue;
1873 case ROUTE_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001874 tipc_node_unlock(n_ptr);
1875 tipc_cltr_recv_routing_table(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 continue;
1877 case NAME_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001878 tipc_node_unlock(n_ptr);
1879 tipc_named_recv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880 continue;
1881 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001882 tipc_node_unlock(n_ptr);
1883 tipc_port_recv_proto_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 continue;
1885 case MSG_FRAGMENTER:
1886 l_ptr->stats.recv_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01001887 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
1888 &buf, &msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 l_ptr->stats.recv_fragmented++;
1890 goto deliver;
1891 }
1892 break;
1893 case CHANGEOVER_PROTOCOL:
1894 type = msg_type(msg);
Per Liden4323add2006-01-18 00:38:21 +01001895 if (link_recv_changeover_msg(&l_ptr, &buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896 msg = buf_msg(buf);
1897 seq_no = msg_seqno(msg);
1898 TIPC_SKB_CB(buf)->handle
1899 = b_ptr;
1900 if (type == ORIGINAL_MSG)
1901 goto deliver;
1902 goto protocol_check;
1903 }
1904 break;
1905 }
1906 }
Per Liden4323add2006-01-18 00:38:21 +01001907 tipc_node_unlock(n_ptr);
1908 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909 continue;
1910 }
1911 link_handle_out_of_seq_msg(l_ptr, buf);
1912 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001913 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914 continue;
1915 }
1916
1917 if (msg_user(msg) == LINK_PROTOCOL) {
1918 link_recv_proto_msg(l_ptr, buf);
1919 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001920 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921 continue;
1922 }
1923 msg_dbg(msg,"NSEQ<REC<");
1924 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1925
1926 if (link_working_working(l_ptr)) {
1927 /* Re-insert in front of queue */
1928 msg_dbg(msg,"RECV-REINS:");
1929 buf->next = head;
1930 head = buf;
Per Liden4323add2006-01-18 00:38:21 +01001931 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001932 continue;
1933 }
Per Liden4323add2006-01-18 00:38:21 +01001934 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935cont:
1936 buf_discard(buf);
1937 }
Per Liden4323add2006-01-18 00:38:21 +01001938 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939}
1940
1941/*
1942 * link_defer_buf(): Sort a received out-of-sequence packet
1943 * into the deferred reception queue.
1944 * Returns the increase of the queue length,i.e. 0 or 1
1945 */
1946
Per Liden4323add2006-01-18 00:38:21 +01001947u32 tipc_link_defer_pkt(struct sk_buff **head,
1948 struct sk_buff **tail,
1949 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001950{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001951 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952 struct sk_buff *crs = *head;
1953 u32 seq_no = msg_seqno(buf_msg(buf));
1954
1955 buf->next = NULL;
1956
1957 /* Empty queue ? */
1958 if (*head == NULL) {
1959 *head = *tail = buf;
1960 return 1;
1961 }
1962
1963 /* Last ? */
1964 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1965 (*tail)->next = buf;
1966 *tail = buf;
1967 return 1;
1968 }
1969
1970 /* Scan through queue and sort it in */
1971 do {
1972 struct tipc_msg *msg = buf_msg(crs);
1973
1974 if (less(seq_no, msg_seqno(msg))) {
1975 buf->next = crs;
1976 if (prev)
1977 prev->next = buf;
1978 else
1979 *head = buf;
1980 return 1;
1981 }
1982 if (seq_no == msg_seqno(msg)) {
1983 break;
1984 }
1985 prev = crs;
1986 crs = crs->next;
1987 }
1988 while (crs);
1989
1990 /* Message is a duplicate of an existing message */
1991
1992 buf_discard(buf);
1993 return 0;
1994}
1995
1996/**
1997 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1998 */
1999
2000static void link_handle_out_of_seq_msg(struct link *l_ptr,
2001 struct sk_buff *buf)
2002{
2003 u32 seq_no = msg_seqno(buf_msg(buf));
2004
2005 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
2006 link_recv_proto_msg(l_ptr, buf);
2007 return;
2008 }
2009
2010 dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
2011 seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
2012
2013 /* Record OOS packet arrival (force mismatch on next timeout) */
2014
2015 l_ptr->checkpoint--;
2016
2017 /*
2018 * Discard packet if a duplicate; otherwise add it to deferred queue
2019 * and notify peer of gap as per protocol specification
2020 */
2021
2022 if (less(seq_no, mod(l_ptr->next_in_no))) {
2023 l_ptr->stats.duplicates++;
2024 buf_discard(buf);
2025 return;
2026 }
2027
Per Liden4323add2006-01-18 00:38:21 +01002028 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
2029 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030 l_ptr->deferred_inqueue_sz++;
2031 l_ptr->stats.deferred_recv++;
2032 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01002033 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034 } else
2035 l_ptr->stats.duplicates++;
2036}
2037
2038/*
2039 * Send protocol message to the other endpoint.
2040 */
Per Liden4323add2006-01-18 00:38:21 +01002041void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
2042 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002043{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002044 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002045 struct tipc_msg *msg = l_ptr->pmsg;
2046 u32 msg_size = sizeof(l_ptr->proto_msg);
2047
2048 if (link_blocked(l_ptr))
2049 return;
2050 msg_set_type(msg, msg_typ);
2051 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
2052 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
Per Liden4323add2006-01-18 00:38:21 +01002053 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01002054
2055 if (msg_typ == STATE_MSG) {
2056 u32 next_sent = mod(l_ptr->next_out_no);
2057
Per Liden4323add2006-01-18 00:38:21 +01002058 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002059 return;
2060 if (l_ptr->next_out)
2061 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
2062 msg_set_next_sent(msg, next_sent);
2063 if (l_ptr->oldest_deferred_in) {
2064 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2065 gap = mod(rec - mod(l_ptr->next_in_no));
2066 }
2067 msg_set_seq_gap(msg, gap);
2068 if (gap)
2069 l_ptr->stats.sent_nacks++;
2070 msg_set_link_tolerance(msg, tolerance);
2071 msg_set_linkprio(msg, priority);
2072 msg_set_max_pkt(msg, ack_mtu);
2073 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
2074 msg_set_probe(msg, probe_msg != 0);
2075 if (probe_msg) {
2076 u32 mtu = l_ptr->max_pkt;
2077
2078 if ((mtu < l_ptr->max_pkt_target) &&
2079 link_working_working(l_ptr) &&
2080 l_ptr->fsm_msg_cnt) {
2081 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
2082 if (l_ptr->max_pkt_probes == 10) {
2083 l_ptr->max_pkt_target = (msg_size - 4);
2084 l_ptr->max_pkt_probes = 0;
2085 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
2086 }
2087 l_ptr->max_pkt_probes++;
2088 }
2089
2090 l_ptr->stats.sent_probes++;
2091 }
2092 l_ptr->stats.sent_states++;
2093 } else { /* RESET_MSG or ACTIVATE_MSG */
2094 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
2095 msg_set_seq_gap(msg, 0);
2096 msg_set_next_sent(msg, 1);
2097 msg_set_link_tolerance(msg, l_ptr->tolerance);
2098 msg_set_linkprio(msg, l_ptr->priority);
2099 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
2100 }
2101
Per Liden4323add2006-01-18 00:38:21 +01002102 if (tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 msg_set_redundant_link(msg);
2104 } else {
2105 msg_clear_redundant_link(msg);
2106 }
2107 msg_set_linkprio(msg, l_ptr->priority);
2108
2109 /* Ensure sequence number will not fit : */
2110
2111 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
2112
2113 /* Congestion? */
2114
Per Liden4323add2006-01-18 00:38:21 +01002115 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002116 if (!l_ptr->proto_msg_queue) {
2117 l_ptr->proto_msg_queue =
2118 buf_acquire(sizeof(l_ptr->proto_msg));
2119 }
2120 buf = l_ptr->proto_msg_queue;
2121 if (!buf)
2122 return;
2123 memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg));
2124 return;
2125 }
2126 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
2127
2128 /* Message can be sent */
2129
2130 msg_dbg(msg, ">>");
2131
2132 buf = buf_acquire(msg_size);
2133 if (!buf)
2134 return;
2135
2136 memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg));
2137 msg_set_size(buf_msg(buf), msg_size);
2138
Per Liden4323add2006-01-18 00:38:21 +01002139 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140 l_ptr->unacked_window = 0;
2141 buf_discard(buf);
2142 return;
2143 }
2144
2145 /* New congestion */
Per Liden4323add2006-01-18 00:38:21 +01002146 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 l_ptr->proto_msg_queue = buf;
2148 l_ptr->stats.bearer_congs++;
2149}
2150
2151/*
2152 * Receive protocol message :
2153 * Note that network plane id propagates through the network, and may
2154 * change at any time. The node with lowest address rules
2155 */
2156
2157static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2158{
2159 u32 rec_gap = 0;
2160 u32 max_pkt_info;
2161 u32 max_pkt_ack;
2162 u32 msg_tol;
2163 struct tipc_msg *msg = buf_msg(buf);
2164
2165 dbg("AT(%u):", jiffies_to_msecs(jiffies));
2166 msg_dbg(msg, "<<");
2167 if (link_blocked(l_ptr))
2168 goto exit;
2169
2170 /* record unnumbered packet arrival (force mismatch on next timeout) */
2171
2172 l_ptr->checkpoint--;
2173
2174 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2175 if (tipc_own_addr > msg_prevnode(msg))
2176 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2177
2178 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2179
2180 switch (msg_type(msg)) {
2181
2182 case RESET_MSG:
2183 if (!link_working_unknown(l_ptr) && l_ptr->peer_session) {
2184 if (msg_session(msg) == l_ptr->peer_session) {
2185 dbg("Duplicate RESET: %u<->%u\n",
2186 msg_session(msg), l_ptr->peer_session);
2187 break; /* duplicate: ignore */
2188 }
2189 }
2190 /* fall thru' */
2191 case ACTIVATE_MSG:
2192 /* Update link settings according other endpoint's values */
2193
2194 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2195
2196 if ((msg_tol = msg_link_tolerance(msg)) &&
2197 (msg_tol > l_ptr->tolerance))
2198 link_set_supervision_props(l_ptr, msg_tol);
2199
2200 if (msg_linkprio(msg) > l_ptr->priority)
2201 l_ptr->priority = msg_linkprio(msg);
2202
2203 max_pkt_info = msg_max_pkt(msg);
2204 if (max_pkt_info) {
2205 if (max_pkt_info < l_ptr->max_pkt_target)
2206 l_ptr->max_pkt_target = max_pkt_info;
2207 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2208 l_ptr->max_pkt = l_ptr->max_pkt_target;
2209 } else {
2210 l_ptr->max_pkt = l_ptr->max_pkt_target;
2211 }
2212 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2213
2214 link_state_event(l_ptr, msg_type(msg));
2215
2216 l_ptr->peer_session = msg_session(msg);
2217 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2218
2219 /* Synchronize broadcast sequence numbers */
Per Liden4323add2006-01-18 00:38:21 +01002220 if (!tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002221 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2222 }
2223 break;
2224 case STATE_MSG:
2225
2226 if ((msg_tol = msg_link_tolerance(msg)))
2227 link_set_supervision_props(l_ptr, msg_tol);
2228
2229 if (msg_linkprio(msg) &&
2230 (msg_linkprio(msg) != l_ptr->priority)) {
2231 warn("Changing prio <%s>: %u->%u\n",
2232 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2233 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01002234 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002235 break;
2236 }
2237 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2238 l_ptr->stats.recv_states++;
2239 if (link_reset_unknown(l_ptr))
2240 break;
2241
2242 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
2243 rec_gap = mod(msg_next_sent(msg) -
2244 mod(l_ptr->next_in_no));
2245 }
2246
2247 max_pkt_ack = msg_max_pkt(msg);
2248 if (max_pkt_ack > l_ptr->max_pkt) {
2249 dbg("Link <%s> updated MTU %u -> %u\n",
2250 l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
2251 l_ptr->max_pkt = max_pkt_ack;
2252 l_ptr->max_pkt_probes = 0;
2253 }
2254
2255 max_pkt_ack = 0;
2256 if (msg_probe(msg)) {
2257 l_ptr->stats.recv_probes++;
2258 if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2259 max_pkt_ack = msg_size(msg);
2260 }
2261 }
2262
2263 /* Protocol message before retransmits, reduce loss risk */
2264
Per Liden4323add2006-01-18 00:38:21 +01002265 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002266
2267 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01002268 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2269 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002270 }
2271 if (msg_seq_gap(msg)) {
2272 msg_dbg(msg, "With Gap:");
2273 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01002274 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2275 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002276 }
2277 break;
2278 default:
2279 msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
2280 }
2281exit:
2282 buf_discard(buf);
2283}
2284
2285
2286/*
Per Liden4323add2006-01-18 00:38:21 +01002287 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01002288 * another bearer. Owner node is locked.
2289 */
Per Liden4323add2006-01-18 00:38:21 +01002290void tipc_link_tunnel(struct link *l_ptr,
2291 struct tipc_msg *tunnel_hdr,
2292 struct tipc_msg *msg,
2293 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002294{
2295 struct link *tunnel;
2296 struct sk_buff *buf;
2297 u32 length = msg_size(msg);
2298
2299 tunnel = l_ptr->owner->active_links[selector & 1];
Per Liden4323add2006-01-18 00:38:21 +01002300 if (!tipc_link_is_up(tunnel))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002301 return;
2302 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
2303 buf = buf_acquire(length + INT_H_SIZE);
2304 if (!buf)
2305 return;
2306 memcpy(buf->data, (unchar *)tunnel_hdr, INT_H_SIZE);
2307 memcpy(buf->data + INT_H_SIZE, (unchar *)msg, length);
2308 dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
2309 msg_dbg(buf_msg(buf), ">SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002310 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002311}
2312
2313
2314
2315/*
2316 * changeover(): Send whole message queue via the remaining link
2317 * Owner node is locked.
2318 */
2319
Per Liden4323add2006-01-18 00:38:21 +01002320void tipc_link_changeover(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002321{
2322 u32 msgcount = l_ptr->out_queue_size;
2323 struct sk_buff *crs = l_ptr->first_out;
2324 struct link *tunnel = l_ptr->owner->active_links[0];
Per Liden4323add2006-01-18 00:38:21 +01002325 int split_bundles = tipc_node_has_redundant_links(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002326 struct tipc_msg tunnel_hdr;
2327
2328 if (!tunnel)
2329 return;
2330
2331 if (!l_ptr->owner->permit_changeover)
2332 return;
2333
2334 msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2335 ORIGINAL_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
2336 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2337 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002338
Per Lidenb97bf3f2006-01-02 19:04:38 +01002339 if (!l_ptr->first_out) {
2340 struct sk_buff *buf;
2341
Per Lidenb97bf3f2006-01-02 19:04:38 +01002342 buf = buf_acquire(INT_H_SIZE);
2343 if (buf) {
2344 memcpy(buf->data, (unchar *)&tunnel_hdr, INT_H_SIZE);
2345 msg_set_size(&tunnel_hdr, INT_H_SIZE);
2346 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2347 tunnel->b_ptr->net_plane);
2348 msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002349 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350 } else {
2351 warn("Memory squeeze; link changeover failed\n");
2352 }
2353 return;
2354 }
Allan Stephensf1310722006-06-25 23:51:37 -07002355
Per Lidenb97bf3f2006-01-02 19:04:38 +01002356 while (crs) {
2357 struct tipc_msg *msg = buf_msg(crs);
2358
2359 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
2360 u32 msgcount = msg_msgcnt(msg);
2361 struct tipc_msg *m = msg_get_wrapped(msg);
2362 unchar* pos = (unchar*)m;
2363
2364 while (msgcount--) {
2365 msg_set_seqno(m,msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002366 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2367 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002368 pos += align(msg_size(m));
2369 m = (struct tipc_msg *)pos;
2370 }
2371 } else {
Per Liden4323add2006-01-18 00:38:21 +01002372 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2373 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002374 }
2375 crs = crs->next;
2376 }
2377}
2378
Per Liden4323add2006-01-18 00:38:21 +01002379void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002380{
2381 struct sk_buff *iter;
2382 struct tipc_msg tunnel_hdr;
2383
2384 msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2385 DUPLICATE_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
2386 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2387 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2388 iter = l_ptr->first_out;
2389 while (iter) {
2390 struct sk_buff *outbuf;
2391 struct tipc_msg *msg = buf_msg(iter);
2392 u32 length = msg_size(msg);
2393
2394 if (msg_user(msg) == MSG_BUNDLER)
2395 msg_set_type(msg, CLOSED_MSG);
2396 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
2397 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
2398 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
2399 outbuf = buf_acquire(length + INT_H_SIZE);
2400 if (outbuf == NULL) {
2401 warn("Memory squeeze; buffer duplication failed\n");
2402 return;
2403 }
2404 memcpy(outbuf->data, (unchar *)&tunnel_hdr, INT_H_SIZE);
2405 memcpy(outbuf->data + INT_H_SIZE, iter->data, length);
2406 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2407 tunnel->b_ptr->net_plane);
2408 msg_dbg(buf_msg(outbuf), ">SEND>");
Per Liden4323add2006-01-18 00:38:21 +01002409 tipc_link_send_buf(tunnel, outbuf);
2410 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002411 return;
2412 iter = iter->next;
2413 }
2414}
2415
2416
2417
2418/**
2419 * buf_extract - extracts embedded TIPC message from another message
2420 * @skb: encapsulating message buffer
2421 * @from_pos: offset to extract from
2422 *
2423 * Returns a new message buffer containing an embedded message. The
2424 * encapsulating message itself is left unchanged.
2425 */
2426
2427static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2428{
2429 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2430 u32 size = msg_size(msg);
2431 struct sk_buff *eb;
2432
2433 eb = buf_acquire(size);
2434 if (eb)
2435 memcpy(eb->data, (unchar *)msg, size);
2436 return eb;
2437}
2438
2439/*
2440 * link_recv_changeover_msg(): Receive tunneled packet sent
2441 * via other link. Node is locked. Return extracted buffer.
2442 */
2443
2444static int link_recv_changeover_msg(struct link **l_ptr,
2445 struct sk_buff **buf)
2446{
2447 struct sk_buff *tunnel_buf = *buf;
2448 struct link *dest_link;
2449 struct tipc_msg *msg;
2450 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2451 u32 msg_typ = msg_type(tunnel_msg);
2452 u32 msg_count = msg_msgcnt(tunnel_msg);
2453
2454 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002455 if (!dest_link) {
2456 msg_dbg(tunnel_msg, "NOLINK/<REC<");
2457 goto exit;
2458 }
Allan Stephensf1310722006-06-25 23:51:37 -07002459 if (dest_link == *l_ptr) {
2460 err("Unexpected changeover message on link <%s>\n",
2461 (*l_ptr)->name);
2462 goto exit;
2463 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464 dbg("%c<-%c:", dest_link->b_ptr->net_plane,
2465 (*l_ptr)->b_ptr->net_plane);
2466 *l_ptr = dest_link;
2467 msg = msg_get_wrapped(tunnel_msg);
2468
2469 if (msg_typ == DUPLICATE_MSG) {
2470 if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
2471 msg_dbg(tunnel_msg, "DROP/<REC<");
2472 goto exit;
2473 }
2474 *buf = buf_extract(tunnel_buf,INT_H_SIZE);
2475 if (*buf == NULL) {
2476 warn("Memory squeeze; failed to extract msg\n");
2477 goto exit;
2478 }
2479 msg_dbg(tunnel_msg, "TNL<REC<");
2480 buf_discard(tunnel_buf);
2481 return 1;
2482 }
2483
2484 /* First original message ?: */
2485
Per Liden4323add2006-01-18 00:38:21 +01002486 if (tipc_link_is_up(dest_link)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002487 msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
Per Liden4323add2006-01-18 00:38:21 +01002488 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002489 dest_link->exp_msg_count = msg_count;
2490 if (!msg_count)
2491 goto exit;
2492 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
2493 msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
2494 dest_link->exp_msg_count = msg_count;
2495 if (!msg_count)
2496 goto exit;
2497 }
2498
2499 /* Receive original message */
2500
2501 if (dest_link->exp_msg_count == 0) {
2502 msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
2503 dbg_print_link(dest_link, "LINK:");
2504 goto exit;
2505 }
2506 dest_link->exp_msg_count--;
2507 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
2508 msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
2509 goto exit;
2510 } else {
2511 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2512 if (*buf != NULL) {
2513 msg_dbg(tunnel_msg, "TNL<REC<");
2514 buf_discard(tunnel_buf);
2515 return 1;
2516 } else {
2517 warn("Memory squeeze; dropped incoming msg\n");
2518 }
2519 }
2520exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002521 *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002522 buf_discard(tunnel_buf);
2523 return 0;
2524}
2525
2526/*
2527 * Bundler functionality:
2528 */
Per Liden4323add2006-01-18 00:38:21 +01002529void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002530{
2531 u32 msgcount = msg_msgcnt(buf_msg(buf));
2532 u32 pos = INT_H_SIZE;
2533 struct sk_buff *obuf;
2534
2535 msg_dbg(buf_msg(buf), "<BNDL<: ");
2536 while (msgcount--) {
2537 obuf = buf_extract(buf, pos);
2538 if (obuf == NULL) {
2539 char addr_string[16];
2540
2541 warn("Buffer allocation failure;\n");
2542 warn(" incoming message(s) from %s lost\n",
2543 addr_string_fill(addr_string,
2544 msg_orignode(buf_msg(buf))));
2545 return;
2546 };
2547 pos += align(msg_size(buf_msg(obuf)));
2548 msg_dbg(buf_msg(obuf), " /");
Per Liden4323add2006-01-18 00:38:21 +01002549 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550 }
2551 buf_discard(buf);
2552}
2553
2554/*
2555 * Fragmentation/defragmentation:
2556 */
2557
2558
2559/*
Per Liden4323add2006-01-18 00:38:21 +01002560 * tipc_link_send_long_buf: Entry for buffers needing fragmentation.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002561 * The buffer is complete, inclusive total message length.
2562 * Returns user data length.
2563 */
Per Liden4323add2006-01-18 00:38:21 +01002564int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002565{
2566 struct tipc_msg *inmsg = buf_msg(buf);
2567 struct tipc_msg fragm_hdr;
2568 u32 insize = msg_size(inmsg);
2569 u32 dsz = msg_data_sz(inmsg);
2570 unchar *crs = buf->data;
2571 u32 rest = insize;
2572 u32 pack_sz = link_max_pkt(l_ptr);
2573 u32 fragm_sz = pack_sz - INT_H_SIZE;
2574 u32 fragm_no = 1;
2575 u32 destaddr = msg_destnode(inmsg);
2576
2577 if (msg_short(inmsg))
2578 destaddr = l_ptr->addr;
2579
2580 if (msg_routed(inmsg))
2581 msg_set_prevnode(inmsg, tipc_own_addr);
2582
2583 /* Prepare reusable fragment header: */
2584
2585 msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
2586 TIPC_OK, INT_H_SIZE, destaddr);
2587 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2588 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2589 msg_set_fragm_no(&fragm_hdr, fragm_no);
2590 l_ptr->stats.sent_fragmented++;
2591
2592 /* Chop up message: */
2593
2594 while (rest > 0) {
2595 struct sk_buff *fragm;
2596
2597 if (rest <= fragm_sz) {
2598 fragm_sz = rest;
2599 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2600 }
2601 fragm = buf_acquire(fragm_sz + INT_H_SIZE);
2602 if (fragm == NULL) {
2603 warn("Memory squeeze; failed to fragment msg\n");
2604 dsz = -ENOMEM;
2605 goto exit;
2606 }
2607 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
2608 memcpy(fragm->data, (unchar *)&fragm_hdr, INT_H_SIZE);
2609 memcpy(fragm->data + INT_H_SIZE, crs, fragm_sz);
2610
2611 /* Send queued messages first, if any: */
2612
2613 l_ptr->stats.sent_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01002614 tipc_link_send_buf(l_ptr, fragm);
2615 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002616 return dsz;
2617 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2618 rest -= fragm_sz;
2619 crs += fragm_sz;
2620 msg_set_type(&fragm_hdr, FRAGMENT);
2621 }
2622exit:
2623 buf_discard(buf);
2624 return dsz;
2625}
2626
2627/*
2628 * A pending message being re-assembled must store certain values
2629 * to handle subsequent fragments correctly. The following functions
2630 * help storing these values in unused, available fields in the
2631 * pending message. This makes dynamic memory allocation unecessary.
2632 */
2633
Sam Ravnborg05790c62006-03-20 22:37:04 -08002634static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002635{
2636 msg_set_seqno(buf_msg(buf), seqno);
2637}
2638
Sam Ravnborg05790c62006-03-20 22:37:04 -08002639static u32 get_fragm_size(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002640{
2641 return msg_ack(buf_msg(buf));
2642}
2643
Sam Ravnborg05790c62006-03-20 22:37:04 -08002644static void set_fragm_size(struct sk_buff *buf, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002645{
2646 msg_set_ack(buf_msg(buf), sz);
2647}
2648
Sam Ravnborg05790c62006-03-20 22:37:04 -08002649static u32 get_expected_frags(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002650{
2651 return msg_bcast_ack(buf_msg(buf));
2652}
2653
Sam Ravnborg05790c62006-03-20 22:37:04 -08002654static void set_expected_frags(struct sk_buff *buf, u32 exp)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002655{
2656 msg_set_bcast_ack(buf_msg(buf), exp);
2657}
2658
Sam Ravnborg05790c62006-03-20 22:37:04 -08002659static u32 get_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002660{
2661 return msg_reroute_cnt(buf_msg(buf));
2662}
2663
Sam Ravnborg05790c62006-03-20 22:37:04 -08002664static void incr_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002665{
2666 msg_incr_reroute_cnt(buf_msg(buf));
2667}
2668
2669/*
Per Liden4323add2006-01-18 00:38:21 +01002670 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002671 * the reassembled buffer if message is complete.
2672 */
Per Liden4323add2006-01-18 00:38:21 +01002673int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
2674 struct tipc_msg **m)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002675{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002676 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002677 struct sk_buff *fbuf = *fb;
2678 struct tipc_msg *fragm = buf_msg(fbuf);
2679 struct sk_buff *pbuf = *pending;
2680 u32 long_msg_seq_no = msg_long_msgno(fragm);
2681
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002682 *fb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002683 msg_dbg(fragm,"FRG<REC<");
2684
2685 /* Is there an incomplete message waiting for this fragment? */
2686
2687 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no)
2688 || (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
2689 prev = pbuf;
2690 pbuf = pbuf->next;
2691 }
2692
2693 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2694 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2695 u32 msg_sz = msg_size(imsg);
2696 u32 fragm_sz = msg_data_sz(fragm);
2697 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2698 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2699 if (msg_type(imsg) == TIPC_MCAST_MSG)
2700 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2701 if (msg_size(imsg) > max) {
2702 msg_dbg(fragm,"<REC<Oversized: ");
2703 buf_discard(fbuf);
2704 return 0;
2705 }
2706 pbuf = buf_acquire(msg_size(imsg));
2707 if (pbuf != NULL) {
2708 pbuf->next = *pending;
2709 *pending = pbuf;
2710 memcpy(pbuf->data, (unchar *)imsg, msg_data_sz(fragm));
2711
2712 /* Prepare buffer for subsequent fragments. */
2713
2714 set_long_msg_seqno(pbuf, long_msg_seq_no);
2715 set_fragm_size(pbuf,fragm_sz);
2716 set_expected_frags(pbuf,exp_fragm_cnt - 1);
2717 } else {
2718 warn("Memory squeeze; got no defragmenting buffer\n");
2719 }
2720 buf_discard(fbuf);
2721 return 0;
2722 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2723 u32 dsz = msg_data_sz(fragm);
2724 u32 fsz = get_fragm_size(pbuf);
2725 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2726 u32 exp_frags = get_expected_frags(pbuf) - 1;
2727 memcpy(pbuf->data + crs, msg_data(fragm), dsz);
2728 buf_discard(fbuf);
2729
2730 /* Is message complete? */
2731
2732 if (exp_frags == 0) {
2733 if (prev)
2734 prev->next = pbuf->next;
2735 else
2736 *pending = pbuf->next;
2737 msg_reset_reroute_cnt(buf_msg(pbuf));
2738 *fb = pbuf;
2739 *m = buf_msg(pbuf);
2740 return 1;
2741 }
2742 set_expected_frags(pbuf,exp_frags);
2743 return 0;
2744 }
2745 dbg(" Discarding orphan fragment %x\n",fbuf);
2746 msg_dbg(fragm,"ORPHAN:");
2747 dbg("Pending long buffers:\n");
2748 dbg_print_buf_chain(*pending);
2749 buf_discard(fbuf);
2750 return 0;
2751}
2752
2753/**
2754 * link_check_defragm_bufs - flush stale incoming message fragments
2755 * @l_ptr: pointer to link
2756 */
2757
2758static void link_check_defragm_bufs(struct link *l_ptr)
2759{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002760 struct sk_buff *prev = NULL;
2761 struct sk_buff *next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002762 struct sk_buff *buf = l_ptr->defragm_buf;
2763
2764 if (!buf)
2765 return;
2766 if (!link_working_working(l_ptr))
2767 return;
2768 while (buf) {
2769 u32 cnt = get_timer_cnt(buf);
2770
2771 next = buf->next;
2772 if (cnt < 4) {
2773 incr_timer_cnt(buf);
2774 prev = buf;
2775 } else {
2776 dbg(" Discarding incomplete long buffer\n");
2777 msg_dbg(buf_msg(buf), "LONG:");
2778 dbg_print_link(l_ptr, "curr:");
2779 dbg("Pending long buffers:\n");
2780 dbg_print_buf_chain(l_ptr->defragm_buf);
2781 if (prev)
2782 prev->next = buf->next;
2783 else
2784 l_ptr->defragm_buf = buf->next;
2785 buf_discard(buf);
2786 }
2787 buf = next;
2788 }
2789}
2790
2791
2792
2793static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2794{
2795 l_ptr->tolerance = tolerance;
2796 l_ptr->continuity_interval =
2797 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2798 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2799}
2800
2801
Per Liden4323add2006-01-18 00:38:21 +01002802void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002803{
2804 /* Data messages from this node, inclusive FIRST_FRAGM */
2805 l_ptr->queue_limit[DATA_LOW] = window;
2806 l_ptr->queue_limit[DATA_MEDIUM] = (window / 3) * 4;
2807 l_ptr->queue_limit[DATA_HIGH] = (window / 3) * 5;
2808 l_ptr->queue_limit[DATA_CRITICAL] = (window / 3) * 6;
2809 /* Transiting data messages,inclusive FIRST_FRAGM */
2810 l_ptr->queue_limit[DATA_LOW + 4] = 300;
2811 l_ptr->queue_limit[DATA_MEDIUM + 4] = 600;
2812 l_ptr->queue_limit[DATA_HIGH + 4] = 900;
2813 l_ptr->queue_limit[DATA_CRITICAL + 4] = 1200;
2814 l_ptr->queue_limit[CONN_MANAGER] = 1200;
2815 l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
2816 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2817 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2818 /* FRAGMENT and LAST_FRAGMENT packets */
2819 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2820}
2821
2822/**
2823 * link_find_link - locate link by name
2824 * @name - ptr to link name string
2825 * @node - ptr to area to be filled with ptr to associated node
2826 *
Per Liden4323add2006-01-18 00:38:21 +01002827 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002828 * this also prevents link deletion.
2829 *
2830 * Returns pointer to link (or 0 if invalid link name).
2831 */
2832
2833static struct link *link_find_link(const char *name, struct node **node)
2834{
2835 struct link_name link_name_parts;
2836 struct bearer *b_ptr;
2837 struct link *l_ptr;
2838
2839 if (!link_name_validate(name, &link_name_parts))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002840 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002841
Per Liden4323add2006-01-18 00:38:21 +01002842 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002843 if (!b_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002844 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002845
Per Liden4323add2006-01-18 00:38:21 +01002846 *node = tipc_node_find(link_name_parts.addr_peer);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002847 if (!*node)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002848 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002849
2850 l_ptr = (*node)->links[b_ptr->identity];
2851 if (!l_ptr || strcmp(l_ptr->name, name))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002852 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002853
2854 return l_ptr;
2855}
2856
Per Liden4323add2006-01-18 00:38:21 +01002857struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
2858 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002859{
2860 struct tipc_link_config *args;
2861 u32 new_value;
2862 struct link *l_ptr;
2863 struct node *node;
2864 int res;
2865
2866 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002867 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002868
2869 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2870 new_value = ntohl(args->value);
2871
Per Liden4323add2006-01-18 00:38:21 +01002872 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002873 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002874 (tipc_bclink_set_queue_limits(new_value) == 0))
2875 return tipc_cfg_reply_none();
2876 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
2877 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002878 }
2879
Per Liden4323add2006-01-18 00:38:21 +01002880 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002881 l_ptr = link_find_link(args->name, &node);
2882 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002883 read_unlock_bh(&tipc_net_lock);
2884 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002885 }
2886
Per Liden4323add2006-01-18 00:38:21 +01002887 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002888 res = -EINVAL;
2889 switch (cmd) {
2890 case TIPC_CMD_SET_LINK_TOL:
2891 if ((new_value >= TIPC_MIN_LINK_TOL) &&
2892 (new_value <= TIPC_MAX_LINK_TOL)) {
2893 link_set_supervision_props(l_ptr, new_value);
Per Liden4323add2006-01-18 00:38:21 +01002894 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2895 0, 0, new_value, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002896 res = TIPC_OK;
2897 }
2898 break;
2899 case TIPC_CMD_SET_LINK_PRI:
Per Liden16cb4b32006-01-13 22:22:22 +01002900 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2901 (new_value <= TIPC_MAX_LINK_PRI)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002902 l_ptr->priority = new_value;
Per Liden4323add2006-01-18 00:38:21 +01002903 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2904 0, 0, 0, new_value, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002905 res = TIPC_OK;
2906 }
2907 break;
2908 case TIPC_CMD_SET_LINK_WINDOW:
2909 if ((new_value >= TIPC_MIN_LINK_WIN) &&
2910 (new_value <= TIPC_MAX_LINK_WIN)) {
Per Liden4323add2006-01-18 00:38:21 +01002911 tipc_link_set_queue_limits(l_ptr, new_value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002912 res = TIPC_OK;
2913 }
2914 break;
2915 }
Per Liden4323add2006-01-18 00:38:21 +01002916 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002917
Per Liden4323add2006-01-18 00:38:21 +01002918 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002919 if (res)
Per Liden4323add2006-01-18 00:38:21 +01002920 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002921
Per Liden4323add2006-01-18 00:38:21 +01002922 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002923}
2924
2925/**
2926 * link_reset_statistics - reset link statistics
2927 * @l_ptr: pointer to link
2928 */
2929
2930static void link_reset_statistics(struct link *l_ptr)
2931{
2932 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2933 l_ptr->stats.sent_info = l_ptr->next_out_no;
2934 l_ptr->stats.recv_info = l_ptr->next_in_no;
2935}
2936
Per Liden4323add2006-01-18 00:38:21 +01002937struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002938{
2939 char *link_name;
2940 struct link *l_ptr;
2941 struct node *node;
2942
2943 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002944 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002945
2946 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002947 if (!strcmp(link_name, tipc_bclink_name)) {
2948 if (tipc_bclink_reset_stats())
2949 return tipc_cfg_reply_error_string("link not found");
2950 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002951 }
2952
Per Liden4323add2006-01-18 00:38:21 +01002953 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002954 l_ptr = link_find_link(link_name, &node);
2955 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002956 read_unlock_bh(&tipc_net_lock);
2957 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002958 }
2959
Per Liden4323add2006-01-18 00:38:21 +01002960 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002961 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002962 tipc_node_unlock(node);
2963 read_unlock_bh(&tipc_net_lock);
2964 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002965}
2966
2967/**
2968 * percent - convert count to a percentage of total (rounding up or down)
2969 */
2970
2971static u32 percent(u32 count, u32 total)
2972{
2973 return (count * 100 + (total / 2)) / total;
2974}
2975
2976/**
Per Liden4323add2006-01-18 00:38:21 +01002977 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002978 * @name: link name
2979 * @buf: print buffer area
2980 * @buf_size: size of print buffer area
2981 *
2982 * Returns length of print buffer data string (or 0 if error)
2983 */
2984
Per Liden4323add2006-01-18 00:38:21 +01002985static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002986{
2987 struct print_buf pb;
2988 struct link *l_ptr;
2989 struct node *node;
2990 char *status;
2991 u32 profile_total = 0;
2992
Per Liden4323add2006-01-18 00:38:21 +01002993 if (!strcmp(name, tipc_bclink_name))
2994 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002995
Per Liden4323add2006-01-18 00:38:21 +01002996 tipc_printbuf_init(&pb, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002997
Per Liden4323add2006-01-18 00:38:21 +01002998 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002999 l_ptr = link_find_link(name, &node);
3000 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003001 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003002 return 0;
3003 }
Per Liden4323add2006-01-18 00:38:21 +01003004 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003005
Per Liden4323add2006-01-18 00:38:21 +01003006 if (tipc_link_is_active(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003007 status = "ACTIVE";
Per Liden4323add2006-01-18 00:38:21 +01003008 else if (tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003009 status = "STANDBY";
3010 else
3011 status = "DEFUNCT";
3012 tipc_printf(&pb, "Link <%s>\n"
3013 " %s MTU:%u Priority:%u Tolerance:%u ms"
3014 " Window:%u packets\n",
3015 l_ptr->name, status, link_max_pkt(l_ptr),
3016 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
3017 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
3018 l_ptr->next_in_no - l_ptr->stats.recv_info,
3019 l_ptr->stats.recv_fragments,
3020 l_ptr->stats.recv_fragmented,
3021 l_ptr->stats.recv_bundles,
3022 l_ptr->stats.recv_bundled);
3023 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
3024 l_ptr->next_out_no - l_ptr->stats.sent_info,
3025 l_ptr->stats.sent_fragments,
3026 l_ptr->stats.sent_fragmented,
3027 l_ptr->stats.sent_bundles,
3028 l_ptr->stats.sent_bundled);
3029 profile_total = l_ptr->stats.msg_length_counts;
3030 if (!profile_total)
3031 profile_total = 1;
3032 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
3033 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
3034 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
3035 l_ptr->stats.msg_length_counts,
3036 l_ptr->stats.msg_lengths_total / profile_total,
3037 percent(l_ptr->stats.msg_length_profile[0], profile_total),
3038 percent(l_ptr->stats.msg_length_profile[1], profile_total),
3039 percent(l_ptr->stats.msg_length_profile[2], profile_total),
3040 percent(l_ptr->stats.msg_length_profile[3], profile_total),
3041 percent(l_ptr->stats.msg_length_profile[4], profile_total),
3042 percent(l_ptr->stats.msg_length_profile[5], profile_total),
3043 percent(l_ptr->stats.msg_length_profile[6], profile_total));
3044 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
3045 l_ptr->stats.recv_states,
3046 l_ptr->stats.recv_probes,
3047 l_ptr->stats.recv_nacks,
3048 l_ptr->stats.deferred_recv,
3049 l_ptr->stats.duplicates);
3050 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
3051 l_ptr->stats.sent_states,
3052 l_ptr->stats.sent_probes,
3053 l_ptr->stats.sent_nacks,
3054 l_ptr->stats.sent_acks,
3055 l_ptr->stats.retransmitted);
3056 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
3057 l_ptr->stats.bearer_congs,
3058 l_ptr->stats.link_congs,
3059 l_ptr->stats.max_queue_sz,
3060 l_ptr->stats.queue_sz_counts
3061 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
3062 : 0);
3063
Per Liden4323add2006-01-18 00:38:21 +01003064 tipc_node_unlock(node);
3065 read_unlock_bh(&tipc_net_lock);
3066 return tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003067}
3068
3069#define MAX_LINK_STATS_INFO 2000
3070
Per Liden4323add2006-01-18 00:38:21 +01003071struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003072{
3073 struct sk_buff *buf;
3074 struct tlv_desc *rep_tlv;
3075 int str_len;
3076
3077 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01003078 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003079
Per Liden4323add2006-01-18 00:38:21 +01003080 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
Per Lidenb97bf3f2006-01-02 19:04:38 +01003081 if (!buf)
3082 return NULL;
3083
3084 rep_tlv = (struct tlv_desc *)buf->data;
3085
Per Liden4323add2006-01-18 00:38:21 +01003086 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
3087 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003088 if (!str_len) {
3089 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +01003090 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003091 }
3092
3093 skb_put(buf, TLV_SPACE(str_len));
3094 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
3095
3096 return buf;
3097}
3098
3099#if 0
3100int link_control(const char *name, u32 op, u32 val)
3101{
3102 int res = -EINVAL;
3103 struct link *l_ptr;
3104 u32 bearer_id;
3105 struct node * node;
3106 u32 a;
3107
3108 a = link_name2addr(name, &bearer_id);
Per Liden4323add2006-01-18 00:38:21 +01003109 read_lock_bh(&tipc_net_lock);
3110 node = tipc_node_find(a);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003111 if (node) {
Per Liden4323add2006-01-18 00:38:21 +01003112 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003113 l_ptr = node->links[bearer_id];
3114 if (l_ptr) {
3115 if (op == TIPC_REMOVE_LINK) {
3116 struct bearer *b_ptr = l_ptr->b_ptr;
3117 spin_lock_bh(&b_ptr->publ.lock);
Per Liden4323add2006-01-18 00:38:21 +01003118 tipc_link_delete(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003119 spin_unlock_bh(&b_ptr->publ.lock);
3120 }
3121 if (op == TIPC_CMD_BLOCK_LINK) {
Per Liden4323add2006-01-18 00:38:21 +01003122 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003123 l_ptr->blocked = 1;
3124 }
3125 if (op == TIPC_CMD_UNBLOCK_LINK) {
3126 l_ptr->blocked = 0;
3127 }
3128 res = TIPC_OK;
3129 }
Per Liden4323add2006-01-18 00:38:21 +01003130 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003131 }
Per Liden4323add2006-01-18 00:38:21 +01003132 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003133 return res;
3134}
3135#endif
3136
3137/**
Per Liden4323add2006-01-18 00:38:21 +01003138 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01003139 * @dest: network address of destination node
3140 * @selector: used to select from set of active links
3141 *
3142 * If no active link can be found, uses default maximum packet size.
3143 */
3144
Per Liden4323add2006-01-18 00:38:21 +01003145u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003146{
3147 struct node *n_ptr;
3148 struct link *l_ptr;
3149 u32 res = MAX_PKT_DEFAULT;
3150
3151 if (dest == tipc_own_addr)
3152 return MAX_MSG_SIZE;
3153
Per Liden4323add2006-01-18 00:38:21 +01003154 read_lock_bh(&tipc_net_lock);
3155 n_ptr = tipc_node_select(dest, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003156 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01003157 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003158 l_ptr = n_ptr->active_links[selector & 1];
3159 if (l_ptr)
3160 res = link_max_pkt(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01003161 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003162 }
Per Liden4323add2006-01-18 00:38:21 +01003163 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003164 return res;
3165}
3166
3167#if 0
3168static void link_dump_rec_queue(struct link *l_ptr)
3169{
3170 struct sk_buff *crs;
3171
3172 if (!l_ptr->oldest_deferred_in) {
3173 info("Reception queue empty\n");
3174 return;
3175 }
3176 info("Contents of Reception queue:\n");
3177 crs = l_ptr->oldest_deferred_in;
3178 while (crs) {
3179 if (crs->data == (void *)0x0000a3a3) {
3180 info("buffer %x invalid\n", crs);
3181 return;
3182 }
3183 msg_dbg(buf_msg(crs), "In rec queue: \n");
3184 crs = crs->next;
3185 }
3186}
3187#endif
3188
3189static void link_dump_send_queue(struct link *l_ptr)
3190{
3191 if (l_ptr->next_out) {
3192 info("\nContents of unsent queue:\n");
3193 dbg_print_buf_chain(l_ptr->next_out);
3194 }
3195 info("\nContents of send queue:\n");
3196 if (l_ptr->first_out) {
3197 dbg_print_buf_chain(l_ptr->first_out);
3198 }
3199 info("Empty send queue\n");
3200}
3201
3202static void link_print(struct link *l_ptr, struct print_buf *buf,
3203 const char *str)
3204{
3205 tipc_printf(buf, str);
3206 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
3207 return;
3208 tipc_printf(buf, "Link %x<%s>:",
3209 l_ptr->addr, l_ptr->b_ptr->publ.name);
3210 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
3211 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
3212 tipc_printf(buf, "SQUE");
3213 if (l_ptr->first_out) {
3214 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
3215 if (l_ptr->next_out)
3216 tipc_printf(buf, "%u..",
3217 msg_seqno(buf_msg(l_ptr->next_out)));
3218 tipc_printf(buf, "%u]",
3219 msg_seqno(buf_msg
3220 (l_ptr->last_out)), l_ptr->out_queue_size);
3221 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
3222 msg_seqno(buf_msg(l_ptr->first_out)))
3223 != (l_ptr->out_queue_size - 1))
3224 || (l_ptr->last_out->next != 0)) {
3225 tipc_printf(buf, "\nSend queue inconsistency\n");
3226 tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
3227 tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
3228 tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
3229 link_dump_send_queue(l_ptr);
3230 }
3231 } else
3232 tipc_printf(buf, "[]");
3233 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
3234 if (l_ptr->oldest_deferred_in) {
3235 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
3236 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
3237 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
3238 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
3239 tipc_printf(buf, ":RQSIZ(%u)",
3240 l_ptr->deferred_inqueue_sz);
3241 }
3242 }
3243 if (link_working_unknown(l_ptr))
3244 tipc_printf(buf, ":WU");
3245 if (link_reset_reset(l_ptr))
3246 tipc_printf(buf, ":RR");
3247 if (link_reset_unknown(l_ptr))
3248 tipc_printf(buf, ":RU");
3249 if (link_working_working(l_ptr))
3250 tipc_printf(buf, ":WW");
3251 tipc_printf(buf, "\n");
3252}
3253