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