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 * @prev_from: sequence number of most previous retransmission request
110 * @stale_limit: time when repeated identical retransmits must force link reset
111 * @ackers: # of peers that needs to ack each packet before it can be released
112 * @acked: # last packet acked by a certain peer. Used for broadcast.
113 * @rcv_nxt: next sequence number to expect for inbound messages
114 * @deferred_queue: deferred queue saved OOS b'cast message received from node
115 * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
116 * @inputq: buffer queue for messages to be delivered upwards
117 * @namedq: buffer queue for name table messages to be delivered upwards
118 * @next_out: ptr to first unsent outbound message in queue
119 * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
120 * @long_msg_seq_no: next identifier to use for outbound fragmented messages
121 * @reasm_buf: head of partially reassembled inbound message fragments
122 * @bc_rcvr: marks that this is a broadcast receiver link
123 * @stats: collects statistics regarding link activity
127 char name[TIPC_MAX_LINK_NAME];
130 /* Management and link supervision data */
144 char if_name[TIPC_MAX_IF_NAME];
147 struct tipc_mon_state mon_state;
152 struct sk_buff *failover_reasm_skb;
153 struct sk_buff_head failover_deferdq;
155 /* Max packet negotiation */
160 struct sk_buff_head transmq;
161 struct sk_buff_head backlogq;
169 unsigned long stale_limit;
174 struct sk_buff_head deferdq;
175 struct sk_buff_head *inputq;
176 struct sk_buff_head *namedq;
178 /* Congestion handling */
179 struct sk_buff_head wakeupq;
181 /* Fragmentation/reassembly */
182 struct sk_buff *reasm_buf;
183 struct sk_buff *reasm_tnlmsg;
188 struct tipc_link *bc_rcvlink;
189 struct tipc_link *bc_sndlink;
194 struct tipc_stats stats;
198 * Error message prefixes
200 static const char *link_co_err = "Link tunneling error, ";
201 static const char *link_rst_msg = "Resetting link ";
203 /* Send states for broadcast NACKs
206 BC_NACK_SND_CONDITIONAL,
207 BC_NACK_SND_UNCONDITIONAL,
208 BC_NACK_SND_SUPPRESS,
211 #define TIPC_BC_RETR_LIM (jiffies + msecs_to_jiffies(10))
212 #define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
215 * Interval between NACKs when packets arrive out of order
217 #define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
222 LINK_ESTABLISHED = 0xe,
223 LINK_ESTABLISHING = 0xe << 4,
224 LINK_RESET = 0x1 << 8,
225 LINK_RESETTING = 0x2 << 12,
226 LINK_PEER_RESET = 0xd << 16,
227 LINK_FAILINGOVER = 0xf << 20,
228 LINK_SYNCHING = 0xc << 24
231 /* Link FSM state checking routines
233 static int link_is_up(struct tipc_link *l)
235 return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
238 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
239 struct sk_buff_head *xmitq);
240 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
241 bool probe_reply, u16 rcvgap,
242 int tolerance, int priority,
243 struct sk_buff_head *xmitq);
244 static void link_print(struct tipc_link *l, const char *str);
245 static int tipc_link_build_nack_msg(struct tipc_link *l,
246 struct sk_buff_head *xmitq);
247 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
248 struct sk_buff_head *xmitq);
249 static bool tipc_link_release_pkts(struct tipc_link *l, u16 to);
250 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data);
251 static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
252 struct tipc_gap_ack_blks *ga,
253 struct sk_buff_head *xmitq);
256 * Simple non-static link routines (i.e. referenced outside this file)
258 bool tipc_link_is_up(struct tipc_link *l)
260 return link_is_up(l);
263 bool tipc_link_peer_is_down(struct tipc_link *l)
265 return l->state == LINK_PEER_RESET;
268 bool tipc_link_is_reset(struct tipc_link *l)
270 return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
273 bool tipc_link_is_establishing(struct tipc_link *l)
275 return l->state == LINK_ESTABLISHING;
278 bool tipc_link_is_synching(struct tipc_link *l)
280 return l->state == LINK_SYNCHING;
283 bool tipc_link_is_failingover(struct tipc_link *l)
285 return l->state == LINK_FAILINGOVER;
288 bool tipc_link_is_blocked(struct tipc_link *l)
290 return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
293 static bool link_is_bc_sndlink(struct tipc_link *l)
295 return !l->bc_sndlink;
298 static bool link_is_bc_rcvlink(struct tipc_link *l)
300 return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
303 void tipc_link_set_active(struct tipc_link *l, bool active)
308 u32 tipc_link_id(struct tipc_link *l)
310 return l->peer_bearer_id << 16 | l->bearer_id;
313 int tipc_link_window(struct tipc_link *l)
318 int tipc_link_prio(struct tipc_link *l)
323 unsigned long tipc_link_tolerance(struct tipc_link *l)
328 struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
333 char tipc_link_plane(struct tipc_link *l)
338 void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
340 l->peer_caps = capabilities;
343 void tipc_link_add_bc_peer(struct tipc_link *snd_l,
344 struct tipc_link *uc_l,
345 struct sk_buff_head *xmitq)
347 struct tipc_link *rcv_l = uc_l->bc_rcvlink;
350 rcv_l->acked = snd_l->snd_nxt - 1;
351 snd_l->state = LINK_ESTABLISHED;
352 tipc_link_build_bc_init_msg(uc_l, xmitq);
355 void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
356 struct tipc_link *rcv_l,
357 struct sk_buff_head *xmitq)
359 u16 ack = snd_l->snd_nxt - 1;
362 rcv_l->bc_peer_is_up = true;
363 rcv_l->state = LINK_ESTABLISHED;
364 tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
365 trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
366 tipc_link_reset(rcv_l);
367 rcv_l->state = LINK_RESET;
368 if (!snd_l->ackers) {
369 trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
370 tipc_link_reset(snd_l);
371 snd_l->state = LINK_RESET;
372 __skb_queue_purge(xmitq);
376 int tipc_link_bc_peers(struct tipc_link *l)
381 static u16 link_bc_rcv_gap(struct tipc_link *l)
383 struct sk_buff *skb = skb_peek(&l->deferdq);
386 if (more(l->snd_nxt, l->rcv_nxt))
387 gap = l->snd_nxt - l->rcv_nxt;
389 gap = buf_seqno(skb) - l->rcv_nxt;
393 void tipc_link_set_mtu(struct tipc_link *l, int mtu)
398 int tipc_link_mtu(struct tipc_link *l)
403 u16 tipc_link_rcv_nxt(struct tipc_link *l)
408 u16 tipc_link_acked(struct tipc_link *l)
413 char *tipc_link_name(struct tipc_link *l)
418 u32 tipc_link_state(struct tipc_link *l)
424 * tipc_link_create - create a new link
425 * @n: pointer to associated node
426 * @if_name: associated interface name
427 * @bearer_id: id (index) of associated bearer
428 * @tolerance: link tolerance to be used by link
429 * @net_plane: network plane (A,B,c..) this link belongs to
430 * @mtu: mtu to be advertised by link
431 * @priority: priority to be used by link
432 * @window: send window to be used by link
433 * @session: session to be used by link
434 * @ownnode: identity of own node
435 * @peer: node id of peer node
436 * @peer_caps: bitmap describing peer node capabilities
437 * @bc_sndlink: the namespace global link used for broadcast sending
438 * @bc_rcvlink: the peer specific link used for broadcast reception
439 * @inputq: queue to put messages ready for delivery
440 * @namedq: queue to put binding table update messages ready for delivery
441 * @link: return value, pointer to put the created link
443 * Returns true if link was created, otherwise false
445 bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
446 int tolerance, char net_plane, u32 mtu, int priority,
447 int window, u32 session, u32 self,
448 u32 peer, u8 *peer_id, u16 peer_caps,
449 struct tipc_link *bc_sndlink,
450 struct tipc_link *bc_rcvlink,
451 struct sk_buff_head *inputq,
452 struct sk_buff_head *namedq,
453 struct tipc_link **link)
455 char peer_str[NODE_ID_STR_LEN] = {0,};
456 char self_str[NODE_ID_STR_LEN] = {0,};
459 l = kzalloc(sizeof(*l), GFP_ATOMIC);
463 l->session = session;
465 /* Set link name for unicast links only */
467 tipc_nodeid2string(self_str, tipc_own_id(net));
468 if (strlen(self_str) > 16)
469 sprintf(self_str, "%x", self);
470 tipc_nodeid2string(peer_str, peer_id);
471 if (strlen(peer_str) > 16)
472 sprintf(peer_str, "%x", peer);
474 /* Peer i/f name will be completed by reset/activate message */
475 snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
476 self_str, if_name, peer_str);
478 strcpy(l->if_name, if_name);
480 l->peer_caps = peer_caps;
482 l->in_session = false;
483 l->bearer_id = bearer_id;
484 l->tolerance = tolerance;
486 bc_rcvlink->tolerance = tolerance;
487 l->net_plane = net_plane;
488 l->advertised_mtu = mtu;
490 l->priority = priority;
491 tipc_link_set_queue_limits(l, window);
493 l->bc_sndlink = bc_sndlink;
494 l->bc_rcvlink = bc_rcvlink;
497 l->state = LINK_RESETTING;
498 __skb_queue_head_init(&l->transmq);
499 __skb_queue_head_init(&l->backlogq);
500 __skb_queue_head_init(&l->deferdq);
501 __skb_queue_head_init(&l->failover_deferdq);
502 skb_queue_head_init(&l->wakeupq);
503 skb_queue_head_init(l->inputq);
508 * tipc_link_bc_create - create new link to be used for broadcast
509 * @n: pointer to associated node
510 * @mtu: mtu to be used initially if no peers
511 * @window: send window to be used
512 * @inputq: queue to put messages ready for delivery
513 * @namedq: queue to put binding table update messages ready for delivery
514 * @link: return value, pointer to put the created link
516 * Returns true if link was created, otherwise false
518 bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
519 int mtu, int window, u16 peer_caps,
520 struct sk_buff_head *inputq,
521 struct sk_buff_head *namedq,
522 struct tipc_link *bc_sndlink,
523 struct tipc_link **link)
527 if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
528 0, ownnode, peer, NULL, peer_caps, bc_sndlink,
529 NULL, inputq, namedq, link))
533 strcpy(l->name, tipc_bclink_name);
534 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
536 l->state = LINK_RESET;
540 /* Broadcast send link is always up */
541 if (link_is_bc_sndlink(l))
542 l->state = LINK_ESTABLISHED;
544 /* Disable replicast if even a single peer doesn't support it */
545 if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
546 tipc_bcast_disable_rcast(net);
552 * tipc_link_fsm_evt - link finite state machine
553 * @l: pointer to link
554 * @evt: state machine event to be processed
556 int tipc_link_fsm_evt(struct tipc_link *l, int evt)
559 int old_state = l->state;
564 case LINK_PEER_RESET_EVT:
565 l->state = LINK_PEER_RESET;
568 l->state = LINK_RESET;
570 case LINK_FAILURE_EVT:
571 case LINK_FAILOVER_BEGIN_EVT:
572 case LINK_ESTABLISH_EVT:
573 case LINK_FAILOVER_END_EVT:
574 case LINK_SYNCH_BEGIN_EVT:
575 case LINK_SYNCH_END_EVT:
582 case LINK_PEER_RESET_EVT:
583 l->state = LINK_ESTABLISHING;
585 case LINK_FAILOVER_BEGIN_EVT:
586 l->state = LINK_FAILINGOVER;
587 case LINK_FAILURE_EVT:
589 case LINK_ESTABLISH_EVT:
590 case LINK_FAILOVER_END_EVT:
592 case LINK_SYNCH_BEGIN_EVT:
593 case LINK_SYNCH_END_EVT:
598 case LINK_PEER_RESET:
601 l->state = LINK_ESTABLISHING;
603 case LINK_PEER_RESET_EVT:
604 case LINK_ESTABLISH_EVT:
605 case LINK_FAILURE_EVT:
607 case LINK_SYNCH_BEGIN_EVT:
608 case LINK_SYNCH_END_EVT:
609 case LINK_FAILOVER_BEGIN_EVT:
610 case LINK_FAILOVER_END_EVT:
615 case LINK_FAILINGOVER:
617 case LINK_FAILOVER_END_EVT:
618 l->state = LINK_RESET;
620 case LINK_PEER_RESET_EVT:
622 case LINK_ESTABLISH_EVT:
623 case LINK_FAILURE_EVT:
625 case LINK_FAILOVER_BEGIN_EVT:
626 case LINK_SYNCH_BEGIN_EVT:
627 case LINK_SYNCH_END_EVT:
632 case LINK_ESTABLISHING:
634 case LINK_ESTABLISH_EVT:
635 l->state = LINK_ESTABLISHED;
637 case LINK_FAILOVER_BEGIN_EVT:
638 l->state = LINK_FAILINGOVER;
641 l->state = LINK_RESET;
643 case LINK_FAILURE_EVT:
644 case LINK_PEER_RESET_EVT:
645 case LINK_SYNCH_BEGIN_EVT:
646 case LINK_FAILOVER_END_EVT:
648 case LINK_SYNCH_END_EVT:
653 case LINK_ESTABLISHED:
655 case LINK_PEER_RESET_EVT:
656 l->state = LINK_PEER_RESET;
657 rc |= TIPC_LINK_DOWN_EVT;
659 case LINK_FAILURE_EVT:
660 l->state = LINK_RESETTING;
661 rc |= TIPC_LINK_DOWN_EVT;
664 l->state = LINK_RESET;
666 case LINK_ESTABLISH_EVT:
667 case LINK_SYNCH_END_EVT:
669 case LINK_SYNCH_BEGIN_EVT:
670 l->state = LINK_SYNCHING;
672 case LINK_FAILOVER_BEGIN_EVT:
673 case LINK_FAILOVER_END_EVT:
680 case LINK_PEER_RESET_EVT:
681 l->state = LINK_PEER_RESET;
682 rc |= TIPC_LINK_DOWN_EVT;
684 case LINK_FAILURE_EVT:
685 l->state = LINK_RESETTING;
686 rc |= TIPC_LINK_DOWN_EVT;
689 l->state = LINK_RESET;
691 case LINK_ESTABLISH_EVT:
692 case LINK_SYNCH_BEGIN_EVT:
694 case LINK_SYNCH_END_EVT:
695 l->state = LINK_ESTABLISHED;
697 case LINK_FAILOVER_BEGIN_EVT:
698 case LINK_FAILOVER_END_EVT:
704 pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
706 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
709 pr_err("Illegal FSM event %x in state %x on link %s\n",
710 evt, l->state, l->name);
711 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
715 /* link_profile_stats - update statistical profiling of traffic
717 static void link_profile_stats(struct tipc_link *l)
720 struct tipc_msg *msg;
723 /* Update counters used in statistical profiling of send traffic */
724 l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
725 l->stats.queue_sz_counts++;
727 skb = skb_peek(&l->transmq);
731 length = msg_size(msg);
733 if (msg_user(msg) == MSG_FRAGMENTER) {
734 if (msg_type(msg) != FIRST_FRAGMENT)
736 length = msg_size(msg_inner_hdr(msg));
738 l->stats.msg_lengths_total += length;
739 l->stats.msg_length_counts++;
741 l->stats.msg_length_profile[0]++;
742 else if (length <= 256)
743 l->stats.msg_length_profile[1]++;
744 else if (length <= 1024)
745 l->stats.msg_length_profile[2]++;
746 else if (length <= 4096)
747 l->stats.msg_length_profile[3]++;
748 else if (length <= 16384)
749 l->stats.msg_length_profile[4]++;
750 else if (length <= 32768)
751 l->stats.msg_length_profile[5]++;
753 l->stats.msg_length_profile[6]++;
757 * tipc_link_too_silent - check if link is "too silent"
758 * @l: tipc link to be checked
760 * Returns true if the link 'silent_intv_cnt' is about to reach the
761 * 'abort_limit' value, otherwise false
763 bool tipc_link_too_silent(struct tipc_link *l)
765 return (l->silent_intv_cnt + 2 > l->abort_limit);
768 /* tipc_link_timeout - perform periodic task as instructed from node timeout
770 int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
777 u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
778 u16 bc_acked = l->bc_rcvlink->acked;
779 struct tipc_mon_state *mstate = &l->mon_state;
781 trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
782 trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
784 case LINK_ESTABLISHED:
787 link_profile_stats(l);
788 tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
789 if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
790 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
791 state = bc_acked != bc_snt;
792 state |= l->bc_rcvlink->rcv_unacked;
793 state |= l->rcv_unacked;
794 state |= !skb_queue_empty(&l->transmq);
795 state |= !skb_queue_empty(&l->deferdq);
796 probe = mstate->probing;
797 probe |= l->silent_intv_cnt;
798 if (probe || mstate->monitoring)
799 l->silent_intv_cnt++;
802 setup = l->rst_cnt++ <= 4;
803 setup |= !(l->rst_cnt % 16);
806 case LINK_ESTABLISHING:
810 case LINK_PEER_RESET:
812 case LINK_FAILINGOVER:
818 if (state || probe || setup)
819 tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
825 * link_schedule_user - schedule a message sender for wakeup after congestion
827 * @hdr: header of message that is being sent
828 * Create pseudo msg to send back to user when congestion abates
830 static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
832 u32 dnode = tipc_own_addr(l->net);
833 u32 dport = msg_origport(hdr);
836 /* Create and schedule wakeup pseudo message */
837 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
838 dnode, l->addr, dport, 0, 0);
841 msg_set_dest_droppable(buf_msg(skb), true);
842 TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
843 skb_queue_tail(&l->wakeupq, skb);
844 l->stats.link_congs++;
845 trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
850 * link_prepare_wakeup - prepare users for wakeup after congestion
852 * Wake up a number of waiting users, as permitted by available space
855 static void link_prepare_wakeup(struct tipc_link *l)
857 struct sk_buff *skb, *tmp;
860 skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
861 imp = TIPC_SKB_CB(skb)->chain_imp;
862 if (l->backlog[imp].len < l->backlog[imp].limit) {
863 skb_unlink(skb, &l->wakeupq);
864 skb_queue_tail(l->inputq, skb);
865 } else if (i++ > 10) {
871 void tipc_link_reset(struct tipc_link *l)
873 struct sk_buff_head list;
875 __skb_queue_head_init(&list);
877 l->in_session = false;
878 /* Force re-synch of peer session number before establishing */
881 l->mtu = l->advertised_mtu;
883 spin_lock_bh(&l->wakeupq.lock);
884 skb_queue_splice_init(&l->wakeupq, &list);
885 spin_unlock_bh(&l->wakeupq.lock);
887 spin_lock_bh(&l->inputq->lock);
888 skb_queue_splice_init(&list, l->inputq);
889 spin_unlock_bh(&l->inputq->lock);
891 __skb_queue_purge(&l->transmq);
892 __skb_queue_purge(&l->deferdq);
893 __skb_queue_purge(&l->backlogq);
894 __skb_queue_purge(&l->failover_deferdq);
895 l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
896 l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
897 l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
898 l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
899 l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
900 kfree_skb(l->reasm_buf);
901 kfree_skb(l->reasm_tnlmsg);
902 kfree_skb(l->failover_reasm_skb);
904 l->reasm_tnlmsg = NULL;
905 l->failover_reasm_skb = NULL;
909 l->snd_nxt_state = 1;
910 l->rcv_nxt_state = 1;
912 l->silent_intv_cnt = 0;
914 l->bc_peer_is_up = false;
915 memset(&l->mon_state, 0, sizeof(l->mon_state));
916 tipc_link_reset_stats(l);
920 * tipc_link_xmit(): enqueue buffer list according to queue situation
922 * @list: chain of buffers containing message
923 * @xmitq: returned list of packets to be sent by caller
925 * Consumes the buffer chain.
926 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
927 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
929 int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
930 struct sk_buff_head *xmitq)
932 struct tipc_msg *hdr = buf_msg(skb_peek(list));
933 unsigned int maxwin = l->window;
934 int imp = msg_importance(hdr);
935 unsigned int mtu = l->mtu;
936 u16 ack = l->rcv_nxt - 1;
937 u16 seqno = l->snd_nxt;
938 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
939 struct sk_buff_head *transmq = &l->transmq;
940 struct sk_buff_head *backlogq = &l->backlogq;
941 struct sk_buff *skb, *_skb, *bskb;
942 int pkt_cnt = skb_queue_len(list);
945 if (unlikely(msg_size(hdr) > mtu)) {
946 pr_warn("Too large msg, purging xmit list %d %d %d %d %d!\n",
947 skb_queue_len(list), msg_user(hdr),
948 msg_type(hdr), msg_size(hdr), mtu);
949 skb_queue_purge(list);
953 /* Allow oversubscription of one data msg per source at congestion */
954 if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
955 if (imp == TIPC_SYSTEM_IMPORTANCE) {
956 pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
959 rc = link_schedule_user(l, hdr);
963 l->stats.sent_fragmented++;
964 l->stats.sent_fragments += pkt_cnt;
967 /* Prepare each packet for sending, and add to relevant queue: */
968 while (skb_queue_len(list)) {
969 skb = skb_peek(list);
971 msg_set_seqno(hdr, seqno);
972 msg_set_ack(hdr, ack);
973 msg_set_bcast_ack(hdr, bc_ack);
975 if (likely(skb_queue_len(transmq) < maxwin)) {
976 _skb = skb_clone(skb, GFP_ATOMIC);
978 skb_queue_purge(list);
982 __skb_queue_tail(transmq, skb);
983 /* next retransmit attempt */
984 if (link_is_bc_sndlink(l))
985 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
986 __skb_queue_tail(xmitq, _skb);
987 TIPC_SKB_CB(skb)->ackers = l->ackers;
989 l->stats.sent_pkts++;
993 if (tipc_msg_bundle(skb_peek_tail(backlogq), hdr, mtu)) {
994 kfree_skb(__skb_dequeue(list));
995 l->stats.sent_bundled++;
998 if (tipc_msg_make_bundle(&bskb, hdr, mtu, l->addr)) {
999 kfree_skb(__skb_dequeue(list));
1000 __skb_queue_tail(backlogq, bskb);
1001 l->backlog[msg_importance(buf_msg(bskb))].len++;
1002 l->stats.sent_bundled++;
1003 l->stats.sent_bundles++;
1006 l->backlog[imp].len += skb_queue_len(list);
1007 skb_queue_splice_tail_init(list, backlogq);
1013 static void tipc_link_advance_backlog(struct tipc_link *l,
1014 struct sk_buff_head *xmitq)
1016 struct sk_buff *skb, *_skb;
1017 struct tipc_msg *hdr;
1018 u16 seqno = l->snd_nxt;
1019 u16 ack = l->rcv_nxt - 1;
1020 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1022 while (skb_queue_len(&l->transmq) < l->window) {
1023 skb = skb_peek(&l->backlogq);
1026 _skb = skb_clone(skb, GFP_ATOMIC);
1029 __skb_dequeue(&l->backlogq);
1031 l->backlog[msg_importance(hdr)].len--;
1032 __skb_queue_tail(&l->transmq, skb);
1033 /* next retransmit attempt */
1034 if (link_is_bc_sndlink(l))
1035 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1037 __skb_queue_tail(xmitq, _skb);
1038 TIPC_SKB_CB(skb)->ackers = l->ackers;
1039 msg_set_seqno(hdr, seqno);
1040 msg_set_ack(hdr, ack);
1041 msg_set_bcast_ack(hdr, bc_ack);
1043 l->stats.sent_pkts++;
1050 * link_retransmit_failure() - Detect repeated retransmit failures
1051 * @l: tipc link sender
1052 * @r: tipc link receiver (= l in case of unicast)
1053 * @from: seqno of the 1st packet in retransmit request
1054 * @rc: returned code
1056 * Return: true if the repeated retransmit failures happens, otherwise
1059 static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
1062 struct sk_buff *skb = skb_peek(&l->transmq);
1063 struct tipc_msg *hdr;
1069 /* Detect repeated retransmit failures on same packet */
1070 if (r->prev_from != from) {
1071 r->prev_from = from;
1072 r->stale_limit = jiffies + msecs_to_jiffies(r->tolerance);
1073 } else if (time_after(jiffies, r->stale_limit)) {
1074 pr_warn("Retransmission failure on link <%s>\n", l->name);
1075 link_print(l, "State of link ");
1076 pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
1077 msg_user(hdr), msg_type(hdr), msg_size(hdr),
1079 pr_info("sqno %u, prev: %x, src: %x\n",
1080 msg_seqno(hdr), msg_prevnode(hdr), msg_orignode(hdr));
1082 trace_tipc_list_dump(&l->transmq, true, "retrans failure!");
1083 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "retrans failure!");
1084 trace_tipc_link_dump(r, TIPC_DUMP_NONE, "retrans failure!");
1086 if (link_is_bc_sndlink(l))
1087 *rc = TIPC_LINK_DOWN_EVT;
1089 *rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1096 /* tipc_link_bc_retrans() - retransmit zero or more packets
1097 * @l: the link to transmit on
1098 * @r: the receiving link ordering the retransmit. Same as l if unicast
1099 * @from: retransmit from (inclusive) this sequence number
1100 * @to: retransmit to (inclusive) this sequence number
1101 * xmitq: queue for accumulating the retransmitted packets
1103 static int tipc_link_bc_retrans(struct tipc_link *l, struct tipc_link *r,
1104 u16 from, u16 to, struct sk_buff_head *xmitq)
1106 struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
1107 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1108 u16 ack = l->rcv_nxt - 1;
1109 struct tipc_msg *hdr;
1117 trace_tipc_link_retrans(r, from, to, &l->transmq);
1119 if (link_retransmit_failure(l, r, from, &rc))
1122 skb_queue_walk(&l->transmq, skb) {
1124 if (less(msg_seqno(hdr), from))
1126 if (more(msg_seqno(hdr), to))
1128 if (link_is_bc_sndlink(l)) {
1129 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1131 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1133 _skb = __pskb_copy(skb, LL_MAX_HEADER + MIN_H_SIZE, GFP_ATOMIC);
1136 hdr = buf_msg(_skb);
1137 msg_set_ack(hdr, ack);
1138 msg_set_bcast_ack(hdr, bc_ack);
1139 _skb->priority = TC_PRIO_CONTROL;
1140 __skb_queue_tail(xmitq, _skb);
1141 l->stats.retransmitted++;
1146 /* tipc_data_input - deliver data and name distr msgs to upper layer
1148 * Consumes buffer if message is of right type
1149 * Node lock must be held
1151 static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
1152 struct sk_buff_head *inputq)
1154 struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
1155 struct tipc_msg *hdr = buf_msg(skb);
1157 switch (msg_user(hdr)) {
1158 case TIPC_LOW_IMPORTANCE:
1159 case TIPC_MEDIUM_IMPORTANCE:
1160 case TIPC_HIGH_IMPORTANCE:
1161 case TIPC_CRITICAL_IMPORTANCE:
1162 if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
1163 skb_queue_tail(mc_inputq, skb);
1168 skb_queue_tail(inputq, skb);
1170 case GROUP_PROTOCOL:
1171 skb_queue_tail(mc_inputq, skb);
1173 case NAME_DISTRIBUTOR:
1174 l->bc_rcvlink->state = LINK_ESTABLISHED;
1175 skb_queue_tail(l->namedq, skb);
1178 case TUNNEL_PROTOCOL:
1179 case MSG_FRAGMENTER:
1180 case BCAST_PROTOCOL:
1183 pr_warn("Dropping received illegal msg type\n");
1189 /* tipc_link_input - process packet that has passed link protocol check
1193 static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
1194 struct sk_buff_head *inputq,
1195 struct sk_buff **reasm_skb)
1197 struct tipc_msg *hdr = buf_msg(skb);
1198 struct sk_buff *iskb;
1199 struct sk_buff_head tmpq;
1200 int usr = msg_user(hdr);
1203 if (usr == MSG_BUNDLER) {
1204 skb_queue_head_init(&tmpq);
1205 l->stats.recv_bundles++;
1206 l->stats.recv_bundled += msg_msgcnt(hdr);
1207 while (tipc_msg_extract(skb, &iskb, &pos))
1208 tipc_data_input(l, iskb, &tmpq);
1209 tipc_skb_queue_splice_tail(&tmpq, inputq);
1211 } else if (usr == MSG_FRAGMENTER) {
1212 l->stats.recv_fragments++;
1213 if (tipc_buf_append(reasm_skb, &skb)) {
1214 l->stats.recv_fragmented++;
1215 tipc_data_input(l, skb, inputq);
1216 } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
1217 pr_warn_ratelimited("Unable to build fragment list\n");
1218 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1221 } else if (usr == BCAST_PROTOCOL) {
1222 tipc_bcast_lock(l->net);
1223 tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
1224 tipc_bcast_unlock(l->net);
1231 /* tipc_link_tnl_rcv() - receive TUNNEL_PROTOCOL message, drop or process the
1232 * inner message along with the ones in the old link's
1235 * @skb: TUNNEL_PROTOCOL message
1236 * @inputq: queue to put messages ready for delivery
1238 static int tipc_link_tnl_rcv(struct tipc_link *l, struct sk_buff *skb,
1239 struct sk_buff_head *inputq)
1241 struct sk_buff **reasm_skb = &l->failover_reasm_skb;
1242 struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
1243 struct sk_buff_head *fdefq = &l->failover_deferdq;
1244 struct tipc_msg *hdr = buf_msg(skb);
1245 struct sk_buff *iskb;
1250 if (msg_type(hdr) == SYNCH_MSG) {
1255 /* Not a fragment? */
1256 if (likely(!msg_nof_fragms(hdr))) {
1257 if (unlikely(!tipc_msg_extract(skb, &iskb, &ipos))) {
1258 pr_warn_ratelimited("Unable to extract msg, defq: %d\n",
1259 skb_queue_len(fdefq));
1264 /* Set fragment type for buf_append */
1265 if (msg_fragm_no(hdr) == 1)
1266 msg_set_type(hdr, FIRST_FRAGMENT);
1267 else if (msg_fragm_no(hdr) < msg_nof_fragms(hdr))
1268 msg_set_type(hdr, FRAGMENT);
1270 msg_set_type(hdr, LAST_FRAGMENT);
1272 if (!tipc_buf_append(reasm_tnlmsg, &skb)) {
1273 /* Successful but non-complete reassembly? */
1274 if (*reasm_tnlmsg || link_is_bc_rcvlink(l))
1276 pr_warn_ratelimited("Unable to reassemble tunnel msg\n");
1277 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1283 seqno = buf_seqno(iskb);
1284 if (unlikely(less(seqno, l->drop_point))) {
1288 if (unlikely(seqno != l->drop_point)) {
1289 __tipc_skb_queue_sorted(fdefq, seqno, iskb);
1294 if (!tipc_data_input(l, iskb, inputq))
1295 rc |= tipc_link_input(l, iskb, inputq, reasm_skb);
1298 } while ((iskb = __tipc_skb_dequeue(fdefq, l->drop_point)));
1303 static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
1305 bool released = false;
1306 struct sk_buff *skb, *tmp;
1308 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1309 if (more(buf_seqno(skb), acked))
1311 __skb_unlink(skb, &l->transmq);
1318 /* tipc_build_gap_ack_blks - build Gap ACK blocks
1319 * @l: tipc link that data have come with gaps in sequence if any
1320 * @data: data buffer to store the Gap ACK blocks after built
1322 * returns the actual allocated memory size
1324 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data)
1326 struct sk_buff *skb = skb_peek(&l->deferdq);
1327 struct tipc_gap_ack_blks *ga = data;
1328 u16 len, expect, seqno = 0;
1334 expect = buf_seqno(skb);
1335 skb_queue_walk(&l->deferdq, skb) {
1336 seqno = buf_seqno(skb);
1337 if (unlikely(more(seqno, expect))) {
1338 ga->gacks[n].ack = htons(expect - 1);
1339 ga->gacks[n].gap = htons(seqno - expect);
1340 if (++n >= MAX_GAP_ACK_BLKS) {
1341 pr_info_ratelimited("Too few Gap ACK blocks!\n");
1344 } else if (unlikely(less(seqno, expect))) {
1345 pr_warn("Unexpected skb in deferdq!\n");
1352 ga->gacks[n].ack = htons(seqno);
1353 ga->gacks[n].gap = 0;
1357 len = tipc_gap_ack_blks_sz(n);
1358 ga->len = htons(len);
1363 /* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
1364 * acked packets, also doing retransmissions if
1366 * @l: tipc link with transmq queue to be advanced
1367 * @acked: seqno of last packet acked by peer without any gaps before
1368 * @gap: # of gap packets
1369 * @ga: buffer pointer to Gap ACK blocks from peer
1370 * @xmitq: queue for accumulating the retransmitted packets if any
1372 * In case of a repeated retransmit failures, the call will return shortly
1373 * with a returned code (e.g. TIPC_LINK_DOWN_EVT)
1375 static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
1376 struct tipc_gap_ack_blks *ga,
1377 struct sk_buff_head *xmitq)
1379 struct sk_buff *skb, *_skb, *tmp;
1380 struct tipc_msg *hdr;
1381 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1382 u16 ack = l->rcv_nxt - 1;
1386 if (gap && link_retransmit_failure(l, l, acked + 1, &rc))
1389 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1390 seqno = buf_seqno(skb);
1393 if (less_eq(seqno, acked)) {
1395 __skb_unlink(skb, &l->transmq);
1397 } else if (less_eq(seqno, acked + gap)) {
1398 /* retransmit skb */
1399 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1401 TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
1403 _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
1406 hdr = buf_msg(_skb);
1407 msg_set_ack(hdr, ack);
1408 msg_set_bcast_ack(hdr, bc_ack);
1409 _skb->priority = TC_PRIO_CONTROL;
1410 __skb_queue_tail(xmitq, _skb);
1411 l->stats.retransmitted++;
1413 /* retry with Gap ACK blocks if any */
1414 if (!ga || n >= ga->gack_cnt)
1416 acked = ntohs(ga->gacks[n].ack);
1417 gap = ntohs(ga->gacks[n].gap);
1426 /* tipc_link_build_state_msg: prepare link state message for transmission
1428 * Note that sending of broadcast ack is coordinated among nodes, to reduce
1429 * risk of ack storms towards the sender
1431 int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1436 /* Broadcast ACK must be sent via a unicast link => defer to caller */
1437 if (link_is_bc_rcvlink(l)) {
1438 if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
1442 /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
1443 l->snd_nxt = l->rcv_nxt;
1444 return TIPC_LINK_SND_STATE;
1449 l->stats.sent_acks++;
1450 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
1454 /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
1456 void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1458 int mtyp = RESET_MSG;
1459 struct sk_buff *skb;
1461 if (l->state == LINK_ESTABLISHING)
1462 mtyp = ACTIVATE_MSG;
1464 tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
1466 /* Inform peer that this endpoint is going down if applicable */
1467 skb = skb_peek_tail(xmitq);
1468 if (skb && (l->state == LINK_RESET))
1469 msg_set_peer_stopping(buf_msg(skb), 1);
1472 /* tipc_link_build_nack_msg: prepare link nack message for transmission
1473 * Note that sending of broadcast NACK is coordinated among nodes, to
1474 * reduce the risk of NACK storms towards the sender
1476 static int tipc_link_build_nack_msg(struct tipc_link *l,
1477 struct sk_buff_head *xmitq)
1479 u32 def_cnt = ++l->stats.deferred_recv;
1480 u32 defq_len = skb_queue_len(&l->deferdq);
1483 if (link_is_bc_rcvlink(l)) {
1484 match1 = def_cnt & 0xf;
1485 match2 = tipc_own_addr(l->net) & 0xf;
1486 if (match1 == match2)
1487 return TIPC_LINK_SND_STATE;
1491 if (defq_len >= 3 && !((defq_len - 3) % 16))
1492 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
1496 /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
1497 * @l: the link that should handle the message
1499 * @xmitq: queue to place packets to be sent after this call
1501 int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1502 struct sk_buff_head *xmitq)
1504 struct sk_buff_head *defq = &l->deferdq;
1505 struct tipc_msg *hdr = buf_msg(skb);
1506 u16 seqno, rcv_nxt, win_lim;
1509 /* Verify and update link state */
1510 if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
1511 return tipc_link_proto_rcv(l, skb, xmitq);
1513 /* Don't send probe at next timeout expiration */
1514 l->silent_intv_cnt = 0;
1518 seqno = msg_seqno(hdr);
1519 rcv_nxt = l->rcv_nxt;
1520 win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
1522 if (unlikely(!link_is_up(l))) {
1523 if (l->state == LINK_ESTABLISHING)
1524 rc = TIPC_LINK_UP_EVT;
1528 /* Drop if outside receive window */
1529 if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
1530 l->stats.duplicates++;
1534 /* Forward queues and wake up waiting users */
1535 if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
1536 tipc_link_advance_backlog(l, xmitq);
1537 if (unlikely(!skb_queue_empty(&l->wakeupq)))
1538 link_prepare_wakeup(l);
1541 /* Defer delivery if sequence gap */
1542 if (unlikely(seqno != rcv_nxt)) {
1543 __tipc_skb_queue_sorted(defq, seqno, skb);
1544 rc |= tipc_link_build_nack_msg(l, xmitq);
1548 /* Deliver packet */
1550 l->stats.recv_pkts++;
1552 if (unlikely(msg_user(hdr) == TUNNEL_PROTOCOL))
1553 rc |= tipc_link_tnl_rcv(l, skb, l->inputq);
1554 else if (!tipc_data_input(l, skb, l->inputq))
1555 rc |= tipc_link_input(l, skb, l->inputq, &l->reasm_buf);
1556 if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
1557 rc |= tipc_link_build_state_msg(l, xmitq);
1558 if (unlikely(rc & ~TIPC_LINK_SND_STATE))
1560 } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
1568 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
1569 bool probe_reply, u16 rcvgap,
1570 int tolerance, int priority,
1571 struct sk_buff_head *xmitq)
1573 struct tipc_link *bcl = l->bc_rcvlink;
1574 struct sk_buff *skb;
1575 struct tipc_msg *hdr;
1576 struct sk_buff_head *dfq = &l->deferdq;
1577 bool node_up = link_is_up(bcl);
1578 struct tipc_mon_state *mstate = &l->mon_state;
1583 /* Don't send protocol message during reset or link failover */
1584 if (tipc_link_is_blocked(l))
1587 if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
1590 if (!skb_queue_empty(dfq))
1591 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1593 skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
1594 tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
1595 l->addr, tipc_own_addr(l->net), 0, 0, 0);
1600 data = msg_data(hdr);
1601 msg_set_session(hdr, l->session);
1602 msg_set_bearer_id(hdr, l->bearer_id);
1603 msg_set_net_plane(hdr, l->net_plane);
1604 msg_set_next_sent(hdr, l->snd_nxt);
1605 msg_set_ack(hdr, l->rcv_nxt - 1);
1606 msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
1607 msg_set_bc_ack_invalid(hdr, !node_up);
1608 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
1609 msg_set_link_tolerance(hdr, tolerance);
1610 msg_set_linkprio(hdr, priority);
1611 msg_set_redundant_link(hdr, node_up);
1612 msg_set_seq_gap(hdr, 0);
1613 msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
1615 if (mtyp == STATE_MSG) {
1616 if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
1617 msg_set_seqno(hdr, l->snd_nxt_state++);
1618 msg_set_seq_gap(hdr, rcvgap);
1619 msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
1620 msg_set_probe(hdr, probe);
1621 msg_set_is_keepalive(hdr, probe || probe_reply);
1622 if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
1623 glen = tipc_build_gap_ack_blks(l, data);
1624 tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
1625 msg_set_size(hdr, INT_H_SIZE + glen + dlen);
1626 skb_trim(skb, INT_H_SIZE + glen + dlen);
1627 l->stats.sent_states++;
1630 /* RESET_MSG or ACTIVATE_MSG */
1631 if (mtyp == ACTIVATE_MSG) {
1632 msg_set_dest_session_valid(hdr, 1);
1633 msg_set_dest_session(hdr, l->peer_session);
1635 msg_set_max_pkt(hdr, l->advertised_mtu);
1636 strcpy(data, l->if_name);
1637 msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
1638 skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
1641 l->stats.sent_probes++;
1643 l->stats.sent_nacks++;
1644 skb->priority = TC_PRIO_CONTROL;
1645 __skb_queue_tail(xmitq, skb);
1646 trace_tipc_proto_build(skb, false, l->name);
1649 void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
1650 struct sk_buff_head *xmitq)
1652 u32 onode = tipc_own_addr(l->net);
1653 struct tipc_msg *hdr, *ihdr;
1654 struct sk_buff_head tnlq;
1655 struct sk_buff *skb;
1656 u32 dnode = l->addr;
1658 skb_queue_head_init(&tnlq);
1659 skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
1660 INT_H_SIZE, BASIC_H_SIZE,
1661 dnode, onode, 0, 0, 0);
1663 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1668 msg_set_msgcnt(hdr, 1);
1669 msg_set_bearer_id(hdr, l->peer_bearer_id);
1671 ihdr = (struct tipc_msg *)msg_data(hdr);
1672 tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1673 BASIC_H_SIZE, dnode);
1674 msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
1675 __skb_queue_tail(&tnlq, skb);
1676 tipc_link_xmit(l, &tnlq, xmitq);
1679 /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
1680 * with contents of the link's transmit and backlog queues.
1682 void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1683 int mtyp, struct sk_buff_head *xmitq)
1685 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1686 struct sk_buff *skb, *tnlskb;
1687 struct tipc_msg *hdr, tnlhdr;
1688 struct sk_buff_head *queue = &l->transmq;
1689 struct sk_buff_head tmpxq, tnlq, frags;
1690 u16 pktlen, pktcnt, seqno = l->snd_nxt;
1691 bool pktcnt_need_update = false;
1698 skb_queue_head_init(&tnlq);
1699 skb_queue_head_init(&tmpxq);
1700 skb_queue_head_init(&frags);
1702 /* At least one packet required for safe algorithm => add dummy */
1703 skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1704 BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
1705 0, 0, TIPC_ERR_NO_PORT);
1707 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1710 skb_queue_tail(&tnlq, skb);
1711 tipc_link_xmit(l, &tnlq, &tmpxq);
1712 __skb_queue_purge(&tmpxq);
1715 * From now on, send only one single ("dummy") SYNCH message
1716 * to peer. The SYNCH message does not contain any data, just
1717 * a header conveying the synch point to the peer.
1719 if (mtyp == SYNCH_MSG && (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1720 tnlskb = tipc_msg_create(TUNNEL_PROTOCOL, SYNCH_MSG,
1721 INT_H_SIZE, 0, l->addr,
1722 tipc_own_addr(l->net),
1725 pr_warn("%sunable to create dummy SYNCH_MSG\n",
1730 hdr = buf_msg(tnlskb);
1731 syncpt = l->snd_nxt + skb_queue_len(&l->backlogq) - 1;
1732 msg_set_syncpt(hdr, syncpt);
1733 msg_set_bearer_id(hdr, l->peer_bearer_id);
1734 __skb_queue_tail(&tnlq, tnlskb);
1735 tipc_link_xmit(tnl, &tnlq, xmitq);
1739 /* Initialize reusable tunnel packet header */
1740 tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
1741 mtyp, INT_H_SIZE, l->addr);
1742 if (mtyp == SYNCH_MSG)
1743 pktcnt = l->snd_nxt - buf_seqno(skb_peek(&l->transmq));
1745 pktcnt = skb_queue_len(&l->transmq);
1746 pktcnt += skb_queue_len(&l->backlogq);
1747 msg_set_msgcnt(&tnlhdr, pktcnt);
1748 msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1750 /* Wrap each packet into a tunnel packet */
1751 skb_queue_walk(queue, skb) {
1753 if (queue == &l->backlogq)
1754 msg_set_seqno(hdr, seqno++);
1755 pktlen = msg_size(hdr);
1757 /* Tunnel link MTU is not large enough? This could be
1759 * 1) Link MTU has just changed or set differently;
1760 * 2) Or FAILOVER on the top of a SYNCH message
1762 * The 2nd case should not happen if peer supports
1763 * TIPC_TUNNEL_ENHANCED
1765 if (pktlen > tnl->mtu - INT_H_SIZE) {
1766 if (mtyp == FAILOVER_MSG &&
1767 (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1768 rc = tipc_msg_fragment(skb, &tnlhdr, tnl->mtu,
1771 pr_warn("%sunable to frag msg: rc %d\n",
1775 pktcnt += skb_queue_len(&frags) - 1;
1776 pktcnt_need_update = true;
1777 skb_queue_splice_tail_init(&frags, &tnlq);
1780 /* Unluckily, peer doesn't have TIPC_TUNNEL_ENHANCED
1781 * => Just warn it and return!
1783 pr_warn_ratelimited("%stoo large msg <%d, %d>: %d!\n",
1784 link_co_err, msg_user(hdr),
1785 msg_type(hdr), msg_size(hdr));
1789 msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
1790 tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
1792 pr_warn("%sunable to send packet\n", link_co_err);
1795 skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
1796 skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
1797 __skb_queue_tail(&tnlq, tnlskb);
1799 if (queue != &l->backlogq) {
1800 queue = &l->backlogq;
1804 if (pktcnt_need_update)
1805 skb_queue_walk(&tnlq, skb) {
1807 msg_set_msgcnt(hdr, pktcnt);
1810 tipc_link_xmit(tnl, &tnlq, xmitq);
1812 if (mtyp == FAILOVER_MSG) {
1813 tnl->drop_point = l->rcv_nxt;
1814 tnl->failover_reasm_skb = l->reasm_buf;
1815 l->reasm_buf = NULL;
1817 /* Failover the link's deferdq */
1818 if (unlikely(!skb_queue_empty(fdefq))) {
1819 pr_warn("Link failover deferdq not empty: %d!\n",
1820 skb_queue_len(fdefq));
1821 __skb_queue_purge(fdefq);
1823 skb_queue_splice_init(&l->deferdq, fdefq);
1828 * tipc_link_failover_prepare() - prepare tnl for link failover
1830 * This is a special version of the precursor - tipc_link_tnl_prepare(),
1831 * see the tipc_node_link_failover() for details
1835 * @xmitq: queue for messages to be xmited
1837 void tipc_link_failover_prepare(struct tipc_link *l, struct tipc_link *tnl,
1838 struct sk_buff_head *xmitq)
1840 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1842 tipc_link_create_dummy_tnl_msg(tnl, xmitq);
1844 /* This failover link enpoint was never established before,
1845 * so it has not received anything from peer.
1846 * Otherwise, it must be a normal failover situation or the
1847 * node has entered SELF_DOWN_PEER_LEAVING and both peer nodes
1848 * would have to start over from scratch instead.
1850 tnl->drop_point = 1;
1851 tnl->failover_reasm_skb = NULL;
1853 /* Initiate the link's failover deferdq */
1854 if (unlikely(!skb_queue_empty(fdefq))) {
1855 pr_warn("Link failover deferdq not empty: %d!\n",
1856 skb_queue_len(fdefq));
1857 __skb_queue_purge(fdefq);
1861 /* tipc_link_validate_msg(): validate message against current link state
1862 * Returns true if message should be accepted, otherwise false
1864 bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
1866 u16 curr_session = l->peer_session;
1867 u16 session = msg_session(hdr);
1868 int mtyp = msg_type(hdr);
1870 if (msg_user(hdr) != LINK_PROTOCOL)
1877 /* Accept only RESET with new session number */
1878 return more(session, curr_session);
1882 /* Accept only ACTIVATE with new or current session number */
1883 return !less(session, curr_session);
1885 /* Accept only STATE with current session number */
1888 if (session != curr_session)
1890 /* Extra sanity check */
1891 if (!link_is_up(l) && msg_ack(hdr))
1893 if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
1895 /* Accept only STATE with new sequence number */
1896 return !less(msg_seqno(hdr), l->rcv_nxt_state);
1902 /* tipc_link_proto_rcv(): receive link level protocol message :
1903 * Note that network plane id propagates through the network, and may
1904 * change at any time. The node with lowest numerical id determines
1907 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
1908 struct sk_buff_head *xmitq)
1910 struct tipc_msg *hdr = buf_msg(skb);
1911 struct tipc_gap_ack_blks *ga = NULL;
1913 u16 ack = msg_ack(hdr);
1914 u16 gap = msg_seq_gap(hdr);
1915 u16 peers_snd_nxt = msg_next_sent(hdr);
1916 u16 peers_tol = msg_link_tolerance(hdr);
1917 u16 peers_prio = msg_linkprio(hdr);
1918 u16 rcv_nxt = l->rcv_nxt;
1919 u16 dlen = msg_data_sz(hdr);
1920 int mtyp = msg_type(hdr);
1921 bool reply = msg_probe(hdr);
1927 trace_tipc_proto_rcv(skb, false, l->name);
1928 if (tipc_link_is_blocked(l) || !xmitq)
1931 if (tipc_own_addr(l->net) > msg_prevnode(hdr))
1932 l->net_plane = msg_net_plane(hdr);
1936 data = msg_data(hdr);
1938 if (!tipc_link_validate_msg(l, hdr)) {
1939 trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
1940 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
1947 /* Complete own link name with peer's interface name */
1948 if_name = strrchr(l->name, ':') + 1;
1949 if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
1951 if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
1953 strncpy(if_name, data, TIPC_MAX_IF_NAME);
1955 /* Update own tolerance if peer indicates a non-zero value */
1956 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
1957 l->tolerance = peers_tol;
1958 l->bc_rcvlink->tolerance = peers_tol;
1960 /* Update own priority if peer's priority is higher */
1961 if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
1962 l->priority = peers_prio;
1964 /* If peer is going down we want full re-establish cycle */
1965 if (msg_peer_stopping(hdr)) {
1966 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1970 /* If this endpoint was re-created while peer was ESTABLISHING
1971 * it doesn't know current session number. Force re-synch.
1973 if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
1974 l->session != msg_dest_session(hdr)) {
1975 if (less(l->session, msg_dest_session(hdr)))
1976 l->session = msg_dest_session(hdr) + 1;
1980 /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
1981 if (mtyp == RESET_MSG || !link_is_up(l))
1982 rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
1984 /* ACTIVATE_MSG takes up link if it was already locally reset */
1985 if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
1986 rc = TIPC_LINK_UP_EVT;
1988 l->peer_session = msg_session(hdr);
1989 l->in_session = true;
1990 l->peer_bearer_id = msg_bearer_id(hdr);
1991 if (l->mtu > msg_max_pkt(hdr))
1992 l->mtu = msg_max_pkt(hdr);
1996 l->rcv_nxt_state = msg_seqno(hdr) + 1;
1998 /* Update own tolerance if peer indicates a non-zero value */
1999 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
2000 l->tolerance = peers_tol;
2001 l->bc_rcvlink->tolerance = peers_tol;
2003 /* Update own prio if peer indicates a different value */
2004 if ((peers_prio != l->priority) &&
2005 in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
2006 l->priority = peers_prio;
2007 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2010 l->silent_intv_cnt = 0;
2011 l->stats.recv_states++;
2013 l->stats.recv_probes++;
2015 if (!link_is_up(l)) {
2016 if (l->state == LINK_ESTABLISHING)
2017 rc = TIPC_LINK_UP_EVT;
2021 /* Receive Gap ACK blocks from peer if any */
2022 if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
2023 ga = (struct tipc_gap_ack_blks *)data;
2024 glen = ntohs(ga->len);
2025 /* sanity check: if failed, ignore Gap ACK blocks */
2026 if (glen != tipc_gap_ack_blks_sz(ga->gack_cnt))
2030 tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
2031 &l->mon_state, l->bearer_id);
2033 /* Send NACK if peer has sent pkts we haven't received yet */
2034 if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
2035 rcvgap = peers_snd_nxt - l->rcv_nxt;
2036 if (rcvgap || reply)
2037 tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
2038 rcvgap, 0, 0, xmitq);
2040 rc |= tipc_link_advance_transmq(l, ack, gap, ga, xmitq);
2042 /* If NACK, retransmit will now start at right position */
2044 l->stats.recv_nacks++;
2046 tipc_link_advance_backlog(l, xmitq);
2047 if (unlikely(!skb_queue_empty(&l->wakeupq)))
2048 link_prepare_wakeup(l);
2055 /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
2057 static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
2059 struct sk_buff_head *xmitq)
2061 struct sk_buff *skb;
2062 struct tipc_msg *hdr;
2063 struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
2064 u16 ack = l->rcv_nxt - 1;
2065 u16 gap_to = peers_snd_nxt - 1;
2067 skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
2068 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
2072 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
2073 msg_set_bcast_ack(hdr, ack);
2074 msg_set_bcgap_after(hdr, ack);
2076 gap_to = buf_seqno(dfrd_skb) - 1;
2077 msg_set_bcgap_to(hdr, gap_to);
2078 msg_set_non_seq(hdr, bcast);
2079 __skb_queue_tail(xmitq, skb);
2083 /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
2085 * Give a newly added peer node the sequence number where it should
2086 * start receiving and acking broadcast packets.
2088 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
2089 struct sk_buff_head *xmitq)
2091 struct sk_buff_head list;
2093 __skb_queue_head_init(&list);
2094 if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
2096 msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
2097 tipc_link_xmit(l, &list, xmitq);
2100 /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
2102 void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
2104 int mtyp = msg_type(hdr);
2105 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2110 if (msg_user(hdr) == BCAST_PROTOCOL) {
2111 l->rcv_nxt = peers_snd_nxt;
2112 l->state = LINK_ESTABLISHED;
2116 if (l->peer_caps & TIPC_BCAST_SYNCH)
2119 if (msg_peer_node_is_up(hdr))
2122 /* Compatibility: accept older, less safe initial synch data */
2123 if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
2124 l->rcv_nxt = peers_snd_nxt;
2127 /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
2129 int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
2130 struct sk_buff_head *xmitq)
2132 struct tipc_link *snd_l = l->bc_sndlink;
2133 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2134 u16 from = msg_bcast_ack(hdr) + 1;
2135 u16 to = from + msg_bc_gap(hdr) - 1;
2141 if (!msg_peer_node_is_up(hdr))
2144 /* Open when peer ackowledges our bcast init msg (pkt #1) */
2146 l->bc_peer_is_up = true;
2148 if (!l->bc_peer_is_up)
2151 l->stats.recv_nacks++;
2153 /* Ignore if peers_snd_nxt goes beyond receive window */
2154 if (more(peers_snd_nxt, l->rcv_nxt + l->window))
2157 rc = tipc_link_bc_retrans(snd_l, l, from, to, xmitq);
2159 l->snd_nxt = peers_snd_nxt;
2160 if (link_bc_rcv_gap(l))
2161 rc |= TIPC_LINK_SND_STATE;
2163 /* Return now if sender supports nack via STATE messages */
2164 if (l->peer_caps & TIPC_BCAST_STATE_NACK)
2167 /* Otherwise, be backwards compatible */
2169 if (!more(peers_snd_nxt, l->rcv_nxt)) {
2170 l->nack_state = BC_NACK_SND_CONDITIONAL;
2174 /* Don't NACK if one was recently sent or peeked */
2175 if (l->nack_state == BC_NACK_SND_SUPPRESS) {
2176 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2180 /* Conditionally delay NACK sending until next synch rcv */
2181 if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
2182 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2183 if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
2187 /* Send NACK now but suppress next one */
2188 tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
2189 l->nack_state = BC_NACK_SND_SUPPRESS;
2193 void tipc_link_bc_ack_rcv(struct tipc_link *l, u16 acked,
2194 struct sk_buff_head *xmitq)
2196 struct sk_buff *skb, *tmp;
2197 struct tipc_link *snd_l = l->bc_sndlink;
2199 if (!link_is_up(l) || !l->bc_peer_is_up)
2202 if (!more(acked, l->acked))
2205 trace_tipc_link_bc_ack(l, l->acked, acked, &snd_l->transmq);
2206 /* Skip over packets peer has already acked */
2207 skb_queue_walk(&snd_l->transmq, skb) {
2208 if (more(buf_seqno(skb), l->acked))
2212 /* Update/release the packets peer is acking now */
2213 skb_queue_walk_from_safe(&snd_l->transmq, skb, tmp) {
2214 if (more(buf_seqno(skb), acked))
2216 if (!--TIPC_SKB_CB(skb)->ackers) {
2217 __skb_unlink(skb, &snd_l->transmq);
2222 tipc_link_advance_backlog(snd_l, xmitq);
2223 if (unlikely(!skb_queue_empty(&snd_l->wakeupq)))
2224 link_prepare_wakeup(snd_l);
2227 /* tipc_link_bc_nack_rcv(): receive broadcast nack message
2228 * This function is here for backwards compatibility, since
2229 * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
2231 int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
2232 struct sk_buff_head *xmitq)
2234 struct tipc_msg *hdr = buf_msg(skb);
2235 u32 dnode = msg_destnode(hdr);
2236 int mtyp = msg_type(hdr);
2237 u16 acked = msg_bcast_ack(hdr);
2238 u16 from = acked + 1;
2239 u16 to = msg_bcgap_to(hdr);
2240 u16 peers_snd_nxt = to + 1;
2245 if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
2248 if (mtyp != STATE_MSG)
2251 if (dnode == tipc_own_addr(l->net)) {
2252 tipc_link_bc_ack_rcv(l, acked, xmitq);
2253 rc = tipc_link_bc_retrans(l->bc_sndlink, l, from, to, xmitq);
2254 l->stats.recv_nacks++;
2258 /* Msg for other node => suppress own NACK at next sync if applicable */
2259 if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
2260 l->nack_state = BC_NACK_SND_SUPPRESS;
2265 void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
2267 int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
2270 l->backlog[TIPC_LOW_IMPORTANCE].limit = max_t(u16, 50, win);
2271 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = max_t(u16, 100, win * 2);
2272 l->backlog[TIPC_HIGH_IMPORTANCE].limit = max_t(u16, 150, win * 3);
2273 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = max_t(u16, 200, win * 4);
2274 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
2278 * link_reset_stats - reset link statistics
2279 * @l: pointer to link
2281 void tipc_link_reset_stats(struct tipc_link *l)
2283 memset(&l->stats, 0, sizeof(l->stats));
2286 static void link_print(struct tipc_link *l, const char *str)
2288 struct sk_buff *hskb = skb_peek(&l->transmq);
2289 u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
2290 u16 tail = l->snd_nxt - 1;
2292 pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
2293 pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
2294 skb_queue_len(&l->transmq), head, tail,
2295 skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
2298 /* Parse and validate nested (link) properties valid for media, bearer and link
2300 int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2304 err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
2305 tipc_nl_prop_policy, NULL);
2309 if (props[TIPC_NLA_PROP_PRIO]) {
2312 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2313 if (prio > TIPC_MAX_LINK_PRI)
2317 if (props[TIPC_NLA_PROP_TOL]) {
2320 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2321 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2325 if (props[TIPC_NLA_PROP_WIN]) {
2328 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2329 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2336 static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
2339 struct nlattr *stats;
2346 struct nla_map map[] = {
2347 {TIPC_NLA_STATS_RX_INFO, 0},
2348 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2349 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2350 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2351 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2352 {TIPC_NLA_STATS_TX_INFO, 0},
2353 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2354 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2355 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2356 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2357 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2358 s->msg_length_counts : 1},
2359 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2360 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2361 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2362 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2363 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2364 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2365 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2366 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2367 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2368 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2369 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2370 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2371 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2372 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2373 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2374 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2375 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2376 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2377 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2378 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2379 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2380 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2381 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2384 stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2388 for (i = 0; i < ARRAY_SIZE(map); i++)
2389 if (nla_put_u32(skb, map[i].key, map[i].val))
2392 nla_nest_end(skb, stats);
2396 nla_nest_cancel(skb, stats);
2401 /* Caller should hold appropriate locks to protect the link */
2402 int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2403 struct tipc_link *link, int nlflags)
2405 u32 self = tipc_own_addr(net);
2406 struct nlattr *attrs;
2407 struct nlattr *prop;
2411 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2412 nlflags, TIPC_NL_LINK_GET);
2416 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2420 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2422 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
2424 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
2426 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
2428 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
2431 if (tipc_link_is_up(link))
2432 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2435 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2438 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2441 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2443 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2445 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2448 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2450 nla_nest_end(msg->skb, prop);
2452 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2456 nla_nest_end(msg->skb, attrs);
2457 genlmsg_end(msg->skb, hdr);
2462 nla_nest_cancel(msg->skb, prop);
2464 nla_nest_cancel(msg->skb, attrs);
2466 genlmsg_cancel(msg->skb, hdr);
2471 static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
2472 struct tipc_stats *stats)
2475 struct nlattr *nest;
2482 struct nla_map map[] = {
2483 {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
2484 {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
2485 {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
2486 {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
2487 {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
2488 {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
2489 {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
2490 {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
2491 {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
2492 {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
2493 {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
2494 {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
2495 {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
2496 {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
2497 {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
2498 {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
2499 {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
2500 {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
2501 {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
2502 (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
2505 nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2509 for (i = 0; i < ARRAY_SIZE(map); i++)
2510 if (nla_put_u32(skb, map[i].key, map[i].val))
2513 nla_nest_end(skb, nest);
2517 nla_nest_cancel(skb, nest);
2522 int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
2526 struct nlattr *attrs;
2527 struct nlattr *prop;
2528 struct tipc_net *tn = net_generic(net, tipc_net_id);
2529 u32 bc_mode = tipc_bcast_get_broadcast_mode(net);
2530 u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
2531 struct tipc_link *bcl = tn->bcl;
2536 tipc_bcast_lock(net);
2538 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2539 NLM_F_MULTI, TIPC_NL_LINK_GET);
2541 tipc_bcast_unlock(net);
2545 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2549 /* The broadcast link is always up */
2550 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2553 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
2555 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
2557 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
2559 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
2562 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2565 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
2567 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
2569 if (bc_mode & BCLINK_MODE_SEL)
2570 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
2573 nla_nest_end(msg->skb, prop);
2575 err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
2579 tipc_bcast_unlock(net);
2580 nla_nest_end(msg->skb, attrs);
2581 genlmsg_end(msg->skb, hdr);
2586 nla_nest_cancel(msg->skb, prop);
2588 nla_nest_cancel(msg->skb, attrs);
2590 tipc_bcast_unlock(net);
2591 genlmsg_cancel(msg->skb, hdr);
2596 void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
2597 struct sk_buff_head *xmitq)
2601 l->bc_rcvlink->tolerance = tol;
2603 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
2606 void tipc_link_set_prio(struct tipc_link *l, u32 prio,
2607 struct sk_buff_head *xmitq)
2610 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
2613 void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
2615 l->abort_limit = limit;
2618 char *tipc_link_name_ext(struct tipc_link *l, char *buf)
2621 scnprintf(buf, TIPC_MAX_LINK_NAME, "null");
2622 else if (link_is_bc_sndlink(l))
2623 scnprintf(buf, TIPC_MAX_LINK_NAME, "broadcast-sender");
2624 else if (link_is_bc_rcvlink(l))
2625 scnprintf(buf, TIPC_MAX_LINK_NAME,
2626 "broadcast-receiver, peer %x", l->addr);
2628 memcpy(buf, l->name, TIPC_MAX_LINK_NAME);
2634 * tipc_link_dump - dump TIPC link data
2635 * @l: tipc link to be dumped
2636 * @dqueues: bitmask to decide if any link queue to be dumped?
2637 * - TIPC_DUMP_NONE: don't dump link queues
2638 * - TIPC_DUMP_TRANSMQ: dump link transmq queue
2639 * - TIPC_DUMP_BACKLOGQ: dump link backlog queue
2640 * - TIPC_DUMP_DEFERDQ: dump link deferd queue
2641 * - TIPC_DUMP_INPUTQ: dump link input queue
2642 * - TIPC_DUMP_WAKEUP: dump link wakeup queue
2643 * - TIPC_DUMP_ALL: dump all the link queues above
2644 * @buf: returned buffer of dump data in format
2646 int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
2649 size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
2650 struct sk_buff_head *list;
2651 struct sk_buff *hskb, *tskb;
2655 i += scnprintf(buf, sz, "link data: (null)\n");
2659 i += scnprintf(buf, sz, "link data: %x", l->addr);
2660 i += scnprintf(buf + i, sz - i, " %x", l->state);
2661 i += scnprintf(buf + i, sz - i, " %u", l->in_session);
2662 i += scnprintf(buf + i, sz - i, " %u", l->session);
2663 i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
2664 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
2665 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
2666 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
2667 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
2668 i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
2669 i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
2670 i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
2671 i += scnprintf(buf + i, sz - i, " %u", l->prev_from);
2672 i += scnprintf(buf + i, sz - i, " %u", 0);
2673 i += scnprintf(buf + i, sz - i, " %u", l->acked);
2676 len = skb_queue_len(list);
2677 hskb = skb_peek(list);
2678 tskb = skb_peek_tail(list);
2679 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2680 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2681 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2684 len = skb_queue_len(list);
2685 hskb = skb_peek(list);
2686 tskb = skb_peek_tail(list);
2687 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2688 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2689 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2691 list = &l->backlogq;
2692 len = skb_queue_len(list);
2693 hskb = skb_peek(list);
2694 tskb = skb_peek_tail(list);
2695 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2696 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2697 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2700 len = skb_queue_len(list);
2701 hskb = skb_peek(list);
2702 tskb = skb_peek_tail(list);
2703 i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
2704 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2705 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2707 if (dqueues & TIPC_DUMP_TRANSMQ) {
2708 i += scnprintf(buf + i, sz - i, "transmq: ");
2709 i += tipc_list_dump(&l->transmq, false, buf + i);
2711 if (dqueues & TIPC_DUMP_BACKLOGQ) {
2712 i += scnprintf(buf + i, sz - i,
2713 "backlogq: <%u %u %u %u %u>, ",
2714 l->backlog[TIPC_LOW_IMPORTANCE].len,
2715 l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
2716 l->backlog[TIPC_HIGH_IMPORTANCE].len,
2717 l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
2718 l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
2719 i += tipc_list_dump(&l->backlogq, false, buf + i);
2721 if (dqueues & TIPC_DUMP_DEFERDQ) {
2722 i += scnprintf(buf + i, sz - i, "deferdq: ");
2723 i += tipc_list_dump(&l->deferdq, false, buf + i);
2725 if (dqueues & TIPC_DUMP_INPUTQ) {
2726 i += scnprintf(buf + i, sz - i, "inputq: ");
2727 i += tipc_list_dump(l->inputq, false, buf + i);
2729 if (dqueues & TIPC_DUMP_WAKEUP) {
2730 i += scnprintf(buf + i, sz - i, "wakeup: ");
2731 i += tipc_list_dump(&l->wakeupq, false, buf + i);