Merge git://git.infradead.org/mtd-2.6
[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-2006, 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 bundle_size = msg_size(bundler_msg);
992         u32 to_pos = align(bundle_size);
993         u32 pad = to_pos - bundle_size;
994
995         if (msg_user(bundler_msg) != MSG_BUNDLER)
996                 return 0;
997         if (msg_type(bundler_msg) != OPEN_MSG)
998                 return 0;
999         if (skb_tailroom(bundler) < (pad + size))
1000                 return 0;
1001         if (link_max_pkt(l_ptr) < (to_pos + size))
1002                 return 0;
1003
1004         skb_put(bundler, pad + size);
1005         memcpy(bundler->data + to_pos, buf->data, size);
1006         msg_set_size(bundler_msg, to_pos + size);
1007         msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
1008         dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
1009             msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
1010         msg_dbg(msg, "PACKD:");
1011         buf_discard(buf);
1012         l_ptr->stats.sent_bundled++;
1013         return 1;
1014 }
1015
1016 static void link_add_to_outqueue(struct link *l_ptr,
1017                                  struct sk_buff *buf,
1018                                  struct tipc_msg *msg)
1019 {
1020         u32 ack = mod(l_ptr->next_in_no - 1);
1021         u32 seqno = mod(l_ptr->next_out_no++);
1022
1023         msg_set_word(msg, 2, ((ack << 16) | seqno));
1024         msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1025         buf->next = NULL;
1026         if (l_ptr->first_out) {
1027                 l_ptr->last_out->next = buf;
1028                 l_ptr->last_out = buf;
1029         } else
1030                 l_ptr->first_out = l_ptr->last_out = buf;
1031         l_ptr->out_queue_size++;
1032 }
1033
1034 /* 
1035  * tipc_link_send_buf() is the 'full path' for messages, called from 
1036  * inside TIPC when the 'fast path' in tipc_send_buf
1037  * has failed, and from link_send()
1038  */
1039
1040 int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
1041 {
1042         struct tipc_msg *msg = buf_msg(buf);
1043         u32 size = msg_size(msg);
1044         u32 dsz = msg_data_sz(msg);
1045         u32 queue_size = l_ptr->out_queue_size;
1046         u32 imp = msg_tot_importance(msg);
1047         u32 queue_limit = l_ptr->queue_limit[imp];
1048         u32 max_packet = link_max_pkt(l_ptr);
1049
1050         msg_set_prevnode(msg, tipc_own_addr);   /* If routed message */
1051
1052         /* Match msg importance against queue limits: */
1053
1054         if (unlikely(queue_size >= queue_limit)) {
1055                 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
1056                         return link_schedule_port(l_ptr, msg_origport(msg),
1057                                                   size);
1058                 }
1059                 msg_dbg(msg, "TIPC: Congestion, throwing away\n");
1060                 buf_discard(buf);
1061                 if (imp > CONN_MANAGER) {
1062                         warn("Resetting link <%s>, send queue full", l_ptr->name);
1063                         tipc_link_reset(l_ptr);
1064                 }
1065                 return dsz;
1066         }
1067
1068         /* Fragmentation needed ? */
1069
1070         if (size > max_packet)
1071                 return tipc_link_send_long_buf(l_ptr, buf);
1072
1073         /* Packet can be queued or sent: */
1074
1075         if (queue_size > l_ptr->stats.max_queue_sz)
1076                 l_ptr->stats.max_queue_sz = queue_size;
1077
1078         if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) && 
1079                    !link_congested(l_ptr))) {
1080                 link_add_to_outqueue(l_ptr, buf, msg);
1081
1082                 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
1083                         l_ptr->unacked_window = 0;
1084                 } else {
1085                         tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1086                         l_ptr->stats.bearer_congs++;
1087                         l_ptr->next_out = buf;
1088                 }
1089                 return dsz;
1090         }
1091         /* Congestion: can message be bundled ?: */
1092
1093         if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
1094             (msg_user(msg) != MSG_FRAGMENTER)) {
1095
1096                 /* Try adding message to an existing bundle */
1097
1098                 if (l_ptr->next_out && 
1099                     link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
1100                         tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
1101                         return dsz;
1102                 }
1103
1104                 /* Try creating a new bundle */
1105
1106                 if (size <= max_packet * 2 / 3) {
1107                         struct sk_buff *bundler = buf_acquire(max_packet);
1108                         struct tipc_msg bundler_hdr;
1109
1110                         if (bundler) {
1111                                 msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
1112                                          TIPC_OK, INT_H_SIZE, l_ptr->addr);
1113                                 memcpy(bundler->data, (unchar *)&bundler_hdr, 
1114                                        INT_H_SIZE);
1115                                 skb_trim(bundler, INT_H_SIZE);
1116                                 link_bundle_buf(l_ptr, bundler, buf);
1117                                 buf = bundler;
1118                                 msg = buf_msg(buf);
1119                                 l_ptr->stats.sent_bundles++;
1120                         }
1121                 }
1122         }
1123         if (!l_ptr->next_out)
1124                 l_ptr->next_out = buf;
1125         link_add_to_outqueue(l_ptr, buf, msg);
1126         tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
1127         return dsz;
1128 }
1129
1130 /* 
1131  * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has 
1132  * not been selected yet, and the the owner node is not locked
1133  * Called by TIPC internal users, e.g. the name distributor
1134  */
1135
1136 int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
1137 {
1138         struct link *l_ptr;
1139         struct node *n_ptr;
1140         int res = -ELINKCONG;
1141
1142         read_lock_bh(&tipc_net_lock);
1143         n_ptr = tipc_node_select(dest, selector);
1144         if (n_ptr) {
1145                 tipc_node_lock(n_ptr);
1146                 l_ptr = n_ptr->active_links[selector & 1];
1147                 if (l_ptr) {
1148                         dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
1149                         res = tipc_link_send_buf(l_ptr, buf);
1150                 } else {
1151                         dbg("Attempt to send msg to unreachable node:\n");
1152                         msg_dbg(buf_msg(buf),">>>");
1153                         buf_discard(buf);
1154                 }
1155                 tipc_node_unlock(n_ptr);
1156         } else {
1157                 dbg("Attempt to send msg to unknown node:\n");
1158                 msg_dbg(buf_msg(buf),">>>");
1159                 buf_discard(buf);
1160         }
1161         read_unlock_bh(&tipc_net_lock);
1162         return res;
1163 }
1164
1165 /* 
1166  * link_send_buf_fast: Entry for data messages where the 
1167  * destination link is known and the header is complete,
1168  * inclusive total message length. Very time critical.
1169  * Link is locked. Returns user data length.
1170  */
1171
1172 static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
1173                               u32 *used_max_pkt)
1174 {
1175         struct tipc_msg *msg = buf_msg(buf);
1176         int res = msg_data_sz(msg);
1177
1178         if (likely(!link_congested(l_ptr))) {
1179                 if (likely(msg_size(msg) <= link_max_pkt(l_ptr))) {
1180                         if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
1181                                 link_add_to_outqueue(l_ptr, buf, msg);
1182                                 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
1183                                                             &l_ptr->media_addr))) {
1184                                         l_ptr->unacked_window = 0;
1185                                         msg_dbg(msg,"SENT_FAST:");
1186                                         return res;
1187                                 }
1188                                 dbg("failed sent fast...\n");
1189                                 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1190                                 l_ptr->stats.bearer_congs++;
1191                                 l_ptr->next_out = buf;
1192                                 return res;
1193                         }
1194                 }
1195                 else
1196                         *used_max_pkt = link_max_pkt(l_ptr);
1197         }
1198         return tipc_link_send_buf(l_ptr, buf);  /* All other cases */
1199 }
1200
1201 /* 
1202  * tipc_send_buf_fast: Entry for data messages where the 
1203  * destination node is known and the header is complete,
1204  * inclusive total message length.
1205  * Returns user data length.
1206  */
1207 int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
1208 {
1209         struct link *l_ptr;
1210         struct node *n_ptr;
1211         int res;
1212         u32 selector = msg_origport(buf_msg(buf)) & 1;
1213         u32 dummy;
1214
1215         if (destnode == tipc_own_addr)
1216                 return tipc_port_recv_msg(buf);
1217
1218         read_lock_bh(&tipc_net_lock);
1219         n_ptr = tipc_node_select(destnode, selector);
1220         if (likely(n_ptr)) {
1221                 tipc_node_lock(n_ptr);
1222                 l_ptr = n_ptr->active_links[selector];
1223                 dbg("send_fast: buf %x selected %x, destnode = %x\n",
1224                     buf, l_ptr, destnode);
1225                 if (likely(l_ptr)) {
1226                         res = link_send_buf_fast(l_ptr, buf, &dummy);
1227                         tipc_node_unlock(n_ptr);
1228                         read_unlock_bh(&tipc_net_lock);
1229                         return res;
1230                 }
1231                 tipc_node_unlock(n_ptr);
1232         }
1233         read_unlock_bh(&tipc_net_lock);
1234         res = msg_data_sz(buf_msg(buf));
1235         tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1236         return res;
1237 }
1238
1239
1240 /* 
1241  * tipc_link_send_sections_fast: Entry for messages where the 
1242  * destination processor is known and the header is complete,
1243  * except for total message length. 
1244  * Returns user data length or errno.
1245  */
1246 int tipc_link_send_sections_fast(struct port *sender, 
1247                                  struct iovec const *msg_sect,
1248                                  const u32 num_sect, 
1249                                  u32 destaddr)
1250 {
1251         struct tipc_msg *hdr = &sender->publ.phdr;
1252         struct link *l_ptr;
1253         struct sk_buff *buf;
1254         struct node *node;
1255         int res;
1256         u32 selector = msg_origport(hdr) & 1;
1257
1258 again:
1259         /*
1260          * Try building message using port's max_pkt hint.
1261          * (Must not hold any locks while building message.)
1262          */
1263
1264         res = msg_build(hdr, msg_sect, num_sect, sender->max_pkt,
1265                         !sender->user_port, &buf);
1266
1267         read_lock_bh(&tipc_net_lock);
1268         node = tipc_node_select(destaddr, selector);
1269         if (likely(node)) {
1270                 tipc_node_lock(node);
1271                 l_ptr = node->active_links[selector];
1272                 if (likely(l_ptr)) {
1273                         if (likely(buf)) {
1274                                 res = link_send_buf_fast(l_ptr, buf,
1275                                                          &sender->max_pkt);
1276                                 if (unlikely(res < 0))
1277                                         buf_discard(buf);
1278 exit:
1279                                 tipc_node_unlock(node);
1280                                 read_unlock_bh(&tipc_net_lock);
1281                                 return res;
1282                         }
1283
1284                         /* Exit if build request was invalid */
1285
1286                         if (unlikely(res < 0))
1287                                 goto exit;
1288
1289                         /* Exit if link (or bearer) is congested */
1290
1291                         if (link_congested(l_ptr) || 
1292                             !list_empty(&l_ptr->b_ptr->cong_links)) {
1293                                 res = link_schedule_port(l_ptr,
1294                                                          sender->publ.ref, res);
1295                                 goto exit;
1296                         }
1297
1298                         /* 
1299                          * Message size exceeds max_pkt hint; update hint,
1300                          * then re-try fast path or fragment the message
1301                          */
1302
1303                         sender->max_pkt = link_max_pkt(l_ptr);
1304                         tipc_node_unlock(node);
1305                         read_unlock_bh(&tipc_net_lock);
1306
1307
1308                         if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
1309                                 goto again;
1310
1311                         return link_send_sections_long(sender, msg_sect,
1312                                                        num_sect, destaddr);
1313                 }
1314                 tipc_node_unlock(node);
1315         }
1316         read_unlock_bh(&tipc_net_lock);
1317
1318         /* Couldn't find a link to the destination node */
1319
1320         if (buf)
1321                 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1322         if (res >= 0)
1323                 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1324                                                  TIPC_ERR_NO_NODE);
1325         return res;
1326 }
1327
1328 /* 
1329  * link_send_sections_long(): Entry for long messages where the 
1330  * destination node is known and the header is complete,
1331  * inclusive total message length. 
1332  * Link and bearer congestion status have been checked to be ok,
1333  * and are ignored if they change.
1334  *
1335  * Note that fragments do not use the full link MTU so that they won't have
1336  * to undergo refragmentation if link changeover causes them to be sent
1337  * over another link with an additional tunnel header added as prefix.
1338  * (Refragmentation will still occur if the other link has a smaller MTU.)
1339  *
1340  * Returns user data length or errno.
1341  */
1342 static int link_send_sections_long(struct port *sender,
1343                                    struct iovec const *msg_sect,
1344                                    u32 num_sect,
1345                                    u32 destaddr)
1346 {
1347         struct link *l_ptr;
1348         struct node *node;
1349         struct tipc_msg *hdr = &sender->publ.phdr;
1350         u32 dsz = msg_data_sz(hdr);
1351         u32 max_pkt,fragm_sz,rest;
1352         struct tipc_msg fragm_hdr;
1353         struct sk_buff *buf,*buf_chain,*prev;
1354         u32 fragm_crs,fragm_rest,hsz,sect_rest;
1355         const unchar *sect_crs;
1356         int curr_sect;
1357         u32 fragm_no;
1358
1359 again:
1360         fragm_no = 1;
1361         max_pkt = sender->max_pkt - INT_H_SIZE;  
1362                 /* leave room for tunnel header in case of link changeover */
1363         fragm_sz = max_pkt - INT_H_SIZE; 
1364                 /* leave room for fragmentation header in each fragment */
1365         rest = dsz;
1366         fragm_crs = 0;
1367         fragm_rest = 0;
1368         sect_rest = 0;
1369         sect_crs = NULL;
1370         curr_sect = -1;
1371
1372         /* Prepare reusable fragment header: */
1373
1374         msg_dbg(hdr, ">FRAGMENTING>");
1375         msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
1376                  TIPC_OK, INT_H_SIZE, msg_destnode(hdr));
1377         msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1378         msg_set_size(&fragm_hdr, max_pkt);
1379         msg_set_fragm_no(&fragm_hdr, 1);
1380
1381         /* Prepare header of first fragment: */
1382
1383         buf_chain = buf = buf_acquire(max_pkt);
1384         if (!buf)
1385                 return -ENOMEM;
1386         buf->next = NULL;
1387         memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE);
1388         hsz = msg_hdr_sz(hdr);
1389         memcpy(buf->data + INT_H_SIZE, (unchar *)hdr, hsz);
1390         msg_dbg(buf_msg(buf), ">BUILD>");
1391
1392         /* Chop up message: */
1393
1394         fragm_crs = INT_H_SIZE + hsz;
1395         fragm_rest = fragm_sz - hsz;
1396
1397         do {            /* For all sections */
1398                 u32 sz;
1399
1400                 if (!sect_rest) {
1401                         sect_rest = msg_sect[++curr_sect].iov_len;
1402                         sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1403                 }
1404
1405                 if (sect_rest < fragm_rest)
1406                         sz = sect_rest;
1407                 else
1408                         sz = fragm_rest;
1409
1410                 if (likely(!sender->user_port)) {
1411                         if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1412 error:
1413                                 for (; buf_chain; buf_chain = buf) {
1414                                         buf = buf_chain->next;
1415                                         buf_discard(buf_chain);
1416                                 }
1417                                 return -EFAULT;
1418                         }
1419                 } else
1420                         memcpy(buf->data + fragm_crs, sect_crs, sz);
1421
1422                 sect_crs += sz;
1423                 sect_rest -= sz;
1424                 fragm_crs += sz;
1425                 fragm_rest -= sz;
1426                 rest -= sz;
1427
1428                 if (!fragm_rest && rest) {
1429
1430                         /* Initiate new fragment: */
1431                         if (rest <= fragm_sz) {
1432                                 fragm_sz = rest;
1433                                 msg_set_type(&fragm_hdr,LAST_FRAGMENT);
1434                         } else {
1435                                 msg_set_type(&fragm_hdr, FRAGMENT);
1436                         }
1437                         msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1438                         msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1439                         prev = buf;
1440                         buf = buf_acquire(fragm_sz + INT_H_SIZE);
1441                         if (!buf)
1442                                 goto error;
1443
1444                         buf->next = NULL;                                
1445                         prev->next = buf;
1446                         memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE);
1447                         fragm_crs = INT_H_SIZE;
1448                         fragm_rest = fragm_sz;
1449                         msg_dbg(buf_msg(buf),"  >BUILD>");
1450                 }
1451         }
1452         while (rest > 0);
1453
1454         /* 
1455          * Now we have a buffer chain. Select a link and check
1456          * that packet size is still OK
1457          */
1458         node = tipc_node_select(destaddr, sender->publ.ref & 1);
1459         if (likely(node)) {
1460                 tipc_node_lock(node);
1461                 l_ptr = node->active_links[sender->publ.ref & 1];
1462                 if (!l_ptr) {
1463                         tipc_node_unlock(node);
1464                         goto reject;
1465                 }
1466                 if (link_max_pkt(l_ptr) < max_pkt) {
1467                         sender->max_pkt = link_max_pkt(l_ptr);
1468                         tipc_node_unlock(node);
1469                         for (; buf_chain; buf_chain = buf) {
1470                                 buf = buf_chain->next;
1471                                 buf_discard(buf_chain);
1472                         }
1473                         goto again;
1474                 }
1475         } else {
1476 reject:
1477                 for (; buf_chain; buf_chain = buf) {
1478                         buf = buf_chain->next;
1479                         buf_discard(buf_chain);
1480                 }
1481                 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1482                                                  TIPC_ERR_NO_NODE);
1483         }
1484
1485         /* Append whole chain to send queue: */
1486
1487         buf = buf_chain;
1488         l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1489         if (!l_ptr->next_out)
1490                 l_ptr->next_out = buf_chain;
1491         l_ptr->stats.sent_fragmented++;
1492         while (buf) {
1493                 struct sk_buff *next = buf->next;
1494                 struct tipc_msg *msg = buf_msg(buf);
1495
1496                 l_ptr->stats.sent_fragments++;
1497                 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1498                 link_add_to_outqueue(l_ptr, buf, msg);
1499                 msg_dbg(msg, ">ADD>");
1500                 buf = next;
1501         }
1502
1503         /* Send it, if possible: */
1504
1505         tipc_link_push_queue(l_ptr);
1506         tipc_node_unlock(node);
1507         return dsz;
1508 }
1509
1510 /* 
1511  * tipc_link_push_packet: Push one unsent packet to the media
1512  */
1513 u32 tipc_link_push_packet(struct link *l_ptr)
1514 {
1515         struct sk_buff *buf = l_ptr->first_out;
1516         u32 r_q_size = l_ptr->retransm_queue_size;
1517         u32 r_q_head = l_ptr->retransm_queue_head;
1518
1519         /* Step to position where retransmission failed, if any,    */
1520         /* consider that buffers may have been released in meantime */
1521
1522         if (r_q_size && buf) {
1523                 u32 last = lesser(mod(r_q_head + r_q_size), 
1524                                   link_last_sent(l_ptr));
1525                 u32 first = msg_seqno(buf_msg(buf));
1526
1527                 while (buf && less(first, r_q_head)) {
1528                         first = mod(first + 1);
1529                         buf = buf->next;
1530                 }
1531                 l_ptr->retransm_queue_head = r_q_head = first;
1532                 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1533         }
1534
1535         /* Continue retransmission now, if there is anything: */
1536
1537         if (r_q_size && buf && !skb_cloned(buf)) {
1538                 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1539                 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in); 
1540                 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1541                         msg_dbg(buf_msg(buf), ">DEF-RETR>");
1542                         l_ptr->retransm_queue_head = mod(++r_q_head);
1543                         l_ptr->retransm_queue_size = --r_q_size;
1544                         l_ptr->stats.retransmitted++;
1545                         return TIPC_OK;
1546                 } else {
1547                         l_ptr->stats.bearer_congs++;
1548                         msg_dbg(buf_msg(buf), "|>DEF-RETR>");
1549                         return PUSH_FAILED;
1550                 }
1551         }
1552
1553         /* Send deferred protocol message, if any: */
1554
1555         buf = l_ptr->proto_msg_queue;
1556         if (buf) {
1557                 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1558                 msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in); 
1559                 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1560                         msg_dbg(buf_msg(buf), ">DEF-PROT>");
1561                         l_ptr->unacked_window = 0;
1562                         buf_discard(buf);
1563                         l_ptr->proto_msg_queue = NULL;
1564                         return TIPC_OK;
1565                 } else {
1566                         msg_dbg(buf_msg(buf), "|>DEF-PROT>");
1567                         l_ptr->stats.bearer_congs++;
1568                         return PUSH_FAILED;
1569                 }
1570         }
1571
1572         /* Send one deferred data message, if send window not full: */
1573
1574         buf = l_ptr->next_out;
1575         if (buf) {
1576                 struct tipc_msg *msg = buf_msg(buf);
1577                 u32 next = msg_seqno(msg);
1578                 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1579
1580                 if (mod(next - first) < l_ptr->queue_limit[0]) {
1581                         msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1582                         msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in); 
1583                         if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1584                                 if (msg_user(msg) == MSG_BUNDLER)
1585                                         msg_set_type(msg, CLOSED_MSG);
1586                                 msg_dbg(msg, ">PUSH-DATA>");
1587                                 l_ptr->next_out = buf->next;
1588                                 return TIPC_OK;
1589                         } else {
1590                                 msg_dbg(msg, "|PUSH-DATA|");
1591                                 l_ptr->stats.bearer_congs++;
1592                                 return PUSH_FAILED;
1593                         }
1594                 }
1595         }
1596         return PUSH_FINISHED;
1597 }
1598
1599 /*
1600  * push_queue(): push out the unsent messages of a link where
1601  *               congestion has abated. Node is locked
1602  */
1603 void tipc_link_push_queue(struct link *l_ptr)
1604 {
1605         u32 res;
1606
1607         if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
1608                 return;
1609
1610         do {
1611                 res = tipc_link_push_packet(l_ptr);
1612         }
1613         while (res == TIPC_OK);
1614         if (res == PUSH_FAILED)
1615                 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1616 }
1617
1618 static void link_reset_all(unsigned long addr)
1619 {
1620         struct node *n_ptr;
1621         char addr_string[16];
1622         u32 i;
1623
1624         read_lock_bh(&tipc_net_lock);
1625         n_ptr = tipc_node_find((u32)addr);
1626         if (!n_ptr) {
1627                 read_unlock_bh(&tipc_net_lock);
1628                 return; /* node no longer exists */
1629         }
1630
1631         tipc_node_lock(n_ptr);
1632
1633         warn("Resetting all links to %s\n", 
1634              addr_string_fill(addr_string, n_ptr->addr));
1635
1636         for (i = 0; i < MAX_BEARERS; i++) {
1637                 if (n_ptr->links[i]) {
1638                         link_print(n_ptr->links[i], TIPC_OUTPUT, 
1639                                    "Resetting link\n");
1640                         tipc_link_reset(n_ptr->links[i]);
1641                 }
1642         }
1643
1644         tipc_node_unlock(n_ptr);
1645         read_unlock_bh(&tipc_net_lock);
1646 }
1647
1648 static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1649 {
1650         struct tipc_msg *msg = buf_msg(buf);
1651
1652         warn("Retransmission failure on link <%s>\n", l_ptr->name);
1653         tipc_msg_print(TIPC_OUTPUT, msg, ">RETR-FAIL>");
1654
1655         if (l_ptr->addr) {
1656
1657                 /* Handle failure on standard link */
1658
1659                 link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
1660                 tipc_link_reset(l_ptr);
1661
1662         } else {
1663
1664                 /* Handle failure on broadcast link */
1665
1666                 struct node *n_ptr;
1667                 char addr_string[16];
1668
1669                 tipc_printf(TIPC_OUTPUT, "Msg seq number: %u,  ", msg_seqno(msg));
1670                 tipc_printf(TIPC_OUTPUT, "Outstanding acks: %u\n", (u32)TIPC_SKB_CB(buf)->handle);
1671                 
1672                 n_ptr = l_ptr->owner->next;
1673                 tipc_node_lock(n_ptr);
1674
1675                 addr_string_fill(addr_string, n_ptr->addr);
1676                 tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
1677                 tipc_printf(TIPC_OUTPUT, "Supported: %d,  ", n_ptr->bclink.supported);
1678                 tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
1679                 tipc_printf(TIPC_OUTPUT, "Last in: %u,  ", n_ptr->bclink.last_in);
1680                 tipc_printf(TIPC_OUTPUT, "Gap after: %u,  ", n_ptr->bclink.gap_after);
1681                 tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
1682                 tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1683
1684                 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1685
1686                 tipc_node_unlock(n_ptr);
1687
1688                 l_ptr->stale_count = 0;
1689         }
1690 }
1691
1692 void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf, 
1693                           u32 retransmits)
1694 {
1695         struct tipc_msg *msg;
1696
1697         if (!buf)
1698                 return;
1699
1700         msg = buf_msg(buf);
1701         
1702         dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
1703
1704         if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
1705                 if (!skb_cloned(buf)) {
1706                         msg_dbg(msg, ">NO_RETR->BCONG>");
1707                         dbg_print_link(l_ptr, "   ");
1708                         l_ptr->retransm_queue_head = msg_seqno(msg);
1709                         l_ptr->retransm_queue_size = retransmits;
1710                         return;
1711                 } else {
1712                         /* Don't retransmit if driver already has the buffer */
1713                 }
1714         } else {
1715                 /* Detect repeated retransmit failures on uncongested bearer */
1716
1717                 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1718                         if (++l_ptr->stale_count > 100) {
1719                                 link_retransmit_failure(l_ptr, buf);
1720                                 return;
1721                         }
1722                 } else {
1723                         l_ptr->last_retransmitted = msg_seqno(msg);
1724                         l_ptr->stale_count = 1;
1725                 }
1726         }
1727
1728         while (retransmits && (buf != l_ptr->next_out) && buf && !skb_cloned(buf)) {
1729                 msg = buf_msg(buf);
1730                 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1731                 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in); 
1732                 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1733                         msg_dbg(buf_msg(buf), ">RETR>");
1734                         buf = buf->next;
1735                         retransmits--;
1736                         l_ptr->stats.retransmitted++;
1737                 } else {
1738                         tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1739                         l_ptr->stats.bearer_congs++;
1740                         l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1741                         l_ptr->retransm_queue_size = retransmits;
1742                         return;
1743                 }
1744         }
1745
1746         l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1747 }
1748
1749 /* 
1750  * link_recv_non_seq: Receive packets which are outside
1751  *                    the link sequence flow
1752  */
1753
1754 static void link_recv_non_seq(struct sk_buff *buf)
1755 {
1756         struct tipc_msg *msg = buf_msg(buf);
1757
1758         if (msg_user(msg) ==  LINK_CONFIG)
1759                 tipc_disc_recv_msg(buf);
1760         else
1761                 tipc_bclink_recv_pkt(buf);
1762 }
1763
1764 /** 
1765  * link_insert_deferred_queue - insert deferred messages back into receive chain
1766  */
1767
1768 static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr, 
1769                                                   struct sk_buff *buf)
1770 {
1771         u32 seq_no;
1772
1773         if (l_ptr->oldest_deferred_in == NULL)
1774                 return buf;
1775
1776         seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1777         if (seq_no == mod(l_ptr->next_in_no)) {
1778                 l_ptr->newest_deferred_in->next = buf;
1779                 buf = l_ptr->oldest_deferred_in;
1780                 l_ptr->oldest_deferred_in = NULL;
1781                 l_ptr->deferred_inqueue_sz = 0;
1782         }
1783         return buf;
1784 }
1785
1786 void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1787 {
1788         read_lock_bh(&tipc_net_lock);
1789         while (head) {
1790                 struct bearer *b_ptr;
1791                 struct node *n_ptr;
1792                 struct link *l_ptr;
1793                 struct sk_buff *crs;
1794                 struct sk_buff *buf = head;
1795                 struct tipc_msg *msg = buf_msg(buf);
1796                 u32 seq_no = msg_seqno(msg);
1797                 u32 ackd = msg_ack(msg);
1798                 u32 released = 0;
1799                 int type;
1800
1801                 b_ptr = (struct bearer *)tb_ptr;
1802                 TIPC_SKB_CB(buf)->handle = b_ptr;
1803
1804                 head = head->next;
1805                 if (unlikely(msg_version(msg) != TIPC_VERSION))
1806                         goto cont;
1807 #if 0
1808                 if (msg_user(msg) != LINK_PROTOCOL)
1809 #endif
1810                         msg_dbg(msg,"<REC<");
1811
1812                 if (unlikely(msg_non_seq(msg))) {
1813                         link_recv_non_seq(buf);
1814                         continue;
1815                 }
1816                 
1817                 if (unlikely(!msg_short(msg) &&
1818                              (msg_destnode(msg) != tipc_own_addr)))
1819                         goto cont;
1820                 
1821                 n_ptr = tipc_node_find(msg_prevnode(msg));
1822                 if (unlikely(!n_ptr))
1823                         goto cont;
1824
1825                 tipc_node_lock(n_ptr);
1826                 l_ptr = n_ptr->links[b_ptr->identity];
1827                 if (unlikely(!l_ptr)) {
1828                         tipc_node_unlock(n_ptr);
1829                         goto cont;
1830                 }
1831                 /* 
1832                  * Release acked messages 
1833                  */
1834                 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
1835                         if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1836                                 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
1837                 }
1838
1839                 crs = l_ptr->first_out;
1840                 while ((crs != l_ptr->next_out) && 
1841                        less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1842                         struct sk_buff *next = crs->next;
1843
1844                         buf_discard(crs);
1845                         crs = next;
1846                         released++;
1847                 }
1848                 if (released) {
1849                         l_ptr->first_out = crs;
1850                         l_ptr->out_queue_size -= released;
1851                 }
1852                 if (unlikely(l_ptr->next_out))
1853                         tipc_link_push_queue(l_ptr);
1854                 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
1855                         tipc_link_wakeup_ports(l_ptr, 0);
1856                 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1857                         l_ptr->stats.sent_acks++;
1858                         tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1859                 }
1860
1861 protocol_check:
1862                 if (likely(link_working_working(l_ptr))) {
1863                         if (likely(seq_no == mod(l_ptr->next_in_no))) {
1864                                 l_ptr->next_in_no++;
1865                                 if (unlikely(l_ptr->oldest_deferred_in))
1866                                         head = link_insert_deferred_queue(l_ptr,
1867                                                                           head);
1868                                 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1869 deliver:
1870                                         if (likely(msg_isdata(msg))) {
1871                                                 tipc_node_unlock(n_ptr);
1872                                                 tipc_port_recv_msg(buf);
1873                                                 continue;
1874                                         }
1875                                         switch (msg_user(msg)) {
1876                                         case MSG_BUNDLER:
1877                                                 l_ptr->stats.recv_bundles++;
1878                                                 l_ptr->stats.recv_bundled += 
1879                                                         msg_msgcnt(msg);
1880                                                 tipc_node_unlock(n_ptr);
1881                                                 tipc_link_recv_bundle(buf);
1882                                                 continue;
1883                                         case ROUTE_DISTRIBUTOR:
1884                                                 tipc_node_unlock(n_ptr);
1885                                                 tipc_cltr_recv_routing_table(buf);
1886                                                 continue;
1887                                         case NAME_DISTRIBUTOR:
1888                                                 tipc_node_unlock(n_ptr);
1889                                                 tipc_named_recv(buf);
1890                                                 continue;
1891                                         case CONN_MANAGER:
1892                                                 tipc_node_unlock(n_ptr);
1893                                                 tipc_port_recv_proto_msg(buf);
1894                                                 continue;
1895                                         case MSG_FRAGMENTER:
1896                                                 l_ptr->stats.recv_fragments++;
1897                                                 if (tipc_link_recv_fragment(&l_ptr->defragm_buf, 
1898                                                                             &buf, &msg)) {
1899                                                         l_ptr->stats.recv_fragmented++;
1900                                                         goto deliver;
1901                                                 }
1902                                                 break;
1903                                         case CHANGEOVER_PROTOCOL:
1904                                                 type = msg_type(msg);
1905                                                 if (link_recv_changeover_msg(&l_ptr, &buf)) {
1906                                                         msg = buf_msg(buf);
1907                                                         seq_no = msg_seqno(msg);
1908                                                         TIPC_SKB_CB(buf)->handle 
1909                                                                 = b_ptr;
1910                                                         if (type == ORIGINAL_MSG)
1911                                                                 goto deliver;
1912                                                         goto protocol_check;
1913                                                 }
1914                                                 break;
1915                                         }
1916                                 }
1917                                 tipc_node_unlock(n_ptr);
1918                                 tipc_net_route_msg(buf);
1919                                 continue;
1920                         }
1921                         link_handle_out_of_seq_msg(l_ptr, buf);
1922                         head = link_insert_deferred_queue(l_ptr, head);
1923                         tipc_node_unlock(n_ptr);
1924                         continue;
1925                 }
1926
1927                 if (msg_user(msg) == LINK_PROTOCOL) {
1928                         link_recv_proto_msg(l_ptr, buf);
1929                         head = link_insert_deferred_queue(l_ptr, head);
1930                         tipc_node_unlock(n_ptr);
1931                         continue;
1932                 }
1933                 msg_dbg(msg,"NSEQ<REC<");
1934                 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1935
1936                 if (link_working_working(l_ptr)) {
1937                         /* Re-insert in front of queue */
1938                         msg_dbg(msg,"RECV-REINS:");
1939                         buf->next = head;
1940                         head = buf;
1941                         tipc_node_unlock(n_ptr);
1942                         continue;
1943                 }
1944                 tipc_node_unlock(n_ptr);
1945 cont:
1946                 buf_discard(buf);
1947         }
1948         read_unlock_bh(&tipc_net_lock);
1949 }
1950
1951 /* 
1952  * link_defer_buf(): Sort a received out-of-sequence packet 
1953  *                   into the deferred reception queue.
1954  * Returns the increase of the queue length,i.e. 0 or 1
1955  */
1956
1957 u32 tipc_link_defer_pkt(struct sk_buff **head,
1958                         struct sk_buff **tail,
1959                         struct sk_buff *buf)
1960 {
1961         struct sk_buff *prev = NULL;
1962         struct sk_buff *crs = *head;
1963         u32 seq_no = msg_seqno(buf_msg(buf));
1964
1965         buf->next = NULL;
1966
1967         /* Empty queue ? */
1968         if (*head == NULL) {
1969                 *head = *tail = buf;
1970                 return 1;
1971         }
1972
1973         /* Last ? */
1974         if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1975                 (*tail)->next = buf;
1976                 *tail = buf;
1977                 return 1;
1978         }
1979
1980         /* Scan through queue and sort it in */
1981         do {
1982                 struct tipc_msg *msg = buf_msg(crs);
1983
1984                 if (less(seq_no, msg_seqno(msg))) {
1985                         buf->next = crs;
1986                         if (prev)
1987                                 prev->next = buf;
1988                         else
1989                                 *head = buf;   
1990                         return 1;
1991                 }
1992                 if (seq_no == msg_seqno(msg)) {
1993                         break;
1994                 }
1995                 prev = crs;
1996                 crs = crs->next;
1997         }
1998         while (crs);
1999
2000         /* Message is a duplicate of an existing message */
2001
2002         buf_discard(buf);
2003         return 0;
2004 }
2005
2006 /** 
2007  * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
2008  */
2009
2010 static void link_handle_out_of_seq_msg(struct link *l_ptr, 
2011                                        struct sk_buff *buf)
2012 {
2013         u32 seq_no = msg_seqno(buf_msg(buf));
2014
2015         if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
2016                 link_recv_proto_msg(l_ptr, buf);
2017                 return;
2018         }
2019
2020         dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n", 
2021             seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
2022
2023         /* Record OOS packet arrival (force mismatch on next timeout) */
2024
2025         l_ptr->checkpoint--;
2026
2027         /* 
2028          * Discard packet if a duplicate; otherwise add it to deferred queue
2029          * and notify peer of gap as per protocol specification
2030          */
2031
2032         if (less(seq_no, mod(l_ptr->next_in_no))) {
2033                 l_ptr->stats.duplicates++;
2034                 buf_discard(buf);
2035                 return;
2036         }
2037
2038         if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
2039                                 &l_ptr->newest_deferred_in, buf)) {
2040                 l_ptr->deferred_inqueue_sz++;
2041                 l_ptr->stats.deferred_recv++;
2042                 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
2043                         tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
2044         } else
2045                 l_ptr->stats.duplicates++;
2046 }
2047
2048 /*
2049  * Send protocol message to the other endpoint.
2050  */
2051 void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
2052                               u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
2053 {
2054         struct sk_buff *buf = NULL;
2055         struct tipc_msg *msg = l_ptr->pmsg;
2056         u32 msg_size = sizeof(l_ptr->proto_msg);
2057
2058         if (link_blocked(l_ptr))
2059                 return;
2060         msg_set_type(msg, msg_typ);
2061         msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
2062         msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in)); 
2063         msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
2064
2065         if (msg_typ == STATE_MSG) {
2066                 u32 next_sent = mod(l_ptr->next_out_no);
2067
2068                 if (!tipc_link_is_up(l_ptr))
2069                         return;
2070                 if (l_ptr->next_out)
2071                         next_sent = msg_seqno(buf_msg(l_ptr->next_out));
2072                 msg_set_next_sent(msg, next_sent);
2073                 if (l_ptr->oldest_deferred_in) {
2074                         u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2075                         gap = mod(rec - mod(l_ptr->next_in_no));
2076                 }
2077                 msg_set_seq_gap(msg, gap);
2078                 if (gap)
2079                         l_ptr->stats.sent_nacks++;
2080                 msg_set_link_tolerance(msg, tolerance);
2081                 msg_set_linkprio(msg, priority);
2082                 msg_set_max_pkt(msg, ack_mtu);
2083                 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
2084                 msg_set_probe(msg, probe_msg != 0);
2085                 if (probe_msg) { 
2086                         u32 mtu = l_ptr->max_pkt;
2087
2088                         if ((mtu < l_ptr->max_pkt_target) &&
2089                             link_working_working(l_ptr) &&
2090                             l_ptr->fsm_msg_cnt) {
2091                                 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
2092                                 if (l_ptr->max_pkt_probes == 10) {
2093                                         l_ptr->max_pkt_target = (msg_size - 4);
2094                                         l_ptr->max_pkt_probes = 0;
2095                                         msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
2096                                 }
2097                                 l_ptr->max_pkt_probes++;
2098                         }
2099
2100                         l_ptr->stats.sent_probes++;
2101                 }
2102                 l_ptr->stats.sent_states++;
2103         } else {                /* RESET_MSG or ACTIVATE_MSG */
2104                 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
2105                 msg_set_seq_gap(msg, 0);
2106                 msg_set_next_sent(msg, 1);
2107                 msg_set_link_tolerance(msg, l_ptr->tolerance);
2108                 msg_set_linkprio(msg, l_ptr->priority);
2109                 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
2110         }
2111
2112         if (tipc_node_has_redundant_links(l_ptr->owner)) {
2113                 msg_set_redundant_link(msg);
2114         } else {
2115                 msg_clear_redundant_link(msg);
2116         }
2117         msg_set_linkprio(msg, l_ptr->priority);
2118
2119         /* Ensure sequence number will not fit : */
2120
2121         msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
2122
2123         /* Congestion? */
2124
2125         if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
2126                 if (!l_ptr->proto_msg_queue) {
2127                         l_ptr->proto_msg_queue =
2128                                 buf_acquire(sizeof(l_ptr->proto_msg));
2129                 }
2130                 buf = l_ptr->proto_msg_queue;
2131                 if (!buf)
2132                         return;
2133                 memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg));
2134                 return;
2135         }
2136         msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
2137
2138         /* Message can be sent */
2139
2140         msg_dbg(msg, ">>");
2141
2142         buf = buf_acquire(msg_size);
2143         if (!buf)
2144                 return;
2145
2146         memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg));
2147         msg_set_size(buf_msg(buf), msg_size);
2148
2149         if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
2150                 l_ptr->unacked_window = 0;
2151                 buf_discard(buf);
2152                 return;
2153         }
2154
2155         /* New congestion */
2156         tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
2157         l_ptr->proto_msg_queue = buf;
2158         l_ptr->stats.bearer_congs++;
2159 }
2160
2161 /*
2162  * Receive protocol message :
2163  * Note that network plane id propagates through the network, and may 
2164  * change at any time. The node with lowest address rules    
2165  */
2166
2167 static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2168 {
2169         u32 rec_gap = 0;
2170         u32 max_pkt_info;
2171         u32 max_pkt_ack;
2172         u32 msg_tol;
2173         struct tipc_msg *msg = buf_msg(buf);
2174
2175         dbg("AT(%u):", jiffies_to_msecs(jiffies));
2176         msg_dbg(msg, "<<");
2177         if (link_blocked(l_ptr))
2178                 goto exit;
2179
2180         /* record unnumbered packet arrival (force mismatch on next timeout) */
2181
2182         l_ptr->checkpoint--;
2183
2184         if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2185                 if (tipc_own_addr > msg_prevnode(msg))
2186                         l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2187
2188         l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2189
2190         switch (msg_type(msg)) {
2191         
2192         case RESET_MSG:
2193                 if (!link_working_unknown(l_ptr) && l_ptr->peer_session) {
2194                         if (msg_session(msg) == l_ptr->peer_session) {
2195                                 dbg("Duplicate RESET: %u<->%u\n",
2196                                     msg_session(msg), l_ptr->peer_session);                                     
2197                                 break; /* duplicate: ignore */
2198                         }
2199                 }
2200                 /* fall thru' */
2201         case ACTIVATE_MSG:
2202                 /* Update link settings according other endpoint's values */
2203
2204                 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2205
2206                 if ((msg_tol = msg_link_tolerance(msg)) &&
2207                     (msg_tol > l_ptr->tolerance))
2208                         link_set_supervision_props(l_ptr, msg_tol);
2209
2210                 if (msg_linkprio(msg) > l_ptr->priority)
2211                         l_ptr->priority = msg_linkprio(msg);
2212
2213                 max_pkt_info = msg_max_pkt(msg);
2214                 if (max_pkt_info) {
2215                         if (max_pkt_info < l_ptr->max_pkt_target)
2216                                 l_ptr->max_pkt_target = max_pkt_info;
2217                         if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2218                                 l_ptr->max_pkt = l_ptr->max_pkt_target;
2219                 } else {
2220                         l_ptr->max_pkt = l_ptr->max_pkt_target;
2221                 }
2222                 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2223
2224                 link_state_event(l_ptr, msg_type(msg));
2225
2226                 l_ptr->peer_session = msg_session(msg);
2227                 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2228
2229                 /* Synchronize broadcast sequence numbers */
2230                 if (!tipc_node_has_redundant_links(l_ptr->owner)) {
2231                         l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2232                 }
2233                 break;
2234         case STATE_MSG:
2235
2236                 if ((msg_tol = msg_link_tolerance(msg)))
2237                         link_set_supervision_props(l_ptr, msg_tol);
2238                 
2239                 if (msg_linkprio(msg) && 
2240                     (msg_linkprio(msg) != l_ptr->priority)) {
2241                         warn("Resetting link <%s>, priority change %u->%u\n",
2242                              l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2243                         l_ptr->priority = msg_linkprio(msg);
2244                         tipc_link_reset(l_ptr); /* Enforce change to take effect */
2245                         break;
2246                 }
2247                 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2248                 l_ptr->stats.recv_states++;
2249                 if (link_reset_unknown(l_ptr))
2250                         break;
2251
2252                 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
2253                         rec_gap = mod(msg_next_sent(msg) - 
2254                                       mod(l_ptr->next_in_no));
2255                 }
2256
2257                 max_pkt_ack = msg_max_pkt(msg);
2258                 if (max_pkt_ack > l_ptr->max_pkt) {
2259                         dbg("Link <%s> updated MTU %u -> %u\n",
2260                             l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
2261                         l_ptr->max_pkt = max_pkt_ack;
2262                         l_ptr->max_pkt_probes = 0;
2263                 }
2264
2265                 max_pkt_ack = 0;
2266                 if (msg_probe(msg)) {
2267                         l_ptr->stats.recv_probes++;
2268                         if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2269                                 max_pkt_ack = msg_size(msg);
2270                         }
2271                 }
2272
2273                 /* Protocol message before retransmits, reduce loss risk */
2274
2275                 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
2276
2277                 if (rec_gap || (msg_probe(msg))) {
2278                         tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2279                                                  0, rec_gap, 0, 0, max_pkt_ack);
2280                 }
2281                 if (msg_seq_gap(msg)) {
2282                         msg_dbg(msg, "With Gap:");
2283                         l_ptr->stats.recv_nacks++;
2284                         tipc_link_retransmit(l_ptr, l_ptr->first_out,
2285                                              msg_seq_gap(msg));
2286                 }
2287                 break;
2288         default:
2289                 msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
2290         }
2291 exit:
2292         buf_discard(buf);
2293 }
2294
2295
2296 /*
2297  * tipc_link_tunnel(): Send one message via a link belonging to 
2298  * another bearer. Owner node is locked.
2299  */
2300 void tipc_link_tunnel(struct link *l_ptr, 
2301                       struct tipc_msg *tunnel_hdr, 
2302                       struct tipc_msg  *msg,
2303                       u32 selector)
2304 {
2305         struct link *tunnel;
2306         struct sk_buff *buf;
2307         u32 length = msg_size(msg);
2308
2309         tunnel = l_ptr->owner->active_links[selector & 1];
2310         if (!tipc_link_is_up(tunnel)) {
2311                 warn("Link changeover error, "
2312                      "tunnel link no longer available\n");
2313                 return;
2314         }
2315         msg_set_size(tunnel_hdr, length + INT_H_SIZE);
2316         buf = buf_acquire(length + INT_H_SIZE);
2317         if (!buf) {
2318                 warn("Link changeover error, "
2319                      "unable to send tunnel msg\n");
2320                 return;
2321         }
2322         memcpy(buf->data, (unchar *)tunnel_hdr, INT_H_SIZE);
2323         memcpy(buf->data + INT_H_SIZE, (unchar *)msg, length);
2324         dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
2325         msg_dbg(buf_msg(buf), ">SEND>");
2326         tipc_link_send_buf(tunnel, buf);
2327 }
2328
2329
2330
2331 /*
2332  * changeover(): Send whole message queue via the remaining link
2333  *               Owner node is locked.
2334  */
2335
2336 void tipc_link_changeover(struct link *l_ptr)
2337 {
2338         u32 msgcount = l_ptr->out_queue_size;
2339         struct sk_buff *crs = l_ptr->first_out;
2340         struct link *tunnel = l_ptr->owner->active_links[0];
2341         struct tipc_msg tunnel_hdr;
2342         int split_bundles;
2343
2344         if (!tunnel)
2345                 return;
2346
2347         if (!l_ptr->owner->permit_changeover) {
2348                 warn("Link changeover error, "
2349                      "peer did not permit changeover\n");
2350                 return;
2351         }
2352
2353         msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2354                  ORIGINAL_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
2355         msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2356         msg_set_msgcnt(&tunnel_hdr, msgcount);
2357         dbg("Link changeover requires %u tunnel messages\n", msgcount);
2358
2359         if (!l_ptr->first_out) {
2360                 struct sk_buff *buf;
2361
2362                 buf = buf_acquire(INT_H_SIZE);
2363                 if (buf) {
2364                         memcpy(buf->data, (unchar *)&tunnel_hdr, INT_H_SIZE);
2365                         msg_set_size(&tunnel_hdr, INT_H_SIZE);
2366                         dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2367                             tunnel->b_ptr->net_plane);
2368                         msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
2369                         tipc_link_send_buf(tunnel, buf);
2370                 } else {
2371                         warn("Link changeover error, "
2372                              "unable to send changeover msg\n");
2373                 }
2374                 return;
2375         }
2376
2377         split_bundles = (l_ptr->owner->active_links[0] != 
2378                          l_ptr->owner->active_links[1]);
2379
2380         while (crs) {
2381                 struct tipc_msg *msg = buf_msg(crs);
2382
2383                 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
2384                         u32 msgcount = msg_msgcnt(msg);
2385                         struct tipc_msg *m = msg_get_wrapped(msg);
2386                         unchar* pos = (unchar*)m;
2387
2388                         while (msgcount--) {
2389                                 msg_set_seqno(m,msg_seqno(msg));
2390                                 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2391                                                  msg_link_selector(m));
2392                                 pos += align(msg_size(m));
2393                                 m = (struct tipc_msg *)pos;
2394                         }
2395                 } else {
2396                         tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2397                                          msg_link_selector(msg));
2398                 }
2399                 crs = crs->next;
2400         }
2401 }
2402
2403 void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
2404 {
2405         struct sk_buff *iter;
2406         struct tipc_msg tunnel_hdr;
2407
2408         msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2409                  DUPLICATE_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
2410         msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2411         msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2412         iter = l_ptr->first_out;
2413         while (iter) {
2414                 struct sk_buff *outbuf;
2415                 struct tipc_msg *msg = buf_msg(iter);
2416                 u32 length = msg_size(msg);
2417
2418                 if (msg_user(msg) == MSG_BUNDLER)
2419                         msg_set_type(msg, CLOSED_MSG);
2420                 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));   /* Update */
2421                 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in); 
2422                 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
2423                 outbuf = buf_acquire(length + INT_H_SIZE);
2424                 if (outbuf == NULL) {
2425                         warn("Link changeover error, "
2426                              "unable to send duplicate msg\n");
2427                         return;
2428                 }
2429                 memcpy(outbuf->data, (unchar *)&tunnel_hdr, INT_H_SIZE);
2430                 memcpy(outbuf->data + INT_H_SIZE, iter->data, length);
2431                 dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2432                     tunnel->b_ptr->net_plane);
2433                 msg_dbg(buf_msg(outbuf), ">SEND>");
2434                 tipc_link_send_buf(tunnel, outbuf);
2435                 if (!tipc_link_is_up(l_ptr))
2436                         return;
2437                 iter = iter->next;
2438         }
2439 }
2440
2441
2442
2443 /**
2444  * buf_extract - extracts embedded TIPC message from another message
2445  * @skb: encapsulating message buffer
2446  * @from_pos: offset to extract from
2447  *
2448  * Returns a new message buffer containing an embedded message.  The 
2449  * encapsulating message itself is left unchanged.
2450  */
2451
2452 static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2453 {
2454         struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2455         u32 size = msg_size(msg);
2456         struct sk_buff *eb;
2457
2458         eb = buf_acquire(size);
2459         if (eb)
2460                 memcpy(eb->data, (unchar *)msg, size);
2461         return eb;
2462 }
2463
2464 /* 
2465  *  link_recv_changeover_msg(): Receive tunneled packet sent
2466  *  via other link. Node is locked. Return extracted buffer.
2467  */
2468
2469 static int link_recv_changeover_msg(struct link **l_ptr,
2470                                     struct sk_buff **buf)
2471 {
2472         struct sk_buff *tunnel_buf = *buf;
2473         struct link *dest_link;
2474         struct tipc_msg *msg;
2475         struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2476         u32 msg_typ = msg_type(tunnel_msg);
2477         u32 msg_count = msg_msgcnt(tunnel_msg);
2478
2479         dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
2480         if (!dest_link) {
2481                 msg_dbg(tunnel_msg, "NOLINK/<REC<");
2482                 goto exit;
2483         }
2484         if (dest_link == *l_ptr) {
2485                 err("Unexpected changeover message on link <%s>\n", 
2486                     (*l_ptr)->name);
2487                 goto exit;
2488         }
2489         dbg("%c<-%c:", dest_link->b_ptr->net_plane,
2490             (*l_ptr)->b_ptr->net_plane);
2491         *l_ptr = dest_link;
2492         msg = msg_get_wrapped(tunnel_msg);
2493
2494         if (msg_typ == DUPLICATE_MSG) {
2495                 if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
2496                         msg_dbg(tunnel_msg, "DROP/<REC<");
2497                         goto exit;
2498                 }
2499                 *buf = buf_extract(tunnel_buf,INT_H_SIZE);
2500                 if (*buf == NULL) {
2501                         warn("Link changeover error, duplicate msg dropped\n");
2502                         goto exit;
2503                 }
2504                 msg_dbg(tunnel_msg, "TNL<REC<");
2505                 buf_discard(tunnel_buf);
2506                 return 1;
2507         }
2508
2509         /* First original message ?: */
2510
2511         if (tipc_link_is_up(dest_link)) {
2512                 msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
2513                 info("Resetting link <%s>, changeover initiated by peer\n",
2514                      dest_link->name);
2515                 tipc_link_reset(dest_link);
2516                 dest_link->exp_msg_count = msg_count;
2517                 dbg("Expecting %u tunnelled messages\n", msg_count);
2518                 if (!msg_count)
2519                         goto exit;
2520         } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
2521                 msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
2522                 dest_link->exp_msg_count = msg_count;
2523                 dbg("Expecting %u tunnelled messages\n", msg_count);
2524                 if (!msg_count)
2525                         goto exit;
2526         }
2527
2528         /* Receive original message */
2529
2530         if (dest_link->exp_msg_count == 0) {
2531                 warn("Link switchover error, "
2532                      "got too many tunnelled messages\n");
2533                 msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
2534                 dbg_print_link(dest_link, "LINK:");
2535                 goto exit;
2536         }
2537         dest_link->exp_msg_count--;
2538         if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
2539                 msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
2540                 goto exit;
2541         } else {
2542                 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2543                 if (*buf != NULL) {
2544                         msg_dbg(tunnel_msg, "TNL<REC<");
2545                         buf_discard(tunnel_buf);
2546                         return 1;
2547                 } else {
2548                         warn("Link changeover error, original msg dropped\n");
2549                 }
2550         }
2551 exit:
2552         *buf = NULL;
2553         buf_discard(tunnel_buf);
2554         return 0;
2555 }
2556
2557 /*
2558  *  Bundler functionality:
2559  */
2560 void tipc_link_recv_bundle(struct sk_buff *buf)
2561 {
2562         u32 msgcount = msg_msgcnt(buf_msg(buf));
2563         u32 pos = INT_H_SIZE;
2564         struct sk_buff *obuf;
2565
2566         msg_dbg(buf_msg(buf), "<BNDL<: ");
2567         while (msgcount--) {
2568                 obuf = buf_extract(buf, pos);
2569                 if (obuf == NULL) {
2570                         warn("Link unable to unbundle message(s)\n");
2571                         break;
2572                 };
2573                 pos += align(msg_size(buf_msg(obuf)));
2574                 msg_dbg(buf_msg(obuf), "     /");
2575                 tipc_net_route_msg(obuf);
2576         }
2577         buf_discard(buf);
2578 }
2579
2580 /*
2581  *  Fragmentation/defragmentation:
2582  */
2583
2584
2585 /* 
2586  * tipc_link_send_long_buf: Entry for buffers needing fragmentation.
2587  * The buffer is complete, inclusive total message length. 
2588  * Returns user data length.
2589  */
2590 int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
2591 {
2592         struct tipc_msg *inmsg = buf_msg(buf);
2593         struct tipc_msg fragm_hdr;
2594         u32 insize = msg_size(inmsg);
2595         u32 dsz = msg_data_sz(inmsg);
2596         unchar *crs = buf->data;
2597         u32 rest = insize;
2598         u32 pack_sz = link_max_pkt(l_ptr);
2599         u32 fragm_sz = pack_sz - INT_H_SIZE;
2600         u32 fragm_no = 1;
2601         u32 destaddr = msg_destnode(inmsg);
2602
2603         if (msg_short(inmsg))
2604                 destaddr = l_ptr->addr;
2605
2606         if (msg_routed(inmsg))
2607                 msg_set_prevnode(inmsg, tipc_own_addr);
2608
2609         /* Prepare reusable fragment header: */
2610
2611         msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
2612                  TIPC_OK, INT_H_SIZE, destaddr);
2613         msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2614         msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2615         msg_set_fragm_no(&fragm_hdr, fragm_no);
2616         l_ptr->stats.sent_fragmented++;
2617
2618         /* Chop up message: */
2619
2620         while (rest > 0) {
2621                 struct sk_buff *fragm;
2622
2623                 if (rest <= fragm_sz) {
2624                         fragm_sz = rest;
2625                         msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2626                 }
2627                 fragm = buf_acquire(fragm_sz + INT_H_SIZE);
2628                 if (fragm == NULL) {
2629                         warn("Link unable to fragment message\n");
2630                         dsz = -ENOMEM;
2631                         goto exit;
2632                 }
2633                 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
2634                 memcpy(fragm->data, (unchar *)&fragm_hdr, INT_H_SIZE);
2635                 memcpy(fragm->data + INT_H_SIZE, crs, fragm_sz);
2636
2637                 /*  Send queued messages first, if any: */
2638
2639                 l_ptr->stats.sent_fragments++;
2640                 tipc_link_send_buf(l_ptr, fragm);
2641                 if (!tipc_link_is_up(l_ptr))
2642                         return dsz;
2643                 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2644                 rest -= fragm_sz;
2645                 crs += fragm_sz;
2646                 msg_set_type(&fragm_hdr, FRAGMENT);
2647         }
2648 exit:
2649         buf_discard(buf);
2650         return dsz;
2651 }
2652
2653 /* 
2654  * A pending message being re-assembled must store certain values 
2655  * to handle subsequent fragments correctly. The following functions 
2656  * help storing these values in unused, available fields in the
2657  * pending message. This makes dynamic memory allocation unecessary.
2658  */
2659
2660 static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
2661 {
2662         msg_set_seqno(buf_msg(buf), seqno);
2663 }
2664
2665 static u32 get_fragm_size(struct sk_buff *buf)
2666 {
2667         return msg_ack(buf_msg(buf));
2668 }
2669
2670 static void set_fragm_size(struct sk_buff *buf, u32 sz)
2671 {
2672         msg_set_ack(buf_msg(buf), sz);
2673 }
2674
2675 static u32 get_expected_frags(struct sk_buff *buf)
2676 {
2677         return msg_bcast_ack(buf_msg(buf));
2678 }
2679
2680 static void set_expected_frags(struct sk_buff *buf, u32 exp)
2681 {
2682         msg_set_bcast_ack(buf_msg(buf), exp);
2683 }
2684
2685 static u32 get_timer_cnt(struct sk_buff *buf)
2686 {
2687         return msg_reroute_cnt(buf_msg(buf));
2688 }
2689
2690 static void incr_timer_cnt(struct sk_buff *buf)
2691 {
2692         msg_incr_reroute_cnt(buf_msg(buf));
2693 }
2694
2695 /* 
2696  * tipc_link_recv_fragment(): Called with node lock on. Returns 
2697  * the reassembled buffer if message is complete.
2698  */
2699 int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb, 
2700                             struct tipc_msg **m)
2701 {
2702         struct sk_buff *prev = NULL;
2703         struct sk_buff *fbuf = *fb;
2704         struct tipc_msg *fragm = buf_msg(fbuf);
2705         struct sk_buff *pbuf = *pending;
2706         u32 long_msg_seq_no = msg_long_msgno(fragm);
2707
2708         *fb = NULL;
2709         msg_dbg(fragm,"FRG<REC<");
2710
2711         /* Is there an incomplete message waiting for this fragment? */
2712
2713         while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no)
2714                         || (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
2715                 prev = pbuf;
2716                 pbuf = pbuf->next;
2717         }
2718
2719         if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2720                 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2721                 u32 msg_sz = msg_size(imsg);
2722                 u32 fragm_sz = msg_data_sz(fragm);
2723                 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2724                 u32 max =  TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2725                 if (msg_type(imsg) == TIPC_MCAST_MSG)
2726                         max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2727                 if (msg_size(imsg) > max) {
2728                         msg_dbg(fragm,"<REC<Oversized: ");
2729                         buf_discard(fbuf);
2730                         return 0;
2731                 }
2732                 pbuf = buf_acquire(msg_size(imsg));
2733                 if (pbuf != NULL) {
2734                         pbuf->next = *pending;
2735                         *pending = pbuf;
2736                         memcpy(pbuf->data, (unchar *)imsg, msg_data_sz(fragm));
2737
2738                         /*  Prepare buffer for subsequent fragments. */
2739
2740                         set_long_msg_seqno(pbuf, long_msg_seq_no); 
2741                         set_fragm_size(pbuf,fragm_sz); 
2742                         set_expected_frags(pbuf,exp_fragm_cnt - 1); 
2743                 } else {
2744                         warn("Link unable to reassemble fragmented message\n");
2745                 }
2746                 buf_discard(fbuf);
2747                 return 0;
2748         } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2749                 u32 dsz = msg_data_sz(fragm);
2750                 u32 fsz = get_fragm_size(pbuf);
2751                 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2752                 u32 exp_frags = get_expected_frags(pbuf) - 1;
2753                 memcpy(pbuf->data + crs, msg_data(fragm), dsz);
2754                 buf_discard(fbuf);
2755
2756                 /* Is message complete? */
2757
2758                 if (exp_frags == 0) {
2759                         if (prev)
2760                                 prev->next = pbuf->next;
2761                         else
2762                                 *pending = pbuf->next;
2763                         msg_reset_reroute_cnt(buf_msg(pbuf));
2764                         *fb = pbuf;
2765                         *m = buf_msg(pbuf);
2766                         return 1;
2767                 }
2768                 set_expected_frags(pbuf,exp_frags);     
2769                 return 0;
2770         }
2771         dbg(" Discarding orphan fragment %x\n",fbuf);
2772         msg_dbg(fragm,"ORPHAN:");
2773         dbg("Pending long buffers:\n");
2774         dbg_print_buf_chain(*pending);
2775         buf_discard(fbuf);
2776         return 0;
2777 }
2778
2779 /**
2780  * link_check_defragm_bufs - flush stale incoming message fragments
2781  * @l_ptr: pointer to link
2782  */
2783
2784 static void link_check_defragm_bufs(struct link *l_ptr)
2785 {
2786         struct sk_buff *prev = NULL;
2787         struct sk_buff *next = NULL;
2788         struct sk_buff *buf = l_ptr->defragm_buf;
2789
2790         if (!buf)
2791                 return;
2792         if (!link_working_working(l_ptr))
2793                 return;
2794         while (buf) {
2795                 u32 cnt = get_timer_cnt(buf);
2796
2797                 next = buf->next;
2798                 if (cnt < 4) {
2799                         incr_timer_cnt(buf);
2800                         prev = buf;
2801                 } else {
2802                         dbg(" Discarding incomplete long buffer\n");
2803                         msg_dbg(buf_msg(buf), "LONG:");
2804                         dbg_print_link(l_ptr, "curr:");
2805                         dbg("Pending long buffers:\n");
2806                         dbg_print_buf_chain(l_ptr->defragm_buf);
2807                         if (prev)
2808                                 prev->next = buf->next;
2809                         else
2810                                 l_ptr->defragm_buf = buf->next;
2811                         buf_discard(buf);
2812                 }
2813                 buf = next;
2814         }
2815 }
2816
2817
2818
2819 static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2820 {
2821         l_ptr->tolerance = tolerance;
2822         l_ptr->continuity_interval =
2823                 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2824         l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2825 }
2826
2827
2828 void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
2829 {
2830         /* Data messages from this node, inclusive FIRST_FRAGM */
2831         l_ptr->queue_limit[DATA_LOW] = window;
2832         l_ptr->queue_limit[DATA_MEDIUM] = (window / 3) * 4;
2833         l_ptr->queue_limit[DATA_HIGH] = (window / 3) * 5;
2834         l_ptr->queue_limit[DATA_CRITICAL] = (window / 3) * 6;
2835         /* Transiting data messages,inclusive FIRST_FRAGM */
2836         l_ptr->queue_limit[DATA_LOW + 4] = 300;
2837         l_ptr->queue_limit[DATA_MEDIUM + 4] = 600;
2838         l_ptr->queue_limit[DATA_HIGH + 4] = 900;
2839         l_ptr->queue_limit[DATA_CRITICAL + 4] = 1200;
2840         l_ptr->queue_limit[CONN_MANAGER] = 1200;
2841         l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
2842         l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2843         l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2844         /* FRAGMENT and LAST_FRAGMENT packets */
2845         l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2846 }
2847
2848 /**
2849  * link_find_link - locate link by name
2850  * @name - ptr to link name string
2851  * @node - ptr to area to be filled with ptr to associated node
2852  * 
2853  * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
2854  * this also prevents link deletion.
2855  * 
2856  * Returns pointer to link (or 0 if invalid link name).
2857  */
2858
2859 static struct link *link_find_link(const char *name, struct node **node)
2860 {
2861         struct link_name link_name_parts;
2862         struct bearer *b_ptr;
2863         struct link *l_ptr; 
2864
2865         if (!link_name_validate(name, &link_name_parts))
2866                 return NULL;
2867
2868         b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
2869         if (!b_ptr)
2870                 return NULL;
2871
2872         *node = tipc_node_find(link_name_parts.addr_peer); 
2873         if (!*node)
2874                 return NULL;
2875
2876         l_ptr = (*node)->links[b_ptr->identity];
2877         if (!l_ptr || strcmp(l_ptr->name, name))
2878                 return NULL;
2879
2880         return l_ptr;
2881 }
2882
2883 struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space, 
2884                                      u16 cmd)
2885 {
2886         struct tipc_link_config *args;
2887         u32 new_value;
2888         struct link *l_ptr;
2889         struct node *node;
2890         int res;
2891
2892         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
2893                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2894
2895         args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2896         new_value = ntohl(args->value);
2897
2898         if (!strcmp(args->name, tipc_bclink_name)) {
2899                 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
2900                     (tipc_bclink_set_queue_limits(new_value) == 0))
2901                         return tipc_cfg_reply_none();
2902                 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
2903                                                    " (cannot change setting on broadcast link)");
2904         }
2905
2906         read_lock_bh(&tipc_net_lock);
2907         l_ptr = link_find_link(args->name, &node); 
2908         if (!l_ptr) {
2909                 read_unlock_bh(&tipc_net_lock);
2910                 return tipc_cfg_reply_error_string("link not found");
2911         }
2912
2913         tipc_node_lock(node);
2914         res = -EINVAL;
2915         switch (cmd) {
2916         case TIPC_CMD_SET_LINK_TOL: 
2917                 if ((new_value >= TIPC_MIN_LINK_TOL) && 
2918                     (new_value <= TIPC_MAX_LINK_TOL)) {
2919                         link_set_supervision_props(l_ptr, new_value);
2920                         tipc_link_send_proto_msg(l_ptr, STATE_MSG, 
2921                                                  0, 0, new_value, 0, 0);
2922                         res = TIPC_OK;
2923                 }
2924                 break;
2925         case TIPC_CMD_SET_LINK_PRI: 
2926                 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2927                     (new_value <= TIPC_MAX_LINK_PRI)) {
2928                         l_ptr->priority = new_value;
2929                         tipc_link_send_proto_msg(l_ptr, STATE_MSG, 
2930                                                  0, 0, 0, new_value, 0);
2931                         res = TIPC_OK;
2932                 }
2933                 break;
2934         case TIPC_CMD_SET_LINK_WINDOW: 
2935                 if ((new_value >= TIPC_MIN_LINK_WIN) && 
2936                     (new_value <= TIPC_MAX_LINK_WIN)) {
2937                         tipc_link_set_queue_limits(l_ptr, new_value);
2938                         res = TIPC_OK;
2939                 }
2940                 break;
2941         }
2942         tipc_node_unlock(node);
2943
2944         read_unlock_bh(&tipc_net_lock);
2945         if (res)
2946                 return tipc_cfg_reply_error_string("cannot change link setting");
2947
2948         return tipc_cfg_reply_none();
2949 }
2950
2951 /**
2952  * link_reset_statistics - reset link statistics
2953  * @l_ptr: pointer to link
2954  */
2955
2956 static void link_reset_statistics(struct link *l_ptr)
2957 {
2958         memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2959         l_ptr->stats.sent_info = l_ptr->next_out_no;
2960         l_ptr->stats.recv_info = l_ptr->next_in_no;
2961 }
2962
2963 struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
2964 {
2965         char *link_name;
2966         struct link *l_ptr; 
2967         struct node *node;
2968
2969         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
2970                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2971
2972         link_name = (char *)TLV_DATA(req_tlv_area);
2973         if (!strcmp(link_name, tipc_bclink_name)) {
2974                 if (tipc_bclink_reset_stats())
2975                         return tipc_cfg_reply_error_string("link not found");
2976                 return tipc_cfg_reply_none();
2977         }
2978
2979         read_lock_bh(&tipc_net_lock);
2980         l_ptr = link_find_link(link_name, &node); 
2981         if (!l_ptr) {
2982                 read_unlock_bh(&tipc_net_lock);
2983                 return tipc_cfg_reply_error_string("link not found");
2984         }
2985
2986         tipc_node_lock(node);
2987         link_reset_statistics(l_ptr);
2988         tipc_node_unlock(node);
2989         read_unlock_bh(&tipc_net_lock);
2990         return tipc_cfg_reply_none();
2991 }
2992
2993 /**
2994  * percent - convert count to a percentage of total (rounding up or down)
2995  */
2996
2997 static u32 percent(u32 count, u32 total)
2998 {
2999         return (count * 100 + (total / 2)) / total;
3000 }
3001
3002 /**
3003  * tipc_link_stats - print link statistics
3004  * @name: link name
3005  * @buf: print buffer area
3006  * @buf_size: size of print buffer area
3007  * 
3008  * Returns length of print buffer data string (or 0 if error)
3009  */
3010
3011 static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
3012 {
3013         struct print_buf pb;
3014         struct link *l_ptr; 
3015         struct node *node;
3016         char *status;
3017         u32 profile_total = 0;
3018
3019         if (!strcmp(name, tipc_bclink_name))
3020                 return tipc_bclink_stats(buf, buf_size);
3021
3022         tipc_printbuf_init(&pb, buf, buf_size);
3023
3024         read_lock_bh(&tipc_net_lock);
3025         l_ptr = link_find_link(name, &node); 
3026         if (!l_ptr) {
3027                 read_unlock_bh(&tipc_net_lock);
3028                 return 0;
3029         }
3030         tipc_node_lock(node);
3031
3032         if (tipc_link_is_active(l_ptr))
3033                 status = "ACTIVE";
3034         else if (tipc_link_is_up(l_ptr))
3035                 status = "STANDBY";
3036         else
3037                 status = "DEFUNCT";
3038         tipc_printf(&pb, "Link <%s>\n"
3039                          "  %s  MTU:%u  Priority:%u  Tolerance:%u ms"
3040                          "  Window:%u packets\n", 
3041                     l_ptr->name, status, link_max_pkt(l_ptr), 
3042                     l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
3043         tipc_printf(&pb, "  RX packets:%u fragments:%u/%u bundles:%u/%u\n", 
3044                     l_ptr->next_in_no - l_ptr->stats.recv_info,
3045                     l_ptr->stats.recv_fragments,
3046                     l_ptr->stats.recv_fragmented,
3047                     l_ptr->stats.recv_bundles,
3048                     l_ptr->stats.recv_bundled);
3049         tipc_printf(&pb, "  TX packets:%u fragments:%u/%u bundles:%u/%u\n", 
3050                     l_ptr->next_out_no - l_ptr->stats.sent_info,
3051                     l_ptr->stats.sent_fragments,
3052                     l_ptr->stats.sent_fragmented, 
3053                     l_ptr->stats.sent_bundles,
3054                     l_ptr->stats.sent_bundled);
3055         profile_total = l_ptr->stats.msg_length_counts;
3056         if (!profile_total)
3057                 profile_total = 1;
3058         tipc_printf(&pb, "  TX profile sample:%u packets  average:%u octets\n"
3059                          "  0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
3060                          "-16354:%u%% -32768:%u%% -66000:%u%%\n",
3061                     l_ptr->stats.msg_length_counts,
3062                     l_ptr->stats.msg_lengths_total / profile_total,
3063                     percent(l_ptr->stats.msg_length_profile[0], profile_total),
3064                     percent(l_ptr->stats.msg_length_profile[1], profile_total),
3065                     percent(l_ptr->stats.msg_length_profile[2], profile_total),
3066                     percent(l_ptr->stats.msg_length_profile[3], profile_total),
3067                     percent(l_ptr->stats.msg_length_profile[4], profile_total),
3068                     percent(l_ptr->stats.msg_length_profile[5], profile_total),
3069                     percent(l_ptr->stats.msg_length_profile[6], profile_total));
3070         tipc_printf(&pb, "  RX states:%u probes:%u naks:%u defs:%u dups:%u\n", 
3071                     l_ptr->stats.recv_states,
3072                     l_ptr->stats.recv_probes,
3073                     l_ptr->stats.recv_nacks,
3074                     l_ptr->stats.deferred_recv, 
3075                     l_ptr->stats.duplicates);
3076         tipc_printf(&pb, "  TX states:%u probes:%u naks:%u acks:%u dups:%u\n", 
3077                     l_ptr->stats.sent_states, 
3078                     l_ptr->stats.sent_probes, 
3079                     l_ptr->stats.sent_nacks, 
3080                     l_ptr->stats.sent_acks, 
3081                     l_ptr->stats.retransmitted);
3082         tipc_printf(&pb, "  Congestion bearer:%u link:%u  Send queue max:%u avg:%u\n",
3083                     l_ptr->stats.bearer_congs,
3084                     l_ptr->stats.link_congs, 
3085                     l_ptr->stats.max_queue_sz,
3086                     l_ptr->stats.queue_sz_counts
3087                     ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
3088                     : 0);
3089
3090         tipc_node_unlock(node);
3091         read_unlock_bh(&tipc_net_lock);
3092         return tipc_printbuf_validate(&pb);
3093 }
3094
3095 #define MAX_LINK_STATS_INFO 2000
3096
3097 struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
3098 {
3099         struct sk_buff *buf;
3100         struct tlv_desc *rep_tlv;
3101         int str_len;
3102
3103         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
3104                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
3105
3106         buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
3107         if (!buf)
3108                 return NULL;
3109
3110         rep_tlv = (struct tlv_desc *)buf->data;
3111
3112         str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
3113                                   (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
3114         if (!str_len) {
3115                 buf_discard(buf);
3116                 return tipc_cfg_reply_error_string("link not found");
3117         }
3118
3119         skb_put(buf, TLV_SPACE(str_len));
3120         TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
3121
3122         return buf;
3123 }
3124
3125 #if 0
3126 int link_control(const char *name, u32 op, u32 val)
3127 {
3128         int res = -EINVAL;
3129         struct link *l_ptr;
3130         u32 bearer_id;
3131         struct node * node;
3132         u32 a;
3133
3134         a = link_name2addr(name, &bearer_id);
3135         read_lock_bh(&tipc_net_lock);
3136         node = tipc_node_find(a);
3137         if (node) {
3138                 tipc_node_lock(node);
3139                 l_ptr = node->links[bearer_id];
3140                 if (l_ptr) {
3141                         if (op == TIPC_REMOVE_LINK) {
3142                                 struct bearer *b_ptr = l_ptr->b_ptr;
3143                                 spin_lock_bh(&b_ptr->publ.lock);
3144                                 tipc_link_delete(l_ptr);
3145                                 spin_unlock_bh(&b_ptr->publ.lock);
3146                         }
3147                         if (op == TIPC_CMD_BLOCK_LINK) {
3148                                 tipc_link_reset(l_ptr);
3149                                 l_ptr->blocked = 1;
3150                         }
3151                         if (op == TIPC_CMD_UNBLOCK_LINK) {
3152                                 l_ptr->blocked = 0;
3153                         }
3154                         res = TIPC_OK;
3155                 }
3156                 tipc_node_unlock(node);
3157         }
3158         read_unlock_bh(&tipc_net_lock);
3159         return res;
3160 }
3161 #endif
3162
3163 /**
3164  * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
3165  * @dest: network address of destination node
3166  * @selector: used to select from set of active links
3167  * 
3168  * If no active link can be found, uses default maximum packet size.
3169  */
3170
3171 u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
3172 {
3173         struct node *n_ptr;
3174         struct link *l_ptr;
3175         u32 res = MAX_PKT_DEFAULT;
3176         
3177         if (dest == tipc_own_addr)
3178                 return MAX_MSG_SIZE;
3179
3180         read_lock_bh(&tipc_net_lock);        
3181         n_ptr = tipc_node_select(dest, selector);
3182         if (n_ptr) {
3183                 tipc_node_lock(n_ptr);
3184                 l_ptr = n_ptr->active_links[selector & 1];
3185                 if (l_ptr)
3186                         res = link_max_pkt(l_ptr);
3187                 tipc_node_unlock(n_ptr);
3188         }
3189         read_unlock_bh(&tipc_net_lock);       
3190         return res;
3191 }
3192
3193 #if 0
3194 static void link_dump_rec_queue(struct link *l_ptr)
3195 {
3196         struct sk_buff *crs;
3197
3198         if (!l_ptr->oldest_deferred_in) {
3199                 info("Reception queue empty\n");
3200                 return;
3201         }
3202         info("Contents of Reception queue:\n");
3203         crs = l_ptr->oldest_deferred_in;
3204         while (crs) {
3205                 if (crs->data == (void *)0x0000a3a3) {
3206                         info("buffer %x invalid\n", crs);
3207                         return;
3208                 }
3209                 msg_dbg(buf_msg(crs), "In rec queue: \n");
3210                 crs = crs->next;
3211         }
3212 }
3213 #endif
3214
3215 static void link_dump_send_queue(struct link *l_ptr)
3216 {
3217         if (l_ptr->next_out) {
3218                 info("\nContents of unsent queue:\n");
3219                 dbg_print_buf_chain(l_ptr->next_out);
3220         }
3221         info("\nContents of send queue:\n");
3222         if (l_ptr->first_out) {
3223                 dbg_print_buf_chain(l_ptr->first_out);
3224         }
3225         info("Empty send queue\n");
3226 }
3227
3228 static void link_print(struct link *l_ptr, struct print_buf *buf,
3229                        const char *str)
3230 {
3231         tipc_printf(buf, str);
3232         if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
3233                 return;
3234         tipc_printf(buf, "Link %x<%s>:",
3235                     l_ptr->addr, l_ptr->b_ptr->publ.name);
3236         tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
3237         tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
3238         tipc_printf(buf, "SQUE");
3239         if (l_ptr->first_out) {
3240                 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
3241                 if (l_ptr->next_out)
3242                         tipc_printf(buf, "%u..",
3243                                     msg_seqno(buf_msg(l_ptr->next_out)));
3244                 tipc_printf(buf, "%u]",
3245                             msg_seqno(buf_msg
3246                                       (l_ptr->last_out)), l_ptr->out_queue_size);
3247                 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) - 
3248                          msg_seqno(buf_msg(l_ptr->first_out))) 
3249                      != (l_ptr->out_queue_size - 1))
3250                     || (l_ptr->last_out->next != 0)) {
3251                         tipc_printf(buf, "\nSend queue inconsistency\n");
3252                         tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
3253                         tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
3254                         tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
3255                         link_dump_send_queue(l_ptr);
3256                 }
3257         } else
3258                 tipc_printf(buf, "[]");
3259         tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
3260         if (l_ptr->oldest_deferred_in) {
3261                 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
3262                 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
3263                 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
3264                 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
3265                         tipc_printf(buf, ":RQSIZ(%u)",
3266                                     l_ptr->deferred_inqueue_sz);
3267                 }
3268         }
3269         if (link_working_unknown(l_ptr))
3270                 tipc_printf(buf, ":WU");
3271         if (link_reset_reset(l_ptr))
3272                 tipc_printf(buf, ":RR");
3273         if (link_reset_unknown(l_ptr))
3274                 tipc_printf(buf, ":RU");
3275         if (link_working_working(l_ptr))
3276                 tipc_printf(buf, ":WW");
3277         tipc_printf(buf, "\n");
3278 }
3279