2 * net/tipc/link.c: TIPC link code
4 * Copyright (c) 1996-2007, 2012-2016, Ericsson AB
5 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
42 #include "name_distr.h"
48 #include <linux/pkt_sched.h>
69 u32 link_congs; /* # port sends blocked by congestion */
72 u32 max_queue_sz; /* send queue size high water mark */
73 u32 accu_queue_sz; /* used for send queue size profiling */
74 u32 queue_sz_counts; /* used for send queue size profiling */
75 u32 msg_length_counts; /* used for message length profiling */
76 u32 msg_lengths_total; /* used for message length profiling */
77 u32 msg_length_profile[7]; /* used for msg. length profiling */
81 * struct tipc_link - TIPC link data structure
82 * @addr: network address of link's peer node
83 * @name: link name character string
84 * @media_addr: media address to use when sending messages over link
86 * @net: pointer to namespace struct
87 * @refcnt: reference counter for permanent references (owner node & timer)
88 * @peer_session: link session # being used by peer end of link
89 * @peer_bearer_id: bearer id used by link's peer endpoint
90 * @bearer_id: local bearer id used by link
91 * @tolerance: minimum link continuity loss needed to reset link [in ms]
92 * @abort_limit: # of unacknowledged continuity probes needed to reset link
93 * @state: current state of link FSM
94 * @peer_caps: bitmap describing capabilities of peer node
95 * @silent_intv_cnt: # of timer intervals without any reception from peer
96 * @proto_msg: template for control messages generated by link
97 * @pmsg: convenience pointer to "proto_msg" field
98 * @priority: current link priority
99 * @net_plane: current link network plane ('A' through 'H')
100 * @mon_state: cookie with information needed by link monitor
101 * @backlog_limit: backlog queue congestion thresholds (indexed by importance)
102 * @exp_msg_count: # of tunnelled messages expected during link changeover
103 * @reset_rcv_checkpt: seq # of last acknowledged message at time of link reset
104 * @mtu: current maximum packet size for this link
105 * @advertised_mtu: advertised own mtu when link is being established
106 * @transmitq: queue for sent, non-acked messages
107 * @backlogq: queue for messages waiting to be sent
108 * @snt_nxt: next sequence number to use for outbound messages
109 * @ackers: # of peers that needs to ack each packet before it can be released
110 * @acked: # last packet acked by a certain peer. Used for broadcast.
111 * @rcv_nxt: next sequence number to expect for inbound messages
112 * @deferred_queue: deferred queue saved OOS b'cast message received from node
113 * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
114 * @inputq: buffer queue for messages to be delivered upwards
115 * @namedq: buffer queue for name table messages to be delivered upwards
116 * @next_out: ptr to first unsent outbound message in queue
117 * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
118 * @long_msg_seq_no: next identifier to use for outbound fragmented messages
119 * @reasm_buf: head of partially reassembled inbound message fragments
120 * @bc_rcvr: marks that this is a broadcast receiver link
121 * @stats: collects statistics regarding link activity
125 char name[TIPC_MAX_LINK_NAME];
128 /* Management and link supervision data */
142 char if_name[TIPC_MAX_IF_NAME];
145 struct tipc_mon_state mon_state;
150 struct sk_buff *failover_reasm_skb;
151 struct sk_buff_head failover_deferdq;
153 /* Max packet negotiation */
158 struct sk_buff_head transmq;
159 struct sk_buff_head backlogq;
170 struct sk_buff_head deferdq;
171 struct sk_buff_head *inputq;
172 struct sk_buff_head *namedq;
174 /* Congestion handling */
175 struct sk_buff_head wakeupq;
177 /* Fragmentation/reassembly */
178 struct sk_buff *reasm_buf;
179 struct sk_buff *reasm_tnlmsg;
184 struct tipc_link *bc_rcvlink;
185 struct tipc_link *bc_sndlink;
190 struct tipc_stats stats;
194 * Error message prefixes
196 static const char *link_co_err = "Link tunneling error, ";
197 static const char *link_rst_msg = "Resetting link ";
199 /* Send states for broadcast NACKs
202 BC_NACK_SND_CONDITIONAL,
203 BC_NACK_SND_UNCONDITIONAL,
204 BC_NACK_SND_SUPPRESS,
207 #define TIPC_BC_RETR_LIM (jiffies + msecs_to_jiffies(10))
208 #define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
211 * Interval between NACKs when packets arrive out of order
213 #define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
218 LINK_ESTABLISHED = 0xe,
219 LINK_ESTABLISHING = 0xe << 4,
220 LINK_RESET = 0x1 << 8,
221 LINK_RESETTING = 0x2 << 12,
222 LINK_PEER_RESET = 0xd << 16,
223 LINK_FAILINGOVER = 0xf << 20,
224 LINK_SYNCHING = 0xc << 24
227 /* Link FSM state checking routines
229 static int link_is_up(struct tipc_link *l)
231 return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
234 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
235 struct sk_buff_head *xmitq);
236 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
237 bool probe_reply, u16 rcvgap,
238 int tolerance, int priority,
239 struct sk_buff_head *xmitq);
240 static void link_print(struct tipc_link *l, const char *str);
241 static int tipc_link_build_nack_msg(struct tipc_link *l,
242 struct sk_buff_head *xmitq);
243 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
244 struct sk_buff_head *xmitq);
245 static bool tipc_link_release_pkts(struct tipc_link *l, u16 to);
246 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data);
247 static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
248 struct tipc_gap_ack_blks *ga,
249 struct sk_buff_head *xmitq);
252 * Simple non-static link routines (i.e. referenced outside this file)
254 bool tipc_link_is_up(struct tipc_link *l)
256 return link_is_up(l);
259 bool tipc_link_peer_is_down(struct tipc_link *l)
261 return l->state == LINK_PEER_RESET;
264 bool tipc_link_is_reset(struct tipc_link *l)
266 return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
269 bool tipc_link_is_establishing(struct tipc_link *l)
271 return l->state == LINK_ESTABLISHING;
274 bool tipc_link_is_synching(struct tipc_link *l)
276 return l->state == LINK_SYNCHING;
279 bool tipc_link_is_failingover(struct tipc_link *l)
281 return l->state == LINK_FAILINGOVER;
284 bool tipc_link_is_blocked(struct tipc_link *l)
286 return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
289 static bool link_is_bc_sndlink(struct tipc_link *l)
291 return !l->bc_sndlink;
294 static bool link_is_bc_rcvlink(struct tipc_link *l)
296 return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
299 void tipc_link_set_active(struct tipc_link *l, bool active)
304 u32 tipc_link_id(struct tipc_link *l)
306 return l->peer_bearer_id << 16 | l->bearer_id;
309 int tipc_link_window(struct tipc_link *l)
314 int tipc_link_prio(struct tipc_link *l)
319 unsigned long tipc_link_tolerance(struct tipc_link *l)
324 struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
329 char tipc_link_plane(struct tipc_link *l)
334 void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
336 l->peer_caps = capabilities;
339 void tipc_link_add_bc_peer(struct tipc_link *snd_l,
340 struct tipc_link *uc_l,
341 struct sk_buff_head *xmitq)
343 struct tipc_link *rcv_l = uc_l->bc_rcvlink;
346 rcv_l->acked = snd_l->snd_nxt - 1;
347 snd_l->state = LINK_ESTABLISHED;
348 tipc_link_build_bc_init_msg(uc_l, xmitq);
351 void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
352 struct tipc_link *rcv_l,
353 struct sk_buff_head *xmitq)
355 u16 ack = snd_l->snd_nxt - 1;
358 rcv_l->bc_peer_is_up = true;
359 rcv_l->state = LINK_ESTABLISHED;
360 tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
361 trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
362 tipc_link_reset(rcv_l);
363 rcv_l->state = LINK_RESET;
364 if (!snd_l->ackers) {
365 trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
366 tipc_link_reset(snd_l);
367 snd_l->state = LINK_RESET;
368 __skb_queue_purge(xmitq);
372 int tipc_link_bc_peers(struct tipc_link *l)
377 static u16 link_bc_rcv_gap(struct tipc_link *l)
379 struct sk_buff *skb = skb_peek(&l->deferdq);
382 if (more(l->snd_nxt, l->rcv_nxt))
383 gap = l->snd_nxt - l->rcv_nxt;
385 gap = buf_seqno(skb) - l->rcv_nxt;
389 void tipc_link_set_mtu(struct tipc_link *l, int mtu)
394 int tipc_link_mtu(struct tipc_link *l)
399 u16 tipc_link_rcv_nxt(struct tipc_link *l)
404 u16 tipc_link_acked(struct tipc_link *l)
409 char *tipc_link_name(struct tipc_link *l)
414 u32 tipc_link_state(struct tipc_link *l)
420 * tipc_link_create - create a new link
421 * @n: pointer to associated node
422 * @if_name: associated interface name
423 * @bearer_id: id (index) of associated bearer
424 * @tolerance: link tolerance to be used by link
425 * @net_plane: network plane (A,B,c..) this link belongs to
426 * @mtu: mtu to be advertised by link
427 * @priority: priority to be used by link
428 * @window: send window to be used by link
429 * @session: session to be used by link
430 * @ownnode: identity of own node
431 * @peer: node id of peer node
432 * @peer_caps: bitmap describing peer node capabilities
433 * @bc_sndlink: the namespace global link used for broadcast sending
434 * @bc_rcvlink: the peer specific link used for broadcast reception
435 * @inputq: queue to put messages ready for delivery
436 * @namedq: queue to put binding table update messages ready for delivery
437 * @link: return value, pointer to put the created link
439 * Returns true if link was created, otherwise false
441 bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
442 int tolerance, char net_plane, u32 mtu, int priority,
443 int window, u32 session, u32 self,
444 u32 peer, u8 *peer_id, u16 peer_caps,
445 struct tipc_link *bc_sndlink,
446 struct tipc_link *bc_rcvlink,
447 struct sk_buff_head *inputq,
448 struct sk_buff_head *namedq,
449 struct tipc_link **link)
451 char peer_str[NODE_ID_STR_LEN] = {0,};
452 char self_str[NODE_ID_STR_LEN] = {0,};
455 l = kzalloc(sizeof(*l), GFP_ATOMIC);
459 l->session = session;
461 /* Set link name for unicast links only */
463 tipc_nodeid2string(self_str, tipc_own_id(net));
464 if (strlen(self_str) > 16)
465 sprintf(self_str, "%x", self);
466 tipc_nodeid2string(peer_str, peer_id);
467 if (strlen(peer_str) > 16)
468 sprintf(peer_str, "%x", peer);
470 /* Peer i/f name will be completed by reset/activate message */
471 snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
472 self_str, if_name, peer_str);
474 strcpy(l->if_name, if_name);
476 l->peer_caps = peer_caps;
478 l->in_session = false;
479 l->bearer_id = bearer_id;
480 l->tolerance = tolerance;
482 bc_rcvlink->tolerance = tolerance;
483 l->net_plane = net_plane;
484 l->advertised_mtu = mtu;
486 l->priority = priority;
487 tipc_link_set_queue_limits(l, window);
489 l->bc_sndlink = bc_sndlink;
490 l->bc_rcvlink = bc_rcvlink;
493 l->state = LINK_RESETTING;
494 __skb_queue_head_init(&l->transmq);
495 __skb_queue_head_init(&l->backlogq);
496 __skb_queue_head_init(&l->deferdq);
497 __skb_queue_head_init(&l->failover_deferdq);
498 skb_queue_head_init(&l->wakeupq);
499 skb_queue_head_init(l->inputq);
504 * tipc_link_bc_create - create new link to be used for broadcast
505 * @n: pointer to associated node
506 * @mtu: mtu to be used initially if no peers
507 * @window: send window to be used
508 * @inputq: queue to put messages ready for delivery
509 * @namedq: queue to put binding table update messages ready for delivery
510 * @link: return value, pointer to put the created link
512 * Returns true if link was created, otherwise false
514 bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
515 int mtu, int window, u16 peer_caps,
516 struct sk_buff_head *inputq,
517 struct sk_buff_head *namedq,
518 struct tipc_link *bc_sndlink,
519 struct tipc_link **link)
523 if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
524 0, ownnode, peer, NULL, peer_caps, bc_sndlink,
525 NULL, inputq, namedq, link))
529 strcpy(l->name, tipc_bclink_name);
530 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
532 l->state = LINK_RESET;
536 /* Broadcast send link is always up */
537 if (link_is_bc_sndlink(l))
538 l->state = LINK_ESTABLISHED;
540 /* Disable replicast if even a single peer doesn't support it */
541 if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
542 tipc_bcast_disable_rcast(net);
548 * tipc_link_fsm_evt - link finite state machine
549 * @l: pointer to link
550 * @evt: state machine event to be processed
552 int tipc_link_fsm_evt(struct tipc_link *l, int evt)
555 int old_state = l->state;
560 case LINK_PEER_RESET_EVT:
561 l->state = LINK_PEER_RESET;
564 l->state = LINK_RESET;
566 case LINK_FAILURE_EVT:
567 case LINK_FAILOVER_BEGIN_EVT:
568 case LINK_ESTABLISH_EVT:
569 case LINK_FAILOVER_END_EVT:
570 case LINK_SYNCH_BEGIN_EVT:
571 case LINK_SYNCH_END_EVT:
578 case LINK_PEER_RESET_EVT:
579 l->state = LINK_ESTABLISHING;
581 case LINK_FAILOVER_BEGIN_EVT:
582 l->state = LINK_FAILINGOVER;
583 case LINK_FAILURE_EVT:
585 case LINK_ESTABLISH_EVT:
586 case LINK_FAILOVER_END_EVT:
588 case LINK_SYNCH_BEGIN_EVT:
589 case LINK_SYNCH_END_EVT:
594 case LINK_PEER_RESET:
597 l->state = LINK_ESTABLISHING;
599 case LINK_PEER_RESET_EVT:
600 case LINK_ESTABLISH_EVT:
601 case LINK_FAILURE_EVT:
603 case LINK_SYNCH_BEGIN_EVT:
604 case LINK_SYNCH_END_EVT:
605 case LINK_FAILOVER_BEGIN_EVT:
606 case LINK_FAILOVER_END_EVT:
611 case LINK_FAILINGOVER:
613 case LINK_FAILOVER_END_EVT:
614 l->state = LINK_RESET;
616 case LINK_PEER_RESET_EVT:
618 case LINK_ESTABLISH_EVT:
619 case LINK_FAILURE_EVT:
621 case LINK_FAILOVER_BEGIN_EVT:
622 case LINK_SYNCH_BEGIN_EVT:
623 case LINK_SYNCH_END_EVT:
628 case LINK_ESTABLISHING:
630 case LINK_ESTABLISH_EVT:
631 l->state = LINK_ESTABLISHED;
633 case LINK_FAILOVER_BEGIN_EVT:
634 l->state = LINK_FAILINGOVER;
637 l->state = LINK_RESET;
639 case LINK_FAILURE_EVT:
640 case LINK_PEER_RESET_EVT:
641 case LINK_SYNCH_BEGIN_EVT:
642 case LINK_FAILOVER_END_EVT:
644 case LINK_SYNCH_END_EVT:
649 case LINK_ESTABLISHED:
651 case LINK_PEER_RESET_EVT:
652 l->state = LINK_PEER_RESET;
653 rc |= TIPC_LINK_DOWN_EVT;
655 case LINK_FAILURE_EVT:
656 l->state = LINK_RESETTING;
657 rc |= TIPC_LINK_DOWN_EVT;
660 l->state = LINK_RESET;
662 case LINK_ESTABLISH_EVT:
663 case LINK_SYNCH_END_EVT:
665 case LINK_SYNCH_BEGIN_EVT:
666 l->state = LINK_SYNCHING;
668 case LINK_FAILOVER_BEGIN_EVT:
669 case LINK_FAILOVER_END_EVT:
676 case LINK_PEER_RESET_EVT:
677 l->state = LINK_PEER_RESET;
678 rc |= TIPC_LINK_DOWN_EVT;
680 case LINK_FAILURE_EVT:
681 l->state = LINK_RESETTING;
682 rc |= TIPC_LINK_DOWN_EVT;
685 l->state = LINK_RESET;
687 case LINK_ESTABLISH_EVT:
688 case LINK_SYNCH_BEGIN_EVT:
690 case LINK_SYNCH_END_EVT:
691 l->state = LINK_ESTABLISHED;
693 case LINK_FAILOVER_BEGIN_EVT:
694 case LINK_FAILOVER_END_EVT:
700 pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
702 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
705 pr_err("Illegal FSM event %x in state %x on link %s\n",
706 evt, l->state, l->name);
707 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
711 /* link_profile_stats - update statistical profiling of traffic
713 static void link_profile_stats(struct tipc_link *l)
716 struct tipc_msg *msg;
719 /* Update counters used in statistical profiling of send traffic */
720 l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
721 l->stats.queue_sz_counts++;
723 skb = skb_peek(&l->transmq);
727 length = msg_size(msg);
729 if (msg_user(msg) == MSG_FRAGMENTER) {
730 if (msg_type(msg) != FIRST_FRAGMENT)
732 length = msg_size(msg_inner_hdr(msg));
734 l->stats.msg_lengths_total += length;
735 l->stats.msg_length_counts++;
737 l->stats.msg_length_profile[0]++;
738 else if (length <= 256)
739 l->stats.msg_length_profile[1]++;
740 else if (length <= 1024)
741 l->stats.msg_length_profile[2]++;
742 else if (length <= 4096)
743 l->stats.msg_length_profile[3]++;
744 else if (length <= 16384)
745 l->stats.msg_length_profile[4]++;
746 else if (length <= 32768)
747 l->stats.msg_length_profile[5]++;
749 l->stats.msg_length_profile[6]++;
753 * tipc_link_too_silent - check if link is "too silent"
754 * @l: tipc link to be checked
756 * Returns true if the link 'silent_intv_cnt' is about to reach the
757 * 'abort_limit' value, otherwise false
759 bool tipc_link_too_silent(struct tipc_link *l)
761 return (l->silent_intv_cnt + 2 > l->abort_limit);
764 /* tipc_link_timeout - perform periodic task as instructed from node timeout
766 int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
773 u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
774 u16 bc_acked = l->bc_rcvlink->acked;
775 struct tipc_mon_state *mstate = &l->mon_state;
777 trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
778 trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
780 case LINK_ESTABLISHED:
783 link_profile_stats(l);
784 tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
785 if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
786 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
787 state = bc_acked != bc_snt;
788 state |= l->bc_rcvlink->rcv_unacked;
789 state |= l->rcv_unacked;
790 state |= !skb_queue_empty(&l->transmq);
791 state |= !skb_queue_empty(&l->deferdq);
792 probe = mstate->probing;
793 probe |= l->silent_intv_cnt;
794 if (probe || mstate->monitoring)
795 l->silent_intv_cnt++;
798 setup = l->rst_cnt++ <= 4;
799 setup |= !(l->rst_cnt % 16);
802 case LINK_ESTABLISHING:
806 case LINK_PEER_RESET:
808 case LINK_FAILINGOVER:
814 if (state || probe || setup)
815 tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
821 * link_schedule_user - schedule a message sender for wakeup after congestion
823 * @hdr: header of message that is being sent
824 * Create pseudo msg to send back to user when congestion abates
826 static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
828 u32 dnode = tipc_own_addr(l->net);
829 u32 dport = msg_origport(hdr);
832 /* Create and schedule wakeup pseudo message */
833 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
834 dnode, l->addr, dport, 0, 0);
837 msg_set_dest_droppable(buf_msg(skb), true);
838 TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
839 skb_queue_tail(&l->wakeupq, skb);
840 l->stats.link_congs++;
841 trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
846 * link_prepare_wakeup - prepare users for wakeup after congestion
848 * Wake up a number of waiting users, as permitted by available space
851 static void link_prepare_wakeup(struct tipc_link *l)
853 struct sk_buff_head *wakeupq = &l->wakeupq;
854 struct sk_buff_head *inputq = l->inputq;
855 struct sk_buff *skb, *tmp;
856 struct sk_buff_head tmpq;
860 __skb_queue_head_init(&tmpq);
862 for (; imp <= TIPC_SYSTEM_IMPORTANCE; imp++)
863 avail[imp] = l->backlog[imp].limit - l->backlog[imp].len;
865 skb_queue_walk_safe(wakeupq, skb, tmp) {
866 imp = TIPC_SKB_CB(skb)->chain_imp;
870 __skb_unlink(skb, wakeupq);
871 __skb_queue_tail(&tmpq, skb);
874 spin_lock_bh(&inputq->lock);
875 skb_queue_splice_tail(&tmpq, inputq);
876 spin_unlock_bh(&inputq->lock);
880 void tipc_link_reset(struct tipc_link *l)
882 struct sk_buff_head list;
884 __skb_queue_head_init(&list);
886 l->in_session = false;
887 /* Force re-synch of peer session number before establishing */
890 l->mtu = l->advertised_mtu;
892 spin_lock_bh(&l->wakeupq.lock);
893 skb_queue_splice_init(&l->wakeupq, &list);
894 spin_unlock_bh(&l->wakeupq.lock);
896 spin_lock_bh(&l->inputq->lock);
897 skb_queue_splice_init(&list, l->inputq);
898 spin_unlock_bh(&l->inputq->lock);
900 __skb_queue_purge(&l->transmq);
901 __skb_queue_purge(&l->deferdq);
902 __skb_queue_purge(&l->backlogq);
903 __skb_queue_purge(&l->failover_deferdq);
904 l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
905 l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
906 l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
907 l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
908 l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
909 kfree_skb(l->reasm_buf);
910 kfree_skb(l->reasm_tnlmsg);
911 kfree_skb(l->failover_reasm_skb);
913 l->reasm_tnlmsg = NULL;
914 l->failover_reasm_skb = NULL;
918 l->snd_nxt_state = 1;
919 l->rcv_nxt_state = 1;
921 l->silent_intv_cnt = 0;
923 l->bc_peer_is_up = false;
924 memset(&l->mon_state, 0, sizeof(l->mon_state));
925 tipc_link_reset_stats(l);
929 * tipc_link_xmit(): enqueue buffer list according to queue situation
931 * @list: chain of buffers containing message
932 * @xmitq: returned list of packets to be sent by caller
934 * Consumes the buffer chain.
935 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
936 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
938 int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
939 struct sk_buff_head *xmitq)
941 struct tipc_msg *hdr = buf_msg(skb_peek(list));
942 unsigned int maxwin = l->window;
943 int imp = msg_importance(hdr);
944 unsigned int mtu = l->mtu;
945 u16 ack = l->rcv_nxt - 1;
946 u16 seqno = l->snd_nxt;
947 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
948 struct sk_buff_head *transmq = &l->transmq;
949 struct sk_buff_head *backlogq = &l->backlogq;
950 struct sk_buff *skb, *_skb, *bskb;
951 int pkt_cnt = skb_queue_len(list);
954 if (unlikely(msg_size(hdr) > mtu)) {
955 pr_warn("Too large msg, purging xmit list %d %d %d %d %d!\n",
956 skb_queue_len(list), msg_user(hdr),
957 msg_type(hdr), msg_size(hdr), mtu);
958 __skb_queue_purge(list);
962 /* Allow oversubscription of one data msg per source at congestion */
963 if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
964 if (imp == TIPC_SYSTEM_IMPORTANCE) {
965 pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
968 rc = link_schedule_user(l, hdr);
972 l->stats.sent_fragmented++;
973 l->stats.sent_fragments += pkt_cnt;
976 /* Prepare each packet for sending, and add to relevant queue: */
977 while (skb_queue_len(list)) {
978 skb = skb_peek(list);
980 msg_set_seqno(hdr, seqno);
981 msg_set_ack(hdr, ack);
982 msg_set_bcast_ack(hdr, bc_ack);
984 if (likely(skb_queue_len(transmq) < maxwin)) {
985 _skb = skb_clone(skb, GFP_ATOMIC);
987 __skb_queue_purge(list);
991 __skb_queue_tail(transmq, skb);
992 /* next retransmit attempt */
993 if (link_is_bc_sndlink(l))
994 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
995 __skb_queue_tail(xmitq, _skb);
996 TIPC_SKB_CB(skb)->ackers = l->ackers;
998 l->stats.sent_pkts++;
1002 if (tipc_msg_bundle(skb_peek_tail(backlogq), hdr, mtu)) {
1003 kfree_skb(__skb_dequeue(list));
1004 l->stats.sent_bundled++;
1007 if (tipc_msg_make_bundle(&bskb, hdr, mtu, l->addr)) {
1008 kfree_skb(__skb_dequeue(list));
1009 __skb_queue_tail(backlogq, bskb);
1010 l->backlog[msg_importance(buf_msg(bskb))].len++;
1011 l->stats.sent_bundled++;
1012 l->stats.sent_bundles++;
1015 l->backlog[imp].len += skb_queue_len(list);
1016 skb_queue_splice_tail_init(list, backlogq);
1022 static void tipc_link_advance_backlog(struct tipc_link *l,
1023 struct sk_buff_head *xmitq)
1025 struct sk_buff *skb, *_skb;
1026 struct tipc_msg *hdr;
1027 u16 seqno = l->snd_nxt;
1028 u16 ack = l->rcv_nxt - 1;
1029 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1031 while (skb_queue_len(&l->transmq) < l->window) {
1032 skb = skb_peek(&l->backlogq);
1035 _skb = skb_clone(skb, GFP_ATOMIC);
1038 __skb_dequeue(&l->backlogq);
1040 l->backlog[msg_importance(hdr)].len--;
1041 __skb_queue_tail(&l->transmq, skb);
1042 /* next retransmit attempt */
1043 if (link_is_bc_sndlink(l))
1044 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1046 __skb_queue_tail(xmitq, _skb);
1047 TIPC_SKB_CB(skb)->ackers = l->ackers;
1048 msg_set_seqno(hdr, seqno);
1049 msg_set_ack(hdr, ack);
1050 msg_set_bcast_ack(hdr, bc_ack);
1052 l->stats.sent_pkts++;
1059 * link_retransmit_failure() - Detect repeated retransmit failures
1060 * @l: tipc link sender
1061 * @r: tipc link receiver (= l in case of unicast)
1062 * @rc: returned code
1064 * Return: true if the repeated retransmit failures happens, otherwise
1067 static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
1070 struct sk_buff *skb = skb_peek(&l->transmq);
1071 struct tipc_msg *hdr;
1076 if (!TIPC_SKB_CB(skb)->retr_cnt)
1079 if (!time_after(jiffies, TIPC_SKB_CB(skb)->retr_stamp +
1080 msecs_to_jiffies(r->tolerance)))
1084 if (link_is_bc_sndlink(l) && !less(r->acked, msg_seqno(hdr)))
1087 pr_warn("Retransmission failure on link <%s>\n", l->name);
1088 link_print(l, "State of link ");
1089 pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
1090 msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
1091 pr_info("sqno %u, prev: %x, dest: %x\n",
1092 msg_seqno(hdr), msg_prevnode(hdr), msg_destnode(hdr));
1093 pr_info("retr_stamp %d, retr_cnt %d\n",
1094 jiffies_to_msecs(TIPC_SKB_CB(skb)->retr_stamp),
1095 TIPC_SKB_CB(skb)->retr_cnt);
1097 trace_tipc_list_dump(&l->transmq, true, "retrans failure!");
1098 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "retrans failure!");
1099 trace_tipc_link_dump(r, TIPC_DUMP_NONE, "retrans failure!");
1101 if (link_is_bc_sndlink(l)) {
1102 r->state = LINK_RESET;
1103 *rc = TIPC_LINK_DOWN_EVT;
1105 *rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1111 /* tipc_link_bc_retrans() - retransmit zero or more packets
1112 * @l: the link to transmit on
1113 * @r: the receiving link ordering the retransmit. Same as l if unicast
1114 * @from: retransmit from (inclusive) this sequence number
1115 * @to: retransmit to (inclusive) this sequence number
1116 * xmitq: queue for accumulating the retransmitted packets
1118 static int tipc_link_bc_retrans(struct tipc_link *l, struct tipc_link *r,
1119 u16 from, u16 to, struct sk_buff_head *xmitq)
1121 struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
1122 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1123 u16 ack = l->rcv_nxt - 1;
1124 struct tipc_msg *hdr;
1132 trace_tipc_link_retrans(r, from, to, &l->transmq);
1134 if (link_retransmit_failure(l, r, &rc))
1137 skb_queue_walk(&l->transmq, skb) {
1139 if (less(msg_seqno(hdr), from))
1141 if (more(msg_seqno(hdr), to))
1144 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1146 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1147 _skb = __pskb_copy(skb, LL_MAX_HEADER + MIN_H_SIZE, GFP_ATOMIC);
1150 hdr = buf_msg(_skb);
1151 msg_set_ack(hdr, ack);
1152 msg_set_bcast_ack(hdr, bc_ack);
1153 _skb->priority = TC_PRIO_CONTROL;
1154 __skb_queue_tail(xmitq, _skb);
1155 l->stats.retransmitted++;
1157 /* Increase actual retrans counter & mark first time */
1158 if (!TIPC_SKB_CB(skb)->retr_cnt++)
1159 TIPC_SKB_CB(skb)->retr_stamp = jiffies;
1164 /* tipc_data_input - deliver data and name distr msgs to upper layer
1166 * Consumes buffer if message is of right type
1167 * Node lock must be held
1169 static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
1170 struct sk_buff_head *inputq)
1172 struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
1173 struct tipc_msg *hdr = buf_msg(skb);
1175 switch (msg_user(hdr)) {
1176 case TIPC_LOW_IMPORTANCE:
1177 case TIPC_MEDIUM_IMPORTANCE:
1178 case TIPC_HIGH_IMPORTANCE:
1179 case TIPC_CRITICAL_IMPORTANCE:
1180 if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
1181 skb_queue_tail(mc_inputq, skb);
1186 skb_queue_tail(inputq, skb);
1188 case GROUP_PROTOCOL:
1189 skb_queue_tail(mc_inputq, skb);
1191 case NAME_DISTRIBUTOR:
1192 l->bc_rcvlink->state = LINK_ESTABLISHED;
1193 skb_queue_tail(l->namedq, skb);
1196 case TUNNEL_PROTOCOL:
1197 case MSG_FRAGMENTER:
1198 case BCAST_PROTOCOL:
1201 pr_warn("Dropping received illegal msg type\n");
1207 /* tipc_link_input - process packet that has passed link protocol check
1211 static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
1212 struct sk_buff_head *inputq,
1213 struct sk_buff **reasm_skb)
1215 struct tipc_msg *hdr = buf_msg(skb);
1216 struct sk_buff *iskb;
1217 struct sk_buff_head tmpq;
1218 int usr = msg_user(hdr);
1221 if (usr == MSG_BUNDLER) {
1222 skb_queue_head_init(&tmpq);
1223 l->stats.recv_bundles++;
1224 l->stats.recv_bundled += msg_msgcnt(hdr);
1225 while (tipc_msg_extract(skb, &iskb, &pos))
1226 tipc_data_input(l, iskb, &tmpq);
1227 tipc_skb_queue_splice_tail(&tmpq, inputq);
1229 } else if (usr == MSG_FRAGMENTER) {
1230 l->stats.recv_fragments++;
1231 if (tipc_buf_append(reasm_skb, &skb)) {
1232 l->stats.recv_fragmented++;
1233 tipc_data_input(l, skb, inputq);
1234 } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
1235 pr_warn_ratelimited("Unable to build fragment list\n");
1236 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1239 } else if (usr == BCAST_PROTOCOL) {
1240 tipc_bcast_lock(l->net);
1241 tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
1242 tipc_bcast_unlock(l->net);
1249 /* tipc_link_tnl_rcv() - receive TUNNEL_PROTOCOL message, drop or process the
1250 * inner message along with the ones in the old link's
1253 * @skb: TUNNEL_PROTOCOL message
1254 * @inputq: queue to put messages ready for delivery
1256 static int tipc_link_tnl_rcv(struct tipc_link *l, struct sk_buff *skb,
1257 struct sk_buff_head *inputq)
1259 struct sk_buff **reasm_skb = &l->failover_reasm_skb;
1260 struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
1261 struct sk_buff_head *fdefq = &l->failover_deferdq;
1262 struct tipc_msg *hdr = buf_msg(skb);
1263 struct sk_buff *iskb;
1268 if (msg_type(hdr) == SYNCH_MSG) {
1273 /* Not a fragment? */
1274 if (likely(!msg_nof_fragms(hdr))) {
1275 if (unlikely(!tipc_msg_extract(skb, &iskb, &ipos))) {
1276 pr_warn_ratelimited("Unable to extract msg, defq: %d\n",
1277 skb_queue_len(fdefq));
1282 /* Set fragment type for buf_append */
1283 if (msg_fragm_no(hdr) == 1)
1284 msg_set_type(hdr, FIRST_FRAGMENT);
1285 else if (msg_fragm_no(hdr) < msg_nof_fragms(hdr))
1286 msg_set_type(hdr, FRAGMENT);
1288 msg_set_type(hdr, LAST_FRAGMENT);
1290 if (!tipc_buf_append(reasm_tnlmsg, &skb)) {
1291 /* Successful but non-complete reassembly? */
1292 if (*reasm_tnlmsg || link_is_bc_rcvlink(l))
1294 pr_warn_ratelimited("Unable to reassemble tunnel msg\n");
1295 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1301 seqno = buf_seqno(iskb);
1302 if (unlikely(less(seqno, l->drop_point))) {
1306 if (unlikely(seqno != l->drop_point)) {
1307 __tipc_skb_queue_sorted(fdefq, seqno, iskb);
1312 if (!tipc_data_input(l, iskb, inputq))
1313 rc |= tipc_link_input(l, iskb, inputq, reasm_skb);
1316 } while ((iskb = __tipc_skb_dequeue(fdefq, l->drop_point)));
1321 static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
1323 bool released = false;
1324 struct sk_buff *skb, *tmp;
1326 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1327 if (more(buf_seqno(skb), acked))
1329 __skb_unlink(skb, &l->transmq);
1336 /* tipc_build_gap_ack_blks - build Gap ACK blocks
1337 * @l: tipc link that data have come with gaps in sequence if any
1338 * @data: data buffer to store the Gap ACK blocks after built
1340 * returns the actual allocated memory size
1342 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data)
1344 struct sk_buff *skb = skb_peek(&l->deferdq);
1345 struct tipc_gap_ack_blks *ga = data;
1346 u16 len, expect, seqno = 0;
1352 expect = buf_seqno(skb);
1353 skb_queue_walk(&l->deferdq, skb) {
1354 seqno = buf_seqno(skb);
1355 if (unlikely(more(seqno, expect))) {
1356 ga->gacks[n].ack = htons(expect - 1);
1357 ga->gacks[n].gap = htons(seqno - expect);
1358 if (++n >= MAX_GAP_ACK_BLKS) {
1359 pr_info_ratelimited("Too few Gap ACK blocks!\n");
1362 } else if (unlikely(less(seqno, expect))) {
1363 pr_warn("Unexpected skb in deferdq!\n");
1370 ga->gacks[n].ack = htons(seqno);
1371 ga->gacks[n].gap = 0;
1375 len = tipc_gap_ack_blks_sz(n);
1376 ga->len = htons(len);
1381 /* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
1382 * acked packets, also doing retransmissions if
1384 * @l: tipc link with transmq queue to be advanced
1385 * @acked: seqno of last packet acked by peer without any gaps before
1386 * @gap: # of gap packets
1387 * @ga: buffer pointer to Gap ACK blocks from peer
1388 * @xmitq: queue for accumulating the retransmitted packets if any
1390 * In case of a repeated retransmit failures, the call will return shortly
1391 * with a returned code (e.g. TIPC_LINK_DOWN_EVT)
1393 static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
1394 struct tipc_gap_ack_blks *ga,
1395 struct sk_buff_head *xmitq)
1397 struct sk_buff *skb, *_skb, *tmp;
1398 struct tipc_msg *hdr;
1399 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1400 u16 ack = l->rcv_nxt - 1;
1401 bool passed = false;
1405 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1406 seqno = buf_seqno(skb);
1409 if (less_eq(seqno, acked)) {
1411 __skb_unlink(skb, &l->transmq);
1413 } else if (less_eq(seqno, acked + gap)) {
1414 /* First, check if repeated retrans failures occurs? */
1415 if (!passed && link_retransmit_failure(l, l, &rc))
1419 /* retransmit skb if unrestricted*/
1420 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1422 TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
1423 _skb = __pskb_copy(skb, LL_MAX_HEADER + MIN_H_SIZE,
1427 hdr = buf_msg(_skb);
1428 msg_set_ack(hdr, ack);
1429 msg_set_bcast_ack(hdr, bc_ack);
1430 _skb->priority = TC_PRIO_CONTROL;
1431 __skb_queue_tail(xmitq, _skb);
1432 l->stats.retransmitted++;
1434 /* Increase actual retrans counter & mark first time */
1435 if (!TIPC_SKB_CB(skb)->retr_cnt++)
1436 TIPC_SKB_CB(skb)->retr_stamp = jiffies;
1438 /* retry with Gap ACK blocks if any */
1439 if (!ga || n >= ga->gack_cnt)
1441 acked = ntohs(ga->gacks[n].ack);
1442 gap = ntohs(ga->gacks[n].gap);
1451 /* tipc_link_build_state_msg: prepare link state message for transmission
1453 * Note that sending of broadcast ack is coordinated among nodes, to reduce
1454 * risk of ack storms towards the sender
1456 int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1461 /* Broadcast ACK must be sent via a unicast link => defer to caller */
1462 if (link_is_bc_rcvlink(l)) {
1463 if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
1467 /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
1468 l->snd_nxt = l->rcv_nxt;
1469 return TIPC_LINK_SND_STATE;
1474 l->stats.sent_acks++;
1475 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
1479 /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
1481 void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1483 int mtyp = RESET_MSG;
1484 struct sk_buff *skb;
1486 if (l->state == LINK_ESTABLISHING)
1487 mtyp = ACTIVATE_MSG;
1489 tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
1491 /* Inform peer that this endpoint is going down if applicable */
1492 skb = skb_peek_tail(xmitq);
1493 if (skb && (l->state == LINK_RESET))
1494 msg_set_peer_stopping(buf_msg(skb), 1);
1497 /* tipc_link_build_nack_msg: prepare link nack message for transmission
1498 * Note that sending of broadcast NACK is coordinated among nodes, to
1499 * reduce the risk of NACK storms towards the sender
1501 static int tipc_link_build_nack_msg(struct tipc_link *l,
1502 struct sk_buff_head *xmitq)
1504 u32 def_cnt = ++l->stats.deferred_recv;
1505 u32 defq_len = skb_queue_len(&l->deferdq);
1508 if (link_is_bc_rcvlink(l)) {
1509 match1 = def_cnt & 0xf;
1510 match2 = tipc_own_addr(l->net) & 0xf;
1511 if (match1 == match2)
1512 return TIPC_LINK_SND_STATE;
1516 if (defq_len >= 3 && !((defq_len - 3) % 16))
1517 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
1521 /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
1522 * @l: the link that should handle the message
1524 * @xmitq: queue to place packets to be sent after this call
1526 int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1527 struct sk_buff_head *xmitq)
1529 struct sk_buff_head *defq = &l->deferdq;
1530 struct tipc_msg *hdr = buf_msg(skb);
1531 u16 seqno, rcv_nxt, win_lim;
1534 /* Verify and update link state */
1535 if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
1536 return tipc_link_proto_rcv(l, skb, xmitq);
1538 /* Don't send probe at next timeout expiration */
1539 l->silent_intv_cnt = 0;
1543 seqno = msg_seqno(hdr);
1544 rcv_nxt = l->rcv_nxt;
1545 win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
1547 if (unlikely(!link_is_up(l))) {
1548 if (l->state == LINK_ESTABLISHING)
1549 rc = TIPC_LINK_UP_EVT;
1553 /* Drop if outside receive window */
1554 if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
1555 l->stats.duplicates++;
1559 /* Forward queues and wake up waiting users */
1560 if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
1561 tipc_link_advance_backlog(l, xmitq);
1562 if (unlikely(!skb_queue_empty(&l->wakeupq)))
1563 link_prepare_wakeup(l);
1566 /* Defer delivery if sequence gap */
1567 if (unlikely(seqno != rcv_nxt)) {
1568 __tipc_skb_queue_sorted(defq, seqno, skb);
1569 rc |= tipc_link_build_nack_msg(l, xmitq);
1573 /* Deliver packet */
1575 l->stats.recv_pkts++;
1577 if (unlikely(msg_user(hdr) == TUNNEL_PROTOCOL))
1578 rc |= tipc_link_tnl_rcv(l, skb, l->inputq);
1579 else if (!tipc_data_input(l, skb, l->inputq))
1580 rc |= tipc_link_input(l, skb, l->inputq, &l->reasm_buf);
1581 if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
1582 rc |= tipc_link_build_state_msg(l, xmitq);
1583 if (unlikely(rc & ~TIPC_LINK_SND_STATE))
1585 } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
1593 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
1594 bool probe_reply, u16 rcvgap,
1595 int tolerance, int priority,
1596 struct sk_buff_head *xmitq)
1598 struct tipc_link *bcl = l->bc_rcvlink;
1599 struct sk_buff *skb;
1600 struct tipc_msg *hdr;
1601 struct sk_buff_head *dfq = &l->deferdq;
1602 bool node_up = link_is_up(bcl);
1603 struct tipc_mon_state *mstate = &l->mon_state;
1608 /* Don't send protocol message during reset or link failover */
1609 if (tipc_link_is_blocked(l))
1612 if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
1615 if (!skb_queue_empty(dfq))
1616 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1618 skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
1619 tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
1620 l->addr, tipc_own_addr(l->net), 0, 0, 0);
1625 data = msg_data(hdr);
1626 msg_set_session(hdr, l->session);
1627 msg_set_bearer_id(hdr, l->bearer_id);
1628 msg_set_net_plane(hdr, l->net_plane);
1629 msg_set_next_sent(hdr, l->snd_nxt);
1630 msg_set_ack(hdr, l->rcv_nxt - 1);
1631 msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
1632 msg_set_bc_ack_invalid(hdr, !node_up);
1633 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
1634 msg_set_link_tolerance(hdr, tolerance);
1635 msg_set_linkprio(hdr, priority);
1636 msg_set_redundant_link(hdr, node_up);
1637 msg_set_seq_gap(hdr, 0);
1638 msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
1640 if (mtyp == STATE_MSG) {
1641 if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
1642 msg_set_seqno(hdr, l->snd_nxt_state++);
1643 msg_set_seq_gap(hdr, rcvgap);
1644 msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
1645 msg_set_probe(hdr, probe);
1646 msg_set_is_keepalive(hdr, probe || probe_reply);
1647 if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
1648 glen = tipc_build_gap_ack_blks(l, data);
1649 tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
1650 msg_set_size(hdr, INT_H_SIZE + glen + dlen);
1651 skb_trim(skb, INT_H_SIZE + glen + dlen);
1652 l->stats.sent_states++;
1655 /* RESET_MSG or ACTIVATE_MSG */
1656 if (mtyp == ACTIVATE_MSG) {
1657 msg_set_dest_session_valid(hdr, 1);
1658 msg_set_dest_session(hdr, l->peer_session);
1660 msg_set_max_pkt(hdr, l->advertised_mtu);
1661 strcpy(data, l->if_name);
1662 msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
1663 skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
1666 l->stats.sent_probes++;
1668 l->stats.sent_nacks++;
1669 skb->priority = TC_PRIO_CONTROL;
1670 __skb_queue_tail(xmitq, skb);
1671 trace_tipc_proto_build(skb, false, l->name);
1674 void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
1675 struct sk_buff_head *xmitq)
1677 u32 onode = tipc_own_addr(l->net);
1678 struct tipc_msg *hdr, *ihdr;
1679 struct sk_buff_head tnlq;
1680 struct sk_buff *skb;
1681 u32 dnode = l->addr;
1683 __skb_queue_head_init(&tnlq);
1684 skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
1685 INT_H_SIZE, BASIC_H_SIZE,
1686 dnode, onode, 0, 0, 0);
1688 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1693 msg_set_msgcnt(hdr, 1);
1694 msg_set_bearer_id(hdr, l->peer_bearer_id);
1696 ihdr = (struct tipc_msg *)msg_data(hdr);
1697 tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1698 BASIC_H_SIZE, dnode);
1699 msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
1700 __skb_queue_tail(&tnlq, skb);
1701 tipc_link_xmit(l, &tnlq, xmitq);
1704 /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
1705 * with contents of the link's transmit and backlog queues.
1707 void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1708 int mtyp, struct sk_buff_head *xmitq)
1710 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1711 struct sk_buff *skb, *tnlskb;
1712 struct tipc_msg *hdr, tnlhdr;
1713 struct sk_buff_head *queue = &l->transmq;
1714 struct sk_buff_head tmpxq, tnlq, frags;
1715 u16 pktlen, pktcnt, seqno = l->snd_nxt;
1716 bool pktcnt_need_update = false;
1723 __skb_queue_head_init(&tnlq);
1724 __skb_queue_head_init(&tmpxq);
1725 __skb_queue_head_init(&frags);
1727 /* At least one packet required for safe algorithm => add dummy */
1728 skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1729 BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
1730 0, 0, TIPC_ERR_NO_PORT);
1732 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1735 __skb_queue_tail(&tnlq, skb);
1736 tipc_link_xmit(l, &tnlq, &tmpxq);
1737 __skb_queue_purge(&tmpxq);
1740 * From now on, send only one single ("dummy") SYNCH message
1741 * to peer. The SYNCH message does not contain any data, just
1742 * a header conveying the synch point to the peer.
1744 if (mtyp == SYNCH_MSG && (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1745 tnlskb = tipc_msg_create(TUNNEL_PROTOCOL, SYNCH_MSG,
1746 INT_H_SIZE, 0, l->addr,
1747 tipc_own_addr(l->net),
1750 pr_warn("%sunable to create dummy SYNCH_MSG\n",
1755 hdr = buf_msg(tnlskb);
1756 syncpt = l->snd_nxt + skb_queue_len(&l->backlogq) - 1;
1757 msg_set_syncpt(hdr, syncpt);
1758 msg_set_bearer_id(hdr, l->peer_bearer_id);
1759 __skb_queue_tail(&tnlq, tnlskb);
1760 tipc_link_xmit(tnl, &tnlq, xmitq);
1764 /* Initialize reusable tunnel packet header */
1765 tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
1766 mtyp, INT_H_SIZE, l->addr);
1767 if (mtyp == SYNCH_MSG)
1768 pktcnt = l->snd_nxt - buf_seqno(skb_peek(&l->transmq));
1770 pktcnt = skb_queue_len(&l->transmq);
1771 pktcnt += skb_queue_len(&l->backlogq);
1772 msg_set_msgcnt(&tnlhdr, pktcnt);
1773 msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1775 /* Wrap each packet into a tunnel packet */
1776 skb_queue_walk(queue, skb) {
1778 if (queue == &l->backlogq)
1779 msg_set_seqno(hdr, seqno++);
1780 pktlen = msg_size(hdr);
1782 /* Tunnel link MTU is not large enough? This could be
1784 * 1) Link MTU has just changed or set differently;
1785 * 2) Or FAILOVER on the top of a SYNCH message
1787 * The 2nd case should not happen if peer supports
1788 * TIPC_TUNNEL_ENHANCED
1790 if (pktlen > tnl->mtu - INT_H_SIZE) {
1791 if (mtyp == FAILOVER_MSG &&
1792 (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1793 rc = tipc_msg_fragment(skb, &tnlhdr, tnl->mtu,
1796 pr_warn("%sunable to frag msg: rc %d\n",
1800 pktcnt += skb_queue_len(&frags) - 1;
1801 pktcnt_need_update = true;
1802 skb_queue_splice_tail_init(&frags, &tnlq);
1805 /* Unluckily, peer doesn't have TIPC_TUNNEL_ENHANCED
1806 * => Just warn it and return!
1808 pr_warn_ratelimited("%stoo large msg <%d, %d>: %d!\n",
1809 link_co_err, msg_user(hdr),
1810 msg_type(hdr), msg_size(hdr));
1814 msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
1815 tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
1817 pr_warn("%sunable to send packet\n", link_co_err);
1820 skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
1821 skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
1822 __skb_queue_tail(&tnlq, tnlskb);
1824 if (queue != &l->backlogq) {
1825 queue = &l->backlogq;
1829 if (pktcnt_need_update)
1830 skb_queue_walk(&tnlq, skb) {
1832 msg_set_msgcnt(hdr, pktcnt);
1835 tipc_link_xmit(tnl, &tnlq, xmitq);
1837 if (mtyp == FAILOVER_MSG) {
1838 tnl->drop_point = l->rcv_nxt;
1839 tnl->failover_reasm_skb = l->reasm_buf;
1840 l->reasm_buf = NULL;
1842 /* Failover the link's deferdq */
1843 if (unlikely(!skb_queue_empty(fdefq))) {
1844 pr_warn("Link failover deferdq not empty: %d!\n",
1845 skb_queue_len(fdefq));
1846 __skb_queue_purge(fdefq);
1848 skb_queue_splice_init(&l->deferdq, fdefq);
1853 * tipc_link_failover_prepare() - prepare tnl for link failover
1855 * This is a special version of the precursor - tipc_link_tnl_prepare(),
1856 * see the tipc_node_link_failover() for details
1860 * @xmitq: queue for messages to be xmited
1862 void tipc_link_failover_prepare(struct tipc_link *l, struct tipc_link *tnl,
1863 struct sk_buff_head *xmitq)
1865 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1867 tipc_link_create_dummy_tnl_msg(tnl, xmitq);
1869 /* This failover link enpoint was never established before,
1870 * so it has not received anything from peer.
1871 * Otherwise, it must be a normal failover situation or the
1872 * node has entered SELF_DOWN_PEER_LEAVING and both peer nodes
1873 * would have to start over from scratch instead.
1875 tnl->drop_point = 1;
1876 tnl->failover_reasm_skb = NULL;
1878 /* Initiate the link's failover deferdq */
1879 if (unlikely(!skb_queue_empty(fdefq))) {
1880 pr_warn("Link failover deferdq not empty: %d!\n",
1881 skb_queue_len(fdefq));
1882 __skb_queue_purge(fdefq);
1886 /* tipc_link_validate_msg(): validate message against current link state
1887 * Returns true if message should be accepted, otherwise false
1889 bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
1891 u16 curr_session = l->peer_session;
1892 u16 session = msg_session(hdr);
1893 int mtyp = msg_type(hdr);
1895 if (msg_user(hdr) != LINK_PROTOCOL)
1902 /* Accept only RESET with new session number */
1903 return more(session, curr_session);
1907 /* Accept only ACTIVATE with new or current session number */
1908 return !less(session, curr_session);
1910 /* Accept only STATE with current session number */
1913 if (session != curr_session)
1915 /* Extra sanity check */
1916 if (!link_is_up(l) && msg_ack(hdr))
1918 if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
1920 /* Accept only STATE with new sequence number */
1921 return !less(msg_seqno(hdr), l->rcv_nxt_state);
1927 /* tipc_link_proto_rcv(): receive link level protocol message :
1928 * Note that network plane id propagates through the network, and may
1929 * change at any time. The node with lowest numerical id determines
1932 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
1933 struct sk_buff_head *xmitq)
1935 struct tipc_msg *hdr = buf_msg(skb);
1936 struct tipc_gap_ack_blks *ga = NULL;
1938 u16 ack = msg_ack(hdr);
1939 u16 gap = msg_seq_gap(hdr);
1940 u16 peers_snd_nxt = msg_next_sent(hdr);
1941 u16 peers_tol = msg_link_tolerance(hdr);
1942 u16 peers_prio = msg_linkprio(hdr);
1943 u16 rcv_nxt = l->rcv_nxt;
1944 u16 dlen = msg_data_sz(hdr);
1945 int mtyp = msg_type(hdr);
1946 bool reply = msg_probe(hdr);
1952 trace_tipc_proto_rcv(skb, false, l->name);
1953 if (tipc_link_is_blocked(l) || !xmitq)
1956 if (tipc_own_addr(l->net) > msg_prevnode(hdr))
1957 l->net_plane = msg_net_plane(hdr);
1961 data = msg_data(hdr);
1963 if (!tipc_link_validate_msg(l, hdr)) {
1964 trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
1965 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
1972 /* Complete own link name with peer's interface name */
1973 if_name = strrchr(l->name, ':') + 1;
1974 if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
1976 if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
1978 strncpy(if_name, data, TIPC_MAX_IF_NAME);
1980 /* Update own tolerance if peer indicates a non-zero value */
1981 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
1982 l->tolerance = peers_tol;
1983 l->bc_rcvlink->tolerance = peers_tol;
1985 /* Update own priority if peer's priority is higher */
1986 if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
1987 l->priority = peers_prio;
1989 /* If peer is going down we want full re-establish cycle */
1990 if (msg_peer_stopping(hdr)) {
1991 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1995 /* If this endpoint was re-created while peer was ESTABLISHING
1996 * it doesn't know current session number. Force re-synch.
1998 if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
1999 l->session != msg_dest_session(hdr)) {
2000 if (less(l->session, msg_dest_session(hdr)))
2001 l->session = msg_dest_session(hdr) + 1;
2005 /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
2006 if (mtyp == RESET_MSG || !link_is_up(l))
2007 rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
2009 /* ACTIVATE_MSG takes up link if it was already locally reset */
2010 if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
2011 rc = TIPC_LINK_UP_EVT;
2013 l->peer_session = msg_session(hdr);
2014 l->in_session = true;
2015 l->peer_bearer_id = msg_bearer_id(hdr);
2016 if (l->mtu > msg_max_pkt(hdr))
2017 l->mtu = msg_max_pkt(hdr);
2021 l->rcv_nxt_state = msg_seqno(hdr) + 1;
2023 /* Update own tolerance if peer indicates a non-zero value */
2024 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
2025 l->tolerance = peers_tol;
2026 l->bc_rcvlink->tolerance = peers_tol;
2028 /* Update own prio if peer indicates a different value */
2029 if ((peers_prio != l->priority) &&
2030 in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
2031 l->priority = peers_prio;
2032 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2035 l->silent_intv_cnt = 0;
2036 l->stats.recv_states++;
2038 l->stats.recv_probes++;
2040 if (!link_is_up(l)) {
2041 if (l->state == LINK_ESTABLISHING)
2042 rc = TIPC_LINK_UP_EVT;
2046 /* Receive Gap ACK blocks from peer if any */
2047 if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
2048 ga = (struct tipc_gap_ack_blks *)data;
2049 glen = ntohs(ga->len);
2050 /* sanity check: if failed, ignore Gap ACK blocks */
2051 if (glen != tipc_gap_ack_blks_sz(ga->gack_cnt))
2055 tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
2056 &l->mon_state, l->bearer_id);
2058 /* Send NACK if peer has sent pkts we haven't received yet */
2059 if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
2060 rcvgap = peers_snd_nxt - l->rcv_nxt;
2061 if (rcvgap || reply)
2062 tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
2063 rcvgap, 0, 0, xmitq);
2065 rc |= tipc_link_advance_transmq(l, ack, gap, ga, xmitq);
2067 /* If NACK, retransmit will now start at right position */
2069 l->stats.recv_nacks++;
2071 tipc_link_advance_backlog(l, xmitq);
2072 if (unlikely(!skb_queue_empty(&l->wakeupq)))
2073 link_prepare_wakeup(l);
2080 /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
2082 static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
2084 struct sk_buff_head *xmitq)
2086 struct sk_buff *skb;
2087 struct tipc_msg *hdr;
2088 struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
2089 u16 ack = l->rcv_nxt - 1;
2090 u16 gap_to = peers_snd_nxt - 1;
2092 skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
2093 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
2097 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
2098 msg_set_bcast_ack(hdr, ack);
2099 msg_set_bcgap_after(hdr, ack);
2101 gap_to = buf_seqno(dfrd_skb) - 1;
2102 msg_set_bcgap_to(hdr, gap_to);
2103 msg_set_non_seq(hdr, bcast);
2104 __skb_queue_tail(xmitq, skb);
2108 /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
2110 * Give a newly added peer node the sequence number where it should
2111 * start receiving and acking broadcast packets.
2113 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
2114 struct sk_buff_head *xmitq)
2116 struct sk_buff_head list;
2118 __skb_queue_head_init(&list);
2119 if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
2121 msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
2122 tipc_link_xmit(l, &list, xmitq);
2125 /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
2127 void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
2129 int mtyp = msg_type(hdr);
2130 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2135 if (msg_user(hdr) == BCAST_PROTOCOL) {
2136 l->rcv_nxt = peers_snd_nxt;
2137 l->state = LINK_ESTABLISHED;
2141 if (l->peer_caps & TIPC_BCAST_SYNCH)
2144 if (msg_peer_node_is_up(hdr))
2147 /* Compatibility: accept older, less safe initial synch data */
2148 if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
2149 l->rcv_nxt = peers_snd_nxt;
2152 /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
2154 int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
2155 struct sk_buff_head *xmitq)
2157 struct tipc_link *snd_l = l->bc_sndlink;
2158 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2159 u16 from = msg_bcast_ack(hdr) + 1;
2160 u16 to = from + msg_bc_gap(hdr) - 1;
2166 if (!msg_peer_node_is_up(hdr))
2169 /* Open when peer ackowledges our bcast init msg (pkt #1) */
2171 l->bc_peer_is_up = true;
2173 if (!l->bc_peer_is_up)
2176 l->stats.recv_nacks++;
2178 /* Ignore if peers_snd_nxt goes beyond receive window */
2179 if (more(peers_snd_nxt, l->rcv_nxt + l->window))
2182 rc = tipc_link_bc_retrans(snd_l, l, from, to, xmitq);
2184 l->snd_nxt = peers_snd_nxt;
2185 if (link_bc_rcv_gap(l))
2186 rc |= TIPC_LINK_SND_STATE;
2188 /* Return now if sender supports nack via STATE messages */
2189 if (l->peer_caps & TIPC_BCAST_STATE_NACK)
2192 /* Otherwise, be backwards compatible */
2194 if (!more(peers_snd_nxt, l->rcv_nxt)) {
2195 l->nack_state = BC_NACK_SND_CONDITIONAL;
2199 /* Don't NACK if one was recently sent or peeked */
2200 if (l->nack_state == BC_NACK_SND_SUPPRESS) {
2201 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2205 /* Conditionally delay NACK sending until next synch rcv */
2206 if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
2207 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2208 if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
2212 /* Send NACK now but suppress next one */
2213 tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
2214 l->nack_state = BC_NACK_SND_SUPPRESS;
2218 void tipc_link_bc_ack_rcv(struct tipc_link *l, u16 acked,
2219 struct sk_buff_head *xmitq)
2221 struct sk_buff *skb, *tmp;
2222 struct tipc_link *snd_l = l->bc_sndlink;
2224 if (!link_is_up(l) || !l->bc_peer_is_up)
2227 if (!more(acked, l->acked))
2230 trace_tipc_link_bc_ack(l, l->acked, acked, &snd_l->transmq);
2231 /* Skip over packets peer has already acked */
2232 skb_queue_walk(&snd_l->transmq, skb) {
2233 if (more(buf_seqno(skb), l->acked))
2237 /* Update/release the packets peer is acking now */
2238 skb_queue_walk_from_safe(&snd_l->transmq, skb, tmp) {
2239 if (more(buf_seqno(skb), acked))
2241 if (!--TIPC_SKB_CB(skb)->ackers) {
2242 __skb_unlink(skb, &snd_l->transmq);
2247 tipc_link_advance_backlog(snd_l, xmitq);
2248 if (unlikely(!skb_queue_empty(&snd_l->wakeupq)))
2249 link_prepare_wakeup(snd_l);
2252 /* tipc_link_bc_nack_rcv(): receive broadcast nack message
2253 * This function is here for backwards compatibility, since
2254 * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
2256 int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
2257 struct sk_buff_head *xmitq)
2259 struct tipc_msg *hdr = buf_msg(skb);
2260 u32 dnode = msg_destnode(hdr);
2261 int mtyp = msg_type(hdr);
2262 u16 acked = msg_bcast_ack(hdr);
2263 u16 from = acked + 1;
2264 u16 to = msg_bcgap_to(hdr);
2265 u16 peers_snd_nxt = to + 1;
2270 if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
2273 if (mtyp != STATE_MSG)
2276 if (dnode == tipc_own_addr(l->net)) {
2277 tipc_link_bc_ack_rcv(l, acked, xmitq);
2278 rc = tipc_link_bc_retrans(l->bc_sndlink, l, from, to, xmitq);
2279 l->stats.recv_nacks++;
2283 /* Msg for other node => suppress own NACK at next sync if applicable */
2284 if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
2285 l->nack_state = BC_NACK_SND_SUPPRESS;
2290 void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
2292 int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
2295 l->backlog[TIPC_LOW_IMPORTANCE].limit = max_t(u16, 50, win);
2296 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = max_t(u16, 100, win * 2);
2297 l->backlog[TIPC_HIGH_IMPORTANCE].limit = max_t(u16, 150, win * 3);
2298 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = max_t(u16, 200, win * 4);
2299 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
2303 * link_reset_stats - reset link statistics
2304 * @l: pointer to link
2306 void tipc_link_reset_stats(struct tipc_link *l)
2308 memset(&l->stats, 0, sizeof(l->stats));
2311 static void link_print(struct tipc_link *l, const char *str)
2313 struct sk_buff *hskb = skb_peek(&l->transmq);
2314 u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
2315 u16 tail = l->snd_nxt - 1;
2317 pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
2318 pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
2319 skb_queue_len(&l->transmq), head, tail,
2320 skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
2323 /* Parse and validate nested (link) properties valid for media, bearer and link
2325 int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2329 err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
2330 tipc_nl_prop_policy, NULL);
2334 if (props[TIPC_NLA_PROP_PRIO]) {
2337 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2338 if (prio > TIPC_MAX_LINK_PRI)
2342 if (props[TIPC_NLA_PROP_TOL]) {
2345 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2346 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2350 if (props[TIPC_NLA_PROP_WIN]) {
2353 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2354 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2361 static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
2364 struct nlattr *stats;
2371 struct nla_map map[] = {
2372 {TIPC_NLA_STATS_RX_INFO, 0},
2373 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2374 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2375 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2376 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2377 {TIPC_NLA_STATS_TX_INFO, 0},
2378 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2379 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2380 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2381 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2382 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2383 s->msg_length_counts : 1},
2384 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2385 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2386 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2387 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2388 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2389 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2390 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2391 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2392 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2393 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2394 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2395 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2396 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2397 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2398 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2399 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2400 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2401 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2402 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2403 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2404 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2405 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2406 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2409 stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2413 for (i = 0; i < ARRAY_SIZE(map); i++)
2414 if (nla_put_u32(skb, map[i].key, map[i].val))
2417 nla_nest_end(skb, stats);
2421 nla_nest_cancel(skb, stats);
2426 /* Caller should hold appropriate locks to protect the link */
2427 int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2428 struct tipc_link *link, int nlflags)
2430 u32 self = tipc_own_addr(net);
2431 struct nlattr *attrs;
2432 struct nlattr *prop;
2436 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2437 nlflags, TIPC_NL_LINK_GET);
2441 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2445 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2447 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
2449 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
2451 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
2453 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
2456 if (tipc_link_is_up(link))
2457 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2460 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2463 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2466 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2468 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2470 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2473 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2475 nla_nest_end(msg->skb, prop);
2477 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2481 nla_nest_end(msg->skb, attrs);
2482 genlmsg_end(msg->skb, hdr);
2487 nla_nest_cancel(msg->skb, prop);
2489 nla_nest_cancel(msg->skb, attrs);
2491 genlmsg_cancel(msg->skb, hdr);
2496 static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
2497 struct tipc_stats *stats)
2500 struct nlattr *nest;
2507 struct nla_map map[] = {
2508 {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
2509 {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
2510 {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
2511 {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
2512 {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
2513 {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
2514 {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
2515 {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
2516 {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
2517 {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
2518 {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
2519 {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
2520 {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
2521 {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
2522 {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
2523 {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
2524 {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
2525 {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
2526 {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
2527 (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
2530 nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2534 for (i = 0; i < ARRAY_SIZE(map); i++)
2535 if (nla_put_u32(skb, map[i].key, map[i].val))
2538 nla_nest_end(skb, nest);
2542 nla_nest_cancel(skb, nest);
2547 int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
2551 struct nlattr *attrs;
2552 struct nlattr *prop;
2553 struct tipc_net *tn = net_generic(net, tipc_net_id);
2554 u32 bc_mode = tipc_bcast_get_broadcast_mode(net);
2555 u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
2556 struct tipc_link *bcl = tn->bcl;
2561 tipc_bcast_lock(net);
2563 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2564 NLM_F_MULTI, TIPC_NL_LINK_GET);
2566 tipc_bcast_unlock(net);
2570 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2574 /* The broadcast link is always up */
2575 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2578 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
2580 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
2582 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
2584 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
2587 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2590 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
2592 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
2594 if (bc_mode & BCLINK_MODE_SEL)
2595 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
2598 nla_nest_end(msg->skb, prop);
2600 err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
2604 tipc_bcast_unlock(net);
2605 nla_nest_end(msg->skb, attrs);
2606 genlmsg_end(msg->skb, hdr);
2611 nla_nest_cancel(msg->skb, prop);
2613 nla_nest_cancel(msg->skb, attrs);
2615 tipc_bcast_unlock(net);
2616 genlmsg_cancel(msg->skb, hdr);
2621 void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
2622 struct sk_buff_head *xmitq)
2626 l->bc_rcvlink->tolerance = tol;
2628 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
2631 void tipc_link_set_prio(struct tipc_link *l, u32 prio,
2632 struct sk_buff_head *xmitq)
2635 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
2638 void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
2640 l->abort_limit = limit;
2643 char *tipc_link_name_ext(struct tipc_link *l, char *buf)
2646 scnprintf(buf, TIPC_MAX_LINK_NAME, "null");
2647 else if (link_is_bc_sndlink(l))
2648 scnprintf(buf, TIPC_MAX_LINK_NAME, "broadcast-sender");
2649 else if (link_is_bc_rcvlink(l))
2650 scnprintf(buf, TIPC_MAX_LINK_NAME,
2651 "broadcast-receiver, peer %x", l->addr);
2653 memcpy(buf, l->name, TIPC_MAX_LINK_NAME);
2659 * tipc_link_dump - dump TIPC link data
2660 * @l: tipc link to be dumped
2661 * @dqueues: bitmask to decide if any link queue to be dumped?
2662 * - TIPC_DUMP_NONE: don't dump link queues
2663 * - TIPC_DUMP_TRANSMQ: dump link transmq queue
2664 * - TIPC_DUMP_BACKLOGQ: dump link backlog queue
2665 * - TIPC_DUMP_DEFERDQ: dump link deferd queue
2666 * - TIPC_DUMP_INPUTQ: dump link input queue
2667 * - TIPC_DUMP_WAKEUP: dump link wakeup queue
2668 * - TIPC_DUMP_ALL: dump all the link queues above
2669 * @buf: returned buffer of dump data in format
2671 int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
2674 size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
2675 struct sk_buff_head *list;
2676 struct sk_buff *hskb, *tskb;
2680 i += scnprintf(buf, sz, "link data: (null)\n");
2684 i += scnprintf(buf, sz, "link data: %x", l->addr);
2685 i += scnprintf(buf + i, sz - i, " %x", l->state);
2686 i += scnprintf(buf + i, sz - i, " %u", l->in_session);
2687 i += scnprintf(buf + i, sz - i, " %u", l->session);
2688 i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
2689 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
2690 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
2691 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
2692 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
2693 i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
2694 i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
2695 i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
2696 i += scnprintf(buf + i, sz - i, " %u", 0);
2697 i += scnprintf(buf + i, sz - i, " %u", 0);
2698 i += scnprintf(buf + i, sz - i, " %u", l->acked);
2701 len = skb_queue_len(list);
2702 hskb = skb_peek(list);
2703 tskb = skb_peek_tail(list);
2704 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2705 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2706 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2709 len = skb_queue_len(list);
2710 hskb = skb_peek(list);
2711 tskb = skb_peek_tail(list);
2712 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2713 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2714 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2716 list = &l->backlogq;
2717 len = skb_queue_len(list);
2718 hskb = skb_peek(list);
2719 tskb = skb_peek_tail(list);
2720 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2721 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2722 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2725 len = skb_queue_len(list);
2726 hskb = skb_peek(list);
2727 tskb = skb_peek_tail(list);
2728 i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
2729 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2730 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2732 if (dqueues & TIPC_DUMP_TRANSMQ) {
2733 i += scnprintf(buf + i, sz - i, "transmq: ");
2734 i += tipc_list_dump(&l->transmq, false, buf + i);
2736 if (dqueues & TIPC_DUMP_BACKLOGQ) {
2737 i += scnprintf(buf + i, sz - i,
2738 "backlogq: <%u %u %u %u %u>, ",
2739 l->backlog[TIPC_LOW_IMPORTANCE].len,
2740 l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
2741 l->backlog[TIPC_HIGH_IMPORTANCE].len,
2742 l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
2743 l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
2744 i += tipc_list_dump(&l->backlogq, false, buf + i);
2746 if (dqueues & TIPC_DUMP_DEFERDQ) {
2747 i += scnprintf(buf + i, sz - i, "deferdq: ");
2748 i += tipc_list_dump(&l->deferdq, false, buf + i);
2750 if (dqueues & TIPC_DUMP_INPUTQ) {
2751 i += scnprintf(buf + i, sz - i, "inputq: ");
2752 i += tipc_list_dump(l->inputq, false, buf + i);
2754 if (dqueues & TIPC_DUMP_WAKEUP) {
2755 i += scnprintf(buf + i, sz - i, "wakeup: ");
2756 i += tipc_list_dump(&l->wakeupq, false, buf + i);