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