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"
49 #include <linux/pkt_sched.h>
70 u32 link_congs; /* # port sends blocked by congestion */
73 u32 max_queue_sz; /* send queue size high water mark */
74 u32 accu_queue_sz; /* used for send queue size profiling */
75 u32 queue_sz_counts; /* used for send queue size profiling */
76 u32 msg_length_counts; /* used for message length profiling */
77 u32 msg_lengths_total; /* used for message length profiling */
78 u32 msg_length_profile[7]; /* used for msg. length profiling */
82 * struct tipc_link - TIPC link data structure
83 * @addr: network address of link's peer node
84 * @name: link name character string
85 * @media_addr: media address to use when sending messages over link
87 * @net: pointer to namespace struct
88 * @refcnt: reference counter for permanent references (owner node & timer)
89 * @peer_session: link session # being used by peer end of link
90 * @peer_bearer_id: bearer id used by link's peer endpoint
91 * @bearer_id: local bearer id used by link
92 * @tolerance: minimum link continuity loss needed to reset link [in ms]
93 * @abort_limit: # of unacknowledged continuity probes needed to reset link
94 * @state: current state of link FSM
95 * @peer_caps: bitmap describing capabilities of peer node
96 * @silent_intv_cnt: # of timer intervals without any reception from peer
97 * @proto_msg: template for control messages generated by link
98 * @pmsg: convenience pointer to "proto_msg" field
99 * @priority: current link priority
100 * @net_plane: current link network plane ('A' through 'H')
101 * @mon_state: cookie with information needed by link monitor
102 * @backlog_limit: backlog queue congestion thresholds (indexed by importance)
103 * @exp_msg_count: # of tunnelled messages expected during link changeover
104 * @reset_rcv_checkpt: seq # of last acknowledged message at time of link reset
105 * @mtu: current maximum packet size for this link
106 * @advertised_mtu: advertised own mtu when link is being established
107 * @transmitq: queue for sent, non-acked messages
108 * @backlogq: queue for messages waiting to be sent
109 * @snt_nxt: next sequence number to use for outbound messages
110 * @ackers: # of peers that needs to ack each packet before it can be released
111 * @acked: # last packet acked by a certain peer. Used for broadcast.
112 * @rcv_nxt: next sequence number to expect for inbound messages
113 * @deferred_queue: deferred queue saved OOS b'cast message received from node
114 * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
115 * @inputq: buffer queue for messages to be delivered upwards
116 * @namedq: buffer queue for name table messages to be delivered upwards
117 * @next_out: ptr to first unsent outbound message in queue
118 * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
119 * @long_msg_seq_no: next identifier to use for outbound fragmented messages
120 * @reasm_buf: head of partially reassembled inbound message fragments
121 * @bc_rcvr: marks that this is a broadcast receiver link
122 * @stats: collects statistics regarding link activity
126 char name[TIPC_MAX_LINK_NAME];
129 /* Management and link supervision data */
143 char if_name[TIPC_MAX_IF_NAME];
146 struct tipc_mon_state mon_state;
151 struct sk_buff *failover_reasm_skb;
152 struct sk_buff_head failover_deferdq;
154 /* Max packet negotiation */
159 struct sk_buff_head transmq;
160 struct sk_buff_head backlogq;
164 struct sk_buff *target_bskb;
172 struct sk_buff_head deferdq;
173 struct sk_buff_head *inputq;
174 struct sk_buff_head *namedq;
176 /* Congestion handling */
177 struct sk_buff_head wakeupq;
179 /* Fragmentation/reassembly */
180 struct sk_buff *reasm_buf;
181 struct sk_buff *reasm_tnlmsg;
186 struct tipc_link *bc_rcvlink;
187 struct tipc_link *bc_sndlink;
192 struct tipc_stats stats;
196 * Error message prefixes
198 static const char *link_co_err = "Link tunneling error, ";
199 static const char *link_rst_msg = "Resetting link ";
201 /* Send states for broadcast NACKs
204 BC_NACK_SND_CONDITIONAL,
205 BC_NACK_SND_UNCONDITIONAL,
206 BC_NACK_SND_SUPPRESS,
209 #define TIPC_BC_RETR_LIM (jiffies + msecs_to_jiffies(10))
210 #define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
213 * Interval between NACKs when packets arrive out of order
215 #define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
220 LINK_ESTABLISHED = 0xe,
221 LINK_ESTABLISHING = 0xe << 4,
222 LINK_RESET = 0x1 << 8,
223 LINK_RESETTING = 0x2 << 12,
224 LINK_PEER_RESET = 0xd << 16,
225 LINK_FAILINGOVER = 0xf << 20,
226 LINK_SYNCHING = 0xc << 24
229 /* Link FSM state checking routines
231 static int link_is_up(struct tipc_link *l)
233 return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
236 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
237 struct sk_buff_head *xmitq);
238 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
239 bool probe_reply, u16 rcvgap,
240 int tolerance, int priority,
241 struct sk_buff_head *xmitq);
242 static void link_print(struct tipc_link *l, const char *str);
243 static int tipc_link_build_nack_msg(struct tipc_link *l,
244 struct sk_buff_head *xmitq);
245 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
246 struct sk_buff_head *xmitq);
247 static bool tipc_link_release_pkts(struct tipc_link *l, u16 to);
248 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data);
249 static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
250 struct tipc_gap_ack_blks *ga,
251 struct sk_buff_head *xmitq);
254 * Simple non-static link routines (i.e. referenced outside this file)
256 bool tipc_link_is_up(struct tipc_link *l)
258 return link_is_up(l);
261 bool tipc_link_peer_is_down(struct tipc_link *l)
263 return l->state == LINK_PEER_RESET;
266 bool tipc_link_is_reset(struct tipc_link *l)
268 return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
271 bool tipc_link_is_establishing(struct tipc_link *l)
273 return l->state == LINK_ESTABLISHING;
276 bool tipc_link_is_synching(struct tipc_link *l)
278 return l->state == LINK_SYNCHING;
281 bool tipc_link_is_failingover(struct tipc_link *l)
283 return l->state == LINK_FAILINGOVER;
286 bool tipc_link_is_blocked(struct tipc_link *l)
288 return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
291 static bool link_is_bc_sndlink(struct tipc_link *l)
293 return !l->bc_sndlink;
296 static bool link_is_bc_rcvlink(struct tipc_link *l)
298 return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
301 void tipc_link_set_active(struct tipc_link *l, bool active)
306 u32 tipc_link_id(struct tipc_link *l)
308 return l->peer_bearer_id << 16 | l->bearer_id;
311 int tipc_link_window(struct tipc_link *l)
316 int tipc_link_prio(struct tipc_link *l)
321 unsigned long tipc_link_tolerance(struct tipc_link *l)
326 struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
331 char tipc_link_plane(struct tipc_link *l)
336 void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
338 l->peer_caps = capabilities;
341 void tipc_link_add_bc_peer(struct tipc_link *snd_l,
342 struct tipc_link *uc_l,
343 struct sk_buff_head *xmitq)
345 struct tipc_link *rcv_l = uc_l->bc_rcvlink;
348 rcv_l->acked = snd_l->snd_nxt - 1;
349 snd_l->state = LINK_ESTABLISHED;
350 tipc_link_build_bc_init_msg(uc_l, xmitq);
353 void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
354 struct tipc_link *rcv_l,
355 struct sk_buff_head *xmitq)
357 u16 ack = snd_l->snd_nxt - 1;
360 rcv_l->bc_peer_is_up = true;
361 rcv_l->state = LINK_ESTABLISHED;
362 tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
363 trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
364 tipc_link_reset(rcv_l);
365 rcv_l->state = LINK_RESET;
366 if (!snd_l->ackers) {
367 trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
368 tipc_link_reset(snd_l);
369 snd_l->state = LINK_RESET;
370 __skb_queue_purge(xmitq);
374 int tipc_link_bc_peers(struct tipc_link *l)
379 static u16 link_bc_rcv_gap(struct tipc_link *l)
381 struct sk_buff *skb = skb_peek(&l->deferdq);
384 if (more(l->snd_nxt, l->rcv_nxt))
385 gap = l->snd_nxt - l->rcv_nxt;
387 gap = buf_seqno(skb) - l->rcv_nxt;
391 void tipc_link_set_mtu(struct tipc_link *l, int mtu)
396 int tipc_link_mtu(struct tipc_link *l)
401 int tipc_link_mss(struct tipc_link *l)
403 #ifdef CONFIG_TIPC_CRYPTO
404 return l->mtu - INT_H_SIZE - EMSG_OVERHEAD;
406 return l->mtu - INT_H_SIZE;
410 u16 tipc_link_rcv_nxt(struct tipc_link *l)
415 u16 tipc_link_acked(struct tipc_link *l)
420 char *tipc_link_name(struct tipc_link *l)
425 u32 tipc_link_state(struct tipc_link *l)
431 * tipc_link_create - create a new link
432 * @n: pointer to associated node
433 * @if_name: associated interface name
434 * @bearer_id: id (index) of associated bearer
435 * @tolerance: link tolerance to be used by link
436 * @net_plane: network plane (A,B,c..) this link belongs to
437 * @mtu: mtu to be advertised by link
438 * @priority: priority to be used by link
439 * @window: send window to be used by link
440 * @session: session to be used by link
441 * @ownnode: identity of own node
442 * @peer: node id of peer node
443 * @peer_caps: bitmap describing peer node capabilities
444 * @bc_sndlink: the namespace global link used for broadcast sending
445 * @bc_rcvlink: the peer specific link used for broadcast reception
446 * @inputq: queue to put messages ready for delivery
447 * @namedq: queue to put binding table update messages ready for delivery
448 * @link: return value, pointer to put the created link
450 * Returns true if link was created, otherwise false
452 bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
453 int tolerance, char net_plane, u32 mtu, int priority,
454 int window, u32 session, u32 self,
455 u32 peer, u8 *peer_id, u16 peer_caps,
456 struct tipc_link *bc_sndlink,
457 struct tipc_link *bc_rcvlink,
458 struct sk_buff_head *inputq,
459 struct sk_buff_head *namedq,
460 struct tipc_link **link)
462 char peer_str[NODE_ID_STR_LEN] = {0,};
463 char self_str[NODE_ID_STR_LEN] = {0,};
466 l = kzalloc(sizeof(*l), GFP_ATOMIC);
470 l->session = session;
472 /* Set link name for unicast links only */
474 tipc_nodeid2string(self_str, tipc_own_id(net));
475 if (strlen(self_str) > 16)
476 sprintf(self_str, "%x", self);
477 tipc_nodeid2string(peer_str, peer_id);
478 if (strlen(peer_str) > 16)
479 sprintf(peer_str, "%x", peer);
481 /* Peer i/f name will be completed by reset/activate message */
482 snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
483 self_str, if_name, peer_str);
485 strcpy(l->if_name, if_name);
487 l->peer_caps = peer_caps;
489 l->in_session = false;
490 l->bearer_id = bearer_id;
491 l->tolerance = tolerance;
493 bc_rcvlink->tolerance = tolerance;
494 l->net_plane = net_plane;
495 l->advertised_mtu = mtu;
497 l->priority = priority;
498 tipc_link_set_queue_limits(l, window);
500 l->bc_sndlink = bc_sndlink;
501 l->bc_rcvlink = bc_rcvlink;
504 l->state = LINK_RESETTING;
505 __skb_queue_head_init(&l->transmq);
506 __skb_queue_head_init(&l->backlogq);
507 __skb_queue_head_init(&l->deferdq);
508 __skb_queue_head_init(&l->failover_deferdq);
509 skb_queue_head_init(&l->wakeupq);
510 skb_queue_head_init(l->inputq);
515 * tipc_link_bc_create - create new link to be used for broadcast
516 * @n: pointer to associated node
517 * @mtu: mtu to be used initially if no peers
518 * @window: send window to be used
519 * @inputq: queue to put messages ready for delivery
520 * @namedq: queue to put binding table update messages ready for delivery
521 * @link: return value, pointer to put the created link
523 * Returns true if link was created, otherwise false
525 bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
526 int mtu, int window, u16 peer_caps,
527 struct sk_buff_head *inputq,
528 struct sk_buff_head *namedq,
529 struct tipc_link *bc_sndlink,
530 struct tipc_link **link)
534 if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
535 0, ownnode, peer, NULL, peer_caps, bc_sndlink,
536 NULL, inputq, namedq, link))
540 strcpy(l->name, tipc_bclink_name);
541 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
543 l->state = LINK_RESET;
547 /* Broadcast send link is always up */
548 if (link_is_bc_sndlink(l))
549 l->state = LINK_ESTABLISHED;
551 /* Disable replicast if even a single peer doesn't support it */
552 if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
553 tipc_bcast_toggle_rcast(net, false);
559 * tipc_link_fsm_evt - link finite state machine
560 * @l: pointer to link
561 * @evt: state machine event to be processed
563 int tipc_link_fsm_evt(struct tipc_link *l, int evt)
566 int old_state = l->state;
571 case LINK_PEER_RESET_EVT:
572 l->state = LINK_PEER_RESET;
575 l->state = LINK_RESET;
577 case LINK_FAILURE_EVT:
578 case LINK_FAILOVER_BEGIN_EVT:
579 case LINK_ESTABLISH_EVT:
580 case LINK_FAILOVER_END_EVT:
581 case LINK_SYNCH_BEGIN_EVT:
582 case LINK_SYNCH_END_EVT:
589 case LINK_PEER_RESET_EVT:
590 l->state = LINK_ESTABLISHING;
592 case LINK_FAILOVER_BEGIN_EVT:
593 l->state = LINK_FAILINGOVER;
594 case LINK_FAILURE_EVT:
596 case LINK_ESTABLISH_EVT:
597 case LINK_FAILOVER_END_EVT:
599 case LINK_SYNCH_BEGIN_EVT:
600 case LINK_SYNCH_END_EVT:
605 case LINK_PEER_RESET:
608 l->state = LINK_ESTABLISHING;
610 case LINK_PEER_RESET_EVT:
611 case LINK_ESTABLISH_EVT:
612 case LINK_FAILURE_EVT:
614 case LINK_SYNCH_BEGIN_EVT:
615 case LINK_SYNCH_END_EVT:
616 case LINK_FAILOVER_BEGIN_EVT:
617 case LINK_FAILOVER_END_EVT:
622 case LINK_FAILINGOVER:
624 case LINK_FAILOVER_END_EVT:
625 l->state = LINK_RESET;
627 case LINK_PEER_RESET_EVT:
629 case LINK_ESTABLISH_EVT:
630 case LINK_FAILURE_EVT:
632 case LINK_FAILOVER_BEGIN_EVT:
633 case LINK_SYNCH_BEGIN_EVT:
634 case LINK_SYNCH_END_EVT:
639 case LINK_ESTABLISHING:
641 case LINK_ESTABLISH_EVT:
642 l->state = LINK_ESTABLISHED;
644 case LINK_FAILOVER_BEGIN_EVT:
645 l->state = LINK_FAILINGOVER;
648 l->state = LINK_RESET;
650 case LINK_FAILURE_EVT:
651 case LINK_PEER_RESET_EVT:
652 case LINK_SYNCH_BEGIN_EVT:
653 case LINK_FAILOVER_END_EVT:
655 case LINK_SYNCH_END_EVT:
660 case LINK_ESTABLISHED:
662 case LINK_PEER_RESET_EVT:
663 l->state = LINK_PEER_RESET;
664 rc |= TIPC_LINK_DOWN_EVT;
666 case LINK_FAILURE_EVT:
667 l->state = LINK_RESETTING;
668 rc |= TIPC_LINK_DOWN_EVT;
671 l->state = LINK_RESET;
673 case LINK_ESTABLISH_EVT:
674 case LINK_SYNCH_END_EVT:
676 case LINK_SYNCH_BEGIN_EVT:
677 l->state = LINK_SYNCHING;
679 case LINK_FAILOVER_BEGIN_EVT:
680 case LINK_FAILOVER_END_EVT:
687 case LINK_PEER_RESET_EVT:
688 l->state = LINK_PEER_RESET;
689 rc |= TIPC_LINK_DOWN_EVT;
691 case LINK_FAILURE_EVT:
692 l->state = LINK_RESETTING;
693 rc |= TIPC_LINK_DOWN_EVT;
696 l->state = LINK_RESET;
698 case LINK_ESTABLISH_EVT:
699 case LINK_SYNCH_BEGIN_EVT:
701 case LINK_SYNCH_END_EVT:
702 l->state = LINK_ESTABLISHED;
704 case LINK_FAILOVER_BEGIN_EVT:
705 case LINK_FAILOVER_END_EVT:
711 pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
713 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
716 pr_err("Illegal FSM event %x in state %x on link %s\n",
717 evt, l->state, l->name);
718 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
722 /* link_profile_stats - update statistical profiling of traffic
724 static void link_profile_stats(struct tipc_link *l)
727 struct tipc_msg *msg;
730 /* Update counters used in statistical profiling of send traffic */
731 l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
732 l->stats.queue_sz_counts++;
734 skb = skb_peek(&l->transmq);
738 length = msg_size(msg);
740 if (msg_user(msg) == MSG_FRAGMENTER) {
741 if (msg_type(msg) != FIRST_FRAGMENT)
743 length = msg_size(msg_inner_hdr(msg));
745 l->stats.msg_lengths_total += length;
746 l->stats.msg_length_counts++;
748 l->stats.msg_length_profile[0]++;
749 else if (length <= 256)
750 l->stats.msg_length_profile[1]++;
751 else if (length <= 1024)
752 l->stats.msg_length_profile[2]++;
753 else if (length <= 4096)
754 l->stats.msg_length_profile[3]++;
755 else if (length <= 16384)
756 l->stats.msg_length_profile[4]++;
757 else if (length <= 32768)
758 l->stats.msg_length_profile[5]++;
760 l->stats.msg_length_profile[6]++;
764 * tipc_link_too_silent - check if link is "too silent"
765 * @l: tipc link to be checked
767 * Returns true if the link 'silent_intv_cnt' is about to reach the
768 * 'abort_limit' value, otherwise false
770 bool tipc_link_too_silent(struct tipc_link *l)
772 return (l->silent_intv_cnt + 2 > l->abort_limit);
775 /* tipc_link_timeout - perform periodic task as instructed from node timeout
777 int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
784 u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
785 u16 bc_acked = l->bc_rcvlink->acked;
786 struct tipc_mon_state *mstate = &l->mon_state;
788 trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
789 trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
791 case LINK_ESTABLISHED:
794 link_profile_stats(l);
795 tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
796 if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
797 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
798 state = bc_acked != bc_snt;
799 state |= l->bc_rcvlink->rcv_unacked;
800 state |= l->rcv_unacked;
801 state |= !skb_queue_empty(&l->transmq);
802 state |= !skb_queue_empty(&l->deferdq);
803 probe = mstate->probing;
804 probe |= l->silent_intv_cnt;
805 if (probe || mstate->monitoring)
806 l->silent_intv_cnt++;
809 setup = l->rst_cnt++ <= 4;
810 setup |= !(l->rst_cnt % 16);
813 case LINK_ESTABLISHING:
817 case LINK_PEER_RESET:
819 case LINK_FAILINGOVER:
825 if (state || probe || setup)
826 tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
832 * link_schedule_user - schedule a message sender for wakeup after congestion
834 * @hdr: header of message that is being sent
835 * Create pseudo msg to send back to user when congestion abates
837 static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
839 u32 dnode = tipc_own_addr(l->net);
840 u32 dport = msg_origport(hdr);
843 /* Create and schedule wakeup pseudo message */
844 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
845 dnode, l->addr, dport, 0, 0);
848 msg_set_dest_droppable(buf_msg(skb), true);
849 TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
850 skb_queue_tail(&l->wakeupq, skb);
851 l->stats.link_congs++;
852 trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
857 * link_prepare_wakeup - prepare users for wakeup after congestion
859 * Wake up a number of waiting users, as permitted by available space
862 static void link_prepare_wakeup(struct tipc_link *l)
864 struct sk_buff_head *wakeupq = &l->wakeupq;
865 struct sk_buff_head *inputq = l->inputq;
866 struct sk_buff *skb, *tmp;
867 struct sk_buff_head tmpq;
871 __skb_queue_head_init(&tmpq);
873 for (; imp <= TIPC_SYSTEM_IMPORTANCE; imp++)
874 avail[imp] = l->backlog[imp].limit - l->backlog[imp].len;
876 skb_queue_walk_safe(wakeupq, skb, tmp) {
877 imp = TIPC_SKB_CB(skb)->chain_imp;
881 __skb_unlink(skb, wakeupq);
882 __skb_queue_tail(&tmpq, skb);
885 spin_lock_bh(&inputq->lock);
886 skb_queue_splice_tail(&tmpq, inputq);
887 spin_unlock_bh(&inputq->lock);
891 void tipc_link_reset(struct tipc_link *l)
893 struct sk_buff_head list;
896 __skb_queue_head_init(&list);
898 l->in_session = false;
899 /* Force re-synch of peer session number before establishing */
902 l->mtu = l->advertised_mtu;
904 spin_lock_bh(&l->wakeupq.lock);
905 skb_queue_splice_init(&l->wakeupq, &list);
906 spin_unlock_bh(&l->wakeupq.lock);
908 spin_lock_bh(&l->inputq->lock);
909 skb_queue_splice_init(&list, l->inputq);
910 spin_unlock_bh(&l->inputq->lock);
912 __skb_queue_purge(&l->transmq);
913 __skb_queue_purge(&l->deferdq);
914 __skb_queue_purge(&l->backlogq);
915 __skb_queue_purge(&l->failover_deferdq);
916 for (imp = 0; imp <= TIPC_SYSTEM_IMPORTANCE; imp++) {
917 l->backlog[imp].len = 0;
918 l->backlog[imp].target_bskb = NULL;
920 kfree_skb(l->reasm_buf);
921 kfree_skb(l->reasm_tnlmsg);
922 kfree_skb(l->failover_reasm_skb);
924 l->reasm_tnlmsg = NULL;
925 l->failover_reasm_skb = NULL;
929 l->snd_nxt_state = 1;
930 l->rcv_nxt_state = 1;
932 l->silent_intv_cnt = 0;
934 l->bc_peer_is_up = false;
935 memset(&l->mon_state, 0, sizeof(l->mon_state));
936 tipc_link_reset_stats(l);
940 * tipc_link_xmit(): enqueue buffer list according to queue situation
942 * @list: chain of buffers containing message
943 * @xmitq: returned list of packets to be sent by caller
945 * Consumes the buffer chain.
946 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
947 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
949 int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
950 struct sk_buff_head *xmitq)
952 struct tipc_msg *hdr = buf_msg(skb_peek(list));
953 struct sk_buff_head *backlogq = &l->backlogq;
954 struct sk_buff_head *transmq = &l->transmq;
955 struct sk_buff *skb, *_skb;
956 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
957 u16 ack = l->rcv_nxt - 1;
958 u16 seqno = l->snd_nxt;
959 int pkt_cnt = skb_queue_len(list);
960 int imp = msg_importance(hdr);
961 unsigned int mss = tipc_link_mss(l);
962 unsigned int maxwin = l->window;
963 unsigned int mtu = l->mtu;
967 if (unlikely(msg_size(hdr) > mtu)) {
968 pr_warn("Too large msg, purging xmit list %d %d %d %d %d!\n",
969 skb_queue_len(list), msg_user(hdr),
970 msg_type(hdr), msg_size(hdr), mtu);
971 __skb_queue_purge(list);
975 /* Allow oversubscription of one data msg per source at congestion */
976 if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
977 if (imp == TIPC_SYSTEM_IMPORTANCE) {
978 pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
981 rc = link_schedule_user(l, hdr);
985 l->stats.sent_fragmented++;
986 l->stats.sent_fragments += pkt_cnt;
989 /* Prepare each packet for sending, and add to relevant queue: */
990 while ((skb = __skb_dequeue(list))) {
991 if (likely(skb_queue_len(transmq) < maxwin)) {
993 msg_set_seqno(hdr, seqno);
994 msg_set_ack(hdr, ack);
995 msg_set_bcast_ack(hdr, bc_ack);
996 _skb = skb_clone(skb, GFP_ATOMIC);
999 __skb_queue_purge(list);
1002 __skb_queue_tail(transmq, skb);
1003 /* next retransmit attempt */
1004 if (link_is_bc_sndlink(l))
1005 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1006 __skb_queue_tail(xmitq, _skb);
1007 TIPC_SKB_CB(skb)->ackers = l->ackers;
1009 l->stats.sent_pkts++;
1013 if (tipc_msg_try_bundle(l->backlog[imp].target_bskb, &skb,
1014 mss, l->addr, &new_bundle)) {
1016 /* Keep a ref. to the skb for next try */
1017 l->backlog[imp].target_bskb = skb;
1018 l->backlog[imp].len++;
1019 __skb_queue_tail(backlogq, skb);
1022 l->stats.sent_bundles++;
1023 l->stats.sent_bundled++;
1025 l->stats.sent_bundled++;
1029 l->backlog[imp].target_bskb = NULL;
1030 l->backlog[imp].len += (1 + skb_queue_len(list));
1031 __skb_queue_tail(backlogq, skb);
1032 skb_queue_splice_tail_init(list, backlogq);
1038 static void tipc_link_advance_backlog(struct tipc_link *l,
1039 struct sk_buff_head *xmitq)
1041 struct sk_buff *skb, *_skb;
1042 struct tipc_msg *hdr;
1043 u16 seqno = l->snd_nxt;
1044 u16 ack = l->rcv_nxt - 1;
1045 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1048 while (skb_queue_len(&l->transmq) < l->window) {
1049 skb = skb_peek(&l->backlogq);
1052 _skb = skb_clone(skb, GFP_ATOMIC);
1055 __skb_dequeue(&l->backlogq);
1057 imp = msg_importance(hdr);
1058 l->backlog[imp].len--;
1059 if (unlikely(skb == l->backlog[imp].target_bskb))
1060 l->backlog[imp].target_bskb = NULL;
1061 __skb_queue_tail(&l->transmq, skb);
1062 /* next retransmit attempt */
1063 if (link_is_bc_sndlink(l))
1064 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1066 __skb_queue_tail(xmitq, _skb);
1067 TIPC_SKB_CB(skb)->ackers = l->ackers;
1068 msg_set_seqno(hdr, seqno);
1069 msg_set_ack(hdr, ack);
1070 msg_set_bcast_ack(hdr, bc_ack);
1072 l->stats.sent_pkts++;
1079 * link_retransmit_failure() - Detect repeated retransmit failures
1080 * @l: tipc link sender
1081 * @r: tipc link receiver (= l in case of unicast)
1082 * @rc: returned code
1084 * Return: true if the repeated retransmit failures happens, otherwise
1087 static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
1090 struct sk_buff *skb = skb_peek(&l->transmq);
1091 struct tipc_msg *hdr;
1096 if (!TIPC_SKB_CB(skb)->retr_cnt)
1099 if (!time_after(jiffies, TIPC_SKB_CB(skb)->retr_stamp +
1100 msecs_to_jiffies(r->tolerance * 10)))
1104 if (link_is_bc_sndlink(l) && !less(r->acked, msg_seqno(hdr)))
1107 pr_warn("Retransmission failure on link <%s>\n", l->name);
1108 link_print(l, "State of link ");
1109 pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
1110 msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
1111 pr_info("sqno %u, prev: %x, dest: %x\n",
1112 msg_seqno(hdr), msg_prevnode(hdr), msg_destnode(hdr));
1113 pr_info("retr_stamp %d, retr_cnt %d\n",
1114 jiffies_to_msecs(TIPC_SKB_CB(skb)->retr_stamp),
1115 TIPC_SKB_CB(skb)->retr_cnt);
1117 trace_tipc_list_dump(&l->transmq, true, "retrans failure!");
1118 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "retrans failure!");
1119 trace_tipc_link_dump(r, TIPC_DUMP_NONE, "retrans failure!");
1121 if (link_is_bc_sndlink(l)) {
1122 r->state = LINK_RESET;
1123 *rc = TIPC_LINK_DOWN_EVT;
1125 *rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1131 /* tipc_link_bc_retrans() - retransmit zero or more packets
1132 * @l: the link to transmit on
1133 * @r: the receiving link ordering the retransmit. Same as l if unicast
1134 * @from: retransmit from (inclusive) this sequence number
1135 * @to: retransmit to (inclusive) this sequence number
1136 * xmitq: queue for accumulating the retransmitted packets
1138 static int tipc_link_bc_retrans(struct tipc_link *l, struct tipc_link *r,
1139 u16 from, u16 to, struct sk_buff_head *xmitq)
1141 struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
1142 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1143 u16 ack = l->rcv_nxt - 1;
1144 struct tipc_msg *hdr;
1152 trace_tipc_link_retrans(r, from, to, &l->transmq);
1154 if (link_retransmit_failure(l, r, &rc))
1157 skb_queue_walk(&l->transmq, skb) {
1159 if (less(msg_seqno(hdr), from))
1161 if (more(msg_seqno(hdr), to))
1164 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1166 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1167 _skb = pskb_copy(skb, GFP_ATOMIC);
1170 hdr = buf_msg(_skb);
1171 msg_set_ack(hdr, ack);
1172 msg_set_bcast_ack(hdr, bc_ack);
1173 _skb->priority = TC_PRIO_CONTROL;
1174 __skb_queue_tail(xmitq, _skb);
1175 l->stats.retransmitted++;
1177 /* Increase actual retrans counter & mark first time */
1178 if (!TIPC_SKB_CB(skb)->retr_cnt++)
1179 TIPC_SKB_CB(skb)->retr_stamp = jiffies;
1184 /* tipc_data_input - deliver data and name distr msgs to upper layer
1186 * Consumes buffer if message is of right type
1187 * Node lock must be held
1189 static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
1190 struct sk_buff_head *inputq)
1192 struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
1193 struct tipc_msg *hdr = buf_msg(skb);
1195 switch (msg_user(hdr)) {
1196 case TIPC_LOW_IMPORTANCE:
1197 case TIPC_MEDIUM_IMPORTANCE:
1198 case TIPC_HIGH_IMPORTANCE:
1199 case TIPC_CRITICAL_IMPORTANCE:
1200 if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
1201 skb_queue_tail(mc_inputq, skb);
1206 skb_queue_tail(inputq, skb);
1208 case GROUP_PROTOCOL:
1209 skb_queue_tail(mc_inputq, skb);
1211 case NAME_DISTRIBUTOR:
1212 l->bc_rcvlink->state = LINK_ESTABLISHED;
1213 skb_queue_tail(l->namedq, skb);
1216 case TUNNEL_PROTOCOL:
1217 case MSG_FRAGMENTER:
1218 case BCAST_PROTOCOL:
1221 pr_warn("Dropping received illegal msg type\n");
1227 /* tipc_link_input - process packet that has passed link protocol check
1231 static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
1232 struct sk_buff_head *inputq,
1233 struct sk_buff **reasm_skb)
1235 struct tipc_msg *hdr = buf_msg(skb);
1236 struct sk_buff *iskb;
1237 struct sk_buff_head tmpq;
1238 int usr = msg_user(hdr);
1241 if (usr == MSG_BUNDLER) {
1242 skb_queue_head_init(&tmpq);
1243 l->stats.recv_bundles++;
1244 l->stats.recv_bundled += msg_msgcnt(hdr);
1245 while (tipc_msg_extract(skb, &iskb, &pos))
1246 tipc_data_input(l, iskb, &tmpq);
1247 tipc_skb_queue_splice_tail(&tmpq, inputq);
1249 } else if (usr == MSG_FRAGMENTER) {
1250 l->stats.recv_fragments++;
1251 if (tipc_buf_append(reasm_skb, &skb)) {
1252 l->stats.recv_fragmented++;
1253 tipc_data_input(l, skb, inputq);
1254 } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
1255 pr_warn_ratelimited("Unable to build fragment list\n");
1256 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1259 } else if (usr == BCAST_PROTOCOL) {
1260 tipc_bcast_lock(l->net);
1261 tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
1262 tipc_bcast_unlock(l->net);
1269 /* tipc_link_tnl_rcv() - receive TUNNEL_PROTOCOL message, drop or process the
1270 * inner message along with the ones in the old link's
1273 * @skb: TUNNEL_PROTOCOL message
1274 * @inputq: queue to put messages ready for delivery
1276 static int tipc_link_tnl_rcv(struct tipc_link *l, struct sk_buff *skb,
1277 struct sk_buff_head *inputq)
1279 struct sk_buff **reasm_skb = &l->failover_reasm_skb;
1280 struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
1281 struct sk_buff_head *fdefq = &l->failover_deferdq;
1282 struct tipc_msg *hdr = buf_msg(skb);
1283 struct sk_buff *iskb;
1288 if (msg_type(hdr) == SYNCH_MSG) {
1293 /* Not a fragment? */
1294 if (likely(!msg_nof_fragms(hdr))) {
1295 if (unlikely(!tipc_msg_extract(skb, &iskb, &ipos))) {
1296 pr_warn_ratelimited("Unable to extract msg, defq: %d\n",
1297 skb_queue_len(fdefq));
1302 /* Set fragment type for buf_append */
1303 if (msg_fragm_no(hdr) == 1)
1304 msg_set_type(hdr, FIRST_FRAGMENT);
1305 else if (msg_fragm_no(hdr) < msg_nof_fragms(hdr))
1306 msg_set_type(hdr, FRAGMENT);
1308 msg_set_type(hdr, LAST_FRAGMENT);
1310 if (!tipc_buf_append(reasm_tnlmsg, &skb)) {
1311 /* Successful but non-complete reassembly? */
1312 if (*reasm_tnlmsg || link_is_bc_rcvlink(l))
1314 pr_warn_ratelimited("Unable to reassemble tunnel msg\n");
1315 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1321 seqno = buf_seqno(iskb);
1322 if (unlikely(less(seqno, l->drop_point))) {
1326 if (unlikely(seqno != l->drop_point)) {
1327 __tipc_skb_queue_sorted(fdefq, seqno, iskb);
1332 if (!tipc_data_input(l, iskb, inputq))
1333 rc |= tipc_link_input(l, iskb, inputq, reasm_skb);
1336 } while ((iskb = __tipc_skb_dequeue(fdefq, l->drop_point)));
1341 static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
1343 bool released = false;
1344 struct sk_buff *skb, *tmp;
1346 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1347 if (more(buf_seqno(skb), acked))
1349 __skb_unlink(skb, &l->transmq);
1356 /* tipc_build_gap_ack_blks - build Gap ACK blocks
1357 * @l: tipc link that data have come with gaps in sequence if any
1358 * @data: data buffer to store the Gap ACK blocks after built
1360 * returns the actual allocated memory size
1362 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data)
1364 struct sk_buff *skb = skb_peek(&l->deferdq);
1365 struct tipc_gap_ack_blks *ga = data;
1366 u16 len, expect, seqno = 0;
1372 expect = buf_seqno(skb);
1373 skb_queue_walk(&l->deferdq, skb) {
1374 seqno = buf_seqno(skb);
1375 if (unlikely(more(seqno, expect))) {
1376 ga->gacks[n].ack = htons(expect - 1);
1377 ga->gacks[n].gap = htons(seqno - expect);
1378 if (++n >= MAX_GAP_ACK_BLKS) {
1379 pr_info_ratelimited("Too few Gap ACK blocks!\n");
1382 } else if (unlikely(less(seqno, expect))) {
1383 pr_warn("Unexpected skb in deferdq!\n");
1390 ga->gacks[n].ack = htons(seqno);
1391 ga->gacks[n].gap = 0;
1395 len = tipc_gap_ack_blks_sz(n);
1396 ga->len = htons(len);
1401 /* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
1402 * acked packets, also doing retransmissions if
1404 * @l: tipc link with transmq queue to be advanced
1405 * @acked: seqno of last packet acked by peer without any gaps before
1406 * @gap: # of gap packets
1407 * @ga: buffer pointer to Gap ACK blocks from peer
1408 * @xmitq: queue for accumulating the retransmitted packets if any
1410 * In case of a repeated retransmit failures, the call will return shortly
1411 * with a returned code (e.g. TIPC_LINK_DOWN_EVT)
1413 static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
1414 struct tipc_gap_ack_blks *ga,
1415 struct sk_buff_head *xmitq)
1417 struct sk_buff *skb, *_skb, *tmp;
1418 struct tipc_msg *hdr;
1419 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1420 u16 ack = l->rcv_nxt - 1;
1421 bool passed = false;
1425 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1426 seqno = buf_seqno(skb);
1429 if (less_eq(seqno, acked)) {
1431 __skb_unlink(skb, &l->transmq);
1433 } else if (less_eq(seqno, acked + gap)) {
1434 /* First, check if repeated retrans failures occurs? */
1435 if (!passed && link_retransmit_failure(l, l, &rc))
1439 /* retransmit skb if unrestricted*/
1440 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1442 TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
1443 _skb = pskb_copy(skb, GFP_ATOMIC);
1446 hdr = buf_msg(_skb);
1447 msg_set_ack(hdr, ack);
1448 msg_set_bcast_ack(hdr, bc_ack);
1449 _skb->priority = TC_PRIO_CONTROL;
1450 __skb_queue_tail(xmitq, _skb);
1451 l->stats.retransmitted++;
1453 /* Increase actual retrans counter & mark first time */
1454 if (!TIPC_SKB_CB(skb)->retr_cnt++)
1455 TIPC_SKB_CB(skb)->retr_stamp = jiffies;
1457 /* retry with Gap ACK blocks if any */
1458 if (!ga || n >= ga->gack_cnt)
1460 acked = ntohs(ga->gacks[n].ack);
1461 gap = ntohs(ga->gacks[n].gap);
1470 /* tipc_link_build_state_msg: prepare link state message for transmission
1472 * Note that sending of broadcast ack is coordinated among nodes, to reduce
1473 * risk of ack storms towards the sender
1475 int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1480 /* Broadcast ACK must be sent via a unicast link => defer to caller */
1481 if (link_is_bc_rcvlink(l)) {
1482 if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
1486 /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
1487 l->snd_nxt = l->rcv_nxt;
1488 return TIPC_LINK_SND_STATE;
1493 l->stats.sent_acks++;
1494 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
1498 /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
1500 void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1502 int mtyp = RESET_MSG;
1503 struct sk_buff *skb;
1505 if (l->state == LINK_ESTABLISHING)
1506 mtyp = ACTIVATE_MSG;
1508 tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
1510 /* Inform peer that this endpoint is going down if applicable */
1511 skb = skb_peek_tail(xmitq);
1512 if (skb && (l->state == LINK_RESET))
1513 msg_set_peer_stopping(buf_msg(skb), 1);
1516 /* tipc_link_build_nack_msg: prepare link nack message for transmission
1517 * Note that sending of broadcast NACK is coordinated among nodes, to
1518 * reduce the risk of NACK storms towards the sender
1520 static int tipc_link_build_nack_msg(struct tipc_link *l,
1521 struct sk_buff_head *xmitq)
1523 u32 def_cnt = ++l->stats.deferred_recv;
1524 struct sk_buff_head *dfq = &l->deferdq;
1525 u32 defq_len = skb_queue_len(dfq);
1528 if (link_is_bc_rcvlink(l)) {
1529 match1 = def_cnt & 0xf;
1530 match2 = tipc_own_addr(l->net) & 0xf;
1531 if (match1 == match2)
1532 return TIPC_LINK_SND_STATE;
1536 if (defq_len >= 3 && !((defq_len - 3) % 16)) {
1537 u16 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1539 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0,
1540 rcvgap, 0, 0, xmitq);
1545 /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
1546 * @l: the link that should handle the message
1548 * @xmitq: queue to place packets to be sent after this call
1550 int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1551 struct sk_buff_head *xmitq)
1553 struct sk_buff_head *defq = &l->deferdq;
1554 struct tipc_msg *hdr = buf_msg(skb);
1555 u16 seqno, rcv_nxt, win_lim;
1558 /* Verify and update link state */
1559 if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
1560 return tipc_link_proto_rcv(l, skb, xmitq);
1562 /* Don't send probe at next timeout expiration */
1563 l->silent_intv_cnt = 0;
1567 seqno = msg_seqno(hdr);
1568 rcv_nxt = l->rcv_nxt;
1569 win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
1571 if (unlikely(!link_is_up(l))) {
1572 if (l->state == LINK_ESTABLISHING)
1573 rc = TIPC_LINK_UP_EVT;
1577 /* Drop if outside receive window */
1578 if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
1579 l->stats.duplicates++;
1583 /* Forward queues and wake up waiting users */
1584 if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
1585 tipc_link_advance_backlog(l, xmitq);
1586 if (unlikely(!skb_queue_empty(&l->wakeupq)))
1587 link_prepare_wakeup(l);
1590 /* Defer delivery if sequence gap */
1591 if (unlikely(seqno != rcv_nxt)) {
1592 __tipc_skb_queue_sorted(defq, seqno, skb);
1593 rc |= tipc_link_build_nack_msg(l, xmitq);
1597 /* Deliver packet */
1599 l->stats.recv_pkts++;
1601 if (unlikely(msg_user(hdr) == TUNNEL_PROTOCOL))
1602 rc |= tipc_link_tnl_rcv(l, skb, l->inputq);
1603 else if (!tipc_data_input(l, skb, l->inputq))
1604 rc |= tipc_link_input(l, skb, l->inputq, &l->reasm_buf);
1605 if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
1606 rc |= tipc_link_build_state_msg(l, xmitq);
1607 if (unlikely(rc & ~TIPC_LINK_SND_STATE))
1609 } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
1617 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
1618 bool probe_reply, u16 rcvgap,
1619 int tolerance, int priority,
1620 struct sk_buff_head *xmitq)
1622 struct tipc_link *bcl = l->bc_rcvlink;
1623 struct sk_buff *skb;
1624 struct tipc_msg *hdr;
1625 struct sk_buff_head *dfq = &l->deferdq;
1626 bool node_up = link_is_up(bcl);
1627 struct tipc_mon_state *mstate = &l->mon_state;
1632 /* Don't send protocol message during reset or link failover */
1633 if (tipc_link_is_blocked(l))
1636 if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
1639 if ((probe || probe_reply) && !skb_queue_empty(dfq))
1640 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1642 skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
1643 tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
1644 l->addr, tipc_own_addr(l->net), 0, 0, 0);
1649 data = msg_data(hdr);
1650 msg_set_session(hdr, l->session);
1651 msg_set_bearer_id(hdr, l->bearer_id);
1652 msg_set_net_plane(hdr, l->net_plane);
1653 msg_set_next_sent(hdr, l->snd_nxt);
1654 msg_set_ack(hdr, l->rcv_nxt - 1);
1655 msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
1656 msg_set_bc_ack_invalid(hdr, !node_up);
1657 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
1658 msg_set_link_tolerance(hdr, tolerance);
1659 msg_set_linkprio(hdr, priority);
1660 msg_set_redundant_link(hdr, node_up);
1661 msg_set_seq_gap(hdr, 0);
1662 msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
1664 if (mtyp == STATE_MSG) {
1665 if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
1666 msg_set_seqno(hdr, l->snd_nxt_state++);
1667 msg_set_seq_gap(hdr, rcvgap);
1668 msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
1669 msg_set_probe(hdr, probe);
1670 msg_set_is_keepalive(hdr, probe || probe_reply);
1671 if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
1672 glen = tipc_build_gap_ack_blks(l, data);
1673 tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
1674 msg_set_size(hdr, INT_H_SIZE + glen + dlen);
1675 skb_trim(skb, INT_H_SIZE + glen + dlen);
1676 l->stats.sent_states++;
1679 /* RESET_MSG or ACTIVATE_MSG */
1680 if (mtyp == ACTIVATE_MSG) {
1681 msg_set_dest_session_valid(hdr, 1);
1682 msg_set_dest_session(hdr, l->peer_session);
1684 msg_set_max_pkt(hdr, l->advertised_mtu);
1685 strcpy(data, l->if_name);
1686 msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
1687 skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
1690 l->stats.sent_probes++;
1692 l->stats.sent_nacks++;
1693 skb->priority = TC_PRIO_CONTROL;
1694 __skb_queue_tail(xmitq, skb);
1695 trace_tipc_proto_build(skb, false, l->name);
1698 void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
1699 struct sk_buff_head *xmitq)
1701 u32 onode = tipc_own_addr(l->net);
1702 struct tipc_msg *hdr, *ihdr;
1703 struct sk_buff_head tnlq;
1704 struct sk_buff *skb;
1705 u32 dnode = l->addr;
1707 __skb_queue_head_init(&tnlq);
1708 skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
1709 INT_H_SIZE, BASIC_H_SIZE,
1710 dnode, onode, 0, 0, 0);
1712 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1717 msg_set_msgcnt(hdr, 1);
1718 msg_set_bearer_id(hdr, l->peer_bearer_id);
1720 ihdr = (struct tipc_msg *)msg_data(hdr);
1721 tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1722 BASIC_H_SIZE, dnode);
1723 msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
1724 __skb_queue_tail(&tnlq, skb);
1725 tipc_link_xmit(l, &tnlq, xmitq);
1728 /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
1729 * with contents of the link's transmit and backlog queues.
1731 void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1732 int mtyp, struct sk_buff_head *xmitq)
1734 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1735 struct sk_buff *skb, *tnlskb;
1736 struct tipc_msg *hdr, tnlhdr;
1737 struct sk_buff_head *queue = &l->transmq;
1738 struct sk_buff_head tmpxq, tnlq, frags;
1739 u16 pktlen, pktcnt, seqno = l->snd_nxt;
1740 bool pktcnt_need_update = false;
1747 __skb_queue_head_init(&tnlq);
1749 * From now on, send only one single ("dummy") SYNCH message
1750 * to peer. The SYNCH message does not contain any data, just
1751 * a header conveying the synch point to the peer.
1753 if (mtyp == SYNCH_MSG && (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1754 tnlskb = tipc_msg_create(TUNNEL_PROTOCOL, SYNCH_MSG,
1755 INT_H_SIZE, 0, l->addr,
1756 tipc_own_addr(l->net),
1759 pr_warn("%sunable to create dummy SYNCH_MSG\n",
1764 hdr = buf_msg(tnlskb);
1765 syncpt = l->snd_nxt + skb_queue_len(&l->backlogq) - 1;
1766 msg_set_syncpt(hdr, syncpt);
1767 msg_set_bearer_id(hdr, l->peer_bearer_id);
1768 __skb_queue_tail(&tnlq, tnlskb);
1769 tipc_link_xmit(tnl, &tnlq, xmitq);
1773 __skb_queue_head_init(&tmpxq);
1774 __skb_queue_head_init(&frags);
1775 /* At least one packet required for safe algorithm => add dummy */
1776 skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1777 BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
1778 0, 0, TIPC_ERR_NO_PORT);
1780 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1783 __skb_queue_tail(&tnlq, skb);
1784 tipc_link_xmit(l, &tnlq, &tmpxq);
1785 __skb_queue_purge(&tmpxq);
1787 /* Initialize reusable tunnel packet header */
1788 tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
1789 mtyp, INT_H_SIZE, l->addr);
1790 if (mtyp == SYNCH_MSG)
1791 pktcnt = l->snd_nxt - buf_seqno(skb_peek(&l->transmq));
1793 pktcnt = skb_queue_len(&l->transmq);
1794 pktcnt += skb_queue_len(&l->backlogq);
1795 msg_set_msgcnt(&tnlhdr, pktcnt);
1796 msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1798 /* Wrap each packet into a tunnel packet */
1799 skb_queue_walk(queue, skb) {
1801 if (queue == &l->backlogq)
1802 msg_set_seqno(hdr, seqno++);
1803 pktlen = msg_size(hdr);
1805 /* Tunnel link MTU is not large enough? This could be
1807 * 1) Link MTU has just changed or set differently;
1808 * 2) Or FAILOVER on the top of a SYNCH message
1810 * The 2nd case should not happen if peer supports
1811 * TIPC_TUNNEL_ENHANCED
1813 if (pktlen > tnl->mtu - INT_H_SIZE) {
1814 if (mtyp == FAILOVER_MSG &&
1815 (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1816 rc = tipc_msg_fragment(skb, &tnlhdr, tnl->mtu,
1819 pr_warn("%sunable to frag msg: rc %d\n",
1823 pktcnt += skb_queue_len(&frags) - 1;
1824 pktcnt_need_update = true;
1825 skb_queue_splice_tail_init(&frags, &tnlq);
1828 /* Unluckily, peer doesn't have TIPC_TUNNEL_ENHANCED
1829 * => Just warn it and return!
1831 pr_warn_ratelimited("%stoo large msg <%d, %d>: %d!\n",
1832 link_co_err, msg_user(hdr),
1833 msg_type(hdr), msg_size(hdr));
1837 msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
1838 tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
1840 pr_warn("%sunable to send packet\n", link_co_err);
1843 skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
1844 skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
1845 __skb_queue_tail(&tnlq, tnlskb);
1847 if (queue != &l->backlogq) {
1848 queue = &l->backlogq;
1852 if (pktcnt_need_update)
1853 skb_queue_walk(&tnlq, skb) {
1855 msg_set_msgcnt(hdr, pktcnt);
1858 tipc_link_xmit(tnl, &tnlq, xmitq);
1860 if (mtyp == FAILOVER_MSG) {
1861 tnl->drop_point = l->rcv_nxt;
1862 tnl->failover_reasm_skb = l->reasm_buf;
1863 l->reasm_buf = NULL;
1865 /* Failover the link's deferdq */
1866 if (unlikely(!skb_queue_empty(fdefq))) {
1867 pr_warn("Link failover deferdq not empty: %d!\n",
1868 skb_queue_len(fdefq));
1869 __skb_queue_purge(fdefq);
1871 skb_queue_splice_init(&l->deferdq, fdefq);
1876 * tipc_link_failover_prepare() - prepare tnl for link failover
1878 * This is a special version of the precursor - tipc_link_tnl_prepare(),
1879 * see the tipc_node_link_failover() for details
1883 * @xmitq: queue for messages to be xmited
1885 void tipc_link_failover_prepare(struct tipc_link *l, struct tipc_link *tnl,
1886 struct sk_buff_head *xmitq)
1888 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1890 tipc_link_create_dummy_tnl_msg(tnl, xmitq);
1892 /* This failover link endpoint was never established before,
1893 * so it has not received anything from peer.
1894 * Otherwise, it must be a normal failover situation or the
1895 * node has entered SELF_DOWN_PEER_LEAVING and both peer nodes
1896 * would have to start over from scratch instead.
1898 tnl->drop_point = 1;
1899 tnl->failover_reasm_skb = NULL;
1901 /* Initiate the link's failover deferdq */
1902 if (unlikely(!skb_queue_empty(fdefq))) {
1903 pr_warn("Link failover deferdq not empty: %d!\n",
1904 skb_queue_len(fdefq));
1905 __skb_queue_purge(fdefq);
1909 /* tipc_link_validate_msg(): validate message against current link state
1910 * Returns true if message should be accepted, otherwise false
1912 bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
1914 u16 curr_session = l->peer_session;
1915 u16 session = msg_session(hdr);
1916 int mtyp = msg_type(hdr);
1918 if (msg_user(hdr) != LINK_PROTOCOL)
1925 /* Accept only RESET with new session number */
1926 return more(session, curr_session);
1930 /* Accept only ACTIVATE with new or current session number */
1931 return !less(session, curr_session);
1933 /* Accept only STATE with current session number */
1936 if (session != curr_session)
1938 /* Extra sanity check */
1939 if (!link_is_up(l) && msg_ack(hdr))
1941 if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
1943 /* Accept only STATE with new sequence number */
1944 return !less(msg_seqno(hdr), l->rcv_nxt_state);
1950 /* tipc_link_proto_rcv(): receive link level protocol message :
1951 * Note that network plane id propagates through the network, and may
1952 * change at any time. The node with lowest numerical id determines
1955 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
1956 struct sk_buff_head *xmitq)
1958 struct tipc_msg *hdr = buf_msg(skb);
1959 struct tipc_gap_ack_blks *ga = NULL;
1961 u16 ack = msg_ack(hdr);
1962 u16 gap = msg_seq_gap(hdr);
1963 u16 peers_snd_nxt = msg_next_sent(hdr);
1964 u16 peers_tol = msg_link_tolerance(hdr);
1965 u16 peers_prio = msg_linkprio(hdr);
1966 u16 rcv_nxt = l->rcv_nxt;
1967 u16 dlen = msg_data_sz(hdr);
1968 int mtyp = msg_type(hdr);
1969 bool reply = msg_probe(hdr);
1975 trace_tipc_proto_rcv(skb, false, l->name);
1976 if (tipc_link_is_blocked(l) || !xmitq)
1979 if (tipc_own_addr(l->net) > msg_prevnode(hdr))
1980 l->net_plane = msg_net_plane(hdr);
1984 data = msg_data(hdr);
1986 if (!tipc_link_validate_msg(l, hdr)) {
1987 trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
1988 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
1995 /* Complete own link name with peer's interface name */
1996 if_name = strrchr(l->name, ':') + 1;
1997 if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
1999 if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
2001 strncpy(if_name, data, TIPC_MAX_IF_NAME);
2003 /* Update own tolerance if peer indicates a non-zero value */
2004 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
2005 l->tolerance = peers_tol;
2006 l->bc_rcvlink->tolerance = peers_tol;
2008 /* Update own priority if peer's priority is higher */
2009 if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
2010 l->priority = peers_prio;
2012 /* If peer is going down we want full re-establish cycle */
2013 if (msg_peer_stopping(hdr)) {
2014 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2018 /* If this endpoint was re-created while peer was ESTABLISHING
2019 * it doesn't know current session number. Force re-synch.
2021 if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
2022 l->session != msg_dest_session(hdr)) {
2023 if (less(l->session, msg_dest_session(hdr)))
2024 l->session = msg_dest_session(hdr) + 1;
2028 /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
2029 if (mtyp == RESET_MSG || !link_is_up(l))
2030 rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
2032 /* ACTIVATE_MSG takes up link if it was already locally reset */
2033 if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
2034 rc = TIPC_LINK_UP_EVT;
2036 l->peer_session = msg_session(hdr);
2037 l->in_session = true;
2038 l->peer_bearer_id = msg_bearer_id(hdr);
2039 if (l->mtu > msg_max_pkt(hdr))
2040 l->mtu = msg_max_pkt(hdr);
2044 l->rcv_nxt_state = msg_seqno(hdr) + 1;
2046 /* Update own tolerance if peer indicates a non-zero value */
2047 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
2048 l->tolerance = peers_tol;
2049 l->bc_rcvlink->tolerance = peers_tol;
2051 /* Update own prio if peer indicates a different value */
2052 if ((peers_prio != l->priority) &&
2053 in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
2054 l->priority = peers_prio;
2055 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2058 l->silent_intv_cnt = 0;
2059 l->stats.recv_states++;
2061 l->stats.recv_probes++;
2063 if (!link_is_up(l)) {
2064 if (l->state == LINK_ESTABLISHING)
2065 rc = TIPC_LINK_UP_EVT;
2069 /* Receive Gap ACK blocks from peer if any */
2070 if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
2071 ga = (struct tipc_gap_ack_blks *)data;
2072 glen = ntohs(ga->len);
2073 /* sanity check: if failed, ignore Gap ACK blocks */
2074 if (glen != tipc_gap_ack_blks_sz(ga->gack_cnt))
2078 tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
2079 &l->mon_state, l->bearer_id);
2081 /* Send NACK if peer has sent pkts we haven't received yet */
2082 if ((reply || msg_is_keepalive(hdr)) &&
2083 more(peers_snd_nxt, rcv_nxt) &&
2084 !tipc_link_is_synching(l) &&
2085 skb_queue_empty(&l->deferdq))
2086 rcvgap = peers_snd_nxt - l->rcv_nxt;
2088 if (rcvgap || reply)
2089 tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
2090 rcvgap, 0, 0, xmitq);
2091 rc |= tipc_link_advance_transmq(l, ack, gap, ga, xmitq);
2093 /* If NACK, retransmit will now start at right position */
2095 l->stats.recv_nacks++;
2097 tipc_link_advance_backlog(l, xmitq);
2098 if (unlikely(!skb_queue_empty(&l->wakeupq)))
2099 link_prepare_wakeup(l);
2106 /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
2108 static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
2110 struct sk_buff_head *xmitq)
2112 struct sk_buff *skb;
2113 struct tipc_msg *hdr;
2114 struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
2115 u16 ack = l->rcv_nxt - 1;
2116 u16 gap_to = peers_snd_nxt - 1;
2118 skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
2119 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
2123 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
2124 msg_set_bcast_ack(hdr, ack);
2125 msg_set_bcgap_after(hdr, ack);
2127 gap_to = buf_seqno(dfrd_skb) - 1;
2128 msg_set_bcgap_to(hdr, gap_to);
2129 msg_set_non_seq(hdr, bcast);
2130 __skb_queue_tail(xmitq, skb);
2134 /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
2136 * Give a newly added peer node the sequence number where it should
2137 * start receiving and acking broadcast packets.
2139 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
2140 struct sk_buff_head *xmitq)
2142 struct sk_buff_head list;
2144 __skb_queue_head_init(&list);
2145 if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
2147 msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
2148 tipc_link_xmit(l, &list, xmitq);
2151 /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
2153 void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
2155 int mtyp = msg_type(hdr);
2156 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2161 if (msg_user(hdr) == BCAST_PROTOCOL) {
2162 l->rcv_nxt = peers_snd_nxt;
2163 l->state = LINK_ESTABLISHED;
2167 if (l->peer_caps & TIPC_BCAST_SYNCH)
2170 if (msg_peer_node_is_up(hdr))
2173 /* Compatibility: accept older, less safe initial synch data */
2174 if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
2175 l->rcv_nxt = peers_snd_nxt;
2178 /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
2180 int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
2181 struct sk_buff_head *xmitq)
2183 struct tipc_link *snd_l = l->bc_sndlink;
2184 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2185 u16 from = msg_bcast_ack(hdr) + 1;
2186 u16 to = from + msg_bc_gap(hdr) - 1;
2192 if (!msg_peer_node_is_up(hdr))
2195 /* Open when peer ackowledges our bcast init msg (pkt #1) */
2197 l->bc_peer_is_up = true;
2199 if (!l->bc_peer_is_up)
2202 l->stats.recv_nacks++;
2204 /* Ignore if peers_snd_nxt goes beyond receive window */
2205 if (more(peers_snd_nxt, l->rcv_nxt + l->window))
2208 rc = tipc_link_bc_retrans(snd_l, l, from, to, xmitq);
2210 l->snd_nxt = peers_snd_nxt;
2211 if (link_bc_rcv_gap(l))
2212 rc |= TIPC_LINK_SND_STATE;
2214 /* Return now if sender supports nack via STATE messages */
2215 if (l->peer_caps & TIPC_BCAST_STATE_NACK)
2218 /* Otherwise, be backwards compatible */
2220 if (!more(peers_snd_nxt, l->rcv_nxt)) {
2221 l->nack_state = BC_NACK_SND_CONDITIONAL;
2225 /* Don't NACK if one was recently sent or peeked */
2226 if (l->nack_state == BC_NACK_SND_SUPPRESS) {
2227 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2231 /* Conditionally delay NACK sending until next synch rcv */
2232 if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
2233 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2234 if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
2238 /* Send NACK now but suppress next one */
2239 tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
2240 l->nack_state = BC_NACK_SND_SUPPRESS;
2244 void tipc_link_bc_ack_rcv(struct tipc_link *l, u16 acked,
2245 struct sk_buff_head *xmitq)
2247 struct sk_buff *skb, *tmp;
2248 struct tipc_link *snd_l = l->bc_sndlink;
2250 if (!link_is_up(l) || !l->bc_peer_is_up)
2253 if (!more(acked, l->acked))
2256 trace_tipc_link_bc_ack(l, l->acked, acked, &snd_l->transmq);
2257 /* Skip over packets peer has already acked */
2258 skb_queue_walk(&snd_l->transmq, skb) {
2259 if (more(buf_seqno(skb), l->acked))
2263 /* Update/release the packets peer is acking now */
2264 skb_queue_walk_from_safe(&snd_l->transmq, skb, tmp) {
2265 if (more(buf_seqno(skb), acked))
2267 if (!--TIPC_SKB_CB(skb)->ackers) {
2268 __skb_unlink(skb, &snd_l->transmq);
2273 tipc_link_advance_backlog(snd_l, xmitq);
2274 if (unlikely(!skb_queue_empty(&snd_l->wakeupq)))
2275 link_prepare_wakeup(snd_l);
2278 /* tipc_link_bc_nack_rcv(): receive broadcast nack message
2279 * This function is here for backwards compatibility, since
2280 * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
2282 int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
2283 struct sk_buff_head *xmitq)
2285 struct tipc_msg *hdr = buf_msg(skb);
2286 u32 dnode = msg_destnode(hdr);
2287 int mtyp = msg_type(hdr);
2288 u16 acked = msg_bcast_ack(hdr);
2289 u16 from = acked + 1;
2290 u16 to = msg_bcgap_to(hdr);
2291 u16 peers_snd_nxt = to + 1;
2296 if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
2299 if (mtyp != STATE_MSG)
2302 if (dnode == tipc_own_addr(l->net)) {
2303 tipc_link_bc_ack_rcv(l, acked, xmitq);
2304 rc = tipc_link_bc_retrans(l->bc_sndlink, l, from, to, xmitq);
2305 l->stats.recv_nacks++;
2309 /* Msg for other node => suppress own NACK at next sync if applicable */
2310 if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
2311 l->nack_state = BC_NACK_SND_SUPPRESS;
2316 void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
2318 int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
2321 l->backlog[TIPC_LOW_IMPORTANCE].limit = max_t(u16, 50, win);
2322 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = max_t(u16, 100, win * 2);
2323 l->backlog[TIPC_HIGH_IMPORTANCE].limit = max_t(u16, 150, win * 3);
2324 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = max_t(u16, 200, win * 4);
2325 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
2329 * link_reset_stats - reset link statistics
2330 * @l: pointer to link
2332 void tipc_link_reset_stats(struct tipc_link *l)
2334 memset(&l->stats, 0, sizeof(l->stats));
2337 static void link_print(struct tipc_link *l, const char *str)
2339 struct sk_buff *hskb = skb_peek(&l->transmq);
2340 u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
2341 u16 tail = l->snd_nxt - 1;
2343 pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
2344 pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
2345 skb_queue_len(&l->transmq), head, tail,
2346 skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
2349 /* Parse and validate nested (link) properties valid for media, bearer and link
2351 int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2355 err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
2356 tipc_nl_prop_policy, NULL);
2360 if (props[TIPC_NLA_PROP_PRIO]) {
2363 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2364 if (prio > TIPC_MAX_LINK_PRI)
2368 if (props[TIPC_NLA_PROP_TOL]) {
2371 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2372 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2376 if (props[TIPC_NLA_PROP_WIN]) {
2379 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2380 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2387 static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
2390 struct nlattr *stats;
2397 struct nla_map map[] = {
2398 {TIPC_NLA_STATS_RX_INFO, 0},
2399 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2400 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2401 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2402 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2403 {TIPC_NLA_STATS_TX_INFO, 0},
2404 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2405 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2406 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2407 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2408 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2409 s->msg_length_counts : 1},
2410 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2411 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2412 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2413 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2414 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2415 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2416 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2417 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2418 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2419 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2420 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2421 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2422 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2423 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2424 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2425 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2426 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2427 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2428 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2429 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2430 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2431 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2432 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2435 stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2439 for (i = 0; i < ARRAY_SIZE(map); i++)
2440 if (nla_put_u32(skb, map[i].key, map[i].val))
2443 nla_nest_end(skb, stats);
2447 nla_nest_cancel(skb, stats);
2452 /* Caller should hold appropriate locks to protect the link */
2453 int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2454 struct tipc_link *link, int nlflags)
2456 u32 self = tipc_own_addr(net);
2457 struct nlattr *attrs;
2458 struct nlattr *prop;
2462 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2463 nlflags, TIPC_NL_LINK_GET);
2467 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2471 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2473 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
2475 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
2477 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
2479 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
2482 if (tipc_link_is_up(link))
2483 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2486 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2489 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2492 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2494 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2496 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2499 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2501 nla_nest_end(msg->skb, prop);
2503 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2507 nla_nest_end(msg->skb, attrs);
2508 genlmsg_end(msg->skb, hdr);
2513 nla_nest_cancel(msg->skb, prop);
2515 nla_nest_cancel(msg->skb, attrs);
2517 genlmsg_cancel(msg->skb, hdr);
2522 static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
2523 struct tipc_stats *stats)
2526 struct nlattr *nest;
2533 struct nla_map map[] = {
2534 {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
2535 {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
2536 {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
2537 {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
2538 {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
2539 {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
2540 {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
2541 {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
2542 {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
2543 {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
2544 {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
2545 {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
2546 {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
2547 {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
2548 {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
2549 {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
2550 {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
2551 {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
2552 {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
2553 (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
2556 nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2560 for (i = 0; i < ARRAY_SIZE(map); i++)
2561 if (nla_put_u32(skb, map[i].key, map[i].val))
2564 nla_nest_end(skb, nest);
2568 nla_nest_cancel(skb, nest);
2573 int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
2577 struct nlattr *attrs;
2578 struct nlattr *prop;
2579 struct tipc_net *tn = net_generic(net, tipc_net_id);
2580 u32 bc_mode = tipc_bcast_get_broadcast_mode(net);
2581 u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
2582 struct tipc_link *bcl = tn->bcl;
2587 tipc_bcast_lock(net);
2589 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2590 NLM_F_MULTI, TIPC_NL_LINK_GET);
2592 tipc_bcast_unlock(net);
2596 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2600 /* The broadcast link is always up */
2601 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2604 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
2606 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
2608 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
2610 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
2613 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2616 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
2618 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
2620 if (bc_mode & BCLINK_MODE_SEL)
2621 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
2624 nla_nest_end(msg->skb, prop);
2626 err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
2630 tipc_bcast_unlock(net);
2631 nla_nest_end(msg->skb, attrs);
2632 genlmsg_end(msg->skb, hdr);
2637 nla_nest_cancel(msg->skb, prop);
2639 nla_nest_cancel(msg->skb, attrs);
2641 tipc_bcast_unlock(net);
2642 genlmsg_cancel(msg->skb, hdr);
2647 void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
2648 struct sk_buff_head *xmitq)
2652 l->bc_rcvlink->tolerance = tol;
2654 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
2657 void tipc_link_set_prio(struct tipc_link *l, u32 prio,
2658 struct sk_buff_head *xmitq)
2661 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
2664 void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
2666 l->abort_limit = limit;
2669 char *tipc_link_name_ext(struct tipc_link *l, char *buf)
2672 scnprintf(buf, TIPC_MAX_LINK_NAME, "null");
2673 else if (link_is_bc_sndlink(l))
2674 scnprintf(buf, TIPC_MAX_LINK_NAME, "broadcast-sender");
2675 else if (link_is_bc_rcvlink(l))
2676 scnprintf(buf, TIPC_MAX_LINK_NAME,
2677 "broadcast-receiver, peer %x", l->addr);
2679 memcpy(buf, l->name, TIPC_MAX_LINK_NAME);
2685 * tipc_link_dump - dump TIPC link data
2686 * @l: tipc link to be dumped
2687 * @dqueues: bitmask to decide if any link queue to be dumped?
2688 * - TIPC_DUMP_NONE: don't dump link queues
2689 * - TIPC_DUMP_TRANSMQ: dump link transmq queue
2690 * - TIPC_DUMP_BACKLOGQ: dump link backlog queue
2691 * - TIPC_DUMP_DEFERDQ: dump link deferd queue
2692 * - TIPC_DUMP_INPUTQ: dump link input queue
2693 * - TIPC_DUMP_WAKEUP: dump link wakeup queue
2694 * - TIPC_DUMP_ALL: dump all the link queues above
2695 * @buf: returned buffer of dump data in format
2697 int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
2700 size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
2701 struct sk_buff_head *list;
2702 struct sk_buff *hskb, *tskb;
2706 i += scnprintf(buf, sz, "link data: (null)\n");
2710 i += scnprintf(buf, sz, "link data: %x", l->addr);
2711 i += scnprintf(buf + i, sz - i, " %x", l->state);
2712 i += scnprintf(buf + i, sz - i, " %u", l->in_session);
2713 i += scnprintf(buf + i, sz - i, " %u", l->session);
2714 i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
2715 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
2716 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
2717 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
2718 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
2719 i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
2720 i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
2721 i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
2722 i += scnprintf(buf + i, sz - i, " %u", 0);
2723 i += scnprintf(buf + i, sz - i, " %u", 0);
2724 i += scnprintf(buf + i, sz - i, " %u", l->acked);
2727 len = skb_queue_len(list);
2728 hskb = skb_peek(list);
2729 tskb = skb_peek_tail(list);
2730 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2731 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2732 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2735 len = skb_queue_len(list);
2736 hskb = skb_peek(list);
2737 tskb = skb_peek_tail(list);
2738 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2739 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2740 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2742 list = &l->backlogq;
2743 len = skb_queue_len(list);
2744 hskb = skb_peek(list);
2745 tskb = skb_peek_tail(list);
2746 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2747 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2748 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2751 len = skb_queue_len(list);
2752 hskb = skb_peek(list);
2753 tskb = skb_peek_tail(list);
2754 i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
2755 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2756 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2758 if (dqueues & TIPC_DUMP_TRANSMQ) {
2759 i += scnprintf(buf + i, sz - i, "transmq: ");
2760 i += tipc_list_dump(&l->transmq, false, buf + i);
2762 if (dqueues & TIPC_DUMP_BACKLOGQ) {
2763 i += scnprintf(buf + i, sz - i,
2764 "backlogq: <%u %u %u %u %u>, ",
2765 l->backlog[TIPC_LOW_IMPORTANCE].len,
2766 l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
2767 l->backlog[TIPC_HIGH_IMPORTANCE].len,
2768 l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
2769 l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
2770 i += tipc_list_dump(&l->backlogq, false, buf + i);
2772 if (dqueues & TIPC_DUMP_DEFERDQ) {
2773 i += scnprintf(buf + i, sz - i, "deferdq: ");
2774 i += tipc_list_dump(&l->deferdq, false, buf + i);
2776 if (dqueues & TIPC_DUMP_INPUTQ) {
2777 i += scnprintf(buf + i, sz - i, "inputq: ");
2778 i += tipc_list_dump(l->inputq, false, buf + i);
2780 if (dqueues & TIPC_DUMP_WAKEUP) {
2781 i += scnprintf(buf + i, sz - i, "wakeup: ");
2782 i += tipc_list_dump(&l->wakeupq, false, buf + i);